blob: f6ea7466a698a58c42ddd2f5a7b968c7463ba1dd [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Client-side variables and functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 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 Tarreaubaaee002006-06-26 02:48:02 +020035#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010036#include <proto/proxy.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020037#include <proto/session.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010038#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <proto/stream_sock.h>
40#include <proto/task.h>
41
42
Willy Tarreau14c8aac2007-05-08 19:46:30 +020043/* Retrieves the original destination address used by the client, and sets the
44 * SN_FRT_ADDR_SET flag.
45 */
46void get_frt_addr(struct session *s)
47{
48 socklen_t namelen = sizeof(s->frt_addr);
49
Willy Tarreau7e5067d2008-12-07 16:27:56 +010050 if (get_original_dst(s->si[0].fd, (struct sockaddr_in *)&s->frt_addr, &namelen) == -1)
51 getsockname(s->si[0].fd, (struct sockaddr *)&s->frt_addr, &namelen);
Willy Tarreau14c8aac2007-05-08 19:46:30 +020052 s->flags |= SN_FRT_ADDR_SET;
53}
Willy Tarreaubaaee002006-06-26 02:48:02 +020054
55/*
56 * FIXME: This should move to the STREAM_SOCK code then split into TCP and HTTP.
57 */
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010058
Willy Tarreaubaaee002006-06-26 02:48:02 +020059/*
60 * this function is called on a read event from a listen socket, corresponding
61 * to an accept. It tries to accept as many connections as possible.
62 * It returns 0.
63 */
64int event_accept(int fd) {
Willy Tarreaueabf3132008-08-29 23:36:51 +020065 struct listener *l = fdtab[fd].owner;
Willy Tarreaue6b98942007-10-29 01:09:36 +010066 struct proxy *p = (struct proxy *)l->private; /* attached frontend */
Willy Tarreaubaaee002006-06-26 02:48:02 +020067 struct session *s;
Willy Tarreauc2168d32007-03-03 20:51:44 +010068 struct http_txn *txn;
Willy Tarreaubaaee002006-06-26 02:48:02 +020069 struct task *t;
70 int cfd;
Willy Tarreaua0250ba2008-01-06 11:22:57 +010071 int max_accept = global.tune.maxaccept;
Willy Tarreaubaaee002006-06-26 02:48:02 +020072
Willy Tarreau3a7d2072009-03-05 23:48:25 +010073 while (p->feconn < p->maxconn &&
74 (!p->fe_maxsps || read_freq_ctr(&p->fe_sess_per_sec) < p->fe_maxsps) &&
75 max_accept--) {
Willy Tarreaubaaee002006-06-26 02:48:02 +020076 struct sockaddr_storage addr;
77 socklen_t laddr = sizeof(addr);
78
79 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) {
80 switch (errno) {
81 case EAGAIN:
82 case EINTR:
83 case ECONNABORTED:
84 return 0; /* nothing more to accept */
85 case ENFILE:
86 send_log(p, LOG_EMERG,
87 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
88 p->id, maxfd);
89 return 0;
90 case EMFILE:
91 send_log(p, LOG_EMERG,
92 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
93 p->id, maxfd);
94 return 0;
95 case ENOBUFS:
96 case ENOMEM:
97 send_log(p, LOG_EMERG,
98 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
99 p->id, maxfd);
100 return 0;
101 default:
102 return 0;
103 }
104 }
105
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200106 if ((s = pool_alloc2(pool2_session)) == NULL) { /* disable this proxy for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107 Alert("out of memory in event_accept().\n");
Willy Tarreauf161a342007-04-08 16:59:42 +0200108 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109 p->state = PR_STIDLE;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100110 goto out_close;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111 }
112
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100113 LIST_ADDQ(&sessions, &s->list);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100114 LIST_INIT(&s->back_refs);
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100115
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200116 s->flags = 0;
Willy Tarreauf8533202008-08-16 14:55:08 +0200117 s->term_trace = 0;
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200118
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119 /* if this session comes from a known monitoring system, we want to ignore
120 * it as soon as possible, which means closing it immediately for TCP.
121 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 if (addr.ss_family == AF_INET &&
123 p->mon_mask.s_addr &&
124 (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr) {
125 if (p->mode == PR_MODE_TCP) {
126 close(cfd);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200127 pool_free2(pool2_session, s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200128 continue;
129 }
130 s->flags |= SN_MONITOR;
131 }
132
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200133 if ((t = pool_alloc2(pool2_task)) == NULL) { /* disable this proxy for a while */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200134 Alert("out of memory in event_accept().\n");
Willy Tarreauf161a342007-04-08 16:59:42 +0200135 EV_FD_CLR(fd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200136 p->state = PR_STIDLE;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100137 goto out_free_session;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200138 }
139
140 s->cli_addr = addr;
141 if (cfd >= global.maxsock) {
142 Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n");
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100143 goto out_free_task;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200144 }
145
146 if ((fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) ||
147 (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY,
148 (char *) &one, sizeof(one)) == -1)) {
149 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100150 goto out_free_task;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200151 }
152
153 if (p->options & PR_O_TCP_CLI_KA)
154 setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
155
Alexandre Cassen87ea5482007-10-11 20:48:58 +0200156 if (p->options & PR_O_TCP_NOLING)
157 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
158
Willy Tarreau9789f7b2008-06-24 08:17:16 +0200159 task_init(t);
Willy Tarreau3bc13772008-12-07 11:50:35 +0100160 t->process = l->handler;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200161 t->context = s;
162
163 s->task = t;
Willy Tarreaub5654f62008-12-07 16:45:10 +0100164 s->listener = l;
Willy Tarreau830ff452006-12-17 19:31:23 +0100165 s->be = s->fe = p;
Willy Tarreau73de9892006-11-30 11:40:23 +0100166
Willy Tarreaua7e76142007-11-03 14:28:39 +0100167 /* in HTTP mode, content switching requires that the backend
168 * first points to the same proxy as the frontend. However, in
169 * TCP mode there will be no header processing so any default
170 * backend must be assigned if set.
171 */
Willy Tarreau67f0eea2008-08-10 22:55:22 +0200172 if (p->mode == PR_MODE_TCP) {
173 if (p->defbe.be)
174 s->be = p->defbe.be;
175 s->flags |= SN_BE_ASSIGNED;
Willy Tarreaua7e76142007-11-03 14:28:39 +0100176 }
177
Willy Tarreauff8d42e2008-12-07 14:37:09 +0100178 s->ana_state = 0; /* analysers may change it but must reset it upon exit */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200179 s->req = s->rep = NULL; /* will be allocated later */
180
Willy Tarreau35374672008-09-03 18:11:02 +0200181 s->si[0].state = s->si[0].prev_state = SI_ST_EST;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200182 s->si[0].err_type = SI_ET_NONE;
183 s->si[0].err_loc = NULL;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200184 s->si[0].owner = t;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200185 s->si[0].shutr = stream_sock_shutr;
Willy Tarreau48adac52008-08-30 04:58:38 +0200186 s->si[0].shutw = stream_sock_shutw;
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100187 s->si[0].chk_rcv = stream_sock_chk_rcv;
188 s->si[0].chk_snd = stream_sock_chk_snd;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200189 s->si[0].fd = cfd;
Willy Tarreaud7704b52008-09-04 11:51:16 +0200190 s->si[0].flags = SI_FL_NONE;
Willy Tarreau35374672008-09-03 18:11:02 +0200191 s->si[0].exp = TICK_ETERNITY;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200192
Willy Tarreau35374672008-09-03 18:11:02 +0200193 s->si[1].state = s->si[1].prev_state = SI_ST_INI;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200194 s->si[1].err_type = SI_ET_NONE;
195 s->si[1].err_loc = NULL;
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200196 s->si[1].owner = t;
Willy Tarreau3c6ab2e2008-09-04 11:19:41 +0200197 s->si[1].shutr = stream_sock_shutr;
Willy Tarreau48adac52008-08-30 04:58:38 +0200198 s->si[1].shutw = stream_sock_shutw;
Willy Tarreau3ffeba12008-12-14 14:42:35 +0100199 s->si[1].chk_rcv = stream_sock_chk_rcv;
200 s->si[1].chk_snd = stream_sock_chk_snd;
Willy Tarreau35374672008-09-03 18:11:02 +0200201 s->si[1].exp = TICK_ETERNITY;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200202 s->si[1].fd = -1; /* just to help with debugging */
Willy Tarreaud7704b52008-09-04 11:51:16 +0200203 s->si[1].flags = SI_FL_NONE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200204
Willy Tarreau7c669d72008-06-20 15:04:11 +0200205 s->srv = s->prev_srv = s->srv_conn = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200206 s->pend_pos = NULL;
Willy Tarreaua7e76142007-11-03 14:28:39 +0100207 s->conn_retries = s->be->conn_retries;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100208
209 /* FIXME: the logs are horribly complicated now, because they are
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200210 * defined in <p>, <p>, and later <be> and <be>.
Willy Tarreauddb358d2006-12-17 22:55:52 +0100211 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200212
213 if (s->flags & SN_MONITOR)
214 s->logs.logwait = 0;
215 else
216 s->logs.logwait = p->to_log;
217
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100218 if (s->logs.logwait & LW_REQ)
219 s->do_log = http_sess_log;
220 else
221 s->do_log = tcp_sess_log;
222
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100223 if (p->mode == PR_MODE_HTTP)
224 s->srv_error = http_return_srv_error;
225 else
226 s->srv_error = NULL;
227
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200228 s->logs.accept_date = date; /* user-visible date for logging */
229 s->logs.tv_accept = now; /* corrected date for internal use */
Willy Tarreau70089872008-06-13 21:12:51 +0200230 tv_zero(&s->logs.tv_request);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200231 s->logs.t_queue = -1;
232 s->logs.t_connect = -1;
233 s->logs.t_data = -1;
234 s->logs.t_close = 0;
Willy Tarreau35d66b02007-01-02 00:28:21 +0100235 s->logs.bytes_in = s->logs.bytes_out = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200236 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
237 s->logs.srv_queue_size = 0; /* we will get this number soon */
238
239 s->data_source = DATA_SRC_NONE;
240
241 s->uniq_id = totalconn;
Willy Tarreau7f062c42009-03-05 18:43:00 +0100242 proxy_inc_fe_ctr(p); /* note: cum_beconn will be increased once assigned */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200243
Willy Tarreauc2168d32007-03-03 20:51:44 +0100244 txn = &s->txn;
Willy Tarreau3d300592007-03-18 18:34:41 +0100245 txn->flags = 0;
Willy Tarreau042cc792007-03-19 16:20:06 +0100246 /* Those variables will be checked and freed if non-NULL in
247 * session.c:session_free(). It is important that they are
248 * properly initialized.
249 */
250 txn->srv_cookie = NULL;
251 txn->cli_cookie = NULL;
252 txn->uri = NULL;
Willy Tarreauc2168d32007-03-03 20:51:44 +0100253 txn->req.cap = NULL;
254 txn->rsp.cap = NULL;
255 txn->hdr_idx.v = NULL;
256 txn->hdr_idx.size = txn->hdr_idx.used = 0;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100257
258 if (p->mode == PR_MODE_HTTP) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100259 txn->status = -1;
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200260 txn->req.hdr_content_len = 0LL;
261 txn->rsp.hdr_content_len = 0LL;
Willy Tarreauc2168d32007-03-03 20:51:44 +0100262 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
Willy Tarreauc11416f2007-06-17 16:58:38 +0200263 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreauc2168d32007-03-03 20:51:44 +0100264 txn->req.sol = txn->req.eol = NULL;
265 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
266 txn->auth_hdr.len = -1;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100267
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200268 if (p->nb_req_cap > 0) {
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100269 if ((txn->req.cap = pool_alloc2(p->req_cap_pool)) == NULL)
270 goto out_fail_reqcap; /* no memory */
271
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200272 memset(txn->req.cap, 0, p->nb_req_cap*sizeof(char *));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200273 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200274
Willy Tarreau45e73e32006-12-17 00:05:15 +0100275
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200276 if (p->nb_rsp_cap > 0) {
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100277 if ((txn->rsp.cap = pool_alloc2(p->rsp_cap_pool)) == NULL)
278 goto out_fail_rspcap; /* no memory */
279
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200280 memset(txn->rsp.cap, 0, p->nb_rsp_cap*sizeof(char *));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200281 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200282
Willy Tarreau45e73e32006-12-17 00:05:15 +0100283
Willy Tarreau1d4154a2007-05-13 22:57:02 +0200284 txn->hdr_idx.size = MAX_HTTP_HDR;
285
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100286 if ((txn->hdr_idx.v = pool_alloc2(p->hdr_idx_pool)) == NULL)
287 goto out_fail_idx; /* no memory */
288
Willy Tarreauc2168d32007-03-03 20:51:44 +0100289 hdr_idx_init(&txn->hdr_idx);
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100290 }
291
Willy Tarreaubaaee002006-06-26 02:48:02 +0200292 if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP)
293 && (p->logfac1 >= 0 || p->logfac2 >= 0)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200294 if (p->to_log) {
295 /* we have the client ip */
296 if (s->logs.logwait & LW_CLIP)
297 if (!(s->logs.logwait &= ~LW_CLIP))
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100298 s->do_log(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200299 }
300 else if (s->cli_addr.ss_family == AF_INET) {
301 char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200302
303 if (!(s->flags & SN_FRT_ADDR_SET))
304 get_frt_addr(s);
305
306 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200307 sn, sizeof(sn)) &&
308 inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
309 pn, sizeof(pn))) {
310 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
311 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port),
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200312 sn, ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200313 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
314 }
315 }
316 else {
317 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200318
319 if (!(s->flags & SN_FRT_ADDR_SET))
320 get_frt_addr(s);
321
322 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->frt_addr)->sin6_addr,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200323 sn, sizeof(sn)) &&
324 inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr,
325 pn, sizeof(pn))) {
326 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
327 pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200328 sn, ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port),
Willy Tarreaubaaee002006-06-26 02:48:02 +0200329 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
330 }
331 }
332 }
333
334 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200335 int len;
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200336
337 if (!(s->flags & SN_FRT_ADDR_SET))
338 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200339
340 if (s->cli_addr.ss_family == AF_INET) {
341 char pn[INET_ADDRSTRLEN];
342 inet_ntop(AF_INET,
343 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
344 pn, sizeof(pn));
345
346 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
347 s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd,
348 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port));
349 }
350 else {
351 char pn[INET6_ADDRSTRLEN];
352 inet_ntop(AF_INET6,
353 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
354 pn, sizeof(pn));
355
356 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
357 s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd,
358 pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port));
359 }
360
361 write(1, trash, len);
362 }
363
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100364 if ((s->req = pool_alloc2(pool2_buffer)) == NULL)
365 goto out_fail_req; /* no memory */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200366
Willy Tarreau54469402006-07-29 16:59:06 +0200367 buffer_init(s->req);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200368 s->req->prod = &s->si[0];
369 s->req->cons = &s->si[1];
Willy Tarreau48adac52008-08-30 04:58:38 +0200370 s->si[0].ib = s->si[1].ob = s->req;
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200371
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200372 s->req->flags |= BF_READ_ATTACHED; /* the producer is already connected */
373
Willy Tarreaub6866442008-07-14 23:54:42 +0200374 if (p->mode == PR_MODE_HTTP) /* reserve some space for header rewriting */
Willy Tarreau03d60bb2009-01-09 11:13:00 +0100375 s->req->max_len -= MAXREWRITE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200376
Willy Tarreau3bc13772008-12-07 11:50:35 +0100377 /* activate default analysers enabled for this listener */
378 s->req->analysers = l->analysers;
Willy Tarreau2df28e82008-08-17 15:20:19 +0200379
380 if (!s->req->analysers)
Willy Tarreau3da77c52008-08-29 09:58:42 +0200381 buffer_write_ena(s->req); /* don't wait to establish connection */
Willy Tarreaudc0a6a02008-08-03 20:38:13 +0200382
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100383 s->req->rto = s->fe->timeout.client;
384 s->req->wto = s->be->timeout.server;
385 s->req->cto = s->be->timeout.connect;
Willy Tarreaud7971282006-07-29 18:36:34 +0200386
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100387 if ((s->rep = pool_alloc2(pool2_buffer)) == NULL)
388 goto out_fail_rep; /* no memory */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200389
Willy Tarreau54469402006-07-29 16:59:06 +0200390 buffer_init(s->rep);
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200391 s->rep->prod = &s->si[1];
392 s->rep->cons = &s->si[0];
Willy Tarreau48adac52008-08-30 04:58:38 +0200393 s->si[0].ob = s->si[1].ib = s->rep;
Willy Tarreau54469402006-07-29 16:59:06 +0200394
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100395 s->rep->rto = s->be->timeout.server;
396 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200397 s->rep->cto = TICK_ETERNITY;
Willy Tarreaud7971282006-07-29 18:36:34 +0200398
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200399 s->req->rex = TICK_ETERNITY;
400 s->req->wex = TICK_ETERNITY;
Willy Tarreauffab5b42008-08-17 18:03:28 +0200401 s->req->analyse_exp = TICK_ETERNITY;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200402 s->rep->rex = TICK_ETERNITY;
403 s->rep->wex = TICK_ETERNITY;
Willy Tarreauffab5b42008-08-17 18:03:28 +0200404 s->rep->analyse_exp = TICK_ETERNITY;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200405 t->expire = TICK_ETERNITY;
406
Willy Tarreau7a966482007-04-15 10:58:02 +0200407 fd_insert(cfd);
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200408 fdtab[cfd].owner = &s->si[0];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200409 fdtab[cfd].state = FD_STREADY;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100410 fdtab[cfd].cb[DIR_RD].f = l->proto->read;
Willy Tarreau54469402006-07-29 16:59:06 +0200411 fdtab[cfd].cb[DIR_RD].b = s->req;
Willy Tarreaue6b98942007-10-29 01:09:36 +0100412 fdtab[cfd].cb[DIR_WR].f = l->proto->write;
Willy Tarreau54469402006-07-29 16:59:06 +0200413 fdtab[cfd].cb[DIR_WR].b = s->rep;
Willy Tarreaue94ebd02007-10-09 17:14:37 +0200414 fdtab[cfd].peeraddr = (struct sockaddr *)&s->cli_addr;
415 fdtab[cfd].peerlen = sizeof(s->cli_addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200416
417 if ((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||
Willy Tarreau0f772532006-12-23 20:51:41 +0100418 (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK))) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200419 /* Either we got a request from a monitoring system on an HTTP instance,
420 * or we're in health check mode with the 'httpchk' option enabled. In
421 * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
422 */
Willy Tarreau0f772532006-12-23 20:51:41 +0100423 struct chunk msg = { .str = "HTTP/1.0 200 OK\r\n\r\n", .len = 19 };
Willy Tarreaudded32d2008-11-30 19:48:07 +0100424 stream_int_retnclose(&s->si[0], &msg); /* forge a 200 response */
Willy Tarreauf8533202008-08-16 14:55:08 +0200425 trace_term(s, TT_CLIENT_1);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200426 t->expire = s->rep->wex;
Willy Tarreau0f772532006-12-23 20:51:41 +0100427 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200428 else if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */
Willy Tarreau0f772532006-12-23 20:51:41 +0100429 struct chunk msg = { .str = "OK\n", .len = 3 };
Willy Tarreaudded32d2008-11-30 19:48:07 +0100430 stream_int_retnclose(&s->si[0], &msg); /* forge an "OK" response */
Willy Tarreauf8533202008-08-16 14:55:08 +0200431 trace_term(s, TT_CLIENT_2);
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200432 t->expire = s->rep->wex;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200433 }
434 else {
Willy Tarreauf161a342007-04-08 16:59:42 +0200435 EV_FD_SET(cfd, DIR_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200436 }
437
Willy Tarreauc65a3ba2008-08-11 23:42:50 +0200438 /* it is important not to call the wakeup function directly but to
439 * pass through task_wakeup(), because this one knows how to apply
440 * priorities to tasks.
441 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200442 if (p->mode != PR_MODE_HEALTH)
Willy Tarreaufdccded2008-08-29 18:19:04 +0200443 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200444
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100445 p->feconn++; /* beconn will be increased later */
446 if (p->feconn > p->feconn_max)
447 p->feconn_max = p->feconn;
Willy Tarreaua7e76142007-11-03 14:28:39 +0100448
449 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau7f062c42009-03-05 18:43:00 +0100450 proxy_inc_be_ctr(s->be);
Willy Tarreaua7e76142007-11-03 14:28:39 +0100451 s->be->beconn++;
452 if (s->be->beconn > s->be->beconn_max)
453 s->be->beconn_max = s->be->beconn;
454 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200455 actconn++;
456 totalconn++;
457
458 // fprintf(stderr, "accepting from %p => %d conn, %d total, task=%p\n", p, actconn, totalconn, t);
Willy Tarreauf1221aa2006-12-17 22:14:12 +0100459 } /* end of while (p->feconn < p->maxconn) */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200460 return 0;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100461
462 /* Error unrolling */
463 out_fail_rep:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200464 pool_free2(pool2_buffer, s->req);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100465 out_fail_req:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200466 pool_free2(p->hdr_idx_pool, txn->hdr_idx.v);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100467 out_fail_idx:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200468 pool_free2(p->rsp_cap_pool, txn->rsp.cap);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100469 out_fail_rspcap:
Willy Tarreau48d63db2008-08-03 17:41:33 +0200470 pool_free2(p->req_cap_pool, txn->req.cap);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100471 out_fail_reqcap:
472 out_free_task:
473 pool_free2(pool2_task, t);
474 out_free_session:
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100475 LIST_DEL(&s->list);
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100476 pool_free2(pool2_session, s);
477 out_close:
478 close(cfd);
479 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200480}
481
482
483
Willy Tarreau8797c062007-05-07 00:55:35 +0200484/************************************************************************/
485/* All supported keywords must be declared here. */
486/************************************************************************/
487
488/* set test->ptr to point to the source IPv4/IPv6 address and test->i to the family */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200489static int
Willy Tarreau97be1452007-06-10 11:47:14 +0200490acl_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir,
491 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200492{
493 test->i = l4->cli_addr.ss_family;
494 if (test->i == AF_INET)
495 test->ptr = (void *)&((struct sockaddr_in *)&l4->cli_addr)->sin_addr;
496 else
497 test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_addr;
498 test->flags = ACL_TEST_F_READ_ONLY;
499 return 1;
500}
501
502
503/* set test->i to the connexion's source port */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200504static int
Willy Tarreau97be1452007-06-10 11:47:14 +0200505acl_fetch_sport(struct proxy *px, struct session *l4, void *l7, int dir,
506 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200507{
508 if (l4->cli_addr.ss_family == AF_INET)
509 test->i = ntohs(((struct sockaddr_in *)&l4->cli_addr)->sin_port);
510 else
511 test->i = ntohs(((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_port);
512 test->flags = 0;
513 return 1;
514}
515
Willy Tarreau662b2d82007-05-08 19:56:15 +0200516
517/* 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 +0200518static int
Willy Tarreau97be1452007-06-10 11:47:14 +0200519acl_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir,
520 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200521{
522 if (!(l4->flags & SN_FRT_ADDR_SET))
523 get_frt_addr(l4);
524
525 test->i = l4->frt_addr.ss_family;
526 if (test->i == AF_INET)
527 test->ptr = (void *)&((struct sockaddr_in *)&l4->frt_addr)->sin_addr;
528 else
529 test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_addr;
530 test->flags = ACL_TEST_F_READ_ONLY;
531 return 1;
532}
533
534
535/* set test->i to the frontend connexion's destination port */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200536static int
Willy Tarreau97be1452007-06-10 11:47:14 +0200537acl_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir,
538 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200539{
540 if (!(l4->flags & SN_FRT_ADDR_SET))
541 get_frt_addr(l4);
542
543 if (l4->frt_addr.ss_family == AF_INET)
544 test->i = ntohs(((struct sockaddr_in *)&l4->frt_addr)->sin_port);
545 else
546 test->i = ntohs(((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_port);
547 test->flags = 0;
548 return 1;
549}
550
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100551
Willy Tarreau8797c062007-05-07 00:55:35 +0200552/* set test->i to the number of connexions to the proxy */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200553static int
Willy Tarreau97be1452007-06-10 11:47:14 +0200554acl_fetch_dconn(struct proxy *px, struct session *l4, void *l7, int dir,
555 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200556{
557 test->i = px->feconn;
558 return 1;
559}
560
561
562/* Note: must not be declared <const> as its list will be overwritten */
563static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau0ceba5a2008-07-25 19:31:03 +0200564 { "src_port", acl_parse_int, acl_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT },
565 { "src", acl_parse_ip, acl_fetch_src, acl_match_ip, ACL_USE_TCP4_PERMANENT },
566 { "dst", acl_parse_ip, acl_fetch_dst, acl_match_ip, ACL_USE_TCP4_PERMANENT },
567 { "dst_port", acl_parse_int, acl_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT },
Willy Tarreau662b2d82007-05-08 19:56:15 +0200568#if 0
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100569 { "src_limit", acl_parse_int, acl_fetch_sconn, acl_match_int },
Willy Tarreau8797c062007-05-07 00:55:35 +0200570#endif
Willy Tarreau0ceba5a2008-07-25 19:31:03 +0200571 { "dst_conn", acl_parse_int, acl_fetch_dconn, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8797c062007-05-07 00:55:35 +0200572 { NULL, NULL, NULL, NULL },
573}};
574
575
576__attribute__((constructor))
577static void __client_init(void)
578{
579 acl_register_keywords(&acl_kws);
580}
581
582
Willy Tarreaubaaee002006-06-26 02:48:02 +0200583/*
584 * Local variables:
585 * c-indent-level: 8
586 * c-basic-offset: 8
587 * End:
588 */