more IPs allowed in identical IPv6 uplink network
[svn/Prometheus-QoS/.git] / parsehosts.c
CommitLineData
8dcd2b4c 1/* Modified by: xChaos, 20131220 */\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
bb5e7385 13extern struct Macro *macro, *macros;\r
1c9cae56 14extern int class_count;\r
15extern int ip_count;\r
16extern int found_lmsid;\r
17extern int free_min;\r
a1204fc9 18extern const int highest_priority;\r
f64d5431 19extern char *ip6prefix;\r
1c9cae56 20\r
2b460b72 21void update_network(char *look_for, struct IP* ip);\r
af37be1d 22/* implemented in networks.c */\r
23\r
a1204fc9 24/* This must be object oriented! This looks almost like constructor ;-) */\r
af37be1d 25void TheIP(char *ipaddr, int is_network)\r
a1204fc9 26{\r
27 create(ip,IP);\r
28 ip->name = "";\r
f19d3cd0 29 ip->addr = ipaddr;\r
a1204fc9 30 ip->sharing = NULL;\r
31 ip->prio = highest_priority+1;\r
32 ip->lmsid = -1;\r
33 ip->fixedprio = \\r
34 ip->mark = \\r
35 ip->min = \\r
36 ip->max = \\r
37 ip->desired = \\r
38 ip->credit = \\r
39 ip->upload = \\r
40 ip->proxy = \\r
41 ip->direct = \\r
42 ip->traffic = \\r
43 ip->pktsup = \\r
c38473c1 44 ip->pps_limit = \\r
a1204fc9 45 ip->pktsdown = 0;\r
46 ip->keyword = keywords;\r
0b9c3c19 47 ip->v6 = (strchr(ip->addr,':')!=NULL);\r
af37be1d 48 ip->mask = ((ip->v6)?64:32);\r
49 if(is_network)\r
50 {\r
51 push(ip, networks);\r
52 }\r
53 else\r
54 {\r
55 push(ip, ips); \r
56 }\r
14e28c6f 57 ip_count++;\r
a1204fc9 58}\r
1c9cae56 59\r
64b2d125 60struct IP *lastIP6range, *lastIP6uplink;\r
14e28c6f 61\r
1c9cae56 62/* == This function strips extra characters after IPv4 address and stores it = */\r
af37be1d 63void parse_and_append_ip(char *str, struct IP *listhead)\r
1c9cae56 64{\r
64b2d125 65 char *ptr, *ipaddr, *nextip6, *ip6buf; \r
66 char *ip6uplink = NULL, *ip6range = NULL, *ipname = NULL, *lmsid = NULL;\r
f64d5431 67\r
b1a5c883 68 if(ip6prefix) /* Try this only if IPv6 subsystem is active... */\r
f64d5431 69 {\r
70 ptr = strstr(str, "::");\r
64b2d125 71 while(ptr && ptr-str > 4)\r
f64d5431 72 {\r
64b2d125 73 nextip6 = strstr(ptr + 2, "::");\r
74 ptr -= 4;\r
75 duplicate(ptr, ip6buf);\r
76 ptr = strstr(ip6buf, "::");\r
f64d5431 77 if(ptr)\r
78 {\r
64b2d125 79 if(*(ptr+2) == '+')\r
80 {\r
81 *(ptr+3) = 0; /* ends with ::+ */\r
82 ip6uplink = ip6buf;\r
83 }\r
84 else\r
85 {\r
86 *(ptr+2) = 0; /* ends with :: */\r
87 ip6range = ip6buf;\r
88 } \r
f64d5431 89 }\r
64b2d125 90 ptr = nextip6;\r
f64d5431 91 }\r
92 }\r
1c9cae56 93\r
94 ptr = strchr(str, '{');\r
95 if(ptr)\r
96 {\r
97 lmsid = ++ptr;\r
98 while(*ptr and *ptr != '}')\r
99 {\r
100 ptr++;\r
101 }\r
102 *ptr = 0;\r
103 }\r
104 \r
105 ptr = str;\r
106 while(*ptr and *ptr!=' ' and *ptr!=9)\r
107 {\r
108 ptr++;\r
109 }\r
110 \r
111 *ptr = 0;\r
112 ipaddr = str;\r
113 ptr++;\r
114 while(*ptr and (*ptr==' ' or *ptr==9))\r
115 {\r
116 ptr++;\r
117 }\r
64b2d125 118 ipname = ptr; \r
1c9cae56 119 while(*ptr and *ptr!=' ' and *ptr!=9)\r
120 {\r
121 ptr++;\r
122 }\r
123 *ptr=0;\r
124\r
f64d5431 125 if(ip6range)\r
126 {\r
127 concatenate(ip6prefix,ip6range,ptr);\r
0b9c3c19 128 ip6range=ptr;\r
67120c62 129 if_exists(ip, ips, eq(ip->addr,ip6range)); /* check - allocated range must be unique */\r
f64d5431 130 else\r
131 {\r
af37be1d 132 TheIP(ip6range, FALSE);\r
f64d5431 133 }\r
0b9c3c19 134 ip->name = ip6range;\r
14e28c6f 135 ip->keyword = defaultkeyword; /* settings for default keyword */\r
f64d5431 136 if(lmsid)\r
137 {\r
138 ip->lmsid = atoi(lmsid);\r
139 }\r
64b2d125 140 lastIP6range = ip;\r
141 }\r
142 else\r
143 {\r
144 lastIP6range = NULL;\r
145 }\r
146\r
147 /* it is ugly to copy+paste and search+replace, but... */\r
148 if(ip6uplink)\r
149 {\r
150 concatenate(ip6prefix,ip6uplink,ptr);\r
151 ip6uplink=ptr;\r
67120c62 152 TheIP(ip6uplink, FALSE); /* always new IP - more IPs in single uplink network */\r
64b2d125 153 ip->name = ip6uplink;\r
154 ip->keyword = defaultkeyword; /* settings for default keyword */\r
155 if(lmsid)\r
156 {\r
157 ip->lmsid = atoi(lmsid);\r
158 }\r
159 lastIP6uplink = ip;\r
14e28c6f 160 }\r
161 else\r
162 {\r
64b2d125 163 lastIP6uplink = NULL;\r
f64d5431 164 }\r
165\r
af37be1d 166 if_exists(ip, listhead, eq(ip->addr,ipaddr));\r
1c9cae56 167 else\r
168 {\r
af37be1d 169 TheIP(ipaddr, (listhead==networks));\r
1c9cae56 170 }\r
1c9cae56 171 ip->name = ipname;\r
172 if(lmsid)\r
173 {\r
174 ip->lmsid = atoi(lmsid);\r
175 found_lmsid = TRUE;\r
176 }\r
177}\r
178\r
179/* == This function parses hosts style main configuration file == */\r
180void parse_hosts(char *hosts)\r
181{\r
182 int groupidx = FIRSTGROUPID;\r
183 char *str, *ptr;\r
184 char *substring;\r
af37be1d 185 struct IP *network;\r
73cf6e9d 186 int pktratio;\r
1c9cae56 187\r
188 parse(hosts)\r
189 {\r
bb5e7385 190 str = _;\r
1c9cae56 191\r
192 if(*str < '0' or *str > '9')\r
193 {\r
194 /* any line starting with non-number is comment ...*/\r
195 continue;\r
196 }\r
8dcd2b4c 197\r
198 ptr = strchr(str,'\r'); /* fore unix-style end of line */\r
199 if(ptr)\r
200 {\r
201 *ptr = 0;\r
202 }\r
203 \r
bb5e7385 204 /* first, expand (rewrite) any predefined macros, if found*/\r
205 for_each(macro, macros)\r
206 {\r
207 substring = strstr(str, macro->rewrite_from);\r
8dcd2b4c 208 if(substring)\r
bb5e7385 209 {\r
210 int l1, l3;\r
211 *substring = 0;\r
212 substring += strlen(macro->rewrite_from);\r
213 l1 = strlen(str);\r
214 l3 = strlen(substring);\r
8dcd2b4c 215 string(ptr, l1 + strlen(macro->rewrite_to) + l3 + 1);\r
bb5e7385 216 strcpy(ptr, str);\r
217 strcat(ptr, macro->rewrite_to);\r
218 strcat(ptr, substring);\r
219 str = ptr;\r
8dcd2b4c 220 /* printf("REWRITE: %s -> %s\n",_,str); */\r
bb5e7385 221 }\r
222 }\r
223\r
1c9cae56 224 /* Does this IP share QoS class with some other ? */\r
225 substring = strstr(str, "sharing-");\r
226 if(substring)\r
227 { \r
228 substring += 8; /* "sharing-" */\r
af37be1d 229 parse_and_append_ip(str, ips);\r
1c9cae56 230 ip->sharing = substring;\r
231 ip->keyword = defaultkeyword; /* settings for default keyword */\r
64b2d125 232 if(lastIP6range)\r
233 {\r
234 lastIP6range->sharing = substring;\r
235 lastIP6range = NULL;\r
236 }\r
237 if(lastIP6uplink)\r
14e28c6f 238 {\r
64b2d125 239 lastIP6uplink->sharing = substring;\r
240 lastIP6uplink = NULL;\r
14e28c6f 241 }\r
1c9cae56 242 while(*substring and *substring != '\n')\r
243 {\r
244 substring++;\r
245 }\r
246 *substring = 0; \r
247 }\r
248 else\r
249 {\r
af37be1d 250 substring = strstr(str, "#255.");\r
64b2d125 251 if(substring and not strstr(str, "#255.255.255.255")) /* do not ping /32 uplinks */\r
1c9cae56 252 {\r
af37be1d 253 /* netmask detected - save network*/\r
254 unsigned bit;\r
255 unsigned num, mask = 8;\r
256 substring += 5;\r
257 while(substring && *substring)\r
1c9cae56 258 {\r
af37be1d 259 ptr = substring;\r
260 substring = strchr(substring, '.');\r
261 if(substring)\r
262 {\r
263 *substring = 0;\r
264 substring += 1;\r
265 }\r
266 num = atoi(ptr);\r
267 for(bit = 1; bit <=128 ; bit<<=1)\r
1c9cae56 268 {\r
af37be1d 269 if(bit & num)\r
270 {\r
271 mask++;\r
272 }\r
1c9cae56 273 }\r
af37be1d 274 } \r
275 parse_and_append_ip(str, networks);\r
276 ip->mask = mask;\r
277 }\r
278 else\r
279 {\r
280 /*Do we have to create new QoS class for this IP ? */\r
281 if_exists(keyword,keywords,(substring=strstr(str,keyword->key)))\r
1c9cae56 282 {\r
af37be1d 283 parse_and_append_ip(str, ips);\r
64b2d125 284 if(lastIP6range)\r
285 {\r
286 lastIP6range->sharing = ip->name;\r
287 lastIP6range = NULL;\r
288 }\r
289 if(lastIP6uplink)\r
af37be1d 290 {\r
64b2d125 291 lastIP6uplink->sharing = ip->name;\r
292 lastIP6uplink = NULL;\r
af37be1d 293 }\r
294 ip->keyword = keyword;\r
295 keyword->ip_count++;\r
296 ip->prio = keyword->default_prio;\r
297 substring += strlen(keyword->key)+1;\r
298 ptr = substring;\r
299 while(*ptr and *ptr != '-')\r
300 {\r
301 ptr++;\r
302 }\r
303 if(*ptr == '-')\r
304 {\r
305 *ptr=0;\r
306 ip->max = ip->desired = atoi(ptr+1);\r
307 }\r
9694a8ec 308\r
af37be1d 309 ip->min = atoi(substring);\r
310 if(ip->min <= 0)\r
311 {\r
312 printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kb/s\n",\r
313 str, free_min);\r
314 ip->min = free_min;\r
315 }\r
9694a8ec 316\r
af37be1d 317 if(ip->max <= ip->min)\r
318 {\r
319 ip->fixedprio = TRUE;\r
320 ip->max = ip->min + ip->keyword->reserve_min;\r
321 }\r
322 else \r
323 {\r
324 ip->max -= ip->keyword->reserve_max;\r
c38473c1 325 if(ip->max < ip->min)\r
af37be1d 326 {\r
c38473c1 327 ip->max = ip->min;\r
af37be1d 328 }\r
329 }\r
06733b88 330\r
73cf6e9d 331 /* avg MTU bytes * 8 >> 10 = in bits, max is in kb/s */\r
332 pktratio = (ip->keyword->allowed_avgmtu*8) >> 10;\r
333 if(pktratio > 0)\r
06733b88 334 {\r
73cf6e9d 335 ip->pps_limit = ip->max/pktratio;\r
336 if(ip->pps_limit > 10000) /* this limit seems to be hardcoded in iptables */\r
337 {\r
338 ip->pps_limit = 0; /* do not apply packet limits */\r
339 }\r
06733b88 340 }\r
341\r
342 ip->mark = FIRSTIPCLASS+1+class_count++; \r
2b460b72 343 update_network(ip->addr, ip);\r
af37be1d 344\r
345 if_exists(group,groups,(group->min == ip->min)) \r
346 { \r
347 group->count++; \r
348 group->desired += ip->min;\r
349 ip->group = group->id; \r
350 }\r
351 else\r
352 {\r
353 create(group,Group);\r
354 group->min = ip->min;\r
355 group->id = groupidx++;\r
356 ip->group = group->id;\r
357\r
358 if(group->min < 8) group->min = 8;\r
359 /* Warning - this is maybe because of primitive tc namespace, can be fixed */\r
360 /* it is because class IDs are derived from min. bandwidth. - xCh */\r
361 //if(group->min>MAX_GUARANTED_KBPS) group->min=MAX_GUARANTED_KBPS;\r
362 \r
363 group->count = 1;\r
364 group->desired = ip->min; \r
365 insert(group, groups, desc_order_by,min);\r
366 }\r
367 }//endif keyword-\r
368 }//endif netmask\r
1c9cae56 369 }//endif sharing-\r
370 }\r
371 fail\r
372 {\r
373 perror(hosts);\r
374 exit(-1);\r
375 }\r
376 done; /* ugly macro end */\r
9694a8ec 377// TheIP("0.0.0.0", TRUE);\r
378// ip->name = "TOTAL";\r
379// ip->mask = 0;\r
8dcd2b4c 380}\r
This page took 0.668331 seconds and 4 git commands to generate.