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_NETWORK_SocketHandle *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 size_t 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 struct GNUNET_CONFIGURATION_Handle *cfg)
129 struct GNUNET_CLIENT_Connection *ret;
130 struct GNUNET_NETWORK_SocketHandle *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 sock = GNUNET_NETWORK_socket_create_from_connect (sched,
153 GNUNET_SERVER_MAX_MESSAGE_SIZE);
154 GNUNET_free (hostname);
157 ret = GNUNET_malloc (sizeof (struct GNUNET_CLIENT_Connection));
160 ret->service_name = GNUNET_strdup (service_name);
166 * Receiver task has completed, free rest of client
170 finish_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
172 struct GNUNET_CLIENT_Connection *sock = cls;
174 GNUNET_array_grow (sock->received_buf, sock->received_size, 0);
175 GNUNET_free (sock->service_name);
181 * Destroy connection with the service.
184 GNUNET_CLIENT_disconnect (struct GNUNET_CLIENT_Connection *sock)
186 GNUNET_assert (sock->sock != NULL);
187 GNUNET_NETWORK_socket_destroy (sock->sock);
189 sock->receiver_handler = NULL;
190 GNUNET_SCHEDULER_add_after (sock->sched,
192 GNUNET_SCHEDULER_PRIORITY_KEEP,
193 sock->receiver_task, &finish_cleanup, sock);
198 * check if message is complete
201 check_complete (struct GNUNET_CLIENT_Connection *conn)
203 if ((conn->received_pos >= sizeof (struct GNUNET_MessageHeader)) &&
204 (conn->received_pos >=
205 ntohs (((const struct GNUNET_MessageHeader *) conn->
206 received_buf)->size)))
207 conn->msg_complete = GNUNET_YES;
212 * Callback function for data received from the network. Note that
213 * both "available" and "err" would be 0 if the read simply timed out.
216 * @param buf pointer to received data
217 * @param available number of bytes availabe in "buf",
218 * possibly 0 (on errors)
219 * @param addr address of the sender
220 * @param addrlen size of addr
221 * @param errCode value of errno (on errors receiving)
224 receive_helper (void *cls,
227 const struct sockaddr *addr, socklen_t addrlen, int errCode)
229 struct GNUNET_CLIENT_Connection *conn = cls;
230 struct GNUNET_TIME_Relative remaining;
232 GNUNET_assert (conn->msg_complete == GNUNET_NO);
233 conn->receiver_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
235 if ((available == 0) || (conn->sock == NULL) || (errCode != 0))
237 /* signal timeout! */
238 if (conn->receiver_handler != NULL)
240 conn->receiver_handler (conn->receiver_handler_cls, NULL);
241 conn->receiver_handler = NULL;
246 /* FIXME: optimize for common fast case where buf contains the
247 entire message and we need no copying... */
250 /* slow path: append to array */
251 if (conn->received_size < conn->received_pos + available)
252 GNUNET_array_grow (conn->received_buf,
253 conn->received_size, conn->received_pos + available);
254 memcpy (&conn->received_buf[conn->received_pos], buf, available);
255 conn->received_pos += available;
256 check_complete (conn);
257 /* check for timeout */
258 remaining = GNUNET_TIME_absolute_get_remaining (conn->receive_timeout);
259 if (remaining.value == 0)
261 /* signal timeout! */
262 conn->receiver_handler (conn->receiver_handler_cls, NULL);
265 /* back to receive -- either for more data or to call callback! */
266 GNUNET_CLIENT_receive (conn,
267 conn->receiver_handler,
268 conn->receiver_handler_cls, remaining);
273 * Continuation to call the receive callback.
276 receive_task (void *scls, const struct GNUNET_SCHEDULER_TaskContext *tc)
278 struct GNUNET_CLIENT_Connection *sock = scls;
279 const struct GNUNET_MessageHeader *cmsg;
280 struct GNUNET_MessageHeader *msg;
281 GNUNET_CLIENT_MessageHandler handler = sock->receiver_handler;
282 void *cls = sock->receiver_handler_cls;
285 GNUNET_assert (GNUNET_YES == sock->msg_complete);
286 sock->receiver_task = GNUNET_SCHEDULER_NO_PREREQUISITE_TASK;
287 cmsg = (const struct GNUNET_MessageHeader *) sock->received_buf;
288 msize = ntohs (cmsg->size);
289 GNUNET_assert (sock->received_pos >= msize);
292 msg = GNUNET_malloc (msize);
293 memcpy (msg, cmsg, msize);
294 memmove (sock->received_buf,
295 &sock->received_buf[msize], sock->received_pos - msize);
297 sock->received_pos -= msize;
298 sock->msg_complete = GNUNET_NO;
299 sock->receiver_handler = NULL;
300 check_complete (sock);
310 * Read from the service.
312 * @param sched scheduler to use
313 * @param sock the service
314 * @param handler function to call with the message
315 * @param cls closure for handler
316 * @param timeout how long to wait until timing out
319 GNUNET_CLIENT_receive (struct GNUNET_CLIENT_Connection *sock,
320 GNUNET_CLIENT_MessageHandler handler,
321 void *cls, struct GNUNET_TIME_Relative timeout)
323 if (sock->sock == NULL)
325 /* already disconnected, fail instantly! */
326 GNUNET_break (0); /* this should not happen in well-written code! */
330 GNUNET_assert (sock->receiver_task ==
331 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK);
332 sock->receiver_handler = handler;
333 sock->receiver_handler_cls = cls;
334 sock->receive_timeout = GNUNET_TIME_relative_to_absolute (timeout);
335 if (GNUNET_YES == sock->msg_complete)
336 sock->receiver_task = GNUNET_SCHEDULER_add_after (sock->sched,
338 GNUNET_SCHEDULER_PRIORITY_KEEP,
339 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
340 &receive_task, sock);
342 sock->receiver_task = GNUNET_NETWORK_receive (sock->sock,
343 GNUNET_SERVER_MAX_MESSAGE_SIZE,
345 &receive_helper, sock);
350 write_shutdown (void *cls, size_t size, void *buf)
352 struct GNUNET_MessageHeader *msg;
354 if (size < sizeof (struct GNUNET_MessageHeader))
355 return 0; /* client disconnected */
356 msg = (struct GNUNET_MessageHeader *) buf;
357 msg->type = htons (GNUNET_MESSAGE_TYPE_SHUTDOWN);
358 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
359 return sizeof (struct GNUNET_MessageHeader);
364 * Request that the service should shutdown.
365 * Afterwards, the connection should be disconnected.
367 * @param sched scheduler to use
368 * @param sock the socket connected to the service
371 GNUNET_CLIENT_service_shutdown (struct GNUNET_CLIENT_Connection *sock)
373 GNUNET_NETWORK_notify_transmit_ready (sock->sock,
374 sizeof (struct GNUNET_MessageHeader),
375 GNUNET_TIME_UNIT_FOREVER_REL,
376 &write_shutdown, NULL);
381 * Report service unavailable.
384 service_test_error (struct GNUNET_SCHEDULER_Handle *s,
385 GNUNET_SCHEDULER_Task task, void *task_cls)
387 GNUNET_SCHEDULER_add_continuation (s,
391 GNUNET_SCHEDULER_REASON_TIMEOUT);
396 * Receive confirmation from test, service is up.
399 * @param msg message received, NULL on timeout or fatal error
402 confirm_handler (void *cls, const struct GNUNET_MessageHeader *msg)
404 struct GNUNET_CLIENT_Connection *conn = cls;
405 /* We may want to consider looking at the reply in more
406 detail in the future, for example, is this the
407 correct service? FIXME! */
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 "Received confirmation that service is running.\n");
414 GNUNET_SCHEDULER_add_continuation (conn->sched,
418 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
422 service_test_error (conn->sched, conn->test_cb, conn->test_cb_cls);
424 GNUNET_CLIENT_disconnect (conn);
429 write_test (void *cls, size_t size, void *buf)
431 struct GNUNET_MessageHeader *msg;
433 if (size < sizeof (struct GNUNET_MessageHeader))
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
437 _("Failure to transmit TEST request.\n"));
439 return 0; /* client disconnected */
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Transmitting TEST request.\n"));
444 msg = (struct GNUNET_MessageHeader *) buf;
445 msg->type = htons (GNUNET_MESSAGE_TYPE_TEST);
446 msg->size = htons (sizeof (struct GNUNET_MessageHeader));
447 return sizeof (struct GNUNET_MessageHeader);
452 * Wait until the service is running.
454 * @param sched scheduler to use
455 * @param service name of the service to wait for
456 * @param cfg configuration to use
457 * @param timeout how long to wait at most in ms
458 * @param task task to run if service is running
459 * (reason will be "PREREQ_DONE" (service running)
460 * or "TIMEOUT" (service not known to be running))
461 * @param task_cls closure for task
464 GNUNET_CLIENT_service_test (struct GNUNET_SCHEDULER_Handle *sched,
466 struct GNUNET_CONFIGURATION_Handle *cfg,
467 struct GNUNET_TIME_Relative timeout,
468 GNUNET_SCHEDULER_Task task, void *task_cls)
470 struct GNUNET_CLIENT_Connection *conn;
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "Testing if service `%s' is running.\n", service);
476 conn = GNUNET_CLIENT_connect (sched, service, cfg);
479 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
481 ("Could not connect to service `%s', must not be running.\n"),
483 service_test_error (sched, task, task_cls);
486 conn->test_cb = task;
487 conn->test_cb_cls = task_cls;
489 GNUNET_NETWORK_notify_transmit_ready (conn->sock,
491 GNUNET_MessageHeader),
492 timeout, &write_test, NULL))
494 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
495 _("Failure to transmit request to service `%s'\n"),
497 service_test_error (sched, task, task_cls);
498 GNUNET_CLIENT_disconnect (conn);
501 GNUNET_CLIENT_receive (conn, &confirm_handler, conn, timeout);
506 * Ask the client to call us once the specified number of bytes
507 * are free in the transmission buffer. May call the notify
508 * method immediately if enough space is available.
510 * @param client connection to the service
511 * @param size number of bytes to send
512 * @param timeout after how long should we give up (and call
513 * notify with buf NULL and size 0)?
514 * @param notify function to call
515 * @param notify_cls closure for notify
516 * @return NULL if our buffer will never hold size bytes,
517 * a handle if the notify callback was queued (can be used to cancel)
519 struct GNUNET_NETWORK_TransmitHandle *
520 GNUNET_CLIENT_notify_transmit_ready (struct GNUNET_CLIENT_Connection *sock,
522 struct GNUNET_TIME_Relative timeout,
523 GNUNET_NETWORK_TransmitReadyNotify
524 notify, void *notify_cls)
526 return GNUNET_NETWORK_notify_transmit_ready (sock->sock,
528 timeout, notify, notify_cls);
532 /* end of client.c */