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