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