X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/6ffa10954fe2613d421fb199da2e50d07c1979e5..refs/heads/mcr-macro-update-1:/print-telnet.c?ds=inline diff --git a/print-telnet.c b/print-telnet.c index e8d0a3ee..817d4226 100644 --- a/print-telnet.c +++ b/print-telnet.c @@ -45,17 +45,19 @@ * are preserved in all copies. */ +/* \summary: Telnet option printer */ + #ifdef HAVE_CONFIG_H -#include "config.h" +#include #endif -#include +#include "netdissect-stdinc.h" #include -#include -#include -#include "interface.h" +#include "netdissect.h" +#include "extract.h" + #define TELCMDS #define TELOPTS @@ -89,7 +91,7 @@ #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, @@ -150,7 +152,7 @@ extern char *telcmds[]; #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", @@ -385,7 +387,7 @@ numstr(int x) /* 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; @@ -394,8 +396,8 @@ telnet_parse(const u_char *sp, u_int length, int print) do { \ if (length < 1) \ goto pktend; \ - TCHECK(*sp); \ - c = *sp++; \ + c = GET_U_1(sp); \ + sp++; \ length--; \ } while (0) @@ -407,7 +409,7 @@ telnet_parse(const u_char *sp, u_int length, int print) FETCH(c, sp, length); if (c == IAC) { /* ! */ if (print) - printf("IAC IAC"); + ND_PRINT("IAC IAC"); goto done; } @@ -425,21 +427,21 @@ telnet_parse(const u_char *sp, u_int length, int print) FETCH(x, sp, length); if (x >= 0 && x < NTELOPTS) { if (print) - (void)printf("%s %s", telcmds[i], telopts[x]); + ND_PRINT("%s %s", telcmds[i], telopts[x]); } else { if (print) - (void)printf("%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 (p[0] == IAC && p[1] == SE) + if (GET_U_1(p) == IAC && GET_U_1(p + 1) == SE) break; p++; } - if (*p != IAC) + if (GET_U_1(p) != IAC) goto pktend; switch (x) { @@ -448,97 +450,104 @@ telnet_parse(const u_char *sp, u_int length, int print) break; FETCH(c, sp, length); if (print) - (void)printf(" %s", STR_OR_ID(c, authcmd)); + ND_PRINT(" %s", STR_OR_ID(c, authcmd)); if (p <= sp) break; FETCH(c, sp, length); if (print) - (void)printf(" %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) - (void)printf(" %s", STR_OR_ID(c, enccmd)); + ND_PRINT(" %s", STR_OR_ID(c, enccmd)); if (p <= sp) break; FETCH(c, sp, length); if (print) - (void)printf(" %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) - (void)printf(" %s", STR_OR_ID(c, cmds)); + ND_PRINT(" %s", STR_OR_ID(c, cmds)); break; } while (p > sp) { FETCH(x, sp, length); if (print) - (void)printf(" %#x", x); + ND_PRINT(" %#x", x); } /* terminating IAC SE */ if (print) - (void)printf(" SE"); + ND_PRINT(" SE"); sp += 2; break; default: if (print) - (void)printf("%s", telcmds[i]); + ND_PRINT("%s", telcmds[i]); goto done; } done: - return sp - osp; + return (int)(sp - osp); -trunc: - (void)printf("[|telnet]"); 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 l; + ndo->ndo_protocol = "telnet"; osp = sp; - while (length > 0 && *sp == IAC) { - l = telnet_parse(sp, length, 0); + 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; /* * now print it */ - if (Xflag && 2 < vflag) { + if (ndo->ndo_Xflag && 2 < ndo->ndo_vflag) { if (first) - printf("\nTelnet:"); - hex_print_with_offset(gndo,"\n", sp, l, sp - osp); + ND_PRINT("\nTelnet:"); + hex_print_with_offset(ndo, "\n", sp, l, (u_int)(sp - osp)); if (l > 8) - printf("\n\t\t\t\t"); + ND_PRINT("\n\t\t\t\t"); else - printf("%*s\t", (8 - l) * 3, ""); + ND_PRINT("%*s\t", (8 - l) * 3, ""); } else - printf("%s", (first) ? " [telnet " : ", "); + ND_PRINT("%s", (first) ? " [telnet " : ", "); - (void)telnet_parse(sp, length, 1); + (void)telnet_parse(ndo, sp, length, 1); first = 0; sp += l; length -= l; + ND_TCHECK_1(sp); } if (!first) { - if (Xflag && 2 < vflag) - printf("\n"); + if (ndo->ndo_Xflag && 2 < ndo->ndo_vflag) + ND_PRINT("\n"); else - printf("]"); + ND_PRINT("]"); } + return; +trunc: + nd_print_trunc(ndo); }