willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2 | * HA-Proxy : High Availability-enabled HTTP/TCP proxy |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3 | * 2000-2003 - Willy Tarreau - willy AT meta-x DOT org. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version |
| 8 | * 2 of the License, or (at your option) any later version. |
| 9 | * |
willy tarreau | 906b268 | 2005-12-17 13:49:52 +0100 | [diff] [blame] | 10 | * Please refer to RFC2068 or RFC2616 for informations about HTTP protocol, and |
| 11 | * RFC2965 for informations about cookies usage. |
| 12 | * |
| 13 | * Pending bugs (may be not fixed because never reproduced) : |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 14 | * - solaris only : sometimes, an HTTP proxy with only a dispatch address causes |
| 15 | * 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] | 16 | * the response. Seen on 1.1.8pre4, but never reproduced. May not be related to |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 17 | * the snprintf() bug since requests were simple (GET / HTTP/1.0), but may be |
| 18 | * related to missing setsid() (fixed in 1.1.15) |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 19 | * - a proxy with an invalid config will prevent the startup even if disabled. |
| 20 | * |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 21 | * ChangeLog has moved to the CHANGELOG file. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 22 | * |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 23 | * TODO: |
| 24 | * - handle properly intermediate incomplete server headers. Done ? |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 25 | * - handle hot-reconfiguration |
willy tarreau | 906b268 | 2005-12-17 13:49:52 +0100 | [diff] [blame] | 26 | * - fix client/server state transition when server is in connect or headers state |
| 27 | * and client suddenly disconnects. The server *should* switch to SHUT_WR, but |
| 28 | * still handle HTTP headers. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 29 | * |
| 30 | */ |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <unistd.h> |
| 35 | #include <string.h> |
| 36 | #include <ctype.h> |
| 37 | #include <sys/time.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <sys/socket.h> |
| 40 | #include <netinet/tcp.h> |
| 41 | #include <netinet/in.h> |
| 42 | #include <arpa/inet.h> |
| 43 | #include <netdb.h> |
| 44 | #include <fcntl.h> |
| 45 | #include <errno.h> |
| 46 | #include <signal.h> |
| 47 | #include <stdarg.h> |
| 48 | #include <sys/resource.h> |
| 49 | #include <time.h> |
| 50 | #include <regex.h> |
| 51 | #include <syslog.h> |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 52 | #if defined(TPROXY) && defined(NETFILTER) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 53 | #include <linux/netfilter_ipv4.h> |
| 54 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 55 | |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame^] | 56 | #define HAPROXY_VERSION "1.1.21" |
| 57 | #define HAPROXY_DATE "2003/05/06" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 58 | |
| 59 | /* this is for libc5 for example */ |
| 60 | #ifndef TCP_NODELAY |
| 61 | #define TCP_NODELAY 1 |
| 62 | #endif |
| 63 | |
| 64 | #ifndef SHUT_RD |
| 65 | #define SHUT_RD 0 |
| 66 | #endif |
| 67 | |
| 68 | #ifndef SHUT_WR |
| 69 | #define SHUT_WR 1 |
| 70 | #endif |
| 71 | |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 72 | #define BUFSIZE 8192 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 73 | |
| 74 | // reserved buffer space for header rewriting |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 75 | #define MAXREWRITE 4096 |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 76 | #define REQURI_LEN 1024 |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 77 | #define CAPTURE_LEN 64 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 78 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 79 | // max # args on a configuration line |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 80 | #define MAX_LINE_ARGS 40 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 81 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 82 | // max # of added headers per request |
| 83 | #define MAX_NEWHDR 10 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 84 | |
| 85 | // max # of matches per regexp |
| 86 | #define MAX_MATCH 10 |
| 87 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 88 | /* 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] | 89 | #define COOKIENAME_LEN 16 |
| 90 | #define SERVERID_LEN 16 |
| 91 | #define CONN_RETRIES 3 |
| 92 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 93 | #define CHK_CONNTIME 2000 |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 94 | #define DEF_CHKINTR 2000 |
| 95 | #define DEF_FALLTIME 3 |
| 96 | #define DEF_RISETIME 2 |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame^] | 97 | #define DEF_CHECK_REQ "OPTIONS / HTTP/1.0\r\n\r\n" |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 98 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 99 | /* default connections limit */ |
| 100 | #define DEFAULT_MAXCONN 2000 |
| 101 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 102 | /* how many bits are needed to code the size of an int (eg: 32bits -> 5) */ |
| 103 | #define INTBITS 5 |
| 104 | |
| 105 | /* show stats this every millisecond, 0 to disable */ |
| 106 | #ifndef STATTIME |
| 107 | #define STATTIME 2000 |
| 108 | #endif |
| 109 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 110 | /* this reduces the number of calls to select() by choosing appropriate |
| 111 | * sheduler precision in milliseconds. It should be near the minimum |
| 112 | * time that is needed by select() to collect all events. All timeouts |
| 113 | * are rounded up by adding this value prior to pass it to select(). |
| 114 | */ |
| 115 | #define SCHEDULER_RESOLUTION 9 |
| 116 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 117 | #define MINTIME(old, new) (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old))) |
| 118 | #define SETNOW(a) (*a=now) |
| 119 | |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 120 | /****** string-specific macros and functions ******/ |
| 121 | /* if a > max, then bound <a> to <max>. The macro returns the new <a> */ |
| 122 | #define UBOUND(a, max) ({ typeof(a) b = (max); if ((a) > b) (a) = b; (a); }) |
| 123 | |
| 124 | /* if a < min, then bound <a> to <min>. The macro returns the new <a> */ |
| 125 | #define LBOUND(a, min) ({ typeof(a) b = (min); if ((a) < b) (a) = b; (a); }) |
| 126 | |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 127 | /* |
| 128 | * copies at most <size-1> chars from <src> to <dst>. Last char is always |
| 129 | * set to 0, unless <size> is 0. The number of chars copied is returned |
| 130 | * (excluding the terminating zero). |
| 131 | * This code has been optimized for size and speed : on x86, it's 45 bytes |
| 132 | * long, uses only registers, and consumes only 4 cycles per char. |
| 133 | */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 134 | int strlcpy2(char *dst, const char *src, int size) { |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 135 | char *orig = dst; |
| 136 | if (size) { |
| 137 | while (--size && (*dst = *src)) { |
| 138 | src++; dst++; |
| 139 | } |
| 140 | *dst = 0; |
| 141 | } |
| 142 | return dst - orig; |
| 143 | } |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 144 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 145 | #define MEM_OPTIM |
| 146 | #ifdef MEM_OPTIM |
| 147 | /* |
| 148 | * Returns a pointer to type <type> taken from the |
| 149 | * pool <pool_type> or dynamically allocated. In the |
| 150 | * first case, <pool_type> is updated to point to the |
| 151 | * next element in the list. |
| 152 | */ |
| 153 | #define pool_alloc(type) ({ \ |
| 154 | void *p; \ |
| 155 | if ((p = pool_##type) == NULL) \ |
| 156 | p = malloc(sizeof_##type); \ |
| 157 | else { \ |
| 158 | pool_##type = *(void **)pool_##type; \ |
| 159 | } \ |
| 160 | p; \ |
| 161 | }) |
| 162 | |
| 163 | /* |
| 164 | * Puts a memory area back to the corresponding pool. |
| 165 | * Items are chained directly through a pointer that |
| 166 | * is written in the beginning of the memory area, so |
willy tarreau | 9da061b | 2005-12-17 12:29:56 +0100 | [diff] [blame] | 167 | * there's no need for any carrier cell. This implies |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 168 | * that each memory area is at least as big as one |
| 169 | * pointer. |
| 170 | */ |
| 171 | #define pool_free(type, ptr) ({ \ |
| 172 | *(void **)ptr = (void *)pool_##type; \ |
| 173 | pool_##type = (void *)ptr; \ |
| 174 | }) |
| 175 | |
| 176 | #else |
| 177 | #define pool_alloc(type) (calloc(1,sizeof_##type)); |
| 178 | #define pool_free(type, ptr) (free(ptr)); |
| 179 | #endif /* MEM_OPTIM */ |
| 180 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 181 | #define sizeof_task sizeof(struct task) |
| 182 | #define sizeof_session sizeof(struct session) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 183 | #define sizeof_buffer sizeof(struct buffer) |
| 184 | #define sizeof_fdtab sizeof(struct fdtab) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 185 | #define sizeof_requri REQURI_LEN |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 186 | #define sizeof_capture CAPTURE_LEN |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 187 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 188 | /* different possible states for the sockets */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 189 | #define FD_STCLOSE 0 |
| 190 | #define FD_STLISTEN 1 |
| 191 | #define FD_STCONN 2 |
| 192 | #define FD_STREADY 3 |
| 193 | #define FD_STERROR 4 |
| 194 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 195 | /* values for task->state */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 196 | #define TASK_IDLE 0 |
| 197 | #define TASK_RUNNING 1 |
| 198 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 199 | /* values for proxy->state */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 200 | #define PR_STNEW 0 |
| 201 | #define PR_STIDLE 1 |
| 202 | #define PR_STRUN 2 |
| 203 | #define PR_STDISABLED 3 |
| 204 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 205 | /* values for proxy->mode */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 206 | #define PR_MODE_TCP 0 |
| 207 | #define PR_MODE_HTTP 1 |
| 208 | #define PR_MODE_HEALTH 2 |
| 209 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 210 | /* bits for proxy->options */ |
| 211 | #define PR_O_REDISP 1 /* allow reconnection to dispatch in case of errors */ |
| 212 | #define PR_O_TRANSP 2 /* transparent mode : use original DEST as dispatch */ |
| 213 | #define PR_O_COOK_RW 4 /* rewrite all direct cookies with the right serverid */ |
| 214 | #define PR_O_COOK_IND 8 /* keep only indirect cookies */ |
| 215 | #define PR_O_COOK_INS 16 /* insert cookies when not accessing a server directly */ |
| 216 | #define PR_O_COOK_ANY (PR_O_COOK_RW | PR_O_COOK_IND | PR_O_COOK_INS) |
| 217 | #define PR_O_BALANCE_RR 32 /* balance in round-robin mode */ |
| 218 | #define PR_O_BALANCE (PR_O_BALANCE_RR) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 219 | #define PR_O_KEEPALIVE 64 /* follow keep-alive sessions */ |
| 220 | #define PR_O_FWDFOR 128 /* insert x-forwarded-for with client address */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 221 | #define PR_O_BIND_SRC 256 /* bind to a specific source address when connect()ing */ |
| 222 | #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] | 223 | #define PR_O_COOK_NOC 1024 /* add a 'Cache-control' header with the cookie */ |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 224 | #define PR_O_COOK_POST 2048 /* don't insert cookies for requests other than a POST */ |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 225 | #define PR_O_HTTP_CHK 4096 /* use HTTP 'OPTIONS' method to check server health */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 226 | #define PR_O_PERSIST 8192 /* server persistence stays effective even when server is down */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 227 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 228 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 229 | /* various session flags */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 230 | #define SN_DIRECT 0x00000001 /* connection made on the server matching the client cookie */ |
| 231 | #define SN_CLDENY 0x00000002 /* a client header matches a deny regex */ |
| 232 | #define SN_CLALLOW 0x00000004 /* a client header matches an allow regex */ |
| 233 | #define SN_SVDENY 0x00000008 /* a server header matches a deny regex */ |
| 234 | #define SN_SVALLOW 0x00000010 /* a server header matches an allow regex */ |
| 235 | #define SN_POST 0x00000020 /* the request was an HTTP POST */ |
| 236 | |
| 237 | #define SN_CK_NONE 0x00000000 /* this session had no cookie */ |
| 238 | #define SN_CK_INVALID 0x00000040 /* this session had a cookie which matches no server */ |
| 239 | #define SN_CK_DOWN 0x00000080 /* this session had cookie matching a down server */ |
| 240 | #define SN_CK_VALID 0x000000C0 /* this session had cookie matching a valid server */ |
| 241 | #define SN_CK_MASK 0x000000C0 /* mask to get this session's cookie flags */ |
| 242 | #define SN_CK_SHIFT 6 /* bit shift */ |
| 243 | |
| 244 | #define SN_ERR_CLITO 0x00000100 /* client time-out */ |
| 245 | #define SN_ERR_CLICL 0x00000200 /* client closed (read/write error) */ |
| 246 | #define SN_ERR_SRVTO 0x00000300 /* server time-out, connect time-out */ |
| 247 | #define SN_ERR_SRVCL 0x00000400 /* server closed (connect/read/write error) */ |
| 248 | #define SN_ERR_PRXCOND 0x00000500 /* the proxy decided to close (deny...) */ |
| 249 | #define SN_ERR_MASK 0x00000700 /* mask to get only session error flags */ |
| 250 | #define SN_ERR_SHIFT 8 /* bit shift */ |
| 251 | |
| 252 | #define SN_FINST_R 0x00001000 /* session ended during client request */ |
| 253 | #define SN_FINST_C 0x00002000 /* session ended during server connect */ |
| 254 | #define SN_FINST_H 0x00003000 /* session ended during server headers */ |
| 255 | #define SN_FINST_D 0x00004000 /* session ended during data phase */ |
| 256 | #define SN_FINST_L 0x00005000 /* session ended while pushing last data to client */ |
| 257 | #define SN_FINST_MASK 0x00007000 /* mask to get only final session state flags */ |
| 258 | #define SN_FINST_SHIFT 12 /* bit shift */ |
| 259 | |
| 260 | #define SN_SCK_NONE 0x00000000 /* no set-cookie seen for the server cookie */ |
| 261 | #define SN_SCK_DELETED 0x00010000 /* existing set-cookie deleted or changed */ |
| 262 | #define SN_SCK_INSERTED 0x00020000 /* new set-cookie inserted or changed existing one */ |
| 263 | #define SN_SCK_SEEN 0x00040000 /* set-cookie seen for the server cookie */ |
| 264 | #define SN_SCK_MASK 0x00070000 /* mask to get the set-cookie field */ |
| 265 | #define SN_SCK_SHIFT 16 /* bit shift */ |
| 266 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 267 | |
| 268 | /* different possible states for the client side */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 269 | #define CL_STHEADERS 0 |
| 270 | #define CL_STDATA 1 |
| 271 | #define CL_STSHUTR 2 |
| 272 | #define CL_STSHUTW 3 |
| 273 | #define CL_STCLOSE 4 |
| 274 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 275 | /* different possible states for the server side */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 276 | #define SV_STIDLE 0 |
| 277 | #define SV_STCONN 1 |
| 278 | #define SV_STHEADERS 2 |
| 279 | #define SV_STDATA 3 |
| 280 | #define SV_STSHUTR 4 |
| 281 | #define SV_STSHUTW 5 |
| 282 | #define SV_STCLOSE 6 |
| 283 | |
| 284 | /* result of an I/O event */ |
| 285 | #define RES_SILENT 0 /* didn't happen */ |
| 286 | #define RES_DATA 1 /* data were sent or received */ |
| 287 | #define RES_NULL 2 /* result is 0 (read == 0), or connect without need for writing */ |
| 288 | #define RES_ERROR 3 /* result -1 or error on the socket (eg: connect()) */ |
| 289 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 290 | /* modes of operation (global.mode) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 291 | #define MODE_DEBUG 1 |
| 292 | #define MODE_STATS 2 |
| 293 | #define MODE_LOG 4 |
| 294 | #define MODE_DAEMON 8 |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 295 | #define MODE_QUIET 16 |
| 296 | |
| 297 | /* server flags */ |
| 298 | #define SRV_RUNNING 1 |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 299 | #define SRV_BACKUP 2 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 300 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 301 | /* what to do when a header matches a regex */ |
| 302 | #define ACT_ALLOW 0 /* allow the request */ |
| 303 | #define ACT_REPLACE 1 /* replace the matching header */ |
| 304 | #define ACT_REMOVE 2 /* remove the matching header */ |
| 305 | #define ACT_DENY 3 /* deny the request */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 306 | #define ACT_PASS 4 /* pass this header without allowing or denying the request */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 307 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 308 | /* configuration sections */ |
| 309 | #define CFG_NONE 0 |
| 310 | #define CFG_GLOBAL 1 |
| 311 | #define CFG_LISTEN 2 |
| 312 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 313 | /* 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] | 314 | #define LW_DATE 1 /* date */ |
| 315 | #define LW_CLIP 2 /* CLient IP */ |
| 316 | #define LW_SVIP 4 /* SerVer IP */ |
| 317 | #define LW_SVID 8 /* server ID */ |
| 318 | #define LW_REQ 16 /* http REQuest */ |
| 319 | #define LW_RESP 32 /* http RESPonse */ |
| 320 | #define LW_PXIP 64 /* proxy IP */ |
| 321 | #define LW_PXID 128 /* proxy ID */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 322 | #define LW_BYTES 256 /* bytes read from server */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 323 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 324 | /*********************************************************************/ |
| 325 | |
| 326 | #define LIST_HEAD(a) ((void *)(&(a))) |
| 327 | |
| 328 | /*********************************************************************/ |
| 329 | |
| 330 | struct hdr_exp { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 331 | struct hdr_exp *next; |
| 332 | regex_t *preg; /* expression to look for */ |
| 333 | int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */ |
| 334 | char *replace; /* expression to set instead */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | struct buffer { |
| 338 | unsigned int l; /* data length */ |
| 339 | 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] | 340 | char *rlim; /* read limit, used for header rewriting */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 341 | unsigned long long total; /* total data read */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 342 | char data[BUFSIZE]; |
| 343 | }; |
| 344 | |
| 345 | struct server { |
| 346 | struct server *next; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 347 | int state; /* server state (SRV_*) */ |
| 348 | int cklen; /* the len of the cookie, to speed up checks */ |
| 349 | char *cookie; /* the id set in the cookie */ |
| 350 | char *id; /* just for identification */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 351 | struct sockaddr_in addr; /* the address to connect to */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 352 | int health; /* 0->rise-1 = bad; rise->rise+fall-1 = good */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 353 | int rise, fall; /* time in iterations */ |
| 354 | int inter; /* time in milliseconds */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 355 | int result; /* 0 = connect OK, -1 = connect KO */ |
| 356 | 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] | 357 | struct proxy *proxy; /* the proxy this server belongs to */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 358 | }; |
| 359 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 360 | /* The base for all tasks */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 361 | struct task { |
| 362 | struct task *next, *prev; /* chaining ... */ |
| 363 | struct task *rqnext; /* chaining in run queue ... */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 364 | struct task *wq; /* the wait queue this task is in */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 365 | int state; /* task state : IDLE or RUNNING */ |
| 366 | 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] | 367 | int (*process)(struct task *t); /* the function which processes the task */ |
| 368 | void *context; /* the task's context */ |
| 369 | }; |
| 370 | |
| 371 | /* WARNING: if new fields are added, they must be initialized in event_accept() */ |
| 372 | struct session { |
| 373 | struct task *task; /* the task associated with this session */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 374 | /* application specific below */ |
| 375 | struct timeval crexpire; /* expiration date for a client read */ |
| 376 | struct timeval cwexpire; /* expiration date for a client write */ |
| 377 | struct timeval srexpire; /* expiration date for a server read */ |
| 378 | struct timeval swexpire; /* expiration date for a server write */ |
| 379 | struct timeval cnexpire; /* expiration date for a connect */ |
| 380 | char res_cr, res_cw, res_sr, res_sw;/* results of some events */ |
| 381 | struct proxy *proxy; /* the proxy this socket belongs to */ |
| 382 | int cli_fd; /* the client side fd */ |
| 383 | int srv_fd; /* the server side fd */ |
| 384 | int cli_state; /* state of the client side */ |
| 385 | int srv_state; /* state of the server side */ |
| 386 | int conn_retries; /* number of connect retries left */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 387 | int flags; /* some flags describing the session */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 388 | struct buffer *req; /* request buffer */ |
| 389 | struct buffer *rep; /* response buffer */ |
| 390 | struct sockaddr_in cli_addr; /* the client address */ |
| 391 | struct sockaddr_in srv_addr; /* the address to connect to */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 392 | struct server *srv; /* the server being used */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 393 | struct { |
| 394 | int logwait; /* log fields waiting to be collected : LW_* */ |
| 395 | struct timeval tv_accept; /* date of the accept() (beginning of the session) */ |
| 396 | long t_request; /* delay before the end of the request arrives, -1 if never occurs */ |
| 397 | long t_connect; /* delay before the connect() to the server succeeds, -1 if never occurs */ |
| 398 | long t_data; /* delay before the first data byte from the server ... */ |
| 399 | unsigned long t_close; /* total session duration */ |
| 400 | char *uri; /* first line if log needed, NULL otherwise */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 401 | char *cli_cookie; /* cookie presented by the client, in capture mode */ |
| 402 | char *srv_cookie; /* cookie presented by the server, in capture mode */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 403 | int status; /* HTTP status from the server, negative if from proxy */ |
| 404 | long long bytes; /* number of bytes transferred from the server */ |
| 405 | } logs; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame^] | 406 | unsigned int uniq_id; /* unique ID used for the traces */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 407 | }; |
| 408 | |
| 409 | struct proxy { |
| 410 | int listen_fd; /* the listen socket */ |
| 411 | int state; /* proxy state */ |
| 412 | struct sockaddr_in listen_addr; /* the address we listen to */ |
| 413 | struct sockaddr_in dispatch_addr; /* the default address to connect to */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 414 | struct server *srv, *cursrv; /* known servers, current server */ |
| 415 | int nbservers; /* # of servers */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 416 | char *cookie_name; /* name of the cookie to look for */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 417 | int cookie_len; /* strlen(cookie_len), computed only once */ |
| 418 | char *capture_name; /* beginning of the name of the cookie to capture */ |
| 419 | int capture_namelen; /* length of the cookie name to match */ |
| 420 | int capture_len; /* length of the string to be captured */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 421 | int clitimeout; /* client I/O timeout (in milliseconds) */ |
| 422 | int srvtimeout; /* server I/O timeout (in milliseconds) */ |
| 423 | int contimeout; /* connect timeout (in milliseconds) */ |
| 424 | char *id; /* proxy id */ |
| 425 | int nbconn; /* # of active sessions */ |
| 426 | int maxconn; /* max # of active sessions */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 427 | int conn_retries; /* maximum number of connect retries */ |
| 428 | int options; /* PR_O_REDISP, PR_O_TRANSP */ |
| 429 | 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] | 430 | 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] | 431 | struct proxy *next; |
| 432 | struct sockaddr_in logsrv1, logsrv2; /* 2 syslog servers */ |
| 433 | char logfac1, logfac2; /* log facility for both servers. -1 = disabled */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 434 | int loglev1, loglev2; /* log level for each server, 7 by default */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 435 | int to_log; /* things to be logged (LW_*) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 436 | struct timeval stop_time; /* date to stop listening, when stopping != 0 */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 437 | int nb_reqadd, nb_rspadd; |
| 438 | struct hdr_exp *req_exp; /* regular expressions for request headers */ |
| 439 | struct hdr_exp *rsp_exp; /* regular expressions for response headers */ |
| 440 | 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] | 441 | int grace; /* grace time after stop request */ |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame^] | 442 | char *check_req; /* HTTP request to use if PR_O_HTTP_CHK is set, else NULL */ |
| 443 | int check_len; /* Length of the HTTP request */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 444 | struct { |
| 445 | char *msg400; /* message for error 400 */ |
| 446 | int len400; /* message length for error 400 */ |
| 447 | char *msg403; /* message for error 403 */ |
| 448 | int len403; /* message length for error 403 */ |
| 449 | char *msg408; /* message for error 408 */ |
| 450 | int len408; /* message length for error 408 */ |
| 451 | char *msg500; /* message for error 500 */ |
| 452 | int len500; /* message length for error 500 */ |
| 453 | char *msg502; /* message for error 502 */ |
| 454 | int len502; /* message length for error 502 */ |
| 455 | char *msg503; /* message for error 503 */ |
| 456 | int len503; /* message length for error 503 */ |
| 457 | char *msg504; /* message for error 504 */ |
| 458 | int len504; /* message length for error 504 */ |
| 459 | } errmsg; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 460 | }; |
| 461 | |
| 462 | /* info about one given fd */ |
| 463 | struct fdtab { |
| 464 | int (*read)(int fd); /* read function */ |
| 465 | int (*write)(int fd); /* write function */ |
| 466 | struct task *owner; /* the session (or proxy) associated with this fd */ |
| 467 | int state; /* the state of this fd */ |
| 468 | }; |
| 469 | |
| 470 | /*********************************************************************/ |
| 471 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 472 | int cfg_maxpconn = 2000; /* # of simultaneous connections per proxy (-N) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 473 | char *cfg_cfgfile = NULL; /* configuration file */ |
| 474 | char *progname = NULL; /* program name */ |
| 475 | int pid; /* current process id */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 476 | |
| 477 | /* global options */ |
| 478 | static struct { |
| 479 | int uid; |
| 480 | int gid; |
| 481 | int nbproc; |
| 482 | int maxconn; |
| 483 | int maxsock; /* max # of sockets */ |
| 484 | int mode; |
| 485 | char *chroot; |
| 486 | int logfac1, logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 487 | int loglev1, loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 488 | struct sockaddr_in logsrv1, logsrv2; |
| 489 | } global = { |
| 490 | logfac1 : -1, |
| 491 | logfac2 : -1, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 492 | loglev1 : 7, /* max syslog level : debug */ |
| 493 | loglev2 : 7, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 494 | /* others NULL OK */ |
| 495 | }; |
| 496 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 497 | /*********************************************************************/ |
| 498 | |
| 499 | fd_set *ReadEvent, |
| 500 | *WriteEvent, |
| 501 | *StaticReadEvent, |
| 502 | *StaticWriteEvent; |
| 503 | |
| 504 | void **pool_session = NULL, |
| 505 | **pool_buffer = NULL, |
| 506 | **pool_fdtab = NULL, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 507 | **pool_requri = NULL, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 508 | **pool_task = NULL, |
| 509 | **pool_capture = NULL; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 510 | |
| 511 | struct proxy *proxy = NULL; /* list of all existing proxies */ |
| 512 | struct fdtab *fdtab = NULL; /* array of all the file descriptors */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 513 | struct task *rq = NULL; /* global run queue */ |
| 514 | struct task wait_queue = { /* global wait queue */ |
| 515 | prev:LIST_HEAD(wait_queue), |
| 516 | next:LIST_HEAD(wait_queue) |
| 517 | }; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 518 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 519 | static int totalconn = 0; /* total # of terminated sessions */ |
| 520 | static int actconn = 0; /* # of active sessions */ |
| 521 | static int maxfd = 0; /* # of the highest fd + 1 */ |
| 522 | static int listeners = 0; /* # of listeners */ |
| 523 | static int stopping = 0; /* non zero means stopping in progress */ |
| 524 | static struct timeval now = {0,0}; /* the current date at any moment */ |
| 525 | |
| 526 | 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] | 527 | /* 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] | 528 | static char trash[BUFSIZE]; |
| 529 | |
| 530 | /* |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 531 | * Syslog facilities and levels. Conforming to RFC3164. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 532 | */ |
| 533 | |
| 534 | #define MAX_SYSLOG_LEN 1024 |
| 535 | #define NB_LOG_FACILITIES 24 |
| 536 | const char *log_facilities[NB_LOG_FACILITIES] = { |
| 537 | "kern", "user", "mail", "daemon", |
| 538 | "auth", "syslog", "lpr", "news", |
| 539 | "uucp", "cron", "auth2", "ftp", |
| 540 | "ntp", "audit", "alert", "cron2", |
| 541 | "local0", "local1", "local2", "local3", |
| 542 | "local4", "local5", "local6", "local7" |
| 543 | }; |
| 544 | |
| 545 | |
| 546 | #define NB_LOG_LEVELS 8 |
| 547 | const char *log_levels[NB_LOG_LEVELS] = { |
| 548 | "emerg", "alert", "crit", "err", |
| 549 | "warning", "notice", "info", "debug" |
| 550 | }; |
| 551 | |
| 552 | #define SYSLOG_PORT 514 |
| 553 | |
| 554 | const char *monthname[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 555 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 556 | |
| 557 | const char sess_term_cond[8] = "-cCsSP67"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, unknown */ |
| 558 | const char sess_fin_state[8] = "-RCHDL67"; /* cliRequest, srvConnect, srvHeader, Data, Last, unknown */ |
| 559 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 560 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 561 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 562 | unknown, Set-cookie Rewritten */ |
| 563 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 564 | #define MAX_HOSTNAME_LEN 32 |
| 565 | static char hostname[MAX_HOSTNAME_LEN] = ""; |
| 566 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 567 | const char *HTTP_302 = |
| 568 | "HTTP/1.0 302 Found\r\n" |
| 569 | "Cache-Control: no-cache\r\n" |
| 570 | "Connection: close\r\n" |
| 571 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 572 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 573 | const char *HTTP_400 = |
| 574 | "HTTP/1.0 400 Bad request\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 575 | "Cache-Control: no-cache\r\n" |
| 576 | "Connection: close\r\n" |
| 577 | "\r\n" |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 578 | "<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] | 579 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 580 | const char *HTTP_403 = |
| 581 | "HTTP/1.0 403 Forbidden\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 582 | "Cache-Control: no-cache\r\n" |
| 583 | "Connection: close\r\n" |
| 584 | "\r\n" |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 585 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n"; |
| 586 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 587 | const char *HTTP_408 = |
| 588 | "HTTP/1.0 408 Request Time-out\r\n" |
| 589 | "Cache-Control: no-cache\r\n" |
| 590 | "Connection: close\r\n" |
| 591 | "\r\n" |
| 592 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n"; |
| 593 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 594 | const char *HTTP_500 = |
| 595 | "HTTP/1.0 500 Server Error\r\n" |
| 596 | "Cache-Control: no-cache\r\n" |
| 597 | "Connection: close\r\n" |
| 598 | "\r\n" |
| 599 | "<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] | 600 | |
| 601 | const char *HTTP_502 = |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 602 | "HTTP/1.0 502 Bad Gateway\r\n" |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 603 | "Cache-Control: no-cache\r\n" |
| 604 | "Connection: close\r\n" |
| 605 | "\r\n" |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 606 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n"; |
| 607 | |
| 608 | const char *HTTP_503 = |
| 609 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 610 | "Cache-Control: no-cache\r\n" |
| 611 | "Connection: close\r\n" |
| 612 | "\r\n" |
| 613 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n"; |
| 614 | |
| 615 | const char *HTTP_504 = |
| 616 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 617 | "Cache-Control: no-cache\r\n" |
| 618 | "Connection: close\r\n" |
| 619 | "\r\n" |
| 620 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n"; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 621 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 622 | /*********************************************************************/ |
| 623 | /* statistics ******************************************************/ |
| 624 | /*********************************************************************/ |
| 625 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 626 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 627 | static int stats_tsk_lsrch, stats_tsk_rsrch, |
| 628 | stats_tsk_good, stats_tsk_right, stats_tsk_left, |
| 629 | stats_tsk_new, stats_tsk_nsrch; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 630 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 631 | |
| 632 | |
| 633 | /*********************************************************************/ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 634 | /* debugging *******************************************************/ |
| 635 | /*********************************************************************/ |
| 636 | #ifdef DEBUG_FULL |
| 637 | static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 638 | static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 639 | #endif |
| 640 | |
| 641 | /*********************************************************************/ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 642 | /* function prototypes *********************************************/ |
| 643 | /*********************************************************************/ |
| 644 | |
| 645 | int event_accept(int fd); |
| 646 | int event_cli_read(int fd); |
| 647 | int event_cli_write(int fd); |
| 648 | int event_srv_read(int fd); |
| 649 | int event_srv_write(int fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 650 | int process_session(struct task *t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 651 | |
| 652 | /*********************************************************************/ |
| 653 | /* general purpose functions ***************************************/ |
| 654 | /*********************************************************************/ |
| 655 | |
| 656 | void display_version() { |
| 657 | printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n"); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 658 | 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] | 659 | } |
| 660 | |
| 661 | /* |
| 662 | * This function prints the command line usage and exits |
| 663 | */ |
| 664 | void usage(char *name) { |
| 665 | display_version(); |
| 666 | fprintf(stderr, |
| 667 | "Usage : %s -f <cfgfile> [ -vd" |
| 668 | #if STATTIME > 0 |
| 669 | "sl" |
| 670 | #endif |
| 671 | "D ] [ -n <maxconn> ] [ -N <maxpconn> ]\n" |
| 672 | " -v displays version\n" |
| 673 | " -d enters debug mode\n" |
| 674 | #if STATTIME > 0 |
| 675 | " -s enables statistics output\n" |
| 676 | " -l enables long statistics format\n" |
| 677 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 678 | " -D goes daemon ; implies -q\n" |
| 679 | " -q quiet mode : don't display messages\n" |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 680 | " -n sets the maximum total # of connections (%d)\n" |
| 681 | " -N sets the default, per-proxy maximum # of connections (%d)\n\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 682 | name, DEFAULT_MAXCONN, cfg_maxpconn); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 683 | exit(1); |
| 684 | } |
| 685 | |
| 686 | |
| 687 | /* |
| 688 | * Displays the message on stderr with the date and pid. |
| 689 | */ |
| 690 | void Alert(char *fmt, ...) { |
| 691 | va_list argp; |
| 692 | struct timeval tv; |
| 693 | struct tm *tm; |
| 694 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 695 | if (!(global.mode & MODE_QUIET)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 696 | va_start(argp, fmt); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 697 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 698 | gettimeofday(&tv, NULL); |
| 699 | tm=localtime(&tv.tv_sec); |
| 700 | fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ", |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 701 | 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] | 702 | vfprintf(stderr, fmt, argp); |
| 703 | fflush(stderr); |
| 704 | va_end(argp); |
| 705 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | |
| 709 | /* |
| 710 | * Displays the message on stderr with the date and pid. |
| 711 | */ |
| 712 | void Warning(char *fmt, ...) { |
| 713 | va_list argp; |
| 714 | struct timeval tv; |
| 715 | struct tm *tm; |
| 716 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 717 | if (!(global.mode & MODE_QUIET)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 718 | va_start(argp, fmt); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 719 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 720 | gettimeofday(&tv, NULL); |
| 721 | tm=localtime(&tv.tv_sec); |
| 722 | fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ", |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 723 | 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] | 724 | vfprintf(stderr, fmt, argp); |
| 725 | fflush(stderr); |
| 726 | va_end(argp); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | /* |
| 731 | * Displays the message on <out> only if quiet mode is not set. |
| 732 | */ |
| 733 | void qfprintf(FILE *out, char *fmt, ...) { |
| 734 | va_list argp; |
| 735 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 736 | if (!(global.mode & MODE_QUIET)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 737 | va_start(argp, fmt); |
| 738 | vfprintf(out, fmt, argp); |
| 739 | fflush(out); |
| 740 | va_end(argp); |
| 741 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | |
| 745 | /* |
| 746 | * converts <str> to a struct sockaddr_in* which is locally allocated. |
| 747 | * The format is "addr:port", where "addr" can be empty or "*" to indicate |
| 748 | * INADDR_ANY. |
| 749 | */ |
| 750 | struct sockaddr_in *str2sa(char *str) { |
| 751 | static struct sockaddr_in sa; |
| 752 | char *c; |
| 753 | int port; |
| 754 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 755 | memset(&sa, 0, sizeof(sa)); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 756 | str=strdup(str); |
| 757 | |
| 758 | if ((c=strrchr(str,':')) != NULL) { |
| 759 | *c++=0; |
| 760 | port=atol(c); |
| 761 | } |
| 762 | else |
| 763 | port=0; |
| 764 | |
| 765 | if (*str == '*' || *str == '\0') { /* INADDR_ANY */ |
| 766 | sa.sin_addr.s_addr = INADDR_ANY; |
| 767 | } |
| 768 | else if ( |
| 769 | #ifndef SOLARIS |
| 770 | !inet_aton(str, &sa.sin_addr) |
| 771 | #else |
| 772 | !inet_pton(AF_INET, str, &sa.sin_addr) |
| 773 | #endif |
| 774 | ) { |
| 775 | struct hostent *he; |
| 776 | |
| 777 | if ((he = gethostbyname(str)) == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 778 | Alert("Invalid server name: '%s'\n", str); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 779 | } |
| 780 | else |
| 781 | sa.sin_addr = *(struct in_addr *) *(he->h_addr_list); |
| 782 | } |
| 783 | sa.sin_port=htons(port); |
| 784 | sa.sin_family=AF_INET; |
| 785 | |
| 786 | free(str); |
| 787 | return &sa; |
| 788 | } |
| 789 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 790 | |
| 791 | /* |
| 792 | * This function sends a syslog message to both log servers of a proxy, |
| 793 | * or to global log servers if the proxy is NULL. |
| 794 | * It also tries not to waste too much time computing the message header. |
| 795 | * It doesn't care about errors nor does it report them. |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 796 | */ |
| 797 | void send_log(struct proxy *p, int level, char *message, ...) { |
| 798 | static int logfd = -1; /* syslog UDP socket */ |
| 799 | static long tvsec = -1; /* to force the string to be initialized */ |
| 800 | struct timeval tv; |
| 801 | va_list argp; |
| 802 | static char logmsg[MAX_SYSLOG_LEN]; |
| 803 | static char *dataptr = NULL; |
| 804 | int fac_level; |
| 805 | int hdr_len, data_len; |
| 806 | struct sockaddr_in *sa[2]; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 807 | int facilities[2], loglevel[2]; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 808 | int nbloggers = 0; |
| 809 | char *log_ptr; |
| 810 | |
| 811 | if (logfd < 0) { |
| 812 | if ((logfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) |
| 813 | return; |
| 814 | } |
| 815 | |
| 816 | if (level < 0 || progname == NULL || message == NULL) |
| 817 | return; |
| 818 | |
| 819 | gettimeofday(&tv, NULL); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 820 | if (tv.tv_sec != tvsec || dataptr == NULL) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 821 | /* this string is rebuild only once a second */ |
| 822 | struct tm *tm = localtime(&tv.tv_sec); |
| 823 | tvsec = tv.tv_sec; |
| 824 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 825 | hdr_len = snprintf(logmsg, sizeof(logmsg), |
| 826 | "<<<<>%s %2d %02d:%02d:%02d %s[%d]: ", |
| 827 | monthname[tm->tm_mon], |
| 828 | tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 829 | progname, pid); |
| 830 | /* WARNING: depending upon implementations, snprintf may return |
| 831 | * either -1 or the number of bytes that would be needed to store |
| 832 | * the total message. In both cases, we must adjust it. |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 833 | */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 834 | if (hdr_len < 0 || hdr_len > sizeof(logmsg)) |
| 835 | hdr_len = sizeof(logmsg); |
| 836 | |
| 837 | dataptr = logmsg + hdr_len; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | va_start(argp, message); |
| 841 | data_len = vsnprintf(dataptr, logmsg + sizeof(logmsg) - dataptr, message, argp); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 842 | if (data_len < 0 || data_len > (logmsg + sizeof(logmsg) - dataptr)) |
| 843 | data_len = logmsg + sizeof(logmsg) - dataptr; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 844 | va_end(argp); |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 845 | dataptr[data_len - 1] = '\n'; /* force a break on ultra-long lines */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 846 | |
| 847 | if (p == NULL) { |
| 848 | if (global.logfac1 >= 0) { |
| 849 | sa[nbloggers] = &global.logsrv1; |
| 850 | facilities[nbloggers] = global.logfac1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 851 | loglevel[nbloggers] = global.loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 852 | nbloggers++; |
| 853 | } |
| 854 | if (global.logfac2 >= 0) { |
| 855 | sa[nbloggers] = &global.logsrv2; |
| 856 | facilities[nbloggers] = global.logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 857 | loglevel[nbloggers] = global.loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 858 | nbloggers++; |
| 859 | } |
| 860 | } else { |
| 861 | if (p->logfac1 >= 0) { |
| 862 | sa[nbloggers] = &p->logsrv1; |
| 863 | facilities[nbloggers] = p->logfac1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 864 | loglevel[nbloggers] = p->loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 865 | nbloggers++; |
| 866 | } |
| 867 | if (p->logfac2 >= 0) { |
| 868 | sa[nbloggers] = &p->logsrv2; |
| 869 | facilities[nbloggers] = p->logfac2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 870 | loglevel[nbloggers] = p->loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 871 | nbloggers++; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | while (nbloggers-- > 0) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 876 | /* we can filter the level of the messages that are sent to each logger */ |
| 877 | if (level > loglevel[nbloggers]) |
| 878 | continue; |
| 879 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 880 | /* For each target, we may have a different facility. |
| 881 | * We can also have a different log level for each message. |
| 882 | * This induces variations in the message header length. |
| 883 | * Since we don't want to recompute it each time, nor copy it every |
| 884 | * time, we only change the facility in the pre-computed header, |
| 885 | * and we change the pointer to the header accordingly. |
| 886 | */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 887 | fac_level = (facilities[nbloggers] << 3) + level; |
| 888 | log_ptr = logmsg + 3; /* last digit of the log level */ |
| 889 | do { |
| 890 | *log_ptr = '0' + fac_level % 10; |
| 891 | fac_level /= 10; |
| 892 | log_ptr--; |
| 893 | } while (fac_level && log_ptr > logmsg); |
| 894 | *log_ptr = '<'; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 895 | |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 896 | /* 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] | 897 | |
| 898 | #ifndef MSG_NOSIGNAL |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 899 | sendto(logfd, log_ptr, dataptr + data_len - log_ptr, MSG_DONTWAIT, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 900 | (struct sockaddr *)sa[nbloggers], sizeof(**sa)); |
| 901 | #else |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 902 | 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] | 903 | (struct sockaddr *)sa[nbloggers], sizeof(**sa)); |
| 904 | #endif |
| 905 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | |
| 909 | /* sets <tv> to the current time */ |
| 910 | static inline struct timeval *tv_now(struct timeval *tv) { |
| 911 | if (tv) |
| 912 | gettimeofday(tv, NULL); |
| 913 | return tv; |
| 914 | } |
| 915 | |
| 916 | /* |
| 917 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 918 | */ |
| 919 | static inline struct timeval *tv_delayfrom(struct timeval *tv, struct timeval *from, int ms) { |
| 920 | if (!tv || !from) |
| 921 | return NULL; |
| 922 | tv->tv_usec = from->tv_usec + (ms%1000)*1000; |
| 923 | tv->tv_sec = from->tv_sec + (ms/1000); |
| 924 | while (tv->tv_usec >= 1000000) { |
| 925 | tv->tv_usec -= 1000000; |
| 926 | tv->tv_sec++; |
| 927 | } |
| 928 | return tv; |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
| 933 | */ |
| 934 | static inline int tv_cmp(struct timeval *tv1, struct timeval *tv2) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 935 | if (tv1->tv_sec < tv2->tv_sec) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 936 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 937 | else if (tv1->tv_sec > tv2->tv_sec) |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 938 | return 1; |
| 939 | else if (tv1->tv_usec < tv2->tv_usec) |
| 940 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 941 | else if (tv1->tv_usec > tv2->tv_usec) |
| 942 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 943 | else |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | /* |
| 948 | * returns the absolute difference, in ms, between tv1 and tv2 |
| 949 | */ |
| 950 | unsigned long tv_delta(struct timeval *tv1, struct timeval *tv2) { |
| 951 | int cmp; |
| 952 | unsigned long ret; |
| 953 | |
| 954 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 955 | cmp = tv_cmp(tv1, tv2); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 956 | if (!cmp) |
| 957 | return 0; /* same dates, null diff */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 958 | else if (cmp < 0) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 959 | struct timeval *tmp = tv1; |
| 960 | tv1 = tv2; |
| 961 | tv2 = tmp; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 962 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 963 | ret = (tv1->tv_sec - tv2->tv_sec) * 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 964 | if (tv1->tv_usec > tv2->tv_usec) |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 965 | ret += (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 966 | else |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 967 | ret -= (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 968 | return (unsigned long) ret; |
| 969 | } |
| 970 | |
| 971 | /* |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 972 | * returns the difference, in ms, between tv1 and tv2 |
| 973 | */ |
| 974 | static inline unsigned long tv_diff(struct timeval *tv1, struct timeval *tv2) { |
| 975 | unsigned long ret; |
| 976 | |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 977 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
| 978 | if (tv2->tv_usec > tv1->tv_usec) |
| 979 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 980 | else |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 981 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 982 | return (unsigned long) ret; |
| 983 | } |
| 984 | |
| 985 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 986 | * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
| 987 | */ |
| 988 | static inline int tv_cmp_ms(struct timeval *tv1, struct timeval *tv2) { |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 989 | if (tv1->tv_sec == tv2->tv_sec) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 990 | if (tv2->tv_usec > tv1->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 991 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 992 | else if (tv1->tv_usec > tv2->tv_usec + 1000) |
| 993 | return 1; |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 994 | else |
| 995 | return 0; |
| 996 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 997 | else if ((tv2->tv_sec > tv1->tv_sec + 1) || |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 998 | ((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] | 999 | return -1; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1000 | else if ((tv1->tv_sec > tv2->tv_sec + 1) || |
| 1001 | ((tv1->tv_sec == tv2->tv_sec + 1) && (tv1->tv_usec + 1000000 > tv2->tv_usec + 1000))) |
| 1002 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1003 | else |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
| 1007 | /* |
| 1008 | * returns the remaining time between tv1=now and event=tv2 |
| 1009 | * if tv2 is passed, 0 is returned. |
| 1010 | */ |
| 1011 | static inline unsigned long tv_remain(struct timeval *tv1, struct timeval *tv2) { |
| 1012 | unsigned long ret; |
| 1013 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1014 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 1015 | return 0; /* event elapsed */ |
| 1016 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1017 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1018 | if (tv2->tv_usec > tv1->tv_usec) |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1019 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1020 | else |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1021 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1022 | return (unsigned long) ret; |
| 1023 | } |
| 1024 | |
| 1025 | |
| 1026 | /* |
| 1027 | * zeroes a struct timeval |
| 1028 | */ |
| 1029 | |
| 1030 | static inline struct timeval *tv_eternity(struct timeval *tv) { |
| 1031 | tv->tv_sec = tv->tv_usec = 0; |
| 1032 | return tv; |
| 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * returns 1 if tv is null, else 0 |
| 1037 | */ |
| 1038 | static inline int tv_iseternity(struct timeval *tv) { |
| 1039 | if (tv->tv_sec == 0 && tv->tv_usec == 0) |
| 1040 | return 1; |
| 1041 | else |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | /* |
| 1046 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 1047 | * considering that 0 is the eternity. |
| 1048 | */ |
| 1049 | static inline int tv_cmp2(struct timeval *tv1, struct timeval *tv2) { |
| 1050 | if (tv_iseternity(tv1)) |
| 1051 | if (tv_iseternity(tv2)) |
| 1052 | return 0; /* same */ |
| 1053 | else |
| 1054 | return 1; /* tv1 later than tv2 */ |
| 1055 | else if (tv_iseternity(tv2)) |
| 1056 | return -1; /* tv2 later than tv1 */ |
| 1057 | |
| 1058 | if (tv1->tv_sec > tv2->tv_sec) |
| 1059 | return 1; |
| 1060 | else if (tv1->tv_sec < tv2->tv_sec) |
| 1061 | return -1; |
| 1062 | else if (tv1->tv_usec > tv2->tv_usec) |
| 1063 | return 1; |
| 1064 | else if (tv1->tv_usec < tv2->tv_usec) |
| 1065 | return -1; |
| 1066 | else |
| 1067 | return 0; |
| 1068 | } |
| 1069 | |
| 1070 | /* |
| 1071 | * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 1072 | * considering that 0 is the eternity. |
| 1073 | */ |
| 1074 | static inline int tv_cmp2_ms(struct timeval *tv1, struct timeval *tv2) { |
| 1075 | if (tv_iseternity(tv1)) |
| 1076 | if (tv_iseternity(tv2)) |
| 1077 | return 0; /* same */ |
| 1078 | else |
| 1079 | return 1; /* tv1 later than tv2 */ |
| 1080 | else if (tv_iseternity(tv2)) |
| 1081 | return -1; /* tv2 later than tv1 */ |
| 1082 | |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1083 | if (tv1->tv_sec == tv2->tv_sec) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1084 | if (tv1->tv_usec > tv2->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1085 | return 1; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1086 | else if (tv2->tv_usec > tv1->tv_usec + 1000) |
willy tarreau | efae184 | 2005-12-17 12:51:03 +0100 | [diff] [blame] | 1087 | return -1; |
| 1088 | else |
| 1089 | return 0; |
| 1090 | } |
| 1091 | else if ((tv1->tv_sec > tv2->tv_sec + 1) || |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1092 | ((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] | 1093 | return 1; |
| 1094 | else if ((tv2->tv_sec > tv1->tv_sec + 1) || |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1095 | ((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] | 1096 | return -1; |
| 1097 | else |
| 1098 | return 0; |
| 1099 | } |
| 1100 | |
| 1101 | /* |
| 1102 | * returns the first event between tv1 and tv2 into tvmin. |
| 1103 | * a zero tv is ignored. tvmin is returned. |
| 1104 | */ |
| 1105 | static inline struct timeval *tv_min(struct timeval *tvmin, |
| 1106 | struct timeval *tv1, struct timeval *tv2) { |
| 1107 | |
| 1108 | if (tv_cmp2(tv1, tv2) <= 0) |
| 1109 | *tvmin = *tv1; |
| 1110 | else |
| 1111 | *tvmin = *tv2; |
| 1112 | |
| 1113 | return tvmin; |
| 1114 | } |
| 1115 | |
| 1116 | |
| 1117 | |
| 1118 | /***********************************************************/ |
| 1119 | /* fd management ***************************************/ |
| 1120 | /***********************************************************/ |
| 1121 | |
| 1122 | |
| 1123 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1124 | /* Deletes an FD from the fdsets, and recomputes the maxfd limit. |
| 1125 | * The file descriptor is also closed. |
| 1126 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1127 | static inline void fd_delete(int fd) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1128 | FD_CLR(fd, StaticReadEvent); |
| 1129 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1130 | close(fd); |
| 1131 | fdtab[fd].state = FD_STCLOSE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1132 | |
| 1133 | while ((maxfd-1 >= 0) && (fdtab[maxfd-1].state == FD_STCLOSE)) |
| 1134 | maxfd--; |
| 1135 | } |
| 1136 | |
| 1137 | /* recomputes the maxfd limit from the fd */ |
| 1138 | static inline void fd_insert(int fd) { |
| 1139 | if (fd+1 > maxfd) |
| 1140 | maxfd = fd+1; |
| 1141 | } |
| 1142 | |
| 1143 | /*************************************************************/ |
| 1144 | /* task management ***************************************/ |
| 1145 | /*************************************************************/ |
| 1146 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1147 | /* puts the task <t> in run queue <q>, and returns <t> */ |
| 1148 | static inline struct task *task_wakeup(struct task **q, struct task *t) { |
| 1149 | if (t->state == TASK_RUNNING) |
| 1150 | return t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1151 | else { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1152 | t->rqnext = *q; |
| 1153 | t->state = TASK_RUNNING; |
| 1154 | return *q = t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1158 | /* removes the task <t> from the queue <q> |
| 1159 | * <s> MUST be <q>'s first task. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1160 | * set the run queue to point to the next one, and return it |
| 1161 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1162 | static inline struct task *task_sleep(struct task **q, struct task *t) { |
| 1163 | if (t->state == TASK_RUNNING) { |
| 1164 | *q = t->rqnext; |
| 1165 | t->state = TASK_IDLE; /* tell that s has left the run queue */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1166 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1167 | return *q; /* return next running task */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1171 | * 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] | 1172 | * from the run queue. A pointer to the task itself is returned. |
| 1173 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1174 | static inline struct task *task_delete(struct task *t) { |
| 1175 | t->prev->next = t->next; |
| 1176 | t->next->prev = t->prev; |
| 1177 | return t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1181 | * 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] | 1182 | */ |
| 1183 | static inline void task_free(struct task *t) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1184 | pool_free(task, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1185 | } |
| 1186 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1187 | /* 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] | 1188 | * may be only moved or left where it was, depending on its timing requirements. |
| 1189 | * <task> is returned. |
| 1190 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1191 | struct task *task_queue(struct task *task) { |
| 1192 | struct task *list = task->wq; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1193 | struct task *start_from; |
| 1194 | |
| 1195 | /* first, test if the task was already in a list */ |
| 1196 | if (task->prev == NULL) { |
| 1197 | // start_from = list; |
| 1198 | start_from = list->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1199 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1200 | stats_tsk_new++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1201 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1202 | /* insert the unlinked <task> into the list, searching back from the last entry */ |
| 1203 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1204 | start_from = start_from->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1205 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1206 | stats_tsk_nsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1207 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | // while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1211 | // start_from = start_from->next; |
| 1212 | // stats_tsk_nsrch++; |
| 1213 | // } |
| 1214 | } |
| 1215 | else if (task->prev == list || |
| 1216 | tv_cmp2(&task->expire, &task->prev->expire) >= 0) { /* walk right */ |
| 1217 | start_from = task->next; |
| 1218 | if (start_from == list || tv_cmp2(&task->expire, &start_from->expire) <= 0) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1219 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1220 | stats_tsk_good++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1221 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1222 | return task; /* it's already in the right place */ |
| 1223 | } |
| 1224 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1225 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1226 | stats_tsk_right++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1227 | #endif |
| 1228 | |
| 1229 | /* if the task is not at the right place, there's little chance that |
| 1230 | * it has only shifted a bit, and it will nearly always be queued |
| 1231 | * at the end of the list because of constant timeouts |
| 1232 | * (observed in real case). |
| 1233 | */ |
| 1234 | #ifndef WE_REALLY_THINK_THAT_THIS_TASK_MAY_HAVE_SHIFTED |
| 1235 | start_from = list->prev; /* assume we'll queue to the end of the list */ |
| 1236 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1237 | start_from = start_from->prev; |
| 1238 | #if STATTIME > 0 |
| 1239 | stats_tsk_lsrch++; |
| 1240 | #endif |
| 1241 | } |
| 1242 | #else /* WE_REALLY_... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1243 | /* insert the unlinked <task> into the list, searching after position <start_from> */ |
| 1244 | while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1245 | start_from = start_from->next; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1246 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1247 | stats_tsk_rsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1248 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1249 | } |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1250 | #endif /* WE_REALLY_... */ |
| 1251 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1252 | /* we need to unlink it now */ |
| 1253 | task_delete(task); |
| 1254 | } |
| 1255 | else { /* walk left. */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1256 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1257 | stats_tsk_left++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1258 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1259 | #ifdef LEFT_TO_TOP /* not very good */ |
| 1260 | start_from = list; |
| 1261 | while (start_from->next != list && tv_cmp2(&task->expire, &start_from->next->expire) > 0) { |
| 1262 | start_from = start_from->next; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1263 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1264 | stats_tsk_lsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1265 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1266 | } |
| 1267 | #else |
| 1268 | start_from = task->prev->prev; /* valid because of the previous test above */ |
| 1269 | while (start_from != list && tv_cmp2(&task->expire, &start_from->expire) < 0) { |
| 1270 | start_from = start_from->prev; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1271 | #if STATTIME > 0 |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1272 | stats_tsk_lsrch++; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 1273 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1274 | } |
| 1275 | #endif |
| 1276 | /* we need to unlink it now */ |
| 1277 | task_delete(task); |
| 1278 | } |
| 1279 | task->prev = start_from; |
| 1280 | task->next = start_from->next; |
| 1281 | task->next->prev = task; |
| 1282 | start_from->next = task; |
| 1283 | return task; |
| 1284 | } |
| 1285 | |
| 1286 | |
| 1287 | /*********************************************************************/ |
| 1288 | /* more specific functions ***************************************/ |
| 1289 | /*********************************************************************/ |
| 1290 | |
| 1291 | /* some prototypes */ |
| 1292 | static int maintain_proxies(void); |
| 1293 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1294 | /* this either returns the sockname or the original destination address. Code |
| 1295 | * inspired from Patrick Schaaf's example of nf_getsockname() implementation. |
| 1296 | */ |
| 1297 | 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] | 1298 | #if defined(TPROXY) && defined(SO_ORIGINAL_DST) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1299 | return getsockopt(fd, SOL_IP, SO_ORIGINAL_DST, (void *)sa, salen); |
| 1300 | #else |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1301 | #if defined(TPROXY) && defined(USE_GETSOCKNAME) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1302 | return getsockname(fd, (struct sockaddr *)sa, salen); |
| 1303 | #else |
| 1304 | return -1; |
| 1305 | #endif |
| 1306 | #endif |
| 1307 | } |
| 1308 | |
| 1309 | /* |
| 1310 | * frees the context associated to a session. It must have been removed first. |
| 1311 | */ |
| 1312 | static inline void session_free(struct session *s) { |
| 1313 | if (s->req) |
| 1314 | pool_free(buffer, s->req); |
| 1315 | if (s->rep) |
| 1316 | pool_free(buffer, s->rep); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1317 | if (s->logs.uri) |
| 1318 | pool_free(requri, s->logs.uri); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1319 | if (s->logs.cli_cookie) |
| 1320 | pool_free(capture, s->logs.cli_cookie); |
| 1321 | if (s->logs.srv_cookie) |
| 1322 | pool_free(capture, s->logs.srv_cookie); |
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 | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1329 | * This function tries to find a running server for the proxy <px>. A first |
| 1330 | * pass looks for active servers, and if none is found, a second pass also |
| 1331 | * looks for backup servers. |
| 1332 | * If no valid server is found, NULL is returned and px->cursrv is left undefined. |
| 1333 | */ |
| 1334 | static inline struct server *find_server(struct proxy *px) { |
| 1335 | struct server *srv = px->cursrv; |
| 1336 | int ignore_backup = 1; |
| 1337 | |
| 1338 | do { |
| 1339 | do { |
| 1340 | if (srv == NULL) |
| 1341 | srv = px->srv; |
| 1342 | if (srv->state & SRV_RUNNING |
| 1343 | && !((srv->state & SRV_BACKUP) && ignore_backup)) |
| 1344 | return srv; |
| 1345 | srv = srv->next; |
| 1346 | } while (srv != px->cursrv); |
| 1347 | } while (ignore_backup--); |
| 1348 | return NULL; |
| 1349 | } |
| 1350 | |
| 1351 | /* |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1352 | * This function initiates a connection to the current server (s->srv) if (s->direct) |
| 1353 | * 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] | 1354 | * it's OK, -1 if it's impossible. |
| 1355 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1356 | int connect_server(struct session *s) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1357 | int one = 1; |
| 1358 | int fd; |
| 1359 | |
| 1360 | // fprintf(stderr,"connect_server : s=%p\n",s); |
| 1361 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 1362 | if (s->flags & SN_DIRECT) { /* srv cannot be null */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1363 | s->srv_addr = s->srv->addr; |
| 1364 | } |
| 1365 | else if (s->proxy->options & PR_O_BALANCE) { |
| 1366 | if (s->proxy->options & PR_O_BALANCE_RR) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1367 | struct server *srv; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1368 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1369 | srv = find_server(s->proxy); |
| 1370 | |
| 1371 | if (srv == NULL) /* no server left */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1372 | return -1; |
| 1373 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1374 | s->srv_addr = srv->addr; |
| 1375 | s->srv = srv; |
| 1376 | s->proxy->cursrv = srv->next; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1377 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1378 | else /* unknown balancing algorithm */ |
| 1379 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1380 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1381 | else if (*(int *)&s->proxy->dispatch_addr.sin_addr) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1382 | /* connect to the defined dispatch addr */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1383 | s->srv_addr = s->proxy->dispatch_addr; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1384 | } |
| 1385 | else if (s->proxy->options & PR_O_TRANSP) { |
| 1386 | /* in transparent mode, use the original dest addr if no dispatch specified */ |
| 1387 | int salen = sizeof(struct sockaddr_in); |
| 1388 | if (get_original_dst(s->cli_fd, &s->srv_addr, &salen) == -1) { |
| 1389 | qfprintf(stderr, "Cannot get original server address.\n"); |
| 1390 | return -1; |
| 1391 | } |
| 1392 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1393 | |
| 1394 | 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] | 1395 | qfprintf(stderr, "Cannot get a server socket.\n"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1396 | return -1; |
| 1397 | } |
| 1398 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1399 | if (fd >= global.maxsock) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1400 | Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 1401 | close(fd); |
| 1402 | return -1; |
| 1403 | } |
| 1404 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1405 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
| 1406 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1407 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1408 | close(fd); |
| 1409 | return -1; |
| 1410 | } |
| 1411 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1412 | /* allow specific binding */ |
| 1413 | if (s->proxy->options & PR_O_BIND_SRC && |
| 1414 | bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) { |
| 1415 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", s->proxy->id); |
| 1416 | close(fd); |
| 1417 | return -1; |
| 1418 | } |
| 1419 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1420 | if ((connect(fd, (struct sockaddr *)&s->srv_addr, sizeof(s->srv_addr)) == -1) && (errno != EINPROGRESS)) { |
| 1421 | if (errno == EAGAIN) { /* no free ports left, try again later */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1422 | qfprintf(stderr,"Cannot connect, no free ports.\n"); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1423 | close(fd); |
| 1424 | return -1; |
| 1425 | } |
| 1426 | else if (errno != EALREADY && errno != EISCONN) { |
| 1427 | close(fd); |
| 1428 | return -1; |
| 1429 | } |
| 1430 | } |
| 1431 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1432 | fdtab[fd].owner = s->task; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1433 | fdtab[fd].read = &event_srv_read; |
| 1434 | fdtab[fd].write = &event_srv_write; |
| 1435 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 1436 | |
| 1437 | FD_SET(fd, StaticWriteEvent); /* for connect status */ |
| 1438 | |
| 1439 | fd_insert(fd); |
| 1440 | |
| 1441 | if (s->proxy->contimeout) |
| 1442 | tv_delayfrom(&s->cnexpire, &now, s->proxy->contimeout); |
| 1443 | else |
| 1444 | tv_eternity(&s->cnexpire); |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | /* |
| 1449 | * this function is called on a read event from a client socket. |
| 1450 | * It returns 0. |
| 1451 | */ |
| 1452 | int event_cli_read(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1453 | struct task *t = fdtab[fd].owner; |
| 1454 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1455 | struct buffer *b = s->req; |
| 1456 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1457 | |
| 1458 | // fprintf(stderr,"event_cli_read : fd=%d, s=%p\n", fd, s); |
| 1459 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1460 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1461 | while (1) { |
| 1462 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 1463 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1464 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1465 | } |
| 1466 | else if (b->r > b->w) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1467 | max = b->rlim - b->r; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1468 | } |
| 1469 | else { |
| 1470 | max = b->w - b->r; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1471 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 1472 | * since it means that the rewrite protection has been removed. This |
| 1473 | * implies that the if statement can be removed. |
| 1474 | */ |
| 1475 | if (max > b->rlim - b->data) |
| 1476 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | if (max == 0) { /* not anymore room to store data */ |
| 1480 | FD_CLR(fd, StaticReadEvent); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1481 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1482 | } |
| 1483 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1484 | #ifndef MSG_NOSIGNAL |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1485 | { |
| 1486 | int skerr, lskerr; |
| 1487 | |
| 1488 | lskerr = sizeof(skerr); |
| 1489 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 1490 | if (skerr) |
| 1491 | ret = -1; |
| 1492 | else |
| 1493 | ret = recv(fd, b->r, max, 0); |
| 1494 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1495 | #else |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1496 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1497 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1498 | if (ret > 0) { |
| 1499 | b->r += ret; |
| 1500 | b->l += ret; |
| 1501 | s->res_cr = RES_DATA; |
| 1502 | |
| 1503 | if (b->r == b->data + BUFSIZE) { |
| 1504 | b->r = b->data; /* wrap around the buffer */ |
| 1505 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1506 | |
| 1507 | b->total += ret; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1508 | /* we hope to read more data or to get a close on next round */ |
| 1509 | continue; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1510 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1511 | else if (ret == 0) { |
| 1512 | s->res_cr = RES_NULL; |
| 1513 | break; |
| 1514 | } |
| 1515 | else if (errno == EAGAIN) {/* ignore EAGAIN */ |
| 1516 | break; |
| 1517 | } |
| 1518 | else { |
| 1519 | s->res_cr = RES_ERROR; |
| 1520 | fdtab[fd].state = FD_STERROR; |
| 1521 | break; |
| 1522 | } |
| 1523 | } /* while(1) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1524 | } |
| 1525 | else { |
| 1526 | s->res_cr = RES_ERROR; |
| 1527 | fdtab[fd].state = FD_STERROR; |
| 1528 | } |
| 1529 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1530 | if (s->res_cr != RES_SILENT) { |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 1531 | if (s->proxy->clitimeout && FD_ISSET(fd, StaticReadEvent)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1532 | tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout); |
| 1533 | else |
| 1534 | tv_eternity(&s->crexpire); |
| 1535 | |
| 1536 | task_wakeup(&rq, t); |
| 1537 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1538 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1539 | return 0; |
| 1540 | } |
| 1541 | |
| 1542 | |
| 1543 | /* |
| 1544 | * this function is called on a read event from a server socket. |
| 1545 | * It returns 0. |
| 1546 | */ |
| 1547 | int event_srv_read(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1548 | struct task *t = fdtab[fd].owner; |
| 1549 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1550 | struct buffer *b = s->rep; |
| 1551 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1552 | |
| 1553 | // fprintf(stderr,"event_srv_read : fd=%d, s=%p\n", fd, s); |
| 1554 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1555 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1556 | while (1) { |
| 1557 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 1558 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1559 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1560 | } |
| 1561 | else if (b->r > b->w) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1562 | max = b->rlim - b->r; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1563 | } |
| 1564 | else { |
| 1565 | max = b->w - b->r; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1566 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 1567 | * since it means that the rewrite protection has been removed. This |
| 1568 | * implies that the if statement can be removed. |
| 1569 | */ |
| 1570 | if (max > b->rlim - b->data) |
| 1571 | max = b->rlim - b->data; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | if (max == 0) { /* not anymore room to store data */ |
| 1575 | FD_CLR(fd, StaticReadEvent); |
| 1576 | break; |
| 1577 | } |
| 1578 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1579 | #ifndef MSG_NOSIGNAL |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1580 | { |
| 1581 | int skerr, lskerr; |
| 1582 | |
| 1583 | lskerr = sizeof(skerr); |
| 1584 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 1585 | if (skerr) |
| 1586 | ret = -1; |
| 1587 | else |
| 1588 | ret = recv(fd, b->r, max, 0); |
| 1589 | } |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1590 | #else |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1591 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1592 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1593 | if (ret > 0) { |
| 1594 | b->r += ret; |
| 1595 | b->l += ret; |
| 1596 | s->res_sr = RES_DATA; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1597 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1598 | if (b->r == b->data + BUFSIZE) { |
| 1599 | b->r = b->data; /* wrap around the buffer */ |
| 1600 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1601 | |
| 1602 | b->total += ret; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1603 | /* we hope to read more data or to get a close on next round */ |
| 1604 | continue; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1605 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1606 | else if (ret == 0) { |
| 1607 | s->res_sr = RES_NULL; |
| 1608 | break; |
| 1609 | } |
| 1610 | else if (errno == EAGAIN) {/* ignore EAGAIN */ |
| 1611 | break; |
| 1612 | } |
| 1613 | else { |
| 1614 | s->res_sr = RES_ERROR; |
| 1615 | fdtab[fd].state = FD_STERROR; |
| 1616 | break; |
| 1617 | } |
| 1618 | } /* while(1) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1619 | } |
| 1620 | else { |
| 1621 | s->res_sr = RES_ERROR; |
| 1622 | fdtab[fd].state = FD_STERROR; |
| 1623 | } |
| 1624 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1625 | if (s->res_sr != RES_SILENT) { |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 1626 | if (s->proxy->srvtimeout && FD_ISSET(fd, StaticReadEvent)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1627 | tv_delayfrom(&s->srexpire, &now, s->proxy->srvtimeout); |
| 1628 | else |
| 1629 | tv_eternity(&s->srexpire); |
| 1630 | |
| 1631 | task_wakeup(&rq, t); |
| 1632 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1633 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1634 | return 0; |
| 1635 | } |
| 1636 | |
| 1637 | /* |
| 1638 | * this function is called on a write event from a client socket. |
| 1639 | * It returns 0. |
| 1640 | */ |
| 1641 | int event_cli_write(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1642 | struct task *t = fdtab[fd].owner; |
| 1643 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1644 | struct buffer *b = s->rep; |
| 1645 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1646 | |
| 1647 | // fprintf(stderr,"event_cli_write : fd=%d, s=%p\n", fd, s); |
| 1648 | |
| 1649 | 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] | 1650 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1651 | // max = BUFSIZE; BUG !!!! |
| 1652 | max = 0; |
| 1653 | } |
| 1654 | else if (b->r > b->w) { |
| 1655 | max = b->r - b->w; |
| 1656 | } |
| 1657 | else |
| 1658 | max = b->data + BUFSIZE - b->w; |
| 1659 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1660 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1661 | #ifndef MSG_NOSIGNAL |
| 1662 | int skerr, lskerr; |
| 1663 | #endif |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1664 | |
| 1665 | if (max == 0) { |
| 1666 | s->res_cw = RES_NULL; |
| 1667 | task_wakeup(&rq, t); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 1668 | tv_eternity(&s->cwexpire); |
| 1669 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1670 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1671 | } |
| 1672 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1673 | #ifndef MSG_NOSIGNAL |
| 1674 | lskerr=sizeof(skerr); |
| 1675 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 1676 | if (skerr) |
| 1677 | ret = -1; |
| 1678 | else |
| 1679 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
| 1680 | #else |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1681 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1682 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1683 | |
| 1684 | if (ret > 0) { |
| 1685 | b->l -= ret; |
| 1686 | b->w += ret; |
| 1687 | |
| 1688 | s->res_cw = RES_DATA; |
| 1689 | |
| 1690 | if (b->w == b->data + BUFSIZE) { |
| 1691 | b->w = b->data; /* wrap around the buffer */ |
| 1692 | } |
| 1693 | } |
| 1694 | else if (ret == 0) { |
| 1695 | /* nothing written, just make as if we were never called */ |
| 1696 | // s->res_cw = RES_NULL; |
| 1697 | return 0; |
| 1698 | } |
| 1699 | else if (errno == EAGAIN) /* ignore EAGAIN */ |
| 1700 | return 0; |
| 1701 | else { |
| 1702 | s->res_cw = RES_ERROR; |
| 1703 | fdtab[fd].state = FD_STERROR; |
| 1704 | } |
| 1705 | } |
| 1706 | else { |
| 1707 | s->res_cw = RES_ERROR; |
| 1708 | fdtab[fd].state = FD_STERROR; |
| 1709 | } |
| 1710 | |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 1711 | if (s->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1712 | tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 1713 | /* FIXME: to avoid the client to read-time-out during writes, we refresh it */ |
| 1714 | s->crexpire = s->cwexpire; |
| 1715 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1716 | else |
| 1717 | tv_eternity(&s->cwexpire); |
| 1718 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1719 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1720 | return 0; |
| 1721 | } |
| 1722 | |
| 1723 | |
| 1724 | /* |
| 1725 | * this function is called on a write event from a server socket. |
| 1726 | * It returns 0. |
| 1727 | */ |
| 1728 | int event_srv_write(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1729 | struct task *t = fdtab[fd].owner; |
| 1730 | struct session *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1731 | struct buffer *b = s->req; |
| 1732 | int ret, max; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1733 | |
| 1734 | //fprintf(stderr,"event_srv_write : fd=%d, s=%p\n", fd, s); |
| 1735 | |
| 1736 | 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] | 1737 | b->r = b->w = b->h = b->lr = b->data; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1738 | // max = BUFSIZE; BUG !!!! |
| 1739 | max = 0; |
| 1740 | } |
| 1741 | else if (b->r > b->w) { |
| 1742 | max = b->r - b->w; |
| 1743 | } |
| 1744 | else |
| 1745 | max = b->data + BUFSIZE - b->w; |
| 1746 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1747 | if (fdtab[fd].state != FD_STERROR) { |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1748 | #ifndef MSG_NOSIGNAL |
| 1749 | int skerr, lskerr; |
| 1750 | #endif |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1751 | if (max == 0) { |
| 1752 | /* 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] | 1753 | s->res_sw = RES_NULL; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1754 | task_wakeup(&rq, t); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1755 | fdtab[fd].state = FD_STREADY; |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 1756 | tv_eternity(&s->swexpire); |
| 1757 | FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1758 | return 0; |
| 1759 | } |
| 1760 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1761 | |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1762 | #ifndef MSG_NOSIGNAL |
| 1763 | lskerr=sizeof(skerr); |
| 1764 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 1765 | if (skerr) |
| 1766 | ret = -1; |
| 1767 | else |
| 1768 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
| 1769 | #else |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1770 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 1771 | #endif |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 1772 | fdtab[fd].state = FD_STREADY; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1773 | if (ret > 0) { |
| 1774 | b->l -= ret; |
| 1775 | b->w += ret; |
| 1776 | |
| 1777 | s->res_sw = RES_DATA; |
| 1778 | |
| 1779 | if (b->w == b->data + BUFSIZE) { |
| 1780 | b->w = b->data; /* wrap around the buffer */ |
| 1781 | } |
| 1782 | } |
| 1783 | else if (ret == 0) { |
| 1784 | /* nothing written, just make as if we were never called */ |
| 1785 | // s->res_sw = RES_NULL; |
| 1786 | return 0; |
| 1787 | } |
| 1788 | else if (errno == EAGAIN) /* ignore EAGAIN */ |
| 1789 | return 0; |
| 1790 | else { |
| 1791 | s->res_sw = RES_ERROR; |
| 1792 | fdtab[fd].state = FD_STERROR; |
| 1793 | } |
| 1794 | } |
| 1795 | else { |
| 1796 | s->res_sw = RES_ERROR; |
| 1797 | fdtab[fd].state = FD_STERROR; |
| 1798 | } |
| 1799 | |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 1800 | if (s->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1801 | tv_delayfrom(&s->swexpire, &now, s->proxy->srvtimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 1802 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 1803 | s->srexpire = s->swexpire; |
| 1804 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1805 | else |
| 1806 | tv_eternity(&s->swexpire); |
| 1807 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1808 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1809 | return 0; |
| 1810 | } |
| 1811 | |
| 1812 | |
| 1813 | /* |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 1814 | * returns a message to the client ; the connection is shut down for read, |
| 1815 | * and the request is cleared so that no server connection can be initiated. |
| 1816 | * The client must be in a valid state for this (HEADER, DATA ...). |
| 1817 | * Nothing is performed on the server side. |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1818 | * The reply buffer doesn't need to be empty before this. |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 1819 | */ |
| 1820 | void client_retnclose(struct session *s, int len, const char *msg) { |
| 1821 | FD_CLR(s->cli_fd, StaticReadEvent); |
| 1822 | FD_SET(s->cli_fd, StaticWriteEvent); |
| 1823 | tv_eternity(&s->crexpire); |
| 1824 | shutdown(s->cli_fd, SHUT_RD); |
| 1825 | s->cli_state = CL_STSHUTR; |
| 1826 | strcpy(s->rep->data, msg); |
| 1827 | s->rep->l = len; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1828 | s->rep->r = s->rep->h = s->rep->lr = s->rep->w = s->rep->data; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 1829 | s->rep->r += len; |
| 1830 | s->req->l = 0; |
| 1831 | } |
| 1832 | |
| 1833 | |
| 1834 | /* |
| 1835 | * returns a message into the rep buffer, and flushes the req buffer. |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1836 | * The reply buffer doesn't need to be empty before this. |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 1837 | */ |
| 1838 | void client_return(struct session *s, int len, const char *msg) { |
| 1839 | strcpy(s->rep->data, msg); |
| 1840 | s->rep->l = len; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1841 | s->rep->r = s->rep->h = s->rep->lr = s->rep->w = s->rep->data; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 1842 | s->rep->r += len; |
| 1843 | s->req->l = 0; |
| 1844 | } |
| 1845 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1846 | /* |
| 1847 | * send a log for the session when we have enough info about it |
| 1848 | */ |
| 1849 | void sess_log(struct session *s) { |
| 1850 | unsigned char *pn; |
| 1851 | struct proxy *p = s->proxy; |
| 1852 | int log; |
| 1853 | char *uri; |
| 1854 | char *pxid; |
| 1855 | char *srv; |
| 1856 | |
| 1857 | /* This is a first attempt at a better logging system. |
| 1858 | * For now, we rely on send_log() to provide the date, although it obviously |
| 1859 | * is the date of the log and not of the request, and most fields are not |
| 1860 | * computed. |
| 1861 | */ |
| 1862 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1863 | log = p->to_log & ~s->logs.logwait; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1864 | |
| 1865 | pn = (log & LW_CLIP) ? |
| 1866 | (unsigned char *)&s->cli_addr.sin_addr : |
| 1867 | (unsigned char *)"\0\0\0\0"; |
| 1868 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1869 | uri = (log & LW_REQ) ? s->logs.uri : "<BADREQ>"; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1870 | pxid = p->id; |
| 1871 | //srv = (log & LW_SVID) ? s->srv->id : "<svid>"; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1872 | srv = ((p->to_log & LW_SVID) && s->srv != NULL) ? s->srv->id : "<NOSRV>"; |
| 1873 | |
| 1874 | if (p->to_log & LW_DATE) { |
| 1875 | struct tm *tm = localtime(&s->logs.tv_accept.tv_sec); |
| 1876 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 1877 | 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 %s %c%c%c%c \"%s\"\n", |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1878 | pn[0], pn[1], pn[2], pn[3], ntohs(s->cli_addr.sin_port), |
| 1879 | tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900, |
| 1880 | tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 1881 | pxid, srv, |
| 1882 | s->logs.t_request, |
| 1883 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_request : -1, |
| 1884 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1885 | s->logs.t_close, |
| 1886 | s->logs.status, s->logs.bytes, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1887 | s->logs.cli_cookie ? s->logs.cli_cookie : "-", |
| 1888 | s->logs.srv_cookie ? s->logs.srv_cookie : "-", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 1889 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1890 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1891 | (p->options & PR_O_COOK_ANY) ? sess_cookie[(s->flags & SN_CK_MASK) >> SN_CK_SHIFT] : '-', |
| 1892 | (p->options & PR_O_COOK_ANY) ? sess_set_cookie[(s->flags & SN_SCK_MASK) >> SN_SCK_SHIFT] : '-', |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1893 | uri); |
| 1894 | } |
| 1895 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 1896 | send_log(p, LOG_INFO, "%d.%d.%d.%d:%d %s %s %d/%d/%d/%d %d %lld %s %s %c%c%c%c \"%s\"\n", |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1897 | pn[0], pn[1], pn[2], pn[3], ntohs(s->cli_addr.sin_port), |
| 1898 | pxid, srv, |
| 1899 | s->logs.t_request, |
| 1900 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_request : -1, |
| 1901 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1902 | s->logs.t_close, |
| 1903 | s->logs.status, s->logs.bytes, |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1904 | s->logs.cli_cookie ? s->logs.cli_cookie : "-", |
| 1905 | s->logs.srv_cookie ? s->logs.srv_cookie : "-", |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 1906 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1907 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1908 | (p->options & PR_O_COOK_ANY) ? sess_cookie[(s->flags & SN_CK_MASK) >> SN_CK_SHIFT] : '-', |
| 1909 | (p->options & PR_O_COOK_ANY) ? sess_set_cookie[(s->flags & SN_SCK_MASK) >> SN_SCK_SHIFT] : '-', |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1910 | uri); |
| 1911 | } |
| 1912 | |
| 1913 | s->logs.logwait = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1914 | } |
| 1915 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 1916 | |
| 1917 | /* |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1918 | * 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] | 1919 | * to an accept. It tries to accept as many connections as possible. |
| 1920 | * It returns 0. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1921 | */ |
| 1922 | int event_accept(int fd) { |
| 1923 | struct proxy *p = (struct proxy *)fdtab[fd].owner; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1924 | struct session *s; |
| 1925 | struct task *t; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1926 | int cfd; |
| 1927 | int one = 1; |
| 1928 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1929 | while (p->nbconn < p->maxconn) { |
| 1930 | struct sockaddr_in addr; |
| 1931 | int laddr = sizeof(addr); |
| 1932 | if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) |
| 1933 | return 0; /* nothing more to accept */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1934 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1935 | if ((s = pool_alloc(session)) == NULL) { /* disable this proxy for a while */ |
| 1936 | Alert("out of memory in event_accept().\n"); |
| 1937 | FD_CLR(fd, StaticReadEvent); |
| 1938 | p->state = PR_STIDLE; |
| 1939 | close(cfd); |
| 1940 | return 0; |
| 1941 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1942 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1943 | if ((t = pool_alloc(task)) == NULL) { /* disable this proxy for a while */ |
| 1944 | Alert("out of memory in event_accept().\n"); |
| 1945 | FD_CLR(fd, StaticReadEvent); |
| 1946 | p->state = PR_STIDLE; |
| 1947 | close(cfd); |
| 1948 | pool_free(session, s); |
| 1949 | return 0; |
| 1950 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1951 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1952 | s->cli_addr = addr; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1953 | if (cfd >= global.maxsock) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1954 | Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 1955 | close(cfd); |
| 1956 | pool_free(task, t); |
| 1957 | pool_free(session, s); |
| 1958 | return 0; |
| 1959 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1960 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 1961 | if ((fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) || |
| 1962 | (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, |
| 1963 | (char *) &one, sizeof(one)) == -1)) { |
| 1964 | Alert("accept(): cannot set the socket in non blocking mode. Giving up\n"); |
| 1965 | close(cfd); |
| 1966 | pool_free(task, t); |
| 1967 | pool_free(session, s); |
| 1968 | return 0; |
| 1969 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 1970 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1971 | t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */ |
| 1972 | t->wq = LIST_HEAD(wait_queue); /* but already has a wait queue assigned */ |
| 1973 | t->state = TASK_IDLE; |
| 1974 | t->process = process_session; |
| 1975 | t->context = s; |
| 1976 | |
| 1977 | s->task = t; |
| 1978 | s->proxy = p; |
| 1979 | s->cli_state = (p->mode == PR_MODE_HTTP) ? CL_STHEADERS : CL_STDATA; /* no HTTP headers for non-HTTP proxies */ |
| 1980 | s->srv_state = SV_STIDLE; |
| 1981 | s->req = s->rep = NULL; /* will be allocated later */ |
| 1982 | s->flags = 0; |
| 1983 | s->res_cr = s->res_cw = s->res_sr = s->res_sw = RES_SILENT; |
| 1984 | s->cli_fd = cfd; |
| 1985 | s->srv_fd = -1; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1986 | s->srv = NULL; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 1987 | s->conn_retries = p->conn_retries; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1988 | |
| 1989 | s->logs.logwait = p->to_log; |
| 1990 | s->logs.tv_accept = now; |
| 1991 | s->logs.t_request = -1; |
| 1992 | s->logs.t_connect = -1; |
| 1993 | s->logs.t_data = -1; |
| 1994 | s->logs.t_close = 0; |
| 1995 | s->logs.uri = NULL; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 1996 | s->logs.cli_cookie = NULL; |
| 1997 | s->logs.srv_cookie = NULL; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 1998 | s->logs.status = -1; |
| 1999 | s->logs.bytes = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2000 | |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame^] | 2001 | s->uniq_id = totalconn; |
| 2002 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2003 | if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP) |
| 2004 | && (p->logfac1 >= 0 || p->logfac2 >= 0)) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 2005 | struct sockaddr_in sockname; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2006 | unsigned char *pn, *sn; |
| 2007 | int namelen; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2008 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2009 | namelen = sizeof(sockname); |
| 2010 | if (get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
| 2011 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
| 2012 | sn = (unsigned char *)&sockname.sin_addr; |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 2013 | pn = (unsigned char *)&s->cli_addr.sin_addr; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2014 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2015 | if (p->to_log) { |
| 2016 | /* we have the client ip */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2017 | if (s->logs.logwait & LW_CLIP) |
| 2018 | if (!(s->logs.logwait &= ~LW_CLIP)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2019 | sess_log(s); |
| 2020 | } |
| 2021 | else |
| 2022 | send_log(p, LOG_INFO, "Connect from %d.%d.%d.%d:%d to %d.%d.%d.%d:%d (%s/%s)\n", |
| 2023 | pn[0], pn[1], pn[2], pn[3], ntohs(s->cli_addr.sin_port), |
| 2024 | sn[0], sn[1], sn[2], sn[3], ntohs(sockname.sin_port), |
| 2025 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2026 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2027 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2028 | if ((global.mode & MODE_DEBUG) && !(global.mode & MODE_QUIET)) { |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame^] | 2029 | struct sockaddr_in sockname; |
| 2030 | unsigned char *pn, *sn; |
| 2031 | int namelen; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2032 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame^] | 2033 | |
| 2034 | namelen = sizeof(sockname); |
| 2035 | if (get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
| 2036 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
| 2037 | sn = (unsigned char *)&sockname.sin_addr; |
| 2038 | pn = (unsigned char *)&s->cli_addr.sin_addr; |
| 2039 | |
| 2040 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%d.%d.%d.%d:%d]\n", |
| 2041 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 2042 | pn[0], pn[1], pn[2], pn[3], ntohs(s->cli_addr.sin_port)); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2043 | write(1, trash, len); |
| 2044 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2045 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2046 | if ((s->req = pool_alloc(buffer)) == NULL) { /* no memory */ |
| 2047 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2048 | pool_free(task, t); |
| 2049 | pool_free(session, s); |
| 2050 | return 0; |
| 2051 | } |
| 2052 | s->req->l = 0; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2053 | s->req->total = 0; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2054 | s->req->h = s->req->r = s->req->lr = s->req->w = s->req->data; /* r and w will be reset further */ |
| 2055 | s->req->rlim = s->req->data + BUFSIZE; |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2056 | if (s->cli_state == CL_STHEADERS) /* reserve some space for header rewriting */ |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2057 | s->req->rlim -= MAXREWRITE; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2058 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2059 | if ((s->rep = pool_alloc(buffer)) == NULL) { /* no memory */ |
| 2060 | pool_free(buffer, s->req); |
| 2061 | close(cfd); /* nothing can be done for this fd without memory */ |
| 2062 | pool_free(task, t); |
| 2063 | pool_free(session, s); |
| 2064 | return 0; |
| 2065 | } |
| 2066 | s->rep->l = 0; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2067 | s->rep->total = 0; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2068 | 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] | 2069 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2070 | fdtab[cfd].read = &event_cli_read; |
| 2071 | fdtab[cfd].write = &event_cli_write; |
| 2072 | fdtab[cfd].owner = t; |
| 2073 | fdtab[cfd].state = FD_STREADY; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2074 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2075 | if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2076 | client_retnclose(s, 3, "OK\n"); /* forge an "OK" response */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2077 | } |
| 2078 | else { |
| 2079 | FD_SET(cfd, StaticReadEvent); |
| 2080 | } |
| 2081 | |
| 2082 | fd_insert(cfd); |
| 2083 | |
| 2084 | tv_eternity(&s->cnexpire); |
| 2085 | tv_eternity(&s->srexpire); |
| 2086 | tv_eternity(&s->swexpire); |
| 2087 | tv_eternity(&s->cwexpire); |
| 2088 | |
| 2089 | if (s->proxy->clitimeout) |
| 2090 | tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout); |
| 2091 | else |
| 2092 | tv_eternity(&s->crexpire); |
| 2093 | |
| 2094 | t->expire = s->crexpire; |
| 2095 | |
| 2096 | task_queue(t); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2097 | |
| 2098 | if (p->mode != PR_MODE_HEALTH) |
| 2099 | task_wakeup(&rq, t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2100 | |
| 2101 | p->nbconn++; |
| 2102 | actconn++; |
| 2103 | totalconn++; |
| 2104 | |
| 2105 | // fprintf(stderr, "accepting from %p => %d conn, %d total\n", p, actconn, totalconn); |
| 2106 | } /* end of while (p->nbconn < p->maxconn) */ |
| 2107 | return 0; |
| 2108 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2109 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2110 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2111 | /* |
| 2112 | * This function is used only for server health-checks. It handles |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2113 | * the connection acknowledgement. If the proxy requires HTTP health-checks, |
| 2114 | * it sends the request. In other cases, it returns 1 if the socket is OK, |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2115 | * or -1 if an error occured. |
| 2116 | */ |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2117 | int event_srv_chk_w(int fd) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2118 | struct task *t = fdtab[fd].owner; |
| 2119 | struct server *s = t->context; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2120 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2121 | int skerr, lskerr; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2122 | lskerr = sizeof(skerr); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2123 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2124 | if (skerr) |
| 2125 | s->result = -1; |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2126 | else { |
| 2127 | if (s->proxy->options & PR_O_HTTP_CHK) { |
| 2128 | int ret; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame^] | 2129 | /* we want to check if this host replies to "OPTIONS / HTTP/1.0" |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2130 | * so we'll send the request, and won't wake the checker up now. |
| 2131 | */ |
| 2132 | #ifndef MSG_NOSIGNAL |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame^] | 2133 | ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT); |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2134 | #else |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame^] | 2135 | ret = send(fd, s->proxy->check_req, s->proxy->check_len, MSG_DONTWAIT | MSG_NOSIGNAL); |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2136 | #endif |
| 2137 | if (ret == 22) { |
| 2138 | FD_SET(fd, StaticReadEvent); /* prepare for reading reply */ |
| 2139 | FD_CLR(fd, StaticWriteEvent); /* nothing more to write */ |
| 2140 | return 0; |
| 2141 | } |
| 2142 | else |
| 2143 | s->result = -1; |
| 2144 | } |
| 2145 | else { |
| 2146 | /* good TCP connection is enough */ |
| 2147 | s->result = 1; |
| 2148 | } |
| 2149 | } |
| 2150 | |
| 2151 | task_wakeup(&rq, t); |
| 2152 | return 0; |
| 2153 | } |
| 2154 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2155 | |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 2156 | /* |
| 2157 | * This function is used only for server health-checks. It handles |
| 2158 | * the server's reply to an HTTP request. It returns 1 if the server replies |
| 2159 | * 2xx or 3xx (valid responses), or -1 in other cases. |
| 2160 | */ |
| 2161 | int event_srv_chk_r(int fd) { |
| 2162 | char reply[64]; |
| 2163 | int len; |
| 2164 | struct task *t = fdtab[fd].owner; |
| 2165 | struct server *s = t->context; |
| 2166 | |
| 2167 | int skerr, lskerr; |
| 2168 | lskerr = sizeof(skerr); |
| 2169 | getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 2170 | s->result = -1; |
| 2171 | if (!skerr) { |
| 2172 | #ifndef MSG_NOSIGNAL |
| 2173 | len = recv(fd, reply, sizeof(reply), 0); |
| 2174 | #else |
| 2175 | len = recv(fd, reply, sizeof(reply), MSG_NOSIGNAL); |
| 2176 | #endif |
| 2177 | if ((len >= sizeof("HTTP/1.0 000")) && |
| 2178 | !memcmp(reply, "HTTP/1.", 7) && |
| 2179 | (reply[9] == '2' || reply[9] == '3')) /* 2xx or 3xx */ |
| 2180 | s->result = 1; |
| 2181 | } |
| 2182 | |
| 2183 | FD_CLR(fd, StaticReadEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2184 | task_wakeup(&rq, t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2185 | return 0; |
| 2186 | } |
| 2187 | |
| 2188 | |
| 2189 | /* |
| 2190 | * this function writes the string <str> at position <pos> which must be in buffer <b>, |
| 2191 | * and moves <end> just after the end of <str>. |
| 2192 | * <b>'s parameters (l, r, w, h, lr) are recomputed to be valid after the shift. |
| 2193 | * the shift value (positive or negative) is returned. |
| 2194 | * If there's no space left, the move is not done. |
| 2195 | * |
| 2196 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2197 | int buffer_replace(struct buffer *b, char *pos, char *end, char *str) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2198 | int delta; |
| 2199 | int len; |
| 2200 | |
| 2201 | len = strlen(str); |
| 2202 | delta = len - (end - pos); |
| 2203 | |
| 2204 | if (delta + b->r >= b->data + BUFSIZE) |
| 2205 | return 0; /* no space left */ |
| 2206 | |
| 2207 | /* first, protect the end of the buffer */ |
| 2208 | memmove(end + delta, end, b->data + b->l - end); |
| 2209 | |
| 2210 | /* now, copy str over pos */ |
| 2211 | memcpy(pos, str,len); |
| 2212 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2213 | /* we only move data after the displaced zone */ |
| 2214 | if (b->r > pos) b->r += delta; |
| 2215 | if (b->w > pos) b->w += delta; |
| 2216 | if (b->h > pos) b->h += delta; |
| 2217 | if (b->lr > pos) b->lr += delta; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2218 | b->l += delta; |
| 2219 | |
| 2220 | return delta; |
| 2221 | } |
| 2222 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2223 | /* same except that the string length is given, which allows str to be NULL if |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2224 | * len is 0. |
| 2225 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2226 | 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] | 2227 | int delta; |
| 2228 | |
| 2229 | delta = len - (end - pos); |
| 2230 | |
| 2231 | if (delta + b->r >= b->data + BUFSIZE) |
| 2232 | return 0; /* no space left */ |
| 2233 | |
| 2234 | /* first, protect the end of the buffer */ |
| 2235 | memmove(end + delta, end, b->data + b->l - end); |
| 2236 | |
| 2237 | /* now, copy str over pos */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2238 | if (len) |
| 2239 | memcpy(pos, str, len); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2240 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2241 | /* we only move data after the displaced zone */ |
| 2242 | if (b->r > pos) b->r += delta; |
| 2243 | if (b->w > pos) b->w += delta; |
| 2244 | if (b->h > pos) b->h += delta; |
| 2245 | if (b->lr > pos) b->lr += delta; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2246 | b->l += delta; |
| 2247 | |
| 2248 | return delta; |
| 2249 | } |
| 2250 | |
| 2251 | |
| 2252 | int exp_replace(char *dst, char *src, char *str, regmatch_t *matches) { |
| 2253 | char *old_dst = dst; |
| 2254 | |
| 2255 | while (*str) { |
| 2256 | if (*str == '\\') { |
| 2257 | str++; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 2258 | if (isdigit((int)*str)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2259 | int len, num; |
| 2260 | |
| 2261 | num = *str - '0'; |
| 2262 | str++; |
| 2263 | |
| 2264 | if (matches[num].rm_so > -1) { |
| 2265 | len = matches[num].rm_eo - matches[num].rm_so; |
| 2266 | memcpy(dst, src + matches[num].rm_so, len); |
| 2267 | dst += len; |
| 2268 | } |
| 2269 | |
| 2270 | } |
| 2271 | else if (*str == 'x') { |
| 2272 | unsigned char hex1, hex2; |
| 2273 | str++; |
| 2274 | |
| 2275 | hex1=toupper(*str++) - '0'; hex2=toupper(*str++) - '0'; |
| 2276 | |
| 2277 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 2278 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 2279 | *dst++ = (hex1<<4) + hex2; |
| 2280 | } |
| 2281 | else |
| 2282 | *dst++ = *str++; |
| 2283 | } |
| 2284 | else |
| 2285 | *dst++ = *str++; |
| 2286 | } |
| 2287 | *dst = 0; |
| 2288 | return dst - old_dst; |
| 2289 | } |
| 2290 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2291 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2292 | /* |
| 2293 | * manages the client FSM and its socket. BTW, it also tries to handle the |
| 2294 | * cookie. It returns 1 if a state has changed (and a resync may be needed), |
| 2295 | * 0 else. |
| 2296 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2297 | int process_cli(struct session *t) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2298 | int s = t->srv_state; |
| 2299 | int c = t->cli_state; |
| 2300 | struct buffer *req = t->req; |
| 2301 | struct buffer *rep = t->rep; |
| 2302 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2303 | #ifdef DEBUG_FULL |
| 2304 | fprintf(stderr,"process_cli: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 2305 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2306 | //fprintf(stderr,"process_cli: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s, |
| 2307 | //FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 2308 | //FD_ISSET(t->srv_fd, StaticReadEvent), FD_ISSET(t->srv_fd, StaticWriteEvent) |
| 2309 | //); |
| 2310 | if (c == CL_STHEADERS) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2311 | /* now parse the partial (or complete) headers */ |
| 2312 | while (req->lr < req->r) { /* this loop only sees one header at each iteration */ |
| 2313 | char *ptr; |
| 2314 | int delete_header; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2315 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2316 | ptr = req->lr; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2317 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2318 | /* look for the end of the current header */ |
| 2319 | while (ptr < req->r && *ptr != '\n' && *ptr != '\r') |
| 2320 | ptr++; |
| 2321 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2322 | if (ptr == req->h) { /* empty line, end of headers */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2323 | int line, len; |
| 2324 | /* we can only get here after an end of headers */ |
| 2325 | /* we'll have something else to do here : add new headers ... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2326 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2327 | if (t->flags & SN_CLDENY) { |
| 2328 | /* no need to go further */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2329 | t->logs.status = 403; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2330 | client_retnclose(t, t->proxy->errmsg.len403, t->proxy->errmsg.msg403); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2331 | if (!(t->flags & SN_ERR_MASK)) |
| 2332 | t->flags |= SN_ERR_PRXCOND; |
| 2333 | if (!(t->flags & SN_FINST_MASK)) |
| 2334 | t->flags |= SN_FINST_R; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2335 | return 1; |
| 2336 | } |
| 2337 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2338 | for (line = 0; line < t->proxy->nb_reqadd; line++) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2339 | len = sprintf(trash, "%s\r\n", t->proxy->req_add[line]); |
| 2340 | buffer_replace2(req, req->h, req->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2341 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2342 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2343 | if (t->proxy->options & PR_O_FWDFOR) { |
| 2344 | /* insert an X-Forwarded-For header */ |
| 2345 | unsigned char *pn; |
| 2346 | pn = (unsigned char *)&t->cli_addr.sin_addr; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2347 | len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d\r\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2348 | pn[0], pn[1], pn[2], pn[3]); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2349 | buffer_replace2(req, req->h, req->h, trash, len); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2350 | } |
| 2351 | |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 2352 | if (!memcmp(req->data, "POST ", 5)) |
| 2353 | t->flags |= SN_POST; /* this is a POST request */ |
| 2354 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2355 | t->cli_state = CL_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2356 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2357 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2358 | t->logs.t_request = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2359 | /* FIXME: we'll set the client in a wait state while we try to |
| 2360 | * connect to the server. Is this really needed ? wouldn't it be |
| 2361 | * better to release the maximum of system buffers instead ? */ |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2362 | //FD_CLR(t->cli_fd, StaticReadEvent); |
| 2363 | //tv_eternity(&t->crexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2364 | break; |
| 2365 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2366 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2367 | /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */ |
| 2368 | if (ptr > req->r - 2) { |
| 2369 | /* this is a partial header, let's wait for more to come */ |
| 2370 | req->lr = ptr; |
| 2371 | break; |
| 2372 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2373 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2374 | /* now we know that *ptr is either \r or \n, |
| 2375 | * and that there are at least 1 char after it. |
| 2376 | */ |
| 2377 | if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n')) |
| 2378 | req->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */ |
| 2379 | else |
| 2380 | req->lr = ptr + 2; /* \r\n or \n\r */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2381 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2382 | /* |
| 2383 | * now we know that we have a full header ; we can do whatever |
| 2384 | * we want with these pointers : |
| 2385 | * req->h = beginning of header |
| 2386 | * ptr = end of header (first \r or \n) |
| 2387 | * req->lr = beginning of next line (next rep->h) |
| 2388 | * req->r = end of data (not used at this stage) |
| 2389 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2390 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2391 | if (t->logs.logwait & LW_REQ) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2392 | /* we have a complete HTTP request that we must log */ |
| 2393 | int urilen; |
| 2394 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2395 | if ((t->logs.uri = pool_alloc(requri)) == NULL) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2396 | Alert("HTTP logging : out of memory.\n"); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2397 | t->logs.status = 500; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2398 | client_retnclose(t, t->proxy->errmsg.len500, t->proxy->errmsg.msg500); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2399 | if (!(t->flags & SN_ERR_MASK)) |
| 2400 | t->flags |= SN_ERR_PRXCOND; |
| 2401 | if (!(t->flags & SN_FINST_MASK)) |
| 2402 | t->flags |= SN_FINST_R; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2403 | return 1; |
| 2404 | } |
| 2405 | |
| 2406 | urilen = ptr - req->h; |
| 2407 | if (urilen >= REQURI_LEN) |
| 2408 | urilen = REQURI_LEN - 1; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2409 | memcpy(t->logs.uri, req->h, urilen); |
| 2410 | t->logs.uri[urilen] = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2411 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2412 | if (!(t->logs.logwait &= ~LW_REQ)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2413 | sess_log(t); |
| 2414 | } |
| 2415 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2416 | delete_header = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2417 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2418 | if ((global.mode & MODE_DEBUG) && !(global.mode & MODE_QUIET)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2419 | int len, max; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame^] | 2420 | len = sprintf(trash, "%08x:%s.clihdr[%04x:%04x]: ", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2421 | max = ptr - req->h; |
| 2422 | UBOUND(max, sizeof(trash) - len - 1); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2423 | len += strlcpy2(trash + len, req->h, max + 1); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2424 | trash[len++] = '\n'; |
| 2425 | write(1, trash, len); |
| 2426 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2427 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2428 | /* try headers regexps */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2429 | if (t->proxy->req_exp != NULL && !(t->flags & SN_CLDENY)) { |
| 2430 | struct hdr_exp *exp; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2431 | char term; |
| 2432 | |
| 2433 | term = *ptr; |
| 2434 | *ptr = '\0'; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2435 | exp = t->proxy->req_exp; |
| 2436 | do { |
| 2437 | if (regexec(exp->preg, req->h, MAX_MATCH, pmatch, 0) == 0) { |
| 2438 | switch (exp->action) { |
| 2439 | case ACT_ALLOW: |
| 2440 | if (!(t->flags & SN_CLDENY)) |
| 2441 | t->flags |= SN_CLALLOW; |
| 2442 | break; |
| 2443 | case ACT_REPLACE: |
| 2444 | if (!(t->flags & SN_CLDENY)) { |
| 2445 | int len = exp_replace(trash, req->h, exp->replace, pmatch); |
| 2446 | ptr += buffer_replace2(req, req->h, ptr, trash, len); |
| 2447 | } |
| 2448 | break; |
| 2449 | case ACT_REMOVE: |
| 2450 | if (!(t->flags & SN_CLDENY)) |
| 2451 | delete_header = 1; |
| 2452 | break; |
| 2453 | case ACT_DENY: |
| 2454 | if (!(t->flags & SN_CLALLOW)) |
| 2455 | t->flags |= SN_CLDENY; |
| 2456 | break; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2457 | case ACT_PASS: /* we simply don't deny this one */ |
| 2458 | break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2459 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2460 | break; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2461 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2462 | } while ((exp = exp->next) != NULL); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2463 | *ptr = term; /* restore the string terminator */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2464 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2465 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2466 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 2467 | * attributes whose name begin with a '$', and associate them with |
| 2468 | * the right cookie, if we want to delete this cookie. |
| 2469 | * So there are 3 cases for each cookie read : |
| 2470 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 2471 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 2472 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 2473 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 2474 | * "special" cookie. |
| 2475 | * At the end of loop, if a "special" cookie remains, we may have to |
| 2476 | * remove it. If no application cookie persists in the header, we |
| 2477 | * *MUST* delete it |
| 2478 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2479 | if (!delete_header && (t->proxy->cookie_name != NULL || t->proxy->capture_name != NULL) |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2480 | && !(t->flags & SN_CLDENY) && (ptr >= req->h + 8) |
willy tarreau | 906b268 | 2005-12-17 13:49:52 +0100 | [diff] [blame] | 2481 | && (strncasecmp(req->h, "Cookie: ", 8) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2482 | char *p1, *p2, *p3, *p4; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2483 | char *del_colon, *del_cookie, *colon; |
| 2484 | int app_cookies; |
| 2485 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2486 | p1 = req->h + 8; /* first char after 'Cookie: ' */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2487 | colon = p1; |
| 2488 | /* del_cookie == NULL => nothing to be deleted */ |
| 2489 | del_colon = del_cookie = NULL; |
| 2490 | app_cookies = 0; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2491 | |
| 2492 | while (p1 < ptr) { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2493 | /* skip spaces and colons, but keep an eye on these ones */ |
| 2494 | while (p1 < ptr) { |
| 2495 | if (*p1 == ';' || *p1 == ',') |
| 2496 | colon = p1; |
| 2497 | else if (!isspace((int)*p1)) |
| 2498 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2499 | p1++; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2500 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2501 | |
| 2502 | if (p1 == ptr) |
| 2503 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2504 | |
| 2505 | /* p1 is at the beginning of the cookie name */ |
| 2506 | p2 = p1; |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2507 | while (p2 < ptr && *p2 != '=') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2508 | p2++; |
| 2509 | |
| 2510 | if (p2 == ptr) |
| 2511 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2512 | |
| 2513 | p3 = p2 + 1; /* skips the '=' sign */ |
| 2514 | if (p3 == ptr) |
| 2515 | break; |
| 2516 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2517 | p4 = p3; |
| 2518 | while (p4 < ptr && !isspace((int)*p4) && *p4 != ';' && *p4 != ',') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2519 | p4++; |
| 2520 | |
| 2521 | /* here, we have the cookie name between p1 and p2, |
| 2522 | * and its value between p3 and p4. |
| 2523 | * we can process it. |
| 2524 | */ |
| 2525 | |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2526 | if (*p1 == '$') { |
| 2527 | /* skip this one */ |
| 2528 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2529 | else { |
| 2530 | /* first, let's see if we want to capture it */ |
| 2531 | if (t->proxy->capture_name != NULL && |
| 2532 | t->logs.cli_cookie == NULL && |
| 2533 | (p4 - p1 >= t->proxy->capture_namelen) && |
| 2534 | memcmp(p1, t->proxy->capture_name, t->proxy->capture_namelen) == 0) { |
| 2535 | int log_len = p4 - p1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2536 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2537 | if ((t->logs.cli_cookie = pool_alloc(capture)) == NULL) { |
| 2538 | Alert("HTTP logging : out of memory.\n"); |
| 2539 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2540 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2541 | if (log_len > t->proxy->capture_len) |
| 2542 | log_len = t->proxy->capture_len; |
| 2543 | memcpy(t->logs.cli_cookie, p1, log_len); |
| 2544 | t->logs.cli_cookie[log_len] = 0; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2545 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2546 | |
| 2547 | if ((p2 - p1 == t->proxy->cookie_len) && (t->proxy->cookie_name != NULL) && |
| 2548 | (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) { |
| 2549 | /* Cool... it's the right one */ |
| 2550 | struct server *srv = t->proxy->srv; |
| 2551 | |
| 2552 | while (srv && |
| 2553 | ((srv->cklen != p4 - p3) || memcmp(p3, srv->cookie, p4 - p3))) { |
| 2554 | srv = srv->next; |
| 2555 | } |
| 2556 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2557 | if (!srv) { |
| 2558 | t->flags &= ~SN_CK_MASK; |
| 2559 | t->flags |= SN_CK_INVALID; |
| 2560 | } |
| 2561 | else if (srv->state & SRV_RUNNING || t->proxy->options & PR_O_PERSIST) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2562 | /* we found the server and it's usable */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2563 | t->flags &= ~SN_CK_MASK; |
| 2564 | t->flags |= SN_CK_VALID | SN_DIRECT; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2565 | t->srv = srv; |
| 2566 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2567 | else { |
| 2568 | t->flags &= ~SN_CK_MASK; |
| 2569 | t->flags |= SN_CK_DOWN; |
| 2570 | } |
| 2571 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2572 | /* if this cookie was set in insert+indirect mode, then it's better that the |
| 2573 | * server never sees it. |
| 2574 | */ |
| 2575 | if (del_cookie == NULL && |
| 2576 | (t->proxy->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2577 | del_cookie = p1; |
| 2578 | del_colon = colon; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2579 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2580 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2581 | else { |
| 2582 | /* now we know that we must keep this cookie since it's |
| 2583 | * not ours. But if we wanted to delete our cookie |
| 2584 | * earlier, we cannot remove the complete header, but we |
| 2585 | * can remove the previous block itself. |
| 2586 | */ |
| 2587 | app_cookies++; |
| 2588 | |
| 2589 | if (del_cookie != NULL) { |
| 2590 | buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 2591 | p4 -= (p1 - del_cookie); |
| 2592 | ptr -= (p1 - del_cookie); |
| 2593 | del_cookie = del_colon = NULL; |
| 2594 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2595 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2596 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2597 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2598 | /* we'll have to look for another cookie ... */ |
| 2599 | p1 = p4; |
| 2600 | } /* while (p1 < ptr) */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2601 | |
| 2602 | /* There's no more cookie on this line. |
| 2603 | * We may have marked the last one(s) for deletion. |
| 2604 | * We must do this now in two ways : |
| 2605 | * - if there is no app cookie, we simply delete the header ; |
| 2606 | * - if there are app cookies, we must delete the end of the |
| 2607 | * string properly, including the colon/semi-colon before |
| 2608 | * the cookie name. |
| 2609 | */ |
| 2610 | if (del_cookie != NULL) { |
| 2611 | if (app_cookies) { |
| 2612 | buffer_replace2(req, del_colon, ptr, NULL, 0); |
| 2613 | /* WARNING! <ptr> becomes invalid for now. If some code |
| 2614 | * below needs to rely on it before the end of the global |
| 2615 | * header loop, we need to correct it with this code : |
| 2616 | * ptr = del_colon; |
| 2617 | */ |
| 2618 | } |
| 2619 | else |
| 2620 | delete_header = 1; |
| 2621 | } |
| 2622 | } /* end of cookie processing on this header */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2623 | |
| 2624 | /* let's look if we have to delete this header */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2625 | if (delete_header && !(t->flags & SN_CLDENY)) { |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2626 | buffer_replace2(req, req->h, req->lr, NULL, 0); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2627 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 2628 | /* WARNING: ptr is not valid anymore, since the header may have been deleted or truncated ! */ |
| 2629 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2630 | req->h = req->lr; |
| 2631 | } /* while (req->lr < req->r) */ |
| 2632 | |
| 2633 | /* end of header processing (even if incomplete) */ |
| 2634 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2635 | if ((req->l < req->rlim - req->data) && ! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 2636 | /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer |
| 2637 | * full. We cannot loop here since event_cli_read will disable it only if |
| 2638 | * req->l == rlim-data |
| 2639 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2640 | FD_SET(t->cli_fd, StaticReadEvent); |
| 2641 | if (t->proxy->clitimeout) |
| 2642 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 2643 | else |
| 2644 | tv_eternity(&t->crexpire); |
| 2645 | } |
| 2646 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2647 | /* 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] | 2648 | * won't be able to free more later, so the session will never terminate. |
| 2649 | */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2650 | if (req->l >= req->rlim - req->data) { |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2651 | t->logs.status = 400; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2652 | client_retnclose(t, t->proxy->errmsg.len400, t->proxy->errmsg.msg400); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2653 | if (!(t->flags & SN_ERR_MASK)) |
| 2654 | t->flags |= SN_ERR_PRXCOND; |
| 2655 | if (!(t->flags & SN_FINST_MASK)) |
| 2656 | t->flags |= SN_FINST_R; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2657 | return 1; |
| 2658 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2659 | else if (t->res_cr == RES_ERROR || t->res_cr == RES_NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2660 | /* read error, or last read : give up. */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2661 | tv_eternity(&t->crexpire); |
| 2662 | fd_delete(t->cli_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2663 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2664 | if (!(t->flags & SN_ERR_MASK)) |
| 2665 | t->flags |= SN_ERR_CLICL; |
| 2666 | if (!(t->flags & SN_FINST_MASK)) |
| 2667 | t->flags |= SN_FINST_R; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2668 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2669 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2670 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 2671 | |
| 2672 | /* read timeout : give up with an error message. |
| 2673 | */ |
| 2674 | t->logs.status = 408; |
| 2675 | client_retnclose(t, t->proxy->errmsg.len408, t->proxy->errmsg.msg408); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2676 | if (!(t->flags & SN_ERR_MASK)) |
| 2677 | t->flags |= SN_ERR_CLITO; |
| 2678 | if (!(t->flags & SN_FINST_MASK)) |
| 2679 | t->flags |= SN_FINST_R; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2680 | return 1; |
| 2681 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2682 | |
| 2683 | return t->cli_state != CL_STHEADERS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2684 | } |
| 2685 | else if (c == CL_STDATA) { |
| 2686 | /* read or write error */ |
| 2687 | if (t->res_cw == RES_ERROR || t->res_cr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2688 | tv_eternity(&t->crexpire); |
| 2689 | tv_eternity(&t->cwexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2690 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2691 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2692 | if (!(t->flags & SN_ERR_MASK)) |
| 2693 | t->flags |= SN_ERR_CLICL; |
| 2694 | if (!(t->flags & SN_FINST_MASK)) |
| 2695 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2696 | return 1; |
| 2697 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2698 | /* last read, or end of server write */ |
| 2699 | else if (t->res_cr == RES_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2700 | FD_CLR(t->cli_fd, StaticReadEvent); |
| 2701 | // if (req->l == 0) /* nothing to write on the server side */ |
| 2702 | // FD_CLR(t->srv_fd, StaticWriteEvent); |
| 2703 | tv_eternity(&t->crexpire); |
| 2704 | shutdown(t->cli_fd, SHUT_RD); |
| 2705 | t->cli_state = CL_STSHUTR; |
| 2706 | return 1; |
| 2707 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2708 | /* last server read and buffer empty */ |
| 2709 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2710 | FD_CLR(t->cli_fd, StaticWriteEvent); |
| 2711 | tv_eternity(&t->cwexpire); |
| 2712 | shutdown(t->cli_fd, SHUT_WR); |
| 2713 | t->cli_state = CL_STSHUTW; |
| 2714 | return 1; |
| 2715 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2716 | /* read timeout */ |
| 2717 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 2718 | FD_CLR(t->cli_fd, StaticReadEvent); |
| 2719 | // if (req->l == 0) /* nothing to write on the server side */ |
| 2720 | // FD_CLR(t->srv_fd, StaticWriteEvent); |
| 2721 | tv_eternity(&t->crexpire); |
| 2722 | shutdown(t->cli_fd, SHUT_RD); |
| 2723 | t->cli_state = CL_STSHUTR; |
| 2724 | if (!(t->flags & SN_ERR_MASK)) |
| 2725 | t->flags |= SN_ERR_CLITO; |
| 2726 | if (!(t->flags & SN_FINST_MASK)) |
| 2727 | t->flags |= SN_FINST_D; |
| 2728 | return 1; |
| 2729 | } |
| 2730 | /* write timeout */ |
| 2731 | else if (tv_cmp2_ms(&t->cwexpire, &now) <= 0) { |
| 2732 | FD_CLR(t->cli_fd, StaticWriteEvent); |
| 2733 | tv_eternity(&t->cwexpire); |
| 2734 | shutdown(t->cli_fd, SHUT_WR); |
| 2735 | t->cli_state = CL_STSHUTW; |
| 2736 | if (!(t->flags & SN_ERR_MASK)) |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2737 | t->flags |= SN_ERR_CLITO; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2738 | if (!(t->flags & SN_FINST_MASK)) |
| 2739 | t->flags |= SN_FINST_D; |
| 2740 | return 1; |
| 2741 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2742 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2743 | if (req->l >= req->rlim - req->data) { |
| 2744 | /* no room to read more data */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2745 | if (FD_ISSET(t->cli_fd, StaticReadEvent)) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2746 | /* stop reading until we get some space */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2747 | FD_CLR(t->cli_fd, StaticReadEvent); |
| 2748 | tv_eternity(&t->crexpire); |
| 2749 | } |
| 2750 | } |
| 2751 | else { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2752 | /* there's still some space in the buffer */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2753 | if (! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 2754 | FD_SET(t->cli_fd, StaticReadEvent); |
| 2755 | if (t->proxy->clitimeout) |
| 2756 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 2757 | else |
| 2758 | tv_eternity(&t->crexpire); |
| 2759 | } |
| 2760 | } |
| 2761 | |
| 2762 | if ((rep->l == 0) || |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2763 | ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2764 | if (FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 2765 | FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */ |
| 2766 | tv_eternity(&t->cwexpire); |
| 2767 | } |
| 2768 | } |
| 2769 | else { /* buffer not empty */ |
| 2770 | if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 2771 | FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2772 | if (t->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2773 | tv_delayfrom(&t->cwexpire, &now, t->proxy->clitimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2774 | /* FIXME: to avoid the client to read-time-out during writes, we refresh it */ |
| 2775 | t->crexpire = t->cwexpire; |
| 2776 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2777 | else |
| 2778 | tv_eternity(&t->cwexpire); |
| 2779 | } |
| 2780 | } |
| 2781 | return 0; /* other cases change nothing */ |
| 2782 | } |
| 2783 | else if (c == CL_STSHUTR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2784 | if (t->res_cw == RES_ERROR) { |
| 2785 | tv_eternity(&t->cwexpire); |
| 2786 | fd_delete(t->cli_fd); |
| 2787 | t->cli_state = CL_STCLOSE; |
| 2788 | if (!(t->flags & SN_ERR_MASK)) |
| 2789 | t->flags |= SN_ERR_CLICL; |
| 2790 | if (!(t->flags & SN_FINST_MASK)) |
| 2791 | t->flags |= SN_FINST_D; |
| 2792 | return 1; |
| 2793 | } |
| 2794 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2795 | tv_eternity(&t->cwexpire); |
| 2796 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2797 | t->cli_state = CL_STCLOSE; |
| 2798 | return 1; |
| 2799 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2800 | else if (tv_cmp2_ms(&t->cwexpire, &now) <= 0) { |
| 2801 | tv_eternity(&t->cwexpire); |
| 2802 | fd_delete(t->cli_fd); |
| 2803 | t->cli_state = CL_STCLOSE; |
| 2804 | if (!(t->flags & SN_ERR_MASK)) |
| 2805 | t->flags |= SN_ERR_CLITO; |
| 2806 | if (!(t->flags & SN_FINST_MASK)) |
| 2807 | t->flags |= SN_FINST_D; |
| 2808 | return 1; |
| 2809 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2810 | else if ((rep->l == 0) || |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2811 | ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2812 | if (FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 2813 | FD_CLR(t->cli_fd, StaticWriteEvent); /* stop writing */ |
| 2814 | tv_eternity(&t->cwexpire); |
| 2815 | } |
| 2816 | } |
| 2817 | else { /* buffer not empty */ |
| 2818 | if (! FD_ISSET(t->cli_fd, StaticWriteEvent)) { |
| 2819 | FD_SET(t->cli_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2820 | if (t->proxy->clitimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2821 | tv_delayfrom(&t->cwexpire, &now, t->proxy->clitimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2822 | /* FIXME: to avoid the client to read-time-out during writes, we refresh it */ |
| 2823 | t->crexpire = t->cwexpire; |
| 2824 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2825 | else |
| 2826 | tv_eternity(&t->cwexpire); |
| 2827 | } |
| 2828 | } |
| 2829 | return 0; |
| 2830 | } |
| 2831 | else if (c == CL_STSHUTW) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2832 | if (t->res_cr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2833 | tv_eternity(&t->crexpire); |
| 2834 | fd_delete(t->cli_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2835 | t->cli_state = CL_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2836 | if (!(t->flags & SN_ERR_MASK)) |
| 2837 | t->flags |= SN_ERR_CLICL; |
| 2838 | if (!(t->flags & SN_FINST_MASK)) |
| 2839 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2840 | return 1; |
| 2841 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2842 | else if (t->res_cr == RES_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
| 2843 | tv_eternity(&t->crexpire); |
| 2844 | fd_delete(t->cli_fd); |
| 2845 | t->cli_state = CL_STCLOSE; |
| 2846 | return 1; |
| 2847 | } |
| 2848 | else if (tv_cmp2_ms(&t->crexpire, &now) <= 0) { |
| 2849 | tv_eternity(&t->crexpire); |
| 2850 | fd_delete(t->cli_fd); |
| 2851 | t->cli_state = CL_STCLOSE; |
| 2852 | if (!(t->flags & SN_ERR_MASK)) |
| 2853 | t->flags |= SN_ERR_CLITO; |
| 2854 | if (!(t->flags & SN_FINST_MASK)) |
| 2855 | t->flags |= SN_FINST_D; |
| 2856 | return 1; |
| 2857 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2858 | else if (req->l >= req->rlim - req->data) { |
| 2859 | /* no room to read more data */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2860 | if (FD_ISSET(t->cli_fd, StaticReadEvent)) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2861 | /* stop reading until we get some space */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2862 | FD_CLR(t->cli_fd, StaticReadEvent); |
| 2863 | tv_eternity(&t->crexpire); |
| 2864 | } |
| 2865 | } |
| 2866 | else { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 2867 | /* there's still some space in the buffer */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2868 | if (! FD_ISSET(t->cli_fd, StaticReadEvent)) { |
| 2869 | FD_SET(t->cli_fd, StaticReadEvent); |
| 2870 | if (t->proxy->clitimeout) |
| 2871 | tv_delayfrom(&t->crexpire, &now, t->proxy->clitimeout); |
| 2872 | else |
| 2873 | tv_eternity(&t->crexpire); |
| 2874 | } |
| 2875 | } |
| 2876 | return 0; |
| 2877 | } |
| 2878 | else { /* CL_STCLOSE: nothing to do */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 2879 | if ((global.mode & MODE_DEBUG) && !(global.mode & MODE_QUIET)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2880 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame^] | 2881 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2882 | write(1, trash, len); |
| 2883 | } |
| 2884 | return 0; |
| 2885 | } |
| 2886 | return 0; |
| 2887 | } |
| 2888 | |
| 2889 | |
| 2890 | /* |
| 2891 | * manages the server FSM and its socket. It returns 1 if a state has changed |
| 2892 | * (and a resync may be needed), 0 else. |
| 2893 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2894 | int process_srv(struct session *t) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2895 | int s = t->srv_state; |
| 2896 | int c = t->cli_state; |
| 2897 | struct buffer *req = t->req; |
| 2898 | struct buffer *rep = t->rep; |
| 2899 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2900 | #ifdef DEBUG_FULL |
| 2901 | fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 2902 | #endif |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2903 | //fprintf(stderr,"process_srv: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s, |
| 2904 | //FD_ISSET(t->cli_fd, StaticReadEvent), FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 2905 | //FD_ISSET(t->srv_fd, StaticReadEvent), FD_ISSET(t->srv_fd, StaticWriteEvent) |
| 2906 | //); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2907 | if (s == SV_STIDLE) { |
| 2908 | if (c == CL_STHEADERS) |
| 2909 | return 0; /* stay in idle, waiting for data to reach the client side */ |
| 2910 | else if (c == CL_STCLOSE || |
| 2911 | c == CL_STSHUTW || |
| 2912 | (c == CL_STSHUTR && t->req->l == 0)) { /* give up */ |
| 2913 | tv_eternity(&t->cnexpire); |
| 2914 | t->srv_state = SV_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2915 | if (!(t->flags & SN_ERR_MASK)) |
| 2916 | t->flags |= SN_ERR_CLICL; |
| 2917 | if (!(t->flags & SN_FINST_MASK)) |
| 2918 | t->flags |= SN_FINST_C; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2919 | return 1; |
| 2920 | } |
| 2921 | else { /* go to SV_STCONN */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2922 | if (connect_server(t) == 0) { /* initiate a connection to the server */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2923 | //fprintf(stderr,"0: c=%d, s=%d\n", c, s); |
| 2924 | t->srv_state = SV_STCONN; |
| 2925 | } |
| 2926 | else { /* try again */ |
| 2927 | while (t->conn_retries-- > 0) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2928 | if ((t->proxy->options & PR_O_REDISP) && (t->conn_retries == 0)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2929 | t->flags &= ~SN_DIRECT; /* ignore cookie and force to use the dispatcher */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2930 | t->srv = NULL; /* it's left to the dispatcher to choose a server */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2931 | if ((t->flags & SN_CK_MASK) == SN_CK_VALID) { |
| 2932 | t->flags &= ~SN_CK_MASK; |
| 2933 | t->flags |= SN_CK_DOWN; |
| 2934 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | if (connect_server(t) == 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2938 | t->srv_state = SV_STCONN; |
| 2939 | break; |
| 2940 | } |
| 2941 | } |
| 2942 | if (t->conn_retries < 0) { |
| 2943 | /* if conn_retries < 0 or other error, let's abort */ |
| 2944 | tv_eternity(&t->cnexpire); |
| 2945 | t->srv_state = SV_STCLOSE; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2946 | t->logs.status = 503; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2947 | if (t->proxy->mode == PR_MODE_HTTP) |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2948 | client_return(t, t->proxy->errmsg.len503, t->proxy->errmsg.msg503); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2949 | if (!(t->flags & SN_ERR_MASK)) |
| 2950 | t->flags |= SN_ERR_SRVCL; |
| 2951 | if (!(t->flags & SN_FINST_MASK)) |
| 2952 | t->flags |= SN_FINST_C; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2953 | } |
| 2954 | } |
| 2955 | return 1; |
| 2956 | } |
| 2957 | } |
| 2958 | else if (s == SV_STCONN) { /* connection in progress */ |
| 2959 | if (t->res_sw == RES_SILENT && tv_cmp2_ms(&t->cnexpire, &now) > 0) { |
| 2960 | //fprintf(stderr,"1: c=%d, s=%d\n", c, s); |
| 2961 | return 0; /* nothing changed */ |
| 2962 | } |
| 2963 | else if (t->res_sw == RES_SILENT || t->res_sw == RES_ERROR) { |
| 2964 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 2965 | /* timeout, connect error or first write error */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2966 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2967 | fd_delete(t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2968 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2969 | t->conn_retries--; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2970 | if (t->conn_retries >= 0) { |
| 2971 | if ((t->proxy->options & PR_O_REDISP) && (t->conn_retries == 0)) { |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 2972 | t->flags &= ~SN_DIRECT; /* ignore cookie and force to use the dispatcher */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2973 | t->srv = NULL; /* it's left to the dispatcher to choose a server */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2974 | if ((t->flags & SN_CK_MASK) == SN_CK_VALID) { |
| 2975 | t->flags &= ~SN_CK_MASK; |
| 2976 | t->flags |= SN_CK_DOWN; |
| 2977 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 2978 | } |
| 2979 | if (connect_server(t) == 0) |
| 2980 | return 0; /* no state changed */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2981 | } |
| 2982 | /* if conn_retries < 0 or other error, let's abort */ |
| 2983 | tv_eternity(&t->cnexpire); |
| 2984 | t->srv_state = SV_STCLOSE; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2985 | t->logs.status = 503; |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2986 | if (t->proxy->mode == PR_MODE_HTTP) |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 2987 | client_return(t, t->proxy->errmsg.len503, t->proxy->errmsg.msg503); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 2988 | if (!(t->flags & SN_ERR_MASK)) |
| 2989 | t->flags |= SN_ERR_SRVCL; |
| 2990 | if (!(t->flags & SN_FINST_MASK)) |
| 2991 | t->flags |= SN_FINST_C; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2992 | return 1; |
| 2993 | } |
| 2994 | else { /* no error or write 0 */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 2995 | t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 2996 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2997 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 2998 | if (req->l == 0) /* nothing to write */ { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 2999 | FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3000 | tv_eternity(&t->swexpire); |
| 3001 | } else /* need the right to write */ { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3002 | FD_SET(t->srv_fd, StaticWriteEvent); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3003 | if (t->proxy->srvtimeout) { |
| 3004 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
| 3005 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 3006 | t->srexpire = t->swexpire; |
| 3007 | } |
| 3008 | else |
| 3009 | tv_eternity(&t->swexpire); |
| 3010 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3011 | |
| 3012 | if (t->proxy->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 3013 | FD_SET(t->srv_fd, StaticReadEvent); |
| 3014 | if (t->proxy->srvtimeout) |
| 3015 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 3016 | else |
| 3017 | tv_eternity(&t->srexpire); |
| 3018 | |
| 3019 | t->srv_state = SV_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3020 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3021 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3022 | else { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3023 | t->srv_state = SV_STHEADERS; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3024 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
| 3025 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3026 | tv_eternity(&t->cnexpire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3027 | return 1; |
| 3028 | } |
| 3029 | } |
| 3030 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3031 | /* now parse the partial (or complete) headers */ |
| 3032 | while (rep->lr < rep->r) { /* this loop only sees one header at each iteration */ |
| 3033 | char *ptr; |
| 3034 | int delete_header; |
| 3035 | |
| 3036 | ptr = rep->lr; |
| 3037 | |
| 3038 | /* look for the end of the current header */ |
| 3039 | while (ptr < rep->r && *ptr != '\n' && *ptr != '\r') |
| 3040 | ptr++; |
| 3041 | |
| 3042 | if (ptr == rep->h) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3043 | int line, len; |
| 3044 | |
| 3045 | /* we can only get here after an end of headers */ |
| 3046 | /* we'll have something else to do here : add new headers ... */ |
| 3047 | |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 3048 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_INS) && |
| 3049 | (!(t->proxy->options & PR_O_COOK_POST) || (t->flags & SN_POST))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3050 | /* the server is known, it's not the one the client requested, we have to |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 3051 | * insert a set-cookie here, except if we want to insert only on POST |
| 3052 | * requests and this one isn't. |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3053 | */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3054 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/\r\n", |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3055 | t->proxy->cookie_name, |
| 3056 | t->srv->cookie ? t->srv->cookie : ""); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3057 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3058 | t->flags |= SN_SCK_INSERTED; |
| 3059 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3060 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3061 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3062 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3063 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3064 | */ |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 3065 | if (t->proxy->options & PR_O_COOK_NOC) |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3066 | //len += sprintf(newhdr + len, "Cache-control: no-cache=\"set-cookie\"\r\n"); |
| 3067 | len += sprintf(trash + len, "Cache-control: private\r\n"); |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 3068 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3069 | buffer_replace2(rep, rep->h, rep->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3070 | } |
| 3071 | |
| 3072 | /* headers to be added */ |
| 3073 | for (line = 0; line < t->proxy->nb_rspadd; line++) { |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3074 | len = sprintf(trash, "%s\r\n", t->proxy->rsp_add[line]); |
| 3075 | buffer_replace2(rep, rep->h, rep->h, trash, len); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3076 | } |
| 3077 | |
| 3078 | t->srv_state = SV_STDATA; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3079 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3080 | t->logs.t_data = tv_diff(&t->logs.tv_accept, &now); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3081 | break; |
| 3082 | } |
| 3083 | |
| 3084 | /* to get a complete header line, we need the ending \r\n, \n\r, \r or \n too */ |
| 3085 | if (ptr > rep->r - 2) { |
| 3086 | /* this is a partial header, let's wait for more to come */ |
| 3087 | rep->lr = ptr; |
| 3088 | break; |
| 3089 | } |
| 3090 | |
| 3091 | // fprintf(stderr,"h=%p, ptr=%p, lr=%p, r=%p, *h=", rep->h, ptr, rep->lr, rep->r); |
| 3092 | // write(2, rep->h, ptr - rep->h); fprintf(stderr,"\n"); |
| 3093 | |
| 3094 | /* now we know that *ptr is either \r or \n, |
| 3095 | * and that there are at least 1 char after it. |
| 3096 | */ |
| 3097 | if ((ptr[0] == ptr[1]) || (ptr[1] != '\r' && ptr[1] != '\n')) |
| 3098 | rep->lr = ptr + 1; /* \r\r, \n\n, \r[^\n], \n[^\r] */ |
| 3099 | else |
| 3100 | rep->lr = ptr + 2; /* \r\n or \n\r */ |
| 3101 | |
| 3102 | /* |
| 3103 | * now we know that we have a full header ; we can do whatever |
| 3104 | * we want with these pointers : |
| 3105 | * rep->h = beginning of header |
| 3106 | * ptr = end of header (first \r or \n) |
| 3107 | * rep->lr = beginning of next line (next rep->h) |
| 3108 | * rep->r = end of data (not used at this stage) |
| 3109 | */ |
| 3110 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3111 | |
| 3112 | if (t->logs.logwait & LW_RESP) { |
| 3113 | t->logs.logwait &= ~LW_RESP; |
| 3114 | t->logs.status = atoi(rep->h + 9); |
| 3115 | } |
| 3116 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3117 | delete_header = 0; |
| 3118 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3119 | if ((global.mode & MODE_DEBUG) && !(global.mode & MODE_QUIET)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3120 | int len, max; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame^] | 3121 | len = sprintf(trash, "%08x:%s.srvhdr[%04x:%04x]: ", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3122 | max = ptr - rep->h; |
| 3123 | UBOUND(max, sizeof(trash) - len - 1); |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3124 | len += strlcpy2(trash + len, rep->h, max + 1); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3125 | trash[len++] = '\n'; |
| 3126 | write(1, trash, len); |
| 3127 | } |
| 3128 | |
| 3129 | /* try headers regexps */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3130 | if (t->proxy->rsp_exp != NULL && !(t->flags & SN_SVDENY)) { |
| 3131 | struct hdr_exp *exp; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3132 | char term; |
| 3133 | |
| 3134 | term = *ptr; |
| 3135 | *ptr = '\0'; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3136 | exp = t->proxy->rsp_exp; |
| 3137 | do { |
| 3138 | if (regexec(exp->preg, rep->h, MAX_MATCH, pmatch, 0) == 0) { |
| 3139 | switch (exp->action) { |
| 3140 | case ACT_ALLOW: |
| 3141 | if (!(t->flags & SN_SVDENY)) |
| 3142 | t->flags |= SN_SVALLOW; |
| 3143 | break; |
| 3144 | case ACT_REPLACE: |
| 3145 | if (!(t->flags & SN_SVDENY)) { |
| 3146 | int len = exp_replace(trash, rep->h, exp->replace, pmatch); |
| 3147 | ptr += buffer_replace2(rep, rep->h, ptr, trash, len); |
| 3148 | } |
| 3149 | break; |
| 3150 | case ACT_REMOVE: |
| 3151 | if (!(t->flags & SN_SVDENY)) |
| 3152 | delete_header = 1; |
| 3153 | break; |
| 3154 | case ACT_DENY: |
| 3155 | if (!(t->flags & SN_SVALLOW)) |
| 3156 | t->flags |= SN_SVDENY; |
| 3157 | break; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3158 | case ACT_PASS: /* we simply don't deny this one */ |
| 3159 | break; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3160 | } |
| 3161 | break; |
| 3162 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3163 | } while ((exp = exp->next) != NULL); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3164 | *ptr = term; /* restore the string terminator */ |
| 3165 | } |
| 3166 | |
| 3167 | /* check for server cookies */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3168 | if (!delete_header /*&& (t->proxy->options & PR_O_COOK_ANY)*/ |
| 3169 | && (t->proxy->cookie_name != NULL || t->proxy->capture_name != NULL) |
| 3170 | && (ptr >= rep->h + 12) |
willy tarreau | 906b268 | 2005-12-17 13:49:52 +0100 | [diff] [blame] | 3171 | && (strncasecmp(rep->h, "Set-Cookie: ", 12) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3172 | char *p1, *p2, *p3, *p4; |
| 3173 | |
| 3174 | p1 = rep->h + 12; /* first char after 'Set-Cookie: ' */ |
| 3175 | |
| 3176 | while (p1 < ptr) { /* in fact, we'll break after the first cookie */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 3177 | while (p1 < ptr && (isspace((int)*p1))) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3178 | p1++; |
| 3179 | |
| 3180 | if (p1 == ptr || *p1 == ';') /* end of cookie */ |
| 3181 | break; |
| 3182 | |
| 3183 | /* p1 is at the beginning of the cookie name */ |
| 3184 | p2 = p1; |
| 3185 | |
| 3186 | while (p2 < ptr && *p2 != '=' && *p2 != ';') |
| 3187 | p2++; |
| 3188 | |
| 3189 | if (p2 == ptr || *p2 == ';') /* next cookie */ |
| 3190 | break; |
| 3191 | |
| 3192 | p3 = p2 + 1; /* skips the '=' sign */ |
| 3193 | if (p3 == ptr) |
| 3194 | break; |
| 3195 | |
| 3196 | p4 = p3; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 3197 | while (p4 < ptr && !isspace((int)*p4) && *p4 != ';') |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3198 | p4++; |
| 3199 | |
| 3200 | /* here, we have the cookie name between p1 and p2, |
| 3201 | * and its value between p3 and p4. |
| 3202 | * we can process it. |
| 3203 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3204 | |
| 3205 | /* first, let's see if we want to capture it */ |
| 3206 | if (t->proxy->capture_name != NULL && |
| 3207 | t->logs.srv_cookie == NULL && |
| 3208 | (p4 - p1 >= t->proxy->capture_namelen) && |
| 3209 | memcmp(p1, t->proxy->capture_name, t->proxy->capture_namelen) == 0) { |
| 3210 | int log_len = p4 - p1; |
| 3211 | |
| 3212 | if ((t->logs.srv_cookie = pool_alloc(capture)) == NULL) { |
| 3213 | Alert("HTTP logging : out of memory.\n"); |
| 3214 | } |
| 3215 | |
| 3216 | if (log_len > t->proxy->capture_len) |
| 3217 | log_len = t->proxy->capture_len; |
| 3218 | memcpy(t->logs.srv_cookie, p1, log_len); |
| 3219 | t->logs.srv_cookie[log_len] = 0; |
| 3220 | } |
| 3221 | |
| 3222 | if ((p2 - p1 == t->proxy->cookie_len) && (t->proxy->cookie_name != NULL) && |
| 3223 | (memcmp(p1, t->proxy->cookie_name, p2 - p1) == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3224 | /* Cool... it's the right one */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3225 | t->flags |= SN_SCK_SEEN; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3226 | |
| 3227 | /* If the cookie is in insert mode on a known server, we'll delete |
| 3228 | * this occurrence because we'll insert another one later. |
| 3229 | * We'll delete it too if the "indirect" option is set and we're in |
| 3230 | * a direct access. */ |
| 3231 | if (((t->srv) && (t->proxy->options & PR_O_COOK_INS)) || |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3232 | ((t->flags & SN_DIRECT) && (t->proxy->options & PR_O_COOK_IND))) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3233 | /* this header must be deleted */ |
| 3234 | delete_header = 1; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3235 | t->flags |= SN_SCK_DELETED; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3236 | } |
| 3237 | else if ((t->srv) && (t->proxy->options & PR_O_COOK_RW)) { |
| 3238 | /* replace bytes p3->p4 with the cookie name associated |
| 3239 | * with this server since we know it. |
| 3240 | */ |
| 3241 | buffer_replace2(rep, p3, p4, t->srv->cookie, t->srv->cklen); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3242 | t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3243 | } |
| 3244 | break; |
| 3245 | } |
| 3246 | else { |
| 3247 | // fprintf(stderr,"Ignoring unknown cookie : "); |
| 3248 | // write(2, p1, p2-p1); |
| 3249 | // fprintf(stderr," = "); |
| 3250 | // write(2, p3, p4-p3); |
| 3251 | // fprintf(stderr,"\n"); |
| 3252 | } |
| 3253 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 3254 | } /* we're now at the end of the cookie value */ |
| 3255 | } /* end of cookie processing */ |
| 3256 | |
| 3257 | /* let's look if we have to delete this header */ |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3258 | if (delete_header && !(t->flags & SN_SVDENY)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3259 | buffer_replace2(rep, rep->h, rep->lr, "", 0); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 3260 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3261 | rep->h = rep->lr; |
| 3262 | } /* while (rep->lr < rep->r) */ |
| 3263 | |
| 3264 | /* end of header processing (even if incomplete) */ |
| 3265 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3266 | if ((rep->l < rep->rlim - rep->data) && ! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 3267 | /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer |
| 3268 | * full. We cannot loop here since event_srv_read will disable it only if |
| 3269 | * rep->l == rlim-data |
| 3270 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3271 | FD_SET(t->srv_fd, StaticReadEvent); |
| 3272 | if (t->proxy->srvtimeout) |
| 3273 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 3274 | else |
| 3275 | tv_eternity(&t->srexpire); |
| 3276 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3277 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3278 | /* read error, write error */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3279 | if (t->res_sw == RES_ERROR || t->res_sr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3280 | tv_eternity(&t->srexpire); |
| 3281 | tv_eternity(&t->swexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3282 | fd_delete(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3283 | t->srv_state = SV_STCLOSE; |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 3284 | t->logs.status = 502; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3285 | client_return(t, t->proxy->errmsg.len502, t->proxy->errmsg.msg502); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3286 | if (!(t->flags & SN_ERR_MASK)) |
| 3287 | t->flags |= SN_ERR_SRVCL; |
| 3288 | if (!(t->flags & SN_FINST_MASK)) |
| 3289 | t->flags |= SN_FINST_H; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3290 | return 1; |
| 3291 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3292 | /* end of client write or end of server read. |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3293 | * since we are in header mode, if there's no space left for headers, we |
| 3294 | * won't be able to free more later, so the session will never terminate. |
| 3295 | */ |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3296 | else if (t->res_sr == RES_NULL || c == CL_STSHUTW || c == CL_STCLOSE || rep->l >= rep->rlim - rep->data) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3297 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 3298 | tv_eternity(&t->srexpire); |
| 3299 | shutdown(t->srv_fd, SHUT_RD); |
| 3300 | t->srv_state = SV_STSHUTR; |
| 3301 | return 1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3302 | } |
| 3303 | /* read timeout : return a 504 to the client. |
| 3304 | */ |
| 3305 | else if (FD_ISSET(t->srv_fd, StaticReadEvent) && tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 3306 | tv_eternity(&t->srexpire); |
| 3307 | tv_eternity(&t->swexpire); |
| 3308 | fd_delete(t->srv_fd); |
| 3309 | t->srv_state = SV_STCLOSE; |
| 3310 | t->logs.status = 504; |
| 3311 | client_return(t, t->proxy->errmsg.len504, t->proxy->errmsg.msg504); |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3312 | if (!(t->flags & SN_ERR_MASK)) |
| 3313 | t->flags |= SN_ERR_SRVTO; |
| 3314 | if (!(t->flags & SN_FINST_MASK)) |
| 3315 | t->flags |= SN_FINST_H; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3316 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3317 | |
| 3318 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3319 | /* last client read and buffer empty */ |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3320 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 3321 | * client shuts read too early, because we may still have |
| 3322 | * some work to do on the headers. |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3323 | * The side-effect is that if the client completely closes its |
| 3324 | * connection during SV_STHEADER, the connection to the server |
| 3325 | * is kept until a response comes back or the timeout is reached. |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3326 | */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3327 | else if ((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && (req->l == 0)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3328 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 3329 | tv_eternity(&t->swexpire); |
| 3330 | shutdown(t->srv_fd, SHUT_WR); |
| 3331 | t->srv_state = SV_STSHUTW; |
| 3332 | return 1; |
| 3333 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3334 | /* write timeout */ |
| 3335 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 3336 | * client shuts read too early, because we may still have |
| 3337 | * some work to do on the headers. |
| 3338 | */ |
| 3339 | else if (FD_ISSET(t->srv_fd, StaticWriteEvent) && tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
| 3340 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 3341 | tv_eternity(&t->swexpire); |
| 3342 | shutdown(t->srv_fd, SHUT_WR); |
| 3343 | t->srv_state = SV_STSHUTW; |
| 3344 | if (!(t->flags & SN_ERR_MASK)) |
| 3345 | t->flags |= SN_ERR_SRVTO; |
| 3346 | if (!(t->flags & SN_FINST_MASK)) |
| 3347 | t->flags |= SN_FINST_H; |
| 3348 | return 1; |
| 3349 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3350 | |
| 3351 | if (req->l == 0) { |
| 3352 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 3353 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 3354 | tv_eternity(&t->swexpire); |
| 3355 | } |
| 3356 | } |
| 3357 | else { /* client buffer not empty */ |
| 3358 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 3359 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3360 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3361 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3362 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 3363 | t->srexpire = t->swexpire; |
| 3364 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3365 | else |
| 3366 | tv_eternity(&t->swexpire); |
| 3367 | } |
| 3368 | } |
| 3369 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3370 | /* be nice with the client side which would like to send a complete header |
| 3371 | * FIXME: COMPLETELY BUGGY !!! not all headers may be processed because the client |
| 3372 | * would read all remaining data at once ! The client should not write past rep->lr |
| 3373 | * when the server is in header state. |
| 3374 | */ |
| 3375 | //return header_processed; |
| 3376 | return t->srv_state != SV_STHEADERS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3377 | } |
| 3378 | else if (s == SV_STDATA) { |
| 3379 | /* read or write error */ |
| 3380 | if (t->res_sw == RES_ERROR || t->res_sr == RES_ERROR) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3381 | tv_eternity(&t->srexpire); |
| 3382 | tv_eternity(&t->swexpire); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3383 | fd_delete(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3384 | t->srv_state = SV_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3385 | if (!(t->flags & SN_ERR_MASK)) |
| 3386 | t->flags |= SN_ERR_SRVCL; |
| 3387 | if (!(t->flags & SN_FINST_MASK)) |
| 3388 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3389 | return 1; |
| 3390 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3391 | /* last read, or end of client write */ |
| 3392 | else if (t->res_sr == RES_NULL || c == CL_STSHUTW || c == CL_STCLOSE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3393 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 3394 | tv_eternity(&t->srexpire); |
| 3395 | shutdown(t->srv_fd, SHUT_RD); |
| 3396 | t->srv_state = SV_STSHUTR; |
| 3397 | return 1; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3398 | } |
| 3399 | /* read timeout */ |
| 3400 | else if (tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 3401 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 3402 | tv_eternity(&t->srexpire); |
| 3403 | shutdown(t->srv_fd, SHUT_RD); |
| 3404 | t->srv_state = SV_STSHUTR; |
| 3405 | if (!(t->flags & SN_ERR_MASK)) |
| 3406 | t->flags |= SN_ERR_SRVTO; |
| 3407 | if (!(t->flags & SN_FINST_MASK)) |
| 3408 | t->flags |= SN_FINST_D; |
| 3409 | return 1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3410 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3411 | /* write timeout */ |
| 3412 | else if (tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3413 | FD_CLR(t->srv_fd, StaticWriteEvent); |
| 3414 | tv_eternity(&t->swexpire); |
| 3415 | shutdown(t->srv_fd, SHUT_WR); |
| 3416 | t->srv_state = SV_STSHUTW; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3417 | if (!(t->flags & SN_ERR_MASK)) |
| 3418 | t->flags |= SN_ERR_SRVTO; |
| 3419 | if (!(t->flags & SN_FINST_MASK)) |
| 3420 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3421 | return 1; |
| 3422 | } |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3423 | |
| 3424 | /* recompute request time-outs */ |
| 3425 | if (req->l == 0) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3426 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 3427 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 3428 | tv_eternity(&t->swexpire); |
| 3429 | } |
| 3430 | } |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3431 | else { /* buffer not empty, there are still data to be transferred */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3432 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 3433 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3434 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3435 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3436 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 3437 | t->srexpire = t->swexpire; |
| 3438 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3439 | else |
| 3440 | tv_eternity(&t->swexpire); |
| 3441 | } |
| 3442 | } |
| 3443 | |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3444 | /* recompute response time-outs */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3445 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
| 3446 | if (FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 3447 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 3448 | tv_eternity(&t->srexpire); |
| 3449 | } |
| 3450 | } |
| 3451 | else { |
| 3452 | if (! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 3453 | FD_SET(t->srv_fd, StaticReadEvent); |
| 3454 | if (t->proxy->srvtimeout) |
| 3455 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 3456 | else |
| 3457 | tv_eternity(&t->srexpire); |
| 3458 | } |
| 3459 | } |
| 3460 | |
| 3461 | return 0; /* other cases change nothing */ |
| 3462 | } |
| 3463 | else if (s == SV_STSHUTR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3464 | if (t->res_sw == RES_ERROR) { |
| 3465 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
| 3466 | tv_eternity(&t->swexpire); |
| 3467 | fd_delete(t->srv_fd); |
| 3468 | //close(t->srv_fd); |
| 3469 | t->srv_state = SV_STCLOSE; |
| 3470 | if (!(t->flags & SN_ERR_MASK)) |
| 3471 | t->flags |= SN_ERR_SRVCL; |
| 3472 | if (!(t->flags & SN_FINST_MASK)) |
| 3473 | t->flags |= SN_FINST_D; |
| 3474 | return 1; |
| 3475 | } |
| 3476 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3477 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3478 | tv_eternity(&t->swexpire); |
| 3479 | fd_delete(t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3480 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3481 | t->srv_state = SV_STCLOSE; |
| 3482 | return 1; |
| 3483 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3484 | else if (tv_cmp2_ms(&t->swexpire, &now) <= 0) { |
| 3485 | //FD_CLR(t->srv_fd, StaticWriteEvent); |
| 3486 | tv_eternity(&t->swexpire); |
| 3487 | fd_delete(t->srv_fd); |
| 3488 | //close(t->srv_fd); |
| 3489 | t->srv_state = SV_STCLOSE; |
| 3490 | if (!(t->flags & SN_ERR_MASK)) |
| 3491 | t->flags |= SN_ERR_SRVTO; |
| 3492 | if (!(t->flags & SN_FINST_MASK)) |
| 3493 | t->flags |= SN_FINST_D; |
| 3494 | return 1; |
| 3495 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3496 | else if (req->l == 0) { |
| 3497 | if (FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 3498 | FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
| 3499 | tv_eternity(&t->swexpire); |
| 3500 | } |
| 3501 | } |
| 3502 | else { /* buffer not empty */ |
| 3503 | if (! FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 3504 | FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3505 | if (t->proxy->srvtimeout) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3506 | tv_delayfrom(&t->swexpire, &now, t->proxy->srvtimeout); |
willy tarreau | b1ff9db | 2005-12-17 13:51:03 +0100 | [diff] [blame] | 3507 | /* FIXME: to avoid the server to read-time-out during writes, we refresh it */ |
| 3508 | t->srexpire = t->swexpire; |
| 3509 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3510 | else |
| 3511 | tv_eternity(&t->swexpire); |
| 3512 | } |
| 3513 | } |
| 3514 | return 0; |
| 3515 | } |
| 3516 | else if (s == SV_STSHUTW) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3517 | if (t->res_sr == RES_ERROR) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3518 | //FD_CLR(t->srv_fd, StaticReadEvent); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3519 | tv_eternity(&t->srexpire); |
| 3520 | fd_delete(t->srv_fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3521 | //close(t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3522 | t->srv_state = SV_STCLOSE; |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3523 | if (!(t->flags & SN_ERR_MASK)) |
| 3524 | t->flags |= SN_ERR_SRVCL; |
| 3525 | if (!(t->flags & SN_FINST_MASK)) |
| 3526 | t->flags |= SN_FINST_D; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3527 | return 1; |
| 3528 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3529 | else if (t->res_sr == RES_NULL || c == CL_STSHUTW || c == CL_STCLOSE) { |
| 3530 | //FD_CLR(t->srv_fd, StaticReadEvent); |
| 3531 | tv_eternity(&t->srexpire); |
| 3532 | fd_delete(t->srv_fd); |
| 3533 | //close(t->srv_fd); |
| 3534 | t->srv_state = SV_STCLOSE; |
| 3535 | return 1; |
| 3536 | } |
| 3537 | else if (tv_cmp2_ms(&t->srexpire, &now) <= 0) { |
| 3538 | //FD_CLR(t->srv_fd, StaticReadEvent); |
| 3539 | tv_eternity(&t->srexpire); |
| 3540 | fd_delete(t->srv_fd); |
| 3541 | //close(t->srv_fd); |
| 3542 | t->srv_state = SV_STCLOSE; |
| 3543 | if (!(t->flags & SN_ERR_MASK)) |
| 3544 | t->flags |= SN_ERR_SRVTO; |
| 3545 | if (!(t->flags & SN_FINST_MASK)) |
| 3546 | t->flags |= SN_FINST_D; |
| 3547 | return 1; |
| 3548 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3549 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
| 3550 | if (FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 3551 | FD_CLR(t->srv_fd, StaticReadEvent); |
| 3552 | tv_eternity(&t->srexpire); |
| 3553 | } |
| 3554 | } |
| 3555 | else { |
| 3556 | if (! FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 3557 | FD_SET(t->srv_fd, StaticReadEvent); |
| 3558 | if (t->proxy->srvtimeout) |
| 3559 | tv_delayfrom(&t->srexpire, &now, t->proxy->srvtimeout); |
| 3560 | else |
| 3561 | tv_eternity(&t->srexpire); |
| 3562 | } |
| 3563 | } |
| 3564 | return 0; |
| 3565 | } |
| 3566 | else { /* SV_STCLOSE : nothing to do */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3567 | if ((global.mode & MODE_DEBUG) && !(global.mode & MODE_QUIET)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3568 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame^] | 3569 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", t->uniq_id, t->proxy->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3570 | write(1, trash, len); |
| 3571 | } |
| 3572 | return 0; |
| 3573 | } |
| 3574 | return 0; |
| 3575 | } |
| 3576 | |
| 3577 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3578 | /* Processes the client and server jobs of a session task, then |
| 3579 | * puts it back to the wait queue in a clean state, or |
| 3580 | * cleans up its resources if it must be deleted. Returns |
| 3581 | * the time the task accepts to wait, or -1 for infinity |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3582 | */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3583 | int process_session(struct task *t) { |
| 3584 | struct session *s = t->context; |
| 3585 | int fsm_resync = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3586 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3587 | do { |
| 3588 | fsm_resync = 0; |
| 3589 | //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", t->cli_state, t->srv_state); |
| 3590 | fsm_resync |= process_cli(s); |
| 3591 | //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", t->cli_state, t->srv_state); |
| 3592 | fsm_resync |= process_srv(s); |
| 3593 | //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", t->cli_state, t->srv_state); |
| 3594 | } while (fsm_resync); |
| 3595 | |
| 3596 | if (s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3597 | struct timeval min1, min2; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3598 | 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] | 3599 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3600 | tv_min(&min1, &s->crexpire, &s->cwexpire); |
| 3601 | tv_min(&min2, &s->srexpire, &s->swexpire); |
| 3602 | tv_min(&min1, &min1, &s->cnexpire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3603 | tv_min(&t->expire, &min1, &min2); |
| 3604 | |
| 3605 | /* restore t to its place in the task list */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3606 | task_queue(t); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3607 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3608 | return tv_remain(&now, &t->expire); /* nothing more to do */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3609 | } |
| 3610 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3611 | s->proxy->nbconn--; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3612 | actconn--; |
| 3613 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3614 | if ((global.mode & MODE_DEBUG) && !(global.mode & MODE_QUIET)) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3615 | int len; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame^] | 3616 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", s->uniq_id, s->proxy->id, (unsigned short)s->cli_fd, (unsigned short)s->srv_fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3617 | write(1, trash, len); |
| 3618 | } |
| 3619 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3620 | s->logs.t_close = tv_diff(&s->logs.tv_accept, &now); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3621 | if (s->rep != NULL) |
| 3622 | s->logs.bytes = s->rep->total; |
| 3623 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3624 | /* let's do a final log if we need it */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 3625 | 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] | 3626 | sess_log(s); |
| 3627 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3628 | /* the task MUST not be in the run queue anymore */ |
| 3629 | task_delete(t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3630 | session_free(s); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3631 | task_free(t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3632 | return -1; /* rest in peace for eternity */ |
| 3633 | } |
| 3634 | |
| 3635 | |
| 3636 | |
| 3637 | /* |
| 3638 | * manages a server health-check. Returns |
| 3639 | * the time the task accepts to wait, or -1 for infinity. |
| 3640 | */ |
| 3641 | int process_chk(struct task *t) { |
| 3642 | struct server *s = t->context; |
| 3643 | int fd = s->curfd; |
| 3644 | int one = 1; |
| 3645 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3646 | //fprintf(stderr, "process_chk: task=%p\n", t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3647 | |
| 3648 | if (fd < 0) { /* no check currently running */ |
| 3649 | //fprintf(stderr, "process_chk: 2\n"); |
| 3650 | if (tv_cmp2_ms(&t->expire, &now) > 0) { /* not good time yet */ |
| 3651 | task_queue(t); /* restore t to its place in the task list */ |
| 3652 | return tv_remain(&now, &t->expire); |
| 3653 | } |
| 3654 | |
| 3655 | /* we'll initiate a new check */ |
| 3656 | s->result = 0; /* no result yet */ |
| 3657 | if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) != -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3658 | if ((fd < global.maxsock) && |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3659 | (fcntl(fd, F_SETFL, O_NONBLOCK) != -1) && |
| 3660 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) != -1)) { |
| 3661 | //fprintf(stderr, "process_chk: 3\n"); |
| 3662 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 3663 | /* allow specific binding */ |
| 3664 | if (s->proxy->options & PR_O_BIND_SRC && |
| 3665 | bind(fd, (struct sockaddr *)&s->proxy->source_addr, sizeof(s->proxy->source_addr)) == -1) { |
| 3666 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", s->proxy->id); |
| 3667 | close(fd); |
| 3668 | s->result = -1; |
| 3669 | } |
| 3670 | else if ((connect(fd, (struct sockaddr *)&s->addr, sizeof(s->addr)) != -1) || (errno == EINPROGRESS)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3671 | /* OK, connection in progress or established */ |
| 3672 | |
| 3673 | //fprintf(stderr, "process_chk: 4\n"); |
| 3674 | |
| 3675 | s->curfd = fd; /* that's how we know a test is in progress ;-) */ |
| 3676 | fdtab[fd].owner = t; |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 3677 | fdtab[fd].read = &event_srv_chk_r; |
| 3678 | fdtab[fd].write = &event_srv_chk_w; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3679 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 3680 | FD_SET(fd, StaticWriteEvent); /* for connect status */ |
| 3681 | fd_insert(fd); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3682 | /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */ |
| 3683 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3684 | task_queue(t); /* restore t to its place in the task list */ |
| 3685 | return tv_remain(&now, &t->expire); |
| 3686 | } |
| 3687 | else if (errno != EALREADY && errno != EISCONN && errno != EAGAIN) { |
| 3688 | s->result = -1; /* a real error */ |
| 3689 | } |
| 3690 | } |
| 3691 | //fprintf(stderr, "process_chk: 5\n"); |
| 3692 | close(fd); |
| 3693 | } |
| 3694 | |
| 3695 | if (!s->result) { /* nothing done */ |
| 3696 | //fprintf(stderr, "process_chk: 6\n"); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3697 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3698 | task_queue(t); /* restore t to its place in the task list */ |
| 3699 | return tv_remain(&now, &t->expire); |
| 3700 | } |
| 3701 | |
| 3702 | /* here, we have seen a failure */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3703 | if (s->health > s->rise) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3704 | s->health--; /* still good */ |
| 3705 | else { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 3706 | if (s->health == s->rise) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3707 | if (!(global.mode & MODE_QUIET)) |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3708 | Warning("server %s/%s DOWN.\n", s->proxy->id, s->id); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 3709 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3710 | 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] | 3711 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3712 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3713 | s->health = 0; /* failure */ |
| 3714 | s->state &= ~SRV_RUNNING; |
| 3715 | } |
| 3716 | |
| 3717 | //fprintf(stderr, "process_chk: 7\n"); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3718 | /* FIXME: we allow up to <inter> for a connection to establish, but we should use another parameter */ |
| 3719 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3720 | } |
| 3721 | else { |
| 3722 | //fprintf(stderr, "process_chk: 8\n"); |
| 3723 | /* there was a test running */ |
| 3724 | if (s->result > 0) { /* good server detected */ |
| 3725 | //fprintf(stderr, "process_chk: 9\n"); |
| 3726 | s->health++; /* was bad, stays for a while */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3727 | if (s->health >= s->rise) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 3728 | if (s->health == s->rise) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3729 | if (!(global.mode & MODE_QUIET)) |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3730 | Warning("server %s/%s UP.\n", s->proxy->id, s->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3731 | 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] | 3732 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3733 | |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3734 | s->health = s->rise + s->fall - 1; /* OK now */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3735 | s->state |= SRV_RUNNING; |
| 3736 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3737 | s->curfd = -1; /* no check running anymore */ |
| 3738 | //FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3739 | fd_delete(fd); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3740 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3741 | } |
| 3742 | else if (s->result < 0 || tv_cmp2_ms(&t->expire, &now) <= 0) { |
| 3743 | //fprintf(stderr, "process_chk: 10\n"); |
| 3744 | /* failure or timeout detected */ |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3745 | if (s->health > s->rise) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3746 | s->health--; /* still good */ |
| 3747 | else { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 3748 | if (s->health == s->rise) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3749 | if (!(global.mode & MODE_QUIET)) |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 3750 | Warning("server %s/%s DOWN.\n", s->proxy->id, s->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3751 | |
| 3752 | 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] | 3753 | } |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3754 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3755 | s->health = 0; /* failure */ |
| 3756 | s->state &= ~SRV_RUNNING; |
| 3757 | } |
| 3758 | s->curfd = -1; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3759 | //FD_CLR(fd, StaticWriteEvent); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3760 | fd_delete(fd); |
willy tarreau | e47c8d7 | 2005-12-17 12:55:52 +0100 | [diff] [blame] | 3761 | tv_delayfrom(&t->expire, &now, s->inter); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3762 | } |
| 3763 | /* if result is 0 and there's no timeout, we have to wait again */ |
| 3764 | } |
| 3765 | //fprintf(stderr, "process_chk: 11\n"); |
| 3766 | s->result = 0; |
| 3767 | task_queue(t); /* restore t to its place in the task list */ |
| 3768 | return tv_remain(&now, &t->expire); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3769 | } |
| 3770 | |
| 3771 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3772 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3773 | #if STATTIME > 0 |
| 3774 | int stats(void); |
| 3775 | #endif |
| 3776 | |
| 3777 | /* |
| 3778 | * Main select() loop. |
| 3779 | */ |
| 3780 | |
| 3781 | void select_loop() { |
| 3782 | int next_time; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3783 | int time2; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3784 | int status; |
| 3785 | int fd,i; |
| 3786 | struct timeval delta; |
| 3787 | int readnotnull, writenotnull; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3788 | struct task *t, *tnext; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3789 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3790 | tv_now(&now); |
| 3791 | |
| 3792 | while (1) { |
| 3793 | next_time = -1; /* set the timer to wait eternally first */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3794 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3795 | /* look for expired tasks and add them to the run queue. |
| 3796 | */ |
| 3797 | tnext = ((struct task *)LIST_HEAD(wait_queue))->next; |
| 3798 | while ((t = tnext) != LIST_HEAD(wait_queue)) { /* we haven't looped ? */ |
| 3799 | tnext = t->next; |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3800 | if (t->state & TASK_RUNNING) |
| 3801 | continue; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3802 | |
| 3803 | /* wakeup expired entries. It doesn't matter if they are |
| 3804 | * already running because of a previous event |
| 3805 | */ |
| 3806 | if (tv_cmp2_ms(&t->expire, &now) <= 0) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3807 | //fprintf(stderr,"task_wakeup(%p, %p)\n", &rq, t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3808 | task_wakeup(&rq, t); |
| 3809 | } |
| 3810 | else { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3811 | /* first non-runnable task. Use its expiration date as an upper bound */ |
| 3812 | int temp_time = tv_remain(&now, &t->expire); |
| 3813 | if (temp_time) |
| 3814 | next_time = temp_time; |
| 3815 | //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] | 3816 | break; |
| 3817 | } |
| 3818 | } |
| 3819 | |
| 3820 | /* process each task in the run queue now. Each task may be deleted |
| 3821 | * since we only use tnext. |
| 3822 | */ |
| 3823 | tnext = rq; |
| 3824 | while ((t = tnext) != NULL) { |
| 3825 | int temp_time; |
| 3826 | |
| 3827 | tnext = t->rqnext; |
| 3828 | task_sleep(&rq, t); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3829 | //fprintf(stderr,"task %p\n",t); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3830 | temp_time = t->process(t); |
| 3831 | next_time = MINTIME(temp_time, next_time); |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3832 | //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] | 3833 | } |
| 3834 | |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 3835 | //fprintf(stderr,"---end of run---\n"); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3836 | |
| 3837 | /* maintain all proxies in a consistent state. This should quickly become a task */ |
| 3838 | time2 = maintain_proxies(); |
| 3839 | next_time = MINTIME(time2, next_time); |
| 3840 | |
| 3841 | /* stop when there's no connection left and we don't allow them anymore */ |
| 3842 | if (!actconn && listeners == 0) |
| 3843 | break; |
| 3844 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3845 | |
| 3846 | #if STATTIME > 0 |
| 3847 | time2 = stats(); |
| 3848 | // fprintf(stderr," stats = %d\n", time2); |
| 3849 | next_time = MINTIME(time2, next_time); |
| 3850 | #endif |
| 3851 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3852 | if (next_time > 0) { /* FIXME */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3853 | /* Convert to timeval */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3854 | /* to avoid eventual select loops due to timer precision */ |
| 3855 | next_time += SCHEDULER_RESOLUTION; |
| 3856 | delta.tv_sec = next_time / 1000; |
| 3857 | delta.tv_usec = (next_time % 1000) * 1000; |
| 3858 | } |
| 3859 | else if (next_time == 0) { /* allow select to return immediately when needed */ |
| 3860 | delta.tv_sec = delta.tv_usec = 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3861 | } |
| 3862 | |
| 3863 | |
| 3864 | /* let's restore fdset state */ |
| 3865 | |
| 3866 | readnotnull = 0; writenotnull = 0; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3867 | 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] | 3868 | readnotnull |= (*(((int*)ReadEvent)+i) = *(((int*)StaticReadEvent)+i)) != 0; |
| 3869 | writenotnull |= (*(((int*)WriteEvent)+i) = *(((int*)StaticWriteEvent)+i)) != 0; |
| 3870 | } |
| 3871 | |
| 3872 | // /* just a verification code, needs to be removed for performance */ |
| 3873 | // for (i=0; i<maxfd; i++) { |
| 3874 | // if (FD_ISSET(i, ReadEvent) != FD_ISSET(i, StaticReadEvent)) |
| 3875 | // abort(); |
| 3876 | // if (FD_ISSET(i, WriteEvent) != FD_ISSET(i, StaticWriteEvent)) |
| 3877 | // abort(); |
| 3878 | // |
| 3879 | // } |
| 3880 | |
| 3881 | status=select(maxfd, |
| 3882 | readnotnull ? ReadEvent : NULL, |
| 3883 | writenotnull ? WriteEvent : NULL, |
| 3884 | NULL, |
| 3885 | (next_time >= 0) ? &delta : NULL); |
| 3886 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3887 | /* this is an experiment on the separation of the select work */ |
| 3888 | // status = (readnotnull ? select(maxfd, ReadEvent, NULL, NULL, (next_time >= 0) ? &delta : NULL) : 0); |
| 3889 | // status |= (writenotnull ? select(maxfd, NULL, WriteEvent, NULL, (next_time >= 0) ? &delta : NULL) : 0); |
| 3890 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3891 | tv_now(&now); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3892 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3893 | if (status > 0) { /* must proceed with events */ |
| 3894 | |
| 3895 | int fds; |
| 3896 | char count; |
| 3897 | |
| 3898 | for (fds = 0; (fds << INTBITS) < maxfd; fds++) |
| 3899 | if ((((int *)(ReadEvent))[fds] | ((int *)(WriteEvent))[fds]) != 0) |
| 3900 | for (count = 1<<INTBITS, fd = fds << INTBITS; count && fd < maxfd; count--, fd++) { |
| 3901 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3902 | /* if we specify read first, the accepts and zero reads will be |
| 3903 | * seen first. Moreover, system buffers will be flushed faster. |
| 3904 | */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3905 | if (fdtab[fd].state == FD_STCLOSE) |
| 3906 | continue; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3907 | |
| 3908 | if (FD_ISSET(fd, ReadEvent)) |
| 3909 | fdtab[fd].read(fd); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3910 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3911 | if (FD_ISSET(fd, WriteEvent)) |
| 3912 | fdtab[fd].write(fd); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3913 | } |
| 3914 | } |
| 3915 | else { |
| 3916 | // fprintf(stderr,"select returned %d, maxfd=%d\n", status, maxfd); |
| 3917 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3918 | } |
| 3919 | } |
| 3920 | |
| 3921 | |
| 3922 | #if STATTIME > 0 |
| 3923 | /* |
| 3924 | * Display proxy statistics regularly. It is designed to be called from the |
| 3925 | * select_loop(). |
| 3926 | */ |
| 3927 | int stats(void) { |
| 3928 | static int lines; |
| 3929 | static struct timeval nextevt; |
| 3930 | static struct timeval lastevt; |
| 3931 | static struct timeval starttime = {0,0}; |
| 3932 | unsigned long totaltime, deltatime; |
| 3933 | int ret; |
| 3934 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 3935 | if (tv_cmp(&now, &nextevt) > 0) { |
willy tarreau | 6e682ce | 2005-12-17 13:26:49 +0100 | [diff] [blame] | 3936 | deltatime = (tv_diff(&lastevt, &now)?:1); |
| 3937 | totaltime = (tv_diff(&starttime, &now)?:1); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3938 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3939 | if (global.mode & MODE_STATS) { |
| 3940 | if ((lines++ % 16 == 0) && !(global.mode & MODE_LOG)) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3941 | qfprintf(stderr, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3942 | "\n active total tsknew tskgood tskleft tskrght tsknsch tsklsch tskrsch\n"); |
| 3943 | if (lines>1) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3944 | qfprintf(stderr,"%07d %07d %07d %07d %07d %07d %07d %07d %07d\n", |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3945 | actconn, totalconn, |
| 3946 | stats_tsk_new, stats_tsk_good, |
| 3947 | stats_tsk_left, stats_tsk_right, |
| 3948 | stats_tsk_nsrch, stats_tsk_lsrch, stats_tsk_rsrch); |
| 3949 | } |
| 3950 | } |
| 3951 | |
| 3952 | tv_delayfrom(&nextevt, &now, STATTIME); |
| 3953 | |
| 3954 | lastevt=now; |
| 3955 | } |
| 3956 | ret = tv_remain(&now, &nextevt); |
| 3957 | return ret; |
| 3958 | } |
| 3959 | #endif |
| 3960 | |
| 3961 | |
| 3962 | /* |
| 3963 | * this function enables proxies when there are enough free sessions, |
| 3964 | * 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] | 3965 | * select_loop(). It returns the time left before next expiration event |
| 3966 | * during stop time, -1 otherwise. |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3967 | */ |
| 3968 | static int maintain_proxies(void) { |
| 3969 | struct proxy *p; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3970 | int tleft; /* time left */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3971 | |
| 3972 | p = proxy; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 3973 | tleft = -1; /* infinite time */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3974 | |
| 3975 | /* if there are enough free sessions, we'll activate proxies */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 3976 | if (actconn < global.maxconn) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 3977 | while (p) { |
| 3978 | if (p->nbconn < p->maxconn) { |
| 3979 | if (p->state == PR_STIDLE) { |
| 3980 | FD_SET(p->listen_fd, StaticReadEvent); |
| 3981 | p->state = PR_STRUN; |
| 3982 | } |
| 3983 | } |
| 3984 | else { |
| 3985 | if (p->state == PR_STRUN) { |
| 3986 | FD_CLR(p->listen_fd, StaticReadEvent); |
| 3987 | p->state = PR_STIDLE; |
| 3988 | } |
| 3989 | } |
| 3990 | p = p->next; |
| 3991 | } |
| 3992 | } |
| 3993 | else { /* block all proxies */ |
| 3994 | while (p) { |
| 3995 | if (p->state == PR_STRUN) { |
| 3996 | FD_CLR(p->listen_fd, StaticReadEvent); |
| 3997 | p->state = PR_STIDLE; |
| 3998 | } |
| 3999 | p = p->next; |
| 4000 | } |
| 4001 | } |
| 4002 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4003 | if (stopping) { |
| 4004 | p = proxy; |
| 4005 | while (p) { |
| 4006 | if (p->state != PR_STDISABLED) { |
| 4007 | int t; |
| 4008 | t = tv_remain(&now, &p->stop_time); |
| 4009 | if (t == 0) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 4010 | Warning("Proxy %s stopped.\n", p->id); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4011 | send_log(p, LOG_WARNING, "Proxy %s stopped.\n", p->id); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 4012 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4013 | fd_delete(p->listen_fd); |
| 4014 | p->state = PR_STDISABLED; |
| 4015 | listeners--; |
| 4016 | } |
| 4017 | else { |
| 4018 | tleft = MINTIME(t, tleft); |
| 4019 | } |
| 4020 | } |
| 4021 | p = p->next; |
| 4022 | } |
| 4023 | } |
| 4024 | return tleft; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | /* |
| 4028 | * this function disables health-check servers so that the process will quickly be ignored |
| 4029 | * by load balancers. |
| 4030 | */ |
| 4031 | static void soft_stop(void) { |
| 4032 | struct proxy *p; |
| 4033 | |
| 4034 | stopping = 1; |
| 4035 | p = proxy; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4036 | tv_now(&now); /* else, the old time before select will be used */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4037 | while (p) { |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 4038 | if (p->state != PR_STDISABLED) { |
| 4039 | Warning("Stopping proxy %s in %d ms.\n", p->id, p->grace); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4040 | 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] | 4041 | tv_delayfrom(&p->stop_time, &now, p->grace); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 4042 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4043 | p = p->next; |
| 4044 | } |
| 4045 | } |
| 4046 | |
| 4047 | /* |
| 4048 | * upon SIGUSR1, let's have a soft stop. |
| 4049 | */ |
| 4050 | void sig_soft_stop(int sig) { |
| 4051 | soft_stop(); |
| 4052 | signal(sig, SIG_IGN); |
| 4053 | } |
| 4054 | |
| 4055 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4056 | /* |
| 4057 | * this function dumps every server's state when the process receives SIGHUP. |
| 4058 | */ |
| 4059 | void sig_dump_state(int sig) { |
| 4060 | struct proxy *p = proxy; |
| 4061 | |
| 4062 | Warning("SIGHUP received, dumping servers states.\n"); |
| 4063 | while (p) { |
| 4064 | struct server *s = p->srv; |
| 4065 | |
| 4066 | send_log(p, LOG_NOTICE, "SIGUP received, dumping servers states.\n"); |
| 4067 | while (s) { |
| 4068 | if (s->state & SRV_RUNNING) { |
| 4069 | Warning("SIGHUP: server %s/%s is UP.\n", p->id, s->id); |
| 4070 | send_log(p, LOG_NOTICE, "SIGUP: server %s/%s is UP.\n", p->id, s->id); |
| 4071 | } |
| 4072 | else { |
| 4073 | Warning("SIGHUP: server %s/%s is DOWN.\n", p->id, s->id); |
| 4074 | send_log(p, LOG_NOTICE, "SIGHUP: server %s/%s is DOWN.\n", p->id, s->id); |
| 4075 | } |
| 4076 | s = s->next; |
| 4077 | } |
| 4078 | p = p->next; |
| 4079 | } |
| 4080 | signal(sig, sig_dump_state); |
| 4081 | } |
| 4082 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4083 | void dump(int sig) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4084 | struct task *t, *tnext; |
| 4085 | struct session *s; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4086 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4087 | tnext = ((struct task *)LIST_HEAD(wait_queue))->next; |
| 4088 | while ((t = tnext) != LIST_HEAD(wait_queue)) { /* we haven't looped ? */ |
| 4089 | tnext = t->next; |
| 4090 | s = t->context; |
| 4091 | qfprintf(stderr,"[dump] wq: task %p, still %ld ms, " |
| 4092 | "cli=%d, srv=%d, cr=%d, cw=%d, sr=%d, sw=%d, " |
| 4093 | "req=%d, rep=%d, clifd=%d\n", |
| 4094 | s, tv_remain(&now, &t->expire), |
| 4095 | s->cli_state, |
| 4096 | s->srv_state, |
| 4097 | FD_ISSET(s->cli_fd, StaticReadEvent), |
| 4098 | FD_ISSET(s->cli_fd, StaticWriteEvent), |
| 4099 | FD_ISSET(s->srv_fd, StaticReadEvent), |
| 4100 | FD_ISSET(s->srv_fd, StaticWriteEvent), |
| 4101 | s->req->l, s->rep?s->rep->l:0, s->cli_fd |
| 4102 | ); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4103 | } |
| 4104 | } |
| 4105 | |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4106 | void chain_regex(struct hdr_exp **head, regex_t *preg, int action, char *replace) { |
| 4107 | struct hdr_exp *exp; |
| 4108 | |
| 4109 | while (*head != NULL) |
| 4110 | head = &(*head)->next; |
| 4111 | |
| 4112 | exp = calloc(1, sizeof(struct hdr_exp)); |
| 4113 | |
| 4114 | exp->preg = preg; |
| 4115 | exp->replace = replace; |
| 4116 | exp->action = action; |
| 4117 | *head = exp; |
| 4118 | } |
| 4119 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4120 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4121 | /* |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4122 | * 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] | 4123 | */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4124 | int cfg_parse_global(char *file, int linenum, char **args) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4125 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4126 | if (!strcmp(args[0], "global")) { /* new section */ |
| 4127 | /* no option, nothing special to do */ |
| 4128 | return 0; |
| 4129 | } |
| 4130 | else if (!strcmp(args[0], "daemon")) { |
| 4131 | global.mode |= MODE_DAEMON; |
| 4132 | } |
| 4133 | else if (!strcmp(args[0], "debug")) { |
| 4134 | global.mode |= MODE_DEBUG; |
| 4135 | } |
| 4136 | else if (!strcmp(args[0], "quiet")) { |
| 4137 | global.mode |= MODE_QUIET; |
| 4138 | } |
| 4139 | else if (!strcmp(args[0], "stats")) { |
| 4140 | global.mode |= MODE_STATS; |
| 4141 | } |
| 4142 | else if (!strcmp(args[0], "uid")) { |
| 4143 | if (global.uid != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4144 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4145 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4146 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4147 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4148 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4149 | return -1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4150 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4151 | global.uid = atol(args[1]); |
| 4152 | } |
| 4153 | else if (!strcmp(args[0], "gid")) { |
| 4154 | if (global.gid != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4155 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4156 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4157 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4158 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4159 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4160 | return -1; |
| 4161 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4162 | global.gid = atol(args[1]); |
| 4163 | } |
| 4164 | else if (!strcmp(args[0], "nbproc")) { |
| 4165 | if (global.nbproc != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4166 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4167 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4168 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4169 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4170 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4171 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4172 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4173 | global.nbproc = atol(args[1]); |
| 4174 | } |
| 4175 | else if (!strcmp(args[0], "maxconn")) { |
| 4176 | if (global.maxconn != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4177 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4178 | return 0; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4179 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4180 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4181 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4182 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4183 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4184 | global.maxconn = atol(args[1]); |
| 4185 | } |
| 4186 | else if (!strcmp(args[0], "chroot")) { |
| 4187 | if (global.chroot != NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4188 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4189 | return 0; |
| 4190 | } |
| 4191 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4192 | Alert("parsing [%s:%d] : '%s' expects a directory as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4193 | return -1; |
| 4194 | } |
| 4195 | global.chroot = strdup(args[1]); |
| 4196 | } |
| 4197 | else if (!strcmp(args[0], "log")) { /* syslog server address */ |
| 4198 | struct sockaddr_in *sa; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4199 | int facility, level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4200 | |
| 4201 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4202 | Alert("parsing [%s:%d] : '%s' expects <address> and <facility> as arguments.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4203 | return -1; |
| 4204 | } |
| 4205 | |
| 4206 | for (facility = 0; facility < NB_LOG_FACILITIES; facility++) |
| 4207 | if (!strcmp(log_facilities[facility], args[2])) |
| 4208 | break; |
| 4209 | |
| 4210 | if (facility >= NB_LOG_FACILITIES) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4211 | Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4212 | exit(1); |
| 4213 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4214 | |
| 4215 | level = 7; /* max syslog level = debug */ |
| 4216 | if (*(args[3])) { |
| 4217 | while (level >= 0 && strcmp(log_levels[level], args[3])) |
| 4218 | level--; |
| 4219 | if (level < 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4220 | Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4221 | exit(1); |
| 4222 | } |
| 4223 | } |
| 4224 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4225 | sa = str2sa(args[1]); |
| 4226 | if (!sa->sin_port) |
| 4227 | sa->sin_port = htons(SYSLOG_PORT); |
| 4228 | |
| 4229 | if (global.logfac1 == -1) { |
| 4230 | global.logsrv1 = *sa; |
| 4231 | global.logfac1 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4232 | global.loglev1 = level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4233 | } |
| 4234 | else if (global.logfac2 == -1) { |
| 4235 | global.logsrv2 = *sa; |
| 4236 | global.logfac2 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4237 | global.loglev2 = level; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4238 | } |
| 4239 | else { |
| 4240 | Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum); |
| 4241 | return -1; |
| 4242 | } |
| 4243 | |
| 4244 | } |
| 4245 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4246 | Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "global"); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4247 | return -1; |
| 4248 | } |
| 4249 | return 0; |
| 4250 | } |
| 4251 | |
| 4252 | |
| 4253 | /* |
| 4254 | * parse a line in a <listen> section. Returns 0 if OK, -1 if error. |
| 4255 | */ |
| 4256 | int cfg_parse_listen(char *file, int linenum, char **args) { |
| 4257 | static struct proxy *curproxy = NULL; |
| 4258 | struct server *newsrv = NULL; |
| 4259 | |
| 4260 | if (!strcmp(args[0], "listen")) { /* new proxy */ |
| 4261 | if (strchr(args[2], ':') == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4262 | Alert("parsing [%s:%d] : '%s' expects <id> and <addr:port> as arguments.\n", |
| 4263 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4264 | return -1; |
| 4265 | } |
| 4266 | |
| 4267 | if ((curproxy = (struct proxy *)calloc(1, sizeof(struct proxy))) == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4268 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4269 | return -1; |
| 4270 | } |
| 4271 | curproxy->next = proxy; |
| 4272 | proxy = curproxy; |
| 4273 | curproxy->id = strdup(args[1]); |
| 4274 | curproxy->listen_addr = *str2sa(args[2]); |
| 4275 | curproxy->state = PR_STNEW; |
| 4276 | /* set default values */ |
| 4277 | curproxy->maxconn = cfg_maxpconn; |
| 4278 | curproxy->conn_retries = CONN_RETRIES; |
| 4279 | curproxy->options = 0; |
| 4280 | curproxy->clitimeout = curproxy->contimeout = curproxy->srvtimeout = 0; |
| 4281 | curproxy->mode = PR_MODE_TCP; |
| 4282 | curproxy->logfac1 = curproxy->logfac2 = -1; /* log disabled */ |
| 4283 | curproxy->to_log = 0; |
| 4284 | return 0; |
| 4285 | } |
| 4286 | else if (curproxy == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4287 | Alert("parsing [%s:%d] : 'listen' expected.\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4288 | return -1; |
| 4289 | } |
| 4290 | |
| 4291 | if (!strcmp(args[0], "mode")) { /* sets the proxy mode */ |
| 4292 | if (!strcmp(args[1], "http")) curproxy->mode = PR_MODE_HTTP; |
| 4293 | else if (!strcmp(args[1], "tcp")) curproxy->mode = PR_MODE_TCP; |
| 4294 | else if (!strcmp(args[1], "health")) curproxy->mode = PR_MODE_HEALTH; |
| 4295 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4296 | Alert("parsing [%s:%d] : unknown proxy mode '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4297 | return -1; |
| 4298 | } |
| 4299 | } |
| 4300 | else if (!strcmp(args[0], "disabled")) { /* disables this proxy */ |
| 4301 | curproxy->state = PR_STDISABLED; |
| 4302 | } |
| 4303 | else if (!strcmp(args[0], "cookie")) { /* cookie name */ |
| 4304 | int cur_arg; |
| 4305 | if (curproxy->cookie_name != NULL) { |
| 4306 | Alert("parsing [%s:%d] : cookie name already specified. Continuing.\n", |
| 4307 | file, linenum); |
| 4308 | return 0; |
| 4309 | } |
| 4310 | |
| 4311 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4312 | Alert("parsing [%s:%d] : '%s' expects <cookie_name> as argument.\n", |
| 4313 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4314 | return -1; |
| 4315 | } |
| 4316 | curproxy->cookie_name = strdup(args[1]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4317 | curproxy->cookie_len = strlen(curproxy->cookie_name); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4318 | |
| 4319 | cur_arg = 2; |
| 4320 | while (*(args[cur_arg])) { |
| 4321 | if (!strcmp(args[cur_arg], "rewrite")) { |
| 4322 | curproxy->options |= PR_O_COOK_RW; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4323 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4324 | else if (!strcmp(args[cur_arg], "indirect")) { |
| 4325 | curproxy->options |= PR_O_COOK_IND; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4326 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4327 | else if (!strcmp(args[cur_arg], "insert")) { |
| 4328 | curproxy->options |= PR_O_COOK_INS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4329 | } |
willy tarreau | 240afa6 | 2005-12-17 13:14:35 +0100 | [diff] [blame] | 4330 | else if (!strcmp(args[cur_arg], "nocache")) { |
| 4331 | curproxy->options |= PR_O_COOK_NOC; |
| 4332 | } |
willy tarreau | cd87894 | 2005-12-17 13:27:43 +0100 | [diff] [blame] | 4333 | else if (!strcmp(args[cur_arg], "postonly")) { |
| 4334 | curproxy->options |= PR_O_COOK_POST; |
| 4335 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4336 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4337 | Alert("parsing [%s:%d] : '%s' supports 'rewrite', 'insert', 'indirect', 'nocache' and 'postonly' options.\n", |
| 4338 | file, linenum, args[0]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4339 | return -1; |
| 4340 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4341 | cur_arg++; |
| 4342 | } |
| 4343 | if ((curproxy->options & (PR_O_COOK_RW|PR_O_COOK_IND)) == (PR_O_COOK_RW|PR_O_COOK_IND)) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4344 | Alert("parsing [%s:%d] : cookie 'rewrite' and 'indirect' mode are incompatibles.\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4345 | file, linenum); |
| 4346 | return -1; |
| 4347 | } |
| 4348 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4349 | else if (!strcmp(args[0], "capture")) { /* name of a cookie to capture */ |
| 4350 | if (curproxy->capture_name != NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4351 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", |
| 4352 | file, linenum, args[0]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4353 | return 0; |
| 4354 | } |
| 4355 | |
| 4356 | if (*(args[4]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4357 | Alert("parsing [%s:%d] : '%s' expects 'cookie' <cookie_name> 'len' <len>.\n", |
| 4358 | file, linenum, args[0]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4359 | return -1; |
| 4360 | } |
| 4361 | curproxy->capture_name = strdup(args[2]); |
| 4362 | curproxy->capture_namelen = strlen(curproxy->capture_name); |
| 4363 | curproxy->capture_len = atol(args[4]); |
| 4364 | if (curproxy->capture_len >= CAPTURE_LEN) { |
| 4365 | Warning("parsing [%s:%d] : truncating capture length to %d bytes.\n", |
| 4366 | file, linenum, CAPTURE_LEN - 1); |
| 4367 | curproxy->capture_len = CAPTURE_LEN - 1; |
| 4368 | } |
| 4369 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4370 | else if (!strcmp(args[0], "contimeout")) { /* connect timeout */ |
| 4371 | if (curproxy->contimeout != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4372 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4373 | return 0; |
| 4374 | } |
| 4375 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4376 | Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n", |
| 4377 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4378 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4379 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4380 | curproxy->contimeout = atol(args[1]); |
| 4381 | } |
| 4382 | else if (!strcmp(args[0], "clitimeout")) { /* client timeout */ |
| 4383 | if (curproxy->clitimeout != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4384 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", |
| 4385 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4386 | return 0; |
| 4387 | } |
| 4388 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4389 | Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n", |
| 4390 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4391 | return -1; |
| 4392 | } |
| 4393 | curproxy->clitimeout = atol(args[1]); |
| 4394 | } |
| 4395 | else if (!strcmp(args[0], "srvtimeout")) { /* server timeout */ |
| 4396 | if (curproxy->srvtimeout != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4397 | Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4398 | return 0; |
| 4399 | } |
| 4400 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4401 | Alert("parsing [%s:%d] : '%s' expects an integer <time_in_ms> as argument.\n", |
| 4402 | file, linenum, args[0]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4403 | return -1; |
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 | curproxy->srvtimeout = atol(args[1]); |
| 4406 | } |
| 4407 | else if (!strcmp(args[0], "retries")) { /* connection retries */ |
| 4408 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4409 | Alert("parsing [%s:%d] : '%s' expects an integer argument (dispatch counts for one).\n", |
| 4410 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4411 | return -1; |
| 4412 | } |
| 4413 | curproxy->conn_retries = atol(args[1]); |
| 4414 | } |
| 4415 | else if (!strcmp(args[0], "option")) { |
| 4416 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4417 | Alert("parsing [%s:%d] : '%s' expects an option name.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4418 | return -1; |
| 4419 | } |
| 4420 | if (!strcmp(args[1], "redispatch")) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4421 | /* enable reconnections to dispatch */ |
| 4422 | curproxy->options |= PR_O_REDISP; |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4423 | #ifdef TPROXY |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4424 | else if (!strcmp(args[1], "transparent")) |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4425 | /* enable transparent proxy connections */ |
| 4426 | curproxy->options |= PR_O_TRANSP; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4427 | #endif |
| 4428 | else if (!strcmp(args[1], "keepalive")) |
| 4429 | /* enable keep-alive */ |
| 4430 | curproxy->options |= PR_O_KEEPALIVE; |
| 4431 | else if (!strcmp(args[1], "forwardfor")) |
| 4432 | /* insert x-forwarded-for field */ |
| 4433 | curproxy->options |= PR_O_FWDFOR; |
| 4434 | else if (!strcmp(args[1], "httplog")) { |
| 4435 | /* generate a complete HTTP log */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4436 | curproxy->to_log |= LW_DATE | LW_CLIP | LW_SVID | LW_REQ | LW_PXID | LW_RESP; |
| 4437 | } |
| 4438 | else if (!strcmp(args[1], "dontlognull")) { |
| 4439 | /* don't log empty requests */ |
| 4440 | curproxy->options |= PR_O_NULLNOLOG; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4441 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 4442 | else if (!strcmp(args[1], "httpchk")) { |
| 4443 | /* use HTTP request to check servers' health */ |
| 4444 | curproxy->options |= PR_O_HTTP_CHK; |
willy tarreau | 2f6ba65 | 2005-12-17 13:57:42 +0100 | [diff] [blame^] | 4445 | if (*args[2]) { |
| 4446 | int reqlen = strlen(args[2]) + strlen("OPTIONS / HTTP/1.0\r\n\r\n"); |
| 4447 | curproxy->check_req = (char *)malloc(reqlen); |
| 4448 | curproxy->check_len = snprintf(curproxy->check_req, reqlen, |
| 4449 | "OPTIONS %s HTTP/1.0\r\n\r\n", args[2]); /* URI to use */ |
| 4450 | } else { |
| 4451 | curproxy->check_req = strdup(DEF_CHECK_REQ); /* default request */ |
| 4452 | curproxy->check_len = strlen(DEF_CHECK_REQ); |
| 4453 | } |
willy tarreau | bc4e1fb | 2005-12-17 13:32:07 +0100 | [diff] [blame] | 4454 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4455 | else if (!strcmp(args[1], "persist")) { |
| 4456 | /* persist on using the server specified by the cookie, even when it's down */ |
| 4457 | curproxy->options |= PR_O_PERSIST; |
| 4458 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4459 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4460 | Alert("parsing [%s:%d] : unknown option '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4461 | return -1; |
| 4462 | } |
| 4463 | return 0; |
| 4464 | } |
| 4465 | else if (!strcmp(args[0], "redispatch") || !strcmp(args[0], "redisp")) { |
| 4466 | /* enable reconnections to dispatch */ |
| 4467 | curproxy->options |= PR_O_REDISP; |
| 4468 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4469 | #ifdef TPROXY |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4470 | else if (!strcmp(args[0], "transparent")) { |
| 4471 | /* enable transparent proxy connections */ |
| 4472 | curproxy->options |= PR_O_TRANSP; |
| 4473 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4474 | #endif |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4475 | else if (!strcmp(args[0], "maxconn")) { /* maxconn */ |
| 4476 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4477 | Alert("parsing [%s:%d] : '%s' expects an integer argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4478 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4479 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4480 | curproxy->maxconn = atol(args[1]); |
| 4481 | } |
| 4482 | else if (!strcmp(args[0], "grace")) { /* grace time (ms) */ |
| 4483 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4484 | Alert("parsing [%s:%d] : '%s' expects a time in milliseconds.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4485 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4486 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4487 | curproxy->grace = atol(args[1]); |
| 4488 | } |
| 4489 | else if (!strcmp(args[0], "dispatch")) { /* dispatch address */ |
| 4490 | if (strchr(args[1], ':') == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4491 | Alert("parsing [%s:%d] : '%s' expects <addr:port> as argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4492 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4493 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4494 | curproxy->dispatch_addr = *str2sa(args[1]); |
| 4495 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4496 | else if (!strcmp(args[0], "balance")) { /* set balancing with optional algorithm */ |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4497 | if (*(args[1])) { |
| 4498 | if (!strcmp(args[1], "roundrobin")) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4499 | curproxy->options |= PR_O_BALANCE_RR; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4500 | } |
| 4501 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4502 | Alert("parsing [%s:%d] : '%s' only supports 'roundrobin' option.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4503 | return -1; |
| 4504 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4505 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4506 | else /* if no option is set, use round-robin by default */ |
| 4507 | curproxy->options |= PR_O_BALANCE_RR; |
| 4508 | } |
| 4509 | else if (!strcmp(args[0], "server")) { /* server address */ |
| 4510 | int cur_arg; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4511 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4512 | if (strchr(args[2], ':') == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4513 | Alert("parsing [%s:%d] : '%s' expects <name> and <addr:port> as arguments.\n", |
| 4514 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4515 | return -1; |
| 4516 | } |
| 4517 | if ((newsrv = (struct server *)calloc(1, sizeof(struct server))) == NULL) { |
| 4518 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
| 4519 | return -1; |
| 4520 | } |
| 4521 | newsrv->next = curproxy->srv; |
| 4522 | curproxy->srv = newsrv; |
| 4523 | newsrv->proxy = curproxy; |
| 4524 | newsrv->id = strdup(args[1]); |
| 4525 | newsrv->addr = *str2sa(args[2]); |
| 4526 | newsrv->state = SRV_RUNNING; /* early server setup */ |
| 4527 | newsrv->curfd = -1; /* no health-check in progress */ |
| 4528 | newsrv->inter = DEF_CHKINTR; |
| 4529 | newsrv->rise = DEF_RISETIME; |
| 4530 | newsrv->fall = DEF_FALLTIME; |
| 4531 | newsrv->health = newsrv->rise; /* up, but will fall down at first failure */ |
| 4532 | cur_arg = 3; |
| 4533 | while (*args[cur_arg]) { |
| 4534 | if (!strcmp(args[cur_arg], "cookie")) { |
| 4535 | newsrv->cookie = strdup(args[cur_arg + 1]); |
| 4536 | newsrv->cklen = strlen(args[cur_arg + 1]); |
| 4537 | cur_arg += 2; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4538 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4539 | else if (!strcmp(args[cur_arg], "rise")) { |
| 4540 | newsrv->rise = atol(args[cur_arg + 1]); |
| 4541 | newsrv->health = newsrv->rise; |
| 4542 | cur_arg += 2; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4543 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4544 | else if (!strcmp(args[cur_arg], "fall")) { |
| 4545 | newsrv->fall = atol(args[cur_arg + 1]); |
| 4546 | cur_arg += 2; |
| 4547 | } |
| 4548 | else if (!strcmp(args[cur_arg], "inter")) { |
| 4549 | newsrv->inter = atol(args[cur_arg + 1]); |
| 4550 | cur_arg += 2; |
| 4551 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4552 | else if (!strcmp(args[cur_arg], "backup")) { |
| 4553 | newsrv->state |= SRV_BACKUP; |
| 4554 | cur_arg ++; |
| 4555 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4556 | else if (!strcmp(args[cur_arg], "check")) { |
| 4557 | struct task *t; |
| 4558 | |
| 4559 | if ((t = pool_alloc(task)) == NULL) { /* disable this proxy for a while */ |
| 4560 | Alert("parsing [%s:%d] : out of memory.\n", file, linenum); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4561 | return -1; |
| 4562 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4563 | |
| 4564 | t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */ |
| 4565 | t->wq = LIST_HEAD(wait_queue); /* but already has a wait queue assigned */ |
| 4566 | t->state = TASK_IDLE; |
| 4567 | t->process = process_chk; |
| 4568 | t->context = newsrv; |
| 4569 | |
| 4570 | if (curproxy->state != PR_STDISABLED) { |
| 4571 | tv_delayfrom(&t->expire, &now, newsrv->inter); /* check this every ms */ |
| 4572 | task_queue(t); |
| 4573 | task_wakeup(&rq, t); |
| 4574 | } |
| 4575 | |
| 4576 | cur_arg += 1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4577 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4578 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4579 | Alert("parsing [%s:%d] : server %s only supports options 'cookie', 'check', 'inter', 'rise' and 'fall'.\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4580 | file, linenum, newsrv->id); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4581 | return -1; |
| 4582 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4583 | } |
| 4584 | curproxy->nbservers++; |
| 4585 | } |
| 4586 | else if (!strcmp(args[0], "log")) { /* syslog server address */ |
| 4587 | struct sockaddr_in *sa; |
| 4588 | int facility; |
| 4589 | |
| 4590 | if (*(args[1]) && *(args[2]) == 0 && !strcmp(args[1], "global")) { |
| 4591 | curproxy->logfac1 = global.logfac1; |
| 4592 | curproxy->logsrv1 = global.logsrv1; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4593 | curproxy->loglev1 = global.loglev1; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4594 | curproxy->logfac2 = global.logfac2; |
| 4595 | curproxy->logsrv2 = global.logsrv2; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4596 | curproxy->loglev2 = global.loglev2; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4597 | } |
| 4598 | else if (*(args[1]) && *(args[2])) { |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4599 | int level; |
| 4600 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4601 | for (facility = 0; facility < NB_LOG_FACILITIES; facility++) |
| 4602 | if (!strcmp(log_facilities[facility], args[2])) |
| 4603 | break; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4604 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4605 | if (facility >= NB_LOG_FACILITIES) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4606 | Alert("parsing [%s:%d] : unknown log facility '%s'\n", file, linenum, args[2]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4607 | exit(1); |
| 4608 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4609 | |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4610 | level = 7; /* max syslog level = debug */ |
| 4611 | if (*(args[3])) { |
| 4612 | while (level >= 0 && strcmp(log_levels[level], args[3])) |
| 4613 | level--; |
| 4614 | if (level < 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4615 | Alert("parsing [%s:%d] : unknown optional log level '%s'\n", file, linenum, args[3]); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4616 | exit(1); |
| 4617 | } |
| 4618 | } |
| 4619 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4620 | sa = str2sa(args[1]); |
| 4621 | if (!sa->sin_port) |
| 4622 | sa->sin_port = htons(SYSLOG_PORT); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4623 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4624 | if (curproxy->logfac1 == -1) { |
| 4625 | curproxy->logsrv1 = *sa; |
| 4626 | curproxy->logfac1 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4627 | curproxy->loglev1 = level; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4628 | } |
| 4629 | else if (curproxy->logfac2 == -1) { |
| 4630 | curproxy->logsrv2 = *sa; |
| 4631 | curproxy->logfac2 = facility; |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4632 | curproxy->loglev2 = level; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4633 | } |
| 4634 | else { |
| 4635 | Alert("parsing [%s:%d] : too many syslog servers\n", file, linenum); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4636 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4637 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4638 | } |
| 4639 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4640 | Alert("parsing [%s:%d] : 'log' expects either <address[:port]> and <facility> or 'global' as arguments.\n", |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4641 | file, linenum); |
| 4642 | return -1; |
| 4643 | } |
| 4644 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4645 | else if (!strcmp(args[0], "source")) { /* address to which we bind when connecting */ |
| 4646 | if (strchr(args[1], ':') == NULL) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4647 | Alert("parsing [%s:%d] : '%s' expects <addr:port> as argument.\n", |
| 4648 | file, linenum, "source"); |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 4649 | return -1; |
| 4650 | } |
| 4651 | |
| 4652 | curproxy->source_addr = *str2sa(args[1]); |
| 4653 | curproxy->options |= PR_O_BIND_SRC; |
| 4654 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4655 | else if (!strcmp(args[0], "cliexp") || !strcmp(args[0], "reqrep")) { /* replace request header from a regex */ |
| 4656 | regex_t *preg; |
| 4657 | |
| 4658 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4659 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 4660 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4661 | return -1; |
| 4662 | } |
| 4663 | |
| 4664 | preg = calloc(1, sizeof(regex_t)); |
| 4665 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4666 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4667 | return -1; |
| 4668 | } |
| 4669 | |
| 4670 | chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 4671 | } |
| 4672 | else if (!strcmp(args[0], "reqdel")) { /* delete request header from a regex */ |
| 4673 | regex_t *preg; |
| 4674 | |
| 4675 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4676 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4677 | return -1; |
| 4678 | } |
| 4679 | |
| 4680 | preg = calloc(1, sizeof(regex_t)); |
| 4681 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4682 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4683 | return -1; |
| 4684 | } |
| 4685 | |
| 4686 | chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL); |
| 4687 | } |
| 4688 | else if (!strcmp(args[0], "reqdeny")) { /* deny a request if a header matches this regex */ |
| 4689 | regex_t *preg; |
| 4690 | |
| 4691 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4692 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4693 | return -1; |
| 4694 | } |
| 4695 | |
| 4696 | preg = calloc(1, sizeof(regex_t)); |
| 4697 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4698 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4699 | return -1; |
| 4700 | } |
| 4701 | |
| 4702 | chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL); |
| 4703 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4704 | else if (!strcmp(args[0], "reqpass")) { /* pass this header without allowing or denying the request */ |
| 4705 | regex_t *preg; |
| 4706 | |
| 4707 | if (*(args[1]) == 0) { |
| 4708 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
| 4709 | return -1; |
| 4710 | } |
| 4711 | |
| 4712 | preg = calloc(1, sizeof(regex_t)); |
| 4713 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
| 4714 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 4715 | return -1; |
| 4716 | } |
| 4717 | |
| 4718 | chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL); |
| 4719 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4720 | else if (!strcmp(args[0], "reqallow")) { /* allow a request if a header matches this regex */ |
| 4721 | regex_t *preg; |
| 4722 | |
| 4723 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4724 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4725 | return -1; |
| 4726 | } |
| 4727 | |
| 4728 | preg = calloc(1, sizeof(regex_t)); |
| 4729 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4730 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4731 | return -1; |
| 4732 | } |
| 4733 | |
| 4734 | chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL); |
| 4735 | } |
| 4736 | else if (!strcmp(args[0], "reqirep")) { /* replace request header from a regex, ignoring case */ |
| 4737 | regex_t *preg; |
| 4738 | |
| 4739 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4740 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 4741 | file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4742 | return -1; |
| 4743 | } |
| 4744 | |
| 4745 | preg = calloc(1, sizeof(regex_t)); |
| 4746 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4747 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4748 | return -1; |
| 4749 | } |
| 4750 | |
| 4751 | chain_regex(&curproxy->req_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 4752 | } |
| 4753 | else if (!strcmp(args[0], "reqidel")) { /* delete request header from a regex ignoring case */ |
| 4754 | regex_t *preg; |
| 4755 | |
| 4756 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4757 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4758 | return -1; |
| 4759 | } |
| 4760 | |
| 4761 | preg = calloc(1, sizeof(regex_t)); |
| 4762 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4763 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4764 | return -1; |
| 4765 | } |
| 4766 | |
| 4767 | chain_regex(&curproxy->req_exp, preg, ACT_REMOVE, NULL); |
| 4768 | } |
| 4769 | else if (!strcmp(args[0], "reqideny")) { /* deny a request if a header matches this regex ignoring case */ |
| 4770 | regex_t *preg; |
| 4771 | |
| 4772 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4773 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4774 | return -1; |
| 4775 | } |
| 4776 | |
| 4777 | preg = calloc(1, sizeof(regex_t)); |
| 4778 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4779 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4780 | return -1; |
| 4781 | } |
| 4782 | |
| 4783 | chain_regex(&curproxy->req_exp, preg, ACT_DENY, NULL); |
| 4784 | } |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4785 | else if (!strcmp(args[0], "reqipass")) { /* pass this header without allowing or denying the request */ |
| 4786 | regex_t *preg; |
| 4787 | |
| 4788 | if (*(args[1]) == 0) { |
| 4789 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
| 4790 | return -1; |
| 4791 | } |
| 4792 | |
| 4793 | preg = calloc(1, sizeof(regex_t)); |
| 4794 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
| 4795 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
| 4796 | return -1; |
| 4797 | } |
| 4798 | |
| 4799 | chain_regex(&curproxy->req_exp, preg, ACT_PASS, NULL); |
| 4800 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4801 | else if (!strcmp(args[0], "reqiallow")) { /* allow a request if a header matches this regex ignoring case */ |
| 4802 | regex_t *preg; |
| 4803 | |
| 4804 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4805 | Alert("parsing [%s:%d] : '%s' expects <regex> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4806 | return -1; |
| 4807 | } |
| 4808 | |
| 4809 | preg = calloc(1, sizeof(regex_t)); |
| 4810 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4811 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4812 | return -1; |
| 4813 | } |
| 4814 | |
| 4815 | chain_regex(&curproxy->req_exp, preg, ACT_ALLOW, NULL); |
| 4816 | } |
| 4817 | else if (!strcmp(args[0], "reqadd")) { /* add request header */ |
| 4818 | if (curproxy->nb_reqadd >= MAX_NEWHDR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4819 | Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4820 | return 0; |
| 4821 | } |
| 4822 | |
| 4823 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4824 | Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4825 | return -1; |
| 4826 | } |
| 4827 | |
| 4828 | curproxy->req_add[curproxy->nb_reqadd++] = strdup(args[1]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4829 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4830 | 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] | 4831 | regex_t *preg; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4832 | |
| 4833 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4834 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 4835 | file, linenum, args[0]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4836 | return -1; |
| 4837 | } |
| 4838 | |
| 4839 | preg = calloc(1, sizeof(regex_t)); |
| 4840 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4841 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4842 | return -1; |
| 4843 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4844 | |
| 4845 | chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 4846 | } |
| 4847 | else if (!strcmp(args[0], "rspdel")) { /* delete response header from a regex */ |
| 4848 | regex_t *preg; |
| 4849 | |
| 4850 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4851 | Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4852 | return -1; |
| 4853 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4854 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4855 | preg = calloc(1, sizeof(regex_t)); |
| 4856 | if (regcomp(preg, args[1], REG_EXTENDED) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4857 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4858 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 4859 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4860 | |
| 4861 | chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2])); |
| 4862 | } |
| 4863 | 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] | 4864 | regex_t *preg; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4865 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4866 | if (*(args[1]) == 0 || *(args[2]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4867 | Alert("parsing [%s:%d] : '%s' expects <search> and <replace> as arguments.\n", |
| 4868 | file, linenum, args[0]); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4869 | return -1; |
| 4870 | } |
| 4871 | |
| 4872 | preg = calloc(1, sizeof(regex_t)); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4873 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4874 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4875 | return -1; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4876 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4877 | |
| 4878 | chain_regex(&curproxy->rsp_exp, preg, ACT_REPLACE, strdup(args[2])); |
| 4879 | } |
| 4880 | else if (!strcmp(args[0], "rspidel")) { /* delete response header from a regex ignoring case */ |
| 4881 | regex_t *preg; |
| 4882 | |
| 4883 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4884 | Alert("parsing [%s:%d] : '%s' expects <search> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4885 | return -1; |
| 4886 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4887 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4888 | preg = calloc(1, sizeof(regex_t)); |
| 4889 | if (regcomp(preg, args[1], REG_EXTENDED | REG_ICASE) != 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4890 | Alert("parsing [%s:%d] : bad regular expression '%s'.\n", file, linenum, args[1]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4891 | return -1; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4892 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4893 | |
| 4894 | chain_regex(&curproxy->rsp_exp, preg, ACT_REMOVE, strdup(args[2])); |
| 4895 | } |
| 4896 | else if (!strcmp(args[0], "rspadd")) { /* add response header */ |
| 4897 | if (curproxy->nb_rspadd >= MAX_NEWHDR) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4898 | Alert("parsing [%s:%d] : too many '%s'. Continuing.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4899 | return 0; |
| 4900 | } |
| 4901 | |
| 4902 | if (*(args[1]) == 0) { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4903 | Alert("parsing [%s:%d] : '%s' expects <header> as an argument.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4904 | return -1; |
| 4905 | } |
| 4906 | |
| 4907 | curproxy->rsp_add[curproxy->nb_rspadd++] = strdup(args[1]); |
| 4908 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 4909 | else if (!strcmp(args[0], "errorloc")) { /* error location */ |
| 4910 | int errnum; |
| 4911 | char *err; |
| 4912 | |
| 4913 | if (*(args[2]) == 0) { |
| 4914 | Alert("parsing [%s:%d] : <errorloc> expects <error> and <url> as arguments.\n", file, linenum); |
| 4915 | return -1; |
| 4916 | } |
| 4917 | |
| 4918 | errnum = atol(args[1]); |
| 4919 | err = malloc(strlen(HTTP_302) + strlen(args[2]) + 5); |
| 4920 | sprintf(err, "%s%s\r\n\r\n", HTTP_302, args[2]); |
| 4921 | |
| 4922 | if (errnum == 400) { |
| 4923 | if (curproxy->errmsg.msg400) { |
| 4924 | Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
| 4925 | free(curproxy->errmsg.msg400); |
| 4926 | } |
| 4927 | curproxy->errmsg.msg400 = err; |
| 4928 | curproxy->errmsg.len400 = strlen(err); |
| 4929 | } |
| 4930 | else if (errnum == 403) { |
| 4931 | if (curproxy->errmsg.msg403) { |
| 4932 | Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
| 4933 | free(curproxy->errmsg.msg403); |
| 4934 | } |
| 4935 | curproxy->errmsg.msg403 = err; |
| 4936 | curproxy->errmsg.len403 = strlen(err); |
| 4937 | } |
| 4938 | else if (errnum == 408) { |
| 4939 | if (curproxy->errmsg.msg408) { |
| 4940 | Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
| 4941 | free(curproxy->errmsg.msg408); |
| 4942 | } |
| 4943 | curproxy->errmsg.msg408 = err; |
| 4944 | curproxy->errmsg.len408 = strlen(err); |
| 4945 | } |
| 4946 | else if (errnum == 500) { |
| 4947 | if (curproxy->errmsg.msg500) { |
| 4948 | Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
| 4949 | free(curproxy->errmsg.msg500); |
| 4950 | } |
| 4951 | curproxy->errmsg.msg500 = err; |
| 4952 | curproxy->errmsg.len500 = strlen(err); |
| 4953 | } |
| 4954 | else if (errnum == 502) { |
| 4955 | if (curproxy->errmsg.msg502) { |
| 4956 | Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
| 4957 | free(curproxy->errmsg.msg502); |
| 4958 | } |
| 4959 | curproxy->errmsg.msg502 = err; |
| 4960 | curproxy->errmsg.len502 = strlen(err); |
| 4961 | } |
| 4962 | else if (errnum == 503) { |
| 4963 | if (curproxy->errmsg.msg503) { |
| 4964 | Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
| 4965 | free(curproxy->errmsg.msg503); |
| 4966 | } |
| 4967 | curproxy->errmsg.msg503 = err; |
| 4968 | curproxy->errmsg.len503 = strlen(err); |
| 4969 | } |
| 4970 | else if (errnum == 504) { |
| 4971 | if (curproxy->errmsg.msg504) { |
| 4972 | Warning("parsing [%s:%d] : error %d already defined.\n", file, linenum, errnum); |
| 4973 | free(curproxy->errmsg.msg504); |
| 4974 | } |
| 4975 | curproxy->errmsg.msg504 = err; |
| 4976 | curproxy->errmsg.len504 = strlen(err); |
| 4977 | } |
| 4978 | else { |
| 4979 | Warning("parsing [%s:%d] : error %d relocation will be ignored.\n", file, linenum, errnum); |
| 4980 | free(err); |
| 4981 | } |
| 4982 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4983 | else { |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 4984 | Alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], "listen"); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4985 | return -1; |
| 4986 | } |
| 4987 | return 0; |
| 4988 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 4989 | |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 4990 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 4991 | /* |
| 4992 | * This function reads and parses the configuration file given in the argument. |
| 4993 | * returns 0 if OK, -1 if error. |
| 4994 | */ |
| 4995 | int readcfgfile(char *file) { |
| 4996 | char thisline[256]; |
| 4997 | char *line; |
| 4998 | FILE *f; |
| 4999 | int linenum = 0; |
| 5000 | char *end; |
| 5001 | char *args[MAX_LINE_ARGS]; |
| 5002 | int arg; |
| 5003 | int cfgerr = 0; |
| 5004 | int confsect = CFG_NONE; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 5005 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5006 | struct proxy *curproxy = NULL; |
| 5007 | struct server *newsrv = NULL; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 5008 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5009 | if ((f=fopen(file,"r")) == NULL) |
| 5010 | return -1; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 5011 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5012 | while (fgets(line = thisline, sizeof(thisline), f) != NULL) { |
| 5013 | linenum++; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5014 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5015 | end = line + strlen(line); |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5016 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5017 | /* skip leading spaces */ |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 5018 | while (isspace((int)*line)) |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5019 | line++; |
| 5020 | |
| 5021 | arg = 0; |
| 5022 | args[arg] = line; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5023 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5024 | while (*line && arg < MAX_LINE_ARGS) { |
| 5025 | /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their |
| 5026 | * C equivalent value. Other combinations left unchanged (eg: \1). |
| 5027 | */ |
| 5028 | if (*line == '\\') { |
| 5029 | int skip = 0; |
| 5030 | if (line[1] == ' ' || line[1] == '\\' || line[1] == '#') { |
| 5031 | *line = line[1]; |
| 5032 | skip = 1; |
| 5033 | } |
| 5034 | else if (line[1] == 'r') { |
| 5035 | *line = '\r'; |
| 5036 | skip = 1; |
| 5037 | } |
| 5038 | else if (line[1] == 'n') { |
| 5039 | *line = '\n'; |
| 5040 | skip = 1; |
| 5041 | } |
| 5042 | else if (line[1] == 't') { |
| 5043 | *line = '\t'; |
| 5044 | skip = 1; |
| 5045 | } |
| 5046 | else if (line[1] == 'x' && (line + 3 < end )) { |
| 5047 | unsigned char hex1, hex2; |
| 5048 | hex1 = toupper(line[2]) - '0'; hex2 = toupper(line[3]) - '0'; |
| 5049 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 5050 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 5051 | *line = (hex1<<4) + hex2; |
| 5052 | skip = 3; |
| 5053 | } |
| 5054 | if (skip) { |
| 5055 | memmove(line + 1, line + 1 + skip, end - (line + skip + 1)); |
| 5056 | end -= skip; |
| 5057 | } |
| 5058 | line++; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5059 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 5060 | else if (*line == '#' || *line == '\n' || *line == '\r') { |
| 5061 | /* end of string, end of loop */ |
| 5062 | *line = 0; |
| 5063 | break; |
| 5064 | } |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 5065 | else if (isspace((int)*line)) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5066 | /* a non-escaped space is an argument separator */ |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 5067 | *line++ = 0; |
willy tarreau | c29948c | 2005-12-17 13:10:27 +0100 | [diff] [blame] | 5068 | while (isspace((int)*line)) |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 5069 | line++; |
| 5070 | args[++arg] = line; |
| 5071 | } |
| 5072 | else { |
| 5073 | line++; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5074 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5075 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5076 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5077 | /* empty line */ |
| 5078 | if (!**args) |
| 5079 | continue; |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 5080 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5081 | /* zero out remaining args */ |
| 5082 | while (++arg < MAX_LINE_ARGS) { |
| 5083 | args[arg] = line; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5084 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5085 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5086 | if (!strcmp(args[0], "listen")) /* new proxy */ |
| 5087 | confsect = CFG_LISTEN; |
| 5088 | else if (!strcmp(args[0], "global")) /* global config */ |
| 5089 | confsect = CFG_GLOBAL; |
| 5090 | /* else it's a section keyword */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5091 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5092 | switch (confsect) { |
| 5093 | case CFG_LISTEN: |
| 5094 | if (cfg_parse_listen(file, linenum, args) < 0) |
| 5095 | return -1; |
| 5096 | break; |
| 5097 | case CFG_GLOBAL: |
| 5098 | if (cfg_parse_global(file, linenum, args) < 0) |
| 5099 | return -1; |
| 5100 | break; |
| 5101 | default: |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5102 | Alert("parsing [%s:%d] : unknown keyword '%s' out of section.\n", file, linenum, args[0]); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5103 | return -1; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5104 | } |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5105 | |
| 5106 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5107 | } |
| 5108 | fclose(f); |
| 5109 | |
| 5110 | /* |
| 5111 | * Now, check for the integrity of all that we have collected. |
| 5112 | */ |
| 5113 | |
| 5114 | if ((curproxy = proxy) == NULL) { |
| 5115 | Alert("parsing %s : no <listen> line. Nothing to do !\n", |
| 5116 | file); |
| 5117 | return -1; |
| 5118 | } |
| 5119 | |
| 5120 | while (curproxy != NULL) { |
willy tarreau | ef900ab | 2005-12-17 12:52:52 +0100 | [diff] [blame] | 5121 | if (curproxy->state == PR_STDISABLED) { |
| 5122 | curproxy = curproxy->next; |
| 5123 | continue; |
| 5124 | } |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5125 | if ((curproxy->mode != PR_MODE_HEALTH) && |
| 5126 | !(curproxy->options & (PR_O_TRANSP | PR_O_BALANCE)) && |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 5127 | (*(int *)&curproxy->dispatch_addr.sin_addr == 0)) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5128 | Alert("parsing %s : listener %s has no dispatch address and is not in transparent or balance mode.\n", |
| 5129 | file, curproxy->id); |
| 5130 | cfgerr++; |
| 5131 | } |
| 5132 | else if ((curproxy->mode != PR_MODE_HEALTH) && (curproxy->options & PR_O_BALANCE)) { |
| 5133 | if (curproxy->options & PR_O_TRANSP) { |
| 5134 | Alert("parsing %s : listener %s cannot use both transparent and balance mode.\n", |
| 5135 | file, curproxy->id); |
| 5136 | cfgerr++; |
| 5137 | } |
| 5138 | else if (curproxy->srv == NULL) { |
| 5139 | Alert("parsing %s : listener %s needs at least 1 server in balance mode.\n", |
| 5140 | file, curproxy->id); |
| 5141 | cfgerr++; |
| 5142 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 5143 | else if (*(int *)&curproxy->dispatch_addr.sin_addr != 0) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5144 | Warning("parsing %s : dispatch address of listener %s will be ignored in balance mode.\n", |
| 5145 | file, curproxy->id); |
| 5146 | } |
| 5147 | } |
| 5148 | 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] | 5149 | if (curproxy->cookie_name != NULL) { |
| 5150 | Warning("parsing %s : cookie will be ignored for listener %s.\n", |
| 5151 | file, curproxy->id); |
| 5152 | } |
| 5153 | if ((newsrv = curproxy->srv) != NULL) { |
| 5154 | Warning("parsing %s : servers will be ignored for listener %s.\n", |
| 5155 | file, curproxy->id); |
| 5156 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 5157 | if (curproxy->rsp_exp != NULL) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5158 | Warning("parsing %s : server regular expressions will be ignored for listener %s.\n", |
| 5159 | file, curproxy->id); |
| 5160 | } |
willy tarreau | e39cd13 | 2005-12-17 13:00:18 +0100 | [diff] [blame] | 5161 | if (curproxy->req_exp != NULL) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5162 | Warning("parsing %s : client regular expressions will be ignored for listener %s.\n", |
| 5163 | file, curproxy->id); |
| 5164 | } |
| 5165 | } |
| 5166 | else if (curproxy->mode == PR_MODE_HTTP) { /* HTTP PROXY */ |
| 5167 | if ((curproxy->cookie_name != NULL) && ((newsrv = curproxy->srv) == NULL)) { |
| 5168 | Alert("parsing %s : HTTP proxy %s has a cookie but no server list !\n", |
| 5169 | file, curproxy->id); |
| 5170 | cfgerr++; |
| 5171 | } |
| 5172 | else { |
| 5173 | while (newsrv != NULL) { |
| 5174 | /* nothing to check for now */ |
| 5175 | newsrv = newsrv->next; |
| 5176 | } |
| 5177 | } |
| 5178 | } |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 5179 | if (curproxy->errmsg.msg400 == NULL) { |
| 5180 | curproxy->errmsg.msg400 = (char *)HTTP_400; |
| 5181 | curproxy->errmsg.len400 = strlen(HTTP_400); |
| 5182 | } |
| 5183 | if (curproxy->errmsg.msg403 == NULL) { |
| 5184 | curproxy->errmsg.msg403 = (char *)HTTP_403; |
| 5185 | curproxy->errmsg.len403 = strlen(HTTP_403); |
| 5186 | } |
| 5187 | if (curproxy->errmsg.msg408 == NULL) { |
| 5188 | curproxy->errmsg.msg408 = (char *)HTTP_408; |
| 5189 | curproxy->errmsg.len408 = strlen(HTTP_408); |
| 5190 | } |
| 5191 | if (curproxy->errmsg.msg500 == NULL) { |
| 5192 | curproxy->errmsg.msg500 = (char *)HTTP_500; |
| 5193 | curproxy->errmsg.len500 = strlen(HTTP_500); |
| 5194 | } |
| 5195 | if (curproxy->errmsg.msg502 == NULL) { |
| 5196 | curproxy->errmsg.msg502 = (char *)HTTP_502; |
| 5197 | curproxy->errmsg.len502 = strlen(HTTP_502); |
| 5198 | } |
| 5199 | if (curproxy->errmsg.msg503 == NULL) { |
| 5200 | curproxy->errmsg.msg503 = (char *)HTTP_503; |
| 5201 | curproxy->errmsg.len503 = strlen(HTTP_503); |
| 5202 | } |
| 5203 | if (curproxy->errmsg.msg504 == NULL) { |
| 5204 | curproxy->errmsg.msg504 = (char *)HTTP_504; |
| 5205 | curproxy->errmsg.len504 = strlen(HTTP_504); |
| 5206 | } |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5207 | curproxy = curproxy->next; |
| 5208 | } |
| 5209 | if (cfgerr > 0) { |
| 5210 | Alert("Errors found in configuration file, aborting.\n"); |
| 5211 | return -1; |
| 5212 | } |
| 5213 | else |
| 5214 | return 0; |
| 5215 | } |
| 5216 | |
| 5217 | |
| 5218 | /* |
| 5219 | * This function initializes all the necessary variables. It only returns |
| 5220 | * if everything is OK. If something fails, it exits. |
| 5221 | */ |
| 5222 | void init(int argc, char **argv) { |
| 5223 | int i; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5224 | int arg_mode = 0; /* MODE_DEBUG, ... */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5225 | char *old_argv = *argv; |
| 5226 | char *tmp; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5227 | int cfg_maxconn = 0; /* # of simultaneous connections, (-n) */ |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5228 | |
| 5229 | if (1<<INTBITS != sizeof(int)*8) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5230 | qfprintf(stderr, |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5231 | "Error: wrong architecture. Recompile so that sizeof(int)=%d\n", |
| 5232 | sizeof(int)*8); |
| 5233 | exit(1); |
| 5234 | } |
| 5235 | |
| 5236 | pid = getpid(); |
| 5237 | progname = *argv; |
| 5238 | while ((tmp = strchr(progname, '/')) != NULL) |
| 5239 | progname = tmp + 1; |
| 5240 | |
| 5241 | argc--; argv++; |
| 5242 | while (argc > 0) { |
| 5243 | char *flag; |
| 5244 | |
| 5245 | if (**argv == '-') { |
| 5246 | flag = *argv+1; |
| 5247 | |
| 5248 | /* 1 arg */ |
| 5249 | if (*flag == 'v') { |
| 5250 | display_version(); |
| 5251 | exit(0); |
| 5252 | } |
| 5253 | else if (*flag == 'd') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5254 | arg_mode |= MODE_DEBUG; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5255 | else if (*flag == 'D') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5256 | arg_mode |= MODE_DAEMON | MODE_QUIET; |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5257 | else if (*flag == 'q') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5258 | arg_mode |= MODE_QUIET; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5259 | #if STATTIME > 0 |
| 5260 | else if (*flag == 's') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5261 | arg_mode |= MODE_STATS; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5262 | else if (*flag == 'l') |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5263 | arg_mode |= MODE_LOG; |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5264 | #endif |
| 5265 | else { /* >=2 args */ |
| 5266 | argv++; argc--; |
| 5267 | if (argc == 0) |
| 5268 | usage(old_argv); |
| 5269 | |
| 5270 | switch (*flag) { |
| 5271 | case 'n' : cfg_maxconn = atol(*argv); break; |
| 5272 | case 'N' : cfg_maxpconn = atol(*argv); break; |
| 5273 | case 'f' : cfg_cfgfile = *argv; break; |
| 5274 | default: usage(old_argv); |
| 5275 | } |
| 5276 | } |
| 5277 | } |
| 5278 | else |
| 5279 | usage(old_argv); |
| 5280 | argv++; argc--; |
| 5281 | } |
| 5282 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5283 | if (!cfg_cfgfile) |
| 5284 | usage(old_argv); |
| 5285 | |
| 5286 | gethostname(hostname, MAX_HOSTNAME_LEN); |
| 5287 | |
| 5288 | if (readcfgfile(cfg_cfgfile) < 0) { |
| 5289 | Alert("Error reading configuration file : %s\n", cfg_cfgfile); |
| 5290 | exit(1); |
| 5291 | } |
| 5292 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5293 | if (cfg_maxconn > 0) |
| 5294 | global.maxconn = cfg_maxconn; |
| 5295 | |
| 5296 | if (global.maxconn == 0) |
| 5297 | global.maxconn = DEFAULT_MAXCONN; |
| 5298 | |
| 5299 | global.maxsock = global.maxconn * 2; /* each connection needs two sockets */ |
| 5300 | |
| 5301 | if (arg_mode & MODE_DEBUG) { |
| 5302 | /* command line debug mode inhibits configuration mode */ |
| 5303 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 5304 | } |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5305 | 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] | 5306 | |
| 5307 | if ((global.mode & MODE_DEBUG) && (global.mode & (MODE_DAEMON | MODE_QUIET))) { |
| 5308 | Warning("<debug> mode incompatible with <quiet> and <daemon>. Keeping <debug> only.\n"); |
| 5309 | global.mode &= ~(MODE_DAEMON | MODE_QUIET); |
| 5310 | } |
| 5311 | |
| 5312 | if ((global.nbproc > 1) && !(global.mode & MODE_DAEMON)) { |
| 5313 | Warning("<nbproc> is only meaningful in daemon mode. Setting limit to 1 process.\n"); |
| 5314 | global.nbproc = 1; |
| 5315 | } |
| 5316 | |
| 5317 | if (global.nbproc < 1) |
| 5318 | global.nbproc = 1; |
| 5319 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5320 | ReadEvent = (fd_set *)calloc(1, |
| 5321 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5322 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5323 | WriteEvent = (fd_set *)calloc(1, |
| 5324 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5325 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5326 | StaticReadEvent = (fd_set *)calloc(1, |
| 5327 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5328 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5329 | StaticWriteEvent = (fd_set *)calloc(1, |
| 5330 | sizeof(fd_set) * |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5331 | (global.maxsock + FD_SETSIZE - 1) / FD_SETSIZE); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5332 | |
| 5333 | fdtab = (struct fdtab *)calloc(1, |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5334 | sizeof(struct fdtab) * (global.maxsock)); |
| 5335 | for (i = 0; i < global.maxsock; i++) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5336 | fdtab[i].state = FD_STCLOSE; |
| 5337 | } |
| 5338 | } |
| 5339 | |
| 5340 | /* |
| 5341 | * this function starts all the proxies. It returns 0 if OK, -1 if not. |
| 5342 | */ |
| 5343 | int start_proxies() { |
| 5344 | struct proxy *curproxy; |
| 5345 | int one = 1; |
| 5346 | int fd; |
| 5347 | |
| 5348 | for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) { |
| 5349 | |
| 5350 | if (curproxy->state == PR_STDISABLED) |
| 5351 | continue; |
| 5352 | |
| 5353 | if ((fd = curproxy->listen_fd = |
| 5354 | socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
| 5355 | Alert("cannot create listening socket for proxy %s. Aborting.\n", |
| 5356 | curproxy->id); |
| 5357 | return -1; |
| 5358 | } |
| 5359 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5360 | if (fd >= global.maxsock) { |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5361 | Alert("socket(): not enough free sockets for proxy %s. Raise -n argument. Aborting.\n", |
| 5362 | curproxy->id); |
| 5363 | close(fd); |
| 5364 | return -1; |
| 5365 | } |
| 5366 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5367 | if ((fcntl(fd, F_SETFL, O_NONBLOCK) == -1) || |
| 5368 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, |
| 5369 | (char *) &one, sizeof(one)) == -1)) { |
| 5370 | Alert("cannot make socket non-blocking for proxy %s. Aborting.\n", |
| 5371 | curproxy->id); |
| 5372 | close(fd); |
| 5373 | return -1; |
| 5374 | } |
| 5375 | |
| 5376 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) { |
| 5377 | Alert("cannot do so_reuseaddr for proxy %s. Continuing.\n", |
| 5378 | curproxy->id); |
| 5379 | } |
| 5380 | |
| 5381 | if (bind(fd, |
| 5382 | (struct sockaddr *)&curproxy->listen_addr, |
| 5383 | sizeof(curproxy->listen_addr)) == -1) { |
| 5384 | Alert("cannot bind socket for proxy %s. Aborting.\n", |
| 5385 | curproxy->id); |
| 5386 | close(fd); |
| 5387 | return -1; |
| 5388 | } |
| 5389 | |
| 5390 | if (listen(fd, curproxy->maxconn) == -1) { |
| 5391 | Alert("cannot listen to socket for proxy %s. Aborting.\n", |
| 5392 | curproxy->id); |
| 5393 | close(fd); |
| 5394 | return -1; |
| 5395 | } |
| 5396 | |
| 5397 | /* the function for the accept() event */ |
| 5398 | fdtab[fd].read = &event_accept; |
| 5399 | fdtab[fd].write = NULL; /* never called */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5400 | 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] | 5401 | curproxy->state = PR_STRUN; |
| 5402 | fdtab[fd].state = FD_STLISTEN; |
| 5403 | FD_SET(fd, StaticReadEvent); |
| 5404 | fd_insert(fd); |
| 5405 | listeners++; |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5406 | |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 5407 | send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id); |
willy tarreau | 535ae7a | 2005-12-17 12:58:00 +0100 | [diff] [blame] | 5408 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5409 | } |
| 5410 | return 0; |
| 5411 | } |
| 5412 | |
| 5413 | |
| 5414 | int main(int argc, char **argv) { |
| 5415 | init(argc, argv); |
| 5416 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5417 | if (global.mode & MODE_QUIET) { |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5418 | /* detach from the tty */ |
willy tarreau | 5cbea6f | 2005-12-17 12:48:26 +0100 | [diff] [blame] | 5419 | fclose(stdin); fclose(stdout); fclose(stderr); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5420 | close(0); close(1); close(2); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5421 | } |
| 5422 | |
| 5423 | signal(SIGQUIT, dump); |
| 5424 | signal(SIGUSR1, sig_soft_stop); |
willy tarreau | 8337c6b | 2005-12-17 13:41:01 +0100 | [diff] [blame] | 5425 | signal(SIGHUP, sig_dump_state); |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5426 | |
| 5427 | /* on very high loads, a sigpipe sometimes happen just between the |
| 5428 | * getsockopt() which tells "it's OK to write", and the following write :-( |
| 5429 | */ |
willy tarreau | 3242e86 | 2005-12-17 12:27:53 +0100 | [diff] [blame] | 5430 | #ifndef MSG_NOSIGNAL |
| 5431 | signal(SIGPIPE, SIG_IGN); |
| 5432 | #endif |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5433 | |
| 5434 | if (start_proxies() < 0) |
| 5435 | exit(1); |
| 5436 | |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5437 | /* open log files */ |
| 5438 | |
| 5439 | /* chroot if needed */ |
| 5440 | if (global.chroot != NULL) { |
| 5441 | if (chroot(global.chroot) == -1) { |
| 5442 | Alert("[%s.main()] Cannot chroot(%s).\n", argv[0], global.chroot); |
| 5443 | exit(1); |
| 5444 | } |
| 5445 | chdir("/"); |
| 5446 | } |
| 5447 | |
| 5448 | /* setgid / setuid */ |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5449 | if (global.gid && setgid(global.gid) == -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5450 | Alert("[%s.main()] Cannot set gid %d.\n", argv[0], global.gid); |
| 5451 | exit(1); |
| 5452 | } |
| 5453 | |
willy tarreau | 036e1ce | 2005-12-17 13:46:33 +0100 | [diff] [blame] | 5454 | if (global.uid && setuid(global.uid) == -1) { |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5455 | Alert("[%s.main()] Cannot set uid %d.\n", argv[0], global.uid); |
| 5456 | exit(1); |
| 5457 | } |
| 5458 | |
| 5459 | if (global.mode & MODE_DAEMON) { |
| 5460 | int ret = 0; |
| 5461 | int proc; |
| 5462 | |
| 5463 | /* the father launches the required number of processes */ |
| 5464 | for (proc = 0; proc < global.nbproc; proc++) { |
| 5465 | ret = fork(); |
| 5466 | if (ret < 0) { |
| 5467 | Alert("[%s.main()] Cannot fork.\n", argv[0]); |
| 5468 | exit(1); /* there has been an error */ |
| 5469 | } |
| 5470 | else if (ret == 0) /* child breaks here */ |
| 5471 | break; |
| 5472 | } |
| 5473 | if (proc == global.nbproc) |
| 5474 | exit(0); /* parent must leave */ |
| 5475 | |
willy tarreau | 750a472 | 2005-12-17 13:21:24 +0100 | [diff] [blame] | 5476 | /* if we're NOT in QUIET mode, we should now close the 3 first FDs to ensure |
| 5477 | * that we can detach from the TTY. We MUST NOT do it in other cases since |
| 5478 | * it would have already be done, and 0-2 would have been affected to listening |
| 5479 | * sockets |
| 5480 | */ |
| 5481 | if (!(global.mode & MODE_QUIET)) { |
| 5482 | /* detach from the tty */ |
| 5483 | fclose(stdin); fclose(stdout); fclose(stderr); |
| 5484 | close(0); close(1); close(2); /* close all fd's */ |
| 5485 | global.mode |= MODE_QUIET; /* ensure that we won't say anything from now */ |
| 5486 | } |
willy tarreau | a159808 | 2005-12-17 13:08:06 +0100 | [diff] [blame] | 5487 | pid = getpid(); /* update child's pid */ |
willy tarreau | e867b48 | 2005-12-17 13:28:43 +0100 | [diff] [blame] | 5488 | setsid(); |
willy tarreau | 9fe663a | 2005-12-17 13:02:59 +0100 | [diff] [blame] | 5489 | } |
| 5490 | |
willy tarreau | 0f7af91 | 2005-12-17 12:21:26 +0100 | [diff] [blame] | 5491 | select_loop(); |
| 5492 | |
| 5493 | exit(0); |
| 5494 | } |