Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 25 Jun 2000 18:07:04 +0100 (BST)
From:      Jan Grant <Jan.Grant@bristol.ac.uk>
To:        questions@freebsd.org
Cc:        net@freebsd.org
Subject:   BUG? bind: Can't assign requested address (EADDRNOTAVAIL)
Message-ID:  <Pine.GHP.4.21.0006251747050.4776-100000@mail.ilrt.bris.ac.uk>

next in thread | raw e-mail | index | archive | help
Small application, IP4-only, and I'm stuck with something that ought to
be trivial :-(

Hmm, I'm thumping my head against a brick wall here with what should be
a simple problem. I can allocate a socket and bind it to a chosen port
(5999) with nary a hitch, if I wish to accept connections from
INADDR_ANY.

However, when I specify a particular interface to listen to (namely,
127.0.0.1) the call to bind() is failing with an EADDRNOTAVAIL.

This code runs fine on Solaris (modulo the sin_len being missing on
that platform), and I can't see what could be the problem:

Brief code snippet included; just to show what's going wrong, not as a
demonstration of good style :-)

I'd appreciate a direct CC: on any responses; thanks in advance.

jan

PS. System is 4-stable as of about 1 week ago.

// Excuse the mess: this seems to demonstrate the problem.
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>         /* for struct sockaddr_in */
#include <netdb.h>              /* For getprotobyname */
#include <sys/param.h>          /* For the ntohs, etc. */
#include <arpa/inet.h>          /* For inet_ntoa */
#include <netdb.h>                              // gethostbyname
#include <unistd.h>
#include <unistd.h>
#include <sys/socket.h>
#include <errno.h>      //errno, EINTR

int main(int argc, char *argv[]) {
        struct protoent * pep = getprotobyname("tcp");
        if (!pep) { perror("getprotobyname"); return 1; }

        int sd = socket(PF_INET, SOCK_STREAM, pep->p_proto);
        if (sd == -1) { perror("socket"); return 1; }

        struct hostent *he = gethostbyname("localhost");
        if (!he) { perror("gethostbyname"); return 2; }

        struct sockaddr_in addr;
        addr.sin_len = sizeof(addr);
        addr.sin_family = AF_INET;
        addr.sin_port = htons(5999);
        memcpy(&addr.sin_addr.s_addr,
		he->h_addr_list[0], sizeof(addr.sin_addr.s_addr));
        //addr.sin_addr.s_addr = htonl(INADDR_ANY); // this works!
        printf("%lx\n", addr.sin_addr.s_addr);          // quick check

        int one = 1;
        if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
                perror("setsockopt");
                return 2;
        }

        if (bind(sd, (const struct sockaddr *) &addr, sizeof addr)) {
                perror("bind");
                close(sd);
                return 3;
        }

        if (listen(sd, 5)) { perror("listen"); return 4; }

        close(sd);
        return 0;
}


-- 
jan grant, ILRT, University of Bristol. http://www.ilrt.bris.ac.uk/
Tel +44(0)117 9287163 Fax +44 (0)117 9287112 RFC822 jan.grant@bris.ac.uk
Ceci n'est pas une pipe |




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?Pine.GHP.4.21.0006251747050.4776-100000>