- add send kx for axolotl
[oweals/gnunet.git] / src / conversation / gnunet-service-conversation.c
1 /*
2   This file is part of GNUnet.
3   Copyright (C) 2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file conversation/gnunet-service-conversation.c
22  * @brief conversation service implementation
23  * @author Simon Dieterle
24  * @author Andreas Fuchs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
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"
36
37
38 /**
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.
42  */
43 #define RING_TIMEOUT GNUNET_TIME_UNIT_DAYS
44
45
46 /**
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).
49  */
50 struct Line;
51
52 /**
53  * The possible connection status
54  */
55 enum ChannelStatus
56 {
57   /**
58    * Our phone is ringing, waiting for the client to pick up.
59    */
60   CS_CALLEE_RINGING,
61
62   /**
63    * We are talking!
64    */
65   CS_CALLEE_CONNECTED,
66
67   /**
68    * We're in shutdown, sending hangup messages before cleaning up.
69    */
70   CS_CALLEE_SHUTDOWN,
71
72   /**
73    * We are waiting for the phone to be picked up.
74    */
75   CS_CALLER_CALLING,
76
77   /**
78    * We are talking!
79    */
80   CS_CALLER_CONNECTED,
81
82   /**
83    * We're in shutdown, sending hangup messages before cleaning up.
84    */
85   CS_CALLER_SHUTDOWN
86
87 };
88
89
90 /**
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.
95  */
96 struct Channel
97 {
98
99   /**
100    * This is a DLL.
101    */
102   struct Channel *next;
103
104   /**
105    * This is a DLL.
106    */
107   struct Channel *prev;
108
109   /**
110    * Line associated with the channel.
111    */
112   struct Line *line;
113
114   /**
115    * Handle for the reliable channel (contol data)
116    */
117   struct GNUNET_CADET_Channel *channel_reliable;
118
119   /**
120    * Handle for unreliable channel (audio data)
121    */
122   struct GNUNET_CADET_Channel *channel_unreliable;
123
124   /**
125    * Transmit handle for pending audio messages
126    */
127   struct GNUNET_CADET_TransmitHandle *unreliable_mth;
128
129   /**
130    * Message queue for control messages
131    */
132   struct GNUNET_MQ_Handle *reliable_mq;
133
134   /**
135    * Target of the line, if we are the caller.
136    */
137   struct GNUNET_PeerIdentity target;
138
139   /**
140    * Temporary buffer for audio data.
141    */
142   void *audio_data;
143
144   /**
145    * Number of bytes in @e audio_data.
146    */
147   size_t audio_size;
148
149   /**
150    * Channel identifier.
151    */
152   uint32_t cid;
153
154   /**
155    * Remote line number.
156    */
157   uint32_t remote_line;
158
159   /**
160    * Current status of this line.
161    */
162   enum ChannelStatus status;
163
164   /**
165    * #GNUNET_YES if the channel was suspended by the other peer.
166    */
167   int8_t suspended_remote;
168
169   /**
170    * #GNUNET_YES if the channel was suspended by the local client.
171    */
172   int8_t suspended_local;
173
174 };
175
176
177 /**
178  * A `struct Line` connects a local client with cadet channels.
179  */
180 struct Line
181 {
182   /**
183    * Kept in a DLL.
184    */
185   struct Line *next;
186
187   /**
188    * Kept in a DLL.
189    */
190   struct Line *prev;
191
192   /**
193    * This is a DLL.
194    */
195   struct Channel *channel_head;
196
197   /**
198    * This is a DLL.
199    */
200   struct Channel *channel_tail;
201
202   /**
203    * Handle to the line client.
204    */
205   struct GNUNET_SERVER_Client *client;
206
207   /**
208    * Generator for channel IDs.
209    */
210   uint32_t cid_gen;
211
212   /**
213    * Our line number.
214    */
215   uint32_t local_line;
216
217 };
218
219
220 /**
221  * Our configuration.
222  */
223 static const struct GNUNET_CONFIGURATION_Handle *cfg;
224
225 /**
226  * Notification context containing all connected clients.
227  */
228 static struct GNUNET_SERVER_NotificationContext *nc;
229
230 /**
231  * Handle for cadet
232  */
233 static struct GNUNET_CADET_Handle *cadet;
234
235 /**
236  * Identity of this peer.
237  */
238 static struct GNUNET_PeerIdentity my_identity;
239
240 /**
241  * Head of DLL of active lines.
242  */
243 static struct Line *lines_head;
244
245 /**
246  * Tail of DLL of active lines.
247  */
248 static struct Line *lines_tail;
249
250 /**
251  * Counter for generating local line numbers.
252  * FIXME: randomize generation in the future
253  * to eliminate information leakage.
254  */
255 static uint32_t local_line_cnt;
256
257
258 /**
259  * Function to register a phone.
260  *
261  * @param cls closure, NULL
262  * @param client the client from which the message is
263  * @param message the message from the client
264  */
265 static void
266 handle_client_register_message (void *cls,
267                                 struct GNUNET_SERVER_Client *client,
268                                 const struct GNUNET_MessageHeader *message)
269 {
270   const struct ClientPhoneRegisterMessage *msg;
271   struct Line *line;
272
273   msg = (const struct ClientPhoneRegisterMessage *) message;
274   line = GNUNET_SERVER_client_get_user_context (client, struct Line);
275   if (NULL != line)
276   {
277     GNUNET_break (0);
278     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
279     return;
280   }
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,
286                                lines_tail,
287                                line);
288   line->local_line = ntohl (msg->line) & (~ (1 << 31));
289   GNUNET_SERVER_receive_done (client, GNUNET_OK);
290 }
291
292
293 /**
294  * Function to handle a pickup request message from the client
295  *
296  * @param cls closure, NULL
297  * @param client the client from which the message is
298  * @param message the message from the client
299  */
300 static void
301 handle_client_pickup_message (void *cls,
302                               struct GNUNET_SERVER_Client *client,
303                               const struct GNUNET_MessageHeader *message)
304 {
305   const struct ClientPhonePickupMessage *msg;
306   struct GNUNET_MQ_Envelope *e;
307   struct CadetPhonePickupMessage *mppm;
308   struct Line *line;
309   struct Channel *ch;
310
311   msg = (const struct ClientPhonePickupMessage *) message;
312   line = GNUNET_SERVER_client_get_user_context (client, struct Line);
313   if (NULL == line)
314   {
315     GNUNET_break (0);
316     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
317     return;
318   }
319   for (ch = line->channel_head; NULL != ch; ch = ch->next)
320     if (msg->cid == ch->cid)
321       break;
322   if (NULL == ch)
323   {
324     /* could have been destroyed asynchronously, ignore message */
325     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326                 "Channel %u not found\n",
327                 msg->cid);
328     GNUNET_SERVER_receive_done (client, GNUNET_YES);
329     return;
330   }
331   switch (ch->status)
332   {
333   case CS_CALLEE_RINGING:
334     ch->status = CS_CALLEE_CONNECTED;
335     break;
336   case CS_CALLEE_CONNECTED:
337     GNUNET_break (0);
338     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
339     return;
340   case CS_CALLEE_SHUTDOWN:
341     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342                 "Ignoring client's PICKUP message, line is in SHUTDOWN\n");
343     break;
344   case CS_CALLER_CALLING:
345   case CS_CALLER_CONNECTED:
346   case CS_CALLER_SHUTDOWN:
347     GNUNET_break (0);
348     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
349     return;
350   }
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);
358 }
359
360
361 /**
362  * Destroy a channel.
363  *
364  * @param ch channel to destroy.
365  */
366 static void
367 destroy_line_cadet_channels (struct Channel *ch)
368 {
369   struct Line *line = ch->line;
370   struct GNUNET_CADET_Channel *t;
371
372   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373               "Destroying cadet channels\n");
374   if (NULL != ch->reliable_mq)
375   {
376     GNUNET_MQ_destroy (ch->reliable_mq);
377     ch->reliable_mq = NULL;
378   }
379   if (NULL != ch->unreliable_mth)
380   {
381     GNUNET_CADET_notify_transmit_ready_cancel (ch->unreliable_mth);
382     ch->unreliable_mth = NULL;
383   }
384   if (NULL != (t = ch->channel_unreliable))
385   {
386     ch->channel_unreliable = NULL;
387     GNUNET_CADET_channel_destroy (t);
388   }
389   if (NULL != (t = ch->channel_reliable))
390   {
391     ch->channel_reliable = NULL;
392     GNUNET_CADET_channel_destroy (t);
393   }
394   GNUNET_CONTAINER_DLL_remove (line->channel_head,
395                                line->channel_tail,
396                                ch);
397   GNUNET_free_non_null (ch->audio_data);
398   GNUNET_free (ch);
399 }
400
401
402 /**
403  * We are done signalling shutdown to the other peer.  Close down
404  * the channel.
405  *
406  * @param cls the `struct Channel` to reset/terminate
407  */
408 static void
409 mq_done_finish_caller_shutdown (void *cls)
410 {
411   struct Channel *ch = cls;
412
413   switch (ch->status)
414   {
415   case CS_CALLEE_RINGING:
416     GNUNET_break (0);
417     break;
418   case CS_CALLEE_CONNECTED:
419     GNUNET_break (0);
420     break;
421   case CS_CALLEE_SHUTDOWN:
422     destroy_line_cadet_channels (ch);
423     break;
424   case CS_CALLER_CALLING:
425     GNUNET_break (0);
426     break;
427   case CS_CALLER_CONNECTED:
428     GNUNET_break (0);
429     break;
430   case CS_CALLER_SHUTDOWN:
431     destroy_line_cadet_channels (ch);
432     break;
433   }
434 }
435
436
437 /**
438  * Function to handle a hangup request message from the client
439  *
440  * @param cls closure, NULL
441  * @param client the client from which the message is
442  * @param message the message from the client
443  */
444 static void
445 handle_client_hangup_message (void *cls,
446                               struct GNUNET_SERVER_Client *client,
447                               const struct GNUNET_MessageHeader *message)
448 {
449   const struct ClientPhoneHangupMessage *msg;
450   struct GNUNET_MQ_Envelope *e;
451   struct CadetPhoneHangupMessage *mhum;
452   struct Line *line;
453   struct Channel *ch;
454
455   msg = (const struct ClientPhoneHangupMessage *) message;
456   line = GNUNET_SERVER_client_get_user_context (client, struct Line);
457   if (NULL == line)
458   {
459     GNUNET_break (0);
460     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
461     return;
462   }
463   for (ch = line->channel_head; NULL != ch; ch = ch->next)
464     if (msg->cid == ch->cid)
465       break;
466   if (NULL == ch)
467   {
468     /* could have been destroyed asynchronously, ignore message */
469     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
470                 "Channel %u not found\n",
471                 msg->cid);
472     GNUNET_SERVER_receive_done (client, GNUNET_OK);
473     return;
474   }
475   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476               "Received HANGUP for channel %u which is in state %d\n",
477               msg->cid,
478               ch->status);
479   switch (ch->status)
480   {
481   case CS_CALLEE_RINGING:
482     ch->status = CS_CALLEE_SHUTDOWN;
483     break;
484   case CS_CALLEE_CONNECTED:
485     ch->status = CS_CALLEE_SHUTDOWN;
486     break;
487   case CS_CALLEE_SHUTDOWN:
488     /* maybe the other peer closed asynchronously... */
489     GNUNET_SERVER_receive_done (client, GNUNET_OK);
490     return;
491   case CS_CALLER_CALLING:
492     ch->status = CS_CALLER_SHUTDOWN;
493     break;
494   case CS_CALLER_CONNECTED:
495     ch->status = CS_CALLER_SHUTDOWN;
496     break;
497   case CS_CALLER_SHUTDOWN:
498     /* maybe the other peer closed asynchronously... */
499     GNUNET_SERVER_receive_done (client, GNUNET_OK);
500     return;
501   }
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,
508                          ch);
509   GNUNET_MQ_send (ch->reliable_mq, e);
510   GNUNET_SERVER_receive_done (client, GNUNET_OK);
511 }
512
513
514 /**
515  * Function to handle a suspend request message from the client
516  *
517  * @param cls closure, NULL
518  * @param client the client from which the message is
519  * @param message the message from the client
520  */
521 static void
522 handle_client_suspend_message (void *cls,
523                                struct GNUNET_SERVER_Client *client,
524                                const struct GNUNET_MessageHeader *message)
525 {
526   const struct ClientPhoneSuspendMessage *msg;
527   struct GNUNET_MQ_Envelope *e;
528   struct CadetPhoneSuspendMessage *mhum;
529   struct Line *line;
530   struct Channel *ch;
531
532   msg = (const struct ClientPhoneSuspendMessage *) message;
533   line = GNUNET_SERVER_client_get_user_context (client, struct Line);
534   if (NULL == line)
535   {
536     GNUNET_break (0);
537     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
538     return;
539   }
540   for (ch = line->channel_head; NULL != ch; ch = ch->next)
541     if (msg->cid == ch->cid)
542       break;
543   if (NULL == ch)
544   {
545     /* could have been destroyed asynchronously, ignore message */
546     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547                 "Channel %u not found\n",
548                 msg->cid);
549     GNUNET_SERVER_receive_done (client, GNUNET_OK);
550     return;
551   }
552   if (GNUNET_YES == ch->suspended_local)
553   {
554     GNUNET_break (0);
555     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
556     return;
557   }
558   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
559               "Received SUSPEND for channel %u which is in state %d\n",
560               msg->cid,
561               ch->status);
562   switch (ch->status)
563   {
564   case CS_CALLEE_RINGING:
565     GNUNET_break (0);
566     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
567     return;
568   case CS_CALLEE_CONNECTED:
569     ch->suspended_local = GNUNET_YES;
570     break;
571   case CS_CALLEE_SHUTDOWN:
572     /* maybe the other peer closed asynchronously... */
573     GNUNET_SERVER_receive_done (client, GNUNET_OK);
574     return;
575   case CS_CALLER_CALLING:
576     GNUNET_break (0);
577     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
578     return;
579   case CS_CALLER_CONNECTED:
580     ch->suspended_local = GNUNET_YES;
581     break;
582   case CS_CALLER_SHUTDOWN:
583     /* maybe the other peer closed asynchronously... */
584     GNUNET_SERVER_receive_done (client, GNUNET_OK);
585     return;
586   }
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);
593 }
594
595
596 /**
597  * Function to handle a resume request message from the client
598  *
599  * @param cls closure, NULL
600  * @param client the client from which the message is
601  * @param message the message from the client
602  */
603 static void
604 handle_client_resume_message (void *cls,
605                               struct GNUNET_SERVER_Client *client,
606                               const struct GNUNET_MessageHeader *message)
607 {
608   const struct ClientPhoneResumeMessage *msg;
609   struct GNUNET_MQ_Envelope *e;
610   struct CadetPhoneResumeMessage *mhum;
611   struct Line *line;
612   struct Channel *ch;
613
614   msg = (const struct ClientPhoneResumeMessage *) message;
615   line = GNUNET_SERVER_client_get_user_context (client, struct Line);
616   if (NULL == line)
617   {
618     GNUNET_break (0);
619     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
620     return;
621   }
622   for (ch = line->channel_head; NULL != ch; ch = ch->next)
623     if (msg->cid == ch->cid)
624       break;
625   if (NULL == ch)
626   {
627     /* could have been destroyed asynchronously, ignore message */
628     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
629                 "Channel %u not found\n",
630                 msg->cid);
631     GNUNET_SERVER_receive_done (client, GNUNET_OK);
632     return;
633   }
634   if (GNUNET_YES != ch->suspended_local)
635   {
636     GNUNET_break (0);
637     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
638     return;
639   }
640   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
641               "Received RESUME for channel %u which is in state %d\n",
642               msg->cid,
643               ch->status);
644   switch (ch->status)
645   {
646   case CS_CALLEE_RINGING:
647     GNUNET_break (0);
648     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
649     return;
650   case CS_CALLEE_CONNECTED:
651     ch->suspended_local = GNUNET_NO;
652     break;
653   case CS_CALLEE_SHUTDOWN:
654     /* maybe the other peer closed asynchronously... */
655     GNUNET_SERVER_receive_done (client, GNUNET_OK);
656     return;
657   case CS_CALLER_CALLING:
658     GNUNET_break (0);
659     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
660     return;
661   case CS_CALLER_CONNECTED:
662     ch->suspended_local = GNUNET_NO;
663     break;
664   case CS_CALLER_SHUTDOWN:
665     /* maybe the other peer closed asynchronously... */
666     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
667     return;
668   }
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);
675 }
676
677
678 /**
679  * Function to handle call request from the client
680  *
681  * @param cls closure, NULL
682  * @param client the client from which the message is
683  * @param message the message from the client
684  */
685 static void
686 handle_client_call_message (void *cls,
687                             struct GNUNET_SERVER_Client *client,
688                             const struct GNUNET_MessageHeader *message)
689 {
690   const struct ClientCallMessage *msg;
691   struct Line *line;
692   struct Channel *ch;
693   struct GNUNET_MQ_Envelope *e;
694   struct CadetPhoneRingMessage *ring;
695
696   msg = (const struct ClientCallMessage *) message;
697   line = GNUNET_SERVER_client_get_user_context (client, struct Line);
698   if (NULL != line)
699   {
700     GNUNET_break (0);
701     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
702     return;
703   }
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,
710                                lines_tail,
711                                line);
712   ch = GNUNET_new (struct Channel);
713   ch->line = line;
714   GNUNET_CONTAINER_DLL_insert (line->channel_head,
715                                line->channel_tail,
716                                ch);
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,
721                                                      ch,
722                                                      &msg->target,
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,
733                                       &ring->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_assert (GNUNET_OK ==
740                  GNUNET_CRYPTO_ecdsa_sign (&msg->caller_id,
741                                            &ring->purpose,
742                                            &ring->signature));
743   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
744               "Sending RING message via cadet\n");
745   GNUNET_MQ_send (ch->reliable_mq, e);
746   GNUNET_SERVER_receive_done (client, GNUNET_OK);
747 }
748
749
750 /**
751  * Transmit audio data via unreliable cadet channel.
752  *
753  * @param cls the `struct Channel` we are transmitting for
754  * @param size number of bytes available in @a buf
755  * @param buf where to copy the data
756  * @return number of bytes copied to @a buf
757  */
758 static size_t
759 transmit_line_audio (void *cls,
760                      size_t size,
761                      void *buf)
762 {
763   struct Channel *ch = cls;
764   struct CadetAudioMessage *mam = buf;
765
766   ch->unreliable_mth = NULL;
767   if ( (NULL == buf) ||
768        (size < sizeof (struct CadetAudioMessage) + ch->audio_size) )
769     {
770     /* eh, other error handling? */
771     return 0;
772   }
773   mam->header.size = htons (sizeof (struct CadetAudioMessage) + ch->audio_size);
774   mam->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_AUDIO);
775   mam->remote_line = htonl (ch->remote_line);
776   mam->source_line = htonl (ch->line->local_line);
777   memcpy (&mam[1], ch->audio_data, ch->audio_size);
778   GNUNET_free (ch->audio_data);
779   ch->audio_data = NULL;
780   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
781               "Sending %u bytes of audio data from line %u to remote line %u via cadet\n",
782               ch->audio_size, ch->line->local_line, ch->remote_line);
783   return sizeof (struct CadetAudioMessage) + ch->audio_size;
784 }
785
786
787 /**
788  * Function to handle audio data from the client
789  *
790  * @param cls closure, NULL
791  * @param client the client from which the message is
792  * @param message the message from the client
793  */
794 static void
795 handle_client_audio_message (void *cls,
796                              struct GNUNET_SERVER_Client *client,
797                              const struct GNUNET_MessageHeader *message)
798 {
799   const struct ClientAudioMessage *msg;
800   struct Line *line;
801   struct Channel *ch;
802   size_t size;
803
804   size = ntohs (message->size) - sizeof (struct ClientAudioMessage);
805   msg = (const struct ClientAudioMessage *) message;
806   line = GNUNET_SERVER_client_get_user_context (client, struct Line);
807   if (NULL == line)
808   {
809     GNUNET_break (0);
810     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
811     return;
812   }
813   for (ch = line->channel_head; NULL != ch; ch = ch->next)
814     if (msg->cid == ch->cid)
815       break;
816   if (NULL == ch)
817   {
818     /* could have been destroyed asynchronously, ignore message */
819     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
820                 "Channel %u not found\n",
821                 msg->cid);
822     GNUNET_SERVER_receive_done (client, GNUNET_OK);
823     return;
824   }
825
826   switch (ch->status)
827   {
828   case CS_CALLEE_RINGING:
829   case CS_CALLER_CALLING:
830     GNUNET_break (0);
831     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
832     return;
833   case CS_CALLEE_CONNECTED:
834   case CS_CALLER_CONNECTED:
835     /* common case, handled below */
836     break;
837   case CS_CALLEE_SHUTDOWN:
838   case CS_CALLER_SHUTDOWN:
839     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
840                 "Cadet audio channel in shutdown; audio data dropped\n");
841     GNUNET_SERVER_receive_done (client, GNUNET_OK);
842     return;
843   }
844   if (GNUNET_YES == ch->suspended_local)
845   {
846     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "This channel is suspended locally\n");
847     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
848     return;
849   }
850   if (NULL == ch->channel_unreliable)
851   {
852     GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
853                 _("Cadet audio channel not ready; audio data dropped\n"));
854     GNUNET_SERVER_receive_done (client, GNUNET_OK);
855     return;
856   }
857   if (NULL != ch->unreliable_mth)
858   {
859     /* NOTE: we may want to not do this and instead combine the data */
860     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861                 "Bandwidth insufficient; dropping previous audio data segment with %u bytes\n",
862                 (unsigned int) ch->audio_size);
863     GNUNET_CADET_notify_transmit_ready_cancel (ch->unreliable_mth);
864     ch->unreliable_mth = NULL;
865     GNUNET_free (ch->audio_data);
866     ch->audio_data = NULL;
867   }
868   ch->audio_size = size;
869   ch->audio_data = GNUNET_malloc (ch->audio_size);
870   memcpy (ch->audio_data,
871           &msg[1],
872           size);
873   ch->unreliable_mth = GNUNET_CADET_notify_transmit_ready (ch->channel_unreliable,
874                                                           GNUNET_NO,
875                                                           GNUNET_TIME_UNIT_FOREVER_REL,
876                                                           sizeof (struct CadetAudioMessage)
877                                                           + ch->audio_size,
878                                                           &transmit_line_audio,
879                                                           ch);
880   GNUNET_SERVER_receive_done (client, GNUNET_OK);
881 }
882
883
884 /**
885  * We are done signalling shutdown to the other peer.
886  * Destroy the channel.
887  *
888  * @param cls the `struct GNUNET_CADET_channel` to destroy
889  */
890 static void
891 mq_done_destroy_channel (void *cls)
892 {
893   struct GNUNET_CADET_Channel *channel = cls;
894
895   GNUNET_CADET_channel_destroy (channel);
896 }
897
898
899 /**
900  * Function to handle a ring message incoming over cadet
901  *
902  * @param cls closure, NULL
903  * @param channel the channel over which the message arrived
904  * @param channel_ctx the channel context, can be NULL
905  *                    or point to the `struct Channel`
906  * @param message the incoming message
907  * @return #GNUNET_OK
908  */
909 static int
910 handle_cadet_ring_message (void *cls,
911                           struct GNUNET_CADET_Channel *channel,
912                           void **channel_ctx,
913                           const struct GNUNET_MessageHeader *message)
914 {
915   const struct CadetPhoneRingMessage *msg;
916   struct Line *line;
917   struct Channel *ch;
918   struct GNUNET_MQ_Envelope *e;
919   struct CadetPhoneHangupMessage *hang_up;
920   struct ClientPhoneRingMessage cring;
921   struct GNUNET_MQ_Handle *reliable_mq;
922
923   msg = (const struct CadetPhoneRingMessage *) message;
924   if ( (msg->purpose.size != htonl (sizeof (struct GNUNET_PeerIdentity) * 2 +
925                                     sizeof (struct GNUNET_TIME_AbsoluteNBO) +
926                                     sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
927                                     sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) ||
928        (GNUNET_OK !=
929         GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_CONVERSATION_RING,
930                                   &msg->purpose,
931                                   &msg->signature,
932                                   &msg->caller_id)) )
933   {
934     GNUNET_break_op (0);
935     return GNUNET_SYSERR;
936   }
937   GNUNET_CADET_receive_done (channel); /* needed? */
938   for (line = lines_head; NULL != line; line = line->next)
939     if (line->local_line == ntohl (msg->remote_line))
940       break;
941   if (NULL == line)
942   {
943     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
944                 _("No available phone for incoming call on line %u, sending HANG_UP signal\n"),
945                 ntohl (msg->remote_line));
946     e = GNUNET_MQ_msg (hang_up,
947                        GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_PHONE_HANG_UP);
948     GNUNET_MQ_notify_sent (e,
949                            &mq_done_destroy_channel,
950                            channel);
951     reliable_mq = GNUNET_CADET_mq_create (channel);
952     GNUNET_MQ_send (reliable_mq, e);
953     /* FIXME: do we need to clean up reliable_mq somehow/somewhere? */
954     return GNUNET_OK;
955   }
956   ch = GNUNET_new (struct Channel);
957   ch->line = line;
958   GNUNET_CONTAINER_DLL_insert (line->channel_head,
959                                line->channel_tail,
960                                ch);
961   ch->status = CS_CALLEE_RINGING;
962   ch->remote_line = ntohl (msg->source_line);
963   ch->channel_reliable = channel;
964   ch->reliable_mq = GNUNET_CADET_mq_create (ch->channel_reliable);
965   ch->cid = line->cid_gen++;
966   ch->target = msg->source;
967   *channel_ctx = ch;
968   cring.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RING);
969   cring.header.size = htons (sizeof (cring));
970   cring.cid = ch->cid;
971   cring.caller_id = msg->caller_id;
972   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973               "Sending RING message to client. CID %u:(%u, %u)\n",
974               ch->cid, ch->remote_line, line->local_line);
975   GNUNET_SERVER_notification_context_unicast (nc,
976                                               line->client,
977                                               &cring.header,
978                                               GNUNET_NO);
979   return GNUNET_OK;
980 }
981
982
983 /**
984  * Function to handle a hangup message incoming over cadet
985  *
986  * @param cls closure, NULL
987  * @param channel the channel over which the message arrived
988  * @param channel_ctx the channel context, can be NULL
989  *                    or point to the `struct Channel`
990  * @param message the incoming message
991  * @return #GNUNET_OK
992  */
993 static int
994 handle_cadet_hangup_message (void *cls,
995                             struct GNUNET_CADET_Channel *channel,
996                             void **channel_ctx,
997                             const struct GNUNET_MessageHeader *message)
998 {
999   struct Channel *ch = *channel_ctx;
1000   struct Line *line;
1001   struct ClientPhoneHangupMessage hup;
1002   enum ChannelStatus status;
1003
1004   if (NULL == ch)
1005   {
1006     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1007                 "HANGUP message received for non-existing line, dropping channel.\n");
1008     return GNUNET_SYSERR;
1009   }
1010   line = ch->line;
1011   *channel_ctx = NULL;
1012   hup.header.size = htons (sizeof (hup));
1013   hup.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP);
1014   hup.cid = ch->cid;
1015   status = ch->status;
1016   GNUNET_CADET_receive_done (channel);
1017   destroy_line_cadet_channels (ch);
1018   switch (status)
1019   {
1020   case CS_CALLEE_RINGING:
1021   case CS_CALLEE_CONNECTED:
1022     break;
1023   case CS_CALLEE_SHUTDOWN:
1024     return GNUNET_OK;
1025   case CS_CALLER_CALLING:
1026   case CS_CALLER_CONNECTED:
1027     break;
1028   case CS_CALLER_SHUTDOWN:
1029     return GNUNET_OK;
1030   }
1031   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1032               "Sending HANG UP message to client\n");
1033   GNUNET_SERVER_notification_context_unicast (nc,
1034                                               line->client,
1035                                               &hup.header,
1036                                               GNUNET_NO);
1037   return GNUNET_OK;
1038 }
1039
1040
1041 /**
1042  * Function to handle a pickup message incoming over cadet
1043  *
1044  * @param cls closure, NULL
1045  * @param channel the channel over which the message arrived
1046  * @param channel_ctx the channel context, can be NULL
1047  *                    or point to the `struct Channel`
1048  * @param message the incoming message
1049  * @return #GNUNET_OK if message was OK,
1050  *         #GNUNET_SYSERR if message violated the protocol
1051  */
1052 static int
1053 handle_cadet_pickup_message (void *cls,
1054                             struct GNUNET_CADET_Channel *channel,
1055                             void **channel_ctx,
1056                             const struct GNUNET_MessageHeader *message)
1057 {
1058   struct Channel *ch = *channel_ctx;
1059   struct Line *line;
1060   struct ClientPhonePickupMessage pick;
1061
1062   if (NULL == ch)
1063   {
1064     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1065                 "PICKUP message received for non-existing channel, dropping channel.\n");
1066     return GNUNET_SYSERR;
1067   }
1068   line = ch->line;
1069   GNUNET_CADET_receive_done (channel);
1070   switch (ch->status)
1071   {
1072   case CS_CALLEE_RINGING:
1073   case CS_CALLEE_CONNECTED:
1074     GNUNET_break_op (0);
1075     destroy_line_cadet_channels (ch);
1076     return GNUNET_SYSERR;
1077   case CS_CALLEE_SHUTDOWN:
1078     GNUNET_break_op (0);
1079     destroy_line_cadet_channels (ch);
1080     return GNUNET_SYSERR;
1081   case CS_CALLER_CALLING:
1082     ch->status = CS_CALLER_CONNECTED;
1083     break;
1084   case CS_CALLER_CONNECTED:
1085     GNUNET_break_op (0);
1086     return GNUNET_OK;
1087   case CS_CALLER_SHUTDOWN:
1088     GNUNET_break_op (0);
1089     mq_done_finish_caller_shutdown (ch);
1090     return GNUNET_SYSERR;
1091   }
1092   pick.header.size = htons (sizeof (pick));
1093   pick.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_PICKED_UP);
1094   pick.cid = ch->cid;
1095   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1096               "Sending PICKED UP message to client\n");
1097   GNUNET_SERVER_notification_context_unicast (nc,
1098                                               line->client,
1099                                               &pick.header,
1100                                               GNUNET_NO);
1101   ch->channel_unreliable = GNUNET_CADET_channel_create (cadet,
1102                                                        ch,
1103                                                        &ch->target,
1104                                                        GNUNET_APPLICATION_TYPE_CONVERSATION_AUDIO,
1105                                                        GNUNET_CADET_OPTION_DEFAULT);
1106   if (NULL == ch->channel_unreliable)
1107   {
1108     GNUNET_break (0);
1109   }
1110   return GNUNET_OK;
1111 }
1112
1113
1114 /**
1115  * Function to handle a suspend message incoming over cadet
1116  *
1117  * @param cls closure, NULL
1118  * @param channel the channel over which the message arrived
1119  * @param channel_ctx the channel context, can be NULL
1120  *                    or point to the `struct Channel`
1121  * @param message the incoming message
1122  * @return #GNUNET_OK
1123  */
1124 static int
1125 handle_cadet_suspend_message (void *cls,
1126                              struct GNUNET_CADET_Channel *channel,
1127                              void **channel_ctx,
1128                              const struct GNUNET_MessageHeader *message)
1129 {
1130   struct Channel *ch = *channel_ctx;
1131   struct Line *line;
1132   struct ClientPhoneSuspendMessage suspend;
1133
1134   if (NULL == ch)
1135   {
1136     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1137                 "SUSPEND message received for non-existing line, dropping channel.\n");
1138     return GNUNET_SYSERR;
1139   }
1140   line = ch->line;
1141   suspend.header.size = htons (sizeof (suspend));
1142   suspend.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND);
1143   suspend.cid = ch->cid;
1144   GNUNET_CADET_receive_done (channel);
1145   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1146               "Suspending channel CID: %u(%u:%u)\n",
1147               ch->cid, ch->remote_line, line->local_line);
1148   switch (ch->status)
1149   {
1150   case CS_CALLEE_RINGING:
1151     GNUNET_break_op (0);
1152     break;
1153   case CS_CALLEE_CONNECTED:
1154     ch->suspended_remote = GNUNET_YES;
1155     break;
1156   case CS_CALLEE_SHUTDOWN:
1157     return GNUNET_OK;
1158   case CS_CALLER_CALLING:
1159     GNUNET_break_op (0);
1160     break;
1161   case CS_CALLER_CONNECTED:
1162     ch->suspended_remote = GNUNET_YES;
1163     break;
1164   case CS_CALLER_SHUTDOWN:
1165     return GNUNET_OK;
1166   }
1167   GNUNET_SERVER_notification_context_unicast (nc,
1168                                               line->client,
1169                                               &suspend.header,
1170                                               GNUNET_NO);
1171   return GNUNET_OK;
1172 }
1173
1174
1175 /**
1176  * Function to handle a resume message incoming over cadet
1177  *
1178  * @param cls closure, NULL
1179  * @param channel the channel over which the message arrived
1180  * @param channel_ctx the channel context, can be NULL
1181  *                    or point to the `struct Channel`
1182  * @param message the incoming message
1183  * @return #GNUNET_OK
1184  */
1185 static int
1186 handle_cadet_resume_message (void *cls,
1187                              struct GNUNET_CADET_Channel *channel,
1188                              void **channel_ctx,
1189                              const struct GNUNET_MessageHeader *message)
1190 {
1191   struct Channel *ch = *channel_ctx;
1192   struct Line *line;
1193   struct ClientPhoneResumeMessage resume;
1194
1195   if (NULL == ch)
1196   {
1197     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1198                 "RESUME message received for non-existing line, dropping channel.\n");
1199     return GNUNET_SYSERR;
1200   }
1201   line = ch->line;
1202   resume.header.size = htons (sizeof (resume));
1203   resume.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME);
1204   resume.cid = ch->cid;
1205   GNUNET_CADET_receive_done (channel);
1206   if (GNUNET_YES != ch->suspended_remote)
1207   {
1208     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1209                 "RESUME message received for non-suspended channel, dropping channel.\n");
1210     return GNUNET_SYSERR;
1211   }
1212   switch (ch->status)
1213   {
1214   case CS_CALLEE_RINGING:
1215     GNUNET_break (0);
1216     break;
1217   case CS_CALLEE_CONNECTED:
1218     ch->suspended_remote = GNUNET_NO;
1219     break;
1220   case CS_CALLEE_SHUTDOWN:
1221     return GNUNET_OK;
1222   case CS_CALLER_CALLING:
1223     GNUNET_break (0);
1224     break;
1225   case CS_CALLER_CONNECTED:
1226     ch->suspended_remote = GNUNET_NO;
1227     break;
1228   case CS_CALLER_SHUTDOWN:
1229     return GNUNET_OK;
1230   }
1231   GNUNET_SERVER_notification_context_unicast (nc,
1232                                               line->client,
1233                                               &resume.header,
1234                                               GNUNET_NO);
1235   return GNUNET_OK;
1236 }
1237
1238
1239 /**
1240  * Function to handle an audio message incoming over cadet
1241  *
1242  * @param cls closure, NULL
1243  * @param channel the channel over which the message arrived
1244  * @param channel_ctx the channel context, can be NULL
1245  *                    or point to the `struct Channel`
1246  * @param message the incoming message
1247  * @return #GNUNET_OK
1248  */
1249 static int
1250 handle_cadet_audio_message (void *cls,
1251                            struct GNUNET_CADET_Channel *channel,
1252                            void **channel_ctx,
1253                            const struct GNUNET_MessageHeader *message)
1254 {
1255   const struct CadetAudioMessage *msg;
1256   struct Channel *ch = *channel_ctx;
1257   struct Line *line;
1258   struct GNUNET_PeerIdentity sender;
1259   size_t msize = ntohs (message->size) - sizeof (struct CadetAudioMessage);
1260   char buf[msize + sizeof (struct ClientAudioMessage)];
1261   struct ClientAudioMessage *cam;
1262   const union GNUNET_CADET_ChannelInfo *info;
1263
1264   msg = (const struct CadetAudioMessage *) message;
1265   if (NULL == ch)
1266   {
1267     info = GNUNET_CADET_channel_get_info (channel,
1268                                          GNUNET_CADET_OPTION_PEER);
1269     if (NULL == info)
1270     {
1271       GNUNET_break (0);
1272       return GNUNET_SYSERR;
1273     }
1274     sender = info->peer;
1275     for (line = lines_head; NULL != line; line = line->next)
1276       if (line->local_line == ntohl (msg->remote_line))
1277       {
1278         for (ch = line->channel_head; NULL != ch; ch = ch->next)
1279         {
1280           if ( (CS_CALLEE_CONNECTED == ch->status) &&
1281                (0 == memcmp (&ch->target,
1282                              &sender,
1283                              sizeof (struct GNUNET_PeerIdentity))) &&
1284                (NULL == ch->channel_unreliable) &&
1285                (ch->remote_line == ntohl (msg->source_line)) )
1286             break;
1287         }
1288         break;
1289       }
1290     if (NULL == line)
1291     {
1292       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1293                   "Received %u bytes of AUDIO data for non-existing line %u, dropping.\n",
1294                   msize, ntohl (msg->remote_line));
1295       return GNUNET_SYSERR;
1296     }
1297     if (NULL == ch)
1298     {
1299       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1300                   "Received %u bytes of AUDIO data for unknown sender.\n",
1301                   msize);
1302       return GNUNET_SYSERR;
1303     }
1304     if ((GNUNET_YES == ch->suspended_local) || (GNUNET_YES == ch->suspended_remote))
1305     {
1306       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1307                   "Received %u bytes of AUDIO data on suspended channel CID %u:(%u:%u); dropping\n",
1308                   msize, ch->cid, ch->remote_line, line->local_line);
1309       GNUNET_CADET_receive_done (channel);
1310       return GNUNET_OK;
1311     }
1312     ch->channel_unreliable = channel;
1313     *channel_ctx = ch;
1314   }
1315   GNUNET_CADET_receive_done (channel);
1316   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1317               "Forwarding %u bytes of AUDIO data to client CID %u:(%u:%u)\n",
1318               msize, ch->cid, ch->remote_line, ch->line->local_line);
1319   cam = (struct ClientAudioMessage *) buf;
1320   cam->header.size = htons (sizeof (buf));
1321   cam->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO);
1322   cam->cid = ch->cid;
1323   memcpy (&cam[1], &msg[1], msize);
1324   GNUNET_SERVER_notification_context_unicast (nc,
1325                                               ch->line->client,
1326                                               &cam->header,
1327                                               GNUNET_YES);
1328   return GNUNET_OK;
1329 }
1330
1331
1332 /**
1333  * Method called whenever another peer has added us to a channel
1334  * the other peer initiated.
1335  *
1336  * @param cls closure
1337  * @param channel new handle to the channel
1338  * @param initiator peer that started the channel
1339  * @param port port
1340  * @param options channel option flags
1341  * @return initial channel context for the channel;
1342  *         (can be NULL -- that's not an error)
1343  */
1344 static void *
1345 inbound_channel (void *cls,
1346                 struct GNUNET_CADET_Channel *channel,
1347                 const struct GNUNET_PeerIdentity *initiator,
1348                 uint32_t port, enum GNUNET_CADET_ChannelOption options)
1349 {
1350   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1351               _("Received incoming Cadet channel on port %u\n"),
1352               (unsigned int) port);
1353   return NULL;
1354 }
1355
1356
1357 /**
1358  * Function called whenever an inbound channel is destroyed.  Should clean up
1359  * any associated state.
1360  *
1361  * @param cls closure (set from #GNUNET_CADET_connect)
1362  * @param channel connection to the other end (henceforth invalid)
1363  * @param channel_ctx place where local state associated
1364  *                   with the channel is stored;
1365  *                   may point to the `struct Channel`
1366  */
1367 static void
1368 inbound_end (void *cls,
1369              const struct GNUNET_CADET_Channel *channel,
1370              void *channel_ctx)
1371 {
1372   struct Channel *ch = channel_ctx;
1373   struct Line *line;
1374   struct ClientPhoneHangupMessage hup;
1375
1376   if (NULL == ch)
1377   {
1378     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1379                 "Cadet channel destroyed, but channel is unknown to us\n");
1380     return;
1381   }
1382   line = ch->line;
1383   if (ch->channel_unreliable == channel)
1384   {
1385     if (NULL != ch->unreliable_mth)
1386     {
1387       GNUNET_CADET_notify_transmit_ready_cancel (ch->unreliable_mth);
1388       ch->unreliable_mth = NULL;
1389     }
1390     ch->channel_unreliable = NULL;
1391     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1392                 "Unreliable channel destroyed\n");
1393     return;
1394   }
1395   if (ch->channel_reliable != channel)
1396   {
1397     /* recursive call, I'm the one destroying 'ch' right now */
1398     return;
1399   }
1400   ch->channel_reliable = NULL;
1401
1402   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403               "Cadet channel destroyed by Cadet in state %d\n",
1404               ch->status);
1405   hup.header.size = htons (sizeof (hup));
1406   hup.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP);
1407   hup.cid = ch->cid;
1408   switch (ch->status)
1409   {
1410   case CS_CALLEE_RINGING:
1411   case CS_CALLEE_CONNECTED:
1412     GNUNET_SERVER_notification_context_unicast (nc,
1413                                                 line->client,
1414                                                 &hup.header,
1415                                                 GNUNET_NO);
1416     break;
1417   case CS_CALLEE_SHUTDOWN:
1418     break;
1419   case CS_CALLER_CALLING:
1420   case CS_CALLER_CONNECTED:
1421     GNUNET_SERVER_notification_context_unicast (nc,
1422                                                 line->client,
1423                                                 &hup.header,
1424                                                 GNUNET_NO);
1425     break;
1426   case CS_CALLER_SHUTDOWN:
1427     break;
1428   }
1429   destroy_line_cadet_channels (ch);
1430 }
1431
1432
1433 /**
1434  * A client disconnected.  Remove all of its data structure entries.
1435  *
1436  * @param cls closure, NULL
1437  * @param client identification of the client
1438  */
1439 static void
1440 handle_client_disconnect (void *cls,
1441                           struct GNUNET_SERVER_Client *client)
1442 {
1443   struct Line *line;
1444
1445   if (NULL == client)
1446     return;
1447   line = GNUNET_SERVER_client_get_user_context (client, struct Line);
1448   if (NULL == line)
1449     return;
1450   GNUNET_SERVER_client_set_user_context (client, NULL);
1451   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1452               "Client disconnected, closing line\n");
1453   GNUNET_CONTAINER_DLL_remove (lines_head,
1454                                lines_tail,
1455                                line);
1456   while (NULL != line->channel_head)
1457     destroy_line_cadet_channels (line->channel_head);
1458   GNUNET_free (line);
1459 }
1460
1461
1462 /**
1463  * Shutdown nicely
1464  *
1465  * @param cls closure, NULL
1466  * @param tc the task context
1467  */
1468 static void
1469 do_shutdown (void *cls,
1470              const struct GNUNET_SCHEDULER_TaskContext *tc)
1471 {
1472   struct Line *line;
1473   struct Channel *ch;
1474
1475   while (NULL != (line = lines_head))
1476   {
1477     while (NULL != (ch = line->channel_head))
1478       destroy_line_cadet_channels (ch);
1479     GNUNET_CONTAINER_DLL_remove (lines_head,
1480                                  lines_tail,
1481                                  line);
1482     GNUNET_SERVER_client_set_user_context (line->client, NULL);
1483     GNUNET_free (line);
1484   }
1485   if (NULL != cadet)
1486   {
1487     GNUNET_CADET_disconnect (cadet);
1488     cadet = NULL;
1489   }
1490   if (NULL != nc)
1491   {
1492     GNUNET_SERVER_notification_context_destroy (nc);
1493     nc = NULL;
1494   }
1495 }
1496
1497
1498 /**
1499  * Main function that will be run by the scheduler.
1500  *
1501  * @param cls closure
1502  * @param server server handle
1503  * @param c configuration
1504  */
1505 static void
1506 run (void *cls,
1507      struct GNUNET_SERVER_Handle *server,
1508      const struct GNUNET_CONFIGURATION_Handle *c)
1509 {
1510   static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1511     {&handle_client_register_message, NULL,
1512      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_REGISTER,
1513      sizeof (struct ClientPhoneRegisterMessage)},
1514     {&handle_client_pickup_message, NULL,
1515      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_PICK_UP,
1516      sizeof (struct ClientPhonePickupMessage) },
1517     {&handle_client_suspend_message, NULL,
1518      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND,
1519      sizeof (struct ClientPhoneSuspendMessage) },
1520     {&handle_client_resume_message, NULL,
1521      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME,
1522      sizeof (struct ClientPhoneResumeMessage) },
1523     {&handle_client_hangup_message, NULL,
1524      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP,
1525      sizeof (struct ClientPhoneHangupMessage) },
1526     {&handle_client_call_message, NULL,
1527      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_CALL,
1528      sizeof (struct ClientCallMessage) },
1529     {&handle_client_audio_message, NULL,
1530      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO,
1531      0},
1532     {NULL, NULL, 0, 0}
1533   };
1534   static struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1535     {&handle_cadet_ring_message,
1536      GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_PHONE_RING,
1537      sizeof (struct CadetPhoneRingMessage)},
1538     {&handle_cadet_hangup_message,
1539      GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_PHONE_HANG_UP,
1540      sizeof (struct CadetPhoneHangupMessage)},
1541     {&handle_cadet_pickup_message,
1542      GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_PHONE_PICK_UP,
1543      sizeof (struct CadetPhonePickupMessage)},
1544     {&handle_cadet_suspend_message,
1545      GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_PHONE_SUSPEND,
1546      sizeof (struct CadetPhoneSuspendMessage)},
1547     {&handle_cadet_resume_message,
1548      GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_PHONE_RESUME,
1549      sizeof (struct CadetPhoneResumeMessage)},
1550     {&handle_cadet_audio_message, GNUNET_MESSAGE_TYPE_CONVERSATION_CADET_AUDIO,
1551      0},
1552     {NULL, 0, 0}
1553   };
1554   static uint32_t ports[] = {
1555     GNUNET_APPLICATION_TYPE_CONVERSATION_CONTROL,
1556     GNUNET_APPLICATION_TYPE_CONVERSATION_AUDIO,
1557     0
1558   };
1559
1560   cfg = c;
1561   GNUNET_assert (GNUNET_OK ==
1562                  GNUNET_CRYPTO_get_peer_identity (cfg,
1563                                                   &my_identity));
1564   cadet = GNUNET_CADET_connect (cfg,
1565                               NULL,
1566                               &inbound_channel,
1567                               &inbound_end,
1568                               cadet_handlers,
1569                               ports);
1570
1571   if (NULL == cadet)
1572   {
1573     GNUNET_break (0);
1574     GNUNET_SCHEDULER_shutdown ();
1575     return;
1576   }
1577   nc = GNUNET_SERVER_notification_context_create (server, 16);
1578   GNUNET_SERVER_add_handlers (server, server_handlers);
1579   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1580   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1581                                 &do_shutdown,
1582                                 NULL);
1583 }
1584
1585
1586 /**
1587  * The main function for the conversation service.
1588  *
1589  * @param argc number of arguments from the command line
1590  * @param argv command line arguments
1591  * @return 0 ok, 1 on error
1592  */
1593 int
1594 main (int argc,
1595       char *const *argv)
1596 {
1597   return (GNUNET_OK ==
1598           GNUNET_SERVICE_run (argc, argv,
1599                               "conversation",
1600                               GNUNET_SERVICE_OPTION_NONE,
1601                               &run, NULL)) ? 0 : 1;
1602 }
1603
1604 /* end of gnunet-service-conversation.c */