blob: 38b0cd0c7bf5c987bf6a772a71898a9fafae300e [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 Hughese2e3bd12017-05-15 10:59:29 -070023/* \summary: Zephyr printer */
24
The Android Open Source Project2949f582009-03-03 19:30:46 -080025#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
28
Elliott Hughese2e3bd12017-05-15 10:59:29 -070029#include <netdissect-stdinc.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080030
31#include <stdio.h>
32#include <string.h>
33#include <stdlib.h>
34
Elliott Hughese2e3bd12017-05-15 10:59:29 -070035#include "netdissect.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080036
37struct z_packet {
Elliott Hughese2e3bd12017-05-15 10:59:29 -070038 const char *version;
The Android Open Source Project2949f582009-03-03 19:30:46 -080039 int numfields;
40 int kind;
Elliott Hughese2e3bd12017-05-15 10:59:29 -070041 const char *uid;
The Android Open Source Project2949f582009-03-03 19:30:46 -080042 int port;
43 int auth;
44 int authlen;
Elliott Hughese2e3bd12017-05-15 10:59:29 -070045 const char *authdata;
46 const char *class;
47 const char *inst;
48 const char *opcode;
49 const char *sender;
The Android Open Source Project2949f582009-03-03 19:30:46 -080050 const char *recipient;
Elliott Hughese2e3bd12017-05-15 10:59:29 -070051 const char *format;
The Android Open Source Project2949f582009-03-03 19:30:46 -080052 int cksum;
53 int multi;
Elliott Hughese2e3bd12017-05-15 10:59:29 -070054 const char *multi_uid;
The Android Open Source Project2949f582009-03-03 19:30:46 -080055 /* Other fields follow here.. */
56};
57
58enum 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
JP Abgrall53f17a92014-02-12 14:02:41 -080070static const struct tok z_types[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -080071 { 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};
81
Elliott Hughes892a68b2015-10-19 14:43:53 -070082static char z_buf[256];
The Android Open Source Project2949f582009-03-03 19:30:46 -080083
Elliott Hughese2e3bd12017-05-15 10:59:29 -070084static const char *
85parse_field(netdissect_options *ndo, const char **pptr, int *len)
The Android Open Source Project2949f582009-03-03 19:30:46 -080086{
Elliott Hughese2e3bd12017-05-15 10:59:29 -070087 const char *s;
The Android Open Source Project2949f582009-03-03 19:30:46 -080088
89 if (*len <= 0 || !pptr || !*pptr)
90 return NULL;
Elliott Hughese2e3bd12017-05-15 10:59:29 -070091 if (*pptr > (const char *) ndo->ndo_snapend)
The Android Open Source Project2949f582009-03-03 19:30:46 -080092 return NULL;
93
94 s = *pptr;
Elliott Hughese2e3bd12017-05-15 10:59:29 -070095 while (*pptr <= (const char *) ndo->ndo_snapend && *len >= 0 && **pptr) {
The Android Open Source Project2949f582009-03-03 19:30:46 -080096 (*pptr)++;
97 (*len)--;
98 }
99 (*pptr)++;
100 (*len)--;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700101 if (*len < 0 || *pptr > (const char *) ndo->ndo_snapend)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800102 return NULL;
103 return s;
104}
105
106static const char *
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700107z_triple(const char *class, const char *inst, const char *recipient)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800108{
109 if (!*recipient)
110 recipient = "*";
111 snprintf(z_buf, sizeof(z_buf), "<%s,%s,%s>", class, inst, recipient);
112 z_buf[sizeof(z_buf)-1] = '\0';
113 return z_buf;
114}
115
116static const char *
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700117str_to_lower(const char *string)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800118{
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700119 char *zb_string;
120
The Android Open Source Project2949f582009-03-03 19:30:46 -0800121 strncpy(z_buf, string, sizeof(z_buf));
122 z_buf[sizeof(z_buf)-1] = '\0';
123
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700124 zb_string = z_buf;
125 while (*zb_string) {
126 *zb_string = tolower((unsigned char)(*zb_string));
127 zb_string++;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800128 }
129
130 return z_buf;
131}
132
133void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700134zephyr_print(netdissect_options *ndo, const u_char *cp, int length)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800135{
136 struct z_packet z;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700137 const char *parse = (const char *) cp;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800138 int parselen = length;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700139 const char *s;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800140 int lose = 0;
141
JP Abgrall53f17a92014-02-12 14:02:41 -0800142 /* squelch compiler warnings */
143
144 z.kind = 0;
145 z.class = 0;
146 z.inst = 0;
147 z.opcode = 0;
148 z.sender = 0;
149 z.recipient = 0;
150
The Android Open Source Project2949f582009-03-03 19:30:46 -0800151#define PARSE_STRING \
Elliott Hughes892a68b2015-10-19 14:43:53 -0700152 s = parse_field(ndo, &parse, &parselen); \
The Android Open Source Project2949f582009-03-03 19:30:46 -0800153 if (!s) lose = 1;
154
155#define PARSE_FIELD_INT(field) \
156 PARSE_STRING \
157 if (!lose) field = strtol(s, 0, 16);
158
159#define PARSE_FIELD_STR(field) \
160 PARSE_STRING \
161 if (!lose) field = s;
162
163 PARSE_FIELD_STR(z.version);
164 if (lose) return;
165 if (strncmp(z.version, "ZEPH", 4))
166 return;
167
168 PARSE_FIELD_INT(z.numfields);
169 PARSE_FIELD_INT(z.kind);
170 PARSE_FIELD_STR(z.uid);
171 PARSE_FIELD_INT(z.port);
172 PARSE_FIELD_INT(z.auth);
173 PARSE_FIELD_INT(z.authlen);
174 PARSE_FIELD_STR(z.authdata);
175 PARSE_FIELD_STR(z.class);
176 PARSE_FIELD_STR(z.inst);
177 PARSE_FIELD_STR(z.opcode);
178 PARSE_FIELD_STR(z.sender);
179 PARSE_FIELD_STR(z.recipient);
180 PARSE_FIELD_STR(z.format);
181 PARSE_FIELD_INT(z.cksum);
182 PARSE_FIELD_INT(z.multi);
183 PARSE_FIELD_STR(z.multi_uid);
184
185 if (lose) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700186 ND_PRINT((ndo, " [|zephyr] (%d)", length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800187 return;
188 }
189
Elliott Hughes892a68b2015-10-19 14:43:53 -0700190 ND_PRINT((ndo, " zephyr"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800191 if (strncmp(z.version+4, "0.2", 3)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700192 ND_PRINT((ndo, " v%s", z.version+4));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800193 return;
194 }
195
Elliott Hughes892a68b2015-10-19 14:43:53 -0700196 ND_PRINT((ndo, " %s", tok2str(z_types, "type %d", z.kind)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800197 if (z.kind == Z_PACKET_SERVACK) {
198 /* Initialization to silence warnings */
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700199 const char *ackdata = NULL;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800200 PARSE_FIELD_STR(ackdata);
201 if (!lose && strcmp(ackdata, "SENT"))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700202 ND_PRINT((ndo, "/%s", str_to_lower(ackdata)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800203 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700204 if (*z.sender) ND_PRINT((ndo, " %s", z.sender));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800205
206 if (!strcmp(z.class, "USER_LOCATE")) {
207 if (!strcmp(z.opcode, "USER_HIDE"))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700208 ND_PRINT((ndo, " hide"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800209 else if (!strcmp(z.opcode, "USER_UNHIDE"))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700210 ND_PRINT((ndo, " unhide"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800211 else
Elliott Hughes892a68b2015-10-19 14:43:53 -0700212 ND_PRINT((ndo, " locate %s", z.inst));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800213 return;
214 }
215
216 if (!strcmp(z.class, "ZEPHYR_ADMIN")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700217 ND_PRINT((ndo, " zephyr-admin %s", str_to_lower(z.opcode)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800218 return;
219 }
220
221 if (!strcmp(z.class, "ZEPHYR_CTL")) {
222 if (!strcmp(z.inst, "CLIENT")) {
223 if (!strcmp(z.opcode, "SUBSCRIBE") ||
224 !strcmp(z.opcode, "SUBSCRIBE_NODEFS") ||
225 !strcmp(z.opcode, "UNSUBSCRIBE")) {
226
Elliott Hughes892a68b2015-10-19 14:43:53 -0700227 ND_PRINT((ndo, " %ssub%s", strcmp(z.opcode, "SUBSCRIBE") ? "un" : "",
The Android Open Source Project2949f582009-03-03 19:30:46 -0800228 strcmp(z.opcode, "SUBSCRIBE_NODEFS") ? "" :
Elliott Hughes892a68b2015-10-19 14:43:53 -0700229 "-nodefs"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800230 if (z.kind != Z_PACKET_SERVACK) {
231 /* Initialization to silence warnings */
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700232 const char *c = NULL, *i = NULL, *r = NULL;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800233 PARSE_FIELD_STR(c);
234 PARSE_FIELD_STR(i);
235 PARSE_FIELD_STR(r);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700236 if (!lose) ND_PRINT((ndo, " %s", z_triple(c, i, r)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800237 }
238 return;
239 }
240
241 if (!strcmp(z.opcode, "GIMME")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700242 ND_PRINT((ndo, " ret"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800243 return;
244 }
245
246 if (!strcmp(z.opcode, "GIMMEDEFS")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700247 ND_PRINT((ndo, " gimme-defs"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800248 return;
249 }
250
251 if (!strcmp(z.opcode, "CLEARSUB")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700252 ND_PRINT((ndo, " clear-subs"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800253 return;
254 }
255
Elliott Hughes892a68b2015-10-19 14:43:53 -0700256 ND_PRINT((ndo, " %s", str_to_lower(z.opcode)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800257 return;
258 }
259
260 if (!strcmp(z.inst, "HM")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700261 ND_PRINT((ndo, " %s", str_to_lower(z.opcode)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800262 return;
263 }
264
265 if (!strcmp(z.inst, "REALM")) {
266 if (!strcmp(z.opcode, "ADD_SUBSCRIBE"))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700267 ND_PRINT((ndo, " realm add-subs"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800268 if (!strcmp(z.opcode, "REQ_SUBSCRIBE"))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700269 ND_PRINT((ndo, " realm req-subs"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800270 if (!strcmp(z.opcode, "RLM_SUBSCRIBE"))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700271 ND_PRINT((ndo, " realm rlm-sub"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800272 if (!strcmp(z.opcode, "RLM_UNSUBSCRIBE"))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700273 ND_PRINT((ndo, " realm rlm-unsub"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800274 return;
275 }
276 }
277
278 if (!strcmp(z.class, "HM_CTL")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700279 ND_PRINT((ndo, " hm_ctl %s", str_to_lower(z.inst)));
280 ND_PRINT((ndo, " %s", str_to_lower(z.opcode)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800281 return;
282 }
283
284 if (!strcmp(z.class, "HM_STAT")) {
285 if (!strcmp(z.inst, "HMST_CLIENT") && !strcmp(z.opcode, "GIMMESTATS")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700286 ND_PRINT((ndo, " get-client-stats"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800287 return;
288 }
289 }
290
291 if (!strcmp(z.class, "WG_CTL")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700292 ND_PRINT((ndo, " wg_ctl %s", str_to_lower(z.inst)));
293 ND_PRINT((ndo, " %s", str_to_lower(z.opcode)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800294 return;
295 }
296
297 if (!strcmp(z.class, "LOGIN")) {
298 if (!strcmp(z.opcode, "USER_FLUSH")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700299 ND_PRINT((ndo, " flush_locs"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800300 return;
301 }
302
303 if (!strcmp(z.opcode, "NONE") ||
304 !strcmp(z.opcode, "OPSTAFF") ||
305 !strcmp(z.opcode, "REALM-VISIBLE") ||
306 !strcmp(z.opcode, "REALM-ANNOUNCED") ||
307 !strcmp(z.opcode, "NET-VISIBLE") ||
308 !strcmp(z.opcode, "NET-ANNOUNCED")) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700309 ND_PRINT((ndo, " set-exposure %s", str_to_lower(z.opcode)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800310 return;
311 }
312 }
313
314 if (!*z.recipient)
315 z.recipient = "*";
316
Elliott Hughes892a68b2015-10-19 14:43:53 -0700317 ND_PRINT((ndo, " to %s", z_triple(z.class, z.inst, z.recipient)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800318 if (*z.opcode)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700319 ND_PRINT((ndo, " op %s", z.opcode));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800320}