From owner-freebsd-questions Mon Aug 21 11:21:27 2000 Delivered-To: freebsd-questions@freebsd.org Received: from smtp.nettoll.com (matrix.fr.uu.net [212.155.143.61]) by hub.freebsd.org (Postfix) with ESMTP id 17A3837B423 for ; Mon, 21 Aug 2000 11:21:19 -0700 (PDT) Received: by smtp.nettoll.com; Mon, 21 Aug 2000 20:15:25 +0200 (MET DST) Message-Id: <4.3.0.20000821201333.02e8c5f0@pop.free.fr> X-Sender: usebsd@pop.free.fr X-Mailer: QUALCOMM Windows Eudora Version 4.3 Date: Mon, 21 Aug 2000 20:27:27 +0200 To: "Eric J. Schwertfeger" , Evren Yurtesen From: mouss Subject: Re: allowing a user to bind a specific IP only? Cc: freebsd-questions@FreeBSD.ORG In-Reply-To: References: Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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