From owner-freebsd-questions Sun Jun 25 10: 7:53 2000 Delivered-To: freebsd-questions@freebsd.org Received: from dire.bris.ac.uk (dire.bris.ac.uk [137.222.10.60]) by hub.freebsd.org (Postfix) with ESMTP id 524F537B6DD; Sun, 25 Jun 2000 10:07:34 -0700 (PDT) (envelope-from Jan.Grant@bristol.ac.uk) Received: from mail.ilrt.bris.ac.uk by dire.bris.ac.uk with SMTP-PRIV with ESMTP; Sun, 25 Jun 2000 18:07:05 +0100 Received: from localhost (cmjg@localhost) by mail.ilrt.bris.ac.uk (8.8.7/8.8.8) with ESMTP id SAA04939; Sun, 25 Jun 2000 18:07:04 +0100 (BST) Date: Sun, 25 Jun 2000 18:07:04 +0100 (BST) From: Jan Grant To: questions@freebsd.org Cc: net@freebsd.org Subject: BUG? bind: Can't assign requested address (EADDRNOTAVAIL) Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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 #include #include /* for struct sockaddr_in */ #include /* For getprotobyname */ #include /* For the ntohs, etc. */ #include /* For inet_ntoa */ #include // gethostbyname #include #include #include #include //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