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