简体   繁体   中英

(How) Can I determine the socket family from the socket file descriptor

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.

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, ie have him register/open sockets with your API. Then you can cache whatever you want about that socket.

为什么不总是发送网络字节顺序?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM