Re: [POSIX & Win32API] Getting the 'routing' informations



Hello Maciej, List

Maciej Piechotka escreveu:

> PS. Sample pseudocode:

I can see the pseudo, but where is the code?


> int test_socket (SOCKET s, struct sockaddr *sockaddr, socklen_t socklen)
> {
> /* ... */
> }

You should return EXIT_FAILURE; here, or people will call your post off-topic.

> I have a binded datagram/packet socket. Is it possible to get an
> information should I send a packet with given sockaddr through this
> socket?

I'm Sorry Maciej, but your question... mmm... I can't pick a side. So many ways to read this.


> I'd like to have at most 2 codes - for BSD sockets and winsock2.

But ok... winsock2 and berkeley sockets are (kind) the same. You can just use a couple ifdefs and you're fine.

on windows, includes should be:
#include <winsock.h>

on linux:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

on windows, you have to do this extra stuff:
WSADATA wsaData;
WSAStartup(0x0202, &wsaData);

for both:

int thisSocket;
struct sockaddr_in destination;

destination.sin_family = AF_INET;
thisSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (thisSocket < 0)
{
printf("\nSocket Creation FAILED!");
return 0;
}


then, to close the socket, again we have some

windows stuff:

closesocket(thisSocket)

WSACleanup();


and linux stuff:

close(thisSocket)


Ok... that wasn't really a howto. Its just to show you that, a cross-platform implementation can work witout too many problems (with sockets...).

I usually get the cross-platform job done this way:

#ifdef __WIN32
#include <winsock.h>
#endif
#ifdef __LINUX
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void closesocket(int socket) { close(socket); }
#endif

#ifdef __WIN32
WSADATA wsaData;
#endif

#ifdef __WIN32
WSAStartup(0x0202, &wsaData);
#endif

#ifdef __WIN32
WSACleanup();
#endif

Rafael
.



Relevant Pages

  • Re: [PATCH] CONFIG_PACKET_MMAP should depend on MMU
    ... The entire data buffer is allocated as one contiguous lump in NOMMU-mode. ... +#ifndef CONFIG_MMU ... * find out where the socket buffers are so that NOMMU mmap can return the ...
    (Linux-Kernel)
  • Re: [PATCH] CONFIG_PACKET_MMAP should depend on MMU
    ... The entire data buffer is allocated as one contiguous lump in NOMMU-mode. ... +#ifndef CONFIG_MMU ... * find out where the socket buffers are so that NOMMU mmap can return the ...
    (Linux-Kernel)
  • Re: TCP-based chat program
    ... In one way, this is simpler than the client, because you don't have to ... two circular lists, LOGINLIST and CHATLIST ... do all the initialisation work for the listening socket ...
    (comp.programming)
  • Re: smtp coding problem
    ... char *emailsubject, char *emailmessage) { ... WSADATA wsaData; ... This shouldn't work for Windows where SOCKET is a handle -- and the ... system's line delimiter which on _some_ systems like Windows is CR LF, ...
    (comp.lang.c)
  • [EXPL] Remote DoS in 6tunnel
    ... Remote DoS in 6tunnel ... The socket that is opened whenever a client connects to 6tunnel is not ... connection is closed by server the ...
    (Securiteam)