traffic->download bug fixed
[svn/Prometheus-QoS/.git] / parsehosts.c
... / ...
CommitLineData
1/* Modified by: xChaos, 20131029 */\r
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
10extern struct IP *ips, *ip, *sharedip, *networks;\r
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
17extern const int highest_priority;\r
18extern char *ip6prefix;\r
19\r
20void update_network(char *look_for, struct IP* ip);\r
21/* implemented in networks.c */\r
22\r
23/* This must be object oriented! This looks almost like constructor ;-) */\r
24void TheIP(char *ipaddr, int is_network)\r
25{\r
26 create(ip,IP);\r
27 ip->name = "";\r
28 ip->addr = ipaddr;\r
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
43 ip->pps_limit = \\r
44 ip->pktsdown = 0;\r
45 ip->keyword = keywords;\r
46 ip->v6 = (strchr(ip->addr,':')!=NULL);\r
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
56 ip_count++;\r
57}\r
58\r
59struct IP *lastIP6;\r
60\r
61/* == This function strips extra characters after IPv4 address and stores it = */\r
62void parse_and_append_ip(char *str, struct IP *listhead)\r
63{\r
64 char *ptr, *ipaddr, *ip6range = NULL, *ipname = NULL, *lmsid = NULL;\r
65\r
66 if(ip6prefix) /* Try this only if IPv6 subsystem is active... */\r
67 {\r
68 ptr = strstr(str, "::");\r
69 if(ptr && ptr-str > 4)\r
70 {\r
71 ptr -= 4; \r
72 duplicate(ptr, ip6range);\r
73 ptr = strstr(ip6range, "::");\r
74 if(ptr)\r
75 {\r
76 *(ptr+2) = 0;\r
77 }\r
78 }\r
79 }\r
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
112 if(ip6range)\r
113 {\r
114 concatenate(ip6prefix,ip6range,ptr);\r
115 ip6range=ptr;\r
116 if_exists(ip, ips, eq(ip->addr,ip6range));\r
117 else\r
118 {\r
119 TheIP(ip6range, FALSE);\r
120 }\r
121 ip->name = ip6range;\r
122 ip->keyword = defaultkeyword; /* settings for default keyword */\r
123 if(lmsid)\r
124 {\r
125 ip->lmsid = atoi(lmsid);\r
126 }\r
127 lastIP6 = ip;\r
128 }\r
129 else\r
130 {\r
131 lastIP6 = NULL;\r
132 }\r
133\r
134 if_exists(ip, listhead, eq(ip->addr,ipaddr));\r
135 else\r
136 {\r
137 TheIP(ipaddr, (listhead==networks));\r
138 }\r
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
153 struct IP *network;\r
154 int pktratio;\r
155\r
156 parse(hosts)\r
157 {\r
158 str=_;\r
159\r
160 if(*str < '0' or *str > '9')\r
161 {\r
162 /* any line starting with non-number is comment ...*/\r
163 continue;\r
164 }\r
165 \r
166 /* Does this IP share QoS class with some other ? */\r
167 substring = strstr(str, "sharing-");\r
168 if(substring)\r
169 { \r
170 substring += 8; /* "sharing-" */\r
171 parse_and_append_ip(str, ips);\r
172 ip->sharing = substring;\r
173 ip->keyword = defaultkeyword; /* settings for default keyword */\r
174 if(lastIP6)\r
175 {\r
176 lastIP6->sharing = substring;\r
177 lastIP6 = NULL;\r
178 }\r
179 while(*substring and *substring != '\n')\r
180 {\r
181 substring++;\r
182 }\r
183 *substring = 0; \r
184 }\r
185 else\r
186 {\r
187 substring = strstr(str, "#255.");\r
188 if(substring and not strstr(str, "#255.255.255.255")) /* do not ping /32 ranges */\r
189 {\r
190 /* netmask detected - save network*/\r
191 unsigned bit;\r
192 unsigned num, mask = 8;\r
193 substring += 5;\r
194 while(substring && *substring)\r
195 {\r
196 ptr = substring;\r
197 substring = strchr(substring, '.');\r
198 if(substring)\r
199 {\r
200 *substring = 0;\r
201 substring += 1;\r
202 }\r
203 num = atoi(ptr);\r
204 for(bit = 1; bit <=128 ; bit<<=1)\r
205 {\r
206 if(bit & num)\r
207 {\r
208 mask++;\r
209 }\r
210 }\r
211 } \r
212 parse_and_append_ip(str, networks);\r
213 ip->mask = mask;\r
214 }\r
215 else\r
216 {\r
217 /*Do we have to create new QoS class for this IP ? */\r
218 if_exists(keyword,keywords,(substring=strstr(str,keyword->key)))\r
219 {\r
220 parse_and_append_ip(str, ips);\r
221 if(lastIP6)\r
222 {\r
223 lastIP6->sharing = ip->name;\r
224 lastIP6 = NULL;\r
225 }\r
226 ip->keyword = keyword;\r
227 keyword->ip_count++;\r
228 ip->prio = keyword->default_prio;\r
229 substring += strlen(keyword->key)+1;\r
230 ptr = substring;\r
231 while(*ptr and *ptr != '-')\r
232 {\r
233 ptr++;\r
234 }\r
235 if(*ptr == '-')\r
236 {\r
237 *ptr=0;\r
238 ip->max = ip->desired = atoi(ptr+1);\r
239 }\r
240\r
241 ip->min = atoi(substring);\r
242 if(ip->min <= 0)\r
243 {\r
244 printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kb/s\n",\r
245 str, free_min);\r
246 ip->min = free_min;\r
247 }\r
248\r
249 if(ip->max <= ip->min)\r
250 {\r
251 ip->fixedprio = TRUE;\r
252 ip->max = ip->min + ip->keyword->reserve_min;\r
253 }\r
254 else \r
255 {\r
256 ip->max -= ip->keyword->reserve_max;\r
257 if(ip->max < ip->min)\r
258 {\r
259 ip->max = ip->min;\r
260 }\r
261 }\r
262\r
263 /* avg MTU bytes * 8 >> 10 = in bits, max is in kb/s */\r
264 pktratio = (ip->keyword->allowed_avgmtu*8) >> 10;\r
265 if(pktratio > 0)\r
266 {\r
267 ip->pps_limit = ip->max/pktratio;\r
268 if(ip->pps_limit > 10000) /* this limit seems to be hardcoded in iptables */\r
269 {\r
270 ip->pps_limit = 0; /* do not apply packet limits */\r
271 }\r
272 }\r
273\r
274 ip->mark = FIRSTIPCLASS+1+class_count++; \r
275 update_network(ip->addr, ip);\r
276\r
277 if_exists(group,groups,(group->min == ip->min)) \r
278 { \r
279 group->count++; \r
280 group->desired += ip->min;\r
281 ip->group = group->id; \r
282 }\r
283 else\r
284 {\r
285 create(group,Group);\r
286 group->min = ip->min;\r
287 group->id = groupidx++;\r
288 ip->group = group->id;\r
289\r
290 if(group->min < 8) group->min = 8;\r
291 /* Warning - this is maybe because of primitive tc namespace, can be fixed */\r
292 /* it is because class IDs are derived from min. bandwidth. - xCh */\r
293 //if(group->min>MAX_GUARANTED_KBPS) group->min=MAX_GUARANTED_KBPS;\r
294 \r
295 group->count = 1;\r
296 group->desired = ip->min; \r
297 insert(group, groups, desc_order_by,min);\r
298 }\r
299 }//endif keyword-\r
300 }//endif netmask\r
301 }//endif sharing-\r
302 }\r
303 fail\r
304 {\r
305 perror(hosts);\r
306 exit(-1);\r
307 }\r
308 done; /* ugly macro end */\r
309// TheIP("0.0.0.0", TRUE);\r
310// ip->name = "TOTAL";\r
311// ip->mask = 0;\r
312}
This page took 0.155958 seconds and 4 git commands to generate.