1 /* ====================================================================
2 * Copyright (c) 1995 The Apache Group. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the Apache Group
19 * for use in the Apache HTTP server project (http://www.apache.org/)."
21 * 4. The names "Apache Server" and "Apache Group" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission.
25 * 5. Redistributions of any form whatsoever must retain the following
27 * "This product includes software developed by the Apache Group
28 * for use in the Apache HTTP server project (http://www.apache.org/)."
30 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
31 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
34 * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
41 * OF THE POSSIBILITY OF SUCH DAMAGE.
42 * ====================================================================
44 * This software consists of voluntary contributions made by many
45 * individuals on behalf of the Apache Group and was originally based
46 * on public domain software written at the National Center for
47 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
48 * For more information on the Apache Group and the Apache HTTP server
49 * project, please see <http://www.apache.org/>.
53 /****************************************************************************
54 * Title : Bandwidth management
55 * File : mod_bandwidth.c
56 * Author : Yann Stettler (stettler@cohprog.com)
58 * Version : 2.0.3 for Apache 1.3+
61 * Provide bandwidth usage limitation either on the whole server or
62 * one a per connection basis based on the size of files, directory
63 * location or remote domain/IP.
65 * Revision : 08/04/97 - "-1" value for LargeFileLimit to disable any
66 * limits for that kind of files.
67 * 01/26/98 - Include in_domain() and in_ip() in this file and
68 * make them static so it will work with Apache 1.3x
69 * 03/29/98 - Change to set bandwidth in VirtualHost directive
70 * 07/17/99 - Minor changes to remove warnings at compil time
72 * Allow the use of the network/mask format when
73 * setting a bandwidth for a remote host/network.
74 * (Thanks to Sami Kuhmonen for the patch)
76 * New directive BandWidthPulse
78 * 10/14/00 - Minor bug fixed
79 * 12/15/00 - Bug fix when using mmap
80 * 08/29/01 - Add a directive to change the data directory
81 * (Thanks to Awesome Walrus <walrus@amur.ru> )
83 ***************************************************************************
84 * Copyright (c)1997 Yann Stettler and CohProg SaRL. All rights reserved.
85 * Written for the Apache Group by :
88 * stettler@cohprog.com
89 * http://www.cohprog.com/
90 * http://www.animanga.com/
92 * Based on the original default_handler module and on bw_module 0.2 from
93 * Etienne BERNARD (eb@via.ecp.fr) at VIA Centrale Reseaux, ECP, France
95 * Many thanks to Colba Net Inc (Montreal) for their sponsoring of
96 * improvements to this module.
98 ***************************************************************************/
105 * Note : this module was writen for Apache 1.3.x and tested on it
109 * 1) Insert the following line at the end of the "Configuration" file :
110 * Module bandwidth_module mod_bandwidth.o
112 * WARNING : This behaviour is not the same between the various main versions
113 * of Apache. Please, read the instruction on our website for the
114 * instructions concerning the latest release :
115 * http://www.cohprog.com/v3/bandwidth/doc-en.html
117 * 2) Run the "Configure" program and re-compile the server with "make".
119 * 3) Create the following directories with "rwx" permission to everybody :
120 * (or rwx for the user under which Apache run : Usualy "nobody")
123 * /tmp/apachebw/master
125 * /==== by Awesome Walrus <walrus@amur.ru> =====================\
126 * Or you may change this name by using BandWidthDataDir global
127 * configuration directive. See below for details.
128 * \==== by Awesome Walrus <walrus@amur.ru> =====================/
130 * Note that if any of those directories doesn't exist, or if they can't
131 * be accessed by the server, the module is totaly disabled except for
132 * logging an error message in the logfile.
134 * Be careful that on some systems the content of the /tmp directory
135 * is deleted at boot time or every so often by a cronjob. If that the
136 * case, either disable this feature or change the location of the
137 * directories used by the module in the sources bellow.
139 * Server configuration directive :
140 * --------------------------------
142 * /==== by Awesome Walrus <walrus@amur.ru> =====================\
144 * Syntax : BandWidthDataDir <directory>
145 * Default : "/tmp/apachebw"
146 * Context : server config
148 * Sets the name of the directory used by mod_bandwidth to store
149 * its internal temporary information.
150 * \==== by Awesome Walrus <walrus@amur.ru> =====================/
153 * Syntax : BandWidthModule <On|Off>
155 * Context : per server config
157 * Enable or disable totaly the whole module. By default, the module is
158 * disable so it is safe to compile it in the server anyway.
160 * PLEASE, NOTE THAT IF YOU SET A BANDWIDTH LIMIT INSIDE A VIRTUALHOST
161 * BLOCK, YOU ALSO __NEED__ TO PUT THE "BandWidthModule On" DIRECTIVE
162 * INSIDE THAT VIRTUALHOST BLOCK !
164 * IF YOU SET BANDWIDTH LIMITS INSIDE DIRECTORY BLOCKS (OUTSIDE OF
165 * ANY VIRTUALHOST BLOCK), YOU ONLY NEED TO PUT THE "BandWidthModule On"
166 * DIRECTIVE ONCE, OUTSIDE OF ANY VIRTUALHOST OR DIRECTORY BLOCK.
169 * Syntax : BandWidthPulse <microseconds>
171 * Context : per server config
173 * Change the algorithm used to calculate bandwidth and transmit data.
174 * In normal mode (old mode), the module try to transmit data in packets
175 * of 1KB. That mean that if the bandwidth available is of 512B, the
176 * module will transmit 1KB, wait 2 seconds, transmit another 1KB and
179 * Seting a value with "BandWidthPulse", will change the algorithm so
180 * that the server will always wait the same amount of time between
181 * sending packets but the size of the packets will change.
182 * The value is in microseconds.
183 * For example, if you set "BandWidthPulse 1000000" (1 sec) and the
184 * bandwidth available is of 512B, the sever will transmit 512B,
185 * wait 1 second, transmit 512B and so on.
187 * The advantage is a smother flow of data. The disadvantage is
188 * a bigger overhead of data transmited for packet header.
189 * Setting too small a value (bellow 1/5 of a sec) is not realy
190 * useful and will put more load on the system and generate more
191 * traffic for packet header.
193 * Note also that the operating system may do some buffering on
194 * it's own and so defeat the purpose of setting small values.
196 * This may be very useful on especialy crowded network connection :
197 * In normal mode, several seconds may happen between the sending of
198 * a full packet. This may lead to timeout or people may believe that
199 * the connection is hanging. Seting a value of 1000000 (1 sec) would
200 * guarantee that some data are sent every seconds...
202 * Directory / Server / Virtual Server configuration directive :
203 * -------------------------------------------------------------
206 * Syntax : BandWidth <domain|ip|all> <rate>
208 * Context : per directory, .htaccess
210 * Limit the bandwidth for files in this directory and
211 * sub-directories based on the remote host <domain> or
212 * <ip> address or for <all> remote hosts.
214 * Ip addresses may now be specified in the network/mask format.
215 * (Ie: 192.168.0.0/21 )
217 * The <rate> is in Bytes/second.
218 * A <rate> of "0" means no bandwidth limit.
220 * Several BandWidth limits can be set for the same
221 * directory to set different limits for different
222 * hosts. In this case, the order of the "BandWidth"
223 * keywords is important as the module will take the
224 * first entry which matches the client address.
227 * <Directory /home/www>
229 * BandWidth 138.195 0
233 * This will limit the bandwith for directory /home/www and
234 * all it's subdirectories to 1024Bytes/sec, except for
235 * *.ecp.fr or 138.195.*.* where no limit is set.
238 * Syntax : LargeFileLimit <filesize> <rate>
240 * Context : per directory, .htaccess
242 * Set a maximal <rate> (in bytes/sec) to use when transfering
243 * a file of <filesize> KBytes or more.
245 * Several "LargeFileLimit" can be set for various files sizes
246 * to create range. The rate used for a given file size will be
247 * the one of the matching range.
249 * A <rate> of "0" mean that there isn't any limit based on
252 * A <rate> of "-1" mean that there isn't any limit for that type
253 * of file. It's override even a BandWidth limit. I found this usefull
254 * to give priority to very small files (html pages, very small pictures)
255 * while seting limits for larger files... (users with their video files
256 * can go to hell ! :)
259 * If the following limits are set :
260 * LargeFileLimit 200 3072
261 * LargeFileLimit 1024 2048
263 * That's mean that a file of less than 200KBytes won't be
264 * limited based on his size. A file with a size between
265 * 200KBytes (included) and 1023Kbytes (included) will be
266 * limited to 3072Bytes/sec and a file of 1024Kbytes or more
267 * will be limited to 2048Bytes/sec.
270 * Syntax : MinBandWidth <domain|ip|all> <rate>
272 * Context : per directory, .htaccess
274 * Set a minimal bandwidth to use for transfering data. This
275 * over-ride both BandWidth and LargeFileLimit rules as well
276 * as the calculated rate based on the number of connections.
278 * The first argument is used in the same way as the first
279 * argument of BandWidth.
281 * <rate> is in bytes per second.
283 * A rate of "0" explicitly means to use the default minimal
284 * value (256 Bytes/sec).
286 * A rate of "-1" means that the minimal rate is equal to the
287 * actual rate defined by BandWidth and LargeFileLimit.
288 * In fact, that means that the final rate won't depend
289 * of the number of connections but only on what was defined.
292 * If BandWidth is set to "3072" (3KBytes/sec) and MinBandWidth
293 * is set to "1024" (1KBytes/sec) that means :
294 * - if there is one connection, the file will be transfered
296 * - if there is two connections, each files will be transfered
298 * - if there is three or more connections, each files will be
299 * transfered at 1024 Bytes/sec. (Minimal of 1024 Bytes/sec).
301 * If MinBandWidth is set to "-1" that means :
302 * - if there is one connection, the file will be transfered
304 * - if there is two or more connections, each files will be
305 * transfered at 3072 Bytes/sec. In effect, the rate doesn't
306 * depend anymore on the number of connections but only on
307 * the configuration values.
309 * Note that the total transfer rate will never exceed your physical
310 * bandwidth limitation.
312 * Note : If both a "BandWidth" and a "LargeFileLimit" limit apply,
313 * the lowest one will be used. (But never lower than the
314 * "MinBandWidth" rate)
316 * If both a virtual server limit is defined and another
317 * apply for a directory under this virtual server, the
318 * directory limit will over-ride it.
320 * If a limit is defined outside a Directory or VirtualHost
321 * directive, it will act as default on a per virtual server
322 * basis. (Ie: each virtual server will have that limit,
323 * _independantly_ of the other servers)
325 * Implementation notes :
326 * ----------------------
328 * 1) This module isn't called when serving a cgi-script. In this
329 * case, it's the functions in mod_cgi.c that handle the connection.
331 * That's not a bug : I didn't want to change the cgi_module and
332 * I was too lazy to do it anyway.
334 * 2) The rate of data transmission is only calculated. It isn't
335 * measured. Which mean that this module calculates the final
336 * rate that should apply for a file and simply divides this
337 * number by the number of actual connections subject to the
338 * same "per directory" directives.
340 * 3) On the "+" side, the module regulate the speed taking
341 * into account the actual time that was needed to send the
342 * data. Which also mean that if data are read by the client
343 * at a slowest rate than the limit, no time will be lost and
344 * data will be sent as fast as the client can read them (but
345 * no faster than the limited rate :) ...
347 * 4) Some kind of buffering is done as side effect. Data are
348 * sent in packet of 1024 Bytes which seems a good value
350 * If another packet size is wanted, change the value of
351 * "#define PACKET" in the codes bellow.
353 * 5) The default value for MinBandWidth is defined by :
354 * "#define MIN_BW_DEFAULT" (in Bytes/sec)
356 * 6) Don't define "BWDEBUG" for a production server :
357 * this would log a _lot_ of useless informations for
364 #include "http_core.h"
365 #include "http_config.h"
366 #include "http_log.h"
367 #include "http_request.h"
368 #include "http_protocol.h"
369 #include "http_main.h"
371 #define MIN_BW_DEFAULT 256 /* Minimal bandwidth defaulted to 256Bps */
372 #define PACKET 1024 /* Sent packet of 1024 bytes */
374 /* #define MASTER_DIR "/tmp/apachebw/master"
375 * #define LINK_DIR "/tmp/apachebw/link"
378 #define MASTER_DIR "master"
379 #define LINK_DIR "link"
381 /* Define BWDEBUG for debuging purpose only ! */
382 /* #define BWDEBUG */
387 #define BANDWIDTH_DISABLED 1<<0
388 #define BANDWIDTH_ENABLED 1<<1
390 #ifdef USE_MMAP_FILES
392 #include <sys/mman.h>
394 /* mmap support for static files based on ideas from John Heidemann's
395 * patch against 1.0.5. See
396 * <http://www.isi.edu/~johnh/SOFTWARE/APACHE/index.html>.
399 /* Files have to be at least this big before they're mmap()d. This is to
400 * deal with systems where the expense of doing an mmap() and an munmap()
401 * outweighs the benefit for small files.
403 #ifndef MMAP_THRESHOLD
405 #define MMAP_THRESHOLD (8*1024)
407 #define MMAP_THRESHOLD 0
412 /* Limits for bandwidth and minimal bandwidth based on directory */
418 /* Limits for bandwidth based on file size */
424 /* Per directory configuration structure */
426 array_header *limits;
427 array_header *minlimits;
428 array_header *sizelimits;
432 /* Per server configuration structure */
435 } bandwidth_server_config;
437 int bw_handler (request_rec *);
439 module bandwidth_module;
441 static long int BWPulse=0;
443 char bandwidth_data_dir[MAX_STRING_LEN]="/tmp/apachebw";
445 /***************************************************************************
446 * Configuration functions *
447 ***************************************************************************/
449 static void *create_bw_config(pool *p, char *path) {
450 bandwidth_config *new=
451 (bandwidth_config *) ap_palloc(p, sizeof(bandwidth_config));
452 new->limits=ap_make_array(p,20,sizeof(bw_entry));
453 new->minlimits=ap_make_array(p,20,sizeof(bw_entry));
454 new->sizelimits=ap_make_array(p,10,sizeof(bw_sizel));
455 new->directory=ap_pstrdup(p, path);
459 static void *create_bw_server_config(pool *p, server_rec *s) {
460 bandwidth_server_config *new;
462 new = (bandwidth_server_config *)ap_pcalloc(p, sizeof(bandwidth_server_config));
464 new->state = BANDWIDTH_DISABLED;
469 /***************************************************************************
470 * Directive functions *
471 ***************************************************************************/
473 static const char *bandwidthmodule(cmd_parms *cmd, bandwidth_config *dconf, int flag) {
474 bandwidth_server_config *sconf;
476 sconf = (bandwidth_server_config *)ap_get_module_config(cmd->server->module_config, &bandwidth_module);
477 sconf->state = (flag ? BANDWIDTH_ENABLED : BANDWIDTH_DISABLED);
482 static const char *set_bandwidth_data_dir(cmd_parms *cmd, void *dummy, char *arg) {
483 arg = ap_os_canonical_filename(cmd->pool, arg);
485 if (!ap_is_directory(arg)) {
486 return "BandWidthDataDir must be a valid directory";
488 ap_cpystrn(bandwidth_data_dir, arg, sizeof(bandwidth_data_dir));
492 static const char *setpulse(cmd_parms *cmd, bandwidth_config *dconf, char *pulse) {
495 if (pulse && *pulse && isdigit(*pulse))
498 return "Invalid argument";
501 return "BandWidth must be a number of bytes/s";
508 static const char *bandwidth(cmd_parms *cmd, void *s, char *from, char *bw) {
509 bandwidth_config *conf=(bandwidth_config *)s;
513 if (bw && *bw && isdigit(*bw))
516 return "Invalid argument";
519 return "BandWidth must be a number of bytes/s";
521 a = (bw_entry *)ap_push_array(conf->limits);
522 a->from = ap_pstrdup(cmd->pool,from);
527 static const char *minbandwidth(cmd_parms *cmd, void *s, char *from, char *bw) {
528 bandwidth_config *conf=(bandwidth_config *)s;
532 if (bw && *bw && (*bw=='-' || isdigit(*bw)))
535 return "Invalid argument";
537 a = (bw_entry *)ap_push_array(conf->minlimits);
538 a->from = ap_pstrdup(cmd->pool,from);
543 static const char *largefilelimit(cmd_parms *cmd, void *s, char *size, char *bw) {
544 bandwidth_config *conf=(bandwidth_config *)s;
546 long int temp, tsize;
548 if (bw && *bw && (*bw=='-' || isdigit(*bw)))
551 return "Invalid argument";
553 if (size && *size && isdigit(*size))
556 return "Invalid argument";
560 return "BandWidth must be a number of bytes/s";
564 return "File size must be a number of Kbytes";
566 a = (bw_sizel *)ap_push_array(conf->sizelimits);
572 static command_rec bw_cmds[] = {
573 { "BandWidth", bandwidth, NULL, RSRC_CONF | OR_LIMIT, TAKE2,
574 "a domain (or ip, or all for all) and a bandwidth limit (in bytes/s)" },
575 { "MinBandWidth", minbandwidth, NULL, RSRC_CONF | OR_LIMIT, TAKE2,
576 "a domain (or ip, or all for all) and a minimal bandwidth limit (in bytes/s)" },
577 { "LargeFileLimit", largefilelimit, NULL, RSRC_CONF | OR_LIMIT, TAKE2,
578 "a filesize (in Kbytes) and a bandwidth limit (in bytes/s)" },
579 { "BandWidthModule", bandwidthmodule, NULL, OR_FILEINFO, FLAG,
580 "On or Off to enable or disable (default) the whole bandwidth module" },
581 { "BandWidthPulse", setpulse, NULL, OR_FILEINFO, TAKE1,
582 "a number of microseconds" },
583 { "BandWidthDataDir", set_bandwidth_data_dir, NULL, RSRC_CONF, TAKE1,
584 "A writable directory where temporary bandwidth info is to be stored" },
588 /***************************************************************************
589 * Internal functions *
590 ***************************************************************************/
592 #ifdef USE_MMAP_FILES
598 static void mmap_cleanup (void *mmv)
600 struct mmap *mmd = mmv;
602 munmap(mmd->mm, mmd->length);
606 static int in_domain(const char *domain, const char *what) {
607 int dl=strlen(domain);
611 if (strcasecmp(domain,&what[wl-dl]) != 0) return 0;
613 /* Make sure we matched an *entire* subdomain --- if the user
614 * said 'allow from good.com', we don't want people from nogood.com
615 * to be able to get in.
618 if (wl == dl) return 1; /* matched whole thing */
619 else return (domain[0] == '.' || what[wl - dl - 1] == '.');
624 static int in_ip(char *domain, char *what) {
626 /* Check a similar screw case to the one checked above ---
627 * "allow from 204.26.2" shouldn't let in people from 204.26.23
630 unsigned long in, out, inmask, outmask;
632 if (sscanf(domain, "%i.%i.%i.%i/%i", &a, &b, &c, &d, &e) < 5) {
633 int l = strlen(domain);
634 if (strncmp(domain,what,l) != 0) return 0;
635 if (domain[l - 1] == '.') return 1;
636 return (what[l] == '\0' || what[l] == '.');
638 in = (a<<24)|(b<<16)|(c<<8)|d;
639 inmask = 0xFFFFFFFF ^ ((1<<(32-e))-1);
641 if (sscanf(what, "%i.%i.%i.%i", &a, &b, &c, &d) < 4)
644 out = (a<<24)|(b<<16)|(c<<8)|d;
646 return ((in&inmask) == (out&inmask));
649 static int is_ip(const char *host)
651 while ((*host == '.') || (*host=='/') || isdigit(*host))
653 return (*host == '\0');
656 static long get_bw_rate(request_rec *r, array_header *a) {
657 bw_entry *e = (bw_entry *)a->elts;
658 const char *remotehost = NULL;
661 remotehost = ap_get_remote_host(r->connection, r->per_dir_config, REMOTE_HOST);
663 for (i=0; i< a->nelts; i++) {
664 if (!strcmp(e[i].from,"all"))
667 if (in_ip(e[i].from, r->connection->remote_ip))
669 if ((remotehost!=NULL) && !is_ip(remotehost)) {
670 if (in_domain(e[i].from,remotehost))
677 static long get_bw_filesize(request_rec *r, array_header *a, off_t filesize) {
678 bw_sizel *e = (bw_sizel *)a->elts;
680 long int tmpsize=0, tmprate=0;
687 for (i=0; i< a->nelts; i++) {
688 if (e[i].size <= filesize )
689 if (tmpsize <= e[i].size) {
698 static int current_connection(char *filename) {
702 * Here, we will do the main work. And that won't take
706 if (stat(filename, &mdata) < 0)
707 return(1); /* strange... should not occure */
709 return(mdata.st_nlink-1);
712 * I told you it wouldn't be long... :)
716 static struct timeval timediff(struct timeval *a, struct timeval *b)
718 struct timeval rslt, tmp;
722 if ((rslt.tv_usec = tmp.tv_usec - b->tv_usec) < 0) {
723 rslt.tv_usec += 1000000;
726 if ((rslt.tv_sec = tmp.tv_sec - b->tv_sec) < 0) {
733 /***************************************************************************
734 * Main function, module core *
735 ***************************************************************************/
737 static int handle_bw(request_rec *r) {
738 int rangestatus, errstatus;
740 bandwidth_config *conf =
741 (bandwidth_config *)ap_get_module_config(r->per_dir_config, &bandwidth_module);
742 bandwidth_server_config *sconf =
743 (bandwidth_server_config *)ap_get_module_config(r->server->module_config, &bandwidth_module);
744 long int bw_rate=0, bw_min=0, bw_f_rate=0, cur_rate;
746 long int tosend, bytessent, filelength;
748 struct timeval opt_time, last_time, now, timespent, timeout;
749 char masterfile[128], filelink[128], *directory;
751 #ifdef USE_MMAP_FILES
755 /* This handler has no use for a request body (yet), but we still
756 * need to read and discard it if the client sent one.
758 if ((errstatus = ap_discard_request_body(r)) != OK)
761 /* Return as fast as possible if request is not a GET or if module not
763 if (r->method_number != M_GET || sconf->state == BANDWIDTH_DISABLED)
766 if (!conf->directory) {
767 directory=(char *)ap_document_root(r);
769 directory=conf->directory;
772 bw_rate=get_bw_rate(r,conf->limits);
773 bw_min=get_bw_rate(r, conf->minlimits);
774 bw_f_rate=get_bw_filesize(r, conf->sizelimits , r->finfo.st_size);
776 if ((bw_rate==0 && bw_f_rate==0) || bw_f_rate < 0 || !directory) return DECLINED;
778 if (r->finfo.st_mode == 0 || (r->path_info && *r->path_info)) {
779 ap_log_reason("File does not exist", r->filename, r);
783 ap_update_mtime (r, r->finfo.st_mtime);
784 ap_set_last_modified(r);
786 if (((errstatus = ap_meets_conditions(r)) != OK)
787 || (errstatus = ap_set_content_length (r, r->finfo.st_size))) {
792 * Create name of the master file.
793 * Will use the inode and device number of the "limited"
797 if (stat(directory, &fdata) < -1) {
798 /* Dunno if this may happen... but well... */
801 sprintf(masterfile,"%s/%s/%d:%ld", bandwidth_data_dir, MASTER_DIR, (short int)fdata.st_dev, (long int)fdata.st_ino);
804 * Check if master file already exist, else create it.
805 * Then we create a hardlink to it using the pid as name.
807 if ((fd=open(masterfile, O_RDONLY|O_CREAT, 0777)) < 0) {
808 ap_log_printf(r->server, "mod_bandwidth : Can't create/access master file %s",masterfile);
813 sprintf(filelink,"%s/%s/%d", bandwidth_data_dir, LINK_DIR, getpid());
814 if (link(masterfile, filelink) < 0) {
815 ap_log_printf(r->server, "mod_bandwidth : Can't create hard link %s", filelink);
819 f = ap_pfopen(r->pool, r->filename,"r");
822 ap_log_error(APLOG_MARK, APLOG_ERR, r->server,
823 "file permissions deny server access: %s", r->filename);
828 * Calculate bandwidth for this file based on location limits and
831 * The following rules applies :
832 * 1) File size limit over-rule location limit if it's slower.
833 * 2) Whatever the resuling limit and number of connections, never
834 * go bellow the minimal limit for this location.
835 * 3) A file size limit of zero mean that we don't care about the
836 * size of the file for this purpose.
841 ap_log_printf(r->server, "mod_bandwidth : Directory : %s Rate : %d Minimum : %d Size rate : %d", directory, bw_rate, bw_min, bw_f_rate);
844 if (bw_f_rate && (bw_rate > bw_f_rate || !bw_rate))
850 bw_min=MIN_BW_DEFAULT;
852 #ifdef USE_MMAP_FILES
854 if ((r->finfo.st_size >= MMAP_THRESHOLD)
855 && ( !r->header_only)) {
856 /* we need to protect ourselves in case we die while we've got the
858 mm = mmap (NULL, r->finfo.st_size, PROT_READ, MAP_PRIVATE,
864 if (mm == (caddr_t)-1) {
867 if (r->finfo.st_size >= MMAP_THRESHOLD) {
868 ap_log_error(APLOG_MARK, APLOG_CRIT, r->server,
869 "mmap_handler: mmap failed: %s", r->filename);
873 rangestatus = ap_set_byterange(r);
874 ap_send_http_header(r);
876 if (!r->header_only) {
878 filelength=r->finfo.st_size;
883 cur_rate=(long int)bw_rate / current_connection(masterfile);
884 if (cur_rate < bw_min)
889 * I feel rather foolish to use select and use the
890 * 1/1000000 of sec for my calculation. But as long as I
891 * am at it, let's do it well...
893 * Note that my loop don't take into account the time
894 * spent outside the send_fd_length function... but in
895 * this case, I feel that I have the moral right to do so :)
898 opt_time.tv_sec=(long int) PACKET / cur_rate;
899 opt_time.tv_usec=(long int)PACKET*1000000/cur_rate-opt_time.tv_sec*1000000;
902 if (tosend+bytessent >= filelength) {
903 tosend=filelength-bytessent;
907 opt_time.tv_sec=(long int)BWPulse/1000000;
908 opt_time.tv_usec=BWPulse-opt_time.tv_sec*1000000;
910 tosend=(long int)((double)BWPulse/(double)1000000*(double)cur_rate);
911 if (tosend+bytessent >= filelength) {
912 tosend=filelength-bytessent;
917 ap_log_printf(r->server, "mod_bandwidth : Current rate : %d [%d/%d] Min : %d",
918 cur_rate, bw_rate, current_connection(masterfile), bw_min);
921 gettimeofday(&last_time, (struct timezone *) 0);
923 ap_send_fd_length(f, r, tosend);
925 if (r->connection->aborted)
929 gettimeofday(&now, (struct timezone *) 0);
930 timespent=timediff(&now, &last_time);
931 timeout=timediff(&opt_time, ×pent);
934 ap_log_printf(r->server, "mod_bandwidth : Sleep : %ld/%ld (Op time : %ld/%ld Spent : %ld/%ld)",
935 timeout.tv_sec, timeout.tv_usec, opt_time.tv_sec, opt_time.tv_usec,
936 timespent.tv_sec, timespent.tv_usec);
939 select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &timeout);
944 while (ap_each_byterange(r, &offset, &length)) {
945 fseek(f, offset, SEEK_SET);
952 cur_rate=(long int)bw_rate / current_connection(masterfile);
953 if (cur_rate < bw_min)
957 opt_time.tv_sec=(long int) PACKET / cur_rate;
958 opt_time.tv_usec=(long int)PACKET*1000000/cur_rate-opt_time.tv_sec*1000000;
961 if (tosend+bytessent >= filelength) {
962 tosend=filelength-bytessent;
966 opt_time.tv_sec=(long int)BWPulse/1000000;
967 opt_time.tv_usec=BWPulse-opt_time.tv_sec*1000000;
969 tosend=(long int)((double)BWPulse/(double)1000000*(double)cur_rate);
970 if (tosend+bytessent >= filelength) {
971 tosend=filelength-bytessent;
977 ap_log_printf(r->server, "mod_bandwidth : Current rate : %d [%d/%d] Min : %d",
978 cur_rate, bw_rate, current_connection(masterfile), bw_min);
980 gettimeofday(&last_time, (struct timezone *) 0);
982 ap_send_fd_length(f, r, tosend);
984 if (r->connection->aborted)
988 gettimeofday(&now, (struct timezone *) 0);
989 timespent=timediff(&now, &last_time);
990 timeout=timediff(&opt_time, ×pent);
993 ap_log_printf(r->server, "mod_bandwidth : Sleep : %ld/%ld (Op time : %ld/%ld Spent : %ld/%ld)",
994 timeout.tv_sec, timeout.tv_usec, opt_time.tv_sec, opt_time.tv_usec,
995 timespent.tv_sec, timespent.tv_usec);
997 select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &timeout);
1004 #ifdef USE_MMAP_FILES
1008 mmd = ap_palloc (r->pool, sizeof (*mmd));
1010 mmd->length = r->finfo.st_size;
1011 ap_register_cleanup (r->pool, (void *)mmd, mmap_cleanup, mmap_cleanup);
1012 ap_unblock_alarms();
1014 rangestatus = ap_set_byterange(r);
1015 ap_send_http_header (r);
1017 if (!r->header_only) {
1019 filelength=r->finfo.st_size;
1024 cur_rate=(long int)bw_rate / current_connection(masterfile);
1025 if (cur_rate < bw_min)
1030 * I feel rather foolish to use select and use the
1031 * 1/1000000 of sec for my calculation. But as long as I
1032 * am at it, let's do it well...
1034 * Note that my loop don't take into account the time
1035 * spent outside the send_fd_length function... but in
1036 * this case, I feel that I have the moral right to do so :)
1039 opt_time.tv_sec=(long int) PACKET / cur_rate;
1040 opt_time.tv_usec=(long int)PACKET*1000000/cur_rate-opt_time.tv_sec*1000000;
1043 if (tosend+bytessent >= filelength) {
1044 tosend=filelength-bytessent;
1048 opt_time.tv_sec=(long int)BWPulse/1000000;
1049 opt_time.tv_usec=BWPulse-opt_time.tv_sec*1000000;
1051 tosend=(long int)((double)BWPulse/(double)1000000*(double)cur_rate);
1052 if (tosend+bytessent >= filelength) {
1053 tosend=filelength-bytessent;
1059 ap_log_printf(r->server, "mod_bandwidth : Current rate : %d [%d/%d] Min : %d Sending : %d",
1060 cur_rate, bw_rate, current_connection(masterfile), bw_min, tosend);
1062 gettimeofday(&last_time, (struct timezone *) 0);
1064 ap_send_mmap(mm, r, bytessent, tosend);
1065 bytessent += tosend;
1066 if (r->connection->aborted)
1070 gettimeofday(&now, (struct timezone *) 0);
1071 timespent=timediff(&now, &last_time);
1072 timeout=timediff(&opt_time, ×pent);
1075 ap_log_printf(r->server, "mod_bandwidth : Sleep : %ld/%ld (Op time : %ld/%ld Spent : %ld/%ld)",
1076 timeout.tv_sec, timeout.tv_usec, opt_time.tv_sec, opt_time.tv_usec,
1077 timespent.tv_sec, timespent.tv_usec);
1079 select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &timeout);
1083 long offset, length;
1084 while (ap_each_byterange(r, &offset, &length)) {
1091 cur_rate=(long int)bw_rate / current_connection(masterfile);
1092 if (cur_rate < bw_min)
1096 opt_time.tv_sec=(long int) PACKET / cur_rate;
1097 opt_time.tv_usec=(long int)PACKET*1000000/cur_rate-opt_time.tv_sec*1000000;
1100 if (tosend+bytessent >= filelength) {
1101 tosend=filelength-bytessent;
1105 opt_time.tv_sec=(long int)BWPulse/1000000;
1106 opt_time.tv_usec=BWPulse-opt_time.tv_sec*1000000;
1108 tosend=(long int)((double)BWPulse/(double)1000000*(double)cur_rate);
1109 if (tosend+bytessent >= filelength) {
1110 tosend=filelength-bytessent;
1116 ap_log_printf(r->server, "mod_bandwidth : Current rate : %d [%d/%d] Min : %d Sending : $d",
1117 cur_rate, bw_rate, current_connection(masterfile), bw_min , tosend);
1119 gettimeofday(&last_time, (struct timezone *) 0);
1121 ap_send_mmap(mm, r, offset+bytessent, tosend);
1122 bytessent += tosend;
1123 if (r->connection->aborted)
1127 gettimeofday(&now, (struct timezone *) 0);
1128 timespent=timediff(&now, &last_time);
1129 timeout=timediff(&opt_time, ×pent);
1132 ap_log_printf(r->server, "mod_bandwidth : Sleep : %ld/%ld (Op time : %ld/%ld Spent : %ld/%ld)",
1133 timeout.tv_sec, timeout.tv_usec, opt_time.tv_sec, opt_time.tv_usec,
1134 timespent.tv_sec, timespent.tv_usec);
1136 select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &timeout);
1145 ap_pfclose(r->pool, f);
1147 /* Remove the hardlink */
1153 static handler_rec bw_handlers[] = {
1154 { "*/*", handle_bw },
1158 module bandwidth_module = {
1159 STANDARD_MODULE_STUFF,
1160 NULL, /* initializer */
1161 create_bw_config, /* bw config creater */
1162 NULL, /* bw merger --- default is to override */
1163 create_bw_server_config, /* server config */
1164 NULL, /* merge server config */
1165 bw_cmds, /* command table */
1166 bw_handlers, /* handlers */
1167 NULL, /* filename translation */
1168 NULL, /* check_user_id */
1169 NULL, /* check auth */
1170 NULL, /* check access */
1171 NULL, /* type_checker */