Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 17 | #include <string.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 23 | #include <common/compat.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 24 | #include <common/config.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 25 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | |
| 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 Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 36 | #include <proto/buffers.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | #include <proto/client.h> |
| 38 | #include <proto/fd.h> |
| 39 | #include <proto/log.h> |
Willy Tarreau | e5f20dc | 2006-12-03 15:21:35 +0100 | [diff] [blame] | 40 | #include <proto/hdr_idx.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | #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 | */ |
| 56 | int event_accept(int fd) { |
| 57 | struct proxy *p = (struct proxy *)fdtab[fd].owner; |
| 58 | struct session *s; |
Willy Tarreau | c2168d3 | 2007-03-03 20:51:44 +0100 | [diff] [blame] | 59 | struct http_txn *txn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | struct task *t; |
| 61 | int cfd; |
| 62 | int max_accept; |
| 63 | |
| 64 | if (global.nbproc > 1) |
| 65 | max_accept = 8; /* let other processes catch some connections too */ |
| 66 | else |
| 67 | max_accept = -1; |
| 68 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 69 | while (p->feconn < p->maxconn && max_accept--) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | struct sockaddr_storage addr; |
| 71 | socklen_t laddr = sizeof(addr); |
| 72 | |
| 73 | if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) { |
| 74 | switch (errno) { |
| 75 | case EAGAIN: |
| 76 | case EINTR: |
| 77 | case ECONNABORTED: |
| 78 | return 0; /* nothing more to accept */ |
| 79 | case ENFILE: |
| 80 | send_log(p, LOG_EMERG, |
| 81 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 82 | p->id, maxfd); |
| 83 | return 0; |
| 84 | case EMFILE: |
| 85 | send_log(p, LOG_EMERG, |
| 86 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 87 | p->id, maxfd); |
| 88 | return 0; |
| 89 | case ENOBUFS: |
| 90 | case ENOMEM: |
| 91 | send_log(p, LOG_EMERG, |
| 92 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 93 | p->id, maxfd); |
| 94 | return 0; |
| 95 | default: |
| 96 | return 0; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if ((s = pool_alloc(session)) == NULL) { /* disable this proxy for a while */ |
| 101 | Alert("out of memory in event_accept().\n"); |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 102 | EV_FD_CLR(fd, DIR_RD); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 103 | p->state = PR_STIDLE; |
| 104 | close(cfd); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | /* if this session comes from a known monitoring system, we want to ignore |
| 109 | * it as soon as possible, which means closing it immediately for TCP. |
| 110 | */ |
| 111 | s->flags = 0; |
| 112 | if (addr.ss_family == AF_INET && |
| 113 | p->mon_mask.s_addr && |
| 114 | (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr) { |
| 115 | if (p->mode == PR_MODE_TCP) { |
| 116 | close(cfd); |
| 117 | pool_free(session, s); |
| 118 | continue; |
| 119 | } |
| 120 | s->flags |= SN_MONITOR; |
| 121 | } |
| 122 | |
| 123 | if ((t = pool_alloc(task)) == NULL) { /* disable this proxy for a while */ |
| 124 | Alert("out of memory in event_accept().\n"); |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 125 | EV_FD_CLR(fd, DIR_RD); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 126 | p->state = PR_STIDLE; |
| 127 | close(cfd); |
| 128 | pool_free(session, s); |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | s->cli_addr = addr; |
| 133 | if (cfd >= global.maxsock) { |
| 134 | Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 135 | close(cfd); |
| 136 | pool_free(task, t); |
| 137 | pool_free(session, s); |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | if ((fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) || |
| 142 | (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, |
| 143 | (char *) &one, sizeof(one)) == -1)) { |
| 144 | Alert("accept(): cannot set the socket in non blocking mode. Giving up\n"); |
| 145 | close(cfd); |
| 146 | pool_free(task, t); |
| 147 | pool_free(session, s); |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | if (p->options & PR_O_TCP_CLI_KA) |
| 152 | setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 153 | |
Willy Tarreau | 964c936 | 2007-01-07 00:38:00 +0100 | [diff] [blame] | 154 | t->wq = NULL; |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 155 | t->qlist.p = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 156 | t->state = TASK_IDLE; |
| 157 | t->process = process_session; |
| 158 | t->context = s; |
| 159 | |
| 160 | s->task = t; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 161 | s->be = s->fe = p; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 162 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 163 | s->cli_state = (p->mode == PR_MODE_HTTP) ? CL_STHEADERS : CL_STDATA; /* no HTTP headers for non-HTTP proxies */ |
| 164 | s->srv_state = SV_STIDLE; |
| 165 | s->req = s->rep = NULL; /* will be allocated later */ |
| 166 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 167 | s->cli_fd = cfd; |
| 168 | s->srv_fd = -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 169 | s->srv = NULL; |
| 170 | s->pend_pos = NULL; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 171 | s->conn_retries = p->conn_retries; |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 172 | |
| 173 | /* FIXME: the logs are horribly complicated now, because they are |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 174 | * defined in <p>, <p>, and later <be> and <be>. |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 175 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 176 | |
| 177 | if (s->flags & SN_MONITOR) |
| 178 | s->logs.logwait = 0; |
| 179 | else |
| 180 | s->logs.logwait = p->to_log; |
| 181 | |
| 182 | s->logs.tv_accept = now; |
| 183 | s->logs.t_request = -1; |
| 184 | s->logs.t_queue = -1; |
| 185 | s->logs.t_connect = -1; |
| 186 | s->logs.t_data = -1; |
| 187 | s->logs.t_close = 0; |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 188 | s->logs.bytes_in = s->logs.bytes_out = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 189 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 190 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 191 | |
| 192 | s->data_source = DATA_SRC_NONE; |
| 193 | |
| 194 | s->uniq_id = totalconn; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 195 | p->cum_feconn++; /* cum_beconn will be increased once assigned */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 196 | |
Willy Tarreau | c2168d3 | 2007-03-03 20:51:44 +0100 | [diff] [blame] | 197 | txn = &s->txn; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 198 | txn->flags = 0; |
Willy Tarreau | 042cc79 | 2007-03-19 16:20:06 +0100 | [diff] [blame] | 199 | /* Those variables will be checked and freed if non-NULL in |
| 200 | * session.c:session_free(). It is important that they are |
| 201 | * properly initialized. |
| 202 | */ |
| 203 | txn->srv_cookie = NULL; |
| 204 | txn->cli_cookie = NULL; |
| 205 | txn->uri = NULL; |
Willy Tarreau | c2168d3 | 2007-03-03 20:51:44 +0100 | [diff] [blame] | 206 | txn->req.cap = NULL; |
| 207 | txn->rsp.cap = NULL; |
| 208 | txn->hdr_idx.v = NULL; |
| 209 | txn->hdr_idx.size = txn->hdr_idx.used = 0; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 210 | |
| 211 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 212 | txn->status = -1; |
| 213 | |
Willy Tarreau | c2168d3 | 2007-03-03 20:51:44 +0100 | [diff] [blame] | 214 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 215 | txn->req.sol = txn->req.eol = NULL; |
| 216 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
| 217 | txn->auth_hdr.len = -1; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 218 | |
Willy Tarreau | c2168d3 | 2007-03-03 20:51:44 +0100 | [diff] [blame] | 219 | txn->hdr_idx.size = MAX_HTTP_HDR; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 220 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 221 | if (p->nb_req_cap > 0) { |
Willy Tarreau | c2168d3 | 2007-03-03 20:51:44 +0100 | [diff] [blame] | 222 | if ((txn->req.cap = |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 223 | pool_alloc_from(p->req_cap_pool, p->nb_req_cap*sizeof(char *))) |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 224 | == NULL) { /* no memory */ |
| 225 | close(cfd); /* nothing can be done for this fd without memory */ |
| 226 | pool_free(task, t); |
| 227 | pool_free(session, s); |
| 228 | return 0; |
| 229 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 230 | memset(txn->req.cap, 0, p->nb_req_cap*sizeof(char *)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 231 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 232 | |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 233 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 234 | if (p->nb_rsp_cap > 0) { |
Willy Tarreau | c2168d3 | 2007-03-03 20:51:44 +0100 | [diff] [blame] | 235 | if ((txn->rsp.cap = |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 236 | pool_alloc_from(p->rsp_cap_pool, p->nb_rsp_cap*sizeof(char *))) |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 237 | == NULL) { /* no memory */ |
Willy Tarreau | c2168d3 | 2007-03-03 20:51:44 +0100 | [diff] [blame] | 238 | if (txn->req.cap != NULL) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 239 | pool_free_to(p->req_cap_pool, txn->req.cap); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 240 | close(cfd); /* nothing can be done for this fd without memory */ |
| 241 | pool_free(task, t); |
| 242 | pool_free(session, s); |
| 243 | return 0; |
| 244 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 245 | memset(txn->rsp.cap, 0, p->nb_rsp_cap*sizeof(char *)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 246 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 247 | |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 248 | |
Willy Tarreau | c2168d3 | 2007-03-03 20:51:44 +0100 | [diff] [blame] | 249 | if ((txn->hdr_idx.v = |
| 250 | pool_alloc_from(p->hdr_idx_pool, txn->hdr_idx.size*sizeof(*txn->hdr_idx.v))) |
Willy Tarreau | e5f20dc | 2006-12-03 15:21:35 +0100 | [diff] [blame] | 251 | == NULL) { /* no memory */ |
Willy Tarreau | c2168d3 | 2007-03-03 20:51:44 +0100 | [diff] [blame] | 252 | if (txn->rsp.cap != NULL) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 253 | pool_free_to(p->rsp_cap_pool, txn->rsp.cap); |
Willy Tarreau | c2168d3 | 2007-03-03 20:51:44 +0100 | [diff] [blame] | 254 | if (txn->req.cap != NULL) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 255 | pool_free_to(p->req_cap_pool, txn->req.cap); |
Willy Tarreau | e5f20dc | 2006-12-03 15:21:35 +0100 | [diff] [blame] | 256 | close(cfd); /* nothing can be done for this fd without memory */ |
| 257 | pool_free(task, t); |
| 258 | pool_free(session, s); |
| 259 | return 0; |
| 260 | } |
Willy Tarreau | c2168d3 | 2007-03-03 20:51:44 +0100 | [diff] [blame] | 261 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | e5f20dc | 2006-12-03 15:21:35 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 264 | if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP) |
| 265 | && (p->logfac1 >= 0 || p->logfac2 >= 0)) { |
| 266 | struct sockaddr_storage sockname; |
| 267 | socklen_t namelen = sizeof(sockname); |
| 268 | |
| 269 | if (addr.ss_family != AF_INET || |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 270 | !(s->fe->options & PR_O_TRANSP) || |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 271 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
| 272 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
| 273 | |
| 274 | if (p->to_log) { |
| 275 | /* we have the client ip */ |
| 276 | if (s->logs.logwait & LW_CLIP) |
| 277 | if (!(s->logs.logwait &= ~LW_CLIP)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 278 | tcp_sess_log(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 279 | } |
| 280 | else if (s->cli_addr.ss_family == AF_INET) { |
| 281 | char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN]; |
| 282 | if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sockname)->sin_addr, |
| 283 | sn, sizeof(sn)) && |
| 284 | inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 285 | pn, sizeof(pn))) { |
| 286 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 287 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port), |
| 288 | sn, ntohs(((struct sockaddr_in *)&sockname)->sin_port), |
| 289 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 290 | } |
| 291 | } |
| 292 | else { |
| 293 | char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN]; |
| 294 | if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sockname)->sin6_addr, |
| 295 | sn, sizeof(sn)) && |
| 296 | inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr, |
| 297 | pn, sizeof(pn))) { |
| 298 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 299 | pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 300 | sn, ntohs(((struct sockaddr_in6 *)&sockname)->sin6_port), |
| 301 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 307 | struct sockaddr_in sockname; |
| 308 | socklen_t namelen = sizeof(sockname); |
| 309 | int len; |
| 310 | if (addr.ss_family != AF_INET || |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 311 | !(s->fe->options & PR_O_TRANSP) || |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 312 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
| 313 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
| 314 | |
| 315 | if (s->cli_addr.ss_family == AF_INET) { |
| 316 | char pn[INET_ADDRSTRLEN]; |
| 317 | inet_ntop(AF_INET, |
| 318 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 319 | pn, sizeof(pn)); |
| 320 | |
| 321 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 322 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 323 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port)); |
| 324 | } |
| 325 | else { |
| 326 | char pn[INET6_ADDRSTRLEN]; |
| 327 | inet_ntop(AF_INET6, |
| 328 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 329 | pn, sizeof(pn)); |
| 330 | |
| 331 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 332 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 333 | pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port)); |
| 334 | } |
| 335 | |
| 336 | write(1, trash, len); |
| 337 | } |
| 338 | |
| 339 | if ((s->req = pool_alloc(buffer)) == NULL) { /* no memory */ |
Willy Tarreau | c2168d3 | 2007-03-03 20:51:44 +0100 | [diff] [blame] | 340 | if (txn->hdr_idx.v != NULL) |
| 341 | pool_free_to(p->hdr_idx_pool, txn->hdr_idx.v); |
| 342 | if (txn->rsp.cap != NULL) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 343 | pool_free_to(p->rsp_cap_pool, txn->rsp.cap); |
Willy Tarreau | c2168d3 | 2007-03-03 20:51:44 +0100 | [diff] [blame] | 344 | if (txn->req.cap != NULL) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 345 | pool_free_to(p->req_cap_pool, txn->req.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 346 | close(cfd); /* nothing can be done for this fd without memory */ |
| 347 | pool_free(task, t); |
| 348 | pool_free(session, s); |
| 349 | return 0; |
| 350 | } |
| 351 | |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 352 | buffer_init(s->req); |
| 353 | s->req->rlim += BUFSIZE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 354 | if (s->cli_state == CL_STHEADERS) /* reserve some space for header rewriting */ |
| 355 | s->req->rlim -= MAXREWRITE; |
| 356 | |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 357 | s->req->rto = s->fe->clitimeout; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 358 | s->req->wto = s->be->srvtimeout; |
| 359 | s->req->cto = s->be->srvtimeout; |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 360 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 361 | if ((s->rep = pool_alloc(buffer)) == NULL) { /* no memory */ |
| 362 | pool_free(buffer, s->req); |
Willy Tarreau | c2168d3 | 2007-03-03 20:51:44 +0100 | [diff] [blame] | 363 | if (txn->hdr_idx.v != NULL) |
| 364 | pool_free_to(p->hdr_idx_pool, txn->hdr_idx.v); |
| 365 | if (txn->rsp.cap != NULL) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 366 | pool_free_to(p->rsp_cap_pool, txn->rsp.cap); |
Willy Tarreau | c2168d3 | 2007-03-03 20:51:44 +0100 | [diff] [blame] | 367 | if (txn->req.cap != NULL) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 368 | pool_free_to(p->req_cap_pool, txn->req.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 369 | close(cfd); /* nothing can be done for this fd without memory */ |
| 370 | pool_free(task, t); |
| 371 | pool_free(session, s); |
| 372 | return 0; |
| 373 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 374 | |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 375 | buffer_init(s->rep); |
| 376 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 377 | s->rep->rto = s->be->srvtimeout; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 378 | s->rep->wto = s->fe->clitimeout; |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 379 | s->rep->cto = 0; |
| 380 | |
Willy Tarreau | 7a96648 | 2007-04-15 10:58:02 +0200 | [diff] [blame] | 381 | fd_insert(cfd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 382 | fdtab[cfd].owner = t; |
| 383 | fdtab[cfd].state = FD_STREADY; |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 384 | fdtab[cfd].cb[DIR_RD].f = &stream_sock_read; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 385 | fdtab[cfd].cb[DIR_RD].b = s->req; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 386 | fdtab[cfd].cb[DIR_WR].f = &stream_sock_write; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 387 | fdtab[cfd].cb[DIR_WR].b = s->rep; |
Willy Tarreau | 3d32d3a | 2007-04-15 11:31:05 +0200 | [diff] [blame] | 388 | fdtab[cfd].ev = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 389 | |
| 390 | if ((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) || |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 391 | (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 392 | /* Either we got a request from a monitoring system on an HTTP instance, |
| 393 | * or we're in health check mode with the 'httpchk' option enabled. In |
| 394 | * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit. |
| 395 | */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 396 | struct chunk msg = { .str = "HTTP/1.0 200 OK\r\n\r\n", .len = 19 }; |
| 397 | client_retnclose(s, &msg); /* forge a 200 response */ |
| 398 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 399 | else if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 400 | struct chunk msg = { .str = "OK\n", .len = 3 }; |
| 401 | client_retnclose(s, &msg); /* forge an "OK" response */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 402 | } |
| 403 | else { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 404 | EV_FD_SET(cfd, DIR_RD); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 405 | } |
| 406 | |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 407 | tv_eternity(&s->req->rex); |
| 408 | tv_eternity(&s->req->wex); |
| 409 | tv_eternity(&s->req->cex); |
| 410 | tv_eternity(&s->rep->rex); |
| 411 | tv_eternity(&s->rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 412 | |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 413 | if (s->fe->clitimeout) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 414 | if (EV_FD_ISSET(cfd, DIR_RD)) |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 415 | tv_delayfrom(&s->req->rex, &now, s->fe->clitimeout); |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 416 | if (EV_FD_ISSET(cfd, DIR_WR)) |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 417 | tv_delayfrom(&s->rep->wex, &now, s->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 418 | } |
| 419 | |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 420 | tv_min(&t->expire, &s->req->rex, &s->rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 421 | |
| 422 | task_queue(t); |
| 423 | |
| 424 | if (p->mode != PR_MODE_HEALTH) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 425 | task_wakeup(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 426 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 427 | p->feconn++; /* beconn will be increased later */ |
| 428 | if (p->feconn > p->feconn_max) |
| 429 | p->feconn_max = p->feconn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 430 | actconn++; |
| 431 | totalconn++; |
| 432 | |
| 433 | // fprintf(stderr, "accepting from %p => %d conn, %d total, task=%p\n", p, actconn, totalconn, t); |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 434 | } /* end of while (p->feconn < p->maxconn) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | |
| 439 | |
| 440 | /* |
| 441 | * Local variables: |
| 442 | * c-indent-level: 8 |
| 443 | * c-basic-offset: 8 |
| 444 | * End: |
| 445 | */ |