home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / ctrlsckt.c < prev    next >
C/C++ Source or Header  |  1990-05-11  |  5KB  |  160 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    ctrlsckt.c (Control Socket)
  6.  * Purpose:    Open a pipe device for IO
  7.  * Subroutine:    open_socket_listener()        returns: int
  8.  * Subroutine:    accept_socket_connection()    returns: struct *connectRec
  9.  * Subroutine:    close_socket()            returns: void
  10.  * Subroutine:    flush_socket()            returns: void
  11.  * Copyright:    1990 Smithsonian Astrophysical Observatory
  12.  *        You may do anything you like with this file except remove
  13.  *        this copyright.  The Smithsonian Astrophysical Observatory
  14.  *        makes no representations about the suitability of this
  15.  *        software for any purpose.  It is provided "as is" without
  16.  *        express or implied warranty.
  17.  * Modified:    {0} John Roll    initial version             1 March 1990
  18.  *        {1} MVH simplified for new connect module    10 March 1990
  19.  *        {n} <who> -- <does what> -- <when>
  20.  */
  21.  
  22. #ifndef VMS
  23.  
  24. #include <stdio.h>
  25. #include <sys/types.h>
  26. #include <sys/socket.h>        /* socket(), bind(), listen(), accept(), etc */
  27. #include <netdb.h>
  28. #include <netinet/in.h>        /* htonl(), struct sockaddr_in */
  29. #ifdef SYSV
  30. #include <string.h>        /* strlen(), strcpy(), strncpy(), strrchr() */
  31. #else
  32. #include <strings.h>        /* strlen(), strcpy(), strncpy(), rindex() */
  33. #endif
  34.  
  35. #include <X11/Xlib.h>        /* X window stuff */
  36. #include "hfiles/control.h"    /* define struct connectRec */
  37.  
  38. /*
  39.  * Subroutine:    open_socket_listener
  40.  * Purpose:    Get a socket on which to listen for connections
  41.  */
  42. int open_socket_listener ( host_name, port_address )
  43.      char **host_name;
  44.      int port_address;
  45. {
  46.   struct hostent *host_port;
  47.   struct sockaddr_in net_address;
  48.   int ipc;
  49.   static char *local_name;
  50.   char *calloc_errchk();
  51.  
  52.   bzero(&net_address, sizeof(net_address));
  53.  
  54.   /* if host name not given, use the local host name of this machine */
  55.   if( *host_name == NULL ) {
  56.     /* if local host name not yet known, get it and remember it */
  57.     if( local_name == NULL ) {
  58.       char name[100];
  59.       if( gethostname(name, 100) != 0 ) {
  60.     perror("Can't get user's hostname for socket\n  ");
  61.     return( -1 );
  62.       }
  63.       local_name = calloc_errchk(strlen(name)+2, 1, "socket host name");
  64.       (void)strncpy(local_name, name, 100);
  65.     }
  66.     *host_name = local_name;
  67.   }
  68.   /* get host port address information */
  69.   if( (host_port = gethostbyname(*host_name)) == NULL ) {
  70.     (void)fprintf(stderr, "Unknown host for socket: %d on %s\n",
  71.           port_address, *host_name);
  72.     return( -1 );
  73.   }
  74.   /* fill in the network address structure */
  75.   net_address.sin_family = AF_INET;
  76.   /* convert port address from host to network byte order */
  77.   net_address.sin_port = htonl(port_address);
  78.   bcopy(host_port->h_addr, &(net_address.sin_addr),
  79.     sizeof(net_address.sin_addr));
  80.   /* create a socket communication endpoint */
  81.   /* use full-duplex byte stream & ARPA Internet protocol */
  82.   if( (ipc = socket(PF_INET, SOCK_STREAM, 0)) < 0 ) {
  83.     perror("Cannot create a socket connection\n  ");
  84.     return( -1 );
  85.   }
  86.   /* bind the socket to the network address */
  87.   if( bind(ipc, &net_address, sizeof(net_address)) ) {
  88.     perror("cannot bind socket to host address\n  ");
  89.     return( -1 );
  90.   }
  91.   /* start  listening for connections (allowing a backlog of 5) */
  92.   if( listen(ipc, 5) ) {
  93.     perror("Cannot listen on socket\n  ");
  94.     return( -1 );
  95.   }
  96.   return( ipc );
  97. }
  98.  
  99. /*
  100.  * Subroutine:    accept_socket_connection
  101.  * Purpose:    accept a connection requested of the socket listener
  102.  */
  103. struct connectRec *accept_socket_connection ( listener )
  104.      struct connectRec *listener;
  105. {
  106.   struct sockaddr net_address;
  107.   int ipc;
  108.   int address_len;
  109.   struct connectRec *connection;
  110.   char *calloc_errchk();
  111.  
  112.   address_len = sizeof(net_address);
  113.   if( (ipc = accept(listener->fd, &net_address, &address_len)) < 0 ) {
  114.     perror("Cannot accept socket connection");
  115.     return( NULL );
  116.   }
  117.   connection = (struct connectRec *)
  118.    calloc_errchk(1, sizeof(struct connectRec), "socket descriptor");
  119.   connection->fd = ipc;
  120.   connection->type = IOP_socket;
  121.   connection->direction = listener->direction;
  122.   connection->protocol = listener->protocol;
  123.   connection->func = listener->func;
  124.   connection->name = listener->name;
  125.   connection->address = listener->address;
  126.   connection->affiliate = listener;
  127.   listener->affiliate = connection;
  128.   return( connection );
  129. }
  130.  
  131. /*
  132.  * Subroutine:    flush_socket
  133.  * Purpose:    flush all pending input on this socket connection
  134.  */
  135. void flush_socket ( ipc, socketname )
  136.      int ipc;
  137.      char *socketname;
  138. {
  139.   extern void flush_disk();
  140.  
  141.   if( socketname == NULL )
  142.     flush_disk(ipc, "socket");
  143.   else
  144.     flush_disk(ipc, socketname);
  145. }
  146.  
  147. /*
  148.  * Subroutine:    close_socket
  149.  * Purpose:    close this socket's file descriptor
  150.  */
  151. void close_socket ( ipc )
  152.      int ipc;
  153. {
  154.   if( close(ipc) )
  155.     perror("Error closing socket\n  ");
  156. }
  157.  
  158. #endif
  159.  
  160.