X-Git-Url: http://git.harvie.cz/?p=svn%2FPrometheus-QoS%2F.git;a=blobdiff_plain;f=parsehosts.c;h=8c9eca159c6d1b3304f8e45342c1c04d2eeaced9;hp=b21af786e7d7c87a4dc0d5afb52107f08fe200a0;hb=dba7666bdacf8846560e5414b6c9535434e98f8b;hpb=cdfd88eb2cfba828e5bfc3f77eba37b3cba286ce diff --git a/parsehosts.c b/parsehosts.c index b21af78..8c9eca1 100644 --- a/parsehosts.c +++ b/parsehosts.c @@ -1,4 +1,4 @@ -/* Modified by: xChaos, 20131029 */ +/* Modified by: xChaos, 20131220 */ #include "cll1-0.6.2.h" #include "ipstruct.h" @@ -10,40 +10,47 @@ extern struct IP *ips, *ip, *sharedip, *networks; extern struct Group *groups, *group; extern struct Keyword *keyword, *defaultkeyword, *keywords; +extern struct Macro *macro, *macros; +extern struct Textfile *previous_classmap, *textline; extern int class_count; +extern int mix_new_hosts; extern int ip_count; extern int found_lmsid; extern int free_min; extern const int highest_priority; extern char *ip6prefix; -struct IP* find_network_for_ip(char *ipaddr_orig); +void update_network(char *look_for, struct IP* ip); /* implemented in networks.c */ /* This must be object oriented! This looks almost like constructor ;-) */ void TheIP(char *ipaddr, int is_network) { create(ip,IP); - ip->name = ""; - ip->addr = ipaddr; - ip->sharing = NULL; - ip->prio = highest_priority+1; - ip->lmsid = -1; - ip->fixedprio = \ - ip->mark = \ - ip->min = \ - ip->max = \ - ip->desired = \ - ip->credit = \ - ip->upload = \ - ip->proxy = \ - ip->direct = \ - ip->traffic = \ - ip->pktsup = \ - ip->pktsdown = 0; - ip->keyword = keywords; - ip->v6 = (strchr(ip->addr,':')!=NULL); - ip->mask = ((ip->v6)?64:32); + ip->name = ""; + ip->addr = ipaddr; + ip->sharing = NULL; + ip->prio = highest_priority+1; + ip->lmsid = -1; + ip->fixedprio = \ + ip->aggregated = \ + ip->mark = \ + ip->min = \ + ip->max = \ + ip->desired = \ + ip->credit = \ + ip->upload = \ + ip->proxy = \ + ip->direct = \ + ip->traffic = \ + ip->traffic_down = \ + ip->traffic_up = \ + ip->pktsup = \ + ip->pps_limit = \ + ip->pktsdown = 0; + ip->keyword = keywords; + ip->v6 = (strchr(ip->addr,':')!=NULL); + ip->mask = ((ip->v6)?64:32); if(is_network) { push(ip, networks); @@ -55,25 +62,37 @@ void TheIP(char *ipaddr, int is_network) ip_count++; } -struct IP *lastIP6; +struct IP *lastIP6range, *lastIP6uplink; /* == This function strips extra characters after IPv4 address and stores it = */ void parse_and_append_ip(char *str, struct IP *listhead) { - char *ptr, *ipaddr, *ip6range = NULL, *ipname = NULL, *lmsid = NULL; + char *ptr, *ipaddr, *nextip6, *ip6buf; + char *ip6uplink = NULL, *ip6range = NULL, *ipname = NULL, *lmsid = NULL; if(ip6prefix) /* Try this only if IPv6 subsystem is active... */ { ptr = strstr(str, "::"); - if(ptr && ptr-str > 4) + while(ptr && ptr-str > 4) { - ptr -= 4; - duplicate(ptr, ip6range); - ptr = strstr(ip6range, "::"); + nextip6 = strstr(ptr + 2, "::"); + ptr -= 4; + duplicate(ptr, ip6buf); + ptr = strstr(ip6buf, "::"); if(ptr) { - *(ptr+2) = 0; + if(*(ptr+2) == '+') + { + *(ptr+3) = 0; /* ends with ::+ */ + ip6uplink = ip6buf; + } + else + { + *(ptr+2) = 0; /* ends with :: */ + ip6range = ip6buf; + } } + ptr = nextip6; } } @@ -101,7 +120,7 @@ void parse_and_append_ip(char *str, struct IP *listhead) { ptr++; } - ipname=ptr; + ipname = ptr; while(*ptr and *ptr!=' ' and *ptr!=9) { ptr++; @@ -112,7 +131,7 @@ void parse_and_append_ip(char *str, struct IP *listhead) { concatenate(ip6prefix,ip6range,ptr); ip6range=ptr; - if_exists(ip, ips, eq(ip->addr,ip6range)); + if_exists(ip, ips, eq(ip->addr,ip6range)); /* check - allocated range must be unique */ else { TheIP(ip6range, FALSE); @@ -123,11 +142,30 @@ void parse_and_append_ip(char *str, struct IP *listhead) { ip->lmsid = atoi(lmsid); } - lastIP6 = ip; + lastIP6range = ip; + } + else + { + lastIP6range = NULL; + } + + /* it is ugly to copy+paste and search+replace, but... */ + if(ip6uplink) + { + concatenate(ip6prefix,ip6uplink,ptr); + ip6uplink=ptr; + TheIP(ip6uplink, FALSE); /* always new IP - more IPs in single uplink network */ + ip->name = ip6uplink; + ip->keyword = defaultkeyword; /* settings for default keyword */ + if(lmsid) + { + ip->lmsid = atoi(lmsid); + } + lastIP6uplink = ip; } else { - lastIP6 = NULL; + lastIP6uplink = NULL; } if_exists(ip, listhead, eq(ip->addr,ipaddr)); @@ -150,17 +188,44 @@ void parse_hosts(char *hosts) char *str, *ptr; char *substring; struct IP *network; + int pktratio; parse(hosts) { - str=_; + str = _; if(*str < '0' or *str > '9') { /* any line starting with non-number is comment ...*/ continue; } - + + ptr = strchr(str,'\r'); /* fore unix-style end of line */ + if(ptr) + { + *ptr = 0; + } + + /* first, expand (rewrite) any predefined macros, if found*/ + for_each(macro, macros) + { + substring = strstr(str, macro->rewrite_from); + if(substring) + { + int l1, l3; + *substring = 0; + substring += strlen(macro->rewrite_from); + l1 = strlen(str); + l3 = strlen(substring); + string(ptr, l1 + strlen(macro->rewrite_to) + l3 + 1); + strcpy(ptr, str); + strcat(ptr, macro->rewrite_to); + strcat(ptr, substring); + str = ptr; + /* printf("REWRITE: %s -> %s\n",_,str); */ + } + } + /* Does this IP share QoS class with some other ? */ substring = strstr(str, "sharing-"); if(substring) @@ -169,10 +234,15 @@ void parse_hosts(char *hosts) parse_and_append_ip(str, ips); ip->sharing = substring; ip->keyword = defaultkeyword; /* settings for default keyword */ - if(lastIP6) + if(lastIP6range) + { + lastIP6range->sharing = substring; + lastIP6range = NULL; + } + if(lastIP6uplink) { - lastIP6->sharing = substring; - lastIP6 = NULL; + lastIP6uplink->sharing = substring; + lastIP6uplink = NULL; } while(*substring and *substring != '\n') { @@ -183,7 +253,7 @@ void parse_hosts(char *hosts) else { substring = strstr(str, "#255."); - if(substring and not strstr(str, "#255.255.255.255")) /* do not ping /32 ranges */ + if(substring and not strstr(str, "#255.255.255.255")) /* do not ping /32 uplinks */ { /* netmask detected - save network*/ unsigned bit; @@ -216,10 +286,15 @@ void parse_hosts(char *hosts) if_exists(keyword,keywords,(substring=strstr(str,keyword->key))) { parse_and_append_ip(str, ips); - if(lastIP6) + if(lastIP6range) { - lastIP6->sharing = ip->name; - lastIP6 = NULL; + lastIP6range->sharing = ip->name; + lastIP6range = NULL; + } + if(lastIP6uplink) + { + lastIP6uplink->sharing = ip->name; + lastIP6uplink = NULL; } ip->keyword = keyword; keyword->ip_count++; @@ -235,6 +310,7 @@ void parse_hosts(char *hosts) *ptr=0; ip->max = ip->desired = atoi(ptr+1); } + ip->min = atoi(substring); if(ip->min <= 0) { @@ -242,6 +318,7 @@ void parse_hosts(char *hosts) str, free_min); ip->min = free_min; } + if(ip->max <= ip->min) { ip->fixedprio = TRUE; @@ -250,24 +327,42 @@ void parse_hosts(char *hosts) else { ip->max -= ip->keyword->reserve_max; - if(ip->maxmin) + if(ip->max < ip->min) { - ip->max=ip->min; + ip->max = ip->min; } } - ip->mark = FIRSTIPCLASS+1+class_count++; - network = find_network_for_ip(ip->addr); - if(network) + /* avg MTU bytes * 8 >> 10 = in bits, max is in kb/s */ + pktratio = (ip->keyword->allowed_avgmtu*8) >> 10; + if(pktratio > 0) { - network->min += ip->min; - network->desired += ip->max; - if(ip->max > network->max) + ip->pps_limit = ip->max/pktratio; + if(ip->pps_limit > 10000) /* this limit seems to be hardcoded in iptables */ { - network->max = ip->max; + ip->pps_limit = 0; /* do not apply packet limits */ } } + if(mix_new_hosts) + for_each(textline, previous_classmap) + { + ptr = strchr(textline->str, ' '); + if(ptr) + { + if(!strncmp(ip->addr, textline->str, ptr-textline->str)) + { + ip->mark = atoi(ptr+1); + printf("Match class: %s %d\n", ip->addr, ip->mark); + } + } + } + + if(!mix_new_hosts || !ip->mark) + ip->mark = FIRSTIPCLASS+1+class_count++; + + update_network(ip->addr, ip); + if_exists(group,groups,(group->min == ip->min)) { group->count++; @@ -300,4 +395,7 @@ void parse_hosts(char *hosts) exit(-1); } done; /* ugly macro end */ -} \ No newline at end of file +// TheIP("0.0.0.0", TRUE); +// ip->name = "TOTAL"; +// ip->mask = 0; +}