OS X doesn't offer the SO_PROTOCOL
socket option which allows the caller to "...retrieve the socket type as an integer." (http://linux.die.net/man/7/socket)
In other words the following program builds and works under linux but won't compile under OS X:
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char **argv)
{
int c, s, type, len;
len = sizeof(type);
s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s < 0)
{
fprintf(stderr, "socket kaboom: %s\n", strerror(errno));
return 1;
}
if (getsockopt(s, SOL_SOCKET, SO_PROTOCOL, &type, &len) < 0)
{
fprintf(stderr, "getsosockopt kaboom: %s\n", strerror(errno));
return 1;
}
printf("socket type: %d\n", type);
return 0;
}
How to accomplish this under OS X?
The standard SO_TYPE
socket option, which returns values like SOCK_STREAM
(corresponding to TCP) and SOCK_DGRAM
(corresponding to UDP), should suffice. With SCTP, SOCK_STREAM
might correspond to TCP or SCTP and SO_PROTOCOL
is useful to distinguish them, but MacOS X does not support SCTP.
Unix domain sockets do not use protocol numbers; therefore, SO_TYPE
is the right choice there as well.