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;
62 * Replies to be transmitted to the client. The actual
63 * response message is allocated after this struct.
68 * This is a doubly-linked list.
70 struct ClientResponse *next;
73 * This is a doubly-linked list.
75 struct ClientResponse *prev;
78 * Client list entry this response belongs to.
80 struct GSF_LocalClient *lc;
83 * Number of bytes in the response.
92 struct GSF_LocalClient
96 * We keep clients in a DLL.
98 struct GSF_LocalClient *next;
101 * We keep clients in a DLL.
103 struct GSF_LocalClient *prev;
108 struct GNUNET_SERVER_Client *client;
111 * Head of list of requests performed on behalf
112 * of this client right now.
114 struct ClientRequest *cr_head;
117 * Tail of list of requests performed on behalf
118 * of this client right now.
120 struct ClientRequest *cr_tail;
123 * Head of linked list of responses.
125 struct ClientResponse *res_head;
128 * Tail of linked list of responses.
130 struct ClientResponse *res_tail;
133 * Context for sending replies.
135 struct GNUNET_CONNECTION_TransmitHandle *th;
141 * Head of linked list of our local clients.
143 static struct GSF_LocalClient *client_head;
147 * Head of linked list of our local clients.
149 static struct GSF_LocalClient *client_tail;
153 * Look up a local client record or create one if it
156 * @param client handle of the client
157 * @return handle to local client entry
159 struct GSF_LocalClient *
160 GSF_local_client_lookup_ (struct GNUNET_SERVER_Client *client)
162 struct GSF_LocalClient *pos;
165 while ( (pos != NULL) &&
166 (pos->client != client) )
170 pos = GNUNET_malloc (sizeof (struct GSF_LocalClient));
171 pos->client = client;
172 GNUNET_CONTAINER_DLL_insert (client_head,
180 * Handle a reply to a pending request. Also called if a request
181 * expires (then with data == NULL). The handler may be called
182 * many times (depending on the request type), but will not be
183 * called during or after a call to GSF_pending_request_cancel
184 * and will also not be called anymore after a call signalling
187 * @param cls user-specified closure
188 * @param pr handle to the original pending request
189 * @param expiration when does 'data' expire?
190 * @param data response data, NULL on request expiration
191 * @param data_len number of bytes in data
192 * @param more GNUNET_YES if the request remains active (may call
193 * this function again), GNUNET_NO if the request is
194 * finished (client must not call GSF_pending_request_cancel_)
197 client_response_handler (void *cls,
198 struct GSF_PendingRequest *pr,
199 struct GNUNET_TIME_Absolute expiration,
204 struct ClientRequest *cr = cls;
205 struct GSF_LocalClient *lc;
206 struct PutMessage *pm;
207 const struct GSF_PendingRequestData *prd;
211 /* ugh, request 'timed out' -- how can this be? */
213 GNUNET_assert (GNUNET_NO == more);
216 GNUNET_STATISTICS_update (stats,
217 gettext_noop ("# replies received for local clients"),
220 prd = GSF_pending_request_get_data_ (pr);
221 GNUNET_assert (pr == cr->pr);
223 pm = GNUNET_malloc (sizeof (PutMessage) + data_len);
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 (prq->expiration);
228 memcpy (&pm[1], data, data_len);
229 GSF_local_client_transmit_ (lc, &pm->header);
231 if (GNUNET_NO == more)
233 GNUNET_CONTAINER_DLL_remove (lc->cr_head,
242 * Handle START_SEARCH-message (search request from local client).
244 * @param client identification of the client
245 * @param message the actual message
246 * @return pending request handle for the request, NULL on error
248 struct GSF_PendingRequest *
249 GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
250 const struct GNUNET_MessageHeader *message)
252 static GNUNET_HashCode all_zeros;
253 const struct SearchMessage *sm;
254 struct GSF_LocalClient *lc;
255 struct ClientRequest *cr;
256 struct GSF_PendingRequest *pr;
259 enum GNUNET_BLOCK_Type type;
260 enum GSF_PendingRequestOptions options;
262 msize = ntohs (message->size);
263 if ( (msize < sizeof (struct SearchMessage)) ||
264 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
267 GNUNET_SERVER_receive_done (client,
271 GNUNET_STATISTICS_update (stats,
272 gettext_noop ("# client searches received"),
275 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
276 sm = (const struct SearchMessage*) message;
277 type = ntohl (sm->type);
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280 "Received request for `%s' of type %u from local client\n",
281 GNUNET_h2s (&sm->query),
282 (unsigned int) type);
284 lc = GSF_local_client_lookup_ (client);
286 /* detect duplicate KBLOCK requests */
287 if ( (type == GNUNET_BLOCK_TYPE_FS_KBLOCK) ||
288 (type == GNUNET_BLOCK_TYPE_FS_NBLOCK) ||
289 (type == GNUNET_BLOCK_TYPE_ANY) )
292 while ( (cl != NULL) &&
293 ( (0 != memcmp (GSF_pending_request_get_query_ (cr->pr),
295 sizeof (GNUNET_HashCode))) ||
296 (GSF_pending_request_get_type_ (cr->pr) != type) ) )
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "Have existing request, merging content-seen lists.\n");
304 GSF_pending_request_update_ (cr->pr,
307 GNUNET_STATISTICS_update (stats,
308 gettext_noop ("# client searches updated (merged content seen list)"),
311 GNUNET_SERVER_receive_done (client,
317 GNUNET_STATISTICS_update (stats,
318 gettext_noop ("# client searches active"),
321 cr = GNUNET_malloc (sizeof (struct ClientRequest));
323 GNUNET_CONTAINER_DLL_insert (lc->cr_head,
326 options = GSF_PRO_LOCAL_REQUEST;
327 if (0 != (1 & ntohl (sm->options)))
328 options |= GSF_PRO_LOCAL_ONLY;
329 cr->pr = GSF_pending_request_create_ (options,
332 (type == GNUNET_BLOCK_TYPE_SBLOCK)
333 ? &sm->target /* namespace */
335 (0 != memcmp (&sm->target,
337 sizeof (GNUNET_HashCode)))
341 ntohl (sm->anonymity_level),
344 &client_response_handler,
351 * Transmit the given message by copying it to the target buffer
352 * "buf". "buf" will be NULL and "size" zero if the socket was closed
353 * for writing in the meantime. In that case, do nothing
354 * (the disconnect or shutdown handler will take care of the rest).
355 * If we were able to transmit messages and there are still more
356 * pending, ask core again for further calls to this function.
358 * @param cls closure, pointer to the 'struct GSF_LocalClient'
359 * @param size number of bytes available in buf
360 * @param buf where the callee should write the message
361 * @return number of bytes written to buf
364 transmit_to_client (void *cls,
368 struct GSF_LocalClient *lc = cls;
370 struct ClientResponse *res;
377 while ( (NULL != (res = lc->res_head) ) &&
378 (res->msize <= size) )
380 memcpy (&cbuf[msize], &res[1], res->msize);
383 GNUNET_CONTAINER_DLL_remove (cl->res_head,
389 lc->th = GNUNET_SERVER_notify_transmit_ready (lc->client,
391 GNUNET_TIME_UNIT_FOREVER_REL,
399 * Transmit a message to the given local client as soon as possible.
400 * If the client disconnects before transmission, the message is
403 * @param lc recipient
404 * @param msg message to transmit to client
407 GSF_local_client_transmit_ (struct GSF_LocalClient *lc,
408 const struct GNUNET_MessageHeader *msg)
410 struct ClientResponse *res;
413 msize = ntohs (msg->size);
414 res = GNUNET_malloc (sizeof (struct ClientResponse) + msize);
417 memcpy (&res[1], msg, msize);
418 GNUNET_CONTAINER_DLL_insert_tail (lc->res_head,
422 lc->tc = GNUNET_CLIENT_notify_transmit_ready (lc->client,
424 GNUNET_TIME_UNIT_FOREVER_REL,
432 * A client disconnected from us. Tear down the local client
436 * @param client handle of the client
439 GSF_client_disconnect_handler_ (void *cls,
440 const struct GNUNET_SERVER_Client *client)
442 struct GSF_LocalClient *pos;
443 struct DisconnectCallback *dc;
444 struct ClientRequest *cr;
445 struct ClientResponse *res;
448 while ( (pos != NULL) &&
449 (pos->client != client) )
453 while (NULL != (cr = pos->cr_head))
455 GNUNET_CONTAINER_DLL_remove (pos->cr_head,
458 GSF_pending_request_cancel_ (cr->pr);
461 while (NULL != (res = pos->res_head))
463 GNUNET_CONTAINER_DLL_remove (pos->res_head,
470 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
473 GSF_handle_local_client_disconnect_ (pos);
478 /* end of gnunet-service-fs_lc.c */