aboutsummaryrefslogtreecommitdiff
path: root/src/conversation/conversation_api.c
blob: 5bd88c01fbc0073781eb8b6ea440423225ad53c9 (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
/*
     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_api.c
 * @brief API for conversation
 * @author Simon Dieterle
 * @author Andreas Fuchs
 * STRUCTURE:
 * - DATA STRUCTURES
 * - DECLARATIONS
 * - AUXILIARY FUNCTIONS
 * - RECEIVE HANDLERS
 * - SEND FUNCTIONS
 * - API CALL DEFINITIONS
 *
 */

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_dnsparser_lib.h"
#include "gnunet_gns_service.h"
#include "gnunet_protocols.h"
#include "conversation.h"
#include "gnunet_conversation_service.h"

#define MAX_TRANSMIT_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)

/******************************************************************************/
/************************      DATA STRUCTURES     ****************************/
/******************************************************************************/

enum GNUNET_CONVERSATION_CallType
{
  CALLER = 0,
  CALLEE
};

/**
* Information about a call
*/
struct GNUNET_CONVERSATION_CallInformation
{

	/**
	* Peer interacting with
	*/
  struct GNUNET_PeerIdentity peer;

	/**
	* Type of call (incoming or outgoing)
	*/
  int type;

	/**
	* Shows if the call ist fully established
	*/
  int established;
};

/**
 * Opaque handle to the service.
 */
struct GNUNET_CONVERSATION_Handle
{

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

  /**
   * Handle to the server connection, to send messages later
   */
  struct GNUNET_CLIENT_Connection *client;

  /**
   * GNS handle
   */
  struct GNUNET_GNS_Handle *gns;

  /**
   * Namestore handle
   */
  struct GNUNET_NAMESTORE_Handle *namestore;

  /**
   * TXT record for gns
   */
  int txt_record_set;

  /**
   * Callback for incoming calls
   */
  GNUNET_CONVERSATION_CallHandler *call_handler;

  /**
   * Callback for rejected calls
   */
  GNUNET_CONVERSATION_RejectHandler *reject_handler;
  
  /**
   * Callback for notifications
   */
  GNUNET_CONVERSATION_NotificationHandler *notification_handler;

  /**
   * Callback for missed calls
   */
  GNUNET_CONVERSATION_MissedCallHandler *missed_call_handler;

  /**
   * The pointer to the call
   */
  struct GNUNET_CONVERSATION_CallInformation *call;
};

/******************************************************************************/
/***********************     AUXILIARY FUNCTIONS      *************************/
/******************************************************************************/

/**
* Initialize the conversation txt record in GNS
*/
static void
setup_gns_txt (struct GNUNET_CONVERSATION_Handle *handle)
{
  struct GNUNET_CRYPTO_EccPublicSignKey zone_pkey;
  struct GNUNET_CRYPTO_EccPrivateKey *zone_key;
  struct GNUNET_CRYPTO_EccPrivateKey *peer_key;
  struct GNUNET_NAMESTORE_RecordData rd;
  struct GNUNET_PeerIdentity peer;

  char *zone_keyfile;
  char *peer_keyfile;

  rd.expiration_time = UINT64_MAX;

  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_filename (handle->cfg, "gns", "ZONEKEY",
					       &zone_keyfile))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
      return;
    }

  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_filename (handle->cfg, "PEER",
					       "PRIVATE_KEY", &peer_keyfile))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
      return;
    }

  zone_key = GNUNET_CRYPTO_ecc_key_create_from_file (zone_keyfile);
  GNUNET_CRYPTO_ecc_key_get_public_for_signature (zone_key, &zone_pkey);
  peer_key = GNUNET_CRYPTO_ecc_key_create_from_file (peer_keyfile);
  GNUNET_CRYPTO_ecc_key_get_public_for_signature (peer_key,
						  &peer.public_key);
  const char *h = GNUNET_i2s_full (&peer);

  rd.data_size = strlen (h) + 1;
  rd.data = h;
  rd.record_type = GNUNET_DNSPARSER_TYPE_TXT;
  rd.flags = GNUNET_NAMESTORE_RF_NONE;

  /* FIXME: continuation? return value? */
  GNUNET_NAMESTORE_records_store (handle->namestore, 
				  zone_key,
				  "conversation", 
				  1, &rd,
				  NULL, NULL);
}

/**
* Callback for checking the conversation txt gns record
*
* @param cls closure
* @param rd_count
* @param rd
*/
static void
check_gns_cb (void *cls, uint32_t rd_count,
	      const struct GNUNET_NAMESTORE_RecordData *rd)
{
  struct GNUNET_CONVERSATION_Handle *h = (struct GNUNET_CONVERSATION_Handle *) cls;

  if (0 == rd_count)
    {
      setup_gns_txt (h);
    }
  else
    {
      h->txt_record_set = GNUNET_YES;
    }
}


/**
 * Check if the gns txt record for conversation exits
 */
static void
check_gns (struct GNUNET_CONVERSATION_Handle *h)
{
  GNUNET_GNS_lookup (h->gns, "conversation.gns", 
		     NULL /* FIXME_ZONE */,
		     GNUNET_DNSPARSER_TYPE_TXT,
		     GNUNET_NO, 
		     NULL, 
		     &check_gns_cb, h);
}


/******************************************************************************/
/***********************      RECEIVE HANDLERS     ****************************/
/******************************************************************************/

/**
 * Function to process all messages received from the service
 *
 * @param cls closure
 * @param msg message received, NULL on timeout or fatal error
 */
static void
receive_message_cb (void *cls, 
		    const struct GNUNET_MessageHeader *msg)
{
  struct GNUNET_CONVERSATION_Handle *h = cls;
  struct ServerClientSessionInitiateMessage *imsg;
  struct ServerClientSessionRejectMessage *rmsg;
  struct GNUNET_CONVERSATION_MissedCallNotification *missed_calls;

  if (NULL != msg)
    {
      switch (ntohs (msg->type))
	{
	case GNUNET_MESSAGE_TYPE_CONVERSATION_SC_SESSION_ACCEPT:
	  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
		      _("%s has accepted your call.\n"),
		      GNUNET_i2s_full (&(h->call->peer)));

	  h->notification_handler (NULL, h, GNUNET_CONVERSATION_NT_CALL_ACCEPTED,
				   &(h->call->peer));
	  h->call->type = CALLEE;

	  break;

	case GNUNET_MESSAGE_TYPE_CONVERSATION_SC_SESSION_REJECT:
	  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
		      _("%s has rejected your call.\n"),
		      GNUNET_i2s_full (&(h->call->peer)));

	  rmsg = (struct ServerClientSessionRejectMessage *) msg;
	  h->reject_handler (NULL, h, ntohs (rmsg->reason), &(h->call->peer));
	  GNUNET_free (h->call);
	  h->call = NULL;

	  break;

	case GNUNET_MESSAGE_TYPE_CONVERSATION_SC_SESSION_TERMINATE:
	  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
		      _("%s has terminated the call.\n"),
		      GNUNET_i2s_full (&(h->call->peer)));

	  h->notification_handler (NULL, h, GNUNET_CONVERSATION_NT_CALL_TERMINATED,
				   &(h->call->peer));
	  GNUNET_free (h->call);
	  h->call = NULL;

	  break;

	case GNUNET_MESSAGE_TYPE_CONVERSATION_SC_SESSION_INITIATE:
	  imsg = (struct ServerClientSessionInitiateMessage *) msg;

	  GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("%s wants to call you.\n"),
		      GNUNET_i2s_full (&(imsg->peer)));

	  h->call =
	    (struct GNUNET_CONVERSATION_CallInformation *)
	    GNUNET_malloc (sizeof (struct GNUNET_CONVERSATION_CallInformation));
	  memcpy (&(h->call->peer), &(imsg->peer),
		  sizeof (struct GNUNET_PeerIdentity));
	  h->call_handler (NULL, h, &(h->call->peer));
	  h->call->type = CALLEE;

	  break;

	case GNUNET_MESSAGE_TYPE_CONVERSATION_SC_MISSED_CALL:
	  missed_calls =
	    (struct GNUNET_CONVERSATION_MissedCallNotification *) (msg +
							   (sizeof
							    (struct
							     GNUNET_MessageHeader)));
	  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
		      _("You &d have missed a calls.\n"),
		      missed_calls->number);
	  h->missed_call_handler (NULL, h, missed_calls);
	  break;

	case GNUNET_MESSAGE_TYPE_CONVERSATION_SC_SERVICE_BLOCKED:
	  GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("The service is blocked.\n"));
	  h->notification_handler (NULL, h, GNUNET_CONVERSATION_NT_SERVICE_BLOCKED,
				   NULL);
	  break;

	case GNUNET_MESSAGE_TYPE_CONVERSATION_SC_PEER_NOT_CONNECTED:
	  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
		      _("The peer you are calling is not connected.\n"));
	  h->notification_handler (NULL, h, GNUNET_CONVERSATION_NT_NO_PEER, NULL);
	  break;

	case GNUNET_MESSAGE_TYPE_CONVERSATION_SC_NO_ANSWER:
	  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
		      _("The peer you are calling does not answer.\n"));
	  h->notification_handler (NULL, h, GNUNET_CONVERSATION_NT_NO_ANSWER,
				   &(h->call->peer));
	  break;

	case GNUNET_MESSAGE_TYPE_CONVERSATION_SC_ERROR:
	  GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Generic error occured.\n"));
	  break;

	default:
	  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
		      _("Got unknown message type.\n"));
	  break;
	}

    }

  GNUNET_CLIENT_receive (h->client, &receive_message_cb, h,
			 GNUNET_TIME_UNIT_FOREVER_REL);
}

/******************************************************************************/
/************************       SEND FUNCTIONS     ****************************/
/******************************************************************************/

/**
 * Function called to send a session initiate message to the service.
 * "buf" will be NULL and "size" zero if the socket was closed for writing in
 * the meantime.
 *
 * @param cls closure, NULL
 * @param size number of bytes available in buf
 * @param buf where the callee should write the initiate message
 * @return number of bytes written to buf
 */
static size_t
transmit_session_initiate_message (void *cls, size_t size, void *buf)
{
  size_t msg_size;
  struct ClientServerSessionInitiateMessage *msg;
  struct GNUNET_CONVERSATION_Handle *h = (struct GNUNET_CONVERSATION_Handle *) cls;

  msg_size = sizeof (struct ClientServerSessionInitiateMessage);

  GNUNET_assert (size >= msg_size);
  msg = buf;
  msg->header.size = htons (msg_size);
  msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_SESSION_INITIATE);
  memcpy (&msg->peer, &(h->call->peer), sizeof (struct GNUNET_PeerIdentity));

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
	      _
	      ("Sending ClientServerSessionInitiateMessage to the service for peer: %s\n"),
	      GNUNET_i2s_full (&(h->call->peer)));

  h->call->type = CALLER;

  return msg_size;
}

/**
 * Function called to send a session accept message to the service.
 * "buf" will be NULL and "size" zero if the socket was closed for writing in
 * the meantime.
 *
 * @param cls closure, NULL
 * @param size number of bytes available in buf
 * @param buf where the callee should write the accept message
 * @return number of bytes written to buf
 */
static size_t
transmit_session_accept_message (void *cls, size_t size, void *buf)
{
  size_t msg_size;
  struct ClientServerSessionAcceptMessage *msg;
  struct GNUNET_CONVERSATION_Handle *h = (struct GNUNET_CONVERSATION_Handle *) cls;

  msg_size = sizeof (struct ClientServerSessionAcceptMessage);

  GNUNET_assert (size >= msg_size);
  msg = buf;
  msg->header.size = htons (msg_size);
  msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_SESSION_ACCEPT);

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
	      _
	      ("Sending ClienServerSessionAcceptMessage to the service for peer: %s\n"),
	      GNUNET_i2s_full (&(h->call->peer)));

  h->call->established = GNUNET_YES;

  return msg_size;
}

/**
 * Function called to send a session reject message to the service.
 * "buf" will be NULL and "size" zero if the socket was closed for writing in
 * the meantime.
 *
 * @param cls closure, NULL
 * @param size number of bytes available in buf
 * @param buf where the callee should write the reject message
 * @return number of bytes written to buf
 */
static size_t
transmit_session_reject_message (void *cls, size_t size, void *buf)
{
  size_t msg_size;
  struct ClientServerSessionRejectMessage *msg;
  struct GNUNET_CONVERSATION_Handle *h = (struct GNUNET_CONVERSATION_Handle *) cls;

  msg_size = sizeof (struct ClientServerSessionRejectMessage);

  GNUNET_assert (size >= msg_size);
  msg = buf;
  msg->header.size = htons (msg_size);
  msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_SESSION_REJECT);
  msg->reason = htons (GNUNET_CONVERSATION_REJECT_REASON_NOT_WANTED);

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
	      _
	      ("Sending ClientServerSessionRejectMessage to the service for peer: %s\n"),
	      GNUNET_i2s_full (&(h->call->peer)));

  GNUNET_free (h->call);
  h->call = NULL;

  return msg_size;
}

/**
 * Function called to send a session terminate message to the service.
 * "buf" will be NULL and "size" zero if the socket was closed for writing in
 * the meantime.
 *
 * @param cls closure, NULL
 * @param size number of bytes available in buf
 * @param buf where the callee should write the terminate message
 * @return number of bytes written to buf
 */
static size_t
transmit_session_terminate_message (void *cls, size_t size, void *buf)
{
  size_t msg_size;
  struct ClientServerSessionTerminateMessage *msg;
  struct GNUNET_CONVERSATION_Handle *h = (struct GNUNET_CONVERSATION_Handle *) cls;

  msg_size = sizeof (struct ClientServerSessionTerminateMessage);

  GNUNET_assert (size >= msg_size);
  msg = buf;
  msg->header.size = htons (msg_size);
  msg->header.type = htons (GNUNET_MESSAGE_TYPE_CONVERSATION_CS_SESSION_TERMINATE);

  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
	      _
	      ("Sending ClientServerSessionTerminateMessage to the service for peer: %s\n"),
	      GNUNET_i2s_full (&(h->call->peer)));

  GNUNET_free (h->call);
  h->call = NULL;

  return msg_size;
}

/**
 * Auxiliary function to call a peer.
 * 
 * @param h conversation handle
 * @return 
 */
static void
initiate_call (struct GNUNET_CONVERSATION_Handle *h, struct GNUNET_PeerIdentity peer)
{
  h->call =
    (struct GNUNET_CONVERSATION_CallInformation *)
    GNUNET_malloc (sizeof (struct GNUNET_CONVERSATION_CallInformation));
  memcpy (&(h->call->peer), &peer, sizeof (struct GNUNET_PeerIdentity));

  GNUNET_CLIENT_notify_transmit_ready (h->client,
				       sizeof (struct
					       ClientServerSessionInitiateMessage),
				       MAX_TRANSMIT_DELAY, GNUNET_YES,
				       &transmit_session_initiate_message, h);

  return;
}

/**
 * Auxiliary function to accept a call.
 * 
 * @param h conversation handle
 */
static void
accept_call (struct GNUNET_CONVERSATION_Handle *h)
{
  GNUNET_CLIENT_notify_transmit_ready (h->client,
				       sizeof (struct
					       ClientServerSessionAcceptMessage),
				       MAX_TRANSMIT_DELAY, GNUNET_YES,
				       &transmit_session_accept_message, h);
}


/**
 * Auxiliary function to reject a call.
 * 
 * @param h conversation handle
 */
static void
reject_call (struct GNUNET_CONVERSATION_Handle *h)
{
  GNUNET_CLIENT_notify_transmit_ready (h->client,
				       sizeof (struct
					       ClientServerSessionRejectMessage),
				       MAX_TRANSMIT_DELAY, GNUNET_YES,
				       &transmit_session_reject_message, h);
}


/**
 * Auxiliary function to terminate a call.
 * 
 * @param h conversation handle
 */
static void
terminate_call (struct GNUNET_CONVERSATION_Handle *h)
{
  GNUNET_CLIENT_notify_transmit_ready (h->client,
				       sizeof (struct
					       ClientServerSessionTerminateMessage),
				       MAX_TRANSMIT_DELAY, GNUNET_YES,
				       &transmit_session_terminate_message,
				       h);
}


/**
 *
 */
static void
gns_call_cb (void *cls, uint32_t rd_count,
	     const struct GNUNET_NAMESTORE_RecordData *rd)
{
  struct GNUNET_CONVERSATION_Handle *handle = cls;
  struct GNUNET_PeerIdentity peer;
  unsigned int i;

  for (i=0;i<rd_count;i++)
  {
    switch (rd[i].record_type)
    {
    case GNUNET_DNSPARSER_TYPE_TXT: /* FIXME:  use fresh record type for voide... */
      if (GNUNET_OK !=
	  GNUNET_CRYPTO_ecc_public_sign_key_from_string (rd[i].data,
							 rd[i].data_size,
							 &peer.public_key))
      {
	GNUNET_break_op (0);
	continue;
      }      
      initiate_call (handle, peer);
      return;
    default:
      break;
    }
  }
  GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
	      "Lookup failed\n");
  handle->notification_handler (NULL, handle, 
				GNUNET_CONVERSATION_NT_NO_PEER,
				NULL);
}


/**
* GNS lookup
*/
static void
gns_lookup_and_call (struct GNUNET_CONVERSATION_Handle *h, const char *callee)
{
  char domain[GNUNET_DNSPARSER_MAX_NAME_LENGTH];
  char *pos;

  pos = domain;
  strcpy (pos, "conversation");
  pos += strlen ("conversation");
  strcpy (pos, ".");
  pos++;
  strcpy (pos, callee);
  pos += strlen (callee);

  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Lookup for %s\n", domain);

  GNUNET_GNS_lookup (h->gns,
		     domain,
		     NULL /* FIXME: ZONE! */,
		     GNUNET_DNSPARSER_TYPE_TXT,
		     GNUNET_NO, 
		     NULL,
		     &gns_call_cb, h);
}


/******************************************************************************/
/**********************      API CALL DEFINITIONS     *************************/
/******************************************************************************/

struct GNUNET_CONVERSATION_Handle *
GNUNET_CONVERSATION_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, 
			     void *cls,
			     GNUNET_CONVERSATION_CallHandler call_handler,
			     GNUNET_CONVERSATION_RejectHandler reject_handler,
			     GNUNET_CONVERSATION_NotificationHandler notification_handler,
			     GNUNET_CONVERSATION_MissedCallHandler missed_call_handler)
{
  struct GNUNET_CONVERSATION_Handle *h;

  GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
	      "GNUNET_CONVERSATION_connect()\n");
  h = GNUNET_malloc (sizeof (struct GNUNET_CONVERSATION_Handle));

  h->cfg = cfg;
  h->call_handler = call_handler;
  h->reject_handler = reject_handler;
  h->notification_handler = notification_handler;
  h->missed_call_handler = missed_call_handler;

  if (NULL == (h->client = GNUNET_CLIENT_connect ("conversation", cfg)))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not access CONVERSATION service\n");
      GNUNET_break (0);
      GNUNET_free (h);

      return NULL;
    }

  if (NULL == (h->gns = GNUNET_GNS_connect (cfg)))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not access GNS service\n");
      GNUNET_break (0);
      GNUNET_CLIENT_disconnect (h->client);
      GNUNET_free (h);

      return NULL;
    }

  if (NULL == (h->namestore = GNUNET_NAMESTORE_connect (cfg)))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		  "Could not access NAMESTORE service\n");
      GNUNET_break (0);
      GNUNET_CLIENT_disconnect (h->client);
      GNUNET_GNS_disconnect (h->gns);
      GNUNET_free (h);

      return NULL;
    }

  check_gns (h);
  GNUNET_CLIENT_receive (h->client, &receive_message_cb, h,
			 GNUNET_TIME_UNIT_FOREVER_REL);

  return h;
}


void
GNUNET_CONVERSATION_disconnect (struct GNUNET_CONVERSATION_Handle *handle)
{
  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "CONVERSATION DISCONNECT\n");

  GNUNET_CLIENT_disconnect (handle->client);
  GNUNET_GNS_disconnect (handle->gns);

  GNUNET_free (handle);
  handle = NULL;
}


void
GNUNET_CONVERSATION_call (struct GNUNET_CONVERSATION_Handle *h, 
			  const char *callee,
			  int doGnsLookup)
{
  struct GNUNET_PeerIdentity peer;

  if (NULL == h || NULL == h->client)
    return;

  if (GNUNET_YES == doGnsLookup)
  {
    gns_lookup_and_call (h, callee);
    return;
  }
  if (GNUNET_OK !=
      GNUNET_CRYPTO_ecc_public_sign_key_from_string (callee, 
						     strlen (callee),
						     &peer.public_key))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
		_("`%s'  is not a valid public key\n"),
		callee);
    h->notification_handler (NULL, h, GNUNET_CONVERSATION_NT_NO_PEER, NULL);
    return;
  }  
  initiate_call (h, peer);
}


void
GNUNET_CONVERSATION_hangup (struct GNUNET_CONVERSATION_Handle *h)
{
  if (NULL == h || NULL == h->client)
    return;

  terminate_call (h);
}


void
GNUNET_CONVERSATION_accept (struct GNUNET_CONVERSATION_Handle *h)
{
  if (NULL == h || NULL == h->client)
    return;

  accept_call (h);
}


void
GNUNET_CONVERSATION_reject (struct GNUNET_CONVERSATION_Handle *h)
{
  if (NULL == h || NULL == h->client)
    return;

  reject_call (h);
}

/* end of conversation_api.c */