Detects Jabber protocol and hook to it
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Thu, 14 Sep 2006 15:52:58 +0000 (15:52 +0000)
committerThadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
Thu, 14 Sep 2006 15:52:58 +0000 (15:52 +0000)
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
improxy.c
jabber.c [new file with mode: 0644]
jabber.h [new file with mode: 0644]
nethook.c [new file with mode: 0644]
nethook.h
proto_detect.c

index 56a1cab..766d2d0 100644 (file)
@@ -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
index 6ab43c0..f252dc3 100644 (file)
--- 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 (file)
index 0000000..c9340a2
--- /dev/null
+++ b/jabber.c
@@ -0,0 +1,40 @@
+#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);
+}
diff --git a/jabber.h b/jabber.h
new file mode 100644 (file)
index 0000000..b4390af
--- /dev/null
+++ b/jabber.h
@@ -0,0 +1,10 @@
+#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
diff --git a/nethook.c b/nethook.c
new file mode 100644 (file)
index 0000000..1630d92
--- /dev/null
+++ b/nethook.c
@@ -0,0 +1,26 @@
+#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;
+    }
+}
index 2bf5035..b896bb5 100644 (file)
--- a/nethook.h
+++ b/nethook.h
@@ -21,4 +21,6 @@ struct _net_hook_t
   gpointer data;
 };
 
+void nethook_event (GConn*, GConnEvent*, gpointer);
+
 #endif
index 8fd790e..afd8cde 100644 (file)
@@ -1,6 +1,7 @@
 #include <gnet.h>
 #include <glib.h>
 #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, "<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)
@@ -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);
 }