-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathGetRouteTable_BySysctl.c
106 lines (71 loc) · 2.13 KB
/
GetRouteTable_BySysctl.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
#include <stdio.h>
#include <unistd.h>
#include <sys/sysctl.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/route.h>
#include "../../tool/AddrTool.h"
#define ROUNDUP(a, size) ((a) & ((size) - 1) ? (1 + (a) | ((size) - 1)) : (a))
#define NEXT_SA(sa) sa = (struct sockaddr *)((char *)sa + (sa->sa_len ? ROUNDUP(sa->sa_len, sizeof(long)) : sizeof(long)))
void getrti_addrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
{
for (int i = 0; i < RTAX_MAX; i++) {
if (addrs & (1 << i)) {
rti_info[i] = sa;
NEXT_SA(sa);
} else
rti_info[i] = NULL;
}
}
int main(int argc, char const *argv[])
{
int mib[6];
mib[0] = CTL_NET;
mib[1] = AF_ROUTE;
mib[2] = 0;
mib[3] = AF_INET;
mib[4] = NET_RT_DUMP;
size_t len = 0;
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
perror("sysctl error!");
exit(1);
}
printf("all size = %zd\n", len);
char *buf = NULL;
if ((buf = malloc(len)) == NULL) {
printf("malloc error!");
exit(1);
}
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
perror("sysctl error!");
exit(1);
}
struct sockaddr *rti_info[RTAX_MAX];
char *ptr = buf;
char *lim = buf + len;
struct rt_msghdr *rtm;
for (ptr = buf; ptr < lim; ptr += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)ptr;
if (rtm->rtm_type != RTM_GET) {
printf("unknow rtm_type = %d\n", rtm->rtm_type);
continue;
}
struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
getrti_addrs(rtm->rtm_addrs, sa, rti_info);
if ((sa = rti_info[RTAX_DST]) != NULL) {
printf("%s", getAddrInfo(sa));
if ((sa = rti_info[RTAX_GATEWAY]) != NULL) {
printf("\t%s", getAddrInfo(sa));
} else
printf("\t");
if ((sa = rti_info[RTAX_NETMASK]) != NULL) {
printf("\t%s", getMaskInfo(sa));
} else
printf("\t");
printf("\n");
}
}
return 0;
}