home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / trace / tcpdump-2.2.1 / print-egp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-17  |  7.4 KB  |  348 lines

  1. /*
  2.  * Copyright (c) 1991 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Lawrence Berkeley Laboratory,
  11.  * Berkeley, CA.  The name of the University may not be used to
  12.  * endorse or promote products derived from this software without
  13.  * specific prior written permission.
  14.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  15.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  16.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  17.  *
  18.  * Initial contribution from Jeff Honig (jch@MITCHELL.CIT.CORNELL.EDU).
  19.  */
  20. #include <stdio.h>
  21. #include <sys/param.h>
  22. #include <sys/uio.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <netinet/in_systm.h>
  26. #include <netinet/ip.h>
  27. #include <netdb.h>
  28.  
  29. #include "interface.h"
  30. #include "addrtoname.h"
  31.  
  32. struct egp_packet {
  33.     u_char  egp_version;
  34. #define    EGP_VERSION    2
  35.     u_char  egp_type;
  36. #define  EGPT_ACQUIRE    3
  37. #define  EGPT_REACH    5
  38. #define  EGPT_POLL    2
  39. #define  EGPT_UPDATE    1
  40. #define  EGPT_ERROR    8
  41.     u_char  egp_code;
  42. #define  EGPC_REQUEST    0
  43. #define  EGPC_CONFIRM    1
  44. #define  EGPC_REFUSE    2
  45. #define  EGPC_CEASE    3
  46. #define  EGPC_CEASEACK    4
  47. #define  EGPC_HELLO    0
  48. #define  EGPC_HEARDU    1
  49.     u_char  egp_status;
  50. #define  EGPS_UNSPEC    0
  51. #define  EGPS_ACTIVE    1
  52. #define  EGPS_PASSIVE    2
  53. #define  EGPS_NORES    3
  54. #define  EGPS_ADMIN    4
  55. #define  EGPS_GODOWN    5
  56. #define  EGPS_PARAM    6
  57. #define  EGPS_PROTO    7
  58. #define  EGPS_INDET    0
  59. #define  EGPS_UP    1
  60. #define  EGPS_DOWN    2
  61. #define  EGPS_UNSOL    0x80
  62.     u_short  egp_checksum;
  63.     u_short  egp_as;
  64.     u_short  egp_sequence;
  65.     union {
  66.         u_short  egpu_hello;
  67.         u_char egpu_gws[2];
  68.         u_short  egpu_reason;
  69. #define  EGPR_UNSPEC    0
  70. #define  EGPR_BADHEAD    1
  71. #define  EGPR_BADDATA    2
  72. #define  EGPR_NOREACH    3
  73. #define  EGPR_XSPOLL    4
  74. #define  EGPR_NORESP    5
  75. #define  EGPR_UVERSION    6
  76.     } egp_handg;
  77. #define  egp_hello  egp_handg.egpu_hello
  78. #define  egp_intgw  egp_handg.egpu_gws[0]
  79. #define  egp_extgw  egp_handg.egpu_gws[1]
  80. #define  egp_reason  egp_handg.egpu_reason
  81.     union {
  82.         u_short  egpu_poll;
  83.         u_long egpu_sourcenet;
  84.     } egp_pands;
  85. #define  egp_poll  egp_pands.egpu_poll
  86. #define  egp_sourcenet  egp_pands.egpu_sourcenet
  87. };
  88.  
  89. char *egp_acquire_codes[] = {
  90.     "request",
  91.     "confirm",
  92.     "refuse",
  93.     "cease",
  94.     "cease_ack"
  95. };
  96.  
  97. char *egp_acquire_status[] = {
  98.     "unspecified",
  99.     "active_mode",
  100.     "passive_mode",
  101.     "insufficient_resources",
  102.     "administratively_prohibited",
  103.     "going_down",
  104.     "parameter_violation",
  105.     "protocol_violation"
  106. };
  107.  
  108. char *egp_reach_codes[] = {
  109.     "hello",
  110.     "i-h-u"
  111. };
  112.  
  113. char *egp_status_updown[] = {
  114.     "indeterminate",
  115.     "up",
  116.     "down"
  117. };
  118.  
  119. char *egp_reasons[] = {
  120.     "unspecified",
  121.     "bad_EGP_header_format",
  122.     "bad_EGP_data_field_format",
  123.     "reachability_info_unavailable",
  124.     "excessive_polling_rate",
  125.     "no_response",
  126.     "unsupported_version"
  127. };
  128.  
  129. static void
  130. egpnrprint(egp, length)
  131.     register struct egp_packet *egp;
  132.     register int length;
  133. {
  134.     register u_char *cp, *ep;
  135. #define TCHECK(n) if (cp > ep - n) goto trunc
  136.     register u_long addr;
  137.     register u_long net;
  138.     register int netlen;
  139.     int gateways, distances, networks;
  140.     int t_gateways;
  141.     char *comma;
  142.  
  143.     addr = egp->egp_sourcenet;
  144.     if (IN_CLASSA(addr)) {
  145.         net = addr & IN_CLASSA_NET;
  146.         netlen = 1;
  147.     } else if (IN_CLASSB(addr)) {
  148.         net = addr & IN_CLASSB_NET;
  149.         netlen = 2;
  150.     } else if (IN_CLASSC(addr)) {
  151.         net = addr & IN_CLASSC_NET;
  152.         netlen = 3;
  153.     } else {
  154.         net = 0;
  155.         netlen = 0;
  156.     }
  157.     cp = (u_char *)(egp + 1);
  158.     ep = snapend;
  159.  
  160.     t_gateways = egp->egp_intgw + egp->egp_extgw;
  161.     for (gateways = 0; gateways < t_gateways; ++gateways) {
  162.         /* Pickup host part of gateway address */
  163.         addr = 0;
  164.         TCHECK(4 - netlen);
  165.         switch (netlen) {
  166.  
  167.         case 1:
  168.             addr = *cp++;
  169.             /* fall through */
  170.         case 2:
  171.             addr = (addr << 8) | *cp++;
  172.             /* fall through */
  173.         case 3:
  174.             addr = (addr << 8) | *cp++;
  175.         }
  176.         addr |= net;
  177.         TCHECK(1);
  178.         distances = *cp++;
  179.         printf(" %s %s ",
  180.                gateways < egp->egp_intgw ? "int" : "ext", 
  181.                intoa(addr));
  182.         
  183.         comma = "";
  184.         putchar('(');
  185.         while (--distances >= 0) {
  186.             TCHECK(2);
  187.             printf("%sd%d:", comma, (int)*cp++);
  188.             comma = ", ";
  189.             networks = *cp++;
  190.             while (--networks >= 0) {
  191.                 /* Pickup network number */
  192.                 TCHECK(1);
  193.                 addr = (u_long)*cp++ << 24;
  194.                 if (IN_CLASSB(addr)) {
  195.                     TCHECK(1);
  196.                     addr |= (u_long)*cp++ << 16;
  197.                 } else if (!IN_CLASSA(addr)) {
  198.                     TCHECK(2);
  199.                     addr |= (u_long)*cp++ << 16;
  200.                     addr |= (u_long)*cp++ << 8;
  201.                 }
  202.                 printf(" %s", intoa(addr));
  203.             }
  204.         }
  205.         putchar(')');
  206.     }
  207.     return;
  208. trunc:
  209.     fputs("[|]", stdout);
  210. }
  211.  
  212. void
  213. egp_print(egp, length, ip)
  214.     register struct egp_packet *egp;
  215.     register int length;
  216.     register struct ip *ip;
  217. {
  218.     register int status;
  219.     register int code;
  220.     register int type;
  221.     
  222.         (void)printf("%s > %s: egp: ",
  223.              ipaddr_string(&ip->ip_src),
  224.              ipaddr_string(&ip->ip_dst));
  225.  
  226.     if (egp->egp_version != EGP_VERSION) {
  227.         printf("[version %d]", egp->egp_version);
  228.         return;
  229.     }
  230.     printf("as:%d seq:%d", ntohs(egp->egp_as), ntohs(egp->egp_sequence));
  231.     
  232.     type = egp->egp_type;
  233.     code = egp->egp_code;
  234.     status = egp->egp_status;
  235.  
  236.     switch (type) {
  237.     case EGPT_ACQUIRE:
  238.         printf(" acquire");
  239.         switch (code) {
  240.         case EGPC_REQUEST:
  241.         case EGPC_CONFIRM:
  242.             printf(" %s", egp_acquire_codes[code]);
  243.             switch (status) {
  244.             case EGPS_UNSPEC:
  245.             case EGPS_ACTIVE:
  246.             case EGPS_PASSIVE:
  247.                 printf(" %s", egp_acquire_status[status]);
  248.                 break;
  249.                 
  250.             default:
  251.                 printf(" [status %d]", status);
  252.                 break;
  253.             }
  254.             printf(" hello:%d poll:%d",
  255.                    ntohs(egp->egp_hello),
  256.                    ntohs(egp->egp_poll));
  257.             break;
  258.             
  259.         case EGPC_REFUSE:
  260.         case EGPC_CEASE:
  261.         case EGPC_CEASEACK:
  262.             printf(" %s", egp_acquire_codes[code]);
  263.             switch (status ) {
  264.             case EGPS_UNSPEC:
  265.             case EGPS_NORES:
  266.             case EGPS_ADMIN:
  267.             case EGPS_GODOWN:
  268.             case EGPS_PARAM:
  269.             case EGPS_PROTO:
  270.                 printf(" %s", egp_acquire_status[status]);
  271.                 break;
  272.                 
  273.             default:
  274.                 printf("[status %d], status");
  275.                 break;
  276.             }
  277.             break;
  278.             
  279.         default:
  280.             printf("[code %d]", code);
  281.             break;
  282.         }
  283.         break;
  284.         
  285.     case EGPT_REACH:
  286.         switch (code) {
  287.  
  288.         case EGPC_HELLO:
  289.         case EGPC_HEARDU:
  290.             printf(" %s", egp_reach_codes[code]);
  291.             if (status <= EGPS_DOWN)
  292.                 printf(" state:%s", egp_status_updown[status]);
  293.             else
  294.                 printf(" [status %d], status");
  295.             break;
  296.             
  297.         default:
  298.             printf("[reach code %d], code");
  299.             break;
  300.         }
  301.         break;
  302.         
  303.     case EGPT_POLL:
  304.         printf(" poll");
  305.         if (egp->egp_status <= EGPS_DOWN)
  306.             printf(" state:%s", egp_status_updown[status]);
  307.         else
  308.             printf(" [status %d]", status);
  309.         printf(" net:%s", intoa(egp->egp_sourcenet));
  310.         break;
  311.         
  312.     case EGPT_UPDATE:
  313.         printf(" update");
  314.         if (status & EGPS_UNSOL) {
  315.             status &= ~EGPS_UNSOL;
  316.             printf(" unsolicitied");
  317.         }
  318.         if (status <= EGPS_DOWN)
  319.             printf(" state:%s", egp_status_updown[status]);
  320.         else
  321.             printf(" [status %d]", status);
  322.         printf(" %s int %d ext %d",
  323.                intoa(egp->egp_sourcenet),
  324.                egp->egp_intgw,
  325.                egp->egp_extgw);
  326.         if (vflag)
  327.             egpnrprint(egp, length);
  328.         break;
  329.         
  330.     case EGPT_ERROR:
  331.         printf(" error");
  332.         if (status <= EGPS_DOWN)
  333.             printf(" state:%s", egp_status_updown[status]);
  334.         else
  335.             printf(" [status]", status);
  336.  
  337.         if (ntohs(egp->egp_reason) <= EGPR_UVERSION)
  338.             printf(" %s", egp_reasons[ntohs(egp->egp_reason)]);
  339.         else
  340.             printf(" [reason]", ntohs(egp->egp_reason));
  341.         break;
  342.         
  343.     default:
  344.         printf("[type %d]", type);
  345.         break;
  346.     }
  347. }
  348.