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 request IDs for clients.
77 static uint32_t request_id = 1;
81 * Disconnect a client and free all resources
82 * that the client allocated (e.g. Sets or Listeners)
84 * @param client the client to disconnect
87 _GSS_client_disconnect (struct GNUNET_SERVER_Client *client)
89 /* FIXME: clean up any data structures belonging to the client */
90 GNUNET_SERVER_client_disconnect (client);
95 * Get set that is owned by the client, if any.
97 * @param client client to look for
98 * @return set that the client owns, NULL if the client
102 get_set (struct GNUNET_SERVER_Client *client)
105 for (set = sets_head; NULL != set; set = set->next)
106 if (set->client == client)
113 * Get the listener associated to a client, if any.
115 * @param client the client
116 * @return listener associated with the client, NULL
119 static struct Listener *
120 get_listener (struct GNUNET_SERVER_Client *client)
122 struct Listener *listener;
123 for (listener = listeners_head; NULL != listener; listener = listener->next)
124 if (listener->client == client)
131 * Get the incoming socket associated with the given id
133 * @param id id to look for
134 * @return the incoming socket associated with the id,
135 * or NULL if there is none
137 static struct Incoming *
138 get_incoming (uint32_t id)
140 struct Incoming *incoming;
141 for (incoming = incoming_head; NULL != incoming; incoming = incoming->next)
142 if (incoming->request_id == id)
149 destroy_incoming (struct Incoming *incoming)
151 if (NULL != incoming->mq)
153 GNUNET_MQ_destroy (incoming->mq);
156 if (NULL != incoming->socket)
158 GNUNET_STREAM_close (incoming->socket);
159 incoming->socket = NULL;
161 GNUNET_CONTAINER_DLL_remove (incoming_head, incoming_tail, incoming);
162 GNUNET_free (incoming);
167 * Handle a request for a set operation for
170 * @param cls the incoming socket
171 * @param mh the message
175 handle_p2p_operation_request (void *cls, const struct GNUNET_MessageHeader *mh)
177 struct Incoming *incoming = cls;
178 const struct OperationRequestMessage *msg = (const struct OperationRequestMessage *) mh;
179 struct GNUNET_MQ_Message *mqm;
180 struct RequestMessage *cmsg;
181 struct Listener *listener;
182 const struct GNUNET_MessageHeader *context_msg;
184 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "got operation request\n");
186 if (ntohs (mh->size) < sizeof *msg)
189 destroy_incoming (incoming);
192 else if (ntohs (mh->size) == sizeof *msg)
198 context_msg = &msg[1].header;
199 if ((ntohs (context_msg->size) + sizeof *msg) != ntohs (msg->header.size))
201 /* size of context message is invalid */
203 destroy_incoming (incoming);
207 /* find the appropriate listener */
208 for (listener = listeners_head;
210 listener = listener->next)
212 if ( (0 != GNUNET_CRYPTO_hash_cmp (&msg->app_id, &listener->app_id)) ||
213 (htons (msg->operation) != listener->operation) )
215 mqm = GNUNET_MQ_msg (cmsg, GNUNET_MESSAGE_TYPE_SET_REQUEST);
217 GNUNET_MQ_nest (mqm, context_msg))
219 /* FIXME: disconnect the peer */
220 GNUNET_MQ_discard (mqm);
223 incoming->request_id = request_id++;
224 cmsg->request_id = htonl (incoming->request_id);
225 GNUNET_MQ_send (listener->client_mq, mqm);
228 /* FIXME: send a reject message */
233 * Called when a client wants to create a new set.
236 * @param client client that sent the message
237 * @param m message sent by the client
240 handle_client_create (void *cls,
241 struct GNUNET_SERVER_Client *client,
242 const struct GNUNET_MessageHeader *m)
244 struct SetCreateMessage *msg = (struct SetCreateMessage *) m;
247 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "new set created\n");
249 if (NULL != get_set (client))
252 GNUNET_SERVER_client_disconnect (client);
256 set = GNUNET_new (struct Set);
258 switch (ntohs (msg->operation))
260 case GNUNET_SET_OPERATION_INTERSECTION:
264 case GNUNET_SET_OPERATION_UNION:
265 set = _GSS_union_set_create ();
270 GNUNET_SERVER_client_disconnect (client);
274 set->client = client;
275 set->client_mq = GNUNET_MQ_queue_for_server_client (client);
276 GNUNET_CONTAINER_DLL_insert (sets_head, sets_tail, set);
278 GNUNET_SERVER_receive_done (client, GNUNET_OK);
283 * Called when a client wants to create a new set.
286 * @param client client that sent the message
287 * @param m message sent by the client
290 handle_client_listen (void *cls,
291 struct GNUNET_SERVER_Client *client,
292 const struct GNUNET_MessageHeader *m)
294 struct ListenMessage *msg = (struct ListenMessage *) m;
295 struct Listener *listener;
297 if (NULL != get_listener (client))
300 GNUNET_SERVER_client_disconnect (client);
304 listener = GNUNET_new (struct Listener);
305 listener->app_id = msg->app_id;
306 listener->operation = msg->operation;
307 GNUNET_CONTAINER_DLL_insert_tail (listeners_head, listeners_tail, listener);
309 GNUNET_SERVER_receive_done (client, GNUNET_OK);
314 * Called when a client wants to create a new set.
317 * @param client client that sent the message
318 * @param m message sent by the client
321 handle_client_add (void *cls,
322 struct GNUNET_SERVER_Client *client,
323 const struct GNUNET_MessageHeader *m)
327 set = get_set (client);
331 _GSS_client_disconnect (client);
334 switch (set->operation)
336 case GNUNET_SET_OPERATION_UNION:
337 _GSS_union_add ((struct ElementMessage *) m, set);
338 case GNUNET_SET_OPERATION_INTERSECTION:
346 GNUNET_SERVER_receive_done (client, GNUNET_OK);
351 * Called when a client wants to evaluate a set operation with another peer.
354 * @param client client that sent the message
355 * @param m message sent by the client
358 handle_client_evaluate (void *cls,
359 struct GNUNET_SERVER_Client *client,
360 const struct GNUNET_MessageHeader *m)
364 set = get_set (client);
368 _GSS_client_disconnect (client);
373 switch (set->operation)
375 case GNUNET_SET_OPERATION_INTERSECTION:
378 case GNUNET_SET_OPERATION_UNION:
379 _GSS_union_evaluate ((struct EvaluateMessage *) m, set);
386 GNUNET_SERVER_receive_done (client, GNUNET_OK);
391 * Handle a cancel request from a client.
394 * @param client the client
395 * @param m the cancel message
398 handle_client_cancel (void *cls,
399 struct GNUNET_SERVER_Client *client,
400 const struct GNUNET_MessageHeader *m)
402 /* FIXME: implement */
403 GNUNET_SERVER_receive_done (client, GNUNET_OK);
408 * Handle an ack from a client.
411 * @param client the client
412 * @param m the message
415 handle_client_ack (void *cls,
416 struct GNUNET_SERVER_Client *client,
417 const struct GNUNET_MessageHeader *m)
419 /* FIXME: implement */
420 GNUNET_SERVER_receive_done (client, GNUNET_OK);
425 * Handle a request from the client to accept
429 * @param client the client
430 * @param m the message
433 handle_client_accept (void *cls,
434 struct GNUNET_SERVER_Client *client,
435 const struct GNUNET_MessageHeader *mh)
438 struct Incoming *incoming;
439 struct AcceptMessage *msg = (struct AcceptMessage *) mh;
441 set = get_set (client);
447 _GSS_client_disconnect (client);
451 incoming = get_incoming (ntohl (msg->request_id));
453 if ( (NULL == incoming) ||
454 (incoming->operation != set->operation) )
457 _GSS_client_disconnect (client);
461 switch (set->operation)
463 case GNUNET_SET_OPERATION_INTERSECTION:
467 case GNUNET_SET_OPERATION_UNION:
468 _GSS_union_accept (msg, set, incoming);
474 /* FIXME: destroy incoming */
476 GNUNET_SERVER_receive_done (client, GNUNET_OK);
481 * Functions of this type are called upon new stream connection from other peers
482 * or upon binding error which happen when the app_port given in
483 * GNUNET_STREAM_listen() is already taken.
485 * @param cls the closure from GNUNET_STREAM_listen
486 * @param socket the socket representing the stream; NULL on binding error
487 * @param initiator the identity of the peer who wants to establish a stream
488 * with us; NULL on binding error
489 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
490 * stream (the socket will be invalid after the call)
493 stream_listen_cb (void *cls,
494 struct GNUNET_STREAM_Socket *socket,
495 const struct GNUNET_PeerIdentity *initiator)
497 struct Incoming *incoming;
498 static const struct GNUNET_MQ_Handler handlers[] = {
499 {handle_p2p_operation_request, GNUNET_MESSAGE_TYPE_SET_P2P_OPERATION_REQUEST},
500 GNUNET_MQ_HANDLERS_END
506 return GNUNET_SYSERR;
509 incoming = GNUNET_new (struct Incoming);
510 incoming->peer = *initiator;
511 incoming->socket = socket;
512 incoming->mq = GNUNET_MQ_queue_for_stream_socket (incoming->socket, handlers, incoming);
513 /* FIXME: timeout for peers that only connect but don't send anything */
514 GNUNET_CONTAINER_DLL_insert_tail (incoming_head, incoming_tail, incoming);
520 * Called to clean up, after a shutdown has been requested.
523 * @param tc context information (why was this task triggered now)
526 shutdown_task (void *cls,
527 const struct GNUNET_SCHEDULER_TaskContext *tc)
529 if (NULL != stream_listen_socket)
531 GNUNET_STREAM_listen_close (stream_listen_socket);
532 stream_listen_socket = NULL;
535 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "handled shutdown request\n");
540 * Function called by the service's run
541 * method to run service-specific setup code.
544 * @param server the initialized server
545 * @param cfg configuration to use
548 run (void *cls, struct GNUNET_SERVER_Handle *server, const struct GNUNET_CONFIGURATION_Handle *cfg)
550 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
551 {handle_client_create, NULL, GNUNET_MESSAGE_TYPE_SET_CREATE, 0},
552 {handle_client_listen, NULL, GNUNET_MESSAGE_TYPE_SET_LISTEN, 0},
553 {handle_client_add, NULL, GNUNET_MESSAGE_TYPE_SET_ADD, 0},
554 {handle_client_cancel, NULL, GNUNET_MESSAGE_TYPE_SET_CANCEL, 0},
555 {handle_client_evaluate, NULL, GNUNET_MESSAGE_TYPE_SET_EVALUATE, 0},
556 {handle_client_ack, NULL, GNUNET_MESSAGE_TYPE_SET_ACK, 0},
557 {handle_client_accept, NULL, GNUNET_MESSAGE_TYPE_SET_ACCEPT, 0},
562 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, NULL);
563 GNUNET_SERVER_add_handlers (server, server_handlers);
564 stream_listen_socket = GNUNET_STREAM_listen (cfg, GNUNET_APPLICATION_TYPE_SET,
565 &stream_listen_cb, NULL,
566 GNUNET_STREAM_OPTION_END);
571 * The main function for the set service.
573 * @param argc number of arguments from the command line
574 * @param argv command line arguments
575 * @return 0 ok, 1 on error
578 main (int argc, char *const *argv)
581 ret = GNUNET_SERVICE_run (argc, argv, "set", GNUNET_SERVICE_OPTION_NONE, &run, NULL);
582 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "exit\n");
583 return (GNUNET_OK == ret) ? 0 : 1;