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