getting ready for IP6tables support
authorxchaos <xchaos@251d49ef-1d17-4917-a970-b30cf55b089b>
Tue, 15 Jan 2013 00:16:56 +0000 (00:16 +0000)
committerxchaos <xchaos@251d49ef-1d17-4917-a970-b30cf55b089b>
Tue, 15 Jan 2013 00:16:56 +0000 (00:16 +0000)
git-svn-id: https://dev.arachne.cz/repos/prometheus/trunk@209 251d49ef-1d17-4917-a970-b30cf55b089b

Makefile
help.c
ipstruct.h
parsehosts.c
parseiptables.c [new file with mode: 0644]
prometheus.c

index 2f89465c0ba0224815d209cdcbe486cfbc07d18a..656439783269e742167ef0957d6f426daa55099d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ prefix=/usr
 mandir=$(prefix)/share/man
 sbindir=$(prefix)/sbin
 sysconfdir=/etc
 mandir=$(prefix)/share/man
 sbindir=$(prefix)/sbin
 sysconfdir=/etc
-OBJECTS=parsehosts.o parselogs.o ipv4subnets.o json.o htmlandlogs.o help.o prometheus.o
+OBJECTS=parsehosts.o parseiptables.o parselogs.o ipv4subnets.o json.o htmlandlogs.o help.o prometheus.o
 HEADERS=cll1-0.6.2.h ipstruct.h
 
 main: prometheus
 HEADERS=cll1-0.6.2.h ipstruct.h
 
 main: prometheus
diff --git a/help.c b/help.c
index 23c71b4b166c0fdfbb2a82dd1536f28be2376059..023673adcaf6d5d65ffd365ff149f6d242453011 100644 (file)
--- a/help.c
+++ b/help.c
@@ -2,6 +2,8 @@
 
 #include "cll1-0.6.2.h"
 
 
 #include "cll1-0.6.2.h"
 
+/* ======= Help screen is hopefuly self-documenting part of code :-) ======= */
+
 void help(void)
 {
  puts("Command line switches:\n\
 void help(void)
 {
  puts("Command line switches:\n\
index dcf5bfa927e83f03892a0a227685a89cdf54bd0f..dab37fcbe97f486109ee942559af9b44c62a444c 100644 (file)
@@ -53,3 +53,6 @@ struct Keyword
  \r
  list(Keyword);\r
 };\r
  \r
  list(Keyword);\r
 };\r
+\r
+void TheIP(char *ipaddr);\r
+/* function implemented in parsehosts.c */\r
index db3bf2eb52f2003c647518487023cceb98d9d118..d17c61917c32ad0235da1f3b036b768e3b56e27b 100644 (file)
@@ -17,11 +17,11 @@ extern int free_min;
 extern const int highest_priority;\r
 \r
 /* This must be object oriented! This looks almost like constructor ;-) */\r
 extern const int highest_priority;\r
 \r
 /* This must be object oriented! This looks almost like constructor ;-) */\r
-void TheIP(void)\r
+void TheIP(char *ipaddr)\r
 {\r
  create(ip,IP);\r
  ip->name        = "";\r
 {\r
  create(ip,IP);\r
  ip->name        = "";\r
- ip->addr        = "";\r
+ ip->addr        = ipaddr;\r
  ip->sharing     = NULL;\r
  ip->prio        = highest_priority+1;\r
  ip->lmsid       = -1;\r
  ip->sharing     = NULL;\r
  ip->prio        = highest_priority+1;\r
  ip->lmsid       = -1;\r
@@ -80,9 +80,8 @@ void parse_ip(char *str)
  if_exists(ip, ips, eq(ip->addr,ipaddr));\r
  else\r
  {\r
  if_exists(ip, ips, eq(ip->addr,ipaddr));\r
  else\r
  {\r
-  TheIP();\r
+  TheIP(ipaddr);\r
  }\r
  }\r
- ip->addr = ipaddr;\r
  ip->name = ipname;\r
  if(lmsid)\r
  {\r
  ip->name = ipname;\r
  if(lmsid)\r
  {\r
diff --git a/parseiptables.c b/parseiptables.c
new file mode 100644 (file)
index 0000000..e736324
--- /dev/null
@@ -0,0 +1,166 @@
+/* Modified by: xChaos, 20130115 */\r
+\r
+#include "cll1-0.6.2.h"\r
+#include "ipstruct.h"\r
+\r
+#define STRLEN 512\r
+\r
+/* globals declared in prometheus.c */\r
+extern struct IP *ips, *ip, *sharedip;\r
+extern char *mark;\r
+extern char *proxy_ip;\r
+extern int free_min;\r
+extern int free_max;\r
+extern int include_upload;\r
+\r
+/* ===================== traffic analyser - uses iptables  ================ */ \r
+\r
+void get_traffic_statistics(const char *whichiptables)\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", 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
+\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
+  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(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
index 9743b30b271033548202234fab31a4900087a8f1..b4de143676ff19ec7a58c47c800ec895de2951ef 100644 (file)
@@ -45,20 +45,19 @@ const char *stats_html_signature = "<span class=\"small\">Statistics generated b
 #define STRLEN 512\r
 #undef DEBUG\r
 \r
 #define STRLEN 512\r
 #undef DEBUG\r
 \r
-/* ======= Help screen is hopefuly self-documenting part of code :-) ======= */\r
-\r
-\r
 /* ======= All path names are defined here (for RPM patch) =======  */\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          *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
 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
@@ -135,6 +134,9 @@ struct Keyword *keyword, *defaultkeyword=NULL, *keywords=NULL;
 void help(void);\r
 /* implemented in help.c */
 \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
 void parse_ip_log(int argc, char **argv);\r
 /* implemented in parselog.c */\r
 \r
@@ -173,8 +175,6 @@ struct Index
  list(Index);\r
 } *idxs=NULL, *idx, *metaindex;\r
 \r
  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
 \r
 /* ====== iptables indexes are used to reduce complexity to log8(N) ===== */\r
 \r
@@ -264,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("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
   option("iptables-in-filename",iptablesfile); /* new */\r
   option("hosts",hosts);\r
   option("lan-interface",lan);\r
@@ -347,159 +350,6 @@ void get_config(char *config_filename)
  }\r
 }\r
 \r
  }\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
  \r
 /* ========== This function executes, logs OR ALSO prints command ========== */\r
 \r
@@ -650,7 +500,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
   /*-----------------------------------------------------------------*/\r
   puts("Parsing iptables verbose output ...");\r
   /*-----------------------------------------------------------------*/\r
   /*-----------------------------------------------------------------*/\r
   puts("Parsing iptables verbose output ...");\r
   /*-----------------------------------------------------------------*/\r
-  get_traffic_statistics();\r
+  get_traffic_statistics(iptables);\r
  }\r
 \r
  /*-----------------------------------------------------------------*/\r
  }\r
 \r
  /*-----------------------------------------------------------------*/\r
This page took 0.294616 seconds and 4 git commands to generate.