]> git.saurik.com Git - apple/network_cmds.git/blob - tcpdump.tproj/print-bootp.c
c93872852af2e189d2714875b5d7ca10a695b600
[apple/network_cmds.git] / tcpdump.tproj / print-bootp.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that: (1) source code distributions
30 * retain the above copyright notice and this paragraph in its entirety, (2)
31 * distributions including binary code include the above copyright notice and
32 * this paragraph in its entirety in the documentation or other materials
33 * provided with the distribution, and (3) all advertising materials mentioning
34 * features or use of this software display the following acknowledgement:
35 * ``This product includes software developed by the University of California,
36 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
37 * the University nor the names of its contributors may be used to endorse
38 * or promote products derived from this software without specific prior
39 * written permission.
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
41 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
42 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
43 *
44 * Format and print bootp packets.
45 */
46 #ifndef lint
47 static const char rcsid[] =
48 "@(#) $Header: /cvs/Darwin/Commands/NeXT/network_cmds/tcpdump.tproj/print-bootp.c,v 1.1.1.1 1999/05/02 03:58:33 wsanchez Exp $ (LBL)";
49 #endif
50
51 #include <sys/param.h>
52 #include <sys/time.h>
53 #include <sys/socket.h>
54
55 #if __STDC__
56 struct mbuf;
57 struct rtentry;
58 #endif
59 #include <net/if.h>
60
61 #include <netinet/in.h>
62 #include <netinet/if_ether.h>
63
64 #include <ctype.h>
65 #include <stdio.h>
66 #include <string.h>
67
68 #include "interface.h"
69 #include "addrtoname.h"
70 #include "bootp.h"
71
72 static void rfc1048_print(const u_char *, u_int);
73 static void cmu_print(const u_char *, u_int);
74
75 static char tstr[] = " [|bootp]";
76
77 /*
78 * Print bootp requests
79 */
80 void
81 bootp_print(register const u_char *cp, u_int length,
82 u_short sport, u_short dport)
83 {
84 register const struct bootp *bp;
85 static u_char vm_cmu[4] = VM_CMU;
86 static u_char vm_rfc1048[4] = VM_RFC1048;
87
88 bp = (struct bootp *)cp;
89 TCHECK(bp->bp_op);
90 switch (bp->bp_op) {
91
92 case BOOTREQUEST:
93 /* Usually, a request goes from a client to a server */
94 if (sport != IPPORT_BOOTPC || dport != IPPORT_BOOTPS)
95 printf(" (request)");
96 break;
97
98 case BOOTREPLY:
99 /* Usually, a reply goes from a server to a client */
100 if (sport != IPPORT_BOOTPS || dport != IPPORT_BOOTPC)
101 printf(" (reply)");
102 break;
103
104 default:
105 printf(" bootp-#%d", bp->bp_op);
106 }
107
108 TCHECK(bp->bp_secs);
109
110 /* The usual hardware address type is 1 (10Mb Ethernet) */
111 if (bp->bp_htype != 1)
112 printf(" htype-#%d", bp->bp_htype);
113
114 /* The usual length for 10Mb Ethernet address is 6 bytes */
115 if (bp->bp_htype != 1 || bp->bp_hlen != 6)
116 printf(" hlen:%d", bp->bp_hlen);
117
118 /* Only print interesting fields */
119 if (bp->bp_hops)
120 printf(" hops:%d", bp->bp_hops);
121 if (bp->bp_xid)
122 printf(" xid:0x%x", (u_int32_t)ntohl(bp->bp_xid));
123 if (bp->bp_secs)
124 printf(" secs:%d", ntohs(bp->bp_secs));
125
126 /* Client's ip address */
127 TCHECK(bp->bp_ciaddr);
128 if (bp->bp_ciaddr.s_addr)
129 printf(" C:%s", ipaddr_string(&bp->bp_ciaddr));
130
131 /* 'your' ip address (bootp client) */
132 TCHECK(bp->bp_yiaddr);
133 if (bp->bp_yiaddr.s_addr)
134 printf(" Y:%s", ipaddr_string(&bp->bp_yiaddr));
135
136 /* Server's ip address */
137 TCHECK(bp->bp_siaddr);
138 if (bp->bp_siaddr.s_addr)
139 printf(" S:%s", ipaddr_string(&bp->bp_siaddr));
140
141 /* Gateway's ip address */
142 TCHECK(bp->bp_giaddr);
143 if (bp->bp_giaddr.s_addr)
144 printf(" G:%s", ipaddr_string(&bp->bp_giaddr));
145
146 /* Client's Ethernet address */
147 if (bp->bp_htype == 1 && bp->bp_hlen == 6) {
148 register const struct ether_header *eh;
149 register const char *e;
150
151 TCHECK2(bp->bp_chaddr[0], 6);
152 eh = (struct ether_header *)packetp;
153 if (bp->bp_op == BOOTREQUEST)
154 e = (const char *)ESRC(eh);
155 else if (bp->bp_op == BOOTREPLY)
156 e = (const char *)EDST(eh);
157 else
158 e = 0;
159 if (e == 0 || memcmp((char *)bp->bp_chaddr, e, 6) != 0)
160 printf(" ether %s", etheraddr_string(bp->bp_chaddr));
161 }
162
163 TCHECK2(bp->bp_sname[0], 1); /* check first char only */
164 if (*bp->bp_sname) {
165 printf(" sname \"");
166 if (fn_print(bp->bp_sname, snapend)) {
167 putchar('"');
168 fputs(tstr + 1, stdout);
169 return;
170 }
171 }
172 TCHECK2(bp->bp_sname[0], 1); /* check first char only */
173 if (*bp->bp_file) {
174 printf(" file \"");
175 if (fn_print(bp->bp_file, snapend)) {
176 putchar('"');
177 fputs(tstr + 1, stdout);
178 return;
179 }
180 }
181
182 /* Decode the vendor buffer */
183 TCHECK(bp->bp_vend[0]);
184 length -= sizeof(*bp) - sizeof(bp->bp_vend);
185 if (memcmp((char *)bp->bp_vend, (char *)vm_rfc1048,
186 sizeof(u_int32_t)) == 0)
187 rfc1048_print(bp->bp_vend, length);
188 else if (memcmp((char *)bp->bp_vend, (char *)vm_cmu,
189 sizeof(u_int32_t)) == 0)
190 cmu_print(bp->bp_vend, length);
191 else {
192 u_int32_t ul;
193
194 memcpy((char *)&ul, (char *)bp->bp_vend, sizeof(ul));
195 if (ul != 0)
196 printf("vend-#0x%x", ul);
197 }
198
199 return;
200 trunc:
201 fputs(tstr, stdout);
202 }
203
204 /* The first character specifies the format to print */
205 static struct tok tag2str[] = {
206 /* RFC1048 tags */
207 { TAG_PAD, " PAD" },
208 { TAG_SUBNET_MASK, "iSM" }, /* subnet mask (RFC950) */
209 { TAG_TIME_OFFSET, "lTZ" }, /* seconds from UTC */
210 { TAG_GATEWAY, "iDG" }, /* default gateway */
211 { TAG_TIME_SERVER, "iTS" }, /* time servers (RFC868) */
212 { TAG_NAME_SERVER, "iIEN" }, /* IEN name servers (IEN116) */
213 { TAG_DOMAIN_SERVER, "iNS" }, /* domain name (RFC1035) */
214 { TAG_LOG_SERVER, "iLOG" }, /* MIT log servers */
215 { TAG_COOKIE_SERVER, "iCS" }, /* cookie servers (RFC865) */
216 { TAG_LPR_SERVER, "iLPR" }, /* lpr server (RFC1179) */
217 { TAG_IMPRESS_SERVER, "iIM" }, /* impress servers (Imagen) */
218 { TAG_RLP_SERVER, "iRL" }, /* resource location (RFC887) */
219 { TAG_HOSTNAME, "aHN" }, /* ascii hostname */
220 { TAG_BOOTSIZE, "sBS" }, /* 512 byte blocks */
221 { TAG_END, " END" },
222 /* RFC1497 tags */
223 { TAG_DUMPPATH, "aDP" },
224 { TAG_DOMAINNAME, "aDN" },
225 { TAG_SWAP_SERVER, "iSS" },
226 { TAG_ROOTPATH, "aRP" },
227 { TAG_EXTPATH, "aEP" },
228 { 0, NULL }
229 };
230
231 static void
232 rfc1048_print(register const u_char *bp, register u_int length)
233 {
234 register u_char tag;
235 register u_int len, size;
236 register const char *cp;
237 register char c;
238 int first;
239 u_int32_t ul;
240 u_short us;
241
242 printf(" vend-rfc1048");
243
244 /* Step over magic cookie */
245 bp += sizeof(int32_t);
246
247 /* Loop while we there is a tag left in the buffer */
248 while (bp + 1 < snapend) {
249 tag = *bp++;
250 if (tag == TAG_PAD)
251 continue;
252 if (tag == TAG_END)
253 return;
254 cp = tok2str(tag2str, "?T%d", tag);
255 c = *cp++;
256 printf(" %s:", cp);
257
258 /* Get the length; check for truncation */
259 if (bp + 1 >= snapend) {
260 fputs(tstr, stdout);
261 return;
262 }
263 len = *bp++;
264 if (bp + len >= snapend) {
265 fputs(tstr, stdout);
266 return;
267 }
268
269 /* Print data */
270 size = len;
271 if (c == '?') {
272 /* Base default formats for unknown tags on data size */
273 if (size & 1)
274 c = 'b';
275 else if (size & 2)
276 c = 's';
277 else
278 c = 'l';
279 }
280 first = 1;
281 switch (c) {
282
283 case 'a':
284 /* ascii strings */
285 putchar('"');
286 (void)fn_printn(bp, size, NULL);
287 putchar('"');
288 bp += size;
289 size = 0;
290 break;
291
292 case 'i':
293 case 'l':
294 /* ip addresses/32-bit words */
295 while (size >= sizeof(ul)) {
296 if (!first)
297 putchar(',');
298 memcpy((char *)&ul, (char *)bp, sizeof(ul));
299 if (c == 'i')
300 printf("%s", ipaddr_string(&ul));
301 else
302 printf("%u", ul);
303 bp += sizeof(ul);
304 size -= sizeof(ul);
305 first = 0;
306 }
307 break;
308
309 case 's':
310 /* shorts */
311 while (size >= sizeof(us)) {
312 if (!first)
313 putchar(',');
314 memcpy((char *)&us, (char *)bp, sizeof(us));
315 printf("%d", us);
316 bp += sizeof(us);
317 size -= sizeof(us);
318 first = 0;
319 }
320 break;
321
322 case 'b':
323 default:
324 /* Bytes */
325 while (size > 0) {
326 if (!first)
327 putchar('.');
328 printf("%d", *bp);
329 ++bp;
330 --size;
331 first = 0;
332 }
333 break;
334 }
335 /* Data left over? */
336 if (size)
337 printf("[len %d]", len);
338 }
339 }
340
341 static void
342 cmu_print(register const u_char *bp, register u_int length)
343 {
344 register const struct cmu_vend *cmu;
345 char *fmt = " %s:%s";
346
347 #define PRINTCMUADDR(m, s) { TCHECK(cmu->m); \
348 if (cmu->m.s_addr != 0) \
349 printf(fmt, s, ipaddr_string(&cmu->m.s_addr)); }
350
351 printf(" vend-cmu");
352 cmu = (struct cmu_vend *)bp;
353
354 /* Only print if there are unknown bits */
355 TCHECK(cmu->v_flags);
356 if ((cmu->v_flags & ~(VF_SMASK)) != 0)
357 printf(" F:0x%x", cmu->v_flags);
358 PRINTCMUADDR(v_dgate, "DG");
359 PRINTCMUADDR(v_smask, cmu->v_flags & VF_SMASK ? "SM" : "SM*");
360 PRINTCMUADDR(v_dns1, "NS1");
361 PRINTCMUADDR(v_dns2, "NS2");
362 PRINTCMUADDR(v_ins1, "IEN1");
363 PRINTCMUADDR(v_ins2, "IEN2");
364 PRINTCMUADDR(v_ts1, "TS1");
365 PRINTCMUADDR(v_ts2, "TS2");
366 return;
367
368 trunc:
369 fputs(tstr, stdout);
370 #undef PRINTCMUADDR
371 }