LinuxSA Mailing list archives

Index: [thread] [date] [subject] [author]
  From: Mark Newton <newton@atdot.dotat.org>
  To  : O'Connor, Steven <Steven.O'Connor@boralenergy.com.au>
  Date: Thu, 18 Mar 1999 13:28:10 +1030 (CST)

Re: socket mysteries solved

O'Connor, Steven wrote:

 > I missed out the setting of addrlen before calling accept().

I must make a note of that for next time someone asks the same question;
The example I gave did set addrlen, but I probably should have drawn more
attention to it.  Sorry 'bout that.

 > I assumed that
 > because you pass addrlen by ref that the accept call wants to write into the
 > variable, which it does. It also reads from the variable as well, and
 > requires it to be setup prior to use .... whoever wrote accept () and
 > getpeername () must have been on the same drugs that the author of select ()
 > was on , no doubt.

One reason is to avoid buffer overflows:  The kernel needs to know
how large the bit of memory you've given it is, otherwise there'd
be nothing to stop people from handing it, say, a pointer to a char
instead of a pointer to a sockaddr_in.  The kernel can't find that
out for itself, so to avoid segfaults and buffer overflow security
exploits you need to tell it.

The reason it wants to write into the variable is that different 
address families have different sockaddr structures;  The IPv6 address
structure is of a different size to a sockaddr_in, which is different
again from a sockaddr_un (for UNIX-domain sockets), which is different
again from an IPX socket, etc.  Because you don't necessarily know 
which family the socket belongs to before you hand it to accept() or
getpeername() (not 'til you get the "family" field in the sockaddr
structure) you can't really know ahead of time how many bytes to 
expect, so the kernel tells you (and uses the value you passed in as an
upper bound).

    - mark

--------------------------------------------------------------------
I tried an internal modem,                    newton@atdot.dotat.org
     but it hurt when I walked.                          Mark Newton
----- Voice: +61-4-1958-3414 ------------- Fax: +61-8-83034403 -----

-- 
Check out the LinuxSA web pages at http://www.linuxsa.org.au/
To unsubscribe from the LinuxSA list:
  mail linuxsa-request@linuxsa.org.au with "unsubscribe" as the subject


Index: [thread] [date] [subject] [author]
Return to the LinuxSA Mailing List Information Page