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