blob: c30e7395b6175a035c84fc729ba14c777965ccb8 [file] [log] [blame]
Willy Tarreau59f98392012-07-06 14:13:49 +02001/*
2 * Connection management functions
3 *
4 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/compat.h>
14#include <common/config.h>
15
16#include <types/connection.h>
Willy Tarreau2c6be842012-07-06 17:12:34 +020017
18#include <proto/stream_interface.h>
Willy Tarreau59f98392012-07-06 14:13:49 +020019
20/* I/O callback for fd-based connections. It calls the read/write handlers
21 * provided by the connection's sock_ops, which must be valid. It returns
22 * FD_WAIT_*.
23 */
24int conn_fd_handler(int fd)
25{
Willy Tarreau80184712012-07-06 14:54:49 +020026 struct connection *conn = fdtab[fd].owner;
Willy Tarreau59f98392012-07-06 14:13:49 +020027 int ret = 0;
28
Willy Tarreau80184712012-07-06 14:54:49 +020029 if (!conn)
Willy Tarreau2c6be842012-07-06 17:12:34 +020030 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020031
Willy Tarreau80184712012-07-06 14:54:49 +020032 if (conn->flags & CO_FL_ERROR)
Willy Tarreau2c6be842012-07-06 17:12:34 +020033 goto leave;
34
35 if (conn->flags & CO_FL_SI_SEND_PROXY)
36 if ((ret = conn_si_send_proxy(conn, CO_FL_SI_SEND_PROXY)))
37 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020038
39 if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
Willy Tarreau80184712012-07-06 14:54:49 +020040 if (!conn->data->read(fd))
Willy Tarreau59f98392012-07-06 14:13:49 +020041 ret |= FD_WAIT_READ;
42
Willy Tarreau80184712012-07-06 14:54:49 +020043 if (conn->flags & CO_FL_ERROR)
Willy Tarreau2c6be842012-07-06 17:12:34 +020044 goto leave;
Willy Tarreau59f98392012-07-06 14:13:49 +020045
46 if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
Willy Tarreau80184712012-07-06 14:54:49 +020047 if (!conn->data->write(fd))
Willy Tarreau59f98392012-07-06 14:13:49 +020048 ret |= FD_WAIT_WRITE;
Willy Tarreau2c6be842012-07-06 17:12:34 +020049 leave:
Willy Tarreau61ace1b2012-07-23 12:14:26 +020050 /* remove the events before leaving */
51 fdtab[fd].ev &= ~(FD_POLL_IN | FD_POLL_OUT | FD_POLL_HUP | FD_POLL_ERR);
Willy Tarreau59f98392012-07-06 14:13:49 +020052 return ret;
53}