home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / MISC / mtp.shar / mtpd.c < prev    next >
C/C++ Source or Header  |  2009-11-06  |  3KB  |  95 lines

  1. /* mtpd.c, Version 1.1, Created 2/9/90 */
  2. /* Dr Alan M. McIvor, BP Sunbury Research Centre */
  3. /* Daemon for handling mtp connections */
  4.  
  5. #include <stdio.h>
  6. #include <types.h>
  7. #include <socket.h>
  8. #include <in.h>
  9. #include <netdb.h>
  10. #include <strings.h>
  11. #include <errno.h>
  12.  
  13. #include "mtp.h"
  14.  
  15. main(argc, argv)
  16.      int argc;
  17.      char **argv;
  18. {
  19.   int s;            /* socket */
  20.   struct protoent *tcp;        /* tcp protocol structure */
  21.   struct sockaddr_in sname;    /* socket name */
  22.   struct sockaddr_in from;    /* name of skt connecting to this one */
  23.   int fromlen;
  24.   int ns;            /* socket connecting to this one */
  25.   void fork_handler();
  26.  
  27.   if ((tcp = getprotobyname("tcp")) == (struct protoent *)0)
  28.     exit(_errmsg(errno, "tcp protocol unknown\n"));
  29.  
  30.   if ((s = socket(AF_INET, SOCK_STREAM, tcp->p_proto)) == -1)
  31.     exit(_errmsg(errno, "Could not get socket\n"));
  32.  
  33.   memset((char *)&sname, 0, sizeof(sname)); /* zero sname */
  34.   sname.sin_family = AF_INET;
  35.   sname.sin_addr.s_addr = INADDR_ANY; /* wild INET card address */
  36.   sname.sin_port = MTP_PORT;
  37.   if (bind(s, (struct sockaddr *)&sname, sizeof(sname)) == -1)
  38.     exit(_errmsg(errno, "Could not bind name\n"));
  39.  
  40.   if (listen(s, 5) == -1)
  41.     exit(_errmsg(errno, "Could not listen\n"));
  42.  
  43.   while (FOREVER)
  44.     {
  45. /*      printf("**mtpd - accepting ...\n");*/
  46.       fromlen = sizeof(from);
  47.       if ((ns = accept(s, (struct sockaddr *)&from, &fromlen)) == -1)
  48.     exit(_errmsg(errno, "Accept error %d\n", errno));
  49. /*      printf("**mtpd - accepted.\n");*/
  50.  
  51. /*
  52.       printf("**mtpd - About to fork %s with s=%d, ns=%d\n",
  53.          CONNECTION_HANDLER, s, ns);
  54. */
  55.       /* fork child process to talk to socket */
  56.       fork_handler(s, ns);
  57.  
  58. /*      printf("**mtpd - connection handler forked.\n");*/
  59.       
  60.       if (close(ns) == -1)
  61.     exit(_errmsg(errno, "Close error\n"));
  62.     }
  63.  
  64.   /* shouldn't ever get here */
  65.  
  66.   if (shutdown(s, 2) == -1)
  67.     exit(_errmsg(errno, "Could not shutdown\n"));
  68.   
  69.   if (close(s) == -1)
  70.     exit(_errmsg(errno, "Close error\n"));
  71.  
  72.   exit(0);
  73. }
  74.  
  75. void fork_handler(s, ns)
  76.      int s, ns;            /* socket descriptors */
  77. {
  78.   extern int os9forkc();
  79.   extern char **environ;
  80.   char *argblk[4];        /* arguments to pass to mtpdc */
  81.  
  82.   argblk[0] = (char *)malloc(32);
  83.   strcpy(argblk[0], CONNECTION_HANDLER);
  84.   argblk[1] = (char *)malloc(32);
  85.   sprintf(argblk[1], "%d", s);
  86.   argblk[2] = (char *)malloc(32);
  87.   sprintf(argblk[2], "%d", ns);
  88.   argblk[3] = (char *)NULL;
  89.  
  90.   if (os9exec(os9forkc, argblk[0], argblk, environ, 0, 0, 5) <= 0)
  91.     exit(_errmsg(errno, "Can't fork %s\n", CONNECTION_HANDLER));
  92.  
  93.   return;
  94. }
  95.