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