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