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_lc.h"
32 * Doubly-linked list of requests we are performing
33 * on behalf of the same client.
39 * This is a doubly-linked list.
41 struct ClientRequest *next;
44 * This is a doubly-linked list.
46 struct ClientRequest *prev;
49 * Request this entry represents.
51 struct GSF_PendingRequest *pr;
54 * Client list this request belongs to.
56 struct GSF_LocalClient *lc;
63 * Replies to be transmitted to the client. The actual
64 * response message is allocated after this struct.
69 * This is a doubly-linked list.
71 struct ClientResponse *next;
74 * This is a doubly-linked list.
76 struct ClientResponse *prev;
79 * Client list entry this response belongs to.
81 struct GSF_LocalClient *lc;
84 * Number of bytes in the response.
94 struct GSF_LocalClient
98 * We keep clients in a DLL.
100 struct GSF_LocalClient *next;
103 * We keep clients in a DLL.
105 struct GSF_LocalClient *prev;
110 struct GNUNET_SERVER_Client *client;
113 * Head of list of requests performed on behalf
114 * of this client right now.
116 struct ClientRequest *cr_head;
119 * Tail of list of requests performed on behalf
120 * of this client right now.
122 struct ClientRequest *cr_tail;
125 * Head of linked list of responses.
127 struct ClientResponse *res_head;
130 * Tail of linked list of responses.
132 struct ClientResponse *res_tail;
135 * Context for sending replies.
137 struct GNUNET_CONNECTION_TransmitHandle *th;
143 * Head of linked list of our local clients.
145 static struct GSF_LocalClient *client_head;
149 * Head of linked list of our local clients.
151 static struct GSF_LocalClient *client_tail;
155 * Look up a local client record or create one if it
158 * @param client handle of the client
159 * @return handle to local client entry
161 struct GSF_LocalClient *
162 GSF_local_client_lookup_ (struct GNUNET_SERVER_Client *client)
164 struct GSF_LocalClient *pos;
167 while ( (pos != NULL) &&
168 (pos->client != client) )
172 pos = GNUNET_malloc (sizeof (struct GSF_LocalClient));
173 pos->client = client;
174 GNUNET_CONTAINER_DLL_insert (client_head,
182 * Handle a reply to a pending request. Also called if a request
183 * expires (then with data == NULL). The handler may be called
184 * many times (depending on the request type), but will not be
185 * called during or after a call to GSF_pending_request_cancel
186 * and will also not be called anymore after a call signalling
189 * @param cls user-specified closure
190 * @param pr handle to the original pending request
191 * @param data response data, NULL on request expiration
192 * @param data_len number of bytes in data
195 client_response_handler (void *cls,
196 struct GSF_PendingRequest *pr,
200 /* FIXME: adapt old code below to new API! */
202 GNUNET_STATISTICS_update (stats,
203 gettext_noop ("# replies received for local clients"),
206 cl = pr->client_request_list->client_list;
207 msize = sizeof (struct PutMessage) + prq->size;
208 creply = GNUNET_malloc (msize + sizeof (struct ClientResponseMessage));
209 creply->msize = msize;
210 creply->client_list = cl;
211 GNUNET_CONTAINER_DLL_insert_after (cl->res_head,
215 pm = (struct PutMessage*) &creply[1];
216 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
217 pm->header.size = htons (msize);
218 pm->type = htonl (prq->type);
219 pm->expiration = GNUNET_TIME_absolute_hton (prq->expiration);
220 memcpy (&pm[1], prq->data, prq->size);
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225 "Transmitting result for query `%s' to client\n",
228 cl->th = GNUNET_SERVER_notify_transmit_ready (cl->client,
230 GNUNET_TIME_UNIT_FOREVER_REL,
234 GNUNET_break (cl->th != NULL);
237 prq->finished = GNUNET_YES;
238 destroy_pending_request (pr);
246 * Handle START_SEARCH-message (search request from local client).
248 * @param client identification of the client
249 * @param message the actual message
250 * @return pending request handle for the request, NULL on error
252 struct GSF_PendingRequest *
253 GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
254 const struct GNUNET_MessageHeader *message)
256 static GNUNET_HashCode all_zeros;
257 const struct SearchMessage *sm;
258 struct GSF_LocalClient *lc;
259 struct ClientRequest *cr;
260 struct GSF_PendingRequest *pr;
263 enum GNUNET_BLOCK_Type type;
264 enum GSF_PendingRequestOptions options;
266 msize = ntohs (message->size);
267 if ( (msize < sizeof (struct SearchMessage)) ||
268 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
271 GNUNET_SERVER_receive_done (client,
275 GNUNET_STATISTICS_update (stats,
276 gettext_noop ("# client searches received"),
279 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
280 sm = (const struct SearchMessage*) message;
281 type = ntohl (sm->type);
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284 "Received request for `%s' of type %u from local client\n",
285 GNUNET_h2s (&sm->query),
286 (unsigned int) type);
288 lc = GSF_local_client_lookup_ (client);
290 /* detect duplicate KBLOCK requests */
291 if ( (type == GNUNET_BLOCK_TYPE_FS_KBLOCK) ||
292 (type == GNUNET_BLOCK_TYPE_FS_NBLOCK) ||
293 (type == GNUNET_BLOCK_TYPE_ANY) )
296 while ( (cl != NULL) &&
297 ( (0 != memcmp (GSF_pending_request_get_query_ (cr->pr),
299 sizeof (GNUNET_HashCode))) ||
300 (GSF_pending_request_get_type_ (cr->pr) != type) ) )
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
306 "Have existing request, merging content-seen lists.\n");
308 GSF_pending_request_update_ (cr->pr,
311 GNUNET_STATISTICS_update (stats,
312 gettext_noop ("# client searches updated (merged content seen list)"),
315 GNUNET_SERVER_receive_done (client,
321 GNUNET_STATISTICS_update (stats,
322 gettext_noop ("# client searches active"),
325 cr = GNUNET_malloc (sizeof (struct ClientRequest));
327 GNUNET_CONTAINER_DLL_insert (lc->cr_head,
330 options = GSF_PRO_LOCAL_REQUEST;
331 if (0 != (1 & ntohl (sm->options)))
332 options |= GSF_PRO_LOCAL_ONLY;
333 cr->pr = GSF_pending_request_create_ (options,
336 (type == GNUNET_BLOCK_TYPE_SBLOCK)
337 ? &sm->target /* namespace */
339 (0 != memcmp (&sm->target,
341 sizeof (GNUNET_HashCode)))
345 ntohl (sm->anonymity_level),
348 &client_response_handler,
355 * Transmit the given message by copying it to the target buffer
356 * "buf". "buf" will be NULL and "size" zero if the socket was closed
357 * for writing in the meantime. In that case, do nothing
358 * (the disconnect or shutdown handler will take care of the rest).
359 * If we were able to transmit messages and there are still more
360 * pending, ask core again for further calls to this function.
362 * @param cls closure, pointer to the 'struct GSF_LocalClient'
363 * @param size number of bytes available in buf
364 * @param buf where the callee should write the message
365 * @return number of bytes written to buf
368 transmit_to_client (void *cls,
372 struct GSF_LocalClient *lc = cls;
374 struct ClientResponse *res;
381 while ( (NULL != (res = lc->res_head) ) &&
382 (res->msize <= size) )
384 memcpy (&cbuf[msize], &res[1], res->msize);
387 GNUNET_CONTAINER_DLL_remove (cl->res_head,
393 lc->th = GNUNET_SERVER_notify_transmit_ready (lc->client,
395 GNUNET_TIME_UNIT_FOREVER_REL,
403 * Transmit a message to the given local client as soon as possible.
404 * If the client disconnects before transmission, the message is
407 * @param lc recipient
408 * @param msg message to transmit to client
411 GSF_local_client_transmit_ (struct GSF_LocalClient *lc,
412 const struct GNUNET_MessageHeader *msg)
414 struct ClientResponse *res;
417 msize = ntohs (msg->size);
418 res = GNUNET_malloc (sizeof (struct ClientResponse) + msize);
421 GNUNET_CONTAINER_DLL_insert_tail (lc->res_head,
425 lc->tc = GNUNET_CLIENT_notify_transmit_ready (lc->client,
427 GNUNET_TIME_UNIT_FOREVER_REL,
435 * A client disconnected from us. Tear down the local client
439 * @param client handle of the client
442 GSF_client_disconnect_handler_ (void *cls,
443 const struct GNUNET_SERVER_Client *client)
445 struct GSF_LocalClient *pos;
446 struct DisconnectCallback *dc;
447 struct ClientRequest *cr;
448 struct ClientResponse *res;
451 while ( (pos != NULL) &&
452 (pos->client != client) )
456 while (NULL != (cr = pos->cr_head))
458 GNUNET_CONTAINER_DLL_remove (pos->cr_head,
461 GSF_pending_request_cancel_ (cr->pr);
464 while (NULL != (res = pos->res_head))
466 GNUNET_CONTAINER_DLL_remove (pos->res_head,
473 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
476 GSF_handle_local_client_disconnect_ (pos);
481 /* end of gnunet-service-fs_lc.c */