blob: 76ea122c988a5e6f720e6b2a6ab32bd0f5b53eea [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Client-side variables and functions.
3 *
Willy Tarreau2c9f5b12009-08-16 19:12:36 +02004 * Copyright 2000-2009 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 Tarreau2dd0d472006-06-29 17:53:05 +020025#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
Willy Tarreaubaaee002006-06-26 02:48:02 +020027#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
Willy Tarreau8797c062007-05-07 00:55:35 +020029#include <proto/acl.h>
Willy Tarreau54469402006-07-29 16:59:06 +020030#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <proto/client.h>
32#include <proto/fd.h>
33#include <proto/log.h>
Willy Tarreaue5f20dc2006-12-03 15:21:35 +010034#include <proto/hdr_idx.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020035#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020036#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010037#include <proto/proxy.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020038#include <proto/session.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010039#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <proto/stream_sock.h>
41#include <proto/task.h>
42
43
Willy Tarreau14c8aac2007-05-08 19:46:30 +020044/* Retrieves the original destination address used by the client, and sets the
45 * SN_FRT_ADDR_SET flag.
46 */
47void get_frt_addr(struct session *s)
48{
49 socklen_t namelen = sizeof(s->frt_addr);
50
Willy Tarreau7e5067d2008-12-07 16:27:56 +010051 if (get_original_dst(s->si[0].fd, (struct sockaddr_in *)&s->frt_addr, &namelen) == -1)
52 getsockname(s->si[0].fd, (struct sockaddr *)&s->frt_addr, &namelen);
Willy Tarreau14c8aac2007-05-08 19:46:30 +020053 s->flags |= SN_FRT_ADDR_SET;
54}
Willy Tarreaubaaee002006-06-26 02:48:02 +020055
56/*
57 * FIXME: This should move to the STREAM_SOCK code then split into TCP and HTTP.
58 */
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010059
Willy Tarreaubaaee002006-06-26 02:48:02 +020060/*
61 * this function is called on a read event from a listen socket, corresponding
62 * to an accept. It tries to accept as many connections as possible.
63 * It returns 0.
64 */
65int event_accept(int fd) {
Willy Tarreaueabf3132008-08-29 23:36:51 +020066 struct listener *l = fdtab[fd].owner;
Willy Tarreaue6b98942007-10-29 01:09:36 +010067 struct proxy *p = (struct proxy *)l->private; /* attached frontend */
Willy Tarreaubaaee002006-06-26 02:48:02 +020068 struct session *s;
Willy Tarreauc2168d32007-03-03 20:51:44 +010069 struct http_txn *txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +020070 struct task *t;
71 int cfd;
Willy Tarreaua0250ba2008-01-06 11:22:57 +010072 int max_accept = global.tune.maxaccept;
Willy Tarreaubaaee002006-06-26 02:48:02 +020073
Willy Tarreau13a34bd2009-05-10 18:52:49 +020074 if (p->fe_sps_lim) {
75 int max = freq_ctr_remain(&p->fe_sess_per_sec, p->fe_sps_lim, 0);
Willy Tarreau79584222009-03-06 09:18:27 +010076 if (max_accept > max)
77 max_accept = max;
78 }
79
Willy Tarreaub00f9c42009-03-21 22:43:12 +010080 while (p->feconn < p->maxconn && actconn < global.maxconn && max_accept--) {
Willy Tarreaubaaee002006-06-26 02:48:02 +020081 struct sockaddr_storage addr;
82 socklen_t laddr = sizeof(addr);
83
84 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) {
85 switch (errno) {
86 case EAGAIN:
87 case EINTR:
88 case ECONNABORTED:
89 return 0; /* nothing more to accept */
90 case ENFILE:
91 send_log(p, LOG_EMERG,
92 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
93 p->id, maxfd);
94 return 0;
95 case EMFILE:
96 send_log(p, LOG_EMERG,
97 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
98 p->id, maxfd);
99 return 0;
100 case ENOBUFS:
101 case ENOMEM:
102 send_log(p, LOG_EMERG,
103 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
104 p->id, maxfd);
105 return 0;
106 default:
107 return 0;
108 }
109 }
110
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +0200111 if (l->nbconn >= l->maxconn) {
112 /* too many connections, we shoot this one and return.
113 * FIXME: it would be better to simply switch the listener's
114 * state to LI_FULL and disable the FD. We could re-enable
115 * it upon fd_delete(), but this requires all protocols to
116 * be switched.
117 */
118 goto out_close;
119 }
120
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200121 if ((s = pool_alloc2(pool2_session)) == NULL) { /* disable this proxy for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 Alert("out of memory in event_accept().\n");
Willy Tarreauf161a342007-04-08 16:59:42 +0200123 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124 p->state = PR_STIDLE;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100125 goto out_close;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200126 }
127
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100128 LIST_ADDQ(&sessions, &s->list);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100129 LIST_INIT(&s->back_refs);
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100130
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200131 s->flags = 0;
Willy Tarreauf8533202008-08-16 14:55:08 +0200132 s->term_trace = 0;
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200133
Willy Tarreaubaaee002006-06-26 02:48:02 +0200134 /* if this session comes from a known monitoring system, we want to ignore
135 * it as soon as possible, which means closing it immediately for TCP.
136 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200137 if (addr.ss_family == AF_INET &&
138 p->mon_mask.s_addr &&
139 (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr) {
140 if (p->mode == PR_MODE_TCP) {
141 close(cfd);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200142 pool_free2(pool2_session, s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200143 continue;
144 }
145 s->flags |= SN_MONITOR;
146 }
147
Willy Tarreaua4613182009-03-21 18:13:21 +0100148 if ((t = task_new()) == NULL) { /* disable this proxy for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200149 Alert("out of memory in event_accept().\n");
Willy Tarreauf161a342007-04-08 16:59:42 +0200150 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200151 p->state = PR_STIDLE;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100152 goto out_free_session;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200153 }
154
155 s->cli_addr = addr;
156 if (cfd >= global.maxsock) {
157 Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100158 goto out_free_task;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200159 }
160
161 if ((fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) ||
162 (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY,
163 (char *) &one, sizeof(one)) == -1)) {
164 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100165 goto out_free_task;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166 }
167
168 if (p->options & PR_O_TCP_CLI_KA)
169 setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
170
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200171 if (p->options & PR_O_TCP_NOLING)
172 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
173
Willy Tarreau3bc13772008-12-07 11:50:35 +0100174 t->process = l->handler;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200175 t->context = s;
Willy Tarreau2c9f5b12009-08-16 19:12:36 +0200176 t->nice = l->nice;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200177
178 s->task = t;
Willy Tarreaub5654f62008-12-07 16:45:10 +0100179 s->listener = l;
Willy Tarreau73de9892006-11-30 11:40:23 +0100180
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200181 /* Note: initially, the session's backend points to the frontend.
182 * This changes later when switching rules are executed or
183 * when the default backend is assigned.
Willy Tarreaua7e76142007-11-03 14:28:39 +0100184 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +0200185 s->be = s->fe = p;
Willy Tarreaua7e76142007-11-03 14:28:39 +0100186
Willy Tarreauff8d42e2008-12-07 14:37:09 +0100187 s->ana_state = 0; /* analysers may change it but must reset it upon exit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200188 s->req = s->rep = NULL; /* will be allocated later */
189
Willy Tarreau35374672008-09-03 18:11:02 +0200190 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200191 s->si[0].err_type = SI_ET_NONE;
192 s->si[0].err_loc = NULL;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200193 s->si[0].owner = t;
Willy Tarreau9650f372009-08-16 14:02:45 +0200194 s->si[0].connect = NULL;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200195 s->si[0].shutr = stream_sock_shutr;
Willy Tarreau48adac52008-08-30 04:58:38 +0200196 s->si[0].shutw = stream_sock_shutw;
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100197 s->si[0].chk_rcv = stream_sock_chk_rcv;
198 s->si[0].chk_snd = stream_sock_chk_snd;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200199 s->si[0].fd = cfd;
Willy Tarreaudc340a92009-06-28 23:10:19 +0200200 s->si[0].flags = SI_FL_NONE | SI_FL_CAP_SPLTCP; /* TCP splicing capable */
Willy Tarreau35374672008-09-03 18:11:02 +0200201 s->si[0].exp = TICK_ETERNITY;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200202
Willy Tarreau35374672008-09-03 18:11:02 +0200203 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200204 s->si[1].err_type = SI_ET_NONE;
205 s->si[1].err_loc = NULL;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200206 s->si[1].owner = t;
Willy Tarreau9650f372009-08-16 14:02:45 +0200207 s->si[1].connect = tcpv4_connect_server;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200208 s->si[1].shutr = stream_sock_shutr;
Willy Tarreau48adac52008-08-30 04:58:38 +0200209 s->si[1].shutw = stream_sock_shutw;
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100210 s->si[1].chk_rcv = stream_sock_chk_rcv;
211 s->si[1].chk_snd = stream_sock_chk_snd;
Willy Tarreau35374672008-09-03 18:11:02 +0200212 s->si[1].exp = TICK_ETERNITY;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200213 s->si[1].fd = -1; /* just to help with debugging */
Willy Tarreaud7704b52008-09-04 11:51:16 +0200214 s->si[1].flags = SI_FL_NONE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200215
Willy Tarreau7c669d72008-06-20 15:04:11 +0200216 s->srv = s->prev_srv = s->srv_conn = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200217 s->pend_pos = NULL;
Willy Tarreaua7e76142007-11-03 14:28:39 +0100218 s->conn_retries = s->be->conn_retries;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100219
220 /* FIXME: the logs are horribly complicated now, because they are
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200221 * defined in <p>, <p>, and later <be> and <be>.
Willy Tarreauddb358d2006-12-17 22:55:52 +0100222 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200223
224 if (s->flags & SN_MONITOR)
225 s->logs.logwait = 0;
226 else
227 s->logs.logwait = p->to_log;
228
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100229 if (s->logs.logwait & LW_REQ)
230 s->do_log = http_sess_log;
231 else
232 s->do_log = tcp_sess_log;
233
Willy Tarreau52a0c602009-08-16 22:45:38 +0200234 /* default error reporting function, may be changed by analysers */
235 s->srv_error = default_srv_error;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100236
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200237 s->logs.accept_date = date; /* user-visible date for logging */
238 s->logs.tv_accept = now; /* corrected date for internal use */
Willy Tarreau70089872008-06-13 21:12:51 +0200239 tv_zero(&s->logs.tv_request);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200240 s->logs.t_queue = -1;
241 s->logs.t_connect = -1;
242 s->logs.t_data = -1;
243 s->logs.t_close = 0;
Willy Tarreau35d66b02007-01-02 00:28:21 +0100244 s->logs.bytes_in = s->logs.bytes_out = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
246 s->logs.srv_queue_size = 0; /* we will get this number soon */
247
248 s->data_source = DATA_SRC_NONE;
249
250 s->uniq_id = totalconn;
Willy Tarreau7f062c42009-03-05 18:43:00 +0100251 proxy_inc_fe_ctr(p); /* note: cum_beconn will be increased once assigned */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200252
Willy Tarreauc2168d32007-03-03 20:51:44 +0100253 txn = &s->txn;
Willy Tarreau3d300592007-03-18 18:34:41 +0100254 txn->flags = 0;
Willy Tarreau042cc792007-03-19 16:20:06 +0100255 /* Those variables will be checked and freed if non-NULL in
256 * session.c:session_free(). It is important that they are
257 * properly initialized.
258 */
259 txn->srv_cookie = NULL;
260 txn->cli_cookie = NULL;
261 txn->uri = NULL;
Willy Tarreauc2168d32007-03-03 20:51:44 +0100262 txn->req.cap = NULL;
263 txn->rsp.cap = NULL;
264 txn->hdr_idx.v = NULL;
265 txn->hdr_idx.size = txn->hdr_idx.used = 0;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100266
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200267 /* we always initialize the HTTP structure because we may use it later */
268 txn->status = -1;
269 txn->req.hdr_content_len = 0LL;
270 txn->rsp.hdr_content_len = 0LL;
271 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
272 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
273 txn->req.sol = txn->req.eol = NULL;
274 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
275 txn->rsp.sol = txn->rsp.eol = NULL;
276 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
277 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
278 txn->auth_hdr.len = -1;
279 if (p->options2 & PR_O2_REQBUG_OK)
280 txn->req.err_pos = -1; /* let buggy requests pass */
Willy Tarreau45e73e32006-12-17 00:05:15 +0100281
Willy Tarreau2492d5b2009-07-11 00:06:00 +0200282 if (p->mode == PR_MODE_HTTP) {
283 /* the captures are only used in HTTP frontends */
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200284 if (p->nb_req_cap > 0) {
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100285 if ((txn->req.cap = pool_alloc2(p->req_cap_pool)) == NULL)
286 goto out_fail_reqcap; /* no memory */
287
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200288 memset(txn->req.cap, 0, p->nb_req_cap*sizeof(char *));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200289 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200290
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200291 if (p->nb_rsp_cap > 0) {
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100292 if ((txn->rsp.cap = pool_alloc2(p->rsp_cap_pool)) == NULL)
293 goto out_fail_rspcap; /* no memory */
294
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200295 memset(txn->rsp.cap, 0, p->nb_rsp_cap*sizeof(char *));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200296 }
Willy Tarreaubf288622009-07-10 23:52:51 +0200297 }
Willy Tarreau45e73e32006-12-17 00:05:15 +0100298
Willy Tarreaubf288622009-07-10 23:52:51 +0200299 if (p->acl_requires & ACL_USE_L7_ANY) {
300 /* we have to allocate header indexes only if we know
301 * that we may make use of them. This of course includes
302 * (mode == PR_MODE_HTTP).
303 */
Willy Tarreau1d4154a2007-05-13 22:57:02 +0200304 txn->hdr_idx.size = MAX_HTTP_HDR;
305
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100306 if ((txn->hdr_idx.v = pool_alloc2(p->hdr_idx_pool)) == NULL)
307 goto out_fail_idx; /* no memory */
308
Willy Tarreauc2168d32007-03-03 20:51:44 +0100309 hdr_idx_init(&txn->hdr_idx);
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100310 }
311
Willy Tarreaubaaee002006-06-26 02:48:02 +0200312 if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP)
313 && (p->logfac1 >= 0 || p->logfac2 >= 0)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200314 if (p->to_log) {
315 /* we have the client ip */
316 if (s->logs.logwait & LW_CLIP)
317 if (!(s->logs.logwait &= ~LW_CLIP))
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100318 s->do_log(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200319 }
320 else if (s->cli_addr.ss_family == AF_INET) {
321 char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200322
323 if (!(s->flags & SN_FRT_ADDR_SET))
324 get_frt_addr(s);
325
326 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327 sn, sizeof(sn)) &&
328 inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
329 pn, sizeof(pn))) {
330 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
331 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port),
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200332 sn, ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200333 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
334 }
335 }
336 else {
337 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200338
339 if (!(s->flags & SN_FRT_ADDR_SET))
340 get_frt_addr(s);
341
342 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->frt_addr)->sin6_addr,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200343 sn, sizeof(sn)) &&
344 inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr,
345 pn, sizeof(pn))) {
346 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
347 pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200348 sn, ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
350 }
351 }
352 }
353
354 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200355 int len;
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200356
357 if (!(s->flags & SN_FRT_ADDR_SET))
358 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359
360 if (s->cli_addr.ss_family == AF_INET) {
361 char pn[INET_ADDRSTRLEN];
362 inet_ntop(AF_INET,
363 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
364 pn, sizeof(pn));
365
366 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
367 s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd,
368 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port));
369 }
370 else {
371 char pn[INET6_ADDRSTRLEN];
372 inet_ntop(AF_INET6,
373 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
374 pn, sizeof(pn));
375
376 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
377 s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd,
378 pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port));
379 }
380
381 write(1, trash, len);
382 }
383
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100384 if ((s->req = pool_alloc2(pool2_buffer)) == NULL)
385 goto out_fail_req; /* no memory */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386
Willy Tarreau27a674e2009-08-17 07:23:33 +0200387 s->req->size = global.tune.bufsize;
Willy Tarreau54469402006-07-29 16:59:06 +0200388 buffer_init(s->req);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200389 s->req->prod = &s->si[0];
390 s->req->cons = &s->si[1];
Willy Tarreau48adac52008-08-30 04:58:38 +0200391 s->si[0].ib = s->si[1].ob = s->req;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200392
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200393 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
394
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100395 if (p->mode == PR_MODE_HTTP) { /* reserve some space for header rewriting */
Willy Tarreau27a674e2009-08-17 07:23:33 +0200396 s->req->max_len -= global.tune.maxrewrite;
Willy Tarreau1b194fe2009-03-21 21:10:04 +0100397 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
398 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200399
Willy Tarreau3bc13772008-12-07 11:50:35 +0100400 /* activate default analysers enabled for this listener */
401 s->req->analysers = l->analysers;
Willy Tarreau2df28e82008-08-17 15:20:19 +0200402
Willy Tarreauc1a21672009-08-16 22:37:44 +0200403 /* note: this should not happen anymore since there's always at least the switching rules */
Willy Tarreau2df28e82008-08-17 15:20:19 +0200404 if (!s->req->analysers)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200405 buffer_write_ena(s->req); /* don't wait to establish connection */
Willy Tarreaudc0a6a02008-08-03 20:38:13 +0200406
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100407 s->req->rto = s->fe->timeout.client;
408 s->req->wto = s->be->timeout.server;
409 s->req->cto = s->be->timeout.connect;
Willy Tarreaud7971282006-07-29 18:36:34 +0200410
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100411 if ((s->rep = pool_alloc2(pool2_buffer)) == NULL)
412 goto out_fail_rep; /* no memory */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200413
Willy Tarreau27a674e2009-08-17 07:23:33 +0200414 s->rep->size = global.tune.bufsize;
Willy Tarreau54469402006-07-29 16:59:06 +0200415 buffer_init(s->rep);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200416 s->rep->prod = &s->si[1];
417 s->rep->cons = &s->si[0];
Willy Tarreau48adac52008-08-30 04:58:38 +0200418 s->si[0].ob = s->si[1].ib = s->rep;
Willy Tarreau54469402006-07-29 16:59:06 +0200419
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100420 s->rep->rto = s->be->timeout.server;
421 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200422 s->rep->cto = TICK_ETERNITY;
Willy Tarreaud7971282006-07-29 18:36:34 +0200423
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200424 s->req->rex = TICK_ETERNITY;
425 s->req->wex = TICK_ETERNITY;
Willy Tarreauffab5b42008-08-17 18:03:28 +0200426 s->req->analyse_exp = TICK_ETERNITY;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200427 s->rep->rex = TICK_ETERNITY;
428 s->rep->wex = TICK_ETERNITY;
Willy Tarreauffab5b42008-08-17 18:03:28 +0200429 s->rep->analyse_exp = TICK_ETERNITY;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200430 t->expire = TICK_ETERNITY;
431
Willy Tarreau7a966482007-04-15 10:58:02 +0200432 fd_insert(cfd);
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200433 fdtab[cfd].owner = &s->si[0];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200434 fdtab[cfd].state = FD_STREADY;
Willy Tarreaufb14edc2009-06-14 15:24:37 +0200435 fdtab[cfd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreau5d707e12009-06-28 11:09:07 +0200436 if (p->options & PR_O_TCP_NOLING)
437 fdtab[cfd].flags |= FD_FL_TCP_NOLING;
438
Willy Tarreaue6b98942007-10-29 01:09:36 +0100439 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
Willy Tarreau54469402006-07-29 16:59:06 +0200440 fdtab[cfd].cb[DIR_RD].b = s->req;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100441 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
Willy Tarreau54469402006-07-29 16:59:06 +0200442 fdtab[cfd].cb[DIR_WR].b = s->rep;
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200443 fdtab[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
444 fdtab[cfd].peerlen = sizeof(s->cli_addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200445
446 if ((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||
Willy Tarreau0f772532006-12-23 20:51:41 +0100447 (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200448 /* Either we got a request from a monitoring system on an HTTP instance,
449 * or we're in health check mode with the 'httpchk' option enabled. In
450 * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
451 */
Willy Tarreau0f772532006-12-23 20:51:41 +0100452 struct chunk msg = { .str = "HTTP/1.0 200 OK\r\n\r\n", .len = 19 };
Willy Tarreaudded32d2008-11-30 19:48:07 +0100453 stream_int_retnclose(&s->si[0], &msg); /* forge a 200 response */
Willy Tarreau2ade3012009-03-06 19:16:39 +0100454 s->req->analysers = 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200455 t->expire = s->rep->wex;
Willy Tarreau0f772532006-12-23 20:51:41 +0100456 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200457 else if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */
Willy Tarreau0f772532006-12-23 20:51:41 +0100458 struct chunk msg = { .str = "OK\n", .len = 3 };
Willy Tarreaudded32d2008-11-30 19:48:07 +0100459 stream_int_retnclose(&s->si[0], &msg); /* forge an "OK" response */
Willy Tarreau2ade3012009-03-06 19:16:39 +0100460 s->req->analysers = 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200461 t->expire = s->rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200462 }
463 else {
Willy Tarreauf161a342007-04-08 16:59:42 +0200464 EV_FD_SET(cfd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465 }
466
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200467 /* it is important not to call the wakeup function directly but to
468 * pass through task_wakeup(), because this one knows how to apply
469 * priorities to tasks.
470 */
Willy Tarreau721fdbc2009-03-08 12:25:07 +0100471 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200472
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +0200473 l->nbconn++; /* warning! right now, it's up to the handler to decrease this */
474 if (l->nbconn >= l->maxconn) {
475 EV_FD_CLR(l->fd, DIR_RD);
476 l->state = LI_FULL;
477 }
478
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100479 p->feconn++; /* beconn will be increased later */
480 if (p->feconn > p->feconn_max)
481 p->feconn_max = p->feconn;
Willy Tarreaua7e76142007-11-03 14:28:39 +0100482
Willy Tarreaubaaee002006-06-26 02:48:02 +0200483 actconn++;
484 totalconn++;
485
486 // fprintf(stderr, "accepting from %p => %d conn, %d total, task=%p\n", p, actconn, totalconn, t);
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100487 } /* end of while (p->feconn < p->maxconn) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200488 return 0;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100489
490 /* Error unrolling */
491 out_fail_rep:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200492 pool_free2(pool2_buffer, s->req);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100493 out_fail_req:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200494 pool_free2(p->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100495 out_fail_idx:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200496 pool_free2(p->rsp_cap_pool, txn->rsp.cap);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100497 out_fail_rspcap:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200498 pool_free2(p->req_cap_pool, txn->req.cap);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100499 out_fail_reqcap:
500 out_free_task:
Willy Tarreaua4613182009-03-21 18:13:21 +0100501 task_free(t);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100502 out_free_session:
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100503 LIST_DEL(&s->list);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100504 pool_free2(pool2_session, s);
505 out_close:
506 close(cfd);
507 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200508}
509
510
511
Willy Tarreau8797c062007-05-07 00:55:35 +0200512/************************************************************************/
513/* All supported keywords must be declared here. */
514/************************************************************************/
515
516/* set test->ptr to point to the source IPv4/IPv6 address and test->i to the family */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200517static int
Willy Tarreau97be1452007-06-10 11:47:14 +0200518acl_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir,
519 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200520{
521 test->i = l4->cli_addr.ss_family;
522 if (test->i == AF_INET)
523 test->ptr = (void *)&((struct sockaddr_in *)&l4->cli_addr)->sin_addr;
524 else
525 test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_addr;
526 test->flags = ACL_TEST_F_READ_ONLY;
527 return 1;
528}
529
530
531/* set test->i to the connexion's source port */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200532static int
Willy Tarreau97be1452007-06-10 11:47:14 +0200533acl_fetch_sport(struct proxy *px, struct session *l4, void *l7, int dir,
534 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200535{
536 if (l4->cli_addr.ss_family == AF_INET)
537 test->i = ntohs(((struct sockaddr_in *)&l4->cli_addr)->sin_port);
538 else
539 test->i = ntohs(((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_port);
540 test->flags = 0;
541 return 1;
542}
543
Willy Tarreau662b2d82007-05-08 19:56:15 +0200544
545/* set test->ptr to point to the frontend's IPv4/IPv6 address and test->i to the family */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200546static int
Willy Tarreau97be1452007-06-10 11:47:14 +0200547acl_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir,
548 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200549{
550 if (!(l4->flags & SN_FRT_ADDR_SET))
551 get_frt_addr(l4);
552
553 test->i = l4->frt_addr.ss_family;
554 if (test->i == AF_INET)
555 test->ptr = (void *)&((struct sockaddr_in *)&l4->frt_addr)->sin_addr;
556 else
557 test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_addr;
558 test->flags = ACL_TEST_F_READ_ONLY;
559 return 1;
560}
561
562
563/* set test->i to the frontend connexion's destination port */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200564static int
Willy Tarreau97be1452007-06-10 11:47:14 +0200565acl_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir,
566 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200567{
568 if (!(l4->flags & SN_FRT_ADDR_SET))
569 get_frt_addr(l4);
570
571 if (l4->frt_addr.ss_family == AF_INET)
572 test->i = ntohs(((struct sockaddr_in *)&l4->frt_addr)->sin_port);
573 else
574 test->i = ntohs(((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_port);
575 test->flags = 0;
576 return 1;
577}
578
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100579
Willy Tarreau8797c062007-05-07 00:55:35 +0200580/* set test->i to the number of connexions to the proxy */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200581static int
Willy Tarreau97be1452007-06-10 11:47:14 +0200582acl_fetch_dconn(struct proxy *px, struct session *l4, void *l7, int dir,
583 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200584{
585 test->i = px->feconn;
586 return 1;
587}
588
589
590/* Note: must not be declared <const> as its list will be overwritten */
591static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +0200592 { "src_port", acl_parse_int, acl_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT },
593 { "src", acl_parse_ip, acl_fetch_src, acl_match_ip, ACL_USE_TCP4_PERMANENT },
594 { "dst", acl_parse_ip, acl_fetch_dst, acl_match_ip, ACL_USE_TCP4_PERMANENT },
595 { "dst_port", acl_parse_int, acl_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT },
Willy Tarreau662b2d82007-05-08 19:56:15 +0200596#if 0
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100597 { "src_limit", acl_parse_int, acl_fetch_sconn, acl_match_int },
Willy Tarreau8797c062007-05-07 00:55:35 +0200598#endif
Willy Tarreau0ceba5a2008-07-25 19:31:03 +0200599 { "dst_conn", acl_parse_int, acl_fetch_dconn, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8797c062007-05-07 00:55:35 +0200600 { NULL, NULL, NULL, NULL },
601}};
602
603
604__attribute__((constructor))
605static void __client_init(void)
606{
607 acl_register_keywords(&acl_kws);
608}
609
610
Willy Tarreaubaaee002006-06-26 02:48:02 +0200611/*
612 * Local variables:
613 * c-indent-level: 8
614 * c-basic-offset: 8
615 * End:
616 */