blob: 0ce2392e377b13a188b01680ca1f75eb1e2efb54 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Client-side variables and functions.
3 *
4 * Copyright 2000-2006 Willy Tarreau <w@1wt.eu>
5 *
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
27#include <types/backend.h>
28#include <types/buffers.h>
29#include <types/global.h>
30#include <types/httperr.h>
31#include <types/polling.h>
32#include <types/proxy.h>
33#include <types/server.h>
34#include <types/session.h>
35
Willy Tarreau54469402006-07-29 16:59:06 +020036#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037#include <proto/client.h>
38#include <proto/fd.h>
39#include <proto/log.h>
Willy Tarreaue5f20dc2006-12-03 15:21:35 +010040#include <proto/hdr_idx.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041#include <proto/proto_http.h>
42#include <proto/stream_sock.h>
43#include <proto/task.h>
44
45
46
47/*
48 * FIXME: This should move to the STREAM_SOCK code then split into TCP and HTTP.
49 */
50
51/*
52 * this function is called on a read event from a listen socket, corresponding
53 * to an accept. It tries to accept as many connections as possible.
54 * It returns 0.
55 */
56int event_accept(int fd) {
57 struct proxy *p = (struct proxy *)fdtab[fd].owner;
58 struct session *s;
59 struct task *t;
60 int cfd;
61 int max_accept;
62
63 if (global.nbproc > 1)
64 max_accept = 8; /* let other processes catch some connections too */
65 else
66 max_accept = -1;
67
68 while (p->nbconn < p->maxconn && max_accept--) {
69 struct sockaddr_storage addr;
70 socklen_t laddr = sizeof(addr);
71
72 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) {
73 switch (errno) {
74 case EAGAIN:
75 case EINTR:
76 case ECONNABORTED:
77 return 0; /* nothing more to accept */
78 case ENFILE:
79 send_log(p, LOG_EMERG,
80 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
81 p->id, maxfd);
82 return 0;
83 case EMFILE:
84 send_log(p, LOG_EMERG,
85 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
86 p->id, maxfd);
87 return 0;
88 case ENOBUFS:
89 case ENOMEM:
90 send_log(p, LOG_EMERG,
91 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
92 p->id, maxfd);
93 return 0;
94 default:
95 return 0;
96 }
97 }
98
99 if ((s = pool_alloc(session)) == NULL) { /* disable this proxy for a while */
100 Alert("out of memory in event_accept().\n");
Willy Tarreau2a429502006-10-15 14:52:29 +0200101 MY_FD_CLR(fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102 p->state = PR_STIDLE;
103 close(cfd);
104 return 0;
105 }
106
107 /* if this session comes from a known monitoring system, we want to ignore
108 * it as soon as possible, which means closing it immediately for TCP.
109 */
110 s->flags = 0;
111 if (addr.ss_family == AF_INET &&
112 p->mon_mask.s_addr &&
113 (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr) {
114 if (p->mode == PR_MODE_TCP) {
115 close(cfd);
116 pool_free(session, s);
117 continue;
118 }
119 s->flags |= SN_MONITOR;
120 }
121
122 if ((t = pool_alloc(task)) == NULL) { /* disable this proxy for a while */
123 Alert("out of memory in event_accept().\n");
Willy Tarreau2a429502006-10-15 14:52:29 +0200124 MY_FD_CLR(fd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200125 p->state = PR_STIDLE;
126 close(cfd);
127 pool_free(session, s);
128 return 0;
129 }
130
131 s->cli_addr = addr;
132 if (cfd >= global.maxsock) {
133 Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n");
134 close(cfd);
135 pool_free(task, t);
136 pool_free(session, s);
137 return 0;
138 }
139
140 if ((fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) ||
141 (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY,
142 (char *) &one, sizeof(one)) == -1)) {
143 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
144 close(cfd);
145 pool_free(task, t);
146 pool_free(session, s);
147 return 0;
148 }
149
150 if (p->options & PR_O_TCP_CLI_KA)
151 setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
152
153 t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */
154 t->wq = LIST_HEAD(wait_queue[0]); /* but already has a wait queue assigned */
155 t->state = TASK_IDLE;
156 t->process = process_session;
157 t->context = s;
158
159 s->task = t;
Willy Tarreau73de9892006-11-30 11:40:23 +0100160 s->be = s->fe = s->fi = p;
161
Willy Tarreaubaaee002006-06-26 02:48:02 +0200162 s->cli_state = (p->mode == PR_MODE_HTTP) ? CL_STHEADERS : CL_STDATA; /* no HTTP headers for non-HTTP proxies */
163 s->srv_state = SV_STIDLE;
Willy Tarreau58f10d72006-12-04 02:26:12 +0100164 s->hdr_state = HTTP_PA_EMPTY; /* at the very beginning of the request */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200165 s->req = s->rep = NULL; /* will be allocated later */
166
Willy Tarreaubaaee002006-06-26 02:48:02 +0200167 s->cli_fd = cfd;
168 s->srv_fd = -1;
169 s->req_line.len = -1;
170 s->auth_hdr.len = -1;
171 s->srv = NULL;
172 s->pend_pos = NULL;
173 s->conn_retries = p->conn_retries;
174
175 if (s->flags & SN_MONITOR)
176 s->logs.logwait = 0;
177 else
178 s->logs.logwait = p->to_log;
179
180 s->logs.tv_accept = now;
181 s->logs.t_request = -1;
182 s->logs.t_queue = -1;
183 s->logs.t_connect = -1;
184 s->logs.t_data = -1;
185 s->logs.t_close = 0;
186 s->logs.uri = NULL;
187 s->logs.cli_cookie = NULL;
188 s->logs.srv_cookie = NULL;
189 s->logs.status = -1;
190 s->logs.bytes = 0;
191 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
192 s->logs.srv_queue_size = 0; /* we will get this number soon */
193
194 s->data_source = DATA_SRC_NONE;
195
196 s->uniq_id = totalconn;
197 p->cum_conn++;
198
199 if (p->nb_req_cap > 0) {
200 if ((s->req_cap =
201 pool_alloc_from(p->req_cap_pool, p->nb_req_cap*sizeof(char *)))
202 == NULL) { /* no memory */
203 close(cfd); /* nothing can be done for this fd without memory */
204 pool_free(task, t);
205 pool_free(session, s);
206 return 0;
207 }
208 memset(s->req_cap, 0, p->nb_req_cap*sizeof(char *));
209 }
210 else
211 s->req_cap = NULL;
212
213 if (p->nb_rsp_cap > 0) {
214 if ((s->rsp_cap =
215 pool_alloc_from(p->rsp_cap_pool, p->nb_rsp_cap*sizeof(char *)))
216 == NULL) { /* no memory */
217 if (s->req_cap != NULL)
218 pool_free_to(p->req_cap_pool, s->req_cap);
219 close(cfd); /* nothing can be done for this fd without memory */
220 pool_free(task, t);
221 pool_free(session, s);
222 return 0;
223 }
224 memset(s->rsp_cap, 0, p->nb_rsp_cap*sizeof(char *));
225 }
226 else
227 s->rsp_cap = NULL;
228
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100229 if (p->mode == PR_MODE_HTTP) {
230 s->hdr_idx.size = MAX_HTTP_HDR;
231 if ((s->hdr_idx.v =
232 pool_alloc_from(p->hdr_idx_pool, s->hdr_idx.size*sizeof(*s->hdr_idx.v)))
233 == NULL) { /* no memory */
234 if (s->rsp_cap != NULL)
235 pool_free_to(p->rsp_cap_pool, s->rsp_cap);
236 if (s->req_cap != NULL)
237 pool_free_to(p->req_cap_pool, s->req_cap);
238 close(cfd); /* nothing can be done for this fd without memory */
239 pool_free(task, t);
240 pool_free(session, s);
241 return 0;
242 }
243 hdr_idx_init(&s->hdr_idx);
244 }
245 else {
246 s->hdr_idx.size = s->hdr_idx.used = 0;
247 s->hdr_idx.v = NULL;
248 }
249
Willy Tarreaubaaee002006-06-26 02:48:02 +0200250 if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP)
251 && (p->logfac1 >= 0 || p->logfac2 >= 0)) {
252 struct sockaddr_storage sockname;
253 socklen_t namelen = sizeof(sockname);
254
255 if (addr.ss_family != AF_INET ||
Willy Tarreau73de9892006-11-30 11:40:23 +0100256 !(s->fe->options & PR_O_TRANSP) ||
Willy Tarreaubaaee002006-06-26 02:48:02 +0200257 get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1)
258 getsockname(cfd, (struct sockaddr *)&sockname, &namelen);
259
260 if (p->to_log) {
261 /* we have the client ip */
262 if (s->logs.logwait & LW_CLIP)
263 if (!(s->logs.logwait &= ~LW_CLIP))
264 sess_log(s);
265 }
266 else if (s->cli_addr.ss_family == AF_INET) {
267 char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN];
268 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sockname)->sin_addr,
269 sn, sizeof(sn)) &&
270 inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
271 pn, sizeof(pn))) {
272 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
273 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port),
274 sn, ntohs(((struct sockaddr_in *)&sockname)->sin_port),
275 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
276 }
277 }
278 else {
279 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
280 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sockname)->sin6_addr,
281 sn, sizeof(sn)) &&
282 inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr,
283 pn, sizeof(pn))) {
284 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
285 pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
286 sn, ntohs(((struct sockaddr_in6 *)&sockname)->sin6_port),
287 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
288 }
289 }
290 }
291
292 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
293 struct sockaddr_in sockname;
294 socklen_t namelen = sizeof(sockname);
295 int len;
296 if (addr.ss_family != AF_INET ||
Willy Tarreau73de9892006-11-30 11:40:23 +0100297 !(s->fe->options & PR_O_TRANSP) ||
Willy Tarreaubaaee002006-06-26 02:48:02 +0200298 get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1)
299 getsockname(cfd, (struct sockaddr *)&sockname, &namelen);
300
301 if (s->cli_addr.ss_family == AF_INET) {
302 char pn[INET_ADDRSTRLEN];
303 inet_ntop(AF_INET,
304 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
305 pn, sizeof(pn));
306
307 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
308 s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd,
309 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port));
310 }
311 else {
312 char pn[INET6_ADDRSTRLEN];
313 inet_ntop(AF_INET6,
314 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
315 pn, sizeof(pn));
316
317 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
318 s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd,
319 pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port));
320 }
321
322 write(1, trash, len);
323 }
324
325 if ((s->req = pool_alloc(buffer)) == NULL) { /* no memory */
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100326 if (s->hdr_idx.v != NULL)
327 pool_free_to(p->hdr_idx_pool, s->hdr_idx.v);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328 if (s->rsp_cap != NULL)
329 pool_free_to(p->rsp_cap_pool, s->rsp_cap);
330 if (s->req_cap != NULL)
331 pool_free_to(p->req_cap_pool, s->req_cap);
332 close(cfd); /* nothing can be done for this fd without memory */
333 pool_free(task, t);
334 pool_free(session, s);
335 return 0;
336 }
337
Willy Tarreau54469402006-07-29 16:59:06 +0200338 buffer_init(s->req);
339 s->req->rlim += BUFSIZE;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200340 if (s->cli_state == CL_STHEADERS) /* reserve some space for header rewriting */
341 s->req->rlim -= MAXREWRITE;
342
Willy Tarreau73de9892006-11-30 11:40:23 +0100343 s->req->rto = s->fe->clitimeout;
344 s->req->wto = s->be->srvtimeout;
345 s->req->cto = s->be->srvtimeout;
Willy Tarreaud7971282006-07-29 18:36:34 +0200346
Willy Tarreaubaaee002006-06-26 02:48:02 +0200347 if ((s->rep = pool_alloc(buffer)) == NULL) { /* no memory */
348 pool_free(buffer, s->req);
349 if (s->rsp_cap != NULL)
350 pool_free_to(p->rsp_cap_pool, s->rsp_cap);
351 if (s->req_cap != NULL)
352 pool_free_to(p->req_cap_pool, s->req_cap);
353 close(cfd); /* nothing can be done for this fd without memory */
354 pool_free(task, t);
355 pool_free(session, s);
356 return 0;
357 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200358
Willy Tarreau54469402006-07-29 16:59:06 +0200359 buffer_init(s->rep);
360
Willy Tarreau73de9892006-11-30 11:40:23 +0100361 s->rep->rto = s->be->srvtimeout;
362 s->rep->wto = s->be->clitimeout;
Willy Tarreaud7971282006-07-29 18:36:34 +0200363 s->rep->cto = 0;
364
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365 fdtab[cfd].owner = t;
366 fdtab[cfd].state = FD_STREADY;
Willy Tarreaud7971282006-07-29 18:36:34 +0200367 fdtab[cfd].cb[DIR_RD].f = &stream_sock_read;
Willy Tarreau54469402006-07-29 16:59:06 +0200368 fdtab[cfd].cb[DIR_RD].b = s->req;
Willy Tarreauf8306d52006-07-29 19:01:31 +0200369 fdtab[cfd].cb[DIR_WR].f = &stream_sock_write;
Willy Tarreau54469402006-07-29 16:59:06 +0200370 fdtab[cfd].cb[DIR_WR].b = s->rep;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200371
372 if ((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||
373 (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK)))
374 /* Either we got a request from a monitoring system on an HTTP instance,
375 * or we're in health check mode with the 'httpchk' option enabled. In
376 * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
377 */
378 client_retnclose(s, 19, "HTTP/1.0 200 OK\r\n\r\n"); /* forge a 200 response */
379 else if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */
380 client_retnclose(s, 3, "OK\n"); /* forge an "OK" response */
381 }
382 else {
Willy Tarreau2a429502006-10-15 14:52:29 +0200383 MY_FD_SET(cfd, StaticReadEvent);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200384 }
385
386#if defined(DEBUG_FULL) && defined(ENABLE_EPOLL)
387 if (PrevReadEvent) {
Willy Tarreau2a429502006-10-15 14:52:29 +0200388 assert(!(MY_FD_ISSET(cfd, PrevReadEvent)));
389 assert(!(MY_FD_ISSET(cfd, PrevWriteEvent)));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200390 }
391#endif
392 fd_insert(cfd);
393
Willy Tarreaud7971282006-07-29 18:36:34 +0200394 tv_eternity(&s->req->rex);
395 tv_eternity(&s->req->wex);
396 tv_eternity(&s->req->cex);
397 tv_eternity(&s->rep->rex);
398 tv_eternity(&s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200399
Willy Tarreau73de9892006-11-30 11:40:23 +0100400 if (s->fe->clitimeout) {
Willy Tarreau2a429502006-10-15 14:52:29 +0200401 if (MY_FD_ISSET(cfd, StaticReadEvent))
Willy Tarreau73de9892006-11-30 11:40:23 +0100402 tv_delayfrom(&s->req->rex, &now, s->fe->clitimeout);
Willy Tarreau2a429502006-10-15 14:52:29 +0200403 if (MY_FD_ISSET(cfd, StaticWriteEvent))
Willy Tarreau73de9892006-11-30 11:40:23 +0100404 tv_delayfrom(&s->rep->wex, &now, s->fe->clitimeout);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200405 }
406
Willy Tarreaud7971282006-07-29 18:36:34 +0200407 tv_min(&t->expire, &s->req->rex, &s->rep->wex);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200408
409 task_queue(t);
410
411 if (p->mode != PR_MODE_HEALTH)
412 task_wakeup(&rq, t);
413
414 p->nbconn++;
415 if (p->nbconn > p->nbconn_max)
416 p->nbconn_max = p->nbconn;
417 actconn++;
418 totalconn++;
419
420 // fprintf(stderr, "accepting from %p => %d conn, %d total, task=%p\n", p, actconn, totalconn, t);
421 } /* end of while (p->nbconn < p->maxconn) */
422 return 0;
423}
424
425
426
427/*
428 * Local variables:
429 * c-indent-level: 8
430 * c-basic-offset: 8
431 * End:
432 */