blob: d42e8aaddce1eff4431dd5b37d8069edafbd70a0 [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>
17
18#include <sys/socket.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21
22#include <haproxy/compat.h>
23#include <haproxy/time.h>
24
25#include <types/backend.h>
26#include <types/buffers.h>
27#include <types/global.h>
28#include <types/httperr.h>
29#include <types/polling.h>
30#include <types/proxy.h>
31#include <types/server.h>
32#include <types/session.h>
33
34#include <proto/client.h>
35#include <proto/fd.h>
36#include <proto/log.h>
37#include <proto/proto_http.h>
38#include <proto/stream_sock.h>
39#include <proto/task.h>
40
41
42
43/*
44 * FIXME: This should move to the STREAM_SOCK code then split into TCP and HTTP.
45 */
46
47/*
48 * this function is called on a read event from a listen socket, corresponding
49 * to an accept. It tries to accept as many connections as possible.
50 * It returns 0.
51 */
52int event_accept(int fd) {
53 struct proxy *p = (struct proxy *)fdtab[fd].owner;
54 struct session *s;
55 struct task *t;
56 int cfd;
57 int max_accept;
58
59 if (global.nbproc > 1)
60 max_accept = 8; /* let other processes catch some connections too */
61 else
62 max_accept = -1;
63
64 while (p->nbconn < p->maxconn && max_accept--) {
65 struct sockaddr_storage addr;
66 socklen_t laddr = sizeof(addr);
67
68 if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) {
69 switch (errno) {
70 case EAGAIN:
71 case EINTR:
72 case ECONNABORTED:
73 return 0; /* nothing more to accept */
74 case ENFILE:
75 send_log(p, LOG_EMERG,
76 "Proxy %s reached system FD limit at %d. Please check system tunables.\n",
77 p->id, maxfd);
78 return 0;
79 case EMFILE:
80 send_log(p, LOG_EMERG,
81 "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n",
82 p->id, maxfd);
83 return 0;
84 case ENOBUFS:
85 case ENOMEM:
86 send_log(p, LOG_EMERG,
87 "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n",
88 p->id, maxfd);
89 return 0;
90 default:
91 return 0;
92 }
93 }
94
95 if ((s = pool_alloc(session)) == NULL) { /* disable this proxy for a while */
96 Alert("out of memory in event_accept().\n");
97 FD_CLR(fd, StaticReadEvent);
98 p->state = PR_STIDLE;
99 close(cfd);
100 return 0;
101 }
102
103 /* if this session comes from a known monitoring system, we want to ignore
104 * it as soon as possible, which means closing it immediately for TCP.
105 */
106 s->flags = 0;
107 if (addr.ss_family == AF_INET &&
108 p->mon_mask.s_addr &&
109 (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr) {
110 if (p->mode == PR_MODE_TCP) {
111 close(cfd);
112 pool_free(session, s);
113 continue;
114 }
115 s->flags |= SN_MONITOR;
116 }
117
118 if ((t = pool_alloc(task)) == NULL) { /* disable this proxy for a while */
119 Alert("out of memory in event_accept().\n");
120 FD_CLR(fd, StaticReadEvent);
121 p->state = PR_STIDLE;
122 close(cfd);
123 pool_free(session, s);
124 return 0;
125 }
126
127 s->cli_addr = addr;
128 if (cfd >= global.maxsock) {
129 Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n");
130 close(cfd);
131 pool_free(task, t);
132 pool_free(session, s);
133 return 0;
134 }
135
136 if ((fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) ||
137 (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY,
138 (char *) &one, sizeof(one)) == -1)) {
139 Alert("accept(): cannot set the socket in non blocking mode. Giving up\n");
140 close(cfd);
141 pool_free(task, t);
142 pool_free(session, s);
143 return 0;
144 }
145
146 if (p->options & PR_O_TCP_CLI_KA)
147 setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one));
148
149 t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */
150 t->wq = LIST_HEAD(wait_queue[0]); /* but already has a wait queue assigned */
151 t->state = TASK_IDLE;
152 t->process = process_session;
153 t->context = s;
154
155 s->task = t;
156 s->proxy = p;
157 s->cli_state = (p->mode == PR_MODE_HTTP) ? CL_STHEADERS : CL_STDATA; /* no HTTP headers for non-HTTP proxies */
158 s->srv_state = SV_STIDLE;
159 s->req = s->rep = NULL; /* will be allocated later */
160
161 s->res_cr = s->res_cw = s->res_sr = s->res_sw = RES_SILENT;
162 s->cli_fd = cfd;
163 s->srv_fd = -1;
164 s->req_line.len = -1;
165 s->auth_hdr.len = -1;
166 s->srv = NULL;
167 s->pend_pos = NULL;
168 s->conn_retries = p->conn_retries;
169
170 if (s->flags & SN_MONITOR)
171 s->logs.logwait = 0;
172 else
173 s->logs.logwait = p->to_log;
174
175 s->logs.tv_accept = now;
176 s->logs.t_request = -1;
177 s->logs.t_queue = -1;
178 s->logs.t_connect = -1;
179 s->logs.t_data = -1;
180 s->logs.t_close = 0;
181 s->logs.uri = NULL;
182 s->logs.cli_cookie = NULL;
183 s->logs.srv_cookie = NULL;
184 s->logs.status = -1;
185 s->logs.bytes = 0;
186 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
187 s->logs.srv_queue_size = 0; /* we will get this number soon */
188
189 s->data_source = DATA_SRC_NONE;
190
191 s->uniq_id = totalconn;
192 p->cum_conn++;
193
194 if (p->nb_req_cap > 0) {
195 if ((s->req_cap =
196 pool_alloc_from(p->req_cap_pool, p->nb_req_cap*sizeof(char *)))
197 == NULL) { /* no memory */
198 close(cfd); /* nothing can be done for this fd without memory */
199 pool_free(task, t);
200 pool_free(session, s);
201 return 0;
202 }
203 memset(s->req_cap, 0, p->nb_req_cap*sizeof(char *));
204 }
205 else
206 s->req_cap = NULL;
207
208 if (p->nb_rsp_cap > 0) {
209 if ((s->rsp_cap =
210 pool_alloc_from(p->rsp_cap_pool, p->nb_rsp_cap*sizeof(char *)))
211 == NULL) { /* no memory */
212 if (s->req_cap != NULL)
213 pool_free_to(p->req_cap_pool, s->req_cap);
214 close(cfd); /* nothing can be done for this fd without memory */
215 pool_free(task, t);
216 pool_free(session, s);
217 return 0;
218 }
219 memset(s->rsp_cap, 0, p->nb_rsp_cap*sizeof(char *));
220 }
221 else
222 s->rsp_cap = NULL;
223
224 if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP)
225 && (p->logfac1 >= 0 || p->logfac2 >= 0)) {
226 struct sockaddr_storage sockname;
227 socklen_t namelen = sizeof(sockname);
228
229 if (addr.ss_family != AF_INET ||
230 !(s->proxy->options & PR_O_TRANSP) ||
231 get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1)
232 getsockname(cfd, (struct sockaddr *)&sockname, &namelen);
233
234 if (p->to_log) {
235 /* we have the client ip */
236 if (s->logs.logwait & LW_CLIP)
237 if (!(s->logs.logwait &= ~LW_CLIP))
238 sess_log(s);
239 }
240 else if (s->cli_addr.ss_family == AF_INET) {
241 char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN];
242 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sockname)->sin_addr,
243 sn, sizeof(sn)) &&
244 inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
245 pn, sizeof(pn))) {
246 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
247 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port),
248 sn, ntohs(((struct sockaddr_in *)&sockname)->sin_port),
249 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
250 }
251 }
252 else {
253 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
254 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sockname)->sin6_addr,
255 sn, sizeof(sn)) &&
256 inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr,
257 pn, sizeof(pn))) {
258 send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
259 pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
260 sn, ntohs(((struct sockaddr_in6 *)&sockname)->sin6_port),
261 p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
262 }
263 }
264 }
265
266 if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) {
267 struct sockaddr_in sockname;
268 socklen_t namelen = sizeof(sockname);
269 int len;
270 if (addr.ss_family != AF_INET ||
271 !(s->proxy->options & PR_O_TRANSP) ||
272 get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1)
273 getsockname(cfd, (struct sockaddr *)&sockname, &namelen);
274
275 if (s->cli_addr.ss_family == AF_INET) {
276 char pn[INET_ADDRSTRLEN];
277 inet_ntop(AF_INET,
278 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
279 pn, sizeof(pn));
280
281 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
282 s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd,
283 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port));
284 }
285 else {
286 char pn[INET6_ADDRSTRLEN];
287 inet_ntop(AF_INET6,
288 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
289 pn, sizeof(pn));
290
291 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
292 s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd,
293 pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port));
294 }
295
296 write(1, trash, len);
297 }
298
299 if ((s->req = pool_alloc(buffer)) == NULL) { /* no memory */
300 if (s->rsp_cap != NULL)
301 pool_free_to(p->rsp_cap_pool, s->rsp_cap);
302 if (s->req_cap != NULL)
303 pool_free_to(p->req_cap_pool, s->req_cap);
304 close(cfd); /* nothing can be done for this fd without memory */
305 pool_free(task, t);
306 pool_free(session, s);
307 return 0;
308 }
309
310 s->req->l = 0;
311 s->req->total = 0;
312 s->req->h = s->req->r = s->req->lr = s->req->w = s->req->data; /* r and w will be reset further */
313 s->req->rlim = s->req->data + BUFSIZE;
314 if (s->cli_state == CL_STHEADERS) /* reserve some space for header rewriting */
315 s->req->rlim -= MAXREWRITE;
316
317 if ((s->rep = pool_alloc(buffer)) == NULL) { /* no memory */
318 pool_free(buffer, s->req);
319 if (s->rsp_cap != NULL)
320 pool_free_to(p->rsp_cap_pool, s->rsp_cap);
321 if (s->req_cap != NULL)
322 pool_free_to(p->req_cap_pool, s->req_cap);
323 close(cfd); /* nothing can be done for this fd without memory */
324 pool_free(task, t);
325 pool_free(session, s);
326 return 0;
327 }
328 s->rep->l = 0;
329 s->rep->total = 0;
330 s->rep->h = s->rep->r = s->rep->lr = s->rep->w = s->rep->rlim = s->rep->data;
331
332 fdtab[cfd].read = &event_cli_read;
333 fdtab[cfd].write = &event_cli_write;
334 fdtab[cfd].owner = t;
335 fdtab[cfd].state = FD_STREADY;
336
337 if ((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||
338 (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK)))
339 /* Either we got a request from a monitoring system on an HTTP instance,
340 * or we're in health check mode with the 'httpchk' option enabled. In
341 * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
342 */
343 client_retnclose(s, 19, "HTTP/1.0 200 OK\r\n\r\n"); /* forge a 200 response */
344 else if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */
345 client_retnclose(s, 3, "OK\n"); /* forge an "OK" response */
346 }
347 else {
348 FD_SET(cfd, StaticReadEvent);
349 }
350
351#if defined(DEBUG_FULL) && defined(ENABLE_EPOLL)
352 if (PrevReadEvent) {
353 assert(!(FD_ISSET(cfd, PrevReadEvent)));
354 assert(!(FD_ISSET(cfd, PrevWriteEvent)));
355 }
356#endif
357 fd_insert(cfd);
358
359 tv_eternity(&s->cnexpire);
360 tv_eternity(&s->srexpire);
361 tv_eternity(&s->swexpire);
362 tv_eternity(&s->crexpire);
363 tv_eternity(&s->cwexpire);
364
365 if (s->proxy->clitimeout) {
366 if (FD_ISSET(cfd, StaticReadEvent))
367 tv_delayfrom(&s->crexpire, &now, s->proxy->clitimeout);
368 if (FD_ISSET(cfd, StaticWriteEvent))
369 tv_delayfrom(&s->cwexpire, &now, s->proxy->clitimeout);
370 }
371
372 tv_min(&t->expire, &s->crexpire, &s->cwexpire);
373
374 task_queue(t);
375
376 if (p->mode != PR_MODE_HEALTH)
377 task_wakeup(&rq, t);
378
379 p->nbconn++;
380 if (p->nbconn > p->nbconn_max)
381 p->nbconn_max = p->nbconn;
382 actconn++;
383 totalconn++;
384
385 // fprintf(stderr, "accepting from %p => %d conn, %d total, task=%p\n", p, actconn, totalconn, t);
386 } /* end of while (p->nbconn < p->maxconn) */
387 return 0;
388}
389
390
391
392/*
393 * Local variables:
394 * c-indent-level: 8
395 * c-basic-offset: 8
396 * End:
397 */