author | hallski <hallski> |
Wed, 01 Oct 2003 13:07:01 +0000 | |
changeset 39 | b909b5af5a97 |
parent 37 | c214625be6b4 |
child 40 | a6c68984c06a |
permissions | -rw-r--r-- |
1 | 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
2 |
/* |
|
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
3 |
* Copyright (C) 2003 Imendio HB |
7
d8eae83450ac
2003-06-13 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
4
diff
changeset
|
4 |
* Copyright (C) 2003 Mikael Hallendal <micke@imendio.com> |
1 | 5 |
* Copyright (C) 2003 CodeFactory AB. |
6 |
* |
|
7 |
* This program is free software; you can redistribute it and/or |
|
8 |
* modify it under the terms of the GNU Lesser General Public License as |
|
9 |
* published by the Free Software Foundation; either version 2 of the |
|
10 |
* License, or (at your option) any later version. |
|
11 |
* |
|
12 |
* This program is distributed in the hope that it will be useful, |
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
15 |
* Lesser General Public License for more details. |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU Lesser General Public |
|
18 |
* License along with this program; if not, write to the |
|
19 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
20 |
* Boston, MA 02111-1307, USA. |
|
21 |
*/ |
|
22 |
||
23 |
#include <config.h> |
|
24 |
||
15
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
25 |
#ifdef HAVE_GNUTLS |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
26 |
#include <gnutls/gnutls.h> |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
27 |
#endif |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
28 |
|
12
fc5e71b1369c
2003-07-07 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
9
diff
changeset
|
29 |
#include <string.h> |
fc5e71b1369c
2003-07-07 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
9
diff
changeset
|
30 |
#include <unistd.h> |
fc5e71b1369c
2003-07-07 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
9
diff
changeset
|
31 |
#include <sys/types.h> |
9
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
32 |
#ifndef __WIN32__ |
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
33 |
#include <netdb.h> |
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
34 |
#include <sys/socket.h> |
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
35 |
#include <netinet/in.h> |
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
36 |
#else |
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
37 |
#include <winsock2.h> |
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
38 |
#endif |
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
39 |
|
1 | 40 |
#include "lm-debug.h" |
41 |
#include "lm-error.h" |
|
42 |
#include "lm-internals.h" |
|
43 |
#include "lm-parser.h" |
|
44 |
#include "lm-sha.h" |
|
39
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
45 |
#include "lm-queue.h" |
1 | 46 |
#include "lm-connection.h" |
47 |
||
48 |
#define IN_BUFFER_SIZE 1024 |
|
49 |
||
50 |
typedef struct { |
|
51 |
LmHandlerPriority priority; |
|
52 |
LmMessageHandler *handler; |
|
53 |
} HandlerData; |
|
54 |
||
55 |
typedef struct { |
|
56 |
GSource source; |
|
57 |
||
58 |
LmConnection *connection; |
|
59 |
} LmIncomingSource; |
|
60 |
||
61 |
struct _LmConnection { |
|
62 |
/* Parameters */ |
|
15
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
63 |
gchar *server; |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
64 |
guint port; |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
65 |
gboolean use_ssl; |
1 | 66 |
|
15
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
67 |
#ifdef HAVE_GNUTLS |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
68 |
gnutls_session gnutls_session; |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
69 |
gnutls_certificate_client_credentials gnutls_xcred; |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
70 |
#endif |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
71 |
|
1 | 72 |
gboolean is_open; |
73 |
gboolean is_authenticated; |
|
74 |
||
75 |
LmParser *parser; |
|
76 |
gchar *stream_id; |
|
77 |
||
78 |
GHashTable *id_handlers; |
|
79 |
GSList *handlers[LM_MESSAGE_TYPE_UNKNOWN]; |
|
80 |
||
81 |
/* Communication */ |
|
82 |
GIOChannel *io_channel; |
|
26
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
83 |
guint io_watch_in; |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
84 |
guint io_watch_err; |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
85 |
guint io_watch_hup; |
1 | 86 |
|
87 |
LmCallback *open_cb; |
|
88 |
LmCallback *close_cb; |
|
89 |
LmCallback *auth_cb; |
|
90 |
LmCallback *register_cb; |
|
91 |
||
92 |
LmCallback *disconnect_cb; |
|
93 |
||
39
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
94 |
LmQueue *incoming_messages; |
1 | 95 |
GSource *incoming_source; |
96 |
||
97 |
gint ref_count; |
|
98 |
}; |
|
99 |
||
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
100 |
typedef enum { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
101 |
AUTH_TYPE_PLAIN = 1, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
102 |
AUTH_TYPE_DIGEST = 2, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
103 |
AUTH_TYPE_0K = 4 |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
104 |
} AuthType; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
105 |
|
1 | 106 |
static void connection_free (LmConnection *connection); |
107 |
||
108 |
||
109 |
static void connection_handle_message (LmConnection *connection, |
|
110 |
LmMessage *message); |
|
111 |
||
112 |
static void connection_new_message_cb (LmParser *parser, |
|
113 |
LmMessage *message, |
|
114 |
LmConnection *connection); |
|
115 |
static gboolean connection_do_open (LmConnection *connection, |
|
116 |
GError **error); |
|
117 |
||
118 |
static void connection_do_close (LmConnection *connection); |
|
119 |
static gboolean connection_in_event (GIOChannel *source, |
|
120 |
GIOCondition condition, |
|
121 |
LmConnection *connection); |
|
122 |
static gboolean connection_error_event (GIOChannel *source, |
|
123 |
GIOCondition condition, |
|
124 |
LmConnection *connection); |
|
125 |
static gboolean connection_hup_event (GIOChannel *source, |
|
126 |
GIOCondition condition, |
|
127 |
LmConnection *connection); |
|
128 |
static gboolean connection_send (LmConnection *connection, |
|
129 |
const gchar *str, |
|
130 |
gint len, |
|
131 |
GError **error); |
|
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
132 |
static LmMessage * connection_create_auth_req_msg (const gchar *username); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
133 |
static LmMessage * connection_create_auth_msg (LmConnection *connection, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
134 |
const gchar *username, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
135 |
const gchar *password, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
136 |
const gchar *resource, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
137 |
gint auth_type); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
138 |
static LmHandlerResult connection_auth_req_reply (LmMessageHandler *handler, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
139 |
LmConnection *connection, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
140 |
LmMessage *m, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
141 |
gpointer user_data); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
142 |
static int connection_check_auth_type (LmMessage *auth_req_rpl); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
143 |
|
1 | 144 |
static LmHandlerResult connection_auth_reply (LmMessageHandler *handler, |
145 |
LmConnection *connection, |
|
146 |
LmMessage *m, |
|
147 |
gpointer user_data); |
|
148 |
||
149 |
static void connection_stream_received (LmConnection *connection, |
|
150 |
LmMessage *m); |
|
151 |
||
152 |
static gint connection_handler_compare_func (HandlerData *a, |
|
153 |
HandlerData *b); |
|
154 |
static gboolean connection_incoming_prepare (GSource *source, |
|
155 |
gint *timeout); |
|
156 |
static gboolean connection_incoming_check (GSource *source); |
|
157 |
static gboolean connection_incoming_dispatch (GSource *source, |
|
158 |
GSourceFunc callback, |
|
159 |
gpointer user_data); |
|
160 |
static GSource * connection_create_source (LmConnection *connection); |
|
161 |
static void connection_signal_disconnect (LmConnection *connection, |
|
162 |
LmDisconnectReason reason); |
|
163 |
||
164 |
static GSourceFuncs incoming_funcs = { |
|
165 |
connection_incoming_prepare, |
|
166 |
connection_incoming_check, |
|
167 |
connection_incoming_dispatch, |
|
168 |
NULL |
|
169 |
}; |
|
170 |
||
171 |
static void |
|
172 |
connection_free (LmConnection *connection) |
|
173 |
{ |
|
174 |
g_free (connection->server); |
|
175 |
||
176 |
if (connection->io_channel) { |
|
26
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
177 |
connection_do_close (connection); |
1 | 178 |
} |
179 |
||
180 |
g_free (connection); |
|
181 |
} |
|
182 |
||
183 |
static void |
|
184 |
connection_handle_message (LmConnection *connection, LmMessage *m) |
|
185 |
{ |
|
186 |
LmMessageHandler *handler; |
|
187 |
GSList *l; |
|
188 |
const gchar *id; |
|
189 |
LmHandlerResult result = LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
190 |
||
191 |
if (lm_message_get_type (m) == LM_MESSAGE_TYPE_STREAM) { |
|
192 |
connection_stream_received (connection, m); |
|
193 |
return; |
|
194 |
} |
|
195 |
||
196 |
id = lm_message_node_get_attribute (m->node, "id"); |
|
197 |
||
198 |
if (id) { |
|
199 |
handler = g_hash_table_lookup (connection->id_handlers, id); |
|
200 |
if (handler) { |
|
201 |
result = _lm_message_handler_handle_message (handler, |
|
202 |
connection, |
|
203 |
m); |
|
204 |
g_hash_table_remove (connection->id_handlers, id); |
|
205 |
} |
|
206 |
} |
|
207 |
||
208 |
if (result == LM_HANDLER_RESULT_REMOVE_MESSAGE) { |
|
209 |
return; |
|
210 |
} |
|
211 |
||
212 |
for (l = connection->handlers[lm_message_get_type (m)]; |
|
213 |
l && result == LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
214 |
l = l->next) { |
|
215 |
HandlerData *hd = (HandlerData *) l->data; |
|
216 |
||
217 |
result = _lm_message_handler_handle_message (hd->handler, |
|
218 |
connection, |
|
219 |
m); |
|
220 |
} |
|
221 |
||
222 |
return; |
|
223 |
} |
|
224 |
||
225 |
static void |
|
226 |
connection_new_message_cb (LmParser *parser, |
|
227 |
LmMessage *m, |
|
228 |
LmConnection *connection) |
|
229 |
{ |
|
230 |
lm_message_ref (m); |
|
231 |
||
232 |
lm_verbose ("New message with type=\"%s\" from: %s\n", |
|
233 |
_lm_message_type_to_string (lm_message_get_type (m)), |
|
234 |
lm_message_node_get_attribute (m->node, "from")); |
|
235 |
||
39
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
236 |
lm_queue_push_tail (connection->incoming_messages, m); |
1 | 237 |
} |
238 |
||
239 |
static gboolean |
|
240 |
connection_do_open (LmConnection *connection, GError **error) |
|
241 |
{ |
|
242 |
struct hostent *he; |
|
243 |
struct in_addr *haddr; |
|
244 |
struct sockaddr_in saddr; |
|
245 |
gint fd; |
|
246 |
||
247 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
248 |
||
249 |
he = gethostbyname(connection->server); |
|
250 |
if (he == NULL) { |
|
251 |
g_set_error (error, |
|
252 |
LM_ERROR, |
|
253 |
LM_ERROR_CONNECTION_OPEN, |
|
254 |
"gethostbyname() failed"); |
|
255 |
return FALSE; |
|
256 |
} |
|
257 |
||
258 |
haddr = ((struct in_addr *) (he->h_addr_list)[0]); |
|
259 |
||
15
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
260 |
#ifdef HAVE_GNUTLS |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
261 |
if (connection->use_ssl) { |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
262 |
gnutls_global_init (); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
263 |
gnutls_certificate_allocate_credentials(&connection->gnutls_xcred); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
264 |
} |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
265 |
#endif |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
266 |
|
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
267 |
fd = socket(AF_INET, SOCK_STREAM, 0); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
268 |
memset(&saddr, 0, sizeof(saddr)); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
269 |
memcpy(&saddr.sin_addr, haddr, sizeof(struct in_addr)); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
270 |
saddr.sin_family = AF_INET; |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
271 |
saddr.sin_port = htons (connection->port); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
272 |
|
1 | 273 |
if (connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) { |
15
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
274 |
g_set_error (error, |
1 | 275 |
LM_ERROR, |
276 |
LM_ERROR_CONNECTION_OPEN, |
|
277 |
"connect() failed"); |
|
278 |
close (fd); |
|
279 |
return FALSE; |
|
280 |
} |
|
15
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
281 |
|
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
282 |
#ifdef HAVE_GNUTLS |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
283 |
if (connection->use_ssl) { |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
284 |
int ret; |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
285 |
const int cert_type_priority[2] = |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
286 |
{ GNUTLS_CRT_X509, GNUTLS_CRT_OPENPGP }; |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
287 |
|
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
288 |
gnutls_init (&connection->gnutls_session, GNUTLS_CLIENT); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
289 |
gnutls_set_default_priority (connection->gnutls_session); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
290 |
gnutls_certificate_type_set_priority (connection->gnutls_session, |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
291 |
cert_type_priority); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
292 |
gnutls_credentials_set (connection->gnutls_session, |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
293 |
GNUTLS_CRD_CERTIFICATE, |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
294 |
connection->gnutls_xcred); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
295 |
|
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
296 |
gnutls_transport_set_ptr (connection->gnutls_session, |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
297 |
(gnutls_transport_ptr) fd); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
298 |
|
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
299 |
ret = gnutls_handshake (connection->gnutls_session); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
300 |
|
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
301 |
if (ret < 0) { |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
302 |
gnutls_perror (ret); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
303 |
shutdown (fd, SHUT_RDWR); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
304 |
close (fd); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
305 |
connection_do_close (connection); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
306 |
g_set_error (error, LM_ERROR, LM_ERROR_CONNECTION_OPEN, |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
307 |
"*** GNUTLS handshake failed"); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
308 |
return FALSE; |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
309 |
} |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
310 |
} |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
311 |
#endif |
1 | 312 |
|
313 |
connection->io_channel = g_io_channel_unix_new (fd); |
|
314 |
g_io_channel_set_close_on_unref (connection->io_channel, TRUE); |
|
315 |
g_io_channel_set_encoding (connection->io_channel, NULL, NULL); |
|
316 |
||
317 |
g_io_channel_set_buffered (connection->io_channel, FALSE); |
|
318 |
g_io_channel_set_flags (connection->io_channel, |
|
319 |
G_IO_FLAG_NONBLOCK, NULL); |
|
26
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
320 |
connection->io_watch_in = g_io_add_watch (connection->io_channel, |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
321 |
G_IO_IN, |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
322 |
(GIOFunc) connection_in_event, |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
323 |
connection); |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
324 |
connection->io_watch_err = g_io_add_watch (connection->io_channel, |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
325 |
G_IO_ERR, |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
326 |
(GIOFunc) connection_error_event, |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
327 |
connection); |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
328 |
connection->io_watch_hup = g_io_add_watch (connection->io_channel, |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
329 |
G_IO_HUP, |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
330 |
(GIOFunc) connection_hup_event, |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
331 |
connection); |
1 | 332 |
|
333 |
connection->is_open = TRUE; |
|
334 |
||
335 |
if (!connection_send (connection, |
|
336 |
"<?xml version='1.0' encoding='UTF-8'?>", -1, |
|
337 |
error)) { |
|
338 |
return FALSE; |
|
339 |
} |
|
340 |
||
341 |
return TRUE; |
|
342 |
} |
|
343 |
||
344 |
static void |
|
345 |
connection_do_close (LmConnection *connection) |
|
346 |
{ |
|
347 |
if (connection->io_channel) { |
|
26
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
348 |
g_source_remove (connection->io_watch_in); |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
349 |
g_source_remove (connection->io_watch_err); |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
350 |
g_source_remove (connection->io_watch_hup); |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
351 |
|
1 | 352 |
g_io_channel_unref (connection->io_channel); |
26
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
353 |
connection->io_channel = NULL; |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
354 |
} |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
355 |
|
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
356 |
if (!connection->is_open) { |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
357 |
return; |
1 | 358 |
} |
359 |
||
360 |
connection->is_open = FALSE; |
|
15
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
361 |
|
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
362 |
#ifdef HAVE_GNUTLS |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
363 |
if (connection->use_ssl) { |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
364 |
gnutls_deinit (connection->gnutls_session); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
365 |
gnutls_certificate_free_credentials (connection->gnutls_xcred); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
366 |
gnutls_global_deinit (); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
367 |
} |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
368 |
#endif |
1 | 369 |
} |
370 |
||
371 |
||
372 |
static gboolean |
|
373 |
connection_in_event (GIOChannel *source, |
|
374 |
GIOCondition condition, |
|
375 |
LmConnection *connection) |
|
376 |
{ |
|
28
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
377 |
gchar buf[IN_BUFFER_SIZE]; |
37
c214625be6b4
2003-08-24 Martyn Russell <ginxd@btopenworld.com>
hallski <hallski>
parents:
28
diff
changeset
|
378 |
gint bytes_read; |
28
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
379 |
GIOStatus status; |
1 | 380 |
|
381 |
if (!connection->io_channel) { |
|
382 |
return FALSE; |
|
383 |
} |
|
26
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
384 |
|
15
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
385 |
#ifdef HAVE_GNUTLS |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
386 |
if (connection->use_ssl) { |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
387 |
bytes_read = gnutls_record_recv (connection->gnutls_session, |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
388 |
buf,IN_BUFFER_SIZE - 1); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
389 |
if (bytes_read <= 0) { |
28
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
390 |
status = G_IO_STATUS_ERROR; |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
391 |
|
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
392 |
//connection_error_event (connection->io_channel, |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
393 |
// G_IO_HUP, |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
394 |
// connection); |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
395 |
} |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
396 |
else { |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
397 |
status = G_IO_STATUS_NORMAL; |
15
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
398 |
} |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
399 |
} else { |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
400 |
#endif |
28
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
401 |
status = g_io_channel_read_chars (connection->io_channel, |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
402 |
buf, IN_BUFFER_SIZE - 1, |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
403 |
&bytes_read, |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
404 |
NULL); |
15
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
405 |
#ifdef HAVE_GNUTLS |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
406 |
} |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
407 |
#endif |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
408 |
|
28
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
409 |
if (status != G_IO_STATUS_NORMAL) { |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
410 |
gint reason; |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
411 |
|
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
412 |
switch (status) { |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
413 |
case G_IO_STATUS_EOF: |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
414 |
reason = LM_DISCONNECT_REASON_HUP; |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
415 |
break; |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
416 |
case G_IO_STATUS_AGAIN: |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
417 |
return TRUE; |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
418 |
break; |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
419 |
case G_IO_STATUS_ERROR: |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
420 |
reason = LM_DISCONNECT_REASON_ERROR; |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
421 |
break; |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
422 |
default: |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
423 |
reason = LM_DISCONNECT_REASON_UNKNOWN; |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
424 |
} |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
425 |
|
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
426 |
connection_do_close (connection); |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
427 |
connection_signal_disconnect (connection, reason); |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
428 |
|
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
429 |
return FALSE; |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
430 |
} |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
431 |
|
1 | 432 |
buf[bytes_read] = '\0'; |
433 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, "\nRECV:\n"); |
|
434 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, |
|
435 |
"-----------------------------------\n"); |
|
28
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
436 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, "'%s'\n", buf); |
1 | 437 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, |
438 |
"-----------------------------------\n"); |
|
28
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
439 |
|
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
440 |
lm_verbose ("Read: %d chars\n", bytes_read); |
8784d9121214
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
26
diff
changeset
|
441 |
|
1 | 442 |
lm_parser_parse (connection->parser, buf); |
443 |
||
444 |
return TRUE; |
|
445 |
} |
|
446 |
||
447 |
static gboolean |
|
448 |
connection_error_event (GIOChannel *source, |
|
449 |
GIOCondition condition, |
|
450 |
LmConnection *connection) |
|
451 |
{ |
|
26
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
452 |
if (!connection->io_channel) { |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
453 |
return FALSE; |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
454 |
} |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
455 |
|
1 | 456 |
lm_verbose ("Error event: %d\n", condition); |
457 |
||
458 |
connection_do_close (connection); |
|
459 |
connection_signal_disconnect (connection, LM_DISCONNECT_REASON_ERROR); |
|
460 |
||
461 |
return TRUE; |
|
462 |
} |
|
463 |
||
464 |
static gboolean |
|
465 |
connection_hup_event (GIOChannel *source, |
|
466 |
GIOCondition condition, |
|
467 |
LmConnection *connection) |
|
468 |
{ |
|
26
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
469 |
if (!connection->io_channel) { |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
470 |
return FALSE; |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
471 |
} |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
472 |
|
1 | 473 |
lm_verbose ("HUP event\n"); |
474 |
||
475 |
connection_do_close (connection); |
|
476 |
connection_signal_disconnect (connection, LM_DISCONNECT_REASON_HUP); |
|
477 |
||
478 |
return TRUE; |
|
479 |
} |
|
480 |
||
481 |
static gboolean |
|
482 |
connection_send (LmConnection *connection, |
|
483 |
const gchar *str, |
|
484 |
gint len, |
|
485 |
GError **error) |
|
486 |
{ |
|
487 |
gsize bytes_written; |
|
488 |
||
489 |
if (!lm_connection_is_open (connection)) { |
|
490 |
g_set_error (error, |
|
491 |
LM_ERROR, |
|
492 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
493 |
"Connection is not open, call lm_connection_open() first"); |
|
494 |
return FALSE; |
|
495 |
} |
|
496 |
||
497 |
if (len == -1) { |
|
498 |
len = strlen (str); |
|
499 |
} |
|
500 |
||
501 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, "\nSEND:\n"); |
|
502 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, |
|
503 |
"-----------------------------------\n"); |
|
504 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, "%s\n", str); |
|
505 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, |
|
506 |
"-----------------------------------\n"); |
|
507 |
||
15
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
508 |
#ifdef HAVE_GNUTLS |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
509 |
if (connection->use_ssl) { |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
510 |
while ((bytes_written = gnutls_record_send (connection->gnutls_session, str, len)) < 0) |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
511 |
if (bytes_written != GNUTLS_E_INTERRUPTED && |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
512 |
bytes_written != GNUTLS_E_AGAIN) |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
513 |
{ |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
514 |
connection_error_event (connection->io_channel, G_IO_HUP, |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
515 |
connection); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
516 |
} |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
517 |
|
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
518 |
} else { |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
519 |
#endif |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
520 |
g_io_channel_write_chars (connection->io_channel, str, len, |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
521 |
&bytes_written, NULL); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
522 |
#ifdef HAVE_GNUTLS |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
523 |
} |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
524 |
#endif |
1 | 525 |
|
526 |
return TRUE; |
|
527 |
} |
|
528 |
||
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
529 |
typedef struct { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
530 |
gchar *username; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
531 |
gchar *password; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
532 |
gchar *resource; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
533 |
} AuthReqData; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
534 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
535 |
static void |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
536 |
auth_req_data_free (AuthReqData *data) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
537 |
g_free (data->username); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
538 |
g_free (data->password); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
539 |
g_free (data->resource); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
540 |
g_free (data); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
541 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
542 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
543 |
static LmMessage * |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
544 |
connection_create_auth_req_msg (const gchar *username) |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
545 |
{ |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
546 |
LmMessage *m; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
547 |
LmMessageNode *q_node; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
548 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
549 |
m = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
550 |
LM_MESSAGE_SUB_TYPE_GET); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
551 |
q_node = lm_message_node_add_child (m->node, "query", NULL); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
552 |
lm_message_node_set_attributes (q_node, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
553 |
"xmlns", "jabber:iq:auth", |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
554 |
NULL); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
555 |
lm_message_node_add_child (q_node, "username", username); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
556 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
557 |
return m; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
558 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
559 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
560 |
static LmMessage * |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
561 |
connection_create_auth_msg (LmConnection *connection, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
562 |
const gchar *username, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
563 |
const gchar *password, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
564 |
const gchar *resource, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
565 |
gint auth_type) |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
566 |
{ |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
567 |
LmMessage *auth_msg; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
568 |
LmMessageNode *q_node; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
569 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
570 |
auth_msg = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
571 |
LM_MESSAGE_SUB_TYPE_SET); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
572 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
573 |
q_node = lm_message_node_add_child (auth_msg->node, "query", NULL); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
574 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
575 |
lm_message_node_set_attributes (q_node, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
576 |
"xmlns", "jabber:iq:auth", |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
577 |
NULL); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
578 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
579 |
lm_message_node_add_child (q_node, "username", username); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
580 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
581 |
if (auth_type & AUTH_TYPE_0K) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
582 |
lm_verbose ("Using 0k auth (not implemented yet)\n"); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
583 |
/* TODO: Should probably use this? */ |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
584 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
585 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
586 |
if (auth_type & AUTH_TYPE_DIGEST) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
587 |
gchar *str; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
588 |
const gchar *digest; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
589 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
590 |
lm_verbose ("Using digest\n"); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
591 |
str = g_strconcat (connection->stream_id, password, NULL); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
592 |
digest = lm_sha_hash (str); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
593 |
g_free (str); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
594 |
lm_message_node_add_child (q_node, "digest", digest); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
595 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
596 |
else if (auth_type & AUTH_TYPE_PLAIN) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
597 |
lm_verbose ("Using plaintext auth\n"); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
598 |
lm_message_node_add_child (q_node, "password", password); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
599 |
} else { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
600 |
/* TODO: Report error somehow */ |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
601 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
602 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
603 |
lm_message_node_add_child (q_node, "resource", resource); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
604 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
605 |
return auth_msg; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
606 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
607 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
608 |
static LmHandlerResult |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
609 |
connection_auth_req_reply (LmMessageHandler *handler, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
610 |
LmConnection *connection, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
611 |
LmMessage *m, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
612 |
gpointer user_data) |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
613 |
{ |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
614 |
int auth_type; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
615 |
LmMessage *auth_msg; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
616 |
LmMessageHandler *auth_handler; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
617 |
AuthReqData *data = (AuthReqData *) user_data; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
618 |
gboolean result; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
619 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
620 |
auth_type = connection_check_auth_type (m); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
621 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
622 |
auth_msg = connection_create_auth_msg (connection, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
623 |
data->username, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
624 |
data->password, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
625 |
data->resource, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
626 |
auth_type); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
627 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
628 |
auth_handler = lm_message_handler_new (connection_auth_reply, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
629 |
NULL, NULL); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
630 |
result = lm_connection_send_with_reply (connection, auth_msg, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
631 |
auth_handler, NULL); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
632 |
lm_message_handler_unref (auth_handler); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
633 |
lm_message_unref (auth_msg); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
634 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
635 |
return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
636 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
637 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
638 |
static int |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
639 |
connection_check_auth_type (LmMessage *auth_req_rpl) |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
640 |
{ |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
641 |
LmMessageNode *q_node; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
642 |
gint ret_val = 0; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
643 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
644 |
q_node = lm_message_node_get_child (auth_req_rpl->node, "query"); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
645 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
646 |
if (!q_node) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
647 |
return AUTH_TYPE_PLAIN; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
648 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
649 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
650 |
if (lm_message_node_get_child (q_node, "password")) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
651 |
ret_val |= AUTH_TYPE_PLAIN; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
652 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
653 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
654 |
if (lm_message_node_get_child (q_node, "digest")) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
655 |
ret_val |= AUTH_TYPE_DIGEST; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
656 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
657 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
658 |
if (lm_message_node_get_child (q_node, "sequence") && |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
659 |
lm_message_node_get_child (q_node, "token")) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
660 |
ret_val |= AUTH_TYPE_0K; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
661 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
662 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
663 |
return ret_val; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
664 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
665 |
|
1 | 666 |
static LmHandlerResult |
667 |
connection_auth_reply (LmMessageHandler *handler, |
|
668 |
LmConnection *connection, |
|
669 |
LmMessage *m, |
|
670 |
gpointer user_data) |
|
671 |
{ |
|
672 |
const gchar *type; |
|
673 |
gboolean result = TRUE; |
|
674 |
||
675 |
g_return_val_if_fail (connection != NULL, |
|
676 |
LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS); |
|
677 |
||
678 |
||
679 |
type = lm_message_node_get_attribute (m->node, "type"); |
|
680 |
if (strcmp (type, "result") == 0) { |
|
681 |
result = TRUE; |
|
682 |
} |
|
683 |
else if (strcmp (type, "error") == 0) { |
|
684 |
result = FALSE; |
|
685 |
} |
|
686 |
||
687 |
lm_verbose ("AUTH reply: %d\n", result); |
|
688 |
||
689 |
if (connection->auth_cb && connection->auth_cb->func) { |
|
690 |
LmCallback *cb = connection->auth_cb; |
|
691 |
||
692 |
(* ((LmResultFunction) cb->func)) (connection, |
|
693 |
result, cb->user_data); |
|
694 |
} |
|
695 |
||
696 |
_lm_utils_free_callback (connection->auth_cb); |
|
697 |
connection->auth_cb = NULL; |
|
698 |
||
699 |
return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
|
700 |
} |
|
701 |
||
702 |
||
703 |
static void |
|
704 |
connection_stream_received (LmConnection *connection, LmMessage *m) |
|
705 |
{ |
|
706 |
gboolean result; |
|
707 |
||
708 |
g_return_if_fail (connection != NULL); |
|
709 |
g_return_if_fail (m != NULL); |
|
710 |
||
711 |
connection->stream_id = g_strdup (lm_message_node_get_attribute (m->node, |
|
712 |
"id"));; |
|
713 |
||
714 |
lm_verbose ("Stream received: %s\n", connection->stream_id); |
|
715 |
||
716 |
/* Check to see if the stream is correctly set up */ |
|
717 |
result = TRUE; |
|
718 |
||
719 |
if (connection->open_cb && connection->open_cb->func) { |
|
720 |
LmCallback *cb = connection->open_cb; |
|
721 |
||
722 |
(* ((LmResultFunction) cb->func)) (connection, result, |
|
723 |
cb->user_data); |
|
724 |
} |
|
725 |
||
726 |
_lm_utils_free_callback (connection->open_cb); |
|
727 |
connection->open_cb = NULL; |
|
728 |
} |
|
729 |
||
730 |
static gint |
|
731 |
connection_handler_compare_func (HandlerData *a, HandlerData *b) |
|
732 |
{ |
|
733 |
return b->priority - a->priority; |
|
734 |
} |
|
735 |
||
736 |
static gboolean |
|
737 |
connection_incoming_prepare (GSource *source, gint *timeout) |
|
738 |
{ |
|
739 |
LmConnection *connection; |
|
740 |
||
741 |
connection = ((LmIncomingSource *)source)->connection; |
|
742 |
||
39
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
743 |
return !lm_queue_is_empty (connection->incoming_messages); |
1 | 744 |
} |
745 |
||
746 |
static gboolean |
|
747 |
connection_incoming_check (GSource *source) |
|
748 |
{ |
|
749 |
return FALSE; |
|
750 |
} |
|
751 |
||
752 |
static gboolean |
|
753 |
connection_incoming_dispatch (GSource *source, |
|
754 |
GSourceFunc callback, |
|
755 |
gpointer user_data) |
|
756 |
{ |
|
757 |
LmConnection *connection; |
|
758 |
LmMessage *m; |
|
759 |
||
760 |
connection = ((LmIncomingSource *) source)->connection; |
|
761 |
||
39
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
762 |
m = (LmMessage *) lm_queue_pop_head (connection->incoming_messages); |
1 | 763 |
|
764 |
if (m) { |
|
765 |
connection_handle_message (connection, m); |
|
766 |
lm_message_unref (m); |
|
767 |
} |
|
768 |
||
769 |
return TRUE; |
|
770 |
} |
|
771 |
||
772 |
static GSource * |
|
773 |
connection_create_source (LmConnection *connection) |
|
774 |
{ |
|
775 |
GSource *source; |
|
776 |
||
777 |
source = g_source_new (&incoming_funcs, sizeof (LmIncomingSource)); |
|
778 |
((LmIncomingSource *) source)->connection = connection; |
|
779 |
||
780 |
return source; |
|
781 |
} |
|
782 |
||
783 |
static void |
|
784 |
connection_signal_disconnect (LmConnection *connection, |
|
785 |
LmDisconnectReason reason) |
|
786 |
{ |
|
787 |
if (connection->disconnect_cb && connection->disconnect_cb->func) { |
|
788 |
LmCallback *cb = connection->disconnect_cb; |
|
789 |
||
790 |
(* ((LmDisconnectFunction) cb->func)) (connection, |
|
791 |
reason, |
|
792 |
cb->user_data); |
|
793 |
} |
|
794 |
} |
|
795 |
||
796 |
/** |
|
797 |
* lm_connection_new: |
|
798 |
* @server: The hostname to the server for the connection. |
|
799 |
* |
|
800 |
* Creates a new closed connection. To open the connection call |
|
801 |
* lm_connection_open(). @server can be #NULL but must be set before calling lm_connection_open(). |
|
802 |
* |
|
803 |
* Return value: A newly created LmConnection, should be unreffed with lm_connection_unref(). |
|
804 |
**/ |
|
805 |
LmConnection * |
|
806 |
lm_connection_new (const gchar *server) |
|
807 |
{ |
|
808 |
LmConnection *connection; |
|
809 |
gint i; |
|
810 |
||
811 |
lm_debug_init (); |
|
812 |
||
813 |
connection = g_new0 (LmConnection, 1); |
|
814 |
||
815 |
if (server) { |
|
816 |
connection->server = g_strdup (server); |
|
817 |
} else { |
|
818 |
connection->server = NULL; |
|
819 |
} |
|
820 |
||
821 |
connection->port = LM_CONNECTION_DEFAULT_PORT; |
|
822 |
connection->use_ssl = FALSE; |
|
823 |
connection->disconnect_cb = NULL; |
|
39
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
824 |
connection->incoming_messages = lm_queue_new (); |
1 | 825 |
connection->incoming_source = connection_create_source (connection); |
826 |
||
827 |
connection->id_handlers = g_hash_table_new_full (g_str_hash, |
|
828 |
g_str_equal, |
|
829 |
g_free, |
|
830 |
(GDestroyNotify) lm_message_handler_unref); |
|
831 |
connection->ref_count = 1; |
|
832 |
g_source_attach (connection->incoming_source, NULL); |
|
833 |
||
834 |
for (i = 0; i < LM_MESSAGE_TYPE_UNKNOWN; ++i) { |
|
835 |
connection->handlers[i] = NULL; |
|
836 |
} |
|
837 |
||
838 |
connection->parser = lm_parser_new |
|
839 |
((LmParserMessageFunction) connection_new_message_cb, |
|
840 |
connection, NULL); |
|
841 |
||
842 |
return connection; |
|
843 |
} |
|
844 |
||
845 |
/** |
|
846 |
* lm_connection_open: |
|
847 |
* @connection: #LmConnection to open |
|
848 |
* @function: Callback function that will be called when the connection is open. |
|
849 |
* @user_data: User data that will be passed to @function. |
|
850 |
* @notify: Function for freeing that user_data, can be NULL. |
|
851 |
* @error: location to store error, or %NULL |
|
852 |
* |
|
853 |
* An async call to open @connection. When the connection is open @function will be called. |
|
854 |
* |
|
855 |
* Return value: #TRUE if everything went fine, otherwise #FALSE. |
|
856 |
**/ |
|
857 |
gboolean |
|
858 |
lm_connection_open (LmConnection *connection, |
|
859 |
LmResultFunction function, |
|
860 |
gpointer user_data, |
|
861 |
GDestroyNotify notify, |
|
862 |
GError **error) |
|
863 |
{ |
|
864 |
LmMessage *m; |
|
865 |
gboolean result; |
|
866 |
||
867 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
868 |
||
869 |
if (lm_connection_is_open (connection)) { |
|
870 |
g_set_error (error, |
|
871 |
LM_ERROR, |
|
872 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
873 |
"Connection is already open, call lm_connection_close() first"); |
|
874 |
return FALSE; |
|
875 |
} |
|
876 |
||
877 |
if (!connection->server) { |
|
878 |
g_set_error (error, |
|
879 |
LM_ERROR, |
|
880 |
LM_ERROR_CONNECTION_OPEN, |
|
881 |
"You need to set the server hostname in the call to lm_connection_new()"); |
|
882 |
return FALSE; |
|
883 |
} |
|
884 |
||
885 |
connection->open_cb = _lm_utils_new_callback (function, user_data, notify); |
|
886 |
||
887 |
lm_verbose ("Connecting to: %s:%d\n", |
|
888 |
connection->server, connection->port); |
|
889 |
||
890 |
if (!connection_do_open (connection, error)) { |
|
891 |
return FALSE; |
|
892 |
} |
|
893 |
||
894 |
m = lm_message_new (connection->server, LM_MESSAGE_TYPE_STREAM); |
|
895 |
lm_message_node_set_attributes (m->node, |
|
896 |
"xmlns:stream", "http://etherx.jabber.org/streams", |
|
897 |
"xmlns", "jabber:client", |
|
898 |
NULL); |
|
899 |
||
900 |
lm_verbose ("Opening stream..."); |
|
901 |
||
902 |
result = lm_connection_send (connection, m, error); |
|
903 |
lm_message_unref (m); |
|
904 |
||
905 |
return result; |
|
906 |
} |
|
907 |
||
908 |
/** |
|
909 |
* lm_connection_open_and_block: |
|
910 |
* @connection: an #LmConnection |
|
911 |
* @error: location to store error, or %NULL |
|
912 |
* |
|
913 |
* Opens @connection and waits until the stream is setup. |
|
914 |
* |
|
915 |
* Return value: #TRUE if no errors where encountered during opening and stream setup successfully, #FALSE otherwise. |
|
916 |
**/ |
|
917 |
gboolean |
|
918 |
lm_connection_open_and_block (LmConnection *connection, GError **error) |
|
919 |
{ |
|
920 |
LmMessage *m; |
|
921 |
gboolean result; |
|
922 |
gboolean finished = FALSE; |
|
923 |
gboolean ret_val = FALSE; |
|
924 |
||
925 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
926 |
||
927 |
if (lm_connection_is_open (connection)) { |
|
928 |
g_set_error (error, |
|
929 |
LM_ERROR, |
|
930 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
931 |
"Connection is already open, call lm_connection_close() first"); |
|
932 |
return FALSE; |
|
933 |
} |
|
934 |
if (!connection->server) { |
|
935 |
g_set_error (error, |
|
936 |
LM_ERROR, |
|
937 |
LM_ERROR_CONNECTION_OPEN, |
|
938 |
"You need to set the server hostname in the call to lm_connection_new()"); |
|
939 |
return FALSE; |
|
940 |
} |
|
941 |
||
942 |
lm_verbose ("(Block)Connecting to: %s:%d\n", |
|
943 |
connection->server, connection->port); |
|
944 |
||
945 |
if (!connection_do_open (connection, error)) { |
|
946 |
return FALSE; |
|
947 |
} |
|
948 |
||
949 |
m = lm_message_new (connection->server, LM_MESSAGE_TYPE_STREAM); |
|
950 |
lm_message_node_set_attributes (m->node, |
|
951 |
"xmlns:stream", "http://etherx.jabber.org/streams", |
|
952 |
"xmlns", "jabber:client", |
|
953 |
NULL); |
|
954 |
||
955 |
lm_verbose ("Sending stream: \n%s\n", |
|
956 |
lm_message_node_to_string (m->node)); |
|
957 |
||
958 |
result = lm_connection_send (connection, m, error); |
|
959 |
lm_message_unref (m); |
|
960 |
||
961 |
g_source_remove (g_source_get_id (connection->incoming_source)); |
|
962 |
g_source_unref (connection->incoming_source); |
|
963 |
||
964 |
while (!finished) { |
|
39
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
965 |
gint n; |
1 | 966 |
|
967 |
g_main_context_iteration (NULL, TRUE); |
|
968 |
||
39
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
969 |
if (lm_queue_is_empty (connection->incoming_messages)) { |
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
970 |
continue; |
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
971 |
} |
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
972 |
|
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
973 |
for (n = 0; n < connection->incoming_messages->length; n++) { |
1 | 974 |
LmMessage *m; |
39
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
975 |
|
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
976 |
m = lm_queue_peek_nth (connection->incoming_messages, n); |
1 | 977 |
if (lm_message_get_type (m) == LM_MESSAGE_TYPE_STREAM) { |
978 |
connection->stream_id = |
|
979 |
g_strdup (lm_message_node_get_attribute (m->node, "id")); |
|
980 |
ret_val = TRUE; |
|
981 |
finished = TRUE; |
|
39
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
982 |
lm_queue_remove_nth (connection->incoming_messages, n); |
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
983 |
break; |
1 | 984 |
} |
985 |
} |
|
986 |
} |
|
987 |
||
988 |
connection->incoming_source = connection_create_source (connection); |
|
989 |
g_source_attach (connection->incoming_source, NULL); |
|
990 |
||
991 |
return ret_val; |
|
992 |
} |
|
993 |
||
994 |
/** |
|
995 |
* lm_connection_close: |
|
996 |
* @connection: #LmConnection to close |
|
997 |
* @error: location to store error, or %NULL |
|
998 |
* |
|
999 |
* A synchronos call to close the connection. When returning the connection is considered to be closed and can be opened again with lm_connection_open(). |
|
1000 |
* |
|
1001 |
* Return value: Returns #TRUE if no errors where detected, otherwise #FALSE. |
|
1002 |
**/ |
|
1003 |
gboolean |
|
1004 |
lm_connection_close (LmConnection *connection, |
|
1005 |
GError **error) |
|
1006 |
{ |
|
1007 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
1008 |
||
1009 |
if (!lm_connection_is_open (connection)) { |
|
1010 |
g_set_error (error, |
|
1011 |
LM_ERROR, |
|
1012 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
1013 |
"Connection is not open, call lm_connection_open() first"); |
|
1014 |
return FALSE; |
|
1015 |
} |
|
1016 |
||
1017 |
lm_verbose ("Disconnecting from: %s:%d\n", |
|
1018 |
connection->server, |
|
1019 |
connection->port); |
|
1020 |
||
1021 |
if (!connection_send (connection, "</stream:stream>", -1, error)) { |
|
1022 |
return FALSE; |
|
1023 |
} |
|
1024 |
||
1025 |
g_io_channel_flush (connection->io_channel, NULL); |
|
1026 |
||
1027 |
connection_do_close (connection); |
|
1028 |
connection_signal_disconnect (connection, LM_DISCONNECT_REASON_OK); |
|
1029 |
||
1030 |
return TRUE; |
|
1031 |
} |
|
1032 |
||
1033 |
/** |
|
1034 |
* lm_connection_authenticate: |
|
1035 |
* @connection: #LmConnection to authenticate. |
|
1036 |
* @username: Username used to authenticate. |
|
1037 |
* @password: Password corresponding to @username. |
|
1038 |
* @resource: Resource used for this connection. |
|
1039 |
* @function: Callback called when authentication is finished. |
|
1040 |
* @user_data: Userdata passed to @function when called. |
|
1041 |
* @notify: Destroy function to free the memory used by @user_data, can be NULL. |
|
1042 |
* @error: location to store error, or %NULL |
|
1043 |
* |
|
1044 |
* Tries to authenticate a user against the server. The #LmResult in the result callback will tell if it succeeded or not. |
|
1045 |
* |
|
1046 |
* Return value: #TRUE if no errors where detected while sending the authentication message, #FALSE otherwise. |
|
1047 |
**/ |
|
1048 |
gboolean |
|
1049 |
lm_connection_authenticate (LmConnection *connection, |
|
1050 |
const gchar *username, |
|
1051 |
const gchar *password, |
|
1052 |
const gchar *resource, |
|
1053 |
LmResultFunction function, |
|
1054 |
gpointer user_data, |
|
1055 |
GDestroyNotify notify, |
|
1056 |
GError **error) |
|
1057 |
{ |
|
1058 |
LmMessage *m; |
|
1059 |
LmMessageHandler *handler; |
|
1060 |
gboolean result; |
|
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1061 |
AuthReqData *data; |
1 | 1062 |
|
1063 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
1064 |
g_return_val_if_fail (username != NULL, FALSE); |
|
1065 |
g_return_val_if_fail (password != NULL, FALSE); |
|
1066 |
g_return_val_if_fail (resource != NULL, FALSE); |
|
1067 |
||
1068 |
if (!lm_connection_is_open (connection)) { |
|
1069 |
g_set_error (error, |
|
1070 |
LM_ERROR, |
|
1071 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
1072 |
"Connection is not open, call lm_connection_open() first"); |
|
1073 |
return FALSE; |
|
1074 |
} |
|
1075 |
||
1076 |
connection->auth_cb = _lm_utils_new_callback (function, |
|
1077 |
user_data, |
|
1078 |
notify); |
|
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1079 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1080 |
m = connection_create_auth_req_msg (username); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1081 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1082 |
data = g_new0 (AuthReqData, 1); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1083 |
data->username = g_strdup (username); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1084 |
data->password = g_strdup (password); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1085 |
data->resource = g_strdup (resource); |
1 | 1086 |
|
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1087 |
handler = lm_message_handler_new (connection_auth_req_reply, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1088 |
data, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1089 |
(GDestroyNotify) auth_req_data_free); |
1 | 1090 |
result = lm_connection_send_with_reply (connection, m, handler, error); |
1091 |
||
1092 |
lm_message_handler_unref (handler); |
|
1093 |
lm_message_unref (m); |
|
1094 |
||
1095 |
return result; |
|
1096 |
} |
|
1097 |
||
1098 |
/** |
|
1099 |
* lm_connection_authenticate_and_block: |
|
1100 |
* @connection: an #LmConnection |
|
1101 |
* @username: Username used to authenticate. |
|
1102 |
* @password: Password corresponding to @username. |
|
1103 |
* @resource: Resource used for this connection. |
|
1104 |
* @error: location to store error, or %NULL |
|
1105 |
* |
|
1106 |
* Tries to authenticate a user against the server. This function blocks until a reply to the authentication attempt is returned and returns whether it was successful or not. |
|
1107 |
* |
|
1108 |
* Return value: #TRUE if no errors where detected and authentication was successful. #FALSE otherwise. |
|
1109 |
**/ |
|
1110 |
gboolean |
|
1111 |
lm_connection_authenticate_and_block (LmConnection *connection, |
|
1112 |
const gchar *username, |
|
1113 |
const gchar *password, |
|
1114 |
const gchar *resource, |
|
1115 |
GError **error) |
|
1116 |
{ |
|
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1117 |
LmMessage *m; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1118 |
LmMessage *result; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1119 |
LmMessageSubType type; |
1 | 1120 |
|
1121 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
1122 |
g_return_val_if_fail (username != NULL, FALSE); |
|
1123 |
g_return_val_if_fail (password != NULL, FALSE); |
|
1124 |
g_return_val_if_fail (resource != NULL, FALSE); |
|
1125 |
||
1126 |
if (!lm_connection_is_open (connection)) { |
|
1127 |
g_set_error (error, |
|
1128 |
LM_ERROR, |
|
1129 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
1130 |
"Connection is not open, call lm_connection_open() first"); |
|
1131 |
return FALSE; |
|
1132 |
} |
|
1133 |
||
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1134 |
m = connection_create_auth_req_msg (username); |
1 | 1135 |
result = lm_connection_send_with_reply_and_block (connection, m, error); |
1136 |
lm_message_unref (m); |
|
1137 |
||
1138 |
if (!result) { |
|
1139 |
return FALSE; |
|
1140 |
} |
|
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1141 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1142 |
m = connection_create_auth_msg (connection, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1143 |
username, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1144 |
password, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1145 |
resource, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1146 |
connection_check_auth_type (result)); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1147 |
lm_message_unref (result); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1148 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1149 |
result = lm_connection_send_with_reply_and_block (connection, m, error); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1150 |
if (!result) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1151 |
return FALSE; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1152 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1153 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1154 |
type = lm_message_get_sub_type (result); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1155 |
lm_message_unref (result); |
1 | 1156 |
|
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1157 |
switch (type) { |
1 | 1158 |
case LM_MESSAGE_SUB_TYPE_RESULT: |
1159 |
return TRUE; |
|
1160 |
break; |
|
1161 |
case LM_MESSAGE_SUB_TYPE_ERROR: |
|
1162 |
return FALSE; |
|
1163 |
break; |
|
1164 |
default: |
|
1165 |
g_assert_not_reached (); |
|
1166 |
break; |
|
1167 |
} |
|
1168 |
||
1169 |
return FALSE; |
|
1170 |
} |
|
1171 |
||
1172 |
/** |
|
1173 |
* lm_connection_is_open: |
|
1174 |
* @connection: #LmConnection to check if it is open. |
|
1175 |
* |
|
1176 |
* Check if the @connection is currently open. |
|
1177 |
* |
|
1178 |
* Return value: #TRUE if connection is open and #FALSE if it is closed. |
|
1179 |
**/ |
|
1180 |
gboolean |
|
1181 |
lm_connection_is_open (LmConnection *connection) |
|
1182 |
{ |
|
1183 |
return connection->is_open; |
|
1184 |
} |
|
1185 |
||
1186 |
/** |
|
1187 |
* lm_connection_is_authenticated: |
|
1188 |
* @connection: #LmConnection to check if it is authenticated |
|
1189 |
* |
|
1190 |
* Check if @connection is authenticated. |
|
1191 |
* |
|
1192 |
* Return value: #TRUE if connection is authenticated, #FALSE otherwise. |
|
1193 |
**/ |
|
1194 |
gboolean |
|
1195 |
lm_connection_is_authenticated (LmConnection *connection) |
|
1196 |
{ |
|
1197 |
return connection->is_authenticated; |
|
1198 |
} |
|
1199 |
||
1200 |
/** |
|
1201 |
* lm_connection_get_server: |
|
1202 |
* @connection: an #LmConnection |
|
1203 |
* |
|
1204 |
* Fetches the server address that @connection is using. |
|
1205 |
* |
|
1206 |
* Return value: the server address |
|
1207 |
**/ |
|
1208 |
const gchar * |
|
1209 |
lm_connection_get_server (LmConnection *connection) |
|
1210 |
{ |
|
1211 |
return connection->server; |
|
1212 |
} |
|
1213 |
||
1214 |
/** |
|
1215 |
* lm_connection_set_server: |
|
1216 |
* @connection: an #LmConnection |
|
1217 |
* @server: Address of the server |
|
1218 |
* |
|
1219 |
* Sets the server address to @connection. Notice that @connection can't be open while doing this. |
|
1220 |
**/ |
|
1221 |
void |
|
1222 |
lm_connection_set_server (LmConnection *connection, const gchar *server) |
|
1223 |
{ |
|
1224 |
if (lm_connection_is_open (connection)) { |
|
1225 |
g_warning ("Can't change server address while connected"); |
|
1226 |
return; |
|
1227 |
} |
|
1228 |
||
1229 |
if (connection->server) { |
|
1230 |
g_free (connection->server); |
|
1231 |
} |
|
1232 |
||
1233 |
connection->server = g_strdup (server); |
|
1234 |
} |
|
1235 |
||
1236 |
/** |
|
1237 |
* lm_connection_get_port: |
|
1238 |
* @connection: an #LmConnection |
|
1239 |
* |
|
1240 |
* Fetches the port tnat @connection is using. |
|
1241 |
* |
|
1242 |
* Return value: |
|
1243 |
**/ |
|
1244 |
guint |
|
1245 |
lm_connection_get_port (LmConnection *connection) |
|
1246 |
{ |
|
1247 |
return connection->port; |
|
1248 |
} |
|
1249 |
||
1250 |
/** |
|
1251 |
* lm_connection_set_port: |
|
1252 |
* @connection: an #LmConnection |
|
1253 |
* @port: server port |
|
1254 |
* |
|
1255 |
* Sets the server port of that @connection will be using. |
|
1256 |
**/ |
|
1257 |
void |
|
1258 |
lm_connection_set_port (LmConnection *connection, guint port) |
|
1259 |
{ |
|
1260 |
if (lm_connection_is_open (connection)) { |
|
1261 |
g_warning ("Can't change server port while connected"); |
|
1262 |
return; |
|
1263 |
} |
|
1264 |
||
1265 |
connection->port = port; |
|
1266 |
} |
|
1267 |
||
1268 |
/** |
|
15
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1269 |
* lm_connection_supports_ssl: |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1270 |
* |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1271 |
* Checks whether Loudmouth supports SSL or not |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1272 |
* |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1273 |
* Return value: #TRUE if this installation of Loudmouth supports SSL, otherwise returnes #FALSE. |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1274 |
**/ |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1275 |
gboolean |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1276 |
lm_connection_supports_ssl (void) |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1277 |
{ |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1278 |
#ifdef HAVE_GNUTLS |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1279 |
return TRUE; |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1280 |
#else |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1281 |
return FALSE; |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1282 |
#endif |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1283 |
} |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1284 |
|
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1285 |
/** |
1 | 1286 |
* lm_connection_get_use_ssl: |
1287 |
* @connection: an #LmConnection |
|
1288 |
* |
|
1289 |
* Fetches if @connection is using SSL or not |
|
1290 |
* |
|
1291 |
* Return value: #TRUE if @connection is using SSL, #FALSE otherwise. |
|
1292 |
**/ |
|
1293 |
gboolean |
|
1294 |
lm_connection_get_use_ssl (LmConnection *connection) |
|
1295 |
{ |
|
1296 |
return connection->use_ssl; |
|
1297 |
} |
|
1298 |
||
1299 |
/** |
|
1300 |
* lm_connection_set_use_ssl: |
|
1301 |
* @connection: an #LmConnection |
|
1302 |
* @use_ssl: whether to use SSL or not. |
|
1303 |
* |
|
1304 |
* Sets whether @connection should use SSL for encryping traffic to/from the server. |
|
1305 |
**/ |
|
1306 |
void |
|
1307 |
lm_connection_set_use_ssl (LmConnection *connection, gboolean use_ssl) |
|
1308 |
{ |
|
1309 |
if (lm_connection_is_open (connection)) { |
|
1310 |
g_warning ("use_ssl can't be changed while connected"); |
|
1311 |
return; |
|
1312 |
} |
|
1313 |
||
1314 |
connection->use_ssl = use_ssl; |
|
1315 |
} |
|
1316 |
||
1317 |
/** |
|
1318 |
* lm_connection_send: |
|
1319 |
* @connection: #LmConnection to send connection over. |
|
1320 |
* @message: #LmMessage to send. |
|
1321 |
* @error: location to store error, or %NULL |
|
1322 |
* |
|
1323 |
* Asynchronous call to send a message. |
|
1324 |
* |
|
1325 |
* Return value: Returns #TRUE if no errors where detected while sending, #FALSE otherwise. |
|
1326 |
**/ |
|
1327 |
gboolean |
|
1328 |
lm_connection_send (LmConnection *connection, |
|
1329 |
LmMessage *message, |
|
1330 |
GError **error) |
|
1331 |
{ |
|
1332 |
gchar *xml_str; |
|
1333 |
gchar *ch; |
|
1334 |
gboolean result; |
|
1335 |
||
1336 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
1337 |
g_return_val_if_fail (message != NULL, FALSE); |
|
1338 |
||
1339 |
xml_str = lm_message_node_to_string (message->node); |
|
1340 |
if ((ch = strstr (xml_str, "</stream:stream>"))) { |
|
1341 |
*ch = '\0'; |
|
1342 |
} |
|
1343 |
||
1344 |
result = connection_send (connection, xml_str, -1, error); |
|
1345 |
g_free (xml_str); |
|
1346 |
||
1347 |
return result; |
|
1348 |
} |
|
1349 |
||
1350 |
/** |
|
1351 |
* lm_connection_send_with_reply: |
|
1352 |
* @connection: #LmConnection used to send message. |
|
1353 |
* @message: #LmMessage to send. |
|
1354 |
* @handler: #LmMessageHandler that will be used when a reply to @message arrives |
|
1355 |
* @error: location to store error, or %NULL |
|
1356 |
* |
|
1357 |
* Send a #LmMessage which will result in a reply. |
|
1358 |
* |
|
1359 |
* Return value: |
|
1360 |
**/ |
|
1361 |
gboolean |
|
1362 |
lm_connection_send_with_reply (LmConnection *connection, |
|
1363 |
LmMessage *message, |
|
1364 |
LmMessageHandler *handler, |
|
1365 |
GError **error) |
|
1366 |
{ |
|
1367 |
gchar *id; |
|
1368 |
||
1369 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
1370 |
g_return_val_if_fail (message != NULL, FALSE); |
|
1371 |
g_return_val_if_fail (handler != NULL, FALSE); |
|
1372 |
||
1373 |
if (lm_message_node_get_attribute (message->node, "id")) { |
|
1374 |
id = g_strdup (lm_message_node_get_attribute (message->node, |
|
1375 |
"id")); |
|
1376 |
} else { |
|
1377 |
id = _lm_utils_generate_id (); |
|
1378 |
lm_message_node_set_attributes (message->node, "id", id, NULL); |
|
1379 |
} |
|
1380 |
||
1381 |
g_hash_table_insert (connection->id_handlers, |
|
1382 |
id, lm_message_handler_ref (handler)); |
|
1383 |
||
1384 |
return lm_connection_send (connection, message, error); |
|
1385 |
} |
|
1386 |
||
1387 |
/** |
|
1388 |
* lm_connection_send_with_reply_and_block: |
|
1389 |
* @connection: an #LmConnection |
|
1390 |
* @message: an #LmMessage |
|
1391 |
* @error: Set if error was detected during sending. |
|
1392 |
* |
|
1393 |
* Send @message and wait for return. |
|
1394 |
* |
|
1395 |
* Return value: The reply |
|
1396 |
**/ |
|
1397 |
LmMessage * |
|
1398 |
lm_connection_send_with_reply_and_block (LmConnection *connection, |
|
1399 |
LmMessage *message, |
|
1400 |
GError **error) |
|
1401 |
{ |
|
1402 |
gchar *id; |
|
1403 |
LmMessage *reply = NULL; |
|
1404 |
||
1405 |
if (lm_message_node_get_attribute (message->node, "id")) { |
|
1406 |
id = g_strdup (lm_message_node_get_attribute (message->node, |
|
1407 |
"id")); |
|
1408 |
} else { |
|
1409 |
id = _lm_utils_generate_id (); |
|
1410 |
lm_message_node_set_attributes (message->node, "id", id, NULL); |
|
1411 |
} |
|
1412 |
||
1413 |
g_source_remove (g_source_get_id (connection->incoming_source)); |
|
1414 |
g_source_unref (connection->incoming_source); |
|
1415 |
||
1416 |
lm_connection_send (connection, message, error); |
|
1417 |
||
1418 |
while (!reply) { |
|
1419 |
const gchar *m_id; |
|
39
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
1420 |
gint n; |
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
1421 |
|
1 | 1422 |
g_main_context_iteration (NULL, TRUE); |
39
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
1423 |
|
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
1424 |
if (lm_queue_is_empty (connection->incoming_messages)) { |
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
1425 |
continue; |
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
1426 |
} |
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
1427 |
|
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
1428 |
for (n = 0; n < connection->incoming_messages->length; n++) { |
1 | 1429 |
LmMessage *m; |
39
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
1430 |
|
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
1431 |
m = lm_queue_peek_nth (connection->incoming_messages, n); |
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
1432 |
|
1 | 1433 |
m_id = lm_message_node_get_attribute (m->node, "id"); |
1434 |
||
1435 |
if (m_id && strcmp (m_id, id) == 0) { |
|
1436 |
reply = m; |
|
39
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
1437 |
lm_queue_remove_nth (connection->incoming_messages, n); |
b909b5af5a97
2003-10-01 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
37
diff
changeset
|
1438 |
break; |
1 | 1439 |
} |
1440 |
} |
|
1441 |
} |
|
1442 |
||
1443 |
g_free (id); |
|
1444 |
connection->incoming_source = connection_create_source (connection); |
|
1445 |
g_source_attach (connection->incoming_source, NULL); |
|
1446 |
||
1447 |
return reply; |
|
1448 |
} |
|
1449 |
||
1450 |
/** |
|
1451 |
* lm_connection_register_message_handler: |
|
1452 |
* @connection: Connection to register a handler for. |
|
1453 |
* @handler: Message handler to register. |
|
1454 |
* @type: Message type that @handler will handle. |
|
1455 |
* @priority: The priority in which to call @handler. |
|
1456 |
* |
|
1457 |
* Registers a #LmMessageHandler to handle incoming messages of a certain type. |
|
1458 |
* To unregister the handler call lm_connection_unregister_message_handler(). |
|
1459 |
**/ |
|
1460 |
void |
|
1461 |
lm_connection_register_message_handler (LmConnection *connection, |
|
1462 |
LmMessageHandler *handler, |
|
1463 |
LmMessageType type, |
|
1464 |
LmHandlerPriority priority) |
|
1465 |
{ |
|
1466 |
HandlerData *hd; |
|
1467 |
||
1468 |
g_return_if_fail (connection != NULL); |
|
1469 |
g_return_if_fail (handler != NULL); |
|
1470 |
g_return_if_fail (type != LM_MESSAGE_TYPE_UNKNOWN); |
|
1471 |
||
1472 |
hd = g_new0 (HandlerData, 1); |
|
1473 |
hd->priority = priority; |
|
1474 |
hd->handler = lm_message_handler_ref (handler); |
|
1475 |
||
1476 |
connection->handlers[type] = g_slist_insert_sorted (connection->handlers[type], |
|
1477 |
hd, |
|
1478 |
(GCompareFunc) connection_handler_compare_func); |
|
1479 |
} |
|
1480 |
||
1481 |
/** |
|
1482 |
* lm_connection_unregister_message_handler: |
|
1483 |
* @connection: Connection to unregister a handler for. |
|
1484 |
* @handler: The handler to unregister. |
|
1485 |
* @type: What type of messages to unregister this handler for. |
|
1486 |
* |
|
1487 |
* Unregisters a handler for @connection. @handler will no longer be called |
|
1488 |
* when incoming messages of @type arrive. |
|
1489 |
**/ |
|
1490 |
void |
|
1491 |
lm_connection_unregister_message_handler (LmConnection *connection, |
|
1492 |
LmMessageHandler *handler, |
|
1493 |
LmMessageType type) |
|
1494 |
{ |
|
1495 |
GSList *l, *prev = NULL; |
|
1496 |
||
1497 |
g_return_if_fail (connection != NULL); |
|
1498 |
g_return_if_fail (handler != NULL); |
|
1499 |
g_return_if_fail (type != LM_MESSAGE_TYPE_UNKNOWN); |
|
1500 |
||
1501 |
for (l = connection->handlers[type]; l; l = l->next) { |
|
1502 |
HandlerData *hd = (HandlerData *) l->data; |
|
1503 |
||
1504 |
if (hd->handler == handler) { |
|
1505 |
if (prev) { |
|
1506 |
prev->next = l->next; |
|
1507 |
} else { |
|
1508 |
connection->handlers[type] = l->next; |
|
1509 |
} |
|
1510 |
l->next = NULL; |
|
1511 |
g_slist_free (l); |
|
1512 |
lm_message_handler_unref (hd->handler); |
|
1513 |
g_free (hd); |
|
1514 |
break; |
|
1515 |
} |
|
1516 |
prev = l; |
|
1517 |
} |
|
1518 |
} |
|
1519 |
||
1520 |
/** |
|
1521 |
* lm_connection_set_disconnect_function: |
|
1522 |
* @connection: Connection to register disconnect callback for. |
|
1523 |
* @function: Function to be called when @connection is closed. |
|
1524 |
* @user_data: User data passed to @function. |
|
1525 |
* @notify: Function that will be called with @user_data when @user_data needs to be freed. Pass #NULL if it shouldn't be freed. |
|
1526 |
* |
|
1527 |
* Set the callback that will be called when a connection is closed. |
|
1528 |
**/ |
|
1529 |
void |
|
1530 |
lm_connection_set_disconnect_function (LmConnection *connection, |
|
1531 |
LmDisconnectFunction function, |
|
1532 |
gpointer user_data, |
|
1533 |
GDestroyNotify notify) |
|
1534 |
{ |
|
1535 |
if (connection->disconnect_cb) { |
|
1536 |
_lm_utils_free_callback (connection->disconnect_cb); |
|
1537 |
} |
|
1538 |
||
1539 |
connection->disconnect_cb = _lm_utils_new_callback (function, |
|
1540 |
user_data, |
|
1541 |
notify); |
|
1542 |
} |
|
1543 |
||
1544 |
/** |
|
1545 |
* lm_connection_send_raw: |
|
1546 |
* @connection: Connection used to send |
|
1547 |
* @str: The string to send, the entire string will be sent. |
|
1548 |
* @error: Set if error was detected during sending. |
|
1549 |
* |
|
1550 |
* Asynchronous call to send a raw string. Useful for debugging and testing. |
|
1551 |
* |
|
1552 |
* Return value: Returns #TRUE if no errors was detected during sending, |
|
1553 |
* #FALSE otherwise. |
|
1554 |
**/ |
|
1555 |
gboolean |
|
1556 |
lm_connection_send_raw (LmConnection *connection, |
|
1557 |
const gchar *str, |
|
1558 |
GError **error) |
|
1559 |
{ |
|
1560 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
1561 |
||
1562 |
return connection_send (connection, str, -1, error); |
|
1563 |
} |
|
1564 |
||
1565 |
/** |
|
1566 |
* lm_connection_ref: |
|
1567 |
* @connection: Connection to add a reference to. |
|
1568 |
* |
|
1569 |
* Add a reference on @connection. To remove a reference call |
|
1570 |
* lm_connection_unref(). |
|
1571 |
* |
|
1572 |
* Return value: Returns the same connection. |
|
1573 |
**/ |
|
1574 |
LmConnection* |
|
1575 |
lm_connection_ref (LmConnection *connection) |
|
1576 |
{ |
|
1577 |
g_return_val_if_fail (connection != NULL, NULL); |
|
1578 |
||
1579 |
connection->ref_count++; |
|
1580 |
||
1581 |
return connection; |
|
1582 |
} |
|
1583 |
||
1584 |
/** |
|
1585 |
* lm_connection_unref: |
|
1586 |
* @connection: Connection to remove reference from. |
|
1587 |
* |
|
1588 |
* Removes a reference on @connection. If there are no references to |
|
1589 |
* @connection it will be fried and shouldn't be used again. |
|
1590 |
**/ |
|
1591 |
void |
|
1592 |
lm_connection_unref (LmConnection *connection) |
|
1593 |
{ |
|
1594 |
g_return_if_fail (connection != NULL); |
|
1595 |
||
1596 |
connection->ref_count--; |
|
1597 |
||
1598 |
if (connection->ref_count == 0) { |
|
1599 |
connection_free (connection); |
|
1600 |
} |
|
1601 |
} |
|
1602 |
||
1603 |
#if 0 |
|
1604 |
void |
|
1605 |
lm_connection_register (LmConnection *connection, |
|
1606 |
const gchar *username, |
|
1607 |
const gchar *password, |
|
1608 |
const gchar *resource, |
|
1609 |
LmRegisterCallback callback, |
|
1610 |
gpointer user_data) |
|
1611 |
{ |
|
1612 |
LmElement *element; |
|
1613 |
LmNode *q_node; |
|
1614 |
gchar *id; |
|
1615 |
static gint register_id = 0; |
|
1616 |
|
|
1617 |
g_return_if_fail (connection != NULL); |
|
1618 |
g_return_if_fail (lm_connection_is_open (connection)); |
|
1619 |
|
|
1620 |
/* Use lm:iq:register name space */ |
|
1621 |
||
1622 |
element = lm_iq_new (LM_IQ_TYPE_SET); |
|
1623 |
|
|
1624 |
q_node = lm_node_new ("query"); |
|
1625 |
lm_node_set_attribute (q_node, "xmlns", JABBER_IQ_REGISTER); |
|
1626 |
lm_node_add_child (q_node, "username", username); |
|
1627 |
lm_node_add_child (q_node, "password", password); |
|
1628 |
lm_node_add_child (q_node, "resource", resource); |
|
1629 |
||
1630 |
lm_element_add_child_node (element, q_node); |
|
1631 |
|
|
1632 |
id = g_strdup_printf ("register_%d", ++register_id); |
|
1633 |
lm_element_set_id (element, id); |
|
1634 |
|
|
1635 |
lm_connection_send (connection, element, NULL); |
|
1636 |
connection_add_callback (connection, id, callback, user_data); |
|
1637 |
lm_element_unref (element); |
|
1638 |
||
1639 |
g_free (id); |
|
1640 |
||
1641 |
|
|
1642 |
} |
|
1643 |
||
1644 |
#endif |