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