14192
|
1 /*
|
|
2 * gaim - Jabber Protocol Plugin
|
|
3 *
|
|
4 * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com>
|
|
5 *
|
|
6 * This program is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or
|
|
9 * (at your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 *
|
|
15 * GNU General Public License for more details.
|
|
16 *
|
|
17 * You should have received a copy of the GNU General Public License
|
|
18 * along with this program; if not, write to the Free Software
|
|
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
20 *
|
|
21 */
|
|
22
|
|
23 #include "blist.h"
|
|
24
|
|
25 #include "internal.h"
|
|
26 #include "cipher.h"
|
|
27 #include "debug.h"
|
|
28 #include "ft.h"
|
|
29 #include "network.h"
|
|
30 #include "notify.h"
|
|
31
|
|
32 #include "buddy.h"
|
|
33 #include "disco.h"
|
|
34 #include "jabber.h"
|
|
35 #include "iq.h"
|
|
36 #include "si.h"
|
|
37
|
|
38 #include "si.h"
|
|
39
|
|
40 struct bytestreams_streamhost {
|
|
41 char *jid;
|
|
42 char *host;
|
|
43 int port;
|
|
44 };
|
|
45
|
|
46 typedef struct _JabberSIXfer {
|
|
47 JabberStream *js;
|
|
48
|
14262
|
49 GaimProxyConnectData *connect_data;
|
14267
|
50 GaimNetworkListenData *listen_data;
|
14192
|
51
|
|
52 gboolean accepted;
|
|
53
|
|
54 char *stream_id;
|
|
55 char *iq_id;
|
|
56
|
|
57 enum {
|
|
58 STREAM_METHOD_UNKNOWN = 0,
|
|
59 STREAM_METHOD_BYTESTREAMS = 2 << 1,
|
|
60 STREAM_METHOD_IBB = 2 << 2,
|
|
61 STREAM_METHOD_UNSUPPORTED = 2 << 31
|
|
62 } stream_method;
|
|
63
|
|
64 GList *streamhosts;
|
|
65 GaimProxyInfo *gpi;
|
|
66
|
|
67 char *rxqueue;
|
|
68 size_t rxlen;
|
|
69 gsize rxmaxlen;
|
|
70 } JabberSIXfer;
|
|
71
|
|
72 static GaimXfer*
|
|
73 jabber_si_xfer_find(JabberStream *js, const char *sid, const char *from)
|
|
74 {
|
|
75 GList *xfers;
|
|
76
|
|
77 if(!sid || !from)
|
|
78 return NULL;
|
|
79
|
|
80 for(xfers = js->file_transfers; xfers; xfers = xfers->next) {
|
|
81 GaimXfer *xfer = xfers->data;
|
|
82 JabberSIXfer *jsx = xfer->data;
|
|
83 if(jsx->stream_id && xfer->who &&
|
|
84 !strcmp(jsx->stream_id, sid) && !strcmp(xfer->who, from))
|
|
85 return xfer;
|
|
86 }
|
|
87
|
|
88 return NULL;
|
|
89 }
|
|
90
|
|
91
|
|
92 static void jabber_si_bytestreams_attempt_connect(GaimXfer *xfer);
|
|
93
|
|
94 static void
|
|
95 jabber_si_bytestreams_connect_cb(gpointer data, gint source, const gchar *error_message)
|
|
96 {
|
|
97 GaimXfer *xfer = data;
|
|
98 JabberSIXfer *jsx = xfer->data;
|
|
99 JabberIq *iq;
|
|
100 xmlnode *query, *su;
|
|
101 struct bytestreams_streamhost *streamhost = jsx->streamhosts->data;
|
|
102
|
|
103 gaim_proxy_info_destroy(jsx->gpi);
|
14262
|
104 jsx->connect_data = NULL;
|
14192
|
105
|
|
106 if(source < 0) {
|
|
107 jsx->streamhosts = g_list_remove(jsx->streamhosts, streamhost);
|
|
108 g_free(streamhost->jid);
|
|
109 g_free(streamhost->host);
|
|
110 g_free(streamhost);
|
|
111 jabber_si_bytestreams_attempt_connect(xfer);
|
|
112 return;
|
|
113 }
|
|
114
|
|
115 iq = jabber_iq_new_query(jsx->js, JABBER_IQ_RESULT, "http://jabber.org/protocol/bytestreams");
|
|
116 xmlnode_set_attrib(iq->node, "to", xfer->who);
|
|
117 jabber_iq_set_id(iq, jsx->iq_id);
|
|
118 query = xmlnode_get_child(iq->node, "query");
|
|
119 su = xmlnode_new_child(query, "streamhost-used");
|
|
120 xmlnode_set_attrib(su, "jid", streamhost->jid);
|
|
121
|
|
122 jabber_iq_send(iq);
|
|
123
|
|
124 gaim_xfer_start(xfer, source, NULL, -1);
|
|
125 }
|
|
126
|
|
127 static void jabber_si_bytestreams_attempt_connect(GaimXfer *xfer)
|
|
128 {
|
|
129 JabberSIXfer *jsx = xfer->data;
|
|
130 struct bytestreams_streamhost *streamhost;
|
|
131 char *dstaddr, *p;
|
|
132 int i;
|
|
133 unsigned char hashval[20];
|
|
134
|
|
135 if(!jsx->streamhosts) {
|
|
136 JabberIq *iq = jabber_iq_new(jsx->js, JABBER_IQ_ERROR);
|
|
137 xmlnode *error, *condition;
|
|
138
|
|
139 if(jsx->iq_id)
|
|
140 jabber_iq_set_id(iq, jsx->iq_id);
|
|
141
|
|
142 xmlnode_set_attrib(iq->node, "to", xfer->who);
|
|
143 error = xmlnode_new_child(iq->node, "error");
|
|
144 xmlnode_set_attrib(error, "code", "404");
|
|
145 xmlnode_set_attrib(error, "type", "cancel");
|
|
146 condition = xmlnode_new_child(error, "condition");
|
|
147 xmlnode_set_namespace(condition, "urn:ietf:params:xml:ns:xmpp-stanzas");
|
|
148 xmlnode_new_child(condition, "item-not-found");
|
|
149
|
|
150 jabber_iq_send(iq);
|
|
151
|
|
152 gaim_xfer_cancel_local(xfer);
|
|
153
|
|
154 return;
|
|
155 }
|
|
156
|
|
157 streamhost = jsx->streamhosts->data;
|
|
158
|
|
159 jsx->gpi = gaim_proxy_info_new();
|
|
160 gaim_proxy_info_set_type(jsx->gpi, GAIM_PROXY_SOCKS5);
|
|
161 gaim_proxy_info_set_host(jsx->gpi, streamhost->host);
|
|
162 gaim_proxy_info_set_port(jsx->gpi, streamhost->port);
|
|
163
|
|
164 dstaddr = g_strdup_printf("%s%s%s@%s/%s", jsx->stream_id, xfer->who, jsx->js->user->node,
|
|
165 jsx->js->user->domain, jsx->js->user->resource);
|
|
166
|
|
167 gaim_cipher_digest_region("sha1", (guchar *)dstaddr, strlen(dstaddr),
|
|
168 sizeof(hashval), hashval, NULL);
|
|
169 g_free(dstaddr);
|
|
170 dstaddr = g_malloc(41);
|
|
171 p = dstaddr;
|
|
172 for(i=0; i<20; i++, p+=2)
|
|
173 snprintf(p, 3, "%02x", hashval[i]);
|
|
174
|
14262
|
175 jsx->connect_data = gaim_proxy_connect_socks5(jsx->gpi, dstaddr, 0,
|
14192
|
176 jabber_si_bytestreams_connect_cb, xfer);
|
|
177 g_free(dstaddr);
|
|
178
|
14262
|
179 if (jsx->connect_data == NULL)
|
14192
|
180 {
|
|
181 jsx->streamhosts = g_list_remove(jsx->streamhosts, streamhost);
|
|
182 g_free(streamhost->jid);
|
|
183 g_free(streamhost->host);
|
|
184 g_free(streamhost);
|
|
185 jabber_si_bytestreams_attempt_connect(xfer);
|
|
186 }
|
|
187 }
|
|
188
|
|
189 void jabber_bytestreams_parse(JabberStream *js, xmlnode *packet)
|
|
190 {
|
|
191 GaimXfer *xfer;
|
|
192 JabberSIXfer *jsx;
|
|
193 xmlnode *query, *streamhost;
|
14294
|
194 const char *sid, *from, *type;
|
|
195
|
|
196 if(!(type = xmlnode_get_attrib(packet, "type")) || strcmp(type, "set"))
|
|
197 return;
|
14192
|
198
|
|
199 if(!(from = xmlnode_get_attrib(packet, "from")))
|
|
200 return;
|
|
201
|
|
202 if(!(query = xmlnode_get_child(packet, "query")))
|
|
203 return;
|
|
204
|
|
205 if(!(sid = xmlnode_get_attrib(query, "sid")))
|
|
206 return;
|
|
207
|
|
208 if(!(xfer = jabber_si_xfer_find(js, sid, from)))
|
|
209 return;
|
|
210
|
|
211 jsx = xfer->data;
|
|
212
|
|
213 if(!jsx->accepted)
|
|
214 return;
|
|
215
|
|
216 if(jsx->iq_id)
|
|
217 g_free(jsx->iq_id);
|
|
218 jsx->iq_id = g_strdup(xmlnode_get_attrib(packet, "id"));
|
|
219
|
|
220 for(streamhost = xmlnode_get_child(query, "streamhost"); streamhost;
|
|
221 streamhost = xmlnode_get_next_twin(streamhost)) {
|
|
222 const char *jid, *host, *port;
|
|
223 int portnum;
|
|
224
|
|
225 if((jid = xmlnode_get_attrib(streamhost, "jid")) &&
|
|
226 (host = xmlnode_get_attrib(streamhost, "host")) &&
|
|
227 (port = xmlnode_get_attrib(streamhost, "port")) &&
|
|
228 (portnum = atoi(port))) {
|
|
229 struct bytestreams_streamhost *sh = g_new0(struct bytestreams_streamhost, 1);
|
|
230 sh->jid = g_strdup(jid);
|
|
231 sh->host = g_strdup(host);
|
|
232 sh->port = portnum;
|
|
233 jsx->streamhosts = g_list_append(jsx->streamhosts, sh);
|
|
234 }
|
|
235 }
|
|
236
|
|
237 jabber_si_bytestreams_attempt_connect(xfer);
|
|
238 }
|
|
239
|
|
240
|
|
241 static void
|
|
242 jabber_si_xfer_bytestreams_send_read_again_resp_cb(gpointer data, gint source,
|
|
243 GaimInputCondition cond)
|
|
244 {
|
|
245 GaimXfer *xfer = data;
|
|
246 JabberSIXfer *jsx = xfer->data;
|
|
247 int len;
|
|
248
|
|
249 len = write(source, jsx->rxqueue + jsx->rxlen, jsx->rxmaxlen - jsx->rxlen);
|
|
250 if (len < 0 && errno == EAGAIN)
|
|
251 return;
|
|
252 else if (len < 0) {
|
|
253 gaim_input_remove(xfer->watcher);
|
|
254 xfer->watcher = 0;
|
|
255 g_free(jsx->rxqueue);
|
|
256 jsx->rxqueue = NULL;
|
|
257 close(source);
|
|
258 gaim_xfer_cancel_remote(xfer);
|
|
259 return;
|
|
260 }
|
|
261 jsx->rxlen += len;
|
|
262
|
|
263 if (jsx->rxlen < jsx->rxmaxlen)
|
|
264 return;
|
|
265
|
|
266 gaim_input_remove(xfer->watcher);
|
|
267 xfer->watcher = 0;
|
|
268 g_free(jsx->rxqueue);
|
|
269 jsx->rxqueue = NULL;
|
|
270
|
|
271 gaim_xfer_start(xfer, source, NULL, -1);
|
|
272 }
|
|
273
|
|
274 static void
|
|
275 jabber_si_xfer_bytestreams_send_read_again_cb(gpointer data, gint source,
|
|
276 GaimInputCondition cond)
|
|
277 {
|
|
278 GaimXfer *xfer = data;
|
|
279 JabberSIXfer *jsx = xfer->data;
|
|
280 int i;
|
|
281 char buffer[256];
|
|
282 int len;
|
|
283 char *dstaddr, *p;
|
|
284 unsigned char hashval[20];
|
|
285 const char *host;
|
|
286
|
|
287 gaim_debug_info("jabber", "in jabber_si_xfer_bytestreams_send_read_again_cb\n");
|
|
288
|
|
289 if(jsx->rxlen < 5) {
|
|
290 gaim_debug_info("jabber", "reading the first 5 bytes\n");
|
|
291 len = read(source, buffer, 5 - jsx->rxlen);
|
|
292 if(len < 0 && errno == EAGAIN)
|
|
293 return;
|
|
294 else if(len <= 0) {
|
|
295 gaim_input_remove(xfer->watcher);
|
|
296 xfer->watcher = 0;
|
|
297 close(source);
|
|
298 gaim_xfer_cancel_remote(xfer);
|
|
299 return;
|
|
300 }
|
|
301 jsx->rxqueue = g_realloc(jsx->rxqueue, len + jsx->rxlen);
|
|
302 memcpy(jsx->rxqueue + jsx->rxlen, buffer, len);
|
|
303 jsx->rxlen += len;
|
|
304 return;
|
|
305 } else if(jsx->rxqueue[0] != 0x05 || jsx->rxqueue[1] != 0x01 ||
|
|
306 jsx->rxqueue[3] != 0x03) {
|
|
307 gaim_debug_info("jabber", "invalid socks5 stuff\n");
|
|
308 gaim_input_remove(xfer->watcher);
|
|
309 xfer->watcher = 0;
|
|
310 close(source);
|
|
311 gaim_xfer_cancel_remote(xfer);
|
|
312 return;
|
|
313 } else if(jsx->rxlen - 5 < jsx->rxqueue[4] + 2) {
|
|
314 gaim_debug_info("jabber", "reading umpteen more bytes\n");
|
|
315 len = read(source, buffer, jsx->rxqueue[4] + 5 + 2 - jsx->rxlen);
|
|
316 if(len < 0 && errno == EAGAIN)
|
|
317 return;
|
|
318 else if(len <= 0) {
|
|
319 gaim_input_remove(xfer->watcher);
|
|
320 xfer->watcher = 0;
|
|
321 close(source);
|
|
322 gaim_xfer_cancel_remote(xfer);
|
|
323 return;
|
|
324 }
|
|
325 jsx->rxqueue = g_realloc(jsx->rxqueue, len + jsx->rxlen);
|
|
326 memcpy(jsx->rxqueue + jsx->rxlen, buffer, len);
|
|
327 jsx->rxlen += len;
|
|
328 }
|
|
329
|
|
330 if(jsx->rxlen - 5 < jsx->rxqueue[4] + 2)
|
|
331 return;
|
|
332
|
|
333 gaim_input_remove(xfer->watcher);
|
|
334 xfer->watcher = 0;
|
|
335
|
|
336 dstaddr = g_strdup_printf("%s%s@%s/%s%s", jsx->stream_id,
|
|
337 jsx->js->user->node, jsx->js->user->domain,
|
|
338 jsx->js->user->resource, xfer->who);
|
|
339
|
|
340 gaim_cipher_digest_region("sha1", (guchar *)dstaddr, strlen(dstaddr),
|
|
341 sizeof(hashval), hashval, NULL);
|
|
342 g_free(dstaddr);
|
|
343 dstaddr = g_malloc(41);
|
|
344 p = dstaddr;
|
|
345 for(i=0; i<20; i++, p+=2)
|
|
346 snprintf(p, 3, "%02x", hashval[i]);
|
|
347
|
|
348 if(jsx->rxqueue[4] != 40 || strncmp(dstaddr, jsx->rxqueue+5, 40) ||
|
|
349 jsx->rxqueue[45] != 0x00 || jsx->rxqueue[46] != 0x00) {
|
|
350 gaim_debug_error("jabber", "someone connected with the wrong info!\n");
|
|
351 close(source);
|
|
352 gaim_xfer_cancel_remote(xfer);
|
|
353 return;
|
|
354 }
|
|
355
|
|
356 g_free(jsx->rxqueue);
|
|
357 host = gaim_network_get_my_ip(jsx->js->fd);
|
|
358
|
|
359 jsx->rxmaxlen = 5 + strlen(host) + 2;
|
|
360 jsx->rxqueue = g_malloc(jsx->rxmaxlen);
|
|
361 jsx->rxlen = 0;
|
|
362
|
|
363 jsx->rxqueue[0] = 0x05;
|
|
364 jsx->rxqueue[1] = 0x00;
|
|
365 jsx->rxqueue[2] = 0x00;
|
|
366 jsx->rxqueue[3] = 0x03;
|
|
367 jsx->rxqueue[4] = strlen(host);
|
|
368 memcpy(jsx->rxqueue + 5, host, strlen(host));
|
|
369 jsx->rxqueue[5+strlen(host)] = 0x00;
|
|
370 jsx->rxqueue[6+strlen(host)] = 0x00;
|
|
371
|
|
372 xfer->watcher = gaim_input_add(source, GAIM_INPUT_WRITE,
|
|
373 jabber_si_xfer_bytestreams_send_read_again_resp_cb, xfer);
|
|
374 jabber_si_xfer_bytestreams_send_read_again_resp_cb(xfer, source,
|
|
375 GAIM_INPUT_WRITE);
|
|
376 }
|
|
377
|
|
378 static void
|
|
379 jabber_si_xfer_bytestreams_send_read_response_cb(gpointer data, gint source,
|
|
380 GaimInputCondition cond)
|
|
381 {
|
|
382 GaimXfer *xfer = data;
|
|
383 JabberSIXfer *jsx = xfer->data;
|
|
384 int len;
|
|
385
|
|
386 len = write(source, jsx->rxqueue + jsx->rxlen, jsx->rxmaxlen - jsx->rxlen);
|
|
387 if (len < 0 && errno == EAGAIN)
|
|
388 return;
|
|
389 else if (len < 0) {
|
|
390 gaim_input_remove(xfer->watcher);
|
|
391 xfer->watcher = 0;
|
|
392 g_free(jsx->rxqueue);
|
|
393 jsx->rxqueue = NULL;
|
|
394 close(source);
|
|
395 gaim_xfer_cancel_remote(xfer);
|
|
396 return;
|
|
397 }
|
|
398 jsx->rxlen += len;
|
|
399
|
|
400 if (jsx->rxlen < jsx->rxmaxlen)
|
|
401 return;
|
|
402
|
|
403 gaim_input_remove(xfer->watcher);
|
|
404 xfer->watcher = 0;
|
|
405
|
|
406 if (jsx->rxqueue[1] == 0x00) {
|
|
407 xfer->watcher = gaim_input_add(source, GAIM_INPUT_READ,
|
|
408 jabber_si_xfer_bytestreams_send_read_again_cb, xfer);
|
|
409 g_free(jsx->rxqueue);
|
|
410 jsx->rxqueue = NULL;
|
|
411 } else {
|
|
412 close(source);
|
|
413 gaim_xfer_cancel_remote(xfer);
|
|
414 }
|
|
415 }
|
|
416
|
|
417 static void
|
|
418 jabber_si_xfer_bytestreams_send_read_cb(gpointer data, gint source,
|
|
419 GaimInputCondition cond)
|
|
420 {
|
|
421 GaimXfer *xfer = data;
|
|
422 JabberSIXfer *jsx = xfer->data;
|
|
423 int i;
|
|
424 int len;
|
|
425 char buffer[256];
|
|
426
|
|
427 gaim_debug_info("jabber", "in jabber_si_xfer_bytestreams_send_read_cb\n");
|
|
428
|
|
429 xfer->fd = source;
|
|
430
|
|
431 if(jsx->rxlen < 2) {
|
|
432 gaim_debug_info("jabber", "reading those first two bytes\n");
|
|
433 len = read(source, buffer, 2 - jsx->rxlen);
|
|
434 if(len < 0 && errno == EAGAIN)
|
|
435 return;
|
|
436 else if(len <= 0) {
|
|
437 gaim_input_remove(xfer->watcher);
|
|
438 xfer->watcher = 0;
|
|
439 close(source);
|
|
440 gaim_xfer_cancel_remote(xfer);
|
|
441 return;
|
|
442 }
|
|
443 jsx->rxqueue = g_realloc(jsx->rxqueue, len + jsx->rxlen);
|
|
444 memcpy(jsx->rxqueue + jsx->rxlen, buffer, len);
|
|
445 jsx->rxlen += len;
|
|
446 return;
|
|
447 } else if(jsx->rxlen - 2 < jsx->rxqueue[1]) {
|
|
448 gaim_debug_info("jabber", "reading the next umpteen bytes\n");
|
|
449 len = read(source, buffer, jsx->rxqueue[1] + 2 - jsx->rxlen);
|
|
450 if(len < 0 && errno == EAGAIN)
|
|
451 return;
|
|
452 else if(len <= 0) {
|
|
453 gaim_input_remove(xfer->watcher);
|
|
454 xfer->watcher = 0;
|
|
455 close(source);
|
|
456 gaim_xfer_cancel_remote(xfer);
|
|
457 return;
|
|
458 }
|
|
459 jsx->rxqueue = g_realloc(jsx->rxqueue, len + jsx->rxlen);
|
|
460 memcpy(jsx->rxqueue + jsx->rxlen, buffer, len);
|
|
461 jsx->rxlen += len;
|
|
462 }
|
|
463
|
|
464 if(jsx->rxlen -2 < jsx->rxqueue[1])
|
|
465 return;
|
|
466
|
|
467 gaim_input_remove(xfer->watcher);
|
|
468 xfer->watcher = 0;
|
|
469
|
|
470 gaim_debug_info("jabber", "checking to make sure we're socks FIVE\n");
|
|
471
|
|
472 if(jsx->rxqueue[0] != 0x05) {
|
|
473 close(source);
|
|
474 gaim_xfer_cancel_remote(xfer);
|
|
475 return;
|
|
476 }
|
|
477
|
|
478 gaim_debug_info("jabber", "going to test %hhu different methods\n", jsx->rxqueue[1]);
|
|
479
|
|
480 for(i=0; i<jsx->rxqueue[1]; i++) {
|
|
481
|
|
482 gaim_debug_info("jabber", "testing %hhu\n", jsx->rxqueue[i+2]);
|
|
483 if(jsx->rxqueue[i+2] == 0x00) {
|
|
484 g_free(jsx->rxqueue);
|
|
485 jsx->rxlen = 0;
|
|
486 jsx->rxmaxlen = 2;
|
|
487 jsx->rxqueue = g_malloc(jsx->rxmaxlen);
|
|
488 jsx->rxqueue[0] = 0x05;
|
|
489 jsx->rxqueue[1] = 0x00;
|
|
490 xfer->watcher = gaim_input_add(source, GAIM_INPUT_WRITE,
|
|
491 jabber_si_xfer_bytestreams_send_read_response_cb,
|
|
492 xfer);
|
|
493 jabber_si_xfer_bytestreams_send_read_response_cb(xfer,
|
|
494 source, GAIM_INPUT_WRITE);
|
|
495 jsx->rxqueue = NULL;
|
|
496 jsx->rxlen = 0;
|
|
497 return;
|
|
498 }
|
|
499 }
|
|
500
|
|
501 g_free(jsx->rxqueue);
|
|
502 jsx->rxlen = 0;
|
|
503 jsx->rxmaxlen = 2;
|
|
504 jsx->rxqueue = g_malloc(jsx->rxmaxlen);
|
|
505 jsx->rxqueue[0] = 0x05;
|
|
506 jsx->rxqueue[1] = 0xFF;
|
|
507 xfer->watcher = gaim_input_add(source, GAIM_INPUT_WRITE,
|
|
508 jabber_si_xfer_bytestreams_send_read_response_cb, xfer);
|
|
509 jabber_si_xfer_bytestreams_send_read_response_cb(xfer,
|
|
510 source, GAIM_INPUT_WRITE);
|
|
511 }
|
|
512
|
|
513 static void
|
|
514 jabber_si_xfer_bytestreams_send_connected_cb(gpointer data, gint source,
|
|
515 GaimInputCondition cond)
|
|
516 {
|
|
517 GaimXfer *xfer = data;
|
|
518 int acceptfd;
|
|
519
|
|
520 gaim_debug_info("jabber", "in jabber_si_xfer_bytestreams_send_connected_cb\n");
|
|
521
|
|
522 acceptfd = accept(source, NULL, 0);
|
|
523 if(acceptfd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
|
|
524 return;
|
|
525 else if(acceptfd == -1) {
|
|
526 gaim_debug_warning("jabber", "accept: %s\n", strerror(errno));
|
|
527 return;
|
|
528 }
|
|
529
|
|
530 gaim_input_remove(xfer->watcher);
|
|
531 close(source);
|
|
532
|
|
533 xfer->watcher = gaim_input_add(acceptfd, GAIM_INPUT_READ,
|
|
534 jabber_si_xfer_bytestreams_send_read_cb, xfer);
|
|
535 }
|
|
536
|
|
537 static void
|
|
538 jabber_si_xfer_bytestreams_listen_cb(int sock, gpointer data)
|
|
539 {
|
|
540 GaimXfer *xfer = data;
|
|
541 JabberSIXfer *jsx;
|
|
542 JabberIq *iq;
|
|
543 xmlnode *query, *streamhost;
|
|
544 char *jid, *port;
|
|
545
|
14267
|
546 jsx = xfer->data;
|
|
547 jsx->listen_data = NULL;
|
|
548
|
14192
|
549 if (gaim_xfer_get_status(xfer) == GAIM_XFER_STATUS_CANCEL_LOCAL) {
|
|
550 gaim_xfer_unref(xfer);
|
|
551 return;
|
|
552 }
|
|
553
|
|
554 gaim_xfer_unref(xfer);
|
|
555
|
|
556 if (sock < 0) {
|
|
557 gaim_xfer_cancel_local(xfer);
|
|
558 return;
|
|
559 }
|
|
560
|
|
561 iq = jabber_iq_new_query(jsx->js, JABBER_IQ_SET,
|
|
562 "http://jabber.org/protocol/bytestreams");
|
|
563 xmlnode_set_attrib(iq->node, "to", xfer->who);
|
|
564 query = xmlnode_get_child(iq->node, "query");
|
|
565
|
|
566 xmlnode_set_attrib(query, "sid", jsx->stream_id);
|
|
567
|
|
568 streamhost = xmlnode_new_child(query, "streamhost");
|
|
569 jid = g_strdup_printf("%s@%s/%s", jsx->js->user->node,
|
|
570 jsx->js->user->domain, jsx->js->user->resource);
|
|
571 xmlnode_set_attrib(streamhost, "jid", jid);
|
|
572 g_free(jid);
|
|
573
|
|
574 /* XXX: shouldn't we use the public IP or something? here */
|
|
575 xmlnode_set_attrib(streamhost, "host",
|
|
576 gaim_network_get_my_ip(jsx->js->fd));
|
|
577 xfer->local_port = gaim_network_get_port_from_fd(sock);
|
|
578 port = g_strdup_printf("%hu", xfer->local_port);
|
|
579 xmlnode_set_attrib(streamhost, "port", port);
|
|
580 g_free(port);
|
|
581
|
|
582 xfer->watcher = gaim_input_add(sock, GAIM_INPUT_READ,
|
|
583 jabber_si_xfer_bytestreams_send_connected_cb, xfer);
|
|
584
|
|
585 /* XXX: insert proxies here */
|
|
586
|
|
587 /* XXX: callback to find out which streamhost they used, or see if they
|
|
588 * screwed it up */
|
|
589 jabber_iq_send(iq);
|
|
590
|
|
591 }
|
|
592
|
|
593 static void
|
|
594 jabber_si_xfer_bytestreams_send_init(GaimXfer *xfer)
|
|
595 {
|
14267
|
596 JabberSIXfer *jsx;
|
|
597
|
14192
|
598 gaim_xfer_ref(xfer);
|
|
599
|
14267
|
600 jsx = xfer->data;
|
|
601 jsx->listen_data = gaim_network_listen_range(0, 0, SOCK_STREAM,
|
|
602 jabber_si_xfer_bytestreams_listen_cb, xfer);
|
|
603 if (jsx->listen_data == NULL) {
|
14192
|
604 gaim_xfer_unref(xfer);
|
|
605 /* XXX: couldn't open a port, we're fscked */
|
|
606 gaim_xfer_cancel_local(xfer);
|
|
607 return;
|
|
608 }
|
|
609
|
|
610 }
|
|
611
|
|
612 static void jabber_si_xfer_send_method_cb(JabberStream *js, xmlnode *packet,
|
|
613 gpointer data)
|
|
614 {
|
|
615 GaimXfer *xfer = data;
|
|
616 xmlnode *si, *feature, *x, *field, *value;
|
|
617
|
|
618 if(!(si = xmlnode_get_child_with_namespace(packet, "si", "http://jabber.org/protocol/si"))) {
|
|
619 gaim_xfer_cancel_remote(xfer);
|
|
620 return;
|
|
621 }
|
|
622
|
|
623 if(!(feature = xmlnode_get_child_with_namespace(si, "feature", "http://jabber.org/protocol/feature-neg"))) {
|
|
624 gaim_xfer_cancel_remote(xfer);
|
|
625 return;
|
|
626 }
|
|
627
|
|
628 if(!(x = xmlnode_get_child_with_namespace(feature, "x", "jabber:x:data"))) {
|
|
629 gaim_xfer_cancel_remote(xfer);
|
|
630 return;
|
|
631 }
|
|
632
|
|
633 for(field = xmlnode_get_child(x, "field"); field; field = xmlnode_get_next_twin(field)) {
|
|
634 const char *var = xmlnode_get_attrib(field, "var");
|
|
635
|
|
636 if(var && !strcmp(var, "stream-method")) {
|
|
637 if((value = xmlnode_get_child(field, "value"))) {
|
|
638 char *val = xmlnode_get_data(value);
|
|
639 if(val && !strcmp(val, "http://jabber.org/protocol/bytestreams")) {
|
|
640 jabber_si_xfer_bytestreams_send_init(xfer);
|
|
641 g_free(val);
|
|
642 return;
|
|
643 }
|
|
644 g_free(val);
|
|
645 }
|
|
646 }
|
|
647 }
|
|
648 gaim_xfer_cancel_remote(xfer);
|
|
649 }
|
|
650
|
|
651 static void jabber_si_xfer_send_request(GaimXfer *xfer)
|
|
652 {
|
|
653 JabberSIXfer *jsx = xfer->data;
|
|
654 JabberIq *iq;
|
|
655 xmlnode *si, *file, *feature, *x, *field, *option, *value;
|
|
656 char buf[32];
|
|
657
|
|
658 xfer->filename = g_path_get_basename(xfer->local_filename);
|
|
659
|
|
660 iq = jabber_iq_new(jsx->js, JABBER_IQ_SET);
|
|
661 xmlnode_set_attrib(iq->node, "to", xfer->who);
|
|
662 si = xmlnode_new_child(iq->node, "si");
|
|
663 xmlnode_set_namespace(si, "http://jabber.org/protocol/si");
|
|
664 jsx->stream_id = jabber_get_next_id(jsx->js);
|
|
665 xmlnode_set_attrib(si, "id", jsx->stream_id);
|
|
666 xmlnode_set_attrib(si, "profile",
|
|
667 "http://jabber.org/protocol/si/profile/file-transfer");
|
|
668
|
|
669 file = xmlnode_new_child(si, "file");
|
|
670 xmlnode_set_namespace(file,
|
|
671 "http://jabber.org/protocol/si/profile/file-transfer");
|
|
672 xmlnode_set_attrib(file, "name", xfer->filename);
|
|
673 g_snprintf(buf, sizeof(buf), "%" G_GSIZE_FORMAT, xfer->size);
|
|
674 xmlnode_set_attrib(file, "size", buf);
|
|
675 /* maybe later we'll do hash and date attribs */
|
|
676
|
|
677 feature = xmlnode_new_child(si, "feature");
|
|
678 xmlnode_set_namespace(feature,
|
|
679 "http://jabber.org/protocol/feature-neg");
|
|
680 x = xmlnode_new_child(feature, "x");
|
|
681 xmlnode_set_namespace(x, "jabber:x:data");
|
|
682 xmlnode_set_attrib(x, "type", "form");
|
|
683 field = xmlnode_new_child(x, "field");
|
|
684 xmlnode_set_attrib(field, "var", "stream-method");
|
|
685 xmlnode_set_attrib(field, "type", "list-single");
|
|
686 option = xmlnode_new_child(field, "option");
|
|
687 value = xmlnode_new_child(option, "value");
|
|
688 xmlnode_insert_data(value, "http://jabber.org/protocol/bytestreams",
|
|
689 -1);
|
|
690 /*
|
|
691 option = xmlnode_new_child(field, "option");
|
|
692 value = xmlnode_new_child(option, "value");
|
|
693 xmlnode_insert_data(value, "http://jabber.org/protocol/ibb", -1);
|
|
694 */
|
|
695
|
|
696 jabber_iq_set_callback(iq, jabber_si_xfer_send_method_cb, xfer);
|
|
697
|
|
698 jabber_iq_send(iq);
|
|
699 }
|
|
700
|
|
701 static void jabber_si_xfer_free(GaimXfer *xfer)
|
|
702 {
|
|
703 JabberSIXfer *jsx = xfer->data;
|
|
704 JabberStream *js = jsx->js;
|
|
705
|
|
706 js->file_transfers = g_list_remove(js->file_transfers, xfer);
|
|
707
|
14262
|
708 if (jsx->connect_data != NULL)
|
|
709 gaim_proxy_connect_cancel(jsx->connect_data);
|
14267
|
710 if (jsx->listen_data != NULL)
|
|
711 gaim_network_listen_cancel(jsx->listen_data);
|
14192
|
712
|
|
713 g_free(jsx->stream_id);
|
|
714 g_free(jsx->iq_id);
|
|
715 /* XXX: free other stuff */
|
|
716 g_free(jsx->rxqueue);
|
|
717 g_free(jsx);
|
|
718 xfer->data = NULL;
|
|
719 }
|
|
720
|
|
721 static void jabber_si_xfer_cancel_send(GaimXfer *xfer)
|
|
722 {
|
|
723 jabber_si_xfer_free(xfer);
|
|
724 gaim_debug(GAIM_DEBUG_INFO, "jabber", "in jabber_si_xfer_cancel_send\n");
|
|
725 }
|
|
726
|
|
727
|
|
728 static void jabber_si_xfer_request_denied(GaimXfer *xfer)
|
|
729 {
|
|
730 jabber_si_xfer_free(xfer);
|
|
731 gaim_debug(GAIM_DEBUG_INFO, "jabber", "in jabber_si_xfer_request_denied\n");
|
|
732 }
|
|
733
|
|
734
|
|
735 static void jabber_si_xfer_cancel_recv(GaimXfer *xfer)
|
|
736 {
|
|
737 jabber_si_xfer_free(xfer);
|
|
738 gaim_debug(GAIM_DEBUG_INFO, "jabber", "in jabber_si_xfer_cancel_recv\n");
|
|
739 }
|
|
740
|
|
741
|
|
742 static void jabber_si_xfer_end(GaimXfer *xfer)
|
|
743 {
|
|
744 jabber_si_xfer_free(xfer);
|
|
745 }
|
|
746
|
|
747
|
|
748 static void jabber_si_xfer_send_disco_cb(JabberStream *js, const char *who,
|
|
749 JabberCapabilities capabilities, gpointer data)
|
|
750 {
|
|
751 GaimXfer *xfer = data;
|
|
752
|
|
753 if(capabilities & JABBER_CAP_SI_FILE_XFER) {
|
|
754 jabber_si_xfer_send_request(xfer);
|
|
755 } else {
|
|
756 char *msg = g_strdup_printf(_("Unable to send file to %s, user does not support file transfers"), who);
|
|
757 gaim_notify_error(js->gc, _("File Send Failed"),
|
|
758 _("File Send Failed"), msg);
|
|
759 g_free(msg);
|
|
760 }
|
|
761 }
|
|
762
|
|
763 static void jabber_si_xfer_init(GaimXfer *xfer)
|
|
764 {
|
|
765 JabberSIXfer *jsx = xfer->data;
|
|
766 JabberIq *iq;
|
|
767 if(gaim_xfer_get_type(xfer) == GAIM_XFER_SEND) {
|
|
768 JabberBuddy *jb;
|
|
769 JabberBuddyResource *jbr = NULL;
|
|
770
|
|
771 jb = jabber_buddy_find(jsx->js, xfer->who, TRUE);
|
|
772 /* XXX */
|
|
773 if(!jb)
|
|
774 return;
|
|
775
|
|
776 /* XXX: for now, send to the first resource available */
|
|
777 if(g_list_length(jb->resources) >= 1) {
|
|
778 char **who_v = g_strsplit(xfer->who, "/", 2);
|
|
779 char *who;
|
|
780
|
|
781 jbr = jabber_buddy_find_resource(jb, NULL);
|
|
782 who = g_strdup_printf("%s/%s", who_v[0], jbr->name);
|
|
783 g_strfreev(who_v);
|
|
784 g_free(xfer->who);
|
|
785 xfer->who = who;
|
|
786 jabber_disco_info_do(jsx->js, who,
|
|
787 jabber_si_xfer_send_disco_cb, xfer);
|
|
788 } else {
|
|
789 return; /* XXX: ick */
|
|
790 }
|
|
791 } else {
|
|
792 xmlnode *si, *feature, *x, *field, *value;
|
|
793
|
|
794 iq = jabber_iq_new(jsx->js, JABBER_IQ_RESULT);
|
|
795 xmlnode_set_attrib(iq->node, "to", xfer->who);
|
|
796 if(jsx->iq_id)
|
|
797 jabber_iq_set_id(iq, jsx->iq_id);
|
|
798
|
|
799 jsx->accepted = TRUE;
|
|
800
|
|
801 si = xmlnode_new_child(iq->node, "si");
|
|
802 xmlnode_set_namespace(si, "http://jabber.org/protocol/si");
|
|
803
|
|
804 feature = xmlnode_new_child(si, "feature");
|
|
805 xmlnode_set_namespace(feature, "http://jabber.org/protocol/feature-neg");
|
|
806
|
|
807 x = xmlnode_new_child(feature, "x");
|
|
808 xmlnode_set_namespace(x, "jabber:x:data");
|
|
809 xmlnode_set_attrib(x, "type", "submit");
|
|
810
|
|
811 field = xmlnode_new_child(x, "field");
|
|
812 xmlnode_set_attrib(field, "var", "stream-method");
|
|
813
|
|
814 value = xmlnode_new_child(field, "value");
|
|
815 if(jsx->stream_method & STREAM_METHOD_BYTESTREAMS)
|
|
816 xmlnode_insert_data(value, "http://jabber.org/protocol/bytestreams", -1);
|
|
817 /*
|
|
818 else if(jsx->stream_method & STREAM_METHOD_IBB)
|
|
819 xmlnode_insert_data(value, "http://jabber.org/protocol/ibb", -1);
|
|
820 */
|
|
821
|
|
822 jabber_iq_send(iq);
|
|
823 }
|
|
824 }
|
|
825
|
|
826 GaimXfer *jabber_si_new_xfer(GaimConnection *gc, const char *who)
|
|
827 {
|
|
828 JabberStream *js;
|
|
829
|
|
830 GaimXfer *xfer;
|
|
831 JabberSIXfer *jsx;
|
|
832
|
|
833 js = gc->proto_data;
|
|
834
|
|
835 xfer = gaim_xfer_new(gc->account, GAIM_XFER_SEND, who);
|
|
836
|
|
837 xfer->data = jsx = g_new0(JabberSIXfer, 1);
|
|
838 jsx->js = js;
|
|
839
|
|
840 gaim_xfer_set_init_fnc(xfer, jabber_si_xfer_init);
|
|
841 gaim_xfer_set_cancel_send_fnc(xfer, jabber_si_xfer_cancel_send);
|
|
842 gaim_xfer_set_end_fnc(xfer, jabber_si_xfer_end);
|
|
843
|
|
844 js->file_transfers = g_list_append(js->file_transfers, xfer);
|
|
845
|
|
846 return xfer;
|
|
847 }
|
|
848
|
|
849 void jabber_si_xfer_send(GaimConnection *gc, const char *who, const char *file)
|
|
850 {
|
|
851 JabberStream *js;
|
|
852
|
|
853 GaimXfer *xfer;
|
|
854
|
|
855 js = gc->proto_data;
|
|
856
|
|
857 if(!gaim_find_buddy(gc->account, who) || !jabber_buddy_find(js, who, FALSE))
|
|
858 return;
|
|
859
|
|
860 xfer = jabber_si_new_xfer(gc, who);
|
|
861
|
|
862 if (file)
|
|
863 gaim_xfer_request_accepted(xfer, file);
|
|
864 else
|
|
865 gaim_xfer_request(xfer);
|
|
866 }
|
|
867
|
|
868 void jabber_si_parse(JabberStream *js, xmlnode *packet)
|
|
869 {
|
|
870 JabberSIXfer *jsx;
|
|
871 GaimXfer *xfer;
|
|
872 xmlnode *si, *file, *feature, *x, *field, *option, *value;
|
|
873 const char *stream_id, *filename, *filesize_c, *profile, *from;
|
|
874 size_t filesize = 0;
|
|
875
|
|
876 if(!(si = xmlnode_get_child(packet, "si")))
|
|
877 return;
|
|
878
|
|
879 if(!(profile = xmlnode_get_attrib(si, "profile")) ||
|
|
880 strcmp(profile, "http://jabber.org/protocol/si/profile/file-transfer"))
|
|
881 return;
|
|
882
|
|
883 if(!(stream_id = xmlnode_get_attrib(si, "id")))
|
|
884 return;
|
|
885
|
|
886 if(!(file = xmlnode_get_child(si, "file")))
|
|
887 return;
|
|
888
|
|
889 if(!(filename = xmlnode_get_attrib(file, "name")))
|
|
890 return;
|
|
891
|
|
892 if((filesize_c = xmlnode_get_attrib(file, "size")))
|
|
893 filesize = atoi(filesize_c);
|
|
894
|
|
895 if(!(feature = xmlnode_get_child(si, "feature")))
|
|
896 return;
|
|
897
|
|
898 if(!(x = xmlnode_get_child_with_namespace(feature, "x", "jabber:x:data")))
|
|
899 return;
|
|
900
|
|
901 if(!(from = xmlnode_get_attrib(packet, "from")))
|
|
902 return;
|
|
903
|
|
904 /* if they've already sent us this file transfer with the same damn id
|
|
905 * then we're gonna ignore it, until I think of something better to do
|
|
906 * with it */
|
|
907 if((xfer = jabber_si_xfer_find(js, stream_id, from)))
|
|
908 return;
|
|
909
|
|
910 jsx = g_new0(JabberSIXfer, 1);
|
|
911
|
|
912 for(field = xmlnode_get_child(x, "field"); field; field = xmlnode_get_next_twin(field)) {
|
|
913 const char *var = xmlnode_get_attrib(field, "var");
|
|
914 if(var && !strcmp(var, "stream-method")) {
|
|
915 for(option = xmlnode_get_child(field, "option"); option;
|
|
916 option = xmlnode_get_next_twin(option)) {
|
|
917 if((value = xmlnode_get_child(option, "value"))) {
|
|
918 char *val;
|
|
919 if((val = xmlnode_get_data(value))) {
|
|
920 if(!strcmp(val, "http://jabber.org/protocol/bytestreams")) {
|
|
921 jsx->stream_method |= STREAM_METHOD_BYTESTREAMS;
|
|
922 /*
|
|
923 } else if(!strcmp(val, "http://jabber.org/protocol/ibb")) {
|
|
924 jsx->stream_method |= STREAM_METHOD_IBB;
|
|
925 */
|
|
926 }
|
|
927 g_free(val);
|
|
928 }
|
|
929 }
|
|
930 }
|
|
931 }
|
|
932 }
|
|
933
|
|
934 if(jsx->stream_method == STREAM_METHOD_UNKNOWN) {
|
|
935 g_free(jsx);
|
|
936 return;
|
|
937 }
|
|
938
|
|
939 jsx->js = js;
|
|
940 jsx->stream_id = g_strdup(stream_id);
|
|
941 jsx->iq_id = g_strdup(xmlnode_get_attrib(packet, "id"));
|
|
942
|
|
943 xfer = gaim_xfer_new(js->gc->account, GAIM_XFER_RECEIVE, from);
|
|
944 xfer->data = jsx;
|
|
945
|
|
946 gaim_xfer_set_filename(xfer, filename);
|
|
947 if(filesize > 0)
|
|
948 gaim_xfer_set_size(xfer, filesize);
|
|
949
|
|
950 gaim_xfer_set_init_fnc(xfer, jabber_si_xfer_init);
|
|
951 gaim_xfer_set_request_denied_fnc(xfer, jabber_si_xfer_request_denied);
|
|
952 gaim_xfer_set_cancel_recv_fnc(xfer, jabber_si_xfer_cancel_recv);
|
|
953 gaim_xfer_set_end_fnc(xfer, jabber_si_xfer_end);
|
|
954
|
|
955 js->file_transfers = g_list_append(js->file_transfers, xfer);
|
|
956
|
|
957 gaim_xfer_request(xfer);
|
|
958 }
|
|
959
|
|
960
|