]>
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.54 2000-04-02 23:50:48 itojun Exp $ (LBL)";
33 #include <sys/param.h>
35 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
45 #include <netinet/ip.h>
46 #include <netinet/ip_var.h>
47 #include <netinet/if_ether.h>
48 #include <netinet/udp.h>
49 #include <netinet/udp_var.h>
50 #include <netinet/tcp.h>
56 #include "interface.h"
57 #include "addrtoname.h"
58 #include "ethertype.h"
59 #include "extract.h" /* must come after interface.h */
60 #include "appletalk.h"
63 static struct tok type2str
[] = {
65 { ddpRTMPrequest
, "rtmpReq" },
74 u_int16_t htype
, ptype
;
75 u_int8_t halen
, palen
;
83 static char tstr
[] = "[|atalk]";
85 static void atp_print(const struct atATP
*, u_int
);
86 static void atp_bitmap_print(u_char
);
87 static void nbp_print(const struct atNBP
*, u_int
, u_short
, u_char
, u_char
);
88 static const char *print_cstring(const char *, const u_char
*);
89 static const struct atNBPtuple
*nbp_tuple_print(const struct atNBPtuple
*,
91 u_short
, u_char
, u_char
);
92 static const struct atNBPtuple
*nbp_name_print(const struct atNBPtuple
*,
94 static const char *ataddr_string(u_short
, u_char
);
95 static void ddp_print(const u_char
*, u_int
, int, u_short
, u_char
, u_char
);
96 static const char *ddpskt_string(int);
99 * Print AppleTalk Datagram Delivery Protocol packets.
102 atalk_print(register const u_char
*bp
, u_int length
)
104 register const struct LAP
*lp
;
105 register const struct atDDP
*dp
;
106 register const struct atShortDDP
*sdp
;
109 lp
= (struct LAP
*)bp
;
111 length
-= sizeof(*lp
);
115 if (length
< ddpSSize
) {
116 (void)printf(" [|sddp %d]", length
);
119 sdp
= (const struct atShortDDP
*)bp
;
121 ataddr_string(0, lp
->src
), ddpskt_string(sdp
->srcSkt
));
123 ataddr_string(0, lp
->dst
), ddpskt_string(sdp
->dstSkt
));
126 ddp_print(bp
, length
, sdp
->type
, 0, lp
->src
, sdp
->srcSkt
);
130 if (length
< ddpSize
) {
131 (void)printf(" [|ddp %d]", length
);
134 dp
= (const struct atDDP
*)bp
;
135 snet
= EXTRACT_16BITS(&dp
->srcNet
);
136 printf("%s.%s", ataddr_string(snet
, dp
->srcNode
),
137 ddpskt_string(dp
->srcSkt
));
139 ataddr_string(EXTRACT_16BITS(&dp
->dstNet
), dp
->dstNode
),
140 ddpskt_string(dp
->dstSkt
));
143 ddp_print(bp
, length
, dp
->type
, snet
, dp
->srcNode
, dp
->srcSkt
);
148 klap_print(bp
, length
);
153 printf("%d > %d at-lap#%d %d",
154 lp
->src
, lp
->dst
, lp
->type
, length
);
159 /* XXX should probably pass in the snap header and do checks like arp_print() */
161 aarp_print(register const u_char
*bp
, u_int length
)
163 register const struct aarp
*ap
;
165 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
168 ap
= (const struct aarp
*)bp
;
169 if (ntohs(ap
->htype
) == 1 && ntohs(ap
->ptype
) == ETHERTYPE_ATALK
&&
170 ap
->halen
== 6 && ap
->palen
== 4 )
171 switch (ntohs(ap
->op
)) {
173 case 1: /* request */
174 (void)printf("who-has %s tell %s",
175 AT(pdaddr
), AT(psaddr
));
178 case 2: /* response */
179 (void)printf("reply %s is-at %s",
180 AT(pdaddr
), etheraddr_string(ap
->hdaddr
));
183 case 3: /* probe (oy!) */
184 (void)printf("probe %s tell %s",
185 AT(pdaddr
), AT(psaddr
));
188 (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
189 length
, ntohs(ap
->op
), ntohs(ap
->htype
), ntohs(ap
->ptype
),
190 ap
->halen
, ap
->palen
);
194 ddp_print(register const u_char
*bp
, register u_int length
, register int t
,
195 register u_short snet
, register u_char snode
, u_char skt
)
201 nbp_print((const struct atNBP
*)bp
, length
, snet
, snode
, skt
);
205 atp_print((const struct atATP
*)bp
, length
);
209 (void)printf(" at-%s %d", tok2str(type2str
, NULL
, t
), length
);
215 atp_print(register const struct atATP
*ap
, u_int length
)
220 if ((const u_char
*)(ap
+ 1) > snapend
) {
221 /* Just bail if we don't have the whole chunk. */
225 length
-= sizeof(*ap
);
226 switch (ap
->control
& 0xc0) {
229 (void)printf(" atp-req%s %d",
230 ap
->control
& atpXO
? " " : "*",
231 EXTRACT_16BITS(&ap
->transID
));
233 atp_bitmap_print(ap
->bitmap
);
236 (void)printf(" [len=%d]", length
);
238 switch (ap
->control
& (atpEOM
|atpSTS
)) {
240 (void)printf(" [EOM]");
243 (void)printf(" [STS]");
246 (void)printf(" [EOM,STS]");
252 (void)printf(" atp-resp%s%d:%d (%d)",
253 ap
->control
& atpEOM
? "*" : " ",
254 EXTRACT_16BITS(&ap
->transID
), ap
->bitmap
, length
);
255 switch (ap
->control
& (atpXO
|atpSTS
)) {
257 (void)printf(" [XO]");
260 (void)printf(" [STS]");
263 (void)printf(" [XO,STS]");
269 (void)printf(" atp-rel %d", EXTRACT_16BITS(&ap
->transID
));
271 atp_bitmap_print(ap
->bitmap
);
273 /* length should be zero */
275 (void)printf(" [len=%d]", length
);
277 /* there shouldn't be any control flags */
278 if (ap
->control
& (atpXO
|atpEOM
|atpSTS
)) {
280 if (ap
->control
& atpXO
) {
281 (void)printf("%cXO", c
);
284 if (ap
->control
& atpEOM
) {
285 (void)printf("%cEOM", c
);
288 if (ap
->control
& atpSTS
) {
289 (void)printf("%cSTS", c
);
297 (void)printf(" atp-0x%x %d (%d)", ap
->control
,
298 EXTRACT_16BITS(&ap
->transID
), length
);
301 data
= EXTRACT_32BITS(&ap
->userData
);
303 (void)printf(" 0x%x", data
);
307 atp_bitmap_print(register u_char bm
)
313 * The '& 0xff' below is needed for compilers that want to sign
314 * extend a u_char, which is the case with the Ultrix compiler.
315 * (gcc is smart enough to eliminate it, at least on the Sparc).
317 if ((bm
+ 1) & (bm
& 0xff)) {
319 for (i
= 0; bm
; ++i
) {
321 (void)printf("%c%d", c
, i
);
331 (void)printf("<0-%d>", i
- 1);
338 nbp_print(register const struct atNBP
*np
, u_int length
, register u_short snet
,
339 register u_char snode
, register u_char skt
)
341 register const struct atNBPtuple
*tp
=
342 (struct atNBPtuple
*)((u_char
*)np
+ nbpHeaderSize
);
346 length
-= nbpHeaderSize
;
348 /* must be room for at least one tuple */
349 (void)printf(" truncated-nbp %d", length
+ nbpHeaderSize
);
352 /* ep points to end of available data */
354 if ((const u_char
*)tp
> ep
) {
358 switch (i
= np
->control
& 0xf0) {
362 (void)printf(i
== nbpLkUp
? " nbp-lkup %d:":" nbp-brRq %d:",
364 if ((const u_char
*)(tp
+ 1) > ep
) {
368 (void)nbp_name_print(tp
, ep
);
370 * look for anomalies: the spec says there can only
371 * be one tuple, the address must match the source
372 * address and the enumerator should be zero.
374 if ((np
->control
& 0xf) != 1)
375 (void)printf(" [ntup=%d]", np
->control
& 0xf);
377 (void)printf(" [enum=%d]", tp
->enumerator
);
378 if (EXTRACT_16BITS(&tp
->net
) != snet
||
379 tp
->node
!= snode
|| tp
->skt
!= skt
)
380 (void)printf(" [addr=%s.%d]",
381 ataddr_string(EXTRACT_16BITS(&tp
->net
),
386 (void)printf(" nbp-reply %d:", np
->id
);
388 /* print each of the tuples in the reply */
389 for (i
= np
->control
& 0xf; --i
>= 0 && tp
; )
390 tp
= nbp_tuple_print(tp
, ep
, snet
, snode
, skt
);
394 (void)printf(" nbp-0x%x %d (%d)", np
->control
, np
->id
,
400 /* print a counted string */
402 print_cstring(register const char *cp
, register const u_char
*ep
)
404 register u_int length
;
406 if (cp
>= (const char *)ep
) {
412 /* Spec says string can be at most 32 bytes long */
414 (void)printf("[len=%u]", length
);
417 while ((int)--length
>= 0) {
418 if (cp
>= (char *)ep
) {
427 static const struct atNBPtuple
*
428 nbp_tuple_print(register const struct atNBPtuple
*tp
,
429 register const u_char
*ep
,
430 register u_short snet
, register u_char snode
,
433 register const struct atNBPtuple
*tpn
;
435 if ((const u_char
*)(tp
+ 1) > ep
) {
439 tpn
= nbp_name_print(tp
, ep
);
441 /* if the enumerator isn't 1, print it */
442 if (tp
->enumerator
!= 1)
443 (void)printf("(%d)", tp
->enumerator
);
445 /* if the socket doesn't match the src socket, print it */
447 (void)printf(" %d", tp
->skt
);
449 /* if the address doesn't match the src address, it's an anomaly */
450 if (EXTRACT_16BITS(&tp
->net
) != snet
|| tp
->node
!= snode
)
451 (void)printf(" [addr=%s]",
452 ataddr_string(EXTRACT_16BITS(&tp
->net
), tp
->node
));
457 static const struct atNBPtuple
*
458 nbp_name_print(const struct atNBPtuple
*tp
, register const u_char
*ep
)
460 register const char *cp
= (const char *)tp
+ nbpTupleSize
;
466 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
469 if ((cp
= print_cstring(cp
, ep
)) != NULL
) {
472 if ((cp
= print_cstring(cp
, ep
)) != NULL
)
476 return ((const struct atNBPtuple
*)cp
);
480 #define HASHNAMESIZE 4096
485 struct hnamemem
*nxt
;
488 static struct hnamemem hnametable
[HASHNAMESIZE
];
491 ataddr_string(u_short atnet
, u_char athost
)
493 register struct hnamemem
*tp
, *tp2
;
494 register int i
= (atnet
<< 8) | athost
;
496 static int first
= 1;
500 * if this is the first call, see if there's an AppleTalk
501 * number to name map file.
503 if (first
&& (first
= 0, !nflag
)
504 && (fp
= fopen("/etc/atalk.names", "r"))) {
508 while (fgets(line
, sizeof(line
), fp
)) {
509 if (line
[0] == '\n' || line
[0] == 0 || line
[0] == '#')
511 if (sscanf(line
, "%d.%d.%d %256s", &i1
, &i2
, &i3
,
513 /* got a hostname. */
514 i3
|= ((i1
<< 8) | i2
) << 8;
515 else if (sscanf(line
, "%d.%d %256s", &i1
, &i2
,
518 i3
= (((i1
<< 8) | i2
) << 8) | 255;
522 for (tp
= &hnametable
[i3
& (HASHNAMESIZE
-1)];
523 tp
->nxt
; tp
= tp
->nxt
)
526 tp
->nxt
= newhnamemem();
527 tp
->name
= savestr(nambuf
);
532 for (tp
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp
->nxt
; tp
= tp
->nxt
)
536 /* didn't have the node name -- see if we've got the net name */
538 for (tp2
= &hnametable
[i
& (HASHNAMESIZE
-1)]; tp2
->nxt
; tp2
= tp2
->nxt
)
539 if (tp2
->addr
== i
) {
540 tp
->addr
= (atnet
<< 8) | athost
;
541 tp
->nxt
= newhnamemem();
542 (void)snprintf(nambuf
, sizeof(nambuf
), "%s.%d",
544 tp
->name
= savestr(nambuf
);
548 tp
->addr
= (atnet
<< 8) | athost
;
549 tp
->nxt
= newhnamemem();
551 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d.%d",
552 atnet
>> 8, atnet
& 0xff, athost
);
554 (void)snprintf(nambuf
, sizeof(nambuf
), "%d.%d", atnet
>> 8,
556 tp
->name
= savestr(nambuf
);
561 static struct tok skt2str
[] = {
562 { rtmpSkt
, "rtmp" }, /* routing table maintenance */
563 { nbpSkt
, "nis" }, /* name info socket */
564 { echoSkt
, "echo" }, /* AppleTalk echo protocol */
565 { zipSkt
, "zip" }, /* zone info protocol */
570 ddpskt_string(register int skt
)
575 (void)snprintf(buf
, sizeof(buf
), "%d", skt
);
578 return (tok2str(skt2str
, "%d", skt
));