-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrouter.c
223 lines (183 loc) · 4.65 KB
/
router.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
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
typedef unsigned long u_long;
#include <router.h>
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
static int _route_size = 0;
static struct route_item _route_table[2600];
static struct route_item *_route_get(in_addr_t prefix, in_addr_t _submask);
static int binary_search(const struct route_item table[], int start, int end, in_addr_t khey)
{
int mid;
while (start < end) {
mid = start + (end - start) / 2;
if (table[mid].prefix < khey) {
start = mid + 1;
} else if (table[mid].prefix > khey) {
end = mid;
} else {
return mid;
}
}
return start;
}
static int route_add(in_addr_t prefix, in_addr_t submask, in_addr_t nexthop)
{
int index;
in_addr_t premask = 0;
struct route_item *fib, *table = _route_table;
assert(_route_size < ARRAY_SIZE(_route_table));
index = binary_search(_route_table, 0, _route_size, prefix);
fib = _route_get(prefix, 0xffffffff);
if (fib != NULL && fib->submask < submask) premask = fib->submask;
fib = &table[index];
if (index < _route_size && fib->prefix == prefix) {
while (index < _route_size) {
fib = &table[index];
if (fib->prefix != prefix) {
break;
} else if (fib->submask == submask) {
return 0;
} else if (fib->submask < submask) {
premask = fib->submask;
index++;
} else {
break;
}
}
while (index > 0) {
fib = &table[index -1];
if (fib->prefix != prefix) {
break;
} else if (fib->submask == submask) {
return 0;
} else if (fib->submask > submask) {
index--;
} else {
break;
}
}
}
memmove(&table[index + 1], &table[index], (_route_size - index) * sizeof(table[0]));
table[index].submask = submask;
table[index].nexthop = nexthop;
table[index].premask = premask;
table[index].prefix = prefix;
_route_size++;
for (index++; index < _route_size; index++) {
fib = &table[index];
if (fib->submask < submask) {
break;
} else if ((fib->prefix & submask) != prefix) {
break;
} else if (fib->premask < submask) {
fib->premask = submask;
}
}
return 0;
}
void route_restore(const char *route)
{
int length = 0;
char sprefix[16] = {};
const char *ptr = NULL;
for (ptr = route - 1; ptr; ptr = strchr(ptr +1, ' ')) {
if (2 == sscanf(ptr +1, "%[0-9.]/%d", sprefix, &length)) {
in_addr_t prefix = inet_addr(sprefix);
route_add(htonl(prefix), ~0 << (32 - length), INADDR_LOOPBACK);
}
}
return;
}
static struct route_item *_route_get(in_addr_t prefix, in_addr_t _submask)
{
int index;
in_addr_t submask;
struct route_item *fib, *table = _route_table;
index = binary_search(table, 0, _route_size, prefix & _submask);
if (index < _route_size &&
(table[index].prefix == (prefix & table[index].submask))) {
while (index + 1 < _route_size) {
if (table[index +1].prefix
!= (prefix & table[index +1].submask)) {
break;
}
index++;
}
return &_route_table[index];
}
if (index-- == 0) {
return NULL;
}
fib = &table[index];
submask = fib->submask;
if ((submask & prefix) == fib->prefix) {
return fib;
} else if (fib->premask) {
assert(fib->premask < _submask);
return _route_get(prefix, fib->prefix);
}
return NULL;
}
const struct route_item *route_get(struct in_addr target)
{
#if 0
printf("\n-------------------------\n");
for (int i = 0; i < _route_size; i++) {
struct route_item *item = &_route_table[i];
printf("%d %08x %08x %08x %08x\n", i, item->prefix, item->submask, item->premask, item->nexthop);
}
printf("-------------------------\n");
#endif
return _route_get(htonl(target.s_addr), ~0);
}
int route_cmd(const char *route)
{
int length = 0;
char sprefix[16] = {};
char sgateway[16] = {};
if (3 == sscanf(route, "%16[0-9.]/%d@%16s", sprefix, &length, sgateway)) {
in_addr_t prefix = inet_addr(sprefix);
in_addr_t gateway = inet_addr(sgateway);
route_add(htonl(prefix), ~0 << (32 - length), htonl(gateway));
return 0;
}
return -1;
}
static int _log_fake_init = 0;
static const char *BLOCK_ROUTE = NULL;
void log_fake_route(uint8_t *fakeip)
{
struct in_addr ip;
u_long dest;
if (_log_fake_init == 0) {
BLOCK_ROUTE = getenv("BLOCK_ROUTE");
_log_fake_init = 1;
}
if (BLOCK_ROUTE == NULL) {
return;
}
memcpy(&ip.s_addr, fakeip, sizeof(ip.s_addr));
dest = htonl(ip.s_addr);
if (route_get(ip) != NULL) {
return;
}
in_addr_t prefix = (dest & ~0xff);
route_add(prefix, 0xffffff00, 0x08080808);
FILE *froute = fopen(BLOCK_ROUTE, "a");
if (froute != NULL) {
fprintf(froute, "%s/24\n", inet_ntoa(ip));
fclose(froute);
}
return;
}