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
197 client_response_handler (void *cls,
198 struct GSF_PendingRequest *pr,
199 struct GNUNET_TIME_Absolute expiration,
203 struct ClientRequest *cr = cls;
204 struct GSF_LocalClient *lc;
205 struct PutMessage *pm;
206 const struct GSF_PendingRequestData *prd;
211 /* ugh, request 'timed out' -- how can this be? */
215 GNUNET_STATISTICS_update (GSF_stats,
216 gettext_noop ("# replies received for local clients"),
219 prd = GSF_pending_request_get_data_ (pr);
220 GNUNET_assert (pr == cr->pr);
222 msize = sizeof (struct PutMessage) + data_len;
223 pm = GNUNET_malloc (msize);
224 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
225 pm->header.size = htons (msize);
226 pm->type = htonl (prd->type);
227 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
228 memcpy (&pm[1], data, data_len);
229 GSF_local_client_transmit_ (lc, &pm->header);
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
232 "Queued reply to query `%s' for local client\n",
233 GNUNET_h2s (&prd->query),
234 (unsigned int) prd->type);
240 * Handle START_SEARCH-message (search request from local client).
242 * @param client identification of the client
243 * @param message the actual message
244 * @return pending request handle for the request, NULL on error
246 struct GSF_PendingRequest *
247 GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
248 const struct GNUNET_MessageHeader *message)
250 static GNUNET_HashCode all_zeros;
251 const struct SearchMessage *sm;
252 struct GSF_LocalClient *lc;
253 struct ClientRequest *cr;
254 struct GSF_PendingRequestData *prd;
257 enum GNUNET_BLOCK_Type type;
258 enum GSF_PendingRequestOptions options;
260 msize = ntohs (message->size);
261 if ( (msize < sizeof (struct SearchMessage)) ||
262 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
265 GNUNET_SERVER_receive_done (client,
269 GNUNET_STATISTICS_update (GSF_stats,
270 gettext_noop ("# client searches received"),
273 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
274 sm = (const struct SearchMessage*) message;
275 type = ntohl (sm->type);
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278 "Received request for `%s' of type %u from local client\n",
279 GNUNET_h2s (&sm->query),
280 (unsigned int) type);
282 lc = GSF_local_client_lookup_ (client);
284 /* detect duplicate KBLOCK requests */
285 if ( (type == GNUNET_BLOCK_TYPE_FS_KBLOCK) ||
286 (type == GNUNET_BLOCK_TYPE_FS_NBLOCK) ||
287 (type == GNUNET_BLOCK_TYPE_ANY) )
289 /* FIXME: this does currently not work to filter duplicate
290 results from *local* datastore since the local store is
291 queried before we continue to process additional
292 messages from the client! -- fix protocol? */
296 prd = GSF_pending_request_get_data_ (cr->pr);
297 if ( (0 != memcmp (&prd->query,
299 sizeof (GNUNET_HashCode))) &&
300 (prd->type == type) )
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308 "Have existing request, merging content-seen lists.\n");
310 GSF_pending_request_update_ (cr->pr,
311 (const GNUNET_HashCode*) &sm[1],
313 GNUNET_STATISTICS_update (GSF_stats,
314 gettext_noop ("# client searches updated (merged content seen list)"),
317 GNUNET_SERVER_receive_done (client,
323 GNUNET_STATISTICS_update (GSF_stats,
324 gettext_noop ("# client searches active"),
327 cr = GNUNET_malloc (sizeof (struct ClientRequest));
329 GNUNET_CONTAINER_DLL_insert (lc->cr_head,
332 options = GSF_PRO_LOCAL_REQUEST;
333 if (0 != (1 & ntohl (sm->options)))
334 options |= GSF_PRO_LOCAL_ONLY;
335 cr->pr = GSF_pending_request_create_ (options,
338 (type == GNUNET_BLOCK_TYPE_FS_SBLOCK)
339 ? &sm->target /* namespace */
341 (0 != memcmp (&sm->target,
343 sizeof (GNUNET_HashCode)))
344 ? (const struct GNUNET_PeerIdentity*) &sm->target
347 ntohl (sm->anonymity_level),
351 (const GNUNET_HashCode*) &sm[1], sc,
352 &client_response_handler,
359 * Transmit the given message by copying it to the target buffer
360 * "buf". "buf" will be NULL and "size" zero if the socket was closed
361 * for writing in the meantime. In that case, do nothing
362 * (the disconnect or shutdown handler will take care of the rest).
363 * If we were able to transmit messages and there are still more
364 * pending, ask core again for further calls to this function.
366 * @param cls closure, pointer to the 'struct GSF_LocalClient'
367 * @param size number of bytes available in buf
368 * @param buf where the callee should write the message
369 * @return number of bytes written to buf
372 transmit_to_client (void *cls,
376 struct GSF_LocalClient *lc = cls;
378 struct ClientResponse *res;
385 while ( (NULL != (res = lc->res_head) ) &&
386 (res->msize <= size) )
388 memcpy (&cbuf[msize], &res[1], res->msize);
391 GNUNET_CONTAINER_DLL_remove (lc->res_head,
397 lc->th = GNUNET_SERVER_notify_transmit_ready (lc->client,
399 GNUNET_TIME_UNIT_FOREVER_REL,
407 * Transmit a message to the given local client as soon as possible.
408 * If the client disconnects before transmission, the message is
411 * @param lc recipient
412 * @param msg message to transmit to client
415 GSF_local_client_transmit_ (struct GSF_LocalClient *lc,
416 const struct GNUNET_MessageHeader *msg)
418 struct ClientResponse *res;
421 msize = ntohs (msg->size);
422 res = GNUNET_malloc (sizeof (struct ClientResponse) + msize);
425 memcpy (&res[1], msg, msize);
426 GNUNET_CONTAINER_DLL_insert_tail (lc->res_head,
430 lc->th = GNUNET_SERVER_notify_transmit_ready (lc->client,
432 GNUNET_TIME_UNIT_FOREVER_REL,
439 * A client disconnected from us. Tear down the local client
443 * @param client handle of the client
446 GSF_client_disconnect_handler_ (void *cls,
447 struct GNUNET_SERVER_Client *client)
449 struct GSF_LocalClient *pos;
450 struct ClientRequest *cr;
451 struct ClientResponse *res;
454 while ( (pos != NULL) &&
455 (pos->client != client) )
459 while (NULL != (cr = pos->cr_head))
461 GNUNET_CONTAINER_DLL_remove (pos->cr_head,
464 GSF_pending_request_cancel_ (cr->pr);
465 GNUNET_STATISTICS_update (GSF_stats,
466 gettext_noop ("# client searches active"),
471 while (NULL != (res = pos->res_head))
473 GNUNET_CONTAINER_DLL_remove (pos->res_head,
480 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
483 GSF_handle_local_client_disconnect_ (pos);
484 GNUNET_CONTAINER_DLL_remove (client_head,
491 /* end of gnunet-service-fs_lc.c */