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

Chris Riddoch socket at peakpeak.com
Wed Apr 18 11:18:46 MDT 2001


"Matt McIllece (1-3762)" <mcillece at elbonia.ast.lmco.com> writes:
> I'm just using the description from the manpage.  Is it incorrect?
> 
> BIND(2)             Linux Programmer's Manual             BIND(2)
> 
> NAME
>        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.

> Should the source code for bind() be included with KRUD?  I searched
> everywhere under /usr and couldn't find it.  Does anyone have a more
> specific path to where it should be?

If the source code for glibc is included, then yes. Chances are, it
probably isn't installed by default - relatively few people find
digging through glibc very useful.

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

While that isn't the source code, it tells you what's involved.

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

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
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.

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

--
Chris Riddoch
socket at peakpeak.com



More information about the LUG mailing list