aboutsummaryrefslogtreecommitdiff
path: root/src/util/client.c
blob: 9a2f4767887c99d7f2e7e8e251c0b1ecd26af7df (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
/*
     This file is part of GNUnet.
     (C) 2001, 2002, 2006, 2008, 2009 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 2, 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 util/client.c
 * @brief code for access to services
 * @author Christian Grothoff
 *
 * Generic TCP code for reliable, record-oriented TCP
 * connections between clients and service providers.
 */

#include "platform.h"
#include "gnunet_common.h"
#include "gnunet_client_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_server_lib.h"
#include "gnunet_scheduler_lib.h"

#define DEBUG_CLIENT GNUNET_NO

/**
 * Struct to refer to a GNUnet TCP connection.
 * This is more than just a socket because if the server
 * drops the connection, the client automatically tries
 * to reconnect (and for that needs connection information).
 */
struct GNUNET_CLIENT_Connection
{

  /**
   * the socket handle, NULL if not live
   */
  struct GNUNET_CONNECTION_Handle *sock;

  /**
   * Our scheduler.
   */
  struct GNUNET_SCHEDULER_Handle *sched;

  /**
   * Name of the service we interact with.
   */
  char *service_name;

  /**
   * ID of task used for receiving.
   */
  GNUNET_SCHEDULER_TaskIdentifier receiver_task;

  /**
   * Handler for current receiver task.
   */
  GNUNET_CLIENT_MessageHandler receiver_handler;

  /**
   * Closure for receiver_handler.
   */
  void *receiver_handler_cls;

  /**
   * Handler for service test completion (NULL unless in service_test)
   */
  GNUNET_SCHEDULER_Task test_cb;

  /**
   * Closure for test_cb (NULL unless in service_test)
   */
  void *test_cb_cls;

  /**
   * Buffer for received message.
   */
  char *received_buf;

  /**
   * Timeout for receiving a response (absolute time).
   */
  struct GNUNET_TIME_Absolute receive_timeout;

  /**
   * Number of bytes in received_buf that are valid.
   */
  size_t received_pos;

  /**
   * Size of received_buf.
   */
  unsigned int received_size;

  /**
   * Do we have a complete response in received_buf?
   */
  int msg_complete;

};


/**
 * Get a connection with a service.
 *
 * @param sched scheduler to use
 * @param service_name name of the service
 * @param cfg configuration to use
 * @return NULL on error (service unknown to configuration)
 */
struct GNUNET_CLIENT_Connection *
GNUNET_CLIENT_connect (struct GNUNET_SCHEDULER_Handle *sched,
                       const char *service_name,
                       const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  struct GNUNET_CLIENT_Connection *ret;
  struct GNUNET_CONNECTION_Handle *sock;
  char *hostname;
  unsigned long long port;

  if ((GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_number (cfg,
                                              service_name,
                                              "PORT",
                                              &port)) ||
      (port > 65535) ||
      (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_string (cfg,
                                              service_name,
                                              "HOSTNAME", &hostname)))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  "Could not determine valid hostname and port for service `%s' from configuration.\n",
                  service_name);
      return NULL;
    }
  sock = GNUNET_CONNECTION_create_from_connect (sched,
                                                    hostname,
                                                    port,
                                                    GNUNET_SERVER_MAX_MESSAGE_SIZE);
  GNUNET_free (hostname);
  if (sock == NULL)
    return NULL;
  ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
  ret->sock = sock;
  ret->sched = sched;
  ret->service_name = GNUNET_strdup (service_name);
  return ret;
}


/**
 * Receiver task has completed, free rest of client
 * data structures.
 */
static void
finish_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_CLIENT_Connection *sock = cls;

  GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
  GNUNET_free (sock->service_name);
  GNUNET_free (sock);
}


/**
 * Destroy connection with the service.
 */
void
GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock)
{
  GNUNET_assert (sock->sock != NULL);
  GNUNET_CONNECTION_destroy (sock->sock);
  sock->sock = NULL;
  sock->receiver_handler = NULL;
  GNUNET_SCHEDULER_add_after (sock->sched,
                              GNUNET_YES,
                              GNUNET_SCHEDULER_PRIORITY_KEEP,
                              sock->receiver_task, &finish_cleanup, sock);
}


/**
 * check if message is complete
 */
static void
check_complete (struct GNUNET_CLIENT_Connection *conn)
{
  if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
      (conn->received_pos >=
       ntohs (((const struct GNUNET_MessageHeader *) conn->
               received_buf)->size)))
    conn->msg_complete = GNUNET_YES;
}


/**
 * Callback function for data received from the network.  Note that
 * both "available" and "errCode" would be 0 if the read simply timed out.
 *
 * @param cls closure
 * @param buf pointer to received data
 * @param available number of bytes availabe in "buf",
 *        possibly 0 (on errors)
 * @param addr address of the sender
 * @param addrlen size of addr
 * @param errCode value of errno (on errors receiving)
 */
static void
receive_helper (void *cls,
                const void *buf,
                size_t available,
                const struct sockaddr *addr, socklen_t addrlen, int errCode)
{
  struct GNUNET_CLIENT_Connection *conn = cls;
  struct GNUNET_TIME_Relative remaining;

  GNUNET_assert (conn->msg_complete == GNUNET_NO);
  conn->receiver_task = GNUNET_SCHEDULER_NO_TASK;

  if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
    {
      /* signal timeout! */
      if (conn->receiver_handler != NULL)
        {
          conn->receiver_handler (conn->receiver_handler_cls, NULL);
          conn->receiver_handler = NULL;
        }
      return;
    }

  /* FIXME: optimize for common fast case where buf contains the
     entire message and we need no copying... */


  /* slow path: append to array */
  if (conn->received_size < conn->received_pos + available)
    GNUNET_array_grow (conn->received_buf,
                       conn->received_size, conn->received_pos + available);
  memcpy (&conn->received_buf[conn->received_pos], buf, available);
  conn->received_pos += available;
  check_complete (conn);
  /* check for timeout */
  remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
  if (remaining.value == 0)
    {
      /* signal timeout! */
      conn->receiver_handler (conn->receiver_handler_cls, NULL);
      return;
    }
  /* back to receive -- either for more data or to call callback! */
  GNUNET_CLIENT_receive (conn,
                         conn->receiver_handler,
                         conn->receiver_handler_cls, remaining);
}


/**
 * Continuation to call the receive callback.
 */
static void
receive_task (void *scls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_CLIENT_Connection *sock = scls;
  GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
  const struct GNUNET_MessageHeader *cmsg = (const struct GNUNET_MessageHeader *) sock->received_buf;
  void *cls = sock->receiver_handler_cls;
  uint16_t msize = ntohs (cmsg->size);
  char mbuf[msize];
  struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader*) mbuf;

  GNUNET_assert (GNUNET_YES == sock->msg_complete);
  sock->receiver_task = GNUNET_SCHEDULER_NO_TASK;
  GNUNET_assert (sock->received_pos >= msize);
  memcpy (msg, cmsg, msize);
  memmove (sock->received_buf,
	   &sock->received_buf[msize], sock->received_pos - msize);
  sock->received_pos -= msize;
  sock->msg_complete = GNUNET_NO;
  sock->receiver_handler = NULL;  
  check_complete (sock);
  if (handler != NULL)
    handler (cls, msg);
}


/**
 * Read from the service.
 *
 * @param sock the service
 * @param handler function to call with the message
 * @param handler_cls closure for handler
 * @param timeout how long to wait until timing out
 */
void
GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
                       GNUNET_CLIENT_MessageHandler handler,
                       void *handler_cls, struct GNUNET_TIME_Relative timeout)
{
  if (sock->sock == NULL)
    {
      /* already disconnected, fail instantly! */
      GNUNET_break (0);         /* this should not happen in well-written code! */
      handler (handler_cls, NULL);
      return;
    }
  GNUNET_assert (sock->receiver_task ==
                 GNUNET_SCHEDULER_NO_TASK);
  sock->receiver_handler = handler;
  sock->receiver_handler_cls = handler_cls;
  sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
  if (GNUNET_YES == sock->msg_complete)
    sock->receiver_task = GNUNET_SCHEDULER_add_after (sock->sched,
                                                      GNUNET_YES,
                                                      GNUNET_SCHEDULER_PRIORITY_KEEP,
                                                      GNUNET_SCHEDULER_NO_TASK,
                                                      &receive_task, sock);
  else
    sock->receiver_task = GNUNET_CONNECTION_receive (sock->sock,
						     GNUNET_SERVER_MAX_MESSAGE_SIZE,
						     timeout,
						     &receive_helper, sock);
}


static size_t
write_shutdown (void *cls, size_t size, void *buf)
{
  struct GNUNET_MessageHeader *msg;

  if (size < sizeof (struct GNUNET_MessageHeader))
    return 0;                   /* client disconnected */
  msg = (struct GNUNET_MessageHeader *) buf;
  msg->type = htons (GNUNET_MESSAGE_TYPE_SHUTDOWN);
  msg->size = htons (sizeof (struct GNUNET_MessageHeader));
  return sizeof (struct GNUNET_MessageHeader);
}


/**
 * Request that the service should shutdown.
 * Afterwards, the connection should be disconnected.
 *
 * @param sock the socket connected to the service
 */
void
GNUNET_CLIENT_service_shutdown (struct GNUNET_CLIENT_Connection *sock)
{
  GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
                                        sizeof (struct GNUNET_MessageHeader),
                                        GNUNET_TIME_UNIT_FOREVER_REL,
                                        &write_shutdown, NULL);
}


/**
 * Report service unavailable.
 */
static void
service_test_error (struct GNUNET_SCHEDULER_Handle *s,
                    GNUNET_SCHEDULER_Task task, void *task_cls)
{
  GNUNET_SCHEDULER_add_continuation (s,
                                     GNUNET_YES,
                                     task,
                                     task_cls,
                                     GNUNET_SCHEDULER_REASON_TIMEOUT);
}


/**
 * Receive confirmation from test, service is up.
 *
 * @param cls closure
 * @param msg message received, NULL on timeout or fatal error
 */
static void
confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
{
  struct GNUNET_CLIENT_Connection *conn = cls;
  /* We may want to consider looking at the reply in more
     detail in the future, for example, is this the
     correct service? FIXME! */
  if (msg != NULL)
    {
#if DEBUG_CLIENT
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Received confirmation that service is running.\n");
#endif
      GNUNET_SCHEDULER_add_continuation (conn->sched,
                                         GNUNET_YES,
                                         conn->test_cb,
                                         conn->test_cb_cls,
                                         GNUNET_SCHEDULER_REASON_PREREQ_DONE);
    }
  else
    {
      service_test_error (conn->sched, conn->test_cb, conn->test_cb_cls);
    }
  GNUNET_CLIENT_disconnect (conn);
}


static size_t
write_test (void *cls, size_t size, void *buf)
{
  struct GNUNET_MessageHeader *msg;

  if (size < sizeof (struct GNUNET_MessageHeader))
    {
#if DEBUG_CLIENT
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  _("Failure to transmit TEST request.\n"));
#endif
      return 0;                 /* client disconnected */
    }
#if DEBUG_CLIENT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Transmitting TEST request.\n"));
#endif
  msg = (struct GNUNET_MessageHeader *) buf;
  msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
  msg->size = htons (sizeof (struct GNUNET_MessageHeader));
  return sizeof (struct GNUNET_MessageHeader);
}


/**
 * Wait until the service is running.
 *
 * @param sched scheduler to use
 * @param service name of the service to wait for
 * @param cfg configuration to use
 * @param timeout how long to wait at most in ms
 * @param task task to run if service is running
 *        (reason will be "PREREQ_DONE" (service running)
 *         or "TIMEOUT" (service not known to be running))
 * @param task_cls closure for task
 */
void
GNUNET_CLIENT_service_test (struct GNUNET_SCHEDULER_Handle *sched,
                            const char *service,
                            const struct GNUNET_CONFIGURATION_Handle *cfg,
                            struct GNUNET_TIME_Relative timeout,
                            GNUNET_SCHEDULER_Task task, void *task_cls)
{
  struct GNUNET_CLIENT_Connection *conn;

#if DEBUG_CLIENT
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Testing if service `%s' is running.\n", service);
#endif
  conn = GNUNET_CLIENT_connect (sched, service, cfg);
  if (conn == NULL)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  _
                  ("Could not connect to service `%s', must not be running.\n"),
                  service);
      service_test_error (sched, task, task_cls);
      return;
    }
  conn->test_cb = task;
  conn->test_cb_cls = task_cls;
  if (NULL ==
      GNUNET_CONNECTION_notify_transmit_ready (conn->sock,
                                            sizeof (struct
                                                    GNUNET_MessageHeader),
                                            timeout, &write_test, NULL))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  _("Failure to transmit request to service `%s'\n"),
                  service);
      service_test_error (sched, task, task_cls);
      GNUNET_CLIENT_disconnect (conn);
      return;
    }
  GNUNET_CLIENT_receive (conn, &confirm_handler, conn, timeout);
}


/**
 * Ask the client to call us once the specified number of bytes
 * are free in the transmission buffer.  May call the notify
 * method immediately if enough space is available.
 *
 * @param sock connection to the service
 * @param size number of bytes to send
 * @param timeout after how long should we give up (and call
 *        notify with buf NULL and size 0)?
 * @param notify function to call
 * @param notify_cls closure for notify
 * @return NULL if our buffer will never hold size bytes,
 *         a handle if the notify callback was queued (can be used to cancel)
 */
struct GNUNET_CONNECTION_TransmitHandle *
GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
                                     size_t size,
                                     struct GNUNET_TIME_Relative timeout,
                                     GNUNET_CONNECTION_TransmitReadyNotify
                                     notify, void *notify_cls)
{
  return GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
                                               size,
                                               timeout, notify, notify_cls);
}


/**
 * Context for processing 
 * "GNUNET_CLIENT_transmit_and_get_response" requests.
 */
struct TARCtx
{
  /**
   * Client handle.
   */
  struct GNUNET_CLIENT_Connection *sock;

  /**
   * Message to transmit; do not free, allocated
   * right after this struct.
   */
  const struct GNUNET_MessageHeader *hdr;

  /**
   * Timeout to use.
   */
  struct GNUNET_TIME_Absolute timeout;

  /**
   * Function to call when done.
   */
  GNUNET_CLIENT_MessageHandler rn;

  /**
   * Closure for "rn".
   */
  void *rn_cls;
};


/**
 * Function called to notify a client about the socket
 * begin ready to queue the message.  "buf" will be
 * NULL and "size" zero if the socket was closed for
 * writing in the meantime.
 *
 * @param cls closure of type "struct TARCtx*"
 * @param size number of bytes available in buf
 * @param buf where the callee should write the message
 * @return number of bytes written to buf
 */
static size_t
transmit_for_response (void *cls,
		       size_t size, 
		       void *buf)
{
  struct TARCtx *tc = cls;
  uint16_t msize;

  msize = ntohs(tc->hdr->size);
  if (NULL == buf)
    {
      tc->rn (tc->rn_cls, NULL);
      GNUNET_free (tc);
      return 0;
    }
  GNUNET_assert (size >= msize);
  memcpy (buf, tc->hdr, msize);
  GNUNET_CLIENT_receive (tc->sock,
			 tc->rn,
			 tc->rn_cls,
			 GNUNET_TIME_absolute_get_remaining (tc->timeout));
  GNUNET_free (tc);
  return msize;
}


/**
 * Convenience API that combines sending a request
 * to the service and waiting for a response.
 * If either operation times out, the callback
 * will be called with a "NULL" response (in which
 * case the connection should probably be destroyed).
 *
 * @param sock connection to use
 * @param hdr message to transmit
 * @param timeout when to give up (for both transmission
 *         and for waiting for a response)
 * @param rn function to call with the response
 * @param rn_cls closure for rn 
 */
void
GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock,
					 const struct GNUNET_MessageHeader *hdr,
					 struct GNUNET_TIME_Relative timeout,
					 GNUNET_CLIENT_MessageHandler rn,
					 void *rn_cls)
{
  struct TARCtx *tc;
  uint16_t msize;

  msize = ntohs(hdr->size);
  tc = GNUNET_malloc(sizeof (struct TARCtx) + msize);
  tc->sock = sock;
  tc->hdr = (const struct GNUNET_MessageHeader*) &tc[1]; 
  memcpy (&tc[1], hdr, msize);
  tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
  tc->rn = rn;
  tc->rn_cls = rn_cls;
  GNUNET_CLIENT_notify_transmit_ready (sock,
				       msize,
				       timeout,
				       &transmit_for_response,
				       tc);
}



/*  end of client.c */