2 This file is part of GNUnet.
3 Copyright (C) 2011 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 3, 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.
21 * @file fs/gnunet-service-fs_lc.c
22 * @brief API to handle 'local clients'
23 * @author Christian Grothoff
26 #include "gnunet-service-fs.h"
27 #include "gnunet-service-fs_lc.h"
28 #include "gnunet-service-fs_cp.h"
29 #include "gnunet-service-fs_pr.h"
33 * Doubly-linked list of requests we are performing
34 * on behalf of the same client.
40 * This is a doubly-linked list.
42 struct ClientRequest *next;
45 * This is a doubly-linked list.
47 struct ClientRequest *prev;
50 * Request this entry represents.
52 struct GSF_PendingRequest *pr;
55 * Client list this request belongs to.
57 struct GSF_LocalClient *lc;
60 * Task scheduled to destroy the request.
62 struct GNUNET_SCHEDULER_Task * kill_task;
68 * Replies to be transmitted to the client. The actual
69 * response message is allocated after this struct.
74 * This is a doubly-linked list.
76 struct ClientResponse *next;
79 * This is a doubly-linked list.
81 struct ClientResponse *prev;
84 * Client list entry this response belongs to.
86 struct GSF_LocalClient *lc;
89 * Number of bytes in the response.
98 struct GSF_LocalClient
102 * We keep clients in a DLL.
104 struct GSF_LocalClient *next;
107 * We keep clients in a DLL.
109 struct GSF_LocalClient *prev;
114 struct GNUNET_SERVER_Client *client;
117 * Head of list of requests performed on behalf
118 * of this client right now.
120 struct ClientRequest *cr_head;
123 * Tail of list of requests performed on behalf
124 * of this client right now.
126 struct ClientRequest *cr_tail;
129 * Head of linked list of responses.
131 struct ClientResponse *res_head;
134 * Tail of linked list of responses.
136 struct ClientResponse *res_tail;
139 * Context for sending replies.
141 struct GNUNET_SERVER_TransmitHandle *th;
147 * Head of linked list of our local clients.
149 static struct GSF_LocalClient *client_head;
152 * Head of linked list of our local clients.
154 static struct GSF_LocalClient *client_tail;
158 * Look up a local client record or create one if it
161 * @param client handle of the client
162 * @return handle to local client entry
164 struct GSF_LocalClient *
165 GSF_local_client_lookup_ (struct GNUNET_SERVER_Client *client)
167 struct GSF_LocalClient *pos;
170 while ((NULL != pos) && (pos->client != client))
174 pos = GNUNET_new (struct GSF_LocalClient);
175 pos->client = client;
176 GNUNET_CONTAINER_DLL_insert (client_head,
184 * Free the given client request.
186 * @param cls the client request to free
187 * @param tc task context
190 client_request_destroy (void *cls,
191 const struct GNUNET_SCHEDULER_TaskContext *tc)
193 struct ClientRequest *cr = cls;
194 struct GSF_LocalClient *lc;
196 cr->kill_task = NULL;
198 GNUNET_CONTAINER_DLL_remove (lc->cr_head, lc->cr_tail, cr);
199 GSF_pending_request_cancel_ (cr->pr, GNUNET_YES);
200 GNUNET_STATISTICS_update (GSF_stats,
201 gettext_noop ("# client searches active"), -1,
208 * Handle a reply to a pending request. Also called if a request
209 * expires (then with data == NULL). The handler may be called
210 * many times (depending on the request type), but will not be
211 * called during or after a call to #GSF_pending_request_cancel()
212 * and will also not be called anymore after a call signalling
215 * @param cls user-specified closure
216 * @param eval evaluation of the result
217 * @param pr handle to the original pending request
218 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
219 * @param expiration when does @a data expire?
220 * @param last_transmission when was the last time we've tried to download this block? (FOREVER if unknown)
221 * @param type type of the block
222 * @param data response data, NULL on request expiration
223 * @param data_len number of bytes in @a data
226 client_response_handler (void *cls,
227 enum GNUNET_BLOCK_EvaluationResult eval,
228 struct GSF_PendingRequest *pr,
229 uint32_t reply_anonymity_level,
230 struct GNUNET_TIME_Absolute expiration,
231 struct GNUNET_TIME_Absolute last_transmission,
232 enum GNUNET_BLOCK_Type type,
236 struct ClientRequest *cr = cls;
237 struct GSF_LocalClient *lc;
238 struct ClientPutMessage *pm;
239 const struct GSF_PendingRequestData *prd;
244 /* local-only request, with no result, clean up. */
245 if (NULL == cr->kill_task)
246 cr->kill_task = GNUNET_SCHEDULER_add_now (&client_request_destroy,
250 prd = GSF_pending_request_get_data_ (pr);
251 GNUNET_break (type != GNUNET_BLOCK_TYPE_ANY);
252 if ((prd->type != type) && (prd->type != GNUNET_BLOCK_TYPE_ANY))
257 GNUNET_STATISTICS_update (GSF_stats,
259 ("# replies received for local clients"), 1,
261 GNUNET_assert (pr == cr->pr);
263 msize = sizeof (struct ClientPutMessage) + data_len;
265 char buf[msize] GNUNET_ALIGN;
267 pm = (struct ClientPutMessage *) buf;
268 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
269 pm->header.size = htons (msize);
270 pm->type = htonl (type);
271 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
272 pm->last_transmission = GNUNET_TIME_absolute_hton (last_transmission);
273 pm->num_transmissions = htonl (prd->num_transmissions);
274 pm->respect_offered = htonl (prd->respect_offered);
275 memcpy (&pm[1], data, data_len);
276 GSF_local_client_transmit_ (lc, &pm->header);
278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
279 "Queued reply to query `%s' for local client\n",
280 GNUNET_h2s (&prd->query), (unsigned int) prd->type);
281 if (GNUNET_BLOCK_EVALUATION_OK_LAST != eval)
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284 "Evaluation %d - keeping query alive\n",
288 if (NULL == cr->kill_task)
289 cr->kill_task = GNUNET_SCHEDULER_add_now (&client_request_destroy, cr);
294 * Handle START_SEARCH-message (search request from local client).
295 * Only responsible for creating the request entry itself and setting
296 * up reply callback and cancellation on client disconnect. Does NOT
297 * execute the actual request strategy (planning).
299 * @param client identification of the client
300 * @param message the actual message
301 * @param prptr where to store the pending request handle for the request
302 * @return #GNUNET_YES to start local processing,
303 * #GNUNET_NO to not (yet) start local processing,
304 * #GNUNET_SYSERR on error
307 GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
308 const struct GNUNET_MessageHeader *message,
309 struct GSF_PendingRequest **prptr)
311 static struct GNUNET_PeerIdentity all_zeros;
312 const struct SearchMessage *sm;
313 struct GSF_LocalClient *lc;
314 struct ClientRequest *cr;
315 struct GSF_PendingRequestData *prd;
318 enum GNUNET_BLOCK_Type type;
319 enum GSF_PendingRequestOptions options;
321 msize = ntohs (message->size);
322 if ((msize < sizeof (struct SearchMessage)) ||
323 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (struct GNUNET_HashCode)))
327 return GNUNET_SYSERR;
329 GNUNET_STATISTICS_update (GSF_stats,
330 gettext_noop ("# client searches received"),
333 sc = (msize - sizeof (struct SearchMessage)) / sizeof (struct GNUNET_HashCode);
334 sm = (const struct SearchMessage *) message;
335 type = ntohl (sm->type);
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 "Received request for `%s' of type %u from local client\n",
338 GNUNET_h2s (&sm->query), (unsigned int) type);
339 lc = GSF_local_client_lookup_ (client);
341 /* detect duplicate UBLOCK requests */
342 if ((type == GNUNET_BLOCK_TYPE_FS_UBLOCK) ||
343 (type == GNUNET_BLOCK_TYPE_ANY))
348 prd = GSF_pending_request_get_data_ (cr->pr);
349 /* only unify with queries that hae not yet started local processing
350 (SEARCH_MESSAGE_OPTION_CONTINUED was always set) and that have a
351 matching query and type */
352 if ((GNUNET_YES != prd->has_started) &&
353 (0 != memcmp (&prd->query,
355 sizeof (struct GNUNET_HashCode))) &&
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
364 "Have existing request, merging content-seen lists.\n");
365 GSF_pending_request_update_ (cr->pr,
366 (const struct GNUNET_HashCode *) &sm[1],
368 GNUNET_STATISTICS_update (GSF_stats,
370 ("# client searches updated (merged content seen list)"),
375 GNUNET_STATISTICS_update (GSF_stats,
376 gettext_noop ("# client searches active"), 1,
378 cr = GNUNET_new (struct ClientRequest);
380 GNUNET_CONTAINER_DLL_insert (lc->cr_head,
383 options = GSF_PRO_LOCAL_REQUEST;
384 if (0 != (SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY & ntohl (sm->options)))
385 options |= GSF_PRO_LOCAL_ONLY;
386 cr->pr = GSF_pending_request_create_ (options, type,
389 memcmp (&sm->target, &all_zeros,
390 sizeof (struct GNUNET_PeerIdentity)))
391 ? &sm->target : NULL, NULL, 0,
393 ntohl (sm->anonymity_level),
398 (const struct GNUNET_HashCode *) &sm[1], sc,
399 &client_response_handler, cr);
403 (SEARCH_MESSAGE_OPTION_CONTINUED & ntohl (sm->options))) ? GNUNET_NO :
409 * Transmit the given message by copying it to the target buffer
410 * "buf". "buf" will be NULL and "size" zero if the socket was closed
411 * for writing in the meantime. In that case, do nothing
412 * (the disconnect or shutdown handler will take care of the rest).
413 * If we were able to transmit messages and there are still more
414 * pending, ask core again for further calls to this function.
416 * @param cls closure, pointer to the `struct GSF_LocalClient`
417 * @param size number of bytes available in @a buf
418 * @param buf where the callee should write the message
419 * @return number of bytes written to @a buf
422 transmit_to_client (void *cls,
426 struct GSF_LocalClient *lc = cls;
428 struct ClientResponse *res;
435 while ((NULL != (res = lc->res_head)) && (res->msize <= size))
437 memcpy (&cbuf[msize], &res[1], res->msize);
440 GNUNET_CONTAINER_DLL_remove (lc->res_head, lc->res_tail, res);
445 GNUNET_SERVER_notify_transmit_ready (lc->client, res->msize,
446 GNUNET_TIME_UNIT_FOREVER_REL,
447 &transmit_to_client, lc);
453 * Transmit a message to the given local client as soon as possible.
454 * If the client disconnects before transmission, the message is
457 * @param lc recipient
458 * @param msg message to transmit to client
461 GSF_local_client_transmit_ (struct GSF_LocalClient *lc,
462 const struct GNUNET_MessageHeader *msg)
464 struct ClientResponse *res;
467 msize = ntohs (msg->size);
468 res = GNUNET_malloc (sizeof (struct ClientResponse) + msize);
474 GNUNET_CONTAINER_DLL_insert_tail (lc->res_head,
479 GNUNET_SERVER_notify_transmit_ready (lc->client, msize,
480 GNUNET_TIME_UNIT_FOREVER_REL,
481 &transmit_to_client, lc);
486 * A client disconnected from us. Tear down the local client
490 * @param client handle of the client
493 GSF_client_disconnect_handler_ (void *cls,
494 struct GNUNET_SERVER_Client *client)
496 struct GSF_LocalClient *pos;
497 struct ClientRequest *cr;
498 struct ClientResponse *res;
501 while ((pos != NULL) && (pos->client != client))
505 while (NULL != (cr = pos->cr_head))
507 if (NULL != cr->kill_task)
508 GNUNET_SCHEDULER_cancel (cr->kill_task);
509 client_request_destroy (cr, NULL);
511 while (NULL != (res = pos->res_head))
513 GNUNET_CONTAINER_DLL_remove (pos->res_head, pos->res_tail, res);
518 GNUNET_SERVER_notify_transmit_ready_cancel (pos->th);
521 GSF_handle_local_client_disconnect_ (pos);
522 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, pos);
527 /* end of gnunet-service-fs_lc.c */