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