home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / IJB20OS2 / SOURCE / BIND.C < prev    next >
C/C++ Source or Header  |  1997-09-16  |  2KB  |  122 lines

  1. char *bind_rcs = "$Id: bind.c,v 2.7 1997/07/25 13:18:38 ACJC Exp $";
  2. /* Written and copyright by the Anonymous Coders and Junkbusters Corporation.
  3.  * Will be made available under the GNU General Public License.
  4.  * This software comes with NO WARRANTY.
  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. #include <netinet/tcp.h>
  28. #include <arpa/inet.h>
  29. #include <sys/signal.h>
  30.  
  31. #endif
  32.  
  33. extern int atoip();
  34.  
  35. long  remote_ip_long;
  36. char *remote_ip_str;
  37.  
  38. /*
  39.  * BIND-PORT (portnum)
  40.  *  if success, return file descriptor
  41.  *  if failure, returns -2 if address is in use, otherwise -1
  42.  */
  43. int    bind_port (hostnam, portnum)
  44. char    *hostnam;
  45. int     portnum;
  46. {
  47.     struct sockaddr_in inaddr;
  48.     int    fd;
  49.     int     one = 1;
  50.  
  51.     memset ((char * ) &inaddr, '\0', sizeof inaddr);
  52.  
  53.     inaddr.sin_family      = AF_INET;
  54.     inaddr.sin_addr.s_addr = atoip(hostnam);
  55.  
  56.     if(sizeof(inaddr.sin_port) == sizeof(short)) {
  57.         inaddr.sin_port = htons(portnum);
  58.     } else {
  59.         inaddr.sin_port = htonl(portnum);
  60.     }
  61.  
  62.     fd = socket(AF_INET, SOCK_STREAM, 0);
  63.  
  64.     if (fd < 0) {
  65.         return(-1);
  66.     }
  67.  
  68.     setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one));
  69.  
  70.     if (bind (fd, (struct sockaddr *)&inaddr, sizeof(inaddr)) < 0) {
  71.         close (fd);
  72. #ifdef _WIN32
  73.         if (errno == WSAEADDRINUSE)
  74. #else
  75.         if (errno == EADDRINUSE)
  76. #endif
  77.         {
  78.             return(-2);
  79.         } else {
  80.             return(-1);
  81.         }
  82.     }
  83.  
  84.     while (listen(fd, 5) == -1) {
  85.         if (errno != EINTR) {
  86.             return(-1);
  87.         }
  88.     }
  89.  
  90.     return fd;
  91. }
  92.  
  93.  
  94. /* 
  95.  * ACCEPT-CONNECTION
  96.  * the argument, fd, is the value returned from bind_port
  97.  *
  98.  * when a connection is accepted, it returns the file descriptor
  99.  * for the connected port
  100.  */
  101. int    accept_connection (fd)
  102. int    fd;
  103. {
  104.     struct sockaddr raddr;
  105.     struct sockaddr_in *rap = (struct sockaddr_in *) &raddr;
  106.     int    afd, raddrlen;
  107.  
  108.     raddrlen = sizeof raddr;
  109.     do {
  110.         afd = accept (fd, &raddr, &raddrlen);
  111.     } while (afd < 1 && errno == EINTR);
  112.  
  113.     if (afd < 0) {
  114.         return(-1);
  115.     }
  116.  
  117.     remote_ip_str  = inet_ntoa(rap->sin_addr);
  118.     remote_ip_long = ntohl(rap->sin_addr.s_addr);
  119.  
  120.     return afd;
  121. }
  122.