From: xchaos Date: Tue, 7 Jan 2020 11:42:25 +0000 (+0000) Subject: version 1.0.0.-a - significat iptables parsing speed improvement X-Git-Url: https://git.harvie.cz/?p=svn%2FPrometheus-QoS%2F.git;a=commitdiff_plain;h=47b5fd64878b8e1206cbdc7fc2ec65308b0d9e89 version 1.0.0.-a - significat iptables parsing speed improvement git-svn-id: https://dev.arachne.cz/repos/prometheus/trunk@277 251d49ef-1d17-4917-a970-b30cf55b089b --- diff --git a/ipstruct.h b/ipstruct.h index 80810fc..3b80747 100644 --- a/ipstruct.h +++ b/ipstruct.h @@ -1,4 +1,6 @@ -/* Modified by: xChaos, 20131029 */ +/* Modified by: xChaos, 20200104 */ + +#define MONITORINGTRHU_CTU struct IP { @@ -26,6 +28,10 @@ struct IP unsigned long pktsup; unsigned long pktsdown; struct Keyword *keyword; +#ifdef MONITORINGTRHU_CTU + char *technology_str; + char *ruian_id_str; +#endif int v6; int mask; struct IP *uplink; @@ -102,3 +108,13 @@ struct Interface void TheIP(char *ipaddr, int is_network); /* function implemented in parsehosts.c */ + +#ifdef MONITORINGTRHU_CTU +struct Technology +{ + char *filename; + list(Technology); +}; + +extern struct Technology *technologies, *technology; +#endif diff --git a/parsehosts.c b/parsehosts.c index 47b06bd..146e897 100644 --- a/parsehosts.c +++ b/parsehosts.c @@ -57,9 +57,17 @@ void TheIP(char *ipaddr, int is_network) { push(ip, ips); } +#ifdef MONITORINGTRHU_CTU + ip->technology_str = NULL; + ip->ruian_id_str = NULL; +#endif + ip_count++; } +#ifdef MONITORINGTRHU_CTU +struct Technology *technologies = NULL, *technology = NULL; +#endif struct IP *lastIP6range, *lastIP6uplink; /* == This function strips extra characters after IPv4 address and stores it = */ @@ -251,7 +259,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 uplinks */ + if(substring and not strstr(str, "#255.255.255.255")) /* ignore /32 subnets */ { /* netmask detected - save network*/ unsigned bit; @@ -280,9 +288,45 @@ void parse_hosts(char *hosts) } else { + /* Main branch - most IP addresses go here */ /*Do we have to create new QoS class for this IP ? */ if_exists(keyword,keywords,(substring=strstr(str,keyword->key))) { +#ifdef MONITORINGTRHU_CTU +//special hack only to generate certain required CSV statistics for www.ctu.cz (regulation body) + char *found_at = strchr(str, '@'); + char *ruian_id_str = NULL; + technology = NULL; + if(found_at) + { + int len; + char *found_ruian_end = strchr(found_at, ' '); + char *found_tech_str = found_at; + while(found_tech_str-- > str && *found_tech_str != ' ' && *found_tech_str != '#'); + if(found_tech_str > str) + { + len = found_at - found_tech_str - 1; + for_each(technology, technologies) + if(!strncmp(technology->filename, found_tech_str + 1, len)) + break; + if(!technology) + { + create(technology,Technology); + string(technology->filename, len + 1); + strncpy(technology->filename, found_tech_str + 1, len); + technology->filename[len] = 0; + push(technology, technologies); + } + if(found_ruian_end) + { + len = found_ruian_end - found_at - 1; + string(ruian_id_str, len + 1); + strncpy(ruian_id_str, found_at + 1, len); + ruian_id_str[len] = 0; + } + } + } +#endif parse_and_append_ip(str, ips); if(lastIP6range) { @@ -345,6 +389,15 @@ void parse_hosts(char *hosts) ip->mark = FIRSTIPCLASS+1+class_count++; update_network(ip->addr, ip); +#ifdef MONITORINGTRHU_CTU + if(technology) + { + ip->technology_str = technology->filename; + ip->ruian_id_str = ruian_id_str; + /* debug printf("[%s,%d,%s,%d]\n", ip->technology_str,ip->lmsid, ip->ruian_id_str, ip->max); */ + } +#endif + if_exists(group,groups,(group->min == ip->min)) { group->count++; @@ -367,7 +420,7 @@ void parse_hosts(char *hosts) group->desired = ip->min; insert(group, groups, desc_order_by,min); } - }//endif keyword- + }//endif keyword- }//endif netmask }//endif sharing- } @@ -380,4 +433,4 @@ void parse_hosts(char *hosts) // TheIP("0.0.0.0", TRUE); // ip->name = "TOTAL"; // ip->mask = 0; -} +} \ No newline at end of file diff --git a/parseiptables.c b/parseiptables.c index db984fa..3685928 100644 --- a/parseiptables.c +++ b/parseiptables.c @@ -3,7 +3,7 @@ #include "cll1-0.6.2.h" #include "ipstruct.h" -#define STRLEN 512 +#define STRLEN 256 /* globals declared in prometheus.c */ extern struct IP *ips, *ip, *sharedip; @@ -14,6 +14,7 @@ extern int free_max; extern int include_upload; int traffic_detected = 0; +extern char *iptablesdump; /* ===================== traffic analyser - uses iptables ================ */ @@ -21,29 +22,31 @@ void get_traffic_statistics(const char *whichiptables, int ipv6) { char *str,*cmd; int downloadflag = 0; - - textfile(Pipe,str) *line,*lines=NULL; - string(str,STRLEN); + FILE *f; string(cmd,STRLEN); - sprintf(cmd,"%s -L -v -x -n -t mangle", whichiptables); - shell(cmd); - input(str,STRLEN) - { - create(line,Pipe); - line->str=str; - string(str,STRLEN); - append(line,lines); - } + sprintf(cmd, "%s -L -v -x -n -t mangle>%s", whichiptables, iptablesdump); + /*-----------------------------------------------------------------*/ + printf("Running %s ...\n", cmd); + /*-----------------------------------------------------------------*/ + system(cmd); + /*-----------------------------------------------------------------*/ + printf("Processing %s ...\n", iptablesdump); + /*-----------------------------------------------------------------*/ + f = fopen(iptablesdump,"r"); + if(!f) + perror(iptablesdump); - for_each(line,lines) + while(!feof(f)) { int col, accept = 0, /*proxyflag = 0, */valid = 1, setchainname = 0, commonflag = 0; unsigned long long traffic = 0; unsigned long pkts = 0; char *ipaddr = NULL,*ptr; - valid_columns(ptr, line->str, ' ', col) + string(str, STRLEN); + fgets(str, STRLEN, f); + valid_columns(ptr, str, ' ', col) if(valid) switch(col) { case 1: if(eq(ptr,"Chain")) diff --git a/prometheus.c b/prometheus.c index 474d5cd..1c91738 100644 --- a/prometheus.c +++ b/prometheus.c @@ -2,12 +2,12 @@ /* Prometheus QoS - you can "steal fire" from your ISP */ /* "fair-per-IP" quality of service (QoS) utility */ /* requires Linux 2.4.x or 2.6.x with HTB support */ -/* Copyright(C) 2005-2019 Michael Polak, Arachne Aerospace */ +/* Copyright(C) 2005-2020 Michael Polak, Arachne Aerospace */ /* iptables-restore support Copyright(C) 2007-2008 ludva */ /* Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ -/* Modified by: xChaos, 20190912 +/* Modified by: xChaos, 20200107 ludva, 20080415 Prometheus QoS is free software; you can redistribute it and/or @@ -29,7 +29,7 @@ #include "cll1-0.6.2.h" #include "ipstruct.h" -const char *version = "0.9.0-c"; +const char *version = "1.0.0-a"; /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* Versions: 0.9.0 is development release, 1.0 will be "stable" */ @@ -40,7 +40,7 @@ const char *version = "0.9.0-c"; /* Warning: unofficial Github mirror is not supported by author! */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ -const char *stats_html_signature = "Statistics generated by Prometheus QoS version %s
GPL+Copyright(C)2005-2019 Michael Polak, Arachne Labs
\n"; +const char *stats_html_signature = "Statistics generated by Prometheus QoS version %s
GPL+Copyright(C)2005-2020 Michael Polak, Arachne Labs
\n"; #define STRLEN 512 #undef DEBUG @@ -63,6 +63,7 @@ char *upstreamfile = "/etc/prometheus/upstream.interfaces"; /* list of interf char *downstreamfile = "/etc/prometheus/downstream.interfaces"; /* list of interfaces to manage */ char *qosfreefile = "/etc/prometheus/qosfree.interfaces"; /* list of interfaces to manage */ char *iptablesfile = "/var/spool/prometheus.iptables"; /* temporary file for iptables-restore*/ +char *iptablesdump = "/var/spool/prometheus.iptables-dump"; /* temporary file for iptables -L -v -x -n -t mangle */ char *ip6tablesfile = "/var/spool/prometheus.ip6tables"; /* temporary file for ip6tables-restore*/ char *credit = "/var/lib/misc/prometheus.credit"; /* credit log file */ char *classmap = "/var/lib/misc/prometheus.classes"; /* credit log file */ @@ -290,6 +291,7 @@ void get_config(char *config_filename) option("ip6tables-save",ip6tablessave); option("ip6tables-restore",ip6tablesrestore); option("iptables-in-filename",iptablesfile); + option("iptables-dump-filename",iptablesdump); option("ip6tables-in-filename",ip6tablesfile); option("hosts",hosts); option("downstream-interfaces-list-filename",downstreamfile); @@ -640,14 +642,34 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); printf("Parsing class defintion file %s ...\n", hosts); /*-----------------------------------------------------------------*/ parse_hosts(hosts); -/* - //this was pretty dumb idea anyway... - if(just_networks) + +#ifdef MONITORINGTRHU_CTU +//special hack only to generate certain required CSV statistics for www.ctu.cz (regulation body) +//not required for everyday use, requires special syntax sugar in hosts file, see parsehosts.c + for_each(technology, technologies) { - analyse_topology("/usr/sbin/traceroute -n -m 10 -w 2 %s.%d"); - exit(-1); - } -*/ + char *filename; + FILE *f; + string(filename, strlen(log_dir) + strlen(technology->filename) + 5); + strcpy(filename, html_log_dir); + strcat(filename, technology->filename); + strcat(filename, ".csv"); + /*-----------------------------------------------------------------*/ + printf("Writing report file %s ...\n", filename); + /*-----------------------------------------------------------------*/ + f = fopen(filename, "w"); + if(f) + { + for_each(ip, ips) if(eq(technology->filename, ip->technology_str)) + { + fprintf(f,"%d,%s,%d\n", ip->lmsid, ip->ruian_id_str, ip->max); + } + fclose(f); + } + else + perror(filename); + } +#endif /*-----------------------------------------------------------------*/ puts("Resolving shared connections ..."); @@ -1220,6 +1242,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version); printf("Writing statistics into HTML page %s ...\n", html); /*-----------------------------------------------------------------*/ write_htmlandlogs(html, d, total, just_preview); + printf("\n"); } if(just_preview)