home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / packages / gnuserv / gnuslib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-23  |  6.3 KB  |  248 lines

  1. /* -*-C-*-
  2.  Common library code for the GNU Emacs server and client.
  3.  
  4.  This file is part of GNU Emacs.
  5.  
  6.  Copying is permitted under those conditions described by the GNU
  7.  General Public License.
  8.  
  9.  Copyright (C) 1989 Free Software Foundation, Inc.
  10.  
  11.  Author: Andy Norman (ange@hplb.hpl.hp.com), based on 
  12.          'etc/server.c' and 'etc/emacsclient.c' from the 18.52 GNU
  13.          Emacs distribution.
  14.  
  15.  Please mail bugs and suggestions to the author at the above address.
  16. */
  17.  
  18. static char rcsid [] = "$Header: gnuslib.c,v 1.6 89/09/28 12:47:34 ange Exp $";
  19.  
  20. #include "gnuserv.h"
  21.  
  22. char *progname = NULL;
  23.  
  24. #ifdef SYSV_IPC
  25. /*
  26.   connect_to_ipc_server -- establish connection with server process via SYSV IPC
  27.                  Returns msqid for server if successful.
  28. */
  29. int connect_to_ipc_server()
  30. {
  31.   int s;            /* connected msqid */
  32.   key_t key;            /* message key */
  33.   char buf[BUFSIZ];        /* buffer for filename */
  34.  
  35.   sprintf(buf,"/tmp/gsrv%d",geteuid());
  36.   creat(buf,0600);
  37.   key = ftok(buf,1);
  38.  
  39.   if ((s = msgget(key,0600)) == -1) {
  40.     perror(progname);
  41.     fprintf(stderr,"%s: unable to access msg queue\n",progname);
  42.     exit(1);
  43.   }; /* if */
  44.  
  45.   return(s);
  46.  
  47. } /* connect_to_ipc_server */
  48.  
  49.  
  50. /*
  51.   disconnect_from_ipc_server -- inform the server that sending has finished,
  52.                                 and wait for its reply.
  53. */
  54. void disconnect_from_ipc_server(s,msgp,echo)
  55.      int s;
  56.      struct msgbuf *msgp;
  57.      int echo;
  58. {
  59.   int len;            /* length of received message */
  60.  
  61.   send_string(s,"\n");        /* newline terminates this message */
  62.   msgp->mtype = 1;
  63.  
  64.   if(msgsnd(s,msgp,strlen(msgp->mtext)+1,0) < 0) {
  65.     perror(progname);
  66.     fprintf(stderr,"%s: unable to send message to server\n",progname);
  67.     exit(1);
  68.   }; /* if */
  69.   
  70.   if((len = msgrcv(s,msgp,BUFSIZ,getpid(),0)) < 0) {
  71.     perror(progname);
  72.     fprintf(stderr,"%s: unable to receive message from server\n",progname);
  73.     exit(1);
  74.   }; /* if */
  75.  
  76.   if (echo) {
  77.     msgp->mtext[len] = '\0';    /* string terminate message */
  78.     printf("%s\n",msgp->mtext);
  79.   }; /* if */
  80.  
  81. } /* disconnect_from_ipc_server */  
  82. #endif SYSV_IPC
  83.  
  84.  
  85. #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS)
  86. /*
  87.   send_string -- send string to socket.
  88. */
  89. void send_string(s,msg)
  90.      int s;
  91.      char *msg;
  92. {
  93.   if (send(s,msg,strlen(msg),0) < 0) {
  94.     perror(progname);
  95.     fprintf(stderr,"%s: unable to send\n",progname);
  96.     exit(1);
  97.   }; /* if */ 
  98.   
  99. } /* send_string */
  100. #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */
  101.  
  102.  
  103. #ifdef UNIX_DOMAIN_SOCKETS
  104. /*
  105.   connect_to_unix_server -- establish connection with server process via a unix-
  106.                   domain socket. Returns socket descriptor for server
  107.                 if successful.
  108. */
  109. int connect_to_unix_server()
  110. {
  111.   int s;            /* connected socket descriptor */
  112.   struct sockaddr_un server;     /* for unix connections */
  113.  
  114.   if ((s = socket(AF_UNIX,SOCK_STREAM,0)) < 0) {
  115.     perror(progname);
  116.     fprintf(stderr,"%s: unable to create socket\n",progname);
  117.     exit(1);
  118.   }; /* if */
  119.   
  120.   server.sun_family = AF_UNIX;
  121.   sprintf(server.sun_path,"/tmp/gsrv%d",geteuid());
  122.  
  123.   if (connect(s,&server,strlen(server.sun_path)+2) < 0) {
  124.     perror(progname);
  125.     fprintf(stderr,"%s: unable to connect to local\n",progname);
  126.     exit(1);
  127.   }; /* if */
  128.  
  129.   return(s);
  130.  
  131. } /* connect_to_unix_server */
  132. #endif /* UNIX_DOMAIN_SOCKETS */
  133.  
  134.  
  135. #ifdef INTERNET_DOMAIN_SOCKETS
  136. /*
  137.   internet_addr -- return the internet addr of the hostname or
  138.                    internet address passed. Return -1 on error.
  139. */
  140. u_long internet_addr(host)
  141.      char *host;
  142. {
  143.   struct hostent *hp;        /* pointer to host info for remote host */
  144.   u_long host_addr;        /* host address */
  145.  
  146.   if ((host_addr = inet_addr(host)) != -1)
  147.     return host_addr;
  148.   else if ((hp = gethostbyname(host)) != NULL)
  149.     return ((struct in_addr *)(hp->h_addr))->s_addr;
  150.   else
  151.     return -1;
  152.  
  153. } /* internet_addr */
  154.  
  155.  
  156. /*
  157.   connect_to_internet_server -- establish connection with server process via 
  158.                   an internet domain socket. Returns socket
  159.                 descriptor for server if successful.
  160. */
  161. int connect_to_internet_server(serverhost,port)
  162.      char *serverhost;
  163.      u_short port;
  164. {
  165.   int s;                /* connected socket descriptor */
  166.   struct servent *sp;            /* pointer to service information */
  167.   struct sockaddr_in peeraddr_in;    /* for peer socket address */
  168.  
  169.   /* clear out address structures */
  170.   bzero((char *)&peeraddr_in,sizeof(struct sockaddr_in));
  171.   
  172.   /* Set up the peer address to which we will connect. */
  173.   peeraddr_in.sin_family = AF_INET;
  174.  
  175.   /* look up the server host's internet address */
  176.   if ((peeraddr_in.sin_addr.s_addr = internet_addr(serverhost)) == -1) {
  177.     fprintf(stderr,"%s: unable to find %s in /etc/hosts or from YP\n",
  178.         progname,serverhost);
  179.     exit(1);
  180.   }; /* if */
  181.   
  182.   if (port == 0) {
  183.     if ((sp = getservbyname ("gnuserv","tcp")) == NULL)
  184.       peeraddr_in.sin_port = htons(DEFAULT_PORT+getuid());
  185.     else
  186.       peeraddr_in.sin_port = sp->s_port;
  187.   } /* if */
  188.   else
  189.     peeraddr_in.sin_port = htons(port);
  190.   
  191.   /* Create the socket. */
  192.   if ((s = socket (AF_INET,SOCK_STREAM, 0))== -1) {
  193.     perror(progname);
  194.     fprintf(stderr,"%s: unable to create socket\n",progname);
  195.     exit(1);
  196.   }; /* if */
  197.   
  198.   /* Try to connect to the remote server at the address
  199.    * which was just built into peeraddr.
  200.    */
  201.   if (connect(s, &peeraddr_in, sizeof(struct sockaddr_in)) == -1) {
  202.     perror(progname);
  203.     fprintf(stderr, "%s: unable to connect to remote\n",progname);
  204.     exit(1);
  205.   }; /* if */
  206.   
  207.   return(s);
  208.  
  209. } /* connect_to_internet_server */
  210. #endif /* INTERNET_DOMAIN_SOCKETS */
  211.  
  212.  
  213. #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS)
  214. /*
  215.   disconnect_from_server -- inform the server that sending has finished, and wait for
  216.                             its reply.
  217. */
  218. void disconnect_from_server(s,echo)
  219.      int s;
  220.      int echo;
  221. {
  222.   char buffer[REPLYSIZ];
  223.   int length;
  224.  
  225.   send_string(s,"\n");        /* make sure server gets string */
  226.  
  227.   if (shutdown(s,1) == -1) {
  228.     perror(progname);
  229.     fprintf(stderr, "%s: unable to shutdown socket\n",progname);
  230.     exit(1);
  231.   }; /* if */
  232.  
  233.   while((length = recv(s,buffer,REPLYSIZ,0)) > 0) {
  234.     buffer[length] = '\0';
  235.     if (echo) printf("%s",buffer);
  236.   }; /* while */
  237.   
  238.   if (echo) putchar('\n');
  239.  
  240.   if(length < 0) {
  241.     perror(progname);
  242.     fprintf(stderr,"%s: unable to read the reply from the server\n",progname);
  243.     exit(1);
  244.   }; /* if */
  245.  
  246. } /* disconnect_from_server */  
  247. #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */
  248.