blob: 9b2e92a5bd4d25bb93578ae393c5fa92548e557e [file] [log] [blame]
Willy Tarreau59f98392012-07-06 14:13:49 +02001/*
2 * Connection management functions
3 *
4 * Copyright 2000-2012 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
Willy Tarreaue1e4a612012-10-05 00:10:55 +020013#include <errno.h>
14
Willy Tarreau59f98392012-07-06 14:13:49 +020015#include <common/compat.h>
16#include <common/config.h>
17
Willy Tarreauc5788912012-08-24 18:12:41 +020018#include <proto/connection.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020019#include <proto/fd.h>
Willy Tarreau2da156f2012-07-23 15:07:23 +020020#include <proto/proto_tcp.h>
Willy Tarreau2542b532012-08-31 16:01:23 +020021#include <proto/session.h>
Willy Tarreau2c6be842012-07-06 17:12:34 +020022#include <proto/stream_interface.h>
Willy Tarreau59f98392012-07-06 14:13:49 +020023
Emeric Brun46591952012-05-18 15:47:34 +020024#ifdef USE_OPENSSL
25#include <proto/ssl_sock.h>
26#endif
27
Willy Tarreau59f98392012-07-06 14:13:49 +020028/* I/O callback for fd-based connections. It calls the read/write handlers
Willy Tarreauafad0e02012-08-09 14:45:22 +020029 * provided by the connection's sock_ops, which must be valid. It returns 0.
Willy Tarreau59f98392012-07-06 14:13:49 +020030 */
31int conn_fd_handler(int fd)
32{
Willy Tarreau80184712012-07-06 14:54:49 +020033 struct connection *conn = fdtab[fd].owner;
Willy Tarreau9e272bf2012-10-03 21:04:48 +020034 unsigned int flags;
Willy Tarreau59f98392012-07-06 14:13:49 +020035
Willy Tarreauc76ae332012-07-12 15:32:13 +020036 if (unlikely(!conn))
Willy Tarreau2542b532012-08-31 16:01:23 +020037 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +020038
Willy Tarreaue9dfa792012-09-01 17:26:16 +020039 /* before engaging there, we clear the new WAIT_* flags so that we can
40 * more easily detect an EAGAIN condition from anywhere.
41 */
Willy Tarreau9e272bf2012-10-03 21:04:48 +020042 flags = conn->flags &= ~(CO_FL_WAIT_DATA|CO_FL_WAIT_ROOM|CO_FL_WAIT_RD|CO_FL_WAIT_WR);
Willy Tarreaue9dfa792012-09-01 17:26:16 +020043
Willy Tarreauc76ae332012-07-12 15:32:13 +020044 process_handshake:
Willy Tarreauf9dabec2012-08-17 17:33:53 +020045 /* The handshake callbacks are called in sequence. If either of them is
46 * missing something, it must enable the required polling at the socket
47 * layer of the connection. Polling state is not guaranteed when entering
48 * these handlers, so any handshake handler which does not complete its
49 * work must explicitly disable events it's not interested in.
50 */
Willy Tarreauc76ae332012-07-12 15:32:13 +020051 while (unlikely(conn->flags & CO_FL_HANDSHAKE)) {
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020052 if (unlikely(conn->flags & (CO_FL_ERROR|CO_FL_WAIT_RD|CO_FL_WAIT_WR)))
Willy Tarreau2c6be842012-07-06 17:12:34 +020053 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020054
Willy Tarreau22cda212012-08-31 17:43:29 +020055 if (conn->flags & CO_FL_ACCEPT_PROXY)
56 if (!conn_recv_proxy(conn, CO_FL_ACCEPT_PROXY))
57 goto leave;
58
Willy Tarreauc76ae332012-07-12 15:32:13 +020059 if (conn->flags & CO_FL_SI_SEND_PROXY)
Willy Tarreauafad0e02012-08-09 14:45:22 +020060 if (!conn_si_send_proxy(conn, CO_FL_SI_SEND_PROXY))
Willy Tarreauc76ae332012-07-12 15:32:13 +020061 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +020062#ifdef USE_OPENSSL
63 if (conn->flags & CO_FL_SSL_WAIT_HS)
64 if (!ssl_sock_handshake(conn, CO_FL_SSL_WAIT_HS))
65 goto leave;
66#endif
Willy Tarreauc76ae332012-07-12 15:32:13 +020067 }
68
Willy Tarreauf9dabec2012-08-17 17:33:53 +020069 /* Once we're purely in the data phase, we disable handshake polling */
70 if (!(conn->flags & CO_FL_POLL_SOCK))
71 __conn_sock_stop_both(conn);
Willy Tarreauc76ae332012-07-12 15:32:13 +020072
Willy Tarreau071e1372012-10-03 01:39:48 +020073 /* The data layer might not be ready yet (eg: when using embryonic
74 * sessions). If we're about to move data, we must initialize it first.
75 * The function may fail and cause the connection to be destroyed, thus
Willy Tarreau2542b532012-08-31 16:01:23 +020076 * we must not use it anymore and should immediately leave instead.
77 */
Willy Tarreau071e1372012-10-03 01:39:48 +020078 if ((conn->flags & CO_FL_INIT_DATA) && conn->data->init(conn) < 0)
Willy Tarreau2542b532012-08-31 16:01:23 +020079 return 0;
80
Willy Tarreau58363cf2012-09-06 14:12:03 +020081 /* The data transfer starts here and stops on error and handshakes */
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020082 if ((fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) &&
Willy Tarreau9e272bf2012-10-03 21:04:48 +020083 !(conn->flags & (CO_FL_WAIT_RD|CO_FL_WAIT_ROOM|CO_FL_ERROR|CO_FL_HANDSHAKE))) {
84 /* force detection of a flag change : if any I/O succeeds, we're
85 * forced to have at least one of the CONN_* flags in conn->flags.
86 */
87 flags = 0;
Willy Tarreau74beec32012-10-03 00:41:04 +020088 conn->data->recv(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +020089 }
Willy Tarreau59f98392012-07-06 14:13:49 +020090
Willy Tarreaud9de7ca2012-09-02 18:48:46 +020091 if ((fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) &&
Willy Tarreau9e272bf2012-10-03 21:04:48 +020092 !(conn->flags & (CO_FL_WAIT_WR|CO_FL_WAIT_DATA|CO_FL_ERROR|CO_FL_HANDSHAKE))) {
93 /* force detection of a flag change : if any I/O succeeds, we're
94 * forced to have at least one of the CONN_* flags in conn->flags.
95 */
96 flags = 0;
Willy Tarreau74beec32012-10-03 00:41:04 +020097 conn->data->send(conn);
Willy Tarreau9e272bf2012-10-03 21:04:48 +020098 }
Willy Tarreau2da156f2012-07-23 15:07:23 +020099
Willy Tarreauc76ae332012-07-12 15:32:13 +0200100 if (unlikely(conn->flags & CO_FL_ERROR))
Willy Tarreau2da156f2012-07-23 15:07:23 +0200101 goto leave;
102
Willy Tarreauc76ae332012-07-12 15:32:13 +0200103 /* It may happen during the data phase that a handshake is
104 * enabled again (eg: SSL)
105 */
106 if (unlikely(conn->flags & CO_FL_HANDSHAKE))
107 goto process_handshake;
108
Willy Tarreauf8deb0c2012-09-01 17:59:22 +0200109 if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && !(conn->flags & CO_FL_WAIT_WR)) {
110 /* still waiting for a connection to establish and nothing was
111 * attempted yet to probe the connection. Then let's retry the
112 * connect().
Willy Tarreau2da156f2012-07-23 15:07:23 +0200113 */
Willy Tarreau239d7182012-07-23 18:53:03 +0200114 if (!tcp_connect_probe(conn))
Willy Tarreauafad0e02012-08-09 14:45:22 +0200115 goto leave;
Willy Tarreau2da156f2012-07-23 15:07:23 +0200116 }
117
Willy Tarreau2c6be842012-07-06 17:12:34 +0200118 leave:
Willy Tarreau2396c1c2012-10-03 21:12:16 +0200119 /* The wake callback may be used to process a critical error and abort the
120 * connection. If so, we don't want to go further as the connection will
121 * have been released and the FD destroyed.
122 */
123 if ((conn->flags & CO_FL_WAKE_DATA) &&
124 ((conn->flags ^ flags) & CO_FL_CONN_STATE) &&
125 conn->data->wake(conn) < 0)
126 return 0;
Willy Tarreaufd31e532012-07-23 18:24:25 +0200127
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200128 /* Last check, verify if the connection just established */
Willy Tarreauc76ae332012-07-12 15:32:13 +0200129 if (unlikely(!(conn->flags & (CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN | CO_FL_CONNECTED))))
Willy Tarreau8f8c92f2012-07-23 19:45:44 +0200130 conn->flags |= CO_FL_CONNECTED;
131
Willy Tarreau61ace1b2012-07-23 12:14:26 +0200132 /* remove the events before leaving */
133 fdtab[fd].ev &= ~(FD_POLL_IN | FD_POLL_OUT | FD_POLL_HUP | FD_POLL_ERR);
Willy Tarreauf9dabec2012-08-17 17:33:53 +0200134
135 /* commit polling changes */
136 conn_cond_update_polling(conn);
Willy Tarreauafad0e02012-08-09 14:45:22 +0200137 return 0;
Willy Tarreau59f98392012-07-06 14:13:49 +0200138}
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200139
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200140/* Update polling on connection <c>'s file descriptor depending on its current
141 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
142 * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*.
143 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200144 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200145 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200146void conn_update_data_polling(struct connection *c)
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200147{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200148 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200149
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200150 if (unlikely(f & CO_FL_ERROR)) {
151 c->flags &= ~(CO_FL_CURR_RD_ENA | CO_FL_CURR_WR_ENA |
152 CO_FL_SOCK_RD_ENA | CO_FL_SOCK_WR_ENA |
153 CO_FL_DATA_RD_ENA | CO_FL_DATA_WR_ENA);
154 fd_stop_both(c->t.sock.fd);
155 return;
156 }
157
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200158 /* update read status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200159 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
160 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
161 fd_stop_recv(c->t.sock.fd);
162 }
163 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
164 (f & (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_DATA_RD_ENA|CO_FL_WAIT_RD))) {
165 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200166 fd_poll_recv(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200167 }
168 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_DATA_RD_ENA)) == CO_FL_DATA_RD_ENA)) {
169 f |= CO_FL_CURR_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200170 fd_want_recv(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200171 }
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200172
173 /* update write status if needed */
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200174 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
175 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
176 fd_stop_send(c->t.sock.fd);
177 }
178 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
179 (f & (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_DATA_WR_ENA|CO_FL_WAIT_WR))) {
180 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200181 fd_poll_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200182 }
183 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_DATA_WR_ENA)) == CO_FL_DATA_WR_ENA)) {
184 f |= CO_FL_CURR_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200185 fd_want_send(c->t.sock.fd);
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200186 }
187 c->flags = f;
188}
189
190/* Update polling on connection <c>'s file descriptor depending on its current
191 * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN
192 * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*.
193 * The connection flags are updated with the new flags at the end of the
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200194 * operation. Polling is totally disabled if an error was reported.
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200195 */
196void conn_update_sock_polling(struct connection *c)
197{
198 unsigned int f = c->flags;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200199
Willy Tarreau0ffde2c2012-10-04 22:21:15 +0200200 if (unlikely(f & CO_FL_ERROR)) {
201 c->flags &= ~(CO_FL_CURR_RD_ENA | CO_FL_CURR_WR_ENA |
202 CO_FL_SOCK_RD_ENA | CO_FL_SOCK_WR_ENA |
203 CO_FL_DATA_RD_ENA | CO_FL_DATA_WR_ENA);
204 fd_stop_both(c->t.sock.fd);
205 return;
206 }
207
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200208 /* update read status if needed */
209 if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_CURR_RD_ENA)) {
210 f &= ~(CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
211 fd_stop_recv(c->t.sock.fd);
212 }
213 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL)) != (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL) &&
214 (f & (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD)) == (CO_FL_SOCK_RD_ENA|CO_FL_WAIT_RD))) {
215 f |= (CO_FL_CURR_RD_ENA|CO_FL_CURR_RD_POL);
216 fd_poll_recv(c->t.sock.fd);
217 }
218 else if (unlikely((f & (CO_FL_CURR_RD_ENA|CO_FL_SOCK_RD_ENA)) == CO_FL_SOCK_RD_ENA)) {
219 f |= CO_FL_CURR_RD_ENA;
220 fd_want_recv(c->t.sock.fd);
221 }
222
223 /* update write status if needed */
224 if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_CURR_WR_ENA)) {
225 f &= ~(CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
226 fd_stop_send(c->t.sock.fd);
227 }
228 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL)) != (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL) &&
229 (f & (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR)) == (CO_FL_SOCK_WR_ENA|CO_FL_WAIT_WR))) {
230 f |= (CO_FL_CURR_WR_ENA|CO_FL_CURR_WR_POL);
231 fd_poll_send(c->t.sock.fd);
232 }
233 else if (unlikely((f & (CO_FL_CURR_WR_ENA|CO_FL_SOCK_WR_ENA)) == CO_FL_SOCK_WR_ENA)) {
234 f |= CO_FL_CURR_WR_ENA;
235 fd_want_send(c->t.sock.fd);
236 }
237 c->flags = f;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200238}
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200239
240/* This handshake handler waits a PROXY protocol header at the beginning of the
241 * raw data stream. The header looks like this :
242 *
243 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
244 *
245 * There must be exactly one space between each field. Fields are :
246 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
247 * - SRC3 : layer 3 (eg: IP) source address in standard text form
248 * - DST3 : layer 3 (eg: IP) destination address in standard text form
249 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
250 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
251 *
252 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
253 *
254 * The header line is small and in all cases smaller than the smallest normal
255 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
256 * can safely use MSG_PEEK and avoid buffering.
257 *
258 * Once the data is fetched, the values are set in the connection's address
259 * fields, and data are removed from the socket's buffer. The function returns
260 * zero if it needs to wait for more data or if it fails, or 1 if it completed
261 * and removed itself.
262 */
263int conn_recv_proxy(struct connection *conn, int flag)
264{
265 char *line, *end;
266 int len;
267
268 /* we might have been called just after an asynchronous shutr */
269 if (conn->flags & CO_FL_SOCK_RD_SH)
270 goto fail;
271
272 do {
273 len = recv(conn->t.sock.fd, trash, trashlen, MSG_PEEK);
274 if (len < 0) {
275 if (errno == EINTR)
276 continue;
277 if (errno == EAGAIN) {
278 conn_sock_poll_recv(conn);
279 return 0;
280 }
281 goto fail;
282 }
283 } while (0);
284
285 if (len < 6)
286 goto missing;
287
288 line = trash;
289 end = trash + len;
290
291 /* Decode a possible proxy request, fail early if it does not match */
292 if (strncmp(line, "PROXY ", 6) != 0)
293 goto fail;
294
295 line += 6;
296 if (len < 18) /* shortest possible line */
297 goto missing;
298
299 if (!memcmp(line, "TCP4 ", 5) != 0) {
300 u32 src3, dst3, sport, dport;
301
302 line += 5;
303
304 src3 = inetaddr_host_lim_ret(line, end, &line);
305 if (line == end)
306 goto missing;
307 if (*line++ != ' ')
308 goto fail;
309
310 dst3 = inetaddr_host_lim_ret(line, end, &line);
311 if (line == end)
312 goto missing;
313 if (*line++ != ' ')
314 goto fail;
315
316 sport = read_uint((const char **)&line, end);
317 if (line == end)
318 goto missing;
319 if (*line++ != ' ')
320 goto fail;
321
322 dport = read_uint((const char **)&line, end);
323 if (line > end - 2)
324 goto missing;
325 if (*line++ != '\r')
326 goto fail;
327 if (*line++ != '\n')
328 goto fail;
329
330 /* update the session's addresses and mark them set */
331 ((struct sockaddr_in *)&conn->addr.from)->sin_family = AF_INET;
332 ((struct sockaddr_in *)&conn->addr.from)->sin_addr.s_addr = htonl(src3);
333 ((struct sockaddr_in *)&conn->addr.from)->sin_port = htons(sport);
334
335 ((struct sockaddr_in *)&conn->addr.to)->sin_family = AF_INET;
336 ((struct sockaddr_in *)&conn->addr.to)->sin_addr.s_addr = htonl(dst3);
337 ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(dport);
338 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
339 }
340 else if (!memcmp(line, "TCP6 ", 5) != 0) {
341 u32 sport, dport;
342 char *src_s;
343 char *dst_s, *sport_s, *dport_s;
344 struct in6_addr src3, dst3;
345
346 line += 5;
347
348 src_s = line;
349 dst_s = sport_s = dport_s = NULL;
350 while (1) {
351 if (line > end - 2) {
352 goto missing;
353 }
354 else if (*line == '\r') {
355 *line = 0;
356 line++;
357 if (*line++ != '\n')
358 goto fail;
359 break;
360 }
361
362 if (*line == ' ') {
363 *line = 0;
364 if (!dst_s)
365 dst_s = line + 1;
366 else if (!sport_s)
367 sport_s = line + 1;
368 else if (!dport_s)
369 dport_s = line + 1;
370 }
371 line++;
372 }
373
374 if (!dst_s || !sport_s || !dport_s)
375 goto fail;
376
377 sport = read_uint((const char **)&sport_s,dport_s - 1);
378 if (*sport_s != 0)
379 goto fail;
380
381 dport = read_uint((const char **)&dport_s,line - 2);
382 if (*dport_s != 0)
383 goto fail;
384
385 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
386 goto fail;
387
388 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
389 goto fail;
390
391 /* update the session's addresses and mark them set */
392 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_family = AF_INET6;
393 memcpy(&((struct sockaddr_in6 *)&conn->addr.from)->sin6_addr, &src3, sizeof(struct in6_addr));
394 ((struct sockaddr_in6 *)&conn->addr.from)->sin6_port = htons(sport);
395
396 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_family = AF_INET6;
397 memcpy(&((struct sockaddr_in6 *)&conn->addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr));
398 ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(dport);
399 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
400 }
401 else {
402 goto fail;
403 }
404
405 /* remove the PROXY line from the request. For this we re-read the
406 * exact line at once. If we don't get the exact same result, we
407 * fail.
408 */
409 len = line - trash;
410 do {
411 int len2 = recv(conn->t.sock.fd, trash, len, 0);
412 if (len2 < 0 && errno == EINTR)
413 continue;
414 if (len2 != len)
415 goto fail;
416 } while (0);
417
418 conn->flags &= ~flag;
419 return 1;
420
421 missing:
422 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
423 * we have not read anything. Otherwise we need to fail because we won't
424 * be able to poll anymore.
425 */
426 fail:
427 conn_sock_stop_both(conn);
428 conn->flags |= CO_FL_ERROR;
429 conn->flags &= ~flag;
430 return 0;
431}
432
433/* Makes a PROXY protocol line from the two addresses. The output is sent to
434 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
435 * It returns the number of bytes composing this line (including the trailing
436 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
437 * TCP6 and "UNKNOWN" formats.
438 */
439int make_proxy_line(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
440{
441 int ret = 0;
442
443 if (src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
444 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 ");
445 if (ret >= buf_len)
446 return 0;
447
448 /* IPv4 src */
449 if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret))
450 return 0;
451
452 ret += strlen(buf + ret);
453 if (ret >= buf_len)
454 return 0;
455
456 buf[ret++] = ' ';
457
458 /* IPv4 dst */
459 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->sin_addr, buf + ret, buf_len - ret))
460 return 0;
461
462 ret += strlen(buf + ret);
463 if (ret >= buf_len)
464 return 0;
465
466 /* source and destination ports */
467 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
468 ntohs(((struct sockaddr_in *)src)->sin_port),
469 ntohs(((struct sockaddr_in *)dst)->sin_port));
470 if (ret >= buf_len)
471 return 0;
472 }
473 else if (src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
474 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 ");
475 if (ret >= buf_len)
476 return 0;
477
478 /* IPv6 src */
479 if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret))
480 return 0;
481
482 ret += strlen(buf + ret);
483 if (ret >= buf_len)
484 return 0;
485
486 buf[ret++] = ' ';
487
488 /* IPv6 dst */
489 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->sin6_addr, buf + ret, buf_len - ret))
490 return 0;
491
492 ret += strlen(buf + ret);
493 if (ret >= buf_len)
494 return 0;
495
496 /* source and destination ports */
497 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
498 ntohs(((struct sockaddr_in6 *)src)->sin6_port),
499 ntohs(((struct sockaddr_in6 *)dst)->sin6_port));
500 if (ret >= buf_len)
501 return 0;
502 }
503 else {
504 /* unknown family combination */
505 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
506 if (ret >= buf_len)
507 return 0;
508 }
509 return ret;
510}