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 |
10 | extern struct IP *ips, *ip, *sharedip, *networks;\r |
1c9cae56 |
11 | extern struct Group *groups, *group;\r |
12 | extern struct Keyword *keyword, *defaultkeyword, *keywords;\r |
bb5e7385 |
13 | extern struct Macro *macro, *macros;\r |
1c9cae56 |
14 | extern int class_count;\r |
15 | extern int ip_count;\r |
16 | extern int found_lmsid;\r |
17 | extern int free_min;\r |
a1204fc9 |
18 | extern const int highest_priority;\r |
f64d5431 |
19 | extern char *ip6prefix;\r |
1c9cae56 |
20 | \r |
2b460b72 |
21 | void 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 |
25 | void 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 |
14e28c6f |
60 | struct IP *lastIP6;\r |
61 | \r |
1c9cae56 |
62 | /* == This function strips extra characters after IPv4 address and stores it = */\r |
af37be1d |
63 | void parse_and_append_ip(char *str, struct IP *listhead)\r |
1c9cae56 |
64 | {\r |
f64d5431 |
65 | char *ptr, *ipaddr, *ip6range = NULL, *ipname = NULL, *lmsid = NULL;\r |
66 | \r |
b1a5c883 |
67 | if(ip6prefix) /* Try this only if IPv6 subsystem is active... */\r |
f64d5431 |
68 | {\r |
69 | ptr = strstr(str, "::");\r |
70 | if(ptr && ptr-str > 4)\r |
71 | {\r |
72 | ptr -= 4; \r |
ca6f7e80 |
73 | duplicate(ptr, ip6range);\r |
f64d5431 |
74 | ptr = strstr(ip6range, "::");\r |
75 | if(ptr)\r |
76 | {\r |
77 | *(ptr+2) = 0;\r |
78 | }\r |
79 | }\r |
80 | }\r |
1c9cae56 |
81 | \r |
82 | ptr = strchr(str, '{');\r |
83 | if(ptr)\r |
84 | {\r |
85 | lmsid = ++ptr;\r |
86 | while(*ptr and *ptr != '}')\r |
87 | {\r |
88 | ptr++;\r |
89 | }\r |
90 | *ptr = 0;\r |
91 | }\r |
92 | \r |
93 | ptr = str;\r |
94 | while(*ptr and *ptr!=' ' and *ptr!=9)\r |
95 | {\r |
96 | ptr++;\r |
97 | }\r |
98 | \r |
99 | *ptr = 0;\r |
100 | ipaddr = str;\r |
101 | ptr++;\r |
102 | while(*ptr and (*ptr==' ' or *ptr==9))\r |
103 | {\r |
104 | ptr++;\r |
105 | }\r |
106 | ipname=ptr; \r |
107 | while(*ptr and *ptr!=' ' and *ptr!=9)\r |
108 | {\r |
109 | ptr++;\r |
110 | }\r |
111 | *ptr=0;\r |
112 | \r |
f64d5431 |
113 | if(ip6range)\r |
114 | {\r |
115 | concatenate(ip6prefix,ip6range,ptr);\r |
0b9c3c19 |
116 | ip6range=ptr;\r |
f64d5431 |
117 | if_exists(ip, ips, eq(ip->addr,ip6range));\r |
118 | else\r |
119 | {\r |
af37be1d |
120 | TheIP(ip6range, FALSE);\r |
f64d5431 |
121 | }\r |
0b9c3c19 |
122 | ip->name = ip6range;\r |
14e28c6f |
123 | ip->keyword = defaultkeyword; /* settings for default keyword */\r |
f64d5431 |
124 | if(lmsid)\r |
125 | {\r |
126 | ip->lmsid = atoi(lmsid);\r |
127 | }\r |
14e28c6f |
128 | lastIP6 = ip;\r |
129 | }\r |
130 | else\r |
131 | {\r |
132 | lastIP6 = NULL;\r |
f64d5431 |
133 | }\r |
134 | \r |
af37be1d |
135 | if_exists(ip, listhead, eq(ip->addr,ipaddr));\r |
1c9cae56 |
136 | else\r |
137 | {\r |
af37be1d |
138 | TheIP(ipaddr, (listhead==networks));\r |
1c9cae56 |
139 | }\r |
1c9cae56 |
140 | ip->name = ipname;\r |
141 | if(lmsid)\r |
142 | {\r |
143 | ip->lmsid = atoi(lmsid);\r |
144 | found_lmsid = TRUE;\r |
145 | }\r |
146 | }\r |
147 | \r |
148 | /* == This function parses hosts style main configuration file == */\r |
149 | void parse_hosts(char *hosts)\r |
150 | {\r |
151 | int groupidx = FIRSTGROUPID;\r |
152 | char *str, *ptr;\r |
153 | char *substring;\r |
af37be1d |
154 | struct IP *network;\r |
73cf6e9d |
155 | int pktratio;\r |
1c9cae56 |
156 | \r |
157 | parse(hosts)\r |
158 | {\r |
bb5e7385 |
159 | str = _;\r |
1c9cae56 |
160 | \r |
161 | if(*str < '0' or *str > '9')\r |
162 | {\r |
163 | /* any line starting with non-number is comment ...*/\r |
164 | continue;\r |
165 | }\r |
166 | \r |
bb5e7385 |
167 | /* first, expand (rewrite) any predefined macros, if found*/\r |
168 | for_each(macro, macros)\r |
169 | {\r |
170 | substring = strstr(str, macro->rewrite_from);\r |
171 | if(substring);\r |
172 | {\r |
173 | int l1, l3;\r |
174 | *substring = 0;\r |
175 | substring += strlen(macro->rewrite_from);\r |
176 | l1 = strlen(str);\r |
177 | l3 = strlen(substring);\r |
178 | string(ptr, l1 + strlen(macro->rewrite_to) + l3);\r |
179 | strcpy(ptr, str);\r |
180 | strcat(ptr, macro->rewrite_to);\r |
181 | strcat(ptr, substring);\r |
182 | str = ptr;\r |
183 | }\r |
184 | }\r |
185 | \r |
1c9cae56 |
186 | /* Does this IP share QoS class with some other ? */\r |
187 | substring = strstr(str, "sharing-");\r |
188 | if(substring)\r |
189 | { \r |
190 | substring += 8; /* "sharing-" */\r |
af37be1d |
191 | parse_and_append_ip(str, ips);\r |
1c9cae56 |
192 | ip->sharing = substring;\r |
193 | ip->keyword = defaultkeyword; /* settings for default keyword */\r |
14e28c6f |
194 | if(lastIP6)\r |
195 | {\r |
196 | lastIP6->sharing = substring;\r |
b1a5c883 |
197 | lastIP6 = NULL;\r |
14e28c6f |
198 | }\r |
1c9cae56 |
199 | while(*substring and *substring != '\n')\r |
200 | {\r |
201 | substring++;\r |
202 | }\r |
203 | *substring = 0; \r |
204 | }\r |
205 | else\r |
206 | {\r |
af37be1d |
207 | substring = strstr(str, "#255.");\r |
208 | if(substring and not strstr(str, "#255.255.255.255")) /* do not ping /32 ranges */\r |
1c9cae56 |
209 | {\r |
af37be1d |
210 | /* netmask detected - save network*/\r |
211 | unsigned bit;\r |
212 | unsigned num, mask = 8;\r |
213 | substring += 5;\r |
214 | while(substring && *substring)\r |
1c9cae56 |
215 | {\r |
af37be1d |
216 | ptr = substring;\r |
217 | substring = strchr(substring, '.');\r |
218 | if(substring)\r |
219 | {\r |
220 | *substring = 0;\r |
221 | substring += 1;\r |
222 | }\r |
223 | num = atoi(ptr);\r |
224 | for(bit = 1; bit <=128 ; bit<<=1)\r |
1c9cae56 |
225 | {\r |
af37be1d |
226 | if(bit & num)\r |
227 | {\r |
228 | mask++;\r |
229 | }\r |
1c9cae56 |
230 | }\r |
af37be1d |
231 | } \r |
232 | parse_and_append_ip(str, networks);\r |
233 | ip->mask = mask;\r |
234 | }\r |
235 | else\r |
236 | {\r |
237 | /*Do we have to create new QoS class for this IP ? */\r |
238 | if_exists(keyword,keywords,(substring=strstr(str,keyword->key)))\r |
1c9cae56 |
239 | {\r |
af37be1d |
240 | parse_and_append_ip(str, ips);\r |
241 | if(lastIP6)\r |
242 | {\r |
243 | lastIP6->sharing = ip->name;\r |
244 | lastIP6 = NULL;\r |
245 | }\r |
246 | ip->keyword = keyword;\r |
247 | keyword->ip_count++;\r |
248 | ip->prio = keyword->default_prio;\r |
249 | substring += strlen(keyword->key)+1;\r |
250 | ptr = substring;\r |
251 | while(*ptr and *ptr != '-')\r |
252 | {\r |
253 | ptr++;\r |
254 | }\r |
255 | if(*ptr == '-')\r |
256 | {\r |
257 | *ptr=0;\r |
258 | ip->max = ip->desired = atoi(ptr+1);\r |
259 | }\r |
9694a8ec |
260 | \r |
af37be1d |
261 | ip->min = atoi(substring);\r |
262 | if(ip->min <= 0)\r |
263 | {\r |
264 | printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kb/s\n",\r |
265 | str, free_min);\r |
266 | ip->min = free_min;\r |
267 | }\r |
9694a8ec |
268 | \r |
af37be1d |
269 | if(ip->max <= ip->min)\r |
270 | {\r |
271 | ip->fixedprio = TRUE;\r |
272 | ip->max = ip->min + ip->keyword->reserve_min;\r |
273 | }\r |
274 | else \r |
275 | {\r |
276 | ip->max -= ip->keyword->reserve_max;\r |
c38473c1 |
277 | if(ip->max < ip->min)\r |
af37be1d |
278 | {\r |
c38473c1 |
279 | ip->max = ip->min;\r |
af37be1d |
280 | }\r |
281 | }\r |
06733b88 |
282 | \r |
73cf6e9d |
283 | /* avg MTU bytes * 8 >> 10 = in bits, max is in kb/s */\r |
284 | pktratio = (ip->keyword->allowed_avgmtu*8) >> 10;\r |
285 | if(pktratio > 0)\r |
06733b88 |
286 | {\r |
73cf6e9d |
287 | ip->pps_limit = ip->max/pktratio;\r |
288 | if(ip->pps_limit > 10000) /* this limit seems to be hardcoded in iptables */\r |
289 | {\r |
290 | ip->pps_limit = 0; /* do not apply packet limits */\r |
291 | }\r |
06733b88 |
292 | }\r |
293 | \r |
294 | ip->mark = FIRSTIPCLASS+1+class_count++; \r |
2b460b72 |
295 | update_network(ip->addr, ip);\r |
af37be1d |
296 | \r |
297 | if_exists(group,groups,(group->min == ip->min)) \r |
298 | { \r |
299 | group->count++; \r |
300 | group->desired += ip->min;\r |
301 | ip->group = group->id; \r |
302 | }\r |
303 | else\r |
304 | {\r |
305 | create(group,Group);\r |
306 | group->min = ip->min;\r |
307 | group->id = groupidx++;\r |
308 | ip->group = group->id;\r |
309 | \r |
310 | if(group->min < 8) group->min = 8;\r |
311 | /* Warning - this is maybe because of primitive tc namespace, can be fixed */\r |
312 | /* it is because class IDs are derived from min. bandwidth. - xCh */\r |
313 | //if(group->min>MAX_GUARANTED_KBPS) group->min=MAX_GUARANTED_KBPS;\r |
314 | \r |
315 | group->count = 1;\r |
316 | group->desired = ip->min; \r |
317 | insert(group, groups, desc_order_by,min);\r |
318 | }\r |
319 | }//endif keyword-\r |
320 | }//endif netmask\r |
1c9cae56 |
321 | }//endif sharing-\r |
322 | }\r |
323 | fail\r |
324 | {\r |
325 | perror(hosts);\r |
326 | exit(-1);\r |
327 | }\r |
328 | done; /* ugly macro end */\r |
9694a8ec |
329 | // TheIP("0.0.0.0", TRUE);\r |
330 | // ip->name = "TOTAL";\r |
331 | // ip->mask = 0;\r |
1c9cae56 |
332 | } |