blob: 43f02d726752aaa598d95ad36c17855b0b252379 [file] [log] [blame]
Willy Tarreau59f98392012-07-06 14:13:49 +02001/*
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 Tarreauafad0e02012-08-09 14:45:22 +020029 * provided by the connection's sock_ops. Returns 0.
Willy Tarreau59f98392012-07-06 14:13:49 +020030 */
31int conn_fd_handler(int fd);
32
Willy Tarreau22cda212012-08-31 17:43:29 +020033/* receive a PROXY protocol header over a connection */
34int conn_recv_proxy(struct connection *conn, int flag);
35
Willy Tarreau15678ef2012-08-31 13:54:11 +020036/* calls the init() function of the data layer if any. Returns <0 in case of
37 * error.
38 */
39static 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 Tarreau8b117082012-08-06 15:06:49 +020046/* Calls the close() function of the data layer if any */
47static inline void conn_data_close(struct connection *conn)
48{
Willy Tarreau4a36b562012-08-06 19:31:45 +020049 if (conn->data && conn->data->close)
Willy Tarreau8b117082012-08-06 15:06:49 +020050 conn->data->close(conn);
51}
52
Willy Tarreaue9dfa792012-09-01 17:26:16 +020053/* 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 Tarreaub5e2cbd2012-08-17 11:55:04 +020058 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +020059void conn_update_sock_polling(struct connection *c);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +020060
Willy Tarreaue9dfa792012-09-01 17:26:16 +020061/* 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 Tarreaub5e2cbd2012-08-17 11:55:04 +020066 */
Willy Tarreaue9dfa792012-09-01 17:26:16 +020067void conn_update_data_polling(struct connection *c);
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +020068
Willy Tarreaue9dfa792012-09-01 17:26:16 +020069/* 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 Tarreaub5e2cbd2012-08-17 11:55:04 +020071 */
72static inline unsigned int conn_data_polling_changes(const struct connection *c)
73{
Willy Tarreaue9dfa792012-09-01 17:26:16 +020074 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 Tarreaub5e2cbd2012-08-17 11:55:04 +020077}
78
Willy Tarreaue9dfa792012-09-01 17:26:16 +020079/* 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 Tarreaub5e2cbd2012-08-17 11:55:04 +020081 */
82static inline unsigned int conn_sock_polling_changes(const struct connection *c)
83{
Willy Tarreaue9dfa792012-09-01 17:26:16 +020084 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 Tarreaub5e2cbd2012-08-17 11:55:04 +020087}
88
89/* Automatically updates polling on connection <c> depending on the DATA flags
90 * if no handshake is in progress.
91 */
92static 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 */
101static 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 */
112static 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 */
125static inline void __conn_data_want_recv(struct connection *c)
126{
127 c->flags |= CO_FL_DATA_RD_ENA;
128}
129
130static inline void __conn_data_stop_recv(struct connection *c)
131{
132 c->flags &= ~CO_FL_DATA_RD_ENA;
133}
134
135static inline void __conn_data_poll_recv(struct connection *c)
136{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200137 c->flags |= CO_FL_WAIT_RD | CO_FL_DATA_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200138}
139
140static inline void __conn_data_want_send(struct connection *c)
141{
142 c->flags |= CO_FL_DATA_WR_ENA;
143}
144
145static inline void __conn_data_stop_send(struct connection *c)
146{
147 c->flags &= ~CO_FL_DATA_WR_ENA;
148}
149
150static inline void __conn_data_poll_send(struct connection *c)
151{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200152 c->flags |= CO_FL_WAIT_WR | CO_FL_DATA_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200153}
154
155static 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
160static 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
166static 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
172static 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
178static 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
184static 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
190static 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
196static 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 */
207static inline void __conn_sock_want_recv(struct connection *c)
208{
209 c->flags |= CO_FL_SOCK_RD_ENA;
210}
211
212static inline void __conn_sock_stop_recv(struct connection *c)
213{
214 c->flags &= ~CO_FL_SOCK_RD_ENA;
215}
216
217static inline void __conn_sock_poll_recv(struct connection *c)
218{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200219 c->flags |= CO_FL_WAIT_RD | CO_FL_SOCK_RD_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200220}
221
222static inline void __conn_sock_want_send(struct connection *c)
223{
224 c->flags |= CO_FL_SOCK_WR_ENA;
225}
226
227static inline void __conn_sock_stop_send(struct connection *c)
228{
229 c->flags &= ~CO_FL_SOCK_WR_ENA;
230}
231
232static inline void __conn_sock_poll_send(struct connection *c)
233{
Willy Tarreaue9dfa792012-09-01 17:26:16 +0200234 c->flags |= CO_FL_WAIT_WR | CO_FL_SOCK_WR_ENA;
Willy Tarreaub5e2cbd2012-08-17 11:55:04 +0200235}
236
237static 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
242static 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
248static 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
254static 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
260static 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
266static 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
272static 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
278static 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 Tarreau8b117082012-08-06 15:06:49 +0200283
Willy Tarreau3af56a92012-08-20 16:55:48 +0200284/* shutdown management */
285static 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
291static 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
297static 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
303static 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 */
310static 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 */
316static 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 Tarreau3cefd522012-08-30 15:49:18 +0200321static inline void clear_target(struct target *dest)
322{
323 dest->type = TARG_TYPE_NONE;
324 dest->ptr.v = NULL;
325}
326
327static 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
333static 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
339static 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
345static 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
351static 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
357static inline struct target *copy_target(struct target *dest, struct target *src)
358{
359 *dest = *src;
360 return dest;
361}
362
363static 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
368static 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
375static 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 Tarreau986a9d22012-08-30 21:11:38 +0200382/* Retrieves the connection's source address */
383static 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 */
399static 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 Tarreau59f98392012-07-06 14:13:49 +0200415#endif /* _PROTO_CONNECTION_H */
416
417/*
418 * Local variables:
419 * c-indent-level: 8
420 * c-basic-offset: 8
421 * End:
422 */