simple iptables based packet limiter to deflect DoS attacks on our clients
[svn/Prometheus-QoS/.git] / parsehosts.c
CommitLineData
cdfd88eb 1/* Modified by: xChaos, 20131029 */\r
1c9cae56 2\r
3#include "cll1-0.6.2.h"\r
4#include "ipstruct.h"\r
5\r
6#define FIRSTGROUPID 1024\r
7#define FIRSTIPCLASS 2048\r
8\r
9/* globals declared in prometheus.c */\r
af37be1d 10extern struct IP *ips, *ip, *sharedip, *networks;\r
1c9cae56 11extern struct Group *groups, *group;\r
12extern struct Keyword *keyword, *defaultkeyword, *keywords;\r
13extern int class_count;\r
14extern int ip_count;\r
15extern int found_lmsid;\r
16extern int free_min;\r
a1204fc9 17extern const int highest_priority;\r
f64d5431 18extern char *ip6prefix;\r
1c9cae56 19\r
2b460b72 20void update_network(char *look_for, struct IP* ip);\r
af37be1d 21/* implemented in networks.c */\r
22\r
a1204fc9 23/* This must be object oriented! This looks almost like constructor ;-) */\r
af37be1d 24void TheIP(char *ipaddr, int is_network)\r
a1204fc9 25{\r
26 create(ip,IP);\r
27 ip->name = "";\r
f19d3cd0 28 ip->addr = ipaddr;\r
a1204fc9 29 ip->sharing = NULL;\r
30 ip->prio = highest_priority+1;\r
31 ip->lmsid = -1;\r
32 ip->fixedprio = \\r
33 ip->mark = \\r
34 ip->min = \\r
35 ip->max = \\r
36 ip->desired = \\r
37 ip->credit = \\r
38 ip->upload = \\r
39 ip->proxy = \\r
40 ip->direct = \\r
41 ip->traffic = \\r
42 ip->pktsup = \\r
c38473c1 43 ip->pps_limit = \\r
a1204fc9 44 ip->pktsdown = 0;\r
45 ip->keyword = keywords;\r
0b9c3c19 46 ip->v6 = (strchr(ip->addr,':')!=NULL);\r
af37be1d 47 ip->mask = ((ip->v6)?64:32);\r
48 if(is_network)\r
49 {\r
50 push(ip, networks);\r
51 }\r
52 else\r
53 {\r
54 push(ip, ips); \r
55 }\r
14e28c6f 56 ip_count++;\r
a1204fc9 57}\r
1c9cae56 58\r
14e28c6f 59struct IP *lastIP6;\r
60\r
1c9cae56 61/* == This function strips extra characters after IPv4 address and stores it = */\r
af37be1d 62void parse_and_append_ip(char *str, struct IP *listhead)\r
1c9cae56 63{\r
f64d5431 64 char *ptr, *ipaddr, *ip6range = NULL, *ipname = NULL, *lmsid = NULL;\r
65\r
b1a5c883 66 if(ip6prefix) /* Try this only if IPv6 subsystem is active... */\r
f64d5431 67 {\r
68 ptr = strstr(str, "::");\r
69 if(ptr && ptr-str > 4)\r
70 {\r
71 ptr -= 4; \r
ca6f7e80 72 duplicate(ptr, ip6range);\r
f64d5431 73 ptr = strstr(ip6range, "::");\r
74 if(ptr)\r
75 {\r
76 *(ptr+2) = 0;\r
77 }\r
78 }\r
79 }\r
1c9cae56 80\r
81 ptr = strchr(str, '{');\r
82 if(ptr)\r
83 {\r
84 lmsid = ++ptr;\r
85 while(*ptr and *ptr != '}')\r
86 {\r
87 ptr++;\r
88 }\r
89 *ptr = 0;\r
90 }\r
91 \r
92 ptr = str;\r
93 while(*ptr and *ptr!=' ' and *ptr!=9)\r
94 {\r
95 ptr++;\r
96 }\r
97 \r
98 *ptr = 0;\r
99 ipaddr = str;\r
100 ptr++;\r
101 while(*ptr and (*ptr==' ' or *ptr==9))\r
102 {\r
103 ptr++;\r
104 }\r
105 ipname=ptr; \r
106 while(*ptr and *ptr!=' ' and *ptr!=9)\r
107 {\r
108 ptr++;\r
109 }\r
110 *ptr=0;\r
111\r
f64d5431 112 if(ip6range)\r
113 {\r
114 concatenate(ip6prefix,ip6range,ptr);\r
0b9c3c19 115 ip6range=ptr;\r
f64d5431 116 if_exists(ip, ips, eq(ip->addr,ip6range));\r
117 else\r
118 {\r
af37be1d 119 TheIP(ip6range, FALSE);\r
f64d5431 120 }\r
0b9c3c19 121 ip->name = ip6range;\r
14e28c6f 122 ip->keyword = defaultkeyword; /* settings for default keyword */\r
f64d5431 123 if(lmsid)\r
124 {\r
125 ip->lmsid = atoi(lmsid);\r
126 }\r
14e28c6f 127 lastIP6 = ip;\r
128 }\r
129 else\r
130 {\r
131 lastIP6 = NULL;\r
f64d5431 132 }\r
133\r
af37be1d 134 if_exists(ip, listhead, eq(ip->addr,ipaddr));\r
1c9cae56 135 else\r
136 {\r
af37be1d 137 TheIP(ipaddr, (listhead==networks));\r
1c9cae56 138 }\r
1c9cae56 139 ip->name = ipname;\r
140 if(lmsid)\r
141 {\r
142 ip->lmsid = atoi(lmsid);\r
143 found_lmsid = TRUE;\r
144 }\r
145}\r
146\r
147/* == This function parses hosts style main configuration file == */\r
148void parse_hosts(char *hosts)\r
149{\r
150 int groupidx = FIRSTGROUPID;\r
151 char *str, *ptr;\r
152 char *substring;\r
af37be1d 153 struct IP *network;\r
1c9cae56 154\r
155 parse(hosts)\r
156 {\r
157 str=_;\r
158\r
159 if(*str < '0' or *str > '9')\r
160 {\r
161 /* any line starting with non-number is comment ...*/\r
162 continue;\r
163 }\r
164 \r
165 /* Does this IP share QoS class with some other ? */\r
166 substring = strstr(str, "sharing-");\r
167 if(substring)\r
168 { \r
169 substring += 8; /* "sharing-" */\r
af37be1d 170 parse_and_append_ip(str, ips);\r
1c9cae56 171 ip->sharing = substring;\r
172 ip->keyword = defaultkeyword; /* settings for default keyword */\r
14e28c6f 173 if(lastIP6)\r
174 {\r
175 lastIP6->sharing = substring;\r
b1a5c883 176 lastIP6 = NULL;\r
14e28c6f 177 }\r
1c9cae56 178 while(*substring and *substring != '\n')\r
179 {\r
180 substring++;\r
181 }\r
182 *substring = 0; \r
183 }\r
184 else\r
185 {\r
af37be1d 186 substring = strstr(str, "#255.");\r
187 if(substring and not strstr(str, "#255.255.255.255")) /* do not ping /32 ranges */\r
1c9cae56 188 {\r
af37be1d 189 /* netmask detected - save network*/\r
190 unsigned bit;\r
191 unsigned num, mask = 8;\r
192 substring += 5;\r
193 while(substring && *substring)\r
1c9cae56 194 {\r
af37be1d 195 ptr = substring;\r
196 substring = strchr(substring, '.');\r
197 if(substring)\r
198 {\r
199 *substring = 0;\r
200 substring += 1;\r
201 }\r
202 num = atoi(ptr);\r
203 for(bit = 1; bit <=128 ; bit<<=1)\r
1c9cae56 204 {\r
af37be1d 205 if(bit & num)\r
206 {\r
207 mask++;\r
208 }\r
1c9cae56 209 }\r
af37be1d 210 } \r
211 parse_and_append_ip(str, networks);\r
212 ip->mask = mask;\r
213 }\r
214 else\r
215 {\r
216 /*Do we have to create new QoS class for this IP ? */\r
217 if_exists(keyword,keywords,(substring=strstr(str,keyword->key)))\r
1c9cae56 218 {\r
af37be1d 219 parse_and_append_ip(str, ips);\r
220 if(lastIP6)\r
221 {\r
222 lastIP6->sharing = ip->name;\r
223 lastIP6 = NULL;\r
224 }\r
225 ip->keyword = keyword;\r
226 keyword->ip_count++;\r
227 ip->prio = keyword->default_prio;\r
228 substring += strlen(keyword->key)+1;\r
229 ptr = substring;\r
230 while(*ptr and *ptr != '-')\r
231 {\r
232 ptr++;\r
233 }\r
234 if(*ptr == '-')\r
235 {\r
236 *ptr=0;\r
237 ip->max = ip->desired = atoi(ptr+1);\r
238 }\r
9694a8ec 239\r
af37be1d 240 ip->min = atoi(substring);\r
241 if(ip->min <= 0)\r
242 {\r
243 printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kb/s\n",\r
244 str, free_min);\r
245 ip->min = free_min;\r
246 }\r
9694a8ec 247\r
af37be1d 248 if(ip->max <= ip->min)\r
249 {\r
250 ip->fixedprio = TRUE;\r
251 ip->max = ip->min + ip->keyword->reserve_min;\r
252 }\r
253 else \r
254 {\r
255 ip->max -= ip->keyword->reserve_max;\r
c38473c1 256 if(ip->max < ip->min)\r
af37be1d 257 {\r
c38473c1 258 ip->max = ip->min;\r
af37be1d 259 }\r
260 }\r
c38473c1 261 \r
262 /* MTU is 1450 bytes = 11600 bits ~= 12 kbit, max is in kb/s */\r
263 ip->pps_limit = ip->max/12;\r
9694a8ec 264 ip->mark = FIRSTIPCLASS+1+class_count++;\r
2b460b72 265 update_network(ip->addr, ip);\r
af37be1d 266\r
267 if_exists(group,groups,(group->min == ip->min)) \r
268 { \r
269 group->count++; \r
270 group->desired += ip->min;\r
271 ip->group = group->id; \r
272 }\r
273 else\r
274 {\r
275 create(group,Group);\r
276 group->min = ip->min;\r
277 group->id = groupidx++;\r
278 ip->group = group->id;\r
279\r
280 if(group->min < 8) group->min = 8;\r
281 /* Warning - this is maybe because of primitive tc namespace, can be fixed */\r
282 /* it is because class IDs are derived from min. bandwidth. - xCh */\r
283 //if(group->min>MAX_GUARANTED_KBPS) group->min=MAX_GUARANTED_KBPS;\r
284 \r
285 group->count = 1;\r
286 group->desired = ip->min; \r
287 insert(group, groups, desc_order_by,min);\r
288 }\r
289 }//endif keyword-\r
290 }//endif netmask\r
1c9cae56 291 }//endif sharing-\r
292 }\r
293 fail\r
294 {\r
295 perror(hosts);\r
296 exit(-1);\r
297 }\r
298 done; /* ugly macro end */\r
9694a8ec 299// TheIP("0.0.0.0", TRUE);\r
300// ip->name = "TOTAL";\r
301// ip->mask = 0;\r
1c9cae56 302}
This page took 0.341987 seconds and 4 git commands to generate.