X-Git-Url: http://git.harvie.cz/?p=svn%2FPrometheus-QoS%2F.git;a=blobdiff_plain;f=prometheus.c;fp=prometheus.c;h=5a9492e3e7592cc4cf8fac51f5a98e674039016e;hp=1b510293460791b215ac569c658910a9fc85840e;hb=dba7666bdacf8846560e5414b6c9535434e98f8b;hpb=3430e838dc632523c571a995d75ace44de1b06b6
diff --git a/prometheus.c b/prometheus.c
index 1b51029..5a9492e 100644
--- a/prometheus.c
+++ b/prometheus.c
@@ -7,7 +7,7 @@
/* Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
-/* Modified by: xChaos, 20190127
+/* Modified by: xChaos, 20190212
ludva, 20080415
Prometheus QoS is free software; you can redistribute it and/or
@@ -40,7 +40,7 @@ const char *version = "0.9.1";
/* Warning: unofficial Github mirror is not supported by author! */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
-const char *stats_html_signature = "Statistics generated by Prometheus QoS version %s
GPL+Copyright(C)2005-2017 Michael Polak, Arachne Labs\n";
+const char *stats_html_signature = "Statistics generated by Prometheus QoS version %s
GPL+Copyright(C)2005-2019 Michael Polak, Arachne Labs\n";
#define STRLEN 512
#undef DEBUG
@@ -88,6 +88,7 @@ char *mark = "MARK";
char *mark_iptables = "MARK --set-mark ";
int dry_run = FALSE; /* preview - use puts() instead of system() */
int mix_new_hosts = FALSE; /* execute only commands not already in log of iptables */
+char *log_file_mode = "w"; /* overwrite command and iptables log files, except append in -x mode */
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]";
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";
FILE *iptables_file = NULL;
@@ -141,7 +142,7 @@ struct Macro *macro, *macros = NULL;
struct Index *idxs = NULL, *idx, *metaindex;
struct Interface *interfaces = NULL, *interface;
struct QosFreeInterface *qosfreeinterfaces = NULL, *qosfreeinterface;
-struct Textfile *previous_commands = NULL, *previous_iptables = NULL, *previous_ip6tables = NULL, *textline;
+struct Textfile *previous_commands = NULL, *previous_iptables = NULL, *previous_ip6tables = NULL, *previous_classmap = NULL, *textline;
#define FREE_CLASS 3
#define OVERLIMIT_CLASS 4
@@ -381,12 +382,17 @@ void safe_run(char *cmd)
{
int skip_cmd = FALSE;
if(mix_new_hosts)
+ {
+ skip_cmd = FALSE;
for_each(textline, previous_commands)
+ {
if(eq(textline->str, cmd))
{
skip_cmd = TRUE;
break;
}
+ }
+ }
if(!skip_cmd)
{
@@ -394,38 +400,52 @@ void safe_run(char *cmd)
{
printf("Executing command: %s\n", cmd);
}
- system(cmd);
+ else
+ system(cmd);
}
}
- if(log_file)
+ if(log_file && !mix_new_hosts)//!!debug
{
fprintf(log_file,"%s\n",cmd);
}
}
void iptables_save_line(char *line, int ipv6)
-{
+{
+ const char *cmd = NULL;
+
if(ipv6)
{
if(mix_new_hosts)
for_each(textline, previous_ip6tables)
if(eq(textline->str, line))
+ {
+ printf("Match: %s\n",line);
return;
+ }
- fprintf(ip6tables_file,"%s\n",line);
+ if(!mix_new_hosts) //!!debug
+ fprintf(ip6tables_file,"%s\n",line);
+ cmd = ip6tables;
}
else
{
if(mix_new_hosts)
for_each(textline, previous_iptables)
if(eq(textline->str, line))
- return;
+ {
+ printf("Match: %s\n",line);
+ return;
+ }
- fprintf(iptables_file,"%s\n",line);
+ if(!mix_new_hosts) //!!debug
+ fprintf(iptables_file,"%s\n",line);
+ cmd = iptables;
}
if(mix_new_hosts)
{
- printf("Adding iptables/ip6tables rule: %s\n", line);
+
+ printf("Executing command: %s %s\n",cmd, line);
}
}
@@ -526,7 +546,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
argument("-2") { run = TRUE; just_preview = TRUE; reduce_ceil = 2; }
argument("-4") { run = TRUE; just_preview = TRUE; reduce_ceil = 4; }
argument("-s") { run = TRUE; just_preview = TRUE; start_shaping = TRUE; }
- argument("-x") { run = TRUE; just_preview = TRUE; mix_new_hosts = TRUE; }
+ argument("-x") { run = TRUE; mix_new_hosts = TRUE; log_file_mode = "a"; }
argument("-r") { run = TRUE; }
argument("-n") { run = TRUE; nodelay = TRUE; }
argument("-a") { run = TRUE; just_networks = TRUE; }
@@ -677,7 +697,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
if(mix_new_hosts)
{
- char *new_filename;
+ char *eoln;
/*-----------------------------------------------------------------*/
printf("Loading log of previously executed commands %s ...\n", cmdlog);
/*-----------------------------------------------------------------*/
@@ -690,16 +710,10 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
printf("Loading log of ip6tables-restore commands %s ...\n", ip6tablesfile);
/*-----------------------------------------------------------------*/
load(textline, previous_ip6tables, ip6tablesfile, Textfile, str);
-
- string(new_filename, strlen(iptablesfile)+2);
- strcpy(new_filename, iptablesfile);
- strcat(new_filename, "-x");
- iptablesfile = new_filename;
-
- string(new_filename, strlen(ip6tablesfile)+2);
- strcpy(new_filename, ip6tablesfile);
- strcat(new_filename, "-x");
- ip6tablesfile = new_filename;
+ /*-----------------------------------------------------------------*/
+ printf("Loading map of IP addresses to tc classes %s ...\n", classmap);
+ /*-----------------------------------------------------------------*/
+ load(textline, previous_classmap, classmap, Textfile, str);
}
/*-----------------------------------------------------------------*/
@@ -762,35 +776,38 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
}
- if(!mix_new_hosts && !just_preview)
+ if(!just_preview)
{
- /*-----------------------------------------------------------------*/
- puts("Initializing iptables and tc classes ...");
- /*-----------------------------------------------------------------*/
-
- iptables_file = fopen(iptablesfile, "w");
- if(iptables_file == NULL)
+ if(!mix_new_hosts)
{
+ /*-----------------------------------------------------------------*/
+ puts("Initializing iptables and tc classes ...");
+ /*-----------------------------------------------------------------*/
+
+ iptables_file = fopen(iptablesfile, "w");
+ if(iptables_file == NULL)
+ {
perror(iptablesfile);
exit(-1);
- }
- iptables_save_line(iptablespreamble, IPv4);
+ }
+ iptables_save_line(iptablespreamble, IPv4);
- if(ip6prefix)
- {
- ip6tables_file = fopen(ip6tablesfile, "w");
- if(ip6tables_file == NULL)
+ if(ip6prefix)
{
- perror(ip6tablesfile);
- exit(-1);
+ ip6tables_file = fopen(ip6tablesfile, "w");
+ if(ip6tables_file == NULL)
+ {
+ perror(ip6tablesfile);
+ exit(-1);
+ }
+ iptables_save_line(iptablespreamble, IPv6);
+ iptables_save_line(ip6preamble, IPv6);
}
- iptables_save_line(iptablespreamble, IPv6);
- iptables_save_line(ip6preamble, IPv6);
- }
- run_iptables_restore();
+ run_iptables_restore();
+ }
- log_file = fopen(cmdlog, "w");
+ log_file = fopen(cmdlog, log_file_mode); /* overwrite command and iptables log files, except append in -x mode */
if(log_file == NULL)
{
perror(cmdlog);
@@ -802,14 +819,17 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
sprintf(str,"%s qdisc del dev %s root 2>/dev/null", tc, interface->name);
safe_run(str);
}
-
- iptables_file=fopen(iptablesfile,"w");
- iptables_save_line(iptablespreamble, IPv4);
- if(ip6prefix)
- {
- ip6tables_file=fopen(ip6tablesfile,"w");
- iptables_save_line(iptablespreamble, IPv6);
- iptables_save_line(ip6preamble, IPv6);
+
+ if(!mix_new_hosts)
+ {
+ iptables_file = fopen(iptablesfile, log_file_mode); /* overwrite command and iptables log files, except append in -x mode */
+ iptables_save_line(iptablespreamble, IPv4);
+ if(ip6prefix)
+ {
+ ip6tables_file = fopen(ip6tablesfile, log_file_mode); /* overwrite command and iptables log files, except append in -x mode */
+ iptables_save_line(iptablespreamble, IPv6);
+ iptables_save_line(ip6preamble, IPv6);
+ }
}
if(qos_free_zone && *qos_free_zone!='0') /* this is currently supported only for IPv4 */
@@ -1400,6 +1420,7 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
}
i++;
}
+ /* classmap */
if(ip->min && f > 0)
{
fprintf(f, "%s %d\n", ip->addr, ip->mark);
@@ -1467,7 +1488,10 @@ Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
safe_run(str);
}
printf("Total IP count: %d\n", i);
- run_iptables_restore();
+ if(!mix_new_hosts)
+ {
+ run_iptables_restore();
+ }
if(log_file)
{
fclose(log_file);