home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / internet / netlite2 / NET / c / IPCMD < prev    next >
Encoding:
Text File  |  1993-03-29  |  4.3 KB  |  174 lines

  1. /* IP-related user commands */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "dbox.h"
  6. #include "werr.h"
  7. #include "global.h"
  8. #include "mbuf.h"
  9. #include "internet.h"
  10. #include "timer.h"
  11. #include "netuser.h"
  12. #include "iface.h"
  13. #include "ip.h"
  14. #include "cmdparse.h"
  15. #include "icmp.h"
  16. #include "misc.h"
  17.  
  18. #define IP_TTL      1
  19. #define IP_OK       0
  20.  
  21. extern char badhost[];
  22. struct cmds ipcmds[] = {
  23.         "address",      doipaddr,       0,      NULLCHAR,       NULLCHAR,
  24.         "ttl",          dottl,          0,      NULLCHAR,       NULLCHAR,
  25.         NULLCHAR,       NULLFP,         0,
  26.                 "ip subcommands: address ttl",   NULLCHAR,
  27. };
  28. int doip(int argc, char **argv)
  29. {
  30.         return subcmd(ipcmds,argc,argv);
  31. }
  32. int doipaddr(int argc, char **argv)
  33. {
  34.         int32 n;
  35.  
  36.         if(argc > 1) {
  37.                 if ((n = resolve(argv[1])) == 0) {
  38.                         werr(1,badhost,argv[1]);
  39.                         return 1;
  40.                 }
  41.  
  42.                 ip_addr = n;
  43.         }
  44.  
  45.         return 0;
  46. }
  47. int dottl(int argc, char **argv)
  48. {
  49.         if (argc > 1) ip_ttl = atoi(argv[1]);
  50.         return 0;
  51. }
  52. void ip_parms(void)
  53. {
  54.         dbox d;
  55.  
  56.         if ((d = dbox_new("IP_Parms")) == NULL)
  57.                  return;
  58.  
  59.         dbox_setnumeric(d, IP_TTL, ip_ttl);
  60.  
  61.         dbox_show(d);
  62.  
  63.         if (dbox_fillin(d) == IP_OK)
  64.                ip_ttl = dbox_getnumeric(d, IP_TTL);
  65.  
  66.         dbox_dispose(&d);
  67. }
  68. /* "route" subcommands */
  69. static struct cmds rtcmds[] = {
  70.         "add", doadd, 3,
  71.         "route add <dest addr>[/<bits>] <if name> [gateway] [metric]",
  72.         "Add failed",
  73.  
  74.         "drop", dodrop, 2,
  75.         "route drop <dest addr>[/<bits>]",
  76.         "Not in table",
  77.  
  78.         NULLCHAR, NULLFP, 0,
  79.         "route subcommands: add, drop",
  80.         NULLCHAR, 
  81. };
  82.  
  83. /* Display and/or manipulate routing table */
  84. int doroute(int argc, char **argv)
  85. {
  86.         return subcmd(rtcmds,argc,argv);
  87. }
  88. /* Add an entry to the routing table
  89.  * E.g., "add 1.2.3.4 ax0 5.6.7.8 3"
  90.  */
  91. int doadd(int argc, char **argv)
  92. {
  93.         struct interface *ifp;
  94.         int32 dest,gateway;
  95.         unsigned bits;
  96.         char *bitp;
  97.         int metric;
  98.  
  99.         if(strcmp(argv[1],"default") == 0){
  100.                 dest = 0;
  101.                 bits = 0;
  102.         } else {
  103.                 if((dest = resolve(argv[1])) == 0){
  104.                         werr(0,badhost,argv[1]);
  105.                         return 1;
  106.                 }
  107.  
  108.                 /* If IP address is followed by an optional slash and
  109.                  * a length field, (e.g., 128.96/16) get it;
  110.                  * otherwise assume a full 32-bit address
  111.                  */
  112.                 if((bitp = strchr(argv[1],'/')) != NULLCHAR){
  113.                         bitp++;
  114.                         bits = atoi(bitp);
  115.                 } else
  116.                         bits = 32;
  117.         }
  118.         for(ifp=ifaces;ifp != NULLIF;ifp = ifp->next){
  119.                 if(strcmp(argv[2],ifp->name) == 0)
  120.                         break;
  121.         }
  122.         if(ifp == NULLIF){
  123.                 werr(0,"Interface \"%s\" unknown\n",argv[2]);
  124.                 return 1;
  125.         }
  126.         if(argc > 3){
  127.                 if((gateway = resolve(argv[3])) == 0){
  128.                         werr(0,badhost,argv[3]);
  129.                         return 1;
  130.                 }
  131.         } else {
  132.                 gateway = 0;
  133.         }
  134.         if(argc > 4)
  135.                 metric = atoi(argv[4]);
  136.         else
  137.                 metric = 0;
  138.  
  139.         rt_add(dest,bits,gateway,metric,ifp);
  140.         return 0;
  141. }
  142. /* Drop an entry from the routing table
  143.  * E.g., "drop 128.96/16
  144.  */
  145. int dodrop(int argc, char **argv)
  146. {
  147.         char *bitp;
  148.         unsigned bits;
  149.         int32 n;
  150.  
  151.         argc = argc;
  152.  
  153.         if(strcmp(argv[1],"default") == 0){
  154.                 n = 0;
  155.                 bits = 0;
  156.         } else {
  157.                 /* If IP address is followed by an optional slash and length field,
  158.                  * (e.g., 128.96/16) get it; otherwise assume a full 32-bit address
  159.                  */
  160.                 if((bitp = strchr(argv[1],'/')) != NULLCHAR){
  161.                         bitp++;
  162.                         bits = atoi(bitp);
  163.                 } else
  164.                         bits = 32;
  165.  
  166.                 if((n = resolve(argv[1])) == 0){
  167.                         werr(0,badhost,argv[1]);
  168.                         return 1;
  169.                 }
  170.         }
  171.         return rt_drop(n,bits);
  172. }
  173.  
  174.