X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Frps%2Frps_api.c;h=7d0674aff12d10ccb2240368bd93d6184421c29f;hb=7a5a724a6f96baf80d2226326124aa01c58ad3fe;hp=30f0a75c55a909bce9cf8b614d79ba037e2abe88;hpb=7f424af3f7d3d3271feafecc6330185e76f95ee2;p=oweals%2Fgnunet.git diff --git a/src/rps/rps_api.c b/src/rps/rps_api.c index 30f0a75c5..7d0674aff 100644 --- a/src/rps/rps_api.c +++ b/src/rps/rps_api.c @@ -1,21 +1,19 @@ /* This file is part of GNUnet. - Copyright (C) + Copyright (C) - GNUnet is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation; either version 3, or (at your - option) any later version. + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . */ /** @@ -27,11 +25,49 @@ #include "gnunet_util_lib.h" #include "rps.h" #include "gnunet_rps_service.h" +#include "rps-sampler_client.h" #include #define LOG(kind,...) GNUNET_log_from (kind, "rps-api",__VA_ARGS__) +/** + * Handle for a request to get peers from biased stream of ids + */ +struct GNUNET_RPS_StreamRequestHandle +{ + /** + * The client issuing the request. + */ + struct GNUNET_RPS_Handle *rps_handle; + + /** + * The number of requested peers. + */ + uint32_t num_peers_left; + + /** + * The callback to be called when we receive an answer. + */ + GNUNET_RPS_NotifyReadyCB ready_cb; + + /** + * The closure for the callback. + */ + void *ready_cb_cls; + + /** + * @brief Next element of the DLL + */ + struct GNUNET_RPS_StreamRequestHandle *next; + + /** + * @brief Previous element of the DLL + */ + struct GNUNET_RPS_StreamRequestHandle *prev; +}; + + /** * Handler to handle requests from a client. */ @@ -43,31 +79,67 @@ struct GNUNET_RPS_Handle const struct GNUNET_CONFIGURATION_Handle *cfg; /** - * The connection to the client. + * The message queue to the client. */ - struct GNUNET_CLIENT_Connection *conn; + struct GNUNET_MQ_Handle *mq; /** - * The message queue to the client. + * @brief Callback called on each update of the view */ - struct GNUNET_MQ_Handle *mq; + GNUNET_RPS_NotifyReadyCB view_update_cb; + + /** + * @brief Closure to each requested update of the view + */ + void *view_update_cls; + + /** + * @brief Closure to each requested peer from the biased stream + */ + void *stream_input_cls; + + /** + * @brief Head of the DLL of stream requests + */ + struct GNUNET_RPS_StreamRequestHandle *stream_requests_head; + + /** + * @brief Tail of the DLL of stream requests + */ + struct GNUNET_RPS_StreamRequestHandle *stream_requests_tail; }; /** - * Handler to single requests from the client. + * Handler for a single request from a client. */ struct GNUNET_RPS_Request_Handle { /** * The client issuing the request. */ - struct GNUNET_RPS_Handle *h; + struct GNUNET_RPS_Handle *rps_handle; + + /** + * The number of requested peers. + */ + uint32_t num_requests; + + /** + * @brief The Sampler for the client request + */ + struct RPS_Sampler *sampler; /** - * The nuber of the request. + * @brief Request handle of the request to the sampler - needed to cancel the request */ - uint64_t n; + struct RPS_SamplerRequestHandle *sampler_rh; + + /** + * @brief Request handle of the request of the biased stream of peers - + * needed to cancel the request + */ + struct GNUNET_RPS_StreamRequestHandle *srh; /** * The callback to be called when we receive an answer. @@ -81,17 +153,6 @@ struct GNUNET_RPS_Request_Handle }; -/** - * Array of Request_Handles. - */ -struct GNUNET_RPS_Request_Handle *req_handlers = NULL; - -/** - * Current length of req_handlers. - */ -unsigned int req_handlers_size = 0; - - /** * Struct used to pack the callback, its closure (provided by the caller) * and the connection handler to the service to pass it to a callback function. @@ -116,36 +177,425 @@ struct cb_cls_pack /** - * This function is called, when the service replies to our request. - * It calls the callback the caller gave us with the provided closure - * and disconnects afterwards. + * @brief Create a new handle for a stream request + * + * @param rps_handle The rps handle + * @param num_peers The number of desired peers + * @param ready_cb The callback to be called, once all peers are ready + * @param cls The colsure to provide to the callback + * + * @return The handle to the stream request + */ +static struct GNUNET_RPS_StreamRequestHandle * +new_stream_request (struct GNUNET_RPS_Handle *rps_handle, + uint64_t num_peers, + GNUNET_RPS_NotifyReadyCB ready_cb, + void *cls) +{ + struct GNUNET_RPS_StreamRequestHandle *srh; + + srh = GNUNET_new (struct GNUNET_RPS_StreamRequestHandle); + + srh->rps_handle = rps_handle; + srh->num_peers_left = num_peers; + srh->ready_cb = ready_cb; + srh->ready_cb_cls = cls; + GNUNET_CONTAINER_DLL_insert (rps_handle->stream_requests_head, + rps_handle->stream_requests_tail, + srh); + + return srh; +} + + +/** + * @brief Remove the given stream request from the list of requests and memory + * + * @param srh The request to be removed + * @param srh_head Head of the DLL to remove request from + * @param srh_tail Tail of the DLL to remove request from + */ +static void +remove_stream_request (struct GNUNET_RPS_StreamRequestHandle *srh, + struct GNUNET_RPS_StreamRequestHandle *srh_head, + struct GNUNET_RPS_StreamRequestHandle *srh_tail) +{ + GNUNET_CONTAINER_DLL_remove (srh_head, + srh_tail, + srh); + + GNUNET_free (srh); +} + + +/** + * @brief Called once the sampler has collected all requested peers. + * + * Calls the callback provided by the client with the corresponding cls. + * + * @param peers The array of @a num_peers that has been returned. + * @param num_peers The number of peers that have been returned + * @param cls The #GNUNET_RPS_Request_Handle + */ +void +peers_ready_cb (const struct GNUNET_PeerIdentity *peers, + uint32_t num_peers, + void *cls) +{ + struct GNUNET_RPS_Request_Handle *rh = cls; + + rh->ready_cb (rh->ready_cb_cls, + num_peers, + peers); + // TODO cleanup, sampler, rh, cancel stuff + // TODO screw this function. We can give the cb,cls directly to the sampler. +} + + +/** + * @brief Callback to collect the peers from the biased stream and put those + * into the sampler. + * + * @param cls The #GNUNET_RPS_Request_Handle + * @param num_peers The number of peer that have been returned + * @param peers The array of @a num_peers that have been returned + */ +void +collect_peers_cb (void *cls, + uint64_t num_peers, + const struct GNUNET_PeerIdentity *peers) +{ + struct GNUNET_RPS_Request_Handle *rh = cls; + + for (uint64_t i = 0; i < num_peers; i++) + { + RPS_sampler_update (rh->sampler, &peers[i]); + } +} + + +/** + * @brief Create new request handle + * + * @param rps_handle Handle to the service + * @param num_requests Number of requests + * @param ready_cb Callback + * @param cls Closure + * + * @return The newly created request handle + */ +static struct GNUNET_RPS_Request_Handle * +new_request_handle (struct GNUNET_RPS_Handle *rps_handle, + uint64_t num_requests, + GNUNET_RPS_NotifyReadyCB ready_cb, + void *cls) +{ + struct GNUNET_RPS_Request_Handle *rh; + + rh = GNUNET_new (struct GNUNET_RPS_Request_Handle); + rh->rps_handle = rps_handle; + rh->num_requests = num_requests; + rh->sampler = RPS_sampler_mod_init (num_requests, + GNUNET_TIME_UNIT_SECONDS); // TODO remove this time-stuff + rh->sampler_rh = RPS_sampler_get_n_rand_peers (rh->sampler, + num_requests, + peers_ready_cb, + rh); + rh->srh = GNUNET_RPS_stream_request (rps_handle, + 0, /* infinite updates */ + collect_peers_cb, + rh); /* cls */ + rh->ready_cb = ready_cb; + rh->ready_cb_cls = cls; + + return rh; +} + + +/* Get internals for debugging/profiling purposes */ + +/** + * Request updates of view + * + * @param rps_handle handle to the rps service + * @param num_req_peers number of peers we want to receive + * (0 for infinite updates) + * @param cls a closure that will be given to the callback + * @param ready_cb the callback called when the peers are available + */ +void +GNUNET_RPS_view_request (struct GNUNET_RPS_Handle *rps_handle, + uint32_t num_updates, + GNUNET_RPS_NotifyReadyCB view_update_cb, + void *cls) +{ + struct GNUNET_MQ_Envelope *ev; + struct GNUNET_RPS_CS_DEBUG_ViewRequest *msg; + + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Client requests %" PRIu32 " view updates\n", + num_updates); + rps_handle->view_update_cb = view_update_cb; + rps_handle->view_update_cls = cls; + + ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REQUEST); + msg->num_updates = htonl (num_updates); + GNUNET_MQ_send (rps_handle->mq, ev); +} + + +void +GNUNET_RPS_view_request_cancel (struct GNUNET_RPS_Handle *rps_handle) +{ + struct GNUNET_MQ_Envelope *ev; + + GNUNET_assert (NULL != rps_handle->view_update_cb); + + rps_handle->view_update_cb = NULL; + + ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_CANCEL); + GNUNET_MQ_send (rps_handle->mq, ev); +} + + +/** + * Request biased stream of peers that are being put into the sampler + * + * @param rps_handle handle to the rps service + * @param num_req_peers number of peers we want to receive + * (0 for infinite updates) + * @param cls a closure that will be given to the callback + * @param ready_cb the callback called when the peers are available + */ +struct GNUNET_RPS_StreamRequestHandle * +GNUNET_RPS_stream_request (struct GNUNET_RPS_Handle *rps_handle, + uint32_t num_peers, + GNUNET_RPS_NotifyReadyCB stream_input_cb, + void *cls) +{ + struct GNUNET_RPS_StreamRequestHandle *srh; + struct GNUNET_MQ_Envelope *ev; + struct GNUNET_RPS_CS_DEBUG_StreamRequest *msg; + + srh = new_stream_request (rps_handle, + num_peers, /* num requests */ + stream_input_cb, + cls); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Client requests %" PRIu32 " biased stream updates\n", + num_peers); + + ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REQUEST); + GNUNET_MQ_send (rps_handle->mq, ev); + return srh; +} + + +/** + * This function is called, when the service updates the view. + * It verifies that @a msg is well-formed. * * @param cls the closure - * @param message the message + * @param msg the message + * @return #GNUNET_OK if @a msg is well-formed + */ +static int +check_view_update (void *cls, + const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg) +{ + uint16_t msize = ntohs (msg->header.size); + uint32_t num_peers = ntohl (msg->num_peers); + (void) cls; + + msize -= sizeof (struct GNUNET_RPS_CS_DEBUG_ViewReply); + if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) || + (msize % sizeof (struct GNUNET_PeerIdentity) != 0) ) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + return GNUNET_OK; +} + + +/** + * This function is called, when the service updated its view. + * It calls the callback the caller provided + * and disconnects afterwards. + * + * @param msg the message */ - static void -handle_reply (void *cls, - const struct GNUNET_MessageHeader *message) +static void +handle_view_update (void *cls, + const struct GNUNET_RPS_CS_DEBUG_ViewReply *msg) { - struct GNUNET_RPS_CS_ReplyMessage *msg; - //struct cb_cls_pack *pack; - //struct GNUNET_RPS_Handle *h; + struct GNUNET_RPS_Handle *h = cls; struct GNUNET_PeerIdentity *peers; - struct GNUNET_RPS_Request_Handle *rh; /* Give the peers back */ - msg = (struct GNUNET_RPS_CS_ReplyMessage *) message; - //pack = (struct cb_cls_pack *) cls; - //h = (struct GNUNET_RPS_Handle *) cls; + LOG (GNUNET_ERROR_TYPE_DEBUG, + "New view of %" PRIu32 " peers:\n", + ntohl (msg->num_peers)); + peers = (struct GNUNET_PeerIdentity *) &msg[1]; - rh = &req_handlers[msg->n]; - rh->ready_cb((rh)->ready_cb_cls, msg->num_peers, peers); + GNUNET_assert (NULL != h); + GNUNET_assert (NULL != h->view_update_cb); + h->view_update_cb (h->view_update_cls, ntohl (msg->num_peers), peers); +} + + +/** + * @brief Send message to service that this client does not want to receive + * further updates from the biased peer stream + * + * @param rps_handle The handle representing the service to the client + */ +static void +cancel_stream (struct GNUNET_RPS_Handle *rps_handle) +{ + struct GNUNET_MQ_Envelope *ev; + + ev = GNUNET_MQ_msg_header (GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_CANCEL); + GNUNET_MQ_send (rps_handle->mq, ev); +} - /* Disconnect */ - //GNUNET_CLIENT_disconnect(pack->service_conn); + +/** + * @brief Cancel a specific request for updates from the biased peer stream + * + * @param srh The request handle to cancel + */ +void +GNUNET_RPS_stream_cancel (struct GNUNET_RPS_StreamRequestHandle *srh) +{ + struct GNUNET_RPS_Handle *rps_handle; + + rps_handle = srh->rps_handle; + GNUNET_CONTAINER_DLL_remove (rps_handle->stream_requests_head, + rps_handle->stream_requests_tail, + srh); + GNUNET_free (srh); + if (NULL == rps_handle->stream_requests_head) cancel_stream (rps_handle); } +/** + * This function is called, when the service sends another peer from the biased + * stream. + * It calls the callback the caller provided + * and disconnects afterwards. + * + * TODO merge with check_view_update + * + * @param msg the message + */ +static int +check_stream_input (void *cls, + const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg) +{ + uint16_t msize = ntohs (msg->header.size); + uint32_t num_peers = ntohl (msg->num_peers); + (void) cls; + + msize -= sizeof (struct GNUNET_RPS_CS_DEBUG_StreamReply); + if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) || + (msize % sizeof (struct GNUNET_PeerIdentity) != 0) ) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + return GNUNET_OK; +} + +/** + * This function is called, when the service sends another peer from the biased + * stream. + * It calls the callback the caller provided + * and disconnects afterwards. + * + * @param msg the message + */ +static void +handle_stream_input (void *cls, + const struct GNUNET_RPS_CS_DEBUG_StreamReply *msg) +{ + struct GNUNET_RPS_Handle *h = cls; + const struct GNUNET_PeerIdentity *peers; + /* The following two pointers are used to prevent that new handles are + * inserted into the DLL, that is currently iterated over, from within a call + * to that handler_cb, are executed and in turn again add themselves to the + * iterated DLL infinitely */ + struct GNUNET_RPS_StreamRequestHandle *srh_head_tmp; + struct GNUNET_RPS_StreamRequestHandle *srh_tail_tmp; + uint64_t num_peers; + uint64_t num_peers_return; + + peers = (struct GNUNET_PeerIdentity *) &msg[1]; + num_peers = ntohl (msg->num_peers); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Received %" PRIu64 " peer(s) from stream input.\n", + num_peers); + srh_head_tmp = h->stream_requests_head; + srh_tail_tmp = h->stream_requests_tail; + h->stream_requests_head = NULL; + h->stream_requests_tail = NULL; + for (struct GNUNET_RPS_StreamRequestHandle *srh_iter = srh_head_tmp; + NULL != srh_iter; + srh_iter = srh_iter->next) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Calling srh - left: %" PRIu64 "\n", + srh_iter->num_peers_left); + if (0 == srh_iter->num_peers_left) /* infinite updates */ + { + num_peers_return = num_peers; + } + else if (num_peers > srh_iter->num_peers_left) + { + num_peers_return = num_peers - srh_iter->num_peers_left; + } + else /* num_peers <= srh_iter->num_peers_left */ + { + num_peers_return = srh_iter->num_peers_left - num_peers; + } + srh_iter->ready_cb (srh_iter->ready_cb_cls, + num_peers_return, + peers); + if (0 == srh_iter->num_peers_left) ; + else if (num_peers_return >= srh_iter->num_peers_left) + { + remove_stream_request (srh_iter, + srh_head_tmp, + srh_tail_tmp); + } + else + { + srh_iter->num_peers_left -= num_peers_return; + } + } + for (struct GNUNET_RPS_StreamRequestHandle *srh_iter = srh_head_tmp; + NULL != srh_iter; + srh_iter = srh_iter->next) + { + GNUNET_CONTAINER_DLL_insert (h->stream_requests_head, + h->stream_requests_tail, + srh_iter); + } + + if (NULL == h->stream_requests_head) + { + cancel_stream (h); + } +} + + +/** + * Reconnect to the service + */ +static void +reconnect (struct GNUNET_RPS_Handle *h); + + /** * Error handler for mq. * @@ -155,38 +605,70 @@ handle_reply (void *cls, * @param cls the closure * @param error error code without specyfied meaning */ - static void -mq_error_handler (void *cls, enum GNUNET_MQ_Error error) +static void +mq_error_handler (void *cls, + enum GNUNET_MQ_Error error) { + struct GNUNET_RPS_Handle *h = cls; //TODO LOG + LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n\ + 1: READ,\n\ + 2: WRITE,\n\ + 4: TIMEOUT\n", + error); + reconnect (h); + /* Resend all pending request as the service destroyed its knowledge + * about them */ +} + + +/** + * Reconnect to the service + */ +static void +reconnect (struct GNUNET_RPS_Handle *h) +{ + struct GNUNET_MQ_MessageHandler mq_handlers[] = { + GNUNET_MQ_hd_var_size (view_update, + GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_VIEW_REPLY, + struct GNUNET_RPS_CS_DEBUG_ViewReply, + h), + GNUNET_MQ_hd_var_size (stream_input, + GNUNET_MESSAGE_TYPE_RPS_CS_DEBUG_STREAM_REPLY, + struct GNUNET_RPS_CS_DEBUG_StreamReply, + h), + GNUNET_MQ_handler_end () + }; + + if (NULL != h->mq) + GNUNET_MQ_destroy (h->mq); + h->mq = GNUNET_CLIENT_connect (h->cfg, + "rps", + mq_handlers, + &mq_error_handler, + h); } + /** * Connect to the rps service * * @param cfg configuration to use * @return a handle to the service */ - struct GNUNET_RPS_Handle * +struct GNUNET_RPS_Handle * GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) { struct GNUNET_RPS_Handle *h; - //struct GNUNET_RPS_Request_Handle *rh; - static const struct GNUNET_MQ_MessageHandler mq_handlers[] = { - {&handle_reply, GNUNET_MESSAGE_TYPE_RPS_CS_REPLY, 0}, - GNUNET_MQ_HANDLERS_END - }; - - h = GNUNET_new(struct GNUNET_RPS_Handle); - //h->cfg = GNUNET_new(struct GNUNET_CONFIGURATION_Handle); - //*h->cfg = *cfg; - h->cfg = cfg; // FIXME |^ - h->conn = GNUNET_CLIENT_connect("rps", cfg); - h->mq = GNUNET_MQ_queue_for_connection_client(h->conn, - mq_handlers, - mq_error_handler, // TODO implement - h); + h = GNUNET_new (struct GNUNET_RPS_Handle); + h->cfg = cfg; + reconnect (h); + if (NULL == h->mq) + { + GNUNET_free (h); + return NULL; + } return h; } @@ -194,35 +676,26 @@ GNUNET_RPS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) /** * Request n random peers. * - * @param h handle to the rps service - * @param n number of peers we want to receive + * @param rps_handle handle to the rps service + * @param num_req_peers number of peers we want to receive * @param ready_cb the callback called when the peers are available * @param cls closure given to the callback * @return a handle to cancel this request */ - struct GNUNET_RPS_Request_Handle * -GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *h, uint32_t n, +struct GNUNET_RPS_Request_Handle * +GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *rps_handle, + uint32_t num_req_peers, GNUNET_RPS_NotifyReadyCB ready_cb, void *cls) { struct GNUNET_RPS_Request_Handle *rh; - struct GNUNET_MQ_Envelope *ev; - struct GNUNET_RPS_CS_RequestMessage *msg; - // assert func != NULL - rh = GNUNET_new (struct GNUNET_RPS_Request_Handle); - rh->h = h; - rh->n = req_handlers_size; // TODO ntoh - rh->ready_cb = ready_cb; - rh->ready_cb_cls = cls; + rh = new_request_handle (rps_handle, + num_req_peers, + ready_cb, + cls); - GNUNET_array_append (req_handlers, req_handlers_size, *rh); - //memcpy(&req_handlers[req_handlers_size-1], rh, sizeof(struct GNUNET_RPS_Request_Handle)); - ev = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_RPS_CS_REQUEST); - msg->num_peers = htonl (n); - msg->n = rh->n; - GNUNET_MQ_send (h->mq, ev); return rh; } @@ -234,12 +707,12 @@ GNUNET_RPS_request_peers (struct GNUNET_RPS_Handle *h, uint32_t n, * @param n number of peers to seed * @param ids the ids of the peers seeded */ - void +void GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h, uint32_t n, const struct GNUNET_PeerIdentity *ids) { - uint32_t size_needed; + size_t size_needed; uint32_t num_peers_max; const struct GNUNET_PeerIdentity *tmp_peer_pointer; struct GNUNET_MQ_Envelope *ev; @@ -248,7 +721,7 @@ GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h, unsigned int i; LOG (GNUNET_ERROR_TYPE_DEBUG, - "Client wants to seed %" PRIX32 " peers:\n", + "Client wants to seed %" PRIu32 " peers:\n", n); for (i = 0 ; i < n ; i++) LOG (GNUNET_ERROR_TYPE_DEBUG, @@ -261,17 +734,17 @@ GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h, n * sizeof (struct GNUNET_PeerIdentity); /* The number of peers that fits in one message together with * the respective header */ - num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE - + num_peers_max = (GNUNET_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_RPS_CS_SeedMessage)) / sizeof (struct GNUNET_PeerIdentity); tmp_peer_pointer = ids; - while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed) + while (GNUNET_MAX_MESSAGE_SIZE < size_needed) { ev = GNUNET_MQ_msg_extra (msg, num_peers_max * sizeof (struct GNUNET_PeerIdentity), GNUNET_MESSAGE_TYPE_RPS_CS_SEED); - msg->num_peers = ntohl (num_peers_max); - memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity)); + msg->num_peers = htonl (num_peers_max); + GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity)); GNUNET_MQ_send (h->mq, ev); n -= num_peers_max; @@ -284,13 +757,13 @@ GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h, ev = GNUNET_MQ_msg_extra (msg, n * sizeof (struct GNUNET_PeerIdentity), GNUNET_MESSAGE_TYPE_RPS_CS_SEED); msg->num_peers = htonl (n); - memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity)); + GNUNET_memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity)); GNUNET_MQ_send (h->mq, ev); } -#if ENABLE_MALICIOUS +#ifdef ENABLE_MALICIOUS /** * Turn RPS service to act malicious. * @@ -305,13 +778,14 @@ GNUNET_RPS_seed_ids (struct GNUNET_RPS_Handle *h, * if @type is 2 the last id is the id of the * peer to be isolated from the rest */ - void +void GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h, uint32_t type, uint32_t num_peers, - const struct GNUNET_PeerIdentity *ids) + const struct GNUNET_PeerIdentity *peer_ids, + const struct GNUNET_PeerIdentity *target_peer) { - uint32_t size_needed; + size_t size_needed; uint32_t num_peers_max; const struct GNUNET_PeerIdentity *tmp_peer_pointer; struct GNUNET_MQ_Envelope *ev; @@ -320,50 +794,62 @@ GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h, unsigned int i; LOG (GNUNET_ERROR_TYPE_DEBUG, - "Client turns malicious with %" PRIX32 " other peers:\n", - n); - for (i = 0 ; i < n ; i++) + "Client turns malicious (type %" PRIu32 ") with %" PRIu32 " other peers:\n", + type, + num_peers); + for (i = 0 ; i < num_peers ; i++) LOG (GNUNET_ERROR_TYPE_DEBUG, "%u. peer: %s\n", i, - GNUNET_i2s (&ids[i])); + GNUNET_i2s (&peer_ids[i])); - /* The actual size the message occupies */ + /* The actual size the message would occupy */ size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) + - n * sizeof (struct GNUNET_PeerIdentity); - /* The number of peers that fits in one message together with + num_peers * sizeof (struct GNUNET_PeerIdentity); + /* The number of peers that fit in one message together with * the respective header */ - num_peers_max = (GNUNET_SERVER_MAX_MESSAGE_SIZE - + num_peers_max = (GNUNET_MAX_MESSAGE_SIZE - sizeof (struct GNUNET_RPS_CS_SeedMessage)) / sizeof (struct GNUNET_PeerIdentity); - tmp_peer_pointer = ids; + tmp_peer_pointer = peer_ids; - while (GNUNET_SERVER_MAX_MESSAGE_SIZE < size_needed) + while (GNUNET_MAX_MESSAGE_SIZE < size_needed) { + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Too many peers to send at once, sending %" PRIu32 " (all we can so far)\n", + num_peers_max); ev = GNUNET_MQ_msg_extra (msg, num_peers_max * sizeof (struct GNUNET_PeerIdentity), GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS); - msg->type = ntohl (type); - msg->num_peers = ntohl (num_peers_max); - memcpy (&msg[1], tmp_peer_pointer, num_peers_max * sizeof (struct GNUNET_PeerIdentity)); + msg->type = htonl (type); + msg->num_peers = htonl (num_peers_max); + if ( (2 == type) || + (3 == type) ) + msg->attacked_peer = peer_ids[num_peers]; + GNUNET_memcpy (&msg[1], + tmp_peer_pointer, + num_peers_max * sizeof (struct GNUNET_PeerIdentity)); + GNUNET_MQ_send (h->mq, ev); - n -= num_peers_max; + num_peers -= num_peers_max; size_needed = sizeof (struct GNUNET_RPS_CS_SeedMessage) + - n * sizeof (struct GNUNET_PeerIdentity); + num_peers * sizeof (struct GNUNET_PeerIdentity); /* Set pointer to beginning of next block of num_peers_max peers */ - tmp_peer_pointer = &ids[num_peers_max]; + tmp_peer_pointer = &peer_ids[num_peers_max]; } ev = GNUNET_MQ_msg_extra (msg, - n * sizeof (struct GNUNET_PeerIdentity), + num_peers * sizeof (struct GNUNET_PeerIdentity), GNUNET_MESSAGE_TYPE_RPS_ACT_MALICIOUS); msg->type = htonl (type); - msg->num_peers = htonl (n); - memcpy (&msg[1], tmp_peer_pointer, n * sizeof (struct GNUNET_PeerIdentity)); + msg->num_peers = htonl (num_peers); + if ( (2 == type) || + (3 == type) ) + msg->attacked_peer = *target_peer; + GNUNET_memcpy (&msg[1], tmp_peer_pointer, num_peers * sizeof (struct GNUNET_PeerIdentity)); GNUNET_MQ_send (h->mq, ev); - } #endif /* ENABLE_MALICIOUS */ @@ -373,10 +859,25 @@ GNUNET_RPS_act_malicious (struct GNUNET_RPS_Handle *h, * * @param rh request handle of request to cancle */ - void +void GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh) { - // TODO + struct GNUNET_RPS_Handle *h; + + h = rh->rps_handle; + if (NULL != rh->srh) + { + remove_stream_request (rh->srh, + h->stream_requests_head, + h->stream_requests_tail); + } + if (NULL == h->stream_requests_head) cancel_stream(h); + if (NULL != rh->sampler_rh) + { + RPS_sampler_request_cancel (rh->sampler_rh); + } + RPS_sampler_destroy (rh->sampler); + GNUNET_free (rh); } @@ -385,11 +886,21 @@ GNUNET_RPS_request_cancel (struct GNUNET_RPS_Request_Handle *rh) * * @param h the handle to the rps service */ - void +void GNUNET_RPS_disconnect (struct GNUNET_RPS_Handle *h) { - if (NULL != h->conn) - GNUNET_CLIENT_disconnect (h->conn); + GNUNET_MQ_destroy (h->mq); + if (NULL != h->stream_requests_head) + { + LOG (GNUNET_ERROR_TYPE_WARNING, + "Still waiting for replies\n"); + } + if (NULL != h->view_update_cb) + { + LOG (GNUNET_ERROR_TYPE_WARNING, + "Still waiting for view updates\n"); + } + GNUNET_free (h); }