Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 16 Apr 2003 20:07:00 +0200
From:      Bernd Walter <ticso@cicely9.cicely.de>
To:        Jan Grant <Jan.Grant@bristol.ac.uk>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: Multiple ip-numbers in jails (fixed INADDR_ANY behaviour).
Message-ID:  <20030416180659.GD6845@cicely9.cicely.de>
In-Reply-To: <Pine.GSO.4.44.0304161532120.14291-100000@mail.ilrt.bris.ac.uk>
References:  <200304161349.h3GDnKxl008394@lurza.secnetix.de> <Pine.GSO.4.44.0304161532120.14291-100000@mail.ilrt.bris.ac.uk>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, Apr 16, 2003 at 03:34:38PM +0100, Jan Grant wrote:
> You can't have multiple processes listen on the same address and port,
> but you CAN have one listen on a specific IP and port and another listen
> on INADDR_ANY and the same port. By extension, you'd expect a _more
> specific_ binding of INADDR_ANY to override a more general one.

You *can* have multiple processes listen on the same address and port.
1) SO_REUSEPORT
2) You can pass a listen filedescriptor between processes (fork, UDS).

[74]cicely30> cat foo.c 
#include <sys/types.h>
#include <sys/socket.h>

#include <netinet/in.h>

int main (int argc, char *argv[]) {
        int listenfd, connfd;
        struct sockaddr_in cliaddr, servaddr;
        socklen_t clilen;
        int val;

        listenfd = socket(AF_INET, SOCK_STREAM, 0);
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port = htons(7000);

        val = 1;
        setsockopt(listenfd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));

        bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
        listen(listenfd, 2);

        for (;;) {
                clilen = sizeof(cliaddr);
                connfd = accept(listenfd, (struct sockaddr*)&cliaddr, &clilen);
                /* do something */
                close(connfd);
        }
}
[75]cicely30> gcc -o foo foo.c
[76]cicely30> ./foo &
[1] 35763
[77]cicely30> ./foo &
[2] 35764
[78]cicely30> netstat -an | grep 7000
tcp4       0      0  *.7000                 *.*                    LISTEN
tcp4       0      0  *.7000                 *.*                    LISTEN

-- 
B.Walter                   BWCT                http://www.bwct.de
ticso@bwct.de                                  info@bwct.de



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