1 /*
2  * Copyright (c) 1988-1990 The Regents of the University of California.
3  * 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 
22 #ifndef lint
23 static char rcsid[] =
24     "@(#) $Header: /usr/staff/martinh/tcpview/RCS/print-arp.c,v 1.3 1993/04/22 20:27:45 martinh Exp $ (LBL)";
25 #endif
26 
27 #include <sys/param.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <net/if.h>
31 #include <netinet/in.h>
32 #include <netinet/if_ether.h>
33 
34 #ifdef TCPVIEW
35 #include "tcpview.h"
36 #endif
37 
38 #include "addrtoname.h"
39 #include "interface.h"
40 
arp_print(ap,length,caplen)41 void arp_print(ap, length, caplen)
42      register struct ether_arp *ap;
43      int length;
44      int caplen;
45 {
46   short hrd, pro, op;
47   if ((u_char *)(ap + 1) > snapend) {
48     printf("[|arp]");
49     return;
50   }
51   if (length < sizeof(struct ether_arp)) {
52     (void)printf("truncated-arp");
53     default_print((u_short *)ap, length);
54     return;
55   }
56 
57   hrd = ntohs(ap->arp_hrd);
58   pro = ntohs(ap->arp_pro);
59   op = ntohs(ap->arp_op);
60 
61   if (hrd != ARPHRD_ETHER || (pro != ETHERTYPE_IP && pro != ETHERTYPE_TRAIL)
62       || ap->arp_hln != sizeof(SHA(ap)) || ap->arp_pln != sizeof(SPA(ap))) {
63     (void)printf("arp-req #%d for proto #%d (%d) hardware %d (%d)",
64 		 op, pro, ap->arp_pln, hrd, ap->arp_hln);
65     return;
66   }
67   if (pro == ETHERTYPE_TRAIL)
68     (void)printf("trailer");
69   switch (op) {
70 
71   case ARPOP_REQUEST:
72     (void)printf("arp who-has %s tell %s",
73 		 ipaddr_string(TPA(ap)),
74 		 ipaddr_string(SPA(ap)));
75     break;
76 
77   case ARPOP_REPLY:
78     (void)printf("arp reply %s is-at %s",
79 		 ipaddr_string(SPA(ap)),
80 		 etheraddr_string(SHA(ap)));
81     break;
82 
83   case REVARP_REQUEST:
84     (void)printf("rarp who-is %s tell %s",
85 		 etheraddr_string(THA(ap)),
86 		 etheraddr_string(SHA(ap)));
87     break;
88 
89   case REVARP_REPLY:
90     (void)printf("rarp reply %s at %s",
91 		 etheraddr_string(THA(ap)),
92 		 ipaddr_string(TPA(ap)));
93     break;
94 
95   default:
96     (void)printf("arp-%d", op);
97     default_print((u_short *)ap, caplen);
98     break;
99   }
100 }
101