getting ready for IP6tables support
[svn/Prometheus-QoS/.git] / prometheus.c
index 780b15858c8dadec585d28a63b31ffe8c0350205..b4de143676ff19ec7a58c47c800ec895de2951ef 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-2012 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, 20121011\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, 20130114\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
 \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
-const char *stats_html_signature = "<span class=\"small\">Statistics generated by Prometheus QoS version %s<br />GPL+Copyright(C)2005-2012 Michael Polak, <a target=\"_blank\" href=\"http://www.arachne.cz/\">Arachne Labs</a></span>\n";\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
-/* ======= Help screen is hopefuly self-documenting part of code :-) ======= */\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
-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          *credit = "/var/lib/misc/prometheus.credit"; /* credit log file */\r
 char        *classmap = "/var/lib/misc/prometheus.classes"; /* credit log file */\r
@@ -148,6 +131,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 +163,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 +175,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
@@ -232,7 +220,10 @@ void get_config(char *config_filename)
    keyword->leaf_discipline="";\r
 \r
    push(keyword,keywords);\r
-   if(!defaultkeyword) defaultkeyword=keyword;\r
+   if(!defaultkeyword)\r
+   {\r
+    defaultkeyword=keyword;\r
+   }\r
    keywordcount++;\r
    \r
    kwd=NULL;\r
@@ -273,6 +264,9 @@ void get_config(char *config_filename)
   option("iptables",iptables);\r
   option("iptables-save",iptablessave); /* new */\r
   option("iptables-restore",iptablesrestore); /* new */\r
+  option("ip6tables",ip6tables);\r
+  option("ip6tables-save",ip6tablessave); /* new */\r
+  option("ip6tables-restore",ip6tablesrestore); /* new */\r
   option("iptables-in-filename",iptablesfile); /* new */\r
   option("hosts",hosts);\r
   option("lan-interface",lan);\r
@@ -356,155 +350,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
-     }\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
@@ -655,7 +500,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
This page took 0.166603 seconds and 4 git commands to generate.