]>
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.58 2000-09-28 06:42:55 guy Exp $ (LBL)";
33 #include <sys/param.h>
35 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
49 #include "interface.h"
50 #include "addrtoname.h"
51 #include "ethertype.h"
52 #include "extract.h" /* must come after interface.h */
53 #include "appletalk.h"
56 static struct tok type2str
[] = {
58 { ddpRTMPrequest
, "rtmpReq" },
67 u_int16_t htype
, ptype
;
68 u_int8_t halen
, palen
;
76 static char tstr
[] = "[|atalk]";
78 static void atp_print(const struct atATP
*, u_int
);
79 static void atp_bitmap_print(u_char
);
80 static void nbp_print(const struct atNBP
*, u_int
, u_short
, u_char
, u_char
);
81 static const char *print_cstring(const char *, const u_char
*);
82 static const struct atNBPtuple
*nbp_tuple_print(const struct atNBPtuple
*,
84 u_short
, u_char
, u_char
);
85 static const struct atNBPtuple
*nbp_name_print(const struct atNBPtuple
*,
87 static const char *ataddr_string(u_short
, u_char
);
88 static void ddp_print(const u_char
*, u_int
, int, u_short
, u_char
, u_char
);
89 static const char *ddpskt_string(int);
92 * Print AppleTalk Datagram Delivery Protocol packets.
95 atalk_print(register const u_char
*bp
, u_int length
)
97 register const struct LAP
*lp
;
98 register const struct atDDP
*dp
;
99 register const struct atShortDDP
*sdp
;
102 lp
= (struct LAP
*)bp
;
104 length
-= sizeof(*lp
);
108 if (length
< ddpSSize
) {
109 (void)printf(" [|sddp %d]", length
);
112 sdp
= (const struct atShortDDP
*)bp
;
114 ataddr_string(0, lp
->src
), ddpskt_string(sdp
->srcSkt
));
116 ataddr_string(0, lp
->dst
), ddpskt_string(sdp
->dstSkt
));
119 ddp_print(bp
, length
, sdp
->type
, 0, lp
->src
, sdp
->srcSkt
);
123 if (length
< ddpSize
) {
124 (void)printf(" [|ddp %d]", length
);
127 dp
= (const struct atDDP
*)bp
;
128 snet
= EXTRACT_16BITS(&dp
->srcNet
);
129 printf("%s.%s", ataddr_string(snet
, dp
->srcNode
),
130 ddpskt_string(dp
->srcSkt
));
132 ataddr_string(EXTRACT_16BITS(&dp
->dstNet
), dp
->dstNode
),
133 ddpskt_string(dp
->dstSkt
));
136 ddp_print(bp
, length
, dp
->type
, snet
, dp
->srcNode
, dp
->srcSkt
);
141 klap_print(bp
, length
);
146 printf("%d > %d at-lap#%d %d",
147 lp
->src
, lp
->dst
, lp
->type
, length
);
152 /* XXX should probably pass in the snap header and do checks like arp_print() */
154 aarp_print(register const u_char
*bp
, u_int length
)
156 register const struct aarp
*ap
;
158 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
161 ap
= (const struct aarp
*)bp
;
162 if (ntohs(ap
->htype
) == 1 && ntohs(ap
->ptype
) == ETHERTYPE_ATALK
&&
163 ap
->halen
== 6 && ap
->palen
== 4 )
164 switch (ntohs(ap
->op
)) {
166 case 1: /* request */
167 (void)printf("who-has %s tell %s",
168 AT(pdaddr
), AT(psaddr
));
171 case 2: /* response */
172 (void)printf("reply %s is-at %s",
173 AT(pdaddr
), etheraddr_string(ap
->hdaddr
));
176 case 3: /* probe (oy!) */
177 (void)printf("probe %s tell %s",
178 AT(pdaddr
), AT(psaddr
));
181 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
182 length
, ntohs(ap
->op
), ntohs(ap
->htype
), ntohs(ap
->ptype
),
183 ap
->halen
, ap
->palen
);
187 ddp_print(register const u_char
*bp
, register u_int length
, register int t
,
188 register u_short snet
, register u_char snode
, u_char skt
)
194 nbp_print((const struct atNBP
*)bp
, length
, snet
, snode
, skt
);
198 atp_print((const struct atATP
*)bp
, length
);
202 (void)printf(" at-%s %d", tok2str(type2str
, NULL
, t
), length
);
208 atp_print(register const struct atATP
*ap
, u_int length
)
213 if ((const u_char
*)(ap
+ 1) > snapend
) {
214 /* Just bail if we don't have the whole chunk. */
218 length
-= sizeof(*ap
);
219 switch (ap
->control
& 0xc0) {
222 (void)printf(" atp-req%s %d",
223 ap
->control
& atpXO
? " " : "*",
224 EXTRACT_16BITS(&ap
->transID
));
226 atp_bitmap_print(ap
->bitmap
);
229 (void)printf(" [len=%d]", length
);
231 switch (ap
->control
& (atpEOM
|atpSTS
)) {
233 (void)printf(" [EOM]");
236 (void)printf(" [STS]");
239 (void)printf(" [EOM,STS]");
245 (void)printf(" atp-resp%s%d:%d (%d)",
246 ap
->control
& atpEOM
? "*" : " ",
247 EXTRACT_16BITS(&ap
->transID
), ap
->bitmap
, length
);
248 switch (ap
->control
& (atpXO
|atpSTS
)) {
250 (void)printf(" [XO]");
253 (void)printf(" [STS]");
256 (void)printf(" [XO,STS]");
262 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap
->transID
));
264 atp_bitmap_print(ap
->bitmap
);
266 /* length should be zero */
268 (void)printf(" [len=%d]", length
);
270 /* there shouldn't be any control flags */
271 if (ap
->control
& (atpXO
|atpEOM
|atpSTS
)) {
273 if (ap
->control
& atpXO
) {
274 (void)printf("%cXO", c
);
277 if (ap
->control
& atpEOM
) {
278 (void)printf("%cEOM", c
);
281 if (ap
->control
& atpSTS
) {
282 (void)printf("%cSTS", c
);
290 (void)printf(" atp-0x%x %d (%d)", ap
->control
,
291 EXTRACT_16BITS(&ap
->transID
), length
);
294 data
= EXTRACT_32BITS(&ap
->userData
);
296 (void)printf(" 0x%x", data
);
300 atp_bitmap_print(register u_char bm
)
306 * The '& 0xff' below is needed for compilers that want to sign
307 * extend a u_char, which is the case with the Ultrix compiler.
308 * (gcc is smart enough to eliminate it, at least on the Sparc).
310 if ((bm
+ 1) & (bm
& 0xff)) {
312 for (i
= 0; bm
; ++i
) {
314 (void)printf("%c%d", c
, i
);
324 (void)printf("<0-%d>", i
- 1);
331 nbp_print(register const struct atNBP
*np
, u_int length
, register u_short snet
,
332 register u_char snode
, register u_char skt
)
334 register const struct atNBPtuple
*tp
=
335 (struct atNBPtuple
*)((u_char
*)np
+ nbpHeaderSize
);
339 length
-= nbpHeaderSize
;
341 /* must be room for at least one tuple */
342 (void)printf(" truncated-nbp %d", length
+ nbpHeaderSize
);
345 /* ep points to end of available data */
347 if ((const u_char
*)tp
> ep
) {
351 switch (i
= np
->control
& 0xf0) {
355 (void)printf(i
== nbpLkUp
? " nbp-lkup %d:":" nbp-brRq %d:",
357 if ((const u_char
*)(tp
+ 1) > ep
) {
361 (void)nbp_name_print(tp
, ep
);
363 * look for anomalies: the spec says there can only
364 * be one tuple, the address must match the source
365 * address and the enumerator should be zero.
367 if ((np
->control
& 0xf) != 1)
368 (void)printf(" [ntup=%d]", np
->control
& 0xf);
370 (void)printf(" [enum=%d]", tp
->enumerator
);
371 if (EXTRACT_16BITS(&tp
->net
) != snet
||
372 tp
->node
!= snode
|| tp
->skt
!= skt
)
373 (void)printf(" [addr=%s.%d]",
374 ataddr_string(EXTRACT_16BITS(&tp
->net
),
379 (void)printf(" nbp-reply %d:", np
->id
);
381 /* print each of the tuples in the reply */
382 for (i
= np
->control
& 0xf; --i
>= 0 && tp
; )
383 tp
= nbp_tuple_print(tp
, ep
, snet
, snode
, skt
);
387 (void)printf(" nbp-0x%x %d (%d)", np
->control
, np
->id
,
393 /* print a counted string */
395 print_cstring(register const char *cp
, register const u_char
*ep
)
397 register u_int length
;
399 if (cp
>= (const char *)ep
) {
405 /* Spec says string can be at most 32 bytes long */
407 (void)printf("[len=%u]", length
);
410 while ((int)--length
>= 0) {
411 if (cp
>= (char *)ep
) {
420 static const struct atNBPtuple
*
421 nbp_tuple_print(register const struct atNBPtuple
*tp
,
422 register const u_char
*ep
,
423 register u_short snet
, register u_char snode
,
426 register const struct atNBPtuple
*tpn
;
428 if ((const u_char
*)(tp
+ 1) > ep
) {
432 tpn
= nbp_name_print(tp
, ep
);
434 /* if the enumerator isn't 1, print it */
435 if (tp
->enumerator
!= 1)
436 (void)printf("(%d)", tp
->enumerator
);
438 /* if the socket doesn't match the src socket, print it */
440 (void)printf(" %d", tp
->skt
);
442 /* if the address doesn't match the src address, it's an anomaly */
443 if (EXTRACT_16BITS(&tp
->net
) != snet
|| tp
->node
!= snode
)
444 (void)printf(" [addr=%s]",
445 ataddr_string(EXTRACT_16BITS(&tp
->net
), tp
->node
));
450 static const struct atNBPtuple
*
451 nbp_name_print(const struct atNBPtuple
*tp
, register const u_char
*ep
)
453 register const char *cp
= (const char *)tp
+ nbpTupleSize
;
459 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
462 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
465 if ((cp
= print_cstring(cp
, ep
)) != NULL
)
469 return ((const struct atNBPtuple
*)cp
);
473 #define HASHNAMESIZE 4096
478 struct hnamemem
*nxt
;
481 static struct hnamemem hnametable
[HASHNAMESIZE
];
484 ataddr_string(u_short atnet
, u_char athost
)
486 register struct hnamemem
*tp
, *tp2
;
487 register int i
= (atnet
<< 8) | athost
;
489 static int first
= 1;
493 * if this is the first call, see if there's an AppleTalk
494 * number to name map file.
496 if (first
&& (first
= 0, !nflag
)
497 && (fp
= fopen("/etc/atalk.names", "r"))) {
501 while (fgets(line
, sizeof(line
), fp
)) {
502 if (line
[0] == '\n' || line
[0] == 0 || line
[0] == '#')
504 if (sscanf(line
, "%d.%d.%d %256s", &i1
, &i2
, &i3
,
506 /* got a hostname. */
507 i3
|= ((i1
<< 8) | i2
) << 8;
508 else if (sscanf(line
, "%d.%d %256s", &i1
, &i2
,
511 i3
= (((i1
<< 8) | i2
) << 8) | 255;
515 for (tp
= &hnametable
[i3
& (HASHNAMESIZE
-1)];
516 tp
->nxt
; tp
= tp
->nxt
)
519 tp
->nxt
= newhnamemem();
520 tp
->name
= savestr(nambuf
);
525 for (tp
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
529 /* didn't have the node name -- see if we've got the net name */
531 for (tp2
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp2
->nxt
; tp2
= tp2
->nxt
)
532 if (tp2
->addr
== i
) {
533 tp
->addr
= (atnet
<< 8) | athost
;
534 tp
->nxt
= newhnamemem();
535 (void)snprintf(nambuf
, sizeof(nambuf
), "%s.%d",
537 tp
->name
= savestr(nambuf
);
541 tp
->addr
= (atnet
<< 8) | athost
;
542 tp
->nxt
= newhnamemem();
544 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d.%d",
545 atnet
>> 8, atnet
& 0xff, athost
);
547 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d", atnet
>> 8,
549 tp
->name
= savestr(nambuf
);
554 static struct tok skt2str
[] = {
555 { rtmpSkt
, "rtmp" }, /* routing table maintenance */
556 { nbpSkt
, "nis" }, /* name info socket */
557 { echoSkt
, "echo" }, /* AppleTalk echo protocol */
558 { zipSkt
, "zip" }, /* zone info protocol */
563 ddpskt_string(register int skt
)
568 (void)snprintf(buf
, sizeof(buf
), "%d", skt
);
571 return (tok2str(skt2str
, "%d", skt
));