X-Git-Url: http://git.harvie.cz/?p=svn%2FPrometheus-QoS%2F.git;a=blobdiff_plain;f=parsehosts.c;h=18418eddb6b9c587d6ec23925a71c6f8ed011c48;hp=d37a71657236390195709271a7af41ea61dcdee2;hb=bb5e73853e892f8bce70e03344a81b192c2b84ff;hpb=0b9c3c198771800cc6c5fbacfc0272549626bcfd diff --git a/parsehosts.c b/parsehosts.c index d37a716..18418ed 100644 --- a/parsehosts.c +++ b/parsehosts.c @@ -1,4 +1,4 @@ -/* Modified by: xChaos, 20121007 */ +/* Modified by: xChaos, 20131029 */ #include "cll1-0.6.2.h" #include "ipstruct.h" @@ -7,9 +7,10 @@ #define FIRSTIPCLASS 2048 /* globals declared in prometheus.c */ -extern struct IP *ips, *ip, *sharedip; +extern struct IP *ips, *ip, *sharedip, *networks; extern struct Group *groups, *group; extern struct Keyword *keyword, *defaultkeyword, *keywords; +extern struct Macro *macro, *macros; extern int class_count; extern int ip_count; extern int found_lmsid; @@ -17,8 +18,11 @@ extern int free_min; extern const int highest_priority; extern char *ip6prefix; +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) +void TheIP(char *ipaddr, int is_network) { create(ip,IP); ip->name = ""; @@ -37,24 +41,36 @@ void TheIP(char *ipaddr) ip->direct = \ ip->traffic = \ ip->pktsup = \ + ip->pps_limit = \ ip->pktsdown = 0; ip->keyword = keywords; ip->v6 = (strchr(ip->addr,':')!=NULL); - push(ip,ips); + ip->mask = ((ip->v6)?64:32); + if(is_network) + { + push(ip, networks); + } + else + { + push(ip, ips); + } + ip_count++; } +struct IP *lastIP6; + /* == This function strips extra characters after IPv4 address and stores it = */ -void parse_ip(char *str) +void parse_and_append_ip(char *str, struct IP *listhead) { char *ptr, *ipaddr, *ip6range = NULL, *ipname = NULL, *lmsid = NULL; - if(ip6prefix) /* Try this only if IPv6 subsystem is active...*/ + if(ip6prefix) /* Try this only if IPv6 subsystem is active... */ { ptr = strstr(str, "::"); if(ptr && ptr-str > 4) { ptr -= 4; - duplicate(ptr,ip6range); + duplicate(ptr, ip6range); ptr = strstr(ip6range, "::"); if(ptr) { @@ -101,20 +117,25 @@ void parse_ip(char *str) if_exists(ip, ips, eq(ip->addr,ip6range)); else { - TheIP(ip6range); + TheIP(ip6range, FALSE); } ip->name = ip6range; - ip->sharing = ipname; + ip->keyword = defaultkeyword; /* settings for default keyword */ if(lmsid) { ip->lmsid = atoi(lmsid); } + lastIP6 = ip; + } + else + { + lastIP6 = NULL; } - if_exists(ip, ips, eq(ip->addr,ipaddr)); + if_exists(ip, listhead, eq(ip->addr,ipaddr)); else { - TheIP(ipaddr); + TheIP(ipaddr, (listhead==networks)); } ip->name = ipname; if(lmsid) @@ -130,10 +151,12 @@ void parse_hosts(char *hosts) int groupidx = FIRSTGROUPID; char *str, *ptr; char *substring; + struct IP *network; + int pktratio; parse(hosts) { - str=_; + str = _; if(*str < '0' or *str > '9') { @@ -141,15 +164,38 @@ void parse_hosts(char *hosts) continue; } + /* 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); + strcpy(ptr, str); + strcat(ptr, macro->rewrite_to); + strcat(ptr, substring); + str = ptr; + } + } + /* Does this IP share QoS class with some other ? */ substring = strstr(str, "sharing-"); if(substring) { substring += 8; /* "sharing-" */ - parse_ip(str); - ip_count++; + parse_and_append_ip(str, ips); ip->sharing = substring; ip->keyword = defaultkeyword; /* settings for default keyword */ + if(lastIP6) + { + lastIP6->sharing = substring; + lastIP6 = NULL; + } while(*substring and *substring != '\n') { substring++; @@ -158,71 +204,120 @@ void parse_hosts(char *hosts) } else { - /*Do we have to create new QoS class for this IP ? */ - - if_exists(keyword,keywords,(substring=strstr(str,keyword->key))) + substring = strstr(str, "#255."); + if(substring and not strstr(str, "#255.255.255.255")) /* do not ping /32 ranges */ { - parse_ip(str); - ip_count++; - ip->keyword = keyword; - keyword->ip_count++; - ip->prio = keyword->default_prio; - substring += strlen(keyword->key)+1; - ptr = substring; - while(*ptr and *ptr != '-') - { - ptr++; - } - if(*ptr == '-') + /* netmask detected - save network*/ + unsigned bit; + unsigned num, mask = 8; + substring += 5; + while(substring && *substring) { - *ptr=0; - ip->max = ip->desired = atoi(ptr+1); - } - ip->min = atoi(substring); - if(ip->min <= 0) - { - printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kb/s\n", - str, free_min); - ip->min = free_min; - } - if(ip->max <= ip->min) - { - ip->fixedprio = TRUE; - ip->max = ip->min + ip->keyword->reserve_min; - } - else - { - ip->max -= ip->keyword->reserve_max; - if(ip->maxmin) + ptr = substring; + substring = strchr(substring, '.'); + if(substring) { - ip->max=ip->min; + *substring = 0; + substring += 1; } - } - ip->mark = FIRSTIPCLASS+1+class_count++; - - if_exists(group,groups,(group->min == ip->min)) - { - group->count++; - group->desired += ip->min; - ip->group = group->id; - } - else + num = atoi(ptr); + for(bit = 1; bit <=128 ; bit<<=1) + { + if(bit & num) + { + mask++; + } + } + } + parse_and_append_ip(str, networks); + ip->mask = mask; + } + else + { + /*Do we have to create new QoS class for this IP ? */ + if_exists(keyword,keywords,(substring=strstr(str,keyword->key))) { - create(group,Group); - group->min = ip->min; - group->id = groupidx++; - ip->group = group->id; - - if(group->min < 8) group->min = 8; - /* Warning - this is maybe because of primitive tc namespace, can be fixed */ - /* it is because class IDs are derived from min. bandwidth. - xCh */ - //if(group->min>MAX_GUARANTED_KBPS) group->min=MAX_GUARANTED_KBPS; - - group->count = 1; - group->desired = ip->min; - insert(group, groups, desc_order_by,min); - } - }//endif keyword- + parse_and_append_ip(str, ips); + if(lastIP6) + { + lastIP6->sharing = ip->name; + lastIP6 = NULL; + } + ip->keyword = keyword; + keyword->ip_count++; + ip->prio = keyword->default_prio; + substring += strlen(keyword->key)+1; + ptr = substring; + while(*ptr and *ptr != '-') + { + ptr++; + } + if(*ptr == '-') + { + *ptr=0; + ip->max = ip->desired = atoi(ptr+1); + } + + ip->min = atoi(substring); + if(ip->min <= 0) + { + printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kb/s\n", + str, free_min); + ip->min = free_min; + } + + if(ip->max <= ip->min) + { + ip->fixedprio = TRUE; + ip->max = ip->min + ip->keyword->reserve_min; + } + else + { + ip->max -= ip->keyword->reserve_max; + if(ip->max < ip->min) + { + ip->max = ip->min; + } + } + + /* avg MTU bytes * 8 >> 10 = in bits, max is in kb/s */ + pktratio = (ip->keyword->allowed_avgmtu*8) >> 10; + if(pktratio > 0) + { + ip->pps_limit = ip->max/pktratio; + if(ip->pps_limit > 10000) /* this limit seems to be hardcoded in iptables */ + { + ip->pps_limit = 0; /* do not apply packet limits */ + } + } + + ip->mark = FIRSTIPCLASS+1+class_count++; + update_network(ip->addr, ip); + + if_exists(group,groups,(group->min == ip->min)) + { + group->count++; + group->desired += ip->min; + ip->group = group->id; + } + else + { + create(group,Group); + group->min = ip->min; + group->id = groupidx++; + ip->group = group->id; + + if(group->min < 8) group->min = 8; + /* Warning - this is maybe because of primitive tc namespace, can be fixed */ + /* it is because class IDs are derived from min. bandwidth. - xCh */ + //if(group->min>MAX_GUARANTED_KBPS) group->min=MAX_GUARANTED_KBPS; + + group->count = 1; + group->desired = ip->min; + insert(group, groups, desc_order_by,min); + } + }//endif keyword- + }//endif netmask }//endif sharing- } fail @@ -231,4 +326,7 @@ void parse_hosts(char *hosts) exit(-1); } done; /* ugly macro end */ +// TheIP("0.0.0.0", TRUE); +// ip->name = "TOTAL"; +// ip->mask = 0; } \ No newline at end of file