IPv6 support: step 1. prometheus.conf + hosts parsing
[svn/Prometheus-QoS/.git] / prometheus.c
index 094008789cd56e3029e1e57641805463d2fb09bd..70bea1f2355559c9dca504dde27523d95bc36558 100644 (file)
@@ -1,13 +1,13 @@
-  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */\r
- /*     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-2013 Michael Polak, Arachne Labs      */\r
-/*    iptables-restore support Copyright(C) 2007-2008 ludva     */\r
-/*  Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf  */\r
-/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */\r
-\r
-/* Modified by: xChaos, 20130107\r
+/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */\r
+/* 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-2013 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, 20130116\r
                  ludva, 20080415\r
  \r
    Prometheus QoS is free software; you can redistribute it and/or\r
    \r
    GNU General Public License is located in file COPYING */\r
 \r
-#define STRLEN 512\r
-#undef DEBUG\r
-\r
 #include "cll1-0.6.2.h"\r
 #include "ipstruct.h"\r
 \r
-const char *version = "0.8.3-g";\r
+const char *version = "0.8.3-h";\r
 \r
-/* Version numbers: 0.8.3 is development releases ("beta"), 0.8.4 will be "stable" */\r
-/* Debian(RPM) package versions/patchlevels: 0.7.9-2, 0.8.0-1, 0.8.0-2, etc. */\r
-/* C source code development versions ("beta"): 0.7.9-a, 0.8.1-b, etc. */\r
-/* C source code release versions: 0.8.0, 0.8.2, 0.8.4, etc. */\r
+/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */\r
+/* Versions: 0.8.3 is development release, 0.8.4 will be "stable"  */\r
+/* Official Trac URL: https://dev.arachne.cz/svn/prometheus        */\r
+/* Official SVN URL: https://dev.arachne.cz/repos/prometheus       */\r
+/* BTC donations account: 19rriLx8vR19wGefPaMhakqnCYNYwjLvxq       */\r
+/* CZK donations account: 2900242944/2010 (transparent account)    */\r
+/* 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-2013 Michael Polak, <a target=\"_blank\" href=\"http://www.arachne.cz/\">Arachne Labs</a></span>\n";\r
 \r
-/* ======= Help screen is hopefuly self-documenting part of code :-) ======= */\r
-\r
-void help(void)\r
-{\r
- puts("Command line switches:\n\\r
-\n\\r
--d   Dry run (preview tc and iptables commands on stdout)\n\\r
--r   Run (reset all statistics and start shaping - daily usage)\n\\r
--p   just generate Preview of data transfer statistics and exit (after -r)\n\\r
--s   start Shaping FUP limits (keeps data transfer stat like -p) (after -r)\n\\r
--n   run Now (like -r delay - overrides qos-free-delay keyword, after boot)\n\\r
--f   just Flush iptables and tc classes and exit (stop shaping, no QiS)\n\\r
--9   emergency iptables flush (like -f, but dumps data transfer statistics)\n\\r
-\n\\r
--c filename  force alternative /etc/prometheus/prometheus.conf filename\n\\r
--h filename  force alternative /etc/hosts filename (overrides hosts keyword)\n\\r
--l Mmm YYYY  generate HTML summary of Logged traffic (Mmm=Jan-Dec) (and exit)\n\\r
--m           generate HTML summary of traffic for yesterday's Month (and exit)\n\\r
--y           generate HTML summary of traffic for yesterday's Year (and exit)\n\\r
--? --help    show this help scree (and exit)\n\\r
--v --version show Version number of this utility (and exit)\n");\r
-}\r
+#define STRLEN 512\r
+#undef DEBUG\r
 \r
 /* ======= All path names are defined here (for RPM patch) =======  */\r
 \r
-const char        *tc = "/sbin/tc"; /* requires tc with HTB support */\r
-const char  *iptables = "/sbin/iptables"; /* requires iptables utility */\r
-const char *iptablessave = "/sbin/iptables-save"; /* not yet required */\r
-const char *iptablesrestore = "/sbin/iptables-restore";  /* requires iptables-restore */\r
-const char        *ls = "/bin/ls"; /* this is not user configurable :-) */\r
+const char               *tc = "/sbin/tc"; /* requires tc with HTB support */\r
+const char         *iptables = "/sbin/iptables"; /* requires iptables utility */\r
+const char        *ip6tables = "/sbin/ip6tables"; /* requires iptables utility */\r
+const char     *iptablessave = "/sbin/iptables-save"; /* not yet required */\r
+const char  *iptablesrestore = "/sbin/iptables-restore";  /* requires iptables-restore */\r
+const char    *ip6tablessave = "/sbin/ip6tables-save"; /* not yet required */\r
+const char *ip6tablesrestore = "/sbin/ip6tables-restore";  /* requires iptables-restore */\r
+const char               *ls = "/bin/ls"; /* this is not user configurable :-) */\r
 \r
 char          *config = "/etc/prometheus/prometheus.conf"; /* main configuration file */\r
 char           *hosts = "/etc/prometheus/hosts"; /* per-IP bandwidth definition file */\r
-\r
 char    *iptablesfile = "/var/spool/prometheus.iptables"; /* temporary file for iptables-restore*/\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
 char            *html = "/var/www/traffic.html"; /* hall of fame - html version */\r
@@ -107,6 +91,7 @@ int       hall_of_fame = TRUE; /* enable hall of fame */
 char              *lan = "eth0"; /* LAN interface */\r
 char       *lan_medium = "100Mbit"; /* 10Mbit/100Mbit ethernet */\r
 char              *wan = "eth1"; /* WAN/ISP interface */\r
+char        *ip6prefix = NULL; /* Prefix for global /48 IPv6 subnet */\r
 char       *wan_medium = "100Mbit"; /* 10Mbit/100Mbit ethernet */\r
 char         *qos_leaf = "sfq perturb 5"; /* leaf discipline */\r
 char    *qos_free_zone = NULL; /* QoS free zone */\r
@@ -148,6 +133,12 @@ struct IP *ips = NULL, *ip, *sharedip;
 struct Group *groups = NULL, *group;\r
 struct Keyword *keyword, *defaultkeyword=NULL, *keywords=NULL;\r
 \r
+void help(void);\r
+/* implemented in help.c */
+\r
+void get_traffic_statistics(const char *whichiptables);\r
+/* implemented in parseiptables.c */\r
+\r
 void parse_ip_log(int argc, char **argv);\r
 /* implemented in parselog.c */\r
 \r
@@ -174,6 +165,7 @@ const char *tr_odd_even(void)
 }\r
 \r
 /* ==== This is C<<1 stuff - learn C<<1 first! https://dev.arachne.cz/svn/cll1h ==== */\r
+/* (except that this code uses obsolete, archaic version of this header file...)     */\r
 \r
 struct Index\r
 {\r
@@ -185,8 +177,6 @@ struct Index
  list(Index);\r
 } *idxs=NULL, *idx, *metaindex;\r
 \r
-void TheIP(void);\r
-/* function implemented in parsehosts.c */\r
 \r
 /* ====== iptables indexes are used to reduce complexity to log8(N) ===== */\r
 \r
@@ -274,12 +264,17 @@ void get_config(char *config_filename)
 \r
   option("tc",tc);\r
   option("iptables",iptables);\r
-  option("iptables-save",iptablessave); /* new */\r
-  option("iptables-restore",iptablesrestore); /* new */\r
-  option("iptables-in-filename",iptablesfile); /* new */\r
+  option("iptables-save",iptablessave);\r
+  option("iptables-restore",iptablesrestore);\r
+  option("ip6tables",ip6tables);\r
+  option("ip6tables-save",ip6tablessave);\r
+  option("ip6tables-restore",ip6tablesrestore);\r
+  option("iptables-in-filename",iptablesfile);\r
+  option("ip6tables-in-filename",ip6tablesfile);\r
   option("hosts",hosts);\r
   option("lan-interface",lan);\r
   option("wan-interface",wan);\r
+  option("ip6-prefix",ip6prefix);\r
   option("lan-medium",lan_medium);\r
   option("wan-medium",wan_medium);\r
   lloption("wan-download",line);\r
@@ -359,159 +354,6 @@ void get_config(char *config_filename)
  }\r
 }\r
 \r
-/* ===================== traffic analyser - uses iptables  ================ */ \r
-\r
-void get_traffic_statistics(void)\r
-{\r
- char *str,*cmd;\r
- int downloadflag=0;\r
-\r
- textfile(Pipe,str) *line,*lines=NULL;\r
- string(str,STRLEN);\r
- string(cmd,STRLEN);\r
-\r
- sprintf(cmd,"%s -L -v -x -n -t mangle",iptables);\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
-\r
- for_each(line,lines)\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
-  /* debug puts(line->str); */\r
-  valid_columns(ptr,line->str,' ',col) \r
-  if(valid) switch(col)\r
-  { \r
-   case 1: if(eq(ptr,"Chain"))\r
-           {\r
-            setchainname=1;\r
-           }\r
-           else if(eq(ptr,"pkts")) \r
-           {\r
-            valid=0;\r
-           }\r
-           else\r
-           {\r
-            sscanf(ptr,"%lu",&pkts); \r
-           }\r
-           break;\r
-   case 2: if(setchainname)\r
-           {\r
-            if(!strncmp(ptr,"post_",5) || eq(ptr,"POSTROUTING"))\r
-            {\r
-             downloadflag = 1;            \r
-            }\r
-            else \r
-            {\r
-             if(!strncmp(ptr,"forw_",5) || eq(ptr,"FORWARD"))\r
-             {\r
-              downloadflag = 0;\r
-             }\r
-            }            \r
-            if(eq(ptr,"post_common") || eq(ptr,"forw_common"))\r
-            {\r
-             commonflag = 1;\r
-            }\r
-           }\r
-           else\r
-           {\r
-            sscanf(ptr,"%Lu",&traffic); \r
-            traffic += (1<<19);\r
-            traffic >>= 20;\r
-           }\r
-           break;\r
-   case 3: if((strncmp(ptr,"post_",5) && strncmp(ptr,"forw_",5)) || commonflag)\r
-           {\r
-            accept=eq(ptr,mark);\r
-           }\r
-            /*if(filter_type==1) accept=eq(ptr,"MARK"); else accept=eq(ptr,"CLASSIFY");*/\r
-           break;\r
-   case 8: if(downloadflag)\r
-           { \r
-            if(strstr(proxy_ip,ptr))\r
-            {\r
-             proxyflag=1; \r
-            }\r
-           }\r
-           else\r
-           {\r
-            ipaddr=ptr; \r
-           }\r
-           break;\r
-   case 9: if(downloadflag)ipaddr=ptr;break;\r
-  }\r
-  \r
-    if(accept && traffic>0 && ipaddr)\r
-    {\r
-     if(proxyflag)\r
-     {\r
-      printf("(proxy) ");\r
-     }\r
-     else if(!downloadflag)\r
-     {\r
-      printf("(upload) ");\r
-     }\r
-     printf("IP %s: %Lu MB (%ld pkts)\n", ipaddr, traffic, pkts);\r
-\r
-     if_exists(ip,ips,eq(ip->addr,ipaddr)); \r
-     else \r
-     {\r
-      TheIP();\r
-      ip->addr = ipaddr;\r
-      if(eq(ip->addr,"0.0.0.0/0"))\r
-      {\r
-       ip->name = "(unregistered)";\r
-       ip->min = free_min;\r
-       ip->max = ip->desired=free_max;\r
-      }\r
-      else\r
-      {\r
-       ip->name = ipaddr;\r
-      }\r
-     }\r
-     \r
-     if(downloadflag)\r
-     {\r
-      if(proxyflag)\r
-      {\r
-       ip->proxy=traffic;\r
-      }\r
-      else\r
-      {\r
-       ip->traffic+=traffic;\r
-      }\r
-      ip->direct=ip->traffic-ip->upload-ip->proxy;\r
-      ip->pktsdown=pkts;\r
-     }\r
-     else\r
-     {\r
-      ip->upload=traffic;\r
-      ip->pktsup=pkts;\r
-      if(include_upload)\r
-      {\r
-       ip->traffic+=traffic;\r
-      }\r
-      else \r
-      {\r
-       if(traffic>ip->traffic)\r
-       {\r
-        ip->traffic=traffic;     \r
-       }\r
-      }\r
-     }\r
-    }  \r
-  }\r
-  free(cmd);\r
-}\r
  \r
 /* ========== This function executes, logs OR ALSO prints command ========== */\r
 \r
@@ -662,7 +504,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
   /*-----------------------------------------------------------------*/\r
   puts("Parsing iptables verbose output ...");\r
   /*-----------------------------------------------------------------*/\r
-  get_traffic_statistics();\r
+  get_traffic_statistics(iptables);\r
  }\r
 \r
  /*-----------------------------------------------------------------*/\r
@@ -720,17 +562,17 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
   puts("Initializing iptables and tc classes ...");\r
   /*-----------------------------------------------------------------*/\r
   \r
-  iptables_file=fopen(iptablesfile,"w");\r
+  iptables_file = fopen(iptablesfile, "w");\r
   if(iptables_file == NULL)\r
   {\r
-    puts("Cannot open iptablesfile!");\r
+    perror(iptablesfile);\r
     exit(-1);\r
   }\r
   \r
-  log_file=fopen(cmdlog,"w");\r
+  log_file = fopen(cmdlog, "w");\r
   if(log_file == NULL) \r
   {\r
-    puts("Cannot open logfile!");\r
+    perror(cmdlog);\r
     exit(-1);\r
   }\r
   \r
@@ -774,7 +616,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
    save_line(str);\r
   }\r
   \r
-  if(ip_count>idxtable_treshold1 && !just_flush)\r
+  if(ip_count > idxtable_treshold1 && !just_flush)\r
   {\r
    int idxcount=0, bitmask=32-idxtable_bitmask1; /* default net mask: 255.255.255.240 */\r
    char *subnet, *buf;\r
@@ -785,7 +627,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
    save_line(":post_common - [0:0]");\r
    save_line(":forw_common - [0:0]");\r
 \r
-   for_each(ip,ips) if(ip->addr && *(ip->addr) && !eq(ip->addr,"0.0.0.0/0"))\r
+   for_each(ip,ips) if(ip->addr && *(ip->addr) && !eq(ip->addr,"0.0.0.0/0") && !strchr(ip->addr,':')) /* only IPv4 */\r
    {\r
     buf=index_id(ip->addr,bitmask);\r
     if_exists(idx,idxs,eq(idx->id,buf))\r
@@ -806,14 +648,14 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
    }\r
 \r
    /* brutal perfomance optimalization */\r
-   while(idxcount>idxtable_treshold2 && bitmask>2*idxtable_bitmask2)\r
+   while(idxcount > idxtable_treshold2 && bitmask > 2*idxtable_bitmask2)\r
    {\r
-    bitmask-=idxtable_bitmask2;\r
-    idxcount=0;\r
+    bitmask -= idxtable_bitmask2;\r
+    idxcount = 0;\r
 \r
     for_each(idx,idxs) if(idx->parent == NULL)\r
     {\r
-     buf=index_id(idx->addr,bitmask);\r
+     buf = index_id(idx->addr,bitmask);\r
      if_exists(metaindex,idxs,eq(metaindex->id,buf))\r
      {\r
       metaindex->children++;\r
@@ -833,7 +675,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
     }\r
    }\r
 \r
-   /* this should slightly optimize throughout ... */\r
+   /* this should slightly optimize throughput ... */\r
    sort(idx,idxs,desc_order_by,children);\r
    sort(idx,idxs,order_by,bitmask);\r
 \r
@@ -1122,7 +964,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
  puts("Generating iptables and tc classes ... ");\r
  /*-----------------------------------------------------------------*/\r
 \r
- for_each(ip, ips) if(ip->mark > 0)\r
+ for_each(ip, ips) if(ip->mark > 0 && !strchr(ip->addr,':')) /* works only for IPv4 so far */\r
  {\r
   if(idxs)\r
   {\r
This page took 0.275799 seconds and 4 git commands to generate.