6 * Copyright (C) 2010-11 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2010 Jakub Adam <jakub.adam@ktknet.cz>
8 * Copyright (C) 2010 Tomáš Hrabčík <tomas.hrabcik@tieto.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <glib/gprintf.h>
36 #include "sip-transport.h"
37 #include "sipe-backend.h"
38 #include "sipe-core.h"
39 #include "sipe-core-private.h"
40 #include "sipe-crypt.h"
41 #include "sipe-dialog.h"
42 #include "sipe-digest.h"
46 #include "sipe-session.h"
47 #include "sipe-utils.h"
49 #define SIPE_FT_KEY_LENGTH 24
50 #define SIPE_FT_CHUNK_HEADER_LENGTH 3
51 #define BUFFER_SIZE 50
54 * DO NOT CHANGE THE FOLLOWING CONSTANTS!!!
56 * It seems that Microsoft Office Communicator client will accept
57 * file transfer invitations *only* within this port range!
59 * If a firewall is active on your system you need to open these ports if
60 * you want to *send* files to other users. Receiving files uses an outgoing
61 * connection and should therefore automatically penetrate your firewall.
63 #define SIPE_FT_TCP_PORT_MIN 6891
64 #define SIPE_FT_TCP_PORT_MAX 6901
67 * File transport (private part)
69 struct sipe_file_transfer_private
{
70 struct sipe_file_transfer
public;
72 struct sipe_core_private
*sipe_private
;
74 guchar encryption_key
[SIPE_FT_KEY_LENGTH
];
75 guchar hash_key
[SIPE_FT_KEY_LENGTH
];
77 gchar
*invitation_cookie
;
79 struct sip_dialog
*dialog
;
81 gpointer cipher_context
;
82 gpointer hmac_context
;
84 gsize bytes_remaining_chunk
;
86 guchar
*encrypted_outbuf
;
90 #define SIPE_FILE_TRANSFER_PUBLIC ((struct sipe_file_transfer *) ft_private)
91 #define SIPE_FILE_TRANSFER_PRIVATE ((struct sipe_file_transfer_private *) ft)
93 static void raise_ft_error(struct sipe_file_transfer_private
*ft_private
,
96 gchar
*tmp
= g_strdup_printf("%s: %s", errmsg
,
97 sipe_backend_ft_get_error(SIPE_FILE_TRANSFER_PUBLIC
));
98 sipe_backend_ft_error(SIPE_FILE_TRANSFER_PUBLIC
, tmp
);
102 static void raise_ft_error_and_cancel(struct sipe_file_transfer_private
*ft_private
,
105 sipe_backend_ft_error(SIPE_FILE_TRANSFER_PUBLIC
, errmsg
);
106 sipe_backend_ft_cancel_local(SIPE_FILE_TRANSFER_PUBLIC
);
109 static void raise_ft_socket_read_error_and_cancel(struct sipe_file_transfer_private
*ft_private
)
111 raise_ft_error_and_cancel(ft_private
, _("Socket read failed"));
114 static void raise_ft_socket_write_error_and_cancel(struct sipe_file_transfer_private
*ft_private
)
116 raise_ft_error_and_cancel(ft_private
, _("Socket write failed"));
119 static gboolean
read_exact(struct sipe_file_transfer_private
*ft_private
,
123 const gulong READ_TIMEOUT
= 10000000;
124 gulong time_spent
= 0;
127 gssize bytes_read
= sipe_backend_ft_read(SIPE_FILE_TRANSFER_PUBLIC
,
129 if (bytes_read
== 0) {
131 time_spent
+= 100000;
132 } else if (bytes_read
< 0 || time_spent
> READ_TIMEOUT
) {
143 static gboolean
read_line(struct sipe_file_transfer_private
*ft_private
,
149 if (size
< 2) return FALSE
;
151 memset(data
, 0, size
--);
153 if (!read_exact(ft_private
, data
+ pos
, 1))
155 } while ((data
[pos
] != '\n') && (++pos
< size
));
157 /* Buffer too short? */
158 if ((pos
== size
) && (data
[pos
- 1] != '\n')) {
165 static gboolean
write_exact(struct sipe_file_transfer_private
*ft_private
,
169 gssize bytes_written
= sipe_backend_ft_write(SIPE_FILE_TRANSFER_PUBLIC
,
171 if ((bytes_written
< 0) || ((gsize
) bytes_written
!= size
))
176 static void generate_key(guchar
*buffer
, gsize size
)
179 while (i
< size
) buffer
[i
++] = rand();
182 static gpointer
sipe_cipher_context_init(const guchar
*enc_key
)
185 * Decryption of file from SIPE file transfer
188 * 1.) SHA1-Key = SHA1sum (Encryption-Key); Do SHA1 digest from Encryption-Key, return 20 bytes SHA1-Key.
189 * 2.) Decrypt-Data = RC4 (Encrypt-Data, substr(SHA1-Key, 0, 15)); Decryption of encrypted data, used 16 bytes SHA1-Key;
192 guchar k2
[SIPE_DIGEST_SHA1_LENGTH
];
195 sipe_digest_sha1(enc_key
, SIPE_FT_KEY_LENGTH
, k2
);
197 /* 2.) RC4 decryption */
198 return sipe_crypt_ft_start(k2
);
201 static gpointer
sipe_hmac_context_init(const guchar
*hash_key
)
207 * 1.) SHA1-Key = SHA1sum (Hash-Key); Do SHA1 digest from Hash-Key, return 20 bytes SHA1-Key.
208 * 2.) MAC = HMAC_SHA1 (Decrypt-Data, substr(HMAC-Key,0,15)); Digest of decrypted file and SHA1-Key (used again only 16 bytes)
211 guchar k2
[SIPE_DIGEST_SHA1_LENGTH
];
214 sipe_digest_sha1(hash_key
, SIPE_FT_KEY_LENGTH
, k2
);
216 /* 2.) HMAC (initialization only) */
217 return sipe_digest_ft_start(k2
);
220 static gchar
*sipe_hmac_finalize(gpointer hmac_context
)
222 guchar hmac_digest
[SIPE_DIGEST_FILETRANSFER_LENGTH
];
224 /* MAC = Digest of decrypted file and SHA1-Key (used again only 16 bytes) */
225 sipe_digest_ft_end(hmac_context
, hmac_digest
);
227 return g_base64_encode(hmac_digest
, sizeof (hmac_digest
));
230 struct sipe_file_transfer
*sipe_core_ft_allocate(struct sipe_core_public
*sipe_public
)
232 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
233 struct sipe_file_transfer_private
*ft_private
=
234 g_new0(struct sipe_file_transfer_private
, 1);
236 ft_private
->sipe_private
= sipe_private
;
237 ft_private
->invitation_cookie
= g_strdup_printf("%u", rand() % 1000000000);
239 return(SIPE_FILE_TRANSFER_PUBLIC
);
242 static void sipe_ft_deallocate(struct sipe_file_transfer
*ft
)
244 struct sipe_file_transfer_private
*ft_private
= SIPE_FILE_TRANSFER_PRIVATE
;
246 if (ft
->backend_private
)
247 sipe_backend_ft_deallocate(ft
);
249 if (ft_private
->cipher_context
)
250 sipe_crypt_ft_destroy(ft_private
->cipher_context
);
252 if (ft_private
->hmac_context
)
253 sipe_digest_ft_destroy(ft_private
->hmac_context
);
255 g_free(ft_private
->invitation_cookie
);
256 g_free(ft_private
->encrypted_outbuf
);
260 void sipe_core_ft_deallocate(struct sipe_file_transfer
*ft
)
262 struct sipe_file_transfer_private
*ft_private
= SIPE_FILE_TRANSFER_PRIVATE
;
263 struct sip_dialog
*dialog
= ft_private
->dialog
;
266 dialog
->filetransfers
= g_slist_remove(dialog
->filetransfers
, ft_private
);
268 sipe_ft_deallocate(ft
);
271 static void sipe_ft_request(struct sipe_file_transfer_private
*ft_private
,
274 struct sip_dialog
*dialog
= ft_private
->dialog
;
275 sip_transport_request(ft_private
->sipe_private
,
279 "Content-Type: text/x-msmsgsinvite; charset=UTF-8\r\n",
285 void sipe_core_ft_cancel(struct sipe_file_transfer
*ft
)
287 struct sipe_file_transfer_private
*ft_private
= SIPE_FILE_TRANSFER_PRIVATE
;
289 gchar
*body
= g_strdup_printf("Invitation-Command: CANCEL\r\n"
290 "Invitation-Cookie: %s\r\n"
291 "Cancel-Code: REJECT\r\n",
292 ft_private
->invitation_cookie
);
293 sipe_ft_request(ft_private
, body
);
297 void sipe_core_ft_incoming_init(struct sipe_file_transfer
*ft
)
299 struct sipe_file_transfer_private
*ft_private
= SIPE_FILE_TRANSFER_PRIVATE
;
300 gchar
*b64_encryption_key
= g_base64_encode(ft_private
->encryption_key
,
302 gchar
*b64_hash_key
= g_base64_encode(ft_private
->hash_key
,
305 gchar
*body
= g_strdup_printf("Invitation-Command: ACCEPT\r\n"
306 "Request-Data: IP-Address:\r\n"
307 "Invitation-Cookie: %s\r\n"
308 "Encryption-Key: %s\r\n"
310 /*"IP-Address: %s\r\n"
313 "Auth-Cookie: 11111111\r\n"
314 "Sender-Connect: TRUE\r\n"*/,
315 ft_private
->invitation_cookie
,
318 /*,sipe_backend_network_ip_address()*/
320 sipe_ft_request(ft_private
, body
);
323 g_free(b64_hash_key
);
324 g_free(b64_encryption_key
);
327 void sipe_core_ft_incoming_accept(struct sipe_file_transfer
*ft
,
332 struct sipe_file_transfer_private
*ft_private
= SIPE_FILE_TRANSFER_PRIVATE
;
335 ft_private
->auth_cookie
= rand() % 1000000000;
337 body
= g_strdup_printf("Invitation-Command: ACCEPT\r\n"
338 "Invitation-Cookie: %s\r\n"
343 "Request-Data: IP-Address:\r\n",
344 ft_private
->invitation_cookie
,
345 sipe_utils_get_suitable_local_ip(fd
),
347 ft_private
->auth_cookie
);
349 if (!ft_private
->dialog
) {
350 struct sip_session
*session
= sipe_session_find_or_add_im(ft_private
->sipe_private
,
352 ft_private
->dialog
= sipe_dialog_find(session
, who
);
355 if (ft_private
->dialog
) {
356 sipe_ft_request(ft_private
, body
);
361 void sipe_core_ft_incoming_start(struct sipe_file_transfer
*ft
,
364 static const guchar VER
[] = "VER MSN_SECURE_FTP\r\n";
365 static const guchar TFR
[] = "TFR\r\n";
366 const gsize FILE_SIZE_OFFSET
= 4;
368 struct sipe_file_transfer_private
*ft_private
= SIPE_FILE_TRANSFER_PRIVATE
;
369 guchar buf
[BUFFER_SIZE
];
373 if (!write_exact(ft_private
, VER
, sizeof(VER
) - 1)) {
374 raise_ft_socket_read_error_and_cancel(ft_private
);
377 if (!read_line(ft_private
, buf
, BUFFER_SIZE
)) {
378 raise_ft_socket_read_error_and_cancel(ft_private
);
382 request
= g_strdup_printf("USR %s %u\r\n",
383 ft_private
->sipe_private
->username
,
384 ft_private
->auth_cookie
);
385 if (!write_exact(ft_private
, (guchar
*)request
, strlen(request
))) {
386 raise_ft_socket_write_error_and_cancel(ft_private
);
392 if (!read_line(ft_private
, buf
, BUFFER_SIZE
)) {
393 raise_ft_socket_read_error_and_cancel(ft_private
);
397 file_size
= g_ascii_strtoull((gchar
*) buf
+ FILE_SIZE_OFFSET
, NULL
, 10);
398 if (file_size
!= total_size
) {
399 raise_ft_error_and_cancel(ft_private
,
400 _("File size is different from the advertised value."));
404 if (!sipe_backend_ft_write(SIPE_FILE_TRANSFER_PUBLIC
, TFR
, sizeof(TFR
) - 1)) {
405 raise_ft_socket_write_error_and_cancel(ft_private
);
409 ft_private
->bytes_remaining_chunk
= 0;
410 ft_private
->cipher_context
= sipe_cipher_context_init(ft_private
->encryption_key
);
411 ft_private
->hmac_context
= sipe_hmac_context_init(ft_private
->hash_key
);
414 gboolean
sipe_core_ft_incoming_stop(struct sipe_file_transfer
*ft
)
416 static const guchar BYE
[] = "BYE 16777989\r\n";
417 const gsize MAC_OFFSET
= 4;
419 struct sipe_file_transfer_private
*ft_private
= SIPE_FILE_TRANSFER_PRIVATE
;
420 gchar buffer
[BUFFER_SIZE
];
425 if (!sipe_backend_ft_write(SIPE_FILE_TRANSFER_PUBLIC
, BYE
, sizeof(BYE
) - 1)) {
426 raise_ft_socket_write_error_and_cancel(ft_private
);
430 if (!read_line(ft_private
, (guchar
*) buffer
, BUFFER_SIZE
)) {
431 raise_ft_socket_read_error_and_cancel(ft_private
);
435 mac_len
= strlen(buffer
);
436 if (mac_len
< (MAC_OFFSET
)) {
437 raise_ft_error_and_cancel(ft_private
,
438 _("Received MAC is corrupted"));
443 mac
= g_strndup(buffer
+ MAC_OFFSET
, mac_len
- MAC_OFFSET
);
444 mac1
= sipe_hmac_finalize(ft_private
->hmac_context
);
445 if (!sipe_strequal(mac
, mac1
)) {
448 raise_ft_error_and_cancel(ft_private
,
449 _("Received file is corrupted"));
458 void sipe_core_ft_outgoing_init(struct sipe_file_transfer
*ft
,
459 const gchar
*filename
, gsize size
,
462 struct sipe_file_transfer_private
*ft_private
= SIPE_FILE_TRANSFER_PRIVATE
;
463 struct sipe_core_private
*sipe_private
= ft_private
->sipe_private
;
464 struct sip_dialog
*dialog
;
466 gchar
*body
= g_strdup_printf("Application-Name: File Transfer\r\n"
467 "Application-GUID: {5D3E02AB-6190-11d3-BBBB-00C04F795683}\r\n"
468 "Invitation-Command: INVITE\r\n"
469 "Invitation-Cookie: %s\r\n"
470 "Application-File: %s\r\n"
471 "Application-FileSize: %" G_GSIZE_FORMAT
"\r\n"
472 //"Connectivity: N\r\n" TODO
473 "Encryption: R\r\n", // TODO: non encrypted file transfer support
474 ft_private
->invitation_cookie
,
478 struct sip_session
*session
= sipe_session_find_or_add_im(sipe_private
, who
);
481 sipe_session_enqueue_message(session
, body
, "text/x-msmsgsinvite");
483 dialog
= sipe_dialog_find(session
, who
);
484 if (dialog
&& !dialog
->outgoing_invite
) {
485 sipe_im_process_queue(sipe_private
, session
);
486 } else if (!dialog
|| !dialog
->outgoing_invite
) {
487 // Need to send the INVITE to get the outgoing dialog setup
488 sipe_im_invite(sipe_private
, session
, who
, body
, "text/x-msmsgsinvite", NULL
, FALSE
);
489 dialog
= sipe_dialog_find(session
, who
);
492 dialog
->filetransfers
= g_slist_append(dialog
->filetransfers
, ft_private
);
493 ft_private
->dialog
= dialog
;
498 void sipe_core_ft_outgoing_start(struct sipe_file_transfer
*ft
,
501 static const guchar VER
[] = "VER MSN_SECURE_FTP\r\n";
503 struct sipe_file_transfer_private
*ft_private
= SIPE_FILE_TRANSFER_PRIVATE
;
504 guchar buf
[BUFFER_SIZE
];
506 unsigned auth_cookie_received
;
507 gboolean users_match
;
509 if (!read_line(ft_private
, buf
, BUFFER_SIZE
)) {
510 raise_ft_socket_read_error_and_cancel(ft_private
);
514 if (!sipe_strequal((gchar
*)buf
, (gchar
*)VER
)) {
515 raise_ft_error_and_cancel(ft_private
,
516 _("File transfer initialization failed."));
517 SIPE_DEBUG_INFO("File transfer VER string incorrect, received: %s expected: %s",
522 if (!write_exact(ft_private
, VER
, sizeof(VER
) - 1)) {
523 raise_ft_socket_write_error_and_cancel(ft_private
);
527 if (!read_line(ft_private
, buf
, BUFFER_SIZE
)) {
528 raise_ft_socket_read_error_and_cancel(ft_private
);
532 parts
= g_strsplit((gchar
*)buf
, " ", 3);
533 auth_cookie_received
= g_ascii_strtoull(parts
[2], NULL
, 10);
534 /* dialog->with has 'sip:' prefix, skip these four characters */
535 users_match
= sipe_strcase_equal(parts
[1],
536 (ft_private
->dialog
->with
+ 4));
539 SIPE_DEBUG_INFO("File transfer authentication: %s Expected: USR %s %u",
541 ft_private
->dialog
->with
+ 4,
542 ft_private
->auth_cookie
);
545 (ft_private
->auth_cookie
!= auth_cookie_received
)) {
546 raise_ft_error_and_cancel(ft_private
,
547 _("File transfer authentication failed."));
551 g_sprintf((gchar
*)buf
, "FIL %" G_GSIZE_FORMAT
"\r\n", total_size
);
552 if (!write_exact(ft_private
, buf
, strlen((gchar
*)buf
))) {
553 raise_ft_socket_write_error_and_cancel(ft_private
);
558 if (!read_line(ft_private
,buf
, BUFFER_SIZE
)) {
559 raise_ft_socket_read_error_and_cancel(ft_private
);
563 ft_private
->bytes_remaining_chunk
= 0;
564 ft_private
->cipher_context
= sipe_cipher_context_init(ft_private
->encryption_key
);
565 ft_private
->hmac_context
= sipe_hmac_context_init(ft_private
->hash_key
);
568 gboolean
sipe_core_ft_outgoing_stop(struct sipe_file_transfer
*ft
)
570 struct sipe_file_transfer_private
*ft_private
= SIPE_FILE_TRANSFER_PRIVATE
;
571 guchar buffer
[BUFFER_SIZE
];
576 if (!read_line(ft_private
, buffer
, BUFFER_SIZE
)) {
577 raise_ft_socket_read_error_and_cancel(ft_private
);
581 mac
= sipe_hmac_finalize(ft_private
->hmac_context
);
582 g_sprintf((gchar
*)buffer
, "MAC %s \r\n", mac
);
585 mac_len
= strlen((gchar
*)buffer
);
586 /* There must be this zero byte between mac and \r\n */
587 buffer
[mac_len
- 3] = 0;
589 if (!write_exact(ft_private
, buffer
, mac_len
)) {
590 raise_ft_socket_write_error_and_cancel(ft_private
);
597 gssize
sipe_core_ft_read(struct sipe_file_transfer
*ft
, guchar
**buffer
,
598 gsize bytes_remaining
, gsize bytes_available
)
600 struct sipe_file_transfer_private
*ft_private
= SIPE_FILE_TRANSFER_PRIVATE
;
604 if (ft_private
->bytes_remaining_chunk
== 0) {
605 guchar hdr_buf
[SIPE_FT_CHUNK_HEADER_LENGTH
];
607 /* read chunk header */
608 if (!read_exact(ft_private
, hdr_buf
, sizeof(hdr_buf
))) {
609 raise_ft_error(ft_private
, _("Socket read failed"));
613 /* chunk header format:
615 * 0: 00 unknown (always zero?)
616 * 1: LL chunk size in bytes (low byte)
617 * 2: HH chunk size in bytes (high byte)
619 * Convert size from little endian to host order
621 ft_private
->bytes_remaining_chunk
=
622 hdr_buf
[1] + (hdr_buf
[2] << 8);
625 bytes_to_read
= MIN(bytes_remaining
, bytes_available
);
626 bytes_to_read
= MIN(bytes_to_read
, ft_private
->bytes_remaining_chunk
);
628 *buffer
= g_malloc(bytes_to_read
);
630 sipe_backend_ft_error(SIPE_FILE_TRANSFER_PUBLIC
, _("Out of memory"));
631 SIPE_DEBUG_ERROR("sipe_core_ft_read: can't allocate %" G_GSIZE_FORMAT
" bytes for receive buffer",
636 bytes_read
= sipe_backend_ft_read(SIPE_FILE_TRANSFER_PUBLIC
, *buffer
, bytes_to_read
);
637 if (bytes_read
< 0) {
638 raise_ft_error(ft_private
, _("Socket read failed"));
644 if (bytes_read
> 0) {
645 guchar
*decrypted
= g_malloc(bytes_read
);
648 sipe_backend_ft_error(SIPE_FILE_TRANSFER_PUBLIC
, _("Out of memory"));
649 SIPE_DEBUG_ERROR("sipe_core_ft_read: can't allocate %" G_GSIZE_FORMAT
" bytes for decryption buffer",
655 sipe_crypt_ft_stream(ft_private
->cipher_context
,
656 *buffer
, bytes_read
, decrypted
);
660 sipe_digest_ft_update(ft_private
->hmac_context
,
661 decrypted
, bytes_read
);
663 ft_private
->bytes_remaining_chunk
-= bytes_read
;
669 gssize
sipe_core_ft_write(struct sipe_file_transfer
*ft
,
670 const guchar
*buffer
, gsize size
)
672 struct sipe_file_transfer_private
*ft_private
= SIPE_FILE_TRANSFER_PRIVATE
;
673 gssize bytes_written
;
675 /* When sending data via server with ForeFront installed, block bigger than
676 * this default causes ending of transmission. Hard limit block to this value
677 * when libpurple sends us more data. */
678 const gsize DEFAULT_BLOCK_SIZE
= 2045;
679 if (size
> DEFAULT_BLOCK_SIZE
)
680 size
= DEFAULT_BLOCK_SIZE
;
682 if (ft_private
->bytes_remaining_chunk
== 0) {
684 guchar local_buf
[16];
685 guchar hdr_buf
[SIPE_FT_CHUNK_HEADER_LENGTH
];
687 memset(local_buf
, 0, sizeof local_buf
);
689 /* Check if receiver did not cancel the transfer
690 before it is finished */
691 bytes_read
= sipe_backend_ft_read(SIPE_FILE_TRANSFER_PUBLIC
,
694 if (bytes_read
< 0) {
695 sipe_backend_ft_error(SIPE_FILE_TRANSFER_PUBLIC
,
696 _("Socket read failed"));
698 } else if ((bytes_read
> 0) &&
699 (g_str_has_prefix((gchar
*)local_buf
, "CCL\r\n") ||
700 g_str_has_prefix((gchar
*)local_buf
, "BYE 2164261682\r\n"))) {
704 if (ft_private
->outbuf_size
< size
) {
705 g_free(ft_private
->encrypted_outbuf
);
706 ft_private
->outbuf_size
= size
;
707 ft_private
->encrypted_outbuf
= g_malloc(ft_private
->outbuf_size
);
708 if (!ft_private
->encrypted_outbuf
) {
709 sipe_backend_ft_error(SIPE_FILE_TRANSFER_PUBLIC
,
711 SIPE_DEBUG_ERROR("sipe_core_ft_write: can't allocate %" G_GSIZE_FORMAT
" bytes for send buffer",
712 ft_private
->outbuf_size
);
717 ft_private
->bytes_remaining_chunk
= size
;
718 ft_private
->outbuf_ptr
= ft_private
->encrypted_outbuf
;
719 sipe_crypt_ft_stream(ft_private
->cipher_context
,
721 ft_private
->encrypted_outbuf
);
722 sipe_digest_ft_update(ft_private
->hmac_context
,
725 /* chunk header format:
727 * 0: 00 unknown (always zero?)
728 * 1: LL chunk size in bytes (low byte)
729 * 2: HH chunk size in bytes (high byte)
731 * Convert size from host order to little endian
734 hdr_buf
[1] = (ft_private
->bytes_remaining_chunk
& 0x00FF);
735 hdr_buf
[2] = (ft_private
->bytes_remaining_chunk
& 0xFF00) >> 8;
737 /* write chunk header */
738 if (!sipe_backend_ft_write(SIPE_FILE_TRANSFER_PUBLIC
, hdr_buf
, sizeof(hdr_buf
))) {
739 sipe_backend_ft_error(SIPE_FILE_TRANSFER_PUBLIC
,
740 _("Socket write failed"));
745 bytes_written
= sipe_backend_ft_write(SIPE_FILE_TRANSFER_PUBLIC
,
746 ft_private
->outbuf_ptr
,
747 ft_private
->bytes_remaining_chunk
);
748 if (bytes_written
< 0) {
749 raise_ft_error(ft_private
, _("Socket write failed"));
750 } else if (bytes_written
> 0) {
751 ft_private
->bytes_remaining_chunk
-= bytes_written
;
752 ft_private
->outbuf_ptr
+= bytes_written
;
755 return bytes_written
;
758 void sipe_ft_incoming_transfer(struct sipe_core_private
*sipe_private
,
759 struct sip_dialog
*dialog
,
762 struct sipe_file_transfer_private
*ft_private
;
765 ft_private
= g_new0(struct sipe_file_transfer_private
, 1);
766 ft_private
->sipe_private
= sipe_private
;
768 generate_key(ft_private
->encryption_key
, SIPE_FT_KEY_LENGTH
);
769 generate_key(ft_private
->hash_key
, SIPE_FT_KEY_LENGTH
);
771 ft_private
->invitation_cookie
= g_strdup(sipe_utils_nameval_find(body
, "Invitation-Cookie"));
773 ft_private
->dialog
= dialog
;
775 file_size
= g_ascii_strtoull(sipe_utils_nameval_find(body
,
776 "Application-FileSize"),
778 sipe_backend_ft_incoming(SIPE_CORE_PUBLIC
,
779 SIPE_FILE_TRANSFER_PUBLIC
,
781 sipe_utils_nameval_find(body
, "Application-File"),
784 if (ft_private
->public.backend_private
!= NULL
) {
785 ft_private
->dialog
->filetransfers
= g_slist_append(ft_private
->dialog
->filetransfers
, ft_private
);
787 sipe_ft_deallocate(SIPE_FILE_TRANSFER_PUBLIC
);
791 static struct sipe_file_transfer_private
*
792 sipe_find_ft(const struct sip_dialog
*dialog
, const gchar
*inv_cookie
)
794 GSList
*ftlist
= dialog
->filetransfers
;
795 for (; ftlist
!= NULL
; ftlist
= ftlist
->next
) {
796 struct sipe_file_transfer_private
*ft_private
= ftlist
->data
;
797 if (sipe_strequal(ft_private
->invitation_cookie
, inv_cookie
))
803 void sipe_ft_incoming_accept(struct sip_dialog
*dialog
, const GSList
*body
)
805 const gchar
*inv_cookie
= sipe_utils_nameval_find(body
, "Invitation-Cookie");
806 struct sipe_file_transfer_private
*ft_private
= sipe_find_ft(dialog
, inv_cookie
);
809 const gchar
*ip
= sipe_utils_nameval_find(body
, "IP-Address");
810 const gchar
*port_str
= sipe_utils_nameval_find(body
, "Port");
811 const gchar
*auth_cookie
= sipe_utils_nameval_find(body
, "AuthCookie");
812 const gchar
*enc_key_b64
= sipe_utils_nameval_find(body
, "Encryption-Key");
813 const gchar
*hash_key_b64
= sipe_utils_nameval_find(body
, "Hash-Key");
816 ft_private
->auth_cookie
= g_ascii_strtoull(auth_cookie
,
820 guchar
*enc_key
= g_base64_decode(enc_key_b64
,
822 if (ret_len
== SIPE_FT_KEY_LENGTH
) {
823 memcpy(ft_private
->encryption_key
,
824 enc_key
, SIPE_FT_KEY_LENGTH
);
826 raise_ft_error_and_cancel(ft_private
,
827 _("Received encryption key has wrong size."));
835 guchar
*hash_key
= g_base64_decode(hash_key_b64
,
837 if (ret_len
== SIPE_FT_KEY_LENGTH
) {
838 memcpy(ft_private
->hash_key
,
839 hash_key
, SIPE_FT_KEY_LENGTH
);
841 raise_ft_error_and_cancel(ft_private
,
842 _("Received hash key has wrong size."));
850 if (ip
&& port_str
) {
851 unsigned short port
= g_ascii_strtoull(port_str
,
854 sipe_backend_ft_incoming_accept(SIPE_FILE_TRANSFER_PUBLIC
,
859 if (!sipe_backend_ft_incoming_accept(SIPE_FILE_TRANSFER_PUBLIC
,
861 SIPE_FT_TCP_PORT_MIN
,
862 SIPE_FT_TCP_PORT_MAX
)) {
863 raise_ft_error_and_cancel(ft_private
,
864 _("Could not create listen socket"));
871 void sipe_ft_incoming_cancel(struct sip_dialog
*dialog
, const GSList
*body
)
873 const gchar
*inv_cookie
= sipe_utils_nameval_find(body
, "Invitation-Cookie");
874 struct sipe_file_transfer_private
*ft_private
= sipe_find_ft(dialog
, inv_cookie
);
877 sipe_backend_ft_cancel_remote(SIPE_FILE_TRANSFER_PUBLIC
);
880 GSList
*sipe_ft_parse_msg_body(const gchar
*body
)
883 gchar
**lines
= g_strsplit(body
, "\r\n", 0);
884 if (sipe_utils_parse_lines(&list
, lines
, ":") == FALSE
) {
885 sipe_utils_nameval_free(list
);