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