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)
95 for (set = sets_head; NULL != set; set = set->next)
96 if (set->client == client)
103 * Get the listener associated with the given client, if any.
105 * @param client the client
106 * @return listener associated with the client, NULL
109 static struct Listener *
110 listener_get (struct GNUNET_SERVER_Client *client)
112 struct Listener *listener;
114 for (listener = listeners_head; NULL != listener; listener = listener->next)
115 if (listener->client == client)
122 * Get the incoming socket associated with the given id.
124 * @param id id to look for
125 * @return the incoming socket associated with the id,
126 * or NULL if there is none
128 static struct Incoming *
129 get_incoming (uint32_t id)
131 struct Incoming *incoming;
133 for (incoming = incoming_head; NULL != incoming; incoming = incoming->next)
134 if (incoming->accept_id == id)
141 * Destroy a listener, free all resources associated with it.
143 * @param listener listener to destroy
146 listener_destroy (struct Listener *listener)
148 if (NULL != listener->client_mq)
150 GNUNET_MQ_destroy (listener->client_mq);
151 listener->client_mq = NULL;
153 GNUNET_CONTAINER_DLL_remove (listeners_head, listeners_tail, listener);
154 GNUNET_free (listener);
159 * Destroy a set, and free all resources associated with it.
161 * @param set the set to destroy
164 set_destroy (struct Set *set)
166 switch (set->operation)
168 case GNUNET_SET_OPERATION_INTERSECTION:
171 case GNUNET_SET_OPERATION_UNION:
172 _GSS_union_set_destroy (set);
178 GNUNET_CONTAINER_DLL_remove (sets_head, sets_tail, set);
184 * Clean up after a client after it is
185 * disconnected (either by us or by itself)
187 * @param cls closure, unused
188 * @param client the client to clean up after
191 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
194 struct Listener *listener;
196 set = set_get (client);
199 listener = listener_get (client);
200 if (NULL != listener)
201 listener_destroy (listener);
206 * Destroy an incoming request from a remote peer
208 * @param incoming remote request to destroy
211 incoming_destroy (struct Incoming *incoming)
213 GNUNET_CONTAINER_DLL_remove (incoming_head, incoming_tail, incoming);
214 GNUNET_free (incoming);
218 static struct Listener *
219 listener_get_by_target (enum GNUNET_SET_OperationType op,
220 const struct GNUNET_HashCode *app_id)
224 for (l = listeners_head; NULL != l; l = l->next)
226 if (l->operation != op)
228 if (0 != GNUNET_CRYPTO_hash_cmp (app_id, &l->app_id))
237 * Suggest the given request to the listener,
238 * who can accept or reject the request.
240 * @param incoming the incoming peer with the request to suggest
241 * @param listener the listener to suggest the request to
244 incoming_suggest (struct Incoming *incoming, struct Listener *listener)
246 struct GNUNET_MQ_Envelope *mqm;
247 struct GNUNET_SET_RequestMessage *cmsg;
249 GNUNET_assert (GNUNET_NO == incoming->suggested);
250 incoming->suggested = GNUNET_YES;
252 GNUNET_SCHEDULER_cancel (incoming->timeout_task);
253 mqm = GNUNET_MQ_msg_nested_mh (cmsg, GNUNET_MESSAGE_TYPE_SET_REQUEST,
254 incoming->context_msg);
255 GNUNET_assert (NULL != mqm);
256 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "suggesting request with accept id %u\n", incoming->accept_id);
257 cmsg->accept_id = htonl (incoming->accept_id);
258 cmsg->peer_id = incoming->tc->peer;
259 GNUNET_MQ_send (listener->client_mq, mqm);
265 * Handle a request for a set operation from
268 * @param cls the incoming socket
269 * @param tunnel the tunnel that sent the message
270 * @param tunnel_ctx the tunnel context
271 * @param mh the message
274 handle_p2p_operation_request (void *cls,
275 struct GNUNET_MESH_Tunnel *tunnel,
277 const struct GNUNET_MessageHeader *mh)
279 struct TunnelContext *tc = *tunnel_ctx;
280 struct Incoming *incoming;
281 const struct OperationRequestMessage *msg = (const struct OperationRequestMessage *) mh;
282 struct Listener *listener;
284 if (CONTEXT_INCOMING != tc->type)
286 /* unexpected request */
288 /* kill the tunnel, cleaner will be called */
289 return GNUNET_SYSERR;
294 if (GNUNET_YES == incoming->received_request)
296 /* double operation request */
298 return GNUNET_SYSERR;
301 incoming->accept_id = accept_id++;
302 incoming->context_msg =
303 GNUNET_copy_message (GNUNET_MQ_extract_nested_mh (msg));
305 if ( (NULL != incoming->context_msg) &&
306 (ntohs (incoming->context_msg->size) > GNUNET_SET_CONTEXT_MESSAGE_MAX_SIZE) )
309 return GNUNET_SYSERR;
312 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "received P2P operation request (op %u, app %s)\n",
313 ntohs (msg->operation), GNUNET_h2s (&msg->app_id));
314 listener = listener_get_by_target (ntohs (msg->operation), &msg->app_id);
315 if (NULL == listener)
317 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
318 "no listener matches incoming request, waiting with timeout\n");
321 incoming_suggest (incoming, listener);
327 * Called when a client wants to create a new set.
330 * @param client client that sent the message
331 * @param m message sent by the client
334 handle_client_create (void *cls,
335 struct GNUNET_SERVER_Client *client,
336 const struct GNUNET_MessageHeader *m)
338 struct GNUNET_SET_CreateMessage *msg = (struct GNUNET_SET_CreateMessage *) m;
341 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "client created new set (operation %u)\n",
342 ntohs (msg->operation));
344 if (NULL != set_get (client))
347 GNUNET_SERVER_client_disconnect (client);
353 switch (ntohs (msg->operation))
355 case GNUNET_SET_OPERATION_INTERSECTION:
356 //set = _GSS_intersection_set_create ();
358 case GNUNET_SET_OPERATION_UNION:
359 set = _GSS_union_set_create ();
363 GNUNET_SERVER_client_disconnect (client);
367 GNUNET_assert (NULL != set);
369 set->client = client;
370 set->client_mq = GNUNET_MQ_queue_for_server_client (client);
371 GNUNET_CONTAINER_DLL_insert (sets_head, sets_tail, set);
372 GNUNET_SERVER_receive_done (client, GNUNET_OK);
377 * Called when a client wants to create a new listener.
380 * @param client client that sent the message
381 * @param m message sent by the client
384 handle_client_listen (void *cls,
385 struct GNUNET_SERVER_Client *client,
386 const struct GNUNET_MessageHeader *m)
388 struct GNUNET_SET_ListenMessage *msg = (struct GNUNET_SET_ListenMessage *) m;
389 struct Listener *listener;
390 struct Incoming *incoming;
392 if (NULL != listener_get (client))
395 GNUNET_SERVER_client_disconnect (client);
398 listener = GNUNET_new (struct Listener);
399 listener->client = client;
400 listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
401 listener->app_id = msg->app_id;
402 listener->operation = ntohs (msg->operation);
403 GNUNET_CONTAINER_DLL_insert_tail (listeners_head, listeners_tail, listener);
404 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "new listener created (op %u, app %s)\n",
405 listener->operation, GNUNET_h2s (&listener->app_id));
406 for (incoming = incoming_head; NULL != incoming; incoming = incoming->next)
408 if ( (GNUNET_NO == incoming->received_request) ||
409 (GNUNET_YES == incoming->suggested) )
411 if (listener->operation != incoming->operation)
413 if (0 != GNUNET_CRYPTO_hash_cmp (&listener->app_id, &incoming->app_id))
415 incoming_suggest (incoming, listener);
417 GNUNET_SERVER_receive_done (client, GNUNET_OK);
422 * Called when a client wants to remove an element
423 * from the set it inhabits.
426 * @param client client that sent the message
427 * @param m message sent by the client
430 handle_client_remove (void *cls,
431 struct GNUNET_SERVER_Client *client,
432 const struct GNUNET_MessageHeader *m)
436 set = set_get (client);
440 GNUNET_SERVER_client_disconnect (client);
443 switch (set->operation)
445 case GNUNET_SET_OPERATION_UNION:
446 _GSS_union_remove ((struct GNUNET_SET_ElementMessage *) m, set);
448 case GNUNET_SET_OPERATION_INTERSECTION:
449 //_GSS_intersection_remove ((struct GNUNET_SET_ElementMessage *) m, set);
456 GNUNET_SERVER_receive_done (client, GNUNET_OK);
462 * Called when the client wants to reject an operation
463 * request from another peer.
466 * @param client client that sent the message
467 * @param m message sent by the client
470 handle_client_reject (void *cls,
471 struct GNUNET_SERVER_Client *client,
472 const struct GNUNET_MessageHeader *m)
474 struct Incoming *incoming;
475 struct GNUNET_SET_AcceptRejectMessage *msg = (struct GNUNET_SET_AcceptRejectMessage *) m;
477 GNUNET_break (0 == ntohl (msg->request_id));
479 incoming = get_incoming (ntohl (msg->accept_reject_id));
480 if (NULL == incoming)
482 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
485 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "peer request rejected by client\n");
486 /* the incoming peer will be destroyed in the tunnel end handler */
487 GNUNET_MESH_tunnel_destroy (incoming->tc->tunnel);
488 GNUNET_SERVER_receive_done (client, GNUNET_OK);
494 * Called when a client wants to add an element to a
498 * @param client client that sent the message
499 * @param m message sent by the client
502 handle_client_add (void *cls,
503 struct GNUNET_SERVER_Client *client,
504 const struct GNUNET_MessageHeader *m)
508 set = set_get (client);
512 GNUNET_SERVER_client_disconnect (client);
515 switch (set->operation)
517 case GNUNET_SET_OPERATION_UNION:
518 _GSS_union_add ((struct GNUNET_SET_ElementMessage *) m, set);
520 case GNUNET_SET_OPERATION_INTERSECTION:
521 //_GSS_intersection_add ((struct GNUNET_SET_ElementMessage *) m, set);
528 GNUNET_SERVER_receive_done (client, GNUNET_OK);
533 * Called when a client wants to evaluate a set operation with another peer.
536 * @param client client that sent the message
537 * @param m message sent by the client
540 handle_client_evaluate (void *cls,
541 struct GNUNET_SERVER_Client *client,
542 const struct GNUNET_MessageHeader *m)
546 set = set_get (client);
550 GNUNET_SERVER_client_disconnect (client);
554 switch (set->operation)
556 case GNUNET_SET_OPERATION_INTERSECTION:
557 //_GSS_intersection_evaluate ((struct GNUNET_SET_EvaluateMessage *) m, set);
559 case GNUNET_SET_OPERATION_UNION:
560 _GSS_union_evaluate ((struct GNUNET_SET_EvaluateMessage *) m, set);
567 GNUNET_SERVER_receive_done (client, GNUNET_OK);
572 * Handle an ack from a client.
575 * @param client the client
576 * @param m the message
579 handle_client_ack (void *cls,
580 struct GNUNET_SERVER_Client *client,
581 const struct GNUNET_MessageHeader *m)
583 /* FIXME: implement */
584 GNUNET_SERVER_receive_done (client, GNUNET_OK);
589 * Handle a request from the client to accept
590 * a set operation that came from a remote peer.
593 * @param client the client
594 * @param mh the message
597 handle_client_accept (void *cls,
598 struct GNUNET_SERVER_Client *client,
599 const struct GNUNET_MessageHeader *mh)
602 struct Incoming *incoming;
603 struct GNUNET_SET_AcceptRejectMessage *msg = (struct GNUNET_SET_AcceptRejectMessage *) mh;
605 incoming = get_incoming (ntohl (msg->accept_reject_id));
607 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "client accepting %u\n", ntohl (msg->accept_reject_id));
609 if (NULL == incoming)
613 GNUNET_SERVER_client_disconnect (client);
617 set = set_get (client);
622 GNUNET_SERVER_client_disconnect (client);
626 switch (set->operation)
628 case GNUNET_SET_OPERATION_INTERSECTION:
629 // _GSS_intersection_accept (msg, set, incoming);
631 case GNUNET_SET_OPERATION_UNION:
632 _GSS_union_accept (msg, set, incoming);
639 /* note: _GSS_*_accept has to make sure the tunnel and mq are set to NULL,
640 * otherwise they will be destroyed and disconnected */
641 incoming_destroy (incoming);
642 GNUNET_SERVER_receive_done (client, GNUNET_OK);
647 * Called to clean up, after a shutdown has been requested.
650 * @param tc context information (why was this task triggered now)
653 shutdown_task (void *cls,
654 const struct GNUNET_SCHEDULER_TaskContext *tc)
658 GNUNET_MESH_disconnect (mesh);
662 while (NULL != incoming_head)
663 incoming_destroy (incoming_head);
665 while (NULL != listeners_head)
666 listener_destroy (listeners_head);
668 while (NULL != sets_head)
669 set_destroy (sets_head);
671 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "handled shutdown request\n");
677 * Signature of the main function of a task.
680 * @param tc context information (why was this task triggered now)
683 incoming_timeout_cb (void *cls,
684 const struct GNUNET_SCHEDULER_TaskContext *tc)
686 struct Incoming *incoming = cls;
688 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "remote peer timed out\n");
689 incoming_destroy (incoming);
694 * Method called whenever another peer has added us to a tunnel
695 * the other peer initiated.
696 * Only called (once) upon reception of data with a message type which was
697 * subscribed to in GNUNET_MESH_connect. A call to GNUNET_MESH_tunnel_destroy
698 * causes te tunnel to be ignored and no further notifications are sent about
702 * @param tunnel new handle to the tunnel
703 * @param initiator peer that started the tunnel
704 * @param port Port this tunnel is for.
705 * @return initial tunnel context for the tunnel
706 * (can be NULL -- that's not an error)
709 tunnel_new_cb (void *cls,
710 struct GNUNET_MESH_Tunnel *tunnel,
711 const struct GNUNET_PeerIdentity *initiator,
714 struct Incoming *incoming;
715 struct TunnelContext *tc;
717 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "new incoming tunnel\n");
719 GNUNET_assert (port == GNUNET_APPLICATION_TYPE_SET);
720 tc = GNUNET_new (struct TunnelContext);
721 incoming = GNUNET_new (struct Incoming);
723 tc->peer = *initiator;
725 tc->mq = GNUNET_MESH_mq_create (tunnel);
727 tc->type = CONTEXT_INCOMING;
728 incoming->timeout_task =
729 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, incoming_timeout_cb, incoming);
730 GNUNET_CONTAINER_DLL_insert_tail (incoming_head, incoming_tail, incoming);
737 * Function called whenever a tunnel is destroyed. Should clean up
738 * any associated state.
739 * GNUNET_MESH_tunnel_destroy. It must NOT call GNUNET_MESH_tunnel_destroy on
742 * @param cls closure (set from GNUNET_MESH_connect)
743 * @param tunnel connection to the other end (henceforth invalid)
744 * @param tunnel_ctx place where local state associated
745 * with the tunnel is stored
748 tunnel_end_cb (void *cls,
749 const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
751 struct TunnelContext *ctx = tunnel_ctx;
753 /* tunnel is dead already */
758 GNUNET_MQ_destroy (ctx->mq);
764 case CONTEXT_INCOMING:
765 incoming_destroy ((struct Incoming *) ctx->data);
767 case CONTEXT_OPERATION_UNION:
768 _GSS_union_operation_destroy ((struct UnionEvaluateOperation *) ctx->data);
770 case CONTEXT_OPERATION_INTERSECTION:
778 GNUNET_free (tunnel_ctx);
783 * Function called by the service's run
784 * method to run service-specific setup code.
787 * @param server the initialized server
788 * @param cfg configuration to use
791 run (void *cls, struct GNUNET_SERVER_Handle *server,
792 const struct GNUNET_CONFIGURATION_Handle *cfg)
794 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
795 {handle_client_accept, NULL, GNUNET_MESSAGE_TYPE_SET_ACCEPT, 0},
796 {handle_client_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ACK, 0},
797 {handle_client_add, NULL, GNUNET_MESSAGE_TYPE_SET_ADD, 0},
798 {handle_client_create, NULL, GNUNET_MESSAGE_TYPE_SET_CREATE, 0},
799 {handle_client_evaluate, NULL, GNUNET_MESSAGE_TYPE_SET_EVALUATE, 0},
800 {handle_client_listen, NULL, GNUNET_MESSAGE_TYPE_SET_LISTEN, 0},
801 {handle_client_reject, NULL, GNUNET_MESSAGE_TYPE_SET_REJECT, 0},
802 {handle_client_remove, NULL, GNUNET_MESSAGE_TYPE_SET_REMOVE, 0},
805 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
806 {handle_p2p_operation_request,
807 GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST, 0},
808 /* messages for the union operation */
809 {_GSS_union_handle_p2p_message,
810 GNUNET_MESSAGE_TYPE_SET_P2P_IBF, 0},
811 {_GSS_union_handle_p2p_message,
812 GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENTS, 0},
813 {_GSS_union_handle_p2p_message,
814 GNUNET_MESSAGE_TYPE_SET_P2P_DONE, 0},
815 {_GSS_union_handle_p2p_message,
816 GNUNET_MESSAGE_TYPE_SET_P2P_ELEMENT_REQUESTS, 0},
817 {_GSS_union_handle_p2p_message,
818 GNUNET_MESSAGE_TYPE_SET_P2P_SE, 0},
819 /* FIXME: messages for intersection operation */
822 static const uint32_t mesh_ports[] = {GNUNET_APPLICATION_TYPE_SET, 0};
825 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
826 &shutdown_task, NULL);
827 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
828 GNUNET_SERVER_add_handlers (server, server_handlers);
830 mesh = GNUNET_MESH_connect (cfg, NULL, tunnel_new_cb, tunnel_end_cb,
831 mesh_handlers, mesh_ports);
834 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "could not connect to mesh\n");
838 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "service started\n");
843 * The main function for the set service.
845 * @param argc number of arguments from the command line
846 * @param argv command line arguments
847 * @return 0 ok, 1 on error
850 main (int argc, char *const *argv)
853 ret = GNUNET_SERVICE_run (argc, argv, "set",
854 GNUNET_SERVICE_OPTION_NONE, &run, NULL);
855 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "exit\n");
856 return (GNUNET_OK == ret) ? 0 : 1;