aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheJackiMonster <thejackimonster@gmail.com>2021-07-12 00:21:25 +0200
committerTheJackiMonster <thejackimonster@gmail.com>2021-07-12 00:21:25 +0200
commitd1a6ac474d731b74ea1b191a76471b698687a278 (patch)
tree342dd1a4d260f02185f11cfb1931bce030cd5b5d
parentb880617514f9e64e991cb8d6fcff74ce3f292e36 (diff)
downloadlibgnunetchat-d1a6ac474d731b74ea1b191a76471b698687a278.tar.gz
libgnunetchat-d1a6ac474d731b74ea1b191a76471b698687a278.zip
Most of the file handling and necessary disk io calls
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r--include/gnunet_chat_lib.h45
-rw-r--r--src/gnunet_chat_context.c10
-rw-r--r--src/gnunet_chat_context_intern.c48
-rw-r--r--src/gnunet_chat_file.c37
-rw-r--r--src/gnunet_chat_file.h11
-rw-r--r--src/gnunet_chat_handle.c30
-rw-r--r--src/gnunet_chat_handle.h5
-rw-r--r--src/gnunet_chat_handle_intern.c157
-rw-r--r--src/gnunet_chat_lib.c213
-rw-r--r--src/gnunet_chat_util.c148
-rw-r--r--src/gnunet_chat_util.h21
11 files changed, 612 insertions, 113 deletions
diff --git a/include/gnunet_chat_lib.h b/include/gnunet_chat_lib.h
index 4c15df5..9c2a432 100644
--- a/include/gnunet_chat_lib.h
+++ b/include/gnunet_chat_lib.h
@@ -194,6 +194,7 @@ typedef int
194 * TODO 194 * TODO
195 * 195 *
196 * @param cfg 196 * @param cfg
197 * @param directory
197 * @param name 198 * @param name
198 * @param warn_cb 199 * @param warn_cb
199 * @param warn_cls 200 * @param warn_cls
@@ -201,6 +202,7 @@ typedef int
201 */ 202 */
202struct GNUNET_CHAT_Handle* 203struct GNUNET_CHAT_Handle*
203GNUNET_CHAT_start (const struct GNUNET_CONFIGURATION_Handle *cfg, 204GNUNET_CHAT_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
205 const char *directory,
204 const char *name, 206 const char *name,
205 GNUNET_CHAT_WarningCallback warn_cb, void *warn_cls, 207 GNUNET_CHAT_WarningCallback warn_cb, void *warn_cls,
206 GNUNET_CHAT_ContextMessageCallback msg_cb, void *msg_cls); 208 GNUNET_CHAT_ContextMessageCallback msg_cb, void *msg_cls);
@@ -398,8 +400,9 @@ GNUNET_CHAT_group_get_context (struct GNUNET_CHAT_Group *group);
398 * 400 *
399 * @param context 401 * @param context
400 * @param text 402 * @param text
403 * @return
401 */ 404 */
402void 405int
403GNUNET_CHAT_context_send_text (struct GNUNET_CHAT_Context *context, 406GNUNET_CHAT_context_send_text (struct GNUNET_CHAT_Context *context,
404 const char *text); 407 const char *text);
405 408
@@ -408,8 +411,9 @@ GNUNET_CHAT_context_send_text (struct GNUNET_CHAT_Context *context,
408 * 411 *
409 * @param context 412 * @param context
410 * @param path 413 * @param path
414 * @return
411 */ 415 */
412void 416int
413GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, 417GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context,
414 const char *path); 418 const char *path);
415 419
@@ -418,8 +422,9 @@ GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context,
418 * 422 *
419 * @param context 423 * @param context
420 * @param uri 424 * @param uri
425 * @return
421 */ 426 */
422void 427int
423GNUNET_CHAT_context_send_uri (struct GNUNET_CHAT_Context *context, 428GNUNET_CHAT_context_send_uri (struct GNUNET_CHAT_Context *context,
424 const char *uri); 429 const char *uri);
425 430
@@ -428,8 +433,9 @@ GNUNET_CHAT_context_send_uri (struct GNUNET_CHAT_Context *context,
428 * 433 *
429 * @param context 434 * @param context
430 * @param file 435 * @param file
436 * @return
431 */ 437 */
432void 438int
433GNUNET_CHAT_context_share_file (struct GNUNET_CHAT_Context *context, 439GNUNET_CHAT_context_share_file (struct GNUNET_CHAT_Context *context,
434 const struct GNUNET_CHAT_File *file); 440 const struct GNUNET_CHAT_File *file);
435 441
@@ -437,17 +443,6 @@ GNUNET_CHAT_context_share_file (struct GNUNET_CHAT_Context *context,
437 * TODO 443 * TODO
438 * 444 *
439 * @param context 445 * @param context
440 * @param hash
441 * @param delay
442 */
443void
444GNUNET_CHAT_context_delete_message (const struct GNUNET_CHAT_Message *message,
445 struct GNUNET_TIME_Relative delay);
446
447/**
448 * TODO
449 *
450 * @param context
451 * @param callback 446 * @param callback
452 * @param cls 447 * @param cls
453 * @return 448 * @return
@@ -528,6 +523,17 @@ GNUNET_CHAT_message_get_invitation (const struct GNUNET_CHAT_Message *message);
528/** 523/**
529 * TODO 524 * TODO
530 * 525 *
526 * @param message
527 * @param delay
528 * @return
529 */
530int
531GNUNET_CHAT_message_delete (const struct GNUNET_CHAT_Message *message,
532 struct GNUNET_TIME_Relative delay);
533
534/**
535 * TODO
536 *
531 * @param file 537 * @param file
532 * @return 538 * @return
533 */ 539 */
@@ -593,6 +599,15 @@ GNUNET_CHAT_file_stop_download (struct GNUNET_CHAT_File *file);
593/** 599/**
594 * TODO 600 * TODO
595 * 601 *
602 * @param file
603 * @return
604 */
605int
606GNUNET_CHAT_file_unindex (struct GNUNET_CHAT_File *file);
607
608/**
609 * TODO
610 *
596 * @param invitation 611 * @param invitation
597 */ 612 */
598void 613void
diff --git a/src/gnunet_chat_context.c b/src/gnunet_chat_context.c
index 54c63f7..da49402 100644
--- a/src/gnunet_chat_context.c
+++ b/src/gnunet_chat_context.c
@@ -24,6 +24,8 @@
24 24
25#include "gnunet_chat_context.h" 25#include "gnunet_chat_context.h"
26 26
27#include "gnunet_chat_context_intern.c"
28
27struct GNUNET_CHAT_Context* 29struct GNUNET_CHAT_Context*
28context_create_from_room (struct GNUNET_CHAT_Handle *handle, 30context_create_from_room (struct GNUNET_CHAT_Handle *handle,
29 struct GNUNET_MESSENGER_Room *room) 31 struct GNUNET_MESSENGER_Room *room)
@@ -47,6 +49,14 @@ context_create_from_room (struct GNUNET_CHAT_Handle *handle,
47void 49void
48context_destroy (struct GNUNET_CHAT_Context* context) 50context_destroy (struct GNUNET_CHAT_Context* context)
49{ 51{
52 GNUNET_CONTAINER_multihashmap_iterate(
53 context->messages, it_destroy_context_messages, NULL
54 );
55
56 GNUNET_CONTAINER_multihashmap_iterate(
57 context->messages, it_destroy_context_invites, NULL
58 );
59
50 GNUNET_CONTAINER_multihashmap_destroy(context->messages); 60 GNUNET_CONTAINER_multihashmap_destroy(context->messages);
51 GNUNET_CONTAINER_multihashmap_destroy(context->invites); 61 GNUNET_CONTAINER_multihashmap_destroy(context->invites);
52 GNUNET_CONTAINER_multihashmap_destroy(context->files); 62 GNUNET_CONTAINER_multihashmap_destroy(context->files);
diff --git a/src/gnunet_chat_context_intern.c b/src/gnunet_chat_context_intern.c
new file mode 100644
index 0000000..af35e60
--- /dev/null
+++ b/src/gnunet_chat_context_intern.c
@@ -0,0 +1,48 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2021 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/*
21 * @author Tobias Frisch
22 * @file gnunet_chat_context_intern.c
23 */
24
25#include "gnunet_chat_invitation.h"
26#include "gnunet_chat_message.h"
27
28#define GNUNET_UNUSED __attribute__ ((unused))
29
30int
31it_destroy_context_messages (GNUNET_UNUSED void *cls,
32 GNUNET_UNUSED const struct GNUNET_HashCode *key,
33 void *value)
34{
35 struct GNUNET_CHAT_Message *message = value;
36 message_destroy(message);
37 return GNUNET_YES;
38}
39
40int
41it_destroy_context_invites (GNUNET_UNUSED void *cls,
42 GNUNET_UNUSED const struct GNUNET_HashCode *key,
43 void *value)
44{
45 struct GNUNET_CHAT_Invitation *invitation = value;
46 invitation_destroy(invitation);
47 return GNUNET_YES;
48}
diff --git a/src/gnunet_chat_file.c b/src/gnunet_chat_file.c
index d472551..fdfbe4c 100644
--- a/src/gnunet_chat_file.c
+++ b/src/gnunet_chat_file.c
@@ -39,6 +39,12 @@ file_create_from_message (struct GNUNET_CHAT_Handle *handle,
39 GNUNET_memcpy(&(file->key), &(message->key), sizeof(file->key)); 39 GNUNET_memcpy(&(file->key), &(message->key), sizeof(file->key));
40 GNUNET_memcpy(&(file->hash), &(message->hash), sizeof(file->hash)); 40 GNUNET_memcpy(&(file->hash), &(message->hash), sizeof(file->hash));
41 41
42 file->published = 0;
43 file->downloaded = 0;
44 file->unindexed = 0;
45
46 file->meta = GNUNET_CONTAINER_meta_data_create();
47
42 file->uri = GNUNET_FS_uri_parse(message->uri, NULL); 48 file->uri = GNUNET_FS_uri_parse(message->uri, NULL);
43 file->download = NULL; 49 file->download = NULL;
44 file->publish = NULL; 50 file->publish = NULL;
@@ -47,12 +53,43 @@ file_create_from_message (struct GNUNET_CHAT_Handle *handle,
47 return file; 53 return file;
48} 54}
49 55
56struct GNUNET_CHAT_File*
57file_create_from_disk (struct GNUNET_CHAT_Handle *handle,
58 const char *name, const struct GNUNET_HashCode *hash,
59 const struct GNUNET_CRYPTO_SymmetricSessionKey *key)
60{
61 struct GNUNET_CHAT_File* file = GNUNET_new(struct GNUNET_CHAT_File);
62
63 file->handle = handle;
64
65 file->name = GNUNET_strndup(name, NAME_MAX);
66
67 GNUNET_memcpy(&(file->key), key, sizeof(file->key));
68 GNUNET_memcpy(&(file->hash), hash, sizeof(file->hash));
69
70 file->published = 0;
71 file->downloaded = 0;
72 file->unindexed = 0;
73
74 file->meta = GNUNET_CONTAINER_meta_data_create();
75
76 file->uri = NULL;
77 file->download = NULL;
78 file->publish = NULL;
79 file->unindex = NULL;
80
81 return file;
82}
83
50void 84void
51file_destroy (struct GNUNET_CHAT_File* file) 85file_destroy (struct GNUNET_CHAT_File* file)
52{ 86{
53 if (file->uri) 87 if (file->uri)
54 GNUNET_FS_uri_destroy(file->uri); 88 GNUNET_FS_uri_destroy(file->uri);
55 89
90 if (file->meta)
91 GNUNET_CONTAINER_meta_data_destroy(file->meta);
92
56 if (file->name) 93 if (file->name)
57 GNUNET_free(file->name); 94 GNUNET_free(file->name);
58 95
diff --git a/src/gnunet_chat_file.h b/src/gnunet_chat_file.h
index 9669bf2..e4f4e12 100644
--- a/src/gnunet_chat_file.h
+++ b/src/gnunet_chat_file.h
@@ -43,6 +43,12 @@ struct GNUNET_CHAT_File
43 struct GNUNET_HashCode hash; 43 struct GNUNET_HashCode hash;
44 struct GNUNET_CRYPTO_SymmetricSessionKey key; 44 struct GNUNET_CRYPTO_SymmetricSessionKey key;
45 45
46 uint64_t published;
47 uint64_t downloaded;
48 uint64_t unindexed;
49
50 struct GNUNET_CONTAINER_MetaData *meta;
51
46 struct GNUNET_FS_Uri* uri; 52 struct GNUNET_FS_Uri* uri;
47 struct GNUNET_FS_DownloadContext* download; 53 struct GNUNET_FS_DownloadContext* download;
48 struct GNUNET_FS_PublishContext* publish; 54 struct GNUNET_FS_PublishContext* publish;
@@ -53,6 +59,11 @@ struct GNUNET_CHAT_File*
53file_create_from_message (struct GNUNET_CHAT_Handle *handle, 59file_create_from_message (struct GNUNET_CHAT_Handle *handle,
54 const struct GNUNET_MESSENGER_MessageFile* message); 60 const struct GNUNET_MESSENGER_MessageFile* message);
55 61
62struct GNUNET_CHAT_File*
63file_create_from_disk (struct GNUNET_CHAT_Handle *handle,
64 const char *name, const struct GNUNET_HashCode *hash,
65 const struct GNUNET_CRYPTO_SymmetricSessionKey *key);
66
56void 67void
57file_destroy (struct GNUNET_CHAT_File* file); 68file_destroy (struct GNUNET_CHAT_File* file);
58 69
diff --git a/src/gnunet_chat_handle.c b/src/gnunet_chat_handle.c
index 11b57ac..7556987 100644
--- a/src/gnunet_chat_handle.c
+++ b/src/gnunet_chat_handle.c
@@ -28,7 +28,8 @@
28 28
29struct GNUNET_CHAT_Handle* 29struct GNUNET_CHAT_Handle*
30handle_create_from_config (const struct GNUNET_CONFIGURATION_Handle* cfg, 30handle_create_from_config (const struct GNUNET_CONFIGURATION_Handle* cfg,
31 const char* name, 31 const char *directory,
32 const char *name,
32 GNUNET_CHAT_ContextMessageCallback msg_cb, 33 GNUNET_CHAT_ContextMessageCallback msg_cb,
33 void *msg_cls, 34 void *msg_cls,
34 GNUNET_CHAT_WarningCallback warn_cb, 35 GNUNET_CHAT_WarningCallback warn_cb,
@@ -38,6 +39,12 @@ handle_create_from_config (const struct GNUNET_CONFIGURATION_Handle* cfg,
38 39
39 handle->cfg = cfg; 40 handle->cfg = cfg;
40 41
42 if ((directory) &&
43 (GNUNET_YES == GNUNET_DISK_directory_test(directory, GNUNET_YES)))
44 handle->directory = GNUNET_strdup(directory);
45 else
46 handle->directory = NULL;
47
41 handle->msg_cb = msg_cb; 48 handle->msg_cb = msg_cb;
42 handle->msg_cls = msg_cls; 49 handle->msg_cls = msg_cls;
43 50
@@ -58,7 +65,7 @@ handle_create_from_config (const struct GNUNET_CONFIGURATION_Handle* cfg,
58 on_handle_arm_connection(handle, GNUNET_NO); 65 on_handle_arm_connection(handle, GNUNET_NO);
59 66
60 handle->fs = GNUNET_FS_start( 67 handle->fs = GNUNET_FS_start(
61 handle->cfg, name, // TODO: raw name? 68 handle->cfg, name, // TODO: raw name? (NULL?)
62 notify_handle_fs_progress, handle, 69 notify_handle_fs_progress, handle,
63 GNUNET_FS_FLAGS_NONE, 70 GNUNET_FS_FLAGS_NONE,
64 GNUNET_FS_OPTIONS_END 71 GNUNET_FS_OPTIONS_END
@@ -85,10 +92,29 @@ handle_destroy (struct GNUNET_CHAT_Handle* handle)
85 if (handle->arm) 92 if (handle->arm)
86 GNUNET_ARM_disconnect(handle->arm); 93 GNUNET_ARM_disconnect(handle->arm);
87 94
95 GNUNET_CONTAINER_multihashmap_iterate(
96 handle->groups, it_destroy_handle_groups, NULL
97 );
98
99 GNUNET_CONTAINER_multishortmap_iterate(
100 handle->contacts, it_destroy_handle_contacts, NULL
101 );
102
103 GNUNET_CONTAINER_multihashmap_iterate(
104 handle->contexts, it_destroy_handle_contexts, NULL
105 );
106
107 GNUNET_CONTAINER_multihashmap_iterate(
108 handle->files, it_destroy_handle_files, NULL
109 );
110
88 GNUNET_CONTAINER_multihashmap_destroy(handle->groups); 111 GNUNET_CONTAINER_multihashmap_destroy(handle->groups);
89 GNUNET_CONTAINER_multishortmap_destroy(handle->contacts); 112 GNUNET_CONTAINER_multishortmap_destroy(handle->contacts);
90 GNUNET_CONTAINER_multihashmap_destroy(handle->contexts); 113 GNUNET_CONTAINER_multihashmap_destroy(handle->contexts);
91 GNUNET_CONTAINER_multihashmap_destroy(handle->files); 114 GNUNET_CONTAINER_multihashmap_destroy(handle->files);
92 115
116 if (handle->directory)
117 GNUNET_free(handle->directory);
118
93 GNUNET_free(handle); 119 GNUNET_free(handle);
94} 120}
diff --git a/src/gnunet_chat_handle.h b/src/gnunet_chat_handle.h
index a0f44e0..49ec43a 100644
--- a/src/gnunet_chat_handle.h
+++ b/src/gnunet_chat_handle.h
@@ -40,6 +40,8 @@ struct GNUNET_CHAT_Handle
40{ 40{
41 const struct GNUNET_CONFIGURATION_Handle* cfg; 41 const struct GNUNET_CONFIGURATION_Handle* cfg;
42 42
43 char *directory;
44
43 GNUNET_CHAT_ContextMessageCallback msg_cb; 45 GNUNET_CHAT_ContextMessageCallback msg_cb;
44 void *msg_cls; 46 void *msg_cls;
45 47
@@ -58,7 +60,8 @@ struct GNUNET_CHAT_Handle
58 60
59struct GNUNET_CHAT_Handle* 61struct GNUNET_CHAT_Handle*
60handle_create_from_config (const struct GNUNET_CONFIGURATION_Handle* cfg, 62handle_create_from_config (const struct GNUNET_CONFIGURATION_Handle* cfg,
61 const char* name, 63 const char *directory,
64 const char *name,
62 GNUNET_CHAT_ContextMessageCallback msg_cb, 65 GNUNET_CHAT_ContextMessageCallback msg_cb,
63 void *msg_cls, 66 void *msg_cls,
64 GNUNET_CHAT_WarningCallback warn_cb, 67 GNUNET_CHAT_WarningCallback warn_cb,
diff --git a/src/gnunet_chat_handle_intern.c b/src/gnunet_chat_handle_intern.c
index 282492c..a34c63e 100644
--- a/src/gnunet_chat_handle_intern.c
+++ b/src/gnunet_chat_handle_intern.c
@@ -24,8 +24,10 @@
24 24
25#include "gnunet_chat_contact.h" 25#include "gnunet_chat_contact.h"
26#include "gnunet_chat_context.h" 26#include "gnunet_chat_context.h"
27#include "gnunet_chat_file.h"
27#include "gnunet_chat_group.h" 28#include "gnunet_chat_group.h"
28#include "gnunet_chat_handle.h" 29#include "gnunet_chat_handle.h"
30#include "gnunet_chat_invitation.h"
29#include "gnunet_chat_message.h" 31#include "gnunet_chat_message.h"
30#include "gnunet_chat_util.h" 32#include "gnunet_chat_util.h"
31 33
@@ -67,79 +69,70 @@ notify_handle_fs_progress(void* cls, const struct GNUNET_FS_ProgressInfo* info)
67 69
68 switch (info->status) { 70 switch (info->status) {
69 case GNUNET_FS_STATUS_PUBLISH_START: { 71 case GNUNET_FS_STATUS_PUBLISH_START: {
70 /*publication_t* publication = (publication_t*) info->value.publish.cctx; 72 struct GNUNET_CHAT_File *file = info->value.publish.cctx;
71 publication->progress = 0.0f;
72 73
73 GNUNET_SCHEDULER_add_now(&CGTK_publication_progress, publication); 74 file->published = 0;
74 75
75 return publication;*/ 76 return file;
76 break;
77 } case GNUNET_FS_STATUS_PUBLISH_PROGRESS: { 77 } case GNUNET_FS_STATUS_PUBLISH_PROGRESS: {
78 /*publication_t* publication = (publication_t*) info->value.publish.cctx; 78 struct GNUNET_CHAT_File *file = info->value.publish.cctx;
79 publication->progress = 1.0f * info->value.publish.completed / info->value.publish.size;
80 79
81 GNUNET_SCHEDULER_add_now(&CGTK_publication_progress, publication); 80 file->published = info->value.publish.completed;
82 81
83 return publication;*/ 82 return file;
84 break;
85 } case GNUNET_FS_STATUS_PUBLISH_COMPLETED: { 83 } case GNUNET_FS_STATUS_PUBLISH_COMPLETED: {
86 /*publication_t* publication = (publication_t*) info->value.publish.cctx; 84 struct GNUNET_CHAT_File *file = info->value.publish.cctx;
87 publication->uri = GNUNET_FS_uri_dup(info->value.publish.specifics.completed.chk_uri); 85
88 publication->progress = 1.0f; 86 file->uri = GNUNET_FS_uri_dup(
87 info->value.publish.specifics.completed.chk_uri
88 );
89 89
90 GNUNET_SCHEDULER_add_now(&CGTK_publication_finish, publication);*/ 90 file->published = info->value.publish.size;
91 file->publish = NULL;
91 break; 92 break;
92 } case GNUNET_FS_STATUS_PUBLISH_ERROR: { 93 } case GNUNET_FS_STATUS_PUBLISH_ERROR: {
93 /*publication_t* publication = (publication_t*) info->value.publish.cctx;
94
95 GNUNET_SCHEDULER_add_now(&CGTK_publication_error, publication);*/
96 break; 94 break;
97 } case GNUNET_FS_STATUS_DOWNLOAD_START: { 95 } case GNUNET_FS_STATUS_DOWNLOAD_START: {
98 /*request_t* request = (request_t*) info->value.download.cctx; 96 struct GNUNET_CHAT_File *file = info->value.download.cctx;
99 request->progress = 0.0f;
100 97
101 return request;*/ 98 file->downloaded = 0;
102 break; 99
100 return file;
103 } case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE: { 101 } case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE: {
104 return info->value.download.cctx; 102 return info->value.download.cctx;
105 } case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE: { 103 } case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE: {
106 return info->value.download.cctx; 104 return info->value.download.cctx;
107 } case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS: { 105 } case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS: {
108 /*request_t* request = (request_t*) info->value.download.cctx; 106 struct GNUNET_CHAT_File *file = info->value.download.cctx;
109 request->progress = 1.0f * info->value.download.completed / info->value.download.size;
110 107
111 GNUNET_SCHEDULER_add_now(&CGTK_request_progress, request); 108 file->downloaded = info->value.download.completed;
112 109
113 return request;*/ 110 return file;
114 break;
115 } case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED: { 111 } case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED: {
116 /*request_t* request = (request_t*) info->value.download.cctx; 112 struct GNUNET_CHAT_File *file = info->value.download.cctx;
117 request->progress = 1.0f;
118 113
119 GNUNET_SCHEDULER_add_now(&CGTK_request_finish, request);*/ 114 file->downloaded = info->value.download.size;
115 file->download = NULL;
120 break; 116 break;
121 } case GNUNET_FS_STATUS_DOWNLOAD_ERROR: { 117 } case GNUNET_FS_STATUS_DOWNLOAD_ERROR: {
122 /*request_t *request = (request_t *) info->value.download.cctx;
123
124 GNUNET_SCHEDULER_add_now(&CGTK_request_error, request);*/
125 break; 118 break;
126 } case GNUNET_FS_STATUS_UNINDEX_START: { 119 } case GNUNET_FS_STATUS_UNINDEX_START: {
127 /*publication_t* publication = (publication_t*) info->value.unindex.cctx; 120 struct GNUNET_CHAT_File *file = info->value.unindex.cctx;
128 publication->progress = 0.0f;
129 121
130 return publication;*/ 122 file->unindexed = 0;
131 break; 123
124 return file;
132 } case GNUNET_FS_STATUS_UNINDEX_PROGRESS: { 125 } case GNUNET_FS_STATUS_UNINDEX_PROGRESS: {
133 /*publication_t* publication = (publication_t*) info->value.unindex.cctx; 126 struct GNUNET_CHAT_File *file = info->value.unindex.cctx;
134 publication->progress = 1.0f * info->value.unindex.completed / info->value.unindex.size;
135 127
136 return publication;*/ 128 file->unindexed = info->value.unindex.completed;
137 break; 129
130 return file;
138 } case GNUNET_FS_STATUS_UNINDEX_COMPLETED: { 131 } case GNUNET_FS_STATUS_UNINDEX_COMPLETED: {
139 /*publication_t* publication = (publication_t*) info->value.unindex.cctx; 132 struct GNUNET_CHAT_File *file = info->value.unindex.cctx;
140 publication->progress = 1.0f;
141 133
142 GNUNET_SCHEDULER_add_now(&CGTK_publication_unindex_finish, publication);*/ 134 file->unindexed = info->value.unindex.size;
135 file->unindex = NULL;
143 break; 136 break;
144 } default: { 137 } default: {
145 break; 138 break;
@@ -290,10 +283,45 @@ on_handle_message (void *cls,
290 ); 283 );
291 284
292 if (message) 285 if (message)
293 goto process_callback; 286 return;
294 287
295 message = message_create_from_msg(context, hash, msg); 288 message = message_create_from_msg(context, hash, msg);
296 289
290 switch (msg->header.kind)
291 {
292 case GNUNET_MESSENGER_KIND_INVITE:
293 {
294 struct GNUNET_CHAT_Invitation *invitation = invitation_create_from_message(
295 context, &(msg->body.invite)
296 );
297
298 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
299 context->invites, hash, invitation,
300 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
301 invitation_destroy(invitation);
302 break;
303 }
304 case GNUNET_MESSENGER_KIND_FILE:
305 {
306 GNUNET_CONTAINER_multihashmap_put(
307 context->files, hash, NULL,
308 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
309 );
310
311 struct GNUNET_CHAT_File *file = file_create_from_message(
312 context->handle, &(msg->body.file)
313 );
314
315 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
316 context->handle->files, &(file->hash), file,
317 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
318 file_destroy(file);
319 break;
320 }
321 default:
322 break;
323 }
324
297 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put( 325 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
298 context->messages, hash, message, 326 context->messages, hash, message,
299 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) 327 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
@@ -302,7 +330,46 @@ on_handle_message (void *cls,
302 return; 330 return;
303 } 331 }
304 332
305process_callback:
306 if (handle->msg_cb) 333 if (handle->msg_cb)
307 handle->msg_cb(handle->msg_cls, context, message); 334 handle->msg_cb(handle->msg_cls, context, message);
308} 335}
336
337int
338it_destroy_handle_groups (GNUNET_UNUSED void *cls,
339 GNUNET_UNUSED const struct GNUNET_HashCode *key,
340 void *value)
341{
342 struct GNUNET_CHAT_Group *group = value;
343 group_destroy(group);
344 return GNUNET_YES;
345}
346
347int
348it_destroy_handle_contacts (GNUNET_UNUSED void *cls,
349 GNUNET_UNUSED const struct GNUNET_ShortHashCode *key,
350 void *value)
351{
352 struct GNUNET_CHAT_Contact *contact = value;
353 contact_destroy(contact);
354 return GNUNET_YES;
355}
356
357int
358it_destroy_handle_contexts (GNUNET_UNUSED void *cls,
359 GNUNET_UNUSED const struct GNUNET_HashCode *key,
360 void *value)
361{
362 struct GNUNET_CHAT_Context *context = value;
363 context_destroy(context);
364 return GNUNET_YES;
365}
366
367int
368it_destroy_handle_files (GNUNET_UNUSED void *cls,
369 GNUNET_UNUSED const struct GNUNET_HashCode *key,
370 void *value)
371{
372 struct GNUNET_CHAT_File *file = value;
373 file_destroy(file);
374 return GNUNET_YES;
375}
diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c
index 23c541d..044a1be 100644
--- a/src/gnunet_chat_lib.c
+++ b/src/gnunet_chat_lib.c
@@ -40,6 +40,7 @@
40 40
41struct GNUNET_CHAT_Handle* 41struct GNUNET_CHAT_Handle*
42GNUNET_CHAT_start (const struct GNUNET_CONFIGURATION_Handle *cfg, 42GNUNET_CHAT_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
43 const char *directory,
43 const char *name, 44 const char *name,
44 GNUNET_CHAT_WarningCallback warn_cb, void *warn_cls, 45 GNUNET_CHAT_WarningCallback warn_cb, void *warn_cls,
45 GNUNET_CHAT_ContextMessageCallback msg_cb, void *msg_cls) 46 GNUNET_CHAT_ContextMessageCallback msg_cb, void *msg_cls)
@@ -47,11 +48,10 @@ GNUNET_CHAT_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
47 if (!cfg) 48 if (!cfg)
48 return NULL; 49 return NULL;
49 50
50 if (!name)
51 return NULL;
52
53 return handle_create_from_config( 51 return handle_create_from_config(
54 cfg, name, msg_cb, msg_cls, warn_cb, warn_cls 52 cfg, directory, name,
53 msg_cb, msg_cls,
54 warn_cb, warn_cls
55 ); 55 );
56} 56}
57 57
@@ -332,6 +332,7 @@ GNUNET_CHAT_group_invite_contact (struct GNUNET_CHAT_Group *group,
332 GNUNET_MESSENGER_send_message(contact->context->room, &msg, contact->member); 332 GNUNET_MESSENGER_send_message(contact->context->room, &msg, contact->member);
333} 333}
334 334
335
335int 336int
336GNUNET_CHAT_group_iterate_contacts (struct GNUNET_CHAT_Group *group, 337GNUNET_CHAT_group_iterate_contacts (struct GNUNET_CHAT_Group *group,
337 GNUNET_CHAT_GroupContactCallback callback, 338 GNUNET_CHAT_GroupContactCallback callback,
@@ -360,12 +361,13 @@ GNUNET_CHAT_group_get_context (struct GNUNET_CHAT_Group *group)
360 return group->context; 361 return group->context;
361} 362}
362 363
363void 364
365int
364GNUNET_CHAT_context_send_text (struct GNUNET_CHAT_Context *context, 366GNUNET_CHAT_context_send_text (struct GNUNET_CHAT_Context *context,
365 const char *text) 367 const char *text)
366{ 368{
367 if ((!context) || (!text)) 369 if ((!context) || (!text))
368 return; 370 return GNUNET_SYSERR;
369 371
370 struct GNUNET_MESSENGER_Message msg; 372 struct GNUNET_MESSENGER_Message msg;
371 msg.header.kind = GNUNET_MESSENGER_KIND_TEXT; 373 msg.header.kind = GNUNET_MESSENGER_KIND_TEXT;
@@ -374,42 +376,109 @@ GNUNET_CHAT_context_send_text (struct GNUNET_CHAT_Context *context,
374 GNUNET_MESSENGER_send_message(context->room, &msg, NULL); 376 GNUNET_MESSENGER_send_message(context->room, &msg, NULL);
375 377
376 GNUNET_free(msg.body.text.text); 378 GNUNET_free(msg.body.text.text);
379 return GNUNET_OK;
377} 380}
378 381
379 382
380void 383int
381GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, 384GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context,
382 const char *path) 385 const char *path)
383{ 386{
384 if ((!context) || (!path)) 387 if ((!context) || (!path))
385 return; 388 return GNUNET_SYSERR;
389
390 if (!(context->handle->directory))
391 return GNUNET_SYSERR;
392
393 struct GNUNET_HashCode hash;
394 if (GNUNET_OK != util_hash_file(path, &hash))
395 return GNUNET_SYSERR;
396
397 char *filename;
398 util_get_filename (context->handle->directory, &hash, &filename);
399
400 if ((GNUNET_OK != GNUNET_DISK_directory_create_for_file(filename)) ||
401 (GNUNET_OK != GNUNET_DISK_file_copy(path, filename)))
402 {
403 GNUNET_free(filename);
404 return GNUNET_SYSERR;
405 }
406
407 struct GNUNET_CRYPTO_SymmetricSessionKey key;
408 GNUNET_CRYPTO_symmetric_create_session_key(&key);
409
410 if (GNUNET_OK != util_encrypt_file(filename, &key))
411 {
412 GNUNET_free(filename);
413 return GNUNET_SYSERR;
414 }
415
416 char* p = GNUNET_strdup(path);
386 417
387 // TODO: encrypt file, publish file, share file 418 struct GNUNET_CHAT_File *file = file_create_from_disk(
419 context->handle, basename(p), &hash, &key
420 );
421
422 GNUNET_free(p);
423
424 struct GNUNET_FS_BlockOptions bo;
425
426 bo.anonymity_level = 1;
427 bo.content_priority = 100;
428 bo.replication_level = 1;
429
430 bo.expiration_time = GNUNET_TIME_absolute_add(
431 GNUNET_TIME_absolute_get(), GNUNET_TIME_relative_get_hour_()
432 );
433
434 struct GNUNET_FS_FileInformation* fi = GNUNET_FS_file_information_create_from_file(
435 context->handle->fs,
436 file,
437 filename,
438 NULL,
439 file->meta,
440 GNUNET_YES,
441 &bo
442 );
443
444 file->publish = GNUNET_FS_publish_start(
445 context->handle->fs, fi,
446 NULL, NULL, NULL,
447 GNUNET_FS_PUBLISH_OPTION_NONE
448 );
449
450 GNUNET_free(filename);
451
452 // TODO: share file
453
454 return GNUNET_OK;
388} 455}
389 456
390 457
391void 458int
392GNUNET_CHAT_context_send_uri (struct GNUNET_CHAT_Context *context, 459GNUNET_CHAT_context_send_uri (struct GNUNET_CHAT_Context *context,
393 const char *uri) 460 const char *uri)
394{ 461{
395 if ((!context) || (!uri)) 462 if ((!context) || (!uri))
396 return; 463 return GNUNET_SYSERR;
397 464
398 struct GNUNET_FS_Uri *furi = GNUNET_FS_uri_parse(uri, NULL); 465 struct GNUNET_FS_Uri *furi = GNUNET_FS_uri_parse(uri, NULL);
399 466
400 if (!furi) 467 if (!furi)
401 return; 468 return GNUNET_SYSERR;
402 469
403 // TODO: download file, hash file, share file 470 // TODO: download file, hash file, share file
471
472 return GNUNET_SYSERR;
404} 473}
405 474
406 475
407void 476int
408GNUNET_CHAT_context_share_file (struct GNUNET_CHAT_Context *context, 477GNUNET_CHAT_context_share_file (struct GNUNET_CHAT_Context *context,
409 const struct GNUNET_CHAT_File *file) 478 const struct GNUNET_CHAT_File *file)
410{ 479{
411 if ((!context) || (!file) || (strlen(file->name) > NAME_MAX)) 480 if ((!context) || (!file) || (strlen(file->name) > NAME_MAX))
412 return; 481 return GNUNET_SYSERR;
413 482
414 struct GNUNET_MESSENGER_Message msg; 483 struct GNUNET_MESSENGER_Message msg;
415 msg.header.kind = GNUNET_MESSENGER_KIND_FILE; 484 msg.header.kind = GNUNET_MESSENGER_KIND_FILE;
@@ -421,26 +490,10 @@ GNUNET_CHAT_context_share_file (struct GNUNET_CHAT_Context *context,
421 GNUNET_MESSENGER_send_message(context->room, &msg, NULL); 490 GNUNET_MESSENGER_send_message(context->room, &msg, NULL);
422 491
423 GNUNET_free(msg.body.file.uri); 492 GNUNET_free(msg.body.file.uri);
493 return GNUNET_OK;
424} 494}
425 495
426 496
427void
428GNUNET_CHAT_context_delete_message (const struct GNUNET_CHAT_Message *message,
429 struct GNUNET_TIME_Relative delay)
430{
431 if (!message)
432 return;
433
434 struct GNUNET_MESSENGER_Message msg;
435 msg.header.kind = GNUNET_MESSENGER_KIND_DELETE;
436 GNUNET_memcpy(&(msg.body.delete.hash), &(message->hash), sizeof(message->hash));
437 msg.body.delete.delay = GNUNET_TIME_relative_hton(delay);
438
439 GNUNET_MESSENGER_send_message(message->context->room, &msg, NULL);
440}
441
442
443
444int 497int
445GNUNET_CHAT_context_iterate_messages (struct GNUNET_CHAT_Context *context, 498GNUNET_CHAT_context_iterate_messages (struct GNUNET_CHAT_Context *context,
446 GNUNET_CHAT_ContextMessageCallback callback, 499 GNUNET_CHAT_ContextMessageCallback callback,
@@ -571,6 +624,23 @@ GNUNET_CHAT_message_get_invitation (const struct GNUNET_CHAT_Message *message)
571} 624}
572 625
573 626
627int
628GNUNET_CHAT_message_delete (const struct GNUNET_CHAT_Message *message,
629 struct GNUNET_TIME_Relative delay)
630{
631 if (!message)
632 return GNUNET_SYSERR;
633
634 struct GNUNET_MESSENGER_Message msg;
635 msg.header.kind = GNUNET_MESSENGER_KIND_DELETE;
636 GNUNET_memcpy(&(msg.body.delete.hash), &(message->hash), sizeof(message->hash));
637 msg.body.delete.delay = GNUNET_TIME_relative_hton(delay);
638
639 GNUNET_MESSENGER_send_message(message->context->room, &msg, NULL);
640 return GNUNET_OK;
641}
642
643
574const struct GNUNET_HashCode* 644const struct GNUNET_HashCode*
575GNUNET_CHAT_file_get_hash (const struct GNUNET_CHAT_File *file) 645GNUNET_CHAT_file_get_hash (const struct GNUNET_CHAT_File *file)
576{ 646{
@@ -590,11 +660,15 @@ GNUNET_CHAT_file_get_size (const struct GNUNET_CHAT_File *file)
590 if (file->uri) 660 if (file->uri)
591 return GNUNET_FS_uri_chk_get_file_size(file->uri); 661 return GNUNET_FS_uri_chk_get_file_size(file->uri);
592 662
593 const char *path = ""; // TODO: path = download_directory + file->name 663 char *filename;
664 util_get_filename (file->handle->directory, &(file->hash), &filename);
594 665
595 // TODO: check size through info or check locally? 666 uint64_t size;
667 if (GNUNET_OK != GNUNET_DISK_file_size(filename, &size, GNUNET_NO, GNUNET_YES))
668 size = 0;
596 669
597 return 0; 670 GNUNET_free(filename);
671 return size;
598} 672}
599 673
600 674
@@ -604,11 +678,13 @@ GNUNET_CHAT_file_is_local (const struct GNUNET_CHAT_File *file)
604 if (!file) 678 if (!file)
605 return GNUNET_SYSERR; 679 return GNUNET_SYSERR;
606 680
607 const char *path = ""; // TODO: path = download_directory + file->name 681 char *filename;
682 util_get_filename (file->handle->directory, &(file->hash), &filename);
608 683
609 // TODO: check locally? 684 int result = GNUNET_DISK_file_test(filename);
610 685
611 return GNUNET_SYSERR; 686 GNUNET_free(filename);
687 return result;
612} 688}
613 689
614 690
@@ -617,27 +693,44 @@ GNUNET_CHAT_file_start_download (struct GNUNET_CHAT_File *file,
617 GNUNET_CHAT_MessageFileDownloadCallback callback, 693 GNUNET_CHAT_MessageFileDownloadCallback callback,
618 void *cls) 694 void *cls)
619{ 695{
620 if (!file) 696 if ((!file) || (!(file->uri)))
621 return GNUNET_SYSERR; 697 return GNUNET_SYSERR;
622 698
623 // TODO: check if downloading? 699 if (file->download)
700 {
701 GNUNET_FS_download_resume(file->download);
702 return GNUNET_OK;
703 }
704
705 const uint64_t size = GNUNET_FS_uri_chk_get_file_size(file->uri);
624 706
625 const char *path = ""; // TODO: path = download_directory + file->name 707 char *filename;
708 util_get_filename (file->handle->directory, &(file->hash), &filename);
709
710 uint64_t offset;
711 if (GNUNET_OK != GNUNET_DISK_file_size(filename, &offset, GNUNET_NO, GNUNET_YES))
712 offset = 0;
713
714 if (offset >= size)
715 return GNUNET_OK;
716
717 const uint64_t remaining = (size - offset);
626 718
627 file->download = GNUNET_FS_download_start( 719 file->download = GNUNET_FS_download_start(
628 file->handle->fs, 720 file->handle->fs,
629 file->uri, 721 file->uri,
722 file->meta,
723 filename,
630 NULL, 724 NULL,
631 path, 725 offset,
632 NULL, 726 remaining,
633 0, 727 1,
634 0,
635 0,
636 GNUNET_FS_DOWNLOAD_OPTION_NONE, 728 GNUNET_FS_DOWNLOAD_OPTION_NONE,
637 NULL, 729 file,
638 NULL 730 NULL
639 ); 731 );
640 732
733 GNUNET_free(filename);
641 return GNUNET_OK; 734 return GNUNET_OK;
642} 735}
643 736
@@ -676,6 +769,34 @@ GNUNET_CHAT_file_stop_download (struct GNUNET_CHAT_File *file)
676} 769}
677 770
678 771
772int
773GNUNET_CHAT_file_unindex (struct GNUNET_CHAT_File *file)
774{
775 if (!file)
776 return GNUNET_SYSERR;
777
778 if (file->publish)
779 {
780 GNUNET_FS_publish_stop(file->publish);
781 file->publish = NULL;
782 return GNUNET_OK;
783 }
784
785 if (file->unindex)
786 return GNUNET_OK;
787
788 char *filename;
789 util_get_filename (file->handle->directory, &(file->hash), &filename);
790
791 file->unindex = GNUNET_FS_unindex_start(
792 file->handle->fs, filename, file
793 );
794
795 GNUNET_free(filename);
796 return GNUNET_OK;
797}
798
799
679void 800void
680GNUNET_CHAT_invitation_accept (struct GNUNET_CHAT_Invitation *invitation) 801GNUNET_CHAT_invitation_accept (struct GNUNET_CHAT_Invitation *invitation)
681{ 802{
diff --git a/src/gnunet_chat_util.c b/src/gnunet_chat_util.c
index 1a75ba9..cdbd46c 100644
--- a/src/gnunet_chat_util.c
+++ b/src/gnunet_chat_util.c
@@ -26,14 +26,14 @@
26 26
27void 27void
28util_shorthash_from_member (const struct GNUNET_MESSENGER_Contact *member, 28util_shorthash_from_member (const struct GNUNET_MESSENGER_Contact *member,
29 struct GNUNET_ShortHashCode* shorthash) 29 struct GNUNET_ShortHashCode *shorthash)
30{ 30{
31 memset(shorthash, 0, sizeof(*shorthash)); 31 memset(shorthash, 0, sizeof(*shorthash));
32 GNUNET_memcpy(shorthash, &member, sizeof(member)); 32 GNUNET_memcpy(shorthash, &member, sizeof(member));
33} 33}
34 34
35void 35void
36util_set_name_field (const char *name, char** field) 36util_set_name_field (const char *name, char **field)
37{ 37{
38 if (*field) 38 if (*field)
39 GNUNET_free(*field); 39 GNUNET_free(*field);
@@ -43,3 +43,147 @@ util_set_name_field (const char *name, char** field)
43 else 43 else
44 *field = NULL; 44 *field = NULL;
45} 45}
46
47int
48util_hash_file (const char *filename, struct GNUNET_HashCode *hash)
49{
50 uint64_t size;
51
52 if (GNUNET_OK != GNUNET_DISK_file_size(filename, &size, GNUNET_NO, GNUNET_YES))
53 return GNUNET_SYSERR;
54
55 struct GNUNET_DISK_FileHandle *file = GNUNET_DISK_file_open(
56 filename, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ
57 );
58
59 if (!file)
60 return GNUNET_SYSERR;
61
62 struct GNUNET_DISK_MapHandle *mapping;
63 const void* data = GNUNET_DISK_file_map(
64 file, &mapping, GNUNET_DISK_MAP_TYPE_READ, size
65 );
66
67 if (!data)
68 {
69 GNUNET_DISK_file_close(file);
70 return GNUNET_SYSERR;
71 }
72
73 GNUNET_CRYPTO_hash(data, size, hash);
74
75 GNUNET_DISK_file_unmap(mapping);
76 GNUNET_DISK_file_close(file);
77
78 return GNUNET_OK;
79}
80
81int
82util_encrypt_file (const char *filename,
83 const struct GNUNET_CRYPTO_SymmetricSessionKey *key)
84{
85 uint64_t size;
86
87 if (GNUNET_OK != GNUNET_DISK_file_size(filename, &size, GNUNET_NO, GNUNET_YES))
88 return GNUNET_SYSERR;
89
90 struct GNUNET_DISK_FileHandle *file = GNUNET_DISK_file_open(
91 filename, GNUNET_DISK_OPEN_READWRITE,
92 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
93 );
94
95 if (!file)
96 return GNUNET_SYSERR;
97
98 struct GNUNET_DISK_MapHandle *mapping;
99 void* data = GNUNET_DISK_file_map(
100 file, &mapping, GNUNET_DISK_MAP_TYPE_READWRITE, size
101 );
102
103 if (!data)
104 {
105 GNUNET_DISK_file_close(file);
106 return GNUNET_SYSERR;
107 }
108
109 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
110 memset(&iv, 0, sizeof(iv));
111
112 ssize_t result = GNUNET_CRYPTO_symmetric_encrypt(data, size, key, &iv, data);
113
114 if (GNUNET_OK != GNUNET_DISK_file_unmap(mapping))
115 result = -1;
116
117 if (GNUNET_OK != GNUNET_DISK_file_sync(file))
118 result = -1;
119
120 if (GNUNET_OK != GNUNET_DISK_file_close(file))
121 result = -1;
122
123 if (result < 0)
124 return GNUNET_SYSERR;
125
126 return GNUNET_OK;
127}
128
129int
130util_decrypt_file (const char *filename,
131 const struct GNUNET_CRYPTO_SymmetricSessionKey *key)
132{
133 uint64_t size;
134
135 if (GNUNET_OK != GNUNET_DISK_file_size(filename, &size, GNUNET_NO, GNUNET_YES))
136 return GNUNET_SYSERR;
137
138 struct GNUNET_DISK_FileHandle *file = GNUNET_DISK_file_open(
139 filename, GNUNET_DISK_OPEN_READWRITE,
140 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
141 );
142
143 if (!file)
144 return GNUNET_SYSERR;
145
146 struct GNUNET_DISK_MapHandle *mapping;
147 void* data = GNUNET_DISK_file_map(
148 file, &mapping, GNUNET_DISK_MAP_TYPE_READWRITE, size
149 );
150
151 if (!data)
152 {
153 GNUNET_DISK_file_close(file);
154 return GNUNET_SYSERR;
155 }
156
157 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
158 memset(&iv, 0, sizeof(iv));
159
160 ssize_t result = GNUNET_CRYPTO_symmetric_decrypt(data, size, key, &iv, data);
161
162 if (GNUNET_OK != GNUNET_DISK_file_unmap(mapping))
163 result = -1;
164
165 if (GNUNET_OK != GNUNET_DISK_file_sync(file))
166 result = -1;
167
168 if (GNUNET_OK != GNUNET_DISK_file_close(file))
169 result = -1;
170
171 if (result < 0)
172 return GNUNET_SYSERR;
173
174 return GNUNET_OK;
175}
176
177int
178util_get_filename (const char *directory, const struct GNUNET_HashCode *hash,
179 char **filename)
180{
181 return GNUNET_asprintf (
182 filename,
183 "%s%s%c%s",
184 directory,
185 "files",
186 DIR_SEPARATOR,
187 GNUNET_h2s_full(hash)
188 );
189}
diff --git a/src/gnunet_chat_util.h b/src/gnunet_chat_util.h
index d9ff947..1ffa01a 100644
--- a/src/gnunet_chat_util.h
+++ b/src/gnunet_chat_util.h
@@ -27,14 +27,31 @@
27 27
28#include <gnunet/platform.h> 28#include <gnunet/platform.h>
29#include <gnunet/gnunet_common.h> 29#include <gnunet/gnunet_common.h>
30#include <gnunet/gnunet_crypto_lib.h>
31#include <gnunet/gnunet_disk_lib.h>
30#include <gnunet/gnunet_messenger_service.h> 32#include <gnunet/gnunet_messenger_service.h>
31#include <gnunet/gnunet_util_lib.h> 33#include <gnunet/gnunet_util_lib.h>
32 34
33void 35void
34util_shorthash_from_member (const struct GNUNET_MESSENGER_Contact *member, 36util_shorthash_from_member (const struct GNUNET_MESSENGER_Contact *member,
35 struct GNUNET_ShortHashCode* shorthash); 37 struct GNUNET_ShortHashCode *shorthash);
36 38
37void 39void
38util_set_name_field (const char *name, char** field); 40util_set_name_field (const char *name, char **field);
41
42int
43util_hash_file (const char *filename, struct GNUNET_HashCode *hash);
44
45int
46util_encrypt_file (const char *filename,
47 const struct GNUNET_CRYPTO_SymmetricSessionKey *key);
48
49int
50util_decrypt_file (const char *filename,
51 const struct GNUNET_CRYPTO_SymmetricSessionKey *key);
52
53int
54util_get_filename (const char *directory, const struct GNUNET_HashCode *hash,
55 char **filename);
39 56
40#endif /* GNUNET_CHAT_UTIL_H_ */ 57#endif /* GNUNET_CHAT_UTIL_H_ */