aboutsummaryrefslogtreecommitdiff
path: root/src/conversation/conversation_api.c
blob: 6e1cbe3fba34c5e85243db8b6d8dc088ad2b38ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
/*
  This file is part of GNUnet
  (C) 2013 Christian Grothoff (and other contributing authors)

  GNUnet is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 3, or (at your
  option) any later version.

  GNUnet is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with GNUnet; see the file COPYING.  If not, write to the
  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  Boston, MA 02111-1307, USA.
 */

/**
 * @file conversation/conversation_api2.c
 * @brief API to the conversation service
 * @author Simon Dieterle
 * @author Andreas Fuchs
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_conversation_service.h"
#include "gnunet_gns_service.h"
#include "conversation.h"


/**
 * Possible states of the phone.
 */
enum PhoneState
{
  /**
   * We still need to register the phone.
   */
  PS_REGISTER = 0,

  /**
   * We are waiting for a call.
   */
  PS_WAITING,

  /**
   * The phone is ringing.
   */
  PS_RINGING,

  /**
   * The phone is in an active conversation.
   */
  PS_ACTIVE
};


/**
 * A phone is a device that can ring to signal an incoming call and
 * that you can pick up to answer the call and hang up to terminate
 * the call.  You can also hang up a ringing phone immediately
 * (without picking it up) to stop it from ringing.  Phones have
 * caller ID.  You can ask the phone for its record and make that
 * record available (via GNS) to enable others to call you.
 * Multiple phones maybe connected to the same line (the line is
 * something rather internal to a phone and not obvious from it).
 * You can only have one conversation per phone at any time.
 */
struct GNUNET_CONVERSATION_Phone
{
  /**
   * Our configuration.
   */
  const struct GNUNET_CONFIGURATION_Handle *cfg;

  /**
   * Handle to talk with CONVERSATION service.
   */
  struct GNUNET_CLIENT_Connection *client;

  /**
   * Function to call for phone events.
   */
  GNUNET_CONVERSATION_EventHandler event_handler;

  /**
   * Closure for @e event_handler
   */
  void *event_handler_cls;

  /**
   * Speaker, or NULL if none is attached.
   */
  struct GNUNET_SPEAKER_Handle *speaker;

  /**
   * Microphone, or NULL if none is attached.
   */
  struct GNUNET_MICROPHONE_Handle *mic;

  /**
   * Connection to NAMESTORE (for reverse lookup).
   */
  struct GNUNET_NAMESTORE_Handle *ns;

  /**
   * Active NAMESTORE lookup (or NULL).
   */
  struct GNUNET_NAMESTORE_QueueEntry *qe;

  /**
   * Handle for transmitting to the CONVERSATION service.
   */
  struct GNUNET_MQ_Handle *mq;

  /**
   * This phone's record.
   */
  struct GNUNET_CONVERSATION_PhoneRecord my_record;

  /**
   * My GNS zone.
   */
  struct GNUNET_CRYPTO_EccPrivateKey my_zone;

  /**
   * Identity of the person calling us (valid while in state #PS_RINGING).
   */
  struct GNUNET_CRYPTO_EccPublicSignKey caller_id;

  /**
   * State machine for the phone.
   */
  enum PhoneState state;

};


/**
 * The phone got disconnected, reconnect to the service.
 *
 * @param phone phone to reconnect
 */
static void
reconnect_phone (struct GNUNET_CONVERSATION_Phone *phone);


/**
 * We have resolved the caller ID using our name service.
 *
 * @param cls the `struct GNUNET_CONVERSATION_Phone`
 * @param zone our zone used for resolution
 * @param label name of the caller
 * @param rd_count number of records we have in @a rd
 * @param rd records we have for the caller's label
 */
static void
handle_caller_name (void *cls,
                    const struct GNUNET_CRYPTO_EccPrivateKey *zone,
                    const char *label,
                    unsigned int rd_count,
                    const struct GNUNET_NAMESTORE_RecordData *rd)
{
  struct GNUNET_CONVERSATION_Phone *phone = cls;
  char *name;

  phone->qe = NULL;
  if (NULL == label)
    name = GNUNET_strdup (GNUNET_NAMESTORE_pkey_to_zkey (&phone->caller_id));
  else
    GNUNET_asprintf (&name, "%.gnu", label);
  phone->event_handler (phone->event_handler_cls,
                        GNUNET_CONVERSATION_EC_RING,
                        name);
  GNUNET_free (name);
}


/**
 * We received a `struct ClientPhoneRingMessage`
 *
 * @param cls the `struct GNUNET_CONVERSATION_Phone`
 * @param msg the message
 */
static void
handle_phone_ring (void *cls,
                   const struct GNUNET_MessageHeader *msg)
{
  struct GNUNET_CONVERSATION_Phone *phone = cls;
  const struct ClientPhoneRingMessage *ring;

  ring = (const struct ClientPhoneRingMessage *) msg;
  switch (phone->state)
  {
  case PS_REGISTER:
    GNUNET_assert (0);
    break;
  case PS_WAITING:
    phone->state = PS_RINGING;
    phone->caller_id = ring->caller_id;
    phone->qe = GNUNET_NAMESTORE_zone_to_name (phone->ns,
                                               &phone->my_zone,
                                               &ring->caller_id,
                                               &handle_caller_name,
                                               phone);
    break;
  case PS_RINGING:
    GNUNET_break (0);
    reconnect_phone (phone);
    break;
  case PS_ACTIVE:
    GNUNET_break (0);
    reconnect_phone (phone);
    break;
  }
}


/**
 * We received a `struct ClientPhoneHangupMessage`.
 *
 * @param cls the `struct GNUNET_CONVERSATION_Phone`
 * @param msg the message
 */
static void
handle_phone_hangup (void *cls,
                     const struct GNUNET_MessageHeader *msg)
{
  struct GNUNET_CONVERSATION_Phone *phone = cls;
  const struct ClientPhoneHangupMessage *hang;
  size_t len;
  const char *reason;

  hang = (const struct ClientPhoneHangupMessage *) msg;
  reason = (const char *) &hang[1];
  len = htons (hang->header.size) - sizeof (struct ClientPhoneHangupMessage);
  if ( (0 == len) ||
       ('\0' != reason[len-1]) )
  {
    GNUNET_break (0);
    reconnect_phone (phone);
    return;
  }
  switch (phone->state)
  {
  case PS_REGISTER:
    GNUNET_assert (0);
    break;
  case PS_WAITING:
    GNUNET_break (0);
    reconnect_phone (phone);
    break;
  case PS_RINGING:
    if (NULL != phone->qe)
    {
      GNUNET_NAMESTORE_cancel (phone->qe);
      phone->qe = NULL;
      phone->state = PS_WAITING;
      break;
    }
    phone->state = PS_WAITING;
    phone->event_handler (phone->event_handler_cls,
                          GNUNET_CONVERSATION_EC_TERMINATED,
                          reason);
    break;
  case PS_ACTIVE:
    GNUNET_break (NULL == phone->qe);
    phone->state = PS_WAITING;
    phone->event_handler (phone->event_handler_cls,
                          GNUNET_CONVERSATION_EC_TERMINATED,
                          reason);
    phone->speaker->disable_speaker (phone->speaker->cls);
    phone->mic->disable_microphone (phone->mic->cls);
    break;
  }
}


/**
 * We received a `struct ClientAudioMessage`
 *
 * @param cls the `struct GNUNET_CONVERSATION_Phone`
 * @param msg the message
 */
static void
handle_phone_audio_message (void *cls,
                            const struct GNUNET_MessageHeader *msg)
{
  struct GNUNET_CONVERSATION_Phone *phone = cls;
  const struct ClientAudioMessage *am;

  am = (const struct ClientAudioMessage *) msg;
  switch (phone->state)
  {
  case PS_REGISTER:
    GNUNET_assert (0);
    break;
  case PS_WAITING:
    GNUNET_break (0);
    reconnect_phone (phone);
    break;
  case PS_RINGING:
    GNUNET_break (0);
    reconnect_phone (phone);
    break;
  case PS_ACTIVE:
    phone->speaker->play (phone->speaker->cls,
                          ntohs (msg->size) - sizeof (struct ClientAudioMessage),
                          &am[1]);
    break;
  }
}


/**
 * We encountered an error talking with the conversation service.
 *
 * @param cls the `struct GNUNET_CONVERSATION_Phone`
 * @param error details about the error
 */
static void
phone_error_handler (void *cls,
                     enum GNUNET_MQ_Error error)
{
  struct GNUNET_CONVERSATION_Phone *phone = cls;

  GNUNET_break (0);
  FPRINTF (stderr,
           _("Internal error %d\n"),
           error);
  reconnect_phone (phone);
}


/**
 * The phone got disconnected, reconnect to the service.
 *
 * @param phone phone to reconnect
 */
static void
reconnect_phone (struct GNUNET_CONVERSATION_Phone *phone)
{
  static struct GNUNET_MQ_MessageHandler handlers[] =
  {
    { &handle_phone_ring,
      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RING,
      sizeof (struct ClientPhoneRingMessage) },
    { &handle_phone_hangup,
      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP,
      0 },
    { &handle_phone_audio_message,
      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO,
      0 },
    { NULL, 0, 0 }
  };
  struct GNUNET_MQ_Envelope *e;
  struct ClientPhoneRegisterMessage *reg;

  if (PS_ACTIVE == phone->state)
  {
    phone->speaker->disable_speaker (phone->speaker->cls);
    phone->mic->disable_microphone (phone->mic->cls);
  }
  if (NULL != phone->mq)
  {
    GNUNET_MQ_destroy (phone->mq);
    phone->mq = NULL;
  }
  if (NULL != phone->client)
  {
    GNUNET_CLIENT_disconnect (phone->client);
    phone->client = NULL;
  }
  phone->state = PS_REGISTER;
  phone->client = GNUNET_CLIENT_connect ("conversation", phone->cfg);
  if (NULL == phone->client)
    return;
  phone->mq = GNUNET_MQ_queue_for_connection_client (phone->client,
                                                     handlers,
                                                     &phone_error_handler,
                                                     phone);
  e = GNUNET_MQ_msg (reg, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_REGISTER);
  reg->line = phone->my_record.line;
  GNUNET_MQ_send (phone->mq, e);
  phone->state = PS_WAITING;
}


/**
 * Create a new phone.
 *
 * @param cfg configuration for the phone; specifies the phone service and
 *        which line the phone is to be connected to
 * @param ego ego to use for name resolution (when determining caller ID)
 * @param event_handler how to notify the owner of the phone about events
 * @param event_handler_cls closure for @a event_handler
 */
struct GNUNET_CONVERSATION_Phone *
GNUNET_CONVERSATION_phone_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
                                  const struct GNUNET_IDENTITY_Ego *ego,
                                  GNUNET_CONVERSATION_EventHandler event_handler,
				  void *event_handler_cls)
{
  struct GNUNET_CONVERSATION_Phone *phone;
  unsigned long long line;

  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_number (cfg,
                                             "CONVERSATION",
                                             "LINE",
                                             &line))
    return NULL;
  phone = GNUNET_new (struct GNUNET_CONVERSATION_Phone);
  if (GNUNET_OK !=
      GNUNET_CRYPTO_get_peer_identity (cfg,
                                       &phone->my_record.peer))
  {
    GNUNET_break (0);
    GNUNET_free (phone);
    return NULL;
  }
  phone->cfg = cfg;
  phone->my_zone = *GNUNET_IDENTITY_ego_get_private_key (ego);
  phone->event_handler = event_handler;
  phone->event_handler_cls = event_handler_cls;
  phone->ns = GNUNET_NAMESTORE_connect (cfg);
  phone->my_record.line = htonl ((uint32_t) line);
  phone->my_record.version = htonl (0);
  reconnect_phone (phone);
  if ( (NULL == phone->client) ||
       (NULL == phone->ns) )
  {
    GNUNET_break (0);
    GNUNET_CONVERSATION_phone_destroy (phone);
    return NULL;
  }
  return phone;
}


/**
 * Fill in a namestore record with the contact information
 * for this phone.  Note that the filled in "data" value
 * is only valid until the phone is destroyed.
 *
 * @param phone phone to create a record for
 * @param rd namestore record to fill in
 */
void
GNUNET_CONVERSATION_phone_get_record (struct GNUNET_CONVERSATION_Phone *phone,
				      struct GNUNET_NAMESTORE_RecordData *rd)
{
  rd->data = &phone->my_record;
  rd->expiration_time = 0;
  rd->data_size = sizeof (struct GNUNET_CONVERSATION_PhoneRecord);
  rd->record_type = GNUNET_NAMESTORE_TYPE_PHONE;
  rd->flags = GNUNET_NAMESTORE_RF_NONE;
}


/**
 * Process recorded audio data.
 *
 * @param cls closure with the `struct GNUNET_CONVERSATION_Phone`
 * @param data_size number of bytes in @a data
 * @param data audio data to play
 */
static void
transmit_phone_audio (void *cls,
                      size_t data_size,
                      const void *data)
{
  struct GNUNET_CONVERSATION_Phone *phone = cls;
  struct GNUNET_MQ_Envelope *e;
  struct ClientAudioMessage *am;

  GNUNET_assert (PS_ACTIVE == phone->state);
  e = GNUNET_MQ_msg_extra (am,
                           data_size,
                           GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO);
  memcpy (&am[1], data, data_size);
  GNUNET_MQ_send (phone->mq, e);
}


/**
 * Picks up a (ringing) phone.  This will connect the speaker
 * to the microphone of the other party, and vice versa.
 *
 * @param phone phone to pick up
 * @param metadata meta data to give to the other user about the pick up event
 * @param speaker speaker to use
 * @param mic microphone to use
 */
void
GNUNET_CONVERSATION_phone_pick_up (struct GNUNET_CONVERSATION_Phone *phone,
                                   const char *metadata,
                                   struct GNUNET_SPEAKER_Handle *speaker,
                                   struct GNUNET_MICROPHONE_Handle *mic)
{
  struct GNUNET_MQ_Envelope *e;
  struct ClientPhonePickupMessage *pick;
  size_t slen;

  GNUNET_assert (PS_RINGING == phone->state);
  phone->speaker = speaker;
  phone->mic = mic;
  slen = strlen (metadata) + 1;
  e = GNUNET_MQ_msg_extra (pick, slen, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_PICK_UP);
  memcpy (&pick[1], metadata, slen);
  GNUNET_MQ_send (phone->mq, e);
  phone->state = PS_ACTIVE;
  phone->speaker->enable_speaker (phone->speaker->cls);
  phone->mic->enable_microphone (phone->mic->cls,
                                 &transmit_phone_audio,
                                 phone);
}


/**
 * Hang up up a (possibly ringing) phone.  This will notify the other
 * party that we are no longer interested in talking with them.
 *
 * @param phone phone to pick up
 * @param reason text we give to the other party about why we terminated the conversation
 */
void
GNUNET_CONVERSATION_phone_hang_up (struct GNUNET_CONVERSATION_Phone *phone,
                                   const char *reason)
{
  struct GNUNET_MQ_Envelope *e;
  struct ClientPhoneHangupMessage *hang;
  size_t slen;

  GNUNET_assert ( (PS_RINGING == phone->state) ||
                  (PS_ACTIVE == phone->state) );
  phone->speaker->disable_speaker (phone->speaker->cls);
  phone->mic->disable_microphone (phone->mic->cls);
  phone->speaker = NULL;
  phone->mic = NULL;
  slen = strlen (reason) + 1;
  e = GNUNET_MQ_msg_extra (hang, slen, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP);
  memcpy (&hang[1], reason, slen);
  GNUNET_MQ_send (phone->mq, e);
  phone->state = PS_WAITING;
}


/**
 * Destroys a phone.
 *
 * @param phone phone to destroy
 */
void
GNUNET_CONVERSATION_phone_destroy (struct GNUNET_CONVERSATION_Phone *phone)
{
  if (NULL != phone->speaker)
  {
    phone->speaker->disable_speaker (phone->speaker->cls);
    phone->speaker = NULL;
  }
  if (NULL != phone->mic)
  {
    phone->mic->disable_microphone (phone->mic->cls);
    phone->mic = NULL;
  }
  if (NULL != phone->qe)
  {
    GNUNET_NAMESTORE_cancel (phone->qe);
    phone->qe = NULL;
  }
  if (NULL != phone->ns)
  {
    GNUNET_NAMESTORE_disconnect (phone->ns);
    phone->ns = NULL;
  }
  if (NULL != phone->mq)
  {
    GNUNET_MQ_destroy (phone->mq);
    phone->mq = NULL;
  }
  if (NULL != phone->client)
  {
    GNUNET_CLIENT_disconnect (phone->client);
    phone->client = NULL;
  }
  GNUNET_free (phone);
}


/* ******************************* Call API *************************** */

/**
 * Possible states of the phone.
 */
enum CallState
{
  /**
   * We still need to lookup the callee.
   */
  CS_LOOKUP = 0,

  /**
   * The call is ringing.
   */
  CS_RINGING,

  /**
   * The call is in an active conversation.
   */
  CS_ACTIVE,

  /**
   * The call is in termination.
   */
  CS_SHUTDOWN
};


/**
 * Handle for an outgoing call.
 */
struct GNUNET_CONVERSATION_Call
{

  /**
   * Our configuration.
   */
  const struct GNUNET_CONFIGURATION_Handle *cfg;

  /**
   * Handle to talk with CONVERSATION service.
   */
  struct GNUNET_CLIENT_Connection *client;

  /**
   * Our caller identity.
   */
  struct GNUNET_IDENTITY_Ego *caller_id;

  /**
   * Target callee as a GNS address/name.
   */
  char *callee;

  /**
   * Our speaker.
   */
  struct GNUNET_SPEAKER_Handle *speaker;

  /**
   * Our microphone.
   */
  struct GNUNET_MICROPHONE_Handle *mic;

  /**
   * Function to call with events.
   */
  GNUNET_CONVERSATION_EventHandler event_handler;

  /**
   * Closure for @e event_handler
   */
  void *event_handler_cls;

  /**
   * Handle for transmitting to the CONVERSATION service.
   */
  struct GNUNET_MQ_Handle *mq;

  /**
   * Connection to GNS (can be NULL).
   */
  struct GNUNET_GNS_Handle *gns;

  /**
   * Active GNS lookup (or NULL).
   */
  struct GNUNET_GNS_LookupRequest *gns_lookup;

  /**
   * Target phone record, only valid after the lookup is done.
   */
  struct GNUNET_CONVERSATION_PhoneRecord phone_record;

  /**
   * State machine for the call.
   */
  enum CallState state;

};


/**
 * The call got disconnected, reconnect to the service.
 *
 * @param call call to reconnect
 */
static void
reconnect_call (struct GNUNET_CONVERSATION_Call *call);


/**
 * We received a `struct ClientPhoneBusyMessage`
 *
 * @param cls the `struct GNUNET_CONVERSATION_Call`
 * @param msg the message
 */
static void
handle_call_busy (void *cls,
                  const struct GNUNET_MessageHeader *msg)
{
  struct GNUNET_CONVERSATION_Call *call = cls;

  switch (call->state)
  {
  case CS_LOOKUP:
    GNUNET_break (0);
    reconnect_call (call);
    break;
  case CS_RINGING:
    call->event_handler (call->event_handler_cls,
                         GNUNET_CONVERSATION_EC_BUSY);
    GNUNET_CONVERSATION_call_stop (call, NULL);
    break;
  case CS_ACTIVE:
    GNUNET_break (0);
    reconnect_call (call);
    break;
  case CS_SHUTDOWN:
    GNUNET_CONVERSATION_call_stop (call, NULL);
    break;
  }
}


/**
 * Process recorded audio data.
 *
 * @param cls closure with the `struct GNUNET_CONVERSATION_Call`
 * @param data_size number of bytes in @a data
 * @param data audio data to play
 */
static void
transmit_call_audio (void *cls,
                     size_t data_size,
                     const void *data)
{
  struct GNUNET_CONVERSATION_Call *call = cls;
  struct GNUNET_MQ_Envelope *e;
  struct ClientAudioMessage *am;

  GNUNET_assert (CS_ACTIVE == call->state);
  e = GNUNET_MQ_msg_extra (am,
                           data_size,
                           GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO);
  memcpy (&am[1], data, data_size);
  GNUNET_MQ_send (call->mq, e);
}


/**
 * We received a `struct ClientPhonePickedupMessage`
 *
 * @param cls the `struct GNUNET_CONVERSATION_Call`
 * @param msg the message
 */
static void
handle_call_picked_up (void *cls,
                       const struct GNUNET_MessageHeader *msg)
{
  struct GNUNET_CONVERSATION_Call *call = cls;
  const struct ClientPhonePickedupMessage *am;
  const char *metadata;
  size_t size;

  am = (const struct ClientPhonePickedupMessage *) msg;
  size = ntohs (am->header.size) - sizeof (struct ClientPhonePickedupMessage);
  metadata = (const char *) &am[1];
  if ( (0 == size) ||
       ('\0' != metadata[size - 1]) )
    metadata = NULL;
  switch (call->state)
  {
  case CS_LOOKUP:
    GNUNET_break (0);
    reconnect_call (call);
    break;
  case CS_RINGING:
    call->state = CS_ACTIVE;
    call->event_handler (call->event_handler_cls,
                         GNUNET_CONVERSATION_EC_READY,
                         metadata);
    call->speaker->enable_speaker (call->speaker->cls);
    call->mic->enable_microphone (call->mic->cls,
                                  &transmit_call_audio,
                                  call);
    break;
  case CS_ACTIVE:
    GNUNET_break (0);
    reconnect_call (call);
    break;
  case CS_SHUTDOWN:
    GNUNET_CONVERSATION_call_stop (call, NULL);
    break;
  }
}


/**
 * We received a `struct ClientPhoneHangupMessage`
 *
 * @param cls the `struct GNUNET_CONVERSATION_Call`
 * @param msg the message
 */
static void
handle_call_hangup (void *cls,
                    const struct GNUNET_MessageHeader *msg)
{
  struct GNUNET_CONVERSATION_Call *call = cls;
  const struct ClientPhoneHangupMessage *am;
  const char *reason;
  size_t size;

  am = (const struct ClientPhoneHangupMessage *) msg;
  size = ntohs (am->header.size) - sizeof (struct ClientPhoneHangupMessage);
  reason = (const char *) &am[1];
  if ( (0 == size) ||
       ('\0' != reason[size - 1]) )
    reason = NULL;
  switch (call->state)
  {
  case CS_LOOKUP:
    GNUNET_break (0);
    reconnect_call (call);
    break;
  case CS_RINGING:
    call->event_handler (call->event_handler_cls,
                         GNUNET_CONVERSATION_EC_TERMINATED,
                         reason);
    GNUNET_CONVERSATION_call_stop (call, NULL);
    return;
  case CS_ACTIVE:
    call->event_handler (call->event_handler_cls,
                         GNUNET_CONVERSATION_EC_TERMINATED,
                         reason);
    GNUNET_CONVERSATION_call_stop (call, NULL);
    return;
  case CS_SHUTDOWN:
    GNUNET_CONVERSATION_call_stop (call, NULL);
    break;
  }
}


/**
 * We received a `struct ClientAudioMessage`
 *
 * @param cls the `struct GNUNET_CONVERSATION_Call`
 * @param msg the message
 */
static void
handle_call_audio_message (void *cls,
                           const struct GNUNET_MessageHeader *msg)
{
  struct GNUNET_CONVERSATION_Call *call = cls;
  const struct ClientAudioMessage *am;

  am = (const struct ClientAudioMessage *) msg;
  switch (call->state)
  {
  case CS_LOOKUP:
    GNUNET_break (0);
    reconnect_call (call);
    break;
  case CS_RINGING:
    GNUNET_break (0);
    reconnect_call (call);
    break;
  case CS_ACTIVE:
    call->speaker->play (call->speaker->cls,
                         ntohs (msg->size) - sizeof (struct ClientAudioMessage),
                         &am[1]);
    break;
  case CS_SHUTDOWN:
    GNUNET_CONVERSATION_call_stop (call, NULL);
    break;

  }
}


/**
 * Iterator called on obtained result for a GNS lookup.
 *
 * @param cls closure with the `struct GNUNET_CONVERSATION_Call`
 * @param rd_count number of records in @a rd
 * @param rd the records in reply
 */
static void
handle_gns_response (void *cls,
                     uint32_t rd_count,
                     const struct GNUNET_NAMESTORE_RecordData *rd)
{
  struct GNUNET_CONVERSATION_Call *call = cls;
  uint32_t i;
  struct GNUNET_MQ_Envelope *e;
  struct ClientCallMessage *ccm;

  call->gns_lookup = NULL;
  for (i=0;i<rd_count;i++)
  {
    if (GNUNET_NAMESTORE_TYPE_PHONE == rd[i].record_type)
    {
      if (rd[i].data_size != sizeof (struct GNUNET_CONVERSATION_PhoneRecord))
      {
        GNUNET_break_op (0);
        continue;
      }
      memcpy (&call->phone_record,
              rd[i].data,
              rd[i].data_size);
      e = GNUNET_MQ_msg (ccm, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_CALL);
      ccm->line = call->phone_record.line;
      ccm->target = call->phone_record.peer;
      ccm->caller_id = *GNUNET_IDENTITY_ego_get_private_key (call->caller_id);
      GNUNET_MQ_send (call->mq, e);
      call->state = CS_RINGING;
      call->event_handler (call->event_handler_cls,
                           GNUNET_CONVERSATION_EC_RINGING);
      return;
    }
  }
  /* not found */
  call->event_handler (call->event_handler_cls,
                       GNUNET_CONVERSATION_EC_GNS_FAIL);
  GNUNET_CONVERSATION_call_stop (call, NULL);
}


/**
 * We encountered an error talking with the conversation service.
 *
 * @param cls the `struct GNUNET_CONVERSATION_Call`
 * @param error details about the error
 */
static void
call_error_handler (void *cls,
                    enum GNUNET_MQ_Error error)
{
  struct GNUNET_CONVERSATION_Call *call = cls;

  GNUNET_break (0);
  FPRINTF (stderr,
           _("Internal error %d\n"),
           error);
  reconnect_call (call);
}


/**
 * The call got disconnected, reconnect to the service.
 *
 * @param call call to reconnect
 */
static void
reconnect_call (struct GNUNET_CONVERSATION_Call *call)
{
  static struct GNUNET_MQ_MessageHandler handlers[] =
  {
    { &handle_call_busy,
      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_BUSY,
      sizeof (struct ClientPhoneBusyMessage) },
    { &handle_call_picked_up,
      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_PICKED_UP,
      0 },
    { &handle_call_hangup,
      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP,
      0 },
    { &handle_call_audio_message,
      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO,
      0 },
    { NULL, 0, 0 }
  };
  struct GNUNET_CRYPTO_EccPublicSignKey my_zone;

  if (CS_ACTIVE == call->state)
  {
    call->speaker->disable_speaker (call->speaker->cls);
    call->mic->disable_microphone (call->mic->cls);
  }
  if (NULL != call->mq)
  {
    GNUNET_MQ_destroy (call->mq);
    call->mq = NULL;
  }
  if (NULL != call->client)
  {
    GNUNET_CLIENT_disconnect (call->client);
    call->client = NULL;
  }
  call->state = CS_SHUTDOWN;
  call->client = GNUNET_CLIENT_connect ("conversation", call->cfg);
  if (NULL == call->client)
    return;
  call->mq = GNUNET_MQ_queue_for_connection_client (call->client,
                                                    handlers,
                                                    &call_error_handler,
                                                    call);
  call->state = CS_LOOKUP;
  GNUNET_IDENTITY_ego_get_public_key (call->caller_id,
                                      &my_zone);
  call->gns_lookup = GNUNET_GNS_lookup (call->gns,
                                        call->callee,
                                        &my_zone,
                                        GNUNET_NAMESTORE_TYPE_PHONE,
                                        GNUNET_NO,
                                        NULL /* FIXME: add shortening support */,
                                        &handle_gns_response, call);
  GNUNET_assert (NULL != call->gns_lookup);
}


/**
 * Call the phone of another user.
 *
 * @param cfg configuration to use, specifies our phone service
 * @param caller_id identity of the caller
 * @param callee GNS name of the callee (used to locate the callee's record)
 * @param speaker speaker to use (will be used automatically immediately once the
 *        #GNUNET_CONVERSATION_EC_READY event is generated); we will NOT generate
 *        a ring tone on the speaker
 * @param mic microphone to use (will be used automatically immediately once the
 *        #GNUNET_CONVERSATION_EC_READY event is generated)
 * @param event_handler how to notify the owner of the phone about events
 * @param event_handler_cls closure for @a event_handler
 */
struct GNUNET_CONVERSATION_Call *
GNUNET_CONVERSATION_call_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
				struct GNUNET_IDENTITY_Ego *caller_id,
				const char *callee,
				struct GNUNET_SPEAKER_Handle *speaker,
				struct GNUNET_MICROPHONE_Handle *mic,
				GNUNET_CONVERSATION_EventHandler event_handler,
				void *event_handler_cls)
{
  struct GNUNET_CONVERSATION_Call *call;

  call = GNUNET_new (struct GNUNET_CONVERSATION_Call);
  call->cfg = cfg;
  call->caller_id = caller_id;
  call->callee = GNUNET_strdup (callee);
  call->speaker = speaker;
  call->mic = mic;
  call->event_handler = event_handler;
  call->event_handler_cls = event_handler_cls;
  call->gns = GNUNET_GNS_connect (cfg);
  reconnect_call (call);

  if ( (NULL == call->client) ||
       (NULL == call->gns) )
  {
    GNUNET_CONVERSATION_call_stop (call, NULL);
    return NULL;
  }
  return call;
}


/**
 * Terminate a call.  The call may be ringing or ready at this time.
 *
 * @param call call to terminate
 * @param reason if the call was active (ringing or ready) this will be the
 *        reason given to the other user for why we hung up
 */
void
GNUNET_CONVERSATION_call_stop (struct GNUNET_CONVERSATION_Call *call,
			       const char *reason)
{
  if (NULL != reason)
  {
    // FIXME: transmit reason to service... (not implemented!)
    GNUNET_break (0);
    // return;
  }
  if (NULL != call->speaker)
  {
    if (CS_ACTIVE == call->state)
      call->speaker->disable_speaker (call->speaker->cls);
    call->speaker = NULL;
  }
  if (NULL != call->mic)
  {
    if (CS_ACTIVE == call->state)
      call->mic->disable_microphone (call->mic->cls);
    call->mic =NULL;
  }
  if (NULL != call->mq)
  {
    GNUNET_MQ_destroy (call->mq);
    call->mq = NULL;
  }
  if (NULL != call->client)
  {
    GNUNET_CLIENT_disconnect (call->client);
    call->client = NULL;
  }
  if (NULL != call->gns_lookup)
  {
    GNUNET_GNS_lookup_cancel (call->gns_lookup);
    call->gns_lookup = NULL;
  }
  if (NULL != call->gns)
  {
    GNUNET_GNS_disconnect (call->gns);
    call->gns = NULL;
  }
  GNUNET_free (call->callee);
  GNUNET_free (call);
}


/* end of conversation_api.c */