This repository has been archived by the owner on Mar 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routetable.c
138 lines (127 loc) · 3.82 KB
/
routetable.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
/*
* Copyright 2009 Christopher Breneman
*
* This file is part of ClueVPN.
*
* ClueVPN 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 3 of the License, or
* (at your option) any later version.
*
* ClueVPN 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 ClueVPN. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "routetable.h"
routetable_t ipv4routetable;
routetable_t ipv6routetable;
int routetable_init(routetable_t *rt) {
rt->root = malloc(sizeof(struct routetable_trienode));
rt->root->hostid = -1;
rt->root->zerobranch = NULL;
rt->root->onebranch = NULL;
return ROUTETABLE_OK;
}
void routetable_cleanupnode(struct routetable_trienode *node) {
if(node->zerobranch) routetable_cleanupnode(node->zerobranch);
if(node->onebranch) routetable_cleanupnode(node->onebranch);
free(node);
}
void routetable_cleanup(routetable_t *rt) {
routetable_cleanupnode(rt->root);
rt->root = NULL;
}
int routetable_clear(routetable_t *rt) {
routetable_cleanup(rt);
return routetable_init(rt);
}
int routetable_addroute(routetable_t *rt, void *subnet_vp, unsigned char cidr, int hostid) {
struct routetable_trienode *cnode = rt->root;
unsigned char *subnet = (unsigned char *)subnet_vp;
int cbit;
char bitval;
for(cbit = 0; cbit < cidr; cbit++) {
bitval = (subnet[cbit / 8] & (0x80 >> (cbit % 8))) ? 1 : 0;
if(!bitval) {
if(!cnode->zerobranch) {
cnode->zerobranch = malloc(sizeof(struct routetable_trienode));
cnode->zerobranch->zerobranch = NULL;
cnode->zerobranch->onebranch = NULL;
cnode->zerobranch->hostid = -1;
}
cnode = cnode->zerobranch;
} else {
if(!cnode->onebranch) {
cnode->onebranch = malloc(sizeof(struct routetable_trienode));
cnode->onebranch->zerobranch = NULL;
cnode->onebranch->onebranch = NULL;
cnode->onebranch->hostid = -1;
}
cnode = cnode->onebranch;
}
}
cnode->hostid = hostid;
return ROUTETABLE_OK;
}
int routetable_getroute(routetable_t *rt, void *ip_vp, unsigned char addrbits, int *hostid) {
struct routetable_trienode *cnode = rt->root;
unsigned char *ip = (unsigned char *)ip_vp;
int cbit;
char bitval;
*hostid = -1;
for(cbit = 0; cbit < addrbits; cbit++) {
if(cnode->hostid >= 0) *hostid = cnode->hostid;
bitval = (ip[cbit / 8] & (0x80 >> (cbit % 8))) ? 1 : 0;
if(!bitval) {
if(!cnode->zerobranch) break;
cnode = cnode->zerobranch;
} else {
if(!cnode->onebranch) break;
cnode = cnode->onebranch;
}
}
if(cnode->hostid >= 0) *hostid = cnode->hostid;
if(*hostid < 0) return ROUTETABLE_NOROUTE;
return ROUTETABLE_OK;
}
/*
void _routetable_test() {
unsigned char caddr[4];
unsigned char ccidr;
int chostid;
int p1, p2, p3, p4, p5, p6;
routetable_t rt;
routetable_init(&rt);
while(1) {
printf("Enter X.X.X.X/X:X to add an address OR X.X.X.X/-1:X to break:\n");
scanf("%d.%d.%d.%d/%d:%d", &p1, &p2, &p3, &p4, &p5, &p6);
if(p5 == -1) break;
caddr[0] = p1;
caddr[1] = p2;
caddr[2] = p3;
caddr[3] = p4;
ccidr = p5;
chostid = p6;
routetable_addroute(&rt, (void *)caddr, ccidr, chostid);
printf("Added %d.%d.%d.%d/%d to host %d\n", p1, p2, p3, p4, p5, p6);
}
while(1) {
printf("Enter X.X.X.X to route an address OR -1.X.X.X to break:\n");
scanf("%d.%d.%d.%d", &p1, &p2, &p3, &p4);
if(p1 == -1) break;
caddr[0] = p1;
caddr[1] = p2;
caddr[2] = p3;
caddr[3] = p4;
routetable_getroute(&rt, (void *)caddr, 32, &chostid);
printf("Routed to host: %d\n", chostid);
}
}
*/