forked from IvnSoft/mod_bw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_bw.c
1322 lines (1102 loc) · 40.9 KB
/
mod_bw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Apache2 - Mod_bw v0.92
Author : Ivan Barrera A. (Bruce)
HomePage : Http://Ivn.cl/apache
Release Date : 20-Jul-2010
Status : Stable
License : Licensed under the Apache Software License v2.0
It must be included as LICENSE in this package.
Platform : Linux/x86 (Tested with Fedora Core 4, Suse, etc)
Linux/x86_64 (Redhat Enterprise 4)
FreeBSD/x86 (Tested on 5.2)
MacOS X/Ppc x86 (Tested on both platforms)
Solaris 8/sparc (Some notes on compile)
Microsoft Windows (Win XP, Win2003. Win7, Others should work)
HP-UX 11 (Thanks to Soumendu Bhattacharya for testing)
Notes : This is a stable version of mod_bw. It should work with
almost any MPM (tested with WinNT/prefork/Worker MPM).
We are reaching the End of mod_bw series 0.x. As soon as this
last changes are confirmed by the users (perhaps some changes
at request), i'll set this release to version 1.0 final.
Limitations : This mod doesn't know how fast the client is
downloading a file, so it just divides the bw assigned
between the users.
MaxConnections works only for the given scope. (i.e , all
will limit maxconnections from all,not per ip or user)
Changelog :
2019-07-04 : Update to make it work with the new api of apache 2.4. See https://httpd.apache.org/docs/current/developer/new_api_2_4.html
2010-07-20 : Fixed ap_get_server_banner unknown on older apache version
2010-05-27 : Fixed weird behaviour on Windows Hosts. (mod_bw.txt)
Added high resolution timers for windows. (speed improvements)
Fixed stupid bug that caused crash when mod is enabled but there is
not a single limit.
2010-05-24 : Code Cleanup. No more warnings or stuff in Visual Studio
2010-04-28 : Bruce's Birthday Gift : A callback to the stats of the mod :)
2010-04-06 : Fixed "Invisible" memory leak. Only seen when serving HUGE streams.
2010-03-10 : Fixed MinBandwidth screwing limits. Another unsigned/signed screw up.
*/
#define VERSION "0.93"
#include "apr_version.h"
#include "apr_buckets.h"
#include "apr_strings.h"
#include "apr_atomic.h"
#include "apr_lib.h"
#include "apr_shm.h"
#include "ap_config.h"
#include "util_filter.h"
#include "ap_mpm.h"
#include "httpd.h"
#include "http_config.h"
#include "http_request.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_log.h"
#include "http_main.h"
#include "util_script.h"
#include "http_core.h"
#include "scoreboard.h"
#if defined(WIN32)
#include <windows.h>
#include <mmsystem.h>
#endif
#define MIN_BW 256 /* Minimal bandwidth 256 bytes */
#define PACKET 8192 /* Default packet at 8192 bytes */
#define MAX_VHOSTS 1024 /* Default number of vhosts to show in stats */
#define MAX_BUF 1024 /* Max length of a temporal buffer */
#define BANDWIDTH_DISABLED 1<<0
#define BANDWIDTH_ENABLED 1<<1
/* Compatibility with regex on apache less than 2.1 */
#if !AP_MODULE_MAGIC_AT_LEAST(20050127,0)
typedef regex_t ap_regex_t;
#define AP_REG_EXTENDED REG_EXTENDED
#define AP_REG_ICASE REG_ICASE
#endif
/* Compatibility with obsolete ap_get_server_version() */
#if !AP_MODULE_MAGIC_AT_LEAST(20051115,4)
#define ap_get_server_banner ap_get_server_version
#endif
/* Compatibility for APR < 1 */
#if ( defined(APR_MAJOR_VERSION) && (APR_MAJOR_VERSION < 1) )
#define apr_atomic_inc32 apr_atomic_inc
#define apr_atomic_dec32 apr_atomic_dec
#define apr_atomic_add32 apr_atomic_add
#define apr_atomic_cas32 apr_atomic_cas
#define apr_atomic_set32 apr_atomic_set
#endif
/* Enum types of "from address" */
enum from_type {
T_ALL,
T_IP,
T_HOST,
T_AGENT
};
/*
- Stats of each conf
-
- id = Configuration ID
- v_name = Vhost name
- time = Time of the last data update
- bandwidth = Estimated bandwidth measured
- bytes_count = Bytes sent last second
- connection_ = Number of simultaneos clientes downloading
- lock = Lock, to avoid simultaneous write access to shm
*/
typedef struct
{
apr_uint32_t id;
char *v_name;
apr_uint32_t connection_count;
apr_uint32_t bandwidth;
apr_uint32_t bytes_count;
apr_uint32_t counter;
volatile apr_uint32_t lock;
apr_time_t time;
} bw_data;
/* A temporal context to save our splitted brigade */
typedef struct ctx_struct_t
{
apr_bucket_brigade *bb;
struct timeval wait;
} ctx_struct;
/* With sid we count the shared memory needed.
BwBase, is a holder to the shared memory base addres */
static char *vnames[MAX_VHOSTS];
static int sid = 0;
bw_data *bwbase;
apr_shm_t *shm;
/* Limits for MaxConnections based on directory */
typedef struct
{
apr_uint32_t sid;
union {
char *from;
apr_ipsubnet_t *ip;
} x;
ap_regex_t *agent;
enum from_type type;
apr_uint32_t max;
} bw_maxconn;
/* Limits for bandwidth and minimal bandwidth based on directory */
typedef struct
{
apr_uint32_t sid;
union {
char *from;
apr_ipsubnet_t *ip;
} x;
ap_regex_t *agent;
enum from_type type;
apr_int32_t rate;
} bw_entry;
/* Limits for bandwidth based on file size */
typedef struct
{
apr_uint32_t sid;
char *file;
apr_uint32_t size;
apr_uint32_t rate;
} bw_sizel;
/* Per directory configuration structure */
typedef struct
{
apr_array_header_t *limits;
apr_array_header_t *minlimits;
apr_array_header_t *sizelimits;
apr_array_header_t *maxconnection;
int packet;
int error;
char *directory;
} bandwidth_config;
/* Per server configuration structure */
typedef struct
{
int state;
int force;
} bandwidth_server_config;
/* Module declaration */
module AP_MODULE_DECLARE_DATA bw_module;
/*---------------------------------------------------------------------*
* Configurations Directives *
*---------------------------------------------------------------------*/
/* Set the mod enabled ... or disabled */
static const char *bandwidthmodule(cmd_parms * cmd, void *dconf, int flag)
{
bandwidth_server_config *sconf;
sconf =
(bandwidth_server_config *) ap_get_module_config(cmd->server->
module_config,
&bw_module);
sconf->state = (flag ? BANDWIDTH_ENABLED : BANDWIDTH_DISABLED);
return NULL;
}
/* Set force mode enabled ... or disabled */
static const char *forcebandwidthmodule(cmd_parms * cmd, void *dconf,
int flag)
{
bandwidth_server_config *sconf;
sconf =
(bandwidth_server_config *) ap_get_module_config(cmd->server->
module_config,
&bw_module);
sconf->force = (flag ? BANDWIDTH_ENABLED : BANDWIDTH_DISABLED);
return NULL;
}
/* Set the packetsize used in the context */
static const char *setpacket(cmd_parms * cmd, void *s, const char *pack)
{
bandwidth_config *conf = (bandwidth_config *) s;
int temp;
if (pack && *pack && apr_isdigit(*pack))
temp = atol(pack);
else
return "Invalid argument";
if ((temp < 1024) || (temp > 131072))
return "Packet must be a number of bytes between 1024 and 131072";
conf->packet = temp;
return NULL;
}
/* Set the error to send when maxconnections is reached */
static const char *bandwidtherror(cmd_parms * cmd, void *s, const char *err)
{
bandwidth_config *conf = (bandwidth_config *) s;
int temp;
if (err && *err && apr_isdigit(*err))
temp = atol(err);
else
return "Invalid argument";
if ((temp < 300) || (temp > 999))
return "Error must be a number between 300 and 599";
conf->error = temp;
return NULL;
}
/* Set the maxconnections on a per host basis */
static const char *maxconnection(cmd_parms * cmd, void *s, const char *from,
const char *maxc)
{
bandwidth_config *conf = (bandwidth_config *) s;
bw_maxconn *a;
int temp;
char *str;
char *where = (char *) apr_pstrdup(cmd->pool, from);
apr_status_t rv;
char msgbuf[MAX_BUF];
if (maxc && *maxc && apr_isdigit(*maxc))
temp = atoi(maxc);
else
return "Invalid argument";
if (temp < 0)
return
"Connections must be a number of simultaneous connections allowed/s";
a = (bw_maxconn *) apr_array_push(conf->maxconnection);
a->x.from = where;
if (!strncasecmp(where,"u:",2))
{
/* Do not limit based on origin, but on user agent */
a->type = T_AGENT;
a->agent = ap_pregcomp(cmd->pool, where+2, 0);
if (a->agent == NULL)
return "Regular expression could not be compiled.";
}
else if (!strcasecmp(where, "all")) {
a->type = T_ALL;
}
else if ((str = strchr(where, '/'))) {
*str++ = '\0';
rv = apr_ipsubnet_create(&a->x.ip, where, str, cmd->pool);
if(APR_STATUS_IS_EINVAL(rv)) {
/* looked nothing like an IP address */
return "An IP address was expected";
}
else if (rv != APR_SUCCESS) {
apr_strerror(rv, msgbuf, sizeof msgbuf);
return apr_pstrdup(cmd->pool, msgbuf);
}
a->type = T_IP;
}
else if (!APR_STATUS_IS_EINVAL(rv = apr_ipsubnet_create(&a->x.ip, where, NULL, cmd->pool))) {
if (rv != APR_SUCCESS) {
apr_strerror(rv, msgbuf, sizeof msgbuf);
return apr_pstrdup(cmd->pool, msgbuf);
}
a->type = T_IP;
}
else { /* no slash, didn't look like an IP address => must be a host */
a->type = T_HOST;
}
a->max = temp;
return NULL;
}
/* Set the bandwidth on a per host basis */
static const char *bandwidth(cmd_parms * cmd, void *s, const char *from,
const char *bw)
{
bandwidth_config *conf = (bandwidth_config *) s;
bw_entry *a;
long int temp;
char *str;
char *where = (char *) apr_pstrdup(cmd->pool, from);
apr_status_t rv;
char msgbuf[MAX_BUF];
if (bw && *bw && apr_isdigit(*bw))
temp = atol(bw);
else
return "Invalid argument";
if (temp < 0)
return "BandWidth must be a number of bytes/s";
a = (bw_entry *) apr_array_push(conf->limits);
a->x.from = where;
if (!strncasecmp(where,"u:",2))
{
/* Do not limit based on origin, but on user agent */
a->type = T_AGENT;
a->agent = ap_pregcomp(cmd->pool, where+2, 0);
if (a->agent == NULL)
return "Regular expression could not be compiled.";
}
else if (!strcasecmp(where, "all")) {
a->type = T_ALL;
}
else if ((str = strchr(where, '/'))) {
*str++ = '\0';
rv = apr_ipsubnet_create(&a->x.ip, where, str, cmd->pool);
if(APR_STATUS_IS_EINVAL(rv)) {
/* looked nothing like an IP address */
return "An IP address was expected";
}
else if (rv != APR_SUCCESS) {
apr_strerror(rv, msgbuf, sizeof msgbuf);
return apr_pstrdup(cmd->pool, msgbuf);
}
a->type = T_IP;
}
else if (!APR_STATUS_IS_EINVAL(rv = apr_ipsubnet_create(&a->x.ip, where, NULL, cmd->pool))) {
if (rv != APR_SUCCESS) {
apr_strerror(rv, msgbuf, sizeof msgbuf);
return apr_pstrdup(cmd->pool, msgbuf);
}
a->type = T_IP;
}
else { /* no slash, didn't look like an IP address => must be a host */
a->type = T_HOST;
}
if (sid < MAX_VHOSTS)
{
vnames[sid] = apr_pcalloc(cmd->pool,apr_snprintf(msgbuf,MAX_BUF,"%s,%s",cmd->server->server_hostname,where) );
vnames[sid] = (char *) apr_pstrdup(cmd->pool, msgbuf);
}
a->rate = temp;
a->sid = sid++;
return NULL;
}
/* Set the minimum bandwidth to send */
static const char *minbandwidth(cmd_parms * cmd, void *s, const char *from,
const char *bw)
{
bandwidth_config *conf = (bandwidth_config *) s;
bw_entry *a;
long int temp;
char *str;
char *where = (char *) apr_pstrdup(cmd->pool, from);
apr_status_t rv;
char msgbuf[MAX_BUF];
if (bw && *bw && (*bw == '-' || apr_isdigit(*bw)))
temp = atol(bw);
else
return "Invalid argument";
a = (bw_entry *) apr_array_push(conf->minlimits);
a->x.from = where;
if (!strncasecmp(where,"u:",2))
{
/* Do not limit based on origin, but on user agent */
a->type = T_AGENT;
a->agent = ap_pregcomp(cmd->pool, where+2, 0);
if (a->agent == NULL)
return "Regular expression could not be compiled.";
}
else if (!strcasecmp(where, "all")) {
a->type = T_ALL;
}
else if ((str = strchr(where, '/'))) {
*str++ = '\0';
rv = apr_ipsubnet_create(&a->x.ip, where, str, cmd->pool);
if(APR_STATUS_IS_EINVAL(rv)) {
/* looked nothing like an IP address */
return "An IP address was expected";
}
else if (rv != APR_SUCCESS) {
apr_strerror(rv, msgbuf, sizeof msgbuf);
return apr_pstrdup(cmd->pool, msgbuf);
}
a->type = T_IP;
}
else if (!APR_STATUS_IS_EINVAL(rv = apr_ipsubnet_create(&a->x.ip, where, NULL, cmd->pool))) {
if (rv != APR_SUCCESS) {
apr_strerror(rv, msgbuf, sizeof msgbuf);
return apr_pstrdup(cmd->pool, msgbuf);
}
a->type = T_IP;
}
else { /* no slash, didn't look like an IP address => must be a host */
a->type = T_HOST;
}
a->rate = temp;
return NULL;
}
/* Set the large file bandwidth limit */
static const char *largefilelimit(cmd_parms * cmd, void *s, const char *file,
const char *size, const char *bw)
{
bandwidth_config *conf = (bandwidth_config *) s;
bw_sizel *a;
long int temp, tsize;
char msgbuf[MAX_BUF];
if (strlen(file) < 1)
return "You must enter a filetype (use * for all)";
if (bw && *bw && (*bw == '-' || apr_isdigit(*bw)))
temp = atol(bw);
else
return "Invalid argument";
if (size && *size && apr_isdigit(*size))
tsize = atol(size);
else
return "Invalid argument";
if (temp < 0)
return "BandWidth must be a number of bytes/s";
if (tsize < 0)
return "File size must be a number of Kbytes";
a = (bw_sizel *) apr_array_push(conf->sizelimits);
a->file = (char *) file;
a->size = tsize;
a->rate = temp;
if (sid < MAX_VHOSTS)
{
vnames[sid] = apr_pcalloc(cmd->pool,apr_snprintf(msgbuf,MAX_BUF,"%s,%s",cmd->server->server_hostname,file) );
vnames[sid] = (char *) apr_pstrdup(cmd->pool, msgbuf);
}
a->sid = sid++;
return NULL;
}
/*----------------------------------------------------------------------------*
* Helper Functions *
*----------------------------------------------------------------------------*/
/* Match the input, as part of a domain */
static int in_domain(const char *domain, const char *what)
{
int dl = strlen(domain);
int wl = strlen(what);
if ((wl - dl) >= 0) {
if (strcasecmp(domain, &what[wl - dl]) != 0)
return 0;
/* Make sure we matched an *entire* subdomain --- if the user
* said 'allow from good.com', we don't want people from nogood.com
* to be able to get in.
*/
if (wl == dl)
return 1; /* matched whole thing */
else
return (domain[0] == '.' || what[wl - dl - 1] == '.');
}
else
return 0;
}
/* Get the bandwidth limit based on from address */
static long get_bw_rate(request_rec * r, apr_array_header_t * a)
{
bw_entry *e = (bw_entry *) a->elts;
const char *remotehost = NULL;
int i;
int gothost = 0;
const char *uastr = NULL;
for (i = 0; i < a->nelts; i++) {
switch (e[i].type) {
case T_AGENT:
uastr = apr_table_get(r->headers_in, "User-Agent");
if (e[i].agent && uastr && ap_regexec(e[i].agent, uastr, 0, NULL, 0)==0 )
return (e[i].rate);
break;
case T_ALL:
return e[i].rate;
case T_IP:
if (apr_ipsubnet_test(e[i].x.ip, r->connection->client_addr)) {
return e[i].rate;
}
break;
case T_HOST:
if (!gothost) {
int remotehost_is_ip;
remotehost = ap_get_remote_host(r->connection, r->per_dir_config,
REMOTE_DOUBLE_REV, &remotehost_is_ip);
if ((remotehost == NULL) || remotehost_is_ip)
gothost = 1;
else
gothost = 2;
}
if ((gothost == 2) && in_domain(e[i].x.from, remotehost))
return (e[i].rate);
break;
}
}
return 0;
}
/*
Match the pattern with the last digist from filename
An asterisk means any.
*/
static int match_ext(const char *file, char *match)
{
if (file == NULL || match == NULL)
return 0;
if (strlen(match) > strlen(file))
return 0;
if (strncmp(match, "*", 1) == 0)
return 1;
file += strlen(file) - strlen(match);
if (strncmp(match, file, strlen(match)) == 0)
return 1;
return 0;
}
/* Get the bandwidth limit based on filesize */
static long get_bw_filesize(request_rec * r, apr_array_header_t * a,
apr_uint32_t filesize, const char *filename)
{
bw_sizel *e = (bw_sizel *) a->elts;
int i;
apr_uint32_t tmpsize = 0, tmprate = 0;
if (!filesize)
return (0);
filesize /= 1024;
for (i = 0; i < a->nelts; i++) {
if ((e[i].size <= filesize) && match_ext(filename, e[i].file))
if (tmpsize <= e[i].size) {
tmpsize = e[i].size;
tmprate = e[i].rate;
}
}
return (tmprate);
}
/* Get the MaxConnections allowed */
static int get_maxconn(request_rec * r, apr_array_header_t * a)
{
bw_maxconn *e = (bw_maxconn *) a->elts;
const char *remotehost = NULL;
int i;
int gothost = 0;
const char *uastr = NULL;
for (i = 0; i < a->nelts; i++) {
switch (e[i].type) {
case T_AGENT:
uastr = apr_table_get(r->headers_in, "User-Agent");
if (e[i].agent && uastr && ap_regexec(e[i].agent, uastr, 0, NULL, 0)==0 )
return (e[i].max);
break;
case T_ALL:
return e[i].max;
case T_IP:
if (apr_ipsubnet_test(e[i].x.ip, r->connection->client_addr)) {
return e[i].max;
}
break;
case T_HOST:
if (!gothost) {
int remotehost_is_ip;
remotehost = ap_get_remote_host(r->connection, r->per_dir_config,
REMOTE_DOUBLE_REV, &remotehost_is_ip);
if ((remotehost == NULL) || remotehost_is_ip)
gothost = 1;
else
gothost = 2;
}
if ((gothost == 2) && in_domain(e[i].x.from, remotehost))
return (e[i].max);
break;
}
}
return 0;
}
/* Get an id based on bandwidth limit */
static int get_sid(request_rec * r, apr_array_header_t * a)
{
bw_entry *e = (bw_entry *) a->elts;
const char *remotehost = NULL;
int i;
int gothost = 0;
const char *uastr;
remotehost =
ap_get_remote_host(r->connection, r->per_dir_config, REMOTE_HOST,
NULL);
for (i = 0; i < a->nelts; i++) {
switch (e[i].type) {
case T_AGENT:
uastr = apr_table_get(r->headers_in, "User-Agent");
if (e[i].agent && uastr && ap_regexec(e[i].agent, uastr, 0, NULL, 0)==0 )
return (e[i].sid);
break;
case T_ALL:
return e[i].sid;
case T_IP:
if (apr_ipsubnet_test(e[i].x.ip, r->connection->client_addr)) {
return e[i].sid;
}
break;
case T_HOST:
if (!gothost) {
int remotehost_is_ip;
remotehost = ap_get_remote_host(r->connection, r->per_dir_config,
REMOTE_DOUBLE_REV, &remotehost_is_ip);
if ((remotehost == NULL) || remotehost_is_ip)
gothost = 1;
else
gothost = 2;
}
if ((gothost == 2) && in_domain(e[i].x.from, remotehost))
return (e[i].sid);
break;
}
}
return -1;
}
/* Get an id based on filesize limit */
static int get_f_sid(request_rec * r, apr_array_header_t * a, apr_uint32_t filesize,
const char *filename)
{
bw_sizel *e = (bw_sizel *) a->elts;
int i;
apr_uint32_t tmpsize = 0, tmpsid = -1;
if (!filesize)
return (0);
filesize /= 1024;
for (i = 0; i < a->nelts; i++) {
if ((e[i].size <= filesize) && match_ext(filename, e[i].file))
if (tmpsize <= e[i].size) {
tmpsize = e[i].size;
tmpsid = e[i].sid;
}
}
if (tmpsid < 0)
return -1;
return (tmpsid);
}
/* Update memory (shm) counters, which holds the bw data per context */
static void update_counters(bw_data * bwstat, ap_filter_t * f)
{
apr_time_t nowtime;
/* Refresh only if 1s has passed */
nowtime = apr_time_now();
if (bwstat->time < (nowtime - 1000000)) {
/* And if we got lock */
if (apr_atomic_cas32(&bwstat->lock, 1, 0) == 0) {
/* Calculate bw used in the last timeinterval */
bwstat->bandwidth = (apr_uint32_t) (
(bwstat->bytes_count / (double) (nowtime - bwstat->time)) *
1000000);
/* Reset counters */
bwstat->bytes_count = 0;
/* Set timestamp */
bwstat->time = apr_time_now();
/* Release lock */
apr_atomic_set32(&bwstat->lock, 0);
}
}
}
static int callback(request_rec * r)
{
int t;
bw_data *bwstat;
if (r->header_only) {
return OK;
}
if (r->args && !strncasecmp(r->args,"csv",3))
{
ap_set_content_type(r, "text/plain");
ap_rputs("id,vhost,scope,lock,count,bw,bytes,hits\n",r);
for (t = 0; t < sid; t++) {
bwstat = bwbase + t;
ap_rprintf(r,"%d,%s,%d,%d,%d,%d,%d\n",t,bwstat->v_name,bwstat->lock,bwstat->connection_count,bwstat->bandwidth,bwstat->bytes_count,bwstat->counter);
}
return OK;
}
ap_set_content_type(r, "text/html");
ap_rputs(DOCTYPE_HTML_3_2, r);
ap_rputs("<HTML>\n", r);
ap_rputs(" <HEAD>\n", r);
ap_rputs(" <TITLE>mod_bw Status</TITLE>\n", r);
ap_rputs(" </HEAD>\n", r);
ap_rputs(" <BODY>\n", r);
ap_rputs(" <H1><SAMP>mod_bw</SAMP> : Status callback\n", r);
ap_rputs(" </H1>\n", r);
ap_rputs(" <P>\n", r);
ap_rprintf(r, " Apache HTTP Server version: \"%s\"\n",
ap_get_server_banner());
ap_rputs(" <BR>\n", r);
ap_rprintf(r, " Server built: \"%s\"\n", ap_get_server_built());
ap_rputs(" </P>\n", r);;
ap_rputs(" </UL>\n", r);
for (t = 0; t < sid; t++) {
bwstat = bwbase + t;
/* This inits the struct that will contain current bw use */
ap_rputs("<hr>",r);
ap_rprintf(r,"id : %d <br>",t);
ap_rprintf(r,"name : %s <br>",bwstat->v_name);
ap_rprintf(r,"lock : %d <br>",bwstat->lock);
ap_rprintf(r,"count: %d <br>",bwstat->connection_count);
ap_rprintf(r,"bw : %d <br>",bwstat->bandwidth);
ap_rprintf(r,"bytes: %d <br>",bwstat->bytes_count);
ap_rprintf(r,"hits : %d <br>",bwstat->counter);
}
ap_rputs(" </BODY>\n", r);
ap_rputs("</HTML>\n", r);
return OK;
}
/*----------------------------------------------------------------------------*
* The Handler and the Output Filter. Core of the mod. *
*----------------------------------------------------------------------------*/
/* With this handler, we can *force* the use of the mod. */
static int handle_bw(request_rec * r)
{
bandwidth_server_config *sconf =
(bandwidth_server_config *) ap_get_module_config(r->server->
module_config,
&bw_module);
bandwidth_config *conf =
(bandwidth_config *) ap_get_module_config(r->per_dir_config,
&bw_module);
bw_data *bwstat;
apr_int32_t confid;
/* Only work on main request/no subrequests */
if (r->main)
return DECLINED;
if (strcmp(r->handler, "modbw-handler")==0) return callback(r);
/* Return if module is not enabled */
if (sconf->state == BANDWIDTH_DISABLED)
return DECLINED;
/* Get the ID of the memory space we are using */
confid = get_sid(r, conf->limits);
/* Only if we have a valid space */
if (confid >= 0) {
/* We "get" the data of the current configuration */
bwstat = bwbase + confid;
apr_atomic_add32(&bwstat->counter, 1);
/* If we are too busy, deny connection */
confid = get_maxconn(r, conf->maxconnection);
if ((bwstat->connection_count >= (apr_uint32_t) confid) && (confid > 0))
return conf->error;
}
/* Add the Filter, if in forced mode */
if (sconf->force == BANDWIDTH_ENABLED)
ap_add_output_filter("mod_bw", NULL, r, r->connection);
/* Pass the control */
return DECLINED;
}
static int bw_filter(ap_filter_t * f, apr_bucket_brigade * bb)
{
request_rec *r = f->r;
bandwidth_config *conf =
(bandwidth_config *) ap_get_module_config(r->per_dir_config,
&bw_module);
bandwidth_server_config *sconf =
(bandwidth_server_config *) ap_get_module_config(r->server->
module_config,
&bw_module);
ctx_struct *ctx = f->ctx;
apr_bucket *b = APR_BRIGADE_FIRST(bb);
bw_data *bwstat, *bwmaxconn;
int confid = -1, connid = -1;
apr_size_t packet = conf->packet, bytes = 0;
apr_off_t bblen = 0;
long int bw_rate, bw_min, bw_f_rate, cur_rate = 0, sleep;
const char *buf;
const char *filename;
/* Only work on main request/no subrequests */
if (r->main) {
ap_remove_output_filter(f);
return ap_pass_brigade(f->next, bb);
}
/* Return as fast as possible if the module is not enabled */
if (sconf->state == BANDWIDTH_DISABLED) {
ap_pass_brigade(f->next, bb);
return APR_SUCCESS;
}
/* Get the bw rates */
bw_rate = get_bw_rate(r, conf->limits);
bw_min = get_bw_rate(r, conf->minlimits);
confid = connid = get_sid(r, conf->limits);
/* Get the filename */
filename = r->finfo.fname;
if (filename == NULL)
filename = r->filename;
/* Get the File Rate. r->finfo.size is not used anymore. */
bblen = r->bytes_sent;
bw_f_rate = get_bw_filesize(r, conf->sizelimits, (off_t) bblen, filename);
/* Check if we've got an ilimited client */
if ((bw_rate == 0 && bw_f_rate == 0) || bw_f_rate < 0) {
ap_pass_brigade(f->next, bb);
return APR_SUCCESS;
}
/*
- File size limit has precedence over location limit, if it's slower.
- The minimal bw used, will never be less than the default minimal bw.
- If file size is zero, all files apply
*/
if (bw_f_rate && (bw_rate > bw_f_rate || !bw_rate)) {
confid = get_f_sid(r, conf->sizelimits, (off_t) bblen, filename);
bw_rate = bw_f_rate;
}
if (bw_min < 0)
bw_min = bw_rate;
else if (!bw_min)
bw_min = MIN_BW;
/* Initialize our temporal space */
if (ctx == NULL) {
apr_bucket_alloc_t *bucket_alloc = apr_bucket_alloc_create(f->r->pool);
f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
ctx->bb = apr_brigade_create(f->r->pool, bucket_alloc);
}
/* We "get" the data of the current configuration */
bwstat = bwbase + confid;
/* If we have a valid bandwidth limit per host, get the maxconn limit */
if (connid >= 0)
bwmaxconn = bwbase + connid;
else
bwmaxconn = bwstat;
/* Add 1 active connection to the record */
apr_atomic_inc32(&bwmaxconn->connection_count);
/* Verbose Output */
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"ID: %i Directory : %s Rate : %ld Minimum : %ld Size rate : %ld",
confid, conf->directory, bw_rate, bw_min, bw_f_rate);
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
"clients : %d/%d rate/min : %ld,%ld", bwmaxconn->connection_count,
(connid >= 0) ? get_maxconn(r, conf->maxconnection) : 0,
bw_rate, bw_min);