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