vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| On 26/12/06 17:38 -0800, Ted Unangst wrote: >>getsockname(int s, struct sockaddr *name, socklen_t *namelen); >> >>namelen is a input and output parameter. The input case is pretty >>obvious >>and I'm going to skip it. For the output case I have seen two >>different behaviours. >> >>Linux and OpenSolaris copy the size of the actual in-kernel structure >>into >>namelen regardles of the input value of namelen. >>The BSDs copy MIN(namelen,sa->sa_len) back into namelen. >> -Is it worth fixing the BSDs to set sa_len as namelen > >yes, it seems the behavior is incorrect. i'm curious what stevens's book >says. > I don't think so. This behavior is what I appreciate. Considering the following code (changed from Tobias'): $ cat test.c #include <sys/types.h> #include <sys/socket.h> #include <err.h> #include <stdio.h> #include <stdlib.h> int main(void) { int s; socklen_t len; //struct sockaddr_storage ss; unsigned char ss[2]; if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) err(1, "socket"); /* we just need the first 2 bytes of the returned struct */ len = sizeof(ss); if (getsockname(s, (struct sockaddr *) &ss, &len)) warn( "getsockname"); if (len > 2) /* Linux etc, buffer overflow? */ printf( "ss truncated\n"); if (len == 2) /* OpenBSD */ printf( "ss just large enough.\n"); exit(0); } $ The third arg of getsockname is a value-result argument. OpenBSD does exactly what we want. If Linux returns by ignoring what we request, there might be no need to initialize the 'len' variable. -- Dasn |