author | hallski <hallski> |
Mon, 04 Aug 2003 20:30:05 +0000 | |
changeset 26 | 5dd8b6da1454 |
parent 21 | 091f3e5ec468 |
child 28 | 8784d9121214 |
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 |
{ |
|
376 |
gchar buf[IN_BUFFER_SIZE]; |
|
377 |
gsize bytes_read; |
|
378 |
||
379 |
if (!connection->io_channel) { |
|
380 |
return FALSE; |
|
381 |
} |
|
26
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
382 |
|
15
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
383 |
#ifdef HAVE_GNUTLS |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
384 |
if (connection->use_ssl) { |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
385 |
bytes_read = gnutls_record_recv (connection->gnutls_session, |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
386 |
buf,IN_BUFFER_SIZE - 1); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
387 |
if (bytes_read <= 0) { |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
388 |
connection_error_event (connection->io_channel, |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
389 |
G_IO_HUP, |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
390 |
connection); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
391 |
} |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
392 |
} else { |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
393 |
#endif |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
394 |
g_io_channel_read_chars (connection->io_channel, |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
395 |
buf, IN_BUFFER_SIZE - 1, |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
396 |
&bytes_read, |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
397 |
NULL); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
398 |
#ifdef HAVE_GNUTLS |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
399 |
} |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
400 |
#endif |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
401 |
|
1 | 402 |
buf[bytes_read] = '\0'; |
403 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, "\nRECV:\n"); |
|
404 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, |
|
405 |
"-----------------------------------\n"); |
|
406 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, "%s\n", buf); |
|
407 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, |
|
408 |
"-----------------------------------\n"); |
|
409 |
||
410 |
lm_parser_parse (connection->parser, buf); |
|
411 |
||
412 |
return TRUE; |
|
413 |
} |
|
414 |
||
415 |
static gboolean |
|
416 |
connection_error_event (GIOChannel *source, |
|
417 |
GIOCondition condition, |
|
418 |
LmConnection *connection) |
|
419 |
{ |
|
26
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
420 |
if (!connection->io_channel) { |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
421 |
return FALSE; |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
422 |
} |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
423 |
|
1 | 424 |
lm_verbose ("Error event: %d\n", condition); |
425 |
||
426 |
connection_do_close (connection); |
|
427 |
connection_signal_disconnect (connection, LM_DISCONNECT_REASON_ERROR); |
|
428 |
||
429 |
return TRUE; |
|
430 |
} |
|
431 |
||
432 |
static gboolean |
|
433 |
connection_hup_event (GIOChannel *source, |
|
434 |
GIOCondition condition, |
|
435 |
LmConnection *connection) |
|
436 |
{ |
|
26
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
437 |
if (!connection->io_channel) { |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
438 |
return FALSE; |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
439 |
} |
5dd8b6da1454
2003-08-04 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
21
diff
changeset
|
440 |
|
1 | 441 |
lm_verbose ("HUP event\n"); |
442 |
||
443 |
connection_do_close (connection); |
|
444 |
connection_signal_disconnect (connection, LM_DISCONNECT_REASON_HUP); |
|
445 |
||
446 |
return TRUE; |
|
447 |
} |
|
448 |
||
449 |
static gboolean |
|
450 |
connection_send (LmConnection *connection, |
|
451 |
const gchar *str, |
|
452 |
gint len, |
|
453 |
GError **error) |
|
454 |
{ |
|
455 |
gsize bytes_written; |
|
456 |
||
457 |
if (!lm_connection_is_open (connection)) { |
|
458 |
g_set_error (error, |
|
459 |
LM_ERROR, |
|
460 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
461 |
"Connection is not open, call lm_connection_open() first"); |
|
462 |
return FALSE; |
|
463 |
} |
|
464 |
||
465 |
if (len == -1) { |
|
466 |
len = strlen (str); |
|
467 |
} |
|
468 |
||
469 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, "\nSEND:\n"); |
|
470 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, |
|
471 |
"-----------------------------------\n"); |
|
472 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, "%s\n", str); |
|
473 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, |
|
474 |
"-----------------------------------\n"); |
|
475 |
||
15
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
476 |
#ifdef HAVE_GNUTLS |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
477 |
if (connection->use_ssl) { |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
478 |
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
|
479 |
if (bytes_written != GNUTLS_E_INTERRUPTED && |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
480 |
bytes_written != GNUTLS_E_AGAIN) |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
481 |
{ |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
482 |
connection_error_event (connection->io_channel, G_IO_HUP, |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
483 |
connection); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
484 |
} |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
485 |
|
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
486 |
} else { |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
487 |
#endif |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
488 |
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
|
489 |
&bytes_written, NULL); |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
490 |
#ifdef HAVE_GNUTLS |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
491 |
} |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
492 |
#endif |
1 | 493 |
|
494 |
return TRUE; |
|
495 |
} |
|
496 |
||
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
497 |
typedef struct { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
498 |
gchar *username; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
499 |
gchar *password; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
500 |
gchar *resource; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
501 |
} AuthReqData; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
502 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
503 |
static void |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
504 |
auth_req_data_free (AuthReqData *data) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
505 |
g_free (data->username); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
506 |
g_free (data->password); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
507 |
g_free (data->resource); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
508 |
g_free (data); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
509 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
510 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
511 |
static LmMessage * |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
512 |
connection_create_auth_req_msg (const gchar *username) |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
513 |
{ |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
514 |
LmMessage *m; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
515 |
LmMessageNode *q_node; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
516 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
517 |
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
|
518 |
LM_MESSAGE_SUB_TYPE_GET); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
519 |
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
|
520 |
lm_message_node_set_attributes (q_node, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
521 |
"xmlns", "jabber:iq:auth", |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
522 |
NULL); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
523 |
lm_message_node_add_child (q_node, "username", username); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
524 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
525 |
return m; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
526 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
527 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
528 |
static LmMessage * |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
529 |
connection_create_auth_msg (LmConnection *connection, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
530 |
const gchar *username, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
531 |
const gchar *password, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
532 |
const gchar *resource, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
533 |
gint auth_type) |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
534 |
{ |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
535 |
LmMessage *auth_msg; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
536 |
LmMessageNode *q_node; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
537 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
538 |
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
|
539 |
LM_MESSAGE_SUB_TYPE_SET); |
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 |
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
|
542 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
543 |
lm_message_node_set_attributes (q_node, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
544 |
"xmlns", "jabber:iq:auth", |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
545 |
NULL); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
546 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
547 |
lm_message_node_add_child (q_node, "username", username); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
548 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
549 |
if (auth_type & AUTH_TYPE_0K) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
550 |
lm_verbose ("Using 0k auth (not implemented yet)\n"); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
551 |
/* TODO: Should probably use this? */ |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
552 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
553 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
554 |
if (auth_type & AUTH_TYPE_DIGEST) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
555 |
gchar *str; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
556 |
const gchar *digest; |
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 |
lm_verbose ("Using digest\n"); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
559 |
str = g_strconcat (connection->stream_id, password, NULL); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
560 |
digest = lm_sha_hash (str); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
561 |
g_free (str); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
562 |
lm_message_node_add_child (q_node, "digest", digest); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
563 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
564 |
else if (auth_type & AUTH_TYPE_PLAIN) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
565 |
lm_verbose ("Using plaintext auth\n"); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
566 |
lm_message_node_add_child (q_node, "password", password); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
567 |
} else { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
568 |
/* TODO: Report error somehow */ |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
569 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
570 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
571 |
lm_message_node_add_child (q_node, "resource", resource); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
572 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
573 |
return auth_msg; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
574 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
575 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
576 |
static LmHandlerResult |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
577 |
connection_auth_req_reply (LmMessageHandler *handler, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
578 |
LmConnection *connection, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
579 |
LmMessage *m, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
580 |
gpointer user_data) |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
581 |
{ |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
582 |
int auth_type; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
583 |
LmMessage *auth_msg; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
584 |
LmMessageHandler *auth_handler; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
585 |
AuthReqData *data = (AuthReqData *) user_data; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
586 |
gboolean result; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
587 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
588 |
auth_type = connection_check_auth_type (m); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
589 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
590 |
auth_msg = connection_create_auth_msg (connection, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
591 |
data->username, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
592 |
data->password, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
593 |
data->resource, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
594 |
auth_type); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
595 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
596 |
auth_handler = lm_message_handler_new (connection_auth_reply, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
597 |
NULL, NULL); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
598 |
result = lm_connection_send_with_reply (connection, auth_msg, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
599 |
auth_handler, NULL); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
600 |
lm_message_handler_unref (auth_handler); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
601 |
lm_message_unref (auth_msg); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
602 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
603 |
return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
604 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
605 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
606 |
static int |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
607 |
connection_check_auth_type (LmMessage *auth_req_rpl) |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
608 |
{ |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
609 |
LmMessageNode *q_node; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
610 |
gint ret_val = 0; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
611 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
612 |
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
|
613 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
614 |
if (!q_node) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
615 |
return AUTH_TYPE_PLAIN; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
616 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
617 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
618 |
if (lm_message_node_get_child (q_node, "password")) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
619 |
ret_val |= AUTH_TYPE_PLAIN; |
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 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
622 |
if (lm_message_node_get_child (q_node, "digest")) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
623 |
ret_val |= AUTH_TYPE_DIGEST; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
624 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
625 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
626 |
if (lm_message_node_get_child (q_node, "sequence") && |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
627 |
lm_message_node_get_child (q_node, "token")) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
628 |
ret_val |= AUTH_TYPE_0K; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
629 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
630 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
631 |
return ret_val; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
632 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
633 |
|
1 | 634 |
static LmHandlerResult |
635 |
connection_auth_reply (LmMessageHandler *handler, |
|
636 |
LmConnection *connection, |
|
637 |
LmMessage *m, |
|
638 |
gpointer user_data) |
|
639 |
{ |
|
640 |
const gchar *type; |
|
641 |
gboolean result = TRUE; |
|
642 |
||
643 |
g_return_val_if_fail (connection != NULL, |
|
644 |
LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS); |
|
645 |
||
646 |
||
647 |
type = lm_message_node_get_attribute (m->node, "type"); |
|
648 |
if (strcmp (type, "result") == 0) { |
|
649 |
result = TRUE; |
|
650 |
} |
|
651 |
else if (strcmp (type, "error") == 0) { |
|
652 |
result = FALSE; |
|
653 |
} |
|
654 |
||
655 |
lm_verbose ("AUTH reply: %d\n", result); |
|
656 |
||
657 |
if (connection->auth_cb && connection->auth_cb->func) { |
|
658 |
LmCallback *cb = connection->auth_cb; |
|
659 |
||
660 |
(* ((LmResultFunction) cb->func)) (connection, |
|
661 |
result, cb->user_data); |
|
662 |
} |
|
663 |
||
664 |
_lm_utils_free_callback (connection->auth_cb); |
|
665 |
connection->auth_cb = NULL; |
|
666 |
||
667 |
return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
|
668 |
} |
|
669 |
||
670 |
||
671 |
static void |
|
672 |
connection_stream_received (LmConnection *connection, LmMessage *m) |
|
673 |
{ |
|
674 |
gboolean result; |
|
675 |
||
676 |
g_return_if_fail (connection != NULL); |
|
677 |
g_return_if_fail (m != NULL); |
|
678 |
||
679 |
connection->stream_id = g_strdup (lm_message_node_get_attribute (m->node, |
|
680 |
"id"));; |
|
681 |
||
682 |
lm_verbose ("Stream received: %s\n", connection->stream_id); |
|
683 |
||
684 |
/* Check to see if the stream is correctly set up */ |
|
685 |
result = TRUE; |
|
686 |
||
687 |
if (connection->open_cb && connection->open_cb->func) { |
|
688 |
LmCallback *cb = connection->open_cb; |
|
689 |
||
690 |
(* ((LmResultFunction) cb->func)) (connection, result, |
|
691 |
cb->user_data); |
|
692 |
} |
|
693 |
||
694 |
_lm_utils_free_callback (connection->open_cb); |
|
695 |
connection->open_cb = NULL; |
|
696 |
} |
|
697 |
||
698 |
static gint |
|
699 |
connection_handler_compare_func (HandlerData *a, HandlerData *b) |
|
700 |
{ |
|
701 |
return b->priority - a->priority; |
|
702 |
} |
|
703 |
||
704 |
static gboolean |
|
705 |
connection_incoming_prepare (GSource *source, gint *timeout) |
|
706 |
{ |
|
707 |
LmConnection *connection; |
|
708 |
||
709 |
connection = ((LmIncomingSource *)source)->connection; |
|
710 |
||
711 |
return !g_queue_is_empty (connection->incoming_messages); |
|
712 |
} |
|
713 |
||
714 |
static gboolean |
|
715 |
connection_incoming_check (GSource *source) |
|
716 |
{ |
|
717 |
return FALSE; |
|
718 |
} |
|
719 |
||
720 |
static gboolean |
|
721 |
connection_incoming_dispatch (GSource *source, |
|
722 |
GSourceFunc callback, |
|
723 |
gpointer user_data) |
|
724 |
{ |
|
725 |
LmConnection *connection; |
|
726 |
LmMessage *m; |
|
727 |
||
728 |
connection = ((LmIncomingSource *) source)->connection; |
|
729 |
||
730 |
m = (LmMessage *) g_queue_pop_head (connection->incoming_messages); |
|
731 |
||
732 |
if (m) { |
|
733 |
connection_handle_message (connection, m); |
|
734 |
lm_message_unref (m); |
|
735 |
} |
|
736 |
||
737 |
return TRUE; |
|
738 |
} |
|
739 |
||
740 |
static GSource * |
|
741 |
connection_create_source (LmConnection *connection) |
|
742 |
{ |
|
743 |
GSource *source; |
|
744 |
||
745 |
source = g_source_new (&incoming_funcs, sizeof (LmIncomingSource)); |
|
746 |
((LmIncomingSource *) source)->connection = connection; |
|
747 |
||
748 |
return source; |
|
749 |
} |
|
750 |
||
751 |
static void |
|
752 |
connection_signal_disconnect (LmConnection *connection, |
|
753 |
LmDisconnectReason reason) |
|
754 |
{ |
|
755 |
if (connection->disconnect_cb && connection->disconnect_cb->func) { |
|
756 |
LmCallback *cb = connection->disconnect_cb; |
|
757 |
||
758 |
(* ((LmDisconnectFunction) cb->func)) (connection, |
|
759 |
reason, |
|
760 |
cb->user_data); |
|
761 |
} |
|
762 |
} |
|
763 |
||
764 |
/** |
|
765 |
* lm_connection_new: |
|
766 |
* @server: The hostname to the server for the connection. |
|
767 |
* |
|
768 |
* Creates a new closed connection. To open the connection call |
|
769 |
* lm_connection_open(). @server can be #NULL but must be set before calling lm_connection_open(). |
|
770 |
* |
|
771 |
* Return value: A newly created LmConnection, should be unreffed with lm_connection_unref(). |
|
772 |
**/ |
|
773 |
LmConnection * |
|
774 |
lm_connection_new (const gchar *server) |
|
775 |
{ |
|
776 |
LmConnection *connection; |
|
777 |
gint i; |
|
778 |
||
779 |
lm_debug_init (); |
|
780 |
||
781 |
connection = g_new0 (LmConnection, 1); |
|
782 |
||
783 |
if (server) { |
|
784 |
connection->server = g_strdup (server); |
|
785 |
} else { |
|
786 |
connection->server = NULL; |
|
787 |
} |
|
788 |
||
789 |
connection->port = LM_CONNECTION_DEFAULT_PORT; |
|
790 |
connection->use_ssl = FALSE; |
|
791 |
connection->disconnect_cb = NULL; |
|
792 |
connection->incoming_messages = g_queue_new (); |
|
793 |
connection->incoming_source = connection_create_source (connection); |
|
794 |
||
795 |
connection->id_handlers = g_hash_table_new_full (g_str_hash, |
|
796 |
g_str_equal, |
|
797 |
g_free, |
|
798 |
(GDestroyNotify) lm_message_handler_unref); |
|
799 |
connection->ref_count = 1; |
|
800 |
g_source_attach (connection->incoming_source, NULL); |
|
801 |
||
802 |
for (i = 0; i < LM_MESSAGE_TYPE_UNKNOWN; ++i) { |
|
803 |
connection->handlers[i] = NULL; |
|
804 |
} |
|
805 |
||
806 |
connection->parser = lm_parser_new |
|
807 |
((LmParserMessageFunction) connection_new_message_cb, |
|
808 |
connection, NULL); |
|
809 |
||
810 |
return connection; |
|
811 |
} |
|
812 |
||
813 |
/** |
|
814 |
* lm_connection_open: |
|
815 |
* @connection: #LmConnection to open |
|
816 |
* @function: Callback function that will be called when the connection is open. |
|
817 |
* @user_data: User data that will be passed to @function. |
|
818 |
* @notify: Function for freeing that user_data, can be NULL. |
|
819 |
* @error: location to store error, or %NULL |
|
820 |
* |
|
821 |
* An async call to open @connection. When the connection is open @function will be called. |
|
822 |
* |
|
823 |
* Return value: #TRUE if everything went fine, otherwise #FALSE. |
|
824 |
**/ |
|
825 |
gboolean |
|
826 |
lm_connection_open (LmConnection *connection, |
|
827 |
LmResultFunction function, |
|
828 |
gpointer user_data, |
|
829 |
GDestroyNotify notify, |
|
830 |
GError **error) |
|
831 |
{ |
|
832 |
LmMessage *m; |
|
833 |
gboolean result; |
|
834 |
||
835 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
836 |
||
837 |
if (lm_connection_is_open (connection)) { |
|
838 |
g_set_error (error, |
|
839 |
LM_ERROR, |
|
840 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
841 |
"Connection is already open, call lm_connection_close() first"); |
|
842 |
return FALSE; |
|
843 |
} |
|
844 |
||
845 |
if (!connection->server) { |
|
846 |
g_set_error (error, |
|
847 |
LM_ERROR, |
|
848 |
LM_ERROR_CONNECTION_OPEN, |
|
849 |
"You need to set the server hostname in the call to lm_connection_new()"); |
|
850 |
return FALSE; |
|
851 |
} |
|
852 |
||
853 |
connection->open_cb = _lm_utils_new_callback (function, user_data, notify); |
|
854 |
||
855 |
lm_verbose ("Connecting to: %s:%d\n", |
|
856 |
connection->server, connection->port); |
|
857 |
||
858 |
if (!connection_do_open (connection, error)) { |
|
859 |
return FALSE; |
|
860 |
} |
|
861 |
||
862 |
m = lm_message_new (connection->server, LM_MESSAGE_TYPE_STREAM); |
|
863 |
lm_message_node_set_attributes (m->node, |
|
864 |
"xmlns:stream", "http://etherx.jabber.org/streams", |
|
865 |
"xmlns", "jabber:client", |
|
866 |
NULL); |
|
867 |
||
868 |
lm_verbose ("Opening stream..."); |
|
869 |
||
870 |
result = lm_connection_send (connection, m, error); |
|
871 |
lm_message_unref (m); |
|
872 |
||
873 |
return result; |
|
874 |
} |
|
875 |
||
876 |
/** |
|
877 |
* lm_connection_open_and_block: |
|
878 |
* @connection: an #LmConnection |
|
879 |
* @error: location to store error, or %NULL |
|
880 |
* |
|
881 |
* Opens @connection and waits until the stream is setup. |
|
882 |
* |
|
883 |
* Return value: #TRUE if no errors where encountered during opening and stream setup successfully, #FALSE otherwise. |
|
884 |
**/ |
|
885 |
gboolean |
|
886 |
lm_connection_open_and_block (LmConnection *connection, GError **error) |
|
887 |
{ |
|
888 |
LmMessage *m; |
|
889 |
gboolean result; |
|
890 |
gboolean finished = FALSE; |
|
891 |
gboolean ret_val = FALSE; |
|
892 |
gint last_len = 0; |
|
893 |
||
894 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
895 |
||
896 |
if (lm_connection_is_open (connection)) { |
|
897 |
g_set_error (error, |
|
898 |
LM_ERROR, |
|
899 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
900 |
"Connection is already open, call lm_connection_close() first"); |
|
901 |
return FALSE; |
|
902 |
} |
|
903 |
if (!connection->server) { |
|
904 |
g_set_error (error, |
|
905 |
LM_ERROR, |
|
906 |
LM_ERROR_CONNECTION_OPEN, |
|
907 |
"You need to set the server hostname in the call to lm_connection_new()"); |
|
908 |
return FALSE; |
|
909 |
} |
|
910 |
||
911 |
lm_verbose ("(Block)Connecting to: %s:%d\n", |
|
912 |
connection->server, connection->port); |
|
913 |
||
914 |
if (!connection_do_open (connection, error)) { |
|
915 |
return FALSE; |
|
916 |
} |
|
917 |
||
918 |
m = lm_message_new (connection->server, LM_MESSAGE_TYPE_STREAM); |
|
919 |
lm_message_node_set_attributes (m->node, |
|
920 |
"xmlns:stream", "http://etherx.jabber.org/streams", |
|
921 |
"xmlns", "jabber:client", |
|
922 |
NULL); |
|
923 |
||
924 |
lm_verbose ("Sending stream: \n%s\n", |
|
925 |
lm_message_node_to_string (m->node)); |
|
926 |
||
927 |
result = lm_connection_send (connection, m, error); |
|
928 |
lm_message_unref (m); |
|
929 |
||
930 |
g_source_remove (g_source_get_id (connection->incoming_source)); |
|
931 |
g_source_unref (connection->incoming_source); |
|
932 |
||
933 |
while (!finished) { |
|
934 |
gint len; |
|
935 |
||
936 |
g_main_context_iteration (NULL, TRUE); |
|
937 |
||
938 |
len = connection->incoming_messages->length; |
|
939 |
||
940 |
if (len > last_len) { |
|
941 |
LmMessage *m; |
|
942 |
||
943 |
last_len = len; |
|
944 |
m = (LmMessage *) g_queue_peek_tail (connection->incoming_messages); |
|
945 |
if (lm_message_get_type (m) == LM_MESSAGE_TYPE_STREAM) { |
|
946 |
connection->stream_id = |
|
947 |
g_strdup (lm_message_node_get_attribute (m->node, "id")); |
|
948 |
ret_val = TRUE; |
|
949 |
finished = TRUE; |
|
950 |
g_queue_pop_tail (connection->incoming_messages); |
|
951 |
} |
|
952 |
} |
|
953 |
} |
|
954 |
||
955 |
connection->incoming_source = connection_create_source (connection); |
|
956 |
g_source_attach (connection->incoming_source, NULL); |
|
957 |
||
958 |
return ret_val; |
|
959 |
} |
|
960 |
||
961 |
/** |
|
962 |
* lm_connection_close: |
|
963 |
* @connection: #LmConnection to close |
|
964 |
* @error: location to store error, or %NULL |
|
965 |
* |
|
966 |
* 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(). |
|
967 |
* |
|
968 |
* Return value: Returns #TRUE if no errors where detected, otherwise #FALSE. |
|
969 |
**/ |
|
970 |
gboolean |
|
971 |
lm_connection_close (LmConnection *connection, |
|
972 |
GError **error) |
|
973 |
{ |
|
974 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
975 |
||
976 |
if (!lm_connection_is_open (connection)) { |
|
977 |
g_set_error (error, |
|
978 |
LM_ERROR, |
|
979 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
980 |
"Connection is not open, call lm_connection_open() first"); |
|
981 |
return FALSE; |
|
982 |
} |
|
983 |
||
984 |
lm_verbose ("Disconnecting from: %s:%d\n", |
|
985 |
connection->server, |
|
986 |
connection->port); |
|
987 |
||
988 |
if (!connection_send (connection, "</stream:stream>", -1, error)) { |
|
989 |
return FALSE; |
|
990 |
} |
|
991 |
||
992 |
g_io_channel_flush (connection->io_channel, NULL); |
|
993 |
||
994 |
connection_do_close (connection); |
|
995 |
connection_signal_disconnect (connection, LM_DISCONNECT_REASON_OK); |
|
996 |
||
997 |
return TRUE; |
|
998 |
} |
|
999 |
||
1000 |
/** |
|
1001 |
* lm_connection_authenticate: |
|
1002 |
* @connection: #LmConnection to authenticate. |
|
1003 |
* @username: Username used to authenticate. |
|
1004 |
* @password: Password corresponding to @username. |
|
1005 |
* @resource: Resource used for this connection. |
|
1006 |
* @function: Callback called when authentication is finished. |
|
1007 |
* @user_data: Userdata passed to @function when called. |
|
1008 |
* @notify: Destroy function to free the memory used by @user_data, can be NULL. |
|
1009 |
* @error: location to store error, or %NULL |
|
1010 |
* |
|
1011 |
* Tries to authenticate a user against the server. The #LmResult in the result callback will tell if it succeeded or not. |
|
1012 |
* |
|
1013 |
* Return value: #TRUE if no errors where detected while sending the authentication message, #FALSE otherwise. |
|
1014 |
**/ |
|
1015 |
gboolean |
|
1016 |
lm_connection_authenticate (LmConnection *connection, |
|
1017 |
const gchar *username, |
|
1018 |
const gchar *password, |
|
1019 |
const gchar *resource, |
|
1020 |
LmResultFunction function, |
|
1021 |
gpointer user_data, |
|
1022 |
GDestroyNotify notify, |
|
1023 |
GError **error) |
|
1024 |
{ |
|
1025 |
LmMessage *m; |
|
1026 |
LmMessageHandler *handler; |
|
1027 |
gboolean result; |
|
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1028 |
AuthReqData *data; |
1 | 1029 |
|
1030 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
1031 |
g_return_val_if_fail (username != NULL, FALSE); |
|
1032 |
g_return_val_if_fail (password != NULL, FALSE); |
|
1033 |
g_return_val_if_fail (resource != NULL, FALSE); |
|
1034 |
||
1035 |
if (!lm_connection_is_open (connection)) { |
|
1036 |
g_set_error (error, |
|
1037 |
LM_ERROR, |
|
1038 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
1039 |
"Connection is not open, call lm_connection_open() first"); |
|
1040 |
return FALSE; |
|
1041 |
} |
|
1042 |
||
1043 |
connection->auth_cb = _lm_utils_new_callback (function, |
|
1044 |
user_data, |
|
1045 |
notify); |
|
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1046 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1047 |
m = connection_create_auth_req_msg (username); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1048 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1049 |
data = g_new0 (AuthReqData, 1); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1050 |
data->username = g_strdup (username); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1051 |
data->password = g_strdup (password); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1052 |
data->resource = g_strdup (resource); |
1 | 1053 |
|
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1054 |
handler = lm_message_handler_new (connection_auth_req_reply, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1055 |
data, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1056 |
(GDestroyNotify) auth_req_data_free); |
1 | 1057 |
result = lm_connection_send_with_reply (connection, m, handler, error); |
1058 |
||
1059 |
lm_message_handler_unref (handler); |
|
1060 |
lm_message_unref (m); |
|
1061 |
||
1062 |
return result; |
|
1063 |
} |
|
1064 |
||
1065 |
/** |
|
1066 |
* lm_connection_authenticate_and_block: |
|
1067 |
* @connection: an #LmConnection |
|
1068 |
* @username: Username used to authenticate. |
|
1069 |
* @password: Password corresponding to @username. |
|
1070 |
* @resource: Resource used for this connection. |
|
1071 |
* @error: location to store error, or %NULL |
|
1072 |
* |
|
1073 |
* 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. |
|
1074 |
* |
|
1075 |
* Return value: #TRUE if no errors where detected and authentication was successful. #FALSE otherwise. |
|
1076 |
**/ |
|
1077 |
gboolean |
|
1078 |
lm_connection_authenticate_and_block (LmConnection *connection, |
|
1079 |
const gchar *username, |
|
1080 |
const gchar *password, |
|
1081 |
const gchar *resource, |
|
1082 |
GError **error) |
|
1083 |
{ |
|
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1084 |
LmMessage *m; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1085 |
LmMessage *result; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1086 |
LmMessageSubType type; |
1 | 1087 |
|
1088 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
1089 |
g_return_val_if_fail (username != NULL, FALSE); |
|
1090 |
g_return_val_if_fail (password != NULL, FALSE); |
|
1091 |
g_return_val_if_fail (resource != NULL, FALSE); |
|
1092 |
||
1093 |
if (!lm_connection_is_open (connection)) { |
|
1094 |
g_set_error (error, |
|
1095 |
LM_ERROR, |
|
1096 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
1097 |
"Connection is not open, call lm_connection_open() first"); |
|
1098 |
return FALSE; |
|
1099 |
} |
|
1100 |
||
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1101 |
m = connection_create_auth_req_msg (username); |
1 | 1102 |
result = lm_connection_send_with_reply_and_block (connection, m, error); |
1103 |
lm_message_unref (m); |
|
1104 |
||
1105 |
if (!result) { |
|
1106 |
return FALSE; |
|
1107 |
} |
|
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1108 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1109 |
m = connection_create_auth_msg (connection, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1110 |
username, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1111 |
password, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1112 |
resource, |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1113 |
connection_check_auth_type (result)); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1114 |
lm_message_unref (result); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1115 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1116 |
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
|
1117 |
if (!result) { |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1118 |
return FALSE; |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1119 |
} |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1120 |
|
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1121 |
type = lm_message_get_sub_type (result); |
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1122 |
lm_message_unref (result); |
1 | 1123 |
|
21
091f3e5ec468
2003-07-17 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
15
diff
changeset
|
1124 |
switch (type) { |
1 | 1125 |
case LM_MESSAGE_SUB_TYPE_RESULT: |
1126 |
return TRUE; |
|
1127 |
break; |
|
1128 |
case LM_MESSAGE_SUB_TYPE_ERROR: |
|
1129 |
return FALSE; |
|
1130 |
break; |
|
1131 |
default: |
|
1132 |
g_assert_not_reached (); |
|
1133 |
break; |
|
1134 |
} |
|
1135 |
||
1136 |
return FALSE; |
|
1137 |
} |
|
1138 |
||
1139 |
/** |
|
1140 |
* lm_connection_is_open: |
|
1141 |
* @connection: #LmConnection to check if it is open. |
|
1142 |
* |
|
1143 |
* Check if the @connection is currently open. |
|
1144 |
* |
|
1145 |
* Return value: #TRUE if connection is open and #FALSE if it is closed. |
|
1146 |
**/ |
|
1147 |
gboolean |
|
1148 |
lm_connection_is_open (LmConnection *connection) |
|
1149 |
{ |
|
1150 |
return connection->is_open; |
|
1151 |
} |
|
1152 |
||
1153 |
/** |
|
1154 |
* lm_connection_is_authenticated: |
|
1155 |
* @connection: #LmConnection to check if it is authenticated |
|
1156 |
* |
|
1157 |
* Check if @connection is authenticated. |
|
1158 |
* |
|
1159 |
* Return value: #TRUE if connection is authenticated, #FALSE otherwise. |
|
1160 |
**/ |
|
1161 |
gboolean |
|
1162 |
lm_connection_is_authenticated (LmConnection *connection) |
|
1163 |
{ |
|
1164 |
return connection->is_authenticated; |
|
1165 |
} |
|
1166 |
||
1167 |
/** |
|
1168 |
* lm_connection_get_server: |
|
1169 |
* @connection: an #LmConnection |
|
1170 |
* |
|
1171 |
* Fetches the server address that @connection is using. |
|
1172 |
* |
|
1173 |
* Return value: the server address |
|
1174 |
**/ |
|
1175 |
const gchar * |
|
1176 |
lm_connection_get_server (LmConnection *connection) |
|
1177 |
{ |
|
1178 |
return connection->server; |
|
1179 |
} |
|
1180 |
||
1181 |
/** |
|
1182 |
* lm_connection_set_server: |
|
1183 |
* @connection: an #LmConnection |
|
1184 |
* @server: Address of the server |
|
1185 |
* |
|
1186 |
* Sets the server address to @connection. Notice that @connection can't be open while doing this. |
|
1187 |
**/ |
|
1188 |
void |
|
1189 |
lm_connection_set_server (LmConnection *connection, const gchar *server) |
|
1190 |
{ |
|
1191 |
if (lm_connection_is_open (connection)) { |
|
1192 |
g_warning ("Can't change server address while connected"); |
|
1193 |
return; |
|
1194 |
} |
|
1195 |
||
1196 |
if (connection->server) { |
|
1197 |
g_free (connection->server); |
|
1198 |
} |
|
1199 |
||
1200 |
connection->server = g_strdup (server); |
|
1201 |
} |
|
1202 |
||
1203 |
/** |
|
1204 |
* lm_connection_get_port: |
|
1205 |
* @connection: an #LmConnection |
|
1206 |
* |
|
1207 |
* Fetches the port tnat @connection is using. |
|
1208 |
* |
|
1209 |
* Return value: |
|
1210 |
**/ |
|
1211 |
guint |
|
1212 |
lm_connection_get_port (LmConnection *connection) |
|
1213 |
{ |
|
1214 |
return connection->port; |
|
1215 |
} |
|
1216 |
||
1217 |
/** |
|
1218 |
* lm_connection_set_port: |
|
1219 |
* @connection: an #LmConnection |
|
1220 |
* @port: server port |
|
1221 |
* |
|
1222 |
* Sets the server port of that @connection will be using. |
|
1223 |
**/ |
|
1224 |
void |
|
1225 |
lm_connection_set_port (LmConnection *connection, guint port) |
|
1226 |
{ |
|
1227 |
if (lm_connection_is_open (connection)) { |
|
1228 |
g_warning ("Can't change server port while connected"); |
|
1229 |
return; |
|
1230 |
} |
|
1231 |
||
1232 |
connection->port = port; |
|
1233 |
} |
|
1234 |
||
1235 |
/** |
|
15
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1236 |
* lm_connection_supports_ssl: |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1237 |
* |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1238 |
* Checks whether Loudmouth supports SSL or not |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1239 |
* |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1240 |
* 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
|
1241 |
**/ |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1242 |
gboolean |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1243 |
lm_connection_supports_ssl (void) |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1244 |
{ |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1245 |
#ifdef HAVE_GNUTLS |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1246 |
return TRUE; |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1247 |
#else |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1248 |
return FALSE; |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1249 |
#endif |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1250 |
} |
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1251 |
|
1ff2f81867e1
2003-07-12 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
12
diff
changeset
|
1252 |
/** |
1 | 1253 |
* lm_connection_get_use_ssl: |
1254 |
* @connection: an #LmConnection |
|
1255 |
* |
|
1256 |
* Fetches if @connection is using SSL or not |
|
1257 |
* |
|
1258 |
* Return value: #TRUE if @connection is using SSL, #FALSE otherwise. |
|
1259 |
**/ |
|
1260 |
gboolean |
|
1261 |
lm_connection_get_use_ssl (LmConnection *connection) |
|
1262 |
{ |
|
1263 |
return connection->use_ssl; |
|
1264 |
} |
|
1265 |
||
1266 |
/** |
|
1267 |
* lm_connection_set_use_ssl: |
|
1268 |
* @connection: an #LmConnection |
|
1269 |
* @use_ssl: whether to use SSL or not. |
|
1270 |
* |
|
1271 |
* Sets whether @connection should use SSL for encryping traffic to/from the server. |
|
1272 |
**/ |
|
1273 |
void |
|
1274 |
lm_connection_set_use_ssl (LmConnection *connection, gboolean use_ssl) |
|
1275 |
{ |
|
1276 |
if (lm_connection_is_open (connection)) { |
|
1277 |
g_warning ("use_ssl can't be changed while connected"); |
|
1278 |
return; |
|
1279 |
} |
|
1280 |
||
1281 |
connection->use_ssl = use_ssl; |
|
1282 |
} |
|
1283 |
||
1284 |
/** |
|
1285 |
* lm_connection_send: |
|
1286 |
* @connection: #LmConnection to send connection over. |
|
1287 |
* @message: #LmMessage to send. |
|
1288 |
* @error: location to store error, or %NULL |
|
1289 |
* |
|
1290 |
* Asynchronous call to send a message. |
|
1291 |
* |
|
1292 |
* Return value: Returns #TRUE if no errors where detected while sending, #FALSE otherwise. |
|
1293 |
**/ |
|
1294 |
gboolean |
|
1295 |
lm_connection_send (LmConnection *connection, |
|
1296 |
LmMessage *message, |
|
1297 |
GError **error) |
|
1298 |
{ |
|
1299 |
gchar *xml_str; |
|
1300 |
gchar *ch; |
|
1301 |
gboolean result; |
|
1302 |
||
1303 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
1304 |
g_return_val_if_fail (message != NULL, FALSE); |
|
1305 |
||
1306 |
xml_str = lm_message_node_to_string (message->node); |
|
1307 |
if ((ch = strstr (xml_str, "</stream:stream>"))) { |
|
1308 |
*ch = '\0'; |
|
1309 |
} |
|
1310 |
||
1311 |
result = connection_send (connection, xml_str, -1, error); |
|
1312 |
g_free (xml_str); |
|
1313 |
||
1314 |
return result; |
|
1315 |
} |
|
1316 |
||
1317 |
/** |
|
1318 |
* lm_connection_send_with_reply: |
|
1319 |
* @connection: #LmConnection used to send message. |
|
1320 |
* @message: #LmMessage to send. |
|
1321 |
* @handler: #LmMessageHandler that will be used when a reply to @message arrives |
|
1322 |
* @error: location to store error, or %NULL |
|
1323 |
* |
|
1324 |
* Send a #LmMessage which will result in a reply. |
|
1325 |
* |
|
1326 |
* Return value: |
|
1327 |
**/ |
|
1328 |
gboolean |
|
1329 |
lm_connection_send_with_reply (LmConnection *connection, |
|
1330 |
LmMessage *message, |
|
1331 |
LmMessageHandler *handler, |
|
1332 |
GError **error) |
|
1333 |
{ |
|
1334 |
gchar *id; |
|
1335 |
||
1336 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
1337 |
g_return_val_if_fail (message != NULL, FALSE); |
|
1338 |
g_return_val_if_fail (handler != NULL, FALSE); |
|
1339 |
||
1340 |
if (lm_message_node_get_attribute (message->node, "id")) { |
|
1341 |
id = g_strdup (lm_message_node_get_attribute (message->node, |
|
1342 |
"id")); |
|
1343 |
} else { |
|
1344 |
id = _lm_utils_generate_id (); |
|
1345 |
lm_message_node_set_attributes (message->node, "id", id, NULL); |
|
1346 |
} |
|
1347 |
||
1348 |
g_hash_table_insert (connection->id_handlers, |
|
1349 |
id, lm_message_handler_ref (handler)); |
|
1350 |
||
1351 |
return lm_connection_send (connection, message, error); |
|
1352 |
} |
|
1353 |
||
1354 |
/** |
|
1355 |
* lm_connection_send_with_reply_and_block: |
|
1356 |
* @connection: an #LmConnection |
|
1357 |
* @message: an #LmMessage |
|
1358 |
* @error: Set if error was detected during sending. |
|
1359 |
* |
|
1360 |
* Send @message and wait for return. |
|
1361 |
* |
|
1362 |
* Return value: The reply |
|
1363 |
**/ |
|
1364 |
LmMessage * |
|
1365 |
lm_connection_send_with_reply_and_block (LmConnection *connection, |
|
1366 |
LmMessage *message, |
|
1367 |
GError **error) |
|
1368 |
{ |
|
1369 |
gchar *id; |
|
1370 |
LmMessage *reply = NULL; |
|
1371 |
gint last_len = 0; |
|
1372 |
||
1373 |
if (lm_message_node_get_attribute (message->node, "id")) { |
|
1374 |
id = g_strdup (lm_message_node_get_attribute (message->node, |
|
1375 |
"id")); |
|
1376 |
} else { |
|
1377 |
id = _lm_utils_generate_id (); |
|
1378 |
lm_message_node_set_attributes (message->node, "id", id, NULL); |
|
1379 |
} |
|
1380 |
||
1381 |
g_source_remove (g_source_get_id (connection->incoming_source)); |
|
1382 |
g_source_unref (connection->incoming_source); |
|
1383 |
||
1384 |
lm_connection_send (connection, message, error); |
|
1385 |
||
1386 |
while (!reply) { |
|
1387 |
gint len; |
|
1388 |
const gchar *m_id; |
|
1389 |
||
1390 |
g_main_context_iteration (NULL, TRUE); |
|
1391 |
||
1392 |
len = connection->incoming_messages->length; |
|
1393 |
||
1394 |
if (len > last_len) { |
|
1395 |
LmMessage *m; |
|
1396 |
last_len = len; |
|
1397 |
m = (LmMessage *) g_queue_peek_tail (connection->incoming_messages); |
|
1398 |
m_id = lm_message_node_get_attribute (m->node, "id"); |
|
1399 |
||
1400 |
if (m_id && strcmp (m_id, id) == 0) { |
|
1401 |
reply = m; |
|
1402 |
} |
|
1403 |
||
1404 |
g_queue_pop_tail (connection->incoming_messages); |
|
1405 |
} |
|
1406 |
} |
|
1407 |
||
1408 |
g_free (id); |
|
1409 |
connection->incoming_source = connection_create_source (connection); |
|
1410 |
g_source_attach (connection->incoming_source, NULL); |
|
1411 |
||
1412 |
return reply; |
|
1413 |
} |
|
1414 |
||
1415 |
/** |
|
1416 |
* lm_connection_register_message_handler: |
|
1417 |
* @connection: Connection to register a handler for. |
|
1418 |
* @handler: Message handler to register. |
|
1419 |
* @type: Message type that @handler will handle. |
|
1420 |
* @priority: The priority in which to call @handler. |
|
1421 |
* |
|
1422 |
* Registers a #LmMessageHandler to handle incoming messages of a certain type. |
|
1423 |
* To unregister the handler call lm_connection_unregister_message_handler(). |
|
1424 |
**/ |
|
1425 |
void |
|
1426 |
lm_connection_register_message_handler (LmConnection *connection, |
|
1427 |
LmMessageHandler *handler, |
|
1428 |
LmMessageType type, |
|
1429 |
LmHandlerPriority priority) |
|
1430 |
{ |
|
1431 |
HandlerData *hd; |
|
1432 |
||
1433 |
g_return_if_fail (connection != NULL); |
|
1434 |
g_return_if_fail (handler != NULL); |
|
1435 |
g_return_if_fail (type != LM_MESSAGE_TYPE_UNKNOWN); |
|
1436 |
||
1437 |
hd = g_new0 (HandlerData, 1); |
|
1438 |
hd->priority = priority; |
|
1439 |
hd->handler = lm_message_handler_ref (handler); |
|
1440 |
||
1441 |
connection->handlers[type] = g_slist_insert_sorted (connection->handlers[type], |
|
1442 |
hd, |
|
1443 |
(GCompareFunc) connection_handler_compare_func); |
|
1444 |
} |
|
1445 |
||
1446 |
/** |
|
1447 |
* lm_connection_unregister_message_handler: |
|
1448 |
* @connection: Connection to unregister a handler for. |
|
1449 |
* @handler: The handler to unregister. |
|
1450 |
* @type: What type of messages to unregister this handler for. |
|
1451 |
* |
|
1452 |
* Unregisters a handler for @connection. @handler will no longer be called |
|
1453 |
* when incoming messages of @type arrive. |
|
1454 |
**/ |
|
1455 |
void |
|
1456 |
lm_connection_unregister_message_handler (LmConnection *connection, |
|
1457 |
LmMessageHandler *handler, |
|
1458 |
LmMessageType type) |
|
1459 |
{ |
|
1460 |
GSList *l, *prev = NULL; |
|
1461 |
||
1462 |
g_return_if_fail (connection != NULL); |
|
1463 |
g_return_if_fail (handler != NULL); |
|
1464 |
g_return_if_fail (type != LM_MESSAGE_TYPE_UNKNOWN); |
|
1465 |
||
1466 |
for (l = connection->handlers[type]; l; l = l->next) { |
|
1467 |
HandlerData *hd = (HandlerData *) l->data; |
|
1468 |
||
1469 |
if (hd->handler == handler) { |
|
1470 |
if (prev) { |
|
1471 |
prev->next = l->next; |
|
1472 |
} else { |
|
1473 |
connection->handlers[type] = l->next; |
|
1474 |
} |
|
1475 |
l->next = NULL; |
|
1476 |
g_slist_free (l); |
|
1477 |
lm_message_handler_unref (hd->handler); |
|
1478 |
g_free (hd); |
|
1479 |
break; |
|
1480 |
} |
|
1481 |
prev = l; |
|
1482 |
} |
|
1483 |
} |
|
1484 |
||
1485 |
/** |
|
1486 |
* lm_connection_set_disconnect_function: |
|
1487 |
* @connection: Connection to register disconnect callback for. |
|
1488 |
* @function: Function to be called when @connection is closed. |
|
1489 |
* @user_data: User data passed to @function. |
|
1490 |
* @notify: Function that will be called with @user_data when @user_data needs to be freed. Pass #NULL if it shouldn't be freed. |
|
1491 |
* |
|
1492 |
* Set the callback that will be called when a connection is closed. |
|
1493 |
**/ |
|
1494 |
void |
|
1495 |
lm_connection_set_disconnect_function (LmConnection *connection, |
|
1496 |
LmDisconnectFunction function, |
|
1497 |
gpointer user_data, |
|
1498 |
GDestroyNotify notify) |
|
1499 |
{ |
|
1500 |
if (connection->disconnect_cb) { |
|
1501 |
_lm_utils_free_callback (connection->disconnect_cb); |
|
1502 |
} |
|
1503 |
||
1504 |
connection->disconnect_cb = _lm_utils_new_callback (function, |
|
1505 |
user_data, |
|
1506 |
notify); |
|
1507 |
} |
|
1508 |
||
1509 |
/** |
|
1510 |
* lm_connection_send_raw: |
|
1511 |
* @connection: Connection used to send |
|
1512 |
* @str: The string to send, the entire string will be sent. |
|
1513 |
* @error: Set if error was detected during sending. |
|
1514 |
* |
|
1515 |
* Asynchronous call to send a raw string. Useful for debugging and testing. |
|
1516 |
* |
|
1517 |
* Return value: Returns #TRUE if no errors was detected during sending, |
|
1518 |
* #FALSE otherwise. |
|
1519 |
**/ |
|
1520 |
gboolean |
|
1521 |
lm_connection_send_raw (LmConnection *connection, |
|
1522 |
const gchar *str, |
|
1523 |
GError **error) |
|
1524 |
{ |
|
1525 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
1526 |
||
1527 |
return connection_send (connection, str, -1, error); |
|
1528 |
} |
|
1529 |
||
1530 |
/** |
|
1531 |
* lm_connection_ref: |
|
1532 |
* @connection: Connection to add a reference to. |
|
1533 |
* |
|
1534 |
* Add a reference on @connection. To remove a reference call |
|
1535 |
* lm_connection_unref(). |
|
1536 |
* |
|
1537 |
* Return value: Returns the same connection. |
|
1538 |
**/ |
|
1539 |
LmConnection* |
|
1540 |
lm_connection_ref (LmConnection *connection) |
|
1541 |
{ |
|
1542 |
g_return_val_if_fail (connection != NULL, NULL); |
|
1543 |
||
1544 |
connection->ref_count++; |
|
1545 |
||
1546 |
return connection; |
|
1547 |
} |
|
1548 |
||
1549 |
/** |
|
1550 |
* lm_connection_unref: |
|
1551 |
* @connection: Connection to remove reference from. |
|
1552 |
* |
|
1553 |
* Removes a reference on @connection. If there are no references to |
|
1554 |
* @connection it will be fried and shouldn't be used again. |
|
1555 |
**/ |
|
1556 |
void |
|
1557 |
lm_connection_unref (LmConnection *connection) |
|
1558 |
{ |
|
1559 |
g_return_if_fail (connection != NULL); |
|
1560 |
||
1561 |
connection->ref_count--; |
|
1562 |
||
1563 |
if (connection->ref_count == 0) { |
|
1564 |
connection_free (connection); |
|
1565 |
} |
|
1566 |
} |
|
1567 |
||
1568 |
#if 0 |
|
1569 |
void |
|
1570 |
lm_connection_register (LmConnection *connection, |
|
1571 |
const gchar *username, |
|
1572 |
const gchar *password, |
|
1573 |
const gchar *resource, |
|
1574 |
LmRegisterCallback callback, |
|
1575 |
gpointer user_data) |
|
1576 |
{ |
|
1577 |
LmElement *element; |
|
1578 |
LmNode *q_node; |
|
1579 |
gchar *id; |
|
1580 |
static gint register_id = 0; |
|
1581 |
|
|
1582 |
g_return_if_fail (connection != NULL); |
|
1583 |
g_return_if_fail (lm_connection_is_open (connection)); |
|
1584 |
|
|
1585 |
/* Use lm:iq:register name space */ |
|
1586 |
||
1587 |
element = lm_iq_new (LM_IQ_TYPE_SET); |
|
1588 |
|
|
1589 |
q_node = lm_node_new ("query"); |
|
1590 |
lm_node_set_attribute (q_node, "xmlns", JABBER_IQ_REGISTER); |
|
1591 |
lm_node_add_child (q_node, "username", username); |
|
1592 |
lm_node_add_child (q_node, "password", password); |
|
1593 |
lm_node_add_child (q_node, "resource", resource); |
|
1594 |
||
1595 |
lm_element_add_child_node (element, q_node); |
|
1596 |
|
|
1597 |
id = g_strdup_printf ("register_%d", ++register_id); |
|
1598 |
lm_element_set_id (element, id); |
|
1599 |
|
|
1600 |
lm_connection_send (connection, element, NULL); |
|
1601 |
connection_add_callback (connection, id, callback, user_data); |
|
1602 |
lm_element_unref (element); |
|
1603 |
||
1604 |
g_free (id); |
|
1605 |
||
1606 |
|
|
1607 |
} |
|
1608 |
||
1609 |
#endif |