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