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