bug fix - logging of shared badwidth classes, better error reporting
[svn/Prometheus-QoS/.git] / prometheus.c
CommitLineData
312ac2c1 1 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */\r
2 /* Prometheus QoS - you can "steal fire" from your ISP */\r
3/* "fair-per-IP" quality of service (QoS) utility */\r
4/* requires Linux 2.4.x or 2.6.x with HTB support */\r
5/* Copyright(C) 2005-2008 Michael Polak (xChaos) */\r
6/* iptables-restore support Copyright(C) 2007-2008 ludva */\r
7/* Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf */\r
8/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */\r
9\r
260c2719 10/* Modified by: xChaos, 20120511\r
a031788f 11 ludva, 20080415\r
208112af 12 \r
007c44c5 13 Prometheus QoS is free software; you can redistribute it and/or\r
14 modify it under the terms of the GNU General Public License as \r
15 published by the Free Software Foundation; either version 2.1 of \r
16 the License, or (at your option) any later version.\r
17\r
18 Prometheus QoS is distributed in the hope that it will be useful,\r
19 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
21 General Public License for more details.\r
22\r
c9012978 23 You should have received a copy of the GNU General Public License\r
24 along with Prometheus Qos; if not, write to the Free Software\r
d1ae4fa7 25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA \r
26 \r
c9012978 27 GNU General Public License is located in file COPYING */\r
007c44c5 28\r
29#define STRLEN 256\r
30#define FIRSTGROUPID 1024\r
43e07280 31#define FIRSTIPCLASS 2048\r
007c44c5 32#undef DEBUG\r
33\r
208112af 34#include "cll1-0.6.2.h"\r
007c44c5 35\r
260c2719 36const char *version = "0.8.3-c";\r
007c44c5 37\r
208112af 38/* Version numbers: 0.8.3 is development releases ("beta"), 0.8.4 will be "stable" */\r
b1b59b3a 39/* Debian(RPM) package versions/patchlevels: 0.7.9-2, 0.8.0-1, 0.8.0-2, etc. */\r
40/* C source code development versions ("beta"): 0.7.9-a, 0.8.1-b, etc. */\r
41/* C source code release versions: 0.8.0, 0.8.2, 0.8.4, etc. */\r
77e71d6b 42\r
103d292c 43const char *stats_html_signature = "<span class=\"small\">Statistics generated by Prometheus QoS version %s<br />GPL+Copyright(C)2005-2012 Michael Polak, <a href=\"http://www.arachne.cz/\">Arachne Labs</a></span>\n";\r
2d114137 44\r
82c702a1 45/* ======= All path names are defined here (for RPM patch) ======= */\r
ae776b10 46\r
2d114137 47char *tc = "/sbin/tc"; /* requires tc with HTB support */\r
48char *iptables = "/sbin/iptables"; /* requires iptables utility */\r
49char *iptablessave = "/sbin/iptables-save"; /* not yet required */\r
ae776b10 50char *iptablesrestore = "/sbin/iptables-restore"; /* requires iptables-restore */\r
208112af 51const char *ls = "/bin/ls"; /* this is not user configurable :-) */\r
ae776b10 52\r
2d114137 53char *config = "/etc/prometheus/prometheus.conf"; /* main configuration file */\r
54char *hosts = "/etc/prometheus/hosts"; /* per-IP bandwidth definition file */\r
ae776b10 55\r
2d114137 56char *iptablesfile = "/var/spool/prometheus.iptables"; /* temporary file for iptables-restore*/\r
57char *credit = "/var/lib/misc/prometheus.credit"; /* credit log file */\r
58char *html = "/var/www/traffic.html"; /* hall of fame filename */\r
59char *preview = "/var/www/preview.html"; /* hall of fame preview */\r
60char *cmdlog = "/var/log/prometheuslog"; /* command log filename */\r
61char *log_dir = "/var/www/logs/"; /* log directory pathname, ended with slash */\r
8bcc3268 62char *log_url = "/logs/"; /* log directory relative URI prefix (partial URL) */\r
2d114137 63char *html_log_dir = "/var/www/logs/html/";\r
ae776b10 64\r
6cc38f96 65char *jquery_url = "http://code.jquery.com/jquery-latest.js";\r
66char *lms_url = "/lms/?m=customerinfo&amp;id=";\r
67int use_jquery_popups = 1;\r
9aa195f6 68int row_odd_even = 0; /*<tr class="odd/even"> */\r
69\r
70const char *tr_odd_even(void)\r
71{\r
72 row_odd_even = 1 - row_odd_even;\r
73 if(row_odd_even)\r
74 {\r
75 return "<tr class=\"even\">\n";\r
76 }\r
77 else\r
78 {\r
79 return "<tr class=\"odd\">\n";\r
80 }\r
81}\r
6cc38f96 82\r
007c44c5 83/* ======= Help screen is hopefuly self-documenting part of code :-) ======= */\r
84\r
85void help(void)\r
86{\r
87 puts("Command line switches:\n\\r
88\n\\r
89-?, --help this help screen\n\\r
208112af 90-v, --version show Version number of this utility and exit\n\\r
91-c filename force alternative /etc/prometheus.Conf filename\n\\r
92-h filename force alternative /etc/Hosts filename (overrides hosts keyword)\n\\r
93-f just Flush iptables and tc classes and exit (stop shaping)\n\\r
007c44c5 94-9 emergency iptables flush (do not read data transfer statistics)\n\\r
208112af 95-p just generate Preview of data transfer statistics and exit\n\\r
96-d Dry run (preview tc and iptables commands on stdout)\n\\r
97-r Run (reset all statistics and start shaping)\n\\r
98-n run Now (start shaping without delay - overrides qos-free-delay keyword)\n\\r
99-l Mmm YYYY generate HTML summary of traffic Logs (Mmm=Jan-Dec or Year, YYYY=year)\n\\r
100-m generate HTML summary of traffic logs for yesterday's Month\n\\r
101-y generate HTML summary of traffic logs for yesterday's Year\n");\r
5b902402 102/* not yet implemented:\r
103-s start shaping! (keep data transfer statistics - but apply shaping)\n\\r
5b902402 104*/\r
007c44c5 105}\r
a1d21464 106\r
007c44c5 107/* === Configuraration file values defaults - stored in global variables ==== */\r
108\r
43cde5c3 109int filter_type = 1; /*1 mark, 2 classify*/\r
110char *mark = "MARK";\r
111char *mark_iptables = "MARK --set-mark ";\r
112int dry_run = 0; /* preview - use puts() instead of system() */\r
113char *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
114FILE *iptables_file = NULL;\r
115int enable_credit = 1; /* enable credit file */\r
116int use_credit = 0; /* use credit file (if enabled)*/\r
117char *title = "Hall of Fame - Greatest Suckers"; /* hall of fame title */\r
118int hall_of_fame = 1; /* enable hall of fame */\r
119char *lan = "eth0"; /* LAN interface */\r
120char *lan_medium = "100Mbit"; /* 10Mbit/100Mbit ethernet */\r
121char *wan = "eth1"; /* WAN/ISP interface */\r
122char *wan_medium = "100Mbit"; /* 10Mbit/100Mbit ethernet */\r
123char *qos_leaf = "sfq perturb 5"; /* leaf discipline */\r
124char *qos_free_zone = NULL; /* QoS free zone */\r
125int qos_proxy = 1; /* include proxy port to QoS */\r
4358455e 126int found_lmsid = 0; /* show links to users in LMS information system */\r
43cde5c3 127int include_upload = 1; /* upload+download=total traffic */\r
128char *proxy_ip = "192.168.1.1/32"; /* our IP with proxy port */\r
129int proxy_port = 3128; /* proxy port number */\r
130long long int line = 1024; /* WAN/ISP download in kbps */\r
131long long int up = 1024; /* WAN/ISP upload in kbps */\r
132int free_min = 32; /* minimum guaranted bandwidth for all undefined hosts */\r
133int free_max = 64; /* maximum allowed bandwidth for all undefined hosts */\r
134int qos_free_delay = 0; /* seconds to sleep before applying new QoS rules */\r
135int digital_divide = 2; /* controls digital divide weirdness ratio, 1...3 */ \r
136int max_nesting = 3; /* maximum nesting of HTB clases, built-in maximum seems to be 4 */\r
208112af 137int htb_r2q = 256; /* should work for leaf values 512 kbps to 8 Mbps */\r
43cde5c3 138int burst = 8; /* HTB burst (in kbits) */\r
139int burst_main = 64;\r
140int burst_group = 32;\r
43cde5c3 141int magic_treshold = 8; /* reduce ceil by X*magic_treshhold kbps (hard shaping) */\r
142int keywordcount = 0;\r
007c44c5 143/* not yet implemented:\r
43cde5c3 144int fixed_packets = 0; maximum number of pps per IP address (not class!) \r
145int packet_limit = 5; maximum number of pps to htn CEIL, not rate !!! \r
007c44c5 146*/\r
43cde5c3 147FILE *log_file = NULL;\r
148char *kwd = "via-prometheus"; /* /etc/hosts comment, eg. #qos-64-128 */\r
007c44c5 149\r
208112af 150const int highest_priority = 0; /* highest HTB priority (HTB built-in value is 0) */\r
151const int lowest_priority = 7; /* lowest HTB priority (HTB built-in value is 7) */\r
152const int idxtable_treshold1 = 24; /* this is no longer configurable */\r
153const int idxtable_treshold2 = 12; /* this is no longer configurable */\r
154const int idxtable_bitmask1 = 3; /* this is no longer configurable */\r
155const int idxtable_bitmask2 = 3; /* this is no longer configurable */\r
007c44c5 156\r
a1d21464 157/* ==== This is C<<1 stuff - learn C<<1 first! https://dev.arachne.cz/svn/cll1h ==== */\r
007c44c5 158\r
159struct IP\r
160{\r
161 char *addr;\r
162 char *name;\r
163 char *sharing;\r
164 int min;\r
165 int desired;\r
166 int max;\r
167 int mark;\r
168 int prio;\r
169 int fixedprio;\r
170 int group;\r
bb9e11ee 171 int lmsid;\r
007c44c5 172 unsigned long long direct;\r
173 unsigned long long proxy;\r
174 unsigned long long upload;\r
175 unsigned long long traffic;\r
176 unsigned long long credit;\r
177 unsigned long pktsup;\r
178 unsigned long pktsdown;\r
179 struct Keyword *keyword;\r
180 list(IP);\r
181} *ips=NULL, *ip, *sharedip;\r
182\r
183struct Group\r
184{\r
185 int min;\r
186 int count;\r
187 int desired;\r
188 int id;\r
189 list(Group);\r
190} *groups=NULL, *group;\r
191\r
192struct Index\r
193{\r
194 char *addr;\r
195 char *id;\r
196 struct Index *parent;\r
197 int bitmask;\r
198 int children;\r
199 list(Index);\r
200} *idxs=NULL, *idx, *metaindex;\r
201\r
202struct Keyword\r
203{\r
204 char *key;\r
205 \r
206 int asymetry_ratio; /* ratio for ADSL-like upload */\r
207 int asymetry_fixed; /* fixed treshold for ADSL-like upload */\r
208 int data_limit; /* hard shaping: apply magic_treshold if max*data_limit MB exceeded */\r
19a47f60 209 int data_prio; /* soft shaping (qos): reduce HTB prio if max*data_prio MB exceeded */\r
007c44c5 210 long fixed_limit; /* fixed data limit for setting lower HTB ceil */\r
211 long fixed_prio; /* fixed data lmit for setting lower HTB prio */\r
212 int reserve_min; /* bonus for nominal HTB rate bandwidth (in kbps) */\r
213 int reserve_max; /* malus for nominal HTB ceil (in kbps) */\r
214// int divide_max; /* relative malus: new_ceil=rate+(old_ceil-rate)/divide_max */\r
215// int htb_ceil_bonus_divide; /* relative bonus: new_ceil=old_ceil+old_ceil/htb_ceil_bonus_divide */\r
216 int default_prio; /* default HTB priority for this keyword */\r
217 char *html_color;\r
218 int ip_count;\r
219 char *leaf_discipline;\r
220 \r
221 list(Keyword);\r
222} *keyword,*defaultkeyword=NULL,*keywords=NULL;\r
223\r
224/* Damned, this must be object oriented! This looks almost like constructor ;-) */\r
225\r
226void TheIP(void)\r
227{\r
228 create(ip,IP);\r
19a47f60 229 ip->name = "";\r
230 ip->addr = "";\r
231 ip->sharing = NULL;\r
232 ip->prio = highest_priority+1;\r
a1d21464 233 ip->lmsid = -1;\r
bb9e11ee 234 ip->fixedprio = \\r
19a47f60 235 ip->mark = \\r
236 ip->min = \\r
237 ip->max = \\r
238 ip->desired = \\r
239 ip->credit = \\r
240 ip->upload = \\r
241 ip->proxy = \\r
242 ip->direct = \\r
243 ip->traffic = \\r
244 ip->pktsup = \\r
245 ip->pktsdown = 0;\r
246 ip->keyword = keywords;\r
007c44c5 247 push(ip,ips);\r
248}\r
249\r
5b902402 250/* ====== iptables indexes are used to reduce complexity to log8(N) ===== */\r
007c44c5 251\r
252char *very_ugly_ipv4_code(char *inip,int bitmask,int format_as_chainname)\r
253{\r
254 /* warning: this function was debugged only for bitmask values 20,24,28 !!!*/\r
260c2719 255 int dot=0, n;\r
007c44c5 256 char *ip,*outip,*outptr,*fmt;\r
257\r
258 duplicate(inip,ip);\r
259 /* debug printf("(%s,%d) -> ",ip,bitmask); */\r
260\r
261 if(ip && *ip && bitmask>=0 && bitmask<=32)\r
bb9e11ee 262 {\r
007c44c5 263 string(outip,strlen(ip)+10); /*fuck unicode? assertion: 10>strlen("_%d_%d") */\r
bb9e11ee 264 }\r
007c44c5 265 else \r
bb9e11ee 266 {\r
007c44c5 267 /* should never exit here */\r
268 return "undefined";\r
bb9e11ee 269 }\r
007c44c5 270 outptr=outip;\r
271 while(ip && *ip)\r
272 {\r
273 if(*ip=='.')\r
274 {\r
275 if(dot<(bitmask/8-1)) \r
276 {\r
277 if(format_as_chainname)\r
bb9e11ee 278 {\r
007c44c5 279 *outptr='_';\r
bb9e11ee 280 }\r
007c44c5 281 else\r
bb9e11ee 282 {\r
007c44c5 283 *outptr='.';\r
bb9e11ee 284 }\r
007c44c5 285 outptr++;\r
286 dot++;\r
287 }\r
288 else\r
289 {\r
260c2719 290 char *cutdot=strchr(ip+1,'.'); /*for bitmask<24*/ \r
291 if(cutdot)\r
292 {\r
293 *cutdot = '\0';\r
294 }\r
295 \r
007c44c5 296 if(format_as_chainname)\r
260c2719 297 {\r
298 fmt = "_%d_%d";\r
299 }\r
007c44c5 300 else\r
260c2719 301 {\r
302 fmt = ".%d";\r
303 }\r
304 \r
007c44c5 305 if(bitmask%8)\r
260c2719 306 {\r
307 n = atoi(ip+1)-atoi(ip+1)%(1<<(8-bitmask%8));\r
308 }\r
007c44c5 309 else\r
260c2719 310 {\r
311 n = 0;\r
312 }\r
007c44c5 313\r
314 /*debug printf("%d/%d => [_%d_%d]\n",atoi(ip+1),bitmask,n,bitmask); */\r
315 sprintf(outptr,fmt,n,bitmask);\r
260c2719 316 if(!format_as_chainname)\r
007c44c5 317 {\r
260c2719 318 while(bitmask<24)\r
319 {\r
320 strcat(outip,".0");\r
321 bitmask+=8;\r
322 }\r
007c44c5 323 }\r
324 /* debug printf("[%s]\n",outip); */\r
325 return outip;\r
326 }\r
327 }\r
328 else \r
329 {\r
330 *outptr=*ip;\r
331 outptr++;\r
332 }\r
333 ip++;\r
334 }\r
335 /*should never exit here*/\r
336 *outptr='\0';\r
337 return outip;\r
338}\r
339\r
340char *hash_id(char *ip,int bitmask)\r
a1d21464 341{ \r
342 return very_ugly_ipv4_code(ip,bitmask,1);\r
343}\r
007c44c5 344\r
345char *subnet_id(char *ip,int bitmask)\r
a1d21464 346{\r
347 return very_ugly_ipv4_code(ip,bitmask,0);\r
348}\r
007c44c5 349\r
350/* ================= Let's parse configuration file here =================== */\r
351\r
352void reject_config_and_exit(char *filename)\r
353{\r
354 printf("Configuration file %s rejected - abnormal exit.",filename);\r
355 exit(-1);\r
356}\r
357\r
358void get_config(char *config_filename)\r
359{\r
360 char *cnf="mark";\r
361 \r
362 printf("Configured keywords: ");\r
363 parse(config_filename)\r
364 {\r
365 option("keyword",kwd);\r
366 if(kwd)\r
367 {\r
368 printf("%s ",kwd);\r
369\r
370 create(keyword,Keyword);\r
371 keyword->key=kwd;\r
372 keyword->asymetry_ratio=1; /* ratio for ADSL-like upload */\r
373 keyword->asymetry_fixed=0; /* fixed treshold for ADSL-like upload */\r
374 keyword->data_limit=8; /* hard shaping: apply magic_treshold if max*data_limit MB exceeded */\r
208112af 375 keyword->data_prio=4; /* soft shaping (qos): reduce HTB prio if max*data_prio MB exceeded */\r
007c44c5 376 keyword->fixed_limit=0; /* fixed data limit for setting lower HTB ceil */\r
377 keyword->fixed_prio=0; /* fixed data limit for setting lower HTB prio */\r
378 keyword->reserve_min=8; /* bonus for nominal HTB rate bandwidth (in kbps) */\r
379 keyword->reserve_max=0; /* malus for nominal HTB ceil (in kbps) */\r
208112af 380 keyword->default_prio=highest_priority+1;\r
007c44c5 381 keyword->html_color="000000";\r
382 keyword->ip_count=0;\r
383 keyword->leaf_discipline="";\r
384\r
385 push(keyword,keywords);\r
386 if(!defaultkeyword) defaultkeyword=keyword;\r
387 keywordcount++;\r
388 \r
389 kwd=NULL;\r
390 }\r
208112af 391 else\r
007c44c5 392 {\r
208112af 393 for_each(keyword,keywords)\r
394 {\r
395 int l=strlen(keyword->key);\r
007c44c5 396\r
208112af 397 if(!strncmp(keyword->key,_,l) && strlen(_)>l+2)\r
398 {\r
399 char *tmptr=_; /* <---- l+1 ----> */\r
400 _+=l+1; /* via-prometheus-asymetry-ratio, etc. */\r
401 ioption("asymetry-ratio",keyword->asymetry_ratio);\r
402 ioption("asymetry-treshold",keyword->asymetry_fixed);\r
403 ioption("magic-relative-limit",keyword->data_limit);\r
404 ioption("magic-relative-prio",keyword->data_prio);\r
405 loption("magic-fixed-limit",keyword->fixed_limit);\r
406 loption("magic-fixed-prio",keyword->fixed_prio);\r
407 ioption("htb-default-prio",keyword->default_prio);\r
408 ioption("htb-rate-bonus",keyword->reserve_min);\r
409 ioption("htb-ceil-malus",keyword->reserve_max);\r
208112af 410 option("leaf-discipline",keyword->leaf_discipline);\r
411 option("html-color",keyword->html_color);\r
412 _=tmptr;\r
413 \r
414 if(keyword->data_limit || keyword->fixed_limit || \r
415 keyword->data_prio || keyword->fixed_prio)\r
bb9e11ee 416 {\r
417 use_credit=1; \r
418 }\r
208112af 419 }\r
420 }\r
007c44c5 421 }\r
422\r
423 option("tc",tc);\r
424 option("iptables",iptables);\r
425 option("iptables-save",iptablessave); /* new */\r
426 option("iptables-restore",iptablesrestore); /* new */\r
427 option("iptables-file",iptablesfile); /* new */\r
428 option("hosts",hosts);\r
429 option("lan-interface",lan);\r
430 option("wan-interface",wan);\r
431 option("lan-medium",lan_medium);\r
432 option("wan-medium",wan_medium);\r
433 lloption("wan-download",line);\r
434 lloption("wan-upload",up);\r
435 ioption("hall-of-fame-enable",hall_of_fame);\r
436 option("hall-of-fame-title",title);\r
437 option("hall-of-fame-filename",html);\r
438 option("hall-of-fame-preview",preview);\r
439 option("log-filename",cmdlog);\r
440 option("credit-filename",credit);\r
441 ioption("credit-enable",enable_credit);\r
442 option("log-traffic-directory",log_dir);\r
5b902402 443 option("log-traffic-html-directory",html_log_dir);\r
007c44c5 444 option("log-traffic-url-path",log_url);\r
6cc38f96 445 option("jquery-url",jquery_url);\r
446 option("lms-url",lms_url);\r
447 ioption("use-jquery-popups",use_jquery_popups);\r
007c44c5 448 option("qos-free-zone",qos_free_zone);\r
449 ioption("qos-free-delay",qos_free_delay);\r
450 ioption("qos-proxy-enable",qos_proxy);\r
451 option("qos-proxy-ip",proxy_ip);\r
452 option("htb-leaf-discipline",qos_leaf);\r
453 ioption("qos-proxy-port",proxy_port);\r
454 ioption("free-rate",free_min);\r
455 ioption("free-ceil",free_max);\r
456 ioption("htb-burst",burst);\r
457 ioption("htb-burst-main",burst_main);\r
458 ioption("htb-burst-group",burst_group);\r
459 ioption("htb-nesting-limit",max_nesting);\r
460 ioption("htb-r2q",htb_r2q);\r
461 ioption("magic-include-upload",include_upload);\r
5b902402 462 ioption("magic-treshold",magic_treshold); \r
260c2719 463 option("filter-type", cnf); \r
007c44c5 464/* not yet implemented:\r
465 ioption("magic-fixed-packets",fixed_packets);\r
466 ioption("magic-relative-packets",packet_limit);\r
467*/\r
468 }\r
469 fail\r
470 { \r
471 perror(config_filename);\r
472 puts("Warning - using built-in defaults instead ...");\r
473 }\r
474 done;\r
475 printf("\n");\r
476 \r
477 /*leaf discipline for keywords*/\r
208112af 478 for_each(keyword,keywords)\r
007c44c5 479 {\r
260c2719 480 if(!strcmpi(keyword->leaf_discipline, ""))\r
208112af 481 {\r
007c44c5 482 keyword->leaf_discipline = qos_leaf;\r
483 }\r
484 }\r
485\r
260c2719 486 if(strcmpi(cnf, "mark"))\r
bb9e11ee 487 {\r
488 filter_type = 2;\r
489 mark = "CLASSIFY";\r
490 mark_iptables = "CLASSIFY --set-class 1:";\r
491 }\r
492 else\r
493 {\r
494 filter_type = 1;\r
495 mark = "MARK";\r
496 mark_iptables = "MARK --set-mark ";\r
007c44c5 497 }\r
498\r
499 /* are supplied values meaningful ?*/\r
500 if(line<=0 || up<=0)\r
501 {\r
b1b59b3a 502 puts("Illegal value of LAN or WAN bandwidth: 0 kbps.");\r
007c44c5 503 reject_config_and_exit(config_filename);\r
504 }\r
505}\r
506\r
507/* ===================== traffic analyser - uses iptables ================ */ \r
508\r
509void get_traffic_statistics(void)\r
510{\r
511 char *str,*cmd;\r
512 int downloadflag=0;\r
513\r
514 textfile(Pipe,str) *line,*lines=NULL;\r
515 string(str,STRLEN);\r
516 string(cmd,STRLEN);\r
517\r
518 sprintf(cmd,"%s -L -v -x -n -t mangle",iptables);\r
519 shell(cmd);\r
520 input(str,STRLEN)\r
521 {\r
522 create(line,Pipe);\r
523 line->str=str;\r
524 string(str,STRLEN);\r
525 append(line,lines);\r
526 }\r
527\r
208112af 528 for_each(line,lines)\r
007c44c5 529 {\r
530 int col, accept=0,proxyflag=0,valid=1,setchainname=0,commonflag=0; \r
531 unsigned long long traffic=0;\r
532 unsigned long pkts=0;\r
533 char *ipaddr=NULL,*ptr;\r
534 \r
535 /* debug puts(line->str); */\r
536 valid_columns(ptr,line->str,' ',col) \r
537 if(valid) switch(col)\r
538 { \r
539 case 1: if(eq(ptr,"Chain"))\r
260c2719 540 {\r
007c44c5 541 setchainname=1;\r
260c2719 542 }\r
007c44c5 543 else if(eq(ptr,"pkts")) \r
260c2719 544 {\r
007c44c5 545 valid=0;\r
260c2719 546 }\r
007c44c5 547 else\r
260c2719 548 {\r
007c44c5 549 sscanf(ptr,"%lu",&pkts); \r
260c2719 550 }\r
007c44c5 551 break;\r
552 case 2: if(setchainname)\r
553 {\r
554 if(!strncmp(ptr,"post_",5) || eq(ptr,"POSTROUTING"))\r
260c2719 555 {\r
556 downloadflag = 1; \r
557 }\r
007c44c5 558 else \r
260c2719 559 {\r
560 if(!strncmp(ptr,"forw_",5) || eq(ptr,"FORWARD"))\r
561 {\r
562 downloadflag = 0;\r
563 }\r
564 } \r
007c44c5 565 if(eq(ptr,"post_common") || eq(ptr,"forw_common"))\r
260c2719 566 {\r
567 commonflag = 1;\r
568 }\r
007c44c5 569 }\r
570 else\r
260c2719 571 {\r
572 sscanf(ptr,"%Lu",&traffic); \r
573 traffic += (1<<19);\r
574 traffic >>= 20;\r
575 }\r
007c44c5 576 break;\r
0045483c 577 case 3: if((strncmp(ptr,"post_",5) && strncmp(ptr,"forw_",5)) || commonflag)\r
260c2719 578 {\r
007c44c5 579 accept=eq(ptr,mark);\r
260c2719 580 }\r
581 /*if(filter_type==1) accept=eq(ptr,"MARK"); else accept=eq(ptr,"CLASSIFY");*/\r
007c44c5 582 break;\r
583 case 8: if(downloadflag)\r
584 { \r
260c2719 585 if(strstr(proxy_ip,ptr))\r
586 {\r
587 proxyflag=1; \r
588 }\r
007c44c5 589 }\r
590 else\r
260c2719 591 {\r
007c44c5 592 ipaddr=ptr; \r
260c2719 593 }\r
594 break;\r
007c44c5 595 case 9: if(downloadflag)ipaddr=ptr;break;\r
596 }\r
597 \r
598 if(accept && traffic>0 && ipaddr)\r
599 {\r
a1d21464 600 if(proxyflag)\r
601 {\r
602 printf("(proxy) ");\r
603 }\r
604 else if(!downloadflag)\r
605 {\r
606 printf("(upload) ");\r
607 }\r
007c44c5 608 printf("IP %s: %Lu M (%ld pkts)\n", ipaddr, traffic, pkts);\r
208112af 609\r
610 if_exists(ip,ips,eq(ip->addr,ipaddr)); \r
007c44c5 611 else \r
612 {\r
613 TheIP();\r
614 ip->addr=ipaddr;\r
615 if(eq(ip->addr,"0.0.0.0/0"))\r
616 {\r
617 ip->name="(unregistered)";\r
618 ip->min=free_min;\r
619 ip->max=ip->desired=free_max;\r
620 }\r
621 }\r
622 \r
623 if(downloadflag)\r
624 {\r
625 if(proxyflag)\r
a1d21464 626 {\r
007c44c5 627 ip->proxy=traffic;\r
a1d21464 628 }\r
007c44c5 629 else\r
a1d21464 630 {\r
007c44c5 631 ip->traffic+=traffic;\r
a1d21464 632 }\r
007c44c5 633 ip->direct=ip->traffic-ip->upload-ip->proxy;\r
634 ip->pktsdown=pkts;\r
635 }\r
636 else\r
637 {\r
638 ip->upload=traffic;\r
639 ip->pktsup=pkts;\r
640 if(include_upload)\r
bb9e11ee 641 {\r
007c44c5 642 ip->traffic+=traffic;\r
bb9e11ee 643 }\r
007c44c5 644 else \r
bb9e11ee 645 {\r
007c44c5 646 if(traffic>ip->traffic)\r
bb9e11ee 647 {\r
007c44c5 648 ip->traffic=traffic; \r
bb9e11ee 649 }\r
650 }\r
007c44c5 651 }\r
652 } \r
653 }\r
654\r
007c44c5 655 free(cmd);\r
656}\r
657 \r
658/* ========== This function executes, logs OR ALSO prints command ========== */\r
659\r
660void safe_run(char *cmd)\r
661{\r
a1d21464 662 if(dry_run)\r
663 {\r
664 printf("\n=>%s\n",cmd);\r
665 }\r
666 else\r
667 {\r
668 system(cmd);\r
669 }\r
670 if(log_file)\r
671 {\r
672 fprintf(log_file,"%s\n",cmd);\r
673 }\r
007c44c5 674}\r
675\r
676void save_line(char *line)\r
677{\r
678 fprintf(iptables_file,"%s\n",line);\r
679}\r
680\r
681void run_restore(void)\r
682{\r
5da44508 683 char *restor;\r
007c44c5 684 string(restor,STRLEN);\r
abe9b855 685\r
686 /*-----------------------------------------------------------------*/\r
687 printf("Running %s <%s ...\n",iptablesrestore,iptablesfile);\r
688 /*-----------------------------------------------------------------*/\r
007c44c5 689 \r
690 save_line("COMMIT");\r
691 fclose(iptables_file);\r
ae776b10 692 if(dry_run) \r
693 {\r
a1d21464 694 parse(iptablesfile)\r
695 {\r
696 printf("%s\n",_);\r
697 }\r
698 done;\r
ae776b10 699 }\r
700\r
701 sprintf(restor,"%s <%s",iptablesrestore, iptablesfile);\r
702 safe_run(restor);\r
007c44c5 703 \r
704 free(restor);\r
705}\r
706\r
707/* == This function strips extra characters after IP address and stores it = */\r
708\r
0eb8bc04 709void parse_ip(char *str)\r
007c44c5 710{\r
4358455e 711 char *ptr,*ipaddr=NULL,*ipname=NULL,*lmsid=NULL;\r
712\r
713 ptr=strchr(str,'{');\r
714 if(ptr)\r
715 {\r
716 lmsid=++ptr;\r
717 while(*ptr && *ptr!='}')\r
a1d21464 718 {\r
4358455e 719 ptr++;\r
a1d21464 720 }\r
4358455e 721 *ptr=0;\r
722 }\r
007c44c5 723 \r
4358455e 724 ptr=str;\r
007c44c5 725 while(*ptr && *ptr!=' ' && *ptr!=9)\r
a1d21464 726 {\r
007c44c5 727 ptr++;\r
a1d21464 728 }\r
007c44c5 729 \r
730 *ptr=0;\r
731 ipaddr=str;\r
732 ptr++;\r
733 while(*ptr && (*ptr==' ' || *ptr==9))\r
a1d21464 734 {\r
007c44c5 735 ptr++;\r
a1d21464 736 }\r
007c44c5 737 ipname=ptr; \r
738 while(*ptr && *ptr!=' ' && *ptr!=9)\r
a1d21464 739 {\r
007c44c5 740 ptr++;\r
a1d21464 741 }\r
007c44c5 742 *ptr=0;\r
743\r
208112af 744 if_exists(ip,ips,eq(ip->addr,ipaddr));\r
745 else\r
746 {\r
747 TheIP();\r
748 }\r
007c44c5 749 ip->addr=ipaddr;\r
750 ip->name=ipname;\r
4358455e 751 if(lmsid)\r
752 {\r
753 ip->lmsid=atoi(lmsid);\r
754 found_lmsid=1;\r
755 }\r
007c44c5 756}\r
757\r
758char *parse_datafile_line(char *str)\r
759{\r
760 char *ptr=strchr(str,' ');\r
761\r
762 if(ptr)\r
763 {\r
764 *ptr=0;\r
765 ptr++;\r
766 return ptr;\r
767 } \r
768 else \r
4358455e 769 {\r
007c44c5 770 return NULL;\r
4358455e 771 }\r
007c44c5 772}\r
773\r
c9012978 774struct IpLog\r
775{\r
776 char *name;\r
777 long traffic;\r
5a8f2e77 778 long guaranted;\r
208112af 779 int i;\r
a1d21464 780 int lmsid;\r
208112af 781 long l;\r
c9012978 782 list(IpLog);\r
783} *iplog,*iplogs;\r
784\r
785void parse_ip_log(int argc, char **argv) \r
786{\r
208112af 787 char *month, *year, *str, *name="(undefined)", *ptr, *ptr2, *filename;\r
788 long traffic=0l, traffic_month, total=0, guaranted;\r
4358455e 789 int col, col2, y_ok, m_ok, accept_month, i=1, any_month=0, lmsid;\r
5a8f2e77 790 char mstr[4], ystr[5];\r
791 FILE *f; \r
c9012978 792 string(str,STRLEN);\r
9da6a279 793 string(filename,STRLEN);\r
c9012978 794\r
5b902402 795 if(argv[1][1]=='l') /* -l */\r
796 {\r
208112af 797 if(argc<4)\r
798 {\r
799 puts("Missing parameter(s)!\nUsage: prometheus -l Mmm YYYY (Mmm=Jan-Dec or Year, YYYY=year)");\r
800 exit(-1);\r
801 }\r
802 else\r
803 {\r
804 month=argv[2];\r
805 if(eq(month,"Year")) any_month=1;\r
806 year=argv[3];\r
807 }\r
5b902402 808 }\r
809 else\r
810 { \r
208112af 811 time_t t = time(NULL) - 3600*24 ; /* yesterday's timestamp*/\r
812 struct tm *timep = localtime(&t); \r
813\r
814 if(argv[1][1]=='m') /* -m yestarday - month */\r
815 {\r
816 strftime(mstr, 4, "%b", timep);\r
817 month=mstr;\r
818 strftime(ystr, 5, "%Y", timep);\r
819 year=ystr; \r
820 }\r
821 else /* -y yesterday - year */\r
822 {\r
823 month="Year";\r
824 any_month=1;\r
825 strftime(ystr, 5, "%Y", timep);\r
826 year=ystr;\r
827 }\r
5b902402 828 }\r
829 printf("Analysing traffic for %s %s ...\n",month,year);\r
c9012978 830\r
493e1ccd 831 /* sorry... next release of C<<1 header file will include for_path_files(name,path) { } macro */\r
832 sprintf(str,"%s %s/",ls,log_dir);\r
c9012978 833 shell(str);\r
493e1ccd 834 input(str,STRLEN) \r
c9012978 835 {\r
493e1ccd 836 if(strstr(str,".log"))\r
9da6a279 837 {\r
493e1ccd 838 ptr=strrchr(str,'\n');\r
839 if(ptr) *ptr='\0';\r
9da6a279 840 sprintf(filename,"%s/%s",log_dir,str);\r
841 printf("Parsing %s ...",filename);\r
493e1ccd 842 accept_month=0;\r
843 traffic_month=0;\r
4358455e 844 guaranted=0;\r
a1d21464 845 lmsid=-1;\r
9da6a279 846 parse(filename)\r
493e1ccd 847 {\r
848 y_ok=m_ok=0; \r
849 valid_columns(ptr,_,'\t',col) switch(col)\r
850 {\r
851 case 2: name = ptr;break;\r
852 case 3: traffic = atol(ptr);break;\r
4358455e 853 /* column number - was 7, now 11...*/\r
493e1ccd 854 case 7:\r
855 case 8:\r
856 case 9:\r
4358455e 857 case 10:\r
260c2719 858 case 11: if(isalpha(*ptr)) /* character, not numeric string = date, just one*/\r
5a8f2e77 859 {\r
208112af 860 valid_columns(ptr2,ptr,' ',col2) switch(col2)\r
861 {\r
862 case 2: if(any_month || eq(ptr2,month)) m_ok = 1; break;\r
863 case 5: if(eq(ptr2,year)) y_ok = 1; break;\r
864 }\r
5a8f2e77 865 }\r
493e1ccd 866 else\r
867 {\r
868 if(col == 7) guaranted = atol(ptr);\r
4358455e 869 if(col == 10) lmsid = atoi(ptr);\r
493e1ccd 870 }\r
871 }\r
872 \r
873 if(y_ok && m_ok) \r
874 {\r
875 traffic_month += traffic;\r
876 accept_month = 1;\r
877 }\r
878 }\r
879 done;\r
b1b59b3a 880\r
493e1ccd 881 if(accept_month)\r
882 {\r
883 create(iplog,IpLog);\r
884 iplog->name = name;\r
885 iplog->guaranted = guaranted;\r
886 iplog->traffic = traffic_month;\r
a1d21464 887 iplog->lmsid = lmsid;\r
493e1ccd 888 insert(iplog,iplogs,desc_order_by,traffic);\r
889 printf(" %ld MB\n",iplog->traffic);\r
890 }\r
891 else\r
208112af 892 {\r
493e1ccd 893 puts(" no records.");\r
208112af 894 }\r
9da6a279 895 }\r
c9012978 896 }\r
897 sprintf(str,"%s/%s-%s.html",html_log_dir,year,month);\r
add90548 898 printf("Writing %s ... ",str);\r
c9012978 899 f=fopen(str,"w");\r
900 if(f)\r
901 {\r
103d292c 902 fprintf(f, "<table class=\"decorated last\"><thead>\n\\r
903<tr><th colspan=\"2\">%s %s</th>\n\\r
904<th style=\"text-align: right\">lms</th>\n\\r
905<th colspan=\"2\">Data transfers</th>\n\\r
906<th style=\"text-align: right\">Min.speed</th>\n\\r
907</tr></thead><tbody>\n ",\r
6cc38f96 908 month, year);\r
208112af 909\r
9aa195f6 910 row_odd_even = 0;\r
6cc38f96 911 for_each(iplog, iplogs)\r
208112af 912 {\r
c9012978 913 if(iplog->traffic)\r
914 {\r
103d292c 915 fprintf(f, "%s<td style=\"text-align: right\">%d</td>\n\\r
916<td style=\"text-align: left\"><a class=\"blue\" href=\"%s%s.log\">%s</td>\n\\r
917<td style=\"text-align: right\">", \r
9aa195f6 918 tr_odd_even(), i++, log_url, iplog->name, iplog->name); \r
ad4cb129 919 if(iplog->lmsid > 0)\r
920 {\r
921 /*base URL will be configurable soon ... */\r
103d292c 922 fprintf(f, "<a class=\"blue\" href=\"%s%d\">%04d</a>\n", lms_url, iplog->lmsid, iplog->lmsid);\r
ad4cb129 923 }\r
924 else if(iplog->lmsid == 0)\r
925 {\r
926 fputs("-------",f);\r
927 } \r
3ece6581 928 fprintf(f, "<td style=\"text-align: right\">%ld&nbsp;MB</td>\n\\r
929 <td style=\"text-align: right\"><strong>%ld&nbsp;GB</strong></td>\n\\r
930 <td style=\"text-align: right\">%ld&nbsp;kb/s</th></tr>\n",\r
6cc38f96 931 iplog->traffic, iplog->traffic>>10, iplog->guaranted);\r
c9012978 932 total+=iplog->traffic>>10;\r
208112af 933 iplog->i=i;\r
934 iplog->l=total;\r
c9012978 935 }\r
208112af 936 }\r
103d292c 937 fprintf(f,"<tr>\\r
938 <td colspan=\"4\" style=\"text-align: left\">Total:</td>\\r
3ece6581 939 <td style=\"text-align: right\"><strong>%ld&nbsp;GB</strong></td>\\r
940 <td style=\"text-align: right\"><strong>%Ld&nbsp;kb/s</strong></td></tr>\n", total, line);\r
9aa195f6 941 fputs("</tbody></table>\n", f);\r
208112af 942\r
9aa195f6 943 row_odd_even = 0;\r
208112af 944 if(i>10)\r
945 {\r
3ece6581 946 fputs("<a name=\"erp\"></a><p><table class=\"decorated last\">\n\\r
947<caption>Enterprise Resource Planning (ERP)</caption>\n\\r
948<thead><tr>\n\\r
949<th>Analytic category</th>\n\\r
950<th colspan=\"2\" style=\"text-align: center\">Active Classes</th>\n\\r
951<th colspan=\"2\" style=\"text-align: center\">Data transfers</th>\n\\r
952</tr></thead><tbody>\n",f);\r
208112af 953\r
954 if_exists(iplog,iplogs,iplog->l>=total/4)\r
955 {\r
9aa195f6 956 fprintf(f,"%s<td>Top 25%% of traffic</td>\n", tr_odd_even());\r
103d292c 957 fprintf(f,"<td style=\"text-align: right\">%d</td><td style=\"text-align: right\">%d %%</td><td style=\"text-align: right\">%ld G</td><td style=\"text-align: right\">%d %%</td></tr>\n",iplog->i,(100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total));\r
208112af 958 }\r
959 \r
960 if_exists(iplog,iplogs,iplog->i==10)\r
961 {\r
9aa195f6 962 fprintf(f,"%s<td>Top 10 downloaders</td>\n", tr_odd_even());\r
3ece6581 963 fprintf(f,"<td style=\"text-align: right\"><strong>10</strong></td>\n\\r
964<td style=\"text-align: right\">%d %%</td>\n\\r
965<td style=\"text-align: right\">%ld G</td>\n\\r
966<td style=\"text-align: right\">%d %%</td></tr>\n",\r
967 (100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total));\r
208112af 968 }\r
969\r
970 if_exists(iplog,iplogs,iplog->l>=total/2)\r
971 {\r
9aa195f6 972 fprintf(f,"%s<td>Top 50%% of traffic</td>\n", tr_odd_even());\r
3ece6581 973 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
974<td style=\"text-align: right\">%d %%</td>\n\\r
975<td style=\"text-align: right\">%ld G</td>\n\\r
976<td style=\"text-align: right\"><strong>%d %%</strong></td></tr>\n",\r
977 iplog->i,(100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total));\r
208112af 978 }\r
979\r
980 if_exists(iplog,iplogs,iplog->l>=4*total/5)\r
981 {\r
9aa195f6 982 fprintf(f,"%s<td>Top 80%% of traffic</td>\n",tr_odd_even());\r
3ece6581 983 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
984<td style=\"text-align: right\">%d %%</td>\n\\r
985<td style=\"text-align: right\">%ld G</td>\n\\r
986<td style=\"text-align: right\"><strong>%d %%</strong></td></tr>\n",\r
987 iplog->i,(100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total));\r
208112af 988 }\r
989\r
990 if_exists (iplog,iplogs,iplog->i>=i/5)\r
991 {\r
9aa195f6 992 fprintf(f,"%s<td>Top 20%% downloaders</td>\n",tr_odd_even());\r
3ece6581 993 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
994<td style=\"text-align: right\"><strong>%d %%</strong></td>\n\\r
995<td style=\"text-align: right\">%ld G</td>\n\\r
996<td style=\"text-align: right\">%d %%</td></tr>\n",\r
997 iplog->i,(100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total));\r
208112af 998 }\r
999\r
1000 if_exists(iplog,iplogs,iplog->i>=i/4)\r
1001 {\r
9aa195f6 1002 fprintf(f,"%s<td>Top 25%% downloaders</td>\n", tr_odd_even());\r
103d292c 1003 fprintf(f,"<td style=\"text-align: right\">%d</td><td style=\"text-align: right\">%d %%</td><td style=\"text-align: right\">%ld G</td><td style=\"text-align: right\">%d %%</td></tr>\n",iplog->i,(100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total));\r
208112af 1004 }\r
1005\r
1006 if_exists(iplog,iplogs,iplog->i>=i/2)\r
1007 {\r
9aa195f6 1008 fprintf(f,"%s<td>Top 50%% downloaders</td>\n",tr_odd_even());\r
3ece6581 1009 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
1010<td style=\"text-align: right\"><strong>%d %%</strong></td>\n\\r
1011<td style=\"text-align: right\">%ld G</td><td style=\"text-align: right\">%d %%</td></tr>\n",iplog->i,(100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total));\r
208112af 1012 }\r
1013\r
1014 if_exists(iplog,iplogs,iplog->i>=4*i/5)\r
1015 {\r
9aa195f6 1016 fprintf(f,"%s<td>Top 80%% downloaders</td>\n",tr_odd_even());\r
103d292c 1017 fprintf(f,"<td style=\"text-align: right\">%d</td><td style=\"text-align: right\">%d %%</td><td style=\"text-align: right\">%ld G</td><td style=\"text-align: right\">%d %%</td></tr>\n",iplog->i,(100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total));\r
208112af 1018 }\r
1019\r
3ece6581 1020 fprintf(f,"</tbody><thead><tr>\n\\r
1021<th>All users, all traffic</th>\n\\r
1022<th style=\"text-align: right\">%d</th>\n\\r
1023<th style=\"text-align: right\">100 %%</th>\n\\r
1024<th style=\"text-align: right\">%ld G</th>\n\\r
1025<th style=\"text-align: right\">100 %%</th></tr>\n",i-1,total);\r
1026 fputs("</thead></table>\n", f);\r
208112af 1027 }\r
1028\r
2d114137 1029 fprintf(f, stats_html_signature, version);\r
c9012978 1030 fclose(f);\r
1031 puts(" done.");\r
1032 }\r
add90548 1033 else\r
1034 {\r
1035 perror(str);\r
1036 }\r
1037}\r
1038\r
1039void append_log(struct IP *self) /*using global variables*/\r
1040{\r
1041 char *d, *str;\r
1042 FILE *f; \r
1043\r
1044 date(d); /* this is typical cll1.h macro - prints current date */ \r
1045 string(str,STRLEN); \r
1046 sprintf(str,"%s/%s.log", log_dir, self->name);\r
1047 f=fopen(str,"a");\r
1048 if(f)\r
1049 {\r
1050 fprintf(f,"%ld\t%s\t%Lu\t%Lu\t%Lu\t%Lu\t%d\t%d\t%d\t%d\t%s",\r
1051 time(NULL), self->name, self->traffic, self->direct, self->proxy,\r
1052 self->upload, self->min, self->max, self->desired, self->lmsid, d); /* d = date*/\r
1053 fclose(f);\r
1054 }\r
1055 else\r
1056 {\r
1057 perror(str);\r
1058 }\r
c9012978 1059}\r
5b902402 1060\r
add90548 1061\r
007c44c5 1062/*-----------------------------------------------------------------*/\r
493e1ccd 1063/* Are you looking for int main(int argc, char **argv) ? :-)) */\r
007c44c5 1064/*-----------------------------------------------------------------*/\r
1065\r
1066program\r
1067{\r
1068 int i=0;\r
1069 FILE *f=NULL;\r
1070 char *str, *ptr, *d;\r
1071 char *substring;\r
1072 int class_count=0,ip_count=0;\r
1073 int parent=1;\r
208112af 1074 int just_flush=FALSE;\r
1075 int nodelay=FALSE;\r
1076 int just_preview=FALSE; /* preview - generate just stats */\r
1077 int just_logs=FALSE; /* just parse logs */\r
1078 int run=FALSE;\r
b014f181 1079 int total=0;\r
33ec95ab 1080 \r
007c44c5 1081 char *chain_forward, *chain_postrouting;\r
1082 char *althosts=NULL;\r
1083 \r
1084 printf("\n\\r
1085Prometheus QoS - \"fair-per-IP\" Quality of Service setup utility.\n\\r
260c2719 1086Version %s - Copyright (C)2005-2012 Michael Polak (xChaos)\n\\r
43cde5c3 1087iptables-restore & burst tunning & classify modification by Ludva\n\\r
0d5026c6 1088Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);\r
007c44c5 1089\r
1090 /*----- Boring... we have to check command line options first: ----*/\r
1091 \r
1092 arguments\r
1093 {\r
1094 argument("-c") { nextargument(config); }\r
1095 argument("-h") { nextargument(althosts);}\r
208112af 1096 argument("-d") { run=TRUE; dry_run=TRUE; }\r
1097 argument("-f") { run=TRUE; just_flush=TRUE; }\r
1098 argument("-9") { run=TRUE; just_flush=9; }\r
1099 argument("-p") { run=TRUE; just_preview=TRUE; }\r
1100 argument("-r") { run=TRUE; }\r
1101 argument("-n") { run=TRUE; nodelay=TRUE; }\r
1102 argument("-l") { just_logs=TRUE; }\r
1103 argument("-m") { just_logs=TRUE; }\r
1104 argument("-y") { just_logs=TRUE; }\r
007c44c5 1105 argument("-?") { help(); exit(0); }\r
1106 argument("--help") { help(); exit(0); }\r
1107 argument("-v") { exit(0); } \r
1108 argument("--version") { exit(0); } \r
1109 }\r
208112af 1110 \r
007c44c5 1111 if(dry_run)\r
208112af 1112 {\r
007c44c5 1113 puts("*** THIS IS JUST DRY RUN ! ***\n");\r
208112af 1114 }\r
007c44c5 1115\r
208112af 1116 date(d); /* this is typical cll1.h macro - prints current date */\r
007c44c5 1117\r
1118 /*-----------------------------------------------------------------*/\r
1119 printf("Parsing configuration file %s ...\n", config);\r
1120 /*-----------------------------------------------------------------*/\r
1121 get_config(config);\r
c9012978 1122 \r
33ec95ab 1123 if(just_logs)\r
c9012978 1124 {\r
208112af 1125 parse_ip_log(argc,argv);\r
1126 exit(0);\r
1127 }\r
1128 else if(not run)\r
1129 {\r
1130 help();\r
1131 exit(0);\r
c9012978 1132 }\r
007c44c5 1133\r
208112af 1134 if(althosts)\r
1135 {\r
1136 hosts=althosts;\r
1137 }\r
007c44c5 1138\r
1139 if(just_flush<9)\r
1140 {\r
1141 /*-----------------------------------------------------------------*/\r
1142 puts("Parsing iptables verbose output ...");\r
1143 /*-----------------------------------------------------------------*/\r
1144 get_traffic_statistics();\r
1145 }\r
1146\r
1147 /*-----------------------------------------------------------------*/\r
1148 printf("Parsing class defintion file %s ...\n", hosts);\r
1149 /*-----------------------------------------------------------------*/\r
1150 int groupidx = FIRSTGROUPID;\r
1151 parse(hosts)\r
1152 {\r
1153 str=_;\r
1154\r
1155 if(*str<'0' || *str>'9')\r
4358455e 1156 {\r
1157 /* any line starting with non-number is comment ...*/\r
007c44c5 1158 continue;\r
4358455e 1159 }\r
007c44c5 1160 \r
1161 //Does this IP share QoS class with some other ?\r
1162 substring=strstr(str,"sharing-");\r
1163 if(substring)\r
1164 { \r
1165 substring+=8; //"sharing-"\r
0eb8bc04 1166 parse_ip(str);\r
007c44c5 1167 ip_count++;\r
1168 ip->sharing=substring;\r
1169 ip->keyword=defaultkeyword; /* settings for default keyword */\r
1170 while(*substring && *substring!='\n')\r
4358455e 1171 {\r
007c44c5 1172 substring++;\r
4358455e 1173 }\r
007c44c5 1174 *substring=0; \r
1175 }\r
1176 else\r
1177 {\r
1178 //Do we have to create new QoS class for this IP ?\r
1179\r
208112af 1180 if_exists(keyword,keywords,(substring=strstr(str,keyword->key)))\r
007c44c5 1181 {\r
0eb8bc04 1182 parse_ip(str);\r
007c44c5 1183 ip_count++;\r
1184 ip->keyword=keyword;\r
1185 keyword->ip_count++;\r
1186 ip->prio=keyword->default_prio;\r
1187 substring+=strlen(keyword->key)+1;\r
1188 ptr=substring;\r
1189 while(*ptr && *ptr!='-')\r
bb9e11ee 1190 {\r
007c44c5 1191 ptr++;\r
bb9e11ee 1192 }\r
007c44c5 1193 if(*ptr=='-')\r
1194 {\r
1195 *ptr=0;\r
260c2719 1196 ip->max = ip->desired=atoi(ptr+1);\r
007c44c5 1197 }\r
260c2719 1198 ip->min = atoi(substring);\r
1199 if(ip->min <= 0)\r
007c44c5 1200 {\r
3ece6581 1201 printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kb/s\n",\r
260c2719 1202 str, free_min);\r
1203 ip->min = free_min;\r
007c44c5 1204 }\r
260c2719 1205 if(ip->max <= ip->min)\r
007c44c5 1206 {\r
260c2719 1207 ip->fixedprio = 1;\r
1208 ip->max = ip->min+ip->keyword->reserve_min;\r
007c44c5 1209 }\r
1210 else \r
1211 {\r
260c2719 1212 ip->max -= ip->keyword->reserve_max;\r
007c44c5 1213 if(ip->max<ip->min)\r
bb9e11ee 1214 {\r
007c44c5 1215 ip->max=ip->min;\r
bb9e11ee 1216 }\r
007c44c5 1217 }\r
43e07280 1218 ip->mark=FIRSTIPCLASS+1+class_count++;\r
007c44c5 1219\r
208112af 1220 if_exists(group,groups,group->min==ip->min) \r
007c44c5 1221 { \r
1222 group->count++; \r
260c2719 1223 group->desired += ip->min;\r
007c44c5 1224 ip->group = group->id; \r
1225 }\r
1226 else\r
1227 {\r
1228 create(group,Group);\r
260c2719 1229 group->min = ip->min;\r
007c44c5 1230 group->id = groupidx++;\r
1231 ip->group = group->id;\r
1232\r
1233 if(group->min<8) group->min=8;\r
1234 /* Warning - this is maybe because of primitive tc namespace, can be fixed */\r
1235 /* it is because class IDs are derived from min. bandwidth. - xCh */\r
1236 //if(group->min>MAX_GUARANTED_KBPS) group->min=MAX_GUARANTED_KBPS;\r
1237 \r
1238 group->count=1;\r
1239 group->desired=ip->min; \r
1240 insert(group,groups,desc_order_by,min);\r
1241 }\r
1242 }//endif keyword-\r
1243 }//endif sharing-\r
1244 }\r
1245 fail\r
1246 {\r
1247 perror(hosts);\r
1248 exit(-1);\r
1249 }\r
1250 done;\r
1251\r
1252 /*-----------------------------------------------------------------*/\r
1253 /* cll1.h - let's allocate brand new character buffer... */\r
1254 /*-----------------------------------------------------------------*/\r
1255 string(str,STRLEN); \r
1256\r
1257 /*-----------------------------------------------------------------*/\r
1258 puts("Resolving shared connections ...");\r
1259 /*-----------------------------------------------------------------*/\r
6cc38f96 1260 for_each(ip,ips) if(ip->sharing)\r
007c44c5 1261 {\r
6cc38f96 1262 for_each(sharedip,ips) if(eq(sharedip->name,ip->sharing))\r
007c44c5 1263 {\r
1264 sharedip->traffic+=ip->traffic;\r
1265 ip->traffic=0;\r
1266 ip->mark=sharedip->mark; \r
b3175d62 1267 ip->lmsid=sharedip->lmsid;\r
007c44c5 1268 break;\r
1269 }\r
1270 if(!sharedip)\r
bb9e11ee 1271 {\r
260c2719 1272 printf("Unresolved shared connection: %s %s sharing-%s\n",\r
1273 ip->addr, ip->name, ip->sharing);\r
bb9e11ee 1274 }\r
007c44c5 1275 }\r
1276\r
1277 if(enable_credit && just_flush<9)\r
1278 {\r
1279 /*-----------------------------------------------------------------*/\r
1280 printf("Parsing credit file %s ...\n", credit);\r
1281 /*-----------------------------------------------------------------*/\r
1282 parse(credit)\r
1283 {\r
1284 ptr=parse_datafile_line(_);\r
1285 if(ptr)\r
1286 {\r
208112af 1287 if_exists(ip,ips,eq(ip->addr,_))\r
1288 {\r
007c44c5 1289 sscanf(ptr,"%Lu",&(ip->credit));\r
208112af 1290 }\r
007c44c5 1291 }\r
1292 }\r
1293 done;\r
1294 }\r
1295\r
1296 if(!just_preview)\r
1297 {\r
1298 /*-----------------------------------------------------------------*/\r
1299 puts("Initializing iptables and tc classes ...");\r
1300 /*-----------------------------------------------------------------*/\r
1301 \r
1302 iptables_file=fopen(iptablesfile,"w");\r
260c2719 1303 if(iptables_file == NULL)\r
bb9e11ee 1304 {\r
007c44c5 1305 puts("Cannot open iptablesfile!");\r
1306 exit(-1);\r
1307 }\r
1308 \r
1309 log_file=fopen(cmdlog,"w");\r
260c2719 1310 if(log_file == NULL) \r
bb9e11ee 1311 {\r
007c44c5 1312 puts("Cannot open logfile!");\r
1313 exit(-1);\r
1314 }\r
1315 \r
1316 save_line(iptablespreamble);\r
1317 run_restore();\r
1318 \r
1319 sprintf(str,"%s qdisc del dev %s root 2>/dev/null",tc,lan);\r
1320 safe_run(str);\r
1321\r
1322 sprintf(str,"%s qdisc del dev %s root 2>/dev/null",tc,wan);\r
1323 safe_run(str);\r
1324 \r
1325 iptables_file=fopen(iptablesfile,"w");\r
1326 save_line(iptablespreamble);\r
1327\r
1328 if(qos_free_zone && *qos_free_zone!='0')\r
1329 {\r
1330 char *chain;\r
1331 \r
1332 sprintf(str,"-A FORWARD -d %s -o %s -j ACCEPT", qos_free_zone, wan);\r
1333 save_line(str);\r
1334 \r
1335 if(qos_proxy)\r
1336 {\r
1337 save_line(":post_noproxy - [0:0]");\r
1338 sprintf(str,"-A POSTROUTING -p ! tcp -o %s -j post_noproxy", lan);\r
1339 save_line(str); \r
1340 sprintf(str,"-A POSTROUTING -s ! %s -o %s -j post_noproxy", proxy_ip, lan);\r
1341 save_line(str); \r
1342 sprintf(str,"-A POSTROUTING -s %s -p tcp --sport ! %d -o %s -j post_noproxy", proxy_ip, proxy_port, lan);\r
1343 save_line(str); \r
1344\r
1345 chain="post_noproxy"; \r
1346 }\r
1347 else\r
bb9e11ee 1348 {\r
007c44c5 1349 chain="POSTROUTING";\r
bb9e11ee 1350 }\r
007c44c5 1351 \r
1352 sprintf(str,"-A %s -s %s -o %s -j ACCEPT", chain, qos_free_zone, lan);\r
1353 save_line(str);\r
1354 }\r
1355 \r
1356 if(ip_count>idxtable_treshold1 && !just_flush)\r
1357 {\r
1358 int idxcount=0, bitmask=32-idxtable_bitmask1; /* default net mask: 255.255.255.240 */\r
e0161edb 1359 char *subnet, *buf;\r
007c44c5 1360 /*-----------------------------------------------------------------*/\r
1361 printf("Detected %d addresses - indexing iptables rules to improve performance...\n",ip_count);\r
1362 /*-----------------------------------------------------------------*/\r
1363\r
1364 save_line(":post_common - [0:0]");\r
1365 save_line(":forw_common - [0:0]");\r
1366\r
260c2719 1367 for_each(ip,ips) if(ip->addr && *(ip->addr) && !eq(ip->addr,"0.0.0.0/0"))\r
007c44c5 1368 {\r
1369 buf=hash_id(ip->addr,bitmask);\r
208112af 1370 if_exists(idx,idxs,eq(idx->id,buf))\r
1371 {\r
007c44c5 1372 idx->children++;\r
208112af 1373 }\r
007c44c5 1374 else\r
1375 {\r
1376 create(idx,Index);\r
1377 idx->addr=ip->addr;\r
1378 idx->id=buf;\r
1379 idx->bitmask=bitmask;\r
1380 idx->parent=NULL;\r
1381 idx->children=0;\r
1382 idxcount++;\r
1383 push(idx,idxs);\r
1384 }\r
1385 }\r
1386\r
1387 /* brutal perfomance optimalization */\r
1388 while(idxcount>idxtable_treshold2 && bitmask>2*idxtable_bitmask2)\r
1389 {\r
1390 bitmask-=idxtable_bitmask2;\r
1391 idxcount=0;\r
208112af 1392\r
6cc38f96 1393 for_each(idx,idxs) if(idx->parent == NULL)\r
007c44c5 1394 {\r
1395 buf=hash_id(idx->addr,bitmask);\r
208112af 1396 if_exists(metaindex,idxs,eq(metaindex->id,buf))\r
1397 {\r
1398 metaindex->children++;\r
1399 }\r
007c44c5 1400 else\r
1401 {\r
1402 create(metaindex,Index);\r
1403 metaindex->addr=idx->addr;\r
1404 metaindex->id=buf;\r
1405 metaindex->bitmask=bitmask;\r
1406 metaindex->parent=NULL;\r
1407 metaindex->children=0;\r
1408 idxcount++;\r
1409 push(metaindex,idxs);\r
1410 }\r
1411 idx->parent=metaindex;\r
1412 }\r
1413 }\r
1414\r
1415 /* this should slightly optimize throughout ... */\r
1416 sort(idx,idxs,desc_order_by,children);\r
1417 sort(idx,idxs,order_by,bitmask);\r
1418\r
1419 i=0;\r
208112af 1420 for_each(idx,idxs)\r
007c44c5 1421 {\r
1422 subnet=subnet_id(idx->addr,idx->bitmask);\r
260c2719 1423 printf("%d: %s/%d\n",\r
1424 ++i, subnet, idx->bitmask);\r
007c44c5 1425 \r
1426 sprintf(str,":post_%s - [0:0]", idx->id);\r
1427 save_line(str);\r
1428\r
1429 sprintf(str,":forw_%s - [0:0]", idx->id);\r
1430 save_line(str);\r
1431\r
1432 if(idx->parent)\r
1433 {\r
1434 string(buf,strlen(idx->parent->id)+6);\r
1435 sprintf(buf,"post_%s",idx->parent->id);\r
1436 }\r
1437 else\r
bb9e11ee 1438 {\r
007c44c5 1439 buf="POSTROUTING";\r
bb9e11ee 1440 }\r
007c44c5 1441\r
1442 sprintf(str,"-A %s -d %s/%d -o %s -j post_%s", buf, subnet, idx->bitmask, lan, idx->id);\r
1443 save_line(str);\r
1444\r
1445 sprintf(str,"-A %s -d %s/%d -o %s -j post_common", buf, subnet, idx->bitmask, lan);\r
1446 save_line(str);\r
1447\r
1448 if(idx->parent)\r
1449 {\r
1450 string(buf,strlen(idx->parent->id)+6);\r
1451 sprintf(buf,"forw_%s",idx->parent->id);\r
1452 }\r
1453 else\r
bb9e11ee 1454 {\r
007c44c5 1455 buf="FORWARD";\r
bb9e11ee 1456 }\r
007c44c5 1457\r
1458 sprintf(str,"-A %s -s %s/%d -o %s -j forw_%s", buf, subnet, idx->bitmask, wan, idx->id);\r
1459 save_line(str);\r
1460\r
1461 sprintf(str,"-A %s -s %s/%d -o %s -j forw_common", buf, subnet, idx->bitmask, wan);\r
1462 save_line(str);\r
1463 }\r
1464 printf("Total indexed iptables chains created: %d\n", i);\r
1465\r
1466 sprintf(str,"-A FORWARD -o %s -j forw_common", wan);\r
1467 save_line(str);\r
1468 \r
1469 sprintf(str,"-A POSTROUTING -o %s -j post_common", lan);\r
1470 save_line(str);\r
1471 }\r
1472 \r
1473 }\r
1474\r
1475 if(just_flush)\r
1476 {\r
1477 fclose(iptables_file);\r
add90548 1478 if(log_file)\r
1479 { \r
1480 fclose(log_file);\r
1481 }\r
007c44c5 1482 puts("Just flushed iptables and tc classes - now exiting ...");\r
1483 exit(0);\r
1484 }\r
1485\r
1486 if(!just_preview)\r
1487 {\r
1488 if(!dry_run && !nodelay && qos_free_delay)\r
1489 {\r
1490 printf("Flushed iptables and tc classes - now sleeping for %d seconds...\n",qos_free_delay);\r
1491 sleep(qos_free_delay);\r
1492 }\r
1493\r
6cc38f96 1494 sprintf(str,"%s qdisc add dev %s root handle 1: htb r2q %d default 1",\r
1495 tc,lan,htb_r2q);\r
007c44c5 1496 safe_run(str);\r
1497\r
208112af 1498 sprintf(str, "%s class add dev %s parent 1: classid 1:2 htb rate %s ceil %s burst %dk prio %d",\r
1499 tc,lan,lan_medium,lan_medium,burst_main,highest_priority);\r
007c44c5 1500 safe_run(str);\r
1501\r
208112af 1502 sprintf(str, "%s class add dev %s parent 1:2 classid 1:1 htb rate %Ldkbit ceil %Ldkbit burst %dk prio %d",\r
1503 tc,lan,line,line,burst_main,highest_priority);\r
007c44c5 1504 safe_run(str);\r
1505\r
1506 sprintf(str,"%s qdisc add dev %s root handle 1: htb r2q %d default 1",tc,wan,htb_r2q);\r
1507 safe_run(str);\r
1508\r
208112af 1509 sprintf(str, "%s class add dev %s parent 1: classid 1:2 htb rate %s ceil %s burst %dk prio %d",\r
1510 tc,wan,wan_medium,wan_medium,burst_main,highest_priority);\r
007c44c5 1511 safe_run(str);\r
1512\r
208112af 1513 sprintf(str, "%s class add dev %s parent 1:2 classid 1:1 htb rate %Ldkbit ceil %Ldkbit burst %dk prio %d",\r
1514 tc,wan,up,up,burst_main,highest_priority);\r
007c44c5 1515 safe_run(str);\r
1516 }\r
1517\r
1518 /*-----------------------------------------------------------------*/\r
208112af 1519 puts("Locating heavy downloaders and generating root classes ...");\r
007c44c5 1520 /*-----------------------------------------------------------------*/\r
bb9e11ee 1521 sort(ip,ips,desc_order_by,traffic); \r
007c44c5 1522\r
1523 /*-----------------------------------------------------------------*/\r
1524 /* sub-scope - local variables */ \r
1525 {\r
6cc38f96 1526 long long int rate = line;\r
1527 long long int max = line;\r
1528 int group_count = 0;\r
1529 FILE *credit_file = NULL;\r
007c44c5 1530 \r
4358455e 1531 if(!just_preview && !dry_run && enable_credit)\r
1532 {\r
6cc38f96 1533 credit_file = fopen(credit,"w");\r
4358455e 1534 }\r
007c44c5 1535 \r
208112af 1536 for_each(group,groups)\r
007c44c5 1537 {\r
1538 if(!just_preview)\r
1539 {\r
007c44c5 1540 //download\r
208112af 1541 sprintf(str,"%s class add dev %s parent 1:%d classid 1:%d htb rate %Ldkbit ceil %Ldkbit burst %dk prio %d #down desired %d", \r
1542 tc, lan, parent, group->id, rate, max, burst_group, highest_priority+1, group->desired);\r
007c44c5 1543 safe_run(str);\r
1544 \r
1545 //upload\r
208112af 1546 sprintf(str,"%s class add dev %s parent 1:%d classid 1:%d htb rate %Ldkbit ceil %Ldkbit burst %dk prio %d #up desired %d", \r
1547 tc, wan, parent, group->id, rate*up/line, max*up/line, burst_group, highest_priority+1, group->desired);\r
007c44c5 1548 safe_run(str);\r
1549 }\r
1550 \r
6cc38f96 1551 if(group_count++ < max_nesting)\r
bb9e11ee 1552 {\r
6cc38f96 1553 parent = group->id;\r
bb9e11ee 1554 }\r
007c44c5 1555 \r
6cc38f96 1556 rate -= digital_divide*group->min;\r
1557 if(rate < group->min)\r
4358455e 1558 {\r
6cc38f96 1559 rate = group->min;\r
4358455e 1560 }\r
007c44c5 1561 \r
1562 /*shaping of aggresive downloaders, with credit file support */\r
1563 if(use_credit)\r
1564 {\r
6cc38f96 1565 int group_rate = group->min, priority_sequence = lowest_priority;\r
007c44c5 1566 \r
6cc38f96 1567 for_each(ip, ips) if(ip->min == group->min && ip->max > ip->min)\r
007c44c5 1568 {\r
6cc38f96 1569 if( ip->keyword->data_limit && !ip->fixedprio \r
1570 && ( ip->traffic>ip->credit\r
1571 + (ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20))) )\r
007c44c5 1572 {\r
4358455e 1573 if(group_rate<ip->max)\r
1574 {\r
1575 ip->max=group_rate;\r
1576 }\r
007c44c5 1577 group_rate+=magic_treshold;\r
208112af 1578 ip->prio=lowest_priority;\r
4358455e 1579 if(ip->prio<highest_priority+2)\r
1580 {\r
1581 ip->prio=highest_priority+2;\r
1582 }\r
007c44c5 1583 }\r
1584 else\r
1585 {\r
6cc38f96 1586 if( ip->keyword->data_prio \r
1587 && !ip->fixedprio \r
1588 && ( ip->traffic>ip->credit\r
1589 + (ip->min*ip->keyword->data_prio+(ip->keyword->fixed_prio<<20))) )\r
007c44c5 1590 {\r
1591 ip->prio=priority_sequence--;\r
4358455e 1592 if(ip->prio<highest_priority+1)\r
1593 {\r
1594 ip->prio=highest_priority+1;\r
1595 }\r
007c44c5 1596 }\r
1597 \r
1598 if(credit_file)\r
1599 {\r
1600 unsigned long long lcredit=0;\r
99127c70 1601 \r
4358455e 1602 if((ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20))>ip->traffic)\r
1603 {\r
007c44c5 1604 lcredit=(ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20))-ip->traffic;\r
4358455e 1605 }\r
007c44c5 1606 fprintf(credit_file,"%s %Lu\n",ip->addr,lcredit);\r
1607 }\r
1608 }\r
6cc38f96 1609 } \r
007c44c5 1610 }\r
1611 }\r
4358455e 1612 if(credit_file)\r
1613 {\r
1614 fclose(credit_file);\r
1615 }\r
007c44c5 1616 }\r
1617\r
1618 if(just_preview)\r
1619 {\r
1620 f=fopen(preview,"w");\r
1621 ptr=preview; \r
1622 }\r
1623 else if(!dry_run && !just_flush)\r
1624 {\r
add90548 1625 const char *previous="/var/run/prometheus.previous";\r
007c44c5 1626 /*-----------------------------------------------------------------*/\r
add90548 1627 printf("Writing data transfer database %s ... ", previous);\r
007c44c5 1628 /*-----------------------------------------------------------------*/\r
add90548 1629 f=fopen(previous,"w");\r
007c44c5 1630 if(f)\r
1631 {\r
260c2719 1632 for_each(ip,ips)\r
6cc38f96 1633 {\r
260c2719 1634 if(ip->traffic || ip->direct || ip->proxy || ip->upload)\r
1635 {\r
1636 fprintf(f,"%s %Lu %Lu %Lu %Lu\n",\r
1637 ip->addr, ip->traffic, ip->direct, ip->proxy, ip->upload);\r
1638 }\r
6cc38f96 1639 }\r
007c44c5 1640 fclose(f);\r
add90548 1641 puts(" done.");\r
1642 }\r
1643 else\r
1644 {\r
1645 perror(previous);\r
007c44c5 1646 }\r
007c44c5 1647 f=fopen(html,"w");\r
1648 ptr=html;\r
1649 }\r
1650\r
1651 if(f)\r
1652 {\r
007c44c5 1653 int count=1;\r
1654 i=0;\r
1655\r
1656 /*-----------------------------------------------------------------*/\r
6cc38f96 1657 printf("Sorting data and generating statistics page %s ...\n", ptr);\r
007c44c5 1658 /*-----------------------------------------------------------------*/\r
1659\r
6cc38f96 1660 if(use_jquery_popups)\r
1661 {\r
1662 fprintf(f,"<script type=\"text/javascript\" src=\"%s\"></script>\n", jquery_url);\r
1663 }\r
3ece6581 1664 fputs("<table class=\"decorated last\">\n\\r
1665<caption>Bandwidth classes</caption>\n\\r
1666<thead><tr>\n\\r
1667<th style=\"text-align: right\">#</th>\n\\r
103d292c 1668<th style=\"text-align: right\">group</th>\n\\r
1669<th style=\"text-align: right\">IPs</th>\n\\r
1670<th style=\"text-align: right\">requested</th>\n",f);\r
6cc38f96 1671 fprintf(f,"<th colspan=\"%d\">data limits</th>\n", keywordcount);\r
9aa195f6 1672 fputs("</tr></thead><tbody>\n",f);\r
208112af 1673\r
9aa195f6 1674 row_odd_even = 0;\r
6cc38f96 1675 for_each(group, groups) \r
007c44c5 1676 { \r
1677#ifdef DEBUG\r
3ece6581 1678 printf("%d kb/s group: %d bandwidth requested: %d kb/s\n",group->min,group->count,group->desired);\r
007c44c5 1679#endif\r
3ece6581 1680 fprintf(f, "%s<td style=\"text-align: right\">%d</td><td style=\"text-align: right\">%d&nbsp;kb/s</td>",\r
9aa195f6 1681 tr_odd_even(), count, group->min);\r
3ece6581 1682 fprintf(f, "<td style=\"text-align: right\">%d</td><td style=\"text-align: right\">%d&nbsp;kb/s</td>",\r
9aa195f6 1683 group->count, group->desired);\r
007c44c5 1684\r
c864e623 1685 for_each(keyword, keywords) if(keyword->ip_count)\r
208112af 1686 {\r
c864e623 1687 fprintf(f,"<td style=\"text-align: right\"><span style=\"color:#%s\">%d&nbsp;MB</span></td>",\r
6cc38f96 1688 keyword->html_color, group->min*keyword->data_limit);\r
208112af 1689 } \r
6cc38f96 1690 i += group->desired; \r
1691 total += group->count;\r
007c44c5 1692 count++; \r
1693 }\r
1694#ifdef DEBUG\r
3ece6581 1695 printf("Total groups: %d Total bandwidth requested: %d kb/s\nAGGREGATION: 1/%d\n",\r
6cc38f96 1696 count, i, i/line);\r
007c44c5 1697#endif\r
e64e760d 1698 fprintf(f,"</tr></tbody>\n\\r
1699<thead><tr>\n\\r
1700<th colspan=\"2\" style=\"text-align: left\">Line %Ld kb/s</td>",line);\r
3ece6581 1701 fprintf(f,"<th style=\"text-align: right\">%d</td><th style=\"text-align: right\">%d kb/s</td>",total,i);\r
007c44c5 1702\r
c864e623 1703 for_each(keyword, keywords) if(keyword->ip_count)\r
208112af 1704 {\r
103d292c 1705 fprintf(f,"<th style=\"text-align: right\">%d IPs</th>",keyword->ip_count);\r
208112af 1706 }\r
6cc38f96 1707 fprintf(f,"</tr><tr><th colspan=\"4\">Aggregation 1/%d</th>\n", (int)(0.5+i/line));\r
1708 fprintf(f,"<th colspan=\"%d\">%d traffic classes</th></tr>\n", keywordcount, total);\r
007c44c5 1709\r
e64e760d 1710 fputs("</thead></table>\n",f);\r
007c44c5 1711 }\r
6cc38f96 1712 else if(!dry_run && !just_flush)\r
1713 {\r
007c44c5 1714 perror(html);\r
6cc38f96 1715 }\r
007c44c5 1716\r
6cc38f96 1717 i=0;\r
007c44c5 1718 if(f)\r
1719 {\r
b014f181 1720 unsigned long long total_traffic=0, total_direct=0, total_proxy=0, total_upload=0, tmp_sum=0;\r
007c44c5 1721 int active_classes=0;\r
3ece6581 1722 int colspan=12;\r
007c44c5 1723 struct Sum {unsigned long long l; int i; list(Sum);} *sum,*sums=NULL;\r
78884bad 1724 int limit_count=0, prio_count=0;\r
4a1b62ea 1725 int popup_button=0;\r
007c44c5 1726\r
1727 if(qos_proxy)\r
4358455e 1728 {\r
1729 colspan++;\r
1730 }\r
007c44c5 1731 \r
3ece6581 1732 fprintf(f,"<p><table class=\"decorated last\">\n<caption>%s",title);\r
9aa195f6 1733 fprintf(f," (%s)</caption>\n", d);\r
3ece6581 1734 fputs("<thead><tr>\n<th colspan=\"3\">&nbsp;</th>\n",f);\r
1735 fputs("<th style=\"text-align: right\">credit</th>\n\\r
1736<th style=\"text-align: right\">FUP</th>\n\\r
1737<th style=\"text-align: right\">total</th>\n\\r
1738<th style=\"text-align: right\">down</th>\n",f);\r
007c44c5 1739 if(qos_proxy)\r
4358455e 1740 {\r
103d292c 1741 fputs("<th style=\"text-align: right\">proxy</th>\n",f);\r
4358455e 1742 }\r
3ece6581 1743 fputs("<th style=\"text-align: right\">up</th>\n\\r
1744<th style=\"text-align: right\">min</th>\n\\r
1745<th style=\"text-align: right\">max</th>\n\\r
1746<th style=\"text-align: right\">limit</th>\n\\r
e64e760d 1747<th>&nbsp;</th>\n\\r
1748</tr><tr>\n\\r
3ece6581 1749<th style=\"text-align: right\">#</th>\n\\r
1750<th>hostname [+sharing]</th>\n\\r
1751<th style=\"text-align: right\">LMS</th>\n\\r
1752<th style=\"text-align: right\">MB</th>\n\\r
1753<th style=\"text-align: right\">MB</th>\n\\r
1754<th style=\"text-align: right\">MB</th>\n\\r
1755<th style=\"text-align: right\">MB</th>\n\\r
1756<th style=\"text-align: right\">MB</th>\n\\r
1757<th style=\"text-align: right\">kb/s</th>\n\\r
1758<th style=\"text-align: right\">kb/s</th>\n\\r
1759<th style=\"text-align: right\">kb/s</th>\n\\r
e64e760d 1760<th>prio</th>\n\\r
3ece6581 1761</tr></thead><tbody>\n",f); \r
007c44c5 1762\r
9aa195f6 1763 row_odd_even = 0;\r
6cc38f96 1764 for_each(ip,ips) if(!use_jquery_popups || !ip->sharing)\r
007c44c5 1765 {\r
1766 char *f1="", *f2="";\r
6cc38f96 1767 i++;\r
1768 \r
1769 if(ip->max < ip->desired)\r
007c44c5 1770 {\r
78884bad 1771 f1="<span style=\"color:red\">";\r
1772 f2="</span>";\r
1773 limit_count++;\r
007c44c5 1774 }\r
6cc38f96 1775 else if(ip->prio > highest_priority+1)\r
007c44c5 1776 {\r
78884bad 1777 f1="<span style=\"color:brown\">";\r
1778 f2="</span>";\r
1779 prio_count++;\r
007c44c5 1780 }\r
1781\r
1782#ifdef DEBUG\r
1783 printf("%03d. %-22s %10Lu (%d/%d)\n",i ,ip->name, ip->traffic, ip->min, ip->max); \r
1784#endif\r
4a1b62ea 1785 /* hostnames -------------------------------------- */\r
103d292c 1786 fprintf(f,"%s<td style=\"text-align: right\"><a name=\"%s\"></a>%d</td><td><a class=\"blue\" href=\"%s%s.log\">%s</a>\n", \r
9aa195f6 1787 tr_odd_even(), ip->name, i, log_url, ip->name, ip->name);\r
3ece6581 1788\r
6cc38f96 1789 if(use_jquery_popups)\r
4a1b62ea 1790 {\r
6cc38f96 1791 fprintf(f,"<span id=\"sharing_%d\" style=\"display:none\">",i);\r
1792 popup_button=0;\r
1793 for_each(sharedip, ips) if(eq(ip->name, sharedip->sharing))\r
1794 {\r
103d292c 1795 fprintf(f,"<br /><a class=\"blue\" href=\"%s%s.log\">%s</a>\n", log_url, sharedip->name, sharedip->name);\r
6cc38f96 1796 popup_button++;\r
1797 }\r
1798 fputs("</span>\n",f);\r
1799 if(popup_button)\r
1800 {\r
103d292c 1801 fprintf(f,"<span>[<a class=\"blue\" href=\"#\" onClick=\"$(this).parent().hide();$(\'#sharing_%d\').show();$(\'#download_%d\').show();$(\'#upload_%d\').show();return(false);\" style=\"cursor: pointer;\">+%d</a>]</span>",\r
6cc38f96 1802 i, i, i, popup_button);\r
1803 }\r
4a1b62ea 1804 }\r
1805 fputs("</td>\n",f);\r
1806 /* ----------------------------------------------- */\r
1807\r
4358455e 1808 if(found_lmsid)\r
1809 {\r
103d292c 1810 fputs("<td style=\"text-align: right\">",f);\r
a1d21464 1811 if(ip->lmsid > 0)\r
4358455e 1812 {\r
103d292c 1813 fprintf(f,"<a class=\"blue\" href=\"%s%d\">%04d</a>\n", lms_url, ip->lmsid, ip->lmsid);\r
4358455e 1814 }\r
a1d21464 1815 else if(ip->lmsid == 0)\r
1816 {\r
ad4cb129 1817 fputs("-------",f);\r
a1d21464 1818 }\r
4a1b62ea 1819 fputs("</td>\n",f);\r
4358455e 1820 }\r
3ece6581 1821 fprintf(f,"<td style=\"text-align: right\">%Lu</td>\n", ip->credit);\r
1822 fprintf(f,"<td style=\"text-align: right\"><span style=\"color:#%s\">%Lu</span></td>",\r
4a1b62ea 1823 ip->keyword->html_color,\r
1824 ip->credit+(ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20)));\r
3ece6581 1825 fprintf(f,"<td style=\"text-align: right\">%s%Lu%s", f1, ip->traffic, f2);\r
4a1b62ea 1826\r
1827 /* download --------------------------------------- */\r
3ece6581 1828 fprintf(f,"</td><td style=\"text-align: right\">%Lu", ip->direct);\r
6cc38f96 1829 if(use_jquery_popups)\r
4a1b62ea 1830 {\r
6cc38f96 1831 fprintf(f,"<span id=\"download_%d\" style=\"display:none\">",i);\r
1832 for_each(sharedip, ips) if(eq(ip->name, sharedip->sharing))\r
1833 {\r
3ece6581 1834 fprintf(f,"<br />%Lu", sharedip->direct);\r
6cc38f96 1835 }\r
1836 fputs("</span>\n",f);\r
4a1b62ea 1837 }\r
6cc38f96 1838 fputs("</td>\n",f);\r
4a1b62ea 1839 /* ----------------------------------------------- */\r
1840\r
007c44c5 1841 if(qos_proxy)\r
4358455e 1842 {\r
3ece6581 1843 fprintf(f,"<td style=\"text-align: right\">%Lu</td>\n", ip->proxy);\r
4358455e 1844 }\r
4a1b62ea 1845 /* upload ---------------------------------------- */\r
3ece6581 1846 fprintf(f,"<td style=\"text-align: right\">%Lu", ip->upload);\r
6cc38f96 1847 if(use_jquery_popups)\r
4a1b62ea 1848 {\r
6cc38f96 1849 fprintf(f,"<span id=\"upload_%d\" style=\"display:none\">",i);\r
1850 for_each(sharedip,ips) if(eq(ip->name, sharedip->sharing))\r
1851 {\r
3ece6581 1852 fprintf(f,"<br />%Lu", sharedip->upload);\r
add90548 1853 if(!just_preview)\r
1854 {\r
1855 append_log(sharedip);\r
1856 }\r
6cc38f96 1857 }\r
1858 fputs("</span>\n",f);\r
4a1b62ea 1859 }\r
6cc38f96 1860 fputs("</td>\n",f);\r
4a1b62ea 1861 /* ----------------------------------------------- */\r
1862\r
3ece6581 1863 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
1864<td style=\"text-align: right\">%d</td>\n\\r
1865<td style=\"text-align: right\">%s%d%s</td>\n\\r
1866<td>%s%d%s</td></tr>\n",\r
1867 ip->min, ip->desired, \r
1868 f1, ip->max, f2, \r
1869 f1, ip->prio, f2);\r
6cc38f96 1870\r
b014f181 1871 total_traffic+=ip->traffic;\r
007c44c5 1872 total_direct+=ip->direct;\r
1873 total_proxy+=ip->proxy;\r
1874 total_upload+=ip->upload;\r
1875 if(ip->traffic>0)\r
1876 {\r
1877 active_classes++;\r
1878 tmp_sum+=ip->traffic;\r
1879 create(sum,Sum);\r
1880 sum->l=tmp_sum;\r
1881 sum->i=active_classes;\r
1882 insert(sum,sums,order_by,i);\r
1883 }\r
6cc38f96 1884\r
007c44c5 1885 if(!just_preview)\r
1886 {\r
add90548 1887 append_log(ip);\r
007c44c5 1888 }\r
007c44c5 1889 }\r
e64e760d 1890 fprintf(f,"</tbody><thead><tr>\n\\r
1891<th colspan=\"%d\" style=\"text-align: left\">%d CLASSES</th>", colspan-7, i);\r
3ece6581 1892 fprintf(f,"<th style=\"text-align: right\">%Lu</th><th style=\"text-align: right\">%Lu</th>\n", total_traffic, total_direct);\r
007c44c5 1893 if(qos_proxy)\r
4358455e 1894 {\r
3ece6581 1895 fprintf(f,"<th style=\"text-align: right\">%Lu</th>\n", total_proxy);\r
4358455e 1896 }\r
3ece6581 1897 fprintf(f,"<th style=\"text-align: right\">%Lu</th>", total_upload);\r
e64e760d 1898 fprintf(f,"<th colspan=\"4\"><span style=\"color:red\">LIMIT %dx</span> <span style=\"color:brown\">LOW-PRIO %dx</span></th></tr>\n</thead></table>\n",limit_count,prio_count);\r
007c44c5 1899\r
9aa195f6 1900 row_odd_even = 0;\r
007c44c5 1901 if(active_classes>10)\r
c9012978 1902 {\r
60a8251d 1903 int top20_count=0,top20_perc1=0;\r
1904 long long top20_perc2=0;\r
1905 unsigned long long top20_sum=0l;\r
1906 \r
3ece6581 1907 fputs("<a name=\"erp\"></a><p><table class=\"decorated last\"><caption>Enterprise Resource Planning (ERP)</caption>\n",f);\r
1908 fputs("<thead><tr>\n\\r
1909<th>Analytic category</th>\n\\r
1910<th colspan=\"2\" style=\"text-align: center\">Active Classes</th>\n\\r
1911<th colspan=\"2\" style=\"text-align: center\">Data transfers</th>\n\\r
1912</tr></thead><tbody>\n",f);\r
c9012978 1913\r
b014f181 1914 if_exists(sum,sums,sum->l>=total_traffic/4)\r
c9012978 1915 {\r
9aa195f6 1916 fprintf(f,"%s<td>Top 25%% of traffic</td>\n", tr_odd_even());\r
103d292c 1917 fprintf(f,"<td style=\"text-align: right\">%d</td><td style=\"text-align: right\">%d %%</td><td style=\"text-align: right\">%Lu M</td><td style=\"text-align: right\">%Ld %%</td></tr>\n",sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
c9012978 1918 }\r
1919 \r
208112af 1920 if_exists(sum,sums,sum->i==10)\r
c9012978 1921 {\r
9aa195f6 1922 fprintf(f,"%s<td>Top 10 downloaders</td>\n", tr_odd_even());\r
3ece6581 1923 fprintf(f,"<td style=\"text-align: right\"><strong>10</strong></td>\n\\r
1924<td style=\"text-align: right\">%d %%</td>\n\\r
1925<td style=\"text-align: right\">%Lu MB</td>\n\\r
1926<td style=\"text-align: right\">%Ld %%</td></tr>\n",\r
1927 (100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
c9012978 1928 }\r
1929\r
b014f181 1930 if_exists(sum,sums,sum->l>=total_traffic/2)\r
c9012978 1931 {\r
9aa195f6 1932 fprintf(f,"%s<td>Top 50%% of traffic</td>\n", tr_odd_even());\r
3ece6581 1933 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
1934<td style=\"text-align: right\">%d %%</td>\n\\r
1935<td style=\"text-align: right\">%Lu MB</td>\n\\r
1936<td style=\"text-align: right\"><strong>%Ld %%</strong></td></tr>\n",\r
1937 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
c9012978 1938 }\r
1939\r
b014f181 1940 if_exists(sum,sums,sum->l>=4*total_traffic/5)\r
c9012978 1941 {\r
9aa195f6 1942 fprintf(f,"%s<td>Top 80%% of traffic</td>\n", tr_odd_even());\r
3ece6581 1943 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
1944<td style=\"text-align: right\">%d %%</td>\n\\r
1945<td style=\"text-align: right\">%Lu MB</td>\n\\r
1946<td style=\"text-align: right\"><strong>%Ld %%</strong></td></tr>\n",\r
1947 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
c9012978 1948 }\r
1949\r
60a8251d 1950 if_exists(sum,sums,sum->i>=(active_classes+1)/5)\r
c9012978 1951 {\r
9aa195f6 1952 fprintf(f,"%s<td>Top 20%% downloaders</td>\n", tr_odd_even());\r
60a8251d 1953 top20_count=sum->i;\r
1954 top20_perc1=(100*sum->i+50)/active_classes;\r
1955 top20_sum=sum->l;\r
b014f181 1956 top20_perc2=(100*sum->l+50)/total_traffic;\r
3ece6581 1957 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
1958<td style=\"text-align: right\"><strong>%d %%</strong></td>\n\\r
1959<td style=\"text-align: right\">%Lu MB</td>\n\\r
1960<td style=\"text-align: right\">%Ld %%</td></tr>\n",\r
1961 top20_count,top20_perc1,top20_sum,top20_perc2);\r
c9012978 1962 }\r
1963\r
208112af 1964 if_exists(sum,sums,sum->i>=(active_classes+1)/4)\r
c9012978 1965 {\r
9aa195f6 1966 fprintf(f,"%s<td>Top 25%% downloaders</td>\n", tr_odd_even());\r
3ece6581 1967 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
1968<td style=\"text-align: right\">%d %%</td>\n\\r
1969<td style=\"text-align: right\">%Lu MB</td>\n\\r
1970<td style=\"text-align: right\">%Ld %%</td></tr>\n",\r
1971 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
c9012978 1972 }\r
1973\r
208112af 1974 if_exists(sum,sums,sum->i>=(active_classes+1)/2)\r
c9012978 1975 {\r
9aa195f6 1976 fprintf(f,"%s<td>Top 50%% downloaders</td>\n", tr_odd_even());\r
3ece6581 1977 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
1978<td style=\"text-align: right\"><strong>%d %%</strong></td>\n\\r
1979<td style=\"text-align: right\">%Lu MB</td>\n\\r
1980<td style=\"text-align: right\">%Ld %%</td></tr>\n",\r
1981 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
c9012978 1982 }\r
1983\r
208112af 1984 if_exists(sum,sums,sum->i>=4*(active_classes+1)/5)\r
c9012978 1985 {\r
9aa195f6 1986 fprintf(f,"%s<td>Top 80%% downloaders</td>\n", tr_odd_even());\r
3ece6581 1987 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\\r
1988<td style=\"text-align: right\">%d %%</td>\n\\r
1989<td style=\"text-align: right\">%Lu MB</td>\n\\r
1990<td style=\"text-align: right\">%Ld %%</td></tr></tbody>\n",\r
1991 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);\r
c9012978 1992 }\r
1993\r
3ece6581 1994 fprintf(f,"<tr><thead><th><a class=\"blue\" href=\"%sERP.log\">All users, all traffic</a></th>\n", log_url);\r
103d292c 1995 fprintf(f,"<th style=\"text-align: right\">%d</th>\n\\r
1996<th style=\"text-align: right\">100 %%</th>\n\\r
1997<th style=\"text-align: right\">%Lu M</th>\n\\r
1998<th style=\"text-align: right\">100 %%</th></tr>\n",active_classes,total_traffic);\r
3ece6581 1999 fputs("</thead></table>\n", f);\r
60a8251d 2000\r
5da44508 2001 /* write basic ERP data to log directory */\r
60a8251d 2002 if(!just_preview)\r
2003 {\r
add90548 2004 FILE *iplog;\r
60a8251d 2005 sprintf(str,"%s/ERP.log",log_dir);\r
2006 iplog=fopen(str,"a");\r
2007 if(iplog)\r
2008 {\r
78884bad 2009 fprintf(iplog,"%ld\t%d\t%d %%\t%Lu M\t%Ld %%\tACTIVE %d\tTRAFFIC %Lu M\tCLASSES %d\tFUP-LIMIT %d\tLOW-PRIO %d\t%s",\r
4a1b62ea 2010 time(NULL), top20_count, top20_perc1, top20_sum, top20_perc2, \r
6cc38f96 2011 active_classes, total_traffic, i, limit_count, prio_count, d); /* d = date*/\r
60a8251d 2012 fclose(iplog);\r
2013 }\r
add90548 2014 else\r
2015 {\r
2016 perror(str);\r
2017 }\r
60a8251d 2018 }\r
c9012978 2019 }\r
60a8251d 2020\r
2d114137 2021 fprintf(f, stats_html_signature, version);\r
007c44c5 2022 fclose(f);\r
2023 }\r
2024\r
2025 if(just_preview)\r
2026 {\r
2027 puts("Statistics preview generated (-p switch) - now exiting ...");\r
2028 exit(0);\r
2029 }\r
2030 \r
2031 /*-----------------------------------------------------------------*/\r
2032 puts("Generating iptables and tc classes ...");\r
2033 /*-----------------------------------------------------------------*/\r
2034\r
2035 i=0;\r
260c2719 2036#ifdef DEBUG\r
007c44c5 2037 printf("%-22s %-15s mark\n","name","ip");\r
260c2719 2038#endif\r
208112af 2039\r
6cc38f96 2040 for_each(ip,ips) if(ip->mark>0)\r
007c44c5 2041 { \r
007c44c5 2042 if(idxs)\r
2043 {\r
2044 char *buf;\r
2045 duplicate(ip->addr,buf);\r
2046 buf=hash_id(ip->addr,32-idxtable_bitmask1); \r
2047 \r
2048 string(chain_forward,6+strlen(buf));\r
2049 strcpy(chain_forward,"forw_");\r
2050 strcat(chain_forward,buf);\r
2051\r
2052 string(chain_postrouting,6+strlen(buf));\r
2053 strcpy(chain_postrouting,"post_");\r
2054 strcat(chain_postrouting,buf);\r
2055 \r
2056 free(buf);\r
2057 }\r
2058 else\r
2059 {\r
2060 chain_forward="FORWARD";\r
2061 chain_postrouting="POSTROUTING";\r
2062 }\r
2063\r
260c2719 2064#ifdef DEBUG\r
007c44c5 2065 printf("%-22s %-16s %04d ", ip->name, ip->addr, ip->mark); \r
260c2719 2066#endif\r
007c44c5 2067\r
2068 /* -------------------------------------------------------- mark download */\r
2069 \r
2070 sprintf(str,"-A %s -d %s/32 -o %s -j %s%d",chain_postrouting,ip->addr,lan,mark_iptables,ip->mark);\r
2071 /*sprintf(str,"-A %s -d %s/32 -o %s -j MARK --set-mark %d",chain_postrouting,ip->addr,lan,ip->mark);*/\r
2072 /* -m limit --limit 1/s */ \r
2073 save_line(str);\r
2074\r
2075 if(qos_proxy)\r
2076 {\r
2077 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);\r
2078 /*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);*/\r
2079 save_line(str);\r
2080 }\r
2081\r
2082 sprintf(str,"-A %s -d %s/32 -o %s -j ACCEPT",chain_postrouting,ip->addr,lan);\r
2083 save_line(str);\r
2084\r
2085 /* -------------------------------------------------------- mark upload */\r
2086 sprintf(str,"-A %s -s %s/32 -o %s -j %s%d",chain_forward,ip->addr,wan,mark_iptables,ip->mark);\r
2087 /* sprintf(str,"-A %s -s %s/32 -o %s -j MARK --set-mark %d",chain_forward,ip->addr,wan,ip->mark);*/\r
2088 save_line(str);\r
2089\r
2090 sprintf(str,"-A %s -s %s/32 -o %s -j ACCEPT",chain_forward,ip->addr,wan);\r
2091 save_line(str);\r
2092\r
2093 if(ip->min)\r
2094 {\r
2095 /* -------------------------------------------------------- download class */\r
260c2719 2096#ifdef DEBUG\r
007c44c5 2097 printf("(down: %dk-%dk ", ip->min, ip->max); \r
260c2719 2098#endif\r
007c44c5 2099\r
2100 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);\r
2101 safe_run(str);\r
2102\r
260c2719 2103 if(strcmpi(ip->keyword->leaf_discipline, "none"))\r
dee5592e 2104 {\r
2105 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*/\r
2106 safe_run(str);\r
2107 } \r
260c2719 2108 if(filter_type == 1)\r
dee5592e 2109 {\r
2110 sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d", tc, lan, ip->mark, ip->mark);\r
2111 safe_run(str);\r
007c44c5 2112 }\r
2113\r
2114 /* -------------------------------------------------------- upload class */\r
260c2719 2115#ifdef DEBUG\r
007c44c5 2116 printf("up: %dk-%dk)\n", (int)((ip->min/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed), \r
2117 (int)((ip->max/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed));\r
260c2719 2118#endif\r
007c44c5 2119\r
2120 sprintf(str,"%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",\r
2121 tc, wan, ip->group, ip->mark,\r
2122 (int)((ip->min/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed),\r
2123 (int)((ip->max/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed), burst, ip->prio);\r
2124 safe_run(str);\r
2125 \r
260c2719 2126 if(strcmpi(ip->keyword->leaf_discipline, "none"))\r
dee5592e 2127 {\r
2128 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*/\r
2129 safe_run(str);\r
2130 } \r
260c2719 2131 if(filter_type == 1)\r
dee5592e 2132 {\r
2133 sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d",tc, wan, ip->mark, ip->mark);\r
2134 safe_run(str);\r
007c44c5 2135 }\r
2136 }\r
2137 else\r
260c2719 2138 {\r
2139#ifdef DEBUG\r
007c44c5 2140 printf("(sharing %s)\n", ip->sharing);\r
260c2719 2141#endif\r
2142 }\r
007c44c5 2143 i++;\r
2144 }\r
2145\r
007c44c5 2146 if(idxs)\r
2147 {\r
dee5592e 2148 chain_forward = "forw_common";\r
2149 chain_postrouting = "post_common";\r
007c44c5 2150 }\r
2151 else\r
2152 {\r
dee5592e 2153 chain_forward = "FORWARD";\r
2154 chain_postrouting = "POSTROUTING";\r
007c44c5 2155 }\r
dee5592e 2156 /* -------------------------------- classify or reject free download */\r
007c44c5 2157 {\r
b6a8d473 2158 char *final_chain = "DROP"; /* REJECT would be better, but it is impossible in mangle */\r
dee5592e 2159 if(free_min) final_chain = "ACCEPT";\r
2160 if(qos_proxy)\r
2161 {\r
2162 if(free_min)\r
2163 {\r
2164 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);\r
2165 save_line(str);\r
2166 }\r
2167 sprintf(str,"-A %s -s %s -p tcp --sport %d -o %s -j %s",chain_postrouting,proxy_ip,proxy_port,lan,final_chain);\r
2168 save_line(str);\r
2169 }\r
2170 if(free_min)\r
2171 {\r
2172 sprintf(str,"-A %s -o %s -j %s%d",chain_postrouting,lan,mark_iptables,3);\r
2173 save_line(str);\r
2174 }\r
2175 sprintf(str,"-A %s -o %s -j %s",chain_postrouting,lan,final_chain);\r
2176 save_line(str);\r
2177 /* ------------------------------- classify or reject free upload */\r
2178 if(free_min)\r
2179 {\r
2180 sprintf(str,"-A %s -o %s -j %s%d",chain_forward,wan,mark_iptables,3);\r
2181 save_line(str);\r
2182 }\r
2183 sprintf(str,"-A %s -o %s -j %s",chain_forward,wan,final_chain);\r
2184 save_line(str);\r
007c44c5 2185 }\r
abe9b855 2186\r
dee5592e 2187 if(free_min) /* allocate free bandwith if it is not zero... */ \r
2188 {\r
2189 /*-----------------------------------------------------------------*/\r
2190 puts("Generating free bandwith classes ...");\r
2191 /*-----------------------------------------------------------------*/\r
208112af 2192 sprintf(str, "%s class add dev %s parent 1:%d classid 1:3 htb rate %dkbit ceil %dkbit burst %dk prio %d",\r
2193 tc,lan,parent,free_min,free_max,burst,lowest_priority);\r
dee5592e 2194 safe_run(str);\r
208112af 2195 sprintf(str, "%s class add dev %s parent 1:%d classid 1:3 htb rate %dkbit ceil %dkbit burst %dk prio %d",\r
2196 tc,wan,parent,free_min,free_max,burst,lowest_priority);\r
dee5592e 2197 safe_run(str);\r
2198 /* tc SFQ */\r
260c2719 2199 if(strcmpi(qos_leaf, "none"))\r
dee5592e 2200 {\r
2201 sprintf(str,"%s qdisc add dev %s parent 1:3 handle 3 %s",tc,lan,qos_leaf);\r
2202 safe_run(str);\r
2203 \r
2204 sprintf(str,"%s qdisc add dev %s parent 1:3 handle 3 %s",tc,wan,qos_leaf);\r
2205 safe_run(str);\r
2206 } \r
2207 /* tc handle 1 fw flowid */\r
2208 sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle 3 fw flowid 1:3",tc,lan);\r
2209 safe_run(str);\r
007c44c5 2210\r
dee5592e 2211 sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle 3 fw flowid 1:3",tc,wan);\r
2212 safe_run(str);\r
007c44c5 2213 }\r
dee5592e 2214 printf("Total IP count: %d\n", i);\r
2215 run_restore(); \r
260c2719 2216 if(log_file)\r
2217 {\r
2218 fclose(log_file);\r
2219 }\r
007c44c5 2220 return 0;\r
007c44c5 2221 /* that's all folks, thank you for reading it all the way up to this point ;-) */\r
2222 /* bad luck C<<1 is not yet finished, I promise no sprintf() next time... */\r
2223}\r
This page took 2.284682 seconds and 4 git commands to generate.