home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / telecomm / ka9q_src / main.c < prev    next >
Encoding:
Text File  |  1988-12-02  |  19.1 KB  |  622 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 "config.h"
  9. #include "global.h"
  10. #include "mbuf.h"
  11. #include "netuser.h"
  12. #include "timer.h"
  13. #include "icmp.h"
  14. #include "iface.h"
  15. #include "ip.h"
  16. #include "tcp.h"
  17. #include "ax25.h"
  18. #include "remote.h"
  19. #include "netrom.h"
  20. #include "ftp.h"
  21. #include "telnet.h"
  22. #include "session.h"
  23. #include "cmdparse.h"
  24.  
  25. #ifdef UNIX        /* BSD or SYS5 */
  26. #include "unix.h"
  27. #endif
  28.  
  29. #ifdef AMIGA
  30. #include "amiga.h"
  31. #endif
  32.  
  33. #ifdef MAC
  34. #include "mac.h"
  35. #endif
  36.  
  37. #ifdef MSDOS
  38. #include "pc.h"
  39. #endif
  40.  
  41. #ifdef ATARI_ST
  42. #include "st.h"
  43. #endif
  44.  
  45. #ifdef LATTICE
  46. extern long _MNEED;        /* reserve memory for child process (shell) */
  47. long _MNEED = 100000;        /* plucked out of thin air */
  48. extern long _32K;
  49. long _32K = 0x8000;        /* keep GST Linker happy */
  50. #endif
  51.  
  52. #ifdef    MWC
  53. long    _stksize                    cmdmode();
  54.                 }
  55.                 continue;
  56.             }
  57. #endif
  58. #ifdef SYS5
  59.             if(c == escape && escape != 0){
  60.                 if(mode != CMD_MODE){
  61.                     printf("\r\n");
  62.                     cmdmode();
  63.                 }
  64.                 continue;
  65.             }
  66. #endif     /* SYS5 */
  67. /* ----------------------------(DG2KK)------------------------------- */
  68. #ifndef FLOW
  69.             if((cnt = ttydriv(c,&ttybuf)) == 0)
  70.                 continue;
  71. #else
  72.             cnt = ttydriv(c,&ttybuf);
  73.             if (cnt == 0) {        /* should be != 0 */
  74.                 ttyflow = 0;    /* stop output to screen */
  75.             } else {
  76.                 ttyflow = 1;    /* restart output */
  77.                 if(mode != CMD_MODE)
  78.                     go();    /* display pending chars */
  79.             }
  80.             if (cnt == 0)
  81.                 continue;
  82. #endif FLOW
  83. /* ------------------------------------------------------------------ */
  84.             switch(mode){
  85.             case CMD_MODE:
  86.                 (void)cmdparse(cmds,ttybuf);
  87.                 fflush(stdout);
  88.                 break;
  89.             case CONV_MODE:
  90. #if    (!MSDOS && !ATARI_ST)
  91.                 if(ttybuf[0] == escape && escape != 0){
  92.                     printf("\n");
  93.                     cmdmode();
  94.                 } else
  95. #endif
  96.                     if(current->parse != NULLFP)
  97.                         (*current->parse)(ttybuf,cnt);
  98.  
  99.                 break;
  100.             }
  101.             if(mode == CMD_MODE){
  102.                 printf(prompt);
  103.                 fflush(stdout);
  104.             }
  105.         }
  106.         keep_things_going();
  107.     }
  108. }
  109. /* Standard commands called from main */
  110.  
  111. /* Enter command mode */
  112. int
  113. cmdmode()
  114. {
  115.     if(mode != CMD_MODE){
  116.         mode = CMD_MODE;
  117.         cooked();
  118.         printf(prompt);
  119.         fflush(stdout);
  120.         echo();
  121.     }
  122.     return 0;
  123. }
  124. static
  125. doexit()
  126. {
  127.     if(logfp != NULLFILE)
  128.         fclose(logfp);
  129.     iostop();
  130.     exit(0);
  131. }
  132. static
  133. dohostname(argc,argv)
  134. int argc;
  135. char *argv[];
  136. {
  137.     char *strncpy();
  138.  
  139.     if(argc < 2)
  140.         printf("%s\n",hostname);
  141.     else 
  142.         strncpy(hostname,argv[1],HOSTNAMELEN);
  143.     return 0;
  144. }
  145. static
  146. int
  147. dolog(argc,argv)
  148. int argc;
  149. char *argv[];
  150. {
  151.     char *strncpy();
  152.  
  153.     static char logname[15];
  154.     if(argc < 2){
  155.         if(logfp)
  156.             printf("Logging to %s\n",logname);
  157.         else
  158.             printf("Logging off\n");
  159.         return 0;
  160.     }
  161.     if(logfp){
  162.         fclose(logfp);
  163.         logfp = NULLFILE;
  164.     }
  165.     if(strcmp(argv[1],"stop") != 0){
  166.         strncpy(logname,argv[1],15);
  167.         logfp = fopen(logname,"a+");
  168.     }
  169.     return 0;
  170. }
  171. static
  172. int
  173. dohelp()
  174. {
  175.     register struct cmds *cmdp;
  176.     int i,j;
  177.  
  178.     printf("Main commands:\n");
  179.     for(i=0,cmdp = cmds;cmdp->name != NULL;cmdp++,i++){
  180.         printf("%s",cmdp->name);
  181.         if((i % 4) == 3)
  182.             printf("\n");
  183.         else {
  184.             for(j=strlen(cmdp->name);j < 16; j++)
  185.                 putchar(' ');
  186.         }
  187.     }
  188.     if((i % 4) != 0)
  189.         printf("\n");
  190.     return 0;
  191. }
  192.  
  193. doecho(argc,argv)
  194. int argc;
  195. char *argv[];
  196. {
  197.     extern int refuse_echo;
  198.  
  199.     if(argc < 2){
  200.         if(refuse_echo)
  201.             printf("Refuse\n");
  202.         else
  203.             printf("Accept\n");
  204.     } else {
  205.         if(argv[1][0] == 'r')
  206.             refuse_echo = 1;
  207.         else if(argv[1][0] == 'a')
  208.             refuse_echo = 0;
  209.         else
  210.             return -1;
  211.     }
  212.     return 0;
  213. }
  214. /* set for unix end of line for remote echo mode telnet */
  215. doeol(argc,argv)
  216. int argc;
  217. char *argv[];
  218. {
  219.     extern int unix_line_mode;
  220.  
  221.     if(argc < 2){
  222.         if(unix_line_mode)
  223.             printf("Unix\n");
  224.         else
  225.             printf("Standard\n");
  226.     } else {
  227.         if(strcmp(argv[1],"unix") == 0)
  228.             unix_line_mode = 1;
  229.         else if(strcmp(argv[1],"standard") == 0)
  230.             unix_line_mode = 0;
  231.         else {
  232.             return -1;
  233.         }
  234.     }
  235.     return 0;
  236. }
  237. /* Attach an interface
  238.  * Syntax: attach <hw type> <I/O address> <vector> <mode> <label> <bufsize> [<speed>]
  239.  */
  240. doattach(argc,argv)
  241. int argc;
  242. char *argv[];
  243. {
  244.     extern struct cmds attab[];
  245.  
  246.     return subcmd(attab,argc,argv);
  247. }
  248. /* Manipulate I/O device parameters */
  249. doparam(argc,argv)
  250. int argc;
  251. char *argv[];
  252. {
  253.     register struct interface *ifp;
  254.  
  255.     for(ifp=ifaces;ifp != NULLIF;ifp = ifp->next){
  256.         if(strcmp(argv[1],ifp->name) == 0)
  257.             break;
  258.     }
  259.     if(ifp == NULLIF){
  260.         printf("Interface \"%s\" unknown\n",argv[1]);
  261.         return 1;
  262.     }
  263.     if(ifp->ioctl == NULLFP){
  264.         printf("Not supported\n");
  265.         return 1;
  266.     }
  267.     /* Pass rest of args to device-specific code */
  268.     return (*ifp->ioctl)(ifp,argc-2,argv+2);
  269. }
  270.  
  271. /* -------------------------------------------------------- */
  272. /* Log messages of the form
  273.  * Tue Jan 31 00:00:00 1987 44.64.0.7:1003 open FTP
  274.  */
  275. /*VARARGS2*/
  276. log(tcb,fmt,arg1,arg2,arg3,arg4)
  277. struct tcb *tcb;
  278. char *fmt;
  279. int arg1,arg2,arg3,arg4;
  280. {
  281.     char *cp;
  282.     long t;
  283.     int fd;
  284.  
  285.     if(logfp == NULLFILE)
  286.         return;
  287.     time(&t);
  288.     cp = ctime(&t);
  289.     rip(cp);
  290.     /* log events which don't have an IP address (AX.25 connects etc) */
  291.     if (tcb != NULLTCB) {
  292.         fprintf(logfp,"%s %s - ",cp,psocket(&tcb->conn.remote));
  293.     } else {
  294.         fprintf(logfp,"%s - ",cp);
  295.     }
  296.     fprintf(logfp,fmt,arg1,arg2,arg3,arg4);
  297.     fprintf(logfp,"\n");
  298.     fflush(logfp);
  299. #if ( MSDOS || ATARI_ST )
  300.     /* MS-DOS doesn't really flush files until they're closed */
  301.     fd = fileno(logfp);
  302.     if((fd = dup(fd)) != -1)
  303.         close(fd);
  304. #endif
  305. }
  306. /* ------------------------------------------------------------------ */
  307.  
  308. /* Configuration-dependent code */
  309.  
  310. /* List of supported hardware devices */
  311. int ec_attach(),asy_attach(),pc_attach(),eg_attach(),hapn_attach(),at_attach(),
  312.     nr_attach();
  313.  
  314. struct cmds attab[] = {
  315. #ifdef    PC_EC
  316.     /* 3-Com Ethernet interface */
  317.     "3c500", ec_attach, 7, 
  318.     "attach 3c500 <address> <vector> arpa <label> <buffers> <mtu>",
  319.     "Could not attach 3c500",
  320. #endif
  321. #ifdef    SLIP
  322.     /* Ordinary PC asynchronous adaptor */
  323.     "asy", asy_attach, 8, 
  324. #ifndef    SLFP
  325.     "attach asy <address> <vector> slip|ax25 <label> <buffers> <mtu> <speed>",
  326. #else
  327.     "attach asy <address> <vector> slip|ax25|slfp <label> <buffers> <mtu> <speed>",
  328. #endif
  329.     "Could not attach asy",
  330. #endif
  331. #ifdef    PC100
  332.     /* PACCOMM PC-100 8530 HDLC adaptor */
  333.     "pc100", pc_attach, 8, 
  334.     "attach pc100 <address> <vector> ax25 <label> <buffers> <mtu> <speed>",
  335.     "Could not attach pc100",
  336. #endif
  337. #ifdef    EAGLE
  338.     /* EAGLE RS-232C 8530 HDLC adaptor */
  339.     "eagle", eg_attach, 8,
  340.     "attach eagle <address> <vector> ax25 <label> <buffers> <mtu> <speed>",
  341.     "Could not attach eagle",
  342. #endif
  343. #ifdef    HAPN
  344.     /* Hamilton Area Packet Radio (HAPN) 8273 HDLC adaptor */
  345.     "hapn", hapn_attach, 8,
  346.     "attach hapn <address> <vector> ax25 <label> <rx bufsize> <mtu> csma|full",
  347.     "Could not attach hapn",
  348. #endif
  349. #ifdef    APPLETALK
  350.     /* Macintosh AppleTalk */
  351.     "0", at_attach, 7,
  352.     "attach 0 <protocol type> <device> arpa <label> <rx bufsize> <mtu>",
  353.     "Could not attach Appletalk",
  354. #endif
  355. #ifdef AX25
  356.     /* Fake NET/ROM interface */
  357.     "netrom", nr_attach, 1,
  358.     "attach netrom",
  359.     "Could not attach netrom",
  360. #endif
  361.     NULLCHAR, NULLFP, 0,
  362.     "Unknown device",
  363.     NULLCHAR,
  364. };
  365.  
  366. /* Protocol tracing function pointers */
  367. #ifdef    TRACE
  368. int ax25_dump(),ether_dump(),ip_dump(),at_dump(),slfp_dump();
  369.  
  370. int (*tracef[])() = {
  371. #ifdef    AX25
  372.     ax25_dump,
  373. #else
  374.     NULLFP,
  375. #endif
  376.  
  377. #ifdef    ETHER
  378.     ether_dump,
  379. #else
  380.     NULLFP,
  381. #endif
  382.     ip_dump,
  383.  
  384. #ifdef    APPLETALK
  385.     at_dump,
  386. #else
  387.     NULLFP,
  388. #endif
  389.  
  390. #ifdef    SLFP
  391.     slfp_dump,
  392. #else
  393.     NULLFP,
  394. #endif
  395. };
  396. #else
  397. int (*tracef[])() = { NULLFP }; /* No tracing at all */
  398. dump(interface,direction,type,bp)
  399. struct interface *interface;
  400. int direction;
  401. unsigned type;
  402. struct mbuf *bp;
  403. {
  404. }
  405. #endif
  406.  
  407. #ifdef    AX25
  408. /* Set up a SLIP link to use AX.25 */
  409. kiss_attach(if_asy,srecv)
  410. struct interface *if_asy;
  411. int (**srecv)();
  412. {
  413.     int kiss_ioctl(),ax_send(),ax_output(),kiss_raw(),kiss_recv();
  414.  
  415.     axarp();
  416.     if(mycall.call[0] == '\0'){
  417.         printf("set mycall first\n");
  418.         free((char *)if_asy);
  419.         return -1;
  420.     }
  421.     if_asy->ioctl = kiss_ioctl;
  422.     if_asy->send = ax_send;
  423.     if_asy->output = ax_output;
  424.     if_asy->raw = kiss_raw;
  425.     if(if_asy->hwaddr == NULLCHAR)
  426.         if_asy->hwaddr = malloc(sizeof(mycall));
  427.     memcpy(if_asy->hwaddr,(char *)&mycall,sizeof(mycall));
  428.     *srecv = kiss_recv;
  429.     return 0;
  430. }
  431.  
  432.  
  433.  
  434. /* Set up a net/rom serial interface */
  435. nrs_attach(if_asy,call)
  436. struct interface *if_asy;
  437. char *call ;
  438. {
  439.     struct ax25_addr addr ;
  440.     int ax_send(),ax_output(),nrs_raw(),nrs_recv(),asy_ioctl();
  441.  
  442.     if (call == NULLCHAR) {                /* no call supplied? */
  443.         if(mycall.call[0] == '\0'){        /* try to use default */
  444.             printf("set mycall first or specify in attach statement\n");
  445.             return -1;
  446.         }
  447.         else
  448.             addr = mycall ;
  449.     }
  450.     else {                /* callsign supplied on attach line */
  451.         if (setcall(&addr,call) == -1) {
  452.             printf ("bad callsign on attach line\n") ;
  453.             return -1 ;
  454.         }
  455.     }
  456.  
  457.     if_asy->flags |= CONNECT_MODE;        /* DG2KK: was: IF_AX25 */
  458.     if_asy->recv = nrs_recv ;
  459.     if_asy->ioctl = asy_ioctl ;
  460.     if_asy->send = ax_send;
  461.     if_asy->output = ax_output;
  462.     if_asy->raw = nrs_raw;
  463.     if(if_asy->hwaddr == NULLCHAR)
  464.         if_asy->hwaddr = malloc(sizeof(addr));
  465.     memcpy(if_asy->hwaddr,(char *)&addr,sizeof(addr));
  466.     return 0;
  467. }
  468.  
  469.  
  470. /* Display or set IP interface control flags */
  471. domode(argc,argv)
  472. int argc;
  473. char *argv[];
  474. {
  475.     register struct interface *ifp;
  476.  
  477.     for(ifp=ifaces;ifp != NULLIF;ifp = ifp->next){
  478.         if(strcmp(argv[1],ifp->name) == 0)
  479.             break;
  480.     }
  481.     if(ifp == NULLIF){
  482.         printf("Interface \"%s\" unknown\n",argv[1]);
  483.         return 1;
  484.     }
  485.     if(argc < 3){
  486.         printf("%s: %s\n",ifp->name,
  487.          (ifp->flags & CONNECT_MODE) ? "VC mode" : "Datagram mode");
  488.         return 0;
  489.     }
  490.     switch(argv[2][0]){
  491.     case 'v':
  492.     case 'c':
  493.     case 'V':
  494.     case 'C':
  495.         ifp->flags = CONNECT_MODE;
  496.         break;
  497.     case 'd':
  498.     case 'D':
  499.         ifp->flags = DATAGRAM_MODE;
  500.         break;
  501.     default:
  502.         printf("Usage: %s [vc | datagram]\n",argv[0]);
  503.         return 1;
  504.     }
  505.     return 0;
  506. }
  507. #endif
  508.  
  509. #ifdef SERVERS
  510. dostart(argc,argv)
  511. int argc;
  512. char *argv[];
  513. {
  514.     return subcmd(startcmds,argc,argv);
  515. }
  516. dostop(argc,argv)
  517. int argc;
  518. char *argv[];
  519. {
  520.     return subcmd(stopcmds,argc,argv);
  521. }
  522. #endif SERVERS
  523.  
  524. #ifdef    TRACE
  525. static
  526. int
  527. dotrace(argc,argv)
  528. int argc;
  529. char *argv[];
  530. {
  531.     struct interface *ifp;
  532.  
  533.     if(argc < 2){
  534.         showtrace(&loopback);
  535.         for(ifp = ifaces; ifp != NULLIF; ifp = ifp->next)
  536.             showtrace(ifp);
  537.         return 0;
  538.     }
  539.     if(strcmp("loopback",argv[1]) == 0)
  540.         ifp = &loopback;
  541.     else 
  542.         for(ifp = ifaces; ifp != NULLIF; ifp = ifp->next)
  543.             if(strcmp(ifp->name,argv[1]) == 0)
  544.                 break;
  545.  
  546.     if(ifp == NULLIF){
  547.         printf("Interface %s unknown\n",argv[1]);
  548.         return 1;
  549.     }
  550.     if(argc >= 3)
  551.         ifp->trace = htoi(argv[2]);
  552.  
  553.     showtrace(ifp);
  554.     return 0;
  555. }
  556. /* Display the trace flags for a particular interface */
  557. static
  558. showtrace(ifp)
  559. register struct interface *ifp;
  560. {
  561.     if(ifp == NULLIF)
  562.         return;
  563.     printf("%s:",ifp->name);
  564.     if(ifp->trace & (IF_TRACE_IN | IF_TRACE_OUT)){
  565.         if(ifp->trace & IF_TRACE_IN)
  566.             printf(" input");
  567.         if(ifp->trace & IF_TRACE_OUT)
  568.             printf(" output");
  569.  
  570.         if(ifp->trace & IF_TRACE_HEX)
  571.             printf(" (Hex/ASCII dump)");
  572.         else if(ifp->trace & IF_TRACE_ASCII)
  573.             printf(" (ASCII dump)");
  574.         else
  575.             printf(" (headers only)");
  576.         printf("\n");
  577.     } else
  578.         printf(" tracing off\n");
  579.     fflush(stdout);
  580. }
  581. #endif
  582.  
  583. #if    (!MSDOS && !ATARI_ST)
  584. static
  585. int
  586. doescape(argc,argv)
  587. int argc;
  588. char *argv[];
  589. {
  590.     if(argc < 2)
  591.         printf("0x%x\n",escape);
  592.     else 
  593.         escape = *argv[1];
  594.     return 0;
  595. }
  596. #endif
  597.  
  598. static
  599. doremote(argc,argv)
  600. int argc;
  601. char *argv[];
  602. {
  603.     struct socket fsock,lsock;
  604.     struct mbuf *bp;
  605.  
  606.     lsock.address = ip_addr;
  607.     fsock.address = resolve(argv[1]);
  608.     lsock.port = fsock.port = atoi(argv[2]);
  609.     bp = alloc_mbuf(1);
  610.     if(strcmp(argv[3],"reset") == 0){
  611.         *bp->data = SYS_RESET;
  612.     } else if(strcmp(argv[3],"exit") == 0){
  613.         *bp->data = SYS_EXIT;
  614.     } else {
  615.         printf("Unknown command %s\n",argv[3]);
  616.         return 1;
  617.     }
  618.     bp->cnt = 1;
  619.     send_udp(&lsock,&fsock,0,0,bp,0,0,0);
  620.     return 0;
  621. }
  622.