commit b655e3a04c2a52d046ad4bec17f3541055f178af
parent ee7a8798fdecb02f422096b360498424f16330b1
Author: Jacki <jacki@thejackimonster.de>
Date: Fri, 27 Sep 2024 18:20:51 +0200
Change discourse id from short hash to own struct
Signed-off-by: Jacki <jacki@thejackimonster.de>
Diffstat:
10 files changed, 95 insertions(+), 31 deletions(-)
diff --git a/include/gnunet/gnunet_chat_lib.h b/include/gnunet/gnunet_chat_lib.h
@@ -204,6 +204,14 @@ enum GNUNET_CHAT_MessageKind
};
/**
+ * Struct to identify discourses
+ */
+struct GNUNET_CHAT_DiscourseId
+{
+ char identifier [32];
+};
+
+/**
* Struct of a chat handle.
*/
struct GNUNET_CHAT_Handle;
@@ -1374,7 +1382,7 @@ GNUNET_CHAT_context_send_tag (struct GNUNET_CHAT_Context *context,
*/
struct GNUNET_CHAT_Discourse*
GNUNET_CHAT_context_open_discourse (struct GNUNET_CHAT_Context *context,
- const struct GNUNET_ShortHashCode *id);
+ const struct GNUNET_CHAT_DiscourseId *id);
/**
* Iterates through the contacts of a given chat <i>context</i> with a selected
@@ -1681,7 +1689,7 @@ GNUNET_CHAT_file_get_name (const struct GNUNET_CHAT_File *file);
* @param[in] file File handle
* @return The hash of file
*/
-const struct GNUNET_HashCode*
+const char*
GNUNET_CHAT_file_get_hash (const struct GNUNET_CHAT_File *file);
/**
@@ -1893,7 +1901,7 @@ GNUNET_CHAT_invitation_is_rejected (const struct GNUNET_CHAT_Invitation *invitat
* @param[in] discourse Chat discourse
* @return Discourse id
*/
-const struct GNUNET_ShortHashCode*
+const struct GNUNET_CHAT_DiscourseId*
GNUNET_CHAT_discourse_get_id (const struct GNUNET_CHAT_Discourse *discourse);
/**
diff --git a/src/gnunet_chat_discourse.c b/src/gnunet_chat_discourse.c
@@ -34,7 +34,7 @@
struct GNUNET_CHAT_Discourse*
discourse_create (struct GNUNET_CHAT_Context *context,
- const struct GNUNET_ShortHashCode *id)
+ const struct GNUNET_CHAT_DiscourseId *id)
{
GNUNET_assert((context) && (id));
@@ -42,7 +42,7 @@ discourse_create (struct GNUNET_CHAT_Context *context,
discourse->context = context;
- GNUNET_memcpy(&(discourse->id), id, sizeof (struct GNUNET_ShortHashCode));
+ GNUNET_memcpy(&(discourse->id), id, sizeof(struct GNUNET_CHAT_DiscourseId));
if (0 != pipe(discourse->pipe))
{
diff --git a/src/gnunet_chat_discourse.h b/src/gnunet_chat_discourse.h
@@ -53,7 +53,7 @@ struct GNUNET_CHAT_Discourse
{
struct GNUNET_CHAT_Context *context;
- struct GNUNET_ShortHashCode id;
+ struct GNUNET_CHAT_DiscourseId id;
int pipe [2];
struct GNUNET_CHAT_DiscourseSubscription *head;
@@ -74,7 +74,7 @@ struct GNUNET_CHAT_Discourse
*/
struct GNUNET_CHAT_Discourse*
discourse_create (struct GNUNET_CHAT_Context *context,
- const struct GNUNET_ShortHashCode *id);
+ const struct GNUNET_CHAT_DiscourseId *id);
/**
* Destroys a chat <i>discourse</i> and frees its memory.
diff --git a/src/gnunet_chat_discourse_intern.c b/src/gnunet_chat_discourse_intern.c
@@ -75,11 +75,10 @@ cb_read_discourse_pipe (void *cls)
memset(&msg, 0, sizeof(msg));
msg.header.kind = GNUNET_MESSENGER_KIND_TALK;
-
- memcpy(
- &(msg.body.talk.discourse),
+
+ util_shorthash_from_discourse_id(
&(discourse->id),
- sizeof(discourse->id)
+ &(msg.body.talk.discourse)
);
char data [MAX_WRITE_SIZE];
diff --git a/src/gnunet_chat_handle_intern.c b/src/gnunet_chat_handle_intern.c
@@ -928,17 +928,20 @@ skip_msg_handing:
}
case GNUNET_MESSENGER_KIND_SUBSCRIBE:
{
- const struct GNUNET_ShortHashCode *id = &(message->msg->body.subscribe.discourse);
+ const struct GNUNET_ShortHashCode *sid = &(message->msg->body.subscribe.discourse);
struct GNUNET_CHAT_Discourse *discourse = GNUNET_CONTAINER_multishortmap_get(
- context->discourses, id
+ context->discourses, sid
);
if (! discourse)
{
- discourse = discourse_create(context, id);
+ struct GNUNET_CHAT_DiscourseId id;
+ util_discourse_id_from_shorthash(sid, &id);
+
+ discourse = discourse_create(context, &id);
if (GNUNET_OK != GNUNET_CONTAINER_multishortmap_put(context->discourses,
- id, discourse, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
+ sid, discourse, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
{
discourse_destroy(discourse);
break;
diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c
@@ -1950,15 +1950,18 @@ GNUNET_CHAT_context_send_tag (struct GNUNET_CHAT_Context *context,
struct GNUNET_CHAT_Discourse*
GNUNET_CHAT_context_open_discourse (struct GNUNET_CHAT_Context *context,
- const struct GNUNET_ShortHashCode *id)
+ const struct GNUNET_CHAT_DiscourseId *id)
{
GNUNET_CHAT_VERSION_ASSERT();
if ((!context) || (!(context->discourses)) || (!(context->room)) || (!id))
return NULL;
+ struct GNUNET_ShortHashCode sid;
+ util_shorthash_from_discourse_id(id, &sid);
+
struct GNUNET_CHAT_Discourse *discourse = GNUNET_CONTAINER_multishortmap_get(
- context->discourses, id
+ context->discourses, &sid
);
if (!discourse)
@@ -1966,7 +1969,7 @@ GNUNET_CHAT_context_open_discourse (struct GNUNET_CHAT_Context *context,
discourse = discourse_create(context, id);
if (GNUNET_OK != GNUNET_CONTAINER_multishortmap_put(context->discourses,
- id, discourse, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
+ &sid, discourse, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
{
discourse_destroy(discourse);
return NULL;
@@ -1977,7 +1980,11 @@ GNUNET_CHAT_context_open_discourse (struct GNUNET_CHAT_Context *context,
memset(&msg, 0, sizeof(msg));
msg.header.kind = GNUNET_MESSENGER_KIND_SUBSCRIBE;
- GNUNET_memcpy(&(msg.body.subscribe.discourse), id, sizeof(struct GNUNET_ShortHashCode));
+ GNUNET_memcpy(
+ &(msg.body.subscribe.discourse),
+ &sid,
+ sizeof(struct GNUNET_ShortHashCode)
+ );
const struct GNUNET_TIME_Relative subscribtion_time = GNUNET_TIME_relative_multiply(
GNUNET_TIME_relative_get_second_(), 10
@@ -2520,7 +2527,7 @@ GNUNET_CHAT_file_get_name (const struct GNUNET_CHAT_File *file)
}
-const struct GNUNET_HashCode*
+const char*
GNUNET_CHAT_file_get_hash (const struct GNUNET_CHAT_File *file)
{
GNUNET_CHAT_VERSION_ASSERT();
@@ -2528,7 +2535,7 @@ GNUNET_CHAT_file_get_hash (const struct GNUNET_CHAT_File *file)
if (!file)
return NULL;
- return &(file->hash);
+ return GNUNET_h2s_full(&(file->hash));
}
@@ -2969,7 +2976,7 @@ GNUNET_CHAT_invitation_is_rejected (const struct GNUNET_CHAT_Invitation *invitat
}
-const struct GNUNET_ShortHashCode*
+const struct GNUNET_CHAT_DiscourseId*
GNUNET_CHAT_discourse_get_id (const struct GNUNET_CHAT_Discourse *discourse)
{
GNUNET_CHAT_VERSION_ASSERT();
@@ -3041,10 +3048,9 @@ GNUNET_CHAT_discourse_close (struct GNUNET_CHAT_Discourse *discourse)
msg.header.kind = GNUNET_MESSENGER_KIND_SUBSCRIBE;
- GNUNET_memcpy(
- &(msg.body.subscribe.discourse),
+ util_shorthash_from_discourse_id(
&(discourse->id),
- sizeof(struct GNUNET_ShortHashCode)
+ &(msg.body.subscribe.discourse)
);
msg.body.subscribe.time = GNUNET_TIME_relative_hton(GNUNET_TIME_relative_get_zero_());
@@ -3080,10 +3086,9 @@ GNUNET_CHAT_discourse_write (struct GNUNET_CHAT_Discourse *discourse,
msg.header.kind = GNUNET_MESSENGER_KIND_TALK;
msg.body.talk.data = GNUNET_malloc(size > max_size? max_size : size);
- GNUNET_memcpy(
- &(msg.body.talk.discourse),
+ util_shorthash_from_discourse_id(
&(discourse->id),
- sizeof(struct GNUNET_ShortHashCode)
+ &(msg.body.talk.discourse)
);
while (size > 0)
diff --git a/src/gnunet_chat_util.c b/src/gnunet_chat_util.c
@@ -49,6 +49,34 @@ util_shorthash_from_member (const struct GNUNET_MESSENGER_Contact *member,
}
void
+util_shorthash_from_discourse_id (const struct GNUNET_CHAT_DiscourseId *id,
+ struct GNUNET_ShortHashCode *shorthash)
+{
+ GNUNET_assert(shorthash);
+
+ memset(shorthash, 0, sizeof(*shorthash));
+ GNUNET_memcpy(
+ shorthash,
+ id,
+ sizeof(*id) < sizeof(*shorthash) ? sizeof(*id) : sizeof(*shorthash)
+ );
+}
+
+void
+util_discourse_id_from_shorthash (const struct GNUNET_ShortHashCode *shorthash,
+ struct GNUNET_CHAT_DiscourseId *id)
+{
+ GNUNET_assert(id);
+
+ memset(id, 0, sizeof(*id));
+ GNUNET_memcpy(
+ id,
+ shorthash,
+ sizeof(*id) < sizeof(*shorthash) ? sizeof(*id) : sizeof(*shorthash)
+ );
+}
+
+void
util_set_name_field (const char *name,
char **field)
{
diff --git a/src/gnunet_chat_util.h b/src/gnunet_chat_util.h
@@ -61,7 +61,28 @@ enum GNUNET_CHAT_ContextType
*/
void
util_shorthash_from_member (const struct GNUNET_MESSENGER_Contact *member,
- struct GNUNET_ShortHashCode *shorthash);
+ struct GNUNET_ShortHashCode *shorthash);
+
+/**
+ * Converts a discourse id into a short hash variant for map access
+ * as key.
+ *
+ * @param[in] id Discourse id
+ * @param[out] shorthash Short hash
+ */
+void
+util_shorthash_from_discourse_id (const struct GNUNET_CHAT_DiscourseId *id,
+ struct GNUNET_ShortHashCode *shorthash);
+
+/**
+ * Converts a short hash variant into a discourse id.
+ *
+ * @param[in] shorthash Short hash
+ * @param[out] id Discourse id
+ */
+void
+util_discourse_id_from_shorthash (const struct GNUNET_ShortHashCode *shorthash,
+ struct GNUNET_CHAT_DiscourseId *id);
/**
* Updates the stored content of a <i>field</i> with
diff --git a/tests/discourse/test_gnunet_chat_discourse_open.c b/tests/discourse/test_gnunet_chat_discourse_open.c
@@ -44,7 +44,7 @@ on_gnunet_chat_discourse_open_msg(void *cls,
account = GNUNET_CHAT_message_get_account(message);
const char *name = GNUNET_CHAT_get_name(handle);
- struct GNUNET_ShortHashCode discourse_id;
+ struct GNUNET_CHAT_DiscourseId discourse_id;
struct GNUNET_CHAT_Discourse *discourse;
discourse = GNUNET_CHAT_message_get_discourse(message);
diff --git a/tests/discourse/test_gnunet_chat_discourse_write.c b/tests/discourse/test_gnunet_chat_discourse_write.c
@@ -44,7 +44,7 @@ on_gnunet_chat_discourse_write_msg(void *cls,
account = GNUNET_CHAT_message_get_account(message);
const char *name = GNUNET_CHAT_get_name(handle);
- struct GNUNET_ShortHashCode discourse_id;
+ struct GNUNET_CHAT_DiscourseId discourse_id;
struct GNUNET_CHAT_Discourse *discourse;
discourse = GNUNET_CHAT_message_get_discourse(message);