diff options
-rw-r--r-- | src/dht/Makefile.am | 26 | ||||
-rw-r--r-- | src/dht/dht.conf.in | 2 | ||||
-rw-r--r-- | src/dht/gnunet-service-xdht.c | 199 | ||||
-rw-r--r-- | src/dht/gnunet-service-xdht_clients.c | 1514 | ||||
-rw-r--r-- | src/dht/gnunet-service-xdht_clients.h | 147 | ||||
-rw-r--r-- | src/dht/gnunet-service-xdht_datacache.c | 275 | ||||
-rw-r--r-- | src/dht/gnunet-service-xdht_datacache.h | 86 | ||||
-rw-r--r-- | src/dht/gnunet-service-xdht_hello.c | 137 | ||||
-rw-r--r-- | src/dht/gnunet-service-xdht_hello.h | 55 | ||||
-rw-r--r-- | src/dht/gnunet-service-xdht_neighbours.c | 2227 | ||||
-rw-r--r-- | src/dht/gnunet-service-xdht_neighbours.h | 147 | ||||
-rw-r--r-- | src/dht/gnunet-service-xdht_nse.c | 116 | ||||
-rw-r--r-- | src/dht/gnunet-service-xdht_nse.h | 52 | ||||
-rw-r--r-- | src/dht/gnunet-service-xdht_routing.c | 458 | ||||
-rw-r--r-- | src/dht/gnunet-service-xdht_routing.h | 96 |
15 files changed, 5535 insertions, 2 deletions
diff --git a/src/dht/Makefile.am b/src/dht/Makefile.am index 44b2a17f6..dcfb20166 100644 --- a/src/dht/Makefile.am +++ b/src/dht/Makefile.am | |||
@@ -49,7 +49,8 @@ libgnunet_plugin_block_dht_la_DEPENDENCIES = \ | |||
49 | 49 | ||
50 | 50 | ||
51 | libexec_PROGRAMS = \ | 51 | libexec_PROGRAMS = \ |
52 | gnunet-service-dht | 52 | gnunet-service-dht \ |
53 | gnunet-service-dht-xvine | ||
53 | 54 | ||
54 | noinst_PROGRAMS = \ | 55 | noinst_PROGRAMS = \ |
55 | gnunet-dht-monitor \ | 56 | gnunet-dht-monitor \ |
@@ -77,6 +78,29 @@ gnunet_service_dht_LDADD = \ | |||
77 | $(top_builddir)/src/util/libgnunetutil.la \ | 78 | $(top_builddir)/src/util/libgnunetutil.la \ |
78 | -lm | 79 | -lm |
79 | 80 | ||
81 | gnunet_service_dht_xvine_SOURCES = \ | ||
82 | gnunet-service-xdht.c gnunet-service-xdht.h \ | ||
83 | gnunet-service-xdht_clients.c gnunet-service-xdht_clients.h \ | ||
84 | gnunet-service-xdht_datacache.c gnunet-service-xdht_datacache.h \ | ||
85 | gnunet-service-xdht_hello.c gnunet-service-xdht_hello.h \ | ||
86 | gnunet-service-xdht_nse.c gnunet-service-xdht_nse.h \ | ||
87 | gnunet-service-xdht_neighbours.c gnunet-service-xdht_neighbours.h \ | ||
88 | gnunet-service-xdht_routing.c gnunet-service-xdht_routing.h | ||
89 | gnunet_service_dht_xvine_LDADD = \ | ||
90 | $(top_builddir)/src/statistics/libgnunetstatistics.la \ | ||
91 | $(top_builddir)/src/core/libgnunetcore.la \ | ||
92 | $(top_builddir)/src/nse/libgnunetnse.la \ | ||
93 | $(top_builddir)/src/ats/libgnunetats.la \ | ||
94 | $(top_builddir)/src/transport/libgnunettransport.la \ | ||
95 | $(top_builddir)/src/peerinfo/libgnunetpeerinfo.la \ | ||
96 | $(top_builddir)/src/hello/libgnunethello.la \ | ||
97 | $(top_builddir)/src/block/libgnunetblock.la \ | ||
98 | $(top_builddir)/src/datacache/libgnunetdatacache.la \ | ||
99 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
100 | -lm | ||
101 | |||
102 | |||
103 | |||
80 | gnunet_dht_get_SOURCES = \ | 104 | gnunet_dht_get_SOURCES = \ |
81 | gnunet-dht-get.c | 105 | gnunet-dht-get.c |
82 | gnunet_dht_get_LDADD = \ | 106 | gnunet_dht_get_LDADD = \ |
diff --git a/src/dht/dht.conf.in b/src/dht/dht.conf.in index b7e03c5d2..bab1191d4 100644 --- a/src/dht/dht.conf.in +++ b/src/dht/dht.conf.in | |||
@@ -11,7 +11,7 @@ UNIXPATH = $GNUNET_RUNTIME_DIR/gnunet-service-dht.sock | |||
11 | UNIX_MATCH_UID = YES | 11 | UNIX_MATCH_UID = YES |
12 | UNIX_MATCH_GID = YES | 12 | UNIX_MATCH_GID = YES |
13 | # DISABLE_SOCKET_FORWARDING = NO | 13 | # DISABLE_SOCKET_FORWARDING = NO |
14 | # USERNAME = | 14 | # USERNAME = |
15 | # MAXBUF = | 15 | # MAXBUF = |
16 | # TIMEOUT = | 16 | # TIMEOUT = |
17 | # DISABLEV6 = | 17 | # DISABLEV6 = |
diff --git a/src/dht/gnunet-service-xdht.c b/src/dht/gnunet-service-xdht.c new file mode 100644 index 000000000..f597a5323 --- /dev/null +++ b/src/dht/gnunet-service-xdht.c | |||
@@ -0,0 +1,199 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file dht/gnunet-service-xdht.c | ||
23 | * @brief GNUnet DHT service | ||
24 | * @author Christian Grothoff | ||
25 | * @author Nathan Evans | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_block_lib.h" | ||
29 | #include "gnunet_util_lib.h" | ||
30 | #include "gnunet_transport_service.h" | ||
31 | #include "gnunet_hello_lib.h" | ||
32 | #include "gnunet_dht_service.h" | ||
33 | #include "gnunet_statistics_service.h" | ||
34 | #include "gnunet-service-dht.h" | ||
35 | #include "gnunet-service-dht_clients.h" | ||
36 | #include "gnunet-service-dht_datacache.h" | ||
37 | #include "gnunet-service-dht_hello.h" | ||
38 | #include "gnunet-service-dht_neighbours.h" | ||
39 | #include "gnunet-service-dht_nse.h" | ||
40 | #include "gnunet-service-dht_routing.h" | ||
41 | |||
42 | |||
43 | |||
44 | /** | ||
45 | * Handle for the statistics service. | ||
46 | */ | ||
47 | struct GNUNET_STATISTICS_Handle *GDS_stats; | ||
48 | |||
49 | /** | ||
50 | * Our handle to the BLOCK library. | ||
51 | */ | ||
52 | struct GNUNET_BLOCK_Context *GDS_block_context; | ||
53 | |||
54 | /** | ||
55 | * The configuration the DHT service is running with | ||
56 | */ | ||
57 | const struct GNUNET_CONFIGURATION_Handle *GDS_cfg; | ||
58 | |||
59 | /** | ||
60 | * Our HELLO | ||
61 | */ | ||
62 | struct GNUNET_MessageHeader *GDS_my_hello; | ||
63 | |||
64 | /** | ||
65 | * Handle to the transport service, for getting our hello | ||
66 | */ | ||
67 | struct GNUNET_TRANSPORT_Handle *GDS_transport_handle; | ||
68 | |||
69 | /** | ||
70 | * Handle to get our current HELLO. | ||
71 | */ | ||
72 | static struct GNUNET_TRANSPORT_GetHelloHandle *ghh; | ||
73 | |||
74 | /** | ||
75 | * Hello address expiration | ||
76 | */ | ||
77 | struct GNUNET_TIME_Relative hello_expiration; | ||
78 | |||
79 | |||
80 | /** | ||
81 | * Receive the HELLO from transport service, free current and replace | ||
82 | * if necessary. | ||
83 | * | ||
84 | * @param cls NULL | ||
85 | * @param message HELLO message of peer | ||
86 | */ | ||
87 | static void | ||
88 | process_hello (void *cls, const struct GNUNET_MessageHeader *message) | ||
89 | { | ||
90 | GNUNET_assert (message != NULL); | ||
91 | GNUNET_free_non_null (GDS_my_hello); | ||
92 | GDS_my_hello = GNUNET_malloc (ntohs (message->size)); | ||
93 | memcpy (GDS_my_hello, message, ntohs (message->size)); | ||
94 | } | ||
95 | |||
96 | |||
97 | /** | ||
98 | * Task run during shutdown. | ||
99 | * | ||
100 | * @param cls unused | ||
101 | * @param tc unused | ||
102 | */ | ||
103 | static void | ||
104 | shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
105 | { | ||
106 | if (NULL != ghh) | ||
107 | { | ||
108 | GNUNET_TRANSPORT_get_hello_cancel (ghh); | ||
109 | ghh = NULL; | ||
110 | } | ||
111 | if (GDS_transport_handle != NULL) | ||
112 | { | ||
113 | GNUNET_TRANSPORT_disconnect (GDS_transport_handle); | ||
114 | GDS_transport_handle = NULL; | ||
115 | } | ||
116 | GDS_NEIGHBOURS_done (); | ||
117 | GDS_DATACACHE_done (); | ||
118 | GDS_ROUTING_done (); | ||
119 | GDS_HELLO_done (); | ||
120 | GDS_NSE_done (); | ||
121 | if (GDS_block_context != NULL) | ||
122 | { | ||
123 | GNUNET_BLOCK_context_destroy (GDS_block_context); | ||
124 | GDS_block_context = NULL; | ||
125 | } | ||
126 | if (GDS_stats != NULL) | ||
127 | { | ||
128 | GNUNET_STATISTICS_destroy (GDS_stats, GNUNET_YES); | ||
129 | GDS_stats = NULL; | ||
130 | } | ||
131 | GNUNET_free_non_null (GDS_my_hello); | ||
132 | GDS_my_hello = NULL; | ||
133 | } | ||
134 | |||
135 | |||
136 | /** | ||
137 | * Process dht requests. | ||
138 | * | ||
139 | * @param cls closure | ||
140 | * @param server the initialized server | ||
141 | * @param c configuration to use | ||
142 | */ | ||
143 | static void | ||
144 | run (void *cls, struct GNUNET_SERVER_Handle *server, | ||
145 | const struct GNUNET_CONFIGURATION_Handle *c) | ||
146 | { | ||
147 | GDS_cfg = c; | ||
148 | if (GNUNET_OK != | ||
149 | GNUNET_CONFIGURATION_get_value_time (c, "transport", "HELLO_EXPIRATION", &hello_expiration)) | ||
150 | { | ||
151 | hello_expiration = GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION; | ||
152 | } | ||
153 | GDS_block_context = GNUNET_BLOCK_context_create (GDS_cfg); | ||
154 | GDS_stats = GNUNET_STATISTICS_create ("dht", GDS_cfg); | ||
155 | GDS_ROUTING_init (); | ||
156 | GDS_NSE_init (); | ||
157 | GDS_DATACACHE_init (); | ||
158 | GDS_HELLO_init (); | ||
159 | GDS_CLIENTS_init (server); | ||
160 | if (GNUNET_OK != GDS_NEIGHBOURS_init ()) | ||
161 | { | ||
162 | shutdown_task (NULL, NULL); | ||
163 | return; | ||
164 | } | ||
165 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task, | ||
166 | NULL); | ||
167 | GDS_transport_handle = | ||
168 | GNUNET_TRANSPORT_connect (GDS_cfg, NULL, NULL, NULL, NULL, NULL); | ||
169 | if (GDS_transport_handle == NULL) | ||
170 | { | ||
171 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
172 | _("Failed to connect to transport service!\n")); | ||
173 | return; | ||
174 | } | ||
175 | ghh = GNUNET_TRANSPORT_get_hello (GDS_transport_handle, &process_hello, NULL); | ||
176 | } | ||
177 | |||
178 | |||
179 | /** | ||
180 | * The main function for the dht service. | ||
181 | * | ||
182 | * @param argc number of arguments from the command line | ||
183 | * @param argv command line arguments | ||
184 | * @return 0 ok, 1 on error | ||
185 | */ | ||
186 | int | ||
187 | main (int argc, char *const *argv) | ||
188 | { | ||
189 | int ret; | ||
190 | |||
191 | ret = | ||
192 | (GNUNET_OK == | ||
193 | GNUNET_SERVICE_run (argc, argv, "dht", GNUNET_SERVICE_OPTION_NONE, &run, | ||
194 | NULL)) ? 0 : 1; | ||
195 | GDS_CLIENTS_done (); | ||
196 | return ret; | ||
197 | } | ||
198 | |||
199 | /* end of gnunet-service-dht.c */ | ||
diff --git a/src/dht/gnunet-service-xdht_clients.c b/src/dht/gnunet-service-xdht_clients.c new file mode 100644 index 000000000..ffcb010de --- /dev/null +++ b/src/dht/gnunet-service-xdht_clients.c | |||
@@ -0,0 +1,1514 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file dht/gnunet-service-xdht_clients.c | ||
23 | * @brief GNUnet DHT service's client management code | ||
24 | * @author Christian Grothoff | ||
25 | * @author Nathan Evans | ||
26 | */ | ||
27 | |||
28 | #include "platform.h" | ||
29 | #include "gnunet_constants.h" | ||
30 | #include "gnunet_protocols.h" | ||
31 | #include "gnunet_statistics_service.h" | ||
32 | #include "gnunet-service-dht.h" | ||
33 | #include "gnunet-service-dht_clients.h" | ||
34 | #include "gnunet-service-dht_datacache.h" | ||
35 | #include "gnunet-service-dht_neighbours.h" | ||
36 | #include "dht.h" | ||
37 | |||
38 | |||
39 | /** | ||
40 | * Should routing details be logged to stderr (for debugging)? | ||
41 | */ | ||
42 | #define LOG_TRAFFIC(kind,...) GNUNET_log_from (kind, "dht-traffic",__VA_ARGS__) | ||
43 | |||
44 | #define LOG(kind,...) GNUNET_log_from (kind, "dht-clients",__VA_ARGS__) | ||
45 | |||
46 | /** | ||
47 | * Linked list of messages to send to clients. | ||
48 | */ | ||
49 | struct PendingMessage | ||
50 | { | ||
51 | /** | ||
52 | * Pointer to next item in the list | ||
53 | */ | ||
54 | struct PendingMessage *next; | ||
55 | |||
56 | /** | ||
57 | * Pointer to previous item in the list | ||
58 | */ | ||
59 | struct PendingMessage *prev; | ||
60 | |||
61 | /** | ||
62 | * Actual message to be sent, allocated at the end of the struct: | ||
63 | * // msg = (cast) &pm[1]; | ||
64 | * // memcpy (&pm[1], data, len); | ||
65 | */ | ||
66 | const struct GNUNET_MessageHeader *msg; | ||
67 | |||
68 | }; | ||
69 | |||
70 | |||
71 | /** | ||
72 | * Struct containing information about a client, | ||
73 | * handle to connect to it, and any pending messages | ||
74 | * that need to be sent to it. | ||
75 | */ | ||
76 | struct ClientList | ||
77 | { | ||
78 | /** | ||
79 | * Linked list of active clients | ||
80 | */ | ||
81 | struct ClientList *next; | ||
82 | |||
83 | /** | ||
84 | * Linked list of active clients | ||
85 | */ | ||
86 | struct ClientList *prev; | ||
87 | |||
88 | /** | ||
89 | * The handle to this client | ||
90 | */ | ||
91 | struct GNUNET_SERVER_Client *client_handle; | ||
92 | |||
93 | /** | ||
94 | * Handle to the current transmission request, NULL | ||
95 | * if none pending. | ||
96 | */ | ||
97 | struct GNUNET_SERVER_TransmitHandle *transmit_handle; | ||
98 | |||
99 | /** | ||
100 | * Linked list of pending messages for this client | ||
101 | */ | ||
102 | struct PendingMessage *pending_head; | ||
103 | |||
104 | /** | ||
105 | * Tail of linked list of pending messages for this client | ||
106 | */ | ||
107 | struct PendingMessage *pending_tail; | ||
108 | |||
109 | }; | ||
110 | |||
111 | |||
112 | /** | ||
113 | * Entry in the DHT routing table for a client's GET request. | ||
114 | */ | ||
115 | struct ClientQueryRecord | ||
116 | { | ||
117 | |||
118 | /** | ||
119 | * The key this request was about | ||
120 | */ | ||
121 | struct GNUNET_HashCode key; | ||
122 | |||
123 | /** | ||
124 | * Client responsible for the request. | ||
125 | */ | ||
126 | struct ClientList *client; | ||
127 | |||
128 | /** | ||
129 | * Extended query (see gnunet_block_lib.h), allocated at the end of this struct. | ||
130 | */ | ||
131 | const void *xquery; | ||
132 | |||
133 | /** | ||
134 | * Replies we have already seen for this request. | ||
135 | */ | ||
136 | struct GNUNET_HashCode *seen_replies; | ||
137 | |||
138 | /** | ||
139 | * Pointer to this nodes heap location in the retry-heap (for fast removal) | ||
140 | */ | ||
141 | struct GNUNET_CONTAINER_HeapNode *hnode; | ||
142 | |||
143 | /** | ||
144 | * What's the delay between re-try operations that we currently use for this | ||
145 | * request? | ||
146 | */ | ||
147 | struct GNUNET_TIME_Relative retry_frequency; | ||
148 | |||
149 | /** | ||
150 | * What's the next time we should re-try this request? | ||
151 | */ | ||
152 | struct GNUNET_TIME_Absolute retry_time; | ||
153 | |||
154 | /** | ||
155 | * The unique identifier of this request | ||
156 | */ | ||
157 | uint64_t unique_id; | ||
158 | |||
159 | /** | ||
160 | * Number of bytes in xquery. | ||
161 | */ | ||
162 | size_t xquery_size; | ||
163 | |||
164 | /** | ||
165 | * Number of entries in 'seen_replies'. | ||
166 | */ | ||
167 | unsigned int seen_replies_count; | ||
168 | |||
169 | /** | ||
170 | * Desired replication level | ||
171 | */ | ||
172 | uint32_t replication; | ||
173 | |||
174 | /** | ||
175 | * Any message options for this request | ||
176 | */ | ||
177 | uint32_t msg_options; | ||
178 | |||
179 | /** | ||
180 | * The type for the data for the GET request. | ||
181 | */ | ||
182 | enum GNUNET_BLOCK_Type type; | ||
183 | |||
184 | }; | ||
185 | |||
186 | |||
187 | /** | ||
188 | * Struct containing paremeters of monitoring requests. | ||
189 | */ | ||
190 | struct ClientMonitorRecord | ||
191 | { | ||
192 | |||
193 | /** | ||
194 | * Next element in DLL. | ||
195 | */ | ||
196 | struct ClientMonitorRecord *next; | ||
197 | |||
198 | /** | ||
199 | * Previous element in DLL. | ||
200 | */ | ||
201 | struct ClientMonitorRecord *prev; | ||
202 | |||
203 | /** | ||
204 | * Type of blocks that are of interest | ||
205 | */ | ||
206 | enum GNUNET_BLOCK_Type type; | ||
207 | |||
208 | /** | ||
209 | * Key of data of interest, NULL for all. | ||
210 | */ | ||
211 | struct GNUNET_HashCode *key; | ||
212 | |||
213 | /** | ||
214 | * Flag whether to notify about GET messages. | ||
215 | */ | ||
216 | int16_t get; | ||
217 | |||
218 | /** | ||
219 | * Flag whether to notify about GET_REPONSE messages. | ||
220 | */ | ||
221 | int16_t get_resp; | ||
222 | |||
223 | /** | ||
224 | * Flag whether to notify about PUT messages. | ||
225 | */ | ||
226 | uint16_t put; | ||
227 | |||
228 | /** | ||
229 | * Client to notify of these requests. | ||
230 | */ | ||
231 | struct ClientList *client; | ||
232 | }; | ||
233 | |||
234 | |||
235 | /** | ||
236 | * List of active clients. | ||
237 | */ | ||
238 | static struct ClientList *client_head; | ||
239 | |||
240 | /** | ||
241 | * List of active clients. | ||
242 | */ | ||
243 | static struct ClientList *client_tail; | ||
244 | |||
245 | /** | ||
246 | * List of active monitoring requests. | ||
247 | */ | ||
248 | static struct ClientMonitorRecord *monitor_head; | ||
249 | |||
250 | /** | ||
251 | * List of active monitoring requests. | ||
252 | */ | ||
253 | static struct ClientMonitorRecord *monitor_tail; | ||
254 | |||
255 | /** | ||
256 | * Hashmap for fast key based lookup, maps keys to 'struct ClientQueryRecord' entries. | ||
257 | */ | ||
258 | static struct GNUNET_CONTAINER_MultiHashMap *forward_map; | ||
259 | |||
260 | /** | ||
261 | * Heap with all of our client's request, sorted by retry time (earliest on top). | ||
262 | */ | ||
263 | static struct GNUNET_CONTAINER_Heap *retry_heap; | ||
264 | |||
265 | /** | ||
266 | * Task that re-transmits requests (using retry_heap). | ||
267 | */ | ||
268 | static GNUNET_SCHEDULER_TaskIdentifier retry_task; | ||
269 | |||
270 | |||
271 | /** | ||
272 | * Task run to check for messages that need to be sent to a client. | ||
273 | * | ||
274 | * @param client a ClientList, containing the client and any messages to be sent to it | ||
275 | */ | ||
276 | static void | ||
277 | process_pending_messages (struct ClientList *client); | ||
278 | |||
279 | |||
280 | /** | ||
281 | * Add a PendingMessage to the clients list of messages to be sent | ||
282 | * | ||
283 | * @param client the active client to send the message to | ||
284 | * @param pending_message the actual message to send | ||
285 | */ | ||
286 | static void | ||
287 | add_pending_message (struct ClientList *client, | ||
288 | struct PendingMessage *pending_message) | ||
289 | { | ||
290 | GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail, | ||
291 | pending_message); | ||
292 | process_pending_messages (client); | ||
293 | } | ||
294 | |||
295 | |||
296 | /** | ||
297 | * Find a client if it exists, add it otherwise. | ||
298 | * | ||
299 | * @param client the server handle to the client | ||
300 | * | ||
301 | * @return the client if found, a new client otherwise | ||
302 | */ | ||
303 | static struct ClientList * | ||
304 | find_active_client (struct GNUNET_SERVER_Client *client) | ||
305 | { | ||
306 | struct ClientList *pos = client_head; | ||
307 | struct ClientList *ret; | ||
308 | |||
309 | while (pos != NULL) | ||
310 | { | ||
311 | if (pos->client_handle == client) | ||
312 | return pos; | ||
313 | pos = pos->next; | ||
314 | } | ||
315 | ret = GNUNET_new (struct ClientList); | ||
316 | ret->client_handle = client; | ||
317 | GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ret); | ||
318 | return ret; | ||
319 | } | ||
320 | |||
321 | |||
322 | /** | ||
323 | * Iterator over hash map entries that frees all entries | ||
324 | * associated with the given client. | ||
325 | * | ||
326 | * @param cls client to search for in source routes | ||
327 | * @param key current key code (ignored) | ||
328 | * @param value value in the hash map, a ClientQueryRecord | ||
329 | * @return #GNUNET_YES (we should continue to iterate) | ||
330 | */ | ||
331 | static int | ||
332 | remove_client_records (void *cls, const struct GNUNET_HashCode * key, void *value) | ||
333 | { | ||
334 | struct ClientList *client = cls; | ||
335 | struct ClientQueryRecord *record = value; | ||
336 | |||
337 | if (record->client != client) | ||
338 | return GNUNET_YES; | ||
339 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
340 | "Removing client %p's record for key %s\n", client, | ||
341 | GNUNET_h2s (key)); | ||
342 | GNUNET_assert (GNUNET_YES == | ||
343 | GNUNET_CONTAINER_multihashmap_remove (forward_map, key, | ||
344 | record)); | ||
345 | if (NULL != record->hnode) | ||
346 | GNUNET_CONTAINER_heap_remove_node (record->hnode); | ||
347 | GNUNET_array_grow (record->seen_replies, record->seen_replies_count, 0); | ||
348 | GNUNET_free (record); | ||
349 | return GNUNET_YES; | ||
350 | } | ||
351 | |||
352 | |||
353 | /** | ||
354 | * Functions with this signature are called whenever a client | ||
355 | * is disconnected on the network level. | ||
356 | * | ||
357 | * @param cls closure (NULL for dht) | ||
358 | * @param client identification of the client; NULL | ||
359 | * for the last call when the server is destroyed | ||
360 | */ | ||
361 | static void | ||
362 | handle_client_disconnect (void *cls, | ||
363 | struct GNUNET_SERVER_Client *client) | ||
364 | { | ||
365 | struct ClientList *pos; | ||
366 | struct PendingMessage *reply; | ||
367 | struct ClientMonitorRecord *monitor; | ||
368 | |||
369 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
370 | "Local client %p disconnects\n", | ||
371 | client); | ||
372 | pos = find_active_client (client); | ||
373 | GNUNET_CONTAINER_DLL_remove (client_head, client_tail, pos); | ||
374 | if (pos->transmit_handle != NULL) | ||
375 | GNUNET_SERVER_notify_transmit_ready_cancel (pos->transmit_handle); | ||
376 | while (NULL != (reply = pos->pending_head)) | ||
377 | { | ||
378 | GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, reply); | ||
379 | GNUNET_free (reply); | ||
380 | } | ||
381 | monitor = monitor_head; | ||
382 | while (NULL != monitor) | ||
383 | { | ||
384 | if (monitor->client == pos) | ||
385 | { | ||
386 | struct ClientMonitorRecord *next; | ||
387 | |||
388 | GNUNET_free_non_null (monitor->key); | ||
389 | next = monitor->next; | ||
390 | GNUNET_CONTAINER_DLL_remove (monitor_head, monitor_tail, monitor); | ||
391 | GNUNET_free (monitor); | ||
392 | monitor = next; | ||
393 | } | ||
394 | else | ||
395 | monitor = monitor->next; | ||
396 | } | ||
397 | GNUNET_CONTAINER_multihashmap_iterate (forward_map, &remove_client_records, | ||
398 | pos); | ||
399 | GNUNET_free (pos); | ||
400 | } | ||
401 | |||
402 | |||
403 | /** | ||
404 | * Route the given request via the DHT. This includes updating | ||
405 | * the bloom filter and retransmission times, building the P2P | ||
406 | * message and initiating the routing operation. | ||
407 | */ | ||
408 | static void | ||
409 | transmit_request (struct ClientQueryRecord *cqr) | ||
410 | { | ||
411 | int32_t reply_bf_mutator; | ||
412 | struct GNUNET_CONTAINER_BloomFilter *reply_bf; | ||
413 | struct GNUNET_CONTAINER_BloomFilter *peer_bf; | ||
414 | |||
415 | GNUNET_STATISTICS_update (GDS_stats, | ||
416 | gettext_noop | ||
417 | ("# GET requests from clients injected"), 1, | ||
418 | GNUNET_NO); | ||
419 | reply_bf_mutator = | ||
420 | (int32_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, | ||
421 | UINT32_MAX); | ||
422 | reply_bf = | ||
423 | GNUNET_BLOCK_construct_bloomfilter (reply_bf_mutator, cqr->seen_replies, | ||
424 | cqr->seen_replies_count); | ||
425 | peer_bf = | ||
426 | GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, | ||
427 | GNUNET_CONSTANTS_BLOOMFILTER_K); | ||
428 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
429 | "Initiating GET for %s, replication %u, already have %u replies\n", | ||
430 | GNUNET_h2s(&cqr->key), cqr->replication, cqr->seen_replies_count); | ||
431 | GDS_NEIGHBOURS_handle_get (cqr->type, cqr->msg_options, cqr->replication, | ||
432 | 0 /* hop count */ , | ||
433 | &cqr->key, cqr->xquery, cqr->xquery_size, reply_bf, | ||
434 | reply_bf_mutator, peer_bf); | ||
435 | GNUNET_CONTAINER_bloomfilter_free (reply_bf); | ||
436 | GNUNET_CONTAINER_bloomfilter_free (peer_bf); | ||
437 | |||
438 | /* exponential back-off for retries. | ||
439 | * max GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD (15 min) */ | ||
440 | cqr->retry_frequency = GNUNET_TIME_STD_BACKOFF (cqr->retry_frequency); | ||
441 | cqr->retry_time = GNUNET_TIME_relative_to_absolute (cqr->retry_frequency); | ||
442 | } | ||
443 | |||
444 | |||
445 | /** | ||
446 | * Task that looks at the 'retry_heap' and transmits all of the requests | ||
447 | * on the heap that are ready for transmission. Then re-schedules | ||
448 | * itself (unless the heap is empty). | ||
449 | * | ||
450 | * @param cls unused | ||
451 | * @param tc scheduler context | ||
452 | */ | ||
453 | static void | ||
454 | transmit_next_request_task (void *cls, | ||
455 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
456 | { | ||
457 | struct ClientQueryRecord *cqr; | ||
458 | struct GNUNET_TIME_Relative delay; | ||
459 | |||
460 | retry_task = GNUNET_SCHEDULER_NO_TASK; | ||
461 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) | ||
462 | return; | ||
463 | while (NULL != (cqr = GNUNET_CONTAINER_heap_remove_root (retry_heap))) | ||
464 | { | ||
465 | cqr->hnode = NULL; | ||
466 | delay = GNUNET_TIME_absolute_get_remaining (cqr->retry_time); | ||
467 | if (delay.rel_value_us > 0) | ||
468 | { | ||
469 | cqr->hnode = | ||
470 | GNUNET_CONTAINER_heap_insert (retry_heap, cqr, | ||
471 | cqr->retry_time.abs_value_us); | ||
472 | retry_task = | ||
473 | GNUNET_SCHEDULER_add_delayed (delay, &transmit_next_request_task, | ||
474 | NULL); | ||
475 | return; | ||
476 | } | ||
477 | transmit_request (cqr); | ||
478 | cqr->hnode = | ||
479 | GNUNET_CONTAINER_heap_insert (retry_heap, cqr, | ||
480 | cqr->retry_time.abs_value_us); | ||
481 | } | ||
482 | } | ||
483 | |||
484 | |||
485 | /** | ||
486 | * Handler for PUT messages. | ||
487 | * | ||
488 | * @param cls closure for the service | ||
489 | * @param client the client we received this message from | ||
490 | * @param message the actual message received | ||
491 | */ | ||
492 | static void | ||
493 | handle_dht_local_put (void *cls, struct GNUNET_SERVER_Client *client, | ||
494 | const struct GNUNET_MessageHeader *message) | ||
495 | { | ||
496 | const struct GNUNET_DHT_ClientPutMessage *dht_msg; | ||
497 | struct GNUNET_CONTAINER_BloomFilter *peer_bf; | ||
498 | uint16_t size; | ||
499 | struct PendingMessage *pm; | ||
500 | struct GNUNET_DHT_ClientPutConfirmationMessage *conf; | ||
501 | |||
502 | size = ntohs (message->size); | ||
503 | if (size < sizeof (struct GNUNET_DHT_ClientPutMessage)) | ||
504 | { | ||
505 | GNUNET_break (0); | ||
506 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
507 | return; | ||
508 | } | ||
509 | GNUNET_STATISTICS_update (GDS_stats, | ||
510 | gettext_noop | ||
511 | ("# PUT requests received from clients"), 1, | ||
512 | GNUNET_NO); | ||
513 | dht_msg = (const struct GNUNET_DHT_ClientPutMessage *) message; | ||
514 | LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG, "XDHT CLIENT-PUT %s @ %u\n", | ||
515 | GNUNET_h2s (&dht_msg->key), getpid ()); | ||
516 | /* give to local clients */ | ||
517 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
518 | "Handling local PUT of %u-bytes for query %s\n", | ||
519 | size - sizeof (struct GNUNET_DHT_ClientPutMessage), | ||
520 | GNUNET_h2s (&dht_msg->key)); | ||
521 | GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (dht_msg->expiration), | ||
522 | &dht_msg->key, 0, NULL, 0, NULL, | ||
523 | ntohl (dht_msg->type), | ||
524 | size - sizeof (struct GNUNET_DHT_ClientPutMessage), | ||
525 | &dht_msg[1]); | ||
526 | /* store locally */ | ||
527 | GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (dht_msg->expiration), | ||
528 | &dht_msg->key, 0, NULL, ntohl (dht_msg->type), | ||
529 | size - sizeof (struct GNUNET_DHT_ClientPutMessage), | ||
530 | &dht_msg[1]); | ||
531 | /* route to other peers */ | ||
532 | peer_bf = | ||
533 | GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, | ||
534 | GNUNET_CONSTANTS_BLOOMFILTER_K); | ||
535 | GDS_NEIGHBOURS_handle_put (ntohl (dht_msg->type), ntohl (dht_msg->options), | ||
536 | ntohl (dht_msg->desired_replication_level), | ||
537 | GNUNET_TIME_absolute_ntoh (dht_msg->expiration), | ||
538 | 0 /* hop count */ , | ||
539 | peer_bf, &dht_msg->key, 0, NULL, &dht_msg[1], | ||
540 | size - | ||
541 | sizeof (struct GNUNET_DHT_ClientPutMessage)); | ||
542 | GDS_CLIENTS_process_put (ntohl (dht_msg->options), | ||
543 | ntohl (dht_msg->type), | ||
544 | 0, | ||
545 | ntohl (dht_msg->desired_replication_level), | ||
546 | 1, | ||
547 | GDS_NEIGHBOURS_get_id(), | ||
548 | GNUNET_TIME_absolute_ntoh (dht_msg->expiration), | ||
549 | &dht_msg->key, | ||
550 | &dht_msg[1], | ||
551 | size - sizeof (struct GNUNET_DHT_ClientPutMessage)); | ||
552 | GNUNET_CONTAINER_bloomfilter_free (peer_bf); | ||
553 | pm = GNUNET_malloc (sizeof (struct PendingMessage) + | ||
554 | sizeof (struct GNUNET_DHT_ClientPutConfirmationMessage)); | ||
555 | conf = (struct GNUNET_DHT_ClientPutConfirmationMessage *) &pm[1]; | ||
556 | conf->header.size = htons (sizeof (struct GNUNET_DHT_ClientPutConfirmationMessage)); | ||
557 | conf->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT_OK); | ||
558 | conf->reserved = htonl (0); | ||
559 | conf->unique_id = dht_msg->unique_id; | ||
560 | pm->msg = &conf->header; | ||
561 | add_pending_message (find_active_client (client), pm); | ||
562 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | ||
563 | } | ||
564 | |||
565 | |||
566 | /** | ||
567 | * Handler for DHT GET messages from the client. | ||
568 | * | ||
569 | * @param cls closure for the service | ||
570 | * @param client the client we received this message from | ||
571 | * @param message the actual message received | ||
572 | */ | ||
573 | static void | ||
574 | handle_dht_local_get (void *cls, struct GNUNET_SERVER_Client *client, | ||
575 | const struct GNUNET_MessageHeader *message) | ||
576 | { | ||
577 | const struct GNUNET_DHT_ClientGetMessage *get; | ||
578 | struct ClientQueryRecord *cqr; | ||
579 | size_t xquery_size; | ||
580 | const char *xquery; | ||
581 | uint16_t size; | ||
582 | |||
583 | size = ntohs (message->size); | ||
584 | if (size < sizeof (struct GNUNET_DHT_ClientGetMessage)) | ||
585 | { | ||
586 | GNUNET_break (0); | ||
587 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
588 | return; | ||
589 | } | ||
590 | xquery_size = size - sizeof (struct GNUNET_DHT_ClientGetMessage); | ||
591 | get = (const struct GNUNET_DHT_ClientGetMessage *) message; | ||
592 | xquery = (const char *) &get[1]; | ||
593 | GNUNET_STATISTICS_update (GDS_stats, | ||
594 | gettext_noop | ||
595 | ("# GET requests received from clients"), 1, | ||
596 | GNUNET_NO); | ||
597 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
598 | "Received GET request for %s from local client %p, xq: %.*s\n", | ||
599 | GNUNET_h2s (&get->key), client, xquery_size, xquery); | ||
600 | |||
601 | LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG, "XDHT CLIENT-GET %s @ %u\n", | ||
602 | GNUNET_h2s (&get->key), getpid ()); | ||
603 | |||
604 | |||
605 | cqr = GNUNET_malloc (sizeof (struct ClientQueryRecord) + xquery_size); | ||
606 | cqr->key = get->key; | ||
607 | cqr->client = find_active_client (client); | ||
608 | cqr->xquery = (void *) &cqr[1]; | ||
609 | memcpy (&cqr[1], xquery, xquery_size); | ||
610 | cqr->hnode = GNUNET_CONTAINER_heap_insert (retry_heap, cqr, 0); | ||
611 | cqr->retry_frequency = GNUNET_TIME_UNIT_SECONDS; | ||
612 | cqr->retry_time = GNUNET_TIME_absolute_get (); | ||
613 | cqr->unique_id = get->unique_id; | ||
614 | cqr->xquery_size = xquery_size; | ||
615 | cqr->replication = ntohl (get->desired_replication_level); | ||
616 | cqr->msg_options = ntohl (get->options); | ||
617 | cqr->type = ntohl (get->type); | ||
618 | // FIXME use cqr->key, set multihashmap create to GNUNET_YES | ||
619 | GNUNET_CONTAINER_multihashmap_put (forward_map, &get->key, cqr, | ||
620 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); | ||
621 | GDS_CLIENTS_process_get (ntohl (get->options), | ||
622 | ntohl (get->type), | ||
623 | 0, | ||
624 | ntohl (get->desired_replication_level), | ||
625 | 1, | ||
626 | GDS_NEIGHBOURS_get_id(), | ||
627 | &get->key); | ||
628 | /* start remote requests */ | ||
629 | if (GNUNET_SCHEDULER_NO_TASK != retry_task) | ||
630 | GNUNET_SCHEDULER_cancel (retry_task); | ||
631 | retry_task = GNUNET_SCHEDULER_add_now (&transmit_next_request_task, NULL); | ||
632 | /* perform local lookup */ | ||
633 | GDS_DATACACHE_handle_get (&get->key, cqr->type, cqr->xquery, xquery_size, | ||
634 | NULL, 0); | ||
635 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | ||
636 | } | ||
637 | |||
638 | |||
639 | /** | ||
640 | * Closure for 'find_by_unique_id'. | ||
641 | */ | ||
642 | struct FindByUniqueIdContext | ||
643 | { | ||
644 | /** | ||
645 | * Where to store the result, if found. | ||
646 | */ | ||
647 | struct ClientQueryRecord *cqr; | ||
648 | |||
649 | uint64_t unique_id; | ||
650 | }; | ||
651 | |||
652 | |||
653 | /** | ||
654 | * Function called for each existing DHT record for the given | ||
655 | * query. Checks if it matches the UID given in the closure | ||
656 | * and if so returns the entry as a result. | ||
657 | * | ||
658 | * @param cls the search context | ||
659 | * @param key query for the lookup (not used) | ||
660 | * @param value the 'struct ClientQueryRecord' | ||
661 | * @return GNUNET_YES to continue iteration (result not yet found) | ||
662 | */ | ||
663 | static int | ||
664 | find_by_unique_id (void *cls, | ||
665 | const struct GNUNET_HashCode *key, | ||
666 | void *value) | ||
667 | { | ||
668 | struct FindByUniqueIdContext *fui_ctx = cls; | ||
669 | struct ClientQueryRecord *cqr = value; | ||
670 | |||
671 | if (cqr->unique_id != fui_ctx->unique_id) | ||
672 | return GNUNET_YES; | ||
673 | fui_ctx->cqr = cqr; | ||
674 | return GNUNET_NO; | ||
675 | } | ||
676 | |||
677 | |||
678 | /** | ||
679 | * Handler for "GET result seen" messages from the client. | ||
680 | * | ||
681 | * @param cls closure for the service | ||
682 | * @param client the client we received this message from | ||
683 | * @param message the actual message received | ||
684 | */ | ||
685 | static void | ||
686 | handle_dht_local_get_result_seen (void *cls, struct GNUNET_SERVER_Client *client, | ||
687 | const struct GNUNET_MessageHeader *message) | ||
688 | { | ||
689 | const struct GNUNET_DHT_ClientGetResultSeenMessage *seen; | ||
690 | uint16_t size; | ||
691 | unsigned int hash_count; | ||
692 | unsigned int old_count; | ||
693 | const struct GNUNET_HashCode *hc; | ||
694 | struct FindByUniqueIdContext fui_ctx; | ||
695 | struct ClientQueryRecord *cqr; | ||
696 | |||
697 | size = ntohs (message->size); | ||
698 | if (size < sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage)) | ||
699 | { | ||
700 | GNUNET_break (0); | ||
701 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
702 | return; | ||
703 | } | ||
704 | seen = (const struct GNUNET_DHT_ClientGetResultSeenMessage *) message; | ||
705 | hash_count = (size - sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage)) / sizeof (struct GNUNET_HashCode); | ||
706 | if (size != sizeof (struct GNUNET_DHT_ClientGetResultSeenMessage) + hash_count * sizeof (struct GNUNET_HashCode)) | ||
707 | { | ||
708 | GNUNET_break (0); | ||
709 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
710 | return; | ||
711 | } | ||
712 | hc = (const struct GNUNET_HashCode*) &seen[1]; | ||
713 | fui_ctx.unique_id = seen->unique_id; | ||
714 | fui_ctx.cqr = NULL; | ||
715 | GNUNET_CONTAINER_multihashmap_get_multiple (forward_map, | ||
716 | &seen->key, | ||
717 | &find_by_unique_id, | ||
718 | &fui_ctx); | ||
719 | if (NULL == (cqr = fui_ctx.cqr)) | ||
720 | { | ||
721 | GNUNET_break (0); | ||
722 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
723 | return; | ||
724 | } | ||
725 | /* finally, update 'seen' list */ | ||
726 | old_count = cqr->seen_replies_count; | ||
727 | GNUNET_array_grow (cqr->seen_replies, | ||
728 | cqr->seen_replies_count, | ||
729 | cqr->seen_replies_count + hash_count); | ||
730 | memcpy (&cqr->seen_replies[old_count], | ||
731 | hc, | ||
732 | sizeof (struct GNUNET_HashCode) * hash_count); | ||
733 | } | ||
734 | |||
735 | |||
736 | /** | ||
737 | * Closure for 'remove_by_unique_id'. | ||
738 | */ | ||
739 | struct RemoveByUniqueIdContext | ||
740 | { | ||
741 | /** | ||
742 | * Client that issued the removal request. | ||
743 | */ | ||
744 | struct ClientList *client; | ||
745 | |||
746 | /** | ||
747 | * Unique ID of the request. | ||
748 | */ | ||
749 | uint64_t unique_id; | ||
750 | }; | ||
751 | |||
752 | |||
753 | /** | ||
754 | * Iterator over hash map entries that frees all entries | ||
755 | * that match the given client and unique ID. | ||
756 | * | ||
757 | * @param cls unique ID and client to search for in source routes | ||
758 | * @param key current key code | ||
759 | * @param value value in the hash map, a ClientQueryRecord | ||
760 | * @return GNUNET_YES (we should continue to iterate) | ||
761 | */ | ||
762 | static int | ||
763 | remove_by_unique_id (void *cls, const struct GNUNET_HashCode * key, void *value) | ||
764 | { | ||
765 | const struct RemoveByUniqueIdContext *ctx = cls; | ||
766 | struct ClientQueryRecord *record = value; | ||
767 | |||
768 | if (record->unique_id != ctx->unique_id) | ||
769 | return GNUNET_YES; | ||
770 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
771 | "Removing client %p's record for key %s (by unique id)\n", | ||
772 | ctx->client->client_handle, GNUNET_h2s (key)); | ||
773 | return remove_client_records (ctx->client, key, record); | ||
774 | } | ||
775 | |||
776 | |||
777 | /** | ||
778 | * Handler for any generic DHT stop messages, calls the appropriate handler | ||
779 | * depending on message type (if processed locally) | ||
780 | * | ||
781 | * @param cls closure for the service | ||
782 | * @param client the client we received this message from | ||
783 | * @param message the actual message received | ||
784 | * | ||
785 | */ | ||
786 | static void | ||
787 | handle_dht_local_get_stop (void *cls, struct GNUNET_SERVER_Client *client, | ||
788 | const struct GNUNET_MessageHeader *message) | ||
789 | { | ||
790 | const struct GNUNET_DHT_ClientGetStopMessage *dht_stop_msg = | ||
791 | (const struct GNUNET_DHT_ClientGetStopMessage *) message; | ||
792 | struct RemoveByUniqueIdContext ctx; | ||
793 | |||
794 | GNUNET_STATISTICS_update (GDS_stats, | ||
795 | gettext_noop | ||
796 | ("# GET STOP requests received from clients"), 1, | ||
797 | GNUNET_NO); | ||
798 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
799 | "Received GET STOP request for %s from local client %p\n", | ||
800 | client, GNUNET_h2s (&dht_stop_msg->key)); | ||
801 | ctx.client = find_active_client (client); | ||
802 | ctx.unique_id = dht_stop_msg->unique_id; | ||
803 | GNUNET_CONTAINER_multihashmap_get_multiple (forward_map, &dht_stop_msg->key, | ||
804 | &remove_by_unique_id, &ctx); | ||
805 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | ||
806 | } | ||
807 | |||
808 | |||
809 | /** | ||
810 | * Handler for monitor start messages | ||
811 | * | ||
812 | * @param cls closure for the service | ||
813 | * @param client the client we received this message from | ||
814 | * @param message the actual message received | ||
815 | * | ||
816 | */ | ||
817 | static void | ||
818 | handle_dht_local_monitor (void *cls, struct GNUNET_SERVER_Client *client, | ||
819 | const struct GNUNET_MessageHeader *message) | ||
820 | { | ||
821 | struct ClientMonitorRecord *r; | ||
822 | const struct GNUNET_DHT_MonitorStartStopMessage *msg; | ||
823 | |||
824 | msg = (struct GNUNET_DHT_MonitorStartStopMessage *) message; | ||
825 | r = GNUNET_malloc (sizeof(struct ClientMonitorRecord)); | ||
826 | |||
827 | r->client = find_active_client(client); | ||
828 | r->type = ntohl(msg->type); | ||
829 | r->get = ntohs(msg->get); | ||
830 | r->get_resp = ntohs(msg->get_resp); | ||
831 | r->put = ntohs(msg->put); | ||
832 | if (0 == ntohs(msg->filter_key)) | ||
833 | r->key = NULL; | ||
834 | else | ||
835 | { | ||
836 | r->key = GNUNET_malloc (sizeof (struct GNUNET_HashCode)); | ||
837 | memcpy (r->key, &msg->key, sizeof (struct GNUNET_HashCode)); | ||
838 | } | ||
839 | GNUNET_CONTAINER_DLL_insert (monitor_head, monitor_tail, r); | ||
840 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | ||
841 | } | ||
842 | |||
843 | /** | ||
844 | * Handler for monitor stop messages | ||
845 | * | ||
846 | * @param cls closure for the service | ||
847 | * @param client the client we received this message from | ||
848 | * @param message the actual message received | ||
849 | * | ||
850 | */ | ||
851 | static void | ||
852 | handle_dht_local_monitor_stop (void *cls, struct GNUNET_SERVER_Client *client, | ||
853 | const struct GNUNET_MessageHeader *message) | ||
854 | { | ||
855 | struct ClientMonitorRecord *r; | ||
856 | const struct GNUNET_DHT_MonitorStartStopMessage *msg; | ||
857 | int keys_match; | ||
858 | |||
859 | msg = (struct GNUNET_DHT_MonitorStartStopMessage *) message; | ||
860 | r = monitor_head; | ||
861 | |||
862 | while (NULL != r) | ||
863 | { | ||
864 | if (NULL == r->key) | ||
865 | keys_match = (0 == ntohs(msg->filter_key)); | ||
866 | else | ||
867 | { | ||
868 | keys_match = (0 != ntohs(msg->filter_key) | ||
869 | && !memcmp(r->key, &msg->key, sizeof(struct GNUNET_HashCode))); | ||
870 | } | ||
871 | if (find_active_client(client) == r->client | ||
872 | && ntohl(msg->type) == r->type | ||
873 | && r->get == msg->get | ||
874 | && r->get_resp == msg->get_resp | ||
875 | && r->put == msg->put | ||
876 | && keys_match | ||
877 | ) | ||
878 | { | ||
879 | GNUNET_CONTAINER_DLL_remove (monitor_head, monitor_tail, r); | ||
880 | GNUNET_free_non_null (r->key); | ||
881 | GNUNET_free (r); | ||
882 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | ||
883 | return; /* Delete only ONE entry */ | ||
884 | } | ||
885 | r = r->next; | ||
886 | } | ||
887 | |||
888 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | ||
889 | } | ||
890 | |||
891 | |||
892 | /** | ||
893 | * Callback called as a result of issuing a GNUNET_SERVER_notify_transmit_ready | ||
894 | * request. A ClientList is passed as closure, take the head of the list | ||
895 | * and copy it into buf, which has the result of sending the message to the | ||
896 | * client. | ||
897 | * | ||
898 | * @param cls closure to this call | ||
899 | * @param size maximum number of bytes available to send | ||
900 | * @param buf where to copy the actual message to | ||
901 | * | ||
902 | * @return the number of bytes actually copied, 0 indicates failure | ||
903 | */ | ||
904 | static size_t | ||
905 | send_reply_to_client (void *cls, size_t size, void *buf) | ||
906 | { | ||
907 | struct ClientList *client = cls; | ||
908 | char *cbuf = buf; | ||
909 | struct PendingMessage *reply; | ||
910 | size_t off; | ||
911 | size_t msize; | ||
912 | |||
913 | client->transmit_handle = NULL; | ||
914 | if (buf == NULL) | ||
915 | { | ||
916 | /* client disconnected */ | ||
917 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
918 | "Client %p disconnected, pending messages will be discarded\n", | ||
919 | client->client_handle); | ||
920 | return 0; | ||
921 | } | ||
922 | off = 0; | ||
923 | while ((NULL != (reply = client->pending_head)) && | ||
924 | (size >= off + (msize = ntohs (reply->msg->size)))) | ||
925 | { | ||
926 | GNUNET_CONTAINER_DLL_remove (client->pending_head, client->pending_tail, | ||
927 | reply); | ||
928 | memcpy (&cbuf[off], reply->msg, msize); | ||
929 | GNUNET_free (reply); | ||
930 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes to client %p\n", | ||
931 | msize, client->client_handle); | ||
932 | off += msize; | ||
933 | } | ||
934 | process_pending_messages (client); | ||
935 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitted %u/%u bytes to client %p\n", | ||
936 | (unsigned int) off, (unsigned int) size, client->client_handle); | ||
937 | return off; | ||
938 | } | ||
939 | |||
940 | |||
941 | /** | ||
942 | * Task run to check for messages that need to be sent to a client. | ||
943 | * | ||
944 | * @param client a ClientList, containing the client and any messages to be sent to it | ||
945 | */ | ||
946 | static void | ||
947 | process_pending_messages (struct ClientList *client) | ||
948 | { | ||
949 | if ((client->pending_head == NULL) || (client->transmit_handle != NULL)) | ||
950 | { | ||
951 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
952 | "Not asking for transmission to %p now: %s\n", | ||
953 | client->client_handle, | ||
954 | client->pending_head == | ||
955 | NULL ? "no more messages" : "request already pending"); | ||
956 | return; | ||
957 | } | ||
958 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
959 | "Asking for transmission of %u bytes to client %p\n", | ||
960 | ntohs (client->pending_head->msg->size), client->client_handle); | ||
961 | client->transmit_handle = | ||
962 | GNUNET_SERVER_notify_transmit_ready (client->client_handle, | ||
963 | ntohs (client->pending_head-> | ||
964 | msg->size), | ||
965 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
966 | &send_reply_to_client, client); | ||
967 | } | ||
968 | |||
969 | |||
970 | /** | ||
971 | * Closure for 'forward_reply' | ||
972 | */ | ||
973 | struct ForwardReplyContext | ||
974 | { | ||
975 | |||
976 | /** | ||
977 | * Actual message to send to matching clients. | ||
978 | */ | ||
979 | struct PendingMessage *pm; | ||
980 | |||
981 | /** | ||
982 | * Embedded payload. | ||
983 | */ | ||
984 | const void *data; | ||
985 | |||
986 | /** | ||
987 | * Type of the data. | ||
988 | */ | ||
989 | enum GNUNET_BLOCK_Type type; | ||
990 | |||
991 | /** | ||
992 | * Number of bytes in data. | ||
993 | */ | ||
994 | size_t data_size; | ||
995 | |||
996 | /** | ||
997 | * Do we need to copy 'pm' because it was already used? | ||
998 | */ | ||
999 | int do_copy; | ||
1000 | |||
1001 | }; | ||
1002 | |||
1003 | |||
1004 | /** | ||
1005 | * Iterator over hash map entries that send a given reply to | ||
1006 | * each of the matching clients. With some tricky recycling | ||
1007 | * of the buffer. | ||
1008 | * | ||
1009 | * @param cls the 'struct ForwardReplyContext' | ||
1010 | * @param key current key | ||
1011 | * @param value value in the hash map, a ClientQueryRecord | ||
1012 | * @return GNUNET_YES (we should continue to iterate), | ||
1013 | * if the result is mal-formed, GNUNET_NO | ||
1014 | */ | ||
1015 | static int | ||
1016 | forward_reply (void *cls, const struct GNUNET_HashCode * key, void *value) | ||
1017 | { | ||
1018 | struct ForwardReplyContext *frc = cls; | ||
1019 | struct ClientQueryRecord *record = value; | ||
1020 | struct PendingMessage *pm; | ||
1021 | struct GNUNET_DHT_ClientResultMessage *reply; | ||
1022 | enum GNUNET_BLOCK_EvaluationResult eval; | ||
1023 | int do_free; | ||
1024 | struct GNUNET_HashCode ch; | ||
1025 | unsigned int i; | ||
1026 | |||
1027 | LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG, | ||
1028 | "XDHT CLIENT-RESULT %s\n", | ||
1029 | GNUNET_h2s (key)); | ||
1030 | if ((record->type != GNUNET_BLOCK_TYPE_ANY) && (record->type != frc->type)) | ||
1031 | { | ||
1032 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1033 | "Record type missmatch, not passing request for key %s to local client\n", | ||
1034 | GNUNET_h2s (key)); | ||
1035 | GNUNET_STATISTICS_update (GDS_stats, | ||
1036 | gettext_noop | ||
1037 | ("# Key match, type mismatches in REPLY to CLIENT"), | ||
1038 | 1, GNUNET_NO); | ||
1039 | return GNUNET_YES; /* type mismatch */ | ||
1040 | } | ||
1041 | GNUNET_CRYPTO_hash (frc->data, frc->data_size, &ch); | ||
1042 | for (i = 0; i < record->seen_replies_count; i++) | ||
1043 | if (0 == memcmp (&record->seen_replies[i], &ch, sizeof (struct GNUNET_HashCode))) | ||
1044 | { | ||
1045 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1046 | "Duplicate reply, not passing request for key %s to local client\n", | ||
1047 | GNUNET_h2s (key)); | ||
1048 | GNUNET_STATISTICS_update (GDS_stats, | ||
1049 | gettext_noop | ||
1050 | ("# Duplicate REPLIES to CLIENT request dropped"), | ||
1051 | 1, GNUNET_NO); | ||
1052 | return GNUNET_YES; /* duplicate */ | ||
1053 | } | ||
1054 | eval = | ||
1055 | GNUNET_BLOCK_evaluate (GDS_block_context, record->type, key, NULL, 0, | ||
1056 | record->xquery, record->xquery_size, frc->data, | ||
1057 | frc->data_size); | ||
1058 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1059 | "Evaluation result is %d for key %s for local client's query\n", | ||
1060 | (int) eval, GNUNET_h2s (key)); | ||
1061 | switch (eval) | ||
1062 | { | ||
1063 | case GNUNET_BLOCK_EVALUATION_OK_LAST: | ||
1064 | do_free = GNUNET_YES; | ||
1065 | break; | ||
1066 | case GNUNET_BLOCK_EVALUATION_OK_MORE: | ||
1067 | GNUNET_array_append (record->seen_replies, record->seen_replies_count, ch); | ||
1068 | do_free = GNUNET_NO; | ||
1069 | break; | ||
1070 | case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE: | ||
1071 | /* should be impossible to encounter here */ | ||
1072 | GNUNET_break (0); | ||
1073 | return GNUNET_YES; | ||
1074 | case GNUNET_BLOCK_EVALUATION_RESULT_INVALID: | ||
1075 | GNUNET_break_op (0); | ||
1076 | return GNUNET_NO; | ||
1077 | case GNUNET_BLOCK_EVALUATION_REQUEST_VALID: | ||
1078 | GNUNET_break (0); | ||
1079 | return GNUNET_NO; | ||
1080 | case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID: | ||
1081 | GNUNET_break (0); | ||
1082 | return GNUNET_NO; | ||
1083 | case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT: | ||
1084 | return GNUNET_YES; | ||
1085 | case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED: | ||
1086 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1087 | _("Unsupported block type (%u) in request!\n"), record->type); | ||
1088 | return GNUNET_NO; | ||
1089 | default: | ||
1090 | GNUNET_break (0); | ||
1091 | return GNUNET_NO; | ||
1092 | } | ||
1093 | if (GNUNET_NO == frc->do_copy) | ||
1094 | { | ||
1095 | /* first time, we can use the original data */ | ||
1096 | pm = frc->pm; | ||
1097 | frc->do_copy = GNUNET_YES; | ||
1098 | } | ||
1099 | else | ||
1100 | { | ||
1101 | /* two clients waiting for same reply, must copy for queueing */ | ||
1102 | pm = GNUNET_malloc (sizeof (struct PendingMessage) + | ||
1103 | ntohs (frc->pm->msg->size)); | ||
1104 | memcpy (pm, frc->pm, | ||
1105 | sizeof (struct PendingMessage) + ntohs (frc->pm->msg->size)); | ||
1106 | pm->next = pm->prev = NULL; | ||
1107 | pm->msg = (struct GNUNET_MessageHeader *) &pm[1]; | ||
1108 | } | ||
1109 | GNUNET_STATISTICS_update (GDS_stats, | ||
1110 | gettext_noop ("# RESULTS queued for clients"), 1, | ||
1111 | GNUNET_NO); | ||
1112 | reply = (struct GNUNET_DHT_ClientResultMessage *) &pm[1]; | ||
1113 | reply->unique_id = record->unique_id; | ||
1114 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1115 | "Queueing reply to query %s for client %p\n", | ||
1116 | GNUNET_h2s (key), | ||
1117 | record->client->client_handle); | ||
1118 | add_pending_message (record->client, pm); | ||
1119 | if (GNUNET_YES == do_free) | ||
1120 | remove_client_records (record->client, key, record); | ||
1121 | return GNUNET_YES; | ||
1122 | } | ||
1123 | |||
1124 | |||
1125 | /** | ||
1126 | * Handle a reply we've received from another peer. If the reply | ||
1127 | * matches any of our pending queries, forward it to the respective | ||
1128 | * client(s). | ||
1129 | * | ||
1130 | * @param expiration when will the reply expire | ||
1131 | * @param key the query this reply is for | ||
1132 | * @param get_path_length number of peers in @a get_path | ||
1133 | * @param get_path path the reply took on get | ||
1134 | * @param put_path_length number of peers in @a put_path | ||
1135 | * @param put_path path the reply took on put | ||
1136 | * @param type type of the reply | ||
1137 | * @param data_size number of bytes in @a data | ||
1138 | * @param data application payload data | ||
1139 | */ | ||
1140 | void | ||
1141 | GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration, | ||
1142 | const struct GNUNET_HashCode *key, | ||
1143 | unsigned int get_path_length, | ||
1144 | const struct GNUNET_PeerIdentity *get_path, | ||
1145 | unsigned int put_path_length, | ||
1146 | const struct GNUNET_PeerIdentity *put_path, | ||
1147 | enum GNUNET_BLOCK_Type type, size_t data_size, | ||
1148 | const void *data) | ||
1149 | { | ||
1150 | struct ForwardReplyContext frc; | ||
1151 | struct PendingMessage *pm; | ||
1152 | struct GNUNET_DHT_ClientResultMessage *reply; | ||
1153 | struct GNUNET_PeerIdentity *paths; | ||
1154 | size_t msize; | ||
1155 | |||
1156 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1157 | "reply for key %s\n", | ||
1158 | GNUNET_h2s (key)); | ||
1159 | |||
1160 | if (NULL == GNUNET_CONTAINER_multihashmap_get (forward_map, key)) | ||
1161 | { | ||
1162 | GNUNET_STATISTICS_update (GDS_stats, | ||
1163 | gettext_noop | ||
1164 | ("# REPLIES ignored for CLIENTS (no match)"), 1, | ||
1165 | GNUNET_NO); | ||
1166 | return; /* no matching request, fast exit! */ | ||
1167 | } | ||
1168 | msize = | ||
1169 | sizeof (struct GNUNET_DHT_ClientResultMessage) + data_size + | ||
1170 | (get_path_length + put_path_length) * sizeof (struct GNUNET_PeerIdentity); | ||
1171 | if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) | ||
1172 | { | ||
1173 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1174 | _("Could not pass reply to client, message too big!\n")); | ||
1175 | return; | ||
1176 | } | ||
1177 | pm = GNUNET_malloc (msize + sizeof (struct PendingMessage)); | ||
1178 | reply = (struct GNUNET_DHT_ClientResultMessage *) &pm[1]; | ||
1179 | pm->msg = &reply->header; | ||
1180 | reply->header.size = htons ((uint16_t) msize); | ||
1181 | reply->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_RESULT); | ||
1182 | reply->type = htonl (type); | ||
1183 | reply->get_path_length = htonl (get_path_length); | ||
1184 | reply->put_path_length = htonl (put_path_length); | ||
1185 | reply->unique_id = 0; /* filled in later */ | ||
1186 | reply->expiration = GNUNET_TIME_absolute_hton (expiration); | ||
1187 | reply->key = *key; | ||
1188 | paths = (struct GNUNET_PeerIdentity *) &reply[1]; | ||
1189 | memcpy (paths, put_path, | ||
1190 | sizeof (struct GNUNET_PeerIdentity) * put_path_length); | ||
1191 | memcpy (&paths[put_path_length], get_path, | ||
1192 | sizeof (struct GNUNET_PeerIdentity) * get_path_length); | ||
1193 | memcpy (&paths[get_path_length + put_path_length], data, data_size); | ||
1194 | frc.do_copy = GNUNET_NO; | ||
1195 | frc.pm = pm; | ||
1196 | frc.data = data; | ||
1197 | frc.data_size = data_size; | ||
1198 | frc.type = type; | ||
1199 | GNUNET_CONTAINER_multihashmap_get_multiple (forward_map, key, &forward_reply, | ||
1200 | &frc); | ||
1201 | if (GNUNET_NO == frc.do_copy) | ||
1202 | { | ||
1203 | /* did not match any of the requests, free! */ | ||
1204 | GNUNET_STATISTICS_update (GDS_stats, | ||
1205 | gettext_noop | ||
1206 | ("# REPLIES ignored for CLIENTS (no match)"), 1, | ||
1207 | GNUNET_NO); | ||
1208 | GNUNET_free (pm); | ||
1209 | } | ||
1210 | } | ||
1211 | |||
1212 | |||
1213 | /** | ||
1214 | * Check if some client is monitoring GET messages and notify | ||
1215 | * them in that case. | ||
1216 | * | ||
1217 | * @param options Options, for instance RecordRoute, DemultiplexEverywhere. | ||
1218 | * @param type The type of data in the request. | ||
1219 | * @param hop_count Hop count so far. | ||
1220 | * @param path_length number of entries in path (or 0 if not recorded). | ||
1221 | * @param path peers on the GET path (or NULL if not recorded). | ||
1222 | * @param desired_replication_level Desired replication level. | ||
1223 | * @param key Key of the requested data. | ||
1224 | */ | ||
1225 | void | ||
1226 | GDS_CLIENTS_process_get (uint32_t options, | ||
1227 | enum GNUNET_BLOCK_Type type, | ||
1228 | uint32_t hop_count, | ||
1229 | uint32_t desired_replication_level, | ||
1230 | unsigned int path_length, | ||
1231 | const struct GNUNET_PeerIdentity *path, | ||
1232 | const struct GNUNET_HashCode * key) | ||
1233 | { | ||
1234 | struct ClientMonitorRecord *m; | ||
1235 | struct ClientList **cl; | ||
1236 | unsigned int cl_size; | ||
1237 | |||
1238 | cl = NULL; | ||
1239 | cl_size = 0; | ||
1240 | for (m = monitor_head; NULL != m; m = m->next) | ||
1241 | { | ||
1242 | if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) && | ||
1243 | (NULL == m->key || | ||
1244 | memcmp (key, m->key, sizeof(struct GNUNET_HashCode)) == 0)) | ||
1245 | { | ||
1246 | struct PendingMessage *pm; | ||
1247 | struct GNUNET_DHT_MonitorGetMessage *mmsg; | ||
1248 | struct GNUNET_PeerIdentity *msg_path; | ||
1249 | size_t msize; | ||
1250 | unsigned int i; | ||
1251 | |||
1252 | /* Don't send duplicates */ | ||
1253 | for (i = 0; i < cl_size; i++) | ||
1254 | if (cl[i] == m->client) | ||
1255 | break; | ||
1256 | if (i < cl_size) | ||
1257 | continue; | ||
1258 | GNUNET_array_append (cl, cl_size, m->client); | ||
1259 | |||
1260 | msize = path_length * sizeof (struct GNUNET_PeerIdentity); | ||
1261 | msize += sizeof (struct GNUNET_DHT_MonitorGetMessage); | ||
1262 | msize += sizeof (struct PendingMessage); | ||
1263 | pm = GNUNET_malloc (msize); | ||
1264 | mmsg = (struct GNUNET_DHT_MonitorGetMessage *) &pm[1]; | ||
1265 | pm->msg = &mmsg->header; | ||
1266 | mmsg->header.size = htons (msize - sizeof (struct PendingMessage)); | ||
1267 | mmsg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET); | ||
1268 | mmsg->options = htonl(options); | ||
1269 | mmsg->type = htonl(type); | ||
1270 | mmsg->hop_count = htonl(hop_count); | ||
1271 | mmsg->desired_replication_level = htonl(desired_replication_level); | ||
1272 | mmsg->get_path_length = htonl(path_length); | ||
1273 | memcpy (&mmsg->key, key, sizeof (struct GNUNET_HashCode)); | ||
1274 | msg_path = (struct GNUNET_PeerIdentity *) &mmsg[1]; | ||
1275 | if (path_length > 0) | ||
1276 | memcpy (msg_path, path, | ||
1277 | path_length * sizeof (struct GNUNET_PeerIdentity)); | ||
1278 | add_pending_message (m->client, pm); | ||
1279 | } | ||
1280 | } | ||
1281 | GNUNET_free_non_null (cl); | ||
1282 | } | ||
1283 | |||
1284 | |||
1285 | /** | ||
1286 | * Check if some client is monitoring GET RESP messages and notify | ||
1287 | * them in that case. | ||
1288 | * | ||
1289 | * @param type The type of data in the result. | ||
1290 | * @param get_path Peers on GET path (or NULL if not recorded). | ||
1291 | * @param get_path_length number of entries in get_path. | ||
1292 | * @param put_path peers on the PUT path (or NULL if not recorded). | ||
1293 | * @param put_path_length number of entries in get_path. | ||
1294 | * @param exp Expiration time of the data. | ||
1295 | * @param key Key of the data. | ||
1296 | * @param data Pointer to the result data. | ||
1297 | * @param size Number of bytes in data. | ||
1298 | */ | ||
1299 | void | ||
1300 | GDS_CLIENTS_process_get_resp (enum GNUNET_BLOCK_Type type, | ||
1301 | const struct GNUNET_PeerIdentity *get_path, | ||
1302 | unsigned int get_path_length, | ||
1303 | const struct GNUNET_PeerIdentity *put_path, | ||
1304 | unsigned int put_path_length, | ||
1305 | struct GNUNET_TIME_Absolute exp, | ||
1306 | const struct GNUNET_HashCode * key, | ||
1307 | const void *data, | ||
1308 | size_t size) | ||
1309 | { | ||
1310 | struct ClientMonitorRecord *m; | ||
1311 | struct ClientList **cl; | ||
1312 | unsigned int cl_size; | ||
1313 | |||
1314 | cl = NULL; | ||
1315 | cl_size = 0; | ||
1316 | for (m = monitor_head; NULL != m; m = m->next) | ||
1317 | { | ||
1318 | if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) && | ||
1319 | (NULL == m->key || | ||
1320 | memcmp (key, m->key, sizeof(struct GNUNET_HashCode)) == 0)) | ||
1321 | { | ||
1322 | struct PendingMessage *pm; | ||
1323 | struct GNUNET_DHT_MonitorGetRespMessage *mmsg; | ||
1324 | struct GNUNET_PeerIdentity *path; | ||
1325 | size_t msize; | ||
1326 | unsigned int i; | ||
1327 | |||
1328 | /* Don't send duplicates */ | ||
1329 | for (i = 0; i < cl_size; i++) | ||
1330 | if (cl[i] == m->client) | ||
1331 | break; | ||
1332 | if (i < cl_size) | ||
1333 | continue; | ||
1334 | GNUNET_array_append (cl, cl_size, m->client); | ||
1335 | |||
1336 | msize = size; | ||
1337 | msize += (get_path_length + put_path_length) | ||
1338 | * sizeof (struct GNUNET_PeerIdentity); | ||
1339 | msize += sizeof (struct GNUNET_DHT_MonitorGetRespMessage); | ||
1340 | msize += sizeof (struct PendingMessage); | ||
1341 | pm = GNUNET_malloc (msize); | ||
1342 | mmsg = (struct GNUNET_DHT_MonitorGetRespMessage *) &pm[1]; | ||
1343 | pm->msg = (struct GNUNET_MessageHeader *) mmsg; | ||
1344 | mmsg->header.size = htons (msize - sizeof (struct PendingMessage)); | ||
1345 | mmsg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_GET_RESP); | ||
1346 | mmsg->type = htonl(type); | ||
1347 | mmsg->put_path_length = htonl(put_path_length); | ||
1348 | mmsg->get_path_length = htonl(get_path_length); | ||
1349 | path = (struct GNUNET_PeerIdentity *) &mmsg[1]; | ||
1350 | if (put_path_length > 0) | ||
1351 | { | ||
1352 | memcpy (path, put_path, | ||
1353 | put_path_length * sizeof (struct GNUNET_PeerIdentity)); | ||
1354 | path = &path[put_path_length]; | ||
1355 | } | ||
1356 | if (get_path_length > 0) | ||
1357 | memcpy (path, get_path, | ||
1358 | get_path_length * sizeof (struct GNUNET_PeerIdentity)); | ||
1359 | mmsg->expiration_time = GNUNET_TIME_absolute_hton(exp); | ||
1360 | memcpy (&mmsg->key, key, sizeof (struct GNUNET_HashCode)); | ||
1361 | if (size > 0) | ||
1362 | memcpy (&path[get_path_length], data, size); | ||
1363 | add_pending_message (m->client, pm); | ||
1364 | } | ||
1365 | } | ||
1366 | GNUNET_free_non_null (cl); | ||
1367 | } | ||
1368 | |||
1369 | |||
1370 | /** | ||
1371 | * Check if some client is monitoring PUT messages and notify | ||
1372 | * them in that case. | ||
1373 | * | ||
1374 | * @param options Options, for instance RecordRoute, DemultiplexEverywhere. | ||
1375 | * @param type The type of data in the request. | ||
1376 | * @param hop_count Hop count so far. | ||
1377 | * @param path_length number of entries in path (or 0 if not recorded). | ||
1378 | * @param path peers on the PUT path (or NULL if not recorded). | ||
1379 | * @param desired_replication_level Desired replication level. | ||
1380 | * @param exp Expiration time of the data. | ||
1381 | * @param key Key under which data is to be stored. | ||
1382 | * @param data Pointer to the data carried. | ||
1383 | * @param size Number of bytes in data. | ||
1384 | */ | ||
1385 | void | ||
1386 | GDS_CLIENTS_process_put (uint32_t options, | ||
1387 | enum GNUNET_BLOCK_Type type, | ||
1388 | uint32_t hop_count, | ||
1389 | uint32_t desired_replication_level, | ||
1390 | unsigned int path_length, | ||
1391 | const struct GNUNET_PeerIdentity *path, | ||
1392 | struct GNUNET_TIME_Absolute exp, | ||
1393 | const struct GNUNET_HashCode * key, | ||
1394 | const void *data, | ||
1395 | size_t size) | ||
1396 | { | ||
1397 | struct ClientMonitorRecord *m; | ||
1398 | struct ClientList **cl; | ||
1399 | unsigned int cl_size; | ||
1400 | |||
1401 | cl = NULL; | ||
1402 | cl_size = 0; | ||
1403 | for (m = monitor_head; NULL != m; m = m->next) | ||
1404 | { | ||
1405 | if ((GNUNET_BLOCK_TYPE_ANY == m->type || m->type == type) && | ||
1406 | (NULL == m->key || | ||
1407 | memcmp (key, m->key, sizeof(struct GNUNET_HashCode)) == 0)) | ||
1408 | { | ||
1409 | struct PendingMessage *pm; | ||
1410 | struct GNUNET_DHT_MonitorPutMessage *mmsg; | ||
1411 | struct GNUNET_PeerIdentity *msg_path; | ||
1412 | size_t msize; | ||
1413 | unsigned int i; | ||
1414 | |||
1415 | /* Don't send duplicates */ | ||
1416 | for (i = 0; i < cl_size; i++) | ||
1417 | if (cl[i] == m->client) | ||
1418 | break; | ||
1419 | if (i < cl_size) | ||
1420 | continue; | ||
1421 | GNUNET_array_append (cl, cl_size, m->client); | ||
1422 | |||
1423 | msize = size; | ||
1424 | msize += path_length * sizeof (struct GNUNET_PeerIdentity); | ||
1425 | msize += sizeof (struct GNUNET_DHT_MonitorPutMessage); | ||
1426 | msize += sizeof (struct PendingMessage); | ||
1427 | pm = GNUNET_malloc (msize); | ||
1428 | mmsg = (struct GNUNET_DHT_MonitorPutMessage *) &pm[1]; | ||
1429 | pm->msg = (struct GNUNET_MessageHeader *) mmsg; | ||
1430 | mmsg->header.size = htons (msize - sizeof (struct PendingMessage)); | ||
1431 | mmsg->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_MONITOR_PUT); | ||
1432 | mmsg->options = htonl(options); | ||
1433 | mmsg->type = htonl(type); | ||
1434 | mmsg->hop_count = htonl(hop_count); | ||
1435 | mmsg->desired_replication_level = htonl(desired_replication_level); | ||
1436 | mmsg->put_path_length = htonl(path_length); | ||
1437 | msg_path = (struct GNUNET_PeerIdentity *) &mmsg[1]; | ||
1438 | if (path_length > 0) | ||
1439 | { | ||
1440 | memcpy (msg_path, path, | ||
1441 | path_length * sizeof (struct GNUNET_PeerIdentity)); | ||
1442 | } | ||
1443 | mmsg->expiration_time = GNUNET_TIME_absolute_hton(exp); | ||
1444 | memcpy (&mmsg->key, key, sizeof (struct GNUNET_HashCode)); | ||
1445 | if (size > 0) | ||
1446 | memcpy (&msg_path[path_length], data, size); | ||
1447 | add_pending_message (m->client, pm); | ||
1448 | } | ||
1449 | } | ||
1450 | GNUNET_free_non_null (cl); | ||
1451 | } | ||
1452 | |||
1453 | |||
1454 | /** | ||
1455 | * Initialize client subsystem. | ||
1456 | * | ||
1457 | * @param server the initialized server | ||
1458 | */ | ||
1459 | void | ||
1460 | GDS_CLIENTS_init (struct GNUNET_SERVER_Handle *server) | ||
1461 | { | ||
1462 | static struct GNUNET_SERVER_MessageHandler plugin_handlers[] = { | ||
1463 | {&handle_dht_local_put, NULL, | ||
1464 | GNUNET_MESSAGE_TYPE_DHT_CLIENT_PUT, 0}, | ||
1465 | {&handle_dht_local_get, NULL, | ||
1466 | GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET, 0}, | ||
1467 | {&handle_dht_local_get_stop, NULL, | ||
1468 | GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_STOP, | ||
1469 | sizeof (struct GNUNET_DHT_ClientGetStopMessage)}, | ||
1470 | {&handle_dht_local_monitor, NULL, | ||
1471 | GNUNET_MESSAGE_TYPE_DHT_MONITOR_START, | ||
1472 | sizeof (struct GNUNET_DHT_MonitorStartStopMessage)}, | ||
1473 | {&handle_dht_local_monitor_stop, NULL, | ||
1474 | GNUNET_MESSAGE_TYPE_DHT_MONITOR_STOP, | ||
1475 | sizeof (struct GNUNET_DHT_MonitorStartStopMessage)}, | ||
1476 | {&handle_dht_local_get_result_seen, NULL, | ||
1477 | GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_RESULTS_KNOWN, 0}, | ||
1478 | {NULL, NULL, 0, 0} | ||
1479 | }; | ||
1480 | forward_map = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO); | ||
1481 | retry_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN); | ||
1482 | GNUNET_SERVER_add_handlers (server, plugin_handlers); | ||
1483 | GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL); | ||
1484 | } | ||
1485 | |||
1486 | |||
1487 | /** | ||
1488 | * Shutdown client subsystem. | ||
1489 | */ | ||
1490 | void | ||
1491 | GDS_CLIENTS_done () | ||
1492 | { | ||
1493 | GNUNET_assert (client_head == NULL); | ||
1494 | GNUNET_assert (client_tail == NULL); | ||
1495 | if (GNUNET_SCHEDULER_NO_TASK != retry_task) | ||
1496 | { | ||
1497 | GNUNET_SCHEDULER_cancel (retry_task); | ||
1498 | retry_task = GNUNET_SCHEDULER_NO_TASK; | ||
1499 | } | ||
1500 | if (NULL != retry_heap) | ||
1501 | { | ||
1502 | GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (retry_heap)); | ||
1503 | GNUNET_CONTAINER_heap_destroy (retry_heap); | ||
1504 | retry_heap = NULL; | ||
1505 | } | ||
1506 | if (NULL != forward_map) | ||
1507 | { | ||
1508 | GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (forward_map)); | ||
1509 | GNUNET_CONTAINER_multihashmap_destroy (forward_map); | ||
1510 | forward_map = NULL; | ||
1511 | } | ||
1512 | } | ||
1513 | |||
1514 | /* end of gnunet-service-dht_clients.c */ | ||
diff --git a/src/dht/gnunet-service-xdht_clients.h b/src/dht/gnunet-service-xdht_clients.h new file mode 100644 index 000000000..dd3cb4d63 --- /dev/null +++ b/src/dht/gnunet-service-xdht_clients.h | |||
@@ -0,0 +1,147 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file dht/gnunet-service-xdht_clients.h | ||
23 | * @brief GNUnet DHT service's client management code | ||
24 | * @author Christian Grothoff | ||
25 | * @author Nathan Evans | ||
26 | */ | ||
27 | #ifndef GNUNET_SERVICE_DHT_CLIENT_H | ||
28 | #define GNUNET_SERVICE_DHT_CLIENT_H | ||
29 | |||
30 | #include "gnunet_util_lib.h" | ||
31 | #include "gnunet_block_lib.h" | ||
32 | |||
33 | /** | ||
34 | * Handle a reply we've received from another peer. If the reply | ||
35 | * matches any of our pending queries, forward it to the respective | ||
36 | * client(s). | ||
37 | * | ||
38 | * @param expiration when will the reply expire | ||
39 | * @param key the query this reply is for | ||
40 | * @param get_path_length number of peers in 'get_path' | ||
41 | * @param get_path path the reply took on get | ||
42 | * @param put_path_length number of peers in 'put_path' | ||
43 | * @param put_path path the reply took on put | ||
44 | * @param type type of the reply | ||
45 | * @param data_size number of bytes in 'data' | ||
46 | * @param data application payload data | ||
47 | */ | ||
48 | void | ||
49 | GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration, | ||
50 | const struct GNUNET_HashCode * key, | ||
51 | unsigned int get_path_length, | ||
52 | const struct GNUNET_PeerIdentity *get_path, | ||
53 | unsigned int put_path_length, | ||
54 | const struct GNUNET_PeerIdentity *put_path, | ||
55 | enum GNUNET_BLOCK_Type type, size_t data_size, | ||
56 | const void *data); | ||
57 | |||
58 | |||
59 | /** | ||
60 | * Check if some client is monitoring GET messages and notify | ||
61 | * them in that case. | ||
62 | * | ||
63 | * @param options Options, for instance RecordRoute, DemultiplexEverywhere. | ||
64 | * @param type The type of data in the request. | ||
65 | * @param hop_count Hop count so far. | ||
66 | * @param path_length number of entries in path (or 0 if not recorded). | ||
67 | * @param path peers on the GET path (or NULL if not recorded). | ||
68 | * @param desired_replication_level Desired replication level. | ||
69 | * @param key Key of the requested data. | ||
70 | */ | ||
71 | void | ||
72 | GDS_CLIENTS_process_get (uint32_t options, | ||
73 | enum GNUNET_BLOCK_Type type, | ||
74 | uint32_t hop_count, | ||
75 | uint32_t desired_replication_level, | ||
76 | unsigned int path_length, | ||
77 | const struct GNUNET_PeerIdentity *path, | ||
78 | const struct GNUNET_HashCode * key); | ||
79 | |||
80 | /** | ||
81 | * Check if some client is monitoring GET RESP messages and notify | ||
82 | * them in that case. | ||
83 | * | ||
84 | * @param type The type of data in the result. | ||
85 | * @param get_path Peers on GET path (or NULL if not recorded). | ||
86 | * @param get_path_length number of entries in get_path. | ||
87 | * @param put_path peers on the PUT path (or NULL if not recorded). | ||
88 | * @param put_path_length number of entries in get_path. | ||
89 | * @param exp Expiration time of the data. | ||
90 | * @param key Key of the data. | ||
91 | * @param data Pointer to the result data. | ||
92 | * @param size Number of bytes in data. | ||
93 | */ | ||
94 | void | ||
95 | GDS_CLIENTS_process_get_resp (enum GNUNET_BLOCK_Type type, | ||
96 | const struct GNUNET_PeerIdentity *get_path, | ||
97 | unsigned int get_path_length, | ||
98 | const struct GNUNET_PeerIdentity *put_path, | ||
99 | unsigned int put_path_length, | ||
100 | struct GNUNET_TIME_Absolute exp, | ||
101 | const struct GNUNET_HashCode * key, | ||
102 | const void *data, | ||
103 | size_t size); | ||
104 | |||
105 | /** | ||
106 | * Check if some client is monitoring PUT messages and notify | ||
107 | * them in that case. | ||
108 | * | ||
109 | * @param options Options, for instance RecordRoute, DemultiplexEverywhere. | ||
110 | * @param type The type of data in the request. | ||
111 | * @param hop_count Hop count so far. | ||
112 | * @param path_length number of entries in path (or 0 if not recorded). | ||
113 | * @param path peers on the PUT path (or NULL if not recorded). | ||
114 | * @param desired_replication_level Desired replication level. | ||
115 | * @param exp Expiration time of the data. | ||
116 | * @param key Key under which data is to be stored. | ||
117 | * @param data Pointer to the data carried. | ||
118 | * @param size Number of bytes in data. | ||
119 | */ | ||
120 | void | ||
121 | GDS_CLIENTS_process_put (uint32_t options, | ||
122 | enum GNUNET_BLOCK_Type type, | ||
123 | uint32_t hop_count, | ||
124 | uint32_t desired_replication_level, | ||
125 | unsigned int path_length, | ||
126 | const struct GNUNET_PeerIdentity *path, | ||
127 | struct GNUNET_TIME_Absolute exp, | ||
128 | const struct GNUNET_HashCode * key, | ||
129 | const void *data, | ||
130 | size_t size); | ||
131 | |||
132 | /** | ||
133 | * Initialize client subsystem. | ||
134 | * | ||
135 | * @param server the initialized server | ||
136 | */ | ||
137 | void | ||
138 | GDS_CLIENTS_init (struct GNUNET_SERVER_Handle *server); | ||
139 | |||
140 | |||
141 | /** | ||
142 | * Shutdown client subsystem. | ||
143 | */ | ||
144 | void | ||
145 | GDS_CLIENTS_done (void); | ||
146 | |||
147 | #endif | ||
diff --git a/src/dht/gnunet-service-xdht_datacache.c b/src/dht/gnunet-service-xdht_datacache.c new file mode 100644 index 000000000..77902e4a9 --- /dev/null +++ b/src/dht/gnunet-service-xdht_datacache.c | |||
@@ -0,0 +1,275 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file dht/gnunet-service-xdht_datacache.c | ||
23 | * @brief GNUnet DHT service's datacache integration | ||
24 | * @author Christian Grothoff | ||
25 | * @author Nathan Evans | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_datacache_lib.h" | ||
29 | #include "gnunet-service-dht_clients.h" | ||
30 | #include "gnunet-service-dht_datacache.h" | ||
31 | #include "gnunet-service-dht_routing.h" | ||
32 | #include "gnunet-service-dht.h" | ||
33 | |||
34 | #define LOG(kind,...) GNUNET_log_from (kind, "dht-dtcache",__VA_ARGS__) | ||
35 | |||
36 | |||
37 | /** | ||
38 | * Handle to the datacache service (for inserting/retrieving data) | ||
39 | */ | ||
40 | static struct GNUNET_DATACACHE_Handle *datacache; | ||
41 | |||
42 | |||
43 | /** | ||
44 | * Handle a datum we've received from another peer. Cache if | ||
45 | * possible. | ||
46 | * | ||
47 | * @param expiration when will the reply expire | ||
48 | * @param key the query this reply is for | ||
49 | * @param put_path_length number of peers in 'put_path' | ||
50 | * @param put_path path the reply took on put | ||
51 | * @param type type of the reply | ||
52 | * @param data_size number of bytes in 'data' | ||
53 | * @param data application payload data | ||
54 | */ | ||
55 | void | ||
56 | GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration, | ||
57 | const struct GNUNET_HashCode * key, | ||
58 | unsigned int put_path_length, | ||
59 | const struct GNUNET_PeerIdentity *put_path, | ||
60 | enum GNUNET_BLOCK_Type type, size_t data_size, | ||
61 | const void *data) | ||
62 | { | ||
63 | int r; | ||
64 | |||
65 | if (NULL == datacache) | ||
66 | { | ||
67 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
68 | _("%s request received, but have no datacache!\n"), "PUT"); | ||
69 | return; | ||
70 | } | ||
71 | if (data_size >= GNUNET_SERVER_MAX_MESSAGE_SIZE) | ||
72 | { | ||
73 | GNUNET_break (0); | ||
74 | return; | ||
75 | } | ||
76 | /* Put size is actual data size plus struct overhead plus path length (if any) */ | ||
77 | GNUNET_STATISTICS_update (GDS_stats, | ||
78 | gettext_noop ("# ITEMS stored in datacache"), 1, | ||
79 | GNUNET_NO); | ||
80 | r = GNUNET_DATACACHE_put (datacache, key, data_size, data, type, expiration, | ||
81 | put_path_length, put_path); | ||
82 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
83 | "DATACACHE PUT for key %s [%u] completed (%d) after %u hops\n", | ||
84 | GNUNET_h2s (key), data_size, r, put_path_length); | ||
85 | } | ||
86 | |||
87 | |||
88 | /** | ||
89 | * Context containing information about a GET request. | ||
90 | */ | ||
91 | struct GetRequestContext | ||
92 | { | ||
93 | /** | ||
94 | * extended query (see gnunet_block_lib.h). | ||
95 | */ | ||
96 | const void *xquery; | ||
97 | |||
98 | /** | ||
99 | * Bloomfilter to filter out duplicate replies (updated) | ||
100 | */ | ||
101 | struct GNUNET_CONTAINER_BloomFilter **reply_bf; | ||
102 | |||
103 | /** | ||
104 | * The key this request was about | ||
105 | */ | ||
106 | struct GNUNET_HashCode key; | ||
107 | |||
108 | /** | ||
109 | * Number of bytes in xquery. | ||
110 | */ | ||
111 | size_t xquery_size; | ||
112 | |||
113 | /** | ||
114 | * Mutator value for the reply_bf, see gnunet_block_lib.h | ||
115 | */ | ||
116 | uint32_t reply_bf_mutator; | ||
117 | |||
118 | /** | ||
119 | * Return value to give back. | ||
120 | */ | ||
121 | enum GNUNET_BLOCK_EvaluationResult eval; | ||
122 | }; | ||
123 | |||
124 | |||
125 | /** | ||
126 | * Iterator for local get request results, | ||
127 | * | ||
128 | * @param cls closure for iterator, a DatacacheGetContext | ||
129 | * @param exp when does this value expire? | ||
130 | * @param key the key this data is stored under | ||
131 | * @param size the size of the data identified by key | ||
132 | * @param data the actual data | ||
133 | * @param type the type of the data | ||
134 | * @param put_path_length number of peers in 'put_path' | ||
135 | * @param put_path path the reply took on put | ||
136 | * @return GNUNET_OK to continue iteration, anything else | ||
137 | * to stop iteration. | ||
138 | */ | ||
139 | static int | ||
140 | datacache_get_iterator (void *cls, | ||
141 | const struct GNUNET_HashCode * key, size_t size, | ||
142 | const char *data, enum GNUNET_BLOCK_Type type, | ||
143 | struct GNUNET_TIME_Absolute exp, | ||
144 | unsigned int put_path_length, | ||
145 | const struct GNUNET_PeerIdentity *put_path) | ||
146 | { | ||
147 | struct GetRequestContext *ctx = cls; | ||
148 | enum GNUNET_BLOCK_EvaluationResult eval; | ||
149 | |||
150 | eval = | ||
151 | GNUNET_BLOCK_evaluate (GDS_block_context, type, key, ctx->reply_bf, | ||
152 | ctx->reply_bf_mutator, ctx->xquery, | ||
153 | ctx->xquery_size, data, size); | ||
154 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
155 | "Found reply for query %s in datacache, evaluation result is %d\n", | ||
156 | GNUNET_h2s (key), (int) eval); | ||
157 | ctx->eval = eval; | ||
158 | switch (eval) | ||
159 | { | ||
160 | case GNUNET_BLOCK_EVALUATION_OK_MORE: | ||
161 | case GNUNET_BLOCK_EVALUATION_OK_LAST: | ||
162 | /* forward to local clients */ | ||
163 | GNUNET_STATISTICS_update (GDS_stats, | ||
164 | gettext_noop | ||
165 | ("# Good RESULTS found in datacache"), 1, | ||
166 | GNUNET_NO); | ||
167 | GDS_CLIENTS_handle_reply (exp, key, 0, NULL, put_path_length, put_path, | ||
168 | type, size, data); | ||
169 | /* forward to other peers */ | ||
170 | GDS_ROUTING_process (type, exp, key, put_path_length, put_path, 0, NULL, | ||
171 | data, size); | ||
172 | break; | ||
173 | case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE: | ||
174 | GNUNET_STATISTICS_update (GDS_stats, | ||
175 | gettext_noop | ||
176 | ("# Duplicate RESULTS found in datacache"), 1, | ||
177 | GNUNET_NO); | ||
178 | break; | ||
179 | case GNUNET_BLOCK_EVALUATION_RESULT_INVALID: | ||
180 | GNUNET_STATISTICS_update (GDS_stats, | ||
181 | gettext_noop | ||
182 | ("# Invalid RESULTS found in datacache"), 1, | ||
183 | GNUNET_NO); | ||
184 | break; | ||
185 | case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT: | ||
186 | GNUNET_STATISTICS_update (GDS_stats, | ||
187 | gettext_noop | ||
188 | ("# Irrelevant RESULTS found in datacache"), 1, | ||
189 | GNUNET_NO); | ||
190 | break; | ||
191 | case GNUNET_BLOCK_EVALUATION_REQUEST_VALID: | ||
192 | GNUNET_break (0); | ||
193 | break; | ||
194 | case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID: | ||
195 | GNUNET_break_op (0); | ||
196 | return GNUNET_SYSERR; | ||
197 | case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED: | ||
198 | GNUNET_STATISTICS_update (GDS_stats, | ||
199 | gettext_noop | ||
200 | ("# Unsupported RESULTS found in datacache"), 1, | ||
201 | GNUNET_NO); | ||
202 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
203 | _("Unsupported block type (%u) in local response!\n"), type); | ||
204 | break; | ||
205 | } | ||
206 | return (eval == GNUNET_BLOCK_EVALUATION_OK_LAST) ? GNUNET_NO : GNUNET_OK; | ||
207 | } | ||
208 | |||
209 | |||
210 | /** | ||
211 | * Handle a GET request we've received from another peer. | ||
212 | * | ||
213 | * @param key the query | ||
214 | * @param type requested data type | ||
215 | * @param xquery extended query | ||
216 | * @param xquery_size number of bytes in xquery | ||
217 | * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL | ||
218 | * @param reply_bf_mutator mutation value for reply_bf | ||
219 | * @return evaluation result for the local replies | ||
220 | */ | ||
221 | enum GNUNET_BLOCK_EvaluationResult | ||
222 | GDS_DATACACHE_handle_get (const struct GNUNET_HashCode * key, | ||
223 | enum GNUNET_BLOCK_Type type, const void *xquery, | ||
224 | size_t xquery_size, | ||
225 | struct GNUNET_CONTAINER_BloomFilter **reply_bf, | ||
226 | uint32_t reply_bf_mutator) | ||
227 | { | ||
228 | struct GetRequestContext ctx; | ||
229 | unsigned int r; | ||
230 | |||
231 | if (datacache == NULL) | ||
232 | return GNUNET_BLOCK_EVALUATION_REQUEST_VALID; | ||
233 | GNUNET_STATISTICS_update (GDS_stats, | ||
234 | gettext_noop ("# GET requests given to datacache"), | ||
235 | 1, GNUNET_NO); | ||
236 | ctx.eval = GNUNET_BLOCK_EVALUATION_REQUEST_VALID; | ||
237 | ctx.key = *key; | ||
238 | ctx.xquery = xquery; | ||
239 | ctx.xquery_size = xquery_size; | ||
240 | ctx.reply_bf = reply_bf; | ||
241 | ctx.reply_bf_mutator = reply_bf_mutator; | ||
242 | r = GNUNET_DATACACHE_get (datacache, key, type, &datacache_get_iterator, | ||
243 | &ctx); | ||
244 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
245 | "DATACACHE GET for key %s completed (%d). %u results found.\n", | ||
246 | GNUNET_h2s (key), ctx.eval, r); | ||
247 | return ctx.eval; | ||
248 | } | ||
249 | |||
250 | |||
251 | /** | ||
252 | * Initialize datacache subsystem. | ||
253 | */ | ||
254 | void | ||
255 | GDS_DATACACHE_init () | ||
256 | { | ||
257 | datacache = GNUNET_DATACACHE_create (GDS_cfg, "dhtcache"); | ||
258 | } | ||
259 | |||
260 | |||
261 | /** | ||
262 | * Shutdown datacache subsystem. | ||
263 | */ | ||
264 | void | ||
265 | GDS_DATACACHE_done () | ||
266 | { | ||
267 | if (datacache != NULL) | ||
268 | { | ||
269 | GNUNET_DATACACHE_destroy (datacache); | ||
270 | datacache = NULL; | ||
271 | } | ||
272 | } | ||
273 | |||
274 | |||
275 | /* end of gnunet-service-dht_datacache.c */ | ||
diff --git a/src/dht/gnunet-service-xdht_datacache.h b/src/dht/gnunet-service-xdht_datacache.h new file mode 100644 index 000000000..69906bf67 --- /dev/null +++ b/src/dht/gnunet-service-xdht_datacache.h | |||
@@ -0,0 +1,86 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file dht/gnunet-service-xdht_datacache.h | ||
23 | * @brief GNUnet DHT service's datacache integration | ||
24 | * @author Christian Grothoff | ||
25 | * @author Nathan Evans | ||
26 | */ | ||
27 | #ifndef GNUNET_SERVICE_DHT_DATACACHE_H | ||
28 | #define GNUNET_SERVICE_DHT_DATACACHE_H | ||
29 | |||
30 | #include "gnunet_util_lib.h" | ||
31 | #include "gnunet_block_lib.h" | ||
32 | |||
33 | /** | ||
34 | * Handle a datum we've received from another peer. Cache if | ||
35 | * possible. | ||
36 | * | ||
37 | * @param expiration when will the reply expire | ||
38 | * @param key the query this reply is for | ||
39 | * @param put_path_length number of peers in 'put_path' | ||
40 | * @param put_path path the reply took on put | ||
41 | * @param type type of the reply | ||
42 | * @param data_size number of bytes in 'data' | ||
43 | * @param data application payload data | ||
44 | */ | ||
45 | void | ||
46 | GDS_DATACACHE_handle_put (struct GNUNET_TIME_Absolute expiration, | ||
47 | const struct GNUNET_HashCode * key, | ||
48 | unsigned int put_path_length, | ||
49 | const struct GNUNET_PeerIdentity *put_path, | ||
50 | enum GNUNET_BLOCK_Type type, size_t data_size, | ||
51 | const void *data); | ||
52 | |||
53 | |||
54 | /** | ||
55 | * Handle a GET request we've received from another peer. | ||
56 | * | ||
57 | * @param key the query | ||
58 | * @param type requested data type | ||
59 | * @param xquery extended query | ||
60 | * @param xquery_size number of bytes in xquery | ||
61 | * @param reply_bf where the reply bf is (to be) stored, possibly updated!, can be NULL | ||
62 | * @param reply_bf_mutator mutation value for reply_bf | ||
63 | * @return evaluation result for the local replies | ||
64 | */ | ||
65 | enum GNUNET_BLOCK_EvaluationResult | ||
66 | GDS_DATACACHE_handle_get (const struct GNUNET_HashCode * key, | ||
67 | enum GNUNET_BLOCK_Type type, const void *xquery, | ||
68 | size_t xquery_size, | ||
69 | struct GNUNET_CONTAINER_BloomFilter **reply_bf, | ||
70 | uint32_t reply_bf_mutator); | ||
71 | |||
72 | |||
73 | /** | ||
74 | * Initialize datacache subsystem. | ||
75 | */ | ||
76 | void | ||
77 | GDS_DATACACHE_init (void); | ||
78 | |||
79 | |||
80 | /** | ||
81 | * Shutdown datacache subsystem. | ||
82 | */ | ||
83 | void | ||
84 | GDS_DATACACHE_done (void); | ||
85 | |||
86 | #endif | ||
diff --git a/src/dht/gnunet-service-xdht_hello.c b/src/dht/gnunet-service-xdht_hello.c new file mode 100644 index 000000000..32f9adf63 --- /dev/null +++ b/src/dht/gnunet-service-xdht_hello.c | |||
@@ -0,0 +1,137 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file dht/gnunet-service-xdht_hello.c | ||
23 | * @brief GNUnet DHT integration with peerinfo | ||
24 | * @author Christian Grothoff | ||
25 | * | ||
26 | * TODO: | ||
27 | * - consider adding mechanism to remove expired HELLOs | ||
28 | */ | ||
29 | #include "platform.h" | ||
30 | #include "gnunet-service-dht.h" | ||
31 | #include "gnunet-service-dht_hello.h" | ||
32 | #include "gnunet_peerinfo_service.h" | ||
33 | |||
34 | |||
35 | /** | ||
36 | * Handle for peerinfo notifications. | ||
37 | */ | ||
38 | static struct GNUNET_PEERINFO_NotifyContext *pnc; | ||
39 | |||
40 | /** | ||
41 | * Hash map of peers to HELLOs. | ||
42 | */ | ||
43 | static struct GNUNET_CONTAINER_MultiPeerMap *peer_to_hello; | ||
44 | |||
45 | |||
46 | /** | ||
47 | * Obtain a peer's HELLO if available | ||
48 | * | ||
49 | * @param peer peer to look for a HELLO from | ||
50 | * @return HELLO for the given peer | ||
51 | */ | ||
52 | const struct GNUNET_HELLO_Message * | ||
53 | GDS_HELLO_get (const struct GNUNET_PeerIdentity *peer) | ||
54 | { | ||
55 | if (NULL == peer_to_hello) | ||
56 | return NULL; | ||
57 | return GNUNET_CONTAINER_multipeermap_get (peer_to_hello, peer); | ||
58 | } | ||
59 | |||
60 | |||
61 | /** | ||
62 | * Function called for each HELLO known to PEERINFO. | ||
63 | * | ||
64 | * @param cls closure | ||
65 | * @param peer id of the peer, NULL for last call | ||
66 | * @param hello hello message for the peer (can be NULL) | ||
67 | * @param err_msg error message (not used) | ||
68 | */ | ||
69 | static void | ||
70 | process_hello (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
71 | const struct GNUNET_HELLO_Message *hello, const char *err_msg) | ||
72 | { | ||
73 | struct GNUNET_TIME_Absolute ex; | ||
74 | struct GNUNET_HELLO_Message *hm; | ||
75 | |||
76 | if (hello == NULL) | ||
77 | return; | ||
78 | ex = GNUNET_HELLO_get_last_expiration (hello); | ||
79 | if (0 == GNUNET_TIME_absolute_get_remaining (ex).rel_value_us) | ||
80 | return; | ||
81 | GNUNET_STATISTICS_update (GDS_stats, | ||
82 | gettext_noop ("# HELLOs obtained from peerinfo"), 1, | ||
83 | GNUNET_NO); | ||
84 | hm = GNUNET_CONTAINER_multipeermap_get (peer_to_hello, peer); | ||
85 | GNUNET_free_non_null (hm); | ||
86 | hm = GNUNET_malloc (GNUNET_HELLO_size (hello)); | ||
87 | memcpy (hm, hello, GNUNET_HELLO_size (hello)); | ||
88 | GNUNET_assert (GNUNET_SYSERR != | ||
89 | GNUNET_CONTAINER_multipeermap_put (peer_to_hello, | ||
90 | peer, hm, | ||
91 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE)); | ||
92 | } | ||
93 | |||
94 | |||
95 | /** | ||
96 | * Initialize HELLO subsystem. | ||
97 | */ | ||
98 | void | ||
99 | GDS_HELLO_init () | ||
100 | { | ||
101 | pnc = GNUNET_PEERINFO_notify (GDS_cfg, GNUNET_NO, &process_hello, NULL); | ||
102 | peer_to_hello = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO); | ||
103 | } | ||
104 | |||
105 | |||
106 | /** | ||
107 | * Free memory occopied by the HELLO. | ||
108 | */ | ||
109 | static int | ||
110 | free_hello (void *cls, | ||
111 | const struct GNUNET_PeerIdentity *key, | ||
112 | void *hello) | ||
113 | { | ||
114 | GNUNET_free (hello); | ||
115 | return GNUNET_OK; | ||
116 | } | ||
117 | |||
118 | |||
119 | /** | ||
120 | * Shutdown HELLO subsystem. | ||
121 | */ | ||
122 | void | ||
123 | GDS_HELLO_done () | ||
124 | { | ||
125 | if (NULL != pnc) | ||
126 | { | ||
127 | GNUNET_PEERINFO_notify_cancel (pnc); | ||
128 | pnc = NULL; | ||
129 | } | ||
130 | if (NULL != peer_to_hello) | ||
131 | { | ||
132 | GNUNET_CONTAINER_multipeermap_iterate (peer_to_hello, &free_hello, NULL); | ||
133 | GNUNET_CONTAINER_multipeermap_destroy (peer_to_hello); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | /* end of gnunet-service-dht_hello.c */ | ||
diff --git a/src/dht/gnunet-service-xdht_hello.h b/src/dht/gnunet-service-xdht_hello.h new file mode 100644 index 000000000..2519b39f4 --- /dev/null +++ b/src/dht/gnunet-service-xdht_hello.h | |||
@@ -0,0 +1,55 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file dht/gnunet-service-xdht_hello.h | ||
23 | * @brief GNUnet DHT integration with peerinfo | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #ifndef GNUNET_SERVICE_DHT_HELLO_H | ||
27 | #define GNUNET_SERVICE_DHT_HELLO_H | ||
28 | |||
29 | #include "gnunet_util_lib.h" | ||
30 | #include "gnunet_hello_lib.h" | ||
31 | |||
32 | /** | ||
33 | * Obtain a peer's HELLO if available | ||
34 | * | ||
35 | * @param peer peer to look for a HELLO from | ||
36 | * @return HELLO for the given peer | ||
37 | */ | ||
38 | const struct GNUNET_HELLO_Message * | ||
39 | GDS_HELLO_get (const struct GNUNET_PeerIdentity *peer); | ||
40 | |||
41 | |||
42 | /** | ||
43 | * Initialize HELLO subsystem. | ||
44 | */ | ||
45 | void | ||
46 | GDS_HELLO_init (void); | ||
47 | |||
48 | |||
49 | /** | ||
50 | * Shutdown HELLO subsystem. | ||
51 | */ | ||
52 | void | ||
53 | GDS_HELLO_done (void); | ||
54 | |||
55 | #endif | ||
diff --git a/src/dht/gnunet-service-xdht_neighbours.c b/src/dht/gnunet-service-xdht_neighbours.c new file mode 100644 index 000000000..f15a33c79 --- /dev/null +++ b/src/dht/gnunet-service-xdht_neighbours.c | |||
@@ -0,0 +1,2227 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009-2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file dht/gnunet-service-xdht_neighbours.c | ||
23 | * @brief GNUnet DHT service's bucket and neighbour management code | ||
24 | * @author Christian Grothoff | ||
25 | * @author Nathan Evans | ||
26 | */ | ||
27 | |||
28 | #include "platform.h" | ||
29 | #include "gnunet_util_lib.h" | ||
30 | #include "gnunet_block_lib.h" | ||
31 | #include "gnunet_hello_lib.h" | ||
32 | #include "gnunet_constants.h" | ||
33 | #include "gnunet_protocols.h" | ||
34 | #include "gnunet_nse_service.h" | ||
35 | #include "gnunet_ats_service.h" | ||
36 | #include "gnunet_core_service.h" | ||
37 | #include "gnunet_datacache_lib.h" | ||
38 | #include "gnunet_transport_service.h" | ||
39 | #include "gnunet_hello_lib.h" | ||
40 | #include "gnunet_dht_service.h" | ||
41 | #include "gnunet_statistics_service.h" | ||
42 | #include "gnunet-service-dht.h" | ||
43 | #include "gnunet-service-dht_clients.h" | ||
44 | #include "gnunet-service-dht_datacache.h" | ||
45 | #include "gnunet-service-dht_hello.h" | ||
46 | #include "gnunet-service-dht_neighbours.h" | ||
47 | #include "gnunet-service-dht_nse.h" | ||
48 | #include "gnunet-service-dht_routing.h" | ||
49 | #include <fenv.h> | ||
50 | #include "dht.h" | ||
51 | |||
52 | #define LOG_TRAFFIC(kind,...) GNUNET_log_from (kind, "dht-traffic",__VA_ARGS__) | ||
53 | |||
54 | /** | ||
55 | * How many buckets will we allow total. | ||
56 | */ | ||
57 | #define MAX_BUCKETS sizeof (struct GNUNET_HashCode) * 8 | ||
58 | |||
59 | /** | ||
60 | * What is the maximum number of peers in a given bucket. | ||
61 | */ | ||
62 | #define DEFAULT_BUCKET_SIZE 8 | ||
63 | |||
64 | /** | ||
65 | * Desired replication level for FIND PEER requests | ||
66 | */ | ||
67 | #define FIND_PEER_REPLICATION_LEVEL 4 | ||
68 | |||
69 | /** | ||
70 | * Maximum allowed replication level for all requests. | ||
71 | */ | ||
72 | #define MAXIMUM_REPLICATION_LEVEL 16 | ||
73 | |||
74 | /** | ||
75 | * Maximum allowed number of pending messages per peer. | ||
76 | */ | ||
77 | #define MAXIMUM_PENDING_PER_PEER 64 | ||
78 | |||
79 | /** | ||
80 | * How often to update our preference levels for peers in our routing tables. | ||
81 | */ | ||
82 | #define DHT_DEFAULT_PREFERENCE_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2) | ||
83 | |||
84 | /** | ||
85 | * How long at least to wait before sending another find peer request. | ||
86 | */ | ||
87 | #define DHT_MINIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30) | ||
88 | |||
89 | /** | ||
90 | * How long at most to wait before sending another find peer request. | ||
91 | */ | ||
92 | #define DHT_MAXIMUM_FIND_PEER_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 10) | ||
93 | |||
94 | /** | ||
95 | * How long at most to wait for transmission of a GET request to another peer? | ||
96 | */ | ||
97 | #define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2) | ||
98 | |||
99 | /** | ||
100 | * Hello address expiration | ||
101 | */ | ||
102 | extern struct GNUNET_TIME_Relative hello_expiration; | ||
103 | |||
104 | |||
105 | GNUNET_NETWORK_STRUCT_BEGIN | ||
106 | |||
107 | /** | ||
108 | * P2P PUT message | ||
109 | */ | ||
110 | struct PeerPutMessage | ||
111 | { | ||
112 | /** | ||
113 | * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_PUT | ||
114 | */ | ||
115 | struct GNUNET_MessageHeader header; | ||
116 | |||
117 | /** | ||
118 | * Processing options | ||
119 | */ | ||
120 | uint32_t options GNUNET_PACKED; | ||
121 | |||
122 | /** | ||
123 | * Content type. | ||
124 | */ | ||
125 | uint32_t type GNUNET_PACKED; | ||
126 | |||
127 | /** | ||
128 | * Hop count | ||
129 | */ | ||
130 | uint32_t hop_count GNUNET_PACKED; | ||
131 | |||
132 | /** | ||
133 | * Replication level for this message | ||
134 | */ | ||
135 | uint32_t desired_replication_level GNUNET_PACKED; | ||
136 | |||
137 | /** | ||
138 | * Length of the PUT path that follows (if tracked). | ||
139 | */ | ||
140 | uint32_t put_path_length GNUNET_PACKED; | ||
141 | |||
142 | /** | ||
143 | * When does the content expire? | ||
144 | */ | ||
145 | struct GNUNET_TIME_AbsoluteNBO expiration_time; | ||
146 | |||
147 | /** | ||
148 | * Bloomfilter (for peer identities) to stop circular routes | ||
149 | */ | ||
150 | char bloomfilter[DHT_BLOOM_SIZE]; | ||
151 | |||
152 | /** | ||
153 | * The key we are storing under. | ||
154 | */ | ||
155 | struct GNUNET_HashCode key; | ||
156 | |||
157 | /* put path (if tracked) */ | ||
158 | |||
159 | /* Payload */ | ||
160 | |||
161 | }; | ||
162 | |||
163 | |||
164 | /** | ||
165 | * P2P Result message | ||
166 | */ | ||
167 | struct PeerResultMessage | ||
168 | { | ||
169 | /** | ||
170 | * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT | ||
171 | */ | ||
172 | struct GNUNET_MessageHeader header; | ||
173 | |||
174 | /** | ||
175 | * Content type. | ||
176 | */ | ||
177 | uint32_t type GNUNET_PACKED; | ||
178 | |||
179 | /** | ||
180 | * Length of the PUT path that follows (if tracked). | ||
181 | */ | ||
182 | uint32_t put_path_length GNUNET_PACKED; | ||
183 | |||
184 | /** | ||
185 | * Length of the GET path that follows (if tracked). | ||
186 | */ | ||
187 | uint32_t get_path_length GNUNET_PACKED; | ||
188 | |||
189 | /** | ||
190 | * When does the content expire? | ||
191 | */ | ||
192 | struct GNUNET_TIME_AbsoluteNBO expiration_time; | ||
193 | |||
194 | /** | ||
195 | * The key of the corresponding GET request. | ||
196 | */ | ||
197 | struct GNUNET_HashCode key; | ||
198 | |||
199 | /* put path (if tracked) */ | ||
200 | |||
201 | /* get path (if tracked) */ | ||
202 | |||
203 | /* Payload */ | ||
204 | |||
205 | }; | ||
206 | |||
207 | |||
208 | /** | ||
209 | * P2P GET message | ||
210 | */ | ||
211 | struct PeerGetMessage | ||
212 | { | ||
213 | /** | ||
214 | * Type: #GNUNET_MESSAGE_TYPE_DHT_P2P_GET | ||
215 | */ | ||
216 | struct GNUNET_MessageHeader header; | ||
217 | |||
218 | /** | ||
219 | * Processing options | ||
220 | */ | ||
221 | uint32_t options GNUNET_PACKED; | ||
222 | |||
223 | /** | ||
224 | * Desired content type. | ||
225 | */ | ||
226 | uint32_t type GNUNET_PACKED; | ||
227 | |||
228 | /** | ||
229 | * Hop count | ||
230 | */ | ||
231 | uint32_t hop_count GNUNET_PACKED; | ||
232 | |||
233 | /** | ||
234 | * Desired replication level for this request. | ||
235 | */ | ||
236 | uint32_t desired_replication_level GNUNET_PACKED; | ||
237 | |||
238 | /** | ||
239 | * Size of the extended query. | ||
240 | */ | ||
241 | uint32_t xquery_size; | ||
242 | |||
243 | /** | ||
244 | * Bloomfilter mutator. | ||
245 | */ | ||
246 | uint32_t bf_mutator; | ||
247 | |||
248 | /** | ||
249 | * Bloomfilter (for peer identities) to stop circular routes | ||
250 | */ | ||
251 | char bloomfilter[DHT_BLOOM_SIZE]; | ||
252 | |||
253 | /** | ||
254 | * The key we are looking for. | ||
255 | */ | ||
256 | struct GNUNET_HashCode key; | ||
257 | |||
258 | /* xquery */ | ||
259 | |||
260 | /* result bloomfilter */ | ||
261 | |||
262 | }; | ||
263 | GNUNET_NETWORK_STRUCT_END | ||
264 | |||
265 | /** | ||
266 | * Linked list of messages to send to a particular other peer. | ||
267 | */ | ||
268 | struct P2PPendingMessage | ||
269 | { | ||
270 | /** | ||
271 | * Pointer to next item in the list | ||
272 | */ | ||
273 | struct P2PPendingMessage *next; | ||
274 | |||
275 | /** | ||
276 | * Pointer to previous item in the list | ||
277 | */ | ||
278 | struct P2PPendingMessage *prev; | ||
279 | |||
280 | /** | ||
281 | * Message importance level. FIXME: used? useful? | ||
282 | */ | ||
283 | unsigned int importance; | ||
284 | |||
285 | /** | ||
286 | * When does this message time out? | ||
287 | */ | ||
288 | struct GNUNET_TIME_Absolute timeout; | ||
289 | |||
290 | /** | ||
291 | * Actual message to be sent, allocated at the end of the struct: | ||
292 | * // msg = (cast) &pm[1]; | ||
293 | * // memcpy (&pm[1], data, len); | ||
294 | */ | ||
295 | const struct GNUNET_MessageHeader *msg; | ||
296 | |||
297 | }; | ||
298 | |||
299 | |||
300 | /** | ||
301 | * Entry for a peer in a bucket. | ||
302 | */ | ||
303 | struct PeerInfo | ||
304 | { | ||
305 | /** | ||
306 | * Next peer entry (DLL) | ||
307 | */ | ||
308 | struct PeerInfo *next; | ||
309 | |||
310 | /** | ||
311 | * Prev peer entry (DLL) | ||
312 | */ | ||
313 | struct PeerInfo *prev; | ||
314 | |||
315 | /** | ||
316 | * Count of outstanding messages for peer. | ||
317 | */ | ||
318 | unsigned int pending_count; | ||
319 | |||
320 | /** | ||
321 | * Head of pending messages to be sent to this peer. | ||
322 | */ | ||
323 | struct P2PPendingMessage *head; | ||
324 | |||
325 | /** | ||
326 | * Tail of pending messages to be sent to this peer. | ||
327 | */ | ||
328 | struct P2PPendingMessage *tail; | ||
329 | |||
330 | /** | ||
331 | * Core handle for sending messages to this peer. | ||
332 | */ | ||
333 | struct GNUNET_CORE_TransmitHandle *th; | ||
334 | |||
335 | /** | ||
336 | * Task for scheduling preference updates | ||
337 | */ | ||
338 | GNUNET_SCHEDULER_TaskIdentifier preference_task; | ||
339 | |||
340 | /** | ||
341 | * What is the identity of the peer? | ||
342 | */ | ||
343 | struct GNUNET_PeerIdentity id; | ||
344 | |||
345 | #if 0 | ||
346 | /** | ||
347 | * What is the average latency for replies received? | ||
348 | */ | ||
349 | struct GNUNET_TIME_Relative latency; | ||
350 | |||
351 | /** | ||
352 | * Transport level distance to peer. | ||
353 | */ | ||
354 | unsigned int distance; | ||
355 | #endif | ||
356 | |||
357 | }; | ||
358 | |||
359 | |||
360 | /** | ||
361 | * Peers are grouped into buckets. | ||
362 | */ | ||
363 | struct PeerBucket | ||
364 | { | ||
365 | /** | ||
366 | * Head of DLL | ||
367 | */ | ||
368 | struct PeerInfo *head; | ||
369 | |||
370 | /** | ||
371 | * Tail of DLL | ||
372 | */ | ||
373 | struct PeerInfo *tail; | ||
374 | |||
375 | /** | ||
376 | * Number of peers in the bucket. | ||
377 | */ | ||
378 | unsigned int peers_size; | ||
379 | }; | ||
380 | |||
381 | |||
382 | /** | ||
383 | * Do we cache all results that we are routing in the local datacache? | ||
384 | */ | ||
385 | static int cache_results; | ||
386 | |||
387 | /** | ||
388 | * Should routing details be logged to stderr (for debugging)? | ||
389 | */ | ||
390 | static int log_route_details_stderr; | ||
391 | |||
392 | /** | ||
393 | * The lowest currently used bucket, initially 0 (for 0-bits matching bucket). | ||
394 | */ | ||
395 | static unsigned int closest_bucket; | ||
396 | |||
397 | /** | ||
398 | * How many peers have we added since we sent out our last | ||
399 | * find peer request? | ||
400 | */ | ||
401 | static unsigned int newly_found_peers; | ||
402 | |||
403 | /** | ||
404 | * Option for testing that disables the 'connect' function of the DHT. | ||
405 | */ | ||
406 | static int disable_try_connect; | ||
407 | |||
408 | /** | ||
409 | * The buckets. Array of size MAX_BUCKET_SIZE. Offset 0 means 0 bits matching. | ||
410 | */ | ||
411 | static struct PeerBucket k_buckets[MAX_BUCKETS]; | ||
412 | |||
413 | /** | ||
414 | * Hash map of all known peers, for easy removal from k_buckets on disconnect. | ||
415 | */ | ||
416 | static struct GNUNET_CONTAINER_MultiPeerMap *all_known_peers; | ||
417 | |||
418 | /** | ||
419 | * Maximum size for each bucket. | ||
420 | */ | ||
421 | static unsigned int bucket_size = DEFAULT_BUCKET_SIZE; | ||
422 | |||
423 | /** | ||
424 | * Task that sends FIND PEER requests. | ||
425 | */ | ||
426 | static GNUNET_SCHEDULER_TaskIdentifier find_peer_task; | ||
427 | |||
428 | /** | ||
429 | * Identity of this peer. | ||
430 | */ | ||
431 | static struct GNUNET_PeerIdentity my_identity; | ||
432 | |||
433 | /** | ||
434 | * Hash of the identity of this peer. | ||
435 | */ | ||
436 | static struct GNUNET_HashCode my_identity_hash; | ||
437 | |||
438 | /** | ||
439 | * Handle to CORE. | ||
440 | */ | ||
441 | static struct GNUNET_CORE_Handle *core_api; | ||
442 | |||
443 | /** | ||
444 | * Handle to ATS. | ||
445 | */ | ||
446 | static struct GNUNET_ATS_PerformanceHandle *atsAPI; | ||
447 | |||
448 | |||
449 | |||
450 | /** | ||
451 | * Find the optimal bucket for this key. | ||
452 | * | ||
453 | * @param hc the hashcode to compare our identity to | ||
454 | * @return the proper bucket index, or GNUNET_SYSERR | ||
455 | * on error (same hashcode) | ||
456 | */ | ||
457 | static int | ||
458 | find_bucket (const struct GNUNET_HashCode *hc) | ||
459 | { | ||
460 | unsigned int bits; | ||
461 | |||
462 | bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, hc); | ||
463 | if (bits == MAX_BUCKETS) | ||
464 | { | ||
465 | /* How can all bits match? Got my own ID? */ | ||
466 | GNUNET_break (0); | ||
467 | return GNUNET_SYSERR; | ||
468 | } | ||
469 | return MAX_BUCKETS - bits - 1; | ||
470 | } | ||
471 | |||
472 | |||
473 | /** | ||
474 | * Let GNUnet core know that we like the given peer. | ||
475 | * | ||
476 | * @param cls the `struct PeerInfo` of the peer | ||
477 | * @param tc scheduler context. | ||
478 | */ | ||
479 | static void | ||
480 | update_core_preference (void *cls, | ||
481 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
482 | { | ||
483 | struct PeerInfo *peer = cls; | ||
484 | uint64_t preference; | ||
485 | unsigned int matching; | ||
486 | int bucket; | ||
487 | struct GNUNET_HashCode phash; | ||
488 | |||
489 | peer->preference_task = GNUNET_SCHEDULER_NO_TASK; | ||
490 | if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) | ||
491 | return; | ||
492 | GNUNET_CRYPTO_hash (&peer->id, | ||
493 | sizeof (struct GNUNET_PeerIdentity), | ||
494 | &phash); | ||
495 | matching = | ||
496 | GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, | ||
497 | &phash); | ||
498 | if (matching >= 64) | ||
499 | matching = 63; | ||
500 | bucket = find_bucket (&phash); | ||
501 | if (bucket == GNUNET_SYSERR) | ||
502 | preference = 0; | ||
503 | else | ||
504 | { | ||
505 | GNUNET_assert (k_buckets[bucket].peers_size != 0); | ||
506 | preference = (1LL << matching) / k_buckets[bucket].peers_size; | ||
507 | } | ||
508 | if (preference == 0) | ||
509 | { | ||
510 | peer->preference_task = | ||
511 | GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL, | ||
512 | &update_core_preference, peer); | ||
513 | return; | ||
514 | } | ||
515 | GNUNET_STATISTICS_update (GDS_stats, | ||
516 | gettext_noop ("# Preference updates given to core"), | ||
517 | 1, GNUNET_NO); | ||
518 | GNUNET_ATS_performance_change_preference (atsAPI, &peer->id, | ||
519 | GNUNET_ATS_PREFERENCE_BANDWIDTH, | ||
520 | (double) preference, GNUNET_ATS_PREFERENCE_END); | ||
521 | peer->preference_task = | ||
522 | GNUNET_SCHEDULER_add_delayed (DHT_DEFAULT_PREFERENCE_INTERVAL, | ||
523 | &update_core_preference, peer); | ||
524 | |||
525 | |||
526 | } | ||
527 | |||
528 | |||
529 | /** | ||
530 | * Closure for 'add_known_to_bloom'. | ||
531 | */ | ||
532 | struct BloomConstructorContext | ||
533 | { | ||
534 | /** | ||
535 | * Bloom filter under construction. | ||
536 | */ | ||
537 | struct GNUNET_CONTAINER_BloomFilter *bloom; | ||
538 | |||
539 | /** | ||
540 | * Mutator to use. | ||
541 | */ | ||
542 | uint32_t bf_mutator; | ||
543 | }; | ||
544 | |||
545 | |||
546 | /** | ||
547 | * Add each of the peers we already know to the bloom filter of | ||
548 | * the request so that we don't get duplicate HELLOs. | ||
549 | * | ||
550 | * @param cls the 'struct BloomConstructorContext'. | ||
551 | * @param key peer identity to add to the bloom filter | ||
552 | * @param value value the peer information (unused) | ||
553 | * @return #GNUNET_YES (we should continue to iterate) | ||
554 | */ | ||
555 | static int | ||
556 | add_known_to_bloom (void *cls, | ||
557 | const struct GNUNET_PeerIdentity *key, | ||
558 | void *value) | ||
559 | { | ||
560 | struct BloomConstructorContext *ctx = cls; | ||
561 | struct GNUNET_HashCode key_hash; | ||
562 | struct GNUNET_HashCode mh; | ||
563 | |||
564 | GNUNET_CRYPTO_hash (key, sizeof (struct GNUNET_PeerIdentity), &key_hash); | ||
565 | GNUNET_BLOCK_mingle_hash (&key_hash, ctx->bf_mutator, &mh); | ||
566 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
567 | "Adding known peer (%s) to bloomfilter for FIND PEER with mutation %u\n", | ||
568 | GNUNET_i2s (key), ctx->bf_mutator); | ||
569 | GNUNET_CONTAINER_bloomfilter_add (ctx->bloom, &mh); | ||
570 | return GNUNET_YES; | ||
571 | } | ||
572 | |||
573 | |||
574 | /** | ||
575 | * Task to send a find peer message for our own peer identifier | ||
576 | * so that we can find the closest peers in the network to ourselves | ||
577 | * and attempt to connect to them. | ||
578 | * | ||
579 | * @param cls closure for this task | ||
580 | * @param tc the context under which the task is running | ||
581 | */ | ||
582 | static void | ||
583 | send_find_peer_message (void *cls, | ||
584 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
585 | { | ||
586 | struct GNUNET_TIME_Relative next_send_time; | ||
587 | struct BloomConstructorContext bcc; | ||
588 | struct GNUNET_CONTAINER_BloomFilter *peer_bf; | ||
589 | |||
590 | find_peer_task = GNUNET_SCHEDULER_NO_TASK; | ||
591 | if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) | ||
592 | return; | ||
593 | if (newly_found_peers > bucket_size) | ||
594 | { | ||
595 | /* If we are finding many peers already, no need to send out our request right now! */ | ||
596 | find_peer_task = | ||
597 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, | ||
598 | &send_find_peer_message, NULL); | ||
599 | newly_found_peers = 0; | ||
600 | return; | ||
601 | } | ||
602 | bcc.bf_mutator = | ||
603 | GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX); | ||
604 | bcc.bloom = | ||
605 | GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, | ||
606 | GNUNET_CONSTANTS_BLOOMFILTER_K); | ||
607 | GNUNET_CONTAINER_multipeermap_iterate (all_known_peers, &add_known_to_bloom, | ||
608 | &bcc); | ||
609 | GNUNET_STATISTICS_update (GDS_stats, | ||
610 | gettext_noop ("# FIND PEER messages initiated"), 1, | ||
611 | GNUNET_NO); | ||
612 | peer_bf = | ||
613 | GNUNET_CONTAINER_bloomfilter_init (NULL, DHT_BLOOM_SIZE, | ||
614 | GNUNET_CONSTANTS_BLOOMFILTER_K); | ||
615 | // FIXME: pass priority!? | ||
616 | GDS_NEIGHBOURS_handle_get (GNUNET_BLOCK_TYPE_DHT_HELLO, | ||
617 | GNUNET_DHT_RO_FIND_PEER, | ||
618 | FIND_PEER_REPLICATION_LEVEL, 0, | ||
619 | &my_identity_hash, NULL, 0, bcc.bloom, | ||
620 | bcc.bf_mutator, peer_bf); | ||
621 | GNUNET_CONTAINER_bloomfilter_free (peer_bf); | ||
622 | GNUNET_CONTAINER_bloomfilter_free (bcc.bloom); | ||
623 | /* schedule next round */ | ||
624 | next_send_time.rel_value_us = | ||
625 | DHT_MINIMUM_FIND_PEER_INTERVAL.rel_value_us + | ||
626 | GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, | ||
627 | DHT_MAXIMUM_FIND_PEER_INTERVAL.rel_value_us / | ||
628 | (newly_found_peers + 1)); | ||
629 | newly_found_peers = 0; | ||
630 | find_peer_task = | ||
631 | GNUNET_SCHEDULER_add_delayed (next_send_time, &send_find_peer_message, | ||
632 | NULL); | ||
633 | } | ||
634 | |||
635 | |||
636 | /** | ||
637 | * Method called whenever a peer connects. | ||
638 | * | ||
639 | * @param cls closure | ||
640 | * @param peer peer identity this notification is about | ||
641 | */ | ||
642 | static void | ||
643 | handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer) | ||
644 | { | ||
645 | struct PeerInfo *ret; | ||
646 | struct GNUNET_HashCode phash; | ||
647 | int peer_bucket; | ||
648 | |||
649 | /* Check for connect to self message */ | ||
650 | if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity))) | ||
651 | return; | ||
652 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
653 | "Connected to %s\n", | ||
654 | GNUNET_i2s (peer)); | ||
655 | if (GNUNET_YES == | ||
656 | GNUNET_CONTAINER_multipeermap_contains (all_known_peers, | ||
657 | peer)) | ||
658 | { | ||
659 | GNUNET_break (0); | ||
660 | return; | ||
661 | } | ||
662 | GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# peers connected"), 1, | ||
663 | GNUNET_NO); | ||
664 | GNUNET_CRYPTO_hash (peer, | ||
665 | sizeof (struct GNUNET_PeerIdentity), | ||
666 | &phash); | ||
667 | peer_bucket = find_bucket (&phash); | ||
668 | GNUNET_assert ((peer_bucket >= 0) && (peer_bucket < MAX_BUCKETS)); | ||
669 | ret = GNUNET_malloc (sizeof (struct PeerInfo)); | ||
670 | #if 0 | ||
671 | ret->latency = latency; | ||
672 | ret->distance = distance; | ||
673 | #endif | ||
674 | ret->id = *peer; | ||
675 | GNUNET_CONTAINER_DLL_insert_tail (k_buckets[peer_bucket].head, | ||
676 | k_buckets[peer_bucket].tail, ret); | ||
677 | k_buckets[peer_bucket].peers_size++; | ||
678 | closest_bucket = GNUNET_MAX (closest_bucket, peer_bucket); | ||
679 | if ((peer_bucket > 0) && (k_buckets[peer_bucket].peers_size <= bucket_size)) | ||
680 | { | ||
681 | ret->preference_task = | ||
682 | GNUNET_SCHEDULER_add_now (&update_core_preference, ret); | ||
683 | newly_found_peers++; | ||
684 | } | ||
685 | GNUNET_assert (GNUNET_OK == | ||
686 | GNUNET_CONTAINER_multipeermap_put (all_known_peers, | ||
687 | peer, ret, | ||
688 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | ||
689 | if (1 == GNUNET_CONTAINER_multipeermap_size (all_known_peers) && | ||
690 | (GNUNET_YES != disable_try_connect)) | ||
691 | { | ||
692 | /* got a first connection, good time to start with FIND PEER requests... */ | ||
693 | find_peer_task = GNUNET_SCHEDULER_add_now (&send_find_peer_message, NULL); | ||
694 | } | ||
695 | } | ||
696 | |||
697 | |||
698 | /** | ||
699 | * Method called whenever a peer disconnects. | ||
700 | * | ||
701 | * @param cls closure | ||
702 | * @param peer peer identity this notification is about | ||
703 | */ | ||
704 | static void | ||
705 | handle_core_disconnect (void *cls, | ||
706 | const struct GNUNET_PeerIdentity *peer) | ||
707 | { | ||
708 | struct PeerInfo *to_remove; | ||
709 | int current_bucket; | ||
710 | struct P2PPendingMessage *pos; | ||
711 | unsigned int discarded; | ||
712 | struct GNUNET_HashCode phash; | ||
713 | |||
714 | /* Check for disconnect from self message */ | ||
715 | if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity))) | ||
716 | return; | ||
717 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
718 | "Disconnected %s\n", | ||
719 | GNUNET_i2s (peer)); | ||
720 | to_remove = | ||
721 | GNUNET_CONTAINER_multipeermap_get (all_known_peers, peer); | ||
722 | if (NULL == to_remove) | ||
723 | { | ||
724 | GNUNET_break (0); | ||
725 | return; | ||
726 | } | ||
727 | GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# peers connected"), -1, | ||
728 | GNUNET_NO); | ||
729 | GNUNET_assert (GNUNET_YES == | ||
730 | GNUNET_CONTAINER_multipeermap_remove (all_known_peers, | ||
731 | peer, | ||
732 | to_remove)); | ||
733 | if (GNUNET_SCHEDULER_NO_TASK != to_remove->preference_task) | ||
734 | { | ||
735 | GNUNET_SCHEDULER_cancel (to_remove->preference_task); | ||
736 | to_remove->preference_task = GNUNET_SCHEDULER_NO_TASK; | ||
737 | } | ||
738 | GNUNET_CRYPTO_hash (peer, | ||
739 | sizeof (struct GNUNET_PeerIdentity), | ||
740 | &phash); | ||
741 | current_bucket = find_bucket (&phash); | ||
742 | GNUNET_assert (current_bucket >= 0); | ||
743 | GNUNET_CONTAINER_DLL_remove (k_buckets[current_bucket].head, | ||
744 | k_buckets[current_bucket].tail, to_remove); | ||
745 | GNUNET_assert (k_buckets[current_bucket].peers_size > 0); | ||
746 | k_buckets[current_bucket].peers_size--; | ||
747 | while ((closest_bucket > 0) && (k_buckets[closest_bucket].peers_size == 0)) | ||
748 | closest_bucket--; | ||
749 | |||
750 | if (to_remove->th != NULL) | ||
751 | { | ||
752 | GNUNET_CORE_notify_transmit_ready_cancel (to_remove->th); | ||
753 | to_remove->th = NULL; | ||
754 | } | ||
755 | discarded = 0; | ||
756 | while (NULL != (pos = to_remove->head)) | ||
757 | { | ||
758 | GNUNET_CONTAINER_DLL_remove (to_remove->head, to_remove->tail, pos); | ||
759 | discarded++; | ||
760 | GNUNET_free (pos); | ||
761 | } | ||
762 | GNUNET_STATISTICS_update (GDS_stats, | ||
763 | gettext_noop | ||
764 | ("# Queued messages discarded (peer disconnected)"), | ||
765 | discarded, GNUNET_NO); | ||
766 | GNUNET_free (to_remove); | ||
767 | } | ||
768 | |||
769 | |||
770 | /** | ||
771 | * Called when core is ready to send a message we asked for | ||
772 | * out to the destination. | ||
773 | * | ||
774 | * @param cls the 'struct PeerInfo' of the target peer | ||
775 | * @param size number of bytes available in buf | ||
776 | * @param buf where the callee should write the message | ||
777 | * @return number of bytes written to buf | ||
778 | */ | ||
779 | static size_t | ||
780 | core_transmit_notify (void *cls, size_t size, void *buf) | ||
781 | { | ||
782 | struct PeerInfo *peer = cls; | ||
783 | char *cbuf = buf; | ||
784 | struct P2PPendingMessage *pending; | ||
785 | size_t off; | ||
786 | size_t msize; | ||
787 | |||
788 | peer->th = NULL; | ||
789 | while ((NULL != (pending = peer->head)) && | ||
790 | (0 == GNUNET_TIME_absolute_get_remaining (pending->timeout).rel_value_us)) | ||
791 | { | ||
792 | peer->pending_count--; | ||
793 | GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending); | ||
794 | GNUNET_free (pending); | ||
795 | } | ||
796 | if (pending == NULL) | ||
797 | { | ||
798 | /* no messages pending */ | ||
799 | return 0; | ||
800 | } | ||
801 | if (buf == NULL) | ||
802 | { | ||
803 | peer->th = | ||
804 | GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO, | ||
805 | pending->importance, | ||
806 | GNUNET_TIME_absolute_get_remaining | ||
807 | (pending->timeout), &peer->id, | ||
808 | ntohs (pending->msg->size), | ||
809 | &core_transmit_notify, peer); | ||
810 | GNUNET_break (NULL != peer->th); | ||
811 | return 0; | ||
812 | } | ||
813 | off = 0; | ||
814 | while ((NULL != (pending = peer->head)) && | ||
815 | (size - off >= (msize = ntohs (pending->msg->size)))) | ||
816 | { | ||
817 | GNUNET_STATISTICS_update (GDS_stats, | ||
818 | gettext_noop | ||
819 | ("# Bytes transmitted to other peers"), msize, | ||
820 | GNUNET_NO); | ||
821 | memcpy (&cbuf[off], pending->msg, msize); | ||
822 | off += msize; | ||
823 | peer->pending_count--; | ||
824 | GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending); | ||
825 | GNUNET_free (pending); | ||
826 | } | ||
827 | if (peer->head != NULL) | ||
828 | { | ||
829 | peer->th = | ||
830 | GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO, | ||
831 | pending->importance, | ||
832 | GNUNET_TIME_absolute_get_remaining | ||
833 | (pending->timeout), &peer->id, msize, | ||
834 | &core_transmit_notify, peer); | ||
835 | GNUNET_break (NULL != peer->th); | ||
836 | } | ||
837 | return off; | ||
838 | } | ||
839 | |||
840 | |||
841 | /** | ||
842 | * Transmit all messages in the peer's message queue. | ||
843 | * | ||
844 | * @param peer message queue to process | ||
845 | */ | ||
846 | static void | ||
847 | process_peer_queue (struct PeerInfo *peer) | ||
848 | { | ||
849 | struct P2PPendingMessage *pending; | ||
850 | |||
851 | if (NULL == (pending = peer->head)) | ||
852 | return; | ||
853 | if (NULL != peer->th) | ||
854 | return; | ||
855 | GNUNET_STATISTICS_update (GDS_stats, | ||
856 | gettext_noop | ||
857 | ("# Bytes of bandwidth requested from core"), | ||
858 | ntohs (pending->msg->size), GNUNET_NO); | ||
859 | peer->th = | ||
860 | GNUNET_CORE_notify_transmit_ready (core_api, GNUNET_NO, | ||
861 | pending->importance, | ||
862 | GNUNET_TIME_absolute_get_remaining | ||
863 | (pending->timeout), &peer->id, | ||
864 | ntohs (pending->msg->size), | ||
865 | &core_transmit_notify, peer); | ||
866 | GNUNET_break (NULL != peer->th); | ||
867 | } | ||
868 | |||
869 | |||
870 | /** | ||
871 | * To how many peers should we (on average) forward the request to | ||
872 | * obtain the desired target_replication count (on average). | ||
873 | * | ||
874 | * @param hop_count number of hops the message has traversed | ||
875 | * @param target_replication the number of total paths desired | ||
876 | * @return Some number of peers to forward the message to | ||
877 | */ | ||
878 | static unsigned int | ||
879 | get_forward_count (uint32_t hop_count, uint32_t target_replication) | ||
880 | { | ||
881 | uint32_t random_value; | ||
882 | uint32_t forward_count; | ||
883 | float target_value; | ||
884 | |||
885 | if (hop_count > GDS_NSE_get () * 4.0) | ||
886 | { | ||
887 | /* forcefully terminate */ | ||
888 | GNUNET_STATISTICS_update (GDS_stats, | ||
889 | gettext_noop ("# requests TTL-dropped"), | ||
890 | 1, GNUNET_NO); | ||
891 | return 0; | ||
892 | } | ||
893 | if (hop_count > GDS_NSE_get () * 2.0) | ||
894 | { | ||
895 | /* Once we have reached our ideal number of hops, only forward to 1 peer */ | ||
896 | return 1; | ||
897 | } | ||
898 | /* bound by system-wide maximum */ | ||
899 | target_replication = | ||
900 | GNUNET_MIN (MAXIMUM_REPLICATION_LEVEL, target_replication); | ||
901 | target_value = | ||
902 | 1 + (target_replication - 1.0) / (GDS_NSE_get () + | ||
903 | ((float) (target_replication - 1.0) * | ||
904 | hop_count)); | ||
905 | /* Set forward count to floor of target_value */ | ||
906 | forward_count = (uint32_t) target_value; | ||
907 | /* Subtract forward_count (floor) from target_value (yields value between 0 and 1) */ | ||
908 | target_value = target_value - forward_count; | ||
909 | random_value = | ||
910 | GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX); | ||
911 | if (random_value < (target_value * UINT32_MAX)) | ||
912 | forward_count++; | ||
913 | return forward_count; | ||
914 | } | ||
915 | |||
916 | |||
917 | /** | ||
918 | * Compute the distance between have and target as a 32-bit value. | ||
919 | * Differences in the lower bits must count stronger than differences | ||
920 | * in the higher bits. | ||
921 | * | ||
922 | * @param target | ||
923 | * @param have | ||
924 | * @return 0 if have==target, otherwise a number | ||
925 | * that is larger as the distance between | ||
926 | * the two hash codes increases | ||
927 | */ | ||
928 | static unsigned int | ||
929 | get_distance (const struct GNUNET_HashCode *target, | ||
930 | const struct GNUNET_HashCode *have) | ||
931 | { | ||
932 | unsigned int bucket; | ||
933 | unsigned int msb; | ||
934 | unsigned int lsb; | ||
935 | unsigned int i; | ||
936 | |||
937 | /* We have to represent the distance between two 2^9 (=512)-bit | ||
938 | * numbers as a 2^5 (=32)-bit number with "0" being used for the | ||
939 | * two numbers being identical; furthermore, we need to | ||
940 | * guarantee that a difference in the number of matching | ||
941 | * bits is always represented in the result. | ||
942 | * | ||
943 | * We use 2^32/2^9 numerical values to distinguish between | ||
944 | * hash codes that have the same LSB bit distance and | ||
945 | * use the highest 2^9 bits of the result to signify the | ||
946 | * number of (mis)matching LSB bits; if we have 0 matching | ||
947 | * and hence 512 mismatching LSB bits we return -1 (since | ||
948 | * 512 itself cannot be represented with 9 bits) */ | ||
949 | |||
950 | /* first, calculate the most significant 9 bits of our | ||
951 | * result, aka the number of LSBs */ | ||
952 | bucket = GNUNET_CRYPTO_hash_matching_bits (target, have); | ||
953 | /* bucket is now a value between 0 and 512 */ | ||
954 | if (bucket == 512) | ||
955 | return 0; /* perfect match */ | ||
956 | if (bucket == 0) | ||
957 | return (unsigned int) -1; /* LSB differs; use max (if we did the bit-shifting | ||
958 | * below, we'd end up with max+1 (overflow)) */ | ||
959 | |||
960 | /* calculate the most significant bits of the final result */ | ||
961 | msb = (512 - bucket) << (32 - 9); | ||
962 | /* calculate the 32-9 least significant bits of the final result by | ||
963 | * looking at the differences in the 32-9 bits following the | ||
964 | * mismatching bit at 'bucket' */ | ||
965 | lsb = 0; | ||
966 | for (i = bucket + 1; | ||
967 | (i < sizeof (struct GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++) | ||
968 | { | ||
969 | if (GNUNET_CRYPTO_hash_get_bit (target, i) != | ||
970 | GNUNET_CRYPTO_hash_get_bit (have, i)) | ||
971 | lsb |= (1 << (bucket + 32 - 9 - i)); /* first bit set will be 10, | ||
972 | * last bit set will be 31 -- if | ||
973 | * i does not reach 512 first... */ | ||
974 | } | ||
975 | return msb | lsb; | ||
976 | } | ||
977 | |||
978 | |||
979 | /** | ||
980 | * Check whether my identity is closer than any known peers. If a | ||
981 | * non-null bloomfilter is given, check if this is the closest peer | ||
982 | * that hasn't already been routed to. | ||
983 | * | ||
984 | * @param key hash code to check closeness to | ||
985 | * @param bloom bloomfilter, exclude these entries from the decision | ||
986 | * @return GNUNET_YES if node location is closest, | ||
987 | * GNUNET_NO otherwise. | ||
988 | */ | ||
989 | static int | ||
990 | am_closest_peer (const struct GNUNET_HashCode *key, | ||
991 | const struct GNUNET_CONTAINER_BloomFilter *bloom) | ||
992 | { | ||
993 | int bits; | ||
994 | int other_bits; | ||
995 | int bucket_num; | ||
996 | int count; | ||
997 | struct PeerInfo *pos; | ||
998 | struct GNUNET_HashCode phash; | ||
999 | |||
1000 | if (0 == memcmp (&my_identity_hash, key, sizeof (struct GNUNET_HashCode))) | ||
1001 | return GNUNET_YES; | ||
1002 | bucket_num = find_bucket (key); | ||
1003 | GNUNET_assert (bucket_num >= 0); | ||
1004 | bits = GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, key); | ||
1005 | pos = k_buckets[bucket_num].head; | ||
1006 | count = 0; | ||
1007 | while ((pos != NULL) && (count < bucket_size)) | ||
1008 | { | ||
1009 | GNUNET_CRYPTO_hash (&pos->id, | ||
1010 | sizeof (struct GNUNET_PeerIdentity), | ||
1011 | &phash); | ||
1012 | if ((bloom != NULL) && | ||
1013 | (GNUNET_YES == | ||
1014 | GNUNET_CONTAINER_bloomfilter_test (bloom, &phash))) | ||
1015 | { | ||
1016 | pos = pos->next; | ||
1017 | continue; /* Skip already checked entries */ | ||
1018 | } | ||
1019 | other_bits = GNUNET_CRYPTO_hash_matching_bits (&phash, key); | ||
1020 | if (other_bits > bits) | ||
1021 | return GNUNET_NO; | ||
1022 | if (other_bits == bits) /* We match the same number of bits */ | ||
1023 | return GNUNET_YES; | ||
1024 | pos = pos->next; | ||
1025 | } | ||
1026 | /* No peers closer, we are the closest! */ | ||
1027 | return GNUNET_YES; | ||
1028 | } | ||
1029 | |||
1030 | |||
1031 | /** | ||
1032 | * Select a peer from the routing table that would be a good routing | ||
1033 | * destination for sending a message for "key". The resulting peer | ||
1034 | * must not be in the set of blocked peers.<p> | ||
1035 | * | ||
1036 | * Note that we should not ALWAYS select the closest peer to the | ||
1037 | * target, peers further away from the target should be chosen with | ||
1038 | * exponentially declining probability. | ||
1039 | * | ||
1040 | * FIXME: double-check that this is fine | ||
1041 | * | ||
1042 | * | ||
1043 | * @param key the key we are selecting a peer to route to | ||
1044 | * @param bloom a bloomfilter containing entries this request has seen already | ||
1045 | * @param hops how many hops has this message traversed thus far | ||
1046 | * @return Peer to route to, or NULL on error | ||
1047 | */ | ||
1048 | static struct PeerInfo * | ||
1049 | select_peer (const struct GNUNET_HashCode * key, | ||
1050 | const struct GNUNET_CONTAINER_BloomFilter *bloom, uint32_t hops) | ||
1051 | { | ||
1052 | unsigned int bc; | ||
1053 | unsigned int count; | ||
1054 | unsigned int selected; | ||
1055 | struct PeerInfo *pos; | ||
1056 | unsigned int dist; | ||
1057 | unsigned int smallest_distance; | ||
1058 | struct PeerInfo *chosen; | ||
1059 | struct GNUNET_HashCode phash; | ||
1060 | |||
1061 | if (hops >= GDS_NSE_get ()) | ||
1062 | { | ||
1063 | /* greedy selection (closest peer that is not in bloomfilter) */ | ||
1064 | smallest_distance = UINT_MAX; | ||
1065 | chosen = NULL; | ||
1066 | for (bc = 0; bc <= closest_bucket; bc++) | ||
1067 | { | ||
1068 | pos = k_buckets[bc].head; | ||
1069 | count = 0; | ||
1070 | while ((pos != NULL) && (count < bucket_size)) | ||
1071 | { | ||
1072 | GNUNET_CRYPTO_hash (&pos->id, | ||
1073 | sizeof (struct GNUNET_PeerIdentity), | ||
1074 | &phash); | ||
1075 | if ((bloom == NULL) || | ||
1076 | (GNUNET_NO == | ||
1077 | GNUNET_CONTAINER_bloomfilter_test (bloom, &phash))) | ||
1078 | { | ||
1079 | dist = get_distance (key, &phash); | ||
1080 | if (dist < smallest_distance) | ||
1081 | { | ||
1082 | chosen = pos; | ||
1083 | smallest_distance = dist; | ||
1084 | } | ||
1085 | } | ||
1086 | else | ||
1087 | { | ||
1088 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1089 | "Excluded peer `%s' due to BF match in greedy routing for %s\n", | ||
1090 | GNUNET_i2s (&pos->id), GNUNET_h2s (key)); | ||
1091 | GNUNET_STATISTICS_update (GDS_stats, | ||
1092 | gettext_noop | ||
1093 | ("# Peers excluded from routing due to Bloomfilter"), | ||
1094 | 1, GNUNET_NO); | ||
1095 | dist = get_distance (key, &phash); | ||
1096 | if (dist < smallest_distance) | ||
1097 | { | ||
1098 | chosen = NULL; | ||
1099 | smallest_distance = dist; | ||
1100 | } | ||
1101 | } | ||
1102 | count++; | ||
1103 | pos = pos->next; | ||
1104 | } | ||
1105 | } | ||
1106 | if (NULL == chosen) | ||
1107 | GNUNET_STATISTICS_update (GDS_stats, | ||
1108 | gettext_noop ("# Peer selection failed"), 1, | ||
1109 | GNUNET_NO); | ||
1110 | return chosen; | ||
1111 | } | ||
1112 | |||
1113 | /* select "random" peer */ | ||
1114 | /* count number of peers that are available and not filtered */ | ||
1115 | count = 0; | ||
1116 | for (bc = 0; bc <= closest_bucket; bc++) | ||
1117 | { | ||
1118 | pos = k_buckets[bc].head; | ||
1119 | while ((pos != NULL) && (count < bucket_size)) | ||
1120 | { | ||
1121 | GNUNET_CRYPTO_hash (&pos->id, | ||
1122 | sizeof (struct GNUNET_PeerIdentity), | ||
1123 | &phash); | ||
1124 | if ((bloom != NULL) && | ||
1125 | (GNUNET_YES == | ||
1126 | GNUNET_CONTAINER_bloomfilter_test (bloom, &phash))) | ||
1127 | { | ||
1128 | GNUNET_STATISTICS_update (GDS_stats, | ||
1129 | gettext_noop | ||
1130 | ("# Peers excluded from routing due to Bloomfilter"), | ||
1131 | 1, GNUNET_NO); | ||
1132 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1133 | "Excluded peer `%s' due to BF match in random routing for %s\n", | ||
1134 | GNUNET_i2s (&pos->id), GNUNET_h2s (key)); | ||
1135 | pos = pos->next; | ||
1136 | continue; /* Ignore bloomfiltered peers */ | ||
1137 | } | ||
1138 | count++; | ||
1139 | pos = pos->next; | ||
1140 | } | ||
1141 | } | ||
1142 | if (0 == count) /* No peers to select from! */ | ||
1143 | { | ||
1144 | GNUNET_STATISTICS_update (GDS_stats, | ||
1145 | gettext_noop ("# Peer selection failed"), 1, | ||
1146 | GNUNET_NO); | ||
1147 | return NULL; | ||
1148 | } | ||
1149 | /* Now actually choose a peer */ | ||
1150 | selected = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count); | ||
1151 | count = 0; | ||
1152 | for (bc = 0; bc <= closest_bucket; bc++) | ||
1153 | { | ||
1154 | for (pos = k_buckets[bc].head; ((pos != NULL) && (count < bucket_size)); pos = pos->next) | ||
1155 | { | ||
1156 | GNUNET_CRYPTO_hash (&pos->id, | ||
1157 | sizeof (struct GNUNET_PeerIdentity), | ||
1158 | &phash); | ||
1159 | if ((bloom != NULL) && | ||
1160 | (GNUNET_YES == | ||
1161 | GNUNET_CONTAINER_bloomfilter_test (bloom, &phash))) | ||
1162 | { | ||
1163 | continue; /* Ignore bloomfiltered peers */ | ||
1164 | } | ||
1165 | if (0 == selected--) | ||
1166 | return pos; | ||
1167 | } | ||
1168 | } | ||
1169 | GNUNET_break (0); | ||
1170 | return NULL; | ||
1171 | } | ||
1172 | |||
1173 | |||
1174 | /** | ||
1175 | * Compute the set of peers that the given request should be | ||
1176 | * forwarded to. | ||
1177 | * | ||
1178 | * @param key routing key | ||
1179 | * @param bloom bloom filter excluding peers as targets, all selected | ||
1180 | * peers will be added to the bloom filter | ||
1181 | * @param hop_count number of hops the request has traversed so far | ||
1182 | * @param target_replication desired number of replicas | ||
1183 | * @param targets where to store an array of target peers (to be | ||
1184 | * free'd by the caller) | ||
1185 | * @return number of peers returned in 'targets'. | ||
1186 | */ | ||
1187 | static unsigned int | ||
1188 | get_target_peers (const struct GNUNET_HashCode *key, | ||
1189 | struct GNUNET_CONTAINER_BloomFilter *bloom, | ||
1190 | uint32_t hop_count, uint32_t target_replication, | ||
1191 | struct PeerInfo ***targets) | ||
1192 | { | ||
1193 | unsigned int ret; | ||
1194 | unsigned int off; | ||
1195 | struct PeerInfo **rtargets; | ||
1196 | struct PeerInfo *nxt; | ||
1197 | struct GNUNET_HashCode nhash; | ||
1198 | |||
1199 | GNUNET_assert (NULL != bloom); | ||
1200 | ret = get_forward_count (hop_count, target_replication); | ||
1201 | if (0 == ret) | ||
1202 | { | ||
1203 | *targets = NULL; | ||
1204 | return 0; | ||
1205 | } | ||
1206 | rtargets = GNUNET_malloc (sizeof (struct PeerInfo *) * ret); | ||
1207 | for (off = 0; off < ret; off++) | ||
1208 | { | ||
1209 | nxt = select_peer (key, bloom, hop_count); | ||
1210 | if (NULL == nxt) | ||
1211 | break; | ||
1212 | rtargets[off] = nxt; | ||
1213 | GNUNET_CRYPTO_hash (&nxt->id, | ||
1214 | sizeof (struct GNUNET_PeerIdentity), | ||
1215 | &nhash); | ||
1216 | GNUNET_break (GNUNET_NO == | ||
1217 | GNUNET_CONTAINER_bloomfilter_test (bloom, | ||
1218 | &nhash)); | ||
1219 | GNUNET_CONTAINER_bloomfilter_add (bloom, &nhash); | ||
1220 | } | ||
1221 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1222 | "Selected %u/%u peers at hop %u for %s (target was %u)\n", off, | ||
1223 | GNUNET_CONTAINER_multipeermap_size (all_known_peers), | ||
1224 | (unsigned int) hop_count, GNUNET_h2s (key), ret); | ||
1225 | if (0 == off) | ||
1226 | { | ||
1227 | GNUNET_free (rtargets); | ||
1228 | *targets = NULL; | ||
1229 | return 0; | ||
1230 | } | ||
1231 | *targets = rtargets; | ||
1232 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1233 | "Forwarding query `%s' to %u peers (goal was %u peers)\n", | ||
1234 | GNUNET_h2s (key), | ||
1235 | off, | ||
1236 | ret); | ||
1237 | return off; | ||
1238 | } | ||
1239 | |||
1240 | |||
1241 | /** | ||
1242 | * Perform a PUT operation. Forwards the given request to other | ||
1243 | * peers. Does not store the data locally. Does not give the | ||
1244 | * data to local clients. May do nothing if this is the only | ||
1245 | * peer in the network (or if we are the closest peer in the | ||
1246 | * network). | ||
1247 | * | ||
1248 | * @param type type of the block | ||
1249 | * @param options routing options | ||
1250 | * @param desired_replication_level desired replication count | ||
1251 | * @param expiration_time when does the content expire | ||
1252 | * @param hop_count how many hops has this message traversed so far | ||
1253 | * @param bf Bloom filter of peers this PUT has already traversed | ||
1254 | * @param key key for the content | ||
1255 | * @param put_path_length number of entries in @a put_path | ||
1256 | * @param put_path peers this request has traversed so far (if tracked) | ||
1257 | * @param data payload to store | ||
1258 | * @param data_size number of bytes in @a data | ||
1259 | */ | ||
1260 | void | ||
1261 | GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type, | ||
1262 | enum GNUNET_DHT_RouteOption options, | ||
1263 | uint32_t desired_replication_level, | ||
1264 | struct GNUNET_TIME_Absolute expiration_time, | ||
1265 | uint32_t hop_count, | ||
1266 | struct GNUNET_CONTAINER_BloomFilter *bf, | ||
1267 | const struct GNUNET_HashCode *key, | ||
1268 | unsigned int put_path_length, | ||
1269 | struct GNUNET_PeerIdentity *put_path, | ||
1270 | const void *data, size_t data_size) | ||
1271 | { | ||
1272 | unsigned int target_count; | ||
1273 | unsigned int i; | ||
1274 | struct PeerInfo **targets; | ||
1275 | struct PeerInfo *target; | ||
1276 | struct P2PPendingMessage *pending; | ||
1277 | size_t msize; | ||
1278 | struct PeerPutMessage *ppm; | ||
1279 | struct GNUNET_PeerIdentity *pp; | ||
1280 | struct GNUNET_HashCode thash; | ||
1281 | |||
1282 | GNUNET_assert (NULL != bf); | ||
1283 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1284 | "Adding myself (%s) to PUT bloomfilter for %s\n", | ||
1285 | GNUNET_i2s (&my_identity), GNUNET_h2s (key)); | ||
1286 | GNUNET_CONTAINER_bloomfilter_add (bf, &my_identity_hash); | ||
1287 | GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# PUT requests routed"), | ||
1288 | 1, GNUNET_NO); | ||
1289 | target_count = | ||
1290 | get_target_peers (key, bf, hop_count, desired_replication_level, | ||
1291 | &targets); | ||
1292 | if (0 == target_count) | ||
1293 | { | ||
1294 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1295 | "Routing PUT for %s terminates after %u hops at %s\n", | ||
1296 | GNUNET_h2s (key), (unsigned int) hop_count, | ||
1297 | GNUNET_i2s (&my_identity)); | ||
1298 | return; | ||
1299 | } | ||
1300 | msize = | ||
1301 | put_path_length * sizeof (struct GNUNET_PeerIdentity) + data_size + | ||
1302 | sizeof (struct PeerPutMessage); | ||
1303 | if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) | ||
1304 | { | ||
1305 | put_path_length = 0; | ||
1306 | msize = data_size + sizeof (struct PeerPutMessage); | ||
1307 | } | ||
1308 | if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) | ||
1309 | { | ||
1310 | GNUNET_break (0); | ||
1311 | GNUNET_free (targets); | ||
1312 | return; | ||
1313 | } | ||
1314 | GNUNET_STATISTICS_update (GDS_stats, | ||
1315 | gettext_noop | ||
1316 | ("# PUT messages queued for transmission"), | ||
1317 | target_count, GNUNET_NO); | ||
1318 | for (i = 0; i < target_count; i++) | ||
1319 | { | ||
1320 | target = targets[i]; | ||
1321 | if (target->pending_count >= MAXIMUM_PENDING_PER_PEER) | ||
1322 | { | ||
1323 | GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"), | ||
1324 | 1, GNUNET_NO); | ||
1325 | continue; /* skip */ | ||
1326 | } | ||
1327 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1328 | "Routing PUT for %s after %u hops to %s\n", GNUNET_h2s (key), | ||
1329 | (unsigned int) hop_count, GNUNET_i2s (&target->id)); | ||
1330 | pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize); | ||
1331 | pending->importance = 0; /* FIXME */ | ||
1332 | pending->timeout = expiration_time; | ||
1333 | ppm = (struct PeerPutMessage *) &pending[1]; | ||
1334 | pending->msg = &ppm->header; | ||
1335 | ppm->header.size = htons (msize); | ||
1336 | ppm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_PUT); | ||
1337 | ppm->options = htonl (options); | ||
1338 | ppm->type = htonl (type); | ||
1339 | ppm->hop_count = htonl (hop_count + 1); | ||
1340 | ppm->desired_replication_level = htonl (desired_replication_level); | ||
1341 | ppm->put_path_length = htonl (put_path_length); | ||
1342 | ppm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time); | ||
1343 | GNUNET_CRYPTO_hash (&target->id, | ||
1344 | sizeof (struct GNUNET_PeerIdentity), | ||
1345 | &thash); | ||
1346 | GNUNET_break (GNUNET_YES == | ||
1347 | GNUNET_CONTAINER_bloomfilter_test (bf, | ||
1348 | &thash)); | ||
1349 | GNUNET_assert (GNUNET_OK == | ||
1350 | GNUNET_CONTAINER_bloomfilter_get_raw_data (bf, | ||
1351 | ppm->bloomfilter, | ||
1352 | DHT_BLOOM_SIZE)); | ||
1353 | ppm->key = *key; | ||
1354 | pp = (struct GNUNET_PeerIdentity *) &ppm[1]; | ||
1355 | memcpy (pp, put_path, | ||
1356 | sizeof (struct GNUNET_PeerIdentity) * put_path_length); | ||
1357 | memcpy (&pp[put_path_length], data, data_size); | ||
1358 | GNUNET_CONTAINER_DLL_insert_tail (target->head, target->tail, pending); | ||
1359 | target->pending_count++; | ||
1360 | process_peer_queue (target); | ||
1361 | } | ||
1362 | GNUNET_free (targets); | ||
1363 | } | ||
1364 | |||
1365 | |||
1366 | /** | ||
1367 | * Perform a GET operation. Forwards the given request to other | ||
1368 | * peers. Does not lookup the key locally. May do nothing if this is | ||
1369 | * the only peer in the network (or if we are the closest peer in the | ||
1370 | * network). | ||
1371 | * | ||
1372 | * @param type type of the block | ||
1373 | * @param options routing options | ||
1374 | * @param desired_replication_level desired replication count | ||
1375 | * @param hop_count how many hops did this request traverse so far? | ||
1376 | * @param key key for the content | ||
1377 | * @param xquery extended query | ||
1378 | * @param xquery_size number of bytes in @a xquery | ||
1379 | * @param reply_bf bloomfilter to filter duplicates | ||
1380 | * @param reply_bf_mutator mutator for @a reply_bf | ||
1381 | * @param peer_bf filter for peers not to select (again) | ||
1382 | */ | ||
1383 | void | ||
1384 | GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type, | ||
1385 | enum GNUNET_DHT_RouteOption options, | ||
1386 | uint32_t desired_replication_level, | ||
1387 | uint32_t hop_count, const struct GNUNET_HashCode * key, | ||
1388 | const void *xquery, size_t xquery_size, | ||
1389 | const struct GNUNET_CONTAINER_BloomFilter *reply_bf, | ||
1390 | uint32_t reply_bf_mutator, | ||
1391 | struct GNUNET_CONTAINER_BloomFilter *peer_bf) | ||
1392 | { | ||
1393 | unsigned int target_count; | ||
1394 | unsigned int i; | ||
1395 | struct PeerInfo **targets; | ||
1396 | struct PeerInfo *target; | ||
1397 | struct P2PPendingMessage *pending; | ||
1398 | size_t msize; | ||
1399 | struct PeerGetMessage *pgm; | ||
1400 | char *xq; | ||
1401 | size_t reply_bf_size; | ||
1402 | struct GNUNET_HashCode thash; | ||
1403 | |||
1404 | GNUNET_assert (NULL != peer_bf); | ||
1405 | GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# GET requests routed"), | ||
1406 | 1, GNUNET_NO); | ||
1407 | target_count = | ||
1408 | get_target_peers (key, peer_bf, hop_count, desired_replication_level, | ||
1409 | &targets); | ||
1410 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1411 | "Adding myself (%s) to GET bloomfilter for %s\n", | ||
1412 | GNUNET_i2s (&my_identity), GNUNET_h2s (key)); | ||
1413 | GNUNET_CONTAINER_bloomfilter_add (peer_bf, &my_identity_hash); | ||
1414 | if (0 == target_count) | ||
1415 | { | ||
1416 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1417 | "Routing GET for %s terminates after %u hops at %s\n", | ||
1418 | GNUNET_h2s (key), (unsigned int) hop_count, | ||
1419 | GNUNET_i2s (&my_identity)); | ||
1420 | return; | ||
1421 | } | ||
1422 | reply_bf_size = GNUNET_CONTAINER_bloomfilter_get_size (reply_bf); | ||
1423 | msize = xquery_size + sizeof (struct PeerGetMessage) + reply_bf_size; | ||
1424 | if (msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) | ||
1425 | { | ||
1426 | GNUNET_break (0); | ||
1427 | GNUNET_free (targets); | ||
1428 | return; | ||
1429 | } | ||
1430 | GNUNET_STATISTICS_update (GDS_stats, | ||
1431 | gettext_noop | ||
1432 | ("# GET messages queued for transmission"), | ||
1433 | target_count, GNUNET_NO); | ||
1434 | /* forward request */ | ||
1435 | for (i = 0; i < target_count; i++) | ||
1436 | { | ||
1437 | target = targets[i]; | ||
1438 | if (target->pending_count >= MAXIMUM_PENDING_PER_PEER) | ||
1439 | { | ||
1440 | GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"), | ||
1441 | 1, GNUNET_NO); | ||
1442 | continue; /* skip */ | ||
1443 | } | ||
1444 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1445 | "Routing GET for %s after %u hops to %s\n", GNUNET_h2s (key), | ||
1446 | (unsigned int) hop_count, GNUNET_i2s (&target->id)); | ||
1447 | pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize); | ||
1448 | pending->importance = 0; /* FIXME */ | ||
1449 | pending->timeout = GNUNET_TIME_relative_to_absolute (GET_TIMEOUT); | ||
1450 | pgm = (struct PeerGetMessage *) &pending[1]; | ||
1451 | pending->msg = &pgm->header; | ||
1452 | pgm->header.size = htons (msize); | ||
1453 | pgm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_GET); | ||
1454 | pgm->options = htonl (options); | ||
1455 | pgm->type = htonl (type); | ||
1456 | pgm->hop_count = htonl (hop_count + 1); | ||
1457 | pgm->desired_replication_level = htonl (desired_replication_level); | ||
1458 | pgm->xquery_size = htonl (xquery_size); | ||
1459 | pgm->bf_mutator = reply_bf_mutator; | ||
1460 | GNUNET_CRYPTO_hash (&target->id, | ||
1461 | sizeof (struct GNUNET_PeerIdentity), | ||
1462 | &thash); | ||
1463 | GNUNET_break (GNUNET_YES == | ||
1464 | GNUNET_CONTAINER_bloomfilter_test (peer_bf, | ||
1465 | &thash)); | ||
1466 | GNUNET_assert (GNUNET_OK == | ||
1467 | GNUNET_CONTAINER_bloomfilter_get_raw_data (peer_bf, | ||
1468 | pgm->bloomfilter, | ||
1469 | DHT_BLOOM_SIZE)); | ||
1470 | pgm->key = *key; | ||
1471 | xq = (char *) &pgm[1]; | ||
1472 | memcpy (xq, xquery, xquery_size); | ||
1473 | if (NULL != reply_bf) | ||
1474 | GNUNET_assert (GNUNET_OK == | ||
1475 | GNUNET_CONTAINER_bloomfilter_get_raw_data (reply_bf, | ||
1476 | &xq | ||
1477 | [xquery_size], | ||
1478 | reply_bf_size)); | ||
1479 | GNUNET_CONTAINER_DLL_insert_tail (target->head, target->tail, pending); | ||
1480 | target->pending_count++; | ||
1481 | process_peer_queue (target); | ||
1482 | } | ||
1483 | GNUNET_free (targets); | ||
1484 | } | ||
1485 | |||
1486 | |||
1487 | /** | ||
1488 | * Handle a reply (route to origin). Only forwards the reply back to | ||
1489 | * the given peer. Does not do local caching or forwarding to local | ||
1490 | * clients. | ||
1491 | * | ||
1492 | * @param target neighbour that should receive the block (if still connected) | ||
1493 | * @param type type of the block | ||
1494 | * @param expiration_time when does the content expire | ||
1495 | * @param key key for the content | ||
1496 | * @param put_path_length number of entries in @a put_path | ||
1497 | * @param put_path peers the original PUT traversed (if tracked) | ||
1498 | * @param get_path_length number of entries in @a get_path | ||
1499 | * @param get_path peers this reply has traversed so far (if tracked) | ||
1500 | * @param data payload of the reply | ||
1501 | * @param data_size number of bytes in @a data | ||
1502 | */ | ||
1503 | void | ||
1504 | GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target, | ||
1505 | enum GNUNET_BLOCK_Type type, | ||
1506 | struct GNUNET_TIME_Absolute expiration_time, | ||
1507 | const struct GNUNET_HashCode * key, | ||
1508 | unsigned int put_path_length, | ||
1509 | const struct GNUNET_PeerIdentity *put_path, | ||
1510 | unsigned int get_path_length, | ||
1511 | const struct GNUNET_PeerIdentity *get_path, | ||
1512 | const void *data, size_t data_size) | ||
1513 | { | ||
1514 | struct PeerInfo *pi; | ||
1515 | struct P2PPendingMessage *pending; | ||
1516 | size_t msize; | ||
1517 | struct PeerResultMessage *prm; | ||
1518 | struct GNUNET_PeerIdentity *paths; | ||
1519 | |||
1520 | msize = | ||
1521 | data_size + sizeof (struct PeerResultMessage) + (get_path_length + | ||
1522 | put_path_length) * | ||
1523 | sizeof (struct GNUNET_PeerIdentity); | ||
1524 | if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) || | ||
1525 | (get_path_length > | ||
1526 | GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) || | ||
1527 | (put_path_length > | ||
1528 | GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) || | ||
1529 | (data_size > GNUNET_SERVER_MAX_MESSAGE_SIZE)) | ||
1530 | { | ||
1531 | GNUNET_break (0); | ||
1532 | return; | ||
1533 | } | ||
1534 | pi = GNUNET_CONTAINER_multipeermap_get (all_known_peers, target); | ||
1535 | if (NULL == pi) | ||
1536 | { | ||
1537 | /* peer disconnected in the meantime, drop reply */ | ||
1538 | return; | ||
1539 | } | ||
1540 | if (pi->pending_count >= MAXIMUM_PENDING_PER_PEER) | ||
1541 | { | ||
1542 | /* skip */ | ||
1543 | GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P messages dropped due to full queue"), | ||
1544 | 1, GNUNET_NO); | ||
1545 | return; | ||
1546 | } | ||
1547 | |||
1548 | GNUNET_STATISTICS_update (GDS_stats, | ||
1549 | gettext_noop | ||
1550 | ("# RESULT messages queued for transmission"), 1, | ||
1551 | GNUNET_NO); | ||
1552 | pending = GNUNET_malloc (sizeof (struct P2PPendingMessage) + msize); | ||
1553 | pending->importance = 0; /* FIXME */ | ||
1554 | pending->timeout = expiration_time; | ||
1555 | prm = (struct PeerResultMessage *) &pending[1]; | ||
1556 | pending->msg = &prm->header; | ||
1557 | prm->header.size = htons (msize); | ||
1558 | prm->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT); | ||
1559 | prm->type = htonl (type); | ||
1560 | prm->put_path_length = htonl (put_path_length); | ||
1561 | prm->get_path_length = htonl (get_path_length); | ||
1562 | prm->expiration_time = GNUNET_TIME_absolute_hton (expiration_time); | ||
1563 | prm->key = *key; | ||
1564 | paths = (struct GNUNET_PeerIdentity *) &prm[1]; | ||
1565 | memcpy (paths, put_path, | ||
1566 | put_path_length * sizeof (struct GNUNET_PeerIdentity)); | ||
1567 | memcpy (&paths[put_path_length], get_path, | ||
1568 | get_path_length * sizeof (struct GNUNET_PeerIdentity)); | ||
1569 | memcpy (&paths[put_path_length + get_path_length], data, data_size); | ||
1570 | GNUNET_CONTAINER_DLL_insert (pi->head, pi->tail, pending); | ||
1571 | pi->pending_count++; | ||
1572 | process_peer_queue (pi); | ||
1573 | } | ||
1574 | |||
1575 | |||
1576 | /** | ||
1577 | * To be called on core init/fail. | ||
1578 | * | ||
1579 | * @param cls service closure | ||
1580 | * @param identity the public identity of this peer | ||
1581 | */ | ||
1582 | static void | ||
1583 | core_init (void *cls, | ||
1584 | const struct GNUNET_PeerIdentity *identity) | ||
1585 | { | ||
1586 | my_identity = *identity; | ||
1587 | GNUNET_CRYPTO_hash (identity, | ||
1588 | sizeof (struct GNUNET_PeerIdentity), | ||
1589 | &my_identity_hash); | ||
1590 | } | ||
1591 | |||
1592 | |||
1593 | /** | ||
1594 | * Core handler for p2p put requests. | ||
1595 | * | ||
1596 | * @param cls closure | ||
1597 | * @param peer sender of the request | ||
1598 | * @param message message | ||
1599 | * @param peer peer identity this notification is about | ||
1600 | * @return #GNUNET_OK to keep the connection open, | ||
1601 | * #GNUNET_SYSERR to close it (signal serious error) | ||
1602 | */ | ||
1603 | static int | ||
1604 | handle_dht_p2p_put (void *cls, | ||
1605 | const struct GNUNET_PeerIdentity *peer, | ||
1606 | const struct GNUNET_MessageHeader *message) | ||
1607 | { | ||
1608 | const struct PeerPutMessage *put; | ||
1609 | const struct GNUNET_PeerIdentity *put_path; | ||
1610 | const void *payload; | ||
1611 | uint32_t putlen; | ||
1612 | uint16_t msize; | ||
1613 | size_t payload_size; | ||
1614 | enum GNUNET_DHT_RouteOption options; | ||
1615 | struct GNUNET_CONTAINER_BloomFilter *bf; | ||
1616 | struct GNUNET_HashCode test_key; | ||
1617 | struct GNUNET_HashCode phash; | ||
1618 | |||
1619 | msize = ntohs (message->size); | ||
1620 | if (msize < sizeof (struct PeerPutMessage)) | ||
1621 | { | ||
1622 | GNUNET_break_op (0); | ||
1623 | return GNUNET_YES; | ||
1624 | } | ||
1625 | put = (const struct PeerPutMessage *) message; | ||
1626 | putlen = ntohl (put->put_path_length); | ||
1627 | if ((msize < | ||
1628 | sizeof (struct PeerPutMessage) + | ||
1629 | putlen * sizeof (struct GNUNET_PeerIdentity)) || | ||
1630 | (putlen > | ||
1631 | GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity))) | ||
1632 | { | ||
1633 | GNUNET_break_op (0); | ||
1634 | return GNUNET_YES; | ||
1635 | } | ||
1636 | GNUNET_STATISTICS_update (GDS_stats, | ||
1637 | gettext_noop ("# P2P PUT requests received"), 1, | ||
1638 | GNUNET_NO); | ||
1639 | GNUNET_STATISTICS_update (GDS_stats, | ||
1640 | gettext_noop ("# P2P PUT bytes received"), msize, | ||
1641 | GNUNET_NO); | ||
1642 | put_path = (const struct GNUNET_PeerIdentity *) &put[1]; | ||
1643 | payload = &put_path[putlen]; | ||
1644 | options = ntohl (put->options); | ||
1645 | payload_size = | ||
1646 | msize - (sizeof (struct PeerPutMessage) + | ||
1647 | putlen * sizeof (struct GNUNET_PeerIdentity)); | ||
1648 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PUT for `%s' from %s\n", | ||
1649 | GNUNET_h2s (&put->key), GNUNET_i2s (peer)); | ||
1650 | GNUNET_CRYPTO_hash (peer, sizeof (struct GNUNET_PeerIdentity), &phash); | ||
1651 | if (GNUNET_YES == log_route_details_stderr) | ||
1652 | { | ||
1653 | char *tmp; | ||
1654 | |||
1655 | tmp = GNUNET_strdup (GNUNET_i2s (&my_identity)); | ||
1656 | LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG, | ||
1657 | "XDHT PUT %s: %s->%s (%u, %u=>%u)\n", | ||
1658 | GNUNET_h2s (&put->key), GNUNET_i2s (peer), tmp, | ||
1659 | ntohl(put->hop_count), | ||
1660 | GNUNET_CRYPTO_hash_matching_bits (&phash, &put->key), | ||
1661 | GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, &put->key) | ||
1662 | ); | ||
1663 | GNUNET_free (tmp); | ||
1664 | } | ||
1665 | switch (GNUNET_BLOCK_get_key | ||
1666 | (GDS_block_context, ntohl (put->type), payload, payload_size, | ||
1667 | &test_key)) | ||
1668 | { | ||
1669 | case GNUNET_YES: | ||
1670 | if (0 != memcmp (&test_key, &put->key, sizeof (struct GNUNET_HashCode))) | ||
1671 | { | ||
1672 | char *put_s = GNUNET_strdup (GNUNET_h2s (&put->key)); | ||
1673 | GNUNET_break_op (0); | ||
1674 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1675 | "PUT with key `%s' for block with key %s\n", | ||
1676 | put_s, GNUNET_h2s (&test_key)); | ||
1677 | GNUNET_free (put_s); | ||
1678 | return GNUNET_YES; | ||
1679 | } | ||
1680 | break; | ||
1681 | case GNUNET_NO: | ||
1682 | GNUNET_break_op (0); | ||
1683 | return GNUNET_YES; | ||
1684 | case GNUNET_SYSERR: | ||
1685 | /* cannot verify, good luck */ | ||
1686 | break; | ||
1687 | } | ||
1688 | if (ntohl (put->type) == GNUNET_BLOCK_TYPE_REGEX) /* FIXME: do for all tpyes */ | ||
1689 | { | ||
1690 | switch (GNUNET_BLOCK_evaluate (GDS_block_context, | ||
1691 | ntohl (put->type), | ||
1692 | NULL, /* query */ | ||
1693 | NULL, 0, /* bloom filer */ | ||
1694 | NULL, 0, /* xquery */ | ||
1695 | payload, payload_size)) | ||
1696 | { | ||
1697 | case GNUNET_BLOCK_EVALUATION_OK_MORE: | ||
1698 | case GNUNET_BLOCK_EVALUATION_OK_LAST: | ||
1699 | break; | ||
1700 | |||
1701 | case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE: | ||
1702 | case GNUNET_BLOCK_EVALUATION_RESULT_INVALID: | ||
1703 | case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT: | ||
1704 | case GNUNET_BLOCK_EVALUATION_REQUEST_VALID: | ||
1705 | case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID: | ||
1706 | case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED: | ||
1707 | default: | ||
1708 | GNUNET_break_op (0); | ||
1709 | return GNUNET_OK; | ||
1710 | } | ||
1711 | } | ||
1712 | |||
1713 | bf = GNUNET_CONTAINER_bloomfilter_init (put->bloomfilter, DHT_BLOOM_SIZE, | ||
1714 | GNUNET_CONSTANTS_BLOOMFILTER_K); | ||
1715 | GNUNET_break_op (GNUNET_YES == | ||
1716 | GNUNET_CONTAINER_bloomfilter_test (bf, &phash)); | ||
1717 | { | ||
1718 | struct GNUNET_PeerIdentity pp[putlen + 1]; | ||
1719 | |||
1720 | /* extend 'put path' by sender */ | ||
1721 | if (0 != (options & GNUNET_DHT_RO_RECORD_ROUTE)) | ||
1722 | { | ||
1723 | memcpy (pp, put_path, putlen * sizeof (struct GNUNET_PeerIdentity)); | ||
1724 | pp[putlen] = *peer; | ||
1725 | putlen++; | ||
1726 | } | ||
1727 | else | ||
1728 | putlen = 0; | ||
1729 | |||
1730 | /* give to local clients */ | ||
1731 | GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (put->expiration_time), | ||
1732 | &put->key, 0, NULL, putlen, pp, ntohl (put->type), | ||
1733 | payload_size, payload); | ||
1734 | /* store locally */ | ||
1735 | if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) || | ||
1736 | (am_closest_peer (&put->key, bf))) | ||
1737 | GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh | ||
1738 | (put->expiration_time), &put->key, putlen, pp, | ||
1739 | ntohl (put->type), payload_size, payload); | ||
1740 | /* route to other peers */ | ||
1741 | GDS_NEIGHBOURS_handle_put (ntohl (put->type), options, | ||
1742 | ntohl (put->desired_replication_level), | ||
1743 | GNUNET_TIME_absolute_ntoh (put->expiration_time), | ||
1744 | ntohl (put->hop_count), bf, &put->key, putlen, | ||
1745 | pp, payload, payload_size); | ||
1746 | /* notify monitoring clients */ | ||
1747 | GDS_CLIENTS_process_put (options, | ||
1748 | ntohl (put->type), | ||
1749 | ntohl (put->hop_count), | ||
1750 | ntohl (put->desired_replication_level), | ||
1751 | putlen, pp, | ||
1752 | GNUNET_TIME_absolute_ntoh (put->expiration_time), | ||
1753 | &put->key, | ||
1754 | payload, | ||
1755 | payload_size); | ||
1756 | } | ||
1757 | GNUNET_CONTAINER_bloomfilter_free (bf); | ||
1758 | return GNUNET_YES; | ||
1759 | } | ||
1760 | |||
1761 | |||
1762 | /** | ||
1763 | * We have received a FIND PEER request. Send matching | ||
1764 | * HELLOs back. | ||
1765 | * | ||
1766 | * @param sender sender of the FIND PEER request | ||
1767 | * @param key peers close to this key are desired | ||
1768 | * @param bf peers matching this bf are excluded | ||
1769 | * @param bf_mutator mutator for bf | ||
1770 | */ | ||
1771 | static void | ||
1772 | handle_find_peer (const struct GNUNET_PeerIdentity *sender, | ||
1773 | const struct GNUNET_HashCode * key, | ||
1774 | struct GNUNET_CONTAINER_BloomFilter *bf, uint32_t bf_mutator) | ||
1775 | { | ||
1776 | int bucket_idx; | ||
1777 | struct PeerBucket *bucket; | ||
1778 | struct PeerInfo *peer; | ||
1779 | unsigned int choice; | ||
1780 | struct GNUNET_HashCode phash; | ||
1781 | struct GNUNET_HashCode mhash; | ||
1782 | const struct GNUNET_HELLO_Message *hello; | ||
1783 | |||
1784 | /* first, check about our own HELLO */ | ||
1785 | if (NULL != GDS_my_hello) | ||
1786 | { | ||
1787 | GNUNET_BLOCK_mingle_hash (&my_identity_hash, bf_mutator, &mhash); | ||
1788 | if ((NULL == bf) || | ||
1789 | (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (bf, &mhash))) | ||
1790 | { | ||
1791 | GDS_NEIGHBOURS_handle_reply (sender, GNUNET_BLOCK_TYPE_DHT_HELLO, | ||
1792 | GNUNET_TIME_relative_to_absolute | ||
1793 | (hello_expiration), | ||
1794 | key, 0, NULL, 0, NULL, GDS_my_hello, | ||
1795 | GNUNET_HELLO_size ((const struct | ||
1796 | GNUNET_HELLO_Message *) | ||
1797 | GDS_my_hello)); | ||
1798 | } | ||
1799 | else | ||
1800 | { | ||
1801 | GNUNET_STATISTICS_update (GDS_stats, | ||
1802 | gettext_noop | ||
1803 | ("# FIND PEER requests ignored due to Bloomfilter"), | ||
1804 | 1, GNUNET_NO); | ||
1805 | } | ||
1806 | } | ||
1807 | else | ||
1808 | { | ||
1809 | GNUNET_STATISTICS_update (GDS_stats, | ||
1810 | gettext_noop | ||
1811 | ("# FIND PEER requests ignored due to lack of HELLO"), | ||
1812 | 1, GNUNET_NO); | ||
1813 | } | ||
1814 | |||
1815 | /* then, also consider sending a random HELLO from the closest bucket */ | ||
1816 | if (0 == memcmp (&my_identity_hash, key, sizeof (struct GNUNET_HashCode))) | ||
1817 | bucket_idx = closest_bucket; | ||
1818 | else | ||
1819 | bucket_idx = GNUNET_MIN (closest_bucket, find_bucket (key)); | ||
1820 | if (bucket_idx == GNUNET_SYSERR) | ||
1821 | return; | ||
1822 | bucket = &k_buckets[bucket_idx]; | ||
1823 | if (bucket->peers_size == 0) | ||
1824 | return; | ||
1825 | choice = | ||
1826 | GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, bucket->peers_size); | ||
1827 | peer = bucket->head; | ||
1828 | while (choice > 0) | ||
1829 | { | ||
1830 | GNUNET_assert (NULL != peer); | ||
1831 | peer = peer->next; | ||
1832 | choice--; | ||
1833 | } | ||
1834 | choice = bucket->peers_size; | ||
1835 | do | ||
1836 | { | ||
1837 | peer = peer->next; | ||
1838 | if (choice-- == 0) | ||
1839 | return; /* no non-masked peer available */ | ||
1840 | if (peer == NULL) | ||
1841 | peer = bucket->head; | ||
1842 | GNUNET_CRYPTO_hash (&peer->id, sizeof (struct GNUNET_PeerIdentity), &phash); | ||
1843 | GNUNET_BLOCK_mingle_hash (&phash, bf_mutator, &mhash); | ||
1844 | hello = GDS_HELLO_get (&peer->id); | ||
1845 | } | ||
1846 | while ((hello == NULL) || | ||
1847 | (GNUNET_YES == GNUNET_CONTAINER_bloomfilter_test (bf, &mhash))); | ||
1848 | GDS_NEIGHBOURS_handle_reply (sender, GNUNET_BLOCK_TYPE_DHT_HELLO, | ||
1849 | GNUNET_TIME_relative_to_absolute | ||
1850 | (GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION), key, | ||
1851 | 0, NULL, 0, NULL, hello, | ||
1852 | GNUNET_HELLO_size (hello)); | ||
1853 | } | ||
1854 | |||
1855 | |||
1856 | /** | ||
1857 | * Core handler for p2p get requests. | ||
1858 | * | ||
1859 | * @param cls closure | ||
1860 | * @param peer sender of the request | ||
1861 | * @param message message | ||
1862 | * @return #GNUNET_OK to keep the connection open, | ||
1863 | * #GNUNET_SYSERR to close it (signal serious error) | ||
1864 | */ | ||
1865 | static int | ||
1866 | handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
1867 | const struct GNUNET_MessageHeader *message) | ||
1868 | { | ||
1869 | struct PeerGetMessage *get; | ||
1870 | uint32_t xquery_size; | ||
1871 | size_t reply_bf_size; | ||
1872 | uint16_t msize; | ||
1873 | enum GNUNET_BLOCK_Type type; | ||
1874 | enum GNUNET_DHT_RouteOption options; | ||
1875 | enum GNUNET_BLOCK_EvaluationResult eval; | ||
1876 | struct GNUNET_CONTAINER_BloomFilter *reply_bf; | ||
1877 | struct GNUNET_CONTAINER_BloomFilter *peer_bf; | ||
1878 | const char *xquery; | ||
1879 | struct GNUNET_HashCode phash; | ||
1880 | |||
1881 | GNUNET_break (0 != | ||
1882 | memcmp (peer, &my_identity, | ||
1883 | sizeof (struct GNUNET_PeerIdentity))); | ||
1884 | /* parse and validate message */ | ||
1885 | msize = ntohs (message->size); | ||
1886 | if (msize < sizeof (struct PeerGetMessage)) | ||
1887 | { | ||
1888 | GNUNET_break_op (0); | ||
1889 | return GNUNET_YES; | ||
1890 | } | ||
1891 | get = (struct PeerGetMessage *) message; | ||
1892 | xquery_size = ntohl (get->xquery_size); | ||
1893 | if (msize < sizeof (struct PeerGetMessage) + xquery_size) | ||
1894 | { | ||
1895 | GNUNET_break_op (0); | ||
1896 | return GNUNET_YES; | ||
1897 | } | ||
1898 | reply_bf_size = msize - (sizeof (struct PeerGetMessage) + xquery_size); | ||
1899 | type = ntohl (get->type); | ||
1900 | options = ntohl (get->options); | ||
1901 | xquery = (const char *) &get[1]; | ||
1902 | reply_bf = NULL; | ||
1903 | GNUNET_STATISTICS_update (GDS_stats, | ||
1904 | gettext_noop ("# P2P GET requests received"), 1, | ||
1905 | GNUNET_NO); | ||
1906 | GNUNET_STATISTICS_update (GDS_stats, | ||
1907 | gettext_noop ("# P2P GET bytes received"), msize, | ||
1908 | GNUNET_NO); | ||
1909 | GNUNET_CRYPTO_hash (peer, sizeof (struct GNUNET_PeerIdentity), &phash); | ||
1910 | if (GNUNET_YES == log_route_details_stderr) | ||
1911 | { | ||
1912 | char *tmp; | ||
1913 | |||
1914 | tmp = GNUNET_strdup (GNUNET_i2s (&my_identity)); | ||
1915 | LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG, | ||
1916 | "XDHT GET %s: %s->%s (%u, %u=>%u) xq: %.*s\n", | ||
1917 | GNUNET_h2s (&get->key), GNUNET_i2s (peer), tmp, | ||
1918 | ntohl(get->hop_count), | ||
1919 | GNUNET_CRYPTO_hash_matching_bits (&phash, &get->key), | ||
1920 | GNUNET_CRYPTO_hash_matching_bits (&my_identity_hash, &get->key), | ||
1921 | ntohl(get->xquery_size), xquery | ||
1922 | ); | ||
1923 | GNUNET_free (tmp); | ||
1924 | } | ||
1925 | |||
1926 | if (reply_bf_size > 0) | ||
1927 | reply_bf = | ||
1928 | GNUNET_CONTAINER_bloomfilter_init (&xquery[xquery_size], reply_bf_size, | ||
1929 | GNUNET_CONSTANTS_BLOOMFILTER_K); | ||
1930 | eval = | ||
1931 | GNUNET_BLOCK_evaluate (GDS_block_context, type, &get->key, &reply_bf, | ||
1932 | get->bf_mutator, xquery, xquery_size, NULL, 0); | ||
1933 | if (eval != GNUNET_BLOCK_EVALUATION_REQUEST_VALID) | ||
1934 | { | ||
1935 | /* request invalid or block type not supported */ | ||
1936 | GNUNET_break_op (eval == GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED); | ||
1937 | if (NULL != reply_bf) | ||
1938 | GNUNET_CONTAINER_bloomfilter_free (reply_bf); | ||
1939 | return GNUNET_YES; | ||
1940 | } | ||
1941 | peer_bf = | ||
1942 | GNUNET_CONTAINER_bloomfilter_init (get->bloomfilter, DHT_BLOOM_SIZE, | ||
1943 | GNUNET_CONSTANTS_BLOOMFILTER_K); | ||
1944 | GNUNET_break_op (GNUNET_YES == | ||
1945 | GNUNET_CONTAINER_bloomfilter_test (peer_bf, | ||
1946 | &phash)); | ||
1947 | /* remember request for routing replies */ | ||
1948 | GDS_ROUTING_add (peer, type, options, &get->key, xquery, xquery_size, | ||
1949 | reply_bf, get->bf_mutator); | ||
1950 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GET for %s at %s after %u hops\n", | ||
1951 | GNUNET_h2s (&get->key), GNUNET_i2s (&my_identity), | ||
1952 | (unsigned int) ntohl (get->hop_count)); | ||
1953 | /* local lookup (this may update the reply_bf) */ | ||
1954 | if ((0 != (options & GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE)) || | ||
1955 | (am_closest_peer (&get->key, peer_bf))) | ||
1956 | { | ||
1957 | if ((0 != (options & GNUNET_DHT_RO_FIND_PEER))) | ||
1958 | { | ||
1959 | GNUNET_STATISTICS_update (GDS_stats, | ||
1960 | gettext_noop | ||
1961 | ("# P2P FIND PEER requests processed"), 1, | ||
1962 | GNUNET_NO); | ||
1963 | handle_find_peer (peer, &get->key, reply_bf, get->bf_mutator); | ||
1964 | } | ||
1965 | else | ||
1966 | { | ||
1967 | eval = | ||
1968 | GDS_DATACACHE_handle_get (&get->key, type, xquery, xquery_size, | ||
1969 | &reply_bf, get->bf_mutator); | ||
1970 | } | ||
1971 | } | ||
1972 | else | ||
1973 | { | ||
1974 | GNUNET_STATISTICS_update (GDS_stats, | ||
1975 | gettext_noop ("# P2P GET requests ONLY routed"), | ||
1976 | 1, GNUNET_NO); | ||
1977 | } | ||
1978 | |||
1979 | GDS_CLIENTS_process_get (options, | ||
1980 | type, | ||
1981 | ntohl(get->hop_count), | ||
1982 | ntohl(get->desired_replication_level), | ||
1983 | 0, NULL, | ||
1984 | &get->key); | ||
1985 | |||
1986 | /* P2P forwarding */ | ||
1987 | if (eval != GNUNET_BLOCK_EVALUATION_OK_LAST) | ||
1988 | GDS_NEIGHBOURS_handle_get (type, options, | ||
1989 | ntohl (get->desired_replication_level), | ||
1990 | ntohl (get->hop_count), &get->key, xquery, | ||
1991 | xquery_size, reply_bf, get->bf_mutator, peer_bf); | ||
1992 | /* clean up */ | ||
1993 | if (NULL != reply_bf) | ||
1994 | GNUNET_CONTAINER_bloomfilter_free (reply_bf); | ||
1995 | GNUNET_CONTAINER_bloomfilter_free (peer_bf); | ||
1996 | return GNUNET_YES; | ||
1997 | } | ||
1998 | |||
1999 | |||
2000 | /** | ||
2001 | * Core handler for p2p result messages. | ||
2002 | * | ||
2003 | * @param cls closure | ||
2004 | * @param message message | ||
2005 | * @param peer peer identity this notification is about | ||
2006 | * @return #GNUNET_YES (do not cut p2p connection) | ||
2007 | */ | ||
2008 | static int | ||
2009 | handle_dht_p2p_result (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
2010 | const struct GNUNET_MessageHeader *message) | ||
2011 | { | ||
2012 | const struct PeerResultMessage *prm; | ||
2013 | const struct GNUNET_PeerIdentity *put_path; | ||
2014 | const struct GNUNET_PeerIdentity *get_path; | ||
2015 | const void *data; | ||
2016 | uint32_t get_path_length; | ||
2017 | uint32_t put_path_length; | ||
2018 | uint16_t msize; | ||
2019 | size_t data_size; | ||
2020 | enum GNUNET_BLOCK_Type type; | ||
2021 | |||
2022 | /* parse and validate message */ | ||
2023 | msize = ntohs (message->size); | ||
2024 | if (msize < sizeof (struct PeerResultMessage)) | ||
2025 | { | ||
2026 | GNUNET_break_op (0); | ||
2027 | return GNUNET_YES; | ||
2028 | } | ||
2029 | prm = (struct PeerResultMessage *) message; | ||
2030 | put_path_length = ntohl (prm->put_path_length); | ||
2031 | get_path_length = ntohl (prm->get_path_length); | ||
2032 | if ((msize < | ||
2033 | sizeof (struct PeerResultMessage) + (get_path_length + | ||
2034 | put_path_length) * | ||
2035 | sizeof (struct GNUNET_PeerIdentity)) || | ||
2036 | (get_path_length > | ||
2037 | GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity)) || | ||
2038 | (put_path_length > | ||
2039 | GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_PeerIdentity))) | ||
2040 | { | ||
2041 | GNUNET_break_op (0); | ||
2042 | return GNUNET_YES; | ||
2043 | } | ||
2044 | put_path = (const struct GNUNET_PeerIdentity *) &prm[1]; | ||
2045 | get_path = &put_path[put_path_length]; | ||
2046 | type = ntohl (prm->type); | ||
2047 | data = (const void *) &get_path[get_path_length]; | ||
2048 | data_size = | ||
2049 | msize - (sizeof (struct PeerResultMessage) + | ||
2050 | (get_path_length + | ||
2051 | put_path_length) * sizeof (struct GNUNET_PeerIdentity)); | ||
2052 | GNUNET_STATISTICS_update (GDS_stats, gettext_noop ("# P2P RESULTS received"), | ||
2053 | 1, GNUNET_NO); | ||
2054 | GNUNET_STATISTICS_update (GDS_stats, | ||
2055 | gettext_noop ("# P2P RESULT bytes received"), | ||
2056 | msize, GNUNET_NO); | ||
2057 | if (GNUNET_YES == log_route_details_stderr) | ||
2058 | { | ||
2059 | char *tmp; | ||
2060 | |||
2061 | tmp = GNUNET_strdup (GNUNET_i2s (&my_identity)); | ||
2062 | LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG, "XDHT RESULT %s: %s->%s (%u)\n", | ||
2063 | GNUNET_h2s (&prm->key), GNUNET_i2s (peer), tmp, | ||
2064 | get_path_length + 1); | ||
2065 | GNUNET_free (tmp); | ||
2066 | } | ||
2067 | /* if we got a HELLO, consider it for our own routing table */ | ||
2068 | if (type == GNUNET_BLOCK_TYPE_DHT_HELLO) | ||
2069 | { | ||
2070 | const struct GNUNET_MessageHeader *h; | ||
2071 | struct GNUNET_PeerIdentity pid; | ||
2072 | int bucket; | ||
2073 | |||
2074 | /* Should be a HELLO, validate and consider using it! */ | ||
2075 | if (data_size < sizeof (struct GNUNET_MessageHeader)) | ||
2076 | { | ||
2077 | GNUNET_break_op (0); | ||
2078 | return GNUNET_YES; | ||
2079 | } | ||
2080 | h = data; | ||
2081 | if (data_size != ntohs (h->size)) | ||
2082 | { | ||
2083 | GNUNET_break_op (0); | ||
2084 | return GNUNET_YES; | ||
2085 | } | ||
2086 | if (GNUNET_OK != | ||
2087 | GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) h, &pid)) | ||
2088 | { | ||
2089 | GNUNET_break_op (0); | ||
2090 | return GNUNET_YES; | ||
2091 | } | ||
2092 | if ((GNUNET_YES != disable_try_connect) && | ||
2093 | 0 != memcmp (&my_identity, &pid, sizeof (struct GNUNET_PeerIdentity))) | ||
2094 | { | ||
2095 | struct GNUNET_HashCode pid_hash; | ||
2096 | |||
2097 | GNUNET_CRYPTO_hash (&pid, sizeof (struct GNUNET_PeerIdentity), &pid_hash); | ||
2098 | bucket = find_bucket (&pid_hash); | ||
2099 | if ((bucket >= 0) && | ||
2100 | (k_buckets[bucket].peers_size < bucket_size) && | ||
2101 | (NULL != GDS_transport_handle)) | ||
2102 | { | ||
2103 | GNUNET_TRANSPORT_offer_hello (GDS_transport_handle, h, NULL, NULL); | ||
2104 | GNUNET_TRANSPORT_try_connect (GDS_transport_handle, &pid, NULL, NULL); /*FIXME TRY_CONNECT change */ | ||
2105 | } | ||
2106 | } | ||
2107 | } | ||
2108 | |||
2109 | /* append 'peer' to 'get_path' */ | ||
2110 | { | ||
2111 | struct GNUNET_PeerIdentity xget_path[get_path_length + 1]; | ||
2112 | |||
2113 | memcpy (xget_path, get_path, | ||
2114 | get_path_length * sizeof (struct GNUNET_PeerIdentity)); | ||
2115 | xget_path[get_path_length] = *peer; | ||
2116 | get_path_length++; | ||
2117 | |||
2118 | /* forward to local clients */ | ||
2119 | GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (prm->expiration_time), | ||
2120 | &prm->key, get_path_length, xget_path, | ||
2121 | put_path_length, put_path, type, data_size, data); | ||
2122 | GDS_CLIENTS_process_get_resp (type, | ||
2123 | xget_path, get_path_length, | ||
2124 | put_path, put_path_length, | ||
2125 | GNUNET_TIME_absolute_ntoh ( | ||
2126 | prm->expiration_time), | ||
2127 | &prm->key, | ||
2128 | data, | ||
2129 | data_size); | ||
2130 | if (GNUNET_YES == cache_results) | ||
2131 | { | ||
2132 | struct GNUNET_PeerIdentity xput_path[get_path_length + 1 + put_path_length]; | ||
2133 | |||
2134 | memcpy (xput_path, put_path, put_path_length * sizeof (struct GNUNET_PeerIdentity)); | ||
2135 | memcpy (&xput_path[put_path_length], | ||
2136 | xget_path, | ||
2137 | get_path_length * sizeof (struct GNUNET_PeerIdentity)); | ||
2138 | |||
2139 | GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (prm->expiration_time), | ||
2140 | &prm->key, | ||
2141 | get_path_length + put_path_length, xput_path, | ||
2142 | type, data_size, data); | ||
2143 | } | ||
2144 | /* forward to other peers */ | ||
2145 | GDS_ROUTING_process (type, GNUNET_TIME_absolute_ntoh (prm->expiration_time), | ||
2146 | &prm->key, put_path_length, put_path, get_path_length, | ||
2147 | xget_path, data, data_size); | ||
2148 | } | ||
2149 | |||
2150 | return GNUNET_YES; | ||
2151 | } | ||
2152 | |||
2153 | |||
2154 | /** | ||
2155 | * Initialize neighbours subsystem. | ||
2156 | * | ||
2157 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | ||
2158 | */ | ||
2159 | int | ||
2160 | GDS_NEIGHBOURS_init () | ||
2161 | { | ||
2162 | static struct GNUNET_CORE_MessageHandler core_handlers[] = { | ||
2163 | {&handle_dht_p2p_get, GNUNET_MESSAGE_TYPE_DHT_P2P_GET, 0}, | ||
2164 | {&handle_dht_p2p_put, GNUNET_MESSAGE_TYPE_DHT_P2P_PUT, 0}, | ||
2165 | {&handle_dht_p2p_result, GNUNET_MESSAGE_TYPE_DHT_P2P_RESULT, 0}, | ||
2166 | {NULL, 0, 0} | ||
2167 | }; | ||
2168 | unsigned long long temp_config_num; | ||
2169 | |||
2170 | disable_try_connect | ||
2171 | = GNUNET_CONFIGURATION_get_value_yesno (GDS_cfg, "DHT", "DISABLE_TRY_CONNECT"); | ||
2172 | if (GNUNET_OK == | ||
2173 | GNUNET_CONFIGURATION_get_value_number (GDS_cfg, "DHT", "bucket_size", | ||
2174 | &temp_config_num)) | ||
2175 | bucket_size = (unsigned int) temp_config_num; | ||
2176 | cache_results | ||
2177 | = GNUNET_CONFIGURATION_get_value_yesno (GDS_cfg, "DHT", "CACHE_RESULTS"); | ||
2178 | |||
2179 | log_route_details_stderr = | ||
2180 | (NULL != getenv("GNUNET_DHT_ROUTE_DEBUG")) ? GNUNET_YES : GNUNET_NO; | ||
2181 | atsAPI = GNUNET_ATS_performance_init (GDS_cfg, NULL, NULL); | ||
2182 | core_api = | ||
2183 | GNUNET_CORE_connect (GDS_cfg, NULL, &core_init, &handle_core_connect, | ||
2184 | &handle_core_disconnect, NULL, GNUNET_NO, NULL, | ||
2185 | GNUNET_NO, core_handlers); | ||
2186 | if (core_api == NULL) | ||
2187 | return GNUNET_SYSERR; | ||
2188 | all_known_peers = GNUNET_CONTAINER_multipeermap_create (256, GNUNET_NO); | ||
2189 | return GNUNET_OK; | ||
2190 | } | ||
2191 | |||
2192 | |||
2193 | /** | ||
2194 | * Shutdown neighbours subsystem. | ||
2195 | */ | ||
2196 | void | ||
2197 | GDS_NEIGHBOURS_done () | ||
2198 | { | ||
2199 | if (NULL == core_api) | ||
2200 | return; | ||
2201 | GNUNET_CORE_disconnect (core_api); | ||
2202 | core_api = NULL; | ||
2203 | GNUNET_ATS_performance_done (atsAPI); | ||
2204 | atsAPI = NULL; | ||
2205 | GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (all_known_peers)); | ||
2206 | GNUNET_CONTAINER_multipeermap_destroy (all_known_peers); | ||
2207 | all_known_peers = NULL; | ||
2208 | if (GNUNET_SCHEDULER_NO_TASK != find_peer_task) | ||
2209 | { | ||
2210 | GNUNET_SCHEDULER_cancel (find_peer_task); | ||
2211 | find_peer_task = GNUNET_SCHEDULER_NO_TASK; | ||
2212 | } | ||
2213 | } | ||
2214 | |||
2215 | /** | ||
2216 | * Get the ID of the local node. | ||
2217 | * | ||
2218 | * @return identity of the local node | ||
2219 | */ | ||
2220 | struct GNUNET_PeerIdentity * | ||
2221 | GDS_NEIGHBOURS_get_id () | ||
2222 | { | ||
2223 | return &my_identity; | ||
2224 | } | ||
2225 | |||
2226 | |||
2227 | /* end of gnunet-service-dht_neighbours.c */ | ||
diff --git a/src/dht/gnunet-service-xdht_neighbours.h b/src/dht/gnunet-service-xdht_neighbours.h new file mode 100644 index 000000000..759aa3d9d --- /dev/null +++ b/src/dht/gnunet-service-xdht_neighbours.h | |||
@@ -0,0 +1,147 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file dht/gnunet-service-xdht_neighbours.h | ||
23 | * @brief GNUnet DHT routing code | ||
24 | * @author Christian Grothoff | ||
25 | * @author Nathan Evans | ||
26 | */ | ||
27 | #ifndef GNUNET_SERVICE_DHT_NEIGHBOURS_H | ||
28 | #define GNUNET_SERVICE_DHT_NEIGHBOURS_H | ||
29 | |||
30 | #include "gnunet_util_lib.h" | ||
31 | #include "gnunet_block_lib.h" | ||
32 | #include "gnunet_dht_service.h" | ||
33 | |||
34 | /** | ||
35 | * Perform a PUT operation. Forwards the given request to other | ||
36 | * peers. Does not store the data locally. Does not give the | ||
37 | * data to local clients. May do nothing if this is the only | ||
38 | * peer in the network (or if we are the closest peer in the | ||
39 | * network). | ||
40 | * | ||
41 | * @param type type of the block | ||
42 | * @param options routing options | ||
43 | * @param desired_replication_level desired replication level | ||
44 | * @param expiration_time when does the content expire | ||
45 | * @param hop_count how many hops has this message traversed so far | ||
46 | * @param bf Bloom filter of peers this PUT has already traversed | ||
47 | * @param key key for the content | ||
48 | * @param put_path_length number of entries in put_path | ||
49 | * @param put_path peers this request has traversed so far (if tracked) | ||
50 | * @param data payload to store | ||
51 | * @param data_size number of bytes in data | ||
52 | */ | ||
53 | void | ||
54 | GDS_NEIGHBOURS_handle_put (enum GNUNET_BLOCK_Type type, | ||
55 | enum GNUNET_DHT_RouteOption options, | ||
56 | uint32_t desired_replication_level, | ||
57 | struct GNUNET_TIME_Absolute expiration_time, | ||
58 | uint32_t hop_count, | ||
59 | struct GNUNET_CONTAINER_BloomFilter *bf, | ||
60 | const struct GNUNET_HashCode * key, | ||
61 | unsigned int put_path_length, | ||
62 | struct GNUNET_PeerIdentity *put_path, | ||
63 | const void *data, size_t data_size); | ||
64 | |||
65 | |||
66 | /** | ||
67 | * Perform a GET operation. Forwards the given request to other | ||
68 | * peers. Does not lookup the key locally. May do nothing if this is | ||
69 | * the only peer in the network (or if we are the closest peer in the | ||
70 | * network). | ||
71 | * | ||
72 | * @param type type of the block | ||
73 | * @param options routing options | ||
74 | * @param desired_replication_level desired replication count | ||
75 | * @param hop_count how many hops did this request traverse so far? | ||
76 | * @param key key for the content | ||
77 | * @param xquery extended query | ||
78 | * @param xquery_size number of bytes in xquery | ||
79 | * @param reply_bf bloomfilter to filter duplicates | ||
80 | * @param reply_bf_mutator mutator for reply_bf | ||
81 | * @param peer_bf filter for peers not to select (again, updated) | ||
82 | */ | ||
83 | void | ||
84 | GDS_NEIGHBOURS_handle_get (enum GNUNET_BLOCK_Type type, | ||
85 | enum GNUNET_DHT_RouteOption options, | ||
86 | uint32_t desired_replication_level, | ||
87 | uint32_t hop_count, const struct GNUNET_HashCode * key, | ||
88 | const void *xquery, size_t xquery_size, | ||
89 | const struct GNUNET_CONTAINER_BloomFilter *reply_bf, | ||
90 | uint32_t reply_bf_mutator, | ||
91 | struct GNUNET_CONTAINER_BloomFilter *peer_bf); | ||
92 | |||
93 | |||
94 | /** | ||
95 | * Handle a reply (route to origin). Only forwards the reply back to | ||
96 | * other peers waiting for it. Does not do local caching or | ||
97 | * forwarding to local clients. | ||
98 | * | ||
99 | * @param target neighbour that should receive the block (if still connected) | ||
100 | * @param type type of the block | ||
101 | * @param expiration_time when does the content expire | ||
102 | * @param key key for the content | ||
103 | * @param put_path_length number of entries in put_path | ||
104 | * @param put_path peers the original PUT traversed (if tracked) | ||
105 | * @param get_path_length number of entries in put_path | ||
106 | * @param get_path peers this reply has traversed so far (if tracked) | ||
107 | * @param data payload of the reply | ||
108 | * @param data_size number of bytes in data | ||
109 | */ | ||
110 | void | ||
111 | GDS_NEIGHBOURS_handle_reply (const struct GNUNET_PeerIdentity *target, | ||
112 | enum GNUNET_BLOCK_Type type, | ||
113 | struct GNUNET_TIME_Absolute expiration_time, | ||
114 | const struct GNUNET_HashCode * key, | ||
115 | unsigned int put_path_length, | ||
116 | const struct GNUNET_PeerIdentity *put_path, | ||
117 | unsigned int get_path_length, | ||
118 | const struct GNUNET_PeerIdentity *get_path, | ||
119 | const void *data, size_t data_size); | ||
120 | |||
121 | |||
122 | /** | ||
123 | * Initialize neighbours subsystem. | ||
124 | * | ||
125 | * @return GNUNET_OK on success, GNUNET_SYSERR on error | ||
126 | */ | ||
127 | int | ||
128 | GDS_NEIGHBOURS_init (void); | ||
129 | |||
130 | |||
131 | /** | ||
132 | * Shutdown neighbours subsystem. | ||
133 | */ | ||
134 | void | ||
135 | GDS_NEIGHBOURS_done (void); | ||
136 | |||
137 | |||
138 | /** | ||
139 | * Get the ID of the local node. | ||
140 | * | ||
141 | * @return identity of the local node | ||
142 | */ | ||
143 | struct GNUNET_PeerIdentity * | ||
144 | GDS_NEIGHBOURS_get_id (); | ||
145 | |||
146 | |||
147 | #endif | ||
diff --git a/src/dht/gnunet-service-xdht_nse.c b/src/dht/gnunet-service-xdht_nse.c new file mode 100644 index 000000000..b2ea7d832 --- /dev/null +++ b/src/dht/gnunet-service-xdht_nse.c | |||
@@ -0,0 +1,116 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file dht/gnunet-service-xdht_nse.c | ||
23 | * @brief GNUnet DHT integration with NSE | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_nse_service.h" | ||
28 | #include "gnunet-service-dht.h" | ||
29 | #include "gnunet-service-dht_nse.h" | ||
30 | |||
31 | /** | ||
32 | * log of the current network size estimate, used as the point where | ||
33 | * we switch between random and deterministic routing. Default | ||
34 | * value of 4.0 is used if NSE module is not available (i.e. not | ||
35 | * configured). | ||
36 | */ | ||
37 | static double log_of_network_size_estimate = 4.0; | ||
38 | |||
39 | /** | ||
40 | * Network size estimation handle. | ||
41 | */ | ||
42 | static struct GNUNET_NSE_Handle *nse; | ||
43 | |||
44 | |||
45 | /** | ||
46 | * Callback that is called when network size estimate is updated. | ||
47 | * | ||
48 | * @param cls closure | ||
49 | * @param timestamp time when the estimate was received from the server (or created by the server) | ||
50 | * @param logestimate the log(Base 2) value of the current network size estimate | ||
51 | * @param std_dev standard deviation for the estimate | ||
52 | * | ||
53 | */ | ||
54 | static void | ||
55 | update_network_size_estimate (void *cls, struct GNUNET_TIME_Absolute timestamp, | ||
56 | double logestimate, double std_dev) | ||
57 | { | ||
58 | GNUNET_STATISTICS_update (GDS_stats, | ||
59 | gettext_noop ("# Network size estimates received"), | ||
60 | 1, GNUNET_NO); | ||
61 | /* do not allow estimates < 0.5 */ | ||
62 | log_of_network_size_estimate = GNUNET_MAX (0.5, logestimate); | ||
63 | } | ||
64 | |||
65 | |||
66 | /** | ||
67 | * Return the log of the current network size estimate. | ||
68 | * | ||
69 | * @return log of NSE | ||
70 | */ | ||
71 | double | ||
72 | GDS_NSE_get () | ||
73 | { | ||
74 | return log_of_network_size_estimate; | ||
75 | } | ||
76 | |||
77 | |||
78 | /** | ||
79 | * Initialize NSE subsystem. | ||
80 | */ | ||
81 | void | ||
82 | GDS_NSE_init () | ||
83 | { | ||
84 | unsigned long long hops; | ||
85 | |||
86 | if ( (GNUNET_YES == | ||
87 | GNUNET_CONFIGURATION_have_value (GDS_cfg, | ||
88 | "dht", | ||
89 | "FORCE_NSE")) && | ||
90 | (GNUNET_OK == | ||
91 | GNUNET_CONFIGURATION_get_value_number (GDS_cfg, | ||
92 | "dht", | ||
93 | "FORCE_NSE", | ||
94 | &hops)) ) | ||
95 | { | ||
96 | log_of_network_size_estimate = (double) hops; | ||
97 | return; | ||
98 | } | ||
99 | nse = GNUNET_NSE_connect (GDS_cfg, &update_network_size_estimate, NULL); | ||
100 | } | ||
101 | |||
102 | |||
103 | /** | ||
104 | * Shutdown NSE subsystem. | ||
105 | */ | ||
106 | void | ||
107 | GDS_NSE_done () | ||
108 | { | ||
109 | if (NULL != nse) | ||
110 | { | ||
111 | GNUNET_NSE_disconnect (nse); | ||
112 | nse = NULL; | ||
113 | } | ||
114 | } | ||
115 | |||
116 | /* end of gnunet-service-dht_nse.c */ | ||
diff --git a/src/dht/gnunet-service-xdht_nse.h b/src/dht/gnunet-service-xdht_nse.h new file mode 100644 index 000000000..666ddf393 --- /dev/null +++ b/src/dht/gnunet-service-xdht_nse.h | |||
@@ -0,0 +1,52 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file dht/gnunet-service-xdht_nse.h | ||
23 | * @brief GNUnet DHT integration with NSE | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #ifndef GNUNET_SERVICE_DHT_NSE_H | ||
27 | #define GNUNET_SERVICE_DHT_NSE_H | ||
28 | |||
29 | |||
30 | /** | ||
31 | * Return the log of the current network size estimate. | ||
32 | * | ||
33 | * @return log of NSE | ||
34 | */ | ||
35 | double | ||
36 | GDS_NSE_get (void); | ||
37 | |||
38 | |||
39 | /** | ||
40 | * Initialize NSE subsystem. | ||
41 | */ | ||
42 | void | ||
43 | GDS_NSE_init (void); | ||
44 | |||
45 | |||
46 | /** | ||
47 | * Shutdown NSE subsystem. | ||
48 | */ | ||
49 | void | ||
50 | GDS_NSE_done (void); | ||
51 | |||
52 | #endif | ||
diff --git a/src/dht/gnunet-service-xdht_routing.c b/src/dht/gnunet-service-xdht_routing.c new file mode 100644 index 000000000..3af1e6ea8 --- /dev/null +++ b/src/dht/gnunet-service-xdht_routing.c | |||
@@ -0,0 +1,458 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file dht/gnunet-service-xdht_routing.c | ||
23 | * @brief GNUnet DHT tracking of requests for routing replies | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet-service-dht_neighbours.h" | ||
28 | #include "gnunet-service-dht_routing.h" | ||
29 | #include "gnunet-service-dht.h" | ||
30 | |||
31 | |||
32 | /** | ||
33 | * Number of requests we track at most (for routing replies). | ||
34 | */ | ||
35 | #define DHT_MAX_RECENT (1024 * 16) | ||
36 | |||
37 | |||
38 | /** | ||
39 | * Information we keep about all recent GET requests | ||
40 | * so that we can route replies. | ||
41 | */ | ||
42 | struct RecentRequest | ||
43 | { | ||
44 | |||
45 | /** | ||
46 | * The peer this request was received from. | ||
47 | */ | ||
48 | struct GNUNET_PeerIdentity peer; | ||
49 | |||
50 | /** | ||
51 | * Key of this request. | ||
52 | */ | ||
53 | struct GNUNET_HashCode key; | ||
54 | |||
55 | /** | ||
56 | * Position of this node in the min heap. | ||
57 | */ | ||
58 | struct GNUNET_CONTAINER_HeapNode *heap_node; | ||
59 | |||
60 | /** | ||
61 | * Bloomfilter for replies to drop. | ||
62 | */ | ||
63 | struct GNUNET_CONTAINER_BloomFilter *reply_bf; | ||
64 | |||
65 | /** | ||
66 | * Type of the requested block. | ||
67 | */ | ||
68 | enum GNUNET_BLOCK_Type type; | ||
69 | |||
70 | /** | ||
71 | * extended query (see gnunet_block_lib.h). Allocated at the | ||
72 | * end of this struct. | ||
73 | */ | ||
74 | const void *xquery; | ||
75 | |||
76 | /** | ||
77 | * Number of bytes in xquery. | ||
78 | */ | ||
79 | size_t xquery_size; | ||
80 | |||
81 | /** | ||
82 | * Mutator value for the reply_bf, see gnunet_block_lib.h | ||
83 | */ | ||
84 | uint32_t reply_bf_mutator; | ||
85 | |||
86 | /** | ||
87 | * Request options. | ||
88 | */ | ||
89 | enum GNUNET_DHT_RouteOption options; | ||
90 | |||
91 | }; | ||
92 | |||
93 | |||
94 | /** | ||
95 | * Recent requests by time inserted. | ||
96 | */ | ||
97 | static struct GNUNET_CONTAINER_Heap *recent_heap; | ||
98 | |||
99 | /** | ||
100 | * Recently seen requests by key. | ||
101 | */ | ||
102 | static struct GNUNET_CONTAINER_MultiHashMap *recent_map; | ||
103 | |||
104 | |||
105 | /** | ||
106 | * Closure for the 'process' function. | ||
107 | */ | ||
108 | struct ProcessContext | ||
109 | { | ||
110 | /** | ||
111 | * Path of the original PUT | ||
112 | */ | ||
113 | const struct GNUNET_PeerIdentity *put_path; | ||
114 | |||
115 | /** | ||
116 | * Path of the reply. | ||
117 | */ | ||
118 | const struct GNUNET_PeerIdentity *get_path; | ||
119 | |||
120 | /** | ||
121 | * Payload of the reply. | ||
122 | */ | ||
123 | const void *data; | ||
124 | |||
125 | /** | ||
126 | * Expiration time of the result. | ||
127 | */ | ||
128 | struct GNUNET_TIME_Absolute expiration_time; | ||
129 | |||
130 | /** | ||
131 | * Number of entries in 'put_path'. | ||
132 | */ | ||
133 | unsigned int put_path_length; | ||
134 | |||
135 | /** | ||
136 | * Number of entries in 'get_path'. | ||
137 | */ | ||
138 | unsigned int get_path_length; | ||
139 | |||
140 | /** | ||
141 | * Number of bytes in 'data'. | ||
142 | */ | ||
143 | size_t data_size; | ||
144 | |||
145 | /** | ||
146 | * Type of the reply. | ||
147 | */ | ||
148 | enum GNUNET_BLOCK_Type type; | ||
149 | |||
150 | }; | ||
151 | |||
152 | |||
153 | /** | ||
154 | * Forward the result to the given peer if it matches the request. | ||
155 | * | ||
156 | * @param cls the `struct ProcessContext` with the result | ||
157 | * @param key the query | ||
158 | * @param value the `struct RecentRequest` with the request | ||
159 | * @return #GNUNET_OK (continue to iterate), | ||
160 | * #GNUNET_SYSERR if the result is malformed or type unsupported | ||
161 | */ | ||
162 | static int | ||
163 | process (void *cls, const struct GNUNET_HashCode * key, void *value) | ||
164 | { | ||
165 | struct ProcessContext *pc = cls; | ||
166 | struct RecentRequest *rr = value; | ||
167 | enum GNUNET_BLOCK_EvaluationResult eval; | ||
168 | unsigned int gpl; | ||
169 | unsigned int ppl; | ||
170 | struct GNUNET_HashCode hc; | ||
171 | const struct GNUNET_HashCode *eval_key; | ||
172 | |||
173 | if ((rr->type != GNUNET_BLOCK_TYPE_ANY) && (rr->type != pc->type)) | ||
174 | return GNUNET_OK; /* type missmatch */ | ||
175 | |||
176 | if (0 != (rr->options & GNUNET_DHT_RO_RECORD_ROUTE)) | ||
177 | { | ||
178 | gpl = pc->get_path_length; | ||
179 | ppl = pc->put_path_length; | ||
180 | } | ||
181 | else | ||
182 | { | ||
183 | gpl = 0; | ||
184 | ppl = 0; | ||
185 | } | ||
186 | if ((0 != (rr->options & GNUNET_DHT_RO_FIND_PEER)) && | ||
187 | (pc->type == GNUNET_BLOCK_TYPE_DHT_HELLO)) | ||
188 | { | ||
189 | /* key may not match HELLO, which is OK since | ||
190 | * the search is approximate. Still, the evaluation | ||
191 | * would fail since the match is not exact. So | ||
192 | * we fake it by changing the key to the actual PID ... */ | ||
193 | GNUNET_BLOCK_get_key (GDS_block_context, GNUNET_BLOCK_TYPE_DHT_HELLO, | ||
194 | pc->data, pc->data_size, &hc); | ||
195 | eval_key = &hc; | ||
196 | } | ||
197 | else | ||
198 | { | ||
199 | eval_key = key; | ||
200 | } | ||
201 | eval = | ||
202 | GNUNET_BLOCK_evaluate (GDS_block_context, pc->type, eval_key, | ||
203 | &rr->reply_bf, rr->reply_bf_mutator, rr->xquery, | ||
204 | rr->xquery_size, pc->data, pc->data_size); | ||
205 | switch (eval) | ||
206 | { | ||
207 | case GNUNET_BLOCK_EVALUATION_OK_MORE: | ||
208 | case GNUNET_BLOCK_EVALUATION_OK_LAST: | ||
209 | GNUNET_STATISTICS_update (GDS_stats, | ||
210 | gettext_noop | ||
211 | ("# Good REPLIES matched against routing table"), | ||
212 | 1, GNUNET_NO); | ||
213 | GDS_NEIGHBOURS_handle_reply (&rr->peer, pc->type, pc->expiration_time, key, | ||
214 | ppl, pc->put_path, gpl, pc->get_path, pc->data, | ||
215 | pc->data_size); | ||
216 | break; | ||
217 | case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE: | ||
218 | GNUNET_STATISTICS_update (GDS_stats, | ||
219 | gettext_noop | ||
220 | ("# Duplicate REPLIES matched against routing table"), | ||
221 | 1, GNUNET_NO); | ||
222 | return GNUNET_OK; | ||
223 | case GNUNET_BLOCK_EVALUATION_RESULT_INVALID: | ||
224 | GNUNET_STATISTICS_update (GDS_stats, | ||
225 | gettext_noop | ||
226 | ("# Invalid REPLIES matched against routing table"), | ||
227 | 1, GNUNET_NO); | ||
228 | return GNUNET_SYSERR; | ||
229 | case GNUNET_BLOCK_EVALUATION_RESULT_IRRELEVANT: | ||
230 | GNUNET_STATISTICS_update (GDS_stats, | ||
231 | gettext_noop | ||
232 | ("# Irrelevant REPLIES matched against routing table"), | ||
233 | 1, GNUNET_NO); | ||
234 | return GNUNET_OK; | ||
235 | case GNUNET_BLOCK_EVALUATION_REQUEST_VALID: | ||
236 | GNUNET_break (0); | ||
237 | return GNUNET_OK; | ||
238 | case GNUNET_BLOCK_EVALUATION_REQUEST_INVALID: | ||
239 | GNUNET_break (0); | ||
240 | return GNUNET_OK; | ||
241 | case GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED: | ||
242 | GNUNET_STATISTICS_update (GDS_stats, | ||
243 | gettext_noop | ||
244 | ("# Unsupported REPLIES matched against routing table"), | ||
245 | 1, GNUNET_NO); | ||
246 | return GNUNET_SYSERR; | ||
247 | default: | ||
248 | GNUNET_break (0); | ||
249 | return GNUNET_SYSERR; | ||
250 | } | ||
251 | return GNUNET_OK; | ||
252 | } | ||
253 | |||
254 | |||
255 | /** | ||
256 | * Handle a reply (route to origin). Only forwards the reply back to | ||
257 | * other peers waiting for it. Does not do local caching or | ||
258 | * forwarding to local clients. Essentially calls | ||
259 | * GDS_NEIGHBOURS_handle_reply for all peers that sent us a matching | ||
260 | * request recently. | ||
261 | * | ||
262 | * @param type type of the block | ||
263 | * @param expiration_time when does the content expire | ||
264 | * @param key key for the content | ||
265 | * @param put_path_length number of entries in put_path | ||
266 | * @param put_path peers the original PUT traversed (if tracked) | ||
267 | * @param get_path_length number of entries in get_path | ||
268 | * @param get_path peers this reply has traversed so far (if tracked) | ||
269 | * @param data payload of the reply | ||
270 | * @param data_size number of bytes in data | ||
271 | */ | ||
272 | void | ||
273 | GDS_ROUTING_process (enum GNUNET_BLOCK_Type type, | ||
274 | struct GNUNET_TIME_Absolute expiration_time, | ||
275 | const struct GNUNET_HashCode * key, unsigned int put_path_length, | ||
276 | const struct GNUNET_PeerIdentity *put_path, | ||
277 | unsigned int get_path_length, | ||
278 | const struct GNUNET_PeerIdentity *get_path, | ||
279 | const void *data, size_t data_size) | ||
280 | { | ||
281 | struct ProcessContext pc; | ||
282 | |||
283 | pc.type = type; | ||
284 | pc.expiration_time = expiration_time; | ||
285 | pc.put_path_length = put_path_length; | ||
286 | pc.put_path = put_path; | ||
287 | pc.get_path_length = get_path_length; | ||
288 | pc.get_path = get_path; | ||
289 | pc.data = data; | ||
290 | pc.data_size = data_size; | ||
291 | if (NULL == data) | ||
292 | { | ||
293 | /* Some apps might have an 'empty' reply as a valid reply; however, | ||
294 | 'process' will call GNUNET_BLOCK_evaluate' which treats a 'NULL' | ||
295 | reply as request-validation (but we need response-validation). | ||
296 | So we set 'data' to a 0-byte non-NULL value just to be sure */ | ||
297 | GNUNET_break (0 == data_size); | ||
298 | pc.data_size = 0; | ||
299 | pc.data = ""; /* something not null */ | ||
300 | } | ||
301 | GNUNET_CONTAINER_multihashmap_get_multiple (recent_map, key, &process, &pc); | ||
302 | } | ||
303 | |||
304 | |||
305 | /** | ||
306 | * Remove the oldest entry from the DHT routing table. Must only | ||
307 | * be called if it is known that there is at least one entry | ||
308 | * in the heap and hashmap. | ||
309 | */ | ||
310 | static void | ||
311 | expire_oldest_entry () | ||
312 | { | ||
313 | struct RecentRequest *recent_req; | ||
314 | |||
315 | GNUNET_STATISTICS_update (GDS_stats, | ||
316 | gettext_noop | ||
317 | ("# Entries removed from routing table"), 1, | ||
318 | GNUNET_NO); | ||
319 | recent_req = GNUNET_CONTAINER_heap_peek (recent_heap); | ||
320 | GNUNET_assert (recent_req != NULL); | ||
321 | GNUNET_CONTAINER_heap_remove_node (recent_req->heap_node); | ||
322 | GNUNET_CONTAINER_bloomfilter_free (recent_req->reply_bf); | ||
323 | GNUNET_assert (GNUNET_YES == | ||
324 | GNUNET_CONTAINER_multihashmap_remove (recent_map, | ||
325 | &recent_req->key, | ||
326 | recent_req)); | ||
327 | GNUNET_free (recent_req); | ||
328 | } | ||
329 | |||
330 | |||
331 | /** | ||
332 | * Try to combine multiple recent requests for the same value | ||
333 | * (if they come from the same peer). | ||
334 | * | ||
335 | * @param cls the new 'struct RecentRequest' (to discard upon successful combination) | ||
336 | * @param key the query | ||
337 | * @param value the existing 'struct RecentRequest' (to update upon successful combination) | ||
338 | * @return GNUNET_OK (continue to iterate), | ||
339 | * GNUNET_SYSERR if the request was successfully combined | ||
340 | */ | ||
341 | static int | ||
342 | try_combine_recent (void *cls, const struct GNUNET_HashCode * key, void *value) | ||
343 | { | ||
344 | struct RecentRequest *in = cls; | ||
345 | struct RecentRequest *rr = value; | ||
346 | |||
347 | if ( (0 != memcmp (&in->peer, | ||
348 | &rr->peer, | ||
349 | sizeof (struct GNUNET_PeerIdentity))) || | ||
350 | (in->type != rr->type) || | ||
351 | (in->xquery_size != rr->xquery_size) || | ||
352 | (0 != memcmp (in->xquery, | ||
353 | rr->xquery, | ||
354 | in->xquery_size)) ) | ||
355 | return GNUNET_OK; | ||
356 | if (in->reply_bf_mutator != rr->reply_bf_mutator) | ||
357 | { | ||
358 | rr->reply_bf_mutator = in->reply_bf_mutator; | ||
359 | GNUNET_CONTAINER_bloomfilter_free (rr->reply_bf); | ||
360 | rr->reply_bf = in->reply_bf; | ||
361 | } | ||
362 | else | ||
363 | { | ||
364 | GNUNET_CONTAINER_bloomfilter_or2 (rr->reply_bf, | ||
365 | in->reply_bf); | ||
366 | GNUNET_CONTAINER_bloomfilter_free (in->reply_bf); | ||
367 | } | ||
368 | GNUNET_free (in); | ||
369 | return GNUNET_SYSERR; | ||
370 | } | ||
371 | |||
372 | |||
373 | /** | ||
374 | * Add a new entry to our routing table. | ||
375 | * | ||
376 | * @param sender peer that originated the request | ||
377 | * @param type type of the block | ||
378 | * @param options options for processing | ||
379 | * @param key key for the content | ||
380 | * @param xquery extended query | ||
381 | * @param xquery_size number of bytes in @a xquery | ||
382 | * @param reply_bf bloomfilter to filter duplicates | ||
383 | * @param reply_bf_mutator mutator for @a reply_bf | ||
384 | */ | ||
385 | void | ||
386 | GDS_ROUTING_add (const struct GNUNET_PeerIdentity *sender, | ||
387 | enum GNUNET_BLOCK_Type type, | ||
388 | enum GNUNET_DHT_RouteOption options, | ||
389 | const struct GNUNET_HashCode * key, const void *xquery, | ||
390 | size_t xquery_size, | ||
391 | const struct GNUNET_CONTAINER_BloomFilter *reply_bf, | ||
392 | uint32_t reply_bf_mutator) | ||
393 | { | ||
394 | struct RecentRequest *recent_req; | ||
395 | |||
396 | while (GNUNET_CONTAINER_heap_get_size (recent_heap) >= DHT_MAX_RECENT) | ||
397 | expire_oldest_entry (); | ||
398 | GNUNET_STATISTICS_update (GDS_stats, | ||
399 | gettext_noop ("# Entries added to routing table"), | ||
400 | 1, GNUNET_NO); | ||
401 | recent_req = GNUNET_malloc (sizeof (struct RecentRequest) + xquery_size); | ||
402 | recent_req->peer = *sender; | ||
403 | recent_req->key = *key; | ||
404 | recent_req->reply_bf = GNUNET_CONTAINER_bloomfilter_copy (reply_bf); | ||
405 | recent_req->type = type; | ||
406 | recent_req->options = options; | ||
407 | recent_req->xquery = &recent_req[1]; | ||
408 | memcpy (&recent_req[1], xquery, xquery_size); | ||
409 | recent_req->xquery_size = xquery_size; | ||
410 | recent_req->reply_bf_mutator = reply_bf_mutator; | ||
411 | if (GNUNET_SYSERR == | ||
412 | GNUNET_CONTAINER_multihashmap_get_multiple (recent_map, key, | ||
413 | &try_combine_recent, recent_req)) | ||
414 | { | ||
415 | GNUNET_STATISTICS_update (GDS_stats, | ||
416 | gettext_noop | ||
417 | ("# DHT requests combined"), | ||
418 | 1, GNUNET_NO); | ||
419 | return; | ||
420 | } | ||
421 | recent_req->heap_node = | ||
422 | GNUNET_CONTAINER_heap_insert (recent_heap, recent_req, | ||
423 | GNUNET_TIME_absolute_get ().abs_value_us); | ||
424 | GNUNET_CONTAINER_multihashmap_put (recent_map, key, recent_req, | ||
425 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); | ||
426 | |||
427 | |||
428 | } | ||
429 | |||
430 | |||
431 | /** | ||
432 | * Initialize routing subsystem. | ||
433 | */ | ||
434 | void | ||
435 | GDS_ROUTING_init () | ||
436 | { | ||
437 | recent_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN); | ||
438 | recent_map = GNUNET_CONTAINER_multihashmap_create (DHT_MAX_RECENT * 4 / 3, GNUNET_NO); | ||
439 | } | ||
440 | |||
441 | |||
442 | /** | ||
443 | * Shutdown routing subsystem. | ||
444 | */ | ||
445 | void | ||
446 | GDS_ROUTING_done () | ||
447 | { | ||
448 | while (GNUNET_CONTAINER_heap_get_size (recent_heap) > 0) | ||
449 | expire_oldest_entry (); | ||
450 | GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (recent_heap)); | ||
451 | GNUNET_CONTAINER_heap_destroy (recent_heap); | ||
452 | recent_heap = NULL; | ||
453 | GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (recent_map)); | ||
454 | GNUNET_CONTAINER_multihashmap_destroy (recent_map); | ||
455 | recent_map = NULL; | ||
456 | } | ||
457 | |||
458 | /* end of gnunet-service-dht_routing.c */ | ||
diff --git a/src/dht/gnunet-service-xdht_routing.h b/src/dht/gnunet-service-xdht_routing.h new file mode 100644 index 000000000..6151edcc3 --- /dev/null +++ b/src/dht/gnunet-service-xdht_routing.h | |||
@@ -0,0 +1,96 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file dht/gnunet-service-xdht_routing.h | ||
23 | * @brief GNUnet DHT tracking of requests for routing replies | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #ifndef GNUNET_SERVICE_DHT_ROUTING_H | ||
27 | #define GNUNET_SERVICE_DHT_ROUTING_H | ||
28 | |||
29 | #include "gnunet_util_lib.h" | ||
30 | #include "gnunet_block_lib.h" | ||
31 | #include "gnunet_dht_service.h" | ||
32 | |||
33 | |||
34 | /** | ||
35 | * Handle a reply (route to origin). Only forwards the reply back to | ||
36 | * other peers waiting for it. Does not do local caching or | ||
37 | * forwarding to local clients. Essentially calls | ||
38 | * GDS_NEIGHBOURS_handle_reply for all peers that sent us a matching | ||
39 | * request recently. | ||
40 | * | ||
41 | * @param type type of the block | ||
42 | * @param expiration_time when does the content expire | ||
43 | * @param key key for the content | ||
44 | * @param put_path_length number of entries in @a put_path | ||
45 | * @param put_path peers the original PUT traversed (if tracked) | ||
46 | * @param get_path_length number of entries in @a get_path | ||
47 | * @param get_path peers this reply has traversed so far (if tracked) | ||
48 | * @param data payload of the reply | ||
49 | * @param data_size number of bytes in @a data | ||
50 | */ | ||
51 | void | ||
52 | GDS_ROUTING_process (enum GNUNET_BLOCK_Type type, | ||
53 | struct GNUNET_TIME_Absolute expiration_time, | ||
54 | const struct GNUNET_HashCode * key, unsigned int put_path_length, | ||
55 | const struct GNUNET_PeerIdentity *put_path, | ||
56 | unsigned int get_path_length, | ||
57 | const struct GNUNET_PeerIdentity *get_path, | ||
58 | const void *data, size_t data_size); | ||
59 | |||
60 | |||
61 | /** | ||
62 | * Add a new entry to our routing table. | ||
63 | * | ||
64 | * @param sender peer that originated the request | ||
65 | * @param type type of the block | ||
66 | * @param options options for processing | ||
67 | * @param key key for the content | ||
68 | * @param xquery extended query | ||
69 | * @param xquery_size number of bytes in @a xquery | ||
70 | * @param reply_bf bloomfilter to filter duplicates | ||
71 | * @param reply_bf_mutator mutator for @a reply_bf | ||
72 | */ | ||
73 | void | ||
74 | GDS_ROUTING_add (const struct GNUNET_PeerIdentity *sender, | ||
75 | enum GNUNET_BLOCK_Type type, | ||
76 | enum GNUNET_DHT_RouteOption options, | ||
77 | const struct GNUNET_HashCode * key, const void *xquery, | ||
78 | size_t xquery_size, | ||
79 | const struct GNUNET_CONTAINER_BloomFilter *reply_bf, | ||
80 | uint32_t reply_bf_mutator); | ||
81 | |||
82 | |||
83 | /** | ||
84 | * Initialize routing subsystem. | ||
85 | */ | ||
86 | void | ||
87 | GDS_ROUTING_init (void); | ||
88 | |||
89 | |||
90 | /** | ||
91 | * Shutdown routing subsystem. | ||
92 | */ | ||
93 | void | ||
94 | GDS_ROUTING_done (void); | ||
95 | |||
96 | #endif | ||