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_cadet_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 cadet channel (or, if it is an
48 * open line, is waiting for a cadet 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 cadet 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_CADET_Channel *channel_reliable;
120 * Handle for unreliable channel (audio data)
122 struct GNUNET_CADET_Channel *channel_unreliable;
125 * Transmit handle for pending audio messages
127 struct GNUNET_CADET_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 cadet 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_CADET_Handle *cadet;
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 CadetPhonePickupMessage *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",
328 GNUNET_SERVER_receive_done (client, GNUNET_YES);
333 case CS_CALLEE_RINGING:
334 ch->status = CS_CALLEE_CONNECTED;
336 case CS_CALLEE_CONNECTED:
338 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
340 case CS_CALLEE_SHUTDOWN:
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342 "Ignoring client's PICKUP message, line is in SHUTDOWN\n");
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 cadet\n");
354 e = GNUNET_MQ_msg (mppm,
355 GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_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_cadet_channels (struct Channel *ch)
369 struct Line *line = ch->line;
370 struct GNUNET_CADET_Channel *t;
372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373 "Destroying cadet 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_CADET_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_CADET_channel_destroy (t);
389 if (NULL != (t = ch->channel_reliable))
391 ch->channel_reliable = NULL;
392 GNUNET_CADET_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_cadet_channels (ch);
424 case CS_CALLER_CALLING:
427 case CS_CALLER_CONNECTED:
430 case CS_CALLER_SHUTDOWN:
431 destroy_line_cadet_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 CadetPhoneHangupMessage *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);
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476 "Received HANGUP for channel %u which is in state %d\n",
481 case CS_CALLEE_RINGING:
482 ch->status = CS_CALLEE_SHUTDOWN;
484 case CS_CALLEE_CONNECTED:
485 ch->status = CS_CALLEE_SHUTDOWN;
487 case CS_CALLEE_SHUTDOWN:
488 /* maybe the other peer closed asynchronously... */
489 GNUNET_SERVER_receive_done (client, GNUNET_OK);
491 case CS_CALLER_CALLING:
492 ch->status = CS_CALLER_SHUTDOWN;
494 case CS_CALLER_CONNECTED:
495 ch->status = CS_CALLER_SHUTDOWN;
497 case CS_CALLER_SHUTDOWN:
498 /* maybe the other peer closed asynchronously... */
499 GNUNET_SERVER_receive_done (client, GNUNET_OK);
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Sending HANG_UP message via cadet\n");
504 e = GNUNET_MQ_msg (mhum,
505 GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_PHONE_HANG_UP);
506 GNUNET_MQ_notify_sent (e,
507 &mq_done_finish_caller_shutdown,
509 GNUNET_MQ_send (ch->reliable_mq, e);
510 GNUNET_SERVER_receive_done (client, GNUNET_OK);
515 * Function to handle a suspend request message from the client
517 * @param cls closure, NULL
518 * @param client the client from which the message is
519 * @param message the message from the client
522 handle_client_suspend_message (void *cls,
523 struct GNUNET_SERVER_Client *client,
524 const struct GNUNET_MessageHeader *message)
526 const struct ClientPhoneSuspendMessage *msg;
527 struct GNUNET_MQ_Envelope *e;
528 struct CadetPhoneSuspendMessage *mhum;
532 msg = (const struct ClientPhoneSuspendMessage *) message;
533 line = GNUNET_SERVER_client_get_user_context (client, struct Line);
537 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
540 for (ch = line->channel_head; NULL != ch; ch = ch->next)
541 if (msg->cid == ch->cid)
545 /* could have been destroyed asynchronously, ignore message */
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "Channel %u not found\n",
549 GNUNET_SERVER_receive_done (client, GNUNET_OK);
552 if (GNUNET_YES == ch->suspended_local)
555 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
559 "Received SUSPEND for channel %u which is in state %d\n",
564 case CS_CALLEE_RINGING:
566 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
568 case CS_CALLEE_CONNECTED:
569 ch->suspended_local = GNUNET_YES;
571 case CS_CALLEE_SHUTDOWN:
572 /* maybe the other peer closed asynchronously... */
573 GNUNET_SERVER_receive_done (client, GNUNET_OK);
575 case CS_CALLER_CALLING:
577 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
579 case CS_CALLER_CONNECTED:
580 ch->suspended_local = GNUNET_YES;
582 case CS_CALLER_SHUTDOWN:
583 /* maybe the other peer closed asynchronously... */
584 GNUNET_SERVER_receive_done (client, GNUNET_OK);
587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
588 "Sending SUSPEND message via cadet\n");
589 e = GNUNET_MQ_msg (mhum,
590 GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_PHONE_SUSPEND);
591 GNUNET_MQ_send (ch->reliable_mq, e);
592 GNUNET_SERVER_receive_done (client, GNUNET_OK);
597 * Function to handle a resume request message from the client
599 * @param cls closure, NULL
600 * @param client the client from which the message is
601 * @param message the message from the client
604 handle_client_resume_message (void *cls,
605 struct GNUNET_SERVER_Client *client,
606 const struct GNUNET_MessageHeader *message)
608 const struct ClientPhoneResumeMessage *msg;
609 struct GNUNET_MQ_Envelope *e;
610 struct CadetPhoneResumeMessage *mhum;
614 msg = (const struct ClientPhoneResumeMessage *) message;
615 line = GNUNET_SERVER_client_get_user_context (client, struct Line);
619 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
622 for (ch = line->channel_head; NULL != ch; ch = ch->next)
623 if (msg->cid == ch->cid)
627 /* could have been destroyed asynchronously, ignore message */
628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
629 "Channel %u not found\n",
631 GNUNET_SERVER_receive_done (client, GNUNET_OK);
634 if (GNUNET_YES != ch->suspended_local)
637 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
641 "Received RESUME for channel %u which is in state %d\n",
646 case CS_CALLEE_RINGING:
648 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
650 case CS_CALLEE_CONNECTED:
651 ch->suspended_local = GNUNET_NO;
653 case CS_CALLEE_SHUTDOWN:
654 /* maybe the other peer closed asynchronously... */
655 GNUNET_SERVER_receive_done (client, GNUNET_OK);
657 case CS_CALLER_CALLING:
659 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
661 case CS_CALLER_CONNECTED:
662 ch->suspended_local = GNUNET_NO;
664 case CS_CALLER_SHUTDOWN:
665 /* maybe the other peer closed asynchronously... */
666 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
670 "Sending RESUME message via cadet\n");
671 e = GNUNET_MQ_msg (mhum,
672 GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_PHONE_RESUME);
673 GNUNET_MQ_send (ch->reliable_mq, e);
674 GNUNET_SERVER_receive_done (client, GNUNET_OK);
679 * Function to handle call request from the client
681 * @param cls closure, NULL
682 * @param client the client from which the message is
683 * @param message the message from the client
686 handle_client_call_message (void *cls,
687 struct GNUNET_SERVER_Client *client,
688 const struct GNUNET_MessageHeader *message)
690 const struct ClientCallMessage *msg;
693 struct GNUNET_MQ_Envelope *e;
694 struct CadetPhoneRingMessage *ring;
696 msg = (const struct ClientCallMessage *) message;
697 line = GNUNET_SERVER_client_get_user_context (client, struct Line);
701 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
704 line = GNUNET_new (struct Line);
705 line->client = client;
706 line->local_line = (local_line_cnt++) | (1 << 31);
707 GNUNET_SERVER_client_set_user_context (client, line);
708 GNUNET_SERVER_notification_context_add (nc, client);
709 GNUNET_CONTAINER_DLL_insert (lines_head,
712 ch = GNUNET_new (struct Channel);
714 GNUNET_CONTAINER_DLL_insert (line->channel_head,
717 ch->target = msg->target;
718 ch->remote_line = ntohl (msg->line);
719 ch->status = CS_CALLER_CALLING;
720 ch->channel_reliable = GNUNET_CADET_channel_create (cadet,
723 GNUNET_APPLICATION_TYPE_CONVERSATION_CONTROL,
724 GNUNET_CADET_OPTION_RELIABLE);
725 ch->reliable_mq = GNUNET_CADET_mq_create (ch->channel_reliable);
726 e = GNUNET_MQ_msg (ring, GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_PHONE_RING);
727 ring->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CONVERSATION_RING);
728 ring->purpose.size = htonl (sizeof (struct GNUNET_PeerIdentity) * 2 +
729 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
730 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
731 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
732 GNUNET_CRYPTO_ecdsa_key_get_public (&msg->caller_id,
734 ring->remote_line = msg->line;
735 ring->source_line = htonl (line->local_line);
736 ring->target = msg->target;
737 ring->source = my_identity;
738 ring->expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (RING_TIMEOUT));
739 GNUNET_CRYPTO_ecdsa_sign (&msg->caller_id,
742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
743 "Sending RING message via cadet\n");
744 GNUNET_MQ_send (ch->reliable_mq, e);
745 GNUNET_SERVER_receive_done (client, GNUNET_OK);
750 * Transmit audio data via unreliable cadet channel.
752 * @param cls the `struct Channel` we are transmitting for
753 * @param size number of bytes available in @a buf
754 * @param buf where to copy the data
755 * @return number of bytes copied to @a buf
758 transmit_line_audio (void *cls,
762 struct Channel *ch = cls;
763 struct CadetAudioMessage *mam = buf;
765 ch->unreliable_mth = NULL;
766 if ( (NULL == buf) ||
767 (size < sizeof (struct CadetAudioMessage) + ch->audio_size) )
769 /* eh, other error handling? */
772 mam->header.size = htons (sizeof (struct CadetAudioMessage) + ch->audio_size);
773 mam->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_AUDIO);
774 mam->remote_line = htonl (ch->remote_line);
775 mam->source_line = htonl (ch->line->local_line);
776 memcpy (&mam[1], ch->audio_data, ch->audio_size);
777 GNUNET_free (ch->audio_data);
778 ch->audio_data = NULL;
779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
780 "Sending %u bytes of audio data from line %u to remote line %u via cadet\n",
781 ch->audio_size, ch->line->local_line, ch->remote_line);
782 return sizeof (struct CadetAudioMessage) + ch->audio_size;
787 * Function to handle audio data from the client
789 * @param cls closure, NULL
790 * @param client the client from which the message is
791 * @param message the message from the client
794 handle_client_audio_message (void *cls,
795 struct GNUNET_SERVER_Client *client,
796 const struct GNUNET_MessageHeader *message)
798 const struct ClientAudioMessage *msg;
803 size = ntohs (message->size) - sizeof (struct ClientAudioMessage);
804 msg = (const struct ClientAudioMessage *) message;
805 line = GNUNET_SERVER_client_get_user_context (client, struct Line);
809 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
812 for (ch = line->channel_head; NULL != ch; ch = ch->next)
813 if (msg->cid == ch->cid)
817 /* could have been destroyed asynchronously, ignore message */
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
819 "Channel %u not found\n",
821 GNUNET_SERVER_receive_done (client, GNUNET_OK);
827 case CS_CALLEE_RINGING:
828 case CS_CALLER_CALLING:
830 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
832 case CS_CALLEE_CONNECTED:
833 case CS_CALLER_CONNECTED:
834 /* common case, handled below */
836 case CS_CALLEE_SHUTDOWN:
837 case CS_CALLER_SHUTDOWN:
838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
839 "Cadet audio channel in shutdown; audio data dropped\n");
840 GNUNET_SERVER_receive_done (client, GNUNET_OK);
843 if (GNUNET_YES == ch->suspended_local)
845 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "This channel is suspended locally\n");
846 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
849 if (NULL == ch->channel_unreliable)
851 GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
852 _("Cadet audio channel not ready; audio data dropped\n"));
853 GNUNET_SERVER_receive_done (client, GNUNET_OK);
856 if (NULL != ch->unreliable_mth)
858 /* NOTE: we may want to not do this and instead combine the data */
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "Bandwidth insufficient; dropping previous audio data segment with %u bytes\n",
861 (unsigned int) ch->audio_size);
862 GNUNET_CADET_notify_transmit_ready_cancel (ch->unreliable_mth);
863 ch->unreliable_mth = NULL;
864 GNUNET_free (ch->audio_data);
865 ch->audio_data = NULL;
867 ch->audio_size = size;
868 ch->audio_data = GNUNET_malloc (ch->audio_size);
869 memcpy (ch->audio_data,
872 ch->unreliable_mth = GNUNET_CADET_notify_transmit_ready (ch->channel_unreliable,
874 GNUNET_TIME_UNIT_FOREVER_REL,
875 sizeof (struct CadetAudioMessage)
877 &transmit_line_audio,
879 GNUNET_SERVER_receive_done (client, GNUNET_OK);
884 * We are done signalling shutdown to the other peer.
885 * Destroy the channel.
887 * @param cls the `struct GNUNET_CADET_channel` to destroy
890 mq_done_destroy_channel (void *cls)
892 struct GNUNET_CADET_Channel *channel = cls;
894 GNUNET_CADET_channel_destroy (channel);
899 * Function to handle a ring message incoming over cadet
901 * @param cls closure, NULL
902 * @param channel the channel over which the message arrived
903 * @param channel_ctx the channel context, can be NULL
904 * or point to the `struct Channel`
905 * @param message the incoming message
909 handle_cadet_ring_message (void *cls,
910 struct GNUNET_CADET_Channel *channel,
912 const struct GNUNET_MessageHeader *message)
914 const struct CadetPhoneRingMessage *msg;
917 struct GNUNET_MQ_Envelope *e;
918 struct CadetPhoneHangupMessage *hang_up;
919 struct ClientPhoneRingMessage cring;
920 struct GNUNET_MQ_Handle *reliable_mq;
922 msg = (const struct CadetPhoneRingMessage *) message;
923 if ( (msg->purpose.size != htonl (sizeof (struct GNUNET_PeerIdentity) * 2 +
924 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
925 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
926 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) ||
928 GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_CONVERSATION_RING,
934 return GNUNET_SYSERR;
936 GNUNET_CADET_receive_done (channel); /* needed? */
937 for (line = lines_head; NULL != line; line = line->next)
938 if (line->local_line == ntohl (msg->remote_line))
942 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
943 _("No available phone for incoming call on line %u, sending HANG_UP signal\n"),
944 ntohl (msg->remote_line));
945 e = GNUNET_MQ_msg (hang_up,
946 GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_PHONE_HANG_UP);
947 GNUNET_MQ_notify_sent (e,
948 &mq_done_destroy_channel,
950 reliable_mq = GNUNET_CADET_mq_create (channel);
951 GNUNET_MQ_send (reliable_mq, e);
952 /* FIXME: do we need to clean up reliable_mq somehow/somewhere? */
955 ch = GNUNET_new (struct Channel);
957 GNUNET_CONTAINER_DLL_insert (line->channel_head,
960 ch->status = CS_CALLEE_RINGING;
961 ch->remote_line = ntohl (msg->source_line);
962 ch->channel_reliable = channel;
963 ch->reliable_mq = GNUNET_CADET_mq_create (ch->channel_reliable);
964 ch->cid = line->cid_gen++;
965 ch->target = msg->source;
967 cring.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RING);
968 cring.header.size = htons (sizeof (cring));
970 cring.caller_id = msg->caller_id;
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
972 "Sending RING message to client. CID %u:(%u, %u)\n",
973 ch->cid, ch->remote_line, line->local_line);
974 GNUNET_SERVER_notification_context_unicast (nc,
983 * Function to handle a hangup message incoming over cadet
985 * @param cls closure, NULL
986 * @param channel the channel over which the message arrived
987 * @param channel_ctx the channel context, can be NULL
988 * or point to the `struct Channel`
989 * @param message the incoming message
993 handle_cadet_hangup_message (void *cls,
994 struct GNUNET_CADET_Channel *channel,
996 const struct GNUNET_MessageHeader *message)
998 struct Channel *ch = *channel_ctx;
1000 struct ClientPhoneHangupMessage hup;
1001 enum ChannelStatus status;
1005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1006 "HANGUP message received for non-existing line, dropping channel.\n");
1007 return GNUNET_SYSERR;
1010 *channel_ctx = NULL;
1011 hup.header.size = htons (sizeof (hup));
1012 hup.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP);
1014 status = ch->status;
1015 GNUNET_CADET_receive_done (channel);
1016 destroy_line_cadet_channels (ch);
1019 case CS_CALLEE_RINGING:
1020 case CS_CALLEE_CONNECTED:
1022 case CS_CALLEE_SHUTDOWN:
1024 case CS_CALLER_CALLING:
1025 case CS_CALLER_CONNECTED:
1027 case CS_CALLER_SHUTDOWN:
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1031 "Sending HANG UP message to client\n");
1032 GNUNET_SERVER_notification_context_unicast (nc,
1041 * Function to handle a pickup message incoming over cadet
1043 * @param cls closure, NULL
1044 * @param channel the channel over which the message arrived
1045 * @param channel_ctx the channel context, can be NULL
1046 * or point to the `struct Channel`
1047 * @param message the incoming message
1048 * @return #GNUNET_OK
1051 handle_cadet_pickup_message (void *cls,
1052 struct GNUNET_CADET_Channel *channel,
1054 const struct GNUNET_MessageHeader *message)
1056 struct Channel *ch = *channel_ctx;
1058 struct ClientPhonePickupMessage pick;
1062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1063 "PICKUP message received for non-existing channel, dropping channel.\n");
1064 return GNUNET_SYSERR;
1067 GNUNET_CADET_receive_done (channel);
1070 case CS_CALLEE_RINGING:
1071 case CS_CALLEE_CONNECTED:
1072 GNUNET_break_op (0);
1073 destroy_line_cadet_channels (ch);
1074 return GNUNET_SYSERR;
1075 case CS_CALLEE_SHUTDOWN:
1076 GNUNET_break_op (0);
1077 destroy_line_cadet_channels (ch);
1079 case CS_CALLER_CALLING:
1080 ch->status = CS_CALLER_CONNECTED;
1082 case CS_CALLER_CONNECTED:
1083 GNUNET_break_op (0);
1085 case CS_CALLER_SHUTDOWN:
1086 GNUNET_break_op (0);
1087 mq_done_finish_caller_shutdown (ch);
1088 return GNUNET_SYSERR;
1090 pick.header.size = htons (sizeof (pick));
1091 pick.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_PICKED_UP);
1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1094 "Sending PICKED UP message to client\n");
1095 GNUNET_SERVER_notification_context_unicast (nc,
1099 ch->channel_unreliable = GNUNET_CADET_channel_create (cadet,
1102 GNUNET_APPLICATION_TYPE_CONVERSATION_AUDIO,
1103 GNUNET_CADET_OPTION_DEFAULT);
1104 if (NULL == ch->channel_unreliable)
1113 * Function to handle a suspend message incoming over cadet
1115 * @param cls closure, NULL
1116 * @param channel the channel over which the message arrived
1117 * @param channel_ctx the channel context, can be NULL
1118 * or point to the `struct Channel`
1119 * @param message the incoming message
1120 * @return #GNUNET_OK
1123 handle_cadet_suspend_message (void *cls,
1124 struct GNUNET_CADET_Channel *channel,
1126 const struct GNUNET_MessageHeader *message)
1128 struct Channel *ch = *channel_ctx;
1130 struct ClientPhoneSuspendMessage suspend;
1134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1135 "SUSPEND message received for non-existing line, dropping channel.\n");
1136 return GNUNET_SYSERR;
1139 suspend.header.size = htons (sizeof (suspend));
1140 suspend.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND);
1141 suspend.cid = ch->cid;
1142 GNUNET_CADET_receive_done (channel);
1143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1144 "Suspending channel CID: %u(%u:%u)\n",
1145 ch->cid, ch->remote_line, line->local_line);
1148 case CS_CALLEE_RINGING:
1149 GNUNET_break_op (0);
1151 case CS_CALLEE_CONNECTED:
1152 ch->suspended_remote = GNUNET_YES;
1154 case CS_CALLEE_SHUTDOWN:
1156 case CS_CALLER_CALLING:
1157 GNUNET_break_op (0);
1159 case CS_CALLER_CONNECTED:
1160 ch->suspended_remote = GNUNET_YES;
1162 case CS_CALLER_SHUTDOWN:
1165 GNUNET_SERVER_notification_context_unicast (nc,
1174 * Function to handle a resume message incoming over cadet
1176 * @param cls closure, NULL
1177 * @param channel the channel over which the message arrived
1178 * @param channel_ctx the channel context, can be NULL
1179 * or point to the `struct Channel`
1180 * @param message the incoming message
1181 * @return #GNUNET_OK
1184 handle_cadet_resume_message (void *cls,
1185 struct GNUNET_CADET_Channel *channel,
1187 const struct GNUNET_MessageHeader *message)
1189 struct Channel *ch = *channel_ctx;
1191 struct ClientPhoneResumeMessage resume;
1195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1196 "RESUME message received for non-existing line, dropping channel.\n");
1197 return GNUNET_SYSERR;
1200 resume.header.size = htons (sizeof (resume));
1201 resume.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME);
1202 resume.cid = ch->cid;
1203 GNUNET_CADET_receive_done (channel);
1204 if (GNUNET_YES != ch->suspended_remote)
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 "RESUME message received for non-suspended channel, dropping channel.\n");
1208 return GNUNET_SYSERR;
1212 case CS_CALLEE_RINGING:
1215 case CS_CALLEE_CONNECTED:
1216 ch->suspended_remote = GNUNET_NO;
1218 case CS_CALLEE_SHUTDOWN:
1220 case CS_CALLER_CALLING:
1223 case CS_CALLER_CONNECTED:
1224 ch->suspended_remote = GNUNET_NO;
1226 case CS_CALLER_SHUTDOWN:
1229 GNUNET_SERVER_notification_context_unicast (nc,
1238 * Function to handle an audio message incoming over cadet
1240 * @param cls closure, NULL
1241 * @param channel the channel over which the message arrived
1242 * @param channel_ctx the channel context, can be NULL
1243 * or point to the `struct Channel`
1244 * @param message the incoming message
1245 * @return #GNUNET_OK
1248 handle_cadet_audio_message (void *cls,
1249 struct GNUNET_CADET_Channel *channel,
1251 const struct GNUNET_MessageHeader *message)
1253 const struct CadetAudioMessage *msg;
1254 struct Channel *ch = *channel_ctx;
1256 struct GNUNET_PeerIdentity sender;
1257 size_t msize = ntohs (message->size) - sizeof (struct CadetAudioMessage);
1258 char buf[msize + sizeof (struct ClientAudioMessage)];
1259 struct ClientAudioMessage *cam;
1260 const union GNUNET_CADET_ChannelInfo *info;
1262 msg = (const struct CadetAudioMessage *) message;
1265 info = GNUNET_CADET_channel_get_info (channel,
1266 GNUNET_CADET_OPTION_PEER);
1270 return GNUNET_SYSERR;
1272 sender = info->peer;
1273 for (line = lines_head; NULL != line; line = line->next)
1274 if (line->local_line == ntohl (msg->remote_line))
1276 for (ch = line->channel_head; NULL != ch; ch = ch->next)
1278 if ( (CS_CALLEE_CONNECTED == ch->status) &&
1279 (0 == memcmp (&ch->target,
1281 sizeof (struct GNUNET_PeerIdentity))) &&
1282 (NULL == ch->channel_unreliable) &&
1283 (ch->remote_line == ntohl (msg->source_line)) )
1290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1291 "Received %u bytes of AUDIO data for non-existing line %u, dropping.\n",
1292 msize, ntohl (msg->remote_line));
1293 return GNUNET_SYSERR;
1297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1298 "Received %u bytes of AUDIO data for unknown sender.\n",
1300 return GNUNET_SYSERR;
1302 if ((GNUNET_YES == ch->suspended_local) || (GNUNET_YES == ch->suspended_remote))
1304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1305 "Received %u bytes of AUDIO data on suspended channel CID %u:(%u:%u); dropping\n",
1306 msize, ch->cid, ch->remote_line, line->local_line);
1307 GNUNET_CADET_receive_done (channel);
1310 ch->channel_unreliable = channel;
1313 GNUNET_CADET_receive_done (channel);
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1315 "Forwarding %u bytes of AUDIO data to client CID %u:(%u:%u)\n",
1316 msize, ch->cid, ch->remote_line, ch->line->local_line);
1317 cam = (struct ClientAudioMessage *) buf;
1318 cam->header.size = htons (sizeof (buf));
1319 cam->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO);
1321 memcpy (&cam[1], &msg[1], msize);
1322 GNUNET_SERVER_notification_context_unicast (nc,
1331 * Method called whenever another peer has added us to a channel
1332 * the other peer initiated.
1334 * @param cls closure
1335 * @param channel new handle to the channel
1336 * @param initiator peer that started the channel
1338 * @param options channel option flags
1339 * @return initial channel context for the channel;
1340 * (can be NULL -- that's not an error)
1343 inbound_channel (void *cls,
1344 struct GNUNET_CADET_Channel *channel,
1345 const struct GNUNET_PeerIdentity *initiator,
1346 uint32_t port, enum GNUNET_CADET_ChannelOption options)
1348 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1349 _("Received incoming Cadet channel on port %u\n"),
1350 (unsigned int) port);
1356 * Function called whenever an inbound channel is destroyed. Should clean up
1357 * any associated state.
1359 * @param cls closure (set from #GNUNET_CADET_connect)
1360 * @param channel connection to the other end (henceforth invalid)
1361 * @param channel_ctx place where local state associated
1362 * with the channel is stored;
1363 * may point to the `struct Channel`
1366 inbound_end (void *cls,
1367 const struct GNUNET_CADET_Channel *channel,
1370 struct Channel *ch = channel_ctx;
1372 struct ClientPhoneHangupMessage hup;
1376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1377 "Cadet channel destroyed, but channel is unknown to us\n");
1381 if (ch->channel_unreliable == channel)
1383 if (NULL != ch->unreliable_mth)
1385 GNUNET_CADET_notify_transmit_ready_cancel (ch->unreliable_mth);
1386 ch->unreliable_mth = NULL;
1388 ch->channel_unreliable = NULL;
1389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1390 "Unreliable channel destroyed\n");
1393 if (ch->channel_reliable != channel)
1395 /* recursive call, I'm the one destroying 'ch' right now */
1398 ch->channel_reliable = NULL;
1400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1401 "Cadet channel destroyed by Cadet in state %d\n",
1403 hup.header.size = htons (sizeof (hup));
1404 hup.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP);
1408 case CS_CALLEE_RINGING:
1409 case CS_CALLEE_CONNECTED:
1410 GNUNET_SERVER_notification_context_unicast (nc,
1415 case CS_CALLEE_SHUTDOWN:
1417 case CS_CALLER_CALLING:
1418 case CS_CALLER_CONNECTED:
1419 GNUNET_SERVER_notification_context_unicast (nc,
1424 case CS_CALLER_SHUTDOWN:
1427 destroy_line_cadet_channels (ch);
1432 * A client disconnected. Remove all of its data structure entries.
1434 * @param cls closure, NULL
1435 * @param client identification of the client
1438 handle_client_disconnect (void *cls,
1439 struct GNUNET_SERVER_Client *client)
1445 line = GNUNET_SERVER_client_get_user_context (client, struct Line);
1448 GNUNET_SERVER_client_set_user_context (client, (void *)NULL);
1449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1450 "Client disconnected, closing line\n");
1451 GNUNET_CONTAINER_DLL_remove (lines_head,
1454 while (NULL != line->channel_head)
1455 destroy_line_cadet_channels (line->channel_head);
1463 * @param cls closure, NULL
1464 * @param tc the task context
1467 do_shutdown (void *cls,
1468 const struct GNUNET_SCHEDULER_TaskContext *tc)
1473 while (NULL != (line = lines_head))
1475 while (NULL != (ch = line->channel_head))
1476 destroy_line_cadet_channels (ch);
1477 GNUNET_CONTAINER_DLL_remove (lines_head,
1480 GNUNET_SERVER_client_set_user_context (line->client, (void *) NULL);
1485 GNUNET_CADET_disconnect (cadet);
1490 GNUNET_SERVER_notification_context_destroy (nc);
1497 * Main function that will be run by the scheduler.
1499 * @param cls closure
1500 * @param server server handle
1501 * @param c configuration
1505 struct GNUNET_SERVER_Handle *server,
1506 const struct GNUNET_CONFIGURATION_Handle *c)
1508 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1509 {&handle_client_register_message, NULL,
1510 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_REGISTER,
1511 sizeof (struct ClientPhoneRegisterMessage)},
1512 {&handle_client_pickup_message, NULL,
1513 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_PICK_UP,
1514 sizeof (struct ClientPhonePickupMessage) },
1515 {&handle_client_suspend_message, NULL,
1516 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND,
1517 sizeof (struct ClientPhoneSuspendMessage) },
1518 {&handle_client_resume_message, NULL,
1519 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME,
1520 sizeof (struct ClientPhoneResumeMessage) },
1521 {&handle_client_hangup_message, NULL,
1522 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP,
1523 sizeof (struct ClientPhoneHangupMessage) },
1524 {&handle_client_call_message, NULL,
1525 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_CALL,
1526 sizeof (struct ClientCallMessage) },
1527 {&handle_client_audio_message, NULL,
1528 GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO,
1532 static struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1533 {&handle_cadet_ring_message,
1534 GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_PHONE_RING,
1535 sizeof (struct CadetPhoneRingMessage)},
1536 {&handle_cadet_hangup_message,
1537 GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_PHONE_HANG_UP,
1538 sizeof (struct CadetPhoneHangupMessage)},
1539 {&handle_cadet_pickup_message,
1540 GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_PHONE_PICK_UP,
1541 sizeof (struct CadetPhonePickupMessage)},
1542 {&handle_cadet_suspend_message,
1543 GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_PHONE_SUSPEND,
1544 sizeof (struct CadetPhoneSuspendMessage)},
1545 {&handle_cadet_resume_message,
1546 GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_PHONE_RESUME,
1547 sizeof (struct CadetPhoneResumeMessage)},
1548 {&handle_cadet_audio_message, GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_AUDIO,
1552 static uint32_t ports[] = {
1553 GNUNET_APPLICATION_TYPE_CONVERSATION_CONTROL,
1554 GNUNET_APPLICATION_TYPE_CONVERSATION_AUDIO,
1559 GNUNET_assert (GNUNET_OK ==
1560 GNUNET_CRYPTO_get_peer_identity (cfg,
1562 cadet = GNUNET_CADET_connect (cfg,
1572 GNUNET_SCHEDULER_shutdown ();
1575 nc = GNUNET_SERVER_notification_context_create (server, 16);
1576 GNUNET_SERVER_add_handlers (server, server_handlers);
1577 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1578 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1585 * The main function for the conversation service.
1587 * @param argc number of arguments from the command line
1588 * @param argv command line arguments
1589 * @return 0 ok, 1 on error
1595 return (GNUNET_OK ==
1596 GNUNET_SERVICE_run (argc, argv,
1598 GNUNET_SERVICE_OPTION_NONE,
1599 &run, NULL)) ? 0 : 1;
1602 /* end of gnunet-service-conversation.c */