home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / internet / netlite / NET / c / MAIN < prev    next >
Text File  |  1993-04-24  |  6KB  |  232 lines

  1. /* Main network program - provides both client and server functions */
  2.  
  3. #define HOSTNAMELEN 32          /* changed from 16 by Bdale 860812 */
  4.  
  5. extern char startup[];  /* File to read startup commands from */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <time.h>
  11. #include <stdarg.h>
  12. #include <ctype.h>
  13. #include "werr.h"
  14. #include "config.h"
  15. #include "global.h"
  16. #include "mbuf.h"
  17. #include "netuser.h"
  18. #include "timer.h"
  19. #include "icmp.h"
  20. #include "iface.h"
  21. #include "ip.h"
  22. #include "tcp.h"
  23. #include "ftp.h"
  24. #include "telnet.h"
  25. #include "session.h"
  26. #include "cmdparse.h"
  27. #include "driver.h"
  28. #include "asy.h"
  29. #include "slip.h"
  30. #include "trace.h"
  31. #include "ethernet.h"
  32. #include "internet.h"
  33. #include "finger.h"
  34. #include "ping.h"
  35. #include "smtp.h"
  36. #include "udp.h"
  37. #include "domain.h"
  38. #include "misc.h"
  39. #include "arc.h"
  40.  
  41. /* Dummy structure for loopback tracing */
  42. struct interface loopback = { NULLIF, "loopback" };
  43. struct interface *ifaces;
  44.  
  45. extern struct mbuf *loopq;
  46.  
  47. char badhost[] = "Unknown host %s\n";
  48. char hostname[HOSTNAMELEN];     
  49. unsigned nsessions = NSESSIONS;
  50. int16 lport = 1001;
  51. char nospace[] = "No space!!\n";        /* Generic malloc fail message */
  52.  
  53. static int donothing(int, char **);       
  54. static int doattach(int, char **);
  55. static int doecho(int, char **);
  56. static int doeol(int, char **);
  57. static int dohostname(int, char **);
  58. static int dostart(int, char **);
  59. static void readconfig(void);
  60.  
  61. static struct cmds cmds[] = {
  62.         "",             donothing,      0, NULLCHAR,    NULLCHAR,
  63.         "attach",       doattach,       2, "attach <hardware> <hw specific options>", NULLCHAR,
  64.         "colour",       docolour,       0, NULLCHAR,    NULLCHAR,
  65.         "domain",       dodomain,       0, NULLCHAR,    NULLCHAR,
  66.         "echo",         doecho,         0, NULLCHAR,    "echo [refuse|accept]",
  67.         "eol",          doeol,          0, NULLCHAR,    "eol options: unix, standard",
  68.         "hostname",     dohostname,     0, NULLCHAR,    NULLCHAR,
  69.         "ip",           doip,           0, NULLCHAR,    NULLCHAR,
  70.         "route",        doroute,        0, NULLCHAR,    NULLCHAR,
  71.         "smtp",         dosmtp,         0, NULLCHAR,    NULLCHAR,
  72.         "start",        dostart,        2, "start <servername>",NULLCHAR,
  73.         "tcp",          dotcp,          0, NULLCHAR,    NULLCHAR,
  74.         NULLCHAR,       NULLFP,         0, "Unknown command; type \"?\" for list",   NULLCHAR, 
  75. };
  76.  
  77. /* "start" subcommands */
  78.  
  79. static struct cmds startcmds[] = {
  80.         "finger",       finger1,        0, NULLCHAR, NULLCHAR,
  81.         "ftp",          ftp1,           0, NULLCHAR, NULLCHAR,
  82.         "smtp",         smtp1,          0, NULLCHAR, NULLCHAR,
  83.         NULLCHAR,       NULLFP,         0,
  84.                 "start options: finger, ftp, smtp", NULLCHAR,
  85. };
  86.  
  87. void net_init(void)
  88. {
  89.         sessions = (struct session *)calloc(nsessions,sizeof(struct session));
  90.         readconfig();
  91. }
  92.  
  93. void net_poll(void)
  94. {
  95.         struct interface *ifp;
  96.         struct mbuf *bp;
  97.         struct ip ip;
  98.  
  99.         /* Service the loopback queue */
  100.         if((bp = dequeue(&loopq)) != NULLBUF){
  101.                 ntohip(&ip,&bp);
  102.                 ip_recv(&ip,bp,0);
  103.         }
  104.         /* Service the interfaces */
  105.         for(ifp = ifaces; ifp != NULLIF; ifp = ifp->next){
  106.                 if(ifp->recv != NULLFP)
  107.                         (*ifp->recv)(ifp);
  108.         }
  109.  
  110.         /* Service the clock if it has ticked */
  111.         check_time();
  112. }
  113.  
  114. void iostop(void)
  115. {
  116.         struct interface *ifp;
  117.  
  118.         /* Stop the interfaces */
  119.         for(ifp = ifaces; ifp != NULLIF; ifp = ifp->next){
  120.                 if(ifp->stop != NULLFP)
  121.                         (*ifp->stop)(ifp);
  122.         }
  123. }
  124.  
  125. static int donothing(int argc, char **argv)
  126. {
  127.         argc = argc;
  128.         argv = argv;
  129.  
  130.         return 0;
  131. }
  132.  
  133. static int dohostname(int argc, char **argv)
  134. {
  135.         if (argc > 1) strncpy(hostname,argv[1],HOSTNAMELEN);
  136.         return 0;
  137. }
  138.  
  139. static int doecho(int argc, char **argv)
  140. {
  141.         extern int refuse_echo;
  142.  
  143.         if (argc > 1) {
  144.                 if(argv[1][0] == 'r')
  145.                         refuse_echo = 1;
  146.                 else if(argv[1][0] == 'a')
  147.                         refuse_echo = 0;
  148.                 else
  149.                         return -1;
  150.         }
  151.         return 0;
  152. }
  153.  
  154. /* set for unix end of line for remote echo mode telnet */
  155. static int doeol(int argc, char **argv)
  156. {
  157.         extern int unix_line_mode;
  158.  
  159.         if (argc > 1) {
  160.                 if(strcmp(argv[1],"unix") == 0)
  161.                         unix_line_mode = 1;
  162.                 else if(strcmp(argv[1],"standard") == 0)
  163.                         unix_line_mode = 0;
  164.                 else {
  165.                         return -1;
  166.                 }
  167.         }
  168.         return 0;
  169. }
  170. /* Attach an interface
  171.  * Syntax: attach <hw type> <port> <mode> <label> <mtu> <speed>
  172.  */
  173. static int doattach(int argc, char **argv)
  174. {
  175.         extern struct cmds attab[];
  176.  
  177.         return subcmd(attab,argc,argv);
  178. }
  179.  
  180. /* Configuration-dependent code */
  181.  
  182. /* List of supported hardware devices */
  183.  
  184. struct cmds attab[] = {
  185.         /* Ordinary asynchronous adaptor */
  186.         "asy", asy_attach, 7, 
  187.         "attach asy internal <port> <mode> <label> <mtu> <speed>",
  188.         "Could not attach asy",
  189.  
  190.         /* Driver spec asynchronous adaptor */
  191.         "driver", driver_attach, 7, 
  192.         "attach driver <driver> <port> <mode> <label> <mtu> <speed>",
  193.         "Could not attach driver",
  194.  
  195.         /* Ethernet driver via DCI */
  196.         "ether", ether_attach, 4,
  197.         "attach ether <port> <label> <mtu>",
  198.         "Could not attach ether",
  199.  
  200.         NULLCHAR, NULLFP, 0,
  201.         "Unknown device",
  202.         NULLCHAR,
  203. };
  204.  
  205. static int dostart(int argc, char **argv)
  206. {
  207.         return subcmd(startcmds,argc,argv);
  208. }
  209.  
  210. static void readconfig(void)
  211. {
  212.         static char inbuf[200],savebuf[200];    /* keep it off the stack */
  213.         int linenum = 0;
  214.         FILE *fp;
  215.  
  216.         if((fp = fopen(startup,"r")) == NULLFILE){
  217.                 werr(1,"Cannot open startup file\n");
  218.                 return;
  219.         }
  220.  
  221.         while(fgets(inbuf,sizeof(inbuf),fp) != NULLCHAR){
  222.                 rip(inbuf);
  223.                 strcpy(savebuf,inbuf);
  224.                 linenum++;
  225.                 if(cmdparse(cmds,inbuf) != 0)
  226.                         werr(0,"startup: line %d: %s\n",linenum,savebuf);
  227.         }
  228.  
  229.         fclose(fp);
  230. }
  231.  
  232.