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