[lug] source for bind(int sockfd, struct sockaddr *my_addr, int addrlen)

Matt McIllece (1-3762) mcillece at elbonia.ast.lmco.com
Wed Apr 18 12:05:42 MDT 2001

>> I'm just using the description from the manpage.  Is it incorrect?
>> BIND(2)             Linux Programmer's Manual             BIND(2)
>>        bind - bind a name to a socket
>It's correct, but it's confusing terminology.  bind() associates an
>address, address family, and a port with a file descriptor.  This is
>usually done when you're writing network servers, just before calling
>listen(), which will begin accepting connections.

Got it.  I'll use the less confusing terms from now on.

>When you write a program in C that uses bind(), you'll need to put:
>#include "sys/types.h"
>#include "sys/socket.h"
>at the top of your source code.  If you look at these files, which are
>/usr/include/sys/types.h and /usr/include/sys/socket.h, the interface
>for the bind() function is provided in socket.h

Yes, but on my machine,

  find /usr/src -name "socket.h" -print | xargs grep -ni bind

produces only a reference to

  /usr/src/linux-2.2.12/include/asm-i386/socket.h:36:#define SO_BINDTODEVICE

which has nothing to do with the interface for bind().  I'm curious, what do you 
get on your machine?

>I don't know where KRUD's sources are, but you might as well go to the
>other source: ftp://ftp.gnu.org/gnu/glibc/glibc-2.2.2.tar.gz

Thanks for the link.

>I suspect what you'll find there is a wrapper and sanity check before
>it sends it on to the appropriate place in the kernel, wherever that

I suspected that, but it didn't turn out that way.  (see my  11:33:30 -0600 
(MDT) post - when it finally posts, that is).

>Have a look at net/socket.c in the kernel tree, for the sys_bind()
>function.  I believe things are broken up a little more by protocol
>somewhere, so have a look at inet_bind() in net/ipv4/af_inet.c, or
>raw_bind() in net/ipv4/raw.c or in any number of other places
>depending on what type of socket you're interested in learning the
>source code for.  Do a recursive grep for 'bind' in the kernel tree
>and have fun.

Thanks.  I did this earlier and posted about it, but the mail seems especially 
slow for me today.

>Of course, where you should look depends on what, specifically, you
>want the source code for.

I want the source code for bind() so I can see how it does what it does...

More information about the LUG mailing list