ConferenceRoom

FreeBSD localhost issue



Occasionally, FreeBSD machines are set up such that connections to the localhost address (127.0.0.1) do not work properly. This erroneous configuration is not supported by ConferenceRoom.

The localhost_check program tests your configuration to make sure the loopback network interface is correctly configured for the standard loopback address. If your machine fails the test, contact your system or network administrator.

The source to the localhost_check program is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h> 
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

void bomb(char *a)
{
 fprintf(stderr, "Failure: %s\n", a);
 if(errno!=0) perror("System code: ");
 exit(0);
}
 
int main(void)
{
 errno=0;
 int s=socket(PF_INET, SOCK_STREAM, 0);
 if(s<0) bomb("Unable to create an INET/STREAM socket");

 struct sockaddr_in sockaddr;
 memset(&sockaddr, 0, sizeof(sockaddr_in));
 sockaddr.sin_family=AF_INET;
 sockaddr.sin_port=0;
 sockaddr.sin_addr.s_addr=htonl(0x7f000001);
 socklen_t i=sizeof(sockaddr);

 errno=0;
 if(bind(s, (struct sockaddr *)  &sockaddr, i)!=0)
  bomb("Unable to bind to localhost");

 i=sizeof(sockaddr);

 errno=0;
 if(getsockname(s, (struct sockaddr *) &sockaddr, &i)!=0)
  bomb("Unable to get socket local information");
 if(sockaddr.sin_port==0) bomb("Port is zero");

 errno=0;
 if(listen(s, 1)!=0) bomb("Unable to listen");

 errno=0;
 int s2=socket(PF_INET, SOCK_STREAM, 0);
 if(s2<0) bomb("Unable to create a second INET/STREAM socket");

 errno=0;
 if(connect(s2, (struct sockaddr *) &sockaddr, i)!=0)
  bomb("Unable to connect the two sockets");

 errno=0;
 i=sizeof(sockaddr);
 int s3=accept(s, (struct sockaddr *) &sockaddr, &i);
 if(s3<0) bomb("Unable to accept connection");

 errno=0;
 if(write(s3, "test", 4)!=4) bomb("Unable to send data");

 char buf[4];
 errno=0;
 if(read(s2, buf, 4)!=4) bomb("Unable to receive data");

 printf("Congratulations, everything checks out okay\n");
}

Maintained by David Schwartz, questions may be sent to WebMaster Support

Contents Copyright (C) 2002-3, WebMaster