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