2 ** Copyright (C) 2006 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
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.
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.
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.
22 #include "proto_detect.h"
25 static void proto_connect (net_hook_t* hook)
29 static void proto_close (net_hook_t* hook)
33 static void proto_write (net_hook_t* hook)
37 static void proto_read (net_hook_t* hook, gchar* buffer, size_t len)
41 str = (GString*) hook->data;
42 g_string_append_len (str, buffer, len);
45 if (!strncmp (str->str, "<stream", 7))
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);
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);
65 net_hook_t* proto_detect_new (GConn* conn)
68 hook = g_slice_new (net_hook_t);
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);
81 void proto_detect_destroy (net_hook_t* hook)
83 if (hook->data != NULL)
85 g_string_free (hook->data, TRUE);
87 g_slice_free (net_hook_t, hook);