blob: 8132c49af7a1d928d9b4d5f5319c5b790012bc6c [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/*
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
Elliott Hughes892a68b2015-10-19 14:43:53 -070023#define NETDISSECT_REWORKED
The Android Open Source Project2949f582009-03-03 19:30:46 -080024#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#include <tcpdump-stdinc.h>
29
30#include <stdio.h>
31#include <string.h>
32#include <stdlib.h>
33
34#include "interface.h"
35
36struct z_packet {
37 char *version;
38 int numfields;
39 int kind;
40 char *uid;
41 int port;
42 int auth;
43 int authlen;
44 char *authdata;
45 char *class;
46 char *inst;
47 char *opcode;
48 char *sender;
49 const char *recipient;
50 char *format;
51 int cksum;
52 int multi;
53 char *multi_uid;
54 /* Other fields follow here.. */
55};
56
57enum z_packet_type {
58 Z_PACKET_UNSAFE = 0,
59 Z_PACKET_UNACKED,
60 Z_PACKET_ACKED,
61 Z_PACKET_HMACK,
62 Z_PACKET_HMCTL,
63 Z_PACKET_SERVACK,
64 Z_PACKET_SERVNAK,
65 Z_PACKET_CLIENTACK,
66 Z_PACKET_STAT
67};
68
JP Abgrall53f17a92014-02-12 14:02:41 -080069static const struct tok z_types[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -080070 { Z_PACKET_UNSAFE, "unsafe" },
71 { Z_PACKET_UNACKED, "unacked" },
72 { Z_PACKET_ACKED, "acked" },
73 { Z_PACKET_HMACK, "hm-ack" },
74 { Z_PACKET_HMCTL, "hm-ctl" },
75 { Z_PACKET_SERVACK, "serv-ack" },
76 { Z_PACKET_SERVNAK, "serv-nak" },
77 { Z_PACKET_CLIENTACK, "client-ack" },
78 { Z_PACKET_STAT, "stat" }
79};
80
Elliott Hughes892a68b2015-10-19 14:43:53 -070081static char z_buf[256];
The Android Open Source Project2949f582009-03-03 19:30:46 -080082
83static char *
Elliott Hughes892a68b2015-10-19 14:43:53 -070084parse_field(netdissect_options *ndo, char **pptr, int *len)
The Android Open Source Project2949f582009-03-03 19:30:46 -080085{
86 char *s;
87
88 if (*len <= 0 || !pptr || !*pptr)
89 return NULL;
Elliott Hughes892a68b2015-10-19 14:43:53 -070090 if (*pptr > (char *) ndo->ndo_snapend)
The Android Open Source Project2949f582009-03-03 19:30:46 -080091 return NULL;
92
93 s = *pptr;
Elliott Hughes892a68b2015-10-19 14:43:53 -070094 while (*pptr <= (char *) ndo->ndo_snapend && *len >= 0 && **pptr) {
The Android Open Source Project2949f582009-03-03 19:30:46 -080095 (*pptr)++;
96 (*len)--;
97 }
98 (*pptr)++;
99 (*len)--;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700100 if (*len < 0 || *pptr > (char *) ndo->ndo_snapend)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800101 return NULL;
102 return s;
103}
104
105static const char *
106z_triple(char *class, char *inst, const char *recipient)
107{
108 if (!*recipient)
109 recipient = "*";
110 snprintf(z_buf, sizeof(z_buf), "<%s,%s,%s>", class, inst, recipient);
111 z_buf[sizeof(z_buf)-1] = '\0';
112 return z_buf;
113}
114
115static const char *
116str_to_lower(char *string)
117{
118 strncpy(z_buf, string, sizeof(z_buf));
119 z_buf[sizeof(z_buf)-1] = '\0';
120
121 string = z_buf;
122 while (*string) {
123 *string = tolower((unsigned char)(*string));
124 string++;
125 }
126
127 return z_buf;
128}
129
130void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700131zephyr_print(netdissect_options *ndo, const u_char *cp, int length)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800132{
133 struct z_packet z;
134 char *parse = (char *) cp;
135 int parselen = length;
136 char *s;
137 int lose = 0;
138
JP Abgrall53f17a92014-02-12 14:02:41 -0800139 /* squelch compiler warnings */
140
141 z.kind = 0;
142 z.class = 0;
143 z.inst = 0;
144 z.opcode = 0;
145 z.sender = 0;
146 z.recipient = 0;
147
The Android Open Source Project2949f582009-03-03 19:30:46 -0800148#define PARSE_STRING \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700149 s = parse_field(ndo, &parse, &parselen); \
The Android Open Source Project2949f582009-03-03 19:30:46 -0800150 if (!s) lose = 1;
151
152#define PARSE_FIELD_INT(field) \
153 PARSE_STRING \
154 if (!lose) field = strtol(s, 0, 16);
155
156#define PARSE_FIELD_STR(field) \
157 PARSE_STRING \
158 if (!lose) field = s;
159
160 PARSE_FIELD_STR(z.version);
161 if (lose) return;
162 if (strncmp(z.version, "ZEPH", 4))
163 return;
164
165 PARSE_FIELD_INT(z.numfields);
166 PARSE_FIELD_INT(z.kind);
167 PARSE_FIELD_STR(z.uid);
168 PARSE_FIELD_INT(z.port);
169 PARSE_FIELD_INT(z.auth);
170 PARSE_FIELD_INT(z.authlen);
171 PARSE_FIELD_STR(z.authdata);
172 PARSE_FIELD_STR(z.class);
173 PARSE_FIELD_STR(z.inst);
174 PARSE_FIELD_STR(z.opcode);
175 PARSE_FIELD_STR(z.sender);
176 PARSE_FIELD_STR(z.recipient);
177 PARSE_FIELD_STR(z.format);
178 PARSE_FIELD_INT(z.cksum);
179 PARSE_FIELD_INT(z.multi);
180 PARSE_FIELD_STR(z.multi_uid);
181
182 if (lose) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700183 ND_PRINT((ndo, " [|zephyr] (%d)", length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800184 return;
185 }
186
Elliott Hughes892a68b2015-10-19 14:43:53 -0700187 ND_PRINT((ndo, " zephyr"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800188 if (strncmp(z.version+4, "0.2", 3)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700189 ND_PRINT((ndo, " v%s", z.version+4));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800190 return;
191 }
192
Elliott Hughes892a68b2015-10-19 14:43:53 -0700193 ND_PRINT((ndo, " %s", tok2str(z_types, "type %d", z.kind)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800194 if (z.kind == Z_PACKET_SERVACK) {
195 /* Initialization to silence warnings */
196 char *ackdata = NULL;
197 PARSE_FIELD_STR(ackdata);
198 if (!lose && strcmp(ackdata, "SENT"))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700199 ND_PRINT((ndo, "/%s", str_to_lower(ackdata)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800200 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700201 if (*z.sender) ND_PRINT((ndo, " %s", z.sender));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800202
203 if (!strcmp(z.class, "USER_LOCATE")) {
204 if (!strcmp(z.opcode, "USER_HIDE"))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700205 ND_PRINT((ndo, " hide"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800206 else if (!strcmp(z.opcode, "USER_UNHIDE"))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700207 ND_PRINT((ndo, " unhide"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800208 else
Elliott Hughes892a68b2015-10-19 14:43:53 -0700209 ND_PRINT((ndo, " locate %s", z.inst));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800210 return;
211 }
212
213 if (!strcmp(z.class, "ZEPHYR_ADMIN")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700214 ND_PRINT((ndo, " zephyr-admin %s", str_to_lower(z.opcode)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800215 return;
216 }
217
218 if (!strcmp(z.class, "ZEPHYR_CTL")) {
219 if (!strcmp(z.inst, "CLIENT")) {
220 if (!strcmp(z.opcode, "SUBSCRIBE") ||
221 !strcmp(z.opcode, "SUBSCRIBE_NODEFS") ||
222 !strcmp(z.opcode, "UNSUBSCRIBE")) {
223
Elliott Hughes892a68b2015-10-19 14:43:53 -0700224 ND_PRINT((ndo, " %ssub%s", strcmp(z.opcode, "SUBSCRIBE") ? "un" : "",
The Android Open Source Project2949f582009-03-03 19:30:46 -0800225 strcmp(z.opcode, "SUBSCRIBE_NODEFS") ? "" :
Elliott Hughes892a68b2015-10-19 14:43:53 -0700226 "-nodefs"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800227 if (z.kind != Z_PACKET_SERVACK) {
228 /* Initialization to silence warnings */
229 char *c = NULL, *i = NULL, *r = NULL;
230 PARSE_FIELD_STR(c);
231 PARSE_FIELD_STR(i);
232 PARSE_FIELD_STR(r);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700233 if (!lose) ND_PRINT((ndo, " %s", z_triple(c, i, r)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800234 }
235 return;
236 }
237
238 if (!strcmp(z.opcode, "GIMME")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700239 ND_PRINT((ndo, " ret"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800240 return;
241 }
242
243 if (!strcmp(z.opcode, "GIMMEDEFS")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700244 ND_PRINT((ndo, " gimme-defs"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800245 return;
246 }
247
248 if (!strcmp(z.opcode, "CLEARSUB")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700249 ND_PRINT((ndo, " clear-subs"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800250 return;
251 }
252
Elliott Hughes892a68b2015-10-19 14:43:53 -0700253 ND_PRINT((ndo, " %s", str_to_lower(z.opcode)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800254 return;
255 }
256
257 if (!strcmp(z.inst, "HM")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700258 ND_PRINT((ndo, " %s", str_to_lower(z.opcode)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800259 return;
260 }
261
262 if (!strcmp(z.inst, "REALM")) {
263 if (!strcmp(z.opcode, "ADD_SUBSCRIBE"))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700264 ND_PRINT((ndo, " realm add-subs"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800265 if (!strcmp(z.opcode, "REQ_SUBSCRIBE"))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700266 ND_PRINT((ndo, " realm req-subs"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800267 if (!strcmp(z.opcode, "RLM_SUBSCRIBE"))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700268 ND_PRINT((ndo, " realm rlm-sub"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800269 if (!strcmp(z.opcode, "RLM_UNSUBSCRIBE"))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700270 ND_PRINT((ndo, " realm rlm-unsub"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800271 return;
272 }
273 }
274
275 if (!strcmp(z.class, "HM_CTL")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700276 ND_PRINT((ndo, " hm_ctl %s", str_to_lower(z.inst)));
277 ND_PRINT((ndo, " %s", str_to_lower(z.opcode)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800278 return;
279 }
280
281 if (!strcmp(z.class, "HM_STAT")) {
282 if (!strcmp(z.inst, "HMST_CLIENT") && !strcmp(z.opcode, "GIMMESTATS")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700283 ND_PRINT((ndo, " get-client-stats"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800284 return;
285 }
286 }
287
288 if (!strcmp(z.class, "WG_CTL")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700289 ND_PRINT((ndo, " wg_ctl %s", str_to_lower(z.inst)));
290 ND_PRINT((ndo, " %s", str_to_lower(z.opcode)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800291 return;
292 }
293
294 if (!strcmp(z.class, "LOGIN")) {
295 if (!strcmp(z.opcode, "USER_FLUSH")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700296 ND_PRINT((ndo, " flush_locs"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800297 return;
298 }
299
300 if (!strcmp(z.opcode, "NONE") ||
301 !strcmp(z.opcode, "OPSTAFF") ||
302 !strcmp(z.opcode, "REALM-VISIBLE") ||
303 !strcmp(z.opcode, "REALM-ANNOUNCED") ||
304 !strcmp(z.opcode, "NET-VISIBLE") ||
305 !strcmp(z.opcode, "NET-ANNOUNCED")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700306 ND_PRINT((ndo, " set-exposure %s", str_to_lower(z.opcode)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800307 return;
308 }
309 }
310
311 if (!*z.recipient)
312 z.recipient = "*";
313
Elliott Hughes892a68b2015-10-19 14:43:53 -0700314 ND_PRINT((ndo, " to %s", z_triple(z.class, z.inst, z.recipient)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800315 if (*z.opcode)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700316 ND_PRINT((ndo, " op %s", z.opcode));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800317}