Major rewrite of leading comments
[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(void)
21 {
22 create(ip,IP);
23 ip->name = "";
24 ip->addr = "";
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();
84 }
85 ip->addr = ipaddr;
86 ip->name = ipname;
87 if(lmsid)
88 {
89 ip->lmsid = atoi(lmsid);
90 found_lmsid = TRUE;
91 }
92 }
93
94 /* == This function parses hosts style main configuration file == */
95 void parse_hosts(char *hosts)
96 {
97 int groupidx = FIRSTGROUPID;
98 char *str, *ptr;
99 char *substring;
100
101 parse(hosts)
102 {
103 str=_;
104
105 if(*str < '0' or *str > '9')
106 {
107 /* any line starting with non-number is comment ...*/
108 continue;
109 }
110
111 /* Does this IP share QoS class with some other ? */
112 substring = strstr(str, "sharing-");
113 if(substring)
114 {
115 substring += 8; /* "sharing-" */
116 parse_ip(str);
117 ip_count++;
118 ip->sharing = substring;
119 ip->keyword = defaultkeyword; /* settings for default keyword */
120 while(*substring and *substring != '\n')
121 {
122 substring++;
123 }
124 *substring = 0;
125 }
126 else
127 {
128 /*Do we have to create new QoS class for this IP ? */
129
130 if_exists(keyword,keywords,(substring=strstr(str,keyword->key)))
131 {
132 parse_ip(str);
133 ip_count++;
134 ip->keyword = keyword;
135 keyword->ip_count++;
136 ip->prio = keyword->default_prio;
137 substring += strlen(keyword->key)+1;
138 ptr = substring;
139 while(*ptr and *ptr != '-')
140 {
141 ptr++;
142 }
143 if(*ptr == '-')
144 {
145 *ptr=0;
146 ip->max = ip->desired = atoi(ptr+1);
147 }
148 ip->min = atoi(substring);
149 if(ip->min <= 0)
150 {
151 printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kb/s\n",
152 str, free_min);
153 ip->min = free_min;
154 }
155 if(ip->max <= ip->min)
156 {
157 ip->fixedprio = TRUE;
158 ip->max = ip->min + ip->keyword->reserve_min;
159 }
160 else
161 {
162 ip->max -= ip->keyword->reserve_max;
163 if(ip->max<ip->min)
164 {
165 ip->max=ip->min;
166 }
167 }
168 ip->mark = FIRSTIPCLASS+1+class_count++;
169
170 if_exists(group,groups,(group->min == ip->min))
171 {
172 group->count++;
173 group->desired += ip->min;
174 ip->group = group->id;
175 }
176 else
177 {
178 create(group,Group);
179 group->min = ip->min;
180 group->id = groupidx++;
181 ip->group = group->id;
182
183 if(group->min < 8) group->min = 8;
184 /* Warning - this is maybe because of primitive tc namespace, can be fixed */
185 /* it is because class IDs are derived from min. bandwidth. - xCh */
186 //if(group->min>MAX_GUARANTED_KBPS) group->min=MAX_GUARANTED_KBPS;
187
188 group->count = 1;
189 group->desired = ip->min;
190 insert(group, groups, desc_order_by,min);
191 }
192 }//endif keyword-
193 }//endif sharing-
194 }
195 fail
196 {
197 perror(hosts);
198 exit(-1);
199 }
200 done; /* ugly macro end */
201 }
This page took 0.424301 seconds and 4 git commands to generate.