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);
229 static struct Listener *
230 get_listener_by_target (enum GNUNET_SET_OperationType op,
231 const struct GNUNET_HashCode *app_id)
235 for (l = listeners_head; NULL != l; l = l->next)
237 if (l->operation != op)
239 if (0 != GNUNET_CRYPTO_hash_cmp (app_id, &l->app_id))
248 * Handle a request for a set operation from
251 * @param cls the incoming socket
252 * @param mh the message
255 handle_p2p_operation_request (void *cls, const struct GNUNET_MessageHeader *mh)
257 struct Incoming *incoming = cls;
258 const struct OperationRequestMessage *msg = (const struct OperationRequestMessage *) mh;
259 struct GNUNET_MQ_Message *mqm;
260 struct GNUNET_SET_RequestMessage *cmsg;
261 struct Listener *listener;
262 const struct GNUNET_MessageHeader *context_msg;
264 context_msg = GNUNET_MQ_extract_nested_mh (msg);
265 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "received P2P operation request (op %u, app %s)\n",
266 ntohs (msg->operation), GNUNET_h2s (&msg->app_id));
267 listener = get_listener_by_target (ntohs (msg->operation), &msg->app_id);
268 if (NULL == listener)
270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
271 "set operation request from peer failed: "
272 "no set with matching application ID and operation type\n");
275 mqm = GNUNET_MQ_msg_nested_mh (cmsg, GNUNET_MESSAGE_TYPE_SET_REQUEST, context_msg);
278 /* FIXME: disconnect the peer */
282 incoming->accept_id = accept_id++;
283 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "sending request with accept id %u\n", incoming->accept_id);
284 cmsg->accept_id = htonl (incoming->accept_id);
285 cmsg->peer_id = incoming->peer;
286 GNUNET_MQ_send (listener->client_mq, mqm);
291 * Called when a client wants to create a new set.
294 * @param client client that sent the message
295 * @param m message sent by the client
298 handle_client_create (void *cls,
299 struct GNUNET_SERVER_Client *client,
300 const struct GNUNET_MessageHeader *m)
302 struct GNUNET_SET_CreateMessage *msg = (struct GNUNET_SET_CreateMessage *) m;
305 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "client created new set (operation %u)\n",
306 ntohs (msg->operation));
308 if (NULL != get_set (client))
311 GNUNET_SERVER_client_disconnect (client);
317 switch (ntohs (msg->operation))
319 case GNUNET_SET_OPERATION_INTERSECTION:
323 case GNUNET_SET_OPERATION_UNION:
324 set = _GSS_union_set_create ();
328 GNUNET_SERVER_client_disconnect (client);
332 GNUNET_assert (NULL != set);
334 set->client = client;
335 GNUNET_SERVER_client_keep (client);
336 set->client_mq = GNUNET_MQ_queue_for_server_client (client);
337 GNUNET_CONTAINER_DLL_insert (sets_head, sets_tail, set);
338 GNUNET_SERVER_receive_done (client, GNUNET_OK);
343 * Called when a client wants to create a new listener.
346 * @param client client that sent the message
347 * @param m message sent by the client
350 handle_client_listen (void *cls,
351 struct GNUNET_SERVER_Client *client,
352 const struct GNUNET_MessageHeader *m)
354 struct GNUNET_SET_ListenMessage *msg = (struct GNUNET_SET_ListenMessage *) m;
355 struct Listener *listener;
357 if (NULL != get_listener (client))
360 GNUNET_SERVER_client_disconnect (client);
363 listener = GNUNET_new (struct Listener);
364 listener->client = client;
365 GNUNET_SERVER_client_keep (client);
366 listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
367 listener->app_id = msg->app_id;
368 listener->operation = ntohs (msg->operation);
369 GNUNET_CONTAINER_DLL_insert_tail (listeners_head, listeners_tail, listener);
370 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "new listener created (op %u, app %s)\n",
371 listener->operation, GNUNET_h2s (&listener->app_id));
372 GNUNET_SERVER_receive_done (client, GNUNET_OK);
377 * Called when a client wants to remove an element
378 * from the set it inhabits.
381 * @param client client that sent the message
382 * @param m message sent by the client
385 handle_client_remove (void *cls,
386 struct GNUNET_SERVER_Client *client,
387 const struct GNUNET_MessageHeader *m)
391 set = get_set (client);
395 GNUNET_SERVER_client_disconnect (client);
398 switch (set->operation)
400 case GNUNET_SET_OPERATION_UNION:
401 _GSS_union_remove ((struct GNUNET_SET_ElementMessage *) m, set);
402 case GNUNET_SET_OPERATION_INTERSECTION:
410 GNUNET_SERVER_receive_done (client, GNUNET_OK);
416 * Called when the client wants to reject an operation
417 * request from another peer.
420 * @param client client that sent the message
421 * @param m message sent by the client
424 handle_client_reject (void *cls,
425 struct GNUNET_SERVER_Client *client,
426 const struct GNUNET_MessageHeader *m)
428 struct Incoming *incoming;
429 struct GNUNET_SET_AcceptRejectMessage *msg = (struct GNUNET_SET_AcceptRejectMessage *) m;
431 GNUNET_break (0 == ntohl (msg->request_id));
433 incoming = get_incoming (ntohl (msg->accept_reject_id));
434 if (NULL == incoming)
436 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
439 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "peer request rejected by client\n");
440 destroy_incoming (incoming);
441 GNUNET_SERVER_receive_done (client, GNUNET_OK);
447 * Called when a client wants to add an element to a
451 * @param client client that sent the message
452 * @param m message sent by the client
455 handle_client_add (void *cls,
456 struct GNUNET_SERVER_Client *client,
457 const struct GNUNET_MessageHeader *m)
461 set = get_set (client);
465 GNUNET_SERVER_client_disconnect (client);
468 switch (set->operation)
470 case GNUNET_SET_OPERATION_UNION:
471 _GSS_union_add ((struct GNUNET_SET_ElementMessage *) m, set);
472 case GNUNET_SET_OPERATION_INTERSECTION:
480 GNUNET_SERVER_receive_done (client, GNUNET_OK);
485 * Called when a client wants to evaluate a set operation with another peer.
488 * @param client client that sent the message
489 * @param m message sent by the client
492 handle_client_evaluate (void *cls,
493 struct GNUNET_SERVER_Client *client,
494 const struct GNUNET_MessageHeader *m)
498 set = get_set (client);
502 GNUNET_SERVER_client_disconnect (client);
507 switch (set->operation)
509 case GNUNET_SET_OPERATION_INTERSECTION:
512 case GNUNET_SET_OPERATION_UNION:
513 _GSS_union_evaluate ((struct GNUNET_SET_EvaluateMessage *) m, set);
520 GNUNET_SERVER_receive_done (client, GNUNET_OK);
525 * Handle an ack from a client.
528 * @param client the client
529 * @param m the message
532 handle_client_ack (void *cls,
533 struct GNUNET_SERVER_Client *client,
534 const struct GNUNET_MessageHeader *m)
536 /* FIXME: implement */
537 GNUNET_SERVER_receive_done (client, GNUNET_OK);
542 * Handle a request from the client to accept
543 * a set operation that came from a remote peer.
546 * @param client the client
547 * @param mh the message
550 handle_client_accept (void *cls,
551 struct GNUNET_SERVER_Client *client,
552 const struct GNUNET_MessageHeader *mh)
555 struct Incoming *incoming;
556 struct GNUNET_SET_AcceptRejectMessage *msg = (struct GNUNET_SET_AcceptRejectMessage *) mh;
558 incoming = get_incoming (ntohl (msg->accept_reject_id));
560 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "client accepting %u\n", ntohl (msg->accept_reject_id));
562 if (NULL == incoming)
566 GNUNET_SERVER_client_disconnect (client);
571 set = get_set (client);
576 GNUNET_SERVER_client_disconnect (client);
580 switch (set->operation)
582 case GNUNET_SET_OPERATION_INTERSECTION:
586 case GNUNET_SET_OPERATION_UNION:
587 _GSS_union_accept (msg, set, incoming);
594 /* note: _GSS_*_accept has to make sure the socket and mq are set to NULL,
595 * otherwise they will be destroyed and disconnected */
596 destroy_incoming (incoming);
597 GNUNET_SERVER_receive_done (client, GNUNET_OK);
602 * Functions of this type are called upon new stream connection from other peers
603 * or upon binding error which happen when the app_port given in
604 * GNUNET_STREAM_listen() is already taken.
606 * @param cls the closure from GNUNET_STREAM_listen
607 * @param socket the socket representing the stream; NULL on binding error
608 * @param initiator the identity of the peer who wants to establish a stream
609 * with us; NULL on binding error
610 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
611 * stream (the socket will be invalid after the call)
614 stream_listen_cb (void *cls,
615 struct GNUNET_STREAM_Socket *socket,
616 const struct GNUNET_PeerIdentity *initiator)
618 struct Incoming *incoming;
619 static const struct GNUNET_MQ_Handler handlers[] = {
620 {handle_p2p_operation_request, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST},
621 GNUNET_MQ_HANDLERS_END
627 return GNUNET_SYSERR;
630 incoming = GNUNET_new (struct Incoming);
631 incoming->peer = *initiator;
632 incoming->socket = socket;
633 incoming->mq = GNUNET_STREAM_mq_create (incoming->socket, handlers, NULL, incoming);
634 /* FIXME: timeout for peers that only connect but don't send anything */
635 GNUNET_CONTAINER_DLL_insert_tail (incoming_head, incoming_tail, incoming);
641 * Called to clean up, after a shutdown has been requested.
644 * @param tc context information (why was this task triggered now)
647 shutdown_task (void *cls,
648 const struct GNUNET_SCHEDULER_TaskContext *tc)
650 if (NULL != stream_listen_socket)
652 GNUNET_STREAM_listen_close (stream_listen_socket);
653 stream_listen_socket = NULL;
656 while (NULL != incoming_head)
658 destroy_incoming (incoming_head);
661 while (NULL != listeners_head)
663 destroy_listener (listeners_head);
666 while (NULL != sets_head)
668 destroy_set (sets_head);
671 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "handled shutdown request\n");
676 * Function called by the service's run
677 * method to run service-specific setup code.
680 * @param server the initialized server
681 * @param cfg configuration to use
684 run (void *cls, struct GNUNET_SERVER_Handle *server,
685 const struct GNUNET_CONFIGURATION_Handle *cfg)
687 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
688 {handle_client_accept, NULL, GNUNET_MESSAGE_TYPE_SET_ACCEPT, 0},
689 {handle_client_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ACK, 0},
690 {handle_client_add, NULL, GNUNET_MESSAGE_TYPE_SET_ADD, 0},
691 {handle_client_create, NULL, GNUNET_MESSAGE_TYPE_SET_CREATE, 0},
692 {handle_client_evaluate, NULL, GNUNET_MESSAGE_TYPE_SET_EVALUATE, 0},
693 {handle_client_listen, NULL, GNUNET_MESSAGE_TYPE_SET_LISTEN, 0},
694 {handle_client_reject, NULL, GNUNET_MESSAGE_TYPE_SET_REJECT, 0},
695 {handle_client_remove, NULL, GNUNET_MESSAGE_TYPE_SET_REMOVE, 0},
700 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL);
701 GNUNET_SERVER_disconnect_notify (server, handle_client_disconnect, NULL);
702 GNUNET_SERVER_add_handlers (server, server_handlers);
703 stream_listen_socket = GNUNET_STREAM_listen (cfg, GNUNET_APPLICATION_TYPE_SET,
704 &stream_listen_cb, NULL,
705 GNUNET_STREAM_OPTION_END);
707 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "set service running\n");
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;