hopefuly final IPv6 bugfix
[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
54 if(ip6prefix) /* Try this only if IPv6 subsystem is active...*/\r
55 {\r
56 ptr = strstr(str, "::");\r
57 if(ptr && ptr-str > 4)\r
58 {\r
59 ptr -= 4; \r
60 duplicate(ptr,ip6range);\r
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
163 }\r
1c9cae56 164 while(*substring and *substring != '\n')\r
165 {\r
166 substring++;\r
167 }\r
168 *substring = 0; \r
169 }\r
170 else\r
171 {\r
172 /*Do we have to create new QoS class for this IP ? */\r
173\r
174 if_exists(keyword,keywords,(substring=strstr(str,keyword->key)))\r
175 {\r
176 parse_ip(str);\r
1c9cae56 177 ip->keyword = keyword;\r
178 keyword->ip_count++;\r
179 ip->prio = keyword->default_prio;\r
180 substring += strlen(keyword->key)+1;\r
181 ptr = substring;\r
182 while(*ptr and *ptr != '-')\r
183 {\r
184 ptr++;\r
185 }\r
186 if(*ptr == '-')\r
187 {\r
188 *ptr=0;\r
189 ip->max = ip->desired = atoi(ptr+1);\r
190 }\r
191 ip->min = atoi(substring);\r
192 if(ip->min <= 0)\r
193 {\r
194 printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kb/s\n",\r
195 str, free_min);\r
196 ip->min = free_min;\r
197 }\r
198 if(ip->max <= ip->min)\r
199 {\r
200 ip->fixedprio = TRUE;\r
201 ip->max = ip->min + ip->keyword->reserve_min;\r
202 }\r
203 else \r
204 {\r
205 ip->max -= ip->keyword->reserve_max;\r
206 if(ip->max<ip->min)\r
207 {\r
208 ip->max=ip->min;\r
209 }\r
210 }\r
211 ip->mark = FIRSTIPCLASS+1+class_count++;\r
212\r
213 if_exists(group,groups,(group->min == ip->min)) \r
214 { \r
215 group->count++; \r
216 group->desired += ip->min;\r
217 ip->group = group->id; \r
218 }\r
219 else\r
220 {\r
221 create(group,Group);\r
222 group->min = ip->min;\r
223 group->id = groupidx++;\r
224 ip->group = group->id;\r
225\r
226 if(group->min < 8) group->min = 8;\r
227 /* Warning - this is maybe because of primitive tc namespace, can be fixed */\r
228 /* it is because class IDs are derived from min. bandwidth. - xCh */\r
229 //if(group->min>MAX_GUARANTED_KBPS) group->min=MAX_GUARANTED_KBPS;\r
230 \r
231 group->count = 1;\r
232 group->desired = ip->min; \r
233 insert(group, groups, desc_order_by,min);\r
234 }\r
235 }//endif keyword-\r
236 }//endif sharing-\r
237 }\r
238 fail\r
239 {\r
240 perror(hosts);\r
241 exit(-1);\r
242 }\r
243 done; /* ugly macro end */\r
244}
This page took 0.260821 seconds and 4 git commands to generate.