X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/d0e12e23776cf87792d3f530731cc42c44a95514..refs/heads/master:/print-zephyr.c diff --git a/print-zephyr.c b/print-zephyr.c index 9b39afad..796f4e93 100644 --- a/print-zephyr.c +++ b/print-zephyr.c @@ -1,7 +1,7 @@ /* * Decode and print Zephyr packets. * - * http://web.mit.edu/zephyr/doc/protocol + * https://web.mit.edu/zephyr/doc/protocol * * Copyright (c) 2001 Nickolai Zeldovich * All rights reserved. @@ -20,41 +20,39 @@ * PURPOSE. */ -#ifndef lint -static const char rcsid[] _U_ = - "@(#) $Header: /tcpdump/master/tcpdump/print-zephyr.c,v 1.9 2005-04-21 06:51:11 guy Exp $"; -#endif +/* \summary: Zephyr printer */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif +#include -#include +#include "netdissect-stdinc.h" #include #include #include -#include "interface.h" +#include "netdissect-ctype.h" + +#include "netdissect.h" +#include "extract.h" struct z_packet { - char *version; + const char *version; int numfields; int kind; - char *uid; + const char *uid; int port; int auth; int authlen; - char *authdata; - char *class; - char *inst; - char *opcode; - char *sender; + const char *authdata; + const char *class; + const char *inst; + const char *opcode; + const char *sender; const char *recipient; - char *format; + const char *format; int cksum; int multi; - char *multi_uid; + const char *multi_uid; /* Other fields follow here.. */ }; @@ -70,7 +68,7 @@ enum z_packet_type { Z_PACKET_STAT }; -static struct tok z_types[] = { +static const struct tok z_types[] = { { Z_PACKET_UNSAFE, "unsafe" }, { Z_PACKET_UNACKED, "unacked" }, { Z_PACKET_ACKED, "acked" }, @@ -79,35 +77,41 @@ static struct tok z_types[] = { { Z_PACKET_SERVACK, "serv-ack" }, { Z_PACKET_SERVNAK, "serv-nak" }, { Z_PACKET_CLIENTACK, "client-ack" }, - { Z_PACKET_STAT, "stat" } + { Z_PACKET_STAT, "stat" }, + { 0, NULL } }; -char z_buf[256]; +static char z_buf[256]; -static char * -parse_field(char **pptr, int *len) +static const char * +parse_field(netdissect_options *ndo, const char **pptr, int *len) { - char *s; - - if (*len <= 0 || !pptr || !*pptr) - return NULL; - if (*pptr > (char *) snapend) - return NULL; + const char *s; + /* Start of string */ s = *pptr; - while (*pptr <= (char *) snapend && *len >= 0 && **pptr) { + /* Scan for the NUL terminator */ + for (;;) { + if (*len == 0) { + /* Ran out of packet data without finding it */ + return NULL; + } + if (GET_U_1(*pptr) == '\0') { + /* Found it */ + break; + } + /* Keep scanning */ (*pptr)++; (*len)--; } + /* Skip the NUL terminator */ (*pptr)++; (*len)--; - if (*len < 0 || *pptr > (char *) snapend) - return NULL; return s; } static const char * -z_triple(char *class, char *inst, const char *recipient) +z_triple(const char *class, const char *inst, const char *recipient) { if (!*recipient) recipient = "*"; @@ -117,31 +121,57 @@ z_triple(char *class, char *inst, const char *recipient) } static const char * -str_to_lower(char *string) +str_to_lower(const char *string) { + char *zb_string; + strncpy(z_buf, string, sizeof(z_buf)); z_buf[sizeof(z_buf)-1] = '\0'; - string = z_buf; - while (*string) { - *string = tolower((unsigned char)(*string)); - string++; + zb_string = z_buf; + while (*zb_string) { + *zb_string = ND_ASCII_TOLOWER(*zb_string); + zb_string++; } return z_buf; } +#define ZEPHYR_PRINT(str1,str2) \ +{ ND_PRINT("%s", (str1)); fn_print_str(ndo, (const u_char *)(str2)); } + void -zephyr_print(const u_char *cp, int length) +zephyr_print(netdissect_options *ndo, const u_char *cp, u_int length) { - struct z_packet z; - char *parse = (char *) cp; + struct z_packet z = { + NULL, /* version */ + 0, /* numfields */ + 0, /* kind */ + NULL, /* uid */ + 0, /* port */ + 0, /* auth */ + 0, /* authlen */ + NULL, /* authdata */ + NULL, /* class */ + NULL, /* inst */ + NULL, /* opcode */ + NULL, /* sender */ + NULL, /* recipient */ + NULL, /* format */ + 0, /* cksum */ + 0, /* multi */ + NULL /* multi_uid */ + }; + const char *parse = (const char *) cp; int parselen = length; - char *s; + const char *s; int lose = 0; -#define PARSE_STRING \ - s = parse_field(&parse, &parselen); \ + ndo->ndo_protocol = "zephyr"; + /* squelch compiler warnings */ + +#define PARSE_STRING \ + s = parse_field(ndo, &parse, &parselen); \ if (!s) lose = 1; #define PARSE_FIELD_INT(field) \ @@ -153,7 +183,9 @@ zephyr_print(const u_char *cp, int length) if (!lose) field = s; PARSE_FIELD_STR(z.version); - if (lose) return; + if (lose) + goto invalid; + if (strncmp(z.version, "ZEPH", 4)) return; @@ -174,39 +206,37 @@ zephyr_print(const u_char *cp, int length) PARSE_FIELD_INT(z.multi); PARSE_FIELD_STR(z.multi_uid); - if (lose) { - printf(" [|zephyr] (%d)", length); - return; - } + if (lose) + goto invalid; - printf(" zephyr"); + ND_PRINT(" zephyr"); if (strncmp(z.version+4, "0.2", 3)) { - printf(" v%s", z.version+4); + ZEPHYR_PRINT(" v", z.version+4) return; } - printf(" %s", tok2str(z_types, "type %d", z.kind)); + ND_PRINT(" %s", tok2str(z_types, "type %d", z.kind)); if (z.kind == Z_PACKET_SERVACK) { /* Initialization to silence warnings */ - char *ackdata = NULL; + const char *ackdata = NULL; PARSE_FIELD_STR(ackdata); if (!lose && strcmp(ackdata, "SENT")) - printf("/%s", str_to_lower(ackdata)); + ZEPHYR_PRINT("/", str_to_lower(ackdata)) } - if (*z.sender) printf(" %s", z.sender); + if (*z.sender) ZEPHYR_PRINT(" ", z.sender); if (!strcmp(z.class, "USER_LOCATE")) { if (!strcmp(z.opcode, "USER_HIDE")) - printf(" hide"); + ND_PRINT(" hide"); else if (!strcmp(z.opcode, "USER_UNHIDE")) - printf(" unhide"); + ND_PRINT(" unhide"); else - printf(" locate %s", z.inst); + ZEPHYR_PRINT(" locate ", z.inst); return; } if (!strcmp(z.class, "ZEPHYR_ADMIN")) { - printf(" zephyr-admin %s", str_to_lower(z.opcode)); + ZEPHYR_PRINT(" zephyr-admin ", str_to_lower(z.opcode)); return; } @@ -216,79 +246,79 @@ zephyr_print(const u_char *cp, int length) !strcmp(z.opcode, "SUBSCRIBE_NODEFS") || !strcmp(z.opcode, "UNSUBSCRIBE")) { - printf(" %ssub%s", strcmp(z.opcode, "SUBSCRIBE") ? "un" : "", + ND_PRINT(" %ssub%s", strcmp(z.opcode, "SUBSCRIBE") ? "un" : "", strcmp(z.opcode, "SUBSCRIBE_NODEFS") ? "" : "-nodefs"); if (z.kind != Z_PACKET_SERVACK) { /* Initialization to silence warnings */ - char *c = NULL, *i = NULL, *r = NULL; + const char *c = NULL, *i = NULL, *r = NULL; PARSE_FIELD_STR(c); PARSE_FIELD_STR(i); PARSE_FIELD_STR(r); - if (!lose) printf(" %s", z_triple(c, i, r)); + if (!lose) ZEPHYR_PRINT(" ", z_triple(c, i, r)); } return; } if (!strcmp(z.opcode, "GIMME")) { - printf(" ret"); + ND_PRINT(" ret"); return; } if (!strcmp(z.opcode, "GIMMEDEFS")) { - printf(" gimme-defs"); + ND_PRINT(" gimme-defs"); return; } if (!strcmp(z.opcode, "CLEARSUB")) { - printf(" clear-subs"); + ND_PRINT(" clear-subs"); return; } - printf(" %s", str_to_lower(z.opcode)); + ZEPHYR_PRINT(" ", str_to_lower(z.opcode)); return; } if (!strcmp(z.inst, "HM")) { - printf(" %s", str_to_lower(z.opcode)); + ZEPHYR_PRINT(" ", str_to_lower(z.opcode)); return; } if (!strcmp(z.inst, "REALM")) { if (!strcmp(z.opcode, "ADD_SUBSCRIBE")) - printf(" realm add-subs"); + ND_PRINT(" realm add-subs"); if (!strcmp(z.opcode, "REQ_SUBSCRIBE")) - printf(" realm req-subs"); + ND_PRINT(" realm req-subs"); if (!strcmp(z.opcode, "RLM_SUBSCRIBE")) - printf(" realm rlm-sub"); + ND_PRINT(" realm rlm-sub"); if (!strcmp(z.opcode, "RLM_UNSUBSCRIBE")) - printf(" realm rlm-unsub"); + ND_PRINT(" realm rlm-unsub"); return; } } if (!strcmp(z.class, "HM_CTL")) { - printf(" hm_ctl %s", str_to_lower(z.inst)); - printf(" %s", str_to_lower(z.opcode)); + ZEPHYR_PRINT(" hm_ctl ", str_to_lower(z.inst)); + ZEPHYR_PRINT(" ", str_to_lower(z.opcode)); return; } if (!strcmp(z.class, "HM_STAT")) { if (!strcmp(z.inst, "HMST_CLIENT") && !strcmp(z.opcode, "GIMMESTATS")) { - printf(" get-client-stats"); + ND_PRINT(" get-client-stats"); return; } } if (!strcmp(z.class, "WG_CTL")) { - printf(" wg_ctl %s", str_to_lower(z.inst)); - printf(" %s", str_to_lower(z.opcode)); + ZEPHYR_PRINT(" wg_ctl ", str_to_lower(z.inst)); + ZEPHYR_PRINT(" ", str_to_lower(z.opcode)); return; } if (!strcmp(z.class, "LOGIN")) { if (!strcmp(z.opcode, "USER_FLUSH")) { - printf(" flush_locs"); + ND_PRINT(" flush_locs"); return; } @@ -298,7 +328,7 @@ zephyr_print(const u_char *cp, int length) !strcmp(z.opcode, "REALM-ANNOUNCED") || !strcmp(z.opcode, "NET-VISIBLE") || !strcmp(z.opcode, "NET-ANNOUNCED")) { - printf(" set-exposure %s", str_to_lower(z.opcode)); + ZEPHYR_PRINT(" set-exposure ", str_to_lower(z.opcode)); return; } } @@ -306,8 +336,11 @@ zephyr_print(const u_char *cp, int length) if (!*z.recipient) z.recipient = "*"; - printf(" to %s", z_triple(z.class, z.inst, z.recipient)); + ZEPHYR_PRINT(" to ", z_triple(z.class, z.inst, z.recipient)); if (*z.opcode) - printf(" op %s", z.opcode); + ZEPHYR_PRINT(" op ", z.opcode); return; + +invalid: + nd_print_invalid(ndo); }