Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/proto/connection.h |
| 3 | * This file contains connection function prototypes |
| 4 | * |
| 5 | * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _PROTO_CONNECTION_H |
| 23 | #define _PROTO_CONNECTION_H |
| 24 | |
| 25 | #include <common/config.h> |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 26 | #include <common/memory.h> |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 27 | #include <types/connection.h> |
Willy Tarreau | d1d5454 | 2012-09-12 22:58:11 +0200 | [diff] [blame] | 28 | #include <types/listener.h> |
Willy Tarreau | 2b199c9 | 2012-11-23 17:32:21 +0100 | [diff] [blame] | 29 | #include <proto/fd.h> |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 30 | #include <proto/obj_type.h> |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 31 | |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 32 | extern struct pool_head *pool2_connection; |
| 33 | |
| 34 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 35 | int init_connection(); |
| 36 | |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 37 | /* I/O callback for fd-based connections. It calls the read/write handlers |
Willy Tarreau | afad0e0 | 2012-08-09 14:45:22 +0200 | [diff] [blame] | 38 | * provided by the connection's sock_ops. Returns 0. |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 39 | */ |
| 40 | int conn_fd_handler(int fd); |
| 41 | |
Willy Tarreau | 22cda21 | 2012-08-31 17:43:29 +0200 | [diff] [blame] | 42 | /* receive a PROXY protocol header over a connection */ |
| 43 | int conn_recv_proxy(struct connection *conn, int flag); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 44 | int make_proxy_line(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst); |
Willy Tarreau | 22cda21 | 2012-08-31 17:43:29 +0200 | [diff] [blame] | 45 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 46 | /* calls the init() function of the transport layer if any. |
| 47 | * Returns <0 in case of error. |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 48 | */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 49 | static inline int conn_xprt_init(struct connection *conn) |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 50 | { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 51 | if (conn->xprt && conn->xprt->init) |
| 52 | return conn->xprt->init(conn); |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 53 | return 0; |
| 54 | } |
| 55 | |
Willy Tarreau | 6c03a64 | 2012-10-12 17:00:05 +0200 | [diff] [blame] | 56 | /* Calls the close() function of the transport layer if any, and always unsets |
Willy Tarreau | 1e95491 | 2012-10-12 17:50:05 +0200 | [diff] [blame] | 57 | * the transport layer. However this is not done if the CO_FL_XPRT_TRACKED flag |
| 58 | * is set, which allows logs to take data from the transport layer very late if |
| 59 | * needed. |
Willy Tarreau | 6c03a64 | 2012-10-12 17:00:05 +0200 | [diff] [blame] | 60 | */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 61 | static inline void conn_xprt_close(struct connection *conn) |
Willy Tarreau | 8b11708 | 2012-08-06 15:06:49 +0200 | [diff] [blame] | 62 | { |
Willy Tarreau | 1e95491 | 2012-10-12 17:50:05 +0200 | [diff] [blame] | 63 | if (conn->xprt && !(conn->flags & CO_FL_XPRT_TRACKED)) { |
Willy Tarreau | 6c03a64 | 2012-10-12 17:00:05 +0200 | [diff] [blame] | 64 | if (conn->xprt->close) |
| 65 | conn->xprt->close(conn); |
| 66 | conn->xprt = NULL; |
| 67 | } |
Willy Tarreau | 8b11708 | 2012-08-06 15:06:49 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Willy Tarreau | 2b199c9 | 2012-11-23 17:32:21 +0100 | [diff] [blame] | 70 | /* If the connection still has a transport layer, then call its close() function |
| 71 | * if any, and delete the file descriptor if a control layer is set. This is |
| 72 | * used to close everything at once and atomically. However this is not done if |
| 73 | * the CO_FL_XPRT_TRACKED flag is set, which allows logs to take data from the |
| 74 | * transport layer very late if needed. |
| 75 | */ |
| 76 | static inline void conn_full_close(struct connection *conn) |
| 77 | { |
| 78 | if (conn->xprt && !(conn->flags & CO_FL_XPRT_TRACKED)) { |
| 79 | if (conn->xprt->close) |
| 80 | conn->xprt->close(conn); |
| 81 | if (conn->ctrl) |
| 82 | fd_delete(conn->t.sock.fd); |
| 83 | conn->xprt = NULL; |
| 84 | } |
| 85 | } |
| 86 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 87 | /* Update polling on connection <c>'s file descriptor depending on its current |
| 88 | * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN |
| 89 | * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*. |
| 90 | * The connection flags are updated with the new flags at the end of the |
Willy Tarreau | 0ffde2c | 2012-10-04 22:21:15 +0200 | [diff] [blame] | 91 | * operation. Polling is totally disabled if an error was reported. |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 92 | */ |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 93 | void conn_update_sock_polling(struct connection *c); |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 94 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 95 | /* Update polling on connection <c>'s file descriptor depending on its current |
| 96 | * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN |
| 97 | * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*. |
| 98 | * The connection flags are updated with the new flags at the end of the |
Willy Tarreau | 0ffde2c | 2012-10-04 22:21:15 +0200 | [diff] [blame] | 99 | * operation. Polling is totally disabled if an error was reported. |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 100 | */ |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 101 | void conn_update_data_polling(struct connection *c); |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 102 | |
Willy Tarreau | 5f1504f | 2012-10-04 23:55:57 +0200 | [diff] [blame] | 103 | /* This callback is used to send a valid PROXY protocol line to a socket being |
| 104 | * established from the local machine. It sets the protocol addresses to the |
| 105 | * local and remote address. This is typically used with health checks or when |
| 106 | * it is not possible to determine the other end's address. It returns 0 if it |
| 107 | * fails in a fatal way or needs to poll to go further, otherwise it returns |
| 108 | * non-zero and removes itself from the connection's flags (the bit is provided |
| 109 | * in <flag> by the caller). It is designed to be called by the connection |
| 110 | * handler and relies on it to commit polling changes. Note that this function |
| 111 | * expects to be able to send the whole line at once, which should always be |
| 112 | * possible since it is supposed to start at the first byte of the outgoing |
| 113 | * data segment. |
| 114 | */ |
| 115 | int conn_local_send_proxy(struct connection *conn, unsigned int flag); |
| 116 | |
Willy Tarreau | 7d28149 | 2012-12-16 19:19:13 +0100 | [diff] [blame] | 117 | /* Refresh the connection's polling flags from its file descriptor status. |
| 118 | * This should be called at the beginning of a connection handler. |
| 119 | */ |
| 120 | static inline void conn_refresh_polling_flags(struct connection *conn) |
| 121 | { |
| 122 | conn->flags &= ~(CO_FL_WAIT_ROOM | CO_FL_WAIT_RD | CO_FL_WAIT_DATA | CO_FL_WAIT_WR); |
| 123 | |
| 124 | if (conn->ctrl) { |
| 125 | unsigned int flags = conn->flags & ~(CO_FL_CURR_RD_ENA | CO_FL_CURR_WR_ENA); |
| 126 | |
| 127 | if (fd_ev_is_set(conn->t.sock.fd, DIR_RD)) |
| 128 | flags |= CO_FL_CURR_RD_ENA; |
| 129 | if (fd_ev_is_set(conn->t.sock.fd, DIR_WR)) |
| 130 | flags |= CO_FL_CURR_WR_ENA; |
| 131 | conn->flags = flags; |
| 132 | } |
| 133 | } |
| 134 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 135 | /* inspects c->flags and returns non-zero if DATA ENA changes from the CURR ENA |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 136 | * or if the WAIT flags are set with their respective ENA flags. Additionally, |
Willy Tarreau | 0ffde2c | 2012-10-04 22:21:15 +0200 | [diff] [blame] | 137 | * non-zero is also returned if an error was reported on the connection. This |
| 138 | * function is used quite often and is inlined. In order to proceed optimally |
| 139 | * with very little code and CPU cycles, the bits are arranged so that a change |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 140 | * can be detected by a few left shifts, a xor, and a mask. These operations |
| 141 | * detect when W&D are both enabled for either direction, when C&D differ for |
| 142 | * either direction and when Error is set. The trick consists in first keeping |
| 143 | * only the bits we're interested in, since they don't collide when shifted, |
| 144 | * and to perform the AND at the end. In practice, the compiler is able to |
| 145 | * replace the last AND with a TEST in boolean conditions. This results in |
| 146 | * checks that are done in 4-6 cycles and less than 30 bytes. |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 147 | */ |
| 148 | static inline unsigned int conn_data_polling_changes(const struct connection *c) |
| 149 | { |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 150 | unsigned int f = c->flags; |
| 151 | f &= CO_FL_DATA_WR_ENA | CO_FL_DATA_RD_ENA | CO_FL_CURR_WR_ENA | |
| 152 | CO_FL_CURR_RD_ENA | CO_FL_ERROR | CO_FL_WAIT_WR | CO_FL_WAIT_RD; |
| 153 | |
| 154 | f = (f & (f << 2)) | /* test W & D */ |
| 155 | ((f ^ (f << 1)) & (CO_FL_CURR_WR_ENA|CO_FL_CURR_RD_ENA)); /* test C ^ D */ |
| 156 | return f & (CO_FL_WAIT_WR | CO_FL_WAIT_RD | CO_FL_CURR_WR_ENA | CO_FL_CURR_RD_ENA | CO_FL_ERROR); |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 159 | /* inspects c->flags and returns non-zero if SOCK ENA changes from the CURR ENA |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 160 | * or if the WAIT flags are set with their respective ENA flags. Additionally, |
Willy Tarreau | 0ffde2c | 2012-10-04 22:21:15 +0200 | [diff] [blame] | 161 | * non-zero is also returned if an error was reported on the connection. This |
| 162 | * function is used quite often and is inlined. In order to proceed optimally |
| 163 | * with very little code and CPU cycles, the bits are arranged so that a change |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 164 | * can be detected by a few left shifts, a xor, and a mask. These operations |
| 165 | * detect when W&S are both enabled for either direction, when C&S differ for |
| 166 | * either direction and when Error is set. The trick consists in first keeping |
| 167 | * only the bits we're interested in, since they don't collide when shifted, |
| 168 | * and to perform the AND at the end. In practice, the compiler is able to |
| 169 | * replace the last AND with a TEST in boolean conditions. This results in |
| 170 | * checks that are done in 4-6 cycles and less than 30 bytes. |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 171 | */ |
| 172 | static inline unsigned int conn_sock_polling_changes(const struct connection *c) |
| 173 | { |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 174 | unsigned int f = c->flags; |
| 175 | f &= CO_FL_SOCK_WR_ENA | CO_FL_SOCK_RD_ENA | CO_FL_CURR_WR_ENA | |
| 176 | CO_FL_CURR_RD_ENA | CO_FL_ERROR | CO_FL_WAIT_WR | CO_FL_WAIT_RD; |
| 177 | |
| 178 | f = (f & (f << 3)) | /* test W & S */ |
| 179 | ((f ^ (f << 2)) & (CO_FL_CURR_WR_ENA|CO_FL_CURR_RD_ENA)); /* test C ^ S */ |
| 180 | return f & (CO_FL_WAIT_WR | CO_FL_WAIT_RD | CO_FL_CURR_WR_ENA | CO_FL_CURR_RD_ENA | CO_FL_ERROR); |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* Automatically updates polling on connection <c> depending on the DATA flags |
| 184 | * if no handshake is in progress. |
| 185 | */ |
| 186 | static inline void conn_cond_update_data_polling(struct connection *c) |
| 187 | { |
| 188 | if (!(c->flags & CO_FL_POLL_SOCK) && conn_data_polling_changes(c)) |
| 189 | conn_update_data_polling(c); |
| 190 | } |
| 191 | |
| 192 | /* Automatically updates polling on connection <c> depending on the SOCK flags |
| 193 | * if a handshake is in progress. |
| 194 | */ |
| 195 | static inline void conn_cond_update_sock_polling(struct connection *c) |
| 196 | { |
| 197 | if ((c->flags & CO_FL_POLL_SOCK) && conn_sock_polling_changes(c)) |
| 198 | conn_update_sock_polling(c); |
| 199 | } |
| 200 | |
Willy Tarreau | 36fb02c | 2012-11-24 11:09:07 +0100 | [diff] [blame] | 201 | /* Stop all polling on the fd. This might be used when an error is encountered |
| 202 | * for example. |
| 203 | */ |
| 204 | static inline void conn_stop_polling(struct connection *c) |
| 205 | { |
| 206 | c->flags &= ~(CO_FL_CURR_RD_ENA | CO_FL_CURR_WR_ENA | |
| 207 | CO_FL_SOCK_RD_ENA | CO_FL_SOCK_WR_ENA | |
| 208 | CO_FL_DATA_RD_ENA | CO_FL_DATA_WR_ENA); |
| 209 | fd_stop_both(c->t.sock.fd); |
| 210 | } |
| 211 | |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 212 | /* Automatically update polling on connection <c> depending on the DATA and |
| 213 | * SOCK flags, and on whether a handshake is in progress or not. This may be |
| 214 | * called at any moment when there is a doubt about the effectiveness of the |
| 215 | * polling state, for instance when entering or leaving the handshake state. |
| 216 | */ |
| 217 | static inline void conn_cond_update_polling(struct connection *c) |
| 218 | { |
Willy Tarreau | 36fb02c | 2012-11-24 11:09:07 +0100 | [diff] [blame] | 219 | if (unlikely(c->flags & CO_FL_ERROR)) |
| 220 | conn_stop_polling(c); |
| 221 | else if (!(c->flags & CO_FL_POLL_SOCK) && conn_data_polling_changes(c)) |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 222 | conn_update_data_polling(c); |
| 223 | else if ((c->flags & CO_FL_POLL_SOCK) && conn_sock_polling_changes(c)) |
| 224 | conn_update_sock_polling(c); |
| 225 | } |
| 226 | |
| 227 | /***** Event manipulation primitives for use by DATA I/O callbacks *****/ |
| 228 | /* The __conn_* versions do not propagate to lower layers and are only meant |
| 229 | * to be used by handlers called by the connection handler. The other ones |
| 230 | * may be used anywhere. |
| 231 | */ |
| 232 | static inline void __conn_data_want_recv(struct connection *c) |
| 233 | { |
| 234 | c->flags |= CO_FL_DATA_RD_ENA; |
| 235 | } |
| 236 | |
| 237 | static inline void __conn_data_stop_recv(struct connection *c) |
| 238 | { |
| 239 | c->flags &= ~CO_FL_DATA_RD_ENA; |
| 240 | } |
| 241 | |
| 242 | static inline void __conn_data_poll_recv(struct connection *c) |
| 243 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 244 | c->flags |= CO_FL_WAIT_RD | CO_FL_DATA_RD_ENA; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | static inline void __conn_data_want_send(struct connection *c) |
| 248 | { |
| 249 | c->flags |= CO_FL_DATA_WR_ENA; |
| 250 | } |
| 251 | |
| 252 | static inline void __conn_data_stop_send(struct connection *c) |
| 253 | { |
| 254 | c->flags &= ~CO_FL_DATA_WR_ENA; |
| 255 | } |
| 256 | |
| 257 | static inline void __conn_data_poll_send(struct connection *c) |
| 258 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 259 | c->flags |= CO_FL_WAIT_WR | CO_FL_DATA_WR_ENA; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | static inline void __conn_data_stop_both(struct connection *c) |
| 263 | { |
| 264 | c->flags &= ~(CO_FL_DATA_WR_ENA | CO_FL_DATA_RD_ENA); |
| 265 | } |
| 266 | |
| 267 | static inline void conn_data_want_recv(struct connection *c) |
| 268 | { |
| 269 | __conn_data_want_recv(c); |
| 270 | conn_cond_update_data_polling(c); |
| 271 | } |
| 272 | |
| 273 | static inline void conn_data_stop_recv(struct connection *c) |
| 274 | { |
| 275 | __conn_data_stop_recv(c); |
| 276 | conn_cond_update_data_polling(c); |
| 277 | } |
| 278 | |
| 279 | static inline void conn_data_poll_recv(struct connection *c) |
| 280 | { |
| 281 | __conn_data_poll_recv(c); |
| 282 | conn_cond_update_data_polling(c); |
| 283 | } |
| 284 | |
| 285 | static inline void conn_data_want_send(struct connection *c) |
| 286 | { |
| 287 | __conn_data_want_send(c); |
| 288 | conn_cond_update_data_polling(c); |
| 289 | } |
| 290 | |
| 291 | static inline void conn_data_stop_send(struct connection *c) |
| 292 | { |
| 293 | __conn_data_stop_send(c); |
| 294 | conn_cond_update_data_polling(c); |
| 295 | } |
| 296 | |
| 297 | static inline void conn_data_poll_send(struct connection *c) |
| 298 | { |
| 299 | __conn_data_poll_send(c); |
| 300 | conn_cond_update_data_polling(c); |
| 301 | } |
| 302 | |
| 303 | static inline void conn_data_stop_both(struct connection *c) |
| 304 | { |
| 305 | __conn_data_stop_both(c); |
| 306 | conn_cond_update_data_polling(c); |
| 307 | } |
| 308 | |
| 309 | /***** Event manipulation primitives for use by handshake I/O callbacks *****/ |
| 310 | /* The __conn_* versions do not propagate to lower layers and are only meant |
| 311 | * to be used by handlers called by the connection handler. The other ones |
| 312 | * may be used anywhere. |
| 313 | */ |
| 314 | static inline void __conn_sock_want_recv(struct connection *c) |
| 315 | { |
| 316 | c->flags |= CO_FL_SOCK_RD_ENA; |
| 317 | } |
| 318 | |
| 319 | static inline void __conn_sock_stop_recv(struct connection *c) |
| 320 | { |
| 321 | c->flags &= ~CO_FL_SOCK_RD_ENA; |
| 322 | } |
| 323 | |
| 324 | static inline void __conn_sock_poll_recv(struct connection *c) |
| 325 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 326 | c->flags |= CO_FL_WAIT_RD | CO_FL_SOCK_RD_ENA; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static inline void __conn_sock_want_send(struct connection *c) |
| 330 | { |
| 331 | c->flags |= CO_FL_SOCK_WR_ENA; |
| 332 | } |
| 333 | |
| 334 | static inline void __conn_sock_stop_send(struct connection *c) |
| 335 | { |
| 336 | c->flags &= ~CO_FL_SOCK_WR_ENA; |
| 337 | } |
| 338 | |
| 339 | static inline void __conn_sock_poll_send(struct connection *c) |
| 340 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 341 | c->flags |= CO_FL_WAIT_WR | CO_FL_SOCK_WR_ENA; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static inline void __conn_sock_stop_both(struct connection *c) |
| 345 | { |
| 346 | c->flags &= ~(CO_FL_SOCK_WR_ENA | CO_FL_SOCK_RD_ENA); |
| 347 | } |
| 348 | |
| 349 | static inline void conn_sock_want_recv(struct connection *c) |
| 350 | { |
| 351 | __conn_sock_want_recv(c); |
| 352 | conn_cond_update_sock_polling(c); |
| 353 | } |
| 354 | |
| 355 | static inline void conn_sock_stop_recv(struct connection *c) |
| 356 | { |
| 357 | __conn_sock_stop_recv(c); |
| 358 | conn_cond_update_sock_polling(c); |
| 359 | } |
| 360 | |
| 361 | static inline void conn_sock_poll_recv(struct connection *c) |
| 362 | { |
| 363 | __conn_sock_poll_recv(c); |
| 364 | conn_cond_update_sock_polling(c); |
| 365 | } |
| 366 | |
| 367 | static inline void conn_sock_want_send(struct connection *c) |
| 368 | { |
| 369 | __conn_sock_want_send(c); |
| 370 | conn_cond_update_sock_polling(c); |
| 371 | } |
| 372 | |
| 373 | static inline void conn_sock_stop_send(struct connection *c) |
| 374 | { |
| 375 | __conn_sock_stop_send(c); |
| 376 | conn_cond_update_sock_polling(c); |
| 377 | } |
| 378 | |
| 379 | static inline void conn_sock_poll_send(struct connection *c) |
| 380 | { |
| 381 | __conn_sock_poll_send(c); |
| 382 | conn_cond_update_sock_polling(c); |
| 383 | } |
| 384 | |
| 385 | static inline void conn_sock_stop_both(struct connection *c) |
| 386 | { |
| 387 | __conn_sock_stop_both(c); |
| 388 | conn_cond_update_sock_polling(c); |
| 389 | } |
Willy Tarreau | 8b11708 | 2012-08-06 15:06:49 +0200 | [diff] [blame] | 390 | |
Willy Tarreau | 3af56a9 | 2012-08-20 16:55:48 +0200 | [diff] [blame] | 391 | /* shutdown management */ |
| 392 | static inline void conn_sock_read0(struct connection *c) |
| 393 | { |
| 394 | c->flags |= CO_FL_SOCK_RD_SH; |
| 395 | __conn_sock_stop_recv(c); |
| 396 | } |
| 397 | |
| 398 | static inline void conn_data_read0(struct connection *c) |
| 399 | { |
| 400 | c->flags |= CO_FL_DATA_RD_SH; |
| 401 | __conn_data_stop_recv(c); |
| 402 | } |
| 403 | |
| 404 | static inline void conn_sock_shutw(struct connection *c) |
| 405 | { |
| 406 | c->flags |= CO_FL_SOCK_WR_SH; |
| 407 | __conn_sock_stop_send(c); |
| 408 | } |
| 409 | |
| 410 | static inline void conn_data_shutw(struct connection *c) |
| 411 | { |
| 412 | c->flags |= CO_FL_DATA_WR_SH; |
| 413 | __conn_data_stop_send(c); |
| 414 | } |
| 415 | |
| 416 | /* detect sock->data read0 transition */ |
| 417 | static inline int conn_data_read0_pending(struct connection *c) |
| 418 | { |
| 419 | return (c->flags & (CO_FL_DATA_RD_SH | CO_FL_SOCK_RD_SH)) == CO_FL_SOCK_RD_SH; |
| 420 | } |
| 421 | |
| 422 | /* detect data->sock shutw transition */ |
| 423 | static inline int conn_sock_shutw_pending(struct connection *c) |
| 424 | { |
| 425 | return (c->flags & (CO_FL_DATA_WR_SH | CO_FL_SOCK_WR_SH)) == CO_FL_DATA_WR_SH; |
| 426 | } |
| 427 | |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 428 | /* Retrieves the connection's source address */ |
| 429 | static inline void conn_get_from_addr(struct connection *conn) |
| 430 | { |
| 431 | if (conn->flags & CO_FL_ADDR_FROM_SET) |
| 432 | return; |
| 433 | |
| 434 | if (!conn->ctrl || !conn->ctrl->get_src) |
| 435 | return; |
| 436 | |
| 437 | if (conn->ctrl->get_src(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from, |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 438 | sizeof(conn->addr.from), |
| 439 | obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1) |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 440 | return; |
| 441 | conn->flags |= CO_FL_ADDR_FROM_SET; |
| 442 | } |
| 443 | |
| 444 | /* Retrieves the connection's original destination address */ |
| 445 | static inline void conn_get_to_addr(struct connection *conn) |
| 446 | { |
| 447 | if (conn->flags & CO_FL_ADDR_TO_SET) |
| 448 | return; |
| 449 | |
| 450 | if (!conn->ctrl || !conn->ctrl->get_dst) |
| 451 | return; |
| 452 | |
| 453 | if (conn->ctrl->get_dst(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to, |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 454 | sizeof(conn->addr.to), |
| 455 | obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1) |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 456 | return; |
| 457 | conn->flags |= CO_FL_ADDR_TO_SET; |
| 458 | } |
| 459 | |
Willy Tarreau | bd99aab | 2012-10-02 20:57:19 +0200 | [diff] [blame] | 460 | /* Assigns a connection with the appropriate data, ctrl, transport layers, and owner. */ |
| 461 | static inline void conn_assign(struct connection *conn, const struct data_cb *data, |
| 462 | const struct protocol *ctrl, const struct xprt_ops *xprt, |
| 463 | void *owner) |
Willy Tarreau | dda5e7c | 2012-09-24 17:15:42 +0200 | [diff] [blame] | 464 | { |
Willy Tarreau | 74beec3 | 2012-10-03 00:41:04 +0200 | [diff] [blame] | 465 | conn->data = data; |
Willy Tarreau | dda5e7c | 2012-09-24 17:15:42 +0200 | [diff] [blame] | 466 | conn->ctrl = ctrl; |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 467 | conn->xprt = xprt; |
Willy Tarreau | cd37995 | 2012-09-27 22:14:33 +0200 | [diff] [blame] | 468 | conn->owner = owner; |
Willy Tarreau | bd99aab | 2012-10-02 20:57:19 +0200 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /* prepares a connection with the appropriate data, ctrl, transport layers, and |
| 472 | * owner. The transport state and context are set to 0. |
| 473 | */ |
| 474 | static inline void conn_prepare(struct connection *conn, const struct data_cb *data, |
| 475 | const struct protocol *ctrl, const struct xprt_ops *xprt, |
| 476 | void *owner) |
| 477 | { |
| 478 | conn_assign(conn, data, ctrl, xprt, owner); |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 479 | conn->xprt_st = 0; |
| 480 | conn->xprt_ctx = NULL; |
Willy Tarreau | dda5e7c | 2012-09-24 17:15:42 +0200 | [diff] [blame] | 481 | } |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 482 | |
Willy Tarreau | 0af2912 | 2012-12-03 15:35:00 +0100 | [diff] [blame] | 483 | /* returns a human-readable error code for conn->err_code, or NULL if the code |
| 484 | * is unknown. |
| 485 | */ |
| 486 | static inline const char *conn_err_code_str(struct connection *c) |
| 487 | { |
| 488 | switch (c->err_code) { |
| 489 | case CO_ER_NONE: return "Success"; |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 490 | case CO_ER_PRX_EMPTY: return "Connection closed while waiting for PROXY protocol header"; |
| 491 | case CO_ER_PRX_ABORT: return "Connection error while waiting for PROXY protocol header"; |
Willy Tarreau | 0af2912 | 2012-12-03 15:35:00 +0100 | [diff] [blame] | 492 | case CO_ER_PRX_TIMEOUT: return "Timeout while waiting for PROXY protocol header"; |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 493 | case CO_ER_PRX_TRUNCATED: return "Truncated PROXY protocol header received"; |
| 494 | case CO_ER_PRX_NOT_HDR: return "Received something which does not look like a PROXY protocol header"; |
| 495 | case CO_ER_PRX_BAD_HDR: return "Received an invalid PROXY protocol header"; |
| 496 | case CO_ER_PRX_BAD_PROTO: return "Received an unhandled protocol in the PROXY protocol header"; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 497 | case CO_ER_SSL_EMPTY: return "Connection closed during SSL handshake"; |
| 498 | case CO_ER_SSL_ABORT: return "Connection error during SSL handshake"; |
Willy Tarreau | 0af2912 | 2012-12-03 15:35:00 +0100 | [diff] [blame] | 499 | case CO_ER_SSL_TIMEOUT: return "Timeout during SSL handshake"; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 500 | case CO_ER_SSL_TOO_MANY: return "Too many SSL connections"; |
| 501 | case CO_ER_SSL_NO_MEM: return "Out of memory when initializing an SSL connection"; |
| 502 | case CO_ER_SSL_RENEG: return "Rejected a client-initiated SSL renegociation attempt"; |
| 503 | case CO_ER_SSL_CA_FAIL: return "SSL client CA chain cannot be verified"; |
| 504 | case CO_ER_SSL_CRT_FAIL: return "SSL client certificate not trusted"; |
| 505 | case CO_ER_SSL_HANDSHAKE: return "SSL handshake failure"; |
Willy Tarreau | 830bf61 | 2013-12-01 20:29:58 +0100 | [diff] [blame] | 506 | case CO_ER_SSL_NO_TARGET: return "Attempt to use SSL on an unknown target (internal error)"; |
Willy Tarreau | 0af2912 | 2012-12-03 15:35:00 +0100 | [diff] [blame] | 507 | } |
| 508 | return NULL; |
| 509 | } |
| 510 | |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 511 | #endif /* _PROTO_CONNECTION_H */ |
| 512 | |
| 513 | /* |
| 514 | * Local variables: |
| 515 | * c-indent-level: 8 |
| 516 | * c-basic-offset: 8 |
| 517 | * End: |
| 518 | */ |