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