version 1.0.0.-a - significat iptables parsing speed improvement
authorxchaos <xchaos@251d49ef-1d17-4917-a970-b30cf55b089b>
Tue, 7 Jan 2020 11:42:25 +0000 (11:42 +0000)
committerxchaos <xchaos@251d49ef-1d17-4917-a970-b30cf55b089b>
Tue, 7 Jan 2020 11:42:25 +0000 (11:42 +0000)
git-svn-id: https://dev.arachne.cz/repos/prometheus/trunk@277 251d49ef-1d17-4917-a970-b30cf55b089b

ipstruct.h
parsehosts.c
parseiptables.c
prometheus.c

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