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