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