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