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 | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 328 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 329 | /* various session flags */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 330 | #define SN_DIRECT 0x00000001 /* connection made on the server matching the client cookie */ |
| 331 | #define SN_CLDENY 0x00000002 /* a client header matches a deny regex */ |
| 332 | #define SN_CLALLOW 0x00000004 /* a client header matches an allow regex */ |
| 333 | #define SN_SVDENY 0x00000008 /* a server header matches a deny regex */ |
| 334 | #define SN_SVALLOW 0x00000010 /* a server header matches an allow regex */ |
| 335 | #define SN_POST 0x00000020 /* the request was an HTTP POST */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 336 | #define SN_MONITOR 0x00000040 /* this session comes from a monitoring system */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 337 | |
| 338 | #define SN_CK_NONE 0x00000000 /* this session had no cookie */ |
| 339 | #define SN_CK_INVALID 0x00000040 /* this session had a cookie which matches no server */ |
| 340 | #define SN_CK_DOWN 0x00000080 /* this session had cookie matching a down server */ |
| 341 | #define SN_CK_VALID 0x000000C0 /* this session had cookie matching a valid server */ |
| 342 | #define SN_CK_MASK 0x000000C0 /* mask to get this session's cookie flags */ |
| 343 | #define SN_CK_SHIFT 6 /* bit shift */ |
| 344 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 345 | #define SN_ERR_NONE 0x00000000 |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 346 | #define SN_ERR_CLITO 0x00000100 /* client time-out */ |
| 347 | #define SN_ERR_CLICL 0x00000200 /* client closed (read/write error) */ |
| 348 | #define SN_ERR_SRVTO 0x00000300 /* server time-out, connect time-out */ |
| 349 | #define SN_ERR_SRVCL 0x00000400 /* server closed (connect/read/write error) */ |
| 350 | #define SN_ERR_PRXCOND 0x00000500 /* the proxy decided to close (deny...) */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 351 | #define SN_ERR_RESOURCE 0x00000600 /* the proxy encountered a lack of a local resources (fd, mem, ...) */ |
| 352 | #define SN_ERR_INTERNAL 0x00000700 /* the proxy encountered an internal error */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 353 | #define SN_ERR_MASK 0x00000700 /* mask to get only session error flags */ |
| 354 | #define SN_ERR_SHIFT 8 /* bit shift */ |
| 355 | |
| 356 | #define SN_FINST_R 0x00001000 /* session ended during client request */ |
| 357 | #define SN_FINST_C 0x00002000 /* session ended during server connect */ |
| 358 | #define SN_FINST_H 0x00003000 /* session ended during server headers */ |
| 359 | #define SN_FINST_D 0x00004000 /* session ended during data phase */ |
| 360 | #define SN_FINST_L 0x00005000 /* session ended while pushing last data to client */ |
| 361 | #define SN_FINST_MASK 0x00007000 /* mask to get only final session state flags */ |
| 362 | #define SN_FINST_SHIFT 12 /* bit shift */ |
| 363 | |
| 364 | #define SN_SCK_NONE 0x00000000 /* no set-cookie seen for the server cookie */ |
| 365 | #define SN_SCK_DELETED 0x00010000 /* existing set-cookie deleted or changed */ |
| 366 | #define SN_SCK_INSERTED 0x00020000 /* new set-cookie inserted or changed existing one */ |
| 367 | #define SN_SCK_SEEN 0x00040000 /* set-cookie seen for the server cookie */ |
| 368 | #define SN_SCK_MASK 0x00070000 /* mask to get the set-cookie field */ |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 369 | #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] | 370 | #define SN_SCK_SHIFT 16 /* bit shift */ |
| 371 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 372 | #define SN_CACHEABLE 0x00100000 /* at least part of the response is cacheable */ |
| 373 | #define SN_CACHE_COOK 0x00200000 /* a cookie in the response is cacheable */ |
| 374 | #define SN_CACHE_SHIFT 20 /* bit shift */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 375 | |
| 376 | /* different possible states for the client side */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 377 | #define CL_STHEADERS 0 |
| 378 | #define CL_STDATA 1 |
| 379 | #define CL_STSHUTR 2 |
| 380 | #define CL_STSHUTW 3 |
| 381 | #define CL_STCLOSE 4 |
| 382 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 383 | /* different possible states for the server side */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 384 | #define SV_STIDLE 0 |
| 385 | #define SV_STCONN 1 |
| 386 | #define SV_STHEADERS 2 |
| 387 | #define SV_STDATA 3 |
| 388 | #define SV_STSHUTR 4 |
| 389 | #define SV_STSHUTW 5 |
| 390 | #define SV_STCLOSE 6 |
| 391 | |
| 392 | /* result of an I/O event */ |
| 393 | #define RES_SILENT 0 /* didn't happen */ |
| 394 | #define RES_DATA 1 /* data were sent or received */ |
| 395 | #define RES_NULL 2 /* result is 0 (read == 0), or connect without need for writing */ |
| 396 | #define RES_ERROR 3 /* result -1 or error on the socket (eg: connect()) */ |
| 397 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 398 | /* modes of operation (global.mode) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 399 | #define MODE_DEBUG 1 |
| 400 | #define MODE_STATS 2 |
| 401 | #define MODE_LOG 4 |
| 402 | #define MODE_DAEMON 8 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 403 | #define MODE_QUIET 16 |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 404 | #define MODE_CHECK 32 |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 405 | #define MODE_VERBOSE 64 |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 406 | #define MODE_STARTING 128 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 407 | |
| 408 | /* server flags */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 409 | #define SRV_RUNNING 1 /* the server is UP */ |
| 410 | #define SRV_BACKUP 2 /* this server is a backup server */ |
| 411 | #define SRV_MAPPORTS 4 /* this server uses mapped ports */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 412 | #define SRV_BIND_SRC 8 /* this server uses a specific source address */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 413 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 414 | /* what to do when a header matches a regex */ |
| 415 | #define ACT_ALLOW 0 /* allow the request */ |
| 416 | #define ACT_REPLACE 1 /* replace the matching header */ |
| 417 | #define ACT_REMOVE 2 /* remove the matching header */ |
| 418 | #define ACT_DENY 3 /* deny the request */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 419 | #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] | 420 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 421 | /* configuration sections */ |
| 422 | #define CFG_NONE 0 |
| 423 | #define CFG_GLOBAL 1 |
| 424 | #define CFG_LISTEN 2 |
| 425 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 426 | /* 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] | 427 | #define LW_DATE 1 /* date */ |
| 428 | #define LW_CLIP 2 /* CLient IP */ |
| 429 | #define LW_SVIP 4 /* SerVer IP */ |
| 430 | #define LW_SVID 8 /* server ID */ |
| 431 | #define LW_REQ 16 /* http REQuest */ |
| 432 | #define LW_RESP 32 /* http RESPonse */ |
| 433 | #define LW_PXIP 64 /* proxy IP */ |
| 434 | #define LW_PXID 128 /* proxy ID */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 435 | #define LW_BYTES 256 /* bytes read from server */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 436 | #define LW_COOKIE 512 /* captured cookie */ |
| 437 | #define LW_REQHDR 1024 /* request header(s) */ |
| 438 | #define LW_RSPHDR 2048 /* response header(s) */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 439 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 440 | /*********************************************************************/ |
| 441 | |
| 442 | #define LIST_HEAD(a) ((void *)(&(a))) |
| 443 | |
| 444 | /*********************************************************************/ |
| 445 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 446 | struct cap_hdr { |
| 447 | struct cap_hdr *next; |
| 448 | char *name; /* header name, case insensitive */ |
| 449 | int namelen; /* length of the header name, to speed-up lookups */ |
| 450 | int len; /* capture length, not including terminal zero */ |
| 451 | int index; /* index in the output array */ |
| 452 | void *pool; /* pool of pre-allocated memory area of (len+1) bytes */ |
| 453 | }; |
| 454 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 455 | struct hdr_exp { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 456 | struct hdr_exp *next; |
| 457 | regex_t *preg; /* expression to look for */ |
| 458 | int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */ |
| 459 | char *replace; /* expression to set instead */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 460 | }; |
| 461 | |
| 462 | struct buffer { |
| 463 | unsigned int l; /* data length */ |
| 464 | 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] | 465 | char *rlim; /* read limit, used for header rewriting */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 466 | unsigned long long total; /* total data read */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 467 | char data[BUFSIZE]; |
| 468 | }; |
| 469 | |
| 470 | struct server { |
| 471 | struct server *next; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 472 | int state; /* server state (SRV_*) */ |
| 473 | int cklen; /* the len of the cookie, to speed up checks */ |
| 474 | char *cookie; /* the id set in the cookie */ |
| 475 | char *id; /* just for identification */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 476 | struct sockaddr_in addr; /* the address to connect to */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 477 | 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] | 478 | short check_port; /* the port to use for the health checks */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 479 | int health; /* 0->rise-1 = bad; rise->rise+fall-1 = good */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 480 | int rise, fall; /* time in iterations */ |
| 481 | int inter; /* time in milliseconds */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 482 | int result; /* 0 = connect OK, -1 = connect KO */ |
| 483 | 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] | 484 | struct proxy *proxy; /* the proxy this server belongs to */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 485 | }; |
| 486 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 487 | /* The base for all tasks */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 488 | struct task { |
| 489 | struct task *next, *prev; /* chaining ... */ |
| 490 | struct task *rqnext; /* chaining in run queue ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 491 | struct task *wq; /* the wait queue this task is in */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 492 | int state; /* task state : IDLE or RUNNING */ |
| 493 | 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] | 494 | int (*process)(struct task *t); /* the function which processes the task */ |
| 495 | void *context; /* the task's context */ |
| 496 | }; |
| 497 | |
| 498 | /* WARNING: if new fields are added, they must be initialized in event_accept() */ |
| 499 | struct session { |
| 500 | struct task *task; /* the task associated with this session */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 501 | /* application specific below */ |
| 502 | struct timeval crexpire; /* expiration date for a client read */ |
| 503 | struct timeval cwexpire; /* expiration date for a client write */ |
| 504 | struct timeval srexpire; /* expiration date for a server read */ |
| 505 | struct timeval swexpire; /* expiration date for a server write */ |
| 506 | struct timeval cnexpire; /* expiration date for a connect */ |
| 507 | char res_cr, res_cw, res_sr, res_sw;/* results of some events */ |
| 508 | struct proxy *proxy; /* the proxy this socket belongs to */ |
| 509 | int cli_fd; /* the client side fd */ |
| 510 | int srv_fd; /* the server side fd */ |
| 511 | int cli_state; /* state of the client side */ |
| 512 | int srv_state; /* state of the server side */ |
| 513 | int conn_retries; /* number of connect retries left */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 514 | int flags; /* some flags describing the session */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 515 | struct buffer *req; /* request buffer */ |
| 516 | struct buffer *rep; /* response buffer */ |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 517 | struct sockaddr_storage cli_addr; /* the client address */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 518 | struct sockaddr_in srv_addr; /* the address to connect to */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 519 | struct server *srv; /* the server being used */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 520 | char **req_cap; /* array of captured request headers (may be NULL) */ |
| 521 | char **rsp_cap; /* array of captured response headers (may be NULL) */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 522 | struct { |
| 523 | int logwait; /* log fields waiting to be collected : LW_* */ |
| 524 | struct timeval tv_accept; /* date of the accept() (beginning of the session) */ |
| 525 | long t_request; /* delay before the end of the request arrives, -1 if never occurs */ |
| 526 | long t_connect; /* delay before the connect() to the server succeeds, -1 if never occurs */ |
| 527 | long t_data; /* delay before the first data byte from the server ... */ |
| 528 | unsigned long t_close; /* total session duration */ |
| 529 | char *uri; /* first line if log needed, NULL otherwise */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 530 | char *cli_cookie; /* cookie presented by the client, in capture mode */ |
| 531 | char *srv_cookie; /* cookie presented by the server, in capture mode */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 532 | int status; /* HTTP status from the server, negative if from proxy */ |
| 533 | long long bytes; /* number of bytes transferred from the server */ |
| 534 | } logs; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 535 | unsigned int uniq_id; /* unique ID used for the traces */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 536 | }; |
| 537 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 538 | struct listener { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 539 | int fd; /* the listen socket */ |
| 540 | struct sockaddr_storage addr; /* the address we listen to */ |
| 541 | struct listener *next; /* next address or NULL */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 542 | }; |
| 543 | |
| 544 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 545 | struct proxy { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 546 | struct listener *listen; /* the listen addresses and sockets */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 547 | 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] | 548 | int state; /* proxy state */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 549 | struct sockaddr_in dispatch_addr; /* the default address to connect to */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 550 | struct server *srv, *cursrv; /* known servers, current server */ |
| 551 | int nbservers; /* # of servers */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 552 | char *cookie_name; /* name of the cookie to look for */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 553 | int cookie_len; /* strlen(cookie_name), computed only once */ |
| 554 | char *appsession_name; /* name of the cookie to look for */ |
| 555 | int appsession_name_len; /* strlen(appsession_name), computed only once */ |
| 556 | int appsession_len; /* length of the appsession cookie value to be used */ |
| 557 | int appsession_timeout; |
| 558 | CHTbl htbl_proxy; /* Per Proxy hashtable */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 559 | char *capture_name; /* beginning of the name of the cookie to capture */ |
| 560 | int capture_namelen; /* length of the cookie name to match */ |
| 561 | int capture_len; /* length of the string to be captured */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 562 | int clitimeout; /* client I/O timeout (in milliseconds) */ |
| 563 | int srvtimeout; /* server I/O timeout (in milliseconds) */ |
| 564 | int contimeout; /* connect timeout (in milliseconds) */ |
| 565 | char *id; /* proxy id */ |
| 566 | int nbconn; /* # of active sessions */ |
| 567 | int maxconn; /* max # of active sessions */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 568 | int conn_retries; /* maximum number of connect retries */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 569 | int options; /* PR_O_REDISP, PR_O_TRANSP, ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 570 | 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] | 571 | 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] | 572 | struct proxy *next; |
| 573 | struct sockaddr_in logsrv1, logsrv2; /* 2 syslog servers */ |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 574 | signed char logfac1, logfac2; /* log facility for both servers. -1 = disabled */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 575 | int loglev1, loglev2; /* log level for each server, 7 by default */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 576 | int to_log; /* things to be logged (LW_*) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 577 | struct timeval stop_time; /* date to stop listening, when stopping != 0 */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 578 | int nb_reqadd, nb_rspadd; |
| 579 | struct hdr_exp *req_exp; /* regular expressions for request headers */ |
| 580 | struct hdr_exp *rsp_exp; /* regular expressions for response headers */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 581 | int nb_req_cap, nb_rsp_cap; /* # of headers to be captured */ |
| 582 | struct cap_hdr *req_cap; /* chained list of request headers to be captured */ |
| 583 | struct cap_hdr *rsp_cap; /* chained list of response headers to be captured */ |
| 584 | 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] | 585 | 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] | 586 | int grace; /* grace time after stop request */ |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 587 | char *check_req; /* HTTP request to use if PR_O_HTTP_CHK is set, else NULL */ |
| 588 | int check_len; /* Length of the HTTP request */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 589 | struct { |
| 590 | char *msg400; /* message for error 400 */ |
| 591 | int len400; /* message length for error 400 */ |
| 592 | char *msg403; /* message for error 403 */ |
| 593 | int len403; /* message length for error 403 */ |
| 594 | char *msg408; /* message for error 408 */ |
| 595 | int len408; /* message length for error 408 */ |
| 596 | char *msg500; /* message for error 500 */ |
| 597 | int len500; /* message length for error 500 */ |
| 598 | char *msg502; /* message for error 502 */ |
| 599 | int len502; /* message length for error 502 */ |
| 600 | char *msg503; /* message for error 503 */ |
| 601 | int len503; /* message length for error 503 */ |
| 602 | char *msg504; /* message for error 504 */ |
| 603 | int len504; /* message length for error 504 */ |
| 604 | } errmsg; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 605 | }; |
| 606 | |
| 607 | /* info about one given fd */ |
| 608 | struct fdtab { |
| 609 | int (*read)(int fd); /* read function */ |
| 610 | int (*write)(int fd); /* write function */ |
| 611 | struct task *owner; /* the session (or proxy) associated with this fd */ |
| 612 | int state; /* the state of this fd */ |
| 613 | }; |
| 614 | |
| 615 | /*********************************************************************/ |
| 616 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 617 | int cfg_maxpconn = DEFAULT_MAXCONN; /* # of simultaneous connections per proxy (-N) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 618 | char *cfg_cfgfile = NULL; /* configuration file */ |
| 619 | char *progname = NULL; /* program name */ |
| 620 | int pid; /* current process id */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 621 | |
| 622 | /* global options */ |
| 623 | static struct { |
| 624 | int uid; |
| 625 | int gid; |
| 626 | int nbproc; |
| 627 | int maxconn; |
| 628 | int maxsock; /* max # of sockets */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 629 | int rlimit_nofile; /* default ulimit-n value : 0=unset */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 630 | int mode; |
| 631 | char *chroot; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 632 | char *pidfile; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 633 | int logfac1, logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 634 | int loglev1, loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 635 | struct sockaddr_in logsrv1, logsrv2; |
| 636 | } global = { |
| 637 | logfac1 : -1, |
| 638 | logfac2 : -1, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 639 | loglev1 : 7, /* max syslog level : debug */ |
| 640 | loglev2 : 7, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 641 | /* others NULL OK */ |
| 642 | }; |
| 643 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 644 | /*********************************************************************/ |
| 645 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 646 | fd_set *StaticReadEvent, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 647 | *StaticWriteEvent; |
| 648 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 649 | int cfg_polling_mechanism = 0; /* POLL_USE_{SELECT|POLL|EPOLL} */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 650 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 651 | void **pool_session = NULL, |
| 652 | **pool_buffer = NULL, |
| 653 | **pool_fdtab = NULL, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 654 | **pool_requri = NULL, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 655 | **pool_task = NULL, |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 656 | **pool_capture = NULL, |
| 657 | **pool_appsess = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 658 | |
| 659 | struct proxy *proxy = NULL; /* list of all existing proxies */ |
| 660 | struct fdtab *fdtab = NULL; /* array of all the file descriptors */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 661 | struct task *rq = NULL; /* global run queue */ |
| 662 | struct task wait_queue = { /* global wait queue */ |
| 663 | prev:LIST_HEAD(wait_queue), |
| 664 | next:LIST_HEAD(wait_queue) |
| 665 | }; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 666 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 667 | static int totalconn = 0; /* total # of terminated sessions */ |
| 668 | static int actconn = 0; /* # of active sessions */ |
| 669 | static int maxfd = 0; /* # of the highest fd + 1 */ |
| 670 | static int listeners = 0; /* # of listeners */ |
| 671 | static int stopping = 0; /* non zero means stopping in progress */ |
| 672 | static struct timeval now = {0,0}; /* the current date at any moment */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 673 | 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] | 674 | |
willy tarreau | 08dedbe | 2005-12-18 01:13:48 +0100 | [diff] [blame] | 675 | #if defined(ENABLE_EPOLL) |
| 676 | /* FIXME: this is dirty, but at the moment, there's no other solution to remove |
| 677 | * the old FDs from outside the loop. Perhaps we should export a global 'poll' |
| 678 | * structure with pointers to functions such as init_fd() and close_fd(), plus |
| 679 | * a private structure with several pointers to places such as below. |
| 680 | */ |
| 681 | |
| 682 | static fd_set *PrevReadEvent = NULL, *PrevWriteEvent = NULL; |
| 683 | #endif |
| 684 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 685 | 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] | 686 | /* 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] | 687 | static char trash[BUFSIZE]; |
| 688 | |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 689 | const int zero = 0; |
| 690 | const int one = 1; |
| 691 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 692 | /* |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 693 | * Syslog facilities and levels. Conforming to RFC3164. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 694 | */ |
| 695 | |
| 696 | #define MAX_SYSLOG_LEN 1024 |
| 697 | #define NB_LOG_FACILITIES 24 |
| 698 | const char *log_facilities[NB_LOG_FACILITIES] = { |
| 699 | "kern", "user", "mail", "daemon", |
| 700 | "auth", "syslog", "lpr", "news", |
| 701 | "uucp", "cron", "auth2", "ftp", |
| 702 | "ntp", "audit", "alert", "cron2", |
| 703 | "local0", "local1", "local2", "local3", |
| 704 | "local4", "local5", "local6", "local7" |
| 705 | }; |
| 706 | |
| 707 | |
| 708 | #define NB_LOG_LEVELS 8 |
| 709 | const char *log_levels[NB_LOG_LEVELS] = { |
| 710 | "emerg", "alert", "crit", "err", |
| 711 | "warning", "notice", "info", "debug" |
| 712 | }; |
| 713 | |
| 714 | #define SYSLOG_PORT 514 |
| 715 | |
| 716 | const char *monthname[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 717 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 718 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 719 | 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] | 720 | const char sess_fin_state[8] = "-RCHDL67"; /* cliRequest, srvConnect, srvHeader, Data, Last, unknown */ |
| 721 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 722 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 723 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 724 | unknown, Set-cookie Rewritten */ |
| 725 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 726 | #define MAX_HOSTNAME_LEN 32 |
| 727 | static char hostname[MAX_HOSTNAME_LEN] = ""; |
| 728 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 729 | const char *HTTP_302 = |
| 730 | "HTTP/1.0 302 Found\r\n" |
| 731 | "Cache-Control: no-cache\r\n" |
| 732 | "Connection: close\r\n" |
| 733 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 734 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 735 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 736 | const char *HTTP_303 = |
| 737 | "HTTP/1.0 303 See Other\r\n" |
| 738 | "Cache-Control: no-cache\r\n" |
| 739 | "Connection: close\r\n" |
| 740 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 741 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 742 | const char *HTTP_400 = |
| 743 | "HTTP/1.0 400 Bad request\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 744 | "Cache-Control: no-cache\r\n" |
| 745 | "Connection: close\r\n" |
| 746 | "\r\n" |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 747 | "<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] | 748 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 749 | const char *HTTP_403 = |
| 750 | "HTTP/1.0 403 Forbidden\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 751 | "Cache-Control: no-cache\r\n" |
| 752 | "Connection: close\r\n" |
| 753 | "\r\n" |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 754 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n"; |
| 755 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 756 | const char *HTTP_408 = |
| 757 | "HTTP/1.0 408 Request Time-out\r\n" |
| 758 | "Cache-Control: no-cache\r\n" |
| 759 | "Connection: close\r\n" |
| 760 | "\r\n" |
| 761 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n"; |
| 762 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 763 | const char *HTTP_500 = |
| 764 | "HTTP/1.0 500 Server Error\r\n" |
| 765 | "Cache-Control: no-cache\r\n" |
| 766 | "Connection: close\r\n" |
| 767 | "\r\n" |
| 768 | "<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] | 769 | |
| 770 | const char *HTTP_502 = |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 771 | "HTTP/1.0 502 Bad Gateway\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 772 | "Cache-Control: no-cache\r\n" |
| 773 | "Connection: close\r\n" |
| 774 | "\r\n" |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 775 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n"; |
| 776 | |
| 777 | const char *HTTP_503 = |
| 778 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 779 | "Cache-Control: no-cache\r\n" |
| 780 | "Connection: close\r\n" |
| 781 | "\r\n" |
| 782 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n"; |
| 783 | |
| 784 | const char *HTTP_504 = |
| 785 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 786 | "Cache-Control: no-cache\r\n" |
| 787 | "Connection: close\r\n" |
| 788 | "\r\n" |
| 789 | "<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] | 790 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 791 | /*********************************************************************/ |
| 792 | /* statistics ******************************************************/ |
| 793 | /*********************************************************************/ |
| 794 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 795 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 796 | static int stats_tsk_lsrch, stats_tsk_rsrch, |
| 797 | stats_tsk_good, stats_tsk_right, stats_tsk_left, |
| 798 | stats_tsk_new, stats_tsk_nsrch; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 799 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 800 | |
| 801 | |
| 802 | /*********************************************************************/ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 803 | /* debugging *******************************************************/ |
| 804 | /*********************************************************************/ |
| 805 | #ifdef DEBUG_FULL |
| 806 | static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 807 | static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 808 | #endif |
| 809 | |
| 810 | /*********************************************************************/ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 811 | /* function prototypes *********************************************/ |
| 812 | /*********************************************************************/ |
| 813 | |
| 814 | int event_accept(int fd); |
| 815 | int event_cli_read(int fd); |
| 816 | int event_cli_write(int fd); |
| 817 | int event_srv_read(int fd); |
| 818 | int event_srv_write(int fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 819 | int process_session(struct task *t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 820 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 821 | static int appsession_task_init(void); |
| 822 | static int appsession_init(void); |
| 823 | static int appsession_refresh(struct task *t); |
| 824 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 825 | /*********************************************************************/ |
| 826 | /* general purpose functions ***************************************/ |
| 827 | /*********************************************************************/ |
| 828 | |
| 829 | void display_version() { |
| 830 | printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n"); |
willy tarreau | 726618c | 2006-01-29 22:42:06 +0100 | [diff] [blame] | 831 | printf("Copyright 2000-2006 Willy Tarreau <w@w.ods.org>\n\n"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | /* |
| 835 | * This function prints the command line usage and exits |
| 836 | */ |
| 837 | void usage(char *name) { |
| 838 | display_version(); |
| 839 | fprintf(stderr, |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 840 | "Usage : %s -f <cfgfile> [ -vdV" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 841 | #if STATTIME > 0 |
| 842 | "sl" |
| 843 | #endif |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 844 | "D ] [ -n <maxconn> ] [ -N <maxpconn> ] [ -p <pidfile> ]\n" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 845 | " -v displays version\n" |
| 846 | " -d enters debug mode\n" |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 847 | " -V enters verbose mode (disables quiet mode)\n" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 848 | #if STATTIME > 0 |
| 849 | " -s enables statistics output\n" |
| 850 | " -l enables long statistics format\n" |
| 851 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 852 | " -D goes daemon ; implies -q\n" |
| 853 | " -q quiet mode : don't display messages\n" |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 854 | " -c check mode : only check config file and exit\n" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 855 | " -n sets the maximum total # of connections (%d)\n" |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 856 | " -N sets the default, per-proxy maximum # of connections (%d)\n" |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 857 | " -p writes pids of all children to this file\n" |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 858 | #if defined(ENABLE_EPOLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 859 | " -de disables epoll() usage even when available\n" |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 860 | #endif |
| 861 | #if defined(ENABLE_POLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 862 | " -dp disables poll() usage even when available\n" |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 863 | #endif |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 864 | "\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 865 | name, DEFAULT_MAXCONN, cfg_maxpconn); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 866 | exit(1); |
| 867 | } |
| 868 | |
| 869 | |
| 870 | /* |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 871 | * Displays the message on stderr with the date and pid. Overrides the quiet |
| 872 | * mode during startup. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 873 | */ |
| 874 | void Alert(char *fmt, ...) { |
| 875 | va_list argp; |
| 876 | struct timeval tv; |
| 877 | struct tm *tm; |
| 878 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 879 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 880 | va_start(argp, fmt); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 881 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 882 | gettimeofday(&tv, NULL); |
| 883 | tm=localtime(&tv.tv_sec); |
| 884 | fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ", |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 885 | 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] | 886 | vfprintf(stderr, fmt, argp); |
| 887 | fflush(stderr); |
| 888 | va_end(argp); |
| 889 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | |
| 893 | /* |
| 894 | * Displays the message on stderr with the date and pid. |
| 895 | */ |
| 896 | void Warning(char *fmt, ...) { |
| 897 | va_list argp; |
| 898 | struct timeval tv; |
| 899 | struct tm *tm; |
| 900 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 901 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 902 | va_start(argp, fmt); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 903 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 904 | gettimeofday(&tv, NULL); |
| 905 | tm=localtime(&tv.tv_sec); |
| 906 | fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ", |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 907 | 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] | 908 | vfprintf(stderr, fmt, argp); |
| 909 | fflush(stderr); |
| 910 | va_end(argp); |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | /* |
| 915 | * Displays the message on <out> only if quiet mode is not set. |
| 916 | */ |
| 917 | void qfprintf(FILE *out, char *fmt, ...) { |
| 918 | va_list argp; |
| 919 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 920 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 921 | va_start(argp, fmt); |
| 922 | vfprintf(out, fmt, argp); |
| 923 | fflush(out); |
| 924 | va_end(argp); |
| 925 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | |
| 929 | /* |
| 930 | * converts <str> to a struct sockaddr_in* which is locally allocated. |
| 931 | * The format is "addr:port", where "addr" can be empty or "*" to indicate |
| 932 | * INADDR_ANY. |
| 933 | */ |
| 934 | struct sockaddr_in *str2sa(char *str) { |
| 935 | static struct sockaddr_in sa; |
| 936 | char *c; |
| 937 | int port; |
| 938 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 939 | memset(&sa, 0, sizeof(sa)); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 940 | str=strdup(str); |
| 941 | |
| 942 | if ((c=strrchr(str,':')) != NULL) { |
| 943 | *c++=0; |
| 944 | port=atol(c); |
| 945 | } |
| 946 | else |
| 947 | port=0; |
| 948 | |
| 949 | if (*str == '*' || *str == '\0') { /* INADDR_ANY */ |
| 950 | sa.sin_addr.s_addr = INADDR_ANY; |
| 951 | } |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 952 | else if (!inet_pton(AF_INET, str, &sa.sin_addr)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 953 | struct hostent *he; |
| 954 | |
| 955 | if ((he = gethostbyname(str)) == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 956 | Alert("Invalid server name: '%s'\n", str); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 957 | } |
| 958 | else |
| 959 | sa.sin_addr = *(struct in_addr *) *(he->h_addr_list); |
| 960 | } |
| 961 | sa.sin_port=htons(port); |
| 962 | sa.sin_family=AF_INET; |
| 963 | |
| 964 | free(str); |
| 965 | return &sa; |
| 966 | } |
| 967 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 968 | /* |
| 969 | * converts <str> to a two struct in_addr* which are locally allocated. |
| 970 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
| 971 | * is optionnal and either in the dotted or CIDR notation. |
| 972 | * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error. |
| 973 | */ |
| 974 | int str2net(char *str, struct in_addr *addr, struct in_addr *mask) { |
| 975 | char *c; |
| 976 | unsigned long len; |
| 977 | |
| 978 | memset(mask, 0, sizeof(*mask)); |
| 979 | memset(addr, 0, sizeof(*addr)); |
| 980 | str=strdup(str); |
| 981 | |
| 982 | if ((c = strrchr(str, '/')) != NULL) { |
| 983 | *c++ = 0; |
| 984 | /* c points to the mask */ |
| 985 | if (strchr(c, '.') != NULL) { /* dotted notation */ |
| 986 | if (!inet_pton(AF_INET, c, mask)) |
| 987 | return 0; |
| 988 | } |
| 989 | else { /* mask length */ |
| 990 | char *err; |
| 991 | len = strtol(c, &err, 10); |
| 992 | if (!*c || (err && *err) || (unsigned)len > 32) |
| 993 | return 0; |
| 994 | if (len) |
| 995 | mask->s_addr = htonl(0xFFFFFFFFUL << (32 - len)); |
| 996 | else |
| 997 | mask->s_addr = 0; |
| 998 | } |
| 999 | } |
| 1000 | else { |
| 1001 | mask->s_addr = 0xFFFFFFFF; |
| 1002 | } |
| 1003 | if (!inet_pton(AF_INET, str, addr)) { |
| 1004 | struct hostent *he; |
| 1005 | |
| 1006 | if ((he = gethostbyname(str)) == NULL) { |
| 1007 | return 0; |
| 1008 | } |
| 1009 | else |
| 1010 | *addr = *(struct in_addr *) *(he->h_addr_list); |
| 1011 | } |
| 1012 | free(str); |
| 1013 | return 1; |
| 1014 | } |
| 1015 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1016 | |
| 1017 | /* |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1018 | * converts <str> to a list of listeners which are dynamically allocated. |
| 1019 | * The format is "{addr|'*'}:port[-end][,{addr|'*'}:port[-end]]*", where : |
| 1020 | * - <addr> can be empty or "*" to indicate INADDR_ANY ; |
| 1021 | * - <port> is a numerical port from 1 to 65535 ; |
| 1022 | * - <end> indicates to use the range from <port> to <end> instead (inclusive). |
| 1023 | * This can be repeated as many times as necessary, separated by a coma. |
| 1024 | * The <tail> argument is a pointer to a current list which should be appended |
| 1025 | * to the tail of the new list. The pointer to the new list is returned. |
| 1026 | */ |
| 1027 | struct listener *str2listener(char *str, struct listener *tail) { |
| 1028 | struct listener *l; |
| 1029 | char *c, *next, *range, *dupstr; |
| 1030 | int port, end; |
| 1031 | |
| 1032 | next = dupstr = strdup(str); |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1033 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1034 | while (next && *next) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1035 | struct sockaddr_storage ss; |
| 1036 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1037 | str = next; |
| 1038 | /* 1) look for the end of the first address */ |
| 1039 | if ((next = strrchr(str, ',')) != NULL) { |
| 1040 | *next++ = 0; |
| 1041 | } |
| 1042 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1043 | /* 2) look for the addr/port delimiter, it's the last colon. */ |
| 1044 | if ((range = strrchr(str, ':')) == NULL) { |
| 1045 | Alert("Missing port number: '%s'\n", str); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1046 | goto fail; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | *range++ = 0; |
| 1050 | |
| 1051 | if (strrchr(str, ':') != NULL) { |
| 1052 | /* IPv6 address contains ':' */ |
| 1053 | memset(&ss, 0, sizeof(ss)); |
| 1054 | ss.ss_family = AF_INET6; |
| 1055 | |
| 1056 | if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in6 *)&ss)->sin6_addr)) { |
| 1057 | Alert("Invalid server address: '%s'\n", str); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1058 | goto fail; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1059 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1060 | } |
| 1061 | else { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1062 | memset(&ss, 0, sizeof(ss)); |
| 1063 | ss.ss_family = AF_INET; |
| 1064 | |
| 1065 | if (*str == '*' || *str == '\0') { /* INADDR_ANY */ |
| 1066 | ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; |
| 1067 | } |
| 1068 | else if (!inet_pton(ss.ss_family, str, &((struct sockaddr_in *)&ss)->sin_addr)) { |
| 1069 | struct hostent *he; |
| 1070 | |
| 1071 | if ((he = gethostbyname(str)) == NULL) { |
| 1072 | Alert("Invalid server name: '%s'\n", str); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1073 | goto fail; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1074 | } |
| 1075 | else |
| 1076 | ((struct sockaddr_in *)&ss)->sin_addr = |
| 1077 | *(struct in_addr *) *(he->h_addr_list); |
| 1078 | } |
| 1079 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1080 | |
| 1081 | /* 3) look for the port-end delimiter */ |
| 1082 | if ((c = strchr(range, '-')) != NULL) { |
| 1083 | *c++ = 0; |
| 1084 | end = atol(c); |
| 1085 | } |
| 1086 | else { |
| 1087 | end = atol(range); |
| 1088 | } |
| 1089 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1090 | port = atol(range); |
| 1091 | |
| 1092 | if (port < 1 || port > 65535) { |
| 1093 | Alert("Invalid port '%d' specified for address '%s'.\n", port, str); |
| 1094 | goto fail; |
| 1095 | } |
| 1096 | |
| 1097 | if (end < 1 || end > 65535) { |
| 1098 | Alert("Invalid port '%d' specified for address '%s'.\n", end, str); |
| 1099 | goto fail; |
| 1100 | } |
| 1101 | |
| 1102 | for (; port <= end; port++) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1103 | l = (struct listener *)calloc(1, sizeof(struct listener)); |
| 1104 | l->next = tail; |
| 1105 | tail = l; |
| 1106 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 1107 | l->addr = ss; |
| 1108 | if (ss.ss_family == AF_INET6) |
| 1109 | ((struct sockaddr_in6 *)(&l->addr))->sin6_port = htons(port); |
| 1110 | else |
| 1111 | ((struct sockaddr_in *)(&l->addr))->sin_port = htons(port); |
| 1112 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1113 | } /* end for(port) */ |
| 1114 | } /* end while(next) */ |
| 1115 | free(dupstr); |
| 1116 | return tail; |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 1117 | fail: |
| 1118 | free(dupstr); |
| 1119 | return NULL; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1120 | } |
| 1121 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1122 | |
| 1123 | #define FD_SETS_ARE_BITFIELDS |
| 1124 | #ifdef FD_SETS_ARE_BITFIELDS |
| 1125 | /* |
| 1126 | * This map is used with all the FD_* macros to check whether a particular bit |
| 1127 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 1128 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 1129 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 1130 | * exclusively to the macros. |
| 1131 | */ |
| 1132 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 1133 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 1134 | |
| 1135 | #else |
| 1136 | #error "Check if your OS uses bitfields for fd_sets" |
| 1137 | #endif |
| 1138 | |
| 1139 | /* will try to encode the string <string> replacing all characters tagged in |
| 1140 | * <map> with the hexadecimal representation of their ASCII-code (2 digits) |
| 1141 | * prefixed by <escape>, and will store the result between <start> (included |
| 1142 | *) and <stop> (excluded), and will always terminate the string with a '\0' |
| 1143 | * before <stop>. The position of the '\0' is returned if the conversion |
| 1144 | * completes. If bytes are missing between <start> and <stop>, then the |
| 1145 | * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0' |
| 1146 | * cannot even be stored so we return <start> without writing the 0. |
| 1147 | * The input string must also be zero-terminated. |
| 1148 | */ |
| 1149 | char hextab[16] = "0123456789ABCDEF"; |
| 1150 | char *encode_string(char *start, char *stop, |
| 1151 | const char escape, const fd_set *map, |
| 1152 | const char *string) |
| 1153 | { |
| 1154 | if (start < stop) { |
| 1155 | stop--; /* reserve one byte for the final '\0' */ |
| 1156 | while (start < stop && *string != 0) { |
| 1157 | if (!FD_ISSET((unsigned char)(*string), map)) |
| 1158 | *start++ = *string; |
| 1159 | else { |
| 1160 | if (start + 3 >= stop) |
| 1161 | break; |
| 1162 | *start++ = escape; |
| 1163 | *start++ = hextab[(*string >> 4) & 15]; |
| 1164 | *start++ = hextab[*string & 15]; |
| 1165 | } |
| 1166 | string++; |
| 1167 | } |
| 1168 | *start = '\0'; |
| 1169 | } |
| 1170 | return start; |
| 1171 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1172 | |
| 1173 | /* |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1174 | * This function sends a syslog message to both log servers of a proxy, |
| 1175 | * or to global log servers if the proxy is NULL. |
| 1176 | * It also tries not to waste too much time computing the message header. |
| 1177 | * It doesn't care about errors nor does it report them. |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1178 | */ |
| 1179 | void send_log(struct proxy *p, int level, char *message, ...) { |
| 1180 | static int logfd = -1; /* syslog UDP socket */ |
| 1181 | static long tvsec = -1; /* to force the string to be initialized */ |
| 1182 | struct timeval tv; |
| 1183 | va_list argp; |
| 1184 | static char logmsg[MAX_SYSLOG_LEN]; |
| 1185 | static char *dataptr = NULL; |
| 1186 | int fac_level; |
| 1187 | int hdr_len, data_len; |
| 1188 | struct sockaddr_in *sa[2]; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1189 | int facilities[2], loglevel[2]; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1190 | int nbloggers = 0; |
| 1191 | char *log_ptr; |
| 1192 | |
| 1193 | if (logfd < 0) { |
| 1194 | if ((logfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) |
| 1195 | return; |
| 1196 | } |
| 1197 | |
| 1198 | if (level < 0 || progname == NULL || message == NULL) |
| 1199 | return; |
| 1200 | |
| 1201 | gettimeofday(&tv, NULL); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1202 | if (tv.tv_sec != tvsec || dataptr == NULL) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1203 | /* this string is rebuild only once a second */ |
| 1204 | struct tm *tm = localtime(&tv.tv_sec); |
| 1205 | tvsec = tv.tv_sec; |
| 1206 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1207 | hdr_len = snprintf(logmsg, sizeof(logmsg), |
| 1208 | "<<<<>%s %2d %02d:%02d:%02d %s[%d]: ", |
| 1209 | monthname[tm->tm_mon], |
| 1210 | tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 1211 | progname, pid); |
| 1212 | /* WARNING: depending upon implementations, snprintf may return |
| 1213 | * either -1 or the number of bytes that would be needed to store |
| 1214 | * the total message. In both cases, we must adjust it. |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1215 | */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1216 | if (hdr_len < 0 || hdr_len > sizeof(logmsg)) |
| 1217 | hdr_len = sizeof(logmsg); |
| 1218 | |
| 1219 | dataptr = logmsg + hdr_len; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | va_start(argp, message); |
| 1223 | data_len = vsnprintf(dataptr, logmsg + sizeof(logmsg) - dataptr, message, argp); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1224 | if (data_len < 0 || data_len > (logmsg + sizeof(logmsg) - dataptr)) |
| 1225 | data_len = logmsg + sizeof(logmsg) - dataptr; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1226 | va_end(argp); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1227 | dataptr[data_len - 1] = '\n'; /* force a break on ultra-long lines */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1228 | |
| 1229 | if (p == NULL) { |
| 1230 | if (global.logfac1 >= 0) { |
| 1231 | sa[nbloggers] = &global.logsrv1; |
| 1232 | facilities[nbloggers] = global.logfac1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1233 | loglevel[nbloggers] = global.loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1234 | nbloggers++; |
| 1235 | } |
| 1236 | if (global.logfac2 >= 0) { |
| 1237 | sa[nbloggers] = &global.logsrv2; |
| 1238 | facilities[nbloggers] = global.logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1239 | loglevel[nbloggers] = global.loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1240 | nbloggers++; |
| 1241 | } |
| 1242 | } else { |
| 1243 | if (p->logfac1 >= 0) { |
| 1244 | sa[nbloggers] = &p->logsrv1; |
| 1245 | facilities[nbloggers] = p->logfac1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1246 | loglevel[nbloggers] = p->loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1247 | nbloggers++; |
| 1248 | } |
| 1249 | if (p->logfac2 >= 0) { |
| 1250 | sa[nbloggers] = &p->logsrv2; |
| 1251 | facilities[nbloggers] = p->logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1252 | loglevel[nbloggers] = p->loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1253 | nbloggers++; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | while (nbloggers-- > 0) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1258 | /* we can filter the level of the messages that are sent to each logger */ |
| 1259 | if (level > loglevel[nbloggers]) |
| 1260 | continue; |
| 1261 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1262 | /* For each target, we may have a different facility. |
| 1263 | * We can also have a different log level for each message. |
| 1264 | * This induces variations in the message header length. |
| 1265 | * Since we don't want to recompute it each time, nor copy it every |
| 1266 | * time, we only change the facility in the pre-computed header, |
| 1267 | * and we change the pointer to the header accordingly. |
| 1268 | */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1269 | fac_level = (facilities[nbloggers] << 3) + level; |
| 1270 | log_ptr = logmsg + 3; /* last digit of the log level */ |
| 1271 | do { |
| 1272 | *log_ptr = '0' + fac_level % 10; |
| 1273 | fac_level /= 10; |
| 1274 | log_ptr--; |
| 1275 | } while (fac_level && log_ptr > logmsg); |
| 1276 | *log_ptr = '<'; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1277 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1278 | /* 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] | 1279 | |
| 1280 | #ifndef MSG_NOSIGNAL |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1281 | sendto(logfd, log_ptr, dataptr + data_len - log_ptr, MSG_DONTWAIT, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1282 | (struct sockaddr *)sa[nbloggers], sizeof(**sa)); |
| 1283 | #else |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 1284 | 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] | 1285 | (struct sockaddr *)sa[nbloggers], sizeof(**sa)); |
| 1286 | #endif |
| 1287 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | |
| 1291 | /* sets <tv> to the current time */ |
| 1292 | static inline struct timeval *tv_now(struct timeval *tv) { |
| 1293 | if (tv) |
| 1294 | gettimeofday(tv, NULL); |
| 1295 | return tv; |
| 1296 | } |
| 1297 | |
| 1298 | /* |
| 1299 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 1300 | */ |
| 1301 | static inline struct timeval *tv_delayfrom(struct timeval *tv, struct timeval *from, int ms) { |
| 1302 | if (!tv || !from) |
| 1303 | return NULL; |
| 1304 | tv->tv_usec = from->tv_usec + (ms%1000)*1000; |
| 1305 | tv->tv_sec = from->tv_sec + (ms/1000); |
| 1306 | while (tv->tv_usec >= 1000000) { |
| 1307 | tv->tv_usec -= 1000000; |
| 1308 | tv->tv_sec++; |
| 1309 | } |
| 1310 | return tv; |
| 1311 | } |
| 1312 | |
| 1313 | /* |
| 1314 | * 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] | 1315 | * 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] | 1316 | */ |
| 1317 | static inline int tv_cmp(struct timeval *tv1, struct timeval *tv2) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1318 | if (tv1->tv_sec < tv2->tv_sec) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1319 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1320 | else if (tv1->tv_sec > tv2->tv_sec) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1321 | return 1; |
| 1322 | else if (tv1->tv_usec < tv2->tv_usec) |
| 1323 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1324 | else if (tv1->tv_usec > tv2->tv_usec) |
| 1325 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1326 | else |
| 1327 | return 0; |
| 1328 | } |
| 1329 | |
| 1330 | /* |
| 1331 | * returns the absolute difference, in ms, between tv1 and tv2 |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1332 | * Must not be used when either argument is eternity. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1333 | */ |
| 1334 | unsigned long tv_delta(struct timeval *tv1, struct timeval *tv2) { |
| 1335 | int cmp; |
| 1336 | unsigned long ret; |
| 1337 | |
| 1338 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1339 | cmp = tv_cmp(tv1, tv2); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1340 | if (!cmp) |
| 1341 | return 0; /* same dates, null diff */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1342 | else if (cmp < 0) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1343 | struct timeval *tmp = tv1; |
| 1344 | tv1 = tv2; |
| 1345 | tv2 = tmp; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1346 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1347 | ret = (tv1->tv_sec - tv2->tv_sec) * 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1348 | if (tv1->tv_usec > tv2->tv_usec) |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1349 | ret += (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1350 | else |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1351 | ret -= (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1352 | return (unsigned long) ret; |
| 1353 | } |
| 1354 | |
| 1355 | /* |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1356 | * returns the difference, in ms, between tv1 and tv2 |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1357 | * Must not be used when either argument is eternity. |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1358 | */ |
| 1359 | static inline unsigned long tv_diff(struct timeval *tv1, struct timeval *tv2) { |
| 1360 | unsigned long ret; |
| 1361 | |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 1362 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
| 1363 | if (tv2->tv_usec > tv1->tv_usec) |
| 1364 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1365 | else |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 1366 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1367 | return (unsigned long) ret; |
| 1368 | } |
| 1369 | |
| 1370 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1371 | * 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] | 1372 | * 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] | 1373 | */ |
| 1374 | static inline int tv_cmp_ms(struct timeval *tv1, struct timeval *tv2) { |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1375 | if (tv1->tv_sec == tv2->tv_sec) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1376 | if (tv2->tv_usec > tv1->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1377 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1378 | else if (tv1->tv_usec > tv2->tv_usec + 1000) |
| 1379 | return 1; |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1380 | else |
| 1381 | return 0; |
| 1382 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1383 | else if ((tv2->tv_sec > tv1->tv_sec + 1) || |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1384 | ((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] | 1385 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1386 | else if ((tv1->tv_sec > tv2->tv_sec + 1) || |
| 1387 | ((tv1->tv_sec == tv2->tv_sec + 1) && (tv1->tv_usec + 1000000 > tv2->tv_usec + 1000))) |
| 1388 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1389 | else |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
| 1393 | /* |
| 1394 | * returns the remaining time between tv1=now and event=tv2 |
| 1395 | * if tv2 is passed, 0 is returned. |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1396 | * Must not be used when either argument is eternity. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1397 | */ |
| 1398 | static inline unsigned long tv_remain(struct timeval *tv1, struct timeval *tv2) { |
| 1399 | unsigned long ret; |
| 1400 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1401 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 1402 | return 0; /* event elapsed */ |
| 1403 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1404 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1405 | if (tv2->tv_usec > tv1->tv_usec) |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1406 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1407 | else |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1408 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1409 | return (unsigned long) ret; |
| 1410 | } |
| 1411 | |
| 1412 | |
| 1413 | /* |
| 1414 | * zeroes a struct timeval |
| 1415 | */ |
| 1416 | |
| 1417 | static inline struct timeval *tv_eternity(struct timeval *tv) { |
| 1418 | tv->tv_sec = tv->tv_usec = 0; |
| 1419 | return tv; |
| 1420 | } |
| 1421 | |
| 1422 | /* |
| 1423 | * returns 1 if tv is null, else 0 |
| 1424 | */ |
| 1425 | static inline int tv_iseternity(struct timeval *tv) { |
| 1426 | if (tv->tv_sec == 0 && tv->tv_usec == 0) |
| 1427 | return 1; |
| 1428 | else |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | /* |
| 1433 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 1434 | * considering that 0 is the eternity. |
| 1435 | */ |
| 1436 | static inline int tv_cmp2(struct timeval *tv1, struct timeval *tv2) { |
| 1437 | if (tv_iseternity(tv1)) |
| 1438 | if (tv_iseternity(tv2)) |
| 1439 | return 0; /* same */ |
| 1440 | else |
| 1441 | return 1; /* tv1 later than tv2 */ |
| 1442 | else if (tv_iseternity(tv2)) |
| 1443 | return -1; /* tv2 later than tv1 */ |
| 1444 | |
| 1445 | if (tv1->tv_sec > tv2->tv_sec) |
| 1446 | return 1; |
| 1447 | else if (tv1->tv_sec < tv2->tv_sec) |
| 1448 | return -1; |
| 1449 | else if (tv1->tv_usec > tv2->tv_usec) |
| 1450 | return 1; |
| 1451 | else if (tv1->tv_usec < tv2->tv_usec) |
| 1452 | return -1; |
| 1453 | else |
| 1454 | return 0; |
| 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 1459 | * considering that 0 is the eternity. |
| 1460 | */ |
| 1461 | static inline int tv_cmp2_ms(struct timeval *tv1, struct timeval *tv2) { |
| 1462 | if (tv_iseternity(tv1)) |
| 1463 | if (tv_iseternity(tv2)) |
| 1464 | return 0; /* same */ |
| 1465 | else |
| 1466 | return 1; /* tv1 later than tv2 */ |
| 1467 | else if (tv_iseternity(tv2)) |
| 1468 | return -1; /* tv2 later than tv1 */ |
| 1469 | |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1470 | if (tv1->tv_sec == tv2->tv_sec) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1471 | if (tv1->tv_usec > tv2->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1472 | return 1; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1473 | else if (tv2->tv_usec > tv1->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1474 | return -1; |
| 1475 | else |
| 1476 | return 0; |
| 1477 | } |
| 1478 | else if ((tv1->tv_sec > tv2->tv_sec + 1) || |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1479 | ((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] | 1480 | return 1; |
| 1481 | else if ((tv2->tv_sec > tv1->tv_sec + 1) || |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1482 | ((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] | 1483 | return -1; |
| 1484 | else |
| 1485 | return 0; |
| 1486 | } |
| 1487 | |
| 1488 | /* |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1489 | * returns the remaining time between tv1=now and event=tv2 |
| 1490 | * if tv2 is passed, 0 is returned. |
| 1491 | * Returns TIME_ETERNITY if tv2 is eternity. |
| 1492 | */ |
| 1493 | static inline unsigned long tv_remain2(struct timeval *tv1, struct timeval *tv2) { |
| 1494 | unsigned long ret; |
| 1495 | |
| 1496 | if (tv_iseternity(tv2)) |
| 1497 | return TIME_ETERNITY; |
| 1498 | |
| 1499 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 1500 | return 0; /* event elapsed */ |
| 1501 | |
| 1502 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
| 1503 | if (tv2->tv_usec > tv1->tv_usec) |
| 1504 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
| 1505 | else |
| 1506 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
| 1507 | return (unsigned long) ret; |
| 1508 | } |
| 1509 | |
| 1510 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1511 | * returns the first event between tv1 and tv2 into tvmin. |
| 1512 | * a zero tv is ignored. tvmin is returned. |
| 1513 | */ |
| 1514 | static inline struct timeval *tv_min(struct timeval *tvmin, |
| 1515 | struct timeval *tv1, struct timeval *tv2) { |
| 1516 | |
| 1517 | if (tv_cmp2(tv1, tv2) <= 0) |
| 1518 | *tvmin = *tv1; |
| 1519 | else |
| 1520 | *tvmin = *tv2; |
| 1521 | |
| 1522 | return tvmin; |
| 1523 | } |
| 1524 | |
| 1525 | |
| 1526 | |
| 1527 | /***********************************************************/ |
| 1528 | /* fd management ***************************************/ |
| 1529 | /***********************************************************/ |
| 1530 | |
| 1531 | |
| 1532 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1533 | /* Deletes an FD from the fdsets, and recomputes the maxfd limit. |
| 1534 | * The file descriptor is also closed. |
| 1535 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1536 | static inline void fd_delete(int fd) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1537 | FD_CLR(fd, StaticReadEvent); |
| 1538 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 08dedbe | 2005-12-18 01:13:48 +0100 | [diff] [blame] | 1539 | #if defined(ENABLE_EPOLL) |
| 1540 | if (PrevReadEvent) { |
| 1541 | FD_CLR(fd, PrevReadEvent); |
| 1542 | FD_CLR(fd, PrevWriteEvent); |
| 1543 | } |
| 1544 | #endif |
| 1545 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1546 | close(fd); |
| 1547 | fdtab[fd].state = FD_STCLOSE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1548 | |
| 1549 | while ((maxfd-1 >= 0) && (fdtab[maxfd-1].state == FD_STCLOSE)) |
| 1550 | maxfd--; |
| 1551 | } |
| 1552 | |
| 1553 | /* recomputes the maxfd limit from the fd */ |
| 1554 | static inline void fd_insert(int fd) { |
| 1555 | if (fd+1 > maxfd) |
| 1556 | maxfd = fd+1; |
| 1557 | } |
| 1558 | |
| 1559 | /*************************************************************/ |
| 1560 | /* task management ***************************************/ |
| 1561 | /*************************************************************/ |
| 1562 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1563 | /* puts the task <t> in run queue <q>, and returns <t> */ |
| 1564 | static inline struct task *task_wakeup(struct task **q, struct task *t) { |
| 1565 | if (t->state == TASK_RUNNING) |
| 1566 | return t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1567 | else { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1568 | t->rqnext = *q; |
| 1569 | t->state = TASK_RUNNING; |
| 1570 | return *q = t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1571 | } |
| 1572 | } |
| 1573 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1574 | /* removes the task <t> from the queue <q> |
| 1575 | * <s> MUST be <q>'s first task. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1576 | * set the run queue to point to the next one, and return it |
| 1577 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1578 | static inline struct task *task_sleep(struct task **q, struct task *t) { |
| 1579 | if (t->state == TASK_RUNNING) { |
| 1580 | *q = t->rqnext; |
| 1581 | t->state = TASK_IDLE; /* tell that s has left the run queue */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1582 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1583 | return *q; /* return next running task */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1587 | * 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] | 1588 | * from the run queue. A pointer to the task itself is returned. |
| 1589 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1590 | static inline struct task *task_delete(struct task *t) { |
| 1591 | t->prev->next = t->next; |
| 1592 | t->next->prev = t->prev; |
| 1593 | return t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1597 | * 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] | 1598 | */ |
| 1599 | static inline void task_free(struct task *t) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1600 | pool_free(task, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1601 | } |
| 1602 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1603 | /* 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] | 1604 | * may be only moved or left where it was, depending on its timing requirements. |
| 1605 | * <task> is returned. |
| 1606 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1607 | struct task *task_queue(struct task *task) { |
| 1608 | struct task *list = task->wq; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1609 | struct task *start_from; |
| 1610 | |
| 1611 | /* first, test if the task was already in a list */ |
| 1612 | if (task->prev == NULL) { |
| 1613 | // start_from = list; |
| 1614 | start_from = list->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1615 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1616 | stats_tsk_new++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1617 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1618 | /* insert the unlinked <task> into the list, searching back from the last entry */ |
| 1619 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1620 | start_from = start_from->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1621 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1622 | stats_tsk_nsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1623 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | // while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1627 | // start_from = start_from->next; |
| 1628 | // stats_tsk_nsrch++; |
| 1629 | // } |
| 1630 | } |
| 1631 | else if (task->prev == list || |
| 1632 | tv_cmp2(&task->expire, &task->prev->expire) >= 0) { /* walk right */ |
| 1633 | start_from = task->next; |
| 1634 | if (start_from == list || tv_cmp2(&task->expire, &start_from->expire) <= 0) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1635 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1636 | stats_tsk_good++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1637 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1638 | return task; /* it's already in the right place */ |
| 1639 | } |
| 1640 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1641 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1642 | stats_tsk_right++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1643 | #endif |
| 1644 | |
| 1645 | /* if the task is not at the right place, there's little chance that |
| 1646 | * it has only shifted a bit, and it will nearly always be queued |
| 1647 | * at the end of the list because of constant timeouts |
| 1648 | * (observed in real case). |
| 1649 | */ |
| 1650 | #ifndef WE_REALLY_THINK_THAT_THIS_TASK_MAY_HAVE_SHIFTED |
| 1651 | start_from = list->prev; /* assume we'll queue to the end of the list */ |
| 1652 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1653 | start_from = start_from->prev; |
| 1654 | #if STATTIME > 0 |
| 1655 | stats_tsk_lsrch++; |
| 1656 | #endif |
| 1657 | } |
| 1658 | #else /* WE_REALLY_... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1659 | /* insert the unlinked <task> into the list, searching after position <start_from> */ |
| 1660 | while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1661 | start_from = start_from->next; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1662 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1663 | stats_tsk_rsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1664 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1665 | } |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1666 | #endif /* WE_REALLY_... */ |
| 1667 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1668 | /* we need to unlink it now */ |
| 1669 | task_delete(task); |
| 1670 | } |
| 1671 | else { /* walk left. */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1672 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1673 | stats_tsk_left++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1674 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1675 | #ifdef LEFT_TO_TOP /* not very good */ |
| 1676 | start_from = list; |
| 1677 | while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1678 | start_from = start_from->next; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1679 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1680 | stats_tsk_lsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1681 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1682 | } |
| 1683 | #else |
| 1684 | start_from = task->prev->prev; /* valid because of the previous test above */ |
| 1685 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1686 | start_from = start_from->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1687 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1688 | stats_tsk_lsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1689 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1690 | } |
| 1691 | #endif |
| 1692 | /* we need to unlink it now */ |
| 1693 | task_delete(task); |
| 1694 | } |
| 1695 | task->prev = start_from; |
| 1696 | task->next = start_from->next; |
| 1697 | task->next->prev = task; |
| 1698 | start_from->next = task; |
| 1699 | return task; |
| 1700 | } |
| 1701 | |
| 1702 | |
| 1703 | /*********************************************************************/ |
| 1704 | /* more specific functions ***************************************/ |
| 1705 | /*********************************************************************/ |
| 1706 | |
| 1707 | /* some prototypes */ |
| 1708 | static int maintain_proxies(void); |
| 1709 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1710 | /* This either returns the sockname or the original destination address. Code |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1711 | * inspired from Patrick Schaaf's example of nf_getsockname() implementation. |
| 1712 | */ |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 1713 | 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] | 1714 | #if defined(TPROXY) && defined(SO_ORIGINAL_DST) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1715 | return getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, (void *)sa, salen); |
| 1716 | #else |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1717 | #if defined(TPROXY) && defined(USE_GETSOCKNAME) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1718 | return getsockname(fd, (struct sockaddr *)sa, salen); |
| 1719 | #else |
| 1720 | return -1; |
| 1721 | #endif |
| 1722 | #endif |
| 1723 | } |
| 1724 | |
| 1725 | /* |
| 1726 | * frees the context associated to a session. It must have been removed first. |
| 1727 | */ |
| 1728 | static inline void session_free(struct session *s) { |
| 1729 | if (s->req) |
| 1730 | pool_free(buffer, s->req); |
| 1731 | if (s->rep) |
| 1732 | pool_free(buffer, s->rep); |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 1733 | |
| 1734 | if (s->rsp_cap != NULL) { |
| 1735 | struct cap_hdr *h; |
| 1736 | for (h = s->proxy->rsp_cap; h; h = h->next) { |
| 1737 | if (s->rsp_cap[h->index] != NULL) |
| 1738 | pool_free_to(h->pool, s->rsp_cap[h->index]); |
| 1739 | } |
| 1740 | pool_free_to(s->proxy->rsp_cap_pool, s->rsp_cap); |
| 1741 | } |
| 1742 | if (s->req_cap != NULL) { |
| 1743 | struct cap_hdr *h; |
| 1744 | for (h = s->proxy->req_cap; h; h = h->next) { |
| 1745 | if (s->req_cap[h->index] != NULL) |
| 1746 | pool_free_to(h->pool, s->req_cap[h->index]); |
| 1747 | } |
| 1748 | pool_free_to(s->proxy->req_cap_pool, s->req_cap); |
| 1749 | } |
| 1750 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1751 | if (s->logs.uri) |
| 1752 | pool_free(requri, s->logs.uri); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1753 | if (s->logs.cli_cookie) |
| 1754 | pool_free(capture, s->logs.cli_cookie); |
| 1755 | if (s->logs.srv_cookie) |
| 1756 | pool_free(capture, s->logs.srv_cookie); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1757 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1758 | pool_free(session, s); |
| 1759 | } |
| 1760 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1761 | |
| 1762 | /* |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1763 | * This function tries to find a running server for the proxy <px>. A first |
| 1764 | * pass looks for active servers, and if none is found, a second pass also |
| 1765 | * looks for backup servers. |
| 1766 | * If no valid server is found, NULL is returned and px->cursrv is left undefined. |
| 1767 | */ |
| 1768 | static inline struct server *find_server(struct proxy *px) { |
| 1769 | struct server *srv = px->cursrv; |
| 1770 | int ignore_backup = 1; |
| 1771 | |
| 1772 | do { |
| 1773 | do { |
| 1774 | if (srv == NULL) |
| 1775 | srv = px->srv; |
| 1776 | if (srv->state & SRV_RUNNING |
| 1777 | && !((srv->state & SRV_BACKUP) && ignore_backup)) |
| 1778 | return srv; |
| 1779 | srv = srv->next; |
| 1780 | } while (srv != px->cursrv); |
| 1781 | } while (ignore_backup--); |
| 1782 | return NULL; |
| 1783 | } |
| 1784 | |
| 1785 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1786 | * 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] | 1787 | * is set, or to the dispatch server if (s->direct) is 0. |
| 1788 | * It can return one of : |
| 1789 | * - SN_ERR_NONE if everything's OK |
| 1790 | * - SN_ERR_SRVTO if there are no more servers |
| 1791 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 1792 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 1793 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 1794 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 1795 | * 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] | 1796 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1797 | int connect_server(struct session *s) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1798 | int fd; |
| 1799 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 1800 | #ifdef DEBUG_FULL |
| 1801 | fprintf(stderr,"connect_server : s=%p\n",s); |
| 1802 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1803 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 1804 | if (s->flags & SN_DIRECT) { /* srv cannot be null */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1805 | s->srv_addr = s->srv->addr; |
| 1806 | } |
| 1807 | else if (s->proxy->options & PR_O_BALANCE) { |
| 1808 | if (s->proxy->options & PR_O_BALANCE_RR) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1809 | struct server *srv; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1810 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1811 | srv = find_server(s->proxy); |
| 1812 | |
| 1813 | if (srv == NULL) /* no server left */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1814 | return SN_ERR_SRVTO; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1815 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1816 | s->srv_addr = srv->addr; |
| 1817 | s->srv = srv; |
| 1818 | s->proxy->cursrv = srv->next; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1819 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1820 | else /* unknown balancing algorithm */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1821 | return SN_ERR_INTERNAL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1822 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1823 | else if (*(int *)&s->proxy->dispatch_addr.sin_addr) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1824 | /* connect to the defined dispatch addr */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1825 | s->srv_addr = s->proxy->dispatch_addr; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1826 | } |
| 1827 | else if (s->proxy->options & PR_O_TRANSP) { |
| 1828 | /* in transparent mode, use the original dest addr if no dispatch specified */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1829 | socklen_t salen = sizeof(s->srv_addr); |
| 1830 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1831 | if (get_original_dst(s->cli_fd, &s->srv_addr, &salen) == -1) { |
| 1832 | qfprintf(stderr, "Cannot get original server address.\n"); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1833 | return SN_ERR_INTERNAL; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1834 | } |
| 1835 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1836 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1837 | /* if this server remaps proxied ports, we'll use |
| 1838 | * the port the client connected to with an offset. */ |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 1839 | if (s->srv != NULL && s->srv->state & SRV_MAPPORTS) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1840 | struct sockaddr_in sockname; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1841 | socklen_t namelen = sizeof(sockname); |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1842 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1843 | if (!(s->proxy->options & PR_O_TRANSP) || |
| 1844 | get_original_dst(s->cli_fd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 1845 | getsockname(s->cli_fd, (struct sockaddr *)&sockname, &namelen); |
| 1846 | s->srv_addr.sin_port = htons(ntohs(s->srv_addr.sin_port) + ntohs(sockname.sin_port)); |
| 1847 | } |
| 1848 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1849 | 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] | 1850 | qfprintf(stderr, "Cannot get a server socket.\n"); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1851 | |
| 1852 | if (errno == ENFILE) |
| 1853 | send_log(s->proxy, LOG_EMERG, |
| 1854 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 1855 | s->proxy->id, maxfd); |
| 1856 | else if (errno == EMFILE) |
| 1857 | send_log(s->proxy, LOG_EMERG, |
| 1858 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 1859 | s->proxy->id, maxfd); |
| 1860 | else if (errno == ENOBUFS || errno == ENOMEM) |
| 1861 | send_log(s->proxy, LOG_EMERG, |
| 1862 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 1863 | s->proxy->id, maxfd); |
| 1864 | /* this is a resource error */ |
| 1865 | return SN_ERR_RESOURCE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1866 | } |
| 1867 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1868 | if (fd >= global.maxsock) { |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1869 | /* do not log anything there, it's a normal condition when this option |
| 1870 | * is used to serialize connections to a server ! |
| 1871 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1872 | Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 1873 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1874 | return SN_ERR_PRXCOND; /* it is a configuration limit */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1875 | } |
| 1876 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1877 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
| 1878 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1879 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1880 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1881 | return SN_ERR_INTERNAL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1882 | } |
| 1883 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1884 | if (s->proxy->options & PR_O_TCP_SRV_KA) |
| 1885 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 1886 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1887 | /* allow specific binding : |
| 1888 | * - server-specific at first |
| 1889 | * - proxy-specific next |
| 1890 | */ |
| 1891 | if (s->srv != NULL && s->srv->state & SRV_BIND_SRC) { |
| 1892 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 1893 | if (bind(fd, (struct sockaddr *)&s->srv->source_addr, sizeof(s->srv->source_addr)) == -1) { |
| 1894 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 1895 | s->proxy->id, s->srv->id); |
| 1896 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1897 | send_log(s->proxy, LOG_EMERG, |
| 1898 | "Cannot bind to source address before connect() for server %s/%s.\n", |
| 1899 | s->proxy->id, s->srv->id); |
| 1900 | return SN_ERR_RESOURCE; |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 1901 | } |
| 1902 | } |
| 1903 | else if (s->proxy->options & PR_O_BIND_SRC) { |
| 1904 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 1905 | if (bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) { |
| 1906 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", s->proxy->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 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1913 | } |
| 1914 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1915 | if ((connect(fd, (struct sockaddr *)&s->srv_addr, sizeof(s->srv_addr)) == -1) && |
| 1916 | (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) { |
| 1917 | |
| 1918 | if (errno == EAGAIN || errno == EADDRINUSE) { |
| 1919 | char *msg; |
| 1920 | if (errno == EAGAIN) /* no free ports left, try again later */ |
| 1921 | msg = "no free ports"; |
| 1922 | else |
| 1923 | msg = "local address already in use"; |
| 1924 | |
| 1925 | qfprintf(stderr,"Cannot connect: %s.\n",msg); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1926 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1927 | send_log(s->proxy, LOG_EMERG, |
| 1928 | "Connect() failed for server %s/%s: %s.\n", |
| 1929 | s->proxy->id, s->srv->id, msg); |
| 1930 | return SN_ERR_RESOURCE; |
| 1931 | } else if (errno == ETIMEDOUT) { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1932 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1933 | close(fd); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1934 | return SN_ERR_SRVTO; |
| 1935 | } else { |
| 1936 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 1937 | //qfprintf(stderr,"Connect(): %d", errno); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1938 | close(fd); |
| 1939 | return SN_ERR_SRVCL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1940 | } |
| 1941 | } |
| 1942 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1943 | fdtab[fd].owner = s->task; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1944 | fdtab[fd].read = &event_srv_read; |
| 1945 | fdtab[fd].write = &event_srv_write; |
| 1946 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 1947 | |
| 1948 | FD_SET(fd, StaticWriteEvent); /* for connect status */ |
| 1949 | |
| 1950 | fd_insert(fd); |
| 1951 | |
| 1952 | if (s->proxy->contimeout) |
| 1953 | tv_delayfrom(&s->cnexpire, &now, s->proxy->contimeout); |
| 1954 | else |
| 1955 | tv_eternity(&s->cnexpire); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 1956 | return SN_ERR_NONE; /* connection is OK */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | /* |
| 1960 | * this function is called on a read event from a client socket. |
| 1961 | * It returns 0. |
| 1962 | */ |
| 1963 | int event_cli_read(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1964 | struct task *t = fdtab[fd].owner; |
| 1965 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1966 | struct buffer *b = s->req; |
| 1967 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1968 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 1969 | #ifdef DEBUG_FULL |
| 1970 | fprintf(stderr,"event_cli_read : fd=%d, s=%p\n", fd, s); |
| 1971 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1972 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1973 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 1974 | #ifdef FILL_BUFFERS |
| 1975 | while (1) |
| 1976 | #else |
| 1977 | do |
| 1978 | #endif |
| 1979 | { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1980 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 1981 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1982 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1983 | } |
| 1984 | else if (b->r > b->w) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1985 | max = b->rlim - b->r; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1986 | } |
| 1987 | else { |
| 1988 | max = b->w - b->r; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1989 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 1990 | * since it means that the rewrite protection has been removed. This |
| 1991 | * implies that the if statement can be removed. |
| 1992 | */ |
| 1993 | if (max > b->rlim - b->data) |
| 1994 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | if (max == 0) { /* not anymore room to store data */ |
| 1998 | FD_CLR(fd, StaticReadEvent); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1999 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2000 | } |
| 2001 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2002 | #ifndef MSG_NOSIGNAL |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2003 | { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2004 | int skerr; |
| 2005 | socklen_t lskerr = sizeof(skerr); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2006 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2007 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2008 | if (skerr) |
| 2009 | ret = -1; |
| 2010 | else |
| 2011 | ret = recv(fd, b->r, max, 0); |
| 2012 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2013 | #else |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2014 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2015 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2016 | if (ret > 0) { |
| 2017 | b->r += ret; |
| 2018 | b->l += ret; |
| 2019 | s->res_cr = RES_DATA; |
| 2020 | |
| 2021 | if (b->r == b->data + BUFSIZE) { |
| 2022 | b->r = b->data; /* wrap around the buffer */ |
| 2023 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2024 | |
| 2025 | b->total += ret; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2026 | /* we hope to read more data or to get a close on next round */ |
| 2027 | continue; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2028 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2029 | else if (ret == 0) { |
| 2030 | s->res_cr = RES_NULL; |
| 2031 | break; |
| 2032 | } |
| 2033 | else if (errno == EAGAIN) {/* ignore EAGAIN */ |
| 2034 | break; |
| 2035 | } |
| 2036 | else { |
| 2037 | s->res_cr = RES_ERROR; |
| 2038 | fdtab[fd].state = FD_STERROR; |
| 2039 | break; |
| 2040 | } |
| 2041 | } /* while(1) */ |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2042 | #ifndef FILL_BUFFERS |
| 2043 | while (0); |
| 2044 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2045 | } |
| 2046 | else { |
| 2047 | s->res_cr = RES_ERROR; |
| 2048 | fdtab[fd].state = FD_STERROR; |
| 2049 | } |
| 2050 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2051 | if (s->res_cr != RES_SILENT) { |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2052 | if (s->proxy->clitimeout && FD_ISSET(fd, StaticReadEvent)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2053 | tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout); |
| 2054 | else |
| 2055 | tv_eternity(&s->crexpire); |
| 2056 | |
| 2057 | task_wakeup(&rq, t); |
| 2058 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2059 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2060 | return 0; |
| 2061 | } |
| 2062 | |
| 2063 | |
| 2064 | /* |
| 2065 | * this function is called on a read event from a server socket. |
| 2066 | * It returns 0. |
| 2067 | */ |
| 2068 | int event_srv_read(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2069 | struct task *t = fdtab[fd].owner; |
| 2070 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2071 | struct buffer *b = s->rep; |
| 2072 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2073 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2074 | #ifdef DEBUG_FULL |
| 2075 | fprintf(stderr,"event_srv_read : fd=%d, s=%p\n", fd, s); |
| 2076 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2077 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2078 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2079 | #ifdef FILL_BUFFERS |
| 2080 | while (1) |
| 2081 | #else |
| 2082 | do |
| 2083 | #endif |
| 2084 | { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2085 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 2086 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2087 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2088 | } |
| 2089 | else if (b->r > b->w) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2090 | max = b->rlim - b->r; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2091 | } |
| 2092 | else { |
| 2093 | max = b->w - b->r; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2094 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 2095 | * since it means that the rewrite protection has been removed. This |
| 2096 | * implies that the if statement can be removed. |
| 2097 | */ |
| 2098 | if (max > b->rlim - b->data) |
| 2099 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | if (max == 0) { /* not anymore room to store data */ |
| 2103 | FD_CLR(fd, StaticReadEvent); |
| 2104 | break; |
| 2105 | } |
| 2106 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2107 | #ifndef MSG_NOSIGNAL |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2108 | { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2109 | int skerr; |
| 2110 | socklen_t lskerr = sizeof(skerr); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2111 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2112 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2113 | if (skerr) |
| 2114 | ret = -1; |
| 2115 | else |
| 2116 | ret = recv(fd, b->r, max, 0); |
| 2117 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2118 | #else |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2119 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2120 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2121 | if (ret > 0) { |
| 2122 | b->r += ret; |
| 2123 | b->l += ret; |
| 2124 | s->res_sr = RES_DATA; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2125 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2126 | if (b->r == b->data + BUFSIZE) { |
| 2127 | b->r = b->data; /* wrap around the buffer */ |
| 2128 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2129 | |
| 2130 | b->total += ret; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2131 | /* we hope to read more data or to get a close on next round */ |
| 2132 | continue; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2133 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2134 | else if (ret == 0) { |
| 2135 | s->res_sr = RES_NULL; |
| 2136 | break; |
| 2137 | } |
| 2138 | else if (errno == EAGAIN) {/* ignore EAGAIN */ |
| 2139 | break; |
| 2140 | } |
| 2141 | else { |
| 2142 | s->res_sr = RES_ERROR; |
| 2143 | fdtab[fd].state = FD_STERROR; |
| 2144 | break; |
| 2145 | } |
| 2146 | } /* while(1) */ |
willy tarreau | 5dffb60 | 2005-12-18 01:15:23 +0100 | [diff] [blame] | 2147 | #ifndef FILL_BUFFERS |
| 2148 | while (0); |
| 2149 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2150 | } |
| 2151 | else { |
| 2152 | s->res_sr = RES_ERROR; |
| 2153 | fdtab[fd].state = FD_STERROR; |
| 2154 | } |
| 2155 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2156 | if (s->res_sr != RES_SILENT) { |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2157 | if (s->proxy->srvtimeout && FD_ISSET(fd, StaticReadEvent)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2158 | tv_delayfrom(&s->srexpire, &now, s->proxy->srvtimeout); |
| 2159 | else |
| 2160 | tv_eternity(&s->srexpire); |
| 2161 | |
| 2162 | task_wakeup(&rq, t); |
| 2163 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2164 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2165 | return 0; |
| 2166 | } |
| 2167 | |
| 2168 | /* |
| 2169 | * this function is called on a write event from a client socket. |
| 2170 | * It returns 0. |
| 2171 | */ |
| 2172 | int event_cli_write(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2173 | struct task *t = fdtab[fd].owner; |
| 2174 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2175 | struct buffer *b = s->rep; |
| 2176 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2177 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2178 | #ifdef DEBUG_FULL |
| 2179 | fprintf(stderr,"event_cli_write : fd=%d, s=%p\n", fd, s); |
| 2180 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2181 | |
| 2182 | 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] | 2183 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2184 | // max = BUFSIZE; BUG !!!! |
| 2185 | max = 0; |
| 2186 | } |
| 2187 | else if (b->r > b->w) { |
| 2188 | max = b->r - b->w; |
| 2189 | } |
| 2190 | else |
| 2191 | max = b->data + BUFSIZE - b->w; |
| 2192 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2193 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2194 | if (max == 0) { |
| 2195 | s->res_cw = RES_NULL; |
| 2196 | task_wakeup(&rq, t); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2197 | tv_eternity(&s->cwexpire); |
| 2198 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2199 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2200 | } |
| 2201 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2202 | #ifndef MSG_NOSIGNAL |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2203 | { |
| 2204 | int skerr; |
| 2205 | socklen_t lskerr = sizeof(skerr); |
| 2206 | |
| 2207 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2208 | if (skerr) |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2209 | ret = -1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2210 | else |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2211 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2212 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2213 | #else |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2214 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2215 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2216 | |
| 2217 | if (ret > 0) { |
| 2218 | b->l -= ret; |
| 2219 | b->w += ret; |
| 2220 | |
| 2221 | s->res_cw = RES_DATA; |
| 2222 | |
| 2223 | if (b->w == b->data + BUFSIZE) { |
| 2224 | b->w = b->data; /* wrap around the buffer */ |
| 2225 | } |
| 2226 | } |
| 2227 | else if (ret == 0) { |
| 2228 | /* nothing written, just make as if we were never called */ |
| 2229 | // s->res_cw = RES_NULL; |
| 2230 | return 0; |
| 2231 | } |
| 2232 | else if (errno == EAGAIN) /* ignore EAGAIN */ |
| 2233 | return 0; |
| 2234 | else { |
| 2235 | s->res_cw = RES_ERROR; |
| 2236 | fdtab[fd].state = FD_STERROR; |
| 2237 | } |
| 2238 | } |
| 2239 | else { |
| 2240 | s->res_cw = RES_ERROR; |
| 2241 | fdtab[fd].state = FD_STERROR; |
| 2242 | } |
| 2243 | |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2244 | if (s->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2245 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2246 | /* FIXME: to avoid the client to read-time-out during writes, we refresh it */ |
| 2247 | s->crexpire = s->cwexpire; |
| 2248 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2249 | else |
| 2250 | tv_eternity(&s->cwexpire); |
| 2251 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2252 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2253 | return 0; |
| 2254 | } |
| 2255 | |
| 2256 | |
| 2257 | /* |
| 2258 | * this function is called on a write event from a server socket. |
| 2259 | * It returns 0. |
| 2260 | */ |
| 2261 | int event_srv_write(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2262 | struct task *t = fdtab[fd].owner; |
| 2263 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2264 | struct buffer *b = s->req; |
| 2265 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2266 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 2267 | #ifdef DEBUG_FULL |
| 2268 | fprintf(stderr,"event_srv_write : fd=%d, s=%p\n", fd, s); |
| 2269 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2270 | |
| 2271 | 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] | 2272 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2273 | // max = BUFSIZE; BUG !!!! |
| 2274 | max = 0; |
| 2275 | } |
| 2276 | else if (b->r > b->w) { |
| 2277 | max = b->r - b->w; |
| 2278 | } |
| 2279 | else |
| 2280 | max = b->data + BUFSIZE - b->w; |
| 2281 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2282 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2283 | if (max == 0) { |
| 2284 | /* 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] | 2285 | if (s->srv_state == SV_STCONN) { |
| 2286 | int skerr; |
| 2287 | socklen_t lskerr = sizeof(skerr); |
| 2288 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2289 | if (skerr) { |
| 2290 | s->res_sw = RES_ERROR; |
| 2291 | fdtab[fd].state = FD_STERROR; |
| 2292 | task_wakeup(&rq, t); |
| 2293 | tv_eternity(&s->swexpire); |
| 2294 | FD_CLR(fd, StaticWriteEvent); |
| 2295 | return 0; |
| 2296 | } |
| 2297 | } |
| 2298 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2299 | s->res_sw = RES_NULL; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2300 | task_wakeup(&rq, t); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2301 | fdtab[fd].state = FD_STREADY; |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2302 | tv_eternity(&s->swexpire); |
| 2303 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2304 | return 0; |
| 2305 | } |
| 2306 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2307 | #ifndef MSG_NOSIGNAL |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2308 | { |
| 2309 | int skerr; |
| 2310 | socklen_t lskerr = sizeof(skerr); |
| 2311 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2312 | if (skerr) |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2313 | ret = -1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2314 | else |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2315 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2316 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2317 | #else |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2318 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 2319 | #endif |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2320 | fdtab[fd].state = FD_STREADY; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2321 | if (ret > 0) { |
| 2322 | b->l -= ret; |
| 2323 | b->w += ret; |
| 2324 | |
| 2325 | s->res_sw = RES_DATA; |
| 2326 | |
| 2327 | if (b->w == b->data + BUFSIZE) { |
| 2328 | b->w = b->data; /* wrap around the buffer */ |
| 2329 | } |
| 2330 | } |
| 2331 | else if (ret == 0) { |
| 2332 | /* nothing written, just make as if we were never called */ |
| 2333 | // s->res_sw = RES_NULL; |
| 2334 | return 0; |
| 2335 | } |
| 2336 | else if (errno == EAGAIN) /* ignore EAGAIN */ |
| 2337 | return 0; |
| 2338 | else { |
| 2339 | s->res_sw = RES_ERROR; |
| 2340 | fdtab[fd].state = FD_STERROR; |
| 2341 | } |
| 2342 | } |
| 2343 | else { |
| 2344 | s->res_sw = RES_ERROR; |
| 2345 | fdtab[fd].state = FD_STERROR; |
| 2346 | } |
| 2347 | |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2348 | if (s->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2349 | tv_delayfrom(&s->swexpire, &now, s->proxy->srvtimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2350 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 2351 | s->srexpire = s->swexpire; |
| 2352 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2353 | else |
| 2354 | tv_eternity(&s->swexpire); |
| 2355 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2356 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2357 | return 0; |
| 2358 | } |
| 2359 | |
| 2360 | |
| 2361 | /* |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2362 | * returns a message to the client ; the connection is shut down for read, |
| 2363 | * and the request is cleared so that no server connection can be initiated. |
| 2364 | * The client must be in a valid state for this (HEADER, DATA ...). |
| 2365 | * Nothing is performed on the server side. |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2366 | * The reply buffer doesn't need to be empty before this. |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2367 | */ |
| 2368 | void client_retnclose(struct session *s, int len, const char *msg) { |
| 2369 | FD_CLR(s->cli_fd, StaticReadEvent); |
| 2370 | FD_SET(s->cli_fd, StaticWriteEvent); |
| 2371 | tv_eternity(&s->crexpire); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2372 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2373 | shutdown(s->cli_fd, SHUT_RD); |
| 2374 | s->cli_state = CL_STSHUTR; |
| 2375 | strcpy(s->rep->data, msg); |
| 2376 | s->rep->l = len; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2377 | 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] | 2378 | s->rep->r += len; |
| 2379 | s->req->l = 0; |
| 2380 | } |
| 2381 | |
| 2382 | |
| 2383 | /* |
| 2384 | * returns a message into the rep buffer, and flushes the req buffer. |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2385 | * The reply buffer doesn't need to be empty before this. |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2386 | */ |
| 2387 | void client_return(struct session *s, int len, const char *msg) { |
| 2388 | strcpy(s->rep->data, msg); |
| 2389 | s->rep->l = len; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2390 | 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] | 2391 | s->rep->r += len; |
| 2392 | s->req->l = 0; |
| 2393 | } |
| 2394 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2395 | /* |
| 2396 | * send a log for the session when we have enough info about it |
| 2397 | */ |
| 2398 | void sess_log(struct session *s) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2399 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2400 | struct proxy *p = s->proxy; |
| 2401 | int log; |
| 2402 | char *uri; |
| 2403 | char *pxid; |
| 2404 | char *srv; |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2405 | struct tm *tm; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2406 | |
| 2407 | /* This is a first attempt at a better logging system. |
| 2408 | * For now, we rely on send_log() to provide the date, although it obviously |
| 2409 | * is the date of the log and not of the request, and most fields are not |
| 2410 | * computed. |
| 2411 | */ |
| 2412 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2413 | log = p->to_log & ~s->logs.logwait; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2414 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2415 | if (s->cli_addr.ss_family == AF_INET) |
| 2416 | inet_ntop(AF_INET, |
| 2417 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 2418 | pn, sizeof(pn)); |
| 2419 | else |
| 2420 | inet_ntop(AF_INET6, |
| 2421 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 2422 | pn, sizeof(pn)); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2423 | |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2424 | uri = (log & LW_REQ) ? s->logs.uri ? s->logs.uri : "<BADREQ>" : ""; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2425 | pxid = p->id; |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2426 | 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] | 2427 | |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2428 | tm = localtime(&s->logs.tv_accept.tv_sec); |
| 2429 | if (p->to_log & LW_REQ) { |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2430 | char tmpline[MAX_SYSLOG_LEN], *h; |
| 2431 | int hdr; |
| 2432 | |
| 2433 | h = tmpline; |
| 2434 | if (p->to_log & LW_REQHDR && (h < tmpline + sizeof(tmpline) - 10)) { |
| 2435 | *(h++) = ' '; |
| 2436 | *(h++) = '{'; |
| 2437 | for (hdr = 0; hdr < p->nb_req_cap; hdr++) { |
| 2438 | if (hdr) |
| 2439 | *(h++) = '|'; |
| 2440 | if (s->req_cap[hdr] != NULL) |
| 2441 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, '#', hdr_encode_map, s->req_cap[hdr]); |
| 2442 | } |
| 2443 | *(h++) = '}'; |
| 2444 | } |
| 2445 | |
| 2446 | if (p->to_log & LW_RSPHDR && (h < tmpline + sizeof(tmpline) - 7)) { |
| 2447 | *(h++) = ' '; |
| 2448 | *(h++) = '{'; |
| 2449 | for (hdr = 0; hdr < p->nb_rsp_cap; hdr++) { |
| 2450 | if (hdr) |
| 2451 | *(h++) = '|'; |
| 2452 | if (s->rsp_cap[hdr] != NULL) |
| 2453 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, '#', hdr_encode_map, s->rsp_cap[hdr]); |
| 2454 | } |
| 2455 | *(h++) = '}'; |
| 2456 | } |
| 2457 | |
| 2458 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 2459 | *(h++) = ' '; |
| 2460 | *(h++) = '"'; |
| 2461 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, '#', url_encode_map, uri); |
| 2462 | *(h++) = '"'; |
| 2463 | } |
| 2464 | *h = '\0'; |
| 2465 | |
willy tarreau | 0fe3965 | 2005-12-18 01:25:24 +0100 | [diff] [blame] | 2466 | 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] | 2467 | pn, |
| 2468 | (s->cli_addr.ss_family == AF_INET) ? |
| 2469 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 2470 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2471 | tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900, |
| 2472 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 2473 | pxid, srv, |
| 2474 | s->logs.t_request, |
| 2475 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_request : -1, |
| 2476 | (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] | 2477 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 2478 | s->logs.status, |
| 2479 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.bytes, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2480 | s->logs.cli_cookie ? s->logs.cli_cookie : "-", |
| 2481 | s->logs.srv_cookie ? s->logs.srv_cookie : "-", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2482 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 2483 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 2484 | (p->options & PR_O_COOK_ANY) ? sess_cookie[(s->flags & SN_CK_MASK) >> SN_CK_SHIFT] : '-', |
| 2485 | (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] | 2486 | p->nbconn, actconn, tmpline); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2487 | } |
| 2488 | else { |
willy tarreau | 0fe3965 | 2005-12-18 01:25:24 +0100 | [diff] [blame] | 2489 | 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] | 2490 | pn, |
| 2491 | (s->cli_addr.ss_family == AF_INET) ? |
| 2492 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 2493 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2494 | tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900, |
| 2495 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2496 | pxid, srv, |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 2497 | (s->logs.t_connect >= 0) ? s->logs.t_connect : -1, |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 2498 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 2499 | (p->to_log & LW_BYTES) ? "" : "+", s->logs.bytes, |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2500 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
willy tarreau | 0fe3965 | 2005-12-18 01:25:24 +0100 | [diff] [blame] | 2501 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 2502 | p->nbconn, actconn); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | s->logs.logwait = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2506 | } |
| 2507 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2508 | |
| 2509 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2510 | * 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] | 2511 | * to an accept. It tries to accept as many connections as possible. |
| 2512 | * It returns 0. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2513 | */ |
| 2514 | int event_accept(int fd) { |
| 2515 | struct proxy *p = (struct proxy *)fdtab[fd].owner; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2516 | struct session *s; |
| 2517 | struct task *t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2518 | int cfd; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2519 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2520 | while (p->nbconn < p->maxconn) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2521 | struct sockaddr_storage addr; |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2522 | socklen_t laddr = sizeof(addr); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2523 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2524 | if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) { |
| 2525 | switch (errno) { |
| 2526 | case EAGAIN: |
| 2527 | case EINTR: |
| 2528 | case ECONNABORTED: |
| 2529 | return 0; /* nothing more to accept */ |
| 2530 | case ENFILE: |
| 2531 | send_log(p, LOG_EMERG, |
| 2532 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 2533 | p->id, maxfd); |
| 2534 | return 0; |
| 2535 | case EMFILE: |
| 2536 | send_log(p, LOG_EMERG, |
| 2537 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 2538 | p->id, maxfd); |
| 2539 | return 0; |
| 2540 | case ENOBUFS: |
| 2541 | case ENOMEM: |
| 2542 | send_log(p, LOG_EMERG, |
| 2543 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 2544 | p->id, maxfd); |
| 2545 | return 0; |
| 2546 | default: |
| 2547 | return 0; |
| 2548 | } |
| 2549 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2550 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2551 | if ((s = pool_alloc(session)) == NULL) { /* disable this proxy for a while */ |
| 2552 | Alert("out of memory in event_accept().\n"); |
| 2553 | FD_CLR(fd, StaticReadEvent); |
| 2554 | p->state = PR_STIDLE; |
| 2555 | close(cfd); |
| 2556 | return 0; |
| 2557 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2558 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2559 | /* if this session comes from a known monitoring system, we want to ignore |
| 2560 | * it as soon as possible, which means closing it immediately for TCP. |
| 2561 | */ |
| 2562 | s->flags = 0; |
| 2563 | if (addr.ss_family == AF_INET && |
| 2564 | p->mon_mask.s_addr && |
| 2565 | (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr) { |
| 2566 | if (p->mode == PR_MODE_TCP) { |
| 2567 | close(cfd); |
| 2568 | pool_free(session, s); |
| 2569 | continue; |
| 2570 | } |
| 2571 | s->flags |= SN_MONITOR; |
| 2572 | } |
| 2573 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2574 | if ((t = pool_alloc(task)) == NULL) { /* disable this proxy for a while */ |
| 2575 | Alert("out of memory in event_accept().\n"); |
| 2576 | FD_CLR(fd, StaticReadEvent); |
| 2577 | p->state = PR_STIDLE; |
| 2578 | close(cfd); |
| 2579 | pool_free(session, s); |
| 2580 | return 0; |
| 2581 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2582 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2583 | s->cli_addr = addr; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2584 | if (cfd >= global.maxsock) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2585 | Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 2586 | close(cfd); |
| 2587 | pool_free(task, t); |
| 2588 | pool_free(session, s); |
| 2589 | return 0; |
| 2590 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2591 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2592 | if ((fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) || |
| 2593 | (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, |
| 2594 | (char *) &one, sizeof(one)) == -1)) { |
| 2595 | Alert("accept(): cannot set the socket in non blocking mode. Giving up\n"); |
| 2596 | close(cfd); |
| 2597 | pool_free(task, t); |
| 2598 | pool_free(session, s); |
| 2599 | return 0; |
| 2600 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2601 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2602 | if (p->options & PR_O_TCP_CLI_KA) |
| 2603 | setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 2604 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2605 | t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */ |
| 2606 | t->wq = LIST_HEAD(wait_queue); /* but already has a wait queue assigned */ |
| 2607 | t->state = TASK_IDLE; |
| 2608 | t->process = process_session; |
| 2609 | t->context = s; |
| 2610 | |
| 2611 | s->task = t; |
| 2612 | s->proxy = p; |
| 2613 | s->cli_state = (p->mode == PR_MODE_HTTP) ? CL_STHEADERS : CL_STDATA; /* no HTTP headers for non-HTTP proxies */ |
| 2614 | s->srv_state = SV_STIDLE; |
| 2615 | s->req = s->rep = NULL; /* will be allocated later */ |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 2616 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2617 | s->res_cr = s->res_cw = s->res_sr = s->res_sw = RES_SILENT; |
| 2618 | s->cli_fd = cfd; |
| 2619 | s->srv_fd = -1; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2620 | s->srv = NULL; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2621 | s->conn_retries = p->conn_retries; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2622 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2623 | if (s->flags & SN_MONITOR) |
| 2624 | s->logs.logwait = 0; |
| 2625 | else |
| 2626 | s->logs.logwait = p->to_log; |
| 2627 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2628 | s->logs.tv_accept = now; |
| 2629 | s->logs.t_request = -1; |
| 2630 | s->logs.t_connect = -1; |
| 2631 | s->logs.t_data = -1; |
| 2632 | s->logs.t_close = 0; |
| 2633 | s->logs.uri = NULL; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2634 | s->logs.cli_cookie = NULL; |
| 2635 | s->logs.srv_cookie = NULL; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2636 | s->logs.status = -1; |
| 2637 | s->logs.bytes = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2638 | |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2639 | s->uniq_id = totalconn; |
| 2640 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2641 | if (p->nb_req_cap > 0) { |
| 2642 | if ((s->req_cap = |
| 2643 | pool_alloc_from(p->req_cap_pool, p->nb_req_cap*sizeof(char *))) |
| 2644 | == NULL) { /* no memory */ |
| 2645 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2646 | pool_free(task, t); |
| 2647 | pool_free(session, s); |
| 2648 | return 0; |
| 2649 | } |
| 2650 | memset(s->req_cap, 0, p->nb_req_cap*sizeof(char *)); |
| 2651 | } |
| 2652 | else |
| 2653 | s->req_cap = NULL; |
| 2654 | |
| 2655 | if (p->nb_rsp_cap > 0) { |
| 2656 | if ((s->rsp_cap = |
| 2657 | pool_alloc_from(p->rsp_cap_pool, p->nb_rsp_cap*sizeof(char *))) |
| 2658 | == NULL) { /* no memory */ |
| 2659 | if (s->req_cap != NULL) |
| 2660 | pool_free_to(p->req_cap_pool, s->req_cap); |
| 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->rsp_cap, 0, p->nb_rsp_cap*sizeof(char *)); |
| 2667 | } |
| 2668 | else |
| 2669 | s->rsp_cap = NULL; |
| 2670 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2671 | if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP) |
| 2672 | && (p->logfac1 >= 0 || p->logfac2 >= 0)) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2673 | struct sockaddr_storage sockname; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2674 | socklen_t namelen = sizeof(sockname); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2675 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2676 | if (addr.ss_family != AF_INET || |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2677 | !(s->proxy->options & PR_O_TRANSP) || |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2678 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2679 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2680 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2681 | if (p->to_log) { |
| 2682 | /* we have the client ip */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2683 | if (s->logs.logwait & LW_CLIP) |
| 2684 | if (!(s->logs.logwait &= ~LW_CLIP)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2685 | sess_log(s); |
| 2686 | } |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2687 | else if (s->cli_addr.ss_family == AF_INET) { |
| 2688 | char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN]; |
| 2689 | if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sockname)->sin_addr, |
| 2690 | sn, sizeof(sn)) && |
| 2691 | inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 2692 | pn, sizeof(pn))) { |
| 2693 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 2694 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port), |
| 2695 | sn, ntohs(((struct sockaddr_in *)&sockname)->sin_port), |
| 2696 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 2697 | } |
| 2698 | } |
| 2699 | else { |
| 2700 | char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN]; |
| 2701 | if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sockname)->sin6_addr, |
| 2702 | sn, sizeof(sn)) && |
| 2703 | inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr, |
| 2704 | pn, sizeof(pn))) { |
| 2705 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 2706 | pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 2707 | sn, ntohs(((struct sockaddr_in6 *)&sockname)->sin6_port), |
| 2708 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 2709 | } |
| 2710 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2711 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2712 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 2713 | 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] | 2714 | struct sockaddr_in sockname; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2715 | socklen_t namelen = sizeof(sockname); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2716 | int len; |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2717 | if (addr.ss_family != AF_INET || |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2718 | !(s->proxy->options & PR_O_TRANSP) || |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2719 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2720 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2721 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 2722 | if (s->cli_addr.ss_family == AF_INET) { |
| 2723 | char pn[INET_ADDRSTRLEN]; |
| 2724 | inet_ntop(AF_INET, |
| 2725 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 2726 | pn, sizeof(pn)); |
| 2727 | |
| 2728 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 2729 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 2730 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port)); |
| 2731 | } |
| 2732 | else { |
| 2733 | char pn[INET6_ADDRSTRLEN]; |
| 2734 | inet_ntop(AF_INET6, |
| 2735 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 2736 | pn, sizeof(pn)); |
| 2737 | |
| 2738 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 2739 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 2740 | pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port)); |
| 2741 | } |
| 2742 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2743 | write(1, trash, len); |
| 2744 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2745 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2746 | if ((s->req = pool_alloc(buffer)) == NULL) { /* no memory */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2747 | if (s->rsp_cap != NULL) |
| 2748 | pool_free_to(p->rsp_cap_pool, s->rsp_cap); |
| 2749 | if (s->req_cap != NULL) |
| 2750 | pool_free_to(p->req_cap_pool, s->req_cap); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2751 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2752 | pool_free(task, t); |
| 2753 | pool_free(session, s); |
| 2754 | return 0; |
| 2755 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2756 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2757 | s->req->l = 0; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2758 | s->req->total = 0; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2759 | s->req->h = s->req->r = s->req->lr = s->req->w = s->req->data; /* r and w will be reset further */ |
| 2760 | s->req->rlim = s->req->data + BUFSIZE; |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2761 | if (s->cli_state == CL_STHEADERS) /* reserve some space for header rewriting */ |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2762 | s->req->rlim -= MAXREWRITE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2763 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2764 | if ((s->rep = pool_alloc(buffer)) == NULL) { /* no memory */ |
| 2765 | pool_free(buffer, s->req); |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 2766 | if (s->rsp_cap != NULL) |
| 2767 | pool_free_to(p->rsp_cap_pool, s->rsp_cap); |
| 2768 | if (s->req_cap != NULL) |
| 2769 | pool_free_to(p->req_cap_pool, s->req_cap); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2770 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2771 | pool_free(task, t); |
| 2772 | pool_free(session, s); |
| 2773 | return 0; |
| 2774 | } |
| 2775 | s->rep->l = 0; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2776 | s->rep->total = 0; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2777 | 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] | 2778 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2779 | fdtab[cfd].read = &event_cli_read; |
| 2780 | fdtab[cfd].write = &event_cli_write; |
| 2781 | fdtab[cfd].owner = t; |
| 2782 | fdtab[cfd].state = FD_STREADY; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2783 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2784 | if ((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) || |
| 2785 | (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK))) |
| 2786 | /* Either we got a request from a monitoring system on an HTTP instance, |
| 2787 | * or we're in health check mode with the 'httpchk' option enabled. In |
| 2788 | * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit. |
| 2789 | */ |
| 2790 | client_retnclose(s, 19, "HTTP/1.0 200 OK\r\n\r\n"); /* forge a 200 response */ |
| 2791 | else if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */ |
| 2792 | client_retnclose(s, 3, "OK\n"); /* forge an "OK" response */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2793 | } |
| 2794 | else { |
| 2795 | FD_SET(cfd, StaticReadEvent); |
| 2796 | } |
| 2797 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2798 | #if defined(DEBUG_FULL) && defined(ENABLE_EPOLL) |
| 2799 | if (PrevReadEvent) { |
| 2800 | assert(!(FD_ISSET(cfd, PrevReadEvent))); |
| 2801 | assert(!(FD_ISSET(cfd, PrevWriteEvent))); |
| 2802 | } |
| 2803 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2804 | fd_insert(cfd); |
| 2805 | |
| 2806 | tv_eternity(&s->cnexpire); |
| 2807 | tv_eternity(&s->srexpire); |
| 2808 | tv_eternity(&s->swexpire); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2809 | tv_eternity(&s->crexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2810 | tv_eternity(&s->cwexpire); |
| 2811 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2812 | if (s->proxy->clitimeout) { |
| 2813 | if (FD_ISSET(cfd, StaticReadEvent)) |
| 2814 | tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout); |
| 2815 | if (FD_ISSET(cfd, StaticWriteEvent)) |
| 2816 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
| 2817 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2818 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 2819 | tv_min(&t->expire, &s->crexpire, &s->cwexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2820 | |
| 2821 | task_queue(t); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2822 | |
| 2823 | if (p->mode != PR_MODE_HEALTH) |
| 2824 | task_wakeup(&rq, t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2825 | |
| 2826 | p->nbconn++; |
| 2827 | actconn++; |
| 2828 | totalconn++; |
| 2829 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2830 | // 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] | 2831 | } /* end of while (p->nbconn < p->maxconn) */ |
| 2832 | return 0; |
| 2833 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2834 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2835 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2836 | /* |
| 2837 | * This function is used only for server health-checks. It handles |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2838 | * the connection acknowledgement. If the proxy requires HTTP health-checks, |
| 2839 | * 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] | 2840 | * or -1 if an error occured. |
| 2841 | */ |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2842 | int event_srv_chk_w(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2843 | struct task *t = fdtab[fd].owner; |
| 2844 | struct server *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2845 | |
willy tarreau | c5f73ed | 2005-12-18 01:26:38 +0100 | [diff] [blame] | 2846 | int skerr; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2847 | socklen_t lskerr = sizeof(skerr); |
| 2848 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2849 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2850 | /* in case of TCP only, this tells us if the connection succeeded */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2851 | if (skerr) |
| 2852 | s->result = -1; |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 2853 | else if (s->result != -1) { |
| 2854 | /* 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] | 2855 | if (s->proxy->options & PR_O_HTTP_CHK) { |
| 2856 | int ret; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2857 | /* 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] | 2858 | * so we'll send the request, and won't wake the checker up now. |
| 2859 | */ |
| 2860 | #ifndef MSG_NOSIGNAL |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2861 | 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] | 2862 | #else |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 2863 | 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] | 2864 | #endif |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 2865 | if (ret == s->proxy->check_len) { |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2866 | FD_SET(fd, StaticReadEvent); /* prepare for reading reply */ |
| 2867 | FD_CLR(fd, StaticWriteEvent); /* nothing more to write */ |
| 2868 | return 0; |
| 2869 | } |
| 2870 | else |
| 2871 | s->result = -1; |
| 2872 | } |
| 2873 | else { |
| 2874 | /* good TCP connection is enough */ |
| 2875 | s->result = 1; |
| 2876 | } |
| 2877 | } |
| 2878 | |
| 2879 | task_wakeup(&rq, t); |
| 2880 | return 0; |
| 2881 | } |
| 2882 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2883 | |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2884 | /* |
| 2885 | * This function is used only for server health-checks. It handles |
| 2886 | * the server's reply to an HTTP request. It returns 1 if the server replies |
| 2887 | * 2xx or 3xx (valid responses), or -1 in other cases. |
| 2888 | */ |
| 2889 | int event_srv_chk_r(int fd) { |
| 2890 | char reply[64]; |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 2891 | int len, result; |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2892 | struct task *t = fdtab[fd].owner; |
| 2893 | struct server *s = t->context; |
| 2894 | |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 2895 | result = len = -1; |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2896 | #ifndef MSG_NOSIGNAL |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 2897 | { |
| 2898 | int skerr; |
| 2899 | socklen_t lskerr = sizeof(skerr); |
| 2900 | |
| 2901 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2902 | if (!skerr) |
| 2903 | len = recv(fd, reply, sizeof(reply), 0); |
| 2904 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2905 | #else |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2906 | /* Warning! Linux returns EAGAIN on SO_ERROR if data are still available |
| 2907 | * but the connection was closed on the remote end. Fortunately, recv still |
| 2908 | * works correctly and we don't need to do the getsockopt() on linux. |
| 2909 | */ |
| 2910 | len = recv(fd, reply, sizeof(reply), MSG_NOSIGNAL); |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2911 | #endif |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 2912 | if ((len >= sizeof("HTTP/1.0 000")) && |
| 2913 | !memcmp(reply, "HTTP/1.", 7) && |
| 2914 | (reply[9] == '2' || reply[9] == '3')) /* 2xx or 3xx */ |
willy tarreau | a4a583a | 2005-12-18 01:39:19 +0100 | [diff] [blame] | 2915 | result = 1; |
| 2916 | |
| 2917 | if (s->result != -1) |
| 2918 | s->result = result; |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2919 | |
| 2920 | FD_CLR(fd, StaticReadEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2921 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2922 | return 0; |
| 2923 | } |
| 2924 | |
| 2925 | |
| 2926 | /* |
| 2927 | * this function writes the string <str> at position <pos> which must be in buffer <b>, |
| 2928 | * and moves <end> just after the end of <str>. |
| 2929 | * <b>'s parameters (l, r, w, h, lr) are recomputed to be valid after the shift. |
| 2930 | * the shift value (positive or negative) is returned. |
| 2931 | * If there's no space left, the move is not done. |
| 2932 | * |
| 2933 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2934 | int buffer_replace(struct buffer *b, char *pos, char *end, char *str) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2935 | int delta; |
| 2936 | int len; |
| 2937 | |
| 2938 | len = strlen(str); |
| 2939 | delta = len - (end - pos); |
| 2940 | |
| 2941 | if (delta + b->r >= b->data + BUFSIZE) |
| 2942 | return 0; /* no space left */ |
| 2943 | |
| 2944 | /* first, protect the end of the buffer */ |
| 2945 | memmove(end + delta, end, b->data + b->l - end); |
| 2946 | |
| 2947 | /* now, copy str over pos */ |
| 2948 | memcpy(pos, str,len); |
| 2949 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2950 | /* we only move data after the displaced zone */ |
| 2951 | if (b->r > pos) b->r += delta; |
| 2952 | if (b->w > pos) b->w += delta; |
| 2953 | if (b->h > pos) b->h += delta; |
| 2954 | if (b->lr > pos) b->lr += delta; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2955 | b->l += delta; |
| 2956 | |
| 2957 | return delta; |
| 2958 | } |
| 2959 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2960 | /* 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] | 2961 | * len is 0. |
| 2962 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2963 | 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] | 2964 | int delta; |
| 2965 | |
| 2966 | delta = len - (end - pos); |
| 2967 | |
| 2968 | if (delta + b->r >= b->data + BUFSIZE) |
| 2969 | return 0; /* no space left */ |
| 2970 | |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 2971 | if (b->data + b->l < end) |
| 2972 | /* The data has been stolen, we could have crashed. Maybe we should abort() ? */ |
| 2973 | return 0; |
| 2974 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2975 | /* first, protect the end of the buffer */ |
| 2976 | memmove(end + delta, end, b->data + b->l - end); |
| 2977 | |
| 2978 | /* now, copy str over pos */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2979 | if (len) |
| 2980 | memcpy(pos, str, len); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2981 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2982 | /* we only move data after the displaced zone */ |
| 2983 | if (b->r > pos) b->r += delta; |
| 2984 | if (b->w > pos) b->w += delta; |
| 2985 | if (b->h > pos) b->h += delta; |
| 2986 | if (b->lr > pos) b->lr += delta; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2987 | b->l += delta; |
| 2988 | |
| 2989 | return delta; |
| 2990 | } |
| 2991 | |
| 2992 | |
| 2993 | int exp_replace(char *dst, char *src, char *str, regmatch_t *matches) { |
| 2994 | char *old_dst = dst; |
| 2995 | |
| 2996 | while (*str) { |
| 2997 | if (*str == '\\') { |
| 2998 | str++; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 2999 | if (isdigit((int)*str)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3000 | int len, num; |
| 3001 | |
| 3002 | num = *str - '0'; |
| 3003 | str++; |
| 3004 | |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3005 | if (matches[num].rm_eo > -1 && matches[num].rm_so > -1) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3006 | len = matches[num].rm_eo - matches[num].rm_so; |
| 3007 | memcpy(dst, src + matches[num].rm_so, len); |
| 3008 | dst += len; |
| 3009 | } |
| 3010 | |
| 3011 | } |
| 3012 | else if (*str == 'x') { |
| 3013 | unsigned char hex1, hex2; |
| 3014 | str++; |
| 3015 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 3016 | hex1 = toupper(*str++) - '0'; |
| 3017 | hex2 = toupper(*str++) - '0'; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3018 | |
| 3019 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 3020 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 3021 | *dst++ = (hex1<<4) + hex2; |
| 3022 | } |
| 3023 | else |
| 3024 | *dst++ = *str++; |
| 3025 | } |
| 3026 | else |
| 3027 | *dst++ = *str++; |
| 3028 | } |
| 3029 | *dst = 0; |
| 3030 | return dst - old_dst; |
| 3031 | } |
| 3032 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 3033 | static int ishex(char s) |
| 3034 | { |
| 3035 | return (s >= '0' && s <= '9') || (s >= 'A' && s <= 'F') || (s >= 'a' && s <= 'f'); |
| 3036 | } |
| 3037 | |
| 3038 | /* returns NULL if the replacement string <str> is valid, or the pointer to the first error */ |
| 3039 | char *check_replace_string(char *str) |
| 3040 | { |
| 3041 | char *err = NULL; |
| 3042 | while (*str) { |
| 3043 | if (*str == '\\') { |
| 3044 | err = str; /* in case of a backslash, we return the pointer to it */ |
| 3045 | str++; |
| 3046 | if (!*str) |
| 3047 | return err; |
| 3048 | else if (isdigit((int)*str)) |
| 3049 | err = NULL; |
| 3050 | else if (*str == 'x') { |
| 3051 | str++; |
| 3052 | if (!ishex(*str)) |
| 3053 | return err; |
| 3054 | str++; |
| 3055 | if (!ishex(*str)) |
| 3056 | return err; |
| 3057 | err = NULL; |
| 3058 | } |
| 3059 | else { |
| 3060 | Warning("'\\%c' : deprecated use of a backslash before something not '\\','x' or a digit.\n", *str); |
| 3061 | err = NULL; |
| 3062 | } |
| 3063 | } |
| 3064 | str++; |
| 3065 | } |
| 3066 | return err; |
| 3067 | } |
| 3068 | |
| 3069 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3070 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3071 | /* |
| 3072 | * manages the client FSM and its socket. BTW, it also tries to handle the |
| 3073 | * cookie. It returns 1 if a state has changed (and a resync may be needed), |
| 3074 | * 0 else. |
| 3075 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3076 | int process_cli(struct session *t) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3077 | int s = t->srv_state; |
| 3078 | int c = t->cli_state; |
| 3079 | struct buffer *req = t->req; |
| 3080 | struct buffer *rep = t->rep; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3081 | int method_checked = 0; |
| 3082 | appsess *asession_temp = NULL; |
| 3083 | appsess local_asession; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3084 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3085 | #ifdef DEBUG_FULL |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3086 | fprintf(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n", |
| 3087 | cli_stnames[c], srv_stnames[s], |
| 3088 | FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 3089 | t->crexpire.tv_sec, t->crexpire.tv_usec, |
| 3090 | t->cwexpire.tv_sec, t->cwexpire.tv_usec); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3091 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3092 | //fprintf(stderr,"process_cli: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s, |
| 3093 | //FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 3094 | //FD_ISSET(t->srv_fd, StaticReadEvent), FD_ISSET(t->srv_fd, StaticWriteEvent) |
| 3095 | //); |
| 3096 | if (c == CL_STHEADERS) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3097 | /* now parse the partial (or complete) headers */ |
| 3098 | while (req->lr < req->r) { /* this loop only sees one header at each iteration */ |
| 3099 | char *ptr; |
| 3100 | int delete_header; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3101 | char *request_line = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3102 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3103 | ptr = req->lr; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3104 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3105 | /* look for the end of the current header */ |
| 3106 | while (ptr < req->r && *ptr != '\n' && *ptr != '\r') |
| 3107 | ptr++; |
| 3108 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3109 | if (ptr == req->h) { /* empty line, end of headers */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3110 | int line, len; |
| 3111 | /* we can only get here after an end of headers */ |
| 3112 | /* we'll have something else to do here : add new headers ... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3113 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3114 | if (t->flags & SN_CLDENY) { |
| 3115 | /* no need to go further */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3116 | t->logs.status = 403; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3117 | client_retnclose(t, t->proxy->errmsg.len403, t->proxy->errmsg.msg403); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3118 | if (!(t->flags & SN_ERR_MASK)) |
| 3119 | t->flags |= SN_ERR_PRXCOND; |
| 3120 | if (!(t->flags & SN_FINST_MASK)) |
| 3121 | t->flags |= SN_FINST_R; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3122 | return 1; |
| 3123 | } |
| 3124 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3125 | for (line = 0; line < t->proxy->nb_reqadd; line++) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3126 | len = sprintf(trash, "%s\r\n", t->proxy->req_add[line]); |
| 3127 | buffer_replace2(req, req->h, req->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3128 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3129 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3130 | if (t->proxy->options & PR_O_FWDFOR) { |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 3131 | if (t->cli_addr.ss_family == AF_INET) { |
| 3132 | unsigned char *pn; |
| 3133 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
| 3134 | len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d\r\n", |
| 3135 | pn[0], pn[1], pn[2], pn[3]); |
| 3136 | buffer_replace2(req, req->h, req->h, trash, len); |
| 3137 | } |
| 3138 | else if (t->cli_addr.ss_family == AF_INET6) { |
| 3139 | char pn[INET6_ADDRSTRLEN]; |
| 3140 | inet_ntop(AF_INET6, |
| 3141 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 3142 | pn, sizeof(pn)); |
| 3143 | len = sprintf(trash, "X-Forwarded-For: %s\r\n", pn); |
| 3144 | buffer_replace2(req, req->h, req->h, trash, len); |
| 3145 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3146 | } |
| 3147 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 3148 | /* add a "connection: close" line if needed */ |
| 3149 | if (t->proxy->options & PR_O_HTTP_CLOSE) |
| 3150 | buffer_replace2(req, req->h, req->h, "Connection: close\r\n", 19); |
| 3151 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 3152 | if (!memcmp(req->data, "POST ", 5)) { |
| 3153 | /* this is a POST request, which is not cacheable by default */ |
| 3154 | t->flags |= SN_POST; |
| 3155 | } |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 3156 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3157 | t->cli_state = CL_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3158 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3159 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3160 | t->logs.t_request = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3161 | /* FIXME: we'll set the client in a wait state while we try to |
| 3162 | * connect to the server. Is this really needed ? wouldn't it be |
| 3163 | * better to release the maximum of system buffers instead ? */ |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3164 | //FD_CLR(t->cli_fd, StaticReadEvent); |
| 3165 | //tv_eternity(&t->crexpire); |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 3166 | |
| 3167 | /* FIXME: if we break here (as up to 1.1.23), having the client |
| 3168 | * shutdown its connection can lead to an abort further. |
| 3169 | * it's better to either return 1 or even jump directly to the |
| 3170 | * data state which will save one schedule. |
| 3171 | */ |
| 3172 | //break; |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 3173 | |
| 3174 | if (!t->proxy->clitimeout || |
| 3175 | (t->srv_state < SV_STDATA && t->proxy->srvtimeout)) |
| 3176 | /* If the client has no timeout, or if the server is not ready yet, |
| 3177 | * and we know for sure that it can expire, then it's cleaner to |
| 3178 | * disable the timeout on the client side so that too low values |
| 3179 | * cannot make the sessions abort too early. |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3180 | * |
| 3181 | * FIXME-20050705: the server needs a way to re-enable this time-out |
| 3182 | * when it switches its state, otherwise a client can stay connected |
| 3183 | * indefinitely. This now seems to be OK. |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 3184 | */ |
| 3185 | tv_eternity(&t->crexpire); |
| 3186 | |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 3187 | goto process_data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3188 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3189 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3190 | /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */ |
| 3191 | if (ptr > req->r - 2) { |
| 3192 | /* this is a partial header, let's wait for more to come */ |
| 3193 | req->lr = ptr; |
| 3194 | break; |
| 3195 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3196 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3197 | /* now we know that *ptr is either \r or \n, |
| 3198 | * and that there are at least 1 char after it. |
| 3199 | */ |
| 3200 | if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n')) |
| 3201 | req->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */ |
| 3202 | else |
| 3203 | req->lr = ptr + 2; /* \r\n or \n\r */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3204 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3205 | /* |
| 3206 | * now we know that we have a full header ; we can do whatever |
| 3207 | * we want with these pointers : |
| 3208 | * req->h = beginning of header |
| 3209 | * ptr = end of header (first \r or \n) |
| 3210 | * req->lr = beginning of next line (next rep->h) |
| 3211 | * req->r = end of data (not used at this stage) |
| 3212 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3213 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3214 | if (!method_checked && (t->proxy->appsession_name != NULL) && |
| 3215 | ((memcmp(req->h, "GET ", 4) == 0) || (memcmp(req->h, "POST ", 4) == 0)) && |
| 3216 | ((request_line = memchr(req->h, ';', req->lr - req->h)) != NULL)) { |
| 3217 | |
| 3218 | /* skip ; */ |
| 3219 | request_line++; |
| 3220 | |
| 3221 | /* look if we have a jsessionid */ |
| 3222 | |
| 3223 | if (strncasecmp(request_line, t->proxy->appsession_name, t->proxy->appsession_name_len) == 0) { |
| 3224 | |
| 3225 | /* skip jsessionid= */ |
| 3226 | request_line += t->proxy->appsession_name_len + 1; |
| 3227 | |
| 3228 | /* First try if we allready have an appsession */ |
| 3229 | asession_temp = &local_asession; |
| 3230 | |
| 3231 | if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) { |
| 3232 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 3233 | send_log(t->proxy, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 3234 | return 0; |
| 3235 | } |
| 3236 | |
| 3237 | /* Copy the sessionid */ |
| 3238 | memcpy(asession_temp->sessid, request_line, t->proxy->appsession_len); |
| 3239 | asession_temp->sessid[t->proxy->appsession_len] = 0; |
| 3240 | asession_temp->serverid = NULL; |
| 3241 | |
| 3242 | /* only do insert, if lookup fails */ |
| 3243 | if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *)&asession_temp)) { |
| 3244 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 3245 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 3246 | send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 3247 | return 0; |
| 3248 | } |
| 3249 | asession_temp->sessid = local_asession.sessid; |
| 3250 | asession_temp->serverid = local_asession.serverid; |
| 3251 | chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3252 | } /* end if (chtbl_lookup()) */ |
| 3253 | else { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3254 | /*free wasted memory;*/ |
| 3255 | pool_free_to(apools.sessid, local_asession.sessid); |
| 3256 | } |
| 3257 | |
| 3258 | tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout); |
| 3259 | asession_temp->request_count++; |
| 3260 | |
| 3261 | #if defined(DEBUG_HASH) |
| 3262 | print_table(&(t->proxy->htbl_proxy)); |
| 3263 | #endif |
| 3264 | |
| 3265 | if (asession_temp->serverid == NULL) { |
| 3266 | Alert("Found Application Session without matching server.\n"); |
| 3267 | } else { |
| 3268 | struct server *srv = t->proxy->srv; |
| 3269 | while (srv) { |
| 3270 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
| 3271 | if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) { |
| 3272 | /* we found the server and it's usable */ |
| 3273 | t->flags &= ~SN_CK_MASK; |
| 3274 | t->flags |= SN_CK_VALID | SN_DIRECT; |
| 3275 | t->srv = srv; |
| 3276 | break; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3277 | } else { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3278 | t->flags &= ~SN_CK_MASK; |
| 3279 | t->flags |= SN_CK_DOWN; |
| 3280 | } |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3281 | } /* end if (strcmp()) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3282 | srv = srv->next; |
| 3283 | }/* end while(srv) */ |
| 3284 | }/* end else of if (asession_temp->serverid == NULL) */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3285 | }/* 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] | 3286 | else { |
| 3287 | //fprintf(stderr,">>>>>>>>>>>>>>>>>>>>>>NO SESSION\n"); |
| 3288 | } |
willy tarreau | 598da41 | 2005-12-18 01:07:29 +0100 | [diff] [blame] | 3289 | method_checked = 1; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3290 | } /* end if (!method_checked ...) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3291 | else{ |
| 3292 | //printf("No Methode-Header with Session-String\n"); |
| 3293 | } |
| 3294 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3295 | if (t->logs.logwait & LW_REQ) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3296 | /* we have a complete HTTP request that we must log */ |
| 3297 | int urilen; |
| 3298 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3299 | if ((t->logs.uri = pool_alloc(requri)) == NULL) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3300 | Alert("HTTP logging : out of memory.\n"); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3301 | t->logs.status = 500; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3302 | client_retnclose(t, t->proxy->errmsg.len500, t->proxy->errmsg.msg500); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3303 | if (!(t->flags & SN_ERR_MASK)) |
| 3304 | t->flags |= SN_ERR_PRXCOND; |
| 3305 | if (!(t->flags & SN_FINST_MASK)) |
| 3306 | t->flags |= SN_FINST_R; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3307 | return 1; |
| 3308 | } |
| 3309 | |
| 3310 | urilen = ptr - req->h; |
| 3311 | if (urilen >= REQURI_LEN) |
| 3312 | urilen = REQURI_LEN - 1; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3313 | memcpy(t->logs.uri, req->h, urilen); |
| 3314 | t->logs.uri[urilen] = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3315 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3316 | if (!(t->logs.logwait &= ~LW_REQ)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3317 | sess_log(t); |
| 3318 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 3319 | else if (t->logs.logwait & LW_REQHDR) { |
| 3320 | struct cap_hdr *h; |
| 3321 | int len; |
| 3322 | for (h = t->proxy->req_cap; h; h = h->next) { |
| 3323 | if ((h->namelen + 2 <= ptr - req->h) && |
| 3324 | (req->h[h->namelen] == ':') && |
| 3325 | (strncasecmp(req->h, h->name, h->namelen) == 0)) { |
| 3326 | |
| 3327 | if (t->req_cap[h->index] == NULL) |
| 3328 | t->req_cap[h->index] = pool_alloc_from(h->pool, h->len + 1); |
| 3329 | |
| 3330 | len = ptr - (req->h + h->namelen + 2); |
| 3331 | if (len > h->len) |
| 3332 | len = h->len; |
| 3333 | |
| 3334 | memcpy(t->req_cap[h->index], req->h + h->namelen + 2, len); |
| 3335 | t->req_cap[h->index][len]=0; |
| 3336 | } |
| 3337 | } |
| 3338 | |
| 3339 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3340 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3341 | delete_header = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3342 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 3343 | 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] | 3344 | int len, max; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 3345 | 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] | 3346 | max = ptr - req->h; |
| 3347 | UBOUND(max, sizeof(trash) - len - 1); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3348 | len += strlcpy2(trash + len, req->h, max + 1); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3349 | trash[len++] = '\n'; |
| 3350 | write(1, trash, len); |
| 3351 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3352 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 3353 | |
| 3354 | /* remove "connection: " if needed */ |
| 3355 | if (!delete_header && (t->proxy->options & PR_O_HTTP_CLOSE) |
| 3356 | && (strncasecmp(req->h, "Connection: ", 12) == 0)) { |
| 3357 | delete_header = 1; |
| 3358 | } |
| 3359 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3360 | /* try headers regexps */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 3361 | if (!delete_header && t->proxy->req_exp != NULL |
| 3362 | && !(t->flags & SN_CLDENY)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3363 | struct hdr_exp *exp; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3364 | char term; |
| 3365 | |
| 3366 | term = *ptr; |
| 3367 | *ptr = '\0'; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3368 | exp = t->proxy->req_exp; |
| 3369 | do { |
| 3370 | if (regexec(exp->preg, req->h, MAX_MATCH, pmatch, 0) == 0) { |
| 3371 | switch (exp->action) { |
| 3372 | case ACT_ALLOW: |
| 3373 | if (!(t->flags & SN_CLDENY)) |
| 3374 | t->flags |= SN_CLALLOW; |
| 3375 | break; |
| 3376 | case ACT_REPLACE: |
| 3377 | if (!(t->flags & SN_CLDENY)) { |
| 3378 | int len = exp_replace(trash, req->h, exp->replace, pmatch); |
| 3379 | ptr += buffer_replace2(req, req->h, ptr, trash, len); |
| 3380 | } |
| 3381 | break; |
| 3382 | case ACT_REMOVE: |
| 3383 | if (!(t->flags & SN_CLDENY)) |
| 3384 | delete_header = 1; |
| 3385 | break; |
| 3386 | case ACT_DENY: |
| 3387 | if (!(t->flags & SN_CLALLOW)) |
| 3388 | t->flags |= SN_CLDENY; |
| 3389 | break; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3390 | case ACT_PASS: /* we simply don't deny this one */ |
| 3391 | break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3392 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3393 | break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3394 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3395 | } while ((exp = exp->next) != NULL); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3396 | *ptr = term; /* restore the string terminator */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3397 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3398 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3399 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3400 | * attributes whose name begin with a '$', and associate them with |
| 3401 | * the right cookie, if we want to delete this cookie. |
| 3402 | * So there are 3 cases for each cookie read : |
| 3403 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3404 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3405 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3406 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3407 | * "special" cookie. |
| 3408 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3409 | * remove it. If no application cookie persists in the header, we |
| 3410 | * *MUST* delete it |
| 3411 | */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3412 | if (!delete_header && |
| 3413 | (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] | 3414 | && !(t->flags & SN_CLDENY) && (ptr >= req->h + 8) |
willy tarreau | 906b268 | 2005-12-17 13:49:52 +0100 | [diff] [blame] | 3415 | && (strncasecmp(req->h, "Cookie: ", 8) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3416 | char *p1, *p2, *p3, *p4; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3417 | char *del_colon, *del_cookie, *colon; |
| 3418 | int app_cookies; |
| 3419 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3420 | p1 = req->h + 8; /* first char after 'Cookie: ' */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3421 | colon = p1; |
| 3422 | /* del_cookie == NULL => nothing to be deleted */ |
| 3423 | del_colon = del_cookie = NULL; |
| 3424 | app_cookies = 0; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3425 | |
| 3426 | while (p1 < ptr) { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3427 | /* skip spaces and colons, but keep an eye on these ones */ |
| 3428 | while (p1 < ptr) { |
| 3429 | if (*p1 == ';' || *p1 == ',') |
| 3430 | colon = p1; |
| 3431 | else if (!isspace((int)*p1)) |
| 3432 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3433 | p1++; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3434 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3435 | |
| 3436 | if (p1 == ptr) |
| 3437 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3438 | |
| 3439 | /* p1 is at the beginning of the cookie name */ |
| 3440 | p2 = p1; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3441 | while (p2 < ptr && *p2 != '=') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3442 | p2++; |
| 3443 | |
| 3444 | if (p2 == ptr) |
| 3445 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3446 | |
| 3447 | p3 = p2 + 1; /* skips the '=' sign */ |
| 3448 | if (p3 == ptr) |
| 3449 | break; |
| 3450 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3451 | p4 = p3; |
| 3452 | while (p4 < ptr && !isspace((int)*p4) && *p4 != ';' && *p4 != ',') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3453 | p4++; |
| 3454 | |
| 3455 | /* here, we have the cookie name between p1 and p2, |
| 3456 | * and its value between p3 and p4. |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3457 | * we can process it : |
| 3458 | * |
| 3459 | * Cookie: NAME=VALUE; |
| 3460 | * | || || | |
| 3461 | * | || || +--> p4 |
| 3462 | * | || |+-------> p3 |
| 3463 | * | || +--------> p2 |
| 3464 | * | |+------------> p1 |
| 3465 | * | +-------------> colon |
| 3466 | * +--------------------> req->h |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3467 | */ |
| 3468 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3469 | if (*p1 == '$') { |
| 3470 | /* skip this one */ |
| 3471 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3472 | else { |
| 3473 | /* first, let's see if we want to capture it */ |
| 3474 | if (t->proxy->capture_name != NULL && |
| 3475 | t->logs.cli_cookie == NULL && |
| 3476 | (p4 - p1 >= t->proxy->capture_namelen) && |
| 3477 | memcmp(p1, t->proxy->capture_name, t->proxy->capture_namelen) == 0) { |
| 3478 | int log_len = p4 - p1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3479 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3480 | if ((t->logs.cli_cookie = pool_alloc(capture)) == NULL) { |
| 3481 | Alert("HTTP logging : out of memory.\n"); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3482 | } else { |
| 3483 | if (log_len > t->proxy->capture_len) |
| 3484 | log_len = t->proxy->capture_len; |
| 3485 | memcpy(t->logs.cli_cookie, p1, log_len); |
| 3486 | t->logs.cli_cookie[log_len] = 0; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3487 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3488 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3489 | |
| 3490 | if ((p2 - p1 == t->proxy->cookie_len) && (t->proxy->cookie_name != NULL) && |
| 3491 | (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) { |
| 3492 | /* Cool... it's the right one */ |
| 3493 | struct server *srv = t->proxy->srv; |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3494 | char *delim; |
| 3495 | |
| 3496 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 3497 | * have the server ID betweek p3 and delim, and the original cookie between |
| 3498 | * delim+1 and p4. Otherwise, delim==p4 : |
| 3499 | * |
| 3500 | * Cookie: NAME=SRV~VALUE; |
| 3501 | * | || || | | |
| 3502 | * | || || | +--> p4 |
| 3503 | * | || || +--------> delim |
| 3504 | * | || |+-----------> p3 |
| 3505 | * | || +------------> p2 |
| 3506 | * | |+----------------> p1 |
| 3507 | * | +-----------------> colon |
| 3508 | * +------------------------> req->h |
| 3509 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3510 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3511 | if (t->proxy->options & PR_O_COOK_PFX) { |
| 3512 | for (delim = p3; delim < p4; delim++) |
| 3513 | if (*delim == COOKIE_DELIM) |
| 3514 | break; |
| 3515 | } |
| 3516 | else |
| 3517 | delim = p4; |
| 3518 | |
| 3519 | |
| 3520 | /* Here, we'll look for the first running server which supports the cookie. |
| 3521 | * This allows to share a same cookie between several servers, for example |
| 3522 | * to dedicate backup servers to specific servers only. |
| 3523 | */ |
| 3524 | while (srv) { |
| 3525 | if ((srv->cklen == delim - p3) && !memcmp(p3, srv->cookie, delim - p3)) { |
| 3526 | if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) { |
| 3527 | /* we found the server and it's usable */ |
| 3528 | t->flags &= ~SN_CK_MASK; |
| 3529 | t->flags |= SN_CK_VALID | SN_DIRECT; |
| 3530 | t->srv = srv; |
| 3531 | break; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3532 | } else { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3533 | /* we found a server, but it's down */ |
| 3534 | t->flags &= ~SN_CK_MASK; |
| 3535 | t->flags |= SN_CK_DOWN; |
| 3536 | } |
| 3537 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3538 | srv = srv->next; |
| 3539 | } |
| 3540 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3541 | if (!srv && !(t->flags & SN_CK_DOWN)) { |
| 3542 | /* no server matched this cookie */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3543 | t->flags &= ~SN_CK_MASK; |
| 3544 | t->flags |= SN_CK_INVALID; |
| 3545 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3546 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3547 | /* depending on the cookie mode, we may have to either : |
| 3548 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3549 | * the server never sees it ; |
| 3550 | * - remove the server id from the cookie value, and tag the cookie as an |
| 3551 | * application cookie so that it does not get accidentely removed later, |
| 3552 | * if we're in cookie prefix mode |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3553 | */ |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 3554 | if ((t->proxy->options & PR_O_COOK_PFX) && (delim != p4)) { |
| 3555 | buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 3556 | p4 -= (delim + 1 - p3); |
| 3557 | ptr -= (delim + 1 - p3); |
| 3558 | del_cookie = del_colon = NULL; |
| 3559 | app_cookies++; /* protect the header from deletion */ |
| 3560 | } |
| 3561 | else if (del_cookie == NULL && |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3562 | (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] | 3563 | del_cookie = p1; |
| 3564 | del_colon = colon; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3565 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3566 | } else { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3567 | /* now we know that we must keep this cookie since it's |
| 3568 | * not ours. But if we wanted to delete our cookie |
| 3569 | * earlier, we cannot remove the complete header, but we |
| 3570 | * can remove the previous block itself. |
| 3571 | */ |
| 3572 | app_cookies++; |
| 3573 | |
| 3574 | if (del_cookie != NULL) { |
| 3575 | buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 3576 | p4 -= (p1 - del_cookie); |
| 3577 | ptr -= (p1 - del_cookie); |
| 3578 | del_cookie = del_colon = NULL; |
| 3579 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3580 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3581 | |
| 3582 | if ((t->proxy->appsession_name != NULL) && |
| 3583 | (memcmp(p1, t->proxy->appsession_name, p2 - p1) == 0)) { |
| 3584 | /* first, let's see if the cookie is our appcookie*/ |
| 3585 | |
| 3586 | /* Cool... it's the right one */ |
| 3587 | |
| 3588 | asession_temp = &local_asession; |
| 3589 | |
| 3590 | if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) { |
| 3591 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3592 | send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3593 | return 0; |
| 3594 | } |
| 3595 | |
| 3596 | memcpy(asession_temp->sessid, p3, t->proxy->appsession_len); |
| 3597 | asession_temp->sessid[t->proxy->appsession_len] = 0; |
| 3598 | asession_temp->serverid = NULL; |
| 3599 | |
| 3600 | /* only do insert, if lookup fails */ |
| 3601 | if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *) &asession_temp) != 0) { |
| 3602 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 3603 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 3604 | send_log(t->proxy, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 3605 | return 0; |
| 3606 | } |
| 3607 | |
| 3608 | asession_temp->sessid = local_asession.sessid; |
| 3609 | asession_temp->serverid = local_asession.serverid; |
| 3610 | chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp); |
| 3611 | } |
| 3612 | else{ |
| 3613 | /* free wasted memory */ |
| 3614 | pool_free_to(apools.sessid, local_asession.sessid); |
| 3615 | } |
| 3616 | |
| 3617 | if (asession_temp->serverid == NULL) { |
| 3618 | Alert("Found Application Session without matching server.\n"); |
| 3619 | } else { |
| 3620 | struct server *srv = t->proxy->srv; |
| 3621 | while (srv) { |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3622 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3623 | if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) { |
| 3624 | /* we found the server and it's usable */ |
| 3625 | t->flags &= ~SN_CK_MASK; |
| 3626 | t->flags |= SN_CK_VALID | SN_DIRECT; |
| 3627 | t->srv = srv; |
| 3628 | break; |
| 3629 | } else { |
| 3630 | t->flags &= ~SN_CK_MASK; |
| 3631 | t->flags |= SN_CK_DOWN; |
| 3632 | } |
| 3633 | } |
| 3634 | srv = srv->next; |
| 3635 | }/* end while(srv) */ |
| 3636 | }/* end else if server == NULL */ |
| 3637 | |
| 3638 | tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3639 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3640 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3641 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3642 | /* we'll have to look for another cookie ... */ |
| 3643 | p1 = p4; |
| 3644 | } /* while (p1 < ptr) */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3645 | |
| 3646 | /* There's no more cookie on this line. |
| 3647 | * We may have marked the last one(s) for deletion. |
| 3648 | * We must do this now in two ways : |
| 3649 | * - if there is no app cookie, we simply delete the header ; |
| 3650 | * - if there are app cookies, we must delete the end of the |
| 3651 | * string properly, including the colon/semi-colon before |
| 3652 | * the cookie name. |
| 3653 | */ |
| 3654 | if (del_cookie != NULL) { |
| 3655 | if (app_cookies) { |
| 3656 | buffer_replace2(req, del_colon, ptr, NULL, 0); |
| 3657 | /* WARNING! <ptr> becomes invalid for now. If some code |
| 3658 | * below needs to rely on it before the end of the global |
| 3659 | * header loop, we need to correct it with this code : |
| 3660 | * ptr = del_colon; |
| 3661 | */ |
| 3662 | } |
| 3663 | else |
| 3664 | delete_header = 1; |
| 3665 | } |
| 3666 | } /* end of cookie processing on this header */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3667 | |
| 3668 | /* let's look if we have to delete this header */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3669 | if (delete_header && !(t->flags & SN_CLDENY)) { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3670 | buffer_replace2(req, req->h, req->lr, NULL, 0); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3671 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3672 | /* WARNING: ptr is not valid anymore, since the header may have been deleted or truncated ! */ |
| 3673 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3674 | req->h = req->lr; |
| 3675 | } /* while (req->lr < req->r) */ |
| 3676 | |
| 3677 | /* end of header processing (even if incomplete) */ |
| 3678 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3679 | if ((req->l < req->rlim - req->data) && ! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 3680 | /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer |
| 3681 | * full. We cannot loop here since event_cli_read will disable it only if |
| 3682 | * req->l == rlim-data |
| 3683 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3684 | FD_SET(t->cli_fd, StaticReadEvent); |
| 3685 | if (t->proxy->clitimeout) |
| 3686 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 3687 | else |
| 3688 | tv_eternity(&t->crexpire); |
| 3689 | } |
| 3690 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3691 | /* 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] | 3692 | * won't be able to free more later, so the session will never terminate. |
| 3693 | */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3694 | if (req->l >= req->rlim - req->data) { |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3695 | t->logs.status = 400; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3696 | client_retnclose(t, t->proxy->errmsg.len400, t->proxy->errmsg.msg400); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3697 | if (!(t->flags & SN_ERR_MASK)) |
| 3698 | t->flags |= SN_ERR_PRXCOND; |
| 3699 | if (!(t->flags & SN_FINST_MASK)) |
| 3700 | t->flags |= SN_FINST_R; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3701 | return 1; |
| 3702 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3703 | else if (t->res_cr == RES_ERROR || t->res_cr == RES_NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3704 | /* read error, or last read : give up. */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3705 | tv_eternity(&t->crexpire); |
| 3706 | fd_delete(t->cli_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3707 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3708 | if (!(t->flags & SN_ERR_MASK)) |
| 3709 | t->flags |= SN_ERR_CLICL; |
| 3710 | if (!(t->flags & SN_FINST_MASK)) |
| 3711 | t->flags |= SN_FINST_R; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3712 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3713 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3714 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 3715 | |
| 3716 | /* read timeout : give up with an error message. |
| 3717 | */ |
| 3718 | t->logs.status = 408; |
| 3719 | client_retnclose(t, t->proxy->errmsg.len408, t->proxy->errmsg.msg408); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3720 | if (!(t->flags & SN_ERR_MASK)) |
| 3721 | t->flags |= SN_ERR_CLITO; |
| 3722 | if (!(t->flags & SN_FINST_MASK)) |
| 3723 | t->flags |= SN_FINST_R; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3724 | return 1; |
| 3725 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3726 | |
| 3727 | return t->cli_state != CL_STHEADERS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3728 | } |
| 3729 | else if (c == CL_STDATA) { |
willy tarreau | 197e8ec | 2005-12-17 14:10:59 +0100 | [diff] [blame] | 3730 | process_data: |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 3731 | /* FIXME: this error handling is partly buggy because we always report |
| 3732 | * a 'DATA' phase while we don't know if the server was in IDLE, CONN |
| 3733 | * or HEADER phase. BTW, it's not logical to expire the client while |
| 3734 | * we're waiting for the server to connect. |
| 3735 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3736 | /* read or write error */ |
| 3737 | if (t->res_cw == RES_ERROR || t->res_cr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3738 | tv_eternity(&t->crexpire); |
| 3739 | tv_eternity(&t->cwexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3740 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3741 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3742 | if (!(t->flags & SN_ERR_MASK)) |
| 3743 | t->flags |= SN_ERR_CLICL; |
| 3744 | if (!(t->flags & SN_FINST_MASK)) |
| 3745 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3746 | return 1; |
| 3747 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3748 | /* last read, or end of server write */ |
| 3749 | 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] | 3750 | FD_CLR(t->cli_fd, StaticReadEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3751 | tv_eternity(&t->crexpire); |
| 3752 | shutdown(t->cli_fd, SHUT_RD); |
| 3753 | t->cli_state = CL_STSHUTR; |
| 3754 | return 1; |
| 3755 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3756 | /* last server read and buffer empty */ |
| 3757 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3758 | FD_CLR(t->cli_fd, StaticWriteEvent); |
| 3759 | tv_eternity(&t->cwexpire); |
| 3760 | shutdown(t->cli_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3761 | /* We must ensure that the read part is still alive when switching |
| 3762 | * to shutw */ |
| 3763 | FD_SET(t->cli_fd, StaticReadEvent); |
| 3764 | if (t->proxy->clitimeout) |
| 3765 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3766 | t->cli_state = CL_STSHUTW; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3767 | //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] | 3768 | return 1; |
| 3769 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3770 | /* read timeout */ |
| 3771 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 3772 | FD_CLR(t->cli_fd, StaticReadEvent); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3773 | tv_eternity(&t->crexpire); |
| 3774 | shutdown(t->cli_fd, SHUT_RD); |
| 3775 | t->cli_state = CL_STSHUTR; |
| 3776 | if (!(t->flags & SN_ERR_MASK)) |
| 3777 | t->flags |= SN_ERR_CLITO; |
| 3778 | if (!(t->flags & SN_FINST_MASK)) |
| 3779 | t->flags |= SN_FINST_D; |
| 3780 | return 1; |
| 3781 | } |
| 3782 | /* write timeout */ |
| 3783 | else if (tv_cmp2_ms(&t->cwexpire, &now) <= 0) { |
| 3784 | FD_CLR(t->cli_fd, StaticWriteEvent); |
| 3785 | tv_eternity(&t->cwexpire); |
| 3786 | shutdown(t->cli_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3787 | /* We must ensure that the read part is still alive when switching |
| 3788 | * to shutw */ |
| 3789 | FD_SET(t->cli_fd, StaticReadEvent); |
| 3790 | if (t->proxy->clitimeout) |
| 3791 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 3792 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3793 | t->cli_state = CL_STSHUTW; |
| 3794 | if (!(t->flags & SN_ERR_MASK)) |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3795 | t->flags |= SN_ERR_CLITO; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3796 | if (!(t->flags & SN_FINST_MASK)) |
| 3797 | t->flags |= SN_FINST_D; |
| 3798 | return 1; |
| 3799 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3800 | |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 3801 | if (req->l >= req->rlim - req->data) { |
| 3802 | /* no room to read more data */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3803 | if (FD_ISSET(t->cli_fd, StaticReadEvent)) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3804 | /* stop reading until we get some space */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3805 | FD_CLR(t->cli_fd, StaticReadEvent); |
| 3806 | tv_eternity(&t->crexpire); |
| 3807 | } |
| 3808 | } |
| 3809 | else { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3810 | /* there's still some space in the buffer */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3811 | if (! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 3812 | FD_SET(t->cli_fd, StaticReadEvent); |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 3813 | if (!t->proxy->clitimeout || |
| 3814 | (t->srv_state < SV_STDATA && t->proxy->srvtimeout)) |
| 3815 | /* If the client has no timeout, or if the server not ready yet, and we |
| 3816 | * know for sure that it can expire, then it's cleaner to disable the |
| 3817 | * timeout on the client side so that too low values cannot make the |
| 3818 | * sessions abort too early. |
| 3819 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3820 | tv_eternity(&t->crexpire); |
willy tarreau | c58fc69 | 2005-12-17 14:13:08 +0100 | [diff] [blame] | 3821 | else |
| 3822 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3823 | } |
| 3824 | } |
| 3825 | |
| 3826 | if ((rep->l == 0) || |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 3827 | ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3828 | if (FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 3829 | FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */ |
| 3830 | tv_eternity(&t->cwexpire); |
| 3831 | } |
| 3832 | } |
| 3833 | else { /* buffer not empty */ |
| 3834 | if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 3835 | FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3836 | if (t->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3837 | tv_delayfrom(&t->cwexpire, &now, t->proxy->clitimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3838 | /* FIXME: to avoid the client to read-time-out during writes, we refresh it */ |
| 3839 | t->crexpire = t->cwexpire; |
| 3840 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3841 | else |
| 3842 | tv_eternity(&t->cwexpire); |
| 3843 | } |
| 3844 | } |
| 3845 | return 0; /* other cases change nothing */ |
| 3846 | } |
| 3847 | else if (c == CL_STSHUTR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3848 | if (t->res_cw == RES_ERROR) { |
| 3849 | tv_eternity(&t->cwexpire); |
| 3850 | fd_delete(t->cli_fd); |
| 3851 | t->cli_state = CL_STCLOSE; |
| 3852 | if (!(t->flags & SN_ERR_MASK)) |
| 3853 | t->flags |= SN_ERR_CLICL; |
| 3854 | if (!(t->flags & SN_FINST_MASK)) |
| 3855 | t->flags |= SN_FINST_D; |
| 3856 | return 1; |
| 3857 | } |
| 3858 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3859 | tv_eternity(&t->cwexpire); |
| 3860 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3861 | t->cli_state = CL_STCLOSE; |
| 3862 | return 1; |
| 3863 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3864 | else if (tv_cmp2_ms(&t->cwexpire, &now) <= 0) { |
| 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_CLITO; |
| 3870 | if (!(t->flags & SN_FINST_MASK)) |
| 3871 | t->flags |= SN_FINST_D; |
| 3872 | return 1; |
| 3873 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3874 | else if ((rep->l == 0) || |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3875 | ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3876 | if (FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 3877 | FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */ |
| 3878 | tv_eternity(&t->cwexpire); |
| 3879 | } |
| 3880 | } |
| 3881 | else { /* buffer not empty */ |
| 3882 | if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 3883 | FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3884 | if (t->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3885 | tv_delayfrom(&t->cwexpire, &now, t->proxy->clitimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3886 | /* FIXME: to avoid the client to read-time-out during writes, we refresh it */ |
| 3887 | t->crexpire = t->cwexpire; |
| 3888 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3889 | else |
| 3890 | tv_eternity(&t->cwexpire); |
| 3891 | } |
| 3892 | } |
| 3893 | return 0; |
| 3894 | } |
| 3895 | else if (c == CL_STSHUTW) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3896 | if (t->res_cr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3897 | tv_eternity(&t->crexpire); |
| 3898 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3899 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3900 | if (!(t->flags & SN_ERR_MASK)) |
| 3901 | t->flags |= SN_ERR_CLICL; |
| 3902 | if (!(t->flags & SN_FINST_MASK)) |
| 3903 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3904 | return 1; |
| 3905 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3906 | else if (t->res_cr == RES_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
| 3907 | tv_eternity(&t->crexpire); |
| 3908 | fd_delete(t->cli_fd); |
| 3909 | t->cli_state = CL_STCLOSE; |
| 3910 | return 1; |
| 3911 | } |
| 3912 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 3913 | tv_eternity(&t->crexpire); |
| 3914 | fd_delete(t->cli_fd); |
| 3915 | t->cli_state = CL_STCLOSE; |
| 3916 | if (!(t->flags & SN_ERR_MASK)) |
| 3917 | t->flags |= SN_ERR_CLITO; |
| 3918 | if (!(t->flags & SN_FINST_MASK)) |
| 3919 | t->flags |= SN_FINST_D; |
| 3920 | return 1; |
| 3921 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3922 | else if (req->l >= req->rlim - req->data) { |
| 3923 | /* no room to read more data */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3924 | |
| 3925 | /* FIXME-20050705: is it possible for a client to maintain a session |
| 3926 | * after the timeout by sending more data after it receives a close ? |
| 3927 | */ |
| 3928 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3929 | if (FD_ISSET(t->cli_fd, StaticReadEvent)) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3930 | /* stop reading until we get some space */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3931 | FD_CLR(t->cli_fd, StaticReadEvent); |
| 3932 | tv_eternity(&t->crexpire); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3933 | //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] | 3934 | } |
| 3935 | } |
| 3936 | else { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3937 | /* there's still some space in the buffer */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3938 | if (! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 3939 | FD_SET(t->cli_fd, StaticReadEvent); |
| 3940 | if (t->proxy->clitimeout) |
| 3941 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 3942 | else |
| 3943 | tv_eternity(&t->crexpire); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 3944 | //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] | 3945 | } |
| 3946 | } |
| 3947 | return 0; |
| 3948 | } |
| 3949 | else { /* CL_STCLOSE: nothing to do */ |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 3950 | 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] | 3951 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 3952 | 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] | 3953 | write(1, trash, len); |
| 3954 | } |
| 3955 | return 0; |
| 3956 | } |
| 3957 | return 0; |
| 3958 | } |
| 3959 | |
| 3960 | |
| 3961 | /* |
| 3962 | * manages the server FSM and its socket. It returns 1 if a state has changed |
| 3963 | * (and a resync may be needed), 0 else. |
| 3964 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3965 | int process_srv(struct session *t) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3966 | int s = t->srv_state; |
| 3967 | int c = t->cli_state; |
| 3968 | struct buffer *req = t->req; |
| 3969 | struct buffer *rep = t->rep; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 3970 | appsess *asession_temp = NULL; |
| 3971 | appsess local_asession; |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3972 | int conn_err; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3973 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3974 | #ifdef DEBUG_FULL |
| 3975 | fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 3976 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3977 | //fprintf(stderr,"process_srv: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s, |
| 3978 | //FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 3979 | //FD_ISSET(t->srv_fd, StaticReadEvent), FD_ISSET(t->srv_fd, StaticWriteEvent) |
| 3980 | //); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3981 | if (s == SV_STIDLE) { |
| 3982 | if (c == CL_STHEADERS) |
| 3983 | return 0; /* stay in idle, waiting for data to reach the client side */ |
| 3984 | else if (c == CL_STCLOSE || |
| 3985 | c == CL_STSHUTW || |
| 3986 | (c == CL_STSHUTR && t->req->l == 0)) { /* give up */ |
| 3987 | tv_eternity(&t->cnexpire); |
| 3988 | t->srv_state = SV_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3989 | if (!(t->flags & SN_ERR_MASK)) |
| 3990 | t->flags |= SN_ERR_CLICL; |
| 3991 | if (!(t->flags & SN_FINST_MASK)) |
| 3992 | t->flags |= SN_FINST_C; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3993 | return 1; |
| 3994 | } |
| 3995 | else { /* go to SV_STCONN */ |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 3996 | /* initiate a connection to the server */ |
| 3997 | conn_err = connect_server(t); |
| 3998 | if (conn_err == SN_ERR_NONE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3999 | //fprintf(stderr,"0: c=%d, s=%d\n", c, s); |
| 4000 | t->srv_state = SV_STCONN; |
| 4001 | } |
| 4002 | else { /* try again */ |
| 4003 | while (t->conn_retries-- > 0) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4004 | if ((t->proxy->options & PR_O_REDISP) && (t->conn_retries == 0)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4005 | t->flags &= ~SN_DIRECT; /* ignore cookie and force to use the dispatcher */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4006 | 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] | 4007 | if ((t->flags & SN_CK_MASK) == SN_CK_VALID) { |
| 4008 | t->flags &= ~SN_CK_MASK; |
| 4009 | t->flags |= SN_CK_DOWN; |
| 4010 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4011 | } |
| 4012 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 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 | t->srv_state = SV_STCONN; |
| 4016 | break; |
| 4017 | } |
| 4018 | } |
| 4019 | if (t->conn_retries < 0) { |
| 4020 | /* if conn_retries < 0 or other error, let's abort */ |
| 4021 | tv_eternity(&t->cnexpire); |
| 4022 | t->srv_state = SV_STCLOSE; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4023 | t->logs.status = 503; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4024 | if (t->proxy->mode == PR_MODE_HTTP) |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4025 | client_return(t, t->proxy->errmsg.len503, t->proxy->errmsg.msg503); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4026 | if (!(t->flags & SN_ERR_MASK)) |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4027 | t->flags |= conn_err; /* report the precise connect() error */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4028 | if (!(t->flags & SN_FINST_MASK)) |
| 4029 | t->flags |= SN_FINST_C; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4030 | } |
| 4031 | } |
| 4032 | return 1; |
| 4033 | } |
| 4034 | } |
| 4035 | else if (s == SV_STCONN) { /* connection in progress */ |
| 4036 | if (t->res_sw == RES_SILENT && tv_cmp2_ms(&t->cnexpire, &now) > 0) { |
| 4037 | //fprintf(stderr,"1: c=%d, s=%d\n", c, s); |
| 4038 | return 0; /* nothing changed */ |
| 4039 | } |
| 4040 | else if (t->res_sw == RES_SILENT || t->res_sw == RES_ERROR) { |
| 4041 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 4042 | /* timeout, connect error or first write error */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4043 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4044 | fd_delete(t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4045 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4046 | t->conn_retries--; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4047 | if (t->conn_retries >= 0) { |
| 4048 | if ((t->proxy->options & PR_O_REDISP) && (t->conn_retries == 0)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4049 | t->flags &= ~SN_DIRECT; /* ignore cookie and force to use the dispatcher */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4050 | 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] | 4051 | if ((t->flags & SN_CK_MASK) == SN_CK_VALID) { |
| 4052 | t->flags &= ~SN_CK_MASK; |
| 4053 | t->flags |= SN_CK_DOWN; |
| 4054 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4055 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4056 | conn_err = connect_server(t); |
| 4057 | if (conn_err == SN_ERR_NONE) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4058 | return 0; /* no state changed */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4059 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4060 | else if (t->res_sw == RES_SILENT) |
| 4061 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 4062 | else |
| 4063 | conn_err = SN_ERR_SRVCL; // it was a connect error. |
| 4064 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4065 | /* if conn_retries < 0 or other error, let's abort */ |
| 4066 | tv_eternity(&t->cnexpire); |
| 4067 | t->srv_state = SV_STCLOSE; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4068 | t->logs.status = 503; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4069 | if (t->proxy->mode == PR_MODE_HTTP) |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4070 | client_return(t, t->proxy->errmsg.len503, t->proxy->errmsg.msg503); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4071 | if (!(t->flags & SN_ERR_MASK)) |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4072 | t->flags |= conn_err; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4073 | if (!(t->flags & SN_FINST_MASK)) |
| 4074 | t->flags |= SN_FINST_C; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4075 | return 1; |
| 4076 | } |
| 4077 | else { /* no error or write 0 */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4078 | t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4079 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4080 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4081 | if (req->l == 0) /* nothing to write */ { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4082 | FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4083 | tv_eternity(&t->swexpire); |
| 4084 | } else /* need the right to write */ { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4085 | FD_SET(t->srv_fd, StaticWriteEvent); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4086 | if (t->proxy->srvtimeout) { |
| 4087 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
| 4088 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 4089 | t->srexpire = t->swexpire; |
| 4090 | } |
| 4091 | else |
| 4092 | tv_eternity(&t->swexpire); |
| 4093 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4094 | |
| 4095 | if (t->proxy->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 4096 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4097 | if (t->proxy->srvtimeout) |
| 4098 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4099 | else |
| 4100 | tv_eternity(&t->srexpire); |
| 4101 | |
| 4102 | t->srv_state = SV_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4103 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4104 | |
| 4105 | /* if the user wants to log as soon as possible, without counting |
| 4106 | bytes from the server, then this is the right moment. */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 4107 | if (t->proxy->to_log && !(t->logs.logwait & LW_BYTES)) { |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4108 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 4109 | sess_log(t); |
| 4110 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4111 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4112 | else { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4113 | t->srv_state = SV_STHEADERS; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4114 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
| 4115 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4116 | tv_eternity(&t->cnexpire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4117 | return 1; |
| 4118 | } |
| 4119 | } |
| 4120 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4121 | /* now parse the partial (or complete) headers */ |
| 4122 | while (rep->lr < rep->r) { /* this loop only sees one header at each iteration */ |
| 4123 | char *ptr; |
| 4124 | int delete_header; |
| 4125 | |
| 4126 | ptr = rep->lr; |
| 4127 | |
| 4128 | /* look for the end of the current header */ |
| 4129 | while (ptr < rep->r && *ptr != '\n' && *ptr != '\r') |
| 4130 | ptr++; |
| 4131 | |
| 4132 | if (ptr == rep->h) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4133 | int line, len; |
| 4134 | |
| 4135 | /* we can only get here after an end of headers */ |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4136 | |
| 4137 | /* first, we'll block if security checks have caught nasty things */ |
| 4138 | if (t->flags & SN_CACHEABLE) { |
| 4139 | if ((t->flags & SN_CACHE_COOK) && |
| 4140 | (t->flags & SN_SCK_ANY) && |
| 4141 | (t->proxy->options & PR_O_CHK_CACHE)) { |
| 4142 | |
| 4143 | /* we're in presence of a cacheable response containing |
| 4144 | * a set-cookie header. We'll block it as requested by |
| 4145 | * the 'checkcache' option, and send an alert. |
| 4146 | */ |
| 4147 | tv_eternity(&t->srexpire); |
| 4148 | tv_eternity(&t->swexpire); |
| 4149 | fd_delete(t->srv_fd); |
| 4150 | t->srv_state = SV_STCLOSE; |
| 4151 | t->logs.status = 502; |
| 4152 | client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502); |
| 4153 | if (!(t->flags & SN_ERR_MASK)) |
| 4154 | t->flags |= SN_ERR_PRXCOND; |
| 4155 | if (!(t->flags & SN_FINST_MASK)) |
| 4156 | t->flags |= SN_FINST_H; |
| 4157 | |
| 4158 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", t->proxy->id, t->srv->id); |
| 4159 | send_log(t->proxy, LOG_ALERT, "Blocking cacheable cookie in response from instance %s, server %s.\n", t->proxy->id, t->srv->id); |
| 4160 | |
| 4161 | return 1; |
| 4162 | } |
| 4163 | } |
| 4164 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4165 | /* next, we'll block if an 'rspideny' or 'rspdeny' filter matched */ |
| 4166 | if (t->flags & SN_SVDENY) { |
| 4167 | tv_eternity(&t->srexpire); |
| 4168 | tv_eternity(&t->swexpire); |
| 4169 | fd_delete(t->srv_fd); |
| 4170 | t->srv_state = SV_STCLOSE; |
| 4171 | t->logs.status = 502; |
| 4172 | client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502); |
| 4173 | if (!(t->flags & SN_ERR_MASK)) |
| 4174 | t->flags |= SN_ERR_PRXCOND; |
| 4175 | if (!(t->flags & SN_FINST_MASK)) |
| 4176 | t->flags |= SN_FINST_H; |
| 4177 | return 1; |
| 4178 | } |
| 4179 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4180 | /* we'll have something else to do here : add new headers ... */ |
| 4181 | |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 4182 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_INS) && |
| 4183 | (!(t->proxy->options & PR_O_COOK_POST) || (t->flags & SN_POST))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4184 | /* 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] | 4185 | * insert a set-cookie here, except if we want to insert only on POST |
| 4186 | * requests and this one isn't. |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4187 | */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4188 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/\r\n", |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4189 | t->proxy->cookie_name, |
| 4190 | t->srv->cookie ? t->srv->cookie : ""); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4191 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4192 | t->flags |= SN_SCK_INSERTED; |
| 4193 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4194 | /* Here, we will tell an eventual cache on the client side that we don't |
| 4195 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 4196 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 4197 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 4198 | */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4199 | if (t->proxy->options & PR_O_COOK_NOC) |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4200 | //len += sprintf(newhdr + len, "Cache-control: no-cache=\"set-cookie\"\r\n"); |
| 4201 | len += sprintf(trash + len, "Cache-control: private\r\n"); |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 4202 | |
| 4203 | if (rep->data + rep->l < rep->h) |
| 4204 | /* The data has been stolen, we will crash cleanly instead of corrupting memory */ |
| 4205 | *(int *)0 = 0; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4206 | buffer_replace2(rep, rep->h, rep->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4207 | } |
| 4208 | |
| 4209 | /* headers to be added */ |
| 4210 | for (line = 0; line < t->proxy->nb_rspadd; line++) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4211 | len = sprintf(trash, "%s\r\n", t->proxy->rsp_add[line]); |
| 4212 | buffer_replace2(rep, rep->h, rep->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4213 | } |
| 4214 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4215 | /* add a "connection: close" line if needed */ |
| 4216 | if (t->proxy->options & PR_O_HTTP_CLOSE) |
| 4217 | buffer_replace2(rep, rep->h, rep->h, "Connection: close\r\n", 19); |
| 4218 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4219 | t->srv_state = SV_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4220 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4221 | t->logs.t_data = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4222 | |
| 4223 | /* if the user wants to log as soon as possible, without counting |
| 4224 | bytes from the server, then this is the right moment. */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 4225 | if (t->proxy->to_log && !(t->logs.logwait & LW_BYTES)) { |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4226 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 4227 | t->logs.bytes = rep->h - rep->data; |
| 4228 | sess_log(t); |
| 4229 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4230 | break; |
| 4231 | } |
| 4232 | |
| 4233 | /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */ |
| 4234 | if (ptr > rep->r - 2) { |
| 4235 | /* this is a partial header, let's wait for more to come */ |
| 4236 | rep->lr = ptr; |
| 4237 | break; |
| 4238 | } |
| 4239 | |
| 4240 | // fprintf(stderr,"h=%p, ptr=%p, lr=%p, r=%p, *h=", rep->h, ptr, rep->lr, rep->r); |
| 4241 | // write(2, rep->h, ptr - rep->h); fprintf(stderr,"\n"); |
| 4242 | |
| 4243 | /* now we know that *ptr is either \r or \n, |
| 4244 | * and that there are at least 1 char after it. |
| 4245 | */ |
| 4246 | if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n')) |
| 4247 | rep->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */ |
| 4248 | else |
| 4249 | rep->lr = ptr + 2; /* \r\n or \n\r */ |
| 4250 | |
| 4251 | /* |
| 4252 | * now we know that we have a full header ; we can do whatever |
| 4253 | * we want with these pointers : |
| 4254 | * rep->h = beginning of header |
| 4255 | * ptr = end of header (first \r or \n) |
| 4256 | * rep->lr = beginning of next line (next rep->h) |
| 4257 | * rep->r = end of data (not used at this stage) |
| 4258 | */ |
| 4259 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4260 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4261 | if (t->logs.status == -1) { |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4262 | t->logs.logwait &= ~LW_RESP; |
| 4263 | t->logs.status = atoi(rep->h + 9); |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4264 | switch (t->logs.status) { |
| 4265 | case 200: |
| 4266 | case 203: |
| 4267 | case 206: |
| 4268 | case 300: |
| 4269 | case 301: |
| 4270 | case 410: |
| 4271 | /* RFC2616 @13.4: |
| 4272 | * "A response received with a status code of |
| 4273 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4274 | * by a cache (...) unless a cache-control |
| 4275 | * directive prohibits caching." |
| 4276 | * |
| 4277 | * RFC2616 @9.5: POST method : |
| 4278 | * "Responses to this method are not cacheable, |
| 4279 | * unless the response includes appropriate |
| 4280 | * Cache-Control or Expires header fields." |
| 4281 | */ |
| 4282 | if ((!t->flags & SN_POST) && (t->proxy->options & PR_O_CHK_CACHE)) |
| 4283 | t->flags |= SN_CACHEABLE | SN_CACHE_COOK; |
| 4284 | break; |
| 4285 | default: |
| 4286 | break; |
| 4287 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 4288 | } |
| 4289 | else if (t->logs.logwait & LW_RSPHDR) { |
| 4290 | struct cap_hdr *h; |
| 4291 | int len; |
| 4292 | for (h = t->proxy->rsp_cap; h; h = h->next) { |
| 4293 | if ((h->namelen + 2 <= ptr - rep->h) && |
| 4294 | (rep->h[h->namelen] == ':') && |
| 4295 | (strncasecmp(rep->h, h->name, h->namelen) == 0)) { |
| 4296 | |
| 4297 | if (t->rsp_cap[h->index] == NULL) |
| 4298 | t->rsp_cap[h->index] = pool_alloc_from(h->pool, h->len + 1); |
| 4299 | |
| 4300 | len = ptr - (rep->h + h->namelen + 2); |
| 4301 | if (len > h->len) |
| 4302 | len = h->len; |
| 4303 | |
| 4304 | memcpy(t->rsp_cap[h->index], rep->h + h->namelen + 2, len); |
| 4305 | t->rsp_cap[h->index][len]=0; |
| 4306 | } |
| 4307 | } |
| 4308 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4309 | } |
| 4310 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4311 | delete_header = 0; |
| 4312 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4313 | 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] | 4314 | int len, max; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 4315 | 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] | 4316 | max = ptr - rep->h; |
| 4317 | UBOUND(max, sizeof(trash) - len - 1); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4318 | len += strlcpy2(trash + len, rep->h, max + 1); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4319 | trash[len++] = '\n'; |
| 4320 | write(1, trash, len); |
| 4321 | } |
| 4322 | |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4323 | /* remove "connection: " if needed */ |
| 4324 | if (!delete_header && (t->proxy->options & PR_O_HTTP_CLOSE) |
| 4325 | && (strncasecmp(rep->h, "Connection: ", 12) == 0)) { |
| 4326 | delete_header = 1; |
| 4327 | } |
| 4328 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4329 | /* try headers regexps */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 4330 | if (!delete_header && t->proxy->rsp_exp != NULL |
| 4331 | && !(t->flags & SN_SVDENY)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4332 | struct hdr_exp *exp; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4333 | char term; |
| 4334 | |
| 4335 | term = *ptr; |
| 4336 | *ptr = '\0'; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4337 | exp = t->proxy->rsp_exp; |
| 4338 | do { |
| 4339 | if (regexec(exp->preg, rep->h, MAX_MATCH, pmatch, 0) == 0) { |
| 4340 | switch (exp->action) { |
| 4341 | case ACT_ALLOW: |
| 4342 | if (!(t->flags & SN_SVDENY)) |
| 4343 | t->flags |= SN_SVALLOW; |
| 4344 | break; |
| 4345 | case ACT_REPLACE: |
| 4346 | if (!(t->flags & SN_SVDENY)) { |
| 4347 | int len = exp_replace(trash, rep->h, exp->replace, pmatch); |
| 4348 | ptr += buffer_replace2(rep, rep->h, ptr, trash, len); |
| 4349 | } |
| 4350 | break; |
| 4351 | case ACT_REMOVE: |
| 4352 | if (!(t->flags & SN_SVDENY)) |
| 4353 | delete_header = 1; |
| 4354 | break; |
| 4355 | case ACT_DENY: |
| 4356 | if (!(t->flags & SN_SVALLOW)) |
| 4357 | t->flags |= SN_SVDENY; |
| 4358 | break; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4359 | case ACT_PASS: /* we simply don't deny this one */ |
| 4360 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4361 | } |
| 4362 | break; |
| 4363 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4364 | } while ((exp = exp->next) != NULL); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4365 | *ptr = term; /* restore the string terminator */ |
| 4366 | } |
| 4367 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4368 | /* check for cache-control: or pragma: headers */ |
| 4369 | if (!delete_header && (t->flags & SN_CACHEABLE)) { |
| 4370 | if (strncasecmp(rep->h, "Pragma: no-cache", 16) == 0) |
| 4371 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4372 | else if (strncasecmp(rep->h, "Cache-control: ", 15) == 0) { |
| 4373 | if (strncasecmp(rep->h + 15, "no-cache", 8) == 0) { |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4374 | if (rep->h + 23 == ptr || rep->h[23] == ',') |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4375 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4376 | else { |
| 4377 | if (strncasecmp(rep->h + 23, "=\"set-cookie", 12) == 0 |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4378 | && (rep->h[35] == '"' || rep->h[35] == ',')) |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4379 | t->flags &= ~SN_CACHE_COOK; |
| 4380 | } |
| 4381 | } else if ((strncasecmp(rep->h + 15, "private", 7) == 0 && |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4382 | (rep->h + 22 == ptr || rep->h[22] == ',')) |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4383 | || (strncasecmp(rep->h + 15, "no-store", 8) == 0 && |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4384 | (rep->h + 23 == ptr || rep->h[23] == ','))) { |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4385 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4386 | } else if (strncasecmp(rep->h + 15, "max-age=0", 9) == 0 && |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4387 | (rep->h + 24 == ptr || rep->h[24] == ',')) { |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4388 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4389 | } else if (strncasecmp(rep->h + 15, "s-maxage=0", 10) == 0 && |
| 4390 | (rep->h + 25 == ptr || rep->h[25] == ',')) { |
| 4391 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4392 | } else if (strncasecmp(rep->h + 15, "public", 6) == 0 && |
| 4393 | (rep->h + 21 == ptr || rep->h[21] == ',')) { |
| 4394 | t->flags |= SN_CACHEABLE | SN_CACHE_COOK; |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4395 | } |
| 4396 | } |
| 4397 | } |
| 4398 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4399 | /* check for server cookies */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4400 | if (!delete_header /*&& (t->proxy->options & PR_O_COOK_ANY)*/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4401 | && (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] | 4402 | && (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4403 | char *p1, *p2, *p3, *p4; |
| 4404 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4405 | t->flags |= SN_SCK_ANY; |
| 4406 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4407 | p1 = rep->h + 12; /* first char after 'Set-Cookie: ' */ |
| 4408 | |
| 4409 | while (p1 < ptr) { /* in fact, we'll break after the first cookie */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 4410 | while (p1 < ptr && (isspace((int)*p1))) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4411 | p1++; |
| 4412 | |
| 4413 | if (p1 == ptr || *p1 == ';') /* end of cookie */ |
| 4414 | break; |
| 4415 | |
| 4416 | /* p1 is at the beginning of the cookie name */ |
| 4417 | p2 = p1; |
| 4418 | |
| 4419 | while (p2 < ptr && *p2 != '=' && *p2 != ';') |
| 4420 | p2++; |
| 4421 | |
| 4422 | if (p2 == ptr || *p2 == ';') /* next cookie */ |
| 4423 | break; |
| 4424 | |
| 4425 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4426 | if (p3 == ptr) |
| 4427 | break; |
| 4428 | |
| 4429 | p4 = p3; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 4430 | while (p4 < ptr && !isspace((int)*p4) && *p4 != ';') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4431 | p4++; |
| 4432 | |
| 4433 | /* here, we have the cookie name between p1 and p2, |
| 4434 | * and its value between p3 and p4. |
| 4435 | * we can process it. |
| 4436 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4437 | |
| 4438 | /* first, let's see if we want to capture it */ |
| 4439 | if (t->proxy->capture_name != NULL && |
| 4440 | t->logs.srv_cookie == NULL && |
| 4441 | (p4 - p1 >= t->proxy->capture_namelen) && |
| 4442 | memcmp(p1, t->proxy->capture_name, t->proxy->capture_namelen) == 0) { |
| 4443 | int log_len = p4 - p1; |
| 4444 | |
| 4445 | if ((t->logs.srv_cookie = pool_alloc(capture)) == NULL) { |
| 4446 | Alert("HTTP logging : out of memory.\n"); |
| 4447 | } |
| 4448 | |
| 4449 | if (log_len > t->proxy->capture_len) |
| 4450 | log_len = t->proxy->capture_len; |
| 4451 | memcpy(t->logs.srv_cookie, p1, log_len); |
| 4452 | t->logs.srv_cookie[log_len] = 0; |
| 4453 | } |
| 4454 | |
| 4455 | if ((p2 - p1 == t->proxy->cookie_len) && (t->proxy->cookie_name != NULL) && |
| 4456 | (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4457 | /* Cool... it's the right one */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4458 | t->flags |= SN_SCK_SEEN; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4459 | |
| 4460 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4461 | * this occurrence because we'll insert another one later. |
| 4462 | * We'll delete it too if the "indirect" option is set and we're in |
| 4463 | * a direct access. */ |
| 4464 | if (((t->srv) && (t->proxy->options & PR_O_COOK_INS)) || |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4465 | ((t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_IND))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4466 | /* this header must be deleted */ |
| 4467 | delete_header = 1; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4468 | t->flags |= SN_SCK_DELETED; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4469 | } |
| 4470 | else if ((t->srv) && (t->proxy->options & PR_O_COOK_RW)) { |
| 4471 | /* replace bytes p3->p4 with the cookie name associated |
| 4472 | * with this server since we know it. |
| 4473 | */ |
| 4474 | buffer_replace2(rep, p3, p4, t->srv->cookie, t->srv->cklen); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4475 | t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4476 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 4477 | else if ((t->srv) && (t->proxy->options & PR_O_COOK_PFX)) { |
| 4478 | /* insert the cookie name associated with this server |
| 4479 | * before existing cookie, and insert a delimitor between them.. |
| 4480 | */ |
| 4481 | buffer_replace2(rep, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4482 | p3[t->srv->cklen] = COOKIE_DELIM; |
| 4483 | t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED; |
| 4484 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4485 | break; |
| 4486 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4487 | |
| 4488 | /* first, let's see if the cookie is our appcookie*/ |
| 4489 | if ((t->proxy->appsession_name != NULL) && |
| 4490 | (memcmp(p1, t->proxy->appsession_name, p2 - p1) == 0)) { |
| 4491 | |
| 4492 | /* Cool... it's the right one */ |
| 4493 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4494 | size_t server_id_len = strlen(t->srv->id) + 1; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4495 | asession_temp = &local_asession; |
| 4496 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4497 | 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] | 4498 | Alert("Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 4499 | send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 4500 | } |
| 4501 | memcpy(asession_temp->sessid, p3, t->proxy->appsession_len); |
| 4502 | asession_temp->sessid[t->proxy->appsession_len] = 0; |
| 4503 | asession_temp->serverid = NULL; |
| 4504 | |
| 4505 | /* only do insert, if lookup fails */ |
| 4506 | if (chtbl_lookup(&(t->proxy->htbl_proxy), (void *) &asession_temp) != 0) { |
| 4507 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 4508 | Alert("Not enought Memory process_srv():asession:calloc().\n"); |
| 4509 | send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession:calloc().\n"); |
| 4510 | return 0; |
| 4511 | } |
| 4512 | asession_temp->sessid = local_asession.sessid; |
| 4513 | asession_temp->serverid = local_asession.serverid; |
| 4514 | chtbl_insert(&(t->proxy->htbl_proxy), (void *) asession_temp); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4515 | }/* end if (chtbl_lookup()) */ |
| 4516 | else { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4517 | /* free wasted memory */ |
| 4518 | pool_free_to(apools.sessid, local_asession.sessid); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4519 | } /* end else from if (chtbl_lookup()) */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4520 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4521 | if (asession_temp->serverid == NULL) { |
| 4522 | 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] | 4523 | Alert("Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 4524 | send_log(t->proxy, LOG_ALERT, "Not enought Memory process_srv():asession->sessid:malloc().\n"); |
| 4525 | } |
| 4526 | asession_temp->serverid[0] = '\0'; |
| 4527 | } |
| 4528 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4529 | if (asession_temp->serverid[0] == '\0') |
| 4530 | memcpy(asession_temp->serverid,t->srv->id,server_id_len); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 4531 | |
| 4532 | tv_delayfrom(&asession_temp->expire, &now, t->proxy->appsession_timeout); |
| 4533 | |
| 4534 | #if defined(DEBUG_HASH) |
| 4535 | print_table(&(t->proxy->htbl_proxy)); |
| 4536 | #endif |
| 4537 | break; |
| 4538 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4539 | else { |
| 4540 | // fprintf(stderr,"Ignoring unknown cookie : "); |
| 4541 | // write(2, p1, p2-p1); |
| 4542 | // fprintf(stderr," = "); |
| 4543 | // write(2, p3, p4-p3); |
| 4544 | // fprintf(stderr,"\n"); |
| 4545 | } |
| 4546 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4547 | } /* we're now at the end of the cookie value */ |
| 4548 | } /* end of cookie processing */ |
| 4549 | |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 4550 | /* check for any set-cookie in case we check for cacheability */ |
| 4551 | if (!delete_header && !(t->flags & SN_SCK_ANY) && |
| 4552 | (t->proxy->options & PR_O_CHK_CACHE) && |
| 4553 | (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) { |
| 4554 | t->flags |= SN_SCK_ANY; |
| 4555 | } |
| 4556 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4557 | /* let's look if we have to delete this header */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4558 | if (delete_header && !(t->flags & SN_SVDENY)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4559 | buffer_replace2(rep, rep->h, rep->lr, "", 0); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4560 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4561 | rep->h = rep->lr; |
| 4562 | } /* while (rep->lr < rep->r) */ |
| 4563 | |
| 4564 | /* end of header processing (even if incomplete) */ |
| 4565 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4566 | if ((rep->l < rep->rlim - rep->data) && ! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 4567 | /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer |
| 4568 | * full. We cannot loop here since event_srv_read will disable it only if |
| 4569 | * rep->l == rlim-data |
| 4570 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4571 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4572 | if (t->proxy->srvtimeout) |
| 4573 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4574 | else |
| 4575 | tv_eternity(&t->srexpire); |
| 4576 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4577 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4578 | /* read error, write error */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4579 | if (t->res_sw == RES_ERROR || t->res_sr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4580 | tv_eternity(&t->srexpire); |
| 4581 | tv_eternity(&t->swexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4582 | fd_delete(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4583 | t->srv_state = SV_STCLOSE; |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 4584 | t->logs.status = 502; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4585 | client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4586 | if (!(t->flags & SN_ERR_MASK)) |
| 4587 | t->flags |= SN_ERR_SRVCL; |
| 4588 | if (!(t->flags & SN_FINST_MASK)) |
| 4589 | t->flags |= SN_FINST_H; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4590 | return 1; |
| 4591 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4592 | /* end of client write or end of server read. |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4593 | * since we are in header mode, if there's no space left for headers, we |
| 4594 | * won't be able to free more later, so the session will never terminate. |
| 4595 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4596 | 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] | 4597 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4598 | tv_eternity(&t->srexpire); |
| 4599 | shutdown(t->srv_fd, SHUT_RD); |
| 4600 | t->srv_state = SV_STSHUTR; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4601 | //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] | 4602 | return 1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4603 | } |
| 4604 | /* read timeout : return a 504 to the client. |
| 4605 | */ |
| 4606 | else if (FD_ISSET(t->srv_fd, StaticReadEvent) && tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 4607 | tv_eternity(&t->srexpire); |
| 4608 | tv_eternity(&t->swexpire); |
| 4609 | fd_delete(t->srv_fd); |
| 4610 | t->srv_state = SV_STCLOSE; |
| 4611 | t->logs.status = 504; |
| 4612 | client_return(t, t->proxy->errmsg.len504, t->proxy->errmsg.msg504); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4613 | if (!(t->flags & SN_ERR_MASK)) |
| 4614 | t->flags |= SN_ERR_SRVTO; |
| 4615 | if (!(t->flags & SN_FINST_MASK)) |
| 4616 | t->flags |= SN_FINST_H; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4617 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4618 | |
| 4619 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4620 | /* last client read and buffer empty */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4621 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 4622 | * client shuts read too early, because we may still have |
| 4623 | * some work to do on the headers. |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4624 | * The side-effect is that if the client completely closes its |
| 4625 | * connection during SV_STHEADER, the connection to the server |
| 4626 | * is kept until a response comes back or the timeout is reached. |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4627 | */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4628 | else if ((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && (req->l == 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4629 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4630 | tv_eternity(&t->swexpire); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4631 | |
| 4632 | /* We must ensure that the read part is still alive when switching |
| 4633 | * to shutw */ |
| 4634 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4635 | if (t->proxy->srvtimeout) |
| 4636 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4637 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4638 | shutdown(t->srv_fd, SHUT_WR); |
| 4639 | t->srv_state = SV_STSHUTW; |
| 4640 | return 1; |
| 4641 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4642 | /* write timeout */ |
| 4643 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 4644 | * client shuts read too early, because we may still have |
| 4645 | * some work to do on the headers. |
| 4646 | */ |
| 4647 | else if (FD_ISSET(t->srv_fd, StaticWriteEvent) && tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
| 4648 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4649 | tv_eternity(&t->swexpire); |
| 4650 | shutdown(t->srv_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4651 | /* We must ensure that the read part is still alive when switching |
| 4652 | * to shutw */ |
| 4653 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4654 | if (t->proxy->srvtimeout) |
| 4655 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4656 | |
| 4657 | /* We must ensure that the read part is still alive when switching |
| 4658 | * to shutw */ |
| 4659 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4660 | if (t->proxy->srvtimeout) |
| 4661 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4662 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4663 | t->srv_state = SV_STSHUTW; |
| 4664 | if (!(t->flags & SN_ERR_MASK)) |
| 4665 | t->flags |= SN_ERR_SRVTO; |
| 4666 | if (!(t->flags & SN_FINST_MASK)) |
| 4667 | t->flags |= SN_FINST_H; |
| 4668 | return 1; |
| 4669 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4670 | |
| 4671 | if (req->l == 0) { |
| 4672 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4673 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 4674 | tv_eternity(&t->swexpire); |
| 4675 | } |
| 4676 | } |
| 4677 | else { /* client buffer not empty */ |
| 4678 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4679 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4680 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4681 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4682 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 4683 | t->srexpire = t->swexpire; |
| 4684 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4685 | else |
| 4686 | tv_eternity(&t->swexpire); |
| 4687 | } |
| 4688 | } |
| 4689 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4690 | /* be nice with the client side which would like to send a complete header |
| 4691 | * FIXME: COMPLETELY BUGGY !!! not all headers may be processed because the client |
| 4692 | * would read all remaining data at once ! The client should not write past rep->lr |
| 4693 | * when the server is in header state. |
| 4694 | */ |
| 4695 | //return header_processed; |
| 4696 | return t->srv_state != SV_STHEADERS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4697 | } |
| 4698 | else if (s == SV_STDATA) { |
| 4699 | /* read or write error */ |
| 4700 | if (t->res_sw == RES_ERROR || t->res_sr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4701 | tv_eternity(&t->srexpire); |
| 4702 | tv_eternity(&t->swexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4703 | fd_delete(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4704 | t->srv_state = SV_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4705 | if (!(t->flags & SN_ERR_MASK)) |
| 4706 | t->flags |= SN_ERR_SRVCL; |
| 4707 | if (!(t->flags & SN_FINST_MASK)) |
| 4708 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4709 | return 1; |
| 4710 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4711 | /* last read, or end of client write */ |
| 4712 | 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] | 4713 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4714 | tv_eternity(&t->srexpire); |
| 4715 | shutdown(t->srv_fd, SHUT_RD); |
| 4716 | t->srv_state = SV_STSHUTR; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4717 | //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] | 4718 | return 1; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 4719 | } |
| 4720 | /* end of client read and no more data to send */ |
| 4721 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
| 4722 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4723 | tv_eternity(&t->swexpire); |
| 4724 | shutdown(t->srv_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4725 | /* We must ensure that the read part is still alive when switching |
| 4726 | * to shutw */ |
| 4727 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4728 | if (t->proxy->srvtimeout) |
| 4729 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4730 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 4731 | t->srv_state = SV_STSHUTW; |
| 4732 | return 1; |
| 4733 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4734 | /* read timeout */ |
| 4735 | else if (tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 4736 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4737 | tv_eternity(&t->srexpire); |
| 4738 | shutdown(t->srv_fd, SHUT_RD); |
| 4739 | t->srv_state = SV_STSHUTR; |
| 4740 | if (!(t->flags & SN_ERR_MASK)) |
| 4741 | t->flags |= SN_ERR_SRVTO; |
| 4742 | if (!(t->flags & SN_FINST_MASK)) |
| 4743 | t->flags |= SN_FINST_D; |
| 4744 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4745 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4746 | /* write timeout */ |
| 4747 | else if (tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4748 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4749 | tv_eternity(&t->swexpire); |
| 4750 | shutdown(t->srv_fd, SHUT_WR); |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 4751 | /* We must ensure that the read part is still alive when switching |
| 4752 | * to shutw */ |
| 4753 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4754 | if (t->proxy->srvtimeout) |
| 4755 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4756 | t->srv_state = SV_STSHUTW; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4757 | if (!(t->flags & SN_ERR_MASK)) |
| 4758 | t->flags |= SN_ERR_SRVTO; |
| 4759 | if (!(t->flags & SN_FINST_MASK)) |
| 4760 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4761 | return 1; |
| 4762 | } |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4763 | |
| 4764 | /* recompute request time-outs */ |
| 4765 | if (req->l == 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4766 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4767 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 4768 | tv_eternity(&t->swexpire); |
| 4769 | } |
| 4770 | } |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4771 | else { /* buffer not empty, there are still data to be transferred */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4772 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4773 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4774 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4775 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4776 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 4777 | t->srexpire = t->swexpire; |
| 4778 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4779 | else |
| 4780 | tv_eternity(&t->swexpire); |
| 4781 | } |
| 4782 | } |
| 4783 | |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4784 | /* recompute response time-outs */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4785 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
| 4786 | if (FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 4787 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4788 | tv_eternity(&t->srexpire); |
| 4789 | } |
| 4790 | } |
| 4791 | else { |
| 4792 | if (! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 4793 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4794 | if (t->proxy->srvtimeout) |
| 4795 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4796 | else |
| 4797 | tv_eternity(&t->srexpire); |
| 4798 | } |
| 4799 | } |
| 4800 | |
| 4801 | return 0; /* other cases change nothing */ |
| 4802 | } |
| 4803 | else if (s == SV_STSHUTR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4804 | if (t->res_sw == RES_ERROR) { |
| 4805 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4806 | tv_eternity(&t->swexpire); |
| 4807 | fd_delete(t->srv_fd); |
| 4808 | //close(t->srv_fd); |
| 4809 | t->srv_state = SV_STCLOSE; |
| 4810 | if (!(t->flags & SN_ERR_MASK)) |
| 4811 | t->flags |= SN_ERR_SRVCL; |
| 4812 | if (!(t->flags & SN_FINST_MASK)) |
| 4813 | t->flags |= SN_FINST_D; |
| 4814 | return 1; |
| 4815 | } |
| 4816 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4817 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4818 | tv_eternity(&t->swexpire); |
| 4819 | fd_delete(t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4820 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4821 | t->srv_state = SV_STCLOSE; |
| 4822 | return 1; |
| 4823 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4824 | else if (tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
| 4825 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
| 4826 | tv_eternity(&t->swexpire); |
| 4827 | fd_delete(t->srv_fd); |
| 4828 | //close(t->srv_fd); |
| 4829 | t->srv_state = SV_STCLOSE; |
| 4830 | if (!(t->flags & SN_ERR_MASK)) |
| 4831 | t->flags |= SN_ERR_SRVTO; |
| 4832 | if (!(t->flags & SN_FINST_MASK)) |
| 4833 | t->flags |= SN_FINST_D; |
| 4834 | return 1; |
| 4835 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4836 | else if (req->l == 0) { |
| 4837 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4838 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 4839 | tv_eternity(&t->swexpire); |
| 4840 | } |
| 4841 | } |
| 4842 | else { /* buffer not empty */ |
| 4843 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 4844 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4845 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4846 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 4847 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 4848 | t->srexpire = t->swexpire; |
| 4849 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4850 | else |
| 4851 | tv_eternity(&t->swexpire); |
| 4852 | } |
| 4853 | } |
| 4854 | return 0; |
| 4855 | } |
| 4856 | else if (s == SV_STSHUTW) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4857 | if (t->res_sr == RES_ERROR) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4858 | //FD_CLR(t->srv_fd, StaticReadEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4859 | tv_eternity(&t->srexpire); |
| 4860 | fd_delete(t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4861 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4862 | t->srv_state = SV_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4863 | if (!(t->flags & SN_ERR_MASK)) |
| 4864 | t->flags |= SN_ERR_SRVCL; |
| 4865 | if (!(t->flags & SN_FINST_MASK)) |
| 4866 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4867 | return 1; |
| 4868 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4869 | else if (t->res_sr == RES_NULL || c == CL_STSHUTW || c == CL_STCLOSE) { |
| 4870 | //FD_CLR(t->srv_fd, StaticReadEvent); |
| 4871 | tv_eternity(&t->srexpire); |
| 4872 | fd_delete(t->srv_fd); |
| 4873 | //close(t->srv_fd); |
| 4874 | t->srv_state = SV_STCLOSE; |
| 4875 | return 1; |
| 4876 | } |
| 4877 | else if (tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 4878 | //FD_CLR(t->srv_fd, StaticReadEvent); |
| 4879 | tv_eternity(&t->srexpire); |
| 4880 | fd_delete(t->srv_fd); |
| 4881 | //close(t->srv_fd); |
| 4882 | t->srv_state = SV_STCLOSE; |
| 4883 | if (!(t->flags & SN_ERR_MASK)) |
| 4884 | t->flags |= SN_ERR_SRVTO; |
| 4885 | if (!(t->flags & SN_FINST_MASK)) |
| 4886 | t->flags |= SN_FINST_D; |
| 4887 | return 1; |
| 4888 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4889 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
| 4890 | if (FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 4891 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 4892 | tv_eternity(&t->srexpire); |
| 4893 | } |
| 4894 | } |
| 4895 | else { |
| 4896 | if (! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 4897 | FD_SET(t->srv_fd, StaticReadEvent); |
| 4898 | if (t->proxy->srvtimeout) |
| 4899 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 4900 | else |
| 4901 | tv_eternity(&t->srexpire); |
| 4902 | } |
| 4903 | } |
| 4904 | return 0; |
| 4905 | } |
| 4906 | else { /* SV_STCLOSE : nothing to do */ |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4907 | 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] | 4908 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 4909 | 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] | 4910 | write(1, trash, len); |
| 4911 | } |
| 4912 | return 0; |
| 4913 | } |
| 4914 | return 0; |
| 4915 | } |
| 4916 | |
| 4917 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4918 | /* Processes the client and server jobs of a session task, then |
| 4919 | * puts it back to the wait queue in a clean state, or |
| 4920 | * cleans up its resources if it must be deleted. Returns |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4921 | * the time the task accepts to wait, or TIME_ETERNITY for |
| 4922 | * infinity. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4923 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4924 | int process_session(struct task *t) { |
| 4925 | struct session *s = t->context; |
| 4926 | int fsm_resync = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4927 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4928 | do { |
| 4929 | fsm_resync = 0; |
| 4930 | //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", t->cli_state, t->srv_state); |
| 4931 | fsm_resync |= process_cli(s); |
| 4932 | //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", t->cli_state, t->srv_state); |
| 4933 | fsm_resync |= process_srv(s); |
| 4934 | //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", t->cli_state, t->srv_state); |
| 4935 | } while (fsm_resync); |
| 4936 | |
| 4937 | if (s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4938 | struct timeval min1, min2; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4939 | 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] | 4940 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4941 | tv_min(&min1, &s->crexpire, &s->cwexpire); |
| 4942 | tv_min(&min2, &s->srexpire, &s->swexpire); |
| 4943 | tv_min(&min1, &min1, &s->cnexpire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4944 | tv_min(&t->expire, &min1, &min2); |
| 4945 | |
| 4946 | /* restore t to its place in the task list */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4947 | task_queue(t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4948 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4949 | return tv_remain2(&now, &t->expire); /* nothing more to do */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4950 | } |
| 4951 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4952 | s->proxy->nbconn--; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4953 | actconn--; |
| 4954 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 4955 | 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] | 4956 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 4957 | 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] | 4958 | write(1, trash, len); |
| 4959 | } |
| 4960 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4961 | s->logs.t_close = tv_diff(&s->logs.tv_accept, &now); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4962 | if (s->rep != NULL) |
| 4963 | s->logs.bytes = s->rep->total; |
| 4964 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4965 | /* let's do a final log if we need it */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4966 | 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] | 4967 | sess_log(s); |
| 4968 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4969 | /* the task MUST not be in the run queue anymore */ |
| 4970 | task_delete(t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4971 | session_free(s); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4972 | task_free(t); |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4973 | return TIME_ETERNITY; /* rest in peace for eternity */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4974 | } |
| 4975 | |
| 4976 | |
| 4977 | |
| 4978 | /* |
| 4979 | * manages a server health-check. Returns |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4980 | * the time the task accepts to wait, or TIME_ETERNITY for infinity. |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4981 | */ |
| 4982 | int process_chk(struct task *t) { |
| 4983 | struct server *s = t->context; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 4984 | struct sockaddr_in sa; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4985 | int fd = s->curfd; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4986 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4987 | //fprintf(stderr, "process_chk: task=%p\n", t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4988 | |
| 4989 | if (fd < 0) { /* no check currently running */ |
| 4990 | //fprintf(stderr, "process_chk: 2\n"); |
| 4991 | if (tv_cmp2_ms(&t->expire, &now) > 0) { /* not good time yet */ |
| 4992 | task_queue(t); /* restore t to its place in the task list */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 4993 | return tv_remain2(&now, &t->expire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4994 | } |
| 4995 | |
| 4996 | /* we'll initiate a new check */ |
| 4997 | s->result = 0; /* no result yet */ |
| 4998 | if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4999 | if ((fd < global.maxsock) && |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5000 | (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) && |
| 5001 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) { |
| 5002 | //fprintf(stderr, "process_chk: 3\n"); |
| 5003 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5004 | /* we'll connect to the check port on the server */ |
| 5005 | sa = s->addr; |
| 5006 | sa.sin_port = htons(s->check_port); |
| 5007 | |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 5008 | /* allow specific binding : |
| 5009 | * - server-specific at first |
| 5010 | * - proxy-specific next |
| 5011 | */ |
| 5012 | if (s->state & SRV_BIND_SRC) { |
| 5013 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 5014 | if (bind(fd, (struct sockaddr *)&s->source_addr, sizeof(s->source_addr)) == -1) { |
| 5015 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 5016 | s->proxy->id, s->id); |
| 5017 | s->result = -1; |
| 5018 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5019 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 5020 | else if (s->proxy->options & PR_O_BIND_SRC) { |
| 5021 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 5022 | if (bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) { |
| 5023 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", |
| 5024 | s->proxy->id); |
| 5025 | s->result = -1; |
| 5026 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5027 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 5028 | |
| 5029 | if (!s->result) { |
| 5030 | if ((connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != -1) || (errno == EINPROGRESS)) { |
| 5031 | /* OK, connection in progress or established */ |
| 5032 | |
| 5033 | //fprintf(stderr, "process_chk: 4\n"); |
| 5034 | |
| 5035 | s->curfd = fd; /* that's how we know a test is in progress ;-) */ |
| 5036 | fdtab[fd].owner = t; |
| 5037 | fdtab[fd].read = &event_srv_chk_r; |
| 5038 | fdtab[fd].write = &event_srv_chk_w; |
| 5039 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 5040 | FD_SET(fd, StaticWriteEvent); /* for connect status */ |
| 5041 | fd_insert(fd); |
| 5042 | /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */ |
| 5043 | tv_delayfrom(&t->expire, &now, s->inter); |
| 5044 | task_queue(t); /* restore t to its place in the task list */ |
| 5045 | return tv_remain(&now, &t->expire); |
| 5046 | } |
| 5047 | else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) { |
| 5048 | s->result = -1; /* a real error */ |
| 5049 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5050 | } |
| 5051 | } |
willy tarreau | 08dedbe | 2005-12-18 01:13:48 +0100 | [diff] [blame] | 5052 | close(fd); /* socket creation error */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5053 | } |
| 5054 | |
| 5055 | if (!s->result) { /* nothing done */ |
| 5056 | //fprintf(stderr, "process_chk: 6\n"); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5057 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5058 | task_queue(t); /* restore t to its place in the task list */ |
| 5059 | return tv_remain(&now, &t->expire); |
| 5060 | } |
| 5061 | |
| 5062 | /* here, we have seen a failure */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5063 | if (s->health > s->rise) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5064 | s->health--; /* still good */ |
| 5065 | else { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5066 | s->state &= ~SRV_RUNNING; |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5067 | if (s->health == s->rise) { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5068 | Warning("Server %s/%s DOWN.\n", s->proxy->id, s->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5069 | 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] | 5070 | |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5071 | if (find_server(s->proxy) == NULL) { |
| 5072 | Alert("Proxy %s has no server available !\n", s->proxy->id); |
| 5073 | send_log(s->proxy, LOG_EMERG, "Proxy %s has no server available !\n", s->proxy->id); |
| 5074 | } |
| 5075 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5076 | s->health = 0; /* failure */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5077 | } |
| 5078 | |
| 5079 | //fprintf(stderr, "process_chk: 7\n"); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5080 | /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */ |
| 5081 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5082 | } |
| 5083 | else { |
| 5084 | //fprintf(stderr, "process_chk: 8\n"); |
| 5085 | /* there was a test running */ |
| 5086 | if (s->result > 0) { /* good server detected */ |
| 5087 | //fprintf(stderr, "process_chk: 9\n"); |
| 5088 | s->health++; /* was bad, stays for a while */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5089 | if (s->health >= s->rise) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5090 | if (s->health == s->rise) { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5091 | Warning("server %s/%s UP.\n", s->proxy->id, s->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5092 | 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] | 5093 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5094 | |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5095 | s->health = s->rise + s->fall - 1; /* OK now */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5096 | s->state |= SRV_RUNNING; |
| 5097 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5098 | s->curfd = -1; /* no check running anymore */ |
| 5099 | //FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5100 | fd_delete(fd); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5101 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5102 | } |
| 5103 | else if (s->result < 0 || tv_cmp2_ms(&t->expire, &now) <= 0) { |
| 5104 | //fprintf(stderr, "process_chk: 10\n"); |
| 5105 | /* failure or timeout detected */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5106 | if (s->health > s->rise) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5107 | s->health--; /* still good */ |
| 5108 | else { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5109 | s->state &= ~SRV_RUNNING; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5110 | |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5111 | if (s->health == s->rise) { |
| 5112 | Warning("Server %s/%s DOWN.\n", s->proxy->id, s->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5113 | 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] | 5114 | |
| 5115 | if (find_server(s->proxy) == NULL) { |
| 5116 | Alert("Proxy %s has no server available !\n", s->proxy->id); |
| 5117 | send_log(s->proxy, LOG_EMERG, "Proxy %s has no server available !\n", s->proxy->id); |
| 5118 | } |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5119 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5120 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5121 | s->health = 0; /* failure */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5122 | } |
| 5123 | s->curfd = -1; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5124 | //FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5125 | fd_delete(fd); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 5126 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5127 | } |
| 5128 | /* if result is 0 and there's no timeout, we have to wait again */ |
| 5129 | } |
| 5130 | //fprintf(stderr, "process_chk: 11\n"); |
| 5131 | s->result = 0; |
| 5132 | task_queue(t); /* restore t to its place in the task list */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5133 | return tv_remain2(&now, &t->expire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5134 | } |
| 5135 | |
| 5136 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5137 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5138 | #if STATTIME > 0 |
| 5139 | int stats(void); |
| 5140 | #endif |
| 5141 | |
| 5142 | /* |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5143 | * This does 4 things : |
| 5144 | * - wake up all expired tasks |
| 5145 | * - call all runnable tasks |
| 5146 | * - call maintain_proxies() to enable/disable the listeners |
| 5147 | * - return the delay till next event in ms, -1 = wait indefinitely |
| 5148 | * Note: this part should be rewritten with the O(ln(n)) scheduler. |
| 5149 | * |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5150 | */ |
| 5151 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5152 | int process_runnable_tasks() { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5153 | int next_time; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5154 | int time2; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5155 | struct task *t, *tnext; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5156 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5157 | next_time = TIME_ETERNITY; /* set the timer to wait eternally first */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5158 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5159 | /* look for expired tasks and add them to the run queue. |
| 5160 | */ |
| 5161 | tnext = ((struct task *)LIST_HEAD(wait_queue))->next; |
| 5162 | while ((t = tnext) != LIST_HEAD(wait_queue)) { /* we haven't looped ? */ |
| 5163 | tnext = t->next; |
| 5164 | if (t->state & TASK_RUNNING) |
| 5165 | continue; |
| 5166 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5167 | if (tv_iseternity(&t->expire)) |
| 5168 | continue; |
| 5169 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5170 | /* wakeup expired entries. It doesn't matter if they are |
| 5171 | * already running because of a previous event |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5172 | */ |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5173 | if (tv_cmp_ms(&t->expire, &now) <= 0) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5174 | task_wakeup(&rq, t); |
| 5175 | } |
| 5176 | else { |
| 5177 | /* first non-runnable task. Use its expiration date as an upper bound */ |
| 5178 | int temp_time = tv_remain(&now, &t->expire); |
| 5179 | if (temp_time) |
| 5180 | next_time = temp_time; |
| 5181 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5182 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5183 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5184 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5185 | /* process each task in the run queue now. Each task may be deleted |
| 5186 | * since we only use tnext. |
| 5187 | */ |
| 5188 | tnext = rq; |
| 5189 | while ((t = tnext) != NULL) { |
| 5190 | int temp_time; |
| 5191 | |
| 5192 | tnext = t->rqnext; |
| 5193 | task_sleep(&rq, t); |
| 5194 | temp_time = t->process(t); |
| 5195 | next_time = MINTIME(temp_time, next_time); |
| 5196 | } |
| 5197 | |
| 5198 | /* maintain all proxies in a consistent state. This should quickly become a task */ |
| 5199 | time2 = maintain_proxies(); |
| 5200 | return MINTIME(time2, next_time); |
| 5201 | } |
| 5202 | |
| 5203 | |
| 5204 | #if defined(ENABLE_EPOLL) |
| 5205 | |
| 5206 | /* |
| 5207 | * Main epoll() loop. |
| 5208 | */ |
| 5209 | |
| 5210 | /* does 3 actions : |
| 5211 | * 0 (POLL_LOOP_ACTION_INIT) : initializes necessary private structures |
| 5212 | * 1 (POLL_LOOP_ACTION_RUN) : runs the loop |
| 5213 | * 2 (POLL_LOOP_ACTION_CLEAN) : cleans up |
| 5214 | * |
| 5215 | * returns 0 if initialization failed, !0 otherwise. |
| 5216 | */ |
| 5217 | |
| 5218 | int epoll_loop(int action) { |
| 5219 | int next_time; |
| 5220 | int status; |
| 5221 | int fd; |
| 5222 | |
| 5223 | int fds, count; |
| 5224 | int pr, pw, sr, sw; |
| 5225 | unsigned rn, ro, wn, wo; /* read new, read old, write new, write old */ |
| 5226 | struct epoll_event ev; |
| 5227 | |
| 5228 | /* private data */ |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5229 | static struct epoll_event *epoll_events = NULL; |
| 5230 | static int epoll_fd; |
| 5231 | |
| 5232 | if (action == POLL_LOOP_ACTION_INIT) { |
| 5233 | epoll_fd = epoll_create(global.maxsock + 1); |
| 5234 | if (epoll_fd < 0) |
| 5235 | return 0; |
| 5236 | else { |
| 5237 | epoll_events = (struct epoll_event*) |
| 5238 | calloc(1, sizeof(struct epoll_event) * global.maxsock); |
| 5239 | PrevReadEvent = (fd_set *) |
| 5240 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
| 5241 | PrevWriteEvent = (fd_set *) |
| 5242 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5243 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5244 | return 1; |
| 5245 | } |
| 5246 | else if (action == POLL_LOOP_ACTION_CLEAN) { |
| 5247 | if (PrevWriteEvent) free(PrevWriteEvent); |
| 5248 | if (PrevReadEvent) free(PrevReadEvent); |
| 5249 | if (epoll_events) free(epoll_events); |
| 5250 | close(epoll_fd); |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5251 | epoll_fd = 0; |
| 5252 | return 1; |
| 5253 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5254 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5255 | /* OK, it's POLL_LOOP_ACTION_RUN */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5256 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5257 | tv_now(&now); |
| 5258 | |
| 5259 | while (1) { |
| 5260 | next_time = process_runnable_tasks(); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5261 | |
| 5262 | /* stop when there's no connection left and we don't allow them anymore */ |
| 5263 | if (!actconn && listeners == 0) |
| 5264 | break; |
| 5265 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5266 | #if STATTIME > 0 |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5267 | { |
| 5268 | int time2; |
| 5269 | time2 = stats(); |
| 5270 | next_time = MINTIME(time2, next_time); |
| 5271 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5272 | #endif |
| 5273 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5274 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) { |
| 5275 | |
| 5276 | rn = ((int*)StaticReadEvent)[fds]; ro = ((int*)PrevReadEvent)[fds]; |
| 5277 | wn = ((int*)StaticWriteEvent)[fds]; wo = ((int*)PrevWriteEvent)[fds]; |
| 5278 | |
| 5279 | if ((ro^rn) | (wo^wn)) { |
| 5280 | for (count = 0, fd = fds << INTBITS; count < (1<<INTBITS) && fd < maxfd; count++, fd++) { |
| 5281 | #define FDSETS_ARE_INT_ALIGNED |
| 5282 | #ifdef FDSETS_ARE_INT_ALIGNED |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5283 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5284 | #define WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
| 5285 | #ifdef WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5286 | pr = (ro >> count) & 1; |
| 5287 | pw = (wo >> count) & 1; |
| 5288 | sr = (rn >> count) & 1; |
| 5289 | sw = (wn >> count) & 1; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5290 | #else |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5291 | pr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&ro); |
| 5292 | pw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wo); |
| 5293 | sr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&rn); |
| 5294 | sw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wn); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5295 | #endif |
| 5296 | #else |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5297 | pr = FD_ISSET(fd, PrevReadEvent); |
| 5298 | pw = FD_ISSET(fd, PrevWriteEvent); |
| 5299 | sr = FD_ISSET(fd, StaticReadEvent); |
| 5300 | sw = FD_ISSET(fd, StaticWriteEvent); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5301 | #endif |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5302 | if (!((sr^pr) | (sw^pw))) |
| 5303 | continue; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5304 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5305 | ev.events = (sr ? EPOLLIN : 0) | (sw ? EPOLLOUT : 0); |
| 5306 | ev.data.fd = fd; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5307 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5308 | #ifdef EPOLL_CTL_MOD_WORKAROUND |
| 5309 | /* I encountered a rarely reproducible problem with |
| 5310 | * EPOLL_CTL_MOD where a modified FD (systematically |
| 5311 | * the one in epoll_events[0], fd#7) would sometimes |
| 5312 | * be set EPOLL_OUT while asked for a read ! This is |
| 5313 | * with the 2.4 epoll patch. The workaround is to |
| 5314 | * delete then recreate in case of modification. |
| 5315 | * This is in 2.4 up to epoll-lt-0.21 but not in 2.6 |
| 5316 | * nor RHEL kernels. |
| 5317 | */ |
| 5318 | |
| 5319 | if ((pr | pw) && fdtab[fd].state != FD_STCLOSE) |
| 5320 | epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &ev); |
| 5321 | |
| 5322 | if ((sr | sw)) |
| 5323 | epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev); |
| 5324 | #else |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5325 | if ((pr | pw)) { |
| 5326 | /* the file-descriptor already exists... */ |
| 5327 | if ((sr | sw)) { |
| 5328 | /* ...and it will still exist */ |
| 5329 | if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fd, &ev) < 0) { |
| 5330 | // perror("epoll_ctl(MOD)"); |
| 5331 | // exit(1); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5332 | } |
| 5333 | } else { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5334 | /* ...and it will be removed */ |
| 5335 | if (fdtab[fd].state != FD_STCLOSE && |
| 5336 | epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &ev) < 0) { |
| 5337 | // perror("epoll_ctl(DEL)"); |
| 5338 | // exit(1); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5339 | } |
| 5340 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5341 | } else { |
| 5342 | /* the file-descriptor did not exist, let's add it */ |
| 5343 | if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) { |
| 5344 | // perror("epoll_ctl(ADD)"); |
| 5345 | // exit(1); |
| 5346 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5347 | } |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5348 | #endif // EPOLL_CTL_MOD_WORKAROUND |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5349 | } |
| 5350 | ((int*)PrevReadEvent)[fds] = rn; |
| 5351 | ((int*)PrevWriteEvent)[fds] = wn; |
| 5352 | } |
| 5353 | } |
| 5354 | |
| 5355 | /* now let's wait for events */ |
| 5356 | status = epoll_wait(epoll_fd, epoll_events, maxfd, next_time); |
| 5357 | tv_now(&now); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5358 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5359 | for (count = 0; count < status; count++) { |
| 5360 | fd = epoll_events[count].data.fd; |
| 5361 | |
| 5362 | if (fdtab[fd].state == FD_STCLOSE) |
| 5363 | continue; |
| 5364 | |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 5365 | if (epoll_events[count].events & ( EPOLLIN | EPOLLERR | EPOLLHUP )) { |
| 5366 | if (FD_ISSET(fd, StaticReadEvent)) |
| 5367 | fdtab[fd].read(fd); |
| 5368 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5369 | |
| 5370 | if (fdtab[fd].state == FD_STCLOSE) |
| 5371 | continue; |
| 5372 | |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 5373 | if (epoll_events[count].events & ( EPOLLOUT | EPOLLERR | EPOLLHUP )) { |
| 5374 | if (FD_ISSET(fd, StaticWriteEvent)) |
| 5375 | fdtab[fd].write(fd); |
| 5376 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5377 | } |
| 5378 | } |
| 5379 | return 1; |
| 5380 | } |
| 5381 | #endif |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5382 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5383 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5384 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5385 | #if defined(ENABLE_POLL) |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5386 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5387 | /* |
| 5388 | * Main poll() loop. |
| 5389 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5390 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5391 | /* does 3 actions : |
| 5392 | * 0 (POLL_LOOP_ACTION_INIT) : initializes necessary private structures |
| 5393 | * 1 (POLL_LOOP_ACTION_RUN) : runs the loop |
| 5394 | * 2 (POLL_LOOP_ACTION_CLEAN) : cleans up |
| 5395 | * |
| 5396 | * returns 0 if initialization failed, !0 otherwise. |
| 5397 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5398 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5399 | int poll_loop(int action) { |
| 5400 | int next_time; |
| 5401 | int status; |
| 5402 | int fd, nbfd; |
| 5403 | |
| 5404 | int fds, count; |
| 5405 | int sr, sw; |
| 5406 | unsigned rn, wn; /* read new, write new */ |
| 5407 | |
| 5408 | /* private data */ |
| 5409 | static struct pollfd *poll_events = NULL; |
| 5410 | |
| 5411 | if (action == POLL_LOOP_ACTION_INIT) { |
| 5412 | poll_events = (struct pollfd*) |
| 5413 | calloc(1, sizeof(struct pollfd) * global.maxsock); |
| 5414 | return 1; |
| 5415 | } |
| 5416 | else if (action == POLL_LOOP_ACTION_CLEAN) { |
| 5417 | if (poll_events) |
| 5418 | free(poll_events); |
| 5419 | return 1; |
| 5420 | } |
| 5421 | |
| 5422 | /* OK, it's POLL_LOOP_ACTION_RUN */ |
| 5423 | |
| 5424 | tv_now(&now); |
| 5425 | |
| 5426 | while (1) { |
| 5427 | next_time = process_runnable_tasks(); |
| 5428 | |
| 5429 | /* stop when there's no connection left and we don't allow them anymore */ |
| 5430 | if (!actconn && listeners == 0) |
| 5431 | break; |
| 5432 | |
| 5433 | #if STATTIME > 0 |
| 5434 | { |
| 5435 | int time2; |
| 5436 | time2 = stats(); |
| 5437 | next_time = MINTIME(time2, next_time); |
| 5438 | } |
| 5439 | #endif |
| 5440 | |
| 5441 | |
| 5442 | nbfd = 0; |
| 5443 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) { |
| 5444 | |
| 5445 | rn = ((int*)StaticReadEvent)[fds]; |
| 5446 | wn = ((int*)StaticWriteEvent)[fds]; |
| 5447 | |
| 5448 | if ((rn|wn)) { |
| 5449 | for (count = 0, fd = fds << INTBITS; count < (1<<INTBITS) && fd < maxfd; count++, fd++) { |
| 5450 | #define FDSETS_ARE_INT_ALIGNED |
| 5451 | #ifdef FDSETS_ARE_INT_ALIGNED |
| 5452 | |
| 5453 | #define WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
| 5454 | #ifdef WE_REALLY_NOW_THAT_FDSETS_ARE_INTS |
| 5455 | sr = (rn >> count) & 1; |
| 5456 | sw = (wn >> count) & 1; |
| 5457 | #else |
| 5458 | sr = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&rn); |
| 5459 | sw = FD_ISSET(fd&((1<<INTBITS)-1), (typeof(fd_set*))&wn); |
| 5460 | #endif |
| 5461 | #else |
| 5462 | sr = FD_ISSET(fd, StaticReadEvent); |
| 5463 | sw = FD_ISSET(fd, StaticWriteEvent); |
| 5464 | #endif |
| 5465 | if ((sr|sw)) { |
| 5466 | poll_events[nbfd].fd = fd; |
| 5467 | poll_events[nbfd].events = (sr ? POLLIN : 0) | (sw ? POLLOUT : 0); |
| 5468 | nbfd++; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5469 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5470 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5471 | } |
| 5472 | } |
| 5473 | |
| 5474 | /* now let's wait for events */ |
| 5475 | status = poll(poll_events, nbfd, next_time); |
| 5476 | tv_now(&now); |
| 5477 | |
| 5478 | for (count = 0; status > 0 && count < nbfd; count++) { |
| 5479 | fd = poll_events[count].fd; |
| 5480 | |
| 5481 | if (!poll_events[count].revents & ( POLLOUT | POLLIN | POLLERR | POLLHUP )) |
| 5482 | continue; |
| 5483 | |
| 5484 | /* ok, we found one active fd */ |
| 5485 | status--; |
| 5486 | |
| 5487 | if (fdtab[fd].state == FD_STCLOSE) |
| 5488 | continue; |
| 5489 | |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 5490 | if (poll_events[count].revents & ( POLLIN | POLLERR | POLLHUP )) { |
| 5491 | if (FD_ISSET(fd, StaticReadEvent)) |
| 5492 | fdtab[fd].read(fd); |
| 5493 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5494 | |
| 5495 | if (fdtab[fd].state == FD_STCLOSE) |
| 5496 | continue; |
| 5497 | |
Willy TARREAU | e78ae26 | 2006-01-08 01:24:12 +0100 | [diff] [blame] | 5498 | if (poll_events[count].revents & ( POLLOUT | POLLERR | POLLHUP )) { |
| 5499 | if (FD_ISSET(fd, StaticWriteEvent)) |
| 5500 | fdtab[fd].write(fd); |
| 5501 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5502 | } |
| 5503 | } |
| 5504 | return 1; |
| 5505 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5506 | #endif |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5507 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5508 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5509 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5510 | /* |
| 5511 | * Main select() loop. |
| 5512 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5513 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5514 | /* does 3 actions : |
| 5515 | * 0 (POLL_LOOP_ACTION_INIT) : initializes necessary private structures |
| 5516 | * 1 (POLL_LOOP_ACTION_RUN) : runs the loop |
| 5517 | * 2 (POLL_LOOP_ACTION_CLEAN) : cleans up |
| 5518 | * |
| 5519 | * returns 0 if initialization failed, !0 otherwise. |
| 5520 | */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5521 | |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5522 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5523 | int select_loop(int action) { |
| 5524 | int next_time; |
| 5525 | int status; |
| 5526 | int fd,i; |
| 5527 | struct timeval delta; |
| 5528 | int readnotnull, writenotnull; |
| 5529 | static fd_set *ReadEvent = NULL, *WriteEvent = NULL; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5530 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5531 | if (action == POLL_LOOP_ACTION_INIT) { |
| 5532 | ReadEvent = (fd_set *) |
| 5533 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
| 5534 | WriteEvent = (fd_set *) |
| 5535 | calloc(1, sizeof(fd_set) * (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
| 5536 | return 1; |
| 5537 | } |
| 5538 | else if (action == POLL_LOOP_ACTION_CLEAN) { |
| 5539 | if (WriteEvent) free(WriteEvent); |
| 5540 | if (ReadEvent) free(ReadEvent); |
| 5541 | return 1; |
| 5542 | } |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5543 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5544 | /* OK, it's POLL_LOOP_ACTION_RUN */ |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5545 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5546 | tv_now(&now); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5547 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5548 | while (1) { |
| 5549 | next_time = process_runnable_tasks(); |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5550 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5551 | /* stop when there's no connection left and we don't allow them anymore */ |
| 5552 | if (!actconn && listeners == 0) |
| 5553 | break; |
| 5554 | |
| 5555 | #if STATTIME > 0 |
| 5556 | { |
| 5557 | int time2; |
| 5558 | time2 = stats(); |
| 5559 | next_time = MINTIME(time2, next_time); |
| 5560 | } |
| 5561 | #endif |
| 5562 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5563 | if (next_time > 0) { /* FIXME */ |
| 5564 | /* Convert to timeval */ |
| 5565 | /* to avoid eventual select loops due to timer precision */ |
| 5566 | next_time += SCHEDULER_RESOLUTION; |
| 5567 | delta.tv_sec = next_time / 1000; |
| 5568 | delta.tv_usec = (next_time % 1000) * 1000; |
| 5569 | } |
| 5570 | else if (next_time == 0) { /* allow select to return immediately when needed */ |
| 5571 | delta.tv_sec = delta.tv_usec = 0; |
| 5572 | } |
| 5573 | |
| 5574 | |
| 5575 | /* let's restore fdset state */ |
| 5576 | |
| 5577 | readnotnull = 0; writenotnull = 0; |
| 5578 | for (i = 0; i < (maxfd + FD_SETSIZE - 1)/(8*sizeof(int)); i++) { |
| 5579 | readnotnull |= (*(((int*)ReadEvent)+i) = *(((int*)StaticReadEvent)+i)) != 0; |
| 5580 | writenotnull |= (*(((int*)WriteEvent)+i) = *(((int*)StaticWriteEvent)+i)) != 0; |
| 5581 | } |
| 5582 | |
| 5583 | // /* just a verification code, needs to be removed for performance */ |
| 5584 | // for (i=0; i<maxfd; i++) { |
| 5585 | // if (FD_ISSET(i, ReadEvent) != FD_ISSET(i, StaticReadEvent)) |
| 5586 | // abort(); |
| 5587 | // if (FD_ISSET(i, WriteEvent) != FD_ISSET(i, StaticWriteEvent)) |
| 5588 | // abort(); |
| 5589 | // |
| 5590 | // } |
| 5591 | |
| 5592 | status = select(maxfd, |
| 5593 | readnotnull ? ReadEvent : NULL, |
| 5594 | writenotnull ? WriteEvent : NULL, |
| 5595 | NULL, |
| 5596 | (next_time >= 0) ? &delta : NULL); |
| 5597 | |
| 5598 | /* this is an experiment on the separation of the select work */ |
| 5599 | // status = (readnotnull ? select(maxfd, ReadEvent, NULL, NULL, (next_time >= 0) ? &delta : NULL) : 0); |
| 5600 | // status |= (writenotnull ? select(maxfd, NULL, WriteEvent, NULL, (next_time >= 0) ? &delta : NULL) : 0); |
| 5601 | |
| 5602 | tv_now(&now); |
| 5603 | |
| 5604 | if (status > 0) { /* must proceed with events */ |
| 5605 | |
| 5606 | int fds; |
| 5607 | char count; |
willy tarreau | ad90a0c | 2005-12-18 01:09:15 +0100 | [diff] [blame] | 5608 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5609 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) |
| 5610 | if ((((int *)(ReadEvent))[fds] | ((int *)(WriteEvent))[fds]) != 0) |
| 5611 | for (count = 1<<INTBITS, fd = fds << INTBITS; count && fd < maxfd; count--, fd++) { |
| 5612 | |
| 5613 | /* if we specify read first, the accepts and zero reads will be |
| 5614 | * seen first. Moreover, system buffers will be flushed faster. |
| 5615 | */ |
| 5616 | if (fdtab[fd].state == FD_STCLOSE) |
| 5617 | continue; |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 5618 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5619 | if (FD_ISSET(fd, ReadEvent)) |
| 5620 | fdtab[fd].read(fd); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 5621 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5622 | if (FD_ISSET(fd, WriteEvent)) |
| 5623 | fdtab[fd].write(fd); |
| 5624 | } |
| 5625 | } |
| 5626 | else { |
| 5627 | // fprintf(stderr,"select returned %d, maxfd=%d\n", status, maxfd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5628 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5629 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 5630 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5631 | } |
| 5632 | |
| 5633 | |
| 5634 | #if STATTIME > 0 |
| 5635 | /* |
| 5636 | * Display proxy statistics regularly. It is designed to be called from the |
| 5637 | * select_loop(). |
| 5638 | */ |
| 5639 | int stats(void) { |
| 5640 | static int lines; |
| 5641 | static struct timeval nextevt; |
| 5642 | static struct timeval lastevt; |
| 5643 | static struct timeval starttime = {0,0}; |
| 5644 | unsigned long totaltime, deltatime; |
| 5645 | int ret; |
| 5646 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5647 | if (tv_cmp(&now, &nextevt) > 0) { |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 5648 | deltatime = (tv_diff(&lastevt, &now)?:1); |
| 5649 | totaltime = (tv_diff(&starttime, &now)?:1); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5650 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5651 | if (global.mode & MODE_STATS) { |
| 5652 | if ((lines++ % 16 == 0) && !(global.mode & MODE_LOG)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5653 | qfprintf(stderr, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5654 | "\n active total tsknew tskgood tskleft tskrght tsknsch tsklsch tskrsch\n"); |
| 5655 | if (lines>1) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5656 | qfprintf(stderr,"%07d %07d %07d %07d %07d %07d %07d %07d %07d\n", |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5657 | actconn, totalconn, |
| 5658 | stats_tsk_new, stats_tsk_good, |
| 5659 | stats_tsk_left, stats_tsk_right, |
| 5660 | stats_tsk_nsrch, stats_tsk_lsrch, stats_tsk_rsrch); |
| 5661 | } |
| 5662 | } |
| 5663 | |
| 5664 | tv_delayfrom(&nextevt, &now, STATTIME); |
| 5665 | |
| 5666 | lastevt=now; |
| 5667 | } |
| 5668 | ret = tv_remain(&now, &nextevt); |
| 5669 | return ret; |
| 5670 | } |
| 5671 | #endif |
| 5672 | |
| 5673 | |
| 5674 | /* |
| 5675 | * this function enables proxies when there are enough free sessions, |
| 5676 | * 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] | 5677 | * select_loop(). It returns the time left before next expiration event |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5678 | * during stop time, TIME_ETERNITY otherwise. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5679 | */ |
| 5680 | static int maintain_proxies(void) { |
| 5681 | struct proxy *p; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5682 | struct listener *l; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5683 | int tleft; /* time left */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5684 | |
| 5685 | p = proxy; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5686 | tleft = TIME_ETERNITY; /* infinite time */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5687 | |
| 5688 | /* if there are enough free sessions, we'll activate proxies */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5689 | if (actconn < global.maxconn) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5690 | while (p) { |
| 5691 | if (p->nbconn < p->maxconn) { |
| 5692 | if (p->state == PR_STIDLE) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5693 | for (l = p->listen; l != NULL; l = l->next) { |
| 5694 | FD_SET(l->fd, StaticReadEvent); |
| 5695 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5696 | p->state = PR_STRUN; |
| 5697 | } |
| 5698 | } |
| 5699 | else { |
| 5700 | if (p->state == PR_STRUN) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5701 | for (l = p->listen; l != NULL; l = l->next) { |
| 5702 | FD_CLR(l->fd, StaticReadEvent); |
| 5703 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5704 | p->state = PR_STIDLE; |
| 5705 | } |
| 5706 | } |
| 5707 | p = p->next; |
| 5708 | } |
| 5709 | } |
| 5710 | else { /* block all proxies */ |
| 5711 | while (p) { |
| 5712 | if (p->state == PR_STRUN) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5713 | for (l = p->listen; l != NULL; l = l->next) { |
| 5714 | FD_CLR(l->fd, StaticReadEvent); |
| 5715 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5716 | p->state = PR_STIDLE; |
| 5717 | } |
| 5718 | p = p->next; |
| 5719 | } |
| 5720 | } |
| 5721 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5722 | if (stopping) { |
| 5723 | p = proxy; |
| 5724 | while (p) { |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5725 | if (p->state != PR_STSTOPPED) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5726 | int t; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 5727 | t = tv_remain2(&now, &p->stop_time); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5728 | if (t == 0) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5729 | Warning("Proxy %s stopped.\n", p->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5730 | send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5731 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 5732 | for (l = p->listen; l != NULL; l = l->next) { |
| 5733 | fd_delete(l->fd); |
| 5734 | listeners--; |
| 5735 | } |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5736 | p->state = PR_STSTOPPED; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5737 | } |
| 5738 | else { |
| 5739 | tleft = MINTIME(t, tleft); |
| 5740 | } |
| 5741 | } |
| 5742 | p = p->next; |
| 5743 | } |
| 5744 | } |
| 5745 | return tleft; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5746 | } |
| 5747 | |
| 5748 | /* |
| 5749 | * 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] | 5750 | * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace |
| 5751 | * 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] | 5752 | */ |
| 5753 | static void soft_stop(void) { |
| 5754 | struct proxy *p; |
| 5755 | |
| 5756 | stopping = 1; |
| 5757 | p = proxy; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5758 | tv_now(&now); /* else, the old time before select will be used */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5759 | while (p) { |
willy tarreau | 808b4e6 | 2006-01-20 19:46:44 +0100 | [diff] [blame] | 5760 | if (p->state != PR_STSTOPPED && p->state != PR_STPAUSED) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5761 | Warning("Stopping proxy %s in %d ms.\n", p->id, p->grace); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5762 | 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] | 5763 | tv_delayfrom(&p->stop_time, &now, p->grace); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5764 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5765 | p = p->next; |
| 5766 | } |
| 5767 | } |
| 5768 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5769 | static void pause_proxy(struct proxy *p) { |
| 5770 | struct listener *l; |
| 5771 | for (l = p->listen; l != NULL; l = l->next) { |
| 5772 | shutdown(l->fd, SHUT_RD); |
| 5773 | FD_CLR(l->fd, StaticReadEvent); |
| 5774 | p->state = PR_STPAUSED; |
| 5775 | } |
| 5776 | } |
| 5777 | |
| 5778 | /* |
| 5779 | * This function temporarily disables listening so that another new instance |
| 5780 | * 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] | 5781 | * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5782 | * the proxy, or a SIGTTIN can be sent to listen again. |
| 5783 | */ |
| 5784 | static void pause_proxies(void) { |
| 5785 | struct proxy *p; |
| 5786 | |
| 5787 | p = proxy; |
| 5788 | tv_now(&now); /* else, the old time before select will be used */ |
| 5789 | while (p) { |
| 5790 | if (p->state != PR_STSTOPPED && p->state != PR_STPAUSED) { |
| 5791 | Warning("Pausing proxy %s.\n", p->id); |
| 5792 | send_log(p, LOG_WARNING, "Pausing proxy %s.\n", p->id); |
| 5793 | pause_proxy(p); |
| 5794 | } |
| 5795 | p = p->next; |
| 5796 | } |
| 5797 | } |
| 5798 | |
| 5799 | |
| 5800 | /* |
| 5801 | * This function reactivates listening. This can be used after a call to |
| 5802 | * sig_pause(), for example when a new instance has failed starting up. |
| 5803 | * It is designed to be called upon reception of a SIGTTIN. |
| 5804 | */ |
| 5805 | static void listen_proxies(void) { |
| 5806 | struct proxy *p; |
| 5807 | struct listener *l; |
| 5808 | |
| 5809 | p = proxy; |
| 5810 | tv_now(&now); /* else, the old time before select will be used */ |
| 5811 | while (p) { |
| 5812 | if (p->state == PR_STPAUSED) { |
| 5813 | Warning("Enabling proxy %s.\n", p->id); |
| 5814 | send_log(p, LOG_WARNING, "Enabling proxy %s.\n", p->id); |
| 5815 | |
| 5816 | for (l = p->listen; l != NULL; l = l->next) { |
| 5817 | if (listen(l->fd, p->maxconn) == 0) { |
| 5818 | if (actconn < global.maxconn && p->nbconn < p->maxconn) { |
| 5819 | FD_SET(l->fd, StaticReadEvent); |
| 5820 | p->state = PR_STRUN; |
| 5821 | } |
| 5822 | else |
| 5823 | p->state = PR_STIDLE; |
| 5824 | } else { |
willy tarreau | cb2e562 | 2006-01-29 21:55:30 +0100 | [diff] [blame] | 5825 | int port; |
| 5826 | |
| 5827 | if (l->addr.ss_family == AF_INET6) |
| 5828 | port = ntohs(((struct sockaddr_in6 *)(&l->addr))->sin6_port); |
| 5829 | else |
| 5830 | port = ntohs(((struct sockaddr_in *)(&l->addr))->sin_port); |
| 5831 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5832 | Warning("Port %d busy while trying to enable proxy %s.\n", |
willy tarreau | cb2e562 | 2006-01-29 21:55:30 +0100 | [diff] [blame] | 5833 | port, p->id); |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5834 | 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] | 5835 | port, p->id); |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5836 | /* Another port might have been enabled. Let's stop everything. */ |
| 5837 | pause_proxy(p); |
| 5838 | break; |
| 5839 | } |
| 5840 | } |
| 5841 | } |
| 5842 | p = p->next; |
| 5843 | } |
| 5844 | } |
| 5845 | |
| 5846 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5847 | /* |
| 5848 | * upon SIGUSR1, let's have a soft stop. |
| 5849 | */ |
| 5850 | void sig_soft_stop(int sig) { |
| 5851 | soft_stop(); |
| 5852 | signal(sig, SIG_IGN); |
| 5853 | } |
| 5854 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5855 | /* |
| 5856 | * upon SIGTTOU, we pause everything |
| 5857 | */ |
| 5858 | void sig_pause(int sig) { |
| 5859 | pause_proxies(); |
| 5860 | signal(sig, sig_pause); |
| 5861 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5862 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 5863 | /* |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 5864 | * upon SIGTTIN, let's have a soft stop. |
| 5865 | */ |
| 5866 | void sig_listen(int sig) { |
| 5867 | listen_proxies(); |
| 5868 | signal(sig, sig_listen); |
| 5869 | } |
| 5870 | |
| 5871 | /* |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 5872 | * this function dumps every server's state when the process receives SIGHUP. |
| 5873 | */ |
| 5874 | void sig_dump_state(int sig) { |
| 5875 | struct proxy *p = proxy; |
| 5876 | |
| 5877 | Warning("SIGHUP received, dumping servers states.\n"); |
| 5878 | while (p) { |
| 5879 | struct server *s = p->srv; |
| 5880 | |
| 5881 | send_log(p, LOG_NOTICE, "SIGUP received, dumping servers states.\n"); |
| 5882 | while (s) { |
| 5883 | if (s->state & SRV_RUNNING) { |
| 5884 | Warning("SIGHUP: server %s/%s is UP.\n", p->id, s->id); |
| 5885 | send_log(p, LOG_NOTICE, "SIGUP: server %s/%s is UP.\n", p->id, s->id); |
| 5886 | } |
| 5887 | else { |
| 5888 | Warning("SIGHUP: server %s/%s is DOWN.\n", p->id, s->id); |
| 5889 | send_log(p, LOG_NOTICE, "SIGHUP: server %s/%s is DOWN.\n", p->id, s->id); |
| 5890 | } |
| 5891 | s = s->next; |
| 5892 | } |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 5893 | |
| 5894 | if (find_server(p) == NULL) { |
| 5895 | Warning("SIGHUP: proxy %s has no server available !\n", p); |
| 5896 | send_log(p, LOG_NOTICE, "SIGHUP: proxy %s has no server available !\n", p); |
| 5897 | } |
| 5898 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 5899 | p = p->next; |
| 5900 | } |
| 5901 | signal(sig, sig_dump_state); |
| 5902 | } |
| 5903 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5904 | void dump(int sig) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5905 | struct task *t, *tnext; |
| 5906 | struct session *s; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5907 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5908 | tnext = ((struct task *)LIST_HEAD(wait_queue))->next; |
| 5909 | while ((t = tnext) != LIST_HEAD(wait_queue)) { /* we haven't looped ? */ |
| 5910 | tnext = t->next; |
| 5911 | s = t->context; |
| 5912 | qfprintf(stderr,"[dump] wq: task %p, still %ld ms, " |
| 5913 | "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, " |
| 5914 | "req=%d, rep=%d, clifd=%d\n", |
| 5915 | s, tv_remain(&now, &t->expire), |
| 5916 | s->cli_state, |
| 5917 | s->srv_state, |
| 5918 | FD_ISSET(s->cli_fd, StaticReadEvent), |
| 5919 | FD_ISSET(s->cli_fd, StaticWriteEvent), |
| 5920 | FD_ISSET(s->srv_fd, StaticReadEvent), |
| 5921 | FD_ISSET(s->srv_fd, StaticWriteEvent), |
| 5922 | s->req->l, s->rep?s->rep->l:0, s->cli_fd |
| 5923 | ); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5924 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 5925 | } |
| 5926 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 5927 | #ifdef DEBUG_MEMORY |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 5928 | static void fast_stop(void) |
| 5929 | { |
| 5930 | struct proxy *p; |
| 5931 | p = proxy; |
| 5932 | while (p) { |
| 5933 | p->grace = 0; |
| 5934 | p = p->next; |
| 5935 | } |
| 5936 | soft_stop(); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5937 | } |
| 5938 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 5939 | void sig_int(int sig) { |
| 5940 | /* This would normally be a hard stop, |
| 5941 | but we want to be sure about deallocation, |
| 5942 | and so on, so we do a soft stop with |
| 5943 | 0 GRACE time |
| 5944 | */ |
| 5945 | fast_stop(); |
| 5946 | /* If we are killed twice, we decide to die*/ |
| 5947 | signal(sig, SIG_DFL); |
| 5948 | } |
| 5949 | |
| 5950 | void sig_term(int sig) { |
| 5951 | /* This would normally be a hard stop, |
| 5952 | but we want to be sure about deallocation, |
| 5953 | and so on, so we do a soft stop with |
| 5954 | 0 GRACE time |
| 5955 | */ |
| 5956 | fast_stop(); |
| 5957 | /* If we are killed twice, we decide to die*/ |
| 5958 | signal(sig, SIG_DFL); |
| 5959 | } |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 5960 | #endif |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 5961 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 5962 | /* returns the pointer to an error in the replacement string, or NULL if OK */ |
| 5963 | 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] | 5964 | struct hdr_exp *exp; |
| 5965 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 5966 | if (replace != NULL) { |
| 5967 | char *err; |
| 5968 | err = check_replace_string(replace); |
| 5969 | if (err) |
| 5970 | return err; |
| 5971 | } |
| 5972 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 5973 | while (*head != NULL) |
| 5974 | head = &(*head)->next; |
| 5975 | |
| 5976 | exp = calloc(1, sizeof(struct hdr_exp)); |
| 5977 | |
| 5978 | exp->preg = preg; |
| 5979 | exp->replace = replace; |
| 5980 | exp->action = action; |
| 5981 | *head = exp; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 5982 | |
| 5983 | return NULL; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 5984 | } |
| 5985 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5986 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5987 | /* |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5988 | * 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] | 5989 | */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5990 | int cfg_parse_global(char *file, int linenum, char **args) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5991 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5992 | if (!strcmp(args[0], "global")) { /* new section */ |
| 5993 | /* no option, nothing special to do */ |
| 5994 | return 0; |
| 5995 | } |
| 5996 | else if (!strcmp(args[0], "daemon")) { |
| 5997 | global.mode |= MODE_DAEMON; |
| 5998 | } |
| 5999 | else if (!strcmp(args[0], "debug")) { |
| 6000 | global.mode |= MODE_DEBUG; |
| 6001 | } |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 6002 | else if (!strcmp(args[0], "noepoll")) { |
| 6003 | cfg_polling_mechanism &= ~POLL_USE_EPOLL; |
| 6004 | } |
| 6005 | else if (!strcmp(args[0], "nopoll")) { |
| 6006 | cfg_polling_mechanism &= ~POLL_USE_POLL; |
| 6007 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6008 | else if (!strcmp(args[0], "quiet")) { |
| 6009 | global.mode |= MODE_QUIET; |
| 6010 | } |
| 6011 | else if (!strcmp(args[0], "stats")) { |
| 6012 | global.mode |= MODE_STATS; |
| 6013 | } |
| 6014 | else if (!strcmp(args[0], "uid")) { |
| 6015 | if (global.uid != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6016 | 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] | 6017 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6018 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6019 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6020 | 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] | 6021 | return -1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6022 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6023 | global.uid = atol(args[1]); |
| 6024 | } |
| 6025 | else if (!strcmp(args[0], "gid")) { |
| 6026 | if (global.gid != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6027 | 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] | 6028 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6029 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6030 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6031 | 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] | 6032 | return -1; |
| 6033 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6034 | global.gid = atol(args[1]); |
| 6035 | } |
| 6036 | else if (!strcmp(args[0], "nbproc")) { |
| 6037 | if (global.nbproc != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6038 | 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] | 6039 | return 0; |
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 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6042 | 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] | 6043 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6044 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6045 | global.nbproc = atol(args[1]); |
| 6046 | } |
| 6047 | else if (!strcmp(args[0], "maxconn")) { |
| 6048 | if (global.maxconn != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6049 | 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] | 6050 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6051 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6052 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6053 | 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] | 6054 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6055 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6056 | global.maxconn = atol(args[1]); |
| 6057 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 6058 | else if (!strcmp(args[0], "ulimit-n")) { |
| 6059 | if (global.rlimit_nofile != 0) { |
| 6060 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
| 6061 | return 0; |
| 6062 | } |
| 6063 | if (*(args[1]) == 0) { |
| 6064 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
| 6065 | return -1; |
| 6066 | } |
| 6067 | global.rlimit_nofile = atol(args[1]); |
| 6068 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6069 | else if (!strcmp(args[0], "chroot")) { |
| 6070 | if (global.chroot != NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6071 | 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] | 6072 | return 0; |
| 6073 | } |
| 6074 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6075 | 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] | 6076 | return -1; |
| 6077 | } |
| 6078 | global.chroot = strdup(args[1]); |
| 6079 | } |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 6080 | else if (!strcmp(args[0], "pidfile")) { |
| 6081 | if (global.pidfile != NULL) { |
| 6082 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
| 6083 | return 0; |
| 6084 | } |
| 6085 | if (*(args[1]) == 0) { |
| 6086 | Alert("parsing [%s:%d] : '%s' expects a file name as an argument.\n", file, linenum, args[0]); |
| 6087 | return -1; |
| 6088 | } |
| 6089 | global.pidfile = strdup(args[1]); |
| 6090 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6091 | else if (!strcmp(args[0], "log")) { /* syslog server address */ |
| 6092 | struct sockaddr_in *sa; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6093 | int facility, level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6094 | |
| 6095 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6096 | 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] | 6097 | return -1; |
| 6098 | } |
| 6099 | |
| 6100 | for (facility = 0; facility < NB_LOG_FACILITIES; facility++) |
| 6101 | if (!strcmp(log_facilities[facility], args[2])) |
| 6102 | break; |
| 6103 | |
| 6104 | if (facility >= NB_LOG_FACILITIES) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6105 | 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] | 6106 | exit(1); |
| 6107 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6108 | |
| 6109 | level = 7; /* max syslog level = debug */ |
| 6110 | if (*(args[3])) { |
| 6111 | while (level >= 0 && strcmp(log_levels[level], args[3])) |
| 6112 | level--; |
| 6113 | if (level < 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6114 | 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] | 6115 | exit(1); |
| 6116 | } |
| 6117 | } |
| 6118 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6119 | sa = str2sa(args[1]); |
| 6120 | if (!sa->sin_port) |
| 6121 | sa->sin_port = htons(SYSLOG_PORT); |
| 6122 | |
| 6123 | if (global.logfac1 == -1) { |
| 6124 | global.logsrv1 = *sa; |
| 6125 | global.logfac1 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6126 | global.loglev1 = level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6127 | } |
| 6128 | else if (global.logfac2 == -1) { |
| 6129 | global.logsrv2 = *sa; |
| 6130 | global.logfac2 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6131 | global.loglev2 = level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6132 | } |
| 6133 | else { |
| 6134 | Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum); |
| 6135 | return -1; |
| 6136 | } |
| 6137 | |
| 6138 | } |
| 6139 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6140 | 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] | 6141 | return -1; |
| 6142 | } |
| 6143 | return 0; |
| 6144 | } |
| 6145 | |
| 6146 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6147 | void init_default_instance() { |
| 6148 | memset(&defproxy, 0, sizeof(defproxy)); |
| 6149 | defproxy.mode = PR_MODE_TCP; |
| 6150 | defproxy.state = PR_STNEW; |
| 6151 | defproxy.maxconn = cfg_maxpconn; |
| 6152 | defproxy.conn_retries = CONN_RETRIES; |
| 6153 | defproxy.logfac1 = defproxy.logfac2 = -1; /* log disabled */ |
| 6154 | } |
| 6155 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6156 | /* |
| 6157 | * parse a line in a <listen> section. Returns 0 if OK, -1 if error. |
| 6158 | */ |
| 6159 | int cfg_parse_listen(char *file, int linenum, char **args) { |
| 6160 | static struct proxy *curproxy = NULL; |
| 6161 | struct server *newsrv = NULL; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 6162 | char *err; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6163 | int rc; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6164 | |
| 6165 | if (!strcmp(args[0], "listen")) { /* new proxy */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6166 | if (!*args[1]) { |
| 6167 | Alert("parsing [%s:%d] : '%s' expects an <id> argument and\n" |
| 6168 | " optionnally supports [addr1]:port1[-end1]{,[addr]:port[-end]}...\n", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6169 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6170 | return -1; |
| 6171 | } |
| 6172 | |
| 6173 | if ((curproxy = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6174 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6175 | return -1; |
| 6176 | } |
| 6177 | curproxy->next = proxy; |
| 6178 | proxy = curproxy; |
| 6179 | curproxy->id = strdup(args[1]); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 6180 | |
| 6181 | /* parse the listener address if any */ |
| 6182 | if (*args[2]) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6183 | curproxy->listen = str2listener(args[2], curproxy->listen); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 6184 | if (!curproxy->listen) |
| 6185 | return -1; |
| 6186 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6187 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6188 | /* set default values */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6189 | curproxy->state = defproxy.state; |
| 6190 | curproxy->maxconn = defproxy.maxconn; |
| 6191 | curproxy->conn_retries = defproxy.conn_retries; |
| 6192 | curproxy->options = defproxy.options; |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6193 | |
| 6194 | if (defproxy.check_req) |
| 6195 | curproxy->check_req = strdup(defproxy.check_req); |
| 6196 | curproxy->check_len = defproxy.check_len; |
| 6197 | |
| 6198 | if (defproxy.cookie_name) |
| 6199 | curproxy->cookie_name = strdup(defproxy.cookie_name); |
| 6200 | curproxy->cookie_len = defproxy.cookie_len; |
| 6201 | |
| 6202 | if (defproxy.capture_name) |
| 6203 | curproxy->capture_name = strdup(defproxy.capture_name); |
| 6204 | curproxy->capture_namelen = defproxy.capture_namelen; |
| 6205 | curproxy->capture_len = defproxy.capture_len; |
| 6206 | |
| 6207 | if (defproxy.errmsg.msg400) |
| 6208 | curproxy->errmsg.msg400 = strdup(defproxy.errmsg.msg400); |
| 6209 | curproxy->errmsg.len400 = defproxy.errmsg.len400; |
| 6210 | |
| 6211 | if (defproxy.errmsg.msg403) |
| 6212 | curproxy->errmsg.msg403 = strdup(defproxy.errmsg.msg403); |
| 6213 | curproxy->errmsg.len403 = defproxy.errmsg.len403; |
| 6214 | |
| 6215 | if (defproxy.errmsg.msg408) |
| 6216 | curproxy->errmsg.msg408 = strdup(defproxy.errmsg.msg408); |
| 6217 | curproxy->errmsg.len408 = defproxy.errmsg.len408; |
| 6218 | |
| 6219 | if (defproxy.errmsg.msg500) |
| 6220 | curproxy->errmsg.msg500 = strdup(defproxy.errmsg.msg500); |
| 6221 | curproxy->errmsg.len500 = defproxy.errmsg.len500; |
| 6222 | |
| 6223 | if (defproxy.errmsg.msg502) |
| 6224 | curproxy->errmsg.msg502 = strdup(defproxy.errmsg.msg502); |
| 6225 | curproxy->errmsg.len502 = defproxy.errmsg.len502; |
| 6226 | |
| 6227 | if (defproxy.errmsg.msg503) |
| 6228 | curproxy->errmsg.msg503 = strdup(defproxy.errmsg.msg503); |
| 6229 | curproxy->errmsg.len503 = defproxy.errmsg.len503; |
| 6230 | |
| 6231 | if (defproxy.errmsg.msg504) |
| 6232 | curproxy->errmsg.msg504 = strdup(defproxy.errmsg.msg504); |
| 6233 | curproxy->errmsg.len504 = defproxy.errmsg.len504; |
| 6234 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6235 | curproxy->clitimeout = defproxy.clitimeout; |
| 6236 | curproxy->contimeout = defproxy.contimeout; |
| 6237 | curproxy->srvtimeout = defproxy.srvtimeout; |
| 6238 | curproxy->mode = defproxy.mode; |
| 6239 | curproxy->logfac1 = defproxy.logfac1; |
| 6240 | curproxy->logsrv1 = defproxy.logsrv1; |
| 6241 | curproxy->loglev1 = defproxy.loglev1; |
| 6242 | curproxy->logfac2 = defproxy.logfac2; |
| 6243 | curproxy->logsrv2 = defproxy.logsrv2; |
| 6244 | curproxy->loglev2 = defproxy.loglev2; |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 6245 | curproxy->to_log = defproxy.to_log & ~LW_COOKIE & ~LW_REQHDR & ~ LW_RSPHDR; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6246 | curproxy->grace = defproxy.grace; |
| 6247 | curproxy->source_addr = defproxy.source_addr; |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 6248 | curproxy->mon_net = defproxy.mon_net; |
| 6249 | curproxy->mon_mask = defproxy.mon_mask; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6250 | return 0; |
| 6251 | } |
| 6252 | 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] | 6253 | /* some variables may have already been initialized earlier */ |
| 6254 | if (defproxy.check_req) free(defproxy.check_req); |
| 6255 | if (defproxy.cookie_name) free(defproxy.cookie_name); |
| 6256 | if (defproxy.capture_name) free(defproxy.capture_name); |
| 6257 | if (defproxy.errmsg.msg400) free(defproxy.errmsg.msg400); |
| 6258 | if (defproxy.errmsg.msg403) free(defproxy.errmsg.msg403); |
| 6259 | if (defproxy.errmsg.msg408) free(defproxy.errmsg.msg408); |
| 6260 | if (defproxy.errmsg.msg500) free(defproxy.errmsg.msg500); |
| 6261 | if (defproxy.errmsg.msg502) free(defproxy.errmsg.msg502); |
| 6262 | if (defproxy.errmsg.msg503) free(defproxy.errmsg.msg503); |
| 6263 | if (defproxy.errmsg.msg504) free(defproxy.errmsg.msg504); |
| 6264 | |
| 6265 | init_default_instance(); |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6266 | curproxy = &defproxy; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6267 | return 0; |
| 6268 | } |
| 6269 | else if (curproxy == NULL) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6270 | Alert("parsing [%s:%d] : 'listen' or 'defaults' expected.\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6271 | return -1; |
| 6272 | } |
| 6273 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6274 | if (!strcmp(args[0], "bind")) { /* new listen addresses */ |
| 6275 | if (curproxy == &defproxy) { |
| 6276 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6277 | return -1; |
| 6278 | } |
| 6279 | |
| 6280 | if (strchr(args[1], ':') == NULL) { |
| 6281 | Alert("parsing [%s:%d] : '%s' expects [addr1]:port1[-end1]{,[addr]:port[-end]}... as arguments.\n", |
| 6282 | file, linenum, args[0]); |
| 6283 | return -1; |
| 6284 | } |
| 6285 | curproxy->listen = str2listener(args[1], curproxy->listen); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 6286 | if (!curproxy->listen) |
| 6287 | return -1; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6288 | return 0; |
| 6289 | } |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 6290 | else if (!strcmp(args[0], "monitor-net")) { /* set the range of IPs to ignore */ |
| 6291 | if (!*args[1] || !str2net(args[1], &curproxy->mon_net, &curproxy->mon_mask)) { |
| 6292 | Alert("parsing [%s:%d] : '%s' expects address[/mask].\n", |
| 6293 | file, linenum, args[0]); |
| 6294 | return -1; |
| 6295 | } |
| 6296 | /* flush useless bits */ |
| 6297 | curproxy->mon_net.s_addr &= curproxy->mon_mask.s_addr; |
| 6298 | return 0; |
| 6299 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6300 | else if (!strcmp(args[0], "mode")) { /* sets the proxy mode */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6301 | if (!strcmp(args[1], "http")) curproxy->mode = PR_MODE_HTTP; |
| 6302 | else if (!strcmp(args[1], "tcp")) curproxy->mode = PR_MODE_TCP; |
| 6303 | else if (!strcmp(args[1], "health")) curproxy->mode = PR_MODE_HEALTH; |
| 6304 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6305 | 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] | 6306 | return -1; |
| 6307 | } |
| 6308 | } |
| 6309 | else if (!strcmp(args[0], "disabled")) { /* disables this proxy */ |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 6310 | curproxy->state = PR_STSTOPPED; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6311 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6312 | else if (!strcmp(args[0], "enabled")) { /* enables this proxy (used to revert a disabled default) */ |
| 6313 | curproxy->state = PR_STNEW; |
| 6314 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6315 | else if (!strcmp(args[0], "cookie")) { /* cookie name */ |
| 6316 | int cur_arg; |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6317 | // if (curproxy == &defproxy) { |
| 6318 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6319 | // return -1; |
| 6320 | // } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6321 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6322 | if (curproxy->cookie_name != NULL) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6323 | // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n", |
| 6324 | // file, linenum); |
| 6325 | // return 0; |
| 6326 | free(curproxy->cookie_name); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6327 | } |
| 6328 | |
| 6329 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6330 | Alert("parsing [%s:%d] : '%s' expects <cookie_name> as argument.\n", |
| 6331 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6332 | return -1; |
| 6333 | } |
| 6334 | curproxy->cookie_name = strdup(args[1]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6335 | curproxy->cookie_len = strlen(curproxy->cookie_name); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6336 | |
| 6337 | cur_arg = 2; |
| 6338 | while (*(args[cur_arg])) { |
| 6339 | if (!strcmp(args[cur_arg], "rewrite")) { |
| 6340 | curproxy->options |= PR_O_COOK_RW; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6341 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6342 | else if (!strcmp(args[cur_arg], "indirect")) { |
| 6343 | curproxy->options |= PR_O_COOK_IND; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6344 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6345 | else if (!strcmp(args[cur_arg], "insert")) { |
| 6346 | curproxy->options |= PR_O_COOK_INS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6347 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 6348 | else if (!strcmp(args[cur_arg], "nocache")) { |
| 6349 | curproxy->options |= PR_O_COOK_NOC; |
| 6350 | } |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 6351 | else if (!strcmp(args[cur_arg], "postonly")) { |
| 6352 | curproxy->options |= PR_O_COOK_POST; |
| 6353 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6354 | else if (!strcmp(args[cur_arg], "prefix")) { |
| 6355 | curproxy->options |= PR_O_COOK_PFX; |
| 6356 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6357 | else { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6358 | 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] | 6359 | file, linenum, args[0]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6360 | return -1; |
| 6361 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6362 | cur_arg++; |
| 6363 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6364 | if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_IND))) { |
| 6365 | Alert("parsing [%s:%d] : cookie 'rewrite' and 'indirect' modes are incompatible.\n", |
| 6366 | file, linenum); |
| 6367 | return -1; |
| 6368 | } |
| 6369 | |
| 6370 | if (!POWEROF2(curproxy->options & (PR_O_COOK_RW|PR_O_COOK_INS|PR_O_COOK_PFX))) { |
| 6371 | 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] | 6372 | file, linenum); |
| 6373 | return -1; |
| 6374 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 6375 | }/* end else if (!strcmp(args[0], "cookie")) */ |
| 6376 | else if (!strcmp(args[0], "appsession")) { /* cookie name */ |
| 6377 | // if (curproxy == &defproxy) { |
| 6378 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6379 | // return -1; |
| 6380 | // } |
| 6381 | |
| 6382 | if (curproxy->appsession_name != NULL) { |
| 6383 | // Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n", |
| 6384 | // file, linenum); |
| 6385 | // return 0; |
| 6386 | free(curproxy->appsession_name); |
| 6387 | } |
| 6388 | |
| 6389 | if (*(args[5]) == 0) { |
| 6390 | Alert("parsing [%s:%d] : '%s' expects 'appsession' <cookie_name> 'len' <len> 'timeout' <timeout>.\n", |
| 6391 | file, linenum, args[0]); |
| 6392 | return -1; |
| 6393 | } |
| 6394 | have_appsession = 1; |
| 6395 | curproxy->appsession_name = strdup(args[1]); |
| 6396 | curproxy->appsession_name_len = strlen(curproxy->appsession_name); |
| 6397 | curproxy->appsession_len = atoi(args[3]); |
| 6398 | curproxy->appsession_timeout = atoi(args[5]); |
| 6399 | rc = chtbl_init(&(curproxy->htbl_proxy), TBLSIZ, hashpjw, match_str, destroy); |
| 6400 | if (rc) { |
| 6401 | Alert("Error Init Appsession Hashtable.\n"); |
| 6402 | return -1; |
| 6403 | } |
| 6404 | } /* Url App Session */ |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 6405 | else if (!strcmp(args[0], "capture")) { |
| 6406 | if (!strcmp(args[1], "cookie")) { /* name of a cookie to capture */ |
| 6407 | // if (curproxy == &defproxy) { |
| 6408 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6409 | // return -1; |
| 6410 | // } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6411 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 6412 | if (curproxy->capture_name != NULL) { |
| 6413 | // Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", |
| 6414 | // file, linenum, args[0]); |
| 6415 | // return 0; |
| 6416 | free(curproxy->capture_name); |
| 6417 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6418 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 6419 | if (*(args[4]) == 0) { |
| 6420 | Alert("parsing [%s:%d] : '%s' expects 'cookie' <cookie_name> 'len' <len>.\n", |
| 6421 | file, linenum, args[0]); |
| 6422 | return -1; |
| 6423 | } |
| 6424 | curproxy->capture_name = strdup(args[2]); |
| 6425 | curproxy->capture_namelen = strlen(curproxy->capture_name); |
| 6426 | curproxy->capture_len = atol(args[4]); |
| 6427 | if (curproxy->capture_len >= CAPTURE_LEN) { |
| 6428 | Warning("parsing [%s:%d] : truncating capture length to %d bytes.\n", |
| 6429 | file, linenum, CAPTURE_LEN - 1); |
| 6430 | curproxy->capture_len = CAPTURE_LEN - 1; |
| 6431 | } |
| 6432 | curproxy->to_log |= LW_COOKIE; |
| 6433 | } |
| 6434 | else if (!strcmp(args[1], "request") && !strcmp(args[2], "header")) { |
| 6435 | struct cap_hdr *hdr; |
| 6436 | |
| 6437 | if (curproxy == &defproxy) { |
| 6438 | Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]); |
| 6439 | return -1; |
| 6440 | } |
| 6441 | |
| 6442 | if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) { |
| 6443 | Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n", |
| 6444 | file, linenum, args[0], args[1]); |
| 6445 | return -1; |
| 6446 | } |
| 6447 | |
| 6448 | hdr = calloc(sizeof(struct cap_hdr), 1); |
| 6449 | hdr->next = curproxy->req_cap; |
| 6450 | hdr->name = strdup(args[3]); |
| 6451 | hdr->namelen = strlen(args[3]); |
| 6452 | hdr->len = atol(args[5]); |
| 6453 | hdr->index = curproxy->nb_req_cap++; |
| 6454 | curproxy->req_cap = hdr; |
| 6455 | curproxy->to_log |= LW_REQHDR; |
| 6456 | } |
| 6457 | else if (!strcmp(args[1], "response") && !strcmp(args[2], "header")) { |
| 6458 | struct cap_hdr *hdr; |
| 6459 | |
| 6460 | if (curproxy == &defproxy) { |
| 6461 | Alert("parsing [%s:%d] : '%s %s' not allowed in 'defaults' section.\n", file, linenum, args[0], args[1]); |
| 6462 | return -1; |
| 6463 | } |
| 6464 | |
| 6465 | if (*(args[3]) == 0 || strcmp(args[4], "len") != 0 || *(args[5]) == 0) { |
| 6466 | Alert("parsing [%s:%d] : '%s %s' expects 'header' <header_name> 'len' <len>.\n", |
| 6467 | file, linenum, args[0], args[1]); |
| 6468 | return -1; |
| 6469 | } |
| 6470 | hdr = calloc(sizeof(struct cap_hdr), 1); |
| 6471 | hdr->next = curproxy->rsp_cap; |
| 6472 | hdr->name = strdup(args[3]); |
| 6473 | hdr->namelen = strlen(args[3]); |
| 6474 | hdr->len = atol(args[5]); |
| 6475 | hdr->index = curproxy->nb_rsp_cap++; |
| 6476 | curproxy->rsp_cap = hdr; |
| 6477 | curproxy->to_log |= LW_RSPHDR; |
| 6478 | } |
| 6479 | else { |
| 6480 | 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] | 6481 | file, linenum, args[0]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6482 | return -1; |
| 6483 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6484 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6485 | else if (!strcmp(args[0], "contimeout")) { /* connect timeout */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6486 | if (curproxy->contimeout != defproxy.contimeout) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6487 | 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] | 6488 | return 0; |
| 6489 | } |
| 6490 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6491 | Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n", |
| 6492 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6493 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6494 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6495 | curproxy->contimeout = atol(args[1]); |
| 6496 | } |
| 6497 | else if (!strcmp(args[0], "clitimeout")) { /* client timeout */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6498 | if (curproxy->clitimeout != defproxy.clitimeout) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6499 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", |
| 6500 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6501 | return 0; |
| 6502 | } |
| 6503 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6504 | Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n", |
| 6505 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6506 | return -1; |
| 6507 | } |
| 6508 | curproxy->clitimeout = atol(args[1]); |
| 6509 | } |
| 6510 | else if (!strcmp(args[0], "srvtimeout")) { /* server timeout */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6511 | if (curproxy->srvtimeout != defproxy.srvtimeout) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6512 | 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] | 6513 | return 0; |
| 6514 | } |
| 6515 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6516 | Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n", |
| 6517 | file, linenum, args[0]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6518 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6519 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6520 | curproxy->srvtimeout = atol(args[1]); |
| 6521 | } |
| 6522 | else if (!strcmp(args[0], "retries")) { /* connection retries */ |
| 6523 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6524 | Alert("parsing [%s:%d] : '%s' expects an integer argument (dispatch counts for one).\n", |
| 6525 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6526 | return -1; |
| 6527 | } |
| 6528 | curproxy->conn_retries = atol(args[1]); |
| 6529 | } |
| 6530 | else if (!strcmp(args[0], "option")) { |
| 6531 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6532 | 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] | 6533 | return -1; |
| 6534 | } |
| 6535 | if (!strcmp(args[1], "redispatch")) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6536 | /* enable reconnections to dispatch */ |
| 6537 | curproxy->options |= PR_O_REDISP; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 6538 | #ifdef TPROXY |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6539 | else if (!strcmp(args[1], "transparent")) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6540 | /* enable transparent proxy connections */ |
| 6541 | curproxy->options |= PR_O_TRANSP; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6542 | #endif |
| 6543 | else if (!strcmp(args[1], "keepalive")) |
| 6544 | /* enable keep-alive */ |
| 6545 | curproxy->options |= PR_O_KEEPALIVE; |
| 6546 | else if (!strcmp(args[1], "forwardfor")) |
| 6547 | /* insert x-forwarded-for field */ |
| 6548 | curproxy->options |= PR_O_FWDFOR; |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 6549 | else if (!strcmp(args[1], "logasap")) |
| 6550 | /* log as soon as possible, without waiting for the session to complete */ |
| 6551 | curproxy->options |= PR_O_LOGASAP; |
| 6552 | else if (!strcmp(args[1], "httpclose")) |
| 6553 | /* force connection: close in both directions in HTTP mode */ |
| 6554 | curproxy->options |= PR_O_HTTP_CLOSE; |
willy tarreau | 97f5857 | 2005-12-18 00:53:44 +0100 | [diff] [blame] | 6555 | else if (!strcmp(args[1], "checkcache")) |
| 6556 | /* require examination of cacheability of the 'set-cookie' field */ |
| 6557 | curproxy->options |= PR_O_CHK_CACHE; |
willy tarreau | c1cae63 | 2005-12-17 14:12:23 +0100 | [diff] [blame] | 6558 | else if (!strcmp(args[1], "httplog")) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6559 | /* generate a complete HTTP log */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 6560 | 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] | 6561 | else if (!strcmp(args[1], "tcplog")) |
| 6562 | /* generate a detailed TCP log */ |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 6563 | 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] | 6564 | else if (!strcmp(args[1], "dontlognull")) { |
| 6565 | /* don't log empty requests */ |
| 6566 | curproxy->options |= PR_O_NULLNOLOG; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6567 | } |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 6568 | else if (!strcmp(args[1], "tcpka")) { |
| 6569 | /* enable TCP keep-alives on client and server sessions */ |
| 6570 | curproxy->options |= PR_O_TCP_CLI_KA | PR_O_TCP_SRV_KA; |
| 6571 | } |
| 6572 | else if (!strcmp(args[1], "clitcpka")) { |
| 6573 | /* enable TCP keep-alives on client sessions */ |
| 6574 | curproxy->options |= PR_O_TCP_CLI_KA; |
| 6575 | } |
| 6576 | else if (!strcmp(args[1], "srvtcpka")) { |
| 6577 | /* enable TCP keep-alives on server sessions */ |
| 6578 | curproxy->options |= PR_O_TCP_SRV_KA; |
| 6579 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 6580 | else if (!strcmp(args[1], "httpchk")) { |
| 6581 | /* use HTTP request to check servers' health */ |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6582 | if (curproxy->check_req != NULL) { |
| 6583 | free(curproxy->check_req); |
| 6584 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 6585 | curproxy->options |= PR_O_HTTP_CHK; |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 6586 | if (!*args[2]) { /* no argument */ |
| 6587 | curproxy->check_req = strdup(DEF_CHECK_REQ); /* default request */ |
| 6588 | curproxy->check_len = strlen(DEF_CHECK_REQ); |
| 6589 | } else if (!*args[3]) { /* one argument : URI */ |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame] | 6590 | int reqlen = strlen(args[2]) + strlen("OPTIONS / HTTP/1.0\r\n\r\n"); |
| 6591 | curproxy->check_req = (char *)malloc(reqlen); |
| 6592 | curproxy->check_len = snprintf(curproxy->check_req, reqlen, |
| 6593 | "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] | 6594 | } else { /* more arguments : METHOD URI [HTTP_VER] */ |
| 6595 | int reqlen = strlen(args[2]) + strlen(args[3]) + 3 + strlen("\r\n\r\n"); |
| 6596 | if (*args[4]) |
| 6597 | reqlen += strlen(args[4]); |
| 6598 | else |
| 6599 | reqlen += strlen("HTTP/1.0"); |
| 6600 | |
| 6601 | curproxy->check_req = (char *)malloc(reqlen); |
| 6602 | curproxy->check_len = snprintf(curproxy->check_req, reqlen, |
| 6603 | "%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] | 6604 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 6605 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6606 | else if (!strcmp(args[1], "persist")) { |
| 6607 | /* persist on using the server specified by the cookie, even when it's down */ |
| 6608 | curproxy->options |= PR_O_PERSIST; |
| 6609 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6610 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6611 | Alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6612 | return -1; |
| 6613 | } |
| 6614 | return 0; |
| 6615 | } |
| 6616 | else if (!strcmp(args[0], "redispatch") || !strcmp(args[0], "redisp")) { |
| 6617 | /* enable reconnections to dispatch */ |
| 6618 | curproxy->options |= PR_O_REDISP; |
| 6619 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 6620 | #ifdef TPROXY |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6621 | else if (!strcmp(args[0], "transparent")) { |
| 6622 | /* enable transparent proxy connections */ |
| 6623 | curproxy->options |= PR_O_TRANSP; |
| 6624 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6625 | #endif |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6626 | else if (!strcmp(args[0], "maxconn")) { /* maxconn */ |
| 6627 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6628 | 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] | 6629 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6630 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6631 | curproxy->maxconn = atol(args[1]); |
| 6632 | } |
| 6633 | else if (!strcmp(args[0], "grace")) { /* grace time (ms) */ |
| 6634 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6635 | 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] | 6636 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6637 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6638 | curproxy->grace = atol(args[1]); |
| 6639 | } |
| 6640 | else if (!strcmp(args[0], "dispatch")) { /* dispatch address */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6641 | if (curproxy == &defproxy) { |
| 6642 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6643 | return -1; |
| 6644 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6645 | if (strchr(args[1], ':') == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6646 | 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] | 6647 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6648 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6649 | curproxy->dispatch_addr = *str2sa(args[1]); |
| 6650 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6651 | else if (!strcmp(args[0], "balance")) { /* set balancing with optional algorithm */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6652 | if (*(args[1])) { |
| 6653 | if (!strcmp(args[1], "roundrobin")) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6654 | curproxy->options |= PR_O_BALANCE_RR; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6655 | } |
| 6656 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6657 | 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] | 6658 | return -1; |
| 6659 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6660 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6661 | else /* if no option is set, use round-robin by default */ |
| 6662 | curproxy->options |= PR_O_BALANCE_RR; |
| 6663 | } |
| 6664 | else if (!strcmp(args[0], "server")) { /* server address */ |
| 6665 | int cur_arg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6666 | char *rport; |
| 6667 | char *raddr; |
| 6668 | short realport; |
| 6669 | int do_check; |
| 6670 | |
| 6671 | if (curproxy == &defproxy) { |
| 6672 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6673 | return -1; |
| 6674 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6675 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6676 | if (!*args[2]) { |
| 6677 | 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] | 6678 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6679 | return -1; |
| 6680 | } |
| 6681 | if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) { |
| 6682 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 6683 | return -1; |
| 6684 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6685 | |
| 6686 | if (curproxy->srv == NULL) |
| 6687 | curproxy->srv = newsrv; |
| 6688 | else |
| 6689 | curproxy->cursrv->next = newsrv; |
| 6690 | curproxy->cursrv = newsrv; |
| 6691 | |
| 6692 | newsrv->next = NULL; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6693 | newsrv->proxy = curproxy; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6694 | |
| 6695 | do_check = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6696 | newsrv->state = SRV_RUNNING; /* early server setup */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6697 | newsrv->id = strdup(args[1]); |
| 6698 | |
| 6699 | /* several ways to check the port component : |
| 6700 | * - IP => port=+0, relative |
| 6701 | * - IP: => port=+0, relative |
| 6702 | * - IP:N => port=N, absolute |
| 6703 | * - IP:+N => port=+N, relative |
| 6704 | * - IP:-N => port=-N, relative |
| 6705 | */ |
| 6706 | raddr = strdup(args[2]); |
| 6707 | rport = strchr(raddr, ':'); |
| 6708 | if (rport) { |
| 6709 | *rport++ = 0; |
| 6710 | realport = atol(rport); |
| 6711 | if (!isdigit((int)*rport)) |
| 6712 | newsrv->state |= SRV_MAPPORTS; |
| 6713 | } else { |
| 6714 | realport = 0; |
| 6715 | newsrv->state |= SRV_MAPPORTS; |
| 6716 | } |
| 6717 | |
| 6718 | newsrv->addr = *str2sa(raddr); |
| 6719 | newsrv->addr.sin_port = htons(realport); |
| 6720 | free(raddr); |
| 6721 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6722 | newsrv->curfd = -1; /* no health-check in progress */ |
| 6723 | newsrv->inter = DEF_CHKINTR; |
| 6724 | newsrv->rise = DEF_RISETIME; |
| 6725 | newsrv->fall = DEF_FALLTIME; |
| 6726 | newsrv->health = newsrv->rise; /* up, but will fall down at first failure */ |
| 6727 | cur_arg = 3; |
| 6728 | while (*args[cur_arg]) { |
| 6729 | if (!strcmp(args[cur_arg], "cookie")) { |
| 6730 | newsrv->cookie = strdup(args[cur_arg + 1]); |
| 6731 | newsrv->cklen = strlen(args[cur_arg + 1]); |
| 6732 | cur_arg += 2; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6733 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6734 | else if (!strcmp(args[cur_arg], "rise")) { |
| 6735 | newsrv->rise = atol(args[cur_arg + 1]); |
| 6736 | newsrv->health = newsrv->rise; |
| 6737 | cur_arg += 2; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6738 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6739 | else if (!strcmp(args[cur_arg], "fall")) { |
| 6740 | newsrv->fall = atol(args[cur_arg + 1]); |
| 6741 | cur_arg += 2; |
| 6742 | } |
| 6743 | else if (!strcmp(args[cur_arg], "inter")) { |
| 6744 | newsrv->inter = atol(args[cur_arg + 1]); |
| 6745 | cur_arg += 2; |
| 6746 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6747 | else if (!strcmp(args[cur_arg], "port")) { |
| 6748 | newsrv->check_port = atol(args[cur_arg + 1]); |
| 6749 | cur_arg += 2; |
| 6750 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6751 | else if (!strcmp(args[cur_arg], "backup")) { |
| 6752 | newsrv->state |= SRV_BACKUP; |
| 6753 | cur_arg ++; |
| 6754 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6755 | else if (!strcmp(args[cur_arg], "check")) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6756 | do_check = 1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6757 | cur_arg += 1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 6758 | } |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6759 | else if (!strcmp(args[cur_arg], "source")) { /* address to which we bind when connecting */ |
| 6760 | if (!*args[cur_arg + 1]) { |
| 6761 | Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n", |
| 6762 | file, linenum, "source"); |
| 6763 | return -1; |
| 6764 | } |
| 6765 | newsrv->state |= SRV_BIND_SRC; |
| 6766 | newsrv->source_addr = *str2sa(args[cur_arg + 1]); |
| 6767 | cur_arg += 2; |
| 6768 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6769 | else { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 6770 | 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] | 6771 | file, linenum, newsrv->id); |
| 6772 | return -1; |
| 6773 | } |
| 6774 | } |
| 6775 | |
| 6776 | if (do_check) { |
| 6777 | struct task *t; |
| 6778 | |
| 6779 | if (!newsrv->check_port && !(newsrv->state & SRV_MAPPORTS)) |
| 6780 | newsrv->check_port = realport; /* by default */ |
| 6781 | if (!newsrv->check_port) { |
| 6782 | 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] | 6783 | file, linenum, newsrv->id); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6784 | return -1; |
| 6785 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6786 | |
| 6787 | if ((t = pool_alloc(task)) == NULL) { |
| 6788 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 6789 | return -1; |
| 6790 | } |
| 6791 | |
| 6792 | t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */ |
| 6793 | t->wq = LIST_HEAD(wait_queue); /* but already has a wait queue assigned */ |
| 6794 | t->state = TASK_IDLE; |
| 6795 | t->process = process_chk; |
| 6796 | t->context = newsrv; |
| 6797 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 6798 | if (curproxy->state != PR_STSTOPPED) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6799 | tv_delayfrom(&t->expire, &now, newsrv->inter); /* check this every ms */ |
| 6800 | task_queue(t); |
| 6801 | task_wakeup(&rq, t); |
| 6802 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6803 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6804 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6805 | curproxy->nbservers++; |
| 6806 | } |
| 6807 | else if (!strcmp(args[0], "log")) { /* syslog server address */ |
| 6808 | struct sockaddr_in *sa; |
| 6809 | int facility; |
| 6810 | |
| 6811 | if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) { |
| 6812 | curproxy->logfac1 = global.logfac1; |
| 6813 | curproxy->logsrv1 = global.logsrv1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6814 | curproxy->loglev1 = global.loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6815 | curproxy->logfac2 = global.logfac2; |
| 6816 | curproxy->logsrv2 = global.logsrv2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6817 | curproxy->loglev2 = global.loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6818 | } |
| 6819 | else if (*(args[1]) && *(args[2])) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6820 | int level; |
| 6821 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6822 | for (facility = 0; facility < NB_LOG_FACILITIES; facility++) |
| 6823 | if (!strcmp(log_facilities[facility], args[2])) |
| 6824 | break; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6825 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6826 | if (facility >= NB_LOG_FACILITIES) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6827 | 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] | 6828 | exit(1); |
| 6829 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6830 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6831 | level = 7; /* max syslog level = debug */ |
| 6832 | if (*(args[3])) { |
| 6833 | while (level >= 0 && strcmp(log_levels[level], args[3])) |
| 6834 | level--; |
| 6835 | if (level < 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6836 | 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] | 6837 | exit(1); |
| 6838 | } |
| 6839 | } |
| 6840 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6841 | sa = str2sa(args[1]); |
| 6842 | if (!sa->sin_port) |
| 6843 | sa->sin_port = htons(SYSLOG_PORT); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6844 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6845 | if (curproxy->logfac1 == -1) { |
| 6846 | curproxy->logsrv1 = *sa; |
| 6847 | curproxy->logfac1 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6848 | curproxy->loglev1 = level; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6849 | } |
| 6850 | else if (curproxy->logfac2 == -1) { |
| 6851 | curproxy->logsrv2 = *sa; |
| 6852 | curproxy->logfac2 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 6853 | curproxy->loglev2 = level; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6854 | } |
| 6855 | else { |
| 6856 | Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6857 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 6858 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6859 | } |
| 6860 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6861 | 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] | 6862 | file, linenum); |
| 6863 | return -1; |
| 6864 | } |
| 6865 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 6866 | 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] | 6867 | if (!*args[1]) { |
| 6868 | Alert("parsing [%s:%d] : '%s' expects <addr>[:<port>] as argument.\n", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6869 | file, linenum, "source"); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 6870 | return -1; |
| 6871 | } |
| 6872 | |
| 6873 | curproxy->source_addr = *str2sa(args[1]); |
| 6874 | curproxy->options |= PR_O_BIND_SRC; |
| 6875 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6876 | else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */ |
| 6877 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6878 | if (curproxy == &defproxy) { |
| 6879 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6880 | return -1; |
| 6881 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6882 | |
| 6883 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6884 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 6885 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6886 | return -1; |
| 6887 | } |
| 6888 | |
| 6889 | preg = calloc(1, sizeof(regex_t)); |
| 6890 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6891 | 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] | 6892 | return -1; |
| 6893 | } |
| 6894 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 6895 | err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 6896 | if (err) { |
| 6897 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 6898 | file, linenum, *err); |
| 6899 | return -1; |
| 6900 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6901 | } |
| 6902 | else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */ |
| 6903 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6904 | if (curproxy == &defproxy) { |
| 6905 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6906 | return -1; |
| 6907 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6908 | |
| 6909 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6910 | 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] | 6911 | return -1; |
| 6912 | } |
| 6913 | |
| 6914 | preg = calloc(1, sizeof(regex_t)); |
| 6915 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6916 | 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] | 6917 | return -1; |
| 6918 | } |
| 6919 | |
| 6920 | chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL); |
| 6921 | } |
| 6922 | else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */ |
| 6923 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6924 | if (curproxy == &defproxy) { |
| 6925 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6926 | return -1; |
| 6927 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6928 | |
| 6929 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6930 | 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] | 6931 | return -1; |
| 6932 | } |
| 6933 | |
| 6934 | preg = calloc(1, sizeof(regex_t)); |
| 6935 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6936 | 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] | 6937 | return -1; |
| 6938 | } |
| 6939 | |
| 6940 | chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL); |
| 6941 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6942 | else if (!strcmp(args[0], "reqpass")) { /* pass this header without allowing or denying the request */ |
| 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 | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6948 | |
| 6949 | if (*(args[1]) == 0) { |
| 6950 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
| 6951 | return -1; |
| 6952 | } |
| 6953 | |
| 6954 | preg = calloc(1, sizeof(regex_t)); |
| 6955 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 6956 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 6957 | return -1; |
| 6958 | } |
| 6959 | |
| 6960 | chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL); |
| 6961 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6962 | else if (!strcmp(args[0], "reqallow")) { /* allow 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_ALLOW, NULL); |
| 6981 | } |
| 6982 | else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */ |
| 6983 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 6984 | if (curproxy == &defproxy) { |
| 6985 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 6986 | return -1; |
| 6987 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6988 | |
| 6989 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6990 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 6991 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6992 | return -1; |
| 6993 | } |
| 6994 | |
| 6995 | preg = calloc(1, sizeof(regex_t)); |
| 6996 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 6997 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 6998 | return -1; |
| 6999 | } |
| 7000 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7001 | err = chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 7002 | if (err) { |
| 7003 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7004 | file, linenum, *err); |
| 7005 | return -1; |
| 7006 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7007 | } |
| 7008 | else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */ |
| 7009 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7010 | if (curproxy == &defproxy) { |
| 7011 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7012 | return -1; |
| 7013 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7014 | |
| 7015 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7016 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7017 | return -1; |
| 7018 | } |
| 7019 | |
| 7020 | preg = calloc(1, sizeof(regex_t)); |
| 7021 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7022 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7023 | return -1; |
| 7024 | } |
| 7025 | |
| 7026 | chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL); |
| 7027 | } |
| 7028 | else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */ |
| 7029 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7030 | if (curproxy == &defproxy) { |
| 7031 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7032 | return -1; |
| 7033 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7034 | |
| 7035 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7036 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7037 | return -1; |
| 7038 | } |
| 7039 | |
| 7040 | preg = calloc(1, sizeof(regex_t)); |
| 7041 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7042 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7043 | return -1; |
| 7044 | } |
| 7045 | |
| 7046 | chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL); |
| 7047 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7048 | else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */ |
| 7049 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7050 | if (curproxy == &defproxy) { |
| 7051 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7052 | return -1; |
| 7053 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7054 | |
| 7055 | if (*(args[1]) == 0) { |
| 7056 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
| 7057 | return -1; |
| 7058 | } |
| 7059 | |
| 7060 | preg = calloc(1, sizeof(regex_t)); |
| 7061 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 7062 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7063 | return -1; |
| 7064 | } |
| 7065 | |
| 7066 | chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL); |
| 7067 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7068 | else if (!strcmp(args[0], "reqiallow")) { /* allow 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_ALLOW, NULL); |
| 7087 | } |
| 7088 | else if (!strcmp(args[0], "reqadd")) { /* add request header */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7089 | if (curproxy == &defproxy) { |
| 7090 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7091 | return -1; |
| 7092 | } |
| 7093 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7094 | if (curproxy->nb_reqadd >= MAX_NEWHDR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7095 | 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] | 7096 | return 0; |
| 7097 | } |
| 7098 | |
| 7099 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7100 | 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] | 7101 | return -1; |
| 7102 | } |
| 7103 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7104 | curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]); |
| 7105 | } |
| 7106 | else if (!strcmp(args[0], "srvexp") || !strcmp(args[0], "rsprep")) { /* replace response header from a regex */ |
| 7107 | regex_t *preg; |
| 7108 | |
| 7109 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 7110 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 7111 | file, linenum, args[0]); |
| 7112 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7113 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7114 | |
| 7115 | preg = calloc(1, sizeof(regex_t)); |
| 7116 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 7117 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7118 | return -1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7119 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7120 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7121 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 7122 | if (err) { |
| 7123 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7124 | file, linenum, *err); |
| 7125 | return -1; |
| 7126 | } |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7127 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7128 | else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */ |
| 7129 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7130 | if (curproxy == &defproxy) { |
| 7131 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7132 | return -1; |
| 7133 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7134 | |
| 7135 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7136 | 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] | 7137 | return -1; |
| 7138 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7139 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7140 | preg = calloc(1, sizeof(regex_t)); |
| 7141 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7142 | 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] | 7143 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7144 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7145 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7146 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2])); |
| 7147 | if (err) { |
| 7148 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7149 | file, linenum, *err); |
| 7150 | return -1; |
| 7151 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7152 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7153 | else if (!strcmp(args[0], "rspdeny")) { /* block response header from a regex */ |
| 7154 | regex_t *preg; |
| 7155 | if (curproxy == &defproxy) { |
| 7156 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7157 | return -1; |
| 7158 | } |
| 7159 | |
| 7160 | if (*(args[1]) == 0) { |
| 7161 | Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]); |
| 7162 | return -1; |
| 7163 | } |
| 7164 | |
| 7165 | preg = calloc(1, sizeof(regex_t)); |
| 7166 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 7167 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7168 | return -1; |
| 7169 | } |
| 7170 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7171 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2])); |
| 7172 | if (err) { |
| 7173 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7174 | file, linenum, *err); |
| 7175 | return -1; |
| 7176 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7177 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7178 | 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] | 7179 | regex_t *preg; |
| 7180 | if (curproxy == &defproxy) { |
| 7181 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7182 | return -1; |
| 7183 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7184 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7185 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 7186 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 7187 | file, linenum, args[0]); |
| 7188 | return -1; |
| 7189 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7190 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7191 | preg = calloc(1, sizeof(regex_t)); |
| 7192 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 7193 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7194 | return -1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7195 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7196 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7197 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 7198 | if (err) { |
| 7199 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7200 | file, linenum, *err); |
| 7201 | return -1; |
| 7202 | } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7203 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7204 | else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */ |
| 7205 | regex_t *preg; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7206 | if (curproxy == &defproxy) { |
| 7207 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7208 | return -1; |
| 7209 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7210 | |
| 7211 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7212 | 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] | 7213 | return -1; |
| 7214 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7215 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7216 | preg = calloc(1, sizeof(regex_t)); |
| 7217 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7218 | 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] | 7219 | return -1; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7220 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7221 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7222 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2])); |
| 7223 | if (err) { |
| 7224 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7225 | file, linenum, *err); |
| 7226 | return -1; |
| 7227 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7228 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7229 | else if (!strcmp(args[0], "rspideny")) { /* block response header from a regex ignoring case */ |
| 7230 | regex_t *preg; |
| 7231 | if (curproxy == &defproxy) { |
| 7232 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7233 | return -1; |
| 7234 | } |
| 7235 | |
| 7236 | if (*(args[1]) == 0) { |
| 7237 | Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]); |
| 7238 | return -1; |
| 7239 | } |
| 7240 | |
| 7241 | preg = calloc(1, sizeof(regex_t)); |
| 7242 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 7243 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 7244 | return -1; |
| 7245 | } |
| 7246 | |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7247 | err = chain_regex(&curproxy->rsp_exp, preg, ACT_DENY, strdup(args[2])); |
| 7248 | if (err) { |
| 7249 | Alert("parsing [%s:%d] : invalid character or unterminated sequence in replacement string near '%c'.\n", |
| 7250 | file, linenum, *err); |
| 7251 | return -1; |
| 7252 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7253 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7254 | else if (!strcmp(args[0], "rspadd")) { /* add response header */ |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7255 | if (curproxy == &defproxy) { |
| 7256 | Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7257 | return -1; |
| 7258 | } |
| 7259 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7260 | if (curproxy->nb_rspadd >= MAX_NEWHDR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7261 | 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] | 7262 | return 0; |
| 7263 | } |
| 7264 | |
| 7265 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7266 | 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] | 7267 | return -1; |
| 7268 | } |
| 7269 | |
| 7270 | curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]); |
| 7271 | } |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7272 | else if (!strcmp(args[0], "errorloc") || |
| 7273 | !strcmp(args[0], "errorloc302") || |
| 7274 | !strcmp(args[0], "errorloc303")) { /* error location */ |
| 7275 | int errnum, errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7276 | char *err; |
| 7277 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7278 | // if (curproxy == &defproxy) { |
| 7279 | // Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]); |
| 7280 | // return -1; |
| 7281 | // } |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7282 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7283 | if (*(args[2]) == 0) { |
| 7284 | Alert("parsing [%s:%d] : <errorloc> expects <error> and <url> as arguments.\n", file, linenum); |
| 7285 | return -1; |
| 7286 | } |
| 7287 | |
| 7288 | errnum = atol(args[1]); |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7289 | if (!strcmp(args[0], "errorloc303")) { |
| 7290 | err = malloc(strlen(HTTP_303) + strlen(args[2]) + 5); |
| 7291 | errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_303, args[2]); |
| 7292 | } else { |
| 7293 | err = malloc(strlen(HTTP_302) + strlen(args[2]) + 5); |
| 7294 | errlen = sprintf(err, "%s%s\r\n\r\n", HTTP_302, args[2]); |
| 7295 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7296 | |
| 7297 | if (errnum == 400) { |
| 7298 | if (curproxy->errmsg.msg400) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7299 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7300 | free(curproxy->errmsg.msg400); |
| 7301 | } |
| 7302 | curproxy->errmsg.msg400 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7303 | curproxy->errmsg.len400 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7304 | } |
| 7305 | else if (errnum == 403) { |
| 7306 | if (curproxy->errmsg.msg403) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7307 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7308 | free(curproxy->errmsg.msg403); |
| 7309 | } |
| 7310 | curproxy->errmsg.msg403 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7311 | curproxy->errmsg.len403 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7312 | } |
| 7313 | else if (errnum == 408) { |
| 7314 | if (curproxy->errmsg.msg408) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7315 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7316 | free(curproxy->errmsg.msg408); |
| 7317 | } |
| 7318 | curproxy->errmsg.msg408 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7319 | curproxy->errmsg.len408 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7320 | } |
| 7321 | else if (errnum == 500) { |
| 7322 | if (curproxy->errmsg.msg500) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7323 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7324 | free(curproxy->errmsg.msg500); |
| 7325 | } |
| 7326 | curproxy->errmsg.msg500 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7327 | curproxy->errmsg.len500 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7328 | } |
| 7329 | else if (errnum == 502) { |
| 7330 | if (curproxy->errmsg.msg502) { |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7331 | //Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7332 | free(curproxy->errmsg.msg502); |
| 7333 | } |
| 7334 | curproxy->errmsg.msg502 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7335 | curproxy->errmsg.len502 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7336 | } |
| 7337 | else if (errnum == 503) { |
| 7338 | if (curproxy->errmsg.msg503) { |
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.msg503); |
| 7341 | } |
| 7342 | curproxy->errmsg.msg503 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7343 | curproxy->errmsg.len503 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7344 | } |
| 7345 | else if (errnum == 504) { |
| 7346 | if (curproxy->errmsg.msg504) { |
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.msg504); |
| 7349 | } |
| 7350 | curproxy->errmsg.msg504 = err; |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7351 | curproxy->errmsg.len504 = errlen; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7352 | } |
| 7353 | else { |
| 7354 | Warning("parsing [%s:%d] : error %d relocation will be ignored.\n", file, linenum, errnum); |
| 7355 | free(err); |
| 7356 | } |
| 7357 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7358 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7359 | 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] | 7360 | return -1; |
| 7361 | } |
| 7362 | return 0; |
| 7363 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7364 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7365 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7366 | /* |
| 7367 | * This function reads and parses the configuration file given in the argument. |
| 7368 | * returns 0 if OK, -1 if error. |
| 7369 | */ |
| 7370 | int readcfgfile(char *file) { |
| 7371 | char thisline[256]; |
| 7372 | char *line; |
| 7373 | FILE *f; |
| 7374 | int linenum = 0; |
| 7375 | char *end; |
| 7376 | char *args[MAX_LINE_ARGS]; |
| 7377 | int arg; |
| 7378 | int cfgerr = 0; |
| 7379 | int confsect = CFG_NONE; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7380 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7381 | struct proxy *curproxy = NULL; |
| 7382 | struct server *newsrv = NULL; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7383 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7384 | if ((f=fopen(file,"r")) == NULL) |
| 7385 | return -1; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7386 | |
willy tarreau | eedaa9f | 2005-12-17 14:08:03 +0100 | [diff] [blame] | 7387 | init_default_instance(); |
| 7388 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7389 | while (fgets(line = thisline, sizeof(thisline), f) != NULL) { |
| 7390 | linenum++; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7391 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7392 | end = line + strlen(line); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7393 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7394 | /* skip leading spaces */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 7395 | while (isspace((int)*line)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7396 | line++; |
| 7397 | |
| 7398 | arg = 0; |
| 7399 | args[arg] = line; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7400 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7401 | while (*line && arg < MAX_LINE_ARGS) { |
| 7402 | /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their |
| 7403 | * C equivalent value. Other combinations left unchanged (eg: \1). |
| 7404 | */ |
| 7405 | if (*line == '\\') { |
| 7406 | int skip = 0; |
| 7407 | if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') { |
| 7408 | *line = line[1]; |
| 7409 | skip = 1; |
| 7410 | } |
| 7411 | else if (line[1] == 'r') { |
| 7412 | *line = '\r'; |
| 7413 | skip = 1; |
| 7414 | } |
| 7415 | else if (line[1] == 'n') { |
| 7416 | *line = '\n'; |
| 7417 | skip = 1; |
| 7418 | } |
| 7419 | else if (line[1] == 't') { |
| 7420 | *line = '\t'; |
| 7421 | skip = 1; |
| 7422 | } |
willy tarreau | c1f4753 | 2005-12-18 01:08:26 +0100 | [diff] [blame] | 7423 | else if (line[1] == 'x') { |
| 7424 | if ((line + 3 < end ) && ishex(line[2]) && ishex(line[3])) { |
| 7425 | unsigned char hex1, hex2; |
| 7426 | hex1 = toupper(line[2]) - '0'; |
| 7427 | hex2 = toupper(line[3]) - '0'; |
| 7428 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 7429 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 7430 | *line = (hex1<<4) + hex2; |
| 7431 | skip = 3; |
| 7432 | } |
| 7433 | else { |
| 7434 | Alert("parsing [%s:%d] : invalid or incomplete '\\x' sequence in '%s'.\n", file, linenum, args[0]); |
| 7435 | return -1; |
| 7436 | } |
| 7437 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7438 | if (skip) { |
| 7439 | memmove(line + 1, line + 1 + skip, end - (line + skip + 1)); |
| 7440 | end -= skip; |
| 7441 | } |
| 7442 | line++; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7443 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7444 | else if (*line == '#' || *line == '\n' || *line == '\r') { |
| 7445 | /* end of string, end of loop */ |
| 7446 | *line = 0; |
| 7447 | break; |
| 7448 | } |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 7449 | else if (isspace((int)*line)) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7450 | /* a non-escaped space is an argument separator */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7451 | *line++ = 0; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 7452 | while (isspace((int)*line)) |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7453 | line++; |
| 7454 | args[++arg] = line; |
| 7455 | } |
| 7456 | else { |
| 7457 | line++; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7458 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7459 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7460 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7461 | /* empty line */ |
| 7462 | if (!**args) |
| 7463 | continue; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7464 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7465 | /* zero out remaining args */ |
| 7466 | while (++arg < MAX_LINE_ARGS) { |
| 7467 | args[arg] = line; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7468 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7469 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7470 | if (!strcmp(args[0], "listen") || !strcmp(args[0], "defaults")) /* new proxy */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7471 | confsect = CFG_LISTEN; |
| 7472 | else if (!strcmp(args[0], "global")) /* global config */ |
| 7473 | confsect = CFG_GLOBAL; |
| 7474 | /* else it's a section keyword */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7475 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7476 | switch (confsect) { |
| 7477 | case CFG_LISTEN: |
| 7478 | if (cfg_parse_listen(file, linenum, args) < 0) |
| 7479 | return -1; |
| 7480 | break; |
| 7481 | case CFG_GLOBAL: |
| 7482 | if (cfg_parse_global(file, linenum, args) < 0) |
| 7483 | return -1; |
| 7484 | break; |
| 7485 | default: |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 7486 | 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] | 7487 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7488 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7489 | |
| 7490 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7491 | } |
| 7492 | fclose(f); |
| 7493 | |
| 7494 | /* |
| 7495 | * Now, check for the integrity of all that we have collected. |
| 7496 | */ |
| 7497 | |
| 7498 | if ((curproxy = proxy) == NULL) { |
| 7499 | Alert("parsing %s : no <listen> line. Nothing to do !\n", |
| 7500 | file); |
| 7501 | return -1; |
| 7502 | } |
| 7503 | |
| 7504 | while (curproxy != NULL) { |
willy tarreau | 0174f31 | 2005-12-18 01:02:42 +0100 | [diff] [blame] | 7505 | curproxy->cursrv = NULL; |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7506 | if (curproxy->state == PR_STSTOPPED) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 7507 | curproxy = curproxy->next; |
| 7508 | continue; |
| 7509 | } |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 7510 | |
| 7511 | if (curproxy->listen == NULL) { |
| 7512 | 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); |
| 7513 | cfgerr++; |
| 7514 | } |
| 7515 | else if ((curproxy->mode != PR_MODE_HEALTH) && |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7516 | !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) && |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7517 | (*(int *)&curproxy->dispatch_addr.sin_addr == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7518 | Alert("parsing %s : listener %s has no dispatch address and is not in transparent or balance mode.\n", |
| 7519 | file, curproxy->id); |
| 7520 | cfgerr++; |
| 7521 | } |
| 7522 | else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) { |
| 7523 | if (curproxy->options & PR_O_TRANSP) { |
| 7524 | Alert("parsing %s : listener %s cannot use both transparent and balance mode.\n", |
| 7525 | file, curproxy->id); |
| 7526 | cfgerr++; |
| 7527 | } |
| 7528 | else if (curproxy->srv == NULL) { |
| 7529 | Alert("parsing %s : listener %s needs at least 1 server in balance mode.\n", |
| 7530 | file, curproxy->id); |
| 7531 | cfgerr++; |
| 7532 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7533 | else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7534 | Warning("parsing %s : dispatch address of listener %s will be ignored in balance mode.\n", |
| 7535 | file, curproxy->id); |
| 7536 | } |
| 7537 | } |
| 7538 | 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] | 7539 | if (curproxy->cookie_name != NULL) { |
| 7540 | Warning("parsing %s : cookie will be ignored for listener %s.\n", |
| 7541 | file, curproxy->id); |
| 7542 | } |
| 7543 | if ((newsrv = curproxy->srv) != NULL) { |
| 7544 | Warning("parsing %s : servers will be ignored for listener %s.\n", |
| 7545 | file, curproxy->id); |
| 7546 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7547 | if (curproxy->rsp_exp != NULL) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7548 | Warning("parsing %s : server regular expressions will be ignored for listener %s.\n", |
| 7549 | file, curproxy->id); |
| 7550 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 7551 | if (curproxy->req_exp != NULL) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7552 | Warning("parsing %s : client regular expressions will be ignored for listener %s.\n", |
| 7553 | file, curproxy->id); |
| 7554 | } |
| 7555 | } |
| 7556 | else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */ |
| 7557 | if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) { |
| 7558 | Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n", |
| 7559 | file, curproxy->id); |
| 7560 | cfgerr++; |
| 7561 | } |
| 7562 | else { |
| 7563 | while (newsrv != NULL) { |
| 7564 | /* nothing to check for now */ |
| 7565 | newsrv = newsrv->next; |
| 7566 | } |
| 7567 | } |
| 7568 | } |
willy tarreau | 25c4ea5 | 2005-12-18 00:49:49 +0100 | [diff] [blame] | 7569 | |
| 7570 | if (curproxy->options & PR_O_LOGASAP) |
| 7571 | curproxy->to_log &= ~LW_BYTES; |
| 7572 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 7573 | if (curproxy->errmsg.msg400 == NULL) { |
| 7574 | curproxy->errmsg.msg400 = (char *)HTTP_400; |
| 7575 | curproxy->errmsg.len400 = strlen(HTTP_400); |
| 7576 | } |
| 7577 | if (curproxy->errmsg.msg403 == NULL) { |
| 7578 | curproxy->errmsg.msg403 = (char *)HTTP_403; |
| 7579 | curproxy->errmsg.len403 = strlen(HTTP_403); |
| 7580 | } |
| 7581 | if (curproxy->errmsg.msg408 == NULL) { |
| 7582 | curproxy->errmsg.msg408 = (char *)HTTP_408; |
| 7583 | curproxy->errmsg.len408 = strlen(HTTP_408); |
| 7584 | } |
| 7585 | if (curproxy->errmsg.msg500 == NULL) { |
| 7586 | curproxy->errmsg.msg500 = (char *)HTTP_500; |
| 7587 | curproxy->errmsg.len500 = strlen(HTTP_500); |
| 7588 | } |
| 7589 | if (curproxy->errmsg.msg502 == NULL) { |
| 7590 | curproxy->errmsg.msg502 = (char *)HTTP_502; |
| 7591 | curproxy->errmsg.len502 = strlen(HTTP_502); |
| 7592 | } |
| 7593 | if (curproxy->errmsg.msg503 == NULL) { |
| 7594 | curproxy->errmsg.msg503 = (char *)HTTP_503; |
| 7595 | curproxy->errmsg.len503 = strlen(HTTP_503); |
| 7596 | } |
| 7597 | if (curproxy->errmsg.msg504 == NULL) { |
| 7598 | curproxy->errmsg.msg504 = (char *)HTTP_504; |
| 7599 | curproxy->errmsg.len504 = strlen(HTTP_504); |
| 7600 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7601 | curproxy = curproxy->next; |
| 7602 | } |
| 7603 | if (cfgerr > 0) { |
| 7604 | Alert("Errors found in configuration file, aborting.\n"); |
| 7605 | return -1; |
| 7606 | } |
| 7607 | else |
| 7608 | return 0; |
| 7609 | } |
| 7610 | |
| 7611 | |
| 7612 | /* |
| 7613 | * This function initializes all the necessary variables. It only returns |
| 7614 | * if everything is OK. If something fails, it exits. |
| 7615 | */ |
| 7616 | void init(int argc, char **argv) { |
| 7617 | int i; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7618 | int arg_mode = 0; /* MODE_DEBUG, ... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7619 | char *old_argv = *argv; |
| 7620 | char *tmp; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 7621 | char *cfg_pidfile = NULL; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7622 | int cfg_maxconn = 0; /* # of simultaneous connections, (-n) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7623 | |
| 7624 | if (1<<INTBITS != sizeof(int)*8) { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 7625 | fprintf(stderr, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7626 | "Error: wrong architecture. Recompile so that sizeof(int)=%d\n", |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7627 | (int)(sizeof(int)*8)); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7628 | exit(1); |
| 7629 | } |
| 7630 | |
willy tarreau | 4302f49 | 2005-12-18 01:00:37 +0100 | [diff] [blame] | 7631 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 7632 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 7633 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 7634 | * printable characters above. |
| 7635 | */ |
| 7636 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 7637 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 7638 | for (i = 0; i < 32; i++) { |
| 7639 | FD_SET(i, hdr_encode_map); |
| 7640 | FD_SET(i, url_encode_map); |
| 7641 | } |
| 7642 | for (i = 127; i < 256; i++) { |
| 7643 | FD_SET(i, hdr_encode_map); |
| 7644 | FD_SET(i, url_encode_map); |
| 7645 | } |
| 7646 | |
| 7647 | tmp = "\"#{|}"; |
| 7648 | while (*tmp) { |
| 7649 | FD_SET(*tmp, hdr_encode_map); |
| 7650 | tmp++; |
| 7651 | } |
| 7652 | |
| 7653 | tmp = "\"#"; |
| 7654 | while (*tmp) { |
| 7655 | FD_SET(*tmp, url_encode_map); |
| 7656 | tmp++; |
| 7657 | } |
| 7658 | |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 7659 | cfg_polling_mechanism = POLL_USE_SELECT; /* select() is always available */ |
| 7660 | #if defined(ENABLE_POLL) |
| 7661 | cfg_polling_mechanism |= POLL_USE_POLL; |
| 7662 | #endif |
| 7663 | #if defined(ENABLE_EPOLL) |
| 7664 | cfg_polling_mechanism |= POLL_USE_EPOLL; |
| 7665 | #endif |
| 7666 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7667 | pid = getpid(); |
| 7668 | progname = *argv; |
| 7669 | while ((tmp = strchr(progname, '/')) != NULL) |
| 7670 | progname = tmp + 1; |
| 7671 | |
| 7672 | argc--; argv++; |
| 7673 | while (argc > 0) { |
| 7674 | char *flag; |
| 7675 | |
| 7676 | if (**argv == '-') { |
| 7677 | flag = *argv+1; |
| 7678 | |
| 7679 | /* 1 arg */ |
| 7680 | if (*flag == 'v') { |
| 7681 | display_version(); |
| 7682 | exit(0); |
| 7683 | } |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7684 | #if defined(ENABLE_EPOLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 7685 | else if (*flag == 'd' && flag[1] == 'e') |
| 7686 | cfg_polling_mechanism &= ~POLL_USE_EPOLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7687 | #endif |
| 7688 | #if defined(ENABLE_POLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 7689 | else if (*flag == 'd' && flag[1] == 'p') |
| 7690 | cfg_polling_mechanism &= ~POLL_USE_POLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 7691 | #endif |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7692 | else if (*flag == 'V') |
| 7693 | arg_mode |= MODE_VERBOSE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7694 | else if (*flag == 'd') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7695 | arg_mode |= MODE_DEBUG; |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 7696 | else if (*flag == 'c') |
| 7697 | arg_mode |= MODE_CHECK; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7698 | else if (*flag == 'D') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7699 | arg_mode |= MODE_DAEMON | MODE_QUIET; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7700 | else if (*flag == 'q') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7701 | arg_mode |= MODE_QUIET; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7702 | #if STATTIME > 0 |
| 7703 | else if (*flag == 's') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7704 | arg_mode |= MODE_STATS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7705 | else if (*flag == 'l') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7706 | arg_mode |= MODE_LOG; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7707 | #endif |
| 7708 | else { /* >=2 args */ |
| 7709 | argv++; argc--; |
| 7710 | if (argc == 0) |
| 7711 | usage(old_argv); |
| 7712 | |
| 7713 | switch (*flag) { |
| 7714 | case 'n' : cfg_maxconn = atol(*argv); break; |
| 7715 | case 'N' : cfg_maxpconn = atol(*argv); break; |
| 7716 | case 'f' : cfg_cfgfile = *argv; break; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 7717 | case 'p' : cfg_pidfile = *argv; break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7718 | default: usage(old_argv); |
| 7719 | } |
| 7720 | } |
| 7721 | } |
| 7722 | else |
| 7723 | usage(old_argv); |
| 7724 | argv++; argc--; |
| 7725 | } |
| 7726 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 7727 | global.mode = MODE_STARTING | /* during startup, we want most of the alerts */ |
| 7728 | (arg_mode & (MODE_DAEMON | MODE_VERBOSE | MODE_QUIET | MODE_CHECK | MODE_DEBUG)); |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 7729 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7730 | if (!cfg_cfgfile) |
| 7731 | usage(old_argv); |
| 7732 | |
| 7733 | gethostname(hostname, MAX_HOSTNAME_LEN); |
| 7734 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7735 | have_appsession = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7736 | if (readcfgfile(cfg_cfgfile) < 0) { |
| 7737 | Alert("Error reading configuration file : %s\n", cfg_cfgfile); |
| 7738 | exit(1); |
| 7739 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7740 | if (have_appsession) |
| 7741 | appsession_init(); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7742 | |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7743 | if (global.mode & MODE_CHECK) { |
willy tarreau | dd07e97 | 2005-12-18 00:48:48 +0100 | [diff] [blame] | 7744 | qfprintf(stdout, "Configuration file is valid : %s\n", cfg_cfgfile); |
| 7745 | exit(0); |
| 7746 | } |
| 7747 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7748 | if (cfg_maxconn > 0) |
| 7749 | global.maxconn = cfg_maxconn; |
| 7750 | |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 7751 | if (cfg_pidfile) { |
| 7752 | if (global.pidfile) |
| 7753 | free(global.pidfile); |
| 7754 | global.pidfile = strdup(cfg_pidfile); |
| 7755 | } |
| 7756 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7757 | if (global.maxconn == 0) |
| 7758 | global.maxconn = DEFAULT_MAXCONN; |
| 7759 | |
| 7760 | global.maxsock = global.maxconn * 2; /* each connection needs two sockets */ |
| 7761 | |
| 7762 | if (arg_mode & MODE_DEBUG) { |
| 7763 | /* command line debug mode inhibits configuration mode */ |
| 7764 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 7765 | } |
willy tarreau | 982249e | 2005-12-18 00:57:06 +0100 | [diff] [blame] | 7766 | global.mode |= (arg_mode & (MODE_DAEMON | MODE_QUIET | MODE_VERBOSE |
| 7767 | | MODE_DEBUG | MODE_STATS | MODE_LOG)); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7768 | |
| 7769 | if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) { |
| 7770 | Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n"); |
| 7771 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 7772 | } |
| 7773 | |
| 7774 | if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) { |
| 7775 | Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n"); |
| 7776 | global.nbproc = 1; |
| 7777 | } |
| 7778 | |
| 7779 | if (global.nbproc < 1) |
| 7780 | global.nbproc = 1; |
| 7781 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7782 | StaticReadEvent = (fd_set *)calloc(1, |
| 7783 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7784 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7785 | StaticWriteEvent = (fd_set *)calloc(1, |
| 7786 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7787 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7788 | |
| 7789 | fdtab = (struct fdtab *)calloc(1, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 7790 | sizeof(struct fdtab) * (global.maxsock)); |
| 7791 | for (i = 0; i < global.maxsock; i++) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7792 | fdtab[i].state = FD_STCLOSE; |
| 7793 | } |
| 7794 | } |
| 7795 | |
| 7796 | /* |
| 7797 | * this function starts all the proxies. It returns 0 if OK, -1 if not. |
| 7798 | */ |
| 7799 | int start_proxies() { |
| 7800 | struct proxy *curproxy; |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7801 | struct listener *listener; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7802 | int fd; |
| 7803 | |
| 7804 | for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) { |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 7805 | if (curproxy->state == PR_STSTOPPED) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7806 | continue; |
| 7807 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7808 | for (listener = curproxy->listen; listener != NULL; listener = listener->next) { |
| 7809 | if ((fd = listener->fd = |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 7810 | socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7811 | Alert("cannot create listening socket for proxy %s. Aborting.\n", |
| 7812 | curproxy->id); |
| 7813 | return -1; |
| 7814 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7815 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7816 | if (fd >= global.maxsock) { |
| 7817 | Alert("socket(): not enough free sockets for proxy %s. Raise -n argument. Aborting.\n", |
| 7818 | curproxy->id); |
| 7819 | close(fd); |
| 7820 | return -1; |
| 7821 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 7822 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7823 | if ((fcntl(fd, F_SETFL, O_NONBLOCK) == -1) || |
| 7824 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 7825 | (char *) &one, sizeof(one)) == -1)) { |
| 7826 | Alert("cannot make socket non-blocking for proxy %s. Aborting.\n", |
| 7827 | curproxy->id); |
| 7828 | close(fd); |
| 7829 | return -1; |
| 7830 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7831 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7832 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) { |
| 7833 | Alert("cannot do so_reuseaddr for proxy %s. Continuing.\n", |
| 7834 | curproxy->id); |
| 7835 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7836 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7837 | if (bind(fd, |
| 7838 | (struct sockaddr *)&listener->addr, |
willy tarreau | 8a86dbf | 2005-12-18 00:45:59 +0100 | [diff] [blame] | 7839 | listener->addr.ss_family == AF_INET6 ? |
| 7840 | sizeof(struct sockaddr_in6) : |
| 7841 | sizeof(struct sockaddr_in)) == -1) { |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7842 | Alert("cannot bind socket for proxy %s. Aborting.\n", |
| 7843 | curproxy->id); |
| 7844 | close(fd); |
| 7845 | return -1; |
| 7846 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7847 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7848 | if (listen(fd, curproxy->maxconn) == -1) { |
| 7849 | Alert("cannot listen to socket for proxy %s. Aborting.\n", |
| 7850 | curproxy->id); |
| 7851 | close(fd); |
| 7852 | return -1; |
| 7853 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7854 | |
willy tarreau | a41a8b4 | 2005-12-17 14:02:24 +0100 | [diff] [blame] | 7855 | /* the function for the accept() event */ |
| 7856 | fdtab[fd].read = &event_accept; |
| 7857 | fdtab[fd].write = NULL; /* never called */ |
| 7858 | fdtab[fd].owner = (struct task *)curproxy; /* reference the proxy instead of a task */ |
| 7859 | curproxy->state = PR_STRUN; |
| 7860 | fdtab[fd].state = FD_STLISTEN; |
| 7861 | FD_SET(fd, StaticReadEvent); |
| 7862 | fd_insert(fd); |
| 7863 | listeners++; |
| 7864 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 7865 | send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 7866 | } |
| 7867 | return 0; |
| 7868 | } |
| 7869 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 7870 | int match_str(const void *key1, const void *key2) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7871 | |
| 7872 | appsess *temp1,*temp2; |
| 7873 | temp1 = (appsess *)key1; |
| 7874 | temp2 = (appsess *)key2; |
| 7875 | |
| 7876 | //fprintf(stdout,">>>>>>>>>>>>>>temp1->sessid :%s:\n",temp1->sessid); |
| 7877 | //fprintf(stdout,">>>>>>>>>>>>>>temp2->sessid :%s:\n",temp2->sessid); |
| 7878 | |
| 7879 | return (strcmp(temp1->sessid,temp2->sessid) == 0); |
| 7880 | }/* end match_str */ |
| 7881 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 7882 | void destroy(void *data) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7883 | appsess *temp1; |
| 7884 | |
| 7885 | //printf("destroy called\n"); |
| 7886 | temp1 = (appsess *)data; |
| 7887 | |
| 7888 | if (temp1->sessid) |
| 7889 | pool_free_to(apools.sessid, temp1->sessid); |
| 7890 | |
| 7891 | if (temp1->serverid) |
| 7892 | pool_free_to(apools.serverid, temp1->serverid); |
| 7893 | |
| 7894 | pool_free(appsess, temp1); |
| 7895 | } /* end destroy */ |
| 7896 | |
| 7897 | void appsession_cleanup( void ) |
| 7898 | { |
| 7899 | struct proxy *p = proxy; |
| 7900 | |
| 7901 | while(p) { |
| 7902 | chtbl_destroy(&(p->htbl_proxy)); |
| 7903 | p = p->next; |
| 7904 | } |
| 7905 | }/* end appsession_cleanup() */ |
| 7906 | |
| 7907 | void pool_destroy(void **pool) |
| 7908 | { |
| 7909 | void *temp, *next; |
| 7910 | next = pool; |
| 7911 | while (next) { |
| 7912 | temp = next; |
| 7913 | next = *(void **)temp; |
| 7914 | free(temp); |
| 7915 | } |
| 7916 | }/* end pool_destroy() */ |
| 7917 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 7918 | void deinit(void) { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7919 | struct proxy *p = proxy; |
| 7920 | struct cap_hdr *h,*h_next; |
| 7921 | struct server *s,*s_next; |
| 7922 | struct listener *l,*l_next; |
| 7923 | |
| 7924 | while (p) { |
| 7925 | if (p->id) |
| 7926 | free(p->id); |
| 7927 | |
| 7928 | if (p->check_req) |
| 7929 | free(p->check_req); |
| 7930 | |
| 7931 | if (p->cookie_name) |
| 7932 | free(p->cookie_name); |
| 7933 | |
| 7934 | if (p->capture_name) |
| 7935 | free(p->capture_name); |
| 7936 | |
| 7937 | /* only strup if the user have set in config. |
| 7938 | When should we free it?! |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 7939 | if (p->errmsg.msg400) free(p->errmsg.msg400); |
| 7940 | if (p->errmsg.msg403) free(p->errmsg.msg403); |
| 7941 | if (p->errmsg.msg408) free(p->errmsg.msg408); |
| 7942 | if (p->errmsg.msg500) free(p->errmsg.msg500); |
| 7943 | if (p->errmsg.msg502) free(p->errmsg.msg502); |
| 7944 | if (p->errmsg.msg503) free(p->errmsg.msg503); |
| 7945 | if (p->errmsg.msg504) free(p->errmsg.msg504); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7946 | */ |
| 7947 | if (p->appsession_name) |
| 7948 | free(p->appsession_name); |
| 7949 | |
| 7950 | h = p->req_cap; |
| 7951 | while (h) { |
| 7952 | h_next = h->next; |
| 7953 | if (h->name) |
| 7954 | free(h->name); |
| 7955 | pool_destroy(h->pool); |
| 7956 | free(h); |
| 7957 | h = h_next; |
| 7958 | }/* end while(h) */ |
| 7959 | |
| 7960 | h = p->rsp_cap; |
| 7961 | while (h) { |
| 7962 | h_next = h->next; |
| 7963 | if (h->name) |
| 7964 | free(h->name); |
| 7965 | |
| 7966 | pool_destroy(h->pool); |
| 7967 | free(h); |
| 7968 | h = h_next; |
| 7969 | }/* end while(h) */ |
| 7970 | |
| 7971 | s = p->srv; |
| 7972 | while (s) { |
| 7973 | s_next = s->next; |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 7974 | if (s->id) |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7975 | free(s->id); |
| 7976 | |
willy tarreau | b952e1d | 2005-12-18 01:31:20 +0100 | [diff] [blame] | 7977 | if (s->cookie) |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7978 | free(s->cookie); |
| 7979 | |
| 7980 | free(s); |
| 7981 | s = s_next; |
| 7982 | }/* end while(s) */ |
| 7983 | |
| 7984 | l = p->listen; |
| 7985 | while (l) { |
| 7986 | l_next = l->next; |
| 7987 | free(l); |
| 7988 | l = l_next; |
| 7989 | }/* end while(l) */ |
| 7990 | |
| 7991 | pool_destroy((void **) p->req_cap_pool); |
| 7992 | pool_destroy((void **) p->rsp_cap_pool); |
| 7993 | p = p->next; |
| 7994 | }/* end while(p) */ |
| 7995 | |
| 7996 | if (global.chroot) free(global.chroot); |
| 7997 | if (global.pidfile) free(global.pidfile); |
| 7998 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 7999 | if (StaticReadEvent) free(StaticReadEvent); |
| 8000 | if (StaticWriteEvent) free(StaticWriteEvent); |
| 8001 | if (fdtab) free(fdtab); |
| 8002 | |
| 8003 | pool_destroy(pool_session); |
| 8004 | pool_destroy(pool_buffer); |
| 8005 | pool_destroy(pool_fdtab); |
| 8006 | pool_destroy(pool_requri); |
| 8007 | pool_destroy(pool_task); |
| 8008 | pool_destroy(pool_capture); |
| 8009 | pool_destroy(pool_appsess); |
| 8010 | |
| 8011 | if (have_appsession) { |
| 8012 | pool_destroy(apools.serverid); |
| 8013 | pool_destroy(apools.sessid); |
| 8014 | } |
| 8015 | } /* end deinit() */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8016 | |
| 8017 | int main(int argc, char **argv) { |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 8018 | struct rlimit limit; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8019 | FILE *pidfile = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8020 | init(argc, argv); |
| 8021 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8022 | signal(SIGQUIT, dump); |
| 8023 | signal(SIGUSR1, sig_soft_stop); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 8024 | signal(SIGHUP, sig_dump_state); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8025 | #ifdef DEBUG_MEMORY |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8026 | signal(SIGINT, sig_int); |
| 8027 | signal(SIGTERM, sig_term); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8028 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8029 | |
| 8030 | /* on very high loads, a sigpipe sometimes happen just between the |
| 8031 | * getsockopt() which tells "it's OK to write", and the following write :-( |
| 8032 | */ |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 8033 | #ifndef MSG_NOSIGNAL |
| 8034 | signal(SIGPIPE, SIG_IGN); |
| 8035 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8036 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8037 | /* start_proxies() sends an alert when it fails. */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8038 | if (start_proxies() < 0) |
| 8039 | exit(1); |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8040 | |
| 8041 | if (listeners == 0) { |
| 8042 | Alert("[%s.main()] No enabled listener found (check the <listen> keywords) ! Exiting.\n", argv[0]); |
| 8043 | exit(1); |
| 8044 | } |
| 8045 | |
willy tarreau | dbd3bef | 2006-01-20 19:35:18 +0100 | [diff] [blame] | 8046 | /* prepare pause/play signals */ |
| 8047 | signal(SIGTTOU, sig_pause); |
| 8048 | signal(SIGTTIN, sig_listen); |
| 8049 | |
Willy TARREAU | e3283d1 | 2006-03-01 22:15:29 +0100 | [diff] [blame^] | 8050 | if (global.mode & MODE_DAEMON) { |
| 8051 | global.mode &= ~MODE_VERBOSE; |
| 8052 | global.mode |= MODE_QUIET; |
| 8053 | } |
| 8054 | |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8055 | /* MODE_QUIET can inhibit alerts and warnings below this line */ |
| 8056 | |
| 8057 | global.mode &= ~MODE_STARTING; |
Willy TARREAU | e3283d1 | 2006-03-01 22:15:29 +0100 | [diff] [blame^] | 8058 | if ((global.mode & MODE_QUIET) && !(global.mode & MODE_VERBOSE)) { |
willy tarreau | d0fb465 | 2005-12-18 01:32:04 +0100 | [diff] [blame] | 8059 | /* detach from the tty */ |
| 8060 | fclose(stdin); fclose(stdout); fclose(stderr); |
| 8061 | close(0); close(1); close(2); |
| 8062 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8063 | |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8064 | /* open log & pid files before the chroot */ |
| 8065 | if (global.mode & MODE_DAEMON && global.pidfile != NULL) { |
| 8066 | int pidfd; |
| 8067 | unlink(global.pidfile); |
| 8068 | pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644); |
| 8069 | if (pidfd < 0) { |
| 8070 | Alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile); |
| 8071 | exit(1); |
| 8072 | } |
| 8073 | pidfile = fdopen(pidfd, "w"); |
| 8074 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8075 | |
| 8076 | /* chroot if needed */ |
| 8077 | if (global.chroot != NULL) { |
| 8078 | if (chroot(global.chroot) == -1) { |
| 8079 | Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot); |
| 8080 | exit(1); |
| 8081 | } |
| 8082 | chdir("/"); |
| 8083 | } |
| 8084 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 8085 | /* ulimits */ |
| 8086 | if (global.rlimit_nofile) { |
| 8087 | limit.rlim_cur = limit.rlim_max = global.rlimit_nofile; |
| 8088 | if (setrlimit(RLIMIT_NOFILE, &limit) == -1) { |
| 8089 | Warning("[%s.main()] Cannot raise FD limit to %d.\n", argv[0], global.rlimit_nofile); |
| 8090 | } |
| 8091 | } |
| 8092 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8093 | /* setgid / setuid */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8094 | if (global.gid && setgid(global.gid) == -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8095 | Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid); |
| 8096 | exit(1); |
| 8097 | } |
| 8098 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 8099 | if (global.uid && setuid(global.uid) == -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8100 | Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid); |
| 8101 | exit(1); |
| 8102 | } |
| 8103 | |
willy tarreau | b1285d5 | 2005-12-18 01:20:14 +0100 | [diff] [blame] | 8104 | /* check ulimits */ |
| 8105 | limit.rlim_cur = limit.rlim_max = 0; |
| 8106 | getrlimit(RLIMIT_NOFILE, &limit); |
| 8107 | if (limit.rlim_cur < global.maxsock) { |
| 8108 | 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", |
| 8109 | argv[0], limit.rlim_cur, global.maxconn, global.maxsock, global.maxsock); |
| 8110 | } |
| 8111 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8112 | if (global.mode & MODE_DAEMON) { |
| 8113 | int ret = 0; |
| 8114 | int proc; |
| 8115 | |
| 8116 | /* the father launches the required number of processes */ |
| 8117 | for (proc = 0; proc < global.nbproc; proc++) { |
| 8118 | ret = fork(); |
| 8119 | if (ret < 0) { |
| 8120 | Alert("[%s.main()] Cannot fork.\n", argv[0]); |
| 8121 | exit(1); /* there has been an error */ |
| 8122 | } |
| 8123 | else if (ret == 0) /* child breaks here */ |
| 8124 | break; |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8125 | if (pidfile != NULL) { |
| 8126 | fprintf(pidfile, "%d\n", ret); |
| 8127 | fflush(pidfile); |
| 8128 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8129 | } |
willy tarreau | fe2c5c1 | 2005-12-17 14:14:34 +0100 | [diff] [blame] | 8130 | /* close the pidfile both in children and father */ |
| 8131 | if (pidfile != NULL) |
| 8132 | fclose(pidfile); |
| 8133 | free(global.pidfile); |
| 8134 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8135 | if (proc == global.nbproc) |
| 8136 | exit(0); /* parent must leave */ |
| 8137 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 8138 | /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure |
| 8139 | * that we can detach from the TTY. We MUST NOT do it in other cases since |
| 8140 | * it would have already be done, and 0-2 would have been affected to listening |
| 8141 | * sockets |
| 8142 | */ |
| 8143 | if (!(global.mode & MODE_QUIET)) { |
| 8144 | /* detach from the tty */ |
| 8145 | fclose(stdin); fclose(stdout); fclose(stderr); |
| 8146 | close(0); close(1); close(2); /* close all fd's */ |
| 8147 | global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */ |
| 8148 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 8149 | pid = getpid(); /* update child's pid */ |
willy tarreau | e867b48 | 2005-12-17 13:28:43 +0100 | [diff] [blame] | 8150 | setsid(); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 8151 | } |
| 8152 | |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8153 | #if defined(ENABLE_EPOLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8154 | if (cfg_polling_mechanism & POLL_USE_EPOLL) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8155 | if (epoll_loop(POLL_LOOP_ACTION_INIT)) { |
| 8156 | epoll_loop(POLL_LOOP_ACTION_RUN); |
| 8157 | epoll_loop(POLL_LOOP_ACTION_CLEAN); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8158 | cfg_polling_mechanism &= POLL_USE_EPOLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8159 | } |
| 8160 | else { |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8161 | Warning("epoll() is not available. Using poll()/select() instead.\n"); |
| 8162 | cfg_polling_mechanism &= ~POLL_USE_EPOLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8163 | } |
| 8164 | } |
| 8165 | #endif |
| 8166 | |
| 8167 | #if defined(ENABLE_POLL) |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8168 | if (cfg_polling_mechanism & POLL_USE_POLL) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8169 | if (poll_loop(POLL_LOOP_ACTION_INIT)) { |
| 8170 | poll_loop(POLL_LOOP_ACTION_RUN); |
| 8171 | poll_loop(POLL_LOOP_ACTION_CLEAN); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8172 | cfg_polling_mechanism &= POLL_USE_POLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8173 | } |
| 8174 | else { |
| 8175 | Warning("poll() is not available. Using select() instead.\n"); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8176 | cfg_polling_mechanism &= ~POLL_USE_POLL; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8177 | } |
| 8178 | } |
| 8179 | #endif |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8180 | if (cfg_polling_mechanism & POLL_USE_SELECT) { |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8181 | if (select_loop(POLL_LOOP_ACTION_INIT)) { |
| 8182 | select_loop(POLL_LOOP_ACTION_RUN); |
| 8183 | select_loop(POLL_LOOP_ACTION_CLEAN); |
willy tarreau | 64a3cc3 | 2005-12-18 01:13:11 +0100 | [diff] [blame] | 8184 | cfg_polling_mechanism &= POLL_USE_SELECT; |
willy tarreau | 1c2ad21 | 2005-12-18 01:11:29 +0100 | [diff] [blame] | 8185 | } |
| 8186 | } |
| 8187 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8188 | |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8189 | /* Free all Hash Keys and all Hash elements */ |
| 8190 | appsession_cleanup(); |
| 8191 | /* Do some cleanup */ |
| 8192 | deinit(); |
| 8193 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 8194 | exit(0); |
| 8195 | } |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 8196 | |
| 8197 | #if defined(DEBUG_HASH) |
| 8198 | static void print_table(const CHTbl *htbl) { |
| 8199 | |
| 8200 | ListElmt *element; |
| 8201 | int i; |
| 8202 | appsess *asession; |
| 8203 | |
| 8204 | /***************************************************************************** |
| 8205 | * * |
| 8206 | * Display the chained hash table. * |
| 8207 | * * |
| 8208 | *****************************************************************************/ |
| 8209 | |
| 8210 | fprintf(stdout, "Table size is %d\n", chtbl_size(htbl)); |
| 8211 | |
| 8212 | for (i = 0; i < TBLSIZ; i++) { |
| 8213 | fprintf(stdout, "Bucket[%03d]\n", i); |
| 8214 | |
| 8215 | for (element = list_head(&htbl->table[i]); element != NULL; element = list_next(element)) { |
| 8216 | //fprintf(stdout, "%c", *(char *)list_data(element)); |
| 8217 | asession = (appsess *)list_data(element); |
| 8218 | fprintf(stdout, "ELEM :%s:", asession->sessid); |
| 8219 | fprintf(stdout, " Server :%s: \n", asession->serverid); |
| 8220 | //fprintf(stdout, " Server request_count :%li:\n",asession->request_count); |
| 8221 | } |
| 8222 | |
| 8223 | fprintf(stdout, "\n"); |
| 8224 | } |
| 8225 | return; |
| 8226 | } /* end print_table */ |
| 8227 | #endif |
| 8228 | |
| 8229 | static int appsession_init(void) |
| 8230 | { |
| 8231 | static int initialized = 0; |
| 8232 | int idlen; |
| 8233 | struct server *s; |
| 8234 | struct proxy *p = proxy; |
| 8235 | |
| 8236 | if (!initialized) { |
| 8237 | if (!appsession_task_init()) { |
| 8238 | apools.sessid = NULL; |
| 8239 | apools.serverid = NULL; |
| 8240 | apools.ser_waste = 0; |
| 8241 | apools.ser_use = 0; |
| 8242 | apools.ser_msize = sizeof(void *); |
| 8243 | apools.ses_waste = 0; |
| 8244 | apools.ses_use = 0; |
| 8245 | apools.ses_msize = sizeof(void *); |
| 8246 | while (p) { |
| 8247 | s = p->srv; |
| 8248 | if (apools.ses_msize < p->appsession_len) |
| 8249 | apools.ses_msize = p->appsession_len; |
| 8250 | while (s) { |
| 8251 | idlen = strlen(s->id); |
| 8252 | if (apools.ser_msize < idlen) |
| 8253 | apools.ser_msize = idlen; |
| 8254 | s = s->next; |
| 8255 | } |
| 8256 | p = p->next; |
| 8257 | } |
| 8258 | apools.ser_msize ++; /* we use strings, so reserve space for '\0' */ |
| 8259 | apools.ses_msize ++; |
| 8260 | } |
| 8261 | else { |
| 8262 | fprintf(stderr, "appsession_task_init failed\n"); |
| 8263 | return -1; |
| 8264 | } |
| 8265 | initialized ++; |
| 8266 | } |
| 8267 | return 0; |
| 8268 | } |
| 8269 | |
| 8270 | static int appsession_task_init(void) |
| 8271 | { |
| 8272 | static int initialized = 0; |
| 8273 | struct task *t; |
| 8274 | if (!initialized) { |
| 8275 | if ((t = pool_alloc(task)) == NULL) |
| 8276 | return -1; |
| 8277 | t->next = t->prev = t->rqnext = NULL; |
| 8278 | t->wq = LIST_HEAD(wait_queue); |
| 8279 | t->state = TASK_IDLE; |
| 8280 | t->context = NULL; |
| 8281 | tv_delayfrom(&t->expire, &now, TBLCHKINT); |
| 8282 | task_queue(t); |
| 8283 | t->process = appsession_refresh; |
| 8284 | initialized ++; |
| 8285 | } |
| 8286 | return 0; |
| 8287 | } |
| 8288 | |
| 8289 | static int appsession_refresh(struct task *t) { |
| 8290 | struct proxy *p = proxy; |
| 8291 | CHTbl *htbl; |
| 8292 | ListElmt *element, *last; |
| 8293 | int i; |
| 8294 | appsess *asession; |
| 8295 | void *data; |
| 8296 | |
| 8297 | while (p) { |
| 8298 | if (p->appsession_name != NULL) { |
| 8299 | htbl = &p->htbl_proxy; |
| 8300 | /* if we ever give up the use of TBLSIZ, we need to change this */ |
| 8301 | for (i = 0; i < TBLSIZ; i++) { |
| 8302 | last = NULL; |
| 8303 | for (element = list_head(&htbl->table[i]); element != NULL; element = list_next(element)) { |
| 8304 | asession = (appsess *)list_data(element); |
| 8305 | if (tv_cmp2_ms(&asession->expire, &now) <= 0) { |
| 8306 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 8307 | int len; |
| 8308 | /* |
| 8309 | on Linux NULL pointers are catched by sprintf, on solaris -> segfault |
| 8310 | */ |
| 8311 | len = sprintf(trash, "appsession_refresh: cleaning up expired Session '%s' on Server %s\n", |
| 8312 | asession->sessid, asession->serverid?asession->serverid:"(null)"); |
| 8313 | write(1, trash, len); |
| 8314 | } |
| 8315 | /* delete the expired element from within the hash table */ |
| 8316 | if ((list_rem_next(&htbl->table[i], last, (void **)&data) == 0) |
| 8317 | && (htbl->table[i].destroy != NULL)) { |
| 8318 | htbl->table[i].destroy(data); |
| 8319 | } |
| 8320 | if (last == NULL) {/* patient lost his head, get a new one */ |
| 8321 | element = list_head(&htbl->table[i]); |
| 8322 | if (element == NULL) break; /* no heads left, go to next patient */ |
| 8323 | } |
| 8324 | else |
| 8325 | element = last; |
| 8326 | }/* end if (tv_cmp2_ms(&asession->expire, &now) <= 0) */ |
| 8327 | else |
| 8328 | last = element; |
| 8329 | }/* end for (element = list_head(&htbl->table[i]); element != NULL; element = list_next(element)) */ |
| 8330 | } |
| 8331 | } |
| 8332 | p = p->next; |
| 8333 | } |
| 8334 | tv_delayfrom(&t->expire, &now, TBLCHKINT); /* check expiration every 5 seconds */ |
| 8335 | return TBLCHKINT; |
| 8336 | } /* end appsession_refresh */ |
| 8337 | |