]>
The Tcpdump Group git mirrors - tcpdump/blob - print-atalk.c
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 * Format and print AppleTalk packets.
25 static const char rcsid
[] =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-atalk.c,v 1.72 2002-08-01 08:53:00 risso Exp $ (LBL)";
33 #include <tcpdump-stdinc.h>
38 #include <netdb.h> /* for MAXHOSTNAMELEN on some platforms */
41 #include "interface.h"
42 #include "addrtoname.h"
43 #include "ethertype.h"
44 #include "extract.h" /* must come after interface.h */
45 #include "appletalk.h"
47 static struct tok type2str
[] = {
49 { ddpRTMPrequest
, "rtmpReq" },
58 u_int16_t htype
, ptype
;
59 u_int8_t halen
, palen
;
67 static char tstr
[] = "[|atalk]";
69 static void atp_print(const struct atATP
*, u_int
);
70 static void atp_bitmap_print(u_char
);
71 static void nbp_print(const struct atNBP
*, u_int
, u_short
, u_char
, u_char
);
72 static const char *print_cstring(const char *, const u_char
*);
73 static const struct atNBPtuple
*nbp_tuple_print(const struct atNBPtuple
*,
75 u_short
, u_char
, u_char
);
76 static const struct atNBPtuple
*nbp_name_print(const struct atNBPtuple
*,
78 static const char *ataddr_string(u_short
, u_char
);
79 static void ddp_print(const u_char
*, u_int
, int, u_short
, u_char
, u_char
);
80 static const char *ddpskt_string(int);
83 * Print LLAP packets received on a physical LocalTalk interface.
86 ltalk_if_print(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*p
)
88 snapend
= p
+ h
->caplen
;
91 llap_print(p
, h
->caplen
);
93 default_print(p
, h
->caplen
);
101 * Print AppleTalk LLAP packets.
104 llap_print(register const u_char
*bp
, u_int length
)
106 register const struct LAP
*lp
;
107 register const struct atDDP
*dp
;
108 register const struct atShortDDP
*sdp
;
112 * Our packet is on a 4-byte boundary, as we're either called
113 * directly from a top-level link-layer printer (ltalk_if_print)
114 * or from the UDP printer. The LLAP+DDP header is a multiple
115 * of 4 bytes in length, so the DDP payload is also on a 4-byte
116 * boundary, and we don't need to align it before calling
119 lp
= (const struct LAP
*)bp
;
121 length
-= sizeof(*lp
);
125 if (length
< ddpSSize
) {
126 (void)printf(" [|sddp %d]", length
);
129 sdp
= (const struct atShortDDP
*)bp
;
131 ataddr_string(0, lp
->src
), ddpskt_string(sdp
->srcSkt
));
133 ataddr_string(0, lp
->dst
), ddpskt_string(sdp
->dstSkt
));
136 ddp_print(bp
, length
, sdp
->type
, 0, lp
->src
, sdp
->srcSkt
);
140 if (length
< ddpSize
) {
141 (void)printf(" [|ddp %d]", 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
));
149 ataddr_string(EXTRACT_16BITS(&dp
->dstNet
), dp
->dstNode
),
150 ddpskt_string(dp
->dstSkt
));
153 ddp_print(bp
, length
, dp
->type
, snet
, dp
->srcNode
, dp
->srcSkt
);
158 klap_print(bp
, length
);
163 printf("%d > %d at-lap#%d %d",
164 lp
->src
, lp
->dst
, lp
->type
, length
);
170 * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
171 * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
175 atalk_print(register const u_char
*bp
, u_int length
)
177 register const struct atDDP
*dp
;
180 if (length
< ddpSize
) {
181 (void)printf(" [|ddp %d]", length
);
184 dp
= (const struct atDDP
*)bp
;
185 snet
= EXTRACT_16BITS(&dp
->srcNet
);
186 printf("%s.%s", ataddr_string(snet
, dp
->srcNode
),
187 ddpskt_string(dp
->srcSkt
));
189 ataddr_string(EXTRACT_16BITS(&dp
->dstNet
), dp
->dstNode
),
190 ddpskt_string(dp
->dstSkt
));
195 static u_char
*abuf
= NULL
;
198 abuf
= (u_char
*)malloc(snaplen
);
200 error("atalk_print: malloc");
202 memcpy((char *)abuf
, (char *)bp
, min(length
, snaplen
));
203 snapend
+= abuf
- (u_char
*)bp
;
208 ddp_print(bp
, length
, dp
->type
, snet
, dp
->srcNode
, dp
->srcSkt
);
211 /* XXX should probably pass in the snap header and do checks like arp_print() */
213 aarp_print(register const u_char
*bp
, u_int length
)
215 register const struct aarp
*ap
;
217 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
220 ap
= (const struct aarp
*)bp
;
221 if (ntohs(ap
->htype
) == 1 && ntohs(ap
->ptype
) == ETHERTYPE_ATALK
&&
222 ap
->halen
== 6 && ap
->palen
== 4 )
223 switch (ntohs(ap
->op
)) {
225 case 1: /* request */
226 (void)printf("who-has %s tell %s",
227 AT(pdaddr
), AT(psaddr
));
230 case 2: /* response */
231 (void)printf("reply %s is-at %s",
232 AT(pdaddr
), etheraddr_string(ap
->hdaddr
));
235 case 3: /* probe (oy!) */
236 (void)printf("probe %s tell %s",
237 AT(pdaddr
), AT(psaddr
));
240 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
241 length
, ntohs(ap
->op
), ntohs(ap
->htype
), ntohs(ap
->ptype
),
242 ap
->halen
, ap
->palen
);
246 * Print AppleTalk Datagram Delivery Protocol packets.
249 ddp_print(register const u_char
*bp
, register u_int length
, register int t
,
250 register u_short snet
, register u_char snode
, u_char skt
)
256 nbp_print((const struct atNBP
*)bp
, length
, snet
, snode
, skt
);
260 atp_print((const struct atATP
*)bp
, length
);
264 (void)printf(" at-%s %d", tok2str(type2str
, NULL
, t
), length
);
270 atp_print(register const struct atATP
*ap
, u_int length
)
275 if ((const u_char
*)(ap
+ 1) > snapend
) {
276 /* Just bail if we don't have the whole chunk. */
280 length
-= sizeof(*ap
);
281 switch (ap
->control
& 0xc0) {
284 (void)printf(" atp-req%s %d",
285 ap
->control
& atpXO
? " " : "*",
286 EXTRACT_16BITS(&ap
->transID
));
288 atp_bitmap_print(ap
->bitmap
);
291 (void)printf(" [len=%d]", length
);
293 switch (ap
->control
& (atpEOM
|atpSTS
)) {
295 (void)printf(" [EOM]");
298 (void)printf(" [STS]");
301 (void)printf(" [EOM,STS]");
307 (void)printf(" atp-resp%s%d:%d (%d)",
308 ap
->control
& atpEOM
? "*" : " ",
309 EXTRACT_16BITS(&ap
->transID
), ap
->bitmap
, length
);
310 switch (ap
->control
& (atpXO
|atpSTS
)) {
312 (void)printf(" [XO]");
315 (void)printf(" [STS]");
318 (void)printf(" [XO,STS]");
324 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap
->transID
));
326 atp_bitmap_print(ap
->bitmap
);
328 /* length should be zero */
330 (void)printf(" [len=%d]", length
);
332 /* there shouldn't be any control flags */
333 if (ap
->control
& (atpXO
|atpEOM
|atpSTS
)) {
335 if (ap
->control
& atpXO
) {
336 (void)printf("%cXO", c
);
339 if (ap
->control
& atpEOM
) {
340 (void)printf("%cEOM", c
);
343 if (ap
->control
& atpSTS
) {
344 (void)printf("%cSTS", c
);
352 (void)printf(" atp-0x%x %d (%d)", ap
->control
,
353 EXTRACT_16BITS(&ap
->transID
), length
);
356 data
= EXTRACT_32BITS(&ap
->userData
);
358 (void)printf(" 0x%x", data
);
362 atp_bitmap_print(register u_char bm
)
368 * The '& 0xff' below is needed for compilers that want to sign
369 * extend a u_char, which is the case with the Ultrix compiler.
370 * (gcc is smart enough to eliminate it, at least on the Sparc).
372 if ((bm
+ 1) & (bm
& 0xff)) {
374 for (i
= 0; bm
; ++i
) {
376 (void)printf("%c%d", c
, i
);
386 (void)printf("<0-%d>", i
- 1);
393 nbp_print(register const struct atNBP
*np
, u_int length
, register u_short snet
,
394 register u_char snode
, register u_char skt
)
396 register const struct atNBPtuple
*tp
=
397 (const struct atNBPtuple
*)((u_char
*)np
+ nbpHeaderSize
);
401 if (length
< nbpHeaderSize
) {
402 (void)printf(" truncated-nbp %d", length
);
406 length
-= nbpHeaderSize
;
408 /* must be room for at least one tuple */
409 (void)printf(" truncated-nbp %d", length
+ nbpHeaderSize
);
412 /* ep points to end of available data */
414 if ((const u_char
*)tp
> ep
) {
418 switch (i
= np
->control
& 0xf0) {
422 (void)printf(i
== nbpLkUp
? " nbp-lkup %d:":" nbp-brRq %d:",
424 if ((const u_char
*)(tp
+ 1) > ep
) {
428 (void)nbp_name_print(tp
, ep
);
430 * look for anomalies: the spec says there can only
431 * be one tuple, the address must match the source
432 * address and the enumerator should be zero.
434 if ((np
->control
& 0xf) != 1)
435 (void)printf(" [ntup=%d]", np
->control
& 0xf);
437 (void)printf(" [enum=%d]", tp
->enumerator
);
438 if (EXTRACT_16BITS(&tp
->net
) != snet
||
439 tp
->node
!= snode
|| tp
->skt
!= skt
)
440 (void)printf(" [addr=%s.%d]",
441 ataddr_string(EXTRACT_16BITS(&tp
->net
),
446 (void)printf(" nbp-reply %d:", np
->id
);
448 /* print each of the tuples in the reply */
449 for (i
= np
->control
& 0xf; --i
>= 0 && tp
; )
450 tp
= nbp_tuple_print(tp
, ep
, snet
, snode
, skt
);
454 (void)printf(" nbp-0x%x %d (%d)", np
->control
, np
->id
,
460 /* print a counted string */
462 print_cstring(register const char *cp
, register const u_char
*ep
)
464 register u_int length
;
466 if (cp
>= (const char *)ep
) {
472 /* Spec says string can be at most 32 bytes long */
474 (void)printf("[len=%u]", length
);
477 while ((int)--length
>= 0) {
478 if (cp
>= (const char *)ep
) {
487 static const struct atNBPtuple
*
488 nbp_tuple_print(register const struct atNBPtuple
*tp
,
489 register const u_char
*ep
,
490 register u_short snet
, register u_char snode
,
493 register const struct atNBPtuple
*tpn
;
495 if ((const u_char
*)(tp
+ 1) > ep
) {
499 tpn
= nbp_name_print(tp
, ep
);
501 /* if the enumerator isn't 1, print it */
502 if (tp
->enumerator
!= 1)
503 (void)printf("(%d)", tp
->enumerator
);
505 /* if the socket doesn't match the src socket, print it */
507 (void)printf(" %d", tp
->skt
);
509 /* if the address doesn't match the src address, it's an anomaly */
510 if (EXTRACT_16BITS(&tp
->net
) != snet
|| tp
->node
!= snode
)
511 (void)printf(" [addr=%s]",
512 ataddr_string(EXTRACT_16BITS(&tp
->net
), tp
->node
));
517 static const struct atNBPtuple
*
518 nbp_name_print(const struct atNBPtuple
*tp
, register const u_char
*ep
)
520 register const char *cp
= (const char *)tp
+ nbpTupleSize
;
526 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
529 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
532 if ((cp
= print_cstring(cp
, ep
)) != NULL
)
536 return ((const struct atNBPtuple
*)cp
);
540 #define HASHNAMESIZE 4096
545 struct hnamemem
*nxt
;
548 static struct hnamemem hnametable
[HASHNAMESIZE
];
551 ataddr_string(u_short atnet
, u_char athost
)
553 register struct hnamemem
*tp
, *tp2
;
554 register int i
= (atnet
<< 8) | athost
;
555 char nambuf
[MAXHOSTNAMELEN
+ 20];
556 static int first
= 1;
560 * if this is the first call, see if there's an AppleTalk
561 * number to name map file.
563 if (first
&& (first
= 0, !nflag
)
564 && (fp
= fopen("/etc/atalk.names", "r"))) {
568 while (fgets(line
, sizeof(line
), fp
)) {
569 if (line
[0] == '\n' || line
[0] == 0 || line
[0] == '#')
571 if (sscanf(line
, "%d.%d.%d %256s", &i1
, &i2
, &i3
,
573 /* got a hostname. */
574 i3
|= ((i1
<< 8) | i2
) << 8;
575 else if (sscanf(line
, "%d.%d %256s", &i1
, &i2
,
578 i3
= (((i1
<< 8) | i2
) << 8) | 255;
582 for (tp
= &hnametable
[i3
& (HASHNAMESIZE
-1)];
583 tp
->nxt
; tp
= tp
->nxt
)
586 tp
->nxt
= newhnamemem();
587 tp
->name
= strdup(nambuf
);
592 for (tp
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
596 /* didn't have the node name -- see if we've got the net name */
598 for (tp2
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp2
->nxt
; tp2
= tp2
->nxt
)
599 if (tp2
->addr
== i
) {
600 tp
->addr
= (atnet
<< 8) | athost
;
601 tp
->nxt
= newhnamemem();
602 (void)snprintf(nambuf
, sizeof(nambuf
), "%s.%d",
604 tp
->name
= strdup(nambuf
);
608 tp
->addr
= (atnet
<< 8) | athost
;
609 tp
->nxt
= newhnamemem();
611 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d.%d",
612 atnet
>> 8, atnet
& 0xff, athost
);
614 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d", atnet
>> 8,
616 tp
->name
= strdup(nambuf
);
621 static struct tok skt2str
[] = {
622 { rtmpSkt
, "rtmp" }, /* routing table maintenance */
623 { nbpSkt
, "nis" }, /* name info socket */
624 { echoSkt
, "echo" }, /* AppleTalk echo protocol */
625 { zipSkt
, "zip" }, /* zone info protocol */
630 ddpskt_string(register int skt
)
635 (void)snprintf(buf
, sizeof(buf
), "%d", skt
);
638 return (tok2str(skt2str
, "%d", skt
));