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