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