| 1 | /* Modified by: xChaos, 20131220 */\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 |
| 10 | extern struct IP *ips, *ip, *sharedip, *networks;\r |
| 11 | extern struct Group *groups, *group;\r |
| 12 | extern struct Keyword *keyword, *defaultkeyword, *keywords;\r |
| 13 | extern struct Macro *macro, *macros;\r |
| 14 | extern int class_count;\r |
| 15 | extern int ip_count;\r |
| 16 | extern int found_lmsid;\r |
| 17 | extern int free_min;\r |
| 18 | extern const int highest_priority;\r |
| 19 | extern char *ip6prefix;\r |
| 20 | \r |
| 21 | void update_network(char *look_for, struct IP* ip);\r |
| 22 | /* implemented in networks.c */\r |
| 23 | \r |
| 24 | /* This must be object oriented! This looks almost like constructor ;-) */\r |
| 25 | void TheIP(char *ipaddr, int is_network)\r |
| 26 | {\r |
| 27 | create(ip,IP);\r |
| 28 | ip->name = "";\r |
| 29 | ip->addr = ipaddr;\r |
| 30 | ip->sharing = NULL;\r |
| 31 | ip->prio = highest_priority+1;\r |
| 32 | ip->lmsid = -1;\r |
| 33 | ip->fixedprio = \\r |
| 34 | ip->mark = \\r |
| 35 | ip->min = \\r |
| 36 | ip->max = \\r |
| 37 | ip->desired = \\r |
| 38 | ip->credit = \\r |
| 39 | ip->upload = \\r |
| 40 | ip->proxy = \\r |
| 41 | ip->direct = \\r |
| 42 | ip->traffic = \\r |
| 43 | ip->pktsup = \\r |
| 44 | ip->pps_limit = \\r |
| 45 | ip->pktsdown = 0;\r |
| 46 | ip->keyword = keywords;\r |
| 47 | ip->v6 = (strchr(ip->addr,':')!=NULL);\r |
| 48 | ip->mask = ((ip->v6)?64:32);\r |
| 49 | if(is_network)\r |
| 50 | {\r |
| 51 | push(ip, networks);\r |
| 52 | }\r |
| 53 | else\r |
| 54 | {\r |
| 55 | push(ip, ips); \r |
| 56 | }\r |
| 57 | ip_count++;\r |
| 58 | }\r |
| 59 | \r |
| 60 | struct IP *lastIP6range, *lastIP6uplink;\r |
| 61 | \r |
| 62 | /* == This function strips extra characters after IPv4 address and stores it = */\r |
| 63 | void parse_and_append_ip(char *str, struct IP *listhead)\r |
| 64 | {\r |
| 65 | char *ptr, *ipaddr, *nextip6, *ip6buf; \r |
| 66 | char *ip6uplink = NULL, *ip6range = NULL, *ipname = NULL, *lmsid = NULL;\r |
| 67 | \r |
| 68 | if(ip6prefix) /* Try this only if IPv6 subsystem is active... */\r |
| 69 | {\r |
| 70 | ptr = strstr(str, "::");\r |
| 71 | while(ptr && ptr-str > 4)\r |
| 72 | {\r |
| 73 | nextip6 = strstr(ptr + 2, "::");\r |
| 74 | ptr -= 4;\r |
| 75 | duplicate(ptr, ip6buf);\r |
| 76 | ptr = strstr(ip6buf, "::");\r |
| 77 | if(ptr)\r |
| 78 | {\r |
| 79 | if(*(ptr+2) == '+')\r |
| 80 | {\r |
| 81 | *(ptr+3) = 0; /* ends with ::+ */\r |
| 82 | ip6uplink = ip6buf;\r |
| 83 | }\r |
| 84 | else\r |
| 85 | {\r |
| 86 | *(ptr+2) = 0; /* ends with :: */\r |
| 87 | ip6range = ip6buf;\r |
| 88 | } \r |
| 89 | }\r |
| 90 | ptr = nextip6;\r |
| 91 | }\r |
| 92 | }\r |
| 93 | \r |
| 94 | ptr = strchr(str, '{');\r |
| 95 | if(ptr)\r |
| 96 | {\r |
| 97 | lmsid = ++ptr;\r |
| 98 | while(*ptr and *ptr != '}')\r |
| 99 | {\r |
| 100 | ptr++;\r |
| 101 | }\r |
| 102 | *ptr = 0;\r |
| 103 | }\r |
| 104 | \r |
| 105 | ptr = str;\r |
| 106 | while(*ptr and *ptr!=' ' and *ptr!=9)\r |
| 107 | {\r |
| 108 | ptr++;\r |
| 109 | }\r |
| 110 | \r |
| 111 | *ptr = 0;\r |
| 112 | ipaddr = str;\r |
| 113 | ptr++;\r |
| 114 | while(*ptr and (*ptr==' ' or *ptr==9))\r |
| 115 | {\r |
| 116 | ptr++;\r |
| 117 | }\r |
| 118 | ipname = ptr; \r |
| 119 | while(*ptr and *ptr!=' ' and *ptr!=9)\r |
| 120 | {\r |
| 121 | ptr++;\r |
| 122 | }\r |
| 123 | *ptr=0;\r |
| 124 | \r |
| 125 | if(ip6range)\r |
| 126 | {\r |
| 127 | concatenate(ip6prefix,ip6range,ptr);\r |
| 128 | ip6range=ptr;\r |
| 129 | if_exists(ip, ips, eq(ip->addr,ip6range)); /* check - allocated range must be unique */\r |
| 130 | else\r |
| 131 | {\r |
| 132 | TheIP(ip6range, FALSE);\r |
| 133 | }\r |
| 134 | ip->name = ip6range;\r |
| 135 | ip->keyword = defaultkeyword; /* settings for default keyword */\r |
| 136 | if(lmsid)\r |
| 137 | {\r |
| 138 | ip->lmsid = atoi(lmsid);\r |
| 139 | }\r |
| 140 | lastIP6range = ip;\r |
| 141 | }\r |
| 142 | else\r |
| 143 | {\r |
| 144 | lastIP6range = NULL;\r |
| 145 | }\r |
| 146 | \r |
| 147 | /* it is ugly to copy+paste and search+replace, but... */\r |
| 148 | if(ip6uplink)\r |
| 149 | {\r |
| 150 | concatenate(ip6prefix,ip6uplink,ptr);\r |
| 151 | ip6uplink=ptr;\r |
| 152 | TheIP(ip6uplink, FALSE); /* always new IP - more IPs in single uplink network */\r |
| 153 | ip->name = ip6uplink;\r |
| 154 | ip->keyword = defaultkeyword; /* settings for default keyword */\r |
| 155 | if(lmsid)\r |
| 156 | {\r |
| 157 | ip->lmsid = atoi(lmsid);\r |
| 158 | }\r |
| 159 | lastIP6uplink = ip;\r |
| 160 | }\r |
| 161 | else\r |
| 162 | {\r |
| 163 | lastIP6uplink = NULL;\r |
| 164 | }\r |
| 165 | \r |
| 166 | if_exists(ip, listhead, eq(ip->addr,ipaddr));\r |
| 167 | else\r |
| 168 | {\r |
| 169 | TheIP(ipaddr, (listhead==networks));\r |
| 170 | }\r |
| 171 | ip->name = ipname;\r |
| 172 | if(lmsid)\r |
| 173 | {\r |
| 174 | ip->lmsid = atoi(lmsid);\r |
| 175 | found_lmsid = TRUE;\r |
| 176 | }\r |
| 177 | }\r |
| 178 | \r |
| 179 | /* == This function parses hosts style main configuration file == */\r |
| 180 | void parse_hosts(char *hosts)\r |
| 181 | {\r |
| 182 | int groupidx = FIRSTGROUPID;\r |
| 183 | char *str, *ptr;\r |
| 184 | char *substring;\r |
| 185 | struct IP *network;\r |
| 186 | int pktratio;\r |
| 187 | \r |
| 188 | parse(hosts)\r |
| 189 | {\r |
| 190 | str = _;\r |
| 191 | \r |
| 192 | if(*str < '0' or *str > '9')\r |
| 193 | {\r |
| 194 | /* any line starting with non-number is comment ...*/\r |
| 195 | continue;\r |
| 196 | }\r |
| 197 | \r |
| 198 | ptr = strchr(str,'\r'); /* fore unix-style end of line */\r |
| 199 | if(ptr)\r |
| 200 | {\r |
| 201 | *ptr = 0;\r |
| 202 | }\r |
| 203 | \r |
| 204 | /* first, expand (rewrite) any predefined macros, if found*/\r |
| 205 | for_each(macro, macros)\r |
| 206 | {\r |
| 207 | substring = strstr(str, macro->rewrite_from);\r |
| 208 | if(substring)\r |
| 209 | {\r |
| 210 | int l1, l3;\r |
| 211 | *substring = 0;\r |
| 212 | substring += strlen(macro->rewrite_from);\r |
| 213 | l1 = strlen(str);\r |
| 214 | l3 = strlen(substring);\r |
| 215 | string(ptr, l1 + strlen(macro->rewrite_to) + l3 + 1);\r |
| 216 | strcpy(ptr, str);\r |
| 217 | strcat(ptr, macro->rewrite_to);\r |
| 218 | strcat(ptr, substring);\r |
| 219 | str = ptr;\r |
| 220 | /* printf("REWRITE: %s -> %s\n",_,str); */\r |
| 221 | }\r |
| 222 | }\r |
| 223 | \r |
| 224 | /* Does this IP share QoS class with some other ? */\r |
| 225 | substring = strstr(str, "sharing-");\r |
| 226 | if(substring)\r |
| 227 | { \r |
| 228 | substring += 8; /* "sharing-" */\r |
| 229 | parse_and_append_ip(str, ips);\r |
| 230 | ip->sharing = substring;\r |
| 231 | ip->keyword = defaultkeyword; /* settings for default keyword */\r |
| 232 | if(lastIP6range)\r |
| 233 | {\r |
| 234 | lastIP6range->sharing = substring;\r |
| 235 | lastIP6range = NULL;\r |
| 236 | }\r |
| 237 | if(lastIP6uplink)\r |
| 238 | {\r |
| 239 | lastIP6uplink->sharing = substring;\r |
| 240 | lastIP6uplink = NULL;\r |
| 241 | }\r |
| 242 | while(*substring and *substring != '\n')\r |
| 243 | {\r |
| 244 | substring++;\r |
| 245 | }\r |
| 246 | *substring = 0; \r |
| 247 | }\r |
| 248 | else\r |
| 249 | {\r |
| 250 | substring = strstr(str, "#255.");\r |
| 251 | if(substring and not strstr(str, "#255.255.255.255")) /* do not ping /32 uplinks */\r |
| 252 | {\r |
| 253 | /* netmask detected - save network*/\r |
| 254 | unsigned bit;\r |
| 255 | unsigned num, mask = 8;\r |
| 256 | substring += 5;\r |
| 257 | while(substring && *substring)\r |
| 258 | {\r |
| 259 | ptr = substring;\r |
| 260 | substring = strchr(substring, '.');\r |
| 261 | if(substring)\r |
| 262 | {\r |
| 263 | *substring = 0;\r |
| 264 | substring += 1;\r |
| 265 | }\r |
| 266 | num = atoi(ptr);\r |
| 267 | for(bit = 1; bit <=128 ; bit<<=1)\r |
| 268 | {\r |
| 269 | if(bit & num)\r |
| 270 | {\r |
| 271 | mask++;\r |
| 272 | }\r |
| 273 | }\r |
| 274 | } \r |
| 275 | parse_and_append_ip(str, networks);\r |
| 276 | ip->mask = mask;\r |
| 277 | }\r |
| 278 | else\r |
| 279 | {\r |
| 280 | /*Do we have to create new QoS class for this IP ? */\r |
| 281 | if_exists(keyword,keywords,(substring=strstr(str,keyword->key)))\r |
| 282 | {\r |
| 283 | parse_and_append_ip(str, ips);\r |
| 284 | if(lastIP6range)\r |
| 285 | {\r |
| 286 | lastIP6range->sharing = ip->name;\r |
| 287 | lastIP6range = NULL;\r |
| 288 | }\r |
| 289 | if(lastIP6uplink)\r |
| 290 | {\r |
| 291 | lastIP6uplink->sharing = ip->name;\r |
| 292 | lastIP6uplink = NULL;\r |
| 293 | }\r |
| 294 | ip->keyword = keyword;\r |
| 295 | keyword->ip_count++;\r |
| 296 | ip->prio = keyword->default_prio;\r |
| 297 | substring += strlen(keyword->key)+1;\r |
| 298 | ptr = substring;\r |
| 299 | while(*ptr and *ptr != '-')\r |
| 300 | {\r |
| 301 | ptr++;\r |
| 302 | }\r |
| 303 | if(*ptr == '-')\r |
| 304 | {\r |
| 305 | *ptr=0;\r |
| 306 | ip->max = ip->desired = atoi(ptr+1);\r |
| 307 | }\r |
| 308 | \r |
| 309 | ip->min = atoi(substring);\r |
| 310 | if(ip->min <= 0)\r |
| 311 | {\r |
| 312 | printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kb/s\n",\r |
| 313 | str, free_min);\r |
| 314 | ip->min = free_min;\r |
| 315 | }\r |
| 316 | \r |
| 317 | if(ip->max <= ip->min)\r |
| 318 | {\r |
| 319 | ip->fixedprio = TRUE;\r |
| 320 | ip->max = ip->min + ip->keyword->reserve_min;\r |
| 321 | }\r |
| 322 | else \r |
| 323 | {\r |
| 324 | ip->max -= ip->keyword->reserve_max;\r |
| 325 | if(ip->max < ip->min)\r |
| 326 | {\r |
| 327 | ip->max = ip->min;\r |
| 328 | }\r |
| 329 | }\r |
| 330 | \r |
| 331 | /* avg MTU bytes * 8 >> 10 = in bits, max is in kb/s */\r |
| 332 | pktratio = (ip->keyword->allowed_avgmtu*8) >> 10;\r |
| 333 | if(pktratio > 0)\r |
| 334 | {\r |
| 335 | ip->pps_limit = ip->max/pktratio;\r |
| 336 | if(ip->pps_limit > 10000) /* this limit seems to be hardcoded in iptables */\r |
| 337 | {\r |
| 338 | ip->pps_limit = 0; /* do not apply packet limits */\r |
| 339 | }\r |
| 340 | }\r |
| 341 | \r |
| 342 | ip->mark = FIRSTIPCLASS+1+class_count++; \r |
| 343 | update_network(ip->addr, ip);\r |
| 344 | \r |
| 345 | if_exists(group,groups,(group->min == ip->min)) \r |
| 346 | { \r |
| 347 | group->count++; \r |
| 348 | group->desired += ip->min;\r |
| 349 | ip->group = group->id; \r |
| 350 | }\r |
| 351 | else\r |
| 352 | {\r |
| 353 | create(group,Group);\r |
| 354 | group->min = ip->min;\r |
| 355 | group->id = groupidx++;\r |
| 356 | ip->group = group->id;\r |
| 357 | \r |
| 358 | if(group->min < 8) group->min = 8;\r |
| 359 | /* Warning - this is maybe because of primitive tc namespace, can be fixed */\r |
| 360 | /* it is because class IDs are derived from min. bandwidth. - xCh */\r |
| 361 | //if(group->min>MAX_GUARANTED_KBPS) group->min=MAX_GUARANTED_KBPS;\r |
| 362 | \r |
| 363 | group->count = 1;\r |
| 364 | group->desired = ip->min; \r |
| 365 | insert(group, groups, desc_order_by,min);\r |
| 366 | }\r |
| 367 | }//endif keyword-\r |
| 368 | }//endif netmask\r |
| 369 | }//endif sharing-\r |
| 370 | }\r |
| 371 | fail\r |
| 372 | {\r |
| 373 | perror(hosts);\r |
| 374 | exit(-1);\r |
| 375 | }\r |
| 376 | done; /* ugly macro end */\r |
| 377 | // TheIP("0.0.0.0", TRUE);\r |
| 378 | // ip->name = "TOTAL";\r |
| 379 | // ip->mask = 0;\r |
| 380 | }\r |