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 |
| 3 | * 2000-2002 - 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 | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 10 | * Pending bugs (may be not fixed because not reproduced) : |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 11 | * - solaris only : sometimes, an HTTP proxy with only a dispatch address causes |
| 12 | * 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] | 13 | * the response. Seen on 1.1.8pre4, but never reproduced. May not be related to |
| 14 | * the snprintf() bug since requests we simple (GET / HTTP/1.0). |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 15 | * - cookie in insert+indirect mode sometimes segfaults ! |
| 16 | * - a proxy with an invalid config will prevent the startup even if disabled. |
| 17 | * |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 18 | * ChangeLog : |
| 19 | * |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame^] | 20 | * 2002/07/15 : 1.1.13 |
| 21 | * - tv_diff used inverted parameters which led to negative times ! |
| 22 | * 2002/07/13 : 1.1.12 |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 23 | * - fixed stats monitoring, and optimized some tv_* for most common cases. |
| 24 | * - replaced temporary 'newhdr' with 'trash' to reduce stack size |
| 25 | * - made HTTP errors more HTML-fiendly. |
| 26 | * - renamed strlcpy() to strlcpy2() because of a slightly difference between |
| 27 | * their behaviour (return value), to avoid confusion. |
| 28 | * - restricted HTTP messages to HTTP proxies only |
| 29 | * - added a 502 message when the connection has been refused by the server, |
| 30 | * to prevent clients from believing this is a zero-byte HTTP 0.9 reply. |
| 31 | * - changed 'Cache-control:' from 'no-cache="set-cookie"' to 'private' when |
| 32 | * inserting a cookie, because some caches (apache) don't understand it. |
| 33 | * - fixed processing of server headers when client is in SHUTR state |
| 34 | * 2002/07/04 : |
| 35 | * - automatically close fd's 0,1 and 2 when going daemon ; setpgrp() after |
| 36 | * setpgid() |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 37 | * 2002/06/04 : 1.1.11 |
| 38 | * - fixed multi-cookie handling in client request to allow clean deletion |
| 39 | * in insert+indirect mode. Now, only the server cookie is deleted and not |
| 40 | * all the header. Should now be compliant to RFC2109. |
| 41 | * - added a "nocache" option to "cookie" to specify that we explicitly want |
| 42 | * to add a "cache-control" header when we add a cookie. |
| 43 | * It is also possible to add an "Expires: <old-date>" to keep compatibility |
| 44 | * with old/broken caches. |
willy tarreau | 96d4037 | 2005-12-17 13:11:56 +0100 | [diff] [blame] | 45 | * 2002/05/10 : 1.1.10 |
| 46 | * - if a cookie is used in insert+indirect mode, it's desirable that the |
| 47 | * the servers don't see it. It was not possible to remove it correctly |
| 48 | * with regexps, so now it's removed automatically. |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 49 | * 2002/04/19 : 1.1.9 |
| 50 | * - don't use snprintf()'s return value as an end of message since it may |
| 51 | * be larger. This caused bus errors and segfaults in internal libc's |
| 52 | * getenv() during localtime() in send_log(). |
| 53 | * - removed dead insecure send_syslog() function and all references to it. |
| 54 | * - fixed warnings on Solaris due to buggy implementation of isXXXX(). |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 55 | * 2002/04/18 : 1.1.8 |
| 56 | * - option "dontlognull" |
| 57 | * - fixed "double space" bug in config parser |
| 58 | * - fixed an uninitialized server field in case of dispatch |
| 59 | * with no existing server which could cause a segfault during |
| 60 | * logging. |
| 61 | * - the pid logged was always the father's, which was wrong for daemons. |
| 62 | * - fixed wrong level "LOG_INFO" for message "proxy started". |
| 63 | * 2002/04/13 : |
| 64 | * - http logging is now complete : |
| 65 | * - ip:port, date, proxy, server |
| 66 | * - req_time, conn_time, hdr_time, tot_time |
| 67 | * - status, size, request |
| 68 | * - source address |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 69 | * 2002/04/12 : 1.1.7 |
| 70 | * - added option forwardfor |
| 71 | * - added reqirep, reqidel, reqiallow, reqideny, rspirep, rspidel |
| 72 | * - added "log global" in "listen" section. |
| 73 | * 2002/04/09 : |
| 74 | * - added a new "global" section : |
| 75 | * - logs |
| 76 | * - debug, quiet, daemon modes |
| 77 | * - uid, gid, chroot, nbproc, maxconn |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 78 | * 2002/04/08 : 1.1.6 |
| 79 | * - regex are now chained and not limited anymore. |
| 80 | * - unavailable server now returns HTTP/502. |
| 81 | * - increased per-line args limit to 40 |
| 82 | * - added reqallow/reqdeny to block some request on matches |
| 83 | * - added HTTP 400/403 responses |
| 84 | * 2002/04/03 : 1.1.5 |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 85 | * - connection logging displayed incorrect source address. |
| 86 | * - added proxy start/stop and server up/down log events. |
| 87 | * - replaced log message short buffers with larger trash. |
| 88 | * - enlarged buffer to 8 kB and replace buffer to 4 kB. |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 89 | * 2002/03/25 : 1.1.4 |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 90 | * - made rise/fall/interval time configurable |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 91 | * 2002/03/22 : 1.1.3 |
willy tarreau | b719f00 | 2005-12-17 12:55:07 +0100 | [diff] [blame] | 92 | * - fixed a bug : cr_expire and cw_expire were inverted in CL_STSHUT[WR] |
| 93 | * which could lead to loops. |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 94 | * 2002/03/21 : 1.1.2 |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 95 | * - fixed a bug in buffer management where we could have a loop |
| 96 | * between event_read() and process_{cli|srv} if R==BUFSIZE-MAXREWRITE. |
| 97 | * => implemented an adjustable buffer limit. |
| 98 | * - fixed a bug : expiration of tasks in wait queue timeout is used again, |
| 99 | * and running tasks are skipped. |
| 100 | * - added some debug lines for accept events. |
| 101 | * - send warnings for servers up/down. |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 102 | * 2002/03/12 : 1.1.1 |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 103 | * - fixed a bug in total failure handling |
| 104 | * - fixed a bug in timestamp comparison within same second (tv_cmp_ms) |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 105 | * 2002/03/10 : 1.1.0 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 106 | * - fixed a few timeout bugs |
| 107 | * - rearranged the task scheduler subsystem to improve performance, |
| 108 | * add new tasks, and make it easier to later port to librt ; |
| 109 | * - allow multiple accept() for one select() wake up ; |
| 110 | * - implemented internal load balancing with basic health-check ; |
| 111 | * - cookie insertion and header add/replace/delete, with better strings |
| 112 | * support. |
| 113 | * 2002/03/08 |
| 114 | * - reworked buffer handling to fix a few rewrite bugs, and |
| 115 | * improve overall performance. |
| 116 | * - implement the "purge" option to delete server cookies in direct mode. |
| 117 | * 2002/03/07 |
| 118 | * - fixed some error cases where the maxfd was not decreased. |
| 119 | * 2002/02/26 |
| 120 | * - now supports transparent proxying, at least on linux 2.4. |
| 121 | * 2002/02/12 |
| 122 | * - soft stop works again (fixed select timeout computation). |
| 123 | * - it seems that TCP proxies sometimes cannot timeout. |
| 124 | * - added a "quiet" mode. |
| 125 | * - enforce file descriptor limitation on socket() and accept(). |
| 126 | * 2001/12/30 : release of version 1.0.2 : fixed a bug in header processing |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 127 | * 2001/12/19 : release of version 1.0.1 : no MSG_NOSIGNAL on solaris |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 128 | * 2001/12/16 : release of version 1.0.0. |
| 129 | * 2001/12/16 : added syslog capability for each accepted connection. |
| 130 | * 2001/11/19 : corrected premature end of files and occasional SIGPIPE. |
| 131 | * 2001/10/31 : added health-check type servers (mode health) which replies OK then closes. |
| 132 | * 2001/10/30 : added the ability to support standard TCP proxies and HTTP proxies |
| 133 | * with or without cookies (use keyword http for this). |
| 134 | * 2001/09/01 : added client/server header replacing with regexps. |
| 135 | * eg: |
| 136 | * cliexp ^(Host:\ [^:]*).* Host:\ \1:80 |
| 137 | * srvexp ^Server:\ .* Server:\ Apache |
| 138 | * 2000/11/29 : first fully working release with complete FSMs and timeouts. |
| 139 | * 2000/11/28 : major rewrite |
| 140 | * 2000/11/26 : first write |
| 141 | * |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 142 | * TODO: |
| 143 | * - handle properly intermediate incomplete server headers. Done ? |
| 144 | * - log proxies start/stop |
| 145 | * - handle hot-reconfiguration |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 146 | * |
| 147 | */ |
| 148 | |
| 149 | #include <stdio.h> |
| 150 | #include <stdlib.h> |
| 151 | #include <unistd.h> |
| 152 | #include <string.h> |
| 153 | #include <ctype.h> |
| 154 | #include <sys/time.h> |
| 155 | #include <sys/types.h> |
| 156 | #include <sys/socket.h> |
| 157 | #include <netinet/tcp.h> |
| 158 | #include <netinet/in.h> |
| 159 | #include <arpa/inet.h> |
| 160 | #include <netdb.h> |
| 161 | #include <fcntl.h> |
| 162 | #include <errno.h> |
| 163 | #include <signal.h> |
| 164 | #include <stdarg.h> |
| 165 | #include <sys/resource.h> |
| 166 | #include <time.h> |
| 167 | #include <regex.h> |
| 168 | #include <syslog.h> |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 169 | #if defined(TPROXY) && defined(NETFILTER) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 170 | #include <linux/netfilter_ipv4.h> |
| 171 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 172 | |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame^] | 173 | #define HAPROXY_VERSION "1.1.13" |
| 174 | #define HAPROXY_DATE "2002/07/15" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 175 | |
| 176 | /* this is for libc5 for example */ |
| 177 | #ifndef TCP_NODELAY |
| 178 | #define TCP_NODELAY 1 |
| 179 | #endif |
| 180 | |
| 181 | #ifndef SHUT_RD |
| 182 | #define SHUT_RD 0 |
| 183 | #endif |
| 184 | |
| 185 | #ifndef SHUT_WR |
| 186 | #define SHUT_WR 1 |
| 187 | #endif |
| 188 | |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 189 | #define BUFSIZE 8192 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 190 | |
| 191 | // reserved buffer space for header rewriting |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 192 | #define MAXREWRITE 4096 |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 193 | #define REQURI_LEN 1024 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 194 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 195 | // max # args on a configuration line |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 196 | #define MAX_LINE_ARGS 40 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 197 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 198 | // max # of added headers per request |
| 199 | #define MAX_NEWHDR 10 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 200 | |
| 201 | // max # of matches per regexp |
| 202 | #define MAX_MATCH 10 |
| 203 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 204 | /* FIXME: serverid_len and cookiename_len are no longer checked in configuration file */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 205 | #define COOKIENAME_LEN 16 |
| 206 | #define SERVERID_LEN 16 |
| 207 | #define CONN_RETRIES 3 |
| 208 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 209 | #define CHK_CONNTIME 2000 |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 210 | #define DEF_CHKINTR 2000 |
| 211 | #define DEF_FALLTIME 3 |
| 212 | #define DEF_RISETIME 2 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 213 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 214 | /* default connections limit */ |
| 215 | #define DEFAULT_MAXCONN 2000 |
| 216 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 217 | /* how many bits are needed to code the size of an int (eg: 32bits -> 5) */ |
| 218 | #define INTBITS 5 |
| 219 | |
| 220 | /* show stats this every millisecond, 0 to disable */ |
| 221 | #ifndef STATTIME |
| 222 | #define STATTIME 2000 |
| 223 | #endif |
| 224 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 225 | /* this reduces the number of calls to select() by choosing appropriate |
| 226 | * sheduler precision in milliseconds. It should be near the minimum |
| 227 | * time that is needed by select() to collect all events. All timeouts |
| 228 | * are rounded up by adding this value prior to pass it to select(). |
| 229 | */ |
| 230 | #define SCHEDULER_RESOLUTION 9 |
| 231 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 232 | #define MINTIME(old, new) (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old))) |
| 233 | #define SETNOW(a) (*a=now) |
| 234 | |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 235 | /****** string-specific macros and functions ******/ |
| 236 | /* if a > max, then bound <a> to <max>. The macro returns the new <a> */ |
| 237 | #define UBOUND(a, max) ({ typeof(a) b = (max); if ((a) > b) (a) = b; (a); }) |
| 238 | |
| 239 | /* if a < min, then bound <a> to <min>. The macro returns the new <a> */ |
| 240 | #define LBOUND(a, min) ({ typeof(a) b = (min); if ((a) < b) (a) = b; (a); }) |
| 241 | |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 242 | /* |
| 243 | * copies at most <size-1> chars from <src> to <dst>. Last char is always |
| 244 | * set to 0, unless <size> is 0. The number of chars copied is returned |
| 245 | * (excluding the terminating zero). |
| 246 | * This code has been optimized for size and speed : on x86, it's 45 bytes |
| 247 | * long, uses only registers, and consumes only 4 cycles per char. |
| 248 | */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 249 | int strlcpy2(char *dst, const char *src, int size) { |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 250 | char *orig = dst; |
| 251 | if (size) { |
| 252 | while (--size && (*dst = *src)) { |
| 253 | src++; dst++; |
| 254 | } |
| 255 | *dst = 0; |
| 256 | } |
| 257 | return dst - orig; |
| 258 | } |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 259 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 260 | #define MEM_OPTIM |
| 261 | #ifdef MEM_OPTIM |
| 262 | /* |
| 263 | * Returns a pointer to type <type> taken from the |
| 264 | * pool <pool_type> or dynamically allocated. In the |
| 265 | * first case, <pool_type> is updated to point to the |
| 266 | * next element in the list. |
| 267 | */ |
| 268 | #define pool_alloc(type) ({ \ |
| 269 | void *p; \ |
| 270 | if ((p = pool_##type) == NULL) \ |
| 271 | p = malloc(sizeof_##type); \ |
| 272 | else { \ |
| 273 | pool_##type = *(void **)pool_##type; \ |
| 274 | } \ |
| 275 | p; \ |
| 276 | }) |
| 277 | |
| 278 | /* |
| 279 | * Puts a memory area back to the corresponding pool. |
| 280 | * Items are chained directly through a pointer that |
| 281 | * is written in the beginning of the memory area, so |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 282 | * there's no need for any carrier cell. This implies |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 283 | * that each memory area is at least as big as one |
| 284 | * pointer. |
| 285 | */ |
| 286 | #define pool_free(type, ptr) ({ \ |
| 287 | *(void **)ptr = (void *)pool_##type; \ |
| 288 | pool_##type = (void *)ptr; \ |
| 289 | }) |
| 290 | |
| 291 | #else |
| 292 | #define pool_alloc(type) (calloc(1,sizeof_##type)); |
| 293 | #define pool_free(type, ptr) (free(ptr)); |
| 294 | #endif /* MEM_OPTIM */ |
| 295 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 296 | #define sizeof_task sizeof(struct task) |
| 297 | #define sizeof_session sizeof(struct session) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 298 | #define sizeof_buffer sizeof(struct buffer) |
| 299 | #define sizeof_fdtab sizeof(struct fdtab) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 300 | #define sizeof_requri REQURI_LEN |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 301 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 302 | /* different possible states for the sockets */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 303 | #define FD_STCLOSE 0 |
| 304 | #define FD_STLISTEN 1 |
| 305 | #define FD_STCONN 2 |
| 306 | #define FD_STREADY 3 |
| 307 | #define FD_STERROR 4 |
| 308 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 309 | /* values for task->state */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 310 | #define TASK_IDLE 0 |
| 311 | #define TASK_RUNNING 1 |
| 312 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 313 | /* values for proxy->state */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 314 | #define PR_STNEW 0 |
| 315 | #define PR_STIDLE 1 |
| 316 | #define PR_STRUN 2 |
| 317 | #define PR_STDISABLED 3 |
| 318 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 319 | /* values for proxy->mode */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 320 | #define PR_MODE_TCP 0 |
| 321 | #define PR_MODE_HTTP 1 |
| 322 | #define PR_MODE_HEALTH 2 |
| 323 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 324 | /* bits for proxy->options */ |
| 325 | #define PR_O_REDISP 1 /* allow reconnection to dispatch in case of errors */ |
| 326 | #define PR_O_TRANSP 2 /* transparent mode : use original DEST as dispatch */ |
| 327 | #define PR_O_COOK_RW 4 /* rewrite all direct cookies with the right serverid */ |
| 328 | #define PR_O_COOK_IND 8 /* keep only indirect cookies */ |
| 329 | #define PR_O_COOK_INS 16 /* insert cookies when not accessing a server directly */ |
| 330 | #define PR_O_COOK_ANY (PR_O_COOK_RW | PR_O_COOK_IND | PR_O_COOK_INS) |
| 331 | #define PR_O_BALANCE_RR 32 /* balance in round-robin mode */ |
| 332 | #define PR_O_BALANCE (PR_O_BALANCE_RR) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 333 | #define PR_O_KEEPALIVE 64 /* follow keep-alive sessions */ |
| 334 | #define PR_O_FWDFOR 128 /* insert x-forwarded-for with client address */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 335 | #define PR_O_BIND_SRC 256 /* bind to a specific source address when connect()ing */ |
| 336 | #define PR_O_NULLNOLOG 512 /* a connect without request will not be logged */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 337 | #define PR_O_COOK_NOC 1024 /* add a 'Cache-control' header with the cookie */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 338 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 339 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 340 | /* various session flags */ |
| 341 | #define SN_DIRECT 1 /* connection made on the server matching the client cookie */ |
| 342 | #define SN_CLDENY 2 /* a client header matches a deny regex */ |
| 343 | #define SN_CLALLOW 4 /* a client header matches an allow regex */ |
| 344 | #define SN_SVDENY 8 /* a server header matches a deny regex */ |
| 345 | #define SN_SVALLOW 16 /* a server header matches an allow regex */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 346 | |
| 347 | /* different possible states for the client side */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 348 | #define CL_STHEADERS 0 |
| 349 | #define CL_STDATA 1 |
| 350 | #define CL_STSHUTR 2 |
| 351 | #define CL_STSHUTW 3 |
| 352 | #define CL_STCLOSE 4 |
| 353 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 354 | /* different possible states for the server side */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 355 | #define SV_STIDLE 0 |
| 356 | #define SV_STCONN 1 |
| 357 | #define SV_STHEADERS 2 |
| 358 | #define SV_STDATA 3 |
| 359 | #define SV_STSHUTR 4 |
| 360 | #define SV_STSHUTW 5 |
| 361 | #define SV_STCLOSE 6 |
| 362 | |
| 363 | /* result of an I/O event */ |
| 364 | #define RES_SILENT 0 /* didn't happen */ |
| 365 | #define RES_DATA 1 /* data were sent or received */ |
| 366 | #define RES_NULL 2 /* result is 0 (read == 0), or connect without need for writing */ |
| 367 | #define RES_ERROR 3 /* result -1 or error on the socket (eg: connect()) */ |
| 368 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 369 | /* modes of operation (global.mode) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 370 | #define MODE_DEBUG 1 |
| 371 | #define MODE_STATS 2 |
| 372 | #define MODE_LOG 4 |
| 373 | #define MODE_DAEMON 8 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 374 | #define MODE_QUIET 16 |
| 375 | |
| 376 | /* server flags */ |
| 377 | #define SRV_RUNNING 1 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 378 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 379 | /* what to do when a header matches a regex */ |
| 380 | #define ACT_ALLOW 0 /* allow the request */ |
| 381 | #define ACT_REPLACE 1 /* replace the matching header */ |
| 382 | #define ACT_REMOVE 2 /* remove the matching header */ |
| 383 | #define ACT_DENY 3 /* deny the request */ |
| 384 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 385 | /* configuration sections */ |
| 386 | #define CFG_NONE 0 |
| 387 | #define CFG_GLOBAL 1 |
| 388 | #define CFG_LISTEN 2 |
| 389 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 390 | /* 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] | 391 | #define LW_DATE 1 /* date */ |
| 392 | #define LW_CLIP 2 /* CLient IP */ |
| 393 | #define LW_SVIP 4 /* SerVer IP */ |
| 394 | #define LW_SVID 8 /* server ID */ |
| 395 | #define LW_REQ 16 /* http REQuest */ |
| 396 | #define LW_RESP 32 /* http RESPonse */ |
| 397 | #define LW_PXIP 64 /* proxy IP */ |
| 398 | #define LW_PXID 128 /* proxy ID */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 399 | #define LW_BYTES 256 /* bytes read from server */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 400 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 401 | /*********************************************************************/ |
| 402 | |
| 403 | #define LIST_HEAD(a) ((void *)(&(a))) |
| 404 | |
| 405 | /*********************************************************************/ |
| 406 | |
| 407 | struct hdr_exp { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 408 | struct hdr_exp *next; |
| 409 | regex_t *preg; /* expression to look for */ |
| 410 | int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */ |
| 411 | char *replace; /* expression to set instead */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 412 | }; |
| 413 | |
| 414 | struct buffer { |
| 415 | unsigned int l; /* data length */ |
| 416 | 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] | 417 | char *rlim; /* read limit, used for header rewriting */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 418 | unsigned long long total; /* total data read */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 419 | char data[BUFSIZE]; |
| 420 | }; |
| 421 | |
| 422 | struct server { |
| 423 | struct server *next; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 424 | int state; /* server state (SRV_*) */ |
| 425 | int cklen; /* the len of the cookie, to speed up checks */ |
| 426 | char *cookie; /* the id set in the cookie */ |
| 427 | char *id; /* just for identification */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 428 | struct sockaddr_in addr; /* the address to connect to */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 429 | int health; /* 0->rise-1 = bad; rise->rise+fall-1 = good */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 430 | int rise, fall; /* time in iterations */ |
| 431 | int inter; /* time in milliseconds */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 432 | int result; /* 0 = connect OK, -1 = connect KO */ |
| 433 | 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] | 434 | struct proxy *proxy; /* the proxy this server belongs to */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 435 | }; |
| 436 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 437 | /* The base for all tasks */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 438 | struct task { |
| 439 | struct task *next, *prev; /* chaining ... */ |
| 440 | struct task *rqnext; /* chaining in run queue ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 441 | struct task *wq; /* the wait queue this task is in */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 442 | int state; /* task state : IDLE or RUNNING */ |
| 443 | 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] | 444 | int (*process)(struct task *t); /* the function which processes the task */ |
| 445 | void *context; /* the task's context */ |
| 446 | }; |
| 447 | |
| 448 | /* WARNING: if new fields are added, they must be initialized in event_accept() */ |
| 449 | struct session { |
| 450 | struct task *task; /* the task associated with this session */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 451 | /* application specific below */ |
| 452 | struct timeval crexpire; /* expiration date for a client read */ |
| 453 | struct timeval cwexpire; /* expiration date for a client write */ |
| 454 | struct timeval srexpire; /* expiration date for a server read */ |
| 455 | struct timeval swexpire; /* expiration date for a server write */ |
| 456 | struct timeval cnexpire; /* expiration date for a connect */ |
| 457 | char res_cr, res_cw, res_sr, res_sw;/* results of some events */ |
| 458 | struct proxy *proxy; /* the proxy this socket belongs to */ |
| 459 | int cli_fd; /* the client side fd */ |
| 460 | int srv_fd; /* the server side fd */ |
| 461 | int cli_state; /* state of the client side */ |
| 462 | int srv_state; /* state of the server side */ |
| 463 | int conn_retries; /* number of connect retries left */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 464 | int flags; /* some flags describing the session */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 465 | struct buffer *req; /* request buffer */ |
| 466 | struct buffer *rep; /* response buffer */ |
| 467 | struct sockaddr_in cli_addr; /* the client address */ |
| 468 | struct sockaddr_in srv_addr; /* the address to connect to */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 469 | struct server *srv; /* the server being used */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 470 | struct { |
| 471 | int logwait; /* log fields waiting to be collected : LW_* */ |
| 472 | struct timeval tv_accept; /* date of the accept() (beginning of the session) */ |
| 473 | long t_request; /* delay before the end of the request arrives, -1 if never occurs */ |
| 474 | long t_connect; /* delay before the connect() to the server succeeds, -1 if never occurs */ |
| 475 | long t_data; /* delay before the first data byte from the server ... */ |
| 476 | unsigned long t_close; /* total session duration */ |
| 477 | char *uri; /* first line if log needed, NULL otherwise */ |
| 478 | int status; /* HTTP status from the server, negative if from proxy */ |
| 479 | long long bytes; /* number of bytes transferred from the server */ |
| 480 | } logs; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 481 | }; |
| 482 | |
| 483 | struct proxy { |
| 484 | int listen_fd; /* the listen socket */ |
| 485 | int state; /* proxy state */ |
| 486 | struct sockaddr_in listen_addr; /* the address we listen to */ |
| 487 | struct sockaddr_in dispatch_addr; /* the default address to connect to */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 488 | struct server *srv, *cursrv; /* known servers, current server */ |
| 489 | int nbservers; /* # of servers */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 490 | char *cookie_name; /* name of the cookie to look for */ |
| 491 | int clitimeout; /* client I/O timeout (in milliseconds) */ |
| 492 | int srvtimeout; /* server I/O timeout (in milliseconds) */ |
| 493 | int contimeout; /* connect timeout (in milliseconds) */ |
| 494 | char *id; /* proxy id */ |
| 495 | int nbconn; /* # of active sessions */ |
| 496 | int maxconn; /* max # of active sessions */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 497 | int conn_retries; /* maximum number of connect retries */ |
| 498 | int options; /* PR_O_REDISP, PR_O_TRANSP */ |
| 499 | 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] | 500 | 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] | 501 | struct proxy *next; |
| 502 | struct sockaddr_in logsrv1, logsrv2; /* 2 syslog servers */ |
| 503 | char logfac1, logfac2; /* log facility for both servers. -1 = disabled */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 504 | int to_log; /* things to be logged (LW_*) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 505 | struct timeval stop_time; /* date to stop listening, when stopping != 0 */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 506 | int nb_reqadd, nb_rspadd; |
| 507 | struct hdr_exp *req_exp; /* regular expressions for request headers */ |
| 508 | struct hdr_exp *rsp_exp; /* regular expressions for response headers */ |
| 509 | 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] | 510 | int grace; /* grace time after stop request */ |
| 511 | }; |
| 512 | |
| 513 | /* info about one given fd */ |
| 514 | struct fdtab { |
| 515 | int (*read)(int fd); /* read function */ |
| 516 | int (*write)(int fd); /* write function */ |
| 517 | struct task *owner; /* the session (or proxy) associated with this fd */ |
| 518 | int state; /* the state of this fd */ |
| 519 | }; |
| 520 | |
| 521 | /*********************************************************************/ |
| 522 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 523 | int cfg_maxpconn = 2000; /* # of simultaneous connections per proxy (-N) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 524 | char *cfg_cfgfile = NULL; /* configuration file */ |
| 525 | char *progname = NULL; /* program name */ |
| 526 | int pid; /* current process id */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 527 | |
| 528 | /* global options */ |
| 529 | static struct { |
| 530 | int uid; |
| 531 | int gid; |
| 532 | int nbproc; |
| 533 | int maxconn; |
| 534 | int maxsock; /* max # of sockets */ |
| 535 | int mode; |
| 536 | char *chroot; |
| 537 | int logfac1, logfac2; |
| 538 | struct sockaddr_in logsrv1, logsrv2; |
| 539 | } global = { |
| 540 | logfac1 : -1, |
| 541 | logfac2 : -1, |
| 542 | /* others NULL OK */ |
| 543 | }; |
| 544 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 545 | /*********************************************************************/ |
| 546 | |
| 547 | fd_set *ReadEvent, |
| 548 | *WriteEvent, |
| 549 | *StaticReadEvent, |
| 550 | *StaticWriteEvent; |
| 551 | |
| 552 | void **pool_session = NULL, |
| 553 | **pool_buffer = NULL, |
| 554 | **pool_fdtab = NULL, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 555 | **pool_requri = NULL, |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 556 | **pool_task = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 557 | |
| 558 | struct proxy *proxy = NULL; /* list of all existing proxies */ |
| 559 | struct fdtab *fdtab = NULL; /* array of all the file descriptors */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 560 | struct task *rq = NULL; /* global run queue */ |
| 561 | struct task wait_queue = { /* global wait queue */ |
| 562 | prev:LIST_HEAD(wait_queue), |
| 563 | next:LIST_HEAD(wait_queue) |
| 564 | }; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 565 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 566 | static int totalconn = 0; /* total # of terminated sessions */ |
| 567 | static int actconn = 0; /* # of active sessions */ |
| 568 | static int maxfd = 0; /* # of the highest fd + 1 */ |
| 569 | static int listeners = 0; /* # of listeners */ |
| 570 | static int stopping = 0; /* non zero means stopping in progress */ |
| 571 | static struct timeval now = {0,0}; /* the current date at any moment */ |
| 572 | |
| 573 | 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] | 574 | /* 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] | 575 | static char trash[BUFSIZE]; |
| 576 | |
| 577 | /* |
| 578 | * Syslog facilities and levels |
| 579 | */ |
| 580 | |
| 581 | #define MAX_SYSLOG_LEN 1024 |
| 582 | #define NB_LOG_FACILITIES 24 |
| 583 | const char *log_facilities[NB_LOG_FACILITIES] = { |
| 584 | "kern", "user", "mail", "daemon", |
| 585 | "auth", "syslog", "lpr", "news", |
| 586 | "uucp", "cron", "auth2", "ftp", |
| 587 | "ntp", "audit", "alert", "cron2", |
| 588 | "local0", "local1", "local2", "local3", |
| 589 | "local4", "local5", "local6", "local7" |
| 590 | }; |
| 591 | |
| 592 | |
| 593 | #define NB_LOG_LEVELS 8 |
| 594 | const char *log_levels[NB_LOG_LEVELS] = { |
| 595 | "emerg", "alert", "crit", "err", |
| 596 | "warning", "notice", "info", "debug" |
| 597 | }; |
| 598 | |
| 599 | #define SYSLOG_PORT 514 |
| 600 | |
| 601 | const char *monthname[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 602 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
| 603 | #define MAX_HOSTNAME_LEN 32 |
| 604 | static char hostname[MAX_HOSTNAME_LEN] = ""; |
| 605 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 606 | const char *HTTP_400 = |
| 607 | "HTTP/1.0 400 Bad request\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 608 | "Cache-Control: no-cache\r\n" |
| 609 | "Connection: close\r\n" |
| 610 | "\r\n" |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 611 | "<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] | 612 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 613 | const char *HTTP_403 = |
| 614 | "HTTP/1.0 403 Forbidden\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 615 | "Cache-Control: no-cache\r\n" |
| 616 | "Connection: close\r\n" |
| 617 | "\r\n" |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 618 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n"; |
| 619 | |
| 620 | const char *HTTP_500 = |
| 621 | "HTTP/1.0 500 Server Error\r\n" |
| 622 | "Cache-Control: no-cache\r\n" |
| 623 | "Connection: close\r\n" |
| 624 | "\r\n" |
| 625 | "<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] | 626 | |
| 627 | const char *HTTP_502 = |
| 628 | "HTTP/1.0 502 Proxy Error\r\n" |
| 629 | "Cache-Control: no-cache\r\n" |
| 630 | "Connection: close\r\n" |
| 631 | "\r\n" |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 632 | "<html><body><h1>502 Proxy Error</h1>\nNo server is available to handle this request.\n</body></html>\n"; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 633 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 634 | /*********************************************************************/ |
| 635 | /* statistics ******************************************************/ |
| 636 | /*********************************************************************/ |
| 637 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 638 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 639 | static int stats_tsk_lsrch, stats_tsk_rsrch, |
| 640 | stats_tsk_good, stats_tsk_right, stats_tsk_left, |
| 641 | stats_tsk_new, stats_tsk_nsrch; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 642 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 643 | |
| 644 | |
| 645 | /*********************************************************************/ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 646 | /* debugging *******************************************************/ |
| 647 | /*********************************************************************/ |
| 648 | #ifdef DEBUG_FULL |
| 649 | static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 650 | static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 651 | #endif |
| 652 | |
| 653 | /*********************************************************************/ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 654 | /* function prototypes *********************************************/ |
| 655 | /*********************************************************************/ |
| 656 | |
| 657 | int event_accept(int fd); |
| 658 | int event_cli_read(int fd); |
| 659 | int event_cli_write(int fd); |
| 660 | int event_srv_read(int fd); |
| 661 | int event_srv_write(int fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 662 | int process_session(struct task *t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 663 | |
| 664 | /*********************************************************************/ |
| 665 | /* general purpose functions ***************************************/ |
| 666 | /*********************************************************************/ |
| 667 | |
| 668 | void display_version() { |
| 669 | printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n"); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 670 | printf("Copyright 2000-2002 Willy Tarreau <willy AT meta-x DOT org>\n\n"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /* |
| 674 | * This function prints the command line usage and exits |
| 675 | */ |
| 676 | void usage(char *name) { |
| 677 | display_version(); |
| 678 | fprintf(stderr, |
| 679 | "Usage : %s -f <cfgfile> [ -vd" |
| 680 | #if STATTIME > 0 |
| 681 | "sl" |
| 682 | #endif |
| 683 | "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n" |
| 684 | " -v displays version\n" |
| 685 | " -d enters debug mode\n" |
| 686 | #if STATTIME > 0 |
| 687 | " -s enables statistics output\n" |
| 688 | " -l enables long statistics format\n" |
| 689 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 690 | " -D goes daemon ; implies -q\n" |
| 691 | " -q quiet mode : don't display messages\n" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 692 | " -n sets the maximum total # of connections (%d)\n" |
| 693 | " -N sets the default, per-proxy maximum # of connections (%d)\n\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 694 | name, DEFAULT_MAXCONN, cfg_maxpconn); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 695 | exit(1); |
| 696 | } |
| 697 | |
| 698 | |
| 699 | /* |
| 700 | * Displays the message on stderr with the date and pid. |
| 701 | */ |
| 702 | void Alert(char *fmt, ...) { |
| 703 | va_list argp; |
| 704 | struct timeval tv; |
| 705 | struct tm *tm; |
| 706 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 707 | if (!(global.mode & MODE_QUIET)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 708 | va_start(argp, fmt); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 709 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 710 | gettimeofday(&tv, NULL); |
| 711 | tm=localtime(&tv.tv_sec); |
| 712 | fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ", |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 713 | 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] | 714 | vfprintf(stderr, fmt, argp); |
| 715 | fflush(stderr); |
| 716 | va_end(argp); |
| 717 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | |
| 721 | /* |
| 722 | * Displays the message on stderr with the date and pid. |
| 723 | */ |
| 724 | void Warning(char *fmt, ...) { |
| 725 | va_list argp; |
| 726 | struct timeval tv; |
| 727 | struct tm *tm; |
| 728 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 729 | if (!(global.mode & MODE_QUIET)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 730 | va_start(argp, fmt); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 731 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 732 | gettimeofday(&tv, NULL); |
| 733 | tm=localtime(&tv.tv_sec); |
| 734 | fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ", |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 735 | 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] | 736 | vfprintf(stderr, fmt, argp); |
| 737 | fflush(stderr); |
| 738 | va_end(argp); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | * Displays the message on <out> only if quiet mode is not set. |
| 744 | */ |
| 745 | void qfprintf(FILE *out, char *fmt, ...) { |
| 746 | va_list argp; |
| 747 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 748 | if (!(global.mode & MODE_QUIET)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 749 | va_start(argp, fmt); |
| 750 | vfprintf(out, fmt, argp); |
| 751 | fflush(out); |
| 752 | va_end(argp); |
| 753 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | |
| 757 | /* |
| 758 | * converts <str> to a struct sockaddr_in* which is locally allocated. |
| 759 | * The format is "addr:port", where "addr" can be empty or "*" to indicate |
| 760 | * INADDR_ANY. |
| 761 | */ |
| 762 | struct sockaddr_in *str2sa(char *str) { |
| 763 | static struct sockaddr_in sa; |
| 764 | char *c; |
| 765 | int port; |
| 766 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 767 | memset(&sa, 0, sizeof(sa)); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 768 | str=strdup(str); |
| 769 | |
| 770 | if ((c=strrchr(str,':')) != NULL) { |
| 771 | *c++=0; |
| 772 | port=atol(c); |
| 773 | } |
| 774 | else |
| 775 | port=0; |
| 776 | |
| 777 | if (*str == '*' || *str == '\0') { /* INADDR_ANY */ |
| 778 | sa.sin_addr.s_addr = INADDR_ANY; |
| 779 | } |
| 780 | else if ( |
| 781 | #ifndef SOLARIS |
| 782 | !inet_aton(str, &sa.sin_addr) |
| 783 | #else |
| 784 | !inet_pton(AF_INET, str, &sa.sin_addr) |
| 785 | #endif |
| 786 | ) { |
| 787 | struct hostent *he; |
| 788 | |
| 789 | if ((he = gethostbyname(str)) == NULL) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 790 | Alert("Invalid server name: <%s>\n", str); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 791 | } |
| 792 | else |
| 793 | sa.sin_addr = *(struct in_addr *) *(he->h_addr_list); |
| 794 | } |
| 795 | sa.sin_port=htons(port); |
| 796 | sa.sin_family=AF_INET; |
| 797 | |
| 798 | free(str); |
| 799 | return &sa; |
| 800 | } |
| 801 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 802 | |
| 803 | /* |
| 804 | * This function sends a syslog message to both log servers of a proxy, |
| 805 | * or to global log servers if the proxy is NULL. |
| 806 | * It also tries not to waste too much time computing the message header. |
| 807 | * It doesn't care about errors nor does it report them. |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 808 | */ |
| 809 | void send_log(struct proxy *p, int level, char *message, ...) { |
| 810 | static int logfd = -1; /* syslog UDP socket */ |
| 811 | static long tvsec = -1; /* to force the string to be initialized */ |
| 812 | struct timeval tv; |
| 813 | va_list argp; |
| 814 | static char logmsg[MAX_SYSLOG_LEN]; |
| 815 | static char *dataptr = NULL; |
| 816 | int fac_level; |
| 817 | int hdr_len, data_len; |
| 818 | struct sockaddr_in *sa[2]; |
| 819 | int facilities[2]; |
| 820 | int nbloggers = 0; |
| 821 | char *log_ptr; |
| 822 | |
| 823 | if (logfd < 0) { |
| 824 | if ((logfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) |
| 825 | return; |
| 826 | } |
| 827 | |
| 828 | if (level < 0 || progname == NULL || message == NULL) |
| 829 | return; |
| 830 | |
| 831 | gettimeofday(&tv, NULL); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 832 | if (tv.tv_sec != tvsec || dataptr == NULL) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 833 | /* this string is rebuild only once a second */ |
| 834 | struct tm *tm = localtime(&tv.tv_sec); |
| 835 | tvsec = tv.tv_sec; |
| 836 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 837 | hdr_len = snprintf(logmsg, sizeof(logmsg), |
| 838 | "<<<<>%s %2d %02d:%02d:%02d %s[%d]: ", |
| 839 | monthname[tm->tm_mon], |
| 840 | tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 841 | progname, pid); |
| 842 | /* WARNING: depending upon implementations, snprintf may return |
| 843 | * either -1 or the number of bytes that would be needed to store |
| 844 | * the total message. In both cases, we must adjust it. |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 845 | */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 846 | if (hdr_len < 0 || hdr_len > sizeof(logmsg)) |
| 847 | hdr_len = sizeof(logmsg); |
| 848 | |
| 849 | dataptr = logmsg + hdr_len; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | va_start(argp, message); |
| 853 | data_len = vsnprintf(dataptr, logmsg + sizeof(logmsg) - dataptr, message, argp); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 854 | if (data_len < 0 || data_len > (logmsg + sizeof(logmsg) - dataptr)) |
| 855 | data_len = logmsg + sizeof(logmsg) - dataptr; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 856 | va_end(argp); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 857 | dataptr[data_len - 1] = '\n'; /* force a break on ultra-long lines */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 858 | |
| 859 | if (p == NULL) { |
| 860 | if (global.logfac1 >= 0) { |
| 861 | sa[nbloggers] = &global.logsrv1; |
| 862 | facilities[nbloggers] = global.logfac1; |
| 863 | nbloggers++; |
| 864 | } |
| 865 | if (global.logfac2 >= 0) { |
| 866 | sa[nbloggers] = &global.logsrv2; |
| 867 | facilities[nbloggers] = global.logfac2; |
| 868 | nbloggers++; |
| 869 | } |
| 870 | } else { |
| 871 | if (p->logfac1 >= 0) { |
| 872 | sa[nbloggers] = &p->logsrv1; |
| 873 | facilities[nbloggers] = p->logfac1; |
| 874 | nbloggers++; |
| 875 | } |
| 876 | if (p->logfac2 >= 0) { |
| 877 | sa[nbloggers] = &p->logsrv2; |
| 878 | facilities[nbloggers] = p->logfac2; |
| 879 | nbloggers++; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | while (nbloggers-- > 0) { |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 884 | /* For each target, we may have a different facility. |
| 885 | * We can also have a different log level for each message. |
| 886 | * This induces variations in the message header length. |
| 887 | * Since we don't want to recompute it each time, nor copy it every |
| 888 | * time, we only change the facility in the pre-computed header, |
| 889 | * and we change the pointer to the header accordingly. |
| 890 | */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 891 | fac_level = (facilities[nbloggers] << 3) + level; |
| 892 | log_ptr = logmsg + 3; /* last digit of the log level */ |
| 893 | do { |
| 894 | *log_ptr = '0' + fac_level % 10; |
| 895 | fac_level /= 10; |
| 896 | log_ptr--; |
| 897 | } while (fac_level && log_ptr > logmsg); |
| 898 | *log_ptr = '<'; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 899 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 900 | /* 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] | 901 | |
| 902 | #ifndef MSG_NOSIGNAL |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 903 | sendto(logfd, log_ptr, dataptr + data_len - log_ptr, MSG_DONTWAIT, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 904 | (struct sockaddr *)sa[nbloggers], sizeof(**sa)); |
| 905 | #else |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 906 | 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] | 907 | (struct sockaddr *)sa[nbloggers], sizeof(**sa)); |
| 908 | #endif |
| 909 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | |
| 913 | /* sets <tv> to the current time */ |
| 914 | static inline struct timeval *tv_now(struct timeval *tv) { |
| 915 | if (tv) |
| 916 | gettimeofday(tv, NULL); |
| 917 | return tv; |
| 918 | } |
| 919 | |
| 920 | /* |
| 921 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 922 | */ |
| 923 | static inline struct timeval *tv_delayfrom(struct timeval *tv, struct timeval *from, int ms) { |
| 924 | if (!tv || !from) |
| 925 | return NULL; |
| 926 | tv->tv_usec = from->tv_usec + (ms%1000)*1000; |
| 927 | tv->tv_sec = from->tv_sec + (ms/1000); |
| 928 | while (tv->tv_usec >= 1000000) { |
| 929 | tv->tv_usec -= 1000000; |
| 930 | tv->tv_sec++; |
| 931 | } |
| 932 | return tv; |
| 933 | } |
| 934 | |
| 935 | /* |
| 936 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
| 937 | */ |
| 938 | static inline int tv_cmp(struct timeval *tv1, struct timeval *tv2) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 939 | if (tv1->tv_sec < tv2->tv_sec) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 940 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 941 | else if (tv1->tv_sec > tv2->tv_sec) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 942 | return 1; |
| 943 | else if (tv1->tv_usec < tv2->tv_usec) |
| 944 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 945 | else if (tv1->tv_usec > tv2->tv_usec) |
| 946 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 947 | else |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | /* |
| 952 | * returns the absolute difference, in ms, between tv1 and tv2 |
| 953 | */ |
| 954 | unsigned long tv_delta(struct timeval *tv1, struct timeval *tv2) { |
| 955 | int cmp; |
| 956 | unsigned long ret; |
| 957 | |
| 958 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 959 | cmp = tv_cmp(tv1, tv2); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 960 | if (!cmp) |
| 961 | return 0; /* same dates, null diff */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 962 | else if (cmp < 0) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 963 | struct timeval *tmp = tv1; |
| 964 | tv1 = tv2; |
| 965 | tv2 = tmp; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 966 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 967 | ret = (tv1->tv_sec - tv2->tv_sec) * 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 968 | if (tv1->tv_usec > tv2->tv_usec) |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 969 | ret += (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 970 | else |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 971 | ret -= (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 972 | return (unsigned long) ret; |
| 973 | } |
| 974 | |
| 975 | /* |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 976 | * returns the difference, in ms, between tv1 and tv2 |
| 977 | */ |
| 978 | static inline unsigned long tv_diff(struct timeval *tv1, struct timeval *tv2) { |
| 979 | unsigned long ret; |
| 980 | |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame^] | 981 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
| 982 | if (tv2->tv_usec > tv1->tv_usec) |
| 983 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 984 | else |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame^] | 985 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 986 | return (unsigned long) ret; |
| 987 | } |
| 988 | |
| 989 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 990 | * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
| 991 | */ |
| 992 | static inline int tv_cmp_ms(struct timeval *tv1, struct timeval *tv2) { |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 993 | if (tv1->tv_sec == tv2->tv_sec) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 994 | if (tv2->tv_usec > tv1->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 995 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 996 | else if (tv1->tv_usec > tv2->tv_usec + 1000) |
| 997 | return 1; |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 998 | else |
| 999 | return 0; |
| 1000 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1001 | else if ((tv2->tv_sec > tv1->tv_sec + 1) || |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1002 | ((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] | 1003 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1004 | else if ((tv1->tv_sec > tv2->tv_sec + 1) || |
| 1005 | ((tv1->tv_sec == tv2->tv_sec + 1) && (tv1->tv_usec + 1000000 > tv2->tv_usec + 1000))) |
| 1006 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1007 | else |
| 1008 | return 0; |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * returns the remaining time between tv1=now and event=tv2 |
| 1013 | * if tv2 is passed, 0 is returned. |
| 1014 | */ |
| 1015 | static inline unsigned long tv_remain(struct timeval *tv1, struct timeval *tv2) { |
| 1016 | unsigned long ret; |
| 1017 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1018 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 1019 | return 0; /* event elapsed */ |
| 1020 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1021 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1022 | if (tv2->tv_usec > tv1->tv_usec) |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1023 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1024 | else |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1025 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1026 | return (unsigned long) ret; |
| 1027 | } |
| 1028 | |
| 1029 | |
| 1030 | /* |
| 1031 | * zeroes a struct timeval |
| 1032 | */ |
| 1033 | |
| 1034 | static inline struct timeval *tv_eternity(struct timeval *tv) { |
| 1035 | tv->tv_sec = tv->tv_usec = 0; |
| 1036 | return tv; |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | * returns 1 if tv is null, else 0 |
| 1041 | */ |
| 1042 | static inline int tv_iseternity(struct timeval *tv) { |
| 1043 | if (tv->tv_sec == 0 && tv->tv_usec == 0) |
| 1044 | return 1; |
| 1045 | else |
| 1046 | return 0; |
| 1047 | } |
| 1048 | |
| 1049 | /* |
| 1050 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 1051 | * considering that 0 is the eternity. |
| 1052 | */ |
| 1053 | static inline int tv_cmp2(struct timeval *tv1, struct timeval *tv2) { |
| 1054 | if (tv_iseternity(tv1)) |
| 1055 | if (tv_iseternity(tv2)) |
| 1056 | return 0; /* same */ |
| 1057 | else |
| 1058 | return 1; /* tv1 later than tv2 */ |
| 1059 | else if (tv_iseternity(tv2)) |
| 1060 | return -1; /* tv2 later than tv1 */ |
| 1061 | |
| 1062 | if (tv1->tv_sec > tv2->tv_sec) |
| 1063 | return 1; |
| 1064 | else if (tv1->tv_sec < tv2->tv_sec) |
| 1065 | return -1; |
| 1066 | else if (tv1->tv_usec > tv2->tv_usec) |
| 1067 | return 1; |
| 1068 | else if (tv1->tv_usec < tv2->tv_usec) |
| 1069 | return -1; |
| 1070 | else |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | /* |
| 1075 | * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 1076 | * considering that 0 is the eternity. |
| 1077 | */ |
| 1078 | static inline int tv_cmp2_ms(struct timeval *tv1, struct timeval *tv2) { |
| 1079 | if (tv_iseternity(tv1)) |
| 1080 | if (tv_iseternity(tv2)) |
| 1081 | return 0; /* same */ |
| 1082 | else |
| 1083 | return 1; /* tv1 later than tv2 */ |
| 1084 | else if (tv_iseternity(tv2)) |
| 1085 | return -1; /* tv2 later than tv1 */ |
| 1086 | |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1087 | if (tv1->tv_sec == tv2->tv_sec) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1088 | if (tv1->tv_usec > tv2->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1089 | return 1; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1090 | else if (tv2->tv_usec > tv1->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1091 | return -1; |
| 1092 | else |
| 1093 | return 0; |
| 1094 | } |
| 1095 | else if ((tv1->tv_sec > tv2->tv_sec + 1) || |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1096 | ((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] | 1097 | return 1; |
| 1098 | else if ((tv2->tv_sec > tv1->tv_sec + 1) || |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1099 | ((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] | 1100 | return -1; |
| 1101 | else |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | /* |
| 1106 | * returns the first event between tv1 and tv2 into tvmin. |
| 1107 | * a zero tv is ignored. tvmin is returned. |
| 1108 | */ |
| 1109 | static inline struct timeval *tv_min(struct timeval *tvmin, |
| 1110 | struct timeval *tv1, struct timeval *tv2) { |
| 1111 | |
| 1112 | if (tv_cmp2(tv1, tv2) <= 0) |
| 1113 | *tvmin = *tv1; |
| 1114 | else |
| 1115 | *tvmin = *tv2; |
| 1116 | |
| 1117 | return tvmin; |
| 1118 | } |
| 1119 | |
| 1120 | |
| 1121 | |
| 1122 | /***********************************************************/ |
| 1123 | /* fd management ***************************************/ |
| 1124 | /***********************************************************/ |
| 1125 | |
| 1126 | |
| 1127 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1128 | /* Deletes an FD from the fdsets, and recomputes the maxfd limit. |
| 1129 | * The file descriptor is also closed. |
| 1130 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1131 | static inline void fd_delete(int fd) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1132 | FD_CLR(fd, StaticReadEvent); |
| 1133 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1134 | close(fd); |
| 1135 | fdtab[fd].state = FD_STCLOSE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1136 | |
| 1137 | while ((maxfd-1 >= 0) && (fdtab[maxfd-1].state == FD_STCLOSE)) |
| 1138 | maxfd--; |
| 1139 | } |
| 1140 | |
| 1141 | /* recomputes the maxfd limit from the fd */ |
| 1142 | static inline void fd_insert(int fd) { |
| 1143 | if (fd+1 > maxfd) |
| 1144 | maxfd = fd+1; |
| 1145 | } |
| 1146 | |
| 1147 | /*************************************************************/ |
| 1148 | /* task management ***************************************/ |
| 1149 | /*************************************************************/ |
| 1150 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1151 | /* puts the task <t> in run queue <q>, and returns <t> */ |
| 1152 | static inline struct task *task_wakeup(struct task **q, struct task *t) { |
| 1153 | if (t->state == TASK_RUNNING) |
| 1154 | return t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1155 | else { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1156 | t->rqnext = *q; |
| 1157 | t->state = TASK_RUNNING; |
| 1158 | return *q = t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1162 | /* removes the task <t> from the queue <q> |
| 1163 | * <s> MUST be <q>'s first task. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1164 | * set the run queue to point to the next one, and return it |
| 1165 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1166 | static inline struct task *task_sleep(struct task **q, struct task *t) { |
| 1167 | if (t->state == TASK_RUNNING) { |
| 1168 | *q = t->rqnext; |
| 1169 | t->state = TASK_IDLE; /* tell that s has left the run queue */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1170 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1171 | return *q; /* return next running task */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1175 | * 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] | 1176 | * from the run queue. A pointer to the task itself is returned. |
| 1177 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1178 | static inline struct task *task_delete(struct task *t) { |
| 1179 | t->prev->next = t->next; |
| 1180 | t->next->prev = t->prev; |
| 1181 | return t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1185 | * 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] | 1186 | */ |
| 1187 | static inline void task_free(struct task *t) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1188 | pool_free(task, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1189 | } |
| 1190 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1191 | /* 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] | 1192 | * may be only moved or left where it was, depending on its timing requirements. |
| 1193 | * <task> is returned. |
| 1194 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1195 | struct task *task_queue(struct task *task) { |
| 1196 | struct task *list = task->wq; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1197 | struct task *start_from; |
| 1198 | |
| 1199 | /* first, test if the task was already in a list */ |
| 1200 | if (task->prev == NULL) { |
| 1201 | // start_from = list; |
| 1202 | start_from = list->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1203 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1204 | stats_tsk_new++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1205 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1206 | /* insert the unlinked <task> into the list, searching back from the last entry */ |
| 1207 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1208 | start_from = start_from->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1209 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1210 | stats_tsk_nsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1211 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | // while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1215 | // start_from = start_from->next; |
| 1216 | // stats_tsk_nsrch++; |
| 1217 | // } |
| 1218 | } |
| 1219 | else if (task->prev == list || |
| 1220 | tv_cmp2(&task->expire, &task->prev->expire) >= 0) { /* walk right */ |
| 1221 | start_from = task->next; |
| 1222 | if (start_from == list || tv_cmp2(&task->expire, &start_from->expire) <= 0) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1223 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1224 | stats_tsk_good++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1225 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1226 | return task; /* it's already in the right place */ |
| 1227 | } |
| 1228 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1229 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1230 | stats_tsk_right++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1231 | #endif |
| 1232 | |
| 1233 | /* if the task is not at the right place, there's little chance that |
| 1234 | * it has only shifted a bit, and it will nearly always be queued |
| 1235 | * at the end of the list because of constant timeouts |
| 1236 | * (observed in real case). |
| 1237 | */ |
| 1238 | #ifndef WE_REALLY_THINK_THAT_THIS_TASK_MAY_HAVE_SHIFTED |
| 1239 | start_from = list->prev; /* assume we'll queue to the end of the list */ |
| 1240 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1241 | start_from = start_from->prev; |
| 1242 | #if STATTIME > 0 |
| 1243 | stats_tsk_lsrch++; |
| 1244 | #endif |
| 1245 | } |
| 1246 | #else /* WE_REALLY_... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1247 | /* insert the unlinked <task> into the list, searching after position <start_from> */ |
| 1248 | while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1249 | start_from = start_from->next; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1250 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1251 | stats_tsk_rsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1252 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1253 | } |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1254 | #endif /* WE_REALLY_... */ |
| 1255 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1256 | /* we need to unlink it now */ |
| 1257 | task_delete(task); |
| 1258 | } |
| 1259 | else { /* walk left. */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1260 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1261 | stats_tsk_left++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1262 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1263 | #ifdef LEFT_TO_TOP /* not very good */ |
| 1264 | start_from = list; |
| 1265 | while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1266 | start_from = start_from->next; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1267 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1268 | stats_tsk_lsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1269 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1270 | } |
| 1271 | #else |
| 1272 | start_from = task->prev->prev; /* valid because of the previous test above */ |
| 1273 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1274 | start_from = start_from->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1275 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1276 | stats_tsk_lsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1277 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1278 | } |
| 1279 | #endif |
| 1280 | /* we need to unlink it now */ |
| 1281 | task_delete(task); |
| 1282 | } |
| 1283 | task->prev = start_from; |
| 1284 | task->next = start_from->next; |
| 1285 | task->next->prev = task; |
| 1286 | start_from->next = task; |
| 1287 | return task; |
| 1288 | } |
| 1289 | |
| 1290 | |
| 1291 | /*********************************************************************/ |
| 1292 | /* more specific functions ***************************************/ |
| 1293 | /*********************************************************************/ |
| 1294 | |
| 1295 | /* some prototypes */ |
| 1296 | static int maintain_proxies(void); |
| 1297 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1298 | /* this either returns the sockname or the original destination address. Code |
| 1299 | * inspired from Patrick Schaaf's example of nf_getsockname() implementation. |
| 1300 | */ |
| 1301 | static int get_original_dst(int fd, struct sockaddr_in *sa, int *salen) { |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1302 | #if defined(TPROXY) && defined(SO_ORIGINAL_DST) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1303 | return getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, (void *)sa, salen); |
| 1304 | #else |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1305 | #if defined(TPROXY) && defined(USE_GETSOCKNAME) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1306 | return getsockname(fd, (struct sockaddr *)sa, salen); |
| 1307 | #else |
| 1308 | return -1; |
| 1309 | #endif |
| 1310 | #endif |
| 1311 | } |
| 1312 | |
| 1313 | /* |
| 1314 | * frees the context associated to a session. It must have been removed first. |
| 1315 | */ |
| 1316 | static inline void session_free(struct session *s) { |
| 1317 | if (s->req) |
| 1318 | pool_free(buffer, s->req); |
| 1319 | if (s->rep) |
| 1320 | pool_free(buffer, s->rep); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1321 | if (s->logs.uri) |
| 1322 | pool_free(requri, s->logs.uri); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1323 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1324 | pool_free(session, s); |
| 1325 | } |
| 1326 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1327 | |
| 1328 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1329 | * This function initiates a connection to the current server (s->srv) if (s->direct) |
| 1330 | * is set, or to the dispatch server if (s->direct) is 0. It returns 0 if |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1331 | * it's OK, -1 if it's impossible. |
| 1332 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1333 | int connect_server(struct session *s) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1334 | int one = 1; |
| 1335 | int fd; |
| 1336 | |
| 1337 | // fprintf(stderr,"connect_server : s=%p\n",s); |
| 1338 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 1339 | if (s->flags & SN_DIRECT) { /* srv cannot be null */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1340 | s->srv_addr = s->srv->addr; |
| 1341 | } |
| 1342 | else if (s->proxy->options & PR_O_BALANCE) { |
| 1343 | if (s->proxy->options & PR_O_BALANCE_RR) { |
| 1344 | int retry = s->proxy->nbservers; |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1345 | while (retry) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1346 | if (s->proxy->cursrv == NULL) |
| 1347 | s->proxy->cursrv = s->proxy->srv; |
| 1348 | if (s->proxy->cursrv->state & SRV_RUNNING) |
| 1349 | break; |
| 1350 | s->proxy->cursrv = s->proxy->cursrv->next; |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1351 | retry--; |
| 1352 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1353 | |
| 1354 | if (retry == 0) /* no server left */ |
| 1355 | return -1; |
| 1356 | |
| 1357 | s->srv = s->proxy->cursrv; |
| 1358 | s->srv_addr = s->srv->addr; |
| 1359 | s->proxy->cursrv = s->proxy->cursrv->next; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1360 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1361 | else /* unknown balancing algorithm */ |
| 1362 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1363 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1364 | else if (*(int *)&s->proxy->dispatch_addr.sin_addr) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1365 | /* connect to the defined dispatch addr */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1366 | s->srv_addr = s->proxy->dispatch_addr; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1367 | } |
| 1368 | else if (s->proxy->options & PR_O_TRANSP) { |
| 1369 | /* in transparent mode, use the original dest addr if no dispatch specified */ |
| 1370 | int salen = sizeof(struct sockaddr_in); |
| 1371 | if (get_original_dst(s->cli_fd, &s->srv_addr, &salen) == -1) { |
| 1372 | qfprintf(stderr, "Cannot get original server address.\n"); |
| 1373 | return -1; |
| 1374 | } |
| 1375 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1376 | |
| 1377 | 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] | 1378 | qfprintf(stderr, "Cannot get a server socket.\n"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1379 | return -1; |
| 1380 | } |
| 1381 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1382 | if (fd >= global.maxsock) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1383 | Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 1384 | close(fd); |
| 1385 | return -1; |
| 1386 | } |
| 1387 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1388 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
| 1389 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1390 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1391 | close(fd); |
| 1392 | return -1; |
| 1393 | } |
| 1394 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1395 | /* allow specific binding */ |
| 1396 | if (s->proxy->options & PR_O_BIND_SRC && |
| 1397 | bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) { |
| 1398 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", s->proxy->id); |
| 1399 | close(fd); |
| 1400 | return -1; |
| 1401 | } |
| 1402 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1403 | if ((connect(fd, (struct sockaddr *)&s->srv_addr, sizeof(s->srv_addr)) == -1) && (errno != EINPROGRESS)) { |
| 1404 | if (errno == EAGAIN) { /* no free ports left, try again later */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1405 | qfprintf(stderr,"Cannot connect, no free ports.\n"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1406 | close(fd); |
| 1407 | return -1; |
| 1408 | } |
| 1409 | else if (errno != EALREADY && errno != EISCONN) { |
| 1410 | close(fd); |
| 1411 | return -1; |
| 1412 | } |
| 1413 | } |
| 1414 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1415 | fdtab[fd].owner = s->task; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1416 | fdtab[fd].read = &event_srv_read; |
| 1417 | fdtab[fd].write = &event_srv_write; |
| 1418 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 1419 | |
| 1420 | FD_SET(fd, StaticWriteEvent); /* for connect status */ |
| 1421 | |
| 1422 | fd_insert(fd); |
| 1423 | |
| 1424 | if (s->proxy->contimeout) |
| 1425 | tv_delayfrom(&s->cnexpire, &now, s->proxy->contimeout); |
| 1426 | else |
| 1427 | tv_eternity(&s->cnexpire); |
| 1428 | return 0; |
| 1429 | } |
| 1430 | |
| 1431 | /* |
| 1432 | * this function is called on a read event from a client socket. |
| 1433 | * It returns 0. |
| 1434 | */ |
| 1435 | int event_cli_read(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1436 | struct task *t = fdtab[fd].owner; |
| 1437 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1438 | struct buffer *b = s->req; |
| 1439 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1440 | |
| 1441 | // fprintf(stderr,"event_cli_read : fd=%d, s=%p\n", fd, s); |
| 1442 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1443 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1444 | while (1) { |
| 1445 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 1446 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1447 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1448 | } |
| 1449 | else if (b->r > b->w) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1450 | max = b->rlim - b->r; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1451 | } |
| 1452 | else { |
| 1453 | max = b->w - b->r; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1454 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 1455 | * since it means that the rewrite protection has been removed. This |
| 1456 | * implies that the if statement can be removed. |
| 1457 | */ |
| 1458 | if (max > b->rlim - b->data) |
| 1459 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | if (max == 0) { /* not anymore room to store data */ |
| 1463 | FD_CLR(fd, StaticReadEvent); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1464 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1465 | } |
| 1466 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1467 | #ifndef MSG_NOSIGNAL |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1468 | { |
| 1469 | int skerr, lskerr; |
| 1470 | |
| 1471 | lskerr = sizeof(skerr); |
| 1472 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 1473 | if (skerr) |
| 1474 | ret = -1; |
| 1475 | else |
| 1476 | ret = recv(fd, b->r, max, 0); |
| 1477 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1478 | #else |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1479 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1480 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1481 | if (ret > 0) { |
| 1482 | b->r += ret; |
| 1483 | b->l += ret; |
| 1484 | s->res_cr = RES_DATA; |
| 1485 | |
| 1486 | if (b->r == b->data + BUFSIZE) { |
| 1487 | b->r = b->data; /* wrap around the buffer */ |
| 1488 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1489 | |
| 1490 | b->total += ret; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1491 | /* we hope to read more data or to get a close on next round */ |
| 1492 | continue; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1493 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1494 | else if (ret == 0) { |
| 1495 | s->res_cr = RES_NULL; |
| 1496 | break; |
| 1497 | } |
| 1498 | else if (errno == EAGAIN) {/* ignore EAGAIN */ |
| 1499 | break; |
| 1500 | } |
| 1501 | else { |
| 1502 | s->res_cr = RES_ERROR; |
| 1503 | fdtab[fd].state = FD_STERROR; |
| 1504 | break; |
| 1505 | } |
| 1506 | } /* while(1) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1507 | } |
| 1508 | else { |
| 1509 | s->res_cr = RES_ERROR; |
| 1510 | fdtab[fd].state = FD_STERROR; |
| 1511 | } |
| 1512 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1513 | if (s->res_cr != RES_SILENT) { |
| 1514 | if (s->proxy->clitimeout) |
| 1515 | tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout); |
| 1516 | else |
| 1517 | tv_eternity(&s->crexpire); |
| 1518 | |
| 1519 | task_wakeup(&rq, t); |
| 1520 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1521 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1522 | return 0; |
| 1523 | } |
| 1524 | |
| 1525 | |
| 1526 | /* |
| 1527 | * this function is called on a read event from a server socket. |
| 1528 | * It returns 0. |
| 1529 | */ |
| 1530 | int event_srv_read(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1531 | struct task *t = fdtab[fd].owner; |
| 1532 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1533 | struct buffer *b = s->rep; |
| 1534 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1535 | |
| 1536 | // fprintf(stderr,"event_srv_read : fd=%d, s=%p\n", fd, s); |
| 1537 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1538 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1539 | while (1) { |
| 1540 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 1541 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1542 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1543 | } |
| 1544 | else if (b->r > b->w) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1545 | max = b->rlim - b->r; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1546 | } |
| 1547 | else { |
| 1548 | max = b->w - b->r; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1549 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 1550 | * since it means that the rewrite protection has been removed. This |
| 1551 | * implies that the if statement can be removed. |
| 1552 | */ |
| 1553 | if (max > b->rlim - b->data) |
| 1554 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | if (max == 0) { /* not anymore room to store data */ |
| 1558 | FD_CLR(fd, StaticReadEvent); |
| 1559 | break; |
| 1560 | } |
| 1561 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1562 | #ifndef MSG_NOSIGNAL |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1563 | { |
| 1564 | int skerr, lskerr; |
| 1565 | |
| 1566 | lskerr = sizeof(skerr); |
| 1567 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 1568 | if (skerr) |
| 1569 | ret = -1; |
| 1570 | else |
| 1571 | ret = recv(fd, b->r, max, 0); |
| 1572 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1573 | #else |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1574 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1575 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1576 | if (ret > 0) { |
| 1577 | b->r += ret; |
| 1578 | b->l += ret; |
| 1579 | s->res_sr = RES_DATA; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1580 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1581 | if (b->r == b->data + BUFSIZE) { |
| 1582 | b->r = b->data; /* wrap around the buffer */ |
| 1583 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1584 | |
| 1585 | b->total += ret; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1586 | /* we hope to read more data or to get a close on next round */ |
| 1587 | continue; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1588 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1589 | else if (ret == 0) { |
| 1590 | s->res_sr = RES_NULL; |
| 1591 | break; |
| 1592 | } |
| 1593 | else if (errno == EAGAIN) {/* ignore EAGAIN */ |
| 1594 | break; |
| 1595 | } |
| 1596 | else { |
| 1597 | s->res_sr = RES_ERROR; |
| 1598 | fdtab[fd].state = FD_STERROR; |
| 1599 | break; |
| 1600 | } |
| 1601 | } /* while(1) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1602 | } |
| 1603 | else { |
| 1604 | s->res_sr = RES_ERROR; |
| 1605 | fdtab[fd].state = FD_STERROR; |
| 1606 | } |
| 1607 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1608 | if (s->res_sr != RES_SILENT) { |
| 1609 | if (s->proxy->srvtimeout) |
| 1610 | tv_delayfrom(&s->srexpire, &now, s->proxy->srvtimeout); |
| 1611 | else |
| 1612 | tv_eternity(&s->srexpire); |
| 1613 | |
| 1614 | task_wakeup(&rq, t); |
| 1615 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1616 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1617 | return 0; |
| 1618 | } |
| 1619 | |
| 1620 | /* |
| 1621 | * this function is called on a write event from a client socket. |
| 1622 | * It returns 0. |
| 1623 | */ |
| 1624 | int event_cli_write(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1625 | struct task *t = fdtab[fd].owner; |
| 1626 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1627 | struct buffer *b = s->rep; |
| 1628 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1629 | |
| 1630 | // fprintf(stderr,"event_cli_write : fd=%d, s=%p\n", fd, s); |
| 1631 | |
| 1632 | 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] | 1633 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1634 | // max = BUFSIZE; BUG !!!! |
| 1635 | max = 0; |
| 1636 | } |
| 1637 | else if (b->r > b->w) { |
| 1638 | max = b->r - b->w; |
| 1639 | } |
| 1640 | else |
| 1641 | max = b->data + BUFSIZE - b->w; |
| 1642 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1643 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1644 | #ifndef MSG_NOSIGNAL |
| 1645 | int skerr, lskerr; |
| 1646 | #endif |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1647 | |
| 1648 | if (max == 0) { |
| 1649 | s->res_cw = RES_NULL; |
| 1650 | task_wakeup(&rq, t); |
| 1651 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1652 | } |
| 1653 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1654 | #ifndef MSG_NOSIGNAL |
| 1655 | lskerr=sizeof(skerr); |
| 1656 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 1657 | if (skerr) |
| 1658 | ret = -1; |
| 1659 | else |
| 1660 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
| 1661 | #else |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1662 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1663 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1664 | |
| 1665 | if (ret > 0) { |
| 1666 | b->l -= ret; |
| 1667 | b->w += ret; |
| 1668 | |
| 1669 | s->res_cw = RES_DATA; |
| 1670 | |
| 1671 | if (b->w == b->data + BUFSIZE) { |
| 1672 | b->w = b->data; /* wrap around the buffer */ |
| 1673 | } |
| 1674 | } |
| 1675 | else if (ret == 0) { |
| 1676 | /* nothing written, just make as if we were never called */ |
| 1677 | // s->res_cw = RES_NULL; |
| 1678 | return 0; |
| 1679 | } |
| 1680 | else if (errno == EAGAIN) /* ignore EAGAIN */ |
| 1681 | return 0; |
| 1682 | else { |
| 1683 | s->res_cw = RES_ERROR; |
| 1684 | fdtab[fd].state = FD_STERROR; |
| 1685 | } |
| 1686 | } |
| 1687 | else { |
| 1688 | s->res_cw = RES_ERROR; |
| 1689 | fdtab[fd].state = FD_STERROR; |
| 1690 | } |
| 1691 | |
| 1692 | if (s->proxy->clitimeout) |
| 1693 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
| 1694 | else |
| 1695 | tv_eternity(&s->cwexpire); |
| 1696 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1697 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1698 | return 0; |
| 1699 | } |
| 1700 | |
| 1701 | |
| 1702 | /* |
| 1703 | * this function is called on a write event from a server socket. |
| 1704 | * It returns 0. |
| 1705 | */ |
| 1706 | int event_srv_write(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1707 | struct task *t = fdtab[fd].owner; |
| 1708 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1709 | struct buffer *b = s->req; |
| 1710 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1711 | |
| 1712 | //fprintf(stderr,"event_srv_write : fd=%d, s=%p\n", fd, s); |
| 1713 | |
| 1714 | 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] | 1715 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1716 | // max = BUFSIZE; BUG !!!! |
| 1717 | max = 0; |
| 1718 | } |
| 1719 | else if (b->r > b->w) { |
| 1720 | max = b->r - b->w; |
| 1721 | } |
| 1722 | else |
| 1723 | max = b->data + BUFSIZE - b->w; |
| 1724 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1725 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1726 | #ifndef MSG_NOSIGNAL |
| 1727 | int skerr, lskerr; |
| 1728 | #endif |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1729 | if (max == 0) { |
| 1730 | /* may be we have received a connection acknowledgement in TCP mode without data */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1731 | s->res_sw = RES_NULL; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1732 | task_wakeup(&rq, t); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1733 | fdtab[fd].state = FD_STREADY; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1734 | return 0; |
| 1735 | } |
| 1736 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1737 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1738 | #ifndef MSG_NOSIGNAL |
| 1739 | lskerr=sizeof(skerr); |
| 1740 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 1741 | if (skerr) |
| 1742 | ret = -1; |
| 1743 | else |
| 1744 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
| 1745 | #else |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1746 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1747 | #endif |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1748 | fdtab[fd].state = FD_STREADY; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1749 | if (ret > 0) { |
| 1750 | b->l -= ret; |
| 1751 | b->w += ret; |
| 1752 | |
| 1753 | s->res_sw = RES_DATA; |
| 1754 | |
| 1755 | if (b->w == b->data + BUFSIZE) { |
| 1756 | b->w = b->data; /* wrap around the buffer */ |
| 1757 | } |
| 1758 | } |
| 1759 | else if (ret == 0) { |
| 1760 | /* nothing written, just make as if we were never called */ |
| 1761 | // s->res_sw = RES_NULL; |
| 1762 | return 0; |
| 1763 | } |
| 1764 | else if (errno == EAGAIN) /* ignore EAGAIN */ |
| 1765 | return 0; |
| 1766 | else { |
| 1767 | s->res_sw = RES_ERROR; |
| 1768 | fdtab[fd].state = FD_STERROR; |
| 1769 | } |
| 1770 | } |
| 1771 | else { |
| 1772 | s->res_sw = RES_ERROR; |
| 1773 | fdtab[fd].state = FD_STERROR; |
| 1774 | } |
| 1775 | |
| 1776 | if (s->proxy->srvtimeout) |
| 1777 | tv_delayfrom(&s->swexpire, &now, s->proxy->srvtimeout); |
| 1778 | else |
| 1779 | tv_eternity(&s->swexpire); |
| 1780 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1781 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1782 | return 0; |
| 1783 | } |
| 1784 | |
| 1785 | |
| 1786 | /* |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 1787 | * returns a message to the client ; the connection is shut down for read, |
| 1788 | * and the request is cleared so that no server connection can be initiated. |
| 1789 | * The client must be in a valid state for this (HEADER, DATA ...). |
| 1790 | * Nothing is performed on the server side. |
| 1791 | * The reply buffer must be empty before this. |
| 1792 | */ |
| 1793 | void client_retnclose(struct session *s, int len, const char *msg) { |
| 1794 | FD_CLR(s->cli_fd, StaticReadEvent); |
| 1795 | FD_SET(s->cli_fd, StaticWriteEvent); |
| 1796 | tv_eternity(&s->crexpire); |
| 1797 | shutdown(s->cli_fd, SHUT_RD); |
| 1798 | s->cli_state = CL_STSHUTR; |
| 1799 | strcpy(s->rep->data, msg); |
| 1800 | s->rep->l = len; |
| 1801 | s->rep->r += len; |
| 1802 | s->req->l = 0; |
| 1803 | } |
| 1804 | |
| 1805 | |
| 1806 | /* |
| 1807 | * returns a message into the rep buffer, and flushes the req buffer. |
| 1808 | * The reply buffer must be empty before this. |
| 1809 | */ |
| 1810 | void client_return(struct session *s, int len, const char *msg) { |
| 1811 | strcpy(s->rep->data, msg); |
| 1812 | s->rep->l = len; |
| 1813 | s->rep->r += len; |
| 1814 | s->req->l = 0; |
| 1815 | } |
| 1816 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1817 | /* |
| 1818 | * send a log for the session when we have enough info about it |
| 1819 | */ |
| 1820 | void sess_log(struct session *s) { |
| 1821 | unsigned char *pn; |
| 1822 | struct proxy *p = s->proxy; |
| 1823 | int log; |
| 1824 | char *uri; |
| 1825 | char *pxid; |
| 1826 | char *srv; |
| 1827 | |
| 1828 | /* This is a first attempt at a better logging system. |
| 1829 | * For now, we rely on send_log() to provide the date, although it obviously |
| 1830 | * is the date of the log and not of the request, and most fields are not |
| 1831 | * computed. |
| 1832 | */ |
| 1833 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1834 | log = p->to_log & ~s->logs.logwait; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1835 | |
| 1836 | pn = (log & LW_CLIP) ? |
| 1837 | (unsigned char *)&s->cli_addr.sin_addr : |
| 1838 | (unsigned char *)"\0\0\0\0"; |
| 1839 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1840 | uri = (log & LW_REQ) ? s->logs.uri : "<BADREQ>"; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1841 | pxid = p->id; |
| 1842 | //srv = (log & LW_SVID) ? s->srv->id : "<svid>"; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1843 | srv = ((p->to_log & LW_SVID) && s->srv != NULL) ? s->srv->id : "<NOSRV>"; |
| 1844 | |
| 1845 | if (p->to_log & LW_DATE) { |
| 1846 | struct tm *tm = localtime(&s->logs.tv_accept.tv_sec); |
| 1847 | |
| 1848 | send_log(p, LOG_INFO, "%d.%d.%d.%d:%d [%02d/%s/%04d:%02d:%02d:%02d] %s %s %d/%d/%d/%d %d %lld \"%s\"\n", |
| 1849 | pn[0], pn[1], pn[2], pn[3], ntohs(s->cli_addr.sin_port), |
| 1850 | tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900, |
| 1851 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 1852 | pxid, srv, |
| 1853 | s->logs.t_request, |
| 1854 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_request : -1, |
| 1855 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1856 | s->logs.t_close, |
| 1857 | s->logs.status, s->logs.bytes, |
| 1858 | uri); |
| 1859 | } |
| 1860 | else { |
| 1861 | send_log(p, LOG_INFO, "%d.%d.%d.%d:%d %s %s %d/%d/%d/%d %d %lld \"%s\"\n", |
| 1862 | pn[0], pn[1], pn[2], pn[3], ntohs(s->cli_addr.sin_port), |
| 1863 | pxid, srv, |
| 1864 | s->logs.t_request, |
| 1865 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_request : -1, |
| 1866 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1867 | s->logs.t_close, |
| 1868 | s->logs.status, s->logs.bytes, |
| 1869 | uri); |
| 1870 | } |
| 1871 | |
| 1872 | s->logs.logwait = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1873 | } |
| 1874 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 1875 | |
| 1876 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1877 | * 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] | 1878 | * to an accept. It tries to accept as many connections as possible. |
| 1879 | * It returns 0. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1880 | */ |
| 1881 | int event_accept(int fd) { |
| 1882 | struct proxy *p = (struct proxy *)fdtab[fd].owner; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1883 | struct session *s; |
| 1884 | struct task *t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1885 | int cfd; |
| 1886 | int one = 1; |
| 1887 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1888 | while (p->nbconn < p->maxconn) { |
| 1889 | struct sockaddr_in addr; |
| 1890 | int laddr = sizeof(addr); |
| 1891 | if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) |
| 1892 | return 0; /* nothing more to accept */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1893 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1894 | if ((s = pool_alloc(session)) == NULL) { /* disable this proxy for a while */ |
| 1895 | Alert("out of memory in event_accept().\n"); |
| 1896 | FD_CLR(fd, StaticReadEvent); |
| 1897 | p->state = PR_STIDLE; |
| 1898 | close(cfd); |
| 1899 | return 0; |
| 1900 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1901 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1902 | if ((t = pool_alloc(task)) == NULL) { /* disable this proxy for a while */ |
| 1903 | Alert("out of memory in event_accept().\n"); |
| 1904 | FD_CLR(fd, StaticReadEvent); |
| 1905 | p->state = PR_STIDLE; |
| 1906 | close(cfd); |
| 1907 | pool_free(session, s); |
| 1908 | return 0; |
| 1909 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1910 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1911 | s->cli_addr = addr; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1912 | if (cfd >= global.maxsock) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1913 | Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 1914 | close(cfd); |
| 1915 | pool_free(task, t); |
| 1916 | pool_free(session, s); |
| 1917 | return 0; |
| 1918 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1919 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1920 | if ((fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) || |
| 1921 | (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, |
| 1922 | (char *) &one, sizeof(one)) == -1)) { |
| 1923 | Alert("accept(): cannot set the socket in non blocking mode. Giving up\n"); |
| 1924 | close(cfd); |
| 1925 | pool_free(task, t); |
| 1926 | pool_free(session, s); |
| 1927 | return 0; |
| 1928 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1929 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1930 | t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */ |
| 1931 | t->wq = LIST_HEAD(wait_queue); /* but already has a wait queue assigned */ |
| 1932 | t->state = TASK_IDLE; |
| 1933 | t->process = process_session; |
| 1934 | t->context = s; |
| 1935 | |
| 1936 | s->task = t; |
| 1937 | s->proxy = p; |
| 1938 | s->cli_state = (p->mode == PR_MODE_HTTP) ? CL_STHEADERS : CL_STDATA; /* no HTTP headers for non-HTTP proxies */ |
| 1939 | s->srv_state = SV_STIDLE; |
| 1940 | s->req = s->rep = NULL; /* will be allocated later */ |
| 1941 | s->flags = 0; |
| 1942 | s->res_cr = s->res_cw = s->res_sr = s->res_sw = RES_SILENT; |
| 1943 | s->cli_fd = cfd; |
| 1944 | s->srv_fd = -1; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1945 | s->srv = NULL; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1946 | s->conn_retries = p->conn_retries; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1947 | |
| 1948 | s->logs.logwait = p->to_log; |
| 1949 | s->logs.tv_accept = now; |
| 1950 | s->logs.t_request = -1; |
| 1951 | s->logs.t_connect = -1; |
| 1952 | s->logs.t_data = -1; |
| 1953 | s->logs.t_close = 0; |
| 1954 | s->logs.uri = NULL; |
| 1955 | s->logs.status = -1; |
| 1956 | s->logs.bytes = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1957 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1958 | if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP) |
| 1959 | && (p->logfac1 >= 0 || p->logfac2 >= 0)) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 1960 | struct sockaddr_in sockname; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1961 | unsigned char *pn, *sn; |
| 1962 | int namelen; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1963 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1964 | namelen = sizeof(sockname); |
| 1965 | if (get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
| 1966 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
| 1967 | sn = (unsigned char *)&sockname.sin_addr; |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 1968 | pn = (unsigned char *)&s->cli_addr.sin_addr; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1969 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1970 | if (p->to_log) { |
| 1971 | /* we have the client ip */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1972 | if (s->logs.logwait & LW_CLIP) |
| 1973 | if (!(s->logs.logwait &= ~LW_CLIP)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1974 | sess_log(s); |
| 1975 | } |
| 1976 | else |
| 1977 | send_log(p, LOG_INFO, "Connect from %d.%d.%d.%d:%d to %d.%d.%d.%d:%d (%s/%s)\n", |
| 1978 | pn[0], pn[1], pn[2], pn[3], ntohs(s->cli_addr.sin_port), |
| 1979 | sn[0], sn[1], sn[2], sn[3], ntohs(sockname.sin_port), |
| 1980 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1981 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1982 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1983 | if ((global.mode & MODE_DEBUG) && !(global.mode & MODE_QUIET)) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1984 | int len; |
| 1985 | len = sprintf(trash, "accept(%04x)=%04x\n", (unsigned short)fd, (unsigned short)cfd); |
| 1986 | write(1, trash, len); |
| 1987 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1988 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1989 | if ((s->req = pool_alloc(buffer)) == NULL) { /* no memory */ |
| 1990 | close(cfd); /* nothing can be done for this fd without memory */ |
| 1991 | pool_free(task, t); |
| 1992 | pool_free(session, s); |
| 1993 | return 0; |
| 1994 | } |
| 1995 | s->req->l = 0; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1996 | s->req->total = 0; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1997 | s->req->h = s->req->r = s->req->lr = s->req->w = s->req->data; /* r and w will be reset further */ |
| 1998 | s->req->rlim = s->req->data + BUFSIZE; |
| 1999 | if (s->cli_state == CL_STHEADERS) /* reserver some space for header rewriting */ |
| 2000 | s->req->rlim -= MAXREWRITE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2001 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2002 | if ((s->rep = pool_alloc(buffer)) == NULL) { /* no memory */ |
| 2003 | pool_free(buffer, s->req); |
| 2004 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2005 | pool_free(task, t); |
| 2006 | pool_free(session, s); |
| 2007 | return 0; |
| 2008 | } |
| 2009 | s->rep->l = 0; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2010 | s->rep->total = 0; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2011 | 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] | 2012 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2013 | fdtab[cfd].read = &event_cli_read; |
| 2014 | fdtab[cfd].write = &event_cli_write; |
| 2015 | fdtab[cfd].owner = t; |
| 2016 | fdtab[cfd].state = FD_STREADY; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2017 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2018 | if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2019 | client_retnclose(s, 3, "OK\n"); /* forge an "OK" response */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2020 | } |
| 2021 | else { |
| 2022 | FD_SET(cfd, StaticReadEvent); |
| 2023 | } |
| 2024 | |
| 2025 | fd_insert(cfd); |
| 2026 | |
| 2027 | tv_eternity(&s->cnexpire); |
| 2028 | tv_eternity(&s->srexpire); |
| 2029 | tv_eternity(&s->swexpire); |
| 2030 | tv_eternity(&s->cwexpire); |
| 2031 | |
| 2032 | if (s->proxy->clitimeout) |
| 2033 | tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout); |
| 2034 | else |
| 2035 | tv_eternity(&s->crexpire); |
| 2036 | |
| 2037 | t->expire = s->crexpire; |
| 2038 | |
| 2039 | task_queue(t); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2040 | |
| 2041 | if (p->mode != PR_MODE_HEALTH) |
| 2042 | task_wakeup(&rq, t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2043 | |
| 2044 | p->nbconn++; |
| 2045 | actconn++; |
| 2046 | totalconn++; |
| 2047 | |
| 2048 | // fprintf(stderr, "accepting from %p => %d conn, %d total\n", p, actconn, totalconn); |
| 2049 | } /* end of while (p->nbconn < p->maxconn) */ |
| 2050 | return 0; |
| 2051 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2052 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2053 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2054 | /* |
| 2055 | * This function is used only for server health-checks. It handles |
| 2056 | * the connection acknowledgement and returns 1 if the socket is OK, |
| 2057 | * or -1 if an error occured. |
| 2058 | */ |
| 2059 | int event_srv_hck(int fd) { |
| 2060 | struct task *t = fdtab[fd].owner; |
| 2061 | struct server *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2062 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2063 | int skerr, lskerr; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2064 | lskerr = sizeof(skerr); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2065 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2066 | if (skerr) |
| 2067 | s->result = -1; |
| 2068 | else |
| 2069 | s->result = 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2070 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2071 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2072 | return 0; |
| 2073 | } |
| 2074 | |
| 2075 | |
| 2076 | /* |
| 2077 | * this function writes the string <str> at position <pos> which must be in buffer <b>, |
| 2078 | * and moves <end> just after the end of <str>. |
| 2079 | * <b>'s parameters (l, r, w, h, lr) are recomputed to be valid after the shift. |
| 2080 | * the shift value (positive or negative) is returned. |
| 2081 | * If there's no space left, the move is not done. |
| 2082 | * |
| 2083 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2084 | int buffer_replace(struct buffer *b, char *pos, char *end, char *str) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2085 | int delta; |
| 2086 | int len; |
| 2087 | |
| 2088 | len = strlen(str); |
| 2089 | delta = len - (end - pos); |
| 2090 | |
| 2091 | if (delta + b->r >= b->data + BUFSIZE) |
| 2092 | return 0; /* no space left */ |
| 2093 | |
| 2094 | /* first, protect the end of the buffer */ |
| 2095 | memmove(end + delta, end, b->data + b->l - end); |
| 2096 | |
| 2097 | /* now, copy str over pos */ |
| 2098 | memcpy(pos, str,len); |
| 2099 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2100 | /* we only move data after the displaced zone */ |
| 2101 | if (b->r > pos) b->r += delta; |
| 2102 | if (b->w > pos) b->w += delta; |
| 2103 | if (b->h > pos) b->h += delta; |
| 2104 | if (b->lr > pos) b->lr += delta; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2105 | b->l += delta; |
| 2106 | |
| 2107 | return delta; |
| 2108 | } |
| 2109 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2110 | /* same except that the string len is given, which allows str to be NULL if |
| 2111 | * len is 0. |
| 2112 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2113 | 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] | 2114 | int delta; |
| 2115 | |
| 2116 | delta = len - (end - pos); |
| 2117 | |
| 2118 | if (delta + b->r >= b->data + BUFSIZE) |
| 2119 | return 0; /* no space left */ |
| 2120 | |
| 2121 | /* first, protect the end of the buffer */ |
| 2122 | memmove(end + delta, end, b->data + b->l - end); |
| 2123 | |
| 2124 | /* now, copy str over pos */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2125 | if (len) |
| 2126 | memcpy(pos, str, len); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2127 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2128 | /* we only move data after the displaced zone */ |
| 2129 | if (b->r > pos) b->r += delta; |
| 2130 | if (b->w > pos) b->w += delta; |
| 2131 | if (b->h > pos) b->h += delta; |
| 2132 | if (b->lr > pos) b->lr += delta; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2133 | b->l += delta; |
| 2134 | |
| 2135 | return delta; |
| 2136 | } |
| 2137 | |
| 2138 | |
| 2139 | int exp_replace(char *dst, char *src, char *str, regmatch_t *matches) { |
| 2140 | char *old_dst = dst; |
| 2141 | |
| 2142 | while (*str) { |
| 2143 | if (*str == '\\') { |
| 2144 | str++; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 2145 | if (isdigit((int)*str)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2146 | int len, num; |
| 2147 | |
| 2148 | num = *str - '0'; |
| 2149 | str++; |
| 2150 | |
| 2151 | if (matches[num].rm_so > -1) { |
| 2152 | len = matches[num].rm_eo - matches[num].rm_so; |
| 2153 | memcpy(dst, src + matches[num].rm_so, len); |
| 2154 | dst += len; |
| 2155 | } |
| 2156 | |
| 2157 | } |
| 2158 | else if (*str == 'x') { |
| 2159 | unsigned char hex1, hex2; |
| 2160 | str++; |
| 2161 | |
| 2162 | hex1=toupper(*str++) - '0'; hex2=toupper(*str++) - '0'; |
| 2163 | |
| 2164 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 2165 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 2166 | *dst++ = (hex1<<4) + hex2; |
| 2167 | } |
| 2168 | else |
| 2169 | *dst++ = *str++; |
| 2170 | } |
| 2171 | else |
| 2172 | *dst++ = *str++; |
| 2173 | } |
| 2174 | *dst = 0; |
| 2175 | return dst - old_dst; |
| 2176 | } |
| 2177 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2178 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2179 | /* |
| 2180 | * manages the client FSM and its socket. BTW, it also tries to handle the |
| 2181 | * cookie. It returns 1 if a state has changed (and a resync may be needed), |
| 2182 | * 0 else. |
| 2183 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2184 | int process_cli(struct session *t) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2185 | int s = t->srv_state; |
| 2186 | int c = t->cli_state; |
| 2187 | struct buffer *req = t->req; |
| 2188 | struct buffer *rep = t->rep; |
| 2189 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2190 | #ifdef DEBUG_FULL |
| 2191 | fprintf(stderr,"process_cli: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 2192 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2193 | //fprintf(stderr,"process_cli: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s, |
| 2194 | //FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 2195 | //FD_ISSET(t->srv_fd, StaticReadEvent), FD_ISSET(t->srv_fd, StaticWriteEvent) |
| 2196 | //); |
| 2197 | if (c == CL_STHEADERS) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2198 | /* now parse the partial (or complete) headers */ |
| 2199 | while (req->lr < req->r) { /* this loop only sees one header at each iteration */ |
| 2200 | char *ptr; |
| 2201 | int delete_header; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2202 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2203 | ptr = req->lr; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2204 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2205 | /* look for the end of the current header */ |
| 2206 | while (ptr < req->r && *ptr != '\n' && *ptr != '\r') |
| 2207 | ptr++; |
| 2208 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2209 | if (ptr == req->h) { /* empty line, end of headers */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2210 | int line, len; |
| 2211 | /* we can only get here after an end of headers */ |
| 2212 | /* we'll have something else to do here : add new headers ... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2213 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2214 | if (t->flags & SN_CLDENY) { |
| 2215 | /* no need to go further */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2216 | t->logs.status = 403; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2217 | if (t->proxy->mode == PR_MODE_HTTP) |
| 2218 | client_retnclose(t, strlen(HTTP_403), HTTP_403); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2219 | return 1; |
| 2220 | } |
| 2221 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2222 | for (line = 0; line < t->proxy->nb_reqadd; line++) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2223 | len = sprintf(trash, "%s\r\n", t->proxy->req_add[line]); |
| 2224 | buffer_replace2(req, req->h, req->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2225 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2226 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2227 | if (t->proxy->options & PR_O_FWDFOR) { |
| 2228 | /* insert an X-Forwarded-For header */ |
| 2229 | unsigned char *pn; |
| 2230 | pn = (unsigned char *)&t->cli_addr.sin_addr; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2231 | len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d\r\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2232 | pn[0], pn[1], pn[2], pn[3]); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2233 | buffer_replace2(req, req->h, req->h, trash, len); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2234 | } |
| 2235 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2236 | t->cli_state = CL_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2237 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2238 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2239 | t->logs.t_request = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2240 | /* FIXME: we'll set the client in a wait state while we try to |
| 2241 | * connect to the server. Is this really needed ? wouldn't it be |
| 2242 | * better to release the maximum of system buffers instead ? */ |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2243 | //FD_CLR(t->cli_fd, StaticReadEvent); |
| 2244 | //tv_eternity(&t->crexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2245 | break; |
| 2246 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2247 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2248 | /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */ |
| 2249 | if (ptr > req->r - 2) { |
| 2250 | /* this is a partial header, let's wait for more to come */ |
| 2251 | req->lr = ptr; |
| 2252 | break; |
| 2253 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2254 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2255 | /* now we know that *ptr is either \r or \n, |
| 2256 | * and that there are at least 1 char after it. |
| 2257 | */ |
| 2258 | if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n')) |
| 2259 | req->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */ |
| 2260 | else |
| 2261 | req->lr = ptr + 2; /* \r\n or \n\r */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2262 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2263 | /* |
| 2264 | * now we know that we have a full header ; we can do whatever |
| 2265 | * we want with these pointers : |
| 2266 | * req->h = beginning of header |
| 2267 | * ptr = end of header (first \r or \n) |
| 2268 | * req->lr = beginning of next line (next rep->h) |
| 2269 | * req->r = end of data (not used at this stage) |
| 2270 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2271 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2272 | if (t->logs.logwait & LW_REQ && |
| 2273 | t->proxy->mode & PR_MODE_HTTP) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2274 | /* we have a complete HTTP request that we must log */ |
| 2275 | int urilen; |
| 2276 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2277 | if ((t->logs.uri = pool_alloc(requri)) == NULL) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2278 | Alert("HTTP logging : out of memory.\n"); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2279 | t->logs.status = 500; |
| 2280 | if (t->proxy->mode == PR_MODE_HTTP) |
| 2281 | client_retnclose(t, strlen(HTTP_500), HTTP_500); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2282 | return 1; |
| 2283 | } |
| 2284 | |
| 2285 | urilen = ptr - req->h; |
| 2286 | if (urilen >= REQURI_LEN) |
| 2287 | urilen = REQURI_LEN - 1; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2288 | memcpy(t->logs.uri, req->h, urilen); |
| 2289 | t->logs.uri[urilen] = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2290 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2291 | if (!(t->logs.logwait &= ~LW_REQ)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2292 | sess_log(t); |
| 2293 | } |
| 2294 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2295 | delete_header = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2296 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2297 | if ((global.mode & MODE_DEBUG) && !(global.mode & MODE_QUIET)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2298 | int len, max; |
| 2299 | len = sprintf(trash, "clihdr[%04x:%04x]: ", (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
| 2300 | max = ptr - req->h; |
| 2301 | UBOUND(max, sizeof(trash) - len - 1); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2302 | len += strlcpy2(trash + len, req->h, max + 1); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2303 | trash[len++] = '\n'; |
| 2304 | write(1, trash, len); |
| 2305 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2306 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2307 | /* try headers regexps */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2308 | if (t->proxy->req_exp != NULL && !(t->flags & SN_CLDENY)) { |
| 2309 | struct hdr_exp *exp; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2310 | char term; |
| 2311 | |
| 2312 | term = *ptr; |
| 2313 | *ptr = '\0'; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2314 | exp = t->proxy->req_exp; |
| 2315 | do { |
| 2316 | if (regexec(exp->preg, req->h, MAX_MATCH, pmatch, 0) == 0) { |
| 2317 | switch (exp->action) { |
| 2318 | case ACT_ALLOW: |
| 2319 | if (!(t->flags & SN_CLDENY)) |
| 2320 | t->flags |= SN_CLALLOW; |
| 2321 | break; |
| 2322 | case ACT_REPLACE: |
| 2323 | if (!(t->flags & SN_CLDENY)) { |
| 2324 | int len = exp_replace(trash, req->h, exp->replace, pmatch); |
| 2325 | ptr += buffer_replace2(req, req->h, ptr, trash, len); |
| 2326 | } |
| 2327 | break; |
| 2328 | case ACT_REMOVE: |
| 2329 | if (!(t->flags & SN_CLDENY)) |
| 2330 | delete_header = 1; |
| 2331 | break; |
| 2332 | case ACT_DENY: |
| 2333 | if (!(t->flags & SN_CLALLOW)) |
| 2334 | t->flags |= SN_CLDENY; |
| 2335 | break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2336 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2337 | break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2338 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2339 | } while ((exp = exp->next) != NULL); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2340 | *ptr = term; /* restore the string terminator */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2341 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2342 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2343 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 2344 | * attributes whose name begin with a '$', and associate them with |
| 2345 | * the right cookie, if we want to delete this cookie. |
| 2346 | * So there are 3 cases for each cookie read : |
| 2347 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 2348 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 2349 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 2350 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 2351 | * "special" cookie. |
| 2352 | * At the end of loop, if a "special" cookie remains, we may have to |
| 2353 | * remove it. If no application cookie persists in the header, we |
| 2354 | * *MUST* delete it |
| 2355 | */ |
| 2356 | if (!delete_header && (t->proxy->cookie_name != NULL) |
| 2357 | && !(t->flags & SN_CLDENY) && (ptr >= req->h + 8) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2358 | && (strncmp(req->h, "Cookie: ", 8) == 0)) { |
| 2359 | char *p1, *p2, *p3, *p4; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2360 | char *del_colon, *del_cookie, *colon; |
| 2361 | int app_cookies; |
| 2362 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2363 | p1 = req->h + 8; /* first char after 'Cookie: ' */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2364 | colon = p1; |
| 2365 | /* del_cookie == NULL => nothing to be deleted */ |
| 2366 | del_colon = del_cookie = NULL; |
| 2367 | app_cookies = 0; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2368 | |
| 2369 | while (p1 < ptr) { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2370 | /* skip spaces and colons, but keep an eye on these ones */ |
| 2371 | while (p1 < ptr) { |
| 2372 | if (*p1 == ';' || *p1 == ',') |
| 2373 | colon = p1; |
| 2374 | else if (!isspace((int)*p1)) |
| 2375 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2376 | p1++; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2377 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2378 | |
| 2379 | if (p1 == ptr) |
| 2380 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2381 | |
| 2382 | /* p1 is at the beginning of the cookie name */ |
| 2383 | p2 = p1; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2384 | while (p2 < ptr && *p2 != '=') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2385 | p2++; |
| 2386 | |
| 2387 | if (p2 == ptr) |
| 2388 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2389 | |
| 2390 | p3 = p2 + 1; /* skips the '=' sign */ |
| 2391 | if (p3 == ptr) |
| 2392 | break; |
| 2393 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2394 | p4 = p3; |
| 2395 | while (p4 < ptr && !isspace((int)*p4) && *p4 != ';' && *p4 != ',') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2396 | p4++; |
| 2397 | |
| 2398 | /* here, we have the cookie name between p1 and p2, |
| 2399 | * and its value between p3 and p4. |
| 2400 | * we can process it. |
| 2401 | */ |
| 2402 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2403 | if (*p1 == '$') { |
| 2404 | /* skip this one */ |
| 2405 | } |
| 2406 | else if ((p2 - p1 == strlen(t->proxy->cookie_name)) && |
| 2407 | (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2408 | /* Cool... it's the right one */ |
| 2409 | struct server *srv = t->proxy->srv; |
| 2410 | |
| 2411 | while (srv && |
| 2412 | ((srv->cklen != p4 - p3) || memcmp(p3, srv->cookie, p4 - p3))) { |
| 2413 | srv = srv->next; |
| 2414 | } |
| 2415 | |
| 2416 | if (srv) { /* we found the server */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2417 | t->flags |= SN_DIRECT; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2418 | t->srv = srv; |
| 2419 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2420 | /* if this cookie was set in insert+indirect mode, then it's better that the |
| 2421 | * server never sees it. |
| 2422 | */ |
| 2423 | if (del_cookie == NULL && |
| 2424 | (t->proxy->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
| 2425 | del_cookie = p1; |
| 2426 | del_colon = colon; |
| 2427 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2428 | } |
| 2429 | else { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2430 | /* now we know that we must keep this cookie since it's |
| 2431 | * not ours. But if we wanted to delete our cookie |
| 2432 | * earlier, we cannot remove the complete header, but we |
| 2433 | * can remove the previous block itself. |
| 2434 | */ |
| 2435 | app_cookies++; |
| 2436 | |
| 2437 | if (del_cookie != NULL) { |
| 2438 | buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 2439 | p4 -= (p1 - del_cookie); |
| 2440 | ptr -= (p1 - del_cookie); |
| 2441 | del_cookie = del_colon = NULL; |
| 2442 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2443 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2444 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2445 | /* we'll have to look for another cookie ... */ |
| 2446 | p1 = p4; |
| 2447 | } /* while (p1 < ptr) */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2448 | |
| 2449 | /* There's no more cookie on this line. |
| 2450 | * We may have marked the last one(s) for deletion. |
| 2451 | * We must do this now in two ways : |
| 2452 | * - if there is no app cookie, we simply delete the header ; |
| 2453 | * - if there are app cookies, we must delete the end of the |
| 2454 | * string properly, including the colon/semi-colon before |
| 2455 | * the cookie name. |
| 2456 | */ |
| 2457 | if (del_cookie != NULL) { |
| 2458 | if (app_cookies) { |
| 2459 | buffer_replace2(req, del_colon, ptr, NULL, 0); |
| 2460 | /* WARNING! <ptr> becomes invalid for now. If some code |
| 2461 | * below needs to rely on it before the end of the global |
| 2462 | * header loop, we need to correct it with this code : |
| 2463 | * ptr = del_colon; |
| 2464 | */ |
| 2465 | } |
| 2466 | else |
| 2467 | delete_header = 1; |
| 2468 | } |
| 2469 | } /* end of cookie processing on this header */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2470 | |
| 2471 | /* let's look if we have to delete this header */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2472 | if (delete_header && !(t->flags & SN_CLDENY)) { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2473 | buffer_replace2(req, req->h, req->lr, NULL, 0); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2474 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2475 | /* WARNING: ptr is not valid anymore, since the header may have been deleted or truncated ! */ |
| 2476 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2477 | req->h = req->lr; |
| 2478 | } /* while (req->lr < req->r) */ |
| 2479 | |
| 2480 | /* end of header processing (even if incomplete) */ |
| 2481 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2482 | if ((req->l < req->rlim - req->data) && ! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 2483 | /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer |
| 2484 | * full. We cannot loop here since event_cli_read will disable it only if |
| 2485 | * req->l == rlim-data |
| 2486 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2487 | FD_SET(t->cli_fd, StaticReadEvent); |
| 2488 | if (t->proxy->clitimeout) |
| 2489 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 2490 | else |
| 2491 | tv_eternity(&t->crexpire); |
| 2492 | } |
| 2493 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2494 | /* 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] | 2495 | * won't be able to free more later, so the session will never terminate. |
| 2496 | */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2497 | if (req->l >= req->rlim - req->data) { |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2498 | t->logs.status = 400; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2499 | if (t->proxy->mode == PR_MODE_HTTP) |
| 2500 | client_retnclose(t, strlen(HTTP_400), HTTP_400); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2501 | return 1; |
| 2502 | } |
| 2503 | else if (t->res_cr == RES_ERROR || t->res_cr == RES_NULL |
| 2504 | || tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 2505 | |
| 2506 | /* read timeout, read error, or last read : give up. |
| 2507 | * since we are in header mode, if there's no space left for headers, we |
| 2508 | * won't be able to free more later, so the session will never terminate. |
| 2509 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2510 | tv_eternity(&t->crexpire); |
| 2511 | fd_delete(t->cli_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2512 | t->cli_state = CL_STCLOSE; |
| 2513 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2514 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2515 | |
| 2516 | return t->cli_state != CL_STHEADERS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2517 | } |
| 2518 | else if (c == CL_STDATA) { |
| 2519 | /* read or write error */ |
| 2520 | if (t->res_cw == RES_ERROR || t->res_cr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2521 | tv_eternity(&t->crexpire); |
| 2522 | tv_eternity(&t->cwexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2523 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2524 | t->cli_state = CL_STCLOSE; |
| 2525 | return 1; |
| 2526 | } |
| 2527 | /* read timeout, last read, or end of server write */ |
| 2528 | else if (t->res_cr == RES_NULL || s == SV_STSHUTW || s == SV_STCLOSE |
| 2529 | || tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2530 | FD_CLR(t->cli_fd, StaticReadEvent); |
| 2531 | // if (req->l == 0) /* nothing to write on the server side */ |
| 2532 | // FD_CLR(t->srv_fd, StaticWriteEvent); |
| 2533 | tv_eternity(&t->crexpire); |
| 2534 | shutdown(t->cli_fd, SHUT_RD); |
| 2535 | t->cli_state = CL_STSHUTR; |
| 2536 | return 1; |
| 2537 | } |
| 2538 | /* write timeout, or last server read and buffer empty */ |
| 2539 | else if (((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) |
| 2540 | ||(tv_cmp2_ms(&t->cwexpire, &now) <= 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2541 | FD_CLR(t->cli_fd, StaticWriteEvent); |
| 2542 | tv_eternity(&t->cwexpire); |
| 2543 | shutdown(t->cli_fd, SHUT_WR); |
| 2544 | t->cli_state = CL_STSHUTW; |
| 2545 | return 1; |
| 2546 | } |
| 2547 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2548 | if (req->l >= req->rlim - req->data) { |
| 2549 | /* no room to read more data */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2550 | if (FD_ISSET(t->cli_fd, StaticReadEvent)) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2551 | /* stop reading until we get some space */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2552 | FD_CLR(t->cli_fd, StaticReadEvent); |
| 2553 | tv_eternity(&t->crexpire); |
| 2554 | } |
| 2555 | } |
| 2556 | else { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2557 | /* there's still some space in the buffer */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2558 | if (! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 2559 | FD_SET(t->cli_fd, StaticReadEvent); |
| 2560 | if (t->proxy->clitimeout) |
| 2561 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 2562 | else |
| 2563 | tv_eternity(&t->crexpire); |
| 2564 | } |
| 2565 | } |
| 2566 | |
| 2567 | if ((rep->l == 0) || |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2568 | ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2569 | if (FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 2570 | FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */ |
| 2571 | tv_eternity(&t->cwexpire); |
| 2572 | } |
| 2573 | } |
| 2574 | else { /* buffer not empty */ |
| 2575 | if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 2576 | FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */ |
| 2577 | if (t->proxy->clitimeout) |
| 2578 | tv_delayfrom(&t->cwexpire, &now, t->proxy->clitimeout); |
| 2579 | else |
| 2580 | tv_eternity(&t->cwexpire); |
| 2581 | } |
| 2582 | } |
| 2583 | return 0; /* other cases change nothing */ |
| 2584 | } |
| 2585 | else if (c == CL_STSHUTR) { |
| 2586 | if ((t->res_cw == RES_ERROR) || |
| 2587 | ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) |
willy tarreau | b719f00 | 2005-12-17 12:55:07 +0100 | [diff] [blame] | 2588 | || (tv_cmp2_ms(&t->cwexpire, &now) <= 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2589 | tv_eternity(&t->cwexpire); |
| 2590 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2591 | t->cli_state = CL_STCLOSE; |
| 2592 | return 1; |
| 2593 | } |
| 2594 | else if ((rep->l == 0) || |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2595 | ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2596 | if (FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 2597 | FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */ |
| 2598 | tv_eternity(&t->cwexpire); |
| 2599 | } |
| 2600 | } |
| 2601 | else { /* buffer not empty */ |
| 2602 | if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 2603 | FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */ |
| 2604 | if (t->proxy->clitimeout) |
| 2605 | tv_delayfrom(&t->cwexpire, &now, t->proxy->clitimeout); |
| 2606 | else |
| 2607 | tv_eternity(&t->cwexpire); |
| 2608 | } |
| 2609 | } |
| 2610 | return 0; |
| 2611 | } |
| 2612 | else if (c == CL_STSHUTW) { |
| 2613 | if (t->res_cr == RES_ERROR || t->res_cr == RES_NULL || s == SV_STSHUTW || |
willy tarreau | b719f00 | 2005-12-17 12:55:07 +0100 | [diff] [blame] | 2614 | s == SV_STCLOSE || tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2615 | tv_eternity(&t->crexpire); |
| 2616 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2617 | t->cli_state = CL_STCLOSE; |
| 2618 | return 1; |
| 2619 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2620 | else if (req->l >= req->rlim - req->data) { |
| 2621 | /* no room to read more data */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2622 | if (FD_ISSET(t->cli_fd, StaticReadEvent)) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2623 | /* stop reading until we get some space */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2624 | FD_CLR(t->cli_fd, StaticReadEvent); |
| 2625 | tv_eternity(&t->crexpire); |
| 2626 | } |
| 2627 | } |
| 2628 | else { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2629 | /* there's still some space in the buffer */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2630 | if (! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 2631 | FD_SET(t->cli_fd, StaticReadEvent); |
| 2632 | if (t->proxy->clitimeout) |
| 2633 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 2634 | else |
| 2635 | tv_eternity(&t->crexpire); |
| 2636 | } |
| 2637 | } |
| 2638 | return 0; |
| 2639 | } |
| 2640 | else { /* CL_STCLOSE: nothing to do */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2641 | if ((global.mode & MODE_DEBUG) && !(global.mode & MODE_QUIET)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2642 | int len; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2643 | len = sprintf(trash, "clicls[%04x:%04x]\n", (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2644 | write(1, trash, len); |
| 2645 | } |
| 2646 | return 0; |
| 2647 | } |
| 2648 | return 0; |
| 2649 | } |
| 2650 | |
| 2651 | |
| 2652 | /* |
| 2653 | * manages the server FSM and its socket. It returns 1 if a state has changed |
| 2654 | * (and a resync may be needed), 0 else. |
| 2655 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2656 | int process_srv(struct session *t) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2657 | int s = t->srv_state; |
| 2658 | int c = t->cli_state; |
| 2659 | struct buffer *req = t->req; |
| 2660 | struct buffer *rep = t->rep; |
| 2661 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2662 | #ifdef DEBUG_FULL |
| 2663 | fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 2664 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2665 | //fprintf(stderr,"process_srv: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s, |
| 2666 | //FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 2667 | //FD_ISSET(t->srv_fd, StaticReadEvent), FD_ISSET(t->srv_fd, StaticWriteEvent) |
| 2668 | //); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2669 | if (s == SV_STIDLE) { |
| 2670 | if (c == CL_STHEADERS) |
| 2671 | return 0; /* stay in idle, waiting for data to reach the client side */ |
| 2672 | else if (c == CL_STCLOSE || |
| 2673 | c == CL_STSHUTW || |
| 2674 | (c == CL_STSHUTR && t->req->l == 0)) { /* give up */ |
| 2675 | tv_eternity(&t->cnexpire); |
| 2676 | t->srv_state = SV_STCLOSE; |
| 2677 | return 1; |
| 2678 | } |
| 2679 | else { /* go to SV_STCONN */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2680 | if (connect_server(t) == 0) { /* initiate a connection to the server */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2681 | //fprintf(stderr,"0: c=%d, s=%d\n", c, s); |
| 2682 | t->srv_state = SV_STCONN; |
| 2683 | } |
| 2684 | else { /* try again */ |
| 2685 | while (t->conn_retries-- > 0) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2686 | if ((t->proxy->options & PR_O_REDISP) && (t->conn_retries == 0)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2687 | t->flags &= ~SN_DIRECT; /* ignore cookie and force to use the dispatcher */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2688 | t->srv = NULL; /* it's left to the dispatcher to choose a server */ |
| 2689 | } |
| 2690 | |
| 2691 | if (connect_server(t) == 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2692 | t->srv_state = SV_STCONN; |
| 2693 | break; |
| 2694 | } |
| 2695 | } |
| 2696 | if (t->conn_retries < 0) { |
| 2697 | /* if conn_retries < 0 or other error, let's abort */ |
| 2698 | tv_eternity(&t->cnexpire); |
| 2699 | t->srv_state = SV_STCLOSE; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2700 | t->logs.status = 502; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2701 | if (t->proxy->mode == PR_MODE_HTTP) |
| 2702 | client_return(t, strlen(HTTP_502), HTTP_502); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2703 | } |
| 2704 | } |
| 2705 | return 1; |
| 2706 | } |
| 2707 | } |
| 2708 | else if (s == SV_STCONN) { /* connection in progress */ |
| 2709 | if (t->res_sw == RES_SILENT && tv_cmp2_ms(&t->cnexpire, &now) > 0) { |
| 2710 | //fprintf(stderr,"1: c=%d, s=%d\n", c, s); |
| 2711 | return 0; /* nothing changed */ |
| 2712 | } |
| 2713 | else if (t->res_sw == RES_SILENT || t->res_sw == RES_ERROR) { |
| 2714 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 2715 | /* timeout, connect error or first write error */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2716 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2717 | fd_delete(t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2718 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2719 | t->conn_retries--; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2720 | if (t->conn_retries >= 0) { |
| 2721 | if ((t->proxy->options & PR_O_REDISP) && (t->conn_retries == 0)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2722 | t->flags &= ~SN_DIRECT; /* ignore cookie and force to use the dispatcher */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2723 | t->srv = NULL; /* it's left to the dispatcher to choose a server */ |
| 2724 | } |
| 2725 | if (connect_server(t) == 0) |
| 2726 | return 0; /* no state changed */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2727 | } |
| 2728 | /* if conn_retries < 0 or other error, let's abort */ |
| 2729 | tv_eternity(&t->cnexpire); |
| 2730 | t->srv_state = SV_STCLOSE; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2731 | t->logs.status = 502; |
| 2732 | if (t->proxy->mode == PR_MODE_HTTP) |
| 2733 | client_return(t, strlen(HTTP_502), HTTP_502); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2734 | return 1; |
| 2735 | } |
| 2736 | else { /* no error or write 0 */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2737 | t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2738 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2739 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
| 2740 | if (req->l == 0) /* nothing to write */ |
| 2741 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 2742 | else /* need the right to write */ |
| 2743 | FD_SET(t->srv_fd, StaticWriteEvent); |
| 2744 | |
| 2745 | if (t->proxy->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 2746 | FD_SET(t->srv_fd, StaticReadEvent); |
| 2747 | if (t->proxy->srvtimeout) |
| 2748 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 2749 | else |
| 2750 | tv_eternity(&t->srexpire); |
| 2751 | |
| 2752 | t->srv_state = SV_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2753 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2754 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2755 | else { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2756 | t->srv_state = SV_STHEADERS; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2757 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
| 2758 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2759 | tv_eternity(&t->cnexpire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2760 | return 1; |
| 2761 | } |
| 2762 | } |
| 2763 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2764 | |
| 2765 | /* now parse the partial (or complete) headers */ |
| 2766 | while (rep->lr < rep->r) { /* this loop only sees one header at each iteration */ |
| 2767 | char *ptr; |
| 2768 | int delete_header; |
| 2769 | |
| 2770 | ptr = rep->lr; |
| 2771 | |
| 2772 | /* look for the end of the current header */ |
| 2773 | while (ptr < rep->r && *ptr != '\n' && *ptr != '\r') |
| 2774 | ptr++; |
| 2775 | |
| 2776 | if (ptr == rep->h) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2777 | int line, len; |
| 2778 | |
| 2779 | /* we can only get here after an end of headers */ |
| 2780 | /* we'll have something else to do here : add new headers ... */ |
| 2781 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2782 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_INS)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2783 | /* the server is known, it's not the one the client requested, we have to |
| 2784 | * insert a set-cookie here. |
| 2785 | */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2786 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/\r\n", |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2787 | t->proxy->cookie_name, t->srv->cookie); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2788 | |
| 2789 | /* Here, we will tell an eventual cache on the client side that we don't |
| 2790 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 2791 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 2792 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 2793 | */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2794 | if (t->proxy->options & PR_O_COOK_NOC) |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2795 | //len += sprintf(newhdr + len, "Cache-control: no-cache=\"set-cookie\"\r\n"); |
| 2796 | len += sprintf(trash + len, "Cache-control: private\r\n"); |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2797 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2798 | buffer_replace2(rep, rep->h, rep->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | /* headers to be added */ |
| 2802 | for (line = 0; line < t->proxy->nb_rspadd; line++) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2803 | len = sprintf(trash, "%s\r\n", t->proxy->rsp_add[line]); |
| 2804 | buffer_replace2(rep, rep->h, rep->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2805 | } |
| 2806 | |
| 2807 | t->srv_state = SV_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2808 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2809 | t->logs.t_data = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2810 | break; |
| 2811 | } |
| 2812 | |
| 2813 | /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */ |
| 2814 | if (ptr > rep->r - 2) { |
| 2815 | /* this is a partial header, let's wait for more to come */ |
| 2816 | rep->lr = ptr; |
| 2817 | break; |
| 2818 | } |
| 2819 | |
| 2820 | // fprintf(stderr,"h=%p, ptr=%p, lr=%p, r=%p, *h=", rep->h, ptr, rep->lr, rep->r); |
| 2821 | // write(2, rep->h, ptr - rep->h); fprintf(stderr,"\n"); |
| 2822 | |
| 2823 | /* now we know that *ptr is either \r or \n, |
| 2824 | * and that there are at least 1 char after it. |
| 2825 | */ |
| 2826 | if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n')) |
| 2827 | rep->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */ |
| 2828 | else |
| 2829 | rep->lr = ptr + 2; /* \r\n or \n\r */ |
| 2830 | |
| 2831 | /* |
| 2832 | * now we know that we have a full header ; we can do whatever |
| 2833 | * we want with these pointers : |
| 2834 | * rep->h = beginning of header |
| 2835 | * ptr = end of header (first \r or \n) |
| 2836 | * rep->lr = beginning of next line (next rep->h) |
| 2837 | * rep->r = end of data (not used at this stage) |
| 2838 | */ |
| 2839 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2840 | |
| 2841 | if (t->logs.logwait & LW_RESP) { |
| 2842 | t->logs.logwait &= ~LW_RESP; |
| 2843 | t->logs.status = atoi(rep->h + 9); |
| 2844 | } |
| 2845 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2846 | delete_header = 0; |
| 2847 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2848 | if ((global.mode & MODE_DEBUG) && !(global.mode & MODE_QUIET)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2849 | int len, max; |
| 2850 | len = sprintf(trash, "srvhdr[%04x:%04x]: ", (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
| 2851 | max = ptr - rep->h; |
| 2852 | UBOUND(max, sizeof(trash) - len - 1); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2853 | len += strlcpy2(trash + len, rep->h, max + 1); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2854 | trash[len++] = '\n'; |
| 2855 | write(1, trash, len); |
| 2856 | } |
| 2857 | |
| 2858 | /* try headers regexps */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2859 | if (t->proxy->rsp_exp != NULL && !(t->flags & SN_SVDENY)) { |
| 2860 | struct hdr_exp *exp; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2861 | char term; |
| 2862 | |
| 2863 | term = *ptr; |
| 2864 | *ptr = '\0'; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2865 | exp = t->proxy->rsp_exp; |
| 2866 | do { |
| 2867 | if (regexec(exp->preg, rep->h, MAX_MATCH, pmatch, 0) == 0) { |
| 2868 | switch (exp->action) { |
| 2869 | case ACT_ALLOW: |
| 2870 | if (!(t->flags & SN_SVDENY)) |
| 2871 | t->flags |= SN_SVALLOW; |
| 2872 | break; |
| 2873 | case ACT_REPLACE: |
| 2874 | if (!(t->flags & SN_SVDENY)) { |
| 2875 | int len = exp_replace(trash, rep->h, exp->replace, pmatch); |
| 2876 | ptr += buffer_replace2(rep, rep->h, ptr, trash, len); |
| 2877 | } |
| 2878 | break; |
| 2879 | case ACT_REMOVE: |
| 2880 | if (!(t->flags & SN_SVDENY)) |
| 2881 | delete_header = 1; |
| 2882 | break; |
| 2883 | case ACT_DENY: |
| 2884 | if (!(t->flags & SN_SVALLOW)) |
| 2885 | t->flags |= SN_SVDENY; |
| 2886 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2887 | } |
| 2888 | break; |
| 2889 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2890 | } while ((exp = exp->next) != NULL); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2891 | *ptr = term; /* restore the string terminator */ |
| 2892 | } |
| 2893 | |
| 2894 | /* check for server cookies */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2895 | if (!delete_header && (t->proxy->options & PR_O_COOK_ANY) |
| 2896 | && (t->proxy->cookie_name != NULL) && (ptr >= rep->h + 12) |
| 2897 | && (strncmp(rep->h, "Set-Cookie: ", 12) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2898 | char *p1, *p2, *p3, *p4; |
| 2899 | |
| 2900 | p1 = rep->h + 12; /* first char after 'Set-Cookie: ' */ |
| 2901 | |
| 2902 | while (p1 < ptr) { /* in fact, we'll break after the first cookie */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 2903 | while (p1 < ptr && (isspace((int)*p1))) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2904 | p1++; |
| 2905 | |
| 2906 | if (p1 == ptr || *p1 == ';') /* end of cookie */ |
| 2907 | break; |
| 2908 | |
| 2909 | /* p1 is at the beginning of the cookie name */ |
| 2910 | p2 = p1; |
| 2911 | |
| 2912 | while (p2 < ptr && *p2 != '=' && *p2 != ';') |
| 2913 | p2++; |
| 2914 | |
| 2915 | if (p2 == ptr || *p2 == ';') /* next cookie */ |
| 2916 | break; |
| 2917 | |
| 2918 | p3 = p2 + 1; /* skips the '=' sign */ |
| 2919 | if (p3 == ptr) |
| 2920 | break; |
| 2921 | |
| 2922 | p4 = p3; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 2923 | while (p4 < ptr && !isspace((int)*p4) && *p4 != ';') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2924 | p4++; |
| 2925 | |
| 2926 | /* here, we have the cookie name between p1 and p2, |
| 2927 | * and its value between p3 and p4. |
| 2928 | * we can process it. |
| 2929 | */ |
| 2930 | |
| 2931 | if ((p2 - p1 == strlen(t->proxy->cookie_name)) && |
| 2932 | (strncmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) { |
| 2933 | /* Cool... it's the right one */ |
| 2934 | |
| 2935 | /* If the cookie is in insert mode on a known server, we'll delete |
| 2936 | * this occurrence because we'll insert another one later. |
| 2937 | * We'll delete it too if the "indirect" option is set and we're in |
| 2938 | * a direct access. */ |
| 2939 | if (((t->srv) && (t->proxy->options & PR_O_COOK_INS)) || |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2940 | ((t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_IND))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2941 | /* this header must be deleted */ |
| 2942 | delete_header = 1; |
| 2943 | } |
| 2944 | else if ((t->srv) && (t->proxy->options & PR_O_COOK_RW)) { |
| 2945 | /* replace bytes p3->p4 with the cookie name associated |
| 2946 | * with this server since we know it. |
| 2947 | */ |
| 2948 | buffer_replace2(rep, p3, p4, t->srv->cookie, t->srv->cklen); |
| 2949 | } |
| 2950 | break; |
| 2951 | } |
| 2952 | else { |
| 2953 | // fprintf(stderr,"Ignoring unknown cookie : "); |
| 2954 | // write(2, p1, p2-p1); |
| 2955 | // fprintf(stderr," = "); |
| 2956 | // write(2, p3, p4-p3); |
| 2957 | // fprintf(stderr,"\n"); |
| 2958 | } |
| 2959 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 2960 | } /* we're now at the end of the cookie value */ |
| 2961 | } /* end of cookie processing */ |
| 2962 | |
| 2963 | /* let's look if we have to delete this header */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2964 | if (delete_header && !(t->flags & SN_SVDENY)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2965 | buffer_replace2(rep, rep->h, rep->lr, "", 0); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2966 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2967 | rep->h = rep->lr; |
| 2968 | } /* while (rep->lr < rep->r) */ |
| 2969 | |
| 2970 | /* end of header processing (even if incomplete) */ |
| 2971 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2972 | if ((rep->l < rep->rlim - rep->data) && ! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 2973 | /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer |
| 2974 | * full. We cannot loop here since event_srv_read will disable it only if |
| 2975 | * rep->l == rlim-data |
| 2976 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2977 | FD_SET(t->srv_fd, StaticReadEvent); |
| 2978 | if (t->proxy->srvtimeout) |
| 2979 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 2980 | else |
| 2981 | tv_eternity(&t->srexpire); |
| 2982 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2983 | |
| 2984 | /* read or write error */ |
| 2985 | if (t->res_sw == RES_ERROR || t->res_sr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2986 | tv_eternity(&t->srexpire); |
| 2987 | tv_eternity(&t->swexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2988 | fd_delete(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2989 | t->srv_state = SV_STCLOSE; |
| 2990 | return 1; |
| 2991 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2992 | /* read timeout, last read, or end of client write |
| 2993 | * since we are in header mode, if there's no space left for headers, we |
| 2994 | * won't be able to free more later, so the session will never terminate. |
| 2995 | */ |
| 2996 | else if (t->res_sr == RES_NULL || c == CL_STSHUTW || c == CL_STCLOSE |
| 2997 | || rep->l >= rep->rlim - rep->data || tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2998 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 2999 | tv_eternity(&t->srexpire); |
| 3000 | shutdown(t->srv_fd, SHUT_RD); |
| 3001 | t->srv_state = SV_STSHUTR; |
| 3002 | return 1; |
| 3003 | |
| 3004 | } |
| 3005 | /* write timeout, or last client read and buffer empty */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3006 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 3007 | * client shuts read too early, because we may still have |
| 3008 | * some work to do on the headers. |
| 3009 | */ |
| 3010 | else if (((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && (req->l == 0)) || |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3011 | (tv_cmp2_ms(&t->swexpire, &now) <= 0)) { |
| 3012 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 3013 | tv_eternity(&t->swexpire); |
| 3014 | shutdown(t->srv_fd, SHUT_WR); |
| 3015 | t->srv_state = SV_STSHUTW; |
| 3016 | return 1; |
| 3017 | } |
| 3018 | |
| 3019 | if (req->l == 0) { |
| 3020 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 3021 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 3022 | tv_eternity(&t->swexpire); |
| 3023 | } |
| 3024 | } |
| 3025 | else { /* client buffer not empty */ |
| 3026 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 3027 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
| 3028 | if (t->proxy->srvtimeout) |
| 3029 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
| 3030 | else |
| 3031 | tv_eternity(&t->swexpire); |
| 3032 | } |
| 3033 | } |
| 3034 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3035 | /* be nice with the client side which would like to send a complete header |
| 3036 | * FIXME: COMPLETELY BUGGY !!! not all headers may be processed because the client |
| 3037 | * would read all remaining data at once ! The client should not write past rep->lr |
| 3038 | * when the server is in header state. |
| 3039 | */ |
| 3040 | //return header_processed; |
| 3041 | return t->srv_state != SV_STHEADERS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3042 | } |
| 3043 | else if (s == SV_STDATA) { |
| 3044 | /* read or write error */ |
| 3045 | if (t->res_sw == RES_ERROR || t->res_sr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3046 | tv_eternity(&t->srexpire); |
| 3047 | tv_eternity(&t->swexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3048 | fd_delete(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3049 | t->srv_state = SV_STCLOSE; |
| 3050 | return 1; |
| 3051 | } |
| 3052 | /* read timeout, last read, or end of client write */ |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3053 | else if (t->res_sr == RES_NULL || c == CL_STSHUTW || c == CL_STCLOSE |
| 3054 | || tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3055 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 3056 | tv_eternity(&t->srexpire); |
| 3057 | shutdown(t->srv_fd, SHUT_RD); |
| 3058 | t->srv_state = SV_STSHUTR; |
| 3059 | return 1; |
| 3060 | |
| 3061 | } |
| 3062 | /* write timeout, or last client read and buffer empty */ |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3063 | else if (((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) |
| 3064 | || (tv_cmp2_ms(&t->swexpire, &now) <= 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3065 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 3066 | tv_eternity(&t->swexpire); |
| 3067 | shutdown(t->srv_fd, SHUT_WR); |
| 3068 | t->srv_state = SV_STSHUTW; |
| 3069 | return 1; |
| 3070 | } |
| 3071 | else if (req->l == 0) { |
| 3072 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 3073 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 3074 | tv_eternity(&t->swexpire); |
| 3075 | } |
| 3076 | } |
| 3077 | else { /* buffer not empty */ |
| 3078 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 3079 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
| 3080 | if (t->proxy->srvtimeout) |
| 3081 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
| 3082 | else |
| 3083 | tv_eternity(&t->swexpire); |
| 3084 | } |
| 3085 | } |
| 3086 | |
| 3087 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
| 3088 | if (FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 3089 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 3090 | tv_eternity(&t->srexpire); |
| 3091 | } |
| 3092 | } |
| 3093 | else { |
| 3094 | if (! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 3095 | FD_SET(t->srv_fd, StaticReadEvent); |
| 3096 | if (t->proxy->srvtimeout) |
| 3097 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 3098 | else |
| 3099 | tv_eternity(&t->srexpire); |
| 3100 | } |
| 3101 | } |
| 3102 | |
| 3103 | return 0; /* other cases change nothing */ |
| 3104 | } |
| 3105 | else if (s == SV_STSHUTR) { |
| 3106 | if ((t->res_sw == RES_ERROR) || |
| 3107 | ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) || |
| 3108 | (tv_cmp2_ms(&t->swexpire, &now) <= 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3109 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3110 | tv_eternity(&t->swexpire); |
| 3111 | fd_delete(t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3112 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3113 | t->srv_state = SV_STCLOSE; |
| 3114 | return 1; |
| 3115 | } |
| 3116 | else if (req->l == 0) { |
| 3117 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 3118 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 3119 | tv_eternity(&t->swexpire); |
| 3120 | } |
| 3121 | } |
| 3122 | else { /* buffer not empty */ |
| 3123 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 3124 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
| 3125 | if (t->proxy->srvtimeout) |
| 3126 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
| 3127 | else |
| 3128 | tv_eternity(&t->swexpire); |
| 3129 | } |
| 3130 | } |
| 3131 | return 0; |
| 3132 | } |
| 3133 | else if (s == SV_STSHUTW) { |
| 3134 | if (t->res_sr == RES_ERROR || t->res_sr == RES_NULL || |
| 3135 | c == CL_STSHUTW || c == CL_STCLOSE || |
| 3136 | tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3137 | //FD_CLR(t->srv_fd, StaticReadEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3138 | tv_eternity(&t->srexpire); |
| 3139 | fd_delete(t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3140 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3141 | t->srv_state = SV_STCLOSE; |
| 3142 | return 1; |
| 3143 | } |
| 3144 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
| 3145 | if (FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 3146 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 3147 | tv_eternity(&t->srexpire); |
| 3148 | } |
| 3149 | } |
| 3150 | else { |
| 3151 | if (! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 3152 | FD_SET(t->srv_fd, StaticReadEvent); |
| 3153 | if (t->proxy->srvtimeout) |
| 3154 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 3155 | else |
| 3156 | tv_eternity(&t->srexpire); |
| 3157 | } |
| 3158 | } |
| 3159 | return 0; |
| 3160 | } |
| 3161 | else { /* SV_STCLOSE : nothing to do */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3162 | if ((global.mode & MODE_DEBUG) && !(global.mode & MODE_QUIET)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3163 | int len; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3164 | len = sprintf(trash, "srvcls[%04x:%04x]\n", (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3165 | write(1, trash, len); |
| 3166 | } |
| 3167 | return 0; |
| 3168 | } |
| 3169 | return 0; |
| 3170 | } |
| 3171 | |
| 3172 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3173 | /* Processes the client and server jobs of a session task, then |
| 3174 | * puts it back to the wait queue in a clean state, or |
| 3175 | * cleans up its resources if it must be deleted. Returns |
| 3176 | * the time the task accepts to wait, or -1 for infinity |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3177 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3178 | int process_session(struct task *t) { |
| 3179 | struct session *s = t->context; |
| 3180 | int fsm_resync = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3181 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3182 | do { |
| 3183 | fsm_resync = 0; |
| 3184 | //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", t->cli_state, t->srv_state); |
| 3185 | fsm_resync |= process_cli(s); |
| 3186 | //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", t->cli_state, t->srv_state); |
| 3187 | fsm_resync |= process_srv(s); |
| 3188 | //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", t->cli_state, t->srv_state); |
| 3189 | } while (fsm_resync); |
| 3190 | |
| 3191 | if (s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3192 | struct timeval min1, min2; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3193 | 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] | 3194 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3195 | tv_min(&min1, &s->crexpire, &s->cwexpire); |
| 3196 | tv_min(&min2, &s->srexpire, &s->swexpire); |
| 3197 | tv_min(&min1, &min1, &s->cnexpire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3198 | tv_min(&t->expire, &min1, &min2); |
| 3199 | |
| 3200 | /* restore t to its place in the task list */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3201 | task_queue(t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3202 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3203 | return tv_remain(&now, &t->expire); /* nothing more to do */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3204 | } |
| 3205 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3206 | s->proxy->nbconn--; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3207 | actconn--; |
| 3208 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3209 | if ((global.mode & MODE_DEBUG) && !(global.mode & MODE_QUIET)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3210 | int len; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3211 | len = sprintf(trash, "closed[%04x:%04x]\n", (unsigned short)s->cli_fd, (unsigned short)s->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3212 | write(1, trash, len); |
| 3213 | } |
| 3214 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3215 | s->logs.t_close = tv_diff(&s->logs.tv_accept, &now); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3216 | if (s->rep != NULL) |
| 3217 | s->logs.bytes = s->rep->total; |
| 3218 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3219 | /* let's do a final log if we need it */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3220 | 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] | 3221 | sess_log(s); |
| 3222 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3223 | /* the task MUST not be in the run queue anymore */ |
| 3224 | task_delete(t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3225 | session_free(s); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3226 | task_free(t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3227 | return -1; /* rest in peace for eternity */ |
| 3228 | } |
| 3229 | |
| 3230 | |
| 3231 | |
| 3232 | /* |
| 3233 | * manages a server health-check. Returns |
| 3234 | * the time the task accepts to wait, or -1 for infinity. |
| 3235 | */ |
| 3236 | int process_chk(struct task *t) { |
| 3237 | struct server *s = t->context; |
| 3238 | int fd = s->curfd; |
| 3239 | int one = 1; |
| 3240 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3241 | //fprintf(stderr, "process_chk: task=%p\n", t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3242 | |
| 3243 | if (fd < 0) { /* no check currently running */ |
| 3244 | //fprintf(stderr, "process_chk: 2\n"); |
| 3245 | if (tv_cmp2_ms(&t->expire, &now) > 0) { /* not good time yet */ |
| 3246 | task_queue(t); /* restore t to its place in the task list */ |
| 3247 | return tv_remain(&now, &t->expire); |
| 3248 | } |
| 3249 | |
| 3250 | /* we'll initiate a new check */ |
| 3251 | s->result = 0; /* no result yet */ |
| 3252 | if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3253 | if ((fd < global.maxsock) && |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3254 | (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) && |
| 3255 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) { |
| 3256 | //fprintf(stderr, "process_chk: 3\n"); |
| 3257 | |
| 3258 | if ((connect(fd, (struct sockaddr *)&s->addr, sizeof(s->addr)) != -1) || (errno == EINPROGRESS)) { |
| 3259 | /* OK, connection in progress or established */ |
| 3260 | |
| 3261 | //fprintf(stderr, "process_chk: 4\n"); |
| 3262 | |
| 3263 | s->curfd = fd; /* that's how we know a test is in progress ;-) */ |
| 3264 | fdtab[fd].owner = t; |
| 3265 | fdtab[fd].read = NULL; |
| 3266 | fdtab[fd].write = &event_srv_hck; |
| 3267 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 3268 | FD_SET(fd, StaticWriteEvent); /* for connect status */ |
| 3269 | fd_insert(fd); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3270 | /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */ |
| 3271 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3272 | task_queue(t); /* restore t to its place in the task list */ |
| 3273 | return tv_remain(&now, &t->expire); |
| 3274 | } |
| 3275 | else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) { |
| 3276 | s->result = -1; /* a real error */ |
| 3277 | } |
| 3278 | } |
| 3279 | //fprintf(stderr, "process_chk: 5\n"); |
| 3280 | close(fd); |
| 3281 | } |
| 3282 | |
| 3283 | if (!s->result) { /* nothing done */ |
| 3284 | //fprintf(stderr, "process_chk: 6\n"); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3285 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3286 | task_queue(t); /* restore t to its place in the task list */ |
| 3287 | return tv_remain(&now, &t->expire); |
| 3288 | } |
| 3289 | |
| 3290 | /* here, we have seen a failure */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3291 | if (s->health > s->rise) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3292 | s->health--; /* still good */ |
| 3293 | else { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 3294 | if (s->health == s->rise) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3295 | if (!(global.mode & MODE_QUIET)) |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 3296 | Warning("server %s DOWN.\n", s->id); |
| 3297 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3298 | send_log(s->proxy, LOG_ALERT, "Server %s/%s is DOWN.\n", s->proxy->id, s->id); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 3299 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3300 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3301 | s->health = 0; /* failure */ |
| 3302 | s->state &= ~SRV_RUNNING; |
| 3303 | } |
| 3304 | |
| 3305 | //fprintf(stderr, "process_chk: 7\n"); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3306 | /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */ |
| 3307 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3308 | } |
| 3309 | else { |
| 3310 | //fprintf(stderr, "process_chk: 8\n"); |
| 3311 | /* there was a test running */ |
| 3312 | if (s->result > 0) { /* good server detected */ |
| 3313 | //fprintf(stderr, "process_chk: 9\n"); |
| 3314 | s->health++; /* was bad, stays for a while */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3315 | if (s->health >= s->rise) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 3316 | if (s->health == s->rise) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3317 | if (!(global.mode & MODE_QUIET)) |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 3318 | Warning("server %s UP.\n", s->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3319 | 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] | 3320 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3321 | |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3322 | s->health = s->rise + s->fall - 1; /* OK now */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3323 | s->state |= SRV_RUNNING; |
| 3324 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3325 | s->curfd = -1; /* no check running anymore */ |
| 3326 | //FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3327 | fd_delete(fd); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3328 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3329 | } |
| 3330 | else if (s->result < 0 || tv_cmp2_ms(&t->expire, &now) <= 0) { |
| 3331 | //fprintf(stderr, "process_chk: 10\n"); |
| 3332 | /* failure or timeout detected */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3333 | if (s->health > s->rise) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3334 | s->health--; /* still good */ |
| 3335 | else { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 3336 | if (s->health == s->rise) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3337 | if (!(global.mode & MODE_QUIET)) |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 3338 | Warning("server %s DOWN.\n", s->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3339 | |
| 3340 | send_log(s->proxy, LOG_ALERT, "Server %s/%s is DOWN.\n", s->proxy->id, s->id); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 3341 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3342 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3343 | s->health = 0; /* failure */ |
| 3344 | s->state &= ~SRV_RUNNING; |
| 3345 | } |
| 3346 | s->curfd = -1; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3347 | //FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3348 | fd_delete(fd); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3349 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3350 | } |
| 3351 | /* if result is 0 and there's no timeout, we have to wait again */ |
| 3352 | } |
| 3353 | //fprintf(stderr, "process_chk: 11\n"); |
| 3354 | s->result = 0; |
| 3355 | task_queue(t); /* restore t to its place in the task list */ |
| 3356 | return tv_remain(&now, &t->expire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3357 | } |
| 3358 | |
| 3359 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3360 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3361 | #if STATTIME > 0 |
| 3362 | int stats(void); |
| 3363 | #endif |
| 3364 | |
| 3365 | /* |
| 3366 | * Main select() loop. |
| 3367 | */ |
| 3368 | |
| 3369 | void select_loop() { |
| 3370 | int next_time; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3371 | int time2; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3372 | int status; |
| 3373 | int fd,i; |
| 3374 | struct timeval delta; |
| 3375 | int readnotnull, writenotnull; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3376 | struct task *t, *tnext; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3377 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3378 | tv_now(&now); |
| 3379 | |
| 3380 | while (1) { |
| 3381 | next_time = -1; /* set the timer to wait eternally first */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3382 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3383 | /* look for expired tasks and add them to the run queue. |
| 3384 | */ |
| 3385 | tnext = ((struct task *)LIST_HEAD(wait_queue))->next; |
| 3386 | while ((t = tnext) != LIST_HEAD(wait_queue)) { /* we haven't looped ? */ |
| 3387 | tnext = t->next; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3388 | if (t->state & TASK_RUNNING) |
| 3389 | continue; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3390 | |
| 3391 | /* wakeup expired entries. It doesn't matter if they are |
| 3392 | * already running because of a previous event |
| 3393 | */ |
| 3394 | if (tv_cmp2_ms(&t->expire, &now) <= 0) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3395 | //fprintf(stderr,"task_wakeup(%p, %p)\n", &rq, t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3396 | task_wakeup(&rq, t); |
| 3397 | } |
| 3398 | else { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3399 | /* first non-runnable task. Use its expiration date as an upper bound */ |
| 3400 | int temp_time = tv_remain(&now, &t->expire); |
| 3401 | if (temp_time) |
| 3402 | next_time = temp_time; |
| 3403 | //fprintf(stderr,"no_task_wakeup(%p, %p) : expire in %d ms\n", &rq, t, temp_time); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3404 | break; |
| 3405 | } |
| 3406 | } |
| 3407 | |
| 3408 | /* process each task in the run queue now. Each task may be deleted |
| 3409 | * since we only use tnext. |
| 3410 | */ |
| 3411 | tnext = rq; |
| 3412 | while ((t = tnext) != NULL) { |
| 3413 | int temp_time; |
| 3414 | |
| 3415 | tnext = t->rqnext; |
| 3416 | task_sleep(&rq, t); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3417 | //fprintf(stderr,"task %p\n",t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3418 | temp_time = t->process(t); |
| 3419 | next_time = MINTIME(temp_time, next_time); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3420 | //fprintf(stderr,"process(%p)=%d -> next_time=%d)\n", t, temp_time, next_time); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3421 | } |
| 3422 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3423 | //fprintf(stderr,"---end of run---\n"); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3424 | |
| 3425 | /* maintain all proxies in a consistent state. This should quickly become a task */ |
| 3426 | time2 = maintain_proxies(); |
| 3427 | next_time = MINTIME(time2, next_time); |
| 3428 | |
| 3429 | /* stop when there's no connection left and we don't allow them anymore */ |
| 3430 | if (!actconn && listeners == 0) |
| 3431 | break; |
| 3432 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3433 | |
| 3434 | #if STATTIME > 0 |
| 3435 | time2 = stats(); |
| 3436 | // fprintf(stderr," stats = %d\n", time2); |
| 3437 | next_time = MINTIME(time2, next_time); |
| 3438 | #endif |
| 3439 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3440 | if (next_time > 0) { /* FIXME */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3441 | /* Convert to timeval */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3442 | /* to avoid eventual select loops due to timer precision */ |
| 3443 | next_time += SCHEDULER_RESOLUTION; |
| 3444 | delta.tv_sec = next_time / 1000; |
| 3445 | delta.tv_usec = (next_time % 1000) * 1000; |
| 3446 | } |
| 3447 | else if (next_time == 0) { /* allow select to return immediately when needed */ |
| 3448 | delta.tv_sec = delta.tv_usec = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3449 | } |
| 3450 | |
| 3451 | |
| 3452 | /* let's restore fdset state */ |
| 3453 | |
| 3454 | readnotnull = 0; writenotnull = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3455 | for (i = 0; i < (global.maxsock + FD_SETSIZE - 1)/(8*sizeof(int)); i++) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3456 | readnotnull |= (*(((int*)ReadEvent)+i) = *(((int*)StaticReadEvent)+i)) != 0; |
| 3457 | writenotnull |= (*(((int*)WriteEvent)+i) = *(((int*)StaticWriteEvent)+i)) != 0; |
| 3458 | } |
| 3459 | |
| 3460 | // /* just a verification code, needs to be removed for performance */ |
| 3461 | // for (i=0; i<maxfd; i++) { |
| 3462 | // if (FD_ISSET(i, ReadEvent) != FD_ISSET(i, StaticReadEvent)) |
| 3463 | // abort(); |
| 3464 | // if (FD_ISSET(i, WriteEvent) != FD_ISSET(i, StaticWriteEvent)) |
| 3465 | // abort(); |
| 3466 | // |
| 3467 | // } |
| 3468 | |
| 3469 | status=select(maxfd, |
| 3470 | readnotnull ? ReadEvent : NULL, |
| 3471 | writenotnull ? WriteEvent : NULL, |
| 3472 | NULL, |
| 3473 | (next_time >= 0) ? &delta : NULL); |
| 3474 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3475 | /* this is an experiment on the separation of the select work */ |
| 3476 | // status = (readnotnull ? select(maxfd, ReadEvent, NULL, NULL, (next_time >= 0) ? &delta : NULL) : 0); |
| 3477 | // status |= (writenotnull ? select(maxfd, NULL, WriteEvent, NULL, (next_time >= 0) ? &delta : NULL) : 0); |
| 3478 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3479 | tv_now(&now); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3480 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3481 | if (status > 0) { /* must proceed with events */ |
| 3482 | |
| 3483 | int fds; |
| 3484 | char count; |
| 3485 | |
| 3486 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) |
| 3487 | if ((((int *)(ReadEvent))[fds] | ((int *)(WriteEvent))[fds]) != 0) |
| 3488 | for (count = 1<<INTBITS, fd = fds << INTBITS; count && fd < maxfd; count--, fd++) { |
| 3489 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3490 | /* if we specify read first, the accepts and zero reads will be |
| 3491 | * seen first. Moreover, system buffers will be flushed faster. |
| 3492 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3493 | if (fdtab[fd].state == FD_STCLOSE) |
| 3494 | continue; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3495 | |
| 3496 | if (FD_ISSET(fd, ReadEvent)) |
| 3497 | fdtab[fd].read(fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3498 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3499 | if (FD_ISSET(fd, WriteEvent)) |
| 3500 | fdtab[fd].write(fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3501 | } |
| 3502 | } |
| 3503 | else { |
| 3504 | // fprintf(stderr,"select returned %d, maxfd=%d\n", status, maxfd); |
| 3505 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3506 | } |
| 3507 | } |
| 3508 | |
| 3509 | |
| 3510 | #if STATTIME > 0 |
| 3511 | /* |
| 3512 | * Display proxy statistics regularly. It is designed to be called from the |
| 3513 | * select_loop(). |
| 3514 | */ |
| 3515 | int stats(void) { |
| 3516 | static int lines; |
| 3517 | static struct timeval nextevt; |
| 3518 | static struct timeval lastevt; |
| 3519 | static struct timeval starttime = {0,0}; |
| 3520 | unsigned long totaltime, deltatime; |
| 3521 | int ret; |
| 3522 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3523 | if (tv_cmp(&now, &nextevt) > 0) { |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame^] | 3524 | deltatime = (tv_diff(&lastevt, &now)?:1); |
| 3525 | totaltime = (tv_diff(&starttime, &now)?:1); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3526 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3527 | if (global.mode & MODE_STATS) { |
| 3528 | if ((lines++ % 16 == 0) && !(global.mode & MODE_LOG)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3529 | qfprintf(stderr, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3530 | "\n active total tsknew tskgood tskleft tskrght tsknsch tsklsch tskrsch\n"); |
| 3531 | if (lines>1) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3532 | qfprintf(stderr,"%07d %07d %07d %07d %07d %07d %07d %07d %07d\n", |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3533 | actconn, totalconn, |
| 3534 | stats_tsk_new, stats_tsk_good, |
| 3535 | stats_tsk_left, stats_tsk_right, |
| 3536 | stats_tsk_nsrch, stats_tsk_lsrch, stats_tsk_rsrch); |
| 3537 | } |
| 3538 | } |
| 3539 | |
| 3540 | tv_delayfrom(&nextevt, &now, STATTIME); |
| 3541 | |
| 3542 | lastevt=now; |
| 3543 | } |
| 3544 | ret = tv_remain(&now, &nextevt); |
| 3545 | return ret; |
| 3546 | } |
| 3547 | #endif |
| 3548 | |
| 3549 | |
| 3550 | /* |
| 3551 | * this function enables proxies when there are enough free sessions, |
| 3552 | * 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] | 3553 | * select_loop(). It returns the time left before next expiration event |
| 3554 | * during stop time, -1 otherwise. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3555 | */ |
| 3556 | static int maintain_proxies(void) { |
| 3557 | struct proxy *p; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3558 | int tleft; /* time left */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3559 | |
| 3560 | p = proxy; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3561 | tleft = -1; /* infinite time */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3562 | |
| 3563 | /* if there are enough free sessions, we'll activate proxies */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3564 | if (actconn < global.maxconn) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3565 | while (p) { |
| 3566 | if (p->nbconn < p->maxconn) { |
| 3567 | if (p->state == PR_STIDLE) { |
| 3568 | FD_SET(p->listen_fd, StaticReadEvent); |
| 3569 | p->state = PR_STRUN; |
| 3570 | } |
| 3571 | } |
| 3572 | else { |
| 3573 | if (p->state == PR_STRUN) { |
| 3574 | FD_CLR(p->listen_fd, StaticReadEvent); |
| 3575 | p->state = PR_STIDLE; |
| 3576 | } |
| 3577 | } |
| 3578 | p = p->next; |
| 3579 | } |
| 3580 | } |
| 3581 | else { /* block all proxies */ |
| 3582 | while (p) { |
| 3583 | if (p->state == PR_STRUN) { |
| 3584 | FD_CLR(p->listen_fd, StaticReadEvent); |
| 3585 | p->state = PR_STIDLE; |
| 3586 | } |
| 3587 | p = p->next; |
| 3588 | } |
| 3589 | } |
| 3590 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3591 | if (stopping) { |
| 3592 | p = proxy; |
| 3593 | while (p) { |
| 3594 | if (p->state != PR_STDISABLED) { |
| 3595 | int t; |
| 3596 | t = tv_remain(&now, &p->stop_time); |
| 3597 | if (t == 0) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 3598 | Warning("Proxy %s stopped.\n", p->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3599 | send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 3600 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3601 | fd_delete(p->listen_fd); |
| 3602 | p->state = PR_STDISABLED; |
| 3603 | listeners--; |
| 3604 | } |
| 3605 | else { |
| 3606 | tleft = MINTIME(t, tleft); |
| 3607 | } |
| 3608 | } |
| 3609 | p = p->next; |
| 3610 | } |
| 3611 | } |
| 3612 | return tleft; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3613 | } |
| 3614 | |
| 3615 | /* |
| 3616 | * this function disables health-check servers so that the process will quickly be ignored |
| 3617 | * by load balancers. |
| 3618 | */ |
| 3619 | static void soft_stop(void) { |
| 3620 | struct proxy *p; |
| 3621 | |
| 3622 | stopping = 1; |
| 3623 | p = proxy; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3624 | tv_now(&now); /* else, the old time before select will be used */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3625 | while (p) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 3626 | if (p->state != PR_STDISABLED) { |
| 3627 | Warning("Stopping proxy %s in %d ms.\n", p->id, p->grace); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3628 | 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] | 3629 | tv_delayfrom(&p->stop_time, &now, p->grace); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 3630 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3631 | p = p->next; |
| 3632 | } |
| 3633 | } |
| 3634 | |
| 3635 | /* |
| 3636 | * upon SIGUSR1, let's have a soft stop. |
| 3637 | */ |
| 3638 | void sig_soft_stop(int sig) { |
| 3639 | soft_stop(); |
| 3640 | signal(sig, SIG_IGN); |
| 3641 | } |
| 3642 | |
| 3643 | |
| 3644 | void dump(int sig) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3645 | struct task *t, *tnext; |
| 3646 | struct session *s; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3647 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3648 | tnext = ((struct task *)LIST_HEAD(wait_queue))->next; |
| 3649 | while ((t = tnext) != LIST_HEAD(wait_queue)) { /* we haven't looped ? */ |
| 3650 | tnext = t->next; |
| 3651 | s = t->context; |
| 3652 | qfprintf(stderr,"[dump] wq: task %p, still %ld ms, " |
| 3653 | "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, " |
| 3654 | "req=%d, rep=%d, clifd=%d\n", |
| 3655 | s, tv_remain(&now, &t->expire), |
| 3656 | s->cli_state, |
| 3657 | s->srv_state, |
| 3658 | FD_ISSET(s->cli_fd, StaticReadEvent), |
| 3659 | FD_ISSET(s->cli_fd, StaticWriteEvent), |
| 3660 | FD_ISSET(s->srv_fd, StaticReadEvent), |
| 3661 | FD_ISSET(s->srv_fd, StaticWriteEvent), |
| 3662 | s->req->l, s->rep?s->rep->l:0, s->cli_fd |
| 3663 | ); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3664 | } |
| 3665 | } |
| 3666 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3667 | void chain_regex(struct hdr_exp **head, regex_t *preg, int action, char *replace) { |
| 3668 | struct hdr_exp *exp; |
| 3669 | |
| 3670 | while (*head != NULL) |
| 3671 | head = &(*head)->next; |
| 3672 | |
| 3673 | exp = calloc(1, sizeof(struct hdr_exp)); |
| 3674 | |
| 3675 | exp->preg = preg; |
| 3676 | exp->replace = replace; |
| 3677 | exp->action = action; |
| 3678 | *head = exp; |
| 3679 | } |
| 3680 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3681 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3682 | /* |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3683 | * 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] | 3684 | */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3685 | int cfg_parse_global(char *file, int linenum, char **args) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3686 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3687 | if (!strcmp(args[0], "global")) { /* new section */ |
| 3688 | /* no option, nothing special to do */ |
| 3689 | return 0; |
| 3690 | } |
| 3691 | else if (!strcmp(args[0], "daemon")) { |
| 3692 | global.mode |= MODE_DAEMON; |
| 3693 | } |
| 3694 | else if (!strcmp(args[0], "debug")) { |
| 3695 | global.mode |= MODE_DEBUG; |
| 3696 | } |
| 3697 | else if (!strcmp(args[0], "quiet")) { |
| 3698 | global.mode |= MODE_QUIET; |
| 3699 | } |
| 3700 | else if (!strcmp(args[0], "stats")) { |
| 3701 | global.mode |= MODE_STATS; |
| 3702 | } |
| 3703 | else if (!strcmp(args[0], "uid")) { |
| 3704 | if (global.uid != 0) { |
| 3705 | Alert("parsing [%s:%d] : <uid> already specified. Continuing.\n", file, linenum); |
| 3706 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3707 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3708 | if (*(args[1]) == 0) { |
| 3709 | Alert("parsing [%s:%d] : <uid> expects an integer argument.\n", file, linenum); |
| 3710 | return -1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3711 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3712 | global.uid = atol(args[1]); |
| 3713 | } |
| 3714 | else if (!strcmp(args[0], "gid")) { |
| 3715 | if (global.gid != 0) { |
| 3716 | Alert("parsing [%s:%d] : <gid> already specified. Continuing.\n", file, linenum); |
| 3717 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3718 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3719 | if (*(args[1]) == 0) { |
| 3720 | Alert("parsing [%s:%d] : <gid> expects an integer argument.\n", file, linenum); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3721 | return -1; |
| 3722 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3723 | global.gid = atol(args[1]); |
| 3724 | } |
| 3725 | else if (!strcmp(args[0], "nbproc")) { |
| 3726 | if (global.nbproc != 0) { |
| 3727 | Alert("parsing [%s:%d] : <nbproc> already specified. Continuing.\n", file, linenum); |
| 3728 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3729 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3730 | if (*(args[1]) == 0) { |
| 3731 | Alert("parsing [%s:%d] : <gid> expects an integer argument.\n", file, linenum); |
| 3732 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3733 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3734 | global.nbproc = atol(args[1]); |
| 3735 | } |
| 3736 | else if (!strcmp(args[0], "maxconn")) { |
| 3737 | if (global.maxconn != 0) { |
| 3738 | Alert("parsing [%s:%d] : <maxconn> already specified. Continuing.\n", file, linenum); |
| 3739 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3740 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3741 | if (*(args[1]) == 0) { |
| 3742 | Alert("parsing [%s:%d] : <maxconn> expects an integer argument.\n", file, linenum); |
| 3743 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3744 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3745 | global.maxconn = atol(args[1]); |
| 3746 | } |
| 3747 | else if (!strcmp(args[0], "chroot")) { |
| 3748 | if (global.chroot != NULL) { |
| 3749 | Alert("parsing [%s:%d] : <chroot> already specified. Continuing.\n", file, linenum); |
| 3750 | return 0; |
| 3751 | } |
| 3752 | if (*(args[1]) == 0) { |
| 3753 | Alert("parsing [%s:%d] : <chroot> expects a directory as an argument.\n", file, linenum); |
| 3754 | return -1; |
| 3755 | } |
| 3756 | global.chroot = strdup(args[1]); |
| 3757 | } |
| 3758 | else if (!strcmp(args[0], "log")) { /* syslog server address */ |
| 3759 | struct sockaddr_in *sa; |
| 3760 | int facility; |
| 3761 | |
| 3762 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 3763 | Alert("parsing [%s:%d] : <log> expects <address> and <facility> as arguments.\n", file, linenum); |
| 3764 | return -1; |
| 3765 | } |
| 3766 | |
| 3767 | for (facility = 0; facility < NB_LOG_FACILITIES; facility++) |
| 3768 | if (!strcmp(log_facilities[facility], args[2])) |
| 3769 | break; |
| 3770 | |
| 3771 | if (facility >= NB_LOG_FACILITIES) { |
| 3772 | Alert("parsing [%s:%d] : unknown log facility <%s>\n", file, linenum, args[2]); |
| 3773 | exit(1); |
| 3774 | } |
| 3775 | |
| 3776 | sa = str2sa(args[1]); |
| 3777 | if (!sa->sin_port) |
| 3778 | sa->sin_port = htons(SYSLOG_PORT); |
| 3779 | |
| 3780 | if (global.logfac1 == -1) { |
| 3781 | global.logsrv1 = *sa; |
| 3782 | global.logfac1 = facility; |
| 3783 | } |
| 3784 | else if (global.logfac2 == -1) { |
| 3785 | global.logsrv2 = *sa; |
| 3786 | global.logfac2 = facility; |
| 3787 | } |
| 3788 | else { |
| 3789 | Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum); |
| 3790 | return -1; |
| 3791 | } |
| 3792 | |
| 3793 | } |
| 3794 | else { |
| 3795 | Alert("parsing [%s:%d] : unknown keyword <%s> in <global> section\n", file, linenum, args[0]); |
| 3796 | return -1; |
| 3797 | } |
| 3798 | return 0; |
| 3799 | } |
| 3800 | |
| 3801 | |
| 3802 | /* |
| 3803 | * parse a line in a <listen> section. Returns 0 if OK, -1 if error. |
| 3804 | */ |
| 3805 | int cfg_parse_listen(char *file, int linenum, char **args) { |
| 3806 | static struct proxy *curproxy = NULL; |
| 3807 | struct server *newsrv = NULL; |
| 3808 | |
| 3809 | if (!strcmp(args[0], "listen")) { /* new proxy */ |
| 3810 | if (strchr(args[2], ':') == NULL) { |
| 3811 | Alert("parsing [%s:%d] : <listen> expects <id> and <addr:port> as arguments.\n", |
| 3812 | file, linenum); |
| 3813 | return -1; |
| 3814 | } |
| 3815 | |
| 3816 | if ((curproxy = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) { |
| 3817 | Alert("parsing [%s:%d] : out of memory\n", file, linenum); |
| 3818 | return -1; |
| 3819 | } |
| 3820 | curproxy->next = proxy; |
| 3821 | proxy = curproxy; |
| 3822 | curproxy->id = strdup(args[1]); |
| 3823 | curproxy->listen_addr = *str2sa(args[2]); |
| 3824 | curproxy->state = PR_STNEW; |
| 3825 | /* set default values */ |
| 3826 | curproxy->maxconn = cfg_maxpconn; |
| 3827 | curproxy->conn_retries = CONN_RETRIES; |
| 3828 | curproxy->options = 0; |
| 3829 | curproxy->clitimeout = curproxy->contimeout = curproxy->srvtimeout = 0; |
| 3830 | curproxy->mode = PR_MODE_TCP; |
| 3831 | curproxy->logfac1 = curproxy->logfac2 = -1; /* log disabled */ |
| 3832 | curproxy->to_log = 0; |
| 3833 | return 0; |
| 3834 | } |
| 3835 | else if (curproxy == NULL) { |
| 3836 | Alert("parsing [%s:%d] : <listen> expected.\n", file, linenum); |
| 3837 | return -1; |
| 3838 | } |
| 3839 | |
| 3840 | if (!strcmp(args[0], "mode")) { /* sets the proxy mode */ |
| 3841 | if (!strcmp(args[1], "http")) curproxy->mode = PR_MODE_HTTP; |
| 3842 | else if (!strcmp(args[1], "tcp")) curproxy->mode = PR_MODE_TCP; |
| 3843 | else if (!strcmp(args[1], "health")) curproxy->mode = PR_MODE_HEALTH; |
| 3844 | else { |
| 3845 | Alert("parsing [%s:%d] : unknown proxy mode <%s>.\n", file, linenum, args[1]); |
| 3846 | return -1; |
| 3847 | } |
| 3848 | } |
| 3849 | else if (!strcmp(args[0], "disabled")) { /* disables this proxy */ |
| 3850 | curproxy->state = PR_STDISABLED; |
| 3851 | } |
| 3852 | else if (!strcmp(args[0], "cookie")) { /* cookie name */ |
| 3853 | int cur_arg; |
| 3854 | if (curproxy->cookie_name != NULL) { |
| 3855 | Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n", |
| 3856 | file, linenum); |
| 3857 | return 0; |
| 3858 | } |
| 3859 | |
| 3860 | if (*(args[1]) == 0) { |
| 3861 | Alert("parsing [%s:%d] : <cookie> expects <cookie_name> as argument.\n", |
| 3862 | file, linenum); |
| 3863 | return -1; |
| 3864 | } |
| 3865 | curproxy->cookie_name = strdup(args[1]); |
| 3866 | |
| 3867 | cur_arg = 2; |
| 3868 | while (*(args[cur_arg])) { |
| 3869 | if (!strcmp(args[cur_arg], "rewrite")) { |
| 3870 | curproxy->options |= PR_O_COOK_RW; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3871 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3872 | else if (!strcmp(args[cur_arg], "indirect")) { |
| 3873 | curproxy->options |= PR_O_COOK_IND; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3874 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3875 | else if (!strcmp(args[cur_arg], "insert")) { |
| 3876 | curproxy->options |= PR_O_COOK_INS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3877 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3878 | else if (!strcmp(args[cur_arg], "nocache")) { |
| 3879 | curproxy->options |= PR_O_COOK_NOC; |
| 3880 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3881 | else { |
| 3882 | Alert("parsing [%s:%d] : <cookie> supports 'rewrite', 'insert' and 'indirect' options.\n", |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3883 | file, linenum); |
| 3884 | return -1; |
| 3885 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3886 | cur_arg++; |
| 3887 | } |
| 3888 | if ((curproxy->options & (PR_O_COOK_RW|PR_O_COOK_IND)) == (PR_O_COOK_RW|PR_O_COOK_IND)) { |
| 3889 | Alert("parsing [%s:%d] : <cookie> 'rewrite' and 'indirect' mode are incompatibles.\n", |
| 3890 | file, linenum); |
| 3891 | return -1; |
| 3892 | } |
| 3893 | } |
| 3894 | else if (!strcmp(args[0], "contimeout")) { /* connect timeout */ |
| 3895 | if (curproxy->contimeout != 0) { |
| 3896 | Alert("parsing [%s:%d] : contimeout already specified. Continuing.\n", file, linenum); |
| 3897 | return 0; |
| 3898 | } |
| 3899 | if (*(args[1]) == 0) { |
| 3900 | Alert("parsing [%s:%d] : <contimeout> expects an integer <time_in_ms> as argument.\n", |
| 3901 | file, linenum); |
| 3902 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3903 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3904 | curproxy->contimeout = atol(args[1]); |
| 3905 | } |
| 3906 | else if (!strcmp(args[0], "clitimeout")) { /* client timeout */ |
| 3907 | if (curproxy->clitimeout != 0) { |
| 3908 | Alert("parsing [%s:%d] : clitimeout already specified. Continuing.\n", |
| 3909 | file, linenum); |
| 3910 | return 0; |
| 3911 | } |
| 3912 | if (*(args[1]) == 0) { |
| 3913 | Alert("parsing [%s:%d] : <clitimeout> expects an integer <time_in_ms> as argument.\n", |
| 3914 | file, linenum); |
| 3915 | return -1; |
| 3916 | } |
| 3917 | curproxy->clitimeout = atol(args[1]); |
| 3918 | } |
| 3919 | else if (!strcmp(args[0], "srvtimeout")) { /* server timeout */ |
| 3920 | if (curproxy->srvtimeout != 0) { |
| 3921 | Alert("parsing [%s:%d] : srvtimeout already specified. Continuing.\n", file, linenum); |
| 3922 | return 0; |
| 3923 | } |
| 3924 | if (*(args[1]) == 0) { |
| 3925 | Alert("parsing [%s:%d] : <srvtimeout> expects an integer <time_in_ms> as argument.\n", |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3926 | file, linenum); |
| 3927 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3928 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3929 | curproxy->srvtimeout = atol(args[1]); |
| 3930 | } |
| 3931 | else if (!strcmp(args[0], "retries")) { /* connection retries */ |
| 3932 | if (*(args[1]) == 0) { |
| 3933 | Alert("parsing [%s:%d] : <retries> expects an integer argument (dispatch counts for one).\n", |
| 3934 | file, linenum); |
| 3935 | return -1; |
| 3936 | } |
| 3937 | curproxy->conn_retries = atol(args[1]); |
| 3938 | } |
| 3939 | else if (!strcmp(args[0], "option")) { |
| 3940 | if (*(args[1]) == 0) { |
| 3941 | Alert("parsing [%s:%d] : <option> expects an option name.\n", file, linenum); |
| 3942 | return -1; |
| 3943 | } |
| 3944 | if (!strcmp(args[1], "redispatch")) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3945 | /* enable reconnections to dispatch */ |
| 3946 | curproxy->options |= PR_O_REDISP; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3947 | #ifdef TPROXY |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3948 | else if (!strcmp(args[1], "transparent")) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3949 | /* enable transparent proxy connections */ |
| 3950 | curproxy->options |= PR_O_TRANSP; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3951 | #endif |
| 3952 | else if (!strcmp(args[1], "keepalive")) |
| 3953 | /* enable keep-alive */ |
| 3954 | curproxy->options |= PR_O_KEEPALIVE; |
| 3955 | else if (!strcmp(args[1], "forwardfor")) |
| 3956 | /* insert x-forwarded-for field */ |
| 3957 | curproxy->options |= PR_O_FWDFOR; |
| 3958 | else if (!strcmp(args[1], "httplog")) { |
| 3959 | /* generate a complete HTTP log */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3960 | curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_REQ | LW_PXID | LW_RESP; |
| 3961 | } |
| 3962 | else if (!strcmp(args[1], "dontlognull")) { |
| 3963 | /* don't log empty requests */ |
| 3964 | curproxy->options |= PR_O_NULLNOLOG; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3965 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3966 | else { |
| 3967 | Alert("parsing [%s:%d] : unknown option <%s>.\n", file, linenum, args[1]); |
| 3968 | return -1; |
| 3969 | } |
| 3970 | return 0; |
| 3971 | } |
| 3972 | else if (!strcmp(args[0], "redispatch") || !strcmp(args[0], "redisp")) { |
| 3973 | /* enable reconnections to dispatch */ |
| 3974 | curproxy->options |= PR_O_REDISP; |
| 3975 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3976 | #ifdef TPROXY |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3977 | else if (!strcmp(args[0], "transparent")) { |
| 3978 | /* enable transparent proxy connections */ |
| 3979 | curproxy->options |= PR_O_TRANSP; |
| 3980 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3981 | #endif |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3982 | else if (!strcmp(args[0], "maxconn")) { /* maxconn */ |
| 3983 | if (*(args[1]) == 0) { |
| 3984 | Alert("parsing [%s:%d] : <maxconn> expects an integer argument.\n", file, linenum); |
| 3985 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3986 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3987 | curproxy->maxconn = atol(args[1]); |
| 3988 | } |
| 3989 | else if (!strcmp(args[0], "grace")) { /* grace time (ms) */ |
| 3990 | if (*(args[1]) == 0) { |
| 3991 | Alert("parsing [%s:%d] : <grace> expects a time in milliseconds.\n", file, linenum); |
| 3992 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3993 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3994 | curproxy->grace = atol(args[1]); |
| 3995 | } |
| 3996 | else if (!strcmp(args[0], "dispatch")) { /* dispatch address */ |
| 3997 | if (strchr(args[1], ':') == NULL) { |
| 3998 | Alert("parsing [%s:%d] : <dispatch> expects <addr:port> as argument.\n", file, linenum); |
| 3999 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4000 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4001 | curproxy->dispatch_addr = *str2sa(args[1]); |
| 4002 | } |
| 4003 | else if (!strcmp(args[0], "balance")) { /* set balancing with optionnal algorithm */ |
| 4004 | if (*(args[1])) { |
| 4005 | if (!strcmp(args[1], "roundrobin")) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4006 | curproxy->options |= PR_O_BALANCE_RR; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4007 | } |
| 4008 | else { |
| 4009 | Alert("parsing [%s:%d] : <balance> supports 'roundrobin' options.\n", file, linenum); |
| 4010 | return -1; |
| 4011 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4012 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4013 | else /* if no option is set, use round-robin by default */ |
| 4014 | curproxy->options |= PR_O_BALANCE_RR; |
| 4015 | } |
| 4016 | else if (!strcmp(args[0], "server")) { /* server address */ |
| 4017 | int cur_arg; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4018 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4019 | if (strchr(args[2], ':') == NULL) { |
| 4020 | Alert("parsing [%s:%d] : <server> expects <name> and <addr:port> as arguments.\n", |
| 4021 | file, linenum); |
| 4022 | return -1; |
| 4023 | } |
| 4024 | if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) { |
| 4025 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 4026 | return -1; |
| 4027 | } |
| 4028 | newsrv->next = curproxy->srv; |
| 4029 | curproxy->srv = newsrv; |
| 4030 | newsrv->proxy = curproxy; |
| 4031 | newsrv->id = strdup(args[1]); |
| 4032 | newsrv->addr = *str2sa(args[2]); |
| 4033 | newsrv->state = SRV_RUNNING; /* early server setup */ |
| 4034 | newsrv->curfd = -1; /* no health-check in progress */ |
| 4035 | newsrv->inter = DEF_CHKINTR; |
| 4036 | newsrv->rise = DEF_RISETIME; |
| 4037 | newsrv->fall = DEF_FALLTIME; |
| 4038 | newsrv->health = newsrv->rise; /* up, but will fall down at first failure */ |
| 4039 | cur_arg = 3; |
| 4040 | while (*args[cur_arg]) { |
| 4041 | if (!strcmp(args[cur_arg], "cookie")) { |
| 4042 | newsrv->cookie = strdup(args[cur_arg + 1]); |
| 4043 | newsrv->cklen = strlen(args[cur_arg + 1]); |
| 4044 | cur_arg += 2; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4045 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4046 | else if (!strcmp(args[cur_arg], "rise")) { |
| 4047 | newsrv->rise = atol(args[cur_arg + 1]); |
| 4048 | newsrv->health = newsrv->rise; |
| 4049 | cur_arg += 2; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4050 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4051 | else if (!strcmp(args[cur_arg], "fall")) { |
| 4052 | newsrv->fall = atol(args[cur_arg + 1]); |
| 4053 | cur_arg += 2; |
| 4054 | } |
| 4055 | else if (!strcmp(args[cur_arg], "inter")) { |
| 4056 | newsrv->inter = atol(args[cur_arg + 1]); |
| 4057 | cur_arg += 2; |
| 4058 | } |
| 4059 | else if (!strcmp(args[cur_arg], "check")) { |
| 4060 | struct task *t; |
| 4061 | |
| 4062 | if ((t = pool_alloc(task)) == NULL) { /* disable this proxy for a while */ |
| 4063 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4064 | return -1; |
| 4065 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4066 | |
| 4067 | t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */ |
| 4068 | t->wq = LIST_HEAD(wait_queue); /* but already has a wait queue assigned */ |
| 4069 | t->state = TASK_IDLE; |
| 4070 | t->process = process_chk; |
| 4071 | t->context = newsrv; |
| 4072 | |
| 4073 | if (curproxy->state != PR_STDISABLED) { |
| 4074 | tv_delayfrom(&t->expire, &now, newsrv->inter); /* check this every ms */ |
| 4075 | task_queue(t); |
| 4076 | task_wakeup(&rq, t); |
| 4077 | } |
| 4078 | |
| 4079 | cur_arg += 1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4080 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4081 | else { |
| 4082 | Alert("parsing [%s:%d] : server %s only supports options 'cookie' and 'check'.\n", |
| 4083 | file, linenum, newsrv->id); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4084 | return -1; |
| 4085 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4086 | } |
| 4087 | curproxy->nbservers++; |
| 4088 | } |
| 4089 | else if (!strcmp(args[0], "log")) { /* syslog server address */ |
| 4090 | struct sockaddr_in *sa; |
| 4091 | int facility; |
| 4092 | |
| 4093 | if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) { |
| 4094 | curproxy->logfac1 = global.logfac1; |
| 4095 | curproxy->logsrv1 = global.logsrv1; |
| 4096 | curproxy->logfac2 = global.logfac2; |
| 4097 | curproxy->logsrv2 = global.logsrv2; |
| 4098 | } |
| 4099 | else if (*(args[1]) && *(args[2])) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4100 | for (facility = 0; facility < NB_LOG_FACILITIES; facility++) |
| 4101 | if (!strcmp(log_facilities[facility], args[2])) |
| 4102 | break; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4103 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4104 | if (facility >= NB_LOG_FACILITIES) { |
| 4105 | Alert("parsing [%s:%d] : unknown log facility <%s>\n", file, linenum, args[2]); |
| 4106 | exit(1); |
| 4107 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4108 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4109 | sa = str2sa(args[1]); |
| 4110 | if (!sa->sin_port) |
| 4111 | sa->sin_port = htons(SYSLOG_PORT); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4112 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4113 | if (curproxy->logfac1 == -1) { |
| 4114 | curproxy->logsrv1 = *sa; |
| 4115 | curproxy->logfac1 = facility; |
| 4116 | } |
| 4117 | else if (curproxy->logfac2 == -1) { |
| 4118 | curproxy->logsrv2 = *sa; |
| 4119 | curproxy->logfac2 = facility; |
| 4120 | } |
| 4121 | else { |
| 4122 | Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4123 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4124 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4125 | } |
| 4126 | else { |
| 4127 | Alert("parsing [%s:%d] : <log> expects either <address[:port]> and <facility> or 'global' as arguments.\n", |
| 4128 | file, linenum); |
| 4129 | return -1; |
| 4130 | } |
| 4131 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4132 | else if (!strcmp(args[0], "source")) { /* address to which we bind when connecting */ |
| 4133 | if (strchr(args[1], ':') == NULL) { |
| 4134 | Alert("parsing [%s:%d] : <source> expects <addr:port> as argument.\n", |
| 4135 | file, linenum); |
| 4136 | return -1; |
| 4137 | } |
| 4138 | |
| 4139 | curproxy->source_addr = *str2sa(args[1]); |
| 4140 | curproxy->options |= PR_O_BIND_SRC; |
| 4141 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4142 | else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */ |
| 4143 | regex_t *preg; |
| 4144 | |
| 4145 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 4146 | Alert("parsing [%s:%d] : <reqrep> expects <search> and <replace> as arguments.\n", |
| 4147 | file, linenum); |
| 4148 | return -1; |
| 4149 | } |
| 4150 | |
| 4151 | preg = calloc(1, sizeof(regex_t)); |
| 4152 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 4153 | Alert("parsing [%s:%d] : bad regular expression <%s>.\n", file, linenum, args[1]); |
| 4154 | return -1; |
| 4155 | } |
| 4156 | |
| 4157 | chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 4158 | } |
| 4159 | else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */ |
| 4160 | regex_t *preg; |
| 4161 | |
| 4162 | if (*(args[1]) == 0) { |
| 4163 | Alert("parsing [%s:%d] : <reqdel> expects <regex> as an argument.\n", file, linenum); |
| 4164 | return -1; |
| 4165 | } |
| 4166 | |
| 4167 | preg = calloc(1, sizeof(regex_t)); |
| 4168 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 4169 | Alert("parsing [%s:%d] : bad regular expression <%s>.\n", file, linenum, args[1]); |
| 4170 | return -1; |
| 4171 | } |
| 4172 | |
| 4173 | chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL); |
| 4174 | } |
| 4175 | else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */ |
| 4176 | regex_t *preg; |
| 4177 | |
| 4178 | if (*(args[1]) == 0) { |
| 4179 | Alert("parsing [%s:%d] : <reqdeny> expects <regex> as an argument.\n", file, linenum); |
| 4180 | return -1; |
| 4181 | } |
| 4182 | |
| 4183 | preg = calloc(1, sizeof(regex_t)); |
| 4184 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 4185 | Alert("parsing [%s:%d] : bad regular expression <%s>.\n", file, linenum, args[1]); |
| 4186 | return -1; |
| 4187 | } |
| 4188 | |
| 4189 | chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL); |
| 4190 | } |
| 4191 | else if (!strcmp(args[0], "reqallow")) { /* allow a request if a header matches this regex */ |
| 4192 | regex_t *preg; |
| 4193 | |
| 4194 | if (*(args[1]) == 0) { |
| 4195 | Alert("parsing [%s:%d] : <reqallow> expects <regex> as an argument.\n", file, linenum); |
| 4196 | return -1; |
| 4197 | } |
| 4198 | |
| 4199 | preg = calloc(1, sizeof(regex_t)); |
| 4200 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 4201 | Alert("parsing [%s:%d] : bad regular expression <%s>.\n", file, linenum, args[1]); |
| 4202 | return -1; |
| 4203 | } |
| 4204 | |
| 4205 | chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL); |
| 4206 | } |
| 4207 | else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */ |
| 4208 | regex_t *preg; |
| 4209 | |
| 4210 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 4211 | Alert("parsing [%s:%d] : <reqirep> expects <search> and <replace> as arguments.\n", |
| 4212 | file, linenum); |
| 4213 | return -1; |
| 4214 | } |
| 4215 | |
| 4216 | preg = calloc(1, sizeof(regex_t)); |
| 4217 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 4218 | Alert("parsing [%s:%d] : bad regular expression <%s>.\n", file, linenum, args[1]); |
| 4219 | return -1; |
| 4220 | } |
| 4221 | |
| 4222 | chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 4223 | } |
| 4224 | else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */ |
| 4225 | regex_t *preg; |
| 4226 | |
| 4227 | if (*(args[1]) == 0) { |
| 4228 | Alert("parsing [%s:%d] : <reqidel> expects <regex> as an argument.\n", file, linenum); |
| 4229 | return -1; |
| 4230 | } |
| 4231 | |
| 4232 | preg = calloc(1, sizeof(regex_t)); |
| 4233 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 4234 | Alert("parsing [%s:%d] : bad regular expression <%s>.\n", file, linenum, args[1]); |
| 4235 | return -1; |
| 4236 | } |
| 4237 | |
| 4238 | chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL); |
| 4239 | } |
| 4240 | else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */ |
| 4241 | regex_t *preg; |
| 4242 | |
| 4243 | if (*(args[1]) == 0) { |
| 4244 | Alert("parsing [%s:%d] : <reqideny> expects <regex> as an argument.\n", file, linenum); |
| 4245 | return -1; |
| 4246 | } |
| 4247 | |
| 4248 | preg = calloc(1, sizeof(regex_t)); |
| 4249 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 4250 | Alert("parsing [%s:%d] : bad regular expression <%s>.\n", file, linenum, args[1]); |
| 4251 | return -1; |
| 4252 | } |
| 4253 | |
| 4254 | chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL); |
| 4255 | } |
| 4256 | else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */ |
| 4257 | regex_t *preg; |
| 4258 | |
| 4259 | if (*(args[1]) == 0) { |
| 4260 | Alert("parsing [%s:%d] : <reqiallow> expects <regex> as an argument.\n", file, linenum); |
| 4261 | return -1; |
| 4262 | } |
| 4263 | |
| 4264 | preg = calloc(1, sizeof(regex_t)); |
| 4265 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 4266 | Alert("parsing [%s:%d] : bad regular expression <%s>.\n", file, linenum, args[1]); |
| 4267 | return -1; |
| 4268 | } |
| 4269 | |
| 4270 | chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL); |
| 4271 | } |
| 4272 | else if (!strcmp(args[0], "reqadd")) { /* add request header */ |
| 4273 | if (curproxy->nb_reqadd >= MAX_NEWHDR) { |
| 4274 | Alert("parsing [%s:%d] : too many `reqadd'. Continuing.\n", file, linenum); |
| 4275 | return 0; |
| 4276 | } |
| 4277 | |
| 4278 | if (*(args[1]) == 0) { |
| 4279 | Alert("parsing [%s:%d] : <reqadd> expects <header> as an argument.\n", file, linenum); |
| 4280 | return -1; |
| 4281 | } |
| 4282 | |
| 4283 | curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4284 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4285 | else if (!strcmp(args[0], "srvexp") || !strcmp(args[0], "rsprep")) { /* replace response header from a regex */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4286 | regex_t *preg; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4287 | |
| 4288 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4289 | Alert("parsing [%s:%d] : <rsprep> expects <search> and <replace> as arguments.\n", |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4290 | file, linenum); |
| 4291 | return -1; |
| 4292 | } |
| 4293 | |
| 4294 | preg = calloc(1, sizeof(regex_t)); |
| 4295 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 4296 | Alert("parsing [%s:%d] : bad regular expression <%s>.\n", file, linenum, args[1]); |
| 4297 | return -1; |
| 4298 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4299 | |
| 4300 | chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 4301 | } |
| 4302 | else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */ |
| 4303 | regex_t *preg; |
| 4304 | |
| 4305 | if (*(args[1]) == 0) { |
| 4306 | Alert("parsing [%s:%d] : <rspdel> expects <search> as an argument.\n", file, linenum); |
| 4307 | return -1; |
| 4308 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4309 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4310 | preg = calloc(1, sizeof(regex_t)); |
| 4311 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 4312 | Alert("parsing [%s:%d] : bad regular expression <%s>.\n", file, linenum, args[1]); |
| 4313 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4314 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4315 | |
| 4316 | chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2])); |
| 4317 | } |
| 4318 | else if (!strcmp(args[0], "rspirep")) { /* replace response header from a regex ignoring case */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4319 | regex_t *preg; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4320 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4321 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
| 4322 | Alert("parsing [%s:%d] : <rspirep> expects <search> and <replace> as arguments.\n", |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4323 | file, linenum); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4324 | return -1; |
| 4325 | } |
| 4326 | |
| 4327 | preg = calloc(1, sizeof(regex_t)); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4328 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4329 | Alert("parsing [%s:%d] : bad regular expression <%s>.\n", file, linenum, args[1]); |
| 4330 | return -1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4331 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4332 | |
| 4333 | chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 4334 | } |
| 4335 | else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */ |
| 4336 | regex_t *preg; |
| 4337 | |
| 4338 | if (*(args[1]) == 0) { |
| 4339 | Alert("parsing [%s:%d] : <rspidel> expects <search> as an argument.\n", file, linenum); |
| 4340 | return -1; |
| 4341 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4342 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4343 | preg = calloc(1, sizeof(regex_t)); |
| 4344 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 4345 | Alert("parsing [%s:%d] : bad regular expression <%s>.\n", file, linenum, args[1]); |
| 4346 | return -1; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4347 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4348 | |
| 4349 | chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2])); |
| 4350 | } |
| 4351 | else if (!strcmp(args[0], "rspadd")) { /* add response header */ |
| 4352 | if (curproxy->nb_rspadd >= MAX_NEWHDR) { |
| 4353 | Alert("parsing [%s:%d] : too many `rspadd'. Continuing.\n", file, linenum); |
| 4354 | return 0; |
| 4355 | } |
| 4356 | |
| 4357 | if (*(args[1]) == 0) { |
| 4358 | Alert("parsing [%s:%d] : <rspadd> expects <header> as an argument.\n", file, linenum); |
| 4359 | return -1; |
| 4360 | } |
| 4361 | |
| 4362 | curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]); |
| 4363 | } |
| 4364 | else { |
| 4365 | Alert("parsing [%s:%d] : unknown keyword <%s> in <listen> section\n", file, linenum, args[0]); |
| 4366 | return -1; |
| 4367 | } |
| 4368 | return 0; |
| 4369 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4370 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4371 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4372 | /* |
| 4373 | * This function reads and parses the configuration file given in the argument. |
| 4374 | * returns 0 if OK, -1 if error. |
| 4375 | */ |
| 4376 | int readcfgfile(char *file) { |
| 4377 | char thisline[256]; |
| 4378 | char *line; |
| 4379 | FILE *f; |
| 4380 | int linenum = 0; |
| 4381 | char *end; |
| 4382 | char *args[MAX_LINE_ARGS]; |
| 4383 | int arg; |
| 4384 | int cfgerr = 0; |
| 4385 | int confsect = CFG_NONE; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4386 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4387 | struct proxy *curproxy = NULL; |
| 4388 | struct server *newsrv = NULL; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4389 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4390 | if ((f=fopen(file,"r")) == NULL) |
| 4391 | return -1; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4392 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4393 | while (fgets(line = thisline, sizeof(thisline), f) != NULL) { |
| 4394 | linenum++; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4395 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4396 | end = line + strlen(line); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4397 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4398 | /* skip leading spaces */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 4399 | while (isspace((int)*line)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4400 | line++; |
| 4401 | |
| 4402 | arg = 0; |
| 4403 | args[arg] = line; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4404 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4405 | while (*line && arg < MAX_LINE_ARGS) { |
| 4406 | /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their |
| 4407 | * C equivalent value. Other combinations left unchanged (eg: \1). |
| 4408 | */ |
| 4409 | if (*line == '\\') { |
| 4410 | int skip = 0; |
| 4411 | if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') { |
| 4412 | *line = line[1]; |
| 4413 | skip = 1; |
| 4414 | } |
| 4415 | else if (line[1] == 'r') { |
| 4416 | *line = '\r'; |
| 4417 | skip = 1; |
| 4418 | } |
| 4419 | else if (line[1] == 'n') { |
| 4420 | *line = '\n'; |
| 4421 | skip = 1; |
| 4422 | } |
| 4423 | else if (line[1] == 't') { |
| 4424 | *line = '\t'; |
| 4425 | skip = 1; |
| 4426 | } |
| 4427 | else if (line[1] == 'x' && (line + 3 < end )) { |
| 4428 | unsigned char hex1, hex2; |
| 4429 | hex1 = toupper(line[2]) - '0'; hex2 = toupper(line[3]) - '0'; |
| 4430 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 4431 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 4432 | *line = (hex1<<4) + hex2; |
| 4433 | skip = 3; |
| 4434 | } |
| 4435 | if (skip) { |
| 4436 | memmove(line + 1, line + 1 + skip, end - (line + skip + 1)); |
| 4437 | end -= skip; |
| 4438 | } |
| 4439 | line++; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4440 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4441 | else if (*line == '#' || *line == '\n' || *line == '\r') { |
| 4442 | /* end of string, end of loop */ |
| 4443 | *line = 0; |
| 4444 | break; |
| 4445 | } |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 4446 | else if (isspace((int)*line)) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4447 | /* a non-escaped space is an argument separator */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4448 | *line++ = 0; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 4449 | while (isspace((int)*line)) |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4450 | line++; |
| 4451 | args[++arg] = line; |
| 4452 | } |
| 4453 | else { |
| 4454 | line++; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4455 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4456 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4457 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4458 | /* empty line */ |
| 4459 | if (!**args) |
| 4460 | continue; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4461 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4462 | /* zero out remaining args */ |
| 4463 | while (++arg < MAX_LINE_ARGS) { |
| 4464 | args[arg] = line; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4465 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4466 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4467 | if (!strcmp(args[0], "listen")) /* new proxy */ |
| 4468 | confsect = CFG_LISTEN; |
| 4469 | else if (!strcmp(args[0], "global")) /* global config */ |
| 4470 | confsect = CFG_GLOBAL; |
| 4471 | /* else it's a section keyword */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4472 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4473 | switch (confsect) { |
| 4474 | case CFG_LISTEN: |
| 4475 | if (cfg_parse_listen(file, linenum, args) < 0) |
| 4476 | return -1; |
| 4477 | break; |
| 4478 | case CFG_GLOBAL: |
| 4479 | if (cfg_parse_global(file, linenum, args) < 0) |
| 4480 | return -1; |
| 4481 | break; |
| 4482 | default: |
| 4483 | Alert("parsing [%s:%d] : unknown keyword <%s> out of section.\n", file, linenum, args[0]); |
| 4484 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4485 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4486 | |
| 4487 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4488 | } |
| 4489 | fclose(f); |
| 4490 | |
| 4491 | /* |
| 4492 | * Now, check for the integrity of all that we have collected. |
| 4493 | */ |
| 4494 | |
| 4495 | if ((curproxy = proxy) == NULL) { |
| 4496 | Alert("parsing %s : no <listen> line. Nothing to do !\n", |
| 4497 | file); |
| 4498 | return -1; |
| 4499 | } |
| 4500 | |
| 4501 | while (curproxy != NULL) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 4502 | if (curproxy->state == PR_STDISABLED) { |
| 4503 | curproxy = curproxy->next; |
| 4504 | continue; |
| 4505 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4506 | if ((curproxy->mode != PR_MODE_HEALTH) && |
| 4507 | !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) && |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4508 | (*(int *)&curproxy->dispatch_addr.sin_addr == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4509 | Alert("parsing %s : listener %s has no dispatch address and is not in transparent or balance mode.\n", |
| 4510 | file, curproxy->id); |
| 4511 | cfgerr++; |
| 4512 | } |
| 4513 | else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) { |
| 4514 | if (curproxy->options & PR_O_TRANSP) { |
| 4515 | Alert("parsing %s : listener %s cannot use both transparent and balance mode.\n", |
| 4516 | file, curproxy->id); |
| 4517 | cfgerr++; |
| 4518 | } |
| 4519 | else if (curproxy->srv == NULL) { |
| 4520 | Alert("parsing %s : listener %s needs at least 1 server in balance mode.\n", |
| 4521 | file, curproxy->id); |
| 4522 | cfgerr++; |
| 4523 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4524 | else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4525 | Warning("parsing %s : dispatch address of listener %s will be ignored in balance mode.\n", |
| 4526 | file, curproxy->id); |
| 4527 | } |
| 4528 | } |
| 4529 | 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] | 4530 | if (curproxy->cookie_name != NULL) { |
| 4531 | Warning("parsing %s : cookie will be ignored for listener %s.\n", |
| 4532 | file, curproxy->id); |
| 4533 | } |
| 4534 | if ((newsrv = curproxy->srv) != NULL) { |
| 4535 | Warning("parsing %s : servers will be ignored for listener %s.\n", |
| 4536 | file, curproxy->id); |
| 4537 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4538 | if (curproxy->rsp_exp != NULL) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4539 | Warning("parsing %s : server regular expressions will be ignored for listener %s.\n", |
| 4540 | file, curproxy->id); |
| 4541 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4542 | if (curproxy->req_exp != NULL) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4543 | Warning("parsing %s : client regular expressions will be ignored for listener %s.\n", |
| 4544 | file, curproxy->id); |
| 4545 | } |
| 4546 | } |
| 4547 | else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */ |
| 4548 | if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) { |
| 4549 | Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n", |
| 4550 | file, curproxy->id); |
| 4551 | cfgerr++; |
| 4552 | } |
| 4553 | else { |
| 4554 | while (newsrv != NULL) { |
| 4555 | /* nothing to check for now */ |
| 4556 | newsrv = newsrv->next; |
| 4557 | } |
| 4558 | } |
| 4559 | } |
| 4560 | curproxy = curproxy->next; |
| 4561 | } |
| 4562 | if (cfgerr > 0) { |
| 4563 | Alert("Errors found in configuration file, aborting.\n"); |
| 4564 | return -1; |
| 4565 | } |
| 4566 | else |
| 4567 | return 0; |
| 4568 | } |
| 4569 | |
| 4570 | |
| 4571 | /* |
| 4572 | * This function initializes all the necessary variables. It only returns |
| 4573 | * if everything is OK. If something fails, it exits. |
| 4574 | */ |
| 4575 | void init(int argc, char **argv) { |
| 4576 | int i; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4577 | int arg_mode = 0; /* MODE_DEBUG, ... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4578 | char *old_argv = *argv; |
| 4579 | char *tmp; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4580 | int cfg_maxconn = 0; /* # of simultaneous connections, (-n) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4581 | |
| 4582 | if (1<<INTBITS != sizeof(int)*8) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4583 | qfprintf(stderr, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4584 | "Error: wrong architecture. Recompile so that sizeof(int)=%d\n", |
| 4585 | sizeof(int)*8); |
| 4586 | exit(1); |
| 4587 | } |
| 4588 | |
| 4589 | pid = getpid(); |
| 4590 | progname = *argv; |
| 4591 | while ((tmp = strchr(progname, '/')) != NULL) |
| 4592 | progname = tmp + 1; |
| 4593 | |
| 4594 | argc--; argv++; |
| 4595 | while (argc > 0) { |
| 4596 | char *flag; |
| 4597 | |
| 4598 | if (**argv == '-') { |
| 4599 | flag = *argv+1; |
| 4600 | |
| 4601 | /* 1 arg */ |
| 4602 | if (*flag == 'v') { |
| 4603 | display_version(); |
| 4604 | exit(0); |
| 4605 | } |
| 4606 | else if (*flag == 'd') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4607 | arg_mode |= MODE_DEBUG; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4608 | else if (*flag == 'D') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4609 | arg_mode |= MODE_DAEMON | MODE_QUIET; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4610 | else if (*flag == 'q') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4611 | arg_mode |= MODE_QUIET; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4612 | #if STATTIME > 0 |
| 4613 | else if (*flag == 's') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4614 | arg_mode |= MODE_STATS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4615 | else if (*flag == 'l') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4616 | arg_mode |= MODE_LOG; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4617 | #endif |
| 4618 | else { /* >=2 args */ |
| 4619 | argv++; argc--; |
| 4620 | if (argc == 0) |
| 4621 | usage(old_argv); |
| 4622 | |
| 4623 | switch (*flag) { |
| 4624 | case 'n' : cfg_maxconn = atol(*argv); break; |
| 4625 | case 'N' : cfg_maxpconn = atol(*argv); break; |
| 4626 | case 'f' : cfg_cfgfile = *argv; break; |
| 4627 | default: usage(old_argv); |
| 4628 | } |
| 4629 | } |
| 4630 | } |
| 4631 | else |
| 4632 | usage(old_argv); |
| 4633 | argv++; argc--; |
| 4634 | } |
| 4635 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4636 | if (!cfg_cfgfile) |
| 4637 | usage(old_argv); |
| 4638 | |
| 4639 | gethostname(hostname, MAX_HOSTNAME_LEN); |
| 4640 | |
| 4641 | if (readcfgfile(cfg_cfgfile) < 0) { |
| 4642 | Alert("Error reading configuration file : %s\n", cfg_cfgfile); |
| 4643 | exit(1); |
| 4644 | } |
| 4645 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4646 | if (cfg_maxconn > 0) |
| 4647 | global.maxconn = cfg_maxconn; |
| 4648 | |
| 4649 | if (global.maxconn == 0) |
| 4650 | global.maxconn = DEFAULT_MAXCONN; |
| 4651 | |
| 4652 | global.maxsock = global.maxconn * 2; /* each connection needs two sockets */ |
| 4653 | |
| 4654 | if (arg_mode & MODE_DEBUG) { |
| 4655 | /* command line debug mode inhibits configuration mode */ |
| 4656 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 4657 | } |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4658 | global.mode |= (arg_mode & (MODE_DAEMON | MODE_QUIET | MODE_DEBUG | MODE_STATS | MODE_LOG)); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4659 | |
| 4660 | if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) { |
| 4661 | Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n"); |
| 4662 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 4663 | } |
| 4664 | |
| 4665 | if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) { |
| 4666 | Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n"); |
| 4667 | global.nbproc = 1; |
| 4668 | } |
| 4669 | |
| 4670 | if (global.nbproc < 1) |
| 4671 | global.nbproc = 1; |
| 4672 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4673 | ReadEvent = (fd_set *)calloc(1, |
| 4674 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4675 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4676 | WriteEvent = (fd_set *)calloc(1, |
| 4677 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4678 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4679 | StaticReadEvent = (fd_set *)calloc(1, |
| 4680 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4681 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4682 | StaticWriteEvent = (fd_set *)calloc(1, |
| 4683 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4684 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4685 | |
| 4686 | fdtab = (struct fdtab *)calloc(1, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4687 | sizeof(struct fdtab) * (global.maxsock)); |
| 4688 | for (i = 0; i < global.maxsock; i++) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4689 | fdtab[i].state = FD_STCLOSE; |
| 4690 | } |
| 4691 | } |
| 4692 | |
| 4693 | /* |
| 4694 | * this function starts all the proxies. It returns 0 if OK, -1 if not. |
| 4695 | */ |
| 4696 | int start_proxies() { |
| 4697 | struct proxy *curproxy; |
| 4698 | int one = 1; |
| 4699 | int fd; |
| 4700 | |
| 4701 | for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) { |
| 4702 | |
| 4703 | if (curproxy->state == PR_STDISABLED) |
| 4704 | continue; |
| 4705 | |
| 4706 | if ((fd = curproxy->listen_fd = |
| 4707 | socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
| 4708 | Alert("cannot create listening socket for proxy %s. Aborting.\n", |
| 4709 | curproxy->id); |
| 4710 | return -1; |
| 4711 | } |
| 4712 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4713 | if (fd >= global.maxsock) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4714 | Alert("socket(): not enough free sockets for proxy %s. Raise -n argument. Aborting.\n", |
| 4715 | curproxy->id); |
| 4716 | close(fd); |
| 4717 | return -1; |
| 4718 | } |
| 4719 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4720 | if ((fcntl(fd, F_SETFL, O_NONBLOCK) == -1) || |
| 4721 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 4722 | (char *) &one, sizeof(one)) == -1)) { |
| 4723 | Alert("cannot make socket non-blocking for proxy %s. Aborting.\n", |
| 4724 | curproxy->id); |
| 4725 | close(fd); |
| 4726 | return -1; |
| 4727 | } |
| 4728 | |
| 4729 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) { |
| 4730 | Alert("cannot do so_reuseaddr for proxy %s. Continuing.\n", |
| 4731 | curproxy->id); |
| 4732 | } |
| 4733 | |
| 4734 | if (bind(fd, |
| 4735 | (struct sockaddr *)&curproxy->listen_addr, |
| 4736 | sizeof(curproxy->listen_addr)) == -1) { |
| 4737 | Alert("cannot bind socket for proxy %s. Aborting.\n", |
| 4738 | curproxy->id); |
| 4739 | close(fd); |
| 4740 | return -1; |
| 4741 | } |
| 4742 | |
| 4743 | if (listen(fd, curproxy->maxconn) == -1) { |
| 4744 | Alert("cannot listen to socket for proxy %s. Aborting.\n", |
| 4745 | curproxy->id); |
| 4746 | close(fd); |
| 4747 | return -1; |
| 4748 | } |
| 4749 | |
| 4750 | /* the function for the accept() event */ |
| 4751 | fdtab[fd].read = &event_accept; |
| 4752 | fdtab[fd].write = NULL; /* never called */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4753 | fdtab[fd].owner = (struct task *)curproxy; /* reference the proxy instead of a task */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4754 | curproxy->state = PR_STRUN; |
| 4755 | fdtab[fd].state = FD_STLISTEN; |
| 4756 | FD_SET(fd, StaticReadEvent); |
| 4757 | fd_insert(fd); |
| 4758 | listeners++; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4759 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4760 | send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 4761 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4762 | } |
| 4763 | return 0; |
| 4764 | } |
| 4765 | |
| 4766 | |
| 4767 | int main(int argc, char **argv) { |
| 4768 | init(argc, argv); |
| 4769 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4770 | if (global.mode & MODE_QUIET) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4771 | /* detach from the tty */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4772 | fclose(stdin); fclose(stdout); fclose(stderr); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4773 | close(0); close(1); close(2); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4774 | } |
| 4775 | |
| 4776 | signal(SIGQUIT, dump); |
| 4777 | signal(SIGUSR1, sig_soft_stop); |
| 4778 | |
| 4779 | /* on very high loads, a sigpipe sometimes happen just between the |
| 4780 | * getsockopt() which tells "it's OK to write", and the following write :-( |
| 4781 | */ |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 4782 | #ifndef MSG_NOSIGNAL |
| 4783 | signal(SIGPIPE, SIG_IGN); |
| 4784 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4785 | |
| 4786 | if (start_proxies() < 0) |
| 4787 | exit(1); |
| 4788 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4789 | /* open log files */ |
| 4790 | |
| 4791 | /* chroot if needed */ |
| 4792 | if (global.chroot != NULL) { |
| 4793 | if (chroot(global.chroot) == -1) { |
| 4794 | Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot); |
| 4795 | exit(1); |
| 4796 | } |
| 4797 | chdir("/"); |
| 4798 | } |
| 4799 | |
| 4800 | /* setgid / setuid */ |
| 4801 | if (global.gid && setregid(global.gid, global.gid) == -1) { |
| 4802 | Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid); |
| 4803 | exit(1); |
| 4804 | } |
| 4805 | |
| 4806 | if (global.uid && setreuid(global.uid, global.uid) == -1) { |
| 4807 | Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid); |
| 4808 | exit(1); |
| 4809 | } |
| 4810 | |
| 4811 | if (global.mode & MODE_DAEMON) { |
| 4812 | int ret = 0; |
| 4813 | int proc; |
| 4814 | |
| 4815 | /* the father launches the required number of processes */ |
| 4816 | for (proc = 0; proc < global.nbproc; proc++) { |
| 4817 | ret = fork(); |
| 4818 | if (ret < 0) { |
| 4819 | Alert("[%s.main()] Cannot fork.\n", argv[0]); |
| 4820 | exit(1); /* there has been an error */ |
| 4821 | } |
| 4822 | else if (ret == 0) /* child breaks here */ |
| 4823 | break; |
| 4824 | } |
| 4825 | if (proc == global.nbproc) |
| 4826 | exit(0); /* parent must leave */ |
| 4827 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4828 | /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure |
| 4829 | * that we can detach from the TTY. We MUST NOT do it in other cases since |
| 4830 | * it would have already be done, and 0-2 would have been affected to listening |
| 4831 | * sockets |
| 4832 | */ |
| 4833 | if (!(global.mode & MODE_QUIET)) { |
| 4834 | /* detach from the tty */ |
| 4835 | fclose(stdin); fclose(stdout); fclose(stderr); |
| 4836 | close(0); close(1); close(2); /* close all fd's */ |
| 4837 | global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */ |
| 4838 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4839 | pid = getpid(); /* update child's pid */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4840 | setpgid(1, 0); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 4841 | setpgrp(); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4842 | } |
| 4843 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4844 | select_loop(); |
| 4845 | |
| 4846 | exit(0); |
| 4847 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4848 | |