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 'connected peers'
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;
65 * Replies to be transmitted to the client. The actual
66 * response message is allocated after this struct.
71 * This is a doubly-linked list.
73 struct ClientResponse *next;
76 * This is a doubly-linked list.
78 struct ClientResponse *prev;
81 * Client list entry this response belongs to.
83 struct GSF_LocalClient *lc;
86 * Number of bytes in the response.
95 struct GSF_LocalClient
99 * We keep clients in a DLL.
101 struct GSF_LocalClient *next;
104 * We keep clients in a DLL.
106 struct GSF_LocalClient *prev;
111 struct GNUNET_SERVER_Client *client;
114 * Head of list of requests performed on behalf
115 * of this client right now.
117 struct ClientRequest *cr_head;
120 * Tail of list of requests performed on behalf
121 * of this client right now.
123 struct ClientRequest *cr_tail;
126 * Head of linked list of responses.
128 struct ClientResponse *res_head;
131 * Tail of linked list of responses.
133 struct ClientResponse *res_tail;
136 * Context for sending replies.
138 struct GNUNET_CONNECTION_TransmitHandle *th;
144 * Head of linked list of our local clients.
146 static struct GSF_LocalClient *client_head;
150 * Head of linked list of our local clients.
152 static struct GSF_LocalClient *client_tail;
156 * Look up a local client record or create one if it
159 * @param client handle of the client
160 * @return handle to local client entry
162 struct GSF_LocalClient *
163 GSF_local_client_lookup_ (struct GNUNET_SERVER_Client *client)
165 struct GSF_LocalClient *pos;
168 while ( (pos != NULL) &&
169 (pos->client != client) )
173 pos = GNUNET_malloc (sizeof (struct GSF_LocalClient));
174 pos->client = client;
175 GNUNET_CONTAINER_DLL_insert (client_head,
183 * Handle a reply to a pending request. Also called if a request
184 * expires (then with data == NULL). The handler may be called
185 * many times (depending on the request type), but will not be
186 * called during or after a call to GSF_pending_request_cancel
187 * and will also not be called anymore after a call signalling
190 * @param cls user-specified closure
191 * @param pr handle to the original pending request
192 * @param expiration when does 'data' expire?
193 * @param type type of the block
194 * @param data response data, NULL on request expiration
195 * @param data_len number of bytes in data
198 client_response_handler (void *cls,
199 struct GSF_PendingRequest *pr,
200 struct GNUNET_TIME_Absolute expiration,
201 enum GNUNET_BLOCK_Type type,
205 struct ClientRequest *cr = cls;
206 struct GSF_LocalClient *lc;
207 struct PutMessage *pm;
208 const struct GSF_PendingRequestData *prd;
213 /* ugh, request 'timed out' -- how can this be? */
217 prd = GSF_pending_request_get_data_ (pr);
218 GNUNET_break (type != GNUNET_BLOCK_TYPE_ANY);
219 if ( (prd->type != type) &&
220 (prd->type != GNUNET_BLOCK_TYPE_ANY) )
225 GNUNET_STATISTICS_update (GSF_stats,
226 gettext_noop ("# replies received for local clients"),
229 GNUNET_assert (pr == cr->pr);
231 msize = sizeof (struct PutMessage) + data_len;
232 pm = GNUNET_malloc (msize);
233 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
234 pm->header.size = htons (msize);
235 pm->type = htonl (type);
236 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
237 memcpy (&pm[1], data, data_len);
238 GSF_local_client_transmit_ (lc, &pm->header);
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241 "Queued reply to query `%s' for local client\n",
242 GNUNET_h2s (&prd->query),
243 (unsigned int) prd->type);
249 * Handle START_SEARCH-message (search request from local client).
251 * @param client identification of the client
252 * @param message the actual message
253 * @return pending request handle for the request, NULL on error
255 struct GSF_PendingRequest *
256 GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
257 const struct GNUNET_MessageHeader *message)
259 static GNUNET_HashCode all_zeros;
260 const struct SearchMessage *sm;
261 struct GSF_LocalClient *lc;
262 struct ClientRequest *cr;
263 struct GSF_PendingRequestData *prd;
266 enum GNUNET_BLOCK_Type type;
267 enum GSF_PendingRequestOptions options;
269 msize = ntohs (message->size);
270 if ( (msize < sizeof (struct SearchMessage)) ||
271 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
274 GNUNET_SERVER_receive_done (client,
278 GNUNET_STATISTICS_update (GSF_stats,
279 gettext_noop ("# client searches received"),
282 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
283 sm = (const struct SearchMessage*) message;
284 type = ntohl (sm->type);
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
287 "Received request for `%s' of type %u from local client\n",
288 GNUNET_h2s (&sm->query),
289 (unsigned int) type);
291 lc = GSF_local_client_lookup_ (client);
293 /* detect duplicate KBLOCK requests */
294 if ( (type == GNUNET_BLOCK_TYPE_FS_KBLOCK) ||
295 (type == GNUNET_BLOCK_TYPE_FS_NBLOCK) ||
296 (type == GNUNET_BLOCK_TYPE_ANY) )
298 /* FIXME: this does currently not work to filter duplicate
299 results from *local* datastore since the local store is
300 queried before we continue to process additional
301 messages from the client! -- fix protocol? */
305 prd = GSF_pending_request_get_data_ (cr->pr);
306 if ( (0 != memcmp (&prd->query,
308 sizeof (GNUNET_HashCode))) &&
309 (prd->type == type) )
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Have existing request, merging content-seen lists.\n");
319 GSF_pending_request_update_ (cr->pr,
320 (const GNUNET_HashCode*) &sm[1],
322 GNUNET_STATISTICS_update (GSF_stats,
323 gettext_noop ("# client searches updated (merged content seen list)"),
326 GNUNET_SERVER_receive_done (client,
332 GNUNET_STATISTICS_update (GSF_stats,
333 gettext_noop ("# client searches active"),
336 cr = GNUNET_malloc (sizeof (struct ClientRequest));
338 GNUNET_CONTAINER_DLL_insert (lc->cr_head,
341 options = GSF_PRO_LOCAL_REQUEST;
342 if (0 != (1 & ntohl (sm->options)))
343 options |= GSF_PRO_LOCAL_ONLY;
344 cr->pr = GSF_pending_request_create_ (options,
347 (type == GNUNET_BLOCK_TYPE_FS_SBLOCK)
348 ? &sm->target /* namespace */
350 (0 != memcmp (&sm->target,
352 sizeof (GNUNET_HashCode)))
353 ? (const struct GNUNET_PeerIdentity*) &sm->target
356 ntohl (sm->anonymity_level),
360 (const GNUNET_HashCode*) &sm[1], sc,
361 &client_response_handler,
368 * Transmit the given message by copying it to the target buffer
369 * "buf". "buf" will be NULL and "size" zero if the socket was closed
370 * for writing in the meantime. In that case, do nothing
371 * (the disconnect or shutdown handler will take care of the rest).
372 * If we were able to transmit messages and there are still more
373 * pending, ask core again for further calls to this function.
375 * @param cls closure, pointer to the 'struct GSF_LocalClient'
376 * @param size number of bytes available in buf
377 * @param buf where the callee should write the message
378 * @return number of bytes written to buf
381 transmit_to_client (void *cls,
385 struct GSF_LocalClient *lc = cls;
387 struct ClientResponse *res;
394 while ( (NULL != (res = lc->res_head) ) &&
395 (res->msize <= size) )
397 memcpy (&cbuf[msize], &res[1], res->msize);
400 GNUNET_CONTAINER_DLL_remove (lc->res_head,
406 lc->th = GNUNET_SERVER_notify_transmit_ready (lc->client,
408 GNUNET_TIME_UNIT_FOREVER_REL,
416 * Transmit a message to the given local client as soon as possible.
417 * If the client disconnects before transmission, the message is
420 * @param lc recipient
421 * @param msg message to transmit to client
424 GSF_local_client_transmit_ (struct GSF_LocalClient *lc,
425 const struct GNUNET_MessageHeader *msg)
427 struct ClientResponse *res;
430 msize = ntohs (msg->size);
431 res = GNUNET_malloc (sizeof (struct ClientResponse) + msize);
434 memcpy (&res[1], msg, msize);
435 GNUNET_CONTAINER_DLL_insert_tail (lc->res_head,
439 lc->th = GNUNET_SERVER_notify_transmit_ready (lc->client,
441 GNUNET_TIME_UNIT_FOREVER_REL,
448 * A client disconnected from us. Tear down the local client
452 * @param client handle of the client
455 GSF_client_disconnect_handler_ (void *cls,
456 struct GNUNET_SERVER_Client *client)
458 struct GSF_LocalClient *pos;
459 struct ClientRequest *cr;
460 struct ClientResponse *res;
463 while ( (pos != NULL) &&
464 (pos->client != client) )
468 while (NULL != (cr = pos->cr_head))
470 GNUNET_CONTAINER_DLL_remove (pos->cr_head,
473 GSF_pending_request_cancel_ (cr->pr);
474 GNUNET_STATISTICS_update (GSF_stats,
475 gettext_noop ("# client searches active"),
480 while (NULL != (res = pos->res_head))
482 GNUNET_CONTAINER_DLL_remove (pos->res_head,
489 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
492 GSF_handle_local_client_disconnect_ (pos);
493 GNUNET_CONTAINER_DLL_remove (client_head,
500 /* end of gnunet-service-fs_lc.c */