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> |
| 27 | |
| 28 | /* 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] | 29 | * provided by the connection's sock_ops. Returns 0. |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 30 | */ |
| 31 | int conn_fd_handler(int fd); |
| 32 | |
Willy Tarreau | 22cda21 | 2012-08-31 17:43:29 +0200 | [diff] [blame] | 33 | /* receive a PROXY protocol header over a connection */ |
| 34 | int conn_recv_proxy(struct connection *conn, int flag); |
| 35 | |
Willy Tarreau | 15678ef | 2012-08-31 13:54:11 +0200 | [diff] [blame] | 36 | /* calls the init() function of the data layer if any. Returns <0 in case of |
| 37 | * error. |
| 38 | */ |
| 39 | static inline int conn_data_init(struct connection *conn) |
| 40 | { |
| 41 | if (conn->data && conn->data->init) |
| 42 | return conn->data->init(conn); |
| 43 | return 0; |
| 44 | } |
| 45 | |
Willy Tarreau | 8b11708 | 2012-08-06 15:06:49 +0200 | [diff] [blame] | 46 | /* Calls the close() function of the data layer if any */ |
| 47 | static inline void conn_data_close(struct connection *conn) |
| 48 | { |
Willy Tarreau | 4a36b56 | 2012-08-06 19:31:45 +0200 | [diff] [blame] | 49 | if (conn->data && conn->data->close) |
Willy Tarreau | 8b11708 | 2012-08-06 15:06:49 +0200 | [diff] [blame] | 50 | conn->data->close(conn); |
| 51 | } |
| 52 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 53 | /* Update polling on connection <c>'s file descriptor depending on its current |
| 54 | * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN |
| 55 | * in CO_FL_WAIT_*, and the sock layer expectations indicated by CO_FL_SOCK_*. |
| 56 | * The connection flags are updated with the new flags at the end of the |
| 57 | * operation. |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 58 | */ |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 59 | void conn_update_sock_polling(struct connection *c); |
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 | /* Update polling on connection <c>'s file descriptor depending on its current |
| 62 | * state as reported in the connection's CO_FL_CURR_* flags, reports of EAGAIN |
| 63 | * in CO_FL_WAIT_*, and the data layer expectations indicated by CO_FL_DATA_*. |
| 64 | * The connection flags are updated with the new flags at the end of the |
| 65 | * operation. |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 66 | */ |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 67 | void conn_update_data_polling(struct connection *c); |
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 | /* inspects c->flags and returns non-zero if DATA ENA changes from the CURR ENA |
| 70 | * or if the WAIT flags set new flags that were not in CURR POL. |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 71 | */ |
| 72 | static inline unsigned int conn_data_polling_changes(const struct connection *c) |
| 73 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 74 | return (((c->flags << 6) ^ (c->flags << 2)) | /* changes in ENA go to bits 30&31 */ |
| 75 | (((c->flags << 8) & ~c->flags))) & /* new bits in POL go to bits 30&31 */ |
| 76 | 0xC0000000; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 77 | } |
| 78 | |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 79 | /* inspects c->flags and returns non-zero if SOCK ENA changes from the CURR ENA |
| 80 | * or if the WAIT flags set new flags that were not in CURR POL. |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 81 | */ |
| 82 | static inline unsigned int conn_sock_polling_changes(const struct connection *c) |
| 83 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 84 | return (((c->flags << 4) ^ (c->flags << 2)) | /* changes in ENA go to bits 30&31 */ |
| 85 | (((c->flags << 8) & ~c->flags))) & /* new bits in POL go to bits 30&31 */ |
| 86 | 0xC0000000; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* Automatically updates polling on connection <c> depending on the DATA flags |
| 90 | * if no handshake is in progress. |
| 91 | */ |
| 92 | static inline void conn_cond_update_data_polling(struct connection *c) |
| 93 | { |
| 94 | if (!(c->flags & CO_FL_POLL_SOCK) && conn_data_polling_changes(c)) |
| 95 | conn_update_data_polling(c); |
| 96 | } |
| 97 | |
| 98 | /* Automatically updates polling on connection <c> depending on the SOCK flags |
| 99 | * if a handshake is in progress. |
| 100 | */ |
| 101 | static inline void conn_cond_update_sock_polling(struct connection *c) |
| 102 | { |
| 103 | if ((c->flags & CO_FL_POLL_SOCK) && conn_sock_polling_changes(c)) |
| 104 | conn_update_sock_polling(c); |
| 105 | } |
| 106 | |
| 107 | /* Automatically update polling on connection <c> depending on the DATA and |
| 108 | * SOCK flags, and on whether a handshake is in progress or not. This may be |
| 109 | * called at any moment when there is a doubt about the effectiveness of the |
| 110 | * polling state, for instance when entering or leaving the handshake state. |
| 111 | */ |
| 112 | static inline void conn_cond_update_polling(struct connection *c) |
| 113 | { |
| 114 | if (!(c->flags & CO_FL_POLL_SOCK) && conn_data_polling_changes(c)) |
| 115 | conn_update_data_polling(c); |
| 116 | else if ((c->flags & CO_FL_POLL_SOCK) && conn_sock_polling_changes(c)) |
| 117 | conn_update_sock_polling(c); |
| 118 | } |
| 119 | |
| 120 | /***** Event manipulation primitives for use by DATA I/O callbacks *****/ |
| 121 | /* The __conn_* versions do not propagate to lower layers and are only meant |
| 122 | * to be used by handlers called by the connection handler. The other ones |
| 123 | * may be used anywhere. |
| 124 | */ |
| 125 | static inline void __conn_data_want_recv(struct connection *c) |
| 126 | { |
| 127 | c->flags |= CO_FL_DATA_RD_ENA; |
| 128 | } |
| 129 | |
| 130 | static inline void __conn_data_stop_recv(struct connection *c) |
| 131 | { |
| 132 | c->flags &= ~CO_FL_DATA_RD_ENA; |
| 133 | } |
| 134 | |
| 135 | static inline void __conn_data_poll_recv(struct connection *c) |
| 136 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 137 | c->flags |= CO_FL_WAIT_RD | CO_FL_DATA_RD_ENA; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static inline void __conn_data_want_send(struct connection *c) |
| 141 | { |
| 142 | c->flags |= CO_FL_DATA_WR_ENA; |
| 143 | } |
| 144 | |
| 145 | static inline void __conn_data_stop_send(struct connection *c) |
| 146 | { |
| 147 | c->flags &= ~CO_FL_DATA_WR_ENA; |
| 148 | } |
| 149 | |
| 150 | static inline void __conn_data_poll_send(struct connection *c) |
| 151 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 152 | c->flags |= CO_FL_WAIT_WR | CO_FL_DATA_WR_ENA; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static inline void __conn_data_stop_both(struct connection *c) |
| 156 | { |
| 157 | c->flags &= ~(CO_FL_DATA_WR_ENA | CO_FL_DATA_RD_ENA); |
| 158 | } |
| 159 | |
| 160 | static inline void conn_data_want_recv(struct connection *c) |
| 161 | { |
| 162 | __conn_data_want_recv(c); |
| 163 | conn_cond_update_data_polling(c); |
| 164 | } |
| 165 | |
| 166 | static inline void conn_data_stop_recv(struct connection *c) |
| 167 | { |
| 168 | __conn_data_stop_recv(c); |
| 169 | conn_cond_update_data_polling(c); |
| 170 | } |
| 171 | |
| 172 | static inline void conn_data_poll_recv(struct connection *c) |
| 173 | { |
| 174 | __conn_data_poll_recv(c); |
| 175 | conn_cond_update_data_polling(c); |
| 176 | } |
| 177 | |
| 178 | static inline void conn_data_want_send(struct connection *c) |
| 179 | { |
| 180 | __conn_data_want_send(c); |
| 181 | conn_cond_update_data_polling(c); |
| 182 | } |
| 183 | |
| 184 | static inline void conn_data_stop_send(struct connection *c) |
| 185 | { |
| 186 | __conn_data_stop_send(c); |
| 187 | conn_cond_update_data_polling(c); |
| 188 | } |
| 189 | |
| 190 | static inline void conn_data_poll_send(struct connection *c) |
| 191 | { |
| 192 | __conn_data_poll_send(c); |
| 193 | conn_cond_update_data_polling(c); |
| 194 | } |
| 195 | |
| 196 | static inline void conn_data_stop_both(struct connection *c) |
| 197 | { |
| 198 | __conn_data_stop_both(c); |
| 199 | conn_cond_update_data_polling(c); |
| 200 | } |
| 201 | |
| 202 | /***** Event manipulation primitives for use by handshake I/O callbacks *****/ |
| 203 | /* The __conn_* versions do not propagate to lower layers and are only meant |
| 204 | * to be used by handlers called by the connection handler. The other ones |
| 205 | * may be used anywhere. |
| 206 | */ |
| 207 | static inline void __conn_sock_want_recv(struct connection *c) |
| 208 | { |
| 209 | c->flags |= CO_FL_SOCK_RD_ENA; |
| 210 | } |
| 211 | |
| 212 | static inline void __conn_sock_stop_recv(struct connection *c) |
| 213 | { |
| 214 | c->flags &= ~CO_FL_SOCK_RD_ENA; |
| 215 | } |
| 216 | |
| 217 | static inline void __conn_sock_poll_recv(struct connection *c) |
| 218 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 219 | c->flags |= CO_FL_WAIT_RD | CO_FL_SOCK_RD_ENA; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static inline void __conn_sock_want_send(struct connection *c) |
| 223 | { |
| 224 | c->flags |= CO_FL_SOCK_WR_ENA; |
| 225 | } |
| 226 | |
| 227 | static inline void __conn_sock_stop_send(struct connection *c) |
| 228 | { |
| 229 | c->flags &= ~CO_FL_SOCK_WR_ENA; |
| 230 | } |
| 231 | |
| 232 | static inline void __conn_sock_poll_send(struct connection *c) |
| 233 | { |
Willy Tarreau | e9dfa79 | 2012-09-01 17:26:16 +0200 | [diff] [blame] | 234 | c->flags |= CO_FL_WAIT_WR | CO_FL_SOCK_WR_ENA; |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static inline void __conn_sock_stop_both(struct connection *c) |
| 238 | { |
| 239 | c->flags &= ~(CO_FL_SOCK_WR_ENA | CO_FL_SOCK_RD_ENA); |
| 240 | } |
| 241 | |
| 242 | static inline void conn_sock_want_recv(struct connection *c) |
| 243 | { |
| 244 | __conn_sock_want_recv(c); |
| 245 | conn_cond_update_sock_polling(c); |
| 246 | } |
| 247 | |
| 248 | static inline void conn_sock_stop_recv(struct connection *c) |
| 249 | { |
| 250 | __conn_sock_stop_recv(c); |
| 251 | conn_cond_update_sock_polling(c); |
| 252 | } |
| 253 | |
| 254 | static inline void conn_sock_poll_recv(struct connection *c) |
| 255 | { |
| 256 | __conn_sock_poll_recv(c); |
| 257 | conn_cond_update_sock_polling(c); |
| 258 | } |
| 259 | |
| 260 | static inline void conn_sock_want_send(struct connection *c) |
| 261 | { |
| 262 | __conn_sock_want_send(c); |
| 263 | conn_cond_update_sock_polling(c); |
| 264 | } |
| 265 | |
| 266 | static inline void conn_sock_stop_send(struct connection *c) |
| 267 | { |
| 268 | __conn_sock_stop_send(c); |
| 269 | conn_cond_update_sock_polling(c); |
| 270 | } |
| 271 | |
| 272 | static inline void conn_sock_poll_send(struct connection *c) |
| 273 | { |
| 274 | __conn_sock_poll_send(c); |
| 275 | conn_cond_update_sock_polling(c); |
| 276 | } |
| 277 | |
| 278 | static inline void conn_sock_stop_both(struct connection *c) |
| 279 | { |
| 280 | __conn_sock_stop_both(c); |
| 281 | conn_cond_update_sock_polling(c); |
| 282 | } |
Willy Tarreau | 8b11708 | 2012-08-06 15:06:49 +0200 | [diff] [blame] | 283 | |
Willy Tarreau | 3af56a9 | 2012-08-20 16:55:48 +0200 | [diff] [blame] | 284 | /* shutdown management */ |
| 285 | static inline void conn_sock_read0(struct connection *c) |
| 286 | { |
| 287 | c->flags |= CO_FL_SOCK_RD_SH; |
| 288 | __conn_sock_stop_recv(c); |
| 289 | } |
| 290 | |
| 291 | static inline void conn_data_read0(struct connection *c) |
| 292 | { |
| 293 | c->flags |= CO_FL_DATA_RD_SH; |
| 294 | __conn_data_stop_recv(c); |
| 295 | } |
| 296 | |
| 297 | static inline void conn_sock_shutw(struct connection *c) |
| 298 | { |
| 299 | c->flags |= CO_FL_SOCK_WR_SH; |
| 300 | __conn_sock_stop_send(c); |
| 301 | } |
| 302 | |
| 303 | static inline void conn_data_shutw(struct connection *c) |
| 304 | { |
| 305 | c->flags |= CO_FL_DATA_WR_SH; |
| 306 | __conn_data_stop_send(c); |
| 307 | } |
| 308 | |
| 309 | /* detect sock->data read0 transition */ |
| 310 | static inline int conn_data_read0_pending(struct connection *c) |
| 311 | { |
| 312 | return (c->flags & (CO_FL_DATA_RD_SH | CO_FL_SOCK_RD_SH)) == CO_FL_SOCK_RD_SH; |
| 313 | } |
| 314 | |
| 315 | /* detect data->sock shutw transition */ |
| 316 | static inline int conn_sock_shutw_pending(struct connection *c) |
| 317 | { |
| 318 | return (c->flags & (CO_FL_DATA_WR_SH | CO_FL_SOCK_WR_SH)) == CO_FL_DATA_WR_SH; |
| 319 | } |
| 320 | |
Willy Tarreau | 3cefd52 | 2012-08-30 15:49:18 +0200 | [diff] [blame] | 321 | static inline void clear_target(struct target *dest) |
| 322 | { |
| 323 | dest->type = TARG_TYPE_NONE; |
| 324 | dest->ptr.v = NULL; |
| 325 | } |
| 326 | |
| 327 | static inline void set_target_client(struct target *dest, struct listener *l) |
| 328 | { |
| 329 | dest->type = TARG_TYPE_CLIENT; |
| 330 | dest->ptr.l = l; |
| 331 | } |
| 332 | |
| 333 | static inline void set_target_server(struct target *dest, struct server *s) |
| 334 | { |
| 335 | dest->type = TARG_TYPE_SERVER; |
| 336 | dest->ptr.s = s; |
| 337 | } |
| 338 | |
| 339 | static inline void set_target_proxy(struct target *dest, struct proxy *p) |
| 340 | { |
| 341 | dest->type = TARG_TYPE_PROXY; |
| 342 | dest->ptr.p = p; |
| 343 | } |
| 344 | |
| 345 | static inline void set_target_applet(struct target *dest, struct si_applet *a) |
| 346 | { |
| 347 | dest->type = TARG_TYPE_APPLET; |
| 348 | dest->ptr.a = a; |
| 349 | } |
| 350 | |
| 351 | static inline void set_target_task(struct target *dest, struct task *t) |
| 352 | { |
| 353 | dest->type = TARG_TYPE_TASK; |
| 354 | dest->ptr.t = t; |
| 355 | } |
| 356 | |
| 357 | static inline struct target *copy_target(struct target *dest, struct target *src) |
| 358 | { |
| 359 | *dest = *src; |
| 360 | return dest; |
| 361 | } |
| 362 | |
| 363 | static inline int target_match(struct target *a, struct target *b) |
| 364 | { |
| 365 | return a->type == b->type && a->ptr.v == b->ptr.v; |
| 366 | } |
| 367 | |
| 368 | static inline struct server *target_srv(struct target *t) |
| 369 | { |
| 370 | if (!t || t->type != TARG_TYPE_SERVER) |
| 371 | return NULL; |
| 372 | return t->ptr.s; |
| 373 | } |
| 374 | |
| 375 | static inline struct listener *target_client(struct target *t) |
| 376 | { |
| 377 | if (!t || t->type != TARG_TYPE_CLIENT) |
| 378 | return NULL; |
| 379 | return t->ptr.l; |
| 380 | } |
| 381 | |
Willy Tarreau | 986a9d2 | 2012-08-30 21:11:38 +0200 | [diff] [blame] | 382 | /* Retrieves the connection's source address */ |
| 383 | static inline void conn_get_from_addr(struct connection *conn) |
| 384 | { |
| 385 | if (conn->flags & CO_FL_ADDR_FROM_SET) |
| 386 | return; |
| 387 | |
| 388 | if (!conn->ctrl || !conn->ctrl->get_src) |
| 389 | return; |
| 390 | |
| 391 | if (conn->ctrl->get_src(conn->t.sock.fd, (struct sockaddr *)&conn->addr.from, |
| 392 | sizeof(conn->addr.from), |
| 393 | conn->target.type != TARG_TYPE_CLIENT) == -1) |
| 394 | return; |
| 395 | conn->flags |= CO_FL_ADDR_FROM_SET; |
| 396 | } |
| 397 | |
| 398 | /* Retrieves the connection's original destination address */ |
| 399 | static inline void conn_get_to_addr(struct connection *conn) |
| 400 | { |
| 401 | if (conn->flags & CO_FL_ADDR_TO_SET) |
| 402 | return; |
| 403 | |
| 404 | if (!conn->ctrl || !conn->ctrl->get_dst) |
| 405 | return; |
| 406 | |
| 407 | if (conn->ctrl->get_dst(conn->t.sock.fd, (struct sockaddr *)&conn->addr.to, |
| 408 | sizeof(conn->addr.to), |
| 409 | conn->target.type != TARG_TYPE_CLIENT) == -1) |
| 410 | return; |
| 411 | conn->flags |= CO_FL_ADDR_TO_SET; |
| 412 | } |
| 413 | |
| 414 | |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 415 | #endif /* _PROTO_CONNECTION_H */ |
| 416 | |
| 417 | /* |
| 418 | * Local variables: |
| 419 | * c-indent-level: 8 |
| 420 | * c-basic-offset: 8 |
| 421 | * End: |
| 422 | */ |