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