*BSD News Article 98070


Return to BSD News archive

From: mfuhr@dimensional.com (Michael Fuhr)
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: Re: Listening at a socket
Date: 19 Jun 1997 14:36:56 -0600
Organization: Dimensional Communications
Lines: 75
Message-ID: <5oc598$3qn@flatland.dimensional.com>
References: <5oblqi$4re@ui-gate.utell.co.uk>
NNTP-Posting-Host: flatland.dimensional.com
X-Newsreader: NN version 6.5.1 (NOV)
Path: euryale.cc.adfa.oz.au!newshost.carno.net.au!harbinger.cc.monash.edu.au!ns.saard.net!spasun.tpa.com.au!duster.adelaide.on.net!news.ade.connect.com.au!news.mel.connect.com.au!munnari.OZ.AU!spool.mu.edu!sol.net!spool.mu.edu!newsspool.sol.net!howland.erols.net!newsfeed.internetmci.com!dimensional.com!flatland.dimensional.com!not-for-mail
Xref: euryale.cc.adfa.oz.au comp.unix.bsd.freebsd.misc:43169

brian@shift.utell.net (Brian Somers) writes:

> This *has* to be a dumb question, but can anyone tell me
> how an OS, once it's received a packet for a given port
> X, can distinguish which process of all the processes that
> are using that port should get that packet.

This is a bit simplistic, but here goes (I'll use TCP for this example):

1.  Daemon starts on server and listens on some port, say 21 (FTP).

2.  Client starts on some host and says "I want to connect to
    server port 21".  The client gets a socket bound to some
    arbitrary port on its own host (say 5678), then connects that
    socket to server port 21.  The connection is now uniquely
    identified as this:

	client:5678  <====>  server:21

3.  Another client on the same host also wishes to connect to
    the server.  It gets a socket bound to another arbitrary port,
    say 6789.  Its connection is uniquely identified as this:

	client:6789  <====>  server:21

4.  An OS uses the remote address, remote port, local address, and
    local port to decide which process gets a particular TCP segment.

> For example, if I run an ftp server, I have my ftpd
> listening on *.21.  If I currently have 2 established
> connections from the same remote machine, I'll have two
> additional ftpd processes running (forked from the first).
> When a packet is received from the other machine destined
> for port 21, how does the OS decide which process to deliver
> the packet to ?

Your example would look something like this:

           ftpd parent
     listening on server:21

           ftpd child #1               client software
             server:21      <=====>      client:5678

           ftpd child #2               client software
             server:21      <=====>      client:6789

If the OS receives a new connection, it'll hand it to the daemon that's
listening for connections, which then forks a child to handle the
request.  Each child's connection can be uniquely identified by the
local address, local port, remote address, and remote port of the
packet in question.

To understand all this better, check out a good book on TCP/IP.  A
couple I recommend are:

    TCP/IP Illustrated, Vol I
    W. Richard Stevens
    Addison-Wesley
    ISBN 0-201-63346-9

    TCP/IP Network Administration
    Craig Hunt
    O'Reilly & Associates
    ISBN 0-937175-82-X

There's also a TCP/IP resource list at:

    http://www.qnx.com/~mphunter/tcpip_resources.html

Hope this helps.

-- 
Michael Fuhr
http://www.dimensional.com/~mfuhr/