2 This file is part of GNUnet.
3 (C) 2012 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.
22 * @file testbed/gnunet-service-testbed.c
23 * @brief implementation of the TESTBED service
24 * @author Sree Harsha Totakura
28 #include "gnunet_service_lib.h"
29 #include "gnunet_server_lib.h"
33 #include "gnunet_testbed_service.h"
34 #include "testbed_api_hosts.h"
39 #define LOG(kind,...) \
40 GNUNET_log (kind, __VA_ARGS__)
45 #define LOG_DEBUG(...) \
46 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
51 * The client handle associated with this context
53 struct GNUNET_SERVER_Client *client;
56 * Event mask of event to be responded in this context
61 * Our host id according to this context
68 * The message queue for sending messages to clients
73 * The message to be sent
75 struct GNUNET_MessageHeader *msg;
78 * The client to send the message to
80 struct GNUNET_SERVER_Client *client;
83 * next pointer for DLL
85 struct MessageQueue *next;
88 * prev pointer for DLL
90 struct MessageQueue *prev;
95 * The structure for identifying a shared service
100 * The name of the shared service
105 * Number of shared peers per instance of the shared service
110 * Number of peers currently sharing the service
112 uint32_t num_sharing;
122 * The destination host
124 const struct GNUNET_TESTBED_Host *dest;
127 * The forwarding (next hop) host
129 const struct GNUNET_TESTBED_Host *fhost;
132 * The controller handle if we have started the controller at the next hop
135 struct GNUNET_TESTBED_Controller *fcontroller;
142 static struct GNUNET_DISK_FileHandle *fh;
145 * The master context; generated with the first INIT message
147 static struct Context *master_context;
150 * The shutdown task handle
152 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
157 static struct GNUNET_TESTBED_Host **host_list;
160 * The size of the host list
162 static uint32_t host_list_size;
167 static struct Route **route_list;
170 * The size of the route list
172 static uint32_t route_list_size;
175 * The message queue head
177 static struct MessageQueue *mq_head;
180 * The message queue tail
182 static struct MessageQueue *mq_tail;
185 * Current Transmit Handle; NULL if no notify transmit exists currently
187 struct GNUNET_SERVER_TransmitHandle *transmit_handle;
190 * The hashmap of shared services
192 struct GNUNET_CONTAINER_MultiHashMap *ss_map;
196 * Function called to notify a client about the connection begin ready to queue
197 * more data. "buf" will be NULL and "size" zero if the connection was closed
198 * for writing in the meantime.
201 * @param size number of bytes available in buf
202 * @param buf where the callee should write the message
203 * @return number of bytes written to buf
206 transmit_ready_notify (void *cls, size_t size, void *buf)
208 struct MessageQueue *mq_entry;
210 transmit_handle = NULL;
212 GNUNET_assert (NULL != mq_entry);
215 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
216 size = ntohs (mq_entry->msg->size);
217 memcpy (buf, mq_entry->msg, size);
218 GNUNET_free (mq_entry->msg);
219 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
220 GNUNET_free (mq_entry);
222 if (NULL != mq_entry)
224 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
225 ntohs (mq_entry->msg->size),
226 GNUNET_TIME_UNIT_FOREVER_REL,
227 &transmit_ready_notify, NULL);
233 * Queues a message in send queue for sending to the service
235 * @param client the client to whom the queued message has to be sent
236 * @param msg the message to queue
239 queue_message (struct GNUNET_SERVER_Client *client,
240 struct GNUNET_MessageHeader *msg)
242 struct MessageQueue *mq_entry;
246 type = ntohs (msg->type);
247 size = ntohs (msg->size);
248 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
249 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
250 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
252 mq_entry->client = client;
253 LOG_DEBUG ( "Queueing message of type %u, size %u for sending\n", type,
255 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
256 if (NULL == transmit_handle)
258 GNUNET_SERVER_notify_transmit_ready (client, size,
259 GNUNET_TIME_UNIT_FOREVER_REL,
260 &transmit_ready_notify, NULL);
265 * Function to add a host to the current list of known hosts
267 * @param host the host to add
268 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
272 host_list_add (struct GNUNET_TESTBED_Host *host)
276 host_id = GNUNET_TESTBED_host_get_id_ (host);
277 if (host_list_size <= host_id)
279 host_list = GNUNET_realloc (host_list,
280 sizeof (struct GNUNET_TESTBED_Host *)
282 host_list_size += (host_id + 10);
284 if (NULL != host_list[host_id])
286 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
287 return GNUNET_SYSERR;
289 host_list[host_id] = host;
295 * Routes message to a host given its host_id
297 * @param host_id the id of the destination host
298 * @param msg the message to be routed
301 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
308 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
311 * @param client identification of the client
312 * @param message the actual message
315 handle_init (void *cls,
316 struct GNUNET_SERVER_Client *client,
317 const struct GNUNET_MessageHeader *message)
319 const struct GNUNET_TESTBED_InitMessage *msg;
320 struct GNUNET_TESTBED_Host *host;
322 if (NULL != master_context)
325 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
328 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
329 master_context = GNUNET_malloc (sizeof (struct Context));
330 master_context->client = client;
331 master_context->host_id = ntohl (msg->host_id);
332 host = GNUNET_TESTBED_host_create_with_id (master_context->host_id,
334 host_list_add (host);
335 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
336 GNUNET_SERVER_client_keep (client);
337 LOG_DEBUG ("Created master context with host ID: %u\n",
338 master_context->host_id);
339 GNUNET_SERVER_receive_done (client, GNUNET_OK);
344 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
347 * @param client identification of the client
348 * @param message the actual message
351 handle_add_host (void *cls,
352 struct GNUNET_SERVER_Client *client,
353 const struct GNUNET_MessageHeader *message)
355 struct GNUNET_TESTBED_Host *host;
356 const struct GNUNET_TESTBED_AddHostMessage *msg;
357 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
362 uint16_t username_length;
363 uint16_t hostname_length;
366 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
367 username_length = ntohs (msg->user_name_length);
368 username_length = (0 == username_length) ? 0 : username_length + 1;
369 username = (char *) &(msg[1]);
370 hostname = username + username_length;
371 if (ntohs (message->size) <=
372 (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length))
375 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
378 hostname_length = ntohs (message->size)
379 - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
380 if (strlen (hostname) != hostname_length)
383 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
386 host_id = ntohl (msg->host_id);
387 LOG_DEBUG ("Received ADDHOST message\n");
388 LOG_DEBUG ("-------host id: %u\n", host_id);
389 if (NULL != hostname) LOG_DEBUG ("-------hostname: %s\n", hostname);
390 if (NULL != username) LOG_DEBUG ("-------username: %s\n", username);
391 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
392 host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
393 ntohs (msg->ssh_port));
394 GNUNET_SERVER_receive_done (client, GNUNET_OK);
395 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
396 if (GNUNET_OK != host_list_add (host))
398 /* We are unable to add a host */
399 emsg = "A host exists with given host-id";
400 LOG_DEBUG ("%s: %u", emsg, host_id);
401 GNUNET_TESTBED_host_destroy (host);
402 reply_size += strlen (emsg) + 1;
403 reply = GNUNET_malloc (reply_size);
404 memcpy (&reply[1], emsg, strlen (emsg) + 1);
407 reply = GNUNET_malloc (reply_size);
408 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
409 reply->header.size = htons (reply_size);
410 reply->host_id = htonl (host_id);
411 queue_message (client, (struct GNUNET_MessageHeader *) reply);
416 * Iterator over hash map entries.
419 * @param key current key code
420 * @param value value in the hash map
421 * @return GNUNET_YES if we should continue to
425 int ss_exists_iterator (void *cls,
426 const struct GNUNET_HashCode * key,
429 struct SharedService *queried_ss = cls;
430 struct SharedService *ss = value;
432 if (0 == strcmp (ss->name, queried_ss->name))
439 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
442 * @param client identification of the client
443 * @param message the actual message
446 handle_configure_shared_service (void *cls,
447 struct GNUNET_SERVER_Client *client,
448 const struct GNUNET_MessageHeader *message)
450 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
451 struct SharedService *ss;
453 struct GNUNET_HashCode hash;
455 uint16_t service_name_size;
457 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
458 msg_size = ntohs (message->size);
459 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
462 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
465 service_name_size = msg_size -
466 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
467 service_name = (char *) &msg[1];
468 if ('\0' != service_name[service_name_size - 1])
471 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
474 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
475 service_name, ntohl (msg->num_peers));
476 if (ntohl (msg->host_id) != master_context->host_id)
478 route_message (ntohl (msg->host_id), message);
479 GNUNET_SERVER_receive_done (client, GNUNET_OK);
482 GNUNET_SERVER_receive_done (client, GNUNET_OK);
483 ss = GNUNET_malloc (sizeof (struct SharedService));
484 ss->name = strdup (service_name);
485 ss->num_shared = ntohl (msg->num_peers);
486 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
488 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
489 &ss_exists_iterator, ss))
491 LOG (GNUNET_ERROR_TYPE_WARNING,
492 "Service %s already configured as a shared service. "
493 "Ignoring service sharing request \n", ss->name);
494 GNUNET_free (ss->name);
498 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
499 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
504 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
507 * @param client identification of the client
508 * @param message the actual message
511 handle_link_controllers (void *cls,
512 struct GNUNET_SERVER_Client *client,
513 const struct GNUNET_MessageHeader *message)
515 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
516 struct GNUNET_CONFIGURATION_Handle *cfg;
520 uint32_t delegated_host_id;
521 uint32_t slave_host_id;
524 msize = ntohs (message->size);
525 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
528 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
531 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
532 delegated_host_id = ntohl (msg->delegated_host_id);
533 if (delegated_host_id == master_context->host_id)
536 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
537 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
540 if ((delegated_host_id < route_list_size) &&
541 (NULL != route_list[delegated_host_id]))
543 LOG (GNUNET_ERROR_TYPE_WARNING,
544 "Delegated host has already been linked\n");
545 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
548 if ((delegated_host_id >= host_list_size) ||
549 (NULL == host_list[delegated_host_id]))
551 LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
552 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
555 slave_host_id = ntohl (msg->slave_host_id);
556 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
558 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
559 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
562 config_size = ntohs (msg->config_size);
563 config = GNUNET_malloc (config_size);
564 dest_size = (uLongf) config_size;
565 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
566 if (Z_OK != uncompress ((Bytef *) config, &dest_size,
567 (const Bytef *) &msg[1], (uLong) msize))
569 GNUNET_break (0); /* Compression error */
570 GNUNET_free (config);
571 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
574 GNUNET_assert (config_size == dest_size);
575 cfg = GNUNET_CONFIGURATION_create ();
576 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
579 GNUNET_break (0); /* Configuration parsing error */
580 GNUNET_break (config);
581 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
584 GNUNET_free (config);
585 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
586 /* If we are not the slave controller then we have to route the request
587 towards the slave controller */
588 if (1 == msg->is_subordinate)
590 GNUNET_break (0); /* FIXME: Implement the slave controller
593 GNUNET_CONFIGURATION_destroy (cfg);
598 * Iterator over hash map entries.
601 * @param key current key code
602 * @param value value in the hash map
603 * @return GNUNET_YES if we should continue to
608 ss_map_free_iterator (void *cls,
609 const struct GNUNET_HashCode * key, void *value)
611 struct SharedService *ss = value;
613 GNUNET_assert (GNUNET_YES ==
614 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
615 GNUNET_free (ss->name);
622 * Task to clean up and shutdown nicely
625 * @param tc the TaskContext from scheduler
628 shutdown_task (void *cls,
629 const struct GNUNET_SCHEDULER_TaskContext *tc)
634 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
635 GNUNET_SCHEDULER_shutdown ();
636 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
637 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
639 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
642 GNUNET_DISK_file_close (fh);
645 /* Clear host list */
646 for (host_id = 0; host_id < host_list_size; host_id++)
647 if (NULL != host_list[host_id])
648 GNUNET_TESTBED_host_destroy (host_list[host_id]);
649 GNUNET_free_non_null (host_list);
650 /* Clear route list */
651 for (route_id = 0; route_id < route_list_size; route_id++)
652 if (NULL != route_list[route_id])
654 if (NULL != route_list[route_id]->fcontroller)
655 GNUNET_TESTBED_controller_stop (route_list[route_id]->fcontroller);
656 GNUNET_free (route_list[route_id]);
658 GNUNET_free_non_null (route_list);
659 GNUNET_free_non_null (master_context);
664 * Callback for client disconnect
667 * @param client the client which has disconnected
670 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
672 if (NULL == master_context)
674 if (client == master_context->client)
676 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
677 GNUNET_SERVER_client_drop (client);
678 /* should not be needed as we're terminated by failure to read
679 from stdin, but if stdin fails for some reason, this shouldn't
680 hurt for now --- might need to revise this later if we ever
681 decide that master connections might be temporarily down
683 GNUNET_SCHEDULER_shutdown ();
692 * @param server the initialized server
693 * @param cfg configuration to use
696 testbed_run (void *cls,
697 struct GNUNET_SERVER_Handle *server,
698 const struct GNUNET_CONFIGURATION_Handle *cfg)
700 static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
702 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT,
703 sizeof (struct GNUNET_TESTBED_InitMessage)},
704 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
705 {&handle_configure_shared_service, NULL,
706 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
707 {&handle_link_controllers, NULL,
708 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
712 GNUNET_SERVER_add_handlers (server,
714 GNUNET_SERVER_disconnect_notify (server,
715 &client_disconnect_cb,
717 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
718 fh = GNUNET_DISK_get_handle_from_native (stdin);
721 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
726 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
734 * The starting point of execution
736 int main (int argc, char *const *argv)
740 GNUNET_SERVICE_run (argc,
743 GNUNET_SERVICE_OPTION_NONE,