X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/3824a6c0417a551961d1a1bf4f94f10eff736afc..9f957a5883cb4c5c99cefa71b42fc9d2d27d73e1:/print-egp.c diff --git a/print-egp.c b/print-egp.c index f75f3d55..b4a5a970 100644 --- a/print-egp.c +++ b/print-egp.c @@ -18,35 +18,32 @@ * Initial contribution from Jeff Honig (jch@MITCHELL.CIT.CORNELL.EDU). */ +/* \summary: Exterior Gateway Protocol (EGP) printer */ + +/* specification: RFC 827 */ #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include +#include -#include +#include -#include "interface.h" +#include "netdissect.h" #include "addrtoname.h" #include "extract.h" -#ifndef lint -static const char rcsid[] _U_ = - "@(#) $Header: /tcpdump/master/tcpdump/print-egp.c,v 1.35 2003-11-15 00:39:20 guy Exp $ (LBL)"; -#endif -#include "ip.h" - struct egp_packet { - u_int8_t egp_version; + nd_uint8_t egp_version; #define EGP_VERSION 2 - u_int8_t egp_type; + nd_uint8_t egp_type; #define EGPT_ACQUIRE 3 #define EGPT_REACH 5 #define EGPT_POLL 2 #define EGPT_UPDATE 1 #define EGPT_ERROR 8 - u_int8_t egp_code; + nd_uint8_t egp_code; #define EGPC_REQUEST 0 #define EGPC_CONFIRM 1 #define EGPC_REFUSE 2 @@ -54,7 +51,7 @@ struct egp_packet { #define EGPC_CEASEACK 4 #define EGPC_HELLO 0 #define EGPC_HEARDU 1 - u_int8_t egp_status; + nd_uint8_t egp_status; #define EGPS_UNSPEC 0 #define EGPS_ACTIVE 1 #define EGPS_PASSIVE 2 @@ -67,13 +64,13 @@ struct egp_packet { #define EGPS_UP 1 #define EGPS_DOWN 2 #define EGPS_UNSOL 0x80 - u_int16_t egp_checksum; - u_int16_t egp_as; - u_int16_t egp_sequence; + nd_uint16_t egp_checksum; + nd_uint16_t egp_as; + nd_uint16_t egp_sequence; union { - u_int16_t egpu_hello; - u_int8_t egpu_gws[2]; - u_int16_t egpu_reason; + nd_uint16_t egpu_hello; + nd_uint8_t egpu_gws[2]; + nd_uint16_t egpu_reason; #define EGPR_UNSPEC 0 #define EGPR_BADHEAD 1 #define EGPR_BADDATA 2 @@ -87,14 +84,14 @@ struct egp_packet { #define egp_extgw egp_handg.egpu_gws[1] #define egp_reason egp_handg.egpu_reason union { - u_int16_t egpu_poll; - u_int32_t egpu_sourcenet; + nd_uint16_t egpu_poll; + nd_ipv4 egpu_sourcenet; } egp_pands; #define egp_poll egp_pands.egpu_poll #define egp_sourcenet egp_pands.egpu_sourcenet }; -const char *egp_acquire_codes[] = { +static const char *egp_acquire_codes[] = { "request", "confirm", "refuse", @@ -102,7 +99,7 @@ const char *egp_acquire_codes[] = { "cease_ack" }; -const char *egp_acquire_status[] = { +static const char *egp_acquire_status[] = { "unspecified", "active_mode", "passive_mode", @@ -113,18 +110,18 @@ const char *egp_acquire_status[] = { "protocol_violation" }; -const char *egp_reach_codes[] = { +static const char *egp_reach_codes[] = { "hello", "i-h-u" }; -const char *egp_status_updown[] = { +static const char *egp_status_updown[] = { "indeterminate", "up", "down" }; -const char *egp_reasons[] = { +static const char *egp_reasons[] = { "unspecified", "bad_EGP_header_format", "bad_EGP_data_field_format", @@ -135,17 +132,18 @@ const char *egp_reasons[] = { }; static void -egpnrprint(register const struct egp_packet *egp) +egpnrprint(netdissect_options *ndo, + const struct egp_packet *egp, u_int length) { - register const u_int8_t *cp; - u_int32_t addr; - register u_int32_t net; - register u_int netlen; - int gateways, distances, networks; - int t_gateways; + const uint8_t *cp; + uint32_t addr; + uint32_t net; + u_int netlen; + u_int gateways, distances, networks; + u_int intgw, extgw, t_gateways; const char *comma; - addr = egp->egp_sourcenet; + addr = EXTRACT_IPV4_TO_NETWORK_ORDER(egp->egp_sourcenet); if (IN_CLASSA(addr)) { net = addr & IN_CLASSA_NET; netlen = 1; @@ -159,112 +157,159 @@ egpnrprint(register const struct egp_packet *egp) net = 0; netlen = 0; } - cp = (u_int8_t *)(egp + 1); + cp = (const uint8_t *)(egp + 1); + length -= sizeof(*egp); - t_gateways = egp->egp_intgw + egp->egp_extgw; + intgw = EXTRACT_U_1(egp->egp_intgw); + extgw = EXTRACT_U_1(egp->egp_extgw); + t_gateways = intgw + extgw; for (gateways = 0; gateways < t_gateways; ++gateways) { /* Pickup host part of gateway address */ addr = 0; - TCHECK2(cp[0], 4 - netlen); + if (length < 4 - netlen) + goto trunc; + ND_TCHECK_LEN(cp, 4 - netlen); switch (netlen) { case 1: - addr = *cp++; + addr = EXTRACT_U_1(cp); + cp++; /* fall through */ case 2: - addr = (addr << 8) | *cp++; + addr = (addr << 8) | EXTRACT_U_1(cp); + cp++; /* fall through */ case 3: - addr = (addr << 8) | *cp++; + addr = (addr << 8) | EXTRACT_U_1(cp); + cp++; + break; } addr |= net; - TCHECK2(cp[0], 1); - distances = *cp++; - printf(" %s %s ", - gateways < (int)egp->egp_intgw ? "int" : "ext", - ipaddr_string(&addr)); + length -= 4 - netlen; + if (length < 1) + goto trunc; + ND_TCHECK_1(cp); + distances = EXTRACT_U_1(cp); + cp++; + length--; + ND_PRINT((ndo, " %s %s ", + gateways < intgw ? "int" : "ext", + ipaddr_string(ndo, &addr))); comma = ""; - putchar('('); - while (--distances >= 0) { - TCHECK2(cp[0], 2); - printf("%sd%d:", comma, (int)*cp++); + ND_PRINT((ndo, "(")); + while (distances != 0) { + if (length < 2) + goto trunc; + ND_TCHECK_2(cp); + ND_PRINT((ndo, "%sd%u:", comma, EXTRACT_U_1(cp))); + cp++; comma = ", "; - networks = *cp++; - while (--networks >= 0) { + networks = EXTRACT_U_1(cp); + cp++; + length -= 2; + while (networks != 0) { /* Pickup network number */ - TCHECK2(cp[0], 1); - addr = (u_int32_t)*cp++ << 24; + if (length < 1) + goto trunc; + ND_TCHECK_1(cp); + addr = ((uint32_t) EXTRACT_U_1(cp)) << 24; + cp++; + length--; if (IN_CLASSB(addr)) { - TCHECK2(cp[0], 1); - addr |= (u_int32_t)*cp++ << 16; + if (length < 1) + goto trunc; + ND_TCHECK_1(cp); + addr |= ((uint32_t) EXTRACT_U_1(cp)) << 16; + cp++; + length--; } else if (!IN_CLASSA(addr)) { - TCHECK2(cp[0], 2); - addr |= (u_int32_t)*cp++ << 16; - addr |= (u_int32_t)*cp++ << 8; + if (length < 2) + goto trunc; + ND_TCHECK_2(cp); + addr |= ((uint32_t) EXTRACT_U_1(cp)) << 16; + cp++; + addr |= ((uint32_t) EXTRACT_U_1(cp)) << 8; + cp++; + length -= 2; } - printf(" %s", ipaddr_string(&addr)); + ND_PRINT((ndo, " %s", ipaddr_string(ndo, &addr))); + networks--; } + distances--; } - putchar(')'); + ND_PRINT((ndo, ")")); } return; trunc: - fputs("[|]", stdout); + ND_PRINT((ndo, "[|]")); } void -egp_print(register const u_int8_t *bp) +egp_print(netdissect_options *ndo, + const uint8_t *bp, u_int length) { - register const struct egp_packet *egp; - register int status; - register int code; - register int type; - - egp = (struct egp_packet *)bp; - if (!TTEST(*egp)) { - printf("[|egp]"); + const struct egp_packet *egp; + u_int version; + u_int type; + u_int code; + u_int status; + + egp = (const struct egp_packet *)bp; + if (length < sizeof(*egp) || !ND_TTEST(*egp)) { + ND_PRINT((ndo, "[|egp]")); return; } - (void)printf("egp: "); - if (egp->egp_version != EGP_VERSION) { - printf("[version %d]", egp->egp_version); + version = EXTRACT_U_1(egp->egp_version); + if (!ndo->ndo_vflag) { + ND_PRINT((ndo, "EGPv%u, AS %u, seq %u, length %u", + version, + EXTRACT_BE_U_2(egp->egp_as), + EXTRACT_BE_U_2(egp->egp_sequence), + length)); + return; + } else + ND_PRINT((ndo, "EGPv%u, length %u", + version, + length)); + + if (version != EGP_VERSION) { + ND_PRINT((ndo, "[version %u]", version)); return; } - printf("as:%d seq:%d", EXTRACT_16BITS(&egp->egp_as), EXTRACT_16BITS(&egp->egp_sequence)); - type = egp->egp_type; - code = egp->egp_code; - status = egp->egp_status; + type = EXTRACT_U_1(egp->egp_type); + code = EXTRACT_U_1(egp->egp_code); + status = EXTRACT_U_1(egp->egp_status); switch (type) { case EGPT_ACQUIRE: - printf(" acquire"); + ND_PRINT((ndo, " acquire")); switch (code) { case EGPC_REQUEST: case EGPC_CONFIRM: - printf(" %s", egp_acquire_codes[code]); + ND_PRINT((ndo, " %s", egp_acquire_codes[code])); switch (status) { case EGPS_UNSPEC: case EGPS_ACTIVE: case EGPS_PASSIVE: - printf(" %s", egp_acquire_status[status]); + ND_PRINT((ndo, " %s", egp_acquire_status[status])); break; default: - printf(" [status %d]", status); + ND_PRINT((ndo, " [status %u]", status)); break; } - printf(" hello:%d poll:%d", - EXTRACT_16BITS(&egp->egp_hello), - EXTRACT_16BITS(&egp->egp_poll)); + ND_PRINT((ndo, " hello:%u poll:%u", + EXTRACT_BE_U_2(egp->egp_hello), + EXTRACT_BE_U_2(egp->egp_poll))); break; case EGPC_REFUSE: case EGPC_CEASE: case EGPC_CEASEACK: - printf(" %s", egp_acquire_codes[code]); + ND_PRINT((ndo, " %s", egp_acquire_codes[code])); switch (status ) { case EGPS_UNSPEC: case EGPS_NORES: @@ -272,17 +317,17 @@ egp_print(register const u_int8_t *bp) case EGPS_GODOWN: case EGPS_PARAM: case EGPS_PROTO: - printf(" %s", egp_acquire_status[status]); + ND_PRINT((ndo, " %s", egp_acquire_status[status])); break; default: - printf("[status %d]", status); + ND_PRINT((ndo, "[status %u]", status)); break; } break; default: - printf("[code %d]", code); + ND_PRINT((ndo, "[code %u]", code)); break; } break; @@ -292,61 +337,61 @@ egp_print(register const u_int8_t *bp) case EGPC_HELLO: case EGPC_HEARDU: - printf(" %s", egp_reach_codes[code]); + ND_PRINT((ndo, " %s", egp_reach_codes[code])); if (status <= EGPS_DOWN) - printf(" state:%s", egp_status_updown[status]); + ND_PRINT((ndo, " state:%s", egp_status_updown[status])); else - printf(" [status %d]", status); + ND_PRINT((ndo, " [status %u]", status)); break; default: - printf("[reach code %d]", code); + ND_PRINT((ndo, "[reach code %u]", code)); break; } break; case EGPT_POLL: - printf(" poll"); - if (egp->egp_status <= EGPS_DOWN) - printf(" state:%s", egp_status_updown[status]); + ND_PRINT((ndo, " poll")); + if (status <= EGPS_DOWN) + ND_PRINT((ndo, " state:%s", egp_status_updown[status])); else - printf(" [status %d]", status); - printf(" net:%s", ipaddr_string(&egp->egp_sourcenet)); + ND_PRINT((ndo, " [status %u]", status)); + ND_PRINT((ndo, " net:%s", ipaddr_string(ndo, &egp->egp_sourcenet))); break; case EGPT_UPDATE: - printf(" update"); + ND_PRINT((ndo, " update")); if (status & EGPS_UNSOL) { status &= ~EGPS_UNSOL; - printf(" unsolicited"); + ND_PRINT((ndo, " unsolicited")); } if (status <= EGPS_DOWN) - printf(" state:%s", egp_status_updown[status]); + ND_PRINT((ndo, " state:%s", egp_status_updown[status])); else - printf(" [status %d]", status); - printf(" %s int %d ext %d", - ipaddr_string(&egp->egp_sourcenet), - egp->egp_intgw, - egp->egp_extgw); - if (vflag) - egpnrprint(egp); + ND_PRINT((ndo, " [status %u]", status)); + ND_PRINT((ndo, " %s int %u ext %u", + ipaddr_string(ndo, &egp->egp_sourcenet), + EXTRACT_U_1(egp->egp_intgw), + EXTRACT_U_1(egp->egp_extgw))); + if (ndo->ndo_vflag) + egpnrprint(ndo, egp, length); break; case EGPT_ERROR: - printf(" error"); + ND_PRINT((ndo, " error")); if (status <= EGPS_DOWN) - printf(" state:%s", egp_status_updown[status]); + ND_PRINT((ndo, " state:%s", egp_status_updown[status])); else - printf(" [status %d]", status); + ND_PRINT((ndo, " [status %u]", status)); - if (EXTRACT_16BITS(&egp->egp_reason) <= EGPR_UVERSION) - printf(" %s", egp_reasons[EXTRACT_16BITS(&egp->egp_reason)]); + if (EXTRACT_BE_U_2(egp->egp_reason) <= EGPR_UVERSION) + ND_PRINT((ndo, " %s", egp_reasons[EXTRACT_BE_U_2(egp->egp_reason)])); else - printf(" [reason %d]", EXTRACT_16BITS(&egp->egp_reason)); + ND_PRINT((ndo, " [reason %u]", EXTRACT_BE_U_2(egp->egp_reason))); break; default: - printf("[type %d]", type); + ND_PRINT((ndo, "[type %u]", type)); break; } }