Home | History | Annotate | Line # | Download | only in dist
print-zephyr.c revision 1.6
      1 /*
      2  * Decode and print Zephyr packets.
      3  *
      4  *	http://web.mit.edu/zephyr/doc/protocol
      5  *
      6  * Copyright (c) 2001 Nickolai Zeldovich <kolya (at) MIT.EDU>
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that: (1) source code
     11  * distributions retain the above copyright notice and this paragraph
     12  * in its entirety, and (2) distributions including binary code include
     13  * the above copyright notice and this paragraph in its entirety in
     14  * the documentation or other materials provided with the distribution.
     15  * The name of the author(s) may not be used to endorse or promote
     16  * products derived from this software without specific prior written
     17  * permission.  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY
     18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE.
     21  */
     22 
     23 #include <sys/cdefs.h>
     24 #ifndef lint
     25 __RCSID("$NetBSD: print-zephyr.c,v 1.6 2017/01/24 23:29:14 christos Exp $");
     26 #endif
     27 
     28 #ifdef HAVE_CONFIG_H
     29 #include "config.h"
     30 #endif
     31 
     32 #include <netdissect-stdinc.h>
     33 
     34 #include <stdio.h>
     35 #include <string.h>
     36 #include <stdlib.h>
     37 
     38 #include "netdissect.h"
     39 
     40 struct z_packet {
     41     const char *version;
     42     int numfields;
     43     int kind;
     44     const char *uid;
     45     int port;
     46     int auth;
     47     int authlen;
     48     const char *authdata;
     49     const char *class;
     50     const char *inst;
     51     const char *opcode;
     52     const char *sender;
     53     const char *recipient;
     54     const char *format;
     55     int cksum;
     56     int multi;
     57     const char *multi_uid;
     58     /* Other fields follow here.. */
     59 };
     60 
     61 enum z_packet_type {
     62     Z_PACKET_UNSAFE = 0,
     63     Z_PACKET_UNACKED,
     64     Z_PACKET_ACKED,
     65     Z_PACKET_HMACK,
     66     Z_PACKET_HMCTL,
     67     Z_PACKET_SERVACK,
     68     Z_PACKET_SERVNAK,
     69     Z_PACKET_CLIENTACK,
     70     Z_PACKET_STAT
     71 };
     72 
     73 static const struct tok z_types[] = {
     74     { Z_PACKET_UNSAFE,		"unsafe" },
     75     { Z_PACKET_UNACKED,		"unacked" },
     76     { Z_PACKET_ACKED,		"acked" },
     77     { Z_PACKET_HMACK,		"hm-ack" },
     78     { Z_PACKET_HMCTL,		"hm-ctl" },
     79     { Z_PACKET_SERVACK,		"serv-ack" },
     80     { Z_PACKET_SERVNAK,		"serv-nak" },
     81     { Z_PACKET_CLIENTACK,	"client-ack" },
     82     { Z_PACKET_STAT,		"stat" }
     83 };
     84 
     85 static char z_buf[256];
     86 
     87 static const char *
     88 parse_field(netdissect_options *ndo, const char **pptr, int *len)
     89 {
     90     const char *s;
     91 
     92     if (*len <= 0 || !pptr || !*pptr)
     93 	return NULL;
     94     if (*pptr > (const char *) ndo->ndo_snapend)
     95 	return NULL;
     96 
     97     s = *pptr;
     98     while (*pptr <= (const char *) ndo->ndo_snapend && *len >= 0 && **pptr) {
     99 	(*pptr)++;
    100 	(*len)--;
    101     }
    102     (*pptr)++;
    103     (*len)--;
    104     if (*len < 0 || *pptr > (const char *) ndo->ndo_snapend)
    105 	return NULL;
    106     return s;
    107 }
    108 
    109 static const char *
    110 z_triple(const char *class, const char *inst, const char *recipient)
    111 {
    112     if (!*recipient)
    113 	recipient = "*";
    114     snprintf(z_buf, sizeof(z_buf), "<%s,%s,%s>", class, inst, recipient);
    115     z_buf[sizeof(z_buf)-1] = '\0';
    116     return z_buf;
    117 }
    118 
    119 static const char *
    120 str_to_lower(const char *string)
    121 {
    122     char *zb_string;
    123 
    124     strncpy(z_buf, string, sizeof(z_buf));
    125     z_buf[sizeof(z_buf)-1] = '\0';
    126 
    127     zb_string = z_buf;
    128     while (*zb_string) {
    129 	*zb_string = tolower((unsigned char)(*zb_string));
    130 	zb_string++;
    131     }
    132 
    133     return z_buf;
    134 }
    135 
    136 void
    137 zephyr_print(netdissect_options *ndo, const u_char *cp, int length)
    138 {
    139     struct z_packet z;
    140     const char *parse = (const char *) cp;
    141     int parselen = length;
    142     const char *s;
    143     int lose = 0;
    144 
    145     /* squelch compiler warnings */
    146 
    147     z.kind = 0;
    148     z.class = 0;
    149     z.inst = 0;
    150     z.opcode = 0;
    151     z.sender = 0;
    152     z.recipient = 0;
    153 
    154     memset(&z, 0, sizeof(z));	/* XXX gcc */
    155 
    156 #define PARSE_STRING				\
    157 	s = parse_field(ndo, &parse, &parselen);	\
    158 	if (!s) lose = 1;
    159 
    160 #define PARSE_FIELD_INT(field)			\
    161 	PARSE_STRING				\
    162 	if (!lose) field = strtol(s, 0, 16);
    163 
    164 #define PARSE_FIELD_STR(field)			\
    165 	PARSE_STRING				\
    166 	if (!lose) field = s;
    167 
    168     PARSE_FIELD_STR(z.version);
    169     if (lose) return;
    170     if (strncmp(z.version, "ZEPH", 4))
    171 	return;
    172 
    173     PARSE_FIELD_INT(z.numfields);
    174     PARSE_FIELD_INT(z.kind);
    175     PARSE_FIELD_STR(z.uid);
    176     PARSE_FIELD_INT(z.port);
    177     PARSE_FIELD_INT(z.auth);
    178     PARSE_FIELD_INT(z.authlen);
    179     PARSE_FIELD_STR(z.authdata);
    180     PARSE_FIELD_STR(z.class);
    181     PARSE_FIELD_STR(z.inst);
    182     PARSE_FIELD_STR(z.opcode);
    183     PARSE_FIELD_STR(z.sender);
    184     PARSE_FIELD_STR(z.recipient);
    185     PARSE_FIELD_STR(z.format);
    186     PARSE_FIELD_INT(z.cksum);
    187     PARSE_FIELD_INT(z.multi);
    188     PARSE_FIELD_STR(z.multi_uid);
    189 
    190     if (lose) {
    191 	ND_PRINT((ndo, " [|zephyr] (%d)", length));
    192 	return;
    193     }
    194 
    195     ND_PRINT((ndo, " zephyr"));
    196     if (strncmp(z.version+4, "0.2", 3)) {
    197 	ND_PRINT((ndo, " v%s", z.version+4));
    198 	return;
    199     }
    200 
    201     ND_PRINT((ndo, " %s", tok2str(z_types, "type %d", z.kind)));
    202     if (z.kind == Z_PACKET_SERVACK) {
    203 	/* Initialization to silence warnings */
    204 	const char *ackdata = NULL;
    205 	PARSE_FIELD_STR(ackdata);
    206 	if (!lose && strcmp(ackdata, "SENT"))
    207 	    ND_PRINT((ndo, "/%s", str_to_lower(ackdata)));
    208     }
    209     if (*z.sender) ND_PRINT((ndo, " %s", z.sender));
    210 
    211     if (!strcmp(z.class, "USER_LOCATE")) {
    212 	if (!strcmp(z.opcode, "USER_HIDE"))
    213 	    ND_PRINT((ndo, " hide"));
    214 	else if (!strcmp(z.opcode, "USER_UNHIDE"))
    215 	    ND_PRINT((ndo, " unhide"));
    216 	else
    217 	    ND_PRINT((ndo, " locate %s", z.inst));
    218 	return;
    219     }
    220 
    221     if (!strcmp(z.class, "ZEPHYR_ADMIN")) {
    222 	ND_PRINT((ndo, " zephyr-admin %s", str_to_lower(z.opcode)));
    223 	return;
    224     }
    225 
    226     if (!strcmp(z.class, "ZEPHYR_CTL")) {
    227 	if (!strcmp(z.inst, "CLIENT")) {
    228 	    if (!strcmp(z.opcode, "SUBSCRIBE") ||
    229 		!strcmp(z.opcode, "SUBSCRIBE_NODEFS") ||
    230 		!strcmp(z.opcode, "UNSUBSCRIBE")) {
    231 
    232 		ND_PRINT((ndo, " %ssub%s", strcmp(z.opcode, "SUBSCRIBE") ? "un" : "",
    233 				   strcmp(z.opcode, "SUBSCRIBE_NODEFS") ? "" :
    234 								   "-nodefs"));
    235 		if (z.kind != Z_PACKET_SERVACK) {
    236 		    /* Initialization to silence warnings */
    237 		    const char *c = NULL, *i = NULL, *r = NULL;
    238 		    PARSE_FIELD_STR(c);
    239 		    PARSE_FIELD_STR(i);
    240 		    PARSE_FIELD_STR(r);
    241 		    if (!lose) ND_PRINT((ndo, " %s", z_triple(c, i, r)));
    242 		}
    243 		return;
    244 	    }
    245 
    246 	    if (!strcmp(z.opcode, "GIMME")) {
    247 		ND_PRINT((ndo, " ret"));
    248 		return;
    249 	    }
    250 
    251 	    if (!strcmp(z.opcode, "GIMMEDEFS")) {
    252 		ND_PRINT((ndo, " gimme-defs"));
    253 		return;
    254 	    }
    255 
    256 	    if (!strcmp(z.opcode, "CLEARSUB")) {
    257 		ND_PRINT((ndo, " clear-subs"));
    258 		return;
    259 	    }
    260 
    261 	    ND_PRINT((ndo, " %s", str_to_lower(z.opcode)));
    262 	    return;
    263 	}
    264 
    265 	if (!strcmp(z.inst, "HM")) {
    266 	    ND_PRINT((ndo, " %s", str_to_lower(z.opcode)));
    267 	    return;
    268 	}
    269 
    270 	if (!strcmp(z.inst, "REALM")) {
    271 	    if (!strcmp(z.opcode, "ADD_SUBSCRIBE"))
    272 		ND_PRINT((ndo, " realm add-subs"));
    273 	    if (!strcmp(z.opcode, "REQ_SUBSCRIBE"))
    274 		ND_PRINT((ndo, " realm req-subs"));
    275 	    if (!strcmp(z.opcode, "RLM_SUBSCRIBE"))
    276 		ND_PRINT((ndo, " realm rlm-sub"));
    277 	    if (!strcmp(z.opcode, "RLM_UNSUBSCRIBE"))
    278 		ND_PRINT((ndo, " realm rlm-unsub"));
    279 	    return;
    280 	}
    281     }
    282 
    283     if (!strcmp(z.class, "HM_CTL")) {
    284 	ND_PRINT((ndo, " hm_ctl %s", str_to_lower(z.inst)));
    285 	ND_PRINT((ndo, " %s", str_to_lower(z.opcode)));
    286 	return;
    287     }
    288 
    289     if (!strcmp(z.class, "HM_STAT")) {
    290 	if (!strcmp(z.inst, "HMST_CLIENT") && !strcmp(z.opcode, "GIMMESTATS")) {
    291 	    ND_PRINT((ndo, " get-client-stats"));
    292 	    return;
    293 	}
    294     }
    295 
    296     if (!strcmp(z.class, "WG_CTL")) {
    297 	ND_PRINT((ndo, " wg_ctl %s", str_to_lower(z.inst)));
    298 	ND_PRINT((ndo, " %s", str_to_lower(z.opcode)));
    299 	return;
    300     }
    301 
    302     if (!strcmp(z.class, "LOGIN")) {
    303 	if (!strcmp(z.opcode, "USER_FLUSH")) {
    304 	    ND_PRINT((ndo, " flush_locs"));
    305 	    return;
    306 	}
    307 
    308 	if (!strcmp(z.opcode, "NONE") ||
    309 	    !strcmp(z.opcode, "OPSTAFF") ||
    310 	    !strcmp(z.opcode, "REALM-VISIBLE") ||
    311 	    !strcmp(z.opcode, "REALM-ANNOUNCED") ||
    312 	    !strcmp(z.opcode, "NET-VISIBLE") ||
    313 	    !strcmp(z.opcode, "NET-ANNOUNCED")) {
    314 	    ND_PRINT((ndo, " set-exposure %s", str_to_lower(z.opcode)));
    315 	    return;
    316 	}
    317     }
    318 
    319     if (!*z.recipient)
    320 	z.recipient = "*";
    321 
    322     ND_PRINT((ndo, " to %s", z_triple(z.class, z.inst, z.recipient)));
    323     if (*z.opcode)
    324 	ND_PRINT((ndo, " op %s", z.opcode));
    325 }
    326