bin_PROGRAMS = improxy
-improxy_SOURCES = improxy.c log.c log.h nethook.h proto_detect.c proto_detect.h
+improxy_SOURCES = improxy.c log.c log.h nethook.c nethook.h proto_detect.c proto_detect.h jabber.c jabber.h
sysconf_DATA = improxy.conf
#define CONFFILE SYSCONFDIR "/improxy.conf"
-void client_event (GConn* conn, GConnEvent* event, gpointer data)
-{
- net_hook_t* hook;
- hook = (net_hook_t*) data;
- switch (event->type)
- {
- case GNET_CONN_CONNECT:
- hook->connect (hook);
- break;
- case GNET_CONN_READ:
- hook->read (hook, event->buffer, event->length);
- break;
- case GNET_CONN_WRITE:
- hook->write (hook);
- break;
- case GNET_CONN_CLOSE:
- hook->close (hook);
- break;
- default:
- g_warning ("Received an unexpected client event.");
- break;
- }
-}
-
void new_client (GServer* server, GConn* conn, gpointer data)
{
net_hook_t* hook;
}
g_message ("Received connection from %s.", conn->hostname);
hook = proto_detect_new (conn);
- gnet_conn_set_callback (conn, client_event, hook);
gnet_conn_read (conn);
}
--- /dev/null
+#include <gnet.h>
+#include <glib.h>
+#include "jabber.h"
+
+static void jabber_connect (net_hook_t* hook)
+{
+}
+
+static void jabber_close (net_hook_t* hook)
+{
+}
+
+static void jabber_write (net_hook_t* hook)
+{
+}
+
+static void jabber_read (net_hook_t* hook, gchar* buffer, size_t len)
+{
+}
+
+net_hook_t* jabber_hook_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 = jabber_connect;
+ hook->close = jabber_close;
+ hook->write = jabber_write;
+ hook->read = jabber_read;
+ hook->data = NULL;
+ gnet_conn_set_callback (hook->conn, nethook_event, hook);
+ return hook;
+}
+
+void jabber_destroy (net_hook_t* hook)
+{
+ g_slice_free (net_hook_t, hook);
+}
--- /dev/null
+#ifndef JABBER_H
+#define JABBER_H
+
+#include <gnet.h>
+#include "nethook.h"
+
+net_hook_t* jabber_hook_new (GConn *conn);
+void jabber_hook_destroy (net_hook_t*);
+
+#endif
--- /dev/null
+#include <gnet.h>
+#include "nethook.h"
+
+void nethook_event (GConn* conn, GConnEvent* event, gpointer data)
+{
+ net_hook_t* hook;
+ hook = (net_hook_t*) data;
+ switch (event->type)
+ {
+ case GNET_CONN_CONNECT:
+ hook->connect (hook);
+ break;
+ case GNET_CONN_READ:
+ hook->read (hook, event->buffer, event->length);
+ break;
+ case GNET_CONN_WRITE:
+ hook->write (hook);
+ break;
+ case GNET_CONN_CLOSE:
+ hook->close (hook);
+ break;
+ default:
+ g_warning ("Received an unexpected client event.");
+ break;
+ }
+}
gpointer data;
};
+void nethook_event (GConn*, GConnEvent*, gpointer);
+
#endif
#include <gnet.h>
#include <glib.h>
#include "proto_detect.h"
+#include "jabber.h"
static void proto_connect (net_hook_t* hook)
{
static void proto_read (net_hook_t* hook, gchar* buffer, size_t len)
{
+ net_hook_t* new_hook;
+ GString* str;
+ str = (GString*) hook->data;
+ g_string_append_len (str, buffer, len);
+ if (str->len >= 7)
+ {
+ if (!strncmp (str->str, "<stream", 7))
+ {
+ /* Connection is a Jabber client */
+ g_debug ("Connection from %s is a Jabber client.",
+ hook->conn->hostname);
+ new_hook = jabber_hook_new (hook->conn);
+ new_hook->read (new_hook, str->str, str->len);
+ proto_detect_destroy (hook);
+ }
+ else
+ {
+ g_debug ("Unrecognized protocol from %s.",
+ hook->conn->hostname);
+ gnet_conn_disconnect (hook->conn);
+ gnet_conn_unref (hook->conn);
+ proto_detect_destroy (hook);
+ }
+ }
}
net_hook_t* proto_detect_new (GConn* conn)
hook->close = proto_close;
hook->write = proto_write;
hook->read = proto_read;
- hook->data = NULL;
+ hook->data = g_string_sized_new (128);
+ gnet_conn_set_callback (hook->conn, nethook_event, hook);
return hook;
}
void proto_detect_destroy (net_hook_t* hook)
{
+ if (hook->data != NULL)
+ {
+ g_string_free (hook->data, TRUE);
+ }
g_slice_free (net_hook_t, hook);
}