-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharpdump.c
More file actions
260 lines (237 loc) · 7.12 KB
/
arpdump.c
File metadata and controls
260 lines (237 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* arpdump.c - dump arp table
* vinod
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#include <err.h>
#include <sys/param.h>
#include <sys/sockio.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/route.h>
#include <net/iso88025.h>
#include <net/arpmon.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <arpa/inet.h>
typedef void (action_fn)(struct sockaddr_dl *sdl,
struct sockaddr_inarp *s_in, struct rt_msghdr *rtm);
static action_fn print_entry;
static int search(u_long addr, action_fn *action);
static int get(char *host);
static struct sockaddr_inarp *getaddr(char *host);
static char *rifname;
static int nflag = 1; /* no reverse dns lookups */
/*
* Display an arp entry
*/
static time_t expire_time;
static char lifname[IF_NAMESIZE];
static int64_t lifindex = -1;
static void
print_entry(struct sockaddr_dl *sdl,
struct sockaddr_inarp *addr, struct rt_msghdr *rtm)
{
const char *host;
struct hostent *hp;
struct iso88025_sockaddr_dl_data *trld;
int seg;
if (nflag == 0)
hp = gethostbyaddr((caddr_t)&(addr->sin_addr),
sizeof addr->sin_addr, AF_INET);
else
hp = 0;
if (hp)
host = hp->h_name;
else {
host = "?";
if (h_errno == TRY_AGAIN)
nflag = 1;
}
printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr));
if (sdl->sdl_alen) {
if ((sdl->sdl_type == IFT_ETHER ||
sdl->sdl_type == IFT_L2VLAN ||
sdl->sdl_type == IFT_BRIDGE) &&
sdl->sdl_alen == ETHER_ADDR_LEN)
printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl)));
else {
int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
printf("%s", link_ntoa(sdl) + n);
}
} else
printf("(incomplete)");
if (sdl->sdl_index != lifindex &&
if_indextoname(sdl->sdl_index, lifname) != NULL) {
lifindex = sdl->sdl_index;
printf(" on %s", lifname);
} else if (sdl->sdl_index == lifindex)
printf(" on %s", lifname);
if (rtm->rtm_rmx.rmx_expire == 0)
printf(" permanent");
else {
static struct timespec tp;
if (tp.tv_sec == 0)
clock_gettime(CLOCK_MONOTONIC, &tp);
if ((expire_time = rtm->rtm_rmx.rmx_expire - tp.tv_sec) > 0)
printf(" expires in %d seconds", (int)expire_time);
else
printf(" expired");
}
if (addr->sin_other & SIN_PROXY)
printf(" published (proxy only)");
if (rtm->rtm_flags & RTF_ANNOUNCE)
printf(" published");
switch(sdl->sdl_type) {
case IFT_ETHER:
printf(" [ethernet]");
break;
case IFT_ISO88025:
printf(" [token-ring]");
trld = SDL_ISO88025(sdl);
if (trld->trld_rcf != 0) {
printf(" rt=%x", ntohs(trld->trld_rcf));
for (seg = 0;
seg < ((TR_RCF_RIFLEN(trld->trld_rcf) - 2 ) / 2);
seg++)
printf(":%x", ntohs(*(trld->trld_route[seg])));
}
break;
case IFT_FDDI:
printf(" [fddi]");
break;
case IFT_ATM:
printf(" [atm]");
break;
case IFT_L2VLAN:
printf(" [vlan]");
break;
case IFT_IEEE1394:
printf(" [firewire]");
break;
case IFT_BRIDGE:
printf(" [bridge]");
break;
default:
break;
}
printf("\n");
}
/*
* Given a hostname, fills up a (static) struct sockaddr_inarp with
* the address of the host and returns a pointer to the
* structure.
*/
static struct sockaddr_inarp *
getaddr(char *host)
{
struct hostent *hp;
static struct sockaddr_inarp reply;
bzero(&reply, sizeof(reply));
reply.sin_len = sizeof(reply);
reply.sin_family = AF_INET;
reply.sin_addr.s_addr = inet_addr(host);
if (reply.sin_addr.s_addr == INADDR_NONE) {
if (!(hp = gethostbyname(host))) {
warnx("%s: %s", host, hstrerror(h_errno));
return (NULL);
}
bcopy((char *)hp->h_addr, (char *)&reply.sin_addr,
sizeof reply.sin_addr);
}
return (&reply);
}
static int
get(char *host)
{
struct sockaddr_inarp *addr;
addr = getaddr(host);
if (addr == NULL)
return (1);
if (0 == search(addr->sin_addr.s_addr, print_entry)) {
printf("%s (%s) -- no entry",
host, inet_ntoa(addr->sin_addr));
if (rifname)
printf(" on %s", rifname);
printf("\n");
return (1);
}
return (0);
}
/*
* Search the arp table and do some action on matching entries
*/
static int
search(u_long addr, action_fn *action)
{
int mib[6];
size_t needed;
char *lim, *buf, *next;
struct rt_msghdr *rtm;
struct sockaddr_inarp *sin2;
struct sockaddr_dl *sdl;
char ifname[IF_NAMESIZE];
int st, found_entry = 0;
mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
mib[2] = 0;
mib[3] = AF_INET;
mib[4] = NET_RT_FLAGS;
#ifdef RTF_LLINFO
mib[5] = RTF_LLINFO;
#else
mib[5] = 0;
#endif
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
err(1, "route-sysctl-estimate");
if (needed == 0) /* empty table */
return 0;
buf = NULL;
for (;;) {
buf = reallocf(buf, needed);
if (buf == NULL)
errx(1, "could not reallocate memory");
st = sysctl(mib, 6, buf, &needed, NULL, 0);
if (st == 0 || errno != ENOMEM)
break;
needed += needed / 8;
}
if (st == -1)
err(1, "actual retrieval of routing table");
lim = buf + needed;
for (next = buf; next < lim; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)next;
sin2 = (struct sockaddr_inarp *)(rtm + 1);
sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2));
if (rifname && if_indextoname(sdl->sdl_index, ifname) &&
strcmp(ifname, rifname))
continue;
if (addr) {
if (addr != sin2->sin_addr.s_addr)
continue;
found_entry = 1;
}
(*action)(sdl, sin2, rtm);
}
free(buf);
return (found_entry);
}
int
main ( int argc, char *argv[] )
{
argc = argc;
if ( argv[1] )
get(argv[1]);
else
search(0, print_entry);
return ( EXIT_SUCCESS );
}