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