forked from robertdavidgraham/masscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrawsock-getif.c
424 lines (348 loc) · 10.3 KB
/
rawsock-getif.c
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
get default route (gateway) IPv4 address of the named network
interface/adapter (like "eth0").
This works on both Linux and windows.
*/
#include "rawsock.h"
#include "string_s.h"
#include "ranges.h" /*for parsing IPv4 addresses */
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
#include <unistd.h>
#include <sys/socket.h>
#include <net/route.h>
#include <netinet/in.h>
#include <net/if_dl.h>
#include <ctype.h>
#define ROUNDUP(a) \
((a) > 0 ? (1 + (((a) - 1) | (sizeof(int) - 1))) : sizeof(int))
static struct sockaddr *
get_rt_address(struct rt_msghdr *rtm, int desired)
{
int i;
int bitmask = rtm->rtm_addrs;
struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
for (i = 0; i < RTAX_MAX; i++) {
if (bitmask & (1 << i)) {
if ((1<<i) == desired)
return sa;
sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
} else
;
}
return NULL;
}
int
rawsock_get_default_interface(char *ifname, size_t sizeof_ifname)
{
int fd;
int seq = time(0);
int err;
struct rt_msghdr *rtm;
size_t sizeof_buffer;
/*
* Requests/responses from the kernel are done with an "rt_msghdr"
* structure followed by an array of "sockaddr" structures.
*/
sizeof_buffer = sizeof(*rtm) + sizeof(struct sockaddr_in)*16;
rtm = (struct rt_msghdr *)malloc(sizeof_buffer);
/*
* Create a socket for querying the kernel
*/
fd = socket(PF_ROUTE, SOCK_RAW, 0);
if (fd <= 0) {
perror("socket(PF_ROUTE)");
free(rtm);
return errno;
}
/*
* Format and send request to kernel
*/
memset(rtm, 0, sizeof_buffer);
rtm->rtm_msglen = sizeof_buffer;
rtm->rtm_type = RTM_GET;
rtm->rtm_flags = RTF_UP | RTF_GATEWAY;
rtm->rtm_version = RTM_VERSION;
rtm->rtm_seq = seq;
rtm->rtm_addrs = RTA_DST | RTA_NETMASK | RTA_GATEWAY | RTA_IFP;
err = write(fd, (char *)rtm, sizeof_buffer);
if (err < 0 || err != sizeof_buffer) {
perror("write(RTM_GET)");
printf("----%u %u\n", err, (unsigned)sizeof_buffer);
close(fd);
free(rtm);
return -1;
}
/*
* Read responses until we find one that belongs to us
*/
for (;;) {
err = read(fd, (char *)rtm, sizeof_buffer);
if (err <= 0)
break;
if (rtm->rtm_seq != seq) {
printf("seq: %u %u\n", rtm->rtm_seq, seq);
continue;
}
if (rtm->rtm_pid != getpid()) {
printf("pid: %u %u\n", rtm->rtm_pid, getpid());
continue;
}
break;
}
close(fd);
//hexdump(rtm+1, err-sizeof(*rtm));
//dump_rt_addresses(rtm);
/*
* Parse our data
*/
{
//struct sockaddr_in *sin;
struct sockaddr_dl *sdl;
sdl = (struct sockaddr_dl *)get_rt_address(rtm, RTA_IFP);
if (sdl) {
size_t len = sdl->sdl_nlen;
if (len > sizeof_ifname-1)
len = sizeof_ifname-1;
memcpy(ifname, sdl->sdl_data, len);
ifname[len] = 0;
return 0;
}
/*sin = (struct sockaddr_in *)get_rt_address(rtm, RTA_GATEWAY);
if (sin) {
*ipv4 = ntohl(sin->sin_addr.s_addr);
free(rtm);
return 0;
}*/
}
free(rtm);
return -1;
}
#elif defined(__linux__)
#include <netinet/in.h>
#include <net/if.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
struct route_info {
struct in_addr dstAddr;
struct in_addr srcAddr;
struct in_addr gateWay;
char ifName[IF_NAMESIZE];
};
static int read_netlink(int fd, char *bufPtr, size_t sizeof_buffer, int seqNum, int pId)
{
struct nlmsghdr *nlHdr;
int readLen = 0, msgLen = 0;
do {
/* Recieve response from the kernel */
if ((readLen = recv(fd, bufPtr, sizeof_buffer - msgLen, 0)) < 0) {
perror("SOCK READ: ");
return -1;
}
nlHdr = (struct nlmsghdr *) bufPtr;
/* Check if the header is valid */
if ((NLMSG_OK(nlHdr, readLen) == 0)
|| (nlHdr->nlmsg_type == NLMSG_ERROR)) {
perror("Error in recieved packet");
return -1;
}
/* Check if the its the last message */
if (nlHdr->nlmsg_type == NLMSG_DONE) {
break;
} else {
/* Else move the pointer to buffer appropriately */
bufPtr += readLen;
msgLen += readLen;
}
/* Check if its a multi part message */
if ((nlHdr->nlmsg_flags & NLM_F_MULTI) == 0) {
/* return if its not */
break;
}
} while ((nlHdr->nlmsg_seq != seqNum) || (nlHdr->nlmsg_pid != pId));
return msgLen;
}
/* For parsing the route info returned */
static int parseRoutes(struct nlmsghdr *nlHdr, struct route_info *rtInfo)
{
struct rtmsg *rtMsg;
struct rtattr *rtAttr;
int rtLen = 0;
rtMsg = (struct rtmsg *) NLMSG_DATA(nlHdr);
/* This must be an IPv4 (AF_INET) route */
if (rtMsg->rtm_family != AF_INET)
return 1;
/* This must be in main routing table */
if (rtMsg->rtm_table != RT_TABLE_MAIN)
return 1;
/* Attributes field*/
rtAttr = (struct rtattr *)RTM_RTA(rtMsg);
rtLen = RTM_PAYLOAD(nlHdr);
for (; RTA_OK(rtAttr, rtLen); rtAttr = RTA_NEXT(rtAttr, rtLen)) {
switch (rtAttr->rta_type) {
case RTA_OIF:
if_indextoname(*(int *) RTA_DATA(rtAttr), rtInfo->ifName);
break;
case RTA_GATEWAY:
rtInfo->gateWay.s_addr = *(u_int *)RTA_DATA(rtAttr);
break;
case RTA_PREFSRC:
rtInfo->srcAddr.s_addr = *(u_int *)RTA_DATA(rtAttr);
break;
case RTA_DST:
rtInfo->dstAddr .s_addr = *(u_int *)RTA_DATA(rtAttr);
break;
}
}
return 0;
}
int rawsock_get_default_interface(char *ifname, size_t sizeof_ifname)
{
int fd;
struct nlmsghdr *nlMsg;
char msgBuf[16384];
int len;
int msgSeq = 0;
unsigned ipv4 = 0;
/*
* Create 'netlink' socket to query kernel
*/
fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
if (fd < 0) {
fprintf(stderr, "%s:%d: socket(NETLINK_ROUTE): %d\n",
__FILE__, __LINE__, errno);
return errno;
}
/*
* format the netlink buffer
*/
memset(msgBuf, 0, sizeof(msgBuf));
nlMsg = (struct nlmsghdr *)msgBuf;
nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
nlMsg->nlmsg_type = RTM_GETROUTE;
nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
nlMsg->nlmsg_seq = msgSeq++;
nlMsg->nlmsg_pid = getpid();
/*
* send first request to kernel
*/
if (send(fd, nlMsg, nlMsg->nlmsg_len, 0) < 0) {
fprintf(stderr, "%s:%d: send(NETLINK_ROUTE): %d\n",
__FILE__, __LINE__, errno);
return errno;
}
/*
* Now read all the responses
*/
len = read_netlink(fd, msgBuf, sizeof(msgBuf), msgSeq, getpid());
if (len <= 0) {
fprintf(stderr, "%s:%d: read_netlink: %d\n",
__FILE__, __LINE__, errno);
return errno;
}
/*
* Parse the response
*/
for (; NLMSG_OK(nlMsg, len); nlMsg = NLMSG_NEXT(nlMsg, len)) {
struct route_info rtInfo[1];
int err;
memset(rtInfo, 0, sizeof(struct route_info));
err = parseRoutes(nlMsg, rtInfo);
if (err != 0)
continue;
/* make sure destination = 0.0.0.0 for "default route" */
if (rtInfo->dstAddr.s_addr != 0)
continue;
/* found the gateway! */
ipv4 = ntohl(rtInfo->gateWay.s_addr);
if (ipv4 == 0)
continue;
strcpy_s(ifname, sizeof_ifname, rtInfo->ifName);
}
close(fd);
return 0;
}
#endif
#if defined(WIN32)
#include <winsock2.h>
#include <iphlpapi.h>
#ifdef _MSC_VER
#pragma comment(lib, "IPHLPAPI.lib")
#endif
int rawsock_get_default_interface(char *ifname, size_t sizeof_ifname)
{
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD err;
ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
/*
* Allocate a proper sized buffer
*/
pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof (IP_ADAPTER_INFO));
if (pAdapterInfo == NULL) {
fprintf(stderr, "Error allocating memory needed to call GetAdaptersinfo\n");
return EFAULT;
}
/*
* Query the adapter info. If the buffer is not big enough, loop around
* and try again
*/
again:
err = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
if (err == ERROR_BUFFER_OVERFLOW) {
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);
if (pAdapterInfo == NULL) {
fprintf(stderr, "Error allocating memory needed to call GetAdaptersinfo\n");
return EFAULT;
}
goto again;
}
if (err != NO_ERROR) {
fprintf(stderr, "GetAdaptersInfo failed with error: %u\n", err);
return EFAULT;
}
/*
* loop through all adapters looking for ours
*/
for ( pAdapter = pAdapterInfo;
pAdapter;
pAdapter = pAdapter->Next) {
unsigned ipv4 = 0;
if (pAdapter->Type != MIB_IF_TYPE_ETHERNET)
continue;
/* See if this adapter has a default-route/gateway configured */
{
const IP_ADDR_STRING *addr;
for (addr = &pAdapter->GatewayList;
addr;
addr = addr->Next) {
struct Range range;
range = range_parse_ipv4(addr->IpAddress.String, 0, 0);
if (range.begin != 0 && range.begin == range.end) {
ipv4 = range.begin;
break;
}
}
}
/*
* When we reach the first adapter with an IP address, then
* we'll use that one
*/
if (ipv4) {
sprintf_s(ifname, sizeof_ifname, "\\Device\\NPF_%s", pAdapter->AdapterName);
}
}
if (pAdapterInfo)
free(pAdapterInfo);
return 0;
}
#endif