version 1.0.0.-a - significat iptables parsing speed improvement
[svn/Prometheus-QoS/.git] / parsehosts.c
index 04fce9ffca9714d2fbfbc474f49d91211477d154..146e897b8fde295714b575f7a30166ff98038649 100644 (file)
@@ -1,4 +1,4 @@
-/* Modified by: xChaos, 20131028 */\r
+/* Modified by: xChaos, 20131220 */\r
 \r
 #include "cll1-0.6.2.h"\r
 #include "ipstruct.h"\r
@@ -10,6 +10,7 @@
 extern struct IP *ips, *ip, *sharedip, *networks;\r
 extern struct Group *groups, *group;\r
 extern struct Keyword *keyword, *defaultkeyword, *keywords;\r
+extern struct Macro *macro, *macros;\r
 extern int class_count;\r
 extern int ip_count;\r
 extern int found_lmsid;\r
@@ -17,33 +18,37 @@ extern int free_min;
 extern const int highest_priority;\r
 extern char *ip6prefix;\r
 \r
-struct IP* find_network_for_ip(char *ipaddr_orig);\r
+void update_network(char *look_for, struct IP* ip);\r
 /* implemented in networks.c */\r
 \r
 /* This must be object oriented! This looks almost like constructor ;-) */\r
 void TheIP(char *ipaddr, int is_network)\r
 {\r
  create(ip,IP);\r
- ip->name        = "";\r
- ip->addr        = ipaddr;\r
- ip->sharing     = NULL;\r
- ip->prio        = highest_priority+1;\r
- ip->lmsid       = -1;\r
- ip->fixedprio   = \\r
- ip->mark        = \\r
- ip->min         = \\r
- ip->max         = \\r
- ip->desired     = \\r
- ip->credit      = \\r
- ip->upload      = \\r
- ip->proxy       = \\r
- ip->direct      = \\r
- ip->traffic     = \\r
- ip->pktsup      = \\r
- ip->pktsdown    = 0;\r
- ip->keyword     = keywords;\r
- ip->v6          = (strchr(ip->addr,':')!=NULL);\r
- ip->mask        = ((ip->v6)?64:32);\r
+ ip->name         = "";\r
+ ip->addr         = ipaddr;\r
+ ip->sharing      = NULL;\r
+ ip->prio         = highest_priority+1;\r
+ ip->lmsid        = -1;\r
+ ip->fixedprio    = \\r
+ ip->aggregated   = \\r
+ ip->mark         = \\r
+ ip->min          = \\r
+ ip->max          = \\r
+ ip->desired      = \\r
+ ip->credit       = \\r
+ ip->upload       = \\r
+ ip->proxy        = \\r
+ ip->direct       = \\r
+ ip->traffic      = \\r
+ ip->traffic_down = \\r
+ ip->traffic_up   = \\r
+ ip->pktsup       = \\r
+ ip->pps_limit    = \\r
+ ip->pktsdown     = 0;\r
+ ip->keyword      = keywords;\r
+ ip->v6           = (strchr(ip->addr,':')!=NULL);\r
+ ip->mask         = ((ip->v6)?64:32);\r
  if(is_network)\r
  {\r
   push(ip, networks);\r
@@ -52,28 +57,48 @@ 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
-struct IP *lastIP6;\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
 void parse_and_append_ip(char *str, struct IP *listhead)\r
 {\r
- char *ptr, *ipaddr, *ip6range = NULL, *ipname = NULL, *lmsid = NULL;\r
+ char *ptr, *ipaddr, *nextip6, *ip6buf; \r
+ char *ip6uplink = NULL, *ip6range = NULL, *ipname = NULL, *lmsid = NULL;\r
 \r
  if(ip6prefix) /* Try this only if IPv6 subsystem is active... */\r
  {\r
   ptr = strstr(str, "::");\r
-  if(ptr && ptr-str > 4)\r
+  while(ptr && ptr-str > 4)\r
   {\r
-   ptr -= 4;   \r
-   duplicate(ptr, ip6range);\r
-   ptr = strstr(ip6range, "::");\r
+   nextip6 = strstr(ptr + 2, "::");\r
+   ptr -= 4;\r
+   duplicate(ptr, ip6buf);\r
+   ptr = strstr(ip6buf, "::");\r
    if(ptr)\r
    {\r
-    *(ptr+2) = 0;\r
+    if(*(ptr+2) == '+')\r
+    {\r
+     *(ptr+3) = 0; /* ends with ::+ */\r
+     ip6uplink = ip6buf;\r
+    }\r
+    else\r
+    {\r
+     *(ptr+2) = 0; /* ends with :: */\r
+     ip6range = ip6buf;\r
+    }    \r
    }\r
+   ptr = nextip6;\r
   }\r
  }\r
 \r
@@ -101,7 +126,7 @@ void parse_and_append_ip(char *str, struct IP *listhead)
  {\r
   ptr++;\r
  }\r
- ipname=ptr; \r
+ ipname = ptr; \r
  while(*ptr and *ptr!=' ' and *ptr!=9)\r
  {\r
   ptr++;\r
@@ -112,7 +137,7 @@ void parse_and_append_ip(char *str, struct IP *listhead)
  {\r
   concatenate(ip6prefix,ip6range,ptr);\r
   ip6range=ptr;\r
-  if_exists(ip, ips, eq(ip->addr,ip6range));\r
+  if_exists(ip, ips, eq(ip->addr,ip6range)); /* check - allocated range must be unique */\r
   else\r
   {\r
    TheIP(ip6range, FALSE);\r
@@ -123,11 +148,30 @@ void parse_and_append_ip(char *str, struct IP *listhead)
   {\r
    ip->lmsid = atoi(lmsid);\r
   }\r
-  lastIP6 = ip;\r
+  lastIP6range = ip;\r
  }\r
  else\r
  {\r
-  lastIP6 = NULL;\r
+  lastIP6range = NULL;\r
+ }\r
+\r
+ /* it is ugly to copy+paste and search+replace, but... */\r
+ if(ip6uplink)\r
+ {\r
+  concatenate(ip6prefix,ip6uplink,ptr);\r
+  ip6uplink=ptr;\r
+  TheIP(ip6uplink, FALSE); /* always new IP - more IPs in single uplink network */\r
+  ip->name = ip6uplink;\r
+  ip->keyword = defaultkeyword; /* settings for default keyword */\r
+  if(lmsid)\r
+  {\r
+   ip->lmsid = atoi(lmsid);\r
+  }\r
+  lastIP6uplink = ip;\r
+ }\r
+ else\r
+ {\r
+  lastIP6uplink = NULL;\r
  }\r
 \r
  if_exists(ip, listhead, eq(ip->addr,ipaddr));\r
@@ -150,17 +194,44 @@ void parse_hosts(char *hosts)
  char *str, *ptr;\r
  char *substring;\r
  struct IP *network;\r
+ int pktratio;\r
 \r
  parse(hosts)\r
  {\r
-  str=_;\r
+  str = _;\r
 \r
   if(*str < '0' or *str > '9')\r
   {\r
    /* any line starting with non-number is comment ...*/\r
    continue;\r
   }\r
-  \r
+\r
+  ptr = strchr(str,'\r'); /* fore unix-style end of line */\r
+  if(ptr)\r
+  {\r
+   *ptr = 0;\r
+  }\r
\r
+  /* first, expand (rewrite) any predefined macros, if found*/\r
+  for_each(macro, macros)\r
+  {\r
+   substring = strstr(str, macro->rewrite_from);\r
+   if(substring)\r
+   {\r
+    int l1, l3;\r
+    *substring = 0;\r
+    substring += strlen(macro->rewrite_from);\r
+    l1 = strlen(str);\r
+    l3 = strlen(substring);\r
+    string(ptr, l1 + strlen(macro->rewrite_to) + l3 + 1);\r
+    strcpy(ptr, str);\r
+    strcat(ptr, macro->rewrite_to);\r
+    strcat(ptr, substring);\r
+    str = ptr;\r
+    /*  printf("REWRITE: %s -> %s\n",_,str); */\r
+   }\r
+  }\r
+\r
   /* Does this IP share QoS class with some other ? */\r
   substring = strstr(str, "sharing-");\r
   if(substring)\r
@@ -169,10 +240,15 @@ void parse_hosts(char *hosts)
    parse_and_append_ip(str, ips);\r
    ip->sharing = substring;\r
    ip->keyword = defaultkeyword; /* settings for default keyword */\r
-   if(lastIP6)\r
+   if(lastIP6range)\r
    {\r
-    lastIP6->sharing = substring;\r
-    lastIP6 = NULL;\r
+    lastIP6range->sharing = substring;\r
+    lastIP6range = NULL;\r
+   }\r
+   if(lastIP6uplink)\r
+   {\r
+    lastIP6uplink->sharing = substring;\r
+    lastIP6uplink = NULL;\r
    }\r
    while(*substring and *substring != '\n')\r
    {\r
@@ -183,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 ranges */\r
+   if(substring and not strstr(str, "#255.255.255.255")) /* ignore /32 subnets */\r
    {\r
     /* netmask detected - save network*/\r
     unsigned bit;\r
@@ -212,14 +288,55 @@ 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(lastIP6)\r
+     if(lastIP6range)\r
+     {\r
+      lastIP6range->sharing = ip->name;\r
+      lastIP6range = NULL;\r
+     }\r
+     if(lastIP6uplink)\r
      {\r
-      lastIP6->sharing = ip->name;\r
-      lastIP6 = NULL;\r
+      lastIP6uplink->sharing = ip->name;\r
+      lastIP6uplink = NULL;\r
      }\r
      ip->keyword = keyword;\r
      keyword->ip_count++;\r
@@ -235,6 +352,7 @@ void parse_hosts(char *hosts)
       *ptr=0;\r
       ip->max = ip->desired = atoi(ptr+1);\r
      }\r
+\r
      ip->min = atoi(substring);\r
      if(ip->min <= 0)\r
      {\r
@@ -242,6 +360,7 @@ void parse_hosts(char *hosts)
              str, free_min);\r
       ip->min = free_min;\r
      }\r
+\r
      if(ip->max <= ip->min)\r
      {\r
       ip->fixedprio = TRUE;\r
@@ -250,24 +369,35 @@ void parse_hosts(char *hosts)
      else \r
      {\r
       ip->max -= ip->keyword->reserve_max;\r
-      if(ip->max<ip->min)\r
+      if(ip->max < ip->min)\r
       {\r
-       ip->max=ip->min;\r
+       ip->max = ip->min;\r
       }\r
      }\r
-     ip->mark = FIRSTIPCLASS+1+class_count++;\r
 \r
-     network = find_network_for_ip(ip->addr);\r
-     if(network)\r
+     /* avg MTU bytes * 8 >> 10 = in bits, max is in kb/s  */\r
+     pktratio = (ip->keyword->allowed_avgmtu*8) >> 10;\r
+     if(pktratio > 0)\r
      {\r
-      network->min += ip->min;\r
-      network->desired += ip->max;\r
-      if(ip->max > network->max)\r
+      ip->pps_limit = ip->max/pktratio;\r
+      if(ip->pps_limit > 10000) /* this limit seems to be hardcoded in iptables */\r
       {\r
-       network->max = ip->max;\r
+       ip->pps_limit = 0; /* do not apply packet limits */\r
       }\r
      }\r
 \r
+     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
@@ -290,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
@@ -300,4 +430,7 @@ void parse_hosts(char *hosts)
   exit(-1);\r
  }\r
  done; /* ugly macro end */\r
+// TheIP("0.0.0.0", TRUE);\r
+// ip->name = "TOTAL";\r
+// ip->mask = 0;\r
 }
\ No newline at end of file
This page took 0.214271 seconds and 4 git commands to generate.