Recognizes jabber server, connects to it and proxies data
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Fri, 22 Sep 2006 19:47:21 +0000 (19:47 +0000)
committerThadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
Fri, 22 Sep 2006 19:47:21 +0000 (19:47 +0000)
The jabber server the client is connecting to is identified in the
data sent by it. It connects to this server and sends all data from
client to the server and all data from server to the client.

Makefile.am
jabber.c
jabber.h
jabber_server.c [new file with mode: 0644]

index 766d2d0..4bd1a66 100644 (file)
@@ -1,3 +1,3 @@
 bin_PROGRAMS = improxy
-improxy_SOURCES = improxy.c log.c log.h nethook.c nethook.h proto_detect.c proto_detect.h jabber.c jabber.h
+improxy_SOURCES = improxy.c log.c log.h nethook.c nethook.h proto_detect.c proto_detect.h jabber.c jabber.h jabber_server.c
 sysconf_DATA = improxy.conf
index 7df9633..41d20a5 100644 (file)
--- a/jabber.c
+++ b/jabber.c
@@ -19,6 +19,7 @@
 
 #include <gnet.h>
 #include <glib.h>
+#include <iksemel.h>
 #include "jabber.h"
 
 static void jabber_connect (net_hook_t* hook)
@@ -35,6 +36,73 @@ static void jabber_write (net_hook_t* hook)
 
 static void jabber_read (net_hook_t* hook, gchar* buffer, size_t len)
 {
+  iks_parse (hook->data, buffer, len, FALSE);
+}
+
+void jabber_error (net_hook_t* hook)
+{
+  g_message ("Error in jabber client connection.");
+}
+
+GString* jabber_new_start (iks* node)
+{
+  GString* buffer;
+  iks* attrib;
+  buffer = g_string_sized_new (256);
+  g_string_append (buffer, iks_string (iks_stack (node), node));
+  g_string_erase (buffer, buffer->len - 2, 1);
+  return buffer;
+}
+
+void jabber_connect_server (net_hook_t* hook, char* server, iks* node)
+{
+  net_hook_t* server_hook;
+  GString* buffer;
+  server_hook = jabber_server_hook_new (hook, server);
+  hook->peer = server_hook->conn;
+  g_message ("Trying to connect to server %s.", server);
+  gnet_conn_connect (hook->peer);
+  buffer = jabber_new_start (node);
+  gnet_conn_write (hook->peer, buffer->str, buffer->len);
+  g_string_free (buffer, TRUE);
+  gnet_conn_read (hook->peer);
+}
+
+void jabber_send (net_hook_t* hook, iks* node)
+{
+  GString* buffer;
+  buffer = g_string_new (iks_string (iks_stack (node), node));
+  gnet_conn_write (hook->peer, buffer->str, buffer->len);
+  g_string_free (buffer, TRUE);
+}
+
+int jabber_parser (gpointer data, int type, iks* node)
+{
+  net_hook_t* hook;
+  char* server;
+  hook = (net_hook_t*) data;
+  switch (type)
+    {
+    case IKS_NODE_START:
+      server = iks_find_attrib (node, "to");
+      if (server == NULL)
+       {
+         jabber_error (hook);
+       }
+      jabber_connect_server (hook, server, node);
+      iks_delete (node);
+      break;
+    case IKS_NODE_NORMAL:
+      jabber_send (hook, node);
+      iks_delete (node);
+      break;
+    case IKS_NODE_STOP:
+      gnet_conn_write (hook->peer, "</stream:stream>", 16);
+      break;
+    case IKS_NODE_ERROR:
+      break;
+    }
+  return IKS_OK;
 }
 
 net_hook_t* jabber_hook_new (GConn* conn)
@@ -48,7 +116,7 @@ net_hook_t* jabber_hook_new (GConn* conn)
   hook->close = jabber_close;
   hook->write = jabber_write;
   hook->read = jabber_read;
-  hook->data = NULL;
+  hook->data = iks_stream_new ("jabber:client", hook, jabber_parser);
   gnet_conn_set_callback (hook->conn, nethook_event, hook);
   return hook;
 }
index bb4bd9f..793cdc9 100644 (file)
--- a/jabber.h
+++ b/jabber.h
@@ -25,5 +25,6 @@
 
 net_hook_t* jabber_hook_new (GConn *conn);
 void jabber_hook_destroy (net_hook_t*);
+net_hook_t* jabber_server_hook_new (net_hook_t*, char*);
 
 #endif
diff --git a/jabber_server.c b/jabber_server.c
new file mode 100644 (file)
index 0000000..b4fe6c3
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+** Copyright (C) 2006 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+**  
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License as published by
+** the Free Software Foundation; either version 2 of the License, or
+** (at your option) any later version.
+**  
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+** GNU General Public License for more details.
+**  
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+**  
+*/
+
+#include <gnet.h>
+#include <glib.h>
+#include <iksemel.h>
+#include "jabber.h"
+
+static void jabber_server_connect (net_hook_t* hook)
+{
+  g_message ("Connected to jabber server.");
+}
+
+static void jabber_server_close (net_hook_t* hook)
+{
+  g_message ("Server disconnected.");
+}
+
+static void jabber_server_write (net_hook_t* hook)
+{
+  g_message ("Have written bytes to server.");
+}
+
+static void jabber_server_read (net_hook_t* hook, gchar* buffer, size_t len)
+{
+  g_message ("Received response from server.");
+  gnet_conn_write (hook->peer, buffer, len);
+}
+
+net_hook_t* jabber_server_hook_new (net_hook_t* client_hook, char* server)
+{
+  net_hook_t* hook;
+  hook = g_slice_new (net_hook_t);
+  hook->conn = gnet_conn_new (server, 5222, nethook_event, hook);
+  hook->peer = client_hook->conn;
+  hook->server = TRUE;
+  hook->connect = jabber_server_connect;
+  hook->close = jabber_server_close;
+  hook->write = jabber_server_write;
+  hook->read = jabber_server_read;
+  hook->data = NULL; /*iks_stream_new ("jabber:client", hook, jabber_server_parser);*/
+  return hook;
+}