summaryrefslogtreecommitdiff
path: root/src/messenger/gnunet-service-messenger.c
blob: 2c92305c414e88333eeca40d244879a431b6b8b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
   This file is part of GNUnet.
   Copyright (C) 2020 GNUnet e.V.

   GNUnet is free software: you can redistribute it and/or modify it
   under the terms of the GNU Affero General Public License as published
   by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   GNUnet is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   SPDX-License-Identifier: AGPL3.0-or-later
 */
/**
 * @author Tobias Frisch
 * @file src/messenger/gnunet-service-messenger.c
 * @brief GNUnet MESSENGER service
 */

#include "gnunet-service-messenger.h"

#include "gnunet-service-messenger_service.h"
#include "messenger_api_message.h"

struct GNUNET_MESSENGER_Client
{
  struct GNUNET_SERVICE_Client *client;
  struct GNUNET_MESSENGER_SrvHandle *handle;
};

struct GNUNET_MESSENGER_Service *messenger;

static int
check_create (void *cls, const struct GNUNET_MESSENGER_CreateMessage *msg)
{
  GNUNET_MQ_check_zero_termination(msg);
  return GNUNET_OK;
}

static void
handle_create (void *cls, const struct GNUNET_MESSENGER_CreateMessage *msg)
{
  struct GNUNET_MESSENGER_Client *msg_client = cls;

  const char *name = ((const char*) msg) + sizeof(*msg);

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Handle created with name: %s\n", name);

  setup_handle_name (msg_client->handle, strlen (name) > 0? name : NULL);

  GNUNET_SERVICE_client_continue (msg_client->client);
}

static void
handle_update (void *cls, const struct GNUNET_MESSENGER_UpdateMessage *msg)
{
  struct GNUNET_MESSENGER_Client *msg_client = cls;

  if (GNUNET_OK != update_handle (msg_client->handle))
    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Name is required to update key!\n");

  GNUNET_SERVICE_client_continue (msg_client->client);
}

static void
handle_destroy (void *cls, const struct GNUNET_MESSENGER_DestroyMessage *msg)
{
  struct GNUNET_MESSENGER_Client *msg_client = cls;

  GNUNET_SERVICE_client_drop (msg_client->client);
}

static int
check_set_name (void *cls, const struct GNUNET_MESSENGER_NameMessage *msg)
{
  GNUNET_MQ_check_zero_termination(msg);
  return GNUNET_OK;
}

static void
handle_set_name (void *cls, const struct GNUNET_MESSENGER_NameMessage *msg)
{
  struct GNUNET_MESSENGER_Client *msg_client = cls;

  const char *name = ((const char*) msg) + sizeof(*msg);

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Handles name is now: %s\n", name);

  if (GNUNET_YES != set_handle_name (msg_client->handle, name))
    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No valid name: %s\n", name);

  GNUNET_SERVICE_client_continue (msg_client->client);
}

static void
handle_room_open (void *cls, const struct GNUNET_MESSENGER_RoomMessage *msg)
{
  struct GNUNET_MESSENGER_Client *msg_client = cls;

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Opening room: %s\n",
             GNUNET_h2s (&(msg->key)));

  if (GNUNET_YES == open_handle_room (msg_client->handle, &(msg->key)))
  {
    const struct GNUNET_ShortHashCode* member_id = get_handle_member_id(msg_client->handle, &(msg->key));

    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Opening room with member id: %s\n",
               GNUNET_sh2s (member_id));

    struct GNUNET_MESSENGER_RoomMessage *response;
    struct GNUNET_MQ_Envelope *env;

    env = GNUNET_MQ_msg(response, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_OPEN);
    GNUNET_memcpy(&(response->key), &(msg->key), sizeof(msg->key));
    GNUNET_MQ_send (msg_client->handle->mq, env);
  }
  else
    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Opening room failed: %s\n",
               GNUNET_h2s (&(msg->key)));

  GNUNET_SERVICE_client_continue (msg_client->client);
}

static void
handle_room_entry (void *cls, const struct GNUNET_MESSENGER_RoomMessage *msg)
{
  struct GNUNET_MESSENGER_Client *msg_client = cls;

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Entering room: %s, %s\n",
             GNUNET_h2s (&(msg->key)), GNUNET_i2s (&(msg->door)));

  if (GNUNET_YES == entry_handle_room (msg_client->handle, &(msg->door), &(msg->key)))
  {
    const struct GNUNET_ShortHashCode* member_id = get_handle_member_id(msg_client->handle, &(msg->key));

    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Entering room with member id: %s\n",
               GNUNET_sh2s (member_id));

    struct GNUNET_MESSENGER_RoomMessage *response;
    struct GNUNET_MQ_Envelope *env;

    env = GNUNET_MQ_msg(response, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_ENTRY);
    GNUNET_memcpy(&(response->door), &(msg->door), sizeof(msg->door));
    GNUNET_memcpy(&(response->key), &(msg->key), sizeof(msg->key));
    GNUNET_MQ_send (msg_client->handle->mq, env);
  }
  else
    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Entrance into room failed: %s, %s\n",
               GNUNET_h2s (&(msg->key)), GNUNET_i2s (&(msg->door)));

  GNUNET_SERVICE_client_continue (msg_client->client);
}

static void
handle_room_close (void *cls, const struct GNUNET_MESSENGER_RoomMessage *msg)
{
  struct GNUNET_MESSENGER_Client *msg_client = cls;

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Closing room: %s\n", GNUNET_h2s (&(msg->key)));

  if (GNUNET_YES == close_handle_room (msg_client->handle, &(msg->key)))
  {
    const struct GNUNET_ShortHashCode* member_id = get_handle_member_id(msg_client->handle, &(msg->key));

    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Closing room with member id: %s\n",
               GNUNET_sh2s (member_id));

    struct GNUNET_MESSENGER_RoomMessage *response;
    struct GNUNET_MQ_Envelope *env;

    env = GNUNET_MQ_msg(response, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_CLOSE);
    GNUNET_memcpy(&(response->key), &(msg->key), sizeof(msg->key));
    GNUNET_MQ_send (msg_client->handle->mq, env);
  }
  else
    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Closing room failed: %s\n", GNUNET_h2s (&(msg->key)));

  GNUNET_SERVICE_client_continue (msg_client->client);
}

static int
check_send_message (void *cls, const struct GNUNET_MESSENGER_SendMessage *msg)
{
  const uint16_t full_length = ntohs (msg->header.size) - sizeof(msg->header);

  if (full_length < sizeof(msg->key))
    return GNUNET_NO;

  const uint16_t length = full_length - sizeof(msg->key);
  const char *buffer = ((const char*) msg) + sizeof(*msg);

  struct GNUNET_MESSENGER_Message message;

  if (GNUNET_YES != decode_message (&message, length, buffer))
    return GNUNET_NO;

  return GNUNET_OK;
}

static void
handle_send_message (void *cls, const struct GNUNET_MESSENGER_SendMessage *msg)
{
  struct GNUNET_MESSENGER_Client *msg_client = cls;

  const struct GNUNET_HashCode *key = &(msg->key);
  const char *buffer = ((const char*) msg) + sizeof(*msg);

  const uint16_t length = ntohs (msg->header.size) - sizeof(*msg);

  struct GNUNET_MESSENGER_Message message;
  decode_message (&message, length, buffer);

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending message: %s to %s\n",
             GNUNET_MESSENGER_name_of_kind (message.header.kind),
             GNUNET_h2s (key));

  if (GNUNET_YES != send_handle_message (msg_client->handle, key, &message))
    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Sending message failed: %s to %s\n",
               GNUNET_MESSENGER_name_of_kind (message.header.kind),
               GNUNET_h2s (key));

  GNUNET_SERVICE_client_continue (msg_client->client);
}

static void
handle_get_message (void *cls, const struct GNUNET_MESSENGER_RecvMessage *msg)
{
  struct GNUNET_MESSENGER_Client *msg_client = cls;

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Requesting message from room: %s\n",
             GNUNET_h2s (&(msg->key)));

  struct GNUNET_MESSENGER_SrvRoom *room = get_service_room (messenger, &(msg->key));

  if (room)
    get_room_message (room, msg_client->handle, &(msg->hash), GNUNET_YES);
  else
    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Room not found: %s\n",
               GNUNET_h2s (&(msg->key)));

  GNUNET_SERVICE_client_continue (msg_client->client);
}

static void*
callback_client_connect (void *cls, struct GNUNET_SERVICE_Client *client, struct GNUNET_MQ_Handle *mq)
{
  struct GNUNET_MESSENGER_Client *msg_client = GNUNET_new(struct GNUNET_MESSENGER_Client);

  msg_client->client = client;
  msg_client->handle = add_service_handle (messenger, mq);

  return msg_client;
}

static void
callback_client_disconnect (void *cls, struct GNUNET_SERVICE_Client *client, void *internal_cls)
{
  struct GNUNET_MESSENGER_Client *msg_client = internal_cls;

  remove_service_handle (messenger, msg_client->handle);

  GNUNET_free(msg_client);
}

/**
 * Setup MESSENGER internals.
 *
 * @param cls closure
 * @param config configuration to use
 * @param service the initialized service
 */
static void
run (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, struct GNUNET_SERVICE_Handle *service)
{
  messenger = create_service (config, service);

  if ((!messenger) || (!messenger->cadet) || (!messenger->identity))
    GNUNET_SCHEDULER_shutdown ();
}

/**
 * Define "main" method using service macro.
 */
GNUNET_SERVICE_MAIN(
    GNUNET_MESSENGER_SERVICE_NAME,
    GNUNET_SERVICE_OPTION_NONE,
    &run,
    &callback_client_connect,
    &callback_client_disconnect,
    NULL,
    GNUNET_MQ_hd_var_size( create, GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_CREATE, struct GNUNET_MESSENGER_CreateMessage, NULL ),
    GNUNET_MQ_hd_fixed_size( update, GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_UPDATE, struct GNUNET_MESSENGER_UpdateMessage, NULL ),
    GNUNET_MQ_hd_fixed_size( destroy, GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_DESTROY, struct GNUNET_MESSENGER_DestroyMessage, NULL ),
    GNUNET_MQ_hd_var_size( set_name, GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_SET_NAME, struct GNUNET_MESSENGER_NameMessage, NULL ),
    GNUNET_MQ_hd_fixed_size( room_open, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_OPEN, struct GNUNET_MESSENGER_RoomMessage, NULL ),
    GNUNET_MQ_hd_fixed_size( room_entry, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_ENTRY, struct GNUNET_MESSENGER_RoomMessage, NULL ),
    GNUNET_MQ_hd_fixed_size( room_close, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_CLOSE, struct GNUNET_MESSENGER_RoomMessage, NULL ),
    GNUNET_MQ_hd_var_size( send_message, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_SEND_MESSAGE, struct GNUNET_MESSENGER_SendMessage, NULL ),
    GNUNET_MQ_hd_fixed_size( get_message, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_GET_MESSAGE, struct GNUNET_MESSENGER_RecvMessage, NULL ),
    GNUNET_MQ_handler_end());