2 This file is part of GNUnet.
3 (C) 2001, 2002, 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
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 2, or (at your
8 option) any later version.
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.
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.
23 * @brief code for access to services
24 * @author Christian Grothoff
26 * Generic TCP code for reliable, record-oriented TCP
27 * connections between clients and service providers.
31 #include "gnunet_common.h"
32 #include "gnunet_client_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_server_lib.h"
35 #include "gnunet_scheduler_lib.h"
37 #define DEBUG_CLIENT GNUNET_NO
40 * Struct to refer to a GNUnet TCP connection.
41 * This is more than just a socket because if the server
42 * drops the connection, the client automatically tries
43 * to reconnect (and for that needs connection information).
45 struct GNUNET_CLIENT_Connection
49 * the socket handle, NULL if not live
51 struct GNUNET_CONNECTION_Handle *sock;
56 struct GNUNET_SCHEDULER_Handle *sched;
59 * Name of the service we interact with.
64 * Handler for current receiver task.
66 GNUNET_CLIENT_MessageHandler receiver_handler;
69 * Closure for receiver_handler.
71 void *receiver_handler_cls;
74 * Handler for service test completion (NULL unless in service_test)
76 GNUNET_SCHEDULER_Task test_cb;
79 * Closure for test_cb (NULL unless in service_test)
84 * Buffer for received message.
89 * Timeout for receiving a response (absolute time).
91 struct GNUNET_TIME_Absolute receive_timeout;
94 * Number of bytes in received_buf that are valid.
99 * Size of received_buf.
101 unsigned int received_size;
104 * Do we have a complete response in received_buf?
109 * Are we currently busy doing receive-processing?
110 * GNUNET_YES if so, GNUNET_NO if not, GNUNET_SYSERR
111 * if the handle should be destroyed as soon as the
112 * receive processing is done.
120 * Get a connection with a service.
122 * @param sched scheduler to use
123 * @param service_name name of the service
124 * @param cfg configuration to use
125 * @return NULL on error (service unknown to configuration)
127 struct GNUNET_CLIENT_Connection *
128 GNUNET_CLIENT_connect (struct GNUNET_SCHEDULER_Handle *sched,
129 const char *service_name,
130 const struct GNUNET_CONFIGURATION_Handle *cfg)
132 struct GNUNET_CLIENT_Connection *ret;
133 struct GNUNET_CONNECTION_Handle *sock;
135 unsigned long long port;
138 GNUNET_CONFIGURATION_get_value_number (cfg,
144 GNUNET_CONFIGURATION_get_value_string (cfg,
146 "HOSTNAME", &hostname)))
148 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
149 _("Could not determine valid hostname and port for service `%s' from configuration.\n"),
153 if (0 == strlen (hostname))
155 GNUNET_free (hostname);
156 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
157 _("Need a non-empty hostname for service `%s'.\n"),
161 sock = GNUNET_CONNECTION_create_from_connect (sched,
165 GNUNET_SERVER_MAX_MESSAGE_SIZE);
166 GNUNET_free (hostname);
169 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
172 ret->service_name = GNUNET_strdup (service_name);
178 * Receiver task has completed, free rest of client
182 finish_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
184 struct GNUNET_CLIENT_Connection *sock = cls;
186 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
187 GNUNET_free (sock->service_name);
193 * Destroy connection with the service.
196 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock)
198 GNUNET_assert (sock->sock != NULL);
199 GNUNET_CONNECTION_destroy (sock->sock);
201 sock->receiver_handler = NULL;
202 if (sock->in_receive == GNUNET_YES)
203 sock->in_receive = GNUNET_SYSERR;
205 GNUNET_SCHEDULER_add_after (sock->sched,
207 GNUNET_SCHEDULER_PRIORITY_KEEP,
208 GNUNET_SCHEDULER_NO_TASK,
209 &finish_cleanup, sock);
214 * Check if message is complete
217 check_complete (struct GNUNET_CLIENT_Connection *conn)
219 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
220 (conn->received_pos >=
221 ntohs (((const struct GNUNET_MessageHeader *) conn->
222 received_buf)->size)))
223 conn->msg_complete = GNUNET_YES;
228 * Callback function for data received from the network. Note that
229 * both "available" and "errCode" would be 0 if the read simply timed out.
232 * @param buf pointer to received data
233 * @param available number of bytes availabe in "buf",
234 * possibly 0 (on errors)
235 * @param addr address of the sender
236 * @param addrlen size of addr
237 * @param errCode value of errno (on errors receiving)
240 receive_helper (void *cls,
243 const struct sockaddr *addr, socklen_t addrlen, int errCode)
245 struct GNUNET_CLIENT_Connection *conn = cls;
246 struct GNUNET_TIME_Relative remaining;
248 GNUNET_assert (conn->msg_complete == GNUNET_NO);
249 if (GNUNET_SYSERR == conn->in_receive)
250 GNUNET_SCHEDULER_add_after (conn->sched,
252 GNUNET_SCHEDULER_PRIORITY_KEEP,
253 GNUNET_SCHEDULER_NO_TASK,
256 conn->in_receive = GNUNET_NO;
257 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
259 /* signal timeout! */
260 if (conn->receiver_handler != NULL)
262 conn->receiver_handler (conn->receiver_handler_cls, NULL);
263 conn->receiver_handler = NULL;
268 /* FIXME: optimize for common fast case where buf contains the
269 entire message and we need no copying... */
272 /* slow path: append to array */
273 if (conn->received_size < conn->received_pos + available)
274 GNUNET_array_grow (conn->received_buf,
275 conn->received_size, conn->received_pos + available);
276 memcpy (&conn->received_buf[conn->received_pos], buf, available);
277 conn->received_pos += available;
278 check_complete (conn);
279 /* check for timeout */
280 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
281 if (remaining.value == 0)
283 /* signal timeout! */
284 conn->receiver_handler (conn->receiver_handler_cls, NULL);
287 /* back to receive -- either for more data or to call callback! */
288 GNUNET_CLIENT_receive (conn,
289 conn->receiver_handler,
290 conn->receiver_handler_cls, remaining);
295 * Continuation to call the receive callback.
298 receive_task (void *scls, const struct GNUNET_SCHEDULER_TaskContext *tc)
300 struct GNUNET_CLIENT_Connection *sock = scls;
301 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
302 const struct GNUNET_MessageHeader *cmsg = (const struct GNUNET_MessageHeader *) sock->received_buf;
303 void *cls = sock->receiver_handler_cls;
304 uint16_t msize = ntohs (cmsg->size);
306 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader*) mbuf;
308 if (GNUNET_SYSERR == sock->in_receive)
309 GNUNET_SCHEDULER_add_after (sock->sched,
311 GNUNET_SCHEDULER_PRIORITY_KEEP,
312 GNUNET_SCHEDULER_NO_TASK,
315 sock->in_receive = GNUNET_NO;
316 GNUNET_assert (GNUNET_YES == sock->msg_complete);
317 GNUNET_assert (sock->received_pos >= msize);
318 memcpy (msg, cmsg, msize);
319 memmove (sock->received_buf,
320 &sock->received_buf[msize], sock->received_pos - msize);
321 sock->received_pos -= msize;
322 sock->msg_complete = GNUNET_NO;
323 sock->receiver_handler = NULL;
324 check_complete (sock);
331 * Read from the service.
333 * @param sock the service
334 * @param handler function to call with the message
335 * @param handler_cls closure for handler
336 * @param timeout how long to wait until timing out
339 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
340 GNUNET_CLIENT_MessageHandler handler,
341 void *handler_cls, struct GNUNET_TIME_Relative timeout)
343 if (sock->sock == NULL)
345 /* already disconnected, fail instantly! */
346 GNUNET_break (0); /* this should not happen in well-written code! */
347 handler (handler_cls, NULL);
350 sock->receiver_handler = handler;
351 sock->receiver_handler_cls = handler_cls;
352 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
353 sock->in_receive = GNUNET_YES;
354 if (GNUNET_YES == sock->msg_complete)
355 GNUNET_SCHEDULER_add_after (sock->sched,
357 GNUNET_SCHEDULER_PRIORITY_KEEP,
358 GNUNET_SCHEDULER_NO_TASK,
359 &receive_task, sock);
361 GNUNET_CONNECTION_receive (sock->sock,
362 GNUNET_SERVER_MAX_MESSAGE_SIZE,
364 &receive_helper, sock);
369 write_shutdown (void *cls, size_t size, void *buf)
371 struct GNUNET_MessageHeader *msg;
373 if (size < sizeof (struct GNUNET_MessageHeader))
374 return 0; /* client disconnected */
375 msg = (struct GNUNET_MessageHeader *) buf;
376 msg->type = htons (GNUNET_MESSAGE_TYPE_SHUTDOWN);
377 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
378 return sizeof (struct GNUNET_MessageHeader);
383 * Request that the service should shutdown.
384 * Afterwards, the connection should be disconnected.
386 * @param sock the socket connected to the service
389 GNUNET_CLIENT_service_shutdown (struct GNUNET_CLIENT_Connection *sock)
391 GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
392 sizeof (struct GNUNET_MessageHeader),
393 GNUNET_TIME_UNIT_FOREVER_REL,
394 &write_shutdown, NULL);
399 * Report service unavailable.
402 service_test_error (struct GNUNET_SCHEDULER_Handle *s,
403 GNUNET_SCHEDULER_Task task, void *task_cls)
405 GNUNET_SCHEDULER_add_continuation (s,
409 GNUNET_SCHEDULER_REASON_TIMEOUT);
414 * Receive confirmation from test, service is up.
417 * @param msg message received, NULL on timeout or fatal error
420 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
422 struct GNUNET_CLIENT_Connection *conn = cls;
423 /* We may want to consider looking at the reply in more
424 detail in the future, for example, is this the
425 correct service? FIXME! */
429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
430 "Received confirmation that service is running.\n");
432 GNUNET_SCHEDULER_add_continuation (conn->sched,
436 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
440 service_test_error (conn->sched, conn->test_cb, conn->test_cb_cls);
442 GNUNET_CLIENT_disconnect (conn);
447 write_test (void *cls, size_t size, void *buf)
449 struct GNUNET_MessageHeader *msg;
451 if (size < sizeof (struct GNUNET_MessageHeader))
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455 _("Failure to transmit TEST request.\n"));
457 return 0; /* client disconnected */
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Transmitting TEST request.\n"));
462 msg = (struct GNUNET_MessageHeader *) buf;
463 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
464 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
465 return sizeof (struct GNUNET_MessageHeader);
470 * Wait until the service is running.
472 * @param sched scheduler to use
473 * @param service name of the service to wait for
474 * @param cfg configuration to use
475 * @param timeout how long to wait at most in ms
476 * @param task task to run if service is running
477 * (reason will be "PREREQ_DONE" (service running)
478 * or "TIMEOUT" (service not known to be running))
479 * @param task_cls closure for task
482 GNUNET_CLIENT_service_test (struct GNUNET_SCHEDULER_Handle *sched,
484 const struct GNUNET_CONFIGURATION_Handle *cfg,
485 struct GNUNET_TIME_Relative timeout,
486 GNUNET_SCHEDULER_Task task, void *task_cls)
488 struct GNUNET_CLIENT_Connection *conn;
491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
492 "Testing if service `%s' is running.\n", service);
494 conn = GNUNET_CLIENT_connect (sched, service, cfg);
497 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
499 ("Could not connect to service `%s', must not be running.\n"),
501 service_test_error (sched, task, task_cls);
504 conn->test_cb = task;
505 conn->test_cb_cls = task_cls;
507 GNUNET_CONNECTION_notify_transmit_ready (conn->sock,
509 GNUNET_MessageHeader),
510 timeout, &write_test, NULL))
512 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
513 _("Failure to transmit request to service `%s'\n"),
515 service_test_error (sched, task, task_cls);
516 GNUNET_CLIENT_disconnect (conn);
519 GNUNET_CLIENT_receive (conn, &confirm_handler, conn, timeout);
524 * Ask the client to call us once the specified number of bytes
525 * are free in the transmission buffer. May call the notify
526 * method immediately if enough space is available.
528 * @param sock connection to the service
529 * @param size number of bytes to send
530 * @param timeout after how long should we give up (and call
531 * notify with buf NULL and size 0)?
532 * @param notify function to call
533 * @param notify_cls closure for notify
534 * @return NULL if our buffer will never hold size bytes,
535 * a handle if the notify callback was queued (can be used to cancel)
537 struct GNUNET_CONNECTION_TransmitHandle *
538 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
540 struct GNUNET_TIME_Relative timeout,
541 GNUNET_CONNECTION_TransmitReadyNotify
542 notify, void *notify_cls)
544 return GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
546 timeout, notify, notify_cls);
551 * Context for processing
552 * "GNUNET_CLIENT_transmit_and_get_response" requests.
559 struct GNUNET_CLIENT_Connection *sock;
562 * Message to transmit; do not free, allocated
563 * right after this struct.
565 const struct GNUNET_MessageHeader *hdr;
570 struct GNUNET_TIME_Absolute timeout;
573 * Function to call when done.
575 GNUNET_CLIENT_MessageHandler rn;
585 * Function called to notify a client about the socket
586 * begin ready to queue the message. "buf" will be
587 * NULL and "size" zero if the socket was closed for
588 * writing in the meantime.
590 * @param cls closure of type "struct TARCtx*"
591 * @param size number of bytes available in buf
592 * @param buf where the callee should write the message
593 * @return number of bytes written to buf
596 transmit_for_response (void *cls,
600 struct TARCtx *tc = cls;
603 msize = ntohs(tc->hdr->size);
606 tc->rn (tc->rn_cls, NULL);
610 GNUNET_assert (size >= msize);
611 memcpy (buf, tc->hdr, msize);
612 GNUNET_CLIENT_receive (tc->sock,
615 GNUNET_TIME_absolute_get_remaining (tc->timeout));
622 * Convenience API that combines sending a request
623 * to the service and waiting for a response.
624 * If either operation times out, the callback
625 * will be called with a "NULL" response (in which
626 * case the connection should probably be destroyed).
628 * @param sock connection to use
629 * @param hdr message to transmit
630 * @param timeout when to give up (for both transmission
631 * and for waiting for a response)
632 * @param rn function to call with the response
633 * @param rn_cls closure for rn
636 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock,
637 const struct GNUNET_MessageHeader *hdr,
638 struct GNUNET_TIME_Relative timeout,
639 GNUNET_CLIENT_MessageHandler rn,
645 msize = ntohs(hdr->size);
646 tc = GNUNET_malloc(sizeof (struct TARCtx) + msize);
648 tc->hdr = (const struct GNUNET_MessageHeader*) &tc[1];
649 memcpy (&tc[1], hdr, msize);
650 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
653 GNUNET_CLIENT_notify_transmit_ready (sock,
656 &transmit_for_response,
662 /* end of client.c */