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 | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 29 | #include <proto/obj_type.h> |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 30 | |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 31 | extern struct pool_head *pool2_connection; |
| 32 | |
| 33 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 34 | int init_connection(); |
| 35 | |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 36 | /* 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] | 37 | * provided by the connection's sock_ops. Returns 0. |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 38 | */ |
| 39 | int conn_fd_handler(int fd); |
| 40 | |
Willy Tarreau | 22cda21 | 2012-08-31 17:43:29 +0200 | [diff] [blame] | 41 | /* receive a PROXY protocol header over a connection */ |
| 42 | int conn_recv_proxy(struct connection *conn, int flag); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 43 | 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] | 44 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 45 | /* calls the init() function of the transport layer if any. |
| 46 | * Returns <0 in case of error. |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 47 | */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 48 | static inline int conn_xprt_init(struct connection *conn) |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 49 | { |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 50 | if (conn->xprt && conn->xprt->init) |
| 51 | return conn->xprt->init(conn); |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
Willy Tarreau | 6c03a64 | 2012-10-12 17:00:05 +0200 | [diff] [blame] | 55 | /* 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] | 56 | * the transport layer. However this is not done if the CO_FL_XPRT_TRACKED flag |
| 57 | * is set, which allows logs to take data from the transport layer very late if |
| 58 | * needed. |
Willy Tarreau | 6c03a64 | 2012-10-12 17:00:05 +0200 | [diff] [blame] | 59 | */ |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 60 | static inline void conn_xprt_close(struct connection *conn) |
Willy Tarreau | 8b11708 | 2012-08-06 15:06:49 +0200 | [diff] [blame] | 61 | { |
Willy Tarreau | 1e95491 | 2012-10-12 17:50:05 +0200 | [diff] [blame] | 62 | if (conn->xprt && !(conn->flags & CO_FL_XPRT_TRACKED)) { |
Willy Tarreau | 6c03a64 | 2012-10-12 17:00:05 +0200 | [diff] [blame] | 63 | if (conn->xprt->close) |
| 64 | conn->xprt->close(conn); |
| 65 | conn->xprt = NULL; |
| 66 | } |
Willy Tarreau | 8b11708 | 2012-08-06 15:06:49 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 69 | /* Update polling on connection <c>'s file descriptor depending on its current |
| 70 | * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN |
| 71 | * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*. |
| 72 | * 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] | 73 | * operation. Polling is totally disabled if an error was reported. |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 74 | */ |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 75 | void conn_update_sock_polling(struct connection *c); |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 76 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 77 | /* Update polling on connection <c>'s file descriptor depending on its current |
| 78 | * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN |
| 79 | * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*. |
| 80 | * 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] | 81 | * operation. Polling is totally disabled if an error was reported. |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 82 | */ |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 83 | void conn_update_data_polling(struct connection *c); |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 84 | |
Willy Tarreau | 5f1504f | 2012-10-04 23:55:57 +0200 | [diff] [blame] | 85 | /* This callback is used to send a valid PROXY protocol line to a socket being |
| 86 | * established from the local machine. It sets the protocol addresses to the |
| 87 | * local and remote address. This is typically used with health checks or when |
| 88 | * it is not possible to determine the other end's address. It returns 0 if it |
| 89 | * fails in a fatal way or needs to poll to go further, otherwise it returns |
| 90 | * non-zero and removes itself from the connection's flags (the bit is provided |
| 91 | * in <flag> by the caller). It is designed to be called by the connection |
| 92 | * handler and relies on it to commit polling changes. Note that this function |
| 93 | * expects to be able to send the whole line at once, which should always be |
| 94 | * possible since it is supposed to start at the first byte of the outgoing |
| 95 | * data segment. |
| 96 | */ |
| 97 | int conn_local_send_proxy(struct connection *conn, unsigned int flag); |
| 98 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 99 | /* 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] | 100 | * 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] | 101 | * non-zero is also returned if an error was reported on the connection. This |
| 102 | * function is used quite often and is inlined. In order to proceed optimally |
| 103 | * 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] | 104 | * can be detected by a few left shifts, a xor, and a mask. These operations |
| 105 | * detect when W&D are both enabled for either direction, when C&D differ for |
| 106 | * either direction and when Error is set. The trick consists in first keeping |
| 107 | * only the bits we're interested in, since they don't collide when shifted, |
| 108 | * and to perform the AND at the end. In practice, the compiler is able to |
| 109 | * replace the last AND with a TEST in boolean conditions. This results in |
| 110 | * 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] | 111 | */ |
| 112 | static inline unsigned int conn_data_polling_changes(const struct connection *c) |
| 113 | { |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 114 | unsigned int f = c->flags; |
| 115 | f &= CO_FL_DATA_WR_ENA | CO_FL_DATA_RD_ENA | CO_FL_CURR_WR_ENA | |
| 116 | CO_FL_CURR_RD_ENA | CO_FL_ERROR | CO_FL_WAIT_WR | CO_FL_WAIT_RD; |
| 117 | |
| 118 | f = (f & (f << 2)) | /* test W & D */ |
| 119 | ((f ^ (f << 1)) & (CO_FL_CURR_WR_ENA|CO_FL_CURR_RD_ENA)); /* test C ^ D */ |
| 120 | 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] | 121 | } |
| 122 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 123 | /* 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] | 124 | * 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] | 125 | * non-zero is also returned if an error was reported on the connection. This |
| 126 | * function is used quite often and is inlined. In order to proceed optimally |
| 127 | * 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] | 128 | * can be detected by a few left shifts, a xor, and a mask. These operations |
| 129 | * detect when W&S are both enabled for either direction, when C&S differ for |
| 130 | * either direction and when Error is set. The trick consists in first keeping |
| 131 | * only the bits we're interested in, since they don't collide when shifted, |
| 132 | * and to perform the AND at the end. In practice, the compiler is able to |
| 133 | * replace the last AND with a TEST in boolean conditions. This results in |
| 134 | * 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] | 135 | */ |
| 136 | static inline unsigned int conn_sock_polling_changes(const struct connection *c) |
| 137 | { |
Willy Tarreau | c8dd77f | 2012-11-05 17:52:26 +0100 | [diff] [blame] | 138 | unsigned int f = c->flags; |
| 139 | f &= CO_FL_SOCK_WR_ENA | CO_FL_SOCK_RD_ENA | CO_FL_CURR_WR_ENA | |
| 140 | CO_FL_CURR_RD_ENA | CO_FL_ERROR | CO_FL_WAIT_WR | CO_FL_WAIT_RD; |
| 141 | |
| 142 | f = (f & (f << 3)) | /* test W & S */ |
| 143 | ((f ^ (f << 2)) & (CO_FL_CURR_WR_ENA|CO_FL_CURR_RD_ENA)); /* test C ^ S */ |
| 144 | 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] | 145 | } |
| 146 | |
| 147 | /* Automatically updates polling on connection <c> depending on the DATA flags |
| 148 | * if no handshake is in progress. |
| 149 | */ |
| 150 | static inline void conn_cond_update_data_polling(struct connection *c) |
| 151 | { |
| 152 | if (!(c->flags & CO_FL_POLL_SOCK) && conn_data_polling_changes(c)) |
| 153 | conn_update_data_polling(c); |
| 154 | } |
| 155 | |
| 156 | /* Automatically updates polling on connection <c> depending on the SOCK flags |
| 157 | * if a handshake is in progress. |
| 158 | */ |
| 159 | static inline void conn_cond_update_sock_polling(struct connection *c) |
| 160 | { |
| 161 | if ((c->flags & CO_FL_POLL_SOCK) && conn_sock_polling_changes(c)) |
| 162 | conn_update_sock_polling(c); |
| 163 | } |
| 164 | |
| 165 | /* Automatically update polling on connection <c> depending on the DATA and |
| 166 | * SOCK flags, and on whether a handshake is in progress or not. This may be |
| 167 | * called at any moment when there is a doubt about the effectiveness of the |
| 168 | * polling state, for instance when entering or leaving the handshake state. |
| 169 | */ |
| 170 | static inline void conn_cond_update_polling(struct connection *c) |
| 171 | { |
| 172 | if (!(c->flags & CO_FL_POLL_SOCK) && conn_data_polling_changes(c)) |
| 173 | conn_update_data_polling(c); |
| 174 | else if ((c->flags & CO_FL_POLL_SOCK) && conn_sock_polling_changes(c)) |
| 175 | conn_update_sock_polling(c); |
| 176 | } |
| 177 | |
| 178 | /***** Event manipulation primitives for use by DATA I/O callbacks *****/ |
| 179 | /* The __conn_* versions do not propagate to lower layers and are only meant |
| 180 | * to be used by handlers called by the connection handler. The other ones |
| 181 | * may be used anywhere. |
| 182 | */ |
| 183 | static inline void __conn_data_want_recv(struct connection *c) |
| 184 | { |
| 185 | c->flags |= CO_FL_DATA_RD_ENA; |
| 186 | } |
| 187 | |
| 188 | static inline void __conn_data_stop_recv(struct connection *c) |
| 189 | { |
| 190 | c->flags &= ~CO_FL_DATA_RD_ENA; |
| 191 | } |
| 192 | |
| 193 | static inline void __conn_data_poll_recv(struct connection *c) |
| 194 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 195 | c->flags |= CO_FL_WAIT_RD | CO_FL_DATA_RD_ENA; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static inline void __conn_data_want_send(struct connection *c) |
| 199 | { |
| 200 | c->flags |= CO_FL_DATA_WR_ENA; |
| 201 | } |
| 202 | |
| 203 | static inline void __conn_data_stop_send(struct connection *c) |
| 204 | { |
| 205 | c->flags &= ~CO_FL_DATA_WR_ENA; |
| 206 | } |
| 207 | |
| 208 | static inline void __conn_data_poll_send(struct connection *c) |
| 209 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 210 | c->flags |= CO_FL_WAIT_WR | CO_FL_DATA_WR_ENA; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static inline void __conn_data_stop_both(struct connection *c) |
| 214 | { |
| 215 | c->flags &= ~(CO_FL_DATA_WR_ENA | CO_FL_DATA_RD_ENA); |
| 216 | } |
| 217 | |
| 218 | static inline void conn_data_want_recv(struct connection *c) |
| 219 | { |
| 220 | __conn_data_want_recv(c); |
| 221 | conn_cond_update_data_polling(c); |
| 222 | } |
| 223 | |
| 224 | static inline void conn_data_stop_recv(struct connection *c) |
| 225 | { |
| 226 | __conn_data_stop_recv(c); |
| 227 | conn_cond_update_data_polling(c); |
| 228 | } |
| 229 | |
| 230 | static inline void conn_data_poll_recv(struct connection *c) |
| 231 | { |
| 232 | __conn_data_poll_recv(c); |
| 233 | conn_cond_update_data_polling(c); |
| 234 | } |
| 235 | |
| 236 | static inline void conn_data_want_send(struct connection *c) |
| 237 | { |
| 238 | __conn_data_want_send(c); |
| 239 | conn_cond_update_data_polling(c); |
| 240 | } |
| 241 | |
| 242 | static inline void conn_data_stop_send(struct connection *c) |
| 243 | { |
| 244 | __conn_data_stop_send(c); |
| 245 | conn_cond_update_data_polling(c); |
| 246 | } |
| 247 | |
| 248 | static inline void conn_data_poll_send(struct connection *c) |
| 249 | { |
| 250 | __conn_data_poll_send(c); |
| 251 | conn_cond_update_data_polling(c); |
| 252 | } |
| 253 | |
| 254 | static inline void conn_data_stop_both(struct connection *c) |
| 255 | { |
| 256 | __conn_data_stop_both(c); |
| 257 | conn_cond_update_data_polling(c); |
| 258 | } |
| 259 | |
| 260 | /***** Event manipulation primitives for use by handshake I/O callbacks *****/ |
| 261 | /* The __conn_* versions do not propagate to lower layers and are only meant |
| 262 | * to be used by handlers called by the connection handler. The other ones |
| 263 | * may be used anywhere. |
| 264 | */ |
| 265 | static inline void __conn_sock_want_recv(struct connection *c) |
| 266 | { |
| 267 | c->flags |= CO_FL_SOCK_RD_ENA; |
| 268 | } |
| 269 | |
| 270 | static inline void __conn_sock_stop_recv(struct connection *c) |
| 271 | { |
| 272 | c->flags &= ~CO_FL_SOCK_RD_ENA; |
| 273 | } |
| 274 | |
| 275 | static inline void __conn_sock_poll_recv(struct connection *c) |
| 276 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 277 | c->flags |= CO_FL_WAIT_RD | CO_FL_SOCK_RD_ENA; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static inline void __conn_sock_want_send(struct connection *c) |
| 281 | { |
| 282 | c->flags |= CO_FL_SOCK_WR_ENA; |
| 283 | } |
| 284 | |
| 285 | static inline void __conn_sock_stop_send(struct connection *c) |
| 286 | { |
| 287 | c->flags &= ~CO_FL_SOCK_WR_ENA; |
| 288 | } |
| 289 | |
| 290 | static inline void __conn_sock_poll_send(struct connection *c) |
| 291 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 292 | c->flags |= CO_FL_WAIT_WR | CO_FL_SOCK_WR_ENA; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static inline void __conn_sock_stop_both(struct connection *c) |
| 296 | { |
| 297 | c->flags &= ~(CO_FL_SOCK_WR_ENA | CO_FL_SOCK_RD_ENA); |
| 298 | } |
| 299 | |
| 300 | static inline void conn_sock_want_recv(struct connection *c) |
| 301 | { |
| 302 | __conn_sock_want_recv(c); |
| 303 | conn_cond_update_sock_polling(c); |
| 304 | } |
| 305 | |
| 306 | static inline void conn_sock_stop_recv(struct connection *c) |
| 307 | { |
| 308 | __conn_sock_stop_recv(c); |
| 309 | conn_cond_update_sock_polling(c); |
| 310 | } |
| 311 | |
| 312 | static inline void conn_sock_poll_recv(struct connection *c) |
| 313 | { |
| 314 | __conn_sock_poll_recv(c); |
| 315 | conn_cond_update_sock_polling(c); |
| 316 | } |
| 317 | |
| 318 | static inline void conn_sock_want_send(struct connection *c) |
| 319 | { |
| 320 | __conn_sock_want_send(c); |
| 321 | conn_cond_update_sock_polling(c); |
| 322 | } |
| 323 | |
| 324 | static inline void conn_sock_stop_send(struct connection *c) |
| 325 | { |
| 326 | __conn_sock_stop_send(c); |
| 327 | conn_cond_update_sock_polling(c); |
| 328 | } |
| 329 | |
| 330 | static inline void conn_sock_poll_send(struct connection *c) |
| 331 | { |
| 332 | __conn_sock_poll_send(c); |
| 333 | conn_cond_update_sock_polling(c); |
| 334 | } |
| 335 | |
| 336 | static inline void conn_sock_stop_both(struct connection *c) |
| 337 | { |
| 338 | __conn_sock_stop_both(c); |
| 339 | conn_cond_update_sock_polling(c); |
| 340 | } |
Willy Tarreau | 8b11708 | 2012-08-06 15:06:49 +0200 | [diff] [blame] | 341 | |
Willy Tarreau | 3af56a9 | 2012-08-20 16:55:48 +0200 | [diff] [blame] | 342 | /* shutdown management */ |
| 343 | static inline void conn_sock_read0(struct connection *c) |
| 344 | { |
| 345 | c->flags |= CO_FL_SOCK_RD_SH; |
| 346 | __conn_sock_stop_recv(c); |
| 347 | } |
| 348 | |
| 349 | static inline void conn_data_read0(struct connection *c) |
| 350 | { |
| 351 | c->flags |= CO_FL_DATA_RD_SH; |
| 352 | __conn_data_stop_recv(c); |
| 353 | } |
| 354 | |
| 355 | static inline void conn_sock_shutw(struct connection *c) |
| 356 | { |
| 357 | c->flags |= CO_FL_SOCK_WR_SH; |
| 358 | __conn_sock_stop_send(c); |
| 359 | } |
| 360 | |
| 361 | static inline void conn_data_shutw(struct connection *c) |
| 362 | { |
| 363 | c->flags |= CO_FL_DATA_WR_SH; |
| 364 | __conn_data_stop_send(c); |
| 365 | } |
| 366 | |
| 367 | /* detect sock->data read0 transition */ |
| 368 | static inline int conn_data_read0_pending(struct connection *c) |
| 369 | { |
| 370 | return (c->flags & (CO_FL_DATA_RD_SH | CO_FL_SOCK_RD_SH)) == CO_FL_SOCK_RD_SH; |
| 371 | } |
| 372 | |
| 373 | /* detect data->sock shutw transition */ |
| 374 | static inline int conn_sock_shutw_pending(struct connection *c) |
| 375 | { |
| 376 | return (c->flags & (CO_FL_DATA_WR_SH | CO_FL_SOCK_WR_SH)) == CO_FL_DATA_WR_SH; |
| 377 | } |
| 378 | |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 379 | /* Retrieves the connection's source address */ |
| 380 | static inline void conn_get_from_addr(struct connection *conn) |
| 381 | { |
| 382 | if (conn->flags & CO_FL_ADDR_FROM_SET) |
| 383 | return; |
| 384 | |
| 385 | if (!conn->ctrl || !conn->ctrl->get_src) |
| 386 | return; |
| 387 | |
| 388 | 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] | 389 | sizeof(conn->addr.from), |
| 390 | obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1) |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 391 | return; |
| 392 | conn->flags |= CO_FL_ADDR_FROM_SET; |
| 393 | } |
| 394 | |
| 395 | /* Retrieves the connection's original destination address */ |
| 396 | static inline void conn_get_to_addr(struct connection *conn) |
| 397 | { |
| 398 | if (conn->flags & CO_FL_ADDR_TO_SET) |
| 399 | return; |
| 400 | |
| 401 | if (!conn->ctrl || !conn->ctrl->get_dst) |
| 402 | return; |
| 403 | |
| 404 | 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] | 405 | sizeof(conn->addr.to), |
| 406 | obj_type(conn->target) != OBJ_TYPE_LISTENER) == -1) |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 407 | return; |
| 408 | conn->flags |= CO_FL_ADDR_TO_SET; |
| 409 | } |
| 410 | |
Willy Tarreau | bd99aab | 2012-10-02 20:57:19 +0200 | [diff] [blame] | 411 | /* Assigns a connection with the appropriate data, ctrl, transport layers, and owner. */ |
| 412 | static inline void conn_assign(struct connection *conn, const struct data_cb *data, |
| 413 | const struct protocol *ctrl, const struct xprt_ops *xprt, |
| 414 | void *owner) |
Willy Tarreau | dda5e7c | 2012-09-24 17:15:42 +0200 | [diff] [blame] | 415 | { |
Willy Tarreau | 74beec3 | 2012-10-03 00:41:04 +0200 | [diff] [blame] | 416 | conn->data = data; |
Willy Tarreau | dda5e7c | 2012-09-24 17:15:42 +0200 | [diff] [blame] | 417 | conn->ctrl = ctrl; |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 418 | conn->xprt = xprt; |
Willy Tarreau | cd37995 | 2012-09-27 22:14:33 +0200 | [diff] [blame] | 419 | conn->owner = owner; |
Willy Tarreau | bd99aab | 2012-10-02 20:57:19 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /* prepares a connection with the appropriate data, ctrl, transport layers, and |
| 423 | * owner. The transport state and context are set to 0. |
| 424 | */ |
| 425 | static inline void conn_prepare(struct connection *conn, const struct data_cb *data, |
| 426 | const struct protocol *ctrl, const struct xprt_ops *xprt, |
| 427 | void *owner) |
| 428 | { |
| 429 | conn_assign(conn, data, ctrl, xprt, owner); |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 430 | conn->xprt_st = 0; |
| 431 | conn->xprt_ctx = NULL; |
Willy Tarreau | dda5e7c | 2012-09-24 17:15:42 +0200 | [diff] [blame] | 432 | } |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 433 | |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 434 | #endif /* _PROTO_CONNECTION_H */ |
| 435 | |
| 436 | /* |
| 437 | * Local variables: |
| 438 | * c-indent-level: 8 |
| 439 | * c-basic-offset: 8 |
| 440 | * End: |
| 441 | */ |