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
26 #include "gnunet-service-set.h"
27 #include "set_protocol.h"
31 * Configuration of our local peer.
32 * (Not declared 'static' as also needed in gnunet-service-set_union.c)
34 const struct GNUNET_CONFIGURATION_Handle *configuration;
37 * Handle to the mesh service, used
38 * to listen for and connect to remote peers.
39 * (Not declared 'static' as also needed in gnunet-service-set_union.c)
41 struct GNUNET_MESH_Handle *mesh;
44 * Sets are held in a doubly linked list.
46 static struct Set *sets_head;
49 * Sets are held in a doubly linked list.
51 static struct Set *sets_tail;
54 * Listeners are held in a doubly linked list.
56 static struct Listener *listeners_head;
59 * Listeners are held in a doubly linked list.
61 static struct Listener *listeners_tail;
64 * Incoming sockets from remote peers are
65 * held in a doubly linked list.
67 static struct Incoming *incoming_head;
70 * Incoming sockets from remote peers are
71 * held in a doubly linked list.
73 static struct Incoming *incoming_tail;
76 * Counter for allocating unique IDs for clients,
77 * used to identify incoming operation requests from remote peers,
78 * that the client can choose to accept or refuse.
80 static uint32_t accept_id = 1;
84 * Get set that is owned by the given client, if any.
86 * @param client client to look for
87 * @return set that the client owns, NULL if the client
91 set_get (struct GNUNET_SERVER_Client *client)
94 for (set = sets_head; NULL != set; set = set->next)
95 if (set->client == client)
102 * Get the listener associated to a client, if any.
104 * @param client the client
105 * @return listener associated with the client, NULL
108 static struct Listener *
109 get_listener (struct GNUNET_SERVER_Client *client)
111 struct Listener *listener;
112 for (listener = listeners_head; NULL != listener; listener = listener->next)
113 if (listener->client == client)
120 * Get the incoming socket associated with the given id.
122 * @param id id to look for
123 * @return the incoming socket associated with the id,
124 * or NULL if there is none
126 static struct Incoming *
127 get_incoming (uint32_t id)
129 struct Incoming *incoming;
130 for (incoming = incoming_head; NULL != incoming; incoming = incoming->next)
131 if (incoming->accept_id == id)
138 * Destroy a listener, free all resources associated with it.
140 * @param listener listener to destroy
143 listener_destroy (struct Listener *listener)
145 if (NULL != listener->client_mq)
147 GNUNET_MQ_destroy (listener->client_mq);
148 listener->client_mq = NULL;
150 GNUNET_CONTAINER_DLL_remove (listeners_head, listeners_tail, listener);
151 GNUNET_free (listener);
156 * Destroy a set, and free all resources associated with it.
158 * @param set the set to destroy
161 set_destroy (struct Set *set)
163 switch (set->operation)
165 case GNUNET_SET_OPERATION_INTERSECTION:
168 case GNUNET_SET_OPERATION_UNION:
169 _GSS_union_set_destroy (set);
175 GNUNET_CONTAINER_DLL_remove (sets_head, sets_tail, set);
181 * Clean up after a client after it is
182 * disconnected (either by us or by itself)
184 * @param cls closure, unused
185 * @param client the client to clean up after
188 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
191 struct Listener *listener;
193 set = set_get (client);
196 listener = get_listener (client);
197 if (NULL != listener)
198 listener_destroy (listener);
203 * Destroy an incoming request from a remote peer
205 * @param incoming remote request to destroy
208 incoming_destroy (struct Incoming *incoming)
210 if (NULL != incoming->tc)
212 GNUNET_free (incoming->tc);
213 GNUNET_assert (NULL != incoming->tc->tunnel);
214 GNUNET_MESH_tunnel_destroy (incoming->tc->tunnel);
217 GNUNET_CONTAINER_DLL_remove (incoming_head, incoming_tail, incoming);
218 GNUNET_free (incoming);
222 static struct Listener *
223 get_listener_by_target (enum GNUNET_SET_OperationType op,
224 const struct GNUNET_HashCode *app_id)
228 for (l = listeners_head; NULL != l; l = l->next)
230 if (l->operation != op)
232 if (0 != GNUNET_CRYPTO_hash_cmp (app_id, &l->app_id))
242 tunnel_context_destroy (struct TunnelContext *tc)
245 /* FIXME destroy the rest */
250 * Handle a request for a set operation from
253 * @param cls the incoming socket
254 * @param tunnel the tunnel that sent the message
255 * @param tunnel_ctx the tunnel context
256 * @param mh the message
259 handle_p2p_operation_request (void *cls,
260 struct GNUNET_MESH_Tunnel *tunnel,
262 const struct GNUNET_MessageHeader *mh)
264 struct TunnelContext *tc = *tunnel_ctx;
265 struct Incoming *incoming;
266 const struct OperationRequestMessage *msg = (const struct OperationRequestMessage *) mh;
267 struct GNUNET_MQ_Envelope *mqm;
268 struct GNUNET_SET_RequestMessage *cmsg;
269 struct Listener *listener;
270 const struct GNUNET_MessageHeader *context_msg;
272 if (CONTEXT_INCOMING != tc->type)
274 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "unexpected operation request\n");
275 tunnel_context_destroy (tc);
276 /* don't kill the whole mesh connection */
282 context_msg = GNUNET_MQ_extract_nested_mh (msg);
283 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "received P2P operation request (op %u, app %s)\n",
284 ntohs (msg->operation), GNUNET_h2s (&msg->app_id));
285 listener = get_listener_by_target (ntohs (msg->operation), &msg->app_id);
286 if (NULL == listener)
288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
289 "set operation request from peer failed: "
290 "no set with matching application ID and operation type\n");
291 tunnel_context_destroy (tc);
292 /* don't kill the whole mesh connection */
295 mqm = GNUNET_MQ_msg_nested_mh (cmsg, GNUNET_MESSAGE_TYPE_SET_REQUEST, context_msg);
298 /* FIXME: disconnect the peer */
300 tunnel_context_destroy (tc);
301 /* don't kill the whole mesh connection */
304 incoming->accept_id = accept_id++;
305 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "sending request with accept id %u\n", incoming->accept_id);
306 cmsg->accept_id = htonl (incoming->accept_id);
307 cmsg->peer_id = incoming->tc->peer;
308 GNUNET_MQ_send (listener->client_mq, mqm);
315 * Called when a client wants to create a new set.
318 * @param client client that sent the message
319 * @param m message sent by the client
322 handle_client_create (void *cls,
323 struct GNUNET_SERVER_Client *client,
324 const struct GNUNET_MessageHeader *m)
326 struct GNUNET_SET_CreateMessage *msg = (struct GNUNET_SET_CreateMessage *) m;
329 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "client created new set (operation %u)\n",
330 ntohs (msg->operation));
332 if (NULL != set_get (client))
335 GNUNET_SERVER_client_disconnect (client);
341 switch (ntohs (msg->operation))
343 case GNUNET_SET_OPERATION_INTERSECTION:
344 //set = _GSS_intersection_set_create ();
346 case GNUNET_SET_OPERATION_UNION:
347 set = _GSS_union_set_create ();
351 GNUNET_SERVER_client_disconnect (client);
355 GNUNET_assert (NULL != set);
357 set->client = client;
358 set->client_mq = GNUNET_MQ_queue_for_server_client (client);
359 GNUNET_CONTAINER_DLL_insert (sets_head, sets_tail, set);
360 GNUNET_SERVER_receive_done (client, GNUNET_OK);
365 * Called when a client wants to create a new listener.
368 * @param client client that sent the message
369 * @param m message sent by the client
372 handle_client_listen (void *cls,
373 struct GNUNET_SERVER_Client *client,
374 const struct GNUNET_MessageHeader *m)
376 struct GNUNET_SET_ListenMessage *msg = (struct GNUNET_SET_ListenMessage *) m;
377 struct Listener *listener;
379 if (NULL != get_listener (client))
382 GNUNET_SERVER_client_disconnect (client);
385 listener = GNUNET_new (struct Listener);
386 listener->client = client;
387 listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
388 listener->app_id = msg->app_id;
389 listener->operation = ntohs (msg->operation);
390 GNUNET_CONTAINER_DLL_insert_tail (listeners_head, listeners_tail, listener);
391 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "new listener created (op %u, app %s)\n",
392 listener->operation, GNUNET_h2s (&listener->app_id));
393 GNUNET_SERVER_receive_done (client, GNUNET_OK);
398 * Called when a client wants to remove an element
399 * from the set it inhabits.
402 * @param client client that sent the message
403 * @param m message sent by the client
406 handle_client_remove (void *cls,
407 struct GNUNET_SERVER_Client *client,
408 const struct GNUNET_MessageHeader *m)
412 set = set_get (client);
416 GNUNET_SERVER_client_disconnect (client);
419 switch (set->operation)
421 case GNUNET_SET_OPERATION_UNION:
422 _GSS_union_remove ((struct GNUNET_SET_ElementMessage *) m, set);
424 case GNUNET_SET_OPERATION_INTERSECTION:
425 //_GSS_intersection_remove ((struct GNUNET_SET_ElementMessage *) m, set);
432 GNUNET_SERVER_receive_done (client, GNUNET_OK);
438 * Called when the client wants to reject an operation
439 * request from another peer.
442 * @param client client that sent the message
443 * @param m message sent by the client
446 handle_client_reject (void *cls,
447 struct GNUNET_SERVER_Client *client,
448 const struct GNUNET_MessageHeader *m)
450 struct Incoming *incoming;
451 struct GNUNET_SET_AcceptRejectMessage *msg = (struct GNUNET_SET_AcceptRejectMessage *) m;
453 GNUNET_break (0 == ntohl (msg->request_id));
455 incoming = get_incoming (ntohl (msg->accept_reject_id));
456 if (NULL == incoming)
458 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
461 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "peer request rejected by client\n");
462 incoming_destroy (incoming);
463 GNUNET_SERVER_receive_done (client, GNUNET_OK);
469 * Called when a client wants to add an element to a
473 * @param client client that sent the message
474 * @param m message sent by the client
477 handle_client_add (void *cls,
478 struct GNUNET_SERVER_Client *client,
479 const struct GNUNET_MessageHeader *m)
483 set = set_get (client);
487 GNUNET_SERVER_client_disconnect (client);
490 switch (set->operation)
492 case GNUNET_SET_OPERATION_UNION:
493 _GSS_union_add ((struct GNUNET_SET_ElementMessage *) m, set);
495 case GNUNET_SET_OPERATION_INTERSECTION:
496 //_GSS_intersection_add ((struct GNUNET_SET_ElementMessage *) m, set);
503 GNUNET_SERVER_receive_done (client, GNUNET_OK);
508 * Called when a client wants to evaluate a set operation with another peer.
511 * @param client client that sent the message
512 * @param m message sent by the client
515 handle_client_evaluate (void *cls,
516 struct GNUNET_SERVER_Client *client,
517 const struct GNUNET_MessageHeader *m)
521 set = set_get (client);
525 GNUNET_SERVER_client_disconnect (client);
529 switch (set->operation)
531 case GNUNET_SET_OPERATION_INTERSECTION:
532 //_GSS_intersection_evaluate ((struct GNUNET_SET_EvaluateMessage *) m, set);
534 case GNUNET_SET_OPERATION_UNION:
535 _GSS_union_evaluate ((struct GNUNET_SET_EvaluateMessage *) m, set);
542 GNUNET_SERVER_receive_done (client, GNUNET_OK);
547 * Handle an ack from a client.
550 * @param client the client
551 * @param m the message
554 handle_client_ack (void *cls,
555 struct GNUNET_SERVER_Client *client,
556 const struct GNUNET_MessageHeader *m)
558 /* FIXME: implement */
559 GNUNET_SERVER_receive_done (client, GNUNET_OK);
564 * Handle a request from the client to accept
565 * a set operation that came from a remote peer.
568 * @param client the client
569 * @param mh the message
572 handle_client_accept (void *cls,
573 struct GNUNET_SERVER_Client *client,
574 const struct GNUNET_MessageHeader *mh)
577 struct Incoming *incoming;
578 struct GNUNET_SET_AcceptRejectMessage *msg = (struct GNUNET_SET_AcceptRejectMessage *) mh;
580 incoming = get_incoming (ntohl (msg->accept_reject_id));
582 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "client accepting %u\n", ntohl (msg->accept_reject_id));
584 if (NULL == incoming)
588 GNUNET_SERVER_client_disconnect (client);
592 set = set_get (client);
597 GNUNET_SERVER_client_disconnect (client);
601 switch (set->operation)
603 case GNUNET_SET_OPERATION_INTERSECTION:
604 // _GSS_intersection_accept (msg, set, incoming);
606 case GNUNET_SET_OPERATION_UNION:
607 _GSS_union_accept (msg, set, incoming);
614 /* note: _GSS_*_accept has to make sure the socket and mq are set to NULL,
615 * otherwise they will be destroyed and disconnected */
616 incoming_destroy (incoming);
617 GNUNET_SERVER_receive_done (client, GNUNET_OK);
622 * Called to clean up, after a shutdown has been requested.
625 * @param tc context information (why was this task triggered now)
628 shutdown_task (void *cls,
629 const struct GNUNET_SCHEDULER_TaskContext *tc)
633 GNUNET_MESH_disconnect (mesh);
637 while (NULL != incoming_head)
639 incoming_destroy (incoming_head);
642 while (NULL != listeners_head)
644 listener_destroy (listeners_head);
647 while (NULL != sets_head)
649 set_destroy (sets_head);
652 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "handled shutdown request\n");
658 * Signature of the main function of a task.
661 * @param tc context information (why was this task triggered now)
664 incoming_timeout_cb (void *cls,
665 const struct GNUNET_SCHEDULER_TaskContext *tc)
667 struct Incoming *incoming = cls;
669 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "remote peer timed out\n");
670 incoming_destroy (incoming);
675 * Method called whenever another peer has added us to a tunnel
676 * the other peer initiated.
677 * Only called (once) upon reception of data with a message type which was
678 * subscribed to in GNUNET_MESH_connect. A call to GNUNET_MESH_tunnel_destroy
679 * causes te tunnel to be ignored and no further notifications are sent about
683 * @param tunnel new handle to the tunnel
684 * @param initiator peer that started the tunnel
685 * @param port Port this tunnel is for.
686 * @return initial tunnel context for the tunnel
687 * (can be NULL -- that's not an error)
690 tunnel_new_cb (void *cls,
691 struct GNUNET_MESH_Tunnel *tunnel,
692 const struct GNUNET_PeerIdentity *initiator,
695 struct Incoming *incoming;
696 struct TunnelContext *tc;
698 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "new incoming tunnel\n");
700 GNUNET_assert (port == GNUNET_APPLICATION_TYPE_SET);
701 tc = GNUNET_new (struct TunnelContext);
702 incoming = GNUNET_new (struct Incoming);
704 tc->peer = *initiator;
706 tc->mq = GNUNET_MESH_mq_create (tunnel);
708 tc->type = CONTEXT_INCOMING;
709 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, incoming_timeout_cb, incoming);
710 GNUNET_CONTAINER_DLL_insert_tail (incoming_head, incoming_tail, incoming);
717 * Function called whenever a tunnel is destroyed. Should clean up
718 * any associated state.
719 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
722 * @param cls closure (set from GNUNET_MESH_connect)
723 * @param tunnel connection to the other end (henceforth invalid)
724 * @param tunnel_ctx place where local state associated
725 * with the tunnel is stored
728 tunnel_end_cb (void *cls,
729 const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
731 struct TunnelContext *ctx = tunnel_ctx;
735 case CONTEXT_INCOMING:
736 incoming_destroy ((struct Incoming *) ctx->data);
738 case CONTEXT_OPERATION_UNION:
739 _GSS_union_operation_destroy ((struct UnionEvaluateOperation *) ctx->data);
741 case CONTEXT_OPERATION_INTERSECTION:
753 * Function called by the service's run
754 * method to run service-specific setup code.
757 * @param server the initialized server
758 * @param cfg configuration to use
761 run (void *cls, struct GNUNET_SERVER_Handle *server,
762 const struct GNUNET_CONFIGURATION_Handle *cfg)
764 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
765 {handle_client_accept, NULL, GNUNET_MESSAGE_TYPE_SET_ACCEPT, 0},
766 {handle_client_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ACK, 0},
767 {handle_client_add, NULL, GNUNET_MESSAGE_TYPE_SET_ADD, 0},
768 {handle_client_create, NULL, GNUNET_MESSAGE_TYPE_SET_CREATE, 0},
769 {handle_client_evaluate, NULL, GNUNET_MESSAGE_TYPE_SET_EVALUATE, 0},
770 {handle_client_listen, NULL, GNUNET_MESSAGE_TYPE_SET_LISTEN, 0},
771 {handle_client_reject, NULL, GNUNET_MESSAGE_TYPE_SET_REJECT, 0},
772 {handle_client_remove, NULL, GNUNET_MESSAGE_TYPE_SET_REMOVE, 0},
775 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
776 {handle_p2p_operation_request,
777 GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST, 0},
778 /* messages for the union operation */
779 {_GSS_union_handle_p2p_message,
780 GNUNET_MESSAGE_TYPE_SET_P2P_IBF, 0},
781 {_GSS_union_handle_p2p_message,
782 GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS, 0},
783 {_GSS_union_handle_p2p_message,
784 GNUNET_MESSAGE_TYPE_SET_P2P_DONE, 0},
785 {_GSS_union_handle_p2p_message,
786 GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS, 0},
787 {_GSS_union_handle_p2p_message,
788 GNUNET_MESSAGE_TYPE_SET_P2P_SE, 0},
789 /* FIXME: messages for intersection operation */
792 static const uint32_t mesh_ports[] = {GNUNET_APPLICATION_TYPE_SET, 0};
795 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
796 &shutdown_task, NULL);
797 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
798 GNUNET_SERVER_add_handlers (server, server_handlers);
800 mesh = GNUNET_MESH_connect (cfg, NULL, tunnel_new_cb, tunnel_end_cb,
801 mesh_handlers, mesh_ports);
804 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not connect to mesh\n");
808 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "service started\n");
813 * The main function for the set service.
815 * @param argc number of arguments from the command line
816 * @param argv command line arguments
817 * @return 0 ok, 1 on error
820 main (int argc, char *const *argv)
823 ret = GNUNET_SERVICE_run (argc, argv, "set",
824 GNUNET_SERVICE_OPTION_NONE, &run, NULL);
825 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "exit\n");
826 return (GNUNET_OK == ret) ? 0 : 1;