]>
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.50 1999-10-30 05:11:10 itojun Exp $ (LBL)";
29 #include <sys/param.h>
31 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/ip.h>
42 #include <netinet/ip_var.h>
43 #include <netinet/if_ether.h>
44 #include <netinet/udp.h>
45 #include <netinet/udp_var.h>
46 #include <netinet/tcp.h>
52 #include "interface.h"
53 #include "addrtoname.h"
54 #include "ethertype.h"
55 #include "extract.h" /* must come after interface.h */
56 #include "appletalk.h"
59 static struct tok type2str
[] = {
61 { ddpRTMPrequest
, "rtmpReq" },
79 static char tstr
[] = "[|atalk]";
81 static void atp_print(const struct atATP
*, u_int
);
82 static void atp_bitmap_print(u_char
);
83 static void nbp_print(const struct atNBP
*, u_int
, u_short
, u_char
, u_char
);
84 static const char *print_cstring(const char *, const u_char
*);
85 static const struct atNBPtuple
*nbp_tuple_print(const struct atNBPtuple
*,
87 u_short
, u_char
, u_char
);
88 static const struct atNBPtuple
*nbp_name_print(const struct atNBPtuple
*,
90 static const char *ataddr_string(u_short
, u_char
);
91 static void ddp_print(const u_char
*, u_int
, int, u_short
, u_char
, u_char
);
92 static const char *ddpskt_string(int);
95 * Print AppleTalk Datagram Delivery Protocol packets.
98 atalk_print(register const u_char
*bp
, u_int length
)
100 register const struct LAP
*lp
;
101 register const struct atDDP
*dp
;
102 register const struct atShortDDP
*sdp
;
105 lp
= (struct LAP
*)bp
;
107 length
-= sizeof(*lp
);
111 if (length
< ddpSSize
) {
112 (void)printf(" [|sddp %d]", length
);
115 sdp
= (const struct atShortDDP
*)bp
;
117 ataddr_string(0, lp
->src
), ddpskt_string(sdp
->srcSkt
));
119 ataddr_string(0, lp
->dst
), ddpskt_string(sdp
->dstSkt
));
122 ddp_print(bp
, length
, sdp
->type
, 0, lp
->src
, sdp
->srcSkt
);
126 if (length
< ddpSize
) {
127 (void)printf(" [|ddp %d]", length
);
130 dp
= (const struct atDDP
*)bp
;
131 snet
= EXTRACT_16BITS(&dp
->srcNet
);
132 printf("%s.%s", ataddr_string(snet
, dp
->srcNode
),
133 ddpskt_string(dp
->srcSkt
));
135 ataddr_string(EXTRACT_16BITS(&dp
->dstNet
), dp
->dstNode
),
136 ddpskt_string(dp
->dstSkt
));
139 ddp_print(bp
, length
, dp
->type
, snet
, dp
->srcNode
, dp
->srcSkt
);
144 klap_print(bp
, length
);
149 printf("%d > %d at-lap#%d %d",
150 lp
->src
, lp
->dst
, lp
->type
, length
);
155 /* XXX should probably pass in the snap header and do checks like arp_print() */
157 aarp_print(register const u_char
*bp
, u_int length
)
159 register const struct aarp
*ap
;
161 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
164 ap
= (const struct aarp
*)bp
;
165 if (ap
->htype
== 1 && ap
->ptype
== ETHERTYPE_ATALK
&&
166 ap
->halen
== 6 && ap
->palen
== 4 )
169 case 1: /* request */
170 (void)printf("who-has %s tell %s",
171 AT(pdaddr
), AT(psaddr
));
174 case 2: /* response */
175 (void)printf("reply %s is-at %s",
176 AT(pdaddr
), etheraddr_string(ap
->hdaddr
));
179 case 3: /* probe (oy!) */
180 (void)printf("probe %s tell %s",
181 AT(pdaddr
), AT(psaddr
));
184 (void)printf("len %d op %d htype %d ptype %#x halen %d palen %d",
185 length
, ap
->op
, ap
->htype
, ap
->ptype
, ap
->halen
, ap
->palen
);
189 ddp_print(register const u_char
*bp
, register u_int length
, register int t
,
190 register u_short snet
, register u_char snode
, u_char skt
)
196 nbp_print((const struct atNBP
*)bp
, length
, snet
, snode
, skt
);
200 atp_print((const struct atATP
*)bp
, length
);
204 (void)printf(" at-%s %d", tok2str(type2str
, NULL
, t
), length
);
210 atp_print(register const struct atATP
*ap
, u_int length
)
215 if ((const u_char
*)(ap
+ 1) > snapend
) {
216 /* Just bail if we don't have the whole chunk. */
220 length
-= sizeof(*ap
);
221 switch (ap
->control
& 0xc0) {
224 (void)printf(" atp-req%s %d",
225 ap
->control
& atpXO
? " " : "*",
226 EXTRACT_16BITS(&ap
->transID
));
228 atp_bitmap_print(ap
->bitmap
);
231 (void)printf(" [len=%d]", length
);
233 switch (ap
->control
& (atpEOM
|atpSTS
)) {
235 (void)printf(" [EOM]");
238 (void)printf(" [STS]");
241 (void)printf(" [EOM,STS]");
247 (void)printf(" atp-resp%s%d:%d (%d)",
248 ap
->control
& atpEOM
? "*" : " ",
249 EXTRACT_16BITS(&ap
->transID
), ap
->bitmap
, length
);
250 switch (ap
->control
& (atpXO
|atpSTS
)) {
252 (void)printf(" [XO]");
255 (void)printf(" [STS]");
258 (void)printf(" [XO,STS]");
264 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap
->transID
));
266 atp_bitmap_print(ap
->bitmap
);
268 /* length should be zero */
270 (void)printf(" [len=%d]", length
);
272 /* there shouldn't be any control flags */
273 if (ap
->control
& (atpXO
|atpEOM
|atpSTS
)) {
275 if (ap
->control
& atpXO
) {
276 (void)printf("%cXO", c
);
279 if (ap
->control
& atpEOM
) {
280 (void)printf("%cEOM", c
);
283 if (ap
->control
& atpSTS
) {
284 (void)printf("%cSTS", c
);
292 (void)printf(" atp-0x%x %d (%d)", ap
->control
,
293 EXTRACT_16BITS(&ap
->transID
), length
);
296 data
= EXTRACT_32BITS(&ap
->userData
);
298 (void)printf(" 0x%x", data
);
302 atp_bitmap_print(register u_char bm
)
308 * The '& 0xff' below is needed for compilers that want to sign
309 * extend a u_char, which is the case with the Ultrix compiler.
310 * (gcc is smart enough to eliminate it, at least on the Sparc).
312 if ((bm
+ 1) & (bm
& 0xff)) {
314 for (i
= 0; bm
; ++i
) {
316 (void)printf("%c%d", c
, i
);
326 (void)printf("<0-%d>", i
- 1);
333 nbp_print(register const struct atNBP
*np
, u_int length
, register u_short snet
,
334 register u_char snode
, register u_char skt
)
336 register const struct atNBPtuple
*tp
=
337 (struct atNBPtuple
*)((u_char
*)np
+ nbpHeaderSize
);
341 length
-= nbpHeaderSize
;
343 /* must be room for at least one tuple */
344 (void)printf(" truncated-nbp %d", length
+ nbpHeaderSize
);
347 /* ep points to end of available data */
349 if ((const u_char
*)tp
> ep
) {
353 switch (i
= np
->control
& 0xf0) {
357 (void)printf(i
== nbpLkUp
? " nbp-lkup %d:":" nbp-brRq %d:",
359 if ((const u_char
*)(tp
+ 1) > ep
) {
363 (void)nbp_name_print(tp
, ep
);
365 * look for anomalies: the spec says there can only
366 * be one tuple, the address must match the source
367 * address and the enumerator should be zero.
369 if ((np
->control
& 0xf) != 1)
370 (void)printf(" [ntup=%d]", np
->control
& 0xf);
372 (void)printf(" [enum=%d]", tp
->enumerator
);
373 if (EXTRACT_16BITS(&tp
->net
) != snet
||
374 tp
->node
!= snode
|| tp
->skt
!= skt
)
375 (void)printf(" [addr=%s.%d]",
376 ataddr_string(EXTRACT_16BITS(&tp
->net
),
381 (void)printf(" nbp-reply %d:", np
->id
);
383 /* print each of the tuples in the reply */
384 for (i
= np
->control
& 0xf; --i
>= 0 && tp
; )
385 tp
= nbp_tuple_print(tp
, ep
, snet
, snode
, skt
);
389 (void)printf(" nbp-0x%x %d (%d)", np
->control
, np
->id
,
395 /* print a counted string */
397 print_cstring(register const char *cp
, register const u_char
*ep
)
399 register u_int length
;
401 if (cp
>= (const char *)ep
) {
407 /* Spec says string can be at most 32 bytes long */
409 (void)printf("[len=%u]", length
);
412 while ((int)--length
>= 0) {
413 if (cp
>= (char *)ep
) {
422 static const struct atNBPtuple
*
423 nbp_tuple_print(register const struct atNBPtuple
*tp
,
424 register const u_char
*ep
,
425 register u_short snet
, register u_char snode
,
428 register const struct atNBPtuple
*tpn
;
430 if ((const u_char
*)(tp
+ 1) > ep
) {
434 tpn
= nbp_name_print(tp
, ep
);
436 /* if the enumerator isn't 1, print it */
437 if (tp
->enumerator
!= 1)
438 (void)printf("(%d)", tp
->enumerator
);
440 /* if the socket doesn't match the src socket, print it */
442 (void)printf(" %d", tp
->skt
);
444 /* if the address doesn't match the src address, it's an anomaly */
445 if (EXTRACT_16BITS(&tp
->net
) != snet
|| tp
->node
!= snode
)
446 (void)printf(" [addr=%s]",
447 ataddr_string(EXTRACT_16BITS(&tp
->net
), tp
->node
));
452 static const struct atNBPtuple
*
453 nbp_name_print(const struct atNBPtuple
*tp
, register const u_char
*ep
)
455 register const char *cp
= (const char *)tp
+ nbpTupleSize
;
461 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
464 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
467 if ((cp
= print_cstring(cp
, ep
)) != NULL
)
471 return ((const struct atNBPtuple
*)cp
);
475 #define HASHNAMESIZE 4096
480 struct hnamemem
*nxt
;
483 static struct hnamemem hnametable
[HASHNAMESIZE
];
486 ataddr_string(u_short atnet
, u_char athost
)
488 register struct hnamemem
*tp
, *tp2
;
489 register int i
= (atnet
<< 8) | athost
;
491 static int first
= 1;
495 * if this is the first call, see if there's an AppleTalk
496 * number to name map file.
498 if (first
&& (first
= 0, !nflag
)
499 && (fp
= fopen("/etc/atalk.names", "r"))) {
503 while (fgets(line
, sizeof(line
), fp
)) {
504 if (line
[0] == '\n' || line
[0] == 0 || line
[0] == '#')
506 if (sscanf(line
, "%d.%d.%d %s", &i1
, &i2
, &i3
,
508 /* got a hostname. */
509 i3
|= ((i1
<< 8) | i2
) << 8;
510 else if (sscanf(line
, "%d.%d %s", &i1
, &i2
,
513 i3
= (((i1
<< 8) | i2
) << 8) | 255;
517 for (tp
= &hnametable
[i3
& (HASHNAMESIZE
-1)];
518 tp
->nxt
; tp
= tp
->nxt
)
521 tp
->nxt
= newhnamemem();
522 tp
->name
= savestr(nambuf
);
527 for (tp
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
531 /* didn't have the node name -- see if we've got the net name */
533 for (tp2
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp2
->nxt
; tp2
= tp2
->nxt
)
534 if (tp2
->addr
== i
) {
535 tp
->addr
= (atnet
<< 8) | athost
;
536 tp
->nxt
= newhnamemem();
537 (void)sprintf(nambuf
, "%s.%d", tp2
->name
, athost
);
538 tp
->name
= savestr(nambuf
);
542 tp
->addr
= (atnet
<< 8) | athost
;
543 tp
->nxt
= newhnamemem();
545 (void)sprintf(nambuf
, "%d.%d.%d",
546 atnet
>> 8, atnet
& 0xff, athost
);
548 (void)sprintf(nambuf
, "%d.%d", atnet
>> 8, atnet
& 0xff);
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)sprintf(buf
, "%d", skt
);
571 return (tok2str(skt2str
, "%d", skt
));