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