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