home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR3 / KA9Q212.ZIP / PPPCMD.C < prev    next >
C/C++ Source or Header  |  1993-07-16  |  14KB  |  626 lines

  1. /*
  2.  *  PPPCMD.C    -- PPP related user commands
  3.  *
  4.  *    This implementation of PPP is declared to be in the public domain.
  5.  *
  6.  *    Jan 91    Bill_Simpson@um.cc.umich.edu
  7.  *        Computer Systems Consulting Services
  8.  *
  9.  *    Acknowledgements and correction history may be found in PPP.C
  10.  */
  11.  
  12. /****************************************************************************
  13. *    $Id: pppcmd.c 1.5 93/07/16 11:48:56 ROOT_DOS Exp $
  14. *    10 Aug 92    1.2        GT    Idle timeout routines.                            *
  15. *    11 Aug 92    1.3        GT    Reopen LCP after close.                            *
  16. *    19 Aug 92    1.4        GT    "exit" option in idle command.                    *
  17. *    14 Jul 93    1.5        GT    Fix warnings.                                    *
  18. ****************************************************************************/
  19.  
  20. #include <stdio.h>
  21. #include "global.h"
  22. #include "mbuf.h"
  23. #include "iface.h"
  24. #include "pktdrvr.h"
  25. #include "ppp.h"
  26. #include "pppfsm.h"
  27. #include "ppplcp.h"
  28. #include "ppppap.h"
  29. #include "pppipcp.h"
  30. #include "cmdparse.h"
  31. #include    "commands.h"
  32.  
  33. static struct iface *ppp_lookup __ARGS((char *ifname));
  34.  
  35. static int doppp_quick        __ARGS((int argc, char *argv[], void *p));
  36. static int doppp_trace        __ARGS((int argc, char *argv[], void *p));
  37. static int doppp_idle __ARGS((int argc, char *argv[], void *p));
  38.  
  39. static int spot __ARGS((int16 work,int16 want,int16 will,int16 mask));
  40. static void genstat        __ARGS((struct ppp_s *ppp_p));
  41. static void lcpstat        __ARGS((struct fsm_s *fsm_p));
  42. static void papstat        __ARGS((struct fsm_s *fsm_p));
  43. static void ipcpstat        __ARGS((struct fsm_s *fsm_p));
  44.  
  45. static int dotry_nak        __ARGS((int argc, char *argv[], void *p));
  46. static int dotry_req        __ARGS((int argc, char *argv[], void *p));
  47. static int dotry_terminate    __ARGS((int argc, char *argv[], void *p));
  48.  
  49. static void idle_timeout __ARGS((void *p));
  50.  
  51.  
  52. /* "ppp" subcommands */
  53. static struct cmds Pppcmds[] = {
  54.     { "ipcp",        doppp_ipcp,    0,    0,    NULLCHAR },
  55.     { "lcp",        doppp_lcp,    0,    0,    NULLCHAR },
  56.     { "pap",        doppp_pap,    0,    0,    NULLCHAR },
  57.     { "quick",    doppp_quick,    0,    0,    NULLCHAR },
  58.     { "trace",    doppp_trace,    0,    0,    NULLCHAR },
  59.     { "idle",        doppp_idle,    0,    0,    NULLCHAR },
  60.     { NULLCHAR },
  61. };
  62.  
  63. /* "ppp <iface> <ncp> try" subcommands */
  64. static struct cmds PppTrycmds[] = {
  65.     { "configure",    dotry_req,    0,    0,    NULLCHAR },
  66.     { "failure",    dotry_nak,    0,    0,    NULLCHAR },
  67.     { "terminate",    dotry_terminate,    0,    0,    NULLCHAR },
  68.     { NULLCHAR },
  69. };
  70.  
  71. static char *PPPStatus[] = {
  72.     "Physical Line Dead",
  73.     "Establishment Phase",
  74.     "Authentication Phase",
  75.     "Network Protocol Phase",
  76.     "Termination Phase"
  77. };
  78.  
  79. static char *NCPStatus[] = {
  80.     "Closed",
  81.     "Listening -- waiting for remote host to attempt open",
  82.     "Starting configuration exchange",
  83.     "Remote host accepted our request; waiting for remote request",
  84.     "We accepted remote request; waiting for reply to our request",
  85.     "Opened",
  86.     "Terminate request sent to remote host"
  87. };
  88.  
  89. int PPPtrace;
  90. struct iface *PPPiface;  /* iface for trace */
  91.  
  92. static int exit_on_idle = 0;            /* nz - exit on idle timeout        */
  93.  
  94.  
  95. /****************************************************************************/
  96.  
  97. static struct iface *
  98. ppp_lookup(ifname)
  99. char *ifname;
  100. {
  101.     register struct iface *ifp;
  102.  
  103.     if ((ifp = if_lookup(ifname)) == NULLIF) {
  104.         tprintf("%s: Interface unknown\n",ifname);
  105.         return(NULLIF);
  106.     }
  107.     if (ifp->type != CL_PPP) {
  108.         tprintf("%s: not a PPP interface\n",ifp->name);
  109.         return(NULLIF);
  110.     }
  111.     return(ifp);
  112. }
  113.  
  114. /****************************************************************************/
  115.  
  116. int
  117. doppp_commands(argc,argv,p)
  118. int argc;
  119. char *argv[];
  120. void *p;
  121. {
  122.     register struct iface *ifp;
  123.  
  124.     if (argc < 2) {
  125.         tprintf( "ppp <iface> required\n" );
  126.         return -1;
  127.     }
  128.     if ((ifp = ppp_lookup(argv[1])) == NULLIF)
  129.         return -1;
  130.  
  131.     if ( argc == 2 ) {
  132.         ppp_show( ifp );
  133.         return 0;
  134.     }
  135.  
  136.     return subcmd(Pppcmds, argc - 1, &argv[1], ifp);
  137. }
  138.  
  139.  
  140. /* Close connection on PPP interface */
  141. int
  142. doppp_close(argc,argv,p)
  143. int argc;
  144. char *argv[];
  145. void *p;
  146. {
  147.     register struct fsm_s *fsm_p = p;
  148.  
  149.     fsm_p->flags &= ~(FSM_ACTIVE | FSM_PASSIVE);
  150.  
  151.     fsm_close( fsm_p );
  152.     return 0;
  153. }
  154.  
  155.  
  156. int
  157. doppp_passive(argc,argv,p)
  158. int argc;
  159. char *argv[];
  160. void *p;
  161. {
  162.     register struct fsm_s *fsm_p = p;
  163.  
  164.     fsm_p->flags &= ~FSM_ACTIVE;
  165.     fsm_p->flags |= FSM_PASSIVE;
  166.  
  167.     fsm_start(fsm_p);
  168.     return 0;
  169. }
  170.  
  171.  
  172. int
  173. doppp_active(argc,argv,p)
  174. int argc;
  175. char *argv[];
  176. void *p;
  177. {
  178.     register struct fsm_s *fsm_p = p;
  179.  
  180.     fsm_p->flags &= ~FSM_PASSIVE;
  181.     fsm_p->flags |= FSM_ACTIVE;
  182.  
  183.     if ( fsm_p->state < fsmLISTEN ) {
  184.         fsm_p->state = fsmLISTEN;
  185.     }
  186.     return 0;
  187. }
  188.  
  189.  
  190. static int
  191. doppp_quick(argc,argv,p)
  192. int argc;
  193. char *argv[];
  194. void *p;
  195. {
  196.     register struct iface *ifp = p;
  197.     register struct ppp_s *ppp_p = ifp->edv;
  198.     struct lcp_s *lcp_p = ppp_p->fsm[Lcp].pdv;
  199.     struct ipcp_s *ipcp_p = ppp_p->fsm[IPcp].pdv;
  200.  
  201.     lcp_p->local.want.accm = 0L;
  202.     lcp_p->local.want.negotiate |= LCP_N_ACCM;
  203.     lcp_p->local.want.magic_number += (long)&lcp_p->local.want.magic_number;
  204.     lcp_p->local.want.negotiate |= LCP_N_MAGIC;
  205.     lcp_p->local.want.negotiate |= LCP_N_ACFC;
  206.     lcp_p->local.want.negotiate |= LCP_N_PFC;
  207.  
  208.     ipcp_p->local.want.compression = PPP_COMPR_PROTOCOL;
  209.     ipcp_p->local.want.slots = 16;
  210.     ipcp_p->local.want.slot_compress = 1;
  211.     ipcp_p->local.want.negotiate |= IPCP_N_COMPRESS;
  212.     doppp_active( 0, NULL, &(ppp_p->fsm[IPcp]) );
  213.  
  214.     return 0;
  215. }
  216.  
  217.  
  218. /****************************************************************************/
  219.  
  220. void
  221. ppp_show(ifp)
  222. struct iface *ifp;
  223. {
  224.     register struct ppp_s *ppp_p = ifp->edv;
  225.  
  226.     genstat(ppp_p);
  227.     if ( ppp_p->fsm[Lcp].pdv != NULL )
  228.         lcpstat(&(ppp_p->fsm[Lcp]));
  229.     if ( ppp_p->fsm[Pap].pdv != NULL )
  230.         papstat(&(ppp_p->fsm[Pap]));
  231.     if ( ppp_p->fsm[IPcp].pdv != NULL )
  232.         ipcpstat(&(ppp_p->fsm[IPcp]));
  233. }
  234.  
  235.  
  236. static void
  237. genstat(ppp_p)
  238. register struct ppp_s *ppp_p;
  239. {
  240.  
  241.     tprintf("%s", PPPStatus[ppp_p->phase]);
  242.  
  243.     if (ppp_p->phase == pppREADY) {
  244.         tprintf("\t(open for %s)",
  245.             tformat(secclock() - ppp_p->upsince));
  246.     }
  247.     tprintf("\n");
  248.  
  249.     tprintf("%10lu In,  %10lu Flags,%6u ME, %6u FE, %6u CSE, %6u other\n",
  250.         ppp_p->InRxOctetCount,
  251.         ppp_p->InOpenFlag,
  252.         ppp_p->InMemory,
  253.         ppp_p->InFrame,
  254.         ppp_p->InChecksum,
  255.         ppp_p->InError);
  256.     tprintf("\t\t%6u Lcp,%6u Pap,%6u IPcp,%6u Unknown\n",
  257.         ppp_p->InNCP[Lcp],
  258.         ppp_p->InNCP[Pap],
  259.         ppp_p->InNCP[IPcp],
  260.         ppp_p->InUnknown);
  261.     tprintf("%10lu Out, %10lu Flags,%6u ME, %6u Fail\n",
  262.         ppp_p->OutTxOctetCount,
  263.         ppp_p->OutOpenFlag,
  264.         ppp_p->OutMemory,
  265.         ppp_p->OutError);
  266.     tprintf("\t\t%6u Lcp,%6u Pap,%6u IPcp\n",
  267.         ppp_p->OutNCP[Lcp],
  268.         ppp_p->OutNCP[Pap],
  269.         ppp_p->OutNCP[IPcp]);
  270. }
  271.  
  272.  
  273. static int
  274. spot(work,want,will,mask)
  275. int16 work;
  276. int16 want;
  277. int16 will;
  278. int16 mask;
  279. {
  280.     char blot = ' ';
  281.     int result = (work & mask);
  282.  
  283.     if ( !(will & mask) ) {
  284.         blot = '*';
  285.     } else if ( (want ^ work) & mask ) {
  286.         blot = (result ? '+' : '-');
  287.     }
  288.     tprintf( "%c", blot );
  289.     return result;
  290. }
  291.  
  292. static void
  293. lcpstat(fsm_p)
  294. struct fsm_s *fsm_p;
  295. {
  296.     struct lcp_s *lcp_p = fsm_p->pdv;
  297.     struct lcp_value_s *localp = &(lcp_p->local.work);
  298.     int16  localwork = lcp_p->local.work.negotiate;
  299.     int16  localwant = lcp_p->local.want.negotiate;
  300.     int16  localwill = lcp_p->local.will_negotiate;
  301.     struct lcp_value_s *remotep = &(lcp_p->remote.work);
  302.     int16  remotework = lcp_p->remote.work.negotiate;
  303.     int16  remotewant = lcp_p->remote.want.negotiate;
  304.     int16  remotewill = lcp_p->remote.will_negotiate;
  305.  
  306.     tprintf("LCP %s\n",
  307.         NCPStatus[fsm_p->state]);
  308.  
  309.     tprintf("\t\t MRU\t ACCM\t\t AP\t PFC  ACFC Magic\n");
  310.  
  311.     tprintf("\tLocal:\t");
  312.  
  313.     spot( localwork, localwant, localwill, LCP_N_MRU );
  314.     tprintf( "%4d\t", localp->mru );
  315.  
  316.     spot( localwork, localwant, localwill, LCP_N_ACCM );
  317.     tprintf( "0x%08lx\t", localp->accm );
  318.  
  319.     if ( spot( localwork, localwant, localwill, LCP_N_AUTHENT ) ) {
  320.         switch ( localp->authentication ) {
  321.         case PPP_PAP_PROTOCOL:
  322.             tprintf( "Pap\t" );
  323.             break;
  324.         default:
  325.             tprintf( "0x%04x\t", localp->authentication);
  326.             break;
  327.         };
  328.     } else {
  329.         tprintf( "None\t" );
  330.     }
  331.  
  332.     tprintf( spot( localwork, localwant, localwill, LCP_N_PFC )
  333.          ? "Yes " : "No  " );
  334.     tprintf( spot( localwork, localwant, localwill, LCP_N_ACFC )
  335.          ? "Yes " : "No  " );
  336.  
  337.     spot( localwork, localwant, localwill, LCP_N_MAGIC );
  338.     if ( localp->magic_number != 0L ) {
  339.         tprintf( "0x%08lx\n", localp->magic_number );
  340.     } else {
  341.         tprintf( "unused\n" );
  342.     }
  343.  
  344.     tprintf("\tRemote:\t");
  345.  
  346.     spot( remotework, remotewant, remotewill, LCP_N_MRU );
  347.     tprintf( "%4d\t", remotep->mru );
  348.  
  349.     spot( remotework, remotewant, remotewill, LCP_N_ACCM );
  350.     tprintf( "0x%08lx\t", remotep->accm );
  351.  
  352.     if ( spot( remotework, remotewant, remotewill, LCP_N_AUTHENT ) ) {
  353.         switch ( remotep->authentication ) {
  354.         case PPP_PAP_PROTOCOL:
  355.             tprintf( "Pap\t" );
  356.             break;
  357.         default:
  358.             tprintf( "0x%04x\t", remotep->authentication);
  359.             break;
  360.         };
  361.     } else {
  362.         tprintf( "None\t" );
  363.     }
  364.  
  365.     tprintf( spot( remotework, remotewant, remotewill, LCP_N_PFC )
  366.          ? "Yes " : "No  " );
  367.     tprintf( spot( remotework, remotewant, remotewill, LCP_N_ACFC )
  368.          ? "Yes " : "No  " );
  369.  
  370.     spot( remotework, remotewant, remotewill, LCP_N_MAGIC );
  371.     if ( remotep->magic_number != 0L ) {
  372.         tprintf( "0x%08lx\n", remotep->magic_number );
  373.     } else {
  374.         tprintf( "unused\n" );
  375.     }
  376. }
  377.  
  378.  
  379. static void
  380. papstat(fsm_p)
  381. struct fsm_s *fsm_p;
  382. {
  383.     struct pap_s *pap_p = fsm_p->pdv;
  384.  
  385.     tprintf("PAP %s\n",
  386.         NCPStatus[fsm_p->state]);
  387.  
  388.     tprintf( "\tMessage: '%s'\n", (pap_p->message == NULL) ?
  389.         "none" : pap_p->message );
  390. }
  391.  
  392.  
  393. static void
  394. ipcpstat(fsm_p)
  395. struct fsm_s *fsm_p;
  396. {
  397.     struct ipcp_s *ipcp_p = fsm_p->pdv;
  398.     struct ipcp_value_s *localp = &(ipcp_p->local.work);
  399.     int16  localwork = ipcp_p->local.work.negotiate;
  400.     struct ipcp_value_s *remotep = &(ipcp_p->remote.work);
  401.     int16  remotework = ipcp_p->remote.work.negotiate;
  402.  
  403.     tprintf("IPCP %s\n",
  404.         NCPStatus[fsm_p->state]);
  405.     tprintf("\tlocal IP address: %s",
  406.         inet_ntoa(localp->address));
  407.     tprintf("  remote IP address: %s\n",
  408.         inet_ntoa(localp->other));
  409.  
  410.     if (localwork & IPCP_N_COMPRESS) {
  411.         tprintf("    In\tTCP header compression enabled:"
  412.             " slots = %d, flag = 0x%02x\n",
  413.             localp->slots,
  414.             localp->slot_compress);
  415.         slhc_i_status(ipcp_p->slhcp);
  416.     }
  417.  
  418.     if (remotework & IPCP_N_COMPRESS) {
  419.         tprintf("    Out\tTCP header compression enabled:"
  420.             " slots = %d, flag = 0x%02x\n",
  421.             remotep->slots,
  422.             remotep->slot_compress);
  423.         slhc_o_status(ipcp_p->slhcp);
  424.     }
  425. }
  426.  
  427.  
  428. /****************************************************************************/
  429. /* Set timeout interval when waiting for response from remote peer */
  430. int
  431. doppp_timeout(argc,argv,p)
  432. int argc;
  433. char *argv[];
  434. void *p;
  435. {
  436.     struct fsm_s *fsm_p = p;
  437.     struct timer *t = &(fsm_p->timer);
  438.  
  439.     if (argc < 2) {
  440.         tprintf("%d\n",dur_timer(t)/1000L);
  441.     } else {
  442.         int x = (int)strtol( argv[1], NULLCHARP, 0 );
  443.  
  444.         if (x <= 0) {
  445.             tprintf("Timeout value %s (%d) must be > 0\n",
  446.                 argv[1], x);
  447.             return -1;
  448.         } else {
  449.             set_timer(t, x * 1000L);
  450.         }
  451.     }
  452.     return 0;
  453. }
  454.  
  455.  
  456. int
  457. doppp_try(argc,argv,p)
  458. int argc;
  459. char *argv[];
  460. void *p;
  461. {
  462.     return subcmd(PppTrycmds, argc, argv, p);
  463. }
  464.  
  465.  
  466. static int
  467. dotry_nak(argc,argv,p)
  468. int argc;
  469. char *argv[];
  470. void *p;
  471. {
  472.     struct fsm_s *fsm_p = p;
  473.  
  474.     if (argc < 2) {
  475.         tprintf("%d\n",fsm_p->try_nak);
  476.     } else {
  477.         int x = (int)strtol( argv[1], NULLCHARP, 0 );
  478.  
  479.         if (x <= 0) {
  480.             tprintf("Value %s (%d) must be > 0\n",
  481.                 argv[1], x);
  482.             return -1;
  483.         } else {
  484.             fsm_p->try_nak = x;
  485.         }
  486.     }
  487.     return 0;
  488. }
  489.  
  490.  
  491. static int
  492. dotry_req(argc,argv,p)
  493. int argc;
  494. char *argv[];
  495. void *p;
  496. {
  497.     struct fsm_s *fsm_p = p;
  498.  
  499.     if (argc < 2) {
  500.         tprintf("%d\n",fsm_p->try_req);
  501.     } else {
  502.         int x = (int)strtol( argv[1], NULLCHARP, 0 );
  503.  
  504.         if (x <= 0) {
  505.             tprintf("Value %s (%d) must be > 0\n",
  506.                 argv[1], x);
  507.             return -1;
  508.         } else {
  509.             fsm_p->try_req = x;
  510.         }
  511.     }
  512.     return 0;
  513. }
  514.  
  515.  
  516. static int
  517. dotry_terminate(argc,argv,p)
  518. int argc;
  519. char *argv[];
  520. void *p;
  521. {
  522.     struct fsm_s *fsm_p = p;
  523.  
  524.     if (argc < 2) {
  525.         tprintf("%d\n",fsm_p->try_terminate);
  526.     } else {
  527.         int x = (int)strtol( argv[1], NULLCHARP, 0 );
  528.  
  529.         if (x <= 0) {
  530.             tprintf("Value %s (%d) must be > 0\n",
  531.                 argv[1], x);
  532.             return -1;
  533.         } else {
  534.             fsm_p->try_terminate = x;
  535.         }
  536.     }
  537.     return 0;
  538. }
  539.  
  540.  
  541. static int
  542. doppp_trace(argc,argv,p)
  543. int argc;
  544. char *argv[];
  545. void *p;
  546. {
  547.     register struct iface *ifp = p;
  548.     register struct ppp_s *ppp_p = ifp->edv;
  549.     int tracing = ppp_p->trace;
  550.     int result = setint(&tracing,"PPP tracing",argc,argv);
  551.  
  552.     ppp_p->trace = tracing;
  553.     return result;
  554. }
  555.  
  556.  
  557. /****************************************************************************
  558. *    doppp_idle                                                                *
  559. *    Set/display the PPP idle timer.                                            *
  560. ****************************************************************************/
  561.  
  562. static int doppp_idle (argc, argv, p)
  563. int argc;
  564. char *argv[];
  565. void *p;
  566.     {
  567.     struct iface *ifp = p;                /* -> interface                        */
  568.     struct ppp_s *ppp_p = ifp->edv;        /* -> PPP control structure            */
  569.  
  570.     if (argc < 2)
  571.         {
  572.         tprintf ("PPP idle timeout: %ld\n", ppp_p->idle_durn / 1000L);
  573.         tprintf ("Exit on idle timeout: %s\n",
  574.                  (exit_on_idle == 0) ? "off" : "on");
  575.         }
  576.     else
  577.         {
  578.         int32 x = strtol (argv[1], NULLCHARP, 0);
  579.  
  580.         if (x < 0)
  581.             {
  582.             tprintf ("Timeout value %s (%ld) must be >= 0\n",
  583.                      argv[1], x);
  584.             return (-1);
  585.             }
  586.         else
  587.             {
  588.             if (ppp_p->idle_durn != 0L)
  589.                 stop_timer (&(ppp_p->idle_timer));
  590.                 
  591.             ppp_p->idle_durn = x * 1000L;
  592.             ppp_p->idle_timer.func = idle_timeout;
  593.             ppp_p->idle_timer.arg = &(ppp_p->fsm[Lcp]);
  594.             set_timer (&(ppp_p->idle_timer), ppp_p->idle_durn);
  595.             if (argc == 3)
  596.                 {
  597.                 if (strcmp (argv[2], "exit") == 0)
  598.                     exit_on_idle = 1;
  599.                 else if (strcmp (argv[2], "noexit") == 0)
  600.                     exit_on_idle = 0;
  601.  
  602.                 }
  603.                     
  604.             }
  605.  
  606.         }
  607.         
  608.     return (0);
  609.     }    /* static int doppp_idle (argc, argv, p) */
  610.  
  611.  
  612. /****************************************************************************
  613. *    idle_timeout                                                            *
  614. *    Performs an "lcp close" when the idle timer times out.                    *
  615. ****************************************************************************/
  616.  
  617. static void idle_timeout (p)
  618. void *p;
  619.     {
  620.     (void) doppp_close (0, NULL, p);
  621.     if (exit_on_idle != 0)
  622.         doexit (0, NULL, NULL);
  623.         
  624.     (void) dolcp_open (0, NULL, p);
  625.     }    /* static void idle_timeout (p) */
  626.