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