blob: cf6606a075f379860347477afcf2b10f2d96ebfb [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 Tarreaufffe1322010-11-11 09:48:16 +010091 if ((s->listener->addr.ss_family != AF_UNIX) &&
92 setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)
Willy Tarreauabe8ea52010-11-11 10:56:04 +010093 goto out_return;
Willy Tarreauf67c9782010-05-23 22:59:00 +020094
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 Tarreauabe8ea52010-11-11 10:56:04 +0100111 goto out_return; /* 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 Tarreauabe8ea52010-11-11 10:56:04 +0100260 out_return:
Willy Tarreaueb472682010-05-28 18:46:57 +0200261 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200262}
263
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200264/* This analyser tries to fetch a line from the request buffer which looks like :
265 *
266 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
267 *
268 * There must be exactly one space between each field. Fields are :
Emeric Brun861ccff2010-10-29 12:03:03 +0200269 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200270 * - SRC3 : layer 3 (eg: IP) source address in standard text form
271 * - DST3 : layer 3 (eg: IP) destination address in standard text form
272 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
273 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
274 *
275 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
276 *
277 * Once the data is fetched, the values are set in the session's field and data
278 * are removed from the buffer. The function returns zero if it needs to wait
279 * for more data (max: timeout_client), or 1 if it has finished and removed itself.
280 */
281int frontend_decode_proxy_request(struct session *s, struct buffer *req, int an_bit)
282{
283 char *line = req->data;
284 char *end = req->data + req->l;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200285 int len;
286
287 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
288 now_ms, __FUNCTION__,
289 s,
290 req,
291 req->rex, req->wex,
292 req->flags,
293 req->l,
294 req->analysers);
295
296 if (req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT))
297 goto fail;
298
Emeric Brunf4711a32010-10-29 15:16:55 +0200299 len = MIN(req->l, 6);
300 if (!len)
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200301 goto missing;
302
Emeric Brunf4711a32010-10-29 15:16:55 +0200303 /* Decode a possible proxy request, fail early if it does not match */
304 if (strncmp(line, "PROXY ", len) != 0)
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200305 goto fail;
Emeric Brunf4711a32010-10-29 15:16:55 +0200306
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200307 line += 6;
Emeric Brunf4711a32010-10-29 15:16:55 +0200308 if (req->l < 18) /* shortest possible line */
309 goto missing;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200310
Emeric Brun861ccff2010-10-29 12:03:03 +0200311 if (!memcmp(line, "TCP4 ", 5) != 0) {
312 u32 src3, dst3, sport, dport;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200313
Emeric Brun861ccff2010-10-29 12:03:03 +0200314 line += 5;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200315
Emeric Brun861ccff2010-10-29 12:03:03 +0200316 src3 = inetaddr_host_lim_ret(line, end, &line);
317 if (line == end)
318 goto missing;
319 if (*line++ != ' ')
320 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200321
Emeric Brun861ccff2010-10-29 12:03:03 +0200322 dst3 = inetaddr_host_lim_ret(line, end, &line);
323 if (line == end)
324 goto missing;
325 if (*line++ != ' ')
326 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200327
Emeric Brun861ccff2010-10-29 12:03:03 +0200328 sport = read_uint((const char **)&line, end);
329 if (line == end)
330 goto missing;
331 if (*line++ != ' ')
332 goto fail;
333
334 dport = read_uint((const char **)&line, end);
335 if (line > end - 2)
336 goto missing;
337 if (*line++ != '\r')
338 goto fail;
339 if (*line++ != '\n')
340 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200341
Emeric Brun861ccff2010-10-29 12:03:03 +0200342 /* update the session's addresses and mark them set */
343 ((struct sockaddr_in *)&s->cli_addr)->sin_family = AF_INET;
344 ((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr = htonl(src3);
345 ((struct sockaddr_in *)&s->cli_addr)->sin_port = htons(sport);
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200346
Emeric Brun861ccff2010-10-29 12:03:03 +0200347 ((struct sockaddr_in *)&s->frt_addr)->sin_family = AF_INET;
348 ((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr = htonl(dst3);
349 ((struct sockaddr_in *)&s->frt_addr)->sin_port = htons(dport);
350 s->flags |= SN_FRT_ADDR_SET;
351
352 }
353 else if (!memcmp(line, "TCP6 ", 5) != 0) {
354 u32 sport, dport;
355 char *src_s;
356 char *dst_s, *sport_s, *dport_s;
357 struct in6_addr src3, dst3;
358
359 line+=5;
360
361 src_s = line;
362 dst_s = sport_s = dport_s = NULL;
363 while (1) {
364 if (line > end - 2) {
365 goto missing;
366 }
367 else if (*line == '\r') {
368 *line = 0;
369 line++;
370 if (*line++ != '\n')
371 goto fail;
372 break;
373 }
374
375 if (*line == ' ') {
376 *line = 0;
377 if (!dst_s)
378 dst_s = line+1;
379 else if (!sport_s)
380 sport_s = line+1;
381 else if (!dport_s)
382 dport_s = line+1;
383 }
384 line++;
385 }
386
387 if (!dst_s || !sport_s || !dport_s)
388 goto fail;
389
390 sport = read_uint((const char **)&sport_s,dport_s-1);
391 if ( *sport_s != 0 )
392 goto fail;
393
394 dport = read_uint((const char **)&dport_s,line-2);
395 if ( *dport_s != 0 )
396 goto fail;
397
398 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
399 goto fail;
400
401 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
402 goto fail;
403
404 /* update the session's addresses and mark them set */
405 ((struct sockaddr_in6 *)&s->cli_addr)->sin6_family = AF_INET6;
406 memcpy(&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr, &src3, sizeof(struct in6_addr));
407 ((struct sockaddr_in6 *)&s->cli_addr)->sin6_port = htons(sport);
408
409 ((struct sockaddr_in6 *)&s->frt_addr)->sin6_family = AF_INET6;
410 memcpy(&((struct sockaddr_in6 *)&s->frt_addr)->sin6_addr, &dst3, sizeof(struct in6_addr));
411 ((struct sockaddr_in6 *)&s->frt_addr)->sin6_port = htons(dport);
412 s->flags |= SN_FRT_ADDR_SET;
413 }
414 else {
415 goto fail;
416 }
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200417
418 /* remove the PROXY line from the request */
419 len = line - req->data;
420 buffer_replace2(req, req->data, line, NULL, 0);
421 req->total -= len; /* don't count the header line */
422
423 req->analysers &= ~an_bit;
424 return 1;
425
426 missing:
427 if (!(req->flags & (BF_SHUTR|BF_FULL))) {
428 buffer_dont_connect(s->req);
429 return 0;
430 }
431 /* missing data and buffer is either full or shutdown => fail */
432
433 fail:
434 buffer_abort(req);
435 buffer_abort(s->rep);
436 req->analysers = 0;
437
438 s->fe->counters.failed_req++;
439 if (s->listener->counters)
440 s->listener->counters->failed_req++;
441
442 if (!(s->flags & SN_ERR_MASK))
443 s->flags |= SN_ERR_PRXCOND;
444 if (!(s->flags & SN_FINST_MASK))
445 s->flags |= SN_FINST_R;
446 return 0;
447}
448
Willy Tarreau645513a2010-05-24 20:55:15 +0200449/* set test->i to the id of the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200450static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200451acl_fetch_fe_id(struct proxy *px, struct session *l4, void *l7, int dir,
452 struct acl_expr *expr, struct acl_test *test) {
Willy Tarreau662b2d82007-05-08 19:56:15 +0200453
Willy Tarreau662b2d82007-05-08 19:56:15 +0200454 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200455
Willy Tarreau645513a2010-05-24 20:55:15 +0200456 test->i = l4->fe->uuid;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200457
Emeric Brun5d16eda2010-01-04 15:47:45 +0100458 return 1;
459}
460
Willy Tarreau645513a2010-05-24 20:55:15 +0200461/* set test->i to the number of connections per second reaching the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200462static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200463acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
464 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200465{
Willy Tarreau645513a2010-05-24 20:55:15 +0200466 test->flags = ACL_TEST_F_VOL_TEST;
467 if (expr->arg_len) {
468 /* another proxy was designated, we must look for it */
469 for (px = proxy; px; px = px->next)
470 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
471 break;
472 }
473 if (!px)
474 return 0;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100475
Willy Tarreau645513a2010-05-24 20:55:15 +0200476 test->i = read_freq_ctr(&px->fe_sess_per_sec);
Emeric Brun5d16eda2010-01-04 15:47:45 +0100477 return 1;
478}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100479
Willy Tarreau645513a2010-05-24 20:55:15 +0200480/* set test->i to the number of concurrent connections on the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200481static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200482acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
483 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200484{
Willy Tarreau645513a2010-05-24 20:55:15 +0200485 test->flags = ACL_TEST_F_VOL_TEST;
486 if (expr->arg_len) {
487 /* another proxy was designated, we must look for it */
488 for (px = proxy; px; px = px->next)
489 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
490 break;
491 }
492 if (!px)
493 return 0;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100494
Willy Tarreau645513a2010-05-24 20:55:15 +0200495 test->i = px->feconn;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100496 return 1;
497}
498
Willy Tarreau8797c062007-05-07 00:55:35 +0200499
500/* Note: must not be declared <const> as its list will be overwritten */
501static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +0200502 { "fe_id", acl_parse_int, acl_fetch_fe_id, acl_match_int, ACL_USE_NOTHING },
503 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
504 { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8797c062007-05-07 00:55:35 +0200505 { NULL, NULL, NULL, NULL },
506}};
507
508
509__attribute__((constructor))
Willy Tarreau03fa5df2010-05-24 21:02:37 +0200510static void __frontend_init(void)
Willy Tarreau8797c062007-05-07 00:55:35 +0200511{
512 acl_register_keywords(&acl_kws);
513}
514
515
Willy Tarreaubaaee002006-06-26 02:48:02 +0200516/*
517 * Local variables:
518 * c-indent-level: 8
519 * c-basic-offset: 8
520 * End:
521 */