Destroy SSL connection properly.
[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           !strncmp (str->str, "<?xml ", 6))
47         {
48           /* Connection is a Jabber client */
49           g_debug ("Connection from %s is a Jabber client.",
50                    hook->conn->hostname);
51           new_hook = jabber_hook_new (hook->conn);
52           new_hook->read (new_hook, str->str, str->len);
53           proto_detect_destroy (hook);
54         }
55       else
56         {
57           g_debug ("Unrecognized protocol from %s.",
58                    hook->conn->hostname);
59           gnet_conn_disconnect (hook->conn);
60           gnet_conn_unref (hook->conn);
61           proto_detect_destroy (hook);
62         }
63     }
64 }
65
66 net_hook_t* proto_detect_new (GConn* conn)
67 {
68   net_hook_t* hook;
69   hook = g_slice_new (net_hook_t);
70   hook->conn = conn;
71   hook->peer = NULL;
72   hook->server = FALSE;
73   hook->connect = proto_connect;
74   hook->close = proto_close;
75   hook->write = proto_write;
76   hook->read = proto_read;
77   hook->data = g_string_sized_new (128);
78   gnet_conn_set_callback (hook->conn, nethook_event, hook);
79   return hook;
80 }
81
82 void proto_detect_destroy (net_hook_t* hook)
83 {
84   if (hook->data != NULL)
85     {
86       g_string_free (hook->data, TRUE);
87     }
88   g_slice_free (net_hook_t, hook);
89 }