diff options
Diffstat (limited to 'src/messenger/gnunet-service-messenger.c')
-rw-r--r-- | src/messenger/gnunet-service-messenger.c | 148 |
1 files changed, 103 insertions, 45 deletions
diff --git a/src/messenger/gnunet-service-messenger.c b/src/messenger/gnunet-service-messenger.c index 2c92305c4..187b65ed5 100644 --- a/src/messenger/gnunet-service-messenger.c +++ b/src/messenger/gnunet-service-messenger.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2020 GNUnet e.V. | 3 | Copyright (C) 2020--2021 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 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 | 6 | under the terms of the GNU Affero General Public License as published |
@@ -52,7 +52,7 @@ handle_create (void *cls, const struct GNUNET_MESSENGER_CreateMessage *msg) | |||
52 | 52 | ||
53 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Handle created with name: %s\n", name); | 53 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Handle created with name: %s\n", name); |
54 | 54 | ||
55 | setup_handle_name (msg_client->handle, strlen (name) > 0? name : NULL); | 55 | setup_handle_name (msg_client->handle, strlen (name) > 0 ? name : NULL); |
56 | 56 | ||
57 | GNUNET_SERVICE_client_continue (msg_client->client); | 57 | GNUNET_SERVICE_client_continue (msg_client->client); |
58 | } | 58 | } |
@@ -62,8 +62,7 @@ handle_update (void *cls, const struct GNUNET_MESSENGER_UpdateMessage *msg) | |||
62 | { | 62 | { |
63 | struct GNUNET_MESSENGER_Client *msg_client = cls; | 63 | struct GNUNET_MESSENGER_Client *msg_client = cls; |
64 | 64 | ||
65 | if (GNUNET_OK != update_handle (msg_client->handle)) | 65 | update_handle (msg_client->handle); |
66 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Name is required to update key!\n"); | ||
67 | 66 | ||
68 | GNUNET_SERVICE_client_continue (msg_client->client); | 67 | GNUNET_SERVICE_client_continue (msg_client->client); |
69 | } | 68 | } |
@@ -92,8 +91,7 @@ handle_set_name (void *cls, const struct GNUNET_MESSENGER_NameMessage *msg) | |||
92 | 91 | ||
93 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Handles name is now: %s\n", name); | 92 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Handles name is now: %s\n", name); |
94 | 93 | ||
95 | if (GNUNET_YES != set_handle_name (msg_client->handle, name)) | 94 | set_handle_name (msg_client->handle, name); |
96 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No valid name: %s\n", name); | ||
97 | 95 | ||
98 | GNUNET_SERVICE_client_continue (msg_client->client); | 96 | GNUNET_SERVICE_client_continue (msg_client->client); |
99 | } | 97 | } |
@@ -103,15 +101,13 @@ handle_room_open (void *cls, const struct GNUNET_MESSENGER_RoomMessage *msg) | |||
103 | { | 101 | { |
104 | struct GNUNET_MESSENGER_Client *msg_client = cls; | 102 | struct GNUNET_MESSENGER_Client *msg_client = cls; |
105 | 103 | ||
106 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Opening room: %s\n", | 104 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Opening room: %s\n", GNUNET_h2s (&(msg->key))); |
107 | GNUNET_h2s (&(msg->key))); | ||
108 | 105 | ||
109 | if (GNUNET_YES == open_handle_room (msg_client->handle, &(msg->key))) | 106 | if (GNUNET_YES == open_handle_room (msg_client->handle, &(msg->key))) |
110 | { | 107 | { |
111 | const struct GNUNET_ShortHashCode* member_id = get_handle_member_id(msg_client->handle, &(msg->key)); | 108 | const struct GNUNET_ShortHashCode *member_id = get_handle_member_id (msg_client->handle, &(msg->key)); |
112 | 109 | ||
113 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Opening room with member id: %s\n", | 110 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Opening room with member id: %s\n", GNUNET_sh2s (member_id)); |
114 | GNUNET_sh2s (member_id)); | ||
115 | 111 | ||
116 | struct GNUNET_MESSENGER_RoomMessage *response; | 112 | struct GNUNET_MESSENGER_RoomMessage *response; |
117 | struct GNUNET_MQ_Envelope *env; | 113 | struct GNUNET_MQ_Envelope *env; |
@@ -121,8 +117,7 @@ handle_room_open (void *cls, const struct GNUNET_MESSENGER_RoomMessage *msg) | |||
121 | GNUNET_MQ_send (msg_client->handle->mq, env); | 117 | GNUNET_MQ_send (msg_client->handle->mq, env); |
122 | } | 118 | } |
123 | else | 119 | else |
124 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Opening room failed: %s\n", | 120 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Opening room failed: %s\n", GNUNET_h2s (&(msg->key))); |
125 | GNUNET_h2s (&(msg->key))); | ||
126 | 121 | ||
127 | GNUNET_SERVICE_client_continue (msg_client->client); | 122 | GNUNET_SERVICE_client_continue (msg_client->client); |
128 | } | 123 | } |
@@ -132,15 +127,13 @@ handle_room_entry (void *cls, const struct GNUNET_MESSENGER_RoomMessage *msg) | |||
132 | { | 127 | { |
133 | struct GNUNET_MESSENGER_Client *msg_client = cls; | 128 | struct GNUNET_MESSENGER_Client *msg_client = cls; |
134 | 129 | ||
135 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Entering room: %s, %s\n", | 130 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Entering room: %s, %s\n", GNUNET_h2s (&(msg->key)), GNUNET_i2s (&(msg->door))); |
136 | GNUNET_h2s (&(msg->key)), GNUNET_i2s (&(msg->door))); | ||
137 | 131 | ||
138 | if (GNUNET_YES == entry_handle_room (msg_client->handle, &(msg->door), &(msg->key))) | 132 | if (GNUNET_YES == entry_handle_room (msg_client->handle, &(msg->door), &(msg->key))) |
139 | { | 133 | { |
140 | const struct GNUNET_ShortHashCode* member_id = get_handle_member_id(msg_client->handle, &(msg->key)); | 134 | const struct GNUNET_ShortHashCode *member_id = get_handle_member_id (msg_client->handle, &(msg->key)); |
141 | 135 | ||
142 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Entering room with member id: %s\n", | 136 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Entering room with member id: %s\n", GNUNET_sh2s (member_id)); |
143 | GNUNET_sh2s (member_id)); | ||
144 | 137 | ||
145 | struct GNUNET_MESSENGER_RoomMessage *response; | 138 | struct GNUNET_MESSENGER_RoomMessage *response; |
146 | struct GNUNET_MQ_Envelope *env; | 139 | struct GNUNET_MQ_Envelope *env; |
@@ -151,8 +144,8 @@ handle_room_entry (void *cls, const struct GNUNET_MESSENGER_RoomMessage *msg) | |||
151 | GNUNET_MQ_send (msg_client->handle->mq, env); | 144 | GNUNET_MQ_send (msg_client->handle->mq, env); |
152 | } | 145 | } |
153 | else | 146 | else |
154 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Entrance into room failed: %s, %s\n", | 147 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Entrance into room failed: %s, %s\n", GNUNET_h2s (&(msg->key)), |
155 | GNUNET_h2s (&(msg->key)), GNUNET_i2s (&(msg->door))); | 148 | GNUNET_i2s (&(msg->door))); |
156 | 149 | ||
157 | GNUNET_SERVICE_client_continue (msg_client->client); | 150 | GNUNET_SERVICE_client_continue (msg_client->client); |
158 | } | 151 | } |
@@ -166,10 +159,9 @@ handle_room_close (void *cls, const struct GNUNET_MESSENGER_RoomMessage *msg) | |||
166 | 159 | ||
167 | if (GNUNET_YES == close_handle_room (msg_client->handle, &(msg->key))) | 160 | if (GNUNET_YES == close_handle_room (msg_client->handle, &(msg->key))) |
168 | { | 161 | { |
169 | const struct GNUNET_ShortHashCode* member_id = get_handle_member_id(msg_client->handle, &(msg->key)); | 162 | const struct GNUNET_ShortHashCode *member_id = get_handle_member_id (msg_client->handle, &(msg->key)); |
170 | 163 | ||
171 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Closing room with member id: %s\n", | 164 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Closing room with member id: %s\n", GNUNET_sh2s (member_id)); |
172 | GNUNET_sh2s (member_id)); | ||
173 | 165 | ||
174 | struct GNUNET_MESSENGER_RoomMessage *response; | 166 | struct GNUNET_MESSENGER_RoomMessage *response; |
175 | struct GNUNET_MQ_Envelope *env; | 167 | struct GNUNET_MQ_Envelope *env; |
@@ -187,17 +179,40 @@ handle_room_close (void *cls, const struct GNUNET_MESSENGER_RoomMessage *msg) | |||
187 | static int | 179 | static int |
188 | check_send_message (void *cls, const struct GNUNET_MESSENGER_SendMessage *msg) | 180 | check_send_message (void *cls, const struct GNUNET_MESSENGER_SendMessage *msg) |
189 | { | 181 | { |
190 | const uint16_t full_length = ntohs (msg->header.size) - sizeof(msg->header); | 182 | const uint16_t full_length = ntohs (msg->header.size); |
191 | 183 | ||
192 | if (full_length < sizeof(msg->key)) | 184 | if (full_length < sizeof(*msg)) |
193 | return GNUNET_NO; | 185 | return GNUNET_NO; |
194 | 186 | ||
195 | const uint16_t length = full_length - sizeof(msg->key); | 187 | const enum GNUNET_MESSENGER_MessageFlags flags = ( |
188 | (enum GNUNET_MESSENGER_MessageFlags) (msg->flags) | ||
189 | ); | ||
190 | |||
191 | const uint16_t length = full_length - sizeof(*msg); | ||
196 | const char *buffer = ((const char*) msg) + sizeof(*msg); | 192 | const char *buffer = ((const char*) msg) + sizeof(*msg); |
197 | 193 | ||
194 | uint16_t key_length = 0; | ||
195 | |||
196 | if (!(flags & GNUNET_MESSENGER_FLAG_PRIVATE)) | ||
197 | goto check_for_message; | ||
198 | |||
199 | struct GNUNET_IDENTITY_PublicKey public_key; | ||
200 | |||
201 | key_length = GNUNET_IDENTITY_read_key_from_buffer(&public_key, buffer, length); | ||
202 | |||
203 | check_for_message: | ||
204 | if (key_length < 0) | ||
205 | return GNUNET_NO; | ||
206 | |||
207 | const uint16_t msg_length = length - key_length; | ||
208 | const char* msg_buffer = buffer + key_length; | ||
209 | |||
198 | struct GNUNET_MESSENGER_Message message; | 210 | struct GNUNET_MESSENGER_Message message; |
199 | 211 | ||
200 | if (GNUNET_YES != decode_message (&message, length, buffer)) | 212 | if (GNUNET_YES != decode_message (&message, msg_length, msg_buffer, GNUNET_NO, NULL)) |
213 | return GNUNET_NO; | ||
214 | |||
215 | if (GNUNET_YES != filter_message_sending(&message)) | ||
201 | return GNUNET_NO; | 216 | return GNUNET_NO; |
202 | 217 | ||
203 | return GNUNET_OK; | 218 | return GNUNET_OK; |
@@ -208,42 +223,85 @@ handle_send_message (void *cls, const struct GNUNET_MESSENGER_SendMessage *msg) | |||
208 | { | 223 | { |
209 | struct GNUNET_MESSENGER_Client *msg_client = cls; | 224 | struct GNUNET_MESSENGER_Client *msg_client = cls; |
210 | 225 | ||
226 | const enum GNUNET_MESSENGER_MessageFlags flags = ( | ||
227 | (enum GNUNET_MESSENGER_MessageFlags) (msg->flags) | ||
228 | ); | ||
229 | |||
211 | const struct GNUNET_HashCode *key = &(msg->key); | 230 | const struct GNUNET_HashCode *key = &(msg->key); |
212 | const char *buffer = ((const char*) msg) + sizeof(*msg); | 231 | const char *buffer = ((const char*) msg) + sizeof(*msg); |
213 | 232 | ||
214 | const uint16_t length = ntohs (msg->header.size) - sizeof(*msg); | 233 | const uint16_t length = ntohs (msg->header.size) - sizeof(*msg); |
234 | uint16_t key_length = 0; | ||
235 | |||
236 | struct GNUNET_IDENTITY_PublicKey public_key; | ||
237 | |||
238 | if (flags & GNUNET_MESSENGER_FLAG_PRIVATE) | ||
239 | key_length = GNUNET_IDENTITY_read_key_from_buffer( | ||
240 | &public_key, buffer, length | ||
241 | ); | ||
242 | |||
243 | const uint16_t msg_length = length - key_length; | ||
244 | const char* msg_buffer = buffer + key_length; | ||
215 | 245 | ||
216 | struct GNUNET_MESSENGER_Message message; | 246 | struct GNUNET_MESSENGER_Message message; |
217 | decode_message (&message, length, buffer); | 247 | decode_message (&message, msg_length, msg_buffer, GNUNET_NO, NULL); |
248 | |||
249 | if ((flags & GNUNET_MESSENGER_FLAG_PRIVATE) && | ||
250 | (GNUNET_YES != encrypt_message(&message, &public_key))) | ||
251 | { | ||
252 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Encrypting message failed: Message got dropped!\n"); | ||
253 | |||
254 | goto end_handling; | ||
255 | } | ||
218 | 256 | ||
219 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending message: %s to %s\n", | 257 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending message: %s to %s\n", |
220 | GNUNET_MESSENGER_name_of_kind (message.header.kind), | 258 | GNUNET_MESSENGER_name_of_kind (message.header.kind), GNUNET_h2s (key)); |
221 | GNUNET_h2s (key)); | ||
222 | 259 | ||
223 | if (GNUNET_YES != send_handle_message (msg_client->handle, key, &message)) | 260 | if (GNUNET_YES != send_handle_message (msg_client->handle, key, &message)) |
224 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sending message failed: %s to %s\n", | 261 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sending message failed: %s to %s\n", |
225 | GNUNET_MESSENGER_name_of_kind (message.header.kind), | 262 | GNUNET_MESSENGER_name_of_kind (message.header.kind), GNUNET_h2s (key)); |
226 | GNUNET_h2s (key)); | ||
227 | 263 | ||
264 | end_handling: | ||
228 | GNUNET_SERVICE_client_continue (msg_client->client); | 265 | GNUNET_SERVICE_client_continue (msg_client->client); |
229 | } | 266 | } |
230 | 267 | ||
231 | static void | 268 | static void |
232 | handle_get_message (void *cls, const struct GNUNET_MESSENGER_RecvMessage *msg) | 269 | handle_get_message (void *cls, const struct GNUNET_MESSENGER_GetMessage *msg) |
233 | { | 270 | { |
234 | struct GNUNET_MESSENGER_Client *msg_client = cls; | 271 | struct GNUNET_MESSENGER_Client *msg_client = cls; |
235 | 272 | ||
236 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Requesting message from room: %s\n", | 273 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Requesting message from room: %s\n", GNUNET_h2s (&(msg->key))); |
237 | GNUNET_h2s (&(msg->key))); | ||
238 | 274 | ||
239 | struct GNUNET_MESSENGER_SrvRoom *room = get_service_room (messenger, &(msg->key)); | 275 | struct GNUNET_MESSENGER_SrvRoom *room = get_service_room (messenger, &(msg->key)); |
240 | 276 | ||
241 | if (room) | 277 | if (!room) |
242 | get_room_message (room, msg_client->handle, &(msg->hash), GNUNET_YES); | 278 | { |
243 | else | 279 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Room not found: %s\n", GNUNET_h2s (&(msg->key))); |
244 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Room not found: %s\n", | 280 | goto end_handling; |
245 | GNUNET_h2s (&(msg->key))); | 281 | } |
282 | |||
283 | const struct GNUNET_MESSENGER_Message *message = get_room_message (room, msg_client->handle, &(msg->hash), | ||
284 | GNUNET_YES); | ||
285 | |||
286 | if (!message) | ||
287 | goto end_handling; | ||
288 | |||
289 | struct GNUNET_MESSENGER_MemberStore *store = get_room_member_store(room); | ||
290 | |||
291 | struct GNUNET_MESSENGER_Member *member = get_store_member_of(store, message); | ||
292 | |||
293 | if (!member) | ||
294 | { | ||
295 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sender of message (%s) unknown!\n", GNUNET_h2s (&(msg->hash))); | ||
296 | goto end_handling; | ||
297 | } | ||
298 | |||
299 | struct GNUNET_MESSENGER_MemberSession *session = get_member_session_of(member, message, &(msg->hash)); | ||
300 | |||
301 | if (session) | ||
302 | notify_handle_message (msg_client->handle, get_room_key(room), session, message, &(msg->hash)); | ||
246 | 303 | ||
304 | end_handling: | ||
247 | GNUNET_SERVICE_client_continue (msg_client->client); | 305 | GNUNET_SERVICE_client_continue (msg_client->client); |
248 | } | 306 | } |
249 | 307 | ||
@@ -271,16 +329,16 @@ callback_client_disconnect (void *cls, struct GNUNET_SERVICE_Client *client, voi | |||
271 | /** | 329 | /** |
272 | * Setup MESSENGER internals. | 330 | * Setup MESSENGER internals. |
273 | * | 331 | * |
274 | * @param cls closure | 332 | * @param[in/out] cls closure |
275 | * @param config configuration to use | 333 | * @param[in] config configuration to use |
276 | * @param service the initialized service | 334 | * @param[in/out] service the initialized service |
277 | */ | 335 | */ |
278 | static void | 336 | static void |
279 | run (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, struct GNUNET_SERVICE_Handle *service) | 337 | run (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, struct GNUNET_SERVICE_Handle *service) |
280 | { | 338 | { |
281 | messenger = create_service (config, service); | 339 | messenger = create_service (config, service); |
282 | 340 | ||
283 | if ((!messenger) || (!messenger->cadet) || (!messenger->identity)) | 341 | if (!messenger) |
284 | GNUNET_SCHEDULER_shutdown (); | 342 | GNUNET_SCHEDULER_shutdown (); |
285 | } | 343 | } |
286 | 344 | ||
@@ -302,5 +360,5 @@ GNUNET_SERVICE_MAIN( | |||
302 | GNUNET_MQ_hd_fixed_size( room_entry, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_ENTRY, struct GNUNET_MESSENGER_RoomMessage, NULL ), | 360 | GNUNET_MQ_hd_fixed_size( room_entry, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_ENTRY, struct GNUNET_MESSENGER_RoomMessage, NULL ), |
303 | GNUNET_MQ_hd_fixed_size( room_close, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_CLOSE, struct GNUNET_MESSENGER_RoomMessage, NULL ), | 361 | GNUNET_MQ_hd_fixed_size( room_close, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_CLOSE, struct GNUNET_MESSENGER_RoomMessage, NULL ), |
304 | GNUNET_MQ_hd_var_size( send_message, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_SEND_MESSAGE, struct GNUNET_MESSENGER_SendMessage, NULL ), | 362 | GNUNET_MQ_hd_var_size( send_message, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_SEND_MESSAGE, struct GNUNET_MESSENGER_SendMessage, NULL ), |
305 | GNUNET_MQ_hd_fixed_size( get_message, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_GET_MESSAGE, struct GNUNET_MESSENGER_RecvMessage, NULL ), | 363 | GNUNET_MQ_hd_fixed_size( get_message, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_GET_MESSAGE, struct GNUNET_MESSENGER_GetMessage, NULL ), |
306 | GNUNET_MQ_handler_end()); | 364 | GNUNET_MQ_handler_end()); |