[601] in linux-net channel archive

home help back first fref pref prev next nref lref last post

Inet Addresses in AF_INET UDP?

daemon@ATHENA.MIT.EDU (Chris Austin)
Fri Jun 30 13:46:10 1995

Date: Fri, 30 Jun 1995 08:05:38 +0000 (   )
From: Chris Austin <caustin@direct.ca>
To: linux-net@vger.rutgers.edu



Hi there,

  I'm trying to get the address from a connectionless internet UDP socket 
and I just can't seem to do it!  I've included a small snippet of code 
from what I've done so far.  Here's the output I get everytime I run it :-

DEBUG: HOST = 5.0.0.0  PORT = 0
DEBUG: network order IP = 5  host order IP = 83886080
DEBUG: dotted-quad IP = 5.0.0.0
DEBUG: addr = 83886080  ipaddr = 5.0.0.0

  If someone could please take a quick peek at this code and tell me what 
I'm missing?  Both the client and server are running thru loopback, 
although I have tried it from a remote site and got pretty much the same 
results.  I have also tried this under kernel 1.2.9, 1.3.4 and 1.3.5.
Everything compiles fine with gcc 2.6.3 with -Wall, and I've included the
correct header files, ie arpa/inet.h, netinet/in.h etc etc

net_sockfd, mud_sockfd and shutdown are globals...


void server_loop(void)
{
  fd_set input_set; /*, output_set, except_set;*/
  
  while (!shutdown)  {
    
    FD_ZERO(&input_set);
    FD_SET(net_sockfd, &input_set);
    FD_SET(mud_sockfd, &input_set);    

    errno = 0;
    
    if (select(mud_sockfd, &input_set, (fd_set *) 0, (fd_set *) 0, 0) < 0)  {
      fprintf(stderr, "Waking up to process signal\n");
    }
    else  {
      if (FD_ISSET(net_sockfd, &input_set))
        recv_net_message();
      if (FD_ISSET(mud_sockfd, &input_set))
	recv_mud_message();
    }
  }
}

void recv_net_message(void)
{
  struct sockaddr_in remote_address;
  int                numbytes, addr_len;
  char               message[MAX_NET_MESSAGE];
  unsigned long      addr;
  char               ipaddr[30];  

  numbytes = recvfrom(net_sockfd, message, MAX_NET_MESSAGE, 0, (struct sockaddr *) &remote_address, &addr_len);
  
  if (numbytes < 0)
    fprintf(stderr, "ZERO bytes received on net socket\n");
  
  fprintf(stderr, "DEBUG: HOST = %s  PORT = %d\n", inet_ntoa(remote_address.sin_addr), ntohs(remote_address.sin_port));
  
  fprintf(stderr, "DEBUG: network hex IP = %lu  host hex IP = %lu\n", remote_address.sin_addr.s_addr,
	                                                       ntohl(remote_address.sin_addr.s_addr));
  fprintf(stderr, "DEBUG: dotted-quad IP = %s\n", inet_ntoa(remote_address.sin_addr));
  
  addr = ntohl(remote_address.sin_addr.s_addr);
  
  sprintf(ipaddr, "%d.%d.%d.%d", (int) ((addr & 0xFF000000) >> 24),
	                         (int) ((addr & 0x00FF0000) >> 16),
	                         (int) ((addr & 0x0000FF00) >>  8),
	                         (int) ((addr & 0x000000FF)));
  fprintf(stderr, "DEBUG: addr = %lu  ipaddr = %s\n", addr, ipaddr);
}
  

void init_net_socket(int port)
{
  struct sockaddr_in  serv_addr;
  
  /* Open our Internet UDP socket */
  
  if ((net_sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)  {
    fprintf(stderr, "Can't open internet datagram socket.\n");
    exit(1);
  }
  
  bzero((char *) &serv_addr, sizeof(serv_addr));
  serv_addr.sin_family      = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port        = htons(port);
  
  if (bind(net_sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)  {
    fprintf(stderr, "Can't bind local address, port = %d\n", port);
    exit(1);
  }
}

void init_mud_socket(void)
{
  if ((mud_sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0)  {
    fprintf(stderr, "Can't open unix datagram socket.\n");
    exit(1);
  }
  
  unlink(MUDSOCK_PATH);
  bzero((char *) &mud_addr, sizeof(mud_addr));
  mud_addr.sun_family      = AF_UNIX;
  strcpy(mud_addr.sun_path, MUDSOCK_PATH);
  mudaddrlen = sizeof(mud_addr.sun_family) + strlen(mud_addr.sun_path);
  
  if (bind(mud_sockfd, (struct sockaddr *) &mud_addr, mudaddrlen) < 0)  {
    fprintf(stderr, "Can't bind local mud socket\n");
    exit(1);
  }
}

home help back first fref pref prev next nref lref last post