1 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2 /* Prometheus QoS - you can "steal fire" from your ISP */
3 /* "fair-per-IP" quality of service (QoS) utility */
4 /* requires Linux 2.4.x or 2.6.x with HTB support */
5 /* Copyright(C) 2005-2008 Michael Polak (xChaos) */
6 /* iptables-restore support Copyright(C) 2007-2008 ludva */
7 /* Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf */
8 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
10 /* Modified: xChaos, 20080422
13 Prometheus QoS is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2.1 of
16 the License, or (at your option) any later version.
18 Prometheus QoS is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with Prometheus Qos; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 GNU General Public License is located in file COPYING */
30 #define FIRSTGROUPID 1024
31 #define FIRSTIPCLASS 2048
36 const char *version
= "0.7.9-c";
38 /* Version numbers: 0.7.9 will be last development ("beta"), 0.8.0 first stable */
39 /* Debian(RPM) package versions/patchlevels: 0.7.9-2, 0.8.0-1, 0.8.0-2, etc. */
40 /* C source code development versions ("beta"): 0.7.9-a, 0.8.1-b, etc. */
41 /* C source code release versions: 0.8.0, 0.8.2, 0.8.4, etc. */
43 const char *stats_html_signature
= "<small>Statistics generated by Prometheus QoS version %s<br>GPL+Copyright(C)2005-2008 Michael Polak, <a href=\"http://www.arachne.cz/\">Arachne Labs</a></small>\n";
45 /* ======= All path names are defined here (for RPM patch) ======= */
47 char *tc
= "/sbin/tc"; /* requires tc with HTB support */
48 char *iptables
= "/sbin/iptables"; /* requires iptables utility */
49 char *iptablessave
= "/sbin/iptables-save"; /* not yet required */
50 char *iptablesrestore
= "/sbin/iptables-restore"; /* requires iptables-restore */
51 char *ls
= "/bin/ls"; /* this is not user configurable :-) */
53 char *config
= "/etc/prometheus/prometheus.conf"; /* main configuration file */
54 char *hosts
= "/etc/prometheus/hosts"; /* per-IP bandwidth definition file */
56 char *iptablesfile
= "/var/spool/prometheus.iptables"; /* temporary file for iptables-restore*/
57 char *credit
= "/var/lib/misc/prometheus.credit"; /* credit log file */
58 char *html
= "/var/www/traffic.html"; /* hall of fame filename */
59 char *preview
= "/var/www/preview.html"; /* hall of fame preview */
60 char *cmdlog
= "/var/log/prometheuslog"; /* command log filename */
61 char *log_dir
= "/var/www/logs/"; /* log directory pathname, ended with slash */
62 char *log_url
= "logs/"; /* log directory relative URI prefix (partial URL) */
63 char *html_log_dir
= "/var/www/logs/html/";
65 /* ======= Help screen is hopefuly self-documenting part of code :-) ======= */
69 puts("Command line switches:\n\
71 -?, --help this help screen\n\
72 -v, --version show version number of this utility and exit\n\
73 -c filename force alternative /etc/prometheus.conf filename\n\
74 -h filename force alternative /etc/hosts filename (overrides hosts keyword)\n\
75 -f just flush iptables and tc classes and exit (stop shaping)\n\
76 -9 emergency iptables flush (do not read data transfer statistics)\n\
77 -p just generate preview of data transfer statistics and exit\n\
78 -n no delay (overrides qos-free-delay keyword)\n\
79 -d dry run (preview tc and iptables commands on stdout)\n\
80 -l Mmm YYYY generate HTML summary of traffic logs (Mmm=Jan-Dec or Year, YYYY=year)\n\
81 -m generate HTML summary of traffic logs for yesterday's month\n\
82 -y generate HTML summary of traffic logs for yesterday's year\n");
83 /* not yet implemented:
84 -s start shaping! (keep data transfer statistics - but apply shaping)\n\
85 -r just reload configuration (...and keep data transfer statistics)\n\
89 /* === Configuraration file values defaults - stored in global variables ==== */
91 int filter_type
=1; /*1 mark, 2 classify*/
93 char *mark_iptables
="MARK --set-mark ";
94 int dry_run
=0; /* preview - use puts() instead of system() */
95 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]";
96 FILE *iptables_file
=NULL
;
97 int enable_credit
=1; /* enable credit file */
98 int use_credit
=0; /* use credit file (if enabled)*/
99 char *title
="Hall of Fame - Greatest Suckers"; /* hall of fame title */
100 int hall_of_fame
=1; /* enable hall of fame */
101 char *lan
="eth0"; /* LAN interface */
102 char *lan_medium
="100Mbit"; /* 10Mbit/100Mbit ethernet */
103 char *wan
="eth1"; /* WAN/ISP interface */
104 char *wan_medium
="100Mbit"; /* 10Mbit/100Mbit ethernet */
105 char *qos_leaf
="sfq perturb 5"; /* leaf discipline */
106 char *qos_free_zone
=NULL
; /* QoS free zone */
107 int qos_proxy
=1; /* include proxy port to QoS */
108 int include_upload
=1; /* upload+download=total traffic */
109 char *proxy_ip
="192.168.1.1/32"; /* our IP with proxy port */
110 int proxy_port
=3128; /* proxy port number */
111 long long int line
=1024; /* WAN/ISP download in kbps */
112 long long int up
=1024; /* WAN/ISP upload in kbps */
113 int free_min
=32; /* minimum guaranted bandwidth for all undefined hosts */
114 int free_max
=64; /* maximum allowed bandwidth for all undefined hosts */
115 int qos_free_delay
=0; /* seconds to sleep before applying new QoS rules */
116 int digital_divide
=2; /* controls digital divide weirdness ratio, 1...3 */
117 int max_nesting
=3; /* maximum nesting of HTB clases, built-in maximum seems to be 4 */
119 int burst
=8; /* HTB burst (in kbits) */
122 int magic_priorities
=8; /* number of priority levels (soft shaping) */
123 int magic_treshold
=8; /* reduce ceil by X*magic_treshhold kbps (hard shaping) */
126 /* not yet implemented:
127 int fixed_packets=0; maximum number of pps per IP address (not class!)
128 int packet_limit=5; maximum number of pps to htn CEIL, not rate !!!
132 char *kwd
="via-prometheus"; /* /etc/hosts comment, eg. #qos-64-128 */
134 const int idxtable_treshold1
=24; /* this is no longer configurable */
135 const int idxtable_treshold2
=12; /* this is no longer configurable */
136 const int idxtable_bitmask1
=3; /* this is no longer configurable */
137 const int idxtable_bitmask2
=3; /* this is no longer configurable */
139 /* ==== This is C<<1 stuff - learn C<<1 first! http://cll1.arachne.cz ==== */
153 unsigned long long direct
;
154 unsigned long long proxy
;
155 unsigned long long upload
;
156 unsigned long long traffic
;
157 unsigned long long credit
;
158 unsigned long pktsup
;
159 unsigned long pktsdown
;
160 struct Keyword
*keyword
;
162 } *ips
=NULL
, *ip
, *sharedip
;
171 } *groups
=NULL
, *group
;
177 struct Index
*parent
;
181 } *idxs
=NULL
, *idx
, *metaindex
;
187 int asymetry_ratio
; /* ratio for ADSL-like upload */
188 int asymetry_fixed
; /* fixed treshold for ADSL-like upload */
189 int data_limit
; /* hard shaping: apply magic_treshold if max*data_limit MB exceeded */
190 int data_prio
; /* soft shaping (qos): reduce HTB prio if max*data_prio MB exceeded */
191 long fixed_limit
; /* fixed data limit for setting lower HTB ceil */
192 long fixed_prio
; /* fixed data lmit for setting lower HTB prio */
193 int reserve_min
; /* bonus for nominal HTB rate bandwidth (in kbps) */
194 int reserve_max
; /* malus for nominal HTB ceil (in kbps) */
195 // int divide_max; /* relative malus: new_ceil=rate+(old_ceil-rate)/divide_max */
196 // int htb_ceil_bonus_divide; /* relative bonus: new_ceil=old_ceil+old_ceil/htb_ceil_bonus_divide */
197 int default_prio
; /* default HTB priority for this keyword */
200 char *leaf_discipline
;
203 } *keyword
,*defaultkeyword
=NULL
,*keywords
=NULL
;
205 /* Damned, this must be object oriented! This looks almost like constructor ;-) */
215 ip
->mark
=ip
->min
=ip
->max
=ip
->desired
=ip
->credit
=0;
216 ip
->upload
=ip
->proxy
=ip
->direct
=ip
->traffic
=0;
217 ip
->pktsup
=ip
->pktsdown
=0;
218 ip
->keyword
=keywords
;
222 /* ====== iptables indexes are used to reduce complexity to log8(N) ===== */
224 char *very_ugly_ipv4_code(char *inip
,int bitmask
,int format_as_chainname
)
226 /* warning: this function was debugged only for bitmask values 20,24,28 !!!*/
228 char *ip
,*outip
,*outptr
,*fmt
;
231 /* debug printf("(%s,%d) -> ",ip,bitmask); */
233 if(ip
&& *ip
&& bitmask
>=0 && bitmask
<=32)
234 string(outip
,strlen(ip
)+10); /*fuck unicode? assertion: 10>strlen("_%d_%d") */
236 /* should never exit here */
243 if(dot
<(bitmask
/8-1))
245 if(format_as_chainname
)
254 char *cutdot
=strchr(ip
+1,'.'); /*for bitmask<24*/
255 if(cutdot
)*cutdot
='\0';
256 if(format_as_chainname
)
261 n
=atoi(ip
+1)-atoi(ip
+1)%(1<<(8-bitmask
%8));
265 /*debug printf("%d/%d => [_%d_%d]\n",atoi(ip+1),bitmask,n,bitmask); */
266 sprintf(outptr
,fmt
,n
,bitmask
);
267 if(!format_as_chainname
) while(bitmask
<24)
272 /* debug printf("[%s]\n",outip); */
283 /*should never exit here*/
288 char *hash_id(char *ip
,int bitmask
)
289 { return very_ugly_ipv4_code(ip
,bitmask
,1); }
291 char *subnet_id(char *ip
,int bitmask
)
292 { return very_ugly_ipv4_code(ip
,bitmask
,0); }
294 /* ================= Let's parse configuration file here =================== */
296 void reject_config_and_exit(char *filename
)
298 printf("Configuration file %s rejected - abnormal exit.",filename
);
302 void get_config(char *config_filename
)
306 printf("Configured keywords: ");
307 parse(config_filename
)
309 option("keyword",kwd
);
314 create(keyword
,Keyword
);
316 keyword
->asymetry_ratio
=1; /* ratio for ADSL-like upload */
317 keyword
->asymetry_fixed
=0; /* fixed treshold for ADSL-like upload */
318 keyword
->data_limit
=8; /* hard shaping: apply magic_treshold if max*data_limit MB exceeded */
319 keyword
->data_prio
=4; /* soft shaping (qos): reduce HTB prio if max*data_prio MB exceeded */
320 keyword
->fixed_limit
=0; /* fixed data limit for setting lower HTB ceil */
321 keyword
->fixed_prio
=0; /* fixed data limit for setting lower HTB prio */
322 keyword
->reserve_min
=8; /* bonus for nominal HTB rate bandwidth (in kbps) */
323 keyword
->reserve_max
=0; /* malus for nominal HTB ceil (in kbps) */
325 keyword->divide_max=0; relative malus: new_ceil=rate+(old_ceil-rate)/divide_max
326 keyword->htb_ceil_bonus_divide=0; relative bonus: new_ceil=old_ceil+old_ceil/htb_ceil_bonus_divide
328 keyword
->default_prio
=1;
329 keyword
->html_color
="000000";
331 keyword
->leaf_discipline
="";
333 push(keyword
,keywords
);
334 if(!defaultkeyword
) defaultkeyword
=keyword
;
339 else every(keyword
,keywords
)
341 int l
=strlen(keyword
->key
);
344 if(!strncmp(keyword
->key
,_
,l
) && strlen(_
)>l
+2)
346 char *tmptr
=_
; /* <---- l+1 ----> */
347 _
+=l
+1; /* via-prometheus-asymetry-ratio, etc. */
348 ioption("asymetry-ratio",keyword
->asymetry_ratio
);
349 ioption("asymetry-treshold",keyword
->asymetry_fixed
);
350 ioption("magic-relative-limit",keyword
->data_limit
);
351 ioption("magic-relative-prio",keyword
->data_prio
);
352 loption("magic-fixed-limit",keyword
->fixed_limit
);
353 loption("magic-fixed-prio",keyword
->fixed_prio
);
354 ioption("htb-default-prio",keyword
->default_prio
);
355 ioption("htb-rate-bonus",keyword
->reserve_min
);
356 ioption("htb-ceil-malus",keyword
->reserve_max
);
358 ioption("htb-ceil-divide",keyword->divide_max);
359 ioption("htb-ceil-bonus-divide",keyword->htb_ceil_bonus_divide);
361 option("leaf-discipline",keyword
->leaf_discipline
);
362 option("html-color",keyword
->html_color
);
365 if(keyword
->data_limit
|| keyword
->fixed_limit
||
366 keyword
->data_prio
|| keyword
->fixed_prio
)
372 option("iptables",iptables
);
373 option("iptables-save",iptablessave
); /* new */
374 option("iptables-restore",iptablesrestore
); /* new */
375 option("iptables-file",iptablesfile
); /* new */
376 option("hosts",hosts
);
377 option("lan-interface",lan
);
378 option("wan-interface",wan
);
379 option("lan-medium",lan_medium
);
380 option("wan-medium",wan_medium
);
381 lloption("wan-download",line
);
382 lloption("wan-upload",up
);
383 ioption("hall-of-fame-enable",hall_of_fame
);
384 option("hall-of-fame-title",title
);
385 option("hall-of-fame-filename",html
);
386 option("hall-of-fame-preview",preview
);
387 option("log-filename",cmdlog
);
388 option("credit-filename",credit
);
389 ioption("credit-enable",enable_credit
);
390 option("log-traffic-directory",log_dir
);
391 option("log-traffic-html-directory",html_log_dir
);
392 option("log-traffic-url-path",log_url
);
393 option("qos-free-zone",qos_free_zone
);
394 ioption("qos-free-delay",qos_free_delay
);
395 ioption("qos-proxy-enable",qos_proxy
);
396 option("qos-proxy-ip",proxy_ip
);
397 option("htb-leaf-discipline",qos_leaf
);
398 ioption("qos-proxy-port",proxy_port
);
399 ioption("free-rate",free_min
);
400 ioption("free-ceil",free_max
);
401 ioption("htb-burst",burst
);
402 ioption("htb-burst-main",burst_main
);
403 ioption("htb-burst-group",burst_group
);
404 ioption("htb-nesting-limit",max_nesting
);
405 ioption("htb-r2q",htb_r2q
);
406 ioption("magic-include-upload",include_upload
);
407 ioption("magic-priorities",magic_priorities
);
408 ioption("magic-treshold",magic_treshold
);
409 option("filter-type", cnf
);
411 /* not yet implemented:
412 ioption("magic-fixed-packets",fixed_packets);
413 ioption("magic-relative-packets",packet_limit);
418 perror(config_filename
);
419 puts("Warning - using built-in defaults instead ...");
424 /*leaf discipline for keywords*/
425 every(keyword
,keywords
)
427 if (!strcmpi(keyword
->leaf_discipline
, "")){
428 keyword
->leaf_discipline
= qos_leaf
;
432 if (strcmpi(cnf
, "mark")){
435 mark_iptables
= "CLASSIFY --set-class 1:";
439 mark_iptables
= "MARK --set-mark ";
442 /* are supplied values meaningful ?*/
445 puts("Illegal value of LAN or WAN bandwidth: 0 kbps.");
446 reject_config_and_exit(config_filename
);
450 /* ===================== traffic analyser - uses iptables ================ */
452 void get_traffic_statistics(void)
457 textfile(Pipe
,str
) *line
,*lines
=NULL
;
461 sprintf(cmd
,"%s -L -v -x -n -t mangle",iptables
);
473 int col
, accept
=0,proxyflag
=0,valid
=1,setchainname
=0,commonflag
=0;
474 unsigned long long traffic
=0;
475 unsigned long pkts
=0;
476 char *ipaddr
=NULL
,*ptr
;
478 /* debug puts(line->str); */
479 valid_columns(ptr
,line
->str
,' ',col
)
480 if(valid
) switch(col
)
482 case 1: if(eq(ptr
,"Chain"))
484 else if(eq(ptr
,"pkts"))
487 sscanf(ptr
,"%lu",&pkts
);
489 case 2: if(setchainname
)
491 if(!strncmp(ptr
,"post_",5) || eq(ptr
,"POSTROUTING"))
494 if(!strncmp(ptr
,"forw_",5) || eq(ptr
,"FORWARD"))
497 if(eq(ptr
,"post_common") || eq(ptr
,"forw_common"))
501 sscanf(ptr
,"%Lu",&traffic
); traffic
+=(1<<19); traffic
>>=20;
503 case 3: if((strncmp(ptr
,"post_",5) && strncmp(ptr
,"forw_",5)) || commonflag
)
505 /*if (filter_type==1) accept=eq(ptr,"MARK"); else accept=eq(ptr,"CLASSIFY");*/
507 case 8: if(downloadflag
)
509 if(strstr(proxy_ip
,ptr
))proxyflag
=1;
514 case 9: if(downloadflag
)ipaddr
=ptr
;break;
517 if(accept
&& traffic
>0 && ipaddr
)
519 if(proxyflag
)printf("(proxy) ");
520 else if(!downloadflag
) printf("(upload) ");
521 printf("IP %s: %Lu M (%ld pkts)\n", ipaddr
, traffic
, pkts
);
522 find(ip
,ips
,eq(ip
->addr
,ipaddr
));
527 if(eq(ip
->addr
,"0.0.0.0/0"))
529 ip
->name
="(unregistered)";
531 ip
->max
=ip
->desired
=free_max
;
540 ip
->traffic
+=traffic
;
541 ip
->direct
=ip
->traffic
-ip
->upload
-ip
->proxy
;
549 ip
->traffic
+=traffic
;
551 if(traffic
>ip
->traffic
)
560 /* ========== This function executes, logs OR ALSO prints command ========== */
562 void safe_run(char *cmd
)
564 if(dry_run
) printf("\n=>%s\n",cmd
); else system(cmd
);
565 if(log_file
) fprintf(log_file
,"%s\n",cmd
);
568 void save_line(char *line
)
570 fprintf(iptables_file
,"%s\n",line
);
573 void run_restore(void)
576 string(restor
,STRLEN
);
578 /*-----------------------------------------------------------------*/
579 printf("Running %s <%s ...\n",iptablesrestore
,iptablesfile
);
580 /*-----------------------------------------------------------------*/
583 fclose(iptables_file
);
593 sprintf(restor
,"%s <%s",iptablesrestore
, iptablesfile
);
599 /* == This function strips extra characters after IP address and stores it = */
601 void parse_ip(char *str
)
603 char *ptr
=str
,*ipaddr
=NULL
,*ipname
=NULL
;;
605 while(*ptr
&& *ptr
!=' ' && *ptr
!=9)
611 while(*ptr
&& (*ptr
==' ' || *ptr
==9))
614 while(*ptr
&& *ptr
!=' ' && *ptr
!=9)
618 find(ip
,ips
,eq(ip
->addr
,ipaddr
)); else TheIP();
623 char *parse_datafile_line(char *str
)
625 char *ptr
=strchr(str
,' ');
645 void parse_ip_log(int argc
, char **argv
)
647 char *month
, *year
, *str
, *name
, *ptr
, *ptr2
;
648 long traffic
, traffic_month
, total
=0, guaranted
;
649 int col
, col2
, y_ok
, m_ok
, accept_month
, i
=1, any_month
=0;
650 char mstr
[4], ystr
[5];
654 if(argv
[1][1]=='l') /* -l */
658 puts("Missing parameter(s)!\nUsage: prometheus -l Mmm YYYY (Mmm=Jan-Dec or Year, YYYY=year)");
664 if(eq(month
,"Year")) any_month
=1;
670 time_t t
= time(NULL
) - 3600*24 ; /* yesterday's timestamp*/
671 struct tm
*timep
= localtime(&t
);
673 if(argv
[1][1]=='m') /* -m yestarday - month */
675 strftime(mstr
, 4, "%b", timep
);
677 strftime(ystr
, 5, "%Y", timep
);
680 else /* -y yesterday - year */
684 strftime(ystr
, 5, "%Y", timep
);
688 printf("Analysing traffic for %s %s ...\n",month
,year
);
690 sprintf(str
,"%s %s/*.log",ls
,log_dir
);
695 ptr
=strrchr(str
,'\n');
697 printf("Parsing %s ...",str
);
704 valid_columns(ptr
,_
,'\t',col
) switch(col
)
706 case 2: name
= ptr
;break;
707 case 3: traffic
= atol(ptr
);break;
708 /* column number - was 7, now 10...*/
712 case 10: if (isalpha(*ptr
)) /* character, not numeric string = date, just one*/
714 valid_columns(ptr2
,ptr
,' ',col2
) switch(col2
)
716 case 2: if(any_month
|| eq(ptr2
,month
)) m_ok
= 1; break;
717 case 5: if(eq(ptr2
,year
)) y_ok
= 1; break;
722 if(col
== 7) guaranted
= atol(ptr
);
728 traffic_month
+= traffic
;
738 iplog
->guaranted
= guaranted
;
739 iplog
->traffic
= traffic_month
;
740 insert(iplog
,iplogs
,desc_order_by
,traffic
);
741 printf(" %ld MB\n",iplog
->traffic
);
744 puts(" no records.");
746 sprintf(str
,"%s/%s-%s.html",html_log_dir
,year
,month
);
747 printf("Writing %s ...",str
);
751 fprintf(f
,"<table border><tr><th colspan=\"4\">Data transfers - %s %s</th><th align=\"right\">Min.speed</th></tr>\n ",month
,year
);
755 fprintf(f
,"<tr><td align=\"right\">%d</td><th>%s</td><td align=\"right\">%ld MB</td><th align=\"right\">%ld GB</th><th align=\"right\">%ld kbps</th></tr>\n",
756 i
++, iplog
->name
, iplog
->traffic
, iplog
->traffic
>>10, iplog
->guaranted
);
757 total
+=iplog
->traffic
>>10;
759 fprintf(f
,"<tr><th colspan=\"3\" align=\"left\">Total:</th><th align=\"right\">%ld GB</th><th align=\"right\">%Ld kbps</th></tr>\n", total
, line
);
760 fputs("</table>\n", f
);
761 fprintf(f
, stats_html_signature
, version
);
768 /*-----------------------------------------------------------------*/
769 /* Are you looking for int main (int argc, char **argv) ? :-)) */
770 /*-----------------------------------------------------------------*/
778 int class_count
=0,ip_count
=0;
782 int just_preview
=0; /* preview - generate just stats */
783 int just_logs
=0; /* just parse logs */
785 char *chain_forward
, *chain_postrouting
;
789 Prometheus QoS - \"fair-per-IP\" Quality of Service setup utility.\n\
790 Version %s - Copyright (C)2005-2008 Michael Polak (xChaos)\n\
791 iptables-restore & burst tunning & classify modification 0.7d by Ludva\n\
792 Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version
);
794 /*----- Boring... we have to check command line options first: ----*/
798 argument("-c") { nextargument(config
); }
799 argument("-h") { nextargument(althosts
);}
800 argument("-d") { dry_run
=1; }
801 argument("-f") { just_flush
=1; }
802 argument("-9") { just_flush
=9; }
803 argument("-p") { just_preview
=1; }
804 argument("-n") { nodelay
=1; }
805 argument("-l") { just_logs
=1; }
806 argument("-m") { just_logs
=1; }
807 argument("-y") { just_logs
=1; }
808 argument("-?") { help(); exit(0); }
809 argument("--help") { help(); exit(0); }
810 argument("-v") { exit(0); }
811 argument("--version") { exit(0); }
815 puts("*** THIS IS JUST DRY RUN ! ***\n");
817 date(d
); /* this is typical cll1.h macro */
819 /*-----------------------------------------------------------------*/
820 printf("Parsing configuration file %s ...\n", config
);
821 /*-----------------------------------------------------------------*/
826 parse_ip_log(argc
,argv
);
830 if(althosts
) hosts
=althosts
;
834 /*-----------------------------------------------------------------*/
835 puts("Parsing iptables verbose output ...");
836 /*-----------------------------------------------------------------*/
837 get_traffic_statistics();
840 /*-----------------------------------------------------------------*/
841 printf("Parsing class defintion file %s ...\n", hosts
);
842 /*-----------------------------------------------------------------*/
843 int groupidx
= FIRSTGROUPID
;
848 if(*str
<'0' || *str
>'9')
851 //Does this IP share QoS class with some other ?
852 substring
=strstr(str
,"sharing-");
855 substring
+=8; //"sharing-"
858 ip
->sharing
=substring
;
859 ip
->keyword
=defaultkeyword
; /* settings for default keyword */
860 while(*substring
&& *substring
!='\n')
866 //Do we have to create new QoS class for this IP ?
868 find(keyword
,keywords
,(substring
=strstr(str
,keyword
->key
)))
874 ip
->prio
=keyword
->default_prio
;
875 substring
+=strlen(keyword
->key
)+1;
877 while(*ptr
&& *ptr
!='-')
882 ip
->max
=ip
->desired
=atoi(ptr
+1);
884 ip
->min
=atoi(substring
);
887 printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kbps\n",str
,free_min
);
893 ip
->max
=ip
->min
+ip
->keyword
->reserve_min
;
897 ip
->max
-=ip
->keyword
->reserve_max
;
900 if(ip->keyword->divide_max>1)
901 ip->max=ip->min+(ip->max-ip->min)/ip->keyword->divide_max;
902 if(ip->keyword->htb_ceil_bonus_divide>0)
903 ip->max+=ip->max/ip->keyword->htb_ceil_bonus_divide;
908 ip
->mark
=FIRSTIPCLASS
+1+class_count
++;
910 find(group
,groups
,group
->min
==ip
->min
)
913 group
->desired
+=ip
->min
;
914 ip
->group
= group
->id
;
920 group
->id
= groupidx
++;
921 ip
->group
= group
->id
;
923 if(group
->min
<8) group
->min
=8;
924 /* Warning - this is maybe because of primitive tc namespace, can be fixed */
925 /* it is because class IDs are derived from min. bandwidth. - xCh */
926 //if(group->min>MAX_GUARANTED_KBPS) group->min=MAX_GUARANTED_KBPS;
929 group
->desired
=ip
->min
;
930 insert(group
,groups
,desc_order_by
,min
);
942 /*-----------------------------------------------------------------*/
943 /* cll1.h - let's allocate brand new character buffer... */
944 /*-----------------------------------------------------------------*/
947 /*-----------------------------------------------------------------*/
948 puts("Resolving shared connections ...");
949 /*-----------------------------------------------------------------*/
950 search(ip
,ips
,ip
->sharing
)
952 search(sharedip
,ips
,eq(sharedip
->name
,ip
->sharing
))
954 sharedip
->traffic
+=ip
->traffic
;
956 ip
->mark
=sharedip
->mark
;
960 printf("Unresolved shared connection: %s %s sharing-%s\n",ip
->addr
,ip
->name
,ip
->sharing
);
963 if(enable_credit
&& just_flush
<9)
965 /*-----------------------------------------------------------------*/
966 printf("Parsing credit file %s ...\n", credit
);
967 /*-----------------------------------------------------------------*/
970 ptr
=parse_datafile_line(_
);
973 find(ip
,ips
,eq(ip
->addr
,_
))
974 sscanf(ptr
,"%Lu",&(ip
->credit
));
982 /*-----------------------------------------------------------------*/
983 puts("Initializing iptables and tc classes ...");
984 /*-----------------------------------------------------------------*/
986 iptables_file
=fopen(iptablesfile
,"w");
987 if (iptables_file
== NULL
) {
988 puts("Cannot open iptablesfile!");
992 log_file
=fopen(cmdlog
,"w");
993 if (log_file
== NULL
) {
994 puts("Cannot open logfile!");
998 save_line(iptablespreamble
);
1001 sprintf(str
,"%s qdisc del dev %s root 2>/dev/null",tc
,lan
);
1004 sprintf(str
,"%s qdisc del dev %s root 2>/dev/null",tc
,wan
);
1007 iptables_file
=fopen(iptablesfile
,"w");
1008 save_line(iptablespreamble
);
1010 if(qos_free_zone
&& *qos_free_zone
!='0')
1014 sprintf(str
,"-A FORWARD -d %s -o %s -j ACCEPT", qos_free_zone
, wan
);
1019 save_line(":post_noproxy - [0:0]");
1020 sprintf(str
,"-A POSTROUTING -p ! tcp -o %s -j post_noproxy", lan
);
1022 sprintf(str
,"-A POSTROUTING -s ! %s -o %s -j post_noproxy", proxy_ip
, lan
);
1024 sprintf(str
,"-A POSTROUTING -s %s -p tcp --sport ! %d -o %s -j post_noproxy", proxy_ip
, proxy_port
, lan
);
1027 chain
="post_noproxy";
1030 chain
="POSTROUTING";
1032 sprintf(str
,"-A %s -s %s -o %s -j ACCEPT", chain
, qos_free_zone
, lan
);
1036 if(ip_count
>idxtable_treshold1
&& !just_flush
)
1038 int idxcount
=0, bitmask
=32-idxtable_bitmask1
; /* default net mask: 255.255.255.240 */
1040 /*-----------------------------------------------------------------*/
1041 printf("Detected %d addresses - indexing iptables rules to improve performance...\n",ip_count
);
1042 /*-----------------------------------------------------------------*/
1044 save_line(":post_common - [0:0]");
1045 save_line(":forw_common - [0:0]");
1047 search(ip
,ips
,ip
->addr
&& *(ip
->addr
) && !eq(ip
->addr
,"0.0.0.0/0"))
1049 buf
=hash_id(ip
->addr
,bitmask
);
1050 find(idx
,idxs
,eq(idx
->id
,buf
))
1057 idx
->bitmask
=bitmask
;
1065 /* brutal perfomance optimalization */
1066 while(idxcount
>idxtable_treshold2
&& bitmask
>2*idxtable_bitmask2
)
1068 bitmask
-=idxtable_bitmask2
;
1070 search(idx
,idxs
,idx
->parent
==NULL
)
1072 buf
=hash_id(idx
->addr
,bitmask
);
1073 find(metaindex
,idxs
,eq(metaindex
->id
,buf
))
1074 metaindex
->children
++;
1077 create(metaindex
,Index
);
1078 metaindex
->addr
=idx
->addr
;
1080 metaindex
->bitmask
=bitmask
;
1081 metaindex
->parent
=NULL
;
1082 metaindex
->children
=0;
1084 push(metaindex
,idxs
);
1086 idx
->parent
=metaindex
;
1090 /* this should slightly optimize throughout ... */
1091 sort(idx
,idxs
,desc_order_by
,children
);
1092 sort(idx
,idxs
,order_by
,bitmask
);
1097 subnet
=subnet_id(idx
->addr
,idx
->bitmask
);
1098 printf("%d: %s/%d\n",++i
,subnet
,idx
->bitmask
);
1100 sprintf(str
,":post_%s - [0:0]", idx
->id
);
1103 sprintf(str
,":forw_%s - [0:0]", idx
->id
);
1108 string(buf
,strlen(idx
->parent
->id
)+6);
1109 sprintf(buf
,"post_%s",idx
->parent
->id
);
1114 sprintf(str
,"-A %s -d %s/%d -o %s -j post_%s", buf
, subnet
, idx
->bitmask
, lan
, idx
->id
);
1117 sprintf(str
,"-A %s -d %s/%d -o %s -j post_common", buf
, subnet
, idx
->bitmask
, lan
);
1122 string(buf
,strlen(idx
->parent
->id
)+6);
1123 sprintf(buf
,"forw_%s",idx
->parent
->id
);
1128 sprintf(str
,"-A %s -s %s/%d -o %s -j forw_%s", buf
, subnet
, idx
->bitmask
, wan
, idx
->id
);
1131 sprintf(str
,"-A %s -s %s/%d -o %s -j forw_common", buf
, subnet
, idx
->bitmask
, wan
);
1134 printf("Total indexed iptables chains created: %d\n", i
);
1136 sprintf(str
,"-A FORWARD -o %s -j forw_common", wan
);
1139 sprintf(str
,"-A POSTROUTING -o %s -j post_common", lan
);
1147 fclose(iptables_file
);
1148 if (log_file
) fclose(log_file
);
1149 puts("Just flushed iptables and tc classes - now exiting ...");
1155 if(!dry_run
&& !nodelay
&& qos_free_delay
)
1157 printf("Flushed iptables and tc classes - now sleeping for %d seconds...\n",qos_free_delay
);
1158 sleep(qos_free_delay
);
1161 sprintf(str
,"%s qdisc add dev %s root handle 1: htb r2q %d default 1",tc
,lan
,htb_r2q
);
1164 sprintf(str
,"%s class add dev %s parent 1: classid 1:2 htb rate %s ceil %s burst %dk prio 0",tc
,lan
,lan_medium
,lan_medium
,burst_main
);
1167 sprintf(str
,"%s class add dev %s parent 1:2 classid 1:1 htb rate %Ldkbit ceil %Ldkbit burst %dk prio 0",tc
,lan
,line
,line
,burst_main
);
1170 sprintf(str
,"%s qdisc add dev %s root handle 1: htb r2q %d default 1",tc
,wan
,htb_r2q
);
1173 sprintf(str
,"%s class add dev %s parent 1: classid 1:2 htb rate %s ceil %s burst %dk prio 0",tc
,wan
,wan_medium
,wan_medium
,burst_main
);
1176 sprintf(str
,"%s class add dev %s parent 1:2 classid 1:1 htb rate %Ldkbit ceil %Ldkbit burst %dk prio 0",tc
,wan
,up
,up
,burst_main
);
1180 /*-----------------------------------------------------------------*/
1181 puts("Locating suckers and generating root classes ...");
1182 /*-----------------------------------------------------------------*/
1183 sort(ip
,ips
,desc_order_by
,traffic
);
1186 /*-----------------------------------------------------------------*/
1187 /* sub-scope - local variables */
1189 long long int rate
=line
;
1190 long long int max
=line
;
1192 FILE *credit_file
=NULL
;
1194 if(!just_preview
&& !dry_run
&& enable_credit
) credit_file
=fopen(credit
,"w");
1202 sprintf(str
,"%s class add dev %s parent 1:%d classid 1:%d htb rate %Ldkbit ceil %Ldkbit burst %dk prio 1 #down desired %d",
1203 tc
, lan
, parent
, group
->id
, rate
, max
, burst_group
, group
->desired
);
1207 sprintf(str
,"%s class add dev %s parent 1:%d classid 1:%d htb rate %Ldkbit ceil %Ldkbit burst %dk prio 1 #up desired %d",
1208 tc
, wan
, parent
, group
->id
, rate
*up
/line
, max
*up
/line
, burst_group
, group
->desired
);
1212 if(group_count
++<max_nesting
) parent
=group
->id
;
1214 rate
-=digital_divide
*group
->min
;
1215 if(rate
<group
->min
)rate
=group
->min
;
1217 /*shaping of aggresive downloaders, with credit file support */
1220 int group_rate
=group
->min
, priority_sequence
=magic_priorities
+1;
1222 search(ip
, ips
, ip
->min
==group
->min
&& ip
->max
>ip
->min
)
1224 if( ip
->keyword
->data_limit
&& !ip
->fixedprio
&&
1225 ip
->traffic
>ip
->credit
+
1226 (ip
->min
*ip
->keyword
->data_limit
+(ip
->keyword
->fixed_limit
<<20)) )
1228 if(group_rate
<ip
->max
) ip
->max
=group_rate
;
1229 group_rate
+=magic_treshold
;
1230 ip
->prio
=magic_priorities
+2;
1231 if(ip
->prio
<3) ip
->prio
=3;
1235 if( ip
->keyword
->data_prio
&& !ip
->fixedprio
&&
1236 ip
->traffic
>ip
->credit
+
1237 (ip
->min
*ip
->keyword
->data_prio
+(ip
->keyword
->fixed_prio
<<20)) )
1239 ip
->prio
=priority_sequence
--;
1240 if(ip
->prio
<2) ip
->prio
=2;
1245 unsigned long long lcredit
=0;
1247 if((ip
->min
*ip
->keyword
->data_limit
+(ip
->keyword
->fixed_limit
<<20))>ip
->traffic
)
1248 lcredit
=(ip
->min
*ip
->keyword
->data_limit
+(ip
->keyword
->fixed_limit
<<20))-ip
->traffic
;
1249 fprintf(credit_file
,"%s %Lu\n",ip
->addr
,lcredit
);
1256 if(credit_file
)fclose(credit_file
);
1261 f
=fopen(preview
,"w");
1264 else if(!dry_run
&& !just_flush
)
1266 /*-----------------------------------------------------------------*/
1267 printf("Writing data transfer database ...\n");
1268 /*-----------------------------------------------------------------*/
1269 f
=fopen("/var/run/prometheus.previous","w");
1272 search(ip
,ips
,ip
->traffic
|| ip
->direct
|| ip
->proxy
||ip
->upload
)
1273 fprintf(f
,"%s %Lu %Lu %Lu %Lu\n",ip
->addr
,ip
->traffic
,ip
->direct
,ip
->proxy
,ip
->upload
);
1287 /*-----------------------------------------------------------------*/
1288 printf("Sorting data and generating statistics page %s ...\n",ptr
);
1289 /*-----------------------------------------------------------------*/
1291 fputs("<table border>\n<tr><th align=\"right\">#</th><th align=\"right\">group</th><th align=\"right\">IPs</th><th align=\"right\">requested</th>\n",f
);
1292 fprintf(f
,"<th colspan=\"%d\">data limits</th>\n",keywordcount
);
1297 printf("%d k group: %d bandwidth requested: %d k\n",group
->min
,group
->count
,group
->desired
);
1299 fprintf(f
,"<tr><td align=\"right\">%d</td><td align=\"right\">%d k</td>",count
,group
->min
);
1300 fprintf(f
,"<td align=\"right\">%d</td><td align=\"right\">%d k</td>",group
->count
,group
->desired
);
1302 every(keyword
,keywords
)
1303 fprintf(f
,"<td align=\"right\"><font color=\"#%s\">%d M</font></td>",keyword
->html_color
,group
->min
*keyword
->data_limit
);
1306 total
+=group
->count
;
1310 printf("Total groups: %d Total bandwidth requested: %d k\nAGGREGATION: 1/%d\n",count
,i
,i
/line
);
1312 fprintf(f
,"<tr><th colspan=\"2\" align=\"left\">Line %Ld k</td>",line
);
1313 fprintf(f
,"<th align=\"right\">%d</td><th align=\"right\">%d k</td>",total
,i
);
1315 every(keyword
,keywords
)
1316 fprintf(f
,"<th align=\"right\">%d IPs</th>",keyword
->ip_count
);
1318 fprintf(f
,"</tr><tr><th colspan=\"4\">Aggregation 1/%d</th>\n",(int)(0.5+i
/line
));
1319 fprintf(f
,"<th colspan=\"%d\">%d traffic classes</th></tr>\n",keywordcount
,total
);
1321 fputs("</table>\n",f
);
1323 else if(!dry_run
&& !just_flush
)
1329 unsigned long long total
=0, total_direct
=0, total_proxy
=0, total_upload
=0, tmp_sum
=0;
1330 int active_classes
=0;
1333 struct Sum
{unsigned long long l
; int i
; list(Sum
);} *sum
,*sums
=NULL
;
1340 fprintf(f
,"<p><table border>\n<tr><th colspan=\"%d\">%s",colspan
,title
);
1341 fprintf(f
," (%s)</th></tr>\n", d
);
1342 fputs("<tr><td align=\"right\">#</td><td>hostname</td>\
1343 <td align=\"right\">credit</td>\
1344 <td align=\"right\">limit</td>\
1345 <td align=\"right\">total</td>\
1346 <td align=\"right\">direct</td>\n",f
);
1348 fputs("<td align=\"right\">proxy</td>\n",f
);
1349 fputs("<td align=\"right\">upload</td>\
1350 <td align=\"right\">minimum</td>\
1351 <td align=\"right\">desired</td>\
1352 <td align=\"right\">maximum</td>\
1353 <td>prio</td></tr>\n",f
);
1357 char *f1
="", *f2
="";
1358 if(ip
->max
<ip
->desired
)
1360 f1
="<font color=\"red\">";
1365 f1
="<font color=\"brown\">";
1370 printf("%03d. %-22s %10Lu (%d/%d)\n",i
,ip
->name
, ip
->traffic
, ip
->min
, ip
->max
);
1372 fprintf(f
,"<tr><td align=\"right\"><a name=\"%s\"></a>%d</td><td><a href=\"%s%s.log\">%s</a></td><td align=\"right\">%Lu M</td>\n",
1373 ip
->name
, i
, log_url
, ip
->name
, ip
->name
, ip
->credit
);
1374 fprintf(f
,"<td align=\"right\"><font color=\"#%s\">%Lu M</font></td>",ip
->keyword
->html_color
,ip
->credit
+(ip
->min
*ip
->keyword
->data_limit
+(ip
->keyword
->fixed_limit
<<20)));
1375 fprintf(f
,"<td align=\"right\">%s%Lu M%s</td><td align=\"right\">%Lu M</td>\n", f1
, ip
->traffic
, f2
, ip
->direct
);
1377 fprintf(f
,"<td align=\"right\">%Lu M</td>\n", ip
->proxy
);
1378 fprintf(f
,"<td align=\"right\">%Lu M</td>\n", ip
->upload
);
1379 fprintf(f
,"<td align=\"right\">%d k</td><td align=\"right\">%d k</td><td align=\"right\">%s%d k%s</td><td>%s%d%s</td></tr>\n",ip
->min
,ip
->desired
,f1
,ip
->max
,f2
,f1
,ip
->prio
,f2
);
1381 total_direct
+=ip
->direct
;
1382 total_proxy
+=ip
->proxy
;
1383 total_upload
+=ip
->upload
;
1387 tmp_sum
+=ip
->traffic
;
1390 sum
->i
=active_classes
;
1391 insert(sum
,sums
,order_by
,i
);
1398 sprintf(str
,"%s/%s.log",log_dir
,ip
->name
);
1399 iplog
=fopen(str
,"a");
1402 fprintf(iplog
,"%ld\t%s\t%Lu\t%Lu\t%Lu\t%Lu\t%d\t%d\t%d\t%s",
1403 time(NULL
),ip
->name
,ip
->traffic
,ip
->direct
,ip
->proxy
,ip
->upload
,ip
->min
,ip
->max
,ip
->desired
,d
); /* d = date*/
1409 fprintf(f
,"<tr><th colspan=\"4 \"align=\"left\">SUMMARY:</td>");
1410 fprintf(f
,"<th align=\"right\">%Lu M</th>\
1411 <th align=\"right\">%Lu M</th>\n", total
, total_direct
);
1413 fprintf(f
,"<th align=\"right\">%Lu M</th>\n", total_proxy
);
1414 fprintf(f
,"<th align=\"right\">%Lu M</th>", total_upload
);
1415 fputs("<td colspan=\"4\"></td></th>\n</table>\n",f
);
1417 if(active_classes
>10)
1419 fputs("<a name=\"erp\"></a><p><table border><tr><th colspan=\"5\">Enterprise Research and Planning (ERP)</th></tr>\n",f
);
1420 fputs("<tr><td>Analytic category</td>\n",f
);
1421 fputs("<td colspan=\"2\" align=\"center\">Active Classes</td><td colspan=\"2\" align=\"center\">Data transfers</td></tr>\n",f
);
1423 find(sum
,sums
,sum
->l
>=total
/4)
1425 fprintf(f
,"<tr><td>Top 25%% of traffic</td>\n");
1426 fprintf(f
,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum
->i
,(100*sum
->i
+50)/active_classes
,sum
->l
,(100*sum
->l
+50)/total
);
1429 find(sum
,sums
,sum
->i
==10)
1431 fprintf(f
,"<tr><td>Top 10 downloaders</td>\n");
1432 fprintf(f
,"<th align=\"right\">10</th><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",(100*sum
->i
+50)/active_classes
,sum
->l
,(100*sum
->l
+50)/total
);
1435 find(sum
,sums
,sum
->l
>=total
/2)
1437 fprintf(f
,"<tr><td>Top 50%% of traffic</td>\n");
1438 fprintf(f
,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><th align=\"right\">%Ld %%</th></tr>\n",sum
->i
,(100*sum
->i
+50)/active_classes
,sum
->l
,(100*sum
->l
+50)/total
);
1441 find(sum
,sums
,sum
->l
>=4*total
/5)
1443 fprintf(f
,"<tr><td>Top 80%% of traffic</td>\n");
1444 fprintf(f
,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><th align=\"right\">%Ld %%</th></tr>\n",sum
->i
,(100*sum
->i
+50)/active_classes
,sum
->l
,(100*sum
->l
+50)/total
);
1447 find (sum
,sums
,sum
->i
>=(active_classes
+1)/5)
1449 fprintf(f
,"<tr><td>Top 20%% downloaders</td>\n");
1450 fprintf(f
,"<td align=\"right\">%d</td><th align=\"right\">%d %%</th><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum
->i
,(100*sum
->i
+50)/active_classes
,sum
->l
,(100*sum
->l
+50)/total
);
1453 find(sum
,sums
,sum
->i
>=(active_classes
+1)/4)
1455 fprintf(f
,"<tr><td>Top 25%% downloaders</td>\n");
1456 fprintf(f
,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum
->i
,(100*sum
->i
+50)/active_classes
,sum
->l
,(100*sum
->l
+50)/total
);
1459 find(sum
,sums
,sum
->i
>=(active_classes
+1)/2)
1461 fprintf(f
,"<tr><td>Top 50%% downloaders</td>\n");
1462 fprintf(f
,"<td align=\"right\">%d</td><th align=\"right\">%d %%</th><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum
->i
,(100*sum
->i
+50)/active_classes
,sum
->l
,(100*sum
->l
+50)/total
);
1465 find(sum
,sums
,sum
->i
>=4*(active_classes
+1)/5)
1467 fprintf(f
,"<tr><td>Top 80%% downloaders</td>\n");
1468 fprintf(f
,"<td align=\"right\">%d</td><td align=\"right\">%d %%</td><td align=\"right\">%Lu M</td><td align=\"right\">%Ld %%</td></tr>\n",sum
->i
,(100*sum
->i
+50)/active_classes
,sum
->l
,(100*sum
->l
+50)/total
);
1471 fprintf(f
,"<tr><td>All users, all traffic</td>\n");
1472 fprintf(f
,"<th align=\"right\">%d</th><th align=\"right\">100 %%</th><th align=\"right\">%Lu M</th><th align=\"right\">100 %%</th></tr>\n",active_classes
,total
);
1473 fputs("</table>\n", f
);
1475 fprintf(f
, stats_html_signature
, version
);
1481 puts("Statistics preview generated (-p switch) - now exiting ...");
1485 /*-----------------------------------------------------------------*/
1486 puts("Generating iptables and tc classes ...");
1487 /*-----------------------------------------------------------------*/
1490 printf("%-22s %-15s mark\n","name","ip");
1491 search(ip
,ips
,ip
->mark
>0)
1497 duplicate(ip
->addr
,buf
);
1498 buf
=hash_id(ip
->addr
,32-idxtable_bitmask1
);
1500 string(chain_forward
,6+strlen(buf
));
1501 strcpy(chain_forward
,"forw_");
1502 strcat(chain_forward
,buf
);
1504 string(chain_postrouting
,6+strlen(buf
));
1505 strcpy(chain_postrouting
,"post_");
1506 strcat(chain_postrouting
,buf
);
1512 chain_forward
="FORWARD";
1513 chain_postrouting
="POSTROUTING";
1516 printf("%-22s %-16s %04d ", ip
->name
, ip
->addr
, ip
->mark
);
1518 /* -------------------------------------------------------- mark download */
1520 sprintf(str
,"-A %s -d %s/32 -o %s -j %s%d",chain_postrouting
,ip
->addr
,lan
,mark_iptables
,ip
->mark
);
1521 /*sprintf(str,"-A %s -d %s/32 -o %s -j MARK --set-mark %d",chain_postrouting,ip->addr,lan,ip->mark);*/
1522 /* -m limit --limit 1/s */
1527 sprintf(str
,"-A %s -s %s -p tcp --sport %d -d %s/32 -o %s -j %s%d",chain_postrouting
,proxy_ip
,proxy_port
,ip
->addr
,lan
,mark_iptables
,ip
->mark
);
1528 /*sprintf(str,"-A %s -s %s -p tcp --sport %d -d %s/32 -o %s -j MARK --set-mark %d",chain_postrouting,proxy_ip,proxy_port,ip->addr,lan,ip->mark);*/
1532 sprintf(str
,"-A %s -d %s/32 -o %s -j ACCEPT",chain_postrouting
,ip
->addr
,lan
);
1535 /* -------------------------------------------------------- mark upload */
1536 sprintf(str
,"-A %s -s %s/32 -o %s -j %s%d",chain_forward
,ip
->addr
,wan
,mark_iptables
,ip
->mark
);
1537 /* sprintf(str,"-A %s -s %s/32 -o %s -j MARK --set-mark %d",chain_forward,ip->addr,wan,ip->mark);*/
1540 sprintf(str
,"-A %s -s %s/32 -o %s -j ACCEPT",chain_forward
,ip
->addr
,wan
);
1545 /* -------------------------------------------------------- download class */
1546 printf("(down: %dk-%dk ", ip
->min
, ip
->max
);
1548 sprintf(str
,"%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d", tc
, lan
, ip
->group
, ip
->mark
,ip
->min
,ip
->max
, burst
, ip
->prio
);
1551 if (strcmpi(ip
->keyword
->leaf_discipline
, "none")){
1552 sprintf(str
,"%s qdisc add dev %s parent 1:%d handle %d %s", tc
, lan
, ip
->mark
, ip
->mark
, ip
->keyword
->leaf_discipline
); /*qos_leaf*/
1556 if (filter_type
== 1){
1557 sprintf(str
,"%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d", tc
, lan
, ip
->mark
, ip
->mark
);
1561 /* -------------------------------------------------------- upload class */
1562 printf("up: %dk-%dk)\n", (int)((ip
->min
/ip
->keyword
->asymetry_ratio
)-ip
->keyword
->asymetry_fixed
),
1563 (int)((ip
->max
/ip
->keyword
->asymetry_ratio
)-ip
->keyword
->asymetry_fixed
));
1565 sprintf(str
,"%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
1566 tc
, wan
, ip
->group
, ip
->mark
,
1567 (int)((ip
->min
/ip
->keyword
->asymetry_ratio
)-ip
->keyword
->asymetry_fixed
),
1568 (int)((ip
->max
/ip
->keyword
->asymetry_ratio
)-ip
->keyword
->asymetry_fixed
), burst
, ip
->prio
);
1571 if (strcmpi(ip
->keyword
->leaf_discipline
, "none")){
1572 sprintf(str
,"%s qdisc add dev %s parent 1:%d handle %d %s",tc
, wan
, ip
->mark
, ip
->mark
, ip
->keyword
->leaf_discipline
); /*qos_leaf*/
1576 if (filter_type
== 1){
1577 sprintf(str
,"%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d",tc
, wan
, ip
->mark
, ip
->mark
);
1582 printf("(sharing %s)\n", ip
->sharing
);
1589 chain_forward
="forw_common";
1590 chain_postrouting
="post_common";
1594 chain_forward
="FORWARD";
1595 chain_postrouting
="POSTROUTING";
1598 /* -------------------------------------------------------- mark download */
1602 sprintf(str
,"-A %s -s %s -p tcp --sport %d -o %s -j %s%d",chain_postrouting
,proxy_ip
,proxy_port
,lan
,mark_iptables
,3);
1604 sprintf(str
,"-A %s -s %s -p tcp --sport %d -o %s -j ACCEPT",chain_postrouting
,proxy_ip
,proxy_port
,lan
);
1607 sprintf(str
,"-A %s -o %s -j %s%d",chain_postrouting
,lan
,mark_iptables
,3);
1609 sprintf(str
,"-A %s -o %s -j ACCEPT",chain_postrouting
,lan
);
1612 /* -------------------------------------------------------- mark upload */
1613 sprintf(str
,"-A %s -o %s -j %s%d",chain_forward
,wan
,mark_iptables
,3);
1615 sprintf(str
,"-A %s -o %s -j ACCEPT",chain_forward
,wan
);
1618 printf("Total IP count: %d\n", i
);
1620 /*-----------------------------------------------------------------*/
1621 puts("Generating free bandwith classes ...");
1622 /*-----------------------------------------------------------------*/
1624 /* ---------------------------------------- tc - free bandwith shared class */
1625 sprintf(str
,"%s class add dev %s parent 1:%d classid 1:3 htb rate %dkbit ceil %dkbit burst %dk prio 2",tc
,lan
,parent
,free_min
,free_max
,burst
);
1628 sprintf(str
,"%s class add dev %s parent 1:%d classid 1:3 htb rate %dkbit ceil %dkbit burst %dk prio 2",tc
,wan
,parent
,free_min
,free_max
,burst
);
1632 if (strcmpi(qos_leaf
, "none")){
1633 sprintf(str
,"%s qdisc add dev %s parent 1:3 handle 3 %s",tc
,lan
,qos_leaf
);
1636 sprintf(str
,"%s qdisc add dev %s parent 1:3 handle 3 %s",tc
,wan
,qos_leaf
);
1640 /* tc handle 1 fw flowid */
1641 sprintf(str
,"%s filter add dev %s parent 1:0 protocol ip handle 3 fw flowid 1:3",tc
,lan
);
1644 sprintf(str
,"%s filter add dev %s parent 1:0 protocol ip handle 3 fw flowid 1:3",tc
,wan
);
1649 if (log_file
) fclose(log_file
);
1652 /* that's all folks, thank you for reading it all the way up to this point ;-) */
1653 /* bad luck C<<1 is not yet finished, I promise no sprintf() next time... */
This page took 1.680637 seconds and 4 git commands to generate.