home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / comm / net / amitcp / amitcp-2.2 / src / appl / fingerd / fingerd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-21  |  4.8 KB  |  232 lines

  1. RCS_ID_C= "$Id: fingerd.c,v 1.4 1993/10/18 15:45:30 ppessi Exp $";
  2. /*
  3.  * fingerd.c --- an example of TCP daemon for AmiTCP/IP
  4.  *
  5.  * This command sends either a AmiTCP/IP banner or the specified file
  6.  * into the TCP socket server_socket 
  7.  *
  8.  * Author: ppessi <Pekka.Pessi@hut.fi>
  9.  *
  10.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  11.  *                  Helsinki University of Technology, Finland.
  12.  *
  13.  * Created      : Mon May 24 23:44:43 1993 ppessi
  14.  * Last modified: Mon Oct 18 17:34:41 1993 ppessi
  15.  *
  16.  * $Log: fingerd.c,v $
  17.  * Revision 1.4  1993/10/18  15:45:30  ppessi
  18.  * Added real version tags.
  19.  *
  20.  * Revision 1.3  1993/10/15  01:24:00  ppessi
  21.  * A new version supporting real fingering. Uses Apipe:.
  22.  *
  23.  * Revision 1.2  1993/08/10  20:46:23  jraja
  24.  * Added version string.
  25.  *
  26.  * Revision 1.1  1993/06/04  11:49:53  jraja
  27.  * Initial revision
  28.  *
  29.  */
  30.  
  31. #include "fingerd_rev.h"
  32. const char version[] = VERSTAG;
  33.  
  34. char copyright[] =
  35.   "Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>\n"
  36.   "Helsinki University of Technology, Finland.\n";
  37.  
  38. #ifdef AMIGA
  39. #if __SASC
  40. #include <proto/socket.h>
  41. #include <proto/dos.h>
  42. #include <clib/exec_protos.h>
  43. #include <pragmas/exec_sysbase_pragmas.h>
  44. #elif __GNUC__
  45. #include <inline/socket.h>
  46. #include <inline/exec.h>
  47. #else
  48. #include <clib/socket_protos.h>
  49. #endif
  50. #endif /* AMIGA */
  51.  
  52. #include <errno.h>
  53. #include <netdb.h>
  54.  
  55. #include <sys/param.h>
  56. #include <sys/socket.h>
  57. #include <sys/ioctl.h>
  58. #include <netinet/in.h>
  59.  
  60. #include <signal.h>
  61.  
  62. #include <dos/dos.h>
  63. #include <exec/execbase.h>
  64. #include <dos/var.h>
  65.  
  66. #include <stdlib.h>
  67. #include <inetdlib.h>
  68.  
  69. #include "pathnames.h"
  70.  
  71. #define isspace(x) (x == ' ')
  72.  
  73. extern struct ExecBase *SysBase;
  74.  
  75. BPTR Stdin = NULL;
  76. BPTR Stdout = NULL;
  77. BPTR Stderr = NULL;
  78.  
  79. void
  80. _STIdosStdio(void)
  81. {
  82.   struct Process *p = (struct Process *)SysBase->ThisTask;
  83.  
  84.   Stdin = p->pr_CIS;
  85.   Stdout = p->pr_COS;
  86.   Stderr = p->pr_CES ? p->pr_CES : Stdout;
  87. }
  88.  
  89. #define BANNER \
  90. "\r\n             AmiTCP/IP 2.1 Release System.\r\n\r\n"
  91.  
  92. int
  93. main(int argc, char **argv)
  94. {
  95.   long len;
  96.   int s = server_socket;
  97.   BPTR pfh;
  98.  
  99. # define CMDHEAD "apipe:" _PATH_FINGER ""
  100. # define CMDLEN (sizeof(CMDHEAD)-1)
  101. # define LINELEN (1024)
  102. # define CMDLINELEN (1024+CMDLEN)
  103.  
  104.   char line[LINELEN], cmdline[CMDLINELEN] = CMDHEAD;
  105.   char *fname = cmdline;
  106.  
  107. #ifdef LOGGING
  108. #include <netinet/in.h>
  109.   struct sockaddr_in sin;
  110.   int sval;
  111. #endif
  112.  
  113.   if (s == -1) {
  114. #ifdef STANDALONE
  115.     struct sockaddr_in sin;
  116.  
  117.     s = serveraccept("finger", &sin);
  118.     if (s != -1) {
  119.       FPrintf(Stderr, "Accepted a connection from %s, port %ld\n",
  120.           inet_ntoa(sin.sin_addr), sin.sin_port);
  121.     } else 
  122. #endif
  123.       return 1;
  124.   }
  125.  
  126. #ifdef LOGGING            /* unused for now */
  127.   sval = sizeof(sin);
  128.   if (getpeername(0, &sin, &sval) < 0)
  129.     fatal("getpeername");
  130. #endif
  131.  
  132.   {
  133.     enum { WSPACE, TOKEN, QUOTED } state = WSPACE;
  134.     char c;
  135.     int received, peek, len = 0;
  136.  
  137.     do {
  138.       received = recv(s, line + len, 1, 0);
  139.       if (received < 0) {
  140.     PrintNetFault(Errno(), "recv");
  141.     return 1;
  142.       }
  143.     } while (received && line[len] != '\n' && len++ < LINELEN - 2);
  144.  
  145.     if (len == 1 && argc > 1) 
  146.       fname = argv[1];
  147.  
  148.     if (line[len - 1] == '\r') 
  149.       len--;
  150.     line[len] = '\0';
  151.  
  152.     /* 
  153.      * Parse command line:
  154.      * Quote arguments,
  155.      * convert /w to -l
  156.      */
  157.     peek = 0; received = len = CMDLEN;
  158.     while ((c = line[peek++]) && len < CMDLINELEN - 4) {
  159.       switch(state) {
  160.       case WSPACE:
  161.     if (c == '/' 
  162.         && (line[peek] == 'W' || line[peek] == 'w') 
  163.         && (line[peek + 1] == '\0' || isspace(line[peek + 1]))) { 
  164.       cmdline[len++] = ' ';
  165.       cmdline[len++] = '-';
  166.       cmdline[len++] = 'l';
  167.       peek += 1;
  168.       received = len;
  169.     } else if (c == '"') {
  170.       cmdline[len++] = '"';
  171.       state = QUOTED;
  172.     } else if (!isspace(c)) {
  173.       cmdline[len++] = ' ';
  174.       cmdline[len++] = '"';
  175.       cmdline[len++] = c;
  176.       state = TOKEN;
  177.     } 
  178.     break;
  179.       case TOKEN:
  180.     if (c == '"') {
  181.       state = QUOTED;
  182.     } else if (isspace(c)) {
  183.       cmdline[len++] = '"';
  184.       state = WSPACE;
  185.       received = len;
  186.     } else {
  187.       cmdline[len++] = c;
  188.     }
  189.     break;
  190.       case QUOTED:
  191.     if (c == '"') {
  192.       state = TOKEN;
  193.     } else {
  194.       cmdline[len++] = c;
  195.     }
  196.     break;
  197.       }
  198.     }
  199.     if (c || state == QUOTED) {
  200.       /* remove last argument */
  201.       len = received;
  202.     } else if (state == TOKEN) {
  203.       cmdline[len++] = '"';
  204.     }
  205.     cmdline[len] = '\0';    /* put eos */
  206.   }
  207.  
  208.   pfh = Open(fname, MODE_OLDFILE);
  209.   
  210.   /* Try to open banner if there is no APipe or Finger */
  211.   if (pfh || 
  212.       argc >= 2 && (pfh = Open(argv[1], MODE_OLDFILE))) {
  213.     while (FGets(pfh, line, sizeof(line))) {
  214.       len = strlen(line);
  215.       /* Change eol's to the network standard */
  216.       if (line[len - 1] == '\n') {
  217.     line[len - 1] = '\r';
  218.     line[len++] = '\n';
  219.       }
  220.       if (send(s, line, len, 0) < 0) break;
  221.     }
  222.     Close(pfh);
  223.   } else {
  224.     if (send(s, BANNER, strlen(BANNER), 0) < 0) {
  225.       PrintNetFault(Errno(), "send");
  226.       return 1;
  227.     }
  228.   }
  229.   
  230.   return 0;
  231. }
  232.