Connection events are handled by hooks
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Thu, 14 Sep 2006 15:17:28 +0000 (15:17 +0000)
committerThadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
Thu, 14 Sep 2006 15:17:28 +0000 (15:17 +0000)
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
improxy.c
nethook.h [new file with mode: 0644]
proto_detect.c [new file with mode: 0644]
proto_detect.h [new file with mode: 0644]

index 6302f7f..56a1cab 100644 (file)
@@ -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
index 4c86384..6ab43c0 100644 (file)
--- a/improxy.c
+++ b/improxy.c
@@ -2,35 +2,28 @@
 #include <gnet.h>
 #include <stdio.h>
 #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 (file)
index 0000000..2bf5035
--- /dev/null
+++ b/nethook.h
@@ -0,0 +1,24 @@
+#ifndef NET_HOOK_H
+#define NET_HOOK_H
+
+#include <gnet.h>
+
+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 (file)
index 0000000..8fd790e
--- /dev/null
@@ -0,0 +1,39 @@
+#include <gnet.h>
+#include <glib.h>
+#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 (file)
index 0000000..56796e2
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef PROTO_DETECT_H
+#define PROTO_DETECT_H
+
+#include <gnet.h>
+#include "nethook.h"
+
+net_hook_t* proto_detect_new (GConn *conn);
+void proto_detect_destroy (net_hook_t*);
+
+#endif