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