From 92d273cbba6ec73c80b77270910b83dfe3bbfab5 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Thu, 14 Sep 2006 15:52:58 +0000 Subject: [PATCH] Detects Jabber protocol and hook to it XMPP Stream is detected and another hook gets to handle the connection, which will parse the protocol and connect to the appropriate server. --- Makefile.am | 2 +- improxy.c | 25 ------------------------- jabber.c | 40 ++++++++++++++++++++++++++++++++++++++++ jabber.h | 10 ++++++++++ nethook.c | 26 ++++++++++++++++++++++++++ nethook.h | 2 ++ proto_detect.c | 32 +++++++++++++++++++++++++++++++- 7 files changed, 110 insertions(+), 27 deletions(-) create mode 100644 jabber.c create mode 100644 jabber.h create mode 100644 nethook.c diff --git a/Makefile.am b/Makefile.am index 56a1cab..766d2d0 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,3 +1,3 @@ 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 diff --git a/improxy.c b/improxy.c index 6ab43c0..f252dc3 100644 --- a/improxy.c +++ b/improxy.c @@ -7,30 +7,6 @@ #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; @@ -41,7 +17,6 @@ void new_client (GServer* server, GConn* conn, gpointer data) } 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); } diff --git a/jabber.c b/jabber.c new file mode 100644 index 0000000..c9340a2 --- /dev/null +++ b/jabber.c @@ -0,0 +1,40 @@ +#include +#include +#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); +} diff --git a/jabber.h b/jabber.h new file mode 100644 index 0000000..b4390af --- /dev/null +++ b/jabber.h @@ -0,0 +1,10 @@ +#ifndef JABBER_H +#define JABBER_H + +#include +#include "nethook.h" + +net_hook_t* jabber_hook_new (GConn *conn); +void jabber_hook_destroy (net_hook_t*); + +#endif diff --git a/nethook.c b/nethook.c new file mode 100644 index 0000000..1630d92 --- /dev/null +++ b/nethook.c @@ -0,0 +1,26 @@ +#include +#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; + } +} diff --git a/nethook.h b/nethook.h index 2bf5035..b896bb5 100644 --- a/nethook.h +++ b/nethook.h @@ -21,4 +21,6 @@ struct _net_hook_t gpointer data; }; +void nethook_event (GConn*, GConnEvent*, gpointer); + #endif diff --git a/proto_detect.c b/proto_detect.c index 8fd790e..afd8cde 100644 --- a/proto_detect.c +++ b/proto_detect.c @@ -1,6 +1,7 @@ #include #include #include "proto_detect.h" +#include "jabber.h" static void proto_connect (net_hook_t* hook) { @@ -16,6 +17,30 @@ static void proto_write (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, "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) @@ -29,11 +54,16 @@ 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); } -- 2.20.1