2 This file is part of GNUnet.
3 (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.
22 * @file fs/gnunet-service-fs_lc.c
23 * @brief API to handle 'local clients'
24 * @author Christian Grothoff
28 #include "gnunet-service-fs.h"
29 #include "gnunet-service-fs_lc.h"
30 #include "gnunet-service-fs_cp.h"
31 #include "gnunet-service-fs_pr.h"
35 * Doubly-linked list of requests we are performing
36 * on behalf of the same client.
42 * This is a doubly-linked list.
44 struct ClientRequest *next;
47 * This is a doubly-linked list.
49 struct ClientRequest *prev;
52 * Request this entry represents.
54 struct GSF_PendingRequest *pr;
57 * Client list this request belongs to.
59 struct GSF_LocalClient *lc;
62 * Task scheduled to destroy the request.
64 GNUNET_SCHEDULER_TaskIdentifier kill_task;
70 * Replies to be transmitted to the client. The actual
71 * response message is allocated after this struct.
76 * This is a doubly-linked list.
78 struct ClientResponse *next;
81 * This is a doubly-linked list.
83 struct ClientResponse *prev;
86 * Client list entry this response belongs to.
88 struct GSF_LocalClient *lc;
91 * Number of bytes in the response.
100 struct GSF_LocalClient
104 * We keep clients in a DLL.
106 struct GSF_LocalClient *next;
109 * We keep clients in a DLL.
111 struct GSF_LocalClient *prev;
116 struct GNUNET_SERVER_Client *client;
119 * Head of list of requests performed on behalf
120 * of this client right now.
122 struct ClientRequest *cr_head;
125 * Tail of list of requests performed on behalf
126 * of this client right now.
128 struct ClientRequest *cr_tail;
131 * Head of linked list of responses.
133 struct ClientResponse *res_head;
136 * Tail of linked list of responses.
138 struct ClientResponse *res_tail;
141 * Context for sending replies.
143 struct GNUNET_SERVER_TransmitHandle *th;
149 * Head of linked list of our local clients.
151 static struct GSF_LocalClient *client_head;
155 * Head of linked list of our local clients.
157 static struct GSF_LocalClient *client_tail;
161 * Look up a local client record or create one if it
164 * @param client handle of the client
165 * @return handle to local client entry
167 struct GSF_LocalClient *
168 GSF_local_client_lookup_ (struct GNUNET_SERVER_Client *client)
170 struct GSF_LocalClient *pos;
173 while ((pos != NULL) && (pos->client != client))
177 pos = GNUNET_malloc (sizeof (struct GSF_LocalClient));
178 pos->client = client;
179 GNUNET_CONTAINER_DLL_insert (client_head, client_tail, pos);
185 * Free the given client request.
187 * @param cls the client request to free
188 * @param tc task context
191 client_request_destroy (void *cls,
192 const struct GNUNET_SCHEDULER_TaskContext *tc)
194 struct ClientRequest *cr = cls;
195 struct GSF_LocalClient *lc;
197 cr->kill_task = GNUNET_SCHEDULER_NO_TASK;
199 GNUNET_CONTAINER_DLL_remove (lc->cr_head, lc->cr_tail, cr);
200 GSF_pending_request_cancel_ (cr->pr, GNUNET_NO);
201 GNUNET_STATISTICS_update (GSF_stats,
202 gettext_noop ("# client searches active"), -1,
209 * Handle a reply to a pending request. Also called if a request
210 * expires (then with data == NULL). The handler may be called
211 * many times (depending on the request type), but will not be
212 * called during or after a call to GSF_pending_request_cancel
213 * and will also not be called anymore after a call signalling
216 * @param cls user-specified closure
217 * @param eval evaluation of the result
218 * @param pr handle to the original pending request
219 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
220 * @param expiration when does 'data' expire?
221 * @param last_transmission when was the last time we've tried to download this block? (FOREVER if unknown)
222 * @param type type of the block
223 * @param data response data, NULL on request expiration
224 * @param data_len number of bytes in data
227 client_response_handler (void *cls, 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, const void *data,
235 struct ClientRequest *cr = cls;
236 struct GSF_LocalClient *lc;
237 struct ClientPutMessage *pm;
238 const struct GSF_PendingRequestData *prd;
243 /* ugh, request 'timed out' -- how can this be? */
247 prd = GSF_pending_request_get_data_ (pr);
248 GNUNET_break (type != GNUNET_BLOCK_TYPE_ANY);
249 if ((prd->type != type) && (prd->type != GNUNET_BLOCK_TYPE_ANY))
254 GNUNET_STATISTICS_update (GSF_stats,
256 ("# replies received for local clients"), 1,
258 GNUNET_assert (pr == cr->pr);
260 msize = sizeof (struct ClientPutMessage) + data_len;
262 char buf[msize] GNUNET_ALIGN;
264 pm = (struct ClientPutMessage *) buf;
265 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
266 pm->header.size = htons (msize);
267 pm->type = htonl (type);
268 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
269 pm->last_transmission = GNUNET_TIME_absolute_hton (last_transmission);
270 memcpy (&pm[1], data, data_len);
271 GSF_local_client_transmit_ (lc, &pm->header);
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274 "Queued reply to query `%s' for local client\n",
275 GNUNET_h2s (&prd->query), (unsigned int) prd->type);
276 if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
278 if (GNUNET_SCHEDULER_NO_TASK != cr->kill_task)
279 cr->kill_task = GNUNET_SCHEDULER_add_now (&client_request_destroy, cr);
284 * Handle START_SEARCH-message (search request from local client).
285 * Only responsible for creating the request entry itself and setting
286 * up reply callback and cancellation on client disconnect. Does NOT
287 * execute the actual request strategy (planning).
289 * @param client identification of the client
290 * @param message the actual message
291 * @param prptr where to store the pending request handle for the request
292 * @return GNUNET_YES to start local processing,
293 * GNUNET_NO to not (yet) start local processing,
294 * GNUNET_SYSERR on error
297 GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
298 const struct GNUNET_MessageHeader
300 struct GSF_PendingRequest **prptr)
302 static struct GNUNET_HashCode all_zeros;
303 const struct SearchMessage *sm;
304 struct GSF_LocalClient *lc;
305 struct ClientRequest *cr;
306 struct GSF_PendingRequestData *prd;
309 enum GNUNET_BLOCK_Type type;
310 enum GSF_PendingRequestOptions options;
312 msize = ntohs (message->size);
313 if ((msize < sizeof (struct SearchMessage)) ||
314 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (struct GNUNET_HashCode)))
318 return GNUNET_SYSERR;
320 GNUNET_STATISTICS_update (GSF_stats,
321 gettext_noop ("# client searches received"), 1,
323 sc = (msize - sizeof (struct SearchMessage)) / sizeof (struct GNUNET_HashCode);
324 sm = (const struct SearchMessage *) message;
325 type = ntohl (sm->type);
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327 "Received request for `%s' of type %u from local client\n",
328 GNUNET_h2s (&sm->query), (unsigned int) type);
329 lc = GSF_local_client_lookup_ (client);
331 /* detect duplicate KBLOCK requests */
332 if ((type == GNUNET_BLOCK_TYPE_FS_KBLOCK) ||
333 (type == GNUNET_BLOCK_TYPE_FS_NBLOCK) || (type == GNUNET_BLOCK_TYPE_ANY))
338 prd = GSF_pending_request_get_data_ (cr->pr);
339 /* only unify with queries that hae not yet started local processing
340 (SEARCH_MESSAGE_OPTION_CONTINUED was always set) and that have a
341 matching query and type */
342 if ((GNUNET_YES != prd->has_started) &&
343 (0 != memcmp (&prd->query, &sm->query, sizeof (struct GNUNET_HashCode))) &&
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Have existing request, merging content-seen lists.\n");
353 GSF_pending_request_update_ (cr->pr, (const struct GNUNET_HashCode *) &sm[1], sc);
354 GNUNET_STATISTICS_update (GSF_stats,
356 ("# client searches updated (merged content seen list)"),
361 GNUNET_STATISTICS_update (GSF_stats,
362 gettext_noop ("# client searches active"), 1,
364 cr = GNUNET_malloc (sizeof (struct ClientRequest));
366 GNUNET_CONTAINER_DLL_insert (lc->cr_head, lc->cr_tail, cr);
367 options = GSF_PRO_LOCAL_REQUEST;
368 if (0 != (SEARCH_MESSAGE_OPTION_LOOPBACK_ONLY & ntohl (sm->options)))
369 options |= GSF_PRO_LOCAL_ONLY;
370 cr->pr = GSF_pending_request_create_ (options, type, &sm->query, (type == GNUNET_BLOCK_TYPE_FS_SBLOCK) ? &sm->target /* namespace */
373 memcmp (&sm->target, &all_zeros,
374 sizeof (struct GNUNET_HashCode)))
375 ? (const struct GNUNET_PeerIdentity *)
376 &sm->target : NULL, NULL, 0,
378 ntohl (sm->anonymity_level),
383 (const struct GNUNET_HashCode *) &sm[1], sc,
384 &client_response_handler, cr);
388 (SEARCH_MESSAGE_OPTION_CONTINUED & ntohl (sm->options))) ? GNUNET_NO :
394 * Transmit the given message by copying it to the target buffer
395 * "buf". "buf" will be NULL and "size" zero if the socket was closed
396 * for writing in the meantime. In that case, do nothing
397 * (the disconnect or shutdown handler will take care of the rest).
398 * If we were able to transmit messages and there are still more
399 * pending, ask core again for further calls to this function.
401 * @param cls closure, pointer to the 'struct GSF_LocalClient'
402 * @param size number of bytes available in buf
403 * @param buf where the callee should write the message
404 * @return number of bytes written to buf
407 transmit_to_client (void *cls, size_t size, void *buf)
409 struct GSF_LocalClient *lc = cls;
411 struct ClientResponse *res;
418 while ((NULL != (res = lc->res_head)) && (res->msize <= size))
420 memcpy (&cbuf[msize], &res[1], res->msize);
423 GNUNET_CONTAINER_DLL_remove (lc->res_head, lc->res_tail, res);
428 GNUNET_SERVER_notify_transmit_ready (lc->client, res->msize,
429 GNUNET_TIME_UNIT_FOREVER_REL,
430 &transmit_to_client, lc);
436 * Transmit a message to the given local client as soon as possible.
437 * If the client disconnects before transmission, the message is
440 * @param lc recipient
441 * @param msg message to transmit to client
444 GSF_local_client_transmit_ (struct GSF_LocalClient *lc,
445 const struct GNUNET_MessageHeader *msg)
447 struct ClientResponse *res;
450 msize = ntohs (msg->size);
451 res = GNUNET_malloc (sizeof (struct ClientResponse) + msize);
454 memcpy (&res[1], msg, msize);
455 GNUNET_CONTAINER_DLL_insert_tail (lc->res_head, lc->res_tail, res);
458 GNUNET_SERVER_notify_transmit_ready (lc->client, msize,
459 GNUNET_TIME_UNIT_FOREVER_REL,
460 &transmit_to_client, lc);
465 * A client disconnected from us. Tear down the local client
469 * @param client handle of the client
472 GSF_client_disconnect_handler_ (void *cls, struct GNUNET_SERVER_Client *client)
474 struct GSF_LocalClient *pos;
475 struct ClientRequest *cr;
476 struct ClientResponse *res;
479 while ((pos != NULL) && (pos->client != client))
483 while (NULL != (cr = pos->cr_head))
485 GNUNET_CONTAINER_DLL_remove (pos->cr_head, pos->cr_tail, cr);
486 GSF_pending_request_cancel_ (cr->pr, GNUNET_NO);
487 GNUNET_STATISTICS_update (GSF_stats,
488 gettext_noop ("# client searches active"), -1,
490 if (GNUNET_SCHEDULER_NO_TASK != cr->kill_task)
491 GNUNET_SCHEDULER_cancel (cr->kill_task);
494 while (NULL != (res = pos->res_head))
496 GNUNET_CONTAINER_DLL_remove (pos->res_head, pos->res_tail, res);
501 GNUNET_SERVER_notify_transmit_ready_cancel (pos->th);
504 GSF_handle_local_client_disconnect_ (pos);
505 GNUNET_CONTAINER_DLL_remove (client_head, client_tail, pos);
510 /* end of gnunet-service-fs_lc.c */