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 * A line connects a local client with a mesh channel (or, if it is an
48 * open line, is waiting for a mesh channel).
53 * The possible connection status
58 * Our phone is ringing, waiting for the client to pick up.
68 * We're in shutdown, sending hangup messages before cleaning up.
73 * We are waiting for the phone to be picked up.
83 * We're in shutdown, sending hangup messages before cleaning up.
91 * A `struct Channel` represents a mesh channel, which is a P2P
92 * connection to another conversation service. Multiple channels can
93 * be attached the the same `struct Line`, which represents a local
94 * client. We keep them in a linked list.
102 struct Channel *next;
107 struct Channel *prev;
110 * Line associated with the channel.
115 * Handle for the reliable channel (contol data)
117 struct GNUNET_MESH_Channel *channel_reliable;
120 * Handle for unreliable channel (audio data)
122 struct GNUNET_MESH_Channel *channel_unreliable;
125 * Transmit handle for pending audio messages
127 struct GNUNET_MESH_TransmitHandle *unreliable_mth;
130 * Message queue for control messages
132 struct GNUNET_MQ_Handle *reliable_mq;
135 * Target of the line, if we are the caller.
137 struct GNUNET_PeerIdentity target;
140 * Temporary buffer for audio data.
145 * Number of bytes in @e audio_data.
150 * Channel identifier.
155 * Remote line number.
157 uint32_t remote_line;
160 * Current status of this line.
162 enum ChannelStatus status;
165 * #GNUNET_YES if the channel was suspended by the other peer.
167 int8_t suspended_remote;
170 * #GNUNET_YES if the channel was suspended by the local client.
172 int8_t suspended_local;
178 * A `struct Line` connects a local client with mesh channels.
195 struct Channel *channel_head;
200 struct Channel *channel_tail;
203 * Handle to the line client.
205 struct GNUNET_SERVER_Client *client;
208 * Generator for channel IDs.
223 static const struct GNUNET_CONFIGURATION_Handle *cfg;
226 * Notification context containing all connected clients.
228 static struct GNUNET_SERVER_NotificationContext *nc;
233 static struct GNUNET_MESH_Handle *mesh;
236 * Identity of this peer.
238 static struct GNUNET_PeerIdentity my_identity;
241 * Head of DLL of active lines.
243 static struct Line *lines_head;
246 * Tail of DLL of active lines.
248 static struct Line *lines_tail;
251 * Counter for generating local line numbers.
252 * FIXME: randomize generation in the future
253 * to eliminate information leakage.
255 static uint32_t local_line_cnt;
259 * Function to register a phone.
261 * @param cls closure, NULL
262 * @param client the client from which the message is
263 * @param message the message from the client
266 handle_client_register_message (void *cls,
267 struct GNUNET_SERVER_Client *client,
268 const struct GNUNET_MessageHeader *message)
270 const struct ClientPhoneRegisterMessage *msg;
273 msg = (const struct ClientPhoneRegisterMessage *) message;
274 line = GNUNET_SERVER_client_get_user_context (client, struct Line);
278 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
281 line = GNUNET_new (struct Line);
282 line->client = client;
283 GNUNET_SERVER_notification_context_add (nc, client);
284 GNUNET_SERVER_client_set_user_context (client, line);
285 GNUNET_CONTAINER_DLL_insert (lines_head,
288 line->local_line = ntohl (msg->line) & (~ (1 << 31));
289 GNUNET_SERVER_receive_done (client, GNUNET_OK);
294 * Function to handle a pickup request message from the client
296 * @param cls closure, NULL
297 * @param client the client from which the message is
298 * @param message the message from the client
301 handle_client_pickup_message (void *cls,
302 struct GNUNET_SERVER_Client *client,
303 const struct GNUNET_MessageHeader *message)
305 const struct ClientPhonePickupMessage *msg;
306 struct GNUNET_MQ_Envelope *e;
307 struct MeshPhonePickupMessage *mppm;
311 msg = (const struct ClientPhonePickupMessage *) message;
312 line = GNUNET_SERVER_client_get_user_context (client, struct Line);
316 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
319 for (ch = line->channel_head; NULL != ch; ch = ch->next)
320 if (msg->cid == ch->cid)
324 /* could have been destroyed asynchronously, ignore message */
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326 "Channel %u not found\n",
332 case CS_CALLEE_RINGING:
333 ch->status = CS_CALLEE_CONNECTED;
335 case CS_CALLEE_CONNECTED:
337 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
339 case CS_CALLEE_SHUTDOWN:
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 "Ignoring client's PICKUP message, line is in SHUTDOWN\n");
342 GNUNET_SERVER_receive_done (client, GNUNET_OK);
344 case CS_CALLER_CALLING:
345 case CS_CALLER_CONNECTED:
346 case CS_CALLER_SHUTDOWN:
348 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
351 GNUNET_break (CS_CALLEE_CONNECTED == ch->status);
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "Sending PICK_UP message to mesh\n");
354 e = GNUNET_MQ_msg (mppm,
355 GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_PICK_UP);
356 GNUNET_MQ_send (ch->reliable_mq, e);
357 GNUNET_SERVER_receive_done (client, GNUNET_OK);
364 * @param ch channel to destroy.
367 destroy_line_mesh_channels (struct Channel *ch)
369 struct Line *line = ch->line;
370 struct GNUNET_MESH_Channel *t;
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "Destroying mesh channels\n");
374 if (NULL != ch->reliable_mq)
376 GNUNET_MQ_destroy (ch->reliable_mq);
377 ch->reliable_mq = NULL;
379 if (NULL != ch->unreliable_mth)
381 GNUNET_MESH_notify_transmit_ready_cancel (ch->unreliable_mth);
382 ch->unreliable_mth = NULL;
384 if (NULL != (t = ch->channel_unreliable))
386 ch->channel_unreliable = NULL;
387 GNUNET_MESH_channel_destroy (t);
389 if (NULL != (t = ch->channel_reliable))
391 ch->channel_reliable = NULL;
392 GNUNET_MESH_channel_destroy (t);
394 GNUNET_CONTAINER_DLL_remove (line->channel_head,
397 GNUNET_free_non_null (ch->audio_data);
403 * We are done signalling shutdown to the other peer. Close down
406 * @param cls the `struct Channel` to reset/terminate
409 mq_done_finish_caller_shutdown (void *cls)
411 struct Channel *ch = cls;
415 case CS_CALLEE_RINGING:
418 case CS_CALLEE_CONNECTED:
421 case CS_CALLEE_SHUTDOWN:
422 destroy_line_mesh_channels (ch);
424 case CS_CALLER_CALLING:
427 case CS_CALLER_CONNECTED:
430 case CS_CALLER_SHUTDOWN:
431 destroy_line_mesh_channels (ch);
438 * Function to handle a hangup request message from the client
440 * @param cls closure, NULL
441 * @param client the client from which the message is
442 * @param message the message from the client
445 handle_client_hangup_message (void *cls,
446 struct GNUNET_SERVER_Client *client,
447 const struct GNUNET_MessageHeader *message)
449 const struct ClientPhoneHangupMessage *msg;
450 struct GNUNET_MQ_Envelope *e;
451 struct MeshPhoneHangupMessage *mhum;
455 msg = (const struct ClientPhoneHangupMessage *) message;
456 line = GNUNET_SERVER_client_get_user_context (client, struct Line);
460 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
463 for (ch = line->channel_head; NULL != ch; ch = ch->next)
464 if (msg->cid == ch->cid)
468 /* could have been destroyed asynchronously, ignore message */
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
470 "Channel %u not found\n",
472 GNUNET_SERVER_receive_done (client, GNUNET_OK);
478 case CS_CALLEE_RINGING:
479 ch->status = CS_CALLEE_SHUTDOWN;
481 case CS_CALLEE_CONNECTED:
482 ch->status = CS_CALLEE_SHUTDOWN;
484 case CS_CALLEE_SHUTDOWN:
485 /* maybe the other peer closed asynchronously... */
487 case CS_CALLER_CALLING:
488 ch->status = CS_CALLER_SHUTDOWN;
490 case CS_CALLER_CONNECTED:
491 ch->status = CS_CALLER_SHUTDOWN;
493 case CS_CALLER_SHUTDOWN:
494 /* maybe the other peer closed asynchronously... */
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Sending HANG_UP message via mesh\n");
499 e = GNUNET_MQ_msg (mhum,
500 GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_HANG_UP);
501 GNUNET_MQ_notify_sent (e,
502 &mq_done_finish_caller_shutdown,
504 GNUNET_MQ_send (ch->reliable_mq, e);
505 GNUNET_SERVER_receive_done (client, GNUNET_OK);
510 * Function to handle a suspend request message from the client
512 * @param cls closure, NULL
513 * @param client the client from which the message is
514 * @param message the message from the client
517 handle_client_suspend_message (void *cls,
518 struct GNUNET_SERVER_Client *client,
519 const struct GNUNET_MessageHeader *message)
521 const struct ClientPhoneSuspendMessage *msg;
522 struct GNUNET_MQ_Envelope *e;
523 struct MeshPhoneSuspendMessage *mhum;
527 msg = (const struct ClientPhoneSuspendMessage *) message;
528 line = GNUNET_SERVER_client_get_user_context (client, struct Line);
532 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
535 for (ch = line->channel_head; NULL != ch; ch = ch->next)
536 if (msg->cid == ch->cid)
540 /* could have been destroyed asynchronously, ignore message */
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542 "Channel %u not found\n",
546 if (GNUNET_YES == ch->suspended_local)
549 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
554 case CS_CALLEE_RINGING:
556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
558 case CS_CALLEE_CONNECTED:
559 ch->suspended_local = GNUNET_YES;
561 case CS_CALLEE_SHUTDOWN:
562 /* maybe the other peer closed asynchronously... */
564 case CS_CALLER_CALLING:
566 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
568 case CS_CALLER_CONNECTED:
569 ch->suspended_local = GNUNET_YES;
571 case CS_CALLER_SHUTDOWN:
572 /* maybe the other peer closed asynchronously... */
575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576 "Sending SUSPEND message via mesh\n");
577 e = GNUNET_MQ_msg (mhum,
578 GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_SUSPEND);
579 GNUNET_MQ_send (ch->reliable_mq, e);
580 GNUNET_SERVER_receive_done (client, GNUNET_OK);
585 * Function to handle a resume request message from the client
587 * @param cls closure, NULL
588 * @param client the client from which the message is
589 * @param message the message from the client
592 handle_client_resume_message (void *cls,
593 struct GNUNET_SERVER_Client *client,
594 const struct GNUNET_MessageHeader *message)
596 const struct ClientPhoneResumeMessage *msg;
597 struct GNUNET_MQ_Envelope *e;
598 struct MeshPhoneResumeMessage *mhum;
602 msg = (const struct ClientPhoneResumeMessage *) message;
603 line = GNUNET_SERVER_client_get_user_context (client, struct Line);
607 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
610 for (ch = line->channel_head; NULL != ch; ch = ch->next)
611 if (msg->cid == ch->cid)
615 /* could have been destroyed asynchronously, ignore message */
616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
617 "Channel %u not found\n",
621 if (GNUNET_YES != ch->suspended_local)
624 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
629 case CS_CALLEE_RINGING:
631 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
633 case CS_CALLEE_CONNECTED:
634 ch->suspended_local = GNUNET_NO;
636 case CS_CALLEE_SHUTDOWN:
637 /* maybe the other peer closed asynchronously... */
639 case CS_CALLER_CALLING:
641 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
643 case CS_CALLER_CONNECTED:
644 ch->suspended_local = GNUNET_NO;
646 case CS_CALLER_SHUTDOWN:
647 /* maybe the other peer closed asynchronously... */
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
651 "Sending RESUME message via mesh\n");
652 e = GNUNET_MQ_msg (mhum,
653 GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_RESUME);
654 GNUNET_MQ_send (ch->reliable_mq, e);
655 GNUNET_SERVER_receive_done (client, GNUNET_OK);
660 * Function to handle call request from the client
662 * @param cls closure, NULL
663 * @param client the client from which the message is
664 * @param message the message from the client
667 handle_client_call_message (void *cls,
668 struct GNUNET_SERVER_Client *client,
669 const struct GNUNET_MessageHeader *message)
671 const struct ClientCallMessage *msg;
674 struct GNUNET_MQ_Envelope *e;
675 struct MeshPhoneRingMessage *ring;
677 msg = (const struct ClientCallMessage *) message;
678 line = GNUNET_SERVER_client_get_user_context (client, struct Line);
682 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
685 line = GNUNET_new (struct Line);
686 line->client = client;
687 line->local_line = (local_line_cnt++) | (1 << 31);
688 GNUNET_SERVER_client_set_user_context (client, line);
689 GNUNET_SERVER_notification_context_add (nc, client);
690 GNUNET_CONTAINER_DLL_insert (lines_head,
693 ch = GNUNET_new (struct Channel);
695 GNUNET_CONTAINER_DLL_insert (line->channel_head,
698 ch->target = msg->target;
699 ch->remote_line = ntohl (msg->line);
700 ch->status = CS_CALLER_CALLING;
701 ch->channel_reliable = GNUNET_MESH_channel_create (mesh,
704 GNUNET_APPLICATION_TYPE_CONVERSATION_CONTROL,
705 GNUNET_MESH_OPTION_RELIABLE);
706 ch->reliable_mq = GNUNET_MESH_mq_create (ch->channel_reliable);
707 e = GNUNET_MQ_msg (ring, GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_RING);
708 ring->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CONVERSATION_RING);
709 ring->purpose.size = htonl (sizeof (struct GNUNET_PeerIdentity) * 2 +
710 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
711 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
712 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
713 GNUNET_CRYPTO_ecdsa_key_get_public (&msg->caller_id,
715 ring->remote_line = msg->line;
716 ring->source_line = line->local_line;
717 ring->target = msg->target;
718 ring->source = my_identity;
719 ring->expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (RING_TIMEOUT));
720 GNUNET_CRYPTO_ecdsa_sign (&msg->caller_id,
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
724 "Sending RING message via mesh\n");
725 GNUNET_MQ_send (ch->reliable_mq, e);
726 GNUNET_SERVER_receive_done (client, GNUNET_OK);
731 * Transmit audio data via unreliable mesh channel.
733 * @param cls the `struct Channel` we are transmitting for
734 * @param size number of bytes available in @a buf
735 * @param buf where to copy the data
736 * @return number of bytes copied to @a buf
739 transmit_line_audio (void *cls,
743 struct Channel *ch = cls;
744 struct MeshAudioMessage *mam = buf;
746 ch->unreliable_mth = NULL;
747 if ( (NULL == buf) ||
748 (size < sizeof (struct MeshAudioMessage) + ch->audio_size) )
750 /* eh, other error handling? */
753 mam->header.size = htons (sizeof (struct MeshAudioMessage) + ch->audio_size);
754 mam->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_AUDIO);
755 mam->remote_line = htonl (ch->remote_line);
756 memcpy (&mam[1], ch->audio_data, ch->audio_size);
757 GNUNET_free (ch->audio_data);
758 ch->audio_data = NULL;
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760 "Sending %u bytes of audio data on line %u via mesh\n",
761 ch->audio_size, ch->remote_line);
762 return sizeof (struct MeshAudioMessage) + ch->audio_size;
767 * Function to handle audio data from the client
769 * @param cls closure, NULL
770 * @param client the client from which the message is
771 * @param message the message from the client
774 handle_client_audio_message (void *cls,
775 struct GNUNET_SERVER_Client *client,
776 const struct GNUNET_MessageHeader *message)
778 const struct ClientAudioMessage *msg;
783 size = ntohs (message->size) - sizeof (struct ClientAudioMessage);
784 msg = (const struct ClientAudioMessage *) message;
785 line = GNUNET_SERVER_client_get_user_context (client, struct Line);
789 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
792 for (ch = line->channel_head; NULL != ch; ch = ch->next)
793 if (msg->cid == ch->cid)
797 /* could have been destroyed asynchronously, ignore message */
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "Channel %u not found\n",
806 case CS_CALLEE_RINGING:
807 case CS_CALLER_CALLING:
809 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
811 case CS_CALLEE_CONNECTED:
812 case CS_CALLER_CONNECTED:
813 /* common case, handled below */
815 case CS_CALLEE_SHUTDOWN:
816 case CS_CALLER_SHUTDOWN:
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
818 "Mesh audio channel in shutdown; audio data dropped\n");
819 GNUNET_SERVER_receive_done (client, GNUNET_OK);
822 if (NULL == ch->channel_unreliable)
824 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
825 _("Mesh audio channel not ready; audio data dropped\n"));
826 GNUNET_SERVER_receive_done (client, GNUNET_OK);
829 if (NULL != ch->unreliable_mth)
831 /* NOTE: we may want to not do this and instead combine the data */
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
833 "Bandwidth insufficient; dropping previous audio data segment with %u bytes\n",
834 (unsigned int) ch->audio_size);
835 GNUNET_MESH_notify_transmit_ready_cancel (ch->unreliable_mth);
836 ch->unreliable_mth = NULL;
837 GNUNET_free (ch->audio_data);
838 ch->audio_data = NULL;
840 ch->audio_size = size;
841 ch->audio_data = GNUNET_malloc (ch->audio_size);
842 memcpy (ch->audio_data,
845 ch->unreliable_mth = GNUNET_MESH_notify_transmit_ready (ch->channel_unreliable,
847 GNUNET_TIME_UNIT_FOREVER_REL,
848 sizeof (struct MeshAudioMessage)
850 &transmit_line_audio,
852 GNUNET_SERVER_receive_done (client, GNUNET_OK);
857 * We are done signalling shutdown to the other peer.
858 * Destroy the channel.
860 * @param cls the `struct GNUNET_MESH_channel` to destroy
863 mq_done_destroy_channel (void *cls)
865 struct GNUNET_MESH_Channel *channel = cls;
867 GNUNET_MESH_channel_destroy (channel);
872 * Function to handle a ring message incoming over mesh
874 * @param cls closure, NULL
875 * @param channel the channel over which the message arrived
876 * @param channel_ctx the channel context, can be NULL
877 * or point to the `struct Channel`
878 * @param message the incoming message
882 handle_mesh_ring_message (void *cls,
883 struct GNUNET_MESH_Channel *channel,
885 const struct GNUNET_MessageHeader *message)
887 const struct MeshPhoneRingMessage *msg;
890 struct GNUNET_MQ_Envelope *e;
891 struct MeshPhoneHangupMessage *hang_up;
892 struct ClientPhoneRingMessage cring;
893 struct GNUNET_MQ_Handle *reliable_mq;
895 msg = (const struct MeshPhoneRingMessage *) message;
896 if ( (msg->purpose.size != htonl (sizeof (struct GNUNET_PeerIdentity) * 2 +
897 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
898 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
899 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) ||
901 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_CONVERSATION_RING,
907 return GNUNET_SYSERR;
909 for (line = lines_head; NULL != line; line = line->next)
910 if (line->local_line == ntohl (msg->remote_line))
914 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
915 _("No available phone for incoming call on line %u, sending HANG_UP signal\n"),
916 ntohl (msg->remote_line));
917 e = GNUNET_MQ_msg (hang_up,
918 GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_HANG_UP);
919 GNUNET_MQ_notify_sent (e,
920 &mq_done_destroy_channel,
922 reliable_mq = GNUNET_MESH_mq_create (channel);
923 GNUNET_MQ_send (reliable_mq, e);
924 /* FIXME: do we need to clean up reliable_mq somehow/somewhere? */
925 GNUNET_MESH_receive_done (channel); /* needed? */
928 ch = GNUNET_new (struct Channel);
930 GNUNET_CONTAINER_DLL_insert (line->channel_head,
933 ch->status = CS_CALLEE_RINGING;
934 ch->remote_line = ntohl (msg->source_line);
935 ch->channel_reliable = channel;
936 ch->reliable_mq = GNUNET_MESH_mq_create (ch->channel_reliable);
937 ch->cid = line->cid_gen++;
938 ch->target = msg->source;
940 cring.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RING);
941 cring.header.size = htons (sizeof (cring));
943 cring.caller_id = msg->caller_id;
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
945 "Sending RING message to client\n");
946 GNUNET_SERVER_notification_context_unicast (nc,
950 GNUNET_MESH_receive_done (channel);
956 * Function to handle a hangup message incoming over mesh
958 * @param cls closure, NULL
959 * @param channel the channel over which the message arrived
960 * @param channel_ctx the channel context, can be NULL
961 * or point to the `struct Channel`
962 * @param message the incoming message
966 handle_mesh_hangup_message (void *cls,
967 struct GNUNET_MESH_Channel *channel,
969 const struct GNUNET_MessageHeader *message)
971 struct Channel *ch = *channel_ctx;
973 struct ClientPhoneHangupMessage hup;
974 enum ChannelStatus status;
978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
979 "HANGUP message received for non-existing line, dropping channel.\n");
980 return GNUNET_SYSERR;
984 hup.header.size = htons (sizeof (hup));
985 hup.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP);
988 GNUNET_MESH_receive_done (channel);
989 destroy_line_mesh_channels (ch);
992 case CS_CALLEE_RINGING:
993 case CS_CALLEE_CONNECTED:
995 case CS_CALLEE_SHUTDOWN:
997 case CS_CALLER_CALLING:
998 case CS_CALLER_CONNECTED:
1000 case CS_CALLER_SHUTDOWN:
1003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1004 "Sending HANG UP message to client\n");
1005 GNUNET_SERVER_notification_context_unicast (nc,
1014 * Function to handle a pickup message incoming over mesh
1016 * @param cls closure, NULL
1017 * @param channel the channel over which the message arrived
1018 * @param channel_ctx the channel context, can be NULL
1019 * or point to the `struct Channel`
1020 * @param message the incoming message
1021 * @return #GNUNET_OK
1024 handle_mesh_pickup_message (void *cls,
1025 struct GNUNET_MESH_Channel *channel,
1027 const struct GNUNET_MessageHeader *message)
1029 struct Channel *ch = *channel_ctx;
1031 struct ClientPhonePickupMessage pick;
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 "PICKUP message received for non-existing channel, dropping channel.\n");
1037 return GNUNET_SYSERR;
1040 GNUNET_MESH_receive_done (channel);
1043 case CS_CALLEE_RINGING:
1044 case CS_CALLEE_CONNECTED:
1045 GNUNET_break_op (0);
1046 destroy_line_mesh_channels (ch);
1047 return GNUNET_SYSERR;
1048 case CS_CALLEE_SHUTDOWN:
1049 GNUNET_break_op (0);
1050 destroy_line_mesh_channels (ch);
1052 case CS_CALLER_CALLING:
1053 ch->status = CS_CALLER_CONNECTED;
1055 case CS_CALLER_CONNECTED:
1056 GNUNET_break_op (0);
1058 case CS_CALLER_SHUTDOWN:
1059 GNUNET_break_op (0);
1060 mq_done_finish_caller_shutdown (ch);
1061 return GNUNET_SYSERR;
1063 pick.header.size = htons (sizeof (pick));
1064 pick.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_PICKED_UP);
1066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1067 "Sending PICKED UP message to client\n");
1068 GNUNET_SERVER_notification_context_unicast (nc,
1072 ch->channel_unreliable = GNUNET_MESH_channel_create (mesh,
1075 GNUNET_APPLICATION_TYPE_CONVERSATION_AUDIO,
1076 GNUNET_MESH_OPTION_DEFAULT);
1077 if (NULL == ch->channel_unreliable)
1086 * Function to handle a suspend message incoming over mesh
1088 * @param cls closure, NULL
1089 * @param channel the channel over which the message arrived
1090 * @param channel_ctx the channel context, can be NULL
1091 * or point to the `struct Channel`
1092 * @param message the incoming message
1093 * @return #GNUNET_OK
1096 handle_mesh_suspend_message (void *cls,
1097 struct GNUNET_MESH_Channel *channel,
1099 const struct GNUNET_MessageHeader *message)
1101 struct Channel *ch = *channel_ctx;
1103 struct ClientPhoneSuspendMessage suspend;
1107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1108 "SUSPEND message received for non-existing line, dropping channel.\n");
1109 return GNUNET_SYSERR;
1112 suspend.header.size = htons (sizeof (suspend));
1113 suspend.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND);
1114 suspend.cid = ch->cid;
1115 GNUNET_MESH_receive_done (channel);
1118 case CS_CALLEE_RINGING:
1119 GNUNET_break_op (0);
1121 case CS_CALLEE_CONNECTED:
1122 ch->suspended_remote = GNUNET_YES;
1124 case CS_CALLEE_SHUTDOWN:
1126 case CS_CALLER_CALLING:
1127 GNUNET_break_op (0);
1129 case CS_CALLER_CONNECTED:
1130 ch->suspended_remote = GNUNET_YES;
1132 case CS_CALLER_SHUTDOWN:
1135 GNUNET_SERVER_notification_context_unicast (nc,
1144 * Function to handle a resume message incoming over mesh
1146 * @param cls closure, NULL
1147 * @param channel the channel over which the message arrived
1148 * @param channel_ctx the channel context, can be NULL
1149 * or point to the `struct Channel`
1150 * @param message the incoming message
1151 * @return #GNUNET_OK
1154 handle_mesh_resume_message (void *cls,
1155 struct GNUNET_MESH_Channel *channel,
1157 const struct GNUNET_MessageHeader *message)
1159 struct Channel *ch = *channel_ctx;
1161 struct ClientPhoneResumeMessage resume;
1165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1166 "RESUME message received for non-existing line, dropping channel.\n");
1167 return GNUNET_SYSERR;
1170 resume.header.size = htons (sizeof (resume));
1171 resume.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME);
1172 resume.cid = ch->cid;
1173 GNUNET_MESH_receive_done (channel);
1174 if (GNUNET_YES != ch->suspended_remote)
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1177 "RESUME message received for non-suspended channel, dropping channel.\n");
1178 return GNUNET_SYSERR;
1182 case CS_CALLEE_RINGING:
1185 case CS_CALLEE_CONNECTED:
1186 ch->suspended_remote = GNUNET_NO;
1188 case CS_CALLEE_SHUTDOWN:
1190 case CS_CALLER_CALLING:
1193 case CS_CALLER_CONNECTED:
1194 ch->suspended_remote = GNUNET_NO;
1196 case CS_CALLER_SHUTDOWN:
1199 GNUNET_SERVER_notification_context_unicast (nc,
1208 * Function to handle an audio message incoming over mesh
1210 * @param cls closure, NULL
1211 * @param channel the channel over which the message arrived
1212 * @param channel_ctx the channel context, can be NULL
1213 * or point to the `struct Channel`
1214 * @param message the incoming message
1215 * @return #GNUNET_OK
1218 handle_mesh_audio_message (void *cls,
1219 struct GNUNET_MESH_Channel *channel,
1221 const struct GNUNET_MessageHeader *message)
1223 const struct MeshAudioMessage *msg;
1224 struct Channel *ch = *channel_ctx;
1226 struct GNUNET_PeerIdentity sender;
1227 size_t msize = ntohs (message->size) - sizeof (struct MeshAudioMessage);
1228 char buf[msize + sizeof (struct ClientAudioMessage)];
1229 struct ClientAudioMessage *cam;
1230 const union GNUNET_MESH_ChannelInfo *info;
1232 msg = (const struct MeshAudioMessage *) message;
1235 info = GNUNET_MESH_channel_get_info (channel,
1236 GNUNET_MESH_OPTION_PEER);
1242 sender = info->peer;
1243 for (line = lines_head; NULL != line; line = line->next)
1244 if (line->local_line == ntohl (msg->remote_line))
1246 for (ch = line->channel_head; NULL != ch; ch = ch->next)
1248 if ( (CS_CALLEE_CONNECTED == ch->status) &&
1249 (0 == memcmp (&ch->target,
1251 sizeof (struct GNUNET_PeerIdentity))) &&
1252 (NULL == ch->channel_unreliable) )
1258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1259 "Received AUDIO data for non-existing line %u, dropping.\n",
1260 ntohl (msg->remote_line));
1261 return GNUNET_SYSERR;
1263 ch->channel_unreliable = channel;
1266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1267 "Forwarding %u bytes of AUDIO data to client\n",
1269 cam = (struct ClientAudioMessage *) buf;
1270 cam->header.size = htons (sizeof (buf));
1271 cam->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO);
1273 memcpy (&cam[1], &msg[1], msize);
1274 GNUNET_SERVER_notification_context_unicast (nc,
1278 GNUNET_MESH_receive_done (channel);
1284 * Method called whenever another peer has added us to a channel
1285 * the other peer initiated.
1287 * @param cls closure
1288 * @param channel new handle to the channel
1289 * @param initiator peer that started the channel
1291 * @param options channel option flags
1292 * @return initial channel context for the channel;
1293 * (can be NULL -- that's not an error)
1296 inbound_channel (void *cls,
1297 struct GNUNET_MESH_Channel *channel,
1298 const struct GNUNET_PeerIdentity *initiator,
1299 uint32_t port, enum GNUNET_MESH_ChannelOption options)
1301 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1302 _("Received incoming channel on port %u\n"),
1303 (unsigned int) port);
1309 * Function called whenever an inbound channel is destroyed. Should clean up
1310 * any associated state.
1312 * @param cls closure (set from #GNUNET_MESH_connect)
1313 * @param channel connection to the other end (henceforth invalid)
1314 * @param channel_ctx place where local state associated
1315 * with the channel is stored;
1316 * may point to the `struct Channel`
1319 inbound_end (void *cls,
1320 const struct GNUNET_MESH_Channel *channel,
1323 struct Channel *ch = channel_ctx;
1325 struct ClientPhoneHangupMessage hup;
1329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1330 "Mesh channel destroyed, but channel is unknown to us\n");
1334 if (ch->channel_unreliable == channel)
1336 if (NULL != ch->unreliable_mth)
1338 GNUNET_MESH_notify_transmit_ready_cancel (ch->unreliable_mth);
1339 ch->unreliable_mth = NULL;
1341 ch->channel_unreliable = NULL;
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1343 "Unreliable channel destroyed\n");
1346 if (ch->channel_reliable != channel)
1348 /* recursive call, I'm the one destroying 'ch' right now */
1351 ch->channel_reliable = NULL;
1353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1354 "Mesh channel destroyed by mesh in state %d\n",
1356 hup.header.size = htons (sizeof (hup));
1357 hup.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP);
1361 case CS_CALLEE_RINGING:
1362 case CS_CALLEE_CONNECTED:
1363 GNUNET_SERVER_notification_context_unicast (nc,
1368 case CS_CALLEE_SHUTDOWN:
1370 case CS_CALLER_CALLING:
1371 case CS_CALLER_CONNECTED:
1372 GNUNET_SERVER_notification_context_unicast (nc,
1377 case CS_CALLER_SHUTDOWN:
1380 destroy_line_mesh_channels (ch);
1385 * A client disconnected. Remove all of its data structure entries.
1387 * @param cls closure, NULL
1388 * @param client identification of the client
1391 handle_client_disconnect (void *cls,
1392 struct GNUNET_SERVER_Client *client)
1398 line = GNUNET_SERVER_client_get_user_context (client, struct Line);
1401 GNUNET_SERVER_client_set_user_context (client, (void *)NULL);
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403 "Client disconnected, closing line\n");
1404 GNUNET_CONTAINER_DLL_remove (lines_head,
1407 while (NULL != line->channel_head)
1408 destroy_line_mesh_channels (line->channel_head);
1416 * @param cls closure, NULL
1417 * @param tc the task context
1420 do_shutdown (void *cls,
1421 const struct GNUNET_SCHEDULER_TaskContext *tc)
1426 while (NULL != (line = lines_head))
1428 while (NULL != (ch = line->channel_head))
1429 destroy_line_mesh_channels (ch);
1430 GNUNET_CONTAINER_DLL_remove (lines_head,
1433 GNUNET_SERVER_client_set_user_context (line->client, (void *) NULL);
1438 GNUNET_MESH_disconnect (mesh);
1443 GNUNET_SERVER_notification_context_destroy (nc);
1450 * Main function that will be run by the scheduler.
1452 * @param cls closure
1453 * @param server server handle
1454 * @param c configuration
1458 struct GNUNET_SERVER_Handle *server,
1459 const struct GNUNET_CONFIGURATION_Handle *c)
1461 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1462 {&handle_client_register_message, NULL,
1463 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_REGISTER,
1464 sizeof (struct ClientPhoneRegisterMessage)},
1465 {&handle_client_pickup_message, NULL,
1466 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_PICK_UP,
1467 sizeof (struct ClientPhonePickupMessage) },
1468 {&handle_client_suspend_message, NULL,
1469 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND,
1470 sizeof (struct ClientPhoneSuspendMessage) },
1471 {&handle_client_resume_message, NULL,
1472 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME,
1473 sizeof (struct ClientPhoneResumeMessage) },
1474 {&handle_client_hangup_message, NULL,
1475 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP,
1476 sizeof (struct ClientPhoneHangupMessage) },
1477 {&handle_client_call_message, NULL,
1478 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_CALL,
1479 sizeof (struct ClientCallMessage) },
1480 {&handle_client_audio_message, NULL,
1481 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO,
1485 static struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1486 {&handle_mesh_ring_message,
1487 GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_RING,
1488 sizeof (struct MeshPhoneRingMessage)},
1489 {&handle_mesh_hangup_message,
1490 GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_HANG_UP,
1491 sizeof (struct MeshPhoneHangupMessage)},
1492 {&handle_mesh_pickup_message,
1493 GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_PICK_UP,
1494 sizeof (struct MeshPhonePickupMessage)},
1495 {&handle_mesh_suspend_message,
1496 GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_SUSPEND,
1497 sizeof (struct MeshPhoneSuspendMessage)},
1498 {&handle_mesh_resume_message,
1499 GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_RESUME,
1500 sizeof (struct MeshPhoneResumeMessage)},
1501 {&handle_mesh_audio_message, GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_AUDIO,
1505 static uint32_t ports[] = {
1506 GNUNET_APPLICATION_TYPE_CONVERSATION_CONTROL,
1507 GNUNET_APPLICATION_TYPE_CONVERSATION_AUDIO,
1512 GNUNET_assert (GNUNET_OK ==
1513 GNUNET_CRYPTO_get_peer_identity (cfg,
1515 mesh = GNUNET_MESH_connect (cfg,
1525 GNUNET_SCHEDULER_shutdown ();
1528 nc = GNUNET_SERVER_notification_context_create (server, 16);
1529 GNUNET_SERVER_add_handlers (server, server_handlers);
1530 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1531 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1538 * The main function for the conversation service.
1540 * @param argc number of arguments from the command line
1541 * @param argv command line arguments
1542 * @return 0 ok, 1 on error
1548 return (GNUNET_OK ==
1549 GNUNET_SERVICE_run (argc, argv,
1551 GNUNET_SERVICE_OPTION_NONE,
1552 &run, NULL)) ? 0 : 1;
1555 /* end of gnunet-service-conversation.c */