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 * ID of task used for receiving.
66 GNUNET_SCHEDULER_TaskIdentifier receiver_task;
69 * Handler for current receiver task.
71 GNUNET_CLIENT_MessageHandler receiver_handler;
74 * Closure for receiver_handler.
76 void *receiver_handler_cls;
79 * Handler for service test completion (NULL unless in service_test)
81 GNUNET_SCHEDULER_Task test_cb;
84 * Closure for test_cb (NULL unless in service_test)
89 * Buffer for received message.
94 * Timeout for receiving a response (absolute time).
96 struct GNUNET_TIME_Absolute receive_timeout;
99 * Number of bytes in received_buf that are valid.
104 * Size of received_buf.
106 unsigned int received_size;
109 * Do we have a complete response in received_buf?
117 * Get a connection with a service.
119 * @param sched scheduler to use
120 * @param service_name name of the service
121 * @param cfg configuration to use
122 * @return NULL on error (service unknown to configuration)
124 struct GNUNET_CLIENT_Connection *
125 GNUNET_CLIENT_connect (struct GNUNET_SCHEDULER_Handle *sched,
126 const char *service_name,
127 const struct GNUNET_CONFIGURATION_Handle *cfg)
129 struct GNUNET_CLIENT_Connection *ret;
130 struct GNUNET_CONNECTION_Handle *sock;
132 unsigned long long port;
135 GNUNET_CONFIGURATION_get_value_number (cfg,
141 GNUNET_CONFIGURATION_get_value_string (cfg,
143 "HOSTNAME", &hostname)))
145 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
146 _("Could not determine valid hostname and port for service `%s' from configuration.\n"),
150 if (0 == strlen (hostname))
152 GNUNET_free (hostname);
153 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
154 _("Need a non-empty hostname for service `%s'.\n"),
158 sock = GNUNET_CONNECTION_create_from_connect (sched,
161 GNUNET_SERVER_MAX_MESSAGE_SIZE);
162 GNUNET_free (hostname);
165 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
168 ret->service_name = GNUNET_strdup (service_name);
174 * Receiver task has completed, free rest of client
178 finish_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
180 struct GNUNET_CLIENT_Connection *sock = cls;
182 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
183 GNUNET_free (sock->service_name);
189 * Destroy connection with the service.
192 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock)
194 GNUNET_assert (sock->sock != NULL);
195 GNUNET_CONNECTION_destroy (sock->sock);
197 sock->receiver_handler = NULL;
198 GNUNET_SCHEDULER_add_after (sock->sched,
200 GNUNET_SCHEDULER_PRIORITY_KEEP,
201 sock->receiver_task, &finish_cleanup, sock);
206 * check if message is complete
209 check_complete (struct GNUNET_CLIENT_Connection *conn)
211 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
212 (conn->received_pos >=
213 ntohs (((const struct GNUNET_MessageHeader *) conn->
214 received_buf)->size)))
215 conn->msg_complete = GNUNET_YES;
220 * Callback function for data received from the network. Note that
221 * both "available" and "errCode" would be 0 if the read simply timed out.
224 * @param buf pointer to received data
225 * @param available number of bytes availabe in "buf",
226 * possibly 0 (on errors)
227 * @param addr address of the sender
228 * @param addrlen size of addr
229 * @param errCode value of errno (on errors receiving)
232 receive_helper (void *cls,
235 const struct sockaddr *addr, socklen_t addrlen, int errCode)
237 struct GNUNET_CLIENT_Connection *conn = cls;
238 struct GNUNET_TIME_Relative remaining;
240 GNUNET_assert (conn->msg_complete == GNUNET_NO);
241 conn->receiver_task = GNUNET_SCHEDULER_NO_TASK;
243 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
245 /* signal timeout! */
246 if (conn->receiver_handler != NULL)
248 conn->receiver_handler (conn->receiver_handler_cls, NULL);
249 conn->receiver_handler = NULL;
254 /* FIXME: optimize for common fast case where buf contains the
255 entire message and we need no copying... */
258 /* slow path: append to array */
259 if (conn->received_size < conn->received_pos + available)
260 GNUNET_array_grow (conn->received_buf,
261 conn->received_size, conn->received_pos + available);
262 memcpy (&conn->received_buf[conn->received_pos], buf, available);
263 conn->received_pos += available;
264 check_complete (conn);
265 /* check for timeout */
266 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
267 if (remaining.value == 0)
269 /* signal timeout! */
270 conn->receiver_handler (conn->receiver_handler_cls, NULL);
273 /* back to receive -- either for more data or to call callback! */
274 GNUNET_CLIENT_receive (conn,
275 conn->receiver_handler,
276 conn->receiver_handler_cls, remaining);
281 * Continuation to call the receive callback.
284 receive_task (void *scls, const struct GNUNET_SCHEDULER_TaskContext *tc)
286 struct GNUNET_CLIENT_Connection *sock = scls;
287 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
288 const struct GNUNET_MessageHeader *cmsg = (const struct GNUNET_MessageHeader *) sock->received_buf;
289 void *cls = sock->receiver_handler_cls;
290 uint16_t msize = ntohs (cmsg->size);
292 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader*) mbuf;
294 GNUNET_assert (GNUNET_YES == sock->msg_complete);
295 sock->receiver_task = GNUNET_SCHEDULER_NO_TASK;
296 GNUNET_assert (sock->received_pos >= msize);
297 memcpy (msg, cmsg, msize);
298 memmove (sock->received_buf,
299 &sock->received_buf[msize], sock->received_pos - msize);
300 sock->received_pos -= msize;
301 sock->msg_complete = GNUNET_NO;
302 sock->receiver_handler = NULL;
303 check_complete (sock);
310 * Read from the service.
312 * @param sock the service
313 * @param handler function to call with the message
314 * @param handler_cls closure for handler
315 * @param timeout how long to wait until timing out
318 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
319 GNUNET_CLIENT_MessageHandler handler,
320 void *handler_cls, struct GNUNET_TIME_Relative timeout)
322 if (sock->sock == NULL)
324 /* already disconnected, fail instantly! */
325 GNUNET_break (0); /* this should not happen in well-written code! */
326 handler (handler_cls, NULL);
329 GNUNET_assert (sock->receiver_task ==
330 GNUNET_SCHEDULER_NO_TASK);
331 sock->receiver_handler = handler;
332 sock->receiver_handler_cls = handler_cls;
333 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
334 if (GNUNET_YES == sock->msg_complete)
335 sock->receiver_task = GNUNET_SCHEDULER_add_after (sock->sched,
337 GNUNET_SCHEDULER_PRIORITY_KEEP,
338 GNUNET_SCHEDULER_NO_TASK,
339 &receive_task, sock);
341 sock->receiver_task = GNUNET_CONNECTION_receive (sock->sock,
342 GNUNET_SERVER_MAX_MESSAGE_SIZE,
344 &receive_helper, sock);
349 write_shutdown (void *cls, size_t size, void *buf)
351 struct GNUNET_MessageHeader *msg;
353 if (size < sizeof (struct GNUNET_MessageHeader))
354 return 0; /* client disconnected */
355 msg = (struct GNUNET_MessageHeader *) buf;
356 msg->type = htons (GNUNET_MESSAGE_TYPE_SHUTDOWN);
357 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
358 return sizeof (struct GNUNET_MessageHeader);
363 * Request that the service should shutdown.
364 * Afterwards, the connection should be disconnected.
366 * @param sock the socket connected to the service
369 GNUNET_CLIENT_service_shutdown (struct GNUNET_CLIENT_Connection *sock)
371 GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
372 sizeof (struct GNUNET_MessageHeader),
373 GNUNET_TIME_UNIT_FOREVER_REL,
374 &write_shutdown, NULL);
379 * Report service unavailable.
382 service_test_error (struct GNUNET_SCHEDULER_Handle *s,
383 GNUNET_SCHEDULER_Task task, void *task_cls)
385 GNUNET_SCHEDULER_add_continuation (s,
389 GNUNET_SCHEDULER_REASON_TIMEOUT);
394 * Receive confirmation from test, service is up.
397 * @param msg message received, NULL on timeout or fatal error
400 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
402 struct GNUNET_CLIENT_Connection *conn = cls;
403 /* We may want to consider looking at the reply in more
404 detail in the future, for example, is this the
405 correct service? FIXME! */
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
410 "Received confirmation that service is running.\n");
412 GNUNET_SCHEDULER_add_continuation (conn->sched,
416 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
420 service_test_error (conn->sched, conn->test_cb, conn->test_cb_cls);
422 GNUNET_CLIENT_disconnect (conn);
427 write_test (void *cls, size_t size, void *buf)
429 struct GNUNET_MessageHeader *msg;
431 if (size < sizeof (struct GNUNET_MessageHeader))
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435 _("Failure to transmit TEST request.\n"));
437 return 0; /* client disconnected */
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Transmitting TEST request.\n"));
442 msg = (struct GNUNET_MessageHeader *) buf;
443 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
444 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
445 return sizeof (struct GNUNET_MessageHeader);
450 * Wait until the service is running.
452 * @param sched scheduler to use
453 * @param service name of the service to wait for
454 * @param cfg configuration to use
455 * @param timeout how long to wait at most in ms
456 * @param task task to run if service is running
457 * (reason will be "PREREQ_DONE" (service running)
458 * or "TIMEOUT" (service not known to be running))
459 * @param task_cls closure for task
462 GNUNET_CLIENT_service_test (struct GNUNET_SCHEDULER_Handle *sched,
464 const struct GNUNET_CONFIGURATION_Handle *cfg,
465 struct GNUNET_TIME_Relative timeout,
466 GNUNET_SCHEDULER_Task task, void *task_cls)
468 struct GNUNET_CLIENT_Connection *conn;
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472 "Testing if service `%s' is running.\n", service);
474 conn = GNUNET_CLIENT_connect (sched, service, cfg);
477 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
479 ("Could not connect to service `%s', must not be running.\n"),
481 service_test_error (sched, task, task_cls);
484 conn->test_cb = task;
485 conn->test_cb_cls = task_cls;
487 GNUNET_CONNECTION_notify_transmit_ready (conn->sock,
489 GNUNET_MessageHeader),
490 timeout, &write_test, NULL))
492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
493 _("Failure to transmit request to service `%s'\n"),
495 service_test_error (sched, task, task_cls);
496 GNUNET_CLIENT_disconnect (conn);
499 GNUNET_CLIENT_receive (conn, &confirm_handler, conn, timeout);
504 * Ask the client to call us once the specified number of bytes
505 * are free in the transmission buffer. May call the notify
506 * method immediately if enough space is available.
508 * @param sock connection to the service
509 * @param size number of bytes to send
510 * @param timeout after how long should we give up (and call
511 * notify with buf NULL and size 0)?
512 * @param notify function to call
513 * @param notify_cls closure for notify
514 * @return NULL if our buffer will never hold size bytes,
515 * a handle if the notify callback was queued (can be used to cancel)
517 struct GNUNET_CONNECTION_TransmitHandle *
518 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
520 struct GNUNET_TIME_Relative timeout,
521 GNUNET_CONNECTION_TransmitReadyNotify
522 notify, void *notify_cls)
524 return GNUNET_CONNECTION_notify_transmit_ready (sock->sock,
526 timeout, notify, notify_cls);
531 * Context for processing
532 * "GNUNET_CLIENT_transmit_and_get_response" requests.
539 struct GNUNET_CLIENT_Connection *sock;
542 * Message to transmit; do not free, allocated
543 * right after this struct.
545 const struct GNUNET_MessageHeader *hdr;
550 struct GNUNET_TIME_Absolute timeout;
553 * Function to call when done.
555 GNUNET_CLIENT_MessageHandler rn;
565 * Function called to notify a client about the socket
566 * begin ready to queue the message. "buf" will be
567 * NULL and "size" zero if the socket was closed for
568 * writing in the meantime.
570 * @param cls closure of type "struct TARCtx*"
571 * @param size number of bytes available in buf
572 * @param buf where the callee should write the message
573 * @return number of bytes written to buf
576 transmit_for_response (void *cls,
580 struct TARCtx *tc = cls;
583 msize = ntohs(tc->hdr->size);
586 tc->rn (tc->rn_cls, NULL);
590 GNUNET_assert (size >= msize);
591 memcpy (buf, tc->hdr, msize);
592 GNUNET_CLIENT_receive (tc->sock,
595 GNUNET_TIME_absolute_get_remaining (tc->timeout));
602 * Convenience API that combines sending a request
603 * to the service and waiting for a response.
604 * If either operation times out, the callback
605 * will be called with a "NULL" response (in which
606 * case the connection should probably be destroyed).
608 * @param sock connection to use
609 * @param hdr message to transmit
610 * @param timeout when to give up (for both transmission
611 * and for waiting for a response)
612 * @param rn function to call with the response
613 * @param rn_cls closure for rn
616 GNUNET_CLIENT_transmit_and_get_response (struct GNUNET_CLIENT_Connection *sock,
617 const struct GNUNET_MessageHeader *hdr,
618 struct GNUNET_TIME_Relative timeout,
619 GNUNET_CLIENT_MessageHandler rn,
625 msize = ntohs(hdr->size);
626 tc = GNUNET_malloc(sizeof (struct TARCtx) + msize);
628 tc->hdr = (const struct GNUNET_MessageHeader*) &tc[1];
629 memcpy (&tc[1], hdr, msize);
630 tc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
633 GNUNET_CLIENT_notify_transmit_ready (sock,
636 &transmit_for_response,
642 /* end of client.c */