(How) Can I determine the socket family from the s

2020-02-29 03:31发布

问题:

I am writing an API which includes IPC functions which send data to another process which may be local or on another host. I'd really like the send function to be as simple as:

int mySendFunc(myDataThing_t* thing, int sd);

without the caller having to know -- in the immediate context of the mySendFunc() call -- whether sd leads to a local or remote process. It seems to me that if I could so something like:

switch (socketFamily(sd)) {
case AF_UNIX:
case AF_LOCAL:
   // Send without byteswapping
   break;
default:
   // Use htons() and htonl() on multi-byte values
   break;
}

It has been suggested that I might implement socketFamily() as:

unsigned short socketFamily(int sd)
{
   struct sockaddr sa;
   size_t len;
   getsockname(sd, &sa, &len);   
   return sa.sa_family;
}

But I'm a little concerned about the efficiency of getsockname() and wonder if I can afford to do it every time I send.

回答1:

See getsockname(2). You then inspect the struct sockaddr for the family.

EDIT: As a side note, its sometimes useful to query info as well, in this case info libc sockets

EDIT:

You really can't know without looking it up every time. It can't be simply cached, as the socket number can be reused by closing and reopening it. I just looked into the glibc code and it seems getsockname is simply a syscall, which could be nasty performance-wise.

But my suggestion is to use some sort of object-oriented concepts. Make the user pass a pointer to a struct you had previously returned to him, i.e. have him register/open sockets with your API. Then you can cache whatever you want about that socket.



回答2:

Why not always send in network byte order?



回答3:

If you control the client and server code I have a different suggestion, which I've used successfully in the past.

Have the first four bytes of your message be a known integer value. The receiver can then inspect the first four bytes to see if it matches the known value. If it matches, then no byte swapping is needed.

This saves you from having to do byte swapping when both machines have the same endianness.



标签: c sockets ipc