diff options
Diffstat (limited to 'src/mesh/mesh_api.c')
-rw-r--r-- | src/mesh/mesh_api.c | 718 |
1 files changed, 379 insertions, 339 deletions
diff --git a/src/mesh/mesh_api.c b/src/mesh/mesh_api.c index 24ee4c5ae..1ccd31dfe 100644 --- a/src/mesh/mesh_api.c +++ b/src/mesh/mesh_api.c | |||
@@ -49,7 +49,7 @@ struct tunnel_message | |||
49 | 49 | ||
50 | struct notify_cls | 50 | struct notify_cls |
51 | { | 51 | { |
52 | void* notify_cls; | 52 | void *notify_cls; |
53 | GNUNET_CONNECTION_TransmitReadyNotify notify; | 53 | GNUNET_CONNECTION_TransmitReadyNotify notify; |
54 | struct GNUNET_MESH_Tunnel *tunnel; | 54 | struct GNUNET_MESH_Tunnel *tunnel; |
55 | }; | 55 | }; |
@@ -66,24 +66,24 @@ struct GNUNET_MESH_Tunnel | |||
66 | GNUNET_MESH_TunnelConnectHandler connect_handler; | 66 | GNUNET_MESH_TunnelConnectHandler connect_handler; |
67 | void *handler_cls; | 67 | void *handler_cls; |
68 | 68 | ||
69 | struct GNUNET_MESH_Handle* handle; | 69 | struct GNUNET_MESH_Handle *handle; |
70 | 70 | ||
71 | /* The application-type requested for this tunnel. Is only needed for pending | 71 | /* The application-type requested for this tunnel. Is only needed for pending |
72 | * by_tupe-tunnels | 72 | * by_tupe-tunnels |
73 | */ | 73 | */ |
74 | uint16_t application_type; | 74 | uint16_t application_type; |
75 | 75 | ||
76 | struct GNUNET_MESH_TransmitHandle* notify_handle; | 76 | struct GNUNET_MESH_TransmitHandle *notify_handle; |
77 | 77 | ||
78 | /* The context of the receive-function. */ | 78 | /* The context of the receive-function. */ |
79 | void *ctx; | 79 | void *ctx; |
80 | 80 | ||
81 | /* A list, usable by application-code (for queues) */ | 81 | /* A list, usable by application-code (for queues) */ |
82 | void* app_head; | 82 | void *app_head; |
83 | void* app_tail; | 83 | void *app_tail; |
84 | 84 | ||
85 | /* A pointer, usable by application-code */ | 85 | /* A pointer, usable by application-code */ |
86 | void* app_data; | 86 | void *app_data; |
87 | }; | 87 | }; |
88 | 88 | ||
89 | struct tunnel_list_element | 89 | struct tunnel_list_element |
@@ -136,51 +136,49 @@ struct GNUNET_MESH_Handle | |||
136 | }; | 136 | }; |
137 | 137 | ||
138 | static void | 138 | static void |
139 | send_end_connect(void* cls, | 139 | send_end_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
140 | const struct GNUNET_SCHEDULER_TaskContext* tc) | ||
141 | { | 140 | { |
142 | if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) | 141 | if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) |
143 | return; | 142 | return; |
144 | 143 | ||
145 | struct GNUNET_MESH_Tunnel* tunnel = cls; | 144 | struct GNUNET_MESH_Tunnel *tunnel = cls; |
146 | 145 | ||
147 | tunnel->connect_handler(tunnel->handler_cls, NULL, NULL); | 146 | tunnel->connect_handler (tunnel->handler_cls, NULL, NULL); |
148 | } | 147 | } |
149 | 148 | ||
150 | static void | 149 | static void |
151 | send_self_connect(void* cls, | 150 | send_self_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
152 | const struct GNUNET_SCHEDULER_TaskContext* tc) | ||
153 | { | 151 | { |
154 | if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) | 152 | if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) |
155 | return; | 153 | return; |
156 | 154 | ||
157 | struct GNUNET_MESH_Tunnel* tunnel = cls; | 155 | struct GNUNET_MESH_Tunnel *tunnel = cls; |
158 | 156 | ||
159 | tunnel->connect_handler(tunnel->handler_cls, &tunnel->handle->myself, NULL); | 157 | tunnel->connect_handler (tunnel->handler_cls, &tunnel->handle->myself, NULL); |
160 | GNUNET_SCHEDULER_add_now(send_end_connect, tunnel); | 158 | GNUNET_SCHEDULER_add_now (send_end_connect, tunnel); |
161 | } | 159 | } |
162 | 160 | ||
163 | static void | 161 | static void |
164 | call_connect_handler (void *cls, | 162 | call_connect_handler (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
165 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
166 | { | 163 | { |
167 | if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) | 164 | if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) |
168 | return; | 165 | return; |
169 | 166 | ||
170 | struct GNUNET_MESH_Tunnel *tunnel = cls; | 167 | struct GNUNET_MESH_Tunnel *tunnel = cls; |
171 | 168 | ||
172 | tunnel->connect_handler (tunnel->handler_cls, &tunnel->peer, | 169 | tunnel->connect_handler (tunnel->handler_cls, &tunnel->peer, NULL); |
173 | NULL); | ||
174 | GNUNET_SCHEDULER_add_now (send_end_connect, tunnel); | 170 | GNUNET_SCHEDULER_add_now (send_end_connect, tunnel); |
175 | } | 171 | } |
176 | 172 | ||
177 | static void | 173 | static void |
178 | core_startup (void *cls, | 174 | core_startup (void *cls, |
179 | struct GNUNET_CORE_Handle *core __attribute__((unused)), | 175 | struct GNUNET_CORE_Handle *core __attribute__ ((unused)), |
180 | const struct GNUNET_PeerIdentity *my_identity, | 176 | const struct GNUNET_PeerIdentity *my_identity, |
181 | const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey __attribute__((unused))) | 177 | const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey |
178 | __attribute__ ((unused))) | ||
182 | { | 179 | { |
183 | struct GNUNET_MESH_Handle *handle = cls; | 180 | struct GNUNET_MESH_Handle *handle = cls; |
181 | |||
184 | memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity)); | 182 | memcpy (&handle->myself, my_identity, sizeof (struct GNUNET_PeerIdentity)); |
185 | handle->connected_to_core = GNUNET_YES; | 183 | handle->connected_to_core = GNUNET_YES; |
186 | } | 184 | } |
@@ -188,21 +186,24 @@ core_startup (void *cls, | |||
188 | static size_t | 186 | static size_t |
189 | send_hello_message (void *cls, size_t size, void *buf) | 187 | send_hello_message (void *cls, size_t size, void *buf) |
190 | { | 188 | { |
191 | if (cls == NULL) return 0; | 189 | if (cls == NULL) |
190 | return 0; | ||
192 | 191 | ||
193 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending hello\n"); | 192 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending hello\n"); |
194 | 193 | ||
195 | struct GNUNET_MESH_Handle *handle = cls; | 194 | struct GNUNET_MESH_Handle *handle = cls; |
196 | struct GNUNET_MessageHeader *hdr = buf; | 195 | struct GNUNET_MessageHeader *hdr = buf; |
197 | 196 | ||
198 | size_t sent = sizeof(struct GNUNET_MessageHeader) + handle->hello_message_size; | 197 | size_t sent = |
198 | sizeof (struct GNUNET_MessageHeader) + handle->hello_message_size; | ||
199 | 199 | ||
200 | if (sent > size) return 0; | 200 | if (sent > size) |
201 | return 0; | ||
201 | 202 | ||
202 | hdr->type = htons(GNUNET_MESSAGE_TYPE_MESH_HELLO); | 203 | hdr->type = htons (GNUNET_MESSAGE_TYPE_MESH_HELLO); |
203 | hdr->size = htons(size); | 204 | hdr->size = htons (size); |
204 | 205 | ||
205 | memcpy(hdr+1, handle->hello_message, handle->hello_message_size); | 206 | memcpy (hdr + 1, handle->hello_message, handle->hello_message_size); |
206 | return sent; | 207 | return sent; |
207 | } | 208 | } |
208 | 209 | ||
@@ -215,26 +216,27 @@ send_hello_message (void *cls, size_t size, void *buf) | |||
215 | */ | 216 | */ |
216 | static void | 217 | static void |
217 | core_connect (void *cls, | 218 | core_connect (void *cls, |
218 | const struct GNUNET_PeerIdentity *peer, | 219 | const struct GNUNET_PeerIdentity *peer, |
219 | const struct GNUNET_TRANSPORT_ATS_Information *atsi) | 220 | const struct GNUNET_TRANSPORT_ATS_Information *atsi) |
220 | { | 221 | { |
221 | struct GNUNET_MESH_Handle *handle = cls; | 222 | struct GNUNET_MESH_Handle *handle = cls; |
222 | 223 | ||
223 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Core tells us we are connected to peer %s\n", GNUNET_i2s(peer)); | 224 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
225 | "Core tells us we are connected to peer %s\n", GNUNET_i2s (peer)); | ||
224 | 226 | ||
225 | /* Send a hello to this peer */ | 227 | /* Send a hello to this peer */ |
226 | GNUNET_CORE_notify_transmit_ready(handle->core, | 228 | GNUNET_CORE_notify_transmit_ready (handle->core, |
227 | GNUNET_NO, | 229 | GNUNET_NO, |
228 | 42, | 230 | 42, |
229 | GNUNET_TIME_UNIT_SECONDS, | 231 | GNUNET_TIME_UNIT_SECONDS, |
230 | peer, | 232 | peer, |
231 | sizeof(struct GNUNET_MessageHeader) + handle->hello_message_size, | 233 | sizeof (struct GNUNET_MessageHeader) + |
232 | &send_hello_message, | 234 | handle->hello_message_size, |
233 | cls); | 235 | &send_hello_message, cls); |
234 | 236 | ||
235 | /* put the new peer into the list of connected peers */ | 237 | /* put the new peer into the list of connected peers */ |
236 | struct peer_list_element *element = | 238 | struct peer_list_element *element = |
237 | GNUNET_malloc (sizeof (struct peer_list_element)); | 239 | GNUNET_malloc (sizeof (struct peer_list_element)); |
238 | memcpy (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)); | 240 | memcpy (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity)); |
239 | 241 | ||
240 | if (NULL != atsi) | 242 | if (NULL != atsi) |
@@ -242,31 +244,32 @@ core_connect (void *cls, | |||
242 | sizeof (struct GNUNET_TRANSPORT_ATS_Information)); | 244 | sizeof (struct GNUNET_TRANSPORT_ATS_Information)); |
243 | 245 | ||
244 | GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head, | 246 | GNUNET_CONTAINER_DLL_insert_after (handle->connected_peers.head, |
245 | handle->connected_peers.tail, | 247 | handle->connected_peers.tail, |
246 | handle->connected_peers.tail, element); | 248 | handle->connected_peers.tail, element); |
247 | 249 | ||
248 | struct tunnel_list_element *tunnel = handle->pending_tunnels.head; | 250 | struct tunnel_list_element *tunnel = handle->pending_tunnels.head; |
251 | |||
249 | while (tunnel != NULL) | 252 | while (tunnel != NULL) |
253 | { | ||
254 | if (0 == | ||
255 | memcmp (&tunnel->tunnel.peer, peer, | ||
256 | sizeof (struct GNUNET_PeerIdentity))) | ||
250 | { | 257 | { |
251 | if (0 == | 258 | struct tunnel_list_element *next = tunnel->next; |
252 | memcmp (&tunnel->tunnel.peer, peer, | 259 | |
253 | sizeof (struct GNUNET_PeerIdentity))) | 260 | GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head, |
254 | { | 261 | handle->pending_tunnels.tail, tunnel); |
255 | struct tunnel_list_element *next = tunnel->next; | 262 | GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head, |
256 | GNUNET_CONTAINER_DLL_remove (handle->pending_tunnels.head, | 263 | handle->established_tunnels.tail, |
257 | handle->pending_tunnels.tail, tunnel); | 264 | handle->established_tunnels.tail, |
258 | GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head, | 265 | tunnel); |
259 | handle->established_tunnels.tail, | 266 | tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls, peer, atsi); |
260 | handle->established_tunnels.tail, | 267 | GNUNET_SCHEDULER_add_now (send_end_connect, tunnel); |
261 | tunnel); | 268 | tunnel = next; |
262 | tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls, | ||
263 | peer, atsi); | ||
264 | GNUNET_SCHEDULER_add_now(send_end_connect, tunnel); | ||
265 | tunnel = next; | ||
266 | } | ||
267 | else | ||
268 | tunnel = tunnel->next; | ||
269 | } | 269 | } |
270 | else | ||
271 | tunnel = tunnel->next; | ||
272 | } | ||
270 | } | 273 | } |
271 | 274 | ||
272 | /** | 275 | /** |
@@ -280,58 +283,63 @@ core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) | |||
280 | { | 283 | { |
281 | struct GNUNET_MESH_Handle *handle = cls; | 284 | struct GNUNET_MESH_Handle *handle = cls; |
282 | 285 | ||
283 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Core tells us we are no longer connected to peer %s\n", GNUNET_i2s(peer)); | 286 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
287 | "Core tells us we are no longer connected to peer %s\n", | ||
288 | GNUNET_i2s (peer)); | ||
284 | 289 | ||
285 | struct peer_list_element *element = handle->connected_peers.head; | 290 | struct peer_list_element *element = handle->connected_peers.head; |
291 | |||
286 | while (element != NULL) | 292 | while (element != NULL) |
287 | { | 293 | { |
288 | if (0 == | 294 | if (0 == memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity))) |
289 | memcmp (&element->peer, peer, sizeof (struct GNUNET_PeerIdentity))) | 295 | break; |
290 | break; | 296 | element = element->next; |
291 | element = element->next; | 297 | } |
292 | } | ||
293 | if (element != NULL) | 298 | if (element != NULL) |
299 | { | ||
300 | GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head, | ||
301 | handle->connected_peers.tail, element); | ||
302 | while (element->type_head != NULL) | ||
294 | { | 303 | { |
295 | GNUNET_CONTAINER_DLL_remove (handle->connected_peers.head, | 304 | struct type_list_element *tail = element->type_tail; |
296 | handle->connected_peers.tail, element); | 305 | |
297 | while (element->type_head != NULL) | 306 | GNUNET_CONTAINER_DLL_remove (element->type_head, element->type_tail, |
298 | { | 307 | tail); |
299 | struct type_list_element* tail = element->type_tail; | 308 | GNUNET_free (tail); |
300 | GNUNET_CONTAINER_DLL_remove(element->type_head, element->type_tail, tail); | ||
301 | GNUNET_free(tail); | ||
302 | } | ||
303 | GNUNET_free (element); | ||
304 | } | 309 | } |
310 | GNUNET_free (element); | ||
311 | } | ||
305 | 312 | ||
306 | struct tunnel_list_element *telement = handle->established_tunnels.head; | 313 | struct tunnel_list_element *telement = handle->established_tunnels.head; |
314 | |||
307 | while (telement != NULL) | 315 | while (telement != NULL) |
316 | { | ||
317 | if (0 == | ||
318 | memcmp (&telement->tunnel.peer, peer, | ||
319 | sizeof (struct GNUNET_PeerIdentity))) | ||
320 | { | ||
321 | /* disconnect tunnels */ | ||
322 | /* outbound tunnels */ | ||
323 | if (telement->tunnel.connect_handler != NULL && | ||
324 | NULL != telement->tunnel.disconnect_handler) | ||
325 | telement->tunnel.disconnect_handler (telement->tunnel.handler_cls, | ||
326 | peer); | ||
327 | /* inbound tunnels */ | ||
328 | else if (NULL != handle->cleaner) | ||
329 | handle->cleaner (handle->cls, &telement->tunnel, &telement->tunnel.ctx); | ||
330 | |||
331 | struct tunnel_list_element *next = telement->next; | ||
332 | |||
333 | GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head, | ||
334 | handle->established_tunnels.tail, telement); | ||
335 | GNUNET_free (telement); | ||
336 | telement = next; | ||
337 | } | ||
338 | else | ||
308 | { | 339 | { |
309 | if (0 == | 340 | telement = telement->next; |
310 | memcmp (&telement->tunnel.peer, peer, | ||
311 | sizeof (struct GNUNET_PeerIdentity))) | ||
312 | { | ||
313 | /* disconnect tunnels */ | ||
314 | /* outbound tunnels */ | ||
315 | if (telement->tunnel.connect_handler != NULL && NULL != telement->tunnel.disconnect_handler) | ||
316 | telement->tunnel.disconnect_handler (telement->tunnel.handler_cls, | ||
317 | peer); | ||
318 | /* inbound tunnels */ | ||
319 | else if (NULL != handle->cleaner) | ||
320 | handle->cleaner (handle->cls, &telement->tunnel, | ||
321 | &telement->tunnel.ctx); | ||
322 | |||
323 | struct tunnel_list_element *next = telement->next; | ||
324 | GNUNET_CONTAINER_DLL_remove (handle->established_tunnels.head, | ||
325 | handle->established_tunnels.tail, | ||
326 | telement); | ||
327 | GNUNET_free (telement); | ||
328 | telement = next; | ||
329 | } | ||
330 | else | ||
331 | { | ||
332 | telement = telement->next; | ||
333 | } | ||
334 | } | 341 | } |
342 | } | ||
335 | } | 343 | } |
336 | 344 | ||
337 | /** | 345 | /** |
@@ -346,66 +354,79 @@ receive_hello (void *cls, | |||
346 | { | 354 | { |
347 | struct GNUNET_MESH_Handle *handle = cls; | 355 | struct GNUNET_MESH_Handle *handle = cls; |
348 | uint16_t *num = (uint16_t *) (message + 1); | 356 | uint16_t *num = (uint16_t *) (message + 1); |
349 | GNUNET_MESH_ApplicationType *ports = (GNUNET_MESH_ApplicationType*) (num + 1); | 357 | GNUNET_MESH_ApplicationType *ports = |
358 | (GNUNET_MESH_ApplicationType *) (num + 1); | ||
350 | unsigned int i; | 359 | unsigned int i; |
351 | 360 | ||
352 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "The peer %s tells us he supports %d application-types.\n", GNUNET_i2s(other), ntohs(*num)); | 361 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
362 | "The peer %s tells us he supports %d application-types.\n", | ||
363 | GNUNET_i2s (other), ntohs (*num)); | ||
353 | 364 | ||
354 | struct peer_list_element *element = handle->connected_peers.head; | 365 | struct peer_list_element *element = handle->connected_peers.head; |
355 | while (element != NULL) | ||
356 | { | ||
357 | if (0 == | ||
358 | memcmp (&element->peer, other, sizeof (struct GNUNET_PeerIdentity))) | ||
359 | break; | ||
360 | element = element->next; | ||
361 | } | ||
362 | |||
363 | GNUNET_assert(NULL != element); | ||
364 | 366 | ||
365 | for (i = 0; i < ntohs(*num); i++) | 367 | while (element != NULL) |
366 | { | 368 | { |
367 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "The peer %s newly supports the application-type %d\n", GNUNET_i2s(other), ntohs(ports[i])); | 369 | if (0 == |
368 | if (GNUNET_APPLICATION_TYPE_END == ntohs(ports[i])) continue; | 370 | memcmp (&element->peer, other, sizeof (struct GNUNET_PeerIdentity))) |
369 | struct type_list_element* new_type = GNUNET_malloc(sizeof *new_type); | 371 | break; |
370 | new_type->type = (GNUNET_MESH_ApplicationType)ntohs (ports[i]); | 372 | element = element->next; |
371 | GNUNET_CONTAINER_DLL_insert(element->type_head, element->type_tail, new_type); | 373 | } |
372 | } | 374 | |
375 | GNUNET_assert (NULL != element); | ||
376 | |||
377 | for (i = 0; i < ntohs (*num); i++) | ||
378 | { | ||
379 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
380 | "The peer %s newly supports the application-type %d\n", | ||
381 | GNUNET_i2s (other), ntohs (ports[i])); | ||
382 | if (GNUNET_APPLICATION_TYPE_END == ntohs (ports[i])) | ||
383 | continue; | ||
384 | struct type_list_element *new_type = GNUNET_malloc (sizeof *new_type); | ||
385 | |||
386 | new_type->type = (GNUNET_MESH_ApplicationType) ntohs (ports[i]); | ||
387 | GNUNET_CONTAINER_DLL_insert (element->type_head, element->type_tail, | ||
388 | new_type); | ||
389 | } | ||
373 | 390 | ||
374 | struct type_list_element *type; | 391 | struct type_list_element *type; |
392 | |||
375 | for (type = element->type_head; type != NULL; type = type->next) | 393 | for (type = element->type_head; type != NULL; type = type->next) |
376 | { | 394 | { |
377 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "The peer %s supports the application-type %d\n", GNUNET_i2s(other), type->type); | 395 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
378 | } | 396 | "The peer %s supports the application-type %d\n", |
397 | GNUNET_i2s (other), type->type); | ||
398 | } | ||
379 | 399 | ||
380 | struct tunnel_list_element *tunnel = handle->pending_by_type_tunnels.head; | 400 | struct tunnel_list_element *tunnel = handle->pending_by_type_tunnels.head; |
401 | |||
381 | while (tunnel != NULL) | 402 | while (tunnel != NULL) |
403 | { | ||
404 | struct tunnel_list_element *next = tunnel->next; | ||
405 | |||
406 | for (i = 0; i < ntohs (*num); i++) | ||
382 | { | 407 | { |
383 | struct tunnel_list_element *next = tunnel->next; | ||
384 | for (i = 0; i < ntohs(*num); i++) | ||
385 | { | ||
386 | if (ntohs (ports[i]) == tunnel->tunnel.application_type) | ||
387 | { | ||
388 | GNUNET_CONTAINER_DLL_remove (handle->pending_by_type_tunnels.head, | ||
389 | handle->pending_by_type_tunnels.tail, | ||
390 | tunnel); | ||
391 | GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels. | ||
392 | head, | ||
393 | handle->established_tunnels. | ||
394 | tail, | ||
395 | handle->established_tunnels. | ||
396 | tail, tunnel); | ||
397 | memcpy(&tunnel->tunnel.peer, other, sizeof(struct GNUNET_PeerIdentity)); | ||
398 | tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls, | ||
399 | &tunnel->tunnel.peer, atsi); | ||
400 | GNUNET_SCHEDULER_add_now (send_end_connect, tunnel); | ||
401 | break; | ||
402 | } | ||
403 | } | ||
404 | if (ntohs (ports[i]) == tunnel->tunnel.application_type) | 408 | if (ntohs (ports[i]) == tunnel->tunnel.application_type) |
405 | tunnel = next; | 409 | { |
406 | else | 410 | GNUNET_CONTAINER_DLL_remove (handle->pending_by_type_tunnels.head, |
407 | tunnel = tunnel->next; | 411 | handle->pending_by_type_tunnels.tail, |
412 | tunnel); | ||
413 | GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head, | ||
414 | handle->established_tunnels.tail, | ||
415 | handle->established_tunnels.tail, | ||
416 | tunnel); | ||
417 | memcpy (&tunnel->tunnel.peer, other, | ||
418 | sizeof (struct GNUNET_PeerIdentity)); | ||
419 | tunnel->tunnel.connect_handler (tunnel->tunnel.handler_cls, | ||
420 | &tunnel->tunnel.peer, atsi); | ||
421 | GNUNET_SCHEDULER_add_now (send_end_connect, tunnel); | ||
422 | break; | ||
423 | } | ||
408 | } | 424 | } |
425 | if (ntohs (ports[i]) == tunnel->tunnel.application_type) | ||
426 | tunnel = next; | ||
427 | else | ||
428 | tunnel = tunnel->next; | ||
429 | } | ||
409 | return GNUNET_OK; | 430 | return GNUNET_OK; |
410 | } | 431 | } |
411 | 432 | ||
@@ -414,110 +435,115 @@ receive_hello (void *cls, | |||
414 | */ | 435 | */ |
415 | static int | 436 | static int |
416 | core_receive (void *cls, | 437 | core_receive (void *cls, |
417 | const struct GNUNET_PeerIdentity *other, | 438 | const struct GNUNET_PeerIdentity *other, |
418 | const struct GNUNET_MessageHeader *message, | 439 | const struct GNUNET_MessageHeader *message, |
419 | const struct GNUNET_TRANSPORT_ATS_Information *atsi) | 440 | const struct GNUNET_TRANSPORT_ATS_Information *atsi) |
420 | { | 441 | { |
421 | struct GNUNET_MESH_Handle *handle = cls; | 442 | struct GNUNET_MESH_Handle *handle = cls; |
422 | struct tunnel_message *tmessage = (struct tunnel_message *) message; | 443 | struct tunnel_message *tmessage = (struct tunnel_message *) message; |
423 | struct GNUNET_MessageHeader *rmessage = | 444 | struct GNUNET_MessageHeader *rmessage = |
424 | (struct GNUNET_MessageHeader *) (tmessage + 1); | 445 | (struct GNUNET_MessageHeader *) (tmessage + 1); |
425 | 446 | ||
426 | struct GNUNET_MESH_MessageHandler *handler; | 447 | struct GNUNET_MESH_MessageHandler *handler; |
427 | 448 | ||
428 | for (handler = handle->handlers; handler->callback != NULL; handler++) | 449 | for (handler = handle->handlers; handler->callback != NULL; handler++) |
450 | { | ||
451 | if ((ntohs (rmessage->type) == handler->type) | ||
452 | && ((handler->expected_size == 0) | ||
453 | || (handler->expected_size == ntohs (rmessage->size)))) | ||
429 | { | 454 | { |
430 | if ( (ntohs (rmessage->type) == handler->type) | 455 | break; |
431 | && ( (handler->expected_size == 0) | ||
432 | || (handler->expected_size == ntohs (rmessage->size))) ) | ||
433 | { | ||
434 | break; | ||
435 | } | ||
436 | } | 456 | } |
457 | } | ||
437 | 458 | ||
438 | /* handler->callback handles this message */ | 459 | /* handler->callback handles this message */ |
439 | 460 | ||
440 | /* If no handler was found, drop the message but keep the channel open */ | 461 | /* If no handler was found, drop the message but keep the channel open */ |
441 | if (handler->callback == NULL) | 462 | if (handler->callback == NULL) |
442 | { | 463 | { |
443 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer %s; dropping it.\n", | 464 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
444 | ntohs(rmessage->type), GNUNET_i2s(other)); | 465 | "Received message of type %d from peer %s; dropping it.\n", |
445 | return GNUNET_OK; | 466 | ntohs (rmessage->type), GNUNET_i2s (other)); |
446 | } | 467 | return GNUNET_OK; |
468 | } | ||
447 | 469 | ||
448 | struct tunnel_list_element *tunnel = handle->established_tunnels.head; | 470 | struct tunnel_list_element *tunnel = handle->established_tunnels.head; |
449 | 471 | ||
450 | while (tunnel != NULL) | 472 | while (tunnel != NULL) |
451 | { | 473 | { |
452 | if (tunnel->tunnel.id.id == tmessage->id.id && | 474 | if (tunnel->tunnel.id.id == tmessage->id.id && |
453 | (0 == | 475 | (0 == |
454 | memcmp (&tmessage->id.initiator, &tunnel->tunnel.id.initiator, | 476 | memcmp (&tmessage->id.initiator, &tunnel->tunnel.id.initiator, |
455 | sizeof (struct GNUNET_PeerIdentity))) | 477 | sizeof (struct GNUNET_PeerIdentity))) |
456 | && (0 == | 478 | && (0 == |
457 | memcmp (&tmessage->id.target, &tunnel->tunnel.id.target, | 479 | memcmp (&tmessage->id.target, &tunnel->tunnel.id.target, |
458 | sizeof (struct GNUNET_PeerIdentity)))) | 480 | sizeof (struct GNUNET_PeerIdentity)))) |
459 | break; | 481 | break; |
460 | tunnel = tunnel->next; | 482 | tunnel = tunnel->next; |
461 | } | 483 | } |
462 | 484 | ||
463 | /* if no tunnel was found: create a new inbound tunnel */ | 485 | /* if no tunnel was found: create a new inbound tunnel */ |
464 | if (tunnel == NULL) | 486 | if (tunnel == NULL) |
465 | { | 487 | { |
466 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "New inbound tunnel from peer %s; first message has type %d.\n", | 488 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
467 | GNUNET_i2s(other), ntohs(rmessage->type)); | 489 | "New inbound tunnel from peer %s; first message has type %d.\n", |
468 | tunnel = GNUNET_malloc (sizeof (struct tunnel_list_element)); | 490 | GNUNET_i2s (other), ntohs (rmessage->type)); |
469 | tunnel->tunnel.connect_handler = NULL; | 491 | tunnel = GNUNET_malloc (sizeof (struct tunnel_list_element)); |
470 | tunnel->tunnel.disconnect_handler = NULL; | 492 | tunnel->tunnel.connect_handler = NULL; |
471 | tunnel->tunnel.handler_cls = NULL; | 493 | tunnel->tunnel.disconnect_handler = NULL; |
472 | tunnel->tunnel.ctx = NULL; | 494 | tunnel->tunnel.handler_cls = NULL; |
473 | tunnel->tunnel.handle = handle; | 495 | tunnel->tunnel.ctx = NULL; |
474 | memcpy (&tunnel->tunnel.peer, other, | 496 | tunnel->tunnel.handle = handle; |
475 | sizeof (struct GNUNET_PeerIdentity)); | 497 | memcpy (&tunnel->tunnel.peer, other, sizeof (struct GNUNET_PeerIdentity)); |
476 | memcpy (&tunnel->tunnel.id, &tmessage->id, sizeof (struct tunnel_id)); | 498 | memcpy (&tunnel->tunnel.id, &tmessage->id, sizeof (struct tunnel_id)); |
477 | 499 | ||
478 | GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head, | 500 | GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head, |
479 | handle->established_tunnels.tail, | 501 | handle->established_tunnels.tail, |
480 | handle->established_tunnels.tail, | 502 | handle->established_tunnels.tail, |
481 | tunnel); | 503 | tunnel); |
482 | } | 504 | } |
483 | else | 505 | else |
484 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Inbound message from peer %s; type %d.\n", | 506 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
485 | GNUNET_i2s(other), ntohs(rmessage->type)); | 507 | "Inbound message from peer %s; type %d.\n", GNUNET_i2s (other), |
508 | ntohs (rmessage->type)); | ||
486 | 509 | ||
487 | return handler->callback (handle->cls, &tunnel->tunnel, | 510 | return handler->callback (handle->cls, &tunnel->tunnel, |
488 | &tunnel->tunnel.ctx, other, rmessage, atsi); | 511 | &tunnel->tunnel.ctx, other, rmessage, atsi); |
489 | } | 512 | } |
490 | 513 | ||
491 | struct GNUNET_MESH_Tunnel * | 514 | struct GNUNET_MESH_Tunnel * |
492 | GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *handle, | 515 | GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *handle, |
493 | struct GNUNET_TIME_Relative timeout, | 516 | struct GNUNET_TIME_Relative timeout, |
494 | GNUNET_MESH_ApplicationType application_type, | 517 | GNUNET_MESH_ApplicationType |
518 | application_type, | ||
495 | GNUNET_MESH_TunnelConnectHandler | 519 | GNUNET_MESH_TunnelConnectHandler |
496 | connect_handler, | 520 | connect_handler, |
497 | GNUNET_MESH_TunnelDisconnectHandler | 521 | GNUNET_MESH_TunnelDisconnectHandler |
498 | disconnect_handler, | 522 | disconnect_handler, void *handler_cls) |
499 | void *handler_cls) | ||
500 | { | 523 | { |
501 | /* Look in the list of connected peers */ | 524 | /* Look in the list of connected peers */ |
502 | struct peer_list_element *element = handle->connected_peers.head; | 525 | struct peer_list_element *element = handle->connected_peers.head; |
503 | while (element != NULL) | ||
504 | { | ||
505 | struct type_list_element* i; | ||
506 | for (i = element->type_head; i != NULL; i = i->next) | ||
507 | if (application_type == i->type) | ||
508 | return GNUNET_MESH_peer_request_connect_all (handle, timeout, 1, | ||
509 | &element->peer, | ||
510 | connect_handler, | ||
511 | disconnect_handler, | ||
512 | handler_cls); | ||
513 | element = element->next; | ||
514 | } | ||
515 | 526 | ||
516 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Trying to connect by tupe %d.\n", application_type); | 527 | while (element != NULL) |
528 | { | ||
529 | struct type_list_element *i; | ||
530 | |||
531 | for (i = element->type_head; i != NULL; i = i->next) | ||
532 | if (application_type == i->type) | ||
533 | return GNUNET_MESH_peer_request_connect_all (handle, timeout, 1, | ||
534 | &element->peer, | ||
535 | connect_handler, | ||
536 | disconnect_handler, | ||
537 | handler_cls); | ||
538 | element = element->next; | ||
539 | } | ||
540 | |||
541 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect by tupe %d.\n", | ||
542 | application_type); | ||
517 | 543 | ||
518 | /* Put into pending list */ | 544 | /* Put into pending list */ |
519 | struct tunnel_list_element *tunnel = | 545 | struct tunnel_list_element *tunnel = |
520 | GNUNET_malloc (sizeof (struct tunnel_list_element)); | 546 | GNUNET_malloc (sizeof (struct tunnel_list_element)); |
521 | 547 | ||
522 | tunnel->tunnel.connect_handler = connect_handler; | 548 | tunnel->tunnel.connect_handler = connect_handler; |
523 | tunnel->tunnel.disconnect_handler = disconnect_handler; | 549 | tunnel->tunnel.disconnect_handler = disconnect_handler; |
@@ -540,19 +566,19 @@ GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Handle *handle, | |||
540 | 566 | ||
541 | struct GNUNET_MESH_Tunnel * | 567 | struct GNUNET_MESH_Tunnel * |
542 | GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle, | 568 | GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle, |
543 | struct GNUNET_TIME_Relative timeout, | 569 | struct GNUNET_TIME_Relative timeout, |
544 | unsigned int num_peers, | 570 | unsigned int num_peers, |
545 | const struct GNUNET_PeerIdentity *peers, | 571 | const struct GNUNET_PeerIdentity *peers, |
546 | GNUNET_MESH_TunnelConnectHandler | 572 | GNUNET_MESH_TunnelConnectHandler |
547 | connect_handler, | 573 | connect_handler, |
548 | GNUNET_MESH_TunnelDisconnectHandler | 574 | GNUNET_MESH_TunnelDisconnectHandler |
549 | disconnect_handler, void *handler_cls) | 575 | disconnect_handler, void *handler_cls) |
550 | { | 576 | { |
551 | if (num_peers != 1) | 577 | if (num_peers != 1) |
552 | return NULL; | 578 | return NULL; |
553 | 579 | ||
554 | struct tunnel_list_element *tunnel = | 580 | struct tunnel_list_element *tunnel = |
555 | GNUNET_malloc (sizeof (struct tunnel_list_element)); | 581 | GNUNET_malloc (sizeof (struct tunnel_list_element)); |
556 | 582 | ||
557 | tunnel->tunnel.connect_handler = connect_handler; | 583 | tunnel->tunnel.connect_handler = connect_handler; |
558 | tunnel->tunnel.disconnect_handler = disconnect_handler; | 584 | tunnel->tunnel.disconnect_handler = disconnect_handler; |
@@ -560,83 +586,85 @@ GNUNET_MESH_peer_request_connect_all (struct GNUNET_MESH_Handle *handle, | |||
560 | tunnel->tunnel.ctx = NULL; | 586 | tunnel->tunnel.ctx = NULL; |
561 | tunnel->tunnel.handle = handle; | 587 | tunnel->tunnel.handle = handle; |
562 | memcpy (&tunnel->tunnel.id.initiator, &handle->myself, | 588 | memcpy (&tunnel->tunnel.id.initiator, &handle->myself, |
563 | sizeof (struct GNUNET_PeerIdentity)); | 589 | sizeof (struct GNUNET_PeerIdentity)); |
564 | memcpy (&tunnel->tunnel.id.target, peers, | 590 | memcpy (&tunnel->tunnel.id.target, peers, |
565 | sizeof (struct GNUNET_PeerIdentity)); | 591 | sizeof (struct GNUNET_PeerIdentity)); |
566 | tunnel->tunnel.id.id = current_id++; | 592 | tunnel->tunnel.id.id = current_id++; |
567 | memcpy (&tunnel->tunnel.peer, peers, sizeof(struct GNUNET_PeerIdentity)); | 593 | memcpy (&tunnel->tunnel.peer, peers, sizeof (struct GNUNET_PeerIdentity)); |
568 | 594 | ||
569 | struct peer_list_element *element = handle->connected_peers.head; | 595 | struct peer_list_element *element = handle->connected_peers.head; |
596 | |||
570 | while (element != NULL) | 597 | while (element != NULL) |
571 | { | 598 | { |
572 | if (0 == | 599 | if (0 == |
573 | memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity))) | 600 | memcmp (&element->peer, peers, sizeof (struct GNUNET_PeerIdentity))) |
574 | break; | 601 | break; |
575 | element = element->next; | 602 | element = element->next; |
576 | } | 603 | } |
577 | 604 | ||
578 | if (element != NULL) | 605 | if (element != NULL) |
579 | { | 606 | { |
580 | /* we are connected to this peer */ | 607 | /* we are connected to this peer */ |
581 | GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head, | 608 | GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head, |
582 | handle->established_tunnels.tail, | 609 | handle->established_tunnels.tail, |
583 | handle->established_tunnels.tail, | 610 | handle->established_tunnels.tail, |
584 | tunnel); | 611 | tunnel); |
585 | GNUNET_SCHEDULER_add_now(call_connect_handler, tunnel); | 612 | GNUNET_SCHEDULER_add_now (call_connect_handler, tunnel); |
586 | } | 613 | } |
587 | else if (0 == | 614 | else if (0 == |
588 | memcmp (peers, &handle->myself, | 615 | memcmp (peers, &handle->myself, sizeof (struct GNUNET_PeerIdentity))) |
589 | sizeof (struct GNUNET_PeerIdentity))) | 616 | { |
590 | { | 617 | /* we are the peer */ |
591 | /* we are the peer */ | 618 | GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head, |
592 | GNUNET_CONTAINER_DLL_insert_after (handle->established_tunnels.head, | 619 | handle->established_tunnels.tail, |
593 | handle->established_tunnels.tail, | 620 | handle->established_tunnels.tail, |
594 | handle->established_tunnels.tail, | 621 | tunnel); |
595 | tunnel); | 622 | GNUNET_SCHEDULER_add_now (send_self_connect, tunnel); |
596 | GNUNET_SCHEDULER_add_now(send_self_connect, tunnel); | 623 | } |
597 | } | ||
598 | else | 624 | else |
599 | { | 625 | { |
600 | /* we are not connected to this peer */ | 626 | /* we are not connected to this peer */ |
601 | GNUNET_CONTAINER_DLL_insert_after (handle->pending_tunnels.head, | 627 | GNUNET_CONTAINER_DLL_insert_after (handle->pending_tunnels.head, |
602 | handle->pending_tunnels.tail, | 628 | handle->pending_tunnels.tail, |
603 | handle->pending_tunnels.tail, | 629 | handle->pending_tunnels.tail, tunnel); |
604 | tunnel); | 630 | (void) GNUNET_CORE_peer_request_connect (handle->core, peers, NULL, NULL); |
605 | (void) GNUNET_CORE_peer_request_connect (handle->core, | 631 | } |
606 | peers, | ||
607 | NULL, NULL); | ||
608 | } | ||
609 | 632 | ||
610 | return &tunnel->tunnel; | 633 | return &tunnel->tunnel; |
611 | } | 634 | } |
612 | 635 | ||
613 | const struct GNUNET_PeerIdentity* | 636 | const struct GNUNET_PeerIdentity * |
614 | GNUNET_MESH_get_peer(const struct GNUNET_MESH_Tunnel* tunnel) | 637 | GNUNET_MESH_get_peer (const struct GNUNET_MESH_Tunnel *tunnel) |
615 | { | 638 | { |
616 | return &tunnel->peer; | 639 | return &tunnel->peer; |
617 | } | 640 | } |
618 | 641 | ||
619 | static size_t | 642 | static size_t |
620 | core_notify(void* cls, size_t size, void* buf) | 643 | core_notify (void *cls, size_t size, void *buf) |
621 | { | 644 | { |
622 | struct notify_cls *ncls = cls; | 645 | struct notify_cls *ncls = cls; |
623 | struct GNUNET_MESH_Tunnel *tunnel = ncls->tunnel; | 646 | struct GNUNET_MESH_Tunnel *tunnel = ncls->tunnel; |
647 | |||
624 | tunnel->notify_handle = NULL; | 648 | tunnel->notify_handle = NULL; |
625 | struct tunnel_message* message = buf; | 649 | struct tunnel_message *message = buf; |
626 | void* cbuf = (void*) &message[1]; | 650 | void *cbuf = (void *) &message[1]; |
627 | GNUNET_assert(NULL != ncls->notify); | 651 | |
652 | GNUNET_assert (NULL != ncls->notify); | ||
628 | 653 | ||
629 | size_t sent = ncls->notify(ncls->notify_cls, size - sizeof(struct tunnel_message), cbuf); | 654 | size_t sent = |
655 | ncls->notify (ncls->notify_cls, size - sizeof (struct tunnel_message), | ||
656 | cbuf); | ||
630 | 657 | ||
631 | GNUNET_free(ncls); | 658 | GNUNET_free (ncls); |
632 | 659 | ||
633 | if (0 == sent) return 0; | 660 | if (0 == sent) |
661 | return 0; | ||
634 | 662 | ||
635 | sent += sizeof(struct tunnel_message); | 663 | sent += sizeof (struct tunnel_message); |
636 | 664 | ||
637 | message->hdr.type = htons(GNUNET_MESSAGE_TYPE_MESH); | 665 | message->hdr.type = htons (GNUNET_MESSAGE_TYPE_MESH); |
638 | message->hdr.size = htons(sent); | 666 | message->hdr.size = htons (sent); |
639 | memcpy(&message->id, &tunnel->id, sizeof(struct tunnel_id)); | 667 | memcpy (&message->id, &tunnel->id, sizeof (struct tunnel_id)); |
640 | return sent; | 668 | return sent; |
641 | } | 669 | } |
642 | 670 | ||
@@ -678,30 +706,30 @@ GNUNET_MESH_notify_transmit_ready (struct | |||
678 | notify, void *notify_cls) | 706 | notify, void *notify_cls) |
679 | { | 707 | { |
680 | if (NULL != tunnel->notify_handle) | 708 | if (NULL != tunnel->notify_handle) |
681 | { | 709 | { |
682 | GNUNET_break(0); | 710 | GNUNET_break (0); |
683 | return NULL; | 711 | return NULL; |
684 | } | 712 | } |
685 | 713 | ||
686 | struct notify_cls *cls = GNUNET_malloc (sizeof (struct notify_cls)); | 714 | struct notify_cls *cls = GNUNET_malloc (sizeof (struct notify_cls)); |
715 | |||
687 | cls->notify_cls = notify_cls; | 716 | cls->notify_cls = notify_cls; |
688 | GNUNET_assert (NULL != notify); | 717 | GNUNET_assert (NULL != notify); |
689 | cls->notify = notify; | 718 | cls->notify = notify; |
690 | cls->tunnel = tunnel; | 719 | cls->tunnel = tunnel; |
691 | 720 | ||
692 | tunnel->notify_handle = (struct GNUNET_MESH_TransmitHandle *) | 721 | tunnel->notify_handle = (struct GNUNET_MESH_TransmitHandle *) |
693 | GNUNET_CORE_notify_transmit_ready (tunnel->handle->core, cork, priority, | 722 | GNUNET_CORE_notify_transmit_ready (tunnel->handle->core, cork, priority, |
694 | maxdelay, &tunnel->peer, | 723 | maxdelay, &tunnel->peer, |
695 | notify_size + | 724 | notify_size + |
696 | sizeof (struct tunnel_message), | 725 | sizeof (struct tunnel_message), |
697 | &core_notify, (void *) cls); | 726 | &core_notify, (void *) cls); |
698 | 727 | ||
699 | return tunnel->notify_handle; | 728 | return tunnel->notify_handle; |
700 | } | 729 | } |
701 | 730 | ||
702 | void | 731 | void |
703 | GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle | 732 | GNUNET_MESH_notify_transmit_ready_cancel (struct GNUNET_MESH_TransmitHandle *th) |
704 | *th) | ||
705 | { | 733 | { |
706 | GNUNET_CORE_notify_transmit_ready_cancel ((struct GNUNET_CORE_TransmitHandle | 734 | GNUNET_CORE_notify_transmit_ready_cancel ((struct GNUNET_CORE_TransmitHandle |
707 | *) th); | 735 | *) th); |
@@ -744,30 +772,34 @@ GNUNET_MESH_tunnel_get_data (struct GNUNET_MESH_Tunnel *tunnel) | |||
744 | } | 772 | } |
745 | 773 | ||
746 | 774 | ||
747 | void build_hello_message(struct GNUNET_MESH_Handle* handle, | 775 | void |
748 | const GNUNET_MESH_ApplicationType *stypes) | 776 | build_hello_message (struct GNUNET_MESH_Handle *handle, |
777 | const GNUNET_MESH_ApplicationType *stypes) | ||
749 | { | 778 | { |
750 | int num = 0; | 779 | int num = 0; |
751 | const GNUNET_MESH_ApplicationType *t; | 780 | const GNUNET_MESH_ApplicationType *t; |
752 | 781 | ||
753 | for (t = stypes; *t != GNUNET_APPLICATION_TYPE_END; t++, num++); | 782 | for (t = stypes; *t != GNUNET_APPLICATION_TYPE_END; t++, num++) ; |
754 | 783 | ||
755 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "I can handle %d app-types.\n", num); | 784 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I can handle %d app-types.\n", num); |
756 | 785 | ||
757 | handle->hello_message_size = sizeof(uint16_t) + /* For the number of types */ | 786 | handle->hello_message_size = sizeof (uint16_t) + /* For the number of types */ |
758 | num * sizeof(GNUNET_MESH_ApplicationType); /* For the types */ | 787 | num * sizeof (GNUNET_MESH_ApplicationType); /* For the types */ |
759 | 788 | ||
760 | uint16_t *nums = GNUNET_malloc(handle->hello_message_size); | 789 | uint16_t *nums = GNUNET_malloc (handle->hello_message_size); |
761 | GNUNET_MESH_ApplicationType *types = (GNUNET_MESH_ApplicationType*)(nums + 1); | 790 | GNUNET_MESH_ApplicationType *types = |
791 | (GNUNET_MESH_ApplicationType *) (nums + 1); | ||
762 | 792 | ||
763 | *nums = htons(num); | 793 | *nums = htons (num); |
764 | 794 | ||
765 | int i; | 795 | int i; |
796 | |||
766 | for (i = 0; i < num; i++) | 797 | for (i = 0; i < num; i++) |
767 | { | 798 | { |
768 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "I can handle the app-type %d\n", stypes[i]); | 799 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I can handle the app-type %d\n", |
769 | types[i] = htons(stypes[i]); | 800 | stypes[i]); |
770 | } | 801 | types[i] = htons (stypes[i]); |
802 | } | ||
771 | 803 | ||
772 | handle->hello_message = nums; | 804 | handle->hello_message = nums; |
773 | } | 805 | } |
@@ -775,15 +807,15 @@ void build_hello_message(struct GNUNET_MESH_Handle* handle, | |||
775 | 807 | ||
776 | struct GNUNET_MESH_Handle * | 808 | struct GNUNET_MESH_Handle * |
777 | GNUNET_MESH_connect (const struct | 809 | GNUNET_MESH_connect (const struct |
778 | GNUNET_CONFIGURATION_Handle | 810 | GNUNET_CONFIGURATION_Handle |
779 | *cfg, void *cls, | 811 | *cfg, void *cls, |
780 | GNUNET_MESH_TunnelEndHandler | 812 | GNUNET_MESH_TunnelEndHandler |
781 | cleaner, | 813 | cleaner, |
782 | const struct GNUNET_MESH_MessageHandler *handlers, | 814 | const struct GNUNET_MESH_MessageHandler *handlers, |
783 | const GNUNET_MESH_ApplicationType *stypes) | 815 | const GNUNET_MESH_ApplicationType *stypes) |
784 | { | 816 | { |
785 | struct GNUNET_MESH_Handle *ret = | 817 | struct GNUNET_MESH_Handle *ret = |
786 | GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle)); | 818 | GNUNET_malloc (sizeof (struct GNUNET_MESH_Handle)); |
787 | 819 | ||
788 | ret->connected_to_core = GNUNET_NO; | 820 | ret->connected_to_core = GNUNET_NO; |
789 | ret->connected_peers.head = NULL; | 821 | ret->connected_peers.head = NULL; |
@@ -793,18 +825,19 @@ GNUNET_MESH_connect (const struct | |||
793 | 825 | ||
794 | const struct GNUNET_MESH_MessageHandler *it; | 826 | const struct GNUNET_MESH_MessageHandler *it; |
795 | unsigned int len = 1; | 827 | unsigned int len = 1; |
828 | |||
796 | for (it = handlers; it->callback != NULL; it++) | 829 | for (it = handlers; it->callback != NULL; it++) |
797 | { | 830 | { |
798 | len++; | 831 | len++; |
799 | } | 832 | } |
800 | 833 | ||
801 | ret->handlers = | 834 | ret->handlers = |
802 | GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler)); | 835 | GNUNET_malloc (len * sizeof (struct GNUNET_MESH_MessageHandler)); |
803 | memset(ret->handlers, 0, len * sizeof(struct GNUNET_MESH_MessageHandler)); | 836 | memset (ret->handlers, 0, len * sizeof (struct GNUNET_MESH_MessageHandler)); |
804 | memcpy (ret->handlers, handlers, | 837 | memcpy (ret->handlers, handlers, |
805 | len * sizeof (struct GNUNET_MESH_MessageHandler)); | 838 | len * sizeof (struct GNUNET_MESH_MessageHandler)); |
806 | 839 | ||
807 | build_hello_message(ret, stypes); | 840 | build_hello_message (ret, stypes); |
808 | 841 | ||
809 | static const struct GNUNET_CORE_MessageHandler core_handlers[] = { | 842 | static const struct GNUNET_CORE_MessageHandler core_handlers[] = { |
810 | {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0}, | 843 | {&core_receive, GNUNET_MESSAGE_TYPE_MESH, 0}, |
@@ -813,14 +846,14 @@ GNUNET_MESH_connect (const struct | |||
813 | }; | 846 | }; |
814 | 847 | ||
815 | ret->core = GNUNET_CORE_connect (cfg, | 848 | ret->core = GNUNET_CORE_connect (cfg, |
816 | 42, | 849 | 42, |
817 | ret, | 850 | ret, |
818 | &core_startup, | 851 | &core_startup, |
819 | &core_connect, | 852 | &core_connect, |
820 | &core_disconnect, | 853 | &core_disconnect, |
821 | NULL, | 854 | NULL, |
822 | NULL, | 855 | NULL, |
823 | GNUNET_NO, NULL, GNUNET_NO, core_handlers); | 856 | GNUNET_NO, NULL, GNUNET_NO, core_handlers); |
824 | return ret; | 857 | return ret; |
825 | } | 858 | } |
826 | 859 | ||
@@ -832,33 +865,40 @@ GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle) | |||
832 | GNUNET_CORE_disconnect (handle->core); | 865 | GNUNET_CORE_disconnect (handle->core); |
833 | 866 | ||
834 | struct peer_list_element *element = handle->connected_peers.head; | 867 | struct peer_list_element *element = handle->connected_peers.head; |
868 | |||
835 | while (element != NULL) | 869 | while (element != NULL) |
870 | { | ||
871 | struct peer_list_element *next = element->next; | ||
872 | |||
873 | while (element->type_head != NULL) | ||
836 | { | 874 | { |
837 | struct peer_list_element *next = element->next; | 875 | struct type_list_element *tail = element->type_tail; |
838 | while (element->type_head != NULL) | 876 | |
839 | { | 877 | GNUNET_CONTAINER_DLL_remove (element->type_head, element->type_tail, |
840 | struct type_list_element* tail = element->type_tail; | 878 | tail); |
841 | GNUNET_CONTAINER_DLL_remove(element->type_head, element->type_tail, tail); | 879 | GNUNET_free (tail); |
842 | GNUNET_free(tail); | ||
843 | } | ||
844 | GNUNET_free (element); | ||
845 | element = next; | ||
846 | } | 880 | } |
881 | GNUNET_free (element); | ||
882 | element = next; | ||
883 | } | ||
847 | 884 | ||
848 | struct tunnel_list_element *tunnel = handle->pending_tunnels.head; | 885 | struct tunnel_list_element *tunnel = handle->pending_tunnels.head; |
886 | |||
849 | while (tunnel != NULL) | 887 | while (tunnel != NULL) |
850 | { | 888 | { |
851 | struct tunnel_list_element *next = tunnel->next; | 889 | struct tunnel_list_element *next = tunnel->next; |
852 | GNUNET_free (tunnel); | 890 | |
853 | tunnel = next; | 891 | GNUNET_free (tunnel); |
854 | } | 892 | tunnel = next; |
893 | } | ||
855 | tunnel = handle->established_tunnels.head;; | 894 | tunnel = handle->established_tunnels.head;; |
856 | while (tunnel != NULL) | 895 | while (tunnel != NULL) |
857 | { | 896 | { |
858 | struct tunnel_list_element *next = tunnel->next; | 897 | struct tunnel_list_element *next = tunnel->next; |
859 | GNUNET_free (tunnel); | 898 | |
860 | tunnel = next; | 899 | GNUNET_free (tunnel); |
861 | } | 900 | tunnel = next; |
901 | } | ||
862 | 902 | ||
863 | GNUNET_free (handle); | 903 | GNUNET_free (handle); |
864 | } | 904 | } |