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