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_CONNECTION_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) &&
174 (pos->client != client) )
178 pos = GNUNET_malloc (sizeof (struct GSF_LocalClient));
179 pos->client = client;
180 GNUNET_CONTAINER_DLL_insert (client_head,
188 * Free the given client request.
190 * @param cls the client request to free
191 * @param tc task context
194 client_request_destroy (void *cls,
195 const struct GNUNET_SCHEDULER_TaskContext *tc)
197 struct ClientRequest *cr = cls;
198 struct GSF_LocalClient *lc;
200 cr->kill_task = GNUNET_SCHEDULER_NO_TASK;
202 GNUNET_CONTAINER_DLL_remove (lc->cr_head,
205 GSF_pending_request_cancel_ (cr->pr);
206 GNUNET_STATISTICS_update (GSF_stats,
207 gettext_noop ("# client searches active"),
215 * Handle a reply to a pending request. Also called if a request
216 * expires (then with data == NULL). The handler may be called
217 * many times (depending on the request type), but will not be
218 * called during or after a call to GSF_pending_request_cancel
219 * and will also not be called anymore after a call signalling
222 * @param cls user-specified closure
223 * @param eval evaluation of the result
224 * @param pr handle to the original pending request
225 * @param reply_anonymity_level anonymity level for the reply, UINT32_MAX for "unknown"
226 * @param expiration when does 'data' expire?
227 * @param type type of the block
228 * @param data response data, NULL on request expiration
229 * @param data_len number of bytes in data
232 client_response_handler (void *cls,
233 enum GNUNET_BLOCK_EvaluationResult eval,
234 struct GSF_PendingRequest *pr,
235 uint32_t reply_anonymity_level,
236 struct GNUNET_TIME_Absolute expiration,
237 enum GNUNET_BLOCK_Type type,
241 struct ClientRequest *cr = cls;
242 struct GSF_LocalClient *lc;
243 struct PutMessage *pm;
244 const struct GSF_PendingRequestData *prd;
249 /* ugh, request 'timed out' -- how can this be? */
253 prd = GSF_pending_request_get_data_ (pr);
254 GNUNET_break (type != GNUNET_BLOCK_TYPE_ANY);
255 if ( (prd->type != type) &&
256 (prd->type != GNUNET_BLOCK_TYPE_ANY) )
261 GNUNET_STATISTICS_update (GSF_stats,
262 gettext_noop ("# replies received for local clients"),
265 GNUNET_assert (pr == cr->pr);
267 msize = sizeof (struct PutMessage) + data_len;
271 pm = (struct PutMessage*) buf;
272 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
273 pm->header.size = htons (msize);
274 pm->type = htonl (type);
275 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
276 memcpy (&pm[1], data, data_len);
277 GSF_local_client_transmit_ (lc, &pm->header);
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Queued reply to query `%s' for local client\n",
282 GNUNET_h2s (&prd->query),
283 (unsigned int) prd->type);
285 if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST)
287 cr->kill_task = GNUNET_SCHEDULER_add_now (&client_request_destroy,
293 * Handle START_SEARCH-message (search request from local client).
295 * @param client identification of the client
296 * @param message the actual message
297 * @return pending request handle for the request, NULL on error
299 struct GSF_PendingRequest *
300 GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
301 const struct GNUNET_MessageHeader *message)
303 static GNUNET_HashCode all_zeros;
304 const struct SearchMessage *sm;
305 struct GSF_LocalClient *lc;
306 struct ClientRequest *cr;
307 struct GSF_PendingRequestData *prd;
310 enum GNUNET_BLOCK_Type type;
311 enum GSF_PendingRequestOptions options;
313 msize = ntohs (message->size);
314 if ( (msize < sizeof (struct SearchMessage)) ||
315 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
318 GNUNET_SERVER_receive_done (client,
322 GNUNET_STATISTICS_update (GSF_stats,
323 gettext_noop ("# client searches received"),
326 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
327 sm = (const struct SearchMessage*) message;
328 type = ntohl (sm->type);
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331 "Received request for `%s' of type %u from local client\n",
332 GNUNET_h2s (&sm->query),
333 (unsigned int) type);
335 lc = GSF_local_client_lookup_ (client);
337 /* detect duplicate KBLOCK requests */
338 if ( (type == GNUNET_BLOCK_TYPE_FS_KBLOCK) ||
339 (type == GNUNET_BLOCK_TYPE_FS_NBLOCK) ||
340 (type == GNUNET_BLOCK_TYPE_ANY) )
342 /* FIXME: this does currently not work to filter duplicate
343 results from *local* datastore since the local store is
344 queried before we continue to process additional
345 messages from the client! -- fix protocol? */
349 prd = GSF_pending_request_get_data_ (cr->pr);
350 if ( (0 != memcmp (&prd->query,
352 sizeof (GNUNET_HashCode))) &&
353 (prd->type == type) )
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361 "Have existing request, merging content-seen lists.\n");
363 GSF_pending_request_update_ (cr->pr,
364 (const GNUNET_HashCode*) &sm[1],
366 GNUNET_STATISTICS_update (GSF_stats,
367 gettext_noop ("# client searches updated (merged content seen list)"),
370 GNUNET_SERVER_receive_done (client,
376 GNUNET_STATISTICS_update (GSF_stats,
377 gettext_noop ("# client searches active"),
380 cr = GNUNET_malloc (sizeof (struct ClientRequest));
382 GNUNET_CONTAINER_DLL_insert (lc->cr_head,
385 options = GSF_PRO_LOCAL_REQUEST;
386 if (0 != (1 & ntohl (sm->options)))
387 options |= GSF_PRO_LOCAL_ONLY;
388 cr->pr = GSF_pending_request_create_ (options,
391 (type == GNUNET_BLOCK_TYPE_FS_SBLOCK)
392 ? &sm->target /* namespace */
394 (0 != memcmp (&sm->target,
396 sizeof (GNUNET_HashCode)))
397 ? (const struct GNUNET_PeerIdentity*) &sm->target
400 ntohl (sm->anonymity_level),
404 (const GNUNET_HashCode*) &sm[1], sc,
405 &client_response_handler,
412 * Transmit the given message by copying it to the target buffer
413 * "buf". "buf" will be NULL and "size" zero if the socket was closed
414 * for writing in the meantime. In that case, do nothing
415 * (the disconnect or shutdown handler will take care of the rest).
416 * If we were able to transmit messages and there are still more
417 * pending, ask core again for further calls to this function.
419 * @param cls closure, pointer to the 'struct GSF_LocalClient'
420 * @param size number of bytes available in buf
421 * @param buf where the callee should write the message
422 * @return number of bytes written to buf
425 transmit_to_client (void *cls,
429 struct GSF_LocalClient *lc = cls;
431 struct ClientResponse *res;
438 while ( (NULL != (res = lc->res_head) ) &&
439 (res->msize <= size) )
441 memcpy (&cbuf[msize], &res[1], res->msize);
444 GNUNET_CONTAINER_DLL_remove (lc->res_head,
450 lc->th = GNUNET_SERVER_notify_transmit_ready (lc->client,
452 GNUNET_TIME_UNIT_FOREVER_REL,
460 * Transmit a message to the given local client as soon as possible.
461 * If the client disconnects before transmission, the message is
464 * @param lc recipient
465 * @param msg message to transmit to client
468 GSF_local_client_transmit_ (struct GSF_LocalClient *lc,
469 const struct GNUNET_MessageHeader *msg)
471 struct ClientResponse *res;
474 msize = ntohs (msg->size);
475 res = GNUNET_malloc (sizeof (struct ClientResponse) + msize);
478 memcpy (&res[1], msg, msize);
479 GNUNET_CONTAINER_DLL_insert_tail (lc->res_head,
483 lc->th = GNUNET_SERVER_notify_transmit_ready (lc->client,
485 GNUNET_TIME_UNIT_FOREVER_REL,
492 * A client disconnected from us. Tear down the local client
496 * @param client handle of the client
499 GSF_client_disconnect_handler_ (void *cls,
500 struct GNUNET_SERVER_Client *client)
502 struct GSF_LocalClient *pos;
503 struct ClientRequest *cr;
504 struct ClientResponse *res;
507 while ( (pos != NULL) &&
508 (pos->client != client) )
512 while (NULL != (cr = pos->cr_head))
514 GNUNET_CONTAINER_DLL_remove (pos->cr_head,
517 GSF_pending_request_cancel_ (cr->pr);
518 GNUNET_STATISTICS_update (GSF_stats,
519 gettext_noop ("# client searches active"),
522 if (GNUNET_SCHEDULER_NO_TASK != cr->kill_task)
523 GNUNET_SCHEDULER_cancel (cr->kill_task);
526 while (NULL != (res = pos->res_head))
528 GNUNET_CONTAINER_DLL_remove (pos->res_head,
535 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
538 GSF_handle_local_client_disconnect_ (pos);
539 GNUNET_CONTAINER_DLL_remove (client_head,
546 /* end of gnunet-service-fs_lc.c */