getting ready for IP6tables support
[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
19 /* This must be object oriented! This looks almost like constructor ;-) */
20 void TheIP(char *ipaddr)
21 {
22 create(ip,IP);
23 ip->name = "";
24 ip->addr = ipaddr;
25 ip->sharing = NULL;
26 ip->prio = highest_priority+1;
27 ip->lmsid = -1;
28 ip->fixedprio = \
29 ip->mark = \
30 ip->min = \
31 ip->max = \
32 ip->desired = \
33 ip->credit = \
34 ip->upload = \
35 ip->proxy = \
36 ip->direct = \
37 ip->traffic = \
38 ip->pktsup = \
39 ip->pktsdown = 0;
40 ip->keyword = keywords;
41 push(ip,ips);
42 }
43
44 /* == This function strips extra characters after IPv4 address and stores it = */
45 void parse_ip(char *str)
46 {
47 char *ptr, *ipaddr = NULL, *ipname = NULL, *lmsid = NULL;
48
49 ptr = strchr(str, '{');
50 if(ptr)
51 {
52 lmsid = ++ptr;
53 while(*ptr and *ptr != '}')
54 {
55 ptr++;
56 }
57 *ptr = 0;
58 }
59
60 ptr = str;
61 while(*ptr and *ptr!=' ' and *ptr!=9)
62 {
63 ptr++;
64 }
65
66 *ptr = 0;
67 ipaddr = str;
68 ptr++;
69 while(*ptr and (*ptr==' ' or *ptr==9))
70 {
71 ptr++;
72 }
73 ipname=ptr;
74 while(*ptr and *ptr!=' ' and *ptr!=9)
75 {
76 ptr++;
77 }
78 *ptr=0;
79
80 if_exists(ip, ips, eq(ip->addr,ipaddr));
81 else
82 {
83 TheIP(ipaddr);
84 }
85 ip->name = ipname;
86 if(lmsid)
87 {
88 ip->lmsid = atoi(lmsid);
89 found_lmsid = TRUE;
90 }
91 }
92
93 /* == This function parses hosts style main configuration file == */
94 void parse_hosts(char *hosts)
95 {
96 int groupidx = FIRSTGROUPID;
97 char *str, *ptr;
98 char *substring;
99
100 parse(hosts)
101 {
102 str=_;
103
104 if(*str < '0' or *str > '9')
105 {
106 /* any line starting with non-number is comment ...*/
107 continue;
108 }
109
110 /* Does this IP share QoS class with some other ? */
111 substring = strstr(str, "sharing-");
112 if(substring)
113 {
114 substring += 8; /* "sharing-" */
115 parse_ip(str);
116 ip_count++;
117 ip->sharing = substring;
118 ip->keyword = defaultkeyword; /* settings for default keyword */
119 while(*substring and *substring != '\n')
120 {
121 substring++;
122 }
123 *substring = 0;
124 }
125 else
126 {
127 /*Do we have to create new QoS class for this IP ? */
128
129 if_exists(keyword,keywords,(substring=strstr(str,keyword->key)))
130 {
131 parse_ip(str);
132 ip_count++;
133 ip->keyword = keyword;
134 keyword->ip_count++;
135 ip->prio = keyword->default_prio;
136 substring += strlen(keyword->key)+1;
137 ptr = substring;
138 while(*ptr and *ptr != '-')
139 {
140 ptr++;
141 }
142 if(*ptr == '-')
143 {
144 *ptr=0;
145 ip->max = ip->desired = atoi(ptr+1);
146 }
147 ip->min = atoi(substring);
148 if(ip->min <= 0)
149 {
150 printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kb/s\n",
151 str, free_min);
152 ip->min = free_min;
153 }
154 if(ip->max <= ip->min)
155 {
156 ip->fixedprio = TRUE;
157 ip->max = ip->min + ip->keyword->reserve_min;
158 }
159 else
160 {
161 ip->max -= ip->keyword->reserve_max;
162 if(ip->max<ip->min)
163 {
164 ip->max=ip->min;
165 }
166 }
167 ip->mark = FIRSTIPCLASS+1+class_count++;
168
169 if_exists(group,groups,(group->min == ip->min))
170 {
171 group->count++;
172 group->desired += ip->min;
173 ip->group = group->id;
174 }
175 else
176 {
177 create(group,Group);
178 group->min = ip->min;
179 group->id = groupidx++;
180 ip->group = group->id;
181
182 if(group->min < 8) group->min = 8;
183 /* Warning - this is maybe because of primitive tc namespace, can be fixed */
184 /* it is because class IDs are derived from min. bandwidth. - xCh */
185 //if(group->min>MAX_GUARANTED_KBPS) group->min=MAX_GUARANTED_KBPS;
186
187 group->count = 1;
188 group->desired = ip->min;
189 insert(group, groups, desc_order_by,min);
190 }
191 }//endif keyword-
192 }//endif sharing-
193 }
194 fail
195 {
196 perror(hosts);
197 exit(-1);
198 }
199 done; /* ugly macro end */
200 }
This page took 0.328947 seconds and 5 git commands to generate.