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"
29 #include "gnunet-service-fs_cp.h"
30 #include "gnunet-service-fs_pr.h"
34 * Doubly-linked list of requests we are performing
35 * on behalf of the same client.
41 * This is a doubly-linked list.
43 struct ClientRequest *next;
46 * This is a doubly-linked list.
48 struct ClientRequest *prev;
51 * Request this entry represents.
53 struct GSF_PendingRequest *pr;
56 * Client list this request belongs to.
58 struct GSF_LocalClient *lc;
64 * Replies to be transmitted to the client. The actual
65 * response message is allocated after this struct.
70 * This is a doubly-linked list.
72 struct ClientResponse *next;
75 * This is a doubly-linked list.
77 struct ClientResponse *prev;
80 * Client list entry this response belongs to.
82 struct GSF_LocalClient *lc;
85 * 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 expiration when does 'data' expire?
192 * @param data response data, NULL on request expiration
193 * @param data_len number of bytes in data
194 * @param more GNUNET_YES if the request remains active (may call
195 * this function again), GNUNET_NO if the request is
196 * finished (client must not call GSF_pending_request_cancel_)
199 client_response_handler (void *cls,
200 struct GSF_PendingRequest *pr,
201 struct GNUNET_TIME_Absolute expiration,
206 struct ClientRequest *cr = cls;
207 struct GSF_LocalClient *lc;
208 struct PutMessage *pm;
209 const struct GSF_PendingRequestData *prd;
214 /* ugh, request 'timed out' -- how can this be? */
216 GNUNET_assert (GNUNET_NO == more);
219 GNUNET_STATISTICS_update (GSF_stats,
220 gettext_noop ("# replies received for local clients"),
223 prd = GSF_pending_request_get_data_ (pr);
224 GNUNET_assert (pr == cr->pr);
226 msize = sizeof (struct PutMessage) + data_len;
227 pm = GNUNET_malloc (msize);
228 pm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_PUT);
229 pm->header.size = htons (msize);
230 pm->type = htonl (prd->type);
231 pm->expiration = GNUNET_TIME_absolute_hton (expiration);
232 memcpy (&pm[1], data, data_len);
233 GSF_local_client_transmit_ (lc, &pm->header);
235 if (GNUNET_NO == more)
237 GNUNET_CONTAINER_DLL_remove (lc->cr_head,
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_PendingRequestData *prd;
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 (GSF_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) )
298 prd = GSF_pending_request_get_data_ (cr->pr);
299 if ( (0 != memcmp (&prd->query,
301 sizeof (GNUNET_HashCode))) &&
302 (prd->type == type) )
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310 "Have existing request, merging content-seen lists.\n");
312 GSF_pending_request_update_ (cr->pr,
313 (const GNUNET_HashCode*) &sm[1],
315 GNUNET_STATISTICS_update (GSF_stats,
316 gettext_noop ("# client searches updated (merged content seen list)"),
319 GNUNET_SERVER_receive_done (client,
325 GNUNET_STATISTICS_update (GSF_stats,
326 gettext_noop ("# client searches active"),
329 cr = GNUNET_malloc (sizeof (struct ClientRequest));
331 GNUNET_CONTAINER_DLL_insert (lc->cr_head,
334 options = GSF_PRO_LOCAL_REQUEST;
335 if (0 != (1 & ntohl (sm->options)))
336 options |= GSF_PRO_LOCAL_ONLY;
337 cr->pr = GSF_pending_request_create_ (options,
340 (type == GNUNET_BLOCK_TYPE_FS_SBLOCK)
341 ? &sm->target /* namespace */
343 (0 != memcmp (&sm->target,
345 sizeof (GNUNET_HashCode)))
346 ? (const struct GNUNET_PeerIdentity*) &sm->target
349 ntohl (sm->anonymity_level),
353 (const GNUNET_HashCode*) &sm[1], sc,
354 &client_response_handler,
361 * Transmit the given message by copying it to the target buffer
362 * "buf". "buf" will be NULL and "size" zero if the socket was closed
363 * for writing in the meantime. In that case, do nothing
364 * (the disconnect or shutdown handler will take care of the rest).
365 * If we were able to transmit messages and there are still more
366 * pending, ask core again for further calls to this function.
368 * @param cls closure, pointer to the 'struct GSF_LocalClient'
369 * @param size number of bytes available in buf
370 * @param buf where the callee should write the message
371 * @return number of bytes written to buf
374 transmit_to_client (void *cls,
378 struct GSF_LocalClient *lc = cls;
380 struct ClientResponse *res;
387 while ( (NULL != (res = lc->res_head) ) &&
388 (res->msize <= size) )
390 memcpy (&cbuf[msize], &res[1], res->msize);
393 GNUNET_CONTAINER_DLL_remove (lc->res_head,
399 lc->th = GNUNET_SERVER_notify_transmit_ready (lc->client,
401 GNUNET_TIME_UNIT_FOREVER_REL,
409 * Transmit a message to the given local client as soon as possible.
410 * If the client disconnects before transmission, the message is
413 * @param lc recipient
414 * @param msg message to transmit to client
417 GSF_local_client_transmit_ (struct GSF_LocalClient *lc,
418 const struct GNUNET_MessageHeader *msg)
420 struct ClientResponse *res;
423 msize = ntohs (msg->size);
424 res = GNUNET_malloc (sizeof (struct ClientResponse) + msize);
427 memcpy (&res[1], msg, msize);
428 GNUNET_CONTAINER_DLL_insert_tail (lc->res_head,
432 lc->th = GNUNET_SERVER_notify_transmit_ready (lc->client,
434 GNUNET_TIME_UNIT_FOREVER_REL,
441 * A client disconnected from us. Tear down the local client
445 * @param client handle of the client
448 GSF_client_disconnect_handler_ (void *cls,
449 struct GNUNET_SERVER_Client *client)
451 struct GSF_LocalClient *pos;
452 struct ClientRequest *cr;
453 struct ClientResponse *res;
456 while ( (pos != NULL) &&
457 (pos->client != client) )
461 while (NULL != (cr = pos->cr_head))
463 GNUNET_CONTAINER_DLL_remove (pos->cr_head,
466 GSF_pending_request_cancel_ (cr->pr);
469 while (NULL != (res = pos->res_head))
471 GNUNET_CONTAINER_DLL_remove (pos->res_head,
478 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
481 GSF_handle_local_client_disconnect_ (pos);
486 /* end of gnunet-service-fs_lc.c */