Drop ".i0", it is an undefined macro.
[netbsd-mini2440.git] / dist / tcpdump / print-atalk.c
blobfc78458aa39e7465da6c6bfa7cde7970f1999dcf
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 * Format and print AppleTalk packets.
26 #include <sys/cdefs.h>
27 #ifndef lint
28 #if 0
29 static const char rcsid[] _U_ =
30 "@(#) Header: /tcpdump/master/tcpdump/print-atalk.c,v 1.81 2004/05/01 09:41:50 hannes Exp (LBL)";
31 #else
32 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
33 #endif
34 #endif
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
40 #include <tcpdump-stdinc.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <pcap.h>
47 #include "interface.h"
48 #include "addrtoname.h"
49 #include "ethertype.h"
50 #include "extract.h" /* must come after interface.h */
51 #include "appletalk.h"
53 static struct tok type2str[] = {
54 { ddpRTMP, "rtmp" },
55 { ddpRTMPrequest, "rtmpReq" },
56 { ddpECHO, "echo" },
57 { ddpIP, "IP" },
58 { ddpARP, "ARP" },
59 { ddpKLAP, "KLAP" },
60 { 0, NULL }
63 struct aarp {
64 u_int16_t htype, ptype;
65 u_int8_t halen, palen;
66 u_int16_t op;
67 u_int8_t hsaddr[6];
68 u_int8_t psaddr[4];
69 u_int8_t hdaddr[6];
70 u_int8_t pdaddr[4];
73 static char tstr[] = "[|atalk]";
75 static void atp_print(const struct atATP *, u_int);
76 static void atp_bitmap_print(u_char);
77 static void nbp_print(const struct atNBP *, u_int, u_short, u_char, u_char);
78 static const char *print_cstring(const char *, const u_char *);
79 static const struct atNBPtuple *nbp_tuple_print(const struct atNBPtuple *,
80 const u_char *,
81 u_short, u_char, u_char);
82 static const struct atNBPtuple *nbp_name_print(const struct atNBPtuple *,
83 const u_char *);
84 static const char *ataddr_string(u_short, u_char);
85 static void ddp_print(const u_char *, u_int, int, u_short, u_char, u_char);
86 static const char *ddpskt_string(int);
89 * Print LLAP packets received on a physical LocalTalk interface.
91 u_int
92 ltalk_if_print(const struct pcap_pkthdr *h, const u_char *p)
94 return (llap_print(p, h->caplen));
98 * Print AppleTalk LLAP packets.
100 u_int
101 llap_print(register const u_char *bp, u_int length)
103 register const struct LAP *lp;
104 register const struct atDDP *dp;
105 register const struct atShortDDP *sdp;
106 u_short snet;
107 u_int hdrlen;
110 * Our packet is on a 4-byte boundary, as we're either called
111 * directly from a top-level link-layer printer (ltalk_if_print)
112 * or from the UDP printer. The LLAP+DDP header is a multiple
113 * of 4 bytes in length, so the DDP payload is also on a 4-byte
114 * boundary, and we don't need to align it before calling
115 * "ddp_print()".
117 lp = (const struct LAP *)bp;
118 bp += sizeof(*lp);
119 length -= sizeof(*lp);
120 hdrlen = sizeof(*lp);
121 switch (lp->type) {
123 case lapShortDDP:
124 if (length < ddpSSize) {
125 (void)printf(" [|sddp %d]", length);
126 return (length);
128 sdp = (const struct atShortDDP *)bp;
129 printf("%s.%s",
130 ataddr_string(0, lp->src), ddpskt_string(sdp->srcSkt));
131 printf(" > %s.%s:",
132 ataddr_string(0, lp->dst), ddpskt_string(sdp->dstSkt));
133 bp += ddpSSize;
134 length -= ddpSSize;
135 hdrlen += ddpSSize;
136 ddp_print(bp, length, sdp->type, 0, lp->src, sdp->srcSkt);
137 break;
139 case lapDDP:
140 if (length < ddpSize) {
141 (void)printf(" [|ddp %d]", length);
142 return (length);
144 dp = (const struct atDDP *)bp;
145 snet = EXTRACT_16BITS(&dp->srcNet);
146 printf("%s.%s", ataddr_string(snet, dp->srcNode),
147 ddpskt_string(dp->srcSkt));
148 printf(" > %s.%s:",
149 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
150 ddpskt_string(dp->dstSkt));
151 bp += ddpSize;
152 length -= ddpSize;
153 hdrlen += ddpSize;
154 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
155 break;
157 #ifdef notdef
158 case lapKLAP:
159 klap_print(bp, length);
160 break;
161 #endif
163 default:
164 printf("%d > %d at-lap#%d %d",
165 lp->src, lp->dst, lp->type, length);
166 break;
168 return (hdrlen);
172 * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
173 * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
174 * packets in them).
176 void
177 atalk_print(register const u_char *bp, u_int length)
179 register const struct atDDP *dp;
180 u_short snet;
182 if(!eflag)
183 printf("AT ");
185 if (length < ddpSize) {
186 (void)printf(" [|ddp %d]", length);
187 return;
189 dp = (const struct atDDP *)bp;
190 snet = EXTRACT_16BITS(&dp->srcNet);
191 printf("%s.%s", ataddr_string(snet, dp->srcNode),
192 ddpskt_string(dp->srcSkt));
193 printf(" > %s.%s: ",
194 ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
195 ddpskt_string(dp->dstSkt));
196 bp += ddpSize;
197 length -= ddpSize;
198 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
201 /* XXX should probably pass in the snap header and do checks like arp_print() */
202 void
203 aarp_print(register const u_char *bp, u_int length)
205 register const struct aarp *ap;
207 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
209 printf("aarp ");
210 ap = (const struct aarp *)bp;
211 if (EXTRACT_16BITS(&ap->htype) == 1 &&
212 EXTRACT_16BITS(&ap->ptype) == ETHERTYPE_ATALK &&
213 ap->halen == 6 && ap->palen == 4 )
214 switch (EXTRACT_16BITS(&ap->op)) {
216 case 1: /* request */
217 (void)printf("who-has %s tell %s",
218 AT(pdaddr), AT(psaddr));
219 return;
221 case 2: /* response */
222 (void)printf("reply %s is-at %s",
223 AT(pdaddr), etheraddr_string(ap->hdaddr));
224 return;
226 case 3: /* probe (oy!) */
227 (void)printf("probe %s tell %s",
228 AT(pdaddr), AT(psaddr));
229 return;
231 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
232 length, EXTRACT_16BITS(&ap->op), EXTRACT_16BITS(&ap->htype),
233 EXTRACT_16BITS(&ap->ptype), ap->halen, ap->palen);
237 * Print AppleTalk Datagram Delivery Protocol packets.
239 static void
240 ddp_print(register const u_char *bp, register u_int length, register int t,
241 register u_short snet, register u_char snode, u_char skt)
244 switch (t) {
246 case ddpNBP:
247 nbp_print((const struct atNBP *)bp, length, snet, snode, skt);
248 break;
250 case ddpATP:
251 atp_print((const struct atATP *)bp, length);
252 break;
254 case ddpEIGRP:
255 eigrp_print(bp, length);
256 break;
258 default:
259 (void)printf(" at-%s %d", tok2str(type2str, NULL, t), length);
260 break;
264 static void
265 atp_print(register const struct atATP *ap, u_int length)
267 char c;
268 u_int32_t data;
270 if ((const u_char *)(ap + 1) > snapend) {
271 /* Just bail if we don't have the whole chunk. */
272 fputs(tstr, stdout);
273 return;
275 length -= sizeof(*ap);
276 switch (ap->control & 0xc0) {
278 case atpReqCode:
279 (void)printf(" atp-req%s %d",
280 ap->control & atpXO? " " : "*",
281 EXTRACT_16BITS(&ap->transID));
283 atp_bitmap_print(ap->bitmap);
285 if (length != 0)
286 (void)printf(" [len=%d]", length);
288 switch (ap->control & (atpEOM|atpSTS)) {
289 case atpEOM:
290 (void)printf(" [EOM]");
291 break;
292 case atpSTS:
293 (void)printf(" [STS]");
294 break;
295 case atpEOM|atpSTS:
296 (void)printf(" [EOM,STS]");
297 break;
299 break;
301 case atpRspCode:
302 (void)printf(" atp-resp%s%d:%d (%d)",
303 ap->control & atpEOM? "*" : " ",
304 EXTRACT_16BITS(&ap->transID), ap->bitmap, length);
305 switch (ap->control & (atpXO|atpSTS)) {
306 case atpXO:
307 (void)printf(" [XO]");
308 break;
309 case atpSTS:
310 (void)printf(" [STS]");
311 break;
312 case atpXO|atpSTS:
313 (void)printf(" [XO,STS]");
314 break;
316 break;
318 case atpRelCode:
319 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap->transID));
321 atp_bitmap_print(ap->bitmap);
323 /* length should be zero */
324 if (length)
325 (void)printf(" [len=%d]", length);
327 /* there shouldn't be any control flags */
328 if (ap->control & (atpXO|atpEOM|atpSTS)) {
329 c = '[';
330 if (ap->control & atpXO) {
331 (void)printf("%cXO", c);
332 c = ',';
334 if (ap->control & atpEOM) {
335 (void)printf("%cEOM", c);
336 c = ',';
338 if (ap->control & atpSTS) {
339 (void)printf("%cSTS", c);
340 c = ',';
342 (void)printf("]");
344 break;
346 default:
347 (void)printf(" atp-0x%x %d (%d)", ap->control,
348 EXTRACT_16BITS(&ap->transID), length);
349 break;
351 data = EXTRACT_32BITS(&ap->userData);
352 if (data != 0)
353 (void)printf(" 0x%x", data);
356 static void
357 atp_bitmap_print(register u_char bm)
359 register char c;
360 register int i;
363 * The '& 0xff' below is needed for compilers that want to sign
364 * extend a u_char, which is the case with the Ultrix compiler.
365 * (gcc is smart enough to eliminate it, at least on the Sparc).
367 if ((bm + 1) & (bm & 0xff)) {
368 c = '<';
369 for (i = 0; bm; ++i) {
370 if (bm & 1) {
371 (void)printf("%c%d", c, i);
372 c = ',';
374 bm >>= 1;
376 (void)printf(">");
377 } else {
378 for (i = 0; bm; ++i)
379 bm >>= 1;
380 if (i > 1)
381 (void)printf("<0-%d>", i - 1);
382 else
383 (void)printf("<0>");
387 static void
388 nbp_print(register const struct atNBP *np, u_int length, register u_short snet,
389 register u_char snode, register u_char skt)
391 register const struct atNBPtuple *tp =
392 (const struct atNBPtuple *)((u_char *)np + nbpHeaderSize);
393 int i;
394 const u_char *ep;
396 if (length < nbpHeaderSize) {
397 (void)printf(" truncated-nbp %d", length);
398 return;
401 length -= nbpHeaderSize;
402 if (length < 8) {
403 /* must be room for at least one tuple */
404 (void)printf(" truncated-nbp %d", length + nbpHeaderSize);
405 return;
407 /* ep points to end of available data */
408 ep = snapend;
409 if ((const u_char *)tp > ep) {
410 fputs(tstr, stdout);
411 return;
413 switch (i = np->control & 0xf0) {
415 case nbpBrRq:
416 case nbpLkUp:
417 (void)printf(i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:",
418 np->id);
419 if ((const u_char *)(tp + 1) > ep) {
420 fputs(tstr, stdout);
421 return;
423 (void)nbp_name_print(tp, ep);
425 * look for anomalies: the spec says there can only
426 * be one tuple, the address must match the source
427 * address and the enumerator should be zero.
429 if ((np->control & 0xf) != 1)
430 (void)printf(" [ntup=%d]", np->control & 0xf);
431 if (tp->enumerator)
432 (void)printf(" [enum=%d]", tp->enumerator);
433 if (EXTRACT_16BITS(&tp->net) != snet ||
434 tp->node != snode || tp->skt != skt)
435 (void)printf(" [addr=%s.%d]",
436 ataddr_string(EXTRACT_16BITS(&tp->net),
437 tp->node), tp->skt);
438 break;
440 case nbpLkUpReply:
441 (void)printf(" nbp-reply %d:", np->id);
443 /* print each of the tuples in the reply */
444 for (i = np->control & 0xf; --i >= 0 && tp; )
445 tp = nbp_tuple_print(tp, ep, snet, snode, skt);
446 break;
448 default:
449 (void)printf(" nbp-0x%x %d (%d)", np->control, np->id,
450 length);
451 break;
455 /* print a counted string */
456 static const char *
457 print_cstring(register const char *cp, register const u_char *ep)
459 register u_int length;
461 if (cp >= (const char *)ep) {
462 fputs(tstr, stdout);
463 return (0);
465 length = *cp++;
467 /* Spec says string can be at most 32 bytes long */
468 if (length > 32) {
469 (void)printf("[len=%u]", length);
470 return (0);
472 while ((int)--length >= 0) {
473 if (cp >= (const char *)ep) {
474 fputs(tstr, stdout);
475 return (0);
477 putchar(*cp++);
479 return (cp);
482 static const struct atNBPtuple *
483 nbp_tuple_print(register const struct atNBPtuple *tp,
484 register const u_char *ep,
485 register u_short snet, register u_char snode,
486 register u_char skt)
488 register const struct atNBPtuple *tpn;
490 if ((const u_char *)(tp + 1) > ep) {
491 fputs(tstr, stdout);
492 return 0;
494 tpn = nbp_name_print(tp, ep);
496 /* if the enumerator isn't 1, print it */
497 if (tp->enumerator != 1)
498 (void)printf("(%d)", tp->enumerator);
500 /* if the socket doesn't match the src socket, print it */
501 if (tp->skt != skt)
502 (void)printf(" %d", tp->skt);
504 /* if the address doesn't match the src address, it's an anomaly */
505 if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode)
506 (void)printf(" [addr=%s]",
507 ataddr_string(EXTRACT_16BITS(&tp->net), tp->node));
509 return (tpn);
512 static const struct atNBPtuple *
513 nbp_name_print(const struct atNBPtuple *tp, register const u_char *ep)
515 register const char *cp = (const char *)tp + nbpTupleSize;
517 putchar(' ');
519 /* Object */
520 putchar('"');
521 if ((cp = print_cstring(cp, ep)) != NULL) {
522 /* Type */
523 putchar(':');
524 if ((cp = print_cstring(cp, ep)) != NULL) {
525 /* Zone */
526 putchar('@');
527 if ((cp = print_cstring(cp, ep)) != NULL)
528 putchar('"');
531 return ((const struct atNBPtuple *)cp);
535 #define HASHNAMESIZE 4096
537 struct hnamemem {
538 int addr;
539 char *name;
540 struct hnamemem *nxt;
543 static struct hnamemem hnametable[HASHNAMESIZE];
545 static const char *
546 ataddr_string(u_short atnet, u_char athost)
548 register struct hnamemem *tp, *tp2;
549 register int i = (atnet << 8) | athost;
550 char nambuf[MAXHOSTNAMELEN + 20];
551 static int first = 1;
552 FILE *fp;
555 * if this is the first call, see if there's an AppleTalk
556 * number to name map file.
558 if (first && (first = 0, !nflag)
559 && (fp = fopen("/etc/atalk.names", "r"))) {
560 char line[256];
561 int i1, i2, i3;
563 while (fgets(line, sizeof(line), fp)) {
564 if (line[0] == '\n' || line[0] == 0 || line[0] == '#')
565 continue;
566 if (sscanf(line, "%d.%d.%d %256s", &i1, &i2, &i3,
567 nambuf) == 4)
568 /* got a hostname. */
569 i3 |= ((i1 << 8) | i2) << 8;
570 else if (sscanf(line, "%d.%d %256s", &i1, &i2,
571 nambuf) == 3)
572 /* got a net name */
573 i3 = (((i1 << 8) | i2) << 8) | 255;
574 else
575 continue;
577 for (tp = &hnametable[i3 & (HASHNAMESIZE-1)];
578 tp->nxt; tp = tp->nxt)
580 tp->addr = i3;
581 tp->nxt = newhnamemem();
582 tp->name = strdup(nambuf);
584 fclose(fp);
587 for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
588 if (tp->addr == i)
589 return (tp->name);
591 /* didn't have the node name -- see if we've got the net name */
592 i |= 255;
593 for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt)
594 if (tp2->addr == i) {
595 tp->addr = (atnet << 8) | athost;
596 tp->nxt = newhnamemem();
597 (void)snprintf(nambuf, sizeof(nambuf), "%s.%d",
598 tp2->name, athost);
599 tp->name = strdup(nambuf);
600 return (tp->name);
603 tp->addr = (atnet << 8) | athost;
604 tp->nxt = newhnamemem();
605 if (athost != 255)
606 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d.%d",
607 atnet >> 8, atnet & 0xff, athost);
608 else
609 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d", atnet >> 8,
610 atnet & 0xff);
611 tp->name = strdup(nambuf);
613 return (tp->name);
616 static struct tok skt2str[] = {
617 { rtmpSkt, "rtmp" }, /* routing table maintenance */
618 { nbpSkt, "nis" }, /* name info socket */
619 { echoSkt, "echo" }, /* AppleTalk echo protocol */
620 { zipSkt, "zip" }, /* zone info protocol */
621 { 0, NULL }
624 static const char *
625 ddpskt_string(register int skt)
627 static char buf[8];
629 if (nflag) {
630 (void)snprintf(buf, sizeof(buf), "%d", skt);
631 return (buf);
633 return (tok2str(skt2str, "%d", skt));