"SfR Fresh" - the SfR Freeware/Shareware Archive 
Member "mod-cband-0.9.7.5/src/mod_cband.h" of archive mod-cband-0.9.7.5.tgz:
As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting with prefixed line numbers.
Alternatively you can here view or download the uninterpreted source code file.
That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
1 /*
2 * $Id: mod_cband.h,v 1.5 2006/05/28 18:58:37 dembol Exp $
3 *
4 * mod_cband - A per-user, per-virtualhost and per-destination bandwidth limiter for the Apache HTTP Server Version 2
5 *
6 * Copyright (c) 2005 Lukasz Dembinski <dembol@cband.linux.pl>
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25 #include "httpd.h"
26 #include "http_main.h"
27 #include "http_core.h"
28 #include "http_config.h"
29 #include "http_request.h"
30 #include "http_protocol.h"
31 #include "http_log.h"
32 #include "apr_strings.h"
33 #include "apr_file_io.h"
34 #include "apr_file_info.h"
35 #include "apr_signal.h"
36 #include "apr_hash.h"
37 #include "apr_time.h"
38 #include "apr_thread_mutex.h"
39 #include "util_filter.h"
40 #include "util_cfgtree.h"
41 #include <sys/shm.h>
42 #include <unistd.h>
43
44 #include "libpatricia.c"
45
46 #define MAX_CLASS_STR_LEN 16
47 #define MAX_DST_LEN 16
48 #define MAX_VIRTUALHOST_NAME 0x100
49 #define MAX_PERIOD_LEN 0x20
50 #define MAX_TRAFFIC_LEN 0x100
51 #define MAX_REMOTE_HOSTS 8192
52 #define MAX_HASH_TABLE_LEN 0x100
53 #define MAX_SHMEM_SEGMENTS 0x1000
54 #define MAX_SHMEM_ENTRIES 0x1000
55 #define MAX_SLOW_REMOTE_LOOPS 5
56 #define MAX_DELAY_LOOPS 100
57 #define MAX_OVERLIMIT_DELAY 10
58 #define MAX_PULSE_LEN 250000
59 #define MAX_PULSES 4
60 #define MAX_CHUNK_LEN 0x8000
61 #define CONST_PULSE_LEN 1000000
62 #define MAX_SLEEP_TIME 100000
63 #define MAX_REMOTE_HOST_LIFE 10
64 #define MIN_SPEED 1024
65 #define MIN_SLEEP_TIME 50000
66 #define PERIOD_LEN 1
67 #define DEFAULT_REFRESH 15
68 #define CBAND_HANDLER_ALL 0
69 #define CBAND_HANDLER_ME 1
70
71 #if (defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)) || defined(__FreeBSD__)
72 /* union semun is defined by including <sys/sem.h> */
73 #else
74 /* according to X/OPEN we have to define it ourselves */
75 union semun {
76 int val;
77 struct semid_ds *buf;
78 unsigned short *array;
79 struct seminfo *__buf;
80 };
81 #endif
82
83 typedef struct mod_cband_virtualhost_config_entry mod_cband_virtualhost_config_entry;
84 typedef struct mod_cband_user_config_entry mod_cband_user_config_entry;
85 typedef struct mod_cband_class_config_entry mod_cband_class_config_entry;
86
87 typedef struct {
88 unsigned long long total_bytes; /* in bytes - total traffic */
89 unsigned long long class_bytes[DST_CLASS]; /* in bytes - class traffic */
90 unsigned long start_time;
91 long score_flush_count;
92 int was_request;
93 } mod_cband_scoreboard_entry;
94
95 typedef struct {
96 unsigned long kbps, rps, max_conn;
97 } mod_cband_speed;
98
99 typedef struct {
100 mod_cband_speed max_speed;
101 mod_cband_speed over_speed;
102 mod_cband_speed curr_speed;
103 mod_cband_speed remote_speed;
104 unsigned long shared_kbps, shared_connections, total_conn;
105 unsigned long total_last_refresh;
106 unsigned long total_last_time;
107 mod_cband_scoreboard_entry total_usage;
108 float current_TX, old_TX;
109 float current_conn, old_conn;
110 int overlimit;
111 unsigned long time_delta;
112 } mod_cband_shmem_data;
113
114 typedef struct {
115 int shmem_id;
116 int shmem_entry_idx;
117 void *shmem_data;
118 } mod_cband_shmem_segment;
119
120 struct mod_cband_virtualhost_config_entry {
121 char *virtual_name;
122 apr_port_t virtual_port;
123 unsigned virtual_defn_line;
124 char *virtual_limit_exceeded;
125 char *virtual_scoreboard;
126 char *virtual_user;
127 unsigned long virtual_limit; /* in units of *_mult bytes - total limit */
128 unsigned long virtual_class_limit[DST_CLASS]; /* in units of *_mult bytes - class limits */
129 unsigned long refresh_time; /* in seconds */
130 unsigned long slice_len; /* in seconds */
131 unsigned int virtual_limit_mult;
132 unsigned int virtual_class_limit_mult[DST_CLASS];
133 mod_cband_speed virtual_class_speed[DST_CLASS];
134 mod_cband_shmem_data *shmem_data;
135 mod_cband_virtualhost_config_entry *next;
136 };
137
138 struct mod_cband_user_config_entry {
139 char *user_name;
140 char *user_limit_exceeded;
141 char *user_scoreboard;
142 unsigned long user_limit; /* in units of *_mult bytes - total limit */
143 unsigned long user_class_limit[DST_CLASS]; /* in units of *_mult bytes - class limits */
144 unsigned long refresh_time;
145 unsigned long slice_len;
146 unsigned int user_limit_mult;
147 unsigned int user_class_limit_mult[DST_CLASS];
148 mod_cband_speed user_class_speed[DST_CLASS];
149 mod_cband_shmem_data *shmem_data; /* in seconds */
150 mod_cband_user_config_entry *next;
151 };
152
153 struct mod_cband_class_config_entry {
154 char *class_name;
155 unsigned int class_nr;
156 mod_cband_shmem_data *shmem_data;
157 mod_cband_class_config_entry *next;
158 };
159
160 typedef struct mod_cband_remote_host {
161 int used;
162 unsigned long remote_addr;
163 unsigned long remote_conn;
164 unsigned long remote_kbps, remote_max_conn;
165 unsigned long remote_last_time;
166 unsigned long remote_last_refresh;
167 unsigned long remote_total_conn;
168 char *virtual_name;
169 } mod_cband_remote_host;
170
171 typedef struct mod_cband_remote_hosts {
172 int shmem_id;
173 int sem_id;
174 struct mod_cband_remote_host *hosts;
175 } mod_cband_remote_hosts;
176
177 typedef struct {
178 mod_cband_virtualhost_config_entry *next_virtualhost;
179 mod_cband_user_config_entry *next_user;
180 mod_cband_class_config_entry *next_class;
181 apr_pool_t *p;
182 char *default_limit_exceeded;
183 int default_limit_exceeded_code;
184 patricia_tree_t *tree;
185 unsigned long start_time; /* in seconds */
186 int sem_id;
187 mod_cband_shmem_segment shmem_seg[MAX_SHMEM_SEGMENTS];
188 mod_cband_remote_hosts remote_hosts;
189 int shmem_seg_idx;
190 unsigned long score_flush_period;
191 unsigned long random_pulse;
192 unsigned long max_chunk_len;
193 } mod_cband_config_header;
194
195 typedef struct mod_cband_brigade_ctx {
196 apr_bucket_brigade *bb;
197 } mod_cband_brigade_ctx;
198
199 typedef struct {
200 unsigned long limit;
201 unsigned long slice_limit;
202 unsigned long class_limit;
203 unsigned long class_slice_limit;
204 unsigned long long usage;
205 unsigned long long class_usage;
206 unsigned int limit_mult;
207 unsigned int class_limit_mult;
208 char *limit_exceeded;
209 char *scoreboard;
210 } mod_cband_limits_usages;