#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.");
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);
}
--- /dev/null
+#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
--- /dev/null
+#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);
+}