willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2 | * HA-Proxy : High Availability-enabled HTTP/TCP proxy |
willy tarreau | 726618c | 2006-01-29 22:42:06 +0100 | [diff] [blame] | 3 | * 2000-2006 - Willy Tarreau - willy AT meta-x DOT org. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version |
| 8 | * 2 of the License, or (at your option) any later version. |
| 9 | * |
willy tarreau | 906b268 | 2005-12-17 13:49:52 +0100 | [diff] [blame] | 10 | * Please refer to RFC2068 or RFC2616 for informations about HTTP protocol, and |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 11 | * RFC2965 for informations about cookies usage. More generally, the IETF HTTP |
| 12 | * Working Group's web site should be consulted for protocol related changes : |
| 13 | * |
| 14 | * http://ftp.ics.uci.edu/pub/ietf/http/ |
willy tarreau | 906b268 | 2005-12-17 13:49:52 +0100 | [diff] [blame] | 15 | * |
| 16 | * Pending bugs (may be not fixed because never reproduced) : |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 17 | * - solaris only : sometimes, an HTTP proxy with only a dispatch address causes |
| 18 | * the proxy to terminate (no core) if the client breaks the connection during |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 19 | * the response. Seen on 1.1.8pre4, but never reproduced. May not be related to |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 20 | * the snprintf() bug since requests were simple (GET / HTTP/1.0), but may be |
| 21 | * related to missing setsid() (fixed in 1.1.15) |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 22 | * - a proxy with an invalid config will prevent the startup even if disabled. |
| 23 | * |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 24 | * ChangeLog has moved to the CHANGELOG file. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 25 | * |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 26 | * TODO: |
| 27 | * - handle properly intermediate incomplete server headers. Done ? |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 28 | * - handle hot-reconfiguration |
willy tarreau | 906b268 | 2005-12-17 13:49:52 +0100 | [diff] [blame] | 29 | * - fix client/server state transition when server is in connect or headers state |
| 30 | * and client suddenly disconnects. The server *should* switch to SHUT_WR, but |
| 31 | * still handle HTTP headers. |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 32 | * - remove MAX_NEWHDR |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 33 | * - cut this huge file into several ones |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 34 | * |
| 35 | */ |
| 36 | |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <unistd.h> |
| 40 | #include <string.h> |
| 41 | #include <ctype.h> |
| 42 | #include <sys/time.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <sys/socket.h> |
| 45 | #include <netinet/tcp.h> |
| 46 | #include <netinet/in.h> |
| 47 | #include <arpa/inet.h> |
| 48 | #include <netdb.h> |
| 49 | #include <fcntl.h> |
| 50 | #include <errno.h> |
| 51 | #include <signal.h> |
| 52 | #include <stdarg.h> |
| 53 | #include <sys/resource.h> |
| 54 | #include <time.h> |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 55 | #include <syslog.h> |
willy tarreau | 77bc854 | 2005-12-18 01:31:43 +0100 | [diff] [blame] | 56 | |
| 57 | #ifdef USE_PCRE |
| 58 | #include <pcre.h> |
| 59 | #include <pcreposix.h> |
| 60 | #else |
| 61 | #include <regex.h> |
| 62 | #endif |
| 63 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 64 | #if defined(TPROXY) && defined(NETFILTER) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 65 | #include <linux/netfilter_ipv4.h> |
| 66 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 67 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 68 | #if defined(__dietlibc__) |
| 69 | #include <strings.h> |
| 70 | #endif |
| 71 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 72 | #if defined(ENABLE_POLL) |
| 73 | #include <sys/poll.h> |
| 74 | #endif |
| 75 | |
| 76 | #if defined(ENABLE_EPOLL) |
| 77 | #if !defined(USE_MY_EPOLL) |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 78 | #include <sys/epoll.h> |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 79 | #else |
| 80 | #include "include/epoll.h" |
| 81 | #endif |
| 82 | #endif |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 83 | |
willy tarreau | 779dc89 | 2006-03-19 19:32:29 +0100 | [diff] [blame] | 84 | #ifdef DEBUG_FULL |
| 85 | #include <assert.h> |
| 86 | #endif |
| 87 | |
willy tarreau | 598da41 | 2005-12-18 01:07:29 +0100 | [diff] [blame] | 88 | #include "include/appsession.h" |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 89 | |
willy tarreau | bfad574 | 2006-03-23 14:19:11 +0100 | [diff] [blame] | 90 | #ifndef HAPROXY_VERSION |
willy tarreau | e0dd269 | 2006-03-30 16:27:34 +0200 | [diff] [blame] | 91 | #define HAPROXY_VERSION "1.2.11.1" |
willy tarreau | bfad574 | 2006-03-23 14:19:11 +0100 | [diff] [blame] | 92 | #endif |
| 93 | |
| 94 | #ifndef HAPROXY_DATE |
willy tarreau | e0dd269 | 2006-03-30 16:27:34 +0200 | [diff] [blame] | 95 | #define HAPROXY_DATE "2006/03/30" |
willy tarreau | bfad574 | 2006-03-23 14:19:11 +0100 | [diff] [blame] | 96 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 97 | |
| 98 | /* this is for libc5 for example */ |
| 99 | #ifndef TCP_NODELAY |
| 100 | #define TCP_NODELAY 1 |
| 101 | #endif |
| 102 | |
| 103 | #ifndef SHUT_RD |
| 104 | #define SHUT_RD 0 |
| 105 | #endif |
| 106 | |
| 107 | #ifndef SHUT_WR |
| 108 | #define SHUT_WR 1 |
| 109 | #endif |
| 110 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 111 | /* |
| 112 | * BUFSIZE defines the size of a read and write buffer. It is the maximum |
| 113 | * amount of bytes which can be stored by the proxy for each session. However, |
| 114 | * when reading HTTP headers, the proxy needs some spare space to add or rewrite |
| 115 | * headers if needed. The size of this spare is defined with MAXREWRITE. So it |
| 116 | * is not possible to process headers longer than BUFSIZE-MAXREWRITE bytes. By |
| 117 | * default, BUFSIZE=16384 bytes and MAXREWRITE=BUFSIZE/2, so the maximum length |
| 118 | * of headers accepted is 8192 bytes, which is in line with Apache's limits. |
| 119 | */ |
| 120 | #ifndef BUFSIZE |
| 121 | #define BUFSIZE 16384 |
| 122 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 123 | |
| 124 | // reserved buffer space for header rewriting |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 125 | #ifndef MAXREWRITE |
| 126 | #define MAXREWRITE (BUFSIZE / 2) |
| 127 | #endif |
| 128 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 129 | #define REQURI_LEN 1024 |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 130 | #define CAPTURE_LEN 64 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 131 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 132 | // max # args on a configuration line |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 133 | #define MAX_LINE_ARGS 40 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 134 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 135 | // max # of added headers per request |
| 136 | #define MAX_NEWHDR 10 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 137 | |
| 138 | // max # of matches per regexp |
| 139 | #define MAX_MATCH 10 |
| 140 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 141 | // cookie delimitor in "prefix" mode. This character is inserted between the |
| 142 | // persistence cookie and the original value. The '~' is allowed by RFC2965, |
| 143 | // and should not be too common in server names. |
| 144 | #ifndef COOKIE_DELIM |
| 145 | #define COOKIE_DELIM '~' |
| 146 | #endif |
| 147 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 148 | #define CONN_RETRIES 3 |
| 149 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 150 | #define CHK_CONNTIME 2000 |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 151 | #define DEF_CHKINTR 2000 |
| 152 | #define DEF_FALLTIME 3 |
| 153 | #define DEF_RISETIME 2 |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 154 | #define DEF_CHECK_REQ "OPTIONS / HTTP/1.0\r\n\r\n" |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 155 | |
Willy TARREAU | 13032e7 | 2006-03-12 17:31:45 +0100 | [diff] [blame] | 156 | /* Default connections limit. |
| 157 | * |
| 158 | * A system limit can be enforced at build time in order to avoid using haproxy |
| 159 | * beyond reasonable system limits. For this, just define SYSTEM_MAXCONN to the |
| 160 | * absolute limit accepted by the system. If the configuration specifies a |
| 161 | * higher value, it will be capped to SYSTEM_MAXCONN and a warning will be |
| 162 | * emitted. The only way to override this limit will be to set it via the |
| 163 | * command-line '-n' argument. |
| 164 | */ |
| 165 | #ifndef SYSTEM_MAXCONN |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 166 | #define DEFAULT_MAXCONN 2000 |
Willy TARREAU | 13032e7 | 2006-03-12 17:31:45 +0100 | [diff] [blame] | 167 | #else |
| 168 | #define DEFAULT_MAXCONN SYSTEM_MAXCONN |
| 169 | #endif |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 170 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 171 | /* how many bits are needed to code the size of an int (eg: 32bits -> 5) */ |
| 172 | #define INTBITS 5 |
| 173 | |
| 174 | /* show stats this every millisecond, 0 to disable */ |
| 175 | #ifndef STATTIME |
| 176 | #define STATTIME 2000 |
| 177 | #endif |
| 178 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 179 | /* this reduces the number of calls to select() by choosing appropriate |
| 180 | * sheduler precision in milliseconds. It should be near the minimum |
| 181 | * time that is needed by select() to collect all events. All timeouts |
| 182 | * are rounded up by adding this value prior to pass it to select(). |
| 183 | */ |
| 184 | #define SCHEDULER_RESOLUTION 9 |
| 185 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 186 | #define TIME_ETERNITY -1 |
| 187 | /* returns the lowest delay amongst <old> and <new>, and respects TIME_ETERNITY */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 188 | #define MINTIME(old, new) (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old))) |
| 189 | #define SETNOW(a) (*a=now) |
| 190 | |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 191 | /****** string-specific macros and functions ******/ |
| 192 | /* if a > max, then bound <a> to <max>. The macro returns the new <a> */ |
| 193 | #define UBOUND(a, max) ({ typeof(a) b = (max); if ((a) > b) (a) = b; (a); }) |
| 194 | |
| 195 | /* if a < min, then bound <a> to <min>. The macro returns the new <a> */ |
| 196 | #define LBOUND(a, min) ({ typeof(a) b = (min); if ((a) < b) (a) = b; (a); }) |
| 197 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 198 | /* returns 1 only if only zero or one bit is set in X, which means that X is a |
| 199 | * power of 2, and 0 otherwise */ |
| 200 | #define POWEROF2(x) (((x) & ((x)-1)) == 0) |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 201 | /* |
| 202 | * copies at most <size-1> chars from <src> to <dst>. Last char is always |
| 203 | * set to 0, unless <size> is 0. The number of chars copied is returned |
| 204 | * (excluding the terminating zero). |
| 205 | * This code has been optimized for size and speed : on x86, it's 45 bytes |
| 206 | * long, uses only registers, and consumes only 4 cycles per char. |
| 207 | */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 208 | int strlcpy2(char *dst, const char *src, int size) { |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 209 | char *orig = dst; |
| 210 | if (size) { |
| 211 | while (--size && (*dst = *src)) { |
| 212 | src++; dst++; |
| 213 | } |
| 214 | *dst = 0; |
| 215 | } |
| 216 | return dst - orig; |
| 217 | } |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 218 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 219 | /* |
| 220 | * Returns a pointer to an area of <__len> bytes taken from the pool <pool> or |
| 221 | * dynamically allocated. In the first case, <__pool> is updated to point to |
| 222 | * the next element in the list. |
| 223 | */ |
| 224 | #define pool_alloc_from(__pool, __len) ({ \ |
| 225 | void *__p; \ |
| 226 | if ((__p = (__pool)) == NULL) \ |
| 227 | __p = malloc(((__len) >= sizeof (void *)) ? (__len) : sizeof(void *)); \ |
| 228 | else { \ |
| 229 | __pool = *(void **)(__pool); \ |
| 230 | } \ |
| 231 | __p; \ |
| 232 | }) |
| 233 | |
| 234 | /* |
| 235 | * Puts a memory area back to the corresponding pool. |
| 236 | * Items are chained directly through a pointer that |
| 237 | * is written in the beginning of the memory area, so |
| 238 | * there's no need for any carrier cell. This implies |
| 239 | * that each memory area is at least as big as one |
| 240 | * pointer. |
| 241 | */ |
| 242 | #define pool_free_to(__pool, __ptr) ({ \ |
| 243 | *(void **)(__ptr) = (void *)(__pool); \ |
| 244 | __pool = (void *)(__ptr); \ |
| 245 | }) |
| 246 | |
| 247 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 248 | #define MEM_OPTIM |
| 249 | #ifdef MEM_OPTIM |
| 250 | /* |
| 251 | * Returns a pointer to type <type> taken from the |
| 252 | * pool <pool_type> or dynamically allocated. In the |
| 253 | * first case, <pool_type> is updated to point to the |
| 254 | * next element in the list. |
| 255 | */ |
| 256 | #define pool_alloc(type) ({ \ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 257 | void *__p; \ |
| 258 | if ((__p = pool_##type) == NULL) \ |
| 259 | __p = malloc(sizeof_##type); \ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 260 | else { \ |
| 261 | pool_##type = *(void **)pool_##type; \ |
| 262 | } \ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 263 | __p; \ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 264 | }) |
| 265 | |
| 266 | /* |
| 267 | * Puts a memory area back to the corresponding pool. |
| 268 | * Items are chained directly through a pointer that |
| 269 | * is written in the beginning of the memory area, so |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 270 | * there's no need for any carrier cell. This implies |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 271 | * that each memory area is at least as big as one |
| 272 | * pointer. |
| 273 | */ |
| 274 | #define pool_free(type, ptr) ({ \ |
| 275 | *(void **)ptr = (void *)pool_##type; \ |
| 276 | pool_##type = (void *)ptr; \ |
| 277 | }) |
| 278 | |
| 279 | #else |
| 280 | #define pool_alloc(type) (calloc(1,sizeof_##type)); |
| 281 | #define pool_free(type, ptr) (free(ptr)); |
| 282 | #endif /* MEM_OPTIM */ |
| 283 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 284 | #define sizeof_task sizeof(struct task) |
| 285 | #define sizeof_session sizeof(struct session) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 286 | #define sizeof_buffer sizeof(struct buffer) |
| 287 | #define sizeof_fdtab sizeof(struct fdtab) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 288 | #define sizeof_requri REQURI_LEN |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 289 | #define sizeof_capture CAPTURE_LEN |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 290 | #define sizeof_curappsession CAPTURE_LEN /* current_session pool */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 291 | #define sizeof_appsess sizeof(struct appsessions) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 292 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 293 | /* different possible states for the sockets */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 294 | #define FD_STCLOSE 0 |
| 295 | #define FD_STLISTEN 1 |
| 296 | #define FD_STCONN 2 |
| 297 | #define FD_STREADY 3 |
| 298 | #define FD_STERROR 4 |
| 299 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 300 | /* values for task->state */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 301 | #define TASK_IDLE 0 |
| 302 | #define TASK_RUNNING 1 |
| 303 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 304 | /* values for proxy->state */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 305 | #define PR_STNEW 0 |
| 306 | #define PR_STIDLE 1 |
| 307 | #define PR_STRUN 2 |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 308 | #define PR_STSTOPPED 3 |
| 309 | #define PR_STPAUSED 4 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 310 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 311 | /* values for proxy->mode */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 312 | #define PR_MODE_TCP 0 |
| 313 | #define PR_MODE_HTTP 1 |
| 314 | #define PR_MODE_HEALTH 2 |
| 315 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 316 | /* possible actions for the *poll() loops */ |
| 317 | #define POLL_LOOP_ACTION_INIT 0 |
| 318 | #define POLL_LOOP_ACTION_RUN 1 |
| 319 | #define POLL_LOOP_ACTION_CLEAN 2 |
| 320 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 321 | /* poll mechanisms available */ |
| 322 | #define POLL_USE_SELECT (1<<0) |
| 323 | #define POLL_USE_POLL (1<<1) |
| 324 | #define POLL_USE_EPOLL (1<<2) |
| 325 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 326 | /* bits for proxy->options */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 327 | #define PR_O_REDISP 0x00000001 /* allow reconnection to dispatch in case of errors */ |
| 328 | #define PR_O_TRANSP 0x00000002 /* transparent mode : use original DEST as dispatch */ |
| 329 | #define PR_O_COOK_RW 0x00000004 /* rewrite all direct cookies with the right serverid */ |
| 330 | #define PR_O_COOK_IND 0x00000008 /* keep only indirect cookies */ |
| 331 | #define PR_O_COOK_INS 0x00000010 /* insert cookies when not accessing a server directly */ |
| 332 | #define PR_O_COOK_PFX 0x00000020 /* rewrite all cookies by prefixing the right serverid */ |
| 333 | #define PR_O_COOK_ANY (PR_O_COOK_RW | PR_O_COOK_IND | PR_O_COOK_INS | PR_O_COOK_PFX) |
| 334 | #define PR_O_BALANCE_RR 0x00000040 /* balance in round-robin mode */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 335 | #define PR_O_KEEPALIVE 0x00000080 /* follow keep-alive sessions */ |
| 336 | #define PR_O_FWDFOR 0x00000100 /* insert x-forwarded-for with client address */ |
| 337 | #define PR_O_BIND_SRC 0x00000200 /* bind to a specific source address when connect()ing */ |
| 338 | #define PR_O_NULLNOLOG 0x00000400 /* a connect without request will not be logged */ |
| 339 | #define PR_O_COOK_NOC 0x00000800 /* add a 'Cache-control' header with the cookie */ |
| 340 | #define PR_O_COOK_POST 0x00001000 /* don't insert cookies for requests other than a POST */ |
| 341 | #define PR_O_HTTP_CHK 0x00002000 /* use HTTP 'OPTIONS' method to check server health */ |
| 342 | #define PR_O_PERSIST 0x00004000 /* server persistence stays effective even when server is down */ |
| 343 | #define PR_O_LOGASAP 0x00008000 /* log as soon as possible, without waiting for the session to complete */ |
| 344 | #define PR_O_HTTP_CLOSE 0x00010000 /* force 'connection: close' in both directions */ |
| 345 | #define PR_O_CHK_CACHE 0x00020000 /* require examination of cacheability of the 'set-cookie' field */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 346 | #define PR_O_TCP_CLI_KA 0x00040000 /* enable TCP keep-alive on client-side sessions */ |
| 347 | #define PR_O_TCP_SRV_KA 0x00080000 /* enable TCP keep-alive on server-side sessions */ |
Willy TARREAU | 3481c46 | 2006-03-01 22:37:57 +0100 | [diff] [blame] | 348 | #define PR_O_USE_ALL_BK 0x00100000 /* load-balance between backup servers */ |
Willy TARREAU | 767ba71 | 2006-03-01 22:40:50 +0100 | [diff] [blame] | 349 | #define PR_O_FORCE_CLO 0x00200000 /* enforce the connection close immediately after server response */ |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 350 | #define PR_O_BALANCE_SH 0x00400000 /* balance on source IP hash */ |
| 351 | #define PR_O_BALANCE (PR_O_BALANCE_RR | PR_O_BALANCE_SH) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 352 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 353 | /* various session flags */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 354 | #define SN_DIRECT 0x00000001 /* connection made on the server matching the client cookie */ |
| 355 | #define SN_CLDENY 0x00000002 /* a client header matches a deny regex */ |
| 356 | #define SN_CLALLOW 0x00000004 /* a client header matches an allow regex */ |
| 357 | #define SN_SVDENY 0x00000008 /* a server header matches a deny regex */ |
| 358 | #define SN_SVALLOW 0x00000010 /* a server header matches an allow regex */ |
| 359 | #define SN_POST 0x00000020 /* the request was an HTTP POST */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 360 | #define SN_MONITOR 0x00000040 /* this session comes from a monitoring system */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 361 | |
| 362 | #define SN_CK_NONE 0x00000000 /* this session had no cookie */ |
| 363 | #define SN_CK_INVALID 0x00000040 /* this session had a cookie which matches no server */ |
| 364 | #define SN_CK_DOWN 0x00000080 /* this session had cookie matching a down server */ |
| 365 | #define SN_CK_VALID 0x000000C0 /* this session had cookie matching a valid server */ |
| 366 | #define SN_CK_MASK 0x000000C0 /* mask to get this session's cookie flags */ |
| 367 | #define SN_CK_SHIFT 6 /* bit shift */ |
| 368 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 369 | #define SN_ERR_NONE 0x00000000 |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 370 | #define SN_ERR_CLITO 0x00000100 /* client time-out */ |
| 371 | #define SN_ERR_CLICL 0x00000200 /* client closed (read/write error) */ |
| 372 | #define SN_ERR_SRVTO 0x00000300 /* server time-out, connect time-out */ |
| 373 | #define SN_ERR_SRVCL 0x00000400 /* server closed (connect/read/write error) */ |
| 374 | #define SN_ERR_PRXCOND 0x00000500 /* the proxy decided to close (deny...) */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 375 | #define SN_ERR_RESOURCE 0x00000600 /* the proxy encountered a lack of a local resources (fd, mem, ...) */ |
| 376 | #define SN_ERR_INTERNAL 0x00000700 /* the proxy encountered an internal error */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 377 | #define SN_ERR_MASK 0x00000700 /* mask to get only session error flags */ |
| 378 | #define SN_ERR_SHIFT 8 /* bit shift */ |
| 379 | |
| 380 | #define SN_FINST_R 0x00001000 /* session ended during client request */ |
| 381 | #define SN_FINST_C 0x00002000 /* session ended during server connect */ |
| 382 | #define SN_FINST_H 0x00003000 /* session ended during server headers */ |
| 383 | #define SN_FINST_D 0x00004000 /* session ended during data phase */ |
| 384 | #define SN_FINST_L 0x00005000 /* session ended while pushing last data to client */ |
| 385 | #define SN_FINST_MASK 0x00007000 /* mask to get only final session state flags */ |
| 386 | #define SN_FINST_SHIFT 12 /* bit shift */ |
| 387 | |
| 388 | #define SN_SCK_NONE 0x00000000 /* no set-cookie seen for the server cookie */ |
| 389 | #define SN_SCK_DELETED 0x00010000 /* existing set-cookie deleted or changed */ |
| 390 | #define SN_SCK_INSERTED 0x00020000 /* new set-cookie inserted or changed existing one */ |
| 391 | #define SN_SCK_SEEN 0x00040000 /* set-cookie seen for the server cookie */ |
| 392 | #define SN_SCK_MASK 0x00070000 /* mask to get the set-cookie field */ |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 393 | #define SN_SCK_ANY 0x00080000 /* at least one set-cookie seen (not to be counted) */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 394 | #define SN_SCK_SHIFT 16 /* bit shift */ |
| 395 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 396 | #define SN_CACHEABLE 0x00100000 /* at least part of the response is cacheable */ |
| 397 | #define SN_CACHE_COOK 0x00200000 /* a cookie in the response is cacheable */ |
| 398 | #define SN_CACHE_SHIFT 20 /* bit shift */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 399 | |
| 400 | /* different possible states for the client side */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 401 | #define CL_STHEADERS 0 |
| 402 | #define CL_STDATA 1 |
| 403 | #define CL_STSHUTR 2 |
| 404 | #define CL_STSHUTW 3 |
| 405 | #define CL_STCLOSE 4 |
| 406 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 407 | /* different possible states for the server side */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 408 | #define SV_STIDLE 0 |
| 409 | #define SV_STCONN 1 |
| 410 | #define SV_STHEADERS 2 |
| 411 | #define SV_STDATA 3 |
| 412 | #define SV_STSHUTR 4 |
| 413 | #define SV_STSHUTW 5 |
| 414 | #define SV_STCLOSE 6 |
| 415 | |
| 416 | /* result of an I/O event */ |
| 417 | #define RES_SILENT 0 /* didn't happen */ |
| 418 | #define RES_DATA 1 /* data were sent or received */ |
| 419 | #define RES_NULL 2 /* result is 0 (read == 0), or connect without need for writing */ |
| 420 | #define RES_ERROR 3 /* result -1 or error on the socket (eg: connect()) */ |
| 421 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 422 | /* modes of operation (global.mode) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 423 | #define MODE_DEBUG 1 |
| 424 | #define MODE_STATS 2 |
| 425 | #define MODE_LOG 4 |
| 426 | #define MODE_DAEMON 8 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 427 | #define MODE_QUIET 16 |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 428 | #define MODE_CHECK 32 |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 429 | #define MODE_VERBOSE 64 |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 430 | #define MODE_STARTING 128 |
willy tarreau | bf8ff3d | 2006-03-25 19:47:03 +0100 | [diff] [blame] | 431 | #define MODE_FOREGROUND 256 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 432 | |
| 433 | /* server flags */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 434 | #define SRV_RUNNING 1 /* the server is UP */ |
| 435 | #define SRV_BACKUP 2 /* this server is a backup server */ |
| 436 | #define SRV_MAPPORTS 4 /* this server uses mapped ports */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 437 | #define SRV_BIND_SRC 8 /* this server uses a specific source address */ |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 438 | #define SRV_CHECKED 16 /* this server needs to be checked */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 439 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 440 | /* what to do when a header matches a regex */ |
| 441 | #define ACT_ALLOW 0 /* allow the request */ |
| 442 | #define ACT_REPLACE 1 /* replace the matching header */ |
| 443 | #define ACT_REMOVE 2 /* remove the matching header */ |
| 444 | #define ACT_DENY 3 /* deny the request */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 445 | #define ACT_PASS 4 /* pass this header without allowing or denying the request */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 446 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 447 | /* configuration sections */ |
| 448 | #define CFG_NONE 0 |
| 449 | #define CFG_GLOBAL 1 |
| 450 | #define CFG_LISTEN 2 |
| 451 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 452 | /* fields that need to be logged. They appear as flags in session->logs.logwait */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 453 | #define LW_DATE 1 /* date */ |
| 454 | #define LW_CLIP 2 /* CLient IP */ |
| 455 | #define LW_SVIP 4 /* SerVer IP */ |
| 456 | #define LW_SVID 8 /* server ID */ |
| 457 | #define LW_REQ 16 /* http REQuest */ |
| 458 | #define LW_RESP 32 /* http RESPonse */ |
| 459 | #define LW_PXIP 64 /* proxy IP */ |
| 460 | #define LW_PXID 128 /* proxy ID */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 461 | #define LW_BYTES 256 /* bytes read from server */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 462 | #define LW_COOKIE 512 /* captured cookie */ |
| 463 | #define LW_REQHDR 1024 /* request header(s) */ |
| 464 | #define LW_RSPHDR 2048 /* response header(s) */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 465 | |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 466 | #define ERR_NONE 0 /* no error */ |
| 467 | #define ERR_RETRYABLE 1 /* retryable error, may be cumulated */ |
| 468 | #define ERR_FATAL 2 /* fatal error, may be cumulated */ |
| 469 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 470 | /*********************************************************************/ |
| 471 | |
| 472 | #define LIST_HEAD(a) ((void *)(&(a))) |
| 473 | |
| 474 | /*********************************************************************/ |
| 475 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 476 | struct cap_hdr { |
| 477 | struct cap_hdr *next; |
| 478 | char *name; /* header name, case insensitive */ |
| 479 | int namelen; /* length of the header name, to speed-up lookups */ |
| 480 | int len; /* capture length, not including terminal zero */ |
| 481 | int index; /* index in the output array */ |
| 482 | void *pool; /* pool of pre-allocated memory area of (len+1) bytes */ |
| 483 | }; |
| 484 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 485 | struct hdr_exp { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 486 | struct hdr_exp *next; |
| 487 | regex_t *preg; /* expression to look for */ |
| 488 | int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */ |
| 489 | char *replace; /* expression to set instead */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 490 | }; |
| 491 | |
| 492 | struct buffer { |
| 493 | unsigned int l; /* data length */ |
| 494 | char *r, *w, *h, *lr; /* read ptr, write ptr, last header ptr, last read */ |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 495 | char *rlim; /* read limit, used for header rewriting */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 496 | unsigned long long total; /* total data read */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 497 | char data[BUFSIZE]; |
| 498 | }; |
| 499 | |
| 500 | struct server { |
| 501 | struct server *next; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 502 | int state; /* server state (SRV_*) */ |
| 503 | int cklen; /* the len of the cookie, to speed up checks */ |
| 504 | char *cookie; /* the id set in the cookie */ |
| 505 | char *id; /* just for identification */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 506 | struct sockaddr_in addr; /* the address to connect to */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 507 | struct sockaddr_in source_addr; /* the address to which we want to bind for connect() */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 508 | short check_port; /* the port to use for the health checks */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 509 | int health; /* 0->rise-1 = bad; rise->rise+fall-1 = good */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 510 | int rise, fall; /* time in iterations */ |
| 511 | int inter; /* time in milliseconds */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 512 | int result; /* 0 = connect OK, -1 = connect KO */ |
| 513 | int curfd; /* file desc used for current test, or -1 if not in test */ |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 514 | int cur_sess; /* number of currently active sessions (including syn_sent) */ |
| 515 | unsigned int cum_sess; /* cumulated number of sessions really sent to this server */ |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 516 | struct proxy *proxy; /* the proxy this server belongs to */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 517 | }; |
| 518 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 519 | /* The base for all tasks */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 520 | struct task { |
| 521 | struct task *next, *prev; /* chaining ... */ |
| 522 | struct task *rqnext; /* chaining in run queue ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 523 | struct task *wq; /* the wait queue this task is in */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 524 | int state; /* task state : IDLE or RUNNING */ |
| 525 | struct timeval expire; /* next expiration time for this task, use only for fast sorting */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 526 | int (*process)(struct task *t); /* the function which processes the task */ |
| 527 | void *context; /* the task's context */ |
| 528 | }; |
| 529 | |
| 530 | /* WARNING: if new fields are added, they must be initialized in event_accept() */ |
| 531 | struct session { |
| 532 | struct task *task; /* the task associated with this session */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 533 | /* application specific below */ |
| 534 | struct timeval crexpire; /* expiration date for a client read */ |
| 535 | struct timeval cwexpire; /* expiration date for a client write */ |
| 536 | struct timeval srexpire; /* expiration date for a server read */ |
| 537 | struct timeval swexpire; /* expiration date for a server write */ |
| 538 | struct timeval cnexpire; /* expiration date for a connect */ |
| 539 | char res_cr, res_cw, res_sr, res_sw;/* results of some events */ |
| 540 | struct proxy *proxy; /* the proxy this socket belongs to */ |
| 541 | int cli_fd; /* the client side fd */ |
| 542 | int srv_fd; /* the server side fd */ |
| 543 | int cli_state; /* state of the client side */ |
| 544 | int srv_state; /* state of the server side */ |
| 545 | int conn_retries; /* number of connect retries left */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 546 | int flags; /* some flags describing the session */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 547 | struct buffer *req; /* request buffer */ |
| 548 | struct buffer *rep; /* response buffer */ |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 549 | struct sockaddr_storage cli_addr; /* the client address */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 550 | struct sockaddr_in srv_addr; /* the address to connect to */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 551 | struct server *srv; /* the server being used */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 552 | char **req_cap; /* array of captured request headers (may be NULL) */ |
| 553 | char **rsp_cap; /* array of captured response headers (may be NULL) */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 554 | struct { |
| 555 | int logwait; /* log fields waiting to be collected : LW_* */ |
| 556 | struct timeval tv_accept; /* date of the accept() (beginning of the session) */ |
| 557 | long t_request; /* delay before the end of the request arrives, -1 if never occurs */ |
| 558 | long t_connect; /* delay before the connect() to the server succeeds, -1 if never occurs */ |
| 559 | long t_data; /* delay before the first data byte from the server ... */ |
| 560 | unsigned long t_close; /* total session duration */ |
| 561 | char *uri; /* first line if log needed, NULL otherwise */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 562 | char *cli_cookie; /* cookie presented by the client, in capture mode */ |
| 563 | char *srv_cookie; /* cookie presented by the server, in capture mode */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 564 | int status; /* HTTP status from the server, negative if from proxy */ |
| 565 | long long bytes; /* number of bytes transferred from the server */ |
| 566 | } logs; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 567 | unsigned int uniq_id; /* unique ID used for the traces */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 568 | }; |
| 569 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 570 | struct listener { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 571 | int fd; /* the listen socket */ |
| 572 | struct sockaddr_storage addr; /* the address we listen to */ |
| 573 | struct listener *next; /* next address or NULL */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 574 | }; |
| 575 | |
| 576 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 577 | struct proxy { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 578 | struct listener *listen; /* the listen addresses and sockets */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 579 | struct in_addr mon_net, mon_mask; /* don't forward connections from this net (network order) FIXME: should support IPv6 */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 580 | int state; /* proxy state */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 581 | struct sockaddr_in dispatch_addr; /* the default address to connect to */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 582 | struct server *srv, *cursrv; /* known servers, current server */ |
willy tarreau | 62084d4 | 2006-03-24 18:57:41 +0100 | [diff] [blame] | 583 | int srv_act, srv_bck; /* # of servers */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 584 | char *cookie_name; /* name of the cookie to look for */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 585 | int cookie_len; /* strlen(cookie_name), computed only once */ |
| 586 | char *appsession_name; /* name of the cookie to look for */ |
| 587 | int appsession_name_len; /* strlen(appsession_name), computed only once */ |
| 588 | int appsession_len; /* length of the appsession cookie value to be used */ |
| 589 | int appsession_timeout; |
| 590 | CHTbl htbl_proxy; /* Per Proxy hashtable */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 591 | char *capture_name; /* beginning of the name of the cookie to capture */ |
| 592 | int capture_namelen; /* length of the cookie name to match */ |
| 593 | int capture_len; /* length of the string to be captured */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 594 | int clitimeout; /* client I/O timeout (in milliseconds) */ |
| 595 | int srvtimeout; /* server I/O timeout (in milliseconds) */ |
| 596 | int contimeout; /* connect timeout (in milliseconds) */ |
| 597 | char *id; /* proxy id */ |
| 598 | int nbconn; /* # of active sessions */ |
| 599 | int maxconn; /* max # of active sessions */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 600 | int conn_retries; /* maximum number of connect retries */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 601 | int options; /* PR_O_REDISP, PR_O_TRANSP, ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 602 | int mode; /* mode = PR_MODE_TCP, PR_MODE_HTTP or PR_MODE_HEALTH */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 603 | struct sockaddr_in source_addr; /* the address to which we want to bind for connect() */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 604 | struct proxy *next; |
| 605 | struct sockaddr_in logsrv1, logsrv2; /* 2 syslog servers */ |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 606 | signed char logfac1, logfac2; /* log facility for both servers. -1 = disabled */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 607 | int loglev1, loglev2; /* log level for each server, 7 by default */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 608 | int to_log; /* things to be logged (LW_*) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 609 | struct timeval stop_time; /* date to stop listening, when stopping != 0 */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 610 | int nb_reqadd, nb_rspadd; |
| 611 | struct hdr_exp *req_exp; /* regular expressions for request headers */ |
| 612 | struct hdr_exp *rsp_exp; /* regular expressions for response headers */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 613 | int nb_req_cap, nb_rsp_cap; /* # of headers to be captured */ |
| 614 | struct cap_hdr *req_cap; /* chained list of request headers to be captured */ |
| 615 | struct cap_hdr *rsp_cap; /* chained list of response headers to be captured */ |
| 616 | void *req_cap_pool, *rsp_cap_pool; /* pools of pre-allocated char ** used to build the sessions */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 617 | char *req_add[MAX_NEWHDR], *rsp_add[MAX_NEWHDR]; /* headers to be added */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 618 | int grace; /* grace time after stop request */ |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 619 | char *check_req; /* HTTP request to use if PR_O_HTTP_CHK is set, else NULL */ |
| 620 | int check_len; /* Length of the HTTP request */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 621 | struct { |
| 622 | char *msg400; /* message for error 400 */ |
| 623 | int len400; /* message length for error 400 */ |
| 624 | char *msg403; /* message for error 403 */ |
| 625 | int len403; /* message length for error 403 */ |
| 626 | char *msg408; /* message for error 408 */ |
| 627 | int len408; /* message length for error 408 */ |
| 628 | char *msg500; /* message for error 500 */ |
| 629 | int len500; /* message length for error 500 */ |
| 630 | char *msg502; /* message for error 502 */ |
| 631 | int len502; /* message length for error 502 */ |
| 632 | char *msg503; /* message for error 503 */ |
| 633 | int len503; /* message length for error 503 */ |
| 634 | char *msg504; /* message for error 504 */ |
| 635 | int len504; /* message length for error 504 */ |
| 636 | } errmsg; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 637 | }; |
| 638 | |
| 639 | /* info about one given fd */ |
| 640 | struct fdtab { |
| 641 | int (*read)(int fd); /* read function */ |
| 642 | int (*write)(int fd); /* write function */ |
| 643 | struct task *owner; /* the session (or proxy) associated with this fd */ |
| 644 | int state; /* the state of this fd */ |
| 645 | }; |
| 646 | |
| 647 | /*********************************************************************/ |
| 648 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 649 | int cfg_maxpconn = DEFAULT_MAXCONN; /* # of simultaneous connections per proxy (-N) */ |
Willy TARREAU | 13032e7 | 2006-03-12 17:31:45 +0100 | [diff] [blame] | 650 | int cfg_maxconn = 0; /* # of simultaneous connections, (-n) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 651 | char *cfg_cfgfile = NULL; /* configuration file */ |
| 652 | char *progname = NULL; /* program name */ |
| 653 | int pid; /* current process id */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 654 | |
| 655 | /* global options */ |
| 656 | static struct { |
| 657 | int uid; |
| 658 | int gid; |
| 659 | int nbproc; |
| 660 | int maxconn; |
| 661 | int maxsock; /* max # of sockets */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 662 | int rlimit_nofile; /* default ulimit-n value : 0=unset */ |
willy tarreau | 746e26b | 2006-03-25 11:14:35 +0100 | [diff] [blame] | 663 | int rlimit_memmax; /* default ulimit-d in megs value : 0=unset */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 664 | int mode; |
| 665 | char *chroot; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 666 | char *pidfile; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 667 | int logfac1, logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 668 | int loglev1, loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 669 | struct sockaddr_in logsrv1, logsrv2; |
| 670 | } global = { |
| 671 | logfac1 : -1, |
| 672 | logfac2 : -1, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 673 | loglev1 : 7, /* max syslog level : debug */ |
| 674 | loglev2 : 7, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 675 | /* others NULL OK */ |
| 676 | }; |
| 677 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 678 | /*********************************************************************/ |
| 679 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 680 | fd_set *StaticReadEvent, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 681 | *StaticWriteEvent; |
| 682 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 683 | int cfg_polling_mechanism = 0; /* POLL_USE_{SELECT|POLL|EPOLL} */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 684 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 685 | void **pool_session = NULL, |
| 686 | **pool_buffer = NULL, |
| 687 | **pool_fdtab = NULL, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 688 | **pool_requri = NULL, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 689 | **pool_task = NULL, |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 690 | **pool_capture = NULL, |
| 691 | **pool_appsess = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 692 | |
| 693 | struct proxy *proxy = NULL; /* list of all existing proxies */ |
| 694 | struct fdtab *fdtab = NULL; /* array of all the file descriptors */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 695 | struct task *rq = NULL; /* global run queue */ |
| 696 | struct task wait_queue = { /* global wait queue */ |
| 697 | prev:LIST_HEAD(wait_queue), |
| 698 | next:LIST_HEAD(wait_queue) |
| 699 | }; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 700 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 701 | static int totalconn = 0; /* total # of terminated sessions */ |
| 702 | static int actconn = 0; /* # of active sessions */ |
| 703 | static int maxfd = 0; /* # of the highest fd + 1 */ |
| 704 | static int listeners = 0; /* # of listeners */ |
| 705 | static int stopping = 0; /* non zero means stopping in progress */ |
| 706 | static struct timeval now = {0,0}; /* the current date at any moment */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 707 | static struct proxy defproxy; /* fake proxy used to assign default values on all instances */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 708 | |
willy tarreau | 53e9970 | 2006-03-25 18:53:50 +0100 | [diff] [blame] | 709 | /* Here we store informations about the pids of the processes we may pause |
| 710 | * or kill. We will send them a signal every 10 ms until we can bind to all |
| 711 | * our ports. With 200 retries, that's about 2 seconds. |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 712 | */ |
willy tarreau | 53e9970 | 2006-03-25 18:53:50 +0100 | [diff] [blame] | 713 | #define MAX_START_RETRIES 200 |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 714 | static int nb_oldpids = 0; |
| 715 | static int *oldpids = NULL; |
| 716 | static int oldpids_sig; /* use USR1 or TERM */ |
| 717 | |
willy tarreau | 08dedbe | 2005-12-18 01:13:48 +0100 | [diff] [blame] | 718 | #if defined(ENABLE_EPOLL) |
| 719 | /* FIXME: this is dirty, but at the moment, there's no other solution to remove |
| 720 | * the old FDs from outside the loop. Perhaps we should export a global 'poll' |
| 721 | * structure with pointers to functions such as init_fd() and close_fd(), plus |
| 722 | * a private structure with several pointers to places such as below. |
| 723 | */ |
| 724 | |
| 725 | static fd_set *PrevReadEvent = NULL, *PrevWriteEvent = NULL; |
| 726 | #endif |
| 727 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 728 | static regmatch_t pmatch[MAX_MATCH]; /* rm_so, rm_eo for regular expressions */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 729 | /* this is used to drain data, and as a temporary buffer for sprintf()... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 730 | static char trash[BUFSIZE]; |
| 731 | |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 732 | const int zero = 0; |
| 733 | const int one = 1; |
| 734 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 735 | /* |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 736 | * Syslog facilities and levels. Conforming to RFC3164. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 737 | */ |
| 738 | |
| 739 | #define MAX_SYSLOG_LEN 1024 |
| 740 | #define NB_LOG_FACILITIES 24 |
| 741 | const char *log_facilities[NB_LOG_FACILITIES] = { |
| 742 | "kern", "user", "mail", "daemon", |
| 743 | "auth", "syslog", "lpr", "news", |
| 744 | "uucp", "cron", "auth2", "ftp", |
| 745 | "ntp", "audit", "alert", "cron2", |
| 746 | "local0", "local1", "local2", "local3", |
| 747 | "local4", "local5", "local6", "local7" |
| 748 | }; |
| 749 | |
| 750 | |
| 751 | #define NB_LOG_LEVELS 8 |
| 752 | const char *log_levels[NB_LOG_LEVELS] = { |
| 753 | "emerg", "alert", "crit", "err", |
| 754 | "warning", "notice", "info", "debug" |
| 755 | }; |
| 756 | |
| 757 | #define SYSLOG_PORT 514 |
| 758 | |
| 759 | const char *monthname[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 760 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 761 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 762 | const char sess_term_cond[8] = "-cCsSPRI"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 763 | const char sess_fin_state[8] = "-RCHDL67"; /* cliRequest, srvConnect, srvHeader, Data, Last, unknown */ |
| 764 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 765 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 766 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 767 | unknown, Set-cookie Rewritten */ |
| 768 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 769 | #define MAX_HOSTNAME_LEN 32 |
| 770 | static char hostname[MAX_HOSTNAME_LEN] = ""; |
| 771 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 772 | const char *HTTP_302 = |
| 773 | "HTTP/1.0 302 Found\r\n" |
| 774 | "Cache-Control: no-cache\r\n" |
| 775 | "Connection: close\r\n" |
| 776 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 777 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 778 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 779 | const char *HTTP_303 = |
| 780 | "HTTP/1.0 303 See Other\r\n" |
| 781 | "Cache-Control: no-cache\r\n" |
| 782 | "Connection: close\r\n" |
| 783 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 784 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 785 | const char *HTTP_400 = |
| 786 | "HTTP/1.0 400 Bad request\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 787 | "Cache-Control: no-cache\r\n" |
| 788 | "Connection: close\r\n" |
| 789 | "\r\n" |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 790 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n"; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 791 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 792 | const char *HTTP_403 = |
| 793 | "HTTP/1.0 403 Forbidden\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 794 | "Cache-Control: no-cache\r\n" |
| 795 | "Connection: close\r\n" |
| 796 | "\r\n" |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 797 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n"; |
| 798 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 799 | const char *HTTP_408 = |
| 800 | "HTTP/1.0 408 Request Time-out\r\n" |
| 801 | "Cache-Control: no-cache\r\n" |
| 802 | "Connection: close\r\n" |
| 803 | "\r\n" |
| 804 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n"; |
| 805 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 806 | const char *HTTP_500 = |
| 807 | "HTTP/1.0 500 Server Error\r\n" |
| 808 | "Cache-Control: no-cache\r\n" |
| 809 | "Connection: close\r\n" |
| 810 | "\r\n" |
| 811 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n"; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 812 | |
| 813 | const char *HTTP_502 = |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 814 | "HTTP/1.0 502 Bad Gateway\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 815 | "Cache-Control: no-cache\r\n" |
| 816 | "Connection: close\r\n" |
| 817 | "\r\n" |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 818 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n"; |
| 819 | |
| 820 | const char *HTTP_503 = |
| 821 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 822 | "Cache-Control: no-cache\r\n" |
| 823 | "Connection: close\r\n" |
| 824 | "\r\n" |
| 825 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n"; |
| 826 | |
| 827 | const char *HTTP_504 = |
| 828 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 829 | "Cache-Control: no-cache\r\n" |
| 830 | "Connection: close\r\n" |
| 831 | "\r\n" |
| 832 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n"; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 833 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 834 | /*********************************************************************/ |
| 835 | /* statistics ******************************************************/ |
| 836 | /*********************************************************************/ |
| 837 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 838 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 839 | static int stats_tsk_lsrch, stats_tsk_rsrch, |
| 840 | stats_tsk_good, stats_tsk_right, stats_tsk_left, |
| 841 | stats_tsk_new, stats_tsk_nsrch; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 842 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 843 | |
| 844 | |
| 845 | /*********************************************************************/ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 846 | /* debugging *******************************************************/ |
| 847 | /*********************************************************************/ |
| 848 | #ifdef DEBUG_FULL |
| 849 | static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 850 | static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 851 | #endif |
| 852 | |
| 853 | /*********************************************************************/ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 854 | /* function prototypes *********************************************/ |
| 855 | /*********************************************************************/ |
| 856 | |
| 857 | int event_accept(int fd); |
| 858 | int event_cli_read(int fd); |
| 859 | int event_cli_write(int fd); |
| 860 | int event_srv_read(int fd); |
| 861 | int event_srv_write(int fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 862 | int process_session(struct task *t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 863 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 864 | static int appsession_task_init(void); |
| 865 | static int appsession_init(void); |
| 866 | static int appsession_refresh(struct task *t); |
| 867 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 868 | /*********************************************************************/ |
| 869 | /* general purpose functions ***************************************/ |
| 870 | /*********************************************************************/ |
| 871 | |
| 872 | void display_version() { |
| 873 | printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n"); |
willy tarreau | 726618c | 2006-01-29 22:42:06 +0100 | [diff] [blame] | 874 | printf("Copyright 2000-2006 Willy Tarreau <w@w.ods.org>\n\n"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /* |
| 878 | * This function prints the command line usage and exits |
| 879 | */ |
| 880 | void usage(char *name) { |
| 881 | display_version(); |
| 882 | fprintf(stderr, |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 883 | "Usage : %s -f <cfgfile> [ -vdV" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 884 | #if STATTIME > 0 |
| 885 | "sl" |
| 886 | #endif |
willy tarreau | 746e26b | 2006-03-25 11:14:35 +0100 | [diff] [blame] | 887 | "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n" |
| 888 | " [ -p <pidfile> ] [ -m <max megs> ]\n" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 889 | " -v displays version\n" |
willy tarreau | bf8ff3d | 2006-03-25 19:47:03 +0100 | [diff] [blame] | 890 | " -d enters debug mode ; -db only disables background mode.\n" |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 891 | " -V enters verbose mode (disables quiet mode)\n" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 892 | #if STATTIME > 0 |
| 893 | " -s enables statistics output\n" |
| 894 | " -l enables long statistics format\n" |
| 895 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 896 | " -D goes daemon ; implies -q\n" |
| 897 | " -q quiet mode : don't display messages\n" |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 898 | " -c check mode : only check config file and exit\n" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 899 | " -n sets the maximum total # of connections (%d)\n" |
willy tarreau | 746e26b | 2006-03-25 11:14:35 +0100 | [diff] [blame] | 900 | " -m limits the usable amount of memory (in MB)\n" |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 901 | " -N sets the default, per-proxy maximum # of connections (%d)\n" |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 902 | " -p writes pids of all children to this file\n" |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 903 | #if defined(ENABLE_EPOLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 904 | " -de disables epoll() usage even when available\n" |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 905 | #endif |
| 906 | #if defined(ENABLE_POLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 907 | " -dp disables poll() usage even when available\n" |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 908 | #endif |
willy tarreau | 53e9970 | 2006-03-25 18:53:50 +0100 | [diff] [blame] | 909 | " -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.\n" |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 910 | "\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 911 | name, DEFAULT_MAXCONN, cfg_maxpconn); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 912 | exit(1); |
| 913 | } |
| 914 | |
| 915 | |
| 916 | /* |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 917 | * Displays the message on stderr with the date and pid. Overrides the quiet |
| 918 | * mode during startup. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 919 | */ |
| 920 | void Alert(char *fmt, ...) { |
| 921 | va_list argp; |
| 922 | struct timeval tv; |
| 923 | struct tm *tm; |
| 924 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 925 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 926 | va_start(argp, fmt); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 927 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 928 | gettimeofday(&tv, NULL); |
| 929 | tm=localtime(&tv.tv_sec); |
| 930 | fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ", |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 931 | tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)getpid()); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 932 | vfprintf(stderr, fmt, argp); |
| 933 | fflush(stderr); |
| 934 | va_end(argp); |
| 935 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | |
| 939 | /* |
| 940 | * Displays the message on stderr with the date and pid. |
| 941 | */ |
| 942 | void Warning(char *fmt, ...) { |
| 943 | va_list argp; |
| 944 | struct timeval tv; |
| 945 | struct tm *tm; |
| 946 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 947 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 948 | va_start(argp, fmt); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 949 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 950 | gettimeofday(&tv, NULL); |
| 951 | tm=localtime(&tv.tv_sec); |
| 952 | fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ", |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 953 | tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)getpid()); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 954 | vfprintf(stderr, fmt, argp); |
| 955 | fflush(stderr); |
| 956 | va_end(argp); |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | /* |
| 961 | * Displays the message on <out> only if quiet mode is not set. |
| 962 | */ |
| 963 | void qfprintf(FILE *out, char *fmt, ...) { |
| 964 | va_list argp; |
| 965 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 966 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 967 | va_start(argp, fmt); |
| 968 | vfprintf(out, fmt, argp); |
| 969 | fflush(out); |
| 970 | va_end(argp); |
| 971 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | |
| 975 | /* |
| 976 | * converts <str> to a struct sockaddr_in* which is locally allocated. |
| 977 | * The format is "addr:port", where "addr" can be empty or "*" to indicate |
| 978 | * INADDR_ANY. |
| 979 | */ |
| 980 | struct sockaddr_in *str2sa(char *str) { |
| 981 | static struct sockaddr_in sa; |
| 982 | char *c; |
| 983 | int port; |
| 984 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 985 | memset(&sa, 0, sizeof(sa)); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 986 | str=strdup(str); |
| 987 | |
| 988 | if ((c=strrchr(str,':')) != NULL) { |
| 989 | *c++=0; |
| 990 | port=atol(c); |
| 991 | } |
| 992 | else |
| 993 | port=0; |
| 994 | |
| 995 | if (*str == '*' || *str == '\0') { /* INADDR_ANY */ |
| 996 | sa.sin_addr.s_addr = INADDR_ANY; |
| 997 | } |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 998 | else if (!inet_pton(AF_INET, str, &sa.sin_addr)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 999 | struct hostent *he; |
| 1000 | |
| 1001 | if ((he = gethostbyname(str)) == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 1002 | Alert("Invalid server name: '%s'\n", str); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1003 | } |
| 1004 | else |
| 1005 | sa.sin_addr = *(struct in_addr *) *(he->h_addr_list); |
| 1006 | } |
| 1007 | sa.sin_port=htons(port); |
| 1008 | sa.sin_family=AF_INET; |
| 1009 | |
| 1010 | free(str); |
| 1011 | return &sa; |
| 1012 | } |
| 1013 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1014 | /* |
| 1015 | * converts <str> to a two struct in_addr* which are locally allocated. |
| 1016 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
| 1017 | * is optionnal and either in the dotted or CIDR notation. |
| 1018 | * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error. |
| 1019 | */ |
| 1020 | int str2net(char *str, struct in_addr *addr, struct in_addr *mask) { |
| 1021 | char *c; |
| 1022 | unsigned long len; |
| 1023 | |
| 1024 | memset(mask, 0, sizeof(*mask)); |
| 1025 | memset(addr, 0, sizeof(*addr)); |
| 1026 | str=strdup(str); |
| 1027 | |
| 1028 | if ((c = strrchr(str, '/')) != NULL) { |
| 1029 | *c++ = 0; |
| 1030 | /* c points to the mask */ |
| 1031 | if (strchr(c, '.') != NULL) { /* dotted notation */ |
| 1032 | if (!inet_pton(AF_INET, c, mask)) |
| 1033 | return 0; |
| 1034 | } |
| 1035 | else { /* mask length */ |
| 1036 | char *err; |
| 1037 | len = strtol(c, &err, 10); |
| 1038 | if (!*c || (err && *err) || (unsigned)len > 32) |
| 1039 | return 0; |
| 1040 | if (len) |
| 1041 | mask->s_addr = htonl(0xFFFFFFFFUL << (32 - len)); |
| 1042 | else |
| 1043 | mask->s_addr = 0; |
| 1044 | } |
| 1045 | } |
| 1046 | else { |
| 1047 | mask->s_addr = 0xFFFFFFFF; |
| 1048 | } |
| 1049 | if (!inet_pton(AF_INET, str, addr)) { |
| 1050 | struct hostent *he; |
| 1051 | |
| 1052 | if ((he = gethostbyname(str)) == NULL) { |
| 1053 | return 0; |
| 1054 | } |
| 1055 | else |
| 1056 | *addr = *(struct in_addr *) *(he->h_addr_list); |
| 1057 | } |
| 1058 | free(str); |
| 1059 | return 1; |
| 1060 | } |
| 1061 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1062 | |
| 1063 | /* |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1064 | * converts <str> to a list of listeners which are dynamically allocated. |
| 1065 | * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where : |
| 1066 | * - <addr> can be empty or "*" to indicate INADDR_ANY ; |
| 1067 | * - <port> is a numerical port from 1 to 65535 ; |
| 1068 | * - <end> indicates to use the range from <port> to <end> instead (inclusive). |
| 1069 | * This can be repeated as many times as necessary, separated by a coma. |
| 1070 | * The <tail> argument is a pointer to a current list which should be appended |
| 1071 | * to the tail of the new list. The pointer to the new list is returned. |
| 1072 | */ |
| 1073 | struct listener *str2listener(char *str, struct listener *tail) { |
| 1074 | struct listener *l; |
| 1075 | char *c, *next, *range, *dupstr; |
| 1076 | int port, end; |
| 1077 | |
| 1078 | next = dupstr = strdup(str); |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1079 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1080 | while (next && *next) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1081 | struct sockaddr_storage ss; |
| 1082 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1083 | str = next; |
| 1084 | /* 1) look for the end of the first address */ |
| 1085 | if ((next = strrchr(str, ',')) != NULL) { |
| 1086 | *next++ = 0; |
| 1087 | } |
| 1088 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1089 | /* 2) look for the addr/port delimiter, it's the last colon. */ |
| 1090 | if ((range = strrchr(str, ':')) == NULL) { |
| 1091 | Alert("Missing port number: '%s'\n", str); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1092 | goto fail; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | *range++ = 0; |
| 1096 | |
| 1097 | if (strrchr(str, ':') != NULL) { |
| 1098 | /* IPv6 address contains ':' */ |
| 1099 | memset(&ss, 0, sizeof(ss)); |
| 1100 | ss.ss_family = AF_INET6; |
| 1101 | |
| 1102 | if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in6 *)&ss)->sin6_addr)) { |
| 1103 | Alert("Invalid server address: '%s'\n", str); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1104 | goto fail; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1105 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1106 | } |
| 1107 | else { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1108 | memset(&ss, 0, sizeof(ss)); |
| 1109 | ss.ss_family = AF_INET; |
| 1110 | |
| 1111 | if (*str == '*' || *str == '\0') { /* INADDR_ANY */ |
| 1112 | ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; |
| 1113 | } |
| 1114 | else if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in *)&ss)->sin_addr)) { |
| 1115 | struct hostent *he; |
| 1116 | |
| 1117 | if ((he = gethostbyname(str)) == NULL) { |
| 1118 | Alert("Invalid server name: '%s'\n", str); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1119 | goto fail; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1120 | } |
| 1121 | else |
| 1122 | ((struct sockaddr_in *)&ss)->sin_addr = |
| 1123 | *(struct in_addr *) *(he->h_addr_list); |
| 1124 | } |
| 1125 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1126 | |
| 1127 | /* 3) look for the port-end delimiter */ |
| 1128 | if ((c = strchr(range, '-')) != NULL) { |
| 1129 | *c++ = 0; |
| 1130 | end = atol(c); |
| 1131 | } |
| 1132 | else { |
| 1133 | end = atol(range); |
| 1134 | } |
| 1135 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1136 | port = atol(range); |
| 1137 | |
| 1138 | if (port < 1 || port > 65535) { |
| 1139 | Alert("Invalid port '%d' specified for address '%s'.\n", port, str); |
| 1140 | goto fail; |
| 1141 | } |
| 1142 | |
| 1143 | if (end < 1 || end > 65535) { |
| 1144 | Alert("Invalid port '%d' specified for address '%s'.\n", end, str); |
| 1145 | goto fail; |
| 1146 | } |
| 1147 | |
| 1148 | for (; port <= end; port++) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1149 | l = (struct listener *)calloc(1, sizeof(struct listener)); |
| 1150 | l->next = tail; |
| 1151 | tail = l; |
| 1152 | |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 1153 | l->fd = -1; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1154 | l->addr = ss; |
| 1155 | if (ss.ss_family == AF_INET6) |
| 1156 | ((struct sockaddr_in6 *)(&l->addr))->sin6_port = htons(port); |
| 1157 | else |
| 1158 | ((struct sockaddr_in *)(&l->addr))->sin_port = htons(port); |
| 1159 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1160 | } /* end for(port) */ |
| 1161 | } /* end while(next) */ |
| 1162 | free(dupstr); |
| 1163 | return tail; |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1164 | fail: |
| 1165 | free(dupstr); |
| 1166 | return NULL; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1167 | } |
| 1168 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1169 | |
| 1170 | #define FD_SETS_ARE_BITFIELDS |
| 1171 | #ifdef FD_SETS_ARE_BITFIELDS |
| 1172 | /* |
| 1173 | * This map is used with all the FD_* macros to check whether a particular bit |
| 1174 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 1175 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 1176 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 1177 | * exclusively to the macros. |
| 1178 | */ |
| 1179 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 1180 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 1181 | |
| 1182 | #else |
| 1183 | #error "Check if your OS uses bitfields for fd_sets" |
| 1184 | #endif |
| 1185 | |
| 1186 | /* will try to encode the string <string> replacing all characters tagged in |
| 1187 | * <map> with the hexadecimal representation of their ASCII-code (2 digits) |
| 1188 | * prefixed by <escape>, and will store the result between <start> (included |
| 1189 | *) and <stop> (excluded), and will always terminate the string with a '\0' |
| 1190 | * before <stop>. The position of the '\0' is returned if the conversion |
| 1191 | * completes. If bytes are missing between <start> and <stop>, then the |
| 1192 | * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0' |
| 1193 | * cannot even be stored so we return <start> without writing the 0. |
| 1194 | * The input string must also be zero-terminated. |
| 1195 | */ |
| 1196 | char hextab[16] = "0123456789ABCDEF"; |
| 1197 | char *encode_string(char *start, char *stop, |
| 1198 | const char escape, const fd_set *map, |
| 1199 | const char *string) |
| 1200 | { |
| 1201 | if (start < stop) { |
| 1202 | stop--; /* reserve one byte for the final '\0' */ |
| 1203 | while (start < stop && *string != 0) { |
| 1204 | if (!FD_ISSET((unsigned char)(*string), map)) |
| 1205 | *start++ = *string; |
| 1206 | else { |
| 1207 | if (start + 3 >= stop) |
| 1208 | break; |
| 1209 | *start++ = escape; |
| 1210 | *start++ = hextab[(*string >> 4) & 15]; |
| 1211 | *start++ = hextab[*string & 15]; |
| 1212 | } |
| 1213 | string++; |
| 1214 | } |
| 1215 | *start = '\0'; |
| 1216 | } |
| 1217 | return start; |
| 1218 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1219 | |
| 1220 | /* |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1221 | * This function sends a syslog message to both log servers of a proxy, |
| 1222 | * or to global log servers if the proxy is NULL. |
| 1223 | * It also tries not to waste too much time computing the message header. |
| 1224 | * It doesn't care about errors nor does it report them. |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1225 | */ |
| 1226 | void send_log(struct proxy *p, int level, char *message, ...) { |
| 1227 | static int logfd = -1; /* syslog UDP socket */ |
| 1228 | static long tvsec = -1; /* to force the string to be initialized */ |
| 1229 | struct timeval tv; |
| 1230 | va_list argp; |
| 1231 | static char logmsg[MAX_SYSLOG_LEN]; |
| 1232 | static char *dataptr = NULL; |
| 1233 | int fac_level; |
| 1234 | int hdr_len, data_len; |
| 1235 | struct sockaddr_in *sa[2]; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1236 | int facilities[2], loglevel[2]; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1237 | int nbloggers = 0; |
| 1238 | char *log_ptr; |
| 1239 | |
| 1240 | if (logfd < 0) { |
| 1241 | if ((logfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) |
| 1242 | return; |
| 1243 | } |
| 1244 | |
| 1245 | if (level < 0 || progname == NULL || message == NULL) |
| 1246 | return; |
| 1247 | |
| 1248 | gettimeofday(&tv, NULL); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1249 | if (tv.tv_sec != tvsec || dataptr == NULL) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1250 | /* this string is rebuild only once a second */ |
| 1251 | struct tm *tm = localtime(&tv.tv_sec); |
| 1252 | tvsec = tv.tv_sec; |
| 1253 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1254 | hdr_len = snprintf(logmsg, sizeof(logmsg), |
| 1255 | "<<<<>%s %2d %02d:%02d:%02d %s[%d]: ", |
| 1256 | monthname[tm->tm_mon], |
| 1257 | tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 1258 | progname, pid); |
| 1259 | /* WARNING: depending upon implementations, snprintf may return |
| 1260 | * either -1 or the number of bytes that would be needed to store |
| 1261 | * the total message. In both cases, we must adjust it. |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1262 | */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1263 | if (hdr_len < 0 || hdr_len > sizeof(logmsg)) |
| 1264 | hdr_len = sizeof(logmsg); |
| 1265 | |
| 1266 | dataptr = logmsg + hdr_len; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | va_start(argp, message); |
| 1270 | data_len = vsnprintf(dataptr, logmsg + sizeof(logmsg) - dataptr, message, argp); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1271 | if (data_len < 0 || data_len > (logmsg + sizeof(logmsg) - dataptr)) |
| 1272 | data_len = logmsg + sizeof(logmsg) - dataptr; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1273 | va_end(argp); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1274 | dataptr[data_len - 1] = '\n'; /* force a break on ultra-long lines */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1275 | |
| 1276 | if (p == NULL) { |
| 1277 | if (global.logfac1 >= 0) { |
| 1278 | sa[nbloggers] = &global.logsrv1; |
| 1279 | facilities[nbloggers] = global.logfac1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1280 | loglevel[nbloggers] = global.loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1281 | nbloggers++; |
| 1282 | } |
| 1283 | if (global.logfac2 >= 0) { |
| 1284 | sa[nbloggers] = &global.logsrv2; |
| 1285 | facilities[nbloggers] = global.logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1286 | loglevel[nbloggers] = global.loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1287 | nbloggers++; |
| 1288 | } |
| 1289 | } else { |
| 1290 | if (p->logfac1 >= 0) { |
| 1291 | sa[nbloggers] = &p->logsrv1; |
| 1292 | facilities[nbloggers] = p->logfac1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1293 | loglevel[nbloggers] = p->loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1294 | nbloggers++; |
| 1295 | } |
| 1296 | if (p->logfac2 >= 0) { |
| 1297 | sa[nbloggers] = &p->logsrv2; |
| 1298 | facilities[nbloggers] = p->logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1299 | loglevel[nbloggers] = p->loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1300 | nbloggers++; |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | while (nbloggers-- > 0) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1305 | /* we can filter the level of the messages that are sent to each logger */ |
| 1306 | if (level > loglevel[nbloggers]) |
| 1307 | continue; |
| 1308 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1309 | /* For each target, we may have a different facility. |
| 1310 | * We can also have a different log level for each message. |
| 1311 | * This induces variations in the message header length. |
| 1312 | * Since we don't want to recompute it each time, nor copy it every |
| 1313 | * time, we only change the facility in the pre-computed header, |
| 1314 | * and we change the pointer to the header accordingly. |
| 1315 | */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1316 | fac_level = (facilities[nbloggers] << 3) + level; |
| 1317 | log_ptr = logmsg + 3; /* last digit of the log level */ |
| 1318 | do { |
| 1319 | *log_ptr = '0' + fac_level % 10; |
| 1320 | fac_level /= 10; |
| 1321 | log_ptr--; |
| 1322 | } while (fac_level && log_ptr > logmsg); |
| 1323 | *log_ptr = '<'; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1324 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1325 | /* the total syslog message now starts at logptr, for dataptr+data_len-logptr */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1326 | |
| 1327 | #ifndef MSG_NOSIGNAL |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1328 | sendto(logfd, log_ptr, dataptr + data_len - log_ptr, MSG_DONTWAIT, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1329 | (struct sockaddr *)sa[nbloggers], sizeof(**sa)); |
| 1330 | #else |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1331 | sendto(logfd, log_ptr, dataptr + data_len - log_ptr, MSG_DONTWAIT | MSG_NOSIGNAL, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1332 | (struct sockaddr *)sa[nbloggers], sizeof(**sa)); |
| 1333 | #endif |
| 1334 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | |
| 1338 | /* sets <tv> to the current time */ |
| 1339 | static inline struct timeval *tv_now(struct timeval *tv) { |
| 1340 | if (tv) |
| 1341 | gettimeofday(tv, NULL); |
| 1342 | return tv; |
| 1343 | } |
| 1344 | |
| 1345 | /* |
| 1346 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 1347 | */ |
| 1348 | static inline struct timeval *tv_delayfrom(struct timeval *tv, struct timeval *from, int ms) { |
| 1349 | if (!tv || !from) |
| 1350 | return NULL; |
| 1351 | tv->tv_usec = from->tv_usec + (ms%1000)*1000; |
| 1352 | tv->tv_sec = from->tv_sec + (ms/1000); |
| 1353 | while (tv->tv_usec >= 1000000) { |
| 1354 | tv->tv_usec -= 1000000; |
| 1355 | tv->tv_sec++; |
| 1356 | } |
| 1357 | return tv; |
| 1358 | } |
| 1359 | |
| 1360 | /* |
| 1361 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1362 | * Must not be used when either argument is eternity. Use tv_cmp2() for that. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1363 | */ |
| 1364 | static inline int tv_cmp(struct timeval *tv1, struct timeval *tv2) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1365 | if (tv1->tv_sec < tv2->tv_sec) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1366 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1367 | else if (tv1->tv_sec > tv2->tv_sec) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1368 | return 1; |
| 1369 | else if (tv1->tv_usec < tv2->tv_usec) |
| 1370 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1371 | else if (tv1->tv_usec > tv2->tv_usec) |
| 1372 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1373 | else |
| 1374 | return 0; |
| 1375 | } |
| 1376 | |
| 1377 | /* |
| 1378 | * returns the absolute difference, in ms, between tv1 and tv2 |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1379 | * Must not be used when either argument is eternity. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1380 | */ |
| 1381 | unsigned long tv_delta(struct timeval *tv1, struct timeval *tv2) { |
| 1382 | int cmp; |
| 1383 | unsigned long ret; |
| 1384 | |
| 1385 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1386 | cmp = tv_cmp(tv1, tv2); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1387 | if (!cmp) |
| 1388 | return 0; /* same dates, null diff */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1389 | else if (cmp < 0) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1390 | struct timeval *tmp = tv1; |
| 1391 | tv1 = tv2; |
| 1392 | tv2 = tmp; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1393 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1394 | ret = (tv1->tv_sec - tv2->tv_sec) * 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1395 | if (tv1->tv_usec > tv2->tv_usec) |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1396 | ret += (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1397 | else |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1398 | ret -= (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1399 | return (unsigned long) ret; |
| 1400 | } |
| 1401 | |
| 1402 | /* |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1403 | * returns the difference, in ms, between tv1 and tv2 |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1404 | * Must not be used when either argument is eternity. |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1405 | */ |
| 1406 | static inline unsigned long tv_diff(struct timeval *tv1, struct timeval *tv2) { |
| 1407 | unsigned long ret; |
| 1408 | |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 1409 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
| 1410 | if (tv2->tv_usec > tv1->tv_usec) |
| 1411 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1412 | else |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 1413 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1414 | return (unsigned long) ret; |
| 1415 | } |
| 1416 | |
| 1417 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1418 | * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1419 | * Must not be used when either argument is eternity. Use tv_cmp2_ms() for that. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1420 | */ |
| 1421 | static inline int tv_cmp_ms(struct timeval *tv1, struct timeval *tv2) { |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1422 | if (tv1->tv_sec == tv2->tv_sec) { |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1423 | if (tv2->tv_usec >= tv1->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1424 | return -1; |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1425 | else if (tv1->tv_usec >= tv2->tv_usec + 1000) |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1426 | return 1; |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1427 | else |
| 1428 | return 0; |
| 1429 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1430 | else if ((tv2->tv_sec > tv1->tv_sec + 1) || |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1431 | ((tv2->tv_sec == tv1->tv_sec + 1) && (tv2->tv_usec + 1000000 >= tv1->tv_usec + 1000))) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1432 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1433 | else if ((tv1->tv_sec > tv2->tv_sec + 1) || |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1434 | ((tv1->tv_sec == tv2->tv_sec + 1) && (tv1->tv_usec + 1000000 >= tv2->tv_usec + 1000))) |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1435 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1436 | else |
| 1437 | return 0; |
| 1438 | } |
| 1439 | |
| 1440 | /* |
| 1441 | * returns the remaining time between tv1=now and event=tv2 |
| 1442 | * if tv2 is passed, 0 is returned. |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1443 | * Must not be used when either argument is eternity. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1444 | */ |
| 1445 | static inline unsigned long tv_remain(struct timeval *tv1, struct timeval *tv2) { |
| 1446 | unsigned long ret; |
| 1447 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1448 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 1449 | return 0; /* event elapsed */ |
| 1450 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1451 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1452 | if (tv2->tv_usec > tv1->tv_usec) |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1453 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1454 | else |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1455 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1456 | return (unsigned long) ret; |
| 1457 | } |
| 1458 | |
| 1459 | |
| 1460 | /* |
| 1461 | * zeroes a struct timeval |
| 1462 | */ |
| 1463 | |
| 1464 | static inline struct timeval *tv_eternity(struct timeval *tv) { |
| 1465 | tv->tv_sec = tv->tv_usec = 0; |
| 1466 | return tv; |
| 1467 | } |
| 1468 | |
| 1469 | /* |
| 1470 | * returns 1 if tv is null, else 0 |
| 1471 | */ |
| 1472 | static inline int tv_iseternity(struct timeval *tv) { |
| 1473 | if (tv->tv_sec == 0 && tv->tv_usec == 0) |
| 1474 | return 1; |
| 1475 | else |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
| 1479 | /* |
| 1480 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 1481 | * considering that 0 is the eternity. |
| 1482 | */ |
| 1483 | static inline int tv_cmp2(struct timeval *tv1, struct timeval *tv2) { |
| 1484 | if (tv_iseternity(tv1)) |
| 1485 | if (tv_iseternity(tv2)) |
| 1486 | return 0; /* same */ |
| 1487 | else |
| 1488 | return 1; /* tv1 later than tv2 */ |
| 1489 | else if (tv_iseternity(tv2)) |
| 1490 | return -1; /* tv2 later than tv1 */ |
| 1491 | |
| 1492 | if (tv1->tv_sec > tv2->tv_sec) |
| 1493 | return 1; |
| 1494 | else if (tv1->tv_sec < tv2->tv_sec) |
| 1495 | return -1; |
| 1496 | else if (tv1->tv_usec > tv2->tv_usec) |
| 1497 | return 1; |
| 1498 | else if (tv1->tv_usec < tv2->tv_usec) |
| 1499 | return -1; |
| 1500 | else |
| 1501 | return 0; |
| 1502 | } |
| 1503 | |
| 1504 | /* |
| 1505 | * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 1506 | * considering that 0 is the eternity. |
| 1507 | */ |
| 1508 | static inline int tv_cmp2_ms(struct timeval *tv1, struct timeval *tv2) { |
| 1509 | if (tv_iseternity(tv1)) |
| 1510 | if (tv_iseternity(tv2)) |
| 1511 | return 0; /* same */ |
| 1512 | else |
| 1513 | return 1; /* tv1 later than tv2 */ |
| 1514 | else if (tv_iseternity(tv2)) |
| 1515 | return -1; /* tv2 later than tv1 */ |
| 1516 | |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1517 | if (tv1->tv_sec == tv2->tv_sec) { |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1518 | if (tv1->tv_usec >= tv2->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1519 | return 1; |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1520 | else if (tv2->tv_usec >= tv1->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1521 | return -1; |
| 1522 | else |
| 1523 | return 0; |
| 1524 | } |
| 1525 | else if ((tv1->tv_sec > tv2->tv_sec + 1) || |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1526 | ((tv1->tv_sec == tv2->tv_sec + 1) && (tv1->tv_usec + 1000000 >= tv2->tv_usec + 1000))) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1527 | return 1; |
| 1528 | else if ((tv2->tv_sec > tv1->tv_sec + 1) || |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1529 | ((tv2->tv_sec == tv1->tv_sec + 1) && (tv2->tv_usec + 1000000 >= tv1->tv_usec + 1000))) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1530 | return -1; |
| 1531 | else |
| 1532 | return 0; |
| 1533 | } |
| 1534 | |
| 1535 | /* |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1536 | * returns the remaining time between tv1=now and event=tv2 |
| 1537 | * if tv2 is passed, 0 is returned. |
| 1538 | * Returns TIME_ETERNITY if tv2 is eternity. |
| 1539 | */ |
| 1540 | static inline unsigned long tv_remain2(struct timeval *tv1, struct timeval *tv2) { |
| 1541 | unsigned long ret; |
| 1542 | |
| 1543 | if (tv_iseternity(tv2)) |
| 1544 | return TIME_ETERNITY; |
| 1545 | |
| 1546 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 1547 | return 0; /* event elapsed */ |
| 1548 | |
| 1549 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
| 1550 | if (tv2->tv_usec > tv1->tv_usec) |
| 1551 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
| 1552 | else |
| 1553 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
| 1554 | return (unsigned long) ret; |
| 1555 | } |
| 1556 | |
| 1557 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1558 | * returns the first event between tv1 and tv2 into tvmin. |
| 1559 | * a zero tv is ignored. tvmin is returned. |
| 1560 | */ |
| 1561 | static inline struct timeval *tv_min(struct timeval *tvmin, |
| 1562 | struct timeval *tv1, struct timeval *tv2) { |
| 1563 | |
| 1564 | if (tv_cmp2(tv1, tv2) <= 0) |
| 1565 | *tvmin = *tv1; |
| 1566 | else |
| 1567 | *tvmin = *tv2; |
| 1568 | |
| 1569 | return tvmin; |
| 1570 | } |
| 1571 | |
| 1572 | |
| 1573 | |
| 1574 | /***********************************************************/ |
| 1575 | /* fd management ***************************************/ |
| 1576 | /***********************************************************/ |
| 1577 | |
| 1578 | |
| 1579 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1580 | /* Deletes an FD from the fdsets, and recomputes the maxfd limit. |
| 1581 | * The file descriptor is also closed. |
| 1582 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1583 | static inline void fd_delete(int fd) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1584 | FD_CLR(fd, StaticReadEvent); |
| 1585 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 08dedbe | 2005-12-18 01:13:48 +0100 | [diff] [blame] | 1586 | #if defined(ENABLE_EPOLL) |
| 1587 | if (PrevReadEvent) { |
| 1588 | FD_CLR(fd, PrevReadEvent); |
| 1589 | FD_CLR(fd, PrevWriteEvent); |
| 1590 | } |
| 1591 | #endif |
| 1592 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1593 | close(fd); |
| 1594 | fdtab[fd].state = FD_STCLOSE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1595 | |
| 1596 | while ((maxfd-1 >= 0) && (fdtab[maxfd-1].state == FD_STCLOSE)) |
| 1597 | maxfd--; |
| 1598 | } |
| 1599 | |
| 1600 | /* recomputes the maxfd limit from the fd */ |
| 1601 | static inline void fd_insert(int fd) { |
| 1602 | if (fd+1 > maxfd) |
| 1603 | maxfd = fd+1; |
| 1604 | } |
| 1605 | |
| 1606 | /*************************************************************/ |
| 1607 | /* task management ***************************************/ |
| 1608 | /*************************************************************/ |
| 1609 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1610 | /* puts the task <t> in run queue <q>, and returns <t> */ |
| 1611 | static inline struct task *task_wakeup(struct task **q, struct task *t) { |
| 1612 | if (t->state == TASK_RUNNING) |
| 1613 | return t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1614 | else { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1615 | t->rqnext = *q; |
| 1616 | t->state = TASK_RUNNING; |
| 1617 | return *q = t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1618 | } |
| 1619 | } |
| 1620 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1621 | /* removes the task <t> from the queue <q> |
| 1622 | * <s> MUST be <q>'s first task. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1623 | * set the run queue to point to the next one, and return it |
| 1624 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1625 | static inline struct task *task_sleep(struct task **q, struct task *t) { |
| 1626 | if (t->state == TASK_RUNNING) { |
| 1627 | *q = t->rqnext; |
| 1628 | t->state = TASK_IDLE; /* tell that s has left the run queue */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1629 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1630 | return *q; /* return next running task */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1634 | * removes the task <t> from its wait queue. It must have already been removed |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1635 | * from the run queue. A pointer to the task itself is returned. |
| 1636 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1637 | static inline struct task *task_delete(struct task *t) { |
| 1638 | t->prev->next = t->next; |
| 1639 | t->next->prev = t->prev; |
| 1640 | return t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1644 | * frees a task. Its context must have been freed since it will be lost. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1645 | */ |
| 1646 | static inline void task_free(struct task *t) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1647 | pool_free(task, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1648 | } |
| 1649 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1650 | /* inserts <task> into its assigned wait queue, where it may already be. In this case, it |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1651 | * may be only moved or left where it was, depending on its timing requirements. |
| 1652 | * <task> is returned. |
| 1653 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1654 | struct task *task_queue(struct task *task) { |
| 1655 | struct task *list = task->wq; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1656 | struct task *start_from; |
| 1657 | |
| 1658 | /* first, test if the task was already in a list */ |
| 1659 | if (task->prev == NULL) { |
| 1660 | // start_from = list; |
| 1661 | start_from = list->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1662 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1663 | stats_tsk_new++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1664 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1665 | /* insert the unlinked <task> into the list, searching back from the last entry */ |
| 1666 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1667 | start_from = start_from->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1668 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1669 | stats_tsk_nsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1670 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | // while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1674 | // start_from = start_from->next; |
| 1675 | // stats_tsk_nsrch++; |
| 1676 | // } |
| 1677 | } |
| 1678 | else if (task->prev == list || |
| 1679 | tv_cmp2(&task->expire, &task->prev->expire) >= 0) { /* walk right */ |
| 1680 | start_from = task->next; |
| 1681 | if (start_from == list || tv_cmp2(&task->expire, &start_from->expire) <= 0) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1682 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1683 | stats_tsk_good++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1684 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1685 | return task; /* it's already in the right place */ |
| 1686 | } |
| 1687 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1688 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1689 | stats_tsk_right++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1690 | #endif |
| 1691 | |
| 1692 | /* if the task is not at the right place, there's little chance that |
| 1693 | * it has only shifted a bit, and it will nearly always be queued |
| 1694 | * at the end of the list because of constant timeouts |
| 1695 | * (observed in real case). |
| 1696 | */ |
| 1697 | #ifndef WE_REALLY_THINK_THAT_THIS_TASK_MAY_HAVE_SHIFTED |
| 1698 | start_from = list->prev; /* assume we'll queue to the end of the list */ |
| 1699 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1700 | start_from = start_from->prev; |
| 1701 | #if STATTIME > 0 |
| 1702 | stats_tsk_lsrch++; |
| 1703 | #endif |
| 1704 | } |
| 1705 | #else /* WE_REALLY_... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1706 | /* insert the unlinked <task> into the list, searching after position <start_from> */ |
| 1707 | while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1708 | start_from = start_from->next; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1709 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1710 | stats_tsk_rsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1711 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1712 | } |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1713 | #endif /* WE_REALLY_... */ |
| 1714 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1715 | /* we need to unlink it now */ |
| 1716 | task_delete(task); |
| 1717 | } |
| 1718 | else { /* walk left. */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1719 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1720 | stats_tsk_left++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1721 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1722 | #ifdef LEFT_TO_TOP /* not very good */ |
| 1723 | start_from = list; |
| 1724 | while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1725 | start_from = start_from->next; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1726 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1727 | stats_tsk_lsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1728 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1729 | } |
| 1730 | #else |
| 1731 | start_from = task->prev->prev; /* valid because of the previous test above */ |
| 1732 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1733 | start_from = start_from->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1734 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1735 | stats_tsk_lsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1736 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1737 | } |
| 1738 | #endif |
| 1739 | /* we need to unlink it now */ |
| 1740 | task_delete(task); |
| 1741 | } |
| 1742 | task->prev = start_from; |
| 1743 | task->next = start_from->next; |
| 1744 | task->next->prev = task; |
| 1745 | start_from->next = task; |
| 1746 | return task; |
| 1747 | } |
| 1748 | |
| 1749 | |
| 1750 | /*********************************************************************/ |
| 1751 | /* more specific functions ***************************************/ |
| 1752 | /*********************************************************************/ |
| 1753 | |
| 1754 | /* some prototypes */ |
| 1755 | static int maintain_proxies(void); |
| 1756 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1757 | /* This either returns the sockname or the original destination address. Code |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1758 | * inspired from Patrick Schaaf's example of nf_getsockname() implementation. |
| 1759 | */ |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1760 | static int get_original_dst(int fd, struct sockaddr_in *sa, socklen_t *salen) { |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1761 | #if defined(TPROXY) && defined(SO_ORIGINAL_DST) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1762 | return getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, (void *)sa, salen); |
| 1763 | #else |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1764 | #if defined(TPROXY) && defined(USE_GETSOCKNAME) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1765 | return getsockname(fd, (struct sockaddr *)sa, salen); |
| 1766 | #else |
| 1767 | return -1; |
| 1768 | #endif |
| 1769 | #endif |
| 1770 | } |
| 1771 | |
| 1772 | /* |
| 1773 | * frees the context associated to a session. It must have been removed first. |
| 1774 | */ |
| 1775 | static inline void session_free(struct session *s) { |
| 1776 | if (s->req) |
| 1777 | pool_free(buffer, s->req); |
| 1778 | if (s->rep) |
| 1779 | pool_free(buffer, s->rep); |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1780 | |
| 1781 | if (s->rsp_cap != NULL) { |
| 1782 | struct cap_hdr *h; |
| 1783 | for (h = s->proxy->rsp_cap; h; h = h->next) { |
| 1784 | if (s->rsp_cap[h->index] != NULL) |
| 1785 | pool_free_to(h->pool, s->rsp_cap[h->index]); |
| 1786 | } |
| 1787 | pool_free_to(s->proxy->rsp_cap_pool, s->rsp_cap); |
| 1788 | } |
| 1789 | if (s->req_cap != NULL) { |
| 1790 | struct cap_hdr *h; |
| 1791 | for (h = s->proxy->req_cap; h; h = h->next) { |
| 1792 | if (s->req_cap[h->index] != NULL) |
| 1793 | pool_free_to(h->pool, s->req_cap[h->index]); |
| 1794 | } |
| 1795 | pool_free_to(s->proxy->req_cap_pool, s->req_cap); |
| 1796 | } |
| 1797 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1798 | if (s->logs.uri) |
| 1799 | pool_free(requri, s->logs.uri); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1800 | if (s->logs.cli_cookie) |
| 1801 | pool_free(capture, s->logs.cli_cookie); |
| 1802 | if (s->logs.srv_cookie) |
| 1803 | pool_free(capture, s->logs.srv_cookie); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1804 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1805 | pool_free(session, s); |
| 1806 | } |
| 1807 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1808 | |
| 1809 | /* |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 1810 | * This function recounts the number of usable active and backup servers for |
| 1811 | * proxy <p>. These numbers are returned into the p->srv_act and p->srv_bck. |
| 1812 | */ |
| 1813 | static inline void recount_servers(struct proxy *px) { |
| 1814 | struct server *srv; |
| 1815 | |
| 1816 | px->srv_act = 0; px->srv_bck = 0; |
| 1817 | for (srv = px->srv; srv != NULL; srv = srv->next) { |
| 1818 | if (srv->state & SRV_RUNNING) { |
| 1819 | if (srv->state & SRV_BACKUP) |
| 1820 | px->srv_bck++; |
| 1821 | else |
| 1822 | px->srv_act++; |
| 1823 | } |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | /* |
| 1828 | * This function tries to find a running server for the proxy <px> following |
| 1829 | * the round-robin method. Depending on the number of active/backup servers, |
| 1830 | * it will either look for active servers, or for backup servers. |
| 1831 | * If any server is found, it will be returned and px->cursrv will be updated |
| 1832 | * to point to the next server. If no valid server is found, NULL is returned. |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1833 | */ |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 1834 | static inline struct server *get_server_rr(struct proxy *px) { |
| 1835 | struct server *srv; |
willy tarreau | 72e583d | 2006-03-23 11:27:02 +0100 | [diff] [blame] | 1836 | struct server *end; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1837 | |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 1838 | if (px->srv_act) { |
| 1839 | srv = px->cursrv; |
willy tarreau | 72e583d | 2006-03-23 11:27:02 +0100 | [diff] [blame] | 1840 | if (srv == NULL) |
| 1841 | srv = px->srv; |
| 1842 | end = srv; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1843 | do { |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 1844 | if ((srv->state & (SRV_RUNNING | SRV_BACKUP)) == SRV_RUNNING) { |
| 1845 | px->cursrv = srv->next; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1846 | return srv; |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 1847 | } |
| 1848 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1849 | srv = srv->next; |
willy tarreau | 72e583d | 2006-03-23 11:27:02 +0100 | [diff] [blame] | 1850 | if (srv == NULL) |
| 1851 | srv = px->srv; |
| 1852 | } while (srv != end); |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 1853 | /* note that theorically we should not get there */ |
| 1854 | } |
Willy TARREAU | 3481c46 | 2006-03-01 22:37:57 +0100 | [diff] [blame] | 1855 | |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 1856 | if (px->srv_bck) { |
Willy TARREAU | 3481c46 | 2006-03-01 22:37:57 +0100 | [diff] [blame] | 1857 | /* By default, we look for the first backup server if all others are |
| 1858 | * DOWN. But in some cases, it may be desirable to load-balance across |
| 1859 | * all backup servers. |
| 1860 | */ |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 1861 | if (px->options & PR_O_USE_ALL_BK) |
| 1862 | srv = px->cursrv; |
| 1863 | else |
| 1864 | srv = px->srv; |
| 1865 | |
| 1866 | if (srv == NULL) |
Willy TARREAU | 3481c46 | 2006-03-01 22:37:57 +0100 | [diff] [blame] | 1867 | srv = px->srv; |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 1868 | end = srv; |
| 1869 | do { |
| 1870 | if (srv->state & SRV_RUNNING) { |
| 1871 | px->cursrv = srv->next; |
| 1872 | return srv; |
| 1873 | } |
| 1874 | srv = srv->next; |
| 1875 | if (srv == NULL) |
| 1876 | srv = px->srv; |
| 1877 | } while (srv != end); |
| 1878 | /* note that theorically we should not get there */ |
| 1879 | } |
Willy TARREAU | 3481c46 | 2006-03-01 22:37:57 +0100 | [diff] [blame] | 1880 | |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 1881 | /* if we get there, it means there are no available servers at all */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1882 | return NULL; |
| 1883 | } |
| 1884 | |
willy tarreau | 62084d4 | 2006-03-24 18:57:41 +0100 | [diff] [blame] | 1885 | |
| 1886 | /* |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 1887 | * This function tries to find a running server for the proxy <px> following |
| 1888 | * the source hash method. Depending on the number of active/backup servers, |
| 1889 | * it will either look for active servers, or for backup servers. |
| 1890 | * If any server is found, it will be returned. If no valid server is found, |
| 1891 | * NULL is returned. |
| 1892 | */ |
| 1893 | static inline struct server *get_server_sh(struct proxy *px, char *addr, int len) { |
| 1894 | struct server *srv; |
| 1895 | |
| 1896 | if (px->srv_act) { |
| 1897 | unsigned int h, l; |
| 1898 | |
| 1899 | l = h = 0; |
| 1900 | if (px->srv_act > 1) { |
| 1901 | while ((l + sizeof (int)) <= len) { |
| 1902 | h ^= ntohl(*(unsigned int *)(&addr[l])); |
| 1903 | l += sizeof (int); |
| 1904 | } |
| 1905 | h %= px->srv_act; |
| 1906 | } |
| 1907 | |
| 1908 | for (srv = px->srv; srv; srv = srv->next) { |
| 1909 | if ((srv->state & (SRV_RUNNING | SRV_BACKUP)) == SRV_RUNNING) { |
| 1910 | if (!h) |
| 1911 | return srv; |
| 1912 | h--; |
| 1913 | } |
| 1914 | } |
| 1915 | /* note that theorically we should not get there */ |
| 1916 | } |
| 1917 | |
| 1918 | if (px->srv_bck) { |
| 1919 | unsigned int h, l; |
| 1920 | |
| 1921 | /* By default, we look for the first backup server if all others are |
| 1922 | * DOWN. But in some cases, it may be desirable to load-balance across |
| 1923 | * all backup servers. |
| 1924 | */ |
| 1925 | l = h = 0; |
| 1926 | if (px->srv_bck > 1 && px->options & PR_O_USE_ALL_BK) { |
| 1927 | while ((l + sizeof (int)) <= len) { |
| 1928 | h ^= ntohl(*(unsigned int *)(&addr[l])); |
| 1929 | l += sizeof (int); |
| 1930 | } |
| 1931 | h %= px->srv_bck; |
| 1932 | } |
| 1933 | |
| 1934 | for (srv = px->srv; srv; srv = srv->next) { |
| 1935 | if (srv->state & SRV_RUNNING) { |
| 1936 | if (!h) |
| 1937 | return srv; |
| 1938 | h--; |
| 1939 | } |
| 1940 | } |
| 1941 | /* note that theorically we should not get there */ |
| 1942 | } |
| 1943 | |
| 1944 | /* if we get there, it means there are no available servers at all */ |
| 1945 | return NULL; |
| 1946 | } |
| 1947 | |
| 1948 | |
| 1949 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1950 | * This function initiates a connection to the current server (s->srv) if (s->direct) |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1951 | * is set, or to the dispatch server if (s->direct) is 0. |
| 1952 | * It can return one of : |
| 1953 | * - SN_ERR_NONE if everything's OK |
| 1954 | * - SN_ERR_SRVTO if there are no more servers |
| 1955 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1956 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1957 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1958 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1959 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1960 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1961 | int connect_server(struct session *s) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1962 | int fd; |
| 1963 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 1964 | #ifdef DEBUG_FULL |
| 1965 | fprintf(stderr,"connect_server : s=%p\n",s); |
| 1966 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1967 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 1968 | if (s->flags & SN_DIRECT) { /* srv cannot be null */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1969 | s->srv_addr = s->srv->addr; |
| 1970 | } |
| 1971 | else if (s->proxy->options & PR_O_BALANCE) { |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 1972 | /* Ensure that srv will not be NULL */ |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 1973 | if (!s->proxy->srv_act && !s->proxy->srv_bck) |
| 1974 | return SN_ERR_SRVTO; |
| 1975 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1976 | if (s->proxy->options & PR_O_BALANCE_RR) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1977 | struct server *srv; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1978 | |
willy tarreau | 4c8c2b5 | 2006-03-24 19:36:41 +0100 | [diff] [blame] | 1979 | srv = get_server_rr(s->proxy); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1980 | s->srv_addr = srv->addr; |
| 1981 | s->srv = srv; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1982 | } |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 1983 | else if (s->proxy->options & PR_O_BALANCE_SH) { |
| 1984 | struct server *srv; |
| 1985 | int len; |
| 1986 | |
| 1987 | if (s->cli_addr.ss_family == AF_INET) |
| 1988 | len = 4; |
| 1989 | else if (s->cli_addr.ss_family == AF_INET6) |
| 1990 | len = 16; |
| 1991 | else /* unknown IP family */ |
| 1992 | return SN_ERR_INTERNAL; |
| 1993 | |
| 1994 | srv = get_server_sh(s->proxy, |
| 1995 | (void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1996 | len); |
| 1997 | s->srv_addr = srv->addr; |
| 1998 | s->srv = srv; |
| 1999 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2000 | else /* unknown balancing algorithm */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2001 | return SN_ERR_INTERNAL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2002 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2003 | else if (*(int *)&s->proxy->dispatch_addr.sin_addr) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2004 | /* connect to the defined dispatch addr */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2005 | s->srv_addr = s->proxy->dispatch_addr; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2006 | } |
| 2007 | else if (s->proxy->options & PR_O_TRANSP) { |
| 2008 | /* in transparent mode, use the original dest addr if no dispatch specified */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2009 | socklen_t salen = sizeof(s->srv_addr); |
| 2010 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2011 | if (get_original_dst(s->cli_fd, &s->srv_addr, &salen) == -1) { |
| 2012 | qfprintf(stderr, "Cannot get original server address.\n"); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2013 | return SN_ERR_INTERNAL; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2014 | } |
| 2015 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2016 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 2017 | /* if this server remaps proxied ports, we'll use |
| 2018 | * the port the client connected to with an offset. */ |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 2019 | if (s->srv != NULL && s->srv->state & SRV_MAPPORTS) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 2020 | struct sockaddr_in sockname; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2021 | socklen_t namelen = sizeof(sockname); |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 2022 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2023 | if (!(s->proxy->options & PR_O_TRANSP) || |
| 2024 | get_original_dst(s->cli_fd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 2025 | getsockname(s->cli_fd, (struct sockaddr *)&sockname, &namelen); |
| 2026 | s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) + ntohs(sockname.sin_port)); |
| 2027 | } |
| 2028 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2029 | if ((fd = s->srv_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2030 | qfprintf(stderr, "Cannot get a server socket.\n"); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2031 | |
| 2032 | if (errno == ENFILE) |
| 2033 | send_log(s->proxy, LOG_EMERG, |
| 2034 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 2035 | s->proxy->id, maxfd); |
| 2036 | else if (errno == EMFILE) |
| 2037 | send_log(s->proxy, LOG_EMERG, |
| 2038 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 2039 | s->proxy->id, maxfd); |
| 2040 | else if (errno == ENOBUFS || errno == ENOMEM) |
| 2041 | send_log(s->proxy, LOG_EMERG, |
| 2042 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 2043 | s->proxy->id, maxfd); |
| 2044 | /* this is a resource error */ |
| 2045 | return SN_ERR_RESOURCE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2046 | } |
| 2047 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2048 | if (fd >= global.maxsock) { |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2049 | /* do not log anything there, it's a normal condition when this option |
| 2050 | * is used to serialize connections to a server ! |
| 2051 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2052 | Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 2053 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2054 | return SN_ERR_PRXCOND; /* it is a configuration limit */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2055 | } |
| 2056 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2057 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
| 2058 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2059 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2060 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2061 | return SN_ERR_INTERNAL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2062 | } |
| 2063 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2064 | if (s->proxy->options & PR_O_TCP_SRV_KA) |
| 2065 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 2066 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 2067 | /* allow specific binding : |
| 2068 | * - server-specific at first |
| 2069 | * - proxy-specific next |
| 2070 | */ |
| 2071 | if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) { |
| 2072 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 2073 | if (bind(fd, (struct sockaddr *)&s->srv->source_addr, sizeof(s->srv->source_addr)) == -1) { |
| 2074 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 2075 | s->proxy->id, s->srv->id); |
| 2076 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2077 | send_log(s->proxy, LOG_EMERG, |
| 2078 | "Cannot bind to source address before connect() for server %s/%s.\n", |
| 2079 | s->proxy->id, s->srv->id); |
| 2080 | return SN_ERR_RESOURCE; |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 2081 | } |
| 2082 | } |
| 2083 | else if (s->proxy->options & PR_O_BIND_SRC) { |
| 2084 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 2085 | if (bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) { |
| 2086 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", s->proxy->id); |
| 2087 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2088 | send_log(s->proxy, LOG_EMERG, |
| 2089 | "Cannot bind to source address before connect() for server %s/%s.\n", |
| 2090 | s->proxy->id, s->srv->id); |
| 2091 | return SN_ERR_RESOURCE; |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 2092 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2093 | } |
| 2094 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2095 | if ((connect(fd, (struct sockaddr *)&s->srv_addr, sizeof(s->srv_addr)) == -1) && |
| 2096 | (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) { |
| 2097 | |
| 2098 | if (errno == EAGAIN || errno == EADDRINUSE) { |
| 2099 | char *msg; |
| 2100 | if (errno == EAGAIN) /* no free ports left, try again later */ |
| 2101 | msg = "no free ports"; |
| 2102 | else |
| 2103 | msg = "local address already in use"; |
| 2104 | |
| 2105 | qfprintf(stderr,"Cannot connect: %s.\n",msg); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2106 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2107 | send_log(s->proxy, LOG_EMERG, |
| 2108 | "Connect() failed for server %s/%s: %s.\n", |
| 2109 | s->proxy->id, s->srv->id, msg); |
| 2110 | return SN_ERR_RESOURCE; |
| 2111 | } else if (errno == ETIMEDOUT) { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2112 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2113 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2114 | return SN_ERR_SRVTO; |
| 2115 | } else { |
| 2116 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2117 | //qfprintf(stderr,"Connect(): %d", errno); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2118 | close(fd); |
| 2119 | return SN_ERR_SRVCL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2120 | } |
| 2121 | } |
| 2122 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2123 | fdtab[fd].owner = s->task; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2124 | fdtab[fd].read = &event_srv_read; |
| 2125 | fdtab[fd].write = &event_srv_write; |
| 2126 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 2127 | |
| 2128 | FD_SET(fd, StaticWriteEvent); /* for connect status */ |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 2129 | #if defined(DEBUG_FULL) && defined(ENABLE_EPOLL) |
| 2130 | if (PrevReadEvent) { |
| 2131 | assert(!(FD_ISSET(fd, PrevReadEvent))); |
| 2132 | assert(!(FD_ISSET(fd, PrevWriteEvent))); |
| 2133 | } |
| 2134 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2135 | |
| 2136 | fd_insert(fd); |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 2137 | s->srv->cur_sess++; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2138 | |
| 2139 | if (s->proxy->contimeout) |
| 2140 | tv_delayfrom(&s->cnexpire, &now, s->proxy->contimeout); |
| 2141 | else |
| 2142 | tv_eternity(&s->cnexpire); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2143 | return SN_ERR_NONE; /* connection is OK */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | /* |
| 2147 | * this function is called on a read event from a client socket. |
| 2148 | * It returns 0. |
| 2149 | */ |
| 2150 | int event_cli_read(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2151 | struct task *t = fdtab[fd].owner; |
| 2152 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2153 | struct buffer *b = s->req; |
| 2154 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2155 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2156 | #ifdef DEBUG_FULL |
| 2157 | fprintf(stderr,"event_cli_read : fd=%d, s=%p\n", fd, s); |
| 2158 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2159 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2160 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2161 | #ifdef FILL_BUFFERS |
| 2162 | while (1) |
| 2163 | #else |
| 2164 | do |
| 2165 | #endif |
| 2166 | { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2167 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 2168 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2169 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2170 | } |
| 2171 | else if (b->r > b->w) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2172 | max = b->rlim - b->r; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2173 | } |
| 2174 | else { |
| 2175 | max = b->w - b->r; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2176 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 2177 | * since it means that the rewrite protection has been removed. This |
| 2178 | * implies that the if statement can be removed. |
| 2179 | */ |
| 2180 | if (max > b->rlim - b->data) |
| 2181 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | if (max == 0) { /* not anymore room to store data */ |
| 2185 | FD_CLR(fd, StaticReadEvent); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2186 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2187 | } |
| 2188 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2189 | #ifndef MSG_NOSIGNAL |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2190 | { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2191 | int skerr; |
| 2192 | socklen_t lskerr = sizeof(skerr); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2193 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2194 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2195 | if (skerr) |
| 2196 | ret = -1; |
| 2197 | else |
| 2198 | ret = recv(fd, b->r, max, 0); |
| 2199 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2200 | #else |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2201 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2202 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2203 | if (ret > 0) { |
| 2204 | b->r += ret; |
| 2205 | b->l += ret; |
| 2206 | s->res_cr = RES_DATA; |
| 2207 | |
| 2208 | if (b->r == b->data + BUFSIZE) { |
| 2209 | b->r = b->data; /* wrap around the buffer */ |
| 2210 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2211 | |
| 2212 | b->total += ret; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2213 | /* we hope to read more data or to get a close on next round */ |
| 2214 | continue; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2215 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2216 | else if (ret == 0) { |
| 2217 | s->res_cr = RES_NULL; |
| 2218 | break; |
| 2219 | } |
| 2220 | else if (errno == EAGAIN) {/* ignore EAGAIN */ |
| 2221 | break; |
| 2222 | } |
| 2223 | else { |
| 2224 | s->res_cr = RES_ERROR; |
| 2225 | fdtab[fd].state = FD_STERROR; |
| 2226 | break; |
| 2227 | } |
| 2228 | } /* while(1) */ |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2229 | #ifndef FILL_BUFFERS |
| 2230 | while (0); |
| 2231 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2232 | } |
| 2233 | else { |
| 2234 | s->res_cr = RES_ERROR; |
| 2235 | fdtab[fd].state = FD_STERROR; |
| 2236 | } |
| 2237 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2238 | if (s->res_cr != RES_SILENT) { |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2239 | if (s->proxy->clitimeout && FD_ISSET(fd, StaticReadEvent)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2240 | tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout); |
| 2241 | else |
| 2242 | tv_eternity(&s->crexpire); |
| 2243 | |
| 2244 | task_wakeup(&rq, t); |
| 2245 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2246 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2247 | return 0; |
| 2248 | } |
| 2249 | |
| 2250 | |
| 2251 | /* |
| 2252 | * this function is called on a read event from a server socket. |
| 2253 | * It returns 0. |
| 2254 | */ |
| 2255 | int event_srv_read(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2256 | struct task *t = fdtab[fd].owner; |
| 2257 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2258 | struct buffer *b = s->rep; |
| 2259 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2260 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2261 | #ifdef DEBUG_FULL |
| 2262 | fprintf(stderr,"event_srv_read : fd=%d, s=%p\n", fd, s); |
| 2263 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2264 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2265 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2266 | #ifdef FILL_BUFFERS |
| 2267 | while (1) |
| 2268 | #else |
| 2269 | do |
| 2270 | #endif |
| 2271 | { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2272 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 2273 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2274 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2275 | } |
| 2276 | else if (b->r > b->w) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2277 | max = b->rlim - b->r; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2278 | } |
| 2279 | else { |
| 2280 | max = b->w - b->r; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2281 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 2282 | * since it means that the rewrite protection has been removed. This |
| 2283 | * implies that the if statement can be removed. |
| 2284 | */ |
| 2285 | if (max > b->rlim - b->data) |
| 2286 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | if (max == 0) { /* not anymore room to store data */ |
| 2290 | FD_CLR(fd, StaticReadEvent); |
| 2291 | break; |
| 2292 | } |
| 2293 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2294 | #ifndef MSG_NOSIGNAL |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2295 | { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2296 | int skerr; |
| 2297 | socklen_t lskerr = sizeof(skerr); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2298 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2299 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2300 | if (skerr) |
| 2301 | ret = -1; |
| 2302 | else |
| 2303 | ret = recv(fd, b->r, max, 0); |
| 2304 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2305 | #else |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2306 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2307 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2308 | if (ret > 0) { |
| 2309 | b->r += ret; |
| 2310 | b->l += ret; |
| 2311 | s->res_sr = RES_DATA; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2312 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2313 | if (b->r == b->data + BUFSIZE) { |
| 2314 | b->r = b->data; /* wrap around the buffer */ |
| 2315 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2316 | |
| 2317 | b->total += ret; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2318 | /* we hope to read more data or to get a close on next round */ |
| 2319 | continue; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2320 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2321 | else if (ret == 0) { |
| 2322 | s->res_sr = RES_NULL; |
| 2323 | break; |
| 2324 | } |
| 2325 | else if (errno == EAGAIN) {/* ignore EAGAIN */ |
| 2326 | break; |
| 2327 | } |
| 2328 | else { |
| 2329 | s->res_sr = RES_ERROR; |
| 2330 | fdtab[fd].state = FD_STERROR; |
| 2331 | break; |
| 2332 | } |
| 2333 | } /* while(1) */ |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2334 | #ifndef FILL_BUFFERS |
| 2335 | while (0); |
| 2336 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2337 | } |
| 2338 | else { |
| 2339 | s->res_sr = RES_ERROR; |
| 2340 | fdtab[fd].state = FD_STERROR; |
| 2341 | } |
| 2342 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2343 | if (s->res_sr != RES_SILENT) { |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2344 | if (s->proxy->srvtimeout && FD_ISSET(fd, StaticReadEvent)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2345 | tv_delayfrom(&s->srexpire, &now, s->proxy->srvtimeout); |
| 2346 | else |
| 2347 | tv_eternity(&s->srexpire); |
| 2348 | |
| 2349 | task_wakeup(&rq, t); |
| 2350 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2351 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2352 | return 0; |
| 2353 | } |
| 2354 | |
| 2355 | /* |
| 2356 | * this function is called on a write event from a client socket. |
| 2357 | * It returns 0. |
| 2358 | */ |
| 2359 | int event_cli_write(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2360 | struct task *t = fdtab[fd].owner; |
| 2361 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2362 | struct buffer *b = s->rep; |
| 2363 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2364 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2365 | #ifdef DEBUG_FULL |
| 2366 | fprintf(stderr,"event_cli_write : fd=%d, s=%p\n", fd, s); |
| 2367 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2368 | |
| 2369 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 2370 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2371 | // max = BUFSIZE; BUG !!!! |
| 2372 | max = 0; |
| 2373 | } |
| 2374 | else if (b->r > b->w) { |
| 2375 | max = b->r - b->w; |
| 2376 | } |
| 2377 | else |
| 2378 | max = b->data + BUFSIZE - b->w; |
| 2379 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2380 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2381 | if (max == 0) { |
| 2382 | s->res_cw = RES_NULL; |
| 2383 | task_wakeup(&rq, t); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2384 | tv_eternity(&s->cwexpire); |
| 2385 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2386 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2387 | } |
| 2388 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2389 | #ifndef MSG_NOSIGNAL |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2390 | { |
| 2391 | int skerr; |
| 2392 | socklen_t lskerr = sizeof(skerr); |
| 2393 | |
| 2394 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2395 | if (skerr) |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2396 | ret = -1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2397 | else |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2398 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2399 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2400 | #else |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2401 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2402 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2403 | |
| 2404 | if (ret > 0) { |
| 2405 | b->l -= ret; |
| 2406 | b->w += ret; |
| 2407 | |
| 2408 | s->res_cw = RES_DATA; |
| 2409 | |
| 2410 | if (b->w == b->data + BUFSIZE) { |
| 2411 | b->w = b->data; /* wrap around the buffer */ |
| 2412 | } |
| 2413 | } |
| 2414 | else if (ret == 0) { |
| 2415 | /* nothing written, just make as if we were never called */ |
| 2416 | // s->res_cw = RES_NULL; |
| 2417 | return 0; |
| 2418 | } |
| 2419 | else if (errno == EAGAIN) /* ignore EAGAIN */ |
| 2420 | return 0; |
| 2421 | else { |
| 2422 | s->res_cw = RES_ERROR; |
| 2423 | fdtab[fd].state = FD_STERROR; |
| 2424 | } |
| 2425 | } |
| 2426 | else { |
| 2427 | s->res_cw = RES_ERROR; |
| 2428 | fdtab[fd].state = FD_STERROR; |
| 2429 | } |
| 2430 | |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2431 | if (s->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2432 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2433 | /* FIXME: to avoid the client to read-time-out during writes, we refresh it */ |
| 2434 | s->crexpire = s->cwexpire; |
| 2435 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2436 | else |
| 2437 | tv_eternity(&s->cwexpire); |
| 2438 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2439 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2440 | return 0; |
| 2441 | } |
| 2442 | |
| 2443 | |
| 2444 | /* |
| 2445 | * this function is called on a write event from a server socket. |
| 2446 | * It returns 0. |
| 2447 | */ |
| 2448 | int event_srv_write(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2449 | struct task *t = fdtab[fd].owner; |
| 2450 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2451 | struct buffer *b = s->req; |
| 2452 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2453 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2454 | #ifdef DEBUG_FULL |
| 2455 | fprintf(stderr,"event_srv_write : fd=%d, s=%p\n", fd, s); |
| 2456 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2457 | |
| 2458 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 2459 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2460 | // max = BUFSIZE; BUG !!!! |
| 2461 | max = 0; |
| 2462 | } |
| 2463 | else if (b->r > b->w) { |
| 2464 | max = b->r - b->w; |
| 2465 | } |
| 2466 | else |
| 2467 | max = b->data + BUFSIZE - b->w; |
| 2468 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2469 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2470 | if (max == 0) { |
| 2471 | /* may be we have received a connection acknowledgement in TCP mode without data */ |
willy tarreau | 48b0659 | 2005-12-18 01:37:12 +0100 | [diff] [blame] | 2472 | if (s->srv_state == SV_STCONN) { |
| 2473 | int skerr; |
| 2474 | socklen_t lskerr = sizeof(skerr); |
| 2475 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2476 | if (skerr) { |
| 2477 | s->res_sw = RES_ERROR; |
| 2478 | fdtab[fd].state = FD_STERROR; |
| 2479 | task_wakeup(&rq, t); |
| 2480 | tv_eternity(&s->swexpire); |
| 2481 | FD_CLR(fd, StaticWriteEvent); |
| 2482 | return 0; |
| 2483 | } |
| 2484 | } |
| 2485 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2486 | s->res_sw = RES_NULL; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2487 | task_wakeup(&rq, t); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2488 | fdtab[fd].state = FD_STREADY; |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2489 | tv_eternity(&s->swexpire); |
| 2490 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2491 | return 0; |
| 2492 | } |
| 2493 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2494 | #ifndef MSG_NOSIGNAL |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2495 | { |
| 2496 | int skerr; |
| 2497 | socklen_t lskerr = sizeof(skerr); |
| 2498 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2499 | if (skerr) |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2500 | ret = -1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2501 | else |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2502 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2503 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2504 | #else |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2505 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2506 | #endif |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2507 | fdtab[fd].state = FD_STREADY; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2508 | if (ret > 0) { |
| 2509 | b->l -= ret; |
| 2510 | b->w += ret; |
| 2511 | |
| 2512 | s->res_sw = RES_DATA; |
| 2513 | |
| 2514 | if (b->w == b->data + BUFSIZE) { |
| 2515 | b->w = b->data; /* wrap around the buffer */ |
| 2516 | } |
| 2517 | } |
| 2518 | else if (ret == 0) { |
| 2519 | /* nothing written, just make as if we were never called */ |
| 2520 | // s->res_sw = RES_NULL; |
| 2521 | return 0; |
| 2522 | } |
| 2523 | else if (errno == EAGAIN) /* ignore EAGAIN */ |
| 2524 | return 0; |
| 2525 | else { |
| 2526 | s->res_sw = RES_ERROR; |
| 2527 | fdtab[fd].state = FD_STERROR; |
| 2528 | } |
| 2529 | } |
| 2530 | else { |
| 2531 | s->res_sw = RES_ERROR; |
| 2532 | fdtab[fd].state = FD_STERROR; |
| 2533 | } |
| 2534 | |
Willy TARREAU | 1cec83c | 2006-03-01 22:33:49 +0100 | [diff] [blame] | 2535 | /* We don't want to re-arm read/write timeouts if we're trying to connect, |
| 2536 | * otherwise it could loop indefinitely ! |
| 2537 | */ |
| 2538 | if (s->srv_state != SV_STCONN) { |
| 2539 | if (s->proxy->srvtimeout) { |
| 2540 | tv_delayfrom(&s->swexpire, &now, s->proxy->srvtimeout); |
| 2541 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 2542 | s->srexpire = s->swexpire; |
| 2543 | } |
| 2544 | else |
| 2545 | tv_eternity(&s->swexpire); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2546 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2547 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2548 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2549 | return 0; |
| 2550 | } |
| 2551 | |
| 2552 | |
| 2553 | /* |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2554 | * returns a message to the client ; the connection is shut down for read, |
| 2555 | * and the request is cleared so that no server connection can be initiated. |
| 2556 | * The client must be in a valid state for this (HEADER, DATA ...). |
| 2557 | * Nothing is performed on the server side. |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2558 | * The reply buffer doesn't need to be empty before this. |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2559 | */ |
| 2560 | void client_retnclose(struct session *s, int len, const char *msg) { |
| 2561 | FD_CLR(s->cli_fd, StaticReadEvent); |
| 2562 | FD_SET(s->cli_fd, StaticWriteEvent); |
| 2563 | tv_eternity(&s->crexpire); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2564 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2565 | shutdown(s->cli_fd, SHUT_RD); |
| 2566 | s->cli_state = CL_STSHUTR; |
| 2567 | strcpy(s->rep->data, msg); |
| 2568 | s->rep->l = len; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2569 | s->rep->r = s->rep->h = s->rep->lr = s->rep->w = s->rep->data; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2570 | s->rep->r += len; |
| 2571 | s->req->l = 0; |
| 2572 | } |
| 2573 | |
| 2574 | |
| 2575 | /* |
| 2576 | * returns a message into the rep buffer, and flushes the req buffer. |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2577 | * The reply buffer doesn't need to be empty before this. |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2578 | */ |
| 2579 | void client_return(struct session *s, int len, const char *msg) { |
| 2580 | strcpy(s->rep->data, msg); |
| 2581 | s->rep->l = len; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2582 | s->rep->r = s->rep->h = s->rep->lr = s->rep->w = s->rep->data; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2583 | s->rep->r += len; |
| 2584 | s->req->l = 0; |
| 2585 | } |
| 2586 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2587 | /* |
| 2588 | * send a log for the session when we have enough info about it |
| 2589 | */ |
| 2590 | void sess_log(struct session *s) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2591 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2592 | struct proxy *p = s->proxy; |
| 2593 | int log; |
| 2594 | char *uri; |
| 2595 | char *pxid; |
| 2596 | char *srv; |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2597 | struct tm *tm; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2598 | |
| 2599 | /* This is a first attempt at a better logging system. |
| 2600 | * For now, we rely on send_log() to provide the date, although it obviously |
| 2601 | * is the date of the log and not of the request, and most fields are not |
| 2602 | * computed. |
| 2603 | */ |
| 2604 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2605 | log = p->to_log & ~s->logs.logwait; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2606 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2607 | if (s->cli_addr.ss_family == AF_INET) |
| 2608 | inet_ntop(AF_INET, |
| 2609 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 2610 | pn, sizeof(pn)); |
| 2611 | else |
| 2612 | inet_ntop(AF_INET6, |
| 2613 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 2614 | pn, sizeof(pn)); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2615 | |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2616 | uri = (log & LW_REQ) ? s->logs.uri ? s->logs.uri : "<BADREQ>" : ""; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2617 | pxid = p->id; |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2618 | srv = (p->to_log & LW_SVID) ? (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "-"; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2619 | |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2620 | tm = localtime(&s->logs.tv_accept.tv_sec); |
| 2621 | if (p->to_log & LW_REQ) { |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2622 | char tmpline[MAX_SYSLOG_LEN], *h; |
| 2623 | int hdr; |
| 2624 | |
| 2625 | h = tmpline; |
| 2626 | if (p->to_log & LW_REQHDR && (h < tmpline + sizeof(tmpline) - 10)) { |
| 2627 | *(h++) = ' '; |
| 2628 | *(h++) = '{'; |
| 2629 | for (hdr = 0; hdr < p->nb_req_cap; hdr++) { |
| 2630 | if (hdr) |
| 2631 | *(h++) = '|'; |
| 2632 | if (s->req_cap[hdr] != NULL) |
| 2633 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, '#', hdr_encode_map, s->req_cap[hdr]); |
| 2634 | } |
| 2635 | *(h++) = '}'; |
| 2636 | } |
| 2637 | |
| 2638 | if (p->to_log & LW_RSPHDR && (h < tmpline + sizeof(tmpline) - 7)) { |
| 2639 | *(h++) = ' '; |
| 2640 | *(h++) = '{'; |
| 2641 | for (hdr = 0; hdr < p->nb_rsp_cap; hdr++) { |
| 2642 | if (hdr) |
| 2643 | *(h++) = '|'; |
| 2644 | if (s->rsp_cap[hdr] != NULL) |
| 2645 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, '#', hdr_encode_map, s->rsp_cap[hdr]); |
| 2646 | } |
| 2647 | *(h++) = '}'; |
| 2648 | } |
| 2649 | |
| 2650 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 2651 | *(h++) = ' '; |
| 2652 | *(h++) = '"'; |
| 2653 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, '#', url_encode_map, uri); |
| 2654 | *(h++) = '"'; |
| 2655 | } |
| 2656 | *h = '\0'; |
| 2657 | |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 2658 | send_log(p, LOG_INFO, "%s:%d [%02d/%s/%04d:%02d:%02d:%02d] %s %s %d/%d/%d/%s%d %d %s%lld %s %s %c%c%c%c %d/%d/%d%s\n", |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2659 | pn, |
| 2660 | (s->cli_addr.ss_family == AF_INET) ? |
| 2661 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 2662 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2663 | tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900, |
| 2664 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 2665 | pxid, srv, |
| 2666 | s->logs.t_request, |
| 2667 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_request : -1, |
| 2668 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 2669 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 2670 | s->logs.status, |
| 2671 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.bytes, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2672 | s->logs.cli_cookie ? s->logs.cli_cookie : "-", |
| 2673 | s->logs.srv_cookie ? s->logs.srv_cookie : "-", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2674 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 2675 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 2676 | (p->options & PR_O_COOK_ANY) ? sess_cookie[(s->flags & SN_CK_MASK) >> SN_CK_SHIFT] : '-', |
| 2677 | (p->options & PR_O_COOK_ANY) ? sess_set_cookie[(s->flags & SN_SCK_MASK) >> SN_SCK_SHIFT] : '-', |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 2678 | s->srv ? s->srv->cur_sess : 0, p->nbconn, actconn, tmpline); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2679 | } |
| 2680 | else { |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 2681 | send_log(p, LOG_INFO, "%s:%d [%02d/%s/%04d:%02d:%02d:%02d] %s %s %d/%s%d %s%lld %c%c %d/%d/%d\n", |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2682 | pn, |
| 2683 | (s->cli_addr.ss_family == AF_INET) ? |
| 2684 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 2685 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2686 | tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900, |
| 2687 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2688 | pxid, srv, |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2689 | (s->logs.t_connect >= 0) ? s->logs.t_connect : -1, |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 2690 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 2691 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.bytes, |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2692 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
willy tarreau | 0fe3965 | 2005-12-18 01:25:24 +0100 | [diff] [blame] | 2693 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 2694 | s->srv ? s->srv->cur_sess : 0, p->nbconn, actconn); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | s->logs.logwait = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2698 | } |
| 2699 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2700 | |
| 2701 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2702 | * this function is called on a read event from a listen socket, corresponding |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2703 | * to an accept. It tries to accept as many connections as possible. |
| 2704 | * It returns 0. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2705 | */ |
| 2706 | int event_accept(int fd) { |
| 2707 | struct proxy *p = (struct proxy *)fdtab[fd].owner; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2708 | struct session *s; |
| 2709 | struct task *t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2710 | int cfd; |
willy tarreau | c2becdc | 2006-03-19 19:36:48 +0100 | [diff] [blame] | 2711 | int max_accept; |
| 2712 | |
| 2713 | if (global.nbproc > 1) |
| 2714 | max_accept = 8; /* let other processes catch some connections too */ |
| 2715 | else |
| 2716 | max_accept = -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2717 | |
willy tarreau | c2becdc | 2006-03-19 19:36:48 +0100 | [diff] [blame] | 2718 | while (p->nbconn < p->maxconn && max_accept--) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2719 | struct sockaddr_storage addr; |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2720 | socklen_t laddr = sizeof(addr); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2721 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2722 | if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) { |
| 2723 | switch (errno) { |
| 2724 | case EAGAIN: |
| 2725 | case EINTR: |
| 2726 | case ECONNABORTED: |
| 2727 | return 0; /* nothing more to accept */ |
| 2728 | case ENFILE: |
| 2729 | send_log(p, LOG_EMERG, |
| 2730 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 2731 | p->id, maxfd); |
| 2732 | return 0; |
| 2733 | case EMFILE: |
| 2734 | send_log(p, LOG_EMERG, |
| 2735 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 2736 | p->id, maxfd); |
| 2737 | return 0; |
| 2738 | case ENOBUFS: |
| 2739 | case ENOMEM: |
| 2740 | send_log(p, LOG_EMERG, |
| 2741 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 2742 | p->id, maxfd); |
| 2743 | return 0; |
| 2744 | default: |
| 2745 | return 0; |
| 2746 | } |
| 2747 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2748 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2749 | if ((s = pool_alloc(session)) == NULL) { /* disable this proxy for a while */ |
| 2750 | Alert("out of memory in event_accept().\n"); |
| 2751 | FD_CLR(fd, StaticReadEvent); |
| 2752 | p->state = PR_STIDLE; |
| 2753 | close(cfd); |
| 2754 | return 0; |
| 2755 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2756 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2757 | /* if this session comes from a known monitoring system, we want to ignore |
| 2758 | * it as soon as possible, which means closing it immediately for TCP. |
| 2759 | */ |
| 2760 | s->flags = 0; |
| 2761 | if (addr.ss_family == AF_INET && |
| 2762 | p->mon_mask.s_addr && |
| 2763 | (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr) { |
| 2764 | if (p->mode == PR_MODE_TCP) { |
| 2765 | close(cfd); |
| 2766 | pool_free(session, s); |
| 2767 | continue; |
| 2768 | } |
| 2769 | s->flags |= SN_MONITOR; |
| 2770 | } |
| 2771 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2772 | if ((t = pool_alloc(task)) == NULL) { /* disable this proxy for a while */ |
| 2773 | Alert("out of memory in event_accept().\n"); |
| 2774 | FD_CLR(fd, StaticReadEvent); |
| 2775 | p->state = PR_STIDLE; |
| 2776 | close(cfd); |
| 2777 | pool_free(session, s); |
| 2778 | return 0; |
| 2779 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2780 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2781 | s->cli_addr = addr; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2782 | if (cfd >= global.maxsock) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2783 | Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 2784 | close(cfd); |
| 2785 | pool_free(task, t); |
| 2786 | pool_free(session, s); |
| 2787 | return 0; |
| 2788 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2789 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2790 | if ((fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) || |
| 2791 | (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, |
| 2792 | (char *) &one, sizeof(one)) == -1)) { |
| 2793 | Alert("accept(): cannot set the socket in non blocking mode. Giving up\n"); |
| 2794 | close(cfd); |
| 2795 | pool_free(task, t); |
| 2796 | pool_free(session, s); |
| 2797 | return 0; |
| 2798 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2799 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2800 | if (p->options & PR_O_TCP_CLI_KA) |
| 2801 | setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 2802 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2803 | t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */ |
| 2804 | t->wq = LIST_HEAD(wait_queue); /* but already has a wait queue assigned */ |
| 2805 | t->state = TASK_IDLE; |
| 2806 | t->process = process_session; |
| 2807 | t->context = s; |
| 2808 | |
| 2809 | s->task = t; |
| 2810 | s->proxy = p; |
| 2811 | s->cli_state = (p->mode == PR_MODE_HTTP) ? CL_STHEADERS : CL_STDATA; /* no HTTP headers for non-HTTP proxies */ |
| 2812 | s->srv_state = SV_STIDLE; |
| 2813 | s->req = s->rep = NULL; /* will be allocated later */ |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 2814 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2815 | s->res_cr = s->res_cw = s->res_sr = s->res_sw = RES_SILENT; |
| 2816 | s->cli_fd = cfd; |
| 2817 | s->srv_fd = -1; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2818 | s->srv = NULL; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2819 | s->conn_retries = p->conn_retries; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2820 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2821 | if (s->flags & SN_MONITOR) |
| 2822 | s->logs.logwait = 0; |
| 2823 | else |
| 2824 | s->logs.logwait = p->to_log; |
| 2825 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2826 | s->logs.tv_accept = now; |
| 2827 | s->logs.t_request = -1; |
| 2828 | s->logs.t_connect = -1; |
| 2829 | s->logs.t_data = -1; |
| 2830 | s->logs.t_close = 0; |
| 2831 | s->logs.uri = NULL; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2832 | s->logs.cli_cookie = NULL; |
| 2833 | s->logs.srv_cookie = NULL; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2834 | s->logs.status = -1; |
| 2835 | s->logs.bytes = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2836 | |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2837 | s->uniq_id = totalconn; |
| 2838 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2839 | if (p->nb_req_cap > 0) { |
| 2840 | if ((s->req_cap = |
| 2841 | pool_alloc_from(p->req_cap_pool, p->nb_req_cap*sizeof(char *))) |
| 2842 | == NULL) { /* no memory */ |
| 2843 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2844 | pool_free(task, t); |
| 2845 | pool_free(session, s); |
| 2846 | return 0; |
| 2847 | } |
| 2848 | memset(s->req_cap, 0, p->nb_req_cap*sizeof(char *)); |
| 2849 | } |
| 2850 | else |
| 2851 | s->req_cap = NULL; |
| 2852 | |
| 2853 | if (p->nb_rsp_cap > 0) { |
| 2854 | if ((s->rsp_cap = |
| 2855 | pool_alloc_from(p->rsp_cap_pool, p->nb_rsp_cap*sizeof(char *))) |
| 2856 | == NULL) { /* no memory */ |
| 2857 | if (s->req_cap != NULL) |
| 2858 | pool_free_to(p->req_cap_pool, s->req_cap); |
| 2859 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2860 | pool_free(task, t); |
| 2861 | pool_free(session, s); |
| 2862 | return 0; |
| 2863 | } |
| 2864 | memset(s->rsp_cap, 0, p->nb_rsp_cap*sizeof(char *)); |
| 2865 | } |
| 2866 | else |
| 2867 | s->rsp_cap = NULL; |
| 2868 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2869 | if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP) |
| 2870 | && (p->logfac1 >= 0 || p->logfac2 >= 0)) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2871 | struct sockaddr_storage sockname; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2872 | socklen_t namelen = sizeof(sockname); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2873 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2874 | if (addr.ss_family != AF_INET || |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2875 | !(s->proxy->options & PR_O_TRANSP) || |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2876 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2877 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2878 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2879 | if (p->to_log) { |
| 2880 | /* we have the client ip */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2881 | if (s->logs.logwait & LW_CLIP) |
| 2882 | if (!(s->logs.logwait &= ~LW_CLIP)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2883 | sess_log(s); |
| 2884 | } |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2885 | else if (s->cli_addr.ss_family == AF_INET) { |
| 2886 | char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN]; |
| 2887 | if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sockname)->sin_addr, |
| 2888 | sn, sizeof(sn)) && |
| 2889 | inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 2890 | pn, sizeof(pn))) { |
| 2891 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 2892 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port), |
| 2893 | sn, ntohs(((struct sockaddr_in *)&sockname)->sin_port), |
| 2894 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 2895 | } |
| 2896 | } |
| 2897 | else { |
| 2898 | char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN]; |
| 2899 | if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sockname)->sin6_addr, |
| 2900 | sn, sizeof(sn)) && |
| 2901 | inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr, |
| 2902 | pn, sizeof(pn))) { |
| 2903 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 2904 | pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 2905 | sn, ntohs(((struct sockaddr_in6 *)&sockname)->sin6_port), |
| 2906 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 2907 | } |
| 2908 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2909 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2910 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 2911 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2912 | struct sockaddr_in sockname; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2913 | socklen_t namelen = sizeof(sockname); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2914 | int len; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2915 | if (addr.ss_family != AF_INET || |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2916 | !(s->proxy->options & PR_O_TRANSP) || |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2917 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2918 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2919 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2920 | if (s->cli_addr.ss_family == AF_INET) { |
| 2921 | char pn[INET_ADDRSTRLEN]; |
| 2922 | inet_ntop(AF_INET, |
| 2923 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 2924 | pn, sizeof(pn)); |
| 2925 | |
| 2926 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 2927 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 2928 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port)); |
| 2929 | } |
| 2930 | else { |
| 2931 | char pn[INET6_ADDRSTRLEN]; |
| 2932 | inet_ntop(AF_INET6, |
| 2933 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 2934 | pn, sizeof(pn)); |
| 2935 | |
| 2936 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 2937 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 2938 | pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port)); |
| 2939 | } |
| 2940 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2941 | write(1, trash, len); |
| 2942 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2943 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2944 | if ((s->req = pool_alloc(buffer)) == NULL) { /* no memory */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2945 | if (s->rsp_cap != NULL) |
| 2946 | pool_free_to(p->rsp_cap_pool, s->rsp_cap); |
| 2947 | if (s->req_cap != NULL) |
| 2948 | pool_free_to(p->req_cap_pool, s->req_cap); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2949 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2950 | pool_free(task, t); |
| 2951 | pool_free(session, s); |
| 2952 | return 0; |
| 2953 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2954 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2955 | s->req->l = 0; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2956 | s->req->total = 0; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2957 | s->req->h = s->req->r = s->req->lr = s->req->w = s->req->data; /* r and w will be reset further */ |
| 2958 | s->req->rlim = s->req->data + BUFSIZE; |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2959 | if (s->cli_state == CL_STHEADERS) /* reserve some space for header rewriting */ |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2960 | s->req->rlim -= MAXREWRITE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2961 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2962 | if ((s->rep = pool_alloc(buffer)) == NULL) { /* no memory */ |
| 2963 | pool_free(buffer, s->req); |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2964 | if (s->rsp_cap != NULL) |
| 2965 | pool_free_to(p->rsp_cap_pool, s->rsp_cap); |
| 2966 | if (s->req_cap != NULL) |
| 2967 | pool_free_to(p->req_cap_pool, s->req_cap); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2968 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2969 | pool_free(task, t); |
| 2970 | pool_free(session, s); |
| 2971 | return 0; |
| 2972 | } |
| 2973 | s->rep->l = 0; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2974 | s->rep->total = 0; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2975 | s->rep->h = s->rep->r = s->rep->lr = s->rep->w = s->rep->rlim = s->rep->data; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2976 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2977 | fdtab[cfd].read = &event_cli_read; |
| 2978 | fdtab[cfd].write = &event_cli_write; |
| 2979 | fdtab[cfd].owner = t; |
| 2980 | fdtab[cfd].state = FD_STREADY; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2981 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2982 | if ((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) || |
| 2983 | (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK))) |
| 2984 | /* Either we got a request from a monitoring system on an HTTP instance, |
| 2985 | * or we're in health check mode with the 'httpchk' option enabled. In |
| 2986 | * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit. |
| 2987 | */ |
| 2988 | client_retnclose(s, 19, "HTTP/1.0 200 OK\r\n\r\n"); /* forge a 200 response */ |
| 2989 | else if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */ |
| 2990 | client_retnclose(s, 3, "OK\n"); /* forge an "OK" response */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2991 | } |
| 2992 | else { |
| 2993 | FD_SET(cfd, StaticReadEvent); |
| 2994 | } |
| 2995 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2996 | #if defined(DEBUG_FULL) && defined(ENABLE_EPOLL) |
| 2997 | if (PrevReadEvent) { |
| 2998 | assert(!(FD_ISSET(cfd, PrevReadEvent))); |
| 2999 | assert(!(FD_ISSET(cfd, PrevWriteEvent))); |
| 3000 | } |
| 3001 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3002 | fd_insert(cfd); |
| 3003 | |
| 3004 | tv_eternity(&s->cnexpire); |
| 3005 | tv_eternity(&s->srexpire); |
| 3006 | tv_eternity(&s->swexpire); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3007 | tv_eternity(&s->crexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3008 | tv_eternity(&s->cwexpire); |
| 3009 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3010 | if (s->proxy->clitimeout) { |
| 3011 | if (FD_ISSET(cfd, StaticReadEvent)) |
| 3012 | tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout); |
| 3013 | if (FD_ISSET(cfd, StaticWriteEvent)) |
| 3014 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
| 3015 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3016 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3017 | tv_min(&t->expire, &s->crexpire, &s->cwexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3018 | |
| 3019 | task_queue(t); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3020 | |
| 3021 | if (p->mode != PR_MODE_HEALTH) |
| 3022 | task_wakeup(&rq, t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3023 | |
| 3024 | p->nbconn++; |
| 3025 | actconn++; |
| 3026 | totalconn++; |
| 3027 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3028 | // fprintf(stderr, "accepting from %p => %d conn, %d total, task=%p\n", p, actconn, totalconn, t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3029 | } /* end of while (p->nbconn < p->maxconn) */ |
| 3030 | return 0; |
| 3031 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3032 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3033 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3034 | /* |
| 3035 | * This function is used only for server health-checks. It handles |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3036 | * the connection acknowledgement. If the proxy requires HTTP health-checks, |
| 3037 | * it sends the request. In other cases, it returns 1 if the socket is OK, |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3038 | * or -1 if an error occured. |
| 3039 | */ |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3040 | int event_srv_chk_w(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3041 | struct task *t = fdtab[fd].owner; |
| 3042 | struct server *s = t->context; |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 3043 | int skerr; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3044 | socklen_t lskerr = sizeof(skerr); |
| 3045 | |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 3046 | skerr = 1; |
| 3047 | if ((getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) |
| 3048 | || (skerr != 0)) { |
| 3049 | /* in case of TCP only, this tells us if the connection failed */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3050 | s->result = -1; |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 3051 | fdtab[fd].state = FD_STERROR; |
| 3052 | FD_CLR(fd, StaticWriteEvent); |
| 3053 | } |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 3054 | else if (s->result != -1) { |
| 3055 | /* we don't want to mark 'UP' a server on which we detected an error earlier */ |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3056 | if (s->proxy->options & PR_O_HTTP_CHK) { |
| 3057 | int ret; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 3058 | /* we want to check if this host replies to "OPTIONS / HTTP/1.0" |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3059 | * so we'll send the request, and won't wake the checker up now. |
| 3060 | */ |
| 3061 | #ifndef MSG_NOSIGNAL |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 3062 | ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT); |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3063 | #else |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 3064 | ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL); |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3065 | #endif |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 3066 | if (ret == s->proxy->check_len) { |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3067 | FD_SET(fd, StaticReadEvent); /* prepare for reading reply */ |
| 3068 | FD_CLR(fd, StaticWriteEvent); /* nothing more to write */ |
| 3069 | return 0; |
| 3070 | } |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 3071 | else { |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3072 | s->result = -1; |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 3073 | FD_CLR(fd, StaticWriteEvent); |
| 3074 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3075 | } |
| 3076 | else { |
| 3077 | /* good TCP connection is enough */ |
| 3078 | s->result = 1; |
| 3079 | } |
| 3080 | } |
| 3081 | |
| 3082 | task_wakeup(&rq, t); |
| 3083 | return 0; |
| 3084 | } |
| 3085 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3086 | |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3087 | /* |
| 3088 | * This function is used only for server health-checks. It handles |
| 3089 | * the server's reply to an HTTP request. It returns 1 if the server replies |
| 3090 | * 2xx or 3xx (valid responses), or -1 in other cases. |
| 3091 | */ |
| 3092 | int event_srv_chk_r(int fd) { |
| 3093 | char reply[64]; |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 3094 | int len, result; |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3095 | struct task *t = fdtab[fd].owner; |
| 3096 | struct server *s = t->context; |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 3097 | int skerr; |
| 3098 | socklen_t lskerr = sizeof(skerr); |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3099 | |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 3100 | result = len = -1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3101 | |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 3102 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 3103 | if (!skerr) { |
| 3104 | #ifndef MSG_NOSIGNAL |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3105 | len = recv(fd, reply, sizeof(reply), 0); |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3106 | #else |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 3107 | /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available |
| 3108 | * but the connection was closed on the remote end. Fortunately, recv still |
| 3109 | * works correctly and we don't need to do the getsockopt() on linux. |
| 3110 | */ |
| 3111 | len = recv(fd, reply, sizeof(reply), MSG_NOSIGNAL); |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3112 | #endif |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 3113 | |
| 3114 | if ((len >= sizeof("HTTP/1.0 000")) && |
| 3115 | !memcmp(reply, "HTTP/1.", 7) && |
| 3116 | (reply[9] == '2' || reply[9] == '3')) /* 2xx or 3xx */ |
| 3117 | result = 1; |
| 3118 | } |
| 3119 | |
| 3120 | if (result == -1) |
| 3121 | fdtab[fd].state = FD_STERROR; |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 3122 | |
| 3123 | if (s->result != -1) |
| 3124 | s->result = result; |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3125 | |
| 3126 | FD_CLR(fd, StaticReadEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3127 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3128 | return 0; |
| 3129 | } |
| 3130 | |
| 3131 | |
| 3132 | /* |
| 3133 | * this function writes the string <str> at position <pos> which must be in buffer <b>, |
| 3134 | * and moves <end> just after the end of <str>. |
| 3135 | * <b>'s parameters (l, r, w, h, lr) are recomputed to be valid after the shift. |
| 3136 | * the shift value (positive or negative) is returned. |
| 3137 | * If there's no space left, the move is not done. |
| 3138 | * |
| 3139 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3140 | int buffer_replace(struct buffer *b, char *pos, char *end, char *str) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3141 | int delta; |
| 3142 | int len; |
| 3143 | |
| 3144 | len = strlen(str); |
| 3145 | delta = len - (end - pos); |
| 3146 | |
| 3147 | if (delta + b->r >= b->data + BUFSIZE) |
| 3148 | return 0; /* no space left */ |
| 3149 | |
| 3150 | /* first, protect the end of the buffer */ |
| 3151 | memmove(end + delta, end, b->data + b->l - end); |
| 3152 | |
| 3153 | /* now, copy str over pos */ |
| 3154 | memcpy(pos, str,len); |
| 3155 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3156 | /* we only move data after the displaced zone */ |
| 3157 | if (b->r > pos) b->r += delta; |
| 3158 | if (b->w > pos) b->w += delta; |
| 3159 | if (b->h > pos) b->h += delta; |
| 3160 | if (b->lr > pos) b->lr += delta; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3161 | b->l += delta; |
| 3162 | |
| 3163 | return delta; |
| 3164 | } |
| 3165 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3166 | /* same except that the string length is given, which allows str to be NULL if |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3167 | * len is 0. |
| 3168 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3169 | int buffer_replace2(struct buffer *b, char *pos, char *end, char *str, int len) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3170 | int delta; |
| 3171 | |
| 3172 | delta = len - (end - pos); |
| 3173 | |
| 3174 | if (delta + b->r >= b->data + BUFSIZE) |
| 3175 | return 0; /* no space left */ |
| 3176 | |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 3177 | if (b->data + b->l < end) |
| 3178 | /* The data has been stolen, we could have crashed. Maybe we should abort() ? */ |
| 3179 | return 0; |
| 3180 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3181 | /* first, protect the end of the buffer */ |
| 3182 | memmove(end + delta, end, b->data + b->l - end); |
| 3183 | |
| 3184 | /* now, copy str over pos */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3185 | if (len) |
| 3186 | memcpy(pos, str, len); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3187 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3188 | /* we only move data after the displaced zone */ |
| 3189 | if (b->r > pos) b->r += delta; |
| 3190 | if (b->w > pos) b->w += delta; |
| 3191 | if (b->h > pos) b->h += delta; |
| 3192 | if (b->lr > pos) b->lr += delta; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3193 | b->l += delta; |
| 3194 | |
| 3195 | return delta; |
| 3196 | } |
| 3197 | |
| 3198 | |
| 3199 | int exp_replace(char *dst, char *src, char *str, regmatch_t *matches) { |
| 3200 | char *old_dst = dst; |
| 3201 | |
| 3202 | while (*str) { |
| 3203 | if (*str == '\\') { |
| 3204 | str++; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 3205 | if (isdigit((int)*str)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3206 | int len, num; |
| 3207 | |
| 3208 | num = *str - '0'; |
| 3209 | str++; |
| 3210 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3211 | if (matches[num].rm_eo > -1 && matches[num].rm_so > -1) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3212 | len = matches[num].rm_eo - matches[num].rm_so; |
| 3213 | memcpy(dst, src + matches[num].rm_so, len); |
| 3214 | dst += len; |
| 3215 | } |
| 3216 | |
| 3217 | } |
| 3218 | else if (*str == 'x') { |
| 3219 | unsigned char hex1, hex2; |
| 3220 | str++; |
| 3221 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 3222 | hex1 = toupper(*str++) - '0'; |
| 3223 | hex2 = toupper(*str++) - '0'; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3224 | |
| 3225 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 3226 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 3227 | *dst++ = (hex1<<4) + hex2; |
| 3228 | } |
| 3229 | else |
| 3230 | *dst++ = *str++; |
| 3231 | } |
| 3232 | else |
| 3233 | *dst++ = *str++; |
| 3234 | } |
| 3235 | *dst = 0; |
| 3236 | return dst - old_dst; |
| 3237 | } |
| 3238 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 3239 | static int ishex(char s) |
| 3240 | { |
| 3241 | return (s >= '0' && s <= '9') || (s >= 'A' && s <= 'F') || (s >= 'a' && s <= 'f'); |
| 3242 | } |
| 3243 | |
| 3244 | /* returns NULL if the replacement string <str> is valid, or the pointer to the first error */ |
| 3245 | char *check_replace_string(char *str) |
| 3246 | { |
| 3247 | char *err = NULL; |
| 3248 | while (*str) { |
| 3249 | if (*str == '\\') { |
| 3250 | err = str; /* in case of a backslash, we return the pointer to it */ |
| 3251 | str++; |
| 3252 | if (!*str) |
| 3253 | return err; |
| 3254 | else if (isdigit((int)*str)) |
| 3255 | err = NULL; |
| 3256 | else if (*str == 'x') { |
| 3257 | str++; |
| 3258 | if (!ishex(*str)) |
| 3259 | return err; |
| 3260 | str++; |
| 3261 | if (!ishex(*str)) |
| 3262 | return err; |
| 3263 | err = NULL; |
| 3264 | } |
| 3265 | else { |
| 3266 | Warning("'\\%c' : deprecated use of a backslash before something not '\\','x' or a digit.\n", *str); |
| 3267 | err = NULL; |
| 3268 | } |
| 3269 | } |
| 3270 | str++; |
| 3271 | } |
| 3272 | return err; |
| 3273 | } |
| 3274 | |
| 3275 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3276 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3277 | /* |
| 3278 | * manages the client FSM and its socket. BTW, it also tries to handle the |
| 3279 | * cookie. It returns 1 if a state has changed (and a resync may be needed), |
| 3280 | * 0 else. |
| 3281 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3282 | int process_cli(struct session *t) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3283 | int s = t->srv_state; |
| 3284 | int c = t->cli_state; |
| 3285 | struct buffer *req = t->req; |
| 3286 | struct buffer *rep = t->rep; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3287 | int method_checked = 0; |
| 3288 | appsess *asession_temp = NULL; |
| 3289 | appsess local_asession; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3290 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3291 | #ifdef DEBUG_FULL |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3292 | fprintf(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n", |
| 3293 | cli_stnames[c], srv_stnames[s], |
| 3294 | FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 3295 | t->crexpire.tv_sec, t->crexpire.tv_usec, |
| 3296 | t->cwexpire.tv_sec, t->cwexpire.tv_usec); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3297 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3298 | //fprintf(stderr,"process_cli: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s, |
| 3299 | //FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 3300 | //FD_ISSET(t->srv_fd, StaticReadEvent), FD_ISSET(t->srv_fd, StaticWriteEvent) |
| 3301 | //); |
| 3302 | if (c == CL_STHEADERS) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3303 | /* now parse the partial (or complete) headers */ |
| 3304 | while (req->lr < req->r) { /* this loop only sees one header at each iteration */ |
| 3305 | char *ptr; |
| 3306 | int delete_header; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3307 | char *request_line = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3308 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3309 | ptr = req->lr; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3310 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3311 | /* look for the end of the current header */ |
| 3312 | while (ptr < req->r && *ptr != '\n' && *ptr != '\r') |
| 3313 | ptr++; |
| 3314 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3315 | if (ptr == req->h) { /* empty line, end of headers */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3316 | int line, len; |
| 3317 | /* we can only get here after an end of headers */ |
| 3318 | /* we'll have something else to do here : add new headers ... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3319 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3320 | if (t->flags & SN_CLDENY) { |
| 3321 | /* no need to go further */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3322 | t->logs.status = 403; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3323 | client_retnclose(t, t->proxy->errmsg.len403, t->proxy->errmsg.msg403); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3324 | if (!(t->flags & SN_ERR_MASK)) |
| 3325 | t->flags |= SN_ERR_PRXCOND; |
| 3326 | if (!(t->flags & SN_FINST_MASK)) |
| 3327 | t->flags |= SN_FINST_R; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3328 | return 1; |
| 3329 | } |
| 3330 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3331 | for (line = 0; line < t->proxy->nb_reqadd; line++) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3332 | len = sprintf(trash, "%s\r\n", t->proxy->req_add[line]); |
| 3333 | buffer_replace2(req, req->h, req->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3334 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3335 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3336 | if (t->proxy->options & PR_O_FWDFOR) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3337 | if (t->cli_addr.ss_family == AF_INET) { |
| 3338 | unsigned char *pn; |
| 3339 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
| 3340 | len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d\r\n", |
| 3341 | pn[0], pn[1], pn[2], pn[3]); |
| 3342 | buffer_replace2(req, req->h, req->h, trash, len); |
| 3343 | } |
| 3344 | else if (t->cli_addr.ss_family == AF_INET6) { |
| 3345 | char pn[INET6_ADDRSTRLEN]; |
| 3346 | inet_ntop(AF_INET6, |
| 3347 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 3348 | pn, sizeof(pn)); |
| 3349 | len = sprintf(trash, "X-Forwarded-For: %s\r\n", pn); |
| 3350 | buffer_replace2(req, req->h, req->h, trash, len); |
| 3351 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3352 | } |
| 3353 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 3354 | /* add a "connection: close" line if needed */ |
| 3355 | if (t->proxy->options & PR_O_HTTP_CLOSE) |
| 3356 | buffer_replace2(req, req->h, req->h, "Connection: close\r\n", 19); |
| 3357 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 3358 | if (!memcmp(req->data, "POST ", 5)) { |
| 3359 | /* this is a POST request, which is not cacheable by default */ |
| 3360 | t->flags |= SN_POST; |
| 3361 | } |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 3362 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3363 | t->cli_state = CL_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3364 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3365 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3366 | t->logs.t_request = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3367 | /* FIXME: we'll set the client in a wait state while we try to |
| 3368 | * connect to the server. Is this really needed ? wouldn't it be |
| 3369 | * better to release the maximum of system buffers instead ? */ |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3370 | //FD_CLR(t->cli_fd, StaticReadEvent); |
| 3371 | //tv_eternity(&t->crexpire); |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 3372 | |
| 3373 | /* FIXME: if we break here (as up to 1.1.23), having the client |
| 3374 | * shutdown its connection can lead to an abort further. |
| 3375 | * it's better to either return 1 or even jump directly to the |
| 3376 | * data state which will save one schedule. |
| 3377 | */ |
| 3378 | //break; |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 3379 | |
| 3380 | if (!t->proxy->clitimeout || |
| 3381 | (t->srv_state < SV_STDATA && t->proxy->srvtimeout)) |
| 3382 | /* If the client has no timeout, or if the server is not ready yet, |
| 3383 | * and we know for sure that it can expire, then it's cleaner to |
| 3384 | * disable the timeout on the client side so that too low values |
| 3385 | * cannot make the sessions abort too early. |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3386 | * |
| 3387 | * FIXME-20050705: the server needs a way to re-enable this time-out |
| 3388 | * when it switches its state, otherwise a client can stay connected |
| 3389 | * indefinitely. This now seems to be OK. |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 3390 | */ |
| 3391 | tv_eternity(&t->crexpire); |
| 3392 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 3393 | goto process_data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3394 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3395 | |
Willy TARREAU | 13032e7 | 2006-03-12 17:31:45 +0100 | [diff] [blame] | 3396 | /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */ |
| 3397 | if (ptr > req->r - 2) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3398 | /* this is a partial header, let's wait for more to come */ |
| 3399 | req->lr = ptr; |
| 3400 | break; |
| 3401 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3402 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3403 | /* now we know that *ptr is either \r or \n, |
| 3404 | * and that there are at least 1 char after it. |
| 3405 | */ |
| 3406 | if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n')) |
| 3407 | req->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */ |
| 3408 | else |
| 3409 | req->lr = ptr + 2; /* \r\n or \n\r */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3410 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3411 | /* |
| 3412 | * now we know that we have a full header ; we can do whatever |
| 3413 | * we want with these pointers : |
| 3414 | * req->h = beginning of header |
| 3415 | * ptr = end of header (first \r or \n) |
| 3416 | * req->lr = beginning of next line (next rep->h) |
| 3417 | * req->r = end of data (not used at this stage) |
| 3418 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3419 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3420 | if (!method_checked && (t->proxy->appsession_name != NULL) && |
| 3421 | ((memcmp(req->h, "GET ", 4) == 0) || (memcmp(req->h, "POST ", 4) == 0)) && |
| 3422 | ((request_line = memchr(req->h, ';', req->lr - req->h)) != NULL)) { |
| 3423 | |
| 3424 | /* skip ; */ |
| 3425 | request_line++; |
| 3426 | |
| 3427 | /* look if we have a jsessionid */ |
| 3428 | |
| 3429 | if (strncasecmp(request_line, t->proxy->appsession_name, t->proxy->appsession_name_len) == 0) { |
| 3430 | |
| 3431 | /* skip jsessionid= */ |
| 3432 | request_line += t->proxy->appsession_name_len + 1; |
| 3433 | |
| 3434 | /* First try if we allready have an appsession */ |
| 3435 | asession_temp = &local_asession; |
| 3436 | |
| 3437 | if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) { |
| 3438 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 3439 | send_log(t->proxy, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 3440 | return 0; |
| 3441 | } |
| 3442 | |
| 3443 | /* Copy the sessionid */ |
| 3444 | memcpy(asession_temp->sessid, request_line, t->proxy->appsession_len); |
| 3445 | asession_temp->sessid[t->proxy->appsession_len] = 0; |
| 3446 | asession_temp->serverid = NULL; |
| 3447 | |
| 3448 | /* only do insert, if lookup fails */ |
| 3449 | if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *)&asession_temp)) { |
| 3450 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 3451 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 3452 | send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 3453 | return 0; |
| 3454 | } |
| 3455 | asession_temp->sessid = local_asession.sessid; |
| 3456 | asession_temp->serverid = local_asession.serverid; |
| 3457 | chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3458 | } /* end if (chtbl_lookup()) */ |
| 3459 | else { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3460 | /*free wasted memory;*/ |
| 3461 | pool_free_to(apools.sessid, local_asession.sessid); |
| 3462 | } |
| 3463 | |
| 3464 | tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout); |
| 3465 | asession_temp->request_count++; |
| 3466 | |
| 3467 | #if defined(DEBUG_HASH) |
| 3468 | print_table(&(t->proxy->htbl_proxy)); |
| 3469 | #endif |
| 3470 | |
| 3471 | if (asession_temp->serverid == NULL) { |
| 3472 | Alert("Found Application Session without matching server.\n"); |
| 3473 | } else { |
| 3474 | struct server *srv = t->proxy->srv; |
| 3475 | while (srv) { |
| 3476 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
| 3477 | if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) { |
| 3478 | /* we found the server and it's usable */ |
| 3479 | t->flags &= ~SN_CK_MASK; |
| 3480 | t->flags |= SN_CK_VALID | SN_DIRECT; |
| 3481 | t->srv = srv; |
| 3482 | break; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3483 | } else { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3484 | t->flags &= ~SN_CK_MASK; |
| 3485 | t->flags |= SN_CK_DOWN; |
| 3486 | } |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3487 | } /* end if (strcmp()) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3488 | srv = srv->next; |
| 3489 | }/* end while(srv) */ |
| 3490 | }/* end else of if (asession_temp->serverid == NULL) */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3491 | }/* end if (strncasecmp(request_line,t->proxy->appsession_name,apssesion_name_len) == 0) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3492 | else { |
| 3493 | //fprintf(stderr,">>>>>>>>>>>>>>>>>>>>>>NO SESSION\n"); |
| 3494 | } |
willy tarreau | 598da41 | 2005-12-18 01:07:29 +0100 | [diff] [blame] | 3495 | method_checked = 1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3496 | } /* end if (!method_checked ...) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3497 | else{ |
| 3498 | //printf("No Methode-Header with Session-String\n"); |
| 3499 | } |
| 3500 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3501 | if (t->logs.logwait & LW_REQ) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3502 | /* we have a complete HTTP request that we must log */ |
| 3503 | int urilen; |
| 3504 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3505 | if ((t->logs.uri = pool_alloc(requri)) == NULL) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3506 | Alert("HTTP logging : out of memory.\n"); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3507 | t->logs.status = 500; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3508 | client_retnclose(t, t->proxy->errmsg.len500, t->proxy->errmsg.msg500); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3509 | if (!(t->flags & SN_ERR_MASK)) |
| 3510 | t->flags |= SN_ERR_PRXCOND; |
| 3511 | if (!(t->flags & SN_FINST_MASK)) |
| 3512 | t->flags |= SN_FINST_R; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3513 | return 1; |
| 3514 | } |
| 3515 | |
| 3516 | urilen = ptr - req->h; |
| 3517 | if (urilen >= REQURI_LEN) |
| 3518 | urilen = REQURI_LEN - 1; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3519 | memcpy(t->logs.uri, req->h, urilen); |
| 3520 | t->logs.uri[urilen] = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3521 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3522 | if (!(t->logs.logwait &= ~LW_REQ)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3523 | sess_log(t); |
| 3524 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 3525 | else if (t->logs.logwait & LW_REQHDR) { |
| 3526 | struct cap_hdr *h; |
| 3527 | int len; |
| 3528 | for (h = t->proxy->req_cap; h; h = h->next) { |
| 3529 | if ((h->namelen + 2 <= ptr - req->h) && |
| 3530 | (req->h[h->namelen] == ':') && |
| 3531 | (strncasecmp(req->h, h->name, h->namelen) == 0)) { |
| 3532 | |
| 3533 | if (t->req_cap[h->index] == NULL) |
| 3534 | t->req_cap[h->index] = pool_alloc_from(h->pool, h->len + 1); |
| 3535 | |
| 3536 | len = ptr - (req->h + h->namelen + 2); |
| 3537 | if (len > h->len) |
| 3538 | len = h->len; |
| 3539 | |
| 3540 | memcpy(t->req_cap[h->index], req->h + h->namelen + 2, len); |
| 3541 | t->req_cap[h->index][len]=0; |
| 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3546 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3547 | delete_header = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3548 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 3549 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3550 | int len, max; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 3551 | len = sprintf(trash, "%08x:%s.clihdr[%04x:%04x]: ", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3552 | max = ptr - req->h; |
| 3553 | UBOUND(max, sizeof(trash) - len - 1); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3554 | len += strlcpy2(trash + len, req->h, max + 1); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3555 | trash[len++] = '\n'; |
| 3556 | write(1, trash, len); |
| 3557 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3558 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 3559 | |
| 3560 | /* remove "connection: " if needed */ |
| 3561 | if (!delete_header && (t->proxy->options & PR_O_HTTP_CLOSE) |
| 3562 | && (strncasecmp(req->h, "Connection: ", 12) == 0)) { |
| 3563 | delete_header = 1; |
| 3564 | } |
| 3565 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3566 | /* try headers regexps */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 3567 | if (!delete_header && t->proxy->req_exp != NULL |
| 3568 | && !(t->flags & SN_CLDENY)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3569 | struct hdr_exp *exp; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3570 | char term; |
| 3571 | |
| 3572 | term = *ptr; |
| 3573 | *ptr = '\0'; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3574 | exp = t->proxy->req_exp; |
| 3575 | do { |
| 3576 | if (regexec(exp->preg, req->h, MAX_MATCH, pmatch, 0) == 0) { |
| 3577 | switch (exp->action) { |
| 3578 | case ACT_ALLOW: |
| 3579 | if (!(t->flags & SN_CLDENY)) |
| 3580 | t->flags |= SN_CLALLOW; |
| 3581 | break; |
| 3582 | case ACT_REPLACE: |
| 3583 | if (!(t->flags & SN_CLDENY)) { |
| 3584 | int len = exp_replace(trash, req->h, exp->replace, pmatch); |
| 3585 | ptr += buffer_replace2(req, req->h, ptr, trash, len); |
| 3586 | } |
| 3587 | break; |
| 3588 | case ACT_REMOVE: |
| 3589 | if (!(t->flags & SN_CLDENY)) |
| 3590 | delete_header = 1; |
| 3591 | break; |
| 3592 | case ACT_DENY: |
| 3593 | if (!(t->flags & SN_CLALLOW)) |
| 3594 | t->flags |= SN_CLDENY; |
| 3595 | break; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3596 | case ACT_PASS: /* we simply don't deny this one */ |
| 3597 | break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3598 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3599 | break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3600 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3601 | } while ((exp = exp->next) != NULL); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3602 | *ptr = term; /* restore the string terminator */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3603 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3604 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3605 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3606 | * attributes whose name begin with a '$', and associate them with |
| 3607 | * the right cookie, if we want to delete this cookie. |
| 3608 | * So there are 3 cases for each cookie read : |
| 3609 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3610 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3611 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3612 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3613 | * "special" cookie. |
| 3614 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3615 | * remove it. If no application cookie persists in the header, we |
| 3616 | * *MUST* delete it |
| 3617 | */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3618 | if (!delete_header && |
| 3619 | (t->proxy->cookie_name != NULL || t->proxy->capture_name != NULL || t->proxy->appsession_name !=NULL) |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3620 | && !(t->flags & SN_CLDENY) && (ptr >= req->h + 8) |
willy tarreau | 906b268 | 2005-12-17 13:49:52 +0100 | [diff] [blame] | 3621 | && (strncasecmp(req->h, "Cookie: ", 8) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3622 | char *p1, *p2, *p3, *p4; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3623 | char *del_colon, *del_cookie, *colon; |
| 3624 | int app_cookies; |
| 3625 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3626 | p1 = req->h + 8; /* first char after 'Cookie: ' */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3627 | colon = p1; |
| 3628 | /* del_cookie == NULL => nothing to be deleted */ |
| 3629 | del_colon = del_cookie = NULL; |
| 3630 | app_cookies = 0; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3631 | |
| 3632 | while (p1 < ptr) { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3633 | /* skip spaces and colons, but keep an eye on these ones */ |
| 3634 | while (p1 < ptr) { |
| 3635 | if (*p1 == ';' || *p1 == ',') |
| 3636 | colon = p1; |
| 3637 | else if (!isspace((int)*p1)) |
| 3638 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3639 | p1++; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3640 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3641 | |
| 3642 | if (p1 == ptr) |
| 3643 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3644 | |
| 3645 | /* p1 is at the beginning of the cookie name */ |
| 3646 | p2 = p1; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3647 | while (p2 < ptr && *p2 != '=') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3648 | p2++; |
| 3649 | |
| 3650 | if (p2 == ptr) |
| 3651 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3652 | |
| 3653 | p3 = p2 + 1; /* skips the '=' sign */ |
| 3654 | if (p3 == ptr) |
| 3655 | break; |
| 3656 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3657 | p4 = p3; |
| 3658 | while (p4 < ptr && !isspace((int)*p4) && *p4 != ';' && *p4 != ',') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3659 | p4++; |
| 3660 | |
| 3661 | /* here, we have the cookie name between p1 and p2, |
| 3662 | * and its value between p3 and p4. |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3663 | * we can process it : |
| 3664 | * |
| 3665 | * Cookie: NAME=VALUE; |
| 3666 | * | || || | |
| 3667 | * | || || +--> p4 |
| 3668 | * | || |+-------> p3 |
| 3669 | * | || +--------> p2 |
| 3670 | * | |+------------> p1 |
| 3671 | * | +-------------> colon |
| 3672 | * +--------------------> req->h |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3673 | */ |
| 3674 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3675 | if (*p1 == '$') { |
| 3676 | /* skip this one */ |
| 3677 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3678 | else { |
| 3679 | /* first, let's see if we want to capture it */ |
| 3680 | if (t->proxy->capture_name != NULL && |
| 3681 | t->logs.cli_cookie == NULL && |
| 3682 | (p4 - p1 >= t->proxy->capture_namelen) && |
| 3683 | memcmp(p1, t->proxy->capture_name, t->proxy->capture_namelen) == 0) { |
| 3684 | int log_len = p4 - p1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3685 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3686 | if ((t->logs.cli_cookie = pool_alloc(capture)) == NULL) { |
| 3687 | Alert("HTTP logging : out of memory.\n"); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3688 | } else { |
| 3689 | if (log_len > t->proxy->capture_len) |
| 3690 | log_len = t->proxy->capture_len; |
| 3691 | memcpy(t->logs.cli_cookie, p1, log_len); |
| 3692 | t->logs.cli_cookie[log_len] = 0; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3693 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3694 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3695 | |
| 3696 | if ((p2 - p1 == t->proxy->cookie_len) && (t->proxy->cookie_name != NULL) && |
| 3697 | (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) { |
| 3698 | /* Cool... it's the right one */ |
| 3699 | struct server *srv = t->proxy->srv; |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3700 | char *delim; |
| 3701 | |
| 3702 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 3703 | * have the server ID betweek p3 and delim, and the original cookie between |
| 3704 | * delim+1 and p4. Otherwise, delim==p4 : |
| 3705 | * |
| 3706 | * Cookie: NAME=SRV~VALUE; |
| 3707 | * | || || | | |
| 3708 | * | || || | +--> p4 |
| 3709 | * | || || +--------> delim |
| 3710 | * | || |+-----------> p3 |
| 3711 | * | || +------------> p2 |
| 3712 | * | |+----------------> p1 |
| 3713 | * | +-----------------> colon |
| 3714 | * +------------------------> req->h |
| 3715 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3716 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3717 | if (t->proxy->options & PR_O_COOK_PFX) { |
| 3718 | for (delim = p3; delim < p4; delim++) |
| 3719 | if (*delim == COOKIE_DELIM) |
| 3720 | break; |
| 3721 | } |
| 3722 | else |
| 3723 | delim = p4; |
| 3724 | |
| 3725 | |
| 3726 | /* Here, we'll look for the first running server which supports the cookie. |
| 3727 | * This allows to share a same cookie between several servers, for example |
| 3728 | * to dedicate backup servers to specific servers only. |
| 3729 | */ |
| 3730 | while (srv) { |
| 3731 | if ((srv->cklen == delim - p3) && !memcmp(p3, srv->cookie, delim - p3)) { |
| 3732 | if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) { |
| 3733 | /* we found the server and it's usable */ |
| 3734 | t->flags &= ~SN_CK_MASK; |
| 3735 | t->flags |= SN_CK_VALID | SN_DIRECT; |
| 3736 | t->srv = srv; |
| 3737 | break; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3738 | } else { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3739 | /* we found a server, but it's down */ |
| 3740 | t->flags &= ~SN_CK_MASK; |
| 3741 | t->flags |= SN_CK_DOWN; |
| 3742 | } |
| 3743 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3744 | srv = srv->next; |
| 3745 | } |
| 3746 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3747 | if (!srv && !(t->flags & SN_CK_DOWN)) { |
| 3748 | /* no server matched this cookie */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3749 | t->flags &= ~SN_CK_MASK; |
| 3750 | t->flags |= SN_CK_INVALID; |
| 3751 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3752 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3753 | /* depending on the cookie mode, we may have to either : |
| 3754 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3755 | * the server never sees it ; |
| 3756 | * - remove the server id from the cookie value, and tag the cookie as an |
| 3757 | * application cookie so that it does not get accidentely removed later, |
| 3758 | * if we're in cookie prefix mode |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3759 | */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3760 | if ((t->proxy->options & PR_O_COOK_PFX) && (delim != p4)) { |
| 3761 | buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 3762 | p4 -= (delim + 1 - p3); |
| 3763 | ptr -= (delim + 1 - p3); |
| 3764 | del_cookie = del_colon = NULL; |
| 3765 | app_cookies++; /* protect the header from deletion */ |
| 3766 | } |
| 3767 | else if (del_cookie == NULL && |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3768 | (t->proxy->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3769 | del_cookie = p1; |
| 3770 | del_colon = colon; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3771 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3772 | } else { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3773 | /* now we know that we must keep this cookie since it's |
| 3774 | * not ours. But if we wanted to delete our cookie |
| 3775 | * earlier, we cannot remove the complete header, but we |
| 3776 | * can remove the previous block itself. |
| 3777 | */ |
| 3778 | app_cookies++; |
| 3779 | |
| 3780 | if (del_cookie != NULL) { |
| 3781 | buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 3782 | p4 -= (p1 - del_cookie); |
| 3783 | ptr -= (p1 - del_cookie); |
| 3784 | del_cookie = del_colon = NULL; |
| 3785 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3786 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3787 | |
| 3788 | if ((t->proxy->appsession_name != NULL) && |
| 3789 | (memcmp(p1, t->proxy->appsession_name, p2 - p1) == 0)) { |
| 3790 | /* first, let's see if the cookie is our appcookie*/ |
| 3791 | |
| 3792 | /* Cool... it's the right one */ |
| 3793 | |
| 3794 | asession_temp = &local_asession; |
| 3795 | |
| 3796 | if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) { |
| 3797 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3798 | send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3799 | return 0; |
| 3800 | } |
| 3801 | |
| 3802 | memcpy(asession_temp->sessid, p3, t->proxy->appsession_len); |
| 3803 | asession_temp->sessid[t->proxy->appsession_len] = 0; |
| 3804 | asession_temp->serverid = NULL; |
| 3805 | |
| 3806 | /* only do insert, if lookup fails */ |
| 3807 | if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *) &asession_temp) != 0) { |
| 3808 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 3809 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 3810 | send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 3811 | return 0; |
| 3812 | } |
| 3813 | |
| 3814 | asession_temp->sessid = local_asession.sessid; |
| 3815 | asession_temp->serverid = local_asession.serverid; |
| 3816 | chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp); |
| 3817 | } |
| 3818 | else{ |
| 3819 | /* free wasted memory */ |
| 3820 | pool_free_to(apools.sessid, local_asession.sessid); |
| 3821 | } |
| 3822 | |
| 3823 | if (asession_temp->serverid == NULL) { |
| 3824 | Alert("Found Application Session without matching server.\n"); |
| 3825 | } else { |
| 3826 | struct server *srv = t->proxy->srv; |
| 3827 | while (srv) { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3828 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3829 | if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) { |
| 3830 | /* we found the server and it's usable */ |
| 3831 | t->flags &= ~SN_CK_MASK; |
| 3832 | t->flags |= SN_CK_VALID | SN_DIRECT; |
| 3833 | t->srv = srv; |
| 3834 | break; |
| 3835 | } else { |
| 3836 | t->flags &= ~SN_CK_MASK; |
| 3837 | t->flags |= SN_CK_DOWN; |
| 3838 | } |
| 3839 | } |
| 3840 | srv = srv->next; |
| 3841 | }/* end while(srv) */ |
| 3842 | }/* end else if server == NULL */ |
| 3843 | |
| 3844 | tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3845 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3846 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3847 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3848 | /* we'll have to look for another cookie ... */ |
| 3849 | p1 = p4; |
| 3850 | } /* while (p1 < ptr) */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3851 | |
| 3852 | /* There's no more cookie on this line. |
| 3853 | * We may have marked the last one(s) for deletion. |
| 3854 | * We must do this now in two ways : |
| 3855 | * - if there is no app cookie, we simply delete the header ; |
| 3856 | * - if there are app cookies, we must delete the end of the |
| 3857 | * string properly, including the colon/semi-colon before |
| 3858 | * the cookie name. |
| 3859 | */ |
| 3860 | if (del_cookie != NULL) { |
| 3861 | if (app_cookies) { |
| 3862 | buffer_replace2(req, del_colon, ptr, NULL, 0); |
| 3863 | /* WARNING! <ptr> becomes invalid for now. If some code |
| 3864 | * below needs to rely on it before the end of the global |
| 3865 | * header loop, we need to correct it with this code : |
| 3866 | * ptr = del_colon; |
| 3867 | */ |
| 3868 | } |
| 3869 | else |
| 3870 | delete_header = 1; |
| 3871 | } |
| 3872 | } /* end of cookie processing on this header */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3873 | |
| 3874 | /* let's look if we have to delete this header */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3875 | if (delete_header && !(t->flags & SN_CLDENY)) { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3876 | buffer_replace2(req, req->h, req->lr, NULL, 0); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3877 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3878 | /* WARNING: ptr is not valid anymore, since the header may have been deleted or truncated ! */ |
| 3879 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3880 | req->h = req->lr; |
| 3881 | } /* while (req->lr < req->r) */ |
| 3882 | |
| 3883 | /* end of header processing (even if incomplete) */ |
| 3884 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3885 | if ((req->l < req->rlim - req->data) && ! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 3886 | /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer |
| 3887 | * full. We cannot loop here since event_cli_read will disable it only if |
| 3888 | * req->l == rlim-data |
| 3889 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3890 | FD_SET(t->cli_fd, StaticReadEvent); |
| 3891 | if (t->proxy->clitimeout) |
| 3892 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 3893 | else |
| 3894 | tv_eternity(&t->crexpire); |
| 3895 | } |
| 3896 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3897 | /* Since we are in header mode, if there's no space left for headers, we |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3898 | * won't be able to free more later, so the session will never terminate. |
| 3899 | */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3900 | if (req->l >= req->rlim - req->data) { |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3901 | t->logs.status = 400; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3902 | client_retnclose(t, t->proxy->errmsg.len400, t->proxy->errmsg.msg400); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3903 | if (!(t->flags & SN_ERR_MASK)) |
| 3904 | t->flags |= SN_ERR_PRXCOND; |
| 3905 | if (!(t->flags & SN_FINST_MASK)) |
| 3906 | t->flags |= SN_FINST_R; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3907 | return 1; |
| 3908 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3909 | else if (t->res_cr == RES_ERROR || t->res_cr == RES_NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3910 | /* read error, or last read : give up. */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3911 | tv_eternity(&t->crexpire); |
| 3912 | fd_delete(t->cli_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3913 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3914 | if (!(t->flags & SN_ERR_MASK)) |
| 3915 | t->flags |= SN_ERR_CLICL; |
| 3916 | if (!(t->flags & SN_FINST_MASK)) |
| 3917 | t->flags |= SN_FINST_R; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3918 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3919 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3920 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 3921 | |
| 3922 | /* read timeout : give up with an error message. |
| 3923 | */ |
| 3924 | t->logs.status = 408; |
| 3925 | client_retnclose(t, t->proxy->errmsg.len408, t->proxy->errmsg.msg408); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3926 | if (!(t->flags & SN_ERR_MASK)) |
| 3927 | t->flags |= SN_ERR_CLITO; |
| 3928 | if (!(t->flags & SN_FINST_MASK)) |
| 3929 | t->flags |= SN_FINST_R; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3930 | return 1; |
| 3931 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3932 | |
| 3933 | return t->cli_state != CL_STHEADERS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3934 | } |
| 3935 | else if (c == CL_STDATA) { |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 3936 | process_data: |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 3937 | /* FIXME: this error handling is partly buggy because we always report |
| 3938 | * a 'DATA' phase while we don't know if the server was in IDLE, CONN |
| 3939 | * or HEADER phase. BTW, it's not logical to expire the client while |
| 3940 | * we're waiting for the server to connect. |
| 3941 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3942 | /* read or write error */ |
| 3943 | if (t->res_cw == RES_ERROR || t->res_cr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3944 | tv_eternity(&t->crexpire); |
| 3945 | tv_eternity(&t->cwexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3946 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3947 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3948 | if (!(t->flags & SN_ERR_MASK)) |
| 3949 | t->flags |= SN_ERR_CLICL; |
| 3950 | if (!(t->flags & SN_FINST_MASK)) |
| 3951 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3952 | return 1; |
| 3953 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3954 | /* last read, or end of server write */ |
| 3955 | else if (t->res_cr == RES_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3956 | FD_CLR(t->cli_fd, StaticReadEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3957 | tv_eternity(&t->crexpire); |
| 3958 | shutdown(t->cli_fd, SHUT_RD); |
| 3959 | t->cli_state = CL_STSHUTR; |
| 3960 | return 1; |
| 3961 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3962 | /* last server read and buffer empty */ |
| 3963 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3964 | FD_CLR(t->cli_fd, StaticWriteEvent); |
| 3965 | tv_eternity(&t->cwexpire); |
| 3966 | shutdown(t->cli_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3967 | /* We must ensure that the read part is still alive when switching |
| 3968 | * to shutw */ |
| 3969 | FD_SET(t->cli_fd, StaticReadEvent); |
| 3970 | if (t->proxy->clitimeout) |
| 3971 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3972 | t->cli_state = CL_STSHUTW; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3973 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3974 | return 1; |
| 3975 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3976 | /* read timeout */ |
| 3977 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 3978 | FD_CLR(t->cli_fd, StaticReadEvent); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3979 | tv_eternity(&t->crexpire); |
| 3980 | shutdown(t->cli_fd, SHUT_RD); |
| 3981 | t->cli_state = CL_STSHUTR; |
| 3982 | if (!(t->flags & SN_ERR_MASK)) |
| 3983 | t->flags |= SN_ERR_CLITO; |
| 3984 | if (!(t->flags & SN_FINST_MASK)) |
| 3985 | t->flags |= SN_FINST_D; |
| 3986 | return 1; |
| 3987 | } |
| 3988 | /* write timeout */ |
| 3989 | else if (tv_cmp2_ms(&t->cwexpire, &now) <= 0) { |
| 3990 | FD_CLR(t->cli_fd, StaticWriteEvent); |
| 3991 | tv_eternity(&t->cwexpire); |
| 3992 | shutdown(t->cli_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3993 | /* We must ensure that the read part is still alive when switching |
| 3994 | * to shutw */ |
| 3995 | FD_SET(t->cli_fd, StaticReadEvent); |
| 3996 | if (t->proxy->clitimeout) |
| 3997 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 3998 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3999 | t->cli_state = CL_STSHUTW; |
| 4000 | if (!(t->flags & SN_ERR_MASK)) |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4001 | t->flags |= SN_ERR_CLITO; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4002 | if (!(t->flags & SN_FINST_MASK)) |
| 4003 | t->flags |= SN_FINST_D; |
| 4004 | return 1; |
| 4005 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4006 | |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 4007 | if (req->l >= req->rlim - req->data) { |
| 4008 | /* no room to read more data */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4009 | if (FD_ISSET(t->cli_fd, StaticReadEvent)) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4010 | /* stop reading until we get some space */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4011 | FD_CLR(t->cli_fd, StaticReadEvent); |
| 4012 | tv_eternity(&t->crexpire); |
| 4013 | } |
| 4014 | } |
| 4015 | else { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4016 | /* there's still some space in the buffer */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4017 | if (! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 4018 | FD_SET(t->cli_fd, StaticReadEvent); |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 4019 | if (!t->proxy->clitimeout || |
| 4020 | (t->srv_state < SV_STDATA && t->proxy->srvtimeout)) |
| 4021 | /* If the client has no timeout, or if the server not ready yet, and we |
| 4022 | * know for sure that it can expire, then it's cleaner to disable the |
| 4023 | * timeout on the client side so that too low values cannot make the |
| 4024 | * sessions abort too early. |
| 4025 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4026 | tv_eternity(&t->crexpire); |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 4027 | else |
| 4028 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4029 | } |
| 4030 | } |
| 4031 | |
| 4032 | if ((rep->l == 0) || |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 4033 | ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4034 | if (FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 4035 | FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */ |
| 4036 | tv_eternity(&t->cwexpire); |
| 4037 | } |
| 4038 | } |
| 4039 | else { /* buffer not empty */ |
| 4040 | if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 4041 | FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4042 | if (t->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4043 | tv_delayfrom(&t->cwexpire, &now, t->proxy->clitimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4044 | /* FIXME: to avoid the client to read-time-out during writes, we refresh it */ |
| 4045 | t->crexpire = t->cwexpire; |
| 4046 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4047 | else |
| 4048 | tv_eternity(&t->cwexpire); |
| 4049 | } |
| 4050 | } |
| 4051 | return 0; /* other cases change nothing */ |
| 4052 | } |
| 4053 | else if (c == CL_STSHUTR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4054 | if (t->res_cw == RES_ERROR) { |
| 4055 | tv_eternity(&t->cwexpire); |
| 4056 | fd_delete(t->cli_fd); |
| 4057 | t->cli_state = CL_STCLOSE; |
| 4058 | if (!(t->flags & SN_ERR_MASK)) |
| 4059 | t->flags |= SN_ERR_CLICL; |
| 4060 | if (!(t->flags & SN_FINST_MASK)) |
| 4061 | t->flags |= SN_FINST_D; |
| 4062 | return 1; |
| 4063 | } |
| 4064 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4065 | tv_eternity(&t->cwexpire); |
| 4066 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4067 | t->cli_state = CL_STCLOSE; |
| 4068 | return 1; |
| 4069 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4070 | else if (tv_cmp2_ms(&t->cwexpire, &now) <= 0) { |
| 4071 | tv_eternity(&t->cwexpire); |
| 4072 | fd_delete(t->cli_fd); |
| 4073 | t->cli_state = CL_STCLOSE; |
| 4074 | if (!(t->flags & SN_ERR_MASK)) |
| 4075 | t->flags |= SN_ERR_CLITO; |
| 4076 | if (!(t->flags & SN_FINST_MASK)) |
| 4077 | t->flags |= SN_FINST_D; |
| 4078 | return 1; |
| 4079 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4080 | else if ((rep->l == 0) || |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4081 | ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4082 | if (FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 4083 | FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */ |
| 4084 | tv_eternity(&t->cwexpire); |
| 4085 | } |
| 4086 | } |
| 4087 | else { /* buffer not empty */ |
| 4088 | if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 4089 | FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4090 | if (t->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4091 | tv_delayfrom(&t->cwexpire, &now, t->proxy->clitimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4092 | /* FIXME: to avoid the client to read-time-out during writes, we refresh it */ |
| 4093 | t->crexpire = t->cwexpire; |
| 4094 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4095 | else |
| 4096 | tv_eternity(&t->cwexpire); |
| 4097 | } |
| 4098 | } |
| 4099 | return 0; |
| 4100 | } |
| 4101 | else if (c == CL_STSHUTW) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4102 | if (t->res_cr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4103 | tv_eternity(&t->crexpire); |
| 4104 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4105 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4106 | if (!(t->flags & SN_ERR_MASK)) |
| 4107 | t->flags |= SN_ERR_CLICL; |
| 4108 | if (!(t->flags & SN_FINST_MASK)) |
| 4109 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4110 | return 1; |
| 4111 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4112 | else if (t->res_cr == RES_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
| 4113 | tv_eternity(&t->crexpire); |
| 4114 | fd_delete(t->cli_fd); |
| 4115 | t->cli_state = CL_STCLOSE; |
| 4116 | return 1; |
| 4117 | } |
| 4118 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 4119 | tv_eternity(&t->crexpire); |
| 4120 | fd_delete(t->cli_fd); |
| 4121 | t->cli_state = CL_STCLOSE; |
| 4122 | if (!(t->flags & SN_ERR_MASK)) |
| 4123 | t->flags |= SN_ERR_CLITO; |
| 4124 | if (!(t->flags & SN_FINST_MASK)) |
| 4125 | t->flags |= SN_FINST_D; |
| 4126 | return 1; |
| 4127 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4128 | else if (req->l >= req->rlim - req->data) { |
| 4129 | /* no room to read more data */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4130 | |
| 4131 | /* FIXME-20050705: is it possible for a client to maintain a session |
| 4132 | * after the timeout by sending more data after it receives a close ? |
| 4133 | */ |
| 4134 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4135 | if (FD_ISSET(t->cli_fd, StaticReadEvent)) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4136 | /* stop reading until we get some space */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4137 | FD_CLR(t->cli_fd, StaticReadEvent); |
| 4138 | tv_eternity(&t->crexpire); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4139 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4140 | } |
| 4141 | } |
| 4142 | else { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4143 | /* there's still some space in the buffer */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4144 | if (! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 4145 | FD_SET(t->cli_fd, StaticReadEvent); |
| 4146 | if (t->proxy->clitimeout) |
| 4147 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 4148 | else |
| 4149 | tv_eternity(&t->crexpire); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4150 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4151 | } |
| 4152 | } |
| 4153 | return 0; |
| 4154 | } |
| 4155 | else { /* CL_STCLOSE: nothing to do */ |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4156 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4157 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 4158 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4159 | write(1, trash, len); |
| 4160 | } |
| 4161 | return 0; |
| 4162 | } |
| 4163 | return 0; |
| 4164 | } |
| 4165 | |
| 4166 | |
| 4167 | /* |
| 4168 | * manages the server FSM and its socket. It returns 1 if a state has changed |
| 4169 | * (and a resync may be needed), 0 else. |
| 4170 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4171 | int process_srv(struct session *t) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4172 | int s = t->srv_state; |
| 4173 | int c = t->cli_state; |
| 4174 | struct buffer *req = t->req; |
| 4175 | struct buffer *rep = t->rep; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4176 | appsess *asession_temp = NULL; |
| 4177 | appsess local_asession; |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4178 | int conn_err; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4179 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4180 | #ifdef DEBUG_FULL |
| 4181 | fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 4182 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4183 | //fprintf(stderr,"process_srv: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s, |
| 4184 | //FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 4185 | //FD_ISSET(t->srv_fd, StaticReadEvent), FD_ISSET(t->srv_fd, StaticWriteEvent) |
| 4186 | //); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4187 | if (s == SV_STIDLE) { |
| 4188 | if (c == CL_STHEADERS) |
| 4189 | return 0; /* stay in idle, waiting for data to reach the client side */ |
| 4190 | else if (c == CL_STCLOSE || |
| 4191 | c == CL_STSHUTW || |
| 4192 | (c == CL_STSHUTR && t->req->l == 0)) { /* give up */ |
| 4193 | tv_eternity(&t->cnexpire); |
| 4194 | t->srv_state = SV_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4195 | if (!(t->flags & SN_ERR_MASK)) |
| 4196 | t->flags |= SN_ERR_CLICL; |
| 4197 | if (!(t->flags & SN_FINST_MASK)) |
| 4198 | t->flags |= SN_FINST_C; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4199 | return 1; |
| 4200 | } |
| 4201 | else { /* go to SV_STCONN */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4202 | /* initiate a connection to the server */ |
| 4203 | conn_err = connect_server(t); |
| 4204 | if (conn_err == SN_ERR_NONE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4205 | //fprintf(stderr,"0: c=%d, s=%d\n", c, s); |
| 4206 | t->srv_state = SV_STCONN; |
| 4207 | } |
| 4208 | else { /* try again */ |
| 4209 | while (t->conn_retries-- > 0) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4210 | if ((t->proxy->options & PR_O_REDISP) && (t->conn_retries == 0)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4211 | t->flags &= ~SN_DIRECT; /* ignore cookie and force to use the dispatcher */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4212 | t->srv = NULL; /* it's left to the dispatcher to choose a server */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4213 | if ((t->flags & SN_CK_MASK) == SN_CK_VALID) { |
| 4214 | t->flags &= ~SN_CK_MASK; |
| 4215 | t->flags |= SN_CK_DOWN; |
| 4216 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4217 | } |
| 4218 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4219 | conn_err = connect_server(t); |
| 4220 | if (conn_err == SN_ERR_NONE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4221 | t->srv_state = SV_STCONN; |
| 4222 | break; |
| 4223 | } |
| 4224 | } |
| 4225 | if (t->conn_retries < 0) { |
| 4226 | /* if conn_retries < 0 or other error, let's abort */ |
| 4227 | tv_eternity(&t->cnexpire); |
| 4228 | t->srv_state = SV_STCLOSE; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4229 | t->logs.status = 503; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4230 | if (t->proxy->mode == PR_MODE_HTTP) |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4231 | client_return(t, t->proxy->errmsg.len503, t->proxy->errmsg.msg503); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4232 | if (!(t->flags & SN_ERR_MASK)) |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4233 | t->flags |= conn_err; /* report the precise connect() error */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4234 | if (!(t->flags & SN_FINST_MASK)) |
| 4235 | t->flags |= SN_FINST_C; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4236 | } |
| 4237 | } |
| 4238 | return 1; |
| 4239 | } |
| 4240 | } |
| 4241 | else if (s == SV_STCONN) { /* connection in progress */ |
| 4242 | if (t->res_sw == RES_SILENT && tv_cmp2_ms(&t->cnexpire, &now) > 0) { |
Willy TARREAU | b451247 | 2006-03-01 22:34:48 +0100 | [diff] [blame] | 4243 | //fprintf(stderr,"1: c=%d, s=%d, now=%d.%06d, exp=%d.%06d\n", c, s, now.tv_sec, now.tv_usec, t->cnexpire.tv_sec, t->cnexpire.tv_usec); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4244 | return 0; /* nothing changed */ |
| 4245 | } |
| 4246 | else if (t->res_sw == RES_SILENT || t->res_sw == RES_ERROR) { |
| 4247 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 4248 | /* timeout, connect error or first write error */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4249 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4250 | fd_delete(t->srv_fd); |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 4251 | t->srv->cur_sess--; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4252 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4253 | t->conn_retries--; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4254 | if (t->conn_retries >= 0) { |
| 4255 | if ((t->proxy->options & PR_O_REDISP) && (t->conn_retries == 0)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4256 | t->flags &= ~SN_DIRECT; /* ignore cookie and force to use the dispatcher */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4257 | t->srv = NULL; /* it's left to the dispatcher to choose a server */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4258 | if ((t->flags & SN_CK_MASK) == SN_CK_VALID) { |
| 4259 | t->flags &= ~SN_CK_MASK; |
| 4260 | t->flags |= SN_CK_DOWN; |
| 4261 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4262 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4263 | conn_err = connect_server(t); |
| 4264 | if (conn_err == SN_ERR_NONE) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4265 | return 0; /* no state changed */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4266 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4267 | else if (t->res_sw == RES_SILENT) |
| 4268 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 4269 | else |
| 4270 | conn_err = SN_ERR_SRVCL; // it was a connect error. |
| 4271 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4272 | /* if conn_retries < 0 or other error, let's abort */ |
| 4273 | tv_eternity(&t->cnexpire); |
| 4274 | t->srv_state = SV_STCLOSE; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4275 | t->logs.status = 503; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4276 | if (t->proxy->mode == PR_MODE_HTTP) |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4277 | client_return(t, t->proxy->errmsg.len503, t->proxy->errmsg.msg503); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4278 | if (!(t->flags & SN_ERR_MASK)) |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4279 | t->flags |= conn_err; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4280 | if (!(t->flags & SN_FINST_MASK)) |
| 4281 | t->flags |= SN_FINST_C; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4282 | return 1; |
| 4283 | } |
| 4284 | else { /* no error or write 0 */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4285 | t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4286 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4287 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4288 | if (req->l == 0) /* nothing to write */ { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4289 | FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4290 | tv_eternity(&t->swexpire); |
| 4291 | } else /* need the right to write */ { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4292 | FD_SET(t->srv_fd, StaticWriteEvent); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4293 | if (t->proxy->srvtimeout) { |
| 4294 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
| 4295 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 4296 | t->srexpire = t->swexpire; |
| 4297 | } |
| 4298 | else |
| 4299 | tv_eternity(&t->swexpire); |
| 4300 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4301 | |
| 4302 | if (t->proxy->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 4303 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4304 | if (t->proxy->srvtimeout) |
| 4305 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4306 | else |
| 4307 | tv_eternity(&t->srexpire); |
| 4308 | |
| 4309 | t->srv_state = SV_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4310 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4311 | |
| 4312 | /* if the user wants to log as soon as possible, without counting |
| 4313 | bytes from the server, then this is the right moment. */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 4314 | if (t->proxy->to_log && !(t->logs.logwait & LW_BYTES)) { |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4315 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 4316 | sess_log(t); |
| 4317 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4318 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4319 | else { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4320 | t->srv_state = SV_STHEADERS; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4321 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
| 4322 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4323 | tv_eternity(&t->cnexpire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4324 | return 1; |
| 4325 | } |
| 4326 | } |
| 4327 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4328 | /* now parse the partial (or complete) headers */ |
| 4329 | while (rep->lr < rep->r) { /* this loop only sees one header at each iteration */ |
| 4330 | char *ptr; |
| 4331 | int delete_header; |
| 4332 | |
| 4333 | ptr = rep->lr; |
| 4334 | |
| 4335 | /* look for the end of the current header */ |
| 4336 | while (ptr < rep->r && *ptr != '\n' && *ptr != '\r') |
| 4337 | ptr++; |
| 4338 | |
| 4339 | if (ptr == rep->h) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4340 | int line, len; |
| 4341 | |
| 4342 | /* we can only get here after an end of headers */ |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4343 | |
| 4344 | /* first, we'll block if security checks have caught nasty things */ |
| 4345 | if (t->flags & SN_CACHEABLE) { |
| 4346 | if ((t->flags & SN_CACHE_COOK) && |
| 4347 | (t->flags & SN_SCK_ANY) && |
| 4348 | (t->proxy->options & PR_O_CHK_CACHE)) { |
| 4349 | |
| 4350 | /* we're in presence of a cacheable response containing |
| 4351 | * a set-cookie header. We'll block it as requested by |
| 4352 | * the 'checkcache' option, and send an alert. |
| 4353 | */ |
| 4354 | tv_eternity(&t->srexpire); |
| 4355 | tv_eternity(&t->swexpire); |
| 4356 | fd_delete(t->srv_fd); |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 4357 | t->srv->cur_sess--; |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4358 | t->srv_state = SV_STCLOSE; |
| 4359 | t->logs.status = 502; |
| 4360 | client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502); |
| 4361 | if (!(t->flags & SN_ERR_MASK)) |
| 4362 | t->flags |= SN_ERR_PRXCOND; |
| 4363 | if (!(t->flags & SN_FINST_MASK)) |
| 4364 | t->flags |= SN_FINST_H; |
| 4365 | |
| 4366 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", t->proxy->id, t->srv->id); |
| 4367 | send_log(t->proxy, LOG_ALERT, "Blocking cacheable cookie in response from instance %s, server %s.\n", t->proxy->id, t->srv->id); |
| 4368 | |
| 4369 | return 1; |
| 4370 | } |
| 4371 | } |
| 4372 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4373 | /* next, we'll block if an 'rspideny' or 'rspdeny' filter matched */ |
| 4374 | if (t->flags & SN_SVDENY) { |
| 4375 | tv_eternity(&t->srexpire); |
| 4376 | tv_eternity(&t->swexpire); |
| 4377 | fd_delete(t->srv_fd); |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 4378 | t->srv->cur_sess--; |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4379 | t->srv_state = SV_STCLOSE; |
| 4380 | t->logs.status = 502; |
| 4381 | client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502); |
| 4382 | if (!(t->flags & SN_ERR_MASK)) |
| 4383 | t->flags |= SN_ERR_PRXCOND; |
| 4384 | if (!(t->flags & SN_FINST_MASK)) |
| 4385 | t->flags |= SN_FINST_H; |
| 4386 | return 1; |
| 4387 | } |
| 4388 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4389 | /* we'll have something else to do here : add new headers ... */ |
| 4390 | |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 4391 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_INS) && |
| 4392 | (!(t->proxy->options & PR_O_COOK_POST) || (t->flags & SN_POST))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4393 | /* the server is known, it's not the one the client requested, we have to |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 4394 | * insert a set-cookie here, except if we want to insert only on POST |
| 4395 | * requests and this one isn't. |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4396 | */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4397 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/\r\n", |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4398 | t->proxy->cookie_name, |
| 4399 | t->srv->cookie ? t->srv->cookie : ""); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4400 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4401 | t->flags |= SN_SCK_INSERTED; |
| 4402 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4403 | /* Here, we will tell an eventual cache on the client side that we don't |
| 4404 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 4405 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 4406 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 4407 | */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4408 | if (t->proxy->options & PR_O_COOK_NOC) |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4409 | //len += sprintf(newhdr + len, "Cache-control: no-cache=\"set-cookie\"\r\n"); |
| 4410 | len += sprintf(trash + len, "Cache-control: private\r\n"); |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 4411 | |
| 4412 | if (rep->data + rep->l < rep->h) |
| 4413 | /* The data has been stolen, we will crash cleanly instead of corrupting memory */ |
| 4414 | *(int *)0 = 0; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4415 | buffer_replace2(rep, rep->h, rep->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | /* headers to be added */ |
| 4419 | for (line = 0; line < t->proxy->nb_rspadd; line++) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4420 | len = sprintf(trash, "%s\r\n", t->proxy->rsp_add[line]); |
| 4421 | buffer_replace2(rep, rep->h, rep->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4422 | } |
| 4423 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4424 | /* add a "connection: close" line if needed */ |
| 4425 | if (t->proxy->options & PR_O_HTTP_CLOSE) |
| 4426 | buffer_replace2(rep, rep->h, rep->h, "Connection: close\r\n", 19); |
| 4427 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4428 | t->srv_state = SV_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4429 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4430 | t->logs.t_data = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4431 | |
Willy TARREAU | 767ba71 | 2006-03-01 22:40:50 +0100 | [diff] [blame] | 4432 | /* client connection already closed or option 'httpclose' required : |
| 4433 | * we close the server's outgoing connection right now. |
| 4434 | */ |
| 4435 | if ((req->l == 0) && |
| 4436 | (c == CL_STSHUTR || c == CL_STCLOSE || t->proxy->options & PR_O_FORCE_CLO)) { |
| 4437 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4438 | tv_eternity(&t->swexpire); |
| 4439 | |
| 4440 | /* We must ensure that the read part is still alive when switching |
| 4441 | * to shutw */ |
| 4442 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4443 | if (t->proxy->srvtimeout) |
| 4444 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4445 | |
| 4446 | shutdown(t->srv_fd, SHUT_WR); |
| 4447 | t->srv_state = SV_STSHUTW; |
| 4448 | } |
| 4449 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4450 | /* if the user wants to log as soon as possible, without counting |
| 4451 | bytes from the server, then this is the right moment. */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 4452 | if (t->proxy->to_log && !(t->logs.logwait & LW_BYTES)) { |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4453 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 4454 | t->logs.bytes = rep->h - rep->data; |
| 4455 | sess_log(t); |
| 4456 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4457 | break; |
| 4458 | } |
| 4459 | |
| 4460 | /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */ |
| 4461 | if (ptr > rep->r - 2) { |
| 4462 | /* this is a partial header, let's wait for more to come */ |
| 4463 | rep->lr = ptr; |
| 4464 | break; |
| 4465 | } |
| 4466 | |
| 4467 | // fprintf(stderr,"h=%p, ptr=%p, lr=%p, r=%p, *h=", rep->h, ptr, rep->lr, rep->r); |
| 4468 | // write(2, rep->h, ptr - rep->h); fprintf(stderr,"\n"); |
| 4469 | |
| 4470 | /* now we know that *ptr is either \r or \n, |
| 4471 | * and that there are at least 1 char after it. |
| 4472 | */ |
| 4473 | if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n')) |
| 4474 | rep->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */ |
| 4475 | else |
| 4476 | rep->lr = ptr + 2; /* \r\n or \n\r */ |
| 4477 | |
| 4478 | /* |
| 4479 | * now we know that we have a full header ; we can do whatever |
| 4480 | * we want with these pointers : |
| 4481 | * rep->h = beginning of header |
| 4482 | * ptr = end of header (first \r or \n) |
| 4483 | * rep->lr = beginning of next line (next rep->h) |
| 4484 | * rep->r = end of data (not used at this stage) |
| 4485 | */ |
| 4486 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4487 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4488 | if (t->logs.status == -1) { |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4489 | t->logs.logwait &= ~LW_RESP; |
| 4490 | t->logs.status = atoi(rep->h + 9); |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4491 | switch (t->logs.status) { |
| 4492 | case 200: |
| 4493 | case 203: |
| 4494 | case 206: |
| 4495 | case 300: |
| 4496 | case 301: |
| 4497 | case 410: |
| 4498 | /* RFC2616 @13.4: |
| 4499 | * "A response received with a status code of |
| 4500 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4501 | * by a cache (...) unless a cache-control |
| 4502 | * directive prohibits caching." |
| 4503 | * |
| 4504 | * RFC2616 @9.5: POST method : |
| 4505 | * "Responses to this method are not cacheable, |
| 4506 | * unless the response includes appropriate |
| 4507 | * Cache-Control or Expires header fields." |
| 4508 | */ |
| 4509 | if ((!t->flags & SN_POST) && (t->proxy->options & PR_O_CHK_CACHE)) |
| 4510 | t->flags |= SN_CACHEABLE | SN_CACHE_COOK; |
| 4511 | break; |
| 4512 | default: |
| 4513 | break; |
| 4514 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 4515 | } |
| 4516 | else if (t->logs.logwait & LW_RSPHDR) { |
| 4517 | struct cap_hdr *h; |
| 4518 | int len; |
| 4519 | for (h = t->proxy->rsp_cap; h; h = h->next) { |
| 4520 | if ((h->namelen + 2 <= ptr - rep->h) && |
| 4521 | (rep->h[h->namelen] == ':') && |
| 4522 | (strncasecmp(rep->h, h->name, h->namelen) == 0)) { |
| 4523 | |
| 4524 | if (t->rsp_cap[h->index] == NULL) |
| 4525 | t->rsp_cap[h->index] = pool_alloc_from(h->pool, h->len + 1); |
| 4526 | |
| 4527 | len = ptr - (rep->h + h->namelen + 2); |
| 4528 | if (len > h->len) |
| 4529 | len = h->len; |
| 4530 | |
| 4531 | memcpy(t->rsp_cap[h->index], rep->h + h->namelen + 2, len); |
| 4532 | t->rsp_cap[h->index][len]=0; |
| 4533 | } |
| 4534 | } |
| 4535 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4536 | } |
| 4537 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4538 | delete_header = 0; |
| 4539 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4540 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4541 | int len, max; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 4542 | len = sprintf(trash, "%08x:%s.srvhdr[%04x:%04x]: ", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4543 | max = ptr - rep->h; |
| 4544 | UBOUND(max, sizeof(trash) - len - 1); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4545 | len += strlcpy2(trash + len, rep->h, max + 1); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4546 | trash[len++] = '\n'; |
| 4547 | write(1, trash, len); |
| 4548 | } |
| 4549 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4550 | /* remove "connection: " if needed */ |
| 4551 | if (!delete_header && (t->proxy->options & PR_O_HTTP_CLOSE) |
| 4552 | && (strncasecmp(rep->h, "Connection: ", 12) == 0)) { |
| 4553 | delete_header = 1; |
| 4554 | } |
| 4555 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4556 | /* try headers regexps */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4557 | if (!delete_header && t->proxy->rsp_exp != NULL |
| 4558 | && !(t->flags & SN_SVDENY)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4559 | struct hdr_exp *exp; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4560 | char term; |
| 4561 | |
| 4562 | term = *ptr; |
| 4563 | *ptr = '\0'; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4564 | exp = t->proxy->rsp_exp; |
| 4565 | do { |
| 4566 | if (regexec(exp->preg, rep->h, MAX_MATCH, pmatch, 0) == 0) { |
| 4567 | switch (exp->action) { |
| 4568 | case ACT_ALLOW: |
| 4569 | if (!(t->flags & SN_SVDENY)) |
| 4570 | t->flags |= SN_SVALLOW; |
| 4571 | break; |
| 4572 | case ACT_REPLACE: |
| 4573 | if (!(t->flags & SN_SVDENY)) { |
| 4574 | int len = exp_replace(trash, rep->h, exp->replace, pmatch); |
| 4575 | ptr += buffer_replace2(rep, rep->h, ptr, trash, len); |
| 4576 | } |
| 4577 | break; |
| 4578 | case ACT_REMOVE: |
| 4579 | if (!(t->flags & SN_SVDENY)) |
| 4580 | delete_header = 1; |
| 4581 | break; |
| 4582 | case ACT_DENY: |
| 4583 | if (!(t->flags & SN_SVALLOW)) |
| 4584 | t->flags |= SN_SVDENY; |
| 4585 | break; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4586 | case ACT_PASS: /* we simply don't deny this one */ |
| 4587 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4588 | } |
| 4589 | break; |
| 4590 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4591 | } while ((exp = exp->next) != NULL); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4592 | *ptr = term; /* restore the string terminator */ |
| 4593 | } |
| 4594 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4595 | /* check for cache-control: or pragma: headers */ |
| 4596 | if (!delete_header && (t->flags & SN_CACHEABLE)) { |
| 4597 | if (strncasecmp(rep->h, "Pragma: no-cache", 16) == 0) |
| 4598 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4599 | else if (strncasecmp(rep->h, "Cache-control: ", 15) == 0) { |
| 4600 | if (strncasecmp(rep->h + 15, "no-cache", 8) == 0) { |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4601 | if (rep->h + 23 == ptr || rep->h[23] == ',') |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4602 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4603 | else { |
| 4604 | if (strncasecmp(rep->h + 23, "=\"set-cookie", 12) == 0 |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4605 | && (rep->h[35] == '"' || rep->h[35] == ',')) |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4606 | t->flags &= ~SN_CACHE_COOK; |
| 4607 | } |
| 4608 | } else if ((strncasecmp(rep->h + 15, "private", 7) == 0 && |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4609 | (rep->h + 22 == ptr || rep->h[22] == ',')) |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4610 | || (strncasecmp(rep->h + 15, "no-store", 8) == 0 && |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4611 | (rep->h + 23 == ptr || rep->h[23] == ','))) { |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4612 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4613 | } else if (strncasecmp(rep->h + 15, "max-age=0", 9) == 0 && |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4614 | (rep->h + 24 == ptr || rep->h[24] == ',')) { |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4615 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4616 | } else if (strncasecmp(rep->h + 15, "s-maxage=0", 10) == 0 && |
| 4617 | (rep->h + 25 == ptr || rep->h[25] == ',')) { |
| 4618 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4619 | } else if (strncasecmp(rep->h + 15, "public", 6) == 0 && |
| 4620 | (rep->h + 21 == ptr || rep->h[21] == ',')) { |
| 4621 | t->flags |= SN_CACHEABLE | SN_CACHE_COOK; |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4622 | } |
| 4623 | } |
| 4624 | } |
| 4625 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4626 | /* check for server cookies */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4627 | if (!delete_header /*&& (t->proxy->options & PR_O_COOK_ANY)*/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4628 | && (t->proxy->cookie_name != NULL || t->proxy->capture_name != NULL || t->proxy->appsession_name !=NULL) |
willy tarreau | 906b268 | 2005-12-17 13:49:52 +0100 | [diff] [blame] | 4629 | && (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4630 | char *p1, *p2, *p3, *p4; |
| 4631 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4632 | t->flags |= SN_SCK_ANY; |
| 4633 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4634 | p1 = rep->h + 12; /* first char after 'Set-Cookie: ' */ |
| 4635 | |
| 4636 | while (p1 < ptr) { /* in fact, we'll break after the first cookie */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 4637 | while (p1 < ptr && (isspace((int)*p1))) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4638 | p1++; |
| 4639 | |
| 4640 | if (p1 == ptr || *p1 == ';') /* end of cookie */ |
| 4641 | break; |
| 4642 | |
| 4643 | /* p1 is at the beginning of the cookie name */ |
| 4644 | p2 = p1; |
| 4645 | |
| 4646 | while (p2 < ptr && *p2 != '=' && *p2 != ';') |
| 4647 | p2++; |
| 4648 | |
| 4649 | if (p2 == ptr || *p2 == ';') /* next cookie */ |
| 4650 | break; |
| 4651 | |
| 4652 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4653 | if (p3 == ptr) |
| 4654 | break; |
| 4655 | |
| 4656 | p4 = p3; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 4657 | while (p4 < ptr && !isspace((int)*p4) && *p4 != ';') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4658 | p4++; |
| 4659 | |
| 4660 | /* here, we have the cookie name between p1 and p2, |
| 4661 | * and its value between p3 and p4. |
| 4662 | * we can process it. |
| 4663 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4664 | |
| 4665 | /* first, let's see if we want to capture it */ |
| 4666 | if (t->proxy->capture_name != NULL && |
| 4667 | t->logs.srv_cookie == NULL && |
| 4668 | (p4 - p1 >= t->proxy->capture_namelen) && |
| 4669 | memcmp(p1, t->proxy->capture_name, t->proxy->capture_namelen) == 0) { |
| 4670 | int log_len = p4 - p1; |
| 4671 | |
| 4672 | if ((t->logs.srv_cookie = pool_alloc(capture)) == NULL) { |
| 4673 | Alert("HTTP logging : out of memory.\n"); |
| 4674 | } |
| 4675 | |
| 4676 | if (log_len > t->proxy->capture_len) |
| 4677 | log_len = t->proxy->capture_len; |
| 4678 | memcpy(t->logs.srv_cookie, p1, log_len); |
| 4679 | t->logs.srv_cookie[log_len] = 0; |
| 4680 | } |
| 4681 | |
| 4682 | if ((p2 - p1 == t->proxy->cookie_len) && (t->proxy->cookie_name != NULL) && |
| 4683 | (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4684 | /* Cool... it's the right one */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4685 | t->flags |= SN_SCK_SEEN; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4686 | |
| 4687 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4688 | * this occurrence because we'll insert another one later. |
| 4689 | * We'll delete it too if the "indirect" option is set and we're in |
| 4690 | * a direct access. */ |
| 4691 | if (((t->srv) && (t->proxy->options & PR_O_COOK_INS)) || |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4692 | ((t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_IND))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4693 | /* this header must be deleted */ |
| 4694 | delete_header = 1; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4695 | t->flags |= SN_SCK_DELETED; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4696 | } |
| 4697 | else if ((t->srv) && (t->proxy->options & PR_O_COOK_RW)) { |
| 4698 | /* replace bytes p3->p4 with the cookie name associated |
| 4699 | * with this server since we know it. |
| 4700 | */ |
| 4701 | buffer_replace2(rep, p3, p4, t->srv->cookie, t->srv->cklen); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4702 | t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4703 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 4704 | else if ((t->srv) && (t->proxy->options & PR_O_COOK_PFX)) { |
| 4705 | /* insert the cookie name associated with this server |
| 4706 | * before existing cookie, and insert a delimitor between them.. |
| 4707 | */ |
| 4708 | buffer_replace2(rep, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4709 | p3[t->srv->cklen] = COOKIE_DELIM; |
| 4710 | t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED; |
| 4711 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4712 | break; |
| 4713 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4714 | |
| 4715 | /* first, let's see if the cookie is our appcookie*/ |
| 4716 | if ((t->proxy->appsession_name != NULL) && |
| 4717 | (memcmp(p1, t->proxy->appsession_name, p2 - p1) == 0)) { |
| 4718 | |
| 4719 | /* Cool... it's the right one */ |
| 4720 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4721 | size_t server_id_len = strlen(t->srv->id) + 1; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4722 | asession_temp = &local_asession; |
| 4723 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4724 | if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4725 | Alert("Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 4726 | send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 4727 | } |
| 4728 | memcpy(asession_temp->sessid, p3, t->proxy->appsession_len); |
| 4729 | asession_temp->sessid[t->proxy->appsession_len] = 0; |
| 4730 | asession_temp->serverid = NULL; |
| 4731 | |
| 4732 | /* only do insert, if lookup fails */ |
| 4733 | if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *) &asession_temp) != 0) { |
| 4734 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 4735 | Alert("Not enought Memory process_srv():asession:calloc().\n"); |
| 4736 | send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession:calloc().\n"); |
| 4737 | return 0; |
| 4738 | } |
| 4739 | asession_temp->sessid = local_asession.sessid; |
| 4740 | asession_temp->serverid = local_asession.serverid; |
| 4741 | chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4742 | }/* end if (chtbl_lookup()) */ |
| 4743 | else { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4744 | /* free wasted memory */ |
| 4745 | pool_free_to(apools.sessid, local_asession.sessid); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4746 | } /* end else from if (chtbl_lookup()) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4747 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4748 | if (asession_temp->serverid == NULL) { |
| 4749 | if ((asession_temp->serverid = pool_alloc_from(apools.serverid, apools.ser_msize)) == NULL) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4750 | Alert("Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 4751 | send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 4752 | } |
| 4753 | asession_temp->serverid[0] = '\0'; |
| 4754 | } |
| 4755 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4756 | if (asession_temp->serverid[0] == '\0') |
| 4757 | memcpy(asession_temp->serverid,t->srv->id,server_id_len); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4758 | |
| 4759 | tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout); |
| 4760 | |
| 4761 | #if defined(DEBUG_HASH) |
| 4762 | print_table(&(t->proxy->htbl_proxy)); |
| 4763 | #endif |
| 4764 | break; |
| 4765 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4766 | else { |
| 4767 | // fprintf(stderr,"Ignoring unknown cookie : "); |
| 4768 | // write(2, p1, p2-p1); |
| 4769 | // fprintf(stderr," = "); |
| 4770 | // write(2, p3, p4-p3); |
| 4771 | // fprintf(stderr,"\n"); |
| 4772 | } |
| 4773 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4774 | } /* we're now at the end of the cookie value */ |
| 4775 | } /* end of cookie processing */ |
| 4776 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4777 | /* check for any set-cookie in case we check for cacheability */ |
| 4778 | if (!delete_header && !(t->flags & SN_SCK_ANY) && |
| 4779 | (t->proxy->options & PR_O_CHK_CACHE) && |
| 4780 | (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) { |
| 4781 | t->flags |= SN_SCK_ANY; |
| 4782 | } |
| 4783 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4784 | /* let's look if we have to delete this header */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4785 | if (delete_header && !(t->flags & SN_SVDENY)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4786 | buffer_replace2(rep, rep->h, rep->lr, "", 0); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4787 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4788 | rep->h = rep->lr; |
| 4789 | } /* while (rep->lr < rep->r) */ |
| 4790 | |
| 4791 | /* end of header processing (even if incomplete) */ |
| 4792 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4793 | if ((rep->l < rep->rlim - rep->data) && ! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 4794 | /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer |
| 4795 | * full. We cannot loop here since event_srv_read will disable it only if |
| 4796 | * rep->l == rlim-data |
| 4797 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4798 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4799 | if (t->proxy->srvtimeout) |
| 4800 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4801 | else |
| 4802 | tv_eternity(&t->srexpire); |
| 4803 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4804 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4805 | /* read error, write error */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4806 | if (t->res_sw == RES_ERROR || t->res_sr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4807 | tv_eternity(&t->srexpire); |
| 4808 | tv_eternity(&t->swexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4809 | fd_delete(t->srv_fd); |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 4810 | t->srv->cur_sess--; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4811 | t->srv_state = SV_STCLOSE; |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 4812 | t->logs.status = 502; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4813 | client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4814 | if (!(t->flags & SN_ERR_MASK)) |
| 4815 | t->flags |= SN_ERR_SRVCL; |
| 4816 | if (!(t->flags & SN_FINST_MASK)) |
| 4817 | t->flags |= SN_FINST_H; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4818 | return 1; |
| 4819 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4820 | /* end of client write or end of server read. |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4821 | * since we are in header mode, if there's no space left for headers, we |
| 4822 | * won't be able to free more later, so the session will never terminate. |
| 4823 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4824 | else if (t->res_sr == RES_NULL || c == CL_STSHUTW || c == CL_STCLOSE || rep->l >= rep->rlim - rep->data) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4825 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4826 | tv_eternity(&t->srexpire); |
| 4827 | shutdown(t->srv_fd, SHUT_RD); |
| 4828 | t->srv_state = SV_STSHUTR; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4829 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4830 | return 1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4831 | } |
| 4832 | /* read timeout : return a 504 to the client. |
| 4833 | */ |
| 4834 | else if (FD_ISSET(t->srv_fd, StaticReadEvent) && tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 4835 | tv_eternity(&t->srexpire); |
| 4836 | tv_eternity(&t->swexpire); |
| 4837 | fd_delete(t->srv_fd); |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 4838 | t->srv->cur_sess--; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4839 | t->srv_state = SV_STCLOSE; |
| 4840 | t->logs.status = 504; |
| 4841 | client_return(t, t->proxy->errmsg.len504, t->proxy->errmsg.msg504); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4842 | if (!(t->flags & SN_ERR_MASK)) |
| 4843 | t->flags |= SN_ERR_SRVTO; |
| 4844 | if (!(t->flags & SN_FINST_MASK)) |
| 4845 | t->flags |= SN_FINST_H; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4846 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4847 | |
| 4848 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4849 | /* last client read and buffer empty */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4850 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 4851 | * client shuts read too early, because we may still have |
| 4852 | * some work to do on the headers. |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4853 | * The side-effect is that if the client completely closes its |
| 4854 | * connection during SV_STHEADER, the connection to the server |
| 4855 | * is kept until a response comes back or the timeout is reached. |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4856 | */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4857 | else if ((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && (req->l == 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4858 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4859 | tv_eternity(&t->swexpire); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4860 | |
| 4861 | /* We must ensure that the read part is still alive when switching |
| 4862 | * to shutw */ |
| 4863 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4864 | if (t->proxy->srvtimeout) |
| 4865 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4866 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4867 | shutdown(t->srv_fd, SHUT_WR); |
| 4868 | t->srv_state = SV_STSHUTW; |
| 4869 | return 1; |
| 4870 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4871 | /* write timeout */ |
| 4872 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 4873 | * client shuts read too early, because we may still have |
| 4874 | * some work to do on the headers. |
| 4875 | */ |
| 4876 | else if (FD_ISSET(t->srv_fd, StaticWriteEvent) && tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
| 4877 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4878 | tv_eternity(&t->swexpire); |
| 4879 | shutdown(t->srv_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4880 | /* We must ensure that the read part is still alive when switching |
| 4881 | * to shutw */ |
| 4882 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4883 | if (t->proxy->srvtimeout) |
| 4884 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4885 | |
| 4886 | /* We must ensure that the read part is still alive when switching |
| 4887 | * to shutw */ |
| 4888 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4889 | if (t->proxy->srvtimeout) |
| 4890 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4891 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4892 | t->srv_state = SV_STSHUTW; |
| 4893 | if (!(t->flags & SN_ERR_MASK)) |
| 4894 | t->flags |= SN_ERR_SRVTO; |
| 4895 | if (!(t->flags & SN_FINST_MASK)) |
| 4896 | t->flags |= SN_FINST_H; |
| 4897 | return 1; |
| 4898 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4899 | |
| 4900 | if (req->l == 0) { |
| 4901 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4902 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 4903 | tv_eternity(&t->swexpire); |
| 4904 | } |
| 4905 | } |
| 4906 | else { /* client buffer not empty */ |
| 4907 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4908 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4909 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4910 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4911 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 4912 | t->srexpire = t->swexpire; |
| 4913 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4914 | else |
| 4915 | tv_eternity(&t->swexpire); |
| 4916 | } |
| 4917 | } |
| 4918 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4919 | /* be nice with the client side which would like to send a complete header |
| 4920 | * FIXME: COMPLETELY BUGGY !!! not all headers may be processed because the client |
| 4921 | * would read all remaining data at once ! The client should not write past rep->lr |
| 4922 | * when the server is in header state. |
| 4923 | */ |
| 4924 | //return header_processed; |
| 4925 | return t->srv_state != SV_STHEADERS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4926 | } |
| 4927 | else if (s == SV_STDATA) { |
| 4928 | /* read or write error */ |
| 4929 | if (t->res_sw == RES_ERROR || t->res_sr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4930 | tv_eternity(&t->srexpire); |
| 4931 | tv_eternity(&t->swexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4932 | fd_delete(t->srv_fd); |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 4933 | t->srv->cur_sess--; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4934 | t->srv_state = SV_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4935 | if (!(t->flags & SN_ERR_MASK)) |
| 4936 | t->flags |= SN_ERR_SRVCL; |
| 4937 | if (!(t->flags & SN_FINST_MASK)) |
| 4938 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4939 | return 1; |
| 4940 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4941 | /* last read, or end of client write */ |
| 4942 | else if (t->res_sr == RES_NULL || c == CL_STSHUTW || c == CL_STCLOSE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4943 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4944 | tv_eternity(&t->srexpire); |
| 4945 | shutdown(t->srv_fd, SHUT_RD); |
| 4946 | t->srv_state = SV_STSHUTR; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4947 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4948 | return 1; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 4949 | } |
| 4950 | /* end of client read and no more data to send */ |
| 4951 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
| 4952 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4953 | tv_eternity(&t->swexpire); |
| 4954 | shutdown(t->srv_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4955 | /* We must ensure that the read part is still alive when switching |
| 4956 | * to shutw */ |
| 4957 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4958 | if (t->proxy->srvtimeout) |
| 4959 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4960 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 4961 | t->srv_state = SV_STSHUTW; |
| 4962 | return 1; |
| 4963 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4964 | /* read timeout */ |
| 4965 | else if (tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 4966 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4967 | tv_eternity(&t->srexpire); |
| 4968 | shutdown(t->srv_fd, SHUT_RD); |
| 4969 | t->srv_state = SV_STSHUTR; |
| 4970 | if (!(t->flags & SN_ERR_MASK)) |
| 4971 | t->flags |= SN_ERR_SRVTO; |
| 4972 | if (!(t->flags & SN_FINST_MASK)) |
| 4973 | t->flags |= SN_FINST_D; |
| 4974 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4975 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4976 | /* write timeout */ |
| 4977 | else if (tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4978 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4979 | tv_eternity(&t->swexpire); |
| 4980 | shutdown(t->srv_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4981 | /* We must ensure that the read part is still alive when switching |
| 4982 | * to shutw */ |
| 4983 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4984 | if (t->proxy->srvtimeout) |
| 4985 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4986 | t->srv_state = SV_STSHUTW; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4987 | if (!(t->flags & SN_ERR_MASK)) |
| 4988 | t->flags |= SN_ERR_SRVTO; |
| 4989 | if (!(t->flags & SN_FINST_MASK)) |
| 4990 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4991 | return 1; |
| 4992 | } |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4993 | |
| 4994 | /* recompute request time-outs */ |
| 4995 | if (req->l == 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4996 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4997 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 4998 | tv_eternity(&t->swexpire); |
| 4999 | } |
| 5000 | } |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 5001 | else { /* buffer not empty, there are still data to be transferred */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5002 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 5003 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 5004 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5005 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 5006 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 5007 | t->srexpire = t->swexpire; |
| 5008 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5009 | else |
| 5010 | tv_eternity(&t->swexpire); |
| 5011 | } |
| 5012 | } |
| 5013 | |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 5014 | /* recompute response time-outs */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5015 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
| 5016 | if (FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 5017 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 5018 | tv_eternity(&t->srexpire); |
| 5019 | } |
| 5020 | } |
| 5021 | else { |
| 5022 | if (! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 5023 | FD_SET(t->srv_fd, StaticReadEvent); |
| 5024 | if (t->proxy->srvtimeout) |
| 5025 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 5026 | else |
| 5027 | tv_eternity(&t->srexpire); |
| 5028 | } |
| 5029 | } |
| 5030 | |
| 5031 | return 0; /* other cases change nothing */ |
| 5032 | } |
| 5033 | else if (s == SV_STSHUTR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5034 | if (t->res_sw == RES_ERROR) { |
| 5035 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
| 5036 | tv_eternity(&t->swexpire); |
| 5037 | fd_delete(t->srv_fd); |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 5038 | t->srv->cur_sess--; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5039 | //close(t->srv_fd); |
| 5040 | t->srv_state = SV_STCLOSE; |
| 5041 | if (!(t->flags & SN_ERR_MASK)) |
| 5042 | t->flags |= SN_ERR_SRVCL; |
| 5043 | if (!(t->flags & SN_FINST_MASK)) |
| 5044 | t->flags |= SN_FINST_D; |
| 5045 | return 1; |
| 5046 | } |
| 5047 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5048 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5049 | tv_eternity(&t->swexpire); |
| 5050 | fd_delete(t->srv_fd); |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 5051 | t->srv->cur_sess--; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5052 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5053 | t->srv_state = SV_STCLOSE; |
| 5054 | return 1; |
| 5055 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5056 | else if (tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
| 5057 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
| 5058 | tv_eternity(&t->swexpire); |
| 5059 | fd_delete(t->srv_fd); |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 5060 | t->srv->cur_sess--; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5061 | //close(t->srv_fd); |
| 5062 | t->srv_state = SV_STCLOSE; |
| 5063 | if (!(t->flags & SN_ERR_MASK)) |
| 5064 | t->flags |= SN_ERR_SRVTO; |
| 5065 | if (!(t->flags & SN_FINST_MASK)) |
| 5066 | t->flags |= SN_FINST_D; |
| 5067 | return 1; |
| 5068 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5069 | else if (req->l == 0) { |
| 5070 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 5071 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 5072 | tv_eternity(&t->swexpire); |
| 5073 | } |
| 5074 | } |
| 5075 | else { /* buffer not empty */ |
| 5076 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 5077 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 5078 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5079 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 5080 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 5081 | t->srexpire = t->swexpire; |
| 5082 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5083 | else |
| 5084 | tv_eternity(&t->swexpire); |
| 5085 | } |
| 5086 | } |
| 5087 | return 0; |
| 5088 | } |
| 5089 | else if (s == SV_STSHUTW) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5090 | if (t->res_sr == RES_ERROR) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5091 | //FD_CLR(t->srv_fd, StaticReadEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5092 | tv_eternity(&t->srexpire); |
| 5093 | fd_delete(t->srv_fd); |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 5094 | t->srv->cur_sess--; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5095 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5096 | t->srv_state = SV_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5097 | if (!(t->flags & SN_ERR_MASK)) |
| 5098 | t->flags |= SN_ERR_SRVCL; |
| 5099 | if (!(t->flags & SN_FINST_MASK)) |
| 5100 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5101 | return 1; |
| 5102 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5103 | else if (t->res_sr == RES_NULL || c == CL_STSHUTW || c == CL_STCLOSE) { |
| 5104 | //FD_CLR(t->srv_fd, StaticReadEvent); |
| 5105 | tv_eternity(&t->srexpire); |
| 5106 | fd_delete(t->srv_fd); |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 5107 | t->srv->cur_sess--; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5108 | //close(t->srv_fd); |
| 5109 | t->srv_state = SV_STCLOSE; |
| 5110 | return 1; |
| 5111 | } |
| 5112 | else if (tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 5113 | //FD_CLR(t->srv_fd, StaticReadEvent); |
| 5114 | tv_eternity(&t->srexpire); |
| 5115 | fd_delete(t->srv_fd); |
willy tarreau | c136461 | 2006-04-07 16:28:28 +0200 | [diff] [blame^] | 5116 | t->srv->cur_sess--; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5117 | //close(t->srv_fd); |
| 5118 | t->srv_state = SV_STCLOSE; |
| 5119 | if (!(t->flags & SN_ERR_MASK)) |
| 5120 | t->flags |= SN_ERR_SRVTO; |
| 5121 | if (!(t->flags & SN_FINST_MASK)) |
| 5122 | t->flags |= SN_FINST_D; |
| 5123 | return 1; |
| 5124 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5125 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
| 5126 | if (FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 5127 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 5128 | tv_eternity(&t->srexpire); |
| 5129 | } |
| 5130 | } |
| 5131 | else { |
| 5132 | if (! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 5133 | FD_SET(t->srv_fd, StaticReadEvent); |
| 5134 | if (t->proxy->srvtimeout) |
| 5135 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 5136 | else |
| 5137 | tv_eternity(&t->srexpire); |
| 5138 | } |
| 5139 | } |
| 5140 | return 0; |
| 5141 | } |
| 5142 | else { /* SV_STCLOSE : nothing to do */ |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 5143 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5144 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 5145 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5146 | write(1, trash, len); |
| 5147 | } |
| 5148 | return 0; |
| 5149 | } |
| 5150 | return 0; |
| 5151 | } |
| 5152 | |
| 5153 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5154 | /* Processes the client and server jobs of a session task, then |
| 5155 | * puts it back to the wait queue in a clean state, or |
| 5156 | * cleans up its resources if it must be deleted. Returns |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5157 | * the time the task accepts to wait, or TIME_ETERNITY for |
| 5158 | * infinity. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5159 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5160 | int process_session(struct task *t) { |
| 5161 | struct session *s = t->context; |
| 5162 | int fsm_resync = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5163 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5164 | do { |
| 5165 | fsm_resync = 0; |
Willy TARREAU | b451247 | 2006-03-01 22:34:48 +0100 | [diff] [blame] | 5166 | //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5167 | fsm_resync |= process_cli(s); |
Willy TARREAU | b451247 | 2006-03-01 22:34:48 +0100 | [diff] [blame] | 5168 | //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5169 | fsm_resync |= process_srv(s); |
Willy TARREAU | b451247 | 2006-03-01 22:34:48 +0100 | [diff] [blame] | 5170 | //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5171 | } while (fsm_resync); |
| 5172 | |
| 5173 | if (s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5174 | struct timeval min1, min2; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5175 | s->res_cw = s->res_cr = s->res_sw = s->res_sr = RES_SILENT; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5176 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5177 | tv_min(&min1, &s->crexpire, &s->cwexpire); |
| 5178 | tv_min(&min2, &s->srexpire, &s->swexpire); |
| 5179 | tv_min(&min1, &min1, &s->cnexpire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5180 | tv_min(&t->expire, &min1, &min2); |
| 5181 | |
| 5182 | /* restore t to its place in the task list */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5183 | task_queue(t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5184 | |
Willy TARREAU | 1cec83c | 2006-03-01 22:33:49 +0100 | [diff] [blame] | 5185 | #ifdef DEBUG_FULL |
| 5186 | /* DEBUG code : this should never ever happen, otherwise it indicates |
| 5187 | * that a task still has something to do and will provoke a quick loop. |
| 5188 | */ |
| 5189 | if (tv_remain2(&now, &t->expire) <= 0) |
| 5190 | exit(100); |
| 5191 | #endif |
| 5192 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5193 | return tv_remain2(&now, &t->expire); /* nothing more to do */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5194 | } |
| 5195 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5196 | s->proxy->nbconn--; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5197 | actconn--; |
| 5198 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 5199 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5200 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 5201 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", s->uniq_id, s->proxy->id, (unsigned short)s->cli_fd, (unsigned short)s->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5202 | write(1, trash, len); |
| 5203 | } |
| 5204 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5205 | s->logs.t_close = tv_diff(&s->logs.tv_accept, &now); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 5206 | if (s->rep != NULL) |
| 5207 | s->logs.bytes = s->rep->total; |
| 5208 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5209 | /* let's do a final log if we need it */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 5210 | if (s->logs.logwait && (!(s->proxy->options & PR_O_NULLNOLOG) || s->req->total)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5211 | sess_log(s); |
| 5212 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5213 | /* the task MUST not be in the run queue anymore */ |
| 5214 | task_delete(t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5215 | session_free(s); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5216 | task_free(t); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5217 | return TIME_ETERNITY; /* rest in peace for eternity */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5218 | } |
| 5219 | |
| 5220 | |
| 5221 | |
| 5222 | /* |
| 5223 | * manages a server health-check. Returns |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5224 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5225 | */ |
| 5226 | int process_chk(struct task *t) { |
| 5227 | struct server *s = t->context; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5228 | struct sockaddr_in sa; |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 5229 | int fd; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5230 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5231 | //fprintf(stderr, "process_chk: task=%p\n", t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5232 | |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 5233 | new_chk: |
| 5234 | fd = s->curfd; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5235 | if (fd < 0) { /* no check currently running */ |
| 5236 | //fprintf(stderr, "process_chk: 2\n"); |
| 5237 | if (tv_cmp2_ms(&t->expire, &now) > 0) { /* not good time yet */ |
| 5238 | task_queue(t); /* restore t to its place in the task list */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5239 | return tv_remain2(&now, &t->expire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5240 | } |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 5241 | |
| 5242 | /* we don't send any health-checks when the proxy is stopped or when |
| 5243 | * the server should not be checked. |
| 5244 | */ |
| 5245 | if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) { |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 5246 | while (tv_cmp2_ms(&t->expire, &now) <= 0) |
| 5247 | tv_delayfrom(&t->expire, &t->expire, s->inter); |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 5248 | task_queue(t); /* restore t to its place in the task list */ |
| 5249 | return tv_remain2(&now, &t->expire); |
| 5250 | } |
| 5251 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5252 | /* we'll initiate a new check */ |
| 5253 | s->result = 0; /* no result yet */ |
| 5254 | if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5255 | if ((fd < global.maxsock) && |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5256 | (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) && |
| 5257 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) { |
| 5258 | //fprintf(stderr, "process_chk: 3\n"); |
| 5259 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5260 | /* we'll connect to the check port on the server */ |
| 5261 | sa = s->addr; |
| 5262 | sa.sin_port = htons(s->check_port); |
| 5263 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 5264 | /* allow specific binding : |
| 5265 | * - server-specific at first |
| 5266 | * - proxy-specific next |
| 5267 | */ |
| 5268 | if (s->state & SRV_BIND_SRC) { |
| 5269 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 5270 | if (bind(fd, (struct sockaddr *)&s->source_addr, sizeof(s->source_addr)) == -1) { |
| 5271 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 5272 | s->proxy->id, s->id); |
| 5273 | s->result = -1; |
| 5274 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5275 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 5276 | else if (s->proxy->options & PR_O_BIND_SRC) { |
| 5277 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 5278 | if (bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) { |
| 5279 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", |
| 5280 | s->proxy->id); |
| 5281 | s->result = -1; |
| 5282 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5283 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 5284 | |
| 5285 | if (!s->result) { |
| 5286 | if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) { |
| 5287 | /* OK, connection in progress or established */ |
| 5288 | |
| 5289 | //fprintf(stderr, "process_chk: 4\n"); |
| 5290 | |
| 5291 | s->curfd = fd; /* that's how we know a test is in progress ;-) */ |
| 5292 | fdtab[fd].owner = t; |
| 5293 | fdtab[fd].read = &event_srv_chk_r; |
| 5294 | fdtab[fd].write = &event_srv_chk_w; |
| 5295 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 5296 | FD_SET(fd, StaticWriteEvent); /* for connect status */ |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 5297 | #ifdef DEBUG_FULL |
| 5298 | assert (!FD_ISSET(fd, StaticReadEvent)); |
| 5299 | #endif |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 5300 | fd_insert(fd); |
| 5301 | /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */ |
| 5302 | tv_delayfrom(&t->expire, &now, s->inter); |
| 5303 | task_queue(t); /* restore t to its place in the task list */ |
| 5304 | return tv_remain(&now, &t->expire); |
| 5305 | } |
| 5306 | else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) { |
| 5307 | s->result = -1; /* a real error */ |
| 5308 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5309 | } |
| 5310 | } |
willy tarreau | 08dedbe | 2005-12-18 01:13:48 +0100 | [diff] [blame] | 5311 | close(fd); /* socket creation error */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5312 | } |
| 5313 | |
| 5314 | if (!s->result) { /* nothing done */ |
| 5315 | //fprintf(stderr, "process_chk: 6\n"); |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 5316 | while (tv_cmp2_ms(&t->expire, &now) <= 0) |
| 5317 | tv_delayfrom(&t->expire, &t->expire, s->inter); |
| 5318 | goto new_chk; /* may be we should initialize a new check */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5319 | } |
| 5320 | |
| 5321 | /* here, we have seen a failure */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5322 | if (s->health > s->rise) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5323 | s->health--; /* still good */ |
| 5324 | else { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5325 | s->state &= ~SRV_RUNNING; |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5326 | if (s->health == s->rise) { |
willy tarreau | 62084d4 | 2006-03-24 18:57:41 +0100 | [diff] [blame] | 5327 | recount_servers(s->proxy); |
| 5328 | Warning("%sServer %s/%s DOWN. %d active and %d backup servers left.%s\n", |
| 5329 | s->state & SRV_BACKUP ? "Backup " : "", |
| 5330 | s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck, |
| 5331 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : ""); |
| 5332 | send_log(s->proxy, LOG_ALERT, |
| 5333 | "%sServer %s/%s is DOWN. %d active and %d backup servers left.%s\n", |
| 5334 | s->state & SRV_BACKUP ? "Backup " : "", |
| 5335 | s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck, |
| 5336 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : ""); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5337 | |
willy tarreau | 62084d4 | 2006-03-24 18:57:41 +0100 | [diff] [blame] | 5338 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5339 | Alert("Proxy %s has no server available !\n", s->proxy->id); |
| 5340 | send_log(s->proxy, LOG_EMERG, "Proxy %s has no server available !\n", s->proxy->id); |
| 5341 | } |
| 5342 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5343 | s->health = 0; /* failure */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5344 | } |
| 5345 | |
| 5346 | //fprintf(stderr, "process_chk: 7\n"); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5347 | /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */ |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 5348 | while (tv_cmp2_ms(&t->expire, &now) <= 0) |
| 5349 | tv_delayfrom(&t->expire, &t->expire, s->inter); |
| 5350 | goto new_chk; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5351 | } |
| 5352 | else { |
| 5353 | //fprintf(stderr, "process_chk: 8\n"); |
| 5354 | /* there was a test running */ |
| 5355 | if (s->result > 0) { /* good server detected */ |
| 5356 | //fprintf(stderr, "process_chk: 9\n"); |
| 5357 | s->health++; /* was bad, stays for a while */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5358 | if (s->health >= s->rise) { |
willy tarreau | 06a1205 | 2006-03-30 14:06:51 +0200 | [diff] [blame] | 5359 | s->state |= SRV_RUNNING; |
| 5360 | |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5361 | if (s->health == s->rise) { |
willy tarreau | 62084d4 | 2006-03-24 18:57:41 +0100 | [diff] [blame] | 5362 | recount_servers(s->proxy); |
| 5363 | Warning("%sServer %s/%s UP. %d active and %d backup servers online.%s\n", |
| 5364 | s->state & SRV_BACKUP ? "Backup " : "", |
| 5365 | s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck, |
| 5366 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : ""); |
| 5367 | send_log(s->proxy, LOG_NOTICE, |
| 5368 | "%sServer %s/%s is UP. %d active and %d backup servers online.%s\n", |
| 5369 | s->state & SRV_BACKUP ? "Backup " : "", |
| 5370 | s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck, |
| 5371 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : ""); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5372 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5373 | |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5374 | s->health = s->rise + s->fall - 1; /* OK now */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5375 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5376 | s->curfd = -1; /* no check running anymore */ |
| 5377 | //FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5378 | fd_delete(fd); |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 5379 | while (tv_cmp2_ms(&t->expire, &now) <= 0) |
| 5380 | tv_delayfrom(&t->expire, &t->expire, s->inter); |
| 5381 | goto new_chk; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5382 | } |
| 5383 | else if (s->result < 0 || tv_cmp2_ms(&t->expire, &now) <= 0) { |
| 5384 | //fprintf(stderr, "process_chk: 10\n"); |
| 5385 | /* failure or timeout detected */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5386 | if (s->health > s->rise) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5387 | s->health--; /* still good */ |
| 5388 | else { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5389 | s->state &= ~SRV_RUNNING; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5390 | |
willy tarreau | 62084d4 | 2006-03-24 18:57:41 +0100 | [diff] [blame] | 5391 | if (s->health == s->rise) { |
| 5392 | recount_servers(s->proxy); |
| 5393 | Warning("%sServer %s/%s DOWN. %d active and %d backup servers left.%s\n", |
| 5394 | s->state & SRV_BACKUP ? "Backup " : "", |
| 5395 | s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck, |
| 5396 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : ""); |
| 5397 | send_log(s->proxy, LOG_ALERT, |
| 5398 | "%sServer %s/%s is DOWN. %d active and %d backup servers left.%s\n", |
| 5399 | s->state & SRV_BACKUP ? "Backup " : "", |
| 5400 | s->proxy->id, s->id, s->proxy->srv_act, s->proxy->srv_bck, |
| 5401 | (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : ""); |
| 5402 | |
| 5403 | if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) { |
| 5404 | Alert("Proxy %s has no server available !\n", s->proxy->id); |
| 5405 | send_log(s->proxy, LOG_EMERG, "Proxy %s has no server available !\n", s->proxy->id); |
| 5406 | } |
| 5407 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5408 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5409 | s->health = 0; /* failure */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5410 | } |
| 5411 | s->curfd = -1; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5412 | //FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5413 | fd_delete(fd); |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 5414 | while (tv_cmp2_ms(&t->expire, &now) <= 0) |
| 5415 | tv_delayfrom(&t->expire, &t->expire, s->inter); |
| 5416 | goto new_chk; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5417 | } |
| 5418 | /* if result is 0 and there's no timeout, we have to wait again */ |
| 5419 | } |
| 5420 | //fprintf(stderr, "process_chk: 11\n"); |
| 5421 | s->result = 0; |
| 5422 | task_queue(t); /* restore t to its place in the task list */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5423 | return tv_remain2(&now, &t->expire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5424 | } |
| 5425 | |
| 5426 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5427 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5428 | #if STATTIME > 0 |
| 5429 | int stats(void); |
| 5430 | #endif |
| 5431 | |
| 5432 | /* |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5433 | * This does 4 things : |
| 5434 | * - wake up all expired tasks |
| 5435 | * - call all runnable tasks |
| 5436 | * - call maintain_proxies() to enable/disable the listeners |
| 5437 | * - return the delay till next event in ms, -1 = wait indefinitely |
| 5438 | * Note: this part should be rewritten with the O(ln(n)) scheduler. |
| 5439 | * |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5440 | */ |
| 5441 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5442 | int process_runnable_tasks() { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5443 | int next_time; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5444 | int time2; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5445 | struct task *t, *tnext; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5446 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5447 | next_time = TIME_ETERNITY; /* set the timer to wait eternally first */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5448 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5449 | /* look for expired tasks and add them to the run queue. |
| 5450 | */ |
| 5451 | tnext = ((struct task *)LIST_HEAD(wait_queue))->next; |
| 5452 | while ((t = tnext) != LIST_HEAD(wait_queue)) { /* we haven't looped ? */ |
| 5453 | tnext = t->next; |
| 5454 | if (t->state & TASK_RUNNING) |
| 5455 | continue; |
| 5456 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5457 | if (tv_iseternity(&t->expire)) |
| 5458 | continue; |
| 5459 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5460 | /* wakeup expired entries. It doesn't matter if they are |
| 5461 | * already running because of a previous event |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5462 | */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5463 | if (tv_cmp_ms(&t->expire, &now) <= 0) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5464 | task_wakeup(&rq, t); |
| 5465 | } |
| 5466 | else { |
| 5467 | /* first non-runnable task. Use its expiration date as an upper bound */ |
| 5468 | int temp_time = tv_remain(&now, &t->expire); |
| 5469 | if (temp_time) |
| 5470 | next_time = temp_time; |
| 5471 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5472 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5473 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5474 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5475 | /* process each task in the run queue now. Each task may be deleted |
| 5476 | * since we only use tnext. |
| 5477 | */ |
| 5478 | tnext = rq; |
| 5479 | while ((t = tnext) != NULL) { |
| 5480 | int temp_time; |
| 5481 | |
| 5482 | tnext = t->rqnext; |
| 5483 | task_sleep(&rq, t); |
| 5484 | temp_time = t->process(t); |
| 5485 | next_time = MINTIME(temp_time, next_time); |
| 5486 | } |
| 5487 | |
| 5488 | /* maintain all proxies in a consistent state. This should quickly become a task */ |
| 5489 | time2 = maintain_proxies(); |
| 5490 | return MINTIME(time2, next_time); |
| 5491 | } |
| 5492 | |
| 5493 | |
| 5494 | #if defined(ENABLE_EPOLL) |
| 5495 | |
| 5496 | /* |
| 5497 | * Main epoll() loop. |
| 5498 | */ |
| 5499 | |
| 5500 | /* does 3 actions : |
| 5501 | * 0 (POLL_LOOP_ACTION_INIT) : initializes necessary private structures |
| 5502 | * 1 (POLL_LOOP_ACTION_RUN) : runs the loop |
| 5503 | * 2 (POLL_LOOP_ACTION_CLEAN) : cleans up |
| 5504 | * |
| 5505 | * returns 0 if initialization failed, !0 otherwise. |
| 5506 | */ |
| 5507 | |
| 5508 | int epoll_loop(int action) { |
| 5509 | int next_time; |
| 5510 | int status; |
| 5511 | int fd; |
| 5512 | |
| 5513 | int fds, count; |
| 5514 | int pr, pw, sr, sw; |
| 5515 | unsigned rn, ro, wn, wo; /* read new, read old, write new, write old */ |
| 5516 | struct epoll_event ev; |
| 5517 | |
| 5518 | /* private data */ |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5519 | static struct epoll_event *epoll_events = NULL; |
| 5520 | static int epoll_fd; |
| 5521 | |
| 5522 | if (action == POLL_LOOP_ACTION_INIT) { |
| 5523 | epoll_fd = epoll_create(global.maxsock + 1); |
| 5524 | if (epoll_fd < 0) |
| 5525 | return 0; |
| 5526 | else { |
| 5527 | epoll_events = (struct epoll_event*) |
| 5528 | calloc(1, sizeof(struct epoll_event) * global.maxsock); |
| 5529 | PrevReadEvent = (fd_set *) |
| 5530 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
| 5531 | PrevWriteEvent = (fd_set *) |
| 5532 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5533 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5534 | return 1; |
| 5535 | } |
| 5536 | else if (action == POLL_LOOP_ACTION_CLEAN) { |
| 5537 | if (PrevWriteEvent) free(PrevWriteEvent); |
| 5538 | if (PrevReadEvent) free(PrevReadEvent); |
| 5539 | if (epoll_events) free(epoll_events); |
| 5540 | close(epoll_fd); |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5541 | epoll_fd = 0; |
| 5542 | return 1; |
| 5543 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5544 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5545 | /* OK, it's POLL_LOOP_ACTION_RUN */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5546 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5547 | tv_now(&now); |
| 5548 | |
| 5549 | while (1) { |
| 5550 | next_time = process_runnable_tasks(); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5551 | |
| 5552 | /* stop when there's no connection left and we don't allow them anymore */ |
| 5553 | if (!actconn && listeners == 0) |
| 5554 | break; |
| 5555 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5556 | #if STATTIME > 0 |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5557 | { |
| 5558 | int time2; |
| 5559 | time2 = stats(); |
| 5560 | next_time = MINTIME(time2, next_time); |
| 5561 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5562 | #endif |
| 5563 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5564 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) { |
| 5565 | |
| 5566 | rn = ((int*)StaticReadEvent)[fds]; ro = ((int*)PrevReadEvent)[fds]; |
| 5567 | wn = ((int*)StaticWriteEvent)[fds]; wo = ((int*)PrevWriteEvent)[fds]; |
| 5568 | |
| 5569 | if ((ro^rn) | (wo^wn)) { |
| 5570 | for (count = 0, fd = fds << INTBITS; count < (1<<INTBITS) && fd < maxfd; count++, fd++) { |
| 5571 | #define FDSETS_ARE_INT_ALIGNED |
| 5572 | #ifdef FDSETS_ARE_INT_ALIGNED |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5573 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5574 | #define WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
| 5575 | #ifdef WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5576 | pr = (ro >> count) & 1; |
| 5577 | pw = (wo >> count) & 1; |
| 5578 | sr = (rn >> count) & 1; |
| 5579 | sw = (wn >> count) & 1; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5580 | #else |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5581 | pr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&ro); |
| 5582 | pw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wo); |
| 5583 | sr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&rn); |
| 5584 | sw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wn); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5585 | #endif |
| 5586 | #else |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5587 | pr = FD_ISSET(fd, PrevReadEvent); |
| 5588 | pw = FD_ISSET(fd, PrevWriteEvent); |
| 5589 | sr = FD_ISSET(fd, StaticReadEvent); |
| 5590 | sw = FD_ISSET(fd, StaticWriteEvent); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5591 | #endif |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5592 | if (!((sr^pr) | (sw^pw))) |
| 5593 | continue; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5594 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5595 | ev.events = (sr ? EPOLLIN : 0) | (sw ? EPOLLOUT : 0); |
| 5596 | ev.data.fd = fd; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5597 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5598 | #ifdef EPOLL_CTL_MOD_WORKAROUND |
| 5599 | /* I encountered a rarely reproducible problem with |
| 5600 | * EPOLL_CTL_MOD where a modified FD (systematically |
| 5601 | * the one in epoll_events[0], fd#7) would sometimes |
| 5602 | * be set EPOLL_OUT while asked for a read ! This is |
| 5603 | * with the 2.4 epoll patch. The workaround is to |
| 5604 | * delete then recreate in case of modification. |
| 5605 | * This is in 2.4 up to epoll-lt-0.21 but not in 2.6 |
| 5606 | * nor RHEL kernels. |
| 5607 | */ |
| 5608 | |
| 5609 | if ((pr | pw) && fdtab[fd].state != FD_STCLOSE) |
| 5610 | epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &ev); |
| 5611 | |
| 5612 | if ((sr | sw)) |
| 5613 | epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev); |
| 5614 | #else |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5615 | if ((pr | pw)) { |
| 5616 | /* the file-descriptor already exists... */ |
| 5617 | if ((sr | sw)) { |
| 5618 | /* ...and it will still exist */ |
| 5619 | if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &ev) < 0) { |
| 5620 | // perror("epoll_ctl(MOD)"); |
| 5621 | // exit(1); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5622 | } |
| 5623 | } else { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5624 | /* ...and it will be removed */ |
| 5625 | if (fdtab[fd].state != FD_STCLOSE && |
| 5626 | epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &ev) < 0) { |
| 5627 | // perror("epoll_ctl(DEL)"); |
| 5628 | // exit(1); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5629 | } |
| 5630 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5631 | } else { |
| 5632 | /* the file-descriptor did not exist, let's add it */ |
| 5633 | if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) { |
| 5634 | // perror("epoll_ctl(ADD)"); |
| 5635 | // exit(1); |
| 5636 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5637 | } |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5638 | #endif // EPOLL_CTL_MOD_WORKAROUND |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5639 | } |
| 5640 | ((int*)PrevReadEvent)[fds] = rn; |
| 5641 | ((int*)PrevWriteEvent)[fds] = wn; |
| 5642 | } |
| 5643 | } |
| 5644 | |
| 5645 | /* now let's wait for events */ |
| 5646 | status = epoll_wait(epoll_fd, epoll_events, maxfd, next_time); |
| 5647 | tv_now(&now); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5648 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5649 | for (count = 0; count < status; count++) { |
| 5650 | fd = epoll_events[count].data.fd; |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 5651 | |
| 5652 | if (FD_ISSET(fd, StaticReadEvent)) { |
| 5653 | if (fdtab[fd].state == FD_STCLOSE) |
| 5654 | continue; |
| 5655 | if (epoll_events[count].events & ( EPOLLIN | EPOLLERR | EPOLLHUP )) |
| 5656 | fdtab[fd].read(fd); |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 5657 | } |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 5658 | |
| 5659 | if (FD_ISSET(fd, StaticWriteEvent)) { |
| 5660 | if (fdtab[fd].state == FD_STCLOSE) |
| 5661 | continue; |
| 5662 | if (epoll_events[count].events & ( EPOLLOUT | EPOLLERR | EPOLLHUP )) |
| 5663 | fdtab[fd].write(fd); |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 5664 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5665 | } |
| 5666 | } |
| 5667 | return 1; |
| 5668 | } |
| 5669 | #endif |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5670 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5671 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5672 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5673 | #if defined(ENABLE_POLL) |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5674 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5675 | /* |
| 5676 | * Main poll() loop. |
| 5677 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5678 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5679 | /* does 3 actions : |
| 5680 | * 0 (POLL_LOOP_ACTION_INIT) : initializes necessary private structures |
| 5681 | * 1 (POLL_LOOP_ACTION_RUN) : runs the loop |
| 5682 | * 2 (POLL_LOOP_ACTION_CLEAN) : cleans up |
| 5683 | * |
| 5684 | * returns 0 if initialization failed, !0 otherwise. |
| 5685 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5686 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5687 | int poll_loop(int action) { |
| 5688 | int next_time; |
| 5689 | int status; |
| 5690 | int fd, nbfd; |
| 5691 | |
| 5692 | int fds, count; |
| 5693 | int sr, sw; |
| 5694 | unsigned rn, wn; /* read new, write new */ |
| 5695 | |
| 5696 | /* private data */ |
| 5697 | static struct pollfd *poll_events = NULL; |
| 5698 | |
| 5699 | if (action == POLL_LOOP_ACTION_INIT) { |
| 5700 | poll_events = (struct pollfd*) |
| 5701 | calloc(1, sizeof(struct pollfd) * global.maxsock); |
| 5702 | return 1; |
| 5703 | } |
| 5704 | else if (action == POLL_LOOP_ACTION_CLEAN) { |
| 5705 | if (poll_events) |
| 5706 | free(poll_events); |
| 5707 | return 1; |
| 5708 | } |
| 5709 | |
| 5710 | /* OK, it's POLL_LOOP_ACTION_RUN */ |
| 5711 | |
| 5712 | tv_now(&now); |
| 5713 | |
| 5714 | while (1) { |
| 5715 | next_time = process_runnable_tasks(); |
| 5716 | |
| 5717 | /* stop when there's no connection left and we don't allow them anymore */ |
| 5718 | if (!actconn && listeners == 0) |
| 5719 | break; |
| 5720 | |
| 5721 | #if STATTIME > 0 |
| 5722 | { |
| 5723 | int time2; |
| 5724 | time2 = stats(); |
| 5725 | next_time = MINTIME(time2, next_time); |
| 5726 | } |
| 5727 | #endif |
| 5728 | |
| 5729 | |
| 5730 | nbfd = 0; |
| 5731 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) { |
| 5732 | |
| 5733 | rn = ((int*)StaticReadEvent)[fds]; |
| 5734 | wn = ((int*)StaticWriteEvent)[fds]; |
| 5735 | |
| 5736 | if ((rn|wn)) { |
| 5737 | for (count = 0, fd = fds << INTBITS; count < (1<<INTBITS) && fd < maxfd; count++, fd++) { |
| 5738 | #define FDSETS_ARE_INT_ALIGNED |
| 5739 | #ifdef FDSETS_ARE_INT_ALIGNED |
| 5740 | |
| 5741 | #define WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
| 5742 | #ifdef WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
| 5743 | sr = (rn >> count) & 1; |
| 5744 | sw = (wn >> count) & 1; |
| 5745 | #else |
| 5746 | sr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&rn); |
| 5747 | sw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wn); |
| 5748 | #endif |
| 5749 | #else |
| 5750 | sr = FD_ISSET(fd, StaticReadEvent); |
| 5751 | sw = FD_ISSET(fd, StaticWriteEvent); |
| 5752 | #endif |
| 5753 | if ((sr|sw)) { |
| 5754 | poll_events[nbfd].fd = fd; |
| 5755 | poll_events[nbfd].events = (sr ? POLLIN : 0) | (sw ? POLLOUT : 0); |
| 5756 | nbfd++; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5757 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5758 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5759 | } |
| 5760 | } |
| 5761 | |
| 5762 | /* now let's wait for events */ |
| 5763 | status = poll(poll_events, nbfd, next_time); |
| 5764 | tv_now(&now); |
| 5765 | |
| 5766 | for (count = 0; status > 0 && count < nbfd; count++) { |
| 5767 | fd = poll_events[count].fd; |
| 5768 | |
| 5769 | if (!poll_events[count].revents & ( POLLOUT | POLLIN | POLLERR | POLLHUP )) |
| 5770 | continue; |
| 5771 | |
| 5772 | /* ok, we found one active fd */ |
| 5773 | status--; |
| 5774 | |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 5775 | if (FD_ISSET(fd, StaticReadEvent)) { |
| 5776 | if (fdtab[fd].state == FD_STCLOSE) |
| 5777 | continue; |
| 5778 | if (poll_events[count].revents & ( POLLIN | POLLERR | POLLHUP )) |
| 5779 | fdtab[fd].read(fd); |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 5780 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5781 | |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 5782 | if (FD_ISSET(fd, StaticWriteEvent)) { |
| 5783 | if (fdtab[fd].state == FD_STCLOSE) |
| 5784 | continue; |
| 5785 | if (poll_events[count].revents & ( POLLOUT | POLLERR | POLLHUP )) |
| 5786 | fdtab[fd].write(fd); |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 5787 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5788 | } |
| 5789 | } |
| 5790 | return 1; |
| 5791 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5792 | #endif |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5793 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5794 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5795 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5796 | /* |
| 5797 | * Main select() loop. |
| 5798 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5799 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5800 | /* does 3 actions : |
| 5801 | * 0 (POLL_LOOP_ACTION_INIT) : initializes necessary private structures |
| 5802 | * 1 (POLL_LOOP_ACTION_RUN) : runs the loop |
| 5803 | * 2 (POLL_LOOP_ACTION_CLEAN) : cleans up |
| 5804 | * |
| 5805 | * returns 0 if initialization failed, !0 otherwise. |
| 5806 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5807 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5808 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5809 | int select_loop(int action) { |
| 5810 | int next_time; |
| 5811 | int status; |
| 5812 | int fd,i; |
| 5813 | struct timeval delta; |
| 5814 | int readnotnull, writenotnull; |
| 5815 | static fd_set *ReadEvent = NULL, *WriteEvent = NULL; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5816 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5817 | if (action == POLL_LOOP_ACTION_INIT) { |
| 5818 | ReadEvent = (fd_set *) |
| 5819 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
| 5820 | WriteEvent = (fd_set *) |
| 5821 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
| 5822 | return 1; |
| 5823 | } |
| 5824 | else if (action == POLL_LOOP_ACTION_CLEAN) { |
| 5825 | if (WriteEvent) free(WriteEvent); |
| 5826 | if (ReadEvent) free(ReadEvent); |
| 5827 | return 1; |
| 5828 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5829 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5830 | /* OK, it's POLL_LOOP_ACTION_RUN */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5831 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5832 | tv_now(&now); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5833 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5834 | while (1) { |
| 5835 | next_time = process_runnable_tasks(); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5836 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5837 | /* stop when there's no connection left and we don't allow them anymore */ |
| 5838 | if (!actconn && listeners == 0) |
| 5839 | break; |
| 5840 | |
| 5841 | #if STATTIME > 0 |
| 5842 | { |
| 5843 | int time2; |
| 5844 | time2 = stats(); |
| 5845 | next_time = MINTIME(time2, next_time); |
| 5846 | } |
| 5847 | #endif |
| 5848 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5849 | if (next_time > 0) { /* FIXME */ |
| 5850 | /* Convert to timeval */ |
| 5851 | /* to avoid eventual select loops due to timer precision */ |
| 5852 | next_time += SCHEDULER_RESOLUTION; |
| 5853 | delta.tv_sec = next_time / 1000; |
| 5854 | delta.tv_usec = (next_time % 1000) * 1000; |
| 5855 | } |
| 5856 | else if (next_time == 0) { /* allow select to return immediately when needed */ |
| 5857 | delta.tv_sec = delta.tv_usec = 0; |
| 5858 | } |
| 5859 | |
| 5860 | |
| 5861 | /* let's restore fdset state */ |
| 5862 | |
| 5863 | readnotnull = 0; writenotnull = 0; |
| 5864 | for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) { |
| 5865 | readnotnull |= (*(((int*)ReadEvent)+i) = *(((int*)StaticReadEvent)+i)) != 0; |
| 5866 | writenotnull |= (*(((int*)WriteEvent)+i) = *(((int*)StaticWriteEvent)+i)) != 0; |
| 5867 | } |
| 5868 | |
| 5869 | // /* just a verification code, needs to be removed for performance */ |
| 5870 | // for (i=0; i<maxfd; i++) { |
| 5871 | // if (FD_ISSET(i, ReadEvent) != FD_ISSET(i, StaticReadEvent)) |
| 5872 | // abort(); |
| 5873 | // if (FD_ISSET(i, WriteEvent) != FD_ISSET(i, StaticWriteEvent)) |
| 5874 | // abort(); |
| 5875 | // |
| 5876 | // } |
| 5877 | |
| 5878 | status = select(maxfd, |
| 5879 | readnotnull ? ReadEvent : NULL, |
| 5880 | writenotnull ? WriteEvent : NULL, |
| 5881 | NULL, |
| 5882 | (next_time >= 0) ? &delta : NULL); |
| 5883 | |
| 5884 | /* this is an experiment on the separation of the select work */ |
| 5885 | // status = (readnotnull ? select(maxfd, ReadEvent, NULL, NULL, (next_time >= 0) ? &delta : NULL) : 0); |
| 5886 | // status |= (writenotnull ? select(maxfd, NULL, WriteEvent, NULL, (next_time >= 0) ? &delta : NULL) : 0); |
| 5887 | |
| 5888 | tv_now(&now); |
| 5889 | |
| 5890 | if (status > 0) { /* must proceed with events */ |
| 5891 | |
| 5892 | int fds; |
| 5893 | char count; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5894 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5895 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) |
| 5896 | if ((((int *)(ReadEvent))[fds] | ((int *)(WriteEvent))[fds]) != 0) |
| 5897 | for (count = 1<<INTBITS, fd = fds << INTBITS; count && fd < maxfd; count--, fd++) { |
| 5898 | |
| 5899 | /* if we specify read first, the accepts and zero reads will be |
| 5900 | * seen first. Moreover, system buffers will be flushed faster. |
| 5901 | */ |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 5902 | if (FD_ISSET(fd, ReadEvent)) { |
| 5903 | if (fdtab[fd].state == FD_STCLOSE) |
| 5904 | continue; |
| 5905 | fdtab[fd].read(fd); |
| 5906 | } |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 5907 | |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 5908 | if (FD_ISSET(fd, WriteEvent)) { |
| 5909 | if (fdtab[fd].state == FD_STCLOSE) |
| 5910 | continue; |
| 5911 | fdtab[fd].write(fd); |
| 5912 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5913 | } |
| 5914 | } |
| 5915 | else { |
| 5916 | // fprintf(stderr,"select returned %d, maxfd=%d\n", status, maxfd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5917 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5918 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5919 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5920 | } |
| 5921 | |
| 5922 | |
| 5923 | #if STATTIME > 0 |
| 5924 | /* |
| 5925 | * Display proxy statistics regularly. It is designed to be called from the |
| 5926 | * select_loop(). |
| 5927 | */ |
| 5928 | int stats(void) { |
| 5929 | static int lines; |
| 5930 | static struct timeval nextevt; |
| 5931 | static struct timeval lastevt; |
| 5932 | static struct timeval starttime = {0,0}; |
| 5933 | unsigned long totaltime, deltatime; |
| 5934 | int ret; |
| 5935 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5936 | if (tv_cmp(&now, &nextevt) > 0) { |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 5937 | deltatime = (tv_diff(&lastevt, &now)?:1); |
| 5938 | totaltime = (tv_diff(&starttime, &now)?:1); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5939 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5940 | if (global.mode & MODE_STATS) { |
| 5941 | if ((lines++ % 16 == 0) && !(global.mode & MODE_LOG)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5942 | qfprintf(stderr, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5943 | "\n active total tsknew tskgood tskleft tskrght tsknsch tsklsch tskrsch\n"); |
| 5944 | if (lines>1) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5945 | qfprintf(stderr,"%07d %07d %07d %07d %07d %07d %07d %07d %07d\n", |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5946 | actconn, totalconn, |
| 5947 | stats_tsk_new, stats_tsk_good, |
| 5948 | stats_tsk_left, stats_tsk_right, |
| 5949 | stats_tsk_nsrch, stats_tsk_lsrch, stats_tsk_rsrch); |
| 5950 | } |
| 5951 | } |
| 5952 | |
| 5953 | tv_delayfrom(&nextevt, &now, STATTIME); |
| 5954 | |
| 5955 | lastevt=now; |
| 5956 | } |
| 5957 | ret = tv_remain(&now, &nextevt); |
| 5958 | return ret; |
| 5959 | } |
| 5960 | #endif |
| 5961 | |
| 5962 | |
| 5963 | /* |
| 5964 | * this function enables proxies when there are enough free sessions, |
| 5965 | * or stops them when the table is full. It is designed to be called from the |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5966 | * select_loop(). It returns the time left before next expiration event |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5967 | * during stop time, TIME_ETERNITY otherwise. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5968 | */ |
| 5969 | static int maintain_proxies(void) { |
| 5970 | struct proxy *p; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5971 | struct listener *l; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5972 | int tleft; /* time left */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5973 | |
| 5974 | p = proxy; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5975 | tleft = TIME_ETERNITY; /* infinite time */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5976 | |
| 5977 | /* if there are enough free sessions, we'll activate proxies */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5978 | if (actconn < global.maxconn) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5979 | while (p) { |
| 5980 | if (p->nbconn < p->maxconn) { |
| 5981 | if (p->state == PR_STIDLE) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5982 | for (l = p->listen; l != NULL; l = l->next) { |
| 5983 | FD_SET(l->fd, StaticReadEvent); |
| 5984 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5985 | p->state = PR_STRUN; |
| 5986 | } |
| 5987 | } |
| 5988 | else { |
| 5989 | if (p->state == PR_STRUN) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5990 | for (l = p->listen; l != NULL; l = l->next) { |
| 5991 | FD_CLR(l->fd, StaticReadEvent); |
| 5992 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5993 | p->state = PR_STIDLE; |
| 5994 | } |
| 5995 | } |
| 5996 | p = p->next; |
| 5997 | } |
| 5998 | } |
| 5999 | else { /* block all proxies */ |
| 6000 | while (p) { |
| 6001 | if (p->state == PR_STRUN) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6002 | for (l = p->listen; l != NULL; l = l->next) { |
| 6003 | FD_CLR(l->fd, StaticReadEvent); |
| 6004 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6005 | p->state = PR_STIDLE; |
| 6006 | } |
| 6007 | p = p->next; |
| 6008 | } |
| 6009 | } |
| 6010 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6011 | if (stopping) { |
| 6012 | p = proxy; |
| 6013 | while (p) { |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 6014 | if (p->state != PR_STSTOPPED) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6015 | int t; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6016 | t = tv_remain2(&now, &p->stop_time); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6017 | if (t == 0) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 6018 | Warning("Proxy %s stopped.\n", p->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6019 | send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 6020 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6021 | for (l = p->listen; l != NULL; l = l->next) { |
| 6022 | fd_delete(l->fd); |
| 6023 | listeners--; |
| 6024 | } |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 6025 | p->state = PR_STSTOPPED; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6026 | } |
| 6027 | else { |
| 6028 | tleft = MINTIME(t, tleft); |
| 6029 | } |
| 6030 | } |
| 6031 | p = p->next; |
| 6032 | } |
| 6033 | } |
| 6034 | return tleft; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6035 | } |
| 6036 | |
| 6037 | /* |
| 6038 | * this function disables health-check servers so that the process will quickly be ignored |
willy tarreau | 808b4e6 | 2006-01-20 19:46:44 +0100 | [diff] [blame] | 6039 | * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace |
| 6040 | * time will not be used since it would already not listen anymore to the socket. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6041 | */ |
| 6042 | static void soft_stop(void) { |
| 6043 | struct proxy *p; |
| 6044 | |
| 6045 | stopping = 1; |
| 6046 | p = proxy; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6047 | tv_now(&now); /* else, the old time before select will be used */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6048 | while (p) { |
Willy TARREAU | 2bfdd8e | 2006-03-12 18:03:05 +0100 | [diff] [blame] | 6049 | if (p->state != PR_STSTOPPED) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 6050 | Warning("Stopping proxy %s in %d ms.\n", p->id, p->grace); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6051 | send_log(p, LOG_WARNING, "Stopping proxy %s in %d ms.\n", p->id, p->grace); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6052 | tv_delayfrom(&p->stop_time, &now, p->grace); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 6053 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6054 | p = p->next; |
| 6055 | } |
| 6056 | } |
| 6057 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 6058 | static void pause_proxy(struct proxy *p) { |
| 6059 | struct listener *l; |
| 6060 | for (l = p->listen; l != NULL; l = l->next) { |
| 6061 | shutdown(l->fd, SHUT_RD); |
| 6062 | FD_CLR(l->fd, StaticReadEvent); |
| 6063 | p->state = PR_STPAUSED; |
| 6064 | } |
| 6065 | } |
| 6066 | |
| 6067 | /* |
| 6068 | * This function temporarily disables listening so that another new instance |
| 6069 | * can start listening. It is designed to be called upon reception of a |
willy tarreau | 808b4e6 | 2006-01-20 19:46:44 +0100 | [diff] [blame] | 6070 | * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 6071 | * the proxy, or a SIGTTIN can be sent to listen again. |
| 6072 | */ |
| 6073 | static void pause_proxies(void) { |
| 6074 | struct proxy *p; |
| 6075 | |
| 6076 | p = proxy; |
| 6077 | tv_now(&now); /* else, the old time before select will be used */ |
| 6078 | while (p) { |
| 6079 | if (p->state != PR_STSTOPPED && p->state != PR_STPAUSED) { |
| 6080 | Warning("Pausing proxy %s.\n", p->id); |
| 6081 | send_log(p, LOG_WARNING, "Pausing proxy %s.\n", p->id); |
| 6082 | pause_proxy(p); |
| 6083 | } |
| 6084 | p = p->next; |
| 6085 | } |
| 6086 | } |
| 6087 | |
| 6088 | |
| 6089 | /* |
| 6090 | * This function reactivates listening. This can be used after a call to |
| 6091 | * sig_pause(), for example when a new instance has failed starting up. |
| 6092 | * It is designed to be called upon reception of a SIGTTIN. |
| 6093 | */ |
| 6094 | static void listen_proxies(void) { |
| 6095 | struct proxy *p; |
| 6096 | struct listener *l; |
| 6097 | |
| 6098 | p = proxy; |
| 6099 | tv_now(&now); /* else, the old time before select will be used */ |
| 6100 | while (p) { |
| 6101 | if (p->state == PR_STPAUSED) { |
| 6102 | Warning("Enabling proxy %s.\n", p->id); |
| 6103 | send_log(p, LOG_WARNING, "Enabling proxy %s.\n", p->id); |
| 6104 | |
| 6105 | for (l = p->listen; l != NULL; l = l->next) { |
| 6106 | if (listen(l->fd, p->maxconn) == 0) { |
| 6107 | if (actconn < global.maxconn && p->nbconn < p->maxconn) { |
| 6108 | FD_SET(l->fd, StaticReadEvent); |
| 6109 | p->state = PR_STRUN; |
| 6110 | } |
| 6111 | else |
| 6112 | p->state = PR_STIDLE; |
| 6113 | } else { |
willy tarreau | cb2e562 | 2006-01-29 21:55:30 +0100 | [diff] [blame] | 6114 | int port; |
| 6115 | |
| 6116 | if (l->addr.ss_family == AF_INET6) |
| 6117 | port = ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port); |
| 6118 | else |
| 6119 | port = ntohs(((struct sockaddr_in *)(&l->addr))->sin_port); |
| 6120 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 6121 | Warning("Port %d busy while trying to enable proxy %s.\n", |
willy tarreau | cb2e562 | 2006-01-29 21:55:30 +0100 | [diff] [blame] | 6122 | port, p->id); |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 6123 | send_log(p, LOG_WARNING, "Port %d busy while trying to enable proxy %s.\n", |
willy tarreau | cb2e562 | 2006-01-29 21:55:30 +0100 | [diff] [blame] | 6124 | port, p->id); |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 6125 | /* Another port might have been enabled. Let's stop everything. */ |
| 6126 | pause_proxy(p); |
| 6127 | break; |
| 6128 | } |
| 6129 | } |
| 6130 | } |
| 6131 | p = p->next; |
| 6132 | } |
| 6133 | } |
| 6134 | |
| 6135 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6136 | /* |
| 6137 | * upon SIGUSR1, let's have a soft stop. |
| 6138 | */ |
| 6139 | void sig_soft_stop(int sig) { |
| 6140 | soft_stop(); |
| 6141 | signal(sig, SIG_IGN); |
| 6142 | } |
| 6143 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 6144 | /* |
| 6145 | * upon SIGTTOU, we pause everything |
| 6146 | */ |
| 6147 | void sig_pause(int sig) { |
| 6148 | pause_proxies(); |
| 6149 | signal(sig, sig_pause); |
| 6150 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6151 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6152 | /* |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 6153 | * upon SIGTTIN, let's have a soft stop. |
| 6154 | */ |
| 6155 | void sig_listen(int sig) { |
| 6156 | listen_proxies(); |
| 6157 | signal(sig, sig_listen); |
| 6158 | } |
| 6159 | |
| 6160 | /* |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6161 | * this function dumps every server's state when the process receives SIGHUP. |
| 6162 | */ |
| 6163 | void sig_dump_state(int sig) { |
| 6164 | struct proxy *p = proxy; |
| 6165 | |
| 6166 | Warning("SIGHUP received, dumping servers states.\n"); |
| 6167 | while (p) { |
| 6168 | struct server *s = p->srv; |
| 6169 | |
| 6170 | send_log(p, LOG_NOTICE, "SIGUP received, dumping servers states.\n"); |
| 6171 | while (s) { |
| 6172 | if (s->state & SRV_RUNNING) { |
willy tarreau | fd6dfe7 | 2006-03-19 19:38:19 +0100 | [diff] [blame] | 6173 | Warning("SIGHUP: Server %s/%s is UP.\n", p->id, s->id); |
| 6174 | send_log(p, LOG_NOTICE, "SIGUP: Server %s/%s is UP.\n", p->id, s->id); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6175 | } |
| 6176 | else { |
willy tarreau | fd6dfe7 | 2006-03-19 19:38:19 +0100 | [diff] [blame] | 6177 | Warning("SIGHUP: Server %s/%s is DOWN.\n", p->id, s->id); |
| 6178 | send_log(p, LOG_NOTICE, "SIGHUP: Server %s/%s is DOWN.\n", p->id, s->id); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6179 | } |
| 6180 | s = s->next; |
| 6181 | } |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 6182 | |
willy tarreau | 62084d4 | 2006-03-24 18:57:41 +0100 | [diff] [blame] | 6183 | if (p->srv_act == 0) { |
| 6184 | if (p->srv_bck) { |
| 6185 | Warning("SIGHUP: Proxy %s is running on backup servers !\n", p->id); |
| 6186 | send_log(p, LOG_NOTICE, "SIGHUP: Proxy %s is running on backup servers !\n", p->id); |
| 6187 | } else { |
| 6188 | Warning("SIGHUP: Proxy %s has no server available !\n", p->id); |
| 6189 | send_log(p, LOG_NOTICE, "SIGHUP: Proxy %s has no server available !\n", p->id); |
| 6190 | } |
| 6191 | } |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 6192 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6193 | p = p->next; |
| 6194 | } |
| 6195 | signal(sig, sig_dump_state); |
| 6196 | } |
| 6197 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6198 | void dump(int sig) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6199 | struct task *t, *tnext; |
| 6200 | struct session *s; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6201 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6202 | tnext = ((struct task *)LIST_HEAD(wait_queue))->next; |
| 6203 | while ((t = tnext) != LIST_HEAD(wait_queue)) { /* we haven't looped ? */ |
| 6204 | tnext = t->next; |
| 6205 | s = t->context; |
| 6206 | qfprintf(stderr,"[dump] wq: task %p, still %ld ms, " |
| 6207 | "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, " |
| 6208 | "req=%d, rep=%d, clifd=%d\n", |
| 6209 | s, tv_remain(&now, &t->expire), |
| 6210 | s->cli_state, |
| 6211 | s->srv_state, |
| 6212 | FD_ISSET(s->cli_fd, StaticReadEvent), |
| 6213 | FD_ISSET(s->cli_fd, StaticWriteEvent), |
| 6214 | FD_ISSET(s->srv_fd, StaticReadEvent), |
| 6215 | FD_ISSET(s->srv_fd, StaticWriteEvent), |
| 6216 | s->req->l, s->rep?s->rep->l:0, s->cli_fd |
| 6217 | ); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6218 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6219 | } |
| 6220 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 6221 | #ifdef DEBUG_MEMORY |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6222 | static void fast_stop(void) |
| 6223 | { |
| 6224 | struct proxy *p; |
| 6225 | p = proxy; |
| 6226 | while (p) { |
| 6227 | p->grace = 0; |
| 6228 | p = p->next; |
| 6229 | } |
| 6230 | soft_stop(); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6231 | } |
| 6232 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6233 | void sig_int(int sig) { |
| 6234 | /* This would normally be a hard stop, |
| 6235 | but we want to be sure about deallocation, |
| 6236 | and so on, so we do a soft stop with |
| 6237 | 0 GRACE time |
| 6238 | */ |
| 6239 | fast_stop(); |
| 6240 | /* If we are killed twice, we decide to die*/ |
| 6241 | signal(sig, SIG_DFL); |
| 6242 | } |
| 6243 | |
| 6244 | void sig_term(int sig) { |
| 6245 | /* This would normally be a hard stop, |
| 6246 | but we want to be sure about deallocation, |
| 6247 | and so on, so we do a soft stop with |
| 6248 | 0 GRACE time |
| 6249 | */ |
| 6250 | fast_stop(); |
| 6251 | /* If we are killed twice, we decide to die*/ |
| 6252 | signal(sig, SIG_DFL); |
| 6253 | } |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 6254 | #endif |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6255 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 6256 | /* returns the pointer to an error in the replacement string, or NULL if OK */ |
| 6257 | char *chain_regex(struct hdr_exp **head, regex_t *preg, int action, char *replace) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 6258 | struct hdr_exp *exp; |
| 6259 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 6260 | if (replace != NULL) { |
| 6261 | char *err; |
| 6262 | err = check_replace_string(replace); |
| 6263 | if (err) |
| 6264 | return err; |
| 6265 | } |
| 6266 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 6267 | while (*head != NULL) |
| 6268 | head = &(*head)->next; |
| 6269 | |
| 6270 | exp = calloc(1, sizeof(struct hdr_exp)); |
| 6271 | |
| 6272 | exp->preg = preg; |
| 6273 | exp->replace = replace; |
| 6274 | exp->action = action; |
| 6275 | *head = exp; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 6276 | |
| 6277 | return NULL; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 6278 | } |
| 6279 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6280 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6281 | /* |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6282 | * parse a line in a <global> section. Returns 0 if OK, -1 if error. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6283 | */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6284 | int cfg_parse_global(char *file, int linenum, char **args) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6285 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6286 | if (!strcmp(args[0], "global")) { /* new section */ |
| 6287 | /* no option, nothing special to do */ |
| 6288 | return 0; |
| 6289 | } |
| 6290 | else if (!strcmp(args[0], "daemon")) { |
| 6291 | global.mode |= MODE_DAEMON; |
| 6292 | } |
| 6293 | else if (!strcmp(args[0], "debug")) { |
| 6294 | global.mode |= MODE_DEBUG; |
| 6295 | } |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 6296 | else if (!strcmp(args[0], "noepoll")) { |
| 6297 | cfg_polling_mechanism &= ~POLL_USE_EPOLL; |
| 6298 | } |
| 6299 | else if (!strcmp(args[0], "nopoll")) { |
| 6300 | cfg_polling_mechanism &= ~POLL_USE_POLL; |
| 6301 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6302 | else if (!strcmp(args[0], "quiet")) { |
| 6303 | global.mode |= MODE_QUIET; |
| 6304 | } |
| 6305 | else if (!strcmp(args[0], "stats")) { |
| 6306 | global.mode |= MODE_STATS; |
| 6307 | } |
| 6308 | else if (!strcmp(args[0], "uid")) { |
| 6309 | if (global.uid != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6310 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6311 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6312 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6313 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6314 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6315 | return -1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6316 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6317 | global.uid = atol(args[1]); |
| 6318 | } |
| 6319 | else if (!strcmp(args[0], "gid")) { |
| 6320 | if (global.gid != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6321 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6322 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6323 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6324 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6325 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6326 | return -1; |
| 6327 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6328 | global.gid = atol(args[1]); |
| 6329 | } |
| 6330 | else if (!strcmp(args[0], "nbproc")) { |
| 6331 | if (global.nbproc != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6332 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6333 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6334 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6335 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6336 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6337 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6338 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6339 | global.nbproc = atol(args[1]); |
| 6340 | } |
| 6341 | else if (!strcmp(args[0], "maxconn")) { |
| 6342 | if (global.maxconn != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6343 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6344 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6345 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6346 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6347 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6348 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6349 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6350 | global.maxconn = atol(args[1]); |
Willy TARREAU | 13032e7 | 2006-03-12 17:31:45 +0100 | [diff] [blame] | 6351 | #ifdef SYSTEM_MAXCONN |
| 6352 | if (global.maxconn > DEFAULT_MAXCONN && cfg_maxconn <= DEFAULT_MAXCONN) { |
| 6353 | Alert("parsing [%s:%d] : maxconn value %d too high for this system.\nLimiting to %d. Please use '-n' to force the value.\n", file, linenum, global.maxconn, DEFAULT_MAXCONN); |
| 6354 | global.maxconn = DEFAULT_MAXCONN; |
| 6355 | } |
| 6356 | #endif /* SYSTEM_MAXCONN */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6357 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 6358 | else if (!strcmp(args[0], "ulimit-n")) { |
| 6359 | if (global.rlimit_nofile != 0) { |
| 6360 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
| 6361 | return 0; |
| 6362 | } |
| 6363 | if (*(args[1]) == 0) { |
| 6364 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
| 6365 | return -1; |
| 6366 | } |
| 6367 | global.rlimit_nofile = atol(args[1]); |
| 6368 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6369 | else if (!strcmp(args[0], "chroot")) { |
| 6370 | if (global.chroot != NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6371 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6372 | return 0; |
| 6373 | } |
| 6374 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6375 | Alert("parsing [%s:%d] : '%s' expects a directory as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6376 | return -1; |
| 6377 | } |
| 6378 | global.chroot = strdup(args[1]); |
| 6379 | } |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 6380 | else if (!strcmp(args[0], "pidfile")) { |
| 6381 | if (global.pidfile != NULL) { |
| 6382 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
| 6383 | return 0; |
| 6384 | } |
| 6385 | if (*(args[1]) == 0) { |
| 6386 | Alert("parsing [%s:%d] : '%s' expects a file name as an argument.\n", file, linenum, args[0]); |
| 6387 | return -1; |
| 6388 | } |
| 6389 | global.pidfile = strdup(args[1]); |
| 6390 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6391 | else if (!strcmp(args[0], "log")) { /* syslog server address */ |
| 6392 | struct sockaddr_in *sa; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6393 | int facility, level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6394 | |
| 6395 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6396 | Alert("parsing [%s:%d] : '%s' expects <address> and <facility> as arguments.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6397 | return -1; |
| 6398 | } |
| 6399 | |
| 6400 | for (facility = 0; facility < NB_LOG_FACILITIES; facility++) |
| 6401 | if (!strcmp(log_facilities[facility], args[2])) |
| 6402 | break; |
| 6403 | |
| 6404 | if (facility >= NB_LOG_FACILITIES) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6405 | Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6406 | exit(1); |
| 6407 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6408 | |
| 6409 | level = 7; /* max syslog level = debug */ |
| 6410 | if (*(args[3])) { |
| 6411 | while (level >= 0 && strcmp(log_levels[level], args[3])) |
| 6412 | level--; |
| 6413 | if (level < 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6414 | Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6415 | exit(1); |
| 6416 | } |
| 6417 | } |
| 6418 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6419 | sa = str2sa(args[1]); |
| 6420 | if (!sa->sin_port) |
| 6421 | sa->sin_port = htons(SYSLOG_PORT); |
| 6422 | |
| 6423 | if (global.logfac1 == -1) { |
| 6424 | global.logsrv1 = *sa; |
| 6425 | global.logfac1 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6426 | global.loglev1 = level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6427 | } |
| 6428 | else if (global.logfac2 == -1) { |
| 6429 | global.logsrv2 = *sa; |
| 6430 | global.logfac2 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6431 | global.loglev2 = level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6432 | } |
| 6433 | else { |
| 6434 | Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum); |
| 6435 | return -1; |
| 6436 | } |
| 6437 | |
| 6438 | } |
| 6439 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6440 | Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "global"); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6441 | return -1; |
| 6442 | } |
| 6443 | return 0; |
| 6444 | } |
| 6445 | |
| 6446 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6447 | void init_default_instance() { |
| 6448 | memset(&defproxy, 0, sizeof(defproxy)); |
| 6449 | defproxy.mode = PR_MODE_TCP; |
| 6450 | defproxy.state = PR_STNEW; |
| 6451 | defproxy.maxconn = cfg_maxpconn; |
| 6452 | defproxy.conn_retries = CONN_RETRIES; |
| 6453 | defproxy.logfac1 = defproxy.logfac2 = -1; /* log disabled */ |
| 6454 | } |
| 6455 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6456 | /* |
| 6457 | * parse a line in a <listen> section. Returns 0 if OK, -1 if error. |
| 6458 | */ |
| 6459 | int cfg_parse_listen(char *file, int linenum, char **args) { |
| 6460 | static struct proxy *curproxy = NULL; |
| 6461 | struct server *newsrv = NULL; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 6462 | char *err; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6463 | int rc; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6464 | |
| 6465 | if (!strcmp(args[0], "listen")) { /* new proxy */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6466 | if (!*args[1]) { |
| 6467 | Alert("parsing [%s:%d] : '%s' expects an <id> argument and\n" |
| 6468 | " optionnally supports [addr1]:port1[-end1]{,[addr]:port[-end]}...\n", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6469 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6470 | return -1; |
| 6471 | } |
| 6472 | |
| 6473 | if ((curproxy = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6474 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6475 | return -1; |
| 6476 | } |
| 6477 | curproxy->next = proxy; |
| 6478 | proxy = curproxy; |
| 6479 | curproxy->id = strdup(args[1]); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 6480 | |
| 6481 | /* parse the listener address if any */ |
| 6482 | if (*args[2]) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6483 | curproxy->listen = str2listener(args[2], curproxy->listen); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 6484 | if (!curproxy->listen) |
| 6485 | return -1; |
Willy TARREAU | 203b0b6 | 2006-03-12 18:00:28 +0100 | [diff] [blame] | 6486 | global.maxsock++; |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 6487 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6488 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6489 | /* set default values */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6490 | curproxy->state = defproxy.state; |
| 6491 | curproxy->maxconn = defproxy.maxconn; |
| 6492 | curproxy->conn_retries = defproxy.conn_retries; |
| 6493 | curproxy->options = defproxy.options; |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6494 | |
| 6495 | if (defproxy.check_req) |
| 6496 | curproxy->check_req = strdup(defproxy.check_req); |
| 6497 | curproxy->check_len = defproxy.check_len; |
| 6498 | |
| 6499 | if (defproxy.cookie_name) |
| 6500 | curproxy->cookie_name = strdup(defproxy.cookie_name); |
| 6501 | curproxy->cookie_len = defproxy.cookie_len; |
| 6502 | |
| 6503 | if (defproxy.capture_name) |
| 6504 | curproxy->capture_name = strdup(defproxy.capture_name); |
| 6505 | curproxy->capture_namelen = defproxy.capture_namelen; |
| 6506 | curproxy->capture_len = defproxy.capture_len; |
| 6507 | |
| 6508 | if (defproxy.errmsg.msg400) |
| 6509 | curproxy->errmsg.msg400 = strdup(defproxy.errmsg.msg400); |
| 6510 | curproxy->errmsg.len400 = defproxy.errmsg.len400; |
| 6511 | |
| 6512 | if (defproxy.errmsg.msg403) |
| 6513 | curproxy->errmsg.msg403 = strdup(defproxy.errmsg.msg403); |
| 6514 | curproxy->errmsg.len403 = defproxy.errmsg.len403; |
| 6515 | |
| 6516 | if (defproxy.errmsg.msg408) |
| 6517 | curproxy->errmsg.msg408 = strdup(defproxy.errmsg.msg408); |
| 6518 | curproxy->errmsg.len408 = defproxy.errmsg.len408; |
| 6519 | |
| 6520 | if (defproxy.errmsg.msg500) |
| 6521 | curproxy->errmsg.msg500 = strdup(defproxy.errmsg.msg500); |
| 6522 | curproxy->errmsg.len500 = defproxy.errmsg.len500; |
| 6523 | |
| 6524 | if (defproxy.errmsg.msg502) |
| 6525 | curproxy->errmsg.msg502 = strdup(defproxy.errmsg.msg502); |
| 6526 | curproxy->errmsg.len502 = defproxy.errmsg.len502; |
| 6527 | |
| 6528 | if (defproxy.errmsg.msg503) |
| 6529 | curproxy->errmsg.msg503 = strdup(defproxy.errmsg.msg503); |
| 6530 | curproxy->errmsg.len503 = defproxy.errmsg.len503; |
| 6531 | |
| 6532 | if (defproxy.errmsg.msg504) |
| 6533 | curproxy->errmsg.msg504 = strdup(defproxy.errmsg.msg504); |
| 6534 | curproxy->errmsg.len504 = defproxy.errmsg.len504; |
| 6535 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6536 | curproxy->clitimeout = defproxy.clitimeout; |
| 6537 | curproxy->contimeout = defproxy.contimeout; |
| 6538 | curproxy->srvtimeout = defproxy.srvtimeout; |
| 6539 | curproxy->mode = defproxy.mode; |
| 6540 | curproxy->logfac1 = defproxy.logfac1; |
| 6541 | curproxy->logsrv1 = defproxy.logsrv1; |
| 6542 | curproxy->loglev1 = defproxy.loglev1; |
| 6543 | curproxy->logfac2 = defproxy.logfac2; |
| 6544 | curproxy->logsrv2 = defproxy.logsrv2; |
| 6545 | curproxy->loglev2 = defproxy.loglev2; |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 6546 | curproxy->to_log = defproxy.to_log & ~LW_COOKIE & ~LW_REQHDR & ~ LW_RSPHDR; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6547 | curproxy->grace = defproxy.grace; |
| 6548 | curproxy->source_addr = defproxy.source_addr; |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 6549 | curproxy->mon_net = defproxy.mon_net; |
| 6550 | curproxy->mon_mask = defproxy.mon_mask; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6551 | return 0; |
| 6552 | } |
| 6553 | else if (!strcmp(args[0], "defaults")) { /* use this one to assign default values */ |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6554 | /* some variables may have already been initialized earlier */ |
| 6555 | if (defproxy.check_req) free(defproxy.check_req); |
| 6556 | if (defproxy.cookie_name) free(defproxy.cookie_name); |
| 6557 | if (defproxy.capture_name) free(defproxy.capture_name); |
| 6558 | if (defproxy.errmsg.msg400) free(defproxy.errmsg.msg400); |
| 6559 | if (defproxy.errmsg.msg403) free(defproxy.errmsg.msg403); |
| 6560 | if (defproxy.errmsg.msg408) free(defproxy.errmsg.msg408); |
| 6561 | if (defproxy.errmsg.msg500) free(defproxy.errmsg.msg500); |
| 6562 | if (defproxy.errmsg.msg502) free(defproxy.errmsg.msg502); |
| 6563 | if (defproxy.errmsg.msg503) free(defproxy.errmsg.msg503); |
| 6564 | if (defproxy.errmsg.msg504) free(defproxy.errmsg.msg504); |
| 6565 | |
| 6566 | init_default_instance(); |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6567 | curproxy = &defproxy; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6568 | return 0; |
| 6569 | } |
| 6570 | else if (curproxy == NULL) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6571 | Alert("parsing [%s:%d] : 'listen' or 'defaults' expected.\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6572 | return -1; |
| 6573 | } |
| 6574 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6575 | if (!strcmp(args[0], "bind")) { /* new listen addresses */ |
| 6576 | if (curproxy == &defproxy) { |
| 6577 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6578 | return -1; |
| 6579 | } |
| 6580 | |
| 6581 | if (strchr(args[1], ':') == NULL) { |
| 6582 | Alert("parsing [%s:%d] : '%s' expects [addr1]:port1[-end1]{,[addr]:port[-end]}... as arguments.\n", |
| 6583 | file, linenum, args[0]); |
| 6584 | return -1; |
| 6585 | } |
| 6586 | curproxy->listen = str2listener(args[1], curproxy->listen); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 6587 | if (!curproxy->listen) |
| 6588 | return -1; |
Willy TARREAU | 203b0b6 | 2006-03-12 18:00:28 +0100 | [diff] [blame] | 6589 | global.maxsock++; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6590 | return 0; |
| 6591 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 6592 | else if (!strcmp(args[0], "monitor-net")) { /* set the range of IPs to ignore */ |
| 6593 | if (!*args[1] || !str2net(args[1], &curproxy->mon_net, &curproxy->mon_mask)) { |
| 6594 | Alert("parsing [%s:%d] : '%s' expects address[/mask].\n", |
| 6595 | file, linenum, args[0]); |
| 6596 | return -1; |
| 6597 | } |
| 6598 | /* flush useless bits */ |
| 6599 | curproxy->mon_net.s_addr &= curproxy->mon_mask.s_addr; |
| 6600 | return 0; |
| 6601 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6602 | else if (!strcmp(args[0], "mode")) { /* sets the proxy mode */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6603 | if (!strcmp(args[1], "http")) curproxy->mode = PR_MODE_HTTP; |
| 6604 | else if (!strcmp(args[1], "tcp")) curproxy->mode = PR_MODE_TCP; |
| 6605 | else if (!strcmp(args[1], "health")) curproxy->mode = PR_MODE_HEALTH; |
| 6606 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6607 | Alert("parsing [%s:%d] : unknown proxy mode '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6608 | return -1; |
| 6609 | } |
| 6610 | } |
| 6611 | else if (!strcmp(args[0], "disabled")) { /* disables this proxy */ |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 6612 | curproxy->state = PR_STSTOPPED; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6613 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6614 | else if (!strcmp(args[0], "enabled")) { /* enables this proxy (used to revert a disabled default) */ |
| 6615 | curproxy->state = PR_STNEW; |
| 6616 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6617 | else if (!strcmp(args[0], "cookie")) { /* cookie name */ |
| 6618 | int cur_arg; |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6619 | // if (curproxy == &defproxy) { |
| 6620 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6621 | // return -1; |
| 6622 | // } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6623 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6624 | if (curproxy->cookie_name != NULL) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6625 | // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n", |
| 6626 | // file, linenum); |
| 6627 | // return 0; |
| 6628 | free(curproxy->cookie_name); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6629 | } |
| 6630 | |
| 6631 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6632 | Alert("parsing [%s:%d] : '%s' expects <cookie_name> as argument.\n", |
| 6633 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6634 | return -1; |
| 6635 | } |
| 6636 | curproxy->cookie_name = strdup(args[1]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6637 | curproxy->cookie_len = strlen(curproxy->cookie_name); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6638 | |
| 6639 | cur_arg = 2; |
| 6640 | while (*(args[cur_arg])) { |
| 6641 | if (!strcmp(args[cur_arg], "rewrite")) { |
| 6642 | curproxy->options |= PR_O_COOK_RW; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6643 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6644 | else if (!strcmp(args[cur_arg], "indirect")) { |
| 6645 | curproxy->options |= PR_O_COOK_IND; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6646 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6647 | else if (!strcmp(args[cur_arg], "insert")) { |
| 6648 | curproxy->options |= PR_O_COOK_INS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6649 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 6650 | else if (!strcmp(args[cur_arg], "nocache")) { |
| 6651 | curproxy->options |= PR_O_COOK_NOC; |
| 6652 | } |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 6653 | else if (!strcmp(args[cur_arg], "postonly")) { |
| 6654 | curproxy->options |= PR_O_COOK_POST; |
| 6655 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6656 | else if (!strcmp(args[cur_arg], "prefix")) { |
| 6657 | curproxy->options |= PR_O_COOK_PFX; |
| 6658 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6659 | else { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6660 | Alert("parsing [%s:%d] : '%s' supports 'rewrite', 'insert', 'prefix', 'indirect', 'nocache' and 'postonly' options.\n", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6661 | file, linenum, args[0]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6662 | return -1; |
| 6663 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6664 | cur_arg++; |
| 6665 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6666 | if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_IND))) { |
| 6667 | Alert("parsing [%s:%d] : cookie 'rewrite' and 'indirect' modes are incompatible.\n", |
| 6668 | file, linenum); |
| 6669 | return -1; |
| 6670 | } |
| 6671 | |
| 6672 | if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_INS|PR_O_COOK_PFX))) { |
| 6673 | Alert("parsing [%s:%d] : cookie 'rewrite', 'insert' and 'prefix' modes are incompatible.\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6674 | file, linenum); |
| 6675 | return -1; |
| 6676 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6677 | }/* end else if (!strcmp(args[0], "cookie")) */ |
| 6678 | else if (!strcmp(args[0], "appsession")) { /* cookie name */ |
| 6679 | // if (curproxy == &defproxy) { |
| 6680 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6681 | // return -1; |
| 6682 | // } |
| 6683 | |
| 6684 | if (curproxy->appsession_name != NULL) { |
| 6685 | // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n", |
| 6686 | // file, linenum); |
| 6687 | // return 0; |
| 6688 | free(curproxy->appsession_name); |
| 6689 | } |
| 6690 | |
| 6691 | if (*(args[5]) == 0) { |
| 6692 | Alert("parsing [%s:%d] : '%s' expects 'appsession' <cookie_name> 'len' <len> 'timeout' <timeout>.\n", |
| 6693 | file, linenum, args[0]); |
| 6694 | return -1; |
| 6695 | } |
| 6696 | have_appsession = 1; |
| 6697 | curproxy->appsession_name = strdup(args[1]); |
| 6698 | curproxy->appsession_name_len = strlen(curproxy->appsession_name); |
| 6699 | curproxy->appsession_len = atoi(args[3]); |
| 6700 | curproxy->appsession_timeout = atoi(args[5]); |
| 6701 | rc = chtbl_init(&(curproxy->htbl_proxy), TBLSIZ, hashpjw, match_str, destroy); |
| 6702 | if (rc) { |
| 6703 | Alert("Error Init Appsession Hashtable.\n"); |
| 6704 | return -1; |
| 6705 | } |
| 6706 | } /* Url App Session */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 6707 | else if (!strcmp(args[0], "capture")) { |
| 6708 | if (!strcmp(args[1], "cookie")) { /* name of a cookie to capture */ |
| 6709 | // if (curproxy == &defproxy) { |
| 6710 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6711 | // return -1; |
| 6712 | // } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6713 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 6714 | if (curproxy->capture_name != NULL) { |
| 6715 | // Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", |
| 6716 | // file, linenum, args[0]); |
| 6717 | // return 0; |
| 6718 | free(curproxy->capture_name); |
| 6719 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6720 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 6721 | if (*(args[4]) == 0) { |
| 6722 | Alert("parsing [%s:%d] : '%s' expects 'cookie' <cookie_name> 'len' <len>.\n", |
| 6723 | file, linenum, args[0]); |
| 6724 | return -1; |
| 6725 | } |
| 6726 | curproxy->capture_name = strdup(args[2]); |
| 6727 | curproxy->capture_namelen = strlen(curproxy->capture_name); |
| 6728 | curproxy->capture_len = atol(args[4]); |
| 6729 | if (curproxy->capture_len >= CAPTURE_LEN) { |
| 6730 | Warning("parsing [%s:%d] : truncating capture length to %d bytes.\n", |
| 6731 | file, linenum, CAPTURE_LEN - 1); |
| 6732 | curproxy->capture_len = CAPTURE_LEN - 1; |
| 6733 | } |
| 6734 | curproxy->to_log |= LW_COOKIE; |
| 6735 | } |
| 6736 | else if (!strcmp(args[1], "request") && !strcmp(args[2], "header")) { |
| 6737 | struct cap_hdr *hdr; |
| 6738 | |
| 6739 | if (curproxy == &defproxy) { |
| 6740 | Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]); |
| 6741 | return -1; |
| 6742 | } |
| 6743 | |
| 6744 | if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) { |
| 6745 | Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n", |
| 6746 | file, linenum, args[0], args[1]); |
| 6747 | return -1; |
| 6748 | } |
| 6749 | |
| 6750 | hdr = calloc(sizeof(struct cap_hdr), 1); |
| 6751 | hdr->next = curproxy->req_cap; |
| 6752 | hdr->name = strdup(args[3]); |
| 6753 | hdr->namelen = strlen(args[3]); |
| 6754 | hdr->len = atol(args[5]); |
| 6755 | hdr->index = curproxy->nb_req_cap++; |
| 6756 | curproxy->req_cap = hdr; |
| 6757 | curproxy->to_log |= LW_REQHDR; |
| 6758 | } |
| 6759 | else if (!strcmp(args[1], "response") && !strcmp(args[2], "header")) { |
| 6760 | struct cap_hdr *hdr; |
| 6761 | |
| 6762 | if (curproxy == &defproxy) { |
| 6763 | Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]); |
| 6764 | return -1; |
| 6765 | } |
| 6766 | |
| 6767 | if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) { |
| 6768 | Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n", |
| 6769 | file, linenum, args[0], args[1]); |
| 6770 | return -1; |
| 6771 | } |
| 6772 | hdr = calloc(sizeof(struct cap_hdr), 1); |
| 6773 | hdr->next = curproxy->rsp_cap; |
| 6774 | hdr->name = strdup(args[3]); |
| 6775 | hdr->namelen = strlen(args[3]); |
| 6776 | hdr->len = atol(args[5]); |
| 6777 | hdr->index = curproxy->nb_rsp_cap++; |
| 6778 | curproxy->rsp_cap = hdr; |
| 6779 | curproxy->to_log |= LW_RSPHDR; |
| 6780 | } |
| 6781 | else { |
| 6782 | Alert("parsing [%s:%d] : '%s' expects 'cookie' or 'request header' or 'response header'.\n", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6783 | file, linenum, args[0]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6784 | return -1; |
| 6785 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6786 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6787 | else if (!strcmp(args[0], "contimeout")) { /* connect timeout */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6788 | if (curproxy->contimeout != defproxy.contimeout) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6789 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6790 | return 0; |
| 6791 | } |
| 6792 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6793 | Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n", |
| 6794 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6795 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6796 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6797 | curproxy->contimeout = atol(args[1]); |
| 6798 | } |
| 6799 | else if (!strcmp(args[0], "clitimeout")) { /* client timeout */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6800 | if (curproxy->clitimeout != defproxy.clitimeout) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6801 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", |
| 6802 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6803 | return 0; |
| 6804 | } |
| 6805 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6806 | Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n", |
| 6807 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6808 | return -1; |
| 6809 | } |
| 6810 | curproxy->clitimeout = atol(args[1]); |
| 6811 | } |
| 6812 | else if (!strcmp(args[0], "srvtimeout")) { /* server timeout */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6813 | if (curproxy->srvtimeout != defproxy.srvtimeout) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6814 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6815 | return 0; |
| 6816 | } |
| 6817 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6818 | Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n", |
| 6819 | file, linenum, args[0]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6820 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6821 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6822 | curproxy->srvtimeout = atol(args[1]); |
| 6823 | } |
| 6824 | else if (!strcmp(args[0], "retries")) { /* connection retries */ |
| 6825 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6826 | Alert("parsing [%s:%d] : '%s' expects an integer argument (dispatch counts for one).\n", |
| 6827 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6828 | return -1; |
| 6829 | } |
| 6830 | curproxy->conn_retries = atol(args[1]); |
| 6831 | } |
| 6832 | else if (!strcmp(args[0], "option")) { |
| 6833 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6834 | Alert("parsing [%s:%d] : '%s' expects an option name.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6835 | return -1; |
| 6836 | } |
| 6837 | if (!strcmp(args[1], "redispatch")) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6838 | /* enable reconnections to dispatch */ |
| 6839 | curproxy->options |= PR_O_REDISP; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 6840 | #ifdef TPROXY |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6841 | else if (!strcmp(args[1], "transparent")) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6842 | /* enable transparent proxy connections */ |
| 6843 | curproxy->options |= PR_O_TRANSP; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6844 | #endif |
| 6845 | else if (!strcmp(args[1], "keepalive")) |
| 6846 | /* enable keep-alive */ |
| 6847 | curproxy->options |= PR_O_KEEPALIVE; |
| 6848 | else if (!strcmp(args[1], "forwardfor")) |
| 6849 | /* insert x-forwarded-for field */ |
| 6850 | curproxy->options |= PR_O_FWDFOR; |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 6851 | else if (!strcmp(args[1], "logasap")) |
| 6852 | /* log as soon as possible, without waiting for the session to complete */ |
| 6853 | curproxy->options |= PR_O_LOGASAP; |
| 6854 | else if (!strcmp(args[1], "httpclose")) |
| 6855 | /* force connection: close in both directions in HTTP mode */ |
| 6856 | curproxy->options |= PR_O_HTTP_CLOSE; |
Willy TARREAU | 767ba71 | 2006-03-01 22:40:50 +0100 | [diff] [blame] | 6857 | else if (!strcmp(args[1], "forceclose")) |
| 6858 | /* force connection: close in both directions in HTTP mode and enforce end of session */ |
| 6859 | curproxy->options |= PR_O_FORCE_CLO | PR_O_HTTP_CLOSE; |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 6860 | else if (!strcmp(args[1], "checkcache")) |
| 6861 | /* require examination of cacheability of the 'set-cookie' field */ |
| 6862 | curproxy->options |= PR_O_CHK_CACHE; |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 6863 | else if (!strcmp(args[1], "httplog")) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6864 | /* generate a complete HTTP log */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 6865 | curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_REQ | LW_PXID | LW_RESP | LW_BYTES; |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 6866 | else if (!strcmp(args[1], "tcplog")) |
| 6867 | /* generate a detailed TCP log */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 6868 | curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_PXID | LW_BYTES; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 6869 | else if (!strcmp(args[1], "dontlognull")) { |
| 6870 | /* don't log empty requests */ |
| 6871 | curproxy->options |= PR_O_NULLNOLOG; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6872 | } |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6873 | else if (!strcmp(args[1], "tcpka")) { |
| 6874 | /* enable TCP keep-alives on client and server sessions */ |
| 6875 | curproxy->options |= PR_O_TCP_CLI_KA | PR_O_TCP_SRV_KA; |
| 6876 | } |
| 6877 | else if (!strcmp(args[1], "clitcpka")) { |
| 6878 | /* enable TCP keep-alives on client sessions */ |
| 6879 | curproxy->options |= PR_O_TCP_CLI_KA; |
| 6880 | } |
| 6881 | else if (!strcmp(args[1], "srvtcpka")) { |
| 6882 | /* enable TCP keep-alives on server sessions */ |
| 6883 | curproxy->options |= PR_O_TCP_SRV_KA; |
| 6884 | } |
Willy TARREAU | 3481c46 | 2006-03-01 22:37:57 +0100 | [diff] [blame] | 6885 | else if (!strcmp(args[1], "allbackups")) { |
| 6886 | /* Use all backup servers simultaneously */ |
| 6887 | curproxy->options |= PR_O_USE_ALL_BK; |
| 6888 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 6889 | else if (!strcmp(args[1], "httpchk")) { |
| 6890 | /* use HTTP request to check servers' health */ |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6891 | if (curproxy->check_req != NULL) { |
| 6892 | free(curproxy->check_req); |
| 6893 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 6894 | curproxy->options |= PR_O_HTTP_CHK; |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6895 | if (!*args[2]) { /* no argument */ |
| 6896 | curproxy->check_req = strdup(DEF_CHECK_REQ); /* default request */ |
| 6897 | curproxy->check_len = strlen(DEF_CHECK_REQ); |
| 6898 | } else if (!*args[3]) { /* one argument : URI */ |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 6899 | int reqlen = strlen(args[2]) + strlen("OPTIONS / HTTP/1.0\r\n\r\n"); |
| 6900 | curproxy->check_req = (char *)malloc(reqlen); |
| 6901 | curproxy->check_len = snprintf(curproxy->check_req, reqlen, |
| 6902 | "OPTIONS %s HTTP/1.0\r\n\r\n", args[2]); /* URI to use */ |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6903 | } else { /* more arguments : METHOD URI [HTTP_VER] */ |
| 6904 | int reqlen = strlen(args[2]) + strlen(args[3]) + 3 + strlen("\r\n\r\n"); |
| 6905 | if (*args[4]) |
| 6906 | reqlen += strlen(args[4]); |
| 6907 | else |
| 6908 | reqlen += strlen("HTTP/1.0"); |
| 6909 | |
| 6910 | curproxy->check_req = (char *)malloc(reqlen); |
| 6911 | curproxy->check_len = snprintf(curproxy->check_req, reqlen, |
| 6912 | "%s %s %s\r\n\r\n", args[2], args[3], *args[4]?args[4]:"HTTP/1.0"); |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 6913 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 6914 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6915 | else if (!strcmp(args[1], "persist")) { |
| 6916 | /* persist on using the server specified by the cookie, even when it's down */ |
| 6917 | curproxy->options |= PR_O_PERSIST; |
| 6918 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6919 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6920 | Alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6921 | return -1; |
| 6922 | } |
| 6923 | return 0; |
| 6924 | } |
| 6925 | else if (!strcmp(args[0], "redispatch") || !strcmp(args[0], "redisp")) { |
| 6926 | /* enable reconnections to dispatch */ |
| 6927 | curproxy->options |= PR_O_REDISP; |
| 6928 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 6929 | #ifdef TPROXY |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6930 | else if (!strcmp(args[0], "transparent")) { |
| 6931 | /* enable transparent proxy connections */ |
| 6932 | curproxy->options |= PR_O_TRANSP; |
| 6933 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6934 | #endif |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6935 | else if (!strcmp(args[0], "maxconn")) { /* maxconn */ |
| 6936 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6937 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6938 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6939 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6940 | curproxy->maxconn = atol(args[1]); |
| 6941 | } |
| 6942 | else if (!strcmp(args[0], "grace")) { /* grace time (ms) */ |
| 6943 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6944 | Alert("parsing [%s:%d] : '%s' expects a time in milliseconds.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6945 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6946 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6947 | curproxy->grace = atol(args[1]); |
| 6948 | } |
| 6949 | else if (!strcmp(args[0], "dispatch")) { /* dispatch address */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6950 | if (curproxy == &defproxy) { |
| 6951 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6952 | return -1; |
| 6953 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6954 | if (strchr(args[1], ':') == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6955 | Alert("parsing [%s:%d] : '%s' expects <addr:port> as argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6956 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6957 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6958 | curproxy->dispatch_addr = *str2sa(args[1]); |
| 6959 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6960 | else if (!strcmp(args[0], "balance")) { /* set balancing with optional algorithm */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6961 | if (*(args[1])) { |
| 6962 | if (!strcmp(args[1], "roundrobin")) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6963 | curproxy->options |= PR_O_BALANCE_RR; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6964 | } |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 6965 | else if (!strcmp(args[1], "source")) { |
| 6966 | curproxy->options |= PR_O_BALANCE_SH; |
| 6967 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6968 | else { |
willy tarreau | 1a3442d | 2006-03-24 21:03:20 +0100 | [diff] [blame] | 6969 | Alert("parsing [%s:%d] : '%s' only supports 'roundrobin' and 'source' options.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6970 | return -1; |
| 6971 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6972 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6973 | else /* if no option is set, use round-robin by default */ |
| 6974 | curproxy->options |= PR_O_BALANCE_RR; |
| 6975 | } |
| 6976 | else if (!strcmp(args[0], "server")) { /* server address */ |
| 6977 | int cur_arg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6978 | char *rport; |
| 6979 | char *raddr; |
| 6980 | short realport; |
| 6981 | int do_check; |
| 6982 | |
| 6983 | if (curproxy == &defproxy) { |
| 6984 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6985 | return -1; |
| 6986 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6987 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6988 | if (!*args[2]) { |
| 6989 | Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6990 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6991 | return -1; |
| 6992 | } |
| 6993 | if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) { |
| 6994 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 6995 | return -1; |
| 6996 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6997 | |
| 6998 | if (curproxy->srv == NULL) |
| 6999 | curproxy->srv = newsrv; |
| 7000 | else |
| 7001 | curproxy->cursrv->next = newsrv; |
| 7002 | curproxy->cursrv = newsrv; |
| 7003 | |
| 7004 | newsrv->next = NULL; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7005 | newsrv->proxy = curproxy; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7006 | |
| 7007 | do_check = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7008 | newsrv->state = SRV_RUNNING; /* early server setup */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7009 | newsrv->id = strdup(args[1]); |
| 7010 | |
| 7011 | /* several ways to check the port component : |
| 7012 | * - IP => port=+0, relative |
| 7013 | * - IP: => port=+0, relative |
| 7014 | * - IP:N => port=N, absolute |
| 7015 | * - IP:+N => port=+N, relative |
| 7016 | * - IP:-N => port=-N, relative |
| 7017 | */ |
| 7018 | raddr = strdup(args[2]); |
| 7019 | rport = strchr(raddr, ':'); |
| 7020 | if (rport) { |
| 7021 | *rport++ = 0; |
| 7022 | realport = atol(rport); |
| 7023 | if (!isdigit((int)*rport)) |
| 7024 | newsrv->state |= SRV_MAPPORTS; |
| 7025 | } else { |
| 7026 | realport = 0; |
| 7027 | newsrv->state |= SRV_MAPPORTS; |
| 7028 | } |
| 7029 | |
| 7030 | newsrv->addr = *str2sa(raddr); |
| 7031 | newsrv->addr.sin_port = htons(realport); |
| 7032 | free(raddr); |
| 7033 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7034 | newsrv->curfd = -1; /* no health-check in progress */ |
| 7035 | newsrv->inter = DEF_CHKINTR; |
| 7036 | newsrv->rise = DEF_RISETIME; |
| 7037 | newsrv->fall = DEF_FALLTIME; |
| 7038 | newsrv->health = newsrv->rise; /* up, but will fall down at first failure */ |
| 7039 | cur_arg = 3; |
| 7040 | while (*args[cur_arg]) { |
| 7041 | if (!strcmp(args[cur_arg], "cookie")) { |
| 7042 | newsrv->cookie = strdup(args[cur_arg + 1]); |
| 7043 | newsrv->cklen = strlen(args[cur_arg + 1]); |
| 7044 | cur_arg += 2; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7045 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7046 | else if (!strcmp(args[cur_arg], "rise")) { |
| 7047 | newsrv->rise = atol(args[cur_arg + 1]); |
| 7048 | newsrv->health = newsrv->rise; |
| 7049 | cur_arg += 2; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7050 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7051 | else if (!strcmp(args[cur_arg], "fall")) { |
| 7052 | newsrv->fall = atol(args[cur_arg + 1]); |
| 7053 | cur_arg += 2; |
| 7054 | } |
| 7055 | else if (!strcmp(args[cur_arg], "inter")) { |
| 7056 | newsrv->inter = atol(args[cur_arg + 1]); |
| 7057 | cur_arg += 2; |
| 7058 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7059 | else if (!strcmp(args[cur_arg], "port")) { |
| 7060 | newsrv->check_port = atol(args[cur_arg + 1]); |
| 7061 | cur_arg += 2; |
| 7062 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7063 | else if (!strcmp(args[cur_arg], "backup")) { |
| 7064 | newsrv->state |= SRV_BACKUP; |
| 7065 | cur_arg ++; |
| 7066 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7067 | else if (!strcmp(args[cur_arg], "check")) { |
Willy TARREAU | 203b0b6 | 2006-03-12 18:00:28 +0100 | [diff] [blame] | 7068 | global.maxsock++; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7069 | do_check = 1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7070 | cur_arg += 1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7071 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 7072 | else if (!strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */ |
| 7073 | if (!*args[cur_arg + 1]) { |
| 7074 | Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n", |
| 7075 | file, linenum, "source"); |
| 7076 | return -1; |
| 7077 | } |
| 7078 | newsrv->state |= SRV_BIND_SRC; |
| 7079 | newsrv->source_addr = *str2sa(args[cur_arg + 1]); |
| 7080 | cur_arg += 2; |
| 7081 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7082 | else { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 7083 | Alert("parsing [%s:%d] : server %s only supports options 'backup', 'cookie', 'check', 'inter', 'rise', 'fall', 'port' and 'source'.\n", |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7084 | file, linenum, newsrv->id); |
| 7085 | return -1; |
| 7086 | } |
| 7087 | } |
| 7088 | |
| 7089 | if (do_check) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7090 | if (!newsrv->check_port && !(newsrv->state & SRV_MAPPORTS)) |
| 7091 | newsrv->check_port = realport; /* by default */ |
| 7092 | if (!newsrv->check_port) { |
| 7093 | Alert("parsing [%s:%d] : server %s has neither service port nor check port. Check has been disabled.\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7094 | file, linenum, newsrv->id); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7095 | return -1; |
| 7096 | } |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 7097 | newsrv->state |= SRV_CHECKED; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7098 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7099 | |
willy tarreau | 62084d4 | 2006-03-24 18:57:41 +0100 | [diff] [blame] | 7100 | if (newsrv->state & SRV_BACKUP) |
| 7101 | curproxy->srv_bck++; |
| 7102 | else |
| 7103 | curproxy->srv_act++; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7104 | } |
| 7105 | else if (!strcmp(args[0], "log")) { /* syslog server address */ |
| 7106 | struct sockaddr_in *sa; |
| 7107 | int facility; |
| 7108 | |
| 7109 | if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) { |
| 7110 | curproxy->logfac1 = global.logfac1; |
| 7111 | curproxy->logsrv1 = global.logsrv1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7112 | curproxy->loglev1 = global.loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7113 | curproxy->logfac2 = global.logfac2; |
| 7114 | curproxy->logsrv2 = global.logsrv2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7115 | curproxy->loglev2 = global.loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7116 | } |
| 7117 | else if (*(args[1]) && *(args[2])) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7118 | int level; |
| 7119 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7120 | for (facility = 0; facility < NB_LOG_FACILITIES; facility++) |
| 7121 | if (!strcmp(log_facilities[facility], args[2])) |
| 7122 | break; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7123 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7124 | if (facility >= NB_LOG_FACILITIES) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7125 | Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7126 | exit(1); |
| 7127 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7128 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7129 | level = 7; /* max syslog level = debug */ |
| 7130 | if (*(args[3])) { |
| 7131 | while (level >= 0 && strcmp(log_levels[level], args[3])) |
| 7132 | level--; |
| 7133 | if (level < 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7134 | Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7135 | exit(1); |
| 7136 | } |
| 7137 | } |
| 7138 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7139 | sa = str2sa(args[1]); |
| 7140 | if (!sa->sin_port) |
| 7141 | sa->sin_port = htons(SYSLOG_PORT); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7142 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7143 | if (curproxy->logfac1 == -1) { |
| 7144 | curproxy->logsrv1 = *sa; |
| 7145 | curproxy->logfac1 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7146 | curproxy->loglev1 = level; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7147 | } |
| 7148 | else if (curproxy->logfac2 == -1) { |
| 7149 | curproxy->logsrv2 = *sa; |
| 7150 | curproxy->logfac2 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7151 | curproxy->loglev2 = level; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7152 | } |
| 7153 | else { |
| 7154 | Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7155 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7156 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7157 | } |
| 7158 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7159 | Alert("parsing [%s:%d] : 'log' expects either <address[:port]> and <facility> or 'global' as arguments.\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7160 | file, linenum); |
| 7161 | return -1; |
| 7162 | } |
| 7163 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7164 | else if (!strcmp(args[0], "source")) { /* address to which we bind when connecting */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7165 | if (!*args[1]) { |
| 7166 | Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7167 | file, linenum, "source"); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7168 | return -1; |
| 7169 | } |
| 7170 | |
| 7171 | curproxy->source_addr = *str2sa(args[1]); |
| 7172 | curproxy->options |= PR_O_BIND_SRC; |
| 7173 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7174 | else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */ |
| 7175 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7176 | if (curproxy == &defproxy) { |
| 7177 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7178 | return -1; |
| 7179 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7180 | |
| 7181 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7182 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 7183 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7184 | return -1; |
| 7185 | } |
| 7186 | |
| 7187 | preg = calloc(1, sizeof(regex_t)); |
| 7188 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7189 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7190 | return -1; |
| 7191 | } |
| 7192 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7193 | err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 7194 | if (err) { |
| 7195 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7196 | file, linenum, *err); |
| 7197 | return -1; |
| 7198 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7199 | } |
| 7200 | else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */ |
| 7201 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7202 | if (curproxy == &defproxy) { |
| 7203 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7204 | return -1; |
| 7205 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7206 | |
| 7207 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7208 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7209 | return -1; |
| 7210 | } |
| 7211 | |
| 7212 | preg = calloc(1, sizeof(regex_t)); |
| 7213 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7214 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7215 | return -1; |
| 7216 | } |
| 7217 | |
| 7218 | chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL); |
| 7219 | } |
| 7220 | else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */ |
| 7221 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7222 | if (curproxy == &defproxy) { |
| 7223 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7224 | return -1; |
| 7225 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7226 | |
| 7227 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7228 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7229 | return -1; |
| 7230 | } |
| 7231 | |
| 7232 | preg = calloc(1, sizeof(regex_t)); |
| 7233 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7234 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7235 | return -1; |
| 7236 | } |
| 7237 | |
| 7238 | chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL); |
| 7239 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7240 | else if (!strcmp(args[0], "reqpass")) { /* pass this header without allowing or denying the request */ |
| 7241 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7242 | if (curproxy == &defproxy) { |
| 7243 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7244 | return -1; |
| 7245 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7246 | |
| 7247 | if (*(args[1]) == 0) { |
| 7248 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
| 7249 | return -1; |
| 7250 | } |
| 7251 | |
| 7252 | preg = calloc(1, sizeof(regex_t)); |
| 7253 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 7254 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7255 | return -1; |
| 7256 | } |
| 7257 | |
| 7258 | chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL); |
| 7259 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7260 | else if (!strcmp(args[0], "reqallow")) { /* allow a request if a header matches this regex */ |
| 7261 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7262 | if (curproxy == &defproxy) { |
| 7263 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7264 | return -1; |
| 7265 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7266 | |
| 7267 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7268 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7269 | return -1; |
| 7270 | } |
| 7271 | |
| 7272 | preg = calloc(1, sizeof(regex_t)); |
| 7273 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7274 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7275 | return -1; |
| 7276 | } |
| 7277 | |
| 7278 | chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL); |
| 7279 | } |
| 7280 | else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */ |
| 7281 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7282 | if (curproxy == &defproxy) { |
| 7283 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7284 | return -1; |
| 7285 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7286 | |
| 7287 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7288 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 7289 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7290 | return -1; |
| 7291 | } |
| 7292 | |
| 7293 | preg = calloc(1, sizeof(regex_t)); |
| 7294 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7295 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7296 | return -1; |
| 7297 | } |
| 7298 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7299 | err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 7300 | if (err) { |
| 7301 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7302 | file, linenum, *err); |
| 7303 | return -1; |
| 7304 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7305 | } |
| 7306 | else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */ |
| 7307 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7308 | if (curproxy == &defproxy) { |
| 7309 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7310 | return -1; |
| 7311 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7312 | |
| 7313 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7314 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7315 | return -1; |
| 7316 | } |
| 7317 | |
| 7318 | preg = calloc(1, sizeof(regex_t)); |
| 7319 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7320 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7321 | return -1; |
| 7322 | } |
| 7323 | |
| 7324 | chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL); |
| 7325 | } |
| 7326 | else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */ |
| 7327 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7328 | if (curproxy == &defproxy) { |
| 7329 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7330 | return -1; |
| 7331 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7332 | |
| 7333 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7334 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7335 | return -1; |
| 7336 | } |
| 7337 | |
| 7338 | preg = calloc(1, sizeof(regex_t)); |
| 7339 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7340 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7341 | return -1; |
| 7342 | } |
| 7343 | |
| 7344 | chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL); |
| 7345 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7346 | else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */ |
| 7347 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7348 | if (curproxy == &defproxy) { |
| 7349 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7350 | return -1; |
| 7351 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7352 | |
| 7353 | if (*(args[1]) == 0) { |
| 7354 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
| 7355 | return -1; |
| 7356 | } |
| 7357 | |
| 7358 | preg = calloc(1, sizeof(regex_t)); |
| 7359 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 7360 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7361 | return -1; |
| 7362 | } |
| 7363 | |
| 7364 | chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL); |
| 7365 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7366 | else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */ |
| 7367 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7368 | if (curproxy == &defproxy) { |
| 7369 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7370 | return -1; |
| 7371 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7372 | |
| 7373 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7374 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7375 | return -1; |
| 7376 | } |
| 7377 | |
| 7378 | preg = calloc(1, sizeof(regex_t)); |
| 7379 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7380 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7381 | return -1; |
| 7382 | } |
| 7383 | |
| 7384 | chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL); |
| 7385 | } |
| 7386 | else if (!strcmp(args[0], "reqadd")) { /* add request header */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7387 | if (curproxy == &defproxy) { |
| 7388 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7389 | return -1; |
| 7390 | } |
| 7391 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7392 | if (curproxy->nb_reqadd >= MAX_NEWHDR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7393 | Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7394 | return 0; |
| 7395 | } |
| 7396 | |
| 7397 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7398 | Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7399 | return -1; |
| 7400 | } |
| 7401 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7402 | curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]); |
| 7403 | } |
| 7404 | else if (!strcmp(args[0], "srvexp") || !strcmp(args[0], "rsprep")) { /* replace response header from a regex */ |
| 7405 | regex_t *preg; |
| 7406 | |
| 7407 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 7408 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 7409 | file, linenum, args[0]); |
| 7410 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7411 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7412 | |
| 7413 | preg = calloc(1, sizeof(regex_t)); |
| 7414 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 7415 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7416 | return -1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7417 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7418 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7419 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 7420 | if (err) { |
| 7421 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7422 | file, linenum, *err); |
| 7423 | return -1; |
| 7424 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7425 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7426 | else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */ |
| 7427 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7428 | if (curproxy == &defproxy) { |
| 7429 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7430 | return -1; |
| 7431 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7432 | |
| 7433 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7434 | Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7435 | return -1; |
| 7436 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7437 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7438 | preg = calloc(1, sizeof(regex_t)); |
| 7439 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7440 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7441 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7442 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7443 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7444 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2])); |
| 7445 | if (err) { |
| 7446 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7447 | file, linenum, *err); |
| 7448 | return -1; |
| 7449 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7450 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7451 | else if (!strcmp(args[0], "rspdeny")) { /* block response header from a regex */ |
| 7452 | regex_t *preg; |
| 7453 | if (curproxy == &defproxy) { |
| 7454 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7455 | return -1; |
| 7456 | } |
| 7457 | |
| 7458 | if (*(args[1]) == 0) { |
| 7459 | Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]); |
| 7460 | return -1; |
| 7461 | } |
| 7462 | |
| 7463 | preg = calloc(1, sizeof(regex_t)); |
| 7464 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 7465 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7466 | return -1; |
| 7467 | } |
| 7468 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7469 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2])); |
| 7470 | if (err) { |
| 7471 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7472 | file, linenum, *err); |
| 7473 | return -1; |
| 7474 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7475 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7476 | else if (!strcmp(args[0], "rspirep")) { /* replace response header from a regex ignoring case */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7477 | regex_t *preg; |
| 7478 | if (curproxy == &defproxy) { |
| 7479 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7480 | return -1; |
| 7481 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7482 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7483 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 7484 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 7485 | file, linenum, args[0]); |
| 7486 | return -1; |
| 7487 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7488 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7489 | preg = calloc(1, sizeof(regex_t)); |
| 7490 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 7491 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7492 | return -1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7493 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7494 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7495 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 7496 | if (err) { |
| 7497 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7498 | file, linenum, *err); |
| 7499 | return -1; |
| 7500 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7501 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7502 | else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */ |
| 7503 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7504 | if (curproxy == &defproxy) { |
| 7505 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7506 | return -1; |
| 7507 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7508 | |
| 7509 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7510 | Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7511 | return -1; |
| 7512 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7513 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7514 | preg = calloc(1, sizeof(regex_t)); |
| 7515 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7516 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7517 | return -1; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7518 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7519 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7520 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2])); |
| 7521 | if (err) { |
| 7522 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7523 | file, linenum, *err); |
| 7524 | return -1; |
| 7525 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7526 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7527 | else if (!strcmp(args[0], "rspideny")) { /* block response header from a regex ignoring case */ |
| 7528 | regex_t *preg; |
| 7529 | if (curproxy == &defproxy) { |
| 7530 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7531 | return -1; |
| 7532 | } |
| 7533 | |
| 7534 | if (*(args[1]) == 0) { |
| 7535 | Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]); |
| 7536 | return -1; |
| 7537 | } |
| 7538 | |
| 7539 | preg = calloc(1, sizeof(regex_t)); |
| 7540 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 7541 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7542 | return -1; |
| 7543 | } |
| 7544 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7545 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2])); |
| 7546 | if (err) { |
| 7547 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7548 | file, linenum, *err); |
| 7549 | return -1; |
| 7550 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7551 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7552 | else if (!strcmp(args[0], "rspadd")) { /* add response header */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7553 | if (curproxy == &defproxy) { |
| 7554 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7555 | return -1; |
| 7556 | } |
| 7557 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7558 | if (curproxy->nb_rspadd >= MAX_NEWHDR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7559 | Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7560 | return 0; |
| 7561 | } |
| 7562 | |
| 7563 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7564 | Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7565 | return -1; |
| 7566 | } |
| 7567 | |
| 7568 | curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]); |
| 7569 | } |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7570 | else if (!strcmp(args[0], "errorloc") || |
| 7571 | !strcmp(args[0], "errorloc302") || |
| 7572 | !strcmp(args[0], "errorloc303")) { /* error location */ |
| 7573 | int errnum, errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7574 | char *err; |
| 7575 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7576 | // if (curproxy == &defproxy) { |
| 7577 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7578 | // return -1; |
| 7579 | // } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7580 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7581 | if (*(args[2]) == 0) { |
| 7582 | Alert("parsing [%s:%d] : <errorloc> expects <error> and <url> as arguments.\n", file, linenum); |
| 7583 | return -1; |
| 7584 | } |
| 7585 | |
| 7586 | errnum = atol(args[1]); |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7587 | if (!strcmp(args[0], "errorloc303")) { |
| 7588 | err = malloc(strlen(HTTP_303) + strlen(args[2]) + 5); |
| 7589 | errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_303, args[2]); |
| 7590 | } else { |
| 7591 | err = malloc(strlen(HTTP_302) + strlen(args[2]) + 5); |
| 7592 | errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_302, args[2]); |
| 7593 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7594 | |
| 7595 | if (errnum == 400) { |
| 7596 | if (curproxy->errmsg.msg400) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7597 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7598 | free(curproxy->errmsg.msg400); |
| 7599 | } |
| 7600 | curproxy->errmsg.msg400 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7601 | curproxy->errmsg.len400 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7602 | } |
| 7603 | else if (errnum == 403) { |
| 7604 | if (curproxy->errmsg.msg403) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7605 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7606 | free(curproxy->errmsg.msg403); |
| 7607 | } |
| 7608 | curproxy->errmsg.msg403 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7609 | curproxy->errmsg.len403 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7610 | } |
| 7611 | else if (errnum == 408) { |
| 7612 | if (curproxy->errmsg.msg408) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7613 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7614 | free(curproxy->errmsg.msg408); |
| 7615 | } |
| 7616 | curproxy->errmsg.msg408 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7617 | curproxy->errmsg.len408 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7618 | } |
| 7619 | else if (errnum == 500) { |
| 7620 | if (curproxy->errmsg.msg500) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7621 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7622 | free(curproxy->errmsg.msg500); |
| 7623 | } |
| 7624 | curproxy->errmsg.msg500 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7625 | curproxy->errmsg.len500 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7626 | } |
| 7627 | else if (errnum == 502) { |
| 7628 | if (curproxy->errmsg.msg502) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7629 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7630 | free(curproxy->errmsg.msg502); |
| 7631 | } |
| 7632 | curproxy->errmsg.msg502 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7633 | curproxy->errmsg.len502 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7634 | } |
| 7635 | else if (errnum == 503) { |
| 7636 | if (curproxy->errmsg.msg503) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7637 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7638 | free(curproxy->errmsg.msg503); |
| 7639 | } |
| 7640 | curproxy->errmsg.msg503 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7641 | curproxy->errmsg.len503 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7642 | } |
| 7643 | else if (errnum == 504) { |
| 7644 | if (curproxy->errmsg.msg504) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7645 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7646 | free(curproxy->errmsg.msg504); |
| 7647 | } |
| 7648 | curproxy->errmsg.msg504 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7649 | curproxy->errmsg.len504 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7650 | } |
| 7651 | else { |
| 7652 | Warning("parsing [%s:%d] : error %d relocation will be ignored.\n", file, linenum, errnum); |
| 7653 | free(err); |
| 7654 | } |
| 7655 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7656 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7657 | Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "listen"); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7658 | return -1; |
| 7659 | } |
| 7660 | return 0; |
| 7661 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7662 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7663 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7664 | /* |
| 7665 | * This function reads and parses the configuration file given in the argument. |
| 7666 | * returns 0 if OK, -1 if error. |
| 7667 | */ |
| 7668 | int readcfgfile(char *file) { |
| 7669 | char thisline[256]; |
| 7670 | char *line; |
| 7671 | FILE *f; |
| 7672 | int linenum = 0; |
| 7673 | char *end; |
| 7674 | char *args[MAX_LINE_ARGS]; |
| 7675 | int arg; |
| 7676 | int cfgerr = 0; |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 7677 | int nbchk, mininter; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7678 | int confsect = CFG_NONE; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7679 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7680 | struct proxy *curproxy = NULL; |
| 7681 | struct server *newsrv = NULL; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7682 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7683 | if ((f=fopen(file,"r")) == NULL) |
| 7684 | return -1; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7685 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7686 | init_default_instance(); |
| 7687 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7688 | while (fgets(line = thisline, sizeof(thisline), f) != NULL) { |
| 7689 | linenum++; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7690 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7691 | end = line + strlen(line); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7692 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7693 | /* skip leading spaces */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 7694 | while (isspace((int)*line)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7695 | line++; |
| 7696 | |
| 7697 | arg = 0; |
| 7698 | args[arg] = line; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7699 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7700 | while (*line && arg < MAX_LINE_ARGS) { |
| 7701 | /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their |
| 7702 | * C equivalent value. Other combinations left unchanged (eg: \1). |
| 7703 | */ |
| 7704 | if (*line == '\\') { |
| 7705 | int skip = 0; |
| 7706 | if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') { |
| 7707 | *line = line[1]; |
| 7708 | skip = 1; |
| 7709 | } |
| 7710 | else if (line[1] == 'r') { |
| 7711 | *line = '\r'; |
| 7712 | skip = 1; |
| 7713 | } |
| 7714 | else if (line[1] == 'n') { |
| 7715 | *line = '\n'; |
| 7716 | skip = 1; |
| 7717 | } |
| 7718 | else if (line[1] == 't') { |
| 7719 | *line = '\t'; |
| 7720 | skip = 1; |
| 7721 | } |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7722 | else if (line[1] == 'x') { |
| 7723 | if ((line + 3 < end ) && ishex(line[2]) && ishex(line[3])) { |
| 7724 | unsigned char hex1, hex2; |
| 7725 | hex1 = toupper(line[2]) - '0'; |
| 7726 | hex2 = toupper(line[3]) - '0'; |
| 7727 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 7728 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 7729 | *line = (hex1<<4) + hex2; |
| 7730 | skip = 3; |
| 7731 | } |
| 7732 | else { |
| 7733 | Alert("parsing [%s:%d] : invalid or incomplete '\\x' sequence in '%s'.\n", file, linenum, args[0]); |
| 7734 | return -1; |
| 7735 | } |
| 7736 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7737 | if (skip) { |
| 7738 | memmove(line + 1, line + 1 + skip, end - (line + skip + 1)); |
| 7739 | end -= skip; |
| 7740 | } |
| 7741 | line++; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7742 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7743 | else if (*line == '#' || *line == '\n' || *line == '\r') { |
| 7744 | /* end of string, end of loop */ |
| 7745 | *line = 0; |
| 7746 | break; |
| 7747 | } |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 7748 | else if (isspace((int)*line)) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7749 | /* a non-escaped space is an argument separator */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7750 | *line++ = 0; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 7751 | while (isspace((int)*line)) |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7752 | line++; |
| 7753 | args[++arg] = line; |
| 7754 | } |
| 7755 | else { |
| 7756 | line++; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7757 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7758 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7759 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7760 | /* empty line */ |
| 7761 | if (!**args) |
| 7762 | continue; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7763 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7764 | /* zero out remaining args */ |
| 7765 | while (++arg < MAX_LINE_ARGS) { |
| 7766 | args[arg] = line; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7767 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7768 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7769 | if (!strcmp(args[0], "listen") || !strcmp(args[0], "defaults")) /* new proxy */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7770 | confsect = CFG_LISTEN; |
| 7771 | else if (!strcmp(args[0], "global")) /* global config */ |
| 7772 | confsect = CFG_GLOBAL; |
| 7773 | /* else it's a section keyword */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7774 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7775 | switch (confsect) { |
| 7776 | case CFG_LISTEN: |
| 7777 | if (cfg_parse_listen(file, linenum, args) < 0) |
| 7778 | return -1; |
| 7779 | break; |
| 7780 | case CFG_GLOBAL: |
| 7781 | if (cfg_parse_global(file, linenum, args) < 0) |
| 7782 | return -1; |
| 7783 | break; |
| 7784 | default: |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7785 | Alert("parsing [%s:%d] : unknown keyword '%s' out of section.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7786 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7787 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7788 | |
| 7789 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7790 | } |
| 7791 | fclose(f); |
| 7792 | |
| 7793 | /* |
| 7794 | * Now, check for the integrity of all that we have collected. |
| 7795 | */ |
| 7796 | |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 7797 | /* will be needed further to delay some tasks */ |
| 7798 | tv_now(&now); |
| 7799 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7800 | if ((curproxy = proxy) == NULL) { |
| 7801 | Alert("parsing %s : no <listen> line. Nothing to do !\n", |
| 7802 | file); |
| 7803 | return -1; |
| 7804 | } |
| 7805 | |
| 7806 | while (curproxy != NULL) { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 7807 | curproxy->cursrv = NULL; |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7808 | if (curproxy->state == PR_STSTOPPED) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 7809 | curproxy = curproxy->next; |
| 7810 | continue; |
| 7811 | } |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 7812 | |
| 7813 | if (curproxy->listen == NULL) { |
| 7814 | Alert("parsing %s : listener %s has no listen address. Please either specify a valid address on the <listen> line, or use the <bind> keyword.\n", file, curproxy->id); |
| 7815 | cfgerr++; |
| 7816 | } |
| 7817 | else if ((curproxy->mode != PR_MODE_HEALTH) && |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7818 | !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) && |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7819 | (*(int *)&curproxy->dispatch_addr.sin_addr == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7820 | Alert("parsing %s : listener %s has no dispatch address and is not in transparent or balance mode.\n", |
| 7821 | file, curproxy->id); |
| 7822 | cfgerr++; |
| 7823 | } |
| 7824 | else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) { |
| 7825 | if (curproxy->options & PR_O_TRANSP) { |
| 7826 | Alert("parsing %s : listener %s cannot use both transparent and balance mode.\n", |
| 7827 | file, curproxy->id); |
| 7828 | cfgerr++; |
| 7829 | } |
| 7830 | else if (curproxy->srv == NULL) { |
| 7831 | Alert("parsing %s : listener %s needs at least 1 server in balance mode.\n", |
| 7832 | file, curproxy->id); |
| 7833 | cfgerr++; |
| 7834 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7835 | else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7836 | Warning("parsing %s : dispatch address of listener %s will be ignored in balance mode.\n", |
| 7837 | file, curproxy->id); |
| 7838 | } |
| 7839 | } |
| 7840 | else if (curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HEALTH) { /* TCP PROXY or HEALTH CHECK */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7841 | if (curproxy->cookie_name != NULL) { |
| 7842 | Warning("parsing %s : cookie will be ignored for listener %s.\n", |
| 7843 | file, curproxy->id); |
| 7844 | } |
| 7845 | if ((newsrv = curproxy->srv) != NULL) { |
| 7846 | Warning("parsing %s : servers will be ignored for listener %s.\n", |
| 7847 | file, curproxy->id); |
| 7848 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7849 | if (curproxy->rsp_exp != NULL) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7850 | Warning("parsing %s : server regular expressions will be ignored for listener %s.\n", |
| 7851 | file, curproxy->id); |
| 7852 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7853 | if (curproxy->req_exp != NULL) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7854 | Warning("parsing %s : client regular expressions will be ignored for listener %s.\n", |
| 7855 | file, curproxy->id); |
| 7856 | } |
| 7857 | } |
| 7858 | else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */ |
| 7859 | if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) { |
| 7860 | Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n", |
| 7861 | file, curproxy->id); |
| 7862 | cfgerr++; |
| 7863 | } |
| 7864 | else { |
| 7865 | while (newsrv != NULL) { |
| 7866 | /* nothing to check for now */ |
| 7867 | newsrv = newsrv->next; |
| 7868 | } |
| 7869 | } |
| 7870 | } |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 7871 | |
| 7872 | if (curproxy->options & PR_O_LOGASAP) |
| 7873 | curproxy->to_log &= ~LW_BYTES; |
| 7874 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7875 | if (curproxy->errmsg.msg400 == NULL) { |
| 7876 | curproxy->errmsg.msg400 = (char *)HTTP_400; |
| 7877 | curproxy->errmsg.len400 = strlen(HTTP_400); |
| 7878 | } |
| 7879 | if (curproxy->errmsg.msg403 == NULL) { |
| 7880 | curproxy->errmsg.msg403 = (char *)HTTP_403; |
| 7881 | curproxy->errmsg.len403 = strlen(HTTP_403); |
| 7882 | } |
| 7883 | if (curproxy->errmsg.msg408 == NULL) { |
| 7884 | curproxy->errmsg.msg408 = (char *)HTTP_408; |
| 7885 | curproxy->errmsg.len408 = strlen(HTTP_408); |
| 7886 | } |
| 7887 | if (curproxy->errmsg.msg500 == NULL) { |
| 7888 | curproxy->errmsg.msg500 = (char *)HTTP_500; |
| 7889 | curproxy->errmsg.len500 = strlen(HTTP_500); |
| 7890 | } |
| 7891 | if (curproxy->errmsg.msg502 == NULL) { |
| 7892 | curproxy->errmsg.msg502 = (char *)HTTP_502; |
| 7893 | curproxy->errmsg.len502 = strlen(HTTP_502); |
| 7894 | } |
| 7895 | if (curproxy->errmsg.msg503 == NULL) { |
| 7896 | curproxy->errmsg.msg503 = (char *)HTTP_503; |
| 7897 | curproxy->errmsg.len503 = strlen(HTTP_503); |
| 7898 | } |
| 7899 | if (curproxy->errmsg.msg504 == NULL) { |
| 7900 | curproxy->errmsg.msg504 = (char *)HTTP_504; |
| 7901 | curproxy->errmsg.len504 = strlen(HTTP_504); |
| 7902 | } |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 7903 | |
| 7904 | /* now we'll start this proxy's health checks if any */ |
| 7905 | /* 1- count the checkers to run simultaneously */ |
| 7906 | nbchk = 0; |
| 7907 | mininter = 0; |
| 7908 | newsrv = curproxy->srv; |
| 7909 | while (newsrv != NULL) { |
| 7910 | if (newsrv->state & SRV_CHECKED) { |
| 7911 | if (!mininter || mininter > newsrv->inter) |
| 7912 | mininter = newsrv->inter; |
| 7913 | nbchk++; |
| 7914 | } |
| 7915 | newsrv = newsrv->next; |
| 7916 | } |
| 7917 | |
| 7918 | /* 2- start them as far as possible from each others while respecting |
| 7919 | * their own intervals. For this, we will start them after their own |
| 7920 | * interval added to the min interval divided by the number of servers, |
| 7921 | * weighted by the server's position in the list. |
| 7922 | */ |
| 7923 | if (nbchk > 0) { |
| 7924 | struct task *t; |
| 7925 | int srvpos; |
| 7926 | |
| 7927 | newsrv = curproxy->srv; |
| 7928 | srvpos = 0; |
| 7929 | while (newsrv != NULL) { |
| 7930 | /* should this server be checked ? */ |
| 7931 | if (newsrv->state & SRV_CHECKED) { |
| 7932 | if ((t = pool_alloc(task)) == NULL) { |
| 7933 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 7934 | return -1; |
| 7935 | } |
| 7936 | |
| 7937 | t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */ |
| 7938 | t->wq = LIST_HEAD(wait_queue); /* but already has a wait queue assigned */ |
| 7939 | t->state = TASK_IDLE; |
| 7940 | t->process = process_chk; |
| 7941 | t->context = newsrv; |
| 7942 | |
| 7943 | /* check this every ms */ |
| 7944 | tv_delayfrom(&t->expire, &now, |
| 7945 | newsrv->inter + mininter * srvpos / nbchk); |
| 7946 | task_queue(t); |
| 7947 | //task_wakeup(&rq, t); |
| 7948 | srvpos++; |
| 7949 | } |
| 7950 | newsrv = newsrv->next; |
| 7951 | } |
| 7952 | } |
| 7953 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7954 | curproxy = curproxy->next; |
| 7955 | } |
| 7956 | if (cfgerr > 0) { |
| 7957 | Alert("Errors found in configuration file, aborting.\n"); |
| 7958 | return -1; |
| 7959 | } |
| 7960 | else |
| 7961 | return 0; |
| 7962 | } |
| 7963 | |
| 7964 | |
| 7965 | /* |
| 7966 | * This function initializes all the necessary variables. It only returns |
| 7967 | * if everything is OK. If something fails, it exits. |
| 7968 | */ |
| 7969 | void init(int argc, char **argv) { |
| 7970 | int i; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7971 | int arg_mode = 0; /* MODE_DEBUG, ... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7972 | char *old_argv = *argv; |
| 7973 | char *tmp; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 7974 | char *cfg_pidfile = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7975 | |
| 7976 | if (1<<INTBITS != sizeof(int)*8) { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 7977 | fprintf(stderr, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7978 | "Error: wrong architecture. Recompile so that sizeof(int)=%d\n", |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7979 | (int)(sizeof(int)*8)); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7980 | exit(1); |
| 7981 | } |
| 7982 | |
willy tarreau | 746e26b | 2006-03-25 11:14:35 +0100 | [diff] [blame] | 7983 | #ifdef HAPROXY_MEMMAX |
| 7984 | global.rlimit_memmax = HAPROXY_MEMMAX; |
| 7985 | #endif |
| 7986 | |
Willy TARREAU | a9e75f6 | 2006-03-01 22:27:48 +0100 | [diff] [blame] | 7987 | /* initialize the libc's localtime structures once for all so that we |
| 7988 | * won't be missing memory if we want to send alerts under OOM conditions. |
| 7989 | */ |
| 7990 | tv_now(&now); |
| 7991 | localtime(&now.tv_sec); |
| 7992 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7993 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 7994 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 7995 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 7996 | * printable characters above. |
| 7997 | */ |
| 7998 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 7999 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 8000 | for (i = 0; i < 32; i++) { |
| 8001 | FD_SET(i, hdr_encode_map); |
| 8002 | FD_SET(i, url_encode_map); |
| 8003 | } |
| 8004 | for (i = 127; i < 256; i++) { |
| 8005 | FD_SET(i, hdr_encode_map); |
| 8006 | FD_SET(i, url_encode_map); |
| 8007 | } |
| 8008 | |
| 8009 | tmp = "\"#{|}"; |
| 8010 | while (*tmp) { |
| 8011 | FD_SET(*tmp, hdr_encode_map); |
| 8012 | tmp++; |
| 8013 | } |
| 8014 | |
| 8015 | tmp = "\"#"; |
| 8016 | while (*tmp) { |
| 8017 | FD_SET(*tmp, url_encode_map); |
| 8018 | tmp++; |
| 8019 | } |
| 8020 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8021 | cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */ |
| 8022 | #if defined(ENABLE_POLL) |
| 8023 | cfg_polling_mechanism |= POLL_USE_POLL; |
| 8024 | #endif |
| 8025 | #if defined(ENABLE_EPOLL) |
| 8026 | cfg_polling_mechanism |= POLL_USE_EPOLL; |
| 8027 | #endif |
| 8028 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8029 | pid = getpid(); |
| 8030 | progname = *argv; |
| 8031 | while ((tmp = strchr(progname, '/')) != NULL) |
| 8032 | progname = tmp + 1; |
| 8033 | |
| 8034 | argc--; argv++; |
| 8035 | while (argc > 0) { |
| 8036 | char *flag; |
| 8037 | |
| 8038 | if (**argv == '-') { |
| 8039 | flag = *argv+1; |
| 8040 | |
| 8041 | /* 1 arg */ |
| 8042 | if (*flag == 'v') { |
| 8043 | display_version(); |
| 8044 | exit(0); |
| 8045 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8046 | #if defined(ENABLE_EPOLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8047 | else if (*flag == 'd' && flag[1] == 'e') |
| 8048 | cfg_polling_mechanism &= ~POLL_USE_EPOLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8049 | #endif |
| 8050 | #if defined(ENABLE_POLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8051 | else if (*flag == 'd' && flag[1] == 'p') |
| 8052 | cfg_polling_mechanism &= ~POLL_USE_POLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8053 | #endif |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 8054 | else if (*flag == 'V') |
| 8055 | arg_mode |= MODE_VERBOSE; |
willy tarreau | bf8ff3d | 2006-03-25 19:47:03 +0100 | [diff] [blame] | 8056 | else if (*flag == 'd' && flag[1] == 'b') |
| 8057 | arg_mode |= MODE_FOREGROUND; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8058 | else if (*flag == 'd') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8059 | arg_mode |= MODE_DEBUG; |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 8060 | else if (*flag == 'c') |
| 8061 | arg_mode |= MODE_CHECK; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8062 | else if (*flag == 'D') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8063 | arg_mode |= MODE_DAEMON | MODE_QUIET; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 8064 | else if (*flag == 'q') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8065 | arg_mode |= MODE_QUIET; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8066 | #if STATTIME > 0 |
| 8067 | else if (*flag == 's') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8068 | arg_mode |= MODE_STATS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8069 | else if (*flag == 'l') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8070 | arg_mode |= MODE_LOG; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8071 | #endif |
willy tarreau | 53e9970 | 2006-03-25 18:53:50 +0100 | [diff] [blame] | 8072 | else if (*flag == 's' && (flag[1] == 'f' || flag[1] == 't')) { |
| 8073 | /* list of pids to finish ('f') or terminate ('t') */ |
| 8074 | |
| 8075 | if (flag[1] == 'f') |
| 8076 | oldpids_sig = SIGUSR1; /* finish then exit */ |
| 8077 | else |
| 8078 | oldpids_sig = SIGTERM; /* terminate immediately */ |
| 8079 | argv++; argc--; |
| 8080 | |
| 8081 | if (argc > 0) { |
| 8082 | oldpids = calloc(argc, sizeof(int)); |
| 8083 | while (argc > 0) { |
| 8084 | oldpids[nb_oldpids] = atol(*argv); |
| 8085 | if (oldpids[nb_oldpids] <= 0) |
| 8086 | usage(old_argv); |
| 8087 | argc--; argv++; |
| 8088 | nb_oldpids++; |
| 8089 | } |
| 8090 | } |
| 8091 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8092 | else { /* >=2 args */ |
| 8093 | argv++; argc--; |
| 8094 | if (argc == 0) |
| 8095 | usage(old_argv); |
| 8096 | |
| 8097 | switch (*flag) { |
| 8098 | case 'n' : cfg_maxconn = atol(*argv); break; |
willy tarreau | 746e26b | 2006-03-25 11:14:35 +0100 | [diff] [blame] | 8099 | case 'm' : global.rlimit_memmax = atol(*argv); break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8100 | case 'N' : cfg_maxpconn = atol(*argv); break; |
| 8101 | case 'f' : cfg_cfgfile = *argv; break; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8102 | case 'p' : cfg_pidfile = *argv; break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8103 | default: usage(old_argv); |
| 8104 | } |
| 8105 | } |
| 8106 | } |
| 8107 | else |
| 8108 | usage(old_argv); |
willy tarreau | 53e9970 | 2006-03-25 18:53:50 +0100 | [diff] [blame] | 8109 | argv++; argc--; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8110 | } |
| 8111 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8112 | global.mode = MODE_STARTING | /* during startup, we want most of the alerts */ |
willy tarreau | bf8ff3d | 2006-03-25 19:47:03 +0100 | [diff] [blame] | 8113 | (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_VERBOSE |
| 8114 | | MODE_QUIET | MODE_CHECK | MODE_DEBUG)); |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 8115 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8116 | if (!cfg_cfgfile) |
| 8117 | usage(old_argv); |
| 8118 | |
| 8119 | gethostname(hostname, MAX_HOSTNAME_LEN); |
| 8120 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8121 | have_appsession = 0; |
Willy TARREAU | 203b0b6 | 2006-03-12 18:00:28 +0100 | [diff] [blame] | 8122 | global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8123 | if (readcfgfile(cfg_cfgfile) < 0) { |
| 8124 | Alert("Error reading configuration file : %s\n", cfg_cfgfile); |
| 8125 | exit(1); |
| 8126 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8127 | if (have_appsession) |
| 8128 | appsession_init(); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8129 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 8130 | if (global.mode & MODE_CHECK) { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 8131 | qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile); |
| 8132 | exit(0); |
| 8133 | } |
| 8134 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8135 | if (cfg_maxconn > 0) |
| 8136 | global.maxconn = cfg_maxconn; |
| 8137 | |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8138 | if (cfg_pidfile) { |
| 8139 | if (global.pidfile) |
| 8140 | free(global.pidfile); |
| 8141 | global.pidfile = strdup(cfg_pidfile); |
| 8142 | } |
| 8143 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8144 | if (global.maxconn == 0) |
| 8145 | global.maxconn = DEFAULT_MAXCONN; |
| 8146 | |
Willy TARREAU | 203b0b6 | 2006-03-12 18:00:28 +0100 | [diff] [blame] | 8147 | global.maxsock += global.maxconn * 2; /* each connection needs two sockets */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8148 | |
willy tarreau | bf8ff3d | 2006-03-25 19:47:03 +0100 | [diff] [blame] | 8149 | if (arg_mode & (MODE_DEBUG | MODE_FOREGROUND)) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8150 | /* command line debug mode inhibits configuration mode */ |
| 8151 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 8152 | } |
willy tarreau | bf8ff3d | 2006-03-25 19:47:03 +0100 | [diff] [blame] | 8153 | global.mode |= (arg_mode & (MODE_DAEMON | MODE_FOREGROUND | MODE_QUIET | |
| 8154 | MODE_VERBOSE | MODE_DEBUG | MODE_STATS | MODE_LOG)); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8155 | |
| 8156 | if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) { |
| 8157 | Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n"); |
| 8158 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 8159 | } |
| 8160 | |
| 8161 | if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) { |
willy tarreau | bf8ff3d | 2006-03-25 19:47:03 +0100 | [diff] [blame] | 8162 | if (!(global.mode & (MODE_FOREGROUND | MODE_DEBUG))) |
| 8163 | Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n"); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8164 | global.nbproc = 1; |
| 8165 | } |
| 8166 | |
| 8167 | if (global.nbproc < 1) |
| 8168 | global.nbproc = 1; |
| 8169 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8170 | StaticReadEvent = (fd_set *)calloc(1, |
| 8171 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8172 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8173 | StaticWriteEvent = (fd_set *)calloc(1, |
| 8174 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8175 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8176 | |
| 8177 | fdtab = (struct fdtab *)calloc(1, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8178 | sizeof(struct fdtab) * (global.maxsock)); |
| 8179 | for (i = 0; i < global.maxsock; i++) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8180 | fdtab[i].state = FD_STCLOSE; |
| 8181 | } |
| 8182 | } |
| 8183 | |
| 8184 | /* |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8185 | * this function starts all the proxies. Its return value is composed from |
| 8186 | * ERR_NONE, ERR_RETRYABLE and ERR_FATAL. Retryable errors will only be printed |
| 8187 | * if <verbose> is not zero. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8188 | */ |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8189 | int start_proxies(int verbose) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8190 | struct proxy *curproxy; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8191 | struct listener *listener; |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8192 | int err = ERR_NONE; |
| 8193 | int fd, pxerr; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8194 | |
| 8195 | for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) { |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8196 | if (curproxy->state != PR_STNEW) |
| 8197 | continue; /* already initialized */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8198 | |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8199 | pxerr = 0; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8200 | for (listener = curproxy->listen; listener != NULL; listener = listener->next) { |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8201 | if (listener->fd != -1) |
| 8202 | continue; /* already initialized */ |
| 8203 | |
| 8204 | if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
| 8205 | if (verbose) |
| 8206 | Alert("cannot create listening socket for proxy %s. Aborting.\n", |
| 8207 | curproxy->id); |
| 8208 | err |= ERR_RETRYABLE; |
| 8209 | pxerr |= 1; |
| 8210 | continue; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8211 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8212 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8213 | if (fd >= global.maxsock) { |
| 8214 | Alert("socket(): not enough free sockets for proxy %s. Raise -n argument. Aborting.\n", |
| 8215 | curproxy->id); |
| 8216 | close(fd); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8217 | err |= ERR_FATAL; |
| 8218 | pxerr |= 1; |
| 8219 | break; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8220 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 8221 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8222 | if ((fcntl(fd, F_SETFL, O_NONBLOCK) == -1) || |
| 8223 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 8224 | (char *) &one, sizeof(one)) == -1)) { |
| 8225 | Alert("cannot make socket non-blocking for proxy %s. Aborting.\n", |
| 8226 | curproxy->id); |
| 8227 | close(fd); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8228 | err |= ERR_FATAL; |
| 8229 | pxerr |= 1; |
| 8230 | break; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8231 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8232 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8233 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) { |
| 8234 | Alert("cannot do so_reuseaddr for proxy %s. Continuing.\n", |
| 8235 | curproxy->id); |
| 8236 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8237 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8238 | if (bind(fd, |
| 8239 | (struct sockaddr *)&listener->addr, |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 8240 | listener->addr.ss_family == AF_INET6 ? |
| 8241 | sizeof(struct sockaddr_in6) : |
| 8242 | sizeof(struct sockaddr_in)) == -1) { |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8243 | if (verbose) |
| 8244 | Alert("cannot bind socket for proxy %s. Aborting.\n", |
| 8245 | curproxy->id); |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8246 | close(fd); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8247 | err |= ERR_RETRYABLE; |
| 8248 | pxerr |= 1; |
| 8249 | continue; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8250 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8251 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8252 | if (listen(fd, curproxy->maxconn) == -1) { |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8253 | if (verbose) |
| 8254 | Alert("cannot listen to socket for proxy %s. Aborting.\n", |
| 8255 | curproxy->id); |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8256 | close(fd); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8257 | err |= ERR_RETRYABLE; |
| 8258 | pxerr |= 1; |
| 8259 | continue; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8260 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8261 | |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8262 | /* the socket is ready */ |
| 8263 | listener->fd = fd; |
| 8264 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8265 | /* the function for the accept() event */ |
| 8266 | fdtab[fd].read = &event_accept; |
| 8267 | fdtab[fd].write = NULL; /* never called */ |
| 8268 | fdtab[fd].owner = (struct task *)curproxy; /* reference the proxy instead of a task */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8269 | fdtab[fd].state = FD_STLISTEN; |
| 8270 | FD_SET(fd, StaticReadEvent); |
| 8271 | fd_insert(fd); |
| 8272 | listeners++; |
| 8273 | } |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8274 | |
| 8275 | if (!pxerr) { |
| 8276 | curproxy->state = PR_STRUN; |
| 8277 | send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id); |
| 8278 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8279 | } |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8280 | |
| 8281 | return err; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8282 | } |
| 8283 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 8284 | int match_str(const void *key1, const void *key2) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8285 | |
| 8286 | appsess *temp1,*temp2; |
| 8287 | temp1 = (appsess *)key1; |
| 8288 | temp2 = (appsess *)key2; |
| 8289 | |
| 8290 | //fprintf(stdout,">>>>>>>>>>>>>>temp1->sessid :%s:\n",temp1->sessid); |
| 8291 | //fprintf(stdout,">>>>>>>>>>>>>>temp2->sessid :%s:\n",temp2->sessid); |
| 8292 | |
| 8293 | return (strcmp(temp1->sessid,temp2->sessid) == 0); |
| 8294 | }/* end match_str */ |
| 8295 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 8296 | void destroy(void *data) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8297 | appsess *temp1; |
| 8298 | |
| 8299 | //printf("destroy called\n"); |
| 8300 | temp1 = (appsess *)data; |
| 8301 | |
| 8302 | if (temp1->sessid) |
| 8303 | pool_free_to(apools.sessid, temp1->sessid); |
| 8304 | |
| 8305 | if (temp1->serverid) |
| 8306 | pool_free_to(apools.serverid, temp1->serverid); |
| 8307 | |
| 8308 | pool_free(appsess, temp1); |
| 8309 | } /* end destroy */ |
| 8310 | |
| 8311 | void appsession_cleanup( void ) |
| 8312 | { |
| 8313 | struct proxy *p = proxy; |
| 8314 | |
| 8315 | while(p) { |
| 8316 | chtbl_destroy(&(p->htbl_proxy)); |
| 8317 | p = p->next; |
| 8318 | } |
| 8319 | }/* end appsession_cleanup() */ |
| 8320 | |
| 8321 | void pool_destroy(void **pool) |
| 8322 | { |
| 8323 | void *temp, *next; |
| 8324 | next = pool; |
| 8325 | while (next) { |
| 8326 | temp = next; |
| 8327 | next = *(void **)temp; |
| 8328 | free(temp); |
| 8329 | } |
| 8330 | }/* end pool_destroy() */ |
| 8331 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 8332 | void deinit(void) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8333 | struct proxy *p = proxy; |
| 8334 | struct cap_hdr *h,*h_next; |
| 8335 | struct server *s,*s_next; |
| 8336 | struct listener *l,*l_next; |
| 8337 | |
| 8338 | while (p) { |
| 8339 | if (p->id) |
| 8340 | free(p->id); |
| 8341 | |
| 8342 | if (p->check_req) |
| 8343 | free(p->check_req); |
| 8344 | |
| 8345 | if (p->cookie_name) |
| 8346 | free(p->cookie_name); |
| 8347 | |
| 8348 | if (p->capture_name) |
| 8349 | free(p->capture_name); |
| 8350 | |
| 8351 | /* only strup if the user have set in config. |
| 8352 | When should we free it?! |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 8353 | if (p->errmsg.msg400) free(p->errmsg.msg400); |
| 8354 | if (p->errmsg.msg403) free(p->errmsg.msg403); |
| 8355 | if (p->errmsg.msg408) free(p->errmsg.msg408); |
| 8356 | if (p->errmsg.msg500) free(p->errmsg.msg500); |
| 8357 | if (p->errmsg.msg502) free(p->errmsg.msg502); |
| 8358 | if (p->errmsg.msg503) free(p->errmsg.msg503); |
| 8359 | if (p->errmsg.msg504) free(p->errmsg.msg504); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8360 | */ |
| 8361 | if (p->appsession_name) |
| 8362 | free(p->appsession_name); |
| 8363 | |
| 8364 | h = p->req_cap; |
| 8365 | while (h) { |
| 8366 | h_next = h->next; |
| 8367 | if (h->name) |
| 8368 | free(h->name); |
| 8369 | pool_destroy(h->pool); |
| 8370 | free(h); |
| 8371 | h = h_next; |
| 8372 | }/* end while(h) */ |
| 8373 | |
| 8374 | h = p->rsp_cap; |
| 8375 | while (h) { |
| 8376 | h_next = h->next; |
| 8377 | if (h->name) |
| 8378 | free(h->name); |
| 8379 | |
| 8380 | pool_destroy(h->pool); |
| 8381 | free(h); |
| 8382 | h = h_next; |
| 8383 | }/* end while(h) */ |
| 8384 | |
| 8385 | s = p->srv; |
| 8386 | while (s) { |
| 8387 | s_next = s->next; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 8388 | if (s->id) |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8389 | free(s->id); |
| 8390 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 8391 | if (s->cookie) |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8392 | free(s->cookie); |
| 8393 | |
| 8394 | free(s); |
| 8395 | s = s_next; |
| 8396 | }/* end while(s) */ |
| 8397 | |
| 8398 | l = p->listen; |
| 8399 | while (l) { |
| 8400 | l_next = l->next; |
| 8401 | free(l); |
| 8402 | l = l_next; |
| 8403 | }/* end while(l) */ |
| 8404 | |
| 8405 | pool_destroy((void **) p->req_cap_pool); |
| 8406 | pool_destroy((void **) p->rsp_cap_pool); |
| 8407 | p = p->next; |
| 8408 | }/* end while(p) */ |
| 8409 | |
| 8410 | if (global.chroot) free(global.chroot); |
| 8411 | if (global.pidfile) free(global.pidfile); |
| 8412 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8413 | if (StaticReadEvent) free(StaticReadEvent); |
| 8414 | if (StaticWriteEvent) free(StaticWriteEvent); |
| 8415 | if (fdtab) free(fdtab); |
| 8416 | |
| 8417 | pool_destroy(pool_session); |
| 8418 | pool_destroy(pool_buffer); |
| 8419 | pool_destroy(pool_fdtab); |
| 8420 | pool_destroy(pool_requri); |
| 8421 | pool_destroy(pool_task); |
| 8422 | pool_destroy(pool_capture); |
| 8423 | pool_destroy(pool_appsess); |
| 8424 | |
| 8425 | if (have_appsession) { |
| 8426 | pool_destroy(apools.serverid); |
| 8427 | pool_destroy(apools.sessid); |
| 8428 | } |
| 8429 | } /* end deinit() */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8430 | |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8431 | /* sends the signal <sig> to all pids found in <oldpids> */ |
| 8432 | static void tell_old_pids(int sig) { |
| 8433 | int p; |
| 8434 | for (p = 0; p < nb_oldpids; p++) |
| 8435 | kill(oldpids[p], sig); |
| 8436 | } |
| 8437 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8438 | int main(int argc, char **argv) { |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8439 | int err, retry; |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 8440 | struct rlimit limit; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8441 | FILE *pidfile = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8442 | init(argc, argv); |
| 8443 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8444 | signal(SIGQUIT, dump); |
| 8445 | signal(SIGUSR1, sig_soft_stop); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 8446 | signal(SIGHUP, sig_dump_state); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8447 | #ifdef DEBUG_MEMORY |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8448 | signal(SIGINT, sig_int); |
| 8449 | signal(SIGTERM, sig_term); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8450 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8451 | |
| 8452 | /* on very high loads, a sigpipe sometimes happen just between the |
| 8453 | * getsockopt() which tells "it's OK to write", and the following write :-( |
| 8454 | */ |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 8455 | #ifndef MSG_NOSIGNAL |
| 8456 | signal(SIGPIPE, SIG_IGN); |
| 8457 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8458 | |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8459 | /* We will loop at most 100 times with 10 ms delay each time. |
| 8460 | * That's at most 1 second. We only send a signal to old pids |
| 8461 | * if we cannot grab at least one port. |
| 8462 | */ |
| 8463 | retry = MAX_START_RETRIES; |
| 8464 | err = ERR_NONE; |
| 8465 | while (retry >= 0) { |
| 8466 | struct timeval w; |
| 8467 | err = start_proxies(retry == 0 || nb_oldpids == 0); |
| 8468 | if (err != ERR_RETRYABLE) |
| 8469 | break; |
| 8470 | if (nb_oldpids == 0) |
| 8471 | break; |
| 8472 | |
| 8473 | tell_old_pids(SIGTTOU); |
| 8474 | /* give some time to old processes to stop listening */ |
| 8475 | w.tv_sec = 0; |
| 8476 | w.tv_usec = 10*1000; |
| 8477 | select(0, NULL, NULL, NULL, &w); |
| 8478 | retry--; |
| 8479 | } |
| 8480 | |
| 8481 | /* Note: start_proxies() sends an alert when it fails. */ |
| 8482 | if (err != ERR_NONE) { |
| 8483 | if (retry != MAX_START_RETRIES && nb_oldpids) |
| 8484 | tell_old_pids(SIGTTIN); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8485 | exit(1); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8486 | } |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8487 | |
| 8488 | if (listeners == 0) { |
| 8489 | Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8490 | /* Note: we don't have to send anything to the old pids because we |
| 8491 | * never stopped them. */ |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8492 | exit(1); |
| 8493 | } |
| 8494 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 8495 | /* prepare pause/play signals */ |
| 8496 | signal(SIGTTOU, sig_pause); |
| 8497 | signal(SIGTTIN, sig_listen); |
| 8498 | |
Willy TARREAU | e3283d1 | 2006-03-01 22:15:29 +0100 | [diff] [blame] | 8499 | if (global.mode & MODE_DAEMON) { |
| 8500 | global.mode &= ~MODE_VERBOSE; |
| 8501 | global.mode |= MODE_QUIET; |
| 8502 | } |
| 8503 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8504 | /* MODE_QUIET can inhibit alerts and warnings below this line */ |
| 8505 | |
| 8506 | global.mode &= ~MODE_STARTING; |
Willy TARREAU | e3283d1 | 2006-03-01 22:15:29 +0100 | [diff] [blame] | 8507 | if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) { |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8508 | /* detach from the tty */ |
| 8509 | fclose(stdin); fclose(stdout); fclose(stderr); |
| 8510 | close(0); close(1); close(2); |
| 8511 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8512 | |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8513 | /* open log & pid files before the chroot */ |
| 8514 | if (global.mode & MODE_DAEMON && global.pidfile != NULL) { |
| 8515 | int pidfd; |
| 8516 | unlink(global.pidfile); |
| 8517 | pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644); |
| 8518 | if (pidfd < 0) { |
| 8519 | Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8520 | if (nb_oldpids) |
| 8521 | tell_old_pids(SIGTTIN); |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8522 | exit(1); |
| 8523 | } |
| 8524 | pidfile = fdopen(pidfd, "w"); |
| 8525 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8526 | |
| 8527 | /* chroot if needed */ |
| 8528 | if (global.chroot != NULL) { |
| 8529 | if (chroot(global.chroot) == -1) { |
| 8530 | Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8531 | if (nb_oldpids) |
| 8532 | tell_old_pids(SIGTTIN); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8533 | } |
| 8534 | chdir("/"); |
| 8535 | } |
| 8536 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 8537 | /* ulimits */ |
Willy TARREAU | dd67617 | 2006-03-12 18:01:33 +0100 | [diff] [blame] | 8538 | if (!global.rlimit_nofile) |
| 8539 | global.rlimit_nofile = global.maxsock; |
| 8540 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 8541 | if (global.rlimit_nofile) { |
| 8542 | limit.rlim_cur = limit.rlim_max = global.rlimit_nofile; |
| 8543 | if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { |
| 8544 | Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile); |
| 8545 | } |
willy tarreau | 746e26b | 2006-03-25 11:14:35 +0100 | [diff] [blame] | 8546 | } |
| 8547 | |
| 8548 | if (global.rlimit_memmax) { |
| 8549 | limit.rlim_cur = limit.rlim_max = |
| 8550 | global.rlimit_memmax * 1048576 / global.nbproc; |
| 8551 | #ifdef RLIMIT_AS |
| 8552 | if (setrlimit(RLIMIT_AS, &limit) == -1) { |
| 8553 | Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n", |
| 8554 | argv[0], global.rlimit_memmax); |
| 8555 | } |
| 8556 | #else |
| 8557 | if (setrlimit(RLIMIT_DATA, &limit) == -1) { |
| 8558 | Warning("[%s.main()] Cannot fix MEM limit to %d megs.\n", |
| 8559 | argv[0], global.rlimit_memmax); |
| 8560 | } |
| 8561 | #endif |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 8562 | } |
| 8563 | |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8564 | if (nb_oldpids) |
| 8565 | tell_old_pids(oldpids_sig); |
| 8566 | |
| 8567 | /* Note that any error at this stage will be fatal because we will not |
| 8568 | * be able to restart the old pids. |
| 8569 | */ |
| 8570 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8571 | /* setgid / setuid */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8572 | if (global.gid && setgid(global.gid) == -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8573 | Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid); |
| 8574 | exit(1); |
| 8575 | } |
| 8576 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8577 | if (global.uid && setuid(global.uid) == -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8578 | Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid); |
| 8579 | exit(1); |
| 8580 | } |
| 8581 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 8582 | /* check ulimits */ |
| 8583 | limit.rlim_cur = limit.rlim_max = 0; |
| 8584 | getrlimit(RLIMIT_NOFILE, &limit); |
| 8585 | if (limit.rlim_cur < global.maxsock) { |
| 8586 | Warning("[%s.main()] FD limit (%d) too low for maxconn=%d/maxsock=%d. Please raise 'ulimit-n' to %d or more to avoid any trouble.\n", |
| 8587 | argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock); |
| 8588 | } |
| 8589 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8590 | if (global.mode & MODE_DAEMON) { |
| 8591 | int ret = 0; |
| 8592 | int proc; |
| 8593 | |
| 8594 | /* the father launches the required number of processes */ |
| 8595 | for (proc = 0; proc < global.nbproc; proc++) { |
| 8596 | ret = fork(); |
| 8597 | if (ret < 0) { |
| 8598 | Alert("[%s.main()] Cannot fork.\n", argv[0]); |
willy tarreau | 41310e7 | 2006-03-25 18:17:56 +0100 | [diff] [blame] | 8599 | if (nb_oldpids) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8600 | exit(1); /* there has been an error */ |
| 8601 | } |
| 8602 | else if (ret == 0) /* child breaks here */ |
| 8603 | break; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8604 | if (pidfile != NULL) { |
| 8605 | fprintf(pidfile, "%d\n", ret); |
| 8606 | fflush(pidfile); |
| 8607 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8608 | } |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8609 | /* close the pidfile both in children and father */ |
| 8610 | if (pidfile != NULL) |
| 8611 | fclose(pidfile); |
| 8612 | free(global.pidfile); |
| 8613 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8614 | if (proc == global.nbproc) |
| 8615 | exit(0); /* parent must leave */ |
| 8616 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 8617 | /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure |
| 8618 | * that we can detach from the TTY. We MUST NOT do it in other cases since |
| 8619 | * it would have already be done, and 0-2 would have been affected to listening |
| 8620 | * sockets |
| 8621 | */ |
| 8622 | if (!(global.mode & MODE_QUIET)) { |
| 8623 | /* detach from the tty */ |
| 8624 | fclose(stdin); fclose(stdout); fclose(stderr); |
| 8625 | close(0); close(1); close(2); /* close all fd's */ |
| 8626 | global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */ |
| 8627 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 8628 | pid = getpid(); /* update child's pid */ |
willy tarreau | e867b48 | 2005-12-17 13:28:43 +0100 | [diff] [blame] | 8629 | setsid(); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8630 | } |
| 8631 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8632 | #if defined(ENABLE_EPOLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8633 | if (cfg_polling_mechanism & POLL_USE_EPOLL) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8634 | if (epoll_loop(POLL_LOOP_ACTION_INIT)) { |
| 8635 | epoll_loop(POLL_LOOP_ACTION_RUN); |
| 8636 | epoll_loop(POLL_LOOP_ACTION_CLEAN); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8637 | cfg_polling_mechanism &= POLL_USE_EPOLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8638 | } |
| 8639 | else { |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8640 | Warning("epoll() is not available. Using poll()/select() instead.\n"); |
| 8641 | cfg_polling_mechanism &= ~POLL_USE_EPOLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8642 | } |
| 8643 | } |
| 8644 | #endif |
| 8645 | |
| 8646 | #if defined(ENABLE_POLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8647 | if (cfg_polling_mechanism & POLL_USE_POLL) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8648 | if (poll_loop(POLL_LOOP_ACTION_INIT)) { |
| 8649 | poll_loop(POLL_LOOP_ACTION_RUN); |
| 8650 | poll_loop(POLL_LOOP_ACTION_CLEAN); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8651 | cfg_polling_mechanism &= POLL_USE_POLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8652 | } |
| 8653 | else { |
| 8654 | Warning("poll() is not available. Using select() instead.\n"); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8655 | cfg_polling_mechanism &= ~POLL_USE_POLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8656 | } |
| 8657 | } |
| 8658 | #endif |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8659 | if (cfg_polling_mechanism & POLL_USE_SELECT) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8660 | if (select_loop(POLL_LOOP_ACTION_INIT)) { |
| 8661 | select_loop(POLL_LOOP_ACTION_RUN); |
| 8662 | select_loop(POLL_LOOP_ACTION_CLEAN); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8663 | cfg_polling_mechanism &= POLL_USE_SELECT; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8664 | } |
| 8665 | } |
| 8666 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8667 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8668 | /* Free all Hash Keys and all Hash elements */ |
| 8669 | appsession_cleanup(); |
| 8670 | /* Do some cleanup */ |
| 8671 | deinit(); |
| 8672 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8673 | exit(0); |
| 8674 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8675 | |
| 8676 | #if defined(DEBUG_HASH) |
| 8677 | static void print_table(const CHTbl *htbl) { |
| 8678 | |
| 8679 | ListElmt *element; |
| 8680 | int i; |
| 8681 | appsess *asession; |
| 8682 | |
| 8683 | /***************************************************************************** |
| 8684 | * * |
| 8685 | * Display the chained hash table. * |
| 8686 | * * |
| 8687 | *****************************************************************************/ |
| 8688 | |
| 8689 | fprintf(stdout, "Table size is %d\n", chtbl_size(htbl)); |
| 8690 | |
| 8691 | for (i = 0; i < TBLSIZ; i++) { |
| 8692 | fprintf(stdout, "Bucket[%03d]\n", i); |
| 8693 | |
| 8694 | for (element = list_head(&htbl->table[i]); element != NULL; element = list_next(element)) { |
| 8695 | //fprintf(stdout, "%c", *(char *)list_data(element)); |
| 8696 | asession = (appsess *)list_data(element); |
| 8697 | fprintf(stdout, "ELEM :%s:", asession->sessid); |
| 8698 | fprintf(stdout, " Server :%s: \n", asession->serverid); |
| 8699 | //fprintf(stdout, " Server request_count :%li:\n",asession->request_count); |
| 8700 | } |
| 8701 | |
| 8702 | fprintf(stdout, "\n"); |
| 8703 | } |
| 8704 | return; |
| 8705 | } /* end print_table */ |
| 8706 | #endif |
| 8707 | |
| 8708 | static int appsession_init(void) |
| 8709 | { |
| 8710 | static int initialized = 0; |
| 8711 | int idlen; |
| 8712 | struct server *s; |
| 8713 | struct proxy *p = proxy; |
| 8714 | |
| 8715 | if (!initialized) { |
| 8716 | if (!appsession_task_init()) { |
| 8717 | apools.sessid = NULL; |
| 8718 | apools.serverid = NULL; |
| 8719 | apools.ser_waste = 0; |
| 8720 | apools.ser_use = 0; |
| 8721 | apools.ser_msize = sizeof(void *); |
| 8722 | apools.ses_waste = 0; |
| 8723 | apools.ses_use = 0; |
| 8724 | apools.ses_msize = sizeof(void *); |
| 8725 | while (p) { |
| 8726 | s = p->srv; |
| 8727 | if (apools.ses_msize < p->appsession_len) |
| 8728 | apools.ses_msize = p->appsession_len; |
| 8729 | while (s) { |
| 8730 | idlen = strlen(s->id); |
| 8731 | if (apools.ser_msize < idlen) |
| 8732 | apools.ser_msize = idlen; |
| 8733 | s = s->next; |
| 8734 | } |
| 8735 | p = p->next; |
| 8736 | } |
| 8737 | apools.ser_msize ++; /* we use strings, so reserve space for '\0' */ |
| 8738 | apools.ses_msize ++; |
| 8739 | } |
| 8740 | else { |
| 8741 | fprintf(stderr, "appsession_task_init failed\n"); |
| 8742 | return -1; |
| 8743 | } |
| 8744 | initialized ++; |
| 8745 | } |
| 8746 | return 0; |
| 8747 | } |
| 8748 | |
| 8749 | static int appsession_task_init(void) |
| 8750 | { |
| 8751 | static int initialized = 0; |
| 8752 | struct task *t; |
| 8753 | if (!initialized) { |
| 8754 | if ((t = pool_alloc(task)) == NULL) |
| 8755 | return -1; |
| 8756 | t->next = t->prev = t->rqnext = NULL; |
| 8757 | t->wq = LIST_HEAD(wait_queue); |
| 8758 | t->state = TASK_IDLE; |
| 8759 | t->context = NULL; |
| 8760 | tv_delayfrom(&t->expire, &now, TBLCHKINT); |
| 8761 | task_queue(t); |
| 8762 | t->process = appsession_refresh; |
| 8763 | initialized ++; |
| 8764 | } |
| 8765 | return 0; |
| 8766 | } |
| 8767 | |
| 8768 | static int appsession_refresh(struct task *t) { |
| 8769 | struct proxy *p = proxy; |
| 8770 | CHTbl *htbl; |
| 8771 | ListElmt *element, *last; |
| 8772 | int i; |
| 8773 | appsess *asession; |
| 8774 | void *data; |
| 8775 | |
| 8776 | while (p) { |
| 8777 | if (p->appsession_name != NULL) { |
| 8778 | htbl = &p->htbl_proxy; |
| 8779 | /* if we ever give up the use of TBLSIZ, we need to change this */ |
| 8780 | for (i = 0; i < TBLSIZ; i++) { |
| 8781 | last = NULL; |
| 8782 | for (element = list_head(&htbl->table[i]); element != NULL; element = list_next(element)) { |
| 8783 | asession = (appsess *)list_data(element); |
| 8784 | if (tv_cmp2_ms(&asession->expire, &now) <= 0) { |
| 8785 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 8786 | int len; |
| 8787 | /* |
| 8788 | on Linux NULL pointers are catched by sprintf, on solaris -> segfault |
| 8789 | */ |
| 8790 | len = sprintf(trash, "appsession_refresh: cleaning up expired Session '%s' on Server %s\n", |
| 8791 | asession->sessid, asession->serverid?asession->serverid:"(null)"); |
| 8792 | write(1, trash, len); |
| 8793 | } |
| 8794 | /* delete the expired element from within the hash table */ |
| 8795 | if ((list_rem_next(&htbl->table[i], last, (void **)&data) == 0) |
| 8796 | && (htbl->table[i].destroy != NULL)) { |
| 8797 | htbl->table[i].destroy(data); |
| 8798 | } |
| 8799 | if (last == NULL) {/* patient lost his head, get a new one */ |
| 8800 | element = list_head(&htbl->table[i]); |
| 8801 | if (element == NULL) break; /* no heads left, go to next patient */ |
| 8802 | } |
| 8803 | else |
| 8804 | element = last; |
| 8805 | }/* end if (tv_cmp2_ms(&asession->expire, &now) <= 0) */ |
| 8806 | else |
| 8807 | last = element; |
| 8808 | }/* end for (element = list_head(&htbl->table[i]); element != NULL; element = list_next(element)) */ |
| 8809 | } |
| 8810 | } |
| 8811 | p = p->next; |
| 8812 | } |
| 8813 | tv_delayfrom(&t->expire, &now, TBLCHKINT); /* check expiration every 5 seconds */ |
| 8814 | return TBLCHKINT; |
| 8815 | } /* end appsession_refresh */ |
| 8816 | |