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 data response data, NULL on request expiration
194 * @param data_len number of bytes in data
195 * @param more GNUNET_YES if the request remains active (may call
196 * this function again), GNUNET_NO if the request is
197 * finished (client must not call GSF_pending_request_cancel_)
200 client_response_handler (void *cls,
201 struct GSF_PendingRequest *pr,
202 struct GNUNET_TIME_Absolute expiration,
207 struct ClientRequest *cr = cls;
208 struct GSF_LocalClient *lc;
209 struct PutMessage *pm;
210 const struct GSF_PendingRequestData *prd;
215 /* ugh, request 'timed out' -- how can this be? */
217 GNUNET_assert (GNUNET_NO == more);
220 GNUNET_STATISTICS_update (GSF_stats,
221 gettext_noop ("# replies received for local clients"),
224 prd = GSF_pending_request_get_data_ (pr);
225 GNUNET_assert (pr == cr->pr);
227 msize = sizeof (struct PutMessage) + data_len;
228 pm = GNUNET_malloc (msize);
229 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
230 pm->header.size = htons (msize);
231 pm->type = htonl (prd->type);
232 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
233 memcpy (&pm[1], data, data_len);
234 GSF_local_client_transmit_ (lc, &pm->header);
236 if (GNUNET_NO == more)
238 GNUNET_CONTAINER_DLL_remove (lc->cr_head,
247 * Handle START_SEARCH-message (search request from local client).
249 * @param client identification of the client
250 * @param message the actual message
251 * @return pending request handle for the request, NULL on error
253 struct GSF_PendingRequest *
254 GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
255 const struct GNUNET_MessageHeader *message)
257 static GNUNET_HashCode all_zeros;
258 const struct SearchMessage *sm;
259 struct GSF_LocalClient *lc;
260 struct ClientRequest *cr;
261 struct GSF_PendingRequestData *prd;
264 enum GNUNET_BLOCK_Type type;
265 enum GSF_PendingRequestOptions options;
267 msize = ntohs (message->size);
268 if ( (msize < sizeof (struct SearchMessage)) ||
269 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
272 GNUNET_SERVER_receive_done (client,
276 GNUNET_STATISTICS_update (GSF_stats,
277 gettext_noop ("# client searches received"),
280 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
281 sm = (const struct SearchMessage*) message;
282 type = ntohl (sm->type);
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "Received request for `%s' of type %u from local client\n",
286 GNUNET_h2s (&sm->query),
287 (unsigned int) type);
289 lc = GSF_local_client_lookup_ (client);
291 /* detect duplicate KBLOCK requests */
292 if ( (type == GNUNET_BLOCK_TYPE_FS_KBLOCK) ||
293 (type == GNUNET_BLOCK_TYPE_FS_NBLOCK) ||
294 (type == GNUNET_BLOCK_TYPE_ANY) )
296 /* FIXME: this does currently not work to filter duplicate
297 results from *local* datastore since the local store is
298 queried before we continue to process additional
299 messages from the client! -- fix protocol? */
303 prd = GSF_pending_request_get_data_ (cr->pr);
304 if ( (0 != memcmp (&prd->query,
306 sizeof (GNUNET_HashCode))) &&
307 (prd->type == type) )
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
315 "Have existing request, merging content-seen lists.\n");
317 GSF_pending_request_update_ (cr->pr,
318 (const GNUNET_HashCode*) &sm[1],
320 GNUNET_STATISTICS_update (GSF_stats,
321 gettext_noop ("# client searches updated (merged content seen list)"),
324 GNUNET_SERVER_receive_done (client,
330 GNUNET_STATISTICS_update (GSF_stats,
331 gettext_noop ("# client searches active"),
334 cr = GNUNET_malloc (sizeof (struct ClientRequest));
336 GNUNET_CONTAINER_DLL_insert (lc->cr_head,
339 options = GSF_PRO_LOCAL_REQUEST;
340 if (0 != (1 & ntohl (sm->options)))
341 options |= GSF_PRO_LOCAL_ONLY;
342 cr->pr = GSF_pending_request_create_ (options,
345 (type == GNUNET_BLOCK_TYPE_FS_SBLOCK)
346 ? &sm->target /* namespace */
348 (0 != memcmp (&sm->target,
350 sizeof (GNUNET_HashCode)))
351 ? (const struct GNUNET_PeerIdentity*) &sm->target
354 ntohl (sm->anonymity_level),
358 (const GNUNET_HashCode*) &sm[1], sc,
359 &client_response_handler,
366 * Transmit the given message by copying it to the target buffer
367 * "buf". "buf" will be NULL and "size" zero if the socket was closed
368 * for writing in the meantime. In that case, do nothing
369 * (the disconnect or shutdown handler will take care of the rest).
370 * If we were able to transmit messages and there are still more
371 * pending, ask core again for further calls to this function.
373 * @param cls closure, pointer to the 'struct GSF_LocalClient'
374 * @param size number of bytes available in buf
375 * @param buf where the callee should write the message
376 * @return number of bytes written to buf
379 transmit_to_client (void *cls,
383 struct GSF_LocalClient *lc = cls;
385 struct ClientResponse *res;
392 while ( (NULL != (res = lc->res_head) ) &&
393 (res->msize <= size) )
395 memcpy (&cbuf[msize], &res[1], res->msize);
398 GNUNET_CONTAINER_DLL_remove (lc->res_head,
404 lc->th = GNUNET_SERVER_notify_transmit_ready (lc->client,
406 GNUNET_TIME_UNIT_FOREVER_REL,
414 * Transmit a message to the given local client as soon as possible.
415 * If the client disconnects before transmission, the message is
418 * @param lc recipient
419 * @param msg message to transmit to client
422 GSF_local_client_transmit_ (struct GSF_LocalClient *lc,
423 const struct GNUNET_MessageHeader *msg)
425 struct ClientResponse *res;
428 msize = ntohs (msg->size);
429 res = GNUNET_malloc (sizeof (struct ClientResponse) + msize);
432 memcpy (&res[1], msg, msize);
433 GNUNET_CONTAINER_DLL_insert_tail (lc->res_head,
437 lc->th = GNUNET_SERVER_notify_transmit_ready (lc->client,
439 GNUNET_TIME_UNIT_FOREVER_REL,
446 * A client disconnected from us. Tear down the local client
450 * @param client handle of the client
453 GSF_client_disconnect_handler_ (void *cls,
454 struct GNUNET_SERVER_Client *client)
456 struct GSF_LocalClient *pos;
457 struct ClientRequest *cr;
458 struct ClientResponse *res;
461 while ( (pos != NULL) &&
462 (pos->client != client) )
466 while (NULL != (cr = pos->cr_head))
468 GNUNET_CONTAINER_DLL_remove (pos->cr_head,
471 GSF_pending_request_cancel_ (cr->pr);
474 while (NULL != (res = pos->res_head))
476 GNUNET_CONTAINER_DLL_remove (pos->res_head,
483 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
486 GSF_handle_local_client_disconnect_ (pos);
487 GNUNET_CONTAINER_DLL_remove (client_head,
494 /* end of gnunet-service-fs_lc.c */