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 |
| 91 | #define HAPROXY_VERSION "1.2.10.1" |
| 92 | #endif |
| 93 | |
| 94 | #ifndef HAPROXY_DATE |
| 95 | #define HAPROXY_DATE "2006/03/23" |
| 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 | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 335 | #define PR_O_BALANCE (PR_O_BALANCE_RR) |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 336 | #define PR_O_KEEPALIVE 0x00000080 /* follow keep-alive sessions */ |
| 337 | #define PR_O_FWDFOR 0x00000100 /* insert x-forwarded-for with client address */ |
| 338 | #define PR_O_BIND_SRC 0x00000200 /* bind to a specific source address when connect()ing */ |
| 339 | #define PR_O_NULLNOLOG 0x00000400 /* a connect without request will not be logged */ |
| 340 | #define PR_O_COOK_NOC 0x00000800 /* add a 'Cache-control' header with the cookie */ |
| 341 | #define PR_O_COOK_POST 0x00001000 /* don't insert cookies for requests other than a POST */ |
| 342 | #define PR_O_HTTP_CHK 0x00002000 /* use HTTP 'OPTIONS' method to check server health */ |
| 343 | #define PR_O_PERSIST 0x00004000 /* server persistence stays effective even when server is down */ |
| 344 | #define PR_O_LOGASAP 0x00008000 /* log as soon as possible, without waiting for the session to complete */ |
| 345 | #define PR_O_HTTP_CLOSE 0x00010000 /* force 'connection: close' in both directions */ |
| 346 | #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] | 347 | #define PR_O_TCP_CLI_KA 0x00040000 /* enable TCP keep-alive on client-side sessions */ |
| 348 | #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] | 349 | #define PR_O_USE_ALL_BK 0x00100000 /* load-balance between backup servers */ |
Willy TARREAU | 767ba71 | 2006-03-01 22:40:50 +0100 | [diff] [blame] | 350 | #define PR_O_FORCE_CLO 0x00200000 /* enforce the connection close immediately after server response */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 351 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 352 | /* various session flags */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 353 | #define SN_DIRECT 0x00000001 /* connection made on the server matching the client cookie */ |
| 354 | #define SN_CLDENY 0x00000002 /* a client header matches a deny regex */ |
| 355 | #define SN_CLALLOW 0x00000004 /* a client header matches an allow regex */ |
| 356 | #define SN_SVDENY 0x00000008 /* a server header matches a deny regex */ |
| 357 | #define SN_SVALLOW 0x00000010 /* a server header matches an allow regex */ |
| 358 | #define SN_POST 0x00000020 /* the request was an HTTP POST */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 359 | #define SN_MONITOR 0x00000040 /* this session comes from a monitoring system */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 360 | |
| 361 | #define SN_CK_NONE 0x00000000 /* this session had no cookie */ |
| 362 | #define SN_CK_INVALID 0x00000040 /* this session had a cookie which matches no server */ |
| 363 | #define SN_CK_DOWN 0x00000080 /* this session had cookie matching a down server */ |
| 364 | #define SN_CK_VALID 0x000000C0 /* this session had cookie matching a valid server */ |
| 365 | #define SN_CK_MASK 0x000000C0 /* mask to get this session's cookie flags */ |
| 366 | #define SN_CK_SHIFT 6 /* bit shift */ |
| 367 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 368 | #define SN_ERR_NONE 0x00000000 |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 369 | #define SN_ERR_CLITO 0x00000100 /* client time-out */ |
| 370 | #define SN_ERR_CLICL 0x00000200 /* client closed (read/write error) */ |
| 371 | #define SN_ERR_SRVTO 0x00000300 /* server time-out, connect time-out */ |
| 372 | #define SN_ERR_SRVCL 0x00000400 /* server closed (connect/read/write error) */ |
| 373 | #define SN_ERR_PRXCOND 0x00000500 /* the proxy decided to close (deny...) */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 374 | #define SN_ERR_RESOURCE 0x00000600 /* the proxy encountered a lack of a local resources (fd, mem, ...) */ |
| 375 | #define SN_ERR_INTERNAL 0x00000700 /* the proxy encountered an internal error */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 376 | #define SN_ERR_MASK 0x00000700 /* mask to get only session error flags */ |
| 377 | #define SN_ERR_SHIFT 8 /* bit shift */ |
| 378 | |
| 379 | #define SN_FINST_R 0x00001000 /* session ended during client request */ |
| 380 | #define SN_FINST_C 0x00002000 /* session ended during server connect */ |
| 381 | #define SN_FINST_H 0x00003000 /* session ended during server headers */ |
| 382 | #define SN_FINST_D 0x00004000 /* session ended during data phase */ |
| 383 | #define SN_FINST_L 0x00005000 /* session ended while pushing last data to client */ |
| 384 | #define SN_FINST_MASK 0x00007000 /* mask to get only final session state flags */ |
| 385 | #define SN_FINST_SHIFT 12 /* bit shift */ |
| 386 | |
| 387 | #define SN_SCK_NONE 0x00000000 /* no set-cookie seen for the server cookie */ |
| 388 | #define SN_SCK_DELETED 0x00010000 /* existing set-cookie deleted or changed */ |
| 389 | #define SN_SCK_INSERTED 0x00020000 /* new set-cookie inserted or changed existing one */ |
| 390 | #define SN_SCK_SEEN 0x00040000 /* set-cookie seen for the server cookie */ |
| 391 | #define SN_SCK_MASK 0x00070000 /* mask to get the set-cookie field */ |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 392 | #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] | 393 | #define SN_SCK_SHIFT 16 /* bit shift */ |
| 394 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 395 | #define SN_CACHEABLE 0x00100000 /* at least part of the response is cacheable */ |
| 396 | #define SN_CACHE_COOK 0x00200000 /* a cookie in the response is cacheable */ |
| 397 | #define SN_CACHE_SHIFT 20 /* bit shift */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 398 | |
| 399 | /* different possible states for the client side */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 400 | #define CL_STHEADERS 0 |
| 401 | #define CL_STDATA 1 |
| 402 | #define CL_STSHUTR 2 |
| 403 | #define CL_STSHUTW 3 |
| 404 | #define CL_STCLOSE 4 |
| 405 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 406 | /* different possible states for the server side */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 407 | #define SV_STIDLE 0 |
| 408 | #define SV_STCONN 1 |
| 409 | #define SV_STHEADERS 2 |
| 410 | #define SV_STDATA 3 |
| 411 | #define SV_STSHUTR 4 |
| 412 | #define SV_STSHUTW 5 |
| 413 | #define SV_STCLOSE 6 |
| 414 | |
| 415 | /* result of an I/O event */ |
| 416 | #define RES_SILENT 0 /* didn't happen */ |
| 417 | #define RES_DATA 1 /* data were sent or received */ |
| 418 | #define RES_NULL 2 /* result is 0 (read == 0), or connect without need for writing */ |
| 419 | #define RES_ERROR 3 /* result -1 or error on the socket (eg: connect()) */ |
| 420 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 421 | /* modes of operation (global.mode) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 422 | #define MODE_DEBUG 1 |
| 423 | #define MODE_STATS 2 |
| 424 | #define MODE_LOG 4 |
| 425 | #define MODE_DAEMON 8 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 426 | #define MODE_QUIET 16 |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 427 | #define MODE_CHECK 32 |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 428 | #define MODE_VERBOSE 64 |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 429 | #define MODE_STARTING 128 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 430 | |
| 431 | /* server flags */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 432 | #define SRV_RUNNING 1 /* the server is UP */ |
| 433 | #define SRV_BACKUP 2 /* this server is a backup server */ |
| 434 | #define SRV_MAPPORTS 4 /* this server uses mapped ports */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 435 | #define SRV_BIND_SRC 8 /* this server uses a specific source address */ |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 436 | #define SRV_CHECKED 16 /* this server needs to be checked */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 437 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 438 | /* what to do when a header matches a regex */ |
| 439 | #define ACT_ALLOW 0 /* allow the request */ |
| 440 | #define ACT_REPLACE 1 /* replace the matching header */ |
| 441 | #define ACT_REMOVE 2 /* remove the matching header */ |
| 442 | #define ACT_DENY 3 /* deny the request */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 443 | #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] | 444 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 445 | /* configuration sections */ |
| 446 | #define CFG_NONE 0 |
| 447 | #define CFG_GLOBAL 1 |
| 448 | #define CFG_LISTEN 2 |
| 449 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 450 | /* 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] | 451 | #define LW_DATE 1 /* date */ |
| 452 | #define LW_CLIP 2 /* CLient IP */ |
| 453 | #define LW_SVIP 4 /* SerVer IP */ |
| 454 | #define LW_SVID 8 /* server ID */ |
| 455 | #define LW_REQ 16 /* http REQuest */ |
| 456 | #define LW_RESP 32 /* http RESPonse */ |
| 457 | #define LW_PXIP 64 /* proxy IP */ |
| 458 | #define LW_PXID 128 /* proxy ID */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 459 | #define LW_BYTES 256 /* bytes read from server */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 460 | #define LW_COOKIE 512 /* captured cookie */ |
| 461 | #define LW_REQHDR 1024 /* request header(s) */ |
| 462 | #define LW_RSPHDR 2048 /* response header(s) */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 463 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 464 | /*********************************************************************/ |
| 465 | |
| 466 | #define LIST_HEAD(a) ((void *)(&(a))) |
| 467 | |
| 468 | /*********************************************************************/ |
| 469 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 470 | struct cap_hdr { |
| 471 | struct cap_hdr *next; |
| 472 | char *name; /* header name, case insensitive */ |
| 473 | int namelen; /* length of the header name, to speed-up lookups */ |
| 474 | int len; /* capture length, not including terminal zero */ |
| 475 | int index; /* index in the output array */ |
| 476 | void *pool; /* pool of pre-allocated memory area of (len+1) bytes */ |
| 477 | }; |
| 478 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 479 | struct hdr_exp { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 480 | struct hdr_exp *next; |
| 481 | regex_t *preg; /* expression to look for */ |
| 482 | int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */ |
| 483 | char *replace; /* expression to set instead */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 484 | }; |
| 485 | |
| 486 | struct buffer { |
| 487 | unsigned int l; /* data length */ |
| 488 | 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] | 489 | char *rlim; /* read limit, used for header rewriting */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 490 | unsigned long long total; /* total data read */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 491 | char data[BUFSIZE]; |
| 492 | }; |
| 493 | |
| 494 | struct server { |
| 495 | struct server *next; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 496 | int state; /* server state (SRV_*) */ |
| 497 | int cklen; /* the len of the cookie, to speed up checks */ |
| 498 | char *cookie; /* the id set in the cookie */ |
| 499 | char *id; /* just for identification */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 500 | struct sockaddr_in addr; /* the address to connect to */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 501 | 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] | 502 | short check_port; /* the port to use for the health checks */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 503 | int health; /* 0->rise-1 = bad; rise->rise+fall-1 = good */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 504 | int rise, fall; /* time in iterations */ |
| 505 | int inter; /* time in milliseconds */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 506 | int result; /* 0 = connect OK, -1 = connect KO */ |
| 507 | int curfd; /* file desc used for current test, or -1 if not in test */ |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 508 | struct proxy *proxy; /* the proxy this server belongs to */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 509 | }; |
| 510 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 511 | /* The base for all tasks */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 512 | struct task { |
| 513 | struct task *next, *prev; /* chaining ... */ |
| 514 | struct task *rqnext; /* chaining in run queue ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 515 | struct task *wq; /* the wait queue this task is in */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 516 | int state; /* task state : IDLE or RUNNING */ |
| 517 | 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] | 518 | int (*process)(struct task *t); /* the function which processes the task */ |
| 519 | void *context; /* the task's context */ |
| 520 | }; |
| 521 | |
| 522 | /* WARNING: if new fields are added, they must be initialized in event_accept() */ |
| 523 | struct session { |
| 524 | struct task *task; /* the task associated with this session */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 525 | /* application specific below */ |
| 526 | struct timeval crexpire; /* expiration date for a client read */ |
| 527 | struct timeval cwexpire; /* expiration date for a client write */ |
| 528 | struct timeval srexpire; /* expiration date for a server read */ |
| 529 | struct timeval swexpire; /* expiration date for a server write */ |
| 530 | struct timeval cnexpire; /* expiration date for a connect */ |
| 531 | char res_cr, res_cw, res_sr, res_sw;/* results of some events */ |
| 532 | struct proxy *proxy; /* the proxy this socket belongs to */ |
| 533 | int cli_fd; /* the client side fd */ |
| 534 | int srv_fd; /* the server side fd */ |
| 535 | int cli_state; /* state of the client side */ |
| 536 | int srv_state; /* state of the server side */ |
| 537 | int conn_retries; /* number of connect retries left */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 538 | int flags; /* some flags describing the session */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 539 | struct buffer *req; /* request buffer */ |
| 540 | struct buffer *rep; /* response buffer */ |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 541 | struct sockaddr_storage cli_addr; /* the client address */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 542 | struct sockaddr_in srv_addr; /* the address to connect to */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 543 | struct server *srv; /* the server being used */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 544 | char **req_cap; /* array of captured request headers (may be NULL) */ |
| 545 | char **rsp_cap; /* array of captured response headers (may be NULL) */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 546 | struct { |
| 547 | int logwait; /* log fields waiting to be collected : LW_* */ |
| 548 | struct timeval tv_accept; /* date of the accept() (beginning of the session) */ |
| 549 | long t_request; /* delay before the end of the request arrives, -1 if never occurs */ |
| 550 | long t_connect; /* delay before the connect() to the server succeeds, -1 if never occurs */ |
| 551 | long t_data; /* delay before the first data byte from the server ... */ |
| 552 | unsigned long t_close; /* total session duration */ |
| 553 | char *uri; /* first line if log needed, NULL otherwise */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 554 | char *cli_cookie; /* cookie presented by the client, in capture mode */ |
| 555 | char *srv_cookie; /* cookie presented by the server, in capture mode */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 556 | int status; /* HTTP status from the server, negative if from proxy */ |
| 557 | long long bytes; /* number of bytes transferred from the server */ |
| 558 | } logs; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 559 | unsigned int uniq_id; /* unique ID used for the traces */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 560 | }; |
| 561 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 562 | struct listener { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 563 | int fd; /* the listen socket */ |
| 564 | struct sockaddr_storage addr; /* the address we listen to */ |
| 565 | struct listener *next; /* next address or NULL */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 566 | }; |
| 567 | |
| 568 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 569 | struct proxy { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 570 | struct listener *listen; /* the listen addresses and sockets */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 571 | 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] | 572 | int state; /* proxy state */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 573 | struct sockaddr_in dispatch_addr; /* the default address to connect to */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 574 | struct server *srv, *cursrv; /* known servers, current server */ |
| 575 | int nbservers; /* # of servers */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 576 | char *cookie_name; /* name of the cookie to look for */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 577 | int cookie_len; /* strlen(cookie_name), computed only once */ |
| 578 | char *appsession_name; /* name of the cookie to look for */ |
| 579 | int appsession_name_len; /* strlen(appsession_name), computed only once */ |
| 580 | int appsession_len; /* length of the appsession cookie value to be used */ |
| 581 | int appsession_timeout; |
| 582 | CHTbl htbl_proxy; /* Per Proxy hashtable */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 583 | char *capture_name; /* beginning of the name of the cookie to capture */ |
| 584 | int capture_namelen; /* length of the cookie name to match */ |
| 585 | int capture_len; /* length of the string to be captured */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 586 | int clitimeout; /* client I/O timeout (in milliseconds) */ |
| 587 | int srvtimeout; /* server I/O timeout (in milliseconds) */ |
| 588 | int contimeout; /* connect timeout (in milliseconds) */ |
| 589 | char *id; /* proxy id */ |
| 590 | int nbconn; /* # of active sessions */ |
| 591 | int maxconn; /* max # of active sessions */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 592 | int conn_retries; /* maximum number of connect retries */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 593 | int options; /* PR_O_REDISP, PR_O_TRANSP, ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 594 | 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] | 595 | 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] | 596 | struct proxy *next; |
| 597 | struct sockaddr_in logsrv1, logsrv2; /* 2 syslog servers */ |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 598 | signed char logfac1, logfac2; /* log facility for both servers. -1 = disabled */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 599 | int loglev1, loglev2; /* log level for each server, 7 by default */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 600 | int to_log; /* things to be logged (LW_*) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 601 | struct timeval stop_time; /* date to stop listening, when stopping != 0 */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 602 | int nb_reqadd, nb_rspadd; |
| 603 | struct hdr_exp *req_exp; /* regular expressions for request headers */ |
| 604 | struct hdr_exp *rsp_exp; /* regular expressions for response headers */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 605 | int nb_req_cap, nb_rsp_cap; /* # of headers to be captured */ |
| 606 | struct cap_hdr *req_cap; /* chained list of request headers to be captured */ |
| 607 | struct cap_hdr *rsp_cap; /* chained list of response headers to be captured */ |
| 608 | 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] | 609 | 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] | 610 | int grace; /* grace time after stop request */ |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 611 | char *check_req; /* HTTP request to use if PR_O_HTTP_CHK is set, else NULL */ |
| 612 | int check_len; /* Length of the HTTP request */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 613 | struct { |
| 614 | char *msg400; /* message for error 400 */ |
| 615 | int len400; /* message length for error 400 */ |
| 616 | char *msg403; /* message for error 403 */ |
| 617 | int len403; /* message length for error 403 */ |
| 618 | char *msg408; /* message for error 408 */ |
| 619 | int len408; /* message length for error 408 */ |
| 620 | char *msg500; /* message for error 500 */ |
| 621 | int len500; /* message length for error 500 */ |
| 622 | char *msg502; /* message for error 502 */ |
| 623 | int len502; /* message length for error 502 */ |
| 624 | char *msg503; /* message for error 503 */ |
| 625 | int len503; /* message length for error 503 */ |
| 626 | char *msg504; /* message for error 504 */ |
| 627 | int len504; /* message length for error 504 */ |
| 628 | } errmsg; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 629 | }; |
| 630 | |
| 631 | /* info about one given fd */ |
| 632 | struct fdtab { |
| 633 | int (*read)(int fd); /* read function */ |
| 634 | int (*write)(int fd); /* write function */ |
| 635 | struct task *owner; /* the session (or proxy) associated with this fd */ |
| 636 | int state; /* the state of this fd */ |
| 637 | }; |
| 638 | |
| 639 | /*********************************************************************/ |
| 640 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 641 | int cfg_maxpconn = DEFAULT_MAXCONN; /* # of simultaneous connections per proxy (-N) */ |
Willy TARREAU | 13032e7 | 2006-03-12 17:31:45 +0100 | [diff] [blame] | 642 | int cfg_maxconn = 0; /* # of simultaneous connections, (-n) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 643 | char *cfg_cfgfile = NULL; /* configuration file */ |
| 644 | char *progname = NULL; /* program name */ |
| 645 | int pid; /* current process id */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 646 | |
| 647 | /* global options */ |
| 648 | static struct { |
| 649 | int uid; |
| 650 | int gid; |
| 651 | int nbproc; |
| 652 | int maxconn; |
| 653 | int maxsock; /* max # of sockets */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 654 | int rlimit_nofile; /* default ulimit-n value : 0=unset */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 655 | int mode; |
| 656 | char *chroot; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 657 | char *pidfile; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 658 | int logfac1, logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 659 | int loglev1, loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 660 | struct sockaddr_in logsrv1, logsrv2; |
| 661 | } global = { |
| 662 | logfac1 : -1, |
| 663 | logfac2 : -1, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 664 | loglev1 : 7, /* max syslog level : debug */ |
| 665 | loglev2 : 7, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 666 | /* others NULL OK */ |
| 667 | }; |
| 668 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 669 | /*********************************************************************/ |
| 670 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 671 | fd_set *StaticReadEvent, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 672 | *StaticWriteEvent; |
| 673 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 674 | int cfg_polling_mechanism = 0; /* POLL_USE_{SELECT|POLL|EPOLL} */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 675 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 676 | void **pool_session = NULL, |
| 677 | **pool_buffer = NULL, |
| 678 | **pool_fdtab = NULL, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 679 | **pool_requri = NULL, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 680 | **pool_task = NULL, |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 681 | **pool_capture = NULL, |
| 682 | **pool_appsess = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 683 | |
| 684 | struct proxy *proxy = NULL; /* list of all existing proxies */ |
| 685 | struct fdtab *fdtab = NULL; /* array of all the file descriptors */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 686 | struct task *rq = NULL; /* global run queue */ |
| 687 | struct task wait_queue = { /* global wait queue */ |
| 688 | prev:LIST_HEAD(wait_queue), |
| 689 | next:LIST_HEAD(wait_queue) |
| 690 | }; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 691 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 692 | static int totalconn = 0; /* total # of terminated sessions */ |
| 693 | static int actconn = 0; /* # of active sessions */ |
| 694 | static int maxfd = 0; /* # of the highest fd + 1 */ |
| 695 | static int listeners = 0; /* # of listeners */ |
| 696 | static int stopping = 0; /* non zero means stopping in progress */ |
| 697 | static struct timeval now = {0,0}; /* the current date at any moment */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 698 | 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] | 699 | |
willy tarreau | 08dedbe | 2005-12-18 01:13:48 +0100 | [diff] [blame] | 700 | #if defined(ENABLE_EPOLL) |
| 701 | /* FIXME: this is dirty, but at the moment, there's no other solution to remove |
| 702 | * the old FDs from outside the loop. Perhaps we should export a global 'poll' |
| 703 | * structure with pointers to functions such as init_fd() and close_fd(), plus |
| 704 | * a private structure with several pointers to places such as below. |
| 705 | */ |
| 706 | |
| 707 | static fd_set *PrevReadEvent = NULL, *PrevWriteEvent = NULL; |
| 708 | #endif |
| 709 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 710 | 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] | 711 | /* 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] | 712 | static char trash[BUFSIZE]; |
| 713 | |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 714 | const int zero = 0; |
| 715 | const int one = 1; |
| 716 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 717 | /* |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 718 | * Syslog facilities and levels. Conforming to RFC3164. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 719 | */ |
| 720 | |
| 721 | #define MAX_SYSLOG_LEN 1024 |
| 722 | #define NB_LOG_FACILITIES 24 |
| 723 | const char *log_facilities[NB_LOG_FACILITIES] = { |
| 724 | "kern", "user", "mail", "daemon", |
| 725 | "auth", "syslog", "lpr", "news", |
| 726 | "uucp", "cron", "auth2", "ftp", |
| 727 | "ntp", "audit", "alert", "cron2", |
| 728 | "local0", "local1", "local2", "local3", |
| 729 | "local4", "local5", "local6", "local7" |
| 730 | }; |
| 731 | |
| 732 | |
| 733 | #define NB_LOG_LEVELS 8 |
| 734 | const char *log_levels[NB_LOG_LEVELS] = { |
| 735 | "emerg", "alert", "crit", "err", |
| 736 | "warning", "notice", "info", "debug" |
| 737 | }; |
| 738 | |
| 739 | #define SYSLOG_PORT 514 |
| 740 | |
| 741 | const char *monthname[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 742 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 743 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 744 | 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] | 745 | const char sess_fin_state[8] = "-RCHDL67"; /* cliRequest, srvConnect, srvHeader, Data, Last, unknown */ |
| 746 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 747 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 748 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 749 | unknown, Set-cookie Rewritten */ |
| 750 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 751 | #define MAX_HOSTNAME_LEN 32 |
| 752 | static char hostname[MAX_HOSTNAME_LEN] = ""; |
| 753 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 754 | const char *HTTP_302 = |
| 755 | "HTTP/1.0 302 Found\r\n" |
| 756 | "Cache-Control: no-cache\r\n" |
| 757 | "Connection: close\r\n" |
| 758 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 759 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 760 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 761 | const char *HTTP_303 = |
| 762 | "HTTP/1.0 303 See Other\r\n" |
| 763 | "Cache-Control: no-cache\r\n" |
| 764 | "Connection: close\r\n" |
| 765 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 766 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 767 | const char *HTTP_400 = |
| 768 | "HTTP/1.0 400 Bad request\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 769 | "Cache-Control: no-cache\r\n" |
| 770 | "Connection: close\r\n" |
| 771 | "\r\n" |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 772 | "<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] | 773 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 774 | const char *HTTP_403 = |
| 775 | "HTTP/1.0 403 Forbidden\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 776 | "Cache-Control: no-cache\r\n" |
| 777 | "Connection: close\r\n" |
| 778 | "\r\n" |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 779 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n"; |
| 780 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 781 | const char *HTTP_408 = |
| 782 | "HTTP/1.0 408 Request Time-out\r\n" |
| 783 | "Cache-Control: no-cache\r\n" |
| 784 | "Connection: close\r\n" |
| 785 | "\r\n" |
| 786 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n"; |
| 787 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 788 | const char *HTTP_500 = |
| 789 | "HTTP/1.0 500 Server Error\r\n" |
| 790 | "Cache-Control: no-cache\r\n" |
| 791 | "Connection: close\r\n" |
| 792 | "\r\n" |
| 793 | "<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] | 794 | |
| 795 | const char *HTTP_502 = |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 796 | "HTTP/1.0 502 Bad Gateway\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 797 | "Cache-Control: no-cache\r\n" |
| 798 | "Connection: close\r\n" |
| 799 | "\r\n" |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 800 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n"; |
| 801 | |
| 802 | const char *HTTP_503 = |
| 803 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 804 | "Cache-Control: no-cache\r\n" |
| 805 | "Connection: close\r\n" |
| 806 | "\r\n" |
| 807 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n"; |
| 808 | |
| 809 | const char *HTTP_504 = |
| 810 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 811 | "Cache-Control: no-cache\r\n" |
| 812 | "Connection: close\r\n" |
| 813 | "\r\n" |
| 814 | "<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] | 815 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 816 | /*********************************************************************/ |
| 817 | /* statistics ******************************************************/ |
| 818 | /*********************************************************************/ |
| 819 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 820 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 821 | static int stats_tsk_lsrch, stats_tsk_rsrch, |
| 822 | stats_tsk_good, stats_tsk_right, stats_tsk_left, |
| 823 | stats_tsk_new, stats_tsk_nsrch; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 824 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 825 | |
| 826 | |
| 827 | /*********************************************************************/ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 828 | /* debugging *******************************************************/ |
| 829 | /*********************************************************************/ |
| 830 | #ifdef DEBUG_FULL |
| 831 | static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 832 | static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 833 | #endif |
| 834 | |
| 835 | /*********************************************************************/ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 836 | /* function prototypes *********************************************/ |
| 837 | /*********************************************************************/ |
| 838 | |
| 839 | int event_accept(int fd); |
| 840 | int event_cli_read(int fd); |
| 841 | int event_cli_write(int fd); |
| 842 | int event_srv_read(int fd); |
| 843 | int event_srv_write(int fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 844 | int process_session(struct task *t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 845 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 846 | static int appsession_task_init(void); |
| 847 | static int appsession_init(void); |
| 848 | static int appsession_refresh(struct task *t); |
| 849 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 850 | /*********************************************************************/ |
| 851 | /* general purpose functions ***************************************/ |
| 852 | /*********************************************************************/ |
| 853 | |
| 854 | void display_version() { |
| 855 | printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n"); |
willy tarreau | 726618c | 2006-01-29 22:42:06 +0100 | [diff] [blame] | 856 | printf("Copyright 2000-2006 Willy Tarreau <w@w.ods.org>\n\n"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | /* |
| 860 | * This function prints the command line usage and exits |
| 861 | */ |
| 862 | void usage(char *name) { |
| 863 | display_version(); |
| 864 | fprintf(stderr, |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 865 | "Usage : %s -f <cfgfile> [ -vdV" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 866 | #if STATTIME > 0 |
| 867 | "sl" |
| 868 | #endif |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 869 | "D ] [ -n <maxconn> ] [ -N <maxpconn> ] [ -p <pidfile> ]\n" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 870 | " -v displays version\n" |
| 871 | " -d enters debug mode\n" |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 872 | " -V enters verbose mode (disables quiet mode)\n" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 873 | #if STATTIME > 0 |
| 874 | " -s enables statistics output\n" |
| 875 | " -l enables long statistics format\n" |
| 876 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 877 | " -D goes daemon ; implies -q\n" |
| 878 | " -q quiet mode : don't display messages\n" |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 879 | " -c check mode : only check config file and exit\n" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 880 | " -n sets the maximum total # of connections (%d)\n" |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 881 | " -N sets the default, per-proxy maximum # of connections (%d)\n" |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 882 | " -p writes pids of all children to this file\n" |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 883 | #if defined(ENABLE_EPOLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 884 | " -de disables epoll() usage even when available\n" |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 885 | #endif |
| 886 | #if defined(ENABLE_POLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 887 | " -dp disables poll() usage even when available\n" |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 888 | #endif |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 889 | "\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 890 | name, DEFAULT_MAXCONN, cfg_maxpconn); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 891 | exit(1); |
| 892 | } |
| 893 | |
| 894 | |
| 895 | /* |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 896 | * Displays the message on stderr with the date and pid. Overrides the quiet |
| 897 | * mode during startup. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 898 | */ |
| 899 | void Alert(char *fmt, ...) { |
| 900 | va_list argp; |
| 901 | struct timeval tv; |
| 902 | struct tm *tm; |
| 903 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 904 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 905 | va_start(argp, fmt); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 906 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 907 | gettimeofday(&tv, NULL); |
| 908 | tm=localtime(&tv.tv_sec); |
| 909 | fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ", |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 910 | 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] | 911 | vfprintf(stderr, fmt, argp); |
| 912 | fflush(stderr); |
| 913 | va_end(argp); |
| 914 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | |
| 918 | /* |
| 919 | * Displays the message on stderr with the date and pid. |
| 920 | */ |
| 921 | void Warning(char *fmt, ...) { |
| 922 | va_list argp; |
| 923 | struct timeval tv; |
| 924 | struct tm *tm; |
| 925 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 926 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 927 | va_start(argp, fmt); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 928 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 929 | gettimeofday(&tv, NULL); |
| 930 | tm=localtime(&tv.tv_sec); |
| 931 | fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ", |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 932 | 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] | 933 | vfprintf(stderr, fmt, argp); |
| 934 | fflush(stderr); |
| 935 | va_end(argp); |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | * Displays the message on <out> only if quiet mode is not set. |
| 941 | */ |
| 942 | void qfprintf(FILE *out, char *fmt, ...) { |
| 943 | va_list argp; |
| 944 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 945 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 946 | va_start(argp, fmt); |
| 947 | vfprintf(out, fmt, argp); |
| 948 | fflush(out); |
| 949 | va_end(argp); |
| 950 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | |
| 954 | /* |
| 955 | * converts <str> to a struct sockaddr_in* which is locally allocated. |
| 956 | * The format is "addr:port", where "addr" can be empty or "*" to indicate |
| 957 | * INADDR_ANY. |
| 958 | */ |
| 959 | struct sockaddr_in *str2sa(char *str) { |
| 960 | static struct sockaddr_in sa; |
| 961 | char *c; |
| 962 | int port; |
| 963 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 964 | memset(&sa, 0, sizeof(sa)); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 965 | str=strdup(str); |
| 966 | |
| 967 | if ((c=strrchr(str,':')) != NULL) { |
| 968 | *c++=0; |
| 969 | port=atol(c); |
| 970 | } |
| 971 | else |
| 972 | port=0; |
| 973 | |
| 974 | if (*str == '*' || *str == '\0') { /* INADDR_ANY */ |
| 975 | sa.sin_addr.s_addr = INADDR_ANY; |
| 976 | } |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 977 | else if (!inet_pton(AF_INET, str, &sa.sin_addr)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 978 | struct hostent *he; |
| 979 | |
| 980 | if ((he = gethostbyname(str)) == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 981 | Alert("Invalid server name: '%s'\n", str); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 982 | } |
| 983 | else |
| 984 | sa.sin_addr = *(struct in_addr *) *(he->h_addr_list); |
| 985 | } |
| 986 | sa.sin_port=htons(port); |
| 987 | sa.sin_family=AF_INET; |
| 988 | |
| 989 | free(str); |
| 990 | return &sa; |
| 991 | } |
| 992 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 993 | /* |
| 994 | * converts <str> to a two struct in_addr* which are locally allocated. |
| 995 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
| 996 | * is optionnal and either in the dotted or CIDR notation. |
| 997 | * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error. |
| 998 | */ |
| 999 | int str2net(char *str, struct in_addr *addr, struct in_addr *mask) { |
| 1000 | char *c; |
| 1001 | unsigned long len; |
| 1002 | |
| 1003 | memset(mask, 0, sizeof(*mask)); |
| 1004 | memset(addr, 0, sizeof(*addr)); |
| 1005 | str=strdup(str); |
| 1006 | |
| 1007 | if ((c = strrchr(str, '/')) != NULL) { |
| 1008 | *c++ = 0; |
| 1009 | /* c points to the mask */ |
| 1010 | if (strchr(c, '.') != NULL) { /* dotted notation */ |
| 1011 | if (!inet_pton(AF_INET, c, mask)) |
| 1012 | return 0; |
| 1013 | } |
| 1014 | else { /* mask length */ |
| 1015 | char *err; |
| 1016 | len = strtol(c, &err, 10); |
| 1017 | if (!*c || (err && *err) || (unsigned)len > 32) |
| 1018 | return 0; |
| 1019 | if (len) |
| 1020 | mask->s_addr = htonl(0xFFFFFFFFUL << (32 - len)); |
| 1021 | else |
| 1022 | mask->s_addr = 0; |
| 1023 | } |
| 1024 | } |
| 1025 | else { |
| 1026 | mask->s_addr = 0xFFFFFFFF; |
| 1027 | } |
| 1028 | if (!inet_pton(AF_INET, str, addr)) { |
| 1029 | struct hostent *he; |
| 1030 | |
| 1031 | if ((he = gethostbyname(str)) == NULL) { |
| 1032 | return 0; |
| 1033 | } |
| 1034 | else |
| 1035 | *addr = *(struct in_addr *) *(he->h_addr_list); |
| 1036 | } |
| 1037 | free(str); |
| 1038 | return 1; |
| 1039 | } |
| 1040 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1041 | |
| 1042 | /* |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1043 | * converts <str> to a list of listeners which are dynamically allocated. |
| 1044 | * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where : |
| 1045 | * - <addr> can be empty or "*" to indicate INADDR_ANY ; |
| 1046 | * - <port> is a numerical port from 1 to 65535 ; |
| 1047 | * - <end> indicates to use the range from <port> to <end> instead (inclusive). |
| 1048 | * This can be repeated as many times as necessary, separated by a coma. |
| 1049 | * The <tail> argument is a pointer to a current list which should be appended |
| 1050 | * to the tail of the new list. The pointer to the new list is returned. |
| 1051 | */ |
| 1052 | struct listener *str2listener(char *str, struct listener *tail) { |
| 1053 | struct listener *l; |
| 1054 | char *c, *next, *range, *dupstr; |
| 1055 | int port, end; |
| 1056 | |
| 1057 | next = dupstr = strdup(str); |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1058 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1059 | while (next && *next) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1060 | struct sockaddr_storage ss; |
| 1061 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1062 | str = next; |
| 1063 | /* 1) look for the end of the first address */ |
| 1064 | if ((next = strrchr(str, ',')) != NULL) { |
| 1065 | *next++ = 0; |
| 1066 | } |
| 1067 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1068 | /* 2) look for the addr/port delimiter, it's the last colon. */ |
| 1069 | if ((range = strrchr(str, ':')) == NULL) { |
| 1070 | Alert("Missing port number: '%s'\n", str); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1071 | goto fail; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | *range++ = 0; |
| 1075 | |
| 1076 | if (strrchr(str, ':') != NULL) { |
| 1077 | /* IPv6 address contains ':' */ |
| 1078 | memset(&ss, 0, sizeof(ss)); |
| 1079 | ss.ss_family = AF_INET6; |
| 1080 | |
| 1081 | if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in6 *)&ss)->sin6_addr)) { |
| 1082 | Alert("Invalid server address: '%s'\n", str); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1083 | goto fail; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1084 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1085 | } |
| 1086 | else { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1087 | memset(&ss, 0, sizeof(ss)); |
| 1088 | ss.ss_family = AF_INET; |
| 1089 | |
| 1090 | if (*str == '*' || *str == '\0') { /* INADDR_ANY */ |
| 1091 | ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; |
| 1092 | } |
| 1093 | else if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in *)&ss)->sin_addr)) { |
| 1094 | struct hostent *he; |
| 1095 | |
| 1096 | if ((he = gethostbyname(str)) == NULL) { |
| 1097 | Alert("Invalid server name: '%s'\n", str); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1098 | goto fail; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1099 | } |
| 1100 | else |
| 1101 | ((struct sockaddr_in *)&ss)->sin_addr = |
| 1102 | *(struct in_addr *) *(he->h_addr_list); |
| 1103 | } |
| 1104 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1105 | |
| 1106 | /* 3) look for the port-end delimiter */ |
| 1107 | if ((c = strchr(range, '-')) != NULL) { |
| 1108 | *c++ = 0; |
| 1109 | end = atol(c); |
| 1110 | } |
| 1111 | else { |
| 1112 | end = atol(range); |
| 1113 | } |
| 1114 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1115 | port = atol(range); |
| 1116 | |
| 1117 | if (port < 1 || port > 65535) { |
| 1118 | Alert("Invalid port '%d' specified for address '%s'.\n", port, str); |
| 1119 | goto fail; |
| 1120 | } |
| 1121 | |
| 1122 | if (end < 1 || end > 65535) { |
| 1123 | Alert("Invalid port '%d' specified for address '%s'.\n", end, str); |
| 1124 | goto fail; |
| 1125 | } |
| 1126 | |
| 1127 | for (; port <= end; port++) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1128 | l = (struct listener *)calloc(1, sizeof(struct listener)); |
| 1129 | l->next = tail; |
| 1130 | tail = l; |
| 1131 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1132 | l->addr = ss; |
| 1133 | if (ss.ss_family == AF_INET6) |
| 1134 | ((struct sockaddr_in6 *)(&l->addr))->sin6_port = htons(port); |
| 1135 | else |
| 1136 | ((struct sockaddr_in *)(&l->addr))->sin_port = htons(port); |
| 1137 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1138 | } /* end for(port) */ |
| 1139 | } /* end while(next) */ |
| 1140 | free(dupstr); |
| 1141 | return tail; |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1142 | fail: |
| 1143 | free(dupstr); |
| 1144 | return NULL; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1145 | } |
| 1146 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1147 | |
| 1148 | #define FD_SETS_ARE_BITFIELDS |
| 1149 | #ifdef FD_SETS_ARE_BITFIELDS |
| 1150 | /* |
| 1151 | * This map is used with all the FD_* macros to check whether a particular bit |
| 1152 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 1153 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 1154 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 1155 | * exclusively to the macros. |
| 1156 | */ |
| 1157 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 1158 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 1159 | |
| 1160 | #else |
| 1161 | #error "Check if your OS uses bitfields for fd_sets" |
| 1162 | #endif |
| 1163 | |
| 1164 | /* will try to encode the string <string> replacing all characters tagged in |
| 1165 | * <map> with the hexadecimal representation of their ASCII-code (2 digits) |
| 1166 | * prefixed by <escape>, and will store the result between <start> (included |
| 1167 | *) and <stop> (excluded), and will always terminate the string with a '\0' |
| 1168 | * before <stop>. The position of the '\0' is returned if the conversion |
| 1169 | * completes. If bytes are missing between <start> and <stop>, then the |
| 1170 | * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0' |
| 1171 | * cannot even be stored so we return <start> without writing the 0. |
| 1172 | * The input string must also be zero-terminated. |
| 1173 | */ |
| 1174 | char hextab[16] = "0123456789ABCDEF"; |
| 1175 | char *encode_string(char *start, char *stop, |
| 1176 | const char escape, const fd_set *map, |
| 1177 | const char *string) |
| 1178 | { |
| 1179 | if (start < stop) { |
| 1180 | stop--; /* reserve one byte for the final '\0' */ |
| 1181 | while (start < stop && *string != 0) { |
| 1182 | if (!FD_ISSET((unsigned char)(*string), map)) |
| 1183 | *start++ = *string; |
| 1184 | else { |
| 1185 | if (start + 3 >= stop) |
| 1186 | break; |
| 1187 | *start++ = escape; |
| 1188 | *start++ = hextab[(*string >> 4) & 15]; |
| 1189 | *start++ = hextab[*string & 15]; |
| 1190 | } |
| 1191 | string++; |
| 1192 | } |
| 1193 | *start = '\0'; |
| 1194 | } |
| 1195 | return start; |
| 1196 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1197 | |
| 1198 | /* |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1199 | * This function sends a syslog message to both log servers of a proxy, |
| 1200 | * or to global log servers if the proxy is NULL. |
| 1201 | * It also tries not to waste too much time computing the message header. |
| 1202 | * It doesn't care about errors nor does it report them. |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1203 | */ |
| 1204 | void send_log(struct proxy *p, int level, char *message, ...) { |
| 1205 | static int logfd = -1; /* syslog UDP socket */ |
| 1206 | static long tvsec = -1; /* to force the string to be initialized */ |
| 1207 | struct timeval tv; |
| 1208 | va_list argp; |
| 1209 | static char logmsg[MAX_SYSLOG_LEN]; |
| 1210 | static char *dataptr = NULL; |
| 1211 | int fac_level; |
| 1212 | int hdr_len, data_len; |
| 1213 | struct sockaddr_in *sa[2]; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1214 | int facilities[2], loglevel[2]; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1215 | int nbloggers = 0; |
| 1216 | char *log_ptr; |
| 1217 | |
| 1218 | if (logfd < 0) { |
| 1219 | if ((logfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) |
| 1220 | return; |
| 1221 | } |
| 1222 | |
| 1223 | if (level < 0 || progname == NULL || message == NULL) |
| 1224 | return; |
| 1225 | |
| 1226 | gettimeofday(&tv, NULL); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1227 | if (tv.tv_sec != tvsec || dataptr == NULL) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1228 | /* this string is rebuild only once a second */ |
| 1229 | struct tm *tm = localtime(&tv.tv_sec); |
| 1230 | tvsec = tv.tv_sec; |
| 1231 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1232 | hdr_len = snprintf(logmsg, sizeof(logmsg), |
| 1233 | "<<<<>%s %2d %02d:%02d:%02d %s[%d]: ", |
| 1234 | monthname[tm->tm_mon], |
| 1235 | tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 1236 | progname, pid); |
| 1237 | /* WARNING: depending upon implementations, snprintf may return |
| 1238 | * either -1 or the number of bytes that would be needed to store |
| 1239 | * the total message. In both cases, we must adjust it. |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1240 | */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1241 | if (hdr_len < 0 || hdr_len > sizeof(logmsg)) |
| 1242 | hdr_len = sizeof(logmsg); |
| 1243 | |
| 1244 | dataptr = logmsg + hdr_len; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | va_start(argp, message); |
| 1248 | data_len = vsnprintf(dataptr, logmsg + sizeof(logmsg) - dataptr, message, argp); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1249 | if (data_len < 0 || data_len > (logmsg + sizeof(logmsg) - dataptr)) |
| 1250 | data_len = logmsg + sizeof(logmsg) - dataptr; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1251 | va_end(argp); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1252 | dataptr[data_len - 1] = '\n'; /* force a break on ultra-long lines */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1253 | |
| 1254 | if (p == NULL) { |
| 1255 | if (global.logfac1 >= 0) { |
| 1256 | sa[nbloggers] = &global.logsrv1; |
| 1257 | facilities[nbloggers] = global.logfac1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1258 | loglevel[nbloggers] = global.loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1259 | nbloggers++; |
| 1260 | } |
| 1261 | if (global.logfac2 >= 0) { |
| 1262 | sa[nbloggers] = &global.logsrv2; |
| 1263 | facilities[nbloggers] = global.logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1264 | loglevel[nbloggers] = global.loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1265 | nbloggers++; |
| 1266 | } |
| 1267 | } else { |
| 1268 | if (p->logfac1 >= 0) { |
| 1269 | sa[nbloggers] = &p->logsrv1; |
| 1270 | facilities[nbloggers] = p->logfac1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1271 | loglevel[nbloggers] = p->loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1272 | nbloggers++; |
| 1273 | } |
| 1274 | if (p->logfac2 >= 0) { |
| 1275 | sa[nbloggers] = &p->logsrv2; |
| 1276 | facilities[nbloggers] = p->logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1277 | loglevel[nbloggers] = p->loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1278 | nbloggers++; |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | while (nbloggers-- > 0) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1283 | /* we can filter the level of the messages that are sent to each logger */ |
| 1284 | if (level > loglevel[nbloggers]) |
| 1285 | continue; |
| 1286 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1287 | /* For each target, we may have a different facility. |
| 1288 | * We can also have a different log level for each message. |
| 1289 | * This induces variations in the message header length. |
| 1290 | * Since we don't want to recompute it each time, nor copy it every |
| 1291 | * time, we only change the facility in the pre-computed header, |
| 1292 | * and we change the pointer to the header accordingly. |
| 1293 | */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1294 | fac_level = (facilities[nbloggers] << 3) + level; |
| 1295 | log_ptr = logmsg + 3; /* last digit of the log level */ |
| 1296 | do { |
| 1297 | *log_ptr = '0' + fac_level % 10; |
| 1298 | fac_level /= 10; |
| 1299 | log_ptr--; |
| 1300 | } while (fac_level && log_ptr > logmsg); |
| 1301 | *log_ptr = '<'; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1302 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1303 | /* 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] | 1304 | |
| 1305 | #ifndef MSG_NOSIGNAL |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1306 | sendto(logfd, log_ptr, dataptr + data_len - log_ptr, MSG_DONTWAIT, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1307 | (struct sockaddr *)sa[nbloggers], sizeof(**sa)); |
| 1308 | #else |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1309 | 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] | 1310 | (struct sockaddr *)sa[nbloggers], sizeof(**sa)); |
| 1311 | #endif |
| 1312 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | |
| 1316 | /* sets <tv> to the current time */ |
| 1317 | static inline struct timeval *tv_now(struct timeval *tv) { |
| 1318 | if (tv) |
| 1319 | gettimeofday(tv, NULL); |
| 1320 | return tv; |
| 1321 | } |
| 1322 | |
| 1323 | /* |
| 1324 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 1325 | */ |
| 1326 | static inline struct timeval *tv_delayfrom(struct timeval *tv, struct timeval *from, int ms) { |
| 1327 | if (!tv || !from) |
| 1328 | return NULL; |
| 1329 | tv->tv_usec = from->tv_usec + (ms%1000)*1000; |
| 1330 | tv->tv_sec = from->tv_sec + (ms/1000); |
| 1331 | while (tv->tv_usec >= 1000000) { |
| 1332 | tv->tv_usec -= 1000000; |
| 1333 | tv->tv_sec++; |
| 1334 | } |
| 1335 | return tv; |
| 1336 | } |
| 1337 | |
| 1338 | /* |
| 1339 | * 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] | 1340 | * 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] | 1341 | */ |
| 1342 | static inline int tv_cmp(struct timeval *tv1, struct timeval *tv2) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1343 | if (tv1->tv_sec < tv2->tv_sec) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1344 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1345 | else if (tv1->tv_sec > tv2->tv_sec) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1346 | return 1; |
| 1347 | else if (tv1->tv_usec < tv2->tv_usec) |
| 1348 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1349 | else if (tv1->tv_usec > tv2->tv_usec) |
| 1350 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1351 | else |
| 1352 | return 0; |
| 1353 | } |
| 1354 | |
| 1355 | /* |
| 1356 | * returns the absolute difference, in ms, between tv1 and tv2 |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1357 | * Must not be used when either argument is eternity. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1358 | */ |
| 1359 | unsigned long tv_delta(struct timeval *tv1, struct timeval *tv2) { |
| 1360 | int cmp; |
| 1361 | unsigned long ret; |
| 1362 | |
| 1363 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1364 | cmp = tv_cmp(tv1, tv2); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1365 | if (!cmp) |
| 1366 | return 0; /* same dates, null diff */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1367 | else if (cmp < 0) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1368 | struct timeval *tmp = tv1; |
| 1369 | tv1 = tv2; |
| 1370 | tv2 = tmp; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1371 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1372 | ret = (tv1->tv_sec - tv2->tv_sec) * 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1373 | if (tv1->tv_usec > tv2->tv_usec) |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1374 | ret += (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1375 | else |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1376 | ret -= (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1377 | return (unsigned long) ret; |
| 1378 | } |
| 1379 | |
| 1380 | /* |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1381 | * returns the difference, in ms, between tv1 and tv2 |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1382 | * Must not be used when either argument is eternity. |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1383 | */ |
| 1384 | static inline unsigned long tv_diff(struct timeval *tv1, struct timeval *tv2) { |
| 1385 | unsigned long ret; |
| 1386 | |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 1387 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
| 1388 | if (tv2->tv_usec > tv1->tv_usec) |
| 1389 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1390 | else |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 1391 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1392 | return (unsigned long) ret; |
| 1393 | } |
| 1394 | |
| 1395 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1396 | * 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] | 1397 | * 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] | 1398 | */ |
| 1399 | static inline int tv_cmp_ms(struct timeval *tv1, struct timeval *tv2) { |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1400 | if (tv1->tv_sec == tv2->tv_sec) { |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1401 | if (tv2->tv_usec >= tv1->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1402 | return -1; |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1403 | else if (tv1->tv_usec >= tv2->tv_usec + 1000) |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1404 | return 1; |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1405 | else |
| 1406 | return 0; |
| 1407 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1408 | else if ((tv2->tv_sec > tv1->tv_sec + 1) || |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1409 | ((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] | 1410 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1411 | else if ((tv1->tv_sec > tv2->tv_sec + 1) || |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1412 | ((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] | 1413 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1414 | else |
| 1415 | return 0; |
| 1416 | } |
| 1417 | |
| 1418 | /* |
| 1419 | * returns the remaining time between tv1=now and event=tv2 |
| 1420 | * if tv2 is passed, 0 is returned. |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1421 | * Must not be used when either argument is eternity. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1422 | */ |
| 1423 | static inline unsigned long tv_remain(struct timeval *tv1, struct timeval *tv2) { |
| 1424 | unsigned long ret; |
| 1425 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1426 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 1427 | return 0; /* event elapsed */ |
| 1428 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1429 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1430 | if (tv2->tv_usec > tv1->tv_usec) |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1431 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1432 | else |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1433 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1434 | return (unsigned long) ret; |
| 1435 | } |
| 1436 | |
| 1437 | |
| 1438 | /* |
| 1439 | * zeroes a struct timeval |
| 1440 | */ |
| 1441 | |
| 1442 | static inline struct timeval *tv_eternity(struct timeval *tv) { |
| 1443 | tv->tv_sec = tv->tv_usec = 0; |
| 1444 | return tv; |
| 1445 | } |
| 1446 | |
| 1447 | /* |
| 1448 | * returns 1 if tv is null, else 0 |
| 1449 | */ |
| 1450 | static inline int tv_iseternity(struct timeval *tv) { |
| 1451 | if (tv->tv_sec == 0 && tv->tv_usec == 0) |
| 1452 | return 1; |
| 1453 | else |
| 1454 | return 0; |
| 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 1459 | * considering that 0 is the eternity. |
| 1460 | */ |
| 1461 | static inline int tv_cmp2(struct timeval *tv1, struct timeval *tv2) { |
| 1462 | if (tv_iseternity(tv1)) |
| 1463 | if (tv_iseternity(tv2)) |
| 1464 | return 0; /* same */ |
| 1465 | else |
| 1466 | return 1; /* tv1 later than tv2 */ |
| 1467 | else if (tv_iseternity(tv2)) |
| 1468 | return -1; /* tv2 later than tv1 */ |
| 1469 | |
| 1470 | if (tv1->tv_sec > tv2->tv_sec) |
| 1471 | return 1; |
| 1472 | else if (tv1->tv_sec < tv2->tv_sec) |
| 1473 | return -1; |
| 1474 | else if (tv1->tv_usec > tv2->tv_usec) |
| 1475 | return 1; |
| 1476 | else if (tv1->tv_usec < tv2->tv_usec) |
| 1477 | return -1; |
| 1478 | else |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
| 1482 | /* |
| 1483 | * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 1484 | * considering that 0 is the eternity. |
| 1485 | */ |
| 1486 | static inline int tv_cmp2_ms(struct timeval *tv1, struct timeval *tv2) { |
| 1487 | if (tv_iseternity(tv1)) |
| 1488 | if (tv_iseternity(tv2)) |
| 1489 | return 0; /* same */ |
| 1490 | else |
| 1491 | return 1; /* tv1 later than tv2 */ |
| 1492 | else if (tv_iseternity(tv2)) |
| 1493 | return -1; /* tv2 later than tv1 */ |
| 1494 | |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1495 | if (tv1->tv_sec == tv2->tv_sec) { |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1496 | if (tv1->tv_usec >= tv2->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1497 | return 1; |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1498 | else if (tv2->tv_usec >= tv1->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1499 | return -1; |
| 1500 | else |
| 1501 | return 0; |
| 1502 | } |
| 1503 | else if ((tv1->tv_sec > tv2->tv_sec + 1) || |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1504 | ((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] | 1505 | return 1; |
| 1506 | else if ((tv2->tv_sec > tv1->tv_sec + 1) || |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1507 | ((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] | 1508 | return -1; |
| 1509 | else |
| 1510 | return 0; |
| 1511 | } |
| 1512 | |
| 1513 | /* |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1514 | * returns the remaining time between tv1=now and event=tv2 |
| 1515 | * if tv2 is passed, 0 is returned. |
| 1516 | * Returns TIME_ETERNITY if tv2 is eternity. |
| 1517 | */ |
| 1518 | static inline unsigned long tv_remain2(struct timeval *tv1, struct timeval *tv2) { |
| 1519 | unsigned long ret; |
| 1520 | |
| 1521 | if (tv_iseternity(tv2)) |
| 1522 | return TIME_ETERNITY; |
| 1523 | |
| 1524 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 1525 | return 0; /* event elapsed */ |
| 1526 | |
| 1527 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
| 1528 | if (tv2->tv_usec > tv1->tv_usec) |
| 1529 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
| 1530 | else |
| 1531 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
| 1532 | return (unsigned long) ret; |
| 1533 | } |
| 1534 | |
| 1535 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1536 | * returns the first event between tv1 and tv2 into tvmin. |
| 1537 | * a zero tv is ignored. tvmin is returned. |
| 1538 | */ |
| 1539 | static inline struct timeval *tv_min(struct timeval *tvmin, |
| 1540 | struct timeval *tv1, struct timeval *tv2) { |
| 1541 | |
| 1542 | if (tv_cmp2(tv1, tv2) <= 0) |
| 1543 | *tvmin = *tv1; |
| 1544 | else |
| 1545 | *tvmin = *tv2; |
| 1546 | |
| 1547 | return tvmin; |
| 1548 | } |
| 1549 | |
| 1550 | |
| 1551 | |
| 1552 | /***********************************************************/ |
| 1553 | /* fd management ***************************************/ |
| 1554 | /***********************************************************/ |
| 1555 | |
| 1556 | |
| 1557 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1558 | /* Deletes an FD from the fdsets, and recomputes the maxfd limit. |
| 1559 | * The file descriptor is also closed. |
| 1560 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1561 | static inline void fd_delete(int fd) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1562 | FD_CLR(fd, StaticReadEvent); |
| 1563 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 08dedbe | 2005-12-18 01:13:48 +0100 | [diff] [blame] | 1564 | #if defined(ENABLE_EPOLL) |
| 1565 | if (PrevReadEvent) { |
| 1566 | FD_CLR(fd, PrevReadEvent); |
| 1567 | FD_CLR(fd, PrevWriteEvent); |
| 1568 | } |
| 1569 | #endif |
| 1570 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1571 | close(fd); |
| 1572 | fdtab[fd].state = FD_STCLOSE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1573 | |
| 1574 | while ((maxfd-1 >= 0) && (fdtab[maxfd-1].state == FD_STCLOSE)) |
| 1575 | maxfd--; |
| 1576 | } |
| 1577 | |
| 1578 | /* recomputes the maxfd limit from the fd */ |
| 1579 | static inline void fd_insert(int fd) { |
| 1580 | if (fd+1 > maxfd) |
| 1581 | maxfd = fd+1; |
| 1582 | } |
| 1583 | |
| 1584 | /*************************************************************/ |
| 1585 | /* task management ***************************************/ |
| 1586 | /*************************************************************/ |
| 1587 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1588 | /* puts the task <t> in run queue <q>, and returns <t> */ |
| 1589 | static inline struct task *task_wakeup(struct task **q, struct task *t) { |
| 1590 | if (t->state == TASK_RUNNING) |
| 1591 | return t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1592 | else { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1593 | t->rqnext = *q; |
| 1594 | t->state = TASK_RUNNING; |
| 1595 | return *q = t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1596 | } |
| 1597 | } |
| 1598 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1599 | /* removes the task <t> from the queue <q> |
| 1600 | * <s> MUST be <q>'s first task. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1601 | * set the run queue to point to the next one, and return it |
| 1602 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1603 | static inline struct task *task_sleep(struct task **q, struct task *t) { |
| 1604 | if (t->state == TASK_RUNNING) { |
| 1605 | *q = t->rqnext; |
| 1606 | t->state = TASK_IDLE; /* tell that s has left the run queue */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1607 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1608 | return *q; /* return next running task */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1612 | * 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] | 1613 | * from the run queue. A pointer to the task itself is returned. |
| 1614 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1615 | static inline struct task *task_delete(struct task *t) { |
| 1616 | t->prev->next = t->next; |
| 1617 | t->next->prev = t->prev; |
| 1618 | return t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1622 | * 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] | 1623 | */ |
| 1624 | static inline void task_free(struct task *t) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1625 | pool_free(task, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1626 | } |
| 1627 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1628 | /* 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] | 1629 | * may be only moved or left where it was, depending on its timing requirements. |
| 1630 | * <task> is returned. |
| 1631 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1632 | struct task *task_queue(struct task *task) { |
| 1633 | struct task *list = task->wq; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1634 | struct task *start_from; |
| 1635 | |
| 1636 | /* first, test if the task was already in a list */ |
| 1637 | if (task->prev == NULL) { |
| 1638 | // start_from = list; |
| 1639 | start_from = list->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1640 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1641 | stats_tsk_new++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1642 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1643 | /* insert the unlinked <task> into the list, searching back from the last entry */ |
| 1644 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1645 | start_from = start_from->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1646 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1647 | stats_tsk_nsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1648 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | // while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1652 | // start_from = start_from->next; |
| 1653 | // stats_tsk_nsrch++; |
| 1654 | // } |
| 1655 | } |
| 1656 | else if (task->prev == list || |
| 1657 | tv_cmp2(&task->expire, &task->prev->expire) >= 0) { /* walk right */ |
| 1658 | start_from = task->next; |
| 1659 | if (start_from == list || tv_cmp2(&task->expire, &start_from->expire) <= 0) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1660 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1661 | stats_tsk_good++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1662 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1663 | return task; /* it's already in the right place */ |
| 1664 | } |
| 1665 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1666 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1667 | stats_tsk_right++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1668 | #endif |
| 1669 | |
| 1670 | /* if the task is not at the right place, there's little chance that |
| 1671 | * it has only shifted a bit, and it will nearly always be queued |
| 1672 | * at the end of the list because of constant timeouts |
| 1673 | * (observed in real case). |
| 1674 | */ |
| 1675 | #ifndef WE_REALLY_THINK_THAT_THIS_TASK_MAY_HAVE_SHIFTED |
| 1676 | start_from = list->prev; /* assume we'll queue to the end of the list */ |
| 1677 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1678 | start_from = start_from->prev; |
| 1679 | #if STATTIME > 0 |
| 1680 | stats_tsk_lsrch++; |
| 1681 | #endif |
| 1682 | } |
| 1683 | #else /* WE_REALLY_... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1684 | /* insert the unlinked <task> into the list, searching after position <start_from> */ |
| 1685 | while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1686 | start_from = start_from->next; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1687 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1688 | stats_tsk_rsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1689 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1690 | } |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1691 | #endif /* WE_REALLY_... */ |
| 1692 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1693 | /* we need to unlink it now */ |
| 1694 | task_delete(task); |
| 1695 | } |
| 1696 | else { /* walk left. */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1697 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1698 | stats_tsk_left++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1699 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1700 | #ifdef LEFT_TO_TOP /* not very good */ |
| 1701 | start_from = list; |
| 1702 | while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1703 | start_from = start_from->next; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1704 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1705 | stats_tsk_lsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1706 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1707 | } |
| 1708 | #else |
| 1709 | start_from = task->prev->prev; /* valid because of the previous test above */ |
| 1710 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1711 | start_from = start_from->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1712 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1713 | stats_tsk_lsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1714 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1715 | } |
| 1716 | #endif |
| 1717 | /* we need to unlink it now */ |
| 1718 | task_delete(task); |
| 1719 | } |
| 1720 | task->prev = start_from; |
| 1721 | task->next = start_from->next; |
| 1722 | task->next->prev = task; |
| 1723 | start_from->next = task; |
| 1724 | return task; |
| 1725 | } |
| 1726 | |
| 1727 | |
| 1728 | /*********************************************************************/ |
| 1729 | /* more specific functions ***************************************/ |
| 1730 | /*********************************************************************/ |
| 1731 | |
| 1732 | /* some prototypes */ |
| 1733 | static int maintain_proxies(void); |
| 1734 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1735 | /* This either returns the sockname or the original destination address. Code |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1736 | * inspired from Patrick Schaaf's example of nf_getsockname() implementation. |
| 1737 | */ |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1738 | 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] | 1739 | #if defined(TPROXY) && defined(SO_ORIGINAL_DST) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1740 | return getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, (void *)sa, salen); |
| 1741 | #else |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1742 | #if defined(TPROXY) && defined(USE_GETSOCKNAME) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1743 | return getsockname(fd, (struct sockaddr *)sa, salen); |
| 1744 | #else |
| 1745 | return -1; |
| 1746 | #endif |
| 1747 | #endif |
| 1748 | } |
| 1749 | |
| 1750 | /* |
| 1751 | * frees the context associated to a session. It must have been removed first. |
| 1752 | */ |
| 1753 | static inline void session_free(struct session *s) { |
| 1754 | if (s->req) |
| 1755 | pool_free(buffer, s->req); |
| 1756 | if (s->rep) |
| 1757 | pool_free(buffer, s->rep); |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1758 | |
| 1759 | if (s->rsp_cap != NULL) { |
| 1760 | struct cap_hdr *h; |
| 1761 | for (h = s->proxy->rsp_cap; h; h = h->next) { |
| 1762 | if (s->rsp_cap[h->index] != NULL) |
| 1763 | pool_free_to(h->pool, s->rsp_cap[h->index]); |
| 1764 | } |
| 1765 | pool_free_to(s->proxy->rsp_cap_pool, s->rsp_cap); |
| 1766 | } |
| 1767 | if (s->req_cap != NULL) { |
| 1768 | struct cap_hdr *h; |
| 1769 | for (h = s->proxy->req_cap; h; h = h->next) { |
| 1770 | if (s->req_cap[h->index] != NULL) |
| 1771 | pool_free_to(h->pool, s->req_cap[h->index]); |
| 1772 | } |
| 1773 | pool_free_to(s->proxy->req_cap_pool, s->req_cap); |
| 1774 | } |
| 1775 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1776 | if (s->logs.uri) |
| 1777 | pool_free(requri, s->logs.uri); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1778 | if (s->logs.cli_cookie) |
| 1779 | pool_free(capture, s->logs.cli_cookie); |
| 1780 | if (s->logs.srv_cookie) |
| 1781 | pool_free(capture, s->logs.srv_cookie); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1782 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1783 | pool_free(session, s); |
| 1784 | } |
| 1785 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1786 | |
| 1787 | /* |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1788 | * This function tries to find a running server for the proxy <px>. A first |
| 1789 | * pass looks for active servers, and if none is found, a second pass also |
| 1790 | * looks for backup servers. |
| 1791 | * If no valid server is found, NULL is returned and px->cursrv is left undefined. |
| 1792 | */ |
| 1793 | static inline struct server *find_server(struct proxy *px) { |
| 1794 | struct server *srv = px->cursrv; |
willy tarreau | 72e583d | 2006-03-23 11:27:02 +0100 | [diff] [blame] | 1795 | struct server *end; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1796 | int ignore_backup = 1; |
| 1797 | |
| 1798 | do { |
willy tarreau | 72e583d | 2006-03-23 11:27:02 +0100 | [diff] [blame] | 1799 | if (srv == NULL) |
| 1800 | srv = px->srv; |
| 1801 | end = srv; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1802 | do { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1803 | if (srv->state & SRV_RUNNING |
| 1804 | && !((srv->state & SRV_BACKUP) && ignore_backup)) |
| 1805 | return srv; |
| 1806 | srv = srv->next; |
willy tarreau | 72e583d | 2006-03-23 11:27:02 +0100 | [diff] [blame] | 1807 | if (srv == NULL) |
| 1808 | srv = px->srv; |
| 1809 | } while (srv != end); |
Willy TARREAU | 3481c46 | 2006-03-01 22:37:57 +0100 | [diff] [blame] | 1810 | |
| 1811 | /* By default, we look for the first backup server if all others are |
| 1812 | * DOWN. But in some cases, it may be desirable to load-balance across |
| 1813 | * all backup servers. |
| 1814 | */ |
| 1815 | if (!(px->options & PR_O_USE_ALL_BK)) |
| 1816 | srv = px->srv; |
| 1817 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1818 | } while (ignore_backup--); |
| 1819 | return NULL; |
| 1820 | } |
| 1821 | |
| 1822 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1823 | * 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] | 1824 | * is set, or to the dispatch server if (s->direct) is 0. |
| 1825 | * It can return one of : |
| 1826 | * - SN_ERR_NONE if everything's OK |
| 1827 | * - SN_ERR_SRVTO if there are no more servers |
| 1828 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1829 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1830 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1831 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1832 | * 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] | 1833 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1834 | int connect_server(struct session *s) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1835 | int fd; |
| 1836 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 1837 | #ifdef DEBUG_FULL |
| 1838 | fprintf(stderr,"connect_server : s=%p\n",s); |
| 1839 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1840 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 1841 | if (s->flags & SN_DIRECT) { /* srv cannot be null */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1842 | s->srv_addr = s->srv->addr; |
| 1843 | } |
| 1844 | else if (s->proxy->options & PR_O_BALANCE) { |
| 1845 | if (s->proxy->options & PR_O_BALANCE_RR) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1846 | struct server *srv; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1847 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1848 | srv = find_server(s->proxy); |
| 1849 | |
| 1850 | if (srv == NULL) /* no server left */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1851 | return SN_ERR_SRVTO; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1852 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1853 | s->srv_addr = srv->addr; |
| 1854 | s->srv = srv; |
| 1855 | s->proxy->cursrv = srv->next; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1856 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1857 | else /* unknown balancing algorithm */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1858 | return SN_ERR_INTERNAL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1859 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1860 | else if (*(int *)&s->proxy->dispatch_addr.sin_addr) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1861 | /* connect to the defined dispatch addr */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1862 | s->srv_addr = s->proxy->dispatch_addr; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1863 | } |
| 1864 | else if (s->proxy->options & PR_O_TRANSP) { |
| 1865 | /* in transparent mode, use the original dest addr if no dispatch specified */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1866 | socklen_t salen = sizeof(s->srv_addr); |
| 1867 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1868 | if (get_original_dst(s->cli_fd, &s->srv_addr, &salen) == -1) { |
| 1869 | qfprintf(stderr, "Cannot get original server address.\n"); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1870 | return SN_ERR_INTERNAL; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1871 | } |
| 1872 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1873 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1874 | /* if this server remaps proxied ports, we'll use |
| 1875 | * the port the client connected to with an offset. */ |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1876 | if (s->srv != NULL && s->srv->state & SRV_MAPPORTS) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1877 | struct sockaddr_in sockname; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1878 | socklen_t namelen = sizeof(sockname); |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1879 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1880 | if (!(s->proxy->options & PR_O_TRANSP) || |
| 1881 | get_original_dst(s->cli_fd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1882 | getsockname(s->cli_fd, (struct sockaddr *)&sockname, &namelen); |
| 1883 | s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) + ntohs(sockname.sin_port)); |
| 1884 | } |
| 1885 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1886 | 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] | 1887 | qfprintf(stderr, "Cannot get a server socket.\n"); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1888 | |
| 1889 | if (errno == ENFILE) |
| 1890 | send_log(s->proxy, LOG_EMERG, |
| 1891 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 1892 | s->proxy->id, maxfd); |
| 1893 | else if (errno == EMFILE) |
| 1894 | send_log(s->proxy, LOG_EMERG, |
| 1895 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 1896 | s->proxy->id, maxfd); |
| 1897 | else if (errno == ENOBUFS || errno == ENOMEM) |
| 1898 | send_log(s->proxy, LOG_EMERG, |
| 1899 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 1900 | s->proxy->id, maxfd); |
| 1901 | /* this is a resource error */ |
| 1902 | return SN_ERR_RESOURCE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1903 | } |
| 1904 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1905 | if (fd >= global.maxsock) { |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1906 | /* do not log anything there, it's a normal condition when this option |
| 1907 | * is used to serialize connections to a server ! |
| 1908 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1909 | Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 1910 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1911 | return SN_ERR_PRXCOND; /* it is a configuration limit */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1912 | } |
| 1913 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1914 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
| 1915 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1916 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1917 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1918 | return SN_ERR_INTERNAL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1919 | } |
| 1920 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1921 | if (s->proxy->options & PR_O_TCP_SRV_KA) |
| 1922 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 1923 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1924 | /* allow specific binding : |
| 1925 | * - server-specific at first |
| 1926 | * - proxy-specific next |
| 1927 | */ |
| 1928 | if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) { |
| 1929 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 1930 | if (bind(fd, (struct sockaddr *)&s->srv->source_addr, sizeof(s->srv->source_addr)) == -1) { |
| 1931 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 1932 | s->proxy->id, s->srv->id); |
| 1933 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1934 | send_log(s->proxy, LOG_EMERG, |
| 1935 | "Cannot bind to source address before connect() for server %s/%s.\n", |
| 1936 | s->proxy->id, s->srv->id); |
| 1937 | return SN_ERR_RESOURCE; |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1938 | } |
| 1939 | } |
| 1940 | else if (s->proxy->options & PR_O_BIND_SRC) { |
| 1941 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 1942 | if (bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) { |
| 1943 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", s->proxy->id); |
| 1944 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1945 | send_log(s->proxy, LOG_EMERG, |
| 1946 | "Cannot bind to source address before connect() for server %s/%s.\n", |
| 1947 | s->proxy->id, s->srv->id); |
| 1948 | return SN_ERR_RESOURCE; |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1949 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1950 | } |
| 1951 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1952 | if ((connect(fd, (struct sockaddr *)&s->srv_addr, sizeof(s->srv_addr)) == -1) && |
| 1953 | (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) { |
| 1954 | |
| 1955 | if (errno == EAGAIN || errno == EADDRINUSE) { |
| 1956 | char *msg; |
| 1957 | if (errno == EAGAIN) /* no free ports left, try again later */ |
| 1958 | msg = "no free ports"; |
| 1959 | else |
| 1960 | msg = "local address already in use"; |
| 1961 | |
| 1962 | qfprintf(stderr,"Cannot connect: %s.\n",msg); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1963 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1964 | send_log(s->proxy, LOG_EMERG, |
| 1965 | "Connect() failed for server %s/%s: %s.\n", |
| 1966 | s->proxy->id, s->srv->id, msg); |
| 1967 | return SN_ERR_RESOURCE; |
| 1968 | } else if (errno == ETIMEDOUT) { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1969 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1970 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1971 | return SN_ERR_SRVTO; |
| 1972 | } else { |
| 1973 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1974 | //qfprintf(stderr,"Connect(): %d", errno); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1975 | close(fd); |
| 1976 | return SN_ERR_SRVCL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1977 | } |
| 1978 | } |
| 1979 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1980 | fdtab[fd].owner = s->task; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1981 | fdtab[fd].read = &event_srv_read; |
| 1982 | fdtab[fd].write = &event_srv_write; |
| 1983 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 1984 | |
| 1985 | FD_SET(fd, StaticWriteEvent); /* for connect status */ |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 1986 | #if defined(DEBUG_FULL) && defined(ENABLE_EPOLL) |
| 1987 | if (PrevReadEvent) { |
| 1988 | assert(!(FD_ISSET(fd, PrevReadEvent))); |
| 1989 | assert(!(FD_ISSET(fd, PrevWriteEvent))); |
| 1990 | } |
| 1991 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1992 | |
| 1993 | fd_insert(fd); |
| 1994 | |
| 1995 | if (s->proxy->contimeout) |
| 1996 | tv_delayfrom(&s->cnexpire, &now, s->proxy->contimeout); |
| 1997 | else |
| 1998 | tv_eternity(&s->cnexpire); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1999 | return SN_ERR_NONE; /* connection is OK */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | /* |
| 2003 | * this function is called on a read event from a client socket. |
| 2004 | * It returns 0. |
| 2005 | */ |
| 2006 | int event_cli_read(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2007 | struct task *t = fdtab[fd].owner; |
| 2008 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2009 | struct buffer *b = s->req; |
| 2010 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2011 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2012 | #ifdef DEBUG_FULL |
| 2013 | fprintf(stderr,"event_cli_read : fd=%d, s=%p\n", fd, s); |
| 2014 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2015 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2016 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2017 | #ifdef FILL_BUFFERS |
| 2018 | while (1) |
| 2019 | #else |
| 2020 | do |
| 2021 | #endif |
| 2022 | { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2023 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 2024 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2025 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2026 | } |
| 2027 | else if (b->r > b->w) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2028 | max = b->rlim - b->r; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2029 | } |
| 2030 | else { |
| 2031 | max = b->w - b->r; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2032 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 2033 | * since it means that the rewrite protection has been removed. This |
| 2034 | * implies that the if statement can be removed. |
| 2035 | */ |
| 2036 | if (max > b->rlim - b->data) |
| 2037 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | if (max == 0) { /* not anymore room to store data */ |
| 2041 | FD_CLR(fd, StaticReadEvent); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2042 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2043 | } |
| 2044 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2045 | #ifndef MSG_NOSIGNAL |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2046 | { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2047 | int skerr; |
| 2048 | socklen_t lskerr = sizeof(skerr); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2049 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2050 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2051 | if (skerr) |
| 2052 | ret = -1; |
| 2053 | else |
| 2054 | ret = recv(fd, b->r, max, 0); |
| 2055 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2056 | #else |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2057 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2058 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2059 | if (ret > 0) { |
| 2060 | b->r += ret; |
| 2061 | b->l += ret; |
| 2062 | s->res_cr = RES_DATA; |
| 2063 | |
| 2064 | if (b->r == b->data + BUFSIZE) { |
| 2065 | b->r = b->data; /* wrap around the buffer */ |
| 2066 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2067 | |
| 2068 | b->total += ret; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2069 | /* we hope to read more data or to get a close on next round */ |
| 2070 | continue; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2071 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2072 | else if (ret == 0) { |
| 2073 | s->res_cr = RES_NULL; |
| 2074 | break; |
| 2075 | } |
| 2076 | else if (errno == EAGAIN) {/* ignore EAGAIN */ |
| 2077 | break; |
| 2078 | } |
| 2079 | else { |
| 2080 | s->res_cr = RES_ERROR; |
| 2081 | fdtab[fd].state = FD_STERROR; |
| 2082 | break; |
| 2083 | } |
| 2084 | } /* while(1) */ |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2085 | #ifndef FILL_BUFFERS |
| 2086 | while (0); |
| 2087 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2088 | } |
| 2089 | else { |
| 2090 | s->res_cr = RES_ERROR; |
| 2091 | fdtab[fd].state = FD_STERROR; |
| 2092 | } |
| 2093 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2094 | if (s->res_cr != RES_SILENT) { |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2095 | if (s->proxy->clitimeout && FD_ISSET(fd, StaticReadEvent)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2096 | tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout); |
| 2097 | else |
| 2098 | tv_eternity(&s->crexpire); |
| 2099 | |
| 2100 | task_wakeup(&rq, t); |
| 2101 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2102 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2103 | return 0; |
| 2104 | } |
| 2105 | |
| 2106 | |
| 2107 | /* |
| 2108 | * this function is called on a read event from a server socket. |
| 2109 | * It returns 0. |
| 2110 | */ |
| 2111 | int event_srv_read(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2112 | struct task *t = fdtab[fd].owner; |
| 2113 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2114 | struct buffer *b = s->rep; |
| 2115 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2116 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2117 | #ifdef DEBUG_FULL |
| 2118 | fprintf(stderr,"event_srv_read : fd=%d, s=%p\n", fd, s); |
| 2119 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2120 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2121 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2122 | #ifdef FILL_BUFFERS |
| 2123 | while (1) |
| 2124 | #else |
| 2125 | do |
| 2126 | #endif |
| 2127 | { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2128 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 2129 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2130 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2131 | } |
| 2132 | else if (b->r > b->w) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2133 | max = b->rlim - b->r; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2134 | } |
| 2135 | else { |
| 2136 | max = b->w - b->r; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2137 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 2138 | * since it means that the rewrite protection has been removed. This |
| 2139 | * implies that the if statement can be removed. |
| 2140 | */ |
| 2141 | if (max > b->rlim - b->data) |
| 2142 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | if (max == 0) { /* not anymore room to store data */ |
| 2146 | FD_CLR(fd, StaticReadEvent); |
| 2147 | break; |
| 2148 | } |
| 2149 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2150 | #ifndef MSG_NOSIGNAL |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2151 | { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2152 | int skerr; |
| 2153 | socklen_t lskerr = sizeof(skerr); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2154 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2155 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2156 | if (skerr) |
| 2157 | ret = -1; |
| 2158 | else |
| 2159 | ret = recv(fd, b->r, max, 0); |
| 2160 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2161 | #else |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2162 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2163 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2164 | if (ret > 0) { |
| 2165 | b->r += ret; |
| 2166 | b->l += ret; |
| 2167 | s->res_sr = RES_DATA; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2168 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2169 | if (b->r == b->data + BUFSIZE) { |
| 2170 | b->r = b->data; /* wrap around the buffer */ |
| 2171 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2172 | |
| 2173 | b->total += ret; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2174 | /* we hope to read more data or to get a close on next round */ |
| 2175 | continue; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2176 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2177 | else if (ret == 0) { |
| 2178 | s->res_sr = RES_NULL; |
| 2179 | break; |
| 2180 | } |
| 2181 | else if (errno == EAGAIN) {/* ignore EAGAIN */ |
| 2182 | break; |
| 2183 | } |
| 2184 | else { |
| 2185 | s->res_sr = RES_ERROR; |
| 2186 | fdtab[fd].state = FD_STERROR; |
| 2187 | break; |
| 2188 | } |
| 2189 | } /* while(1) */ |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2190 | #ifndef FILL_BUFFERS |
| 2191 | while (0); |
| 2192 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2193 | } |
| 2194 | else { |
| 2195 | s->res_sr = RES_ERROR; |
| 2196 | fdtab[fd].state = FD_STERROR; |
| 2197 | } |
| 2198 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2199 | if (s->res_sr != RES_SILENT) { |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2200 | if (s->proxy->srvtimeout && FD_ISSET(fd, StaticReadEvent)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2201 | tv_delayfrom(&s->srexpire, &now, s->proxy->srvtimeout); |
| 2202 | else |
| 2203 | tv_eternity(&s->srexpire); |
| 2204 | |
| 2205 | task_wakeup(&rq, t); |
| 2206 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2207 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2208 | return 0; |
| 2209 | } |
| 2210 | |
| 2211 | /* |
| 2212 | * this function is called on a write event from a client socket. |
| 2213 | * It returns 0. |
| 2214 | */ |
| 2215 | int event_cli_write(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2216 | struct task *t = fdtab[fd].owner; |
| 2217 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2218 | struct buffer *b = s->rep; |
| 2219 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2220 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2221 | #ifdef DEBUG_FULL |
| 2222 | fprintf(stderr,"event_cli_write : fd=%d, s=%p\n", fd, s); |
| 2223 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2224 | |
| 2225 | 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] | 2226 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2227 | // max = BUFSIZE; BUG !!!! |
| 2228 | max = 0; |
| 2229 | } |
| 2230 | else if (b->r > b->w) { |
| 2231 | max = b->r - b->w; |
| 2232 | } |
| 2233 | else |
| 2234 | max = b->data + BUFSIZE - b->w; |
| 2235 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2236 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2237 | if (max == 0) { |
| 2238 | s->res_cw = RES_NULL; |
| 2239 | task_wakeup(&rq, t); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2240 | tv_eternity(&s->cwexpire); |
| 2241 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2242 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2243 | } |
| 2244 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2245 | #ifndef MSG_NOSIGNAL |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2246 | { |
| 2247 | int skerr; |
| 2248 | socklen_t lskerr = sizeof(skerr); |
| 2249 | |
| 2250 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2251 | if (skerr) |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2252 | ret = -1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2253 | else |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2254 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2255 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2256 | #else |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2257 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2258 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2259 | |
| 2260 | if (ret > 0) { |
| 2261 | b->l -= ret; |
| 2262 | b->w += ret; |
| 2263 | |
| 2264 | s->res_cw = RES_DATA; |
| 2265 | |
| 2266 | if (b->w == b->data + BUFSIZE) { |
| 2267 | b->w = b->data; /* wrap around the buffer */ |
| 2268 | } |
| 2269 | } |
| 2270 | else if (ret == 0) { |
| 2271 | /* nothing written, just make as if we were never called */ |
| 2272 | // s->res_cw = RES_NULL; |
| 2273 | return 0; |
| 2274 | } |
| 2275 | else if (errno == EAGAIN) /* ignore EAGAIN */ |
| 2276 | return 0; |
| 2277 | else { |
| 2278 | s->res_cw = RES_ERROR; |
| 2279 | fdtab[fd].state = FD_STERROR; |
| 2280 | } |
| 2281 | } |
| 2282 | else { |
| 2283 | s->res_cw = RES_ERROR; |
| 2284 | fdtab[fd].state = FD_STERROR; |
| 2285 | } |
| 2286 | |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2287 | if (s->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2288 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2289 | /* FIXME: to avoid the client to read-time-out during writes, we refresh it */ |
| 2290 | s->crexpire = s->cwexpire; |
| 2291 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2292 | else |
| 2293 | tv_eternity(&s->cwexpire); |
| 2294 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2295 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2296 | return 0; |
| 2297 | } |
| 2298 | |
| 2299 | |
| 2300 | /* |
| 2301 | * this function is called on a write event from a server socket. |
| 2302 | * It returns 0. |
| 2303 | */ |
| 2304 | int event_srv_write(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2305 | struct task *t = fdtab[fd].owner; |
| 2306 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2307 | struct buffer *b = s->req; |
| 2308 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2309 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2310 | #ifdef DEBUG_FULL |
| 2311 | fprintf(stderr,"event_srv_write : fd=%d, s=%p\n", fd, s); |
| 2312 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2313 | |
| 2314 | 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] | 2315 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2316 | // max = BUFSIZE; BUG !!!! |
| 2317 | max = 0; |
| 2318 | } |
| 2319 | else if (b->r > b->w) { |
| 2320 | max = b->r - b->w; |
| 2321 | } |
| 2322 | else |
| 2323 | max = b->data + BUFSIZE - b->w; |
| 2324 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2325 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2326 | if (max == 0) { |
| 2327 | /* 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] | 2328 | if (s->srv_state == SV_STCONN) { |
| 2329 | int skerr; |
| 2330 | socklen_t lskerr = sizeof(skerr); |
| 2331 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2332 | if (skerr) { |
| 2333 | s->res_sw = RES_ERROR; |
| 2334 | fdtab[fd].state = FD_STERROR; |
| 2335 | task_wakeup(&rq, t); |
| 2336 | tv_eternity(&s->swexpire); |
| 2337 | FD_CLR(fd, StaticWriteEvent); |
| 2338 | return 0; |
| 2339 | } |
| 2340 | } |
| 2341 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2342 | s->res_sw = RES_NULL; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2343 | task_wakeup(&rq, t); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2344 | fdtab[fd].state = FD_STREADY; |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2345 | tv_eternity(&s->swexpire); |
| 2346 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2347 | return 0; |
| 2348 | } |
| 2349 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2350 | #ifndef MSG_NOSIGNAL |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2351 | { |
| 2352 | int skerr; |
| 2353 | socklen_t lskerr = sizeof(skerr); |
| 2354 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2355 | if (skerr) |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2356 | ret = -1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2357 | else |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2358 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2359 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2360 | #else |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2361 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2362 | #endif |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2363 | fdtab[fd].state = FD_STREADY; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2364 | if (ret > 0) { |
| 2365 | b->l -= ret; |
| 2366 | b->w += ret; |
| 2367 | |
| 2368 | s->res_sw = RES_DATA; |
| 2369 | |
| 2370 | if (b->w == b->data + BUFSIZE) { |
| 2371 | b->w = b->data; /* wrap around the buffer */ |
| 2372 | } |
| 2373 | } |
| 2374 | else if (ret == 0) { |
| 2375 | /* nothing written, just make as if we were never called */ |
| 2376 | // s->res_sw = RES_NULL; |
| 2377 | return 0; |
| 2378 | } |
| 2379 | else if (errno == EAGAIN) /* ignore EAGAIN */ |
| 2380 | return 0; |
| 2381 | else { |
| 2382 | s->res_sw = RES_ERROR; |
| 2383 | fdtab[fd].state = FD_STERROR; |
| 2384 | } |
| 2385 | } |
| 2386 | else { |
| 2387 | s->res_sw = RES_ERROR; |
| 2388 | fdtab[fd].state = FD_STERROR; |
| 2389 | } |
| 2390 | |
Willy TARREAU | 1cec83c | 2006-03-01 22:33:49 +0100 | [diff] [blame] | 2391 | /* We don't want to re-arm read/write timeouts if we're trying to connect, |
| 2392 | * otherwise it could loop indefinitely ! |
| 2393 | */ |
| 2394 | if (s->srv_state != SV_STCONN) { |
| 2395 | if (s->proxy->srvtimeout) { |
| 2396 | tv_delayfrom(&s->swexpire, &now, s->proxy->srvtimeout); |
| 2397 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 2398 | s->srexpire = s->swexpire; |
| 2399 | } |
| 2400 | else |
| 2401 | tv_eternity(&s->swexpire); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2402 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2403 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2404 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2405 | return 0; |
| 2406 | } |
| 2407 | |
| 2408 | |
| 2409 | /* |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2410 | * returns a message to the client ; the connection is shut down for read, |
| 2411 | * and the request is cleared so that no server connection can be initiated. |
| 2412 | * The client must be in a valid state for this (HEADER, DATA ...). |
| 2413 | * Nothing is performed on the server side. |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2414 | * The reply buffer doesn't need to be empty before this. |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2415 | */ |
| 2416 | void client_retnclose(struct session *s, int len, const char *msg) { |
| 2417 | FD_CLR(s->cli_fd, StaticReadEvent); |
| 2418 | FD_SET(s->cli_fd, StaticWriteEvent); |
| 2419 | tv_eternity(&s->crexpire); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2420 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2421 | shutdown(s->cli_fd, SHUT_RD); |
| 2422 | s->cli_state = CL_STSHUTR; |
| 2423 | strcpy(s->rep->data, msg); |
| 2424 | s->rep->l = len; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2425 | 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] | 2426 | s->rep->r += len; |
| 2427 | s->req->l = 0; |
| 2428 | } |
| 2429 | |
| 2430 | |
| 2431 | /* |
| 2432 | * returns a message into the rep buffer, and flushes the req buffer. |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2433 | * The reply buffer doesn't need to be empty before this. |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2434 | */ |
| 2435 | void client_return(struct session *s, int len, const char *msg) { |
| 2436 | strcpy(s->rep->data, msg); |
| 2437 | s->rep->l = len; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2438 | 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] | 2439 | s->rep->r += len; |
| 2440 | s->req->l = 0; |
| 2441 | } |
| 2442 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2443 | /* |
| 2444 | * send a log for the session when we have enough info about it |
| 2445 | */ |
| 2446 | void sess_log(struct session *s) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2447 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2448 | struct proxy *p = s->proxy; |
| 2449 | int log; |
| 2450 | char *uri; |
| 2451 | char *pxid; |
| 2452 | char *srv; |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2453 | struct tm *tm; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2454 | |
| 2455 | /* This is a first attempt at a better logging system. |
| 2456 | * For now, we rely on send_log() to provide the date, although it obviously |
| 2457 | * is the date of the log and not of the request, and most fields are not |
| 2458 | * computed. |
| 2459 | */ |
| 2460 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2461 | log = p->to_log & ~s->logs.logwait; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2462 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2463 | if (s->cli_addr.ss_family == AF_INET) |
| 2464 | inet_ntop(AF_INET, |
| 2465 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 2466 | pn, sizeof(pn)); |
| 2467 | else |
| 2468 | inet_ntop(AF_INET6, |
| 2469 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 2470 | pn, sizeof(pn)); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2471 | |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2472 | uri = (log & LW_REQ) ? s->logs.uri ? s->logs.uri : "<BADREQ>" : ""; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2473 | pxid = p->id; |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2474 | 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] | 2475 | |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2476 | tm = localtime(&s->logs.tv_accept.tv_sec); |
| 2477 | if (p->to_log & LW_REQ) { |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2478 | char tmpline[MAX_SYSLOG_LEN], *h; |
| 2479 | int hdr; |
| 2480 | |
| 2481 | h = tmpline; |
| 2482 | if (p->to_log & LW_REQHDR && (h < tmpline + sizeof(tmpline) - 10)) { |
| 2483 | *(h++) = ' '; |
| 2484 | *(h++) = '{'; |
| 2485 | for (hdr = 0; hdr < p->nb_req_cap; hdr++) { |
| 2486 | if (hdr) |
| 2487 | *(h++) = '|'; |
| 2488 | if (s->req_cap[hdr] != NULL) |
| 2489 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, '#', hdr_encode_map, s->req_cap[hdr]); |
| 2490 | } |
| 2491 | *(h++) = '}'; |
| 2492 | } |
| 2493 | |
| 2494 | if (p->to_log & LW_RSPHDR && (h < tmpline + sizeof(tmpline) - 7)) { |
| 2495 | *(h++) = ' '; |
| 2496 | *(h++) = '{'; |
| 2497 | for (hdr = 0; hdr < p->nb_rsp_cap; hdr++) { |
| 2498 | if (hdr) |
| 2499 | *(h++) = '|'; |
| 2500 | if (s->rsp_cap[hdr] != NULL) |
| 2501 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, '#', hdr_encode_map, s->rsp_cap[hdr]); |
| 2502 | } |
| 2503 | *(h++) = '}'; |
| 2504 | } |
| 2505 | |
| 2506 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 2507 | *(h++) = ' '; |
| 2508 | *(h++) = '"'; |
| 2509 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, '#', url_encode_map, uri); |
| 2510 | *(h++) = '"'; |
| 2511 | } |
| 2512 | *h = '\0'; |
| 2513 | |
willy tarreau | 0fe3965 | 2005-12-18 01:25:24 +0100 | [diff] [blame] | 2514 | 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%s\n", |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2515 | pn, |
| 2516 | (s->cli_addr.ss_family == AF_INET) ? |
| 2517 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 2518 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2519 | tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900, |
| 2520 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 2521 | pxid, srv, |
| 2522 | s->logs.t_request, |
| 2523 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_request : -1, |
| 2524 | (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] | 2525 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 2526 | s->logs.status, |
| 2527 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.bytes, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2528 | s->logs.cli_cookie ? s->logs.cli_cookie : "-", |
| 2529 | s->logs.srv_cookie ? s->logs.srv_cookie : "-", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2530 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 2531 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 2532 | (p->options & PR_O_COOK_ANY) ? sess_cookie[(s->flags & SN_CK_MASK) >> SN_CK_SHIFT] : '-', |
| 2533 | (p->options & PR_O_COOK_ANY) ? sess_set_cookie[(s->flags & SN_SCK_MASK) >> SN_SCK_SHIFT] : '-', |
willy tarreau | 0fe3965 | 2005-12-18 01:25:24 +0100 | [diff] [blame] | 2534 | p->nbconn, actconn, tmpline); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2535 | } |
| 2536 | else { |
willy tarreau | 0fe3965 | 2005-12-18 01:25:24 +0100 | [diff] [blame] | 2537 | send_log(p, LOG_INFO, "%s:%d [%02d/%s/%04d:%02d:%02d:%02d] %s %s %d/%s%d %s%lld %c%c %d/%d\n", |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2538 | pn, |
| 2539 | (s->cli_addr.ss_family == AF_INET) ? |
| 2540 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 2541 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2542 | tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900, |
| 2543 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2544 | pxid, srv, |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2545 | (s->logs.t_connect >= 0) ? s->logs.t_connect : -1, |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 2546 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 2547 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.bytes, |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2548 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
willy tarreau | 0fe3965 | 2005-12-18 01:25:24 +0100 | [diff] [blame] | 2549 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 2550 | p->nbconn, actconn); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | s->logs.logwait = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2554 | } |
| 2555 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2556 | |
| 2557 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2558 | * 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] | 2559 | * to an accept. It tries to accept as many connections as possible. |
| 2560 | * It returns 0. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2561 | */ |
| 2562 | int event_accept(int fd) { |
| 2563 | struct proxy *p = (struct proxy *)fdtab[fd].owner; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2564 | struct session *s; |
| 2565 | struct task *t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2566 | int cfd; |
willy tarreau | c2becdc | 2006-03-19 19:36:48 +0100 | [diff] [blame] | 2567 | int max_accept; |
| 2568 | |
| 2569 | if (global.nbproc > 1) |
| 2570 | max_accept = 8; /* let other processes catch some connections too */ |
| 2571 | else |
| 2572 | max_accept = -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2573 | |
willy tarreau | c2becdc | 2006-03-19 19:36:48 +0100 | [diff] [blame] | 2574 | while (p->nbconn < p->maxconn && max_accept--) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2575 | struct sockaddr_storage addr; |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2576 | socklen_t laddr = sizeof(addr); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2577 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2578 | if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) { |
| 2579 | switch (errno) { |
| 2580 | case EAGAIN: |
| 2581 | case EINTR: |
| 2582 | case ECONNABORTED: |
| 2583 | return 0; /* nothing more to accept */ |
| 2584 | case ENFILE: |
| 2585 | send_log(p, LOG_EMERG, |
| 2586 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 2587 | p->id, maxfd); |
| 2588 | return 0; |
| 2589 | case EMFILE: |
| 2590 | send_log(p, LOG_EMERG, |
| 2591 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 2592 | p->id, maxfd); |
| 2593 | return 0; |
| 2594 | case ENOBUFS: |
| 2595 | case ENOMEM: |
| 2596 | send_log(p, LOG_EMERG, |
| 2597 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 2598 | p->id, maxfd); |
| 2599 | return 0; |
| 2600 | default: |
| 2601 | return 0; |
| 2602 | } |
| 2603 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2604 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2605 | if ((s = pool_alloc(session)) == NULL) { /* disable this proxy for a while */ |
| 2606 | Alert("out of memory in event_accept().\n"); |
| 2607 | FD_CLR(fd, StaticReadEvent); |
| 2608 | p->state = PR_STIDLE; |
| 2609 | close(cfd); |
| 2610 | return 0; |
| 2611 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2612 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2613 | /* if this session comes from a known monitoring system, we want to ignore |
| 2614 | * it as soon as possible, which means closing it immediately for TCP. |
| 2615 | */ |
| 2616 | s->flags = 0; |
| 2617 | if (addr.ss_family == AF_INET && |
| 2618 | p->mon_mask.s_addr && |
| 2619 | (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr) { |
| 2620 | if (p->mode == PR_MODE_TCP) { |
| 2621 | close(cfd); |
| 2622 | pool_free(session, s); |
| 2623 | continue; |
| 2624 | } |
| 2625 | s->flags |= SN_MONITOR; |
| 2626 | } |
| 2627 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2628 | if ((t = pool_alloc(task)) == NULL) { /* disable this proxy for a while */ |
| 2629 | Alert("out of memory in event_accept().\n"); |
| 2630 | FD_CLR(fd, StaticReadEvent); |
| 2631 | p->state = PR_STIDLE; |
| 2632 | close(cfd); |
| 2633 | pool_free(session, s); |
| 2634 | return 0; |
| 2635 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2636 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2637 | s->cli_addr = addr; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2638 | if (cfd >= global.maxsock) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2639 | Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 2640 | close(cfd); |
| 2641 | pool_free(task, t); |
| 2642 | pool_free(session, s); |
| 2643 | return 0; |
| 2644 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2645 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2646 | if ((fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) || |
| 2647 | (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, |
| 2648 | (char *) &one, sizeof(one)) == -1)) { |
| 2649 | Alert("accept(): cannot set the socket in non blocking mode. Giving up\n"); |
| 2650 | close(cfd); |
| 2651 | pool_free(task, t); |
| 2652 | pool_free(session, s); |
| 2653 | return 0; |
| 2654 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2655 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2656 | if (p->options & PR_O_TCP_CLI_KA) |
| 2657 | setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 2658 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2659 | t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */ |
| 2660 | t->wq = LIST_HEAD(wait_queue); /* but already has a wait queue assigned */ |
| 2661 | t->state = TASK_IDLE; |
| 2662 | t->process = process_session; |
| 2663 | t->context = s; |
| 2664 | |
| 2665 | s->task = t; |
| 2666 | s->proxy = p; |
| 2667 | s->cli_state = (p->mode == PR_MODE_HTTP) ? CL_STHEADERS : CL_STDATA; /* no HTTP headers for non-HTTP proxies */ |
| 2668 | s->srv_state = SV_STIDLE; |
| 2669 | s->req = s->rep = NULL; /* will be allocated later */ |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 2670 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2671 | s->res_cr = s->res_cw = s->res_sr = s->res_sw = RES_SILENT; |
| 2672 | s->cli_fd = cfd; |
| 2673 | s->srv_fd = -1; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2674 | s->srv = NULL; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2675 | s->conn_retries = p->conn_retries; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2676 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2677 | if (s->flags & SN_MONITOR) |
| 2678 | s->logs.logwait = 0; |
| 2679 | else |
| 2680 | s->logs.logwait = p->to_log; |
| 2681 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2682 | s->logs.tv_accept = now; |
| 2683 | s->logs.t_request = -1; |
| 2684 | s->logs.t_connect = -1; |
| 2685 | s->logs.t_data = -1; |
| 2686 | s->logs.t_close = 0; |
| 2687 | s->logs.uri = NULL; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2688 | s->logs.cli_cookie = NULL; |
| 2689 | s->logs.srv_cookie = NULL; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2690 | s->logs.status = -1; |
| 2691 | s->logs.bytes = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2692 | |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2693 | s->uniq_id = totalconn; |
| 2694 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2695 | if (p->nb_req_cap > 0) { |
| 2696 | if ((s->req_cap = |
| 2697 | pool_alloc_from(p->req_cap_pool, p->nb_req_cap*sizeof(char *))) |
| 2698 | == NULL) { /* no memory */ |
| 2699 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2700 | pool_free(task, t); |
| 2701 | pool_free(session, s); |
| 2702 | return 0; |
| 2703 | } |
| 2704 | memset(s->req_cap, 0, p->nb_req_cap*sizeof(char *)); |
| 2705 | } |
| 2706 | else |
| 2707 | s->req_cap = NULL; |
| 2708 | |
| 2709 | if (p->nb_rsp_cap > 0) { |
| 2710 | if ((s->rsp_cap = |
| 2711 | pool_alloc_from(p->rsp_cap_pool, p->nb_rsp_cap*sizeof(char *))) |
| 2712 | == NULL) { /* no memory */ |
| 2713 | if (s->req_cap != NULL) |
| 2714 | pool_free_to(p->req_cap_pool, s->req_cap); |
| 2715 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2716 | pool_free(task, t); |
| 2717 | pool_free(session, s); |
| 2718 | return 0; |
| 2719 | } |
| 2720 | memset(s->rsp_cap, 0, p->nb_rsp_cap*sizeof(char *)); |
| 2721 | } |
| 2722 | else |
| 2723 | s->rsp_cap = NULL; |
| 2724 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2725 | if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP) |
| 2726 | && (p->logfac1 >= 0 || p->logfac2 >= 0)) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2727 | struct sockaddr_storage sockname; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2728 | socklen_t namelen = sizeof(sockname); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2729 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2730 | if (addr.ss_family != AF_INET || |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2731 | !(s->proxy->options & PR_O_TRANSP) || |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2732 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2733 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2734 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2735 | if (p->to_log) { |
| 2736 | /* we have the client ip */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2737 | if (s->logs.logwait & LW_CLIP) |
| 2738 | if (!(s->logs.logwait &= ~LW_CLIP)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2739 | sess_log(s); |
| 2740 | } |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2741 | else if (s->cli_addr.ss_family == AF_INET) { |
| 2742 | char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN]; |
| 2743 | if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sockname)->sin_addr, |
| 2744 | sn, sizeof(sn)) && |
| 2745 | inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 2746 | pn, sizeof(pn))) { |
| 2747 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 2748 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port), |
| 2749 | sn, ntohs(((struct sockaddr_in *)&sockname)->sin_port), |
| 2750 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 2751 | } |
| 2752 | } |
| 2753 | else { |
| 2754 | char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN]; |
| 2755 | if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sockname)->sin6_addr, |
| 2756 | sn, sizeof(sn)) && |
| 2757 | inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr, |
| 2758 | pn, sizeof(pn))) { |
| 2759 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 2760 | pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 2761 | sn, ntohs(((struct sockaddr_in6 *)&sockname)->sin6_port), |
| 2762 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 2763 | } |
| 2764 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2765 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2766 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 2767 | 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] | 2768 | struct sockaddr_in sockname; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2769 | socklen_t namelen = sizeof(sockname); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2770 | int len; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2771 | if (addr.ss_family != AF_INET || |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2772 | !(s->proxy->options & PR_O_TRANSP) || |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2773 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2774 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2775 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2776 | if (s->cli_addr.ss_family == AF_INET) { |
| 2777 | char pn[INET_ADDRSTRLEN]; |
| 2778 | inet_ntop(AF_INET, |
| 2779 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 2780 | pn, sizeof(pn)); |
| 2781 | |
| 2782 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 2783 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 2784 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port)); |
| 2785 | } |
| 2786 | else { |
| 2787 | char pn[INET6_ADDRSTRLEN]; |
| 2788 | inet_ntop(AF_INET6, |
| 2789 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 2790 | pn, sizeof(pn)); |
| 2791 | |
| 2792 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 2793 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 2794 | pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port)); |
| 2795 | } |
| 2796 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2797 | write(1, trash, len); |
| 2798 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2799 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2800 | if ((s->req = pool_alloc(buffer)) == NULL) { /* no memory */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2801 | if (s->rsp_cap != NULL) |
| 2802 | pool_free_to(p->rsp_cap_pool, s->rsp_cap); |
| 2803 | if (s->req_cap != NULL) |
| 2804 | pool_free_to(p->req_cap_pool, s->req_cap); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2805 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2806 | pool_free(task, t); |
| 2807 | pool_free(session, s); |
| 2808 | return 0; |
| 2809 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2810 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2811 | s->req->l = 0; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2812 | s->req->total = 0; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2813 | s->req->h = s->req->r = s->req->lr = s->req->w = s->req->data; /* r and w will be reset further */ |
| 2814 | s->req->rlim = s->req->data + BUFSIZE; |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2815 | if (s->cli_state == CL_STHEADERS) /* reserve some space for header rewriting */ |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2816 | s->req->rlim -= MAXREWRITE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2817 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2818 | if ((s->rep = pool_alloc(buffer)) == NULL) { /* no memory */ |
| 2819 | pool_free(buffer, s->req); |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2820 | if (s->rsp_cap != NULL) |
| 2821 | pool_free_to(p->rsp_cap_pool, s->rsp_cap); |
| 2822 | if (s->req_cap != NULL) |
| 2823 | pool_free_to(p->req_cap_pool, s->req_cap); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2824 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2825 | pool_free(task, t); |
| 2826 | pool_free(session, s); |
| 2827 | return 0; |
| 2828 | } |
| 2829 | s->rep->l = 0; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2830 | s->rep->total = 0; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2831 | 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] | 2832 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2833 | fdtab[cfd].read = &event_cli_read; |
| 2834 | fdtab[cfd].write = &event_cli_write; |
| 2835 | fdtab[cfd].owner = t; |
| 2836 | fdtab[cfd].state = FD_STREADY; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2837 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2838 | if ((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) || |
| 2839 | (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK))) |
| 2840 | /* Either we got a request from a monitoring system on an HTTP instance, |
| 2841 | * or we're in health check mode with the 'httpchk' option enabled. In |
| 2842 | * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit. |
| 2843 | */ |
| 2844 | client_retnclose(s, 19, "HTTP/1.0 200 OK\r\n\r\n"); /* forge a 200 response */ |
| 2845 | else if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */ |
| 2846 | client_retnclose(s, 3, "OK\n"); /* forge an "OK" response */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2847 | } |
| 2848 | else { |
| 2849 | FD_SET(cfd, StaticReadEvent); |
| 2850 | } |
| 2851 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2852 | #if defined(DEBUG_FULL) && defined(ENABLE_EPOLL) |
| 2853 | if (PrevReadEvent) { |
| 2854 | assert(!(FD_ISSET(cfd, PrevReadEvent))); |
| 2855 | assert(!(FD_ISSET(cfd, PrevWriteEvent))); |
| 2856 | } |
| 2857 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2858 | fd_insert(cfd); |
| 2859 | |
| 2860 | tv_eternity(&s->cnexpire); |
| 2861 | tv_eternity(&s->srexpire); |
| 2862 | tv_eternity(&s->swexpire); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2863 | tv_eternity(&s->crexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2864 | tv_eternity(&s->cwexpire); |
| 2865 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2866 | if (s->proxy->clitimeout) { |
| 2867 | if (FD_ISSET(cfd, StaticReadEvent)) |
| 2868 | tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout); |
| 2869 | if (FD_ISSET(cfd, StaticWriteEvent)) |
| 2870 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
| 2871 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2872 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2873 | tv_min(&t->expire, &s->crexpire, &s->cwexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2874 | |
| 2875 | task_queue(t); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2876 | |
| 2877 | if (p->mode != PR_MODE_HEALTH) |
| 2878 | task_wakeup(&rq, t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2879 | |
| 2880 | p->nbconn++; |
| 2881 | actconn++; |
| 2882 | totalconn++; |
| 2883 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2884 | // 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] | 2885 | } /* end of while (p->nbconn < p->maxconn) */ |
| 2886 | return 0; |
| 2887 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2888 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2889 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2890 | /* |
| 2891 | * This function is used only for server health-checks. It handles |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2892 | * the connection acknowledgement. If the proxy requires HTTP health-checks, |
| 2893 | * 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] | 2894 | * or -1 if an error occured. |
| 2895 | */ |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2896 | int event_srv_chk_w(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2897 | struct task *t = fdtab[fd].owner; |
| 2898 | struct server *s = t->context; |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2899 | int skerr; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2900 | socklen_t lskerr = sizeof(skerr); |
| 2901 | |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 2902 | skerr = 1; |
| 2903 | if ((getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) == -1) |
| 2904 | || (skerr != 0)) { |
| 2905 | /* in case of TCP only, this tells us if the connection failed */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2906 | s->result = -1; |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 2907 | fdtab[fd].state = FD_STERROR; |
| 2908 | FD_CLR(fd, StaticWriteEvent); |
| 2909 | } |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 2910 | else if (s->result != -1) { |
| 2911 | /* 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] | 2912 | if (s->proxy->options & PR_O_HTTP_CHK) { |
| 2913 | int ret; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2914 | /* 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] | 2915 | * so we'll send the request, and won't wake the checker up now. |
| 2916 | */ |
| 2917 | #ifndef MSG_NOSIGNAL |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2918 | 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] | 2919 | #else |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2920 | 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] | 2921 | #endif |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 2922 | if (ret == s->proxy->check_len) { |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2923 | FD_SET(fd, StaticReadEvent); /* prepare for reading reply */ |
| 2924 | FD_CLR(fd, StaticWriteEvent); /* nothing more to write */ |
| 2925 | return 0; |
| 2926 | } |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 2927 | else { |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2928 | s->result = -1; |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 2929 | FD_CLR(fd, StaticWriteEvent); |
| 2930 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2931 | } |
| 2932 | else { |
| 2933 | /* good TCP connection is enough */ |
| 2934 | s->result = 1; |
| 2935 | } |
| 2936 | } |
| 2937 | |
| 2938 | task_wakeup(&rq, t); |
| 2939 | return 0; |
| 2940 | } |
| 2941 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2942 | |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2943 | /* |
| 2944 | * This function is used only for server health-checks. It handles |
| 2945 | * the server's reply to an HTTP request. It returns 1 if the server replies |
| 2946 | * 2xx or 3xx (valid responses), or -1 in other cases. |
| 2947 | */ |
| 2948 | int event_srv_chk_r(int fd) { |
| 2949 | char reply[64]; |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 2950 | int len, result; |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2951 | struct task *t = fdtab[fd].owner; |
| 2952 | struct server *s = t->context; |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 2953 | int skerr; |
| 2954 | socklen_t lskerr = sizeof(skerr); |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2955 | |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 2956 | result = len = -1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2957 | |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 2958 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2959 | if (!skerr) { |
| 2960 | #ifndef MSG_NOSIGNAL |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2961 | len = recv(fd, reply, sizeof(reply), 0); |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2962 | #else |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 2963 | /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available |
| 2964 | * but the connection was closed on the remote end. Fortunately, recv still |
| 2965 | * works correctly and we don't need to do the getsockopt() on linux. |
| 2966 | */ |
| 2967 | len = recv(fd, reply, sizeof(reply), MSG_NOSIGNAL); |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2968 | #endif |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 2969 | |
| 2970 | if ((len >= sizeof("HTTP/1.0 000")) && |
| 2971 | !memcmp(reply, "HTTP/1.", 7) && |
| 2972 | (reply[9] == '2' || reply[9] == '3')) /* 2xx or 3xx */ |
| 2973 | result = 1; |
| 2974 | } |
| 2975 | |
| 2976 | if (result == -1) |
| 2977 | fdtab[fd].state = FD_STERROR; |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 2978 | |
| 2979 | if (s->result != -1) |
| 2980 | s->result = result; |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2981 | |
| 2982 | FD_CLR(fd, StaticReadEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2983 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2984 | return 0; |
| 2985 | } |
| 2986 | |
| 2987 | |
| 2988 | /* |
| 2989 | * this function writes the string <str> at position <pos> which must be in buffer <b>, |
| 2990 | * and moves <end> just after the end of <str>. |
| 2991 | * <b>'s parameters (l, r, w, h, lr) are recomputed to be valid after the shift. |
| 2992 | * the shift value (positive or negative) is returned. |
| 2993 | * If there's no space left, the move is not done. |
| 2994 | * |
| 2995 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2996 | int buffer_replace(struct buffer *b, char *pos, char *end, char *str) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2997 | int delta; |
| 2998 | int len; |
| 2999 | |
| 3000 | len = strlen(str); |
| 3001 | delta = len - (end - pos); |
| 3002 | |
| 3003 | if (delta + b->r >= b->data + BUFSIZE) |
| 3004 | return 0; /* no space left */ |
| 3005 | |
| 3006 | /* first, protect the end of the buffer */ |
| 3007 | memmove(end + delta, end, b->data + b->l - end); |
| 3008 | |
| 3009 | /* now, copy str over pos */ |
| 3010 | memcpy(pos, str,len); |
| 3011 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3012 | /* we only move data after the displaced zone */ |
| 3013 | if (b->r > pos) b->r += delta; |
| 3014 | if (b->w > pos) b->w += delta; |
| 3015 | if (b->h > pos) b->h += delta; |
| 3016 | if (b->lr > pos) b->lr += delta; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3017 | b->l += delta; |
| 3018 | |
| 3019 | return delta; |
| 3020 | } |
| 3021 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3022 | /* 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] | 3023 | * len is 0. |
| 3024 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3025 | 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] | 3026 | int delta; |
| 3027 | |
| 3028 | delta = len - (end - pos); |
| 3029 | |
| 3030 | if (delta + b->r >= b->data + BUFSIZE) |
| 3031 | return 0; /* no space left */ |
| 3032 | |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 3033 | if (b->data + b->l < end) |
| 3034 | /* The data has been stolen, we could have crashed. Maybe we should abort() ? */ |
| 3035 | return 0; |
| 3036 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3037 | /* first, protect the end of the buffer */ |
| 3038 | memmove(end + delta, end, b->data + b->l - end); |
| 3039 | |
| 3040 | /* now, copy str over pos */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3041 | if (len) |
| 3042 | memcpy(pos, str, len); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3043 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3044 | /* we only move data after the displaced zone */ |
| 3045 | if (b->r > pos) b->r += delta; |
| 3046 | if (b->w > pos) b->w += delta; |
| 3047 | if (b->h > pos) b->h += delta; |
| 3048 | if (b->lr > pos) b->lr += delta; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3049 | b->l += delta; |
| 3050 | |
| 3051 | return delta; |
| 3052 | } |
| 3053 | |
| 3054 | |
| 3055 | int exp_replace(char *dst, char *src, char *str, regmatch_t *matches) { |
| 3056 | char *old_dst = dst; |
| 3057 | |
| 3058 | while (*str) { |
| 3059 | if (*str == '\\') { |
| 3060 | str++; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 3061 | if (isdigit((int)*str)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3062 | int len, num; |
| 3063 | |
| 3064 | num = *str - '0'; |
| 3065 | str++; |
| 3066 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3067 | if (matches[num].rm_eo > -1 && matches[num].rm_so > -1) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3068 | len = matches[num].rm_eo - matches[num].rm_so; |
| 3069 | memcpy(dst, src + matches[num].rm_so, len); |
| 3070 | dst += len; |
| 3071 | } |
| 3072 | |
| 3073 | } |
| 3074 | else if (*str == 'x') { |
| 3075 | unsigned char hex1, hex2; |
| 3076 | str++; |
| 3077 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 3078 | hex1 = toupper(*str++) - '0'; |
| 3079 | hex2 = toupper(*str++) - '0'; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3080 | |
| 3081 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 3082 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 3083 | *dst++ = (hex1<<4) + hex2; |
| 3084 | } |
| 3085 | else |
| 3086 | *dst++ = *str++; |
| 3087 | } |
| 3088 | else |
| 3089 | *dst++ = *str++; |
| 3090 | } |
| 3091 | *dst = 0; |
| 3092 | return dst - old_dst; |
| 3093 | } |
| 3094 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 3095 | static int ishex(char s) |
| 3096 | { |
| 3097 | return (s >= '0' && s <= '9') || (s >= 'A' && s <= 'F') || (s >= 'a' && s <= 'f'); |
| 3098 | } |
| 3099 | |
| 3100 | /* returns NULL if the replacement string <str> is valid, or the pointer to the first error */ |
| 3101 | char *check_replace_string(char *str) |
| 3102 | { |
| 3103 | char *err = NULL; |
| 3104 | while (*str) { |
| 3105 | if (*str == '\\') { |
| 3106 | err = str; /* in case of a backslash, we return the pointer to it */ |
| 3107 | str++; |
| 3108 | if (!*str) |
| 3109 | return err; |
| 3110 | else if (isdigit((int)*str)) |
| 3111 | err = NULL; |
| 3112 | else if (*str == 'x') { |
| 3113 | str++; |
| 3114 | if (!ishex(*str)) |
| 3115 | return err; |
| 3116 | str++; |
| 3117 | if (!ishex(*str)) |
| 3118 | return err; |
| 3119 | err = NULL; |
| 3120 | } |
| 3121 | else { |
| 3122 | Warning("'\\%c' : deprecated use of a backslash before something not '\\','x' or a digit.\n", *str); |
| 3123 | err = NULL; |
| 3124 | } |
| 3125 | } |
| 3126 | str++; |
| 3127 | } |
| 3128 | return err; |
| 3129 | } |
| 3130 | |
| 3131 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3132 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3133 | /* |
| 3134 | * manages the client FSM and its socket. BTW, it also tries to handle the |
| 3135 | * cookie. It returns 1 if a state has changed (and a resync may be needed), |
| 3136 | * 0 else. |
| 3137 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3138 | int process_cli(struct session *t) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3139 | int s = t->srv_state; |
| 3140 | int c = t->cli_state; |
| 3141 | struct buffer *req = t->req; |
| 3142 | struct buffer *rep = t->rep; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3143 | int method_checked = 0; |
| 3144 | appsess *asession_temp = NULL; |
| 3145 | appsess local_asession; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3146 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3147 | #ifdef DEBUG_FULL |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3148 | fprintf(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n", |
| 3149 | cli_stnames[c], srv_stnames[s], |
| 3150 | FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 3151 | t->crexpire.tv_sec, t->crexpire.tv_usec, |
| 3152 | t->cwexpire.tv_sec, t->cwexpire.tv_usec); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3153 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3154 | //fprintf(stderr,"process_cli: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s, |
| 3155 | //FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 3156 | //FD_ISSET(t->srv_fd, StaticReadEvent), FD_ISSET(t->srv_fd, StaticWriteEvent) |
| 3157 | //); |
| 3158 | if (c == CL_STHEADERS) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3159 | /* now parse the partial (or complete) headers */ |
| 3160 | while (req->lr < req->r) { /* this loop only sees one header at each iteration */ |
| 3161 | char *ptr; |
| 3162 | int delete_header; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3163 | char *request_line = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3164 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3165 | ptr = req->lr; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3166 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3167 | /* look for the end of the current header */ |
| 3168 | while (ptr < req->r && *ptr != '\n' && *ptr != '\r') |
| 3169 | ptr++; |
| 3170 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3171 | if (ptr == req->h) { /* empty line, end of headers */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3172 | int line, len; |
| 3173 | /* we can only get here after an end of headers */ |
| 3174 | /* we'll have something else to do here : add new headers ... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3175 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3176 | if (t->flags & SN_CLDENY) { |
| 3177 | /* no need to go further */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3178 | t->logs.status = 403; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3179 | client_retnclose(t, t->proxy->errmsg.len403, t->proxy->errmsg.msg403); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3180 | if (!(t->flags & SN_ERR_MASK)) |
| 3181 | t->flags |= SN_ERR_PRXCOND; |
| 3182 | if (!(t->flags & SN_FINST_MASK)) |
| 3183 | t->flags |= SN_FINST_R; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3184 | return 1; |
| 3185 | } |
| 3186 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3187 | for (line = 0; line < t->proxy->nb_reqadd; line++) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3188 | len = sprintf(trash, "%s\r\n", t->proxy->req_add[line]); |
| 3189 | buffer_replace2(req, req->h, req->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3190 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3191 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3192 | if (t->proxy->options & PR_O_FWDFOR) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3193 | if (t->cli_addr.ss_family == AF_INET) { |
| 3194 | unsigned char *pn; |
| 3195 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
| 3196 | len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d\r\n", |
| 3197 | pn[0], pn[1], pn[2], pn[3]); |
| 3198 | buffer_replace2(req, req->h, req->h, trash, len); |
| 3199 | } |
| 3200 | else if (t->cli_addr.ss_family == AF_INET6) { |
| 3201 | char pn[INET6_ADDRSTRLEN]; |
| 3202 | inet_ntop(AF_INET6, |
| 3203 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 3204 | pn, sizeof(pn)); |
| 3205 | len = sprintf(trash, "X-Forwarded-For: %s\r\n", pn); |
| 3206 | buffer_replace2(req, req->h, req->h, trash, len); |
| 3207 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3208 | } |
| 3209 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 3210 | /* add a "connection: close" line if needed */ |
| 3211 | if (t->proxy->options & PR_O_HTTP_CLOSE) |
| 3212 | buffer_replace2(req, req->h, req->h, "Connection: close\r\n", 19); |
| 3213 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 3214 | if (!memcmp(req->data, "POST ", 5)) { |
| 3215 | /* this is a POST request, which is not cacheable by default */ |
| 3216 | t->flags |= SN_POST; |
| 3217 | } |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 3218 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3219 | t->cli_state = CL_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3220 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3221 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3222 | t->logs.t_request = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3223 | /* FIXME: we'll set the client in a wait state while we try to |
| 3224 | * connect to the server. Is this really needed ? wouldn't it be |
| 3225 | * better to release the maximum of system buffers instead ? */ |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3226 | //FD_CLR(t->cli_fd, StaticReadEvent); |
| 3227 | //tv_eternity(&t->crexpire); |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 3228 | |
| 3229 | /* FIXME: if we break here (as up to 1.1.23), having the client |
| 3230 | * shutdown its connection can lead to an abort further. |
| 3231 | * it's better to either return 1 or even jump directly to the |
| 3232 | * data state which will save one schedule. |
| 3233 | */ |
| 3234 | //break; |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 3235 | |
| 3236 | if (!t->proxy->clitimeout || |
| 3237 | (t->srv_state < SV_STDATA && t->proxy->srvtimeout)) |
| 3238 | /* If the client has no timeout, or if the server is not ready yet, |
| 3239 | * and we know for sure that it can expire, then it's cleaner to |
| 3240 | * disable the timeout on the client side so that too low values |
| 3241 | * cannot make the sessions abort too early. |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3242 | * |
| 3243 | * FIXME-20050705: the server needs a way to re-enable this time-out |
| 3244 | * when it switches its state, otherwise a client can stay connected |
| 3245 | * indefinitely. This now seems to be OK. |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 3246 | */ |
| 3247 | tv_eternity(&t->crexpire); |
| 3248 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 3249 | goto process_data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3250 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3251 | |
Willy TARREAU | 13032e7 | 2006-03-12 17:31:45 +0100 | [diff] [blame] | 3252 | /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */ |
| 3253 | if (ptr > req->r - 2) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3254 | /* this is a partial header, let's wait for more to come */ |
| 3255 | req->lr = ptr; |
| 3256 | break; |
| 3257 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3258 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3259 | /* now we know that *ptr is either \r or \n, |
| 3260 | * and that there are at least 1 char after it. |
| 3261 | */ |
| 3262 | if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n')) |
| 3263 | req->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */ |
| 3264 | else |
| 3265 | req->lr = ptr + 2; /* \r\n or \n\r */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3266 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3267 | /* |
| 3268 | * now we know that we have a full header ; we can do whatever |
| 3269 | * we want with these pointers : |
| 3270 | * req->h = beginning of header |
| 3271 | * ptr = end of header (first \r or \n) |
| 3272 | * req->lr = beginning of next line (next rep->h) |
| 3273 | * req->r = end of data (not used at this stage) |
| 3274 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3275 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3276 | if (!method_checked && (t->proxy->appsession_name != NULL) && |
| 3277 | ((memcmp(req->h, "GET ", 4) == 0) || (memcmp(req->h, "POST ", 4) == 0)) && |
| 3278 | ((request_line = memchr(req->h, ';', req->lr - req->h)) != NULL)) { |
| 3279 | |
| 3280 | /* skip ; */ |
| 3281 | request_line++; |
| 3282 | |
| 3283 | /* look if we have a jsessionid */ |
| 3284 | |
| 3285 | if (strncasecmp(request_line, t->proxy->appsession_name, t->proxy->appsession_name_len) == 0) { |
| 3286 | |
| 3287 | /* skip jsessionid= */ |
| 3288 | request_line += t->proxy->appsession_name_len + 1; |
| 3289 | |
| 3290 | /* First try if we allready have an appsession */ |
| 3291 | asession_temp = &local_asession; |
| 3292 | |
| 3293 | if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) { |
| 3294 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 3295 | send_log(t->proxy, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 3296 | return 0; |
| 3297 | } |
| 3298 | |
| 3299 | /* Copy the sessionid */ |
| 3300 | memcpy(asession_temp->sessid, request_line, t->proxy->appsession_len); |
| 3301 | asession_temp->sessid[t->proxy->appsession_len] = 0; |
| 3302 | asession_temp->serverid = NULL; |
| 3303 | |
| 3304 | /* only do insert, if lookup fails */ |
| 3305 | if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *)&asession_temp)) { |
| 3306 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 3307 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 3308 | send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 3309 | return 0; |
| 3310 | } |
| 3311 | asession_temp->sessid = local_asession.sessid; |
| 3312 | asession_temp->serverid = local_asession.serverid; |
| 3313 | chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3314 | } /* end if (chtbl_lookup()) */ |
| 3315 | else { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3316 | /*free wasted memory;*/ |
| 3317 | pool_free_to(apools.sessid, local_asession.sessid); |
| 3318 | } |
| 3319 | |
| 3320 | tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout); |
| 3321 | asession_temp->request_count++; |
| 3322 | |
| 3323 | #if defined(DEBUG_HASH) |
| 3324 | print_table(&(t->proxy->htbl_proxy)); |
| 3325 | #endif |
| 3326 | |
| 3327 | if (asession_temp->serverid == NULL) { |
| 3328 | Alert("Found Application Session without matching server.\n"); |
| 3329 | } else { |
| 3330 | struct server *srv = t->proxy->srv; |
| 3331 | while (srv) { |
| 3332 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
| 3333 | if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) { |
| 3334 | /* we found the server and it's usable */ |
| 3335 | t->flags &= ~SN_CK_MASK; |
| 3336 | t->flags |= SN_CK_VALID | SN_DIRECT; |
| 3337 | t->srv = srv; |
| 3338 | break; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3339 | } else { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3340 | t->flags &= ~SN_CK_MASK; |
| 3341 | t->flags |= SN_CK_DOWN; |
| 3342 | } |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3343 | } /* end if (strcmp()) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3344 | srv = srv->next; |
| 3345 | }/* end while(srv) */ |
| 3346 | }/* end else of if (asession_temp->serverid == NULL) */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3347 | }/* 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] | 3348 | else { |
| 3349 | //fprintf(stderr,">>>>>>>>>>>>>>>>>>>>>>NO SESSION\n"); |
| 3350 | } |
willy tarreau | 598da41 | 2005-12-18 01:07:29 +0100 | [diff] [blame] | 3351 | method_checked = 1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3352 | } /* end if (!method_checked ...) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3353 | else{ |
| 3354 | //printf("No Methode-Header with Session-String\n"); |
| 3355 | } |
| 3356 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3357 | if (t->logs.logwait & LW_REQ) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3358 | /* we have a complete HTTP request that we must log */ |
| 3359 | int urilen; |
| 3360 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3361 | if ((t->logs.uri = pool_alloc(requri)) == NULL) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3362 | Alert("HTTP logging : out of memory.\n"); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3363 | t->logs.status = 500; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3364 | client_retnclose(t, t->proxy->errmsg.len500, t->proxy->errmsg.msg500); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3365 | if (!(t->flags & SN_ERR_MASK)) |
| 3366 | t->flags |= SN_ERR_PRXCOND; |
| 3367 | if (!(t->flags & SN_FINST_MASK)) |
| 3368 | t->flags |= SN_FINST_R; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3369 | return 1; |
| 3370 | } |
| 3371 | |
| 3372 | urilen = ptr - req->h; |
| 3373 | if (urilen >= REQURI_LEN) |
| 3374 | urilen = REQURI_LEN - 1; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3375 | memcpy(t->logs.uri, req->h, urilen); |
| 3376 | t->logs.uri[urilen] = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3377 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3378 | if (!(t->logs.logwait &= ~LW_REQ)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3379 | sess_log(t); |
| 3380 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 3381 | else if (t->logs.logwait & LW_REQHDR) { |
| 3382 | struct cap_hdr *h; |
| 3383 | int len; |
| 3384 | for (h = t->proxy->req_cap; h; h = h->next) { |
| 3385 | if ((h->namelen + 2 <= ptr - req->h) && |
| 3386 | (req->h[h->namelen] == ':') && |
| 3387 | (strncasecmp(req->h, h->name, h->namelen) == 0)) { |
| 3388 | |
| 3389 | if (t->req_cap[h->index] == NULL) |
| 3390 | t->req_cap[h->index] = pool_alloc_from(h->pool, h->len + 1); |
| 3391 | |
| 3392 | len = ptr - (req->h + h->namelen + 2); |
| 3393 | if (len > h->len) |
| 3394 | len = h->len; |
| 3395 | |
| 3396 | memcpy(t->req_cap[h->index], req->h + h->namelen + 2, len); |
| 3397 | t->req_cap[h->index][len]=0; |
| 3398 | } |
| 3399 | } |
| 3400 | |
| 3401 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3402 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3403 | delete_header = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3404 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 3405 | 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] | 3406 | int len, max; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 3407 | 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] | 3408 | max = ptr - req->h; |
| 3409 | UBOUND(max, sizeof(trash) - len - 1); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3410 | len += strlcpy2(trash + len, req->h, max + 1); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3411 | trash[len++] = '\n'; |
| 3412 | write(1, trash, len); |
| 3413 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3414 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 3415 | |
| 3416 | /* remove "connection: " if needed */ |
| 3417 | if (!delete_header && (t->proxy->options & PR_O_HTTP_CLOSE) |
| 3418 | && (strncasecmp(req->h, "Connection: ", 12) == 0)) { |
| 3419 | delete_header = 1; |
| 3420 | } |
| 3421 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3422 | /* try headers regexps */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 3423 | if (!delete_header && t->proxy->req_exp != NULL |
| 3424 | && !(t->flags & SN_CLDENY)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3425 | struct hdr_exp *exp; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3426 | char term; |
| 3427 | |
| 3428 | term = *ptr; |
| 3429 | *ptr = '\0'; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3430 | exp = t->proxy->req_exp; |
| 3431 | do { |
| 3432 | if (regexec(exp->preg, req->h, MAX_MATCH, pmatch, 0) == 0) { |
| 3433 | switch (exp->action) { |
| 3434 | case ACT_ALLOW: |
| 3435 | if (!(t->flags & SN_CLDENY)) |
| 3436 | t->flags |= SN_CLALLOW; |
| 3437 | break; |
| 3438 | case ACT_REPLACE: |
| 3439 | if (!(t->flags & SN_CLDENY)) { |
| 3440 | int len = exp_replace(trash, req->h, exp->replace, pmatch); |
| 3441 | ptr += buffer_replace2(req, req->h, ptr, trash, len); |
| 3442 | } |
| 3443 | break; |
| 3444 | case ACT_REMOVE: |
| 3445 | if (!(t->flags & SN_CLDENY)) |
| 3446 | delete_header = 1; |
| 3447 | break; |
| 3448 | case ACT_DENY: |
| 3449 | if (!(t->flags & SN_CLALLOW)) |
| 3450 | t->flags |= SN_CLDENY; |
| 3451 | break; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3452 | case ACT_PASS: /* we simply don't deny this one */ |
| 3453 | break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3454 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3455 | break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3456 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3457 | } while ((exp = exp->next) != NULL); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3458 | *ptr = term; /* restore the string terminator */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3459 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3460 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3461 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3462 | * attributes whose name begin with a '$', and associate them with |
| 3463 | * the right cookie, if we want to delete this cookie. |
| 3464 | * So there are 3 cases for each cookie read : |
| 3465 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3466 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3467 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3468 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3469 | * "special" cookie. |
| 3470 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3471 | * remove it. If no application cookie persists in the header, we |
| 3472 | * *MUST* delete it |
| 3473 | */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3474 | if (!delete_header && |
| 3475 | (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] | 3476 | && !(t->flags & SN_CLDENY) && (ptr >= req->h + 8) |
willy tarreau | 906b268 | 2005-12-17 13:49:52 +0100 | [diff] [blame] | 3477 | && (strncasecmp(req->h, "Cookie: ", 8) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3478 | char *p1, *p2, *p3, *p4; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3479 | char *del_colon, *del_cookie, *colon; |
| 3480 | int app_cookies; |
| 3481 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3482 | p1 = req->h + 8; /* first char after 'Cookie: ' */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3483 | colon = p1; |
| 3484 | /* del_cookie == NULL => nothing to be deleted */ |
| 3485 | del_colon = del_cookie = NULL; |
| 3486 | app_cookies = 0; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3487 | |
| 3488 | while (p1 < ptr) { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3489 | /* skip spaces and colons, but keep an eye on these ones */ |
| 3490 | while (p1 < ptr) { |
| 3491 | if (*p1 == ';' || *p1 == ',') |
| 3492 | colon = p1; |
| 3493 | else if (!isspace((int)*p1)) |
| 3494 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3495 | p1++; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3496 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3497 | |
| 3498 | if (p1 == ptr) |
| 3499 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3500 | |
| 3501 | /* p1 is at the beginning of the cookie name */ |
| 3502 | p2 = p1; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3503 | while (p2 < ptr && *p2 != '=') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3504 | p2++; |
| 3505 | |
| 3506 | if (p2 == ptr) |
| 3507 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3508 | |
| 3509 | p3 = p2 + 1; /* skips the '=' sign */ |
| 3510 | if (p3 == ptr) |
| 3511 | break; |
| 3512 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3513 | p4 = p3; |
| 3514 | while (p4 < ptr && !isspace((int)*p4) && *p4 != ';' && *p4 != ',') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3515 | p4++; |
| 3516 | |
| 3517 | /* here, we have the cookie name between p1 and p2, |
| 3518 | * and its value between p3 and p4. |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3519 | * we can process it : |
| 3520 | * |
| 3521 | * Cookie: NAME=VALUE; |
| 3522 | * | || || | |
| 3523 | * | || || +--> p4 |
| 3524 | * | || |+-------> p3 |
| 3525 | * | || +--------> p2 |
| 3526 | * | |+------------> p1 |
| 3527 | * | +-------------> colon |
| 3528 | * +--------------------> req->h |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3529 | */ |
| 3530 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3531 | if (*p1 == '$') { |
| 3532 | /* skip this one */ |
| 3533 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3534 | else { |
| 3535 | /* first, let's see if we want to capture it */ |
| 3536 | if (t->proxy->capture_name != NULL && |
| 3537 | t->logs.cli_cookie == NULL && |
| 3538 | (p4 - p1 >= t->proxy->capture_namelen) && |
| 3539 | memcmp(p1, t->proxy->capture_name, t->proxy->capture_namelen) == 0) { |
| 3540 | int log_len = p4 - p1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3541 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3542 | if ((t->logs.cli_cookie = pool_alloc(capture)) == NULL) { |
| 3543 | Alert("HTTP logging : out of memory.\n"); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3544 | } else { |
| 3545 | if (log_len > t->proxy->capture_len) |
| 3546 | log_len = t->proxy->capture_len; |
| 3547 | memcpy(t->logs.cli_cookie, p1, log_len); |
| 3548 | t->logs.cli_cookie[log_len] = 0; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3549 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3550 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3551 | |
| 3552 | if ((p2 - p1 == t->proxy->cookie_len) && (t->proxy->cookie_name != NULL) && |
| 3553 | (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) { |
| 3554 | /* Cool... it's the right one */ |
| 3555 | struct server *srv = t->proxy->srv; |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3556 | char *delim; |
| 3557 | |
| 3558 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 3559 | * have the server ID betweek p3 and delim, and the original cookie between |
| 3560 | * delim+1 and p4. Otherwise, delim==p4 : |
| 3561 | * |
| 3562 | * Cookie: NAME=SRV~VALUE; |
| 3563 | * | || || | | |
| 3564 | * | || || | +--> p4 |
| 3565 | * | || || +--------> delim |
| 3566 | * | || |+-----------> p3 |
| 3567 | * | || +------------> p2 |
| 3568 | * | |+----------------> p1 |
| 3569 | * | +-----------------> colon |
| 3570 | * +------------------------> req->h |
| 3571 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3572 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3573 | if (t->proxy->options & PR_O_COOK_PFX) { |
| 3574 | for (delim = p3; delim < p4; delim++) |
| 3575 | if (*delim == COOKIE_DELIM) |
| 3576 | break; |
| 3577 | } |
| 3578 | else |
| 3579 | delim = p4; |
| 3580 | |
| 3581 | |
| 3582 | /* Here, we'll look for the first running server which supports the cookie. |
| 3583 | * This allows to share a same cookie between several servers, for example |
| 3584 | * to dedicate backup servers to specific servers only. |
| 3585 | */ |
| 3586 | while (srv) { |
| 3587 | if ((srv->cklen == delim - p3) && !memcmp(p3, srv->cookie, delim - p3)) { |
| 3588 | if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) { |
| 3589 | /* we found the server and it's usable */ |
| 3590 | t->flags &= ~SN_CK_MASK; |
| 3591 | t->flags |= SN_CK_VALID | SN_DIRECT; |
| 3592 | t->srv = srv; |
| 3593 | break; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3594 | } else { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3595 | /* we found a server, but it's down */ |
| 3596 | t->flags &= ~SN_CK_MASK; |
| 3597 | t->flags |= SN_CK_DOWN; |
| 3598 | } |
| 3599 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3600 | srv = srv->next; |
| 3601 | } |
| 3602 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3603 | if (!srv && !(t->flags & SN_CK_DOWN)) { |
| 3604 | /* no server matched this cookie */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3605 | t->flags &= ~SN_CK_MASK; |
| 3606 | t->flags |= SN_CK_INVALID; |
| 3607 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3608 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3609 | /* depending on the cookie mode, we may have to either : |
| 3610 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3611 | * the server never sees it ; |
| 3612 | * - remove the server id from the cookie value, and tag the cookie as an |
| 3613 | * application cookie so that it does not get accidentely removed later, |
| 3614 | * if we're in cookie prefix mode |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3615 | */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3616 | if ((t->proxy->options & PR_O_COOK_PFX) && (delim != p4)) { |
| 3617 | buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 3618 | p4 -= (delim + 1 - p3); |
| 3619 | ptr -= (delim + 1 - p3); |
| 3620 | del_cookie = del_colon = NULL; |
| 3621 | app_cookies++; /* protect the header from deletion */ |
| 3622 | } |
| 3623 | else if (del_cookie == NULL && |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3624 | (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] | 3625 | del_cookie = p1; |
| 3626 | del_colon = colon; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3627 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3628 | } else { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3629 | /* now we know that we must keep this cookie since it's |
| 3630 | * not ours. But if we wanted to delete our cookie |
| 3631 | * earlier, we cannot remove the complete header, but we |
| 3632 | * can remove the previous block itself. |
| 3633 | */ |
| 3634 | app_cookies++; |
| 3635 | |
| 3636 | if (del_cookie != NULL) { |
| 3637 | buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 3638 | p4 -= (p1 - del_cookie); |
| 3639 | ptr -= (p1 - del_cookie); |
| 3640 | del_cookie = del_colon = NULL; |
| 3641 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3642 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3643 | |
| 3644 | if ((t->proxy->appsession_name != NULL) && |
| 3645 | (memcmp(p1, t->proxy->appsession_name, p2 - p1) == 0)) { |
| 3646 | /* first, let's see if the cookie is our appcookie*/ |
| 3647 | |
| 3648 | /* Cool... it's the right one */ |
| 3649 | |
| 3650 | asession_temp = &local_asession; |
| 3651 | |
| 3652 | if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) { |
| 3653 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3654 | send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3655 | return 0; |
| 3656 | } |
| 3657 | |
| 3658 | memcpy(asession_temp->sessid, p3, t->proxy->appsession_len); |
| 3659 | asession_temp->sessid[t->proxy->appsession_len] = 0; |
| 3660 | asession_temp->serverid = NULL; |
| 3661 | |
| 3662 | /* only do insert, if lookup fails */ |
| 3663 | if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *) &asession_temp) != 0) { |
| 3664 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 3665 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 3666 | send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 3667 | return 0; |
| 3668 | } |
| 3669 | |
| 3670 | asession_temp->sessid = local_asession.sessid; |
| 3671 | asession_temp->serverid = local_asession.serverid; |
| 3672 | chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp); |
| 3673 | } |
| 3674 | else{ |
| 3675 | /* free wasted memory */ |
| 3676 | pool_free_to(apools.sessid, local_asession.sessid); |
| 3677 | } |
| 3678 | |
| 3679 | if (asession_temp->serverid == NULL) { |
| 3680 | Alert("Found Application Session without matching server.\n"); |
| 3681 | } else { |
| 3682 | struct server *srv = t->proxy->srv; |
| 3683 | while (srv) { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3684 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3685 | if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) { |
| 3686 | /* we found the server and it's usable */ |
| 3687 | t->flags &= ~SN_CK_MASK; |
| 3688 | t->flags |= SN_CK_VALID | SN_DIRECT; |
| 3689 | t->srv = srv; |
| 3690 | break; |
| 3691 | } else { |
| 3692 | t->flags &= ~SN_CK_MASK; |
| 3693 | t->flags |= SN_CK_DOWN; |
| 3694 | } |
| 3695 | } |
| 3696 | srv = srv->next; |
| 3697 | }/* end while(srv) */ |
| 3698 | }/* end else if server == NULL */ |
| 3699 | |
| 3700 | tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3701 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3702 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3703 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3704 | /* we'll have to look for another cookie ... */ |
| 3705 | p1 = p4; |
| 3706 | } /* while (p1 < ptr) */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3707 | |
| 3708 | /* There's no more cookie on this line. |
| 3709 | * We may have marked the last one(s) for deletion. |
| 3710 | * We must do this now in two ways : |
| 3711 | * - if there is no app cookie, we simply delete the header ; |
| 3712 | * - if there are app cookies, we must delete the end of the |
| 3713 | * string properly, including the colon/semi-colon before |
| 3714 | * the cookie name. |
| 3715 | */ |
| 3716 | if (del_cookie != NULL) { |
| 3717 | if (app_cookies) { |
| 3718 | buffer_replace2(req, del_colon, ptr, NULL, 0); |
| 3719 | /* WARNING! <ptr> becomes invalid for now. If some code |
| 3720 | * below needs to rely on it before the end of the global |
| 3721 | * header loop, we need to correct it with this code : |
| 3722 | * ptr = del_colon; |
| 3723 | */ |
| 3724 | } |
| 3725 | else |
| 3726 | delete_header = 1; |
| 3727 | } |
| 3728 | } /* end of cookie processing on this header */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3729 | |
| 3730 | /* let's look if we have to delete this header */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3731 | if (delete_header && !(t->flags & SN_CLDENY)) { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3732 | buffer_replace2(req, req->h, req->lr, NULL, 0); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3733 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3734 | /* WARNING: ptr is not valid anymore, since the header may have been deleted or truncated ! */ |
| 3735 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3736 | req->h = req->lr; |
| 3737 | } /* while (req->lr < req->r) */ |
| 3738 | |
| 3739 | /* end of header processing (even if incomplete) */ |
| 3740 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3741 | if ((req->l < req->rlim - req->data) && ! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 3742 | /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer |
| 3743 | * full. We cannot loop here since event_cli_read will disable it only if |
| 3744 | * req->l == rlim-data |
| 3745 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3746 | FD_SET(t->cli_fd, StaticReadEvent); |
| 3747 | if (t->proxy->clitimeout) |
| 3748 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 3749 | else |
| 3750 | tv_eternity(&t->crexpire); |
| 3751 | } |
| 3752 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3753 | /* 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] | 3754 | * won't be able to free more later, so the session will never terminate. |
| 3755 | */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3756 | if (req->l >= req->rlim - req->data) { |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3757 | t->logs.status = 400; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3758 | client_retnclose(t, t->proxy->errmsg.len400, t->proxy->errmsg.msg400); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3759 | if (!(t->flags & SN_ERR_MASK)) |
| 3760 | t->flags |= SN_ERR_PRXCOND; |
| 3761 | if (!(t->flags & SN_FINST_MASK)) |
| 3762 | t->flags |= SN_FINST_R; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3763 | return 1; |
| 3764 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3765 | else if (t->res_cr == RES_ERROR || t->res_cr == RES_NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3766 | /* read error, or last read : give up. */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3767 | tv_eternity(&t->crexpire); |
| 3768 | fd_delete(t->cli_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3769 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3770 | if (!(t->flags & SN_ERR_MASK)) |
| 3771 | t->flags |= SN_ERR_CLICL; |
| 3772 | if (!(t->flags & SN_FINST_MASK)) |
| 3773 | t->flags |= SN_FINST_R; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3774 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3775 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3776 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 3777 | |
| 3778 | /* read timeout : give up with an error message. |
| 3779 | */ |
| 3780 | t->logs.status = 408; |
| 3781 | client_retnclose(t, t->proxy->errmsg.len408, t->proxy->errmsg.msg408); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3782 | if (!(t->flags & SN_ERR_MASK)) |
| 3783 | t->flags |= SN_ERR_CLITO; |
| 3784 | if (!(t->flags & SN_FINST_MASK)) |
| 3785 | t->flags |= SN_FINST_R; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3786 | return 1; |
| 3787 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3788 | |
| 3789 | return t->cli_state != CL_STHEADERS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3790 | } |
| 3791 | else if (c == CL_STDATA) { |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 3792 | process_data: |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 3793 | /* FIXME: this error handling is partly buggy because we always report |
| 3794 | * a 'DATA' phase while we don't know if the server was in IDLE, CONN |
| 3795 | * or HEADER phase. BTW, it's not logical to expire the client while |
| 3796 | * we're waiting for the server to connect. |
| 3797 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3798 | /* read or write error */ |
| 3799 | if (t->res_cw == RES_ERROR || t->res_cr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3800 | tv_eternity(&t->crexpire); |
| 3801 | tv_eternity(&t->cwexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3802 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3803 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3804 | if (!(t->flags & SN_ERR_MASK)) |
| 3805 | t->flags |= SN_ERR_CLICL; |
| 3806 | if (!(t->flags & SN_FINST_MASK)) |
| 3807 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3808 | return 1; |
| 3809 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3810 | /* last read, or end of server write */ |
| 3811 | 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] | 3812 | FD_CLR(t->cli_fd, StaticReadEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3813 | tv_eternity(&t->crexpire); |
| 3814 | shutdown(t->cli_fd, SHUT_RD); |
| 3815 | t->cli_state = CL_STSHUTR; |
| 3816 | return 1; |
| 3817 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3818 | /* last server read and buffer empty */ |
| 3819 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3820 | FD_CLR(t->cli_fd, StaticWriteEvent); |
| 3821 | tv_eternity(&t->cwexpire); |
| 3822 | shutdown(t->cli_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3823 | /* We must ensure that the read part is still alive when switching |
| 3824 | * to shutw */ |
| 3825 | FD_SET(t->cli_fd, StaticReadEvent); |
| 3826 | if (t->proxy->clitimeout) |
| 3827 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3828 | t->cli_state = CL_STSHUTW; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3829 | //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] | 3830 | return 1; |
| 3831 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3832 | /* read timeout */ |
| 3833 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 3834 | FD_CLR(t->cli_fd, StaticReadEvent); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3835 | tv_eternity(&t->crexpire); |
| 3836 | shutdown(t->cli_fd, SHUT_RD); |
| 3837 | t->cli_state = CL_STSHUTR; |
| 3838 | if (!(t->flags & SN_ERR_MASK)) |
| 3839 | t->flags |= SN_ERR_CLITO; |
| 3840 | if (!(t->flags & SN_FINST_MASK)) |
| 3841 | t->flags |= SN_FINST_D; |
| 3842 | return 1; |
| 3843 | } |
| 3844 | /* write timeout */ |
| 3845 | else if (tv_cmp2_ms(&t->cwexpire, &now) <= 0) { |
| 3846 | FD_CLR(t->cli_fd, StaticWriteEvent); |
| 3847 | tv_eternity(&t->cwexpire); |
| 3848 | shutdown(t->cli_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3849 | /* We must ensure that the read part is still alive when switching |
| 3850 | * to shutw */ |
| 3851 | FD_SET(t->cli_fd, StaticReadEvent); |
| 3852 | if (t->proxy->clitimeout) |
| 3853 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 3854 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3855 | t->cli_state = CL_STSHUTW; |
| 3856 | if (!(t->flags & SN_ERR_MASK)) |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3857 | t->flags |= SN_ERR_CLITO; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3858 | if (!(t->flags & SN_FINST_MASK)) |
| 3859 | t->flags |= SN_FINST_D; |
| 3860 | return 1; |
| 3861 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3862 | |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 3863 | if (req->l >= req->rlim - req->data) { |
| 3864 | /* no room to read more data */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3865 | if (FD_ISSET(t->cli_fd, StaticReadEvent)) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3866 | /* stop reading until we get some space */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3867 | FD_CLR(t->cli_fd, StaticReadEvent); |
| 3868 | tv_eternity(&t->crexpire); |
| 3869 | } |
| 3870 | } |
| 3871 | else { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3872 | /* there's still some space in the buffer */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3873 | if (! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 3874 | FD_SET(t->cli_fd, StaticReadEvent); |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 3875 | if (!t->proxy->clitimeout || |
| 3876 | (t->srv_state < SV_STDATA && t->proxy->srvtimeout)) |
| 3877 | /* If the client has no timeout, or if the server not ready yet, and we |
| 3878 | * know for sure that it can expire, then it's cleaner to disable the |
| 3879 | * timeout on the client side so that too low values cannot make the |
| 3880 | * sessions abort too early. |
| 3881 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3882 | tv_eternity(&t->crexpire); |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 3883 | else |
| 3884 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3885 | } |
| 3886 | } |
| 3887 | |
| 3888 | if ((rep->l == 0) || |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 3889 | ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3890 | if (FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 3891 | FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */ |
| 3892 | tv_eternity(&t->cwexpire); |
| 3893 | } |
| 3894 | } |
| 3895 | else { /* buffer not empty */ |
| 3896 | if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 3897 | FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3898 | if (t->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3899 | tv_delayfrom(&t->cwexpire, &now, t->proxy->clitimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3900 | /* FIXME: to avoid the client to read-time-out during writes, we refresh it */ |
| 3901 | t->crexpire = t->cwexpire; |
| 3902 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3903 | else |
| 3904 | tv_eternity(&t->cwexpire); |
| 3905 | } |
| 3906 | } |
| 3907 | return 0; /* other cases change nothing */ |
| 3908 | } |
| 3909 | else if (c == CL_STSHUTR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3910 | if (t->res_cw == RES_ERROR) { |
| 3911 | tv_eternity(&t->cwexpire); |
| 3912 | fd_delete(t->cli_fd); |
| 3913 | t->cli_state = CL_STCLOSE; |
| 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_D; |
| 3918 | return 1; |
| 3919 | } |
| 3920 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3921 | tv_eternity(&t->cwexpire); |
| 3922 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3923 | t->cli_state = CL_STCLOSE; |
| 3924 | return 1; |
| 3925 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3926 | else if (tv_cmp2_ms(&t->cwexpire, &now) <= 0) { |
| 3927 | tv_eternity(&t->cwexpire); |
| 3928 | fd_delete(t->cli_fd); |
| 3929 | t->cli_state = CL_STCLOSE; |
| 3930 | if (!(t->flags & SN_ERR_MASK)) |
| 3931 | t->flags |= SN_ERR_CLITO; |
| 3932 | if (!(t->flags & SN_FINST_MASK)) |
| 3933 | t->flags |= SN_FINST_D; |
| 3934 | return 1; |
| 3935 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3936 | else if ((rep->l == 0) || |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3937 | ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3938 | if (FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 3939 | FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */ |
| 3940 | tv_eternity(&t->cwexpire); |
| 3941 | } |
| 3942 | } |
| 3943 | else { /* buffer not empty */ |
| 3944 | if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 3945 | FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3946 | if (t->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3947 | tv_delayfrom(&t->cwexpire, &now, t->proxy->clitimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3948 | /* FIXME: to avoid the client to read-time-out during writes, we refresh it */ |
| 3949 | t->crexpire = t->cwexpire; |
| 3950 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3951 | else |
| 3952 | tv_eternity(&t->cwexpire); |
| 3953 | } |
| 3954 | } |
| 3955 | return 0; |
| 3956 | } |
| 3957 | else if (c == CL_STSHUTW) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3958 | if (t->res_cr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3959 | tv_eternity(&t->crexpire); |
| 3960 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3961 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3962 | if (!(t->flags & SN_ERR_MASK)) |
| 3963 | t->flags |= SN_ERR_CLICL; |
| 3964 | if (!(t->flags & SN_FINST_MASK)) |
| 3965 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3966 | return 1; |
| 3967 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3968 | else if (t->res_cr == RES_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
| 3969 | tv_eternity(&t->crexpire); |
| 3970 | fd_delete(t->cli_fd); |
| 3971 | t->cli_state = CL_STCLOSE; |
| 3972 | return 1; |
| 3973 | } |
| 3974 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 3975 | tv_eternity(&t->crexpire); |
| 3976 | fd_delete(t->cli_fd); |
| 3977 | t->cli_state = CL_STCLOSE; |
| 3978 | if (!(t->flags & SN_ERR_MASK)) |
| 3979 | t->flags |= SN_ERR_CLITO; |
| 3980 | if (!(t->flags & SN_FINST_MASK)) |
| 3981 | t->flags |= SN_FINST_D; |
| 3982 | return 1; |
| 3983 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3984 | else if (req->l >= req->rlim - req->data) { |
| 3985 | /* no room to read more data */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3986 | |
| 3987 | /* FIXME-20050705: is it possible for a client to maintain a session |
| 3988 | * after the timeout by sending more data after it receives a close ? |
| 3989 | */ |
| 3990 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3991 | if (FD_ISSET(t->cli_fd, StaticReadEvent)) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3992 | /* stop reading until we get some space */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3993 | FD_CLR(t->cli_fd, StaticReadEvent); |
| 3994 | tv_eternity(&t->crexpire); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3995 | //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] | 3996 | } |
| 3997 | } |
| 3998 | else { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3999 | /* there's still some space in the buffer */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4000 | if (! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 4001 | FD_SET(t->cli_fd, StaticReadEvent); |
| 4002 | if (t->proxy->clitimeout) |
| 4003 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 4004 | else |
| 4005 | tv_eternity(&t->crexpire); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4006 | //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] | 4007 | } |
| 4008 | } |
| 4009 | return 0; |
| 4010 | } |
| 4011 | else { /* CL_STCLOSE: nothing to do */ |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4012 | 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] | 4013 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 4014 | 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] | 4015 | write(1, trash, len); |
| 4016 | } |
| 4017 | return 0; |
| 4018 | } |
| 4019 | return 0; |
| 4020 | } |
| 4021 | |
| 4022 | |
| 4023 | /* |
| 4024 | * manages the server FSM and its socket. It returns 1 if a state has changed |
| 4025 | * (and a resync may be needed), 0 else. |
| 4026 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4027 | int process_srv(struct session *t) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4028 | int s = t->srv_state; |
| 4029 | int c = t->cli_state; |
| 4030 | struct buffer *req = t->req; |
| 4031 | struct buffer *rep = t->rep; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4032 | appsess *asession_temp = NULL; |
| 4033 | appsess local_asession; |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4034 | int conn_err; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4035 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4036 | #ifdef DEBUG_FULL |
| 4037 | fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 4038 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4039 | //fprintf(stderr,"process_srv: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s, |
| 4040 | //FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 4041 | //FD_ISSET(t->srv_fd, StaticReadEvent), FD_ISSET(t->srv_fd, StaticWriteEvent) |
| 4042 | //); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4043 | if (s == SV_STIDLE) { |
| 4044 | if (c == CL_STHEADERS) |
| 4045 | return 0; /* stay in idle, waiting for data to reach the client side */ |
| 4046 | else if (c == CL_STCLOSE || |
| 4047 | c == CL_STSHUTW || |
| 4048 | (c == CL_STSHUTR && t->req->l == 0)) { /* give up */ |
| 4049 | tv_eternity(&t->cnexpire); |
| 4050 | t->srv_state = SV_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4051 | if (!(t->flags & SN_ERR_MASK)) |
| 4052 | t->flags |= SN_ERR_CLICL; |
| 4053 | if (!(t->flags & SN_FINST_MASK)) |
| 4054 | t->flags |= SN_FINST_C; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4055 | return 1; |
| 4056 | } |
| 4057 | else { /* go to SV_STCONN */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4058 | /* initiate a connection to the server */ |
| 4059 | conn_err = connect_server(t); |
| 4060 | if (conn_err == SN_ERR_NONE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4061 | //fprintf(stderr,"0: c=%d, s=%d\n", c, s); |
| 4062 | t->srv_state = SV_STCONN; |
| 4063 | } |
| 4064 | else { /* try again */ |
| 4065 | while (t->conn_retries-- > 0) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4066 | if ((t->proxy->options & PR_O_REDISP) && (t->conn_retries == 0)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4067 | t->flags &= ~SN_DIRECT; /* ignore cookie and force to use the dispatcher */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4068 | 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] | 4069 | if ((t->flags & SN_CK_MASK) == SN_CK_VALID) { |
| 4070 | t->flags &= ~SN_CK_MASK; |
| 4071 | t->flags |= SN_CK_DOWN; |
| 4072 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4073 | } |
| 4074 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4075 | conn_err = connect_server(t); |
| 4076 | if (conn_err == SN_ERR_NONE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4077 | t->srv_state = SV_STCONN; |
| 4078 | break; |
| 4079 | } |
| 4080 | } |
| 4081 | if (t->conn_retries < 0) { |
| 4082 | /* if conn_retries < 0 or other error, let's abort */ |
| 4083 | tv_eternity(&t->cnexpire); |
| 4084 | t->srv_state = SV_STCLOSE; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4085 | t->logs.status = 503; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4086 | if (t->proxy->mode == PR_MODE_HTTP) |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4087 | client_return(t, t->proxy->errmsg.len503, t->proxy->errmsg.msg503); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4088 | if (!(t->flags & SN_ERR_MASK)) |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4089 | t->flags |= conn_err; /* report the precise connect() error */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4090 | if (!(t->flags & SN_FINST_MASK)) |
| 4091 | t->flags |= SN_FINST_C; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4092 | } |
| 4093 | } |
| 4094 | return 1; |
| 4095 | } |
| 4096 | } |
| 4097 | else if (s == SV_STCONN) { /* connection in progress */ |
| 4098 | 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] | 4099 | //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] | 4100 | return 0; /* nothing changed */ |
| 4101 | } |
| 4102 | else if (t->res_sw == RES_SILENT || t->res_sw == RES_ERROR) { |
| 4103 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 4104 | /* timeout, connect error or first write error */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4105 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4106 | fd_delete(t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4107 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4108 | t->conn_retries--; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4109 | if (t->conn_retries >= 0) { |
| 4110 | if ((t->proxy->options & PR_O_REDISP) && (t->conn_retries == 0)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4111 | t->flags &= ~SN_DIRECT; /* ignore cookie and force to use the dispatcher */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4112 | 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] | 4113 | if ((t->flags & SN_CK_MASK) == SN_CK_VALID) { |
| 4114 | t->flags &= ~SN_CK_MASK; |
| 4115 | t->flags |= SN_CK_DOWN; |
| 4116 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4117 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4118 | conn_err = connect_server(t); |
| 4119 | if (conn_err == SN_ERR_NONE) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4120 | return 0; /* no state changed */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4121 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4122 | else if (t->res_sw == RES_SILENT) |
| 4123 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 4124 | else |
| 4125 | conn_err = SN_ERR_SRVCL; // it was a connect error. |
| 4126 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4127 | /* if conn_retries < 0 or other error, let's abort */ |
| 4128 | tv_eternity(&t->cnexpire); |
| 4129 | t->srv_state = SV_STCLOSE; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4130 | t->logs.status = 503; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4131 | if (t->proxy->mode == PR_MODE_HTTP) |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4132 | client_return(t, t->proxy->errmsg.len503, t->proxy->errmsg.msg503); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4133 | if (!(t->flags & SN_ERR_MASK)) |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4134 | t->flags |= conn_err; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4135 | if (!(t->flags & SN_FINST_MASK)) |
| 4136 | t->flags |= SN_FINST_C; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4137 | return 1; |
| 4138 | } |
| 4139 | else { /* no error or write 0 */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4140 | t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4141 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4142 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4143 | if (req->l == 0) /* nothing to write */ { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4144 | FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4145 | tv_eternity(&t->swexpire); |
| 4146 | } else /* need the right to write */ { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4147 | FD_SET(t->srv_fd, StaticWriteEvent); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4148 | if (t->proxy->srvtimeout) { |
| 4149 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
| 4150 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 4151 | t->srexpire = t->swexpire; |
| 4152 | } |
| 4153 | else |
| 4154 | tv_eternity(&t->swexpire); |
| 4155 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4156 | |
| 4157 | if (t->proxy->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 4158 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4159 | if (t->proxy->srvtimeout) |
| 4160 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4161 | else |
| 4162 | tv_eternity(&t->srexpire); |
| 4163 | |
| 4164 | t->srv_state = SV_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4165 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4166 | |
| 4167 | /* if the user wants to log as soon as possible, without counting |
| 4168 | bytes from the server, then this is the right moment. */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 4169 | if (t->proxy->to_log && !(t->logs.logwait & LW_BYTES)) { |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4170 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 4171 | sess_log(t); |
| 4172 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4173 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4174 | else { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4175 | t->srv_state = SV_STHEADERS; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4176 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
| 4177 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4178 | tv_eternity(&t->cnexpire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4179 | return 1; |
| 4180 | } |
| 4181 | } |
| 4182 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4183 | /* now parse the partial (or complete) headers */ |
| 4184 | while (rep->lr < rep->r) { /* this loop only sees one header at each iteration */ |
| 4185 | char *ptr; |
| 4186 | int delete_header; |
| 4187 | |
| 4188 | ptr = rep->lr; |
| 4189 | |
| 4190 | /* look for the end of the current header */ |
| 4191 | while (ptr < rep->r && *ptr != '\n' && *ptr != '\r') |
| 4192 | ptr++; |
| 4193 | |
| 4194 | if (ptr == rep->h) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4195 | int line, len; |
| 4196 | |
| 4197 | /* we can only get here after an end of headers */ |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4198 | |
| 4199 | /* first, we'll block if security checks have caught nasty things */ |
| 4200 | if (t->flags & SN_CACHEABLE) { |
| 4201 | if ((t->flags & SN_CACHE_COOK) && |
| 4202 | (t->flags & SN_SCK_ANY) && |
| 4203 | (t->proxy->options & PR_O_CHK_CACHE)) { |
| 4204 | |
| 4205 | /* we're in presence of a cacheable response containing |
| 4206 | * a set-cookie header. We'll block it as requested by |
| 4207 | * the 'checkcache' option, and send an alert. |
| 4208 | */ |
| 4209 | tv_eternity(&t->srexpire); |
| 4210 | tv_eternity(&t->swexpire); |
| 4211 | fd_delete(t->srv_fd); |
| 4212 | t->srv_state = SV_STCLOSE; |
| 4213 | t->logs.status = 502; |
| 4214 | client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502); |
| 4215 | if (!(t->flags & SN_ERR_MASK)) |
| 4216 | t->flags |= SN_ERR_PRXCOND; |
| 4217 | if (!(t->flags & SN_FINST_MASK)) |
| 4218 | t->flags |= SN_FINST_H; |
| 4219 | |
| 4220 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", t->proxy->id, t->srv->id); |
| 4221 | send_log(t->proxy, LOG_ALERT, "Blocking cacheable cookie in response from instance %s, server %s.\n", t->proxy->id, t->srv->id); |
| 4222 | |
| 4223 | return 1; |
| 4224 | } |
| 4225 | } |
| 4226 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4227 | /* next, we'll block if an 'rspideny' or 'rspdeny' filter matched */ |
| 4228 | if (t->flags & SN_SVDENY) { |
| 4229 | tv_eternity(&t->srexpire); |
| 4230 | tv_eternity(&t->swexpire); |
| 4231 | fd_delete(t->srv_fd); |
| 4232 | t->srv_state = SV_STCLOSE; |
| 4233 | t->logs.status = 502; |
| 4234 | client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502); |
| 4235 | if (!(t->flags & SN_ERR_MASK)) |
| 4236 | t->flags |= SN_ERR_PRXCOND; |
| 4237 | if (!(t->flags & SN_FINST_MASK)) |
| 4238 | t->flags |= SN_FINST_H; |
| 4239 | return 1; |
| 4240 | } |
| 4241 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4242 | /* we'll have something else to do here : add new headers ... */ |
| 4243 | |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 4244 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_INS) && |
| 4245 | (!(t->proxy->options & PR_O_COOK_POST) || (t->flags & SN_POST))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4246 | /* 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] | 4247 | * insert a set-cookie here, except if we want to insert only on POST |
| 4248 | * requests and this one isn't. |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4249 | */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4250 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/\r\n", |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4251 | t->proxy->cookie_name, |
| 4252 | t->srv->cookie ? t->srv->cookie : ""); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4253 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4254 | t->flags |= SN_SCK_INSERTED; |
| 4255 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4256 | /* Here, we will tell an eventual cache on the client side that we don't |
| 4257 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 4258 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 4259 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 4260 | */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4261 | if (t->proxy->options & PR_O_COOK_NOC) |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4262 | //len += sprintf(newhdr + len, "Cache-control: no-cache=\"set-cookie\"\r\n"); |
| 4263 | len += sprintf(trash + len, "Cache-control: private\r\n"); |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 4264 | |
| 4265 | if (rep->data + rep->l < rep->h) |
| 4266 | /* The data has been stolen, we will crash cleanly instead of corrupting memory */ |
| 4267 | *(int *)0 = 0; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4268 | buffer_replace2(rep, rep->h, rep->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4269 | } |
| 4270 | |
| 4271 | /* headers to be added */ |
| 4272 | for (line = 0; line < t->proxy->nb_rspadd; line++) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4273 | len = sprintf(trash, "%s\r\n", t->proxy->rsp_add[line]); |
| 4274 | buffer_replace2(rep, rep->h, rep->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4275 | } |
| 4276 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4277 | /* add a "connection: close" line if needed */ |
| 4278 | if (t->proxy->options & PR_O_HTTP_CLOSE) |
| 4279 | buffer_replace2(rep, rep->h, rep->h, "Connection: close\r\n", 19); |
| 4280 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4281 | t->srv_state = SV_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4282 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4283 | t->logs.t_data = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4284 | |
Willy TARREAU | 767ba71 | 2006-03-01 22:40:50 +0100 | [diff] [blame] | 4285 | /* client connection already closed or option 'httpclose' required : |
| 4286 | * we close the server's outgoing connection right now. |
| 4287 | */ |
| 4288 | if ((req->l == 0) && |
| 4289 | (c == CL_STSHUTR || c == CL_STCLOSE || t->proxy->options & PR_O_FORCE_CLO)) { |
| 4290 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4291 | tv_eternity(&t->swexpire); |
| 4292 | |
| 4293 | /* We must ensure that the read part is still alive when switching |
| 4294 | * to shutw */ |
| 4295 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4296 | if (t->proxy->srvtimeout) |
| 4297 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4298 | |
| 4299 | shutdown(t->srv_fd, SHUT_WR); |
| 4300 | t->srv_state = SV_STSHUTW; |
| 4301 | } |
| 4302 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4303 | /* if the user wants to log as soon as possible, without counting |
| 4304 | bytes from the server, then this is the right moment. */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 4305 | if (t->proxy->to_log && !(t->logs.logwait & LW_BYTES)) { |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4306 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 4307 | t->logs.bytes = rep->h - rep->data; |
| 4308 | sess_log(t); |
| 4309 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4310 | break; |
| 4311 | } |
| 4312 | |
| 4313 | /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */ |
| 4314 | if (ptr > rep->r - 2) { |
| 4315 | /* this is a partial header, let's wait for more to come */ |
| 4316 | rep->lr = ptr; |
| 4317 | break; |
| 4318 | } |
| 4319 | |
| 4320 | // fprintf(stderr,"h=%p, ptr=%p, lr=%p, r=%p, *h=", rep->h, ptr, rep->lr, rep->r); |
| 4321 | // write(2, rep->h, ptr - rep->h); fprintf(stderr,"\n"); |
| 4322 | |
| 4323 | /* now we know that *ptr is either \r or \n, |
| 4324 | * and that there are at least 1 char after it. |
| 4325 | */ |
| 4326 | if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n')) |
| 4327 | rep->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */ |
| 4328 | else |
| 4329 | rep->lr = ptr + 2; /* \r\n or \n\r */ |
| 4330 | |
| 4331 | /* |
| 4332 | * now we know that we have a full header ; we can do whatever |
| 4333 | * we want with these pointers : |
| 4334 | * rep->h = beginning of header |
| 4335 | * ptr = end of header (first \r or \n) |
| 4336 | * rep->lr = beginning of next line (next rep->h) |
| 4337 | * rep->r = end of data (not used at this stage) |
| 4338 | */ |
| 4339 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4340 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4341 | if (t->logs.status == -1) { |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4342 | t->logs.logwait &= ~LW_RESP; |
| 4343 | t->logs.status = atoi(rep->h + 9); |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4344 | switch (t->logs.status) { |
| 4345 | case 200: |
| 4346 | case 203: |
| 4347 | case 206: |
| 4348 | case 300: |
| 4349 | case 301: |
| 4350 | case 410: |
| 4351 | /* RFC2616 @13.4: |
| 4352 | * "A response received with a status code of |
| 4353 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4354 | * by a cache (...) unless a cache-control |
| 4355 | * directive prohibits caching." |
| 4356 | * |
| 4357 | * RFC2616 @9.5: POST method : |
| 4358 | * "Responses to this method are not cacheable, |
| 4359 | * unless the response includes appropriate |
| 4360 | * Cache-Control or Expires header fields." |
| 4361 | */ |
| 4362 | if ((!t->flags & SN_POST) && (t->proxy->options & PR_O_CHK_CACHE)) |
| 4363 | t->flags |= SN_CACHEABLE | SN_CACHE_COOK; |
| 4364 | break; |
| 4365 | default: |
| 4366 | break; |
| 4367 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 4368 | } |
| 4369 | else if (t->logs.logwait & LW_RSPHDR) { |
| 4370 | struct cap_hdr *h; |
| 4371 | int len; |
| 4372 | for (h = t->proxy->rsp_cap; h; h = h->next) { |
| 4373 | if ((h->namelen + 2 <= ptr - rep->h) && |
| 4374 | (rep->h[h->namelen] == ':') && |
| 4375 | (strncasecmp(rep->h, h->name, h->namelen) == 0)) { |
| 4376 | |
| 4377 | if (t->rsp_cap[h->index] == NULL) |
| 4378 | t->rsp_cap[h->index] = pool_alloc_from(h->pool, h->len + 1); |
| 4379 | |
| 4380 | len = ptr - (rep->h + h->namelen + 2); |
| 4381 | if (len > h->len) |
| 4382 | len = h->len; |
| 4383 | |
| 4384 | memcpy(t->rsp_cap[h->index], rep->h + h->namelen + 2, len); |
| 4385 | t->rsp_cap[h->index][len]=0; |
| 4386 | } |
| 4387 | } |
| 4388 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4389 | } |
| 4390 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4391 | delete_header = 0; |
| 4392 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4393 | 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] | 4394 | int len, max; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 4395 | 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] | 4396 | max = ptr - rep->h; |
| 4397 | UBOUND(max, sizeof(trash) - len - 1); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4398 | len += strlcpy2(trash + len, rep->h, max + 1); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4399 | trash[len++] = '\n'; |
| 4400 | write(1, trash, len); |
| 4401 | } |
| 4402 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4403 | /* remove "connection: " if needed */ |
| 4404 | if (!delete_header && (t->proxy->options & PR_O_HTTP_CLOSE) |
| 4405 | && (strncasecmp(rep->h, "Connection: ", 12) == 0)) { |
| 4406 | delete_header = 1; |
| 4407 | } |
| 4408 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4409 | /* try headers regexps */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4410 | if (!delete_header && t->proxy->rsp_exp != NULL |
| 4411 | && !(t->flags & SN_SVDENY)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4412 | struct hdr_exp *exp; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4413 | char term; |
| 4414 | |
| 4415 | term = *ptr; |
| 4416 | *ptr = '\0'; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4417 | exp = t->proxy->rsp_exp; |
| 4418 | do { |
| 4419 | if (regexec(exp->preg, rep->h, MAX_MATCH, pmatch, 0) == 0) { |
| 4420 | switch (exp->action) { |
| 4421 | case ACT_ALLOW: |
| 4422 | if (!(t->flags & SN_SVDENY)) |
| 4423 | t->flags |= SN_SVALLOW; |
| 4424 | break; |
| 4425 | case ACT_REPLACE: |
| 4426 | if (!(t->flags & SN_SVDENY)) { |
| 4427 | int len = exp_replace(trash, rep->h, exp->replace, pmatch); |
| 4428 | ptr += buffer_replace2(rep, rep->h, ptr, trash, len); |
| 4429 | } |
| 4430 | break; |
| 4431 | case ACT_REMOVE: |
| 4432 | if (!(t->flags & SN_SVDENY)) |
| 4433 | delete_header = 1; |
| 4434 | break; |
| 4435 | case ACT_DENY: |
| 4436 | if (!(t->flags & SN_SVALLOW)) |
| 4437 | t->flags |= SN_SVDENY; |
| 4438 | break; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4439 | case ACT_PASS: /* we simply don't deny this one */ |
| 4440 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4441 | } |
| 4442 | break; |
| 4443 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4444 | } while ((exp = exp->next) != NULL); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4445 | *ptr = term; /* restore the string terminator */ |
| 4446 | } |
| 4447 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4448 | /* check for cache-control: or pragma: headers */ |
| 4449 | if (!delete_header && (t->flags & SN_CACHEABLE)) { |
| 4450 | if (strncasecmp(rep->h, "Pragma: no-cache", 16) == 0) |
| 4451 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4452 | else if (strncasecmp(rep->h, "Cache-control: ", 15) == 0) { |
| 4453 | if (strncasecmp(rep->h + 15, "no-cache", 8) == 0) { |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4454 | if (rep->h + 23 == ptr || rep->h[23] == ',') |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4455 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4456 | else { |
| 4457 | if (strncasecmp(rep->h + 23, "=\"set-cookie", 12) == 0 |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4458 | && (rep->h[35] == '"' || rep->h[35] == ',')) |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4459 | t->flags &= ~SN_CACHE_COOK; |
| 4460 | } |
| 4461 | } else if ((strncasecmp(rep->h + 15, "private", 7) == 0 && |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4462 | (rep->h + 22 == ptr || rep->h[22] == ',')) |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4463 | || (strncasecmp(rep->h + 15, "no-store", 8) == 0 && |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4464 | (rep->h + 23 == ptr || rep->h[23] == ','))) { |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4465 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4466 | } else if (strncasecmp(rep->h + 15, "max-age=0", 9) == 0 && |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4467 | (rep->h + 24 == ptr || rep->h[24] == ',')) { |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4468 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4469 | } else if (strncasecmp(rep->h + 15, "s-maxage=0", 10) == 0 && |
| 4470 | (rep->h + 25 == ptr || rep->h[25] == ',')) { |
| 4471 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4472 | } else if (strncasecmp(rep->h + 15, "public", 6) == 0 && |
| 4473 | (rep->h + 21 == ptr || rep->h[21] == ',')) { |
| 4474 | t->flags |= SN_CACHEABLE | SN_CACHE_COOK; |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4475 | } |
| 4476 | } |
| 4477 | } |
| 4478 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4479 | /* check for server cookies */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4480 | if (!delete_header /*&& (t->proxy->options & PR_O_COOK_ANY)*/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4481 | && (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] | 4482 | && (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4483 | char *p1, *p2, *p3, *p4; |
| 4484 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4485 | t->flags |= SN_SCK_ANY; |
| 4486 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4487 | p1 = rep->h + 12; /* first char after 'Set-Cookie: ' */ |
| 4488 | |
| 4489 | while (p1 < ptr) { /* in fact, we'll break after the first cookie */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 4490 | while (p1 < ptr && (isspace((int)*p1))) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4491 | p1++; |
| 4492 | |
| 4493 | if (p1 == ptr || *p1 == ';') /* end of cookie */ |
| 4494 | break; |
| 4495 | |
| 4496 | /* p1 is at the beginning of the cookie name */ |
| 4497 | p2 = p1; |
| 4498 | |
| 4499 | while (p2 < ptr && *p2 != '=' && *p2 != ';') |
| 4500 | p2++; |
| 4501 | |
| 4502 | if (p2 == ptr || *p2 == ';') /* next cookie */ |
| 4503 | break; |
| 4504 | |
| 4505 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4506 | if (p3 == ptr) |
| 4507 | break; |
| 4508 | |
| 4509 | p4 = p3; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 4510 | while (p4 < ptr && !isspace((int)*p4) && *p4 != ';') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4511 | p4++; |
| 4512 | |
| 4513 | /* here, we have the cookie name between p1 and p2, |
| 4514 | * and its value between p3 and p4. |
| 4515 | * we can process it. |
| 4516 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4517 | |
| 4518 | /* first, let's see if we want to capture it */ |
| 4519 | if (t->proxy->capture_name != NULL && |
| 4520 | t->logs.srv_cookie == NULL && |
| 4521 | (p4 - p1 >= t->proxy->capture_namelen) && |
| 4522 | memcmp(p1, t->proxy->capture_name, t->proxy->capture_namelen) == 0) { |
| 4523 | int log_len = p4 - p1; |
| 4524 | |
| 4525 | if ((t->logs.srv_cookie = pool_alloc(capture)) == NULL) { |
| 4526 | Alert("HTTP logging : out of memory.\n"); |
| 4527 | } |
| 4528 | |
| 4529 | if (log_len > t->proxy->capture_len) |
| 4530 | log_len = t->proxy->capture_len; |
| 4531 | memcpy(t->logs.srv_cookie, p1, log_len); |
| 4532 | t->logs.srv_cookie[log_len] = 0; |
| 4533 | } |
| 4534 | |
| 4535 | if ((p2 - p1 == t->proxy->cookie_len) && (t->proxy->cookie_name != NULL) && |
| 4536 | (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4537 | /* Cool... it's the right one */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4538 | t->flags |= SN_SCK_SEEN; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4539 | |
| 4540 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4541 | * this occurrence because we'll insert another one later. |
| 4542 | * We'll delete it too if the "indirect" option is set and we're in |
| 4543 | * a direct access. */ |
| 4544 | if (((t->srv) && (t->proxy->options & PR_O_COOK_INS)) || |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4545 | ((t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_IND))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4546 | /* this header must be deleted */ |
| 4547 | delete_header = 1; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4548 | t->flags |= SN_SCK_DELETED; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4549 | } |
| 4550 | else if ((t->srv) && (t->proxy->options & PR_O_COOK_RW)) { |
| 4551 | /* replace bytes p3->p4 with the cookie name associated |
| 4552 | * with this server since we know it. |
| 4553 | */ |
| 4554 | buffer_replace2(rep, p3, p4, t->srv->cookie, t->srv->cklen); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4555 | t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4556 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 4557 | else if ((t->srv) && (t->proxy->options & PR_O_COOK_PFX)) { |
| 4558 | /* insert the cookie name associated with this server |
| 4559 | * before existing cookie, and insert a delimitor between them.. |
| 4560 | */ |
| 4561 | buffer_replace2(rep, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4562 | p3[t->srv->cklen] = COOKIE_DELIM; |
| 4563 | t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED; |
| 4564 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4565 | break; |
| 4566 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4567 | |
| 4568 | /* first, let's see if the cookie is our appcookie*/ |
| 4569 | if ((t->proxy->appsession_name != NULL) && |
| 4570 | (memcmp(p1, t->proxy->appsession_name, p2 - p1) == 0)) { |
| 4571 | |
| 4572 | /* Cool... it's the right one */ |
| 4573 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4574 | size_t server_id_len = strlen(t->srv->id) + 1; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4575 | asession_temp = &local_asession; |
| 4576 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4577 | 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] | 4578 | Alert("Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 4579 | send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 4580 | } |
| 4581 | memcpy(asession_temp->sessid, p3, t->proxy->appsession_len); |
| 4582 | asession_temp->sessid[t->proxy->appsession_len] = 0; |
| 4583 | asession_temp->serverid = NULL; |
| 4584 | |
| 4585 | /* only do insert, if lookup fails */ |
| 4586 | if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *) &asession_temp) != 0) { |
| 4587 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 4588 | Alert("Not enought Memory process_srv():asession:calloc().\n"); |
| 4589 | send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession:calloc().\n"); |
| 4590 | return 0; |
| 4591 | } |
| 4592 | asession_temp->sessid = local_asession.sessid; |
| 4593 | asession_temp->serverid = local_asession.serverid; |
| 4594 | chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4595 | }/* end if (chtbl_lookup()) */ |
| 4596 | else { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4597 | /* free wasted memory */ |
| 4598 | pool_free_to(apools.sessid, local_asession.sessid); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4599 | } /* end else from if (chtbl_lookup()) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4600 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4601 | if (asession_temp->serverid == NULL) { |
| 4602 | 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] | 4603 | Alert("Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 4604 | send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 4605 | } |
| 4606 | asession_temp->serverid[0] = '\0'; |
| 4607 | } |
| 4608 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4609 | if (asession_temp->serverid[0] == '\0') |
| 4610 | memcpy(asession_temp->serverid,t->srv->id,server_id_len); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4611 | |
| 4612 | tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout); |
| 4613 | |
| 4614 | #if defined(DEBUG_HASH) |
| 4615 | print_table(&(t->proxy->htbl_proxy)); |
| 4616 | #endif |
| 4617 | break; |
| 4618 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4619 | else { |
| 4620 | // fprintf(stderr,"Ignoring unknown cookie : "); |
| 4621 | // write(2, p1, p2-p1); |
| 4622 | // fprintf(stderr," = "); |
| 4623 | // write(2, p3, p4-p3); |
| 4624 | // fprintf(stderr,"\n"); |
| 4625 | } |
| 4626 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4627 | } /* we're now at the end of the cookie value */ |
| 4628 | } /* end of cookie processing */ |
| 4629 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4630 | /* check for any set-cookie in case we check for cacheability */ |
| 4631 | if (!delete_header && !(t->flags & SN_SCK_ANY) && |
| 4632 | (t->proxy->options & PR_O_CHK_CACHE) && |
| 4633 | (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) { |
| 4634 | t->flags |= SN_SCK_ANY; |
| 4635 | } |
| 4636 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4637 | /* let's look if we have to delete this header */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4638 | if (delete_header && !(t->flags & SN_SVDENY)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4639 | buffer_replace2(rep, rep->h, rep->lr, "", 0); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4640 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4641 | rep->h = rep->lr; |
| 4642 | } /* while (rep->lr < rep->r) */ |
| 4643 | |
| 4644 | /* end of header processing (even if incomplete) */ |
| 4645 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4646 | if ((rep->l < rep->rlim - rep->data) && ! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 4647 | /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer |
| 4648 | * full. We cannot loop here since event_srv_read will disable it only if |
| 4649 | * rep->l == rlim-data |
| 4650 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4651 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4652 | if (t->proxy->srvtimeout) |
| 4653 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4654 | else |
| 4655 | tv_eternity(&t->srexpire); |
| 4656 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4657 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4658 | /* read error, write error */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4659 | if (t->res_sw == RES_ERROR || t->res_sr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4660 | tv_eternity(&t->srexpire); |
| 4661 | tv_eternity(&t->swexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4662 | fd_delete(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4663 | t->srv_state = SV_STCLOSE; |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 4664 | t->logs.status = 502; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4665 | client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4666 | if (!(t->flags & SN_ERR_MASK)) |
| 4667 | t->flags |= SN_ERR_SRVCL; |
| 4668 | if (!(t->flags & SN_FINST_MASK)) |
| 4669 | t->flags |= SN_FINST_H; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4670 | return 1; |
| 4671 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4672 | /* end of client write or end of server read. |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4673 | * since we are in header mode, if there's no space left for headers, we |
| 4674 | * won't be able to free more later, so the session will never terminate. |
| 4675 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4676 | 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] | 4677 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4678 | tv_eternity(&t->srexpire); |
| 4679 | shutdown(t->srv_fd, SHUT_RD); |
| 4680 | t->srv_state = SV_STSHUTR; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4681 | //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] | 4682 | return 1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4683 | } |
| 4684 | /* read timeout : return a 504 to the client. |
| 4685 | */ |
| 4686 | else if (FD_ISSET(t->srv_fd, StaticReadEvent) && tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 4687 | tv_eternity(&t->srexpire); |
| 4688 | tv_eternity(&t->swexpire); |
| 4689 | fd_delete(t->srv_fd); |
| 4690 | t->srv_state = SV_STCLOSE; |
| 4691 | t->logs.status = 504; |
| 4692 | client_return(t, t->proxy->errmsg.len504, t->proxy->errmsg.msg504); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4693 | if (!(t->flags & SN_ERR_MASK)) |
| 4694 | t->flags |= SN_ERR_SRVTO; |
| 4695 | if (!(t->flags & SN_FINST_MASK)) |
| 4696 | t->flags |= SN_FINST_H; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4697 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4698 | |
| 4699 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4700 | /* last client read and buffer empty */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4701 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 4702 | * client shuts read too early, because we may still have |
| 4703 | * some work to do on the headers. |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4704 | * The side-effect is that if the client completely closes its |
| 4705 | * connection during SV_STHEADER, the connection to the server |
| 4706 | * is kept until a response comes back or the timeout is reached. |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4707 | */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4708 | else if ((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && (req->l == 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4709 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4710 | tv_eternity(&t->swexpire); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4711 | |
| 4712 | /* We must ensure that the read part is still alive when switching |
| 4713 | * to shutw */ |
| 4714 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4715 | if (t->proxy->srvtimeout) |
| 4716 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4717 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4718 | shutdown(t->srv_fd, SHUT_WR); |
| 4719 | t->srv_state = SV_STSHUTW; |
| 4720 | return 1; |
| 4721 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4722 | /* write timeout */ |
| 4723 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 4724 | * client shuts read too early, because we may still have |
| 4725 | * some work to do on the headers. |
| 4726 | */ |
| 4727 | else if (FD_ISSET(t->srv_fd, StaticWriteEvent) && tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
| 4728 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4729 | tv_eternity(&t->swexpire); |
| 4730 | shutdown(t->srv_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4731 | /* We must ensure that the read part is still alive when switching |
| 4732 | * to shutw */ |
| 4733 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4734 | if (t->proxy->srvtimeout) |
| 4735 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4736 | |
| 4737 | /* We must ensure that the read part is still alive when switching |
| 4738 | * to shutw */ |
| 4739 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4740 | if (t->proxy->srvtimeout) |
| 4741 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4742 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4743 | t->srv_state = SV_STSHUTW; |
| 4744 | if (!(t->flags & SN_ERR_MASK)) |
| 4745 | t->flags |= SN_ERR_SRVTO; |
| 4746 | if (!(t->flags & SN_FINST_MASK)) |
| 4747 | t->flags |= SN_FINST_H; |
| 4748 | return 1; |
| 4749 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4750 | |
| 4751 | if (req->l == 0) { |
| 4752 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4753 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 4754 | tv_eternity(&t->swexpire); |
| 4755 | } |
| 4756 | } |
| 4757 | else { /* client buffer not empty */ |
| 4758 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4759 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4760 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4761 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4762 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 4763 | t->srexpire = t->swexpire; |
| 4764 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4765 | else |
| 4766 | tv_eternity(&t->swexpire); |
| 4767 | } |
| 4768 | } |
| 4769 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4770 | /* be nice with the client side which would like to send a complete header |
| 4771 | * FIXME: COMPLETELY BUGGY !!! not all headers may be processed because the client |
| 4772 | * would read all remaining data at once ! The client should not write past rep->lr |
| 4773 | * when the server is in header state. |
| 4774 | */ |
| 4775 | //return header_processed; |
| 4776 | return t->srv_state != SV_STHEADERS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4777 | } |
| 4778 | else if (s == SV_STDATA) { |
| 4779 | /* read or write error */ |
| 4780 | if (t->res_sw == RES_ERROR || t->res_sr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4781 | tv_eternity(&t->srexpire); |
| 4782 | tv_eternity(&t->swexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4783 | fd_delete(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4784 | t->srv_state = SV_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4785 | if (!(t->flags & SN_ERR_MASK)) |
| 4786 | t->flags |= SN_ERR_SRVCL; |
| 4787 | if (!(t->flags & SN_FINST_MASK)) |
| 4788 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4789 | return 1; |
| 4790 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4791 | /* last read, or end of client write */ |
| 4792 | 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] | 4793 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4794 | tv_eternity(&t->srexpire); |
| 4795 | shutdown(t->srv_fd, SHUT_RD); |
| 4796 | t->srv_state = SV_STSHUTR; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4797 | //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] | 4798 | return 1; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 4799 | } |
| 4800 | /* end of client read and no more data to send */ |
| 4801 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
| 4802 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4803 | tv_eternity(&t->swexpire); |
| 4804 | shutdown(t->srv_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4805 | /* We must ensure that the read part is still alive when switching |
| 4806 | * to shutw */ |
| 4807 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4808 | if (t->proxy->srvtimeout) |
| 4809 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4810 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 4811 | t->srv_state = SV_STSHUTW; |
| 4812 | return 1; |
| 4813 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4814 | /* read timeout */ |
| 4815 | else if (tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 4816 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4817 | tv_eternity(&t->srexpire); |
| 4818 | shutdown(t->srv_fd, SHUT_RD); |
| 4819 | t->srv_state = SV_STSHUTR; |
| 4820 | if (!(t->flags & SN_ERR_MASK)) |
| 4821 | t->flags |= SN_ERR_SRVTO; |
| 4822 | if (!(t->flags & SN_FINST_MASK)) |
| 4823 | t->flags |= SN_FINST_D; |
| 4824 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4825 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4826 | /* write timeout */ |
| 4827 | else if (tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4828 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4829 | tv_eternity(&t->swexpire); |
| 4830 | shutdown(t->srv_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4831 | /* We must ensure that the read part is still alive when switching |
| 4832 | * to shutw */ |
| 4833 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4834 | if (t->proxy->srvtimeout) |
| 4835 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4836 | t->srv_state = SV_STSHUTW; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4837 | if (!(t->flags & SN_ERR_MASK)) |
| 4838 | t->flags |= SN_ERR_SRVTO; |
| 4839 | if (!(t->flags & SN_FINST_MASK)) |
| 4840 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4841 | return 1; |
| 4842 | } |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4843 | |
| 4844 | /* recompute request time-outs */ |
| 4845 | if (req->l == 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4846 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4847 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 4848 | tv_eternity(&t->swexpire); |
| 4849 | } |
| 4850 | } |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4851 | else { /* buffer not empty, there are still data to be transferred */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4852 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4853 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4854 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4855 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4856 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 4857 | t->srexpire = t->swexpire; |
| 4858 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4859 | else |
| 4860 | tv_eternity(&t->swexpire); |
| 4861 | } |
| 4862 | } |
| 4863 | |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4864 | /* recompute response time-outs */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4865 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
| 4866 | if (FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 4867 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4868 | tv_eternity(&t->srexpire); |
| 4869 | } |
| 4870 | } |
| 4871 | else { |
| 4872 | if (! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 4873 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4874 | if (t->proxy->srvtimeout) |
| 4875 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4876 | else |
| 4877 | tv_eternity(&t->srexpire); |
| 4878 | } |
| 4879 | } |
| 4880 | |
| 4881 | return 0; /* other cases change nothing */ |
| 4882 | } |
| 4883 | else if (s == SV_STSHUTR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4884 | if (t->res_sw == RES_ERROR) { |
| 4885 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4886 | tv_eternity(&t->swexpire); |
| 4887 | fd_delete(t->srv_fd); |
| 4888 | //close(t->srv_fd); |
| 4889 | t->srv_state = SV_STCLOSE; |
| 4890 | if (!(t->flags & SN_ERR_MASK)) |
| 4891 | t->flags |= SN_ERR_SRVCL; |
| 4892 | if (!(t->flags & SN_FINST_MASK)) |
| 4893 | t->flags |= SN_FINST_D; |
| 4894 | return 1; |
| 4895 | } |
| 4896 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4897 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4898 | tv_eternity(&t->swexpire); |
| 4899 | fd_delete(t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4900 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4901 | t->srv_state = SV_STCLOSE; |
| 4902 | return 1; |
| 4903 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4904 | else if (tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
| 4905 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4906 | tv_eternity(&t->swexpire); |
| 4907 | fd_delete(t->srv_fd); |
| 4908 | //close(t->srv_fd); |
| 4909 | t->srv_state = SV_STCLOSE; |
| 4910 | if (!(t->flags & SN_ERR_MASK)) |
| 4911 | t->flags |= SN_ERR_SRVTO; |
| 4912 | if (!(t->flags & SN_FINST_MASK)) |
| 4913 | t->flags |= SN_FINST_D; |
| 4914 | return 1; |
| 4915 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4916 | else if (req->l == 0) { |
| 4917 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4918 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 4919 | tv_eternity(&t->swexpire); |
| 4920 | } |
| 4921 | } |
| 4922 | else { /* buffer not empty */ |
| 4923 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4924 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4925 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4926 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4927 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 4928 | t->srexpire = t->swexpire; |
| 4929 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4930 | else |
| 4931 | tv_eternity(&t->swexpire); |
| 4932 | } |
| 4933 | } |
| 4934 | return 0; |
| 4935 | } |
| 4936 | else if (s == SV_STSHUTW) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4937 | if (t->res_sr == RES_ERROR) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4938 | //FD_CLR(t->srv_fd, StaticReadEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4939 | tv_eternity(&t->srexpire); |
| 4940 | fd_delete(t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4941 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4942 | t->srv_state = SV_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4943 | if (!(t->flags & SN_ERR_MASK)) |
| 4944 | t->flags |= SN_ERR_SRVCL; |
| 4945 | if (!(t->flags & SN_FINST_MASK)) |
| 4946 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4947 | return 1; |
| 4948 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4949 | else if (t->res_sr == RES_NULL || c == CL_STSHUTW || c == CL_STCLOSE) { |
| 4950 | //FD_CLR(t->srv_fd, StaticReadEvent); |
| 4951 | tv_eternity(&t->srexpire); |
| 4952 | fd_delete(t->srv_fd); |
| 4953 | //close(t->srv_fd); |
| 4954 | t->srv_state = SV_STCLOSE; |
| 4955 | return 1; |
| 4956 | } |
| 4957 | else if (tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 4958 | //FD_CLR(t->srv_fd, StaticReadEvent); |
| 4959 | tv_eternity(&t->srexpire); |
| 4960 | fd_delete(t->srv_fd); |
| 4961 | //close(t->srv_fd); |
| 4962 | t->srv_state = SV_STCLOSE; |
| 4963 | if (!(t->flags & SN_ERR_MASK)) |
| 4964 | t->flags |= SN_ERR_SRVTO; |
| 4965 | if (!(t->flags & SN_FINST_MASK)) |
| 4966 | t->flags |= SN_FINST_D; |
| 4967 | return 1; |
| 4968 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4969 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
| 4970 | if (FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 4971 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4972 | tv_eternity(&t->srexpire); |
| 4973 | } |
| 4974 | } |
| 4975 | else { |
| 4976 | if (! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 4977 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4978 | if (t->proxy->srvtimeout) |
| 4979 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4980 | else |
| 4981 | tv_eternity(&t->srexpire); |
| 4982 | } |
| 4983 | } |
| 4984 | return 0; |
| 4985 | } |
| 4986 | else { /* SV_STCLOSE : nothing to do */ |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4987 | 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] | 4988 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 4989 | 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] | 4990 | write(1, trash, len); |
| 4991 | } |
| 4992 | return 0; |
| 4993 | } |
| 4994 | return 0; |
| 4995 | } |
| 4996 | |
| 4997 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4998 | /* Processes the client and server jobs of a session task, then |
| 4999 | * puts it back to the wait queue in a clean state, or |
| 5000 | * cleans up its resources if it must be deleted. Returns |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5001 | * the time the task accepts to wait, or TIME_ETERNITY for |
| 5002 | * infinity. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5003 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5004 | int process_session(struct task *t) { |
| 5005 | struct session *s = t->context; |
| 5006 | int fsm_resync = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5007 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5008 | do { |
| 5009 | fsm_resync = 0; |
Willy TARREAU | b451247 | 2006-03-01 22:34:48 +0100 | [diff] [blame] | 5010 | //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] | 5011 | fsm_resync |= process_cli(s); |
Willy TARREAU | b451247 | 2006-03-01 22:34:48 +0100 | [diff] [blame] | 5012 | //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] | 5013 | fsm_resync |= process_srv(s); |
Willy TARREAU | b451247 | 2006-03-01 22:34:48 +0100 | [diff] [blame] | 5014 | //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] | 5015 | } while (fsm_resync); |
| 5016 | |
| 5017 | if (s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5018 | struct timeval min1, min2; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5019 | 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] | 5020 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5021 | tv_min(&min1, &s->crexpire, &s->cwexpire); |
| 5022 | tv_min(&min2, &s->srexpire, &s->swexpire); |
| 5023 | tv_min(&min1, &min1, &s->cnexpire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5024 | tv_min(&t->expire, &min1, &min2); |
| 5025 | |
| 5026 | /* restore t to its place in the task list */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5027 | task_queue(t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5028 | |
Willy TARREAU | 1cec83c | 2006-03-01 22:33:49 +0100 | [diff] [blame] | 5029 | #ifdef DEBUG_FULL |
| 5030 | /* DEBUG code : this should never ever happen, otherwise it indicates |
| 5031 | * that a task still has something to do and will provoke a quick loop. |
| 5032 | */ |
| 5033 | if (tv_remain2(&now, &t->expire) <= 0) |
| 5034 | exit(100); |
| 5035 | #endif |
| 5036 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5037 | return tv_remain2(&now, &t->expire); /* nothing more to do */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5038 | } |
| 5039 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5040 | s->proxy->nbconn--; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5041 | actconn--; |
| 5042 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 5043 | 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] | 5044 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 5045 | 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] | 5046 | write(1, trash, len); |
| 5047 | } |
| 5048 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5049 | s->logs.t_close = tv_diff(&s->logs.tv_accept, &now); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 5050 | if (s->rep != NULL) |
| 5051 | s->logs.bytes = s->rep->total; |
| 5052 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5053 | /* let's do a final log if we need it */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 5054 | 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] | 5055 | sess_log(s); |
| 5056 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5057 | /* the task MUST not be in the run queue anymore */ |
| 5058 | task_delete(t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5059 | session_free(s); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5060 | task_free(t); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5061 | return TIME_ETERNITY; /* rest in peace for eternity */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5062 | } |
| 5063 | |
| 5064 | |
| 5065 | |
| 5066 | /* |
| 5067 | * manages a server health-check. Returns |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5068 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5069 | */ |
| 5070 | int process_chk(struct task *t) { |
| 5071 | struct server *s = t->context; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5072 | struct sockaddr_in sa; |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 5073 | int fd; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5074 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5075 | //fprintf(stderr, "process_chk: task=%p\n", t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5076 | |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 5077 | new_chk: |
| 5078 | fd = s->curfd; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5079 | if (fd < 0) { /* no check currently running */ |
| 5080 | //fprintf(stderr, "process_chk: 2\n"); |
| 5081 | if (tv_cmp2_ms(&t->expire, &now) > 0) { /* not good time yet */ |
| 5082 | task_queue(t); /* restore t to its place in the task list */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5083 | return tv_remain2(&now, &t->expire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5084 | } |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 5085 | |
| 5086 | /* we don't send any health-checks when the proxy is stopped or when |
| 5087 | * the server should not be checked. |
| 5088 | */ |
| 5089 | if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) { |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 5090 | while (tv_cmp2_ms(&t->expire, &now) <= 0) |
| 5091 | tv_delayfrom(&t->expire, &t->expire, s->inter); |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 5092 | task_queue(t); /* restore t to its place in the task list */ |
| 5093 | return tv_remain2(&now, &t->expire); |
| 5094 | } |
| 5095 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5096 | /* we'll initiate a new check */ |
| 5097 | s->result = 0; /* no result yet */ |
| 5098 | if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5099 | if ((fd < global.maxsock) && |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5100 | (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) && |
| 5101 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) { |
| 5102 | //fprintf(stderr, "process_chk: 3\n"); |
| 5103 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5104 | /* we'll connect to the check port on the server */ |
| 5105 | sa = s->addr; |
| 5106 | sa.sin_port = htons(s->check_port); |
| 5107 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 5108 | /* allow specific binding : |
| 5109 | * - server-specific at first |
| 5110 | * - proxy-specific next |
| 5111 | */ |
| 5112 | if (s->state & SRV_BIND_SRC) { |
| 5113 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 5114 | if (bind(fd, (struct sockaddr *)&s->source_addr, sizeof(s->source_addr)) == -1) { |
| 5115 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 5116 | s->proxy->id, s->id); |
| 5117 | s->result = -1; |
| 5118 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5119 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 5120 | else if (s->proxy->options & PR_O_BIND_SRC) { |
| 5121 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 5122 | if (bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) { |
| 5123 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", |
| 5124 | s->proxy->id); |
| 5125 | s->result = -1; |
| 5126 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5127 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 5128 | |
| 5129 | if (!s->result) { |
| 5130 | if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) { |
| 5131 | /* OK, connection in progress or established */ |
| 5132 | |
| 5133 | //fprintf(stderr, "process_chk: 4\n"); |
| 5134 | |
| 5135 | s->curfd = fd; /* that's how we know a test is in progress ;-) */ |
| 5136 | fdtab[fd].owner = t; |
| 5137 | fdtab[fd].read = &event_srv_chk_r; |
| 5138 | fdtab[fd].write = &event_srv_chk_w; |
| 5139 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 5140 | FD_SET(fd, StaticWriteEvent); /* for connect status */ |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 5141 | #ifdef DEBUG_FULL |
| 5142 | assert (!FD_ISSET(fd, StaticReadEvent)); |
| 5143 | #endif |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 5144 | fd_insert(fd); |
| 5145 | /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */ |
| 5146 | tv_delayfrom(&t->expire, &now, s->inter); |
| 5147 | task_queue(t); /* restore t to its place in the task list */ |
| 5148 | return tv_remain(&now, &t->expire); |
| 5149 | } |
| 5150 | else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) { |
| 5151 | s->result = -1; /* a real error */ |
| 5152 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5153 | } |
| 5154 | } |
willy tarreau | 08dedbe | 2005-12-18 01:13:48 +0100 | [diff] [blame] | 5155 | close(fd); /* socket creation error */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5156 | } |
| 5157 | |
| 5158 | if (!s->result) { /* nothing done */ |
| 5159 | //fprintf(stderr, "process_chk: 6\n"); |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 5160 | while (tv_cmp2_ms(&t->expire, &now) <= 0) |
| 5161 | tv_delayfrom(&t->expire, &t->expire, s->inter); |
| 5162 | goto new_chk; /* may be we should initialize a new check */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5163 | } |
| 5164 | |
| 5165 | /* here, we have seen a failure */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5166 | if (s->health > s->rise) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5167 | s->health--; /* still good */ |
| 5168 | else { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5169 | s->state &= ~SRV_RUNNING; |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5170 | if (s->health == s->rise) { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5171 | Warning("Server %s/%s DOWN.\n", s->proxy->id, s->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5172 | send_log(s->proxy, LOG_ALERT, "Server %s/%s is DOWN.\n", s->proxy->id, s->id); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5173 | |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5174 | if (find_server(s->proxy) == NULL) { |
| 5175 | Alert("Proxy %s has no server available !\n", s->proxy->id); |
| 5176 | send_log(s->proxy, LOG_EMERG, "Proxy %s has no server available !\n", s->proxy->id); |
| 5177 | } |
| 5178 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5179 | s->health = 0; /* failure */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5180 | } |
| 5181 | |
| 5182 | //fprintf(stderr, "process_chk: 7\n"); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5183 | /* 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] | 5184 | while (tv_cmp2_ms(&t->expire, &now) <= 0) |
| 5185 | tv_delayfrom(&t->expire, &t->expire, s->inter); |
| 5186 | goto new_chk; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5187 | } |
| 5188 | else { |
| 5189 | //fprintf(stderr, "process_chk: 8\n"); |
| 5190 | /* there was a test running */ |
| 5191 | if (s->result > 0) { /* good server detected */ |
| 5192 | //fprintf(stderr, "process_chk: 9\n"); |
| 5193 | s->health++; /* was bad, stays for a while */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5194 | if (s->health >= s->rise) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5195 | if (s->health == s->rise) { |
willy tarreau | fd6dfe7 | 2006-03-19 19:38:19 +0100 | [diff] [blame] | 5196 | Warning("Server %s/%s UP.\n", s->proxy->id, s->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5197 | send_log(s->proxy, LOG_NOTICE, "Server %s/%s is UP.\n", s->proxy->id, s->id); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5198 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5199 | |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5200 | s->health = s->rise + s->fall - 1; /* OK now */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5201 | s->state |= SRV_RUNNING; |
| 5202 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5203 | s->curfd = -1; /* no check running anymore */ |
| 5204 | //FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5205 | fd_delete(fd); |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 5206 | while (tv_cmp2_ms(&t->expire, &now) <= 0) |
| 5207 | tv_delayfrom(&t->expire, &t->expire, s->inter); |
| 5208 | goto new_chk; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5209 | } |
| 5210 | else if (s->result < 0 || tv_cmp2_ms(&t->expire, &now) <= 0) { |
| 5211 | //fprintf(stderr, "process_chk: 10\n"); |
| 5212 | /* failure or timeout detected */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5213 | if (s->health > s->rise) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5214 | s->health--; /* still good */ |
| 5215 | else { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5216 | s->state &= ~SRV_RUNNING; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5217 | |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5218 | if (s->health == s->rise) { |
| 5219 | Warning("Server %s/%s DOWN.\n", s->proxy->id, s->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5220 | send_log(s->proxy, LOG_ALERT, "Server %s/%s is DOWN.\n", s->proxy->id, s->id); |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5221 | |
| 5222 | if (find_server(s->proxy) == NULL) { |
| 5223 | Alert("Proxy %s has no server available !\n", s->proxy->id); |
| 5224 | send_log(s->proxy, LOG_EMERG, "Proxy %s has no server available !\n", s->proxy->id); |
| 5225 | } |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5226 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5227 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5228 | s->health = 0; /* failure */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5229 | } |
| 5230 | s->curfd = -1; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5231 | //FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5232 | fd_delete(fd); |
willy tarreau | 25424f8 | 2006-03-19 19:37:48 +0100 | [diff] [blame] | 5233 | while (tv_cmp2_ms(&t->expire, &now) <= 0) |
| 5234 | tv_delayfrom(&t->expire, &t->expire, s->inter); |
| 5235 | goto new_chk; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5236 | } |
| 5237 | /* if result is 0 and there's no timeout, we have to wait again */ |
| 5238 | } |
| 5239 | //fprintf(stderr, "process_chk: 11\n"); |
| 5240 | s->result = 0; |
| 5241 | task_queue(t); /* restore t to its place in the task list */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5242 | return tv_remain2(&now, &t->expire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5243 | } |
| 5244 | |
| 5245 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5246 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5247 | #if STATTIME > 0 |
| 5248 | int stats(void); |
| 5249 | #endif |
| 5250 | |
| 5251 | /* |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5252 | * This does 4 things : |
| 5253 | * - wake up all expired tasks |
| 5254 | * - call all runnable tasks |
| 5255 | * - call maintain_proxies() to enable/disable the listeners |
| 5256 | * - return the delay till next event in ms, -1 = wait indefinitely |
| 5257 | * Note: this part should be rewritten with the O(ln(n)) scheduler. |
| 5258 | * |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5259 | */ |
| 5260 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5261 | int process_runnable_tasks() { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5262 | int next_time; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5263 | int time2; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5264 | struct task *t, *tnext; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5265 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5266 | next_time = TIME_ETERNITY; /* set the timer to wait eternally first */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5267 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5268 | /* look for expired tasks and add them to the run queue. |
| 5269 | */ |
| 5270 | tnext = ((struct task *)LIST_HEAD(wait_queue))->next; |
| 5271 | while ((t = tnext) != LIST_HEAD(wait_queue)) { /* we haven't looped ? */ |
| 5272 | tnext = t->next; |
| 5273 | if (t->state & TASK_RUNNING) |
| 5274 | continue; |
| 5275 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5276 | if (tv_iseternity(&t->expire)) |
| 5277 | continue; |
| 5278 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5279 | /* wakeup expired entries. It doesn't matter if they are |
| 5280 | * already running because of a previous event |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5281 | */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5282 | if (tv_cmp_ms(&t->expire, &now) <= 0) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5283 | task_wakeup(&rq, t); |
| 5284 | } |
| 5285 | else { |
| 5286 | /* first non-runnable task. Use its expiration date as an upper bound */ |
| 5287 | int temp_time = tv_remain(&now, &t->expire); |
| 5288 | if (temp_time) |
| 5289 | next_time = temp_time; |
| 5290 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5291 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5292 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5293 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5294 | /* process each task in the run queue now. Each task may be deleted |
| 5295 | * since we only use tnext. |
| 5296 | */ |
| 5297 | tnext = rq; |
| 5298 | while ((t = tnext) != NULL) { |
| 5299 | int temp_time; |
| 5300 | |
| 5301 | tnext = t->rqnext; |
| 5302 | task_sleep(&rq, t); |
| 5303 | temp_time = t->process(t); |
| 5304 | next_time = MINTIME(temp_time, next_time); |
| 5305 | } |
| 5306 | |
| 5307 | /* maintain all proxies in a consistent state. This should quickly become a task */ |
| 5308 | time2 = maintain_proxies(); |
| 5309 | return MINTIME(time2, next_time); |
| 5310 | } |
| 5311 | |
| 5312 | |
| 5313 | #if defined(ENABLE_EPOLL) |
| 5314 | |
| 5315 | /* |
| 5316 | * Main epoll() loop. |
| 5317 | */ |
| 5318 | |
| 5319 | /* does 3 actions : |
| 5320 | * 0 (POLL_LOOP_ACTION_INIT) : initializes necessary private structures |
| 5321 | * 1 (POLL_LOOP_ACTION_RUN) : runs the loop |
| 5322 | * 2 (POLL_LOOP_ACTION_CLEAN) : cleans up |
| 5323 | * |
| 5324 | * returns 0 if initialization failed, !0 otherwise. |
| 5325 | */ |
| 5326 | |
| 5327 | int epoll_loop(int action) { |
| 5328 | int next_time; |
| 5329 | int status; |
| 5330 | int fd; |
| 5331 | |
| 5332 | int fds, count; |
| 5333 | int pr, pw, sr, sw; |
| 5334 | unsigned rn, ro, wn, wo; /* read new, read old, write new, write old */ |
| 5335 | struct epoll_event ev; |
| 5336 | |
| 5337 | /* private data */ |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5338 | static struct epoll_event *epoll_events = NULL; |
| 5339 | static int epoll_fd; |
| 5340 | |
| 5341 | if (action == POLL_LOOP_ACTION_INIT) { |
| 5342 | epoll_fd = epoll_create(global.maxsock + 1); |
| 5343 | if (epoll_fd < 0) |
| 5344 | return 0; |
| 5345 | else { |
| 5346 | epoll_events = (struct epoll_event*) |
| 5347 | calloc(1, sizeof(struct epoll_event) * global.maxsock); |
| 5348 | PrevReadEvent = (fd_set *) |
| 5349 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
| 5350 | PrevWriteEvent = (fd_set *) |
| 5351 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5352 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5353 | return 1; |
| 5354 | } |
| 5355 | else if (action == POLL_LOOP_ACTION_CLEAN) { |
| 5356 | if (PrevWriteEvent) free(PrevWriteEvent); |
| 5357 | if (PrevReadEvent) free(PrevReadEvent); |
| 5358 | if (epoll_events) free(epoll_events); |
| 5359 | close(epoll_fd); |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5360 | epoll_fd = 0; |
| 5361 | return 1; |
| 5362 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5363 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5364 | /* OK, it's POLL_LOOP_ACTION_RUN */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5365 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5366 | tv_now(&now); |
| 5367 | |
| 5368 | while (1) { |
| 5369 | next_time = process_runnable_tasks(); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5370 | |
| 5371 | /* stop when there's no connection left and we don't allow them anymore */ |
| 5372 | if (!actconn && listeners == 0) |
| 5373 | break; |
| 5374 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5375 | #if STATTIME > 0 |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5376 | { |
| 5377 | int time2; |
| 5378 | time2 = stats(); |
| 5379 | next_time = MINTIME(time2, next_time); |
| 5380 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5381 | #endif |
| 5382 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5383 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) { |
| 5384 | |
| 5385 | rn = ((int*)StaticReadEvent)[fds]; ro = ((int*)PrevReadEvent)[fds]; |
| 5386 | wn = ((int*)StaticWriteEvent)[fds]; wo = ((int*)PrevWriteEvent)[fds]; |
| 5387 | |
| 5388 | if ((ro^rn) | (wo^wn)) { |
| 5389 | for (count = 0, fd = fds << INTBITS; count < (1<<INTBITS) && fd < maxfd; count++, fd++) { |
| 5390 | #define FDSETS_ARE_INT_ALIGNED |
| 5391 | #ifdef FDSETS_ARE_INT_ALIGNED |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5392 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5393 | #define WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
| 5394 | #ifdef WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5395 | pr = (ro >> count) & 1; |
| 5396 | pw = (wo >> count) & 1; |
| 5397 | sr = (rn >> count) & 1; |
| 5398 | sw = (wn >> count) & 1; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5399 | #else |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5400 | pr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&ro); |
| 5401 | pw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wo); |
| 5402 | sr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&rn); |
| 5403 | sw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wn); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5404 | #endif |
| 5405 | #else |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5406 | pr = FD_ISSET(fd, PrevReadEvent); |
| 5407 | pw = FD_ISSET(fd, PrevWriteEvent); |
| 5408 | sr = FD_ISSET(fd, StaticReadEvent); |
| 5409 | sw = FD_ISSET(fd, StaticWriteEvent); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5410 | #endif |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5411 | if (!((sr^pr) | (sw^pw))) |
| 5412 | continue; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5413 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5414 | ev.events = (sr ? EPOLLIN : 0) | (sw ? EPOLLOUT : 0); |
| 5415 | ev.data.fd = fd; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5416 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5417 | #ifdef EPOLL_CTL_MOD_WORKAROUND |
| 5418 | /* I encountered a rarely reproducible problem with |
| 5419 | * EPOLL_CTL_MOD where a modified FD (systematically |
| 5420 | * the one in epoll_events[0], fd#7) would sometimes |
| 5421 | * be set EPOLL_OUT while asked for a read ! This is |
| 5422 | * with the 2.4 epoll patch. The workaround is to |
| 5423 | * delete then recreate in case of modification. |
| 5424 | * This is in 2.4 up to epoll-lt-0.21 but not in 2.6 |
| 5425 | * nor RHEL kernels. |
| 5426 | */ |
| 5427 | |
| 5428 | if ((pr | pw) && fdtab[fd].state != FD_STCLOSE) |
| 5429 | epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &ev); |
| 5430 | |
| 5431 | if ((sr | sw)) |
| 5432 | epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev); |
| 5433 | #else |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5434 | if ((pr | pw)) { |
| 5435 | /* the file-descriptor already exists... */ |
| 5436 | if ((sr | sw)) { |
| 5437 | /* ...and it will still exist */ |
| 5438 | if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &ev) < 0) { |
| 5439 | // perror("epoll_ctl(MOD)"); |
| 5440 | // exit(1); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5441 | } |
| 5442 | } else { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5443 | /* ...and it will be removed */ |
| 5444 | if (fdtab[fd].state != FD_STCLOSE && |
| 5445 | epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &ev) < 0) { |
| 5446 | // perror("epoll_ctl(DEL)"); |
| 5447 | // exit(1); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5448 | } |
| 5449 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5450 | } else { |
| 5451 | /* the file-descriptor did not exist, let's add it */ |
| 5452 | if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) { |
| 5453 | // perror("epoll_ctl(ADD)"); |
| 5454 | // exit(1); |
| 5455 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5456 | } |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5457 | #endif // EPOLL_CTL_MOD_WORKAROUND |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5458 | } |
| 5459 | ((int*)PrevReadEvent)[fds] = rn; |
| 5460 | ((int*)PrevWriteEvent)[fds] = wn; |
| 5461 | } |
| 5462 | } |
| 5463 | |
| 5464 | /* now let's wait for events */ |
| 5465 | status = epoll_wait(epoll_fd, epoll_events, maxfd, next_time); |
| 5466 | tv_now(&now); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5467 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5468 | for (count = 0; count < status; count++) { |
| 5469 | fd = epoll_events[count].data.fd; |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 5470 | |
| 5471 | if (FD_ISSET(fd, StaticReadEvent)) { |
| 5472 | if (fdtab[fd].state == FD_STCLOSE) |
| 5473 | continue; |
| 5474 | if (epoll_events[count].events & ( EPOLLIN | EPOLLERR | EPOLLHUP )) |
| 5475 | fdtab[fd].read(fd); |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 5476 | } |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 5477 | |
| 5478 | if (FD_ISSET(fd, StaticWriteEvent)) { |
| 5479 | if (fdtab[fd].state == FD_STCLOSE) |
| 5480 | continue; |
| 5481 | if (epoll_events[count].events & ( EPOLLOUT | EPOLLERR | EPOLLHUP )) |
| 5482 | fdtab[fd].write(fd); |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 5483 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5484 | } |
| 5485 | } |
| 5486 | return 1; |
| 5487 | } |
| 5488 | #endif |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5489 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5490 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5491 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5492 | #if defined(ENABLE_POLL) |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5493 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5494 | /* |
| 5495 | * Main poll() loop. |
| 5496 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5497 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5498 | /* does 3 actions : |
| 5499 | * 0 (POLL_LOOP_ACTION_INIT) : initializes necessary private structures |
| 5500 | * 1 (POLL_LOOP_ACTION_RUN) : runs the loop |
| 5501 | * 2 (POLL_LOOP_ACTION_CLEAN) : cleans up |
| 5502 | * |
| 5503 | * returns 0 if initialization failed, !0 otherwise. |
| 5504 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5505 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5506 | int poll_loop(int action) { |
| 5507 | int next_time; |
| 5508 | int status; |
| 5509 | int fd, nbfd; |
| 5510 | |
| 5511 | int fds, count; |
| 5512 | int sr, sw; |
| 5513 | unsigned rn, wn; /* read new, write new */ |
| 5514 | |
| 5515 | /* private data */ |
| 5516 | static struct pollfd *poll_events = NULL; |
| 5517 | |
| 5518 | if (action == POLL_LOOP_ACTION_INIT) { |
| 5519 | poll_events = (struct pollfd*) |
| 5520 | calloc(1, sizeof(struct pollfd) * global.maxsock); |
| 5521 | return 1; |
| 5522 | } |
| 5523 | else if (action == POLL_LOOP_ACTION_CLEAN) { |
| 5524 | if (poll_events) |
| 5525 | free(poll_events); |
| 5526 | return 1; |
| 5527 | } |
| 5528 | |
| 5529 | /* OK, it's POLL_LOOP_ACTION_RUN */ |
| 5530 | |
| 5531 | tv_now(&now); |
| 5532 | |
| 5533 | while (1) { |
| 5534 | next_time = process_runnable_tasks(); |
| 5535 | |
| 5536 | /* stop when there's no connection left and we don't allow them anymore */ |
| 5537 | if (!actconn && listeners == 0) |
| 5538 | break; |
| 5539 | |
| 5540 | #if STATTIME > 0 |
| 5541 | { |
| 5542 | int time2; |
| 5543 | time2 = stats(); |
| 5544 | next_time = MINTIME(time2, next_time); |
| 5545 | } |
| 5546 | #endif |
| 5547 | |
| 5548 | |
| 5549 | nbfd = 0; |
| 5550 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) { |
| 5551 | |
| 5552 | rn = ((int*)StaticReadEvent)[fds]; |
| 5553 | wn = ((int*)StaticWriteEvent)[fds]; |
| 5554 | |
| 5555 | if ((rn|wn)) { |
| 5556 | for (count = 0, fd = fds << INTBITS; count < (1<<INTBITS) && fd < maxfd; count++, fd++) { |
| 5557 | #define FDSETS_ARE_INT_ALIGNED |
| 5558 | #ifdef FDSETS_ARE_INT_ALIGNED |
| 5559 | |
| 5560 | #define WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
| 5561 | #ifdef WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
| 5562 | sr = (rn >> count) & 1; |
| 5563 | sw = (wn >> count) & 1; |
| 5564 | #else |
| 5565 | sr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&rn); |
| 5566 | sw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wn); |
| 5567 | #endif |
| 5568 | #else |
| 5569 | sr = FD_ISSET(fd, StaticReadEvent); |
| 5570 | sw = FD_ISSET(fd, StaticWriteEvent); |
| 5571 | #endif |
| 5572 | if ((sr|sw)) { |
| 5573 | poll_events[nbfd].fd = fd; |
| 5574 | poll_events[nbfd].events = (sr ? POLLIN : 0) | (sw ? POLLOUT : 0); |
| 5575 | nbfd++; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5576 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5577 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5578 | } |
| 5579 | } |
| 5580 | |
| 5581 | /* now let's wait for events */ |
| 5582 | status = poll(poll_events, nbfd, next_time); |
| 5583 | tv_now(&now); |
| 5584 | |
| 5585 | for (count = 0; status > 0 && count < nbfd; count++) { |
| 5586 | fd = poll_events[count].fd; |
| 5587 | |
| 5588 | if (!poll_events[count].revents & ( POLLOUT | POLLIN | POLLERR | POLLHUP )) |
| 5589 | continue; |
| 5590 | |
| 5591 | /* ok, we found one active fd */ |
| 5592 | status--; |
| 5593 | |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 5594 | if (FD_ISSET(fd, StaticReadEvent)) { |
| 5595 | if (fdtab[fd].state == FD_STCLOSE) |
| 5596 | continue; |
| 5597 | if (poll_events[count].revents & ( POLLIN | POLLERR | POLLHUP )) |
| 5598 | fdtab[fd].read(fd); |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 5599 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5600 | |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 5601 | if (FD_ISSET(fd, StaticWriteEvent)) { |
| 5602 | if (fdtab[fd].state == FD_STCLOSE) |
| 5603 | continue; |
| 5604 | if (poll_events[count].revents & ( POLLOUT | POLLERR | POLLHUP )) |
| 5605 | fdtab[fd].write(fd); |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 5606 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5607 | } |
| 5608 | } |
| 5609 | return 1; |
| 5610 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5611 | #endif |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5612 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5613 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5614 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5615 | /* |
| 5616 | * Main select() loop. |
| 5617 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5618 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5619 | /* does 3 actions : |
| 5620 | * 0 (POLL_LOOP_ACTION_INIT) : initializes necessary private structures |
| 5621 | * 1 (POLL_LOOP_ACTION_RUN) : runs the loop |
| 5622 | * 2 (POLL_LOOP_ACTION_CLEAN) : cleans up |
| 5623 | * |
| 5624 | * returns 0 if initialization failed, !0 otherwise. |
| 5625 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5626 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5627 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5628 | int select_loop(int action) { |
| 5629 | int next_time; |
| 5630 | int status; |
| 5631 | int fd,i; |
| 5632 | struct timeval delta; |
| 5633 | int readnotnull, writenotnull; |
| 5634 | static fd_set *ReadEvent = NULL, *WriteEvent = NULL; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5635 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5636 | if (action == POLL_LOOP_ACTION_INIT) { |
| 5637 | ReadEvent = (fd_set *) |
| 5638 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
| 5639 | WriteEvent = (fd_set *) |
| 5640 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
| 5641 | return 1; |
| 5642 | } |
| 5643 | else if (action == POLL_LOOP_ACTION_CLEAN) { |
| 5644 | if (WriteEvent) free(WriteEvent); |
| 5645 | if (ReadEvent) free(ReadEvent); |
| 5646 | return 1; |
| 5647 | } |
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 | /* OK, it's POLL_LOOP_ACTION_RUN */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5650 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5651 | tv_now(&now); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5652 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5653 | while (1) { |
| 5654 | next_time = process_runnable_tasks(); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5655 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5656 | /* stop when there's no connection left and we don't allow them anymore */ |
| 5657 | if (!actconn && listeners == 0) |
| 5658 | break; |
| 5659 | |
| 5660 | #if STATTIME > 0 |
| 5661 | { |
| 5662 | int time2; |
| 5663 | time2 = stats(); |
| 5664 | next_time = MINTIME(time2, next_time); |
| 5665 | } |
| 5666 | #endif |
| 5667 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5668 | if (next_time > 0) { /* FIXME */ |
| 5669 | /* Convert to timeval */ |
| 5670 | /* to avoid eventual select loops due to timer precision */ |
| 5671 | next_time += SCHEDULER_RESOLUTION; |
| 5672 | delta.tv_sec = next_time / 1000; |
| 5673 | delta.tv_usec = (next_time % 1000) * 1000; |
| 5674 | } |
| 5675 | else if (next_time == 0) { /* allow select to return immediately when needed */ |
| 5676 | delta.tv_sec = delta.tv_usec = 0; |
| 5677 | } |
| 5678 | |
| 5679 | |
| 5680 | /* let's restore fdset state */ |
| 5681 | |
| 5682 | readnotnull = 0; writenotnull = 0; |
| 5683 | for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) { |
| 5684 | readnotnull |= (*(((int*)ReadEvent)+i) = *(((int*)StaticReadEvent)+i)) != 0; |
| 5685 | writenotnull |= (*(((int*)WriteEvent)+i) = *(((int*)StaticWriteEvent)+i)) != 0; |
| 5686 | } |
| 5687 | |
| 5688 | // /* just a verification code, needs to be removed for performance */ |
| 5689 | // for (i=0; i<maxfd; i++) { |
| 5690 | // if (FD_ISSET(i, ReadEvent) != FD_ISSET(i, StaticReadEvent)) |
| 5691 | // abort(); |
| 5692 | // if (FD_ISSET(i, WriteEvent) != FD_ISSET(i, StaticWriteEvent)) |
| 5693 | // abort(); |
| 5694 | // |
| 5695 | // } |
| 5696 | |
| 5697 | status = select(maxfd, |
| 5698 | readnotnull ? ReadEvent : NULL, |
| 5699 | writenotnull ? WriteEvent : NULL, |
| 5700 | NULL, |
| 5701 | (next_time >= 0) ? &delta : NULL); |
| 5702 | |
| 5703 | /* this is an experiment on the separation of the select work */ |
| 5704 | // status = (readnotnull ? select(maxfd, ReadEvent, NULL, NULL, (next_time >= 0) ? &delta : NULL) : 0); |
| 5705 | // status |= (writenotnull ? select(maxfd, NULL, WriteEvent, NULL, (next_time >= 0) ? &delta : NULL) : 0); |
| 5706 | |
| 5707 | tv_now(&now); |
| 5708 | |
| 5709 | if (status > 0) { /* must proceed with events */ |
| 5710 | |
| 5711 | int fds; |
| 5712 | char count; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5713 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5714 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) |
| 5715 | if ((((int *)(ReadEvent))[fds] | ((int *)(WriteEvent))[fds]) != 0) |
| 5716 | for (count = 1<<INTBITS, fd = fds << INTBITS; count && fd < maxfd; count--, fd++) { |
| 5717 | |
| 5718 | /* if we specify read first, the accepts and zero reads will be |
| 5719 | * seen first. Moreover, system buffers will be flushed faster. |
| 5720 | */ |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 5721 | if (FD_ISSET(fd, ReadEvent)) { |
| 5722 | if (fdtab[fd].state == FD_STCLOSE) |
| 5723 | continue; |
| 5724 | fdtab[fd].read(fd); |
| 5725 | } |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 5726 | |
willy tarreau | 05be12b | 2006-03-19 19:35:00 +0100 | [diff] [blame] | 5727 | if (FD_ISSET(fd, WriteEvent)) { |
| 5728 | if (fdtab[fd].state == FD_STCLOSE) |
| 5729 | continue; |
| 5730 | fdtab[fd].write(fd); |
| 5731 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5732 | } |
| 5733 | } |
| 5734 | else { |
| 5735 | // fprintf(stderr,"select returned %d, maxfd=%d\n", status, maxfd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5736 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5737 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5738 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5739 | } |
| 5740 | |
| 5741 | |
| 5742 | #if STATTIME > 0 |
| 5743 | /* |
| 5744 | * Display proxy statistics regularly. It is designed to be called from the |
| 5745 | * select_loop(). |
| 5746 | */ |
| 5747 | int stats(void) { |
| 5748 | static int lines; |
| 5749 | static struct timeval nextevt; |
| 5750 | static struct timeval lastevt; |
| 5751 | static struct timeval starttime = {0,0}; |
| 5752 | unsigned long totaltime, deltatime; |
| 5753 | int ret; |
| 5754 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5755 | if (tv_cmp(&now, &nextevt) > 0) { |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 5756 | deltatime = (tv_diff(&lastevt, &now)?:1); |
| 5757 | totaltime = (tv_diff(&starttime, &now)?:1); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5758 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5759 | if (global.mode & MODE_STATS) { |
| 5760 | if ((lines++ % 16 == 0) && !(global.mode & MODE_LOG)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5761 | qfprintf(stderr, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5762 | "\n active total tsknew tskgood tskleft tskrght tsknsch tsklsch tskrsch\n"); |
| 5763 | if (lines>1) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5764 | qfprintf(stderr,"%07d %07d %07d %07d %07d %07d %07d %07d %07d\n", |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5765 | actconn, totalconn, |
| 5766 | stats_tsk_new, stats_tsk_good, |
| 5767 | stats_tsk_left, stats_tsk_right, |
| 5768 | stats_tsk_nsrch, stats_tsk_lsrch, stats_tsk_rsrch); |
| 5769 | } |
| 5770 | } |
| 5771 | |
| 5772 | tv_delayfrom(&nextevt, &now, STATTIME); |
| 5773 | |
| 5774 | lastevt=now; |
| 5775 | } |
| 5776 | ret = tv_remain(&now, &nextevt); |
| 5777 | return ret; |
| 5778 | } |
| 5779 | #endif |
| 5780 | |
| 5781 | |
| 5782 | /* |
| 5783 | * this function enables proxies when there are enough free sessions, |
| 5784 | * 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] | 5785 | * select_loop(). It returns the time left before next expiration event |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5786 | * during stop time, TIME_ETERNITY otherwise. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5787 | */ |
| 5788 | static int maintain_proxies(void) { |
| 5789 | struct proxy *p; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5790 | struct listener *l; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5791 | int tleft; /* time left */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5792 | |
| 5793 | p = proxy; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5794 | tleft = TIME_ETERNITY; /* infinite time */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5795 | |
| 5796 | /* if there are enough free sessions, we'll activate proxies */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5797 | if (actconn < global.maxconn) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5798 | while (p) { |
| 5799 | if (p->nbconn < p->maxconn) { |
| 5800 | if (p->state == PR_STIDLE) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5801 | for (l = p->listen; l != NULL; l = l->next) { |
| 5802 | FD_SET(l->fd, StaticReadEvent); |
| 5803 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5804 | p->state = PR_STRUN; |
| 5805 | } |
| 5806 | } |
| 5807 | else { |
| 5808 | if (p->state == PR_STRUN) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5809 | for (l = p->listen; l != NULL; l = l->next) { |
| 5810 | FD_CLR(l->fd, StaticReadEvent); |
| 5811 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5812 | p->state = PR_STIDLE; |
| 5813 | } |
| 5814 | } |
| 5815 | p = p->next; |
| 5816 | } |
| 5817 | } |
| 5818 | else { /* block all proxies */ |
| 5819 | while (p) { |
| 5820 | if (p->state == PR_STRUN) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5821 | for (l = p->listen; l != NULL; l = l->next) { |
| 5822 | FD_CLR(l->fd, StaticReadEvent); |
| 5823 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5824 | p->state = PR_STIDLE; |
| 5825 | } |
| 5826 | p = p->next; |
| 5827 | } |
| 5828 | } |
| 5829 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5830 | if (stopping) { |
| 5831 | p = proxy; |
| 5832 | while (p) { |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5833 | if (p->state != PR_STSTOPPED) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5834 | int t; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5835 | t = tv_remain2(&now, &p->stop_time); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5836 | if (t == 0) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5837 | Warning("Proxy %s stopped.\n", p->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5838 | send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5839 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5840 | for (l = p->listen; l != NULL; l = l->next) { |
| 5841 | fd_delete(l->fd); |
| 5842 | listeners--; |
| 5843 | } |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5844 | p->state = PR_STSTOPPED; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5845 | } |
| 5846 | else { |
| 5847 | tleft = MINTIME(t, tleft); |
| 5848 | } |
| 5849 | } |
| 5850 | p = p->next; |
| 5851 | } |
| 5852 | } |
| 5853 | return tleft; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5854 | } |
| 5855 | |
| 5856 | /* |
| 5857 | * 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] | 5858 | * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace |
| 5859 | * 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] | 5860 | */ |
| 5861 | static void soft_stop(void) { |
| 5862 | struct proxy *p; |
| 5863 | |
| 5864 | stopping = 1; |
| 5865 | p = proxy; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5866 | tv_now(&now); /* else, the old time before select will be used */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5867 | while (p) { |
Willy TARREAU | 2bfdd8e | 2006-03-12 18:03:05 +0100 | [diff] [blame] | 5868 | if (p->state != PR_STSTOPPED) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5869 | Warning("Stopping proxy %s in %d ms.\n", p->id, p->grace); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5870 | 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] | 5871 | tv_delayfrom(&p->stop_time, &now, p->grace); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5872 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5873 | p = p->next; |
| 5874 | } |
| 5875 | } |
| 5876 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5877 | static void pause_proxy(struct proxy *p) { |
| 5878 | struct listener *l; |
| 5879 | for (l = p->listen; l != NULL; l = l->next) { |
| 5880 | shutdown(l->fd, SHUT_RD); |
| 5881 | FD_CLR(l->fd, StaticReadEvent); |
| 5882 | p->state = PR_STPAUSED; |
| 5883 | } |
| 5884 | } |
| 5885 | |
| 5886 | /* |
| 5887 | * This function temporarily disables listening so that another new instance |
| 5888 | * 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] | 5889 | * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5890 | * the proxy, or a SIGTTIN can be sent to listen again. |
| 5891 | */ |
| 5892 | static void pause_proxies(void) { |
| 5893 | struct proxy *p; |
| 5894 | |
| 5895 | p = proxy; |
| 5896 | tv_now(&now); /* else, the old time before select will be used */ |
| 5897 | while (p) { |
| 5898 | if (p->state != PR_STSTOPPED && p->state != PR_STPAUSED) { |
| 5899 | Warning("Pausing proxy %s.\n", p->id); |
| 5900 | send_log(p, LOG_WARNING, "Pausing proxy %s.\n", p->id); |
| 5901 | pause_proxy(p); |
| 5902 | } |
| 5903 | p = p->next; |
| 5904 | } |
| 5905 | } |
| 5906 | |
| 5907 | |
| 5908 | /* |
| 5909 | * This function reactivates listening. This can be used after a call to |
| 5910 | * sig_pause(), for example when a new instance has failed starting up. |
| 5911 | * It is designed to be called upon reception of a SIGTTIN. |
| 5912 | */ |
| 5913 | static void listen_proxies(void) { |
| 5914 | struct proxy *p; |
| 5915 | struct listener *l; |
| 5916 | |
| 5917 | p = proxy; |
| 5918 | tv_now(&now); /* else, the old time before select will be used */ |
| 5919 | while (p) { |
| 5920 | if (p->state == PR_STPAUSED) { |
| 5921 | Warning("Enabling proxy %s.\n", p->id); |
| 5922 | send_log(p, LOG_WARNING, "Enabling proxy %s.\n", p->id); |
| 5923 | |
| 5924 | for (l = p->listen; l != NULL; l = l->next) { |
| 5925 | if (listen(l->fd, p->maxconn) == 0) { |
| 5926 | if (actconn < global.maxconn && p->nbconn < p->maxconn) { |
| 5927 | FD_SET(l->fd, StaticReadEvent); |
| 5928 | p->state = PR_STRUN; |
| 5929 | } |
| 5930 | else |
| 5931 | p->state = PR_STIDLE; |
| 5932 | } else { |
willy tarreau | cb2e562 | 2006-01-29 21:55:30 +0100 | [diff] [blame] | 5933 | int port; |
| 5934 | |
| 5935 | if (l->addr.ss_family == AF_INET6) |
| 5936 | port = ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port); |
| 5937 | else |
| 5938 | port = ntohs(((struct sockaddr_in *)(&l->addr))->sin_port); |
| 5939 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5940 | Warning("Port %d busy while trying to enable proxy %s.\n", |
willy tarreau | cb2e562 | 2006-01-29 21:55:30 +0100 | [diff] [blame] | 5941 | port, p->id); |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5942 | 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] | 5943 | port, p->id); |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5944 | /* Another port might have been enabled. Let's stop everything. */ |
| 5945 | pause_proxy(p); |
| 5946 | break; |
| 5947 | } |
| 5948 | } |
| 5949 | } |
| 5950 | p = p->next; |
| 5951 | } |
| 5952 | } |
| 5953 | |
| 5954 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5955 | /* |
| 5956 | * upon SIGUSR1, let's have a soft stop. |
| 5957 | */ |
| 5958 | void sig_soft_stop(int sig) { |
| 5959 | soft_stop(); |
| 5960 | signal(sig, SIG_IGN); |
| 5961 | } |
| 5962 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5963 | /* |
| 5964 | * upon SIGTTOU, we pause everything |
| 5965 | */ |
| 5966 | void sig_pause(int sig) { |
| 5967 | pause_proxies(); |
| 5968 | signal(sig, sig_pause); |
| 5969 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5970 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 5971 | /* |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5972 | * upon SIGTTIN, let's have a soft stop. |
| 5973 | */ |
| 5974 | void sig_listen(int sig) { |
| 5975 | listen_proxies(); |
| 5976 | signal(sig, sig_listen); |
| 5977 | } |
| 5978 | |
| 5979 | /* |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 5980 | * this function dumps every server's state when the process receives SIGHUP. |
| 5981 | */ |
| 5982 | void sig_dump_state(int sig) { |
| 5983 | struct proxy *p = proxy; |
| 5984 | |
| 5985 | Warning("SIGHUP received, dumping servers states.\n"); |
| 5986 | while (p) { |
| 5987 | struct server *s = p->srv; |
| 5988 | |
| 5989 | send_log(p, LOG_NOTICE, "SIGUP received, dumping servers states.\n"); |
| 5990 | while (s) { |
| 5991 | if (s->state & SRV_RUNNING) { |
willy tarreau | fd6dfe7 | 2006-03-19 19:38:19 +0100 | [diff] [blame] | 5992 | Warning("SIGHUP: Server %s/%s is UP.\n", p->id, s->id); |
| 5993 | 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] | 5994 | } |
| 5995 | else { |
willy tarreau | fd6dfe7 | 2006-03-19 19:38:19 +0100 | [diff] [blame] | 5996 | Warning("SIGHUP: Server %s/%s is DOWN.\n", p->id, s->id); |
| 5997 | 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] | 5998 | } |
| 5999 | s = s->next; |
| 6000 | } |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 6001 | |
| 6002 | if (find_server(p) == NULL) { |
willy tarreau | 1fb3493 | 2006-03-23 11:22:10 +0100 | [diff] [blame] | 6003 | Warning("SIGHUP: Proxy %s has no server available !\n", p->id); |
| 6004 | send_log(p, LOG_NOTICE, "SIGHUP: Proxy %s has no server available !\n", p->id); |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 6005 | } |
| 6006 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6007 | p = p->next; |
| 6008 | } |
| 6009 | signal(sig, sig_dump_state); |
| 6010 | } |
| 6011 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6012 | void dump(int sig) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6013 | struct task *t, *tnext; |
| 6014 | struct session *s; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6015 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6016 | tnext = ((struct task *)LIST_HEAD(wait_queue))->next; |
| 6017 | while ((t = tnext) != LIST_HEAD(wait_queue)) { /* we haven't looped ? */ |
| 6018 | tnext = t->next; |
| 6019 | s = t->context; |
| 6020 | qfprintf(stderr,"[dump] wq: task %p, still %ld ms, " |
| 6021 | "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, " |
| 6022 | "req=%d, rep=%d, clifd=%d\n", |
| 6023 | s, tv_remain(&now, &t->expire), |
| 6024 | s->cli_state, |
| 6025 | s->srv_state, |
| 6026 | FD_ISSET(s->cli_fd, StaticReadEvent), |
| 6027 | FD_ISSET(s->cli_fd, StaticWriteEvent), |
| 6028 | FD_ISSET(s->srv_fd, StaticReadEvent), |
| 6029 | FD_ISSET(s->srv_fd, StaticWriteEvent), |
| 6030 | s->req->l, s->rep?s->rep->l:0, s->cli_fd |
| 6031 | ); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6032 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6033 | } |
| 6034 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 6035 | #ifdef DEBUG_MEMORY |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6036 | static void fast_stop(void) |
| 6037 | { |
| 6038 | struct proxy *p; |
| 6039 | p = proxy; |
| 6040 | while (p) { |
| 6041 | p->grace = 0; |
| 6042 | p = p->next; |
| 6043 | } |
| 6044 | soft_stop(); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6045 | } |
| 6046 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6047 | void sig_int(int sig) { |
| 6048 | /* This would normally be a hard stop, |
| 6049 | but we want to be sure about deallocation, |
| 6050 | and so on, so we do a soft stop with |
| 6051 | 0 GRACE time |
| 6052 | */ |
| 6053 | fast_stop(); |
| 6054 | /* If we are killed twice, we decide to die*/ |
| 6055 | signal(sig, SIG_DFL); |
| 6056 | } |
| 6057 | |
| 6058 | void sig_term(int sig) { |
| 6059 | /* This would normally be a hard stop, |
| 6060 | but we want to be sure about deallocation, |
| 6061 | and so on, so we do a soft stop with |
| 6062 | 0 GRACE time |
| 6063 | */ |
| 6064 | fast_stop(); |
| 6065 | /* If we are killed twice, we decide to die*/ |
| 6066 | signal(sig, SIG_DFL); |
| 6067 | } |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 6068 | #endif |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6069 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 6070 | /* returns the pointer to an error in the replacement string, or NULL if OK */ |
| 6071 | 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] | 6072 | struct hdr_exp *exp; |
| 6073 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 6074 | if (replace != NULL) { |
| 6075 | char *err; |
| 6076 | err = check_replace_string(replace); |
| 6077 | if (err) |
| 6078 | return err; |
| 6079 | } |
| 6080 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 6081 | while (*head != NULL) |
| 6082 | head = &(*head)->next; |
| 6083 | |
| 6084 | exp = calloc(1, sizeof(struct hdr_exp)); |
| 6085 | |
| 6086 | exp->preg = preg; |
| 6087 | exp->replace = replace; |
| 6088 | exp->action = action; |
| 6089 | *head = exp; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 6090 | |
| 6091 | return NULL; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 6092 | } |
| 6093 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6094 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6095 | /* |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6096 | * 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] | 6097 | */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6098 | int cfg_parse_global(char *file, int linenum, char **args) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6099 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6100 | if (!strcmp(args[0], "global")) { /* new section */ |
| 6101 | /* no option, nothing special to do */ |
| 6102 | return 0; |
| 6103 | } |
| 6104 | else if (!strcmp(args[0], "daemon")) { |
| 6105 | global.mode |= MODE_DAEMON; |
| 6106 | } |
| 6107 | else if (!strcmp(args[0], "debug")) { |
| 6108 | global.mode |= MODE_DEBUG; |
| 6109 | } |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 6110 | else if (!strcmp(args[0], "noepoll")) { |
| 6111 | cfg_polling_mechanism &= ~POLL_USE_EPOLL; |
| 6112 | } |
| 6113 | else if (!strcmp(args[0], "nopoll")) { |
| 6114 | cfg_polling_mechanism &= ~POLL_USE_POLL; |
| 6115 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6116 | else if (!strcmp(args[0], "quiet")) { |
| 6117 | global.mode |= MODE_QUIET; |
| 6118 | } |
| 6119 | else if (!strcmp(args[0], "stats")) { |
| 6120 | global.mode |= MODE_STATS; |
| 6121 | } |
| 6122 | else if (!strcmp(args[0], "uid")) { |
| 6123 | if (global.uid != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6124 | 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] | 6125 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6126 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6127 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6128 | 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] | 6129 | return -1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6130 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6131 | global.uid = atol(args[1]); |
| 6132 | } |
| 6133 | else if (!strcmp(args[0], "gid")) { |
| 6134 | if (global.gid != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6135 | 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] | 6136 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6137 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6138 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6139 | 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] | 6140 | return -1; |
| 6141 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6142 | global.gid = atol(args[1]); |
| 6143 | } |
| 6144 | else if (!strcmp(args[0], "nbproc")) { |
| 6145 | if (global.nbproc != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6146 | 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] | 6147 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6148 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6149 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6150 | 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] | 6151 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6152 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6153 | global.nbproc = atol(args[1]); |
| 6154 | } |
| 6155 | else if (!strcmp(args[0], "maxconn")) { |
| 6156 | if (global.maxconn != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6157 | 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] | 6158 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6159 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6160 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6161 | 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] | 6162 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6163 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6164 | global.maxconn = atol(args[1]); |
Willy TARREAU | 13032e7 | 2006-03-12 17:31:45 +0100 | [diff] [blame] | 6165 | #ifdef SYSTEM_MAXCONN |
| 6166 | if (global.maxconn > DEFAULT_MAXCONN && cfg_maxconn <= DEFAULT_MAXCONN) { |
| 6167 | 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); |
| 6168 | global.maxconn = DEFAULT_MAXCONN; |
| 6169 | } |
| 6170 | #endif /* SYSTEM_MAXCONN */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6171 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 6172 | else if (!strcmp(args[0], "ulimit-n")) { |
| 6173 | if (global.rlimit_nofile != 0) { |
| 6174 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
| 6175 | return 0; |
| 6176 | } |
| 6177 | if (*(args[1]) == 0) { |
| 6178 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
| 6179 | return -1; |
| 6180 | } |
| 6181 | global.rlimit_nofile = atol(args[1]); |
| 6182 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6183 | else if (!strcmp(args[0], "chroot")) { |
| 6184 | if (global.chroot != NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6185 | 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] | 6186 | return 0; |
| 6187 | } |
| 6188 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6189 | 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] | 6190 | return -1; |
| 6191 | } |
| 6192 | global.chroot = strdup(args[1]); |
| 6193 | } |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 6194 | else if (!strcmp(args[0], "pidfile")) { |
| 6195 | if (global.pidfile != NULL) { |
| 6196 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
| 6197 | return 0; |
| 6198 | } |
| 6199 | if (*(args[1]) == 0) { |
| 6200 | Alert("parsing [%s:%d] : '%s' expects a file name as an argument.\n", file, linenum, args[0]); |
| 6201 | return -1; |
| 6202 | } |
| 6203 | global.pidfile = strdup(args[1]); |
| 6204 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6205 | else if (!strcmp(args[0], "log")) { /* syslog server address */ |
| 6206 | struct sockaddr_in *sa; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6207 | int facility, level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6208 | |
| 6209 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6210 | 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] | 6211 | return -1; |
| 6212 | } |
| 6213 | |
| 6214 | for (facility = 0; facility < NB_LOG_FACILITIES; facility++) |
| 6215 | if (!strcmp(log_facilities[facility], args[2])) |
| 6216 | break; |
| 6217 | |
| 6218 | if (facility >= NB_LOG_FACILITIES) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6219 | 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] | 6220 | exit(1); |
| 6221 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6222 | |
| 6223 | level = 7; /* max syslog level = debug */ |
| 6224 | if (*(args[3])) { |
| 6225 | while (level >= 0 && strcmp(log_levels[level], args[3])) |
| 6226 | level--; |
| 6227 | if (level < 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6228 | 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] | 6229 | exit(1); |
| 6230 | } |
| 6231 | } |
| 6232 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6233 | sa = str2sa(args[1]); |
| 6234 | if (!sa->sin_port) |
| 6235 | sa->sin_port = htons(SYSLOG_PORT); |
| 6236 | |
| 6237 | if (global.logfac1 == -1) { |
| 6238 | global.logsrv1 = *sa; |
| 6239 | global.logfac1 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6240 | global.loglev1 = level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6241 | } |
| 6242 | else if (global.logfac2 == -1) { |
| 6243 | global.logsrv2 = *sa; |
| 6244 | global.logfac2 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6245 | global.loglev2 = level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6246 | } |
| 6247 | else { |
| 6248 | Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum); |
| 6249 | return -1; |
| 6250 | } |
| 6251 | |
| 6252 | } |
| 6253 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6254 | 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] | 6255 | return -1; |
| 6256 | } |
| 6257 | return 0; |
| 6258 | } |
| 6259 | |
| 6260 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6261 | void init_default_instance() { |
| 6262 | memset(&defproxy, 0, sizeof(defproxy)); |
| 6263 | defproxy.mode = PR_MODE_TCP; |
| 6264 | defproxy.state = PR_STNEW; |
| 6265 | defproxy.maxconn = cfg_maxpconn; |
| 6266 | defproxy.conn_retries = CONN_RETRIES; |
| 6267 | defproxy.logfac1 = defproxy.logfac2 = -1; /* log disabled */ |
| 6268 | } |
| 6269 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6270 | /* |
| 6271 | * parse a line in a <listen> section. Returns 0 if OK, -1 if error. |
| 6272 | */ |
| 6273 | int cfg_parse_listen(char *file, int linenum, char **args) { |
| 6274 | static struct proxy *curproxy = NULL; |
| 6275 | struct server *newsrv = NULL; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 6276 | char *err; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6277 | int rc; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6278 | |
| 6279 | if (!strcmp(args[0], "listen")) { /* new proxy */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6280 | if (!*args[1]) { |
| 6281 | Alert("parsing [%s:%d] : '%s' expects an <id> argument and\n" |
| 6282 | " optionnally supports [addr1]:port1[-end1]{,[addr]:port[-end]}...\n", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6283 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6284 | return -1; |
| 6285 | } |
| 6286 | |
| 6287 | if ((curproxy = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6288 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6289 | return -1; |
| 6290 | } |
| 6291 | curproxy->next = proxy; |
| 6292 | proxy = curproxy; |
| 6293 | curproxy->id = strdup(args[1]); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 6294 | |
| 6295 | /* parse the listener address if any */ |
| 6296 | if (*args[2]) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6297 | curproxy->listen = str2listener(args[2], curproxy->listen); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 6298 | if (!curproxy->listen) |
| 6299 | return -1; |
Willy TARREAU | 203b0b6 | 2006-03-12 18:00:28 +0100 | [diff] [blame] | 6300 | global.maxsock++; |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 6301 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6302 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6303 | /* set default values */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6304 | curproxy->state = defproxy.state; |
| 6305 | curproxy->maxconn = defproxy.maxconn; |
| 6306 | curproxy->conn_retries = defproxy.conn_retries; |
| 6307 | curproxy->options = defproxy.options; |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6308 | |
| 6309 | if (defproxy.check_req) |
| 6310 | curproxy->check_req = strdup(defproxy.check_req); |
| 6311 | curproxy->check_len = defproxy.check_len; |
| 6312 | |
| 6313 | if (defproxy.cookie_name) |
| 6314 | curproxy->cookie_name = strdup(defproxy.cookie_name); |
| 6315 | curproxy->cookie_len = defproxy.cookie_len; |
| 6316 | |
| 6317 | if (defproxy.capture_name) |
| 6318 | curproxy->capture_name = strdup(defproxy.capture_name); |
| 6319 | curproxy->capture_namelen = defproxy.capture_namelen; |
| 6320 | curproxy->capture_len = defproxy.capture_len; |
| 6321 | |
| 6322 | if (defproxy.errmsg.msg400) |
| 6323 | curproxy->errmsg.msg400 = strdup(defproxy.errmsg.msg400); |
| 6324 | curproxy->errmsg.len400 = defproxy.errmsg.len400; |
| 6325 | |
| 6326 | if (defproxy.errmsg.msg403) |
| 6327 | curproxy->errmsg.msg403 = strdup(defproxy.errmsg.msg403); |
| 6328 | curproxy->errmsg.len403 = defproxy.errmsg.len403; |
| 6329 | |
| 6330 | if (defproxy.errmsg.msg408) |
| 6331 | curproxy->errmsg.msg408 = strdup(defproxy.errmsg.msg408); |
| 6332 | curproxy->errmsg.len408 = defproxy.errmsg.len408; |
| 6333 | |
| 6334 | if (defproxy.errmsg.msg500) |
| 6335 | curproxy->errmsg.msg500 = strdup(defproxy.errmsg.msg500); |
| 6336 | curproxy->errmsg.len500 = defproxy.errmsg.len500; |
| 6337 | |
| 6338 | if (defproxy.errmsg.msg502) |
| 6339 | curproxy->errmsg.msg502 = strdup(defproxy.errmsg.msg502); |
| 6340 | curproxy->errmsg.len502 = defproxy.errmsg.len502; |
| 6341 | |
| 6342 | if (defproxy.errmsg.msg503) |
| 6343 | curproxy->errmsg.msg503 = strdup(defproxy.errmsg.msg503); |
| 6344 | curproxy->errmsg.len503 = defproxy.errmsg.len503; |
| 6345 | |
| 6346 | if (defproxy.errmsg.msg504) |
| 6347 | curproxy->errmsg.msg504 = strdup(defproxy.errmsg.msg504); |
| 6348 | curproxy->errmsg.len504 = defproxy.errmsg.len504; |
| 6349 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6350 | curproxy->clitimeout = defproxy.clitimeout; |
| 6351 | curproxy->contimeout = defproxy.contimeout; |
| 6352 | curproxy->srvtimeout = defproxy.srvtimeout; |
| 6353 | curproxy->mode = defproxy.mode; |
| 6354 | curproxy->logfac1 = defproxy.logfac1; |
| 6355 | curproxy->logsrv1 = defproxy.logsrv1; |
| 6356 | curproxy->loglev1 = defproxy.loglev1; |
| 6357 | curproxy->logfac2 = defproxy.logfac2; |
| 6358 | curproxy->logsrv2 = defproxy.logsrv2; |
| 6359 | curproxy->loglev2 = defproxy.loglev2; |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 6360 | curproxy->to_log = defproxy.to_log & ~LW_COOKIE & ~LW_REQHDR & ~ LW_RSPHDR; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6361 | curproxy->grace = defproxy.grace; |
| 6362 | curproxy->source_addr = defproxy.source_addr; |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 6363 | curproxy->mon_net = defproxy.mon_net; |
| 6364 | curproxy->mon_mask = defproxy.mon_mask; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6365 | return 0; |
| 6366 | } |
| 6367 | 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] | 6368 | /* some variables may have already been initialized earlier */ |
| 6369 | if (defproxy.check_req) free(defproxy.check_req); |
| 6370 | if (defproxy.cookie_name) free(defproxy.cookie_name); |
| 6371 | if (defproxy.capture_name) free(defproxy.capture_name); |
| 6372 | if (defproxy.errmsg.msg400) free(defproxy.errmsg.msg400); |
| 6373 | if (defproxy.errmsg.msg403) free(defproxy.errmsg.msg403); |
| 6374 | if (defproxy.errmsg.msg408) free(defproxy.errmsg.msg408); |
| 6375 | if (defproxy.errmsg.msg500) free(defproxy.errmsg.msg500); |
| 6376 | if (defproxy.errmsg.msg502) free(defproxy.errmsg.msg502); |
| 6377 | if (defproxy.errmsg.msg503) free(defproxy.errmsg.msg503); |
| 6378 | if (defproxy.errmsg.msg504) free(defproxy.errmsg.msg504); |
| 6379 | |
| 6380 | init_default_instance(); |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6381 | curproxy = &defproxy; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6382 | return 0; |
| 6383 | } |
| 6384 | else if (curproxy == NULL) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6385 | Alert("parsing [%s:%d] : 'listen' or 'defaults' expected.\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6386 | return -1; |
| 6387 | } |
| 6388 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6389 | if (!strcmp(args[0], "bind")) { /* new listen addresses */ |
| 6390 | if (curproxy == &defproxy) { |
| 6391 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6392 | return -1; |
| 6393 | } |
| 6394 | |
| 6395 | if (strchr(args[1], ':') == NULL) { |
| 6396 | Alert("parsing [%s:%d] : '%s' expects [addr1]:port1[-end1]{,[addr]:port[-end]}... as arguments.\n", |
| 6397 | file, linenum, args[0]); |
| 6398 | return -1; |
| 6399 | } |
| 6400 | curproxy->listen = str2listener(args[1], curproxy->listen); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 6401 | if (!curproxy->listen) |
| 6402 | return -1; |
Willy TARREAU | 203b0b6 | 2006-03-12 18:00:28 +0100 | [diff] [blame] | 6403 | global.maxsock++; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6404 | return 0; |
| 6405 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 6406 | else if (!strcmp(args[0], "monitor-net")) { /* set the range of IPs to ignore */ |
| 6407 | if (!*args[1] || !str2net(args[1], &curproxy->mon_net, &curproxy->mon_mask)) { |
| 6408 | Alert("parsing [%s:%d] : '%s' expects address[/mask].\n", |
| 6409 | file, linenum, args[0]); |
| 6410 | return -1; |
| 6411 | } |
| 6412 | /* flush useless bits */ |
| 6413 | curproxy->mon_net.s_addr &= curproxy->mon_mask.s_addr; |
| 6414 | return 0; |
| 6415 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6416 | else if (!strcmp(args[0], "mode")) { /* sets the proxy mode */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6417 | if (!strcmp(args[1], "http")) curproxy->mode = PR_MODE_HTTP; |
| 6418 | else if (!strcmp(args[1], "tcp")) curproxy->mode = PR_MODE_TCP; |
| 6419 | else if (!strcmp(args[1], "health")) curproxy->mode = PR_MODE_HEALTH; |
| 6420 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6421 | 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] | 6422 | return -1; |
| 6423 | } |
| 6424 | } |
| 6425 | else if (!strcmp(args[0], "disabled")) { /* disables this proxy */ |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 6426 | curproxy->state = PR_STSTOPPED; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6427 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6428 | else if (!strcmp(args[0], "enabled")) { /* enables this proxy (used to revert a disabled default) */ |
| 6429 | curproxy->state = PR_STNEW; |
| 6430 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6431 | else if (!strcmp(args[0], "cookie")) { /* cookie name */ |
| 6432 | int cur_arg; |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6433 | // if (curproxy == &defproxy) { |
| 6434 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6435 | // return -1; |
| 6436 | // } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6437 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6438 | if (curproxy->cookie_name != NULL) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6439 | // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n", |
| 6440 | // file, linenum); |
| 6441 | // return 0; |
| 6442 | free(curproxy->cookie_name); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6443 | } |
| 6444 | |
| 6445 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6446 | Alert("parsing [%s:%d] : '%s' expects <cookie_name> as argument.\n", |
| 6447 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6448 | return -1; |
| 6449 | } |
| 6450 | curproxy->cookie_name = strdup(args[1]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6451 | curproxy->cookie_len = strlen(curproxy->cookie_name); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6452 | |
| 6453 | cur_arg = 2; |
| 6454 | while (*(args[cur_arg])) { |
| 6455 | if (!strcmp(args[cur_arg], "rewrite")) { |
| 6456 | curproxy->options |= PR_O_COOK_RW; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6457 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6458 | else if (!strcmp(args[cur_arg], "indirect")) { |
| 6459 | curproxy->options |= PR_O_COOK_IND; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6460 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6461 | else if (!strcmp(args[cur_arg], "insert")) { |
| 6462 | curproxy->options |= PR_O_COOK_INS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6463 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 6464 | else if (!strcmp(args[cur_arg], "nocache")) { |
| 6465 | curproxy->options |= PR_O_COOK_NOC; |
| 6466 | } |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 6467 | else if (!strcmp(args[cur_arg], "postonly")) { |
| 6468 | curproxy->options |= PR_O_COOK_POST; |
| 6469 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6470 | else if (!strcmp(args[cur_arg], "prefix")) { |
| 6471 | curproxy->options |= PR_O_COOK_PFX; |
| 6472 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6473 | else { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6474 | 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] | 6475 | file, linenum, args[0]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6476 | return -1; |
| 6477 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6478 | cur_arg++; |
| 6479 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6480 | if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_IND))) { |
| 6481 | Alert("parsing [%s:%d] : cookie 'rewrite' and 'indirect' modes are incompatible.\n", |
| 6482 | file, linenum); |
| 6483 | return -1; |
| 6484 | } |
| 6485 | |
| 6486 | if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_INS|PR_O_COOK_PFX))) { |
| 6487 | 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] | 6488 | file, linenum); |
| 6489 | return -1; |
| 6490 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6491 | }/* end else if (!strcmp(args[0], "cookie")) */ |
| 6492 | else if (!strcmp(args[0], "appsession")) { /* cookie name */ |
| 6493 | // if (curproxy == &defproxy) { |
| 6494 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6495 | // return -1; |
| 6496 | // } |
| 6497 | |
| 6498 | if (curproxy->appsession_name != NULL) { |
| 6499 | // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n", |
| 6500 | // file, linenum); |
| 6501 | // return 0; |
| 6502 | free(curproxy->appsession_name); |
| 6503 | } |
| 6504 | |
| 6505 | if (*(args[5]) == 0) { |
| 6506 | Alert("parsing [%s:%d] : '%s' expects 'appsession' <cookie_name> 'len' <len> 'timeout' <timeout>.\n", |
| 6507 | file, linenum, args[0]); |
| 6508 | return -1; |
| 6509 | } |
| 6510 | have_appsession = 1; |
| 6511 | curproxy->appsession_name = strdup(args[1]); |
| 6512 | curproxy->appsession_name_len = strlen(curproxy->appsession_name); |
| 6513 | curproxy->appsession_len = atoi(args[3]); |
| 6514 | curproxy->appsession_timeout = atoi(args[5]); |
| 6515 | rc = chtbl_init(&(curproxy->htbl_proxy), TBLSIZ, hashpjw, match_str, destroy); |
| 6516 | if (rc) { |
| 6517 | Alert("Error Init Appsession Hashtable.\n"); |
| 6518 | return -1; |
| 6519 | } |
| 6520 | } /* Url App Session */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 6521 | else if (!strcmp(args[0], "capture")) { |
| 6522 | if (!strcmp(args[1], "cookie")) { /* name of a cookie to capture */ |
| 6523 | // if (curproxy == &defproxy) { |
| 6524 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6525 | // return -1; |
| 6526 | // } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6527 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 6528 | if (curproxy->capture_name != NULL) { |
| 6529 | // Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", |
| 6530 | // file, linenum, args[0]); |
| 6531 | // return 0; |
| 6532 | free(curproxy->capture_name); |
| 6533 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6534 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 6535 | if (*(args[4]) == 0) { |
| 6536 | Alert("parsing [%s:%d] : '%s' expects 'cookie' <cookie_name> 'len' <len>.\n", |
| 6537 | file, linenum, args[0]); |
| 6538 | return -1; |
| 6539 | } |
| 6540 | curproxy->capture_name = strdup(args[2]); |
| 6541 | curproxy->capture_namelen = strlen(curproxy->capture_name); |
| 6542 | curproxy->capture_len = atol(args[4]); |
| 6543 | if (curproxy->capture_len >= CAPTURE_LEN) { |
| 6544 | Warning("parsing [%s:%d] : truncating capture length to %d bytes.\n", |
| 6545 | file, linenum, CAPTURE_LEN - 1); |
| 6546 | curproxy->capture_len = CAPTURE_LEN - 1; |
| 6547 | } |
| 6548 | curproxy->to_log |= LW_COOKIE; |
| 6549 | } |
| 6550 | else if (!strcmp(args[1], "request") && !strcmp(args[2], "header")) { |
| 6551 | struct cap_hdr *hdr; |
| 6552 | |
| 6553 | if (curproxy == &defproxy) { |
| 6554 | Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]); |
| 6555 | return -1; |
| 6556 | } |
| 6557 | |
| 6558 | if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) { |
| 6559 | Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n", |
| 6560 | file, linenum, args[0], args[1]); |
| 6561 | return -1; |
| 6562 | } |
| 6563 | |
| 6564 | hdr = calloc(sizeof(struct cap_hdr), 1); |
| 6565 | hdr->next = curproxy->req_cap; |
| 6566 | hdr->name = strdup(args[3]); |
| 6567 | hdr->namelen = strlen(args[3]); |
| 6568 | hdr->len = atol(args[5]); |
| 6569 | hdr->index = curproxy->nb_req_cap++; |
| 6570 | curproxy->req_cap = hdr; |
| 6571 | curproxy->to_log |= LW_REQHDR; |
| 6572 | } |
| 6573 | else if (!strcmp(args[1], "response") && !strcmp(args[2], "header")) { |
| 6574 | struct cap_hdr *hdr; |
| 6575 | |
| 6576 | if (curproxy == &defproxy) { |
| 6577 | Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]); |
| 6578 | return -1; |
| 6579 | } |
| 6580 | |
| 6581 | if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) { |
| 6582 | Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n", |
| 6583 | file, linenum, args[0], args[1]); |
| 6584 | return -1; |
| 6585 | } |
| 6586 | hdr = calloc(sizeof(struct cap_hdr), 1); |
| 6587 | hdr->next = curproxy->rsp_cap; |
| 6588 | hdr->name = strdup(args[3]); |
| 6589 | hdr->namelen = strlen(args[3]); |
| 6590 | hdr->len = atol(args[5]); |
| 6591 | hdr->index = curproxy->nb_rsp_cap++; |
| 6592 | curproxy->rsp_cap = hdr; |
| 6593 | curproxy->to_log |= LW_RSPHDR; |
| 6594 | } |
| 6595 | else { |
| 6596 | 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] | 6597 | file, linenum, args[0]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6598 | return -1; |
| 6599 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6600 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6601 | else if (!strcmp(args[0], "contimeout")) { /* connect timeout */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6602 | if (curproxy->contimeout != defproxy.contimeout) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6603 | 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] | 6604 | return 0; |
| 6605 | } |
| 6606 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6607 | Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n", |
| 6608 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6609 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6610 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6611 | curproxy->contimeout = atol(args[1]); |
| 6612 | } |
| 6613 | else if (!strcmp(args[0], "clitimeout")) { /* client timeout */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6614 | if (curproxy->clitimeout != defproxy.clitimeout) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6615 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", |
| 6616 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6617 | return 0; |
| 6618 | } |
| 6619 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6620 | Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n", |
| 6621 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6622 | return -1; |
| 6623 | } |
| 6624 | curproxy->clitimeout = atol(args[1]); |
| 6625 | } |
| 6626 | else if (!strcmp(args[0], "srvtimeout")) { /* server timeout */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6627 | if (curproxy->srvtimeout != defproxy.srvtimeout) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6628 | 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] | 6629 | return 0; |
| 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 an integer <time_in_ms> as argument.\n", |
| 6633 | file, linenum, args[0]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6634 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6635 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6636 | curproxy->srvtimeout = atol(args[1]); |
| 6637 | } |
| 6638 | else if (!strcmp(args[0], "retries")) { /* connection retries */ |
| 6639 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6640 | Alert("parsing [%s:%d] : '%s' expects an integer argument (dispatch counts for one).\n", |
| 6641 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6642 | return -1; |
| 6643 | } |
| 6644 | curproxy->conn_retries = atol(args[1]); |
| 6645 | } |
| 6646 | else if (!strcmp(args[0], "option")) { |
| 6647 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6648 | 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] | 6649 | return -1; |
| 6650 | } |
| 6651 | if (!strcmp(args[1], "redispatch")) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6652 | /* enable reconnections to dispatch */ |
| 6653 | curproxy->options |= PR_O_REDISP; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 6654 | #ifdef TPROXY |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6655 | else if (!strcmp(args[1], "transparent")) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6656 | /* enable transparent proxy connections */ |
| 6657 | curproxy->options |= PR_O_TRANSP; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6658 | #endif |
| 6659 | else if (!strcmp(args[1], "keepalive")) |
| 6660 | /* enable keep-alive */ |
| 6661 | curproxy->options |= PR_O_KEEPALIVE; |
| 6662 | else if (!strcmp(args[1], "forwardfor")) |
| 6663 | /* insert x-forwarded-for field */ |
| 6664 | curproxy->options |= PR_O_FWDFOR; |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 6665 | else if (!strcmp(args[1], "logasap")) |
| 6666 | /* log as soon as possible, without waiting for the session to complete */ |
| 6667 | curproxy->options |= PR_O_LOGASAP; |
| 6668 | else if (!strcmp(args[1], "httpclose")) |
| 6669 | /* force connection: close in both directions in HTTP mode */ |
| 6670 | curproxy->options |= PR_O_HTTP_CLOSE; |
Willy TARREAU | 767ba71 | 2006-03-01 22:40:50 +0100 | [diff] [blame] | 6671 | else if (!strcmp(args[1], "forceclose")) |
| 6672 | /* force connection: close in both directions in HTTP mode and enforce end of session */ |
| 6673 | curproxy->options |= PR_O_FORCE_CLO | PR_O_HTTP_CLOSE; |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 6674 | else if (!strcmp(args[1], "checkcache")) |
| 6675 | /* require examination of cacheability of the 'set-cookie' field */ |
| 6676 | curproxy->options |= PR_O_CHK_CACHE; |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 6677 | else if (!strcmp(args[1], "httplog")) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6678 | /* generate a complete HTTP log */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 6679 | 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] | 6680 | else if (!strcmp(args[1], "tcplog")) |
| 6681 | /* generate a detailed TCP log */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 6682 | 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] | 6683 | else if (!strcmp(args[1], "dontlognull")) { |
| 6684 | /* don't log empty requests */ |
| 6685 | curproxy->options |= PR_O_NULLNOLOG; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6686 | } |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6687 | else if (!strcmp(args[1], "tcpka")) { |
| 6688 | /* enable TCP keep-alives on client and server sessions */ |
| 6689 | curproxy->options |= PR_O_TCP_CLI_KA | PR_O_TCP_SRV_KA; |
| 6690 | } |
| 6691 | else if (!strcmp(args[1], "clitcpka")) { |
| 6692 | /* enable TCP keep-alives on client sessions */ |
| 6693 | curproxy->options |= PR_O_TCP_CLI_KA; |
| 6694 | } |
| 6695 | else if (!strcmp(args[1], "srvtcpka")) { |
| 6696 | /* enable TCP keep-alives on server sessions */ |
| 6697 | curproxy->options |= PR_O_TCP_SRV_KA; |
| 6698 | } |
Willy TARREAU | 3481c46 | 2006-03-01 22:37:57 +0100 | [diff] [blame] | 6699 | else if (!strcmp(args[1], "allbackups")) { |
| 6700 | /* Use all backup servers simultaneously */ |
| 6701 | curproxy->options |= PR_O_USE_ALL_BK; |
| 6702 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 6703 | else if (!strcmp(args[1], "httpchk")) { |
| 6704 | /* use HTTP request to check servers' health */ |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6705 | if (curproxy->check_req != NULL) { |
| 6706 | free(curproxy->check_req); |
| 6707 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 6708 | curproxy->options |= PR_O_HTTP_CHK; |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6709 | if (!*args[2]) { /* no argument */ |
| 6710 | curproxy->check_req = strdup(DEF_CHECK_REQ); /* default request */ |
| 6711 | curproxy->check_len = strlen(DEF_CHECK_REQ); |
| 6712 | } else if (!*args[3]) { /* one argument : URI */ |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 6713 | int reqlen = strlen(args[2]) + strlen("OPTIONS / HTTP/1.0\r\n\r\n"); |
| 6714 | curproxy->check_req = (char *)malloc(reqlen); |
| 6715 | curproxy->check_len = snprintf(curproxy->check_req, reqlen, |
| 6716 | "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] | 6717 | } else { /* more arguments : METHOD URI [HTTP_VER] */ |
| 6718 | int reqlen = strlen(args[2]) + strlen(args[3]) + 3 + strlen("\r\n\r\n"); |
| 6719 | if (*args[4]) |
| 6720 | reqlen += strlen(args[4]); |
| 6721 | else |
| 6722 | reqlen += strlen("HTTP/1.0"); |
| 6723 | |
| 6724 | curproxy->check_req = (char *)malloc(reqlen); |
| 6725 | curproxy->check_len = snprintf(curproxy->check_req, reqlen, |
| 6726 | "%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] | 6727 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 6728 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6729 | else if (!strcmp(args[1], "persist")) { |
| 6730 | /* persist on using the server specified by the cookie, even when it's down */ |
| 6731 | curproxy->options |= PR_O_PERSIST; |
| 6732 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6733 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6734 | Alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6735 | return -1; |
| 6736 | } |
| 6737 | return 0; |
| 6738 | } |
| 6739 | else if (!strcmp(args[0], "redispatch") || !strcmp(args[0], "redisp")) { |
| 6740 | /* enable reconnections to dispatch */ |
| 6741 | curproxy->options |= PR_O_REDISP; |
| 6742 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 6743 | #ifdef TPROXY |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6744 | else if (!strcmp(args[0], "transparent")) { |
| 6745 | /* enable transparent proxy connections */ |
| 6746 | curproxy->options |= PR_O_TRANSP; |
| 6747 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6748 | #endif |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6749 | else if (!strcmp(args[0], "maxconn")) { /* maxconn */ |
| 6750 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6751 | 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] | 6752 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6753 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6754 | curproxy->maxconn = atol(args[1]); |
| 6755 | } |
| 6756 | else if (!strcmp(args[0], "grace")) { /* grace time (ms) */ |
| 6757 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6758 | 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] | 6759 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6760 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6761 | curproxy->grace = atol(args[1]); |
| 6762 | } |
| 6763 | else if (!strcmp(args[0], "dispatch")) { /* dispatch address */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6764 | if (curproxy == &defproxy) { |
| 6765 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6766 | return -1; |
| 6767 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6768 | if (strchr(args[1], ':') == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6769 | 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] | 6770 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6771 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6772 | curproxy->dispatch_addr = *str2sa(args[1]); |
| 6773 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6774 | else if (!strcmp(args[0], "balance")) { /* set balancing with optional algorithm */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6775 | if (*(args[1])) { |
| 6776 | if (!strcmp(args[1], "roundrobin")) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6777 | curproxy->options |= PR_O_BALANCE_RR; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6778 | } |
| 6779 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6780 | Alert("parsing [%s:%d] : '%s' only supports 'roundrobin' option.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6781 | return -1; |
| 6782 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6783 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6784 | else /* if no option is set, use round-robin by default */ |
| 6785 | curproxy->options |= PR_O_BALANCE_RR; |
| 6786 | } |
| 6787 | else if (!strcmp(args[0], "server")) { /* server address */ |
| 6788 | int cur_arg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6789 | char *rport; |
| 6790 | char *raddr; |
| 6791 | short realport; |
| 6792 | int do_check; |
| 6793 | |
| 6794 | if (curproxy == &defproxy) { |
| 6795 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6796 | return -1; |
| 6797 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6798 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6799 | if (!*args[2]) { |
| 6800 | 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] | 6801 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6802 | return -1; |
| 6803 | } |
| 6804 | if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) { |
| 6805 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 6806 | return -1; |
| 6807 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6808 | |
| 6809 | if (curproxy->srv == NULL) |
| 6810 | curproxy->srv = newsrv; |
| 6811 | else |
| 6812 | curproxy->cursrv->next = newsrv; |
| 6813 | curproxy->cursrv = newsrv; |
| 6814 | |
| 6815 | newsrv->next = NULL; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6816 | newsrv->proxy = curproxy; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6817 | |
| 6818 | do_check = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6819 | newsrv->state = SRV_RUNNING; /* early server setup */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6820 | newsrv->id = strdup(args[1]); |
| 6821 | |
| 6822 | /* several ways to check the port component : |
| 6823 | * - IP => port=+0, relative |
| 6824 | * - IP: => port=+0, relative |
| 6825 | * - IP:N => port=N, absolute |
| 6826 | * - IP:+N => port=+N, relative |
| 6827 | * - IP:-N => port=-N, relative |
| 6828 | */ |
| 6829 | raddr = strdup(args[2]); |
| 6830 | rport = strchr(raddr, ':'); |
| 6831 | if (rport) { |
| 6832 | *rport++ = 0; |
| 6833 | realport = atol(rport); |
| 6834 | if (!isdigit((int)*rport)) |
| 6835 | newsrv->state |= SRV_MAPPORTS; |
| 6836 | } else { |
| 6837 | realport = 0; |
| 6838 | newsrv->state |= SRV_MAPPORTS; |
| 6839 | } |
| 6840 | |
| 6841 | newsrv->addr = *str2sa(raddr); |
| 6842 | newsrv->addr.sin_port = htons(realport); |
| 6843 | free(raddr); |
| 6844 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6845 | newsrv->curfd = -1; /* no health-check in progress */ |
| 6846 | newsrv->inter = DEF_CHKINTR; |
| 6847 | newsrv->rise = DEF_RISETIME; |
| 6848 | newsrv->fall = DEF_FALLTIME; |
| 6849 | newsrv->health = newsrv->rise; /* up, but will fall down at first failure */ |
| 6850 | cur_arg = 3; |
| 6851 | while (*args[cur_arg]) { |
| 6852 | if (!strcmp(args[cur_arg], "cookie")) { |
| 6853 | newsrv->cookie = strdup(args[cur_arg + 1]); |
| 6854 | newsrv->cklen = strlen(args[cur_arg + 1]); |
| 6855 | cur_arg += 2; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6856 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6857 | else if (!strcmp(args[cur_arg], "rise")) { |
| 6858 | newsrv->rise = atol(args[cur_arg + 1]); |
| 6859 | newsrv->health = newsrv->rise; |
| 6860 | cur_arg += 2; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6861 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6862 | else if (!strcmp(args[cur_arg], "fall")) { |
| 6863 | newsrv->fall = atol(args[cur_arg + 1]); |
| 6864 | cur_arg += 2; |
| 6865 | } |
| 6866 | else if (!strcmp(args[cur_arg], "inter")) { |
| 6867 | newsrv->inter = atol(args[cur_arg + 1]); |
| 6868 | cur_arg += 2; |
| 6869 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6870 | else if (!strcmp(args[cur_arg], "port")) { |
| 6871 | newsrv->check_port = atol(args[cur_arg + 1]); |
| 6872 | cur_arg += 2; |
| 6873 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6874 | else if (!strcmp(args[cur_arg], "backup")) { |
| 6875 | newsrv->state |= SRV_BACKUP; |
| 6876 | cur_arg ++; |
| 6877 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6878 | else if (!strcmp(args[cur_arg], "check")) { |
Willy TARREAU | 203b0b6 | 2006-03-12 18:00:28 +0100 | [diff] [blame] | 6879 | global.maxsock++; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6880 | do_check = 1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6881 | cur_arg += 1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6882 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6883 | else if (!strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */ |
| 6884 | if (!*args[cur_arg + 1]) { |
| 6885 | Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n", |
| 6886 | file, linenum, "source"); |
| 6887 | return -1; |
| 6888 | } |
| 6889 | newsrv->state |= SRV_BIND_SRC; |
| 6890 | newsrv->source_addr = *str2sa(args[cur_arg + 1]); |
| 6891 | cur_arg += 2; |
| 6892 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6893 | else { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6894 | 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] | 6895 | file, linenum, newsrv->id); |
| 6896 | return -1; |
| 6897 | } |
| 6898 | } |
| 6899 | |
| 6900 | if (do_check) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6901 | if (!newsrv->check_port && !(newsrv->state & SRV_MAPPORTS)) |
| 6902 | newsrv->check_port = realport; /* by default */ |
| 6903 | if (!newsrv->check_port) { |
| 6904 | 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] | 6905 | file, linenum, newsrv->id); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6906 | return -1; |
| 6907 | } |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 6908 | newsrv->state |= SRV_CHECKED; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6909 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6910 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6911 | curproxy->nbservers++; |
| 6912 | } |
| 6913 | else if (!strcmp(args[0], "log")) { /* syslog server address */ |
| 6914 | struct sockaddr_in *sa; |
| 6915 | int facility; |
| 6916 | |
| 6917 | if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) { |
| 6918 | curproxy->logfac1 = global.logfac1; |
| 6919 | curproxy->logsrv1 = global.logsrv1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6920 | curproxy->loglev1 = global.loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6921 | curproxy->logfac2 = global.logfac2; |
| 6922 | curproxy->logsrv2 = global.logsrv2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6923 | curproxy->loglev2 = global.loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6924 | } |
| 6925 | else if (*(args[1]) && *(args[2])) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6926 | int level; |
| 6927 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6928 | for (facility = 0; facility < NB_LOG_FACILITIES; facility++) |
| 6929 | if (!strcmp(log_facilities[facility], args[2])) |
| 6930 | break; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6931 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6932 | if (facility >= NB_LOG_FACILITIES) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6933 | 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] | 6934 | exit(1); |
| 6935 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6936 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6937 | level = 7; /* max syslog level = debug */ |
| 6938 | if (*(args[3])) { |
| 6939 | while (level >= 0 && strcmp(log_levels[level], args[3])) |
| 6940 | level--; |
| 6941 | if (level < 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6942 | 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] | 6943 | exit(1); |
| 6944 | } |
| 6945 | } |
| 6946 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6947 | sa = str2sa(args[1]); |
| 6948 | if (!sa->sin_port) |
| 6949 | sa->sin_port = htons(SYSLOG_PORT); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6950 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6951 | if (curproxy->logfac1 == -1) { |
| 6952 | curproxy->logsrv1 = *sa; |
| 6953 | curproxy->logfac1 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6954 | curproxy->loglev1 = level; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6955 | } |
| 6956 | else if (curproxy->logfac2 == -1) { |
| 6957 | curproxy->logsrv2 = *sa; |
| 6958 | curproxy->logfac2 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6959 | curproxy->loglev2 = level; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6960 | } |
| 6961 | else { |
| 6962 | Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6963 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6964 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6965 | } |
| 6966 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6967 | 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] | 6968 | file, linenum); |
| 6969 | return -1; |
| 6970 | } |
| 6971 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 6972 | 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] | 6973 | if (!*args[1]) { |
| 6974 | Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6975 | file, linenum, "source"); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 6976 | return -1; |
| 6977 | } |
| 6978 | |
| 6979 | curproxy->source_addr = *str2sa(args[1]); |
| 6980 | curproxy->options |= PR_O_BIND_SRC; |
| 6981 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6982 | else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */ |
| 6983 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6984 | if (curproxy == &defproxy) { |
| 6985 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6986 | return -1; |
| 6987 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6988 | |
| 6989 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6990 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 6991 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6992 | return -1; |
| 6993 | } |
| 6994 | |
| 6995 | preg = calloc(1, sizeof(regex_t)); |
| 6996 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6997 | 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] | 6998 | return -1; |
| 6999 | } |
| 7000 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7001 | err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 7002 | if (err) { |
| 7003 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7004 | file, linenum, *err); |
| 7005 | return -1; |
| 7006 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7007 | } |
| 7008 | else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */ |
| 7009 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7010 | if (curproxy == &defproxy) { |
| 7011 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7012 | return -1; |
| 7013 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7014 | |
| 7015 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7016 | 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] | 7017 | return -1; |
| 7018 | } |
| 7019 | |
| 7020 | preg = calloc(1, sizeof(regex_t)); |
| 7021 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7022 | 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] | 7023 | return -1; |
| 7024 | } |
| 7025 | |
| 7026 | chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL); |
| 7027 | } |
| 7028 | else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */ |
| 7029 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7030 | if (curproxy == &defproxy) { |
| 7031 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7032 | return -1; |
| 7033 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7034 | |
| 7035 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7036 | 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] | 7037 | return -1; |
| 7038 | } |
| 7039 | |
| 7040 | preg = calloc(1, sizeof(regex_t)); |
| 7041 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7042 | 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] | 7043 | return -1; |
| 7044 | } |
| 7045 | |
| 7046 | chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL); |
| 7047 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7048 | else if (!strcmp(args[0], "reqpass")) { /* pass this header without allowing or denying the request */ |
| 7049 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7050 | if (curproxy == &defproxy) { |
| 7051 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7052 | return -1; |
| 7053 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7054 | |
| 7055 | if (*(args[1]) == 0) { |
| 7056 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
| 7057 | return -1; |
| 7058 | } |
| 7059 | |
| 7060 | preg = calloc(1, sizeof(regex_t)); |
| 7061 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 7062 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7063 | return -1; |
| 7064 | } |
| 7065 | |
| 7066 | chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL); |
| 7067 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7068 | else if (!strcmp(args[0], "reqallow")) { /* allow a request if a header matches this regex */ |
| 7069 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7070 | if (curproxy == &defproxy) { |
| 7071 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7072 | return -1; |
| 7073 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7074 | |
| 7075 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7076 | 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] | 7077 | return -1; |
| 7078 | } |
| 7079 | |
| 7080 | preg = calloc(1, sizeof(regex_t)); |
| 7081 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7082 | 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] | 7083 | return -1; |
| 7084 | } |
| 7085 | |
| 7086 | chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL); |
| 7087 | } |
| 7088 | else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */ |
| 7089 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7090 | if (curproxy == &defproxy) { |
| 7091 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7092 | return -1; |
| 7093 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7094 | |
| 7095 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7096 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 7097 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7098 | return -1; |
| 7099 | } |
| 7100 | |
| 7101 | preg = calloc(1, sizeof(regex_t)); |
| 7102 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7103 | 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] | 7104 | return -1; |
| 7105 | } |
| 7106 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7107 | err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 7108 | if (err) { |
| 7109 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7110 | file, linenum, *err); |
| 7111 | return -1; |
| 7112 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7113 | } |
| 7114 | else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */ |
| 7115 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7116 | if (curproxy == &defproxy) { |
| 7117 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7118 | return -1; |
| 7119 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7120 | |
| 7121 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7122 | 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] | 7123 | return -1; |
| 7124 | } |
| 7125 | |
| 7126 | preg = calloc(1, sizeof(regex_t)); |
| 7127 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7128 | 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] | 7129 | return -1; |
| 7130 | } |
| 7131 | |
| 7132 | chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL); |
| 7133 | } |
| 7134 | else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */ |
| 7135 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7136 | if (curproxy == &defproxy) { |
| 7137 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7138 | return -1; |
| 7139 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7140 | |
| 7141 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7142 | 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] | 7143 | return -1; |
| 7144 | } |
| 7145 | |
| 7146 | preg = calloc(1, sizeof(regex_t)); |
| 7147 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7148 | 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] | 7149 | return -1; |
| 7150 | } |
| 7151 | |
| 7152 | chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL); |
| 7153 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7154 | else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */ |
| 7155 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7156 | if (curproxy == &defproxy) { |
| 7157 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7158 | return -1; |
| 7159 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7160 | |
| 7161 | if (*(args[1]) == 0) { |
| 7162 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
| 7163 | return -1; |
| 7164 | } |
| 7165 | |
| 7166 | preg = calloc(1, sizeof(regex_t)); |
| 7167 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 7168 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7169 | return -1; |
| 7170 | } |
| 7171 | |
| 7172 | chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL); |
| 7173 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7174 | else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */ |
| 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) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7182 | 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] | 7183 | return -1; |
| 7184 | } |
| 7185 | |
| 7186 | preg = calloc(1, sizeof(regex_t)); |
| 7187 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7188 | 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] | 7189 | return -1; |
| 7190 | } |
| 7191 | |
| 7192 | chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL); |
| 7193 | } |
| 7194 | else if (!strcmp(args[0], "reqadd")) { /* add request header */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7195 | if (curproxy == &defproxy) { |
| 7196 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7197 | return -1; |
| 7198 | } |
| 7199 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7200 | if (curproxy->nb_reqadd >= MAX_NEWHDR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7201 | 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] | 7202 | return 0; |
| 7203 | } |
| 7204 | |
| 7205 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7206 | 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] | 7207 | return -1; |
| 7208 | } |
| 7209 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7210 | curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]); |
| 7211 | } |
| 7212 | else if (!strcmp(args[0], "srvexp") || !strcmp(args[0], "rsprep")) { /* replace response header from a regex */ |
| 7213 | regex_t *preg; |
| 7214 | |
| 7215 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 7216 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 7217 | file, linenum, args[0]); |
| 7218 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7219 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7220 | |
| 7221 | preg = calloc(1, sizeof(regex_t)); |
| 7222 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 7223 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7224 | return -1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7225 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7226 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7227 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 7228 | if (err) { |
| 7229 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7230 | file, linenum, *err); |
| 7231 | return -1; |
| 7232 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7233 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7234 | else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */ |
| 7235 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7236 | if (curproxy == &defproxy) { |
| 7237 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7238 | return -1; |
| 7239 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7240 | |
| 7241 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7242 | 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] | 7243 | return -1; |
| 7244 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7245 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7246 | preg = calloc(1, sizeof(regex_t)); |
| 7247 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7248 | 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] | 7249 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7250 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7251 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7252 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2])); |
| 7253 | if (err) { |
| 7254 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7255 | file, linenum, *err); |
| 7256 | return -1; |
| 7257 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7258 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7259 | else if (!strcmp(args[0], "rspdeny")) { /* block response header from a regex */ |
| 7260 | regex_t *preg; |
| 7261 | if (curproxy == &defproxy) { |
| 7262 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7263 | return -1; |
| 7264 | } |
| 7265 | |
| 7266 | if (*(args[1]) == 0) { |
| 7267 | Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]); |
| 7268 | return -1; |
| 7269 | } |
| 7270 | |
| 7271 | preg = calloc(1, sizeof(regex_t)); |
| 7272 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 7273 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7274 | return -1; |
| 7275 | } |
| 7276 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7277 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2])); |
| 7278 | if (err) { |
| 7279 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7280 | file, linenum, *err); |
| 7281 | return -1; |
| 7282 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7283 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7284 | 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] | 7285 | regex_t *preg; |
| 7286 | if (curproxy == &defproxy) { |
| 7287 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7288 | return -1; |
| 7289 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7290 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7291 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 7292 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 7293 | file, linenum, args[0]); |
| 7294 | return -1; |
| 7295 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7296 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7297 | preg = calloc(1, sizeof(regex_t)); |
| 7298 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 7299 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7300 | return -1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7301 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7302 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7303 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 7304 | if (err) { |
| 7305 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7306 | file, linenum, *err); |
| 7307 | return -1; |
| 7308 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7309 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7310 | else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */ |
| 7311 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7312 | if (curproxy == &defproxy) { |
| 7313 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7314 | return -1; |
| 7315 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7316 | |
| 7317 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7318 | 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] | 7319 | return -1; |
| 7320 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7321 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7322 | preg = calloc(1, sizeof(regex_t)); |
| 7323 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7324 | 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] | 7325 | return -1; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7326 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7327 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7328 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2])); |
| 7329 | if (err) { |
| 7330 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7331 | file, linenum, *err); |
| 7332 | return -1; |
| 7333 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7334 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7335 | else if (!strcmp(args[0], "rspideny")) { /* block response header from a regex ignoring case */ |
| 7336 | regex_t *preg; |
| 7337 | if (curproxy == &defproxy) { |
| 7338 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7339 | return -1; |
| 7340 | } |
| 7341 | |
| 7342 | if (*(args[1]) == 0) { |
| 7343 | Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]); |
| 7344 | return -1; |
| 7345 | } |
| 7346 | |
| 7347 | preg = calloc(1, sizeof(regex_t)); |
| 7348 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 7349 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7350 | return -1; |
| 7351 | } |
| 7352 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7353 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2])); |
| 7354 | if (err) { |
| 7355 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7356 | file, linenum, *err); |
| 7357 | return -1; |
| 7358 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7359 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7360 | else if (!strcmp(args[0], "rspadd")) { /* add response header */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7361 | if (curproxy == &defproxy) { |
| 7362 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7363 | return -1; |
| 7364 | } |
| 7365 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7366 | if (curproxy->nb_rspadd >= MAX_NEWHDR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7367 | 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] | 7368 | return 0; |
| 7369 | } |
| 7370 | |
| 7371 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7372 | 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] | 7373 | return -1; |
| 7374 | } |
| 7375 | |
| 7376 | curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]); |
| 7377 | } |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7378 | else if (!strcmp(args[0], "errorloc") || |
| 7379 | !strcmp(args[0], "errorloc302") || |
| 7380 | !strcmp(args[0], "errorloc303")) { /* error location */ |
| 7381 | int errnum, errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7382 | char *err; |
| 7383 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7384 | // if (curproxy == &defproxy) { |
| 7385 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7386 | // return -1; |
| 7387 | // } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7388 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7389 | if (*(args[2]) == 0) { |
| 7390 | Alert("parsing [%s:%d] : <errorloc> expects <error> and <url> as arguments.\n", file, linenum); |
| 7391 | return -1; |
| 7392 | } |
| 7393 | |
| 7394 | errnum = atol(args[1]); |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7395 | if (!strcmp(args[0], "errorloc303")) { |
| 7396 | err = malloc(strlen(HTTP_303) + strlen(args[2]) + 5); |
| 7397 | errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_303, args[2]); |
| 7398 | } else { |
| 7399 | err = malloc(strlen(HTTP_302) + strlen(args[2]) + 5); |
| 7400 | errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_302, args[2]); |
| 7401 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7402 | |
| 7403 | if (errnum == 400) { |
| 7404 | if (curproxy->errmsg.msg400) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7405 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7406 | free(curproxy->errmsg.msg400); |
| 7407 | } |
| 7408 | curproxy->errmsg.msg400 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7409 | curproxy->errmsg.len400 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7410 | } |
| 7411 | else if (errnum == 403) { |
| 7412 | if (curproxy->errmsg.msg403) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7413 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7414 | free(curproxy->errmsg.msg403); |
| 7415 | } |
| 7416 | curproxy->errmsg.msg403 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7417 | curproxy->errmsg.len403 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7418 | } |
| 7419 | else if (errnum == 408) { |
| 7420 | if (curproxy->errmsg.msg408) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7421 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7422 | free(curproxy->errmsg.msg408); |
| 7423 | } |
| 7424 | curproxy->errmsg.msg408 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7425 | curproxy->errmsg.len408 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7426 | } |
| 7427 | else if (errnum == 500) { |
| 7428 | if (curproxy->errmsg.msg500) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7429 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7430 | free(curproxy->errmsg.msg500); |
| 7431 | } |
| 7432 | curproxy->errmsg.msg500 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7433 | curproxy->errmsg.len500 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7434 | } |
| 7435 | else if (errnum == 502) { |
| 7436 | if (curproxy->errmsg.msg502) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7437 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7438 | free(curproxy->errmsg.msg502); |
| 7439 | } |
| 7440 | curproxy->errmsg.msg502 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7441 | curproxy->errmsg.len502 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7442 | } |
| 7443 | else if (errnum == 503) { |
| 7444 | if (curproxy->errmsg.msg503) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7445 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7446 | free(curproxy->errmsg.msg503); |
| 7447 | } |
| 7448 | curproxy->errmsg.msg503 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7449 | curproxy->errmsg.len503 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7450 | } |
| 7451 | else if (errnum == 504) { |
| 7452 | if (curproxy->errmsg.msg504) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7453 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7454 | free(curproxy->errmsg.msg504); |
| 7455 | } |
| 7456 | curproxy->errmsg.msg504 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7457 | curproxy->errmsg.len504 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7458 | } |
| 7459 | else { |
| 7460 | Warning("parsing [%s:%d] : error %d relocation will be ignored.\n", file, linenum, errnum); |
| 7461 | free(err); |
| 7462 | } |
| 7463 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7464 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7465 | 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] | 7466 | return -1; |
| 7467 | } |
| 7468 | return 0; |
| 7469 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7470 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7471 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7472 | /* |
| 7473 | * This function reads and parses the configuration file given in the argument. |
| 7474 | * returns 0 if OK, -1 if error. |
| 7475 | */ |
| 7476 | int readcfgfile(char *file) { |
| 7477 | char thisline[256]; |
| 7478 | char *line; |
| 7479 | FILE *f; |
| 7480 | int linenum = 0; |
| 7481 | char *end; |
| 7482 | char *args[MAX_LINE_ARGS]; |
| 7483 | int arg; |
| 7484 | int cfgerr = 0; |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 7485 | int nbchk, mininter; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7486 | int confsect = CFG_NONE; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7487 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7488 | struct proxy *curproxy = NULL; |
| 7489 | struct server *newsrv = NULL; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7490 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7491 | if ((f=fopen(file,"r")) == NULL) |
| 7492 | return -1; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7493 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7494 | init_default_instance(); |
| 7495 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7496 | while (fgets(line = thisline, sizeof(thisline), f) != NULL) { |
| 7497 | linenum++; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7498 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7499 | end = line + strlen(line); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7500 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7501 | /* skip leading spaces */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 7502 | while (isspace((int)*line)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7503 | line++; |
| 7504 | |
| 7505 | arg = 0; |
| 7506 | args[arg] = line; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7507 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7508 | while (*line && arg < MAX_LINE_ARGS) { |
| 7509 | /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their |
| 7510 | * C equivalent value. Other combinations left unchanged (eg: \1). |
| 7511 | */ |
| 7512 | if (*line == '\\') { |
| 7513 | int skip = 0; |
| 7514 | if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') { |
| 7515 | *line = line[1]; |
| 7516 | skip = 1; |
| 7517 | } |
| 7518 | else if (line[1] == 'r') { |
| 7519 | *line = '\r'; |
| 7520 | skip = 1; |
| 7521 | } |
| 7522 | else if (line[1] == 'n') { |
| 7523 | *line = '\n'; |
| 7524 | skip = 1; |
| 7525 | } |
| 7526 | else if (line[1] == 't') { |
| 7527 | *line = '\t'; |
| 7528 | skip = 1; |
| 7529 | } |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7530 | else if (line[1] == 'x') { |
| 7531 | if ((line + 3 < end ) && ishex(line[2]) && ishex(line[3])) { |
| 7532 | unsigned char hex1, hex2; |
| 7533 | hex1 = toupper(line[2]) - '0'; |
| 7534 | hex2 = toupper(line[3]) - '0'; |
| 7535 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 7536 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 7537 | *line = (hex1<<4) + hex2; |
| 7538 | skip = 3; |
| 7539 | } |
| 7540 | else { |
| 7541 | Alert("parsing [%s:%d] : invalid or incomplete '\\x' sequence in '%s'.\n", file, linenum, args[0]); |
| 7542 | return -1; |
| 7543 | } |
| 7544 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7545 | if (skip) { |
| 7546 | memmove(line + 1, line + 1 + skip, end - (line + skip + 1)); |
| 7547 | end -= skip; |
| 7548 | } |
| 7549 | line++; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7550 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7551 | else if (*line == '#' || *line == '\n' || *line == '\r') { |
| 7552 | /* end of string, end of loop */ |
| 7553 | *line = 0; |
| 7554 | break; |
| 7555 | } |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 7556 | else if (isspace((int)*line)) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7557 | /* a non-escaped space is an argument separator */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7558 | *line++ = 0; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 7559 | while (isspace((int)*line)) |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7560 | line++; |
| 7561 | args[++arg] = line; |
| 7562 | } |
| 7563 | else { |
| 7564 | line++; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7565 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7566 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7567 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7568 | /* empty line */ |
| 7569 | if (!**args) |
| 7570 | continue; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7571 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7572 | /* zero out remaining args */ |
| 7573 | while (++arg < MAX_LINE_ARGS) { |
| 7574 | args[arg] = line; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7575 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7576 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7577 | if (!strcmp(args[0], "listen") || !strcmp(args[0], "defaults")) /* new proxy */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7578 | confsect = CFG_LISTEN; |
| 7579 | else if (!strcmp(args[0], "global")) /* global config */ |
| 7580 | confsect = CFG_GLOBAL; |
| 7581 | /* else it's a section keyword */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7582 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7583 | switch (confsect) { |
| 7584 | case CFG_LISTEN: |
| 7585 | if (cfg_parse_listen(file, linenum, args) < 0) |
| 7586 | return -1; |
| 7587 | break; |
| 7588 | case CFG_GLOBAL: |
| 7589 | if (cfg_parse_global(file, linenum, args) < 0) |
| 7590 | return -1; |
| 7591 | break; |
| 7592 | default: |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7593 | 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] | 7594 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7595 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7596 | |
| 7597 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7598 | } |
| 7599 | fclose(f); |
| 7600 | |
| 7601 | /* |
| 7602 | * Now, check for the integrity of all that we have collected. |
| 7603 | */ |
| 7604 | |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 7605 | /* will be needed further to delay some tasks */ |
| 7606 | tv_now(&now); |
| 7607 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7608 | if ((curproxy = proxy) == NULL) { |
| 7609 | Alert("parsing %s : no <listen> line. Nothing to do !\n", |
| 7610 | file); |
| 7611 | return -1; |
| 7612 | } |
| 7613 | |
| 7614 | while (curproxy != NULL) { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 7615 | curproxy->cursrv = NULL; |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7616 | if (curproxy->state == PR_STSTOPPED) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 7617 | curproxy = curproxy->next; |
| 7618 | continue; |
| 7619 | } |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 7620 | |
| 7621 | if (curproxy->listen == NULL) { |
| 7622 | 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); |
| 7623 | cfgerr++; |
| 7624 | } |
| 7625 | else if ((curproxy->mode != PR_MODE_HEALTH) && |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7626 | !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) && |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7627 | (*(int *)&curproxy->dispatch_addr.sin_addr == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7628 | Alert("parsing %s : listener %s has no dispatch address and is not in transparent or balance mode.\n", |
| 7629 | file, curproxy->id); |
| 7630 | cfgerr++; |
| 7631 | } |
| 7632 | else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) { |
| 7633 | if (curproxy->options & PR_O_TRANSP) { |
| 7634 | Alert("parsing %s : listener %s cannot use both transparent and balance mode.\n", |
| 7635 | file, curproxy->id); |
| 7636 | cfgerr++; |
| 7637 | } |
| 7638 | else if (curproxy->srv == NULL) { |
| 7639 | Alert("parsing %s : listener %s needs at least 1 server in balance mode.\n", |
| 7640 | file, curproxy->id); |
| 7641 | cfgerr++; |
| 7642 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7643 | else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7644 | Warning("parsing %s : dispatch address of listener %s will be ignored in balance mode.\n", |
| 7645 | file, curproxy->id); |
| 7646 | } |
| 7647 | } |
| 7648 | 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] | 7649 | if (curproxy->cookie_name != NULL) { |
| 7650 | Warning("parsing %s : cookie will be ignored for listener %s.\n", |
| 7651 | file, curproxy->id); |
| 7652 | } |
| 7653 | if ((newsrv = curproxy->srv) != NULL) { |
| 7654 | Warning("parsing %s : servers will be ignored for listener %s.\n", |
| 7655 | file, curproxy->id); |
| 7656 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7657 | if (curproxy->rsp_exp != NULL) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7658 | Warning("parsing %s : server regular expressions will be ignored for listener %s.\n", |
| 7659 | file, curproxy->id); |
| 7660 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7661 | if (curproxy->req_exp != NULL) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7662 | Warning("parsing %s : client regular expressions will be ignored for listener %s.\n", |
| 7663 | file, curproxy->id); |
| 7664 | } |
| 7665 | } |
| 7666 | else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */ |
| 7667 | if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) { |
| 7668 | Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n", |
| 7669 | file, curproxy->id); |
| 7670 | cfgerr++; |
| 7671 | } |
| 7672 | else { |
| 7673 | while (newsrv != NULL) { |
| 7674 | /* nothing to check for now */ |
| 7675 | newsrv = newsrv->next; |
| 7676 | } |
| 7677 | } |
| 7678 | } |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 7679 | |
| 7680 | if (curproxy->options & PR_O_LOGASAP) |
| 7681 | curproxy->to_log &= ~LW_BYTES; |
| 7682 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7683 | if (curproxy->errmsg.msg400 == NULL) { |
| 7684 | curproxy->errmsg.msg400 = (char *)HTTP_400; |
| 7685 | curproxy->errmsg.len400 = strlen(HTTP_400); |
| 7686 | } |
| 7687 | if (curproxy->errmsg.msg403 == NULL) { |
| 7688 | curproxy->errmsg.msg403 = (char *)HTTP_403; |
| 7689 | curproxy->errmsg.len403 = strlen(HTTP_403); |
| 7690 | } |
| 7691 | if (curproxy->errmsg.msg408 == NULL) { |
| 7692 | curproxy->errmsg.msg408 = (char *)HTTP_408; |
| 7693 | curproxy->errmsg.len408 = strlen(HTTP_408); |
| 7694 | } |
| 7695 | if (curproxy->errmsg.msg500 == NULL) { |
| 7696 | curproxy->errmsg.msg500 = (char *)HTTP_500; |
| 7697 | curproxy->errmsg.len500 = strlen(HTTP_500); |
| 7698 | } |
| 7699 | if (curproxy->errmsg.msg502 == NULL) { |
| 7700 | curproxy->errmsg.msg502 = (char *)HTTP_502; |
| 7701 | curproxy->errmsg.len502 = strlen(HTTP_502); |
| 7702 | } |
| 7703 | if (curproxy->errmsg.msg503 == NULL) { |
| 7704 | curproxy->errmsg.msg503 = (char *)HTTP_503; |
| 7705 | curproxy->errmsg.len503 = strlen(HTTP_503); |
| 7706 | } |
| 7707 | if (curproxy->errmsg.msg504 == NULL) { |
| 7708 | curproxy->errmsg.msg504 = (char *)HTTP_504; |
| 7709 | curproxy->errmsg.len504 = strlen(HTTP_504); |
| 7710 | } |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame] | 7711 | |
| 7712 | /* now we'll start this proxy's health checks if any */ |
| 7713 | /* 1- count the checkers to run simultaneously */ |
| 7714 | nbchk = 0; |
| 7715 | mininter = 0; |
| 7716 | newsrv = curproxy->srv; |
| 7717 | while (newsrv != NULL) { |
| 7718 | if (newsrv->state & SRV_CHECKED) { |
| 7719 | if (!mininter || mininter > newsrv->inter) |
| 7720 | mininter = newsrv->inter; |
| 7721 | nbchk++; |
| 7722 | } |
| 7723 | newsrv = newsrv->next; |
| 7724 | } |
| 7725 | |
| 7726 | /* 2- start them as far as possible from each others while respecting |
| 7727 | * their own intervals. For this, we will start them after their own |
| 7728 | * interval added to the min interval divided by the number of servers, |
| 7729 | * weighted by the server's position in the list. |
| 7730 | */ |
| 7731 | if (nbchk > 0) { |
| 7732 | struct task *t; |
| 7733 | int srvpos; |
| 7734 | |
| 7735 | newsrv = curproxy->srv; |
| 7736 | srvpos = 0; |
| 7737 | while (newsrv != NULL) { |
| 7738 | /* should this server be checked ? */ |
| 7739 | if (newsrv->state & SRV_CHECKED) { |
| 7740 | if ((t = pool_alloc(task)) == NULL) { |
| 7741 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 7742 | return -1; |
| 7743 | } |
| 7744 | |
| 7745 | t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */ |
| 7746 | t->wq = LIST_HEAD(wait_queue); /* but already has a wait queue assigned */ |
| 7747 | t->state = TASK_IDLE; |
| 7748 | t->process = process_chk; |
| 7749 | t->context = newsrv; |
| 7750 | |
| 7751 | /* check this every ms */ |
| 7752 | tv_delayfrom(&t->expire, &now, |
| 7753 | newsrv->inter + mininter * srvpos / nbchk); |
| 7754 | task_queue(t); |
| 7755 | //task_wakeup(&rq, t); |
| 7756 | srvpos++; |
| 7757 | } |
| 7758 | newsrv = newsrv->next; |
| 7759 | } |
| 7760 | } |
| 7761 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7762 | curproxy = curproxy->next; |
| 7763 | } |
| 7764 | if (cfgerr > 0) { |
| 7765 | Alert("Errors found in configuration file, aborting.\n"); |
| 7766 | return -1; |
| 7767 | } |
| 7768 | else |
| 7769 | return 0; |
| 7770 | } |
| 7771 | |
| 7772 | |
| 7773 | /* |
| 7774 | * This function initializes all the necessary variables. It only returns |
| 7775 | * if everything is OK. If something fails, it exits. |
| 7776 | */ |
| 7777 | void init(int argc, char **argv) { |
| 7778 | int i; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7779 | int arg_mode = 0; /* MODE_DEBUG, ... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7780 | char *old_argv = *argv; |
| 7781 | char *tmp; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 7782 | char *cfg_pidfile = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7783 | |
| 7784 | if (1<<INTBITS != sizeof(int)*8) { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 7785 | fprintf(stderr, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7786 | "Error: wrong architecture. Recompile so that sizeof(int)=%d\n", |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7787 | (int)(sizeof(int)*8)); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7788 | exit(1); |
| 7789 | } |
| 7790 | |
Willy TARREAU | a9e75f6 | 2006-03-01 22:27:48 +0100 | [diff] [blame] | 7791 | /* initialize the libc's localtime structures once for all so that we |
| 7792 | * won't be missing memory if we want to send alerts under OOM conditions. |
| 7793 | */ |
| 7794 | tv_now(&now); |
| 7795 | localtime(&now.tv_sec); |
| 7796 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7797 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 7798 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 7799 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 7800 | * printable characters above. |
| 7801 | */ |
| 7802 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 7803 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 7804 | for (i = 0; i < 32; i++) { |
| 7805 | FD_SET(i, hdr_encode_map); |
| 7806 | FD_SET(i, url_encode_map); |
| 7807 | } |
| 7808 | for (i = 127; i < 256; i++) { |
| 7809 | FD_SET(i, hdr_encode_map); |
| 7810 | FD_SET(i, url_encode_map); |
| 7811 | } |
| 7812 | |
| 7813 | tmp = "\"#{|}"; |
| 7814 | while (*tmp) { |
| 7815 | FD_SET(*tmp, hdr_encode_map); |
| 7816 | tmp++; |
| 7817 | } |
| 7818 | |
| 7819 | tmp = "\"#"; |
| 7820 | while (*tmp) { |
| 7821 | FD_SET(*tmp, url_encode_map); |
| 7822 | tmp++; |
| 7823 | } |
| 7824 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 7825 | cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */ |
| 7826 | #if defined(ENABLE_POLL) |
| 7827 | cfg_polling_mechanism |= POLL_USE_POLL; |
| 7828 | #endif |
| 7829 | #if defined(ENABLE_EPOLL) |
| 7830 | cfg_polling_mechanism |= POLL_USE_EPOLL; |
| 7831 | #endif |
| 7832 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7833 | pid = getpid(); |
| 7834 | progname = *argv; |
| 7835 | while ((tmp = strchr(progname, '/')) != NULL) |
| 7836 | progname = tmp + 1; |
| 7837 | |
| 7838 | argc--; argv++; |
| 7839 | while (argc > 0) { |
| 7840 | char *flag; |
| 7841 | |
| 7842 | if (**argv == '-') { |
| 7843 | flag = *argv+1; |
| 7844 | |
| 7845 | /* 1 arg */ |
| 7846 | if (*flag == 'v') { |
| 7847 | display_version(); |
| 7848 | exit(0); |
| 7849 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7850 | #if defined(ENABLE_EPOLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 7851 | else if (*flag == 'd' && flag[1] == 'e') |
| 7852 | cfg_polling_mechanism &= ~POLL_USE_EPOLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7853 | #endif |
| 7854 | #if defined(ENABLE_POLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 7855 | else if (*flag == 'd' && flag[1] == 'p') |
| 7856 | cfg_polling_mechanism &= ~POLL_USE_POLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7857 | #endif |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7858 | else if (*flag == 'V') |
| 7859 | arg_mode |= MODE_VERBOSE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7860 | else if (*flag == 'd') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7861 | arg_mode |= MODE_DEBUG; |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 7862 | else if (*flag == 'c') |
| 7863 | arg_mode |= MODE_CHECK; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7864 | else if (*flag == 'D') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7865 | arg_mode |= MODE_DAEMON | MODE_QUIET; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7866 | else if (*flag == 'q') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7867 | arg_mode |= MODE_QUIET; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7868 | #if STATTIME > 0 |
| 7869 | else if (*flag == 's') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7870 | arg_mode |= MODE_STATS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7871 | else if (*flag == 'l') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7872 | arg_mode |= MODE_LOG; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7873 | #endif |
| 7874 | else { /* >=2 args */ |
| 7875 | argv++; argc--; |
| 7876 | if (argc == 0) |
| 7877 | usage(old_argv); |
| 7878 | |
| 7879 | switch (*flag) { |
| 7880 | case 'n' : cfg_maxconn = atol(*argv); break; |
| 7881 | case 'N' : cfg_maxpconn = atol(*argv); break; |
| 7882 | case 'f' : cfg_cfgfile = *argv; break; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 7883 | case 'p' : cfg_pidfile = *argv; break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7884 | default: usage(old_argv); |
| 7885 | } |
| 7886 | } |
| 7887 | } |
| 7888 | else |
| 7889 | usage(old_argv); |
| 7890 | argv++; argc--; |
| 7891 | } |
| 7892 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 7893 | global.mode = MODE_STARTING | /* during startup, we want most of the alerts */ |
| 7894 | (arg_mode & (MODE_DAEMON | MODE_VERBOSE | MODE_QUIET | MODE_CHECK | MODE_DEBUG)); |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 7895 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7896 | if (!cfg_cfgfile) |
| 7897 | usage(old_argv); |
| 7898 | |
| 7899 | gethostname(hostname, MAX_HOSTNAME_LEN); |
| 7900 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7901 | have_appsession = 0; |
Willy TARREAU | 203b0b6 | 2006-03-12 18:00:28 +0100 | [diff] [blame] | 7902 | global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7903 | if (readcfgfile(cfg_cfgfile) < 0) { |
| 7904 | Alert("Error reading configuration file : %s\n", cfg_cfgfile); |
| 7905 | exit(1); |
| 7906 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7907 | if (have_appsession) |
| 7908 | appsession_init(); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7909 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7910 | if (global.mode & MODE_CHECK) { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 7911 | qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile); |
| 7912 | exit(0); |
| 7913 | } |
| 7914 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7915 | if (cfg_maxconn > 0) |
| 7916 | global.maxconn = cfg_maxconn; |
| 7917 | |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 7918 | if (cfg_pidfile) { |
| 7919 | if (global.pidfile) |
| 7920 | free(global.pidfile); |
| 7921 | global.pidfile = strdup(cfg_pidfile); |
| 7922 | } |
| 7923 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7924 | if (global.maxconn == 0) |
| 7925 | global.maxconn = DEFAULT_MAXCONN; |
| 7926 | |
Willy TARREAU | 203b0b6 | 2006-03-12 18:00:28 +0100 | [diff] [blame] | 7927 | global.maxsock += global.maxconn * 2; /* each connection needs two sockets */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7928 | |
| 7929 | if (arg_mode & MODE_DEBUG) { |
| 7930 | /* command line debug mode inhibits configuration mode */ |
| 7931 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 7932 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7933 | global.mode |= (arg_mode & (MODE_DAEMON | MODE_QUIET | MODE_VERBOSE |
| 7934 | | MODE_DEBUG | MODE_STATS | MODE_LOG)); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7935 | |
| 7936 | if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) { |
| 7937 | Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n"); |
| 7938 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 7939 | } |
| 7940 | |
| 7941 | if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) { |
| 7942 | Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n"); |
| 7943 | global.nbproc = 1; |
| 7944 | } |
| 7945 | |
| 7946 | if (global.nbproc < 1) |
| 7947 | global.nbproc = 1; |
| 7948 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7949 | StaticReadEvent = (fd_set *)calloc(1, |
| 7950 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7951 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7952 | StaticWriteEvent = (fd_set *)calloc(1, |
| 7953 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7954 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7955 | |
| 7956 | fdtab = (struct fdtab *)calloc(1, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7957 | sizeof(struct fdtab) * (global.maxsock)); |
| 7958 | for (i = 0; i < global.maxsock; i++) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7959 | fdtab[i].state = FD_STCLOSE; |
| 7960 | } |
| 7961 | } |
| 7962 | |
| 7963 | /* |
| 7964 | * this function starts all the proxies. It returns 0 if OK, -1 if not. |
| 7965 | */ |
| 7966 | int start_proxies() { |
| 7967 | struct proxy *curproxy; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7968 | struct listener *listener; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7969 | int fd; |
| 7970 | |
| 7971 | for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) { |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7972 | if (curproxy->state == PR_STSTOPPED) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7973 | continue; |
| 7974 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7975 | for (listener = curproxy->listen; listener != NULL; listener = listener->next) { |
| 7976 | if ((fd = listener->fd = |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 7977 | socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7978 | Alert("cannot create listening socket for proxy %s. Aborting.\n", |
| 7979 | curproxy->id); |
| 7980 | return -1; |
| 7981 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7982 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7983 | if (fd >= global.maxsock) { |
| 7984 | Alert("socket(): not enough free sockets for proxy %s. Raise -n argument. Aborting.\n", |
| 7985 | curproxy->id); |
| 7986 | close(fd); |
| 7987 | return -1; |
| 7988 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7989 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7990 | if ((fcntl(fd, F_SETFL, O_NONBLOCK) == -1) || |
| 7991 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 7992 | (char *) &one, sizeof(one)) == -1)) { |
| 7993 | Alert("cannot make socket non-blocking for proxy %s. Aborting.\n", |
| 7994 | curproxy->id); |
| 7995 | close(fd); |
| 7996 | return -1; |
| 7997 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7998 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7999 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) { |
| 8000 | Alert("cannot do so_reuseaddr for proxy %s. Continuing.\n", |
| 8001 | curproxy->id); |
| 8002 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8003 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8004 | if (bind(fd, |
| 8005 | (struct sockaddr *)&listener->addr, |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 8006 | listener->addr.ss_family == AF_INET6 ? |
| 8007 | sizeof(struct sockaddr_in6) : |
| 8008 | sizeof(struct sockaddr_in)) == -1) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8009 | Alert("cannot bind socket for proxy %s. Aborting.\n", |
| 8010 | curproxy->id); |
| 8011 | close(fd); |
| 8012 | return -1; |
| 8013 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8014 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8015 | if (listen(fd, curproxy->maxconn) == -1) { |
| 8016 | Alert("cannot listen to socket for proxy %s. Aborting.\n", |
| 8017 | curproxy->id); |
| 8018 | close(fd); |
| 8019 | return -1; |
| 8020 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8021 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 8022 | /* the function for the accept() event */ |
| 8023 | fdtab[fd].read = &event_accept; |
| 8024 | fdtab[fd].write = NULL; /* never called */ |
| 8025 | fdtab[fd].owner = (struct task *)curproxy; /* reference the proxy instead of a task */ |
| 8026 | curproxy->state = PR_STRUN; |
| 8027 | fdtab[fd].state = FD_STLISTEN; |
| 8028 | FD_SET(fd, StaticReadEvent); |
| 8029 | fd_insert(fd); |
| 8030 | listeners++; |
| 8031 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 8032 | send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8033 | } |
| 8034 | return 0; |
| 8035 | } |
| 8036 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 8037 | int match_str(const void *key1, const void *key2) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8038 | |
| 8039 | appsess *temp1,*temp2; |
| 8040 | temp1 = (appsess *)key1; |
| 8041 | temp2 = (appsess *)key2; |
| 8042 | |
| 8043 | //fprintf(stdout,">>>>>>>>>>>>>>temp1->sessid :%s:\n",temp1->sessid); |
| 8044 | //fprintf(stdout,">>>>>>>>>>>>>>temp2->sessid :%s:\n",temp2->sessid); |
| 8045 | |
| 8046 | return (strcmp(temp1->sessid,temp2->sessid) == 0); |
| 8047 | }/* end match_str */ |
| 8048 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 8049 | void destroy(void *data) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8050 | appsess *temp1; |
| 8051 | |
| 8052 | //printf("destroy called\n"); |
| 8053 | temp1 = (appsess *)data; |
| 8054 | |
| 8055 | if (temp1->sessid) |
| 8056 | pool_free_to(apools.sessid, temp1->sessid); |
| 8057 | |
| 8058 | if (temp1->serverid) |
| 8059 | pool_free_to(apools.serverid, temp1->serverid); |
| 8060 | |
| 8061 | pool_free(appsess, temp1); |
| 8062 | } /* end destroy */ |
| 8063 | |
| 8064 | void appsession_cleanup( void ) |
| 8065 | { |
| 8066 | struct proxy *p = proxy; |
| 8067 | |
| 8068 | while(p) { |
| 8069 | chtbl_destroy(&(p->htbl_proxy)); |
| 8070 | p = p->next; |
| 8071 | } |
| 8072 | }/* end appsession_cleanup() */ |
| 8073 | |
| 8074 | void pool_destroy(void **pool) |
| 8075 | { |
| 8076 | void *temp, *next; |
| 8077 | next = pool; |
| 8078 | while (next) { |
| 8079 | temp = next; |
| 8080 | next = *(void **)temp; |
| 8081 | free(temp); |
| 8082 | } |
| 8083 | }/* end pool_destroy() */ |
| 8084 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 8085 | void deinit(void) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8086 | struct proxy *p = proxy; |
| 8087 | struct cap_hdr *h,*h_next; |
| 8088 | struct server *s,*s_next; |
| 8089 | struct listener *l,*l_next; |
| 8090 | |
| 8091 | while (p) { |
| 8092 | if (p->id) |
| 8093 | free(p->id); |
| 8094 | |
| 8095 | if (p->check_req) |
| 8096 | free(p->check_req); |
| 8097 | |
| 8098 | if (p->cookie_name) |
| 8099 | free(p->cookie_name); |
| 8100 | |
| 8101 | if (p->capture_name) |
| 8102 | free(p->capture_name); |
| 8103 | |
| 8104 | /* only strup if the user have set in config. |
| 8105 | When should we free it?! |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 8106 | if (p->errmsg.msg400) free(p->errmsg.msg400); |
| 8107 | if (p->errmsg.msg403) free(p->errmsg.msg403); |
| 8108 | if (p->errmsg.msg408) free(p->errmsg.msg408); |
| 8109 | if (p->errmsg.msg500) free(p->errmsg.msg500); |
| 8110 | if (p->errmsg.msg502) free(p->errmsg.msg502); |
| 8111 | if (p->errmsg.msg503) free(p->errmsg.msg503); |
| 8112 | if (p->errmsg.msg504) free(p->errmsg.msg504); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8113 | */ |
| 8114 | if (p->appsession_name) |
| 8115 | free(p->appsession_name); |
| 8116 | |
| 8117 | h = p->req_cap; |
| 8118 | while (h) { |
| 8119 | h_next = h->next; |
| 8120 | if (h->name) |
| 8121 | free(h->name); |
| 8122 | pool_destroy(h->pool); |
| 8123 | free(h); |
| 8124 | h = h_next; |
| 8125 | }/* end while(h) */ |
| 8126 | |
| 8127 | h = p->rsp_cap; |
| 8128 | while (h) { |
| 8129 | h_next = h->next; |
| 8130 | if (h->name) |
| 8131 | free(h->name); |
| 8132 | |
| 8133 | pool_destroy(h->pool); |
| 8134 | free(h); |
| 8135 | h = h_next; |
| 8136 | }/* end while(h) */ |
| 8137 | |
| 8138 | s = p->srv; |
| 8139 | while (s) { |
| 8140 | s_next = s->next; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 8141 | if (s->id) |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8142 | free(s->id); |
| 8143 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 8144 | if (s->cookie) |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8145 | free(s->cookie); |
| 8146 | |
| 8147 | free(s); |
| 8148 | s = s_next; |
| 8149 | }/* end while(s) */ |
| 8150 | |
| 8151 | l = p->listen; |
| 8152 | while (l) { |
| 8153 | l_next = l->next; |
| 8154 | free(l); |
| 8155 | l = l_next; |
| 8156 | }/* end while(l) */ |
| 8157 | |
| 8158 | pool_destroy((void **) p->req_cap_pool); |
| 8159 | pool_destroy((void **) p->rsp_cap_pool); |
| 8160 | p = p->next; |
| 8161 | }/* end while(p) */ |
| 8162 | |
| 8163 | if (global.chroot) free(global.chroot); |
| 8164 | if (global.pidfile) free(global.pidfile); |
| 8165 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8166 | if (StaticReadEvent) free(StaticReadEvent); |
| 8167 | if (StaticWriteEvent) free(StaticWriteEvent); |
| 8168 | if (fdtab) free(fdtab); |
| 8169 | |
| 8170 | pool_destroy(pool_session); |
| 8171 | pool_destroy(pool_buffer); |
| 8172 | pool_destroy(pool_fdtab); |
| 8173 | pool_destroy(pool_requri); |
| 8174 | pool_destroy(pool_task); |
| 8175 | pool_destroy(pool_capture); |
| 8176 | pool_destroy(pool_appsess); |
| 8177 | |
| 8178 | if (have_appsession) { |
| 8179 | pool_destroy(apools.serverid); |
| 8180 | pool_destroy(apools.sessid); |
| 8181 | } |
| 8182 | } /* end deinit() */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8183 | |
| 8184 | int main(int argc, char **argv) { |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 8185 | struct rlimit limit; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8186 | FILE *pidfile = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8187 | init(argc, argv); |
| 8188 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8189 | signal(SIGQUIT, dump); |
| 8190 | signal(SIGUSR1, sig_soft_stop); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 8191 | signal(SIGHUP, sig_dump_state); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8192 | #ifdef DEBUG_MEMORY |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8193 | signal(SIGINT, sig_int); |
| 8194 | signal(SIGTERM, sig_term); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8195 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8196 | |
| 8197 | /* on very high loads, a sigpipe sometimes happen just between the |
| 8198 | * getsockopt() which tells "it's OK to write", and the following write :-( |
| 8199 | */ |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 8200 | #ifndef MSG_NOSIGNAL |
| 8201 | signal(SIGPIPE, SIG_IGN); |
| 8202 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8203 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8204 | /* start_proxies() sends an alert when it fails. */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8205 | if (start_proxies() < 0) |
| 8206 | exit(1); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8207 | |
| 8208 | if (listeners == 0) { |
| 8209 | Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]); |
| 8210 | exit(1); |
| 8211 | } |
| 8212 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 8213 | /* prepare pause/play signals */ |
| 8214 | signal(SIGTTOU, sig_pause); |
| 8215 | signal(SIGTTIN, sig_listen); |
| 8216 | |
Willy TARREAU | e3283d1 | 2006-03-01 22:15:29 +0100 | [diff] [blame] | 8217 | if (global.mode & MODE_DAEMON) { |
| 8218 | global.mode &= ~MODE_VERBOSE; |
| 8219 | global.mode |= MODE_QUIET; |
| 8220 | } |
| 8221 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8222 | /* MODE_QUIET can inhibit alerts and warnings below this line */ |
| 8223 | |
| 8224 | global.mode &= ~MODE_STARTING; |
Willy TARREAU | e3283d1 | 2006-03-01 22:15:29 +0100 | [diff] [blame] | 8225 | if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) { |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8226 | /* detach from the tty */ |
| 8227 | fclose(stdin); fclose(stdout); fclose(stderr); |
| 8228 | close(0); close(1); close(2); |
| 8229 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8230 | |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8231 | /* open log & pid files before the chroot */ |
| 8232 | if (global.mode & MODE_DAEMON && global.pidfile != NULL) { |
| 8233 | int pidfd; |
| 8234 | unlink(global.pidfile); |
| 8235 | pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644); |
| 8236 | if (pidfd < 0) { |
| 8237 | Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile); |
| 8238 | exit(1); |
| 8239 | } |
| 8240 | pidfile = fdopen(pidfd, "w"); |
| 8241 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8242 | |
| 8243 | /* chroot if needed */ |
| 8244 | if (global.chroot != NULL) { |
| 8245 | if (chroot(global.chroot) == -1) { |
| 8246 | Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot); |
| 8247 | exit(1); |
| 8248 | } |
| 8249 | chdir("/"); |
| 8250 | } |
| 8251 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 8252 | /* ulimits */ |
Willy TARREAU | dd67617 | 2006-03-12 18:01:33 +0100 | [diff] [blame] | 8253 | if (!global.rlimit_nofile) |
| 8254 | global.rlimit_nofile = global.maxsock; |
| 8255 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 8256 | if (global.rlimit_nofile) { |
| 8257 | limit.rlim_cur = limit.rlim_max = global.rlimit_nofile; |
| 8258 | if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { |
| 8259 | Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile); |
| 8260 | } |
| 8261 | } |
| 8262 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8263 | /* setgid / setuid */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8264 | if (global.gid && setgid(global.gid) == -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8265 | Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid); |
| 8266 | exit(1); |
| 8267 | } |
| 8268 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8269 | if (global.uid && setuid(global.uid) == -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8270 | Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid); |
| 8271 | exit(1); |
| 8272 | } |
| 8273 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 8274 | /* check ulimits */ |
| 8275 | limit.rlim_cur = limit.rlim_max = 0; |
| 8276 | getrlimit(RLIMIT_NOFILE, &limit); |
| 8277 | if (limit.rlim_cur < global.maxsock) { |
| 8278 | 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", |
| 8279 | argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock); |
| 8280 | } |
| 8281 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8282 | if (global.mode & MODE_DAEMON) { |
| 8283 | int ret = 0; |
| 8284 | int proc; |
| 8285 | |
| 8286 | /* the father launches the required number of processes */ |
| 8287 | for (proc = 0; proc < global.nbproc; proc++) { |
| 8288 | ret = fork(); |
| 8289 | if (ret < 0) { |
| 8290 | Alert("[%s.main()] Cannot fork.\n", argv[0]); |
| 8291 | exit(1); /* there has been an error */ |
| 8292 | } |
| 8293 | else if (ret == 0) /* child breaks here */ |
| 8294 | break; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8295 | if (pidfile != NULL) { |
| 8296 | fprintf(pidfile, "%d\n", ret); |
| 8297 | fflush(pidfile); |
| 8298 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8299 | } |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8300 | /* close the pidfile both in children and father */ |
| 8301 | if (pidfile != NULL) |
| 8302 | fclose(pidfile); |
| 8303 | free(global.pidfile); |
| 8304 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8305 | if (proc == global.nbproc) |
| 8306 | exit(0); /* parent must leave */ |
| 8307 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 8308 | /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure |
| 8309 | * that we can detach from the TTY. We MUST NOT do it in other cases since |
| 8310 | * it would have already be done, and 0-2 would have been affected to listening |
| 8311 | * sockets |
| 8312 | */ |
| 8313 | if (!(global.mode & MODE_QUIET)) { |
| 8314 | /* detach from the tty */ |
| 8315 | fclose(stdin); fclose(stdout); fclose(stderr); |
| 8316 | close(0); close(1); close(2); /* close all fd's */ |
| 8317 | global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */ |
| 8318 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 8319 | pid = getpid(); /* update child's pid */ |
willy tarreau | e867b48 | 2005-12-17 13:28:43 +0100 | [diff] [blame] | 8320 | setsid(); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8321 | } |
| 8322 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8323 | #if defined(ENABLE_EPOLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8324 | if (cfg_polling_mechanism & POLL_USE_EPOLL) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8325 | if (epoll_loop(POLL_LOOP_ACTION_INIT)) { |
| 8326 | epoll_loop(POLL_LOOP_ACTION_RUN); |
| 8327 | epoll_loop(POLL_LOOP_ACTION_CLEAN); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8328 | cfg_polling_mechanism &= POLL_USE_EPOLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8329 | } |
| 8330 | else { |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8331 | Warning("epoll() is not available. Using poll()/select() instead.\n"); |
| 8332 | cfg_polling_mechanism &= ~POLL_USE_EPOLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8333 | } |
| 8334 | } |
| 8335 | #endif |
| 8336 | |
| 8337 | #if defined(ENABLE_POLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8338 | if (cfg_polling_mechanism & POLL_USE_POLL) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8339 | if (poll_loop(POLL_LOOP_ACTION_INIT)) { |
| 8340 | poll_loop(POLL_LOOP_ACTION_RUN); |
| 8341 | poll_loop(POLL_LOOP_ACTION_CLEAN); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8342 | cfg_polling_mechanism &= POLL_USE_POLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8343 | } |
| 8344 | else { |
| 8345 | Warning("poll() is not available. Using select() instead.\n"); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8346 | cfg_polling_mechanism &= ~POLL_USE_POLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8347 | } |
| 8348 | } |
| 8349 | #endif |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8350 | if (cfg_polling_mechanism & POLL_USE_SELECT) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8351 | if (select_loop(POLL_LOOP_ACTION_INIT)) { |
| 8352 | select_loop(POLL_LOOP_ACTION_RUN); |
| 8353 | select_loop(POLL_LOOP_ACTION_CLEAN); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8354 | cfg_polling_mechanism &= POLL_USE_SELECT; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8355 | } |
| 8356 | } |
| 8357 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8358 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8359 | /* Free all Hash Keys and all Hash elements */ |
| 8360 | appsession_cleanup(); |
| 8361 | /* Do some cleanup */ |
| 8362 | deinit(); |
| 8363 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8364 | exit(0); |
| 8365 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8366 | |
| 8367 | #if defined(DEBUG_HASH) |
| 8368 | static void print_table(const CHTbl *htbl) { |
| 8369 | |
| 8370 | ListElmt *element; |
| 8371 | int i; |
| 8372 | appsess *asession; |
| 8373 | |
| 8374 | /***************************************************************************** |
| 8375 | * * |
| 8376 | * Display the chained hash table. * |
| 8377 | * * |
| 8378 | *****************************************************************************/ |
| 8379 | |
| 8380 | fprintf(stdout, "Table size is %d\n", chtbl_size(htbl)); |
| 8381 | |
| 8382 | for (i = 0; i < TBLSIZ; i++) { |
| 8383 | fprintf(stdout, "Bucket[%03d]\n", i); |
| 8384 | |
| 8385 | for (element = list_head(&htbl->table[i]); element != NULL; element = list_next(element)) { |
| 8386 | //fprintf(stdout, "%c", *(char *)list_data(element)); |
| 8387 | asession = (appsess *)list_data(element); |
| 8388 | fprintf(stdout, "ELEM :%s:", asession->sessid); |
| 8389 | fprintf(stdout, " Server :%s: \n", asession->serverid); |
| 8390 | //fprintf(stdout, " Server request_count :%li:\n",asession->request_count); |
| 8391 | } |
| 8392 | |
| 8393 | fprintf(stdout, "\n"); |
| 8394 | } |
| 8395 | return; |
| 8396 | } /* end print_table */ |
| 8397 | #endif |
| 8398 | |
| 8399 | static int appsession_init(void) |
| 8400 | { |
| 8401 | static int initialized = 0; |
| 8402 | int idlen; |
| 8403 | struct server *s; |
| 8404 | struct proxy *p = proxy; |
| 8405 | |
| 8406 | if (!initialized) { |
| 8407 | if (!appsession_task_init()) { |
| 8408 | apools.sessid = NULL; |
| 8409 | apools.serverid = NULL; |
| 8410 | apools.ser_waste = 0; |
| 8411 | apools.ser_use = 0; |
| 8412 | apools.ser_msize = sizeof(void *); |
| 8413 | apools.ses_waste = 0; |
| 8414 | apools.ses_use = 0; |
| 8415 | apools.ses_msize = sizeof(void *); |
| 8416 | while (p) { |
| 8417 | s = p->srv; |
| 8418 | if (apools.ses_msize < p->appsession_len) |
| 8419 | apools.ses_msize = p->appsession_len; |
| 8420 | while (s) { |
| 8421 | idlen = strlen(s->id); |
| 8422 | if (apools.ser_msize < idlen) |
| 8423 | apools.ser_msize = idlen; |
| 8424 | s = s->next; |
| 8425 | } |
| 8426 | p = p->next; |
| 8427 | } |
| 8428 | apools.ser_msize ++; /* we use strings, so reserve space for '\0' */ |
| 8429 | apools.ses_msize ++; |
| 8430 | } |
| 8431 | else { |
| 8432 | fprintf(stderr, "appsession_task_init failed\n"); |
| 8433 | return -1; |
| 8434 | } |
| 8435 | initialized ++; |
| 8436 | } |
| 8437 | return 0; |
| 8438 | } |
| 8439 | |
| 8440 | static int appsession_task_init(void) |
| 8441 | { |
| 8442 | static int initialized = 0; |
| 8443 | struct task *t; |
| 8444 | if (!initialized) { |
| 8445 | if ((t = pool_alloc(task)) == NULL) |
| 8446 | return -1; |
| 8447 | t->next = t->prev = t->rqnext = NULL; |
| 8448 | t->wq = LIST_HEAD(wait_queue); |
| 8449 | t->state = TASK_IDLE; |
| 8450 | t->context = NULL; |
| 8451 | tv_delayfrom(&t->expire, &now, TBLCHKINT); |
| 8452 | task_queue(t); |
| 8453 | t->process = appsession_refresh; |
| 8454 | initialized ++; |
| 8455 | } |
| 8456 | return 0; |
| 8457 | } |
| 8458 | |
| 8459 | static int appsession_refresh(struct task *t) { |
| 8460 | struct proxy *p = proxy; |
| 8461 | CHTbl *htbl; |
| 8462 | ListElmt *element, *last; |
| 8463 | int i; |
| 8464 | appsess *asession; |
| 8465 | void *data; |
| 8466 | |
| 8467 | while (p) { |
| 8468 | if (p->appsession_name != NULL) { |
| 8469 | htbl = &p->htbl_proxy; |
| 8470 | /* if we ever give up the use of TBLSIZ, we need to change this */ |
| 8471 | for (i = 0; i < TBLSIZ; i++) { |
| 8472 | last = NULL; |
| 8473 | for (element = list_head(&htbl->table[i]); element != NULL; element = list_next(element)) { |
| 8474 | asession = (appsess *)list_data(element); |
| 8475 | if (tv_cmp2_ms(&asession->expire, &now) <= 0) { |
| 8476 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 8477 | int len; |
| 8478 | /* |
| 8479 | on Linux NULL pointers are catched by sprintf, on solaris -> segfault |
| 8480 | */ |
| 8481 | len = sprintf(trash, "appsession_refresh: cleaning up expired Session '%s' on Server %s\n", |
| 8482 | asession->sessid, asession->serverid?asession->serverid:"(null)"); |
| 8483 | write(1, trash, len); |
| 8484 | } |
| 8485 | /* delete the expired element from within the hash table */ |
| 8486 | if ((list_rem_next(&htbl->table[i], last, (void **)&data) == 0) |
| 8487 | && (htbl->table[i].destroy != NULL)) { |
| 8488 | htbl->table[i].destroy(data); |
| 8489 | } |
| 8490 | if (last == NULL) {/* patient lost his head, get a new one */ |
| 8491 | element = list_head(&htbl->table[i]); |
| 8492 | if (element == NULL) break; /* no heads left, go to next patient */ |
| 8493 | } |
| 8494 | else |
| 8495 | element = last; |
| 8496 | }/* end if (tv_cmp2_ms(&asession->expire, &now) <= 0) */ |
| 8497 | else |
| 8498 | last = element; |
| 8499 | }/* end for (element = list_head(&htbl->table[i]); element != NULL; element = list_next(element)) */ |
| 8500 | } |
| 8501 | } |
| 8502 | p = p->next; |
| 8503 | } |
| 8504 | tv_delayfrom(&t->expire, &now, TBLCHKINT); /* check expiration every 5 seconds */ |
| 8505 | return TBLCHKINT; |
| 8506 | } /* end appsession_refresh */ |
| 8507 | |