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