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 START_SEARCH-message (search request from local client).
184 * @param client identification of the client
185 * @param message the actual message
186 * @return pending request handle for the request, NULL on error
188 struct GSF_PendingRequest *
189 GSF_local_client_start_search_handler_ (struct GNUNET_SERVER_Client *client,
190 const struct GNUNET_MessageHeader *message)
192 static GNUNET_HashCode all_zeros;
193 const struct SearchMessage *sm;
194 struct GSF_LocalClient *lc;
195 struct ClientRequest *cr;
196 struct GSF_PendingRequest *pr;
199 enum GNUNET_BLOCK_Type type;
200 enum GSF_PendingRequestOptions options;
202 msize = ntohs (message->size);
203 if ( (msize < sizeof (struct SearchMessage)) ||
204 (0 != (msize - sizeof (struct SearchMessage)) % sizeof (GNUNET_HashCode)) )
207 GNUNET_SERVER_receive_done (client,
211 GNUNET_STATISTICS_update (stats,
212 gettext_noop ("# client searches received"),
215 sc = (msize - sizeof (struct SearchMessage)) / sizeof (GNUNET_HashCode);
216 sm = (const struct SearchMessage*) message;
217 type = ntohl (sm->type);
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220 "Received request for `%s' of type %u from local client\n",
221 GNUNET_h2s (&sm->query),
222 (unsigned int) type);
224 lc = GSF_local_client_lookup_ (client);
226 /* detect duplicate KBLOCK requests */
227 if ( (type == GNUNET_BLOCK_TYPE_FS_KBLOCK) ||
228 (type == GNUNET_BLOCK_TYPE_FS_NBLOCK) ||
229 (type == GNUNET_BLOCK_TYPE_ANY) )
232 while ( (cl != NULL) &&
233 ( (0 != memcmp (GSF_pending_request_get_query_ (cr->pr),
235 sizeof (GNUNET_HashCode))) ||
236 (GSF_pending_request_get_type_ (cr->pr) != type) ) )
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242 "Have existing request, merging content-seen lists.\n");
244 GSF_pending_request_update_ (cr->pr,
247 GNUNET_STATISTICS_update (stats,
248 gettext_noop ("# client searches updated (merged content seen list)"),
251 GNUNET_SERVER_receive_done (client,
257 GNUNET_STATISTICS_update (stats,
258 gettext_noop ("# client searches active"),
261 cr = GNUNET_malloc (sizeof (struct ClientRequest));
263 GNUNET_CONTAINER_DLL_insert (lc->cr_head,
266 options = GSF_PRO_LOCAL_REQUEST;
267 if (0 != (1 & ntohl (sm->options)))
268 options |= GSF_PRO_LOCAL_ONLY;
269 cr->pr = GSF_pending_request_create_ (options,
272 (type == GNUNET_BLOCK_TYPE_SBLOCK)
273 ? &sm->target /* namespace */
275 (0 != memcmp (&sm->target,
277 sizeof (GNUNET_HashCode)))
280 NULL /* bf */, 0 /* mingle */,
281 ntohl (sm->anonymity_level),
284 &client_response_handler,
291 * Transmit the given message by copying it to the target buffer
292 * "buf". "buf" will be NULL and "size" zero if the socket was closed
293 * for writing in the meantime. In that case, do nothing
294 * (the disconnect or shutdown handler will take care of the rest).
295 * If we were able to transmit messages and there are still more
296 * pending, ask core again for further calls to this function.
298 * @param cls closure, pointer to the 'struct GSF_LocalClient'
299 * @param size number of bytes available in buf
300 * @param buf where the callee should write the message
301 * @return number of bytes written to buf
304 transmit_to_client (void *cls,
308 struct GSF_LocalClient *lc = cls;
310 struct ClientResponse *res;
317 while ( (NULL != (res = lc->res_head) ) &&
318 (res->msize <= size) )
320 memcpy (&cbuf[msize], &res[1], res->msize);
323 GNUNET_CONTAINER_DLL_remove (cl->res_head,
329 lc->th = GNUNET_SERVER_notify_transmit_ready (lc->client,
331 GNUNET_TIME_UNIT_FOREVER_REL,
339 * Transmit a message to the given local client as soon as possible.
340 * If the client disconnects before transmission, the message is
343 * @param lc recipient
344 * @param msg message to transmit to client
347 GSF_local_client_transmit_ (struct GSF_LocalClient *lc,
348 const struct GNUNET_MessageHeader *msg)
350 struct ClientResponse *res;
353 msize = ntohs (msg->size);
354 res = GNUNET_malloc (sizeof (struct ClientResponse) + msize);
357 GNUNET_CONTAINER_DLL_insert_tail (lc->res_head,
361 lc->tc = GNUNET_CLIENT_notify_transmit_ready (lc->client,
363 GNUNET_TIME_UNIT_FOREVER_REL,
371 * A client disconnected from us. Tear down the local client
375 * @param client handle of the client
378 GSF_client_disconnect_handler_ (void *cls,
379 const struct GNUNET_SERVER_Client *client)
381 struct GSF_LocalClient *pos;
382 struct DisconnectCallback *dc;
383 struct ClientRequest *cr;
384 struct ClientResponse *res;
387 while ( (pos != NULL) &&
388 (pos->client != client) )
392 while (NULL != (cr = pos->cr_head))
394 GNUNET_CONTAINER_DLL_remove (pos->cr_head,
397 GSF_pending_request_cancel_ (cr->pr);
400 while (NULL != (res = pos->res_head))
402 GNUNET_CONTAINER_DLL_remove (pos->res_head,
409 GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th);
412 GSF_handle_local_client_disconnect_ (pos);
417 /* end of gnunet-service-fs_lc.c */