-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.c
109 lines (86 loc) · 2.36 KB
/
route.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
/*===============================================================
* 文件名称:route.c
* 创建日期:2016年08月03日
================================================================*/
#include <stdio.h>
static void writeRouteToLog()
{
char *cmd = "route -n > /tmp/route_ipv4.log";
char *cmdInet6 = "route -n -A inet6 > /tmp/route_ipv6.log";
system(cmd);
system(cmdInet6);
return;
}
#define MAX_CURRENT_ROUTE_NUM (32)
#define MAX_ROUTE_LEN (1024)
static int getRouteFromLog(char *vpFileName, unsigned int *vNum, char (*vppRouteArr)[MAX_ROUTE_LEN])
{
int ret = 0;
if((NULL == vpFileName) || (NULL == vNum) || (NULL == vppRouteArr))
{
printf("input para error!\n");
return -1;
}
unsigned int maxNum = *vNum;
*vNum = 0;
FILE *fp = NULL;
fp = fopen(vpFileName, "r");
if(NULL == fp)
{
printf("cant open file %s!\n", vpFileName);
return -1;
}
int lineNum = 0;
while(!feof(fp))
{
if(0x2 > lineNum)
{
lineNum ++;
continue;
}
fgets(vppRouteArr[*vNum], MAX_ROUTE_LEN, fp);
(*vNum) ++;
if(MAX_CURRENT_ROUTE_NUM < *vNum)
{
printf("route num is greater than 32!\n");
break;
}
}
fclose(fp);
return 0;
}
int main(void)
{
writeRouteToLog();
int ret;
char currentRouteArr[MAX_CURRENT_ROUTE_NUM][MAX_ROUTE_LEN] = {0};
unsigned int routeNum = MAX_CURRENT_ROUTE_NUM;
char *file = "/tmp/route_ipv4.log";
writeRouteToLog();
ret = getRouteFromLog(file, &routeNum, currentRouteArr);
if(0 != ret)
{
return ret;
}
unsigned int loop = 0;
for(loop=0; loop<routeNum; loop++)
{
printf("%d: %s", loop, ¤tRouteArr[loop]);
}
char DestIPAddress[32] = {0};
char DestSubnetMask[32] = {0};
char GatewayIPAddress[32] = {0};
char Interface[256] = {0};
sscanf((char *)¤tRouteArr[2],
"%[^ ] %[^ ] %[^ ] %*[^ ] %*[^ ] %*[^ ] %*[^ ] %[^\n]",
DestIPAddress,
DestSubnetMask,
GatewayIPAddress,
Interface);
printf("\nDestIPAddress=%s\nDestSubnetMask=%s\nGatewayIPAddress=%s\nInterface=%s\n",
DestIPAddress,
DestSubnetMask,
GatewayIPAddress,
Interface);
return 0;
}