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);
327 set = GNUNET_new (struct Set);
329 switch (ntohs (msg->operation))
331 case GNUNET_SET_OPERATION_INTERSECTION:
335 case GNUNET_SET_OPERATION_UNION:
336 set = _GSS_union_set_create ();
341 GNUNET_SERVER_client_disconnect (client);
345 set->client = client;
346 GNUNET_SERVER_client_keep (client);
347 set->client_mq = GNUNET_MQ_queue_for_server_client (client);
348 GNUNET_CONTAINER_DLL_insert (sets_head, sets_tail, set);
349 GNUNET_SERVER_receive_done (client, GNUNET_OK);
354 * Called when a client wants to create a new listener.
357 * @param client client that sent the message
358 * @param m message sent by the client
361 handle_client_listen (void *cls,
362 struct GNUNET_SERVER_Client *client,
363 const struct GNUNET_MessageHeader *m)
365 struct ListenMessage *msg = (struct ListenMessage *) m;
366 struct Listener *listener;
368 if (NULL != get_listener (client))
371 GNUNET_SERVER_client_disconnect (client);
374 listener = GNUNET_new (struct Listener);
375 listener->client = client;
376 GNUNET_SERVER_client_keep (client);
377 listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
378 listener->app_id = msg->app_id;
379 listener->operation = ntohs (msg->operation);
380 GNUNET_CONTAINER_DLL_insert_tail (listeners_head, listeners_tail, listener);
381 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "new listener created (op %u, app %s)\n",
382 listener->operation, GNUNET_h2s (&listener->app_id));
383 GNUNET_SERVER_receive_done (client, GNUNET_OK);
388 * Called when a client wants to remove an element
389 * from the set it inhabits.
392 * @param client client that sent the message
393 * @param m message sent by the client
396 handle_client_remove (void *cls,
397 struct GNUNET_SERVER_Client *client,
398 const struct GNUNET_MessageHeader *m)
402 set = get_set (client);
406 GNUNET_SERVER_client_disconnect (client);
409 switch (set->operation)
411 case GNUNET_SET_OPERATION_UNION:
412 _GSS_union_remove ((struct ElementMessage *) m, set);
413 case GNUNET_SET_OPERATION_INTERSECTION:
421 GNUNET_SERVER_receive_done (client, GNUNET_OK);
426 * Called when a client wants to add an element to a
430 * @param client client that sent the message
431 * @param m message sent by the client
434 handle_client_add (void *cls,
435 struct GNUNET_SERVER_Client *client,
436 const struct GNUNET_MessageHeader *m)
440 set = get_set (client);
444 GNUNET_SERVER_client_disconnect (client);
447 switch (set->operation)
449 case GNUNET_SET_OPERATION_UNION:
450 _GSS_union_add ((struct ElementMessage *) m, set);
451 case GNUNET_SET_OPERATION_INTERSECTION:
459 GNUNET_SERVER_receive_done (client, GNUNET_OK);
464 * Called when a client wants to evaluate a set operation with another peer.
467 * @param client client that sent the message
468 * @param m message sent by the client
471 handle_client_evaluate (void *cls,
472 struct GNUNET_SERVER_Client *client,
473 const struct GNUNET_MessageHeader *m)
477 set = get_set (client);
481 GNUNET_SERVER_client_disconnect (client);
486 switch (set->operation)
488 case GNUNET_SET_OPERATION_INTERSECTION:
491 case GNUNET_SET_OPERATION_UNION:
492 _GSS_union_evaluate ((struct EvaluateMessage *) m, set);
499 GNUNET_SERVER_receive_done (client, GNUNET_OK);
504 * Handle a cancel request from a client.
507 * @param client the client
508 * @param m the cancel message
511 handle_client_cancel (void *cls,
512 struct GNUNET_SERVER_Client *client,
513 const struct GNUNET_MessageHeader *m)
515 /* FIXME: implement */
516 GNUNET_SERVER_receive_done (client, GNUNET_OK);
521 * Handle an ack from a client.
524 * @param client the client
525 * @param m the message
528 handle_client_ack (void *cls,
529 struct GNUNET_SERVER_Client *client,
530 const struct GNUNET_MessageHeader *m)
532 /* FIXME: implement */
533 GNUNET_SERVER_receive_done (client, GNUNET_OK);
538 * Handle a request from the client to accept
539 * a set operation that came from a remote peer.
542 * @param client the client
543 * @param mh the message
546 handle_client_accept (void *cls,
547 struct GNUNET_SERVER_Client *client,
548 const struct GNUNET_MessageHeader *mh)
551 struct Incoming *incoming;
552 struct AcceptMessage *msg = (struct AcceptMessage *) mh;
555 incoming = get_incoming (ntohl (msg->accept_id));
557 if (NULL == incoming)
560 GNUNET_SERVER_client_disconnect (client);
564 if (0 == ntohl (msg->request_id))
566 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "peer request rejected by client\n");
567 destroy_incoming (incoming);
568 GNUNET_SERVER_receive_done (client, GNUNET_OK);
572 set = get_set (client);
577 GNUNET_SERVER_client_disconnect (client);
581 switch (set->operation)
583 case GNUNET_SET_OPERATION_INTERSECTION:
587 case GNUNET_SET_OPERATION_UNION:
588 _GSS_union_accept (msg, set, incoming);
595 /* note: _GSS_*_accept has to make sure the socket and mq are set to NULL,
596 * otherwise they will be destroyed and disconnected */
597 destroy_incoming (incoming);
598 GNUNET_SERVER_receive_done (client, GNUNET_OK);
603 * Functions of this type are called upon new stream connection from other peers
604 * or upon binding error which happen when the app_port given in
605 * GNUNET_STREAM_listen() is already taken.
607 * @param cls the closure from GNUNET_STREAM_listen
608 * @param socket the socket representing the stream; NULL on binding error
609 * @param initiator the identity of the peer who wants to establish a stream
610 * with us; NULL on binding error
611 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
612 * stream (the socket will be invalid after the call)
615 stream_listen_cb (void *cls,
616 struct GNUNET_STREAM_Socket *socket,
617 const struct GNUNET_PeerIdentity *initiator)
619 struct Incoming *incoming;
620 static const struct GNUNET_MQ_Handler handlers[] = {
621 {handle_p2p_operation_request, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST},
622 GNUNET_MQ_HANDLERS_END
628 return GNUNET_SYSERR;
631 incoming = GNUNET_new (struct Incoming);
632 incoming->peer = *initiator;
633 incoming->socket = socket;
634 incoming->mq = GNUNET_MQ_queue_for_stream_socket (incoming->socket, handlers, incoming);
635 /* FIXME: timeout for peers that only connect but don't send anything */
636 GNUNET_CONTAINER_DLL_insert_tail (incoming_head, incoming_tail, incoming);
642 * Called to clean up, after a shutdown has been requested.
645 * @param tc context information (why was this task triggered now)
648 shutdown_task (void *cls,
649 const struct GNUNET_SCHEDULER_TaskContext *tc)
651 if (NULL != stream_listen_socket)
653 GNUNET_STREAM_listen_close (stream_listen_socket);
654 stream_listen_socket = NULL;
657 while (NULL != incoming_head)
659 destroy_incoming (incoming_head);
662 while (NULL != listeners_head)
664 destroy_listener (listeners_head);
667 while (NULL != sets_head)
669 destroy_set (sets_head);
672 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "handled shutdown request\n");
677 * Function called by the service's run
678 * method to run service-specific setup code.
681 * @param server the initialized server
682 * @param cfg configuration to use
685 run (void *cls, struct GNUNET_SERVER_Handle *server,
686 const struct GNUNET_CONFIGURATION_Handle *cfg)
688 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
689 {handle_client_create, NULL, GNUNET_MESSAGE_TYPE_SET_CREATE, 0},
690 {handle_client_listen, NULL, GNUNET_MESSAGE_TYPE_SET_LISTEN, 0},
691 {handle_client_add, NULL, GNUNET_MESSAGE_TYPE_SET_ADD, 0},
692 {handle_client_remove, NULL, GNUNET_MESSAGE_TYPE_SET_REMOVE, 0},
693 {handle_client_cancel, NULL, GNUNET_MESSAGE_TYPE_SET_CANCEL, 0},
694 {handle_client_evaluate, NULL, GNUNET_MESSAGE_TYPE_SET_EVALUATE, 0},
695 {handle_client_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ACK, 0},
696 {handle_client_accept, NULL, GNUNET_MESSAGE_TYPE_SET_ACCEPT, 0},
701 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL);
702 GNUNET_SERVER_disconnect_notify (server, handle_client_disconnect, NULL);
703 GNUNET_SERVER_add_handlers (server, server_handlers);
704 stream_listen_socket = GNUNET_STREAM_listen (cfg, GNUNET_APPLICATION_TYPE_SET,
705 &stream_listen_cb, NULL,
706 GNUNET_STREAM_OPTION_END);
711 * The main function for the set service.
713 * @param argc number of arguments from the command line
714 * @param argv command line arguments
715 * @return 0 ok, 1 on error
718 main (int argc, char *const *argv)
721 ret = GNUNET_SERVICE_run (argc, argv, "set", GNUNET_SERVICE_OPTION_NONE, &run, NULL);
722 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "exit\n");
723 return (GNUNET_OK == ret) ? 0 : 1;