Fix a bug when '-f -H' is used and the target already exists. cpdup was
[dragonfly.git] / contrib / tcpdump-3.8.3 / print-atalk.c
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3  *      The Regents of the University of California.  All rights reserved.
4  *
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
16  * written permission.
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.
20  *
21  * Format and print AppleTalk packets.
22  */
23
24 #ifndef lint
25 static const char rcsid[] _U_ =
26     "@(#) $Header: /tcpdump/master/tcpdump/print-atalk.c,v 1.78.2.2 2003/11/16 08:51:11 guy Exp $ (LBL)";
27 #endif
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <tcpdump-stdinc.h>
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <pcap.h>
39
40 #include "interface.h"
41 #include "addrtoname.h"
42 #include "ethertype.h"
43 #include "extract.h"                    /* must come after interface.h */
44 #include "appletalk.h"
45
46 static struct tok type2str[] = {
47         { ddpRTMP,              "rtmp" },
48         { ddpRTMPrequest,       "rtmpReq" },
49         { ddpECHO,              "echo" },
50         { ddpIP,                "IP" },
51         { ddpARP,               "ARP" },
52         { ddpKLAP,              "KLAP" },
53         { 0,                    NULL }
54 };
55
56 struct aarp {
57         u_int16_t       htype, ptype;
58         u_int8_t        halen, palen;
59         u_int16_t       op;
60         u_int8_t        hsaddr[6];
61         u_int8_t        psaddr[4];
62         u_int8_t        hdaddr[6];
63         u_int8_t        pdaddr[4];
64 };
65
66 static char tstr[] = "[|atalk]";
67
68 static void atp_print(const struct atATP *, u_int);
69 static void atp_bitmap_print(u_char);
70 static void nbp_print(const struct atNBP *, u_int, u_short, u_char, u_char);
71 static const char *print_cstring(const char *, const u_char *);
72 static const struct atNBPtuple *nbp_tuple_print(const struct atNBPtuple *,
73                                                 const u_char *,
74                                                 u_short, u_char, u_char);
75 static const struct atNBPtuple *nbp_name_print(const struct atNBPtuple *,
76                                                const u_char *);
77 static const char *ataddr_string(u_short, u_char);
78 static void ddp_print(const u_char *, u_int, int, u_short, u_char, u_char);
79 static const char *ddpskt_string(int);
80
81 /*
82  * Print LLAP packets received on a physical LocalTalk interface.
83  */
84 u_int
85 ltalk_if_print(const struct pcap_pkthdr *h, const u_char *p)
86 {
87         return (llap_print(p, h->caplen));
88 }
89
90 /*
91  * Print AppleTalk LLAP packets.
92  */
93 u_int
94 llap_print(register const u_char *bp, u_int length)
95 {
96         register const struct LAP *lp;
97         register const struct atDDP *dp;
98         register const struct atShortDDP *sdp;
99         u_short snet;
100         u_int hdrlen;
101
102         /*
103          * Our packet is on a 4-byte boundary, as we're either called
104          * directly from a top-level link-layer printer (ltalk_if_print)
105          * or from the UDP printer.  The LLAP+DDP header is a multiple
106          * of 4 bytes in length, so the DDP payload is also on a 4-byte
107          * boundary, and we don't need to align it before calling
108          * "ddp_print()".
109          */
110         lp = (const struct LAP *)bp;
111         bp += sizeof(*lp);
112         length -= sizeof(*lp);
113         hdrlen = sizeof(*lp);
114         switch (lp->type) {
115
116         case lapShortDDP:
117                 if (length < ddpSSize) {
118                         (void)printf(" [|sddp %d]", length);
119                         return (length);
120                 }
121                 sdp = (const struct atShortDDP *)bp;
122                 printf("%s.%s",
123                     ataddr_string(0, lp->src), ddpskt_string(sdp->srcSkt));
124                 printf(" > %s.%s:",
125                     ataddr_string(0, lp->dst), ddpskt_string(sdp->dstSkt));
126                 bp += ddpSSize;
127                 length -= ddpSSize;
128                 hdrlen += ddpSSize;
129                 ddp_print(bp, length, sdp->type, 0, lp->src, sdp->srcSkt);
130                 break;
131
132         case lapDDP:
133                 if (length < ddpSize) {
134                         (void)printf(" [|ddp %d]", length);
135                         return (length);
136                 }
137                 dp = (const struct atDDP *)bp;
138                 snet = EXTRACT_16BITS(&dp->srcNet);
139                 printf("%s.%s", ataddr_string(snet, dp->srcNode),
140                     ddpskt_string(dp->srcSkt));
141                 printf(" > %s.%s:",
142                     ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
143                     ddpskt_string(dp->dstSkt));
144                 bp += ddpSize;
145                 length -= ddpSize;
146                 hdrlen += ddpSize;
147                 ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
148                 break;
149
150 #ifdef notdef
151         case lapKLAP:
152                 klap_print(bp, length);
153                 break;
154 #endif
155
156         default:
157                 printf("%d > %d at-lap#%d %d",
158                     lp->src, lp->dst, lp->type, length);
159                 break;
160         }
161         return (hdrlen);
162 }
163
164 /*
165  * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
166  * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
167  * packets in them).
168  */
169 void
170 atalk_print(register const u_char *bp, u_int length)
171 {
172         register const struct atDDP *dp;
173         u_short snet;
174
175         if (length < ddpSize) {
176                 (void)printf(" [|ddp %d]", length);
177                 return;
178         }
179         dp = (const struct atDDP *)bp;
180         snet = EXTRACT_16BITS(&dp->srcNet);
181         printf("%s.%s", ataddr_string(snet, dp->srcNode),
182                ddpskt_string(dp->srcSkt));
183         printf(" > %s.%s:",
184                ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
185                ddpskt_string(dp->dstSkt));
186         bp += ddpSize;
187         length -= ddpSize;
188         ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
189 }
190
191 /* XXX should probably pass in the snap header and do checks like arp_print() */
192 void
193 aarp_print(register const u_char *bp, u_int length)
194 {
195         register const struct aarp *ap;
196
197 #define AT(member) ataddr_string((ap->member[1]<<8)|ap->member[2],ap->member[3])
198
199         printf("aarp ");
200         ap = (const struct aarp *)bp;
201         if (EXTRACT_16BITS(&ap->htype) == 1 &&
202             EXTRACT_16BITS(&ap->ptype) == ETHERTYPE_ATALK &&
203             ap->halen == 6 && ap->palen == 4 )
204                 switch (EXTRACT_16BITS(&ap->op)) {
205
206                 case 1:                         /* request */
207                         (void)printf("who-has %s tell %s",
208                             AT(pdaddr), AT(psaddr));
209                         return;
210
211                 case 2:                         /* response */
212                         (void)printf("reply %s is-at %s",
213                             AT(pdaddr), etheraddr_string(ap->hdaddr));
214                         return;
215
216                 case 3:                         /* probe (oy!) */
217                         (void)printf("probe %s tell %s",
218                             AT(pdaddr), AT(psaddr));
219                         return;
220                 }
221         (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
222             length, EXTRACT_16BITS(&ap->op), EXTRACT_16BITS(&ap->htype),
223             EXTRACT_16BITS(&ap->ptype), ap->halen, ap->palen);
224 }
225
226 /*
227  * Print AppleTalk Datagram Delivery Protocol packets.
228  */
229 static void
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)
232 {
233
234         switch (t) {
235
236         case ddpNBP:
237                 nbp_print((const struct atNBP *)bp, length, snet, snode, skt);
238                 break;
239
240         case ddpATP:
241                 atp_print((const struct atATP *)bp, length);
242                 break;
243
244         default:
245                 (void)printf(" at-%s %d", tok2str(type2str, NULL, t), length);
246                 break;
247         }
248 }
249
250 static void
251 atp_print(register const struct atATP *ap, u_int length)
252 {
253         char c;
254         u_int32_t data;
255
256         if ((const u_char *)(ap + 1) > snapend) {
257                 /* Just bail if we don't have the whole chunk. */
258                 fputs(tstr, stdout);
259                 return;
260         }
261         length -= sizeof(*ap);
262         switch (ap->control & 0xc0) {
263
264         case atpReqCode:
265                 (void)printf(" atp-req%s %d",
266                              ap->control & atpXO? " " : "*",
267                              EXTRACT_16BITS(&ap->transID));
268
269                 atp_bitmap_print(ap->bitmap);
270
271                 if (length != 0)
272                         (void)printf(" [len=%d]", length);
273
274                 switch (ap->control & (atpEOM|atpSTS)) {
275                 case atpEOM:
276                         (void)printf(" [EOM]");
277                         break;
278                 case atpSTS:
279                         (void)printf(" [STS]");
280                         break;
281                 case atpEOM|atpSTS:
282                         (void)printf(" [EOM,STS]");
283                         break;
284                 }
285                 break;
286
287         case atpRspCode:
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)) {
292                 case atpXO:
293                         (void)printf(" [XO]");
294                         break;
295                 case atpSTS:
296                         (void)printf(" [STS]");
297                         break;
298                 case atpXO|atpSTS:
299                         (void)printf(" [XO,STS]");
300                         break;
301                 }
302                 break;
303
304         case atpRelCode:
305                 (void)printf(" atp-rel  %d", EXTRACT_16BITS(&ap->transID));
306
307                 atp_bitmap_print(ap->bitmap);
308
309                 /* length should be zero */
310                 if (length)
311                         (void)printf(" [len=%d]", length);
312
313                 /* there shouldn't be any control flags */
314                 if (ap->control & (atpXO|atpEOM|atpSTS)) {
315                         c = '[';
316                         if (ap->control & atpXO) {
317                                 (void)printf("%cXO", c);
318                                 c = ',';
319                         }
320                         if (ap->control & atpEOM) {
321                                 (void)printf("%cEOM", c);
322                                 c = ',';
323                         }
324                         if (ap->control & atpSTS) {
325                                 (void)printf("%cSTS", c);
326                                 c = ',';
327                         }
328                         (void)printf("]");
329                 }
330                 break;
331
332         default:
333                 (void)printf(" atp-0x%x  %d (%d)", ap->control,
334                              EXTRACT_16BITS(&ap->transID), length);
335                 break;
336         }
337         data = EXTRACT_32BITS(&ap->userData);
338         if (data != 0)
339                 (void)printf(" 0x%x", data);
340 }
341
342 static void
343 atp_bitmap_print(register u_char bm)
344 {
345         register char c;
346         register int i;
347
348         /*
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).
352          */
353         if ((bm + 1) & (bm & 0xff)) {
354                 c = '<';
355                 for (i = 0; bm; ++i) {
356                         if (bm & 1) {
357                                 (void)printf("%c%d", c, i);
358                                 c = ',';
359                         }
360                         bm >>= 1;
361                 }
362                 (void)printf(">");
363         } else {
364                 for (i = 0; bm; ++i)
365                         bm >>= 1;
366                 if (i > 1)
367                         (void)printf("<0-%d>", i - 1);
368                 else
369                         (void)printf("<0>");
370         }
371 }
372
373 static void
374 nbp_print(register const struct atNBP *np, u_int length, register u_short snet,
375           register u_char snode, register u_char skt)
376 {
377         register const struct atNBPtuple *tp =
378                 (const struct atNBPtuple *)((u_char *)np + nbpHeaderSize);
379         int i;
380         const u_char *ep;
381
382         if (length < nbpHeaderSize) {
383                 (void)printf(" truncated-nbp %d", length);
384                 return;
385         }
386
387         length -= nbpHeaderSize;
388         if (length < 8) {
389                 /* must be room for at least one tuple */
390                 (void)printf(" truncated-nbp %d", length + nbpHeaderSize);
391                 return;
392         }
393         /* ep points to end of available data */
394         ep = snapend;
395         if ((const u_char *)tp > ep) {
396                 fputs(tstr, stdout);
397                 return;
398         }
399         switch (i = np->control & 0xf0) {
400
401         case nbpBrRq:
402         case nbpLkUp:
403                 (void)printf(i == nbpLkUp? " nbp-lkup %d:":" nbp-brRq %d:",
404                              np->id);
405                 if ((const u_char *)(tp + 1) > ep) {
406                         fputs(tstr, stdout);
407                         return;
408                 }
409                 (void)nbp_name_print(tp, ep);
410                 /*
411                  * look for anomalies: the spec says there can only
412                  * be one tuple, the address must match the source
413                  * address and the enumerator should be zero.
414                  */
415                 if ((np->control & 0xf) != 1)
416                         (void)printf(" [ntup=%d]", np->control & 0xf);
417                 if (tp->enumerator)
418                         (void)printf(" [enum=%d]", tp->enumerator);
419                 if (EXTRACT_16BITS(&tp->net) != snet ||
420                     tp->node != snode || tp->skt != skt)
421                         (void)printf(" [addr=%s.%d]",
422                             ataddr_string(EXTRACT_16BITS(&tp->net),
423                             tp->node), tp->skt);
424                 break;
425
426         case nbpLkUpReply:
427                 (void)printf(" nbp-reply %d:", np->id);
428
429                 /* print each of the tuples in the reply */
430                 for (i = np->control & 0xf; --i >= 0 && tp; )
431                         tp = nbp_tuple_print(tp, ep, snet, snode, skt);
432                 break;
433
434         default:
435                 (void)printf(" nbp-0x%x  %d (%d)", np->control, np->id,
436                                 length);
437                 break;
438         }
439 }
440
441 /* print a counted string */
442 static const char *
443 print_cstring(register const char *cp, register const u_char *ep)
444 {
445         register u_int length;
446
447         if (cp >= (const char *)ep) {
448                 fputs(tstr, stdout);
449                 return (0);
450         }
451         length = *cp++;
452
453         /* Spec says string can be at most 32 bytes long */
454         if (length > 32) {
455                 (void)printf("[len=%u]", length);
456                 return (0);
457         }
458         while ((int)--length >= 0) {
459                 if (cp >= (const char *)ep) {
460                         fputs(tstr, stdout);
461                         return (0);
462                 }
463                 putchar(*cp++);
464         }
465         return (cp);
466 }
467
468 static const struct atNBPtuple *
469 nbp_tuple_print(register const struct atNBPtuple *tp,
470                 register const u_char *ep,
471                 register u_short snet, register u_char snode,
472                 register u_char skt)
473 {
474         register const struct atNBPtuple *tpn;
475
476         if ((const u_char *)(tp + 1) > ep) {
477                 fputs(tstr, stdout);
478                 return 0;
479         }
480         tpn = nbp_name_print(tp, ep);
481
482         /* if the enumerator isn't 1, print it */
483         if (tp->enumerator != 1)
484                 (void)printf("(%d)", tp->enumerator);
485
486         /* if the socket doesn't match the src socket, print it */
487         if (tp->skt != skt)
488                 (void)printf(" %d", tp->skt);
489
490         /* if the address doesn't match the src address, it's an anomaly */
491         if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode)
492                 (void)printf(" [addr=%s]",
493                     ataddr_string(EXTRACT_16BITS(&tp->net), tp->node));
494
495         return (tpn);
496 }
497
498 static const struct atNBPtuple *
499 nbp_name_print(const struct atNBPtuple *tp, register const u_char *ep)
500 {
501         register const char *cp = (const char *)tp + nbpTupleSize;
502
503         putchar(' ');
504
505         /* Object */
506         putchar('"');
507         if ((cp = print_cstring(cp, ep)) != NULL) {
508                 /* Type */
509                 putchar(':');
510                 if ((cp = print_cstring(cp, ep)) != NULL) {
511                         /* Zone */
512                         putchar('@');
513                         if ((cp = print_cstring(cp, ep)) != NULL)
514                                 putchar('"');
515                 }
516         }
517         return ((const struct atNBPtuple *)cp);
518 }
519
520
521 #define HASHNAMESIZE 4096
522
523 struct hnamemem {
524         int addr;
525         char *name;
526         struct hnamemem *nxt;
527 };
528
529 static struct hnamemem hnametable[HASHNAMESIZE];
530
531 static const char *
532 ataddr_string(u_short atnet, u_char athost)
533 {
534         register struct hnamemem *tp, *tp2;
535         register int i = (atnet << 8) | athost;
536         char nambuf[MAXHOSTNAMELEN + 20];
537         static int first = 1;
538         FILE *fp;
539
540         /*
541          * if this is the first call, see if there's an AppleTalk
542          * number to name map file.
543          */
544         if (first && (first = 0, !nflag)
545             && (fp = fopen("/etc/atalk.names", "r"))) {
546                 char line[256];
547                 int i1, i2, i3;
548
549                 while (fgets(line, sizeof(line), fp)) {
550                         if (line[0] == '\n' || line[0] == 0 || line[0] == '#')
551                                 continue;
552                         if (sscanf(line, "%d.%d.%d %256s", &i1, &i2, &i3,
553                                      nambuf) == 4)
554                                 /* got a hostname. */
555                                 i3 |= ((i1 << 8) | i2) << 8;
556                         else if (sscanf(line, "%d.%d %256s", &i1, &i2,
557                                         nambuf) == 3)
558                                 /* got a net name */
559                                 i3 = (((i1 << 8) | i2) << 8) | 255;
560                         else
561                                 continue;
562
563                         for (tp = &hnametable[i3 & (HASHNAMESIZE-1)];
564                              tp->nxt; tp = tp->nxt)
565                                 ;
566                         tp->addr = i3;
567                         tp->nxt = newhnamemem();
568                         tp->name = strdup(nambuf);
569                 }
570                 fclose(fp);
571         }
572
573         for (tp = &hnametable[i & (HASHNAMESIZE-1)]; tp->nxt; tp = tp->nxt)
574                 if (tp->addr == i)
575                         return (tp->name);
576
577         /* didn't have the node name -- see if we've got the net name */
578         i |= 255;
579         for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt)
580                 if (tp2->addr == i) {
581                         tp->addr = (atnet << 8) | athost;
582                         tp->nxt = newhnamemem();
583                         (void)snprintf(nambuf, sizeof(nambuf), "%s.%d",
584                             tp2->name, athost);
585                         tp->name = strdup(nambuf);
586                         return (tp->name);
587                 }
588
589         tp->addr = (atnet << 8) | athost;
590         tp->nxt = newhnamemem();
591         if (athost != 255)
592                 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d.%d",
593                     atnet >> 8, atnet & 0xff, athost);
594         else
595                 (void)snprintf(nambuf, sizeof(nambuf), "%d.%d", atnet >> 8,
596                     atnet & 0xff);
597         tp->name = strdup(nambuf);
598
599         return (tp->name);
600 }
601
602 static struct tok skt2str[] = {
603         { rtmpSkt,      "rtmp" },       /* routing table maintenance */
604         { nbpSkt,       "nis" },        /* name info socket */
605         { echoSkt,      "echo" },       /* AppleTalk echo protocol */
606         { zipSkt,       "zip" },        /* zone info protocol */
607         { 0,            NULL }
608 };
609
610 static const char *
611 ddpskt_string(register int skt)
612 {
613         static char buf[8];
614
615         if (nflag) {
616                 (void)snprintf(buf, sizeof(buf), "%d", skt);
617                 return (buf);
618         }
619         return (tok2str(skt2str, "%d", skt));
620 }