aboutsummaryrefslogtreecommitdiff
path: root/src/messenger
diff options
context:
space:
mode:
Diffstat (limited to 'src/messenger')
-rw-r--r--src/messenger/Makefile.am13
-rw-r--r--src/messenger/gnunet-service-messenger.c8
-rw-r--r--src/messenger/gnunet-service-messenger_tunnel.c8
-rw-r--r--src/messenger/messenger.conf.in1
-rw-r--r--src/messenger/messenger_api.c7
-rw-r--r--src/messenger/messenger_api_message.c8
-rw-r--r--src/messenger/messenger_api_message.h8
-rw-r--r--src/messenger/plugin_gnsrecord_messenger.c243
8 files changed, 289 insertions, 7 deletions
diff --git a/src/messenger/Makefile.am b/src/messenger/Makefile.am
index 3fc532e7e..4be11f3aa 100644
--- a/src/messenger/Makefile.am
+++ b/src/messenger/Makefile.am
@@ -10,6 +10,19 @@ pkgcfgdir= $(pkgdatadir)/config.d/
10 10
11libexecdir= $(pkglibdir)/libexec/ 11libexecdir= $(pkglibdir)/libexec/
12 12
13plugin_LTLIBRARIES = \
14 libgnunet_plugin_gnsrecord_messenger.la
15
16
17libgnunet_plugin_gnsrecord_messenger_la_SOURCES = \
18 plugin_gnsrecord_messenger.c
19libgnunet_plugin_gnsrecord_messenger_la_LIBADD = \
20 $(top_builddir)/src/util/libgnunetutil.la \
21 $(LTLIBINTL)
22libgnunet_plugin_gnsrecord_messenger_la_LDFLAGS = \
23 $(GN_PLUGIN_LDFLAGS)
24
25
13pkgcfg_DATA = \ 26pkgcfg_DATA = \
14 messenger.conf 27 messenger.conf
15 28
diff --git a/src/messenger/gnunet-service-messenger.c b/src/messenger/gnunet-service-messenger.c
index 989157ceb..a994e16ee 100644
--- a/src/messenger/gnunet-service-messenger.c
+++ b/src/messenger/gnunet-service-messenger.c
@@ -227,10 +227,10 @@ check_for_message:
227 if (GNUNET_YES != decode_message (&message, msg_length, msg_buffer, GNUNET_NO, NULL)) 227 if (GNUNET_YES != decode_message (&message, msg_length, msg_buffer, GNUNET_NO, NULL))
228 return GNUNET_NO; 228 return GNUNET_NO;
229 229
230 if (GNUNET_YES != filter_message_sending(&message)) 230 const int allowed = filter_message_sending(&message);
231 return GNUNET_NO;
232 231
233 return GNUNET_OK; 232 cleanup_message(&message);
233 return GNUNET_YES == allowed? GNUNET_OK : GNUNET_NO;
234} 234}
235 235
236static void 236static void
@@ -278,6 +278,8 @@ handle_send_message (void *cls,
278 GNUNET_MESSENGER_name_of_kind (message.header.kind), GNUNET_h2s (key)); 278 GNUNET_MESSENGER_name_of_kind (message.header.kind), GNUNET_h2s (key));
279 279
280end_handling: 280end_handling:
281 cleanup_message(&message);
282
281 GNUNET_SERVICE_client_continue (msg_client->client); 283 GNUNET_SERVICE_client_continue (msg_client->client);
282} 284}
283 285
diff --git a/src/messenger/gnunet-service-messenger_tunnel.c b/src/messenger/gnunet-service-messenger_tunnel.c
index fdd6429e6..c21f298b3 100644
--- a/src/messenger/gnunet-service-messenger_tunnel.c
+++ b/src/messenger/gnunet-service-messenger_tunnel.c
@@ -176,6 +176,9 @@ handle_tunnel_message (void *cls, const struct GNUNET_MessageHeader *header)
176{ 176{
177 struct GNUNET_MESSENGER_SrvTunnel *tunnel = cls; 177 struct GNUNET_MESSENGER_SrvTunnel *tunnel = cls;
178 178
179 if (!tunnel)
180 return;
181
179 const uint16_t length = ntohs (header->size) - sizeof(*header); 182 const uint16_t length = ntohs (header->size) - sizeof(*header);
180 const char *buffer = (const char*) &header[1]; 183 const char *buffer = (const char*) &header[1];
181 184
@@ -190,9 +193,6 @@ handle_tunnel_message (void *cls, const struct GNUNET_MessageHeader *header)
190 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got message of kind: %s!\n", 193 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got message of kind: %s!\n",
191 GNUNET_MESSENGER_name_of_kind(message.header.kind)); 194 GNUNET_MESSENGER_name_of_kind(message.header.kind));
192 195
193 if (!tunnel)
194 return;
195
196 const int new_message = update_room_message ( 196 const int new_message = update_room_message (
197 tunnel->room, copy_message (&message), &hash 197 tunnel->room, copy_message (&message), &hash
198 ); 198 );
@@ -226,6 +226,8 @@ handle_tunnel_message (void *cls, const struct GNUNET_MessageHeader *header)
226 } 226 }
227 227
228receive_done: 228receive_done:
229 cleanup_message(&message);
230
229 GNUNET_CADET_receive_done (tunnel->channel); 231 GNUNET_CADET_receive_done (tunnel->channel);
230} 232}
231 233
diff --git a/src/messenger/messenger.conf.in b/src/messenger/messenger.conf.in
index 59e11b166..6b54550ea 100644
--- a/src/messenger/messenger.conf.in
+++ b/src/messenger/messenger.conf.in
@@ -1,5 +1,6 @@
1[messenger] 1[messenger]
2START_ON_DEMAND = YES 2START_ON_DEMAND = YES
3RUN_PER_USER = YES
3PORT = 2097 4PORT = 2097
4HOSTNAME = localhost 5HOSTNAME = localhost
5BINARY = gnunet-service-messenger 6BINARY = gnunet-service-messenger
diff --git a/src/messenger/messenger_api.c b/src/messenger/messenger_api.c
index ef6244e19..81a02e858 100644
--- a/src/messenger/messenger_api.c
+++ b/src/messenger/messenger_api.c
@@ -214,6 +214,7 @@ check_recv_message (void *cls,
214 if (GNUNET_YES != decode_message (&message, length, buffer, GNUNET_YES, NULL)) 214 if (GNUNET_YES != decode_message (&message, length, buffer, GNUNET_YES, NULL))
215 return GNUNET_NO; 215 return GNUNET_NO;
216 216
217 cleanup_message(&message);
217 return GNUNET_OK; 218 return GNUNET_OK;
218} 219}
219 220
@@ -254,11 +255,15 @@ handle_recv_message (void *cls,
254 255
255 handle_room_message (room, contact, &message, hash); 256 handle_room_message (room, contact, &message, hash);
256 257
258 const struct GNUNET_MESSENGER_Message *stored_message = get_room_message(room, hash);
259
257 if (handle->msg_callback) 260 if (handle->msg_callback)
258 handle->msg_callback (handle->msg_cls, room, contact, &message, hash, flags); 261 handle->msg_callback (handle->msg_cls, room, contact, stored_message, hash, flags);
259 } 262 }
260 else 263 else
261 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Room not found\n"); 264 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Room not found\n");
265
266 cleanup_message(&message);
262} 267}
263 268
264static void 269static void
diff --git a/src/messenger/messenger_api_message.c b/src/messenger/messenger_api_message.c
index 05bf654fb..4e64cc22f 100644
--- a/src/messenger/messenger_api_message.c
+++ b/src/messenger/messenger_api_message.c
@@ -126,6 +126,14 @@ destroy_message_body (enum GNUNET_MESSENGER_MessageKind kind,
126} 126}
127 127
128void 128void
129cleanup_message (struct GNUNET_MESSENGER_Message *message)
130{
131 GNUNET_assert(message);
132
133 destroy_message_body (message->header.kind, &(message->body));
134}
135
136void
129destroy_message (struct GNUNET_MESSENGER_Message *message) 137destroy_message (struct GNUNET_MESSENGER_Message *message)
130{ 138{
131 GNUNET_assert(message); 139 GNUNET_assert(message);
diff --git a/src/messenger/messenger_api_message.h b/src/messenger/messenger_api_message.h
index 6aebf4014..21161c134 100644
--- a/src/messenger/messenger_api_message.h
+++ b/src/messenger/messenger_api_message.h
@@ -62,6 +62,14 @@ struct GNUNET_MESSENGER_Message*
62copy_message (const struct GNUNET_MESSENGER_Message *message); 62copy_message (const struct GNUNET_MESSENGER_Message *message);
63 63
64/** 64/**
65 * Frees the messages body memory.
66 *
67 * @param[in/out] message Message
68 */
69void
70cleanup_message (struct GNUNET_MESSENGER_Message *message);
71
72/**
65 * Destroys a message and frees its memory fully. 73 * Destroys a message and frees its memory fully.
66 * 74 *
67 * @param[in/out] message Message 75 * @param[in/out] message Message
diff --git a/src/messenger/plugin_gnsrecord_messenger.c b/src/messenger/plugin_gnsrecord_messenger.c
new file mode 100644
index 000000000..2219f0bde
--- /dev/null
+++ b/src/messenger/plugin_gnsrecord_messenger.c
@@ -0,0 +1,243 @@
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 src/messenger/plugin_gnsrecord_messenger.c
23 * @brief Plugin to provide the API for useful GNS records to improve
24 * the usability of the messenger service.
25 */
26
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_gnsrecord_lib.h"
30#include "gnunet_messenger_service.h"
31#include "gnunet_gnsrecord_plugin.h"
32
33
34/**
35 * Convert the 'value' of a record to a string.
36 *
37 * @param cls closure, unused
38 * @param type type of the record
39 * @param data value in binary encoding
40 * @param data_size number of bytes in @a data
41 * @return NULL on error, otherwise human-readable representation of the value
42 */
43static char *
44messenger_value_to_string (void *cls,
45 uint32_t type,
46 const void *data,
47 size_t data_size)
48{
49 (void) cls;
50 switch (type)
51 {
52 case GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_ENTRY:
53 {
54 if (data_size != sizeof(struct GNUNET_MESSENGER_RoomEntryRecord))
55 {
56 GNUNET_break_op (0);
57 return NULL;
58 }
59
60 const struct GNUNET_MESSENGER_RoomEntryRecord *record = data;
61
62 char *door = GNUNET_CRYPTO_eddsa_public_key_to_string (&(record->door.public_key));
63 char *key = GNUNET_STRINGS_data_to_string_alloc (&(record->key), sizeof(struct GNUNET_HashCode));
64
65 char *ret;
66 GNUNET_asprintf (&ret, "%s-%s", door, key);
67 GNUNET_free (key);
68 GNUNET_free (door);
69 return ret;
70 }
71
72 default:
73 return NULL;
74 }
75}
76
77
78/**
79 * Convert human-readable version of a 'value' of a record to the binary
80 * representation.
81 *
82 * @param cls closure, unused
83 * @param type type of the record
84 * @param s human-readable string
85 * @param data set to value in binary encoding (will be allocated)
86 * @param data_size set to number of bytes in @a data
87 * @return #GNUNET_OK on success
88 */
89static int
90messenger_string_to_value (void *cls,
91 uint32_t type,
92 const char *s,
93 void **data,
94 size_t *data_size)
95{
96 (void) cls;
97 if (NULL == s)
98 {
99 GNUNET_break (0);
100 return GNUNET_SYSERR;
101 }
102
103 switch (type)
104 {
105 case GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_ENTRY:
106 {
107 char key [103];
108 const char *dash;
109 struct GNUNET_PeerIdentity door;
110
111 if ((NULL == (dash = strchr (s, '-'))) ||
112 (1 != sscanf (s, "%103s-", key)) ||
113 (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string (dash + 1,
114 strlen (dash + 1),
115 &(door.public_key))))
116 {
117 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
118 _ ("Unable to parse MESSENGER_ROOM_ENTRY record `%s'\n"),
119 s);
120 return GNUNET_SYSERR;
121 }
122
123 struct GNUNET_MESSENGER_RoomEntryRecord *record = GNUNET_new (
124 struct GNUNET_MESSENGER_RoomEntryRecord
125 );
126
127 if (GNUNET_OK != GNUNET_STRINGS_string_to_data (key,
128 strlen (key),
129 &(record->key),
130 sizeof(struct GNUNET_HashCode)))
131 {
132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
133 _ ("Unable to parse MESSENGER_ROOM_ENTRY record `%s'\n"),
134 s);
135 GNUNET_free (record);
136 return GNUNET_SYSERR;
137 }
138
139 record->door = door;
140 *data = record;
141 *data_size = sizeof(struct GNUNET_MESSENGER_RoomEntryRecord);
142 return GNUNET_OK;
143 }
144
145 default:
146 return GNUNET_SYSERR;
147 }
148}
149
150
151/**
152 * Mapping of record type numbers to human-readable
153 * record type names.
154 */
155static struct
156{
157 const char *name;
158 uint32_t number;
159} name_map[] = {
160 { "MESSENGER_ROOM_ENTRY", GNUNET_GNSRECORD_TYPE_MESSENGER_ROOM_ENTRY },
161 { NULL, UINT32_MAX }
162};
163
164
165/**
166 * Convert a type name (e.g. "AAAA") to the corresponding number.
167 *
168 * @param cls closure, unused
169 * @param gns_typename name to convert
170 * @return corresponding number, UINT32_MAX on error
171 */
172static uint32_t
173messenger_typename_to_number (void *cls,
174 const char *gns_typename)
175{
176 unsigned int i;
177
178 (void) cls;
179 i = 0;
180 while ((name_map[i].name != NULL) &&
181 (0 != strcasecmp (gns_typename, name_map[i].name)))
182 i++;
183 return name_map[i].number;
184}
185
186
187/**
188 * Convert a type number to the corresponding type string (e.g. 1 to "A")
189 *
190 * @param cls closure, unused
191 * @param type number of a type to convert
192 * @return corresponding typestring, NULL on error
193 */
194static const char *
195messenger_number_to_typename (void *cls,
196 uint32_t type)
197{
198 unsigned int i;
199
200 (void) cls;
201 i = 0;
202 while ((name_map[i].name != NULL) &&
203 (type != name_map[i].number))
204 i++;
205 return name_map[i].name;
206}
207
208
209/**
210 * Entry point for the plugin.
211 *
212 * @param cls NULL
213 * @return the exported block API
214 */
215void *
216libgnunet_plugin_gnsrecord_messenger_init (void *cls)
217{
218 struct GNUNET_GNSRECORD_PluginFunctions *api;
219
220 (void) cls;
221 api = GNUNET_new (struct GNUNET_GNSRECORD_PluginFunctions);
222 api->value_to_string = &messenger_value_to_string;
223 api->string_to_value = &messenger_string_to_value;
224 api->typename_to_number = &messenger_typename_to_number;
225 api->number_to_typename = &messenger_number_to_typename;
226 return api;
227}
228
229
230/**
231 * Exit point from the plugin.
232 *
233 * @param cls the return value from #libgnunet_plugin_block_test_init
234 * @return NULL
235 */
236void *
237libgnunet_plugin_gnsrecord_messenger_done (void *cls)
238{
239 struct GNUNET_GNSRECORD_PluginFunctions *api = cls;
240
241 GNUNET_free (api);
242 return NULL;
243}