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