home *** CD-ROM | disk | FTP | other *** search
/ Freelog 24 / Freelog024.iso / Popup / Junkbuster / bind.c < prev    next >
C/C++ Source or Header  |  1998-10-30  |  2KB  |  124 lines

  1. char *bind_rcs = "$Id: bind.c,v 2.9 1998/10/22 15:30:16 ACJC Exp $";
  2. /* Written and copyright 1997 Anonymous Coders and Junkbusters Corporation.
  3.  * Distributed under the GNU General Public License; see the README file.
  4.  * This code comes with NO WARRANTY. http://www.junkbusters.com/ht/en/gpl.html
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <sys/types.h>
  12. #include <fcntl.h>
  13. #include <sys/stat.h>
  14.  
  15. #ifdef _WIN32
  16.  
  17. #include <io.h>
  18. #include <windows.h>
  19.  
  20. #else
  21.  
  22. #include <unistd.h>
  23. #include <netinet/in.h>
  24. #include <sys/ioctl.h>
  25. #include <netdb.h> 
  26. #include <sys/socket.h>
  27. #ifndef __BEOS__
  28. #include <netinet/tcp.h>
  29. #include <arpa/inet.h>
  30. #include <sys/signal.h>
  31. #endif
  32.  
  33. #endif
  34.  
  35. extern int atoip();
  36.  
  37. long  remote_ip_long;
  38. char *remote_ip_str;
  39.  
  40. /*
  41.  * BIND-PORT (portnum)
  42.  *  if success, return file descriptor
  43.  *  if failure, returns -2 if address is in use, otherwise -1
  44.  */
  45. int    bind_port (hostnam, portnum)
  46. char    *hostnam;
  47. int     portnum;
  48. {
  49.     struct sockaddr_in inaddr;
  50.     int    fd;
  51.     int     one = 1;
  52.  
  53.     memset ((char * ) &inaddr, '\0', sizeof inaddr);
  54.  
  55.     inaddr.sin_family      = AF_INET;
  56.     inaddr.sin_addr.s_addr = atoip(hostnam);
  57.  
  58.     if(sizeof(inaddr.sin_port) == sizeof(short)) {
  59.         inaddr.sin_port = htons(portnum);
  60.     } else {
  61.         inaddr.sin_port = htonl(portnum);
  62.     }
  63.  
  64.     fd = socket(AF_INET, SOCK_STREAM, 0);
  65.  
  66.     if (fd < 0) {
  67.         return(-1);
  68.     }
  69.  
  70.     setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
  71.  
  72.     if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0) {
  73.         close (fd);
  74. #ifdef _WIN32
  75.         if (errno == WSAEADDRINUSE)
  76. #else
  77.         if (errno == EADDRINUSE)
  78. #endif
  79.         {
  80.             return(-2);
  81.         } else {
  82.             return(-1);
  83.         }
  84.     }
  85.  
  86.     while (listen(fd, 5) == -1) {
  87.         if (errno != EINTR) {
  88.             return(-1);
  89.         }
  90.     }
  91.  
  92.     return fd;
  93. }
  94.  
  95.  
  96. /* 
  97.  * ACCEPT-CONNECTION
  98.  * the argument, fd, is the value returned from bind_port
  99.  *
  100.  * when a connection is accepted, it returns the file descriptor
  101.  * for the connected port
  102.  */
  103. int    accept_connection (fd)
  104. int    fd;
  105. {
  106.     struct sockaddr raddr;
  107.     struct sockaddr_in *rap = (struct sockaddr_in *) &raddr;
  108.     int    afd, raddrlen;
  109.  
  110.     raddrlen = sizeof raddr;
  111.     do {
  112.         afd = accept (fd, &raddr, &raddrlen);
  113.     } while (afd < 1 && errno == EINTR);
  114.  
  115.     if (afd < 0) {
  116.         return(-1);
  117.     }
  118.  
  119.     remote_ip_str  = strdup(inet_ntoa(rap->sin_addr));
  120.     remote_ip_long = ntohl(rap->sin_addr.s_addr);
  121.  
  122.     return afd;
  123. }
  124.