From: xchaos Date: Fri, 11 May 2012 09:35:46 +0000 (+0000) Subject: cleanup, code readability, etc. X-Git-Url: https://git.harvie.cz/?a=commitdiff_plain;h=260c2719f98b332eb9be47b348b0ef9615c16e33;p=svn%2FPrometheus-QoS%2F.git cleanup, code readability, etc. git-svn-id: https://dev.arachne.cz/repos/prometheus/trunk@162 251d49ef-1d17-4917-a970-b30cf55b089b --- diff --git a/prometheus.c b/prometheus.c index 5152fe4..3657448 100644 --- a/prometheus.c +++ b/prometheus.c @@ -7,7 +7,7 @@ /* Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ -/* Modified by: xChaos, 20111130 +/* Modified by: xChaos, 20120511 ludva, 20080415 Prometheus QoS is free software; you can redistribute it and/or @@ -33,14 +33,14 @@ #include "cll1-0.6.2.h" -const char *version = "0.8.3-b"; +const char *version = "0.8.3-c"; /* Version numbers: 0.8.3 is development releases ("beta"), 0.8.4 will be "stable" */ /* Debian(RPM) package versions/patchlevels: 0.7.9-2, 0.8.0-1, 0.8.0-2, etc. */ /* C source code development versions ("beta"): 0.7.9-a, 0.8.1-b, etc. */ /* C source code release versions: 0.8.0, 0.8.2, 0.8.4, etc. */ -const char *stats_html_signature = "Statistics generated by Prometheus QoS version %s
GPL+Copyright(C)2005-2011 Michael Polak, Arachne Labs
\n"; +const char *stats_html_signature = "Statistics generated by Prometheus QoS version %s
GPL+Copyright(C)2005-2012 Michael Polak, Arachne Labs
\n"; /* ======= All path names are defined here (for RPM patch) ======= */ @@ -252,7 +252,7 @@ void TheIP(void) char *very_ugly_ipv4_code(char *inip,int bitmask,int format_as_chainname) { /* warning: this function was debugged only for bitmask values 20,24,28 !!!*/ - int dot=0,n; + int dot=0, n; char *ip,*outip,*outptr,*fmt; duplicate(inip,ip); @@ -287,23 +287,39 @@ char *very_ugly_ipv4_code(char *inip,int bitmask,int format_as_chainname) } else { - char *cutdot=strchr(ip+1,'.'); /*for bitmask<24*/ - if(cutdot)*cutdot='\0'; + char *cutdot=strchr(ip+1,'.'); /*for bitmask<24*/ + if(cutdot) + { + *cutdot = '\0'; + } + if(format_as_chainname) - fmt="_%d_%d"; + { + fmt = "_%d_%d"; + } else - fmt=".%d"; + { + fmt = ".%d"; + } + if(bitmask%8) - n=atoi(ip+1)-atoi(ip+1)%(1<<(8-bitmask%8)); + { + n = atoi(ip+1)-atoi(ip+1)%(1<<(8-bitmask%8)); + } else - n=0; + { + n = 0; + } /*debug printf("%d/%d => [_%d_%d]\n",atoi(ip+1),bitmask,n,bitmask); */ sprintf(outptr,fmt,n,bitmask); - if(!format_as_chainname) while(bitmask<24) + if(!format_as_chainname) { - strcat(outip,".0"); - bitmask+=8; + while(bitmask<24) + { + strcat(outip,".0"); + bitmask+=8; + } } /* debug printf("[%s]\n",outip); */ return outip; @@ -444,8 +460,7 @@ void get_config(char *config_filename) ioption("htb-r2q",htb_r2q); ioption("magic-include-upload",include_upload); ioption("magic-treshold",magic_treshold); - option("filter-type", cnf); - + option("filter-type", cnf); /* not yet implemented: ioption("magic-fixed-packets",fixed_packets); ioption("magic-relative-packets",packet_limit); @@ -462,13 +477,13 @@ void get_config(char *config_filename) /*leaf discipline for keywords*/ for_each(keyword,keywords) { - if (!strcmpi(keyword->leaf_discipline, "")) + if(!strcmpi(keyword->leaf_discipline, "")) { keyword->leaf_discipline = qos_leaf; } } - if (strcmpi(cnf, "mark")) + if(strcmpi(cnf, "mark")) { filter_type = 2; mark = "CLASSIFY"; @@ -522,37 +537,61 @@ void get_traffic_statistics(void) if(valid) switch(col) { case 1: if(eq(ptr,"Chain")) + { setchainname=1; + } else if(eq(ptr,"pkts")) + { valid=0; + } else + { sscanf(ptr,"%lu",&pkts); + } break; case 2: if(setchainname) { if(!strncmp(ptr,"post_",5) || eq(ptr,"POSTROUTING")) - downloadflag=1; + { + downloadflag = 1; + } else - if(!strncmp(ptr,"forw_",5) || eq(ptr,"FORWARD")) - downloadflag=0; - + { + if(!strncmp(ptr,"forw_",5) || eq(ptr,"FORWARD")) + { + downloadflag = 0; + } + } if(eq(ptr,"post_common") || eq(ptr,"forw_common")) - commonflag=1; + { + commonflag = 1; + } } else - sscanf(ptr,"%Lu",&traffic); traffic+=(1<<19); traffic>>=20; + { + sscanf(ptr,"%Lu",&traffic); + traffic += (1<<19); + traffic >>= 20; + } break; case 3: if((strncmp(ptr,"post_",5) && strncmp(ptr,"forw_",5)) || commonflag) + { accept=eq(ptr,mark); - /*if (filter_type==1) accept=eq(ptr,"MARK"); else accept=eq(ptr,"CLASSIFY");*/ + } + /*if(filter_type==1) accept=eq(ptr,"MARK"); else accept=eq(ptr,"CLASSIFY");*/ break; case 8: if(downloadflag) { - if(strstr(proxy_ip,ptr))proxyflag=1; + if(strstr(proxy_ip,ptr)) + { + proxyflag=1; + } } else + { ipaddr=ptr; - break; + } + break; case 9: if(downloadflag)ipaddr=ptr;break; } @@ -816,7 +855,7 @@ void parse_ip_log(int argc, char **argv) case 8: case 9: case 10: - case 11: if (isalpha(*ptr)) /* character, not numeric string = date, just one*/ + case 11: if(isalpha(*ptr)) /* character, not numeric string = date, just one*/ { valid_columns(ptr2,ptr,' ',col2) switch(col2) { @@ -979,7 +1018,7 @@ program printf("\n\ Prometheus QoS - \"fair-per-IP\" Quality of Service setup utility.\n\ -Version %s - Copyright (C)2005-2011 Michael Polak (xChaos)\n\ +Version %s - Copyright (C)2005-2012 Michael Polak (xChaos)\n\ iptables-restore & burst tunning & classify modification by Ludva\n\ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); @@ -1089,22 +1128,23 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); if(*ptr=='-') { *ptr=0; - ip->max=ip->desired=atoi(ptr+1); + ip->max = ip->desired=atoi(ptr+1); } - ip->min=atoi(substring); - if(ip->min<=0) + ip->min = atoi(substring); + if(ip->min <= 0) { - printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kbps\n",str,free_min); - ip->min=free_min; + printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kbps\n", + str, free_min); + ip->min = free_min; } - if(ip->max<=ip->min) + if(ip->max <= ip->min) { - ip->fixedprio=1; - ip->max=ip->min+ip->keyword->reserve_min; + ip->fixedprio = 1; + ip->max = ip->min+ip->keyword->reserve_min; } else { - ip->max-=ip->keyword->reserve_max; + ip->max -= ip->keyword->reserve_max; if(ip->maxmin) { ip->max=ip->min; @@ -1115,13 +1155,13 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); if_exists(group,groups,group->min==ip->min) { group->count++; - group->desired+=ip->min; + group->desired += ip->min; ip->group = group->id; } else { create(group,Group); - group->min=ip->min; + group->min = ip->min; group->id = groupidx++; ip->group = group->id; @@ -1164,7 +1204,8 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); } if(!sharedip) { - printf("Unresolved shared connection: %s %s sharing-%s\n",ip->addr,ip->name,ip->sharing); + printf("Unresolved shared connection: %s %s sharing-%s\n", + ip->addr, ip->name, ip->sharing); } } @@ -1194,14 +1235,14 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); /*-----------------------------------------------------------------*/ iptables_file=fopen(iptablesfile,"w"); - if (iptables_file == NULL) + if(iptables_file == NULL) { puts("Cannot open iptablesfile!"); exit(-1); } log_file=fopen(cmdlog,"w"); - if (log_file == NULL) + if(log_file == NULL) { puts("Cannot open logfile!"); exit(-1); @@ -1258,7 +1299,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); save_line(":post_common - [0:0]"); save_line(":forw_common - [0:0]"); - for_each(ip,ips) if (ip->addr && *(ip->addr) && !eq(ip->addr,"0.0.0.0/0")) + for_each(ip,ips) if(ip->addr && *(ip->addr) && !eq(ip->addr,"0.0.0.0/0")) { buf=hash_id(ip->addr,bitmask); if_exists(idx,idxs,eq(idx->id,buf)) @@ -1314,7 +1355,8 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); for_each(idx,idxs) { subnet=subnet_id(idx->addr,idx->bitmask); - printf("%d: %s/%d\n",++i,subnet,idx->bitmask); + printf("%d: %s/%d\n", + ++i, subnet, idx->bitmask); sprintf(str,":post_%s - [0:0]", idx->id); save_line(str); @@ -1368,7 +1410,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); if(just_flush) { fclose(iptables_file); - if (log_file) fclose(log_file); + if(log_file) fclose(log_file); puts("Just flushed iptables and tc classes - now exiting ..."); exit(0); } @@ -1518,10 +1560,13 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); f=fopen("/var/run/prometheus.previous","w"); if(f) { - for_each(ip,ips) if(ip->traffic || ip->direct || ip->proxy || ip->upload) + for_each(ip,ips) { - fprintf(f,"%s %Lu %Lu %Lu %Lu\n", - ip->addr, ip->traffic, ip->direct, ip->proxy, ip->upload); + if(ip->traffic || ip->direct || ip->proxy || ip->upload) + { + fprintf(f,"%s %Lu %Lu %Lu %Lu\n", + ip->addr, ip->traffic, ip->direct, ip->proxy, ip->upload); + } } fclose(f); } @@ -1862,7 +1907,9 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); /*-----------------------------------------------------------------*/ i=0; +#ifdef DEBUG printf("%-22s %-15s mark\n","name","ip"); +#endif for_each(ip,ips) if(ip->mark>0) { @@ -1888,7 +1935,9 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); chain_postrouting="POSTROUTING"; } +#ifdef DEBUG printf("%-22s %-16s %04d ", ip->name, ip->addr, ip->mark); +#endif /* -------------------------------------------------------- mark download */ @@ -1918,25 +1967,29 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); if(ip->min) { /* -------------------------------------------------------- download class */ +#ifdef DEBUG printf("(down: %dk-%dk ", ip->min, ip->max); +#endif sprintf(str,"%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d", tc, lan, ip->group, ip->mark,ip->min,ip->max, burst, ip->prio); safe_run(str); - if (strcmpi(ip->keyword->leaf_discipline, "none")) + if(strcmpi(ip->keyword->leaf_discipline, "none")) { sprintf(str,"%s qdisc add dev %s parent 1:%d handle %d %s", tc, lan, ip->mark, ip->mark, ip->keyword->leaf_discipline); /*qos_leaf*/ safe_run(str); } - if (filter_type == 1) + if(filter_type == 1) { sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d", tc, lan, ip->mark, ip->mark); safe_run(str); } /* -------------------------------------------------------- upload class */ +#ifdef DEBUG printf("up: %dk-%dk)\n", (int)((ip->min/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed), (int)((ip->max/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed)); +#endif sprintf(str,"%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d", tc, wan, ip->group, ip->mark, @@ -1944,19 +1997,23 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); (int)((ip->max/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed), burst, ip->prio); safe_run(str); - if (strcmpi(ip->keyword->leaf_discipline, "none")) + if(strcmpi(ip->keyword->leaf_discipline, "none")) { sprintf(str,"%s qdisc add dev %s parent 1:%d handle %d %s",tc, wan, ip->mark, ip->mark, ip->keyword->leaf_discipline); /*qos_leaf*/ safe_run(str); } - if (filter_type == 1) + if(filter_type == 1) { sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d",tc, wan, ip->mark, ip->mark); safe_run(str); } } else + { +#ifdef DEBUG printf("(sharing %s)\n", ip->sharing); +#endif + } i++; } @@ -2013,7 +2070,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); tc,wan,parent,free_min,free_max,burst,lowest_priority); safe_run(str); /* tc SFQ */ - if (strcmpi(qos_leaf, "none")) + if(strcmpi(qos_leaf, "none")) { sprintf(str,"%s qdisc add dev %s parent 1:3 handle 3 %s",tc,lan,qos_leaf); safe_run(str); @@ -2030,7 +2087,10 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); } printf("Total IP count: %d\n", i); run_restore(); - if (log_file) fclose(log_file); + if(log_file) + { + fclose(log_file); + } return 0; /* that's all folks, thank you for reading it all the way up to this point ;-) */ /* bad luck C<<1 is not yet finished, I promise no sprintf() next time... */