From owner-freebsd-questions Wed Jan 19 2: 1: 4 2000 Delivered-To: freebsd-questions@freebsd.org Received: from dorifer.heim3.tu-clausthal.de (dorifer.heim3.tu-clausthal.de [139.174.243.252]) by hub.freebsd.org (Postfix) with ESMTP id 228B115281 for ; Wed, 19 Jan 2000 02:00:58 -0800 (PST) (envelope-from olli@dorifer.heim3.tu-clausthal.de) Received: (from olli@localhost) by dorifer.heim3.tu-clausthal.de (8.9.3/8.9.3) id LAA02225; Wed, 19 Jan 2000 11:00:57 +0100 (CET) (envelope-from olli) Date: Wed, 19 Jan 2000 11:00:57 +0100 (CET) Message-Id: <200001191000.LAA02225@dorifer.heim3.tu-clausthal.de> From: Oliver Fromme To: freebsd-questions@FreeBSD.ORG Reply-To: freebsd-questions@FreeBSD.ORG Subject: Re: Finding an open port. X-Newsgroups: list.freebsd-questions In-Reply-To: <86387u$25h2$1@atlantis.rz.tu-clausthal.de> User-Agent: tin/1.4.1-19991201 ("Polish") (UNIX) (FreeBSD/3.4-19991219-STABLE (i386)) MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Laurence Berland 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