Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 13 | #include <common/compat.h> |
| 14 | #include <common/config.h> |
| 15 | |
| 16 | #include <types/connection.h> |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 17 | |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 18 | #include <proto/proto_tcp.h> |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 19 | #include <proto/stream_interface.h> |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 20 | |
| 21 | /* I/O callback for fd-based connections. It calls the read/write handlers |
| 22 | * provided by the connection's sock_ops, which must be valid. It returns |
| 23 | * FD_WAIT_*. |
| 24 | */ |
| 25 | int conn_fd_handler(int fd) |
| 26 | { |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 27 | struct connection *conn = fdtab[fd].owner; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 28 | int ret = 0; |
| 29 | |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 30 | if (!conn) |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 31 | goto leave; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 32 | |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 33 | if (conn->flags & CO_FL_ERROR) |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 34 | goto leave; |
| 35 | |
| 36 | if (conn->flags & CO_FL_SI_SEND_PROXY) |
| 37 | if ((ret = conn_si_send_proxy(conn, CO_FL_SI_SEND_PROXY))) |
| 38 | goto leave; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 39 | |
| 40 | if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR)) |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 41 | if (!conn->data->read(fd)) |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 42 | ret |= FD_WAIT_READ; |
| 43 | |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 44 | if (conn->flags & CO_FL_ERROR) |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 45 | goto leave; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 46 | |
| 47 | if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR)) |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 48 | if (!conn->data->write(fd)) |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 49 | ret |= FD_WAIT_WRITE; |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 50 | |
| 51 | if (conn->flags & CO_FL_ERROR) |
| 52 | goto leave; |
| 53 | |
| 54 | if (conn->flags & CO_FL_WAIT_L4_CONN) { |
| 55 | /* still waiting for a connection to establish and no data to |
| 56 | * send in order to probe it ? Then let's retry the connect(). |
| 57 | */ |
| 58 | if (!tcp_connect_probe(fd)) |
| 59 | ret |= FD_WAIT_WRITE; |
| 60 | } |
| 61 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 62 | leave: |
Willy Tarreau | 61ace1b | 2012-07-23 12:14:26 +0200 | [diff] [blame] | 63 | /* remove the events before leaving */ |
| 64 | fdtab[fd].ev &= ~(FD_POLL_IN | FD_POLL_OUT | FD_POLL_HUP | FD_POLL_ERR); |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 65 | return ret; |
| 66 | } |