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 | 598da41 | 2005-12-18 01:07:29 +0100 | [diff] [blame] | 84 | #include "include/appsession.h" |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 85 | |
willy tarreau | 065f1c0 | 2006-01-29 22:10:07 +0100 | [diff] [blame] | 86 | #define HAPROXY_VERSION "1.2.8" |
| 87 | #define HAPROXY_DATE "2006/01/29" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 88 | |
| 89 | /* this is for libc5 for example */ |
| 90 | #ifndef TCP_NODELAY |
| 91 | #define TCP_NODELAY 1 |
| 92 | #endif |
| 93 | |
| 94 | #ifndef SHUT_RD |
| 95 | #define SHUT_RD 0 |
| 96 | #endif |
| 97 | |
| 98 | #ifndef SHUT_WR |
| 99 | #define SHUT_WR 1 |
| 100 | #endif |
| 101 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 102 | /* |
| 103 | * BUFSIZE defines the size of a read and write buffer. It is the maximum |
| 104 | * amount of bytes which can be stored by the proxy for each session. However, |
| 105 | * when reading HTTP headers, the proxy needs some spare space to add or rewrite |
| 106 | * headers if needed. The size of this spare is defined with MAXREWRITE. So it |
| 107 | * is not possible to process headers longer than BUFSIZE-MAXREWRITE bytes. By |
| 108 | * default, BUFSIZE=16384 bytes and MAXREWRITE=BUFSIZE/2, so the maximum length |
| 109 | * of headers accepted is 8192 bytes, which is in line with Apache's limits. |
| 110 | */ |
| 111 | #ifndef BUFSIZE |
| 112 | #define BUFSIZE 16384 |
| 113 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 114 | |
| 115 | // reserved buffer space for header rewriting |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 116 | #ifndef MAXREWRITE |
| 117 | #define MAXREWRITE (BUFSIZE / 2) |
| 118 | #endif |
| 119 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 120 | #define REQURI_LEN 1024 |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 121 | #define CAPTURE_LEN 64 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 122 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 123 | // max # args on a configuration line |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 124 | #define MAX_LINE_ARGS 40 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 125 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 126 | // max # of added headers per request |
| 127 | #define MAX_NEWHDR 10 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 128 | |
| 129 | // max # of matches per regexp |
| 130 | #define MAX_MATCH 10 |
| 131 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 132 | // cookie delimitor in "prefix" mode. This character is inserted between the |
| 133 | // persistence cookie and the original value. The '~' is allowed by RFC2965, |
| 134 | // and should not be too common in server names. |
| 135 | #ifndef COOKIE_DELIM |
| 136 | #define COOKIE_DELIM '~' |
| 137 | #endif |
| 138 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 139 | #define CONN_RETRIES 3 |
| 140 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 141 | #define CHK_CONNTIME 2000 |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 142 | #define DEF_CHKINTR 2000 |
| 143 | #define DEF_FALLTIME 3 |
| 144 | #define DEF_RISETIME 2 |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 145 | #define DEF_CHECK_REQ "OPTIONS / HTTP/1.0\r\n\r\n" |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 146 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 147 | /* default connections limit */ |
| 148 | #define DEFAULT_MAXCONN 2000 |
| 149 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 150 | /* how many bits are needed to code the size of an int (eg: 32bits -> 5) */ |
| 151 | #define INTBITS 5 |
| 152 | |
| 153 | /* show stats this every millisecond, 0 to disable */ |
| 154 | #ifndef STATTIME |
| 155 | #define STATTIME 2000 |
| 156 | #endif |
| 157 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 158 | /* this reduces the number of calls to select() by choosing appropriate |
| 159 | * sheduler precision in milliseconds. It should be near the minimum |
| 160 | * time that is needed by select() to collect all events. All timeouts |
| 161 | * are rounded up by adding this value prior to pass it to select(). |
| 162 | */ |
| 163 | #define SCHEDULER_RESOLUTION 9 |
| 164 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 165 | #define TIME_ETERNITY -1 |
| 166 | /* returns the lowest delay amongst <old> and <new>, and respects TIME_ETERNITY */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 167 | #define MINTIME(old, new) (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old))) |
| 168 | #define SETNOW(a) (*a=now) |
| 169 | |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 170 | /****** string-specific macros and functions ******/ |
| 171 | /* if a > max, then bound <a> to <max>. The macro returns the new <a> */ |
| 172 | #define UBOUND(a, max) ({ typeof(a) b = (max); if ((a) > b) (a) = b; (a); }) |
| 173 | |
| 174 | /* if a < min, then bound <a> to <min>. The macro returns the new <a> */ |
| 175 | #define LBOUND(a, min) ({ typeof(a) b = (min); if ((a) < b) (a) = b; (a); }) |
| 176 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 177 | /* returns 1 only if only zero or one bit is set in X, which means that X is a |
| 178 | * power of 2, and 0 otherwise */ |
| 179 | #define POWEROF2(x) (((x) & ((x)-1)) == 0) |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 180 | /* |
| 181 | * copies at most <size-1> chars from <src> to <dst>. Last char is always |
| 182 | * set to 0, unless <size> is 0. The number of chars copied is returned |
| 183 | * (excluding the terminating zero). |
| 184 | * This code has been optimized for size and speed : on x86, it's 45 bytes |
| 185 | * long, uses only registers, and consumes only 4 cycles per char. |
| 186 | */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 187 | int strlcpy2(char *dst, const char *src, int size) { |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 188 | char *orig = dst; |
| 189 | if (size) { |
| 190 | while (--size && (*dst = *src)) { |
| 191 | src++; dst++; |
| 192 | } |
| 193 | *dst = 0; |
| 194 | } |
| 195 | return dst - orig; |
| 196 | } |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 197 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 198 | /* |
| 199 | * Returns a pointer to an area of <__len> bytes taken from the pool <pool> or |
| 200 | * dynamically allocated. In the first case, <__pool> is updated to point to |
| 201 | * the next element in the list. |
| 202 | */ |
| 203 | #define pool_alloc_from(__pool, __len) ({ \ |
| 204 | void *__p; \ |
| 205 | if ((__p = (__pool)) == NULL) \ |
| 206 | __p = malloc(((__len) >= sizeof (void *)) ? (__len) : sizeof(void *)); \ |
| 207 | else { \ |
| 208 | __pool = *(void **)(__pool); \ |
| 209 | } \ |
| 210 | __p; \ |
| 211 | }) |
| 212 | |
| 213 | /* |
| 214 | * Puts a memory area back to the corresponding pool. |
| 215 | * Items are chained directly through a pointer that |
| 216 | * is written in the beginning of the memory area, so |
| 217 | * there's no need for any carrier cell. This implies |
| 218 | * that each memory area is at least as big as one |
| 219 | * pointer. |
| 220 | */ |
| 221 | #define pool_free_to(__pool, __ptr) ({ \ |
| 222 | *(void **)(__ptr) = (void *)(__pool); \ |
| 223 | __pool = (void *)(__ptr); \ |
| 224 | }) |
| 225 | |
| 226 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 227 | #define MEM_OPTIM |
| 228 | #ifdef MEM_OPTIM |
| 229 | /* |
| 230 | * Returns a pointer to type <type> taken from the |
| 231 | * pool <pool_type> or dynamically allocated. In the |
| 232 | * first case, <pool_type> is updated to point to the |
| 233 | * next element in the list. |
| 234 | */ |
| 235 | #define pool_alloc(type) ({ \ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 236 | void *__p; \ |
| 237 | if ((__p = pool_##type) == NULL) \ |
| 238 | __p = malloc(sizeof_##type); \ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 239 | else { \ |
| 240 | pool_##type = *(void **)pool_##type; \ |
| 241 | } \ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 242 | __p; \ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 243 | }) |
| 244 | |
| 245 | /* |
| 246 | * Puts a memory area back to the corresponding pool. |
| 247 | * Items are chained directly through a pointer that |
| 248 | * is written in the beginning of the memory area, so |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 249 | * there's no need for any carrier cell. This implies |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 250 | * that each memory area is at least as big as one |
| 251 | * pointer. |
| 252 | */ |
| 253 | #define pool_free(type, ptr) ({ \ |
| 254 | *(void **)ptr = (void *)pool_##type; \ |
| 255 | pool_##type = (void *)ptr; \ |
| 256 | }) |
| 257 | |
| 258 | #else |
| 259 | #define pool_alloc(type) (calloc(1,sizeof_##type)); |
| 260 | #define pool_free(type, ptr) (free(ptr)); |
| 261 | #endif /* MEM_OPTIM */ |
| 262 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 263 | #define sizeof_task sizeof(struct task) |
| 264 | #define sizeof_session sizeof(struct session) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 265 | #define sizeof_buffer sizeof(struct buffer) |
| 266 | #define sizeof_fdtab sizeof(struct fdtab) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 267 | #define sizeof_requri REQURI_LEN |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 268 | #define sizeof_capture CAPTURE_LEN |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 269 | #define sizeof_curappsession CAPTURE_LEN /* current_session pool */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 270 | #define sizeof_appsess sizeof(struct appsessions) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 271 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 272 | /* different possible states for the sockets */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 273 | #define FD_STCLOSE 0 |
| 274 | #define FD_STLISTEN 1 |
| 275 | #define FD_STCONN 2 |
| 276 | #define FD_STREADY 3 |
| 277 | #define FD_STERROR 4 |
| 278 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 279 | /* values for task->state */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 280 | #define TASK_IDLE 0 |
| 281 | #define TASK_RUNNING 1 |
| 282 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 283 | /* values for proxy->state */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 284 | #define PR_STNEW 0 |
| 285 | #define PR_STIDLE 1 |
| 286 | #define PR_STRUN 2 |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 287 | #define PR_STSTOPPED 3 |
| 288 | #define PR_STPAUSED 4 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 289 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 290 | /* values for proxy->mode */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 291 | #define PR_MODE_TCP 0 |
| 292 | #define PR_MODE_HTTP 1 |
| 293 | #define PR_MODE_HEALTH 2 |
| 294 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 295 | /* possible actions for the *poll() loops */ |
| 296 | #define POLL_LOOP_ACTION_INIT 0 |
| 297 | #define POLL_LOOP_ACTION_RUN 1 |
| 298 | #define POLL_LOOP_ACTION_CLEAN 2 |
| 299 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 300 | /* poll mechanisms available */ |
| 301 | #define POLL_USE_SELECT (1<<0) |
| 302 | #define POLL_USE_POLL (1<<1) |
| 303 | #define POLL_USE_EPOLL (1<<2) |
| 304 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 305 | /* bits for proxy->options */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 306 | #define PR_O_REDISP 0x00000001 /* allow reconnection to dispatch in case of errors */ |
| 307 | #define PR_O_TRANSP 0x00000002 /* transparent mode : use original DEST as dispatch */ |
| 308 | #define PR_O_COOK_RW 0x00000004 /* rewrite all direct cookies with the right serverid */ |
| 309 | #define PR_O_COOK_IND 0x00000008 /* keep only indirect cookies */ |
| 310 | #define PR_O_COOK_INS 0x00000010 /* insert cookies when not accessing a server directly */ |
| 311 | #define PR_O_COOK_PFX 0x00000020 /* rewrite all cookies by prefixing the right serverid */ |
| 312 | #define PR_O_COOK_ANY (PR_O_COOK_RW | PR_O_COOK_IND | PR_O_COOK_INS | PR_O_COOK_PFX) |
| 313 | #define PR_O_BALANCE_RR 0x00000040 /* balance in round-robin mode */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 314 | #define PR_O_BALANCE (PR_O_BALANCE_RR) |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 315 | #define PR_O_KEEPALIVE 0x00000080 /* follow keep-alive sessions */ |
| 316 | #define PR_O_FWDFOR 0x00000100 /* insert x-forwarded-for with client address */ |
| 317 | #define PR_O_BIND_SRC 0x00000200 /* bind to a specific source address when connect()ing */ |
| 318 | #define PR_O_NULLNOLOG 0x00000400 /* a connect without request will not be logged */ |
| 319 | #define PR_O_COOK_NOC 0x00000800 /* add a 'Cache-control' header with the cookie */ |
| 320 | #define PR_O_COOK_POST 0x00001000 /* don't insert cookies for requests other than a POST */ |
| 321 | #define PR_O_HTTP_CHK 0x00002000 /* use HTTP 'OPTIONS' method to check server health */ |
| 322 | #define PR_O_PERSIST 0x00004000 /* server persistence stays effective even when server is down */ |
| 323 | #define PR_O_LOGASAP 0x00008000 /* log as soon as possible, without waiting for the session to complete */ |
| 324 | #define PR_O_HTTP_CLOSE 0x00010000 /* force 'connection: close' in both directions */ |
| 325 | #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] | 326 | #define PR_O_TCP_CLI_KA 0x00040000 /* enable TCP keep-alive on client-side sessions */ |
| 327 | #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] | 328 | #define PR_O_USE_ALL_BK 0x00100000 /* load-balance between backup servers */ |
Willy TARREAU | 767ba71 | 2006-03-01 22:40:50 +0100 | [diff] [blame] | 329 | #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] | 330 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 331 | /* various session flags */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 332 | #define SN_DIRECT 0x00000001 /* connection made on the server matching the client cookie */ |
| 333 | #define SN_CLDENY 0x00000002 /* a client header matches a deny regex */ |
| 334 | #define SN_CLALLOW 0x00000004 /* a client header matches an allow regex */ |
| 335 | #define SN_SVDENY 0x00000008 /* a server header matches a deny regex */ |
| 336 | #define SN_SVALLOW 0x00000010 /* a server header matches an allow regex */ |
| 337 | #define SN_POST 0x00000020 /* the request was an HTTP POST */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 338 | #define SN_MONITOR 0x00000040 /* this session comes from a monitoring system */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 339 | |
| 340 | #define SN_CK_NONE 0x00000000 /* this session had no cookie */ |
| 341 | #define SN_CK_INVALID 0x00000040 /* this session had a cookie which matches no server */ |
| 342 | #define SN_CK_DOWN 0x00000080 /* this session had cookie matching a down server */ |
| 343 | #define SN_CK_VALID 0x000000C0 /* this session had cookie matching a valid server */ |
| 344 | #define SN_CK_MASK 0x000000C0 /* mask to get this session's cookie flags */ |
| 345 | #define SN_CK_SHIFT 6 /* bit shift */ |
| 346 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 347 | #define SN_ERR_NONE 0x00000000 |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 348 | #define SN_ERR_CLITO 0x00000100 /* client time-out */ |
| 349 | #define SN_ERR_CLICL 0x00000200 /* client closed (read/write error) */ |
| 350 | #define SN_ERR_SRVTO 0x00000300 /* server time-out, connect time-out */ |
| 351 | #define SN_ERR_SRVCL 0x00000400 /* server closed (connect/read/write error) */ |
| 352 | #define SN_ERR_PRXCOND 0x00000500 /* the proxy decided to close (deny...) */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 353 | #define SN_ERR_RESOURCE 0x00000600 /* the proxy encountered a lack of a local resources (fd, mem, ...) */ |
| 354 | #define SN_ERR_INTERNAL 0x00000700 /* the proxy encountered an internal error */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 355 | #define SN_ERR_MASK 0x00000700 /* mask to get only session error flags */ |
| 356 | #define SN_ERR_SHIFT 8 /* bit shift */ |
| 357 | |
| 358 | #define SN_FINST_R 0x00001000 /* session ended during client request */ |
| 359 | #define SN_FINST_C 0x00002000 /* session ended during server connect */ |
| 360 | #define SN_FINST_H 0x00003000 /* session ended during server headers */ |
| 361 | #define SN_FINST_D 0x00004000 /* session ended during data phase */ |
| 362 | #define SN_FINST_L 0x00005000 /* session ended while pushing last data to client */ |
| 363 | #define SN_FINST_MASK 0x00007000 /* mask to get only final session state flags */ |
| 364 | #define SN_FINST_SHIFT 12 /* bit shift */ |
| 365 | |
| 366 | #define SN_SCK_NONE 0x00000000 /* no set-cookie seen for the server cookie */ |
| 367 | #define SN_SCK_DELETED 0x00010000 /* existing set-cookie deleted or changed */ |
| 368 | #define SN_SCK_INSERTED 0x00020000 /* new set-cookie inserted or changed existing one */ |
| 369 | #define SN_SCK_SEEN 0x00040000 /* set-cookie seen for the server cookie */ |
| 370 | #define SN_SCK_MASK 0x00070000 /* mask to get the set-cookie field */ |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 371 | #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] | 372 | #define SN_SCK_SHIFT 16 /* bit shift */ |
| 373 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 374 | #define SN_CACHEABLE 0x00100000 /* at least part of the response is cacheable */ |
| 375 | #define SN_CACHE_COOK 0x00200000 /* a cookie in the response is cacheable */ |
| 376 | #define SN_CACHE_SHIFT 20 /* bit shift */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 377 | |
| 378 | /* different possible states for the client side */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 379 | #define CL_STHEADERS 0 |
| 380 | #define CL_STDATA 1 |
| 381 | #define CL_STSHUTR 2 |
| 382 | #define CL_STSHUTW 3 |
| 383 | #define CL_STCLOSE 4 |
| 384 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 385 | /* different possible states for the server side */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 386 | #define SV_STIDLE 0 |
| 387 | #define SV_STCONN 1 |
| 388 | #define SV_STHEADERS 2 |
| 389 | #define SV_STDATA 3 |
| 390 | #define SV_STSHUTR 4 |
| 391 | #define SV_STSHUTW 5 |
| 392 | #define SV_STCLOSE 6 |
| 393 | |
| 394 | /* result of an I/O event */ |
| 395 | #define RES_SILENT 0 /* didn't happen */ |
| 396 | #define RES_DATA 1 /* data were sent or received */ |
| 397 | #define RES_NULL 2 /* result is 0 (read == 0), or connect without need for writing */ |
| 398 | #define RES_ERROR 3 /* result -1 or error on the socket (eg: connect()) */ |
| 399 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 400 | /* modes of operation (global.mode) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 401 | #define MODE_DEBUG 1 |
| 402 | #define MODE_STATS 2 |
| 403 | #define MODE_LOG 4 |
| 404 | #define MODE_DAEMON 8 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 405 | #define MODE_QUIET 16 |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 406 | #define MODE_CHECK 32 |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 407 | #define MODE_VERBOSE 64 |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 408 | #define MODE_STARTING 128 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 409 | |
| 410 | /* server flags */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 411 | #define SRV_RUNNING 1 /* the server is UP */ |
| 412 | #define SRV_BACKUP 2 /* this server is a backup server */ |
| 413 | #define SRV_MAPPORTS 4 /* this server uses mapped ports */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 414 | #define SRV_BIND_SRC 8 /* this server uses a specific source address */ |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame^] | 415 | #define SRV_CHECKED 16 /* this server needs to be checked */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 416 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 417 | /* what to do when a header matches a regex */ |
| 418 | #define ACT_ALLOW 0 /* allow the request */ |
| 419 | #define ACT_REPLACE 1 /* replace the matching header */ |
| 420 | #define ACT_REMOVE 2 /* remove the matching header */ |
| 421 | #define ACT_DENY 3 /* deny the request */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 422 | #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] | 423 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 424 | /* configuration sections */ |
| 425 | #define CFG_NONE 0 |
| 426 | #define CFG_GLOBAL 1 |
| 427 | #define CFG_LISTEN 2 |
| 428 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 429 | /* 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] | 430 | #define LW_DATE 1 /* date */ |
| 431 | #define LW_CLIP 2 /* CLient IP */ |
| 432 | #define LW_SVIP 4 /* SerVer IP */ |
| 433 | #define LW_SVID 8 /* server ID */ |
| 434 | #define LW_REQ 16 /* http REQuest */ |
| 435 | #define LW_RESP 32 /* http RESPonse */ |
| 436 | #define LW_PXIP 64 /* proxy IP */ |
| 437 | #define LW_PXID 128 /* proxy ID */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 438 | #define LW_BYTES 256 /* bytes read from server */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 439 | #define LW_COOKIE 512 /* captured cookie */ |
| 440 | #define LW_REQHDR 1024 /* request header(s) */ |
| 441 | #define LW_RSPHDR 2048 /* response header(s) */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 442 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 443 | /*********************************************************************/ |
| 444 | |
| 445 | #define LIST_HEAD(a) ((void *)(&(a))) |
| 446 | |
| 447 | /*********************************************************************/ |
| 448 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 449 | struct cap_hdr { |
| 450 | struct cap_hdr *next; |
| 451 | char *name; /* header name, case insensitive */ |
| 452 | int namelen; /* length of the header name, to speed-up lookups */ |
| 453 | int len; /* capture length, not including terminal zero */ |
| 454 | int index; /* index in the output array */ |
| 455 | void *pool; /* pool of pre-allocated memory area of (len+1) bytes */ |
| 456 | }; |
| 457 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 458 | struct hdr_exp { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 459 | struct hdr_exp *next; |
| 460 | regex_t *preg; /* expression to look for */ |
| 461 | int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */ |
| 462 | char *replace; /* expression to set instead */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 463 | }; |
| 464 | |
| 465 | struct buffer { |
| 466 | unsigned int l; /* data length */ |
| 467 | 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] | 468 | char *rlim; /* read limit, used for header rewriting */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 469 | unsigned long long total; /* total data read */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 470 | char data[BUFSIZE]; |
| 471 | }; |
| 472 | |
| 473 | struct server { |
| 474 | struct server *next; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 475 | int state; /* server state (SRV_*) */ |
| 476 | int cklen; /* the len of the cookie, to speed up checks */ |
| 477 | char *cookie; /* the id set in the cookie */ |
| 478 | char *id; /* just for identification */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 479 | struct sockaddr_in addr; /* the address to connect to */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 480 | 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] | 481 | short check_port; /* the port to use for the health checks */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 482 | int health; /* 0->rise-1 = bad; rise->rise+fall-1 = good */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 483 | int rise, fall; /* time in iterations */ |
| 484 | int inter; /* time in milliseconds */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 485 | int result; /* 0 = connect OK, -1 = connect KO */ |
| 486 | 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] | 487 | struct proxy *proxy; /* the proxy this server belongs to */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 488 | }; |
| 489 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 490 | /* The base for all tasks */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 491 | struct task { |
| 492 | struct task *next, *prev; /* chaining ... */ |
| 493 | struct task *rqnext; /* chaining in run queue ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 494 | struct task *wq; /* the wait queue this task is in */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 495 | int state; /* task state : IDLE or RUNNING */ |
| 496 | 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] | 497 | int (*process)(struct task *t); /* the function which processes the task */ |
| 498 | void *context; /* the task's context */ |
| 499 | }; |
| 500 | |
| 501 | /* WARNING: if new fields are added, they must be initialized in event_accept() */ |
| 502 | struct session { |
| 503 | struct task *task; /* the task associated with this session */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 504 | /* application specific below */ |
| 505 | struct timeval crexpire; /* expiration date for a client read */ |
| 506 | struct timeval cwexpire; /* expiration date for a client write */ |
| 507 | struct timeval srexpire; /* expiration date for a server read */ |
| 508 | struct timeval swexpire; /* expiration date for a server write */ |
| 509 | struct timeval cnexpire; /* expiration date for a connect */ |
| 510 | char res_cr, res_cw, res_sr, res_sw;/* results of some events */ |
| 511 | struct proxy *proxy; /* the proxy this socket belongs to */ |
| 512 | int cli_fd; /* the client side fd */ |
| 513 | int srv_fd; /* the server side fd */ |
| 514 | int cli_state; /* state of the client side */ |
| 515 | int srv_state; /* state of the server side */ |
| 516 | int conn_retries; /* number of connect retries left */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 517 | int flags; /* some flags describing the session */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 518 | struct buffer *req; /* request buffer */ |
| 519 | struct buffer *rep; /* response buffer */ |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 520 | struct sockaddr_storage cli_addr; /* the client address */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 521 | struct sockaddr_in srv_addr; /* the address to connect to */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 522 | struct server *srv; /* the server being used */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 523 | char **req_cap; /* array of captured request headers (may be NULL) */ |
| 524 | char **rsp_cap; /* array of captured response headers (may be NULL) */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 525 | struct { |
| 526 | int logwait; /* log fields waiting to be collected : LW_* */ |
| 527 | struct timeval tv_accept; /* date of the accept() (beginning of the session) */ |
| 528 | long t_request; /* delay before the end of the request arrives, -1 if never occurs */ |
| 529 | long t_connect; /* delay before the connect() to the server succeeds, -1 if never occurs */ |
| 530 | long t_data; /* delay before the first data byte from the server ... */ |
| 531 | unsigned long t_close; /* total session duration */ |
| 532 | char *uri; /* first line if log needed, NULL otherwise */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 533 | char *cli_cookie; /* cookie presented by the client, in capture mode */ |
| 534 | char *srv_cookie; /* cookie presented by the server, in capture mode */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 535 | int status; /* HTTP status from the server, negative if from proxy */ |
| 536 | long long bytes; /* number of bytes transferred from the server */ |
| 537 | } logs; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 538 | unsigned int uniq_id; /* unique ID used for the traces */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 539 | }; |
| 540 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 541 | struct listener { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 542 | int fd; /* the listen socket */ |
| 543 | struct sockaddr_storage addr; /* the address we listen to */ |
| 544 | struct listener *next; /* next address or NULL */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 545 | }; |
| 546 | |
| 547 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 548 | struct proxy { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 549 | struct listener *listen; /* the listen addresses and sockets */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 550 | 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] | 551 | int state; /* proxy state */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 552 | struct sockaddr_in dispatch_addr; /* the default address to connect to */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 553 | struct server *srv, *cursrv; /* known servers, current server */ |
| 554 | int nbservers; /* # of servers */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 555 | char *cookie_name; /* name of the cookie to look for */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 556 | int cookie_len; /* strlen(cookie_name), computed only once */ |
| 557 | char *appsession_name; /* name of the cookie to look for */ |
| 558 | int appsession_name_len; /* strlen(appsession_name), computed only once */ |
| 559 | int appsession_len; /* length of the appsession cookie value to be used */ |
| 560 | int appsession_timeout; |
| 561 | CHTbl htbl_proxy; /* Per Proxy hashtable */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 562 | char *capture_name; /* beginning of the name of the cookie to capture */ |
| 563 | int capture_namelen; /* length of the cookie name to match */ |
| 564 | int capture_len; /* length of the string to be captured */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 565 | int clitimeout; /* client I/O timeout (in milliseconds) */ |
| 566 | int srvtimeout; /* server I/O timeout (in milliseconds) */ |
| 567 | int contimeout; /* connect timeout (in milliseconds) */ |
| 568 | char *id; /* proxy id */ |
| 569 | int nbconn; /* # of active sessions */ |
| 570 | int maxconn; /* max # of active sessions */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 571 | int conn_retries; /* maximum number of connect retries */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 572 | int options; /* PR_O_REDISP, PR_O_TRANSP, ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 573 | 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] | 574 | 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] | 575 | struct proxy *next; |
| 576 | struct sockaddr_in logsrv1, logsrv2; /* 2 syslog servers */ |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 577 | signed char logfac1, logfac2; /* log facility for both servers. -1 = disabled */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 578 | int loglev1, loglev2; /* log level for each server, 7 by default */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 579 | int to_log; /* things to be logged (LW_*) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 580 | struct timeval stop_time; /* date to stop listening, when stopping != 0 */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 581 | int nb_reqadd, nb_rspadd; |
| 582 | struct hdr_exp *req_exp; /* regular expressions for request headers */ |
| 583 | struct hdr_exp *rsp_exp; /* regular expressions for response headers */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 584 | int nb_req_cap, nb_rsp_cap; /* # of headers to be captured */ |
| 585 | struct cap_hdr *req_cap; /* chained list of request headers to be captured */ |
| 586 | struct cap_hdr *rsp_cap; /* chained list of response headers to be captured */ |
| 587 | 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] | 588 | 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] | 589 | int grace; /* grace time after stop request */ |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 590 | char *check_req; /* HTTP request to use if PR_O_HTTP_CHK is set, else NULL */ |
| 591 | int check_len; /* Length of the HTTP request */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 592 | struct { |
| 593 | char *msg400; /* message for error 400 */ |
| 594 | int len400; /* message length for error 400 */ |
| 595 | char *msg403; /* message for error 403 */ |
| 596 | int len403; /* message length for error 403 */ |
| 597 | char *msg408; /* message for error 408 */ |
| 598 | int len408; /* message length for error 408 */ |
| 599 | char *msg500; /* message for error 500 */ |
| 600 | int len500; /* message length for error 500 */ |
| 601 | char *msg502; /* message for error 502 */ |
| 602 | int len502; /* message length for error 502 */ |
| 603 | char *msg503; /* message for error 503 */ |
| 604 | int len503; /* message length for error 503 */ |
| 605 | char *msg504; /* message for error 504 */ |
| 606 | int len504; /* message length for error 504 */ |
| 607 | } errmsg; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 608 | }; |
| 609 | |
| 610 | /* info about one given fd */ |
| 611 | struct fdtab { |
| 612 | int (*read)(int fd); /* read function */ |
| 613 | int (*write)(int fd); /* write function */ |
| 614 | struct task *owner; /* the session (or proxy) associated with this fd */ |
| 615 | int state; /* the state of this fd */ |
| 616 | }; |
| 617 | |
| 618 | /*********************************************************************/ |
| 619 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 620 | int cfg_maxpconn = DEFAULT_MAXCONN; /* # of simultaneous connections per proxy (-N) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 621 | char *cfg_cfgfile = NULL; /* configuration file */ |
| 622 | char *progname = NULL; /* program name */ |
| 623 | int pid; /* current process id */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 624 | |
| 625 | /* global options */ |
| 626 | static struct { |
| 627 | int uid; |
| 628 | int gid; |
| 629 | int nbproc; |
| 630 | int maxconn; |
| 631 | int maxsock; /* max # of sockets */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 632 | int rlimit_nofile; /* default ulimit-n value : 0=unset */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 633 | int mode; |
| 634 | char *chroot; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 635 | char *pidfile; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 636 | int logfac1, logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 637 | int loglev1, loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 638 | struct sockaddr_in logsrv1, logsrv2; |
| 639 | } global = { |
| 640 | logfac1 : -1, |
| 641 | logfac2 : -1, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 642 | loglev1 : 7, /* max syslog level : debug */ |
| 643 | loglev2 : 7, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 644 | /* others NULL OK */ |
| 645 | }; |
| 646 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 647 | /*********************************************************************/ |
| 648 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 649 | fd_set *StaticReadEvent, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 650 | *StaticWriteEvent; |
| 651 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 652 | int cfg_polling_mechanism = 0; /* POLL_USE_{SELECT|POLL|EPOLL} */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 653 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 654 | void **pool_session = NULL, |
| 655 | **pool_buffer = NULL, |
| 656 | **pool_fdtab = NULL, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 657 | **pool_requri = NULL, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 658 | **pool_task = NULL, |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 659 | **pool_capture = NULL, |
| 660 | **pool_appsess = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 661 | |
| 662 | struct proxy *proxy = NULL; /* list of all existing proxies */ |
| 663 | struct fdtab *fdtab = NULL; /* array of all the file descriptors */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 664 | struct task *rq = NULL; /* global run queue */ |
| 665 | struct task wait_queue = { /* global wait queue */ |
| 666 | prev:LIST_HEAD(wait_queue), |
| 667 | next:LIST_HEAD(wait_queue) |
| 668 | }; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 669 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 670 | static int totalconn = 0; /* total # of terminated sessions */ |
| 671 | static int actconn = 0; /* # of active sessions */ |
| 672 | static int maxfd = 0; /* # of the highest fd + 1 */ |
| 673 | static int listeners = 0; /* # of listeners */ |
| 674 | static int stopping = 0; /* non zero means stopping in progress */ |
| 675 | static struct timeval now = {0,0}; /* the current date at any moment */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 676 | 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] | 677 | |
willy tarreau | 08dedbe | 2005-12-18 01:13:48 +0100 | [diff] [blame] | 678 | #if defined(ENABLE_EPOLL) |
| 679 | /* FIXME: this is dirty, but at the moment, there's no other solution to remove |
| 680 | * the old FDs from outside the loop. Perhaps we should export a global 'poll' |
| 681 | * structure with pointers to functions such as init_fd() and close_fd(), plus |
| 682 | * a private structure with several pointers to places such as below. |
| 683 | */ |
| 684 | |
| 685 | static fd_set *PrevReadEvent = NULL, *PrevWriteEvent = NULL; |
| 686 | #endif |
| 687 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 688 | 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] | 689 | /* 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] | 690 | static char trash[BUFSIZE]; |
| 691 | |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 692 | const int zero = 0; |
| 693 | const int one = 1; |
| 694 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 695 | /* |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 696 | * Syslog facilities and levels. Conforming to RFC3164. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 697 | */ |
| 698 | |
| 699 | #define MAX_SYSLOG_LEN 1024 |
| 700 | #define NB_LOG_FACILITIES 24 |
| 701 | const char *log_facilities[NB_LOG_FACILITIES] = { |
| 702 | "kern", "user", "mail", "daemon", |
| 703 | "auth", "syslog", "lpr", "news", |
| 704 | "uucp", "cron", "auth2", "ftp", |
| 705 | "ntp", "audit", "alert", "cron2", |
| 706 | "local0", "local1", "local2", "local3", |
| 707 | "local4", "local5", "local6", "local7" |
| 708 | }; |
| 709 | |
| 710 | |
| 711 | #define NB_LOG_LEVELS 8 |
| 712 | const char *log_levels[NB_LOG_LEVELS] = { |
| 713 | "emerg", "alert", "crit", "err", |
| 714 | "warning", "notice", "info", "debug" |
| 715 | }; |
| 716 | |
| 717 | #define SYSLOG_PORT 514 |
| 718 | |
| 719 | const char *monthname[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 720 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 721 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 722 | 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] | 723 | const char sess_fin_state[8] = "-RCHDL67"; /* cliRequest, srvConnect, srvHeader, Data, Last, unknown */ |
| 724 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 725 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 726 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 727 | unknown, Set-cookie Rewritten */ |
| 728 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 729 | #define MAX_HOSTNAME_LEN 32 |
| 730 | static char hostname[MAX_HOSTNAME_LEN] = ""; |
| 731 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 732 | const char *HTTP_302 = |
| 733 | "HTTP/1.0 302 Found\r\n" |
| 734 | "Cache-Control: no-cache\r\n" |
| 735 | "Connection: close\r\n" |
| 736 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 737 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 738 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 739 | const char *HTTP_303 = |
| 740 | "HTTP/1.0 303 See Other\r\n" |
| 741 | "Cache-Control: no-cache\r\n" |
| 742 | "Connection: close\r\n" |
| 743 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 744 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 745 | const char *HTTP_400 = |
| 746 | "HTTP/1.0 400 Bad request\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 747 | "Cache-Control: no-cache\r\n" |
| 748 | "Connection: close\r\n" |
| 749 | "\r\n" |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 750 | "<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] | 751 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 752 | const char *HTTP_403 = |
| 753 | "HTTP/1.0 403 Forbidden\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 754 | "Cache-Control: no-cache\r\n" |
| 755 | "Connection: close\r\n" |
| 756 | "\r\n" |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 757 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n"; |
| 758 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 759 | const char *HTTP_408 = |
| 760 | "HTTP/1.0 408 Request Time-out\r\n" |
| 761 | "Cache-Control: no-cache\r\n" |
| 762 | "Connection: close\r\n" |
| 763 | "\r\n" |
| 764 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n"; |
| 765 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 766 | const char *HTTP_500 = |
| 767 | "HTTP/1.0 500 Server Error\r\n" |
| 768 | "Cache-Control: no-cache\r\n" |
| 769 | "Connection: close\r\n" |
| 770 | "\r\n" |
| 771 | "<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] | 772 | |
| 773 | const char *HTTP_502 = |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 774 | "HTTP/1.0 502 Bad Gateway\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 775 | "Cache-Control: no-cache\r\n" |
| 776 | "Connection: close\r\n" |
| 777 | "\r\n" |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 778 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n"; |
| 779 | |
| 780 | const char *HTTP_503 = |
| 781 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 782 | "Cache-Control: no-cache\r\n" |
| 783 | "Connection: close\r\n" |
| 784 | "\r\n" |
| 785 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n"; |
| 786 | |
| 787 | const char *HTTP_504 = |
| 788 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 789 | "Cache-Control: no-cache\r\n" |
| 790 | "Connection: close\r\n" |
| 791 | "\r\n" |
| 792 | "<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] | 793 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 794 | /*********************************************************************/ |
| 795 | /* statistics ******************************************************/ |
| 796 | /*********************************************************************/ |
| 797 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 798 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 799 | static int stats_tsk_lsrch, stats_tsk_rsrch, |
| 800 | stats_tsk_good, stats_tsk_right, stats_tsk_left, |
| 801 | stats_tsk_new, stats_tsk_nsrch; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 802 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 803 | |
| 804 | |
| 805 | /*********************************************************************/ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 806 | /* debugging *******************************************************/ |
| 807 | /*********************************************************************/ |
| 808 | #ifdef DEBUG_FULL |
| 809 | static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 810 | static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 811 | #endif |
| 812 | |
| 813 | /*********************************************************************/ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 814 | /* function prototypes *********************************************/ |
| 815 | /*********************************************************************/ |
| 816 | |
| 817 | int event_accept(int fd); |
| 818 | int event_cli_read(int fd); |
| 819 | int event_cli_write(int fd); |
| 820 | int event_srv_read(int fd); |
| 821 | int event_srv_write(int fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 822 | int process_session(struct task *t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 823 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 824 | static int appsession_task_init(void); |
| 825 | static int appsession_init(void); |
| 826 | static int appsession_refresh(struct task *t); |
| 827 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 828 | /*********************************************************************/ |
| 829 | /* general purpose functions ***************************************/ |
| 830 | /*********************************************************************/ |
| 831 | |
| 832 | void display_version() { |
| 833 | printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n"); |
willy tarreau | 726618c | 2006-01-29 22:42:06 +0100 | [diff] [blame] | 834 | printf("Copyright 2000-2006 Willy Tarreau <w@w.ods.org>\n\n"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | /* |
| 838 | * This function prints the command line usage and exits |
| 839 | */ |
| 840 | void usage(char *name) { |
| 841 | display_version(); |
| 842 | fprintf(stderr, |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 843 | "Usage : %s -f <cfgfile> [ -vdV" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 844 | #if STATTIME > 0 |
| 845 | "sl" |
| 846 | #endif |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 847 | "D ] [ -n <maxconn> ] [ -N <maxpconn> ] [ -p <pidfile> ]\n" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 848 | " -v displays version\n" |
| 849 | " -d enters debug mode\n" |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 850 | " -V enters verbose mode (disables quiet mode)\n" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 851 | #if STATTIME > 0 |
| 852 | " -s enables statistics output\n" |
| 853 | " -l enables long statistics format\n" |
| 854 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 855 | " -D goes daemon ; implies -q\n" |
| 856 | " -q quiet mode : don't display messages\n" |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 857 | " -c check mode : only check config file and exit\n" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 858 | " -n sets the maximum total # of connections (%d)\n" |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 859 | " -N sets the default, per-proxy maximum # of connections (%d)\n" |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 860 | " -p writes pids of all children to this file\n" |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 861 | #if defined(ENABLE_EPOLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 862 | " -de disables epoll() usage even when available\n" |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 863 | #endif |
| 864 | #if defined(ENABLE_POLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 865 | " -dp disables poll() usage even when available\n" |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 866 | #endif |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 867 | "\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 868 | name, DEFAULT_MAXCONN, cfg_maxpconn); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 869 | exit(1); |
| 870 | } |
| 871 | |
| 872 | |
| 873 | /* |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 874 | * Displays the message on stderr with the date and pid. Overrides the quiet |
| 875 | * mode during startup. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 876 | */ |
| 877 | void Alert(char *fmt, ...) { |
| 878 | va_list argp; |
| 879 | struct timeval tv; |
| 880 | struct tm *tm; |
| 881 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 882 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 883 | va_start(argp, fmt); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 884 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 885 | gettimeofday(&tv, NULL); |
| 886 | tm=localtime(&tv.tv_sec); |
| 887 | fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ", |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 888 | 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] | 889 | vfprintf(stderr, fmt, argp); |
| 890 | fflush(stderr); |
| 891 | va_end(argp); |
| 892 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | |
| 896 | /* |
| 897 | * Displays the message on stderr with the date and pid. |
| 898 | */ |
| 899 | void Warning(char *fmt, ...) { |
| 900 | va_list argp; |
| 901 | struct timeval tv; |
| 902 | struct tm *tm; |
| 903 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 904 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
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, "[WARNING] %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 | } |
| 915 | } |
| 916 | |
| 917 | /* |
| 918 | * Displays the message on <out> only if quiet mode is not set. |
| 919 | */ |
| 920 | void qfprintf(FILE *out, char *fmt, ...) { |
| 921 | va_list argp; |
| 922 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 923 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 924 | va_start(argp, fmt); |
| 925 | vfprintf(out, fmt, argp); |
| 926 | fflush(out); |
| 927 | va_end(argp); |
| 928 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | |
| 932 | /* |
| 933 | * converts <str> to a struct sockaddr_in* which is locally allocated. |
| 934 | * The format is "addr:port", where "addr" can be empty or "*" to indicate |
| 935 | * INADDR_ANY. |
| 936 | */ |
| 937 | struct sockaddr_in *str2sa(char *str) { |
| 938 | static struct sockaddr_in sa; |
| 939 | char *c; |
| 940 | int port; |
| 941 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 942 | memset(&sa, 0, sizeof(sa)); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 943 | str=strdup(str); |
| 944 | |
| 945 | if ((c=strrchr(str,':')) != NULL) { |
| 946 | *c++=0; |
| 947 | port=atol(c); |
| 948 | } |
| 949 | else |
| 950 | port=0; |
| 951 | |
| 952 | if (*str == '*' || *str == '\0') { /* INADDR_ANY */ |
| 953 | sa.sin_addr.s_addr = INADDR_ANY; |
| 954 | } |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 955 | else if (!inet_pton(AF_INET, str, &sa.sin_addr)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 956 | struct hostent *he; |
| 957 | |
| 958 | if ((he = gethostbyname(str)) == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 959 | Alert("Invalid server name: '%s'\n", str); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 960 | } |
| 961 | else |
| 962 | sa.sin_addr = *(struct in_addr *) *(he->h_addr_list); |
| 963 | } |
| 964 | sa.sin_port=htons(port); |
| 965 | sa.sin_family=AF_INET; |
| 966 | |
| 967 | free(str); |
| 968 | return &sa; |
| 969 | } |
| 970 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 971 | /* |
| 972 | * converts <str> to a two struct in_addr* which are locally allocated. |
| 973 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
| 974 | * is optionnal and either in the dotted or CIDR notation. |
| 975 | * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error. |
| 976 | */ |
| 977 | int str2net(char *str, struct in_addr *addr, struct in_addr *mask) { |
| 978 | char *c; |
| 979 | unsigned long len; |
| 980 | |
| 981 | memset(mask, 0, sizeof(*mask)); |
| 982 | memset(addr, 0, sizeof(*addr)); |
| 983 | str=strdup(str); |
| 984 | |
| 985 | if ((c = strrchr(str, '/')) != NULL) { |
| 986 | *c++ = 0; |
| 987 | /* c points to the mask */ |
| 988 | if (strchr(c, '.') != NULL) { /* dotted notation */ |
| 989 | if (!inet_pton(AF_INET, c, mask)) |
| 990 | return 0; |
| 991 | } |
| 992 | else { /* mask length */ |
| 993 | char *err; |
| 994 | len = strtol(c, &err, 10); |
| 995 | if (!*c || (err && *err) || (unsigned)len > 32) |
| 996 | return 0; |
| 997 | if (len) |
| 998 | mask->s_addr = htonl(0xFFFFFFFFUL << (32 - len)); |
| 999 | else |
| 1000 | mask->s_addr = 0; |
| 1001 | } |
| 1002 | } |
| 1003 | else { |
| 1004 | mask->s_addr = 0xFFFFFFFF; |
| 1005 | } |
| 1006 | if (!inet_pton(AF_INET, str, addr)) { |
| 1007 | struct hostent *he; |
| 1008 | |
| 1009 | if ((he = gethostbyname(str)) == NULL) { |
| 1010 | return 0; |
| 1011 | } |
| 1012 | else |
| 1013 | *addr = *(struct in_addr *) *(he->h_addr_list); |
| 1014 | } |
| 1015 | free(str); |
| 1016 | return 1; |
| 1017 | } |
| 1018 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1019 | |
| 1020 | /* |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1021 | * converts <str> to a list of listeners which are dynamically allocated. |
| 1022 | * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where : |
| 1023 | * - <addr> can be empty or "*" to indicate INADDR_ANY ; |
| 1024 | * - <port> is a numerical port from 1 to 65535 ; |
| 1025 | * - <end> indicates to use the range from <port> to <end> instead (inclusive). |
| 1026 | * This can be repeated as many times as necessary, separated by a coma. |
| 1027 | * The <tail> argument is a pointer to a current list which should be appended |
| 1028 | * to the tail of the new list. The pointer to the new list is returned. |
| 1029 | */ |
| 1030 | struct listener *str2listener(char *str, struct listener *tail) { |
| 1031 | struct listener *l; |
| 1032 | char *c, *next, *range, *dupstr; |
| 1033 | int port, end; |
| 1034 | |
| 1035 | next = dupstr = strdup(str); |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1036 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1037 | while (next && *next) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1038 | struct sockaddr_storage ss; |
| 1039 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1040 | str = next; |
| 1041 | /* 1) look for the end of the first address */ |
| 1042 | if ((next = strrchr(str, ',')) != NULL) { |
| 1043 | *next++ = 0; |
| 1044 | } |
| 1045 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1046 | /* 2) look for the addr/port delimiter, it's the last colon. */ |
| 1047 | if ((range = strrchr(str, ':')) == NULL) { |
| 1048 | Alert("Missing port number: '%s'\n", str); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1049 | goto fail; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | *range++ = 0; |
| 1053 | |
| 1054 | if (strrchr(str, ':') != NULL) { |
| 1055 | /* IPv6 address contains ':' */ |
| 1056 | memset(&ss, 0, sizeof(ss)); |
| 1057 | ss.ss_family = AF_INET6; |
| 1058 | |
| 1059 | if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in6 *)&ss)->sin6_addr)) { |
| 1060 | Alert("Invalid server address: '%s'\n", str); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1061 | goto fail; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1062 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1063 | } |
| 1064 | else { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1065 | memset(&ss, 0, sizeof(ss)); |
| 1066 | ss.ss_family = AF_INET; |
| 1067 | |
| 1068 | if (*str == '*' || *str == '\0') { /* INADDR_ANY */ |
| 1069 | ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; |
| 1070 | } |
| 1071 | else if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in *)&ss)->sin_addr)) { |
| 1072 | struct hostent *he; |
| 1073 | |
| 1074 | if ((he = gethostbyname(str)) == NULL) { |
| 1075 | Alert("Invalid server name: '%s'\n", str); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1076 | goto fail; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1077 | } |
| 1078 | else |
| 1079 | ((struct sockaddr_in *)&ss)->sin_addr = |
| 1080 | *(struct in_addr *) *(he->h_addr_list); |
| 1081 | } |
| 1082 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1083 | |
| 1084 | /* 3) look for the port-end delimiter */ |
| 1085 | if ((c = strchr(range, '-')) != NULL) { |
| 1086 | *c++ = 0; |
| 1087 | end = atol(c); |
| 1088 | } |
| 1089 | else { |
| 1090 | end = atol(range); |
| 1091 | } |
| 1092 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1093 | port = atol(range); |
| 1094 | |
| 1095 | if (port < 1 || port > 65535) { |
| 1096 | Alert("Invalid port '%d' specified for address '%s'.\n", port, str); |
| 1097 | goto fail; |
| 1098 | } |
| 1099 | |
| 1100 | if (end < 1 || end > 65535) { |
| 1101 | Alert("Invalid port '%d' specified for address '%s'.\n", end, str); |
| 1102 | goto fail; |
| 1103 | } |
| 1104 | |
| 1105 | for (; port <= end; port++) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1106 | l = (struct listener *)calloc(1, sizeof(struct listener)); |
| 1107 | l->next = tail; |
| 1108 | tail = l; |
| 1109 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1110 | l->addr = ss; |
| 1111 | if (ss.ss_family == AF_INET6) |
| 1112 | ((struct sockaddr_in6 *)(&l->addr))->sin6_port = htons(port); |
| 1113 | else |
| 1114 | ((struct sockaddr_in *)(&l->addr))->sin_port = htons(port); |
| 1115 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1116 | } /* end for(port) */ |
| 1117 | } /* end while(next) */ |
| 1118 | free(dupstr); |
| 1119 | return tail; |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1120 | fail: |
| 1121 | free(dupstr); |
| 1122 | return NULL; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1123 | } |
| 1124 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1125 | |
| 1126 | #define FD_SETS_ARE_BITFIELDS |
| 1127 | #ifdef FD_SETS_ARE_BITFIELDS |
| 1128 | /* |
| 1129 | * This map is used with all the FD_* macros to check whether a particular bit |
| 1130 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 1131 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 1132 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 1133 | * exclusively to the macros. |
| 1134 | */ |
| 1135 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 1136 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 1137 | |
| 1138 | #else |
| 1139 | #error "Check if your OS uses bitfields for fd_sets" |
| 1140 | #endif |
| 1141 | |
| 1142 | /* will try to encode the string <string> replacing all characters tagged in |
| 1143 | * <map> with the hexadecimal representation of their ASCII-code (2 digits) |
| 1144 | * prefixed by <escape>, and will store the result between <start> (included |
| 1145 | *) and <stop> (excluded), and will always terminate the string with a '\0' |
| 1146 | * before <stop>. The position of the '\0' is returned if the conversion |
| 1147 | * completes. If bytes are missing between <start> and <stop>, then the |
| 1148 | * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0' |
| 1149 | * cannot even be stored so we return <start> without writing the 0. |
| 1150 | * The input string must also be zero-terminated. |
| 1151 | */ |
| 1152 | char hextab[16] = "0123456789ABCDEF"; |
| 1153 | char *encode_string(char *start, char *stop, |
| 1154 | const char escape, const fd_set *map, |
| 1155 | const char *string) |
| 1156 | { |
| 1157 | if (start < stop) { |
| 1158 | stop--; /* reserve one byte for the final '\0' */ |
| 1159 | while (start < stop && *string != 0) { |
| 1160 | if (!FD_ISSET((unsigned char)(*string), map)) |
| 1161 | *start++ = *string; |
| 1162 | else { |
| 1163 | if (start + 3 >= stop) |
| 1164 | break; |
| 1165 | *start++ = escape; |
| 1166 | *start++ = hextab[(*string >> 4) & 15]; |
| 1167 | *start++ = hextab[*string & 15]; |
| 1168 | } |
| 1169 | string++; |
| 1170 | } |
| 1171 | *start = '\0'; |
| 1172 | } |
| 1173 | return start; |
| 1174 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1175 | |
| 1176 | /* |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1177 | * This function sends a syslog message to both log servers of a proxy, |
| 1178 | * or to global log servers if the proxy is NULL. |
| 1179 | * It also tries not to waste too much time computing the message header. |
| 1180 | * It doesn't care about errors nor does it report them. |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1181 | */ |
| 1182 | void send_log(struct proxy *p, int level, char *message, ...) { |
| 1183 | static int logfd = -1; /* syslog UDP socket */ |
| 1184 | static long tvsec = -1; /* to force the string to be initialized */ |
| 1185 | struct timeval tv; |
| 1186 | va_list argp; |
| 1187 | static char logmsg[MAX_SYSLOG_LEN]; |
| 1188 | static char *dataptr = NULL; |
| 1189 | int fac_level; |
| 1190 | int hdr_len, data_len; |
| 1191 | struct sockaddr_in *sa[2]; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1192 | int facilities[2], loglevel[2]; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1193 | int nbloggers = 0; |
| 1194 | char *log_ptr; |
| 1195 | |
| 1196 | if (logfd < 0) { |
| 1197 | if ((logfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) |
| 1198 | return; |
| 1199 | } |
| 1200 | |
| 1201 | if (level < 0 || progname == NULL || message == NULL) |
| 1202 | return; |
| 1203 | |
| 1204 | gettimeofday(&tv, NULL); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1205 | if (tv.tv_sec != tvsec || dataptr == NULL) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1206 | /* this string is rebuild only once a second */ |
| 1207 | struct tm *tm = localtime(&tv.tv_sec); |
| 1208 | tvsec = tv.tv_sec; |
| 1209 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1210 | hdr_len = snprintf(logmsg, sizeof(logmsg), |
| 1211 | "<<<<>%s %2d %02d:%02d:%02d %s[%d]: ", |
| 1212 | monthname[tm->tm_mon], |
| 1213 | tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 1214 | progname, pid); |
| 1215 | /* WARNING: depending upon implementations, snprintf may return |
| 1216 | * either -1 or the number of bytes that would be needed to store |
| 1217 | * the total message. In both cases, we must adjust it. |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1218 | */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1219 | if (hdr_len < 0 || hdr_len > sizeof(logmsg)) |
| 1220 | hdr_len = sizeof(logmsg); |
| 1221 | |
| 1222 | dataptr = logmsg + hdr_len; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | va_start(argp, message); |
| 1226 | data_len = vsnprintf(dataptr, logmsg + sizeof(logmsg) - dataptr, message, argp); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1227 | if (data_len < 0 || data_len > (logmsg + sizeof(logmsg) - dataptr)) |
| 1228 | data_len = logmsg + sizeof(logmsg) - dataptr; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1229 | va_end(argp); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1230 | dataptr[data_len - 1] = '\n'; /* force a break on ultra-long lines */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1231 | |
| 1232 | if (p == NULL) { |
| 1233 | if (global.logfac1 >= 0) { |
| 1234 | sa[nbloggers] = &global.logsrv1; |
| 1235 | facilities[nbloggers] = global.logfac1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1236 | loglevel[nbloggers] = global.loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1237 | nbloggers++; |
| 1238 | } |
| 1239 | if (global.logfac2 >= 0) { |
| 1240 | sa[nbloggers] = &global.logsrv2; |
| 1241 | facilities[nbloggers] = global.logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1242 | loglevel[nbloggers] = global.loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1243 | nbloggers++; |
| 1244 | } |
| 1245 | } else { |
| 1246 | if (p->logfac1 >= 0) { |
| 1247 | sa[nbloggers] = &p->logsrv1; |
| 1248 | facilities[nbloggers] = p->logfac1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1249 | loglevel[nbloggers] = p->loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1250 | nbloggers++; |
| 1251 | } |
| 1252 | if (p->logfac2 >= 0) { |
| 1253 | sa[nbloggers] = &p->logsrv2; |
| 1254 | facilities[nbloggers] = p->logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1255 | loglevel[nbloggers] = p->loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1256 | nbloggers++; |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | while (nbloggers-- > 0) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1261 | /* we can filter the level of the messages that are sent to each logger */ |
| 1262 | if (level > loglevel[nbloggers]) |
| 1263 | continue; |
| 1264 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1265 | /* For each target, we may have a different facility. |
| 1266 | * We can also have a different log level for each message. |
| 1267 | * This induces variations in the message header length. |
| 1268 | * Since we don't want to recompute it each time, nor copy it every |
| 1269 | * time, we only change the facility in the pre-computed header, |
| 1270 | * and we change the pointer to the header accordingly. |
| 1271 | */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1272 | fac_level = (facilities[nbloggers] << 3) + level; |
| 1273 | log_ptr = logmsg + 3; /* last digit of the log level */ |
| 1274 | do { |
| 1275 | *log_ptr = '0' + fac_level % 10; |
| 1276 | fac_level /= 10; |
| 1277 | log_ptr--; |
| 1278 | } while (fac_level && log_ptr > logmsg); |
| 1279 | *log_ptr = '<'; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1280 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1281 | /* 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] | 1282 | |
| 1283 | #ifndef MSG_NOSIGNAL |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1284 | sendto(logfd, log_ptr, dataptr + data_len - log_ptr, MSG_DONTWAIT, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1285 | (struct sockaddr *)sa[nbloggers], sizeof(**sa)); |
| 1286 | #else |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1287 | 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] | 1288 | (struct sockaddr *)sa[nbloggers], sizeof(**sa)); |
| 1289 | #endif |
| 1290 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | |
| 1294 | /* sets <tv> to the current time */ |
| 1295 | static inline struct timeval *tv_now(struct timeval *tv) { |
| 1296 | if (tv) |
| 1297 | gettimeofday(tv, NULL); |
| 1298 | return tv; |
| 1299 | } |
| 1300 | |
| 1301 | /* |
| 1302 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 1303 | */ |
| 1304 | static inline struct timeval *tv_delayfrom(struct timeval *tv, struct timeval *from, int ms) { |
| 1305 | if (!tv || !from) |
| 1306 | return NULL; |
| 1307 | tv->tv_usec = from->tv_usec + (ms%1000)*1000; |
| 1308 | tv->tv_sec = from->tv_sec + (ms/1000); |
| 1309 | while (tv->tv_usec >= 1000000) { |
| 1310 | tv->tv_usec -= 1000000; |
| 1311 | tv->tv_sec++; |
| 1312 | } |
| 1313 | return tv; |
| 1314 | } |
| 1315 | |
| 1316 | /* |
| 1317 | * 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] | 1318 | * 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] | 1319 | */ |
| 1320 | static inline int tv_cmp(struct timeval *tv1, struct timeval *tv2) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1321 | if (tv1->tv_sec < tv2->tv_sec) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1322 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1323 | else if (tv1->tv_sec > tv2->tv_sec) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1324 | return 1; |
| 1325 | else if (tv1->tv_usec < tv2->tv_usec) |
| 1326 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1327 | else if (tv1->tv_usec > tv2->tv_usec) |
| 1328 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1329 | else |
| 1330 | return 0; |
| 1331 | } |
| 1332 | |
| 1333 | /* |
| 1334 | * returns the absolute difference, in ms, between tv1 and tv2 |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1335 | * Must not be used when either argument is eternity. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1336 | */ |
| 1337 | unsigned long tv_delta(struct timeval *tv1, struct timeval *tv2) { |
| 1338 | int cmp; |
| 1339 | unsigned long ret; |
| 1340 | |
| 1341 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1342 | cmp = tv_cmp(tv1, tv2); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1343 | if (!cmp) |
| 1344 | return 0; /* same dates, null diff */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1345 | else if (cmp < 0) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1346 | struct timeval *tmp = tv1; |
| 1347 | tv1 = tv2; |
| 1348 | tv2 = tmp; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1349 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1350 | ret = (tv1->tv_sec - tv2->tv_sec) * 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1351 | if (tv1->tv_usec > tv2->tv_usec) |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1352 | ret += (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1353 | else |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1354 | ret -= (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1355 | return (unsigned long) ret; |
| 1356 | } |
| 1357 | |
| 1358 | /* |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1359 | * returns the difference, in ms, between tv1 and tv2 |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1360 | * Must not be used when either argument is eternity. |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1361 | */ |
| 1362 | static inline unsigned long tv_diff(struct timeval *tv1, struct timeval *tv2) { |
| 1363 | unsigned long ret; |
| 1364 | |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 1365 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
| 1366 | if (tv2->tv_usec > tv1->tv_usec) |
| 1367 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1368 | else |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 1369 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1370 | return (unsigned long) ret; |
| 1371 | } |
| 1372 | |
| 1373 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1374 | * 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] | 1375 | * 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] | 1376 | */ |
| 1377 | static inline int tv_cmp_ms(struct timeval *tv1, struct timeval *tv2) { |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1378 | if (tv1->tv_sec == tv2->tv_sec) { |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1379 | if (tv2->tv_usec >= tv1->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1380 | return -1; |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1381 | else if (tv1->tv_usec >= tv2->tv_usec + 1000) |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1382 | return 1; |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1383 | else |
| 1384 | return 0; |
| 1385 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1386 | else if ((tv2->tv_sec > tv1->tv_sec + 1) || |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1387 | ((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] | 1388 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1389 | else if ((tv1->tv_sec > tv2->tv_sec + 1) || |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1390 | ((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] | 1391 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1392 | else |
| 1393 | return 0; |
| 1394 | } |
| 1395 | |
| 1396 | /* |
| 1397 | * returns the remaining time between tv1=now and event=tv2 |
| 1398 | * if tv2 is passed, 0 is returned. |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1399 | * Must not be used when either argument is eternity. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1400 | */ |
| 1401 | static inline unsigned long tv_remain(struct timeval *tv1, struct timeval *tv2) { |
| 1402 | unsigned long ret; |
| 1403 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1404 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 1405 | return 0; /* event elapsed */ |
| 1406 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1407 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1408 | if (tv2->tv_usec > tv1->tv_usec) |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1409 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1410 | else |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1411 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1412 | return (unsigned long) ret; |
| 1413 | } |
| 1414 | |
| 1415 | |
| 1416 | /* |
| 1417 | * zeroes a struct timeval |
| 1418 | */ |
| 1419 | |
| 1420 | static inline struct timeval *tv_eternity(struct timeval *tv) { |
| 1421 | tv->tv_sec = tv->tv_usec = 0; |
| 1422 | return tv; |
| 1423 | } |
| 1424 | |
| 1425 | /* |
| 1426 | * returns 1 if tv is null, else 0 |
| 1427 | */ |
| 1428 | static inline int tv_iseternity(struct timeval *tv) { |
| 1429 | if (tv->tv_sec == 0 && tv->tv_usec == 0) |
| 1430 | return 1; |
| 1431 | else |
| 1432 | return 0; |
| 1433 | } |
| 1434 | |
| 1435 | /* |
| 1436 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 1437 | * considering that 0 is the eternity. |
| 1438 | */ |
| 1439 | static inline int tv_cmp2(struct timeval *tv1, struct timeval *tv2) { |
| 1440 | if (tv_iseternity(tv1)) |
| 1441 | if (tv_iseternity(tv2)) |
| 1442 | return 0; /* same */ |
| 1443 | else |
| 1444 | return 1; /* tv1 later than tv2 */ |
| 1445 | else if (tv_iseternity(tv2)) |
| 1446 | return -1; /* tv2 later than tv1 */ |
| 1447 | |
| 1448 | if (tv1->tv_sec > tv2->tv_sec) |
| 1449 | return 1; |
| 1450 | else if (tv1->tv_sec < tv2->tv_sec) |
| 1451 | return -1; |
| 1452 | else if (tv1->tv_usec > tv2->tv_usec) |
| 1453 | return 1; |
| 1454 | else if (tv1->tv_usec < tv2->tv_usec) |
| 1455 | return -1; |
| 1456 | else |
| 1457 | return 0; |
| 1458 | } |
| 1459 | |
| 1460 | /* |
| 1461 | * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 1462 | * considering that 0 is the eternity. |
| 1463 | */ |
| 1464 | static inline int tv_cmp2_ms(struct timeval *tv1, struct timeval *tv2) { |
| 1465 | if (tv_iseternity(tv1)) |
| 1466 | if (tv_iseternity(tv2)) |
| 1467 | return 0; /* same */ |
| 1468 | else |
| 1469 | return 1; /* tv1 later than tv2 */ |
| 1470 | else if (tv_iseternity(tv2)) |
| 1471 | return -1; /* tv2 later than tv1 */ |
| 1472 | |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1473 | if (tv1->tv_sec == tv2->tv_sec) { |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1474 | if (tv1->tv_usec >= tv2->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1475 | return 1; |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1476 | else if (tv2->tv_usec >= tv1->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1477 | return -1; |
| 1478 | else |
| 1479 | return 0; |
| 1480 | } |
| 1481 | else if ((tv1->tv_sec > tv2->tv_sec + 1) || |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1482 | ((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] | 1483 | return 1; |
| 1484 | else if ((tv2->tv_sec > tv1->tv_sec + 1) || |
Willy TARREAU | c9a6439 | 2006-03-01 22:30:20 +0100 | [diff] [blame] | 1485 | ((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] | 1486 | return -1; |
| 1487 | else |
| 1488 | return 0; |
| 1489 | } |
| 1490 | |
| 1491 | /* |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1492 | * returns the remaining time between tv1=now and event=tv2 |
| 1493 | * if tv2 is passed, 0 is returned. |
| 1494 | * Returns TIME_ETERNITY if tv2 is eternity. |
| 1495 | */ |
| 1496 | static inline unsigned long tv_remain2(struct timeval *tv1, struct timeval *tv2) { |
| 1497 | unsigned long ret; |
| 1498 | |
| 1499 | if (tv_iseternity(tv2)) |
| 1500 | return TIME_ETERNITY; |
| 1501 | |
| 1502 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 1503 | return 0; /* event elapsed */ |
| 1504 | |
| 1505 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
| 1506 | if (tv2->tv_usec > tv1->tv_usec) |
| 1507 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
| 1508 | else |
| 1509 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
| 1510 | return (unsigned long) ret; |
| 1511 | } |
| 1512 | |
| 1513 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1514 | * returns the first event between tv1 and tv2 into tvmin. |
| 1515 | * a zero tv is ignored. tvmin is returned. |
| 1516 | */ |
| 1517 | static inline struct timeval *tv_min(struct timeval *tvmin, |
| 1518 | struct timeval *tv1, struct timeval *tv2) { |
| 1519 | |
| 1520 | if (tv_cmp2(tv1, tv2) <= 0) |
| 1521 | *tvmin = *tv1; |
| 1522 | else |
| 1523 | *tvmin = *tv2; |
| 1524 | |
| 1525 | return tvmin; |
| 1526 | } |
| 1527 | |
| 1528 | |
| 1529 | |
| 1530 | /***********************************************************/ |
| 1531 | /* fd management ***************************************/ |
| 1532 | /***********************************************************/ |
| 1533 | |
| 1534 | |
| 1535 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1536 | /* Deletes an FD from the fdsets, and recomputes the maxfd limit. |
| 1537 | * The file descriptor is also closed. |
| 1538 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1539 | static inline void fd_delete(int fd) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1540 | FD_CLR(fd, StaticReadEvent); |
| 1541 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 08dedbe | 2005-12-18 01:13:48 +0100 | [diff] [blame] | 1542 | #if defined(ENABLE_EPOLL) |
| 1543 | if (PrevReadEvent) { |
| 1544 | FD_CLR(fd, PrevReadEvent); |
| 1545 | FD_CLR(fd, PrevWriteEvent); |
| 1546 | } |
| 1547 | #endif |
| 1548 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1549 | close(fd); |
| 1550 | fdtab[fd].state = FD_STCLOSE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1551 | |
| 1552 | while ((maxfd-1 >= 0) && (fdtab[maxfd-1].state == FD_STCLOSE)) |
| 1553 | maxfd--; |
| 1554 | } |
| 1555 | |
| 1556 | /* recomputes the maxfd limit from the fd */ |
| 1557 | static inline void fd_insert(int fd) { |
| 1558 | if (fd+1 > maxfd) |
| 1559 | maxfd = fd+1; |
| 1560 | } |
| 1561 | |
| 1562 | /*************************************************************/ |
| 1563 | /* task management ***************************************/ |
| 1564 | /*************************************************************/ |
| 1565 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1566 | /* puts the task <t> in run queue <q>, and returns <t> */ |
| 1567 | static inline struct task *task_wakeup(struct task **q, struct task *t) { |
| 1568 | if (t->state == TASK_RUNNING) |
| 1569 | return t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1570 | else { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1571 | t->rqnext = *q; |
| 1572 | t->state = TASK_RUNNING; |
| 1573 | return *q = t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1574 | } |
| 1575 | } |
| 1576 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1577 | /* removes the task <t> from the queue <q> |
| 1578 | * <s> MUST be <q>'s first task. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1579 | * set the run queue to point to the next one, and return it |
| 1580 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1581 | static inline struct task *task_sleep(struct task **q, struct task *t) { |
| 1582 | if (t->state == TASK_RUNNING) { |
| 1583 | *q = t->rqnext; |
| 1584 | t->state = TASK_IDLE; /* tell that s has left the run queue */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1585 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1586 | return *q; /* return next running task */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1590 | * 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] | 1591 | * from the run queue. A pointer to the task itself is returned. |
| 1592 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1593 | static inline struct task *task_delete(struct task *t) { |
| 1594 | t->prev->next = t->next; |
| 1595 | t->next->prev = t->prev; |
| 1596 | return t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1600 | * 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] | 1601 | */ |
| 1602 | static inline void task_free(struct task *t) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1603 | pool_free(task, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1604 | } |
| 1605 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1606 | /* 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] | 1607 | * may be only moved or left where it was, depending on its timing requirements. |
| 1608 | * <task> is returned. |
| 1609 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1610 | struct task *task_queue(struct task *task) { |
| 1611 | struct task *list = task->wq; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1612 | struct task *start_from; |
| 1613 | |
| 1614 | /* first, test if the task was already in a list */ |
| 1615 | if (task->prev == NULL) { |
| 1616 | // start_from = list; |
| 1617 | start_from = list->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1618 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1619 | stats_tsk_new++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1620 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1621 | /* insert the unlinked <task> into the list, searching back from the last entry */ |
| 1622 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1623 | start_from = start_from->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1624 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1625 | stats_tsk_nsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1626 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | // while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1630 | // start_from = start_from->next; |
| 1631 | // stats_tsk_nsrch++; |
| 1632 | // } |
| 1633 | } |
| 1634 | else if (task->prev == list || |
| 1635 | tv_cmp2(&task->expire, &task->prev->expire) >= 0) { /* walk right */ |
| 1636 | start_from = task->next; |
| 1637 | if (start_from == list || tv_cmp2(&task->expire, &start_from->expire) <= 0) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1638 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1639 | stats_tsk_good++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1640 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1641 | return task; /* it's already in the right place */ |
| 1642 | } |
| 1643 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1644 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1645 | stats_tsk_right++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1646 | #endif |
| 1647 | |
| 1648 | /* if the task is not at the right place, there's little chance that |
| 1649 | * it has only shifted a bit, and it will nearly always be queued |
| 1650 | * at the end of the list because of constant timeouts |
| 1651 | * (observed in real case). |
| 1652 | */ |
| 1653 | #ifndef WE_REALLY_THINK_THAT_THIS_TASK_MAY_HAVE_SHIFTED |
| 1654 | start_from = list->prev; /* assume we'll queue to the end of the list */ |
| 1655 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1656 | start_from = start_from->prev; |
| 1657 | #if STATTIME > 0 |
| 1658 | stats_tsk_lsrch++; |
| 1659 | #endif |
| 1660 | } |
| 1661 | #else /* WE_REALLY_... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1662 | /* insert the unlinked <task> into the list, searching after position <start_from> */ |
| 1663 | while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1664 | start_from = start_from->next; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1665 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1666 | stats_tsk_rsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1667 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1668 | } |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1669 | #endif /* WE_REALLY_... */ |
| 1670 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1671 | /* we need to unlink it now */ |
| 1672 | task_delete(task); |
| 1673 | } |
| 1674 | else { /* walk left. */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1675 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1676 | stats_tsk_left++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1677 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1678 | #ifdef LEFT_TO_TOP /* not very good */ |
| 1679 | start_from = list; |
| 1680 | while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1681 | start_from = start_from->next; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1682 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1683 | stats_tsk_lsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1684 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1685 | } |
| 1686 | #else |
| 1687 | start_from = task->prev->prev; /* valid because of the previous test above */ |
| 1688 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1689 | start_from = start_from->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1690 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1691 | stats_tsk_lsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1692 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1693 | } |
| 1694 | #endif |
| 1695 | /* we need to unlink it now */ |
| 1696 | task_delete(task); |
| 1697 | } |
| 1698 | task->prev = start_from; |
| 1699 | task->next = start_from->next; |
| 1700 | task->next->prev = task; |
| 1701 | start_from->next = task; |
| 1702 | return task; |
| 1703 | } |
| 1704 | |
| 1705 | |
| 1706 | /*********************************************************************/ |
| 1707 | /* more specific functions ***************************************/ |
| 1708 | /*********************************************************************/ |
| 1709 | |
| 1710 | /* some prototypes */ |
| 1711 | static int maintain_proxies(void); |
| 1712 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1713 | /* This either returns the sockname or the original destination address. Code |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1714 | * inspired from Patrick Schaaf's example of nf_getsockname() implementation. |
| 1715 | */ |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1716 | 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] | 1717 | #if defined(TPROXY) && defined(SO_ORIGINAL_DST) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1718 | return getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, (void *)sa, salen); |
| 1719 | #else |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1720 | #if defined(TPROXY) && defined(USE_GETSOCKNAME) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1721 | return getsockname(fd, (struct sockaddr *)sa, salen); |
| 1722 | #else |
| 1723 | return -1; |
| 1724 | #endif |
| 1725 | #endif |
| 1726 | } |
| 1727 | |
| 1728 | /* |
| 1729 | * frees the context associated to a session. It must have been removed first. |
| 1730 | */ |
| 1731 | static inline void session_free(struct session *s) { |
| 1732 | if (s->req) |
| 1733 | pool_free(buffer, s->req); |
| 1734 | if (s->rep) |
| 1735 | pool_free(buffer, s->rep); |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1736 | |
| 1737 | if (s->rsp_cap != NULL) { |
| 1738 | struct cap_hdr *h; |
| 1739 | for (h = s->proxy->rsp_cap; h; h = h->next) { |
| 1740 | if (s->rsp_cap[h->index] != NULL) |
| 1741 | pool_free_to(h->pool, s->rsp_cap[h->index]); |
| 1742 | } |
| 1743 | pool_free_to(s->proxy->rsp_cap_pool, s->rsp_cap); |
| 1744 | } |
| 1745 | if (s->req_cap != NULL) { |
| 1746 | struct cap_hdr *h; |
| 1747 | for (h = s->proxy->req_cap; h; h = h->next) { |
| 1748 | if (s->req_cap[h->index] != NULL) |
| 1749 | pool_free_to(h->pool, s->req_cap[h->index]); |
| 1750 | } |
| 1751 | pool_free_to(s->proxy->req_cap_pool, s->req_cap); |
| 1752 | } |
| 1753 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1754 | if (s->logs.uri) |
| 1755 | pool_free(requri, s->logs.uri); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1756 | if (s->logs.cli_cookie) |
| 1757 | pool_free(capture, s->logs.cli_cookie); |
| 1758 | if (s->logs.srv_cookie) |
| 1759 | pool_free(capture, s->logs.srv_cookie); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1760 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1761 | pool_free(session, s); |
| 1762 | } |
| 1763 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1764 | |
| 1765 | /* |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1766 | * This function tries to find a running server for the proxy <px>. A first |
| 1767 | * pass looks for active servers, and if none is found, a second pass also |
| 1768 | * looks for backup servers. |
| 1769 | * If no valid server is found, NULL is returned and px->cursrv is left undefined. |
| 1770 | */ |
| 1771 | static inline struct server *find_server(struct proxy *px) { |
| 1772 | struct server *srv = px->cursrv; |
| 1773 | int ignore_backup = 1; |
| 1774 | |
| 1775 | do { |
| 1776 | do { |
| 1777 | if (srv == NULL) |
| 1778 | srv = px->srv; |
| 1779 | if (srv->state & SRV_RUNNING |
| 1780 | && !((srv->state & SRV_BACKUP) && ignore_backup)) |
| 1781 | return srv; |
| 1782 | srv = srv->next; |
| 1783 | } while (srv != px->cursrv); |
Willy TARREAU | 3481c46 | 2006-03-01 22:37:57 +0100 | [diff] [blame] | 1784 | |
| 1785 | /* By default, we look for the first backup server if all others are |
| 1786 | * DOWN. But in some cases, it may be desirable to load-balance across |
| 1787 | * all backup servers. |
| 1788 | */ |
| 1789 | if (!(px->options & PR_O_USE_ALL_BK)) |
| 1790 | srv = px->srv; |
| 1791 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1792 | } while (ignore_backup--); |
| 1793 | return NULL; |
| 1794 | } |
| 1795 | |
| 1796 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1797 | * 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] | 1798 | * is set, or to the dispatch server if (s->direct) is 0. |
| 1799 | * It can return one of : |
| 1800 | * - SN_ERR_NONE if everything's OK |
| 1801 | * - SN_ERR_SRVTO if there are no more servers |
| 1802 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1803 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1804 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1805 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1806 | * 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] | 1807 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1808 | int connect_server(struct session *s) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1809 | int fd; |
| 1810 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 1811 | #ifdef DEBUG_FULL |
| 1812 | fprintf(stderr,"connect_server : s=%p\n",s); |
| 1813 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1814 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 1815 | if (s->flags & SN_DIRECT) { /* srv cannot be null */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1816 | s->srv_addr = s->srv->addr; |
| 1817 | } |
| 1818 | else if (s->proxy->options & PR_O_BALANCE) { |
| 1819 | if (s->proxy->options & PR_O_BALANCE_RR) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1820 | struct server *srv; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1821 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1822 | srv = find_server(s->proxy); |
| 1823 | |
| 1824 | if (srv == NULL) /* no server left */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1825 | return SN_ERR_SRVTO; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1826 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1827 | s->srv_addr = srv->addr; |
| 1828 | s->srv = srv; |
| 1829 | s->proxy->cursrv = srv->next; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1830 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1831 | else /* unknown balancing algorithm */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1832 | return SN_ERR_INTERNAL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1833 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1834 | else if (*(int *)&s->proxy->dispatch_addr.sin_addr) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1835 | /* connect to the defined dispatch addr */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1836 | s->srv_addr = s->proxy->dispatch_addr; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1837 | } |
| 1838 | else if (s->proxy->options & PR_O_TRANSP) { |
| 1839 | /* in transparent mode, use the original dest addr if no dispatch specified */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1840 | socklen_t salen = sizeof(s->srv_addr); |
| 1841 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1842 | if (get_original_dst(s->cli_fd, &s->srv_addr, &salen) == -1) { |
| 1843 | qfprintf(stderr, "Cannot get original server address.\n"); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1844 | return SN_ERR_INTERNAL; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1845 | } |
| 1846 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1847 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1848 | /* if this server remaps proxied ports, we'll use |
| 1849 | * the port the client connected to with an offset. */ |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1850 | if (s->srv != NULL && s->srv->state & SRV_MAPPORTS) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1851 | struct sockaddr_in sockname; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1852 | socklen_t namelen = sizeof(sockname); |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1853 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1854 | if (!(s->proxy->options & PR_O_TRANSP) || |
| 1855 | get_original_dst(s->cli_fd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1856 | getsockname(s->cli_fd, (struct sockaddr *)&sockname, &namelen); |
| 1857 | s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) + ntohs(sockname.sin_port)); |
| 1858 | } |
| 1859 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1860 | 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] | 1861 | qfprintf(stderr, "Cannot get a server socket.\n"); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1862 | |
| 1863 | if (errno == ENFILE) |
| 1864 | send_log(s->proxy, LOG_EMERG, |
| 1865 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 1866 | s->proxy->id, maxfd); |
| 1867 | else if (errno == EMFILE) |
| 1868 | send_log(s->proxy, LOG_EMERG, |
| 1869 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 1870 | s->proxy->id, maxfd); |
| 1871 | else if (errno == ENOBUFS || errno == ENOMEM) |
| 1872 | send_log(s->proxy, LOG_EMERG, |
| 1873 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 1874 | s->proxy->id, maxfd); |
| 1875 | /* this is a resource error */ |
| 1876 | return SN_ERR_RESOURCE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1877 | } |
| 1878 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1879 | if (fd >= global.maxsock) { |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1880 | /* do not log anything there, it's a normal condition when this option |
| 1881 | * is used to serialize connections to a server ! |
| 1882 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1883 | Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 1884 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1885 | return SN_ERR_PRXCOND; /* it is a configuration limit */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1886 | } |
| 1887 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1888 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
| 1889 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1890 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1891 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1892 | return SN_ERR_INTERNAL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1893 | } |
| 1894 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1895 | if (s->proxy->options & PR_O_TCP_SRV_KA) |
| 1896 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 1897 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1898 | /* allow specific binding : |
| 1899 | * - server-specific at first |
| 1900 | * - proxy-specific next |
| 1901 | */ |
| 1902 | if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) { |
| 1903 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 1904 | if (bind(fd, (struct sockaddr *)&s->srv->source_addr, sizeof(s->srv->source_addr)) == -1) { |
| 1905 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 1906 | s->proxy->id, s->srv->id); |
| 1907 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1908 | send_log(s->proxy, LOG_EMERG, |
| 1909 | "Cannot bind to source address before connect() for server %s/%s.\n", |
| 1910 | s->proxy->id, s->srv->id); |
| 1911 | return SN_ERR_RESOURCE; |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1912 | } |
| 1913 | } |
| 1914 | else if (s->proxy->options & PR_O_BIND_SRC) { |
| 1915 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 1916 | if (bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) { |
| 1917 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", s->proxy->id); |
| 1918 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1919 | send_log(s->proxy, LOG_EMERG, |
| 1920 | "Cannot bind to source address before connect() for server %s/%s.\n", |
| 1921 | s->proxy->id, s->srv->id); |
| 1922 | return SN_ERR_RESOURCE; |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1923 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1924 | } |
| 1925 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1926 | if ((connect(fd, (struct sockaddr *)&s->srv_addr, sizeof(s->srv_addr)) == -1) && |
| 1927 | (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) { |
| 1928 | |
| 1929 | if (errno == EAGAIN || errno == EADDRINUSE) { |
| 1930 | char *msg; |
| 1931 | if (errno == EAGAIN) /* no free ports left, try again later */ |
| 1932 | msg = "no free ports"; |
| 1933 | else |
| 1934 | msg = "local address already in use"; |
| 1935 | |
| 1936 | qfprintf(stderr,"Cannot connect: %s.\n",msg); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1937 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1938 | send_log(s->proxy, LOG_EMERG, |
| 1939 | "Connect() failed for server %s/%s: %s.\n", |
| 1940 | s->proxy->id, s->srv->id, msg); |
| 1941 | return SN_ERR_RESOURCE; |
| 1942 | } else if (errno == ETIMEDOUT) { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1943 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1944 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1945 | return SN_ERR_SRVTO; |
| 1946 | } else { |
| 1947 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1948 | //qfprintf(stderr,"Connect(): %d", errno); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1949 | close(fd); |
| 1950 | return SN_ERR_SRVCL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1951 | } |
| 1952 | } |
| 1953 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1954 | fdtab[fd].owner = s->task; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1955 | fdtab[fd].read = &event_srv_read; |
| 1956 | fdtab[fd].write = &event_srv_write; |
| 1957 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 1958 | |
| 1959 | FD_SET(fd, StaticWriteEvent); /* for connect status */ |
| 1960 | |
| 1961 | fd_insert(fd); |
| 1962 | |
| 1963 | if (s->proxy->contimeout) |
| 1964 | tv_delayfrom(&s->cnexpire, &now, s->proxy->contimeout); |
| 1965 | else |
| 1966 | tv_eternity(&s->cnexpire); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1967 | return SN_ERR_NONE; /* connection is OK */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | /* |
| 1971 | * this function is called on a read event from a client socket. |
| 1972 | * It returns 0. |
| 1973 | */ |
| 1974 | int event_cli_read(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1975 | struct task *t = fdtab[fd].owner; |
| 1976 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1977 | struct buffer *b = s->req; |
| 1978 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1979 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 1980 | #ifdef DEBUG_FULL |
| 1981 | fprintf(stderr,"event_cli_read : fd=%d, s=%p\n", fd, s); |
| 1982 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1983 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1984 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 1985 | #ifdef FILL_BUFFERS |
| 1986 | while (1) |
| 1987 | #else |
| 1988 | do |
| 1989 | #endif |
| 1990 | { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1991 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 1992 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1993 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1994 | } |
| 1995 | else if (b->r > b->w) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1996 | max = b->rlim - b->r; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1997 | } |
| 1998 | else { |
| 1999 | max = b->w - b->r; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2000 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 2001 | * since it means that the rewrite protection has been removed. This |
| 2002 | * implies that the if statement can be removed. |
| 2003 | */ |
| 2004 | if (max > b->rlim - b->data) |
| 2005 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | if (max == 0) { /* not anymore room to store data */ |
| 2009 | FD_CLR(fd, StaticReadEvent); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2010 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2011 | } |
| 2012 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2013 | #ifndef MSG_NOSIGNAL |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2014 | { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2015 | int skerr; |
| 2016 | socklen_t lskerr = sizeof(skerr); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2017 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2018 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2019 | if (skerr) |
| 2020 | ret = -1; |
| 2021 | else |
| 2022 | ret = recv(fd, b->r, max, 0); |
| 2023 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2024 | #else |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2025 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2026 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2027 | if (ret > 0) { |
| 2028 | b->r += ret; |
| 2029 | b->l += ret; |
| 2030 | s->res_cr = RES_DATA; |
| 2031 | |
| 2032 | if (b->r == b->data + BUFSIZE) { |
| 2033 | b->r = b->data; /* wrap around the buffer */ |
| 2034 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2035 | |
| 2036 | b->total += ret; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2037 | /* we hope to read more data or to get a close on next round */ |
| 2038 | continue; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2039 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2040 | else if (ret == 0) { |
| 2041 | s->res_cr = RES_NULL; |
| 2042 | break; |
| 2043 | } |
| 2044 | else if (errno == EAGAIN) {/* ignore EAGAIN */ |
| 2045 | break; |
| 2046 | } |
| 2047 | else { |
| 2048 | s->res_cr = RES_ERROR; |
| 2049 | fdtab[fd].state = FD_STERROR; |
| 2050 | break; |
| 2051 | } |
| 2052 | } /* while(1) */ |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2053 | #ifndef FILL_BUFFERS |
| 2054 | while (0); |
| 2055 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2056 | } |
| 2057 | else { |
| 2058 | s->res_cr = RES_ERROR; |
| 2059 | fdtab[fd].state = FD_STERROR; |
| 2060 | } |
| 2061 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2062 | if (s->res_cr != RES_SILENT) { |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2063 | if (s->proxy->clitimeout && FD_ISSET(fd, StaticReadEvent)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2064 | tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout); |
| 2065 | else |
| 2066 | tv_eternity(&s->crexpire); |
| 2067 | |
| 2068 | task_wakeup(&rq, t); |
| 2069 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2070 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2071 | return 0; |
| 2072 | } |
| 2073 | |
| 2074 | |
| 2075 | /* |
| 2076 | * this function is called on a read event from a server socket. |
| 2077 | * It returns 0. |
| 2078 | */ |
| 2079 | int event_srv_read(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2080 | struct task *t = fdtab[fd].owner; |
| 2081 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2082 | struct buffer *b = s->rep; |
| 2083 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2084 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2085 | #ifdef DEBUG_FULL |
| 2086 | fprintf(stderr,"event_srv_read : fd=%d, s=%p\n", fd, s); |
| 2087 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2088 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2089 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2090 | #ifdef FILL_BUFFERS |
| 2091 | while (1) |
| 2092 | #else |
| 2093 | do |
| 2094 | #endif |
| 2095 | { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2096 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 2097 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2098 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2099 | } |
| 2100 | else if (b->r > b->w) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2101 | max = b->rlim - b->r; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2102 | } |
| 2103 | else { |
| 2104 | max = b->w - b->r; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2105 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 2106 | * since it means that the rewrite protection has been removed. This |
| 2107 | * implies that the if statement can be removed. |
| 2108 | */ |
| 2109 | if (max > b->rlim - b->data) |
| 2110 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2111 | } |
| 2112 | |
| 2113 | if (max == 0) { /* not anymore room to store data */ |
| 2114 | FD_CLR(fd, StaticReadEvent); |
| 2115 | break; |
| 2116 | } |
| 2117 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2118 | #ifndef MSG_NOSIGNAL |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2119 | { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2120 | int skerr; |
| 2121 | socklen_t lskerr = sizeof(skerr); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2122 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2123 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2124 | if (skerr) |
| 2125 | ret = -1; |
| 2126 | else |
| 2127 | ret = recv(fd, b->r, max, 0); |
| 2128 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2129 | #else |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2130 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2131 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2132 | if (ret > 0) { |
| 2133 | b->r += ret; |
| 2134 | b->l += ret; |
| 2135 | s->res_sr = RES_DATA; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2136 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2137 | if (b->r == b->data + BUFSIZE) { |
| 2138 | b->r = b->data; /* wrap around the buffer */ |
| 2139 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2140 | |
| 2141 | b->total += ret; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2142 | /* we hope to read more data or to get a close on next round */ |
| 2143 | continue; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2144 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2145 | else if (ret == 0) { |
| 2146 | s->res_sr = RES_NULL; |
| 2147 | break; |
| 2148 | } |
| 2149 | else if (errno == EAGAIN) {/* ignore EAGAIN */ |
| 2150 | break; |
| 2151 | } |
| 2152 | else { |
| 2153 | s->res_sr = RES_ERROR; |
| 2154 | fdtab[fd].state = FD_STERROR; |
| 2155 | break; |
| 2156 | } |
| 2157 | } /* while(1) */ |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2158 | #ifndef FILL_BUFFERS |
| 2159 | while (0); |
| 2160 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2161 | } |
| 2162 | else { |
| 2163 | s->res_sr = RES_ERROR; |
| 2164 | fdtab[fd].state = FD_STERROR; |
| 2165 | } |
| 2166 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2167 | if (s->res_sr != RES_SILENT) { |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2168 | if (s->proxy->srvtimeout && FD_ISSET(fd, StaticReadEvent)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2169 | tv_delayfrom(&s->srexpire, &now, s->proxy->srvtimeout); |
| 2170 | else |
| 2171 | tv_eternity(&s->srexpire); |
| 2172 | |
| 2173 | task_wakeup(&rq, t); |
| 2174 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2175 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2176 | return 0; |
| 2177 | } |
| 2178 | |
| 2179 | /* |
| 2180 | * this function is called on a write event from a client socket. |
| 2181 | * It returns 0. |
| 2182 | */ |
| 2183 | int event_cli_write(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2184 | struct task *t = fdtab[fd].owner; |
| 2185 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2186 | struct buffer *b = s->rep; |
| 2187 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2188 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2189 | #ifdef DEBUG_FULL |
| 2190 | fprintf(stderr,"event_cli_write : fd=%d, s=%p\n", fd, s); |
| 2191 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2192 | |
| 2193 | 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] | 2194 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2195 | // max = BUFSIZE; BUG !!!! |
| 2196 | max = 0; |
| 2197 | } |
| 2198 | else if (b->r > b->w) { |
| 2199 | max = b->r - b->w; |
| 2200 | } |
| 2201 | else |
| 2202 | max = b->data + BUFSIZE - b->w; |
| 2203 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2204 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2205 | if (max == 0) { |
| 2206 | s->res_cw = RES_NULL; |
| 2207 | task_wakeup(&rq, t); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2208 | tv_eternity(&s->cwexpire); |
| 2209 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2210 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2211 | } |
| 2212 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2213 | #ifndef MSG_NOSIGNAL |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2214 | { |
| 2215 | int skerr; |
| 2216 | socklen_t lskerr = sizeof(skerr); |
| 2217 | |
| 2218 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2219 | if (skerr) |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2220 | ret = -1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2221 | else |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2222 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2223 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2224 | #else |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2225 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2226 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2227 | |
| 2228 | if (ret > 0) { |
| 2229 | b->l -= ret; |
| 2230 | b->w += ret; |
| 2231 | |
| 2232 | s->res_cw = RES_DATA; |
| 2233 | |
| 2234 | if (b->w == b->data + BUFSIZE) { |
| 2235 | b->w = b->data; /* wrap around the buffer */ |
| 2236 | } |
| 2237 | } |
| 2238 | else if (ret == 0) { |
| 2239 | /* nothing written, just make as if we were never called */ |
| 2240 | // s->res_cw = RES_NULL; |
| 2241 | return 0; |
| 2242 | } |
| 2243 | else if (errno == EAGAIN) /* ignore EAGAIN */ |
| 2244 | return 0; |
| 2245 | else { |
| 2246 | s->res_cw = RES_ERROR; |
| 2247 | fdtab[fd].state = FD_STERROR; |
| 2248 | } |
| 2249 | } |
| 2250 | else { |
| 2251 | s->res_cw = RES_ERROR; |
| 2252 | fdtab[fd].state = FD_STERROR; |
| 2253 | } |
| 2254 | |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2255 | if (s->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2256 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2257 | /* FIXME: to avoid the client to read-time-out during writes, we refresh it */ |
| 2258 | s->crexpire = s->cwexpire; |
| 2259 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2260 | else |
| 2261 | tv_eternity(&s->cwexpire); |
| 2262 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2263 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2264 | return 0; |
| 2265 | } |
| 2266 | |
| 2267 | |
| 2268 | /* |
| 2269 | * this function is called on a write event from a server socket. |
| 2270 | * It returns 0. |
| 2271 | */ |
| 2272 | int event_srv_write(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2273 | struct task *t = fdtab[fd].owner; |
| 2274 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2275 | struct buffer *b = s->req; |
| 2276 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2277 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2278 | #ifdef DEBUG_FULL |
| 2279 | fprintf(stderr,"event_srv_write : fd=%d, s=%p\n", fd, s); |
| 2280 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2281 | |
| 2282 | 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] | 2283 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2284 | // max = BUFSIZE; BUG !!!! |
| 2285 | max = 0; |
| 2286 | } |
| 2287 | else if (b->r > b->w) { |
| 2288 | max = b->r - b->w; |
| 2289 | } |
| 2290 | else |
| 2291 | max = b->data + BUFSIZE - b->w; |
| 2292 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2293 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2294 | if (max == 0) { |
| 2295 | /* 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] | 2296 | if (s->srv_state == SV_STCONN) { |
| 2297 | int skerr; |
| 2298 | socklen_t lskerr = sizeof(skerr); |
| 2299 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2300 | if (skerr) { |
| 2301 | s->res_sw = RES_ERROR; |
| 2302 | fdtab[fd].state = FD_STERROR; |
| 2303 | task_wakeup(&rq, t); |
| 2304 | tv_eternity(&s->swexpire); |
| 2305 | FD_CLR(fd, StaticWriteEvent); |
| 2306 | return 0; |
| 2307 | } |
| 2308 | } |
| 2309 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2310 | s->res_sw = RES_NULL; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2311 | task_wakeup(&rq, t); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2312 | fdtab[fd].state = FD_STREADY; |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2313 | tv_eternity(&s->swexpire); |
| 2314 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2315 | return 0; |
| 2316 | } |
| 2317 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2318 | #ifndef MSG_NOSIGNAL |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2319 | { |
| 2320 | int skerr; |
| 2321 | socklen_t lskerr = sizeof(skerr); |
| 2322 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2323 | if (skerr) |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2324 | ret = -1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2325 | else |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2326 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2327 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2328 | #else |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2329 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2330 | #endif |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2331 | fdtab[fd].state = FD_STREADY; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2332 | if (ret > 0) { |
| 2333 | b->l -= ret; |
| 2334 | b->w += ret; |
| 2335 | |
| 2336 | s->res_sw = RES_DATA; |
| 2337 | |
| 2338 | if (b->w == b->data + BUFSIZE) { |
| 2339 | b->w = b->data; /* wrap around the buffer */ |
| 2340 | } |
| 2341 | } |
| 2342 | else if (ret == 0) { |
| 2343 | /* nothing written, just make as if we were never called */ |
| 2344 | // s->res_sw = RES_NULL; |
| 2345 | return 0; |
| 2346 | } |
| 2347 | else if (errno == EAGAIN) /* ignore EAGAIN */ |
| 2348 | return 0; |
| 2349 | else { |
| 2350 | s->res_sw = RES_ERROR; |
| 2351 | fdtab[fd].state = FD_STERROR; |
| 2352 | } |
| 2353 | } |
| 2354 | else { |
| 2355 | s->res_sw = RES_ERROR; |
| 2356 | fdtab[fd].state = FD_STERROR; |
| 2357 | } |
| 2358 | |
Willy TARREAU | 1cec83c | 2006-03-01 22:33:49 +0100 | [diff] [blame] | 2359 | /* We don't want to re-arm read/write timeouts if we're trying to connect, |
| 2360 | * otherwise it could loop indefinitely ! |
| 2361 | */ |
| 2362 | if (s->srv_state != SV_STCONN) { |
| 2363 | if (s->proxy->srvtimeout) { |
| 2364 | tv_delayfrom(&s->swexpire, &now, s->proxy->srvtimeout); |
| 2365 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 2366 | s->srexpire = s->swexpire; |
| 2367 | } |
| 2368 | else |
| 2369 | tv_eternity(&s->swexpire); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2370 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2371 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2372 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2373 | return 0; |
| 2374 | } |
| 2375 | |
| 2376 | |
| 2377 | /* |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2378 | * returns a message to the client ; the connection is shut down for read, |
| 2379 | * and the request is cleared so that no server connection can be initiated. |
| 2380 | * The client must be in a valid state for this (HEADER, DATA ...). |
| 2381 | * Nothing is performed on the server side. |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2382 | * The reply buffer doesn't need to be empty before this. |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2383 | */ |
| 2384 | void client_retnclose(struct session *s, int len, const char *msg) { |
| 2385 | FD_CLR(s->cli_fd, StaticReadEvent); |
| 2386 | FD_SET(s->cli_fd, StaticWriteEvent); |
| 2387 | tv_eternity(&s->crexpire); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2388 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2389 | shutdown(s->cli_fd, SHUT_RD); |
| 2390 | s->cli_state = CL_STSHUTR; |
| 2391 | strcpy(s->rep->data, msg); |
| 2392 | s->rep->l = len; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2393 | 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] | 2394 | s->rep->r += len; |
| 2395 | s->req->l = 0; |
| 2396 | } |
| 2397 | |
| 2398 | |
| 2399 | /* |
| 2400 | * returns a message into the rep buffer, and flushes the req buffer. |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2401 | * The reply buffer doesn't need to be empty before this. |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2402 | */ |
| 2403 | void client_return(struct session *s, int len, const char *msg) { |
| 2404 | strcpy(s->rep->data, msg); |
| 2405 | s->rep->l = len; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2406 | 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] | 2407 | s->rep->r += len; |
| 2408 | s->req->l = 0; |
| 2409 | } |
| 2410 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2411 | /* |
| 2412 | * send a log for the session when we have enough info about it |
| 2413 | */ |
| 2414 | void sess_log(struct session *s) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2415 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2416 | struct proxy *p = s->proxy; |
| 2417 | int log; |
| 2418 | char *uri; |
| 2419 | char *pxid; |
| 2420 | char *srv; |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2421 | struct tm *tm; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2422 | |
| 2423 | /* This is a first attempt at a better logging system. |
| 2424 | * For now, we rely on send_log() to provide the date, although it obviously |
| 2425 | * is the date of the log and not of the request, and most fields are not |
| 2426 | * computed. |
| 2427 | */ |
| 2428 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2429 | log = p->to_log & ~s->logs.logwait; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2430 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2431 | if (s->cli_addr.ss_family == AF_INET) |
| 2432 | inet_ntop(AF_INET, |
| 2433 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 2434 | pn, sizeof(pn)); |
| 2435 | else |
| 2436 | inet_ntop(AF_INET6, |
| 2437 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 2438 | pn, sizeof(pn)); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2439 | |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2440 | uri = (log & LW_REQ) ? s->logs.uri ? s->logs.uri : "<BADREQ>" : ""; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2441 | pxid = p->id; |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2442 | 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] | 2443 | |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2444 | tm = localtime(&s->logs.tv_accept.tv_sec); |
| 2445 | if (p->to_log & LW_REQ) { |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2446 | char tmpline[MAX_SYSLOG_LEN], *h; |
| 2447 | int hdr; |
| 2448 | |
| 2449 | h = tmpline; |
| 2450 | if (p->to_log & LW_REQHDR && (h < tmpline + sizeof(tmpline) - 10)) { |
| 2451 | *(h++) = ' '; |
| 2452 | *(h++) = '{'; |
| 2453 | for (hdr = 0; hdr < p->nb_req_cap; hdr++) { |
| 2454 | if (hdr) |
| 2455 | *(h++) = '|'; |
| 2456 | if (s->req_cap[hdr] != NULL) |
| 2457 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, '#', hdr_encode_map, s->req_cap[hdr]); |
| 2458 | } |
| 2459 | *(h++) = '}'; |
| 2460 | } |
| 2461 | |
| 2462 | if (p->to_log & LW_RSPHDR && (h < tmpline + sizeof(tmpline) - 7)) { |
| 2463 | *(h++) = ' '; |
| 2464 | *(h++) = '{'; |
| 2465 | for (hdr = 0; hdr < p->nb_rsp_cap; hdr++) { |
| 2466 | if (hdr) |
| 2467 | *(h++) = '|'; |
| 2468 | if (s->rsp_cap[hdr] != NULL) |
| 2469 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, '#', hdr_encode_map, s->rsp_cap[hdr]); |
| 2470 | } |
| 2471 | *(h++) = '}'; |
| 2472 | } |
| 2473 | |
| 2474 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 2475 | *(h++) = ' '; |
| 2476 | *(h++) = '"'; |
| 2477 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, '#', url_encode_map, uri); |
| 2478 | *(h++) = '"'; |
| 2479 | } |
| 2480 | *h = '\0'; |
| 2481 | |
willy tarreau | 0fe3965 | 2005-12-18 01:25:24 +0100 | [diff] [blame] | 2482 | 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] | 2483 | pn, |
| 2484 | (s->cli_addr.ss_family == AF_INET) ? |
| 2485 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 2486 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2487 | tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900, |
| 2488 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 2489 | pxid, srv, |
| 2490 | s->logs.t_request, |
| 2491 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_request : -1, |
| 2492 | (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] | 2493 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 2494 | s->logs.status, |
| 2495 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.bytes, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2496 | s->logs.cli_cookie ? s->logs.cli_cookie : "-", |
| 2497 | s->logs.srv_cookie ? s->logs.srv_cookie : "-", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2498 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 2499 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 2500 | (p->options & PR_O_COOK_ANY) ? sess_cookie[(s->flags & SN_CK_MASK) >> SN_CK_SHIFT] : '-', |
| 2501 | (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] | 2502 | p->nbconn, actconn, tmpline); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2503 | } |
| 2504 | else { |
willy tarreau | 0fe3965 | 2005-12-18 01:25:24 +0100 | [diff] [blame] | 2505 | 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] | 2506 | pn, |
| 2507 | (s->cli_addr.ss_family == AF_INET) ? |
| 2508 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 2509 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2510 | tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900, |
| 2511 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2512 | pxid, srv, |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2513 | (s->logs.t_connect >= 0) ? s->logs.t_connect : -1, |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 2514 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 2515 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.bytes, |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2516 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
willy tarreau | 0fe3965 | 2005-12-18 01:25:24 +0100 | [diff] [blame] | 2517 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 2518 | p->nbconn, actconn); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2519 | } |
| 2520 | |
| 2521 | s->logs.logwait = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2522 | } |
| 2523 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2524 | |
| 2525 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2526 | * 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] | 2527 | * to an accept. It tries to accept as many connections as possible. |
| 2528 | * It returns 0. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2529 | */ |
| 2530 | int event_accept(int fd) { |
| 2531 | struct proxy *p = (struct proxy *)fdtab[fd].owner; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2532 | struct session *s; |
| 2533 | struct task *t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2534 | int cfd; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2535 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2536 | while (p->nbconn < p->maxconn) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2537 | struct sockaddr_storage addr; |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2538 | socklen_t laddr = sizeof(addr); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2539 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2540 | if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) { |
| 2541 | switch (errno) { |
| 2542 | case EAGAIN: |
| 2543 | case EINTR: |
| 2544 | case ECONNABORTED: |
| 2545 | return 0; /* nothing more to accept */ |
| 2546 | case ENFILE: |
| 2547 | send_log(p, LOG_EMERG, |
| 2548 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 2549 | p->id, maxfd); |
| 2550 | return 0; |
| 2551 | case EMFILE: |
| 2552 | send_log(p, LOG_EMERG, |
| 2553 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 2554 | p->id, maxfd); |
| 2555 | return 0; |
| 2556 | case ENOBUFS: |
| 2557 | case ENOMEM: |
| 2558 | send_log(p, LOG_EMERG, |
| 2559 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 2560 | p->id, maxfd); |
| 2561 | return 0; |
| 2562 | default: |
| 2563 | return 0; |
| 2564 | } |
| 2565 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2566 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2567 | if ((s = pool_alloc(session)) == NULL) { /* disable this proxy for a while */ |
| 2568 | Alert("out of memory in event_accept().\n"); |
| 2569 | FD_CLR(fd, StaticReadEvent); |
| 2570 | p->state = PR_STIDLE; |
| 2571 | close(cfd); |
| 2572 | return 0; |
| 2573 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2574 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2575 | /* if this session comes from a known monitoring system, we want to ignore |
| 2576 | * it as soon as possible, which means closing it immediately for TCP. |
| 2577 | */ |
| 2578 | s->flags = 0; |
| 2579 | if (addr.ss_family == AF_INET && |
| 2580 | p->mon_mask.s_addr && |
| 2581 | (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr) { |
| 2582 | if (p->mode == PR_MODE_TCP) { |
| 2583 | close(cfd); |
| 2584 | pool_free(session, s); |
| 2585 | continue; |
| 2586 | } |
| 2587 | s->flags |= SN_MONITOR; |
| 2588 | } |
| 2589 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2590 | if ((t = pool_alloc(task)) == NULL) { /* disable this proxy for a while */ |
| 2591 | Alert("out of memory in event_accept().\n"); |
| 2592 | FD_CLR(fd, StaticReadEvent); |
| 2593 | p->state = PR_STIDLE; |
| 2594 | close(cfd); |
| 2595 | pool_free(session, s); |
| 2596 | return 0; |
| 2597 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2598 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2599 | s->cli_addr = addr; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2600 | if (cfd >= global.maxsock) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2601 | Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 2602 | close(cfd); |
| 2603 | pool_free(task, t); |
| 2604 | pool_free(session, s); |
| 2605 | return 0; |
| 2606 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2607 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2608 | if ((fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) || |
| 2609 | (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, |
| 2610 | (char *) &one, sizeof(one)) == -1)) { |
| 2611 | Alert("accept(): cannot set the socket in non blocking mode. Giving up\n"); |
| 2612 | close(cfd); |
| 2613 | pool_free(task, t); |
| 2614 | pool_free(session, s); |
| 2615 | return 0; |
| 2616 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2617 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2618 | if (p->options & PR_O_TCP_CLI_KA) |
| 2619 | setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 2620 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2621 | t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */ |
| 2622 | t->wq = LIST_HEAD(wait_queue); /* but already has a wait queue assigned */ |
| 2623 | t->state = TASK_IDLE; |
| 2624 | t->process = process_session; |
| 2625 | t->context = s; |
| 2626 | |
| 2627 | s->task = t; |
| 2628 | s->proxy = p; |
| 2629 | s->cli_state = (p->mode == PR_MODE_HTTP) ? CL_STHEADERS : CL_STDATA; /* no HTTP headers for non-HTTP proxies */ |
| 2630 | s->srv_state = SV_STIDLE; |
| 2631 | s->req = s->rep = NULL; /* will be allocated later */ |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 2632 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2633 | s->res_cr = s->res_cw = s->res_sr = s->res_sw = RES_SILENT; |
| 2634 | s->cli_fd = cfd; |
| 2635 | s->srv_fd = -1; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2636 | s->srv = NULL; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2637 | s->conn_retries = p->conn_retries; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2638 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2639 | if (s->flags & SN_MONITOR) |
| 2640 | s->logs.logwait = 0; |
| 2641 | else |
| 2642 | s->logs.logwait = p->to_log; |
| 2643 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2644 | s->logs.tv_accept = now; |
| 2645 | s->logs.t_request = -1; |
| 2646 | s->logs.t_connect = -1; |
| 2647 | s->logs.t_data = -1; |
| 2648 | s->logs.t_close = 0; |
| 2649 | s->logs.uri = NULL; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2650 | s->logs.cli_cookie = NULL; |
| 2651 | s->logs.srv_cookie = NULL; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2652 | s->logs.status = -1; |
| 2653 | s->logs.bytes = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2654 | |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2655 | s->uniq_id = totalconn; |
| 2656 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2657 | if (p->nb_req_cap > 0) { |
| 2658 | if ((s->req_cap = |
| 2659 | pool_alloc_from(p->req_cap_pool, p->nb_req_cap*sizeof(char *))) |
| 2660 | == NULL) { /* no memory */ |
| 2661 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2662 | pool_free(task, t); |
| 2663 | pool_free(session, s); |
| 2664 | return 0; |
| 2665 | } |
| 2666 | memset(s->req_cap, 0, p->nb_req_cap*sizeof(char *)); |
| 2667 | } |
| 2668 | else |
| 2669 | s->req_cap = NULL; |
| 2670 | |
| 2671 | if (p->nb_rsp_cap > 0) { |
| 2672 | if ((s->rsp_cap = |
| 2673 | pool_alloc_from(p->rsp_cap_pool, p->nb_rsp_cap*sizeof(char *))) |
| 2674 | == NULL) { /* no memory */ |
| 2675 | if (s->req_cap != NULL) |
| 2676 | pool_free_to(p->req_cap_pool, s->req_cap); |
| 2677 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2678 | pool_free(task, t); |
| 2679 | pool_free(session, s); |
| 2680 | return 0; |
| 2681 | } |
| 2682 | memset(s->rsp_cap, 0, p->nb_rsp_cap*sizeof(char *)); |
| 2683 | } |
| 2684 | else |
| 2685 | s->rsp_cap = NULL; |
| 2686 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2687 | if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP) |
| 2688 | && (p->logfac1 >= 0 || p->logfac2 >= 0)) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2689 | struct sockaddr_storage sockname; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2690 | socklen_t namelen = sizeof(sockname); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2691 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2692 | if (addr.ss_family != AF_INET || |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2693 | !(s->proxy->options & PR_O_TRANSP) || |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2694 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2695 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2696 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2697 | if (p->to_log) { |
| 2698 | /* we have the client ip */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2699 | if (s->logs.logwait & LW_CLIP) |
| 2700 | if (!(s->logs.logwait &= ~LW_CLIP)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2701 | sess_log(s); |
| 2702 | } |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2703 | else if (s->cli_addr.ss_family == AF_INET) { |
| 2704 | char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN]; |
| 2705 | if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sockname)->sin_addr, |
| 2706 | sn, sizeof(sn)) && |
| 2707 | inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 2708 | pn, sizeof(pn))) { |
| 2709 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 2710 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port), |
| 2711 | sn, ntohs(((struct sockaddr_in *)&sockname)->sin_port), |
| 2712 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 2713 | } |
| 2714 | } |
| 2715 | else { |
| 2716 | char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN]; |
| 2717 | if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sockname)->sin6_addr, |
| 2718 | sn, sizeof(sn)) && |
| 2719 | inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr, |
| 2720 | pn, sizeof(pn))) { |
| 2721 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 2722 | pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 2723 | sn, ntohs(((struct sockaddr_in6 *)&sockname)->sin6_port), |
| 2724 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 2725 | } |
| 2726 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2727 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2728 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 2729 | 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] | 2730 | struct sockaddr_in sockname; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2731 | socklen_t namelen = sizeof(sockname); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2732 | int len; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2733 | if (addr.ss_family != AF_INET || |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2734 | !(s->proxy->options & PR_O_TRANSP) || |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2735 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2736 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2737 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2738 | if (s->cli_addr.ss_family == AF_INET) { |
| 2739 | char pn[INET_ADDRSTRLEN]; |
| 2740 | inet_ntop(AF_INET, |
| 2741 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 2742 | pn, sizeof(pn)); |
| 2743 | |
| 2744 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 2745 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 2746 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port)); |
| 2747 | } |
| 2748 | else { |
| 2749 | char pn[INET6_ADDRSTRLEN]; |
| 2750 | inet_ntop(AF_INET6, |
| 2751 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 2752 | pn, sizeof(pn)); |
| 2753 | |
| 2754 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 2755 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 2756 | pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port)); |
| 2757 | } |
| 2758 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2759 | write(1, trash, len); |
| 2760 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2761 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2762 | if ((s->req = pool_alloc(buffer)) == NULL) { /* no memory */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2763 | if (s->rsp_cap != NULL) |
| 2764 | pool_free_to(p->rsp_cap_pool, s->rsp_cap); |
| 2765 | if (s->req_cap != NULL) |
| 2766 | pool_free_to(p->req_cap_pool, s->req_cap); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2767 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2768 | pool_free(task, t); |
| 2769 | pool_free(session, s); |
| 2770 | return 0; |
| 2771 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2772 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2773 | s->req->l = 0; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2774 | s->req->total = 0; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2775 | s->req->h = s->req->r = s->req->lr = s->req->w = s->req->data; /* r and w will be reset further */ |
| 2776 | s->req->rlim = s->req->data + BUFSIZE; |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2777 | if (s->cli_state == CL_STHEADERS) /* reserve some space for header rewriting */ |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2778 | s->req->rlim -= MAXREWRITE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2779 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2780 | if ((s->rep = pool_alloc(buffer)) == NULL) { /* no memory */ |
| 2781 | pool_free(buffer, s->req); |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2782 | if (s->rsp_cap != NULL) |
| 2783 | pool_free_to(p->rsp_cap_pool, s->rsp_cap); |
| 2784 | if (s->req_cap != NULL) |
| 2785 | pool_free_to(p->req_cap_pool, s->req_cap); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2786 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2787 | pool_free(task, t); |
| 2788 | pool_free(session, s); |
| 2789 | return 0; |
| 2790 | } |
| 2791 | s->rep->l = 0; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2792 | s->rep->total = 0; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2793 | 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] | 2794 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2795 | fdtab[cfd].read = &event_cli_read; |
| 2796 | fdtab[cfd].write = &event_cli_write; |
| 2797 | fdtab[cfd].owner = t; |
| 2798 | fdtab[cfd].state = FD_STREADY; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2799 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2800 | if ((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) || |
| 2801 | (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK))) |
| 2802 | /* Either we got a request from a monitoring system on an HTTP instance, |
| 2803 | * or we're in health check mode with the 'httpchk' option enabled. In |
| 2804 | * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit. |
| 2805 | */ |
| 2806 | client_retnclose(s, 19, "HTTP/1.0 200 OK\r\n\r\n"); /* forge a 200 response */ |
| 2807 | else if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */ |
| 2808 | client_retnclose(s, 3, "OK\n"); /* forge an "OK" response */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2809 | } |
| 2810 | else { |
| 2811 | FD_SET(cfd, StaticReadEvent); |
| 2812 | } |
| 2813 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2814 | #if defined(DEBUG_FULL) && defined(ENABLE_EPOLL) |
| 2815 | if (PrevReadEvent) { |
| 2816 | assert(!(FD_ISSET(cfd, PrevReadEvent))); |
| 2817 | assert(!(FD_ISSET(cfd, PrevWriteEvent))); |
| 2818 | } |
| 2819 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2820 | fd_insert(cfd); |
| 2821 | |
| 2822 | tv_eternity(&s->cnexpire); |
| 2823 | tv_eternity(&s->srexpire); |
| 2824 | tv_eternity(&s->swexpire); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2825 | tv_eternity(&s->crexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2826 | tv_eternity(&s->cwexpire); |
| 2827 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2828 | if (s->proxy->clitimeout) { |
| 2829 | if (FD_ISSET(cfd, StaticReadEvent)) |
| 2830 | tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout); |
| 2831 | if (FD_ISSET(cfd, StaticWriteEvent)) |
| 2832 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
| 2833 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2834 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2835 | tv_min(&t->expire, &s->crexpire, &s->cwexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2836 | |
| 2837 | task_queue(t); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2838 | |
| 2839 | if (p->mode != PR_MODE_HEALTH) |
| 2840 | task_wakeup(&rq, t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2841 | |
| 2842 | p->nbconn++; |
| 2843 | actconn++; |
| 2844 | totalconn++; |
| 2845 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2846 | // 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] | 2847 | } /* end of while (p->nbconn < p->maxconn) */ |
| 2848 | return 0; |
| 2849 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2850 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2851 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2852 | /* |
| 2853 | * This function is used only for server health-checks. It handles |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2854 | * the connection acknowledgement. If the proxy requires HTTP health-checks, |
| 2855 | * 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] | 2856 | * or -1 if an error occured. |
| 2857 | */ |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2858 | int event_srv_chk_w(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2859 | struct task *t = fdtab[fd].owner; |
| 2860 | struct server *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2861 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2862 | int skerr; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2863 | socklen_t lskerr = sizeof(skerr); |
| 2864 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2865 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2866 | /* in case of TCP only, this tells us if the connection succeeded */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2867 | if (skerr) |
| 2868 | s->result = -1; |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 2869 | else if (s->result != -1) { |
| 2870 | /* 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] | 2871 | if (s->proxy->options & PR_O_HTTP_CHK) { |
| 2872 | int ret; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2873 | /* 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] | 2874 | * so we'll send the request, and won't wake the checker up now. |
| 2875 | */ |
| 2876 | #ifndef MSG_NOSIGNAL |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2877 | 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] | 2878 | #else |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2879 | 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] | 2880 | #endif |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 2881 | if (ret == s->proxy->check_len) { |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2882 | FD_SET(fd, StaticReadEvent); /* prepare for reading reply */ |
| 2883 | FD_CLR(fd, StaticWriteEvent); /* nothing more to write */ |
| 2884 | return 0; |
| 2885 | } |
| 2886 | else |
| 2887 | s->result = -1; |
| 2888 | } |
| 2889 | else { |
| 2890 | /* good TCP connection is enough */ |
| 2891 | s->result = 1; |
| 2892 | } |
| 2893 | } |
| 2894 | |
| 2895 | task_wakeup(&rq, t); |
| 2896 | return 0; |
| 2897 | } |
| 2898 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2899 | |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2900 | /* |
| 2901 | * This function is used only for server health-checks. It handles |
| 2902 | * the server's reply to an HTTP request. It returns 1 if the server replies |
| 2903 | * 2xx or 3xx (valid responses), or -1 in other cases. |
| 2904 | */ |
| 2905 | int event_srv_chk_r(int fd) { |
| 2906 | char reply[64]; |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 2907 | int len, result; |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2908 | struct task *t = fdtab[fd].owner; |
| 2909 | struct server *s = t->context; |
| 2910 | |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 2911 | result = len = -1; |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2912 | #ifndef MSG_NOSIGNAL |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2913 | { |
| 2914 | int skerr; |
| 2915 | socklen_t lskerr = sizeof(skerr); |
| 2916 | |
| 2917 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2918 | if (!skerr) |
| 2919 | len = recv(fd, reply, sizeof(reply), 0); |
| 2920 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2921 | #else |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2922 | /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available |
| 2923 | * but the connection was closed on the remote end. Fortunately, recv still |
| 2924 | * works correctly and we don't need to do the getsockopt() on linux. |
| 2925 | */ |
| 2926 | len = recv(fd, reply, sizeof(reply), MSG_NOSIGNAL); |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2927 | #endif |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2928 | if ((len >= sizeof("HTTP/1.0 000")) && |
| 2929 | !memcmp(reply, "HTTP/1.", 7) && |
| 2930 | (reply[9] == '2' || reply[9] == '3')) /* 2xx or 3xx */ |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 2931 | result = 1; |
| 2932 | |
| 2933 | if (s->result != -1) |
| 2934 | s->result = result; |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2935 | |
| 2936 | FD_CLR(fd, StaticReadEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2937 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2938 | return 0; |
| 2939 | } |
| 2940 | |
| 2941 | |
| 2942 | /* |
| 2943 | * this function writes the string <str> at position <pos> which must be in buffer <b>, |
| 2944 | * and moves <end> just after the end of <str>. |
| 2945 | * <b>'s parameters (l, r, w, h, lr) are recomputed to be valid after the shift. |
| 2946 | * the shift value (positive or negative) is returned. |
| 2947 | * If there's no space left, the move is not done. |
| 2948 | * |
| 2949 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2950 | int buffer_replace(struct buffer *b, char *pos, char *end, char *str) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2951 | int delta; |
| 2952 | int len; |
| 2953 | |
| 2954 | len = strlen(str); |
| 2955 | delta = len - (end - pos); |
| 2956 | |
| 2957 | if (delta + b->r >= b->data + BUFSIZE) |
| 2958 | return 0; /* no space left */ |
| 2959 | |
| 2960 | /* first, protect the end of the buffer */ |
| 2961 | memmove(end + delta, end, b->data + b->l - end); |
| 2962 | |
| 2963 | /* now, copy str over pos */ |
| 2964 | memcpy(pos, str,len); |
| 2965 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2966 | /* we only move data after the displaced zone */ |
| 2967 | if (b->r > pos) b->r += delta; |
| 2968 | if (b->w > pos) b->w += delta; |
| 2969 | if (b->h > pos) b->h += delta; |
| 2970 | if (b->lr > pos) b->lr += delta; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2971 | b->l += delta; |
| 2972 | |
| 2973 | return delta; |
| 2974 | } |
| 2975 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2976 | /* 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] | 2977 | * len is 0. |
| 2978 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2979 | 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] | 2980 | int delta; |
| 2981 | |
| 2982 | delta = len - (end - pos); |
| 2983 | |
| 2984 | if (delta + b->r >= b->data + BUFSIZE) |
| 2985 | return 0; /* no space left */ |
| 2986 | |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 2987 | if (b->data + b->l < end) |
| 2988 | /* The data has been stolen, we could have crashed. Maybe we should abort() ? */ |
| 2989 | return 0; |
| 2990 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2991 | /* first, protect the end of the buffer */ |
| 2992 | memmove(end + delta, end, b->data + b->l - end); |
| 2993 | |
| 2994 | /* now, copy str over pos */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2995 | if (len) |
| 2996 | memcpy(pos, str, len); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2997 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2998 | /* we only move data after the displaced zone */ |
| 2999 | if (b->r > pos) b->r += delta; |
| 3000 | if (b->w > pos) b->w += delta; |
| 3001 | if (b->h > pos) b->h += delta; |
| 3002 | if (b->lr > pos) b->lr += delta; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3003 | b->l += delta; |
| 3004 | |
| 3005 | return delta; |
| 3006 | } |
| 3007 | |
| 3008 | |
| 3009 | int exp_replace(char *dst, char *src, char *str, regmatch_t *matches) { |
| 3010 | char *old_dst = dst; |
| 3011 | |
| 3012 | while (*str) { |
| 3013 | if (*str == '\\') { |
| 3014 | str++; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 3015 | if (isdigit((int)*str)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3016 | int len, num; |
| 3017 | |
| 3018 | num = *str - '0'; |
| 3019 | str++; |
| 3020 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3021 | if (matches[num].rm_eo > -1 && matches[num].rm_so > -1) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3022 | len = matches[num].rm_eo - matches[num].rm_so; |
| 3023 | memcpy(dst, src + matches[num].rm_so, len); |
| 3024 | dst += len; |
| 3025 | } |
| 3026 | |
| 3027 | } |
| 3028 | else if (*str == 'x') { |
| 3029 | unsigned char hex1, hex2; |
| 3030 | str++; |
| 3031 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 3032 | hex1 = toupper(*str++) - '0'; |
| 3033 | hex2 = toupper(*str++) - '0'; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3034 | |
| 3035 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 3036 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 3037 | *dst++ = (hex1<<4) + hex2; |
| 3038 | } |
| 3039 | else |
| 3040 | *dst++ = *str++; |
| 3041 | } |
| 3042 | else |
| 3043 | *dst++ = *str++; |
| 3044 | } |
| 3045 | *dst = 0; |
| 3046 | return dst - old_dst; |
| 3047 | } |
| 3048 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 3049 | static int ishex(char s) |
| 3050 | { |
| 3051 | return (s >= '0' && s <= '9') || (s >= 'A' && s <= 'F') || (s >= 'a' && s <= 'f'); |
| 3052 | } |
| 3053 | |
| 3054 | /* returns NULL if the replacement string <str> is valid, or the pointer to the first error */ |
| 3055 | char *check_replace_string(char *str) |
| 3056 | { |
| 3057 | char *err = NULL; |
| 3058 | while (*str) { |
| 3059 | if (*str == '\\') { |
| 3060 | err = str; /* in case of a backslash, we return the pointer to it */ |
| 3061 | str++; |
| 3062 | if (!*str) |
| 3063 | return err; |
| 3064 | else if (isdigit((int)*str)) |
| 3065 | err = NULL; |
| 3066 | else if (*str == 'x') { |
| 3067 | str++; |
| 3068 | if (!ishex(*str)) |
| 3069 | return err; |
| 3070 | str++; |
| 3071 | if (!ishex(*str)) |
| 3072 | return err; |
| 3073 | err = NULL; |
| 3074 | } |
| 3075 | else { |
| 3076 | Warning("'\\%c' : deprecated use of a backslash before something not '\\','x' or a digit.\n", *str); |
| 3077 | err = NULL; |
| 3078 | } |
| 3079 | } |
| 3080 | str++; |
| 3081 | } |
| 3082 | return err; |
| 3083 | } |
| 3084 | |
| 3085 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3086 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3087 | /* |
| 3088 | * manages the client FSM and its socket. BTW, it also tries to handle the |
| 3089 | * cookie. It returns 1 if a state has changed (and a resync may be needed), |
| 3090 | * 0 else. |
| 3091 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3092 | int process_cli(struct session *t) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3093 | int s = t->srv_state; |
| 3094 | int c = t->cli_state; |
| 3095 | struct buffer *req = t->req; |
| 3096 | struct buffer *rep = t->rep; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3097 | int method_checked = 0; |
| 3098 | appsess *asession_temp = NULL; |
| 3099 | appsess local_asession; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3100 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3101 | #ifdef DEBUG_FULL |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3102 | fprintf(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n", |
| 3103 | cli_stnames[c], srv_stnames[s], |
| 3104 | FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 3105 | t->crexpire.tv_sec, t->crexpire.tv_usec, |
| 3106 | t->cwexpire.tv_sec, t->cwexpire.tv_usec); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3107 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3108 | //fprintf(stderr,"process_cli: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s, |
| 3109 | //FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 3110 | //FD_ISSET(t->srv_fd, StaticReadEvent), FD_ISSET(t->srv_fd, StaticWriteEvent) |
| 3111 | //); |
| 3112 | if (c == CL_STHEADERS) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3113 | /* now parse the partial (or complete) headers */ |
| 3114 | while (req->lr < req->r) { /* this loop only sees one header at each iteration */ |
| 3115 | char *ptr; |
| 3116 | int delete_header; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3117 | char *request_line = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3118 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3119 | ptr = req->lr; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3120 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3121 | /* look for the end of the current header */ |
| 3122 | while (ptr < req->r && *ptr != '\n' && *ptr != '\r') |
| 3123 | ptr++; |
| 3124 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3125 | if (ptr == req->h) { /* empty line, end of headers */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3126 | int line, len; |
| 3127 | /* we can only get here after an end of headers */ |
| 3128 | /* we'll have something else to do here : add new headers ... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3129 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3130 | if (t->flags & SN_CLDENY) { |
| 3131 | /* no need to go further */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3132 | t->logs.status = 403; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3133 | client_retnclose(t, t->proxy->errmsg.len403, t->proxy->errmsg.msg403); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3134 | if (!(t->flags & SN_ERR_MASK)) |
| 3135 | t->flags |= SN_ERR_PRXCOND; |
| 3136 | if (!(t->flags & SN_FINST_MASK)) |
| 3137 | t->flags |= SN_FINST_R; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3138 | return 1; |
| 3139 | } |
| 3140 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3141 | for (line = 0; line < t->proxy->nb_reqadd; line++) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3142 | len = sprintf(trash, "%s\r\n", t->proxy->req_add[line]); |
| 3143 | buffer_replace2(req, req->h, req->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3144 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3145 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3146 | if (t->proxy->options & PR_O_FWDFOR) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3147 | if (t->cli_addr.ss_family == AF_INET) { |
| 3148 | unsigned char *pn; |
| 3149 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
| 3150 | len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d\r\n", |
| 3151 | pn[0], pn[1], pn[2], pn[3]); |
| 3152 | buffer_replace2(req, req->h, req->h, trash, len); |
| 3153 | } |
| 3154 | else if (t->cli_addr.ss_family == AF_INET6) { |
| 3155 | char pn[INET6_ADDRSTRLEN]; |
| 3156 | inet_ntop(AF_INET6, |
| 3157 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 3158 | pn, sizeof(pn)); |
| 3159 | len = sprintf(trash, "X-Forwarded-For: %s\r\n", pn); |
| 3160 | buffer_replace2(req, req->h, req->h, trash, len); |
| 3161 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3162 | } |
| 3163 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 3164 | /* add a "connection: close" line if needed */ |
| 3165 | if (t->proxy->options & PR_O_HTTP_CLOSE) |
| 3166 | buffer_replace2(req, req->h, req->h, "Connection: close\r\n", 19); |
| 3167 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 3168 | if (!memcmp(req->data, "POST ", 5)) { |
| 3169 | /* this is a POST request, which is not cacheable by default */ |
| 3170 | t->flags |= SN_POST; |
| 3171 | } |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 3172 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3173 | t->cli_state = CL_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3174 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3175 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3176 | t->logs.t_request = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3177 | /* FIXME: we'll set the client in a wait state while we try to |
| 3178 | * connect to the server. Is this really needed ? wouldn't it be |
| 3179 | * better to release the maximum of system buffers instead ? */ |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3180 | //FD_CLR(t->cli_fd, StaticReadEvent); |
| 3181 | //tv_eternity(&t->crexpire); |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 3182 | |
| 3183 | /* FIXME: if we break here (as up to 1.1.23), having the client |
| 3184 | * shutdown its connection can lead to an abort further. |
| 3185 | * it's better to either return 1 or even jump directly to the |
| 3186 | * data state which will save one schedule. |
| 3187 | */ |
| 3188 | //break; |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 3189 | |
| 3190 | if (!t->proxy->clitimeout || |
| 3191 | (t->srv_state < SV_STDATA && t->proxy->srvtimeout)) |
| 3192 | /* If the client has no timeout, or if the server is not ready yet, |
| 3193 | * and we know for sure that it can expire, then it's cleaner to |
| 3194 | * disable the timeout on the client side so that too low values |
| 3195 | * cannot make the sessions abort too early. |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3196 | * |
| 3197 | * FIXME-20050705: the server needs a way to re-enable this time-out |
| 3198 | * when it switches its state, otherwise a client can stay connected |
| 3199 | * indefinitely. This now seems to be OK. |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 3200 | */ |
| 3201 | tv_eternity(&t->crexpire); |
| 3202 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 3203 | goto process_data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3204 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3205 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3206 | /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */ |
| 3207 | if (ptr > req->r - 2) { |
| 3208 | /* this is a partial header, let's wait for more to come */ |
| 3209 | req->lr = ptr; |
| 3210 | break; |
| 3211 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3212 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3213 | /* now we know that *ptr is either \r or \n, |
| 3214 | * and that there are at least 1 char after it. |
| 3215 | */ |
| 3216 | if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n')) |
| 3217 | req->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */ |
| 3218 | else |
| 3219 | req->lr = ptr + 2; /* \r\n or \n\r */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3220 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3221 | /* |
| 3222 | * now we know that we have a full header ; we can do whatever |
| 3223 | * we want with these pointers : |
| 3224 | * req->h = beginning of header |
| 3225 | * ptr = end of header (first \r or \n) |
| 3226 | * req->lr = beginning of next line (next rep->h) |
| 3227 | * req->r = end of data (not used at this stage) |
| 3228 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3229 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3230 | if (!method_checked && (t->proxy->appsession_name != NULL) && |
| 3231 | ((memcmp(req->h, "GET ", 4) == 0) || (memcmp(req->h, "POST ", 4) == 0)) && |
| 3232 | ((request_line = memchr(req->h, ';', req->lr - req->h)) != NULL)) { |
| 3233 | |
| 3234 | /* skip ; */ |
| 3235 | request_line++; |
| 3236 | |
| 3237 | /* look if we have a jsessionid */ |
| 3238 | |
| 3239 | if (strncasecmp(request_line, t->proxy->appsession_name, t->proxy->appsession_name_len) == 0) { |
| 3240 | |
| 3241 | /* skip jsessionid= */ |
| 3242 | request_line += t->proxy->appsession_name_len + 1; |
| 3243 | |
| 3244 | /* First try if we allready have an appsession */ |
| 3245 | asession_temp = &local_asession; |
| 3246 | |
| 3247 | if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) { |
| 3248 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 3249 | send_log(t->proxy, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 3250 | return 0; |
| 3251 | } |
| 3252 | |
| 3253 | /* Copy the sessionid */ |
| 3254 | memcpy(asession_temp->sessid, request_line, t->proxy->appsession_len); |
| 3255 | asession_temp->sessid[t->proxy->appsession_len] = 0; |
| 3256 | asession_temp->serverid = NULL; |
| 3257 | |
| 3258 | /* only do insert, if lookup fails */ |
| 3259 | if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *)&asession_temp)) { |
| 3260 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 3261 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 3262 | send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 3263 | return 0; |
| 3264 | } |
| 3265 | asession_temp->sessid = local_asession.sessid; |
| 3266 | asession_temp->serverid = local_asession.serverid; |
| 3267 | chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3268 | } /* end if (chtbl_lookup()) */ |
| 3269 | else { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3270 | /*free wasted memory;*/ |
| 3271 | pool_free_to(apools.sessid, local_asession.sessid); |
| 3272 | } |
| 3273 | |
| 3274 | tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout); |
| 3275 | asession_temp->request_count++; |
| 3276 | |
| 3277 | #if defined(DEBUG_HASH) |
| 3278 | print_table(&(t->proxy->htbl_proxy)); |
| 3279 | #endif |
| 3280 | |
| 3281 | if (asession_temp->serverid == NULL) { |
| 3282 | Alert("Found Application Session without matching server.\n"); |
| 3283 | } else { |
| 3284 | struct server *srv = t->proxy->srv; |
| 3285 | while (srv) { |
| 3286 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
| 3287 | if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) { |
| 3288 | /* we found the server and it's usable */ |
| 3289 | t->flags &= ~SN_CK_MASK; |
| 3290 | t->flags |= SN_CK_VALID | SN_DIRECT; |
| 3291 | t->srv = srv; |
| 3292 | break; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3293 | } else { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3294 | t->flags &= ~SN_CK_MASK; |
| 3295 | t->flags |= SN_CK_DOWN; |
| 3296 | } |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3297 | } /* end if (strcmp()) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3298 | srv = srv->next; |
| 3299 | }/* end while(srv) */ |
| 3300 | }/* end else of if (asession_temp->serverid == NULL) */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3301 | }/* 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] | 3302 | else { |
| 3303 | //fprintf(stderr,">>>>>>>>>>>>>>>>>>>>>>NO SESSION\n"); |
| 3304 | } |
willy tarreau | 598da41 | 2005-12-18 01:07:29 +0100 | [diff] [blame] | 3305 | method_checked = 1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3306 | } /* end if (!method_checked ...) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3307 | else{ |
| 3308 | //printf("No Methode-Header with Session-String\n"); |
| 3309 | } |
| 3310 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3311 | if (t->logs.logwait & LW_REQ) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3312 | /* we have a complete HTTP request that we must log */ |
| 3313 | int urilen; |
| 3314 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3315 | if ((t->logs.uri = pool_alloc(requri)) == NULL) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3316 | Alert("HTTP logging : out of memory.\n"); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3317 | t->logs.status = 500; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3318 | client_retnclose(t, t->proxy->errmsg.len500, t->proxy->errmsg.msg500); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3319 | if (!(t->flags & SN_ERR_MASK)) |
| 3320 | t->flags |= SN_ERR_PRXCOND; |
| 3321 | if (!(t->flags & SN_FINST_MASK)) |
| 3322 | t->flags |= SN_FINST_R; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3323 | return 1; |
| 3324 | } |
| 3325 | |
| 3326 | urilen = ptr - req->h; |
| 3327 | if (urilen >= REQURI_LEN) |
| 3328 | urilen = REQURI_LEN - 1; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3329 | memcpy(t->logs.uri, req->h, urilen); |
| 3330 | t->logs.uri[urilen] = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3331 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3332 | if (!(t->logs.logwait &= ~LW_REQ)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3333 | sess_log(t); |
| 3334 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 3335 | else if (t->logs.logwait & LW_REQHDR) { |
| 3336 | struct cap_hdr *h; |
| 3337 | int len; |
| 3338 | for (h = t->proxy->req_cap; h; h = h->next) { |
| 3339 | if ((h->namelen + 2 <= ptr - req->h) && |
| 3340 | (req->h[h->namelen] == ':') && |
| 3341 | (strncasecmp(req->h, h->name, h->namelen) == 0)) { |
| 3342 | |
| 3343 | if (t->req_cap[h->index] == NULL) |
| 3344 | t->req_cap[h->index] = pool_alloc_from(h->pool, h->len + 1); |
| 3345 | |
| 3346 | len = ptr - (req->h + h->namelen + 2); |
| 3347 | if (len > h->len) |
| 3348 | len = h->len; |
| 3349 | |
| 3350 | memcpy(t->req_cap[h->index], req->h + h->namelen + 2, len); |
| 3351 | t->req_cap[h->index][len]=0; |
| 3352 | } |
| 3353 | } |
| 3354 | |
| 3355 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3356 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3357 | delete_header = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3358 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 3359 | 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] | 3360 | int len, max; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 3361 | 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] | 3362 | max = ptr - req->h; |
| 3363 | UBOUND(max, sizeof(trash) - len - 1); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3364 | len += strlcpy2(trash + len, req->h, max + 1); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3365 | trash[len++] = '\n'; |
| 3366 | write(1, trash, len); |
| 3367 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3368 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 3369 | |
| 3370 | /* remove "connection: " if needed */ |
| 3371 | if (!delete_header && (t->proxy->options & PR_O_HTTP_CLOSE) |
| 3372 | && (strncasecmp(req->h, "Connection: ", 12) == 0)) { |
| 3373 | delete_header = 1; |
| 3374 | } |
| 3375 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3376 | /* try headers regexps */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 3377 | if (!delete_header && t->proxy->req_exp != NULL |
| 3378 | && !(t->flags & SN_CLDENY)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3379 | struct hdr_exp *exp; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3380 | char term; |
| 3381 | |
| 3382 | term = *ptr; |
| 3383 | *ptr = '\0'; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3384 | exp = t->proxy->req_exp; |
| 3385 | do { |
| 3386 | if (regexec(exp->preg, req->h, MAX_MATCH, pmatch, 0) == 0) { |
| 3387 | switch (exp->action) { |
| 3388 | case ACT_ALLOW: |
| 3389 | if (!(t->flags & SN_CLDENY)) |
| 3390 | t->flags |= SN_CLALLOW; |
| 3391 | break; |
| 3392 | case ACT_REPLACE: |
| 3393 | if (!(t->flags & SN_CLDENY)) { |
| 3394 | int len = exp_replace(trash, req->h, exp->replace, pmatch); |
| 3395 | ptr += buffer_replace2(req, req->h, ptr, trash, len); |
| 3396 | } |
| 3397 | break; |
| 3398 | case ACT_REMOVE: |
| 3399 | if (!(t->flags & SN_CLDENY)) |
| 3400 | delete_header = 1; |
| 3401 | break; |
| 3402 | case ACT_DENY: |
| 3403 | if (!(t->flags & SN_CLALLOW)) |
| 3404 | t->flags |= SN_CLDENY; |
| 3405 | break; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3406 | case ACT_PASS: /* we simply don't deny this one */ |
| 3407 | break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3408 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3409 | break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3410 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3411 | } while ((exp = exp->next) != NULL); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3412 | *ptr = term; /* restore the string terminator */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3413 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3414 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3415 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3416 | * attributes whose name begin with a '$', and associate them with |
| 3417 | * the right cookie, if we want to delete this cookie. |
| 3418 | * So there are 3 cases for each cookie read : |
| 3419 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3420 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3421 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3422 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3423 | * "special" cookie. |
| 3424 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3425 | * remove it. If no application cookie persists in the header, we |
| 3426 | * *MUST* delete it |
| 3427 | */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3428 | if (!delete_header && |
| 3429 | (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] | 3430 | && !(t->flags & SN_CLDENY) && (ptr >= req->h + 8) |
willy tarreau | 906b268 | 2005-12-17 13:49:52 +0100 | [diff] [blame] | 3431 | && (strncasecmp(req->h, "Cookie: ", 8) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3432 | char *p1, *p2, *p3, *p4; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3433 | char *del_colon, *del_cookie, *colon; |
| 3434 | int app_cookies; |
| 3435 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3436 | p1 = req->h + 8; /* first char after 'Cookie: ' */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3437 | colon = p1; |
| 3438 | /* del_cookie == NULL => nothing to be deleted */ |
| 3439 | del_colon = del_cookie = NULL; |
| 3440 | app_cookies = 0; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3441 | |
| 3442 | while (p1 < ptr) { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3443 | /* skip spaces and colons, but keep an eye on these ones */ |
| 3444 | while (p1 < ptr) { |
| 3445 | if (*p1 == ';' || *p1 == ',') |
| 3446 | colon = p1; |
| 3447 | else if (!isspace((int)*p1)) |
| 3448 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3449 | p1++; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3450 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3451 | |
| 3452 | if (p1 == ptr) |
| 3453 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3454 | |
| 3455 | /* p1 is at the beginning of the cookie name */ |
| 3456 | p2 = p1; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3457 | while (p2 < ptr && *p2 != '=') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3458 | p2++; |
| 3459 | |
| 3460 | if (p2 == ptr) |
| 3461 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3462 | |
| 3463 | p3 = p2 + 1; /* skips the '=' sign */ |
| 3464 | if (p3 == ptr) |
| 3465 | break; |
| 3466 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3467 | p4 = p3; |
| 3468 | while (p4 < ptr && !isspace((int)*p4) && *p4 != ';' && *p4 != ',') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3469 | p4++; |
| 3470 | |
| 3471 | /* here, we have the cookie name between p1 and p2, |
| 3472 | * and its value between p3 and p4. |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3473 | * we can process it : |
| 3474 | * |
| 3475 | * Cookie: NAME=VALUE; |
| 3476 | * | || || | |
| 3477 | * | || || +--> p4 |
| 3478 | * | || |+-------> p3 |
| 3479 | * | || +--------> p2 |
| 3480 | * | |+------------> p1 |
| 3481 | * | +-------------> colon |
| 3482 | * +--------------------> req->h |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3483 | */ |
| 3484 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3485 | if (*p1 == '$') { |
| 3486 | /* skip this one */ |
| 3487 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3488 | else { |
| 3489 | /* first, let's see if we want to capture it */ |
| 3490 | if (t->proxy->capture_name != NULL && |
| 3491 | t->logs.cli_cookie == NULL && |
| 3492 | (p4 - p1 >= t->proxy->capture_namelen) && |
| 3493 | memcmp(p1, t->proxy->capture_name, t->proxy->capture_namelen) == 0) { |
| 3494 | int log_len = p4 - p1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3495 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3496 | if ((t->logs.cli_cookie = pool_alloc(capture)) == NULL) { |
| 3497 | Alert("HTTP logging : out of memory.\n"); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3498 | } else { |
| 3499 | if (log_len > t->proxy->capture_len) |
| 3500 | log_len = t->proxy->capture_len; |
| 3501 | memcpy(t->logs.cli_cookie, p1, log_len); |
| 3502 | t->logs.cli_cookie[log_len] = 0; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3503 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3504 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3505 | |
| 3506 | if ((p2 - p1 == t->proxy->cookie_len) && (t->proxy->cookie_name != NULL) && |
| 3507 | (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) { |
| 3508 | /* Cool... it's the right one */ |
| 3509 | struct server *srv = t->proxy->srv; |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3510 | char *delim; |
| 3511 | |
| 3512 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 3513 | * have the server ID betweek p3 and delim, and the original cookie between |
| 3514 | * delim+1 and p4. Otherwise, delim==p4 : |
| 3515 | * |
| 3516 | * Cookie: NAME=SRV~VALUE; |
| 3517 | * | || || | | |
| 3518 | * | || || | +--> p4 |
| 3519 | * | || || +--------> delim |
| 3520 | * | || |+-----------> p3 |
| 3521 | * | || +------------> p2 |
| 3522 | * | |+----------------> p1 |
| 3523 | * | +-----------------> colon |
| 3524 | * +------------------------> req->h |
| 3525 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3526 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3527 | if (t->proxy->options & PR_O_COOK_PFX) { |
| 3528 | for (delim = p3; delim < p4; delim++) |
| 3529 | if (*delim == COOKIE_DELIM) |
| 3530 | break; |
| 3531 | } |
| 3532 | else |
| 3533 | delim = p4; |
| 3534 | |
| 3535 | |
| 3536 | /* Here, we'll look for the first running server which supports the cookie. |
| 3537 | * This allows to share a same cookie between several servers, for example |
| 3538 | * to dedicate backup servers to specific servers only. |
| 3539 | */ |
| 3540 | while (srv) { |
| 3541 | if ((srv->cklen == delim - p3) && !memcmp(p3, srv->cookie, delim - p3)) { |
| 3542 | if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) { |
| 3543 | /* we found the server and it's usable */ |
| 3544 | t->flags &= ~SN_CK_MASK; |
| 3545 | t->flags |= SN_CK_VALID | SN_DIRECT; |
| 3546 | t->srv = srv; |
| 3547 | break; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3548 | } else { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3549 | /* we found a server, but it's down */ |
| 3550 | t->flags &= ~SN_CK_MASK; |
| 3551 | t->flags |= SN_CK_DOWN; |
| 3552 | } |
| 3553 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3554 | srv = srv->next; |
| 3555 | } |
| 3556 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3557 | if (!srv && !(t->flags & SN_CK_DOWN)) { |
| 3558 | /* no server matched this cookie */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3559 | t->flags &= ~SN_CK_MASK; |
| 3560 | t->flags |= SN_CK_INVALID; |
| 3561 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3562 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3563 | /* depending on the cookie mode, we may have to either : |
| 3564 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3565 | * the server never sees it ; |
| 3566 | * - remove the server id from the cookie value, and tag the cookie as an |
| 3567 | * application cookie so that it does not get accidentely removed later, |
| 3568 | * if we're in cookie prefix mode |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3569 | */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3570 | if ((t->proxy->options & PR_O_COOK_PFX) && (delim != p4)) { |
| 3571 | buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 3572 | p4 -= (delim + 1 - p3); |
| 3573 | ptr -= (delim + 1 - p3); |
| 3574 | del_cookie = del_colon = NULL; |
| 3575 | app_cookies++; /* protect the header from deletion */ |
| 3576 | } |
| 3577 | else if (del_cookie == NULL && |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3578 | (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] | 3579 | del_cookie = p1; |
| 3580 | del_colon = colon; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3581 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3582 | } else { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3583 | /* now we know that we must keep this cookie since it's |
| 3584 | * not ours. But if we wanted to delete our cookie |
| 3585 | * earlier, we cannot remove the complete header, but we |
| 3586 | * can remove the previous block itself. |
| 3587 | */ |
| 3588 | app_cookies++; |
| 3589 | |
| 3590 | if (del_cookie != NULL) { |
| 3591 | buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 3592 | p4 -= (p1 - del_cookie); |
| 3593 | ptr -= (p1 - del_cookie); |
| 3594 | del_cookie = del_colon = NULL; |
| 3595 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3596 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3597 | |
| 3598 | if ((t->proxy->appsession_name != NULL) && |
| 3599 | (memcmp(p1, t->proxy->appsession_name, p2 - p1) == 0)) { |
| 3600 | /* first, let's see if the cookie is our appcookie*/ |
| 3601 | |
| 3602 | /* Cool... it's the right one */ |
| 3603 | |
| 3604 | asession_temp = &local_asession; |
| 3605 | |
| 3606 | if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) { |
| 3607 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3608 | send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3609 | return 0; |
| 3610 | } |
| 3611 | |
| 3612 | memcpy(asession_temp->sessid, p3, t->proxy->appsession_len); |
| 3613 | asession_temp->sessid[t->proxy->appsession_len] = 0; |
| 3614 | asession_temp->serverid = NULL; |
| 3615 | |
| 3616 | /* only do insert, if lookup fails */ |
| 3617 | if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *) &asession_temp) != 0) { |
| 3618 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 3619 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 3620 | send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 3621 | return 0; |
| 3622 | } |
| 3623 | |
| 3624 | asession_temp->sessid = local_asession.sessid; |
| 3625 | asession_temp->serverid = local_asession.serverid; |
| 3626 | chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp); |
| 3627 | } |
| 3628 | else{ |
| 3629 | /* free wasted memory */ |
| 3630 | pool_free_to(apools.sessid, local_asession.sessid); |
| 3631 | } |
| 3632 | |
| 3633 | if (asession_temp->serverid == NULL) { |
| 3634 | Alert("Found Application Session without matching server.\n"); |
| 3635 | } else { |
| 3636 | struct server *srv = t->proxy->srv; |
| 3637 | while (srv) { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3638 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3639 | if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) { |
| 3640 | /* we found the server and it's usable */ |
| 3641 | t->flags &= ~SN_CK_MASK; |
| 3642 | t->flags |= SN_CK_VALID | SN_DIRECT; |
| 3643 | t->srv = srv; |
| 3644 | break; |
| 3645 | } else { |
| 3646 | t->flags &= ~SN_CK_MASK; |
| 3647 | t->flags |= SN_CK_DOWN; |
| 3648 | } |
| 3649 | } |
| 3650 | srv = srv->next; |
| 3651 | }/* end while(srv) */ |
| 3652 | }/* end else if server == NULL */ |
| 3653 | |
| 3654 | tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3655 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3656 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3657 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3658 | /* we'll have to look for another cookie ... */ |
| 3659 | p1 = p4; |
| 3660 | } /* while (p1 < ptr) */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3661 | |
| 3662 | /* There's no more cookie on this line. |
| 3663 | * We may have marked the last one(s) for deletion. |
| 3664 | * We must do this now in two ways : |
| 3665 | * - if there is no app cookie, we simply delete the header ; |
| 3666 | * - if there are app cookies, we must delete the end of the |
| 3667 | * string properly, including the colon/semi-colon before |
| 3668 | * the cookie name. |
| 3669 | */ |
| 3670 | if (del_cookie != NULL) { |
| 3671 | if (app_cookies) { |
| 3672 | buffer_replace2(req, del_colon, ptr, NULL, 0); |
| 3673 | /* WARNING! <ptr> becomes invalid for now. If some code |
| 3674 | * below needs to rely on it before the end of the global |
| 3675 | * header loop, we need to correct it with this code : |
| 3676 | * ptr = del_colon; |
| 3677 | */ |
| 3678 | } |
| 3679 | else |
| 3680 | delete_header = 1; |
| 3681 | } |
| 3682 | } /* end of cookie processing on this header */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3683 | |
| 3684 | /* let's look if we have to delete this header */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3685 | if (delete_header && !(t->flags & SN_CLDENY)) { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3686 | buffer_replace2(req, req->h, req->lr, NULL, 0); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3687 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3688 | /* WARNING: ptr is not valid anymore, since the header may have been deleted or truncated ! */ |
| 3689 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3690 | req->h = req->lr; |
| 3691 | } /* while (req->lr < req->r) */ |
| 3692 | |
| 3693 | /* end of header processing (even if incomplete) */ |
| 3694 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3695 | if ((req->l < req->rlim - req->data) && ! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 3696 | /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer |
| 3697 | * full. We cannot loop here since event_cli_read will disable it only if |
| 3698 | * req->l == rlim-data |
| 3699 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3700 | FD_SET(t->cli_fd, StaticReadEvent); |
| 3701 | if (t->proxy->clitimeout) |
| 3702 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 3703 | else |
| 3704 | tv_eternity(&t->crexpire); |
| 3705 | } |
| 3706 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3707 | /* 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] | 3708 | * won't be able to free more later, so the session will never terminate. |
| 3709 | */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3710 | if (req->l >= req->rlim - req->data) { |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3711 | t->logs.status = 400; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3712 | client_retnclose(t, t->proxy->errmsg.len400, t->proxy->errmsg.msg400); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3713 | if (!(t->flags & SN_ERR_MASK)) |
| 3714 | t->flags |= SN_ERR_PRXCOND; |
| 3715 | if (!(t->flags & SN_FINST_MASK)) |
| 3716 | t->flags |= SN_FINST_R; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3717 | return 1; |
| 3718 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3719 | else if (t->res_cr == RES_ERROR || t->res_cr == RES_NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3720 | /* read error, or last read : give up. */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3721 | tv_eternity(&t->crexpire); |
| 3722 | fd_delete(t->cli_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3723 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3724 | if (!(t->flags & SN_ERR_MASK)) |
| 3725 | t->flags |= SN_ERR_CLICL; |
| 3726 | if (!(t->flags & SN_FINST_MASK)) |
| 3727 | t->flags |= SN_FINST_R; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3728 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3729 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3730 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 3731 | |
| 3732 | /* read timeout : give up with an error message. |
| 3733 | */ |
| 3734 | t->logs.status = 408; |
| 3735 | client_retnclose(t, t->proxy->errmsg.len408, t->proxy->errmsg.msg408); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3736 | if (!(t->flags & SN_ERR_MASK)) |
| 3737 | t->flags |= SN_ERR_CLITO; |
| 3738 | if (!(t->flags & SN_FINST_MASK)) |
| 3739 | t->flags |= SN_FINST_R; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3740 | return 1; |
| 3741 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3742 | |
| 3743 | return t->cli_state != CL_STHEADERS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3744 | } |
| 3745 | else if (c == CL_STDATA) { |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 3746 | process_data: |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 3747 | /* FIXME: this error handling is partly buggy because we always report |
| 3748 | * a 'DATA' phase while we don't know if the server was in IDLE, CONN |
| 3749 | * or HEADER phase. BTW, it's not logical to expire the client while |
| 3750 | * we're waiting for the server to connect. |
| 3751 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3752 | /* read or write error */ |
| 3753 | if (t->res_cw == RES_ERROR || t->res_cr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3754 | tv_eternity(&t->crexpire); |
| 3755 | tv_eternity(&t->cwexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3756 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3757 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3758 | if (!(t->flags & SN_ERR_MASK)) |
| 3759 | t->flags |= SN_ERR_CLICL; |
| 3760 | if (!(t->flags & SN_FINST_MASK)) |
| 3761 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3762 | return 1; |
| 3763 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3764 | /* last read, or end of server write */ |
| 3765 | 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] | 3766 | FD_CLR(t->cli_fd, StaticReadEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3767 | tv_eternity(&t->crexpire); |
| 3768 | shutdown(t->cli_fd, SHUT_RD); |
| 3769 | t->cli_state = CL_STSHUTR; |
| 3770 | return 1; |
| 3771 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3772 | /* last server read and buffer empty */ |
| 3773 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3774 | FD_CLR(t->cli_fd, StaticWriteEvent); |
| 3775 | tv_eternity(&t->cwexpire); |
| 3776 | shutdown(t->cli_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3777 | /* We must ensure that the read part is still alive when switching |
| 3778 | * to shutw */ |
| 3779 | FD_SET(t->cli_fd, StaticReadEvent); |
| 3780 | if (t->proxy->clitimeout) |
| 3781 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3782 | t->cli_state = CL_STSHUTW; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3783 | //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] | 3784 | return 1; |
| 3785 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3786 | /* read timeout */ |
| 3787 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 3788 | FD_CLR(t->cli_fd, StaticReadEvent); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3789 | tv_eternity(&t->crexpire); |
| 3790 | shutdown(t->cli_fd, SHUT_RD); |
| 3791 | t->cli_state = CL_STSHUTR; |
| 3792 | if (!(t->flags & SN_ERR_MASK)) |
| 3793 | t->flags |= SN_ERR_CLITO; |
| 3794 | if (!(t->flags & SN_FINST_MASK)) |
| 3795 | t->flags |= SN_FINST_D; |
| 3796 | return 1; |
| 3797 | } |
| 3798 | /* write timeout */ |
| 3799 | else if (tv_cmp2_ms(&t->cwexpire, &now) <= 0) { |
| 3800 | FD_CLR(t->cli_fd, StaticWriteEvent); |
| 3801 | tv_eternity(&t->cwexpire); |
| 3802 | shutdown(t->cli_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3803 | /* We must ensure that the read part is still alive when switching |
| 3804 | * to shutw */ |
| 3805 | FD_SET(t->cli_fd, StaticReadEvent); |
| 3806 | if (t->proxy->clitimeout) |
| 3807 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 3808 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3809 | t->cli_state = CL_STSHUTW; |
| 3810 | if (!(t->flags & SN_ERR_MASK)) |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3811 | t->flags |= SN_ERR_CLITO; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3812 | if (!(t->flags & SN_FINST_MASK)) |
| 3813 | t->flags |= SN_FINST_D; |
| 3814 | return 1; |
| 3815 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3816 | |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 3817 | if (req->l >= req->rlim - req->data) { |
| 3818 | /* no room to read more data */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3819 | if (FD_ISSET(t->cli_fd, StaticReadEvent)) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3820 | /* stop reading until we get some space */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3821 | FD_CLR(t->cli_fd, StaticReadEvent); |
| 3822 | tv_eternity(&t->crexpire); |
| 3823 | } |
| 3824 | } |
| 3825 | else { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3826 | /* there's still some space in the buffer */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3827 | if (! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 3828 | FD_SET(t->cli_fd, StaticReadEvent); |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 3829 | if (!t->proxy->clitimeout || |
| 3830 | (t->srv_state < SV_STDATA && t->proxy->srvtimeout)) |
| 3831 | /* If the client has no timeout, or if the server not ready yet, and we |
| 3832 | * know for sure that it can expire, then it's cleaner to disable the |
| 3833 | * timeout on the client side so that too low values cannot make the |
| 3834 | * sessions abort too early. |
| 3835 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3836 | tv_eternity(&t->crexpire); |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 3837 | else |
| 3838 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3839 | } |
| 3840 | } |
| 3841 | |
| 3842 | if ((rep->l == 0) || |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 3843 | ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3844 | if (FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 3845 | FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */ |
| 3846 | tv_eternity(&t->cwexpire); |
| 3847 | } |
| 3848 | } |
| 3849 | else { /* buffer not empty */ |
| 3850 | if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 3851 | FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3852 | if (t->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3853 | tv_delayfrom(&t->cwexpire, &now, t->proxy->clitimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3854 | /* FIXME: to avoid the client to read-time-out during writes, we refresh it */ |
| 3855 | t->crexpire = t->cwexpire; |
| 3856 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3857 | else |
| 3858 | tv_eternity(&t->cwexpire); |
| 3859 | } |
| 3860 | } |
| 3861 | return 0; /* other cases change nothing */ |
| 3862 | } |
| 3863 | else if (c == CL_STSHUTR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3864 | if (t->res_cw == RES_ERROR) { |
| 3865 | tv_eternity(&t->cwexpire); |
| 3866 | fd_delete(t->cli_fd); |
| 3867 | t->cli_state = CL_STCLOSE; |
| 3868 | if (!(t->flags & SN_ERR_MASK)) |
| 3869 | t->flags |= SN_ERR_CLICL; |
| 3870 | if (!(t->flags & SN_FINST_MASK)) |
| 3871 | t->flags |= SN_FINST_D; |
| 3872 | return 1; |
| 3873 | } |
| 3874 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3875 | tv_eternity(&t->cwexpire); |
| 3876 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3877 | t->cli_state = CL_STCLOSE; |
| 3878 | return 1; |
| 3879 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3880 | else if (tv_cmp2_ms(&t->cwexpire, &now) <= 0) { |
| 3881 | tv_eternity(&t->cwexpire); |
| 3882 | fd_delete(t->cli_fd); |
| 3883 | t->cli_state = CL_STCLOSE; |
| 3884 | if (!(t->flags & SN_ERR_MASK)) |
| 3885 | t->flags |= SN_ERR_CLITO; |
| 3886 | if (!(t->flags & SN_FINST_MASK)) |
| 3887 | t->flags |= SN_FINST_D; |
| 3888 | return 1; |
| 3889 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3890 | else if ((rep->l == 0) || |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3891 | ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3892 | if (FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 3893 | FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */ |
| 3894 | tv_eternity(&t->cwexpire); |
| 3895 | } |
| 3896 | } |
| 3897 | else { /* buffer not empty */ |
| 3898 | if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 3899 | FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3900 | if (t->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3901 | tv_delayfrom(&t->cwexpire, &now, t->proxy->clitimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3902 | /* FIXME: to avoid the client to read-time-out during writes, we refresh it */ |
| 3903 | t->crexpire = t->cwexpire; |
| 3904 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3905 | else |
| 3906 | tv_eternity(&t->cwexpire); |
| 3907 | } |
| 3908 | } |
| 3909 | return 0; |
| 3910 | } |
| 3911 | else if (c == CL_STSHUTW) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3912 | if (t->res_cr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3913 | tv_eternity(&t->crexpire); |
| 3914 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3915 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3916 | if (!(t->flags & SN_ERR_MASK)) |
| 3917 | t->flags |= SN_ERR_CLICL; |
| 3918 | if (!(t->flags & SN_FINST_MASK)) |
| 3919 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3920 | return 1; |
| 3921 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3922 | else if (t->res_cr == RES_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
| 3923 | tv_eternity(&t->crexpire); |
| 3924 | fd_delete(t->cli_fd); |
| 3925 | t->cli_state = CL_STCLOSE; |
| 3926 | return 1; |
| 3927 | } |
| 3928 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 3929 | tv_eternity(&t->crexpire); |
| 3930 | fd_delete(t->cli_fd); |
| 3931 | t->cli_state = CL_STCLOSE; |
| 3932 | if (!(t->flags & SN_ERR_MASK)) |
| 3933 | t->flags |= SN_ERR_CLITO; |
| 3934 | if (!(t->flags & SN_FINST_MASK)) |
| 3935 | t->flags |= SN_FINST_D; |
| 3936 | return 1; |
| 3937 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3938 | else if (req->l >= req->rlim - req->data) { |
| 3939 | /* no room to read more data */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3940 | |
| 3941 | /* FIXME-20050705: is it possible for a client to maintain a session |
| 3942 | * after the timeout by sending more data after it receives a close ? |
| 3943 | */ |
| 3944 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3945 | if (FD_ISSET(t->cli_fd, StaticReadEvent)) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3946 | /* stop reading until we get some space */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3947 | FD_CLR(t->cli_fd, StaticReadEvent); |
| 3948 | tv_eternity(&t->crexpire); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3949 | //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] | 3950 | } |
| 3951 | } |
| 3952 | else { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3953 | /* there's still some space in the buffer */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3954 | if (! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 3955 | FD_SET(t->cli_fd, StaticReadEvent); |
| 3956 | if (t->proxy->clitimeout) |
| 3957 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 3958 | else |
| 3959 | tv_eternity(&t->crexpire); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3960 | //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] | 3961 | } |
| 3962 | } |
| 3963 | return 0; |
| 3964 | } |
| 3965 | else { /* CL_STCLOSE: nothing to do */ |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 3966 | 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] | 3967 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 3968 | 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] | 3969 | write(1, trash, len); |
| 3970 | } |
| 3971 | return 0; |
| 3972 | } |
| 3973 | return 0; |
| 3974 | } |
| 3975 | |
| 3976 | |
| 3977 | /* |
| 3978 | * manages the server FSM and its socket. It returns 1 if a state has changed |
| 3979 | * (and a resync may be needed), 0 else. |
| 3980 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3981 | int process_srv(struct session *t) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3982 | int s = t->srv_state; |
| 3983 | int c = t->cli_state; |
| 3984 | struct buffer *req = t->req; |
| 3985 | struct buffer *rep = t->rep; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3986 | appsess *asession_temp = NULL; |
| 3987 | appsess local_asession; |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3988 | int conn_err; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3989 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3990 | #ifdef DEBUG_FULL |
| 3991 | fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 3992 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3993 | //fprintf(stderr,"process_srv: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s, |
| 3994 | //FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 3995 | //FD_ISSET(t->srv_fd, StaticReadEvent), FD_ISSET(t->srv_fd, StaticWriteEvent) |
| 3996 | //); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3997 | if (s == SV_STIDLE) { |
| 3998 | if (c == CL_STHEADERS) |
| 3999 | return 0; /* stay in idle, waiting for data to reach the client side */ |
| 4000 | else if (c == CL_STCLOSE || |
| 4001 | c == CL_STSHUTW || |
| 4002 | (c == CL_STSHUTR && t->req->l == 0)) { /* give up */ |
| 4003 | tv_eternity(&t->cnexpire); |
| 4004 | t->srv_state = SV_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4005 | if (!(t->flags & SN_ERR_MASK)) |
| 4006 | t->flags |= SN_ERR_CLICL; |
| 4007 | if (!(t->flags & SN_FINST_MASK)) |
| 4008 | t->flags |= SN_FINST_C; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4009 | return 1; |
| 4010 | } |
| 4011 | else { /* go to SV_STCONN */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4012 | /* initiate a connection to the server */ |
| 4013 | conn_err = connect_server(t); |
| 4014 | if (conn_err == SN_ERR_NONE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4015 | //fprintf(stderr,"0: c=%d, s=%d\n", c, s); |
| 4016 | t->srv_state = SV_STCONN; |
| 4017 | } |
| 4018 | else { /* try again */ |
| 4019 | while (t->conn_retries-- > 0) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4020 | if ((t->proxy->options & PR_O_REDISP) && (t->conn_retries == 0)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4021 | t->flags &= ~SN_DIRECT; /* ignore cookie and force to use the dispatcher */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4022 | 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] | 4023 | if ((t->flags & SN_CK_MASK) == SN_CK_VALID) { |
| 4024 | t->flags &= ~SN_CK_MASK; |
| 4025 | t->flags |= SN_CK_DOWN; |
| 4026 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4027 | } |
| 4028 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4029 | conn_err = connect_server(t); |
| 4030 | if (conn_err == SN_ERR_NONE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4031 | t->srv_state = SV_STCONN; |
| 4032 | break; |
| 4033 | } |
| 4034 | } |
| 4035 | if (t->conn_retries < 0) { |
| 4036 | /* if conn_retries < 0 or other error, let's abort */ |
| 4037 | tv_eternity(&t->cnexpire); |
| 4038 | t->srv_state = SV_STCLOSE; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4039 | t->logs.status = 503; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4040 | if (t->proxy->mode == PR_MODE_HTTP) |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4041 | client_return(t, t->proxy->errmsg.len503, t->proxy->errmsg.msg503); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4042 | if (!(t->flags & SN_ERR_MASK)) |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4043 | t->flags |= conn_err; /* report the precise connect() error */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4044 | if (!(t->flags & SN_FINST_MASK)) |
| 4045 | t->flags |= SN_FINST_C; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4046 | } |
| 4047 | } |
| 4048 | return 1; |
| 4049 | } |
| 4050 | } |
| 4051 | else if (s == SV_STCONN) { /* connection in progress */ |
| 4052 | 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] | 4053 | //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] | 4054 | return 0; /* nothing changed */ |
| 4055 | } |
| 4056 | else if (t->res_sw == RES_SILENT || t->res_sw == RES_ERROR) { |
| 4057 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 4058 | /* timeout, connect error or first write error */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4059 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4060 | fd_delete(t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4061 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4062 | t->conn_retries--; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4063 | if (t->conn_retries >= 0) { |
| 4064 | if ((t->proxy->options & PR_O_REDISP) && (t->conn_retries == 0)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4065 | t->flags &= ~SN_DIRECT; /* ignore cookie and force to use the dispatcher */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4066 | 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] | 4067 | if ((t->flags & SN_CK_MASK) == SN_CK_VALID) { |
| 4068 | t->flags &= ~SN_CK_MASK; |
| 4069 | t->flags |= SN_CK_DOWN; |
| 4070 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4071 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4072 | conn_err = connect_server(t); |
| 4073 | if (conn_err == SN_ERR_NONE) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4074 | return 0; /* no state changed */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4075 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4076 | else if (t->res_sw == RES_SILENT) |
| 4077 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 4078 | else |
| 4079 | conn_err = SN_ERR_SRVCL; // it was a connect error. |
| 4080 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4081 | /* if conn_retries < 0 or other error, let's abort */ |
| 4082 | tv_eternity(&t->cnexpire); |
| 4083 | t->srv_state = SV_STCLOSE; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4084 | t->logs.status = 503; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4085 | if (t->proxy->mode == PR_MODE_HTTP) |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4086 | client_return(t, t->proxy->errmsg.len503, t->proxy->errmsg.msg503); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4087 | if (!(t->flags & SN_ERR_MASK)) |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4088 | t->flags |= conn_err; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4089 | if (!(t->flags & SN_FINST_MASK)) |
| 4090 | t->flags |= SN_FINST_C; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4091 | return 1; |
| 4092 | } |
| 4093 | else { /* no error or write 0 */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4094 | t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4095 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4096 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4097 | if (req->l == 0) /* nothing to write */ { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4098 | FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4099 | tv_eternity(&t->swexpire); |
| 4100 | } else /* need the right to write */ { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4101 | FD_SET(t->srv_fd, StaticWriteEvent); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4102 | if (t->proxy->srvtimeout) { |
| 4103 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
| 4104 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 4105 | t->srexpire = t->swexpire; |
| 4106 | } |
| 4107 | else |
| 4108 | tv_eternity(&t->swexpire); |
| 4109 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4110 | |
| 4111 | if (t->proxy->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 4112 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4113 | if (t->proxy->srvtimeout) |
| 4114 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4115 | else |
| 4116 | tv_eternity(&t->srexpire); |
| 4117 | |
| 4118 | t->srv_state = SV_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4119 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4120 | |
| 4121 | /* if the user wants to log as soon as possible, without counting |
| 4122 | bytes from the server, then this is the right moment. */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 4123 | if (t->proxy->to_log && !(t->logs.logwait & LW_BYTES)) { |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4124 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 4125 | sess_log(t); |
| 4126 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4127 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4128 | else { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4129 | t->srv_state = SV_STHEADERS; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4130 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
| 4131 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4132 | tv_eternity(&t->cnexpire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4133 | return 1; |
| 4134 | } |
| 4135 | } |
| 4136 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4137 | /* now parse the partial (or complete) headers */ |
| 4138 | while (rep->lr < rep->r) { /* this loop only sees one header at each iteration */ |
| 4139 | char *ptr; |
| 4140 | int delete_header; |
| 4141 | |
| 4142 | ptr = rep->lr; |
| 4143 | |
| 4144 | /* look for the end of the current header */ |
| 4145 | while (ptr < rep->r && *ptr != '\n' && *ptr != '\r') |
| 4146 | ptr++; |
| 4147 | |
| 4148 | if (ptr == rep->h) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4149 | int line, len; |
| 4150 | |
| 4151 | /* we can only get here after an end of headers */ |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4152 | |
| 4153 | /* first, we'll block if security checks have caught nasty things */ |
| 4154 | if (t->flags & SN_CACHEABLE) { |
| 4155 | if ((t->flags & SN_CACHE_COOK) && |
| 4156 | (t->flags & SN_SCK_ANY) && |
| 4157 | (t->proxy->options & PR_O_CHK_CACHE)) { |
| 4158 | |
| 4159 | /* we're in presence of a cacheable response containing |
| 4160 | * a set-cookie header. We'll block it as requested by |
| 4161 | * the 'checkcache' option, and send an alert. |
| 4162 | */ |
| 4163 | tv_eternity(&t->srexpire); |
| 4164 | tv_eternity(&t->swexpire); |
| 4165 | fd_delete(t->srv_fd); |
| 4166 | t->srv_state = SV_STCLOSE; |
| 4167 | t->logs.status = 502; |
| 4168 | client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502); |
| 4169 | if (!(t->flags & SN_ERR_MASK)) |
| 4170 | t->flags |= SN_ERR_PRXCOND; |
| 4171 | if (!(t->flags & SN_FINST_MASK)) |
| 4172 | t->flags |= SN_FINST_H; |
| 4173 | |
| 4174 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", t->proxy->id, t->srv->id); |
| 4175 | send_log(t->proxy, LOG_ALERT, "Blocking cacheable cookie in response from instance %s, server %s.\n", t->proxy->id, t->srv->id); |
| 4176 | |
| 4177 | return 1; |
| 4178 | } |
| 4179 | } |
| 4180 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4181 | /* next, we'll block if an 'rspideny' or 'rspdeny' filter matched */ |
| 4182 | if (t->flags & SN_SVDENY) { |
| 4183 | tv_eternity(&t->srexpire); |
| 4184 | tv_eternity(&t->swexpire); |
| 4185 | fd_delete(t->srv_fd); |
| 4186 | t->srv_state = SV_STCLOSE; |
| 4187 | t->logs.status = 502; |
| 4188 | client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502); |
| 4189 | if (!(t->flags & SN_ERR_MASK)) |
| 4190 | t->flags |= SN_ERR_PRXCOND; |
| 4191 | if (!(t->flags & SN_FINST_MASK)) |
| 4192 | t->flags |= SN_FINST_H; |
| 4193 | return 1; |
| 4194 | } |
| 4195 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4196 | /* we'll have something else to do here : add new headers ... */ |
| 4197 | |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 4198 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_INS) && |
| 4199 | (!(t->proxy->options & PR_O_COOK_POST) || (t->flags & SN_POST))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4200 | /* 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] | 4201 | * insert a set-cookie here, except if we want to insert only on POST |
| 4202 | * requests and this one isn't. |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4203 | */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4204 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/\r\n", |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4205 | t->proxy->cookie_name, |
| 4206 | t->srv->cookie ? t->srv->cookie : ""); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4207 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4208 | t->flags |= SN_SCK_INSERTED; |
| 4209 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4210 | /* Here, we will tell an eventual cache on the client side that we don't |
| 4211 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 4212 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 4213 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 4214 | */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4215 | if (t->proxy->options & PR_O_COOK_NOC) |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4216 | //len += sprintf(newhdr + len, "Cache-control: no-cache=\"set-cookie\"\r\n"); |
| 4217 | len += sprintf(trash + len, "Cache-control: private\r\n"); |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 4218 | |
| 4219 | if (rep->data + rep->l < rep->h) |
| 4220 | /* The data has been stolen, we will crash cleanly instead of corrupting memory */ |
| 4221 | *(int *)0 = 0; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4222 | buffer_replace2(rep, rep->h, rep->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4223 | } |
| 4224 | |
| 4225 | /* headers to be added */ |
| 4226 | for (line = 0; line < t->proxy->nb_rspadd; line++) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4227 | len = sprintf(trash, "%s\r\n", t->proxy->rsp_add[line]); |
| 4228 | buffer_replace2(rep, rep->h, rep->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4229 | } |
| 4230 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4231 | /* add a "connection: close" line if needed */ |
| 4232 | if (t->proxy->options & PR_O_HTTP_CLOSE) |
| 4233 | buffer_replace2(rep, rep->h, rep->h, "Connection: close\r\n", 19); |
| 4234 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4235 | t->srv_state = SV_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4236 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4237 | t->logs.t_data = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4238 | |
Willy TARREAU | 767ba71 | 2006-03-01 22:40:50 +0100 | [diff] [blame] | 4239 | /* client connection already closed or option 'httpclose' required : |
| 4240 | * we close the server's outgoing connection right now. |
| 4241 | */ |
| 4242 | if ((req->l == 0) && |
| 4243 | (c == CL_STSHUTR || c == CL_STCLOSE || t->proxy->options & PR_O_FORCE_CLO)) { |
| 4244 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4245 | tv_eternity(&t->swexpire); |
| 4246 | |
| 4247 | /* We must ensure that the read part is still alive when switching |
| 4248 | * to shutw */ |
| 4249 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4250 | if (t->proxy->srvtimeout) |
| 4251 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4252 | |
| 4253 | shutdown(t->srv_fd, SHUT_WR); |
| 4254 | t->srv_state = SV_STSHUTW; |
| 4255 | } |
| 4256 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4257 | /* if the user wants to log as soon as possible, without counting |
| 4258 | bytes from the server, then this is the right moment. */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 4259 | if (t->proxy->to_log && !(t->logs.logwait & LW_BYTES)) { |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4260 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 4261 | t->logs.bytes = rep->h - rep->data; |
| 4262 | sess_log(t); |
| 4263 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4264 | break; |
| 4265 | } |
| 4266 | |
| 4267 | /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */ |
| 4268 | if (ptr > rep->r - 2) { |
| 4269 | /* this is a partial header, let's wait for more to come */ |
| 4270 | rep->lr = ptr; |
| 4271 | break; |
| 4272 | } |
| 4273 | |
| 4274 | // fprintf(stderr,"h=%p, ptr=%p, lr=%p, r=%p, *h=", rep->h, ptr, rep->lr, rep->r); |
| 4275 | // write(2, rep->h, ptr - rep->h); fprintf(stderr,"\n"); |
| 4276 | |
| 4277 | /* now we know that *ptr is either \r or \n, |
| 4278 | * and that there are at least 1 char after it. |
| 4279 | */ |
| 4280 | if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n')) |
| 4281 | rep->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */ |
| 4282 | else |
| 4283 | rep->lr = ptr + 2; /* \r\n or \n\r */ |
| 4284 | |
| 4285 | /* |
| 4286 | * now we know that we have a full header ; we can do whatever |
| 4287 | * we want with these pointers : |
| 4288 | * rep->h = beginning of header |
| 4289 | * ptr = end of header (first \r or \n) |
| 4290 | * rep->lr = beginning of next line (next rep->h) |
| 4291 | * rep->r = end of data (not used at this stage) |
| 4292 | */ |
| 4293 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4294 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4295 | if (t->logs.status == -1) { |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4296 | t->logs.logwait &= ~LW_RESP; |
| 4297 | t->logs.status = atoi(rep->h + 9); |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4298 | switch (t->logs.status) { |
| 4299 | case 200: |
| 4300 | case 203: |
| 4301 | case 206: |
| 4302 | case 300: |
| 4303 | case 301: |
| 4304 | case 410: |
| 4305 | /* RFC2616 @13.4: |
| 4306 | * "A response received with a status code of |
| 4307 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4308 | * by a cache (...) unless a cache-control |
| 4309 | * directive prohibits caching." |
| 4310 | * |
| 4311 | * RFC2616 @9.5: POST method : |
| 4312 | * "Responses to this method are not cacheable, |
| 4313 | * unless the response includes appropriate |
| 4314 | * Cache-Control or Expires header fields." |
| 4315 | */ |
| 4316 | if ((!t->flags & SN_POST) && (t->proxy->options & PR_O_CHK_CACHE)) |
| 4317 | t->flags |= SN_CACHEABLE | SN_CACHE_COOK; |
| 4318 | break; |
| 4319 | default: |
| 4320 | break; |
| 4321 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 4322 | } |
| 4323 | else if (t->logs.logwait & LW_RSPHDR) { |
| 4324 | struct cap_hdr *h; |
| 4325 | int len; |
| 4326 | for (h = t->proxy->rsp_cap; h; h = h->next) { |
| 4327 | if ((h->namelen + 2 <= ptr - rep->h) && |
| 4328 | (rep->h[h->namelen] == ':') && |
| 4329 | (strncasecmp(rep->h, h->name, h->namelen) == 0)) { |
| 4330 | |
| 4331 | if (t->rsp_cap[h->index] == NULL) |
| 4332 | t->rsp_cap[h->index] = pool_alloc_from(h->pool, h->len + 1); |
| 4333 | |
| 4334 | len = ptr - (rep->h + h->namelen + 2); |
| 4335 | if (len > h->len) |
| 4336 | len = h->len; |
| 4337 | |
| 4338 | memcpy(t->rsp_cap[h->index], rep->h + h->namelen + 2, len); |
| 4339 | t->rsp_cap[h->index][len]=0; |
| 4340 | } |
| 4341 | } |
| 4342 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4343 | } |
| 4344 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4345 | delete_header = 0; |
| 4346 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4347 | 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] | 4348 | int len, max; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 4349 | 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] | 4350 | max = ptr - rep->h; |
| 4351 | UBOUND(max, sizeof(trash) - len - 1); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4352 | len += strlcpy2(trash + len, rep->h, max + 1); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4353 | trash[len++] = '\n'; |
| 4354 | write(1, trash, len); |
| 4355 | } |
| 4356 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4357 | /* remove "connection: " if needed */ |
| 4358 | if (!delete_header && (t->proxy->options & PR_O_HTTP_CLOSE) |
| 4359 | && (strncasecmp(rep->h, "Connection: ", 12) == 0)) { |
| 4360 | delete_header = 1; |
| 4361 | } |
| 4362 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4363 | /* try headers regexps */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4364 | if (!delete_header && t->proxy->rsp_exp != NULL |
| 4365 | && !(t->flags & SN_SVDENY)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4366 | struct hdr_exp *exp; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4367 | char term; |
| 4368 | |
| 4369 | term = *ptr; |
| 4370 | *ptr = '\0'; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4371 | exp = t->proxy->rsp_exp; |
| 4372 | do { |
| 4373 | if (regexec(exp->preg, rep->h, MAX_MATCH, pmatch, 0) == 0) { |
| 4374 | switch (exp->action) { |
| 4375 | case ACT_ALLOW: |
| 4376 | if (!(t->flags & SN_SVDENY)) |
| 4377 | t->flags |= SN_SVALLOW; |
| 4378 | break; |
| 4379 | case ACT_REPLACE: |
| 4380 | if (!(t->flags & SN_SVDENY)) { |
| 4381 | int len = exp_replace(trash, rep->h, exp->replace, pmatch); |
| 4382 | ptr += buffer_replace2(rep, rep->h, ptr, trash, len); |
| 4383 | } |
| 4384 | break; |
| 4385 | case ACT_REMOVE: |
| 4386 | if (!(t->flags & SN_SVDENY)) |
| 4387 | delete_header = 1; |
| 4388 | break; |
| 4389 | case ACT_DENY: |
| 4390 | if (!(t->flags & SN_SVALLOW)) |
| 4391 | t->flags |= SN_SVDENY; |
| 4392 | break; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4393 | case ACT_PASS: /* we simply don't deny this one */ |
| 4394 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4395 | } |
| 4396 | break; |
| 4397 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4398 | } while ((exp = exp->next) != NULL); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4399 | *ptr = term; /* restore the string terminator */ |
| 4400 | } |
| 4401 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4402 | /* check for cache-control: or pragma: headers */ |
| 4403 | if (!delete_header && (t->flags & SN_CACHEABLE)) { |
| 4404 | if (strncasecmp(rep->h, "Pragma: no-cache", 16) == 0) |
| 4405 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4406 | else if (strncasecmp(rep->h, "Cache-control: ", 15) == 0) { |
| 4407 | if (strncasecmp(rep->h + 15, "no-cache", 8) == 0) { |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4408 | if (rep->h + 23 == ptr || rep->h[23] == ',') |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4409 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4410 | else { |
| 4411 | if (strncasecmp(rep->h + 23, "=\"set-cookie", 12) == 0 |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4412 | && (rep->h[35] == '"' || rep->h[35] == ',')) |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4413 | t->flags &= ~SN_CACHE_COOK; |
| 4414 | } |
| 4415 | } else if ((strncasecmp(rep->h + 15, "private", 7) == 0 && |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4416 | (rep->h + 22 == ptr || rep->h[22] == ',')) |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4417 | || (strncasecmp(rep->h + 15, "no-store", 8) == 0 && |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4418 | (rep->h + 23 == ptr || rep->h[23] == ','))) { |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4419 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4420 | } else if (strncasecmp(rep->h + 15, "max-age=0", 9) == 0 && |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4421 | (rep->h + 24 == ptr || rep->h[24] == ',')) { |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4422 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4423 | } else if (strncasecmp(rep->h + 15, "s-maxage=0", 10) == 0 && |
| 4424 | (rep->h + 25 == ptr || rep->h[25] == ',')) { |
| 4425 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4426 | } else if (strncasecmp(rep->h + 15, "public", 6) == 0 && |
| 4427 | (rep->h + 21 == ptr || rep->h[21] == ',')) { |
| 4428 | t->flags |= SN_CACHEABLE | SN_CACHE_COOK; |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4429 | } |
| 4430 | } |
| 4431 | } |
| 4432 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4433 | /* check for server cookies */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4434 | if (!delete_header /*&& (t->proxy->options & PR_O_COOK_ANY)*/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4435 | && (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] | 4436 | && (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4437 | char *p1, *p2, *p3, *p4; |
| 4438 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4439 | t->flags |= SN_SCK_ANY; |
| 4440 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4441 | p1 = rep->h + 12; /* first char after 'Set-Cookie: ' */ |
| 4442 | |
| 4443 | while (p1 < ptr) { /* in fact, we'll break after the first cookie */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 4444 | while (p1 < ptr && (isspace((int)*p1))) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4445 | p1++; |
| 4446 | |
| 4447 | if (p1 == ptr || *p1 == ';') /* end of cookie */ |
| 4448 | break; |
| 4449 | |
| 4450 | /* p1 is at the beginning of the cookie name */ |
| 4451 | p2 = p1; |
| 4452 | |
| 4453 | while (p2 < ptr && *p2 != '=' && *p2 != ';') |
| 4454 | p2++; |
| 4455 | |
| 4456 | if (p2 == ptr || *p2 == ';') /* next cookie */ |
| 4457 | break; |
| 4458 | |
| 4459 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4460 | if (p3 == ptr) |
| 4461 | break; |
| 4462 | |
| 4463 | p4 = p3; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 4464 | while (p4 < ptr && !isspace((int)*p4) && *p4 != ';') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4465 | p4++; |
| 4466 | |
| 4467 | /* here, we have the cookie name between p1 and p2, |
| 4468 | * and its value between p3 and p4. |
| 4469 | * we can process it. |
| 4470 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4471 | |
| 4472 | /* first, let's see if we want to capture it */ |
| 4473 | if (t->proxy->capture_name != NULL && |
| 4474 | t->logs.srv_cookie == NULL && |
| 4475 | (p4 - p1 >= t->proxy->capture_namelen) && |
| 4476 | memcmp(p1, t->proxy->capture_name, t->proxy->capture_namelen) == 0) { |
| 4477 | int log_len = p4 - p1; |
| 4478 | |
| 4479 | if ((t->logs.srv_cookie = pool_alloc(capture)) == NULL) { |
| 4480 | Alert("HTTP logging : out of memory.\n"); |
| 4481 | } |
| 4482 | |
| 4483 | if (log_len > t->proxy->capture_len) |
| 4484 | log_len = t->proxy->capture_len; |
| 4485 | memcpy(t->logs.srv_cookie, p1, log_len); |
| 4486 | t->logs.srv_cookie[log_len] = 0; |
| 4487 | } |
| 4488 | |
| 4489 | if ((p2 - p1 == t->proxy->cookie_len) && (t->proxy->cookie_name != NULL) && |
| 4490 | (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4491 | /* Cool... it's the right one */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4492 | t->flags |= SN_SCK_SEEN; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4493 | |
| 4494 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4495 | * this occurrence because we'll insert another one later. |
| 4496 | * We'll delete it too if the "indirect" option is set and we're in |
| 4497 | * a direct access. */ |
| 4498 | if (((t->srv) && (t->proxy->options & PR_O_COOK_INS)) || |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4499 | ((t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_IND))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4500 | /* this header must be deleted */ |
| 4501 | delete_header = 1; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4502 | t->flags |= SN_SCK_DELETED; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4503 | } |
| 4504 | else if ((t->srv) && (t->proxy->options & PR_O_COOK_RW)) { |
| 4505 | /* replace bytes p3->p4 with the cookie name associated |
| 4506 | * with this server since we know it. |
| 4507 | */ |
| 4508 | buffer_replace2(rep, p3, p4, t->srv->cookie, t->srv->cklen); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4509 | t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4510 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 4511 | else if ((t->srv) && (t->proxy->options & PR_O_COOK_PFX)) { |
| 4512 | /* insert the cookie name associated with this server |
| 4513 | * before existing cookie, and insert a delimitor between them.. |
| 4514 | */ |
| 4515 | buffer_replace2(rep, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4516 | p3[t->srv->cklen] = COOKIE_DELIM; |
| 4517 | t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED; |
| 4518 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4519 | break; |
| 4520 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4521 | |
| 4522 | /* first, let's see if the cookie is our appcookie*/ |
| 4523 | if ((t->proxy->appsession_name != NULL) && |
| 4524 | (memcmp(p1, t->proxy->appsession_name, p2 - p1) == 0)) { |
| 4525 | |
| 4526 | /* Cool... it's the right one */ |
| 4527 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4528 | size_t server_id_len = strlen(t->srv->id) + 1; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4529 | asession_temp = &local_asession; |
| 4530 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4531 | 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] | 4532 | Alert("Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 4533 | send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 4534 | } |
| 4535 | memcpy(asession_temp->sessid, p3, t->proxy->appsession_len); |
| 4536 | asession_temp->sessid[t->proxy->appsession_len] = 0; |
| 4537 | asession_temp->serverid = NULL; |
| 4538 | |
| 4539 | /* only do insert, if lookup fails */ |
| 4540 | if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *) &asession_temp) != 0) { |
| 4541 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 4542 | Alert("Not enought Memory process_srv():asession:calloc().\n"); |
| 4543 | send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession:calloc().\n"); |
| 4544 | return 0; |
| 4545 | } |
| 4546 | asession_temp->sessid = local_asession.sessid; |
| 4547 | asession_temp->serverid = local_asession.serverid; |
| 4548 | chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4549 | }/* end if (chtbl_lookup()) */ |
| 4550 | else { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4551 | /* free wasted memory */ |
| 4552 | pool_free_to(apools.sessid, local_asession.sessid); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4553 | } /* end else from if (chtbl_lookup()) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4554 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4555 | if (asession_temp->serverid == NULL) { |
| 4556 | 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] | 4557 | Alert("Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 4558 | send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 4559 | } |
| 4560 | asession_temp->serverid[0] = '\0'; |
| 4561 | } |
| 4562 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4563 | if (asession_temp->serverid[0] == '\0') |
| 4564 | memcpy(asession_temp->serverid,t->srv->id,server_id_len); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4565 | |
| 4566 | tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout); |
| 4567 | |
| 4568 | #if defined(DEBUG_HASH) |
| 4569 | print_table(&(t->proxy->htbl_proxy)); |
| 4570 | #endif |
| 4571 | break; |
| 4572 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4573 | else { |
| 4574 | // fprintf(stderr,"Ignoring unknown cookie : "); |
| 4575 | // write(2, p1, p2-p1); |
| 4576 | // fprintf(stderr," = "); |
| 4577 | // write(2, p3, p4-p3); |
| 4578 | // fprintf(stderr,"\n"); |
| 4579 | } |
| 4580 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4581 | } /* we're now at the end of the cookie value */ |
| 4582 | } /* end of cookie processing */ |
| 4583 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4584 | /* check for any set-cookie in case we check for cacheability */ |
| 4585 | if (!delete_header && !(t->flags & SN_SCK_ANY) && |
| 4586 | (t->proxy->options & PR_O_CHK_CACHE) && |
| 4587 | (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) { |
| 4588 | t->flags |= SN_SCK_ANY; |
| 4589 | } |
| 4590 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4591 | /* let's look if we have to delete this header */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4592 | if (delete_header && !(t->flags & SN_SVDENY)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4593 | buffer_replace2(rep, rep->h, rep->lr, "", 0); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4594 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4595 | rep->h = rep->lr; |
| 4596 | } /* while (rep->lr < rep->r) */ |
| 4597 | |
| 4598 | /* end of header processing (even if incomplete) */ |
| 4599 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4600 | if ((rep->l < rep->rlim - rep->data) && ! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 4601 | /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer |
| 4602 | * full. We cannot loop here since event_srv_read will disable it only if |
| 4603 | * rep->l == rlim-data |
| 4604 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4605 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4606 | if (t->proxy->srvtimeout) |
| 4607 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4608 | else |
| 4609 | tv_eternity(&t->srexpire); |
| 4610 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4611 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4612 | /* read error, write error */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4613 | if (t->res_sw == RES_ERROR || t->res_sr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4614 | tv_eternity(&t->srexpire); |
| 4615 | tv_eternity(&t->swexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4616 | fd_delete(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4617 | t->srv_state = SV_STCLOSE; |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 4618 | t->logs.status = 502; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4619 | client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4620 | if (!(t->flags & SN_ERR_MASK)) |
| 4621 | t->flags |= SN_ERR_SRVCL; |
| 4622 | if (!(t->flags & SN_FINST_MASK)) |
| 4623 | t->flags |= SN_FINST_H; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4624 | return 1; |
| 4625 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4626 | /* end of client write or end of server read. |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4627 | * since we are in header mode, if there's no space left for headers, we |
| 4628 | * won't be able to free more later, so the session will never terminate. |
| 4629 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4630 | 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] | 4631 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4632 | tv_eternity(&t->srexpire); |
| 4633 | shutdown(t->srv_fd, SHUT_RD); |
| 4634 | t->srv_state = SV_STSHUTR; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4635 | //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] | 4636 | return 1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4637 | } |
| 4638 | /* read timeout : return a 504 to the client. |
| 4639 | */ |
| 4640 | else if (FD_ISSET(t->srv_fd, StaticReadEvent) && tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 4641 | tv_eternity(&t->srexpire); |
| 4642 | tv_eternity(&t->swexpire); |
| 4643 | fd_delete(t->srv_fd); |
| 4644 | t->srv_state = SV_STCLOSE; |
| 4645 | t->logs.status = 504; |
| 4646 | client_return(t, t->proxy->errmsg.len504, t->proxy->errmsg.msg504); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4647 | if (!(t->flags & SN_ERR_MASK)) |
| 4648 | t->flags |= SN_ERR_SRVTO; |
| 4649 | if (!(t->flags & SN_FINST_MASK)) |
| 4650 | t->flags |= SN_FINST_H; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4651 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4652 | |
| 4653 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4654 | /* last client read and buffer empty */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4655 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 4656 | * client shuts read too early, because we may still have |
| 4657 | * some work to do on the headers. |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4658 | * The side-effect is that if the client completely closes its |
| 4659 | * connection during SV_STHEADER, the connection to the server |
| 4660 | * is kept until a response comes back or the timeout is reached. |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4661 | */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4662 | else if ((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && (req->l == 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4663 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4664 | tv_eternity(&t->swexpire); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4665 | |
| 4666 | /* We must ensure that the read part is still alive when switching |
| 4667 | * to shutw */ |
| 4668 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4669 | if (t->proxy->srvtimeout) |
| 4670 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4671 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4672 | shutdown(t->srv_fd, SHUT_WR); |
| 4673 | t->srv_state = SV_STSHUTW; |
| 4674 | return 1; |
| 4675 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4676 | /* write timeout */ |
| 4677 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 4678 | * client shuts read too early, because we may still have |
| 4679 | * some work to do on the headers. |
| 4680 | */ |
| 4681 | else if (FD_ISSET(t->srv_fd, StaticWriteEvent) && tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
| 4682 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4683 | tv_eternity(&t->swexpire); |
| 4684 | shutdown(t->srv_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4685 | /* We must ensure that the read part is still alive when switching |
| 4686 | * to shutw */ |
| 4687 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4688 | if (t->proxy->srvtimeout) |
| 4689 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4690 | |
| 4691 | /* We must ensure that the read part is still alive when switching |
| 4692 | * to shutw */ |
| 4693 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4694 | if (t->proxy->srvtimeout) |
| 4695 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4696 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4697 | t->srv_state = SV_STSHUTW; |
| 4698 | if (!(t->flags & SN_ERR_MASK)) |
| 4699 | t->flags |= SN_ERR_SRVTO; |
| 4700 | if (!(t->flags & SN_FINST_MASK)) |
| 4701 | t->flags |= SN_FINST_H; |
| 4702 | return 1; |
| 4703 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4704 | |
| 4705 | if (req->l == 0) { |
| 4706 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4707 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 4708 | tv_eternity(&t->swexpire); |
| 4709 | } |
| 4710 | } |
| 4711 | else { /* client buffer not empty */ |
| 4712 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4713 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4714 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4715 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4716 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 4717 | t->srexpire = t->swexpire; |
| 4718 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4719 | else |
| 4720 | tv_eternity(&t->swexpire); |
| 4721 | } |
| 4722 | } |
| 4723 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4724 | /* be nice with the client side which would like to send a complete header |
| 4725 | * FIXME: COMPLETELY BUGGY !!! not all headers may be processed because the client |
| 4726 | * would read all remaining data at once ! The client should not write past rep->lr |
| 4727 | * when the server is in header state. |
| 4728 | */ |
| 4729 | //return header_processed; |
| 4730 | return t->srv_state != SV_STHEADERS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4731 | } |
| 4732 | else if (s == SV_STDATA) { |
| 4733 | /* read or write error */ |
| 4734 | if (t->res_sw == RES_ERROR || t->res_sr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4735 | tv_eternity(&t->srexpire); |
| 4736 | tv_eternity(&t->swexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4737 | fd_delete(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4738 | t->srv_state = SV_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4739 | if (!(t->flags & SN_ERR_MASK)) |
| 4740 | t->flags |= SN_ERR_SRVCL; |
| 4741 | if (!(t->flags & SN_FINST_MASK)) |
| 4742 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4743 | return 1; |
| 4744 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4745 | /* last read, or end of client write */ |
| 4746 | 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] | 4747 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4748 | tv_eternity(&t->srexpire); |
| 4749 | shutdown(t->srv_fd, SHUT_RD); |
| 4750 | t->srv_state = SV_STSHUTR; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4751 | //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] | 4752 | return 1; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 4753 | } |
| 4754 | /* end of client read and no more data to send */ |
| 4755 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
| 4756 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4757 | tv_eternity(&t->swexpire); |
| 4758 | shutdown(t->srv_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4759 | /* We must ensure that the read part is still alive when switching |
| 4760 | * to shutw */ |
| 4761 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4762 | if (t->proxy->srvtimeout) |
| 4763 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4764 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 4765 | t->srv_state = SV_STSHUTW; |
| 4766 | return 1; |
| 4767 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4768 | /* read timeout */ |
| 4769 | else if (tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 4770 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4771 | tv_eternity(&t->srexpire); |
| 4772 | shutdown(t->srv_fd, SHUT_RD); |
| 4773 | t->srv_state = SV_STSHUTR; |
| 4774 | if (!(t->flags & SN_ERR_MASK)) |
| 4775 | t->flags |= SN_ERR_SRVTO; |
| 4776 | if (!(t->flags & SN_FINST_MASK)) |
| 4777 | t->flags |= SN_FINST_D; |
| 4778 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4779 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4780 | /* write timeout */ |
| 4781 | else if (tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4782 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4783 | tv_eternity(&t->swexpire); |
| 4784 | shutdown(t->srv_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4785 | /* We must ensure that the read part is still alive when switching |
| 4786 | * to shutw */ |
| 4787 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4788 | if (t->proxy->srvtimeout) |
| 4789 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4790 | t->srv_state = SV_STSHUTW; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4791 | if (!(t->flags & SN_ERR_MASK)) |
| 4792 | t->flags |= SN_ERR_SRVTO; |
| 4793 | if (!(t->flags & SN_FINST_MASK)) |
| 4794 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4795 | return 1; |
| 4796 | } |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4797 | |
| 4798 | /* recompute request time-outs */ |
| 4799 | if (req->l == 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4800 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4801 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 4802 | tv_eternity(&t->swexpire); |
| 4803 | } |
| 4804 | } |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4805 | else { /* buffer not empty, there are still data to be transferred */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4806 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4807 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4808 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4809 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4810 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 4811 | t->srexpire = t->swexpire; |
| 4812 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4813 | else |
| 4814 | tv_eternity(&t->swexpire); |
| 4815 | } |
| 4816 | } |
| 4817 | |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4818 | /* recompute response time-outs */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4819 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
| 4820 | if (FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 4821 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4822 | tv_eternity(&t->srexpire); |
| 4823 | } |
| 4824 | } |
| 4825 | else { |
| 4826 | if (! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 4827 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4828 | if (t->proxy->srvtimeout) |
| 4829 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4830 | else |
| 4831 | tv_eternity(&t->srexpire); |
| 4832 | } |
| 4833 | } |
| 4834 | |
| 4835 | return 0; /* other cases change nothing */ |
| 4836 | } |
| 4837 | else if (s == SV_STSHUTR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4838 | if (t->res_sw == RES_ERROR) { |
| 4839 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4840 | tv_eternity(&t->swexpire); |
| 4841 | fd_delete(t->srv_fd); |
| 4842 | //close(t->srv_fd); |
| 4843 | t->srv_state = SV_STCLOSE; |
| 4844 | if (!(t->flags & SN_ERR_MASK)) |
| 4845 | t->flags |= SN_ERR_SRVCL; |
| 4846 | if (!(t->flags & SN_FINST_MASK)) |
| 4847 | t->flags |= SN_FINST_D; |
| 4848 | return 1; |
| 4849 | } |
| 4850 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4851 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4852 | tv_eternity(&t->swexpire); |
| 4853 | fd_delete(t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4854 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4855 | t->srv_state = SV_STCLOSE; |
| 4856 | return 1; |
| 4857 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4858 | else if (tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
| 4859 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4860 | tv_eternity(&t->swexpire); |
| 4861 | fd_delete(t->srv_fd); |
| 4862 | //close(t->srv_fd); |
| 4863 | t->srv_state = SV_STCLOSE; |
| 4864 | if (!(t->flags & SN_ERR_MASK)) |
| 4865 | t->flags |= SN_ERR_SRVTO; |
| 4866 | if (!(t->flags & SN_FINST_MASK)) |
| 4867 | t->flags |= SN_FINST_D; |
| 4868 | return 1; |
| 4869 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4870 | else if (req->l == 0) { |
| 4871 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4872 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 4873 | tv_eternity(&t->swexpire); |
| 4874 | } |
| 4875 | } |
| 4876 | else { /* buffer not empty */ |
| 4877 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4878 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4879 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4880 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4881 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 4882 | t->srexpire = t->swexpire; |
| 4883 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4884 | else |
| 4885 | tv_eternity(&t->swexpire); |
| 4886 | } |
| 4887 | } |
| 4888 | return 0; |
| 4889 | } |
| 4890 | else if (s == SV_STSHUTW) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4891 | if (t->res_sr == RES_ERROR) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4892 | //FD_CLR(t->srv_fd, StaticReadEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4893 | tv_eternity(&t->srexpire); |
| 4894 | fd_delete(t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4895 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4896 | t->srv_state = SV_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4897 | if (!(t->flags & SN_ERR_MASK)) |
| 4898 | t->flags |= SN_ERR_SRVCL; |
| 4899 | if (!(t->flags & SN_FINST_MASK)) |
| 4900 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4901 | return 1; |
| 4902 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4903 | else if (t->res_sr == RES_NULL || c == CL_STSHUTW || c == CL_STCLOSE) { |
| 4904 | //FD_CLR(t->srv_fd, StaticReadEvent); |
| 4905 | tv_eternity(&t->srexpire); |
| 4906 | fd_delete(t->srv_fd); |
| 4907 | //close(t->srv_fd); |
| 4908 | t->srv_state = SV_STCLOSE; |
| 4909 | return 1; |
| 4910 | } |
| 4911 | else if (tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 4912 | //FD_CLR(t->srv_fd, StaticReadEvent); |
| 4913 | tv_eternity(&t->srexpire); |
| 4914 | fd_delete(t->srv_fd); |
| 4915 | //close(t->srv_fd); |
| 4916 | t->srv_state = SV_STCLOSE; |
| 4917 | if (!(t->flags & SN_ERR_MASK)) |
| 4918 | t->flags |= SN_ERR_SRVTO; |
| 4919 | if (!(t->flags & SN_FINST_MASK)) |
| 4920 | t->flags |= SN_FINST_D; |
| 4921 | return 1; |
| 4922 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4923 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
| 4924 | if (FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 4925 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4926 | tv_eternity(&t->srexpire); |
| 4927 | } |
| 4928 | } |
| 4929 | else { |
| 4930 | if (! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 4931 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4932 | if (t->proxy->srvtimeout) |
| 4933 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4934 | else |
| 4935 | tv_eternity(&t->srexpire); |
| 4936 | } |
| 4937 | } |
| 4938 | return 0; |
| 4939 | } |
| 4940 | else { /* SV_STCLOSE : nothing to do */ |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4941 | 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] | 4942 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 4943 | 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] | 4944 | write(1, trash, len); |
| 4945 | } |
| 4946 | return 0; |
| 4947 | } |
| 4948 | return 0; |
| 4949 | } |
| 4950 | |
| 4951 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4952 | /* Processes the client and server jobs of a session task, then |
| 4953 | * puts it back to the wait queue in a clean state, or |
| 4954 | * cleans up its resources if it must be deleted. Returns |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4955 | * the time the task accepts to wait, or TIME_ETERNITY for |
| 4956 | * infinity. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4957 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4958 | int process_session(struct task *t) { |
| 4959 | struct session *s = t->context; |
| 4960 | int fsm_resync = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4961 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4962 | do { |
| 4963 | fsm_resync = 0; |
Willy TARREAU | b451247 | 2006-03-01 22:34:48 +0100 | [diff] [blame] | 4964 | //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] | 4965 | fsm_resync |= process_cli(s); |
Willy TARREAU | b451247 | 2006-03-01 22:34:48 +0100 | [diff] [blame] | 4966 | //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] | 4967 | fsm_resync |= process_srv(s); |
Willy TARREAU | b451247 | 2006-03-01 22:34:48 +0100 | [diff] [blame] | 4968 | //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] | 4969 | } while (fsm_resync); |
| 4970 | |
| 4971 | if (s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4972 | struct timeval min1, min2; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4973 | 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] | 4974 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4975 | tv_min(&min1, &s->crexpire, &s->cwexpire); |
| 4976 | tv_min(&min2, &s->srexpire, &s->swexpire); |
| 4977 | tv_min(&min1, &min1, &s->cnexpire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4978 | tv_min(&t->expire, &min1, &min2); |
| 4979 | |
| 4980 | /* restore t to its place in the task list */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4981 | task_queue(t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4982 | |
Willy TARREAU | 1cec83c | 2006-03-01 22:33:49 +0100 | [diff] [blame] | 4983 | #ifdef DEBUG_FULL |
| 4984 | /* DEBUG code : this should never ever happen, otherwise it indicates |
| 4985 | * that a task still has something to do and will provoke a quick loop. |
| 4986 | */ |
| 4987 | if (tv_remain2(&now, &t->expire) <= 0) |
| 4988 | exit(100); |
| 4989 | #endif |
| 4990 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4991 | return tv_remain2(&now, &t->expire); /* nothing more to do */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4992 | } |
| 4993 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4994 | s->proxy->nbconn--; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4995 | actconn--; |
| 4996 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4997 | 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] | 4998 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 4999 | 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] | 5000 | write(1, trash, len); |
| 5001 | } |
| 5002 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5003 | s->logs.t_close = tv_diff(&s->logs.tv_accept, &now); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 5004 | if (s->rep != NULL) |
| 5005 | s->logs.bytes = s->rep->total; |
| 5006 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5007 | /* let's do a final log if we need it */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 5008 | 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] | 5009 | sess_log(s); |
| 5010 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5011 | /* the task MUST not be in the run queue anymore */ |
| 5012 | task_delete(t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5013 | session_free(s); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5014 | task_free(t); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5015 | return TIME_ETERNITY; /* rest in peace for eternity */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5016 | } |
| 5017 | |
| 5018 | |
| 5019 | |
| 5020 | /* |
| 5021 | * manages a server health-check. Returns |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5022 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5023 | */ |
| 5024 | int process_chk(struct task *t) { |
| 5025 | struct server *s = t->context; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5026 | struct sockaddr_in sa; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5027 | int fd = s->curfd; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5028 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5029 | //fprintf(stderr, "process_chk: task=%p\n", t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5030 | |
| 5031 | if (fd < 0) { /* no check currently running */ |
| 5032 | //fprintf(stderr, "process_chk: 2\n"); |
| 5033 | if (tv_cmp2_ms(&t->expire, &now) > 0) { /* not good time yet */ |
| 5034 | task_queue(t); /* restore t to its place in the task list */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5035 | return tv_remain2(&now, &t->expire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5036 | } |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame^] | 5037 | |
| 5038 | /* we don't send any health-checks when the proxy is stopped or when |
| 5039 | * the server should not be checked. |
| 5040 | */ |
| 5041 | if (!(s->state & SRV_CHECKED) || s->proxy->state == PR_STSTOPPED) { |
| 5042 | tv_delayfrom(&t->expire, &now, s->inter); |
| 5043 | task_queue(t); /* restore t to its place in the task list */ |
| 5044 | return tv_remain2(&now, &t->expire); |
| 5045 | } |
| 5046 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5047 | /* we'll initiate a new check */ |
| 5048 | s->result = 0; /* no result yet */ |
| 5049 | if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5050 | if ((fd < global.maxsock) && |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5051 | (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) && |
| 5052 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) { |
| 5053 | //fprintf(stderr, "process_chk: 3\n"); |
| 5054 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5055 | /* we'll connect to the check port on the server */ |
| 5056 | sa = s->addr; |
| 5057 | sa.sin_port = htons(s->check_port); |
| 5058 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 5059 | /* allow specific binding : |
| 5060 | * - server-specific at first |
| 5061 | * - proxy-specific next |
| 5062 | */ |
| 5063 | if (s->state & SRV_BIND_SRC) { |
| 5064 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 5065 | if (bind(fd, (struct sockaddr *)&s->source_addr, sizeof(s->source_addr)) == -1) { |
| 5066 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 5067 | s->proxy->id, s->id); |
| 5068 | s->result = -1; |
| 5069 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5070 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 5071 | else if (s->proxy->options & PR_O_BIND_SRC) { |
| 5072 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 5073 | if (bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) { |
| 5074 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", |
| 5075 | s->proxy->id); |
| 5076 | s->result = -1; |
| 5077 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5078 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 5079 | |
| 5080 | if (!s->result) { |
| 5081 | if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) { |
| 5082 | /* OK, connection in progress or established */ |
| 5083 | |
| 5084 | //fprintf(stderr, "process_chk: 4\n"); |
| 5085 | |
| 5086 | s->curfd = fd; /* that's how we know a test is in progress ;-) */ |
| 5087 | fdtab[fd].owner = t; |
| 5088 | fdtab[fd].read = &event_srv_chk_r; |
| 5089 | fdtab[fd].write = &event_srv_chk_w; |
| 5090 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 5091 | FD_SET(fd, StaticWriteEvent); /* for connect status */ |
| 5092 | fd_insert(fd); |
| 5093 | /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */ |
| 5094 | tv_delayfrom(&t->expire, &now, s->inter); |
| 5095 | task_queue(t); /* restore t to its place in the task list */ |
| 5096 | return tv_remain(&now, &t->expire); |
| 5097 | } |
| 5098 | else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) { |
| 5099 | s->result = -1; /* a real error */ |
| 5100 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5101 | } |
| 5102 | } |
willy tarreau | 08dedbe | 2005-12-18 01:13:48 +0100 | [diff] [blame] | 5103 | close(fd); /* socket creation error */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5104 | } |
| 5105 | |
| 5106 | if (!s->result) { /* nothing done */ |
| 5107 | //fprintf(stderr, "process_chk: 6\n"); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5108 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5109 | task_queue(t); /* restore t to its place in the task list */ |
| 5110 | return tv_remain(&now, &t->expire); |
| 5111 | } |
| 5112 | |
| 5113 | /* here, we have seen a failure */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5114 | if (s->health > s->rise) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5115 | s->health--; /* still good */ |
| 5116 | else { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5117 | s->state &= ~SRV_RUNNING; |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5118 | if (s->health == s->rise) { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5119 | Warning("Server %s/%s DOWN.\n", s->proxy->id, s->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5120 | 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] | 5121 | |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5122 | if (find_server(s->proxy) == NULL) { |
| 5123 | Alert("Proxy %s has no server available !\n", s->proxy->id); |
| 5124 | send_log(s->proxy, LOG_EMERG, "Proxy %s has no server available !\n", s->proxy->id); |
| 5125 | } |
| 5126 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5127 | s->health = 0; /* failure */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5128 | } |
| 5129 | |
| 5130 | //fprintf(stderr, "process_chk: 7\n"); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5131 | /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */ |
| 5132 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5133 | } |
| 5134 | else { |
| 5135 | //fprintf(stderr, "process_chk: 8\n"); |
| 5136 | /* there was a test running */ |
| 5137 | if (s->result > 0) { /* good server detected */ |
| 5138 | //fprintf(stderr, "process_chk: 9\n"); |
| 5139 | s->health++; /* was bad, stays for a while */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5140 | if (s->health >= s->rise) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5141 | if (s->health == s->rise) { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5142 | Warning("server %s/%s UP.\n", s->proxy->id, s->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5143 | 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] | 5144 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5145 | |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5146 | s->health = s->rise + s->fall - 1; /* OK now */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5147 | s->state |= SRV_RUNNING; |
| 5148 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5149 | s->curfd = -1; /* no check running anymore */ |
| 5150 | //FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5151 | fd_delete(fd); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5152 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5153 | } |
| 5154 | else if (s->result < 0 || tv_cmp2_ms(&t->expire, &now) <= 0) { |
| 5155 | //fprintf(stderr, "process_chk: 10\n"); |
| 5156 | /* failure or timeout detected */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5157 | if (s->health > s->rise) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5158 | s->health--; /* still good */ |
| 5159 | else { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5160 | s->state &= ~SRV_RUNNING; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5161 | |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5162 | if (s->health == s->rise) { |
| 5163 | Warning("Server %s/%s DOWN.\n", s->proxy->id, s->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5164 | 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] | 5165 | |
| 5166 | if (find_server(s->proxy) == NULL) { |
| 5167 | Alert("Proxy %s has no server available !\n", s->proxy->id); |
| 5168 | send_log(s->proxy, LOG_EMERG, "Proxy %s has no server available !\n", s->proxy->id); |
| 5169 | } |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5170 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5171 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5172 | s->health = 0; /* failure */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5173 | } |
| 5174 | s->curfd = -1; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5175 | //FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5176 | fd_delete(fd); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5177 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5178 | } |
| 5179 | /* if result is 0 and there's no timeout, we have to wait again */ |
| 5180 | } |
| 5181 | //fprintf(stderr, "process_chk: 11\n"); |
| 5182 | s->result = 0; |
| 5183 | task_queue(t); /* restore t to its place in the task list */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5184 | return tv_remain2(&now, &t->expire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5185 | } |
| 5186 | |
| 5187 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5188 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5189 | #if STATTIME > 0 |
| 5190 | int stats(void); |
| 5191 | #endif |
| 5192 | |
| 5193 | /* |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5194 | * This does 4 things : |
| 5195 | * - wake up all expired tasks |
| 5196 | * - call all runnable tasks |
| 5197 | * - call maintain_proxies() to enable/disable the listeners |
| 5198 | * - return the delay till next event in ms, -1 = wait indefinitely |
| 5199 | * Note: this part should be rewritten with the O(ln(n)) scheduler. |
| 5200 | * |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5201 | */ |
| 5202 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5203 | int process_runnable_tasks() { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5204 | int next_time; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5205 | int time2; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5206 | struct task *t, *tnext; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5207 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5208 | next_time = TIME_ETERNITY; /* set the timer to wait eternally first */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5209 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5210 | /* look for expired tasks and add them to the run queue. |
| 5211 | */ |
| 5212 | tnext = ((struct task *)LIST_HEAD(wait_queue))->next; |
| 5213 | while ((t = tnext) != LIST_HEAD(wait_queue)) { /* we haven't looped ? */ |
| 5214 | tnext = t->next; |
| 5215 | if (t->state & TASK_RUNNING) |
| 5216 | continue; |
| 5217 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5218 | if (tv_iseternity(&t->expire)) |
| 5219 | continue; |
| 5220 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5221 | /* wakeup expired entries. It doesn't matter if they are |
| 5222 | * already running because of a previous event |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5223 | */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5224 | if (tv_cmp_ms(&t->expire, &now) <= 0) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5225 | task_wakeup(&rq, t); |
| 5226 | } |
| 5227 | else { |
| 5228 | /* first non-runnable task. Use its expiration date as an upper bound */ |
| 5229 | int temp_time = tv_remain(&now, &t->expire); |
| 5230 | if (temp_time) |
| 5231 | next_time = temp_time; |
| 5232 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5233 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5234 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5235 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5236 | /* process each task in the run queue now. Each task may be deleted |
| 5237 | * since we only use tnext. |
| 5238 | */ |
| 5239 | tnext = rq; |
| 5240 | while ((t = tnext) != NULL) { |
| 5241 | int temp_time; |
| 5242 | |
| 5243 | tnext = t->rqnext; |
| 5244 | task_sleep(&rq, t); |
| 5245 | temp_time = t->process(t); |
| 5246 | next_time = MINTIME(temp_time, next_time); |
| 5247 | } |
| 5248 | |
| 5249 | /* maintain all proxies in a consistent state. This should quickly become a task */ |
| 5250 | time2 = maintain_proxies(); |
| 5251 | return MINTIME(time2, next_time); |
| 5252 | } |
| 5253 | |
| 5254 | |
| 5255 | #if defined(ENABLE_EPOLL) |
| 5256 | |
| 5257 | /* |
| 5258 | * Main epoll() loop. |
| 5259 | */ |
| 5260 | |
| 5261 | /* does 3 actions : |
| 5262 | * 0 (POLL_LOOP_ACTION_INIT) : initializes necessary private structures |
| 5263 | * 1 (POLL_LOOP_ACTION_RUN) : runs the loop |
| 5264 | * 2 (POLL_LOOP_ACTION_CLEAN) : cleans up |
| 5265 | * |
| 5266 | * returns 0 if initialization failed, !0 otherwise. |
| 5267 | */ |
| 5268 | |
| 5269 | int epoll_loop(int action) { |
| 5270 | int next_time; |
| 5271 | int status; |
| 5272 | int fd; |
| 5273 | |
| 5274 | int fds, count; |
| 5275 | int pr, pw, sr, sw; |
| 5276 | unsigned rn, ro, wn, wo; /* read new, read old, write new, write old */ |
| 5277 | struct epoll_event ev; |
| 5278 | |
| 5279 | /* private data */ |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5280 | static struct epoll_event *epoll_events = NULL; |
| 5281 | static int epoll_fd; |
| 5282 | |
| 5283 | if (action == POLL_LOOP_ACTION_INIT) { |
| 5284 | epoll_fd = epoll_create(global.maxsock + 1); |
| 5285 | if (epoll_fd < 0) |
| 5286 | return 0; |
| 5287 | else { |
| 5288 | epoll_events = (struct epoll_event*) |
| 5289 | calloc(1, sizeof(struct epoll_event) * global.maxsock); |
| 5290 | PrevReadEvent = (fd_set *) |
| 5291 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
| 5292 | PrevWriteEvent = (fd_set *) |
| 5293 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5294 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5295 | return 1; |
| 5296 | } |
| 5297 | else if (action == POLL_LOOP_ACTION_CLEAN) { |
| 5298 | if (PrevWriteEvent) free(PrevWriteEvent); |
| 5299 | if (PrevReadEvent) free(PrevReadEvent); |
| 5300 | if (epoll_events) free(epoll_events); |
| 5301 | close(epoll_fd); |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5302 | epoll_fd = 0; |
| 5303 | return 1; |
| 5304 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5305 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5306 | /* OK, it's POLL_LOOP_ACTION_RUN */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5307 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5308 | tv_now(&now); |
| 5309 | |
| 5310 | while (1) { |
| 5311 | next_time = process_runnable_tasks(); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5312 | |
| 5313 | /* stop when there's no connection left and we don't allow them anymore */ |
| 5314 | if (!actconn && listeners == 0) |
| 5315 | break; |
| 5316 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5317 | #if STATTIME > 0 |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5318 | { |
| 5319 | int time2; |
| 5320 | time2 = stats(); |
| 5321 | next_time = MINTIME(time2, next_time); |
| 5322 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5323 | #endif |
| 5324 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5325 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) { |
| 5326 | |
| 5327 | rn = ((int*)StaticReadEvent)[fds]; ro = ((int*)PrevReadEvent)[fds]; |
| 5328 | wn = ((int*)StaticWriteEvent)[fds]; wo = ((int*)PrevWriteEvent)[fds]; |
| 5329 | |
| 5330 | if ((ro^rn) | (wo^wn)) { |
| 5331 | for (count = 0, fd = fds << INTBITS; count < (1<<INTBITS) && fd < maxfd; count++, fd++) { |
| 5332 | #define FDSETS_ARE_INT_ALIGNED |
| 5333 | #ifdef FDSETS_ARE_INT_ALIGNED |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5334 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5335 | #define WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
| 5336 | #ifdef WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5337 | pr = (ro >> count) & 1; |
| 5338 | pw = (wo >> count) & 1; |
| 5339 | sr = (rn >> count) & 1; |
| 5340 | sw = (wn >> count) & 1; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5341 | #else |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5342 | pr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&ro); |
| 5343 | pw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wo); |
| 5344 | sr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&rn); |
| 5345 | sw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wn); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5346 | #endif |
| 5347 | #else |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5348 | pr = FD_ISSET(fd, PrevReadEvent); |
| 5349 | pw = FD_ISSET(fd, PrevWriteEvent); |
| 5350 | sr = FD_ISSET(fd, StaticReadEvent); |
| 5351 | sw = FD_ISSET(fd, StaticWriteEvent); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5352 | #endif |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5353 | if (!((sr^pr) | (sw^pw))) |
| 5354 | continue; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5355 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5356 | ev.events = (sr ? EPOLLIN : 0) | (sw ? EPOLLOUT : 0); |
| 5357 | ev.data.fd = fd; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5358 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5359 | #ifdef EPOLL_CTL_MOD_WORKAROUND |
| 5360 | /* I encountered a rarely reproducible problem with |
| 5361 | * EPOLL_CTL_MOD where a modified FD (systematically |
| 5362 | * the one in epoll_events[0], fd#7) would sometimes |
| 5363 | * be set EPOLL_OUT while asked for a read ! This is |
| 5364 | * with the 2.4 epoll patch. The workaround is to |
| 5365 | * delete then recreate in case of modification. |
| 5366 | * This is in 2.4 up to epoll-lt-0.21 but not in 2.6 |
| 5367 | * nor RHEL kernels. |
| 5368 | */ |
| 5369 | |
| 5370 | if ((pr | pw) && fdtab[fd].state != FD_STCLOSE) |
| 5371 | epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &ev); |
| 5372 | |
| 5373 | if ((sr | sw)) |
| 5374 | epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev); |
| 5375 | #else |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5376 | if ((pr | pw)) { |
| 5377 | /* the file-descriptor already exists... */ |
| 5378 | if ((sr | sw)) { |
| 5379 | /* ...and it will still exist */ |
| 5380 | if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &ev) < 0) { |
| 5381 | // perror("epoll_ctl(MOD)"); |
| 5382 | // exit(1); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5383 | } |
| 5384 | } else { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5385 | /* ...and it will be removed */ |
| 5386 | if (fdtab[fd].state != FD_STCLOSE && |
| 5387 | epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &ev) < 0) { |
| 5388 | // perror("epoll_ctl(DEL)"); |
| 5389 | // exit(1); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5390 | } |
| 5391 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5392 | } else { |
| 5393 | /* the file-descriptor did not exist, let's add it */ |
| 5394 | if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) { |
| 5395 | // perror("epoll_ctl(ADD)"); |
| 5396 | // exit(1); |
| 5397 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5398 | } |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5399 | #endif // EPOLL_CTL_MOD_WORKAROUND |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5400 | } |
| 5401 | ((int*)PrevReadEvent)[fds] = rn; |
| 5402 | ((int*)PrevWriteEvent)[fds] = wn; |
| 5403 | } |
| 5404 | } |
| 5405 | |
| 5406 | /* now let's wait for events */ |
| 5407 | status = epoll_wait(epoll_fd, epoll_events, maxfd, next_time); |
| 5408 | tv_now(&now); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5409 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5410 | for (count = 0; count < status; count++) { |
| 5411 | fd = epoll_events[count].data.fd; |
| 5412 | |
| 5413 | if (fdtab[fd].state == FD_STCLOSE) |
| 5414 | continue; |
| 5415 | |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 5416 | if (epoll_events[count].events & ( EPOLLIN | EPOLLERR | EPOLLHUP )) { |
| 5417 | if (FD_ISSET(fd, StaticReadEvent)) |
| 5418 | fdtab[fd].read(fd); |
| 5419 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5420 | |
| 5421 | if (fdtab[fd].state == FD_STCLOSE) |
| 5422 | continue; |
| 5423 | |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 5424 | if (epoll_events[count].events & ( EPOLLOUT | EPOLLERR | EPOLLHUP )) { |
| 5425 | if (FD_ISSET(fd, StaticWriteEvent)) |
| 5426 | fdtab[fd].write(fd); |
| 5427 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5428 | } |
| 5429 | } |
| 5430 | return 1; |
| 5431 | } |
| 5432 | #endif |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5433 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5434 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5435 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5436 | #if defined(ENABLE_POLL) |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5437 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5438 | /* |
| 5439 | * Main poll() loop. |
| 5440 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5441 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5442 | /* does 3 actions : |
| 5443 | * 0 (POLL_LOOP_ACTION_INIT) : initializes necessary private structures |
| 5444 | * 1 (POLL_LOOP_ACTION_RUN) : runs the loop |
| 5445 | * 2 (POLL_LOOP_ACTION_CLEAN) : cleans up |
| 5446 | * |
| 5447 | * returns 0 if initialization failed, !0 otherwise. |
| 5448 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5449 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5450 | int poll_loop(int action) { |
| 5451 | int next_time; |
| 5452 | int status; |
| 5453 | int fd, nbfd; |
| 5454 | |
| 5455 | int fds, count; |
| 5456 | int sr, sw; |
| 5457 | unsigned rn, wn; /* read new, write new */ |
| 5458 | |
| 5459 | /* private data */ |
| 5460 | static struct pollfd *poll_events = NULL; |
| 5461 | |
| 5462 | if (action == POLL_LOOP_ACTION_INIT) { |
| 5463 | poll_events = (struct pollfd*) |
| 5464 | calloc(1, sizeof(struct pollfd) * global.maxsock); |
| 5465 | return 1; |
| 5466 | } |
| 5467 | else if (action == POLL_LOOP_ACTION_CLEAN) { |
| 5468 | if (poll_events) |
| 5469 | free(poll_events); |
| 5470 | return 1; |
| 5471 | } |
| 5472 | |
| 5473 | /* OK, it's POLL_LOOP_ACTION_RUN */ |
| 5474 | |
| 5475 | tv_now(&now); |
| 5476 | |
| 5477 | while (1) { |
| 5478 | next_time = process_runnable_tasks(); |
| 5479 | |
| 5480 | /* stop when there's no connection left and we don't allow them anymore */ |
| 5481 | if (!actconn && listeners == 0) |
| 5482 | break; |
| 5483 | |
| 5484 | #if STATTIME > 0 |
| 5485 | { |
| 5486 | int time2; |
| 5487 | time2 = stats(); |
| 5488 | next_time = MINTIME(time2, next_time); |
| 5489 | } |
| 5490 | #endif |
| 5491 | |
| 5492 | |
| 5493 | nbfd = 0; |
| 5494 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) { |
| 5495 | |
| 5496 | rn = ((int*)StaticReadEvent)[fds]; |
| 5497 | wn = ((int*)StaticWriteEvent)[fds]; |
| 5498 | |
| 5499 | if ((rn|wn)) { |
| 5500 | for (count = 0, fd = fds << INTBITS; count < (1<<INTBITS) && fd < maxfd; count++, fd++) { |
| 5501 | #define FDSETS_ARE_INT_ALIGNED |
| 5502 | #ifdef FDSETS_ARE_INT_ALIGNED |
| 5503 | |
| 5504 | #define WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
| 5505 | #ifdef WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
| 5506 | sr = (rn >> count) & 1; |
| 5507 | sw = (wn >> count) & 1; |
| 5508 | #else |
| 5509 | sr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&rn); |
| 5510 | sw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wn); |
| 5511 | #endif |
| 5512 | #else |
| 5513 | sr = FD_ISSET(fd, StaticReadEvent); |
| 5514 | sw = FD_ISSET(fd, StaticWriteEvent); |
| 5515 | #endif |
| 5516 | if ((sr|sw)) { |
| 5517 | poll_events[nbfd].fd = fd; |
| 5518 | poll_events[nbfd].events = (sr ? POLLIN : 0) | (sw ? POLLOUT : 0); |
| 5519 | nbfd++; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5520 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5521 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5522 | } |
| 5523 | } |
| 5524 | |
| 5525 | /* now let's wait for events */ |
| 5526 | status = poll(poll_events, nbfd, next_time); |
| 5527 | tv_now(&now); |
| 5528 | |
| 5529 | for (count = 0; status > 0 && count < nbfd; count++) { |
| 5530 | fd = poll_events[count].fd; |
| 5531 | |
| 5532 | if (!poll_events[count].revents & ( POLLOUT | POLLIN | POLLERR | POLLHUP )) |
| 5533 | continue; |
| 5534 | |
| 5535 | /* ok, we found one active fd */ |
| 5536 | status--; |
| 5537 | |
| 5538 | if (fdtab[fd].state == FD_STCLOSE) |
| 5539 | continue; |
| 5540 | |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 5541 | if (poll_events[count].revents & ( POLLIN | POLLERR | POLLHUP )) { |
| 5542 | if (FD_ISSET(fd, StaticReadEvent)) |
| 5543 | fdtab[fd].read(fd); |
| 5544 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5545 | |
| 5546 | if (fdtab[fd].state == FD_STCLOSE) |
| 5547 | continue; |
| 5548 | |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 5549 | if (poll_events[count].revents & ( POLLOUT | POLLERR | POLLHUP )) { |
| 5550 | if (FD_ISSET(fd, StaticWriteEvent)) |
| 5551 | fdtab[fd].write(fd); |
| 5552 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5553 | } |
| 5554 | } |
| 5555 | return 1; |
| 5556 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5557 | #endif |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5558 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5559 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5560 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5561 | /* |
| 5562 | * Main select() loop. |
| 5563 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5564 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5565 | /* does 3 actions : |
| 5566 | * 0 (POLL_LOOP_ACTION_INIT) : initializes necessary private structures |
| 5567 | * 1 (POLL_LOOP_ACTION_RUN) : runs the loop |
| 5568 | * 2 (POLL_LOOP_ACTION_CLEAN) : cleans up |
| 5569 | * |
| 5570 | * returns 0 if initialization failed, !0 otherwise. |
| 5571 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5572 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5573 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5574 | int select_loop(int action) { |
| 5575 | int next_time; |
| 5576 | int status; |
| 5577 | int fd,i; |
| 5578 | struct timeval delta; |
| 5579 | int readnotnull, writenotnull; |
| 5580 | static fd_set *ReadEvent = NULL, *WriteEvent = NULL; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5581 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5582 | if (action == POLL_LOOP_ACTION_INIT) { |
| 5583 | ReadEvent = (fd_set *) |
| 5584 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
| 5585 | WriteEvent = (fd_set *) |
| 5586 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
| 5587 | return 1; |
| 5588 | } |
| 5589 | else if (action == POLL_LOOP_ACTION_CLEAN) { |
| 5590 | if (WriteEvent) free(WriteEvent); |
| 5591 | if (ReadEvent) free(ReadEvent); |
| 5592 | return 1; |
| 5593 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5594 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5595 | /* OK, it's POLL_LOOP_ACTION_RUN */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5596 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5597 | tv_now(&now); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5598 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5599 | while (1) { |
| 5600 | next_time = process_runnable_tasks(); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5601 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5602 | /* stop when there's no connection left and we don't allow them anymore */ |
| 5603 | if (!actconn && listeners == 0) |
| 5604 | break; |
| 5605 | |
| 5606 | #if STATTIME > 0 |
| 5607 | { |
| 5608 | int time2; |
| 5609 | time2 = stats(); |
| 5610 | next_time = MINTIME(time2, next_time); |
| 5611 | } |
| 5612 | #endif |
| 5613 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5614 | if (next_time > 0) { /* FIXME */ |
| 5615 | /* Convert to timeval */ |
| 5616 | /* to avoid eventual select loops due to timer precision */ |
| 5617 | next_time += SCHEDULER_RESOLUTION; |
| 5618 | delta.tv_sec = next_time / 1000; |
| 5619 | delta.tv_usec = (next_time % 1000) * 1000; |
| 5620 | } |
| 5621 | else if (next_time == 0) { /* allow select to return immediately when needed */ |
| 5622 | delta.tv_sec = delta.tv_usec = 0; |
| 5623 | } |
| 5624 | |
| 5625 | |
| 5626 | /* let's restore fdset state */ |
| 5627 | |
| 5628 | readnotnull = 0; writenotnull = 0; |
| 5629 | for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) { |
| 5630 | readnotnull |= (*(((int*)ReadEvent)+i) = *(((int*)StaticReadEvent)+i)) != 0; |
| 5631 | writenotnull |= (*(((int*)WriteEvent)+i) = *(((int*)StaticWriteEvent)+i)) != 0; |
| 5632 | } |
| 5633 | |
| 5634 | // /* just a verification code, needs to be removed for performance */ |
| 5635 | // for (i=0; i<maxfd; i++) { |
| 5636 | // if (FD_ISSET(i, ReadEvent) != FD_ISSET(i, StaticReadEvent)) |
| 5637 | // abort(); |
| 5638 | // if (FD_ISSET(i, WriteEvent) != FD_ISSET(i, StaticWriteEvent)) |
| 5639 | // abort(); |
| 5640 | // |
| 5641 | // } |
| 5642 | |
| 5643 | status = select(maxfd, |
| 5644 | readnotnull ? ReadEvent : NULL, |
| 5645 | writenotnull ? WriteEvent : NULL, |
| 5646 | NULL, |
| 5647 | (next_time >= 0) ? &delta : NULL); |
| 5648 | |
| 5649 | /* this is an experiment on the separation of the select work */ |
| 5650 | // status = (readnotnull ? select(maxfd, ReadEvent, NULL, NULL, (next_time >= 0) ? &delta : NULL) : 0); |
| 5651 | // status |= (writenotnull ? select(maxfd, NULL, WriteEvent, NULL, (next_time >= 0) ? &delta : NULL) : 0); |
| 5652 | |
| 5653 | tv_now(&now); |
| 5654 | |
| 5655 | if (status > 0) { /* must proceed with events */ |
| 5656 | |
| 5657 | int fds; |
| 5658 | char count; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5659 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5660 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) |
| 5661 | if ((((int *)(ReadEvent))[fds] | ((int *)(WriteEvent))[fds]) != 0) |
| 5662 | for (count = 1<<INTBITS, fd = fds << INTBITS; count && fd < maxfd; count--, fd++) { |
| 5663 | |
| 5664 | /* if we specify read first, the accepts and zero reads will be |
| 5665 | * seen first. Moreover, system buffers will be flushed faster. |
| 5666 | */ |
| 5667 | if (fdtab[fd].state == FD_STCLOSE) |
| 5668 | continue; |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 5669 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5670 | if (FD_ISSET(fd, ReadEvent)) |
| 5671 | fdtab[fd].read(fd); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 5672 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5673 | if (FD_ISSET(fd, WriteEvent)) |
| 5674 | fdtab[fd].write(fd); |
| 5675 | } |
| 5676 | } |
| 5677 | else { |
| 5678 | // fprintf(stderr,"select returned %d, maxfd=%d\n", status, maxfd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5679 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5680 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5681 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5682 | } |
| 5683 | |
| 5684 | |
| 5685 | #if STATTIME > 0 |
| 5686 | /* |
| 5687 | * Display proxy statistics regularly. It is designed to be called from the |
| 5688 | * select_loop(). |
| 5689 | */ |
| 5690 | int stats(void) { |
| 5691 | static int lines; |
| 5692 | static struct timeval nextevt; |
| 5693 | static struct timeval lastevt; |
| 5694 | static struct timeval starttime = {0,0}; |
| 5695 | unsigned long totaltime, deltatime; |
| 5696 | int ret; |
| 5697 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5698 | if (tv_cmp(&now, &nextevt) > 0) { |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 5699 | deltatime = (tv_diff(&lastevt, &now)?:1); |
| 5700 | totaltime = (tv_diff(&starttime, &now)?:1); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5701 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5702 | if (global.mode & MODE_STATS) { |
| 5703 | if ((lines++ % 16 == 0) && !(global.mode & MODE_LOG)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5704 | qfprintf(stderr, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5705 | "\n active total tsknew tskgood tskleft tskrght tsknsch tsklsch tskrsch\n"); |
| 5706 | if (lines>1) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5707 | qfprintf(stderr,"%07d %07d %07d %07d %07d %07d %07d %07d %07d\n", |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5708 | actconn, totalconn, |
| 5709 | stats_tsk_new, stats_tsk_good, |
| 5710 | stats_tsk_left, stats_tsk_right, |
| 5711 | stats_tsk_nsrch, stats_tsk_lsrch, stats_tsk_rsrch); |
| 5712 | } |
| 5713 | } |
| 5714 | |
| 5715 | tv_delayfrom(&nextevt, &now, STATTIME); |
| 5716 | |
| 5717 | lastevt=now; |
| 5718 | } |
| 5719 | ret = tv_remain(&now, &nextevt); |
| 5720 | return ret; |
| 5721 | } |
| 5722 | #endif |
| 5723 | |
| 5724 | |
| 5725 | /* |
| 5726 | * this function enables proxies when there are enough free sessions, |
| 5727 | * 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] | 5728 | * select_loop(). It returns the time left before next expiration event |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5729 | * during stop time, TIME_ETERNITY otherwise. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5730 | */ |
| 5731 | static int maintain_proxies(void) { |
| 5732 | struct proxy *p; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5733 | struct listener *l; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5734 | int tleft; /* time left */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5735 | |
| 5736 | p = proxy; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5737 | tleft = TIME_ETERNITY; /* infinite time */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5738 | |
| 5739 | /* if there are enough free sessions, we'll activate proxies */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5740 | if (actconn < global.maxconn) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5741 | while (p) { |
| 5742 | if (p->nbconn < p->maxconn) { |
| 5743 | if (p->state == PR_STIDLE) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5744 | for (l = p->listen; l != NULL; l = l->next) { |
| 5745 | FD_SET(l->fd, StaticReadEvent); |
| 5746 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5747 | p->state = PR_STRUN; |
| 5748 | } |
| 5749 | } |
| 5750 | else { |
| 5751 | if (p->state == PR_STRUN) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5752 | for (l = p->listen; l != NULL; l = l->next) { |
| 5753 | FD_CLR(l->fd, StaticReadEvent); |
| 5754 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5755 | p->state = PR_STIDLE; |
| 5756 | } |
| 5757 | } |
| 5758 | p = p->next; |
| 5759 | } |
| 5760 | } |
| 5761 | else { /* block all proxies */ |
| 5762 | while (p) { |
| 5763 | if (p->state == PR_STRUN) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5764 | for (l = p->listen; l != NULL; l = l->next) { |
| 5765 | FD_CLR(l->fd, StaticReadEvent); |
| 5766 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5767 | p->state = PR_STIDLE; |
| 5768 | } |
| 5769 | p = p->next; |
| 5770 | } |
| 5771 | } |
| 5772 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5773 | if (stopping) { |
| 5774 | p = proxy; |
| 5775 | while (p) { |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5776 | if (p->state != PR_STSTOPPED) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5777 | int t; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5778 | t = tv_remain2(&now, &p->stop_time); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5779 | if (t == 0) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5780 | Warning("Proxy %s stopped.\n", p->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5781 | send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5782 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5783 | for (l = p->listen; l != NULL; l = l->next) { |
| 5784 | fd_delete(l->fd); |
| 5785 | listeners--; |
| 5786 | } |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5787 | p->state = PR_STSTOPPED; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5788 | } |
| 5789 | else { |
| 5790 | tleft = MINTIME(t, tleft); |
| 5791 | } |
| 5792 | } |
| 5793 | p = p->next; |
| 5794 | } |
| 5795 | } |
| 5796 | return tleft; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5797 | } |
| 5798 | |
| 5799 | /* |
| 5800 | * 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] | 5801 | * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace |
| 5802 | * 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] | 5803 | */ |
| 5804 | static void soft_stop(void) { |
| 5805 | struct proxy *p; |
| 5806 | |
| 5807 | stopping = 1; |
| 5808 | p = proxy; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5809 | tv_now(&now); /* else, the old time before select will be used */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5810 | while (p) { |
willy tarreau | 808b4e6 | 2006-01-20 19:46:44 +0100 | [diff] [blame] | 5811 | if (p->state != PR_STSTOPPED && p->state != PR_STPAUSED) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5812 | Warning("Stopping proxy %s in %d ms.\n", p->id, p->grace); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5813 | 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] | 5814 | tv_delayfrom(&p->stop_time, &now, p->grace); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5815 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5816 | p = p->next; |
| 5817 | } |
| 5818 | } |
| 5819 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5820 | static void pause_proxy(struct proxy *p) { |
| 5821 | struct listener *l; |
| 5822 | for (l = p->listen; l != NULL; l = l->next) { |
| 5823 | shutdown(l->fd, SHUT_RD); |
| 5824 | FD_CLR(l->fd, StaticReadEvent); |
| 5825 | p->state = PR_STPAUSED; |
| 5826 | } |
| 5827 | } |
| 5828 | |
| 5829 | /* |
| 5830 | * This function temporarily disables listening so that another new instance |
| 5831 | * 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] | 5832 | * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5833 | * the proxy, or a SIGTTIN can be sent to listen again. |
| 5834 | */ |
| 5835 | static void pause_proxies(void) { |
| 5836 | struct proxy *p; |
| 5837 | |
| 5838 | p = proxy; |
| 5839 | tv_now(&now); /* else, the old time before select will be used */ |
| 5840 | while (p) { |
| 5841 | if (p->state != PR_STSTOPPED && p->state != PR_STPAUSED) { |
| 5842 | Warning("Pausing proxy %s.\n", p->id); |
| 5843 | send_log(p, LOG_WARNING, "Pausing proxy %s.\n", p->id); |
| 5844 | pause_proxy(p); |
| 5845 | } |
| 5846 | p = p->next; |
| 5847 | } |
| 5848 | } |
| 5849 | |
| 5850 | |
| 5851 | /* |
| 5852 | * This function reactivates listening. This can be used after a call to |
| 5853 | * sig_pause(), for example when a new instance has failed starting up. |
| 5854 | * It is designed to be called upon reception of a SIGTTIN. |
| 5855 | */ |
| 5856 | static void listen_proxies(void) { |
| 5857 | struct proxy *p; |
| 5858 | struct listener *l; |
| 5859 | |
| 5860 | p = proxy; |
| 5861 | tv_now(&now); /* else, the old time before select will be used */ |
| 5862 | while (p) { |
| 5863 | if (p->state == PR_STPAUSED) { |
| 5864 | Warning("Enabling proxy %s.\n", p->id); |
| 5865 | send_log(p, LOG_WARNING, "Enabling proxy %s.\n", p->id); |
| 5866 | |
| 5867 | for (l = p->listen; l != NULL; l = l->next) { |
| 5868 | if (listen(l->fd, p->maxconn) == 0) { |
| 5869 | if (actconn < global.maxconn && p->nbconn < p->maxconn) { |
| 5870 | FD_SET(l->fd, StaticReadEvent); |
| 5871 | p->state = PR_STRUN; |
| 5872 | } |
| 5873 | else |
| 5874 | p->state = PR_STIDLE; |
| 5875 | } else { |
willy tarreau | cb2e562 | 2006-01-29 21:55:30 +0100 | [diff] [blame] | 5876 | int port; |
| 5877 | |
| 5878 | if (l->addr.ss_family == AF_INET6) |
| 5879 | port = ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port); |
| 5880 | else |
| 5881 | port = ntohs(((struct sockaddr_in *)(&l->addr))->sin_port); |
| 5882 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5883 | Warning("Port %d busy while trying to enable proxy %s.\n", |
willy tarreau | cb2e562 | 2006-01-29 21:55:30 +0100 | [diff] [blame] | 5884 | port, p->id); |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5885 | 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] | 5886 | port, p->id); |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5887 | /* Another port might have been enabled. Let's stop everything. */ |
| 5888 | pause_proxy(p); |
| 5889 | break; |
| 5890 | } |
| 5891 | } |
| 5892 | } |
| 5893 | p = p->next; |
| 5894 | } |
| 5895 | } |
| 5896 | |
| 5897 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5898 | /* |
| 5899 | * upon SIGUSR1, let's have a soft stop. |
| 5900 | */ |
| 5901 | void sig_soft_stop(int sig) { |
| 5902 | soft_stop(); |
| 5903 | signal(sig, SIG_IGN); |
| 5904 | } |
| 5905 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5906 | /* |
| 5907 | * upon SIGTTOU, we pause everything |
| 5908 | */ |
| 5909 | void sig_pause(int sig) { |
| 5910 | pause_proxies(); |
| 5911 | signal(sig, sig_pause); |
| 5912 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5913 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 5914 | /* |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5915 | * upon SIGTTIN, let's have a soft stop. |
| 5916 | */ |
| 5917 | void sig_listen(int sig) { |
| 5918 | listen_proxies(); |
| 5919 | signal(sig, sig_listen); |
| 5920 | } |
| 5921 | |
| 5922 | /* |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 5923 | * this function dumps every server's state when the process receives SIGHUP. |
| 5924 | */ |
| 5925 | void sig_dump_state(int sig) { |
| 5926 | struct proxy *p = proxy; |
| 5927 | |
| 5928 | Warning("SIGHUP received, dumping servers states.\n"); |
| 5929 | while (p) { |
| 5930 | struct server *s = p->srv; |
| 5931 | |
| 5932 | send_log(p, LOG_NOTICE, "SIGUP received, dumping servers states.\n"); |
| 5933 | while (s) { |
| 5934 | if (s->state & SRV_RUNNING) { |
| 5935 | Warning("SIGHUP: server %s/%s is UP.\n", p->id, s->id); |
| 5936 | send_log(p, LOG_NOTICE, "SIGUP: server %s/%s is UP.\n", p->id, s->id); |
| 5937 | } |
| 5938 | else { |
| 5939 | Warning("SIGHUP: server %s/%s is DOWN.\n", p->id, s->id); |
| 5940 | send_log(p, LOG_NOTICE, "SIGHUP: server %s/%s is DOWN.\n", p->id, s->id); |
| 5941 | } |
| 5942 | s = s->next; |
| 5943 | } |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5944 | |
| 5945 | if (find_server(p) == NULL) { |
| 5946 | Warning("SIGHUP: proxy %s has no server available !\n", p); |
| 5947 | send_log(p, LOG_NOTICE, "SIGHUP: proxy %s has no server available !\n", p); |
| 5948 | } |
| 5949 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 5950 | p = p->next; |
| 5951 | } |
| 5952 | signal(sig, sig_dump_state); |
| 5953 | } |
| 5954 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5955 | void dump(int sig) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5956 | struct task *t, *tnext; |
| 5957 | struct session *s; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5958 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5959 | tnext = ((struct task *)LIST_HEAD(wait_queue))->next; |
| 5960 | while ((t = tnext) != LIST_HEAD(wait_queue)) { /* we haven't looped ? */ |
| 5961 | tnext = t->next; |
| 5962 | s = t->context; |
| 5963 | qfprintf(stderr,"[dump] wq: task %p, still %ld ms, " |
| 5964 | "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, " |
| 5965 | "req=%d, rep=%d, clifd=%d\n", |
| 5966 | s, tv_remain(&now, &t->expire), |
| 5967 | s->cli_state, |
| 5968 | s->srv_state, |
| 5969 | FD_ISSET(s->cli_fd, StaticReadEvent), |
| 5970 | FD_ISSET(s->cli_fd, StaticWriteEvent), |
| 5971 | FD_ISSET(s->srv_fd, StaticReadEvent), |
| 5972 | FD_ISSET(s->srv_fd, StaticWriteEvent), |
| 5973 | s->req->l, s->rep?s->rep->l:0, s->cli_fd |
| 5974 | ); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5975 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 5976 | } |
| 5977 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 5978 | #ifdef DEBUG_MEMORY |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 5979 | static void fast_stop(void) |
| 5980 | { |
| 5981 | struct proxy *p; |
| 5982 | p = proxy; |
| 5983 | while (p) { |
| 5984 | p->grace = 0; |
| 5985 | p = p->next; |
| 5986 | } |
| 5987 | soft_stop(); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5988 | } |
| 5989 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 5990 | void sig_int(int sig) { |
| 5991 | /* This would normally be a hard stop, |
| 5992 | but we want to be sure about deallocation, |
| 5993 | and so on, so we do a soft stop with |
| 5994 | 0 GRACE time |
| 5995 | */ |
| 5996 | fast_stop(); |
| 5997 | /* If we are killed twice, we decide to die*/ |
| 5998 | signal(sig, SIG_DFL); |
| 5999 | } |
| 6000 | |
| 6001 | void sig_term(int sig) { |
| 6002 | /* This would normally be a hard stop, |
| 6003 | but we want to be sure about deallocation, |
| 6004 | and so on, so we do a soft stop with |
| 6005 | 0 GRACE time |
| 6006 | */ |
| 6007 | fast_stop(); |
| 6008 | /* If we are killed twice, we decide to die*/ |
| 6009 | signal(sig, SIG_DFL); |
| 6010 | } |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 6011 | #endif |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6012 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 6013 | /* returns the pointer to an error in the replacement string, or NULL if OK */ |
| 6014 | 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] | 6015 | struct hdr_exp *exp; |
| 6016 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 6017 | if (replace != NULL) { |
| 6018 | char *err; |
| 6019 | err = check_replace_string(replace); |
| 6020 | if (err) |
| 6021 | return err; |
| 6022 | } |
| 6023 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 6024 | while (*head != NULL) |
| 6025 | head = &(*head)->next; |
| 6026 | |
| 6027 | exp = calloc(1, sizeof(struct hdr_exp)); |
| 6028 | |
| 6029 | exp->preg = preg; |
| 6030 | exp->replace = replace; |
| 6031 | exp->action = action; |
| 6032 | *head = exp; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 6033 | |
| 6034 | return NULL; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 6035 | } |
| 6036 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6037 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6038 | /* |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6039 | * 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] | 6040 | */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6041 | int cfg_parse_global(char *file, int linenum, char **args) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6042 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6043 | if (!strcmp(args[0], "global")) { /* new section */ |
| 6044 | /* no option, nothing special to do */ |
| 6045 | return 0; |
| 6046 | } |
| 6047 | else if (!strcmp(args[0], "daemon")) { |
| 6048 | global.mode |= MODE_DAEMON; |
| 6049 | } |
| 6050 | else if (!strcmp(args[0], "debug")) { |
| 6051 | global.mode |= MODE_DEBUG; |
| 6052 | } |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 6053 | else if (!strcmp(args[0], "noepoll")) { |
| 6054 | cfg_polling_mechanism &= ~POLL_USE_EPOLL; |
| 6055 | } |
| 6056 | else if (!strcmp(args[0], "nopoll")) { |
| 6057 | cfg_polling_mechanism &= ~POLL_USE_POLL; |
| 6058 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6059 | else if (!strcmp(args[0], "quiet")) { |
| 6060 | global.mode |= MODE_QUIET; |
| 6061 | } |
| 6062 | else if (!strcmp(args[0], "stats")) { |
| 6063 | global.mode |= MODE_STATS; |
| 6064 | } |
| 6065 | else if (!strcmp(args[0], "uid")) { |
| 6066 | if (global.uid != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6067 | 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] | 6068 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6069 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6070 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6071 | 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] | 6072 | return -1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6073 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6074 | global.uid = atol(args[1]); |
| 6075 | } |
| 6076 | else if (!strcmp(args[0], "gid")) { |
| 6077 | if (global.gid != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6078 | 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] | 6079 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6080 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6081 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6082 | 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] | 6083 | return -1; |
| 6084 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6085 | global.gid = atol(args[1]); |
| 6086 | } |
| 6087 | else if (!strcmp(args[0], "nbproc")) { |
| 6088 | if (global.nbproc != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6089 | 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] | 6090 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6091 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6092 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6093 | 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] | 6094 | return -1; |
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 | global.nbproc = atol(args[1]); |
| 6097 | } |
| 6098 | else if (!strcmp(args[0], "maxconn")) { |
| 6099 | if (global.maxconn != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6100 | 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] | 6101 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6102 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6103 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6104 | 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] | 6105 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6106 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6107 | global.maxconn = atol(args[1]); |
| 6108 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 6109 | else if (!strcmp(args[0], "ulimit-n")) { |
| 6110 | if (global.rlimit_nofile != 0) { |
| 6111 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
| 6112 | return 0; |
| 6113 | } |
| 6114 | if (*(args[1]) == 0) { |
| 6115 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
| 6116 | return -1; |
| 6117 | } |
| 6118 | global.rlimit_nofile = atol(args[1]); |
| 6119 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6120 | else if (!strcmp(args[0], "chroot")) { |
| 6121 | if (global.chroot != NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6122 | 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] | 6123 | return 0; |
| 6124 | } |
| 6125 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6126 | 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] | 6127 | return -1; |
| 6128 | } |
| 6129 | global.chroot = strdup(args[1]); |
| 6130 | } |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 6131 | else if (!strcmp(args[0], "pidfile")) { |
| 6132 | if (global.pidfile != NULL) { |
| 6133 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
| 6134 | return 0; |
| 6135 | } |
| 6136 | if (*(args[1]) == 0) { |
| 6137 | Alert("parsing [%s:%d] : '%s' expects a file name as an argument.\n", file, linenum, args[0]); |
| 6138 | return -1; |
| 6139 | } |
| 6140 | global.pidfile = strdup(args[1]); |
| 6141 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6142 | else if (!strcmp(args[0], "log")) { /* syslog server address */ |
| 6143 | struct sockaddr_in *sa; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6144 | int facility, level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6145 | |
| 6146 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6147 | 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] | 6148 | return -1; |
| 6149 | } |
| 6150 | |
| 6151 | for (facility = 0; facility < NB_LOG_FACILITIES; facility++) |
| 6152 | if (!strcmp(log_facilities[facility], args[2])) |
| 6153 | break; |
| 6154 | |
| 6155 | if (facility >= NB_LOG_FACILITIES) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6156 | 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] | 6157 | exit(1); |
| 6158 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6159 | |
| 6160 | level = 7; /* max syslog level = debug */ |
| 6161 | if (*(args[3])) { |
| 6162 | while (level >= 0 && strcmp(log_levels[level], args[3])) |
| 6163 | level--; |
| 6164 | if (level < 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6165 | 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] | 6166 | exit(1); |
| 6167 | } |
| 6168 | } |
| 6169 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6170 | sa = str2sa(args[1]); |
| 6171 | if (!sa->sin_port) |
| 6172 | sa->sin_port = htons(SYSLOG_PORT); |
| 6173 | |
| 6174 | if (global.logfac1 == -1) { |
| 6175 | global.logsrv1 = *sa; |
| 6176 | global.logfac1 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6177 | global.loglev1 = level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6178 | } |
| 6179 | else if (global.logfac2 == -1) { |
| 6180 | global.logsrv2 = *sa; |
| 6181 | global.logfac2 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6182 | global.loglev2 = level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6183 | } |
| 6184 | else { |
| 6185 | Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum); |
| 6186 | return -1; |
| 6187 | } |
| 6188 | |
| 6189 | } |
| 6190 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6191 | 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] | 6192 | return -1; |
| 6193 | } |
| 6194 | return 0; |
| 6195 | } |
| 6196 | |
| 6197 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6198 | void init_default_instance() { |
| 6199 | memset(&defproxy, 0, sizeof(defproxy)); |
| 6200 | defproxy.mode = PR_MODE_TCP; |
| 6201 | defproxy.state = PR_STNEW; |
| 6202 | defproxy.maxconn = cfg_maxpconn; |
| 6203 | defproxy.conn_retries = CONN_RETRIES; |
| 6204 | defproxy.logfac1 = defproxy.logfac2 = -1; /* log disabled */ |
| 6205 | } |
| 6206 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6207 | /* |
| 6208 | * parse a line in a <listen> section. Returns 0 if OK, -1 if error. |
| 6209 | */ |
| 6210 | int cfg_parse_listen(char *file, int linenum, char **args) { |
| 6211 | static struct proxy *curproxy = NULL; |
| 6212 | struct server *newsrv = NULL; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 6213 | char *err; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6214 | int rc; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6215 | |
| 6216 | if (!strcmp(args[0], "listen")) { /* new proxy */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6217 | if (!*args[1]) { |
| 6218 | Alert("parsing [%s:%d] : '%s' expects an <id> argument and\n" |
| 6219 | " optionnally supports [addr1]:port1[-end1]{,[addr]:port[-end]}...\n", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6220 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6221 | return -1; |
| 6222 | } |
| 6223 | |
| 6224 | if ((curproxy = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6225 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6226 | return -1; |
| 6227 | } |
| 6228 | curproxy->next = proxy; |
| 6229 | proxy = curproxy; |
| 6230 | curproxy->id = strdup(args[1]); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 6231 | |
| 6232 | /* parse the listener address if any */ |
| 6233 | if (*args[2]) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6234 | curproxy->listen = str2listener(args[2], curproxy->listen); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 6235 | if (!curproxy->listen) |
| 6236 | return -1; |
| 6237 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6238 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6239 | /* set default values */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6240 | curproxy->state = defproxy.state; |
| 6241 | curproxy->maxconn = defproxy.maxconn; |
| 6242 | curproxy->conn_retries = defproxy.conn_retries; |
| 6243 | curproxy->options = defproxy.options; |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6244 | |
| 6245 | if (defproxy.check_req) |
| 6246 | curproxy->check_req = strdup(defproxy.check_req); |
| 6247 | curproxy->check_len = defproxy.check_len; |
| 6248 | |
| 6249 | if (defproxy.cookie_name) |
| 6250 | curproxy->cookie_name = strdup(defproxy.cookie_name); |
| 6251 | curproxy->cookie_len = defproxy.cookie_len; |
| 6252 | |
| 6253 | if (defproxy.capture_name) |
| 6254 | curproxy->capture_name = strdup(defproxy.capture_name); |
| 6255 | curproxy->capture_namelen = defproxy.capture_namelen; |
| 6256 | curproxy->capture_len = defproxy.capture_len; |
| 6257 | |
| 6258 | if (defproxy.errmsg.msg400) |
| 6259 | curproxy->errmsg.msg400 = strdup(defproxy.errmsg.msg400); |
| 6260 | curproxy->errmsg.len400 = defproxy.errmsg.len400; |
| 6261 | |
| 6262 | if (defproxy.errmsg.msg403) |
| 6263 | curproxy->errmsg.msg403 = strdup(defproxy.errmsg.msg403); |
| 6264 | curproxy->errmsg.len403 = defproxy.errmsg.len403; |
| 6265 | |
| 6266 | if (defproxy.errmsg.msg408) |
| 6267 | curproxy->errmsg.msg408 = strdup(defproxy.errmsg.msg408); |
| 6268 | curproxy->errmsg.len408 = defproxy.errmsg.len408; |
| 6269 | |
| 6270 | if (defproxy.errmsg.msg500) |
| 6271 | curproxy->errmsg.msg500 = strdup(defproxy.errmsg.msg500); |
| 6272 | curproxy->errmsg.len500 = defproxy.errmsg.len500; |
| 6273 | |
| 6274 | if (defproxy.errmsg.msg502) |
| 6275 | curproxy->errmsg.msg502 = strdup(defproxy.errmsg.msg502); |
| 6276 | curproxy->errmsg.len502 = defproxy.errmsg.len502; |
| 6277 | |
| 6278 | if (defproxy.errmsg.msg503) |
| 6279 | curproxy->errmsg.msg503 = strdup(defproxy.errmsg.msg503); |
| 6280 | curproxy->errmsg.len503 = defproxy.errmsg.len503; |
| 6281 | |
| 6282 | if (defproxy.errmsg.msg504) |
| 6283 | curproxy->errmsg.msg504 = strdup(defproxy.errmsg.msg504); |
| 6284 | curproxy->errmsg.len504 = defproxy.errmsg.len504; |
| 6285 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6286 | curproxy->clitimeout = defproxy.clitimeout; |
| 6287 | curproxy->contimeout = defproxy.contimeout; |
| 6288 | curproxy->srvtimeout = defproxy.srvtimeout; |
| 6289 | curproxy->mode = defproxy.mode; |
| 6290 | curproxy->logfac1 = defproxy.logfac1; |
| 6291 | curproxy->logsrv1 = defproxy.logsrv1; |
| 6292 | curproxy->loglev1 = defproxy.loglev1; |
| 6293 | curproxy->logfac2 = defproxy.logfac2; |
| 6294 | curproxy->logsrv2 = defproxy.logsrv2; |
| 6295 | curproxy->loglev2 = defproxy.loglev2; |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 6296 | curproxy->to_log = defproxy.to_log & ~LW_COOKIE & ~LW_REQHDR & ~ LW_RSPHDR; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6297 | curproxy->grace = defproxy.grace; |
| 6298 | curproxy->source_addr = defproxy.source_addr; |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 6299 | curproxy->mon_net = defproxy.mon_net; |
| 6300 | curproxy->mon_mask = defproxy.mon_mask; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6301 | return 0; |
| 6302 | } |
| 6303 | 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] | 6304 | /* some variables may have already been initialized earlier */ |
| 6305 | if (defproxy.check_req) free(defproxy.check_req); |
| 6306 | if (defproxy.cookie_name) free(defproxy.cookie_name); |
| 6307 | if (defproxy.capture_name) free(defproxy.capture_name); |
| 6308 | if (defproxy.errmsg.msg400) free(defproxy.errmsg.msg400); |
| 6309 | if (defproxy.errmsg.msg403) free(defproxy.errmsg.msg403); |
| 6310 | if (defproxy.errmsg.msg408) free(defproxy.errmsg.msg408); |
| 6311 | if (defproxy.errmsg.msg500) free(defproxy.errmsg.msg500); |
| 6312 | if (defproxy.errmsg.msg502) free(defproxy.errmsg.msg502); |
| 6313 | if (defproxy.errmsg.msg503) free(defproxy.errmsg.msg503); |
| 6314 | if (defproxy.errmsg.msg504) free(defproxy.errmsg.msg504); |
| 6315 | |
| 6316 | init_default_instance(); |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6317 | curproxy = &defproxy; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6318 | return 0; |
| 6319 | } |
| 6320 | else if (curproxy == NULL) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6321 | Alert("parsing [%s:%d] : 'listen' or 'defaults' expected.\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6322 | return -1; |
| 6323 | } |
| 6324 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6325 | if (!strcmp(args[0], "bind")) { /* new listen addresses */ |
| 6326 | if (curproxy == &defproxy) { |
| 6327 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6328 | return -1; |
| 6329 | } |
| 6330 | |
| 6331 | if (strchr(args[1], ':') == NULL) { |
| 6332 | Alert("parsing [%s:%d] : '%s' expects [addr1]:port1[-end1]{,[addr]:port[-end]}... as arguments.\n", |
| 6333 | file, linenum, args[0]); |
| 6334 | return -1; |
| 6335 | } |
| 6336 | curproxy->listen = str2listener(args[1], curproxy->listen); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 6337 | if (!curproxy->listen) |
| 6338 | return -1; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6339 | return 0; |
| 6340 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 6341 | else if (!strcmp(args[0], "monitor-net")) { /* set the range of IPs to ignore */ |
| 6342 | if (!*args[1] || !str2net(args[1], &curproxy->mon_net, &curproxy->mon_mask)) { |
| 6343 | Alert("parsing [%s:%d] : '%s' expects address[/mask].\n", |
| 6344 | file, linenum, args[0]); |
| 6345 | return -1; |
| 6346 | } |
| 6347 | /* flush useless bits */ |
| 6348 | curproxy->mon_net.s_addr &= curproxy->mon_mask.s_addr; |
| 6349 | return 0; |
| 6350 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6351 | else if (!strcmp(args[0], "mode")) { /* sets the proxy mode */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6352 | if (!strcmp(args[1], "http")) curproxy->mode = PR_MODE_HTTP; |
| 6353 | else if (!strcmp(args[1], "tcp")) curproxy->mode = PR_MODE_TCP; |
| 6354 | else if (!strcmp(args[1], "health")) curproxy->mode = PR_MODE_HEALTH; |
| 6355 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6356 | 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] | 6357 | return -1; |
| 6358 | } |
| 6359 | } |
| 6360 | else if (!strcmp(args[0], "disabled")) { /* disables this proxy */ |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 6361 | curproxy->state = PR_STSTOPPED; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6362 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6363 | else if (!strcmp(args[0], "enabled")) { /* enables this proxy (used to revert a disabled default) */ |
| 6364 | curproxy->state = PR_STNEW; |
| 6365 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6366 | else if (!strcmp(args[0], "cookie")) { /* cookie name */ |
| 6367 | int cur_arg; |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6368 | // if (curproxy == &defproxy) { |
| 6369 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6370 | // return -1; |
| 6371 | // } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6372 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6373 | if (curproxy->cookie_name != NULL) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6374 | // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n", |
| 6375 | // file, linenum); |
| 6376 | // return 0; |
| 6377 | free(curproxy->cookie_name); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6378 | } |
| 6379 | |
| 6380 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6381 | Alert("parsing [%s:%d] : '%s' expects <cookie_name> as argument.\n", |
| 6382 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6383 | return -1; |
| 6384 | } |
| 6385 | curproxy->cookie_name = strdup(args[1]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6386 | curproxy->cookie_len = strlen(curproxy->cookie_name); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6387 | |
| 6388 | cur_arg = 2; |
| 6389 | while (*(args[cur_arg])) { |
| 6390 | if (!strcmp(args[cur_arg], "rewrite")) { |
| 6391 | curproxy->options |= PR_O_COOK_RW; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6392 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6393 | else if (!strcmp(args[cur_arg], "indirect")) { |
| 6394 | curproxy->options |= PR_O_COOK_IND; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6395 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6396 | else if (!strcmp(args[cur_arg], "insert")) { |
| 6397 | curproxy->options |= PR_O_COOK_INS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6398 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 6399 | else if (!strcmp(args[cur_arg], "nocache")) { |
| 6400 | curproxy->options |= PR_O_COOK_NOC; |
| 6401 | } |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 6402 | else if (!strcmp(args[cur_arg], "postonly")) { |
| 6403 | curproxy->options |= PR_O_COOK_POST; |
| 6404 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6405 | else if (!strcmp(args[cur_arg], "prefix")) { |
| 6406 | curproxy->options |= PR_O_COOK_PFX; |
| 6407 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6408 | else { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6409 | 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] | 6410 | file, linenum, args[0]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6411 | return -1; |
| 6412 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6413 | cur_arg++; |
| 6414 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6415 | if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_IND))) { |
| 6416 | Alert("parsing [%s:%d] : cookie 'rewrite' and 'indirect' modes are incompatible.\n", |
| 6417 | file, linenum); |
| 6418 | return -1; |
| 6419 | } |
| 6420 | |
| 6421 | if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_INS|PR_O_COOK_PFX))) { |
| 6422 | 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] | 6423 | file, linenum); |
| 6424 | return -1; |
| 6425 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6426 | }/* end else if (!strcmp(args[0], "cookie")) */ |
| 6427 | else if (!strcmp(args[0], "appsession")) { /* cookie name */ |
| 6428 | // if (curproxy == &defproxy) { |
| 6429 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6430 | // return -1; |
| 6431 | // } |
| 6432 | |
| 6433 | if (curproxy->appsession_name != NULL) { |
| 6434 | // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n", |
| 6435 | // file, linenum); |
| 6436 | // return 0; |
| 6437 | free(curproxy->appsession_name); |
| 6438 | } |
| 6439 | |
| 6440 | if (*(args[5]) == 0) { |
| 6441 | Alert("parsing [%s:%d] : '%s' expects 'appsession' <cookie_name> 'len' <len> 'timeout' <timeout>.\n", |
| 6442 | file, linenum, args[0]); |
| 6443 | return -1; |
| 6444 | } |
| 6445 | have_appsession = 1; |
| 6446 | curproxy->appsession_name = strdup(args[1]); |
| 6447 | curproxy->appsession_name_len = strlen(curproxy->appsession_name); |
| 6448 | curproxy->appsession_len = atoi(args[3]); |
| 6449 | curproxy->appsession_timeout = atoi(args[5]); |
| 6450 | rc = chtbl_init(&(curproxy->htbl_proxy), TBLSIZ, hashpjw, match_str, destroy); |
| 6451 | if (rc) { |
| 6452 | Alert("Error Init Appsession Hashtable.\n"); |
| 6453 | return -1; |
| 6454 | } |
| 6455 | } /* Url App Session */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 6456 | else if (!strcmp(args[0], "capture")) { |
| 6457 | if (!strcmp(args[1], "cookie")) { /* name of a cookie to capture */ |
| 6458 | // if (curproxy == &defproxy) { |
| 6459 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6460 | // return -1; |
| 6461 | // } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6462 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 6463 | if (curproxy->capture_name != NULL) { |
| 6464 | // Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", |
| 6465 | // file, linenum, args[0]); |
| 6466 | // return 0; |
| 6467 | free(curproxy->capture_name); |
| 6468 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6469 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 6470 | if (*(args[4]) == 0) { |
| 6471 | Alert("parsing [%s:%d] : '%s' expects 'cookie' <cookie_name> 'len' <len>.\n", |
| 6472 | file, linenum, args[0]); |
| 6473 | return -1; |
| 6474 | } |
| 6475 | curproxy->capture_name = strdup(args[2]); |
| 6476 | curproxy->capture_namelen = strlen(curproxy->capture_name); |
| 6477 | curproxy->capture_len = atol(args[4]); |
| 6478 | if (curproxy->capture_len >= CAPTURE_LEN) { |
| 6479 | Warning("parsing [%s:%d] : truncating capture length to %d bytes.\n", |
| 6480 | file, linenum, CAPTURE_LEN - 1); |
| 6481 | curproxy->capture_len = CAPTURE_LEN - 1; |
| 6482 | } |
| 6483 | curproxy->to_log |= LW_COOKIE; |
| 6484 | } |
| 6485 | else if (!strcmp(args[1], "request") && !strcmp(args[2], "header")) { |
| 6486 | struct cap_hdr *hdr; |
| 6487 | |
| 6488 | if (curproxy == &defproxy) { |
| 6489 | Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]); |
| 6490 | return -1; |
| 6491 | } |
| 6492 | |
| 6493 | if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) { |
| 6494 | Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n", |
| 6495 | file, linenum, args[0], args[1]); |
| 6496 | return -1; |
| 6497 | } |
| 6498 | |
| 6499 | hdr = calloc(sizeof(struct cap_hdr), 1); |
| 6500 | hdr->next = curproxy->req_cap; |
| 6501 | hdr->name = strdup(args[3]); |
| 6502 | hdr->namelen = strlen(args[3]); |
| 6503 | hdr->len = atol(args[5]); |
| 6504 | hdr->index = curproxy->nb_req_cap++; |
| 6505 | curproxy->req_cap = hdr; |
| 6506 | curproxy->to_log |= LW_REQHDR; |
| 6507 | } |
| 6508 | else if (!strcmp(args[1], "response") && !strcmp(args[2], "header")) { |
| 6509 | struct cap_hdr *hdr; |
| 6510 | |
| 6511 | if (curproxy == &defproxy) { |
| 6512 | Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]); |
| 6513 | return -1; |
| 6514 | } |
| 6515 | |
| 6516 | if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) { |
| 6517 | Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n", |
| 6518 | file, linenum, args[0], args[1]); |
| 6519 | return -1; |
| 6520 | } |
| 6521 | hdr = calloc(sizeof(struct cap_hdr), 1); |
| 6522 | hdr->next = curproxy->rsp_cap; |
| 6523 | hdr->name = strdup(args[3]); |
| 6524 | hdr->namelen = strlen(args[3]); |
| 6525 | hdr->len = atol(args[5]); |
| 6526 | hdr->index = curproxy->nb_rsp_cap++; |
| 6527 | curproxy->rsp_cap = hdr; |
| 6528 | curproxy->to_log |= LW_RSPHDR; |
| 6529 | } |
| 6530 | else { |
| 6531 | 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] | 6532 | file, linenum, args[0]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6533 | return -1; |
| 6534 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6535 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6536 | else if (!strcmp(args[0], "contimeout")) { /* connect timeout */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6537 | if (curproxy->contimeout != defproxy.contimeout) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6538 | 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] | 6539 | return 0; |
| 6540 | } |
| 6541 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6542 | Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n", |
| 6543 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6544 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6545 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6546 | curproxy->contimeout = atol(args[1]); |
| 6547 | } |
| 6548 | else if (!strcmp(args[0], "clitimeout")) { /* client timeout */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6549 | if (curproxy->clitimeout != defproxy.clitimeout) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6550 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", |
| 6551 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6552 | return 0; |
| 6553 | } |
| 6554 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6555 | Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n", |
| 6556 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6557 | return -1; |
| 6558 | } |
| 6559 | curproxy->clitimeout = atol(args[1]); |
| 6560 | } |
| 6561 | else if (!strcmp(args[0], "srvtimeout")) { /* server timeout */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6562 | if (curproxy->srvtimeout != defproxy.srvtimeout) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6563 | 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] | 6564 | return 0; |
| 6565 | } |
| 6566 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6567 | Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n", |
| 6568 | file, linenum, args[0]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6569 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6570 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6571 | curproxy->srvtimeout = atol(args[1]); |
| 6572 | } |
| 6573 | else if (!strcmp(args[0], "retries")) { /* connection retries */ |
| 6574 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6575 | Alert("parsing [%s:%d] : '%s' expects an integer argument (dispatch counts for one).\n", |
| 6576 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6577 | return -1; |
| 6578 | } |
| 6579 | curproxy->conn_retries = atol(args[1]); |
| 6580 | } |
| 6581 | else if (!strcmp(args[0], "option")) { |
| 6582 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6583 | 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] | 6584 | return -1; |
| 6585 | } |
| 6586 | if (!strcmp(args[1], "redispatch")) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6587 | /* enable reconnections to dispatch */ |
| 6588 | curproxy->options |= PR_O_REDISP; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 6589 | #ifdef TPROXY |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6590 | else if (!strcmp(args[1], "transparent")) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6591 | /* enable transparent proxy connections */ |
| 6592 | curproxy->options |= PR_O_TRANSP; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6593 | #endif |
| 6594 | else if (!strcmp(args[1], "keepalive")) |
| 6595 | /* enable keep-alive */ |
| 6596 | curproxy->options |= PR_O_KEEPALIVE; |
| 6597 | else if (!strcmp(args[1], "forwardfor")) |
| 6598 | /* insert x-forwarded-for field */ |
| 6599 | curproxy->options |= PR_O_FWDFOR; |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 6600 | else if (!strcmp(args[1], "logasap")) |
| 6601 | /* log as soon as possible, without waiting for the session to complete */ |
| 6602 | curproxy->options |= PR_O_LOGASAP; |
| 6603 | else if (!strcmp(args[1], "httpclose")) |
| 6604 | /* force connection: close in both directions in HTTP mode */ |
| 6605 | curproxy->options |= PR_O_HTTP_CLOSE; |
Willy TARREAU | 767ba71 | 2006-03-01 22:40:50 +0100 | [diff] [blame] | 6606 | else if (!strcmp(args[1], "forceclose")) |
| 6607 | /* force connection: close in both directions in HTTP mode and enforce end of session */ |
| 6608 | curproxy->options |= PR_O_FORCE_CLO | PR_O_HTTP_CLOSE; |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 6609 | else if (!strcmp(args[1], "checkcache")) |
| 6610 | /* require examination of cacheability of the 'set-cookie' field */ |
| 6611 | curproxy->options |= PR_O_CHK_CACHE; |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 6612 | else if (!strcmp(args[1], "httplog")) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6613 | /* generate a complete HTTP log */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 6614 | 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] | 6615 | else if (!strcmp(args[1], "tcplog")) |
| 6616 | /* generate a detailed TCP log */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 6617 | 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] | 6618 | else if (!strcmp(args[1], "dontlognull")) { |
| 6619 | /* don't log empty requests */ |
| 6620 | curproxy->options |= PR_O_NULLNOLOG; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6621 | } |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6622 | else if (!strcmp(args[1], "tcpka")) { |
| 6623 | /* enable TCP keep-alives on client and server sessions */ |
| 6624 | curproxy->options |= PR_O_TCP_CLI_KA | PR_O_TCP_SRV_KA; |
| 6625 | } |
| 6626 | else if (!strcmp(args[1], "clitcpka")) { |
| 6627 | /* enable TCP keep-alives on client sessions */ |
| 6628 | curproxy->options |= PR_O_TCP_CLI_KA; |
| 6629 | } |
| 6630 | else if (!strcmp(args[1], "srvtcpka")) { |
| 6631 | /* enable TCP keep-alives on server sessions */ |
| 6632 | curproxy->options |= PR_O_TCP_SRV_KA; |
| 6633 | } |
Willy TARREAU | 3481c46 | 2006-03-01 22:37:57 +0100 | [diff] [blame] | 6634 | else if (!strcmp(args[1], "allbackups")) { |
| 6635 | /* Use all backup servers simultaneously */ |
| 6636 | curproxy->options |= PR_O_USE_ALL_BK; |
| 6637 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 6638 | else if (!strcmp(args[1], "httpchk")) { |
| 6639 | /* use HTTP request to check servers' health */ |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6640 | if (curproxy->check_req != NULL) { |
| 6641 | free(curproxy->check_req); |
| 6642 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 6643 | curproxy->options |= PR_O_HTTP_CHK; |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6644 | if (!*args[2]) { /* no argument */ |
| 6645 | curproxy->check_req = strdup(DEF_CHECK_REQ); /* default request */ |
| 6646 | curproxy->check_len = strlen(DEF_CHECK_REQ); |
| 6647 | } else if (!*args[3]) { /* one argument : URI */ |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 6648 | int reqlen = strlen(args[2]) + strlen("OPTIONS / HTTP/1.0\r\n\r\n"); |
| 6649 | curproxy->check_req = (char *)malloc(reqlen); |
| 6650 | curproxy->check_len = snprintf(curproxy->check_req, reqlen, |
| 6651 | "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] | 6652 | } else { /* more arguments : METHOD URI [HTTP_VER] */ |
| 6653 | int reqlen = strlen(args[2]) + strlen(args[3]) + 3 + strlen("\r\n\r\n"); |
| 6654 | if (*args[4]) |
| 6655 | reqlen += strlen(args[4]); |
| 6656 | else |
| 6657 | reqlen += strlen("HTTP/1.0"); |
| 6658 | |
| 6659 | curproxy->check_req = (char *)malloc(reqlen); |
| 6660 | curproxy->check_len = snprintf(curproxy->check_req, reqlen, |
| 6661 | "%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] | 6662 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 6663 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6664 | else if (!strcmp(args[1], "persist")) { |
| 6665 | /* persist on using the server specified by the cookie, even when it's down */ |
| 6666 | curproxy->options |= PR_O_PERSIST; |
| 6667 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6668 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6669 | Alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6670 | return -1; |
| 6671 | } |
| 6672 | return 0; |
| 6673 | } |
| 6674 | else if (!strcmp(args[0], "redispatch") || !strcmp(args[0], "redisp")) { |
| 6675 | /* enable reconnections to dispatch */ |
| 6676 | curproxy->options |= PR_O_REDISP; |
| 6677 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 6678 | #ifdef TPROXY |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6679 | else if (!strcmp(args[0], "transparent")) { |
| 6680 | /* enable transparent proxy connections */ |
| 6681 | curproxy->options |= PR_O_TRANSP; |
| 6682 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6683 | #endif |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6684 | else if (!strcmp(args[0], "maxconn")) { /* maxconn */ |
| 6685 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6686 | 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] | 6687 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6688 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6689 | curproxy->maxconn = atol(args[1]); |
| 6690 | } |
| 6691 | else if (!strcmp(args[0], "grace")) { /* grace time (ms) */ |
| 6692 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6693 | 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] | 6694 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6695 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6696 | curproxy->grace = atol(args[1]); |
| 6697 | } |
| 6698 | else if (!strcmp(args[0], "dispatch")) { /* dispatch address */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6699 | if (curproxy == &defproxy) { |
| 6700 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6701 | return -1; |
| 6702 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6703 | if (strchr(args[1], ':') == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6704 | 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] | 6705 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6706 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6707 | curproxy->dispatch_addr = *str2sa(args[1]); |
| 6708 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6709 | else if (!strcmp(args[0], "balance")) { /* set balancing with optional algorithm */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6710 | if (*(args[1])) { |
| 6711 | if (!strcmp(args[1], "roundrobin")) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6712 | curproxy->options |= PR_O_BALANCE_RR; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6713 | } |
| 6714 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6715 | 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] | 6716 | return -1; |
| 6717 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6718 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6719 | else /* if no option is set, use round-robin by default */ |
| 6720 | curproxy->options |= PR_O_BALANCE_RR; |
| 6721 | } |
| 6722 | else if (!strcmp(args[0], "server")) { /* server address */ |
| 6723 | int cur_arg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6724 | char *rport; |
| 6725 | char *raddr; |
| 6726 | short realport; |
| 6727 | int do_check; |
| 6728 | |
| 6729 | if (curproxy == &defproxy) { |
| 6730 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6731 | return -1; |
| 6732 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6733 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6734 | if (!*args[2]) { |
| 6735 | 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] | 6736 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6737 | return -1; |
| 6738 | } |
| 6739 | if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) { |
| 6740 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 6741 | return -1; |
| 6742 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6743 | |
| 6744 | if (curproxy->srv == NULL) |
| 6745 | curproxy->srv = newsrv; |
| 6746 | else |
| 6747 | curproxy->cursrv->next = newsrv; |
| 6748 | curproxy->cursrv = newsrv; |
| 6749 | |
| 6750 | newsrv->next = NULL; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6751 | newsrv->proxy = curproxy; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6752 | |
| 6753 | do_check = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6754 | newsrv->state = SRV_RUNNING; /* early server setup */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6755 | newsrv->id = strdup(args[1]); |
| 6756 | |
| 6757 | /* several ways to check the port component : |
| 6758 | * - IP => port=+0, relative |
| 6759 | * - IP: => port=+0, relative |
| 6760 | * - IP:N => port=N, absolute |
| 6761 | * - IP:+N => port=+N, relative |
| 6762 | * - IP:-N => port=-N, relative |
| 6763 | */ |
| 6764 | raddr = strdup(args[2]); |
| 6765 | rport = strchr(raddr, ':'); |
| 6766 | if (rport) { |
| 6767 | *rport++ = 0; |
| 6768 | realport = atol(rport); |
| 6769 | if (!isdigit((int)*rport)) |
| 6770 | newsrv->state |= SRV_MAPPORTS; |
| 6771 | } else { |
| 6772 | realport = 0; |
| 6773 | newsrv->state |= SRV_MAPPORTS; |
| 6774 | } |
| 6775 | |
| 6776 | newsrv->addr = *str2sa(raddr); |
| 6777 | newsrv->addr.sin_port = htons(realport); |
| 6778 | free(raddr); |
| 6779 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6780 | newsrv->curfd = -1; /* no health-check in progress */ |
| 6781 | newsrv->inter = DEF_CHKINTR; |
| 6782 | newsrv->rise = DEF_RISETIME; |
| 6783 | newsrv->fall = DEF_FALLTIME; |
| 6784 | newsrv->health = newsrv->rise; /* up, but will fall down at first failure */ |
| 6785 | cur_arg = 3; |
| 6786 | while (*args[cur_arg]) { |
| 6787 | if (!strcmp(args[cur_arg], "cookie")) { |
| 6788 | newsrv->cookie = strdup(args[cur_arg + 1]); |
| 6789 | newsrv->cklen = strlen(args[cur_arg + 1]); |
| 6790 | cur_arg += 2; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6791 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6792 | else if (!strcmp(args[cur_arg], "rise")) { |
| 6793 | newsrv->rise = atol(args[cur_arg + 1]); |
| 6794 | newsrv->health = newsrv->rise; |
| 6795 | cur_arg += 2; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6796 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6797 | else if (!strcmp(args[cur_arg], "fall")) { |
| 6798 | newsrv->fall = atol(args[cur_arg + 1]); |
| 6799 | cur_arg += 2; |
| 6800 | } |
| 6801 | else if (!strcmp(args[cur_arg], "inter")) { |
| 6802 | newsrv->inter = atol(args[cur_arg + 1]); |
| 6803 | cur_arg += 2; |
| 6804 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6805 | else if (!strcmp(args[cur_arg], "port")) { |
| 6806 | newsrv->check_port = atol(args[cur_arg + 1]); |
| 6807 | cur_arg += 2; |
| 6808 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6809 | else if (!strcmp(args[cur_arg], "backup")) { |
| 6810 | newsrv->state |= SRV_BACKUP; |
| 6811 | cur_arg ++; |
| 6812 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6813 | else if (!strcmp(args[cur_arg], "check")) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6814 | do_check = 1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6815 | cur_arg += 1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6816 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6817 | else if (!strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */ |
| 6818 | if (!*args[cur_arg + 1]) { |
| 6819 | Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n", |
| 6820 | file, linenum, "source"); |
| 6821 | return -1; |
| 6822 | } |
| 6823 | newsrv->state |= SRV_BIND_SRC; |
| 6824 | newsrv->source_addr = *str2sa(args[cur_arg + 1]); |
| 6825 | cur_arg += 2; |
| 6826 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6827 | else { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6828 | 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] | 6829 | file, linenum, newsrv->id); |
| 6830 | return -1; |
| 6831 | } |
| 6832 | } |
| 6833 | |
| 6834 | if (do_check) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6835 | if (!newsrv->check_port && !(newsrv->state & SRV_MAPPORTS)) |
| 6836 | newsrv->check_port = realport; /* by default */ |
| 6837 | if (!newsrv->check_port) { |
| 6838 | 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] | 6839 | file, linenum, newsrv->id); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6840 | return -1; |
| 6841 | } |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame^] | 6842 | newsrv->state |= SRV_CHECKED; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6843 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6844 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6845 | curproxy->nbservers++; |
| 6846 | } |
| 6847 | else if (!strcmp(args[0], "log")) { /* syslog server address */ |
| 6848 | struct sockaddr_in *sa; |
| 6849 | int facility; |
| 6850 | |
| 6851 | if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) { |
| 6852 | curproxy->logfac1 = global.logfac1; |
| 6853 | curproxy->logsrv1 = global.logsrv1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6854 | curproxy->loglev1 = global.loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6855 | curproxy->logfac2 = global.logfac2; |
| 6856 | curproxy->logsrv2 = global.logsrv2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6857 | curproxy->loglev2 = global.loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6858 | } |
| 6859 | else if (*(args[1]) && *(args[2])) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6860 | int level; |
| 6861 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6862 | for (facility = 0; facility < NB_LOG_FACILITIES; facility++) |
| 6863 | if (!strcmp(log_facilities[facility], args[2])) |
| 6864 | break; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6865 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6866 | if (facility >= NB_LOG_FACILITIES) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6867 | 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] | 6868 | exit(1); |
| 6869 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6870 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6871 | level = 7; /* max syslog level = debug */ |
| 6872 | if (*(args[3])) { |
| 6873 | while (level >= 0 && strcmp(log_levels[level], args[3])) |
| 6874 | level--; |
| 6875 | if (level < 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6876 | 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] | 6877 | exit(1); |
| 6878 | } |
| 6879 | } |
| 6880 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6881 | sa = str2sa(args[1]); |
| 6882 | if (!sa->sin_port) |
| 6883 | sa->sin_port = htons(SYSLOG_PORT); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6884 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6885 | if (curproxy->logfac1 == -1) { |
| 6886 | curproxy->logsrv1 = *sa; |
| 6887 | curproxy->logfac1 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6888 | curproxy->loglev1 = level; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6889 | } |
| 6890 | else if (curproxy->logfac2 == -1) { |
| 6891 | curproxy->logsrv2 = *sa; |
| 6892 | curproxy->logfac2 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6893 | curproxy->loglev2 = level; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6894 | } |
| 6895 | else { |
| 6896 | Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6897 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6898 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6899 | } |
| 6900 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6901 | 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] | 6902 | file, linenum); |
| 6903 | return -1; |
| 6904 | } |
| 6905 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 6906 | 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] | 6907 | if (!*args[1]) { |
| 6908 | Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6909 | file, linenum, "source"); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 6910 | return -1; |
| 6911 | } |
| 6912 | |
| 6913 | curproxy->source_addr = *str2sa(args[1]); |
| 6914 | curproxy->options |= PR_O_BIND_SRC; |
| 6915 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6916 | else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */ |
| 6917 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6918 | if (curproxy == &defproxy) { |
| 6919 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6920 | return -1; |
| 6921 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6922 | |
| 6923 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6924 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 6925 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6926 | return -1; |
| 6927 | } |
| 6928 | |
| 6929 | preg = calloc(1, sizeof(regex_t)); |
| 6930 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6931 | 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] | 6932 | return -1; |
| 6933 | } |
| 6934 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 6935 | err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 6936 | if (err) { |
| 6937 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 6938 | file, linenum, *err); |
| 6939 | return -1; |
| 6940 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6941 | } |
| 6942 | else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */ |
| 6943 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6944 | if (curproxy == &defproxy) { |
| 6945 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6946 | return -1; |
| 6947 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6948 | |
| 6949 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6950 | 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] | 6951 | return -1; |
| 6952 | } |
| 6953 | |
| 6954 | preg = calloc(1, sizeof(regex_t)); |
| 6955 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6956 | 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] | 6957 | return -1; |
| 6958 | } |
| 6959 | |
| 6960 | chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL); |
| 6961 | } |
| 6962 | else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */ |
| 6963 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6964 | if (curproxy == &defproxy) { |
| 6965 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6966 | return -1; |
| 6967 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6968 | |
| 6969 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6970 | 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] | 6971 | return -1; |
| 6972 | } |
| 6973 | |
| 6974 | preg = calloc(1, sizeof(regex_t)); |
| 6975 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6976 | 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] | 6977 | return -1; |
| 6978 | } |
| 6979 | |
| 6980 | chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL); |
| 6981 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6982 | else if (!strcmp(args[0], "reqpass")) { /* pass this header without allowing or denying the request */ |
| 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 | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6988 | |
| 6989 | if (*(args[1]) == 0) { |
| 6990 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
| 6991 | return -1; |
| 6992 | } |
| 6993 | |
| 6994 | preg = calloc(1, sizeof(regex_t)); |
| 6995 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 6996 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 6997 | return -1; |
| 6998 | } |
| 6999 | |
| 7000 | chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL); |
| 7001 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7002 | else if (!strcmp(args[0], "reqallow")) { /* allow a request if a header matches this regex */ |
| 7003 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7004 | if (curproxy == &defproxy) { |
| 7005 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7006 | return -1; |
| 7007 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7008 | |
| 7009 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7010 | 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] | 7011 | return -1; |
| 7012 | } |
| 7013 | |
| 7014 | preg = calloc(1, sizeof(regex_t)); |
| 7015 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7016 | 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] | 7017 | return -1; |
| 7018 | } |
| 7019 | |
| 7020 | chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL); |
| 7021 | } |
| 7022 | else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */ |
| 7023 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7024 | if (curproxy == &defproxy) { |
| 7025 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7026 | return -1; |
| 7027 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7028 | |
| 7029 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7030 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 7031 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7032 | return -1; |
| 7033 | } |
| 7034 | |
| 7035 | preg = calloc(1, sizeof(regex_t)); |
| 7036 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7037 | 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] | 7038 | return -1; |
| 7039 | } |
| 7040 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7041 | err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 7042 | if (err) { |
| 7043 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7044 | file, linenum, *err); |
| 7045 | return -1; |
| 7046 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7047 | } |
| 7048 | else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */ |
| 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 | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7054 | |
| 7055 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7056 | 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] | 7057 | return -1; |
| 7058 | } |
| 7059 | |
| 7060 | preg = calloc(1, sizeof(regex_t)); |
| 7061 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7062 | 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] | 7063 | return -1; |
| 7064 | } |
| 7065 | |
| 7066 | chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL); |
| 7067 | } |
| 7068 | else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */ |
| 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 | REG_ICASE) != 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_DENY, NULL); |
| 7087 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7088 | else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */ |
| 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 | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7094 | |
| 7095 | if (*(args[1]) == 0) { |
| 7096 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
| 7097 | return -1; |
| 7098 | } |
| 7099 | |
| 7100 | preg = calloc(1, sizeof(regex_t)); |
| 7101 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 7102 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7103 | return -1; |
| 7104 | } |
| 7105 | |
| 7106 | chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL); |
| 7107 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7108 | else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */ |
| 7109 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7110 | if (curproxy == &defproxy) { |
| 7111 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7112 | return -1; |
| 7113 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7114 | |
| 7115 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7116 | 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] | 7117 | return -1; |
| 7118 | } |
| 7119 | |
| 7120 | preg = calloc(1, sizeof(regex_t)); |
| 7121 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7122 | 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] | 7123 | return -1; |
| 7124 | } |
| 7125 | |
| 7126 | chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL); |
| 7127 | } |
| 7128 | else if (!strcmp(args[0], "reqadd")) { /* add request header */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7129 | if (curproxy == &defproxy) { |
| 7130 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7131 | return -1; |
| 7132 | } |
| 7133 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7134 | if (curproxy->nb_reqadd >= MAX_NEWHDR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7135 | 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] | 7136 | return 0; |
| 7137 | } |
| 7138 | |
| 7139 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7140 | 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] | 7141 | return -1; |
| 7142 | } |
| 7143 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7144 | curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]); |
| 7145 | } |
| 7146 | else if (!strcmp(args[0], "srvexp") || !strcmp(args[0], "rsprep")) { /* replace response header from a regex */ |
| 7147 | regex_t *preg; |
| 7148 | |
| 7149 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 7150 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 7151 | file, linenum, args[0]); |
| 7152 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7153 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7154 | |
| 7155 | preg = calloc(1, sizeof(regex_t)); |
| 7156 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 7157 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7158 | return -1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7159 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7160 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7161 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 7162 | if (err) { |
| 7163 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7164 | file, linenum, *err); |
| 7165 | return -1; |
| 7166 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7167 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7168 | else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */ |
| 7169 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7170 | if (curproxy == &defproxy) { |
| 7171 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7172 | return -1; |
| 7173 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7174 | |
| 7175 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7176 | 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] | 7177 | return -1; |
| 7178 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7179 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7180 | preg = calloc(1, sizeof(regex_t)); |
| 7181 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7182 | 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] | 7183 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7184 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7185 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7186 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2])); |
| 7187 | if (err) { |
| 7188 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7189 | file, linenum, *err); |
| 7190 | return -1; |
| 7191 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7192 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7193 | else if (!strcmp(args[0], "rspdeny")) { /* block response header from a regex */ |
| 7194 | regex_t *preg; |
| 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 | |
| 7200 | if (*(args[1]) == 0) { |
| 7201 | Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]); |
| 7202 | return -1; |
| 7203 | } |
| 7204 | |
| 7205 | preg = calloc(1, sizeof(regex_t)); |
| 7206 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 7207 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7208 | return -1; |
| 7209 | } |
| 7210 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7211 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2])); |
| 7212 | if (err) { |
| 7213 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7214 | file, linenum, *err); |
| 7215 | return -1; |
| 7216 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7217 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7218 | 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] | 7219 | regex_t *preg; |
| 7220 | if (curproxy == &defproxy) { |
| 7221 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7222 | return -1; |
| 7223 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7224 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7225 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 7226 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 7227 | file, linenum, args[0]); |
| 7228 | return -1; |
| 7229 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7230 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7231 | preg = calloc(1, sizeof(regex_t)); |
| 7232 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 7233 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7234 | return -1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7235 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7236 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7237 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 7238 | if (err) { |
| 7239 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7240 | file, linenum, *err); |
| 7241 | return -1; |
| 7242 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7243 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7244 | else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */ |
| 7245 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7246 | if (curproxy == &defproxy) { |
| 7247 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7248 | return -1; |
| 7249 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7250 | |
| 7251 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7252 | 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] | 7253 | return -1; |
| 7254 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7255 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7256 | preg = calloc(1, sizeof(regex_t)); |
| 7257 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7258 | 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] | 7259 | return -1; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7260 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7261 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7262 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2])); |
| 7263 | if (err) { |
| 7264 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7265 | file, linenum, *err); |
| 7266 | return -1; |
| 7267 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7268 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7269 | else if (!strcmp(args[0], "rspideny")) { /* block response header from a regex ignoring case */ |
| 7270 | regex_t *preg; |
| 7271 | if (curproxy == &defproxy) { |
| 7272 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7273 | return -1; |
| 7274 | } |
| 7275 | |
| 7276 | if (*(args[1]) == 0) { |
| 7277 | Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]); |
| 7278 | return -1; |
| 7279 | } |
| 7280 | |
| 7281 | preg = calloc(1, sizeof(regex_t)); |
| 7282 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 7283 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7284 | return -1; |
| 7285 | } |
| 7286 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7287 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2])); |
| 7288 | if (err) { |
| 7289 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7290 | file, linenum, *err); |
| 7291 | return -1; |
| 7292 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7293 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7294 | else if (!strcmp(args[0], "rspadd")) { /* add response header */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7295 | if (curproxy == &defproxy) { |
| 7296 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7297 | return -1; |
| 7298 | } |
| 7299 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7300 | if (curproxy->nb_rspadd >= MAX_NEWHDR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7301 | 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] | 7302 | return 0; |
| 7303 | } |
| 7304 | |
| 7305 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7306 | 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] | 7307 | return -1; |
| 7308 | } |
| 7309 | |
| 7310 | curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]); |
| 7311 | } |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7312 | else if (!strcmp(args[0], "errorloc") || |
| 7313 | !strcmp(args[0], "errorloc302") || |
| 7314 | !strcmp(args[0], "errorloc303")) { /* error location */ |
| 7315 | int errnum, errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7316 | char *err; |
| 7317 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7318 | // if (curproxy == &defproxy) { |
| 7319 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7320 | // return -1; |
| 7321 | // } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7322 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7323 | if (*(args[2]) == 0) { |
| 7324 | Alert("parsing [%s:%d] : <errorloc> expects <error> and <url> as arguments.\n", file, linenum); |
| 7325 | return -1; |
| 7326 | } |
| 7327 | |
| 7328 | errnum = atol(args[1]); |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7329 | if (!strcmp(args[0], "errorloc303")) { |
| 7330 | err = malloc(strlen(HTTP_303) + strlen(args[2]) + 5); |
| 7331 | errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_303, args[2]); |
| 7332 | } else { |
| 7333 | err = malloc(strlen(HTTP_302) + strlen(args[2]) + 5); |
| 7334 | errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_302, args[2]); |
| 7335 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7336 | |
| 7337 | if (errnum == 400) { |
| 7338 | if (curproxy->errmsg.msg400) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7339 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7340 | free(curproxy->errmsg.msg400); |
| 7341 | } |
| 7342 | curproxy->errmsg.msg400 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7343 | curproxy->errmsg.len400 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7344 | } |
| 7345 | else if (errnum == 403) { |
| 7346 | if (curproxy->errmsg.msg403) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7347 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7348 | free(curproxy->errmsg.msg403); |
| 7349 | } |
| 7350 | curproxy->errmsg.msg403 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7351 | curproxy->errmsg.len403 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7352 | } |
| 7353 | else if (errnum == 408) { |
| 7354 | if (curproxy->errmsg.msg408) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7355 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7356 | free(curproxy->errmsg.msg408); |
| 7357 | } |
| 7358 | curproxy->errmsg.msg408 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7359 | curproxy->errmsg.len408 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7360 | } |
| 7361 | else if (errnum == 500) { |
| 7362 | if (curproxy->errmsg.msg500) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7363 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7364 | free(curproxy->errmsg.msg500); |
| 7365 | } |
| 7366 | curproxy->errmsg.msg500 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7367 | curproxy->errmsg.len500 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7368 | } |
| 7369 | else if (errnum == 502) { |
| 7370 | if (curproxy->errmsg.msg502) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7371 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7372 | free(curproxy->errmsg.msg502); |
| 7373 | } |
| 7374 | curproxy->errmsg.msg502 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7375 | curproxy->errmsg.len502 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7376 | } |
| 7377 | else if (errnum == 503) { |
| 7378 | if (curproxy->errmsg.msg503) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7379 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7380 | free(curproxy->errmsg.msg503); |
| 7381 | } |
| 7382 | curproxy->errmsg.msg503 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7383 | curproxy->errmsg.len503 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7384 | } |
| 7385 | else if (errnum == 504) { |
| 7386 | if (curproxy->errmsg.msg504) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7387 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7388 | free(curproxy->errmsg.msg504); |
| 7389 | } |
| 7390 | curproxy->errmsg.msg504 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7391 | curproxy->errmsg.len504 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7392 | } |
| 7393 | else { |
| 7394 | Warning("parsing [%s:%d] : error %d relocation will be ignored.\n", file, linenum, errnum); |
| 7395 | free(err); |
| 7396 | } |
| 7397 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7398 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7399 | 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] | 7400 | return -1; |
| 7401 | } |
| 7402 | return 0; |
| 7403 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7404 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7405 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7406 | /* |
| 7407 | * This function reads and parses the configuration file given in the argument. |
| 7408 | * returns 0 if OK, -1 if error. |
| 7409 | */ |
| 7410 | int readcfgfile(char *file) { |
| 7411 | char thisline[256]; |
| 7412 | char *line; |
| 7413 | FILE *f; |
| 7414 | int linenum = 0; |
| 7415 | char *end; |
| 7416 | char *args[MAX_LINE_ARGS]; |
| 7417 | int arg; |
| 7418 | int cfgerr = 0; |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame^] | 7419 | int nbchk, mininter; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7420 | int confsect = CFG_NONE; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7421 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7422 | struct proxy *curproxy = NULL; |
| 7423 | struct server *newsrv = NULL; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7424 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7425 | if ((f=fopen(file,"r")) == NULL) |
| 7426 | return -1; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7427 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7428 | init_default_instance(); |
| 7429 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7430 | while (fgets(line = thisline, sizeof(thisline), f) != NULL) { |
| 7431 | linenum++; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7432 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7433 | end = line + strlen(line); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7434 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7435 | /* skip leading spaces */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 7436 | while (isspace((int)*line)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7437 | line++; |
| 7438 | |
| 7439 | arg = 0; |
| 7440 | args[arg] = line; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7441 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7442 | while (*line && arg < MAX_LINE_ARGS) { |
| 7443 | /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their |
| 7444 | * C equivalent value. Other combinations left unchanged (eg: \1). |
| 7445 | */ |
| 7446 | if (*line == '\\') { |
| 7447 | int skip = 0; |
| 7448 | if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') { |
| 7449 | *line = line[1]; |
| 7450 | skip = 1; |
| 7451 | } |
| 7452 | else if (line[1] == 'r') { |
| 7453 | *line = '\r'; |
| 7454 | skip = 1; |
| 7455 | } |
| 7456 | else if (line[1] == 'n') { |
| 7457 | *line = '\n'; |
| 7458 | skip = 1; |
| 7459 | } |
| 7460 | else if (line[1] == 't') { |
| 7461 | *line = '\t'; |
| 7462 | skip = 1; |
| 7463 | } |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7464 | else if (line[1] == 'x') { |
| 7465 | if ((line + 3 < end ) && ishex(line[2]) && ishex(line[3])) { |
| 7466 | unsigned char hex1, hex2; |
| 7467 | hex1 = toupper(line[2]) - '0'; |
| 7468 | hex2 = toupper(line[3]) - '0'; |
| 7469 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 7470 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 7471 | *line = (hex1<<4) + hex2; |
| 7472 | skip = 3; |
| 7473 | } |
| 7474 | else { |
| 7475 | Alert("parsing [%s:%d] : invalid or incomplete '\\x' sequence in '%s'.\n", file, linenum, args[0]); |
| 7476 | return -1; |
| 7477 | } |
| 7478 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7479 | if (skip) { |
| 7480 | memmove(line + 1, line + 1 + skip, end - (line + skip + 1)); |
| 7481 | end -= skip; |
| 7482 | } |
| 7483 | line++; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7484 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7485 | else if (*line == '#' || *line == '\n' || *line == '\r') { |
| 7486 | /* end of string, end of loop */ |
| 7487 | *line = 0; |
| 7488 | break; |
| 7489 | } |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 7490 | else if (isspace((int)*line)) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7491 | /* a non-escaped space is an argument separator */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7492 | *line++ = 0; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 7493 | while (isspace((int)*line)) |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7494 | line++; |
| 7495 | args[++arg] = line; |
| 7496 | } |
| 7497 | else { |
| 7498 | line++; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7499 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7500 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7501 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7502 | /* empty line */ |
| 7503 | if (!**args) |
| 7504 | continue; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7505 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7506 | /* zero out remaining args */ |
| 7507 | while (++arg < MAX_LINE_ARGS) { |
| 7508 | args[arg] = line; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7509 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7510 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7511 | if (!strcmp(args[0], "listen") || !strcmp(args[0], "defaults")) /* new proxy */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7512 | confsect = CFG_LISTEN; |
| 7513 | else if (!strcmp(args[0], "global")) /* global config */ |
| 7514 | confsect = CFG_GLOBAL; |
| 7515 | /* else it's a section keyword */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7516 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7517 | switch (confsect) { |
| 7518 | case CFG_LISTEN: |
| 7519 | if (cfg_parse_listen(file, linenum, args) < 0) |
| 7520 | return -1; |
| 7521 | break; |
| 7522 | case CFG_GLOBAL: |
| 7523 | if (cfg_parse_global(file, linenum, args) < 0) |
| 7524 | return -1; |
| 7525 | break; |
| 7526 | default: |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7527 | 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] | 7528 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7529 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7530 | |
| 7531 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7532 | } |
| 7533 | fclose(f); |
| 7534 | |
| 7535 | /* |
| 7536 | * Now, check for the integrity of all that we have collected. |
| 7537 | */ |
| 7538 | |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame^] | 7539 | /* will be needed further to delay some tasks */ |
| 7540 | tv_now(&now); |
| 7541 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7542 | if ((curproxy = proxy) == NULL) { |
| 7543 | Alert("parsing %s : no <listen> line. Nothing to do !\n", |
| 7544 | file); |
| 7545 | return -1; |
| 7546 | } |
| 7547 | |
| 7548 | while (curproxy != NULL) { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 7549 | curproxy->cursrv = NULL; |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7550 | if (curproxy->state == PR_STSTOPPED) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 7551 | curproxy = curproxy->next; |
| 7552 | continue; |
| 7553 | } |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 7554 | |
| 7555 | if (curproxy->listen == NULL) { |
| 7556 | 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); |
| 7557 | cfgerr++; |
| 7558 | } |
| 7559 | else if ((curproxy->mode != PR_MODE_HEALTH) && |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7560 | !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) && |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7561 | (*(int *)&curproxy->dispatch_addr.sin_addr == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7562 | Alert("parsing %s : listener %s has no dispatch address and is not in transparent or balance mode.\n", |
| 7563 | file, curproxy->id); |
| 7564 | cfgerr++; |
| 7565 | } |
| 7566 | else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) { |
| 7567 | if (curproxy->options & PR_O_TRANSP) { |
| 7568 | Alert("parsing %s : listener %s cannot use both transparent and balance mode.\n", |
| 7569 | file, curproxy->id); |
| 7570 | cfgerr++; |
| 7571 | } |
| 7572 | else if (curproxy->srv == NULL) { |
| 7573 | Alert("parsing %s : listener %s needs at least 1 server in balance mode.\n", |
| 7574 | file, curproxy->id); |
| 7575 | cfgerr++; |
| 7576 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7577 | else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7578 | Warning("parsing %s : dispatch address of listener %s will be ignored in balance mode.\n", |
| 7579 | file, curproxy->id); |
| 7580 | } |
| 7581 | } |
| 7582 | 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] | 7583 | if (curproxy->cookie_name != NULL) { |
| 7584 | Warning("parsing %s : cookie will be ignored for listener %s.\n", |
| 7585 | file, curproxy->id); |
| 7586 | } |
| 7587 | if ((newsrv = curproxy->srv) != NULL) { |
| 7588 | Warning("parsing %s : servers will be ignored for listener %s.\n", |
| 7589 | file, curproxy->id); |
| 7590 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7591 | if (curproxy->rsp_exp != NULL) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7592 | Warning("parsing %s : server regular expressions will be ignored for listener %s.\n", |
| 7593 | file, curproxy->id); |
| 7594 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7595 | if (curproxy->req_exp != NULL) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7596 | Warning("parsing %s : client regular expressions will be ignored for listener %s.\n", |
| 7597 | file, curproxy->id); |
| 7598 | } |
| 7599 | } |
| 7600 | else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */ |
| 7601 | if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) { |
| 7602 | Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n", |
| 7603 | file, curproxy->id); |
| 7604 | cfgerr++; |
| 7605 | } |
| 7606 | else { |
| 7607 | while (newsrv != NULL) { |
| 7608 | /* nothing to check for now */ |
| 7609 | newsrv = newsrv->next; |
| 7610 | } |
| 7611 | } |
| 7612 | } |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 7613 | |
| 7614 | if (curproxy->options & PR_O_LOGASAP) |
| 7615 | curproxy->to_log &= ~LW_BYTES; |
| 7616 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7617 | if (curproxy->errmsg.msg400 == NULL) { |
| 7618 | curproxy->errmsg.msg400 = (char *)HTTP_400; |
| 7619 | curproxy->errmsg.len400 = strlen(HTTP_400); |
| 7620 | } |
| 7621 | if (curproxy->errmsg.msg403 == NULL) { |
| 7622 | curproxy->errmsg.msg403 = (char *)HTTP_403; |
| 7623 | curproxy->errmsg.len403 = strlen(HTTP_403); |
| 7624 | } |
| 7625 | if (curproxy->errmsg.msg408 == NULL) { |
| 7626 | curproxy->errmsg.msg408 = (char *)HTTP_408; |
| 7627 | curproxy->errmsg.len408 = strlen(HTTP_408); |
| 7628 | } |
| 7629 | if (curproxy->errmsg.msg500 == NULL) { |
| 7630 | curproxy->errmsg.msg500 = (char *)HTTP_500; |
| 7631 | curproxy->errmsg.len500 = strlen(HTTP_500); |
| 7632 | } |
| 7633 | if (curproxy->errmsg.msg502 == NULL) { |
| 7634 | curproxy->errmsg.msg502 = (char *)HTTP_502; |
| 7635 | curproxy->errmsg.len502 = strlen(HTTP_502); |
| 7636 | } |
| 7637 | if (curproxy->errmsg.msg503 == NULL) { |
| 7638 | curproxy->errmsg.msg503 = (char *)HTTP_503; |
| 7639 | curproxy->errmsg.len503 = strlen(HTTP_503); |
| 7640 | } |
| 7641 | if (curproxy->errmsg.msg504 == NULL) { |
| 7642 | curproxy->errmsg.msg504 = (char *)HTTP_504; |
| 7643 | curproxy->errmsg.len504 = strlen(HTTP_504); |
| 7644 | } |
Willy TARREAU | 3759f98 | 2006-03-01 22:44:17 +0100 | [diff] [blame^] | 7645 | |
| 7646 | /* now we'll start this proxy's health checks if any */ |
| 7647 | /* 1- count the checkers to run simultaneously */ |
| 7648 | nbchk = 0; |
| 7649 | mininter = 0; |
| 7650 | newsrv = curproxy->srv; |
| 7651 | while (newsrv != NULL) { |
| 7652 | if (newsrv->state & SRV_CHECKED) { |
| 7653 | if (!mininter || mininter > newsrv->inter) |
| 7654 | mininter = newsrv->inter; |
| 7655 | nbchk++; |
| 7656 | } |
| 7657 | newsrv = newsrv->next; |
| 7658 | } |
| 7659 | |
| 7660 | /* 2- start them as far as possible from each others while respecting |
| 7661 | * their own intervals. For this, we will start them after their own |
| 7662 | * interval added to the min interval divided by the number of servers, |
| 7663 | * weighted by the server's position in the list. |
| 7664 | */ |
| 7665 | if (nbchk > 0) { |
| 7666 | struct task *t; |
| 7667 | int srvpos; |
| 7668 | |
| 7669 | newsrv = curproxy->srv; |
| 7670 | srvpos = 0; |
| 7671 | while (newsrv != NULL) { |
| 7672 | /* should this server be checked ? */ |
| 7673 | if (newsrv->state & SRV_CHECKED) { |
| 7674 | if ((t = pool_alloc(task)) == NULL) { |
| 7675 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 7676 | return -1; |
| 7677 | } |
| 7678 | |
| 7679 | t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */ |
| 7680 | t->wq = LIST_HEAD(wait_queue); /* but already has a wait queue assigned */ |
| 7681 | t->state = TASK_IDLE; |
| 7682 | t->process = process_chk; |
| 7683 | t->context = newsrv; |
| 7684 | |
| 7685 | /* check this every ms */ |
| 7686 | tv_delayfrom(&t->expire, &now, |
| 7687 | newsrv->inter + mininter * srvpos / nbchk); |
| 7688 | task_queue(t); |
| 7689 | //task_wakeup(&rq, t); |
| 7690 | srvpos++; |
| 7691 | } |
| 7692 | newsrv = newsrv->next; |
| 7693 | } |
| 7694 | } |
| 7695 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7696 | curproxy = curproxy->next; |
| 7697 | } |
| 7698 | if (cfgerr > 0) { |
| 7699 | Alert("Errors found in configuration file, aborting.\n"); |
| 7700 | return -1; |
| 7701 | } |
| 7702 | else |
| 7703 | return 0; |
| 7704 | } |
| 7705 | |
| 7706 | |
| 7707 | /* |
| 7708 | * This function initializes all the necessary variables. It only returns |
| 7709 | * if everything is OK. If something fails, it exits. |
| 7710 | */ |
| 7711 | void init(int argc, char **argv) { |
| 7712 | int i; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7713 | int arg_mode = 0; /* MODE_DEBUG, ... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7714 | char *old_argv = *argv; |
| 7715 | char *tmp; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 7716 | char *cfg_pidfile = NULL; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7717 | int cfg_maxconn = 0; /* # of simultaneous connections, (-n) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7718 | |
| 7719 | if (1<<INTBITS != sizeof(int)*8) { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 7720 | fprintf(stderr, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7721 | "Error: wrong architecture. Recompile so that sizeof(int)=%d\n", |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7722 | (int)(sizeof(int)*8)); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7723 | exit(1); |
| 7724 | } |
| 7725 | |
Willy TARREAU | a9e75f6 | 2006-03-01 22:27:48 +0100 | [diff] [blame] | 7726 | /* initialize the libc's localtime structures once for all so that we |
| 7727 | * won't be missing memory if we want to send alerts under OOM conditions. |
| 7728 | */ |
| 7729 | tv_now(&now); |
| 7730 | localtime(&now.tv_sec); |
| 7731 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7732 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 7733 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 7734 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 7735 | * printable characters above. |
| 7736 | */ |
| 7737 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 7738 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 7739 | for (i = 0; i < 32; i++) { |
| 7740 | FD_SET(i, hdr_encode_map); |
| 7741 | FD_SET(i, url_encode_map); |
| 7742 | } |
| 7743 | for (i = 127; i < 256; i++) { |
| 7744 | FD_SET(i, hdr_encode_map); |
| 7745 | FD_SET(i, url_encode_map); |
| 7746 | } |
| 7747 | |
| 7748 | tmp = "\"#{|}"; |
| 7749 | while (*tmp) { |
| 7750 | FD_SET(*tmp, hdr_encode_map); |
| 7751 | tmp++; |
| 7752 | } |
| 7753 | |
| 7754 | tmp = "\"#"; |
| 7755 | while (*tmp) { |
| 7756 | FD_SET(*tmp, url_encode_map); |
| 7757 | tmp++; |
| 7758 | } |
| 7759 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 7760 | cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */ |
| 7761 | #if defined(ENABLE_POLL) |
| 7762 | cfg_polling_mechanism |= POLL_USE_POLL; |
| 7763 | #endif |
| 7764 | #if defined(ENABLE_EPOLL) |
| 7765 | cfg_polling_mechanism |= POLL_USE_EPOLL; |
| 7766 | #endif |
| 7767 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7768 | pid = getpid(); |
| 7769 | progname = *argv; |
| 7770 | while ((tmp = strchr(progname, '/')) != NULL) |
| 7771 | progname = tmp + 1; |
| 7772 | |
| 7773 | argc--; argv++; |
| 7774 | while (argc > 0) { |
| 7775 | char *flag; |
| 7776 | |
| 7777 | if (**argv == '-') { |
| 7778 | flag = *argv+1; |
| 7779 | |
| 7780 | /* 1 arg */ |
| 7781 | if (*flag == 'v') { |
| 7782 | display_version(); |
| 7783 | exit(0); |
| 7784 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7785 | #if defined(ENABLE_EPOLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 7786 | else if (*flag == 'd' && flag[1] == 'e') |
| 7787 | cfg_polling_mechanism &= ~POLL_USE_EPOLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7788 | #endif |
| 7789 | #if defined(ENABLE_POLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 7790 | else if (*flag == 'd' && flag[1] == 'p') |
| 7791 | cfg_polling_mechanism &= ~POLL_USE_POLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7792 | #endif |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7793 | else if (*flag == 'V') |
| 7794 | arg_mode |= MODE_VERBOSE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7795 | else if (*flag == 'd') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7796 | arg_mode |= MODE_DEBUG; |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 7797 | else if (*flag == 'c') |
| 7798 | arg_mode |= MODE_CHECK; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7799 | else if (*flag == 'D') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7800 | arg_mode |= MODE_DAEMON | MODE_QUIET; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7801 | else if (*flag == 'q') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7802 | arg_mode |= MODE_QUIET; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7803 | #if STATTIME > 0 |
| 7804 | else if (*flag == 's') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7805 | arg_mode |= MODE_STATS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7806 | else if (*flag == 'l') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7807 | arg_mode |= MODE_LOG; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7808 | #endif |
| 7809 | else { /* >=2 args */ |
| 7810 | argv++; argc--; |
| 7811 | if (argc == 0) |
| 7812 | usage(old_argv); |
| 7813 | |
| 7814 | switch (*flag) { |
| 7815 | case 'n' : cfg_maxconn = atol(*argv); break; |
| 7816 | case 'N' : cfg_maxpconn = atol(*argv); break; |
| 7817 | case 'f' : cfg_cfgfile = *argv; break; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 7818 | case 'p' : cfg_pidfile = *argv; break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7819 | default: usage(old_argv); |
| 7820 | } |
| 7821 | } |
| 7822 | } |
| 7823 | else |
| 7824 | usage(old_argv); |
| 7825 | argv++; argc--; |
| 7826 | } |
| 7827 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 7828 | global.mode = MODE_STARTING | /* during startup, we want most of the alerts */ |
| 7829 | (arg_mode & (MODE_DAEMON | MODE_VERBOSE | MODE_QUIET | MODE_CHECK | MODE_DEBUG)); |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 7830 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7831 | if (!cfg_cfgfile) |
| 7832 | usage(old_argv); |
| 7833 | |
| 7834 | gethostname(hostname, MAX_HOSTNAME_LEN); |
| 7835 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7836 | have_appsession = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7837 | if (readcfgfile(cfg_cfgfile) < 0) { |
| 7838 | Alert("Error reading configuration file : %s\n", cfg_cfgfile); |
| 7839 | exit(1); |
| 7840 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7841 | if (have_appsession) |
| 7842 | appsession_init(); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7843 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7844 | if (global.mode & MODE_CHECK) { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 7845 | qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile); |
| 7846 | exit(0); |
| 7847 | } |
| 7848 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7849 | if (cfg_maxconn > 0) |
| 7850 | global.maxconn = cfg_maxconn; |
| 7851 | |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 7852 | if (cfg_pidfile) { |
| 7853 | if (global.pidfile) |
| 7854 | free(global.pidfile); |
| 7855 | global.pidfile = strdup(cfg_pidfile); |
| 7856 | } |
| 7857 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7858 | if (global.maxconn == 0) |
| 7859 | global.maxconn = DEFAULT_MAXCONN; |
| 7860 | |
| 7861 | global.maxsock = global.maxconn * 2; /* each connection needs two sockets */ |
| 7862 | |
| 7863 | if (arg_mode & MODE_DEBUG) { |
| 7864 | /* command line debug mode inhibits configuration mode */ |
| 7865 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 7866 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7867 | global.mode |= (arg_mode & (MODE_DAEMON | MODE_QUIET | MODE_VERBOSE |
| 7868 | | MODE_DEBUG | MODE_STATS | MODE_LOG)); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7869 | |
| 7870 | if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) { |
| 7871 | Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n"); |
| 7872 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 7873 | } |
| 7874 | |
| 7875 | if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) { |
| 7876 | Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n"); |
| 7877 | global.nbproc = 1; |
| 7878 | } |
| 7879 | |
| 7880 | if (global.nbproc < 1) |
| 7881 | global.nbproc = 1; |
| 7882 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7883 | StaticReadEvent = (fd_set *)calloc(1, |
| 7884 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7885 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7886 | StaticWriteEvent = (fd_set *)calloc(1, |
| 7887 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7888 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7889 | |
| 7890 | fdtab = (struct fdtab *)calloc(1, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7891 | sizeof(struct fdtab) * (global.maxsock)); |
| 7892 | for (i = 0; i < global.maxsock; i++) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7893 | fdtab[i].state = FD_STCLOSE; |
| 7894 | } |
| 7895 | } |
| 7896 | |
| 7897 | /* |
| 7898 | * this function starts all the proxies. It returns 0 if OK, -1 if not. |
| 7899 | */ |
| 7900 | int start_proxies() { |
| 7901 | struct proxy *curproxy; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7902 | struct listener *listener; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7903 | int fd; |
| 7904 | |
| 7905 | for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) { |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7906 | if (curproxy->state == PR_STSTOPPED) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7907 | continue; |
| 7908 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7909 | for (listener = curproxy->listen; listener != NULL; listener = listener->next) { |
| 7910 | if ((fd = listener->fd = |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 7911 | socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7912 | Alert("cannot create listening socket for proxy %s. Aborting.\n", |
| 7913 | curproxy->id); |
| 7914 | return -1; |
| 7915 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7916 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7917 | if (fd >= global.maxsock) { |
| 7918 | Alert("socket(): not enough free sockets for proxy %s. Raise -n argument. Aborting.\n", |
| 7919 | curproxy->id); |
| 7920 | close(fd); |
| 7921 | return -1; |
| 7922 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7923 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7924 | if ((fcntl(fd, F_SETFL, O_NONBLOCK) == -1) || |
| 7925 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 7926 | (char *) &one, sizeof(one)) == -1)) { |
| 7927 | Alert("cannot make socket non-blocking for proxy %s. Aborting.\n", |
| 7928 | curproxy->id); |
| 7929 | close(fd); |
| 7930 | return -1; |
| 7931 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7932 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7933 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) { |
| 7934 | Alert("cannot do so_reuseaddr for proxy %s. Continuing.\n", |
| 7935 | curproxy->id); |
| 7936 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7937 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7938 | if (bind(fd, |
| 7939 | (struct sockaddr *)&listener->addr, |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 7940 | listener->addr.ss_family == AF_INET6 ? |
| 7941 | sizeof(struct sockaddr_in6) : |
| 7942 | sizeof(struct sockaddr_in)) == -1) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7943 | Alert("cannot bind socket for proxy %s. Aborting.\n", |
| 7944 | curproxy->id); |
| 7945 | close(fd); |
| 7946 | return -1; |
| 7947 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7948 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7949 | if (listen(fd, curproxy->maxconn) == -1) { |
| 7950 | Alert("cannot listen to socket for proxy %s. Aborting.\n", |
| 7951 | curproxy->id); |
| 7952 | close(fd); |
| 7953 | return -1; |
| 7954 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7955 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7956 | /* the function for the accept() event */ |
| 7957 | fdtab[fd].read = &event_accept; |
| 7958 | fdtab[fd].write = NULL; /* never called */ |
| 7959 | fdtab[fd].owner = (struct task *)curproxy; /* reference the proxy instead of a task */ |
| 7960 | curproxy->state = PR_STRUN; |
| 7961 | fdtab[fd].state = FD_STLISTEN; |
| 7962 | FD_SET(fd, StaticReadEvent); |
| 7963 | fd_insert(fd); |
| 7964 | listeners++; |
| 7965 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7966 | send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7967 | } |
| 7968 | return 0; |
| 7969 | } |
| 7970 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 7971 | int match_str(const void *key1, const void *key2) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7972 | |
| 7973 | appsess *temp1,*temp2; |
| 7974 | temp1 = (appsess *)key1; |
| 7975 | temp2 = (appsess *)key2; |
| 7976 | |
| 7977 | //fprintf(stdout,">>>>>>>>>>>>>>temp1->sessid :%s:\n",temp1->sessid); |
| 7978 | //fprintf(stdout,">>>>>>>>>>>>>>temp2->sessid :%s:\n",temp2->sessid); |
| 7979 | |
| 7980 | return (strcmp(temp1->sessid,temp2->sessid) == 0); |
| 7981 | }/* end match_str */ |
| 7982 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 7983 | void destroy(void *data) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7984 | appsess *temp1; |
| 7985 | |
| 7986 | //printf("destroy called\n"); |
| 7987 | temp1 = (appsess *)data; |
| 7988 | |
| 7989 | if (temp1->sessid) |
| 7990 | pool_free_to(apools.sessid, temp1->sessid); |
| 7991 | |
| 7992 | if (temp1->serverid) |
| 7993 | pool_free_to(apools.serverid, temp1->serverid); |
| 7994 | |
| 7995 | pool_free(appsess, temp1); |
| 7996 | } /* end destroy */ |
| 7997 | |
| 7998 | void appsession_cleanup( void ) |
| 7999 | { |
| 8000 | struct proxy *p = proxy; |
| 8001 | |
| 8002 | while(p) { |
| 8003 | chtbl_destroy(&(p->htbl_proxy)); |
| 8004 | p = p->next; |
| 8005 | } |
| 8006 | }/* end appsession_cleanup() */ |
| 8007 | |
| 8008 | void pool_destroy(void **pool) |
| 8009 | { |
| 8010 | void *temp, *next; |
| 8011 | next = pool; |
| 8012 | while (next) { |
| 8013 | temp = next; |
| 8014 | next = *(void **)temp; |
| 8015 | free(temp); |
| 8016 | } |
| 8017 | }/* end pool_destroy() */ |
| 8018 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 8019 | void deinit(void) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8020 | struct proxy *p = proxy; |
| 8021 | struct cap_hdr *h,*h_next; |
| 8022 | struct server *s,*s_next; |
| 8023 | struct listener *l,*l_next; |
| 8024 | |
| 8025 | while (p) { |
| 8026 | if (p->id) |
| 8027 | free(p->id); |
| 8028 | |
| 8029 | if (p->check_req) |
| 8030 | free(p->check_req); |
| 8031 | |
| 8032 | if (p->cookie_name) |
| 8033 | free(p->cookie_name); |
| 8034 | |
| 8035 | if (p->capture_name) |
| 8036 | free(p->capture_name); |
| 8037 | |
| 8038 | /* only strup if the user have set in config. |
| 8039 | When should we free it?! |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 8040 | if (p->errmsg.msg400) free(p->errmsg.msg400); |
| 8041 | if (p->errmsg.msg403) free(p->errmsg.msg403); |
| 8042 | if (p->errmsg.msg408) free(p->errmsg.msg408); |
| 8043 | if (p->errmsg.msg500) free(p->errmsg.msg500); |
| 8044 | if (p->errmsg.msg502) free(p->errmsg.msg502); |
| 8045 | if (p->errmsg.msg503) free(p->errmsg.msg503); |
| 8046 | if (p->errmsg.msg504) free(p->errmsg.msg504); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8047 | */ |
| 8048 | if (p->appsession_name) |
| 8049 | free(p->appsession_name); |
| 8050 | |
| 8051 | h = p->req_cap; |
| 8052 | while (h) { |
| 8053 | h_next = h->next; |
| 8054 | if (h->name) |
| 8055 | free(h->name); |
| 8056 | pool_destroy(h->pool); |
| 8057 | free(h); |
| 8058 | h = h_next; |
| 8059 | }/* end while(h) */ |
| 8060 | |
| 8061 | h = p->rsp_cap; |
| 8062 | while (h) { |
| 8063 | h_next = h->next; |
| 8064 | if (h->name) |
| 8065 | free(h->name); |
| 8066 | |
| 8067 | pool_destroy(h->pool); |
| 8068 | free(h); |
| 8069 | h = h_next; |
| 8070 | }/* end while(h) */ |
| 8071 | |
| 8072 | s = p->srv; |
| 8073 | while (s) { |
| 8074 | s_next = s->next; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 8075 | if (s->id) |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8076 | free(s->id); |
| 8077 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 8078 | if (s->cookie) |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8079 | free(s->cookie); |
| 8080 | |
| 8081 | free(s); |
| 8082 | s = s_next; |
| 8083 | }/* end while(s) */ |
| 8084 | |
| 8085 | l = p->listen; |
| 8086 | while (l) { |
| 8087 | l_next = l->next; |
| 8088 | free(l); |
| 8089 | l = l_next; |
| 8090 | }/* end while(l) */ |
| 8091 | |
| 8092 | pool_destroy((void **) p->req_cap_pool); |
| 8093 | pool_destroy((void **) p->rsp_cap_pool); |
| 8094 | p = p->next; |
| 8095 | }/* end while(p) */ |
| 8096 | |
| 8097 | if (global.chroot) free(global.chroot); |
| 8098 | if (global.pidfile) free(global.pidfile); |
| 8099 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8100 | if (StaticReadEvent) free(StaticReadEvent); |
| 8101 | if (StaticWriteEvent) free(StaticWriteEvent); |
| 8102 | if (fdtab) free(fdtab); |
| 8103 | |
| 8104 | pool_destroy(pool_session); |
| 8105 | pool_destroy(pool_buffer); |
| 8106 | pool_destroy(pool_fdtab); |
| 8107 | pool_destroy(pool_requri); |
| 8108 | pool_destroy(pool_task); |
| 8109 | pool_destroy(pool_capture); |
| 8110 | pool_destroy(pool_appsess); |
| 8111 | |
| 8112 | if (have_appsession) { |
| 8113 | pool_destroy(apools.serverid); |
| 8114 | pool_destroy(apools.sessid); |
| 8115 | } |
| 8116 | } /* end deinit() */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8117 | |
| 8118 | int main(int argc, char **argv) { |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 8119 | struct rlimit limit; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8120 | FILE *pidfile = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8121 | init(argc, argv); |
| 8122 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8123 | signal(SIGQUIT, dump); |
| 8124 | signal(SIGUSR1, sig_soft_stop); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 8125 | signal(SIGHUP, sig_dump_state); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8126 | #ifdef DEBUG_MEMORY |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8127 | signal(SIGINT, sig_int); |
| 8128 | signal(SIGTERM, sig_term); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8129 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8130 | |
| 8131 | /* on very high loads, a sigpipe sometimes happen just between the |
| 8132 | * getsockopt() which tells "it's OK to write", and the following write :-( |
| 8133 | */ |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 8134 | #ifndef MSG_NOSIGNAL |
| 8135 | signal(SIGPIPE, SIG_IGN); |
| 8136 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8137 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8138 | /* start_proxies() sends an alert when it fails. */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8139 | if (start_proxies() < 0) |
| 8140 | exit(1); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8141 | |
| 8142 | if (listeners == 0) { |
| 8143 | Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]); |
| 8144 | exit(1); |
| 8145 | } |
| 8146 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 8147 | /* prepare pause/play signals */ |
| 8148 | signal(SIGTTOU, sig_pause); |
| 8149 | signal(SIGTTIN, sig_listen); |
| 8150 | |
Willy TARREAU | e3283d1 | 2006-03-01 22:15:29 +0100 | [diff] [blame] | 8151 | if (global.mode & MODE_DAEMON) { |
| 8152 | global.mode &= ~MODE_VERBOSE; |
| 8153 | global.mode |= MODE_QUIET; |
| 8154 | } |
| 8155 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8156 | /* MODE_QUIET can inhibit alerts and warnings below this line */ |
| 8157 | |
| 8158 | global.mode &= ~MODE_STARTING; |
Willy TARREAU | e3283d1 | 2006-03-01 22:15:29 +0100 | [diff] [blame] | 8159 | if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) { |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8160 | /* detach from the tty */ |
| 8161 | fclose(stdin); fclose(stdout); fclose(stderr); |
| 8162 | close(0); close(1); close(2); |
| 8163 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8164 | |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8165 | /* open log & pid files before the chroot */ |
| 8166 | if (global.mode & MODE_DAEMON && global.pidfile != NULL) { |
| 8167 | int pidfd; |
| 8168 | unlink(global.pidfile); |
| 8169 | pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644); |
| 8170 | if (pidfd < 0) { |
| 8171 | Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile); |
| 8172 | exit(1); |
| 8173 | } |
| 8174 | pidfile = fdopen(pidfd, "w"); |
| 8175 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8176 | |
| 8177 | /* chroot if needed */ |
| 8178 | if (global.chroot != NULL) { |
| 8179 | if (chroot(global.chroot) == -1) { |
| 8180 | Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot); |
| 8181 | exit(1); |
| 8182 | } |
| 8183 | chdir("/"); |
| 8184 | } |
| 8185 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 8186 | /* ulimits */ |
| 8187 | if (global.rlimit_nofile) { |
| 8188 | limit.rlim_cur = limit.rlim_max = global.rlimit_nofile; |
| 8189 | if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { |
| 8190 | Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile); |
| 8191 | } |
| 8192 | } |
| 8193 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8194 | /* setgid / setuid */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8195 | if (global.gid && setgid(global.gid) == -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8196 | Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid); |
| 8197 | exit(1); |
| 8198 | } |
| 8199 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8200 | if (global.uid && setuid(global.uid) == -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8201 | Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid); |
| 8202 | exit(1); |
| 8203 | } |
| 8204 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 8205 | /* check ulimits */ |
| 8206 | limit.rlim_cur = limit.rlim_max = 0; |
| 8207 | getrlimit(RLIMIT_NOFILE, &limit); |
| 8208 | if (limit.rlim_cur < global.maxsock) { |
| 8209 | 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", |
| 8210 | argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock); |
| 8211 | } |
| 8212 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8213 | if (global.mode & MODE_DAEMON) { |
| 8214 | int ret = 0; |
| 8215 | int proc; |
| 8216 | |
| 8217 | /* the father launches the required number of processes */ |
| 8218 | for (proc = 0; proc < global.nbproc; proc++) { |
| 8219 | ret = fork(); |
| 8220 | if (ret < 0) { |
| 8221 | Alert("[%s.main()] Cannot fork.\n", argv[0]); |
| 8222 | exit(1); /* there has been an error */ |
| 8223 | } |
| 8224 | else if (ret == 0) /* child breaks here */ |
| 8225 | break; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8226 | if (pidfile != NULL) { |
| 8227 | fprintf(pidfile, "%d\n", ret); |
| 8228 | fflush(pidfile); |
| 8229 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8230 | } |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8231 | /* close the pidfile both in children and father */ |
| 8232 | if (pidfile != NULL) |
| 8233 | fclose(pidfile); |
| 8234 | free(global.pidfile); |
| 8235 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8236 | if (proc == global.nbproc) |
| 8237 | exit(0); /* parent must leave */ |
| 8238 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 8239 | /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure |
| 8240 | * that we can detach from the TTY. We MUST NOT do it in other cases since |
| 8241 | * it would have already be done, and 0-2 would have been affected to listening |
| 8242 | * sockets |
| 8243 | */ |
| 8244 | if (!(global.mode & MODE_QUIET)) { |
| 8245 | /* detach from the tty */ |
| 8246 | fclose(stdin); fclose(stdout); fclose(stderr); |
| 8247 | close(0); close(1); close(2); /* close all fd's */ |
| 8248 | global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */ |
| 8249 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 8250 | pid = getpid(); /* update child's pid */ |
willy tarreau | e867b48 | 2005-12-17 13:28:43 +0100 | [diff] [blame] | 8251 | setsid(); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8252 | } |
| 8253 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8254 | #if defined(ENABLE_EPOLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8255 | if (cfg_polling_mechanism & POLL_USE_EPOLL) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8256 | if (epoll_loop(POLL_LOOP_ACTION_INIT)) { |
| 8257 | epoll_loop(POLL_LOOP_ACTION_RUN); |
| 8258 | epoll_loop(POLL_LOOP_ACTION_CLEAN); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8259 | cfg_polling_mechanism &= POLL_USE_EPOLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8260 | } |
| 8261 | else { |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8262 | Warning("epoll() is not available. Using poll()/select() instead.\n"); |
| 8263 | cfg_polling_mechanism &= ~POLL_USE_EPOLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8264 | } |
| 8265 | } |
| 8266 | #endif |
| 8267 | |
| 8268 | #if defined(ENABLE_POLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8269 | if (cfg_polling_mechanism & POLL_USE_POLL) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8270 | if (poll_loop(POLL_LOOP_ACTION_INIT)) { |
| 8271 | poll_loop(POLL_LOOP_ACTION_RUN); |
| 8272 | poll_loop(POLL_LOOP_ACTION_CLEAN); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8273 | cfg_polling_mechanism &= POLL_USE_POLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8274 | } |
| 8275 | else { |
| 8276 | Warning("poll() is not available. Using select() instead.\n"); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8277 | cfg_polling_mechanism &= ~POLL_USE_POLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8278 | } |
| 8279 | } |
| 8280 | #endif |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8281 | if (cfg_polling_mechanism & POLL_USE_SELECT) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8282 | if (select_loop(POLL_LOOP_ACTION_INIT)) { |
| 8283 | select_loop(POLL_LOOP_ACTION_RUN); |
| 8284 | select_loop(POLL_LOOP_ACTION_CLEAN); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8285 | cfg_polling_mechanism &= POLL_USE_SELECT; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8286 | } |
| 8287 | } |
| 8288 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8289 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8290 | /* Free all Hash Keys and all Hash elements */ |
| 8291 | appsession_cleanup(); |
| 8292 | /* Do some cleanup */ |
| 8293 | deinit(); |
| 8294 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8295 | exit(0); |
| 8296 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8297 | |
| 8298 | #if defined(DEBUG_HASH) |
| 8299 | static void print_table(const CHTbl *htbl) { |
| 8300 | |
| 8301 | ListElmt *element; |
| 8302 | int i; |
| 8303 | appsess *asession; |
| 8304 | |
| 8305 | /***************************************************************************** |
| 8306 | * * |
| 8307 | * Display the chained hash table. * |
| 8308 | * * |
| 8309 | *****************************************************************************/ |
| 8310 | |
| 8311 | fprintf(stdout, "Table size is %d\n", chtbl_size(htbl)); |
| 8312 | |
| 8313 | for (i = 0; i < TBLSIZ; i++) { |
| 8314 | fprintf(stdout, "Bucket[%03d]\n", i); |
| 8315 | |
| 8316 | for (element = list_head(&htbl->table[i]); element != NULL; element = list_next(element)) { |
| 8317 | //fprintf(stdout, "%c", *(char *)list_data(element)); |
| 8318 | asession = (appsess *)list_data(element); |
| 8319 | fprintf(stdout, "ELEM :%s:", asession->sessid); |
| 8320 | fprintf(stdout, " Server :%s: \n", asession->serverid); |
| 8321 | //fprintf(stdout, " Server request_count :%li:\n",asession->request_count); |
| 8322 | } |
| 8323 | |
| 8324 | fprintf(stdout, "\n"); |
| 8325 | } |
| 8326 | return; |
| 8327 | } /* end print_table */ |
| 8328 | #endif |
| 8329 | |
| 8330 | static int appsession_init(void) |
| 8331 | { |
| 8332 | static int initialized = 0; |
| 8333 | int idlen; |
| 8334 | struct server *s; |
| 8335 | struct proxy *p = proxy; |
| 8336 | |
| 8337 | if (!initialized) { |
| 8338 | if (!appsession_task_init()) { |
| 8339 | apools.sessid = NULL; |
| 8340 | apools.serverid = NULL; |
| 8341 | apools.ser_waste = 0; |
| 8342 | apools.ser_use = 0; |
| 8343 | apools.ser_msize = sizeof(void *); |
| 8344 | apools.ses_waste = 0; |
| 8345 | apools.ses_use = 0; |
| 8346 | apools.ses_msize = sizeof(void *); |
| 8347 | while (p) { |
| 8348 | s = p->srv; |
| 8349 | if (apools.ses_msize < p->appsession_len) |
| 8350 | apools.ses_msize = p->appsession_len; |
| 8351 | while (s) { |
| 8352 | idlen = strlen(s->id); |
| 8353 | if (apools.ser_msize < idlen) |
| 8354 | apools.ser_msize = idlen; |
| 8355 | s = s->next; |
| 8356 | } |
| 8357 | p = p->next; |
| 8358 | } |
| 8359 | apools.ser_msize ++; /* we use strings, so reserve space for '\0' */ |
| 8360 | apools.ses_msize ++; |
| 8361 | } |
| 8362 | else { |
| 8363 | fprintf(stderr, "appsession_task_init failed\n"); |
| 8364 | return -1; |
| 8365 | } |
| 8366 | initialized ++; |
| 8367 | } |
| 8368 | return 0; |
| 8369 | } |
| 8370 | |
| 8371 | static int appsession_task_init(void) |
| 8372 | { |
| 8373 | static int initialized = 0; |
| 8374 | struct task *t; |
| 8375 | if (!initialized) { |
| 8376 | if ((t = pool_alloc(task)) == NULL) |
| 8377 | return -1; |
| 8378 | t->next = t->prev = t->rqnext = NULL; |
| 8379 | t->wq = LIST_HEAD(wait_queue); |
| 8380 | t->state = TASK_IDLE; |
| 8381 | t->context = NULL; |
| 8382 | tv_delayfrom(&t->expire, &now, TBLCHKINT); |
| 8383 | task_queue(t); |
| 8384 | t->process = appsession_refresh; |
| 8385 | initialized ++; |
| 8386 | } |
| 8387 | return 0; |
| 8388 | } |
| 8389 | |
| 8390 | static int appsession_refresh(struct task *t) { |
| 8391 | struct proxy *p = proxy; |
| 8392 | CHTbl *htbl; |
| 8393 | ListElmt *element, *last; |
| 8394 | int i; |
| 8395 | appsess *asession; |
| 8396 | void *data; |
| 8397 | |
| 8398 | while (p) { |
| 8399 | if (p->appsession_name != NULL) { |
| 8400 | htbl = &p->htbl_proxy; |
| 8401 | /* if we ever give up the use of TBLSIZ, we need to change this */ |
| 8402 | for (i = 0; i < TBLSIZ; i++) { |
| 8403 | last = NULL; |
| 8404 | for (element = list_head(&htbl->table[i]); element != NULL; element = list_next(element)) { |
| 8405 | asession = (appsess *)list_data(element); |
| 8406 | if (tv_cmp2_ms(&asession->expire, &now) <= 0) { |
| 8407 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 8408 | int len; |
| 8409 | /* |
| 8410 | on Linux NULL pointers are catched by sprintf, on solaris -> segfault |
| 8411 | */ |
| 8412 | len = sprintf(trash, "appsession_refresh: cleaning up expired Session '%s' on Server %s\n", |
| 8413 | asession->sessid, asession->serverid?asession->serverid:"(null)"); |
| 8414 | write(1, trash, len); |
| 8415 | } |
| 8416 | /* delete the expired element from within the hash table */ |
| 8417 | if ((list_rem_next(&htbl->table[i], last, (void **)&data) == 0) |
| 8418 | && (htbl->table[i].destroy != NULL)) { |
| 8419 | htbl->table[i].destroy(data); |
| 8420 | } |
| 8421 | if (last == NULL) {/* patient lost his head, get a new one */ |
| 8422 | element = list_head(&htbl->table[i]); |
| 8423 | if (element == NULL) break; /* no heads left, go to next patient */ |
| 8424 | } |
| 8425 | else |
| 8426 | element = last; |
| 8427 | }/* end if (tv_cmp2_ms(&asession->expire, &now) <= 0) */ |
| 8428 | else |
| 8429 | last = element; |
| 8430 | }/* end for (element = list_head(&htbl->table[i]); element != NULL; element = list_next(element)) */ |
| 8431 | } |
| 8432 | } |
| 8433 | p = p->next; |
| 8434 | } |
| 8435 | tv_delayfrom(&t->expire, &now, TBLCHKINT); /* check expiration every 5 seconds */ |
| 8436 | return TBLCHKINT; |
| 8437 | } /* end appsession_refresh */ |
| 8438 | |