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