13234
|
1 /*
|
|
2 * Gaim's oscar protocol plugin
|
|
3 * This file is the legal property of its developers.
|
|
4 * Please see the AUTHORS file distributed alongside this file.
|
|
5 *
|
|
6 * This library is free software; you can redistribute it and/or
|
|
7 * modify it under the terms of the GNU Lesser General Public
|
|
8 * License as published by the Free Software Foundation; either
|
|
9 * version 2 of the License, or (at your option) any later version.
|
|
10 *
|
|
11 * This library 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 GNU
|
|
14 * Lesser General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU Lesser General Public
|
|
17 * License along with this library; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 */
|
|
20
|
|
21 /*
|
13592
|
22 * Functions dealing with peer connections. This includes the code
|
|
23 * used to establish a peer connection for both Oscar File transfer
|
|
24 * (OFT) and Oscar Direct Connect (ODC). (ODC is also referred to
|
|
25 * as DirectIM and IM Image.)
|
13234
|
26 */
|
|
27
|
|
28 #ifdef HAVE_CONFIG_H
|
|
29 #include <config.h>
|
|
30 #endif
|
|
31
|
13592
|
32 /* From the oscar PRPL */
|
13234
|
33 #include "oscar.h"
|
|
34 #include "peer.h"
|
|
35
|
13592
|
36 /* From Gaim */
|
|
37 #include "conversation.h"
|
|
38 #include "ft.h"
|
|
39 #include "network.h"
|
|
40 #include "notify.h"
|
|
41 #include "request.h"
|
|
42 #include "util.h"
|
|
43
|
13234
|
44 #ifndef _WIN32
|
|
45 #include <stdio.h>
|
|
46 #include <netdb.h>
|
|
47 #include <sys/socket.h>
|
|
48 #include <netinet/in.h>
|
|
49 #include <arpa/inet.h> /* for inet_ntoa */
|
|
50 #include <limits.h> /* for UINT_MAX */
|
|
51 #endif
|
|
52
|
|
53 #ifdef _WIN32
|
|
54 #include "win32dep.h"
|
|
55 #endif
|
|
56
|
|
57 /*
|
|
58 * I really want to switch all our networking code to using IPv6 only,
|
|
59 * but that really isn't a good idea at all. Evan S. of Adium says
|
|
60 * OS X sets all connections as "AF_INET6/PF_INET6," even if there is
|
|
61 * nothing inherently IPv6 about them. And I feel like Linux kernel
|
|
62 * 2.6.5 is doing the same thing. So we REALLY should accept
|
|
63 * connections if they're showing up as IPv6. Old OSes (Solaris?)
|
|
64 * that might not have full IPv6 support yet will fail if we try
|
|
65 * to use PF_INET6 but it isn't defined. --Mark Doliner
|
|
66 */
|
|
67 #ifndef PF_INET6
|
|
68 #define PF_INET6 PF_INET
|
|
69 #endif
|
|
70
|
13592
|
71 PeerConnection *
|
|
72 peer_connection_find_by_type(OscarData *od, const char *sn, OscarCapability type)
|
13234
|
73 {
|
13254
|
74 GList *cur;
|
13592
|
75 PeerConnection *conn;
|
13234
|
76
|
13592
|
77 for (cur = od->peer_connections; cur != NULL; cur = cur->next)
|
13254
|
78 {
|
|
79 conn = cur->data;
|
13592
|
80 if ((conn->type == type) && !aim_sncmp(conn->sn, sn))
|
|
81 return conn;
|
13234
|
82 }
|
|
83
|
|
84 return NULL;
|
|
85 }
|
|
86
|
|
87 /**
|
13592
|
88 * @param cookie This must be exactly 8 characters.
|
13234
|
89 */
|
13592
|
90 PeerConnection *
|
|
91 peer_connection_find_by_cookie(OscarData *od, const char *sn, const guchar *cookie)
|
|
92 {
|
|
93 GList *cur;
|
|
94 PeerConnection *conn;
|
|
95
|
|
96 for (cur = od->peer_connections; cur != NULL; cur = cur->next)
|
|
97 {
|
|
98 conn = cur->data;
|
|
99 if (!memcmp(conn->cookie, cookie, 8) && !aim_sncmp(conn->sn, sn))
|
|
100 return conn;
|
|
101 }
|
|
102
|
|
103 return NULL;
|
|
104 }
|
|
105
|
|
106 PeerConnection *
|
|
107 peer_connection_new(OscarData *od, OscarCapability type, const char *sn)
|
13234
|
108 {
|
13592
|
109 PeerConnection *conn;
|
|
110 GaimAccount *account;
|
|
111
|
|
112 account = gaim_connection_get_account(od->gc);
|
|
113
|
|
114 conn = g_new0(PeerConnection, 1);
|
|
115 conn->od = od;
|
|
116 conn->type = type;
|
|
117 conn->sn = g_strdup(sn);
|
|
118 conn->buffer_outgoing = gaim_circ_buffer_new(0);
|
|
119 conn->listenerfd = -1;
|
|
120 conn->fd = -1;
|
|
121 conn->lastactivity = time(NULL);
|
|
122 conn->use_proxy |= gaim_account_get_bool(account, "use_rv_proxy", FALSE);
|
13234
|
123
|
13592
|
124 if (type == OSCAR_CAPABILITY_DIRECTIM)
|
|
125 memcpy(conn->magic, "ODC2", 4);
|
|
126 else if (type == OSCAR_CAPABILITY_SENDFILE)
|
|
127 memcpy(conn->magic, "OFT2", 4);
|
|
128
|
|
129 od->peer_connections = g_list_prepend(od->peer_connections, conn);
|
|
130
|
|
131 return conn;
|
|
132 }
|
|
133
|
|
134 static void
|
|
135 peer_connection_close(PeerConnection *conn)
|
|
136 {
|
|
137 if (conn->type == OSCAR_CAPABILITY_DIRECTIM)
|
|
138 peer_odc_close(conn);
|
|
139 else if (conn->type == OSCAR_CAPABILITY_SENDFILE)
|
|
140 peer_oft_close(conn);
|
13234
|
141
|
13592
|
142 if (conn->watcher_incoming != 0)
|
|
143 {
|
|
144 gaim_input_remove(conn->watcher_incoming);
|
|
145 conn->watcher_incoming = 0;
|
|
146 }
|
|
147 if (conn->watcher_outgoing != 0)
|
|
148 {
|
|
149 gaim_input_remove(conn->watcher_outgoing);
|
|
150 conn->watcher_outgoing = 0;
|
|
151 }
|
|
152 if (conn->listenerfd != -1)
|
|
153 {
|
|
154 close(conn->listenerfd);
|
|
155 conn->listenerfd = -1;
|
|
156 }
|
|
157 if (conn->fd != -1)
|
|
158 {
|
|
159 close(conn->fd);
|
|
160 conn->fd = -1;
|
|
161 }
|
13234
|
162
|
13592
|
163 g_free(conn->buffer_incoming.data);
|
|
164 conn->buffer_incoming.data = NULL;
|
|
165 conn->buffer_incoming.len = 0;
|
|
166 conn->buffer_incoming.offset = 0;
|
|
167
|
|
168 gaim_circ_buffer_destroy(conn->buffer_outgoing);
|
|
169 conn->buffer_outgoing = gaim_circ_buffer_new(0);
|
13234
|
170
|
13592
|
171 conn->flags &= ~PEER_CONNECTION_FLAG_IS_INCOMING;
|
|
172 }
|
|
173
|
|
174 static gboolean
|
|
175 peer_connection_destroy_cb(gpointer data)
|
|
176 {
|
|
177 PeerConnection *conn;
|
|
178
|
|
179 conn = data;
|
|
180
|
|
181 gaim_request_close_with_handle(conn);
|
|
182
|
|
183 peer_connection_close(conn);
|
13234
|
184
|
13592
|
185 if (conn->xfer != NULL)
|
|
186 {
|
|
187 GaimXferStatusType status;
|
|
188 conn->xfer->data = NULL;
|
|
189 status = gaim_xfer_get_status(conn->xfer);
|
|
190 if ((status != GAIM_XFER_STATUS_DONE) &&
|
|
191 (status != GAIM_XFER_STATUS_CANCEL_LOCAL) &&
|
|
192 (status != GAIM_XFER_STATUS_CANCEL_REMOTE))
|
|
193 {
|
13608
|
194 if ((conn->disconnect_reason == OSCAR_DISCONNECT_REMOTE_CLOSED) ||
|
|
195 (conn->disconnect_reason == OSCAR_DISCONNECT_REMOTE_REFUSED))
|
13592
|
196 gaim_xfer_cancel_remote(conn->xfer);
|
|
197 else
|
|
198 gaim_xfer_cancel_local(conn->xfer);
|
|
199 }
|
|
200 gaim_xfer_unref(conn->xfer);
|
|
201 conn->xfer = NULL;
|
13234
|
202 }
|
|
203
|
13592
|
204 g_free(conn->proxyip);
|
|
205 g_free(conn->clientip);
|
|
206 g_free(conn->verifiedip);
|
|
207 gaim_circ_buffer_destroy(conn->buffer_outgoing);
|
13234
|
208
|
13592
|
209 conn->od->peer_connections = g_list_remove(conn->od->peer_connections, conn);
|
|
210
|
|
211 g_free(conn);
|
|
212
|
|
213 return FALSE;
|
|
214 }
|
13234
|
215
|
13592
|
216 void
|
13608
|
217 peer_connection_destroy(PeerConnection *conn, OscarDisconnectReason reason)
|
13592
|
218 {
|
|
219 conn->disconnect_reason = reason;
|
|
220 if (conn->destroy_timeout != 0)
|
|
221 gaim_timeout_remove(conn->destroy_timeout);
|
|
222 peer_connection_destroy_cb(conn);
|
|
223 }
|
13234
|
224
|
13592
|
225 void
|
13608
|
226 peer_connection_schedule_destroy(PeerConnection *conn, OscarDisconnectReason reason)
|
13592
|
227 {
|
|
228 if (conn->destroy_timeout != 0)
|
|
229 /* Already taken care of */
|
|
230 return;
|
|
231
|
|
232 gaim_debug_info("oscar", "Scheduling destruction of peer connection\n");
|
|
233 conn->disconnect_reason = reason;
|
|
234 conn->destroy_timeout = gaim_timeout_add(0, peer_connection_destroy_cb, conn);
|
13234
|
235 }
|
|
236
|
13592
|
237 /*******************************************************************/
|
|
238 /* Begin code for receiving data on a peer connection */
|
|
239 /*******************************************************************/
|
|
240
|
13234
|
241 /**
|
13592
|
242 * This should be used to read ODC and OFT framing info. It should
|
|
243 * NOT be used to read the payload sent across the connection (IMs,
|
|
244 * file data, etc), and it should NOT be used to read proxy negotiation
|
|
245 * headers.
|
13234
|
246 *
|
13592
|
247 * Unlike flap_connection_recv_cb(), this only reads one frame at a
|
|
248 * time. This is done so that the watcher can be changed during the
|
|
249 * handling of the frame. If the watcher is changed then this
|
|
250 * function will not read in any more data. This happens when
|
|
251 * reading the payload of a direct IM frame, or when we're
|
|
252 * receiving a file from the remote user. Once the data has been
|
|
253 * read, the watcher will be switched back to this function to
|
|
254 * continue reading the next frame.
|
13234
|
255 */
|
13592
|
256 void
|
|
257 peer_connection_recv_cb(gpointer data, gint source, GaimInputCondition cond)
|
13234
|
258 {
|
13592
|
259 PeerConnection *conn;
|
|
260 ssize_t read;
|
|
261 guint8 header[6];
|
|
262
|
|
263 conn = data;
|
|
264
|
|
265 /* Start reading a new ODC/OFT frame */
|
|
266 if (conn->buffer_incoming.data == NULL)
|
|
267 {
|
|
268 /* Peek at the first 6 bytes to get the length */
|
|
269 read = recv(conn->fd, &header, 6, MSG_PEEK);
|
13234
|
270
|
13592
|
271 /* Check if the remote user closed the connection */
|
|
272 if (read == 0)
|
|
273 {
|
13608
|
274 peer_connection_destroy(conn, OSCAR_DISCONNECT_REMOTE_CLOSED);
|
13592
|
275 return;
|
|
276 }
|
|
277
|
|
278 /* If there was an error then close the connection */
|
|
279 if (read == -1)
|
|
280 {
|
|
281 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
|
|
282 /* No worries */
|
|
283 return;
|
13234
|
284
|
13608
|
285 peer_connection_destroy(conn, OSCAR_DISCONNECT_LOST_CONNECTION);
|
13592
|
286 return;
|
|
287 }
|
|
288
|
|
289 conn->lastactivity = time(NULL);
|
|
290
|
|
291 /* If we don't even have the first 6 bytes then do nothing */
|
|
292 if (read < 6)
|
|
293 return;
|
|
294
|
|
295 /* Read the first 6 bytes (magic string and frame length) */
|
|
296 read = recv(conn->fd, &header, 6, 0);
|
13234
|
297
|
13592
|
298 /* All ODC/OFT frames must start with a magic string */
|
|
299 if (memcmp(conn->magic, header, 4))
|
|
300 {
|
|
301 gaim_debug_warning("oscar", "Expecting magic string to "
|
|
302 "be %c%c%c%c but received magic string %c%c%c%c. "
|
|
303 "Closing connection.\n",
|
|
304 conn->magic[0], conn->magic[1], conn->magic[2],
|
|
305 conn->magic[3], header[0], header[1], header[2], header[3]);
|
13608
|
306 peer_connection_destroy(conn, OSCAR_DISCONNECT_INVALID_DATA);
|
13592
|
307 return;
|
|
308 }
|
|
309
|
|
310 /* Initialize a new temporary ByteStream for incoming data */
|
|
311 conn->buffer_incoming.len = aimutil_get16(&header[4]) - 6;
|
|
312 conn->buffer_incoming.data = g_new(guint8, conn->buffer_incoming.len);
|
|
313 conn->buffer_incoming.offset = 0;
|
13234
|
314 }
|
|
315
|
13592
|
316 /* Read data into the temporary buffer until it is complete */
|
|
317 read = recv(conn->fd,
|
|
318 &conn->buffer_incoming.data[conn->buffer_incoming.offset],
|
|
319 conn->buffer_incoming.len - conn->buffer_incoming.offset,
|
|
320 0);
|
|
321
|
|
322 /* Check if the remote user closed the connection */
|
|
323 if (read == 0)
|
|
324 {
|
13608
|
325 peer_connection_destroy(conn, OSCAR_DISCONNECT_REMOTE_CLOSED);
|
13592
|
326 return;
|
|
327 }
|
|
328
|
|
329 if (read == -1)
|
|
330 {
|
|
331 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
|
|
332 /* No worries */
|
|
333 return;
|
|
334
|
13608
|
335 peer_connection_destroy(conn, OSCAR_DISCONNECT_LOST_CONNECTION);
|
13592
|
336 return;
|
|
337 }
|
|
338
|
|
339 conn->lastactivity = time(NULL);
|
|
340 conn->buffer_incoming.offset += read;
|
|
341 if (conn->buffer_incoming.offset < conn->buffer_incoming.len)
|
|
342 /* Waiting for more data to arrive */
|
|
343 return;
|
|
344
|
|
345 /* We have a complete ODC/OFT frame! Handle it and continue reading */
|
|
346 byte_stream_rewind(&conn->buffer_incoming);
|
|
347 if (conn->type == OSCAR_CAPABILITY_DIRECTIM)
|
|
348 {
|
|
349 peer_odc_recv_frame(conn, &conn->buffer_incoming);
|
|
350 }
|
|
351 else if (conn->type == OSCAR_CAPABILITY_SENDFILE)
|
|
352 {
|
|
353 peer_oft_recv_frame(conn, &conn->buffer_incoming);
|
|
354 }
|
|
355 g_free(conn->buffer_incoming.data);
|
|
356 conn->buffer_incoming.data = NULL;
|
|
357 }
|
13234
|
358
|
13592
|
359 /*******************************************************************/
|
|
360 /* End code for receiving data on a peer connection */
|
|
361 /*******************************************************************/
|
|
362
|
|
363 /*******************************************************************/
|
|
364 /* Begin code for sending data on a peer connection */
|
|
365 /*******************************************************************/
|
|
366
|
|
367 static void
|
|
368 send_cb(gpointer data, gint source, GaimInputCondition cond)
|
|
369 {
|
|
370 PeerConnection *conn;
|
|
371 gsize writelen;
|
|
372 ssize_t wrotelen;
|
|
373
|
|
374 conn = data;
|
|
375 writelen = gaim_circ_buffer_get_max_read(conn->buffer_outgoing);
|
|
376
|
|
377 if (writelen == 0)
|
|
378 {
|
|
379 gaim_input_remove(conn->watcher_outgoing);
|
|
380 conn->watcher_outgoing = 0;
|
|
381 return;
|
|
382 }
|
|
383
|
|
384 wrotelen = send(conn->fd, conn->buffer_outgoing->outptr, writelen, 0);
|
|
385 if (wrotelen <= 0)
|
|
386 {
|
|
387 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
|
|
388 /* No worries */
|
|
389 return;
|
|
390
|
|
391 if (conn->ready)
|
13608
|
392 peer_connection_schedule_destroy(conn, OSCAR_DISCONNECT_LOST_CONNECTION);
|
13592
|
393 else
|
|
394 {
|
|
395 /*
|
|
396 * This could happen when unable to send a negotiation
|
|
397 * frame to a peer proxy server.
|
|
398 */
|
|
399 peer_connection_trynext(conn);
|
|
400 }
|
|
401 return;
|
|
402 }
|
|
403
|
|
404 gaim_circ_buffer_mark_read(conn->buffer_outgoing, wrotelen);
|
|
405 conn->lastactivity = time(NULL);
|
13234
|
406 }
|
|
407
|
|
408 /**
|
13592
|
409 * This should be called by OFT/ODC code to send a standard OFT or ODC
|
|
410 * frame across the peer connection along with some payload data. Or
|
|
411 * maybe a file. Anything, really.
|
13234
|
412 */
|
13592
|
413 void
|
|
414 peer_connection_send(PeerConnection *conn, ByteStream *bs)
|
13234
|
415 {
|
13592
|
416 /* Add everything to our outgoing buffer */
|
|
417 gaim_circ_buffer_append(conn->buffer_outgoing, bs->data, bs->len);
|
13234
|
418
|
13592
|
419 /* If we haven't already started writing stuff, then start the cycle */
|
|
420 if (conn->watcher_outgoing == 0)
|
|
421 {
|
|
422 conn->watcher_outgoing = gaim_input_add(conn->fd,
|
|
423 GAIM_INPUT_WRITE, send_cb, conn);
|
|
424 send_cb(conn, conn->fd, 0);
|
|
425 }
|
|
426 }
|
|
427
|
|
428 /*******************************************************************/
|
|
429 /* End code for sending data on a peer connection */
|
|
430 /*******************************************************************/
|
13234
|
431
|
13592
|
432 /*******************************************************************/
|
|
433 /* Begin code for establishing a peer connection */
|
|
434 /*******************************************************************/
|
13234
|
435
|
13592
|
436 void
|
|
437 peer_connection_finalize_connection(PeerConnection *conn)
|
|
438 {
|
|
439 conn->watcher_incoming = gaim_input_add(conn->fd,
|
|
440 GAIM_INPUT_READ, peer_connection_recv_cb, conn);
|
13234
|
441
|
13592
|
442 if (conn->type == OSCAR_CAPABILITY_DIRECTIM)
|
|
443 {
|
|
444 /*
|
|
445 * If we are connecting to them then send our cookie so they
|
|
446 * can verify who we are. Note: This doesn't seem to be
|
|
447 * necessary, but it also doesn't seem to hurt.
|
|
448 */
|
|
449 if (!(conn->flags & PEER_CONNECTION_FLAG_IS_INCOMING))
|
|
450 peer_odc_send_cookie(conn);
|
|
451 }
|
|
452 else if (conn->type == OSCAR_CAPABILITY_SENDFILE)
|
|
453 {
|
|
454 if (gaim_xfer_get_type(conn->xfer) == GAIM_XFER_SEND)
|
|
455 {
|
|
456 peer_oft_send_prompt(conn);
|
|
457 }
|
13234
|
458 }
|
|
459
|
13592
|
460 /*
|
|
461 * Tell the remote user that we're connected (which may also imply
|
|
462 * that we've accepted their request).
|
|
463 */
|
|
464 if (!(conn->flags & PEER_CONNECTION_FLAG_IS_INCOMING))
|
|
465 aim_im_sendch2_connected(conn);
|
13234
|
466 }
|
|
467
|
|
468 /**
|
13592
|
469 * We tried to make an outgoing connection to a remote user. It
|
|
470 * either connected or failed to connect.
|
13234
|
471 */
|
13592
|
472 static void
|
|
473 peer_connection_established_cb(gpointer data, gint source, GaimInputCondition cond)
|
13234
|
474 {
|
13592
|
475 NewPeerConnectionData *new_conn_data;
|
|
476 GaimConnection *gc;
|
|
477 PeerConnection *conn;
|
13234
|
478
|
13592
|
479 new_conn_data = data;
|
|
480 gc = new_conn_data->gc;
|
|
481 conn = new_conn_data->conn;
|
|
482 g_free(new_conn_data);
|
13234
|
483
|
13592
|
484 if (!g_list_find(gaim_connections_get_all(), gc))
|
|
485 {
|
|
486 if (source >= 0)
|
|
487 close(source);
|
|
488 return;
|
13234
|
489 }
|
|
490
|
13592
|
491 if (source < 0)
|
|
492 {
|
|
493 peer_connection_trynext(conn);
|
|
494 return;
|
|
495 }
|
13234
|
496
|
13592
|
497 conn->fd = source;
|
13234
|
498
|
13592
|
499 peer_connection_finalize_connection(conn);
|
13239
|
500 }
|
13234
|
501
|
|
502 /**
|
13592
|
503 * This is the watcher callback for any listening socket that is
|
|
504 * waiting for a peer to connect. When a peer connects we set the
|
|
505 * input watcher to start reading data from the peer.
|
13234
|
506 *
|
13592
|
507 * To make sure that the connection is with the intended person and
|
|
508 * not with a malicious middle man, we don't send anything until we've
|
|
509 * received a peer frame from the remote user and have verified that
|
|
510 * the cookie in the peer frame matches the cookie that was exchanged
|
|
511 * in the channel 2 ICBM.
|
13234
|
512 */
|
13592
|
513 void
|
|
514 peer_connection_listen_cb(gpointer data, gint source, GaimInputCondition cond)
|
13239
|
515 {
|
13592
|
516 PeerConnection *conn;
|
|
517 OscarData *od;
|
|
518 GaimConnection *gc;
|
|
519 GaimAccount *account;
|
|
520 struct sockaddr addr;
|
|
521 socklen_t addrlen = sizeof(addr);
|
13234
|
522
|
13592
|
523 conn = data;
|
|
524 od = conn->od;
|
|
525 gc = od->gc;
|
|
526 account = gaim_connection_get_account(gc);
|
13234
|
527
|
13592
|
528 gaim_debug_info("oscar", "Accepting connection on listener socket.\n");
|
|
529
|
|
530 conn->fd = accept(conn->listenerfd, &addr, &addrlen);
|
|
531 if (conn->fd == -1)
|
|
532 {
|
|
533 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
|
|
534 /* No connection yet--no worries */
|
|
535 /* TODO: Hmm, but they SHOULD be connected if we're here, right? */
|
|
536 return;
|
13234
|
537
|
13592
|
538 peer_connection_trynext(conn);
|
|
539 return;
|
|
540 }
|
|
541
|
|
542 if ((addr.sa_family != PF_INET) && (addr.sa_family != PF_INET6))
|
|
543 {
|
|
544 /* Invalid connection type?! Continue waiting. */
|
|
545 close(conn->fd);
|
|
546 return;
|
|
547 }
|
|
548
|
|
549 fcntl(conn->fd, F_SETFL, O_NONBLOCK);
|
|
550 gaim_input_remove(conn->watcher_incoming);
|
|
551
|
|
552 peer_connection_finalize_connection(conn);
|
13234
|
553 }
|
|
554
|
|
555 /**
|
13592
|
556 * We've just opened a listener socket, so we send the remote
|
|
557 * user an ICBM and ask them to connect to us.
|
13234
|
558 */
|
13592
|
559 static void
|
|
560 peer_connection_establish_listener_cb(int listenerfd, gpointer data)
|
13234
|
561 {
|
13592
|
562 NewPeerConnectionData *new_conn_data;
|
|
563 PeerConnection *conn;
|
|
564 OscarData *od;
|
|
565 GaimConnection *gc;
|
|
566 GaimAccount *account;
|
|
567 GaimConversation *conv;
|
|
568 char *tmp;
|
|
569 FlapConnection *bos_conn;
|
|
570 const char *listener_ip;
|
|
571 unsigned short listener_port;
|
13234
|
572
|
13592
|
573 new_conn_data = data;
|
|
574 gc = new_conn_data->gc;
|
|
575 conn = new_conn_data->conn;
|
|
576 g_free(new_conn_data);
|
13234
|
577
|
13592
|
578 if (!g_list_find(gaim_connections_get_all(), gc))
|
|
579 {
|
|
580 if (listenerfd != -1)
|
|
581 close(listenerfd);
|
|
582 return;
|
13234
|
583 }
|
|
584
|
13592
|
585 if (listenerfd == -1)
|
|
586 {
|
|
587 /* Could not open listener socket */
|
|
588 peer_connection_trynext(conn);
|
|
589 return;
|
|
590 }
|
|
591
|
|
592 od = conn->od;
|
|
593 account = gaim_connection_get_account(gc);
|
|
594 conn->listenerfd = listenerfd;
|
13234
|
595
|
13592
|
596 /* Send the "please connect to me!" ICBM */
|
|
597 bos_conn = flap_connection_findbygroup(od, SNAC_FAMILY_ICBM);
|
|
598 listener_ip = gaim_network_get_my_ip(bos_conn->fd);
|
|
599 listener_port = gaim_network_get_port_from_fd(conn->listenerfd);
|
|
600 if (conn->type == OSCAR_CAPABILITY_DIRECTIM)
|
|
601 {
|
|
602 aim_im_sendch2_odc_requestdirect(od,
|
|
603 conn->cookie, conn->sn, gaim_network_ip_atoi(listener_ip),
|
|
604 listener_port, ++conn->lastrequestnumber);
|
13234
|
605
|
13592
|
606 /* Print a message to a local conversation window */
|
|
607 conv = gaim_conversation_new(GAIM_CONV_TYPE_IM, account, conn->sn);
|
|
608 tmp = g_strdup_printf(_("Asking %s to connect to us at %s:%hu for "
|
|
609 "Direct IM."), conn->sn, listener_ip, listener_port);
|
|
610 gaim_conversation_write(conv, NULL, tmp, GAIM_MESSAGE_SYSTEM, time(NULL));
|
|
611 g_free(tmp);
|
|
612 }
|
|
613 else if (conn->type == OSCAR_CAPABILITY_SENDFILE)
|
|
614 {
|
|
615 aim_im_sendch2_sendfile_requestdirect(od,
|
|
616 conn->cookie, conn->sn,
|
|
617 gaim_network_ip_atoi(listener_ip),
|
|
618 listener_port, ++conn->lastrequestnumber,
|
|
619 (const gchar *)conn->xferdata.name,
|
|
620 conn->xferdata.size, conn->xferdata.totfiles);
|
|
621 }
|
13234
|
622 }
|
|
623
|
|
624 /**
|
13592
|
625 * Try to establish the given PeerConnection using a defined
|
|
626 * sequence of steps.
|
13234
|
627 */
|
13592
|
628 void
|
|
629 peer_connection_trynext(PeerConnection *conn)
|
13234
|
630 {
|
13592
|
631 NewPeerConnectionData *new_conn_data;
|
|
632 GaimAccount *account;
|
13234
|
633
|
13592
|
634 new_conn_data = g_new(NewPeerConnectionData, 1);
|
|
635 new_conn_data->gc = conn->od->gc;
|
|
636 new_conn_data->conn = conn;
|
|
637
|
|
638 account = gaim_connection_get_account(new_conn_data->gc);
|
13234
|
639
|
13592
|
640 /*
|
|
641 * Close any remnants of a previous failed connection attempt.
|
|
642 */
|
|
643 peer_connection_close(conn);
|
13234
|
644
|
13592
|
645 /*
|
|
646 * 1. Attempt to connect to the remote user using their verifiedip.
|
|
647 */
|
|
648 if (!(conn->flags & PEER_CONNECTION_FLAG_TRIED_VERIFIEDIP) &&
|
|
649 (conn->verifiedip != NULL) && (conn->port != 0) && (!conn->use_proxy))
|
|
650 {
|
|
651 conn->flags |= PEER_CONNECTION_FLAG_TRIED_VERIFIEDIP;
|
13234
|
652
|
13592
|
653 if (conn->type == OSCAR_CAPABILITY_DIRECTIM)
|
|
654 {
|
|
655 gchar *tmp;
|
|
656 GaimConversation *conv;
|
|
657 tmp = g_strdup_printf(_("Attempting to connect to %s:%hu."),
|
|
658 conn->verifiedip, conn->port);
|
|
659 conv = gaim_conversation_new(GAIM_CONV_TYPE_IM, account, conn->sn);
|
|
660 gaim_conversation_write(conv, NULL, tmp,
|
|
661 GAIM_MESSAGE_SYSTEM, time(NULL));
|
|
662 g_free(tmp);
|
|
663 }
|
13234
|
664
|
13592
|
665 if (gaim_proxy_connect(account, conn->verifiedip, conn->port,
|
|
666 peer_connection_established_cb, new_conn_data) == 0)
|
|
667 {
|
|
668 /* Connecting... */
|
|
669 return;
|
|
670 }
|
|
671 }
|
13234
|
672
|
13592
|
673 /*
|
|
674 * 2. Attempt to connect to the remote user using their clientip.
|
|
675 */
|
|
676 if (!(conn->flags & PEER_CONNECTION_FLAG_TRIED_CLIENTIP) &&
|
|
677 (conn->clientip != NULL) && (conn->port != 0) && (!conn->use_proxy))
|
|
678 {
|
|
679 conn->flags |= PEER_CONNECTION_FLAG_TRIED_CLIENTIP;
|
13234
|
680
|
13592
|
681 if (strcmp(conn->verifiedip, conn->clientip))
|
|
682 {
|
|
683 if (conn->type == OSCAR_CAPABILITY_DIRECTIM)
|
|
684 {
|
|
685 gchar *tmp;
|
|
686 GaimConversation *conv;
|
|
687 tmp = g_strdup_printf(_("Attempting to connect to %s:%hu."),
|
|
688 conn->clientip, conn->port);
|
|
689 conv = gaim_conversation_new(GAIM_CONV_TYPE_IM, account, conn->sn);
|
|
690 gaim_conversation_write(conv, NULL, tmp,
|
|
691 GAIM_MESSAGE_SYSTEM, time(NULL));
|
|
692 g_free(tmp);
|
|
693 }
|
13234
|
694
|
13592
|
695 if (gaim_proxy_connect(account, conn->clientip, conn->port,
|
|
696 peer_connection_established_cb, new_conn_data) == 0)
|
|
697 {
|
|
698 /* Connecting... */
|
|
699 return;
|
|
700 }
|
|
701 }
|
|
702 }
|
13234
|
703
|
13592
|
704 /*
|
|
705 * 3. Attempt to have the remote user connect to us (using both
|
|
706 * our verifiedip and our clientip).
|
|
707 */
|
|
708 if (!(conn->flags & PEER_CONNECTION_FLAG_TRIED_INCOMING) &&
|
|
709 (!conn->use_proxy))
|
|
710 {
|
|
711 conn->flags |= PEER_CONNECTION_FLAG_TRIED_INCOMING;
|
13234
|
712
|
13592
|
713 /*
|
|
714 * Remote user is connecting to us, so we'll need to verify
|
|
715 * that the user who connected is our friend.
|
|
716 */
|
|
717 conn->flags |= PEER_CONNECTION_FLAG_IS_INCOMING;
|
13234
|
718
|
13592
|
719 if (gaim_network_listen_range(5190, 5290, SOCK_STREAM,
|
|
720 peer_connection_establish_listener_cb, new_conn_data))
|
|
721 {
|
|
722 /* Opening listener socket... */
|
|
723 return;
|
|
724 }
|
|
725 }
|
13234
|
726
|
13592
|
727 /*
|
|
728 * 4. Attempt to have both users connect to an intermediate proxy
|
|
729 * server.
|
|
730 */
|
|
731 if (!(conn->flags & PEER_CONNECTION_FLAG_TRIED_PROXY))
|
|
732 {
|
|
733 conn->flags |= PEER_CONNECTION_FLAG_TRIED_PROXY;
|
13234
|
734
|
13592
|
735 /*
|
|
736 * If we initiate the proxy connection, then the remote user
|
|
737 * could be anyone, so we need to verify that the user who
|
|
738 * connected is our friend.
|
|
739 */
|
|
740 if (!conn->use_proxy)
|
|
741 conn->flags |= PEER_CONNECTION_FLAG_IS_INCOMING;
|
13234
|
742
|
13592
|
743 if (conn->type == OSCAR_CAPABILITY_DIRECTIM)
|
|
744 {
|
|
745 gchar *tmp;
|
|
746 GaimConversation *conv;
|
|
747 tmp = g_strdup_printf(_("Attempting to connect via proxy server."));
|
|
748 conv = gaim_conversation_new(GAIM_CONV_TYPE_IM, account, conn->sn);
|
|
749 gaim_conversation_write(conv, NULL, tmp,
|
|
750 GAIM_MESSAGE_SYSTEM, time(NULL));
|
|
751 g_free(tmp);
|
|
752 }
|
13234
|
753
|
13592
|
754 if (gaim_proxy_connect(account,
|
|
755 (conn->proxyip != NULL) ? conn->proxyip : PEER_PROXY_SERVER,
|
|
756 PEER_PROXY_PORT,
|
|
757 peer_proxy_connection_established_cb, new_conn_data) == 0)
|
|
758 {
|
|
759 /* Connecting... */
|
|
760 return;
|
|
761 }
|
|
762 }
|
13234
|
763
|
13592
|
764 g_free(new_conn_data);
|
13234
|
765
|
13592
|
766 /* Give up! */
|
13608
|
767 peer_connection_destroy(conn, OSCAR_DISCONNECT_COULD_NOT_CONNECT);
|
13234
|
768 }
|
|
769
|
|
770 /**
|
13592
|
771 * Initiate a peer connection with someone.
|
13234
|
772 */
|
13592
|
773 void
|
|
774 peer_connection_propose(OscarData *od, OscarCapability type, const char *sn)
|
13234
|
775 {
|
13592
|
776 PeerConnection *conn;
|
|
777 GaimAccount *account;
|
|
778
|
|
779 account = gaim_connection_get_account(od->gc);
|
13234
|
780
|
13592
|
781 if (type == OSCAR_CAPABILITY_DIRECTIM)
|
|
782 {
|
|
783 conn = peer_connection_find_by_type(od, sn, type);
|
|
784 if (conn != NULL)
|
|
785 {
|
|
786 if (conn->ready)
|
|
787 {
|
|
788 GaimConversation *conv;
|
13234
|
789
|
13592
|
790 gaim_debug_info("oscar", "Already have a direct IM "
|
|
791 "session with %s.\n", sn);
|
|
792 account = gaim_connection_get_account(od->gc);
|
|
793 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM,
|
|
794 sn, account);
|
|
795 if (conv != NULL)
|
|
796 gaim_conversation_present(conv);
|
|
797 return;
|
|
798 }
|
13234
|
799
|
13592
|
800 /* Cancel the old connection and try again */
|
13608
|
801 peer_connection_destroy(conn, OSCAR_DISCONNECT_RETRYING);
|
13592
|
802 }
|
13234
|
803 }
|
|
804
|
13592
|
805 conn = peer_connection_new(od, type, sn);
|
|
806 conn->flags |= PEER_CONNECTION_FLAG_INITIATED_BY_ME;
|
|
807 conn->flags |= PEER_CONNECTION_FLAG_APPROVED;
|
|
808 aim_icbm_makecookie(conn->cookie);
|
|
809
|
|
810 peer_connection_trynext(conn);
|
|
811 }
|
|
812
|
|
813 /**
|
|
814 * Someone else wants to establish a peer connection with us,
|
|
815 * and we said yes.
|
|
816 */
|
|
817 static void
|
|
818 peer_connection_got_proposition_yes_cb(gpointer data, gint id)
|
|
819 {
|
|
820 PeerConnection *conn;
|
|
821
|
|
822 conn = data;
|
13234
|
823
|
13592
|
824 conn->flags |= PEER_CONNECTION_FLAG_APPROVED;
|
|
825 peer_connection_trynext(conn);
|
|
826 }
|
|
827
|
|
828 /**
|
|
829 * Someone else wants to establish a peer connection with us,
|
|
830 * and we said no.
|
|
831 *
|
|
832 * "Well, one time my friend asked me if I wanted to play the
|
|
833 * piccolo. But I said no."
|
|
834 */
|
|
835 static void
|
|
836 peer_connection_got_proposition_no_cb(gpointer data, gint id)
|
|
837 {
|
|
838 PeerConnection *conn;
|
|
839
|
|
840 conn = data;
|
|
841
|
|
842 aim_im_denytransfer(conn->od, conn->sn, conn->cookie,
|
|
843 AIM_TRANSFER_DENY_DECLINE);
|
13608
|
844 peer_connection_destroy(conn, OSCAR_DISCONNECT_LOCAL_CLOSED);
|
13234
|
845 }
|
|
846
|
|
847 /**
|
13592
|
848 * Someone else wants to establish a peer connection with us.
|
13234
|
849 */
|
13592
|
850 void
|
|
851 peer_connection_got_proposition(OscarData *od, const gchar *sn, const gchar *message, IcbmArgsCh2 *args)
|
13234
|
852 {
|
13592
|
853 GaimConnection *gc;
|
|
854 GaimAccount *account;
|
|
855 PeerConnection *conn;
|
|
856 gchar *buf;
|
|
857
|
|
858 gc = od->gc;
|
|
859 account = gaim_connection_get_account(gc);
|
13239
|
860
|
13592
|
861 /*
|
|
862 * If we have a connection with this same cookie then they are
|
|
863 * probably just telling us they weren't able to connect to us
|
|
864 * and we should try connecting to them, instead. Or they want
|
|
865 * to go through a proxy.
|
|
866 */
|
|
867 conn = peer_connection_find_by_cookie(od, sn, args->cookie);
|
|
868 if ((conn != NULL) && (conn->type == args->type))
|
|
869 {
|
|
870 gaim_debug_info("oscar", "Remote user wants to try a "
|
|
871 "different connection method\n");
|
|
872 g_free(conn->proxyip);
|
|
873 g_free(conn->clientip);
|
|
874 g_free(conn->verifiedip);
|
|
875 if (args->use_proxy)
|
|
876 conn->proxyip = g_strdup(args->proxyip);
|
|
877 else
|
|
878 conn->proxyip = NULL;
|
|
879 conn->verifiedip = g_strdup(args->verifiedip);
|
|
880 conn->clientip = g_strdup(args->clientip);
|
|
881 conn->port = args->port;
|
|
882 conn->use_proxy |= args->use_proxy;
|
|
883 conn->lastrequestnumber++;
|
|
884 peer_connection_trynext(conn);
|
|
885 return;
|
|
886 }
|
13239
|
887
|
13592
|
888 /* If this is a direct IM, then close any existing session */
|
|
889 if (args->type == OSCAR_CAPABILITY_DIRECTIM)
|
|
890 {
|
|
891 conn = peer_connection_find_by_type(od, sn, args->type);
|
|
892 if (conn != NULL)
|
|
893 {
|
|
894 /* Close the old direct IM and start a new one */
|
|
895 gaim_debug_info("oscar", "Received new direct IM request "
|
|
896 "from %s. Destroying old connection.\n", sn);
|
13608
|
897 peer_connection_destroy(conn, OSCAR_DISCONNECT_REMOTE_CLOSED);
|
13592
|
898 }
|
|
899 }
|
13239
|
900
|
13592
|
901 /* Check for proper arguments */
|
|
902 if (args->type == OSCAR_CAPABILITY_SENDFILE)
|
|
903 {
|
|
904 if ((args->info.sendfile.filename == NULL) ||
|
|
905 (args->info.sendfile.totsize == 0) ||
|
|
906 (args->info.sendfile.totfiles == 0))
|
|
907 {
|
|
908 gaim_debug_warning("oscar",
|
|
909 "%s tried to send you a file with incomplete "
|
|
910 "information.\n", sn);
|
|
911 return;
|
|
912 }
|
|
913 }
|
13234
|
914
|
13592
|
915 conn = peer_connection_new(od, args->type, sn);
|
|
916 memcpy(conn->cookie, args->cookie, 8);
|
|
917 if (args->use_proxy)
|
|
918 conn->proxyip = g_strdup(args->proxyip);
|
|
919 conn->clientip = g_strdup(args->clientip);
|
|
920 conn->verifiedip = g_strdup(args->verifiedip);
|
|
921 conn->port = args->port;
|
|
922 conn->use_proxy |= args->use_proxy;
|
|
923 conn->lastrequestnumber++;
|
|
924
|
|
925 if (args->type == OSCAR_CAPABILITY_DIRECTIM)
|
|
926 {
|
|
927 buf = g_strdup_printf(_("%s has just asked to directly connect to %s"),
|
|
928 sn, gaim_account_get_username(account));
|
|
929
|
|
930 gaim_request_action(conn, NULL, buf,
|
|
931 _("This requires a direct connection between "
|
|
932 "the two computers and is necessary for IM "
|
|
933 "Images. Because your IP address will be "
|
|
934 "revealed, this may be considered a privacy "
|
|
935 "risk."),
|
|
936 GAIM_DEFAULT_ACTION_NONE, conn, 2,
|
|
937 _("_Connect"), G_CALLBACK(peer_connection_got_proposition_yes_cb),
|
|
938 _("Cancel"), G_CALLBACK(peer_connection_got_proposition_no_cb));
|
|
939 }
|
|
940 else if (args->type == OSCAR_CAPABILITY_SENDFILE)
|
|
941 {
|
|
942 gchar *filename;
|
|
943
|
|
944 conn->xfer = gaim_xfer_new(account, GAIM_XFER_RECEIVE, sn);
|
|
945 conn->xfer->data = conn;
|
|
946 gaim_xfer_ref(conn->xfer);
|
|
947 gaim_xfer_set_size(conn->xfer, args->info.sendfile.totsize);
|
|
948
|
|
949 /* Set the file name */
|
|
950 if (g_utf8_validate(args->info.sendfile.filename, -1, NULL))
|
|
951 filename = g_strdup(args->info.sendfile.filename);
|
|
952 else
|
|
953 filename = gaim_utf8_salvage(args->info.sendfile.filename);
|
|
954
|
|
955 if (args->info.sendfile.subtype == AIM_OFT_SUBTYPE_SEND_DIR)
|
|
956 {
|
|
957 /*
|
|
958 * If they are sending us a directory then the last character
|
|
959 * of the file name will be an asterisk. We don't want to
|
|
960 * save stuff to a directory named "*" so we remove the
|
|
961 * asterisk from the file name.
|
|
962 */
|
|
963 char *tmp = strrchr(filename, '\\');
|
|
964 if ((tmp != NULL) && (tmp[1] == '*'))
|
|
965 tmp[0] = '\0';
|
13239
|
966 }
|
13592
|
967 gaim_xfer_set_filename(conn->xfer, filename);
|
|
968 g_free(filename);
|
|
969
|
|
970 /*
|
|
971 * Set the message (unless this is the dummy message from an
|
|
972 * ICQ client or an empty message from an AIM client.
|
|
973 * TODO: Maybe we should strip HTML and then see if strlen>0?
|
|
974 */
|
|
975 if ((message != NULL) &&
|
|
976 (g_ascii_strncasecmp(message, "<ICQ_COOL_FT>", 13) != 0) &&
|
|
977 (g_ascii_strcasecmp(message, "<HTML>") != 0))
|
|
978 {
|
|
979 gaim_xfer_set_message(conn->xfer, message);
|
|
980 }
|
|
981
|
|
982 /* Setup our I/O op functions */
|
|
983 gaim_xfer_set_init_fnc(conn->xfer, peer_oft_recvcb_init);
|
|
984 gaim_xfer_set_end_fnc(conn->xfer, peer_oft_recvcb_end);
|
|
985 gaim_xfer_set_request_denied_fnc(conn->xfer, peer_oft_cb_generic_cancel);
|
|
986 gaim_xfer_set_cancel_recv_fnc(conn->xfer, peer_oft_cb_generic_cancel);
|
|
987 gaim_xfer_set_ack_fnc(conn->xfer, peer_oft_recvcb_ack_recv);
|
|
988
|
|
989 /* Now perform the request */
|
|
990 gaim_xfer_request(conn->xfer);
|
13234
|
991 }
|
|
992 }
|
13592
|
993
|
|
994 /*******************************************************************/
|
|
995 /* End code for establishing a peer connection */
|
|
996 /*******************************************************************/
|