2 This file is part of GNUnet
3 (C) 2013 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 set/gnunet-service-set.c
23 * @brief two-peer set operations
24 * @author Florian Dold
28 #include "gnunet-service-set.h"
29 #include "set_protocol.h"
33 * Configuration of our local peer.
35 const struct GNUNET_CONFIGURATION_Handle *configuration;
38 * Socket listening for other peers via stream.
40 static struct GNUNET_STREAM_ListenSocket *stream_listen_socket;
43 * Sets are held in a doubly linked list.
45 static struct Set *sets_head;
48 * Sets are held in a doubly linked list.
50 static struct Set *sets_tail;
53 * Listeners are held in a doubly linked list.
55 static struct Listener *listeners_head;
58 * Listeners are held in a doubly linked list.
60 static struct Listener *listeners_tail;
63 * Incoming sockets from remote peers are
64 * held in a doubly linked list.
66 static struct Incoming *incoming_head;
69 * Incoming sockets from remote peers are
70 * held in a doubly linked list.
72 static struct Incoming *incoming_tail;
75 * Counter for allocating unique IDs for clients,
76 * used to identify incoming operation requests from remote peers,
77 * that the client can choose to accept or refuse.
79 static uint32_t accept_id = 1;
83 * Get set that is owned by the client, if any.
85 * @param client client to look for
86 * @return set that the client owns, NULL if the client
90 get_set (struct GNUNET_SERVER_Client *client)
93 for (set = sets_head; NULL != set; set = set->next)
94 if (set->client == client)
101 * Get the listener associated to a client, if any.
103 * @param client the client
104 * @return listener associated with the client, NULL
107 static struct Listener *
108 get_listener (struct GNUNET_SERVER_Client *client)
110 struct Listener *listener;
111 for (listener = listeners_head; NULL != listener; listener = listener->next)
112 if (listener->client == client)
119 * Get the incoming socket associated with the given id.
121 * @param id id to look for
122 * @return the incoming socket associated with the id,
123 * or NULL if there is none
125 static struct Incoming *
126 get_incoming (uint32_t id)
128 struct Incoming *incoming;
129 for (incoming = incoming_head; NULL != incoming; incoming = incoming->next)
130 if (incoming->accept_id == id)
137 * Destroy a listener, free all resources associated with it.
139 * @param listener listener to destroy
142 destroy_listener (struct Listener *listener)
144 if (NULL != listener->client_mq)
146 GNUNET_MQ_destroy (listener->client_mq);
147 listener->client_mq = NULL;
149 if (NULL != listener->client)
151 GNUNET_SERVER_client_drop (listener->client);
152 listener->client = NULL;
155 GNUNET_CONTAINER_DLL_remove (listeners_head, listeners_tail, listener);
156 GNUNET_free (listener);
161 * Destroy a set, and free all resources associated with it.
163 * @param set the set to destroy
166 destroy_set (struct Set *set)
168 switch (set->operation)
170 case GNUNET_SET_OPERATION_INTERSECTION:
173 case GNUNET_SET_OPERATION_UNION:
174 _GSS_union_set_destroy (set);
180 GNUNET_CONTAINER_DLL_remove (sets_head, sets_tail, set);
186 * Clean up after a client after it is
187 * disconnected (either by us or by itself)
189 * @param cls closure, unused
190 * @param client the client to clean up after
193 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
196 struct Listener *listener;
198 set = get_set (client);
201 listener = get_listener (client);
202 if (NULL != listener)
203 destroy_listener (listener);
208 * Destroy an incoming request from a remote peer
210 * @param incoming remote request to destroy
213 destroy_incoming (struct Incoming *incoming)
215 if (NULL != incoming->mq)
217 GNUNET_MQ_destroy (incoming->mq);
220 if (NULL != incoming->socket)
222 GNUNET_STREAM_close (incoming->socket);
223 incoming->socket = NULL;
225 GNUNET_CONTAINER_DLL_remove (incoming_head, incoming_tail, incoming);
226 GNUNET_free (incoming);
231 * Handle a request for a set operation from
234 * @param cls the incoming socket
235 * @param mh the message
238 handle_p2p_operation_request (void *cls, const struct GNUNET_MessageHeader *mh)
240 struct Incoming *incoming = cls;
241 const struct OperationRequestMessage *msg = (const struct OperationRequestMessage *) mh;
242 struct GNUNET_MQ_Message *mqm;
243 struct RequestMessage *cmsg;
244 struct Listener *listener;
245 const struct GNUNET_MessageHeader *context_msg;
247 if (ntohs (mh->size) < sizeof *msg)
249 /* message is to small for its type */
251 destroy_incoming (incoming);
254 else if (ntohs (mh->size) == sizeof *msg)
256 /* there is no context message */
261 context_msg = &msg[1].header;
262 if ((ntohs (context_msg->size) + sizeof *msg) != ntohs (msg->header.size))
264 /* size of context message is invalid */
266 destroy_incoming (incoming);
271 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "received P2P operation request (op %u, app %s)\n",
272 ntohs (msg->operation), GNUNET_h2s (&msg->app_id));
274 /* find the appropriate listener */
275 for (listener = listeners_head;
277 listener = listener->next)
279 if ( (0 != GNUNET_CRYPTO_hash_cmp (&msg->app_id, &listener->app_id)) ||
280 (ntohs (msg->operation) != listener->operation) )
282 mqm = GNUNET_MQ_msg (cmsg, GNUNET_MESSAGE_TYPE_SET_REQUEST);
283 if (GNUNET_OK != GNUNET_MQ_nest_mh (mqm, context_msg))
285 /* FIXME: disconnect the peer */
286 GNUNET_MQ_discard (mqm);
290 incoming->accept_id = accept_id++;
291 cmsg->accept_id = htonl (incoming->accept_id);
292 GNUNET_MQ_send (listener->client_mq, mqm);
296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
297 "set operation request from peer failed: "
298 "no set with matching application ID and operation type\n");
303 * Called when a client wants to create a new set.
306 * @param client client that sent the message
307 * @param m message sent by the client
310 handle_client_create (void *cls,
311 struct GNUNET_SERVER_Client *client,
312 const struct GNUNET_MessageHeader *m)
314 struct SetCreateMessage *msg = (struct SetCreateMessage *) m;
317 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "client created new set (operation %u)\n",
318 ntohs (msg->operation));
320 if (NULL != get_set (client))
323 GNUNET_SERVER_client_disconnect (client);
329 switch (ntohs (msg->operation))
331 case GNUNET_SET_OPERATION_INTERSECTION:
335 case GNUNET_SET_OPERATION_UNION:
336 set = _GSS_union_set_create ();
340 GNUNET_SERVER_client_disconnect (client);
344 GNUNET_assert (NULL != set);
346 set->client = client;
347 GNUNET_SERVER_client_keep (client);
348 set->client_mq = GNUNET_MQ_queue_for_server_client (client);
349 GNUNET_CONTAINER_DLL_insert (sets_head, sets_tail, set);
350 GNUNET_SERVER_receive_done (client, GNUNET_OK);
355 * Called when a client wants to create a new listener.
358 * @param client client that sent the message
359 * @param m message sent by the client
362 handle_client_listen (void *cls,
363 struct GNUNET_SERVER_Client *client,
364 const struct GNUNET_MessageHeader *m)
366 struct ListenMessage *msg = (struct ListenMessage *) m;
367 struct Listener *listener;
369 if (NULL != get_listener (client))
372 GNUNET_SERVER_client_disconnect (client);
375 listener = GNUNET_new (struct Listener);
376 listener->client = client;
377 GNUNET_SERVER_client_keep (client);
378 listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
379 listener->app_id = msg->app_id;
380 listener->operation = ntohs (msg->operation);
381 GNUNET_CONTAINER_DLL_insert_tail (listeners_head, listeners_tail, listener);
382 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "new listener created (op %u, app %s)\n",
383 listener->operation, GNUNET_h2s (&listener->app_id));
384 GNUNET_SERVER_receive_done (client, GNUNET_OK);
389 * Called when a client wants to remove an element
390 * from the set it inhabits.
393 * @param client client that sent the message
394 * @param m message sent by the client
397 handle_client_remove (void *cls,
398 struct GNUNET_SERVER_Client *client,
399 const struct GNUNET_MessageHeader *m)
403 set = get_set (client);
407 GNUNET_SERVER_client_disconnect (client);
410 switch (set->operation)
412 case GNUNET_SET_OPERATION_UNION:
413 _GSS_union_remove ((struct ElementMessage *) m, set);
414 case GNUNET_SET_OPERATION_INTERSECTION:
422 GNUNET_SERVER_receive_done (client, GNUNET_OK);
427 * Called when a client wants to add an element to a
431 * @param client client that sent the message
432 * @param m message sent by the client
435 handle_client_add (void *cls,
436 struct GNUNET_SERVER_Client *client,
437 const struct GNUNET_MessageHeader *m)
441 set = get_set (client);
445 GNUNET_SERVER_client_disconnect (client);
448 switch (set->operation)
450 case GNUNET_SET_OPERATION_UNION:
451 _GSS_union_add ((struct ElementMessage *) m, set);
452 case GNUNET_SET_OPERATION_INTERSECTION:
460 GNUNET_SERVER_receive_done (client, GNUNET_OK);
465 * Called when a client wants to evaluate a set operation with another peer.
468 * @param client client that sent the message
469 * @param m message sent by the client
472 handle_client_evaluate (void *cls,
473 struct GNUNET_SERVER_Client *client,
474 const struct GNUNET_MessageHeader *m)
478 set = get_set (client);
482 GNUNET_SERVER_client_disconnect (client);
487 switch (set->operation)
489 case GNUNET_SET_OPERATION_INTERSECTION:
492 case GNUNET_SET_OPERATION_UNION:
493 _GSS_union_evaluate ((struct EvaluateMessage *) m, set);
500 GNUNET_SERVER_receive_done (client, GNUNET_OK);
505 * Handle a cancel request from a client.
508 * @param client the client
509 * @param m the cancel message
512 handle_client_cancel (void *cls,
513 struct GNUNET_SERVER_Client *client,
514 const struct GNUNET_MessageHeader *m)
516 /* FIXME: implement */
517 GNUNET_SERVER_receive_done (client, GNUNET_OK);
522 * Handle an ack from a client.
525 * @param client the client
526 * @param m the message
529 handle_client_ack (void *cls,
530 struct GNUNET_SERVER_Client *client,
531 const struct GNUNET_MessageHeader *m)
533 /* FIXME: implement */
534 GNUNET_SERVER_receive_done (client, GNUNET_OK);
539 * Handle a request from the client to accept
540 * a set operation that came from a remote peer.
543 * @param client the client
544 * @param mh the message
547 handle_client_accept (void *cls,
548 struct GNUNET_SERVER_Client *client,
549 const struct GNUNET_MessageHeader *mh)
552 struct Incoming *incoming;
553 struct AcceptMessage *msg = (struct AcceptMessage *) mh;
556 incoming = get_incoming (ntohl (msg->accept_id));
558 if (NULL == incoming)
561 GNUNET_SERVER_client_disconnect (client);
565 if (0 == ntohl (msg->request_id))
567 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "peer request rejected by client\n");
568 destroy_incoming (incoming);
569 GNUNET_SERVER_receive_done (client, GNUNET_OK);
573 set = get_set (client);
578 GNUNET_SERVER_client_disconnect (client);
582 switch (set->operation)
584 case GNUNET_SET_OPERATION_INTERSECTION:
588 case GNUNET_SET_OPERATION_UNION:
589 _GSS_union_accept (msg, set, incoming);
596 /* note: _GSS_*_accept has to make sure the socket and mq are set to NULL,
597 * otherwise they will be destroyed and disconnected */
598 destroy_incoming (incoming);
599 GNUNET_SERVER_receive_done (client, GNUNET_OK);
604 * Functions of this type are called upon new stream connection from other peers
605 * or upon binding error which happen when the app_port given in
606 * GNUNET_STREAM_listen() is already taken.
608 * @param cls the closure from GNUNET_STREAM_listen
609 * @param socket the socket representing the stream; NULL on binding error
610 * @param initiator the identity of the peer who wants to establish a stream
611 * with us; NULL on binding error
612 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
613 * stream (the socket will be invalid after the call)
616 stream_listen_cb (void *cls,
617 struct GNUNET_STREAM_Socket *socket,
618 const struct GNUNET_PeerIdentity *initiator)
620 struct Incoming *incoming;
621 static const struct GNUNET_MQ_Handler handlers[] = {
622 {handle_p2p_operation_request, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST},
623 GNUNET_MQ_HANDLERS_END
629 return GNUNET_SYSERR;
632 incoming = GNUNET_new (struct Incoming);
633 incoming->peer = *initiator;
634 incoming->socket = socket;
635 incoming->mq = GNUNET_STREAM_mq_create (incoming->socket, handlers, NULL, incoming);
636 /* FIXME: timeout for peers that only connect but don't send anything */
637 GNUNET_CONTAINER_DLL_insert_tail (incoming_head, incoming_tail, incoming);
643 * Called to clean up, after a shutdown has been requested.
646 * @param tc context information (why was this task triggered now)
649 shutdown_task (void *cls,
650 const struct GNUNET_SCHEDULER_TaskContext *tc)
652 if (NULL != stream_listen_socket)
654 GNUNET_STREAM_listen_close (stream_listen_socket);
655 stream_listen_socket = NULL;
658 while (NULL != incoming_head)
660 destroy_incoming (incoming_head);
663 while (NULL != listeners_head)
665 destroy_listener (listeners_head);
668 while (NULL != sets_head)
670 destroy_set (sets_head);
673 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "handled shutdown request\n");
678 * Function called by the service's run
679 * method to run service-specific setup code.
682 * @param server the initialized server
683 * @param cfg configuration to use
686 run (void *cls, struct GNUNET_SERVER_Handle *server,
687 const struct GNUNET_CONFIGURATION_Handle *cfg)
689 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
690 {handle_client_create, NULL, GNUNET_MESSAGE_TYPE_SET_CREATE, 0},
691 {handle_client_listen, NULL, GNUNET_MESSAGE_TYPE_SET_LISTEN, 0},
692 {handle_client_add, NULL, GNUNET_MESSAGE_TYPE_SET_ADD, 0},
693 {handle_client_remove, NULL, GNUNET_MESSAGE_TYPE_SET_REMOVE, 0},
694 {handle_client_cancel, NULL, GNUNET_MESSAGE_TYPE_SET_CANCEL, 0},
695 {handle_client_evaluate, NULL, GNUNET_MESSAGE_TYPE_SET_EVALUATE, 0},
696 {handle_client_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ACK, 0},
697 {handle_client_accept, NULL, GNUNET_MESSAGE_TYPE_SET_ACCEPT, 0},
702 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL);
703 GNUNET_SERVER_disconnect_notify (server, handle_client_disconnect, NULL);
704 GNUNET_SERVER_add_handlers (server, server_handlers);
705 stream_listen_socket = GNUNET_STREAM_listen (cfg, GNUNET_APPLICATION_TYPE_SET,
706 &stream_listen_cb, NULL,
707 GNUNET_STREAM_OPTION_END);
712 * The main function for the set service.
714 * @param argc number of arguments from the command line
715 * @param argv command line arguments
716 * @return 0 ok, 1 on error
719 main (int argc, char *const *argv)
722 ret = GNUNET_SERVICE_run (argc, argv, "set", GNUNET_SERVICE_OPTION_NONE, &run, NULL);
723 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "exit\n");
724 return (GNUNET_OK == ret) ? 0 : 1;