blob: 4fc4460f0c37fc5bd03a2b958db60d821d87ef61 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau03fa5df2010-05-24 21:02:37 +02002 * Frontend variables and functions.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreau03fa5df2010-05-24 21:02:37 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <errno.h>
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020017#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020018
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
Willy Tarreau2dd0d472006-06-29 17:53:05 +020023#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020024#include <common/config.h>
Willy Tarreau8b0cbf92010-10-15 23:23:19 +020025#include <common/debug.h>
26#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020027#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
Willy Tarreaubaaee002006-06-26 02:48:02 +020029#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreau8797c062007-05-07 00:55:35 +020031#include <proto/acl.h>
Willy Tarreau54469402006-07-29 16:59:06 +020032#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020034#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035#include <proto/log.h>
Willy Tarreaue5f20dc2006-12-03 15:21:35 +010036#include <proto/hdr_idx.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020037#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020038#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010039#include <proto/proxy.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020040#include <proto/session.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010041#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <proto/stream_sock.h>
43#include <proto/task.h>
44
45
Willy Tarreau14c8aac2007-05-08 19:46:30 +020046/* Retrieves the original destination address used by the client, and sets the
47 * SN_FRT_ADDR_SET flag.
48 */
49void get_frt_addr(struct session *s)
50{
51 socklen_t namelen = sizeof(s->frt_addr);
52
Willy Tarreau7e5067d2008-12-07 16:27:56 +010053 if (get_original_dst(s->si[0].fd, (struct sockaddr_in *)&s->frt_addr, &namelen) == -1)
54 getsockname(s->si[0].fd, (struct sockaddr *)&s->frt_addr, &namelen);
Willy Tarreau14c8aac2007-05-08 19:46:30 +020055 s->flags |= SN_FRT_ADDR_SET;
56}
Willy Tarreaubaaee002006-06-26 02:48:02 +020057
Willy Tarreau81f9aa32010-06-01 17:45:26 +020058/* Finish a session accept() for a proxy (TCP or HTTP). It returns a negative
Willy Tarreauabe8ea52010-11-11 10:56:04 +010059 * value in case of a critical failure which must cause the listener to be
60 * disabled, a positive value in case of success, or zero if it is a success
61 * but the session must be closed ASAP (eg: monitoring).
Willy Tarreaubaaee002006-06-26 02:48:02 +020062 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020063int frontend_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +020064{
Willy Tarreau81f9aa32010-06-01 17:45:26 +020065 int cfd = s->si[0].fd;
Emeric Brunb982a3d2010-01-04 15:45:53 +010066
Willy Tarreaueb472682010-05-28 18:46:57 +020067 tv_zero(&s->logs.tv_request);
68 s->logs.t_queue = -1;
69 s->logs.t_connect = -1;
70 s->logs.t_data = -1;
71 s->logs.t_close = 0;
72 s->logs.bytes_in = s->logs.bytes_out = 0;
73 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
74 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaubaaee002006-06-26 02:48:02 +020075
Willy Tarreau35a09942010-06-01 17:12:40 +020076 s->data_state = DATA_ST_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +020077 s->data_source = DATA_SRC_NONE;
Willy Tarreaua5555ec2008-11-30 19:02:32 +010078
Willy Tarreau35a09942010-06-01 17:12:40 +020079 /* FIXME: the logs are horribly complicated now, because they are
80 * defined in <p>, <p>, and later <be> and <be>.
Willy Tarreaueb472682010-05-28 18:46:57 +020081 */
Willy Tarreau35a09942010-06-01 17:12:40 +020082 if (s->logs.logwait & LW_REQ)
83 s->do_log = http_sess_log;
84 else
85 s->do_log = tcp_sess_log;
86
87 /* default error reporting function, may be changed by analysers */
88 s->srv_error = default_srv_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +020089
Willy Tarreauf67c9782010-05-23 22:59:00 +020090 /* Adjust some socket options */
Willy Tarreau9c3bc222010-12-24 14:49:37 +010091 if (s->listener->addr.ss_family == AF_INET || s->listener->addr.ss_family == AF_INET6) {
92 if (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY,
93 (char *) &one, sizeof(one)) == -1)
94 goto out_return;
Willy Tarreauf67c9782010-05-23 22:59:00 +020095
Willy Tarreau9c3bc222010-12-24 14:49:37 +010096 if (s->fe->options & PR_O_TCP_CLI_KA)
97 setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE,
98 (char *) &one, sizeof(one));
Willy Tarreauf67c9782010-05-23 22:59:00 +020099
Willy Tarreau9c3bc222010-12-24 14:49:37 +0100100 if (s->fe->options & PR_O_TCP_NOLING)
101 setsockopt(cfd, SOL_SOCKET, SO_LINGER,
102 (struct linger *) &nolinger, sizeof(struct linger));
103 }
Willy Tarreauf67c9782010-05-23 22:59:00 +0200104
105 if (global.tune.client_sndbuf)
106 setsockopt(cfd, SOL_SOCKET, SO_SNDBUF, &global.tune.client_sndbuf, sizeof(global.tune.client_sndbuf));
107
108 if (global.tune.client_rcvbuf)
109 setsockopt(cfd, SOL_SOCKET, SO_RCVBUF, &global.tune.client_rcvbuf, sizeof(global.tune.client_rcvbuf));
110
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200111 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200112 /* the captures are only used in HTTP frontends */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200113 if (unlikely(s->fe->nb_req_cap > 0 &&
114 (s->txn.req.cap = pool_alloc2(s->fe->req_cap_pool)) == NULL))
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100115 goto out_return; /* no memory */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200117 if (unlikely(s->fe->nb_rsp_cap > 0 &&
118 (s->txn.rsp.cap = pool_alloc2(s->fe->rsp_cap_pool)) == NULL))
Willy Tarreau35a09942010-06-01 17:12:40 +0200119 goto out_free_reqcap; /* no memory */
Willy Tarreaueb472682010-05-28 18:46:57 +0200120 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200121
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200122 if (s->fe->acl_requires & ACL_USE_L7_ANY) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200123 /* we have to allocate header indexes only if we know
124 * that we may make use of them. This of course includes
125 * (mode == PR_MODE_HTTP).
Willy Tarreau042cc792007-03-19 16:20:06 +0100126 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200127 s->txn.hdr_idx.size = MAX_HTTP_HDR;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100128
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200129 if (unlikely((s->txn.hdr_idx.v = pool_alloc2(s->fe->hdr_idx_pool)) == NULL))
Willy Tarreau35a09942010-06-01 17:12:40 +0200130 goto out_free_rspcap; /* no memory */
Willy Tarreau45e73e32006-12-17 00:05:15 +0100131
Willy Tarreaueb472682010-05-28 18:46:57 +0200132 /* and now initialize the HTTP transaction state */
133 http_init_txn(s);
134 }
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100135
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200136 if ((s->fe->mode == PR_MODE_TCP || s->fe->mode == PR_MODE_HTTP)
137 && (s->fe->logfac1 >= 0 || s->fe->logfac2 >= 0)) {
138 if (likely(s->fe->to_log)) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200139 /* we have the client ip */
140 if (s->logs.logwait & LW_CLIP)
141 if (!(s->logs.logwait &= ~LW_CLIP))
142 s->do_log(s);
Willy Tarreaua3445fc2010-05-20 16:17:07 +0200143 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200144 else if (s->cli_addr.ss_family == AF_INET) {
145 char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200146
Willy Tarreaueb472682010-05-28 18:46:57 +0200147 if (!(s->flags & SN_FRT_ADDR_SET))
148 get_frt_addr(s);
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200149
Willy Tarreaueb472682010-05-28 18:46:57 +0200150 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr,
151 sn, sizeof(sn)) &&
152 inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
153 pn, sizeof(pn))) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200154 send_log(s->fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
Willy Tarreaueb472682010-05-28 18:46:57 +0200155 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port),
156 sn, ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port),
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200157 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200158 }
159 }
Emeric Brunab844ea2010-10-22 16:33:18 +0200160 else if (s->cli_addr.ss_family == AF_INET6) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200161 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200162
163 if (!(s->flags & SN_FRT_ADDR_SET))
164 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200165
Willy Tarreaueb472682010-05-28 18:46:57 +0200166 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->frt_addr)->sin6_addr,
167 sn, sizeof(sn)) &&
168 inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr,
169 pn, sizeof(pn))) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200170 send_log(s->fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
Willy Tarreaueb472682010-05-28 18:46:57 +0200171 pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
172 sn, ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port),
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200173 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200174 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200175 }
Emeric Brunab844ea2010-10-22 16:33:18 +0200176 else {
177 /* UNIX socket, only the destination is known */
178 send_log(s->fe, LOG_INFO, "Connect to unix:%d (%s/%s)\n",
179 s->listener->luid,
180 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
181 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200182 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200183
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200184 if (unlikely((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200185 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200186
Willy Tarreaueb472682010-05-28 18:46:57 +0200187 if (!(s->flags & SN_FRT_ADDR_SET))
188 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189
Willy Tarreaueb472682010-05-28 18:46:57 +0200190 if (s->cli_addr.ss_family == AF_INET) {
191 char pn[INET_ADDRSTRLEN];
192 inet_ntop(AF_INET,
193 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
194 pn, sizeof(pn));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200195
Willy Tarreaueb472682010-05-28 18:46:57 +0200196 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200197 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
Willy Tarreaueb472682010-05-28 18:46:57 +0200198 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port));
199 }
Emeric Brunab844ea2010-10-22 16:33:18 +0200200 else if (s->cli_addr.ss_family == AF_INET6) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200201 char pn[INET6_ADDRSTRLEN];
202 inet_ntop(AF_INET6,
203 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
204 pn, sizeof(pn));
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200205
Willy Tarreaueb472682010-05-28 18:46:57 +0200206 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200207 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
Willy Tarreaueb472682010-05-28 18:46:57 +0200208 pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port));
209 }
Emeric Brunab844ea2010-10-22 16:33:18 +0200210 else {
211 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [unix:%d]\n",
212 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
213 s->listener->luid);
214 }
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200215
Willy Tarreaueb472682010-05-28 18:46:57 +0200216 write(1, trash, len);
217 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200218
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200219 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreaueb472682010-05-28 18:46:57 +0200220 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200221
Willy Tarreaueb472682010-05-28 18:46:57 +0200222 /* note: this should not happen anymore since there's always at least the switching rules */
223 if (!s->req->analysers) {
224 buffer_auto_connect(s->req); /* don't wait to establish connection */
225 buffer_auto_close(s->req); /* let the producer forward close requests */
226 }
Willy Tarreaud7971282006-07-29 18:36:34 +0200227
Willy Tarreaueb472682010-05-28 18:46:57 +0200228 s->req->rto = s->fe->timeout.client;
Willy Tarreaueb472682010-05-28 18:46:57 +0200229 s->rep->wto = s->fe->timeout.client;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200230
Willy Tarreaueb472682010-05-28 18:46:57 +0200231 fdtab[cfd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200232 if (s->fe->options & PR_O_TCP_NOLING)
Willy Tarreaueb472682010-05-28 18:46:57 +0200233 fdtab[cfd].flags |= FD_FL_TCP_NOLING;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +0200234
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200235 if (unlikely((s->fe->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||
236 (s->fe->mode == PR_MODE_HEALTH && (s->fe->options & PR_O_HTTP_CHK)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200237 /* Either we got a request from a monitoring system on an HTTP instance,
238 * or we're in health check mode with the 'httpchk' option enabled. In
239 * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
240 */
241 struct chunk msg;
242 chunk_initstr(&msg, "HTTP/1.0 200 OK\r\n\r\n");
243 stream_int_retnclose(&s->si[0], &msg); /* forge a 200 response */
244 s->req->analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200245 s->task->expire = s->rep->wex;
Willy Tarreau35a09942010-06-01 17:12:40 +0200246 EV_FD_CLR(cfd, DIR_RD);
Willy Tarreaueb472682010-05-28 18:46:57 +0200247 }
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200248 else if (unlikely(s->fe->mode == PR_MODE_HEALTH)) { /* health check mode, no client reading */
Willy Tarreaueb472682010-05-28 18:46:57 +0200249 struct chunk msg;
250 chunk_initstr(&msg, "OK\n");
251 stream_int_retnclose(&s->si[0], &msg); /* forge an "OK" response */
252 s->req->analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200253 s->task->expire = s->rep->wex;
Willy Tarreau35a09942010-06-01 17:12:40 +0200254 EV_FD_CLR(cfd, DIR_RD);
Willy Tarreaueb472682010-05-28 18:46:57 +0200255 }
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200256 /* everything's OK, let's go on */
Willy Tarreaueb472682010-05-28 18:46:57 +0200257 return 1;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100258
259 /* Error unrolling */
Willy Tarreau35a09942010-06-01 17:12:40 +0200260 out_free_rspcap:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200261 pool_free2(s->fe->rsp_cap_pool, s->txn.rsp.cap);
Willy Tarreau35a09942010-06-01 17:12:40 +0200262 out_free_reqcap:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200263 pool_free2(s->fe->req_cap_pool, s->txn.req.cap);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100264 out_return:
Willy Tarreaueb472682010-05-28 18:46:57 +0200265 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200266}
267
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200268/* This analyser tries to fetch a line from the request buffer which looks like :
269 *
270 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
271 *
272 * There must be exactly one space between each field. Fields are :
Emeric Brun861ccff2010-10-29 12:03:03 +0200273 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200274 * - SRC3 : layer 3 (eg: IP) source address in standard text form
275 * - DST3 : layer 3 (eg: IP) destination address in standard text form
276 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
277 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
278 *
279 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
280 *
281 * Once the data is fetched, the values are set in the session's field and data
282 * are removed from the buffer. The function returns zero if it needs to wait
283 * for more data (max: timeout_client), or 1 if it has finished and removed itself.
284 */
285int frontend_decode_proxy_request(struct session *s, struct buffer *req, int an_bit)
286{
287 char *line = req->data;
288 char *end = req->data + req->l;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200289 int len;
290
291 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
292 now_ms, __FUNCTION__,
293 s,
294 req,
295 req->rex, req->wex,
296 req->flags,
297 req->l,
298 req->analysers);
299
300 if (req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT))
301 goto fail;
302
Emeric Brunf4711a32010-10-29 15:16:55 +0200303 len = MIN(req->l, 6);
304 if (!len)
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200305 goto missing;
306
Emeric Brunf4711a32010-10-29 15:16:55 +0200307 /* Decode a possible proxy request, fail early if it does not match */
308 if (strncmp(line, "PROXY ", len) != 0)
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200309 goto fail;
Emeric Brunf4711a32010-10-29 15:16:55 +0200310
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200311 line += 6;
Emeric Brunf4711a32010-10-29 15:16:55 +0200312 if (req->l < 18) /* shortest possible line */
313 goto missing;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200314
Emeric Brun861ccff2010-10-29 12:03:03 +0200315 if (!memcmp(line, "TCP4 ", 5) != 0) {
316 u32 src3, dst3, sport, dport;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200317
Emeric Brun861ccff2010-10-29 12:03:03 +0200318 line += 5;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200319
Emeric Brun861ccff2010-10-29 12:03:03 +0200320 src3 = inetaddr_host_lim_ret(line, end, &line);
321 if (line == end)
322 goto missing;
323 if (*line++ != ' ')
324 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200325
Emeric Brun861ccff2010-10-29 12:03:03 +0200326 dst3 = inetaddr_host_lim_ret(line, end, &line);
327 if (line == end)
328 goto missing;
329 if (*line++ != ' ')
330 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200331
Emeric Brun861ccff2010-10-29 12:03:03 +0200332 sport = read_uint((const char **)&line, end);
333 if (line == end)
334 goto missing;
335 if (*line++ != ' ')
336 goto fail;
337
338 dport = read_uint((const char **)&line, end);
339 if (line > end - 2)
340 goto missing;
341 if (*line++ != '\r')
342 goto fail;
343 if (*line++ != '\n')
344 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200345
Emeric Brun861ccff2010-10-29 12:03:03 +0200346 /* update the session's addresses and mark them set */
347 ((struct sockaddr_in *)&s->cli_addr)->sin_family = AF_INET;
348 ((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr = htonl(src3);
349 ((struct sockaddr_in *)&s->cli_addr)->sin_port = htons(sport);
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200350
Emeric Brun861ccff2010-10-29 12:03:03 +0200351 ((struct sockaddr_in *)&s->frt_addr)->sin_family = AF_INET;
352 ((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr = htonl(dst3);
353 ((struct sockaddr_in *)&s->frt_addr)->sin_port = htons(dport);
354 s->flags |= SN_FRT_ADDR_SET;
355
356 }
357 else if (!memcmp(line, "TCP6 ", 5) != 0) {
358 u32 sport, dport;
359 char *src_s;
360 char *dst_s, *sport_s, *dport_s;
361 struct in6_addr src3, dst3;
362
363 line+=5;
364
365 src_s = line;
366 dst_s = sport_s = dport_s = NULL;
367 while (1) {
368 if (line > end - 2) {
369 goto missing;
370 }
371 else if (*line == '\r') {
372 *line = 0;
373 line++;
374 if (*line++ != '\n')
375 goto fail;
376 break;
377 }
378
379 if (*line == ' ') {
380 *line = 0;
381 if (!dst_s)
382 dst_s = line+1;
383 else if (!sport_s)
384 sport_s = line+1;
385 else if (!dport_s)
386 dport_s = line+1;
387 }
388 line++;
389 }
390
391 if (!dst_s || !sport_s || !dport_s)
392 goto fail;
393
394 sport = read_uint((const char **)&sport_s,dport_s-1);
395 if ( *sport_s != 0 )
396 goto fail;
397
398 dport = read_uint((const char **)&dport_s,line-2);
399 if ( *dport_s != 0 )
400 goto fail;
401
402 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
403 goto fail;
404
405 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
406 goto fail;
407
408 /* update the session's addresses and mark them set */
409 ((struct sockaddr_in6 *)&s->cli_addr)->sin6_family = AF_INET6;
410 memcpy(&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr, &src3, sizeof(struct in6_addr));
411 ((struct sockaddr_in6 *)&s->cli_addr)->sin6_port = htons(sport);
412
413 ((struct sockaddr_in6 *)&s->frt_addr)->sin6_family = AF_INET6;
414 memcpy(&((struct sockaddr_in6 *)&s->frt_addr)->sin6_addr, &dst3, sizeof(struct in6_addr));
415 ((struct sockaddr_in6 *)&s->frt_addr)->sin6_port = htons(dport);
416 s->flags |= SN_FRT_ADDR_SET;
417 }
418 else {
419 goto fail;
420 }
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200421
422 /* remove the PROXY line from the request */
423 len = line - req->data;
424 buffer_replace2(req, req->data, line, NULL, 0);
425 req->total -= len; /* don't count the header line */
426
427 req->analysers &= ~an_bit;
428 return 1;
429
430 missing:
431 if (!(req->flags & (BF_SHUTR|BF_FULL))) {
432 buffer_dont_connect(s->req);
433 return 0;
434 }
435 /* missing data and buffer is either full or shutdown => fail */
436
437 fail:
438 buffer_abort(req);
439 buffer_abort(s->rep);
440 req->analysers = 0;
441
442 s->fe->counters.failed_req++;
443 if (s->listener->counters)
444 s->listener->counters->failed_req++;
445
446 if (!(s->flags & SN_ERR_MASK))
447 s->flags |= SN_ERR_PRXCOND;
448 if (!(s->flags & SN_FINST_MASK))
449 s->flags |= SN_FINST_R;
450 return 0;
451}
452
Willy Tarreau645513a2010-05-24 20:55:15 +0200453/* set test->i to the id of the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200454static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200455acl_fetch_fe_id(struct proxy *px, struct session *l4, void *l7, int dir,
456 struct acl_expr *expr, struct acl_test *test) {
Willy Tarreau662b2d82007-05-08 19:56:15 +0200457
Willy Tarreau662b2d82007-05-08 19:56:15 +0200458 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200459
Willy Tarreau645513a2010-05-24 20:55:15 +0200460 test->i = l4->fe->uuid;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200461
Emeric Brun5d16eda2010-01-04 15:47:45 +0100462 return 1;
463}
464
Willy Tarreau645513a2010-05-24 20:55:15 +0200465/* set test->i to the number of connections per second reaching the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200466static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200467acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
468 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200469{
Willy Tarreau645513a2010-05-24 20:55:15 +0200470 test->flags = ACL_TEST_F_VOL_TEST;
471 if (expr->arg_len) {
472 /* another proxy was designated, we must look for it */
473 for (px = proxy; px; px = px->next)
474 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
475 break;
476 }
477 if (!px)
478 return 0;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100479
Willy Tarreau645513a2010-05-24 20:55:15 +0200480 test->i = read_freq_ctr(&px->fe_sess_per_sec);
Emeric Brun5d16eda2010-01-04 15:47:45 +0100481 return 1;
482}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100483
Willy Tarreau645513a2010-05-24 20:55:15 +0200484/* set test->i to the number of concurrent connections on the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200485static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200486acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
487 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200488{
Willy Tarreau645513a2010-05-24 20:55:15 +0200489 test->flags = ACL_TEST_F_VOL_TEST;
490 if (expr->arg_len) {
491 /* another proxy was designated, we must look for it */
492 for (px = proxy; px; px = px->next)
493 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
494 break;
495 }
496 if (!px)
497 return 0;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100498
Willy Tarreau645513a2010-05-24 20:55:15 +0200499 test->i = px->feconn;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100500 return 1;
501}
502
Willy Tarreau8797c062007-05-07 00:55:35 +0200503
504/* Note: must not be declared <const> as its list will be overwritten */
505static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +0200506 { "fe_id", acl_parse_int, acl_fetch_fe_id, acl_match_int, ACL_USE_NOTHING },
507 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
508 { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8797c062007-05-07 00:55:35 +0200509 { NULL, NULL, NULL, NULL },
510}};
511
512
513__attribute__((constructor))
Willy Tarreau03fa5df2010-05-24 21:02:37 +0200514static void __frontend_init(void)
Willy Tarreau8797c062007-05-07 00:55:35 +0200515{
516 acl_register_keywords(&acl_kws);
517}
518
519
Willy Tarreaubaaee002006-06-26 02:48:02 +0200520/*
521 * Local variables:
522 * c-indent-level: 8
523 * c-basic-offset: 8
524 * End:
525 */