2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file conversation/gnunet-service-conversation.c
22 * @brief conversation service implementation
23 * @author Simon Dieterle
24 * @author Andreas Fuchs
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_applications.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_signatures.h"
33 #include "gnunet_mesh_service.h"
34 #include "gnunet_conversation_service.h"
35 #include "conversation.h"
39 * How long is our signature on a call valid? Needs to be long enough for time zone
40 * differences and network latency to not matter. No strong need for it to be short,
41 * but we simply like all signatures to eventually expire.
43 #define RING_TIMEOUT GNUNET_TIME_UNIT_DAYS
47 * The possible connection status
52 * We are waiting for incoming calls.
57 * Our phone is ringing, waiting for the client to pick up.
67 * We are waiting for the phone to be picked up.
77 * We're in shutdown, sending hangup messages before cleaning up.
84 * A line connects a local client with a mesh tunnel (or, if it is an
85 * open line, is waiting for a mesh tunnel).
100 * Handle for the reliable tunnel (contol data)
102 struct GNUNET_MESH_Tunnel *tunnel_reliable;
105 * Handle for unreliable tunnel (audio data)
107 struct GNUNET_MESH_Tunnel *tunnel_unreliable;
110 * Transmit handle for pending audio messages
112 struct GNUNET_MESH_TransmitHandle *unreliable_mth;
115 * Message queue for control messages
117 struct GNUNET_MQ_Handle *reliable_mq;
120 * Handle to the line client.
122 struct GNUNET_SERVER_Client *client;
125 * Target of the line, if we are the caller.
127 struct GNUNET_PeerIdentity target;
135 * Remote line number.
137 uint32_t remote_line;
140 * Current status of this line.
142 enum LineStatus status;
150 static const struct GNUNET_CONFIGURATION_Handle *cfg;
153 * Notification context containing all connected clients.
155 static struct GNUNET_SERVER_NotificationContext *nc;
160 static struct GNUNET_MESH_Handle *mesh;
163 * Identity of this peer.
165 static struct GNUNET_PeerIdentity my_identity;
168 * Head of DLL of active lines.
170 static struct Line *lines_head;
173 * Tail of DLL of active lines.
175 static struct Line *lines_tail;
178 * Counter for generating local line numbers.
179 * FIXME: randomize generation in the future
180 * to eliminate information leakage.
182 static uint32_t local_line_cnt;
186 * Function to register a phone.
188 * @param cls closure, NULL
189 * @param client the client from which the message is
190 * @param message the message from the client
193 handle_client_register_message (void *cls,
194 struct GNUNET_SERVER_Client *client,
195 const struct GNUNET_MessageHeader *message)
197 const struct ClientPhoneRegisterMessage *msg;
200 msg = (struct ClientPhoneRegisterMessage *) message;
201 line = GNUNET_SERVER_client_get_user_context (client, struct Line);
204 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
207 line = GNUNET_new (struct Line);
208 line->client = client;
209 GNUNET_SERVER_notification_context_add (nc, client);
210 GNUNET_CONTAINER_DLL_insert (lines_head,
213 line->local_line = ntohl (msg->line);
214 GNUNET_SERVER_client_set_user_context (client, line);
215 GNUNET_SERVER_receive_done (client, GNUNET_OK);
220 * Function to handle a pickup request message from the client
222 * @param cls closure, NULL
223 * @param client the client from which the message is
224 * @param message the message from the client
227 handle_client_pickup_message (void *cls,
228 struct GNUNET_SERVER_Client *client,
229 const struct GNUNET_MessageHeader *message)
231 const struct ClientPhonePickupMessage *msg;
233 msg = (struct ClientPhonePickupMessage *) message;
234 GNUNET_break (0); // FIXME
235 GNUNET_SERVER_receive_done (client, GNUNET_OK);
240 * Function to handle a hangup request message from the client
242 * @param cls closure, NULL
243 * @param client the client from which the message is
244 * @param message the message from the client
247 handle_client_hangup_message (void *cls,
248 struct GNUNET_SERVER_Client *client,
249 const struct GNUNET_MessageHeader *message)
251 const struct ClientPhoneHangupMessage *msg;
253 msg = (struct ClientPhoneHangupMessage *) message;
254 GNUNET_break (0); // FIXME
255 GNUNET_SERVER_receive_done (client, GNUNET_OK);
260 * Function to handle call request the client
262 * @param cls closure, NULL
263 * @param client the client from which the message is
264 * @param message the message from the client
267 handle_client_call_message (void *cls,
268 struct GNUNET_SERVER_Client *client,
269 const struct GNUNET_MessageHeader *message)
271 const struct ClientCallMessage *msg;
273 struct GNUNET_MQ_Envelope *e;
274 struct MeshPhoneRingMessage *ring;
276 msg = (struct ClientCallMessage *) message;
277 line = GNUNET_SERVER_client_get_user_context (client, struct Line);
280 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
283 line = GNUNET_new (struct Line);
284 line->target = msg->target;
285 GNUNET_CONTAINER_DLL_insert (lines_head,
288 line->remote_line = ntohl (msg->line);
289 line->status = LS_CALLER_CALLING;
290 line->tunnel_reliable = GNUNET_MESH_tunnel_create (mesh,
293 GNUNET_APPLICATION_TYPE_CONVERSATION_CONTROL,
296 line->reliable_mq = GNUNET_MESH_mq_create (line->tunnel_reliable);
297 line->local_line = local_line_cnt++;
298 e = GNUNET_MQ_msg (ring, GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_RING);
299 ring->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CONVERSATION_RING);
300 ring->purpose.size = htonl (sizeof (struct GNUNET_PeerIdentity) * 2 +
301 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
302 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
303 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey));
304 GNUNET_CRYPTO_ecc_key_get_public_for_signature (&msg->caller_id,
306 ring->remote_line = msg->line;
307 ring->source_line = line->local_line;
308 ring->target = msg->target;
309 ring->source = my_identity;
310 ring->expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (RING_TIMEOUT));
311 GNUNET_CRYPTO_ecc_sign (&msg->caller_id,
314 GNUNET_MQ_send (line->reliable_mq, e);
315 GNUNET_SERVER_client_set_user_context (client, line);
316 GNUNET_SERVER_receive_done (client, GNUNET_OK);
321 * Function to handle audio data from the client
323 * @param cls closure, NULL
324 * @param client the client from which the message is
325 * @param message the message from the client
328 handle_client_audio_message (void *cls,
329 struct GNUNET_SERVER_Client *client,
330 const struct GNUNET_MessageHeader *message)
332 const struct ClientAudioMessage *msg;
334 msg = (struct ClientAudioMessage *) message;
335 GNUNET_break (0); // FIXME
336 GNUNET_SERVER_receive_done (client, GNUNET_OK);
341 * Function to handle a ring message incoming over mesh
343 * @param cls closure, NULL
344 * @param tunnel the tunnel over which the message arrived
345 * @param tunnel_ctx the tunnel context, can be NULL
346 * @param message the incoming message
350 handle_mesh_ring_message (void *cls,
351 struct GNUNET_MESH_Tunnel *tunnel,
353 const struct GNUNET_MessageHeader *message)
355 const struct MeshPhoneRingMessage *msg;
357 struct GNUNET_MQ_Envelope *e;
358 struct MeshPhoneBusyMessage *busy;
359 struct ClientPhoneRingMessage cring;
361 msg = (const struct MeshPhoneRingMessage *) message;
362 if ( (msg->purpose.size != htonl (sizeof (struct GNUNET_PeerIdentity) * 2 +
363 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
364 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
365 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey))) ||
367 GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_CONVERSATION_RING,
373 return GNUNET_SYSERR;
375 for (line = lines_head; NULL != line; line = line->next)
376 if ( (line->local_line == ntohl (msg->remote_line)) &&
377 (LS_CALLEE_LISTEN == line->status) )
381 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
382 _("No available phone for incoming call on line %u, sending BUSY signal\n"),
383 ntohl (msg->remote_line));
384 e = GNUNET_MQ_msg (busy, GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_BUSY);
385 GNUNET_MQ_send (line->reliable_mq, e);
388 line->status = LS_CALLEE_RINGING;
389 line->remote_line = ntohl (msg->source_line);
390 line->tunnel_reliable = tunnel;
391 cring.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RING);
392 cring.header.size = htons (sizeof (cring));
393 cring.reserved = htonl (0);
394 cring.caller_id = msg->caller_id;
395 GNUNET_SERVER_notification_context_unicast (nc,
404 * Function to handle a hangup message incoming over mesh
406 * @param cls closure, NULL
407 * @param tunnel the tunnel over which the message arrived
408 * @param tunnel_ctx the tunnel context, can be NULL
409 * @param message the incoming message
413 handle_mesh_hangup_message (void *cls,
414 struct GNUNET_MESH_Tunnel *tunnel,
416 const struct GNUNET_MessageHeader *message)
418 const struct MeshPhoneHangupMessage *msg;
420 msg = (const struct MeshPhoneHangupMessage *) message;
421 GNUNET_break (0); // FIXME
427 * Function to handle a pickup message incoming over mesh
429 * @param cls closure, NULL
430 * @param tunnel the tunnel over which the message arrived
431 * @param tunnel_ctx the tunnel context, can be NULL
432 * @param message the incoming message
436 handle_mesh_pickup_message (void *cls,
437 struct GNUNET_MESH_Tunnel *tunnel,
439 const struct GNUNET_MessageHeader *message)
441 const struct MeshPhonePickupMessage *msg;
442 struct Line *line = *tunnel_ctx;
444 msg = (const struct MeshPhonePickupMessage *) message;
445 GNUNET_break (0); // FIXME
448 line->tunnel_unreliable = GNUNET_MESH_tunnel_create (mesh,
451 GNUNET_APPLICATION_TYPE_CONVERSATION_AUDIO,
461 * Function to handle a busy message incoming over mesh
463 * @param cls closure, NULL
464 * @param tunnel the tunnel over which the message arrived
465 * @param tunnel_ctx the tunnel context, can be NULL
466 * @param message the incoming message
470 handle_mesh_busy_message (void *cls,
471 struct GNUNET_MESH_Tunnel *tunnel,
473 const struct GNUNET_MessageHeader *message)
475 const struct MeshPhoneBusyMessage *msg;
477 msg = (const struct MeshPhoneBusyMessage *) message;
478 GNUNET_break (0); // FIXME
484 * Function to handle an audio message incoming over mesh
486 * @param cls closure, NULL
487 * @param tunnel the tunnel over which the message arrived
488 * @param tunnel_ctx the tunnel context, can be NULL
489 * @param message the incoming message
493 handle_mesh_audio_message (void *cls,
494 struct GNUNET_MESH_Tunnel *tunnel,
496 const struct GNUNET_MessageHeader *message)
498 const struct MeshAudioMessage *msg;
500 msg = (const struct MeshAudioMessage *) message;
501 GNUNET_break (0); // FIXME
507 * Method called whenever another peer has added us to a tunnel
508 * the other peer initiated.
511 * @param tunnel new handle to the tunnel
512 * @param initiator peer that started the tunnel
514 * @return initial tunnel context for the tunnel (can be NULL -- that's not an error)
517 inbound_tunnel (void *cls,
518 struct GNUNET_MESH_Tunnel *tunnel,
519 const struct GNUNET_PeerIdentity *initiator,
523 GNUNET_break (0); // FIXME
524 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
525 _("Received incoming tunnel on port %d\n"), port);
531 * Function called whenever an inbound tunnel is destroyed. Should clean up
532 * any associated state.
534 * @param cls closure (set from #GNUNET_MESH_connect)
535 * @param tunnel connection to the other end (henceforth invalid)
536 * @param tunnel_ctx place where local state associated
537 * with the tunnel is stored
540 inbound_end (void *cls,
541 const struct GNUNET_MESH_Tunnel *tunnel,
544 GNUNET_break (0); // FIXME
549 * A client disconnected. Remove all of its data structure entries.
551 * @param cls closure, NULL
552 * @param client identification of the client
555 handle_client_disconnect (void *cls,
556 struct GNUNET_SERVER_Client *client)
560 line = GNUNET_SERVER_client_get_user_context (client, struct Line);
563 GNUNET_CONTAINER_DLL_remove (lines_head,
567 GNUNET_SERVER_client_set_user_context (client, NULL);
574 * @param cls closure, NULL
575 * @param tc the task context
578 do_shutdown (void *cls,
579 const struct GNUNET_SCHEDULER_TaskContext *tc)
581 GNUNET_break (0); // FIXME
584 GNUNET_MESH_disconnect (mesh);
589 GNUNET_SERVER_notification_context_destroy (nc);
596 * Main function that will be run by the scheduler.
599 * @param server server handle
600 * @param c configuration
604 struct GNUNET_SERVER_Handle *server,
605 const struct GNUNET_CONFIGURATION_Handle *c)
607 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
608 {&handle_client_register_message, NULL,
609 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_REGISTER,
610 sizeof (struct ClientPhoneRegisterMessage)},
611 {&handle_client_pickup_message, NULL,
612 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_PICK_UP,
614 {&handle_client_hangup_message, NULL,
615 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP,
617 {&handle_client_call_message, NULL,
618 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_CALL,
620 {&handle_client_audio_message, NULL,
621 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO,
625 static struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
626 {&handle_mesh_ring_message,
627 GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_RING,
628 sizeof (struct MeshPhoneRingMessage)},
629 {&handle_mesh_hangup_message,
630 GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_HANG_UP,
632 {&handle_mesh_pickup_message,
633 GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_PICK_UP,
635 {&handle_mesh_busy_message,
636 GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_BUSY,
637 sizeof (struct MeshPhoneBusyMessage)},
638 {&handle_mesh_audio_message, GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_AUDIO,
642 static uint32_t ports[] = {
643 GNUNET_APPLICATION_TYPE_CONVERSATION_CONTROL,
644 GNUNET_APPLICATION_TYPE_CONVERSATION_AUDIO,
649 GNUNET_assert (GNUNET_OK ==
650 GNUNET_CRYPTO_get_host_identity (cfg,
652 mesh = GNUNET_MESH_connect (cfg,
662 GNUNET_SCHEDULER_shutdown ();
665 nc = GNUNET_SERVER_notification_context_create (server, 16);
666 GNUNET_SERVER_add_handlers (server, server_handlers);
667 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
668 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
675 * The main function for the conversation service.
677 * @param argc number of arguments from the command line
678 * @param argv command line arguments
679 * @return 0 ok, 1 on error
686 GNUNET_SERVICE_run (argc, argv,
688 GNUNET_SERVICE_OPTION_NONE,
689 &run, NULL)) ? 0 : 1;
692 /* end of gnunet-service-conversation.c */