From nobody@FreeBSD.ORG  Sun Jun 25 10:28:03 2000
Return-Path: <nobody@FreeBSD.ORG>
Received: by hub.freebsd.org (Postfix, from userid 32767)
	id A7B0937B582; Sun, 25 Jun 2000 10:28:03 -0700 (PDT)
Message-Id: <20000625172803.A7B0937B582@hub.freebsd.org>
Date: Sun, 25 Jun 2000 10:28:03 -0700 (PDT)
From: jan.grant@bristol.ac.uk
Sender: nobody@FreeBSD.ORG
To: freebsd-gnats-submit@FreeBSD.org
Subject: bind(2) erroneously complains EADDRNOTAVAIL
X-Send-Pr-Version: www-1.0

>Number:         19505
>Category:       kern
>Synopsis:       bind(2) erroneously complains EADDRNOTAVAIL
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Jun 25 10:30:00 PDT 2000
>Closed-Date:    Sun Jun 25 12:51:17 PDT 2000
>Last-Modified:  Sun Jun 25 12:51:39 PDT 2000
>Originator:     jan grant
>Release:        4.0-stable
>Organization:
university of bristol
>Environment:
FreeBSD tribble.ilrt.bris.ac.uk 4.0-STABLE FreeBSD 4.0-STABLE #0: Wed Jun  7 09:34:59 BS
T 2000     cmjg@tribble.ilrt.bris.ac.uk:/usr/src/sys/compile/JAN  i386

>Description:
I've got a trivial little ip4-only program which I want to listen on
127.0.0.1.5999 only, as opposed to *.5999 (tcp socket)

It fails at the call to bind: complaining that EADDRNOTAVAIL. The same
program (modulo sin_len - missing on that system) works on Solaris,
which leads me to believe the problem isn't with my source, but with bind.

Running the program using INADDR_ANY works correctly.

I've marked this as high-priority since there are some programs (named)
that need this functionality.

>How-To-Repeat:
Trivial little program repeats the bug: compile with gcc -o test test.cc.
Email me for source.

#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

unsigned short sport = 5999;

int main(int argc, char *argv[]) {
        struct protoent * pep = getprotobyname("tcp");
        if (!pep) {
                perror("cannot getprotobyname tcp");
                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 localhost");
                return 2;
        }
        struct sockaddr_in addr;
        addr.sin_len = sizeof(addr);
        addr.sin_family = AF_INET;
        addr.sin_port = htons(sport);
        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;
        }
        // ...and listen...
        if (listen(sd, 5)) {
                perror("listen");
                return 4;
        }
        close(sd);
        return 0;
}

>Fix:


>Release-Note:
>Audit-Trail:

From: arabis <arabis@kantrip.net>
To: freebsd-gnats-submit@FreeBSD.org, jan.grant@bristol.ac.uk
Cc:  
Subject: Re: kern/19505: bind(2) erroneously complains EADDRNOTAVAIL
Date: Sun, 25 Jun 2000 19:20:07 +0100

 My fault entirely.bzeroing the sockaddr_in first fixes it.
 
 
 
State-Changed-From-To: open->closed 
State-Changed-By: wollman 
State-Changed-When: Sun Jun 25 12:51:17 PDT 2000 
State-Changed-Why:  
Submitter reports pilot error. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=19505 
>Unformatted:
