json improvements
[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-2012 Michael Polak, Arachne Labs */
6 /* iptables-restore support Copyright(C) 2007-2008 ludva */
7 /* Credit: CZFree.Net,Martin Devera,Netdave,Aquarius,Gandalf */
8 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
9
10 /* Modified by: xChaos, 20120612
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-f";
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 target=\"_blank\" 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 - html version */
61 char *json = "/var/www/logs/traffic.json"; /* hall of fame - json version */
62 char *json_preview = "/var/www/logs/preview.json"; /* hall of fame preview - json version */
63 char *cmdlog = "/var/log/prometheuslog"; /* command log filename */
64 char *log_dir = "/var/www/logs/"; /* log directory pathname, ended with slash */
65 char *log_url = "/logs/"; /* log directory relative URI prefix (partial URL) */
66 char *html_log_dir = "/var/www/logs/html/";
67
68 char *jquery_url = "http://code.jquery.com/jquery-latest.js";
69 char *lms_url = "/lms/?m=customerinfo&amp;id=";
70 int use_jquery_popups = 1;
71 int row_odd_even = 0; /*<tr class="odd/even"> */
72
73
74 const char *tr_odd_even(void)
75 {
76 row_odd_even = 1 - row_odd_even;
77 if(row_odd_even)
78 {
79 return "<tr class=\"even\">\n";
80 }
81 else
82 {
83 return "<tr class=\"odd\">\n";
84 }
85 }
86
87 /* ======= Help screen is hopefuly self-documenting part of code :-) ======= */
88
89 void help(void)
90 {
91 puts("Command line switches:\n\
92 \n\
93 -d Dry run (preview tc and iptables commands on stdout)\n\
94 -r Run (reset all statistics and start shaping - daily usage)\n\
95 -p just generate Preview of data transfer statistics and exit (after -r)\n\
96 -s start Shaping FUP limits (keeps data transfer stat like -p) (after -r)\n\
97 -n run Now (like -r delay - overrides qos-free-delay keyword, after boot)\n\
98 -f just Flush iptables and tc classes and exit (stop shaping, no QiS)\n\
99 -9 emergency iptables flush (like -f, but dumps data transfer statistics)\n\
100 \n\
101 -c filename force alternative /etc/prometheus/prometheus.conf filename\n\
102 -h filename force alternative /etc/hosts filename (overrides hosts keyword)\n\
103 -l Mmm YYYY generate HTML summary of Logged traffic (Mmm=Jan-Dec) (and exit)\n\
104 -m generate HTML summary of traffic for yesterday's Month (and exit)\n\
105 -y generate HTML summary of traffic for yesterday's Year (and exit)\n\
106 -? --help show this help scree (and exit)\n\
107 -v --version show Version number of this utility (and exit)\n");
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("json-preview",json_preview);
444 option("log-filename",cmdlog);
445 option("credit-filename",credit);
446 option("classmap-filename",classmap);
447 ioption("credit-enable",enable_credit);
448 option("log-traffic-directory",log_dir);
449 option("log-traffic-html-directory",html_log_dir);
450 option("log-traffic-url-path",log_url);
451 option("jquery-url",jquery_url);
452 option("lms-url",lms_url);
453 ioption("use-jquery-popups",use_jquery_popups);
454 option("qos-free-zone",qos_free_zone);
455 ioption("qos-free-delay",qos_free_delay);
456 ioption("qos-proxy-enable",qos_proxy);
457 option("qos-proxy-ip",proxy_ip);
458 option("htb-leaf-discipline",qos_leaf);
459 ioption("qos-proxy-port",proxy_port);
460 ioption("free-rate",free_min);
461 ioption("free-ceil",free_max);
462 ioption("htb-burst",burst);
463 ioption("htb-burst-main",burst_main);
464 ioption("htb-burst-group",burst_group);
465 ioption("htb-nesting-limit",max_nesting);
466 ioption("htb-r2q",htb_r2q);
467 ioption("magic-include-upload",include_upload);
468 ioption("magic-treshold",magic_treshold);
469 option("filter-type", cnf);
470 /* not yet implemented:
471 ioption("magic-fixed-packets",fixed_packets);
472 ioption("magic-relative-packets",packet_limit);
473 */
474 }
475 fail
476 {
477 perror(config_filename);
478 puts("Warning - using built-in defaults instead ...");
479 }
480 done; /* ugly macro end */
481 printf("\n");
482
483 /* leaf discipline for keywords */
484 for_each(keyword,keywords)
485 {
486 if(!strcmpi(keyword->leaf_discipline, ""))
487 {
488 keyword->leaf_discipline = qos_leaf;
489 }
490 }
491
492 if(strcmpi(cnf, "mark"))
493 {
494 filter_type = 2;
495 mark = "CLASSIFY";
496 mark_iptables = "CLASSIFY --set-class 1:";
497 }
498 else
499 {
500 filter_type = 1;
501 mark = "MARK";
502 mark_iptables = "MARK --set-mark ";
503 }
504
505 /* are supplied values meaningful ?*/
506 if(line<=0 || up<=0)
507 {
508 puts("Illegal value of LAN or WAN bandwidth: 0 kbps.");
509 reject_config_and_exit(config_filename);
510 }
511 }
512
513 /* ===================== traffic analyser - uses iptables ================ */
514
515 void get_traffic_statistics(void)
516 {
517 char *str,*cmd;
518 int downloadflag=0;
519
520 textfile(Pipe,str) *line,*lines=NULL;
521 string(str,STRLEN);
522 string(cmd,STRLEN);
523
524 sprintf(cmd,"%s -L -v -x -n -t mangle",iptables);
525 shell(cmd);
526 input(str,STRLEN)
527 {
528 create(line,Pipe);
529 line->str=str;
530 string(str,STRLEN);
531 append(line,lines);
532 }
533
534 for_each(line,lines)
535 {
536 int col, accept=0,proxyflag=0,valid=1,setchainname=0,commonflag=0;
537 unsigned long long traffic=0;
538 unsigned long pkts=0;
539 char *ipaddr=NULL,*ptr;
540
541 /* debug puts(line->str); */
542 valid_columns(ptr,line->str,' ',col)
543 if(valid) switch(col)
544 {
545 case 1: if(eq(ptr,"Chain"))
546 {
547 setchainname=1;
548 }
549 else if(eq(ptr,"pkts"))
550 {
551 valid=0;
552 }
553 else
554 {
555 sscanf(ptr,"%lu",&pkts);
556 }
557 break;
558 case 2: if(setchainname)
559 {
560 if(!strncmp(ptr,"post_",5) || eq(ptr,"POSTROUTING"))
561 {
562 downloadflag = 1;
563 }
564 else
565 {
566 if(!strncmp(ptr,"forw_",5) || eq(ptr,"FORWARD"))
567 {
568 downloadflag = 0;
569 }
570 }
571 if(eq(ptr,"post_common") || eq(ptr,"forw_common"))
572 {
573 commonflag = 1;
574 }
575 }
576 else
577 {
578 sscanf(ptr,"%Lu",&traffic);
579 traffic += (1<<19);
580 traffic >>= 20;
581 }
582 break;
583 case 3: if((strncmp(ptr,"post_",5) && strncmp(ptr,"forw_",5)) || commonflag)
584 {
585 accept=eq(ptr,mark);
586 }
587 /*if(filter_type==1) accept=eq(ptr,"MARK"); else accept=eq(ptr,"CLASSIFY");*/
588 break;
589 case 8: if(downloadflag)
590 {
591 if(strstr(proxy_ip,ptr))
592 {
593 proxyflag=1;
594 }
595 }
596 else
597 {
598 ipaddr=ptr;
599 }
600 break;
601 case 9: if(downloadflag)ipaddr=ptr;break;
602 }
603
604 if(accept && traffic>0 && ipaddr)
605 {
606 if(proxyflag)
607 {
608 printf("(proxy) ");
609 }
610 else if(!downloadflag)
611 {
612 printf("(upload) ");
613 }
614 printf("IP %s: %Lu MB (%ld pkts)\n", ipaddr, traffic, pkts);
615
616 if_exists(ip,ips,eq(ip->addr,ipaddr));
617 else
618 {
619 TheIP();
620 ip->addr=ipaddr;
621 if(eq(ip->addr,"0.0.0.0/0"))
622 {
623 ip->name="(unregistered)";
624 ip->min=free_min;
625 ip->max=ip->desired=free_max;
626 }
627 }
628
629 if(downloadflag)
630 {
631 if(proxyflag)
632 {
633 ip->proxy=traffic;
634 }
635 else
636 {
637 ip->traffic+=traffic;
638 }
639 ip->direct=ip->traffic-ip->upload-ip->proxy;
640 ip->pktsdown=pkts;
641 }
642 else
643 {
644 ip->upload=traffic;
645 ip->pktsup=pkts;
646 if(include_upload)
647 {
648 ip->traffic+=traffic;
649 }
650 else
651 {
652 if(traffic>ip->traffic)
653 {
654 ip->traffic=traffic;
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; /* ugly macro end */
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; /* ugly macro end */
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\" target=\"_blank\" 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\" target=\"_blank\" 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 <th colspan=\"3\" style=\"text-align: left\">Total:</th>\
944 <th colspan=\"2\" style=\"text-align: right\"><strong>%ld&nbsp;GB</strong></th>\
945 <th style=\"text-align: right\"><strong>%Ld&nbsp;kb/s</strong></th></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>\n\
963 <td style=\"text-align: right\">%d %%</td>\n\
964 <td style=\"text-align: right\">%ld GB</td>\n\
965 <td style=\"text-align: right\">%d %%</td></tr>\n",
966 iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total));
967 }
968
969 if_exists(iplog,iplogs,iplog->i==10)
970 {
971 fprintf(f,"%s<td>Top 10 downloaders</td>\n", tr_odd_even());
972 fprintf(f,"<td style=\"text-align: right\"><strong>10</strong></td>\n\
973 <td style=\"text-align: right\">%d %%</td>\n\
974 <td style=\"text-align: right\">%ld GB</td>\n\
975 <td style=\"text-align: right\">%d %%</td></tr>\n",
976 (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total));
977 }
978
979 if_exists(iplog,iplogs,iplog->l>=total/2)
980 {
981 fprintf(f,"%s<td>Top 50%% of traffic</td>\n", tr_odd_even());
982 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
983 <td style=\"text-align: right\">%d %%</td>\n\
984 <td style=\"text-align: right\">%ld GB</td>\n\
985 <td style=\"text-align: right\"><strong>%d %%</strong></td></tr>\n",
986 iplog->i,(100*iplog->i+50)/i,iplog->l,(int)((100*iplog->l+50)/total));
987 }
988
989 if_exists(iplog,iplogs,iplog->l>=4*total/5)
990 {
991 fprintf(f,"%s<td>Top 80%% of traffic</td>\n",tr_odd_even());
992 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
993 <td style=\"text-align: right\">%d %%</td>\n\
994 <td style=\"text-align: right\">%ld GB</td>\n\
995 <td style=\"text-align: right\"><strong>%d %%</strong></td></tr>\n",
996 iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total));
997 }
998
999 if_exists (iplog,iplogs,iplog->i>=i/5)
1000 {
1001 fprintf(f,"%s<td>Top 20%% downloaders</td>\n",tr_odd_even());
1002 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
1003 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
1004 <td style=\"text-align: right\">%ld GB</td>\n\
1005 <td style=\"text-align: right\">%d %%</td></tr>\n",
1006 iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total));
1007 }
1008
1009 if_exists(iplog,iplogs,iplog->i>=i/4)
1010 {
1011 fprintf(f,"%s<td>Top 25%% downloaders</td>\n", tr_odd_even());
1012 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
1013 <td style=\"text-align: right\">%d %%</td>\n\
1014 <td style=\"text-align: right\">%ld GB</td>\n\
1015 <td style=\"text-align: right\">%d %%</td></tr>\n",
1016 iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total));
1017 }
1018
1019 if_exists(iplog,iplogs,iplog->i>=i/2)
1020 {
1021 fprintf(f,"%s<td>Top 50%% downloaders</td>\n",tr_odd_even());
1022 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
1023 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
1024 <td style=\"text-align: right\">%ld GB</td>\n\
1025 <td style=\"text-align: right\">%d %%</td></tr>\n",
1026 iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total));
1027 }
1028
1029 if_exists(iplog,iplogs,iplog->i>=4*i/5)
1030 {
1031 fprintf(f,"%s<td>Top 80%% downloaders</td>\n",tr_odd_even());
1032 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
1033 <td style=\"text-align: right\">%d %%</td>\n\
1034 <td style=\"text-align: right\">%ld GB</td>\n\
1035 <td style=\"text-align: right\">%d %%</td></tr>\n",
1036 iplog->i, (100*iplog->i+50)/i, iplog->l, (int)((100*iplog->l+50)/total));
1037 }
1038
1039 fprintf(f,"</tbody><thead><tr><th><a class=\"blue\" target=\"_blank\" href=\"%sERP.log\">All users, all traffic</a></th>\n", log_url);
1040 fprintf(f,"<th style=\"text-align: right\">%d</th>\n\
1041 <th style=\"text-align: right\">100 %%</th>\n\
1042 <th style=\"text-align: right\">%ld GB</th>\n\
1043 <th style=\"text-align: right\">100 %%</th></tr>\n",i-1,total);
1044 fputs("</thead></table>\n", f);
1045 }
1046
1047 fprintf(f, stats_html_signature, version);
1048 fclose(f);
1049 puts("done.");
1050 }
1051 else
1052 {
1053 perror(str);
1054 }
1055 }
1056
1057 void append_log(struct IP *self) /*using global variables*/
1058 {
1059 char *d, *str;
1060 FILE *f;
1061
1062 date(d); /* this is typical cll1.h macro - prints current date */
1063 string(str,STRLEN);
1064 sprintf(str,"%s/%s.log", log_dir, self->name);
1065 f=fopen(str,"a");
1066 if(f > 0)
1067 {
1068 fprintf(f,"%ld\t%s\t%Lu\t%Lu\t%Lu\t%Lu\t%d\t%d\t%d\t%d\t%s",
1069 time(NULL), self->name, self->traffic, self->direct, self->proxy,
1070 self->upload, self->min, self->max, self->desired, self->lmsid, d); /* d = date*/
1071 fclose(f);
1072 }
1073 else
1074 {
1075 perror(str);
1076 }
1077 }
1078
1079
1080 /*-----------------------------------------------------------------*/
1081 /* Are you looking for int main(int argc, char **argv) ? :-)) */
1082 /*-----------------------------------------------------------------*/
1083
1084 program
1085 {
1086 int i=0; /* just plain old Fortran style integer :-) */
1087 FILE *f=NULL; /* everything is just stream of bytes... */
1088 char *str, *ptr, *d; /* LET A$=B$ :-) */
1089 char *substring;
1090 int class_count=0,ip_count=0;
1091 int parent=1;
1092 int just_flush=FALSE; /* deactivates all previous actions */
1093 int nodelay=FALSE;
1094 int just_preview=FALSE; /* preview - generate just stats */
1095 int start_shaping=FALSE; /* apply FUP - requires classmap file */
1096 int just_logs=FALSE; /* just parse logs */
1097 int run=FALSE;
1098 int total=0;
1099
1100 char *chain_forward, *chain_postrouting;
1101 char *althosts=NULL;
1102
1103 printf("\n\
1104 Prometheus QoS - \"fair-per-IP\" Quality of Service setup utility.\n\
1105 Version %s - Copyright (C)2005-2012 Michael Polak, Arachne Labs\n\
1106 iptables-restore & burst tunning & classify modification by Ludva\n\
1107 Credit: CZFree.Net, Martin Devera, Netdave, Aquarius, Gandalf\n\n",version);
1108
1109 /*----- Boring... we have to check command line options first: ----*/
1110 arguments
1111 {
1112 argument("-c") { nextargument(config); }
1113 argument("-h") { nextargument(althosts);}
1114 argument("-d") { run=TRUE; dry_run=TRUE; }
1115 argument("-f") { run=TRUE; just_flush=TRUE; }
1116 argument("-9") { run=TRUE; just_flush=9; }
1117 argument("-p") { run=TRUE; just_preview=TRUE; }
1118 argument("-s") { run=TRUE; just_preview=TRUE; start_shaping=TRUE; }
1119 argument("-r") { run=TRUE; }
1120 argument("-n") { run=TRUE; nodelay=TRUE; }
1121 argument("-l") { just_logs=TRUE; }
1122 argument("-m") { just_logs=TRUE; }
1123 argument("-y") { just_logs=TRUE; }
1124 argument("-?") { help(); exit(0); }
1125 argument("--help") { help(); exit(0); }
1126 argument("-v") { exit(0); }
1127 argument("--version") { exit(0); }
1128 }
1129
1130 if(dry_run)
1131 {
1132 puts("*** THIS IS JUST DRY RUN ! ***\n");
1133 }
1134
1135 date(d); /* this is typical cll1.h macro - prints current date */
1136
1137 /*-----------------------------------------------------------------*/
1138 printf("Parsing configuration file %s ...\n", config);
1139 /*-----------------------------------------------------------------*/
1140 get_config(config);
1141
1142 if(just_logs)
1143 {
1144 parse_ip_log(argc,argv);
1145 exit(0);
1146 }
1147 else if(not run)
1148 {
1149 help();
1150 exit(0);
1151 }
1152
1153 if(althosts)
1154 {
1155 hosts=althosts;
1156 }
1157
1158 if(just_flush<9)
1159 {
1160 /*-----------------------------------------------------------------*/
1161 puts("Parsing iptables verbose output ...");
1162 /*-----------------------------------------------------------------*/
1163 get_traffic_statistics();
1164 }
1165
1166 /*-----------------------------------------------------------------*/
1167 printf("Parsing class defintion file %s ...\n", hosts);
1168 /*-----------------------------------------------------------------*/
1169 int groupidx = FIRSTGROUPID;
1170 parse(hosts)
1171 {
1172 str=_;
1173
1174 if(*str<'0' || *str>'9')
1175 {
1176 /* any line starting with non-number is comment ...*/
1177 continue;
1178 }
1179
1180 //Does this IP share QoS class with some other ?
1181 substring=strstr(str,"sharing-");
1182 if(substring)
1183 {
1184 substring+=8; //"sharing-"
1185 parse_ip(str);
1186 ip_count++;
1187 ip->sharing=substring;
1188 ip->keyword=defaultkeyword; /* settings for default keyword */
1189 while(*substring && *substring!='\n')
1190 {
1191 substring++;
1192 }
1193 *substring=0;
1194 }
1195 else
1196 {
1197 //Do we have to create new QoS class for this IP ?
1198
1199 if_exists(keyword,keywords,(substring=strstr(str,keyword->key)))
1200 {
1201 parse_ip(str);
1202 ip_count++;
1203 ip->keyword=keyword;
1204 keyword->ip_count++;
1205 ip->prio=keyword->default_prio;
1206 substring+=strlen(keyword->key)+1;
1207 ptr=substring;
1208 while(*ptr && *ptr!='-')
1209 {
1210 ptr++;
1211 }
1212 if(*ptr=='-')
1213 {
1214 *ptr=0;
1215 ip->max = ip->desired=atoi(ptr+1);
1216 }
1217 ip->min = atoi(substring);
1218 if(ip->min <= 0)
1219 {
1220 printf(" %s: Illegal value of minimum bandwidth 0 kbps, using %d kb/s\n",
1221 str, free_min);
1222 ip->min = free_min;
1223 }
1224 if(ip->max <= ip->min)
1225 {
1226 ip->fixedprio = 1;
1227 ip->max = ip->min+ip->keyword->reserve_min;
1228 }
1229 else
1230 {
1231 ip->max -= ip->keyword->reserve_max;
1232 if(ip->max<ip->min)
1233 {
1234 ip->max=ip->min;
1235 }
1236 }
1237 ip->mark=FIRSTIPCLASS+1+class_count++;
1238
1239 if_exists(group,groups,group->min==ip->min)
1240 {
1241 group->count++;
1242 group->desired += ip->min;
1243 ip->group = group->id;
1244 }
1245 else
1246 {
1247 create(group,Group);
1248 group->min = ip->min;
1249 group->id = groupidx++;
1250 ip->group = group->id;
1251
1252 if(group->min<8) group->min=8;
1253 /* Warning - this is maybe because of primitive tc namespace, can be fixed */
1254 /* it is because class IDs are derived from min. bandwidth. - xCh */
1255 //if(group->min>MAX_GUARANTED_KBPS) group->min=MAX_GUARANTED_KBPS;
1256
1257 group->count=1;
1258 group->desired=ip->min;
1259 insert(group,groups,desc_order_by,min);
1260 }
1261 }//endif keyword-
1262 }//endif sharing-
1263 }
1264 fail
1265 {
1266 perror(hosts);
1267 exit(-1);
1268 }
1269 done; /* ugly macro end */
1270
1271 /*-----------------------------------------------------------------*/
1272 /* cll1.h - let's allocate brand new character buffer... */
1273 /*-----------------------------------------------------------------*/
1274 string(str,STRLEN);
1275
1276 /*-----------------------------------------------------------------*/
1277 puts("Resolving shared connections ...");
1278 /*-----------------------------------------------------------------*/
1279 for_each(ip,ips) if(ip->sharing)
1280 {
1281 for_each(sharedip,ips) if(eq(sharedip->name,ip->sharing))
1282 {
1283 sharedip->traffic+=ip->traffic;
1284 ip->traffic=0;
1285 ip->mark=sharedip->mark;
1286 ip->lmsid=sharedip->lmsid;
1287 break;
1288 }
1289 if(!sharedip)
1290 {
1291 printf("Unresolved shared connection: %s %s sharing-%s\n",
1292 ip->addr, ip->name, ip->sharing);
1293 }
1294 }
1295
1296 if(enable_credit && just_flush<9)
1297 {
1298 /*-----------------------------------------------------------------*/
1299 printf("Parsing credit file %s ...\n", credit);
1300 /*-----------------------------------------------------------------*/
1301 parse(credit)
1302 {
1303 ptr=parse_datafile_line(_);
1304 if(ptr)
1305 {
1306 if_exists(ip,ips,eq(ip->addr,_))
1307 {
1308 sscanf(ptr,"%Lu",&(ip->credit));
1309 }
1310 }
1311 }
1312 done; /* ugly macro end */
1313 }
1314
1315 if(!just_preview)
1316 {
1317 /*-----------------------------------------------------------------*/
1318 puts("Initializing iptables and tc classes ...");
1319 /*-----------------------------------------------------------------*/
1320
1321 iptables_file=fopen(iptablesfile,"w");
1322 if(iptables_file == NULL)
1323 {
1324 puts("Cannot open iptablesfile!");
1325 exit(-1);
1326 }
1327
1328 log_file=fopen(cmdlog,"w");
1329 if(log_file == NULL)
1330 {
1331 puts("Cannot open logfile!");
1332 exit(-1);
1333 }
1334
1335 save_line(iptablespreamble);
1336 run_restore();
1337
1338 sprintf(str,"%s qdisc del dev %s root 2>/dev/null",tc,lan);
1339 safe_run(str);
1340
1341 sprintf(str,"%s qdisc del dev %s root 2>/dev/null",tc,wan);
1342 safe_run(str);
1343
1344 iptables_file=fopen(iptablesfile,"w");
1345 save_line(iptablespreamble);
1346
1347 if(qos_free_zone && *qos_free_zone!='0')
1348 {
1349 char *chain;
1350
1351 sprintf(str,"-A FORWARD -d %s -o %s -j ACCEPT", qos_free_zone, wan);
1352 save_line(str);
1353
1354 if(qos_proxy)
1355 {
1356 save_line(":post_noproxy - [0:0]");
1357 sprintf(str,"-A POSTROUTING -p ! tcp -o %s -j post_noproxy", lan);
1358 save_line(str);
1359 sprintf(str,"-A POSTROUTING -s ! %s -o %s -j post_noproxy", proxy_ip, lan);
1360 save_line(str);
1361 sprintf(str,"-A POSTROUTING -s %s -p tcp --sport ! %d -o %s -j post_noproxy", proxy_ip, proxy_port, lan);
1362 save_line(str);
1363
1364 chain="post_noproxy";
1365 }
1366 else
1367 {
1368 chain="POSTROUTING";
1369 }
1370
1371 sprintf(str,"-A %s -s %s -o %s -j ACCEPT", chain, qos_free_zone, lan);
1372 save_line(str);
1373 }
1374
1375 if(ip_count>idxtable_treshold1 && !just_flush)
1376 {
1377 int idxcount=0, bitmask=32-idxtable_bitmask1; /* default net mask: 255.255.255.240 */
1378 char *subnet, *buf;
1379 /*-----------------------------------------------------------------*/
1380 printf("Detected %d addresses - indexing iptables rules to improve performance...\n",ip_count);
1381 /*-----------------------------------------------------------------*/
1382
1383 save_line(":post_common - [0:0]");
1384 save_line(":forw_common - [0:0]");
1385
1386 for_each(ip,ips) if(ip->addr && *(ip->addr) && !eq(ip->addr,"0.0.0.0/0"))
1387 {
1388 buf=hash_id(ip->addr,bitmask);
1389 if_exists(idx,idxs,eq(idx->id,buf))
1390 {
1391 idx->children++;
1392 }
1393 else
1394 {
1395 create(idx,Index);
1396 idx->addr=ip->addr;
1397 idx->id=buf;
1398 idx->bitmask=bitmask;
1399 idx->parent=NULL;
1400 idx->children=0;
1401 idxcount++;
1402 push(idx,idxs);
1403 }
1404 }
1405
1406 /* brutal perfomance optimalization */
1407 while(idxcount>idxtable_treshold2 && bitmask>2*idxtable_bitmask2)
1408 {
1409 bitmask-=idxtable_bitmask2;
1410 idxcount=0;
1411
1412 for_each(idx,idxs) if(idx->parent == NULL)
1413 {
1414 buf=hash_id(idx->addr,bitmask);
1415 if_exists(metaindex,idxs,eq(metaindex->id,buf))
1416 {
1417 metaindex->children++;
1418 }
1419 else
1420 {
1421 create(metaindex,Index);
1422 metaindex->addr=idx->addr;
1423 metaindex->id=buf;
1424 metaindex->bitmask=bitmask;
1425 metaindex->parent=NULL;
1426 metaindex->children=0;
1427 idxcount++;
1428 push(metaindex,idxs);
1429 }
1430 idx->parent=metaindex;
1431 }
1432 }
1433
1434 /* this should slightly optimize throughout ... */
1435 sort(idx,idxs,desc_order_by,children);
1436 sort(idx,idxs,order_by,bitmask);
1437
1438 i=0;
1439 for_each(idx,idxs)
1440 {
1441 subnet=subnet_id(idx->addr,idx->bitmask);
1442 printf("%d: %s/%d\n",
1443 ++i, subnet, idx->bitmask);
1444
1445 sprintf(str,":post_%s - [0:0]", idx->id);
1446 save_line(str);
1447
1448 sprintf(str,":forw_%s - [0:0]", idx->id);
1449 save_line(str);
1450
1451 if(idx->parent)
1452 {
1453 string(buf,strlen(idx->parent->id)+6);
1454 sprintf(buf,"post_%s",idx->parent->id);
1455 }
1456 else
1457 {
1458 buf="POSTROUTING";
1459 }
1460
1461 sprintf(str,"-A %s -d %s/%d -o %s -j post_%s", buf, subnet, idx->bitmask, lan, idx->id);
1462 save_line(str);
1463
1464 sprintf(str,"-A %s -d %s/%d -o %s -j post_common", buf, subnet, idx->bitmask, lan);
1465 save_line(str);
1466
1467 if(idx->parent)
1468 {
1469 string(buf,strlen(idx->parent->id)+6);
1470 sprintf(buf,"forw_%s",idx->parent->id);
1471 }
1472 else
1473 {
1474 buf="FORWARD";
1475 }
1476
1477 sprintf(str,"-A %s -s %s/%d -o %s -j forw_%s", buf, subnet, idx->bitmask, wan, idx->id);
1478 save_line(str);
1479
1480 sprintf(str,"-A %s -s %s/%d -o %s -j forw_common", buf, subnet, idx->bitmask, wan);
1481 save_line(str);
1482 }
1483 printf("Total indexed iptables chains created: %d\n", i);
1484
1485 sprintf(str,"-A FORWARD -o %s -j forw_common", wan);
1486 save_line(str);
1487
1488 sprintf(str,"-A POSTROUTING -o %s -j post_common", lan);
1489 save_line(str);
1490 }
1491
1492 }
1493
1494 if(just_flush)
1495 {
1496 fclose(iptables_file);
1497 if(log_file)
1498 {
1499 fclose(log_file);
1500 }
1501 puts("Just flushed iptables and tc classes - now exiting ...");
1502 exit(0);
1503 }
1504
1505 if(!just_preview)
1506 {
1507 if(!dry_run && !nodelay && qos_free_delay)
1508 {
1509 printf("Flushed iptables and tc classes - now sleeping for %d seconds...\n",qos_free_delay);
1510 sleep(qos_free_delay);
1511 }
1512
1513 sprintf(str,"%s qdisc add dev %s root handle 1: htb r2q %d default 1",
1514 tc,lan,htb_r2q);
1515 safe_run(str);
1516
1517 sprintf(str, "%s class add dev %s parent 1: classid 1:2 htb rate %s ceil %s burst %dk prio %d",
1518 tc,lan,lan_medium,lan_medium,burst_main,highest_priority);
1519 safe_run(str);
1520
1521 sprintf(str, "%s class add dev %s parent 1:2 classid 1:1 htb rate %Ldkbit ceil %Ldkbit burst %dk prio %d",
1522 tc,lan,line,line,burst_main,highest_priority);
1523 safe_run(str);
1524
1525 sprintf(str,"%s qdisc add dev %s root handle 1: htb r2q %d default 1",tc,wan,htb_r2q);
1526 safe_run(str);
1527
1528 sprintf(str, "%s class add dev %s parent 1: classid 1:2 htb rate %s ceil %s burst %dk prio %d",
1529 tc,wan,wan_medium,wan_medium,burst_main,highest_priority);
1530 safe_run(str);
1531
1532 sprintf(str, "%s class add dev %s parent 1:2 classid 1:1 htb rate %Ldkbit ceil %Ldkbit burst %dk prio %d",
1533 tc,wan,up,up,burst_main,highest_priority);
1534 safe_run(str);
1535 }
1536
1537 /*-----------------------------------------------------------------*/
1538 puts("Locating heavy downloaders and generating root classes ...");
1539 /*-----------------------------------------------------------------*/
1540 sort(ip,ips,desc_order_by,traffic);
1541
1542 /*-----------------------------------------------------------------*/
1543 /* sub-scope - local variables */
1544 {
1545 long long int rate = line;
1546 long long int max = line;
1547 int group_count = 0;
1548 FILE *credit_file = NULL;
1549
1550 if(!just_preview && !dry_run && enable_credit)
1551 {
1552 credit_file = fopen(credit,"w");
1553 }
1554
1555 for_each(group,groups)
1556 {
1557 if(!just_preview)
1558 {
1559 //download
1560 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",
1561 tc, lan, parent, group->id, rate, max, burst_group, highest_priority+1, group->desired);
1562 safe_run(str);
1563
1564 //upload
1565 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",
1566 tc, wan, parent, group->id, rate*up/line, max*up/line, burst_group, highest_priority+1, group->desired);
1567 safe_run(str);
1568 }
1569
1570 if(group_count++ < max_nesting)
1571 {
1572 parent = group->id;
1573 }
1574
1575 rate -= digital_divide*group->min;
1576 if(rate < group->min)
1577 {
1578 rate = group->min;
1579 }
1580
1581 /*shaping of aggresive downloaders, with credit file support */
1582 if(use_credit)
1583 {
1584 int group_rate = group->min, priority_sequence = lowest_priority;
1585
1586 for_each(ip, ips) if(ip->min == group->min && ip->max > ip->min)
1587 {
1588 if( ip->keyword->data_limit && !ip->fixedprio
1589 && ( ip->traffic>ip->credit
1590 + (ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20))) )
1591 {
1592 if(group_rate<ip->max)
1593 {
1594 ip->max=group_rate;
1595 }
1596 group_rate+=magic_treshold;
1597 ip->prio=lowest_priority;
1598 if(ip->prio<highest_priority+2)
1599 {
1600 ip->prio=highest_priority+2;
1601 }
1602 }
1603 else
1604 {
1605 if( ip->keyword->data_prio
1606 && !ip->fixedprio
1607 && ( ip->traffic>ip->credit
1608 + (ip->min*ip->keyword->data_prio+(ip->keyword->fixed_prio<<20))) )
1609 {
1610 ip->prio=priority_sequence--;
1611 if(ip->prio<highest_priority+1)
1612 {
1613 ip->prio=highest_priority+1;
1614 }
1615 }
1616
1617 if(credit_file)
1618 {
1619 unsigned long long lcredit=0;
1620
1621 if((ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20))>ip->traffic)
1622 {
1623 lcredit=(ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20))-ip->traffic;
1624 }
1625 fprintf(credit_file,"%s %Lu\n",ip->addr,lcredit);
1626 }
1627 }
1628 }
1629 }
1630 }
1631 if(credit_file)
1632 {
1633 fclose(credit_file);
1634 }
1635 }
1636
1637 if(just_preview)
1638 {
1639 if(start_shaping)
1640 {
1641 printf("Reading %s and applying Fair Use Policy rules ... \n", classmap);
1642 parse(classmap)
1643 {
1644 ptr=strchr(_,' ');
1645 if(ptr)
1646 {
1647 *ptr=0;
1648 ptr++;
1649 if_exists(ip,ips,eq(ip->addr,_))
1650 {
1651 ip->mark=atoi(ptr);
1652 if(ip->max < ip->desired) /* apply FUP limit immediately.... */
1653 {
1654 printf("Applying limit for %-22s %-16s %04d ", ip->name, ip->addr, ip->mark);
1655 printf("(down: %dk-%dk ", ip->min, ip->max);
1656 sprintf(str, "%s class change dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
1657 tc, lan, ip->group, ip->mark,ip->min,ip->max, burst, ip->prio);
1658 safe_run(str);
1659 printf("up: %dk-%dk)\n", (int)((ip->min/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed),
1660 (int)((ip->max/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed));
1661 sprintf(str,"%s class change dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
1662 tc, wan, ip->group, ip->mark,
1663 (int)((ip->min/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed),
1664 (int)((ip->max/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed), burst, ip->prio);
1665 safe_run(str);
1666 }
1667 }
1668 }
1669 }
1670 fail
1671 {
1672 perror(classmap);
1673 puts("Warning - classmap file not fund, just generating preview ...");
1674 start_shaping=FALSE;
1675 }
1676 done; /* ugly macro end */
1677 }
1678 html=preview;
1679 json=json_preview;
1680 }
1681
1682 if(!dry_run && !just_flush)
1683 {
1684 /*-----------------------------------------------------------------*/
1685 printf("Writing json overview %s ... ", json);
1686 /*-----------------------------------------------------------------*/
1687 f=fopen(json, "w");
1688 if(f > 0)
1689 {
1690 int jsoncount=0;
1691 fprintf(f, "{\n");
1692 for_each(ip, ips)
1693 {
1694 if( ip->lmsid > 0
1695 && (ip->traffic || ip->direct || ip->proxy || ip->upload))
1696 {
1697 if(jsoncount)
1698 {
1699 fprintf(f, ",\n");
1700 }
1701 fprintf(f, " \"%s\":{ \"lms\": %d, \"ip\":\"%s\", \"total\":%Lu, \"down\":%Lu, \"proxy\":%Lu, \"up\":%Lu, \"min\":%d, \"max\":%d, \"limit\":%d }",
1702 ip->name, ip->lmsid, ip->addr, ip->traffic, ip->direct, ip->proxy, ip->upload, ip->min, ip->desired, ip->max);
1703 jsoncount++;
1704 }
1705 }
1706 fprintf(f, "}\n");
1707 fclose(f);
1708 puts("done.");
1709 }
1710 else
1711 {
1712 perror(json);
1713 }
1714 }
1715
1716 f=fopen(html,"w");
1717 if(f > 0)
1718 {
1719 int count=1;
1720 i=0;
1721
1722 /*-----------------------------------------------------------------*/
1723 printf("Sorting data and generating statistics page %s ...\n", html);
1724 /*-----------------------------------------------------------------*/
1725
1726 if(use_jquery_popups)
1727 {
1728 fprintf(f,"<script type=\"text/javascript\" src=\"%s\"></script>\n", jquery_url);
1729 }
1730 fputs("<table class=\"decorated last\">\n\
1731 <caption>Bandwidth classes</caption>\n\
1732 <thead><tr>\n\
1733 <th style=\"text-align: right\">#</th>\n\
1734 <th style=\"text-align: right\">group</th>\n\
1735 <th style=\"text-align: right\">IPs</th>\n\
1736 <th style=\"text-align: right\">requested</th>\n",f);
1737 fprintf(f,"<th colspan=\"%d\">data limits</th>\n", keywordcount);
1738 fputs("</tr></thead><tbody>\n",f);
1739
1740 row_odd_even = 0;
1741 for_each(group, groups)
1742 {
1743 #ifdef DEBUG
1744 printf("%d kb/s group: %d bandwidth requested: %d kb/s\n",group->min,group->count,group->desired);
1745 #endif
1746 fprintf(f, "%s<td style=\"text-align: right\">%d</td><td style=\"text-align: right\">%d&nbsp;kb/s</td>",
1747 tr_odd_even(), count, group->min);
1748 fprintf(f, "<td style=\"text-align: right\">%d</td><td style=\"text-align: right\">%d&nbsp;kb/s</td>",
1749 group->count, group->desired);
1750
1751 for_each(keyword, keywords) if(keyword->ip_count)
1752 {
1753 fprintf(f,"<td style=\"text-align: right\"><span style=\"color:#%s\">%d&nbsp;MB</span></td>",
1754 keyword->html_color, group->min*keyword->data_limit);
1755 }
1756 i += group->desired;
1757 total += group->count;
1758 count++;
1759 }
1760 #ifdef DEBUG
1761 printf("Total groups: %d Total bandwidth requested: %d kb/s\nAGGREGATION: 1/%d\n",
1762 count, i, i/line);
1763 #endif
1764 fprintf(f,"</tr></tbody>\n\
1765 <thead><tr>\n\
1766 <th colspan=\"2\" style=\"text-align: left\">Line %Ld kb/s</td>",line);
1767 fprintf(f,"<th style=\"text-align: right\">%d</td><th style=\"text-align: right\">%d kb/s</td>",total,i);
1768
1769 for_each(keyword, keywords) if(keyword->ip_count)
1770 {
1771 fprintf(f,"<th style=\"text-align: right\">%d IPs</th>",keyword->ip_count);
1772 }
1773 fprintf(f,"</tr><tr><th colspan=\"4\">Aggregation 1/%d</th>\n", (int)(0.5+i/line));
1774 fprintf(f,"<th colspan=\"%d\">%d traffic classes</th></tr>\n", keywordcount, total);
1775
1776 fputs("</thead></table>\n",f);
1777 }
1778 else if(!dry_run && !just_flush)
1779 {
1780 perror(html);
1781 }
1782
1783 i=0;
1784 if(f > 0)
1785 {
1786 unsigned long long total_traffic=0, total_direct=0, total_proxy=0, total_upload=0, tmp_sum=0;
1787 int active_classes=0;
1788 int colspan=12;
1789 struct Sum {unsigned long long l; int i; list(Sum);} *sum,*sums=NULL;
1790 int limit_count=0, prio_count=0;
1791 int popup_button=0;
1792
1793 if(qos_proxy)
1794 {
1795 colspan++;
1796 }
1797
1798 fprintf(f,"<p><table class=\"decorated last\">\n<caption>%s",title);
1799 fprintf(f," (%s)</caption>\n", d);
1800 fputs("<thead><tr>\n<th colspan=\"3\">&nbsp;</th>\n",f);
1801 fputs("<th style=\"text-align: right\">credit</th>\n\
1802 <th style=\"text-align: right\">FUP</th>\n\
1803 <th style=\"text-align: right\">total</th>\n\
1804 <th style=\"text-align: right\">down</th>\n",f);
1805 if(qos_proxy)
1806 {
1807 fputs("<th style=\"text-align: right\">proxy</th>\n",f);
1808 }
1809 fputs("<th style=\"text-align: right\">up</th>\n\
1810 <th style=\"text-align: right\">min</th>\n\
1811 <th style=\"text-align: right\">max</th>\n\
1812 <th style=\"text-align: right\">limit</th>\n\
1813 <th>&nbsp;</th>\n\
1814 </tr><tr>\n\
1815 <th style=\"text-align: right\">#</th>\n\
1816 <th>hostname [+sharing]</th>\n\
1817 <th style=\"text-align: right\">LMS</th>\n\
1818 <th style=\"text-align: right\">MB</th>\n\
1819 <th style=\"text-align: right\">MB</th>\n\
1820 <th style=\"text-align: right\">MB</th>\n\
1821 <th style=\"text-align: right\">MB</th>\n\
1822 <th style=\"text-align: right\">MB</th>\n\
1823 <th style=\"text-align: right\">kb/s</th>\n\
1824 <th style=\"text-align: right\">kb/s</th>\n\
1825 <th style=\"text-align: right\">kb/s</th>\n\
1826 <th>prio</th>\n\
1827 </tr></thead><tbody>\n",f);
1828
1829 row_odd_even = 0;
1830 for_each(ip,ips) if(!use_jquery_popups || !ip->sharing)
1831 {
1832 char *f1="", *f2="";
1833 i++;
1834
1835 if(ip->max < ip->desired)
1836 {
1837 f1="<span style=\"color:red\">";
1838 f2="</span>";
1839 limit_count++;
1840 }
1841 else if(ip->prio > highest_priority+1)
1842 {
1843 f1="<span style=\"color:brown\">";
1844 f2="</span>";
1845 prio_count++;
1846 }
1847
1848 #ifdef DEBUG
1849 printf("%03d. %-22s %10Lu (%d/%d)\n",i ,ip->name, ip->traffic, ip->min, ip->max);
1850 #endif
1851 /* hostnames -------------------------------------- */
1852 fprintf(f,"%s<td style=\"text-align: right\"><a name=\"%s\"></a>%d</td><td><a class=\"blue\" target=\"_blank\" href=\"%s%s.log\">%s</a>\n",
1853 tr_odd_even(), ip->name, i, log_url, ip->name, ip->name);
1854
1855 if(use_jquery_popups)
1856 {
1857 fprintf(f,"<span id=\"sharing_%d\" style=\"display:none\">",i);
1858 popup_button=0;
1859 for_each(sharedip, ips) if(eq(ip->name, sharedip->sharing))
1860 {
1861 fprintf(f,"<br /><a class=\"blue\" target=\"_blank\" href=\"%s%s.log\">%s</a>\n", log_url, sharedip->name, sharedip->name);
1862 popup_button++;
1863 }
1864 fputs("</span>\n",f);
1865 if(popup_button)
1866 {
1867 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>",
1868 i, i, i, popup_button);
1869 }
1870 }
1871 fputs("</td>\n",f);
1872 /* ----------------------------------------------- */
1873
1874 if(found_lmsid)
1875 {
1876 fputs("<td style=\"text-align: right\">",f);
1877 if(ip->lmsid > 0)
1878 {
1879 fprintf(f,"<a class=\"blue\" target=\"_blank\" href=\"%s%d\">%04d</a>\n", lms_url, ip->lmsid, ip->lmsid);
1880 }
1881 else if(ip->lmsid == 0)
1882 {
1883 fputs("-------",f);
1884 }
1885 fputs("</td>\n",f);
1886 }
1887 fprintf(f,"<td style=\"text-align: right\">%Lu</td>\n", ip->credit);
1888 fprintf(f,"<td style=\"text-align: right\"><span style=\"color:#%s\">%Lu</span></td>",
1889 ip->keyword->html_color,
1890 ip->credit+(ip->min*ip->keyword->data_limit+(ip->keyword->fixed_limit<<20)));
1891 fprintf(f,"<td style=\"text-align: right\">%s%Lu%s", f1, ip->traffic, f2);
1892
1893 /* download --------------------------------------- */
1894 fprintf(f,"</td><td style=\"text-align: right\">%Lu", ip->direct);
1895 if(use_jquery_popups)
1896 {
1897 fprintf(f,"<span id=\"download_%d\" style=\"display:none\">",i);
1898 for_each(sharedip, ips) if(eq(ip->name, sharedip->sharing))
1899 {
1900 fprintf(f,"<br />%Lu", sharedip->direct);
1901 }
1902 fputs("</span>\n",f);
1903 }
1904 fputs("</td>\n",f);
1905 /* ----------------------------------------------- */
1906
1907 if(qos_proxy)
1908 {
1909 fprintf(f,"<td style=\"text-align: right\">%Lu</td>\n", ip->proxy);
1910 }
1911 /* upload ---------------------------------------- */
1912 fprintf(f,"<td style=\"text-align: right\">%Lu", ip->upload);
1913 if(use_jquery_popups)
1914 {
1915 fprintf(f,"<span id=\"upload_%d\" style=\"display:none\">",i);
1916 for_each(sharedip,ips) if(eq(ip->name, sharedip->sharing))
1917 {
1918 fprintf(f,"<br />%Lu", sharedip->upload);
1919 }
1920 fputs("</span>\n",f);
1921 }
1922 fputs("</td>\n",f);
1923 /* ----------------------------------------------- */
1924
1925 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
1926 <td style=\"text-align: right\">%d</td>\n\
1927 <td style=\"text-align: right\">%s%d%s</td>\n\
1928 <td>%s%d%s</td></tr>\n",
1929 ip->min, ip->desired,
1930 f1, ip->max, f2,
1931 f1, ip->prio, f2);
1932
1933 total_traffic+=ip->traffic;
1934 total_direct+=ip->direct;
1935 total_proxy+=ip->proxy;
1936 total_upload+=ip->upload;
1937 if(ip->traffic>0)
1938 {
1939 active_classes++;
1940 tmp_sum+=ip->traffic;
1941 create(sum,Sum);
1942 sum->l=tmp_sum;
1943 sum->i=active_classes;
1944 insert(sum,sums,order_by,i);
1945 }
1946
1947 if(!just_preview)
1948 {
1949 append_log(ip);
1950 for_each(sharedip,ips) if(eq(ip->name, sharedip->sharing))
1951 {
1952 append_log(sharedip);
1953 }
1954 }
1955 }
1956 fprintf(f,"</tbody><thead><tr>\n\
1957 <th colspan=\"%d\" style=\"text-align: left\">%d CLASSES</th>", colspan-7, i);
1958 fprintf(f,"<th style=\"text-align: right\">%Lu</th><th style=\"text-align: right\">%Lu</th>\n", total_traffic, total_direct);
1959 if(qos_proxy)
1960 {
1961 fprintf(f,"<th style=\"text-align: right\">%Lu</th>\n", total_proxy);
1962 }
1963 fprintf(f,"<th style=\"text-align: right\">%Lu</th>", total_upload);
1964 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);
1965
1966 row_odd_even = 0;
1967 if(active_classes>10)
1968 {
1969 int top20_count=0,top20_perc1=0;
1970 long long top20_perc2=0;
1971 unsigned long long top20_sum=0l;
1972
1973 fputs("<a name=\"erp\"></a><p><table class=\"decorated last\"><caption>Enterprise Resource Planning (ERP)</caption>\n",f);
1974 fputs("<thead><tr>\n\
1975 <th>Analytic category</th>\n\
1976 <th colspan=\"2\" style=\"text-align: center\">Active Classes</th>\n\
1977 <th colspan=\"2\" style=\"text-align: center\">Data transfers</th>\n\
1978 </tr></thead><tbody>\n",f);
1979
1980 if_exists(sum,sums,sum->l>=total_traffic/4)
1981 {
1982 fprintf(f,"%s<td>Top 25%% of traffic</td>\n", tr_odd_even());
1983 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
1984 <td style=\"text-align: right\">%d %%</td>\n\
1985 <td style=\"text-align: right\">%Lu MB</td>\n\
1986 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
1987 sum->i, (100*sum->i+50)/active_classes, sum->l, (100*sum->l+50)/total_traffic);
1988 }
1989
1990 if_exists(sum,sums,sum->i==10)
1991 {
1992 fprintf(f,"%s<td>Top 10 downloaders</td>\n", tr_odd_even());
1993 fprintf(f,"<td style=\"text-align: right\"><strong>10</strong></td>\n\
1994 <td style=\"text-align: right\">%d %%</td>\n\
1995 <td style=\"text-align: right\">%Lu MB</td>\n\
1996 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
1997 (100*sum->i+50)/active_classes, sum->l, (100*sum->l+50)/total_traffic);
1998 }
1999
2000 if_exists(sum,sums,sum->l>=total_traffic/2)
2001 {
2002 fprintf(f,"%s<td>Top 50%% of traffic</td>\n", tr_odd_even());
2003 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
2004 <td style=\"text-align: right\">%d %%</td>\n\
2005 <td style=\"text-align: right\">%Lu MB</td>\n\
2006 <td style=\"text-align: right\"><strong>%Ld %%</strong></td></tr>\n",
2007 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
2008 }
2009
2010 if_exists(sum,sums,sum->l>=4*total_traffic/5)
2011 {
2012 fprintf(f,"%s<td>Top 80%% of traffic</td>\n", tr_odd_even());
2013 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
2014 <td style=\"text-align: right\">%d %%</td>\n\
2015 <td style=\"text-align: right\">%Lu MB</td>\n\
2016 <td style=\"text-align: right\"><strong>%Ld %%</strong></td></tr>\n",
2017 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
2018 }
2019
2020 if_exists(sum,sums,sum->i>=(active_classes+1)/5)
2021 {
2022 fprintf(f,"%s<td>Top 20%% downloaders</td>\n", tr_odd_even());
2023 top20_count=sum->i;
2024 top20_perc1=(100*sum->i+50)/active_classes;
2025 top20_sum=sum->l;
2026 top20_perc2=(100*sum->l+50)/total_traffic;
2027 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
2028 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
2029 <td style=\"text-align: right\">%Lu MB</td>\n\
2030 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
2031 top20_count,top20_perc1,top20_sum,top20_perc2);
2032 }
2033
2034 if_exists(sum,sums,sum->i>=(active_classes+1)/4)
2035 {
2036 fprintf(f,"%s<td>Top 25%% downloaders</td>\n", tr_odd_even());
2037 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
2038 <td style=\"text-align: right\">%d %%</td>\n\
2039 <td style=\"text-align: right\">%Lu MB</td>\n\
2040 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
2041 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
2042 }
2043
2044 if_exists(sum,sums,sum->i>=(active_classes+1)/2)
2045 {
2046 fprintf(f,"%s<td>Top 50%% downloaders</td>\n", tr_odd_even());
2047 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
2048 <td style=\"text-align: right\"><strong>%d %%</strong></td>\n\
2049 <td style=\"text-align: right\">%Lu MB</td>\n\
2050 <td style=\"text-align: right\">%Ld %%</td></tr>\n",
2051 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
2052 }
2053
2054 if_exists(sum,sums,sum->i>=4*(active_classes+1)/5)
2055 {
2056 fprintf(f,"%s<td>Top 80%% downloaders</td>\n", tr_odd_even());
2057 fprintf(f,"<td style=\"text-align: right\">%d</td>\n\
2058 <td style=\"text-align: right\">%d %%</td>\n\
2059 <td style=\"text-align: right\">%Lu MB</td>\n\
2060 <td style=\"text-align: right\">%Ld %%</td></tr></tbody>\n",
2061 sum->i,(100*sum->i+50)/active_classes,sum->l,(100*sum->l+50)/total_traffic);
2062 }
2063
2064 fprintf(f,"<thead><tr><th><a class=\"blue\" target=\"_blank\" href=\"%sERP.log\">All users, all traffic</a></th>\n", log_url);
2065 fprintf(f,"<th style=\"text-align: right\">%d</th>\n\
2066 <th style=\"text-align: right\">100 %%</th>\n\
2067 <th style=\"text-align: right\">%Lu MB</th>\n\
2068 <th style=\"text-align: right\">100 %%</th></tr>\n",active_classes,total_traffic);
2069 fputs("</thead></table>\n", f);
2070
2071 /* write basic ERP data to log directory */
2072 if(!just_preview)
2073 {
2074 FILE *iplog;
2075 sprintf(str,"%s/ERP.log",log_dir);
2076 iplog=fopen(str,"a");
2077 if(iplog)
2078 {
2079 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",
2080 time(NULL), top20_count, top20_perc1, top20_sum, top20_perc2,
2081 active_classes, total_traffic, i, limit_count, prio_count, d); /* d = date*/
2082 fclose(iplog);
2083 }
2084 else
2085 {
2086 perror(str);
2087 }
2088 }
2089 }
2090
2091 fprintf(f, stats_html_signature, version);
2092 fclose(f);
2093 }
2094
2095 if(just_preview)
2096 {
2097 char swchar='p';
2098 if(start_shaping)
2099 {
2100 swchar='s';
2101 }
2102 printf("Statistics preview generated (-%c switch) - now exiting ...\n", swchar);
2103 exit(0);
2104 }
2105
2106 i=0;
2107 #ifdef DEBUG
2108 printf("%-22s %-15s mark\n","name","ip");
2109 #endif
2110
2111 printf("Writing %s ... ", classmap);
2112 f = fopen(classmap, "w");
2113 if(f < 0)
2114 {
2115 perror(classmap);
2116 }
2117
2118 /*-----------------------------------------------------------------*/
2119 puts("Generating iptables and tc classes ... ");
2120 /*-----------------------------------------------------------------*/
2121
2122 for_each(ip, ips) if(ip->mark > 0)
2123 {
2124 if(idxs)
2125 {
2126 char *buf;
2127 duplicate(ip->addr,buf);
2128 buf=hash_id(ip->addr,32-idxtable_bitmask1);
2129
2130 string(chain_forward,6+strlen(buf));
2131 strcpy(chain_forward,"forw_");
2132 strcat(chain_forward,buf);
2133
2134 string(chain_postrouting,6+strlen(buf));
2135 strcpy(chain_postrouting,"post_");
2136 strcat(chain_postrouting,buf);
2137
2138 free(buf);
2139 }
2140 else
2141 {
2142 chain_forward="FORWARD";
2143 chain_postrouting="POSTROUTING";
2144 }
2145
2146 #ifdef DEBUG
2147 printf("%-22s %-16s %04d ", ip->name, ip->addr, ip->mark);
2148 #endif
2149
2150 /* -------------------------------------------------------- mark download */
2151
2152 sprintf(str, "-A %s -d %s/32 -o %s -j %s%d",
2153 chain_postrouting, ip->addr, lan, mark_iptables, ip->mark);
2154 /*sprintf(str,"-A %s -d %s/32 -o %s -j MARK --set-mark %d",chain_postrouting,ip->addr,lan,ip->mark);*/
2155 /* -m limit --limit 1/s */
2156 save_line(str);
2157
2158 if(qos_proxy)
2159 {
2160 sprintf(str, "-A %s -s %s -p tcp --sport %d -d %s/32 -o %s -j %s%d",
2161 chain_postrouting, proxy_ip, proxy_port, ip->addr, lan, mark_iptables, ip->mark);
2162 /*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);*/
2163 save_line(str);
2164 }
2165
2166 sprintf(str, "-A %s -d %s/32 -o %s -j ACCEPT",
2167 chain_postrouting, ip->addr, lan);
2168 save_line(str);
2169
2170 /* -------------------------------------------------------- mark upload */
2171 sprintf(str, "-A %s -s %s/32 -o %s -j %s%d",
2172 chain_forward, ip->addr, wan, mark_iptables, ip->mark);
2173 /* sprintf(str,"-A %s -s %s/32 -o %s -j MARK --set-mark %d",chain_forward,ip->addr,wan,ip->mark);*/
2174 save_line(str);
2175
2176 sprintf(str, "-A %s -s %s/32 -o %s -j ACCEPT",
2177 chain_forward, ip->addr, wan);
2178 save_line(str);
2179
2180 if(ip->min)
2181 {
2182 /* -------------------------------------------------------- download class */
2183 #ifdef DEBUG
2184 printf("(down: %dk-%dk ", ip->min, ip->max);
2185 #endif
2186
2187 sprintf(str, "%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
2188 tc, lan, ip->group, ip->mark,ip->min,ip->max, burst, ip->prio);
2189 safe_run(str);
2190
2191 if(strcmpi(ip->keyword->leaf_discipline, "none"))
2192 {
2193 sprintf(str, "%s qdisc add dev %s parent 1:%d handle %d %s",
2194 tc, lan, ip->mark, ip->mark, ip->keyword->leaf_discipline); /*qos_leaf*/
2195 safe_run(str);
2196 }
2197
2198 if(filter_type == 1)
2199 {
2200 sprintf(str, "%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d",
2201 tc, lan, ip->mark, ip->mark);
2202 safe_run(str);
2203 }
2204
2205 /* -------------------------------------------------------- upload class */
2206 #ifdef DEBUG
2207 printf("up: %dk-%dk)\n", (int)((ip->min/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed),
2208 (int)((ip->max/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed));
2209 #endif
2210
2211 sprintf(str,"%s class add dev %s parent 1:%d classid 1:%d htb rate %dkbit ceil %dkbit burst %dk prio %d",
2212 tc, wan, ip->group, ip->mark,
2213 (int)((ip->min/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed),
2214 (int)((ip->max/ip->keyword->asymetry_ratio)-ip->keyword->asymetry_fixed), burst, ip->prio);
2215 safe_run(str);
2216
2217 if(strcmpi(ip->keyword->leaf_discipline, "none"))
2218 {
2219 sprintf(str, "%s qdisc add dev %s parent 1:%d handle %d %s",
2220 tc, wan, ip->mark, ip->mark, ip->keyword->leaf_discipline); /*qos_leaf*/
2221 safe_run(str);
2222 }
2223
2224 if(filter_type == 1)
2225 {
2226 sprintf(str, "%s filter add dev %s parent 1:0 protocol ip handle %d fw flowid 1:%d",
2227 tc, wan, ip->mark, ip->mark);
2228 safe_run(str);
2229 }
2230
2231 if(f > 0)
2232 {
2233 fprintf(f, "%s %d\n", ip->addr, ip->mark);
2234 }
2235 }
2236 else
2237 {
2238 #ifdef DEBUG
2239 printf("(sharing %s)\n", ip->sharing);
2240 #endif
2241 }
2242 i++;
2243 }
2244 if(f > 0)
2245 {
2246 puts("done.");
2247 fclose(f);
2248 }
2249
2250 if(idxs)
2251 {
2252 chain_forward = "forw_common";
2253 chain_postrouting = "post_common";
2254 }
2255 else
2256 {
2257 chain_forward = "FORWARD";
2258 chain_postrouting = "POSTROUTING";
2259 }
2260 /* -------------------------------- classify or reject free download */
2261 {
2262 char *final_chain = "DROP"; /* REJECT would be better, but it is impossible in mangle */
2263 if(free_min)
2264 {
2265 final_chain = "ACCEPT";
2266 }
2267 if(qos_proxy)
2268 {
2269 if(free_min)
2270 {
2271 sprintf(str,"-A %s -s %s -p tcp --sport %d -o %s -j %s%d",
2272 chain_postrouting,proxy_ip,proxy_port,lan,mark_iptables,3);
2273 save_line(str);
2274 }
2275 sprintf(str,"-A %s -s %s -p tcp --sport %d -o %s -j %s",
2276 chain_postrouting,proxy_ip,proxy_port,lan,final_chain);
2277 save_line(str);
2278 }
2279 if(free_min)
2280 {
2281 sprintf(str,"-A %s -o %s -j %s%d", chain_postrouting, lan, mark_iptables, 3);
2282 save_line(str);
2283 }
2284 sprintf(str,"-A %s -o %s -j %s", chain_postrouting, lan, final_chain);
2285 save_line(str);
2286 /* ------------------------------- classify or reject free upload */
2287 if(free_min)
2288 {
2289 sprintf(str,"-A %s -o %s -j %s%d", chain_forward, wan, mark_iptables, 3);
2290 save_line(str);
2291 }
2292 sprintf(str,"-A %s -o %s -j %s", chain_forward, wan, final_chain);
2293 save_line(str);
2294 }
2295
2296 if(free_min) /* allocate free bandwith if it is not zero... */
2297 {
2298 /*-----------------------------------------------------------------*/
2299 puts("Generating free bandwith classes ...");
2300 /*-----------------------------------------------------------------*/
2301 sprintf(str, "%s class add dev %s parent 1:%d classid 1:3 htb rate %dkbit ceil %dkbit burst %dk prio %d",
2302 tc, lan, parent, free_min, free_max,burst, lowest_priority);
2303 safe_run(str);
2304 sprintf(str, "%s class add dev %s parent 1:%d classid 1:3 htb rate %dkbit ceil %dkbit burst %dk prio %d",
2305 tc, wan, parent, free_min, free_max, burst, lowest_priority);
2306 safe_run(str);
2307 /* tc SFQ */
2308 if(strcmpi(qos_leaf, "none"))
2309 {
2310 sprintf(str,"%s qdisc add dev %s parent 1:3 handle 3 %s", tc, lan, qos_leaf);
2311 safe_run(str);
2312
2313 sprintf(str,"%s qdisc add dev %s parent 1:3 handle 3 %s", tc, wan, qos_leaf);
2314 safe_run(str);
2315 }
2316 /* tc handle 1 fw flowid */
2317 sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle 3 fw flowid 1:3", tc, lan);
2318 safe_run(str);
2319
2320 sprintf(str,"%s filter add dev %s parent 1:0 protocol ip handle 3 fw flowid 1:3", tc, wan);
2321 safe_run(str);
2322 }
2323 printf("Total IP count: %d\n", i);
2324 run_restore();
2325 if(log_file)
2326 {
2327 fclose(log_file);
2328 }
2329 return 0;
2330 /* that's all folks, thank you for reading it all the way up to this point ;-) */
2331 /* bad luck C<<1 is not yet finished, I promise no sprintf() next time... */
2332 }
This page took 3.491228 seconds and 4 git commands to generate.