MINOR: connection: add a handler for fd-based connections

This connection handler will be used as an I/O handler for events
detected on a file descriptor. It is not used yet.
diff --git a/Makefile b/Makefile
index 7cc1708..f73c8da 100644
--- a/Makefile
+++ b/Makefile
@@ -543,7 +543,7 @@
        src/uri_auth.o src/standard.o src/buffers.o src/log.o src/task.o \
        src/time.o src/fd.o src/pipe.o src/regex.o src/cfgparse.o src/server.o \
        src/checks.o src/queue.o src/frontend.o src/proxy.o src/peers.o \
-       src/arg.o src/stick_table.o src/proto_uxst.o \
+       src/arg.o src/stick_table.o src/proto_uxst.o src/connection.o \
        src/proto_http.o src/sock_raw.o src/appsession.o src/backend.o \
        src/lb_chash.o src/lb_fwlc.o src/lb_fwrr.o src/lb_map.o src/lb_fas.o \
        src/stream_interface.o src/dumpstats.o src/proto_tcp.o \
diff --git a/Makefile.bsd b/Makefile.bsd
index 369178d..acfdd2a 100644
--- a/Makefile.bsd
+++ b/Makefile.bsd
@@ -113,7 +113,7 @@
        src/peers.o src/stream_interface.o src/dumpstats.o src/proto_tcp.o \
        src/session.o src/hdr_idx.o src/ev_select.o src/signal.o \
        src/lb_chash.o src/lb_fwlc.o src/lb_fwrr.o src/lb_map.o src/lb_fas.o \
-       src/ev_poll.o src/ev_kqueue.o \
+       src/ev_poll.o src/ev_kqueue.o src/connection.o \
        src/arg.o src/acl.o src/memory.o src/freq_ctr.o \
        src/auth.o src/stick_table.o src/sample.o
 
diff --git a/Makefile.osx b/Makefile.osx
index 76da19e..31a7f99 100644
--- a/Makefile.osx
+++ b/Makefile.osx
@@ -110,7 +110,7 @@
        src/peers.o src/stream_interface.o src/dumpstats.o src/proto_tcp.o \
        src/session.o src/hdr_idx.o src/ev_select.o src/signal.o \
        src/lb_chash.o src/lb_fwlc.o src/lb_fwrr.o src/lb_map.o src/lb_fas.o \
-       src/ev_poll.o \
+       src/ev_poll.o src/connection.o \
        src/arg.o src/acl.o src/memory.o src/freq_ctr.o \
        src/auth.o src/stick_table.o src/sample.o
 
diff --git a/include/proto/connection.h b/include/proto/connection.h
new file mode 100644
index 0000000..b080449
--- /dev/null
+++ b/include/proto/connection.h
@@ -0,0 +1,40 @@
+/*
+ * include/proto/connection.h
+ * This file contains connection function prototypes
+ *
+ * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, version 2.1
+ * exclusively.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef _PROTO_CONNECTION_H
+#define _PROTO_CONNECTION_H
+
+#include <common/config.h>
+#include <types/connection.h>
+
+/* I/O callback for fd-based connections. It calls the read/write handlers
+ * provided by the connection's sock_ops. Returns FD_WAIT_*.
+ */
+int conn_fd_handler(int fd);
+
+#endif /* _PROTO_CONNECTION_H */
+
+/*
+ * Local variables:
+ *  c-indent-level: 8
+ *  c-basic-offset: 8
+ * End:
+ */
diff --git a/src/connection.c b/src/connection.c
new file mode 100644
index 0000000..a35e322
--- /dev/null
+++ b/src/connection.c
@@ -0,0 +1,46 @@
+/*
+ * Connection management functions
+ *
+ * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ *
+ */
+
+#include <common/compat.h>
+#include <common/config.h>
+
+#include <types/connection.h>
+#include <types/stream_interface.h>
+
+/* I/O callback for fd-based connections. It calls the read/write handlers
+ * provided by the connection's sock_ops, which must be valid. It returns
+ * FD_WAIT_*.
+ */
+int conn_fd_handler(int fd)
+{
+	struct stream_interface *si = fdtab[fd].owner;
+	int ret = 0;
+
+	if (!si)
+		return ret;
+
+	if (si->conn.flags & CO_FL_ERROR)
+		return ret;
+
+	if (fdtab[fd].ev & (FD_POLL_IN | FD_POLL_HUP | FD_POLL_ERR))
+		if (!si->conn.data->read(fd))
+			ret |= FD_WAIT_READ;
+
+	if (si->conn.flags & CO_FL_ERROR)
+		return ret;
+
+	if (fdtab[fd].ev & (FD_POLL_OUT | FD_POLL_ERR))
+		if (!si->conn.data->write(fd))
+			ret |= FD_WAIT_WRITE;
+
+	return ret;
+}