Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 19 Jan 2000 11:00:57 +0100 (CET)
From:      Oliver Fromme <olli@dorifer.heim3.tu-clausthal.de>
To:        freebsd-questions@FreeBSD.ORG
Subject:   Re: Finding an open port.
Message-ID:  <200001191000.LAA02225@dorifer.heim3.tu-clausthal.de>
In-Reply-To: <86387u$25h2$1@atlantis.rz.tu-clausthal.de>

next in thread | previous in thread | raw e-mail | index | archive | help
Laurence Berland <stuyman@confusion.net> wrote in list.freebsd-questions:
 > As some may be aware (from the post that started the rather long
 > volatile variable thread), I am attempting to write an ftp server for a
 > computer science project.  I've gotten most of the code done, only the
 > actual data module needs to be written (the Server-DTP for those
 > familiar with the protocol).  I'm trying to figure out the best way to
 > find a spare port for PASV connections, and was hoping there's a
 > slightly more elegant solution than calling bind and checking for
 > EAADDRINUSE on errno.

That's easy:  If you set the port number to 0, the system
will find a free one for you.  Here's some code for you to
experiment.  It's from an FTP implementation of mine (sorry
for using C++ style comments).

   int socke;      // socket descriptor
   struct sockaddr_in datalisten;
   int llen;       // size of "datalisten"

   //
   //   Try to open a socket for listening to the incoming
   //   data connection.
   //   First get a file descriptor for a TCP socket.
   //

   if ((socke = socket(AF_INET, SOCK_STREAM, 6)) < 0)
           err (EX_OSERR, "socket()");

   //
   //   Bind a local address to our socket for listening.
   //   To make this easier, we just copy the properties of
   //   the local part of of the existing FTP command/control
   //   connection (this is cmd->fd here).
   //   Setting the port number to zero lets bind() choose
   //   an appropriate port number for us.
   //

   llen = sizeof(datalisten);
   if (getsockname(->fd, (struct sockaddr *) &datalisten, &llen) < 0) {
           close (socke);
           err (EX_OSERR, "getsockname()");
   }
   datalisten.sin_port = 0;	//  <-- !!!
   llen = sizeof(datalisten);
   if (bind(socke, (struct sockaddr *) &datalisten, llen) < 0) {
           close (socke);
           err (EX_OSERR, "bind()");
   }

   //
   //   Find out which port number was assigned to our
   //   socket by bind().
   //

   if (getsockname(socke, (struct sockaddr *) &datalisten, &llen) < 0) {
           close (socke);
           err (EX_OSERR, "getsockname()");
   }

   //
   //   Our portnumber is now in datalisten.sin_port.
   //   Now call listen() etc...
   //


 > I'm expecting my copy of the Stevens book any day
 > now, so hopefully that'll stop my incessant babbling on these lists...

That book will help you a lot.  :)

Regards
   Oliver

-- 
Oliver Fromme, Leibnizstr. 18/61, 38678 Clausthal, Germany
(Info: finger userinfo:olli@dorifer.heim3.tu-clausthal.de)

"In jedem Stück Kohle wartet ein Diamant auf seine Geburt"
                                         (Terry Pratchett)


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200001191000.LAA02225>