57bd01ae96f13fa8ab92997a8f6e62237395d71a
[cascardo/rnetproxy.git] / proto_detect.c
1 /*
2 ** Copyright (C) 2006 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
3 **  
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
8 **  
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ** GNU General Public License for more details.
13 **  
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 **  
18 */
19
20 #include <gnet.h>
21 #include <glib.h>
22 #include "proto_detect.h"
23 #include "jabber.h"
24
25 static void proto_connect (net_hook_t* hook)
26 {
27 }
28
29 static void proto_close (net_hook_t* hook)
30 {
31 }
32
33 static void proto_write (net_hook_t* hook)
34 {
35 }
36
37 static void proto_read (net_hook_t* hook, gchar* buffer, size_t len)
38 {
39   net_hook_t* new_hook;
40   GString* str;
41   str = (GString*) hook->data;
42   g_string_append_len (str, buffer, len);
43   if (str->len >= 7)
44     {
45       if (!strncmp (str->str, "<stream", 7))
46         {
47           /* Connection is a Jabber client */
48           g_debug ("Connection from %s is a Jabber client.",
49                    hook->conn->hostname);
50           new_hook = jabber_hook_new (hook->conn);
51           new_hook->read (new_hook, str->str, str->len);
52           proto_detect_destroy (hook);
53         }
54       else
55         {
56           g_debug ("Unrecognized protocol from %s.",
57                    hook->conn->hostname);
58           gnet_conn_disconnect (hook->conn);
59           gnet_conn_unref (hook->conn);
60           proto_detect_destroy (hook);
61         }
62     }
63 }
64
65 net_hook_t* proto_detect_new (GConn* conn)
66 {
67   net_hook_t* hook;
68   hook = g_slice_new (net_hook_t);
69   hook->conn = conn;
70   hook->peer = NULL;
71   hook->server = FALSE;
72   hook->connect = proto_connect;
73   hook->close = proto_close;
74   hook->write = proto_write;
75   hook->read = proto_read;
76   hook->data = g_string_sized_new (128);
77   gnet_conn_set_callback (hook->conn, nethook_event, hook);
78   return hook;
79 }
80
81 void proto_detect_destroy (net_hook_t* hook)
82 {
83   if (hook->data != NULL)
84     {
85       g_string_free (hook->data, TRUE);
86     }
87   g_slice_free (net_hook_t, hook);
88 }