From eb4ec46de4d4e508d613b567db31383438b76af9 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Thu, 14 Sep 2006 15:17:28 +0000 Subject: [PATCH] Connection events are handled by hooks Every connection event (connect, close, read, write) is handled by a hook, so we can plugin hooks for every protocol, including protocol detection. --- Makefile.am | 2 +- improxy.c | 27 +++++++++++---------------- nethook.h | 24 ++++++++++++++++++++++++ proto_detect.c | 39 +++++++++++++++++++++++++++++++++++++++ proto_detect.h | 10 ++++++++++ 5 files changed, 85 insertions(+), 17 deletions(-) create mode 100644 nethook.h create mode 100644 proto_detect.c create mode 100644 proto_detect.h diff --git a/Makefile.am b/Makefile.am index 6302f7f..56a1cab 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,3 +1,3 @@ bin_PROGRAMS = improxy -improxy_SOURCES = improxy.c log.c log.h +improxy_SOURCES = improxy.c log.c log.h nethook.h proto_detect.c proto_detect.h sysconf_DATA = improxy.conf diff --git a/improxy.c b/improxy.c index 4c86384..6ab43c0 100644 --- a/improxy.c +++ b/improxy.c @@ -2,35 +2,28 @@ #include #include #include "log.h" +#include "nethook.h" +#include "proto_detect.h" #define CONFFILE SYSCONFDIR "/improxy.conf" void client_event (GConn* conn, GConnEvent* event, gpointer data) { - GConn* server; - server = (GConn*) data; + net_hook_t* hook; + hook = (net_hook_t*) data; switch (event->type) { case GNET_CONN_CONNECT: - g_debug ("Connected to localhost:80."); - gnet_conn_read (conn); + hook->connect (hook); break; case GNET_CONN_READ: - if (server == NULL) - { - g_debug ("Establishing connection to localhost:80."); - server = gnet_conn_new ("127.0.0.1", 80, client_event, (gpointer) conn); - gnet_conn_connect (server); - gnet_conn_set_callback (conn, client_event, (gpointer) server); - } - gnet_conn_write (server, event->buffer, event->length); - gnet_conn_read (conn); + hook->read (hook, event->buffer, event->length); break; case GNET_CONN_WRITE: + hook->write (hook); break; case GNET_CONN_CLOSE: - gnet_conn_unref (server); - gnet_conn_unref (conn); + hook->close (hook); break; default: g_warning ("Received an unexpected client event."); @@ -40,13 +33,15 @@ void client_event (GConn* conn, GConnEvent* event, gpointer data) void new_client (GServer* server, GConn* conn, gpointer data) { + net_hook_t* hook; if (conn == NULL) { g_critical ("Server has received an error event."); return; } g_message ("Received connection from %s.", conn->hostname); - gnet_conn_set_callback (conn, client_event, NULL); + hook = proto_detect_new (conn); + gnet_conn_set_callback (conn, client_event, hook); gnet_conn_read (conn); } diff --git a/nethook.h b/nethook.h new file mode 100644 index 0000000..2bf5035 --- /dev/null +++ b/nethook.h @@ -0,0 +1,24 @@ +#ifndef NET_HOOK_H +#define NET_HOOK_H + +#include + +typedef struct _net_hook_t net_hook_t; +typedef void (*net_connect) (net_hook_t*); +typedef void (*net_close) (net_hook_t*); +typedef void (*net_write) (net_hook_t*); +typedef void (*net_read) (net_hook_t*, gchar*, size_t); + +struct _net_hook_t +{ + GConn* conn; + GConn* peer; + gboolean server; + net_connect connect; + net_close close; + net_write write; + net_read read; + gpointer data; +}; + +#endif diff --git a/proto_detect.c b/proto_detect.c new file mode 100644 index 0000000..8fd790e --- /dev/null +++ b/proto_detect.c @@ -0,0 +1,39 @@ +#include +#include +#include "proto_detect.h" + +static void proto_connect (net_hook_t* hook) +{ +} + +static void proto_close (net_hook_t* hook) +{ +} + +static void proto_write (net_hook_t* hook) +{ +} + +static void proto_read (net_hook_t* hook, gchar* buffer, size_t len) +{ +} + +net_hook_t* proto_detect_new (GConn* conn) +{ + net_hook_t* hook; + hook = g_slice_new (net_hook_t); + hook->conn = conn; + hook->peer = NULL; + hook->server = FALSE; + hook->connect = proto_connect; + hook->close = proto_close; + hook->write = proto_write; + hook->read = proto_read; + hook->data = NULL; + return hook; +} + +void proto_detect_destroy (net_hook_t* hook) +{ + g_slice_free (net_hook_t, hook); +} diff --git a/proto_detect.h b/proto_detect.h new file mode 100644 index 0000000..56796e2 --- /dev/null +++ b/proto_detect.h @@ -0,0 +1,10 @@ +#ifndef PROTO_DETECT_H +#define PROTO_DETECT_H + +#include +#include "nethook.h" + +net_hook_t* proto_detect_new (GConn *conn); +void proto_detect_destroy (net_hook_t*); + +#endif -- 2.20.1