-nicer logs
[oweals/gnunet.git] / src / conversation / gnunet-service-conversation.c
1 /*
2   This file is part of GNUnet.
3   (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_CRYPTO_ecdsa_sign (&msg->caller_id,
740                             &ring->purpose,
741                             &ring->signature);
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);
746 }
747
748
749 /**
750  * Transmit audio data via unreliable cadet channel.
751  *
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
756  */
757 static size_t
758 transmit_line_audio (void *cls,
759                      size_t size,
760                      void *buf)
761 {
762   struct Channel *ch = cls;
763   struct CadetAudioMessage *mam = buf;
764
765   ch->unreliable_mth = NULL;
766   if ( (NULL == buf) ||
767        (size < sizeof (struct CadetAudioMessage) + ch->audio_size) )
768     {
769     /* eh, other error handling? */
770     return 0;
771   }
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;
783 }
784
785
786 /**
787  * Function to handle audio data from the client
788  *
789  * @param cls closure, NULL
790  * @param client the client from which the message is
791  * @param message the message from the client
792  */
793 static void
794 handle_client_audio_message (void *cls,
795                              struct GNUNET_SERVER_Client *client,
796                              const struct GNUNET_MessageHeader *message)
797 {
798   const struct ClientAudioMessage *msg;
799   struct Line *line;
800   struct Channel *ch;
801   size_t size;
802
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);
806   if (NULL == line)
807   {
808     GNUNET_break (0);
809     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
810     return;
811   }
812   for (ch = line->channel_head; NULL != ch; ch = ch->next)
813     if (msg->cid == ch->cid)
814       break;
815   if (NULL == ch)
816   {
817     /* could have been destroyed asynchronously, ignore message */
818     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
819                 "Channel %u not found\n",
820                 msg->cid);
821     GNUNET_SERVER_receive_done (client, GNUNET_OK);
822     return;
823   }
824
825   switch (ch->status)
826   {
827   case CS_CALLEE_RINGING:
828   case CS_CALLER_CALLING:
829     GNUNET_break (0);
830     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
831     return;
832   case CS_CALLEE_CONNECTED:
833   case CS_CALLER_CONNECTED:
834     /* common case, handled below */
835     break;
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);
841     return;
842   }
843   if (GNUNET_YES == ch->suspended_local)
844   {
845     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "This channel is suspended locally\n");
846     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
847     return;
848   }
849   if (NULL == ch->channel_unreliable)
850   {
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);
854     return;
855   }
856   if (NULL != ch->unreliable_mth)
857   {
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;
866   }
867   ch->audio_size = size;
868   ch->audio_data = GNUNET_malloc (ch->audio_size);
869   memcpy (ch->audio_data,
870           &msg[1],
871           size);
872   ch->unreliable_mth = GNUNET_CADET_notify_transmit_ready (ch->channel_unreliable,
873                                                           GNUNET_NO,
874                                                           GNUNET_TIME_UNIT_FOREVER_REL,
875                                                           sizeof (struct CadetAudioMessage)
876                                                           + ch->audio_size,
877                                                           &transmit_line_audio,
878                                                           ch);
879   GNUNET_SERVER_receive_done (client, GNUNET_OK);
880 }
881
882
883 /**
884  * We are done signalling shutdown to the other peer.
885  * Destroy the channel.
886  *
887  * @param cls the `struct GNUNET_CADET_channel` to destroy
888  */
889 static void
890 mq_done_destroy_channel (void *cls)
891 {
892   struct GNUNET_CADET_Channel *channel = cls;
893
894   GNUNET_CADET_channel_destroy (channel);
895 }
896
897
898 /**
899  * Function to handle a ring message incoming over cadet
900  *
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
906  * @return #GNUNET_OK
907  */
908 static int
909 handle_cadet_ring_message (void *cls,
910                           struct GNUNET_CADET_Channel *channel,
911                           void **channel_ctx,
912                           const struct GNUNET_MessageHeader *message)
913 {
914   const struct CadetPhoneRingMessage *msg;
915   struct Line *line;
916   struct Channel *ch;
917   struct GNUNET_MQ_Envelope *e;
918   struct CadetPhoneHangupMessage *hang_up;
919   struct ClientPhoneRingMessage cring;
920   struct GNUNET_MQ_Handle *reliable_mq;
921
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))) ||
927        (GNUNET_OK !=
928         GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_CONVERSATION_RING,
929                                   &msg->purpose,
930                                   &msg->signature,
931                                   &msg->caller_id)) )
932   {
933     GNUNET_break_op (0);
934     return GNUNET_SYSERR;
935   }
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))
939       break;
940   if (NULL == line)
941   {
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,
949                            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? */
953     return GNUNET_OK;
954   }
955   ch = GNUNET_new (struct Channel);
956   ch->line = line;
957   GNUNET_CONTAINER_DLL_insert (line->channel_head,
958                                line->channel_tail,
959                                ch);
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;
966   *channel_ctx = ch;
967   cring.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RING);
968   cring.header.size = htons (sizeof (cring));
969   cring.cid = ch->cid;
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,
975                                               line->client,
976                                               &cring.header,
977                                               GNUNET_NO);
978   return GNUNET_OK;
979 }
980
981
982 /**
983  * Function to handle a hangup message incoming over cadet
984  *
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
990  * @return #GNUNET_OK
991  */
992 static int
993 handle_cadet_hangup_message (void *cls,
994                             struct GNUNET_CADET_Channel *channel,
995                             void **channel_ctx,
996                             const struct GNUNET_MessageHeader *message)
997 {
998   struct Channel *ch = *channel_ctx;
999   struct Line *line;
1000   struct ClientPhoneHangupMessage hup;
1001   enum ChannelStatus status;
1002
1003   if (NULL == ch)
1004   {
1005     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1006                 "HANGUP message received for non-existing line, dropping channel.\n");
1007     return GNUNET_SYSERR;
1008   }
1009   line = ch->line;
1010   *channel_ctx = NULL;
1011   hup.header.size = htons (sizeof (hup));
1012   hup.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP);
1013   hup.cid = ch->cid;
1014   status = ch->status;
1015   GNUNET_CADET_receive_done (channel);
1016   destroy_line_cadet_channels (ch);
1017   switch (status)
1018   {
1019   case CS_CALLEE_RINGING:
1020   case CS_CALLEE_CONNECTED:
1021     break;
1022   case CS_CALLEE_SHUTDOWN:
1023     return GNUNET_OK;
1024   case CS_CALLER_CALLING:
1025   case CS_CALLER_CONNECTED:
1026     break;
1027   case CS_CALLER_SHUTDOWN:
1028     return GNUNET_OK;
1029   }
1030   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1031               "Sending HANG UP message to client\n");
1032   GNUNET_SERVER_notification_context_unicast (nc,
1033                                               line->client,
1034                                               &hup.header,
1035                                               GNUNET_NO);
1036   return GNUNET_OK;
1037 }
1038
1039
1040 /**
1041  * Function to handle a pickup message incoming over cadet
1042  *
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
1049  */
1050 static int
1051 handle_cadet_pickup_message (void *cls,
1052                             struct GNUNET_CADET_Channel *channel,
1053                             void **channel_ctx,
1054                             const struct GNUNET_MessageHeader *message)
1055 {
1056   struct Channel *ch = *channel_ctx;
1057   struct Line *line;
1058   struct ClientPhonePickupMessage pick;
1059
1060   if (NULL == ch)
1061   {
1062     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1063                 "PICKUP message received for non-existing channel, dropping channel.\n");
1064     return GNUNET_SYSERR;
1065   }
1066   line = ch->line;
1067   GNUNET_CADET_receive_done (channel);
1068   switch (ch->status)
1069   {
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);
1078     break;
1079   case CS_CALLER_CALLING:
1080     ch->status = CS_CALLER_CONNECTED;
1081     break;
1082   case CS_CALLER_CONNECTED:
1083     GNUNET_break_op (0);
1084     return GNUNET_OK;
1085   case CS_CALLER_SHUTDOWN:
1086     GNUNET_break_op (0);
1087     mq_done_finish_caller_shutdown (ch);
1088     return GNUNET_SYSERR;
1089   }
1090   pick.header.size = htons (sizeof (pick));
1091   pick.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_PICKED_UP);
1092   pick.cid = ch->cid;
1093   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1094               "Sending PICKED UP message to client\n");
1095   GNUNET_SERVER_notification_context_unicast (nc,
1096                                               line->client,
1097                                               &pick.header,
1098                                               GNUNET_NO);
1099   ch->channel_unreliable = GNUNET_CADET_channel_create (cadet,
1100                                                        ch,
1101                                                        &ch->target,
1102                                                        GNUNET_APPLICATION_TYPE_CONVERSATION_AUDIO,
1103                                                        GNUNET_CADET_OPTION_DEFAULT);
1104   if (NULL == ch->channel_unreliable)
1105   {
1106     GNUNET_break (0);
1107   }
1108   return GNUNET_OK;
1109 }
1110
1111
1112 /**
1113  * Function to handle a suspend message incoming over cadet
1114  *
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
1121  */
1122 static int
1123 handle_cadet_suspend_message (void *cls,
1124                              struct GNUNET_CADET_Channel *channel,
1125                              void **channel_ctx,
1126                              const struct GNUNET_MessageHeader *message)
1127 {
1128   struct Channel *ch = *channel_ctx;
1129   struct Line *line;
1130   struct ClientPhoneSuspendMessage suspend;
1131
1132   if (NULL == ch)
1133   {
1134     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1135                 "SUSPEND message received for non-existing line, dropping channel.\n");
1136     return GNUNET_SYSERR;
1137   }
1138   line = ch->line;
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);
1146   switch (ch->status)
1147   {
1148   case CS_CALLEE_RINGING:
1149     GNUNET_break_op (0);
1150     break;
1151   case CS_CALLEE_CONNECTED:
1152     ch->suspended_remote = GNUNET_YES;
1153     break;
1154   case CS_CALLEE_SHUTDOWN:
1155     return GNUNET_OK;
1156   case CS_CALLER_CALLING:
1157     GNUNET_break_op (0);
1158     break;
1159   case CS_CALLER_CONNECTED:
1160     ch->suspended_remote = GNUNET_YES;
1161     break;
1162   case CS_CALLER_SHUTDOWN:
1163     return GNUNET_OK;
1164   }
1165   GNUNET_SERVER_notification_context_unicast (nc,
1166                                               line->client,
1167                                               &suspend.header,
1168                                               GNUNET_NO);
1169   return GNUNET_OK;
1170 }
1171
1172
1173 /**
1174  * Function to handle a resume message incoming over cadet
1175  *
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
1182  */
1183 static int
1184 handle_cadet_resume_message (void *cls,
1185                              struct GNUNET_CADET_Channel *channel,
1186                              void **channel_ctx,
1187                              const struct GNUNET_MessageHeader *message)
1188 {
1189   struct Channel *ch = *channel_ctx;
1190   struct Line *line;
1191   struct ClientPhoneResumeMessage resume;
1192
1193   if (NULL == ch)
1194   {
1195     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1196                 "RESUME message received for non-existing line, dropping channel.\n");
1197     return GNUNET_SYSERR;
1198   }
1199   line = ch->line;
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)
1205   {
1206     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207                 "RESUME message received for non-suspended channel, dropping channel.\n");
1208     return GNUNET_SYSERR;
1209   }
1210   switch (ch->status)
1211   {
1212   case CS_CALLEE_RINGING:
1213     GNUNET_break (0);
1214     break;
1215   case CS_CALLEE_CONNECTED:
1216     ch->suspended_remote = GNUNET_NO;
1217     break;
1218   case CS_CALLEE_SHUTDOWN:
1219     return GNUNET_OK;
1220   case CS_CALLER_CALLING:
1221     GNUNET_break (0);
1222     break;
1223   case CS_CALLER_CONNECTED:
1224     ch->suspended_remote = GNUNET_NO;
1225     break;
1226   case CS_CALLER_SHUTDOWN:
1227     return GNUNET_OK;
1228   }
1229   GNUNET_SERVER_notification_context_unicast (nc,
1230                                               line->client,
1231                                               &resume.header,
1232                                               GNUNET_NO);
1233   return GNUNET_OK;
1234 }
1235
1236
1237 /**
1238  * Function to handle an audio message incoming over cadet
1239  *
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
1246  */
1247 static int
1248 handle_cadet_audio_message (void *cls,
1249                            struct GNUNET_CADET_Channel *channel,
1250                            void **channel_ctx,
1251                            const struct GNUNET_MessageHeader *message)
1252 {
1253   const struct CadetAudioMessage *msg;
1254   struct Channel *ch = *channel_ctx;
1255   struct Line *line;
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;
1261
1262   msg = (const struct CadetAudioMessage *) message;
1263   if (NULL == ch)
1264   {
1265     info = GNUNET_CADET_channel_get_info (channel,
1266                                          GNUNET_CADET_OPTION_PEER);
1267     if (NULL == info)
1268     {
1269       GNUNET_break (0);
1270       return GNUNET_SYSERR;
1271     }
1272     sender = info->peer;
1273     for (line = lines_head; NULL != line; line = line->next)
1274       if (line->local_line == ntohl (msg->remote_line))
1275       {
1276         for (ch = line->channel_head; NULL != ch; ch = ch->next)
1277         {
1278           if ( (CS_CALLEE_CONNECTED == ch->status) &&
1279                (0 == memcmp (&ch->target,
1280                              &sender,
1281                              sizeof (struct GNUNET_PeerIdentity))) &&
1282                (NULL == ch->channel_unreliable) &&
1283                (ch->remote_line == ntohl (msg->source_line)) )
1284             break;
1285         }
1286         break;
1287       }
1288     if (NULL == line)
1289     {
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;
1294     }
1295     if (NULL == ch)
1296     {
1297       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1298                   "Received %u bytes of AUDIO data for unknown sender.\n",
1299                   msize);
1300       return GNUNET_SYSERR;
1301     }
1302     if ((GNUNET_YES == ch->suspended_local) || (GNUNET_YES == ch->suspended_remote))
1303     {
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);
1308       return GNUNET_OK;
1309     }
1310     ch->channel_unreliable = channel;
1311     *channel_ctx = ch;
1312   }
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);
1320   cam->cid = ch->cid;
1321   memcpy (&cam[1], &msg[1], msize);
1322   GNUNET_SERVER_notification_context_unicast (nc,
1323                                               ch->line->client,
1324                                               &cam->header,
1325                                               GNUNET_YES);
1326   return GNUNET_OK;
1327 }
1328
1329
1330 /**
1331  * Method called whenever another peer has added us to a channel
1332  * the other peer initiated.
1333  *
1334  * @param cls closure
1335  * @param channel new handle to the channel
1336  * @param initiator peer that started the channel
1337  * @param port port
1338  * @param options channel option flags
1339  * @return initial channel context for the channel;
1340  *         (can be NULL -- that's not an error)
1341  */
1342 static void *
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)
1347 {
1348   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1349               _("Received incoming Cadet channel on port %u\n"),
1350               (unsigned int) port);
1351   return NULL;
1352 }
1353
1354
1355 /**
1356  * Function called whenever an inbound channel is destroyed.  Should clean up
1357  * any associated state.
1358  *
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`
1364  */
1365 static void
1366 inbound_end (void *cls,
1367              const struct GNUNET_CADET_Channel *channel,
1368              void *channel_ctx)
1369 {
1370   struct Channel *ch = channel_ctx;
1371   struct Line *line;
1372   struct ClientPhoneHangupMessage hup;
1373
1374   if (NULL == ch)
1375   {
1376     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1377                 "Cadet channel destroyed, but channel is unknown to us\n");
1378     return;
1379   }
1380   line = ch->line;
1381   if (ch->channel_unreliable == channel)
1382   {
1383     if (NULL != ch->unreliable_mth)
1384     {
1385       GNUNET_CADET_notify_transmit_ready_cancel (ch->unreliable_mth);
1386       ch->unreliable_mth = NULL;
1387     }
1388     ch->channel_unreliable = NULL;
1389     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1390                 "Unreliable channel destroyed\n");
1391     return;
1392   }
1393   if (ch->channel_reliable != channel)
1394   {
1395     /* recursive call, I'm the one destroying 'ch' right now */
1396     return;
1397   }
1398   ch->channel_reliable = NULL;
1399
1400   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1401               "Cadet channel destroyed by Cadet in state %d\n",
1402               ch->status);
1403   hup.header.size = htons (sizeof (hup));
1404   hup.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP);
1405   hup.cid = ch->cid;
1406   switch (ch->status)
1407   {
1408   case CS_CALLEE_RINGING:
1409   case CS_CALLEE_CONNECTED:
1410     GNUNET_SERVER_notification_context_unicast (nc,
1411                                                 line->client,
1412                                                 &hup.header,
1413                                                 GNUNET_NO);
1414     break;
1415   case CS_CALLEE_SHUTDOWN:
1416     break;
1417   case CS_CALLER_CALLING:
1418   case CS_CALLER_CONNECTED:
1419     GNUNET_SERVER_notification_context_unicast (nc,
1420                                                 line->client,
1421                                                 &hup.header,
1422                                                 GNUNET_NO);
1423     break;
1424   case CS_CALLER_SHUTDOWN:
1425     break;
1426   }
1427   destroy_line_cadet_channels (ch);
1428 }
1429
1430
1431 /**
1432  * A client disconnected.  Remove all of its data structure entries.
1433  *
1434  * @param cls closure, NULL
1435  * @param client identification of the client
1436  */
1437 static void
1438 handle_client_disconnect (void *cls,
1439                           struct GNUNET_SERVER_Client *client)
1440 {
1441   struct Line *line;
1442
1443   if (NULL == client)
1444     return;
1445   line = GNUNET_SERVER_client_get_user_context (client, struct Line);
1446   if (NULL == line)
1447     return;
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,
1452                                lines_tail,
1453                                line);
1454   while (NULL != line->channel_head)
1455     destroy_line_cadet_channels (line->channel_head);
1456   GNUNET_free (line);
1457 }
1458
1459
1460 /**
1461  * Shutdown nicely
1462  *
1463  * @param cls closure, NULL
1464  * @param tc the task context
1465  */
1466 static void
1467 do_shutdown (void *cls,
1468              const struct GNUNET_SCHEDULER_TaskContext *tc)
1469 {
1470   struct Line *line;
1471   struct Channel *ch;
1472
1473   while (NULL != (line = lines_head))
1474   {
1475     while (NULL != (ch = line->channel_head))
1476       destroy_line_cadet_channels (ch);
1477     GNUNET_CONTAINER_DLL_remove (lines_head,
1478                                  lines_tail,
1479                                  line);
1480     GNUNET_SERVER_client_set_user_context (line->client, (void *) NULL);
1481     GNUNET_free (line);
1482   }
1483   if (NULL != cadet)
1484   {
1485     GNUNET_CADET_disconnect (cadet);
1486     cadet = NULL;
1487   }
1488   if (NULL != nc)
1489   {
1490     GNUNET_SERVER_notification_context_destroy (nc);
1491     nc = NULL;
1492   }
1493 }
1494
1495
1496 /**
1497  * Main function that will be run by the scheduler.
1498  *
1499  * @param cls closure
1500  * @param server server handle
1501  * @param c configuration
1502  */
1503 static void
1504 run (void *cls,
1505      struct GNUNET_SERVER_Handle *server,
1506      const struct GNUNET_CONFIGURATION_Handle *c)
1507 {
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,
1529      0},
1530     {NULL, NULL, 0, 0}
1531   };
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,
1549      0},
1550     {NULL, 0, 0}
1551   };
1552   static uint32_t ports[] = {
1553     GNUNET_APPLICATION_TYPE_CONVERSATION_CONTROL,
1554     GNUNET_APPLICATION_TYPE_CONVERSATION_AUDIO,
1555     0
1556   };
1557
1558   cfg = c;
1559   GNUNET_assert (GNUNET_OK ==
1560                  GNUNET_CRYPTO_get_peer_identity (cfg,
1561                                                   &my_identity));
1562   cadet = GNUNET_CADET_connect (cfg,
1563                               NULL,
1564                               &inbound_channel,
1565                               &inbound_end,
1566                               cadet_handlers,
1567                               ports);
1568
1569   if (NULL == cadet)
1570   {
1571     GNUNET_break (0);
1572     GNUNET_SCHEDULER_shutdown ();
1573     return;
1574   }
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,
1579                                 &do_shutdown,
1580                                 NULL);
1581 }
1582
1583
1584 /**
1585  * The main function for the conversation service.
1586  *
1587  * @param argc number of arguments from the command line
1588  * @param argv command line arguments
1589  * @return 0 ok, 1 on error
1590  */
1591 int
1592 main (int argc,
1593       char *const *argv)
1594 {
1595   return (GNUNET_OK ==
1596           GNUNET_SERVICE_run (argc, argv,
1597                               "conversation",
1598                               GNUNET_SERVICE_OPTION_NONE,
1599                               &run, NULL)) ? 0 : 1;
1600 }
1601
1602 /* end of gnunet-service-conversation.c */