]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-telnet.c
On Solaris, for 64-bit builds, use the 64-bit pcap-config.
[tcpdump] / print-telnet.c
index 6a2680b413c8140a76e7da4f017fb342194b8898..b0283f2047f7a38b305194a3066ab70ea69b8a60 100644 (file)
  *      are preserved in all copies.
  */
 
  *      are preserved in all copies.
  */
 
-#define NETDISSECT_REWORKED
+/* \summary: Telnet option printer */
+
 #ifdef HAVE_CONFIG_H
 #ifdef HAVE_CONFIG_H
-#include "config.h"
+#include <config.h>
 #endif
 
 #endif
 
-#include <tcpdump-stdinc.h>
+#include "netdissect-stdinc.h"
 
 #include <stdio.h>
 
 
 #include <stdio.h>
 
-#include "interface.h"
+#include "netdissect.h"
+#include "extract.h"
 
 
-#define TELCMDS
-#define TELOPTS
 
 /*     NetBSD: telnet.h,v 1.9 2001/06/11 01:50:50 wiz Exp      */
 
 
 /*     NetBSD: telnet.h,v 1.9 2001/06/11 01:50:50 wiz Exp      */
 
 
 #define SYNCH  242             /* for telfunc calls */
 
 
 #define SYNCH  242             /* for telfunc calls */
 
-#ifdef TELCMDS
-const char *telcmds[] = {
+static const char *telcmds[] = {
        "EOF", "SUSP", "ABORT", "EOR",
        "SE", "NOP", "DMARK", "BRK", "IP", "AO", "AYT", "EC",
        "EL", "GA", "SB", "WILL", "WONT", "DO", "DONT", "IAC", 0,
 };
        "EOF", "SUSP", "ABORT", "EOR",
        "SE", "NOP", "DMARK", "BRK", "IP", "AO", "AYT", "EC",
        "EL", "GA", "SB", "WILL", "WONT", "DO", "DONT", "IAC", 0,
 };
-#else
-extern char *telcmds[];
-#endif
 
 #define        TELCMD_FIRST    xEOF
 #define        TELCMD_LAST     IAC
 
 #define        TELCMD_FIRST    xEOF
 #define        TELCMD_LAST     IAC
@@ -148,8 +144,7 @@ extern char *telcmds[];
 
 
 #define        NTELOPTS        (1+TELOPT_NEW_ENVIRON)
 
 
 #define        NTELOPTS        (1+TELOPT_NEW_ENVIRON)
-#ifdef TELOPTS
-const char *telopts[NTELOPTS+1] = {
+static const char *telopts[NTELOPTS+1] = {
        "BINARY", "ECHO", "RCP", "SUPPRESS GO AHEAD", "NAME",
        "STATUS", "TIMING MARK", "RCTE", "NAOL", "NAOP",
        "NAOCRD", "NAOHTS", "NAOHTD", "NAOFFD", "NAOVTS",
        "BINARY", "ECHO", "RCP", "SUPPRESS GO AHEAD", "NAME",
        "STATUS", "TIMING MARK", "RCTE", "NAOL", "NAOP",
        "NAOCRD", "NAOHTS", "NAOHTD", "NAOFFD", "NAOVTS",
@@ -166,7 +161,6 @@ const char *telopts[NTELOPTS+1] = {
 #define        TELOPT_LAST     TELOPT_NEW_ENVIRON
 #define        TELOPT_OK(x)    ((unsigned int)(x) <= TELOPT_LAST)
 #define        TELOPT(x)       telopts[(x)-TELOPT_FIRST]
 #define        TELOPT_LAST     TELOPT_NEW_ENVIRON
 #define        TELOPT_OK(x)    ((unsigned int)(x) <= TELOPT_LAST)
 #define        TELOPT(x)       telopts[(x)-TELOPT_FIRST]
-#endif
 
 /* sub-option qualifiers */
 #define        TELQUAL_IS      0       /* option is... */
 
 /* sub-option qualifiers */
 #define        TELQUAL_IS      0       /* option is... */
@@ -393,8 +387,8 @@ telnet_parse(netdissect_options *ndo, const u_char *sp, u_int length, int print)
        do { \
                if (length < 1) \
                        goto pktend; \
        do { \
                if (length < 1) \
                        goto pktend; \
-               ND_TCHECK(*sp); \
-               c = *sp++; \
+               c = GET_U_1(sp); \
+               sp++; \
                length--; \
        } while (0)
 
                length--; \
        } while (0)
 
@@ -406,7 +400,7 @@ telnet_parse(netdissect_options *ndo, const u_char *sp, u_int length, int print)
        FETCH(c, sp, length);
        if (c == IAC) {         /* <IAC><IAC>! */
                if (print)
        FETCH(c, sp, length);
        if (c == IAC) {         /* <IAC><IAC>! */
                if (print)
-                       ND_PRINT((ndo, "IAC IAC"));
+                       ND_PRINT("IAC IAC");
                goto done;
        }
 
                goto done;
        }
 
@@ -424,21 +418,21 @@ telnet_parse(netdissect_options *ndo, const u_char *sp, u_int length, int print)
                FETCH(x, sp, length);
                if (x >= 0 && x < NTELOPTS) {
                        if (print)
                FETCH(x, sp, length);
                if (x >= 0 && x < NTELOPTS) {
                        if (print)
-                               ND_PRINT((ndo, "%s %s", telcmds[i], telopts[x]));
+                               ND_PRINT("%s %s", telcmds[i], telopts[x]);
                } else {
                        if (print)
                } else {
                        if (print)
-                               ND_PRINT((ndo, "%s %#x", telcmds[i], x));
+                               ND_PRINT("%s %#x", telcmds[i], x);
                }
                if (c != SB)
                        break;
                /* IAC SB .... IAC SE */
                p = sp;
                while (length > (u_int)(p + 1 - sp)) {
                }
                if (c != SB)
                        break;
                /* IAC SB .... IAC SE */
                p = sp;
                while (length > (u_int)(p + 1 - sp)) {
-                       if (p[0] == IAC && p[1] == SE)
+                       if (GET_U_1(p) == IAC && GET_U_1(p + 1) == SE)
                                break;
                        p++;
                }
                                break;
                        p++;
                }
-               if (*p != IAC)
+               if (GET_U_1(p) != IAC)
                        goto pktend;
 
                switch (x) {
                        goto pktend;
 
                switch (x) {
@@ -447,54 +441,52 @@ telnet_parse(netdissect_options *ndo, const u_char *sp, u_int length, int print)
                                break;
                        FETCH(c, sp, length);
                        if (print)
                                break;
                        FETCH(c, sp, length);
                        if (print)
-                               ND_PRINT((ndo, " %s", STR_OR_ID(c, authcmd)));
+                               ND_PRINT(" %s", STR_OR_ID(c, authcmd));
                        if (p <= sp)
                                break;
                        FETCH(c, sp, length);
                        if (print)
                        if (p <= sp)
                                break;
                        FETCH(c, sp, length);
                        if (print)
-                               ND_PRINT((ndo, " %s", STR_OR_ID(c, authtype)));
+                               ND_PRINT(" %s", STR_OR_ID(c, authtype));
                        break;
                case TELOPT_ENCRYPT:
                        if (p <= sp)
                                break;
                        FETCH(c, sp, length);
                        if (print)
                        break;
                case TELOPT_ENCRYPT:
                        if (p <= sp)
                                break;
                        FETCH(c, sp, length);
                        if (print)
-                               ND_PRINT((ndo, " %s", STR_OR_ID(c, enccmd)));
+                               ND_PRINT(" %s", STR_OR_ID(c, enccmd));
                        if (p <= sp)
                                break;
                        FETCH(c, sp, length);
                        if (print)
                        if (p <= sp)
                                break;
                        FETCH(c, sp, length);
                        if (print)
-                               ND_PRINT((ndo, " %s", STR_OR_ID(c, enctype)));
+                               ND_PRINT(" %s", STR_OR_ID(c, enctype));
                        break;
                default:
                        if (p <= sp)
                                break;
                        FETCH(c, sp, length);
                        if (print)
                        break;
                default:
                        if (p <= sp)
                                break;
                        FETCH(c, sp, length);
                        if (print)
-                               ND_PRINT((ndo, " %s", STR_OR_ID(c, cmds)));
+                               ND_PRINT(" %s", STR_OR_ID(c, cmds));
                        break;
                }
                while (p > sp) {
                        FETCH(x, sp, length);
                        if (print)
                        break;
                }
                while (p > sp) {
                        FETCH(x, sp, length);
                        if (print)
-                               ND_PRINT((ndo, " %#x", x));
+                               ND_PRINT(" %#x", x);
                }
                /* terminating IAC SE */
                if (print)
                }
                /* terminating IAC SE */
                if (print)
-                       ND_PRINT((ndo, " SE"));
+                       ND_PRINT(" SE");
                sp += 2;
                break;
        default:
                if (print)
                sp += 2;
                break;
        default:
                if (print)
-                       ND_PRINT((ndo, "%s", telcmds[i]));
+                       ND_PRINT("%s", telcmds[i]);
                goto done;
        }
 
 done:
                goto done;
        }
 
 done:
-       return sp - osp;
+       return (int)(sp - osp);
 
 
-trunc:
-       ND_PRINT((ndo, "[|telnet]"));
 pktend:
        return -1;
 #undef FETCH
 pktend:
        return -1;
 #undef FETCH
@@ -507,9 +499,14 @@ telnet_print(netdissect_options *ndo, const u_char *sp, u_int length)
        const u_char *osp;
        int l;
 
        const u_char *osp;
        int l;
 
+       ndo->ndo_protocol = "telnet";
        osp = sp;
 
        osp = sp;
 
-       while (length > 0 && *sp == IAC) {
+       while (length > 0 && GET_U_1(sp) == IAC) {
+               /*
+                * Parse the Telnet command without printing it,
+                * to determine its length.
+                */
                l = telnet_parse(ndo, sp, length, 0);
                if (l < 0)
                        break;
                l = telnet_parse(ndo, sp, length, 0);
                if (l < 0)
                        break;
@@ -519,14 +516,14 @@ telnet_print(netdissect_options *ndo, const u_char *sp, u_int length)
                 */
                if (ndo->ndo_Xflag && 2 < ndo->ndo_vflag) {
                        if (first)
                 */
                if (ndo->ndo_Xflag && 2 < ndo->ndo_vflag) {
                        if (first)
-                               ND_PRINT((ndo, "\nTelnet:"));
-                       hex_print_with_offset(ndo, "\n", sp, l, sp - osp);
+                               ND_PRINT("\nTelnet:");
+                       hex_print_with_offset(ndo, "\n", sp, l, (u_int)(sp - osp));
                        if (l > 8)
                        if (l > 8)
-                               ND_PRINT((ndo, "\n\t\t\t\t"));
+                               ND_PRINT("\n\t\t\t\t");
                        else
                        else
-                               ND_PRINT((ndo, "%*s\t", (8 - l) * 3, ""));
+                               ND_PRINT("%*s\t", (8 - l) * 3, "");
                } else
                } else
-                       ND_PRINT((ndo, "%s", (first) ? " [telnet " : ", "));
+                       ND_PRINT("%s", (first) ? " [telnet " : ", ");
 
                (void)telnet_parse(ndo, sp, length, 1);
                first = 0;
 
                (void)telnet_parse(ndo, sp, length, 1);
                first = 0;
@@ -536,8 +533,8 @@ telnet_print(netdissect_options *ndo, const u_char *sp, u_int length)
        }
        if (!first) {
                if (ndo->ndo_Xflag && 2 < ndo->ndo_vflag)
        }
        if (!first) {
                if (ndo->ndo_Xflag && 2 < ndo->ndo_vflag)
-                       ND_PRINT((ndo, "\n"));
+                       ND_PRINT("\n");
                else
                else
-                       ND_PRINT((ndo, "]"));
+                       ND_PRINT("]");
        }
 }
        }
 }