Subject: What's wrong with this code?
To: None <tech-net@NetBSD.ORG>
From: Michael C. Richardson <mcr@sandelman.ottawa.on.ca>
List: tech-net
Date: 03/22/1998 17:45:01
  I remove the sin_len stuff, and it compiles and runs fine on a Linux
machine. I can't understand why I can can't bind like this.
  This comes from hacking plug-gw to bind to only one interface.

istari-[~/src] mcr 243 >netstat -an -f inet | grep tcp
tcp        0      0  209.151.24.30.1144     209.151.0.6.22         ESTABLISHED
tcp        0      0  209.151.24.30.1120     209.151.24.17.22       ESTABLISHED
tcp        0      0  *.692                  *.*                    LISTEN
tcp        0      0  209.151.24.30.1082     209.151.0.3.22         ESTABLISHED
tcp        0      0  *.21669                *.*                    LISTEN
tcp        0      0  209.151.24.30.6001     209.151.24.30.1031     ESTABLISHED
tcp        0      0  209.151.24.30.1031     209.151.24.30.6001     ESTABLISHED
tcp        0      0  209.151.24.30.6001     209.151.24.30.1030     ESTABLISHED
tcp        0      0  209.151.24.30.1030     209.151.24.30.6001     ESTABLISHED
tcp        0      0  209.151.24.30.6001     209.151.24.30.1029     ESTABLISHED
tcp        0      0  209.151.24.30.1029     209.151.24.30.6001     ESTABLISHED
tcp        0      0  209.151.24.30.6001     209.151.24.30.1028     ESTABLISHED
tcp        0      0  209.151.24.30.1028     209.151.24.30.6001     ESTABLISHED
tcp        0      0  209.151.24.30.6001     209.151.24.30.1027     ESTABLISHED
tcp        0      0  209.151.24.30.1027     209.151.24.30.6001     ESTABLISHED
tcp        0      0  209.151.24.30.6001     209.151.24.30.1026     ESTABLISHED
tcp        0      0  209.151.24.30.1026     209.151.24.30.6001     ESTABLISHED
tcp        0      0  209.151.24.30.6001     209.151.24.30.1025     ESTABLISHED
tcp        0      0  209.151.24.30.1025     209.151.24.30.6001     ESTABLISHED
tcp        0      0  *.6001                 *.*                    LISTEN
tcp        0      0  209.151.24.30.22       209.151.24.29.1543     ESTABLISHED
tcp        0      0  *.22                   *.*                    LISTEN
tcp        0      0  *.515                  *.*                    LISTEN
tcp        0      0  *.53                   *.*                    LISTEN
tcp        0      0  *.2049                 *.*                    LISTEN
tcp        0      0  *.663                  *.*                    LISTEN
tcp        0      0  *.111                  *.*                    LISTEN

  [just to show that I have nothing bound to *.8000]

istari-[~/src] mcr 245 >cc -g -o k1 k1.c
istari-[~/src] mcr 246 >./k1
Failed to bind port 127.0.0.1:8000, Can't assign requested address

  The code:

#include <stdio.h>
#include	<sys/signal.h>
#include	<syslog.h>
#include	<errno.h>
#include	<sys/types.h>
#include	<sys/socket.h>
#include	<sys/wait.h>
#include	<netinet/in.h>
#include	<netdb.h>
#include	<sys/time.h>
#include	<sys/resource.h>
#include	<sys/fcntl.h>
#include <arpa/inet.h>

#define LLEV LOG_ERR

int main()
{
  struct sockaddr_in sa;
  int sock,sockl;
  pid_t pid;
  int boundok = 1;
  int reuse = 1;
  int devnull;
  

  int port = 8000;
  
  openlog("k1", LOG_PID, LOG_AUTH);

  sa.sin_family = AF_INET;
  
  sa.sin_addr.s_addr = inet_addr("127.0.0.1");

#if BSD4_4
  sa.sin_len  = sizeof(sa);
#endif
  sa.sin_port = htons(port);

  sock = socket(AF_INET, SOCK_STREAM, 0);
  if( sock < 0){
    fprintf(stderr,"Failed to create socket, %s\n", strerror(errno));
    exit(1);
  }
  
  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse));
  if( bind(sock, (struct sockaddr *)&sa, sizeof(sa)) == -1)
    {
      fprintf(stderr,"Failed to bind port %s:%d, %s\n",
	     inet_ntoa(sa.sin_addr), port, strerror(errno));
    }
  else
    {
      printf("okay");
    }
}