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