-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetroutes.c
189 lines (159 loc) · 5.01 KB
/
getroutes.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
/*
* This file is part of the sockstress project.
*
* Copyright (C) 2008-2013 Outpost24 AB
* (Written by Jack C. Louis for Outpost24 AB)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "ext/xmalloc.h"
#include "arp.h"
#include "ext/patricia_trie/patricia.h"
static void get_netroutes(void);
static int masktocidr(uint32_t );
static int need_netroutes=1;
static patricia_tree_t *rt=NULL;
static patricia_node_t *node=NULL;
typedef union route_info_t {
struct info_s {
char *intf;
uint16_t metric;
uint16_t flags;
struct sockaddr_storage gw;
} *info_s;
void *p;
} route_info_t;
int getroutes(char **intf, struct sockaddr *tgt, struct sockaddr **gw) {
static char lookup[128];
route_info_t ri_u;
sa_u ts_u, gws_u;
static struct sockaddr_storage gw_s;
assert(intf != NULL && tgt != NULL && gw != NULL);
ts_u.s=tgt;
*gw=NULL;
switch (ts_u.fs->family) {
case AF_INET:
sprintf(lookup, "%s/32", INT_NTOA(ts_u.sin->sin_addr.s_addr));
break;
default:
ERR("unknown address family `%d'", ts_u.fs->family);
return -1;
}
if (need_netroutes) {
get_netroutes();
}
node=try_search_best(rt, lookup);
if (node == NULL) {
ERR("bad luck");
*intf=NULL;
*gw=NULL;
return -1;
}
ri_u.p=node->data;
assert(node->data != NULL);
DBG("found interface `%s' for network `%s'", ri_u.info_s->intf, lookup);
*intf=ri_u.info_s->intf;
if (ri_u.info_s->gw.ss_family != 0) {
memcpy(&gw_s, &ri_u.info_s->gw, sizeof(struct sockaddr_storage));
gws_u.ss=&gw_s;
*gw=gws_u.s;
}
else {
*gw=NULL;
}
return 1;
}
static int masktocidr(uint32_t mask) {
int j=0, cidr=0;
/* endian */
for (j=0; j < 32; j++) {
if ((mask & 0x80000000) == 0x80000000) {
cidr++;
}
mask <<= 1;
}
return cidr;
}
static void get_netroutes(void) {
FILE *pnr=NULL;
char lbuf[1024], intf[32];
uint32_t dest, gw, refcnt, use, mask, irtt;
uint16_t metric, flags, window, mtu;
char destnet[128], gwstr[128], addstr[128];
int lineno=0;
pnr=fopen("/proc/net/route", "r");
if (pnr == NULL) {
ERR("cant open /proc/net/route: `%s'", strerror(errno));
exit(1);
}
rt=New_Patricia(128);
/*
* Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
* eth1 0045A8C0 00000000 0001 0 0 0 00FFFFFF 0 0 0
*/
for (lineno=0; fgets(lbuf, sizeof(lbuf) -1, pnr) != NULL; lineno++) {
if (lineno == 0) {
continue;
}
#if 0
#define RTF_UP 0x0001 /* route usable */
#define RTF_GATEWAY 0x0002 /* destination is a gateway */
#define RTF_HOST 0x0004 /* host entry (net otherwise) */
#define RTF_REINSTATE 0x0008 /* reinstate route after tmout */
#define RTF_DYNAMIC 0x0010 /* created dyn. (by redirect) */
#define RTF_MODIFIED 0x0020 /* modified dyn. (by redirect) */
#define RTF_MTU 0x0040 /* specific MTU for this route */
#define RTF_MSS RTF_MTU /* Compatibility :-( */
#define RTF_WINDOW 0x0080 /* per route window clamping */
#define RTF_IRTT 0x0100 /* Initial round trip time */
#define RTF_REJECT 0x0200 /* Reject route */
#endif
/* in de gw fl ref us me ma mt wi ir */
if (sscanf(lbuf, "%31s %x %x %hx %u %u %hu %x %hu %hu %u", intf, &dest, &gw, &flags, &refcnt, &use, &metric, &mask, &mtu, &window, &irtt) == 11) {
int mycidr=0;
strcpy(destnet, INT_NTOA(dest));
mycidr=masktocidr(mask);
strcpy(gwstr, INT_NTOA(gw));
if (flags & RTF_UP && mycidr > -1) {
sa_u s_u;
route_info_t ri_u;
ri_u.p=xmalloc(sizeof(*ri_u.info_s));
memset(ri_u.p, 0, sizeof(*ri_u.info_s));
ri_u.info_s->intf=xstrdup(intf);
ri_u.info_s->metric=metric; /* could only be 0xff anyhow */
ri_u.info_s->flags=flags;
if ((flags & RTF_GATEWAY) == RTF_GATEWAY) {
s_u.ss=&ri_u.info_s->gw;
s_u.sin->sin_addr.s_addr=gw;
s_u.sin->sin_family=AF_INET;
}
sprintf(addstr, "%s/%d", destnet, mycidr);
DBG("net %s via %s metric %u", addstr, (flags & RTF_GATEWAY) == 0 ? intf : gwstr, metric);
node=make_and_lookup(rt, addstr);
if (node == NULL) {
exit(1);
}
node->data=ri_u.p;
}
}
else {
ERR("can not parse `%s'", lbuf);
}
}
fclose(pnr);
need_netroutes=0;
return;
}