blob: eea1ce043046858b28bb54a4cdcf33ec87e2f6f7 [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
59 * value in case of failure, a positive value in case of success, or zero if
60 * it is a success but the session must be closed ASAP.
Willy Tarreaubaaee002006-06-26 02:48:02 +020061 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020062int frontend_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +020063{
Willy Tarreau81f9aa32010-06-01 17:45:26 +020064 int cfd = s->si[0].fd;
Emeric Brunb982a3d2010-01-04 15:45:53 +010065
Willy Tarreaueb472682010-05-28 18:46:57 +020066 tv_zero(&s->logs.tv_request);
67 s->logs.t_queue = -1;
68 s->logs.t_connect = -1;
69 s->logs.t_data = -1;
70 s->logs.t_close = 0;
71 s->logs.bytes_in = s->logs.bytes_out = 0;
72 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
73 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaubaaee002006-06-26 02:48:02 +020074
Willy Tarreau35a09942010-06-01 17:12:40 +020075 s->data_state = DATA_ST_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +020076 s->data_source = DATA_SRC_NONE;
Willy Tarreaua5555ec2008-11-30 19:02:32 +010077
Willy Tarreau35a09942010-06-01 17:12:40 +020078 /* FIXME: the logs are horribly complicated now, because they are
79 * defined in <p>, <p>, and later <be> and <be>.
Willy Tarreaueb472682010-05-28 18:46:57 +020080 */
Willy Tarreau35a09942010-06-01 17:12:40 +020081 if (s->logs.logwait & LW_REQ)
82 s->do_log = http_sess_log;
83 else
84 s->do_log = tcp_sess_log;
85
86 /* default error reporting function, may be changed by analysers */
87 s->srv_error = default_srv_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +020088
Willy Tarreauf67c9782010-05-23 22:59:00 +020089 /* Adjust some socket options */
Willy Tarreau35a09942010-06-01 17:12:40 +020090 if (unlikely(setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) {
Willy Tarreauf67c9782010-05-23 22:59:00 +020091 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
Willy Tarreau35a09942010-06-01 17:12:40 +020092 goto out_delete_cfd;
Willy Tarreauf67c9782010-05-23 22:59:00 +020093 }
94
Willy Tarreau81f9aa32010-06-01 17:45:26 +020095 if (s->fe->options & PR_O_TCP_CLI_KA)
Willy Tarreauf67c9782010-05-23 22:59:00 +020096 setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
97
Willy Tarreau81f9aa32010-06-01 17:45:26 +020098 if (s->fe->options & PR_O_TCP_NOLING)
Willy Tarreauf67c9782010-05-23 22:59:00 +020099 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
100
101 if (global.tune.client_sndbuf)
102 setsockopt(cfd, SOL_SOCKET, SO_SNDBUF, &global.tune.client_sndbuf, sizeof(global.tune.client_sndbuf));
103
104 if (global.tune.client_rcvbuf)
105 setsockopt(cfd, SOL_SOCKET, SO_RCVBUF, &global.tune.client_rcvbuf, sizeof(global.tune.client_rcvbuf));
106
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200107 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200108 /* the captures are only used in HTTP frontends */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200109 if (unlikely(s->fe->nb_req_cap > 0 &&
110 (s->txn.req.cap = pool_alloc2(s->fe->req_cap_pool)) == NULL))
Willy Tarreau35a09942010-06-01 17:12:40 +0200111 goto out_delete_cfd; /* no memory */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200113 if (unlikely(s->fe->nb_rsp_cap > 0 &&
114 (s->txn.rsp.cap = pool_alloc2(s->fe->rsp_cap_pool)) == NULL))
Willy Tarreau35a09942010-06-01 17:12:40 +0200115 goto out_free_reqcap; /* no memory */
Willy Tarreaueb472682010-05-28 18:46:57 +0200116 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200118 if (s->fe->acl_requires & ACL_USE_L7_ANY) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200119 /* we have to allocate header indexes only if we know
120 * that we may make use of them. This of course includes
121 * (mode == PR_MODE_HTTP).
Willy Tarreau042cc792007-03-19 16:20:06 +0100122 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200123 s->txn.hdr_idx.size = MAX_HTTP_HDR;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100124
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200125 if (unlikely((s->txn.hdr_idx.v = pool_alloc2(s->fe->hdr_idx_pool)) == NULL))
Willy Tarreau35a09942010-06-01 17:12:40 +0200126 goto out_free_rspcap; /* no memory */
Willy Tarreau45e73e32006-12-17 00:05:15 +0100127
Willy Tarreaueb472682010-05-28 18:46:57 +0200128 /* and now initialize the HTTP transaction state */
129 http_init_txn(s);
130 }
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100131
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200132 if ((s->fe->mode == PR_MODE_TCP || s->fe->mode == PR_MODE_HTTP)
133 && (s->fe->logfac1 >= 0 || s->fe->logfac2 >= 0)) {
134 if (likely(s->fe->to_log)) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200135 /* we have the client ip */
136 if (s->logs.logwait & LW_CLIP)
137 if (!(s->logs.logwait &= ~LW_CLIP))
138 s->do_log(s);
Willy Tarreaua3445fc2010-05-20 16:17:07 +0200139 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200140 else if (s->cli_addr.ss_family == AF_INET) {
141 char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200142
Willy Tarreaueb472682010-05-28 18:46:57 +0200143 if (!(s->flags & SN_FRT_ADDR_SET))
144 get_frt_addr(s);
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200145
Willy Tarreaueb472682010-05-28 18:46:57 +0200146 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr,
147 sn, sizeof(sn)) &&
148 inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
149 pn, sizeof(pn))) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200150 send_log(s->fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
Willy Tarreaueb472682010-05-28 18:46:57 +0200151 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port),
152 sn, ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port),
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200153 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200154 }
155 }
Emeric Brunab844ea2010-10-22 16:33:18 +0200156 else if (s->cli_addr.ss_family == AF_INET6) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200157 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200158
159 if (!(s->flags & SN_FRT_ADDR_SET))
160 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200161
Willy Tarreaueb472682010-05-28 18:46:57 +0200162 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->frt_addr)->sin6_addr,
163 sn, sizeof(sn)) &&
164 inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr,
165 pn, sizeof(pn))) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200166 send_log(s->fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
Willy Tarreaueb472682010-05-28 18:46:57 +0200167 pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
168 sn, ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port),
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200169 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200171 }
Emeric Brunab844ea2010-10-22 16:33:18 +0200172 else {
173 /* UNIX socket, only the destination is known */
174 send_log(s->fe, LOG_INFO, "Connect to unix:%d (%s/%s)\n",
175 s->listener->luid,
176 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
177 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200178 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200179
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200180 if (unlikely((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200181 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200182
Willy Tarreaueb472682010-05-28 18:46:57 +0200183 if (!(s->flags & SN_FRT_ADDR_SET))
184 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185
Willy Tarreaueb472682010-05-28 18:46:57 +0200186 if (s->cli_addr.ss_family == AF_INET) {
187 char pn[INET_ADDRSTRLEN];
188 inet_ntop(AF_INET,
189 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
190 pn, sizeof(pn));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200191
Willy Tarreaueb472682010-05-28 18:46:57 +0200192 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200193 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
Willy Tarreaueb472682010-05-28 18:46:57 +0200194 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port));
195 }
Emeric Brunab844ea2010-10-22 16:33:18 +0200196 else if (s->cli_addr.ss_family == AF_INET6) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200197 char pn[INET6_ADDRSTRLEN];
198 inet_ntop(AF_INET6,
199 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
200 pn, sizeof(pn));
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200201
Willy Tarreaueb472682010-05-28 18:46:57 +0200202 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200203 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
Willy Tarreaueb472682010-05-28 18:46:57 +0200204 pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port));
205 }
Emeric Brunab844ea2010-10-22 16:33:18 +0200206 else {
207 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [unix:%d]\n",
208 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
209 s->listener->luid);
210 }
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200211
Willy Tarreaueb472682010-05-28 18:46:57 +0200212 write(1, trash, len);
213 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200214
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200215 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreaueb472682010-05-28 18:46:57 +0200216 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200217
Willy Tarreaueb472682010-05-28 18:46:57 +0200218 /* note: this should not happen anymore since there's always at least the switching rules */
219 if (!s->req->analysers) {
220 buffer_auto_connect(s->req); /* don't wait to establish connection */
221 buffer_auto_close(s->req); /* let the producer forward close requests */
222 }
Willy Tarreaud7971282006-07-29 18:36:34 +0200223
Willy Tarreaueb472682010-05-28 18:46:57 +0200224 s->req->rto = s->fe->timeout.client;
Willy Tarreaueb472682010-05-28 18:46:57 +0200225 s->rep->wto = s->fe->timeout.client;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200226
Willy Tarreaueb472682010-05-28 18:46:57 +0200227 fdtab[cfd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200228 if (s->fe->options & PR_O_TCP_NOLING)
Willy Tarreaueb472682010-05-28 18:46:57 +0200229 fdtab[cfd].flags |= FD_FL_TCP_NOLING;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +0200230
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200231 if (unlikely((s->fe->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||
232 (s->fe->mode == PR_MODE_HEALTH && (s->fe->options & PR_O_HTTP_CHK)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200233 /* Either we got a request from a monitoring system on an HTTP instance,
234 * or we're in health check mode with the 'httpchk' option enabled. In
235 * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
236 */
237 struct chunk msg;
238 chunk_initstr(&msg, "HTTP/1.0 200 OK\r\n\r\n");
239 stream_int_retnclose(&s->si[0], &msg); /* forge a 200 response */
240 s->req->analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200241 s->task->expire = s->rep->wex;
Willy Tarreau35a09942010-06-01 17:12:40 +0200242 EV_FD_CLR(cfd, DIR_RD);
Willy Tarreaueb472682010-05-28 18:46:57 +0200243 }
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200244 else if (unlikely(s->fe->mode == PR_MODE_HEALTH)) { /* health check mode, no client reading */
Willy Tarreaueb472682010-05-28 18:46:57 +0200245 struct chunk msg;
246 chunk_initstr(&msg, "OK\n");
247 stream_int_retnclose(&s->si[0], &msg); /* forge an "OK" response */
248 s->req->analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200249 s->task->expire = s->rep->wex;
Willy Tarreau35a09942010-06-01 17:12:40 +0200250 EV_FD_CLR(cfd, DIR_RD);
Willy Tarreaueb472682010-05-28 18:46:57 +0200251 }
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200252 /* everything's OK, let's go on */
Willy Tarreaueb472682010-05-28 18:46:57 +0200253 return 1;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100254
255 /* Error unrolling */
Willy Tarreau35a09942010-06-01 17:12:40 +0200256 out_free_rspcap:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200257 pool_free2(s->fe->rsp_cap_pool, s->txn.rsp.cap);
Willy Tarreau35a09942010-06-01 17:12:40 +0200258 out_free_reqcap:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200259 pool_free2(s->fe->req_cap_pool, s->txn.req.cap);
Willy Tarreau35a09942010-06-01 17:12:40 +0200260 out_delete_cfd:
261 fd_delete(cfd);
Willy Tarreaueb472682010-05-28 18:46:57 +0200262 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200263}
264
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200265/* This analyser tries to fetch a line from the request buffer which looks like :
266 *
267 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
268 *
269 * There must be exactly one space between each field. Fields are :
Emeric Brun861ccff2010-10-29 12:03:03 +0200270 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200271 * - SRC3 : layer 3 (eg: IP) source address in standard text form
272 * - DST3 : layer 3 (eg: IP) destination address in standard text form
273 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
274 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
275 *
276 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
277 *
278 * Once the data is fetched, the values are set in the session's field and data
279 * are removed from the buffer. The function returns zero if it needs to wait
280 * for more data (max: timeout_client), or 1 if it has finished and removed itself.
281 */
282int frontend_decode_proxy_request(struct session *s, struct buffer *req, int an_bit)
283{
284 char *line = req->data;
285 char *end = req->data + req->l;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200286 int len;
287
288 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
289 now_ms, __FUNCTION__,
290 s,
291 req,
292 req->rex, req->wex,
293 req->flags,
294 req->l,
295 req->analysers);
296
297 if (req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT))
298 goto fail;
299
Emeric Brunf4711a32010-10-29 15:16:55 +0200300 len = MIN(req->l, 6);
301 if (!len)
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200302 goto missing;
303
Emeric Brunf4711a32010-10-29 15:16:55 +0200304 /* Decode a possible proxy request, fail early if it does not match */
305 if (strncmp(line, "PROXY ", len) != 0)
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200306 goto fail;
Emeric Brunf4711a32010-10-29 15:16:55 +0200307
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200308 line += 6;
Emeric Brunf4711a32010-10-29 15:16:55 +0200309 if (req->l < 18) /* shortest possible line */
310 goto missing;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200311
Emeric Brun861ccff2010-10-29 12:03:03 +0200312 if (!memcmp(line, "TCP4 ", 5) != 0) {
313 u32 src3, dst3, sport, dport;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200314
Emeric Brun861ccff2010-10-29 12:03:03 +0200315 line += 5;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200316
Emeric Brun861ccff2010-10-29 12:03:03 +0200317 src3 = inetaddr_host_lim_ret(line, end, &line);
318 if (line == end)
319 goto missing;
320 if (*line++ != ' ')
321 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200322
Emeric Brun861ccff2010-10-29 12:03:03 +0200323 dst3 = inetaddr_host_lim_ret(line, end, &line);
324 if (line == end)
325 goto missing;
326 if (*line++ != ' ')
327 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200328
Emeric Brun861ccff2010-10-29 12:03:03 +0200329 sport = read_uint((const char **)&line, end);
330 if (line == end)
331 goto missing;
332 if (*line++ != ' ')
333 goto fail;
334
335 dport = read_uint((const char **)&line, end);
336 if (line > end - 2)
337 goto missing;
338 if (*line++ != '\r')
339 goto fail;
340 if (*line++ != '\n')
341 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200342
Emeric Brun861ccff2010-10-29 12:03:03 +0200343 /* update the session's addresses and mark them set */
344 ((struct sockaddr_in *)&s->cli_addr)->sin_family = AF_INET;
345 ((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr = htonl(src3);
346 ((struct sockaddr_in *)&s->cli_addr)->sin_port = htons(sport);
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200347
Emeric Brun861ccff2010-10-29 12:03:03 +0200348 ((struct sockaddr_in *)&s->frt_addr)->sin_family = AF_INET;
349 ((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr = htonl(dst3);
350 ((struct sockaddr_in *)&s->frt_addr)->sin_port = htons(dport);
351 s->flags |= SN_FRT_ADDR_SET;
352
353 }
354 else if (!memcmp(line, "TCP6 ", 5) != 0) {
355 u32 sport, dport;
356 char *src_s;
357 char *dst_s, *sport_s, *dport_s;
358 struct in6_addr src3, dst3;
359
360 line+=5;
361
362 src_s = line;
363 dst_s = sport_s = dport_s = NULL;
364 while (1) {
365 if (line > end - 2) {
366 goto missing;
367 }
368 else if (*line == '\r') {
369 *line = 0;
370 line++;
371 if (*line++ != '\n')
372 goto fail;
373 break;
374 }
375
376 if (*line == ' ') {
377 *line = 0;
378 if (!dst_s)
379 dst_s = line+1;
380 else if (!sport_s)
381 sport_s = line+1;
382 else if (!dport_s)
383 dport_s = line+1;
384 }
385 line++;
386 }
387
388 if (!dst_s || !sport_s || !dport_s)
389 goto fail;
390
391 sport = read_uint((const char **)&sport_s,dport_s-1);
392 if ( *sport_s != 0 )
393 goto fail;
394
395 dport = read_uint((const char **)&dport_s,line-2);
396 if ( *dport_s != 0 )
397 goto fail;
398
399 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
400 goto fail;
401
402 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
403 goto fail;
404
405 /* update the session's addresses and mark them set */
406 ((struct sockaddr_in6 *)&s->cli_addr)->sin6_family = AF_INET6;
407 memcpy(&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr, &src3, sizeof(struct in6_addr));
408 ((struct sockaddr_in6 *)&s->cli_addr)->sin6_port = htons(sport);
409
410 ((struct sockaddr_in6 *)&s->frt_addr)->sin6_family = AF_INET6;
411 memcpy(&((struct sockaddr_in6 *)&s->frt_addr)->sin6_addr, &dst3, sizeof(struct in6_addr));
412 ((struct sockaddr_in6 *)&s->frt_addr)->sin6_port = htons(dport);
413 s->flags |= SN_FRT_ADDR_SET;
414 }
415 else {
416 goto fail;
417 }
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200418
419 /* remove the PROXY line from the request */
420 len = line - req->data;
421 buffer_replace2(req, req->data, line, NULL, 0);
422 req->total -= len; /* don't count the header line */
423
424 req->analysers &= ~an_bit;
425 return 1;
426
427 missing:
428 if (!(req->flags & (BF_SHUTR|BF_FULL))) {
429 buffer_dont_connect(s->req);
430 return 0;
431 }
432 /* missing data and buffer is either full or shutdown => fail */
433
434 fail:
435 buffer_abort(req);
436 buffer_abort(s->rep);
437 req->analysers = 0;
438
439 s->fe->counters.failed_req++;
440 if (s->listener->counters)
441 s->listener->counters->failed_req++;
442
443 if (!(s->flags & SN_ERR_MASK))
444 s->flags |= SN_ERR_PRXCOND;
445 if (!(s->flags & SN_FINST_MASK))
446 s->flags |= SN_FINST_R;
447 return 0;
448}
449
Willy Tarreau645513a2010-05-24 20:55:15 +0200450/* set test->i to the id of the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200451static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200452acl_fetch_fe_id(struct proxy *px, struct session *l4, void *l7, int dir,
453 struct acl_expr *expr, struct acl_test *test) {
Willy Tarreau662b2d82007-05-08 19:56:15 +0200454
Willy Tarreau662b2d82007-05-08 19:56:15 +0200455 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200456
Willy Tarreau645513a2010-05-24 20:55:15 +0200457 test->i = l4->fe->uuid;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200458
Emeric Brun5d16eda2010-01-04 15:47:45 +0100459 return 1;
460}
461
Willy Tarreau645513a2010-05-24 20:55:15 +0200462/* set test->i to the number of connections per second reaching the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200463static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200464acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
465 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200466{
Willy Tarreau645513a2010-05-24 20:55:15 +0200467 test->flags = ACL_TEST_F_VOL_TEST;
468 if (expr->arg_len) {
469 /* another proxy was designated, we must look for it */
470 for (px = proxy; px; px = px->next)
471 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
472 break;
473 }
474 if (!px)
475 return 0;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100476
Willy Tarreau645513a2010-05-24 20:55:15 +0200477 test->i = read_freq_ctr(&px->fe_sess_per_sec);
Emeric Brun5d16eda2010-01-04 15:47:45 +0100478 return 1;
479}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100480
Willy Tarreau645513a2010-05-24 20:55:15 +0200481/* set test->i to the number of concurrent connections on the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200482static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200483acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
484 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200485{
Willy Tarreau645513a2010-05-24 20:55:15 +0200486 test->flags = ACL_TEST_F_VOL_TEST;
487 if (expr->arg_len) {
488 /* another proxy was designated, we must look for it */
489 for (px = proxy; px; px = px->next)
490 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
491 break;
492 }
493 if (!px)
494 return 0;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100495
Willy Tarreau645513a2010-05-24 20:55:15 +0200496 test->i = px->feconn;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100497 return 1;
498}
499
Willy Tarreau8797c062007-05-07 00:55:35 +0200500
501/* Note: must not be declared <const> as its list will be overwritten */
502static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +0200503 { "fe_id", acl_parse_int, acl_fetch_fe_id, acl_match_int, ACL_USE_NOTHING },
504 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
505 { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8797c062007-05-07 00:55:35 +0200506 { NULL, NULL, NULL, NULL },
507}};
508
509
510__attribute__((constructor))
Willy Tarreau03fa5df2010-05-24 21:02:37 +0200511static void __frontend_init(void)
Willy Tarreau8797c062007-05-07 00:55:35 +0200512{
513 acl_register_keywords(&acl_kws);
514}
515
516
Willy Tarreaubaaee002006-06-26 02:48:02 +0200517/*
518 * Local variables:
519 * c-indent-level: 8
520 * c-basic-offset: 8
521 * End:
522 */