Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / tcpdump / print-zephyr.c
blob90bae4ee13dab5c4d28c3ffdfe0b20c109a2e239
1 /* $NetBSD$ */
3 /*
4 * Decode and print Zephyr packets.
6 * http://web.mit.edu/zephyr/doc/protocol
8 * Copyright (c) 2001 Nickolai Zeldovich <kolya@MIT.EDU>
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that: (1) source code
13 * distributions retain the above copyright notice and this paragraph
14 * in its entirety, and (2) distributions including binary code include
15 * the above copyright notice and this paragraph in its entirety in
16 * the documentation or other materials provided with the distribution.
17 * The name of the author(s) may not be used to endorse or promote
18 * products derived from this software without specific prior written
19 * permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE.
25 #include <sys/cdefs.h>
26 #ifndef lint
27 #if 0
28 static const char rcsid[] _U_ =
29 "@(#) Header: /tcpdump/master/tcpdump/print-zephyr.c,v 1.8.2.1 2005/04/21 06:51:24 guy Exp";
30 #else
31 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
32 #endif
33 #endif
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
39 #include <tcpdump-stdinc.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
45 #include "interface.h"
47 struct z_packet {
48 char *version;
49 int numfields;
50 int kind;
51 char *uid;
52 int port;
53 int auth;
54 int authlen;
55 char *authdata;
56 char *class;
57 char *inst;
58 char *opcode;
59 char *sender;
60 const char *recipient;
61 char *format;
62 int cksum;
63 int multi;
64 char *multi_uid;
65 /* Other fields follow here.. */
68 enum z_packet_type {
69 Z_PACKET_UNSAFE = 0,
70 Z_PACKET_UNACKED,
71 Z_PACKET_ACKED,
72 Z_PACKET_HMACK,
73 Z_PACKET_HMCTL,
74 Z_PACKET_SERVACK,
75 Z_PACKET_SERVNAK,
76 Z_PACKET_CLIENTACK,
77 Z_PACKET_STAT
80 static struct tok z_types[] = {
81 { Z_PACKET_UNSAFE, "unsafe" },
82 { Z_PACKET_UNACKED, "unacked" },
83 { Z_PACKET_ACKED, "acked" },
84 { Z_PACKET_HMACK, "hm-ack" },
85 { Z_PACKET_HMCTL, "hm-ctl" },
86 { Z_PACKET_SERVACK, "serv-ack" },
87 { Z_PACKET_SERVNAK, "serv-nak" },
88 { Z_PACKET_CLIENTACK, "client-ack" },
89 { Z_PACKET_STAT, "stat" }
92 char z_buf[256];
94 static char *
95 parse_field(char **pptr, int *len)
97 char *s;
99 if (*len <= 0 || !pptr || !*pptr)
100 return NULL;
101 if (*pptr > (char *) snapend)
102 return NULL;
104 s = *pptr;
105 while (*pptr <= (char *) snapend && *len >= 0 && **pptr) {
106 (*pptr)++;
107 (*len)--;
109 (*pptr)++;
110 (*len)--;
111 if (*len < 0 || *pptr > (char *) snapend)
112 return NULL;
113 return s;
116 static const char *
117 z_triple(char *class, char *inst, const char *recipient)
119 if (!*recipient)
120 recipient = "*";
121 snprintf(z_buf, sizeof(z_buf), "<%s,%s,%s>", class, inst, recipient);
122 z_buf[sizeof(z_buf)-1] = '\0';
123 return z_buf;
126 static const char *
127 str_to_lower(char *string)
129 strncpy(z_buf, string, sizeof(z_buf));
130 z_buf[sizeof(z_buf)-1] = '\0';
132 string = z_buf;
133 while (*string) {
134 *string = tolower((unsigned char)(*string));
135 string++;
138 return z_buf;
141 void
142 zephyr_print(const u_char *cp, int length)
144 struct z_packet z;
145 char *parse = (char *) cp;
146 int parselen = length;
147 char *s;
148 int lose = 0;
150 memset(&z, 0, sizeof(z)); /* XXX gcc */
152 #define PARSE_STRING \
153 s = parse_field(&parse, &parselen); \
154 if (!s) lose = 1;
156 #define PARSE_FIELD_INT(field) \
157 PARSE_STRING \
158 if (!lose) field = strtol(s, 0, 16);
160 #define PARSE_FIELD_STR(field) \
161 PARSE_STRING \
162 if (!lose) field = s;
164 PARSE_FIELD_STR(z.version);
165 if (lose) return;
166 if (strncmp(z.version, "ZEPH", 4))
167 return;
169 PARSE_FIELD_INT(z.numfields);
170 PARSE_FIELD_INT(z.kind);
171 PARSE_FIELD_STR(z.uid);
172 PARSE_FIELD_INT(z.port);
173 PARSE_FIELD_INT(z.auth);
174 PARSE_FIELD_INT(z.authlen);
175 PARSE_FIELD_STR(z.authdata);
176 PARSE_FIELD_STR(z.class);
177 PARSE_FIELD_STR(z.inst);
178 PARSE_FIELD_STR(z.opcode);
179 PARSE_FIELD_STR(z.sender);
180 PARSE_FIELD_STR(z.recipient);
181 PARSE_FIELD_STR(z.format);
182 PARSE_FIELD_INT(z.cksum);
183 PARSE_FIELD_INT(z.multi);
184 PARSE_FIELD_STR(z.multi_uid);
186 if (lose) {
187 printf(" [|zephyr] (%d)", length);
188 return;
191 printf(" zephyr");
192 if (strncmp(z.version+4, "0.2", 3)) {
193 printf(" v%s", z.version+4);
194 return;
197 printf(" %s", tok2str(z_types, "type %d", z.kind));
198 if (z.kind == Z_PACKET_SERVACK) {
199 /* Initialization to silence warnings */
200 char *ackdata = NULL;
201 PARSE_FIELD_STR(ackdata);
202 if (!lose && strcmp(ackdata, "SENT"))
203 printf("/%s", str_to_lower(ackdata));
205 if (*z.sender) printf(" %s", z.sender);
207 if (!strcmp(z.class, "USER_LOCATE")) {
208 if (!strcmp(z.opcode, "USER_HIDE"))
209 printf(" hide");
210 else if (!strcmp(z.opcode, "USER_UNHIDE"))
211 printf(" unhide");
212 else
213 printf(" locate %s", z.inst);
214 return;
217 if (!strcmp(z.class, "ZEPHYR_ADMIN")) {
218 printf(" zephyr-admin %s", str_to_lower(z.opcode));
219 return;
222 if (!strcmp(z.class, "ZEPHYR_CTL")) {
223 if (!strcmp(z.inst, "CLIENT")) {
224 if (!strcmp(z.opcode, "SUBSCRIBE") ||
225 !strcmp(z.opcode, "SUBSCRIBE_NODEFS") ||
226 !strcmp(z.opcode, "UNSUBSCRIBE")) {
228 printf(" %ssub%s", strcmp(z.opcode, "SUBSCRIBE") ? "un" : "",
229 strcmp(z.opcode, "SUBSCRIBE_NODEFS") ? "" :
230 "-nodefs");
231 if (z.kind != Z_PACKET_SERVACK) {
232 /* Initialization to silence warnings */
233 char *c = NULL, *i = NULL, *r = NULL;
234 PARSE_FIELD_STR(c);
235 PARSE_FIELD_STR(i);
236 PARSE_FIELD_STR(r);
237 if (!lose) printf(" %s", z_triple(c, i, r));
239 return;
242 if (!strcmp(z.opcode, "GIMME")) {
243 printf(" ret");
244 return;
247 if (!strcmp(z.opcode, "GIMMEDEFS")) {
248 printf(" gimme-defs");
249 return;
252 if (!strcmp(z.opcode, "CLEARSUB")) {
253 printf(" clear-subs");
254 return;
257 printf(" %s", str_to_lower(z.opcode));
258 return;
261 if (!strcmp(z.inst, "HM")) {
262 printf(" %s", str_to_lower(z.opcode));
263 return;
266 if (!strcmp(z.inst, "REALM")) {
267 if (!strcmp(z.opcode, "ADD_SUBSCRIBE"))
268 printf(" realm add-subs");
269 if (!strcmp(z.opcode, "REQ_SUBSCRIBE"))
270 printf(" realm req-subs");
271 if (!strcmp(z.opcode, "RLM_SUBSCRIBE"))
272 printf(" realm rlm-sub");
273 if (!strcmp(z.opcode, "RLM_UNSUBSCRIBE"))
274 printf(" realm rlm-unsub");
275 return;
279 if (!strcmp(z.class, "HM_CTL")) {
280 printf(" hm_ctl %s", str_to_lower(z.inst));
281 printf(" %s", str_to_lower(z.opcode));
282 return;
285 if (!strcmp(z.class, "HM_STAT")) {
286 if (!strcmp(z.inst, "HMST_CLIENT") && !strcmp(z.opcode, "GIMMESTATS")) {
287 printf(" get-client-stats");
288 return;
292 if (!strcmp(z.class, "WG_CTL")) {
293 printf(" wg_ctl %s", str_to_lower(z.inst));
294 printf(" %s", str_to_lower(z.opcode));
295 return;
298 if (!strcmp(z.class, "LOGIN")) {
299 if (!strcmp(z.opcode, "USER_FLUSH")) {
300 printf(" flush_locs");
301 return;
304 if (!strcmp(z.opcode, "NONE") ||
305 !strcmp(z.opcode, "OPSTAFF") ||
306 !strcmp(z.opcode, "REALM-VISIBLE") ||
307 !strcmp(z.opcode, "REALM-ANNOUNCED") ||
308 !strcmp(z.opcode, "NET-VISIBLE") ||
309 !strcmp(z.opcode, "NET-ANNOUNCED")) {
310 printf(" set-exposure %s", str_to_lower(z.opcode));
311 return;
315 if (!*z.recipient)
316 z.recipient = "*";
318 printf(" to %s", z_triple(z.class, z.inst, z.recipient));
319 if (*z.opcode)
320 printf(" op %s", z.opcode);
321 return;