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