]> git.saurik.com Git - apple/network_cmds.git/blob - tcpdump.tproj/print-atalk.c
f939466a9c1234bbe5f667536479efe216b5fada
[apple/network_cmds.git] / tcpdump.tproj / print-atalk.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) 1988, 1989, 1990, 1991, 1992, 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 AppleTalk packets.
45 */
46
47 #ifndef lint
48 static const char rcsid[] =
49 "@(#) $Header: /cvs/Darwin/Commands/NeXT/network_cmds/tcpdump.tproj/print-atalk.c,v 1.1.1.1 1999/05/02 03:58:33 wsanchez Exp $ (LBL)";
50 #endif
51
52 #include <sys/param.h>
53 #include <sys/time.h>
54 #include <sys/socket.h>
55
56 #if __STDC__
57 struct mbuf;
58 struct rtentry;
59 #endif
60 #include <net/if.h>
61
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/ip_var.h>
66 #include <netinet/if_ether.h>
67 #include <netinet/udp.h>
68 #include <netinet/udp_var.h>
69 #include <netinet/tcp.h>
70 #include <netinet/tcpip.h>
71
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75
76 #include "interface.h"
77 #include "addrtoname.h"
78 #include "ethertype.h"
79 #include "extract.h" /* must come after interface.h */
80 #include "appletalk.h"
81
82 static struct tok type2str[] = {
83 { ddpRTMP, "rtmp" },
84 { ddpRTMPrequest, "rtmpReq" },
85 { ddpECHO, "echo" },
86 { ddpIP, "IP" },
87 { ddpARP, "ARP" },
88 { ddpKLAP, "KLAP" },
89 { 0, NULL }
90 };
91
92 struct aarp {
93 u_short htype, ptype;
94 u_char halen, palen;
95 u_short op;
96 u_char hsaddr[6];
97 u_char psaddr[4];
98 u_char hdaddr[6];
99 u_char pdaddr[4];
100 };
101
102 static char tstr[] = "[|atalk]";
103
104 static void atp_print(const struct atATP *, u_int);
105 static void atp_bitmap_print(u_char);
106 static void nbp_print(const struct atNBP *, u_int, u_short, u_char, u_char);
107 static const char *print_cstring(const char *, const u_char *);
108 static const struct atNBPtuple *nbp_tuple_print(const struct atNBPtuple *,
109 const u_char *,
110 u_short, u_char, u_char);
111 static const struct atNBPtuple *nbp_name_print(const struct atNBPtuple *,
112 const u_char *);
113 static const char *ataddr_string(u_short, u_char);
114 static void ddp_print(const u_char *, u_int, int, u_short, u_char, u_char);
115 static const char *ddpskt_string(int);
116
117 /*
118 * Print AppleTalk Datagram Delivery Protocol packets.
119 */
120 void
121 atalk_print(register const u_char *bp, u_int length)
122 {
123 register const struct LAP *lp;
124 register const struct atDDP *dp;
125 register const struct atShortDDP *sdp;
126 u_short snet;
127
128 lp = (struct LAP *)bp;
129 bp += sizeof(*lp);
130 length -= sizeof(*lp);
131 switch (lp->type) {
132
133 case lapShortDDP:
134 if (length < ddpSSize) {
135 (void)printf(" [|sddp %d]", length);
136 return;
137 }
138 sdp = (const struct atShortDDP *)bp;
139 printf("%s.%s",
140 ataddr_string(0, lp->src), ddpskt_string(sdp->srcSkt));
141 printf(" > %s.%s:",
142 ataddr_string(0, lp->dst), ddpskt_string(sdp->dstSkt));
143 bp += ddpSSize;
144 length -= ddpSSize;
145 ddp_print(bp, length, sdp->type, 0, lp->src, sdp->srcSkt);
146 break;
147
148 case lapDDP:
149 if (length < ddpSize) {
150 (void)printf(" [|ddp %d]", length);
151 return;
152 }
153 dp = (const struct atDDP *)bp;
154 snet = EXTRACT_16BITS(&dp->srcNet);
155 printf("%s.%s", ataddr_string(snet, dp->srcNode),
156 ddpskt_string(dp->srcSkt));
157 printf(" > %s.%s:",
158 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
159 ddpskt_string(dp->dstSkt));
160 bp += ddpSize;
161 length -= ddpSize;
162 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
163 break;
164
165 #ifdef notdef
166 case lapKLAP:
167 klap_print(bp, length);
168 break;
169 #endif
170
171 default:
172 printf("%d > %d at-lap#%d %d",
173 lp->src, lp->dst, lp->type, length);
174 break;
175 }
176 }
177
178 /* XXX should probably pass in the snap header and do checks like arp_print() */
179 void
180 aarp_print(register const u_char *bp, u_int length)
181 {
182 register const struct aarp *ap;
183
184 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
185
186 printf("aarp ");
187 ap = (const struct aarp *)bp;
188 if (ap->htype == 1 && ap->ptype == ETHERTYPE_ATALK &&
189 ap->halen == 6 && ap->palen == 4 )
190 switch (ap->op) {
191
192 case 1: /* request */
193 (void)printf("who-has %s tell %s",
194 AT(pdaddr), AT(psaddr));
195 return;
196
197 case 2: /* response */
198 (void)printf("reply %s is-at %s",
199 AT(pdaddr), etheraddr_string(ap->hdaddr));
200 return;
201
202 case 3: /* probe (oy!) */
203 (void)printf("probe %s tell %s",
204 AT(pdaddr), AT(psaddr));
205 return;
206 }
207 (void)printf("len %d op %d htype %d ptype %#x halen %d palen %d",
208 length, ap->op, ap->htype, ap->ptype, ap->halen, ap->palen );
209 }
210
211 static void
212 ddp_print(register const u_char *bp, register u_int length, register int t,
213 register u_short snet, register u_char snode, u_char skt)
214 {
215
216 switch (t) {
217
218 case ddpNBP:
219 nbp_print((const struct atNBP *)bp, length, snet, snode, skt);
220 break;
221
222 case ddpATP:
223 atp_print((const struct atATP *)bp, length);
224 break;
225
226 default:
227 (void)printf(" at-%s %d", tok2str(type2str, NULL, t), length);
228 break;
229 }
230 }
231
232 static void
233 atp_print(register const struct atATP *ap, u_int length)
234 {
235 char c;
236 u_int32_t data;
237
238 if ((const u_char *)(ap + 1) > snapend) {
239 /* Just bail if we don't have the whole chunk. */
240 fputs(tstr, stdout);
241 return;
242 }
243 length -= sizeof(*ap);
244 switch (ap->control & 0xc0) {
245
246 case atpReqCode:
247 (void)printf(" atp-req%s %d",
248 ap->control & atpXO? " " : "*",
249 EXTRACT_16BITS(&ap->transID));
250
251 atp_bitmap_print(ap->bitmap);
252
253 if (length != 0)
254 (void)printf(" [len=%d]", length);
255
256 switch (ap->control & (atpEOM|atpSTS)) {
257 case atpEOM:
258 (void)printf(" [EOM]");
259 break;
260 case atpSTS:
261 (void)printf(" [STS]");
262 break;
263 case atpEOM|atpSTS:
264 (void)printf(" [EOM,STS]");
265 break;
266 }
267 break;
268
269 case atpRspCode:
270 (void)printf(" atp-resp%s%d:%d (%d)",
271 ap->control & atpEOM? "*" : " ",
272 EXTRACT_16BITS(&ap->transID), ap->bitmap, length);
273 switch (ap->control & (atpXO|atpSTS)) {
274 case atpXO:
275 (void)printf(" [XO]");
276 break;
277 case atpSTS:
278 (void)printf(" [STS]");
279 break;
280 case atpXO|atpSTS:
281 (void)printf(" [XO,STS]");
282 break;
283 }
284 break;
285
286 case atpRelCode:
287 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap->transID));
288
289 atp_bitmap_print(ap->bitmap);
290
291 /* length should be zero */
292 if (length)
293 (void)printf(" [len=%d]", length);
294
295 /* there shouldn't be any control flags */
296 if (ap->control & (atpXO|atpEOM|atpSTS)) {
297 c = '[';
298 if (ap->control & atpXO) {
299 (void)printf("%cXO", c);
300 c = ',';
301 }
302 if (ap->control & atpEOM) {
303 (void)printf("%cEOM", c);
304 c = ',';
305 }
306 if (ap->control & atpSTS) {
307 (void)printf("%cSTS", c);
308 c = ',';
309 }
310 (void)printf("]");
311 }
312 break;
313
314 default:
315 (void)printf(" atp-0x%x %d (%d)", ap->control,
316 EXTRACT_16BITS(&ap->transID), length);
317 break;
318 }
319 data = EXTRACT_32BITS(&ap->userData);
320 if (data != 0)
321 (void)printf(" 0x%x", data);
322 }
323
324 static void
325 atp_bitmap_print(register u_char bm)
326 {
327 register char c;
328 register int i;
329
330 /*
331 * The '& 0xff' below is needed for compilers that want to sign
332 * extend a u_char, which is the case with the Ultrix compiler.
333 * (gcc is smart enough to eliminate it, at least on the Sparc).
334 */
335 if ((bm + 1) & (bm & 0xff)) {
336 c = '<';
337 for (i = 0; bm; ++i) {
338 if (bm & 1) {
339 (void)printf("%c%d", c, i);
340 c = ',';
341 }
342 bm >>= 1;
343 }
344 (void)printf(">");
345 } else {
346 for (i = 0; bm; ++i)
347 bm >>= 1;
348 if (i > 1)
349 (void)printf("<0-%d>", i - 1);
350 else
351 (void)printf("<0>");
352 }
353 }
354
355 static void
356 nbp_print(register const struct atNBP *np, u_int length, register u_short snet,
357 register u_char snode, register u_char skt)
358 {
359 register const struct atNBPtuple *tp =
360 (struct atNBPtuple *)((u_char *)np + nbpHeaderSize);
361 int i;
362 const u_char *ep;
363
364 length -= nbpHeaderSize;
365 if (length < 8) {
366 /* must be room for at least one tuple */
367 (void)printf(" truncated-nbp %d", length + nbpHeaderSize);
368 return;
369 }
370 /* ep points to end of available data */
371 ep = snapend;
372 if ((const u_char *)tp > ep) {
373 fputs(tstr, stdout);
374 return;
375 }
376 switch (i = np->control & 0xf0) {
377
378 case nbpBrRq:
379 case nbpLkUp:
380 (void)printf(i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:",
381 np->id);
382 if ((const u_char *)(tp + 1) > ep) {
383 fputs(tstr, stdout);
384 return;
385 }
386 (void)nbp_name_print(tp, ep);
387 /*
388 * look for anomalies: the spec says there can only
389 * be one tuple, the address must match the source
390 * address and the enumerator should be zero.
391 */
392 if ((np->control & 0xf) != 1)
393 (void)printf(" [ntup=%d]", np->control & 0xf);
394 if (tp->enumerator)
395 (void)printf(" [enum=%d]", tp->enumerator);
396 if (EXTRACT_16BITS(&tp->net) != snet ||
397 tp->node != snode || tp->skt != skt)
398 (void)printf(" [addr=%s.%d]",
399 ataddr_string(EXTRACT_16BITS(&tp->net),
400 tp->node), tp->skt);
401 break;
402
403 case nbpLkUpReply:
404 (void)printf(" nbp-reply %d:", np->id);
405
406 /* print each of the tuples in the reply */
407 for (i = np->control & 0xf; --i >= 0 && tp; )
408 tp = nbp_tuple_print(tp, ep, snet, snode, skt);
409 break;
410
411 default:
412 (void)printf(" nbp-0x%x %d (%d)", np->control, np->id,
413 length);
414 break;
415 }
416 }
417
418 /* print a counted string */
419 static const char *
420 print_cstring(register const char *cp, register const u_char *ep)
421 {
422 register u_int length;
423
424 if (cp >= (const char *)ep) {
425 fputs(tstr, stdout);
426 return (0);
427 }
428 length = *cp++;
429
430 /* Spec says string can be at most 32 bytes long */
431 if (length < 0 || length > 32) {
432 (void)printf("[len=%d]", length);
433 return (0);
434 }
435 while (--length >= 0) {
436 if (cp >= (char *)ep) {
437 fputs(tstr, stdout);
438 return (0);
439 }
440 putchar(*cp++);
441 }
442 return (cp);
443 }
444
445 static const struct atNBPtuple *
446 nbp_tuple_print(register const struct atNBPtuple *tp,
447 register const u_char *ep,
448 register u_short snet, register u_char snode,
449 register u_char skt)
450 {
451 register const struct atNBPtuple *tpn;
452
453 if ((const u_char *)(tp + 1) > ep) {
454 fputs(tstr, stdout);
455 return 0;
456 }
457 tpn = nbp_name_print(tp, ep);
458
459 /* if the enumerator isn't 1, print it */
460 if (tp->enumerator != 1)
461 (void)printf("(%d)", tp->enumerator);
462
463 /* if the socket doesn't match the src socket, print it */
464 if (tp->skt != skt)
465 (void)printf(" %d", tp->skt);
466
467 /* if the address doesn't match the src address, it's an anomaly */
468 if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode)
469 (void)printf(" [addr=%s]",
470 ataddr_string(EXTRACT_16BITS(&tp->net), tp->node));
471
472 return (tpn);
473 }
474
475 static const struct atNBPtuple *
476 nbp_name_print(const struct atNBPtuple *tp, register const u_char *ep)
477 {
478 register const char *cp = (const char *)tp + nbpTupleSize;
479
480 putchar(' ');
481
482 /* Object */
483 putchar('"');
484 if ((cp = print_cstring(cp, ep)) != NULL) {
485 /* Type */
486 putchar(':');
487 if ((cp = print_cstring(cp, ep)) != NULL) {
488 /* Zone */
489 putchar('@');
490 if ((cp = print_cstring(cp, ep)) != NULL)
491 putchar('"');
492 }
493 }
494 return ((const struct atNBPtuple *)cp);
495 }
496
497
498 #define HASHNAMESIZE 4096
499
500 struct hnamemem {
501 int addr;
502 char *name;
503 struct hnamemem *nxt;
504 };
505
506 static struct hnamemem hnametable[HASHNAMESIZE];
507
508 static const char *
509 ataddr_string(u_short atnet, u_char athost)
510 {
511 register struct hnamemem *tp, *tp2;
512 register int i = (atnet << 8) | athost;
513 char nambuf[256];
514 static int first = 1;
515 FILE *fp;
516
517 /*
518 * if this is the first call, see if there's an AppleTalk
519 * number to name map file.
520 */
521 if (first && (first = 0, !nflag)
522 && (fp = fopen("/etc/atalk.names", "r"))) {
523 char line[256];
524 int i1, i2, i3;
525
526 while (fgets(line, sizeof(line), fp)) {
527 if (line[0] == '\n' || line[0] == 0 || line[0] == '#')
528 continue;
529 if (sscanf(line, "%d.%d.%d %s", &i1, &i2, &i3,
530 nambuf) == 4)
531 /* got a hostname. */
532 i3 |= ((i1 << 8) | i2) << 8;
533 else if (sscanf(line, "%d.%d %s", &i1, &i2,
534 nambuf) == 3)
535 /* got a net name */
536 i3 = (((i1 << 8) | i2) << 8) | 255;
537 else
538 continue;
539
540 for (tp = &hnametable[i3 & (HASHNAMESIZE-1)];
541 tp->nxt; tp = tp->nxt)
542 ;
543 tp->addr = i3;
544 tp->nxt = newhnamemem();
545 tp->name = savestr(nambuf);
546 }
547 fclose(fp);
548 }
549
550 for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
551 if (tp->addr == i)
552 return (tp->name);
553
554 /* didn't have the node name -- see if we've got the net name */
555 i |= 255;
556 for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt)
557 if (tp2->addr == i) {
558 tp->addr = (atnet << 8) | athost;
559 tp->nxt = newhnamemem();
560 (void)sprintf(nambuf, "%s.%d", tp2->name, athost);
561 tp->name = savestr(nambuf);
562 return (tp->name);
563 }
564
565 tp->addr = (atnet << 8) | athost;
566 tp->nxt = newhnamemem();
567 if (athost != 255)
568 (void)sprintf(nambuf, "%d.%d.%d",
569 atnet >> 8, atnet & 0xff, athost);
570 else
571 (void)sprintf(nambuf, "%d.%d", atnet >> 8, atnet & 0xff);
572 tp->name = savestr(nambuf);
573
574 return (tp->name);
575 }
576
577 static struct tok skt2str[] = {
578 { rtmpSkt, "rtmp" }, /* routing table maintenance */
579 { nbpSkt, "nis" }, /* name info socket */
580 { echoSkt, "echo" }, /* AppleTalk echo protocol */
581 { zipSkt, "zip" }, /* zone info protocol */
582 { 0, NULL }
583 };
584
585 static const char *
586 ddpskt_string(register int skt)
587 {
588 static char buf[8];
589
590 if (nflag) {
591 (void)sprintf(buf, "%d", skt);
592 return (buf);
593 }
594 return (tok2str(skt2str, "%d", skt));
595 }