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