Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 21 Aug 2000 20:27:27 +0200
From:      mouss <usebsd@free.fr>
To:        "Eric J. Schwertfeger" <ejs@bfd.com>, Evren Yurtesen <yurtesen@ispro.net.tr>
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: allowing a user to bind a specific IP only?
Message-ID:  <4.3.0.20000821201333.02e8c5f0@pop.free.fr>
In-Reply-To: <Pine.BSF.4.21.0008131634310.72220-100000@harlie.bfd.com>
References:  <Pine.BSF.4.21.0008131905470.41423-100000@finland.ispro.net.tr>

next in thread | previous in thread | raw e-mail | index | archive | help
Am I missing something or what?

a process can bind to a single address by using that address in the call to 
bind().
servers generally set the address to 0 (INADDR_ANY), which means bind to all
local addresses, but you can specify the address.
the same convention applies to ports: you bind to a specific port by specifying
it in the call to bind(), and you bind to a "random" port by specifying 0 
as the port
(you can bind to a "reserved" port by using rresvport() but let's keep 
things simple).

an example code is as follows:

====
char* ipaddress;
int port;
struct sockadd_in addr;

memset((char*) &addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons((u_short) port);
if (ipaddr != NULL) {
   if (inet_aton(ipaddr, &(addr.sin_addr)) == 0) {
     your_error("bad IP address");
     exit(0); /* do you really wanna exit? */
   }
} else {
   add.sin_addr.s_addr = htonl(INADDR_ANY); /* bind to all local addresses */
error = bind(fd, (struct sockaddr*) &addr, sizeof(addr));
=====

so, if you use ipaddress=NULL, you bind to all local addresses, but if you use
ipaddress="10.1.2.3" you'll bind to this address (you must use a locally 
configured
address, otherwise you'll get an error for the TCP stack).

you can then run two processes to bind to the same port, say 9000, with one 
binding
to one address, and the other to the remaining addresses. you'll have
to use SO_REUSEADDR socket option and start the "restrictive" one first 
(the one which
binds to the specific address).


cheers,
mouss



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?4.3.0.20000821201333.02e8c5f0>