]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-telnet.c
Use more the EXTRACT_U_1() macro (40/n)
[tcpdump] / print-telnet.c
index 44a48f475a708c60e870dcbdac0ca18418cd0d49..e3782177a9f4c0534bb3759030479cf37ec51e74 100644 (file)
  *      are preserved in all copies.
  */
 
  *      are preserved in all copies.
  */
 
+/* \summary: Telnet option printer */
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#ifndef lint
-static const char rcsid[] _U_ =
-     "@(#) $Header: /tcpdump/master/tcpdump/print-telnet.c,v 1.24 2003-12-29 11:05:10 hannes Exp $";
-#endif
-
-#include <tcpdump-stdinc.h>
+#include <netdissect-stdinc.h>
 
 #include <stdio.h>
 
 #include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 
 
-#include "interface.h"
-#include "addrtoname.h"
+#include "netdissect.h"
+#include "extract.h"
+
+static const char tstr[] = " [|telnet]";
 
 #define TELCMDS
 #define TELOPTS
 
 #define TELCMDS
 #define TELOPTS
@@ -95,7 +92,7 @@ static const char rcsid[] _U_ =
 #define SYNCH  242             /* for telfunc calls */
 
 #ifdef TELCMDS
 #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,
@@ -156,7 +153,7 @@ extern char *telcmds[];
 
 #define        NTELOPTS        (1+TELOPT_NEW_ENVIRON)
 #ifdef TELOPTS
 
 #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",
@@ -391,7 +388,7 @@ numstr(int x)
 
 /* sp points to IAC byte */
 static int
 
 /* sp points to IAC byte */
 static int
-telnet_parse(const u_char *sp, u_int length, int print)
+telnet_parse(netdissect_options *ndo, const u_char *sp, u_int length, int print)
 {
        int i, x;
        u_int c;
 {
        int i, x;
        u_int c;
@@ -400,7 +397,7 @@ telnet_parse(const u_char *sp, u_int length, int print)
        do { \
                if (length < 1) \
                        goto pktend; \
        do { \
                if (length < 1) \
                        goto pktend; \
-               TCHECK(*sp); \
+               ND_TCHECK(*sp); \
                c = *sp++; \
                length--; \
        } while (0)
                c = *sp++; \
                length--; \
        } while (0)
@@ -413,7 +410,7 @@ telnet_parse(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)
-                       printf("IAC IAC");
+                       ND_PRINT((ndo, "IAC IAC"));
                goto done;
        }
 
                goto done;
        }
 
@@ -431,20 +428,22 @@ telnet_parse(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)
-                               (void)printf("%s %s", telcmds[i], telopts[x]);
+                               ND_PRINT((ndo, "%s %s", telcmds[i], telopts[x]));
                } else {
                        if (print)
                } else {
                        if (print)
-                               (void)printf("%s %#x", telcmds[i], x);
+                               ND_PRINT((ndo, "%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)) {
+                       ND_TCHECK_2(p);
                        if (p[0] == IAC && p[1] == SE)
                                break;
                        p++;
                }
                        if (p[0] == IAC && p[1] == SE)
                                break;
                        p++;
                }
+               ND_TCHECK(*p);
                if (*p != IAC)
                        goto pktend;
 
                if (*p != IAC)
                        goto pktend;
 
@@ -454,47 +453,46 @@ telnet_parse(const u_char *sp, u_int length, int print)
                                break;
                        FETCH(c, sp, length);
                        if (print)
                                break;
                        FETCH(c, sp, length);
                        if (print)
-                               (void)printf(" %s", STR_OR_ID(c, authcmd));
+                               ND_PRINT((ndo, " %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)
-                               (void)printf(" %s", STR_OR_ID(c, authtype));
+                               ND_PRINT((ndo, " %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)
-                               (void)printf(" %s", STR_OR_ID(c, enccmd));
+                               ND_PRINT((ndo, " %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)
-                               (void)printf(" %s", STR_OR_ID(c, enctype));
+                               ND_PRINT((ndo, " %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)
-                               (void)printf(" %s", STR_OR_ID(c, cmds));
+                               ND_PRINT((ndo, " %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)
-                               (void)printf(" %#x", x);
+                               ND_PRINT((ndo, " %#x", x));
                }
                /* terminating IAC SE */
                if (print)
                }
                /* terminating IAC SE */
                if (print)
-                       (void)printf(" SE");
+                       ND_PRINT((ndo, " SE"));
                sp += 2;
                sp += 2;
-               length -= 2;
                break;
        default:
                if (print)
                break;
        default:
                if (print)
-                       (void)printf("%s", telcmds[i]);
+                       ND_PRINT((ndo, "%s", telcmds[i]));
                goto done;
        }
 
                goto done;
        }
 
@@ -502,14 +500,14 @@ done:
        return sp - osp;
 
 trunc:
        return sp - osp;
 
 trunc:
-       (void)printf("[|telnet]");
+       ND_PRINT((ndo, "%s", tstr));
 pktend:
        return -1;
 #undef FETCH
 }
 
 void
 pktend:
        return -1;
 #undef FETCH
 }
 
 void
-telnet_print(const u_char *sp, u_int length)
+telnet_print(netdissect_options *ndo, const u_char *sp, u_int length)
 {
        int first = 1;
        const u_char *osp;
 {
        int first = 1;
        const u_char *osp;
@@ -517,35 +515,44 @@ telnet_print(const u_char *sp, u_int length)
 
        osp = sp;
 
 
        osp = sp;
 
-       while (length > 0 && *sp == IAC) {
-               l = telnet_parse(sp, length, 0);
+       ND_TCHECK(*sp);
+       while (length > 0 && EXTRACT_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;
 
                /*
                 * now print it
                 */
                if (l < 0)
                        break;
 
                /*
                 * now print it
                 */
-               if (Xflag && 2 < vflag) {
+               if (ndo->ndo_Xflag && 2 < ndo->ndo_vflag) {
                        if (first)
                        if (first)
-                               printf("\nTelnet:");
-                       hex_print_with_offset("\n", sp, l, sp - osp);
+                               ND_PRINT((ndo, "\nTelnet:"));
+                       hex_print_with_offset(ndo, "\n", sp, l, sp - osp);
                        if (l > 8)
                        if (l > 8)
-                               printf("\n\t\t\t\t");
+                               ND_PRINT((ndo, "\n\t\t\t\t"));
                        else
                        else
-                               printf("%*s\t", (8 - l) * 3, "");
+                               ND_PRINT((ndo, "%*s\t", (8 - l) * 3, ""));
                } else
                } else
-                       printf("%s", (first) ? " [telnet " : ", ");
+                       ND_PRINT((ndo, "%s", (first) ? " [telnet " : ", "));
 
 
-               (void)telnet_parse(sp, length, 1);
+               (void)telnet_parse(ndo, sp, length, 1);
                first = 0;
 
                sp += l;
                length -= l;
                first = 0;
 
                sp += l;
                length -= l;
+               ND_TCHECK(*sp);
        }
        if (!first) {
        }
        if (!first) {
-               if (Xflag && 2 < vflag)
-                       printf("\n");
+               if (ndo->ndo_Xflag && 2 < ndo->ndo_vflag)
+                       ND_PRINT((ndo, "\n"));
                else
                else
-                       printf("]");
+                       ND_PRINT((ndo, "]"));
        }
        }
+       return;
+trunc:
+       ND_PRINT((ndo, "%s", tstr));
 }
 }