]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-pppoe.c
Makefile.in: don't remove configure and config.h.in in make distclean.
[tcpdump] / print-pppoe.c
index 8a2870a9517dfd23fb258271551dcf9e4c031725..65518dff16d97adde75b1d7eb537d602354f7998 100644 (file)
  * Original code by Greg Stark <[email protected]>
  */
 
+/* \summary: PPP-over-Ethernet (PPPoE) printer */
+
 #ifdef HAVE_CONFIG_H
-#include "config.h"
+#include <config.h>
 #endif
 
-#include <tcpdump-stdinc.h>
+#include "netdissect-stdinc.h"
+
+#include "netdissect-ctype.h"
 
-#include "interface.h"
-#include "extract.h"                   /* must come after interface.h */
+#define ND_LONGJMP_FROM_TCHECK
+#include "netdissect.h"
+#include "extract.h"
 
 /* Codes */
 enum {
@@ -82,50 +87,52 @@ static const struct tok pppoetag2str[] = {
 #define PPPOE_HDRLEN 6
 #define MAXTAGPRINT 80
 
-u_int
-pppoe_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, register const u_char *p)
+void
+pppoe_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
 {
-       return (pppoe_print(ndo, p, h->len));
+       ndo->ndo_protocol = "pppoe";
+       ndo->ndo_ll_hdr_len += pppoe_print(ndo, p, h->len);
 }
 
 u_int
-pppoe_print(netdissect_options *ndo, register const u_char *bp, u_int length)
+pppoe_print(netdissect_options *ndo, const u_char *bp, u_int length)
 {
        uint16_t pppoe_ver, pppoe_type, pppoe_code, pppoe_sessionid;
        u_int pppoe_length;
        const u_char *pppoe_packet, *pppoe_payload;
 
+       ndo->ndo_protocol = "pppoe";
        if (length < PPPOE_HDRLEN) {
-               ND_PRINT((ndo, "truncated-pppoe %u", length));
-               return (length);
+               ND_PRINT(" (length %u < %u)", length, PPPOE_HDRLEN);
+               goto invalid;
        }
        length -= PPPOE_HDRLEN;
        pppoe_packet = bp;
-       ND_TCHECK2(*pppoe_packet, PPPOE_HDRLEN);
-       pppoe_ver  = (pppoe_packet[0] & 0xF0) >> 4;
-       pppoe_type  = (pppoe_packet[0] & 0x0F);
-       pppoe_code = pppoe_packet[1];
-       pppoe_sessionid = EXTRACT_16BITS(pppoe_packet + 2);
-       pppoe_length    = EXTRACT_16BITS(pppoe_packet + 4);
+       ND_TCHECK_LEN(pppoe_packet, PPPOE_HDRLEN);
+       pppoe_ver  = (GET_U_1(pppoe_packet) & 0xF0) >> 4;
+       pppoe_type  = (GET_U_1(pppoe_packet) & 0x0F);
+       pppoe_code = GET_U_1(pppoe_packet + 1);
+       pppoe_sessionid = GET_BE_U_2(pppoe_packet + 2);
+       pppoe_length    = GET_BE_U_2(pppoe_packet + 4);
        pppoe_payload = pppoe_packet + PPPOE_HDRLEN;
 
        if (pppoe_ver != 1) {
-               ND_PRINT((ndo, " [ver %d]",pppoe_ver));
+               ND_PRINT(" [ver %u]",pppoe_ver);
        }
        if (pppoe_type != 1) {
-               ND_PRINT((ndo, " [type %d]",pppoe_type));
+               ND_PRINT(" [type %u]",pppoe_type);
        }
 
-       ND_PRINT((ndo, "PPPoE %s", tok2str(pppoecode2str, "PAD-%x", pppoe_code)));
+       ND_PRINT("PPPoE %s", tok2str(pppoecode2str, "PAD-%x", pppoe_code));
        if (pppoe_code == PPPOE_PADI && pppoe_length > 1484 - PPPOE_HDRLEN) {
-               ND_PRINT((ndo, " [len %u!]",pppoe_length));
+               ND_PRINT(" [len %u!]",pppoe_length);
        }
        if (pppoe_length > length) {
-               ND_PRINT((ndo, " [len %u > %u!]", pppoe_length, length));
+               ND_PRINT(" [len %u > %u!]", pppoe_length, length);
                pppoe_length = length;
        }
        if (pppoe_sessionid) {
-               ND_PRINT((ndo, " [ses 0x%x]", pppoe_sessionid));
+               ND_PRINT(" [ses 0x%x]", pppoe_sessionid);
        }
 
        if (pppoe_code) {
@@ -139,61 +146,61 @@ pppoe_print(netdissect_options *ndo, register const u_char *bp, u_int length)
                 * tag_type is previous tag or 0xffff for first iteration
                 */
                while (tag_type && p < pppoe_payload + pppoe_length) {
-                       ND_TCHECK2(*p, 4);
-                       tag_type = EXTRACT_16BITS(p);
-                       tag_len = EXTRACT_16BITS(p + 2);
+                       tag_type = GET_BE_U_2(p);
+                       tag_len = GET_BE_U_2(p + 2);
                        p += 4;
                        /* p points to tag_value */
 
                        if (tag_len) {
-                               unsigned isascii = 0, isgarbage = 0;
+                               unsigned ascii_count = 0, garbage_count = 0;
                                const u_char *v;
                                char tag_str[MAXTAGPRINT];
                                unsigned tag_str_len = 0;
 
                                /* TODO print UTF-8 decoded text */
-                               ND_TCHECK2(*p, tag_len);
+                               ND_TCHECK_LEN(p, tag_len);
                                for (v = p; v < p + tag_len && tag_str_len < MAXTAGPRINT-1; v++)
-                                       if (*v >= 32 && *v < 127) {
-                                               tag_str[tag_str_len++] = *v;
-                                               isascii++;
+                                       if (ND_ASCII_ISPRINT(GET_U_1(v))) {
+                                               tag_str[tag_str_len++] = GET_U_1(v);
+                                               ascii_count++;
                                        } else {
                                                tag_str[tag_str_len++] = '.';
-                                               isgarbage++;
+                                               garbage_count++;
                                        }
                                tag_str[tag_str_len] = 0;
 
-                               if (isascii > isgarbage) {
-                                       ND_PRINT((ndo, " [%s \"%*.*s\"]",
+                               if (ascii_count > garbage_count) {
+                                       ND_PRINT(" [%s \"%*.*s\"]",
                                               tok2str(pppoetag2str, "TAG-0x%x", tag_type),
                                               (int)tag_str_len,
                                               (int)tag_str_len,
-                                              tag_str));
+                                              tag_str);
                                } else {
                                        /* Print hex, not fast to abuse printf but this doesn't get used much */
-                                       ND_PRINT((ndo, " [%s 0x", tok2str(pppoetag2str, "TAG-0x%x", tag_type)));
+                                       ND_PRINT(" [%s 0x", tok2str(pppoetag2str, "TAG-0x%x", tag_type));
                                        for (v=p; v<p+tag_len; v++) {
-                                               ND_PRINT((ndo, "%02X", *v));
+                                               ND_PRINT("%02X", GET_U_1(v));
                                        }
-                                       ND_PRINT((ndo, "]"));
+                                       ND_PRINT("]");
                                }
 
 
                        } else
-                               ND_PRINT((ndo, " [%s]", tok2str(pppoetag2str,
-                                   "TAG-0x%x", tag_type)));
+                               ND_PRINT(" [%s]", tok2str(pppoetag2str,
+                                   "TAG-0x%x", tag_type));
 
                        p += tag_len;
                        /* p points to next tag */
                }
-               return (0);
+               return PPPOE_HDRLEN;
        } else {
                /* PPPoE data */
-               ND_PRINT((ndo, " "));
+               ND_PRINT(" ");
                return (PPPOE_HDRLEN + ppp_print(ndo, pppoe_payload, pppoe_length));
        }
+       /* NOTREACHED */
 
-trunc:
-       ND_PRINT((ndo, "[|pppoe]"));
-       return (PPPOE_HDRLEN);
+invalid:
+       nd_print_invalid(ndo);
+       return 0;
 }