]>
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.65 2001-01-20 07:22:23 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 */
44 #include "interface.h"
45 #include "addrtoname.h"
46 #include "ethertype.h"
47 #include "extract.h" /* must come after interface.h */
48 #include "appletalk.h"
50 static struct tok type2str
[] = {
52 { ddpRTMPrequest
, "rtmpReq" },
61 u_int16_t htype
, ptype
;
62 u_int8_t halen
, palen
;
70 static char tstr
[] = "[|atalk]";
72 static void atp_print(const struct atATP
*, u_int
);
73 static void atp_bitmap_print(u_char
);
74 static void nbp_print(const struct atNBP
*, u_int
, u_short
, u_char
, u_char
);
75 static const char *print_cstring(const char *, const u_char
*);
76 static const struct atNBPtuple
*nbp_tuple_print(const struct atNBPtuple
*,
78 u_short
, u_char
, u_char
);
79 static const struct atNBPtuple
*nbp_name_print(const struct atNBPtuple
*,
81 static const char *ataddr_string(u_short
, u_char
);
82 static void ddp_print(const u_char
*, u_int
, int, u_short
, u_char
, u_char
);
83 static const char *ddpskt_string(int);
86 * Print AppleTalk LLAP packets.
89 llap_print(register const u_char
*bp
, u_int length
)
91 register const struct LAP
*lp
;
92 register const struct atDDP
*dp
;
93 register const struct atShortDDP
*sdp
;
96 lp
= (struct LAP
*)bp
;
98 length
-= sizeof(*lp
);
102 if (length
< ddpSSize
) {
103 (void)printf(" [|sddp %d]", length
);
106 sdp
= (const struct atShortDDP
*)bp
;
108 ataddr_string(0, lp
->src
), ddpskt_string(sdp
->srcSkt
));
110 ataddr_string(0, lp
->dst
), ddpskt_string(sdp
->dstSkt
));
113 ddp_print(bp
, length
, sdp
->type
, 0, lp
->src
, sdp
->srcSkt
);
117 if (length
< ddpSize
) {
118 (void)printf(" [|ddp %d]", length
);
121 dp
= (const struct atDDP
*)bp
;
122 snet
= EXTRACT_16BITS(&dp
->srcNet
);
123 printf("%s.%s", ataddr_string(snet
, dp
->srcNode
),
124 ddpskt_string(dp
->srcSkt
));
126 ataddr_string(EXTRACT_16BITS(&dp
->dstNet
), dp
->dstNode
),
127 ddpskt_string(dp
->dstSkt
));
130 ddp_print(bp
, length
, dp
->type
, snet
, dp
->srcNode
, dp
->srcSkt
);
135 klap_print(bp
, length
);
140 printf("%d > %d at-lap#%d %d",
141 lp
->src
, lp
->dst
, lp
->type
, length
);
147 * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
148 * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
152 atalk_print(register const u_char
*bp
, u_int length
)
154 register const struct atDDP
*dp
;
157 if (length
< ddpSize
) {
158 (void)printf(" [|ddp %d]", length
);
161 dp
= (const struct atDDP
*)bp
;
162 snet
= EXTRACT_16BITS(&dp
->srcNet
);
163 printf("%s.%s", ataddr_string(snet
, dp
->srcNode
),
164 ddpskt_string(dp
->srcSkt
));
166 ataddr_string(EXTRACT_16BITS(&dp
->dstNet
), dp
->dstNode
),
167 ddpskt_string(dp
->dstSkt
));
170 ddp_print(bp
, length
, dp
->type
, snet
, dp
->srcNode
, dp
->srcSkt
);
173 /* XXX should probably pass in the snap header and do checks like arp_print() */
175 aarp_print(register const u_char
*bp
, u_int length
)
177 register const struct aarp
*ap
;
179 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
182 ap
= (const struct aarp
*)bp
;
183 if (ntohs(ap
->htype
) == 1 && ntohs(ap
->ptype
) == ETHERTYPE_ATALK
&&
184 ap
->halen
== 6 && ap
->palen
== 4 )
185 switch (ntohs(ap
->op
)) {
187 case 1: /* request */
188 (void)printf("who-has %s tell %s",
189 AT(pdaddr
), AT(psaddr
));
192 case 2: /* response */
193 (void)printf("reply %s is-at %s",
194 AT(pdaddr
), etheraddr_string(ap
->hdaddr
));
197 case 3: /* probe (oy!) */
198 (void)printf("probe %s tell %s",
199 AT(pdaddr
), AT(psaddr
));
202 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
203 length
, ntohs(ap
->op
), ntohs(ap
->htype
), ntohs(ap
->ptype
),
204 ap
->halen
, ap
->palen
);
208 * Print AppleTalk Datagram Delivery Protocol packets.
211 ddp_print(register const u_char
*bp
, register u_int length
, register int t
,
212 register u_short snet
, register u_char snode
, u_char skt
)
218 nbp_print((const struct atNBP
*)bp
, length
, snet
, snode
, skt
);
222 atp_print((const struct atATP
*)bp
, length
);
226 (void)printf(" at-%s %d", tok2str(type2str
, NULL
, t
), length
);
232 atp_print(register const struct atATP
*ap
, u_int length
)
237 if ((const u_char
*)(ap
+ 1) > snapend
) {
238 /* Just bail if we don't have the whole chunk. */
242 length
-= sizeof(*ap
);
243 switch (ap
->control
& 0xc0) {
246 (void)printf(" atp-req%s %d",
247 ap
->control
& atpXO
? " " : "*",
248 EXTRACT_16BITS(&ap
->transID
));
250 atp_bitmap_print(ap
->bitmap
);
253 (void)printf(" [len=%d]", length
);
255 switch (ap
->control
& (atpEOM
|atpSTS
)) {
257 (void)printf(" [EOM]");
260 (void)printf(" [STS]");
263 (void)printf(" [EOM,STS]");
269 (void)printf(" atp-resp%s%d:%d (%d)",
270 ap
->control
& atpEOM
? "*" : " ",
271 EXTRACT_16BITS(&ap
->transID
), ap
->bitmap
, length
);
272 switch (ap
->control
& (atpXO
|atpSTS
)) {
274 (void)printf(" [XO]");
277 (void)printf(" [STS]");
280 (void)printf(" [XO,STS]");
286 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap
->transID
));
288 atp_bitmap_print(ap
->bitmap
);
290 /* length should be zero */
292 (void)printf(" [len=%d]", length
);
294 /* there shouldn't be any control flags */
295 if (ap
->control
& (atpXO
|atpEOM
|atpSTS
)) {
297 if (ap
->control
& atpXO
) {
298 (void)printf("%cXO", c
);
301 if (ap
->control
& atpEOM
) {
302 (void)printf("%cEOM", c
);
305 if (ap
->control
& atpSTS
) {
306 (void)printf("%cSTS", c
);
314 (void)printf(" atp-0x%x %d (%d)", ap
->control
,
315 EXTRACT_16BITS(&ap
->transID
), length
);
318 data
= EXTRACT_32BITS(&ap
->userData
);
320 (void)printf(" 0x%x", data
);
324 atp_bitmap_print(register u_char bm
)
330 * The '& 0xff' below is needed for compilers that want to sign
331 * extend a u_char, which is the case with the Ultrix compiler.
332 * (gcc is smart enough to eliminate it, at least on the Sparc).
334 if ((bm
+ 1) & (bm
& 0xff)) {
336 for (i
= 0; bm
; ++i
) {
338 (void)printf("%c%d", c
, i
);
348 (void)printf("<0-%d>", i
- 1);
355 nbp_print(register const struct atNBP
*np
, u_int length
, register u_short snet
,
356 register u_char snode
, register u_char skt
)
358 register const struct atNBPtuple
*tp
=
359 (struct atNBPtuple
*)((u_char
*)np
+ nbpHeaderSize
);
363 length
-= nbpHeaderSize
;
365 /* must be room for at least one tuple */
366 (void)printf(" truncated-nbp %d", length
+ nbpHeaderSize
);
369 /* ep points to end of available data */
371 if ((const u_char
*)tp
> ep
) {
375 switch (i
= np
->control
& 0xf0) {
379 (void)printf(i
== nbpLkUp
? " nbp-lkup %d:":" nbp-brRq %d:",
381 if ((const u_char
*)(tp
+ 1) > ep
) {
385 (void)nbp_name_print(tp
, ep
);
387 * look for anomalies: the spec says there can only
388 * be one tuple, the address must match the source
389 * address and the enumerator should be zero.
391 if ((np
->control
& 0xf) != 1)
392 (void)printf(" [ntup=%d]", np
->control
& 0xf);
394 (void)printf(" [enum=%d]", tp
->enumerator
);
395 if (EXTRACT_16BITS(&tp
->net
) != snet
||
396 tp
->node
!= snode
|| tp
->skt
!= skt
)
397 (void)printf(" [addr=%s.%d]",
398 ataddr_string(EXTRACT_16BITS(&tp
->net
),
403 (void)printf(" nbp-reply %d:", np
->id
);
405 /* print each of the tuples in the reply */
406 for (i
= np
->control
& 0xf; --i
>= 0 && tp
; )
407 tp
= nbp_tuple_print(tp
, ep
, snet
, snode
, skt
);
411 (void)printf(" nbp-0x%x %d (%d)", np
->control
, np
->id
,
417 /* print a counted string */
419 print_cstring(register const char *cp
, register const u_char
*ep
)
421 register u_int length
;
423 if (cp
>= (const char *)ep
) {
429 /* Spec says string can be at most 32 bytes long */
431 (void)printf("[len=%u]", length
);
434 while ((int)--length
>= 0) {
435 if (cp
>= (char *)ep
) {
444 static const struct atNBPtuple
*
445 nbp_tuple_print(register const struct atNBPtuple
*tp
,
446 register const u_char
*ep
,
447 register u_short snet
, register u_char snode
,
450 register const struct atNBPtuple
*tpn
;
452 if ((const u_char
*)(tp
+ 1) > ep
) {
456 tpn
= nbp_name_print(tp
, ep
);
458 /* if the enumerator isn't 1, print it */
459 if (tp
->enumerator
!= 1)
460 (void)printf("(%d)", tp
->enumerator
);
462 /* if the socket doesn't match the src socket, print it */
464 (void)printf(" %d", tp
->skt
);
466 /* if the address doesn't match the src address, it's an anomaly */
467 if (EXTRACT_16BITS(&tp
->net
) != snet
|| tp
->node
!= snode
)
468 (void)printf(" [addr=%s]",
469 ataddr_string(EXTRACT_16BITS(&tp
->net
), tp
->node
));
474 static const struct atNBPtuple
*
475 nbp_name_print(const struct atNBPtuple
*tp
, register const u_char
*ep
)
477 register const char *cp
= (const char *)tp
+ nbpTupleSize
;
483 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
486 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
489 if ((cp
= print_cstring(cp
, ep
)) != NULL
)
493 return ((const struct atNBPtuple
*)cp
);
497 #define HASHNAMESIZE 4096
502 struct hnamemem
*nxt
;
505 static struct hnamemem hnametable
[HASHNAMESIZE
];
508 ataddr_string(u_short atnet
, u_char athost
)
510 register struct hnamemem
*tp
, *tp2
;
511 register int i
= (atnet
<< 8) | athost
;
512 char nambuf
[MAXHOSTNAMELEN
+ 20];
513 static int first
= 1;
517 * if this is the first call, see if there's an AppleTalk
518 * number to name map file.
520 if (first
&& (first
= 0, !nflag
)
521 && (fp
= fopen("/etc/atalk.names", "r"))) {
525 while (fgets(line
, sizeof(line
), fp
)) {
526 if (line
[0] == '\n' || line
[0] == 0 || line
[0] == '#')
528 if (sscanf(line
, "%d.%d.%d %256s", &i1
, &i2
, &i3
,
530 /* got a hostname. */
531 i3
|= ((i1
<< 8) | i2
) << 8;
532 else if (sscanf(line
, "%d.%d %256s", &i1
, &i2
,
535 i3
= (((i1
<< 8) | i2
) << 8) | 255;
539 for (tp
= &hnametable
[i3
& (HASHNAMESIZE
-1)];
540 tp
->nxt
; tp
= tp
->nxt
)
543 tp
->nxt
= newhnamemem();
544 tp
->name
= strdup(nambuf
);
549 for (tp
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
553 /* didn't have the node name -- see if we've got the net name */
555 for (tp2
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp2
->nxt
; tp2
= tp2
->nxt
)
556 if (tp2
->addr
== i
) {
557 tp
->addr
= (atnet
<< 8) | athost
;
558 tp
->nxt
= newhnamemem();
559 (void)snprintf(nambuf
, sizeof(nambuf
), "%s.%d",
561 tp
->name
= strdup(nambuf
);
565 tp
->addr
= (atnet
<< 8) | athost
;
566 tp
->nxt
= newhnamemem();
568 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d.%d",
569 atnet
>> 8, atnet
& 0xff, athost
);
571 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d", atnet
>> 8,
573 tp
->name
= strdup(nambuf
);
578 static struct tok skt2str
[] = {
579 { rtmpSkt
, "rtmp" }, /* routing table maintenance */
580 { nbpSkt
, "nis" }, /* name info socket */
581 { echoSkt
, "echo" }, /* AppleTalk echo protocol */
582 { zipSkt
, "zip" }, /* zone info protocol */
587 ddpskt_string(register int skt
)
592 (void)snprintf(buf
, sizeof(buf
), "%d", skt
);
595 return (tok2str(skt2str
, "%d", skt
));