home *** CD-ROM | disk | FTP | other *** search
/ Informática Multimedia: Special Games (Alt) / INFESPGAMES.iso / os2 / backgam / source / opensock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-17  |  2.4 KB  |  70 lines

  1. /*************************************************************
  2.  *    ______                                                 *
  3.  *   /     /\  TinyFugue was derived from a client initially *
  4.  *  /   __/  \ written by Anton Rang (Tarrant) and later     *
  5.  *  |  / /\  | modified by Leo Plotkin (Grod).  The early    *
  6.  *  |  |/    | versions of TinyFugue written by Greg Hudson  *
  7.  *  |  X__/  | (Explorer_Bob).  The current version is       *
  8.  *  \ /      / written and maintained by Ken Keys (Hawkeye), *
  9.  *   \______/  who can be reached at kkeys@ucsd.edu.         *
  10.  *                                                           *
  11.  *             No copyright 1992, no rights reserved.        *
  12.  *             Fugue is in the public domain.                *
  13.  *************************************************************/
  14.  
  15. /***********************************************
  16.  * Fugue socket opener                         *
  17.  *                                             *
  18.  * Used by socket.c or tf.connect.c to open    *
  19.  * a socket and find the host.                 *
  20.  *                                             *
  21.  * If this were a .c file, some linkers would  *
  22.  * link the object file into the tf executable *
  23.  * even if it's not used.                      *
  24.  ***********************************************/
  25.  
  26. #include <sys/socket.h>
  27.  
  28. static int get_host_address(name, addr)
  29.     char *name;
  30.     struct in_addr *addr;
  31. {
  32.     extern struct hostent *gethostbyname();
  33.     extern unsigned long inet_addr();
  34.     struct hostent *blob;
  35.     union {
  36.         long signed_thingy;
  37.         unsigned long unsigned_thingy;
  38.     } thingy;
  39.  
  40.     if (isdigit(*name)) {           /* IP address. */
  41.         thingy.unsigned_thingy = addr->s_addr = inet_addr(name);
  42.         if (thingy.signed_thingy == -1) return 0;
  43.     } else {                        /* Host name. */
  44.         if ((blob = gethostbyname(name)) == NULL) return 0;
  45.         bcopy(blob->h_addr, addr, sizeof(struct in_addr));
  46.     }
  47.     return 1;
  48. }
  49.  
  50. static int open_sock(host, port, addr, tfcerrnop)
  51.     char *host, *port;
  52.     struct sockaddr_in *addr;
  53.     int *tfcerrnop;
  54. {
  55.     int fd;
  56.  
  57.     *tfcerrnop = TFC_OK;
  58.     addr->sin_family = AF_INET;
  59.     addr->sin_port = htons(atoi(port));
  60.  
  61.     if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  62.         *tfcerrnop = TFC_ESOCKET;
  63.     } else if (!get_host_address(host, &addr->sin_addr)) {
  64.         close(fd);
  65.         fd = -1;
  66.         *tfcerrnop = TFC_CANT_FIND;
  67.     }
  68.     return fd;
  69. }
  70.