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