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