- debug
[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_mesh_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 mesh channel (or, if it is an
48  * open line, is waiting for a mesh 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 mesh channel, which is a P2P
92  * connection to another conversation service.  Multiple channels can
93  * be attached the the same `struct Line`, which represents a local
94  * client.  We keep them in a linked list.
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_MESH_Channel *channel_reliable;
118
119   /**
120    * Handle for unreliable channel (audio data)
121    */
122   struct GNUNET_MESH_Channel *channel_unreliable;
123
124   /**
125    * Transmit handle for pending audio messages
126    */
127   struct GNUNET_MESH_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 mesh 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 mesh
232  */
233 static struct GNUNET_MESH_Handle *mesh;
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 MeshPhonePickupMessage *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     return;
329   }
330   switch (ch->status)
331   {
332   case CS_CALLEE_RINGING:
333     ch->status = CS_CALLEE_CONNECTED;
334     break;
335   case CS_CALLEE_CONNECTED:
336     GNUNET_break (0);
337     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
338     return;
339   case CS_CALLEE_SHUTDOWN:
340     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341                 "Ignoring client's PICKUP message, line is in SHUTDOWN\n");
342     GNUNET_SERVER_receive_done (client, GNUNET_OK);
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 mesh\n");
354   e = GNUNET_MQ_msg (mppm,
355                      GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_PICK_UP);
356   GNUNET_MQ_send (ch->reliable_mq, e);
357   GNUNET_SERVER_receive_done (client, GNUNET_OK);
358 }
359
360
361 /**
362  * Destroy a channel.
363  *
364  * @param ch channel to destroy.
365  */
366 static void
367 destroy_line_mesh_channels (struct Channel *ch)
368 {
369   struct Line *line = ch->line;
370   struct GNUNET_MESH_Channel *t;
371
372   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373               "Destroying mesh 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_MESH_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_MESH_channel_destroy (t);
388   }
389   if (NULL != (t = ch->channel_reliable))
390   {
391     ch->channel_reliable = NULL;
392     GNUNET_MESH_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_mesh_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_mesh_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 MeshPhoneHangupMessage *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
476   switch (ch->status)
477   {
478   case CS_CALLEE_RINGING:
479     ch->status = CS_CALLEE_SHUTDOWN;
480     break;
481   case CS_CALLEE_CONNECTED:
482     ch->status = CS_CALLEE_SHUTDOWN;
483     break;
484   case CS_CALLEE_SHUTDOWN:
485     /* maybe the other peer closed asynchronously... */
486     return;
487   case CS_CALLER_CALLING:
488     ch->status = CS_CALLER_SHUTDOWN;
489     break;
490   case CS_CALLER_CONNECTED:
491     ch->status = CS_CALLER_SHUTDOWN;
492     break;
493   case CS_CALLER_SHUTDOWN:
494     /* maybe the other peer closed asynchronously... */
495     return;
496   }
497   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498               "Sending HANG_UP message via mesh\n");
499   e = GNUNET_MQ_msg (mhum,
500                      GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_HANG_UP);
501   GNUNET_MQ_notify_sent (e,
502                          &mq_done_finish_caller_shutdown,
503                          ch);
504   GNUNET_MQ_send (ch->reliable_mq, e);
505   GNUNET_SERVER_receive_done (client, GNUNET_OK);
506 }
507
508
509 /**
510  * Function to handle a suspend request message from the client
511  *
512  * @param cls closure, NULL
513  * @param client the client from which the message is
514  * @param message the message from the client
515  */
516 static void
517 handle_client_suspend_message (void *cls,
518                                struct GNUNET_SERVER_Client *client,
519                                const struct GNUNET_MessageHeader *message)
520 {
521   const struct ClientPhoneSuspendMessage *msg;
522   struct GNUNET_MQ_Envelope *e;
523   struct MeshPhoneSuspendMessage *mhum;
524   struct Line *line;
525   struct Channel *ch;
526
527   msg = (const struct ClientPhoneSuspendMessage *) message;
528   line = GNUNET_SERVER_client_get_user_context (client, struct Line);
529   if (NULL == line)
530   {
531     GNUNET_break (0);
532     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
533     return;
534   }
535   for (ch = line->channel_head; NULL != ch; ch = ch->next)
536     if (msg->cid == ch->cid)
537       break;
538   if (NULL == ch)
539   {
540     /* could have been destroyed asynchronously, ignore message */
541     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542                 "Channel %u not found\n",
543                 msg->cid);
544     return;
545   }
546   if (GNUNET_YES == ch->suspended_local)
547   {
548     GNUNET_break (0);
549     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
550     return;
551   }
552   switch (ch->status)
553   {
554   case CS_CALLEE_RINGING:
555     GNUNET_break (0);
556     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
557     return;
558   case CS_CALLEE_CONNECTED:
559     ch->suspended_local = GNUNET_YES;
560     break;
561   case CS_CALLEE_SHUTDOWN:
562     /* maybe the other peer closed asynchronously... */
563     return;
564   case CS_CALLER_CALLING:
565     GNUNET_break (0);
566     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
567     return;
568   case CS_CALLER_CONNECTED:
569     ch->suspended_local = GNUNET_YES;
570     break;
571   case CS_CALLER_SHUTDOWN:
572     /* maybe the other peer closed asynchronously... */
573     return;
574   }
575   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
576               "Sending SUSPEND message via mesh\n");
577   e = GNUNET_MQ_msg (mhum,
578                      GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_SUSPEND);
579   GNUNET_MQ_send (ch->reliable_mq, e);
580   GNUNET_SERVER_receive_done (client, GNUNET_OK);
581 }
582
583
584 /**
585  * Function to handle a resume request message from the client
586  *
587  * @param cls closure, NULL
588  * @param client the client from which the message is
589  * @param message the message from the client
590  */
591 static void
592 handle_client_resume_message (void *cls,
593                               struct GNUNET_SERVER_Client *client,
594                               const struct GNUNET_MessageHeader *message)
595 {
596   const struct ClientPhoneResumeMessage *msg;
597   struct GNUNET_MQ_Envelope *e;
598   struct MeshPhoneResumeMessage *mhum;
599   struct Line *line;
600   struct Channel *ch;
601
602   msg = (const struct ClientPhoneResumeMessage *) message;
603   line = GNUNET_SERVER_client_get_user_context (client, struct Line);
604   if (NULL == line)
605   {
606     GNUNET_break (0);
607     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
608     return;
609   }
610   for (ch = line->channel_head; NULL != ch; ch = ch->next)
611     if (msg->cid == ch->cid)
612       break;
613   if (NULL == ch)
614   {
615     /* could have been destroyed asynchronously, ignore message */
616     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
617                 "Channel %u not found\n",
618                 msg->cid);
619     return;
620   }
621   if (GNUNET_YES != ch->suspended_local)
622   {
623     GNUNET_break (0);
624     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
625     return;
626   }
627   switch (ch->status)
628   {
629   case CS_CALLEE_RINGING:
630     GNUNET_break (0);
631     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
632     return;
633   case CS_CALLEE_CONNECTED:
634     ch->suspended_local = GNUNET_NO;
635     break;
636   case CS_CALLEE_SHUTDOWN:
637     /* maybe the other peer closed asynchronously... */
638     return;
639   case CS_CALLER_CALLING:
640     GNUNET_break (0);
641     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
642     return;
643   case CS_CALLER_CONNECTED:
644     ch->suspended_local = GNUNET_NO;
645     break;
646   case CS_CALLER_SHUTDOWN:
647     /* maybe the other peer closed asynchronously... */
648     return;
649   }
650   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
651               "Sending RESUME message via mesh\n");
652   e = GNUNET_MQ_msg (mhum,
653                      GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_RESUME);
654   GNUNET_MQ_send (ch->reliable_mq, e);
655   GNUNET_SERVER_receive_done (client, GNUNET_OK);
656 }
657
658
659 /**
660  * Function to handle call request from the client
661  *
662  * @param cls closure, NULL
663  * @param client the client from which the message is
664  * @param message the message from the client
665  */
666 static void
667 handle_client_call_message (void *cls,
668                             struct GNUNET_SERVER_Client *client,
669                             const struct GNUNET_MessageHeader *message)
670 {
671   const struct ClientCallMessage *msg;
672   struct Line *line;
673   struct Channel *ch;
674   struct GNUNET_MQ_Envelope *e;
675   struct MeshPhoneRingMessage *ring;
676
677   msg = (const struct ClientCallMessage *) message;
678   line = GNUNET_SERVER_client_get_user_context (client, struct Line);
679   if (NULL != line)
680   {
681     GNUNET_break (0);
682     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
683     return;
684   }
685   line = GNUNET_new (struct Line);
686   line->client = client;
687   line->local_line = (local_line_cnt++) | (1 << 31);
688   GNUNET_SERVER_client_set_user_context (client, line);
689   GNUNET_SERVER_notification_context_add (nc, client);
690   GNUNET_CONTAINER_DLL_insert (lines_head,
691                                lines_tail,
692                                line);
693   ch = GNUNET_new (struct Channel);
694   ch->line = line;
695   GNUNET_CONTAINER_DLL_insert (line->channel_head,
696                                line->channel_tail,
697                                ch);
698   ch->target = msg->target;
699   ch->remote_line = ntohl (msg->line);
700   ch->status = CS_CALLER_CALLING;
701   ch->channel_reliable = GNUNET_MESH_channel_create (mesh,
702                                                      ch,
703                                                      &msg->target,
704                                                      GNUNET_APPLICATION_TYPE_CONVERSATION_CONTROL,
705                                                      GNUNET_MESH_OPTION_RELIABLE);
706   ch->reliable_mq = GNUNET_MESH_mq_create (ch->channel_reliable);
707   e = GNUNET_MQ_msg (ring, GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_RING);
708   ring->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_CONVERSATION_RING);
709   ring->purpose.size = htonl (sizeof (struct GNUNET_PeerIdentity) * 2 +
710                               sizeof (struct GNUNET_TIME_AbsoluteNBO) +
711                               sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
712                               sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
713   GNUNET_CRYPTO_ecdsa_key_get_public (&msg->caller_id,
714                                       &ring->caller_id);
715   ring->remote_line = msg->line;
716   ring->source_line = htonl (line->local_line);
717   ring->target = msg->target;
718   ring->source = my_identity;
719   ring->expiration_time = GNUNET_TIME_absolute_hton (GNUNET_TIME_relative_to_absolute (RING_TIMEOUT));
720   GNUNET_CRYPTO_ecdsa_sign (&msg->caller_id,
721                             &ring->purpose,
722                             &ring->signature);
723   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
724               "Sending RING message via mesh\n");
725   GNUNET_MQ_send (ch->reliable_mq, e);
726   GNUNET_SERVER_receive_done (client, GNUNET_OK);
727 }
728
729
730 /**
731  * Transmit audio data via unreliable mesh channel.
732  *
733  * @param cls the `struct Channel` we are transmitting for
734  * @param size number of bytes available in @a buf
735  * @param buf where to copy the data
736  * @return number of bytes copied to @a buf
737  */
738 static size_t
739 transmit_line_audio (void *cls,
740                      size_t size,
741                      void *buf)
742 {
743   struct Channel *ch = cls;
744   struct MeshAudioMessage *mam = buf;
745
746   ch->unreliable_mth = NULL;
747   if ( (NULL == buf) ||
748        (size < sizeof (struct MeshAudioMessage) + ch->audio_size) )
749     {
750     /* eh, other error handling? */
751     return 0;
752   }
753   mam->header.size = htons (sizeof (struct MeshAudioMessage) + ch->audio_size);
754   mam->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_AUDIO);
755   mam->remote_line = htonl (ch->remote_line);
756   memcpy (&mam[1], ch->audio_data, ch->audio_size);
757   GNUNET_free (ch->audio_data);
758   ch->audio_data = NULL;
759   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760               "Sending %u bytes of audio data from line %u to remote line %u via mesh\n",
761               ch->audio_size, ch->line->local_line, ch->remote_line);
762   return sizeof (struct MeshAudioMessage) + ch->audio_size;
763 }
764
765
766 /**
767  * Function to handle audio data from the client
768  *
769  * @param cls closure, NULL
770  * @param client the client from which the message is
771  * @param message the message from the client
772  */
773 static void
774 handle_client_audio_message (void *cls,
775                              struct GNUNET_SERVER_Client *client,
776                              const struct GNUNET_MessageHeader *message)
777 {
778   const struct ClientAudioMessage *msg;
779   struct Line *line;
780   struct Channel *ch;
781   size_t size;
782
783   size = ntohs (message->size) - sizeof (struct ClientAudioMessage);
784   msg = (const struct ClientAudioMessage *) message;
785   line = GNUNET_SERVER_client_get_user_context (client, struct Line);
786   if (NULL == line)
787   {
788     GNUNET_break (0);
789     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
790     return;
791   }
792   for (ch = line->channel_head; NULL != ch; ch = ch->next)
793     if (msg->cid == ch->cid)
794       break;
795   if (NULL == ch)
796   {
797     /* could have been destroyed asynchronously, ignore message */
798     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799                 "Channel %u not found\n",
800                 msg->cid);
801     return;
802   }
803
804   switch (ch->status)
805   {
806   case CS_CALLEE_RINGING:
807   case CS_CALLER_CALLING:
808     GNUNET_break (0);
809     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
810     return;
811   case CS_CALLEE_CONNECTED:
812   case CS_CALLER_CONNECTED:
813     /* common case, handled below */
814     break;
815   case CS_CALLEE_SHUTDOWN:
816   case CS_CALLER_SHUTDOWN:
817     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
818                 "Mesh audio channel in shutdown; audio data dropped\n");
819     GNUNET_SERVER_receive_done (client, GNUNET_OK);
820     return;
821   }
822   if (GNUNET_YES == ch->suspended_local)
823   {
824     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "This channel is suspended locally\n");
825     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
826     return;
827   }
828   if (NULL == ch->channel_unreliable)
829   {
830     GNUNET_log (GNUNET_ERROR_TYPE_INFO | GNUNET_ERROR_TYPE_BULK,
831                 _("Mesh audio channel not ready; audio data dropped\n"));
832     GNUNET_SERVER_receive_done (client, GNUNET_OK);
833     return;
834   }
835   if (NULL != ch->unreliable_mth)
836   {
837     /* NOTE: we may want to not do this and instead combine the data */
838     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
839                 "Bandwidth insufficient; dropping previous audio data segment with %u bytes\n",
840                 (unsigned int) ch->audio_size);
841     GNUNET_MESH_notify_transmit_ready_cancel (ch->unreliable_mth);
842     ch->unreliable_mth = NULL;
843     GNUNET_free (ch->audio_data);
844     ch->audio_data = NULL;
845   }
846   ch->audio_size = size;
847   ch->audio_data = GNUNET_malloc (ch->audio_size);
848   memcpy (ch->audio_data,
849           &msg[1],
850           size);
851   ch->unreliable_mth = GNUNET_MESH_notify_transmit_ready (ch->channel_unreliable,
852                                                           GNUNET_NO,
853                                                           GNUNET_TIME_UNIT_FOREVER_REL,
854                                                           sizeof (struct MeshAudioMessage)
855                                                           + ch->audio_size,
856                                                           &transmit_line_audio,
857                                                           ch);
858   GNUNET_SERVER_receive_done (client, GNUNET_OK);
859 }
860
861
862 /**
863  * We are done signalling shutdown to the other peer.
864  * Destroy the channel.
865  *
866  * @param cls the `struct GNUNET_MESH_channel` to destroy
867  */
868 static void
869 mq_done_destroy_channel (void *cls)
870 {
871   struct GNUNET_MESH_Channel *channel = cls;
872
873   GNUNET_MESH_channel_destroy (channel);
874 }
875
876
877 /**
878  * Function to handle a ring message incoming over mesh
879  *
880  * @param cls closure, NULL
881  * @param channel the channel over which the message arrived
882  * @param channel_ctx the channel context, can be NULL
883  *                    or point to the `struct Channel`
884  * @param message the incoming message
885  * @return #GNUNET_OK
886  */
887 static int
888 handle_mesh_ring_message (void *cls,
889                           struct GNUNET_MESH_Channel *channel,
890                           void **channel_ctx,
891                           const struct GNUNET_MessageHeader *message)
892 {
893   const struct MeshPhoneRingMessage *msg;
894   struct Line *line;
895   struct Channel *ch;
896   struct GNUNET_MQ_Envelope *e;
897   struct MeshPhoneHangupMessage *hang_up;
898   struct ClientPhoneRingMessage cring;
899   struct GNUNET_MQ_Handle *reliable_mq;
900
901   msg = (const struct MeshPhoneRingMessage *) message;
902   if ( (msg->purpose.size != htonl (sizeof (struct GNUNET_PeerIdentity) * 2 +
903                                     sizeof (struct GNUNET_TIME_AbsoluteNBO) +
904                                     sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
905                                     sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) ||
906        (GNUNET_OK !=
907         GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_CONVERSATION_RING,
908                                   &msg->purpose,
909                                   &msg->signature,
910                                   &msg->caller_id)) )
911   {
912     GNUNET_break_op (0);
913     return GNUNET_SYSERR;
914   }
915   for (line = lines_head; NULL != line; line = line->next)
916     if (line->local_line == ntohl (msg->remote_line))
917       break;
918   if (NULL == line)
919   {
920     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
921                 _("No available phone for incoming call on line %u, sending HANG_UP signal\n"),
922                 ntohl (msg->remote_line));
923     e = GNUNET_MQ_msg (hang_up,
924                        GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_HANG_UP);
925     GNUNET_MQ_notify_sent (e,
926                            &mq_done_destroy_channel,
927                            channel);
928     reliable_mq = GNUNET_MESH_mq_create (channel);
929     GNUNET_MQ_send (reliable_mq, e);
930     /* FIXME: do we need to clean up reliable_mq somehow/somewhere? */
931     GNUNET_MESH_receive_done (channel); /* needed? */
932     return GNUNET_OK;
933   }
934   ch = GNUNET_new (struct Channel);
935   ch->line = line;
936   GNUNET_CONTAINER_DLL_insert (line->channel_head,
937                                line->channel_tail,
938                                ch);
939   ch->status = CS_CALLEE_RINGING;
940   ch->remote_line = ntohl (msg->source_line);
941   ch->channel_reliable = channel;
942   ch->reliable_mq = GNUNET_MESH_mq_create (ch->channel_reliable);
943   ch->cid = line->cid_gen++;
944   ch->target = msg->source;
945   *channel_ctx = ch;
946   cring.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RING);
947   cring.header.size = htons (sizeof (cring));
948   cring.cid = ch->cid;
949   cring.caller_id = msg->caller_id;
950   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
951               "Sending RING message to client. CID %u:(%u, %u)\n", 
952               ch->cid, ch->remote_line, line->local_line);
953   GNUNET_SERVER_notification_context_unicast (nc,
954                                               line->client,
955                                               &cring.header,
956                                               GNUNET_NO);
957   GNUNET_MESH_receive_done (channel);
958   return GNUNET_OK;
959 }
960
961
962 /**
963  * Function to handle a hangup message incoming over mesh
964  *
965  * @param cls closure, NULL
966  * @param channel the channel over which the message arrived
967  * @param channel_ctx the channel context, can be NULL
968  *                    or point to the `struct Channel`
969  * @param message the incoming message
970  * @return #GNUNET_OK
971  */
972 static int
973 handle_mesh_hangup_message (void *cls,
974                             struct GNUNET_MESH_Channel *channel,
975                             void **channel_ctx,
976                             const struct GNUNET_MessageHeader *message)
977 {
978   struct Channel *ch = *channel_ctx;
979   struct Line *line;
980   struct ClientPhoneHangupMessage hup;
981   enum ChannelStatus status;
982
983   if (NULL == ch)
984   {
985     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
986                 "HANGUP message received for non-existing line, dropping channel.\n");
987     return GNUNET_SYSERR;
988   }
989   line = ch->line;
990   *channel_ctx = NULL;
991   hup.header.size = htons (sizeof (hup));
992   hup.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP);
993   hup.cid = ch->cid;
994   status = ch->status;
995   GNUNET_MESH_receive_done (channel);
996   destroy_line_mesh_channels (ch);
997   switch (status)
998   {
999   case CS_CALLEE_RINGING:
1000   case CS_CALLEE_CONNECTED:
1001     break;
1002   case CS_CALLEE_SHUTDOWN:
1003     return GNUNET_OK;
1004   case CS_CALLER_CALLING:
1005   case CS_CALLER_CONNECTED:
1006     break;
1007   case CS_CALLER_SHUTDOWN:
1008     return GNUNET_OK;
1009   }
1010   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1011               "Sending HANG UP message to client\n");
1012   GNUNET_SERVER_notification_context_unicast (nc,
1013                                               line->client,
1014                                               &hup.header,
1015                                               GNUNET_NO);
1016   return GNUNET_OK;
1017 }
1018
1019
1020 /**
1021  * Function to handle a pickup message incoming over mesh
1022  *
1023  * @param cls closure, NULL
1024  * @param channel the channel over which the message arrived
1025  * @param channel_ctx the channel context, can be NULL
1026  *                    or point to the `struct Channel`
1027  * @param message the incoming message
1028  * @return #GNUNET_OK
1029  */
1030 static int
1031 handle_mesh_pickup_message (void *cls,
1032                             struct GNUNET_MESH_Channel *channel,
1033                             void **channel_ctx,
1034                             const struct GNUNET_MessageHeader *message)
1035 {
1036   struct Channel *ch = *channel_ctx;
1037   struct Line *line;
1038   struct ClientPhonePickupMessage pick;
1039
1040   if (NULL == ch)
1041   {
1042     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1043                 "PICKUP message received for non-existing channel, dropping channel.\n");
1044     return GNUNET_SYSERR;
1045   }
1046   line = ch->line;
1047   GNUNET_MESH_receive_done (channel);
1048   switch (ch->status)
1049   {
1050   case CS_CALLEE_RINGING:
1051   case CS_CALLEE_CONNECTED:
1052     GNUNET_break_op (0);
1053     destroy_line_mesh_channels (ch);
1054     return GNUNET_SYSERR;
1055   case CS_CALLEE_SHUTDOWN:
1056     GNUNET_break_op (0);
1057     destroy_line_mesh_channels (ch);
1058     break;
1059   case CS_CALLER_CALLING:
1060     ch->status = CS_CALLER_CONNECTED;
1061     break;
1062   case CS_CALLER_CONNECTED:
1063     GNUNET_break_op (0);
1064     return GNUNET_OK;
1065   case CS_CALLER_SHUTDOWN:
1066     GNUNET_break_op (0);
1067     mq_done_finish_caller_shutdown (ch);
1068     return GNUNET_SYSERR;
1069   }
1070   pick.header.size = htons (sizeof (pick));
1071   pick.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_PICKED_UP);
1072   pick.cid = ch->cid;
1073   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1074               "Sending PICKED UP message to client\n");
1075   GNUNET_SERVER_notification_context_unicast (nc,
1076                                               line->client,
1077                                               &pick.header,
1078                                               GNUNET_NO);
1079   ch->channel_unreliable = GNUNET_MESH_channel_create (mesh,
1080                                                        ch,
1081                                                        &ch->target,
1082                                                        GNUNET_APPLICATION_TYPE_CONVERSATION_AUDIO,
1083                                                        GNUNET_MESH_OPTION_DEFAULT);
1084   if (NULL == ch->channel_unreliable)
1085   {
1086     GNUNET_break (0);
1087   }
1088   return GNUNET_OK;
1089 }
1090
1091
1092 /**
1093  * Function to handle a suspend message incoming over mesh
1094  *
1095  * @param cls closure, NULL
1096  * @param channel the channel over which the message arrived
1097  * @param channel_ctx the channel context, can be NULL
1098  *                    or point to the `struct Channel`
1099  * @param message the incoming message
1100  * @return #GNUNET_OK
1101  */
1102 static int
1103 handle_mesh_suspend_message (void *cls,
1104                              struct GNUNET_MESH_Channel *channel,
1105                              void **channel_ctx,
1106                              const struct GNUNET_MessageHeader *message)
1107 {
1108   struct Channel *ch = *channel_ctx;
1109   struct Line *line;
1110   struct ClientPhoneSuspendMessage suspend;
1111
1112   if (NULL == ch)
1113   {
1114     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1115                 "SUSPEND message received for non-existing line, dropping channel.\n");
1116     return GNUNET_SYSERR;
1117   }
1118   line = ch->line;
1119   suspend.header.size = htons (sizeof (suspend));
1120   suspend.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND);
1121   suspend.cid = ch->cid;
1122   GNUNET_MESH_receive_done (channel);
1123   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124               "Suspending channel CID: %u(%u:%u)\n",
1125               ch->cid, ch->remote_line, line->local_line);
1126   switch (ch->status)
1127   {
1128   case CS_CALLEE_RINGING:
1129     GNUNET_break_op (0);
1130     break;
1131   case CS_CALLEE_CONNECTED:
1132     ch->suspended_remote = GNUNET_YES;
1133     break;
1134   case CS_CALLEE_SHUTDOWN:
1135     return GNUNET_OK;
1136   case CS_CALLER_CALLING:
1137     GNUNET_break_op (0);
1138     break;
1139   case CS_CALLER_CONNECTED:
1140     ch->suspended_remote = GNUNET_YES;
1141     break;
1142   case CS_CALLER_SHUTDOWN:
1143     return GNUNET_OK;
1144   }
1145   GNUNET_SERVER_notification_context_unicast (nc,
1146                                               line->client,
1147                                               &suspend.header,
1148                                               GNUNET_NO);
1149   return GNUNET_OK;
1150 }
1151
1152
1153 /**
1154  * Function to handle a resume message incoming over mesh
1155  *
1156  * @param cls closure, NULL
1157  * @param channel the channel over which the message arrived
1158  * @param channel_ctx the channel context, can be NULL
1159  *                    or point to the `struct Channel`
1160  * @param message the incoming message
1161  * @return #GNUNET_OK
1162  */
1163 static int
1164 handle_mesh_resume_message (void *cls,
1165                              struct GNUNET_MESH_Channel *channel,
1166                              void **channel_ctx,
1167                              const struct GNUNET_MessageHeader *message)
1168 {
1169   struct Channel *ch = *channel_ctx;
1170   struct Line *line;
1171   struct ClientPhoneResumeMessage resume;
1172
1173   if (NULL == ch)
1174   {
1175     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1176                 "RESUME message received for non-existing line, dropping channel.\n");
1177     return GNUNET_SYSERR;
1178   }
1179   line = ch->line;
1180   resume.header.size = htons (sizeof (resume));
1181   resume.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME);
1182   resume.cid = ch->cid;
1183   GNUNET_MESH_receive_done (channel);
1184   if (GNUNET_YES != ch->suspended_remote)
1185   {
1186     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1187                 "RESUME message received for non-suspended channel, dropping channel.\n");
1188     return GNUNET_SYSERR;
1189   }
1190   switch (ch->status)
1191   {
1192   case CS_CALLEE_RINGING:
1193     GNUNET_break (0);
1194     break;
1195   case CS_CALLEE_CONNECTED:
1196     ch->suspended_remote = GNUNET_NO;
1197     break;
1198   case CS_CALLEE_SHUTDOWN:
1199     return GNUNET_OK;
1200   case CS_CALLER_CALLING:
1201     GNUNET_break (0);
1202     break;
1203   case CS_CALLER_CONNECTED:
1204     ch->suspended_remote = GNUNET_NO;
1205     break;
1206   case CS_CALLER_SHUTDOWN:
1207     return GNUNET_OK;
1208   }
1209   GNUNET_SERVER_notification_context_unicast (nc,
1210                                               line->client,
1211                                               &resume.header,
1212                                               GNUNET_NO);
1213   return GNUNET_OK;
1214 }
1215
1216
1217 /**
1218  * Function to handle an audio message incoming over mesh
1219  *
1220  * @param cls closure, NULL
1221  * @param channel the channel over which the message arrived
1222  * @param channel_ctx the channel context, can be NULL
1223  *                    or point to the `struct Channel`
1224  * @param message the incoming message
1225  * @return #GNUNET_OK
1226  */
1227 static int
1228 handle_mesh_audio_message (void *cls,
1229                            struct GNUNET_MESH_Channel *channel,
1230                            void **channel_ctx,
1231                            const struct GNUNET_MessageHeader *message)
1232 {
1233   const struct MeshAudioMessage *msg;
1234   struct Channel *ch = *channel_ctx;
1235   struct Line *line;
1236   struct GNUNET_PeerIdentity sender;
1237   size_t msize = ntohs (message->size) - sizeof (struct MeshAudioMessage);
1238   char buf[msize + sizeof (struct ClientAudioMessage)];
1239   struct ClientAudioMessage *cam;
1240   const union GNUNET_MESH_ChannelInfo *info;
1241
1242   msg = (const struct MeshAudioMessage *) message;
1243   if (NULL == ch)
1244   {
1245     info = GNUNET_MESH_channel_get_info (channel,
1246                                          GNUNET_MESH_OPTION_PEER);
1247     if (NULL == info)
1248     {
1249       GNUNET_break (0);
1250       return GNUNET_OK;
1251     }
1252     sender = info->peer;
1253     for (line = lines_head; NULL != line; line = line->next)
1254       if (line->local_line == ntohl (msg->remote_line))
1255       {
1256         for (ch = line->channel_head; NULL != ch; ch = ch->next)
1257         {
1258           if ( (CS_CALLEE_CONNECTED == ch->status) &&
1259                (0 == memcmp (&ch->target,
1260                              &sender,
1261                              sizeof (struct GNUNET_PeerIdentity))) &&
1262                (NULL == ch->channel_unreliable) )
1263             break;
1264         }
1265         break;
1266       }
1267     if (NULL == line)
1268     {
1269       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1270                   "Received %u bytes of AUDIO data for non-existing line %u, dropping.\n",
1271                   msize, ntohl (msg->remote_line));
1272       return GNUNET_SYSERR;
1273     }
1274     if (NULL == ch)
1275     {
1276       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1277                   "Received %u bytes of AUDIO data for unknown sender.\n",
1278                   msize);
1279       return GNUNET_SYSERR;
1280     }
1281     if ((GNUNET_YES == ch->suspended_local) || (GNUNET_YES == ch->suspended_remote))
1282     {
1283       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1284                   "Received %u bytes of AUDIO data on suspended channel CID %u:(%u:%u); dropping\n",
1285                   msize, ch->cid, ch->remote_line, line->local_line);
1286       return GNUNET_OK;
1287     }
1288     ch->channel_unreliable = channel;
1289     *channel_ctx = ch;
1290   }
1291   GNUNET_break (ch->line->local_line == ntohl (msg->remote_line));
1292   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1293               "Forwarding %u bytes of AUDIO data to client CID %u:(%u:%u)\n",
1294               msize, ch->cid, ch->remote_line, ch->line->local_line);
1295   cam = (struct ClientAudioMessage *) buf;
1296   cam->header.size = htons (sizeof (buf));
1297   cam->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO);
1298   cam->cid = ch->cid;
1299   memcpy (&cam[1], &msg[1], msize);
1300   GNUNET_SERVER_notification_context_unicast (nc,
1301                                               ch->line->client,
1302                                               &cam->header,
1303                                               GNUNET_YES);
1304   GNUNET_MESH_receive_done (channel);
1305   return GNUNET_OK;
1306 }
1307
1308
1309 /**
1310  * Method called whenever another peer has added us to a channel
1311  * the other peer initiated.
1312  *
1313  * @param cls closure
1314  * @param channel new handle to the channel
1315  * @param initiator peer that started the channel
1316  * @param port port
1317  * @param options channel option flags
1318  * @return initial channel context for the channel;
1319  *         (can be NULL -- that's not an error)
1320  */
1321 static void *
1322 inbound_channel (void *cls,
1323                 struct GNUNET_MESH_Channel *channel,
1324                 const struct GNUNET_PeerIdentity *initiator,
1325                 uint32_t port, enum GNUNET_MESH_ChannelOption options)
1326 {
1327   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1328               _("Received incoming channel on port %u\n"),
1329               (unsigned int) port);
1330   return NULL;
1331 }
1332
1333
1334 /**
1335  * Function called whenever an inbound channel is destroyed.  Should clean up
1336  * any associated state.
1337  *
1338  * @param cls closure (set from #GNUNET_MESH_connect)
1339  * @param channel connection to the other end (henceforth invalid)
1340  * @param channel_ctx place where local state associated
1341  *                   with the channel is stored;
1342  *                   may point to the `struct Channel`
1343  */
1344 static void
1345 inbound_end (void *cls,
1346              const struct GNUNET_MESH_Channel *channel,
1347              void *channel_ctx)
1348 {
1349   struct Channel *ch = channel_ctx;
1350   struct Line *line;
1351   struct ClientPhoneHangupMessage hup;
1352
1353   if (NULL == ch)
1354   {
1355     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1356                 "Mesh channel destroyed, but channel is unknown to us\n");
1357     return;
1358   }
1359   line = ch->line;
1360   if (ch->channel_unreliable == channel)
1361   {
1362     if (NULL != ch->unreliable_mth)
1363     {
1364       GNUNET_MESH_notify_transmit_ready_cancel (ch->unreliable_mth);
1365       ch->unreliable_mth = NULL;
1366     }
1367     ch->channel_unreliable = NULL;
1368     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1369                 "Unreliable channel destroyed\n");
1370     return;
1371   }
1372   if (ch->channel_reliable != channel)
1373   {
1374     /* recursive call, I'm the one destroying 'ch' right now */
1375     return;
1376   }
1377   ch->channel_reliable = NULL;
1378
1379   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1380               "Mesh channel destroyed by mesh in state %d\n",
1381               ch->status);
1382   hup.header.size = htons (sizeof (hup));
1383   hup.header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP);
1384   hup.cid = ch->cid;
1385   switch (ch->status)
1386   {
1387   case CS_CALLEE_RINGING:
1388   case CS_CALLEE_CONNECTED:
1389     GNUNET_SERVER_notification_context_unicast (nc,
1390                                                 line->client,
1391                                                 &hup.header,
1392                                                 GNUNET_NO);
1393     break;
1394   case CS_CALLEE_SHUTDOWN:
1395     break;
1396   case CS_CALLER_CALLING:
1397   case CS_CALLER_CONNECTED:
1398     GNUNET_SERVER_notification_context_unicast (nc,
1399                                                 line->client,
1400                                                 &hup.header,
1401                                                 GNUNET_NO);
1402     break;
1403   case CS_CALLER_SHUTDOWN:
1404     break;
1405   }
1406   destroy_line_mesh_channels (ch);
1407 }
1408
1409
1410 /**
1411  * A client disconnected.  Remove all of its data structure entries.
1412  *
1413  * @param cls closure, NULL
1414  * @param client identification of the client
1415  */
1416 static void
1417 handle_client_disconnect (void *cls,
1418                           struct GNUNET_SERVER_Client *client)
1419 {
1420   struct Line *line;
1421
1422   if (NULL == client)
1423     return;
1424   line = GNUNET_SERVER_client_get_user_context (client, struct Line);
1425   if (NULL == line)
1426     return;
1427   GNUNET_SERVER_client_set_user_context (client, (void *)NULL);
1428   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429               "Client disconnected, closing line\n");
1430   GNUNET_CONTAINER_DLL_remove (lines_head,
1431                                lines_tail,
1432                                line);
1433   while (NULL != line->channel_head)
1434     destroy_line_mesh_channels (line->channel_head);
1435   GNUNET_free (line);
1436 }
1437
1438
1439 /**
1440  * Shutdown nicely
1441  *
1442  * @param cls closure, NULL
1443  * @param tc the task context
1444  */
1445 static void
1446 do_shutdown (void *cls,
1447              const struct GNUNET_SCHEDULER_TaskContext *tc)
1448 {
1449   struct Line *line;
1450   struct Channel *ch;
1451
1452   while (NULL != (line = lines_head))
1453   {
1454     while (NULL != (ch = line->channel_head))
1455       destroy_line_mesh_channels (ch);
1456     GNUNET_CONTAINER_DLL_remove (lines_head,
1457                                  lines_tail,
1458                                  line);
1459     GNUNET_SERVER_client_set_user_context (line->client, (void *) NULL);
1460     GNUNET_free (line);
1461   }
1462   if (NULL != mesh)
1463   {
1464     GNUNET_MESH_disconnect (mesh);
1465     mesh = NULL;
1466   }
1467   if (NULL != nc)
1468   {
1469     GNUNET_SERVER_notification_context_destroy (nc);
1470     nc = NULL;
1471   }
1472 }
1473
1474
1475 /**
1476  * Main function that will be run by the scheduler.
1477  *
1478  * @param cls closure
1479  * @param server server handle
1480  * @param c configuration
1481  */
1482 static void
1483 run (void *cls,
1484      struct GNUNET_SERVER_Handle *server,
1485      const struct GNUNET_CONFIGURATION_Handle *c)
1486 {
1487   static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1488     {&handle_client_register_message, NULL,
1489      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_REGISTER,
1490      sizeof (struct ClientPhoneRegisterMessage)},
1491     {&handle_client_pickup_message, NULL,
1492      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_PICK_UP,
1493      sizeof (struct ClientPhonePickupMessage) },
1494     {&handle_client_suspend_message, NULL,
1495      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND,
1496      sizeof (struct ClientPhoneSuspendMessage) },
1497     {&handle_client_resume_message, NULL,
1498      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME,
1499      sizeof (struct ClientPhoneResumeMessage) },
1500     {&handle_client_hangup_message, NULL,
1501      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP,
1502      sizeof (struct ClientPhoneHangupMessage) },
1503     {&handle_client_call_message, NULL,
1504      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_CALL,
1505      sizeof (struct ClientCallMessage) },
1506     {&handle_client_audio_message, NULL,
1507      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO,
1508      0},
1509     {NULL, NULL, 0, 0}
1510   };
1511   static struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
1512     {&handle_mesh_ring_message,
1513      GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_RING,
1514      sizeof (struct MeshPhoneRingMessage)},
1515     {&handle_mesh_hangup_message,
1516      GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_HANG_UP,
1517      sizeof (struct MeshPhoneHangupMessage)},
1518     {&handle_mesh_pickup_message,
1519      GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_PICK_UP,
1520      sizeof (struct MeshPhonePickupMessage)},
1521     {&handle_mesh_suspend_message,
1522      GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_SUSPEND,
1523      sizeof (struct MeshPhoneSuspendMessage)},
1524     {&handle_mesh_resume_message,
1525      GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_PHONE_RESUME,
1526      sizeof (struct MeshPhoneResumeMessage)},
1527     {&handle_mesh_audio_message, GNUNET_MESSAGE_TYPE_CONVERSATION_MESH_AUDIO,
1528      0},
1529     {NULL, 0, 0}
1530   };
1531   static uint32_t ports[] = {
1532     GNUNET_APPLICATION_TYPE_CONVERSATION_CONTROL,
1533     GNUNET_APPLICATION_TYPE_CONVERSATION_AUDIO,
1534     0
1535   };
1536
1537   cfg = c;
1538   GNUNET_assert (GNUNET_OK ==
1539                  GNUNET_CRYPTO_get_peer_identity (cfg,
1540                                                   &my_identity));
1541   mesh = GNUNET_MESH_connect (cfg,
1542                               NULL,
1543                               &inbound_channel,
1544                               &inbound_end,
1545                               mesh_handlers,
1546                               ports);
1547
1548   if (NULL == mesh)
1549   {
1550     GNUNET_break (0);
1551     GNUNET_SCHEDULER_shutdown ();
1552     return;
1553   }
1554   nc = GNUNET_SERVER_notification_context_create (server, 16);
1555   GNUNET_SERVER_add_handlers (server, server_handlers);
1556   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
1557   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1558                                 &do_shutdown,
1559                                 NULL);
1560 }
1561
1562
1563 /**
1564  * The main function for the conversation service.
1565  *
1566  * @param argc number of arguments from the command line
1567  * @param argv command line arguments
1568  * @return 0 ok, 1 on error
1569  */
1570 int
1571 main (int argc,
1572       char *const *argv)
1573 {
1574   return (GNUNET_OK ==
1575           GNUNET_SERVICE_run (argc, argv,
1576                               "conversation",
1577                               GNUNET_SERVICE_OPTION_NONE,
1578                               &run, NULL)) ? 0 : 1;
1579 }
1580
1581 /* end of gnunet-service-conversation.c */