OSDN Git Service

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