]>
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.67 2001-07-05 18:54:14 guy Exp $ (LBL)";
33 #include <sys/param.h>
35 #include <sys/socket.h>
37 #include <netinet/in.h>
42 #include <netdb.h> /* for MAXHOSTNAMELEN on some platforms */
45 #include "interface.h"
46 #include "addrtoname.h"
47 #include "ethertype.h"
48 #include "extract.h" /* must come after interface.h */
49 #include "appletalk.h"
51 static struct tok type2str
[] = {
53 { ddpRTMPrequest
, "rtmpReq" },
62 u_int16_t htype
, ptype
;
63 u_int8_t halen
, palen
;
71 static char tstr
[] = "[|atalk]";
73 static void atp_print(const struct atATP
*, u_int
);
74 static void atp_bitmap_print(u_char
);
75 static void nbp_print(const struct atNBP
*, u_int
, u_short
, u_char
, u_char
);
76 static const char *print_cstring(const char *, const u_char
*);
77 static const struct atNBPtuple
*nbp_tuple_print(const struct atNBPtuple
*,
79 u_short
, u_char
, u_char
);
80 static const struct atNBPtuple
*nbp_name_print(const struct atNBPtuple
*,
82 static const char *ataddr_string(u_short
, u_char
);
83 static void ddp_print(const u_char
*, u_int
, int, u_short
, u_char
, u_char
);
84 static const char *ddpskt_string(int);
87 * Print LLAP packets received on a physical LocalTalk interface.
90 ltalk_if_print(u_char
*user
, const struct pcap_pkthdr
*h
, const u_char
*p
)
92 snapend
= p
+ h
->caplen
;
95 llap_print(p
, h
->caplen
);
97 default_print(p
, h
->caplen
);
105 * Print AppleTalk LLAP packets.
108 llap_print(register const u_char
*bp
, u_int length
)
110 register const struct LAP
*lp
;
111 register const struct atDDP
*dp
;
112 register const struct atShortDDP
*sdp
;
115 lp
= (struct LAP
*)bp
;
117 length
-= sizeof(*lp
);
121 if (length
< ddpSSize
) {
122 (void)printf(" [|sddp %d]", length
);
125 sdp
= (const struct atShortDDP
*)bp
;
127 ataddr_string(0, lp
->src
), ddpskt_string(sdp
->srcSkt
));
129 ataddr_string(0, lp
->dst
), ddpskt_string(sdp
->dstSkt
));
132 ddp_print(bp
, length
, sdp
->type
, 0, lp
->src
, sdp
->srcSkt
);
136 if (length
< ddpSize
) {
137 (void)printf(" [|ddp %d]", length
);
140 dp
= (const struct atDDP
*)bp
;
141 snet
= EXTRACT_16BITS(&dp
->srcNet
);
142 printf("%s.%s", ataddr_string(snet
, dp
->srcNode
),
143 ddpskt_string(dp
->srcSkt
));
145 ataddr_string(EXTRACT_16BITS(&dp
->dstNet
), dp
->dstNode
),
146 ddpskt_string(dp
->dstSkt
));
149 ddp_print(bp
, length
, dp
->type
, snet
, dp
->srcNode
, dp
->srcSkt
);
154 klap_print(bp
, length
);
159 printf("%d > %d at-lap#%d %d",
160 lp
->src
, lp
->dst
, lp
->type
, length
);
166 * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
167 * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
171 atalk_print(register const u_char
*bp
, u_int length
)
173 register const struct atDDP
*dp
;
176 if (length
< ddpSize
) {
177 (void)printf(" [|ddp %d]", length
);
180 dp
= (const struct atDDP
*)bp
;
181 snet
= EXTRACT_16BITS(&dp
->srcNet
);
182 printf("%s.%s", ataddr_string(snet
, dp
->srcNode
),
183 ddpskt_string(dp
->srcSkt
));
185 ataddr_string(EXTRACT_16BITS(&dp
->dstNet
), dp
->dstNode
),
186 ddpskt_string(dp
->dstSkt
));
189 ddp_print(bp
, length
, dp
->type
, snet
, dp
->srcNode
, dp
->srcSkt
);
192 /* XXX should probably pass in the snap header and do checks like arp_print() */
194 aarp_print(register const u_char
*bp
, u_int length
)
196 register const struct aarp
*ap
;
198 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
201 ap
= (const struct aarp
*)bp
;
202 if (ntohs(ap
->htype
) == 1 && ntohs(ap
->ptype
) == ETHERTYPE_ATALK
&&
203 ap
->halen
== 6 && ap
->palen
== 4 )
204 switch (ntohs(ap
->op
)) {
206 case 1: /* request */
207 (void)printf("who-has %s tell %s",
208 AT(pdaddr
), AT(psaddr
));
211 case 2: /* response */
212 (void)printf("reply %s is-at %s",
213 AT(pdaddr
), etheraddr_string(ap
->hdaddr
));
216 case 3: /* probe (oy!) */
217 (void)printf("probe %s tell %s",
218 AT(pdaddr
), AT(psaddr
));
221 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
222 length
, ntohs(ap
->op
), ntohs(ap
->htype
), ntohs(ap
->ptype
),
223 ap
->halen
, ap
->palen
);
227 * Print AppleTalk Datagram Delivery Protocol packets.
230 ddp_print(register const u_char
*bp
, register u_int length
, register int t
,
231 register u_short snet
, register u_char snode
, u_char skt
)
237 nbp_print((const struct atNBP
*)bp
, length
, snet
, snode
, skt
);
241 atp_print((const struct atATP
*)bp
, length
);
245 (void)printf(" at-%s %d", tok2str(type2str
, NULL
, t
), length
);
251 atp_print(register const struct atATP
*ap
, u_int length
)
256 if ((const u_char
*)(ap
+ 1) > snapend
) {
257 /* Just bail if we don't have the whole chunk. */
261 length
-= sizeof(*ap
);
262 switch (ap
->control
& 0xc0) {
265 (void)printf(" atp-req%s %d",
266 ap
->control
& atpXO
? " " : "*",
267 EXTRACT_16BITS(&ap
->transID
));
269 atp_bitmap_print(ap
->bitmap
);
272 (void)printf(" [len=%d]", length
);
274 switch (ap
->control
& (atpEOM
|atpSTS
)) {
276 (void)printf(" [EOM]");
279 (void)printf(" [STS]");
282 (void)printf(" [EOM,STS]");
288 (void)printf(" atp-resp%s%d:%d (%d)",
289 ap
->control
& atpEOM
? "*" : " ",
290 EXTRACT_16BITS(&ap
->transID
), ap
->bitmap
, length
);
291 switch (ap
->control
& (atpXO
|atpSTS
)) {
293 (void)printf(" [XO]");
296 (void)printf(" [STS]");
299 (void)printf(" [XO,STS]");
305 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap
->transID
));
307 atp_bitmap_print(ap
->bitmap
);
309 /* length should be zero */
311 (void)printf(" [len=%d]", length
);
313 /* there shouldn't be any control flags */
314 if (ap
->control
& (atpXO
|atpEOM
|atpSTS
)) {
316 if (ap
->control
& atpXO
) {
317 (void)printf("%cXO", c
);
320 if (ap
->control
& atpEOM
) {
321 (void)printf("%cEOM", c
);
324 if (ap
->control
& atpSTS
) {
325 (void)printf("%cSTS", c
);
333 (void)printf(" atp-0x%x %d (%d)", ap
->control
,
334 EXTRACT_16BITS(&ap
->transID
), length
);
337 data
= EXTRACT_32BITS(&ap
->userData
);
339 (void)printf(" 0x%x", data
);
343 atp_bitmap_print(register u_char bm
)
349 * The '& 0xff' below is needed for compilers that want to sign
350 * extend a u_char, which is the case with the Ultrix compiler.
351 * (gcc is smart enough to eliminate it, at least on the Sparc).
353 if ((bm
+ 1) & (bm
& 0xff)) {
355 for (i
= 0; bm
; ++i
) {
357 (void)printf("%c%d", c
, i
);
367 (void)printf("<0-%d>", i
- 1);
374 nbp_print(register const struct atNBP
*np
, u_int length
, register u_short snet
,
375 register u_char snode
, register u_char skt
)
377 register const struct atNBPtuple
*tp
=
378 (struct atNBPtuple
*)((u_char
*)np
+ nbpHeaderSize
);
382 length
-= nbpHeaderSize
;
384 /* must be room for at least one tuple */
385 (void)printf(" truncated-nbp %d", length
+ nbpHeaderSize
);
388 /* ep points to end of available data */
390 if ((const u_char
*)tp
> ep
) {
394 switch (i
= np
->control
& 0xf0) {
398 (void)printf(i
== nbpLkUp
? " nbp-lkup %d:":" nbp-brRq %d:",
400 if ((const u_char
*)(tp
+ 1) > ep
) {
404 (void)nbp_name_print(tp
, ep
);
406 * look for anomalies: the spec says there can only
407 * be one tuple, the address must match the source
408 * address and the enumerator should be zero.
410 if ((np
->control
& 0xf) != 1)
411 (void)printf(" [ntup=%d]", np
->control
& 0xf);
413 (void)printf(" [enum=%d]", tp
->enumerator
);
414 if (EXTRACT_16BITS(&tp
->net
) != snet
||
415 tp
->node
!= snode
|| tp
->skt
!= skt
)
416 (void)printf(" [addr=%s.%d]",
417 ataddr_string(EXTRACT_16BITS(&tp
->net
),
422 (void)printf(" nbp-reply %d:", np
->id
);
424 /* print each of the tuples in the reply */
425 for (i
= np
->control
& 0xf; --i
>= 0 && tp
; )
426 tp
= nbp_tuple_print(tp
, ep
, snet
, snode
, skt
);
430 (void)printf(" nbp-0x%x %d (%d)", np
->control
, np
->id
,
436 /* print a counted string */
438 print_cstring(register const char *cp
, register const u_char
*ep
)
440 register u_int length
;
442 if (cp
>= (const char *)ep
) {
448 /* Spec says string can be at most 32 bytes long */
450 (void)printf("[len=%u]", length
);
453 while ((int)--length
>= 0) {
454 if (cp
>= (char *)ep
) {
463 static const struct atNBPtuple
*
464 nbp_tuple_print(register const struct atNBPtuple
*tp
,
465 register const u_char
*ep
,
466 register u_short snet
, register u_char snode
,
469 register const struct atNBPtuple
*tpn
;
471 if ((const u_char
*)(tp
+ 1) > ep
) {
475 tpn
= nbp_name_print(tp
, ep
);
477 /* if the enumerator isn't 1, print it */
478 if (tp
->enumerator
!= 1)
479 (void)printf("(%d)", tp
->enumerator
);
481 /* if the socket doesn't match the src socket, print it */
483 (void)printf(" %d", tp
->skt
);
485 /* if the address doesn't match the src address, it's an anomaly */
486 if (EXTRACT_16BITS(&tp
->net
) != snet
|| tp
->node
!= snode
)
487 (void)printf(" [addr=%s]",
488 ataddr_string(EXTRACT_16BITS(&tp
->net
), tp
->node
));
493 static const struct atNBPtuple
*
494 nbp_name_print(const struct atNBPtuple
*tp
, register const u_char
*ep
)
496 register const char *cp
= (const char *)tp
+ nbpTupleSize
;
502 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
505 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
508 if ((cp
= print_cstring(cp
, ep
)) != NULL
)
512 return ((const struct atNBPtuple
*)cp
);
516 #define HASHNAMESIZE 4096
521 struct hnamemem
*nxt
;
524 static struct hnamemem hnametable
[HASHNAMESIZE
];
527 ataddr_string(u_short atnet
, u_char athost
)
529 register struct hnamemem
*tp
, *tp2
;
530 register int i
= (atnet
<< 8) | athost
;
531 char nambuf
[MAXHOSTNAMELEN
+ 20];
532 static int first
= 1;
536 * if this is the first call, see if there's an AppleTalk
537 * number to name map file.
539 if (first
&& (first
= 0, !nflag
)
540 && (fp
= fopen("/etc/atalk.names", "r"))) {
544 while (fgets(line
, sizeof(line
), fp
)) {
545 if (line
[0] == '\n' || line
[0] == 0 || line
[0] == '#')
547 if (sscanf(line
, "%d.%d.%d %256s", &i1
, &i2
, &i3
,
549 /* got a hostname. */
550 i3
|= ((i1
<< 8) | i2
) << 8;
551 else if (sscanf(line
, "%d.%d %256s", &i1
, &i2
,
554 i3
= (((i1
<< 8) | i2
) << 8) | 255;
558 for (tp
= &hnametable
[i3
& (HASHNAMESIZE
-1)];
559 tp
->nxt
; tp
= tp
->nxt
)
562 tp
->nxt
= newhnamemem();
563 tp
->name
= strdup(nambuf
);
568 for (tp
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
572 /* didn't have the node name -- see if we've got the net name */
574 for (tp2
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp2
->nxt
; tp2
= tp2
->nxt
)
575 if (tp2
->addr
== i
) {
576 tp
->addr
= (atnet
<< 8) | athost
;
577 tp
->nxt
= newhnamemem();
578 (void)snprintf(nambuf
, sizeof(nambuf
), "%s.%d",
580 tp
->name
= strdup(nambuf
);
584 tp
->addr
= (atnet
<< 8) | athost
;
585 tp
->nxt
= newhnamemem();
587 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d.%d",
588 atnet
>> 8, atnet
& 0xff, athost
);
590 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d", atnet
>> 8,
592 tp
->name
= strdup(nambuf
);
597 static struct tok skt2str
[] = {
598 { rtmpSkt
, "rtmp" }, /* routing table maintenance */
599 { nbpSkt
, "nis" }, /* name info socket */
600 { echoSkt
, "echo" }, /* AppleTalk echo protocol */
601 { zipSkt
, "zip" }, /* zone info protocol */
606 ddpskt_string(register int skt
)
611 (void)snprintf(buf
, sizeof(buf
), "%d", skt
);
614 return (tok2str(skt2str
, "%d", skt
));