bug fixes
[svn/Prometheus-QoS/.git] / parsehosts.c
CommitLineData
1c9cae56 1/* Modified by: xChaos, 20121007 */\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;\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
a1204fc9 17extern const int highest_priority;\r
f64d5431 18extern char *ip6prefix;\r
1c9cae56 19\r
a1204fc9 20/* This must be object oriented! This looks almost like constructor ;-) */\r
f19d3cd0 21void TheIP(char *ipaddr)\r
a1204fc9 22{\r
23 create(ip,IP);\r
24 ip->name = "";\r
f19d3cd0 25 ip->addr = ipaddr;\r
a1204fc9 26 ip->sharing = NULL;\r
27 ip->prio = highest_priority+1;\r
28 ip->lmsid = -1;\r
29 ip->fixedprio = \\r
30 ip->mark = \\r
31 ip->min = \\r
32 ip->max = \\r
33 ip->desired = \\r
34 ip->credit = \\r
35 ip->upload = \\r
36 ip->proxy = \\r
37 ip->direct = \\r
38 ip->traffic = \\r
39 ip->pktsup = \\r
40 ip->pktsdown = 0;\r
41 ip->keyword = keywords;\r
0b9c3c19 42 ip->v6 = (strchr(ip->addr,':')!=NULL);\r
a1204fc9 43 push(ip,ips);\r
14e28c6f 44 ip_count++;\r
a1204fc9 45}\r
1c9cae56 46\r
14e28c6f 47struct IP *lastIP6;\r
48\r
1c9cae56 49/* == This function strips extra characters after IPv4 address and stores it = */\r
14e28c6f 50parse_ip(char *str)\r
1c9cae56 51{\r
f64d5431 52 char *ptr, *ipaddr, *ip6range = NULL, *ipname = NULL, *lmsid = NULL;\r
53\r
b1a5c883 54 if(ip6prefix) /* Try this only if IPv6 subsystem is active... */\r
f64d5431 55 {\r
56 ptr = strstr(str, "::");\r
57 if(ptr && ptr-str > 4)\r
58 {\r
59 ptr -= 4; \r
ca6f7e80 60 duplicate(ptr, ip6range);\r
f64d5431 61 ptr = strstr(ip6range, "::");\r
62 if(ptr)\r
63 {\r
64 *(ptr+2) = 0;\r
65 }\r
66 }\r
67 }\r
1c9cae56 68\r
69 ptr = strchr(str, '{');\r
70 if(ptr)\r
71 {\r
72 lmsid = ++ptr;\r
73 while(*ptr and *ptr != '}')\r
74 {\r
75 ptr++;\r
76 }\r
77 *ptr = 0;\r
78 }\r
79 \r
80 ptr = str;\r
81 while(*ptr and *ptr!=' ' and *ptr!=9)\r
82 {\r
83 ptr++;\r
84 }\r
85 \r
86 *ptr = 0;\r
87 ipaddr = str;\r
88 ptr++;\r
89 while(*ptr and (*ptr==' ' or *ptr==9))\r
90 {\r
91 ptr++;\r
92 }\r
93 ipname=ptr; \r
94 while(*ptr and *ptr!=' ' and *ptr!=9)\r
95 {\r
96 ptr++;\r
97 }\r
98 *ptr=0;\r
99\r
f64d5431 100 if(ip6range)\r
101 {\r
102 concatenate(ip6prefix,ip6range,ptr);\r
0b9c3c19 103 ip6range=ptr;\r
f64d5431 104 if_exists(ip, ips, eq(ip->addr,ip6range));\r
105 else\r
106 {\r
107 TheIP(ip6range);\r
108 }\r
0b9c3c19 109 ip->name = ip6range;\r
14e28c6f 110 ip->keyword = defaultkeyword; /* settings for default keyword */\r
f64d5431 111 if(lmsid)\r
112 {\r
113 ip->lmsid = atoi(lmsid);\r
114 }\r
14e28c6f 115 lastIP6 = ip;\r
116 }\r
117 else\r
118 {\r
119 lastIP6 = NULL;\r
f64d5431 120 }\r
121\r
1c9cae56 122 if_exists(ip, ips, eq(ip->addr,ipaddr));\r
123 else\r
124 {\r
f19d3cd0 125 TheIP(ipaddr);\r
1c9cae56 126 }\r
1c9cae56 127 ip->name = ipname;\r
128 if(lmsid)\r
129 {\r
130 ip->lmsid = atoi(lmsid);\r
131 found_lmsid = TRUE;\r
132 }\r
133}\r
134\r
135/* == This function parses hosts style main configuration file == */\r
136void parse_hosts(char *hosts)\r
137{\r
138 int groupidx = FIRSTGROUPID;\r
139 char *str, *ptr;\r
140 char *substring;\r
141\r
142 parse(hosts)\r
143 {\r
144 str=_;\r
145\r
146 if(*str < '0' or *str > '9')\r
147 {\r
148 /* any line starting with non-number is comment ...*/\r
149 continue;\r
150 }\r
151 \r
152 /* Does this IP share QoS class with some other ? */\r
153 substring = strstr(str, "sharing-");\r
154 if(substring)\r
155 { \r
156 substring += 8; /* "sharing-" */\r
157 parse_ip(str);\r
1c9cae56 158 ip->sharing = substring;\r
159 ip->keyword = defaultkeyword; /* settings for default keyword */\r
14e28c6f 160 if(lastIP6)\r
161 {\r
162 lastIP6->sharing = substring;\r
b1a5c883 163 lastIP6 = NULL;\r
14e28c6f 164 }\r
1c9cae56 165 while(*substring and *substring != '\n')\r
166 {\r
167 substring++;\r
168 }\r
169 *substring = 0; \r
170 }\r
171 else\r
172 {\r
173 /*Do we have to create new QoS class for this IP ? */\r
174\r
175 if_exists(keyword,keywords,(substring=strstr(str,keyword->key)))\r
176 {\r
177 parse_ip(str);\r
b1a5c883 178 if(lastIP6)\r
179 {\r
180 lastIP6->sharing = ip->name;\r
181 lastIP6 = NULL;\r
182 }\r
1c9cae56 183 ip->keyword = keyword;\r
184 keyword->ip_count++;\r
185 ip->prio = keyword->default_prio;\r
186 substring += strlen(keyword->key)+1;\r
187 ptr = substring;\r
188 while(*ptr and *ptr != '-')\r
189 {\r
190 ptr++;\r
191 }\r
192 if(*ptr == '-')\r
193 {\r
194 *ptr=0;\r
195 ip->max = ip->desired = atoi(ptr+1);\r
196 }\r
197 ip->min = atoi(substring);\r
198 if(ip->min <= 0)\r
199 {\r
200 printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kb/s\n",\r
201 str, free_min);\r
202 ip->min = free_min;\r
203 }\r
204 if(ip->max <= ip->min)\r
205 {\r
206 ip->fixedprio = TRUE;\r
207 ip->max = ip->min + ip->keyword->reserve_min;\r
208 }\r
209 else \r
210 {\r
211 ip->max -= ip->keyword->reserve_max;\r
212 if(ip->max<ip->min)\r
213 {\r
214 ip->max=ip->min;\r
215 }\r
216 }\r
217 ip->mark = FIRSTIPCLASS+1+class_count++;\r
218\r
219 if_exists(group,groups,(group->min == ip->min)) \r
220 { \r
221 group->count++; \r
222 group->desired += ip->min;\r
223 ip->group = group->id; \r
224 }\r
225 else\r
226 {\r
227 create(group,Group);\r
228 group->min = ip->min;\r
229 group->id = groupidx++;\r
230 ip->group = group->id;\r
231\r
232 if(group->min < 8) group->min = 8;\r
233 /* Warning - this is maybe because of primitive tc namespace, can be fixed */\r
234 /* it is because class IDs are derived from min. bandwidth. - xCh */\r
235 //if(group->min>MAX_GUARANTED_KBPS) group->min=MAX_GUARANTED_KBPS;\r
236 \r
237 group->count = 1;\r
238 group->desired = ip->min; \r
239 insert(group, groups, desc_order_by,min);\r
240 }\r
241 }//endif keyword-\r
242 }//endif sharing-\r
243 }\r
244 fail\r
245 {\r
246 perror(hosts);\r
247 exit(-1);\r
248 }\r
249 done; /* ugly macro end */\r
250}
This page took 0.30929 seconds and 4 git commands to generate.