-x: mix_new_hosts
authorxchaos <xchaos@251d49ef-1d17-4917-a970-b30cf55b089b>
Tue, 29 Jan 2019 21:30:38 +0000 (21:30 +0000)
committerxchaos <xchaos@251d49ef-1d17-4917-a970-b30cf55b089b>
Tue, 29 Jan 2019 21:30:38 +0000 (21:30 +0000)
git-svn-id: https://dev.arachne.cz/repos/prometheus/trunk@269 251d49ef-1d17-4917-a970-b30cf55b089b

ipstruct.h
prometheus.c
utils.c

index 80810fcaffcc0ec999070298780b66858286c32e..f8d09e5501fabc6521e20510e698ab9e96fcdee7 100644 (file)
@@ -79,6 +79,13 @@ struct QosFreeInterface
  list(QosFreeInterface);\r
 };\r
 \r
+struct Textfile\r
+{\r
+ char *str;\r
+ int _eoln;\r
+ list(Textfile);\r
+};\r
+\r
 struct Index\r
 {\r
  char *addr;\r
index 236e4ab58a74411c0a43ba7d645b43634e67f138..1b510293460791b215ac569c658910a9fc85840e 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-2017 Michael Polak, Arachne Aerospace     */\r
+/* Copyright(C) 2005-2019 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, 20171106\r
+/* Modified by: xChaos, 20190127\r
                  ludva, 20080415\r
  \r
    Prometheus QoS is free software; you can redistribute it and/or\r
@@ -87,6 +87,7 @@ char      *final_chain = "DROP"; /* REJECT would be better, but it is impossible
 char             *mark = "MARK";\r
 char    *mark_iptables = "MARK --set-mark ";\r
 int            dry_run = FALSE; /* preview - use puts() instead of system() */\r
+int      mix_new_hosts = FALSE; /* execute only commands not already in log of iptables */\r
 char *iptablespreamble = "*mangle\n:PREROUTING ACCEPT [0:0]\n:POSTROUTING ACCEPT [0:0]\n:INPUT ACCEPT [0:0]\n:OUTPUT ACCEPT [0:0]\n:FORWARD ACCEPT [0:0]";\r
 char      *ip6preamble = "-A FORWARD -p ipv6-icmp -j ACCEPT\n-A POSTROUTING -p ipv6-icmp -j ACCEPT\n-A FORWARD -s fe80::/10 -j ACCEPT\n-A FORWARD -d ff00::/8 -j ACCEPT\n-A POSTROUTING -s fe80::/10 -j ACCEPT\n-A POSTROUTING -d ff00::/8 -j ACCEPT";\r
 FILE    *iptables_file = NULL;\r
@@ -126,12 +127,12 @@ int           ip_count = 0;
 FILE         *log_file = NULL;\r
 char              *kwd = "via-prometheus"; /* /etc/hosts comment, eg. #qos-64-128 */\r
 \r
-const int highest_priority   = 0; /* highest HTB priority (HTB built-in value is 0) */\r
-const int lowest_priority    = 7; /* lowest HTB priority /include/uapi/linux/pkt_sched.h: #define TC_HTB_NUMPRIO 8 */\r
-const int idxtable_treshold1 = 24;      /* this is no longer configurable */\r
-const int idxtable_treshold2 = 12;      /* this is no longer configurable */\r
-const int idxtable_bitmask1  = 3;        /* this is no longer configurable */\r
-const int idxtable_bitmask2  = 3;        /* this is no longer configurable */\r
+const int highest_priority   = 0;  /* highest HTB priority (HTB built-in value is 0) */\r
+const int lowest_priority    = 7;  /* lowest HTB priority /include/uapi/linux/pkt_sched.h: #define TC_HTB_NUMPRIO 8 */\r
+const int idxtable_treshold1 = 24; /* this is no longer configurable */\r
+const int idxtable_treshold2 = 12; /* this is no longer configurable */\r
+const int idxtable_bitmask1  = 3;  /* this is no longer configurable */\r
+const int idxtable_bitmask2  = 3;  /* this is no longer configurable */\r
 \r
 struct IP *ips = NULL, *networks = NULL, *ip, *sharedip;\r
 struct Group *groups = NULL, *group;\r
@@ -140,6 +141,7 @@ struct Macro *macro, *macros = NULL;
 struct Index *idxs = NULL, *idx, *metaindex;\r
 struct Interface *interfaces = NULL, *interface;\r
 struct QosFreeInterface *qosfreeinterfaces = NULL, *qosfreeinterface;\r
+struct Textfile *previous_commands = NULL, *previous_iptables = NULL, *previous_ip6tables = NULL, *textline;\r
 \r
 #define FREE_CLASS      3\r
 #define OVERLIMIT_CLASS 4\r
@@ -184,7 +186,6 @@ const char *tr_odd_even(void)
  }\r
 }\r
 \r
-\r
 /* ====== iptables indexes are used to reduce complexity to log8(N) ===== */\r
 \r
 char *index_id(char *ip, int bitmask);\r
@@ -378,7 +379,23 @@ void safe_run(char *cmd)
  }\r
  else\r
  {\r
-  system(cmd);\r
+  int skip_cmd = FALSE;\r
+  if(mix_new_hosts)\r
+   for_each(textline, previous_commands)\r
+    if(eq(textline->str, cmd))\r
+    {\r
+     skip_cmd = TRUE;\r
+     break;\r
+    }\r
\r
+  if(!skip_cmd)\r
+  {\r
+   if(mix_new_hosts)\r
+   {\r
+    printf("Executing command: %s\n", cmd);\r
+   }\r
+   system(cmd);\r
+  }\r
  }\r
  if(log_file)\r
  {\r
@@ -387,15 +404,29 @@ void safe_run(char *cmd)
 }\r
 \r
 void iptables_save_line(char *line, int ipv6)\r
-{\r
+{   \r
  if(ipv6)\r
  {\r
+  if(mix_new_hosts)\r
+   for_each(textline, previous_ip6tables)\r
+    if(eq(textline->str, line))\r
+     return;\r
+\r
   fprintf(ip6tables_file,"%s\n",line);\r
  }\r
  else\r
  {\r
+  if(mix_new_hosts)\r
+   for_each(textline, previous_iptables)\r
+    if(eq(textline->str, line))\r
+     return; \r
+\r
   fprintf(iptables_file,"%s\n",line);\r
  }\r
+ if(mix_new_hosts)\r
+ {\r
+  printf("Adding iptables/ip6tables rule: %s\n", line);\r
+ } \r
 }\r
 \r
 #define IPv4 FALSE\r
@@ -469,7 +500,7 @@ program
  int just_preview  = FALSE;       /* preview - generate just stats */\r
  int start_shaping = FALSE;       /* apply FUP - requires classmap file */\r
  int stop_shaping  = FALSE;       /* lift FUP - requires classmap file */\r
- int reduce_ceil     = 0;           /* allow only rate+(ceil-rate)/2, /4, etc. */\r
+ int reduce_ceil   = 0;           /* allow only rate+(ceil-rate)/2, /4, etc. */\r
  int just_logs     = FALSE;       /* just parse logs */\r
  int run           = FALSE;\r
  int total         = 0;\r
@@ -478,7 +509,7 @@ program
   \r
  printf("\n\\r
 Prometheus QoS - \"fair-per-IP\" Quality of Service setup utility.\n\\r
-Version %s - Copyright (C)2005-2017 Michael Polak, Arachne Labs\n\\r
+Version %s - Copyright (C)2005-2019 Michael Polak, Arachne Labs\n\\r
 iptables-restore & burst tunning & classify modification by Ludva\n\\r
 Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);\r
 \r
@@ -487,20 +518,21 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
  {\r
   argument("-c") { nextargument(config); }\r
   argument("-h") { nextargument(althosts);}\r
-  argument("-d") { run=TRUE; dry_run=TRUE; }\r
-  argument("-f") { run=TRUE; just_flush=TRUE; }\r
-  argument("-9") { run=TRUE; just_flush=9; }\r
-  argument("-p") { run=TRUE; just_preview=TRUE; }\r
-  argument("-q") { run=TRUE; just_preview=TRUE; stop_shaping=TRUE; }\r
-  argument("-2") { run=TRUE; just_preview=TRUE; reduce_ceil=2; }\r
-  argument("-4") { run=TRUE; just_preview=TRUE; reduce_ceil=4; }\r
-  argument("-s") { run=TRUE; just_preview=TRUE; start_shaping=TRUE; }\r
-  argument("-r") { run=TRUE; }\r
-  argument("-n") { run=TRUE; nodelay=TRUE; }\r
-  argument("-a") { run=TRUE; just_networks=TRUE; }\r
-  argument("-l") { just_logs=TRUE; }\r
-  argument("-m") { just_logs=TRUE; }\r
-  argument("-y") { just_logs=TRUE; }\r
+  argument("-d") { run = TRUE; dry_run = TRUE; }\r
+  argument("-f") { run = TRUE; just_flush = TRUE; }\r
+  argument("-9") { run = TRUE; just_flush = 9; }\r
+  argument("-p") { run = TRUE; just_preview = TRUE; }\r
+  argument("-q") { run = TRUE; just_preview = TRUE; stop_shaping = TRUE; }\r
+  argument("-2") { run = TRUE; just_preview = TRUE; reduce_ceil = 2; }\r
+  argument("-4") { run = TRUE; just_preview = TRUE; reduce_ceil = 4; }\r
+  argument("-s") { run = TRUE; just_preview = TRUE; start_shaping = TRUE; }\r
+  argument("-x") { run = TRUE; just_preview = TRUE; mix_new_hosts = TRUE; }\r
+  argument("-r") { run = TRUE; }\r
+  argument("-n") { run = TRUE; nodelay = TRUE; }\r
+  argument("-a") { run = TRUE; just_networks = TRUE; }\r
+  argument("-l") { just_logs = TRUE; }\r
+  argument("-m") { just_logs = TRUE; }\r
+  argument("-y") { just_logs = TRUE; }\r
   argument("-?") { help(); exit(0); }\r
   argument("--help") { help(); exit(0); }\r
   argument("-v") { exit(0); } \r
@@ -586,7 +618,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
   hosts = althosts;\r
  }\r
 \r
- if(just_flush<9)\r
+ if(!mix_new_hosts && just_flush<9)\r
  {\r
   /*-----------------------------------------------------------------*/\r
   puts("Parsing iptables verbose output ...");\r
@@ -630,8 +662,6 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
  }\r
  done; /* ugly macro end */\r
 \r
-\r
-\r
  /*-----------------------------------------------------------------*/\r
  printf("Parsing class defintion file %s ...\n", hosts);\r
  /*-----------------------------------------------------------------*/\r
@@ -645,6 +675,33 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
  }\r
 */\r
 \r
+ if(mix_new_hosts)\r
+ {\r
+  char *new_filename;\r
+  /*-----------------------------------------------------------------*/\r
+  printf("Loading log of previously executed commands %s ...\n", cmdlog);\r
+  /*-----------------------------------------------------------------*/\r
+  load(textline, previous_commands, cmdlog, Textfile, str);\r
+  /*-----------------------------------------------------------------*/\r
+  printf("Loading log of iptables-restore commands %s ...\n", iptablesfile);\r
+  /*-----------------------------------------------------------------*/\r
+  load(textline, previous_iptables, iptablesfile, Textfile, str);\r
+  /*-----------------------------------------------------------------*/\r
+  printf("Loading log of ip6tables-restore commands %s ...\n", ip6tablesfile);\r
+  /*-----------------------------------------------------------------*/\r
+  load(textline, previous_ip6tables, ip6tablesfile, Textfile, str);\r
+\r
+  string(new_filename, strlen(iptablesfile)+2);\r
+  strcpy(new_filename, iptablesfile);\r
+  strcat(new_filename, "-x");\r
+  iptablesfile = new_filename;\r
+\r
+  string(new_filename, strlen(ip6tablesfile)+2);\r
+  strcpy(new_filename, ip6tablesfile);\r
+  strcat(new_filename, "-x");\r
+  ip6tablesfile = new_filename;\r
+ }\r
+\r
  /*-----------------------------------------------------------------*/\r
  puts("Resolving shared connections ...");\r
  /*-----------------------------------------------------------------*/\r
@@ -685,7 +742,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
   }\r
  }\r
 \r
- if(enable_credit && just_flush<9)\r
+ if(!mix_new_hosts && enable_credit && just_flush<9)\r
  {\r
   /*-----------------------------------------------------------------*/\r
   printf("Parsing credit file %s ...\n", credit);\r
@@ -705,7 +762,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
  }\r
 \r
 \r
- if(!just_preview)\r
+ if(!mix_new_hosts && !just_preview)\r
  {\r
   /*-----------------------------------------------------------------*/\r
   puts("Initializing iptables and tc classes ...");\r
@@ -925,7 +982,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
   exit(0);\r
  }\r
 \r
- if(!just_preview)\r
+ if(!mix_new_hosts && !just_preview)\r
  {\r
   if(!dry_run && !nodelay && qos_free_delay)\r
   {\r
@@ -949,10 +1006,13 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
   }\r
  }\r
 \r
- /*-----------------------------------------------------------------*/\r
- puts("Locating heavy downloaders and generating root classes ...");\r
- /*-----------------------------------------------------------------*/\r
- sort(ip,ips,desc_order_by,traffic); \r
+ if(mix_new_hosts)\r
+ {\r
+  /*-----------------------------------------------------------------*/\r
+  puts("Locating heavy downloaders and generating root classes ...");\r
+  /*-----------------------------------------------------------------*/\r
+  sort(ip,ips,desc_order_by,traffic);\r
+ }\r
 \r
  /*-----------------------------------------------------------------*/\r
  for_each(interface, interfaces)\r
@@ -1187,7 +1247,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
   json_traffic=json_preview;\r
  }\r
 \r
- if(!dry_run && !just_flush)\r
+ if(!mix_new_hosts && !dry_run && !just_flush)\r
  {\r
   /*-----------------------------------------------------------------*/\r
   printf("Writing json traffic overview  %s ... ", json_traffic);\r
@@ -1218,8 +1278,8 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
 \r
   printf("Statistics preview generated (-%c switch) - now exiting ...\n", swchar);\r
   exit(0);\r
- }  \r
-\r
+ }\r
\r
  i=0;\r
 #ifdef DEBUG\r
  printf("%-22s %-15s mark\n","name","ip");\r
diff --git a/utils.c b/utils.c
index 09ae9f9ff5e405f966e1e957d2ffb75b166851d6..15d5604631d28f75156af528403765d2467f830d 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -1,5 +1,6 @@
 #include <string.h>
 #include <stdlib.h>
+#include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
This page took 0.205202 seconds and 4 git commands to generate.