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