core: move IM related code out of sipe.c
[siplcs.git] / src / core / sipe-ft.c
bloba918aa9b91c3d5c7934e3389281a5d30cf2bf679
1 /**
2 * @file sipe-ft.c
4 * pidgin-sipe
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
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
29 #include <stdlib.h>
30 #include <string.h>
32 #include <glib.h>
33 #include <glib/gprintf.h>
35 #include "sipmsg.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"
43 #include "sipe-ft.h"
44 #include "sipe-im.h"
45 #include "sipe-nls.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
66 /**
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];
76 unsigned auth_cookie;
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;
87 guchar *outbuf_ptr;
88 gsize outbuf_size;
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,
94 const gchar *errmsg)
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);
99 g_free(tmp);
102 static void raise_ft_error_and_cancel(struct sipe_file_transfer_private *ft_private,
103 const gchar *errmsg)
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,
120 guchar *data,
121 gsize size)
123 const gulong READ_TIMEOUT = 10000000;
124 gulong time_spent = 0;
126 while (size) {
127 gssize bytes_read = sipe_backend_ft_read(SIPE_FILE_TRANSFER_PUBLIC,
128 data, size);
129 if (bytes_read == 0) {
130 g_usleep(100000);
131 time_spent += 100000;
132 } else if (bytes_read < 0 || time_spent > READ_TIMEOUT) {
133 return FALSE;
134 } else {
135 size -= bytes_read;
136 data += bytes_read;
137 time_spent = 0;
140 return TRUE;
143 static gboolean read_line(struct sipe_file_transfer_private *ft_private,
144 guchar *data,
145 gsize size)
147 gsize pos = 0;
149 if (size < 2) return FALSE;
151 memset(data, 0, size--);
152 do {
153 if (!read_exact(ft_private, data + pos, 1))
154 return FALSE;
155 } while ((data[pos] != '\n') && (++pos < size));
157 /* Buffer too short? */
158 if ((pos == size) && (data[pos - 1] != '\n')) {
159 return FALSE;
162 return TRUE;
165 static gboolean write_exact(struct sipe_file_transfer_private *ft_private,
166 const guchar *data,
167 gsize size)
169 gssize bytes_written = sipe_backend_ft_write(SIPE_FILE_TRANSFER_PUBLIC,
170 data, size);
171 if ((bytes_written < 0) || ((gsize) bytes_written != size))
172 return FALSE;
173 return TRUE;
176 static void generate_key(guchar *buffer, gsize size)
178 gsize i = 0;
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
187 * Decryption:
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];
194 /* 1.) SHA1 sum */
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)
204 * Count MAC digest
206 * HMAC digest:
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];
213 /* 1.) SHA1 sum */
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);
257 g_free(ft_private);
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;
265 if (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,
272 const gchar *body)
274 struct sip_dialog *dialog = ft_private->dialog;
275 sip_transport_request(ft_private->sipe_private,
276 "MESSAGE",
277 dialog->with,
278 dialog->with,
279 "Content-Type: text/x-msmsgsinvite; charset=UTF-8\r\n",
280 body,
281 dialog,
282 NULL);
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);
294 g_free(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,
301 SIPE_FT_KEY_LENGTH);
302 gchar *b64_hash_key = g_base64_encode(ft_private->hash_key,
303 SIPE_FT_KEY_LENGTH);
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"
309 "Hash-Key: %s\r\n"
310 /*"IP-Address: %s\r\n"
311 "Port: 6900\r\n"
312 "PortX: 11178\r\n"
313 "Auth-Cookie: 11111111\r\n"
314 "Sender-Connect: TRUE\r\n"*/,
315 ft_private->invitation_cookie,
316 b64_encryption_key,
317 b64_hash_key
318 /*,sipe_backend_network_ip_address()*/
320 sipe_ft_request(ft_private, body);
322 g_free(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,
328 const gchar *who,
329 int fd,
330 unsigned short port)
332 struct sipe_file_transfer_private *ft_private = SIPE_FILE_TRANSFER_PRIVATE;
333 gchar *body;
335 ft_private->auth_cookie = rand() % 1000000000;
337 body = g_strdup_printf("Invitation-Command: ACCEPT\r\n"
338 "Invitation-Cookie: %s\r\n"
339 "IP-Address: %s\r\n"
340 "Port: %u\r\n"
341 "PortX: 11178\r\n"
342 "AuthCookie: %u\r\n"
343 "Request-Data: IP-Address:\r\n",
344 ft_private->invitation_cookie,
345 sipe_utils_get_suitable_local_ip(fd),
346 port,
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,
351 who);
352 ft_private->dialog = sipe_dialog_find(session, who);
355 if (ft_private->dialog) {
356 sipe_ft_request(ft_private, body);
358 g_free(body);
361 void sipe_core_ft_incoming_start(struct sipe_file_transfer *ft,
362 gsize total_size)
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];
370 gchar *request;
371 gsize file_size;
373 if (!write_exact(ft_private, VER, sizeof(VER) - 1)) {
374 raise_ft_socket_read_error_and_cancel(ft_private);
375 return;
377 if (!read_line(ft_private, buf, BUFFER_SIZE)) {
378 raise_ft_socket_read_error_and_cancel(ft_private);
379 return;
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);
387 g_free(request);
388 return;
390 g_free(request);
392 if (!read_line(ft_private, buf, BUFFER_SIZE)) {
393 raise_ft_socket_read_error_and_cancel(ft_private);
394 return;
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."));
401 return;
404 if (!sipe_backend_ft_write(SIPE_FILE_TRANSFER_PUBLIC, TFR, sizeof(TFR) - 1)) {
405 raise_ft_socket_write_error_and_cancel(ft_private);
406 return;
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];
421 gsize mac_len;
422 gchar *mac;
423 gchar *mac1;
425 if (!sipe_backend_ft_write(SIPE_FILE_TRANSFER_PUBLIC, BYE, sizeof(BYE) - 1)) {
426 raise_ft_socket_write_error_and_cancel(ft_private);
427 return FALSE;
430 if (!read_line(ft_private, (guchar *) buffer, BUFFER_SIZE)) {
431 raise_ft_socket_read_error_and_cancel(ft_private);
432 return FALSE;
435 mac_len = strlen(buffer);
436 if (mac_len < (MAC_OFFSET)) {
437 raise_ft_error_and_cancel(ft_private,
438 _("Received MAC is corrupted"));
439 return FALSE;
442 /* Check MAC */
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)) {
446 g_free(mac1);
447 g_free(mac);
448 raise_ft_error_and_cancel(ft_private,
449 _("Received file is corrupted"));
450 return(FALSE);
452 g_free(mac1);
453 g_free(mac);
455 return(TRUE);
458 void sipe_core_ft_outgoing_init(struct sipe_file_transfer *ft,
459 const gchar *filename, gsize size,
460 const gchar *who)
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,
475 filename,
476 size);
478 struct sip_session *session = sipe_session_find_or_add_im(sipe_private, who);
480 // Queue the message
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;
495 g_free(body);
498 void sipe_core_ft_outgoing_start(struct sipe_file_transfer *ft,
499 gsize total_size)
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];
505 gchar **parts;
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);
511 return;
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",
518 buf, VER);
519 return;
522 if (!write_exact(ft_private, VER, sizeof(VER) - 1)) {
523 raise_ft_socket_write_error_and_cancel(ft_private);
524 return;
527 if (!read_line(ft_private, buf, BUFFER_SIZE)) {
528 raise_ft_socket_read_error_and_cancel(ft_private);
529 return;
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));
537 g_strfreev(parts);
539 SIPE_DEBUG_INFO("File transfer authentication: %s Expected: USR %s %u",
540 buf,
541 ft_private->dialog->with + 4,
542 ft_private->auth_cookie);
544 if (!users_match ||
545 (ft_private->auth_cookie != auth_cookie_received)) {
546 raise_ft_error_and_cancel(ft_private,
547 _("File transfer authentication failed."));
548 return;
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);
554 return;
557 /* TFR */
558 if (!read_line(ft_private ,buf, BUFFER_SIZE)) {
559 raise_ft_socket_read_error_and_cancel(ft_private);
560 return;
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];
572 gchar *mac;
573 gsize mac_len;
575 /* BYE */
576 if (!read_line(ft_private, buffer, BUFFER_SIZE)) {
577 raise_ft_socket_read_error_and_cancel(ft_private);
578 return FALSE;
581 mac = sipe_hmac_finalize(ft_private->hmac_context);
582 g_sprintf((gchar *)buffer, "MAC %s \r\n", mac);
583 g_free(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);
591 return FALSE;
594 return TRUE;
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;
601 gsize bytes_to_read;
602 gssize bytes_read;
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"));
610 return -1;
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);
629 if (!*buffer) {
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",
632 bytes_to_read);
633 return -1;
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"));
639 g_free(*buffer);
640 *buffer = NULL;
641 return -1;
644 if (bytes_read > 0) {
645 guchar *decrypted = g_malloc(bytes_read);
647 if (!decrypted) {
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",
650 (gsize)bytes_read);
651 g_free(*buffer);
652 *buffer = NULL;
653 return -1;
655 sipe_crypt_ft_stream(ft_private->cipher_context,
656 *buffer, bytes_read, decrypted);
657 g_free(*buffer);
658 *buffer = decrypted;
660 sipe_digest_ft_update(ft_private->hmac_context,
661 decrypted, bytes_read);
663 ft_private->bytes_remaining_chunk -= bytes_read;
666 return(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) {
683 gssize bytes_read;
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,
692 local_buf,
693 sizeof(local_buf));
694 if (bytes_read < 0) {
695 sipe_backend_ft_error(SIPE_FILE_TRANSFER_PUBLIC,
696 _("Socket read failed"));
697 return -1;
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"))) {
701 return -1;
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,
710 _("Out of memory"));
711 SIPE_DEBUG_ERROR("sipe_core_ft_write: can't allocate %" G_GSIZE_FORMAT " bytes for send buffer",
712 ft_private->outbuf_size);
713 return -1;
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,
720 buffer, size,
721 ft_private->encrypted_outbuf);
722 sipe_digest_ft_update(ft_private->hmac_context,
723 buffer, size);
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
733 hdr_buf[0] = 0;
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"));
741 return -1;
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,
760 const GSList *body)
762 struct sipe_file_transfer_private *ft_private;
763 gsize file_size;
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"),
777 NULL, 10);
778 sipe_backend_ft_incoming(SIPE_CORE_PUBLIC,
779 SIPE_FILE_TRANSFER_PUBLIC,
780 dialog->with,
781 sipe_utils_nameval_find(body, "Application-File"),
782 file_size);
784 if (ft_private->public.backend_private != NULL) {
785 ft_private->dialog->filetransfers = g_slist_append(ft_private->dialog->filetransfers, ft_private);
786 } else {
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))
798 return ft_private;
800 return NULL;
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);
808 if (ft_private) {
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");
815 if (auth_cookie)
816 ft_private->auth_cookie = g_ascii_strtoull(auth_cookie,
817 NULL, 10);
818 if (enc_key_b64) {
819 gsize ret_len;
820 guchar *enc_key = g_base64_decode(enc_key_b64,
821 &ret_len);
822 if (ret_len == SIPE_FT_KEY_LENGTH) {
823 memcpy(ft_private->encryption_key,
824 enc_key, SIPE_FT_KEY_LENGTH);
825 } else {
826 raise_ft_error_and_cancel(ft_private,
827 _("Received encryption key has wrong size."));
828 g_free(enc_key);
829 return;
831 g_free(enc_key);
833 if (hash_key_b64) {
834 gsize ret_len;
835 guchar *hash_key = g_base64_decode(hash_key_b64,
836 &ret_len);
837 if (ret_len == SIPE_FT_KEY_LENGTH) {
838 memcpy(ft_private->hash_key,
839 hash_key, SIPE_FT_KEY_LENGTH);
840 } else {
841 raise_ft_error_and_cancel(ft_private,
842 _("Received hash key has wrong size."));
843 g_free(hash_key);
844 return;
846 g_free(hash_key);
850 if (ip && port_str) {
851 unsigned short port = g_ascii_strtoull(port_str,
852 NULL, 10);
854 sipe_backend_ft_incoming_accept(SIPE_FILE_TRANSFER_PUBLIC,
856 port,
857 port);
858 } else {
859 if (!sipe_backend_ft_incoming_accept(SIPE_FILE_TRANSFER_PUBLIC,
860 NULL,
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"));
865 return;
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);
876 if (ft_private)
877 sipe_backend_ft_cancel_remote(SIPE_FILE_TRANSFER_PUBLIC);
880 GSList *sipe_ft_parse_msg_body(const gchar *body)
882 GSList *list = NULL;
883 gchar **lines = g_strsplit(body, "\r\n", 0);
884 if (sipe_utils_parse_lines(&list, lines, ":") == FALSE) {
885 sipe_utils_nameval_free(list);
886 list = NULL;
888 g_strfreev(lines);
889 return list;
893 Local Variables:
894 mode: c
895 c-file-style: "bsd"
896 indent-tabs-mode: t
897 tab-width: 8
898 End: