1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of libtrace. |
---|
7 | * |
---|
8 | * This code has been developed by the University of Waikato WAND |
---|
9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
10 | * |
---|
11 | * libtrace is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
13 | * the Free Software Foundation; either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * libtrace is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * |
---|
25 | */ |
---|
26 | /* ARP */ |
---|
27 | #include <stdio.h> |
---|
28 | #include <inttypes.h> |
---|
29 | #include <dlfcn.h> |
---|
30 | #include "libpacketdump.h" |
---|
31 | #include <sys/socket.h> |
---|
32 | #ifndef WIN32 |
---|
33 | #include <netinet/in_systm.h> |
---|
34 | #endif |
---|
35 | #include <netinet/in.h> |
---|
36 | #include <netinet/ip.h> |
---|
37 | #include <net/if_arp.h> |
---|
38 | #include <arpa/inet.h> |
---|
39 | #include <string.h> |
---|
40 | |
---|
41 | /* |
---|
42 | * Converts an ARP hardware address to a printable string. |
---|
43 | * Takes an ARP header structure and a pointer to the start |
---|
44 | * of the hardware address in the structure that we should |
---|
45 | * attempt to decode. |
---|
46 | */ |
---|
47 | static char *format_hrd(const struct arphdr *arp, const char *hrd) { |
---|
48 | static char buffer[1024] = {0,}; |
---|
49 | int i, ret; |
---|
50 | size_t bufused; |
---|
51 | |
---|
52 | if (!hrd) { |
---|
53 | strncpy(buffer, "(Truncated)", sizeof(buffer)); |
---|
54 | return buffer; |
---|
55 | } |
---|
56 | |
---|
57 | switch(ntohs(arp->ar_hrd)) { |
---|
58 | case ARPHRD_ETHER: |
---|
59 | trace_ether_ntoa((const unsigned char *)hrd, buffer); |
---|
60 | break; |
---|
61 | default: |
---|
62 | bufused = 0; |
---|
63 | for (i=0;i<arp->ar_hln;i++) { |
---|
64 | if (bufused >= sizeof(buffer)) { |
---|
65 | break; |
---|
66 | } |
---|
67 | ret = snprintf(buffer + bufused, |
---|
68 | sizeof(buffer) - bufused, |
---|
69 | "%02x ", |
---|
70 | (unsigned char)hrd[i]); |
---|
71 | if (ret > 0) { |
---|
72 | bufused += ret; |
---|
73 | } |
---|
74 | } |
---|
75 | break; |
---|
76 | } |
---|
77 | |
---|
78 | return buffer; |
---|
79 | } |
---|
80 | |
---|
81 | /* |
---|
82 | * Converts an ARP protocol address to a printable string. |
---|
83 | * Takes an ARP header structure and a pointer to the start |
---|
84 | * of the protocol address in the structure that we should |
---|
85 | * attempt to decode. |
---|
86 | */ |
---|
87 | static char *format_pro(const struct arphdr *arp, const char *pro) { |
---|
88 | static char buffer[1024] = {0,}; |
---|
89 | int i, ret; |
---|
90 | size_t bufused; |
---|
91 | |
---|
92 | if (!pro) { |
---|
93 | strncpy(buffer, "(Truncated)", sizeof(buffer)); |
---|
94 | return buffer; |
---|
95 | } |
---|
96 | |
---|
97 | switch(ntohs(arp->ar_pro)) { |
---|
98 | case 0x0800: |
---|
99 | snprintf(buffer,sizeof(buffer),"%s", |
---|
100 | inet_ntoa(*(struct in_addr*)pro)); |
---|
101 | break; |
---|
102 | default: |
---|
103 | snprintf(buffer, sizeof(buffer), "%s", " ("); |
---|
104 | bufused = 2; |
---|
105 | for (i=0;i<arp->ar_pln;i++) { |
---|
106 | if (bufused >= sizeof(buffer)) { |
---|
107 | break; |
---|
108 | } |
---|
109 | ret = snprintf(buffer + bufused, |
---|
110 | sizeof(buffer) - bufused, |
---|
111 | "%02x ", |
---|
112 | (unsigned char)pro[i]); |
---|
113 | if (ret > 0) { |
---|
114 | bufused += ret; |
---|
115 | } |
---|
116 | } |
---|
117 | if (bufused < sizeof(buffer)) { |
---|
118 | snprintf(buffer + bufused, |
---|
119 | sizeof(buffer) - bufused, |
---|
120 | ")"); |
---|
121 | } |
---|
122 | break; |
---|
123 | } |
---|
124 | return buffer; |
---|
125 | |
---|
126 | } |
---|
127 | |
---|
128 | DLLEXPORT void decode(int link_type UNUSED,const char *packet,unsigned len) |
---|
129 | { |
---|
130 | struct arphdr *arp = (struct arphdr*)packet; |
---|
131 | const char *source_hrd = NULL; |
---|
132 | const char *source_pro = NULL; |
---|
133 | const char *dest_hrd = NULL; |
---|
134 | const char *dest_pro = NULL; |
---|
135 | |
---|
136 | if (len < sizeof(struct arphdr)) { |
---|
137 | printf(" ARP: (Truncated)\n"); |
---|
138 | return; |
---|
139 | } |
---|
140 | |
---|
141 | if (len >= sizeof(struct arphdr) + arp->ar_hln) |
---|
142 | source_hrd = packet + sizeof(struct arphdr); |
---|
143 | if (len >= sizeof(struct arphdr) + arp->ar_hln + arp->ar_pln) |
---|
144 | source_pro = source_hrd + arp->ar_hln; |
---|
145 | if (len >= sizeof(struct arphdr) + arp->ar_hln * 2 + arp->ar_pln) |
---|
146 | dest_hrd = source_pro + arp->ar_pln; |
---|
147 | if (len >= sizeof(struct arphdr) + arp->ar_hln * 2 + arp->ar_pln * 2) |
---|
148 | dest_pro = dest_hrd + arp->ar_hln; |
---|
149 | |
---|
150 | switch(ntohs(arp->ar_op)) { |
---|
151 | case ARPOP_REQUEST: |
---|
152 | printf(" ARP: who-has %s", format_pro(arp, dest_pro)); |
---|
153 | printf(" tell %s (%s)\n", format_pro(arp, source_pro), |
---|
154 | format_hrd(arp, source_hrd)); |
---|
155 | break; |
---|
156 | case ARPOP_REPLY: |
---|
157 | printf(" ARP: reply %s", format_pro(arp, source_pro)); |
---|
158 | printf(" is-at %s\n", format_hrd(arp, source_hrd)); |
---|
159 | break; |
---|
160 | default: |
---|
161 | printf(" ARP: Unknown opcode (%i) from %s to %s\n", |
---|
162 | ntohs(arp->ar_op), |
---|
163 | format_pro(arp, source_pro), |
---|
164 | format_pro(arp, dest_pro)); |
---|
165 | |
---|
166 | break; |
---|
167 | } |
---|
168 | return; |
---|
169 | } |
---|