aboutsummaryrefslogtreecommitdiff
path: root/src/cadet
diff options
context:
space:
mode:
Diffstat (limited to 'src/cadet')
-rw-r--r--src/cadet/cadet.h67
-rw-r--r--src/cadet/cadet_api.c762
-rw-r--r--src/cadet/cadet_api_get_channel.c139
-rw-r--r--src/cadet/cadet_api_get_path.c155
-rw-r--r--src/cadet/cadet_api_helper.c16
-rw-r--r--src/cadet/cadet_api_list_peers.c126
-rw-r--r--src/cadet/cadet_api_list_tunnels.c134
-rw-r--r--src/cadet/cadet_protocol.h58
-rw-r--r--src/cadet/cadet_test_lib.c252
-rw-r--r--src/cadet/cadet_test_lib.h34
-rw-r--r--src/cadet/desirability_table.c40
-rw-r--r--src/cadet/gnunet-cadet-profiler.c837
-rw-r--r--src/cadet/gnunet-cadet.c712
-rw-r--r--src/cadet/gnunet-service-cadet.c1174
-rw-r--r--src/cadet/gnunet-service-cadet.h42
-rw-r--r--src/cadet/gnunet-service-cadet_channel.c1870
-rw-r--r--src/cadet/gnunet-service-cadet_channel.h90
-rw-r--r--src/cadet/gnunet-service-cadet_connection.c760
-rw-r--r--src/cadet/gnunet-service-cadet_connection.h83
-rw-r--r--src/cadet/gnunet-service-cadet_core.c1164
-rw-r--r--src/cadet/gnunet-service-cadet_core.h8
-rw-r--r--src/cadet/gnunet-service-cadet_dht.c308
-rw-r--r--src/cadet/gnunet-service-cadet_dht.h14
-rw-r--r--src/cadet/gnunet-service-cadet_hello.c96
-rw-r--r--src/cadet/gnunet-service-cadet_hello.h10
-rw-r--r--src/cadet/gnunet-service-cadet_paths.c748
-rw-r--r--src/cadet/gnunet-service-cadet_paths.h50
-rw-r--r--src/cadet/gnunet-service-cadet_peer.c1244
-rw-r--r--src/cadet/gnunet-service-cadet_peer.h116
-rw-r--r--src/cadet/gnunet-service-cadet_tunnels.c3467
-rw-r--r--src/cadet/gnunet-service-cadet_tunnels.h88
-rw-r--r--src/cadet/test_cadet.c1198
-rw-r--r--src/cadet/test_cadet_flow.c747
-rw-r--r--src/cadet/test_cadet_local_mq.c265
34 files changed, 8403 insertions, 8471 deletions
diff --git a/src/cadet/cadet.h b/src/cadet/cadet.h
index ff3e47d19..9b2050e67 100644
--- a/src/cadet/cadet.h
+++ b/src/cadet/cadet.h
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @author Bartlomiej Polot 22 * @author Bartlomiej Polot
@@ -35,18 +35,18 @@ extern "C" {
35 35
36#include <stdint.h> 36#include <stdint.h>
37 37
38#if ! defined(GNUNET_CULL_LOGGING) 38#if !defined(GNUNET_CULL_LOGGING)
39#define CADET_TIMING_START \ 39#define CADET_TIMING_START \
40 struct GNUNET_TIME_Absolute __timestamp; \ 40 struct GNUNET_TIME_Absolute __timestamp; \
41 __timestamp = GNUNET_TIME_absolute_get () 41 __timestamp = GNUNET_TIME_absolute_get()
42 42
43#define CADET_TIMING_END \ 43#define CADET_TIMING_END \
44 struct GNUNET_TIME_Relative __duration; \ 44 struct GNUNET_TIME_Relative __duration; \
45 __duration = GNUNET_TIME_absolute_get_duration (__timestamp); \ 45 __duration = GNUNET_TIME_absolute_get_duration(__timestamp); \
46 LOG (GNUNET_ERROR_TYPE_INFO, \ 46 LOG(GNUNET_ERROR_TYPE_INFO, \
47 " %s duration %s\n", \ 47 " %s duration %s\n", \
48 __FUNCTION__, \ 48 __FUNCTION__, \
49 GNUNET_STRINGS_relative_time_to_string (__duration, GNUNET_YES)); 49 GNUNET_STRINGS_relative_time_to_string(__duration, GNUNET_YES));
50#else 50#else
51#define CADET_TIMING_START 51#define CADET_TIMING_START
52#define CADET_TIMING_END 52#define CADET_TIMING_END
@@ -99,8 +99,7 @@ GNUNET_NETWORK_STRUCT_BEGIN
99/** 99/**
100 * Number uniquely identifying a channel of a client. 100 * Number uniquely identifying a channel of a client.
101 */ 101 */
102struct GNUNET_CADET_ClientChannelNumber 102struct GNUNET_CADET_ClientChannelNumber {
103{
104 /** 103 /**
105 * Values for channel numbering. 104 * Values for channel numbering.
106 * Local channel numbers given by the service (incoming) are 105 * Local channel numbers given by the service (incoming) are
@@ -115,8 +114,7 @@ struct GNUNET_CADET_ClientChannelNumber
115/** 114/**
116 * Message for a client to create and destroy channels. 115 * Message for a client to create and destroy channels.
117 */ 116 */
118struct GNUNET_CADET_PortMessage 117struct GNUNET_CADET_PortMessage {
119{
120 /** 118 /**
121 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN 119 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN
122 * or #GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE 120 * or #GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE
@@ -135,8 +133,7 @@ struct GNUNET_CADET_PortMessage
135/** 133/**
136 * Message for a client to create channels. 134 * Message for a client to create channels.
137 */ 135 */
138struct GNUNET_CADET_LocalChannelCreateMessage 136struct GNUNET_CADET_LocalChannelCreateMessage {
139{
140 /** 137 /**
141 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE 138 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE
142 * 139 *
@@ -169,8 +166,7 @@ struct GNUNET_CADET_LocalChannelCreateMessage
169/** 166/**
170 * Message for or to a client to destroy tunnel. 167 * Message for or to a client to destroy tunnel.
171 */ 168 */
172struct GNUNET_CADET_LocalChannelDestroyMessage 169struct GNUNET_CADET_LocalChannelDestroyMessage {
173{
174 /** 170 /**
175 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY 171 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY
176 */ 172 */
@@ -186,8 +182,7 @@ struct GNUNET_CADET_LocalChannelDestroyMessage
186/** 182/**
187 * Message for cadet data traffic. 183 * Message for cadet data traffic.
188 */ 184 */
189struct GNUNET_CADET_LocalData 185struct GNUNET_CADET_LocalData {
190{
191 /** 186 /**
192 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA 187 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA
193 */ 188 */
@@ -214,8 +209,7 @@ struct GNUNET_CADET_LocalData
214 * Message to allow the client send more data to the service 209 * Message to allow the client send more data to the service
215 * (always service -> client). 210 * (always service -> client).
216 */ 211 */
217struct GNUNET_CADET_LocalAck 212struct GNUNET_CADET_LocalAck {
218{
219 /** 213 /**
220 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK 214 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK
221 */ 215 */
@@ -233,8 +227,7 @@ struct GNUNET_CADET_LocalAck
233 * 227 *
234 * TODO: split into two messages! 228 * TODO: split into two messages!
235 */ 229 */
236struct GNUNET_CADET_LocalInfo 230struct GNUNET_CADET_LocalInfo {
237{
238 /** 231 /**
239 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL or 232 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL or
240 * #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER 233 * #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER
@@ -256,8 +249,7 @@ struct GNUNET_CADET_LocalInfo
256/** 249/**
257 * Message to inform the client about channels in the service. 250 * Message to inform the client about channels in the service.
258 */ 251 */
259struct GNUNET_CADET_RequestPathInfoMessage 252struct GNUNET_CADET_RequestPathInfoMessage {
260{
261 /** 253 /**
262 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PATH 254 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PATH
263 */ 255 */
@@ -278,8 +270,7 @@ struct GNUNET_CADET_RequestPathInfoMessage
278/** 270/**
279 * Message to inform the client about channels in the service. 271 * Message to inform the client about channels in the service.
280 */ 272 */
281struct GNUNET_CADET_ChannelInfoMessage 273struct GNUNET_CADET_ChannelInfoMessage {
282{
283 /** 274 /**
284 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL. 275 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL.
285 */ 276 */
@@ -302,8 +293,7 @@ struct GNUNET_CADET_ChannelInfoMessage
302/** 293/**
303 * Message to as the service about information on a channel. 294 * Message to as the service about information on a channel.
304 */ 295 */
305struct GNUNET_CADET_RequestChannelInfoMessage 296struct GNUNET_CADET_RequestChannelInfoMessage {
306{
307 /** 297 /**
308 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_CHANNEL. 298 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_CHANNEL.
309 */ 299 */
@@ -319,8 +309,7 @@ struct GNUNET_CADET_RequestChannelInfoMessage
319/** 309/**
320 * Message to inform the client about one of the paths known to the service. 310 * Message to inform the client about one of the paths known to the service.
321 */ 311 */
322struct GNUNET_CADET_LocalInfoPath 312struct GNUNET_CADET_LocalInfoPath {
323{
324 /** 313 /**
325 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH. 314 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH.
326 */ 315 */
@@ -336,8 +325,7 @@ struct GNUNET_CADET_LocalInfoPath
336/** 325/**
337 * Message to inform the client about one of the peers in the service. 326 * Message to inform the client about one of the peers in the service.
338 */ 327 */
339struct GNUNET_CADET_LocalInfoPeers 328struct GNUNET_CADET_LocalInfoPeers {
340{
341 /** 329 /**
342 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS 330 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS
343 */ 331 */
@@ -370,8 +358,7 @@ struct GNUNET_CADET_LocalInfoPeers
370 * 358 *
371 * TODO: split into two messages! 359 * TODO: split into two messages!
372 */ 360 */
373struct GNUNET_CADET_LocalInfoTunnel 361struct GNUNET_CADET_LocalInfoTunnel {
374{
375 /** 362 /**
376 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL 363 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL
377 * or #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS 364 * or #GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS
@@ -419,7 +406,7 @@ GNUNET_NETWORK_STRUCT_END
419 * @return String representing FWD or BCK. 406 * @return String representing FWD or BCK.
420 */ 407 */
421char * 408char *
422GC_f2s (int fwd); 409GC_f2s(int fwd);
423 410
424 411
425/** 412/**
@@ -431,7 +418,7 @@ GC_f2s (int fwd);
431 * @return True if bigger (arg1) has a higher value than smaller (arg 2). 418 * @return True if bigger (arg1) has a higher value than smaller (arg 2).
432 */ 419 */
433int 420int
434GC_is_pid_bigger (uint32_t bigger, uint32_t smaller); 421GC_is_pid_bigger(uint32_t bigger, uint32_t smaller);
435 422
436 423
437/** 424/**
@@ -443,7 +430,7 @@ GC_is_pid_bigger (uint32_t bigger, uint32_t smaller);
443 * @return Highest ACK value from the two. 430 * @return Highest ACK value from the two.
444 */ 431 */
445uint32_t 432uint32_t
446GC_max_pid (uint32_t a, uint32_t b); 433GC_max_pid(uint32_t a, uint32_t b);
447 434
448 435
449/** 436/**
@@ -455,7 +442,7 @@ GC_max_pid (uint32_t a, uint32_t b);
455 * @return Lowest ACK value from the two. 442 * @return Lowest ACK value from the two.
456 */ 443 */
457uint32_t 444uint32_t
458GC_min_pid (uint32_t a, uint32_t b); 445GC_min_pid(uint32_t a, uint32_t b);
459 446
460 447
461/** 448/**
@@ -468,7 +455,7 @@ GC_min_pid (uint32_t a, uint32_t b);
468 * @return The size of the output. 455 * @return The size of the output.
469 */ 456 */
470size_t 457size_t
471GC_bin2s (void *bin, unsigned int len, char **output); 458GC_bin2s(void *bin, unsigned int len, char **output);
472 459
473 460
474/** 461/**
@@ -482,7 +469,7 @@ GC_bin2s (void *bin, unsigned int len, char **output);
482 * @return Human readable string description. 469 * @return Human readable string description.
483 */ 470 */
484const char * 471const char *
485GC_m2s (uint16_t m); 472GC_m2s(uint16_t m);
486 473
487#if 0 /* keep Emacsens' auto-indent happy */ 474#if 0 /* keep Emacsens' auto-indent happy */
488{ 475{
diff --git a/src/cadet/cadet_api.c b/src/cadet/cadet_api.c
index 7148ab956..61ff71b31 100644
--- a/src/cadet/cadet_api.c
+++ b/src/cadet/cadet_api.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file cadet/cadet_api.c 21 * @file cadet/cadet_api.c
22 * @brief cadet api: client implementation of cadet service 22 * @brief cadet api: client implementation of cadet service
@@ -30,13 +30,12 @@
30#include "cadet.h" 30#include "cadet.h"
31#include "cadet_protocol.h" 31#include "cadet_protocol.h"
32 32
33#define LOG(kind, ...) GNUNET_log_from (kind, "cadet-api", __VA_ARGS__) 33#define LOG(kind, ...) GNUNET_log_from(kind, "cadet-api", __VA_ARGS__)
34 34
35/** 35/**
36 * Opaque handle to the service. 36 * Opaque handle to the service.
37 */ 37 */
38struct GNUNET_CADET_Handle 38struct GNUNET_CADET_Handle {
39{
40 /** 39 /**
41 * Message queue. 40 * Message queue.
42 */ 41 */
@@ -77,9 +76,7 @@ struct GNUNET_CADET_Handle
77/** 76/**
78 * Opaque handle to a channel. 77 * Opaque handle to a channel.
79 */ 78 */
80struct GNUNET_CADET_Channel 79struct GNUNET_CADET_Channel {
81{
82
83 /** 80 /**
84 * Other end of the channel. 81 * Other end of the channel.
85 */ 82 */
@@ -143,9 +140,7 @@ struct GNUNET_CADET_Channel
143/** 140/**
144 * Opaque handle to a port. 141 * Opaque handle to a port.
145 */ 142 */
146struct GNUNET_CADET_Port 143struct GNUNET_CADET_Port {
147{
148
149 /** 144 /**
150 * Port "number" 145 * Port "number"
151 */ 146 */
@@ -196,10 +191,10 @@ struct GNUNET_CADET_Port
196 * @return The port handle if known, NULL otherwise. 191 * @return The port handle if known, NULL otherwise.
197 */ 192 */
198static struct GNUNET_CADET_Port * 193static struct GNUNET_CADET_Port *
199find_port (const struct GNUNET_CADET_Handle *h, 194find_port(const struct GNUNET_CADET_Handle *h,
200 const struct GNUNET_HashCode *hash) 195 const struct GNUNET_HashCode *hash)
201{ 196{
202 return GNUNET_CONTAINER_multihashmap_get (h->ports, hash); 197 return GNUNET_CONTAINER_multihashmap_get(h->ports, hash);
203} 198}
204 199
205 200
@@ -211,11 +206,11 @@ find_port (const struct GNUNET_CADET_Handle *h,
211 * @return handle to the required channel or NULL if not found 206 * @return handle to the required channel or NULL if not found
212 */ 207 */
213static struct GNUNET_CADET_Channel * 208static struct GNUNET_CADET_Channel *
214find_channel (struct GNUNET_CADET_Handle *h, 209find_channel(struct GNUNET_CADET_Handle *h,
215 struct GNUNET_CADET_ClientChannelNumber ccn) 210 struct GNUNET_CADET_ClientChannelNumber ccn)
216{ 211{
217 return GNUNET_CONTAINER_multihashmap32_get (h->channels, 212 return GNUNET_CONTAINER_multihashmap32_get(h->channels,
218 ntohl (ccn.channel_of_client)); 213 ntohl(ccn.channel_of_client));
219} 214}
220 215
221 216
@@ -227,33 +222,33 @@ find_channel (struct GNUNET_CADET_Handle *h,
227 * @return Handle to the created channel. 222 * @return Handle to the created channel.
228 */ 223 */
229static struct GNUNET_CADET_Channel * 224static struct GNUNET_CADET_Channel *
230create_channel (struct GNUNET_CADET_Handle *h, 225create_channel(struct GNUNET_CADET_Handle *h,
231 const struct GNUNET_CADET_ClientChannelNumber *ccnp) 226 const struct GNUNET_CADET_ClientChannelNumber *ccnp)
232{ 227{
233 struct GNUNET_CADET_Channel *ch; 228 struct GNUNET_CADET_Channel *ch;
234 struct GNUNET_CADET_ClientChannelNumber ccn; 229 struct GNUNET_CADET_ClientChannelNumber ccn;
235 230
236 ch = GNUNET_new (struct GNUNET_CADET_Channel); 231 ch = GNUNET_new(struct GNUNET_CADET_Channel);
237 ch->cadet = h; 232 ch->cadet = h;
238 if (NULL == ccnp) 233 if (NULL == ccnp)
239 { 234 {
240 while (NULL != find_channel (h, h->next_ccn)) 235 while (NULL != find_channel(h, h->next_ccn))
241 h->next_ccn.channel_of_client = 236 h->next_ccn.channel_of_client =
242 htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI | 237 htonl(GNUNET_CADET_LOCAL_CHANNEL_ID_CLI |
243 (1 + ntohl (h->next_ccn.channel_of_client))); 238 (1 + ntohl(h->next_ccn.channel_of_client)));
244 ccn = h->next_ccn; 239 ccn = h->next_ccn;
245 } 240 }
246 else 241 else
247 { 242 {
248 ccn = *ccnp; 243 ccn = *ccnp;
249 } 244 }
250 ch->ccn = ccn; 245 ch->ccn = ccn;
251 GNUNET_assert (GNUNET_OK == 246 GNUNET_assert(GNUNET_OK ==
252 GNUNET_CONTAINER_multihashmap32_put ( 247 GNUNET_CONTAINER_multihashmap32_put(
253 h->channels, 248 h->channels,
254 ntohl (ch->ccn.channel_of_client), 249 ntohl(ch->ccn.channel_of_client),
255 ch, 250 ch,
256 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 251 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
257 return ch; 252 return ch;
258} 253}
259 254
@@ -269,31 +264,31 @@ create_channel (struct GNUNET_CADET_Handle *h,
269 * @param call_cleaner Whether to call the cleaner handler. 264 * @param call_cleaner Whether to call the cleaner handler.
270 */ 265 */
271static void 266static void
272destroy_channel (struct GNUNET_CADET_Channel *ch) 267destroy_channel(struct GNUNET_CADET_Channel *ch)
273{ 268{
274 struct GNUNET_CADET_Handle *h = ch->cadet; 269 struct GNUNET_CADET_Handle *h = ch->cadet;
275 270
276 LOG (GNUNET_ERROR_TYPE_DEBUG, 271 LOG(GNUNET_ERROR_TYPE_DEBUG,
277 "Destroying channel %X of %p\n", 272 "Destroying channel %X of %p\n",
278 htonl (ch->ccn.channel_of_client), 273 htonl(ch->ccn.channel_of_client),
279 h); 274 h);
280 GNUNET_assert ( 275 GNUNET_assert(
281 GNUNET_YES == 276 GNUNET_YES ==
282 GNUNET_CONTAINER_multihashmap32_remove (h->channels, 277 GNUNET_CONTAINER_multihashmap32_remove(h->channels,
283 ntohl (ch->ccn.channel_of_client), 278 ntohl(ch->ccn.channel_of_client),
284 ch)); 279 ch));
285 if (NULL != ch->mq_cont) 280 if (NULL != ch->mq_cont)
286 { 281 {
287 GNUNET_SCHEDULER_cancel (ch->mq_cont); 282 GNUNET_SCHEDULER_cancel(ch->mq_cont);
288 ch->mq_cont = NULL; 283 ch->mq_cont = NULL;
289 } 284 }
290 /* signal channel destruction */ 285 /* signal channel destruction */
291 if (NULL != ch->disconnects) 286 if (NULL != ch->disconnects)
292 ch->disconnects (ch->ctx, ch); 287 ch->disconnects(ch->ctx, ch);
293 if (NULL != ch->pending_env) 288 if (NULL != ch->pending_env)
294 GNUNET_MQ_discard (ch->pending_env); 289 GNUNET_MQ_discard(ch->pending_env);
295 GNUNET_MQ_destroy (ch->mq); 290 GNUNET_MQ_destroy(ch->mq);
296 GNUNET_free (ch); 291 GNUNET_free(ch);
297} 292}
298 293
299 294
@@ -304,7 +299,7 @@ destroy_channel (struct GNUNET_CADET_Channel *ch)
304 * @param h handle to the cadet 299 * @param h handle to the cadet
305 */ 300 */
306static void 301static void
307reconnect (struct GNUNET_CADET_Handle *h); 302reconnect(struct GNUNET_CADET_Handle *h);
308 303
309 304
310/** 305/**
@@ -317,17 +312,17 @@ reconnect (struct GNUNET_CADET_Handle *h);
317 * @return #GNUNET_OK (continue to iterate) 312 * @return #GNUNET_OK (continue to iterate)
318 */ 313 */
319static int 314static int
320open_port_cb (void *cls, const struct GNUNET_HashCode *id, void *value) 315open_port_cb(void *cls, const struct GNUNET_HashCode *id, void *value)
321{ 316{
322 struct GNUNET_CADET_Handle *h = cls; 317 struct GNUNET_CADET_Handle *h = cls;
323 struct GNUNET_CADET_Port *port = value; 318 struct GNUNET_CADET_Port *port = value;
324 struct GNUNET_CADET_PortMessage *msg; 319 struct GNUNET_CADET_PortMessage *msg;
325 struct GNUNET_MQ_Envelope *env; 320 struct GNUNET_MQ_Envelope *env;
326 321
327 (void) id; 322 (void)id;
328 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN); 323 env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN);
329 msg->port = port->id; 324 msg->port = port->id;
330 GNUNET_MQ_send (h->mq, env); 325 GNUNET_MQ_send(h->mq, env);
331 return GNUNET_OK; 326 return GNUNET_OK;
332} 327}
333 328
@@ -339,14 +334,14 @@ open_port_cb (void *cls, const struct GNUNET_HashCode *id, void *value)
339 * @param cls closure (cadet handle) 334 * @param cls closure (cadet handle)
340 */ 335 */
341static void 336static void
342reconnect_cbk (void *cls) 337reconnect_cbk(void *cls)
343{ 338{
344 struct GNUNET_CADET_Handle *h = cls; 339 struct GNUNET_CADET_Handle *h = cls;
345 340
346 h->reconnect_task = NULL; 341 h->reconnect_task = NULL;
347 h->reconnect_time = GNUNET_TIME_STD_BACKOFF (h->reconnect_time); 342 h->reconnect_time = GNUNET_TIME_STD_BACKOFF(h->reconnect_time);
348 reconnect (h); 343 reconnect(h);
349 GNUNET_CONTAINER_multihashmap_iterate (h->ports, &open_port_cb, h); 344 GNUNET_CONTAINER_multihashmap_iterate(h->ports, &open_port_cb, h);
350} 345}
351 346
352 347
@@ -356,12 +351,12 @@ reconnect_cbk (void *cls)
356 * @param ch Channel to notify about. 351 * @param ch Channel to notify about.
357 */ 352 */
358static void 353static void
359notify_window_size (struct GNUNET_CADET_Channel *ch) 354notify_window_size(struct GNUNET_CADET_Channel *ch)
360{ 355{
361 if (NULL != ch->window_changes) 356 if (NULL != ch->window_changes)
362 ch->window_changes (ch->ctx, 357 ch->window_changes(ch->ctx,
363 ch, /* FIXME: remove 'ch'? */ 358 ch, /* FIXME: remove 'ch'? */
364 ch->allow_send); 359 ch->allow_send);
365} 360}
366 361
367 362
@@ -371,32 +366,32 @@ notify_window_size (struct GNUNET_CADET_Channel *ch)
371 * @param cls Closure (channel whose mq to activate). 366 * @param cls Closure (channel whose mq to activate).
372 */ 367 */
373static void 368static void
374cadet_mq_send_now (void *cls) 369cadet_mq_send_now(void *cls)
375{ 370{
376 struct GNUNET_CADET_Channel *ch = cls; 371 struct GNUNET_CADET_Channel *ch = cls;
377 struct GNUNET_MQ_Envelope *env = ch->pending_env; 372 struct GNUNET_MQ_Envelope *env = ch->pending_env;
378 373
379 ch->mq_cont = NULL; 374 ch->mq_cont = NULL;
380 if (0 == ch->allow_send) 375 if (0 == ch->allow_send)
381 { 376 {
382 /* how did we get here? */ 377 /* how did we get here? */
383 GNUNET_break (0); 378 GNUNET_break(0);
384 return; 379 return;
385 } 380 }
386 if (NULL == env) 381 if (NULL == env)
387 { 382 {
388 /* how did we get here? */ 383 /* how did we get here? */
389 GNUNET_break (0); 384 GNUNET_break(0);
390 return; 385 return;
391 } 386 }
392 ch->allow_send--; 387 ch->allow_send--;
393 ch->pending_env = NULL; 388 ch->pending_env = NULL;
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 389 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
395 "Sending message on channel %s to CADET, new window size is %u\n", 390 "Sending message on channel %s to CADET, new window size is %u\n",
396 GNUNET_i2s (&ch->peer), 391 GNUNET_i2s(&ch->peer),
397 ch->allow_send); 392 ch->allow_send);
398 GNUNET_MQ_send (ch->cadet->mq, env); 393 GNUNET_MQ_send(ch->cadet->mq, env);
399 GNUNET_MQ_impl_send_continue (ch->mq); 394 GNUNET_MQ_impl_send_continue(ch->mq);
400} 395}
401 396
402 397
@@ -413,9 +408,9 @@ cadet_mq_send_now (void *cls)
413 * @param impl_state state of the implementation 408 * @param impl_state state of the implementation
414 */ 409 */
415static void 410static void
416cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq, 411cadet_mq_send_impl(struct GNUNET_MQ_Handle *mq,
417 const struct GNUNET_MessageHeader *msg, 412 const struct GNUNET_MessageHeader *msg,
418 void *impl_state) 413 void *impl_state)
419{ 414{
420 struct GNUNET_CADET_Channel *ch = impl_state; 415 struct GNUNET_CADET_Channel *ch = impl_state;
421 struct GNUNET_CADET_Handle *h = ch->cadet; 416 struct GNUNET_CADET_Handle *h = ch->cadet;
@@ -426,31 +421,31 @@ cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
426 enum GNUNET_MQ_PriorityPreferences pp; 421 enum GNUNET_MQ_PriorityPreferences pp;
427 422
428 if (NULL == h->mq) 423 if (NULL == h->mq)
429 { 424 {
430 /* We're currently reconnecting, pretend this worked */ 425 /* We're currently reconnecting, pretend this worked */
431 GNUNET_MQ_impl_send_continue (mq); 426 GNUNET_MQ_impl_send_continue(mq);
432 return; 427 return;
433 } 428 }
434 orig_env = GNUNET_MQ_get_current_envelope (mq); 429 orig_env = GNUNET_MQ_get_current_envelope(mq);
435 pp = GNUNET_MQ_env_get_options (orig_env); 430 pp = GNUNET_MQ_env_get_options(orig_env);
436 431
437 /* check message size for sanity */ 432 /* check message size for sanity */
438 msize = ntohs (msg->size); 433 msize = ntohs(msg->size);
439 if (msize > GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE) 434 if (msize > GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE)
440 { 435 {
441 GNUNET_break (0); 436 GNUNET_break(0);
442 GNUNET_MQ_impl_send_continue (mq); 437 GNUNET_MQ_impl_send_continue(mq);
443 return; 438 return;
444 } 439 }
445 env = GNUNET_MQ_msg_nested_mh (cadet_msg, 440 env = GNUNET_MQ_msg_nested_mh(cadet_msg,
446 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 441 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
447 msg); 442 msg);
448 cadet_msg->ccn = ch->ccn; 443 cadet_msg->ccn = ch->ccn;
449 cadet_msg->pp = htonl ((uint32_t) pp); 444 cadet_msg->pp = htonl((uint32_t)pp);
450 GNUNET_assert (NULL == ch->pending_env); 445 GNUNET_assert(NULL == ch->pending_env);
451 ch->pending_env = env; 446 ch->pending_env = env;
452 if (0 < ch->allow_send) 447 if (0 < ch->allow_send)
453 ch->mq_cont = GNUNET_SCHEDULER_add_now (&cadet_mq_send_now, ch); 448 ch->mq_cont = GNUNET_SCHEDULER_add_now(&cadet_mq_send_now, ch);
454} 449}
455 450
456 451
@@ -462,11 +457,11 @@ cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
462 * @param impl_state state of the implementation 457 * @param impl_state state of the implementation
463 */ 458 */
464static void 459static void
465cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state) 460cadet_mq_destroy_impl(struct GNUNET_MQ_Handle *mq, void *impl_state)
466{ 461{
467 struct GNUNET_CADET_Channel *ch = impl_state; 462 struct GNUNET_CADET_Channel *ch = impl_state;
468 463
469 GNUNET_assert (mq == ch->mq); 464 GNUNET_assert(mq == ch->mq);
470 ch->mq = NULL; 465 ch->mq = NULL;
471} 466}
472 467
@@ -480,25 +475,25 @@ cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
480 * @param error error code 475 * @param error error code
481 */ 476 */
482static void 477static void
483cadet_mq_error_handler (void *cls, enum GNUNET_MQ_Error error) 478cadet_mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
484{ 479{
485 struct GNUNET_CADET_Channel *ch = cls; 480 struct GNUNET_CADET_Channel *ch = cls;
486 481
487 if (GNUNET_MQ_ERROR_NO_MATCH == error) 482 if (GNUNET_MQ_ERROR_NO_MATCH == error)
488 { 483 {
489 /* Got a message we did not understand, still try to continue! */ 484 /* Got a message we did not understand, still try to continue! */
490 GNUNET_break_op (0); 485 GNUNET_break_op(0);
491 GNUNET_CADET_receive_done (ch); 486 GNUNET_CADET_receive_done(ch);
492 } 487 }
493 else 488 else
494 { 489 {
495 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 490 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
496 "MQ error in communication with CADET: %d\n", 491 "MQ error in communication with CADET: %d\n",
497 error); 492 error);
498 if (NULL != ch->disconnects) 493 if (NULL != ch->disconnects)
499 ch->disconnects (ch->ctx, ch); 494 ch->disconnects(ch->ctx, ch);
500 GNUNET_CADET_channel_destroy (ch); 495 GNUNET_CADET_channel_destroy(ch);
501 } 496 }
502} 497}
503 498
504 499
@@ -510,19 +505,19 @@ cadet_mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
510 * @param impl_state state specific to the implementation 505 * @param impl_state state specific to the implementation
511 */ 506 */
512static void 507static void
513cadet_mq_cancel_impl (struct GNUNET_MQ_Handle *mq, void *impl_state) 508cadet_mq_cancel_impl(struct GNUNET_MQ_Handle *mq, void *impl_state)
514{ 509{
515 struct GNUNET_CADET_Channel *ch = impl_state; 510 struct GNUNET_CADET_Channel *ch = impl_state;
516 511
517 (void) mq; 512 (void)mq;
518 GNUNET_assert (NULL != ch->pending_env); 513 GNUNET_assert(NULL != ch->pending_env);
519 GNUNET_MQ_discard (ch->pending_env); 514 GNUNET_MQ_discard(ch->pending_env);
520 ch->pending_env = NULL; 515 ch->pending_env = NULL;
521 if (NULL != ch->mq_cont) 516 if (NULL != ch->mq_cont)
522 { 517 {
523 GNUNET_SCHEDULER_cancel (ch->mq_cont); 518 GNUNET_SCHEDULER_cancel(ch->mq_cont);
524 ch->mq_cont = NULL; 519 ch->mq_cont = NULL;
525 } 520 }
526} 521}
527 522
528 523
@@ -533,7 +528,7 @@ cadet_mq_cancel_impl (struct GNUNET_MQ_Handle *mq, void *impl_state)
533 * @param msg A message with the details of the new incoming channel 528 * @param msg A message with the details of the new incoming channel
534 */ 529 */
535static void 530static void
536handle_channel_created ( 531handle_channel_created(
537 void *cls, 532 void *cls,
538 const struct GNUNET_CADET_LocalChannelCreateMessage *msg) 533 const struct GNUNET_CADET_LocalChannelCreateMessage *msg)
539{ 534{
@@ -545,52 +540,52 @@ handle_channel_created (
545 540
546 ccn = msg->ccn; 541 ccn = msg->ccn;
547 port_number = &msg->port; 542 port_number = &msg->port;
548 if (ntohl (ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI) 543 if (ntohl(ccn.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
549 { 544 {
550 GNUNET_break (0); 545 GNUNET_break(0);
551 return; 546 return;
552 } 547 }
553 port = find_port (h, port_number); 548 port = find_port(h, port_number);
554 if (NULL == port) 549 if (NULL == port)
555 { 550 {
556 /* We could have closed the port but the service didn't know about it yet 551 /* We could have closed the port but the service didn't know about it yet
557 * This is not an error. 552 * This is not an error.
558 */ 553 */
559 struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg; 554 struct GNUNET_CADET_LocalChannelDestroyMessage *d_msg;
560 struct GNUNET_MQ_Envelope *env; 555 struct GNUNET_MQ_Envelope *env;
561 556
562 LOG (GNUNET_ERROR_TYPE_DEBUG, 557 LOG(GNUNET_ERROR_TYPE_DEBUG,
563 "No handler for incoming channel %X (on port %s, recently closed?)\n", 558 "No handler for incoming channel %X (on port %s, recently closed?)\n",
564 ntohl (ccn.channel_of_client), 559 ntohl(ccn.channel_of_client),
565 GNUNET_h2s (port_number)); 560 GNUNET_h2s(port_number));
566 env = 561 env =
567 GNUNET_MQ_msg (d_msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY); 562 GNUNET_MQ_msg(d_msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
568 d_msg->ccn = msg->ccn; 563 d_msg->ccn = msg->ccn;
569 GNUNET_MQ_send (h->mq, env); 564 GNUNET_MQ_send(h->mq, env);
570 return; 565 return;
571 } 566 }
572 567
573 ch = create_channel (h, &ccn); 568 ch = create_channel(h, &ccn);
574 ch->peer = msg->peer; 569 ch->peer = msg->peer;
575 ch->incoming_port = port; 570 ch->incoming_port = port;
576 LOG (GNUNET_ERROR_TYPE_DEBUG, 571 LOG(GNUNET_ERROR_TYPE_DEBUG,
577 "Creating incoming channel %X [%s] %p\n", 572 "Creating incoming channel %X [%s] %p\n",
578 ntohl (ccn.channel_of_client), 573 ntohl(ccn.channel_of_client),
579 GNUNET_h2s (port_number), 574 GNUNET_h2s(port_number),
580 ch); 575 ch);
581 576
582 GNUNET_assert (NULL != port->connects); 577 GNUNET_assert(NULL != port->connects);
583 ch->window_changes = port->window_changes; 578 ch->window_changes = port->window_changes;
584 ch->disconnects = port->disconnects; 579 ch->disconnects = port->disconnects;
585 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl, 580 ch->mq = GNUNET_MQ_queue_for_callbacks(&cadet_mq_send_impl,
586 &cadet_mq_destroy_impl, 581 &cadet_mq_destroy_impl,
587 &cadet_mq_cancel_impl, 582 &cadet_mq_cancel_impl,
588 ch, 583 ch,
589 port->handlers, 584 port->handlers,
590 &cadet_mq_error_handler, 585 &cadet_mq_error_handler,
591 ch); 586 ch);
592 ch->ctx = port->connects (port->cls, ch, &msg->peer); 587 ch->ctx = port->connects(port->cls, ch, &msg->peer);
593 GNUNET_MQ_set_handlers_closure (ch->mq, ch->ctx); 588 GNUNET_MQ_set_handlers_closure(ch->mq, ch->ctx);
594} 589}
595 590
596 591
@@ -601,25 +596,25 @@ handle_channel_created (
601 * @param msg A message with the details of the channel being destroyed 596 * @param msg A message with the details of the channel being destroyed
602 */ 597 */
603static void 598static void
604handle_channel_destroy ( 599handle_channel_destroy(
605 void *cls, 600 void *cls,
606 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg) 601 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
607{ 602{
608 struct GNUNET_CADET_Handle *h = cls; 603 struct GNUNET_CADET_Handle *h = cls;
609 struct GNUNET_CADET_Channel *ch; 604 struct GNUNET_CADET_Channel *ch;
610 605
611 ch = find_channel (h, msg->ccn); 606 ch = find_channel(h, msg->ccn);
612 if (NULL == ch) 607 if (NULL == ch)
613 { 608 {
614 LOG (GNUNET_ERROR_TYPE_DEBUG, 609 LOG(GNUNET_ERROR_TYPE_DEBUG,
615 "Received channel destroy for unknown channel %X from CADET service (recently close?)\n", 610 "Received channel destroy for unknown channel %X from CADET service (recently close?)\n",
616 ntohl (msg->ccn.channel_of_client)); 611 ntohl(msg->ccn.channel_of_client));
617 return; 612 return;
618 } 613 }
619 LOG (GNUNET_ERROR_TYPE_DEBUG, 614 LOG(GNUNET_ERROR_TYPE_DEBUG,
620 "Received channel destroy for channel %X from CADET service\n", 615 "Received channel destroy for channel %X from CADET service\n",
621 ntohl (msg->ccn.channel_of_client)); 616 ntohl(msg->ccn.channel_of_client));
622 destroy_channel (ch); 617 destroy_channel(ch);
623} 618}
624 619
625 620
@@ -632,17 +627,17 @@ handle_channel_destroy (
632 * #GNUNET_SYSERR otherwise 627 * #GNUNET_SYSERR otherwise
633 */ 628 */
634static int 629static int
635check_local_data (void *cls, const struct GNUNET_CADET_LocalData *message) 630check_local_data(void *cls, const struct GNUNET_CADET_LocalData *message)
636{ 631{
637 uint16_t size; 632 uint16_t size;
638 633
639 (void) cls; 634 (void)cls;
640 size = ntohs (message->header.size); 635 size = ntohs(message->header.size);
641 if (sizeof (*message) + sizeof (struct GNUNET_MessageHeader) > size) 636 if (sizeof(*message) + sizeof(struct GNUNET_MessageHeader) > size)
642 { 637 {
643 GNUNET_break (0); 638 GNUNET_break(0);
644 return GNUNET_SYSERR; 639 return GNUNET_SYSERR;
645 } 640 }
646 return GNUNET_OK; 641 return GNUNET_OK;
647} 642}
648 643
@@ -654,7 +649,7 @@ check_local_data (void *cls, const struct GNUNET_CADET_LocalData *message)
654 * @param message A message encapsulating the data 649 * @param message A message encapsulating the data
655 */ 650 */
656static void 651static void
657handle_local_data (void *cls, const struct GNUNET_CADET_LocalData *message) 652handle_local_data(void *cls, const struct GNUNET_CADET_LocalData *message)
658{ 653{
659 struct GNUNET_CADET_Handle *h = cls; 654 struct GNUNET_CADET_Handle *h = cls;
660 const struct GNUNET_MessageHeader *payload; 655 const struct GNUNET_MessageHeader *payload;
@@ -662,25 +657,25 @@ handle_local_data (void *cls, const struct GNUNET_CADET_LocalData *message)
662 uint16_t type; 657 uint16_t type;
663 int fwd; 658 int fwd;
664 659
665 ch = find_channel (h, message->ccn); 660 ch = find_channel(h, message->ccn);
666 if (NULL == ch) 661 if (NULL == ch)
667 { 662 {
668 LOG (GNUNET_ERROR_TYPE_DEBUG, 663 LOG(GNUNET_ERROR_TYPE_DEBUG,
669 "Unknown channel %X for incoming data (recently closed?)\n", 664 "Unknown channel %X for incoming data (recently closed?)\n",
670 ntohl (message->ccn.channel_of_client)); 665 ntohl(message->ccn.channel_of_client));
671 return; 666 return;
672 } 667 }
673 668
674 payload = (const struct GNUNET_MessageHeader *) &message[1]; 669 payload = (const struct GNUNET_MessageHeader *)&message[1];
675 type = ntohs (payload->type); 670 type = ntohs(payload->type);
676 fwd = ntohl (ch->ccn.channel_of_client) <= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI; 671 fwd = ntohl(ch->ccn.channel_of_client) <= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
677 LOG (GNUNET_ERROR_TYPE_DEBUG, 672 LOG(GNUNET_ERROR_TYPE_DEBUG,
678 "Got a %s data on channel %s [%X] of type %u\n", 673 "Got a %s data on channel %s [%X] of type %u\n",
679 fwd ? "FWD" : "BWD", 674 fwd ? "FWD" : "BWD",
680 GNUNET_i2s (&ch->peer), 675 GNUNET_i2s(&ch->peer),
681 ntohl (message->ccn.channel_of_client), 676 ntohl(message->ccn.channel_of_client),
682 type); 677 type);
683 GNUNET_MQ_inject_message (ch->mq, payload); 678 GNUNET_MQ_inject_message(ch->mq, payload);
684} 679}
685 680
686 681
@@ -692,37 +687,37 @@ handle_local_data (void *cls, const struct GNUNET_CADET_LocalData *message)
692 * @param message Message itself. 687 * @param message Message itself.
693 */ 688 */
694static void 689static void
695handle_local_ack (void *cls, const struct GNUNET_CADET_LocalAck *message) 690handle_local_ack(void *cls, const struct GNUNET_CADET_LocalAck *message)
696{ 691{
697 struct GNUNET_CADET_Handle *h = cls; 692 struct GNUNET_CADET_Handle *h = cls;
698 struct GNUNET_CADET_Channel *ch; 693 struct GNUNET_CADET_Channel *ch;
699 694
700 ch = find_channel (h, message->ccn); 695 ch = find_channel(h, message->ccn);
701 if (NULL == ch) 696 if (NULL == ch)
702 { 697 {
703 LOG (GNUNET_ERROR_TYPE_DEBUG, 698 LOG(GNUNET_ERROR_TYPE_DEBUG,
704 "ACK on unknown channel %X\n", 699 "ACK on unknown channel %X\n",
705 ntohl (message->ccn.channel_of_client)); 700 ntohl(message->ccn.channel_of_client));
706 return; 701 return;
707 } 702 }
708 ch->allow_send++; 703 ch->allow_send++;
709 LOG (GNUNET_ERROR_TYPE_DEBUG, 704 LOG(GNUNET_ERROR_TYPE_DEBUG,
710 "Got an ACK on mq channel %X (peer %s); new window size is %u!\n", 705 "Got an ACK on mq channel %X (peer %s); new window size is %u!\n",
711 ntohl (ch->ccn.channel_of_client), 706 ntohl(ch->ccn.channel_of_client),
712 GNUNET_i2s (&ch->peer), 707 GNUNET_i2s(&ch->peer),
713 ch->allow_send); 708 ch->allow_send);
714 if (NULL == ch->pending_env) 709 if (NULL == ch->pending_env)
715 { 710 {
716 LOG (GNUNET_ERROR_TYPE_DEBUG, 711 LOG(GNUNET_ERROR_TYPE_DEBUG,
717 "Got an ACK on mq channel %X, allow send now %u!\n", 712 "Got an ACK on mq channel %X, allow send now %u!\n",
718 ntohl (ch->ccn.channel_of_client), 713 ntohl(ch->ccn.channel_of_client),
719 ch->allow_send); 714 ch->allow_send);
720 notify_window_size (ch); 715 notify_window_size(ch);
721 return; 716 return;
722 } 717 }
723 if (NULL != ch->mq_cont) 718 if (NULL != ch->mq_cont)
724 return; /* already working on it! */ 719 return; /* already working on it! */
725 ch->mq_cont = GNUNET_SCHEDULER_add_now (&cadet_mq_send_now, ch); 720 ch->mq_cont = GNUNET_SCHEDULER_add_now(&cadet_mq_send_now, ch);
726} 721}
727 722
728 723
@@ -736,16 +731,16 @@ handle_local_ack (void *cls, const struct GNUNET_CADET_LocalAck *message)
736 * @return #GNUNET_OK (continue to iterate) 731 * @return #GNUNET_OK (continue to iterate)
737 */ 732 */
738static int 733static int
739destroy_channel_cb (void *cls, uint32_t cid, void *value) 734destroy_channel_cb(void *cls, uint32_t cid, void *value)
740{ 735{
741 /* struct GNUNET_CADET_Handle *handle = cls; */ 736 /* struct GNUNET_CADET_Handle *handle = cls; */
742 struct GNUNET_CADET_Channel *ch = value; 737 struct GNUNET_CADET_Channel *ch = value;
743 738
744 (void) cls; 739 (void)cls;
745 (void) cid; 740 (void)cid;
746 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 741 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
747 "Destroying channel due to GNUNET_CADET_disconnect()\n"); 742 "Destroying channel due to GNUNET_CADET_disconnect()\n");
748 destroy_channel (ch); 743 destroy_channel(ch);
749 return GNUNET_OK; 744 return GNUNET_OK;
750} 745}
751 746
@@ -759,17 +754,17 @@ destroy_channel_cb (void *cls, uint32_t cid, void *value)
759 * @param error error code 754 * @param error error code
760 */ 755 */
761static void 756static void
762handle_mq_error (void *cls, enum GNUNET_MQ_Error error) 757handle_mq_error(void *cls, enum GNUNET_MQ_Error error)
763{ 758{
764 struct GNUNET_CADET_Handle *h = cls; 759 struct GNUNET_CADET_Handle *h = cls;
765 760
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %u\n", error); 761 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "MQ ERROR: %u\n", error);
767 GNUNET_CONTAINER_multihashmap32_iterate (h->channels, &destroy_channel_cb, h); 762 GNUNET_CONTAINER_multihashmap32_iterate(h->channels, &destroy_channel_cb, h);
768 GNUNET_MQ_destroy (h->mq); 763 GNUNET_MQ_destroy(h->mq);
769 h->mq = NULL; 764 h->mq = NULL;
770 GNUNET_assert (NULL == h->reconnect_task); 765 GNUNET_assert(NULL == h->reconnect_task);
771 h->reconnect_task = 766 h->reconnect_task =
772 GNUNET_SCHEDULER_add_delayed (h->reconnect_time, &reconnect_cbk, h); 767 GNUNET_SCHEDULER_add_delayed(h->reconnect_time, &reconnect_cbk, h);
773} 768}
774 769
775 770
@@ -780,30 +775,30 @@ handle_mq_error (void *cls, enum GNUNET_MQ_Error error)
780 * @param h handle to the cadet 775 * @param h handle to the cadet
781 */ 776 */
782static void 777static void
783reconnect (struct GNUNET_CADET_Handle *h) 778reconnect(struct GNUNET_CADET_Handle *h)
784{ 779{
785 struct GNUNET_MQ_MessageHandler handlers[] = 780 struct GNUNET_MQ_MessageHandler handlers[] =
786 {GNUNET_MQ_hd_fixed_size (channel_created, 781 { GNUNET_MQ_hd_fixed_size(channel_created,
787 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE, 782 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
788 struct GNUNET_CADET_LocalChannelCreateMessage, 783 struct GNUNET_CADET_LocalChannelCreateMessage,
789 h), 784 h),
790 GNUNET_MQ_hd_fixed_size (channel_destroy, 785 GNUNET_MQ_hd_fixed_size(channel_destroy,
791 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY, 786 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
792 struct GNUNET_CADET_LocalChannelDestroyMessage, 787 struct GNUNET_CADET_LocalChannelDestroyMessage,
793 h), 788 h),
794 GNUNET_MQ_hd_var_size (local_data, 789 GNUNET_MQ_hd_var_size(local_data,
795 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 790 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
796 struct GNUNET_CADET_LocalData, 791 struct GNUNET_CADET_LocalData,
792 h),
793 GNUNET_MQ_hd_fixed_size(local_ack,
794 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
795 struct GNUNET_CADET_LocalAck,
797 h), 796 h),
798 GNUNET_MQ_hd_fixed_size (local_ack, 797 GNUNET_MQ_handler_end() };
799 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
800 struct GNUNET_CADET_LocalAck,
801 h),
802 GNUNET_MQ_handler_end ()};
803 798
804 GNUNET_assert (NULL == h->mq); 799 GNUNET_assert(NULL == h->mq);
805 h->mq = 800 h->mq =
806 GNUNET_CLIENT_connect (h->cfg, "cadet", handlers, &handle_mq_error, h); 801 GNUNET_CLIENT_connect(h->cfg, "cadet", handlers, &handle_mq_error, h);
807} 802}
808 803
809 804
@@ -817,16 +812,16 @@ reconnect (struct GNUNET_CADET_Handle *h)
817 * @return #GNUNET_OK (continue to iterate) 812 * @return #GNUNET_OK (continue to iterate)
818 */ 813 */
819static int 814static int
820destroy_port_cb (void *cls, const struct GNUNET_HashCode *id, void *value) 815destroy_port_cb(void *cls, const struct GNUNET_HashCode *id, void *value)
821{ 816{
822 /* struct GNUNET_CADET_Handle *handle = cls; */ 817 /* struct GNUNET_CADET_Handle *handle = cls; */
823 struct GNUNET_CADET_Port *port = value; 818 struct GNUNET_CADET_Port *port = value;
824 819
825 (void) cls; 820 (void)cls;
826 (void) id; 821 (void)id;
827 /* This is a warning, the app should have cleanly closed all open ports */ 822 /* This is a warning, the app should have cleanly closed all open ports */
828 GNUNET_break (0); 823 GNUNET_break(0);
829 GNUNET_CADET_close_port (port); 824 GNUNET_CADET_close_port(port);
830 return GNUNET_OK; 825 return GNUNET_OK;
831} 826}
832 827
@@ -840,29 +835,29 @@ destroy_port_cb (void *cls, const struct GNUNET_HashCode *id, void *value)
840 * @param handle connection to cadet to disconnect 835 * @param handle connection to cadet to disconnect
841 */ 836 */
842void 837void
843GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle) 838GNUNET_CADET_disconnect(struct GNUNET_CADET_Handle *handle)
844{ 839{
845 GNUNET_CONTAINER_multihashmap_iterate (handle->ports, 840 GNUNET_CONTAINER_multihashmap_iterate(handle->ports,
846 &destroy_port_cb, 841 &destroy_port_cb,
847 handle); 842 handle);
848 GNUNET_CONTAINER_multihashmap_destroy (handle->ports); 843 GNUNET_CONTAINER_multihashmap_destroy(handle->ports);
849 handle->ports = NULL; 844 handle->ports = NULL;
850 GNUNET_CONTAINER_multihashmap32_iterate (handle->channels, 845 GNUNET_CONTAINER_multihashmap32_iterate(handle->channels,
851 &destroy_channel_cb, 846 &destroy_channel_cb,
852 handle); 847 handle);
853 GNUNET_CONTAINER_multihashmap32_destroy (handle->channels); 848 GNUNET_CONTAINER_multihashmap32_destroy(handle->channels);
854 handle->channels = NULL; 849 handle->channels = NULL;
855 if (NULL != handle->mq) 850 if (NULL != handle->mq)
856 { 851 {
857 GNUNET_MQ_destroy (handle->mq); 852 GNUNET_MQ_destroy(handle->mq);
858 handle->mq = NULL; 853 handle->mq = NULL;
859 } 854 }
860 if (NULL != handle->reconnect_task) 855 if (NULL != handle->reconnect_task)
861 { 856 {
862 GNUNET_SCHEDULER_cancel (handle->reconnect_task); 857 GNUNET_SCHEDULER_cancel(handle->reconnect_task);
863 handle->reconnect_task = NULL; 858 handle->reconnect_task = NULL;
864 } 859 }
865 GNUNET_free (handle); 860 GNUNET_free(handle);
866} 861}
867 862
868 863
@@ -873,22 +868,22 @@ GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
873 * @param p Port handle. 868 * @param p Port handle.
874 */ 869 */
875void 870void
876GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p) 871GNUNET_CADET_close_port(struct GNUNET_CADET_Port *p)
877{ 872{
878 GNUNET_assert ( 873 GNUNET_assert(
879 GNUNET_YES == 874 GNUNET_YES ==
880 GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports, &p->id, p)); 875 GNUNET_CONTAINER_multihashmap_remove(p->cadet->ports, &p->id, p));
881 if (NULL != p->cadet->mq) 876 if (NULL != p->cadet->mq)
882 { 877 {
883 struct GNUNET_CADET_PortMessage *msg; 878 struct GNUNET_CADET_PortMessage *msg;
884 struct GNUNET_MQ_Envelope *env; 879 struct GNUNET_MQ_Envelope *env;
885 880
886 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE); 881 env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE);
887 msg->port = p->id; 882 msg->port = p->id;
888 GNUNET_MQ_send (p->cadet->mq, env); 883 GNUNET_MQ_send(p->cadet->mq, env);
889 } 884 }
890 GNUNET_free_non_null (p->handlers); 885 GNUNET_free_non_null(p->handlers);
891 GNUNET_free (p); 886 GNUNET_free(p);
892} 887}
893 888
894 889
@@ -902,22 +897,22 @@ GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p)
902 * @param channel Channel handle, becomes invalid after this call. 897 * @param channel Channel handle, becomes invalid after this call.
903 */ 898 */
904void 899void
905GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel) 900GNUNET_CADET_channel_destroy(struct GNUNET_CADET_Channel *channel)
906{ 901{
907 struct GNUNET_CADET_Handle *h = channel->cadet; 902 struct GNUNET_CADET_Handle *h = channel->cadet;
908 struct GNUNET_CADET_LocalChannelDestroyMessage *msg; 903 struct GNUNET_CADET_LocalChannelDestroyMessage *msg;
909 struct GNUNET_MQ_Envelope *env; 904 struct GNUNET_MQ_Envelope *env;
910 905
911 if (NULL != h->mq) 906 if (NULL != h->mq)
912 { 907 {
913 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY); 908 env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
914 msg->ccn = channel->ccn; 909 msg->ccn = channel->ccn;
915 GNUNET_MQ_send (h->mq, env); 910 GNUNET_MQ_send(h->mq, env);
916 } 911 }
917 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 912 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
918 "Destroying channel due to GNUNET_CADET_channel_destroy()\n"); 913 "Destroying channel due to GNUNET_CADET_channel_destroy()\n");
919 channel->disconnects = NULL; 914 channel->disconnects = NULL;
920 destroy_channel (channel); 915 destroy_channel(channel);
921} 916}
922 917
923 918
@@ -931,18 +926,19 @@ GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
931 * @return Union with an answer to the query. 926 * @return Union with an answer to the query.
932 */ 927 */
933const union GNUNET_CADET_ChannelInfo * 928const union GNUNET_CADET_ChannelInfo *
934GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel, 929GNUNET_CADET_channel_get_info(struct GNUNET_CADET_Channel *channel,
935 enum GNUNET_CADET_ChannelInfoOption option, 930 enum GNUNET_CADET_ChannelInfoOption option,
936 ...) 931 ...)
937{ 932{
938 switch (option) 933 switch (option)
939 { 934 {
940 case GNUNET_CADET_OPTION_PEER: 935 case GNUNET_CADET_OPTION_PEER:
941 return (const union GNUNET_CADET_ChannelInfo *) &channel->peer; 936 return (const union GNUNET_CADET_ChannelInfo *)&channel->peer;
942 default: 937
943 GNUNET_break (0); 938 default:
944 return NULL; 939 GNUNET_break(0);
945 } 940 return NULL;
941 }
946} 942}
947 943
948 944
@@ -952,17 +948,17 @@ GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
952 * @param ch Channel on which to send the ACK. 948 * @param ch Channel on which to send the ACK.
953 */ 949 */
954void 950void
955GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel) 951GNUNET_CADET_receive_done(struct GNUNET_CADET_Channel *channel)
956{ 952{
957 struct GNUNET_CADET_LocalAck *msg; 953 struct GNUNET_CADET_LocalAck *msg;
958 struct GNUNET_MQ_Envelope *env; 954 struct GNUNET_MQ_Envelope *env;
959 955
960 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK); 956 env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
961 LOG (GNUNET_ERROR_TYPE_DEBUG, 957 LOG(GNUNET_ERROR_TYPE_DEBUG,
962 "Sending ACK on channel %X\n", 958 "Sending ACK on channel %X\n",
963 ntohl (channel->ccn.channel_of_client)); 959 ntohl(channel->ccn.channel_of_client));
964 msg->ccn = channel->ccn; 960 msg->ccn = channel->ccn;
965 GNUNET_MQ_send (channel->cadet->mq, env); 961 GNUNET_MQ_send(channel->cadet->mq, env);
966} 962}
967 963
968 964
@@ -974,23 +970,23 @@ GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
974 * @return Handle to the cadet service NULL on error. 970 * @return Handle to the cadet service NULL on error.
975 */ 971 */
976struct GNUNET_CADET_Handle * 972struct GNUNET_CADET_Handle *
977GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) 973GNUNET_CADET_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
978{ 974{
979 struct GNUNET_CADET_Handle *h; 975 struct GNUNET_CADET_Handle *h;
980 976
981 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect()\n"); 977 LOG(GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect()\n");
982 h = GNUNET_new (struct GNUNET_CADET_Handle); 978 h = GNUNET_new(struct GNUNET_CADET_Handle);
983 h->cfg = cfg; 979 h->cfg = cfg;
984 h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES); 980 h->ports = GNUNET_CONTAINER_multihashmap_create(4, GNUNET_YES);
985 h->channels = GNUNET_CONTAINER_multihashmap32_create (4); 981 h->channels = GNUNET_CONTAINER_multihashmap32_create(4);
986 reconnect (h); 982 reconnect(h);
987 if (NULL == h->mq) 983 if (NULL == h->mq)
988 { 984 {
989 GNUNET_break (0); 985 GNUNET_break(0);
990 GNUNET_CADET_disconnect (h); 986 GNUNET_CADET_disconnect(h);
991 return NULL; 987 return NULL;
992 } 988 }
993 h->next_ccn.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI); 989 h->next_ccn.channel_of_client = htonl(GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
994 return h; 990 return h;
995} 991}
996 992
@@ -1008,41 +1004,41 @@ GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
1008 * @return Port handle, NULL if port is in use 1004 * @return Port handle, NULL if port is in use
1009 */ 1005 */
1010struct GNUNET_CADET_Port * 1006struct GNUNET_CADET_Port *
1011GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h, 1007GNUNET_CADET_open_port(struct GNUNET_CADET_Handle *h,
1012 const struct GNUNET_HashCode *port, 1008 const struct GNUNET_HashCode *port,
1013 GNUNET_CADET_ConnectEventHandler connects, 1009 GNUNET_CADET_ConnectEventHandler connects,
1014 void *connects_cls, 1010 void *connects_cls,
1015 GNUNET_CADET_WindowSizeEventHandler window_changes, 1011 GNUNET_CADET_WindowSizeEventHandler window_changes,
1016 GNUNET_CADET_DisconnectEventHandler disconnects, 1012 GNUNET_CADET_DisconnectEventHandler disconnects,
1017 const struct GNUNET_MQ_MessageHandler *handlers) 1013 const struct GNUNET_MQ_MessageHandler *handlers)
1018{ 1014{
1019 struct GNUNET_CADET_Port *p; 1015 struct GNUNET_CADET_Port *p;
1020 1016
1021 GNUNET_assert (NULL != connects); 1017 GNUNET_assert(NULL != connects);
1022 GNUNET_assert (NULL != disconnects); 1018 GNUNET_assert(NULL != disconnects);
1023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1019 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1024 "Listening to CADET port %s\n", 1020 "Listening to CADET port %s\n",
1025 GNUNET_h2s (port)); 1021 GNUNET_h2s(port));
1026 1022
1027 p = GNUNET_new (struct GNUNET_CADET_Port); 1023 p = GNUNET_new(struct GNUNET_CADET_Port);
1028 p->cadet = h; 1024 p->cadet = h;
1029 p->id = *port; 1025 p->id = *port;
1030 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put ( 1026 if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
1031 h->ports, 1027 h->ports,
1032 &p->id, 1028 &p->id,
1033 p, 1029 p,
1034 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 1030 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1035 { 1031 {
1036 GNUNET_free (p); 1032 GNUNET_free(p);
1037 return NULL; 1033 return NULL;
1038 } 1034 }
1039 p->connects = connects; 1035 p->connects = connects;
1040 p->cls = connects_cls; 1036 p->cls = connects_cls;
1041 p->window_changes = window_changes; 1037 p->window_changes = window_changes;
1042 p->disconnects = disconnects; 1038 p->disconnects = disconnects;
1043 p->handlers = GNUNET_MQ_copy_handlers (handlers); 1039 p->handlers = GNUNET_MQ_copy_handlers(handlers);
1044 1040
1045 GNUNET_assert (GNUNET_OK == open_port_cb (h, &p->id, p)); 1041 GNUNET_assert(GNUNET_OK == open_port_cb(h, &p->id, p));
1046 return p; 1042 return p;
1047} 1043}
1048 1044
@@ -1066,45 +1062,45 @@ GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h,
1066 * @return Handle to the channel. 1062 * @return Handle to the channel.
1067 */ 1063 */
1068struct GNUNET_CADET_Channel * 1064struct GNUNET_CADET_Channel *
1069GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h, 1065GNUNET_CADET_channel_create(struct GNUNET_CADET_Handle *h,
1070 void *channel_cls, 1066 void *channel_cls,
1071 const struct GNUNET_PeerIdentity *destination, 1067 const struct GNUNET_PeerIdentity *destination,
1072 const struct GNUNET_HashCode *port, 1068 const struct GNUNET_HashCode *port,
1073 GNUNET_CADET_WindowSizeEventHandler window_changes, 1069 GNUNET_CADET_WindowSizeEventHandler window_changes,
1074 GNUNET_CADET_DisconnectEventHandler disconnects, 1070 GNUNET_CADET_DisconnectEventHandler disconnects,
1075 const struct GNUNET_MQ_MessageHandler *handlers) 1071 const struct GNUNET_MQ_MessageHandler *handlers)
1076{ 1072{
1077 struct GNUNET_CADET_Channel *ch; 1073 struct GNUNET_CADET_Channel *ch;
1078 struct GNUNET_CADET_LocalChannelCreateMessage *msg; 1074 struct GNUNET_CADET_LocalChannelCreateMessage *msg;
1079 struct GNUNET_MQ_Envelope *env; 1075 struct GNUNET_MQ_Envelope *env;
1080 1076
1081 GNUNET_assert (NULL != disconnects); 1077 GNUNET_assert(NULL != disconnects);
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1078 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1083 "Creating channel to peer %s at port %s\n", 1079 "Creating channel to peer %s at port %s\n",
1084 GNUNET_i2s (destination), 1080 GNUNET_i2s(destination),
1085 GNUNET_h2s (port)); 1081 GNUNET_h2s(port));
1086 ch = create_channel (h, NULL); 1082 ch = create_channel(h, NULL);
1087 ch->ctx = channel_cls; 1083 ch->ctx = channel_cls;
1088 ch->peer = *destination; 1084 ch->peer = *destination;
1089 ch->window_changes = window_changes; 1085 ch->window_changes = window_changes;
1090 ch->disconnects = disconnects; 1086 ch->disconnects = disconnects;
1091 1087
1092 /* Create MQ for channel */ 1088 /* Create MQ for channel */
1093 ch->mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl, 1089 ch->mq = GNUNET_MQ_queue_for_callbacks(&cadet_mq_send_impl,
1094 &cadet_mq_destroy_impl, 1090 &cadet_mq_destroy_impl,
1095 &cadet_mq_cancel_impl, 1091 &cadet_mq_cancel_impl,
1096 ch, 1092 ch,
1097 handlers, 1093 handlers,
1098 &cadet_mq_error_handler, 1094 &cadet_mq_error_handler,
1099 ch); 1095 ch);
1100 GNUNET_MQ_set_handlers_closure (ch->mq, channel_cls); 1096 GNUNET_MQ_set_handlers_closure(ch->mq, channel_cls);
1101 1097
1102 /* Request channel creation to service */ 1098 /* Request channel creation to service */
1103 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE); 1099 env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
1104 msg->ccn = ch->ccn; 1100 msg->ccn = ch->ccn;
1105 msg->port = *port; 1101 msg->port = *port;
1106 msg->peer = *destination; 1102 msg->peer = *destination;
1107 GNUNET_MQ_send (h->mq, env); 1103 GNUNET_MQ_send(h->mq, env);
1108 return ch; 1104 return ch;
1109} 1105}
1110 1106
@@ -1117,7 +1113,7 @@ GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1117 * @return NULL if @a channel is not yet connected. 1113 * @return NULL if @a channel is not yet connected.
1118 */ 1114 */
1119struct GNUNET_MQ_Handle * 1115struct GNUNET_MQ_Handle *
1120GNUNET_CADET_get_mq (const struct GNUNET_CADET_Channel *channel) 1116GNUNET_CADET_get_mq(const struct GNUNET_CADET_Channel *channel)
1121{ 1117{
1122 return channel->mq; 1118 return channel->mq;
1123} 1119}
diff --git a/src/cadet/cadet_api_get_channel.c b/src/cadet/cadet_api_get_channel.c
index 81f0691f1..5479f4238 100644
--- a/src/cadet/cadet_api_get_channel.c
+++ b/src/cadet/cadet_api_get_channel.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file cadet/cadet_api_get_channel.c 21 * @file cadet/cadet_api_get_channel.c
22 * @brief cadet api: client implementation of cadet service 22 * @brief cadet api: client implementation of cadet service
@@ -34,9 +34,7 @@
34/** 34/**
35 * Operation handle. 35 * Operation handle.
36 */ 36 */
37struct GNUNET_CADET_ChannelMonitor 37struct GNUNET_CADET_ChannelMonitor {
38{
39
40 /** 38 /**
41 * Channel callback. 39 * Channel callback.
42 */ 40 */
@@ -56,7 +54,7 @@ struct GNUNET_CADET_ChannelMonitor
56 * Message queue to talk to CADET service. 54 * Message queue to talk to CADET service.
57 */ 55 */
58 struct GNUNET_MQ_Handle *mq; 56 struct GNUNET_MQ_Handle *mq;
59 57
60 /** 58 /**
61 * Task to reconnect. 59 * Task to reconnect.
62 */ 60 */
@@ -71,7 +69,6 @@ struct GNUNET_CADET_ChannelMonitor
71 * Peer we want information about. 69 * Peer we want information about.
72 */ 70 */
73 struct GNUNET_PeerIdentity peer; 71 struct GNUNET_PeerIdentity peer;
74
75}; 72};
76 73
77 74
@@ -84,11 +81,11 @@ struct GNUNET_CADET_ChannelMonitor
84 * #GNUNET_SYSERR otherwise 81 * #GNUNET_SYSERR otherwise
85 */ 82 */
86static int 83static int
87check_channel_info (void *cls, 84check_channel_info(void *cls,
88 const struct GNUNET_CADET_ChannelInfoMessage *message) 85 const struct GNUNET_CADET_ChannelInfoMessage *message)
89{ 86{
90 (void) cls; 87 (void)cls;
91 88
92 return GNUNET_OK; 89 return GNUNET_OK;
93} 90}
94 91
@@ -96,39 +93,39 @@ check_channel_info (void *cls,
96/** 93/**
97 * Process a local peer info reply, pass info to the user. 94 * Process a local peer info reply, pass info to the user.
98 * 95 *
99 * @param cls Closure 96 * @param cls Closure
100 * @param message Message itself. 97 * @param message Message itself.
101 */ 98 */
102static void 99static void
103handle_channel_info (void *cls, 100handle_channel_info(void *cls,
104 const struct GNUNET_CADET_ChannelInfoMessage *message) 101 const struct GNUNET_CADET_ChannelInfoMessage *message)
105{ 102{
106 struct GNUNET_CADET_ChannelMonitor *cm = cls; 103 struct GNUNET_CADET_ChannelMonitor *cm = cls;
107 struct GNUNET_CADET_ChannelInternals ci; 104 struct GNUNET_CADET_ChannelInternals ci;
108 105
109 ci.root = message->root; 106 ci.root = message->root;
110 ci.dest = message->dest; 107 ci.dest = message->dest;
111 cm->channel_cb (cm->channel_cb_cls, 108 cm->channel_cb(cm->channel_cb_cls,
112 &ci); 109 &ci);
113 GNUNET_CADET_get_channel_cancel (cm); 110 GNUNET_CADET_get_channel_cancel(cm);
114} 111}
115 112
116 113
117/** 114/**
118 * Process a local peer info reply, pass info to the user. 115 * Process a local peer info reply, pass info to the user.
119 * 116 *
120 * @param cls Closure 117 * @param cls Closure
121 * @param message Message itself. 118 * @param message Message itself.
122 */ 119 */
123static void 120static void
124handle_channel_info_end (void *cls, 121handle_channel_info_end(void *cls,
125 const struct GNUNET_MessageHeader *message) 122 const struct GNUNET_MessageHeader *message)
126{ 123{
127 struct GNUNET_CADET_ChannelMonitor *cm = cls; 124 struct GNUNET_CADET_ChannelMonitor *cm = cls;
128 125
129 cm->channel_cb (cm->channel_cb_cls, 126 cm->channel_cb(cm->channel_cb_cls,
130 NULL); 127 NULL);
131 GNUNET_CADET_get_channel_cancel (cm); 128 GNUNET_CADET_get_channel_cancel(cm);
132} 129}
133 130
134 131
@@ -138,7 +135,7 @@ handle_channel_info_end (void *cls,
138 * @param cls a `struct GNUNET_CADET_ChannelMonitor` operation 135 * @param cls a `struct GNUNET_CADET_ChannelMonitor` operation
139 */ 136 */
140static void 137static void
141reconnect (void *cls); 138reconnect(void *cls);
142 139
143 140
144/** 141/**
@@ -148,18 +145,18 @@ reconnect (void *cls);
148 * @param error error code from MQ 145 * @param error error code from MQ
149 */ 146 */
150static void 147static void
151error_handler (void *cls, 148error_handler(void *cls,
152 enum GNUNET_MQ_Error error) 149 enum GNUNET_MQ_Error error)
153{ 150{
154 struct GNUNET_CADET_ChannelMonitor *cm = cls; 151 struct GNUNET_CADET_ChannelMonitor *cm = cls;
155 152
156 GNUNET_MQ_destroy (cm->mq); 153 GNUNET_MQ_destroy(cm->mq);
157 cm->mq = NULL; 154 cm->mq = NULL;
158 cm->backoff = GNUNET_TIME_randomized_backoff (cm->backoff, 155 cm->backoff = GNUNET_TIME_randomized_backoff(cm->backoff,
159 GNUNET_TIME_UNIT_MINUTES); 156 GNUNET_TIME_UNIT_MINUTES);
160 cm->reconnect_task = GNUNET_SCHEDULER_add_delayed (cm->backoff, 157 cm->reconnect_task = GNUNET_SCHEDULER_add_delayed(cm->backoff,
161 &reconnect, 158 &reconnect,
162 cm); 159 cm);
163} 160}
164 161
165 162
@@ -169,36 +166,36 @@ error_handler (void *cls,
169 * @param cls a `struct GNUNET_CADET_ChannelMonitor` operation 166 * @param cls a `struct GNUNET_CADET_ChannelMonitor` operation
170 */ 167 */
171static void 168static void
172reconnect (void *cls) 169reconnect(void *cls)
173{ 170{
174 struct GNUNET_CADET_ChannelMonitor *cm = cls; 171 struct GNUNET_CADET_ChannelMonitor *cm = cls;
175 struct GNUNET_MQ_MessageHandler handlers[] = { 172 struct GNUNET_MQ_MessageHandler handlers[] = {
176 GNUNET_MQ_hd_fixed_size (channel_info_end, 173 GNUNET_MQ_hd_fixed_size(channel_info_end,
177 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL_END, 174 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL_END,
178 struct GNUNET_MessageHeader, 175 struct GNUNET_MessageHeader,
179 cm), 176 cm),
180 GNUNET_MQ_hd_var_size (channel_info, 177 GNUNET_MQ_hd_var_size(channel_info,
181 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL, 178 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL,
182 struct GNUNET_CADET_ChannelInfoMessage, 179 struct GNUNET_CADET_ChannelInfoMessage,
183 cm), 180 cm),
184 GNUNET_MQ_handler_end () 181 GNUNET_MQ_handler_end()
185 }; 182 };
186 struct GNUNET_CADET_RequestChannelInfoMessage *msg; 183 struct GNUNET_CADET_RequestChannelInfoMessage *msg;
187 struct GNUNET_MQ_Envelope *env; 184 struct GNUNET_MQ_Envelope *env;
188 185
189 cm->reconnect_task = NULL; 186 cm->reconnect_task = NULL;
190 cm->mq = GNUNET_CLIENT_connect (cm->cfg, 187 cm->mq = GNUNET_CLIENT_connect(cm->cfg,
191 "cadet", 188 "cadet",
192 handlers, 189 handlers,
193 &error_handler, 190 &error_handler,
194 cm); 191 cm);
195 if (NULL == cm->mq) 192 if (NULL == cm->mq)
196 return; 193 return;
197 env = GNUNET_MQ_msg (msg, 194 env = GNUNET_MQ_msg(msg,
198 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_CHANNEL); 195 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_CHANNEL);
199 msg->target = cm->peer; 196 msg->target = cm->peer;
200 GNUNET_MQ_send (cm->mq, 197 GNUNET_MQ_send(cm->mq,
201 env); 198 env);
202} 199}
203 200
204 201
@@ -212,29 +209,29 @@ reconnect (void *cls)
212 * @return NULL on error 209 * @return NULL on error
213 */ 210 */
214struct GNUNET_CADET_ChannelMonitor * 211struct GNUNET_CADET_ChannelMonitor *
215GNUNET_CADET_get_channel (const struct GNUNET_CONFIGURATION_Handle *cfg, 212GNUNET_CADET_get_channel(const struct GNUNET_CONFIGURATION_Handle *cfg,
216 struct GNUNET_PeerIdentity *peer, 213 struct GNUNET_PeerIdentity *peer,
217 GNUNET_CADET_ChannelCB callback, 214 GNUNET_CADET_ChannelCB callback,
218 void *callback_cls) 215 void *callback_cls)
219{ 216{
220 struct GNUNET_CADET_ChannelMonitor *cm; 217 struct GNUNET_CADET_ChannelMonitor *cm;
221 218
222 if (NULL == callback) 219 if (NULL == callback)
223 { 220 {
224 GNUNET_break (0); 221 GNUNET_break(0);
225 return NULL; 222 return NULL;
226 } 223 }
227 cm = GNUNET_new (struct GNUNET_CADET_ChannelMonitor); 224 cm = GNUNET_new(struct GNUNET_CADET_ChannelMonitor);
228 cm->channel_cb = callback; 225 cm->channel_cb = callback;
229 cm->channel_cb_cls = callback_cls; 226 cm->channel_cb_cls = callback_cls;
230 cm->cfg = cfg; 227 cm->cfg = cfg;
231 cm->peer = *peer; 228 cm->peer = *peer;
232 reconnect (cm); 229 reconnect(cm);
233 if (NULL == cm->mq) 230 if (NULL == cm->mq)
234 { 231 {
235 GNUNET_free (cm); 232 GNUNET_free(cm);
236 return NULL; 233 return NULL;
237 } 234 }
238 return cm; 235 return cm;
239} 236}
240 237
@@ -246,15 +243,15 @@ GNUNET_CADET_get_channel (const struct GNUNET_CONFIGURATION_Handle *cfg,
246 * @return Closure that was given to #GNUNET_CADET_get_channel(). 243 * @return Closure that was given to #GNUNET_CADET_get_channel().
247 */ 244 */
248void * 245void *
249GNUNET_CADET_get_channel_cancel (struct GNUNET_CADET_ChannelMonitor *cm) 246GNUNET_CADET_get_channel_cancel(struct GNUNET_CADET_ChannelMonitor *cm)
250{ 247{
251 void *ret = cm->channel_cb_cls; 248 void *ret = cm->channel_cb_cls;
252 249
253 if (NULL != cm->mq) 250 if (NULL != cm->mq)
254 GNUNET_MQ_destroy (cm->mq); 251 GNUNET_MQ_destroy(cm->mq);
255 if (NULL != cm->reconnect_task) 252 if (NULL != cm->reconnect_task)
256 GNUNET_SCHEDULER_cancel (cm->reconnect_task); 253 GNUNET_SCHEDULER_cancel(cm->reconnect_task);
257 GNUNET_free (cm); 254 GNUNET_free(cm);
258 return ret; 255 return ret;
259} 256}
260 257
diff --git a/src/cadet/cadet_api_get_path.c b/src/cadet/cadet_api_get_path.c
index fcc79c3d5..0f18014c3 100644
--- a/src/cadet/cadet_api_get_path.c
+++ b/src/cadet/cadet_api_get_path.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file cadet/cadet_api_get_path.c 21 * @file cadet/cadet_api_get_path.c
22 * @brief cadet api: client implementation of cadet service 22 * @brief cadet api: client implementation of cadet service
@@ -34,9 +34,7 @@
34/** 34/**
35 * Operation handle. 35 * Operation handle.
36 */ 36 */
37struct GNUNET_CADET_GetPath 37struct GNUNET_CADET_GetPath {
38{
39
40 /** 38 /**
41 * Monitor callback 39 * Monitor callback
42 */ 40 */
@@ -71,7 +69,6 @@ struct GNUNET_CADET_GetPath
71 * Peer we want information about. 69 * Peer we want information about.
72 */ 70 */
73 struct GNUNET_PeerIdentity id; 71 struct GNUNET_PeerIdentity id;
74
75}; 72};
76 73
77 74
@@ -84,24 +81,24 @@ struct GNUNET_CADET_GetPath
84 * #GNUNET_SYSERR otherwise 81 * #GNUNET_SYSERR otherwise
85 */ 82 */
86static int 83static int
87check_get_path (void *cls, 84check_get_path(void *cls,
88 const struct GNUNET_CADET_LocalInfoPath *message) 85 const struct GNUNET_CADET_LocalInfoPath *message)
89{ 86{
90 size_t msize = sizeof (struct GNUNET_CADET_LocalInfoPath); 87 size_t msize = sizeof(struct GNUNET_CADET_LocalInfoPath);
91 size_t esize; 88 size_t esize;
92 89
93 (void) cls; 90 (void)cls;
94 esize = ntohs (message->header.size); 91 esize = ntohs(message->header.size);
95 if (esize < msize) 92 if (esize < msize)
96 { 93 {
97 GNUNET_break (0); 94 GNUNET_break(0);
98 return GNUNET_SYSERR; 95 return GNUNET_SYSERR;
99 } 96 }
100 if (0 != ((esize - msize) % sizeof (struct GNUNET_PeerIdentity))) 97 if (0 != ((esize - msize) % sizeof(struct GNUNET_PeerIdentity)))
101 { 98 {
102 GNUNET_break (0); 99 GNUNET_break(0);
103 return GNUNET_SYSERR; 100 return GNUNET_SYSERR;
104 } 101 }
105 return GNUNET_OK; 102 return GNUNET_OK;
106} 103}
107 104
@@ -113,19 +110,19 @@ check_get_path (void *cls,
113 * @param message Message itself. 110 * @param message Message itself.
114 */ 111 */
115static void 112static void
116handle_get_path (void *cls, 113handle_get_path(void *cls,
117 const struct GNUNET_CADET_LocalInfoPath *message) 114 const struct GNUNET_CADET_LocalInfoPath *message)
118{ 115{
119 struct GNUNET_CADET_GetPath *gp = cls; 116 struct GNUNET_CADET_GetPath *gp = cls;
120 struct GNUNET_CADET_PeerPathDetail ppd; 117 struct GNUNET_CADET_PeerPathDetail ppd;
121 118
122 ppd.peer = gp->id; 119 ppd.peer = gp->id;
123 ppd.path = (const struct GNUNET_PeerIdentity *) &message[1]; 120 ppd.path = (const struct GNUNET_PeerIdentity *)&message[1];
124 ppd.target_offset = ntohl (message->off); 121 ppd.target_offset = ntohl(message->off);
125 ppd.path_length = (ntohs (message->header.size) - sizeof (*message)) 122 ppd.path_length = (ntohs(message->header.size) - sizeof(*message))
126 / sizeof (struct GNUNET_PeerIdentity); 123 / sizeof(struct GNUNET_PeerIdentity);
127 gp->path_cb (gp->path_cb_cls, 124 gp->path_cb(gp->path_cb_cls,
128 &ppd); 125 &ppd);
129} 126}
130 127
131 128
@@ -136,15 +133,15 @@ handle_get_path (void *cls,
136 * @param message Message itself. 133 * @param message Message itself.
137 */ 134 */
138static void 135static void
139handle_get_path_end (void *cls, 136handle_get_path_end(void *cls,
140 const struct GNUNET_MessageHeader *message) 137 const struct GNUNET_MessageHeader *message)
141{ 138{
142 struct GNUNET_CADET_GetPath *gp = cls; 139 struct GNUNET_CADET_GetPath *gp = cls;
143 140
144 (void) message; 141 (void)message;
145 gp->path_cb (gp->path_cb_cls, 142 gp->path_cb(gp->path_cb_cls,
146 NULL); 143 NULL);
147 GNUNET_CADET_get_path_cancel (gp); 144 GNUNET_CADET_get_path_cancel(gp);
148} 145}
149 146
150 147
@@ -154,7 +151,7 @@ handle_get_path_end (void *cls,
154 * @param cls a `struct GNUNET_CADET_GetPath` operation 151 * @param cls a `struct GNUNET_CADET_GetPath` operation
155 */ 152 */
156static void 153static void
157reconnect (void *cls); 154reconnect(void *cls);
158 155
159 156
160/** 157/**
@@ -164,18 +161,18 @@ reconnect (void *cls);
164 * @param error error code from MQ 161 * @param error error code from MQ
165 */ 162 */
166static void 163static void
167error_handler (void *cls, 164error_handler(void *cls,
168 enum GNUNET_MQ_Error error) 165 enum GNUNET_MQ_Error error)
169{ 166{
170 struct GNUNET_CADET_GetPath *gp = cls; 167 struct GNUNET_CADET_GetPath *gp = cls;
171 168
172 GNUNET_MQ_destroy (gp->mq); 169 GNUNET_MQ_destroy(gp->mq);
173 gp->mq = NULL; 170 gp->mq = NULL;
174 gp->backoff = GNUNET_TIME_randomized_backoff (gp->backoff, 171 gp->backoff = GNUNET_TIME_randomized_backoff(gp->backoff,
175 GNUNET_TIME_UNIT_MINUTES); 172 GNUNET_TIME_UNIT_MINUTES);
176 gp->reconnect_task = GNUNET_SCHEDULER_add_delayed (gp->backoff, 173 gp->reconnect_task = GNUNET_SCHEDULER_add_delayed(gp->backoff,
177 &reconnect, 174 &reconnect,
178 gp); 175 gp);
179} 176}
180 177
181 178
@@ -185,36 +182,36 @@ error_handler (void *cls,
185 * @param cls a `struct GNUNET_CADET_GetPath` operation 182 * @param cls a `struct GNUNET_CADET_GetPath` operation
186 */ 183 */
187static void 184static void
188reconnect (void *cls) 185reconnect(void *cls)
189{ 186{
190 struct GNUNET_CADET_GetPath *gp = cls; 187 struct GNUNET_CADET_GetPath *gp = cls;
191 struct GNUNET_MQ_MessageHandler handlers[] = { 188 struct GNUNET_MQ_MessageHandler handlers[] = {
192 GNUNET_MQ_hd_var_size (get_path, 189 GNUNET_MQ_hd_var_size(get_path,
193 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH, 190 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH,
194 struct GNUNET_CADET_LocalInfoPath, 191 struct GNUNET_CADET_LocalInfoPath,
195 gp), 192 gp),
196 GNUNET_MQ_hd_fixed_size (get_path_end, 193 GNUNET_MQ_hd_fixed_size(get_path_end,
197 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH_END, 194 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH_END,
198 struct GNUNET_MessageHeader, 195 struct GNUNET_MessageHeader,
199 gp), 196 gp),
200 GNUNET_MQ_handler_end () 197 GNUNET_MQ_handler_end()
201 }; 198 };
202 struct GNUNET_CADET_RequestPathInfoMessage *msg; 199 struct GNUNET_CADET_RequestPathInfoMessage *msg;
203 struct GNUNET_MQ_Envelope *env; 200 struct GNUNET_MQ_Envelope *env;
204 201
205 gp->reconnect_task = NULL; 202 gp->reconnect_task = NULL;
206 gp->mq = GNUNET_CLIENT_connect (gp->cfg, 203 gp->mq = GNUNET_CLIENT_connect(gp->cfg,
207 "cadet", 204 "cadet",
208 handlers, 205 handlers,
209 &error_handler, 206 &error_handler,
210 gp); 207 gp);
211 if (NULL == gp->mq) 208 if (NULL == gp->mq)
212 return; 209 return;
213 env = GNUNET_MQ_msg (msg, 210 env = GNUNET_MQ_msg(msg,
214 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PATH); 211 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PATH);
215 msg->peer = gp->id; 212 msg->peer = gp->id;
216 GNUNET_MQ_send (gp->mq, 213 GNUNET_MQ_send(gp->mq,
217 env); 214 env);
218} 215}
219 216
220 217
@@ -228,29 +225,29 @@ reconnect (void *cls)
228 * @return NULL on error 225 * @return NULL on error
229 */ 226 */
230struct GNUNET_CADET_GetPath * 227struct GNUNET_CADET_GetPath *
231GNUNET_CADET_get_path (const struct GNUNET_CONFIGURATION_Handle *cfg, 228GNUNET_CADET_get_path(const struct GNUNET_CONFIGURATION_Handle *cfg,
232 const struct GNUNET_PeerIdentity *id, 229 const struct GNUNET_PeerIdentity *id,
233 GNUNET_CADET_PathCB callback, 230 GNUNET_CADET_PathCB callback,
234 void *callback_cls) 231 void *callback_cls)
235{ 232{
236 struct GNUNET_CADET_GetPath *gp; 233 struct GNUNET_CADET_GetPath *gp;
237 234
238 if (NULL == callback) 235 if (NULL == callback)
239 { 236 {
240 GNUNET_break (0); 237 GNUNET_break(0);
241 return NULL; 238 return NULL;
242 } 239 }
243 gp = GNUNET_new (struct GNUNET_CADET_GetPath); 240 gp = GNUNET_new(struct GNUNET_CADET_GetPath);
244 gp->path_cb = callback; 241 gp->path_cb = callback;
245 gp->path_cb_cls = callback_cls; 242 gp->path_cb_cls = callback_cls;
246 gp->cfg = cfg; 243 gp->cfg = cfg;
247 gp->id = *id; 244 gp->id = *id;
248 reconnect (gp); 245 reconnect(gp);
249 if (NULL == gp->mq) 246 if (NULL == gp->mq)
250 { 247 {
251 GNUNET_free (gp); 248 GNUNET_free(gp);
252 return NULL; 249 return NULL;
253 } 250 }
254 return gp; 251 return gp;
255} 252}
256 253
@@ -262,15 +259,15 @@ GNUNET_CADET_get_path (const struct GNUNET_CONFIGURATION_Handle *cfg,
262 * @return closure from #GNUNET_CADET_get_path(). 259 * @return closure from #GNUNET_CADET_get_path().
263 */ 260 */
264void * 261void *
265GNUNET_CADET_get_path_cancel (struct GNUNET_CADET_GetPath *gp) 262GNUNET_CADET_get_path_cancel(struct GNUNET_CADET_GetPath *gp)
266{ 263{
267 void *ret = gp->path_cb_cls; 264 void *ret = gp->path_cb_cls;
268 265
269 if (NULL != gp->mq) 266 if (NULL != gp->mq)
270 GNUNET_MQ_destroy (gp->mq); 267 GNUNET_MQ_destroy(gp->mq);
271 if (NULL != gp->reconnect_task) 268 if (NULL != gp->reconnect_task)
272 GNUNET_SCHEDULER_cancel (gp->reconnect_task); 269 GNUNET_SCHEDULER_cancel(gp->reconnect_task);
273 GNUNET_free (gp); 270 GNUNET_free(gp);
274 return ret; 271 return ret;
275} 272}
276 273
diff --git a/src/cadet/cadet_api_helper.c b/src/cadet/cadet_api_helper.c
index 6f7634068..78e5eb45c 100644
--- a/src/cadet/cadet_api_helper.c
+++ b/src/cadet/cadet_api_helper.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file cadet/cadet_api_helper.c 21 * @file cadet/cadet_api_helper.c
22 * @brief cadet api: client implementation of cadet service 22 * @brief cadet api: client implementation of cadet service
@@ -41,15 +41,15 @@
41 * @return A GNUNET_HashCode usable for the new CADET API. 41 * @return A GNUNET_HashCode usable for the new CADET API.
42 */ 42 */
43const struct GNUNET_HashCode * 43const struct GNUNET_HashCode *
44GC_u2h (uint32_t port) 44GC_u2h(uint32_t port)
45{ 45{
46 static struct GNUNET_HashCode hash; 46 static struct GNUNET_HashCode hash;
47 47
48 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 48 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
49 "This is a transitional function, use proper crypto hashes as CADET ports\n"); 49 "This is a transitional function, use proper crypto hashes as CADET ports\n");
50 GNUNET_CRYPTO_hash (&port, 50 GNUNET_CRYPTO_hash(&port,
51 sizeof (port), 51 sizeof(port),
52 &hash); 52 &hash);
53 return &hash; 53 return &hash;
54} 54}
55 55
diff --git a/src/cadet/cadet_api_list_peers.c b/src/cadet/cadet_api_list_peers.c
index 4dc6d02f5..65e9ad4da 100644
--- a/src/cadet/cadet_api_list_peers.c
+++ b/src/cadet/cadet_api_list_peers.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file cadet/cadet_api_list_peers.c 21 * @file cadet/cadet_api_list_peers.c
22 * @brief cadet api: client implementation of cadet service 22 * @brief cadet api: client implementation of cadet service
@@ -34,9 +34,7 @@
34/** 34/**
35 * Operation handle. 35 * Operation handle.
36 */ 36 */
37struct GNUNET_CADET_PeersLister 37struct GNUNET_CADET_PeersLister {
38{
39
40 /** 38 /**
41 * Monitor callback 39 * Monitor callback
42 */ 40 */
@@ -66,7 +64,6 @@ struct GNUNET_CADET_PeersLister
66 * Backoff for reconnect attempts. 64 * Backoff for reconnect attempts.
67 */ 65 */
68 struct GNUNET_TIME_Relative backoff; 66 struct GNUNET_TIME_Relative backoff;
69
70}; 67};
71 68
72 69
@@ -77,18 +74,18 @@ struct GNUNET_CADET_PeersLister
77 * @param info Message itself. 74 * @param info Message itself.
78 */ 75 */
79static void 76static void
80handle_get_peers (void *cls, 77handle_get_peers(void *cls,
81 const struct GNUNET_CADET_LocalInfoPeers *info) 78 const struct GNUNET_CADET_LocalInfoPeers *info)
82{ 79{
83 struct GNUNET_CADET_PeersLister *pl = cls; 80 struct GNUNET_CADET_PeersLister *pl = cls;
84 struct GNUNET_CADET_PeerListEntry ple; 81 struct GNUNET_CADET_PeerListEntry ple;
85 82
86 ple.peer = info->destination; 83 ple.peer = info->destination;
87 ple.have_tunnel = (int) ntohs (info->tunnel); 84 ple.have_tunnel = (int)ntohs(info->tunnel);
88 ple.n_paths = (unsigned int) ntohs (info->paths); 85 ple.n_paths = (unsigned int)ntohs(info->paths);
89 ple.best_path_length = (unsigned int) ntohl (info->best_path_length); 86 ple.best_path_length = (unsigned int)ntohl(info->best_path_length);
90 pl->peers_cb (pl->peers_cb_cls, 87 pl->peers_cb(pl->peers_cb_cls,
91 &ple); 88 &ple);
92} 89}
93 90
94 91
@@ -99,15 +96,16 @@ handle_get_peers (void *cls,
99 * @param msg Message itself. 96 * @param msg Message itself.
100 */ 97 */
101static void 98static void
102handle_get_peers_end (void *cls, 99handle_get_peers_end(void *cls,
103 const struct GNUNET_MessageHeader *msg) 100 const struct GNUNET_MessageHeader *msg)
104{ 101{
105 struct GNUNET_CADET_PeersLister *pl = cls; 102 struct GNUNET_CADET_PeersLister *pl = cls;
106 (void) msg;
107 103
108 pl->peers_cb (pl->peers_cb_cls, 104 (void)msg;
109 NULL); 105
110 GNUNET_CADET_list_peers_cancel (pl); 106 pl->peers_cb(pl->peers_cb_cls,
107 NULL);
108 GNUNET_CADET_list_peers_cancel(pl);
111} 109}
112 110
113 111
@@ -117,7 +115,7 @@ handle_get_peers_end (void *cls,
117 * @param cls a `struct GNUNET_CADET_PeersLister` operation 115 * @param cls a `struct GNUNET_CADET_PeersLister` operation
118 */ 116 */
119static void 117static void
120reconnect (void *cls); 118reconnect(void *cls);
121 119
122 120
123/** 121/**
@@ -127,18 +125,18 @@ reconnect (void *cls);
127 * @param error error code from MQ 125 * @param error error code from MQ
128 */ 126 */
129static void 127static void
130error_handler (void *cls, 128error_handler(void *cls,
131 enum GNUNET_MQ_Error error) 129 enum GNUNET_MQ_Error error)
132{ 130{
133 struct GNUNET_CADET_PeersLister *pl = cls; 131 struct GNUNET_CADET_PeersLister *pl = cls;
134 132
135 GNUNET_MQ_destroy (pl->mq); 133 GNUNET_MQ_destroy(pl->mq);
136 pl->mq = NULL; 134 pl->mq = NULL;
137 pl->backoff = GNUNET_TIME_randomized_backoff (pl->backoff, 135 pl->backoff = GNUNET_TIME_randomized_backoff(pl->backoff,
138 GNUNET_TIME_UNIT_MINUTES); 136 GNUNET_TIME_UNIT_MINUTES);
139 pl->reconnect_task = GNUNET_SCHEDULER_add_delayed (pl->backoff, 137 pl->reconnect_task = GNUNET_SCHEDULER_add_delayed(pl->backoff,
140 &reconnect, 138 &reconnect,
141 pl); 139 pl);
142} 140}
143 141
144 142
@@ -148,35 +146,35 @@ error_handler (void *cls,
148 * @param cls a `struct GNUNET_CADET_PeersLister` operation 146 * @param cls a `struct GNUNET_CADET_PeersLister` operation
149 */ 147 */
150static void 148static void
151reconnect (void *cls) 149reconnect(void *cls)
152{ 150{
153 struct GNUNET_CADET_PeersLister *pl = cls; 151 struct GNUNET_CADET_PeersLister *pl = cls;
154 struct GNUNET_MQ_MessageHandler handlers[] = { 152 struct GNUNET_MQ_MessageHandler handlers[] = {
155 GNUNET_MQ_hd_fixed_size (get_peers, 153 GNUNET_MQ_hd_fixed_size(get_peers,
156 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS, 154 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
157 struct GNUNET_CADET_LocalInfoPeers, 155 struct GNUNET_CADET_LocalInfoPeers,
158 pl), 156 pl),
159 GNUNET_MQ_hd_fixed_size (get_peers_end, 157 GNUNET_MQ_hd_fixed_size(get_peers_end,
160 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS_END, 158 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS_END,
161 struct GNUNET_MessageHeader, 159 struct GNUNET_MessageHeader,
162 pl), 160 pl),
163 GNUNET_MQ_handler_end () 161 GNUNET_MQ_handler_end()
164 }; 162 };
165 struct GNUNET_MessageHeader *msg; 163 struct GNUNET_MessageHeader *msg;
166 struct GNUNET_MQ_Envelope *env; 164 struct GNUNET_MQ_Envelope *env;
167 165
168 pl->reconnect_task = NULL; 166 pl->reconnect_task = NULL;
169 pl->mq = GNUNET_CLIENT_connect (pl->cfg, 167 pl->mq = GNUNET_CLIENT_connect(pl->cfg,
170 "cadet", 168 "cadet",
171 handlers, 169 handlers,
172 &error_handler, 170 &error_handler,
173 pl); 171 pl);
174 if (NULL == pl->mq) 172 if (NULL == pl->mq)
175 return; 173 return;
176 env = GNUNET_MQ_msg (msg, 174 env = GNUNET_MQ_msg(msg,
177 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PEERS); 175 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PEERS);
178 GNUNET_MQ_send (pl->mq, 176 GNUNET_MQ_send(pl->mq,
179 env); 177 env);
180} 178}
181 179
182 180
@@ -191,27 +189,27 @@ reconnect (void *cls)
191 * @return NULL on error 189 * @return NULL on error
192 */ 190 */
193struct GNUNET_CADET_PeersLister * 191struct GNUNET_CADET_PeersLister *
194GNUNET_CADET_list_peers (const struct GNUNET_CONFIGURATION_Handle *cfg, 192GNUNET_CADET_list_peers(const struct GNUNET_CONFIGURATION_Handle *cfg,
195 GNUNET_CADET_PeersCB callback, 193 GNUNET_CADET_PeersCB callback,
196 void *callback_cls) 194 void *callback_cls)
197{ 195{
198 struct GNUNET_CADET_PeersLister *pl; 196 struct GNUNET_CADET_PeersLister *pl;
199 197
200 if (NULL == callback) 198 if (NULL == callback)
201 { 199 {
202 GNUNET_break (0); 200 GNUNET_break(0);
203 return NULL; 201 return NULL;
204 } 202 }
205 pl = GNUNET_new (struct GNUNET_CADET_PeersLister); 203 pl = GNUNET_new(struct GNUNET_CADET_PeersLister);
206 pl->peers_cb = callback; 204 pl->peers_cb = callback;
207 pl->peers_cb_cls = callback_cls; 205 pl->peers_cb_cls = callback_cls;
208 pl->cfg = cfg; 206 pl->cfg = cfg;
209 reconnect (pl); 207 reconnect(pl);
210 if (NULL == pl->mq) 208 if (NULL == pl->mq)
211 { 209 {
212 GNUNET_free (pl); 210 GNUNET_free(pl);
213 return NULL; 211 return NULL;
214 } 212 }
215 return pl; 213 return pl;
216} 214}
217 215
@@ -223,15 +221,15 @@ GNUNET_CADET_list_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
223 * @return Closure given to GNUNET_CADET_get_peers(). 221 * @return Closure given to GNUNET_CADET_get_peers().
224 */ 222 */
225void * 223void *
226GNUNET_CADET_list_peers_cancel (struct GNUNET_CADET_PeersLister *pl) 224GNUNET_CADET_list_peers_cancel(struct GNUNET_CADET_PeersLister *pl)
227{ 225{
228 void *ret = pl->peers_cb_cls; 226 void *ret = pl->peers_cb_cls;
229 227
230 if (NULL != pl->mq) 228 if (NULL != pl->mq)
231 GNUNET_MQ_destroy (pl->mq); 229 GNUNET_MQ_destroy(pl->mq);
232 if (NULL != pl->reconnect_task) 230 if (NULL != pl->reconnect_task)
233 GNUNET_SCHEDULER_cancel (pl->reconnect_task); 231 GNUNET_SCHEDULER_cancel(pl->reconnect_task);
234 GNUNET_free (pl); 232 GNUNET_free(pl);
235 return ret; 233 return ret;
236} 234}
237 235
diff --git a/src/cadet/cadet_api_list_tunnels.c b/src/cadet/cadet_api_list_tunnels.c
index a2ba65993..2ea8cf26c 100644
--- a/src/cadet/cadet_api_list_tunnels.c
+++ b/src/cadet/cadet_api_list_tunnels.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file cadet/cadet_api_list_tunnels.c 21 * @file cadet/cadet_api_list_tunnels.c
22 * @brief cadet api: client implementation of cadet service 22 * @brief cadet api: client implementation of cadet service
@@ -34,9 +34,7 @@
34/** 34/**
35 * Operation handle. 35 * Operation handle.
36 */ 36 */
37struct GNUNET_CADET_ListTunnels 37struct GNUNET_CADET_ListTunnels {
38{
39
40 /** 38 /**
41 * Monitor callback 39 * Monitor callback
42 */ 40 */
@@ -46,7 +44,7 @@ struct GNUNET_CADET_ListTunnels
46 * Info callback closure for @c tunnels_cb. 44 * Info callback closure for @c tunnels_cb.
47 */ 45 */
48 void *tunnels_cb_cls; 46 void *tunnels_cb_cls;
49 47
50 /** 48 /**
51 * Message queue to talk to CADET service. 49 * Message queue to talk to CADET service.
52 */ 50 */
@@ -66,7 +64,6 @@ struct GNUNET_CADET_ListTunnels
66 * Backoff for reconnect attempts. 64 * Backoff for reconnect attempts.
67 */ 65 */
68 struct GNUNET_TIME_Relative backoff; 66 struct GNUNET_TIME_Relative backoff;
69
70}; 67};
71 68
72 69
@@ -77,19 +74,19 @@ struct GNUNET_CADET_ListTunnels
77 * @param info Message itself. 74 * @param info Message itself.
78 */ 75 */
79static void 76static void
80handle_get_tunnels (void *cls, 77handle_get_tunnels(void *cls,
81 const struct GNUNET_CADET_LocalInfoTunnel *info) 78 const struct GNUNET_CADET_LocalInfoTunnel *info)
82{ 79{
83 struct GNUNET_CADET_ListTunnels *lt = cls; 80 struct GNUNET_CADET_ListTunnels *lt = cls;
84 struct GNUNET_CADET_TunnelDetails td; 81 struct GNUNET_CADET_TunnelDetails td;
85 82
86 td.peer = info->destination; 83 td.peer = info->destination;
87 td.channels = ntohl (info->channels); 84 td.channels = ntohl(info->channels);
88 td.connections = ntohl (info->connections); 85 td.connections = ntohl(info->connections);
89 td.estate = ntohs (info->estate); 86 td.estate = ntohs(info->estate);
90 td.cstate = ntohs (info->cstate); 87 td.cstate = ntohs(info->cstate);
91 lt->tunnels_cb (lt->tunnels_cb_cls, 88 lt->tunnels_cb(lt->tunnels_cb_cls,
92 &td); 89 &td);
93} 90}
94 91
95 92
@@ -100,15 +97,16 @@ handle_get_tunnels (void *cls,
100 * @param message Message itself. 97 * @param message Message itself.
101 */ 98 */
102static void 99static void
103handle_get_tunnels_end (void *cls, 100handle_get_tunnels_end(void *cls,
104 const struct GNUNET_MessageHeader *msg) 101 const struct GNUNET_MessageHeader *msg)
105{ 102{
106 struct GNUNET_CADET_ListTunnels *lt = cls; 103 struct GNUNET_CADET_ListTunnels *lt = cls;
107 (void) msg; 104
108 105 (void)msg;
109 lt->tunnels_cb (lt->tunnels_cb_cls, 106
110 NULL); 107 lt->tunnels_cb(lt->tunnels_cb_cls,
111 GNUNET_CADET_list_tunnels_cancel (lt); 108 NULL);
109 GNUNET_CADET_list_tunnels_cancel(lt);
112} 110}
113 111
114 112
@@ -118,7 +116,7 @@ handle_get_tunnels_end (void *cls,
118 * @param cls a `struct GNUNET_CADET_ListTunnels` operation 116 * @param cls a `struct GNUNET_CADET_ListTunnels` operation
119 */ 117 */
120static void 118static void
121reconnect (void *cls); 119reconnect(void *cls);
122 120
123 121
124/** 122/**
@@ -128,18 +126,18 @@ reconnect (void *cls);
128 * @param error error code from MQ 126 * @param error error code from MQ
129 */ 127 */
130static void 128static void
131error_handler (void *cls, 129error_handler(void *cls,
132 enum GNUNET_MQ_Error error) 130 enum GNUNET_MQ_Error error)
133{ 131{
134 struct GNUNET_CADET_ListTunnels *lt = cls; 132 struct GNUNET_CADET_ListTunnels *lt = cls;
135 133
136 GNUNET_MQ_destroy (lt->mq); 134 GNUNET_MQ_destroy(lt->mq);
137 lt->mq = NULL; 135 lt->mq = NULL;
138 lt->backoff = GNUNET_TIME_randomized_backoff (lt->backoff, 136 lt->backoff = GNUNET_TIME_randomized_backoff(lt->backoff,
139 GNUNET_TIME_UNIT_MINUTES); 137 GNUNET_TIME_UNIT_MINUTES);
140 lt->reconnect_task = GNUNET_SCHEDULER_add_delayed (lt->backoff, 138 lt->reconnect_task = GNUNET_SCHEDULER_add_delayed(lt->backoff,
141 &reconnect, 139 &reconnect,
142 lt); 140 lt);
143} 141}
144 142
145 143
@@ -149,35 +147,35 @@ error_handler (void *cls,
149 * @param cls a `struct GNUNET_CADET_ListTunnels` operation 147 * @param cls a `struct GNUNET_CADET_ListTunnels` operation
150 */ 148 */
151static void 149static void
152reconnect (void *cls) 150reconnect(void *cls)
153{ 151{
154 struct GNUNET_CADET_ListTunnels *lt = cls; 152 struct GNUNET_CADET_ListTunnels *lt = cls;
155 struct GNUNET_MQ_MessageHandler handlers[] = { 153 struct GNUNET_MQ_MessageHandler handlers[] = {
156 GNUNET_MQ_hd_fixed_size (get_tunnels, 154 GNUNET_MQ_hd_fixed_size(get_tunnels,
157 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS, 155 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
158 struct GNUNET_CADET_LocalInfoTunnel, 156 struct GNUNET_CADET_LocalInfoTunnel,
159 lt), 157 lt),
160 GNUNET_MQ_hd_fixed_size (get_tunnels_end, 158 GNUNET_MQ_hd_fixed_size(get_tunnels_end,
161 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS_END, 159 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS_END,
162 struct GNUNET_MessageHeader, 160 struct GNUNET_MessageHeader,
163 lt), 161 lt),
164 GNUNET_MQ_handler_end () 162 GNUNET_MQ_handler_end()
165 }; 163 };
166 struct GNUNET_MessageHeader *msg; 164 struct GNUNET_MessageHeader *msg;
167 struct GNUNET_MQ_Envelope *env; 165 struct GNUNET_MQ_Envelope *env;
168 166
169 lt->reconnect_task = NULL; 167 lt->reconnect_task = NULL;
170 lt->mq = GNUNET_CLIENT_connect (lt->cfg, 168 lt->mq = GNUNET_CLIENT_connect(lt->cfg,
171 "cadet", 169 "cadet",
172 handlers, 170 handlers,
173 &error_handler, 171 &error_handler,
174 lt); 172 lt);
175 if (NULL == lt->mq) 173 if (NULL == lt->mq)
176 return; 174 return;
177 env = GNUNET_MQ_msg (msg, 175 env = GNUNET_MQ_msg(msg,
178 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_TUNNELS); 176 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_TUNNELS);
179 GNUNET_MQ_send (lt->mq, 177 GNUNET_MQ_send(lt->mq,
180 env); 178 env);
181} 179}
182 180
183 181
@@ -192,27 +190,27 @@ reconnect (void *cls)
192 * @return NULL on error 190 * @return NULL on error
193 */ 191 */
194struct GNUNET_CADET_ListTunnels * 192struct GNUNET_CADET_ListTunnels *
195GNUNET_CADET_list_tunnels (const struct GNUNET_CONFIGURATION_Handle *cfg, 193GNUNET_CADET_list_tunnels(const struct GNUNET_CONFIGURATION_Handle *cfg,
196 GNUNET_CADET_TunnelsCB callback, 194 GNUNET_CADET_TunnelsCB callback,
197 void *callback_cls) 195 void *callback_cls)
198{ 196{
199 struct GNUNET_CADET_ListTunnels *lt; 197 struct GNUNET_CADET_ListTunnels *lt;
200 198
201 if (NULL == callback) 199 if (NULL == callback)
202 { 200 {
203 GNUNET_break (0); 201 GNUNET_break(0);
204 return NULL; 202 return NULL;
205 } 203 }
206 lt = GNUNET_new (struct GNUNET_CADET_ListTunnels); 204 lt = GNUNET_new(struct GNUNET_CADET_ListTunnels);
207 lt->tunnels_cb = callback; 205 lt->tunnels_cb = callback;
208 lt->tunnels_cb_cls = callback_cls; 206 lt->tunnels_cb_cls = callback_cls;
209 lt->cfg = cfg; 207 lt->cfg = cfg;
210 reconnect (lt); 208 reconnect(lt);
211 if (NULL == lt->mq) 209 if (NULL == lt->mq)
212 { 210 {
213 GNUNET_free (lt); 211 GNUNET_free(lt);
214 return NULL; 212 return NULL;
215 } 213 }
216 return lt; 214 return lt;
217} 215}
218 216
@@ -224,15 +222,15 @@ GNUNET_CADET_list_tunnels (const struct GNUNET_CONFIGURATION_Handle *cfg,
224 * @return Closure given to GNUNET_CADET_list_tunnels(). 222 * @return Closure given to GNUNET_CADET_list_tunnels().
225 */ 223 */
226void * 224void *
227GNUNET_CADET_list_tunnels_cancel (struct GNUNET_CADET_ListTunnels *lt) 225GNUNET_CADET_list_tunnels_cancel(struct GNUNET_CADET_ListTunnels *lt)
228{ 226{
229 void *ret = lt->tunnels_cb_cls; 227 void *ret = lt->tunnels_cb_cls;
230 228
231 if (NULL != lt->mq) 229 if (NULL != lt->mq)
232 GNUNET_MQ_destroy (lt->mq); 230 GNUNET_MQ_destroy(lt->mq);
233 if (NULL != lt->reconnect_task) 231 if (NULL != lt->reconnect_task)
234 GNUNET_SCHEDULER_cancel (lt->reconnect_task); 232 GNUNET_SCHEDULER_cancel(lt->reconnect_task);
235 GNUNET_free (lt); 233 GNUNET_free(lt);
236 return ret; 234 return ret;
237} 235}
238 236
diff --git a/src/cadet/cadet_protocol.h b/src/cadet/cadet_protocol.h
index 762babb8a..6252a1da5 100644
--- a/src/cadet/cadet_protocol.h
+++ b/src/cadet/cadet_protocol.h
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file cadet/cadet_protocol.h 22 * @file cadet/cadet_protocol.h
@@ -46,7 +46,7 @@ struct GNUNET_CADET_TunnelMessage;
46extern "C" 46extern "C"
47{ 47{
48#if 0 48#if 0
49 /* keep Emacsens' auto-indent happy */ 49/* keep Emacsens' auto-indent happy */
50} 50}
51#endif 51#endif
52#endif 52#endif
@@ -66,8 +66,7 @@ GNUNET_NETWORK_STRUCT_BEGIN
66/** 66/**
67 * Message for cadet connection creation. 67 * Message for cadet connection creation.
68 */ 68 */
69struct GNUNET_CADET_ConnectionCreateMessage 69struct GNUNET_CADET_ConnectionCreateMessage {
70{
71 /** 70 /**
72 * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE 71 * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE
73 * 72 *
@@ -100,8 +99,7 @@ struct GNUNET_CADET_ConnectionCreateMessage
100/** 99/**
101 * Message for ack'ing a connection 100 * Message for ack'ing a connection
102 */ 101 */
103struct GNUNET_CADET_ConnectionCreateAckMessage 102struct GNUNET_CADET_ConnectionCreateAckMessage {
104{
105 /** 103 /**
106 * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK 104 * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK
107 */ 105 */
@@ -116,15 +114,13 @@ struct GNUNET_CADET_ConnectionCreateAckMessage
116 * ID of the connection. 114 * ID of the connection.
117 */ 115 */
118 struct GNUNET_CADET_ConnectionTunnelIdentifier cid; 116 struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
119
120}; 117};
121 118
122 119
123/** 120/**
124 * Message for notifying a disconnection in a path 121 * Message for notifying a disconnection in a path
125 */ 122 */
126struct GNUNET_CADET_ConnectionBrokenMessage 123struct GNUNET_CADET_ConnectionBrokenMessage {
127{
128 /** 124 /**
129 * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN. 125 * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN.
130 */ 126 */
@@ -155,8 +151,7 @@ struct GNUNET_CADET_ConnectionBrokenMessage
155/** 151/**
156 * Message to destroy a connection. 152 * Message to destroy a connection.
157 */ 153 */
158struct GNUNET_CADET_ConnectionDestroyMessage 154struct GNUNET_CADET_ConnectionDestroyMessage {
159{
160 /** 155 /**
161 * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY 156 * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY
162 */ 157 */
@@ -185,8 +180,7 @@ struct GNUNET_CADET_ConnectionDestroyMessage
185 * against the respective #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED 180 * against the respective #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED
186 * messages. 181 * messages.
187 */ 182 */
188struct CadetEncryptedMessageIdentifier 183struct CadetEncryptedMessageIdentifier {
189{
190 /** 184 /**
191 * This number is incremented by one per message. It may wrap around. 185 * This number is incremented by one per message. It may wrap around.
192 * In network byte order. 186 * In network byte order.
@@ -199,7 +193,6 @@ struct CadetEncryptedMessageIdentifier
199 * Flags to be used in GNUNET_CADET_KX. 193 * Flags to be used in GNUNET_CADET_KX.
200 */ 194 */
201enum GNUNET_CADET_KX_Flags { 195enum GNUNET_CADET_KX_Flags {
202
203 /** 196 /**
204 * Should the peer reply with its KX details? 197 * Should the peer reply with its KX details?
205 */ 198 */
@@ -215,8 +208,7 @@ enum GNUNET_CADET_KX_Flags {
215/** 208/**
216 * Message for a Key eXchange for a tunnel. 209 * Message for a Key eXchange for a tunnel.
217 */ 210 */
218struct GNUNET_CADET_TunnelKeyExchangeMessage 211struct GNUNET_CADET_TunnelKeyExchangeMessage {
219{
220 /** 212 /**
221 * Type: #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX or 213 * Type: #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX or
222 * #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX_AUTH as part 214 * #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX_AUTH as part
@@ -264,7 +256,6 @@ struct GNUNET_CADET_TunnelKeyExchangeMessage
264 * using 'gcry_sexp_sprint'. 256 * using 'gcry_sexp_sprint'.
265 */ 257 */
266 struct GNUNET_CRYPTO_EcdhePublicKey ratchet_key; 258 struct GNUNET_CRYPTO_EcdhePublicKey ratchet_key;
267
268}; 259};
269 260
270 261
@@ -272,9 +263,7 @@ struct GNUNET_CADET_TunnelKeyExchangeMessage
272 * Message for a Key eXchange for a tunnel, with authentication. 263 * Message for a Key eXchange for a tunnel, with authentication.
273 * Used as a response to the initial KX as well as for rekeying. 264 * Used as a response to the initial KX as well as for rekeying.
274 */ 265 */
275struct GNUNET_CADET_TunnelKeyExchangeAuthMessage 266struct GNUNET_CADET_TunnelKeyExchangeAuthMessage {
276{
277
278 /** 267 /**
279 * Message header with key material. 268 * Message header with key material.
280 */ 269 */
@@ -294,7 +283,6 @@ struct GNUNET_CADET_TunnelKeyExchangeAuthMessage
294 * signature or payload data. 283 * signature or payload data.
295 */ 284 */
296 struct GNUNET_HashCode auth; 285 struct GNUNET_HashCode auth;
297
298}; 286};
299 287
300 288
@@ -302,9 +290,7 @@ struct GNUNET_CADET_TunnelKeyExchangeAuthMessage
302 * Encrypted axolotl header with numbers that identify which 290 * Encrypted axolotl header with numbers that identify which
303 * keys in which ratchet are to be used to decrypt the body. 291 * keys in which ratchet are to be used to decrypt the body.
304 */ 292 */
305struct GNUNET_CADET_AxHeader 293struct GNUNET_CADET_AxHeader {
306{
307
308 /** 294 /**
309 * Number of messages sent with the current ratchet key. 295 * Number of messages sent with the current ratchet key.
310 */ 296 */
@@ -319,15 +305,13 @@ struct GNUNET_CADET_AxHeader
319 * Current ratchet key. 305 * Current ratchet key.
320 */ 306 */
321 struct GNUNET_CRYPTO_EcdhePublicKey DHRs; 307 struct GNUNET_CRYPTO_EcdhePublicKey DHRs;
322
323}; 308};
324 309
325 310
326/** 311/**
327 * Axolotl-encrypted tunnel message with application payload. 312 * Axolotl-encrypted tunnel message with application payload.
328 */ 313 */
329struct GNUNET_CADET_TunnelEncryptedMessage 314struct GNUNET_CADET_TunnelEncryptedMessage {
330{
331 /** 315 /**
332 * Type: #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED 316 * Type: #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED
333 */ 317 */
@@ -370,8 +354,7 @@ struct GNUNET_CADET_TunnelEncryptedMessage
370/** 354/**
371 * Message to create a Channel. 355 * Message to create a Channel.
372 */ 356 */
373struct GNUNET_CADET_ChannelOpenMessage 357struct GNUNET_CADET_ChannelOpenMessage {
374{
375 /** 358 /**
376 * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN 359 * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN
377 */ 360 */
@@ -398,8 +381,7 @@ struct GNUNET_CADET_ChannelOpenMessage
398 * Message to acknowledge opening a channel of type 381 * Message to acknowledge opening a channel of type
399 * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK. 382 * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK.
400 */ 383 */
401struct GNUNET_CADET_ChannelOpenAckMessage 384struct GNUNET_CADET_ChannelOpenAckMessage {
402{
403 /** 385 /**
404 * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK 386 * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK
405 */ 387 */
@@ -427,8 +409,7 @@ struct GNUNET_CADET_ChannelOpenAckMessage
427 * Message to destroy a channel of type 409 * Message to destroy a channel of type
428 * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY. 410 * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY.
429 */ 411 */
430struct GNUNET_CADET_ChannelDestroyMessage 412struct GNUNET_CADET_ChannelDestroyMessage {
431{
432 /** 413 /**
433 * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY 414 * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY
434 */ 415 */
@@ -449,8 +430,7 @@ struct GNUNET_CADET_ChannelDestroyMessage
449/** 430/**
450 * Number used to uniquely identify messages in a CADET Channel. 431 * Number used to uniquely identify messages in a CADET Channel.
451 */ 432 */
452struct ChannelMessageIdentifier 433struct ChannelMessageIdentifier {
453{
454 /** 434 /**
455 * Unique ID of the message, cycles around, in NBO. 435 * Unique ID of the message, cycles around, in NBO.
456 */ 436 */
@@ -461,8 +441,7 @@ struct ChannelMessageIdentifier
461/** 441/**
462 * Message for cadet data traffic. 442 * Message for cadet data traffic.
463 */ 443 */
464struct GNUNET_CADET_ChannelAppDataMessage 444struct GNUNET_CADET_ChannelAppDataMessage {
465{
466 /** 445 /**
467 * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA. 446 * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA.
468 */ 447 */
@@ -487,8 +466,7 @@ struct GNUNET_CADET_ChannelAppDataMessage
487/** 466/**
488 * Message to acknowledge end-to-end data. 467 * Message to acknowledge end-to-end data.
489 */ 468 */
490struct GNUNET_CADET_ChannelDataAckMessage 469struct GNUNET_CADET_ChannelDataAckMessage {
491{
492 /** 470 /**
493 * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK 471 * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK
494 */ 472 */
diff --git a/src/cadet/cadet_test_lib.c b/src/cadet/cadet_test_lib.c
index db16e4015..ae1eb6f00 100644
--- a/src/cadet/cadet_test_lib.c
+++ b/src/cadet/cadet_test_lib.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file cadet/cadet_test_lib.c 21 * @file cadet/cadet_test_lib.c
22 * @author Bartlomiej Polot 22 * @author Bartlomiej Polot
@@ -31,8 +31,7 @@
31/** 31/**
32 * Test context for a CADET Test. 32 * Test context for a CADET Test.
33 */ 33 */
34struct GNUNET_CADET_TEST_Context 34struct GNUNET_CADET_TEST_Context {
35{
36 /** 35 /**
37 * Array of running peers. 36 * Array of running peers.
38 */ 37 */
@@ -92,15 +91,13 @@ struct GNUNET_CADET_TEST_Context
92 * Number of ports in #ports. 91 * Number of ports in #ports.
93 */ 92 */
94 unsigned int port_count; 93 unsigned int port_count;
95
96}; 94};
97 95
98 96
99/** 97/**
100 * Context for a cadet adapter callback. 98 * Context for a cadet adapter callback.
101 */ 99 */
102struct GNUNET_CADET_TEST_AdapterContext 100struct GNUNET_CADET_TEST_AdapterContext {
103{
104 /** 101 /**
105 * Peer number for the particular peer. 102 * Peer number for the particular peer.
106 */ 103 */
@@ -129,33 +126,33 @@ struct GNUNET_CADET_TEST_AdapterContext
129 * @return service handle to return in 'op_result', NULL on error 126 * @return service handle to return in 'op_result', NULL on error
130 */ 127 */
131static void * 128static void *
132cadet_connect_adapter (void *cls, 129cadet_connect_adapter(void *cls,
133 const struct GNUNET_CONFIGURATION_Handle *cfg) 130 const struct GNUNET_CONFIGURATION_Handle *cfg)
134{ 131{
135 struct GNUNET_CADET_TEST_AdapterContext *actx = cls; 132 struct GNUNET_CADET_TEST_AdapterContext *actx = cls;
136 struct GNUNET_CADET_TEST_Context *ctx = actx->ctx; 133 struct GNUNET_CADET_TEST_Context *ctx = actx->ctx;
137 struct GNUNET_CADET_Handle *h; 134 struct GNUNET_CADET_Handle *h;
138 135
139 h = GNUNET_CADET_connect (cfg); 136 h = GNUNET_CADET_connect(cfg);
140 if (NULL == h) 137 if (NULL == h)
141 { 138 {
142 GNUNET_break(0); 139 GNUNET_break(0);
143 return NULL; 140 return NULL;
144 } 141 }
145 if (NULL == ctx->ports) 142 if (NULL == ctx->ports)
146 return h; 143 return h;
147 actx->ports = GNUNET_new_array (ctx->port_count, 144 actx->ports = GNUNET_new_array(ctx->port_count,
148 struct GNUNET_CADET_Port *); 145 struct GNUNET_CADET_Port *);
149 for (unsigned int i = 0; i < ctx->port_count; i++) 146 for (unsigned int i = 0; i < ctx->port_count; i++)
150 { 147 {
151 actx->ports[i] = GNUNET_CADET_open_port (h, 148 actx->ports[i] = GNUNET_CADET_open_port(h,
152 ctx->ports[i], 149 ctx->ports[i],
153 ctx->connects, 150 ctx->connects,
154 (void *) (long) actx->peer, 151 (void *)(long)actx->peer,
155 ctx->window_changes, 152 ctx->window_changes,
156 ctx->disconnects, 153 ctx->disconnects,
157 ctx->handlers); 154 ctx->handlers);
158 } 155 }
159 return h; 156 return h;
160} 157}
161 158
@@ -168,23 +165,23 @@ cadet_connect_adapter (void *cls,
168 * @param op_result service handle returned from the connect adapter 165 * @param op_result service handle returned from the connect adapter
169 */ 166 */
170static void 167static void
171cadet_disconnect_adapter (void *cls, 168cadet_disconnect_adapter(void *cls,
172 void *op_result) 169 void *op_result)
173{ 170{
174 struct GNUNET_CADET_Handle *cadet = op_result; 171 struct GNUNET_CADET_Handle *cadet = op_result;
175 struct GNUNET_CADET_TEST_AdapterContext *actx = cls; 172 struct GNUNET_CADET_TEST_AdapterContext *actx = cls;
176 173
177 if (NULL != actx->ports) 174 if (NULL != actx->ports)
178 {
179 for (unsigned int i = 0; i < actx->ctx->port_count; i++)
180 { 175 {
181 GNUNET_CADET_close_port (actx->ports[i]); 176 for (unsigned int i = 0; i < actx->ctx->port_count; i++)
182 actx->ports[i] = NULL; 177 {
178 GNUNET_CADET_close_port(actx->ports[i]);
179 actx->ports[i] = NULL;
180 }
181 GNUNET_free(actx->ports);
183 } 182 }
184 GNUNET_free (actx->ports); 183 GNUNET_free(actx);
185 } 184 GNUNET_CADET_disconnect(cadet);
186 GNUNET_free (actx);
187 GNUNET_CADET_disconnect (cadet);
188} 185}
189 186
190 187
@@ -199,7 +196,7 @@ cadet_disconnect_adapter (void *cls,
199 * NULL if operation has executed successfully. 196 * NULL if operation has executed successfully.
200 */ 197 */
201static void 198static void
202cadet_connect_cb (void *cls, 199cadet_connect_cb(void *cls,
203 struct GNUNET_TESTBED_Operation *op, 200 struct GNUNET_TESTBED_Operation *op,
204 void *ca_result, 201 void *ca_result,
205 const char *emsg) 202 const char *emsg)
@@ -207,47 +204,48 @@ cadet_connect_cb (void *cls,
207 struct GNUNET_CADET_TEST_Context *ctx = cls; 204 struct GNUNET_CADET_TEST_Context *ctx = cls;
208 205
209 if (NULL != emsg) 206 if (NULL != emsg)
210 {
211 fprintf (stderr,
212 "Failed to connect to CADET service: %s\n",
213 emsg);
214 GNUNET_SCHEDULER_shutdown ();
215 return;
216 }
217 for (unsigned int i = 0; i < ctx->num_peers; i++)
218 if (op == ctx->ops[i])
219 { 207 {
220 ctx->cadets[i] = ca_result; 208 fprintf(stderr,
221 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 209 "Failed to connect to CADET service: %s\n",
222 "...cadet %u connected\n", 210 emsg);
223 i); 211 GNUNET_SCHEDULER_shutdown();
212 return;
224 } 213 }
225 for (unsigned int i = 0; i < ctx->num_peers; i++) 214 for (unsigned int i = 0; i < ctx->num_peers; i++)
215 if (op == ctx->ops[i])
216 {
217 ctx->cadets[i] = ca_result;
218 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
219 "...cadet %u connected\n",
220 i);
221 }
222 for (unsigned int i = 0; i < ctx->num_peers; i++)
226 if (NULL == ctx->cadets[i]) 223 if (NULL == ctx->cadets[i])
227 return; /* still some CADET connections missing */ 224 return;
225 /* still some CADET connections missing */
228 /* all CADET connections ready! */ 226 /* all CADET connections ready! */
229 ctx->app_main (ctx->app_main_cls, 227 ctx->app_main(ctx->app_main_cls,
230 ctx, 228 ctx,
231 ctx->num_peers, 229 ctx->num_peers,
232 ctx->peers, 230 ctx->peers,
233 ctx->cadets); 231 ctx->cadets);
234} 232}
235 233
236 234
237void 235void
238GNUNET_CADET_TEST_cleanup (struct GNUNET_CADET_TEST_Context *ctx) 236GNUNET_CADET_TEST_cleanup(struct GNUNET_CADET_TEST_Context *ctx)
239{ 237{
240 for (unsigned int i = 0; i < ctx->num_peers; i++) 238 for (unsigned int i = 0; i < ctx->num_peers; i++)
241 { 239 {
242 GNUNET_assert (NULL != ctx->ops[i]); 240 GNUNET_assert(NULL != ctx->ops[i]);
243 GNUNET_TESTBED_operation_done (ctx->ops[i]); 241 GNUNET_TESTBED_operation_done(ctx->ops[i]);
244 ctx->ops[i] = NULL; 242 ctx->ops[i] = NULL;
245 } 243 }
246 GNUNET_free (ctx->ops); 244 GNUNET_free(ctx->ops);
247 GNUNET_free (ctx->cadets); 245 GNUNET_free(ctx->cadets);
248 GNUNET_free (ctx->handlers); 246 GNUNET_free(ctx->handlers);
249 GNUNET_free (ctx); 247 GNUNET_free(ctx);
250 GNUNET_SCHEDULER_shutdown (); 248 GNUNET_SCHEDULER_shutdown();
251} 249}
252 250
253 251
@@ -265,7 +263,7 @@ GNUNET_CADET_TEST_cleanup (struct GNUNET_CADET_TEST_Context *ctx)
265 * failed 263 * failed
266 */ 264 */
267static void 265static void
268cadet_test_run (void *cls, 266cadet_test_run(void *cls,
269 struct GNUNET_TESTBED_RunHandle *h, 267 struct GNUNET_TESTBED_RunHandle *h,
270 unsigned int num_peers, 268 unsigned int num_peers,
271 struct GNUNET_TESTBED_Peer **peers, 269 struct GNUNET_TESTBED_Peer **peers,
@@ -275,47 +273,47 @@ cadet_test_run (void *cls,
275 struct GNUNET_CADET_TEST_Context *ctx = cls; 273 struct GNUNET_CADET_TEST_Context *ctx = cls;
276 274
277 if (0 != links_failed) 275 if (0 != links_failed)
278 { 276 {
279 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 277 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
280 "Some links failed (%u), ending\n", 278 "Some links failed (%u), ending\n",
281 links_failed); 279 links_failed);
282 exit (77); 280 exit(77);
283 } 281 }
284 if (num_peers != ctx->num_peers) 282 if (num_peers != ctx->num_peers)
285 { 283 {
286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 284 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
287 "Peers started %u/%u, ending\n", 285 "Peers started %u/%u, ending\n",
288 num_peers, 286 num_peers,
289 ctx->num_peers); 287 ctx->num_peers);
290 exit (1); 288 exit(1);
291 } 289 }
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 290 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
293 "Testbed up, %u peers and %u links\n", 291 "Testbed up, %u peers and %u links\n",
294 num_peers, 292 num_peers,
295 links_succeeded); 293 links_succeeded);
296 ctx->peers = peers; 294 ctx->peers = peers;
297 for (unsigned int i = 0; i < num_peers; i++) 295 for (unsigned int i = 0; i < num_peers; i++)
298 { 296 {
299 struct GNUNET_CADET_TEST_AdapterContext *newctx; 297 struct GNUNET_CADET_TEST_AdapterContext *newctx;
300 298
301 newctx = GNUNET_new (struct GNUNET_CADET_TEST_AdapterContext); 299 newctx = GNUNET_new(struct GNUNET_CADET_TEST_AdapterContext);
302 newctx->peer = i; 300 newctx->peer = i;
303 newctx->ctx = ctx; 301 newctx->ctx = ctx;
304 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 302 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
305 "Connecting to cadet %u\n", 303 "Connecting to cadet %u\n",
306 i); 304 i);
307 ctx->ops[i] = GNUNET_TESTBED_service_connect (ctx, 305 ctx->ops[i] = GNUNET_TESTBED_service_connect(ctx,
308 peers[i], 306 peers[i],
309 "cadet", 307 "cadet",
310 &cadet_connect_cb, 308 &cadet_connect_cb,
311 ctx, 309 ctx,
312 &cadet_connect_adapter, 310 &cadet_connect_adapter,
313 &cadet_disconnect_adapter, 311 &cadet_disconnect_adapter,
314 newctx); 312 newctx);
315 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 313 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
316 "op handle %p\n", 314 "op handle %p\n",
317 ctx->ops[i]); 315 ctx->ops[i]);
318 } 316 }
319} 317}
320 318
321 319
@@ -335,41 +333,41 @@ cadet_test_run (void *cls,
335 * @param ports Ports the peers offer, NULL-terminated. 333 * @param ports Ports the peers offer, NULL-terminated.
336 */ 334 */
337void 335void
338GNUNET_CADET_TEST_ruN (const char *testname, 336GNUNET_CADET_TEST_ruN(const char *testname,
339 const char *cfgfile, 337 const char *cfgfile,
340 unsigned int num_peers, 338 unsigned int num_peers,
341 GNUNET_CADET_TEST_AppMain tmain, 339 GNUNET_CADET_TEST_AppMain tmain,
342 void *tmain_cls, 340 void *tmain_cls,
343 GNUNET_CADET_ConnectEventHandler connects, 341 GNUNET_CADET_ConnectEventHandler connects,
344 GNUNET_CADET_WindowSizeEventHandler window_changes, 342 GNUNET_CADET_WindowSizeEventHandler window_changes,
345 GNUNET_CADET_DisconnectEventHandler disconnects, 343 GNUNET_CADET_DisconnectEventHandler disconnects,
346 struct GNUNET_MQ_MessageHandler *handlers, 344 struct GNUNET_MQ_MessageHandler *handlers,
347 const struct GNUNET_HashCode **ports) 345 const struct GNUNET_HashCode **ports)
348{ 346{
349 struct GNUNET_CADET_TEST_Context *ctx; 347 struct GNUNET_CADET_TEST_Context *ctx;
350 348
351 ctx = GNUNET_new (struct GNUNET_CADET_TEST_Context); 349 ctx = GNUNET_new(struct GNUNET_CADET_TEST_Context);
352 ctx->num_peers = num_peers; 350 ctx->num_peers = num_peers;
353 ctx->ops = GNUNET_new_array (num_peers, 351 ctx->ops = GNUNET_new_array(num_peers,
354 struct GNUNET_TESTBED_Operation *); 352 struct GNUNET_TESTBED_Operation *);
355 ctx->cadets = GNUNET_new_array (num_peers, 353 ctx->cadets = GNUNET_new_array(num_peers,
356 struct GNUNET_CADET_Handle *); 354 struct GNUNET_CADET_Handle *);
357 ctx->app_main = tmain; 355 ctx->app_main = tmain;
358 ctx->app_main_cls = tmain_cls; 356 ctx->app_main_cls = tmain_cls;
359 ctx->connects = connects; 357 ctx->connects = connects;
360 ctx->window_changes = window_changes; 358 ctx->window_changes = window_changes;
361 ctx->disconnects = disconnects; 359 ctx->disconnects = disconnects;
362 ctx->handlers = GNUNET_MQ_copy_handlers (handlers); 360 ctx->handlers = GNUNET_MQ_copy_handlers(handlers);
363 ctx->ports = ports; 361 ctx->ports = ports;
364 ctx->port_count = 0; 362 ctx->port_count = 0;
365 while (NULL != ctx->ports[ctx->port_count]) 363 while (NULL != ctx->ports[ctx->port_count])
366 ctx->port_count++; 364 ctx->port_count++;
367 GNUNET_TESTBED_test_run (testname, 365 GNUNET_TESTBED_test_run(testname,
368 cfgfile, 366 cfgfile,
369 num_peers, 367 num_peers,
370 0LL, NULL, NULL, 368 0LL, NULL, NULL,
371 &cadet_test_run, 369 &cadet_test_run,
372 ctx); 370 ctx);
373} 371}
374 372
375/* end of cadet_test_lib.c */ 373/* end of cadet_test_lib.c */
diff --git a/src/cadet/cadet_test_lib.h b/src/cadet/cadet_test_lib.h
index 69c519d07..f526831a6 100644
--- a/src/cadet/cadet_test_lib.h
+++ b/src/cadet/cadet_test_lib.h
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file cadet/cadet_test_lib.h 21 * @file cadet/cadet_test_lib.h
22 * @author Bartlomiej Polot 22 * @author Bartlomiej Polot
@@ -52,10 +52,10 @@ struct GNUNET_CADET_TEST_Context;
52 * @param cadets Handle to each of the CADETs of the peers. 52 * @param cadets Handle to each of the CADETs of the peers.
53 */ 53 */
54typedef void (*GNUNET_CADET_TEST_AppMain) (void *cls, 54typedef void (*GNUNET_CADET_TEST_AppMain) (void *cls,
55 struct GNUNET_CADET_TEST_Context *ctx, 55 struct GNUNET_CADET_TEST_Context *ctx,
56 unsigned int num_peers, 56 unsigned int num_peers,
57 struct GNUNET_TESTBED_Peer **peers, 57 struct GNUNET_TESTBED_Peer **peers,
58 struct GNUNET_CADET_Handle **cadets); 58 struct GNUNET_CADET_Handle **cadets);
59 59
60 60
61/** 61/**
@@ -74,16 +74,16 @@ typedef void (*GNUNET_CADET_TEST_AppMain) (void *cls,
74 * @param ports Ports the peers offer, NULL-terminated. 74 * @param ports Ports the peers offer, NULL-terminated.
75 */ 75 */
76void 76void
77GNUNET_CADET_TEST_ruN (const char *testname, 77GNUNET_CADET_TEST_ruN(const char *testname,
78 const char *cfgfile, 78 const char *cfgfile,
79 unsigned int num_peers, 79 unsigned int num_peers,
80 GNUNET_CADET_TEST_AppMain tmain, 80 GNUNET_CADET_TEST_AppMain tmain,
81 void *tmain_cls, 81 void *tmain_cls,
82 GNUNET_CADET_ConnectEventHandler connects, 82 GNUNET_CADET_ConnectEventHandler connects,
83 GNUNET_CADET_WindowSizeEventHandler window_changes, 83 GNUNET_CADET_WindowSizeEventHandler window_changes,
84 GNUNET_CADET_DisconnectEventHandler disconnects, 84 GNUNET_CADET_DisconnectEventHandler disconnects,
85 struct GNUNET_MQ_MessageHandler *handlers, 85 struct GNUNET_MQ_MessageHandler *handlers,
86 const struct GNUNET_HashCode **ports); 86 const struct GNUNET_HashCode **ports);
87 87
88/** 88/**
89 * Clean up the testbed. 89 * Clean up the testbed.
@@ -91,7 +91,7 @@ GNUNET_CADET_TEST_ruN (const char *testname,
91 * @param ctx handle for the testbed 91 * @param ctx handle for the testbed
92 */ 92 */
93void 93void
94GNUNET_CADET_TEST_cleanup (struct GNUNET_CADET_TEST_Context *ctx); 94GNUNET_CADET_TEST_cleanup(struct GNUNET_CADET_TEST_Context *ctx);
95 95
96 96
97#if 0 /* keep Emacsens' auto-indent happy */ 97#if 0 /* keep Emacsens' auto-indent happy */
diff --git a/src/cadet/desirability_table.c b/src/cadet/desirability_table.c
index 21ec3e388..5c23de76b 100644
--- a/src/cadet/desirability_table.c
+++ b/src/cadet/desirability_table.c
@@ -8,28 +8,26 @@
8#include <stdio.h> 8#include <stdio.h>
9 9
10int 10int
11main () 11main()
12{ 12{
13 for (unsigned int num_alts=1; num_alts<10; num_alts++) 13 for (unsigned int num_alts = 1; num_alts < 10; num_alts++)
14 for (unsigned int off=0; off<10; off++) 14 for (unsigned int off = 0; off < 10; off++)
15 for (double delta=-(int) off;delta<=5;delta += 0.25) 15 for (double delta = -(int)off; delta <= 5; delta += 0.25)
16 { 16 {
17 double weight_alts; 17 double weight_alts;
18
19 if (delta <= - 1.0)
20 weight_alts = - 1.0 * num_alts / delta; /* discount alternative paths */
21 else if (delta >= 1.0)
22 weight_alts = 1.0 * num_alts * delta; /* overcount alternative paths */
23 else
24 weight_alts = 1.0 * num_alts; /* count alternative paths normally */
25
26 fprintf (stderr,
27 "Paths: %u Offset: %u Delta: %5.2f SCORE: %f\n",
28 num_alts,
29 off,
30 delta,
31 ((off + 1.0) / (weight_alts * weight_alts)));
32 }
33 18
19 if (delta <= -1.0)
20 weight_alts = -1.0 * num_alts / delta; /* discount alternative paths */
21 else if (delta >= 1.0)
22 weight_alts = 1.0 * num_alts * delta; /* overcount alternative paths */
23 else
24 weight_alts = 1.0 * num_alts; /* count alternative paths normally */
34 25
26 fprintf(stderr,
27 "Paths: %u Offset: %u Delta: %5.2f SCORE: %f\n",
28 num_alts,
29 off,
30 delta,
31 ((off + 1.0) / (weight_alts * weight_alts)));
32 }
35} 33}
diff --git a/src/cadet/gnunet-cadet-profiler.c b/src/cadet/gnunet-cadet-profiler.c
index 34ea53a57..b3e7fa30d 100644
--- a/src/cadet/gnunet-cadet-profiler.c
+++ b/src/cadet/gnunet-cadet-profiler.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file cadet/gnunet-cadet-profiler.c 21 * @file cadet/gnunet-cadet-profiler.c
22 * 22 *
@@ -41,28 +41,27 @@
41/** 41/**
42 * How long until we give up on connecting the peers? 42 * How long until we give up on connecting the peers?
43 */ 43 */
44#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120) 44#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120)
45 45
46/** 46/**
47 * Time to wait for stuff that should be rather fast 47 * Time to wait for stuff that should be rather fast
48 */ 48 */
49#define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300) 49#define SHORT_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 300)
50 50
51/** 51/**
52 * Total number of rounds. 52 * Total number of rounds.
53 */ 53 */
54#define number_rounds sizeof(rounds)/sizeof(rounds[0]) 54#define number_rounds sizeof(rounds) / sizeof(rounds[0])
55 55
56/** 56/**
57 * Ratio of peers active. First round always is 1.0. 57 * Ratio of peers active. First round always is 1.0.
58 */ 58 */
59static float rounds[] = {0.8, 0.6, 0.8, 0.5, 0.3, 0.8, 0.0}; 59static float rounds[] = { 0.8, 0.6, 0.8, 0.5, 0.3, 0.8, 0.0 };
60 60
61/** 61/**
62 * Message type for pings. 62 * Message type for pings.
63 */ 63 */
64struct CadetPingMessage 64struct CadetPingMessage {
65{
66 /** 65 /**
67 * Header. Type PING/PONG. 66 * Header. Type PING/PONG.
68 */ 67 */
@@ -87,8 +86,7 @@ struct CadetPingMessage
87/** 86/**
88 * Peer description. 87 * Peer description.
89 */ 88 */
90struct CadetPeer 89struct CadetPeer {
91{
92 /** 90 /**
93 * Testbed Operation (to get peer id, etc). 91 * Testbed Operation (to get peer id, etc).
94 */ 92 */
@@ -158,7 +156,6 @@ struct CadetPeer
158 float var[number_rounds]; 156 float var[number_rounds];
159 unsigned int pongs[number_rounds]; 157 unsigned int pongs[number_rounds];
160 unsigned int pings[number_rounds]; 158 unsigned int pings[number_rounds];
161
162}; 159};
163 160
164/** 161/**
@@ -256,7 +253,7 @@ static struct GNUNET_SCHEDULER_Task *round_task;
256 * @param cls Closure (unsued). 253 * @param cls Closure (unsued).
257 */ 254 */
258static void 255static void
259start_test (void *cls); 256start_test(void *cls);
260 257
261 258
262/** 259/**
@@ -267,12 +264,12 @@ start_test (void *cls);
267 * @return A time between 0 a max-1 ms. 264 * @return A time between 0 a max-1 ms.
268 */ 265 */
269static struct GNUNET_TIME_Relative 266static struct GNUNET_TIME_Relative
270delay_ms_rnd (unsigned int max) 267delay_ms_rnd(unsigned int max)
271{ 268{
272 unsigned int rnd; 269 unsigned int rnd;
273 270
274 rnd = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, max); 271 rnd = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, max);
275 return GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, rnd); 272 return GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, rnd);
276} 273}
277 274
278 275
@@ -284,7 +281,7 @@ delay_ms_rnd (unsigned int max)
284 * @return Index of peer in peers. 281 * @return Index of peer in peers.
285 */ 282 */
286static unsigned int 283static unsigned int
287get_index (struct CadetPeer *peer) 284get_index(struct CadetPeer *peer)
288{ 285{
289 return peer - peers; 286 return peer - peers;
290} 287}
@@ -294,23 +291,23 @@ get_index (struct CadetPeer *peer)
294 * Show the results of the test (banwidth acheived) and log them to GAUGER 291 * Show the results of the test (banwidth acheived) and log them to GAUGER
295 */ 292 */
296static void 293static void
297show_end_data (void) 294show_end_data(void)
298{ 295{
299 struct CadetPeer *peer; 296 struct CadetPeer *peer;
300 unsigned int i; 297 unsigned int i;
301 unsigned int j; 298 unsigned int j;
302 299
303 for (i = 0; i < number_rounds; i++) 300 for (i = 0; i < number_rounds; i++)
304 {
305 for (j = 0; j < peers_pinging; j++)
306 { 301 {
307 peer = &peers[j]; 302 for (j = 0; j < peers_pinging; j++)
308 fprintf (stdout, 303 {
309 "ROUND %3u PEER %3u: %10.2f / %10.2f, PINGS: %3u, PONGS: %3u\n", 304 peer = &peers[j];
310 i, j, peer->mean[i], sqrt (peer->var[i] / (peer->pongs[i] - 1)), 305 fprintf(stdout,
311 peer->pings[i], peer->pongs[i]); 306 "ROUND %3u PEER %3u: %10.2f / %10.2f, PINGS: %3u, PONGS: %3u\n",
307 i, j, peer->mean[i], sqrt(peer->var[i] / (peer->pongs[i] - 1)),
308 peer->pings[i], peer->pongs[i]);
309 }
312 } 310 }
313 }
314} 311}
315 312
316 313
@@ -320,46 +317,46 @@ show_end_data (void)
320 * @param cls Closure (unused). 317 * @param cls Closure (unused).
321 */ 318 */
322static void 319static void
323disconnect_cadet_peers (void *cls) 320disconnect_cadet_peers(void *cls)
324{ 321{
325 long line = (long) cls; 322 long line = (long)cls;
326 unsigned int i; 323 unsigned int i;
327 324
328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 325 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
329 "disconnecting cadet service, called from line %ld\n", 326 "disconnecting cadet service, called from line %ld\n",
330 line); 327 line);
331 disconnect_task = NULL; 328 disconnect_task = NULL;
332 for (i = 0; i < peers_total; i++) 329 for (i = 0; i < peers_total; i++)
333 {
334 if (NULL != peers[i].op)
335 GNUNET_TESTBED_operation_done (peers[i].op);
336
337 if (peers[i].up != GNUNET_YES)
338 continue;
339
340 if (NULL != peers[i].ch)
341 { 330 {
342 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 331 if (NULL != peers[i].op)
343 "%u: channel %p\n", i, peers[i].ch); 332 GNUNET_TESTBED_operation_done(peers[i].op);
344 GNUNET_CADET_channel_destroy (peers[i].ch); 333
334 if (peers[i].up != GNUNET_YES)
335 continue;
336
337 if (NULL != peers[i].ch)
338 {
339 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
340 "%u: channel %p\n", i, peers[i].ch);
341 GNUNET_CADET_channel_destroy(peers[i].ch);
342 }
343 if (NULL != peers[i].warmup_ch)
344 {
345 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
346 "%u: warmup channel %p\n",
347 i, peers[i].warmup_ch);
348 GNUNET_CADET_channel_destroy(peers[i].warmup_ch);
349 }
350 if (NULL != peers[i].incoming_ch)
351 {
352 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
353 "%u: incoming channel %p\n",
354 i, peers[i].incoming_ch);
355 GNUNET_CADET_channel_destroy(peers[i].incoming_ch);
356 }
345 } 357 }
346 if (NULL != peers[i].warmup_ch) 358 GNUNET_CADET_TEST_cleanup(test_ctx);
347 { 359 GNUNET_SCHEDULER_shutdown();
348 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
349 "%u: warmup channel %p\n",
350 i, peers[i].warmup_ch);
351 GNUNET_CADET_channel_destroy (peers[i].warmup_ch);
352 }
353 if (NULL != peers[i].incoming_ch)
354 {
355 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
356 "%u: incoming channel %p\n",
357 i, peers[i].incoming_ch);
358 GNUNET_CADET_channel_destroy (peers[i].incoming_ch);
359 }
360 }
361 GNUNET_CADET_TEST_cleanup (test_ctx);
362 GNUNET_SCHEDULER_shutdown ();
363} 360}
364 361
365 362
@@ -369,26 +366,26 @@ disconnect_cadet_peers (void *cls)
369 * @param cls Closure (unused). 366 * @param cls Closure (unused).
370 */ 367 */
371static void 368static void
372shutdown_task (void *cls) 369shutdown_task(void *cls)
373{ 370{
374 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 371 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
375 "Ending test.\n"); 372 "Ending test.\n");
376 if (NULL != disconnect_task) 373 if (NULL != disconnect_task)
377 { 374 {
378 GNUNET_SCHEDULER_cancel (disconnect_task); 375 GNUNET_SCHEDULER_cancel(disconnect_task);
379 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, 376 disconnect_task = GNUNET_SCHEDULER_add_now(&disconnect_cadet_peers,
380 (void *) __LINE__); 377 (void *)__LINE__);
381 } 378 }
382 if (NULL != round_task) 379 if (NULL != round_task)
383 { 380 {
384 GNUNET_SCHEDULER_cancel (round_task); 381 GNUNET_SCHEDULER_cancel(round_task);
385 round_task = NULL; 382 round_task = NULL;
386 } 383 }
387 if (NULL != test_task) 384 if (NULL != test_task)
388 { 385 {
389 GNUNET_SCHEDULER_cancel (test_task); 386 GNUNET_SCHEDULER_cancel(test_task);
390 test_task = NULL; 387 test_task = NULL;
391 } 388 }
392} 389}
393 390
394 391
@@ -398,14 +395,14 @@ shutdown_task (void *cls)
398 * @param line Line in the code the abort is requested from (__LINE__). 395 * @param line Line in the code the abort is requested from (__LINE__).
399 */ 396 */
400static void 397static void
401abort_test (long line) 398abort_test(long line)
402{ 399{
403 if (disconnect_task != NULL) 400 if (disconnect_task != NULL)
404 { 401 {
405 GNUNET_SCHEDULER_cancel (disconnect_task); 402 GNUNET_SCHEDULER_cancel(disconnect_task);
406 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, 403 disconnect_task = GNUNET_SCHEDULER_add_now(&disconnect_cadet_peers,
407 (void *) line); 404 (void *)line);
408 } 405 }
409} 406}
410 407
411/** 408/**
@@ -418,16 +415,15 @@ abort_test (long line)
418 * operation has executed successfully. 415 * operation has executed successfully.
419 */ 416 */
420static void 417static void
421stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg) 418stats_cont(void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
422{ 419{
423 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n"); 420 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "... collecting statistics done.\n");
424 GNUNET_TESTBED_operation_done (stats_op); 421 GNUNET_TESTBED_operation_done(stats_op);
425 422
426 if (NULL != disconnect_task) 423 if (NULL != disconnect_task)
427 GNUNET_SCHEDULER_cancel (disconnect_task); 424 GNUNET_SCHEDULER_cancel(disconnect_task);
428 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, 425 disconnect_task = GNUNET_SCHEDULER_add_now(&disconnect_cadet_peers,
429 (void *) __LINE__); 426 (void *)__LINE__);
430
431} 427}
432 428
433 429
@@ -443,20 +439,20 @@ stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
443 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration 439 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
444 */ 440 */
445static int 441static int
446stats_iterator (void *cls, 442stats_iterator(void *cls,
447 const struct GNUNET_TESTBED_Peer *peer, 443 const struct GNUNET_TESTBED_Peer *peer,
448 const char *subsystem, 444 const char *subsystem,
449 const char *name, 445 const char *name,
450 uint64_t value, 446 uint64_t value,
451 int is_persistent) 447 int is_persistent)
452{ 448{
453 uint32_t i; 449 uint32_t i;
454 450
455 i = GNUNET_TESTBED_get_index (peer); 451 i = GNUNET_TESTBED_get_index(peer);
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 452 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
457 " STATS %u - %s [%s]: %llu\n", 453 " STATS %u - %s [%s]: %llu\n",
458 i, subsystem, name, 454 i, subsystem, name,
459 (unsigned long long) value); 455 (unsigned long long)value);
460 456
461 return GNUNET_OK; 457 return GNUNET_OK;
462} 458}
@@ -468,15 +464,15 @@ stats_iterator (void *cls,
468 * @param cls Closure (NULL). 464 * @param cls Closure (NULL).
469 */ 465 */
470static void 466static void
471collect_stats (void *cls) 467collect_stats(void *cls)
472{ 468{
473 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 469 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
474 "Start collecting statistics...\n"); 470 "Start collecting statistics...\n");
475 stats_op = GNUNET_TESTBED_get_statistics (peers_total, 471 stats_op = GNUNET_TESTBED_get_statistics(peers_total,
476 testbed_handles, 472 testbed_handles,
477 NULL, NULL, 473 NULL, NULL,
478 &stats_iterator, 474 &stats_iterator,
479 &stats_cont, NULL); 475 &stats_cont, NULL);
480} 476}
481 477
482 478
@@ -486,11 +482,11 @@ collect_stats (void *cls)
486 * @param cls Closure (unused). 482 * @param cls Closure (unused).
487 */ 483 */
488static void 484static void
489finish_profiler (void *cls) 485finish_profiler(void *cls)
490{ 486{
491 test_finished = GNUNET_YES; 487 test_finished = GNUNET_YES;
492 show_end_data (); 488 show_end_data();
493 GNUNET_SCHEDULER_add_now (&collect_stats, NULL); 489 GNUNET_SCHEDULER_add_now(&collect_stats, NULL);
494} 490}
495 491
496 492
@@ -500,7 +496,7 @@ finish_profiler (void *cls)
500 * @param target Desired number of running peers. 496 * @param target Desired number of running peers.
501 */ 497 */
502static void 498static void
503adjust_running_peers (unsigned int target) 499adjust_running_peers(unsigned int target)
504{ 500{
505 struct GNUNET_TESTBED_Operation *op; 501 struct GNUNET_TESTBED_Operation *op;
506 unsigned int delta; 502 unsigned int delta;
@@ -508,58 +504,60 @@ adjust_running_peers (unsigned int target)
508 unsigned int i; 504 unsigned int i;
509 unsigned int r; 505 unsigned int r;
510 506
511 GNUNET_assert (target <= peers_total); 507 GNUNET_assert(target <= peers_total);
512 508
513 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "adjust peers to %u\n", target); 509 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "adjust peers to %u\n", target);
514 if (target > peers_running) 510 if (target > peers_running)
515 {
516 delta = target - peers_running;
517 run = GNUNET_YES;
518 }
519 else
520 {
521 delta = peers_running - target;
522 run = GNUNET_NO;
523 }
524
525 for (i = 0; i < delta; i++)
526 {
527 do {
528 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
529 peers_total - peers_pinging);
530 r += peers_pinging;
531 } while (peers[r].up == run || NULL != peers[r].incoming);
532 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "St%s peer %u: %s\n",
533 run ? "arting" : "opping", r, GNUNET_i2s (&peers[r].id));
534
535 if (NULL != peers[r].ping_task)
536 { 511 {
537 GNUNET_SCHEDULER_cancel (peers[r].ping_task); 512 delta = target - peers_running;
538 peers[r].ping_task = NULL; 513 run = GNUNET_YES;
539 } 514 }
540 if (NULL != peers[r].ping_ntr) 515 else
541 { 516 {
542 GNUNET_CADET_notify_transmit_ready_cancel (peers[r].ping_ntr); 517 delta = peers_running - target;
543 peers[r].ping_ntr = NULL; 518 run = GNUNET_NO;
544 } 519 }
545 peers[r].up = run;
546 520
547 if (NULL != peers[r].ch) 521 for (i = 0; i < delta; i++)
548 GNUNET_CADET_channel_destroy (peers[r].ch);
549 peers[r].ch = NULL;
550 if (NULL != peers[r].dest)
551 { 522 {
552 if (NULL != peers[r].dest->incoming_ch) 523 do
553 GNUNET_CADET_channel_destroy (peers[r].dest->incoming_ch); 524 {
554 peers[r].dest->incoming_ch = NULL; 525 r = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
526 peers_total - peers_pinging);
527 r += peers_pinging;
528 }
529 while (peers[r].up == run || NULL != peers[r].incoming);
530 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "St%s peer %u: %s\n",
531 run ? "arting" : "opping", r, GNUNET_i2s(&peers[r].id));
532
533 if (NULL != peers[r].ping_task)
534 {
535 GNUNET_SCHEDULER_cancel(peers[r].ping_task);
536 peers[r].ping_task = NULL;
537 }
538 if (NULL != peers[r].ping_ntr)
539 {
540 GNUNET_CADET_notify_transmit_ready_cancel(peers[r].ping_ntr);
541 peers[r].ping_ntr = NULL;
542 }
543 peers[r].up = run;
544
545 if (NULL != peers[r].ch)
546 GNUNET_CADET_channel_destroy(peers[r].ch);
547 peers[r].ch = NULL;
548 if (NULL != peers[r].dest)
549 {
550 if (NULL != peers[r].dest->incoming_ch)
551 GNUNET_CADET_channel_destroy(peers[r].dest->incoming_ch);
552 peers[r].dest->incoming_ch = NULL;
553 }
554
555 op = GNUNET_TESTBED_peer_manage_service(&peers[r], testbed_handles[r],
556 "cadet", NULL, NULL, run);
557 GNUNET_break(NULL != op);
558 peers_running += run ? 1 : -1;
559 GNUNET_assert(peers_running > 0);
555 } 560 }
556
557 op = GNUNET_TESTBED_peer_manage_service (&peers[r], testbed_handles[r],
558 "cadet", NULL, NULL, run);
559 GNUNET_break (NULL != op);
560 peers_running += run ? 1 : -1;
561 GNUNET_assert (peers_running > 0);
562 }
563} 561}
564 562
565 563
@@ -569,23 +567,23 @@ adjust_running_peers (unsigned int target)
569 * @param cls Closure (round #). 567 * @param cls Closure (round #).
570 */ 568 */
571static void 569static void
572next_rnd (void *cls) 570next_rnd(void *cls)
573{ 571{
574 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 572 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
575 "ROUND %u\n", 573 "ROUND %u\n",
576 current_round); 574 current_round);
577 if (0.0 == rounds[current_round]) 575 if (0.0 == rounds[current_round])
578 { 576 {
579 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Finishing\n"); 577 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Finishing\n");
580 GNUNET_SCHEDULER_add_now (&finish_profiler, NULL); 578 GNUNET_SCHEDULER_add_now(&finish_profiler, NULL);
581 return; 579 return;
582 } 580 }
583 adjust_running_peers (rounds[current_round] * peers_total); 581 adjust_running_peers(rounds[current_round] * peers_total);
584 current_round++; 582 current_round++;
585 583
586 round_task = GNUNET_SCHEDULER_add_delayed (round_time, 584 round_task = GNUNET_SCHEDULER_add_delayed(round_time,
587 &next_rnd, 585 &next_rnd,
588 NULL); 586 NULL);
589} 587}
590 588
591 589
@@ -599,7 +597,7 @@ next_rnd (void *cls)
599 * @return Number of bytes written to buf. 597 * @return Number of bytes written to buf.
600 */ 598 */
601static size_t 599static size_t
602tmt_rdy_ping (void *cls, size_t size, void *buf); 600tmt_rdy_ping(void *cls, size_t size, void *buf);
603 601
604 602
605/** 603/**
@@ -610,22 +608,22 @@ tmt_rdy_ping (void *cls, size_t size, void *buf);
610 * @param buf Buffer to copy data to. 608 * @param buf Buffer to copy data to.
611 */ 609 */
612static size_t 610static size_t
613tmt_rdy_pong (void *cls, size_t size, void *buf) 611tmt_rdy_pong(void *cls, size_t size, void *buf)
614{ 612{
615 struct CadetPingMessage *ping = cls; 613 struct CadetPingMessage *ping = cls;
616 struct CadetPingMessage *pong; 614 struct CadetPingMessage *pong;
617 615
618 if (0 == size || NULL == buf) 616 if (0 == size || NULL == buf)
619 { 617 {
620 GNUNET_free (ping); 618 GNUNET_free(ping);
621 return 0; 619 return 0;
622 } 620 }
623 pong = (struct CadetPingMessage *) buf; 621 pong = (struct CadetPingMessage *)buf;
624 GNUNET_memcpy (pong, ping, sizeof (*ping)); 622 GNUNET_memcpy(pong, ping, sizeof(*ping));
625 pong->header.type = htons (PONG); 623 pong->header.type = htons(PONG);
626 624
627 GNUNET_free (ping); 625 GNUNET_free(ping);
628 return sizeof (*ping); 626 return sizeof(*ping);
629} 627}
630 628
631 629
@@ -635,22 +633,22 @@ tmt_rdy_pong (void *cls, size_t size, void *buf)
635 * @param cls Closure (peer). 633 * @param cls Closure (peer).
636 */ 634 */
637static void 635static void
638ping (void *cls) 636ping(void *cls)
639{ 637{
640 struct CadetPeer *peer = cls; 638 struct CadetPeer *peer = cls;
641 639
642 peer->ping_task = NULL; 640 peer->ping_task = NULL;
643 if (GNUNET_YES == test_finished) 641 if (GNUNET_YES == test_finished)
644 return; 642 return;
645 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 643 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
646 "%u -> %u (%u)\n", 644 "%u -> %u (%u)\n",
647 get_index (peer), 645 get_index(peer),
648 get_index (peer->dest), 646 get_index(peer->dest),
649 peer->data_sent); 647 peer->data_sent);
650 peer->ping_ntr = GNUNET_CADET_notify_transmit_ready (peer->ch, GNUNET_NO, 648 peer->ping_ntr = GNUNET_CADET_notify_transmit_ready(peer->ch, GNUNET_NO,
651 GNUNET_TIME_UNIT_FOREVER_REL, 649 GNUNET_TIME_UNIT_FOREVER_REL,
652 sizeof (struct CadetPingMessage), 650 sizeof(struct CadetPingMessage),
653 &tmt_rdy_ping, peer); 651 &tmt_rdy_ping, peer);
654} 652}
655 653
656/** 654/**
@@ -660,16 +658,16 @@ ping (void *cls)
660 * @param tc Task context. 658 * @param tc Task context.
661 */ 659 */
662static void 660static void
663pong (struct GNUNET_CADET_Channel *channel, 661pong(struct GNUNET_CADET_Channel *channel,
664 const struct CadetPingMessage *ping) 662 const struct CadetPingMessage *ping)
665{ 663{
666 struct CadetPingMessage *copy; 664 struct CadetPingMessage *copy;
667 665
668 copy = GNUNET_new (struct CadetPingMessage); 666 copy = GNUNET_new(struct CadetPingMessage);
669 *copy = *ping; 667 *copy = *ping;
670 GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO, 668 GNUNET_CADET_notify_transmit_ready(channel, GNUNET_NO,
671 GNUNET_TIME_UNIT_FOREVER_REL, 669 GNUNET_TIME_UNIT_FOREVER_REL,
672 sizeof (struct CadetPingMessage), 670 sizeof(struct CadetPingMessage),
673 &tmt_rdy_pong, copy); 671 &tmt_rdy_pong, copy);
674} 672}
675 673
@@ -682,39 +680,39 @@ pong (struct GNUNET_CADET_Channel *channel,
682 * @param buf Buffer to copy data to. 680 * @param buf Buffer to copy data to.
683 */ 681 */
684static size_t 682static size_t
685tmt_rdy_ping (void *cls, size_t size, void *buf) 683tmt_rdy_ping(void *cls, size_t size, void *buf)
686{ 684{
687 struct CadetPeer *peer = cls; 685 struct CadetPeer *peer = cls;
688 struct CadetPingMessage *msg = buf; 686 struct CadetPingMessage *msg = buf;
689 687
690 peer->ping_ntr = NULL; 688 peer->ping_ntr = NULL;
691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 689 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
692 "tmt_rdy called, filling buffer\n"); 690 "tmt_rdy called, filling buffer\n");
693 if (size < sizeof (struct CadetPingMessage) || NULL == buf) 691 if (size < sizeof(struct CadetPingMessage) || NULL == buf)
694 { 692 {
695 GNUNET_break (GNUNET_YES == test_finished); 693 GNUNET_break(GNUNET_YES == test_finished);
696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 694 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
697 "size %u, buf %p, data_sent %u, data_received %u\n", 695 "size %u, buf %p, data_sent %u, data_received %u\n",
698 (unsigned int) size, 696 (unsigned int)size,
699 buf, 697 buf,
700 peer->data_sent, 698 peer->data_sent,
701 peer->data_received); 699 peer->data_received);
702 700
703 return 0; 701 return 0;
704 } 702 }
705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 703 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
706 "Sending: msg %d\n", 704 "Sending: msg %d\n",
707 peer->data_sent); 705 peer->data_sent);
708 msg->header.size = htons (size); 706 msg->header.size = htons(size);
709 msg->header.type = htons (PING); 707 msg->header.type = htons(PING);
710 msg->counter = htonl (peer->data_sent++); 708 msg->counter = htonl(peer->data_sent++);
711 msg->round_number = htonl (current_round); 709 msg->round_number = htonl(current_round);
712 msg->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ()); 710 msg->timestamp = GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get());
713 peer->pings[current_round]++; 711 peer->pings[current_round]++;
714 peer->ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (PING_PERIOD), 712 peer->ping_task = GNUNET_SCHEDULER_add_delayed(delay_ms_rnd(PING_PERIOD),
715 &ping, peer); 713 &ping, peer);
716 714
717 return sizeof (struct CadetPingMessage); 715 return sizeof(struct CadetPingMessage);
718} 716}
719 717
720 718
@@ -729,18 +727,18 @@ tmt_rdy_ping (void *cls, size_t size, void *buf)
729 * GNUNET_SYSERR to close it (signal serious error) 727 * GNUNET_SYSERR to close it (signal serious error)
730 */ 728 */
731int 729int
732ping_handler (void *cls, struct GNUNET_CADET_Channel *channel, 730ping_handler(void *cls, struct GNUNET_CADET_Channel *channel,
733 void **channel_ctx, 731 void **channel_ctx,
734 const struct GNUNET_MessageHeader *message) 732 const struct GNUNET_MessageHeader *message)
735{ 733{
736 long n = (long) cls; 734 long n = (long)cls;
737 735
738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 736 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
739 "%u got PING\n", 737 "%u got PING\n",
740 (unsigned int) n); 738 (unsigned int)n);
741 GNUNET_CADET_receive_done (channel); 739 GNUNET_CADET_receive_done(channel);
742 if (GNUNET_NO == test_finished) 740 if (GNUNET_NO == test_finished)
743 pong (channel, (struct CadetPingMessage *) message); 741 pong(channel, (struct CadetPingMessage *)message);
744 742
745 return GNUNET_OK; 743 return GNUNET_OK;
746} 744}
@@ -757,11 +755,11 @@ ping_handler (void *cls, struct GNUNET_CADET_Channel *channel,
757 * GNUNET_SYSERR to close it (signal serious error) 755 * GNUNET_SYSERR to close it (signal serious error)
758 */ 756 */
759int 757int
760pong_handler (void *cls, struct GNUNET_CADET_Channel *channel, 758pong_handler(void *cls, struct GNUNET_CADET_Channel *channel,
761 void **channel_ctx, 759 void **channel_ctx,
762 const struct GNUNET_MessageHeader *message) 760 const struct GNUNET_MessageHeader *message)
763{ 761{
764 long n = (long) cls; 762 long n = (long)cls;
765 struct CadetPeer *peer; 763 struct CadetPeer *peer;
766 struct CadetPingMessage *msg; 764 struct CadetPingMessage *msg;
767 struct GNUNET_TIME_Absolute send_time; 765 struct GNUNET_TIME_Absolute send_time;
@@ -769,24 +767,24 @@ pong_handler (void *cls, struct GNUNET_CADET_Channel *channel,
769 unsigned int r /* Ping round */; 767 unsigned int r /* Ping round */;
770 float delta; 768 float delta;
771 769
772 GNUNET_CADET_receive_done (channel); 770 GNUNET_CADET_receive_done(channel);
773 peer = &peers[n]; 771 peer = &peers[n];
774 772
775 msg = (struct CadetPingMessage *) message; 773 msg = (struct CadetPingMessage *)message;
776 774
777 send_time = GNUNET_TIME_absolute_ntoh (msg->timestamp); 775 send_time = GNUNET_TIME_absolute_ntoh(msg->timestamp);
778 latency = GNUNET_TIME_absolute_get_duration (send_time); 776 latency = GNUNET_TIME_absolute_get_duration(send_time);
779 r = ntohl (msg->round_number); 777 r = ntohl(msg->round_number);
780 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u <- %u (%u) latency: %s\n", 778 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%u <- %u (%u) latency: %s\n",
781 get_index (peer), 779 get_index(peer),
782 get_index (peer->dest), 780 get_index(peer->dest),
783 (uint32_t) ntohl (msg->counter), 781 (uint32_t)ntohl(msg->counter),
784 GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO)); 782 GNUNET_STRINGS_relative_time_to_string(latency, GNUNET_NO));
785 783
786 /* Online variance calculation */ 784 /* Online variance calculation */
787 peer->pongs[r]++; 785 peer->pongs[r]++;
788 delta = latency.rel_value_us - peer->mean[r]; 786 delta = latency.rel_value_us - peer->mean[r];
789 peer->mean[r] = peer->mean[r] + delta/peer->pongs[r]; 787 peer->mean[r] = peer->mean[r] + delta / peer->pongs[r];
790 peer->var[r] += delta * (latency.rel_value_us - peer->mean[r]); 788 peer->var[r] += delta * (latency.rel_value_us - peer->mean[r]);
791 789
792 return GNUNET_OK; 790 return GNUNET_OK;
@@ -797,9 +795,9 @@ pong_handler (void *cls, struct GNUNET_CADET_Channel *channel,
797 * Handlers, for diverse services 795 * Handlers, for diverse services
798 */ 796 */
799static struct GNUNET_CADET_MessageHandler handlers[] = { 797static struct GNUNET_CADET_MessageHandler handlers[] = {
800 {&ping_handler, PING, sizeof (struct CadetPingMessage)}, 798 { &ping_handler, PING, sizeof(struct CadetPingMessage) },
801 {&pong_handler, PONG, sizeof (struct CadetPingMessage)}, 799 { &pong_handler, PONG, sizeof(struct CadetPingMessage) },
802 {NULL, 0, 0} 800 { NULL, 0, 0 }
803}; 801};
804 802
805 803
@@ -816,41 +814,41 @@ static struct GNUNET_CADET_MessageHandler handlers[] = {
816 * (can be NULL -- that's not an error). 814 * (can be NULL -- that's not an error).
817 */ 815 */
818static void * 816static void *
819incoming_channel (void *cls, struct GNUNET_CADET_Channel *channel, 817incoming_channel(void *cls, struct GNUNET_CADET_Channel *channel,
820 const struct GNUNET_PeerIdentity *initiator, 818 const struct GNUNET_PeerIdentity *initiator,
821 const struct GNUNET_HashCode *port, 819 const struct GNUNET_HashCode *port,
822 enum GNUNET_CADET_ChannelOption options) 820 enum GNUNET_CADET_ChannelOption options)
823{ 821{
824 long n = (long) cls; 822 long n = (long)cls;
825 struct CadetPeer *peer; 823 struct CadetPeer *peer;
826 824
827 peer = GNUNET_CONTAINER_multipeermap_get (ids, initiator); 825 peer = GNUNET_CONTAINER_multipeermap_get(ids, initiator);
828 GNUNET_assert (NULL != peer); 826 GNUNET_assert(NULL != peer);
829 if (NULL == peers[n].incoming) 827 if (NULL == peers[n].incoming)
830 {
831 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
832 "WARMUP %3u: %u <= %u\n",
833 peers_warmup,
834 (unsigned int) n,
835 get_index (peer));
836 peers_warmup++;
837 if (peers_warmup < peers_total)
838 return NULL;
839 if (NULL != test_task)
840 { 828 {
841 GNUNET_SCHEDULER_cancel (test_task); 829 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
842 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, 830 "WARMUP %3u: %u <= %u\n",
843 &start_test, NULL); 831 peers_warmup,
832 (unsigned int)n,
833 get_index(peer));
834 peers_warmup++;
835 if (peers_warmup < peers_total)
836 return NULL;
837 if (NULL != test_task)
838 {
839 GNUNET_SCHEDULER_cancel(test_task);
840 test_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
841 &start_test, NULL);
842 }
843 return NULL;
844 } 844 }
845 return NULL; 845 GNUNET_assert(peer == peers[n].incoming);
846 } 846 GNUNET_assert(peer->dest == &peers[n]);
847 GNUNET_assert (peer == peers[n].incoming); 847 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
848 GNUNET_assert (peer->dest == &peers[n]); 848 "%u <= %u %p\n",
849 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 849 (unsigned int)n,
850 "%u <= %u %p\n", 850 get_index(peer),
851 (unsigned int) n, 851 channel);
852 get_index (peer),
853 channel);
854 peers[n].incoming_ch = channel; 852 peers[n].incoming_ch = channel;
855 853
856 return NULL; 854 return NULL;
@@ -866,15 +864,15 @@ incoming_channel (void *cls, struct GNUNET_CADET_Channel *channel,
866 * with the channel is stored 864 * with the channel is stored
867 */ 865 */
868static void 866static void
869channel_cleaner (void *cls, 867channel_cleaner(void *cls,
870 const struct GNUNET_CADET_Channel *channel, 868 const struct GNUNET_CADET_Channel *channel,
871 void *channel_ctx) 869 void *channel_ctx)
872{ 870{
873 long n = (long) cls; 871 long n = (long)cls;
874 struct CadetPeer *peer = &peers[n]; 872 struct CadetPeer *peer = &peers[n];
875 873
876 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 874 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
877 "Channel %p disconnected at peer %ld\n", channel, n); 875 "Channel %p disconnected at peer %ld\n", channel, n);
878 if (peer->ch == channel) 876 if (peer->ch == channel)
879 peer->ch = NULL; 877 peer->ch = NULL;
880} 878}
@@ -888,14 +886,15 @@ channel_cleaner (void *cls,
888 * @return Random peer not yet connected to. 886 * @return Random peer not yet connected to.
889 */ 887 */
890static struct CadetPeer * 888static struct CadetPeer *
891select_random_peer (struct CadetPeer *peer) 889select_random_peer(struct CadetPeer *peer)
892{ 890{
893 unsigned int r; 891 unsigned int r;
894 892
895 do 893 do
896 { 894 {
897 r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, peers_total); 895 r = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, peers_total);
898 } while (NULL != peers[r].incoming); 896 }
897 while (NULL != peers[r].incoming);
899 peers[r].incoming = peer; 898 peers[r].incoming = peer;
900 899
901 return &peers[r]; 900 return &peers[r];
@@ -910,45 +909,45 @@ select_random_peer (struct CadetPeer *peer)
910 * @param cls Closure (unsued). 909 * @param cls Closure (unsued).
911 */ 910 */
912static void 911static void
913start_test (void *cls) 912start_test(void *cls)
914{ 913{
915 unsigned long i; 914 unsigned long i;
916 915
917 test_task = NULL; 916 test_task = NULL;
918 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start profiler\n"); 917 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Start profiler\n");
919 918
920 919
921 for (i = 0; i < peers_pinging; i++) 920 for (i = 0; i < peers_pinging; i++)
922 {
923 peers[i].dest = select_random_peer (&peers[i]);
924 peers[i].ch = GNUNET_CADET_channel_create (peers[i].cadet, NULL,
925 &peers[i].dest->id,
926 GC_u2h (1));
927 if (NULL == peers[i].ch)
928 { 921 {
929 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Channel %lu failed\n", i); 922 peers[i].dest = select_random_peer(&peers[i]);
930 GNUNET_CADET_TEST_cleanup (test_ctx); 923 peers[i].ch = GNUNET_CADET_channel_create(peers[i].cadet, NULL,
931 return; 924 &peers[i].dest->id,
925 GC_u2h(1));
926 if (NULL == peers[i].ch)
927 {
928 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Channel %lu failed\n", i);
929 GNUNET_CADET_TEST_cleanup(test_ctx);
930 return;
931 }
932 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
933 "%lu => %u %p\n",
934 i,
935 get_index(peers[i].dest),
936 peers[i].ch);
937 peers[i].ping_task = GNUNET_SCHEDULER_add_delayed(delay_ms_rnd(2000),
938 &ping, &peers[i]);
932 } 939 }
933 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
934 "%lu => %u %p\n",
935 i,
936 get_index (peers[i].dest),
937 peers[i].ch);
938 peers[i].ping_task = GNUNET_SCHEDULER_add_delayed (delay_ms_rnd (2000),
939 &ping, &peers[i]);
940 }
941 peers_running = peers_total; 940 peers_running = peers_total;
942 if (NULL != disconnect_task) 941 if (NULL != disconnect_task)
943 GNUNET_SCHEDULER_cancel (disconnect_task); 942 GNUNET_SCHEDULER_cancel(disconnect_task);
944 disconnect_task = 943 disconnect_task =
945 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(round_time, 944 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(round_time,
946 number_rounds + 1), 945 number_rounds + 1),
947 &disconnect_cadet_peers, 946 &disconnect_cadet_peers,
948 (void *) __LINE__); 947 (void *)__LINE__);
949 round_task = GNUNET_SCHEDULER_add_delayed (round_time, 948 round_task = GNUNET_SCHEDULER_add_delayed(round_time,
950 &next_rnd, 949 &next_rnd,
951 NULL); 950 NULL);
952} 951}
953 952
954 953
@@ -956,26 +955,26 @@ start_test (void *cls)
956 * Do warmup: create some channels to spread information about the topology. 955 * Do warmup: create some channels to spread information about the topology.
957 */ 956 */
958static void 957static void
959warmup (void) 958warmup(void)
960{ 959{
961 struct CadetPeer *peer; 960 struct CadetPeer *peer;
962 unsigned int i; 961 unsigned int i;
963 962
964 for (i = 0; i < peers_total; i++) 963 for (i = 0; i < peers_total; i++)
965 {
966 peer = select_random_peer (NULL);
967 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "WARMUP %u => %u\n",
968 i, get_index (peer));
969 peers[i].warmup_ch =
970 GNUNET_CADET_channel_create (peers[i].cadet, NULL, &peer->id,
971 GC_u2h (1));
972 if (NULL == peers[i].warmup_ch)
973 { 964 {
974 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Warmup %u failed\n", i); 965 peer = select_random_peer(NULL);
975 GNUNET_CADET_TEST_cleanup (test_ctx); 966 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "WARMUP %u => %u\n",
976 return; 967 i, get_index(peer));
968 peers[i].warmup_ch =
969 GNUNET_CADET_channel_create(peers[i].cadet, NULL, &peer->id,
970 GC_u2h(1));
971 if (NULL == peers[i].warmup_ch)
972 {
973 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Warmup %u failed\n", i);
974 GNUNET_CADET_TEST_cleanup(test_ctx);
975 return;
976 }
977 } 977 }
978 }
979} 978}
980 979
981 980
@@ -989,48 +988,48 @@ warmup (void)
989 * NULL if the operation is successfull 988 * NULL if the operation is successfull
990 */ 989 */
991static void 990static void
992peer_id_cb (void *cls, 991peer_id_cb(void *cls,
993 struct GNUNET_TESTBED_Operation *op, 992 struct GNUNET_TESTBED_Operation *op,
994 const struct GNUNET_TESTBED_PeerInformation *pinfo, 993 const struct GNUNET_TESTBED_PeerInformation *pinfo,
995 const char *emsg) 994 const char *emsg)
996{ 995{
997 long n = (long) cls; 996 long n = (long)cls;
998 997
999 if (NULL == pinfo || NULL != emsg) 998 if (NULL == pinfo || NULL != emsg)
1000 { 999 {
1001 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg); 1000 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
1002 abort_test (__LINE__); 1001 abort_test(__LINE__);
1003 return; 1002 return;
1004 } 1003 }
1005 peers[n].id = *(pinfo->result.id); 1004 peers[n].id = *(pinfo->result.id);
1006 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1005 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1007 "%ld id: %s\n", 1006 "%ld id: %s\n",
1008 n, 1007 n,
1009 GNUNET_i2s (&peers[n].id)); 1008 GNUNET_i2s(&peers[n].id));
1010 GNUNET_break (GNUNET_OK == 1009 GNUNET_break(GNUNET_OK ==
1011 GNUNET_CONTAINER_multipeermap_put (ids, &peers[n].id, &peers[n], 1010 GNUNET_CONTAINER_multipeermap_put(ids, &peers[n].id, &peers[n],
1012 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)); 1011 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1013 1012
1014 GNUNET_TESTBED_operation_done (peers[n].op); 1013 GNUNET_TESTBED_operation_done(peers[n].op);
1015 peers[n].op = NULL; 1014 peers[n].op = NULL;
1016 1015
1017 p_ids++; 1016 p_ids++;
1018 if (p_ids < peers_total) 1017 if (p_ids < peers_total)
1019 return; 1018 return;
1020 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n"); 1019 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Got all IDs, starting profiler\n");
1021 if (do_warmup) 1020 if (do_warmup)
1022 { 1021 {
1023 struct GNUNET_TIME_Relative delay; 1022 struct GNUNET_TIME_Relative delay;
1024 1023
1025 warmup(); 1024 warmup();
1026 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 1025 delay = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
1027 100 * peers_total); 1026 100 * peers_total);
1028 test_task = GNUNET_SCHEDULER_add_delayed (delay, &start_test, NULL); 1027 test_task = GNUNET_SCHEDULER_add_delayed(delay, &start_test, NULL);
1029 return; /* start_test from incoming_channel */ 1028 return; /* start_test from incoming_channel */
1030 } 1029 }
1031 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Starting in a second...\n"); 1030 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Starting in a second...\n");
1032 test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, 1031 test_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
1033 &start_test, NULL); 1032 &start_test, NULL);
1034} 1033}
1035 1034
1036 1035
@@ -1044,37 +1043,37 @@ peer_id_cb (void *cls,
1044 * @param cadetes Handle to each of the CADETs of the peers. 1043 * @param cadetes Handle to each of the CADETs of the peers.
1045 */ 1044 */
1046static void 1045static void
1047tmain (void *cls, 1046tmain(void *cls,
1048 struct GNUNET_CADET_TEST_Context *ctx, 1047 struct GNUNET_CADET_TEST_Context *ctx,
1049 unsigned int num_peers, 1048 unsigned int num_peers,
1050 struct GNUNET_TESTBED_Peer **testbed_peers, 1049 struct GNUNET_TESTBED_Peer **testbed_peers,
1051 struct GNUNET_CADET_Handle **cadetes) 1050 struct GNUNET_CADET_Handle **cadetes)
1052{ 1051{
1053 unsigned long i; 1052 unsigned long i;
1054 1053
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1054 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1056 "test main\n"); 1055 "test main\n");
1057 test_ctx = ctx; 1056 test_ctx = ctx;
1058 GNUNET_assert (peers_total == num_peers); 1057 GNUNET_assert(peers_total == num_peers);
1059 peers_running = num_peers; 1058 peers_running = num_peers;
1060 testbed_handles = testbed_peers; 1059 testbed_handles = testbed_peers;
1061 disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME, 1060 disconnect_task = GNUNET_SCHEDULER_add_delayed(SHORT_TIME,
1062 &disconnect_cadet_peers, 1061 &disconnect_cadet_peers,
1063 (void *) __LINE__); 1062 (void *)__LINE__);
1064 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); 1063 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, NULL);
1065 for (i = 0; i < peers_total; i++) 1064 for (i = 0; i < peers_total; i++)
1066 { 1065 {
1067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1066 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1068 "requesting id %ld\n", 1067 "requesting id %ld\n",
1069 i); 1068 i);
1070 peers[i].up = GNUNET_YES; 1069 peers[i].up = GNUNET_YES;
1071 peers[i].cadet = cadetes[i]; 1070 peers[i].cadet = cadetes[i];
1072 peers[i].op = 1071 peers[i].op =
1073 GNUNET_TESTBED_peer_get_information (testbed_handles[i], 1072 GNUNET_TESTBED_peer_get_information(testbed_handles[i],
1074 GNUNET_TESTBED_PIT_IDENTITY, 1073 GNUNET_TESTBED_PIT_IDENTITY,
1075 &peer_id_cb, (void *) i); 1074 &peer_id_cb, (void *)i);
1076 } 1075 }
1077 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "requested peer ids\n"); 1076 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "requested peer ids\n");
1078 /* Continues from pi_cb -> do_test */ 1077 /* Continues from pi_cb -> do_test */
1079} 1078}
1080 1079
@@ -1083,7 +1082,7 @@ tmain (void *cls,
1083 * Main: start profiler. 1082 * Main: start profiler.
1084 */ 1083 */
1085int 1084int
1086main (int argc, char *argv[]) 1085main(int argc, char *argv[])
1087{ 1086{
1088 static const struct GNUNET_HashCode *ports[2]; 1087 static const struct GNUNET_HashCode *ports[2];
1089 const char *config_file; 1088 const char *config_file;
@@ -1091,59 +1090,59 @@ main (int argc, char *argv[])
1091 config_file = ".profiler.conf"; 1090 config_file = ".profiler.conf";
1092 1091
1093 if (4 > argc) 1092 if (4 > argc)
1094 { 1093 {
1095 fprintf (stderr, 1094 fprintf(stderr,
1096 "usage: %s ROUND_TIME PEERS PINGS [DO_WARMUP]\n", 1095 "usage: %s ROUND_TIME PEERS PINGS [DO_WARMUP]\n",
1097 argv[0]); 1096 argv[0]);
1098 fprintf (stderr, 1097 fprintf(stderr,
1099 "example: %s 30s 16 1 Y\n", 1098 "example: %s 30s 16 1 Y\n",
1100 argv[0]); 1099 argv[0]);
1101 return 1; 1100 return 1;
1102 } 1101 }
1103 1102
1104 if (GNUNET_OK != 1103 if (GNUNET_OK !=
1105 GNUNET_STRINGS_fancy_time_to_relative (argv[1], 1104 GNUNET_STRINGS_fancy_time_to_relative(argv[1],
1106 &round_time)) 1105 &round_time))
1107 { 1106 {
1108 fprintf (stderr, 1107 fprintf(stderr,
1109 "%s is not a valid time\n", 1108 "%s is not a valid time\n",
1110 argv[1]); 1109 argv[1]);
1111 return 1; 1110 return 1;
1112 } 1111 }
1113 1112
1114 peers_total = atoll (argv[2]); 1113 peers_total = atoll(argv[2]);
1115 if (2 > peers_total) 1114 if (2 > peers_total)
1116 { 1115 {
1117 fprintf (stderr, 1116 fprintf(stderr,
1118 "%s peers is not valid (> 2)\n", 1117 "%s peers is not valid (> 2)\n",
1119 argv[1]); 1118 argv[1]);
1120 return 1; 1119 return 1;
1121 } 1120 }
1122 peers = GNUNET_new_array (peers_total, 1121 peers = GNUNET_new_array(peers_total,
1123 struct CadetPeer); 1122 struct CadetPeer);
1124 peers_pinging = atoll (argv[3]); 1123 peers_pinging = atoll(argv[3]);
1125 1124
1126 if (peers_total < 2 * peers_pinging) 1125 if (peers_total < 2 * peers_pinging)
1127 { 1126 {
1128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1127 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1129 "not enough peers, total should be > 2 * peers_pinging\n"); 1128 "not enough peers, total should be > 2 * peers_pinging\n");
1130 return 1; 1129 return 1;
1131 } 1130 }
1132 1131
1133 do_warmup = (5 > argc || argv[4][0] != 'N'); 1132 do_warmup = (5 > argc || argv[4][0] != 'N');
1134 1133
1135 ids = GNUNET_CONTAINER_multipeermap_create (2 * peers_total, 1134 ids = GNUNET_CONTAINER_multipeermap_create(2 * peers_total,
1136 GNUNET_YES); 1135 GNUNET_YES);
1137 GNUNET_assert (NULL != ids); 1136 GNUNET_assert(NULL != ids);
1138 p_ids = 0; 1137 p_ids = 0;
1139 test_finished = GNUNET_NO; 1138 test_finished = GNUNET_NO;
1140 ports[0] = GC_u2h (1); 1139 ports[0] = GC_u2h(1);
1141 ports[1] = 0; 1140 ports[1] = 0;
1142 GNUNET_CADET_TEST_run ("cadet-profiler", config_file, peers_total, 1141 GNUNET_CADET_TEST_run("cadet-profiler", config_file, peers_total,
1143 &tmain, NULL, /* tmain cls */ 1142 &tmain, NULL, /* tmain cls */
1144 &incoming_channel, &channel_cleaner, 1143 &incoming_channel, &channel_cleaner,
1145 handlers, ports); 1144 handlers, ports);
1146 GNUNET_free (peers); 1145 GNUNET_free(peers);
1147 1146
1148 return 0; 1147 return 0;
1149} 1148}
diff --git a/src/cadet/gnunet-cadet.c b/src/cadet/gnunet-cadet.c
index 4e81d52f2..f5cf86211 100644
--- a/src/cadet/gnunet-cadet.c
+++ b/src/cadet/gnunet-cadet.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file cadet/gnunet-cadet.c 22 * @file cadet/gnunet-cadet.c
@@ -143,7 +143,7 @@ static unsigned int sent_pkt;
143 * Wait for input on STDIO and send it out over the #ch. 143 * Wait for input on STDIO and send it out over the #ch.
144 */ 144 */
145static void 145static void
146listen_stdio (void); 146listen_stdio(void);
147 147
148 148
149/** 149/**
@@ -154,21 +154,25 @@ listen_stdio (void);
154 * @return Human readable string. 154 * @return Human readable string.
155 */ 155 */
156static const char * 156static const char *
157enc_2s (uint16_t status) 157enc_2s(uint16_t status)
158{ 158{
159 switch (status) 159 switch (status)
160 { 160 {
161 case 0: 161 case 0:
162 return "NULL "; 162 return "NULL ";
163 case 1: 163
164 return "KSENT"; 164 case 1:
165 case 2: 165 return "KSENT";
166 return "KRECV"; 166
167 case 3: 167 case 2:
168 return "READY"; 168 return "KRECV";
169 default: 169
170 return ""; 170 case 3:
171 } 171 return "READY";
172
173 default:
174 return "";
175 }
172} 176}
173 177
174 178
@@ -180,23 +184,28 @@ enc_2s (uint16_t status)
180 * @return Human readable string. 184 * @return Human readable string.
181 */ 185 */
182static const char * 186static const char *
183conn_2s (uint16_t status) 187conn_2s(uint16_t status)
184{ 188{
185 switch (status) 189 switch (status)
186 { 190 {
187 case 0: 191 case 0:
188 return "NEW "; 192 return "NEW ";
189 case 1: 193
190 return "SRCH "; 194 case 1:
191 case 2: 195 return "SRCH ";
192 return "WAIT "; 196
193 case 3: 197 case 2:
194 return "READY"; 198 return "WAIT ";
195 case 4: 199
196 return "SHUTD"; 200 case 3:
197 default: 201 return "READY";
198 return ""; 202
199 } 203 case 4:
204 return "SHUTD";
205
206 default:
207 return "";
208 }
200} 209}
201 210
202 211
@@ -206,60 +215,60 @@ conn_2s (uint16_t status)
206 * @param cls Closure (unused). 215 * @param cls Closure (unused).
207 */ 216 */
208static void 217static void
209shutdown_task (void *cls) 218shutdown_task(void *cls)
210{ 219{
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n"); 220 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
212 if (NULL != lp) 221 if (NULL != lp)
213 { 222 {
214 GNUNET_CADET_close_port (lp); 223 GNUNET_CADET_close_port(lp);
215 lp = NULL; 224 lp = NULL;
216 } 225 }
217 if (NULL != ch) 226 if (NULL != ch)
218 { 227 {
219 GNUNET_CADET_channel_destroy (ch); 228 GNUNET_CADET_channel_destroy(ch);
220 ch = NULL; 229 ch = NULL;
221 } 230 }
222 if (NULL != gpo) 231 if (NULL != gpo)
223 { 232 {
224 GNUNET_CADET_get_path_cancel (gpo); 233 GNUNET_CADET_get_path_cancel(gpo);
225 gpo = NULL; 234 gpo = NULL;
226 } 235 }
227 if (NULL != plo) 236 if (NULL != plo)
228 { 237 {
229 GNUNET_CADET_list_peers_cancel (plo); 238 GNUNET_CADET_list_peers_cancel(plo);
230 plo = NULL; 239 plo = NULL;
231 } 240 }
232 if (NULL != tio) 241 if (NULL != tio)
233 { 242 {
234 GNUNET_CADET_list_tunnels_cancel (tio); 243 GNUNET_CADET_list_tunnels_cancel(tio);
235 tio = NULL; 244 tio = NULL;
236 } 245 }
237 if (NULL != mh) 246 if (NULL != mh)
238 { 247 {
239 GNUNET_CADET_disconnect (mh); 248 GNUNET_CADET_disconnect(mh);
240 mh = NULL; 249 mh = NULL;
241 } 250 }
242 if (NULL != rd_task) 251 if (NULL != rd_task)
243 { 252 {
244 GNUNET_SCHEDULER_cancel (rd_task); 253 GNUNET_SCHEDULER_cancel(rd_task);
245 rd_task = NULL; 254 rd_task = NULL;
246 } 255 }
247 if (NULL != echo_task) 256 if (NULL != echo_task)
248 { 257 {
249 GNUNET_SCHEDULER_cancel (echo_task); 258 GNUNET_SCHEDULER_cancel(echo_task);
250 echo_task = NULL; 259 echo_task = NULL;
251 } 260 }
252 if (NULL != job) 261 if (NULL != job)
253 { 262 {
254 GNUNET_SCHEDULER_cancel (job); 263 GNUNET_SCHEDULER_cancel(job);
255 job = NULL; 264 job = NULL;
256 } 265 }
257} 266}
258 267
259void 268void
260mq_cb (void *cls) 269mq_cb(void *cls)
261{ 270{
262 listen_stdio (); 271 listen_stdio();
263} 272}
264 273
265 274
@@ -269,7 +278,7 @@ mq_cb (void *cls)
269 * @param cls Closure (unused). 278 * @param cls Closure (unused).
270 */ 279 */
271static void 280static void
272read_stdio (void *cls) 281read_stdio(void *cls)
273{ 282{
274 struct GNUNET_MQ_Envelope *env; 283 struct GNUNET_MQ_Envelope *env;
275 struct GNUNET_MessageHeader *msg; 284 struct GNUNET_MessageHeader *msg;
@@ -277,41 +286,41 @@ read_stdio (void *cls)
277 ssize_t data_size; 286 ssize_t data_size;
278 287
279 rd_task = NULL; 288 rd_task = NULL;
280 data_size = read (0, buf, 60000); 289 data_size = read(0, buf, 60000);
281 if (data_size < 1) 290 if (data_size < 1)
282 { 291 {
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 292 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
284 "read() returned %s\n", 293 "read() returned %s\n",
285 strerror (errno)); 294 strerror(errno));
286 GNUNET_SCHEDULER_shutdown (); 295 GNUNET_SCHEDULER_shutdown();
287 return; 296 return;
288 } 297 }
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 298 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
290 "Read %u bytes from stdio\n", 299 "Read %u bytes from stdio\n",
291 (unsigned int) data_size); 300 (unsigned int)data_size);
292 env = GNUNET_MQ_msg_extra (msg, data_size, GNUNET_MESSAGE_TYPE_CADET_CLI); 301 env = GNUNET_MQ_msg_extra(msg, data_size, GNUNET_MESSAGE_TYPE_CADET_CLI);
293 GNUNET_memcpy (&msg[1], buf, data_size); 302 GNUNET_memcpy(&msg[1], buf, data_size);
294 GNUNET_MQ_send (GNUNET_CADET_get_mq (ch), env); 303 GNUNET_MQ_send(GNUNET_CADET_get_mq(ch), env);
295 304
296 sent_pkt++; 305 sent_pkt++;
297 306
298 if (GNUNET_NO == echo) 307 if (GNUNET_NO == echo)
299 {
300 // Use MQ's notification if too much data of stdin is pooring in too fast.
301 if (STREAM_BUFFER_SIZE < sent_pkt)
302 { 308 {
303 GNUNET_MQ_notify_sent (env, mq_cb, cls); 309 // Use MQ's notification if too much data of stdin is pooring in too fast.
304 sent_pkt = 0; 310 if (STREAM_BUFFER_SIZE < sent_pkt)
311 {
312 GNUNET_MQ_notify_sent(env, mq_cb, cls);
313 sent_pkt = 0;
314 }
315 else
316 {
317 listen_stdio();
318 }
305 } 319 }
306 else 320 else
307 { 321 {
308 listen_stdio (); 322 echo_time = GNUNET_TIME_absolute_get();
309 } 323 }
310 }
311 else
312 {
313 echo_time = GNUNET_TIME_absolute_get ();
314 }
315} 324}
316 325
317 326
@@ -319,20 +328,20 @@ read_stdio (void *cls)
319 * Wait for input on STDIO and send it out over the #ch. 328 * Wait for input on STDIO and send it out over the #ch.
320 */ 329 */
321static void 330static void
322listen_stdio () 331listen_stdio()
323{ 332{
324 struct GNUNET_NETWORK_FDSet *rs; 333 struct GNUNET_NETWORK_FDSet *rs;
325 334
326 /* FIXME: why use 'rs' here, seems overly complicated... */ 335 /* FIXME: why use 'rs' here, seems overly complicated... */
327 rs = GNUNET_NETWORK_fdset_create (); 336 rs = GNUNET_NETWORK_fdset_create();
328 GNUNET_NETWORK_fdset_set_native (rs, 0); /* STDIN */ 337 GNUNET_NETWORK_fdset_set_native(rs, 0); /* STDIN */
329 rd_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, 338 rd_task = GNUNET_SCHEDULER_add_select(GNUNET_SCHEDULER_PRIORITY_DEFAULT,
330 GNUNET_TIME_UNIT_FOREVER_REL, 339 GNUNET_TIME_UNIT_FOREVER_REL,
331 rs, 340 rs,
332 NULL, 341 NULL,
333 &read_stdio, 342 &read_stdio,
334 NULL); 343 NULL);
335 GNUNET_NETWORK_fdset_destroy (rs); 344 GNUNET_NETWORK_fdset_destroy(rs);
336} 345}
337 346
338 347
@@ -346,12 +355,12 @@ listen_stdio ()
346 * @param channel connection to the other end (henceforth invalid) 355 * @param channel connection to the other end (henceforth invalid)
347 */ 356 */
348static void 357static void
349channel_ended (void *cls, const struct GNUNET_CADET_Channel *channel) 358channel_ended(void *cls, const struct GNUNET_CADET_Channel *channel)
350{ 359{
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel ended!\n"); 360 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Channel ended!\n");
352 GNUNET_assert (channel == ch); 361 GNUNET_assert(channel == ch);
353 ch = NULL; 362 ch = NULL;
354 GNUNET_SCHEDULER_shutdown (); 363 GNUNET_SCHEDULER_shutdown();
355} 364}
356 365
357 366
@@ -370,20 +379,20 @@ channel_ended (void *cls, const struct GNUNET_CADET_Channel *channel)
370 * @return initial channel context for the channel, we use @a channel 379 * @return initial channel context for the channel, we use @a channel
371 */ 380 */
372static void * 381static void *
373channel_incoming (void *cls, 382channel_incoming(void *cls,
374 struct GNUNET_CADET_Channel *channel, 383 struct GNUNET_CADET_Channel *channel,
375 const struct GNUNET_PeerIdentity *initiator) 384 const struct GNUNET_PeerIdentity *initiator)
376{ 385{
377 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 386 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
378 "Incoming connection from %s\n", 387 "Incoming connection from %s\n",
379 GNUNET_i2s_full (initiator)); 388 GNUNET_i2s_full(initiator));
380 GNUNET_assert (NULL == ch); 389 GNUNET_assert(NULL == ch);
381 GNUNET_assert (NULL != lp); 390 GNUNET_assert(NULL != lp);
382 GNUNET_CADET_close_port (lp); 391 GNUNET_CADET_close_port(lp);
383 lp = NULL; 392 lp = NULL;
384 ch = channel; 393 ch = channel;
385 if (GNUNET_NO == echo) 394 if (GNUNET_NO == echo)
386 listen_stdio (); 395 listen_stdio();
387 return channel; 396 return channel;
388} 397}
389 398
@@ -394,7 +403,7 @@ channel_incoming (void *cls,
394 * @param cls Closure (NULL). 403 * @param cls Closure (NULL).
395 */ 404 */
396static void 405static void
397send_echo (void *cls) 406send_echo(void *cls)
398{ 407{
399 struct GNUNET_MQ_Envelope *env; 408 struct GNUNET_MQ_Envelope *env;
400 struct GNUNET_MessageHeader *msg; 409 struct GNUNET_MessageHeader *msg;
@@ -402,8 +411,8 @@ send_echo (void *cls)
402 echo_task = NULL; 411 echo_task = NULL;
403 if (NULL == ch) 412 if (NULL == ch)
404 return; 413 return;
405 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_CLI); 414 env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_CADET_CLI);
406 GNUNET_MQ_send (GNUNET_CADET_get_mq (ch), env); 415 GNUNET_MQ_send(GNUNET_CADET_get_mq(ch), env);
407} 416}
408 417
409 418
@@ -416,7 +425,7 @@ send_echo (void *cls)
416 * #GNUNET_SYSERR to close it (signal serious error). 425 * #GNUNET_SYSERR to close it (signal serious error).
417 */ 426 */
418static int 427static int
419check_data (void *cls, const struct GNUNET_MessageHeader *message) 428check_data(void *cls, const struct GNUNET_MessageHeader *message)
420{ 429{
421 return GNUNET_OK; /* all is well-formed */ 430 return GNUNET_OK; /* all is well-formed */
422} 431}
@@ -433,59 +442,59 @@ check_data (void *cls, const struct GNUNET_MessageHeader *message)
433 * @param message The actual message. 442 * @param message The actual message.
434 */ 443 */
435static void 444static void
436handle_data (void *cls, const struct GNUNET_MessageHeader *message) 445handle_data(void *cls, const struct GNUNET_MessageHeader *message)
437{ 446{
438 size_t payload_size = ntohs (message->size) - sizeof (*message); 447 size_t payload_size = ntohs(message->size) - sizeof(*message);
439 uint16_t len; 448 uint16_t len;
440 ssize_t done; 449 ssize_t done;
441 uint16_t off; 450 uint16_t off;
442 const char *buf; 451 const char *buf;
443 452
444 GNUNET_CADET_receive_done (ch); 453 GNUNET_CADET_receive_done(ch);
445 if (GNUNET_YES == echo) 454 if (GNUNET_YES == echo)
446 {
447 if (NULL != listen_port)
448 {
449 struct GNUNET_MQ_Envelope *env;
450 struct GNUNET_MessageHeader *msg;
451
452 env =
453 GNUNET_MQ_msg_extra (msg, payload_size, GNUNET_MESSAGE_TYPE_CADET_CLI);
454 GNUNET_memcpy (&msg[1], &message[1], payload_size);
455 GNUNET_MQ_send (GNUNET_CADET_get_mq (ch), env);
456 return;
457 }
458 else
459 { 455 {
460 struct GNUNET_TIME_Relative latency; 456 if (NULL != listen_port)
461 457 {
462 latency = GNUNET_TIME_absolute_get_duration (echo_time); 458 struct GNUNET_MQ_Envelope *env;
463 echo_time = GNUNET_TIME_UNIT_FOREVER_ABS; 459 struct GNUNET_MessageHeader *msg;
464 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 460
465 "time: %s\n", 461 env =
466 GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO)); 462 GNUNET_MQ_msg_extra(msg, payload_size, GNUNET_MESSAGE_TYPE_CADET_CLI);
467 echo_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, 463 GNUNET_memcpy(&msg[1], &message[1], payload_size);
468 &send_echo, 464 GNUNET_MQ_send(GNUNET_CADET_get_mq(ch), env);
469 NULL); 465 return;
466 }
467 else
468 {
469 struct GNUNET_TIME_Relative latency;
470
471 latency = GNUNET_TIME_absolute_get_duration(echo_time);
472 echo_time = GNUNET_TIME_UNIT_FOREVER_ABS;
473 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
474 "time: %s\n",
475 GNUNET_STRINGS_relative_time_to_string(latency, GNUNET_NO));
476 echo_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
477 &send_echo,
478 NULL);
479 }
470 } 480 }
471 }
472 481
473 len = ntohs (message->size) - sizeof (*message); 482 len = ntohs(message->size) - sizeof(*message);
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes\n", len); 483 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes\n", len);
475 buf = (const char *) &message[1]; 484 buf = (const char *)&message[1];
476 off = 0; 485 off = 0;
477 while (off < len) 486 while (off < len)
478 {
479 done = write (1, &buf[off], len - off);
480 if (done <= 0)
481 { 487 {
482 if (-1 == done) 488 done = write(1, &buf[off], len - off);
483 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "write"); 489 if (done <= 0)
484 GNUNET_SCHEDULER_shutdown (); 490 {
485 return; 491 if (-1 == done)
492 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "write");
493 GNUNET_SCHEDULER_shutdown();
494 return;
495 }
496 off += done;
486 } 497 }
487 off += done;
488 }
489} 498}
490 499
491 500
@@ -499,19 +508,19 @@ handle_data (void *cls, const struct GNUNET_MessageHeader *message)
499 * @param ple information about peer, or NULL on "EOF". 508 * @param ple information about peer, or NULL on "EOF".
500 */ 509 */
501static void 510static void
502peers_callback (void *cls, const struct GNUNET_CADET_PeerListEntry *ple) 511peers_callback(void *cls, const struct GNUNET_CADET_PeerListEntry *ple)
503{ 512{
504 if (NULL == ple) 513 if (NULL == ple)
505 { 514 {
506 plo = NULL; 515 plo = NULL;
507 GNUNET_SCHEDULER_shutdown (); 516 GNUNET_SCHEDULER_shutdown();
508 return; 517 return;
509 } 518 }
510 fprintf (stdout, 519 fprintf(stdout,
511 "%s tunnel: %c, paths: %u\n", 520 "%s tunnel: %c, paths: %u\n",
512 GNUNET_i2s_full (&ple->peer), 521 GNUNET_i2s_full(&ple->peer),
513 ple->have_tunnel ? 'Y' : 'N', 522 ple->have_tunnel ? 'Y' : 'N',
514 ple->n_paths); 523 ple->n_paths);
515} 524}
516 525
517 526
@@ -523,20 +532,20 @@ peers_callback (void *cls, const struct GNUNET_CADET_PeerListEntry *ple)
523 * @param ppd path detail 532 * @param ppd path detail
524 */ 533 */
525static void 534static void
526path_callback (void *cls, const struct GNUNET_CADET_PeerPathDetail *ppd) 535path_callback(void *cls, const struct GNUNET_CADET_PeerPathDetail *ppd)
527{ 536{
528 if (NULL == ppd) 537 if (NULL == ppd)
529 { 538 {
530 gpo = NULL; 539 gpo = NULL;
531 GNUNET_SCHEDULER_shutdown (); 540 GNUNET_SCHEDULER_shutdown();
532 return; 541 return;
533 } 542 }
534 fprintf (stdout, "Path of length %u: ", ppd->path_length); 543 fprintf(stdout, "Path of length %u: ", ppd->path_length);
535 for (unsigned int i = 0; i < ppd->path_length; i++) 544 for (unsigned int i = 0; i < ppd->path_length; i++)
536 fprintf (stdout, 545 fprintf(stdout,
537 (i == ppd->target_offset) ? "*%s* " : "%s ", 546 (i == ppd->target_offset) ? "*%s* " : "%s ",
538 GNUNET_i2s (&ppd->path[i])); 547 GNUNET_i2s(&ppd->path[i]));
539 fprintf (stdout, "\n"); 548 fprintf(stdout, "\n");
540} 549}
541 550
542 551
@@ -547,21 +556,21 @@ path_callback (void *cls, const struct GNUNET_CADET_PeerPathDetail *ppd)
547 * @param td tunnel details 556 * @param td tunnel details
548 */ 557 */
549static void 558static void
550tunnels_callback (void *cls, const struct GNUNET_CADET_TunnelDetails *td) 559tunnels_callback(void *cls, const struct GNUNET_CADET_TunnelDetails *td)
551{ 560{
552 if (NULL == td) 561 if (NULL == td)
553 { 562 {
554 tio = NULL; 563 tio = NULL;
555 GNUNET_SCHEDULER_shutdown (); 564 GNUNET_SCHEDULER_shutdown();
556 return; 565 return;
557 } 566 }
558 fprintf (stdout, 567 fprintf(stdout,
559 "%s [ENC: %s, CON: %s] CHs: %u, CONNs: %u\n", 568 "%s [ENC: %s, CON: %s] CHs: %u, CONNs: %u\n",
560 GNUNET_i2s_full (&td->peer), 569 GNUNET_i2s_full(&td->peer),
561 enc_2s (td->estate), 570 enc_2s(td->estate),
562 conn_2s (td->cstate), 571 conn_2s(td->cstate),
563 td->channels, 572 td->channels,
564 td->connections); 573 td->connections);
565} 574}
566 575
567 576
@@ -571,10 +580,10 @@ tunnels_callback (void *cls, const struct GNUNET_CADET_TunnelDetails *td)
571 * @param cls Closure (unused). 580 * @param cls Closure (unused).
572 */ 581 */
573static void 582static void
574get_peers (void *cls) 583get_peers(void *cls)
575{ 584{
576 job = NULL; 585 job = NULL;
577 plo = GNUNET_CADET_list_peers (my_cfg, &peers_callback, NULL); 586 plo = GNUNET_CADET_list_peers(my_cfg, &peers_callback, NULL);
578} 587}
579 588
580 589
@@ -584,20 +593,20 @@ get_peers (void *cls)
584 * @param cls Closure (unused). 593 * @param cls Closure (unused).
585 */ 594 */
586static void 595static void
587show_peer (void *cls) 596show_peer(void *cls)
588{ 597{
589 struct GNUNET_PeerIdentity pid; 598 struct GNUNET_PeerIdentity pid;
590 599
591 job = NULL; 600 job = NULL;
592 if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id, 601 if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string(peer_id,
593 strlen (peer_id), 602 strlen(peer_id),
594 &pid.public_key)) 603 &pid.public_key))
595 { 604 {
596 fprintf (stderr, _ ("Invalid peer ID `%s'\n"), peer_id); 605 fprintf(stderr, _("Invalid peer ID `%s'\n"), peer_id);
597 GNUNET_SCHEDULER_shutdown (); 606 GNUNET_SCHEDULER_shutdown();
598 return; 607 return;
599 } 608 }
600 gpo = GNUNET_CADET_get_path (my_cfg, &pid, &path_callback, NULL); 609 gpo = GNUNET_CADET_get_path(my_cfg, &pid, &path_callback, NULL);
601} 610}
602 611
603 612
@@ -607,10 +616,10 @@ show_peer (void *cls)
607 * @param cls Closure (unused). 616 * @param cls Closure (unused).
608 */ 617 */
609static void 618static void
610get_tunnels (void *cls) 619get_tunnels(void *cls)
611{ 620{
612 job = NULL; 621 job = NULL;
613 tio = GNUNET_CADET_list_tunnels (my_cfg, &tunnels_callback, NULL); 622 tio = GNUNET_CADET_list_tunnels(my_cfg, &tunnels_callback, NULL);
614} 623}
615 624
616 625
@@ -620,10 +629,10 @@ get_tunnels (void *cls)
620 * @param cls Closure (unused). 629 * @param cls Closure (unused).
621 */ 630 */
622static void 631static void
623show_channel (void *cls) 632show_channel(void *cls)
624{ 633{
625 job = NULL; 634 job = NULL;
626 GNUNET_break (0); 635 GNUNET_break(0);
627} 636}
628 637
629 638
@@ -633,10 +642,10 @@ show_channel (void *cls)
633 * @param cls Closure (unused). 642 * @param cls Closure (unused).
634 */ 643 */
635static void 644static void
636show_connection (void *cls) 645show_connection(void *cls)
637{ 646{
638 job = NULL; 647 job = NULL;
639 GNUNET_break (0); 648 GNUNET_break(0);
640} 649}
641 650
642 651
@@ -649,17 +658,17 @@ show_connection (void *cls)
649 * @param cfg configuration 658 * @param cfg configuration
650 */ 659 */
651static void 660static void
652run (void *cls, 661run(void *cls,
653 char *const *args, 662 char *const *args,
654 const char *cfgfile, 663 const char *cfgfile,
655 const struct GNUNET_CONFIGURATION_Handle *cfg) 664 const struct GNUNET_CONFIGURATION_Handle *cfg)
656{ 665{
657 struct GNUNET_MQ_MessageHandler handlers[] = 666 struct GNUNET_MQ_MessageHandler handlers[] =
658 {GNUNET_MQ_hd_var_size (data, 667 { GNUNET_MQ_hd_var_size(data,
659 GNUNET_MESSAGE_TYPE_CADET_CLI, 668 GNUNET_MESSAGE_TYPE_CADET_CLI,
660 struct GNUNET_MessageHeader, 669 struct GNUNET_MessageHeader,
661 NULL), 670 NULL),
662 GNUNET_MQ_handler_end ()}; 671 GNUNET_MQ_handler_end() };
663 672
664 /* FIXME add option to monitor apps */ 673 /* FIXME add option to monitor apps */
665 my_cfg = cfg; 674 my_cfg = cfg;
@@ -670,102 +679,102 @@ run (void *cls,
670 if ((0 != (request_peers | request_tunnels) || NULL != conn_id || 679 if ((0 != (request_peers | request_tunnels) || NULL != conn_id ||
671 NULL != channel_id) && 680 NULL != channel_id) &&
672 target_id != NULL) 681 target_id != NULL)
673 { 682 {
674 fprintf (stderr, 683 fprintf(stderr,
675 _ ("Extra arguments are not applicable " 684 _("Extra arguments are not applicable "
676 "in combination with this option.\n")); 685 "in combination with this option.\n"));
677 return; 686 return;
678 } 687 }
679 688
680 if (NULL != peer_id) 689 if (NULL != peer_id)
681 { 690 {
682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show peer\n"); 691 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Show peer\n");
683 job = GNUNET_SCHEDULER_add_now (&show_peer, NULL); 692 job = GNUNET_SCHEDULER_add_now(&show_peer, NULL);
684 } 693 }
685 else if (NULL != channel_id) 694 else if (NULL != channel_id)
686 { 695 {
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show channel\n"); 696 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Show channel\n");
688 job = GNUNET_SCHEDULER_add_now (&show_channel, NULL); 697 job = GNUNET_SCHEDULER_add_now(&show_channel, NULL);
689 } 698 }
690 else if (NULL != conn_id) 699 else if (NULL != conn_id)
691 { 700 {
692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show connection\n"); 701 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Show connection\n");
693 job = GNUNET_SCHEDULER_add_now (&show_connection, NULL); 702 job = GNUNET_SCHEDULER_add_now(&show_connection, NULL);
694 } 703 }
695 else if (GNUNET_YES == request_peers) 704 else if (GNUNET_YES == request_peers)
696 { 705 {
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all peers\n"); 706 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Show all peers\n");
698 job = GNUNET_SCHEDULER_add_now (&get_peers, NULL); 707 job = GNUNET_SCHEDULER_add_now(&get_peers, NULL);
699 } 708 }
700 else if (GNUNET_YES == request_tunnels) 709 else if (GNUNET_YES == request_tunnels)
701 { 710 {
702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all tunnels\n"); 711 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Show all tunnels\n");
703 job = GNUNET_SCHEDULER_add_now (&get_tunnels, NULL); 712 job = GNUNET_SCHEDULER_add_now(&get_tunnels, NULL);
704 } 713 }
705
706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET service\n");
707 mh = GNUNET_CADET_connect (cfg);
708 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
709 if (NULL == mh)
710 {
711 GNUNET_SCHEDULER_shutdown ();
712 return;
713 }
714 if (NULL != listen_port)
715 {
716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Opening CADET listen port\n");
717 GNUNET_CRYPTO_hash (listen_port, strlen (listen_port), &porthash);
718 lp = GNUNET_CADET_open_port (mh,
719 &porthash,
720 &channel_incoming,
721 NULL,
722 NULL /* window changes */,
723 &channel_ended,
724 handlers);
725 }
726 if (NULL != target_id)
727 {
728 struct GNUNET_PeerIdentity pid;
729 714
730 if (GNUNET_OK != 715 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET service\n");
731 GNUNET_CRYPTO_eddsa_public_key_from_string (target_id, 716 mh = GNUNET_CADET_connect(cfg);
732 strlen (target_id), 717 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, NULL);
733 &pid.public_key)) 718 if (NULL == mh)
734 { 719 {
735 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, 720 GNUNET_SCHEDULER_shutdown();
736 _ ("Invalid target `%s'\n"),
737 target_id);
738 GNUNET_SCHEDULER_shutdown ();
739 return; 721 return;
740 } 722 }
741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 723 if (NULL != listen_port)
742 "Connecting to `%s:%s'\n",
743 target_id,
744 target_port);
745 GNUNET_CRYPTO_hash (target_port, strlen (target_port), &porthash);
746 ch = GNUNET_CADET_channel_create (mh,
747 NULL,
748 &pid,
749 &porthash,
750 NULL /* window changes */,
751 &channel_ended,
752 handlers);
753 if (GNUNET_YES == echo)
754 { 724 {
755 echo_task = GNUNET_SCHEDULER_add_now (&send_echo, NULL); 725 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Opening CADET listen port\n");
726 GNUNET_CRYPTO_hash(listen_port, strlen(listen_port), &porthash);
727 lp = GNUNET_CADET_open_port(mh,
728 &porthash,
729 &channel_incoming,
730 NULL,
731 NULL /* window changes */,
732 &channel_ended,
733 handlers);
756 } 734 }
757 else 735 if (NULL != target_id)
758 { 736 {
759 listen_stdio (); 737 struct GNUNET_PeerIdentity pid;
738
739 if (GNUNET_OK !=
740 GNUNET_CRYPTO_eddsa_public_key_from_string(target_id,
741 strlen(target_id),
742 &pid.public_key))
743 {
744 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE,
745 _("Invalid target `%s'\n"),
746 target_id);
747 GNUNET_SCHEDULER_shutdown();
748 return;
749 }
750 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
751 "Connecting to `%s:%s'\n",
752 target_id,
753 target_port);
754 GNUNET_CRYPTO_hash(target_port, strlen(target_port), &porthash);
755 ch = GNUNET_CADET_channel_create(mh,
756 NULL,
757 &pid,
758 &porthash,
759 NULL /* window changes */,
760 &channel_ended,
761 handlers);
762 if (GNUNET_YES == echo)
763 {
764 echo_task = GNUNET_SCHEDULER_add_now(&send_echo, NULL);
765 }
766 else
767 {
768 listen_stdio();
769 }
760 } 770 }
761 }
762 771
763 if ((NULL == lp) && (NULL == job) && (NULL == ch)) 772 if ((NULL == lp) && (NULL == job) && (NULL == ch))
764 { 773 {
765 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, _ ("No action requested\n")); 774 GNUNET_log(GNUNET_ERROR_TYPE_MESSAGE, _("No action requested\n"));
766 GNUNET_SCHEDULER_shutdown (); 775 GNUNET_SCHEDULER_shutdown();
767 return; 776 return;
768 } 777 }
769} 778}
770 779
771 780
@@ -777,60 +786,61 @@ run (void *cls,
777 * @return 0 ok, 1 on error 786 * @return 0 ok, 1 on error
778 */ 787 */
779int 788int
780main (int argc, char *const *argv) 789main(int argc, char *const *argv)
781{ 790{
782 int res; 791 int res;
783 const char helpstr[] = 792 const char helpstr[] =
784 "Create tunnels and retrieve info about CADET's status."; 793 "Create tunnels and retrieve info about CADET's status.";
785 struct GNUNET_GETOPT_CommandLineOption options[] = 794 struct GNUNET_GETOPT_CommandLineOption options[] =
786 {/* I would use the terminology 'circuit' here... --lynX */ 795 { /* I would use the terminology 'circuit' here... --lynX */
787 GNUNET_GETOPT_option_string ( 796 GNUNET_GETOPT_option_string(
788 'C', 797 'C',
789 "connection", 798 "connection",
790 "CONNECTION_ID", 799 "CONNECTION_ID",
791 gettext_noop ("Provide information about a particular connection"), 800 gettext_noop("Provide information about a particular connection"),
792 &conn_id), 801 &conn_id),
793 GNUNET_GETOPT_option_flag ('e', 802 GNUNET_GETOPT_option_flag('e',
794 "echo", 803 "echo",
795 gettext_noop ("Activate echo mode"), 804 gettext_noop("Activate echo mode"),
796 &echo), 805 &echo),
797 GNUNET_GETOPT_option_string ( 806 GNUNET_GETOPT_option_string(
798 'o', 807 'o',
799 "open-port", 808 "open-port",
800 "SHARED_SECRET", 809 "SHARED_SECRET",
801 gettext_noop ( 810 gettext_noop(
802 "Listen for connections using a shared secret among sender and recipient"), 811 "Listen for connections using a shared secret among sender and recipient"),
803 &listen_port), 812 &listen_port),
804 GNUNET_GETOPT_option_string ('p', 813 GNUNET_GETOPT_option_string('p',
805 "peer", 814 "peer",
806 "PEER_ID", 815 "PEER_ID",
807 gettext_noop ( 816 gettext_noop(
808 "Provide information about a patricular peer"), 817 "Provide information about a patricular peer"),
809 &peer_id), 818 &peer_id),
810 GNUNET_GETOPT_option_flag ('P', 819 GNUNET_GETOPT_option_flag('P',
811 "peers", 820 "peers",
812 gettext_noop ( 821 gettext_noop(
813 "Provide information about all peers"), 822 "Provide information about all peers"),
814 &request_peers), 823 &request_peers),
815 GNUNET_GETOPT_option_flag ('T', 824 GNUNET_GETOPT_option_flag('T',
816 "tunnels", 825 "tunnels",
817 gettext_noop ( 826 gettext_noop(
818 "Provide information about all tunnels"), 827 "Provide information about all tunnels"),
819 &request_tunnels), 828 &request_tunnels),
820 GNUNET_GETOPT_OPTION_END}; 829 GNUNET_GETOPT_OPTION_END
821 830 };
822 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 831
832 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
823 return 2; 833 return 2;
824 834
825 res = GNUNET_PROGRAM_run (argc, 835 res = GNUNET_PROGRAM_run(argc,
826 argv, 836 argv,
827 "gnunet-cadet (OPTIONS | PEER_ID SHARED_SECRET)", 837 "gnunet-cadet (OPTIONS | PEER_ID SHARED_SECRET)",
828 gettext_noop (helpstr), 838 gettext_noop(helpstr),
829 options, 839 options,
830 &run, 840 &run,
831 NULL); 841 NULL);
832 842
833 GNUNET_free ((void *) argv); 843 GNUNET_free((void *)argv);
834 844
835 if (GNUNET_OK == res) 845 if (GNUNET_OK == res)
836 return 0; 846 return 0;
diff --git a/src/cadet/gnunet-service-cadet.c b/src/cadet/gnunet-service-cadet.c
index 209f7d392..a06cd184f 100644
--- a/src/cadet/gnunet-service-cadet.c
+++ b/src/cadet/gnunet-service-cadet.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file cadet/gnunet-service-cadet.c 22 * @file cadet/gnunet-service-cadet.c
@@ -46,14 +46,13 @@
46#include "gnunet-service-cadet_peer.h" 46#include "gnunet-service-cadet_peer.h"
47#include "gnunet-service-cadet_paths.h" 47#include "gnunet-service-cadet_paths.h"
48 48
49#define LOG(level, ...) GNUNET_log (level,__VA_ARGS__) 49#define LOG(level, ...) GNUNET_log(level, __VA_ARGS__)
50 50
51 51
52/** 52/**
53 * Struct containing information about a client of the service 53 * Struct containing information about a client of the service
54 */ 54 */
55struct CadetClient 55struct CadetClient {
56{
57 /** 56 /**
58 * Linked list next 57 * Linked list next
59 */ 58 */
@@ -202,11 +201,11 @@ unsigned long long drop_percent;
202 * @param env envelope with the message 201 * @param env envelope with the message
203 */ 202 */
204void 203void
205GSC_send_to_client (struct CadetClient *c, 204GSC_send_to_client(struct CadetClient *c,
206 struct GNUNET_MQ_Envelope *env) 205 struct GNUNET_MQ_Envelope *env)
207{ 206{
208 GNUNET_MQ_send (c->mq, 207 GNUNET_MQ_send(c->mq,
209 env); 208 env);
210} 209}
211 210
212 211
@@ -217,14 +216,14 @@ GSC_send_to_client (struct CadetClient *c,
217 * @return string for debugging 216 * @return string for debugging
218 */ 217 */
219const char * 218const char *
220GSC_2s (struct CadetClient *c) 219GSC_2s(struct CadetClient *c)
221{ 220{
222 static char buf[32]; 221 static char buf[32];
223 222
224 GNUNET_snprintf (buf, 223 GNUNET_snprintf(buf,
225 sizeof (buf), 224 sizeof(buf),
226 "Client(%u)", 225 "Client(%u)",
227 c->id); 226 c->id);
228 return buf; 227 return buf;
229} 228}
230 229
@@ -237,11 +236,11 @@ GSC_2s (struct CadetClient *c)
237 * @return NULL if no such channel exists 236 * @return NULL if no such channel exists
238 */ 237 */
239static struct CadetChannel * 238static struct CadetChannel *
240lookup_channel (struct CadetClient *c, 239lookup_channel(struct CadetClient *c,
241 struct GNUNET_CADET_ClientChannelNumber ccn) 240 struct GNUNET_CADET_ClientChannelNumber ccn)
242{ 241{
243 return GNUNET_CONTAINER_multihashmap32_get (c->channels, 242 return GNUNET_CONTAINER_multihashmap32_get(c->channels,
244 ntohl (ccn.channel_of_client)); 243 ntohl(ccn.channel_of_client));
245} 244}
246 245
247 246
@@ -252,23 +251,23 @@ lookup_channel (struct CadetClient *c,
252 * @param c client handle 251 * @param c client handle
253 */ 252 */
254static struct GNUNET_CADET_ClientChannelNumber 253static struct GNUNET_CADET_ClientChannelNumber
255client_get_next_ccn (struct CadetClient *c) 254client_get_next_ccn(struct CadetClient *c)
256{ 255{
257 struct GNUNET_CADET_ClientChannelNumber ccn = c->next_ccn; 256 struct GNUNET_CADET_ClientChannelNumber ccn = c->next_ccn;
258 257
259 /* increment until we have a free one... */ 258 /* increment until we have a free one... */
260 while (NULL != 259 while (NULL !=
261 lookup_channel (c, 260 lookup_channel(c,
262 ccn)) 261 ccn))
263 { 262 {
264 ccn.channel_of_client 263 ccn.channel_of_client
265 = htonl (1 + (ntohl (ccn.channel_of_client))); 264 = htonl(1 + (ntohl(ccn.channel_of_client)));
266 if (ntohl (ccn.channel_of_client) >= 265 if (ntohl(ccn.channel_of_client) >=
267 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI) 266 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
268 ccn.channel_of_client = htonl (0); 267 ccn.channel_of_client = htonl(0);
269 } 268 }
270 c->next_ccn.channel_of_client 269 c->next_ccn.channel_of_client
271 = htonl (1 + (ntohl (ccn.channel_of_client))); 270 = htonl(1 + (ntohl(ccn.channel_of_client)));
272 return ccn; 271 return ccn;
273} 272}
274 273
@@ -286,38 +285,38 @@ client_get_next_ccn (struct CadetClient *c)
286 * @return local channel number assigned to the new client 285 * @return local channel number assigned to the new client
287 */ 286 */
288struct GNUNET_CADET_ClientChannelNumber 287struct GNUNET_CADET_ClientChannelNumber
289GSC_bind (struct CadetClient *c, 288GSC_bind(struct CadetClient *c,
290 struct CadetChannel *ch, 289 struct CadetChannel *ch,
291 struct CadetPeer *dest, 290 struct CadetPeer *dest,
292 const struct GNUNET_HashCode *port, 291 const struct GNUNET_HashCode *port,
293 uint32_t options) 292 uint32_t options)
294{ 293{
295 struct GNUNET_MQ_Envelope *env; 294 struct GNUNET_MQ_Envelope *env;
296 struct GNUNET_CADET_LocalChannelCreateMessage *cm; 295 struct GNUNET_CADET_LocalChannelCreateMessage *cm;
297 struct GNUNET_CADET_ClientChannelNumber ccn; 296 struct GNUNET_CADET_ClientChannelNumber ccn;
298 297
299 ccn = client_get_next_ccn (c); 298 ccn = client_get_next_ccn(c);
300 GNUNET_assert (GNUNET_YES == 299 GNUNET_assert(GNUNET_YES ==
301 GNUNET_CONTAINER_multihashmap32_put (c->channels, 300 GNUNET_CONTAINER_multihashmap32_put(c->channels,
302 ntohl (ccn.channel_of_client), 301 ntohl(ccn.channel_of_client),
303 ch, 302 ch,
304 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 303 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
305 LOG (GNUNET_ERROR_TYPE_DEBUG, 304 LOG(GNUNET_ERROR_TYPE_DEBUG,
306 "Accepting incoming %s from %s on open port %s (%u), assigning ccn %X\n", 305 "Accepting incoming %s from %s on open port %s (%u), assigning ccn %X\n",
307 GCCH_2s (ch), 306 GCCH_2s(ch),
308 GCP_2s (dest), 307 GCP_2s(dest),
309 GNUNET_h2s (port), 308 GNUNET_h2s(port),
310 (uint32_t) ntohl (options), 309 (uint32_t)ntohl(options),
311 (uint32_t) ntohl (ccn.channel_of_client)); 310 (uint32_t)ntohl(ccn.channel_of_client));
312 /* notify local client about incoming connection! */ 311 /* notify local client about incoming connection! */
313 env = GNUNET_MQ_msg (cm, 312 env = GNUNET_MQ_msg(cm,
314 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE); 313 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE);
315 cm->ccn = ccn; 314 cm->ccn = ccn;
316 cm->port = *port; 315 cm->port = *port;
317 cm->opt = htonl (options); 316 cm->opt = htonl(options);
318 cm->peer = *GCP_get_id (dest); 317 cm->peer = *GCP_get_id(dest);
319 GSC_send_to_client (c, 318 GSC_send_to_client(c,
320 env); 319 env);
321 return ccn; 320 return ccn;
322} 321}
323 322
@@ -332,16 +331,16 @@ GSC_bind (struct CadetClient *c,
332 * @return #GNUNET_OK (iterate over all entries) 331 * @return #GNUNET_OK (iterate over all entries)
333 */ 332 */
334static int 333static int
335destroy_tunnels_now (void *cls, 334destroy_tunnels_now(void *cls,
336 const struct GNUNET_PeerIdentity *pid, 335 const struct GNUNET_PeerIdentity *pid,
337 void *value) 336 void *value)
338{ 337{
339 struct CadetPeer *cp = value; 338 struct CadetPeer *cp = value;
340 struct CadetTunnel *t = GCP_get_tunnel (cp, 339 struct CadetTunnel *t = GCP_get_tunnel(cp,
341 GNUNET_NO); 340 GNUNET_NO);
342 341
343 if (NULL != t) 342 if (NULL != t)
344 GCT_destroy_tunnel_now (t); 343 GCT_destroy_tunnel_now(t);
345 return GNUNET_OK; 344 return GNUNET_OK;
346} 345}
347 346
@@ -356,13 +355,13 @@ destroy_tunnels_now (void *cls,
356 * @return #GNUNET_OK (iterate over all entries) 355 * @return #GNUNET_OK (iterate over all entries)
357 */ 356 */
358static int 357static int
359destroy_paths_now (void *cls, 358destroy_paths_now(void *cls,
360 const struct GNUNET_PeerIdentity *pid, 359 const struct GNUNET_PeerIdentity *pid,
361 void *value) 360 void *value)
362{ 361{
363 struct CadetPeer *cp = value; 362 struct CadetPeer *cp = value;
364 363
365 GCP_drop_owned_paths (cp); 364 GCP_drop_owned_paths(cp);
366 return GNUNET_OK; 365 return GNUNET_OK;
367} 366}
368 367
@@ -371,50 +370,50 @@ destroy_paths_now (void *cls,
371 * Shutdown everything once the clients have disconnected. 370 * Shutdown everything once the clients have disconnected.
372 */ 371 */
373static void 372static void
374shutdown_rest () 373shutdown_rest()
375{ 374{
376 if (NULL != stats) 375 if (NULL != stats)
377 { 376 {
378 GNUNET_STATISTICS_destroy (stats, 377 GNUNET_STATISTICS_destroy(stats,
379 GNUNET_NO); 378 GNUNET_NO);
380 stats = NULL; 379 stats = NULL;
381 } 380 }
382 /* Destroy tunnels. Note that all channels must be destroyed first! */ 381 /* Destroy tunnels. Note that all channels must be destroyed first! */
383 GCP_iterate_all (&destroy_tunnels_now, 382 GCP_iterate_all(&destroy_tunnels_now,
384 NULL); 383 NULL);
385 /* All tunnels, channels, connections and CORE must be down before this point. */ 384 /* All tunnels, channels, connections and CORE must be down before this point. */
386 GCP_iterate_all (&destroy_paths_now, 385 GCP_iterate_all(&destroy_paths_now,
387 NULL); 386 NULL);
388 /* All paths, tunnels, channels, connections and CORE must be down before this point. */ 387 /* All paths, tunnels, channels, connections and CORE must be down before this point. */
389 GCP_destroy_all_peers (); 388 GCP_destroy_all_peers();
390 if (NULL != open_ports) 389 if (NULL != open_ports)
391 { 390 {
392 GNUNET_CONTAINER_multihashmap_destroy (open_ports); 391 GNUNET_CONTAINER_multihashmap_destroy(open_ports);
393 open_ports = NULL; 392 open_ports = NULL;
394 } 393 }
395 if (NULL != loose_channels) 394 if (NULL != loose_channels)
396 { 395 {
397 GNUNET_CONTAINER_multihashmap_destroy (loose_channels); 396 GNUNET_CONTAINER_multihashmap_destroy(loose_channels);
398 loose_channels = NULL; 397 loose_channels = NULL;
399 } 398 }
400 if (NULL != peers) 399 if (NULL != peers)
401 { 400 {
402 GNUNET_CONTAINER_multipeermap_destroy (peers); 401 GNUNET_CONTAINER_multipeermap_destroy(peers);
403 peers = NULL; 402 peers = NULL;
404 } 403 }
405 if (NULL != connections) 404 if (NULL != connections)
406 { 405 {
407 GNUNET_CONTAINER_multishortmap_destroy (connections); 406 GNUNET_CONTAINER_multishortmap_destroy(connections);
408 connections = NULL; 407 connections = NULL;
409 } 408 }
410 if (NULL != ats_ch) 409 if (NULL != ats_ch)
411 { 410 {
412 GNUNET_ATS_connectivity_done (ats_ch); 411 GNUNET_ATS_connectivity_done(ats_ch);
413 ats_ch = NULL; 412 ats_ch = NULL;
414 } 413 }
415 GCD_shutdown (); 414 GCD_shutdown();
416 GCH_shutdown (); 415 GCH_shutdown();
417 GNUNET_free_non_null (my_private_key); 416 GNUNET_free_non_null(my_private_key);
418 my_private_key = NULL; 417 my_private_key = NULL;
419} 418}
420 419
@@ -425,14 +424,14 @@ shutdown_rest ()
425 * @param cls unused 424 * @param cls unused
426 */ 425 */
427static void 426static void
428shutdown_task (void *cls) 427shutdown_task(void *cls)
429{ 428{
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 429 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
431 "Shutting down\n"); 430 "Shutting down\n");
432 shutting_down = GNUNET_YES; 431 shutting_down = GNUNET_YES;
433 GCO_shutdown (); 432 GCO_shutdown();
434 if (NULL == clients_head) 433 if (NULL == clients_head)
435 shutdown_rest (); 434 shutdown_rest();
436} 435}
437 436
438 437
@@ -446,20 +445,20 @@ shutdown_task (void *cls)
446 * @return #GNUNET_YES (iterate over all such channels) 445 * @return #GNUNET_YES (iterate over all such channels)
447 */ 446 */
448static int 447static int
449bind_loose_channel (void *cls, 448bind_loose_channel(void *cls,
450 const struct GNUNET_HashCode *port, 449 const struct GNUNET_HashCode *port,
451 void *value) 450 void *value)
452{ 451{
453 struct OpenPort *op = cls; 452 struct OpenPort *op = cls;
454 struct CadetChannel *ch = value; 453 struct CadetChannel *ch = value;
455 454
456 GCCH_bind (ch, 455 GCCH_bind(ch,
457 op->c, 456 op->c,
458 &op->port); 457 &op->port);
459 GNUNET_assert (GNUNET_YES == 458 GNUNET_assert(GNUNET_YES ==
460 GNUNET_CONTAINER_multihashmap_remove (loose_channels, 459 GNUNET_CONTAINER_multihashmap_remove(loose_channels,
461 &op->h_port, 460 &op->h_port,
462 ch)); 461 ch));
463 return GNUNET_YES; 462 return GNUNET_YES;
464} 463}
465 464
@@ -474,44 +473,44 @@ bind_loose_channel (void *cls,
474 * @param pmsg The actual message. 473 * @param pmsg The actual message.
475 */ 474 */
476static void 475static void
477handle_port_open (void *cls, 476handle_port_open(void *cls,
478 const struct GNUNET_CADET_PortMessage *pmsg) 477 const struct GNUNET_CADET_PortMessage *pmsg)
479{ 478{
480 struct CadetClient *c = cls; 479 struct CadetClient *c = cls;
481 struct OpenPort *op; 480 struct OpenPort *op;
482 481
483 LOG (GNUNET_ERROR_TYPE_DEBUG, 482 LOG(GNUNET_ERROR_TYPE_DEBUG,
484 "Open port %s requested by %s\n", 483 "Open port %s requested by %s\n",
485 GNUNET_h2s (&pmsg->port), 484 GNUNET_h2s(&pmsg->port),
486 GSC_2s (c)); 485 GSC_2s(c));
487 if (NULL == c->ports) 486 if (NULL == c->ports)
488 c->ports = GNUNET_CONTAINER_multihashmap_create (4, 487 c->ports = GNUNET_CONTAINER_multihashmap_create(4,
489 GNUNET_NO); 488 GNUNET_NO);
490 op = GNUNET_new (struct OpenPort); 489 op = GNUNET_new(struct OpenPort);
491 op->c = c; 490 op->c = c;
492 op->port = pmsg->port; 491 op->port = pmsg->port;
493 GCCH_hash_port (&op->h_port, 492 GCCH_hash_port(&op->h_port,
494 &pmsg->port, 493 &pmsg->port,
495 &my_full_id); 494 &my_full_id);
496 if (GNUNET_OK != 495 if (GNUNET_OK !=
497 GNUNET_CONTAINER_multihashmap_put (c->ports, 496 GNUNET_CONTAINER_multihashmap_put(c->ports,
498 &op->port, 497 &op->port,
499 op, 498 op,
500 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 499 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
501 { 500 {
502 GNUNET_break (0); 501 GNUNET_break(0);
503 GNUNET_SERVICE_client_drop (c->client); 502 GNUNET_SERVICE_client_drop(c->client);
504 return; 503 return;
505 } 504 }
506 (void) GNUNET_CONTAINER_multihashmap_put (open_ports, 505 (void)GNUNET_CONTAINER_multihashmap_put(open_ports,
507 &op->h_port, 506 &op->h_port,
508 op, 507 op,
509 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 508 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
510 GNUNET_CONTAINER_multihashmap_get_multiple (loose_channels, 509 GNUNET_CONTAINER_multihashmap_get_multiple(loose_channels,
511 &op->h_port, 510 &op->h_port,
512 &bind_loose_channel, 511 &bind_loose_channel,
513 op); 512 op);
514 GNUNET_SERVICE_client_continue (c->client); 513 GNUNET_SERVICE_client_continue(c->client);
515} 514}
516 515
517 516
@@ -525,41 +524,41 @@ handle_port_open (void *cls,
525 * @param pmsg The actual message. 524 * @param pmsg The actual message.
526 */ 525 */
527static void 526static void
528handle_port_close (void *cls, 527handle_port_close(void *cls,
529 const struct GNUNET_CADET_PortMessage *pmsg) 528 const struct GNUNET_CADET_PortMessage *pmsg)
530{ 529{
531 struct CadetClient *c = cls; 530 struct CadetClient *c = cls;
532 struct OpenPort *op; 531 struct OpenPort *op;
533 532
534 LOG (GNUNET_ERROR_TYPE_DEBUG, 533 LOG(GNUNET_ERROR_TYPE_DEBUG,
535 "Closing port %s as requested by %s\n", 534 "Closing port %s as requested by %s\n",
536 GNUNET_h2s (&pmsg->port), 535 GNUNET_h2s(&pmsg->port),
537 GSC_2s (c)); 536 GSC_2s(c));
538 if (NULL == c->ports) 537 if (NULL == c->ports)
539 { 538 {
540 /* Client closed a port despite _never_ having opened one? */ 539 /* Client closed a port despite _never_ having opened one? */
541 GNUNET_break (0); 540 GNUNET_break(0);
542 GNUNET_SERVICE_client_drop (c->client); 541 GNUNET_SERVICE_client_drop(c->client);
543 return; 542 return;
544 } 543 }
545 op = GNUNET_CONTAINER_multihashmap_get (c->ports, 544 op = GNUNET_CONTAINER_multihashmap_get(c->ports,
546 &pmsg->port); 545 &pmsg->port);
547 if (NULL == op) 546 if (NULL == op)
548 { 547 {
549 GNUNET_break (0); 548 GNUNET_break(0);
550 GNUNET_SERVICE_client_drop (c->client); 549 GNUNET_SERVICE_client_drop(c->client);
551 return; 550 return;
552 } 551 }
553 GNUNET_assert (GNUNET_YES == 552 GNUNET_assert(GNUNET_YES ==
554 GNUNET_CONTAINER_multihashmap_remove (c->ports, 553 GNUNET_CONTAINER_multihashmap_remove(c->ports,
555 &op->port, 554 &op->port,
556 op)); 555 op));
557 GNUNET_assert (GNUNET_YES == 556 GNUNET_assert(GNUNET_YES ==
558 GNUNET_CONTAINER_multihashmap_remove (open_ports, 557 GNUNET_CONTAINER_multihashmap_remove(open_ports,
559 &op->h_port, 558 &op->h_port,
560 op)); 559 op));
561 GNUNET_free (op); 560 GNUNET_free(op);
562 GNUNET_SERVICE_client_continue (c->client); 561 GNUNET_SERVICE_client_continue(c->client);
563} 562}
564 563
565 564
@@ -570,54 +569,54 @@ handle_port_close (void *cls,
570 * @param tcm The actual message. 569 * @param tcm The actual message.
571 */ 570 */
572static void 571static void
573handle_channel_create (void *cls, 572handle_channel_create(void *cls,
574 const struct GNUNET_CADET_LocalChannelCreateMessage *tcm) 573 const struct GNUNET_CADET_LocalChannelCreateMessage *tcm)
575{ 574{
576 struct CadetClient *c = cls; 575 struct CadetClient *c = cls;
577 struct CadetChannel *ch; 576 struct CadetChannel *ch;
578 577
579 if (ntohl (tcm->ccn.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI) 578 if (ntohl(tcm->ccn.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
580 { 579 {
581 /* Channel ID not in allowed range. */ 580 /* Channel ID not in allowed range. */
582 GNUNET_break (0); 581 GNUNET_break(0);
583 GNUNET_SERVICE_client_drop (c->client); 582 GNUNET_SERVICE_client_drop(c->client);
584 return; 583 return;
585 } 584 }
586 ch = lookup_channel (c, 585 ch = lookup_channel(c,
587 tcm->ccn); 586 tcm->ccn);
588 if (NULL != ch) 587 if (NULL != ch)
589 { 588 {
590 /* Channel ID already in use. Not allowed. */ 589 /* Channel ID already in use. Not allowed. */
591 GNUNET_break (0); 590 GNUNET_break(0);
592 GNUNET_SERVICE_client_drop (c->client); 591 GNUNET_SERVICE_client_drop(c->client);
593 return; 592 return;
594 } 593 }
595 LOG (GNUNET_ERROR_TYPE_DEBUG, 594 LOG(GNUNET_ERROR_TYPE_DEBUG,
596 "New channel to %s at port %s requested by %s\n", 595 "New channel to %s at port %s requested by %s\n",
597 GNUNET_i2s (&tcm->peer), 596 GNUNET_i2s(&tcm->peer),
598 GNUNET_h2s (&tcm->port), 597 GNUNET_h2s(&tcm->port),
599 GSC_2s (c)); 598 GSC_2s(c));
600 599
601 /* Create channel */ 600 /* Create channel */
602 ch = GCCH_channel_local_new (c, 601 ch = GCCH_channel_local_new(c,
603 tcm->ccn, 602 tcm->ccn,
604 GCP_get (&tcm->peer, 603 GCP_get(&tcm->peer,
605 GNUNET_YES), 604 GNUNET_YES),
606 &tcm->port, 605 &tcm->port,
607 ntohl (tcm->opt)); 606 ntohl(tcm->opt));
608 if (NULL == ch) 607 if (NULL == ch)
609 { 608 {
610 GNUNET_break (0); 609 GNUNET_break(0);
611 GNUNET_SERVICE_client_drop (c->client); 610 GNUNET_SERVICE_client_drop(c->client);
612 return; 611 return;
613 } 612 }
614 GNUNET_assert (GNUNET_YES == 613 GNUNET_assert(GNUNET_YES ==
615 GNUNET_CONTAINER_multihashmap32_put (c->channels, 614 GNUNET_CONTAINER_multihashmap32_put(c->channels,
616 ntohl (tcm->ccn.channel_of_client), 615 ntohl(tcm->ccn.channel_of_client),
617 ch, 616 ch,
618 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 617 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
619 618
620 GNUNET_SERVICE_client_continue (c->client); 619 GNUNET_SERVICE_client_continue(c->client);
621} 620}
622 621
623 622
@@ -628,37 +627,37 @@ handle_channel_create (void *cls,
628 * @param msg the actual message 627 * @param msg the actual message
629 */ 628 */
630static void 629static void
631handle_channel_destroy (void *cls, 630handle_channel_destroy(void *cls,
632 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg) 631 const struct GNUNET_CADET_LocalChannelDestroyMessage *msg)
633{ 632{
634 struct CadetClient *c = cls; 633 struct CadetClient *c = cls;
635 struct CadetChannel *ch; 634 struct CadetChannel *ch;
636 635
637 ch = lookup_channel (c, 636 ch = lookup_channel(c,
638 msg->ccn); 637 msg->ccn);
639 if (NULL == ch) 638 if (NULL == ch)
640 { 639 {
641 /* Client attempted to destroy unknown channel. 640 /* Client attempted to destroy unknown channel.
642 Can happen if the other side went down at the same time.*/ 641 Can happen if the other side went down at the same time.*/
643 LOG (GNUNET_ERROR_TYPE_DEBUG, 642 LOG(GNUNET_ERROR_TYPE_DEBUG,
644 "%s tried to destroy unknown channel %X\n", 643 "%s tried to destroy unknown channel %X\n",
645 GSC_2s(c), 644 GSC_2s(c),
646 (uint32_t) ntohl (msg->ccn.channel_of_client)); 645 (uint32_t)ntohl(msg->ccn.channel_of_client));
647 GNUNET_SERVICE_client_continue (c->client); 646 GNUNET_SERVICE_client_continue(c->client);
648 return; 647 return;
649 } 648 }
650 LOG (GNUNET_ERROR_TYPE_DEBUG, 649 LOG(GNUNET_ERROR_TYPE_DEBUG,
651 "%s is destroying %s\n", 650 "%s is destroying %s\n",
652 GSC_2s(c), 651 GSC_2s(c),
653 GCCH_2s (ch)); 652 GCCH_2s(ch));
654 GNUNET_assert (GNUNET_YES == 653 GNUNET_assert(GNUNET_YES ==
655 GNUNET_CONTAINER_multihashmap32_remove (c->channels, 654 GNUNET_CONTAINER_multihashmap32_remove(c->channels,
656 ntohl (msg->ccn.channel_of_client), 655 ntohl(msg->ccn.channel_of_client),
657 ch)); 656 ch));
658 GCCH_channel_local_destroy (ch, 657 GCCH_channel_local_destroy(ch,
659 c, 658 c,
660 msg->ccn); 659 msg->ccn);
661 GNUNET_SERVICE_client_continue (c->client); 660 GNUNET_SERVICE_client_continue(c->client);
662} 661}
663 662
664 663
@@ -670,8 +669,8 @@ handle_channel_destroy (void *cls,
670 * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not 669 * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not
671 */ 670 */
672static int 671static int
673check_local_data (void *cls, 672check_local_data(void *cls,
674 const struct GNUNET_CADET_LocalData *msg) 673 const struct GNUNET_CADET_LocalData *msg)
675{ 674{
676 size_t payload_size; 675 size_t payload_size;
677 size_t payload_claimed_size; 676 size_t payload_claimed_size;
@@ -684,36 +683,36 @@ check_local_data (void *cls,
684 cadet_api will likely no more, so we could then 683 cadet_api will likely no more, so we could then
685 simplify this mess again. */ 684 simplify this mess again. */
686 /* Sanity check for message size */ 685 /* Sanity check for message size */
687 payload_size = ntohs (msg->header.size) - sizeof (*msg); 686 payload_size = ntohs(msg->header.size) - sizeof(*msg);
688 buf = (const char *) &msg[1]; 687 buf = (const char *)&msg[1];
689 while (payload_size >= sizeof (struct GNUNET_MessageHeader)) 688 while (payload_size >= sizeof(struct GNUNET_MessageHeader))
690 {
691 /* need to memcpy() for alignment */
692 GNUNET_memcpy (&pa,
693 buf,
694 sizeof (pa));
695 payload_claimed_size = ntohs (pa.size);
696 if ( (payload_size < payload_claimed_size) ||
697 (payload_claimed_size < sizeof (struct GNUNET_MessageHeader)) ||
698 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size) )
699 { 689 {
700 GNUNET_break (0); 690 /* need to memcpy() for alignment */
701 LOG (GNUNET_ERROR_TYPE_DEBUG, 691 GNUNET_memcpy(&pa,
702 "Local data of %u total size had sub-message %u at %u with %u bytes\n", 692 buf,
703 ntohs (msg->header.size), 693 sizeof(pa));
704 ntohs (pa.type), 694 payload_claimed_size = ntohs(pa.size);
705 (unsigned int) (buf - (const char *) &msg[1]), 695 if ((payload_size < payload_claimed_size) ||
706 (unsigned int) payload_claimed_size); 696 (payload_claimed_size < sizeof(struct GNUNET_MessageHeader)) ||
707 return GNUNET_SYSERR; 697 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_claimed_size))
698 {
699 GNUNET_break(0);
700 LOG(GNUNET_ERROR_TYPE_DEBUG,
701 "Local data of %u total size had sub-message %u at %u with %u bytes\n",
702 ntohs(msg->header.size),
703 ntohs(pa.type),
704 (unsigned int)(buf - (const char *)&msg[1]),
705 (unsigned int)payload_claimed_size);
706 return GNUNET_SYSERR;
707 }
708 payload_size -= payload_claimed_size;
709 buf += payload_claimed_size;
708 } 710 }
709 payload_size -= payload_claimed_size;
710 buf += payload_claimed_size;
711 }
712 if (0 != payload_size) 711 if (0 != payload_size)
713 { 712 {
714 GNUNET_break_op (0); 713 GNUNET_break_op(0);
715 return GNUNET_SYSERR; 714 return GNUNET_SYSERR;
716 } 715 }
717 return GNUNET_OK; 716 return GNUNET_OK;
718} 717}
719 718
@@ -726,47 +725,47 @@ check_local_data (void *cls,
726 * @param msg the actual message 725 * @param msg the actual message
727 */ 726 */
728static void 727static void
729handle_local_data (void *cls, 728handle_local_data(void *cls,
730 const struct GNUNET_CADET_LocalData *msg) 729 const struct GNUNET_CADET_LocalData *msg)
731{ 730{
732 struct CadetClient *c = cls; 731 struct CadetClient *c = cls;
733 struct CadetChannel *ch; 732 struct CadetChannel *ch;
734 size_t payload_size; 733 size_t payload_size;
735 const char *buf; 734 const char *buf;
736 735
737 ch = lookup_channel (c, 736 ch = lookup_channel(c,
738 msg->ccn); 737 msg->ccn);
739 if (NULL == ch) 738 if (NULL == ch)
740 { 739 {
741 /* Channel does not exist (anymore) */ 740 /* Channel does not exist (anymore) */
742 LOG (GNUNET_ERROR_TYPE_WARNING, 741 LOG(GNUNET_ERROR_TYPE_WARNING,
743 "Dropping payload for channel %u from client (channel unknown, other endpoint may have disconnected)\n", 742 "Dropping payload for channel %u from client (channel unknown, other endpoint may have disconnected)\n",
744 (unsigned int) ntohl (msg->ccn.channel_of_client)); 743 (unsigned int)ntohl(msg->ccn.channel_of_client));
745 GNUNET_SERVICE_client_continue (c->client); 744 GNUNET_SERVICE_client_continue(c->client);
746 return; 745 return;
747 } 746 }
748 payload_size = ntohs (msg->header.size) - sizeof (*msg); 747 payload_size = ntohs(msg->header.size) - sizeof(*msg);
749 GNUNET_STATISTICS_update (stats, 748 GNUNET_STATISTICS_update(stats,
750 "# payload received from clients", 749 "# payload received from clients",
751 payload_size, 750 payload_size,
752 GNUNET_NO); 751 GNUNET_NO);
753 buf = (const char *) &msg[1]; 752 buf = (const char *)&msg[1];
754 LOG (GNUNET_ERROR_TYPE_DEBUG, 753 LOG(GNUNET_ERROR_TYPE_DEBUG,
755 "Received %u bytes payload from %s for %s\n", 754 "Received %u bytes payload from %s for %s\n",
756 (unsigned int) payload_size, 755 (unsigned int)payload_size,
757 GSC_2s (c), 756 GSC_2s(c),
758 GCCH_2s (ch)); 757 GCCH_2s(ch));
759 if (GNUNET_OK != 758 if (GNUNET_OK !=
760 GCCH_handle_local_data (ch, 759 GCCH_handle_local_data(ch,
761 msg->ccn, 760 msg->ccn,
762 buf, 761 buf,
763 payload_size)) 762 payload_size))
764 { 763 {
765 GNUNET_break (0); 764 GNUNET_break(0);
766 GNUNET_SERVICE_client_drop (c->client); 765 GNUNET_SERVICE_client_drop(c->client);
767 return; 766 return;
768 } 767 }
769 GNUNET_SERVICE_client_continue (c->client); 768 GNUNET_SERVICE_client_continue(c->client);
770} 769}
771 770
772 771
@@ -777,30 +776,30 @@ handle_local_data (void *cls,
777 * @param msg The actual message. 776 * @param msg The actual message.
778 */ 777 */
779static void 778static void
780handle_local_ack (void *cls, 779handle_local_ack(void *cls,
781 const struct GNUNET_CADET_LocalAck *msg) 780 const struct GNUNET_CADET_LocalAck *msg)
782{ 781{
783 struct CadetClient *c = cls; 782 struct CadetClient *c = cls;
784 struct CadetChannel *ch; 783 struct CadetChannel *ch;
785 784
786 ch = lookup_channel (c, 785 ch = lookup_channel(c,
787 msg->ccn); 786 msg->ccn);
788 if (NULL == ch) 787 if (NULL == ch)
789 { 788 {
790 /* Channel does not exist (anymore) */ 789 /* Channel does not exist (anymore) */
791 LOG (GNUNET_ERROR_TYPE_WARNING, 790 LOG(GNUNET_ERROR_TYPE_WARNING,
792 "Ignoring local ACK for channel %u from client (channel unknown, other endpoint may have disconnected)\n", 791 "Ignoring local ACK for channel %u from client (channel unknown, other endpoint may have disconnected)\n",
793 (unsigned int) ntohl (msg->ccn.channel_of_client)); 792 (unsigned int)ntohl(msg->ccn.channel_of_client));
794 GNUNET_SERVICE_client_continue (c->client); 793 GNUNET_SERVICE_client_continue(c->client);
795 return; 794 return;
796 } 795 }
797 LOG (GNUNET_ERROR_TYPE_DEBUG, 796 LOG(GNUNET_ERROR_TYPE_DEBUG,
798 "Got a local ACK from %s for %s\n", 797 "Got a local ACK from %s for %s\n",
799 GSC_2s(c), 798 GSC_2s(c),
800 GCCH_2s (ch)); 799 GCCH_2s(ch));
801 GCCH_handle_local_ack (ch, 800 GCCH_handle_local_ack(ch,
802 msg->ccn); 801 msg->ccn);
803 GNUNET_SERVICE_client_continue (c->client); 802 GNUNET_SERVICE_client_continue(c->client);
804} 803}
805 804
806 805
@@ -813,24 +812,24 @@ handle_local_ack (void *cls,
813 * @return #GNUNET_YES, to keep iterating. 812 * @return #GNUNET_YES, to keep iterating.
814 */ 813 */
815static int 814static int
816get_all_peers_iterator (void *cls, 815get_all_peers_iterator(void *cls,
817 const struct GNUNET_PeerIdentity *peer, 816 const struct GNUNET_PeerIdentity *peer,
818 void *value) 817 void *value)
819{ 818{
820 struct CadetClient *c = cls; 819 struct CadetClient *c = cls;
821 struct CadetPeer *p = value; 820 struct CadetPeer *p = value;
822 struct GNUNET_MQ_Envelope *env; 821 struct GNUNET_MQ_Envelope *env;
823 struct GNUNET_CADET_LocalInfoPeers *msg; 822 struct GNUNET_CADET_LocalInfoPeers *msg;
824 823
825 env = GNUNET_MQ_msg (msg, 824 env = GNUNET_MQ_msg(msg,
826 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS); 825 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
827 msg->destination = *peer; 826 msg->destination = *peer;
828 msg->paths = htons (GCP_count_paths (p)); 827 msg->paths = htons(GCP_count_paths(p));
829 msg->tunnel = htons (NULL != GCP_get_tunnel (p, 828 msg->tunnel = htons(NULL != GCP_get_tunnel(p,
830 GNUNET_NO)); 829 GNUNET_NO));
831 msg->best_path_length = htonl (0); // FIXME: get length of shortest known path! 830 msg->best_path_length = htonl(0); // FIXME: get length of shortest known path!
832 GNUNET_MQ_send (c->mq, 831 GNUNET_MQ_send(c->mq,
833 env); 832 env);
834 return GNUNET_YES; 833 return GNUNET_YES;
835} 834}
836 835
@@ -842,20 +841,20 @@ get_all_peers_iterator (void *cls,
842 * @param message The actual message. 841 * @param message The actual message.
843 */ 842 */
844static void 843static void
845handle_get_peers (void *cls, 844handle_get_peers(void *cls,
846 const struct GNUNET_MessageHeader *message) 845 const struct GNUNET_MessageHeader *message)
847{ 846{
848 struct CadetClient *c = cls; 847 struct CadetClient *c = cls;
849 struct GNUNET_MQ_Envelope *env; 848 struct GNUNET_MQ_Envelope *env;
850 struct GNUNET_MessageHeader *reply; 849 struct GNUNET_MessageHeader *reply;
851 850
852 GCP_iterate_all (&get_all_peers_iterator, 851 GCP_iterate_all(&get_all_peers_iterator,
853 c); 852 c);
854 env = GNUNET_MQ_msg (reply, 853 env = GNUNET_MQ_msg(reply,
855 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS_END); 854 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS_END);
856 GNUNET_MQ_send (c->mq, 855 GNUNET_MQ_send(c->mq,
857 env); 856 env);
858 GNUNET_SERVICE_client_continue (c->client); 857 GNUNET_SERVICE_client_continue(c->client);
859} 858}
860 859
861 860
@@ -870,9 +869,9 @@ handle_get_peers (void *cls,
870 * #GNUNET_NO otherwise. 869 * #GNUNET_NO otherwise.
871 */ 870 */
872static int 871static int
873path_info_iterator (void *cls, 872path_info_iterator(void *cls,
874 struct CadetPeerPath *path, 873 struct CadetPeerPath *path,
875 unsigned int off) 874 unsigned int off)
876{ 875{
877 struct GNUNET_MQ_Handle *mq = cls; 876 struct GNUNET_MQ_Handle *mq = cls;
878 struct GNUNET_MQ_Envelope *env; 877 struct GNUNET_MQ_Envelope *env;
@@ -881,36 +880,36 @@ path_info_iterator (void *cls,
881 size_t path_size; 880 size_t path_size;
882 unsigned int path_length; 881 unsigned int path_length;
883 882
884 path_length = GCPP_get_length (path); 883 path_length = GCPP_get_length(path);
885 path_size = sizeof (struct GNUNET_PeerIdentity) * path_length; 884 path_size = sizeof(struct GNUNET_PeerIdentity) * path_length;
886 if (sizeof (*resp) + path_size > UINT16_MAX) 885 if (sizeof(*resp) + path_size > UINT16_MAX)
887 { 886 {
888 /* try just giving the relevant path */ 887 /* try just giving the relevant path */
889 path_length = GNUNET_MIN ((UINT16_MAX - sizeof (*resp)) / sizeof (struct GNUNET_PeerIdentity), 888 path_length = GNUNET_MIN((UINT16_MAX - sizeof(*resp)) / sizeof(struct GNUNET_PeerIdentity),
890 off); 889 off);
891 path_size = sizeof (struct GNUNET_PeerIdentity) * path_length; 890 path_size = sizeof(struct GNUNET_PeerIdentity) * path_length;
892 } 891 }
893 if (sizeof (*resp) + path_size > UINT16_MAX) 892 if (sizeof(*resp) + path_size > UINT16_MAX)
894 { 893 {
895 LOG (GNUNET_ERROR_TYPE_WARNING, 894 LOG(GNUNET_ERROR_TYPE_WARNING,
896 "Path of %u entries is too long for info message\n", 895 "Path of %u entries is too long for info message\n",
897 path_length); 896 path_length);
898 return GNUNET_YES; 897 return GNUNET_YES;
899 } 898 }
900 env = GNUNET_MQ_msg_extra (resp, 899 env = GNUNET_MQ_msg_extra(resp,
901 path_size, 900 path_size,
902 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH); 901 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH);
903 id = (struct GNUNET_PeerIdentity *) &resp[1]; 902 id = (struct GNUNET_PeerIdentity *)&resp[1];
904 903
905 /* Don't copy first peer. First peer is always the local one. Last 904 /* Don't copy first peer. First peer is always the local one. Last
906 * peer is always the destination (leave as 0, EOL). 905 * peer is always the destination (leave as 0, EOL).
907 */ 906 */
908 for (unsigned int i = 0; i < path_length; i++) 907 for (unsigned int i = 0; i < path_length; i++)
909 id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path, 908 id[i] = *GCP_get_id(GCPP_get_peer_at_offset(path,
910 i)); 909 i));
911 resp->off = htonl (off); 910 resp->off = htonl(off);
912 GNUNET_MQ_send (mq, 911 GNUNET_MQ_send(mq,
913 env); 912 env);
914 return GNUNET_YES; 913 return GNUNET_YES;
915} 914}
916 915
@@ -922,25 +921,25 @@ path_info_iterator (void *cls,
922 * @param msg The actual message. 921 * @param msg The actual message.
923 */ 922 */
924static void 923static void
925handle_show_path (void *cls, 924handle_show_path(void *cls,
926 const struct GNUNET_CADET_RequestPathInfoMessage *msg) 925 const struct GNUNET_CADET_RequestPathInfoMessage *msg)
927{ 926{
928 struct CadetClient *c = cls; 927 struct CadetClient *c = cls;
929 struct CadetPeer *p; 928 struct CadetPeer *p;
930 struct GNUNET_MQ_Envelope *env; 929 struct GNUNET_MQ_Envelope *env;
931 struct GNUNET_MessageHeader *resp; 930 struct GNUNET_MessageHeader *resp;
932 931
933 p = GCP_get (&msg->peer, 932 p = GCP_get(&msg->peer,
934 GNUNET_NO); 933 GNUNET_NO);
935 if (NULL != p) 934 if (NULL != p)
936 GCP_iterate_indirect_paths (p, 935 GCP_iterate_indirect_paths(p,
937 &path_info_iterator, 936 &path_info_iterator,
938 c->mq); 937 c->mq);
939 env = GNUNET_MQ_msg (resp, 938 env = GNUNET_MQ_msg(resp,
940 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH_END); 939 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PATH_END);
941 GNUNET_MQ_send (c->mq, 940 GNUNET_MQ_send(c->mq,
942 env); 941 env);
943 GNUNET_SERVICE_client_continue (c->client); 942 GNUNET_SERVICE_client_continue(c->client);
944} 943}
945 944
946 945
@@ -953,9 +952,9 @@ handle_show_path (void *cls,
953 * @return #GNUNET_YES, to keep iterating. 952 * @return #GNUNET_YES, to keep iterating.
954 */ 953 */
955static int 954static int
956get_all_tunnels_iterator (void *cls, 955get_all_tunnels_iterator(void *cls,
957 const struct GNUNET_PeerIdentity *peer, 956 const struct GNUNET_PeerIdentity *peer,
958 void *value) 957 void *value)
959{ 958{
960 struct CadetClient *c = cls; 959 struct CadetClient *c = cls;
961 struct CadetPeer *p = value; 960 struct CadetPeer *p = value;
@@ -963,19 +962,19 @@ get_all_tunnels_iterator (void *cls,
963 struct GNUNET_CADET_LocalInfoTunnel *msg; 962 struct GNUNET_CADET_LocalInfoTunnel *msg;
964 struct CadetTunnel *t; 963 struct CadetTunnel *t;
965 964
966 t = GCP_get_tunnel (p, 965 t = GCP_get_tunnel(p,
967 GNUNET_NO); 966 GNUNET_NO);
968 if (NULL == t) 967 if (NULL == t)
969 return GNUNET_YES; 968 return GNUNET_YES;
970 env = GNUNET_MQ_msg (msg, 969 env = GNUNET_MQ_msg(msg,
971 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS); 970 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
972 msg->destination = *peer; 971 msg->destination = *peer;
973 msg->channels = htonl (GCT_count_channels (t)); 972 msg->channels = htonl(GCT_count_channels(t));
974 msg->connections = htonl (GCT_count_any_connections (t)); 973 msg->connections = htonl(GCT_count_any_connections(t));
975 msg->cstate = htons (0); 974 msg->cstate = htons(0);
976 msg->estate = htons ((uint16_t) GCT_get_estate (t)); 975 msg->estate = htons((uint16_t)GCT_get_estate(t));
977 GNUNET_MQ_send (c->mq, 976 GNUNET_MQ_send(c->mq,
978 env); 977 env);
979 return GNUNET_YES; 978 return GNUNET_YES;
980} 979}
981 980
@@ -987,20 +986,20 @@ get_all_tunnels_iterator (void *cls,
987 * @param message The actual message. 986 * @param message The actual message.
988 */ 987 */
989static void 988static void
990handle_info_tunnels (void *cls, 989handle_info_tunnels(void *cls,
991 const struct GNUNET_MessageHeader *message) 990 const struct GNUNET_MessageHeader *message)
992{ 991{
993 struct CadetClient *c = cls; 992 struct CadetClient *c = cls;
994 struct GNUNET_MQ_Envelope *env; 993 struct GNUNET_MQ_Envelope *env;
995 struct GNUNET_MessageHeader *reply; 994 struct GNUNET_MessageHeader *reply;
996 995
997 GCP_iterate_all (&get_all_tunnels_iterator, 996 GCP_iterate_all(&get_all_tunnels_iterator,
998 c); 997 c);
999 env = GNUNET_MQ_msg (reply, 998 env = GNUNET_MQ_msg(reply,
1000 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS_END); 999 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS_END);
1001 GNUNET_MQ_send (c->mq, 1000 GNUNET_MQ_send(c->mq,
1002 env); 1001 env);
1003 GNUNET_SERVICE_client_continue (c->client); 1002 GNUNET_SERVICE_client_continue(c->client);
1004} 1003}
1005 1004
1006 1005
@@ -1013,28 +1012,28 @@ handle_info_tunnels (void *cls,
1013 * @return @a c 1012 * @return @a c
1014 */ 1013 */
1015static void * 1014static void *
1016client_connect_cb (void *cls, 1015client_connect_cb(void *cls,
1017 struct GNUNET_SERVICE_Client *client, 1016 struct GNUNET_SERVICE_Client *client,
1018 struct GNUNET_MQ_Handle *mq) 1017 struct GNUNET_MQ_Handle *mq)
1019{ 1018{
1020 struct CadetClient *c; 1019 struct CadetClient *c;
1021 1020
1022 c = GNUNET_new (struct CadetClient); 1021 c = GNUNET_new(struct CadetClient);
1023 c->client = client; 1022 c->client = client;
1024 c->mq = mq; 1023 c->mq = mq;
1025 c->id = next_client_id++; /* overflow not important: just for debug */ 1024 c->id = next_client_id++; /* overflow not important: just for debug */
1026 c->channels 1025 c->channels
1027 = GNUNET_CONTAINER_multihashmap32_create (32); 1026 = GNUNET_CONTAINER_multihashmap32_create(32);
1028 GNUNET_CONTAINER_DLL_insert (clients_head, 1027 GNUNET_CONTAINER_DLL_insert(clients_head,
1029 clients_tail, 1028 clients_tail,
1030 c); 1029 c);
1031 GNUNET_STATISTICS_update (stats, 1030 GNUNET_STATISTICS_update(stats,
1032 "# clients", 1031 "# clients",
1033 +1, 1032 +1,
1034 GNUNET_NO); 1033 GNUNET_NO);
1035 LOG (GNUNET_ERROR_TYPE_DEBUG, 1034 LOG(GNUNET_ERROR_TYPE_DEBUG,
1036 "%s connected\n", 1035 "%s connected\n",
1037 GSC_2s (c)); 1036 GSC_2s(c));
1038 return c; 1037 return c;
1039} 1038}
1040 1039
@@ -1047,22 +1046,22 @@ client_connect_cb (void *cls,
1047 * @param ch the channel object 1046 * @param ch the channel object
1048 */ 1047 */
1049void 1048void
1050GSC_handle_remote_channel_destroy (struct CadetClient *c, 1049GSC_handle_remote_channel_destroy(struct CadetClient *c,
1051 struct GNUNET_CADET_ClientChannelNumber ccn, 1050 struct GNUNET_CADET_ClientChannelNumber ccn,
1052 struct CadetChannel *ch) 1051 struct CadetChannel *ch)
1053{ 1052{
1054 struct GNUNET_MQ_Envelope *env; 1053 struct GNUNET_MQ_Envelope *env;
1055 struct GNUNET_CADET_LocalChannelDestroyMessage *tdm; 1054 struct GNUNET_CADET_LocalChannelDestroyMessage *tdm;
1056 1055
1057 env = GNUNET_MQ_msg (tdm, 1056 env = GNUNET_MQ_msg(tdm,
1058 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY); 1057 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY);
1059 tdm->ccn = ccn; 1058 tdm->ccn = ccn;
1060 GSC_send_to_client (c, 1059 GSC_send_to_client(c,
1061 env); 1060 env);
1062 GNUNET_assert (GNUNET_YES == 1061 GNUNET_assert(GNUNET_YES ==
1063 GNUNET_CONTAINER_multihashmap32_remove (c->channels, 1062 GNUNET_CONTAINER_multihashmap32_remove(c->channels,
1064 ntohl (ccn.channel_of_client), 1063 ntohl(ccn.channel_of_client),
1065 ch)); 1064 ch));
1066} 1065}
1067 1066
1068 1067
@@ -1074,13 +1073,13 @@ GSC_handle_remote_channel_destroy (struct CadetClient *c,
1074 * @param ch the channel that was lost 1073 * @param ch the channel that was lost
1075 */ 1074 */
1076void 1075void
1077GSC_drop_loose_channel (const struct GNUNET_HashCode *h_port, 1076GSC_drop_loose_channel(const struct GNUNET_HashCode *h_port,
1078 struct CadetChannel *ch) 1077 struct CadetChannel *ch)
1079{ 1078{
1080 GNUNET_assert (GNUNET_YES == 1079 GNUNET_assert(GNUNET_YES ==
1081 GNUNET_CONTAINER_multihashmap_remove (loose_channels, 1080 GNUNET_CONTAINER_multihashmap_remove(loose_channels,
1082 h_port, 1081 h_port,
1083 ch)); 1082 ch));
1084} 1083}
1085 1084
1086 1085
@@ -1093,26 +1092,26 @@ GSC_drop_loose_channel (const struct GNUNET_HashCode *h_port,
1093 * @return #GNUNET_OK, keep iterating. 1092 * @return #GNUNET_OK, keep iterating.
1094 */ 1093 */
1095static int 1094static int
1096channel_destroy_iterator (void *cls, 1095channel_destroy_iterator(void *cls,
1097 uint32_t key, 1096 uint32_t key,
1098 void *value) 1097 void *value)
1099{ 1098{
1100 struct CadetClient *c = cls; 1099 struct CadetClient *c = cls;
1101 struct GNUNET_CADET_ClientChannelNumber ccn; 1100 struct GNUNET_CADET_ClientChannelNumber ccn;
1102 struct CadetChannel *ch = value; 1101 struct CadetChannel *ch = value;
1103 1102
1104 LOG (GNUNET_ERROR_TYPE_DEBUG, 1103 LOG(GNUNET_ERROR_TYPE_DEBUG,
1105 "Destroying %s, due to %s disconnecting.\n", 1104 "Destroying %s, due to %s disconnecting.\n",
1106 GCCH_2s (ch), 1105 GCCH_2s(ch),
1107 GSC_2s (c)); 1106 GSC_2s(c));
1108 ccn.channel_of_client = htonl (key); 1107 ccn.channel_of_client = htonl(key);
1109 GCCH_channel_local_destroy (ch, 1108 GCCH_channel_local_destroy(ch,
1110 c, 1109 c,
1111 ccn); 1110 ccn);
1112 GNUNET_assert (GNUNET_YES == 1111 GNUNET_assert(GNUNET_YES ==
1113 GNUNET_CONTAINER_multihashmap32_remove (c->channels, 1112 GNUNET_CONTAINER_multihashmap32_remove(c->channels,
1114 key, 1113 key,
1115 ch)); 1114 ch));
1116 return GNUNET_OK; 1115 return GNUNET_OK;
1117} 1116}
1118 1117
@@ -1126,27 +1125,27 @@ channel_destroy_iterator (void *cls,
1126 * @return #GNUNET_OK, keep iterating. 1125 * @return #GNUNET_OK, keep iterating.
1127 */ 1126 */
1128static int 1127static int
1129client_release_ports (void *cls, 1128client_release_ports(void *cls,
1130 const struct GNUNET_HashCode *port, 1129 const struct GNUNET_HashCode *port,
1131 void *value) 1130 void *value)
1132{ 1131{
1133 struct CadetClient *c = cls; 1132 struct CadetClient *c = cls;
1134 struct OpenPort *op = value; 1133 struct OpenPort *op = value;
1135 1134
1136 GNUNET_assert (c == op->c); 1135 GNUNET_assert(c == op->c);
1137 LOG (GNUNET_ERROR_TYPE_DEBUG, 1136 LOG(GNUNET_ERROR_TYPE_DEBUG,
1138 "Closing port %s due to %s disconnect.\n", 1137 "Closing port %s due to %s disconnect.\n",
1139 GNUNET_h2s (port), 1138 GNUNET_h2s(port),
1140 GSC_2s (c)); 1139 GSC_2s(c));
1141 GNUNET_assert (GNUNET_YES == 1140 GNUNET_assert(GNUNET_YES ==
1142 GNUNET_CONTAINER_multihashmap_remove (open_ports, 1141 GNUNET_CONTAINER_multihashmap_remove(open_ports,
1143 &op->h_port, 1142 &op->h_port,
1144 op)); 1143 op));
1145 GNUNET_assert (GNUNET_YES == 1144 GNUNET_assert(GNUNET_YES ==
1146 GNUNET_CONTAINER_multihashmap_remove (c->ports, 1145 GNUNET_CONTAINER_multihashmap_remove(c->ports,
1147 port, 1146 port,
1148 op)); 1147 op));
1149 GNUNET_free (op); 1148 GNUNET_free(op);
1150 return GNUNET_OK; 1149 return GNUNET_OK;
1151} 1150}
1152 1151
@@ -1159,42 +1158,42 @@ client_release_ports (void *cls,
1159 * @param internal_cls should be equal to @a c 1158 * @param internal_cls should be equal to @a c
1160 */ 1159 */
1161static void 1160static void
1162client_disconnect_cb (void *cls, 1161client_disconnect_cb(void *cls,
1163 struct GNUNET_SERVICE_Client *client, 1162 struct GNUNET_SERVICE_Client *client,
1164 void *internal_cls) 1163 void *internal_cls)
1165{ 1164{
1166 struct CadetClient *c = internal_cls; 1165 struct CadetClient *c = internal_cls;
1167 1166
1168 GNUNET_assert (c->client == client); 1167 GNUNET_assert(c->client == client);
1169 LOG (GNUNET_ERROR_TYPE_DEBUG, 1168 LOG(GNUNET_ERROR_TYPE_DEBUG,
1170 "%s is disconnecting.\n", 1169 "%s is disconnecting.\n",
1171 GSC_2s (c)); 1170 GSC_2s(c));
1172 if (NULL != c->channels) 1171 if (NULL != c->channels)
1173 { 1172 {
1174 GNUNET_CONTAINER_multihashmap32_iterate (c->channels, 1173 GNUNET_CONTAINER_multihashmap32_iterate(c->channels,
1175 &channel_destroy_iterator, 1174 &channel_destroy_iterator,
1176 c); 1175 c);
1177 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (c->channels)); 1176 GNUNET_assert(0 == GNUNET_CONTAINER_multihashmap32_size(c->channels));
1178 GNUNET_CONTAINER_multihashmap32_destroy (c->channels); 1177 GNUNET_CONTAINER_multihashmap32_destroy(c->channels);
1179 } 1178 }
1180 if (NULL != c->ports) 1179 if (NULL != c->ports)
1181 { 1180 {
1182 GNUNET_CONTAINER_multihashmap_iterate (c->ports, 1181 GNUNET_CONTAINER_multihashmap_iterate(c->ports,
1183 &client_release_ports, 1182 &client_release_ports,
1184 c); 1183 c);
1185 GNUNET_CONTAINER_multihashmap_destroy (c->ports); 1184 GNUNET_CONTAINER_multihashmap_destroy(c->ports);
1186 } 1185 }
1187 GNUNET_CONTAINER_DLL_remove (clients_head, 1186 GNUNET_CONTAINER_DLL_remove(clients_head,
1188 clients_tail, 1187 clients_tail,
1189 c); 1188 c);
1190 GNUNET_STATISTICS_update (stats, 1189 GNUNET_STATISTICS_update(stats,
1191 "# clients", 1190 "# clients",
1192 -1, 1191 -1,
1193 GNUNET_NO); 1192 GNUNET_NO);
1194 GNUNET_free (c); 1193 GNUNET_free(c);
1195 if ( (NULL == clients_head) && 1194 if ((NULL == clients_head) &&
1196 (GNUNET_YES == shutting_down) ) 1195 (GNUNET_YES == shutting_down))
1197 shutdown_rest (); 1196 shutdown_rest();
1198} 1197}
1199 1198
1200 1199
@@ -1206,93 +1205,92 @@ client_disconnect_cb (void *cls,
1206 * @param c configuration to use 1205 * @param c configuration to use
1207 */ 1206 */
1208static void 1207static void
1209run (void *cls, 1208run(void *cls,
1210 const struct GNUNET_CONFIGURATION_Handle *c, 1209 const struct GNUNET_CONFIGURATION_Handle *c,
1211 struct GNUNET_SERVICE_Handle *service) 1210 struct GNUNET_SERVICE_Handle *service)
1212{ 1211{
1213 cfg = c; 1212 cfg = c;
1214 if (GNUNET_OK != 1213 if (GNUNET_OK !=
1215 GNUNET_CONFIGURATION_get_value_number (c, 1214 GNUNET_CONFIGURATION_get_value_number(c,
1216 "CADET", 1215 "CADET",
1217 "RATCHET_MESSAGES", 1216 "RATCHET_MESSAGES",
1218 &ratchet_messages)) 1217 &ratchet_messages))
1219 { 1218 {
1220 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING, 1219 GNUNET_log_config_invalid(GNUNET_ERROR_TYPE_WARNING,
1221 "CADET", 1220 "CADET",
1222 "RATCHET_MESSAGES", 1221 "RATCHET_MESSAGES",
1223 "needs to be a number"); 1222 "needs to be a number");
1224 ratchet_messages = 64; 1223 ratchet_messages = 64;
1225 } 1224 }
1226 if (GNUNET_OK != 1225 if (GNUNET_OK !=
1227 GNUNET_CONFIGURATION_get_value_time (c, 1226 GNUNET_CONFIGURATION_get_value_time(c,
1228 "CADET", 1227 "CADET",
1229 "RATCHET_TIME", 1228 "RATCHET_TIME",
1230 &ratchet_time)) 1229 &ratchet_time))
1231 { 1230 {
1232 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING, 1231 GNUNET_log_config_invalid(GNUNET_ERROR_TYPE_WARNING,
1233 "CADET", 1232 "CADET",
1234 "RATCHET_TIME", 1233 "RATCHET_TIME",
1235 "need delay value"); 1234 "need delay value");
1236 ratchet_time = GNUNET_TIME_UNIT_HOURS; 1235 ratchet_time = GNUNET_TIME_UNIT_HOURS;
1237 } 1236 }
1238 if (GNUNET_OK != 1237 if (GNUNET_OK !=
1239 GNUNET_CONFIGURATION_get_value_time (c, 1238 GNUNET_CONFIGURATION_get_value_time(c,
1240 "CADET", 1239 "CADET",
1241 "REFRESH_CONNECTION_TIME", 1240 "REFRESH_CONNECTION_TIME",
1242 &keepalive_period)) 1241 &keepalive_period))
1243 { 1242 {
1244 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING, 1243 GNUNET_log_config_invalid(GNUNET_ERROR_TYPE_WARNING,
1245 "CADET", 1244 "CADET",
1246 "REFRESH_CONNECTION_TIME", 1245 "REFRESH_CONNECTION_TIME",
1247 "need delay value"); 1246 "need delay value");
1248 keepalive_period = GNUNET_TIME_UNIT_MINUTES; 1247 keepalive_period = GNUNET_TIME_UNIT_MINUTES;
1249 } 1248 }
1250 if (GNUNET_OK != 1249 if (GNUNET_OK !=
1251 GNUNET_CONFIGURATION_get_value_number (c, 1250 GNUNET_CONFIGURATION_get_value_number(c,
1252 "CADET", 1251 "CADET",
1253 "DROP_PERCENT", 1252 "DROP_PERCENT",
1254 &drop_percent)) 1253 &drop_percent))
1255 { 1254 {
1256 drop_percent = 0; 1255 drop_percent = 0;
1257 } 1256 }
1258 else 1257 else
1259 { 1258 {
1260 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n"); 1259 LOG(GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1261 LOG (GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n"); 1260 LOG(GNUNET_ERROR_TYPE_WARNING, "Cadet is running with DROP enabled.\n");
1262 LOG (GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n"); 1261 LOG(GNUNET_ERROR_TYPE_WARNING, "This is NOT a good idea!\n");
1263 LOG (GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n"); 1262 LOG(GNUNET_ERROR_TYPE_WARNING, "Remove DROP_PERCENT from config file.\n");
1264 LOG (GNUNET_ERROR_TYPE_WARNING, "**************************************\n"); 1263 LOG(GNUNET_ERROR_TYPE_WARNING, "**************************************\n");
1265 } 1264 }
1266 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c); 1265 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration(c);
1267 if (NULL == my_private_key) 1266 if (NULL == my_private_key)
1268 { 1267 {
1269 GNUNET_break (0); 1268 GNUNET_break(0);
1270 GNUNET_SCHEDULER_shutdown (); 1269 GNUNET_SCHEDULER_shutdown();
1271 return; 1270 return;
1272 } 1271 }
1273 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, 1272 GNUNET_CRYPTO_eddsa_key_get_public(my_private_key,
1274 &my_full_id.public_key); 1273 &my_full_id.public_key);
1275 stats = GNUNET_STATISTICS_create ("cadet", 1274 stats = GNUNET_STATISTICS_create("cadet",
1276 c); 1275 c);
1277 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, 1276 GNUNET_SCHEDULER_add_shutdown(&shutdown_task,
1278 NULL); 1277 NULL);
1279 ats_ch = GNUNET_ATS_connectivity_init (c); 1278 ats_ch = GNUNET_ATS_connectivity_init(c);
1280 /* FIXME: optimize code to allow GNUNET_YES here! */ 1279 /* FIXME: optimize code to allow GNUNET_YES here! */
1281 open_ports = GNUNET_CONTAINER_multihashmap_create (16, 1280 open_ports = GNUNET_CONTAINER_multihashmap_create(16,
1282 GNUNET_NO); 1281 GNUNET_NO);
1283 loose_channels = GNUNET_CONTAINER_multihashmap_create (16, 1282 loose_channels = GNUNET_CONTAINER_multihashmap_create(16,
1284 GNUNET_NO); 1283 GNUNET_NO);
1285 peers = GNUNET_CONTAINER_multipeermap_create (16, 1284 peers = GNUNET_CONTAINER_multipeermap_create(16,
1286 GNUNET_YES); 1285 GNUNET_YES);
1287 connections = GNUNET_CONTAINER_multishortmap_create (256, 1286 connections = GNUNET_CONTAINER_multishortmap_create(256,
1288 GNUNET_YES); 1287 GNUNET_YES);
1289 GCH_init (c); 1288 GCH_init(c);
1290 GCD_init (c); 1289 GCD_init(c);
1291 GCO_init (c); 1290 GCO_init(c);
1292 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1291 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
1293 "CADET started for peer %s\n", 1292 "CADET started for peer %s\n",
1294 GNUNET_i2s (&my_full_id)); 1293 GNUNET_i2s(&my_full_id));
1295
1296} 1294}
1297 1295
1298 1296
@@ -1300,48 +1298,48 @@ run (void *cls,
1300 * Define "main" method using service macro. 1298 * Define "main" method using service macro.
1301 */ 1299 */
1302GNUNET_SERVICE_MAIN 1300GNUNET_SERVICE_MAIN
1303("cadet", 1301 ("cadet",
1304 GNUNET_SERVICE_OPTION_NONE, 1302 GNUNET_SERVICE_OPTION_NONE,
1305 &run, 1303 &run,
1306 &client_connect_cb, 1304 &client_connect_cb,
1307 &client_disconnect_cb, 1305 &client_disconnect_cb,
1308 NULL, 1306 NULL,
1309 GNUNET_MQ_hd_fixed_size (port_open, 1307 GNUNET_MQ_hd_fixed_size(port_open,
1310 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN, 1308 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1311 struct GNUNET_CADET_PortMessage, 1309 struct GNUNET_CADET_PortMessage,
1312 NULL), 1310 NULL),
1313 GNUNET_MQ_hd_fixed_size (port_close, 1311 GNUNET_MQ_hd_fixed_size(port_close,
1314 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE, 1312 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1315 struct GNUNET_CADET_PortMessage, 1313 struct GNUNET_CADET_PortMessage,
1316 NULL), 1314 NULL),
1317 GNUNET_MQ_hd_fixed_size (channel_create, 1315 GNUNET_MQ_hd_fixed_size(channel_create,
1318 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE, 1316 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_CREATE,
1319 struct GNUNET_CADET_LocalChannelCreateMessage, 1317 struct GNUNET_CADET_LocalChannelCreateMessage,
1320 NULL), 1318 NULL),
1321 GNUNET_MQ_hd_fixed_size (channel_destroy, 1319 GNUNET_MQ_hd_fixed_size(channel_destroy,
1322 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY, 1320 GNUNET_MESSAGE_TYPE_CADET_LOCAL_CHANNEL_DESTROY,
1323 struct GNUNET_CADET_LocalChannelDestroyMessage, 1321 struct GNUNET_CADET_LocalChannelDestroyMessage,
1324 NULL), 1322 NULL),
1325 GNUNET_MQ_hd_var_size (local_data, 1323 GNUNET_MQ_hd_var_size(local_data,
1326 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 1324 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1327 struct GNUNET_CADET_LocalData, 1325 struct GNUNET_CADET_LocalData,
1328 NULL), 1326 NULL),
1329 GNUNET_MQ_hd_fixed_size (local_ack, 1327 GNUNET_MQ_hd_fixed_size(local_ack,
1330 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK, 1328 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1331 struct GNUNET_CADET_LocalAck, 1329 struct GNUNET_CADET_LocalAck,
1332 NULL), 1330 NULL),
1333 GNUNET_MQ_hd_fixed_size (get_peers, 1331 GNUNET_MQ_hd_fixed_size(get_peers,
1334 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PEERS, 1332 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PEERS,
1335 struct GNUNET_MessageHeader, 1333 struct GNUNET_MessageHeader,
1336 NULL), 1334 NULL),
1337 GNUNET_MQ_hd_fixed_size (show_path, 1335 GNUNET_MQ_hd_fixed_size(show_path,
1338 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PATH, 1336 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_PATH,
1339 struct GNUNET_CADET_RequestPathInfoMessage, 1337 struct GNUNET_CADET_RequestPathInfoMessage,
1340 NULL), 1338 NULL),
1341 GNUNET_MQ_hd_fixed_size (info_tunnels, 1339 GNUNET_MQ_hd_fixed_size(info_tunnels,
1342 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_TUNNELS, 1340 GNUNET_MESSAGE_TYPE_CADET_LOCAL_REQUEST_INFO_TUNNELS,
1343 struct GNUNET_MessageHeader, 1341 struct GNUNET_MessageHeader,
1344 NULL), 1342 NULL),
1345 GNUNET_MQ_handler_end ()); 1343 GNUNET_MQ_handler_end());
1346 1344
1347/* end of gnunet-service-cadet-new.c */ 1345/* end of gnunet-service-cadet-new.c */
diff --git a/src/cadet/gnunet-service-cadet.h b/src/cadet/gnunet-service-cadet.h
index 3daeeff74..8decdba0d 100644
--- a/src/cadet/gnunet-service-cadet.h
+++ b/src/cadet/gnunet-service-cadet.h
@@ -12,12 +12,12 @@
12 WITHOUT ANY WARRANTY; without even the implied warranty of 12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Affero General Public License for more details. 14 Affero General Public License for more details.
15 15
16 You should have received a copy of the GNU Affero General Public License 16 You should have received a copy of the GNU Affero General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18
19 SPDX-License-Identifier: AGPL3.0-or-later 19 SPDX-License-Identifier: AGPL3.0-or-later
20*/ 20 */
21 21
22/** 22/**
23 * @file cadet/gnunet-service-cadet.h 23 * @file cadet/gnunet-service-cadet.h
@@ -64,8 +64,7 @@ struct CadetPeerPath;
64/** 64/**
65 * Entry in a peer path. 65 * Entry in a peer path.
66 */ 66 */
67struct CadetPeerPathEntry 67struct CadetPeerPathEntry {
68{
69 /** 68 /**
70 * DLL of paths where the same @e peer is at the same offset. 69 * DLL of paths where the same @e peer is at the same offset.
71 */ 70 */
@@ -99,14 +98,12 @@ struct CadetPeerPathEntry
99 * against overflows. 98 * against overflows.
100 */ 99 */
101 int score; 100 int score;
102
103}; 101};
104 102
105/** 103/**
106 * Entry in list of connections used by tunnel, with metadata. 104 * Entry in list of connections used by tunnel, with metadata.
107 */ 105 */
108struct CadetTConnection 106struct CadetTConnection {
109{
110 /** 107 /**
111 * Next in DLL. 108 * Next in DLL.
112 */ 109 */
@@ -147,9 +144,7 @@ struct CadetTConnection
147/** 144/**
148 * Port opened by a client. 145 * Port opened by a client.
149 */ 146 */
150struct OpenPort 147struct OpenPort {
151{
152
153 /** 148 /**
154 * Client that opened the port. 149 * Client that opened the port.
155 */ 150 */
@@ -164,7 +159,6 @@ struct OpenPort
164 * Port hashed with our PID (matches incoming OPEN messages). 159 * Port hashed with our PID (matches incoming OPEN messages).
165 */ 160 */
166 struct GNUNET_HashCode h_port; 161 struct GNUNET_HashCode h_port;
167
168}; 162};
169 163
170 164
@@ -272,8 +266,8 @@ extern unsigned long long drop_percent;
272 * @param env envelope with the message 266 * @param env envelope with the message
273 */ 267 */
274void 268void
275GSC_send_to_client (struct CadetClient *c, 269GSC_send_to_client(struct CadetClient *c,
276 struct GNUNET_MQ_Envelope *env); 270 struct GNUNET_MQ_Envelope *env);
277 271
278 272
279/** 273/**
@@ -284,9 +278,9 @@ GSC_send_to_client (struct CadetClient *c,
284 * @param ch the channel object 278 * @param ch the channel object
285 */ 279 */
286void 280void
287GSC_handle_remote_channel_destroy (struct CadetClient *c, 281GSC_handle_remote_channel_destroy(struct CadetClient *c,
288 struct GNUNET_CADET_ClientChannelNumber ccn, 282 struct GNUNET_CADET_ClientChannelNumber ccn,
289 struct CadetChannel *ch); 283 struct CadetChannel *ch);
290 284
291/** 285/**
292 * A client that created a loose channel that was not bound to a port 286 * A client that created a loose channel that was not bound to a port
@@ -296,8 +290,8 @@ GSC_handle_remote_channel_destroy (struct CadetClient *c,
296 * @param ch the channel that was lost 290 * @param ch the channel that was lost
297 */ 291 */
298void 292void
299GSC_drop_loose_channel (const struct GNUNET_HashCode *h_port, 293GSC_drop_loose_channel(const struct GNUNET_HashCode *h_port,
300 struct CadetChannel *ch); 294 struct CadetChannel *ch);
301 295
302 296
303/** 297/**
@@ -312,11 +306,11 @@ GSC_drop_loose_channel (const struct GNUNET_HashCode *h_port,
312 * @return local channel number assigned to the new client 306 * @return local channel number assigned to the new client
313 */ 307 */
314struct GNUNET_CADET_ClientChannelNumber 308struct GNUNET_CADET_ClientChannelNumber
315GSC_bind (struct CadetClient *c, 309GSC_bind(struct CadetClient *c,
316 struct CadetChannel *ch, 310 struct CadetChannel *ch,
317 struct CadetPeer *dest, 311 struct CadetPeer *dest,
318 const struct GNUNET_HashCode *port, 312 const struct GNUNET_HashCode *port,
319 uint32_t options); 313 uint32_t options);
320 314
321 315
322/** 316/**
@@ -326,7 +320,7 @@ GSC_bind (struct CadetClient *c,
326 * @return string for debugging 320 * @return string for debugging
327 */ 321 */
328const char * 322const char *
329GSC_2s (struct CadetClient *c); 323GSC_2s(struct CadetClient *c);
330 324
331 325
332#endif 326#endif
diff --git a/src/cadet/gnunet-service-cadet_channel.c b/src/cadet/gnunet-service-cadet_channel.c
index 664b8a7c1..dad5325fc 100644
--- a/src/cadet/gnunet-service-cadet_channel.c
+++ b/src/cadet/gnunet-service-cadet_channel.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file cadet/gnunet-service-cadet_channel.c 21 * @file cadet/gnunet-service-cadet_channel.c
22 * @brief logical links between CADET clients 22 * @brief logical links between CADET clients
@@ -41,26 +41,26 @@
41#include "gnunet-service-cadet_tunnels.h" 41#include "gnunet-service-cadet_tunnels.h"
42#include "gnunet-service-cadet_paths.h" 42#include "gnunet-service-cadet_paths.h"
43 43
44#define LOG(level, ...) GNUNET_log_from (level, "cadet-chn", __VA_ARGS__) 44#define LOG(level, ...) GNUNET_log_from(level, "cadet-chn", __VA_ARGS__)
45 45
46/** 46/**
47 * How long do we initially wait before retransmitting? 47 * How long do we initially wait before retransmitting?
48 */ 48 */
49#define CADET_INITIAL_RETRANSMIT_TIME \ 49#define CADET_INITIAL_RETRANSMIT_TIME \
50 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250) 50 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250)
51 51
52/** 52/**
53 * How long do we wait before dropping state about incoming 53 * How long do we wait before dropping state about incoming
54 * connection to closed port? 54 * connection to closed port?
55 */ 55 */
56#define TIMEOUT_CLOSED_PORT \ 56#define TIMEOUT_CLOSED_PORT \
57 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30) 57 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
58 58
59/** 59/**
60 * How long do we wait at least before retransmitting ever? 60 * How long do we wait at least before retransmitting ever?
61 */ 61 */
62#define MIN_RTT_DELAY \ 62#define MIN_RTT_DELAY \
63 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 75) 63 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 75)
64 64
65/** 65/**
66 * Maximum message ID into the future we accept for out-of-order messages. 66 * Maximum message ID into the future we accept for out-of-order messages.
@@ -78,8 +78,7 @@
78/** 78/**
79 * All the states a channel can be in. 79 * All the states a channel can be in.
80 */ 80 */
81enum CadetChannelState 81enum CadetChannelState {
82{
83 /** 82 /**
84 * Uninitialized status, should never appear in operation. 83 * Uninitialized status, should never appear in operation.
85 */ 84 */
@@ -108,8 +107,7 @@ enum CadetChannelState
108 * Note that we DO use this structure also for unreliable 107 * Note that we DO use this structure also for unreliable
109 * messages. 108 * messages.
110 */ 109 */
111struct CadetReliableMessage 110struct CadetReliableMessage {
112{
113 /** 111 /**
114 * Double linked list, FIFO style 112 * Double linked list, FIFO style
115 */ 113 */
@@ -173,8 +171,7 @@ struct CadetReliableMessage
173/** 171/**
174 * List of received out-of-order data messages. 172 * List of received out-of-order data messages.
175 */ 173 */
176struct CadetOutOfOrderMessage 174struct CadetOutOfOrderMessage {
177{
178 /** 175 /**
179 * Double linked list, FIFO style 176 * Double linked list, FIFO style
180 */ 177 */
@@ -203,8 +200,7 @@ struct CadetOutOfOrderMessage
203 * loopback channel, in which case it has two of these endpoints. 200 * loopback channel, in which case it has two of these endpoints.
204 * Note that flow control also is required in both directions. 201 * Note that flow control also is required in both directions.
205 */ 202 */
206struct CadetChannelClient 203struct CadetChannelClient {
207{
208 /** 204 /**
209 * Client handle. Not by itself sufficient to designate 205 * Client handle. Not by itself sufficient to designate
210 * the client endpoint, as the same client handle may 206 * the client endpoint, as the same client handle may
@@ -245,8 +241,7 @@ struct CadetChannelClient
245/** 241/**
246 * Struct containing all information regarding a channel to a remote client. 242 * Struct containing all information regarding a channel to a remote client.
247 */ 243 */
248struct CadetChannel 244struct CadetChannel {
249{
250 /** 245 /**
251 * Tunnel this channel is in. 246 * Tunnel this channel is in.
252 */ 247 */
@@ -391,24 +386,24 @@ struct CadetChannel
391 * @return Static string with the channel IDs. 386 * @return Static string with the channel IDs.
392 */ 387 */
393const char * 388const char *
394GCCH_2s (const struct CadetChannel *ch) 389GCCH_2s(const struct CadetChannel *ch)
395{ 390{
396 static char buf[128]; 391 static char buf[128];
397 392
398 GNUNET_snprintf (buf, 393 GNUNET_snprintf(buf,
399 sizeof (buf), 394 sizeof(buf),
400 "Channel %s:%s ctn:%X(%X/%X)", 395 "Channel %s:%s ctn:%X(%X/%X)",
401 (GNUNET_YES == ch->is_loopback) 396 (GNUNET_YES == ch->is_loopback)
402 ? "loopback" 397 ? "loopback"
403 : GNUNET_i2s (GCP_get_id (GCT_get_destination (ch->t))), 398 : GNUNET_i2s(GCP_get_id(GCT_get_destination(ch->t))),
404 GNUNET_h2s (&ch->port), 399 GNUNET_h2s(&ch->port),
405 ch->ctn, 400 ch->ctn,
406 (NULL == ch->owner) 401 (NULL == ch->owner)
407 ? 0 402 ? 0
408 : ntohl (ch->owner->ccn.channel_of_client), 403 : ntohl(ch->owner->ccn.channel_of_client),
409 (NULL == ch->dest) 404 (NULL == ch->dest)
410 ? 0 405 ? 0
411 : ntohl (ch->dest->ccn.channel_of_client)); 406 : ntohl(ch->dest->ccn.channel_of_client));
412 return buf; 407 return buf;
413} 408}
414 409
@@ -423,19 +418,19 @@ GCCH_2s (const struct CadetChannel *ch)
423 * @param listener peer that is listining on @a port 418 * @param listener peer that is listining on @a port
424 */ 419 */
425void 420void
426GCCH_hash_port (struct GNUNET_HashCode *h_port, 421GCCH_hash_port(struct GNUNET_HashCode *h_port,
427 const struct GNUNET_HashCode *port, 422 const struct GNUNET_HashCode *port,
428 const struct GNUNET_PeerIdentity *listener) 423 const struct GNUNET_PeerIdentity *listener)
429{ 424{
430 struct GNUNET_HashContext *hc; 425 struct GNUNET_HashContext *hc;
431 426
432 hc = GNUNET_CRYPTO_hash_context_start (); 427 hc = GNUNET_CRYPTO_hash_context_start();
433 GNUNET_CRYPTO_hash_context_read (hc, port, sizeof (*port)); 428 GNUNET_CRYPTO_hash_context_read(hc, port, sizeof(*port));
434 GNUNET_CRYPTO_hash_context_read (hc, listener, sizeof (*listener)); 429 GNUNET_CRYPTO_hash_context_read(hc, listener, sizeof(*listener));
435 GNUNET_CRYPTO_hash_context_finish (hc, h_port); 430 GNUNET_CRYPTO_hash_context_finish(hc, h_port);
436 LOG (GNUNET_ERROR_TYPE_DEBUG, 431 LOG(GNUNET_ERROR_TYPE_DEBUG,
437 "Calculated port hash %s\n", 432 "Calculated port hash %s\n",
438 GNUNET_h2s (h_port)); 433 GNUNET_h2s(h_port));
439} 434}
440 435
441 436
@@ -447,7 +442,7 @@ GCCH_hash_port (struct GNUNET_HashCode *h_port,
447 * @return ID used to identify the channel with the remote peer. 442 * @return ID used to identify the channel with the remote peer.
448 */ 443 */
449struct GNUNET_CADET_ChannelTunnelNumber 444struct GNUNET_CADET_ChannelTunnelNumber
450GCCH_get_id (const struct CadetChannel *ch) 445GCCH_get_id(const struct CadetChannel *ch)
451{ 446{
452 return ch->ctn; 447 return ch->ctn;
453} 448}
@@ -459,18 +454,18 @@ GCCH_get_id (const struct CadetChannel *ch)
459 * @param ccc data structure to clean up 454 * @param ccc data structure to clean up
460 */ 455 */
461static void 456static void
462free_channel_client (struct CadetChannelClient *ccc) 457free_channel_client(struct CadetChannelClient *ccc)
463{ 458{
464 struct CadetOutOfOrderMessage *com; 459 struct CadetOutOfOrderMessage *com;
465 460
466 while (NULL != (com = ccc->head_recv)) 461 while (NULL != (com = ccc->head_recv))
467 { 462 {
468 GNUNET_CONTAINER_DLL_remove (ccc->head_recv, ccc->tail_recv, com); 463 GNUNET_CONTAINER_DLL_remove(ccc->head_recv, ccc->tail_recv, com);
469 ccc->num_recv--; 464 ccc->num_recv--;
470 GNUNET_MQ_discard (com->env); 465 GNUNET_MQ_discard(com->env);
471 GNUNET_free (com); 466 GNUNET_free(com);
472 } 467 }
473 GNUNET_free (ccc); 468 GNUNET_free(ccc);
474} 469}
475 470
476 471
@@ -480,57 +475,57 @@ free_channel_client (struct CadetChannelClient *ccc)
480 * @param ch channel to destroy 475 * @param ch channel to destroy
481 */ 476 */
482static void 477static void
483channel_destroy (struct CadetChannel *ch) 478channel_destroy(struct CadetChannel *ch)
484{ 479{
485 struct CadetReliableMessage *crm; 480 struct CadetReliableMessage *crm;
486 481
487 while (NULL != (crm = ch->head_sent)) 482 while (NULL != (crm = ch->head_sent))
488 {
489 GNUNET_assert (ch == crm->ch);
490 if (NULL != crm->qe)
491 { 483 {
492 GCT_send_cancel (crm->qe); 484 GNUNET_assert(ch == crm->ch);
493 crm->qe = NULL; 485 if (NULL != crm->qe)
486 {
487 GCT_send_cancel(crm->qe);
488 crm->qe = NULL;
489 }
490 GNUNET_CONTAINER_DLL_remove(ch->head_sent, ch->tail_sent, crm);
491 GNUNET_free(crm->data_message);
492 GNUNET_free(crm);
494 } 493 }
495 GNUNET_CONTAINER_DLL_remove (ch->head_sent, ch->tail_sent, crm);
496 GNUNET_free (crm->data_message);
497 GNUNET_free (crm);
498 }
499 if (CADET_CHANNEL_LOOSE == ch->state) 494 if (CADET_CHANNEL_LOOSE == ch->state)
500 { 495 {
501 GSC_drop_loose_channel (&ch->h_port, ch); 496 GSC_drop_loose_channel(&ch->h_port, ch);
502 } 497 }
503 if (NULL != ch->owner) 498 if (NULL != ch->owner)
504 { 499 {
505 free_channel_client (ch->owner); 500 free_channel_client(ch->owner);
506 ch->owner = NULL; 501 ch->owner = NULL;
507 } 502 }
508 if (NULL != ch->dest) 503 if (NULL != ch->dest)
509 { 504 {
510 free_channel_client (ch->dest); 505 free_channel_client(ch->dest);
511 ch->dest = NULL; 506 ch->dest = NULL;
512 } 507 }
513 if (NULL != ch->last_control_qe) 508 if (NULL != ch->last_control_qe)
514 { 509 {
515 GCT_send_cancel (ch->last_control_qe); 510 GCT_send_cancel(ch->last_control_qe);
516 ch->last_control_qe = NULL; 511 ch->last_control_qe = NULL;
517 } 512 }
518 if (NULL != ch->retry_data_task) 513 if (NULL != ch->retry_data_task)
519 { 514 {
520 GNUNET_SCHEDULER_cancel (ch->retry_data_task); 515 GNUNET_SCHEDULER_cancel(ch->retry_data_task);
521 ch->retry_data_task = NULL; 516 ch->retry_data_task = NULL;
522 } 517 }
523 if (NULL != ch->retry_control_task) 518 if (NULL != ch->retry_control_task)
524 { 519 {
525 GNUNET_SCHEDULER_cancel (ch->retry_control_task); 520 GNUNET_SCHEDULER_cancel(ch->retry_control_task);
526 ch->retry_control_task = NULL; 521 ch->retry_control_task = NULL;
527 } 522 }
528 if (GNUNET_NO == ch->is_loopback) 523 if (GNUNET_NO == ch->is_loopback)
529 { 524 {
530 GCT_remove_channel (ch->t, ch, ch->ctn); 525 GCT_remove_channel(ch->t, ch, ch->ctn);
531 ch->t = NULL; 526 ch->t = NULL;
532 } 527 }
533 GNUNET_free (ch); 528 GNUNET_free(ch);
534} 529}
535 530
536 531
@@ -540,7 +535,7 @@ channel_destroy (struct CadetChannel *ch)
540 * @param cls Channel for which to send. 535 * @param cls Channel for which to send.
541 */ 536 */
542static void 537static void
543send_channel_open (void *cls); 538send_channel_open(void *cls);
544 539
545 540
546/** 541/**
@@ -552,20 +547,20 @@ send_channel_open (void *cls);
552 * if transmission failed 547 * if transmission failed
553 */ 548 */
554static void 549static void
555channel_open_sent_cb (void *cls, 550channel_open_sent_cb(void *cls,
556 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid) 551 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
557{ 552{
558 struct CadetChannel *ch = cls; 553 struct CadetChannel *ch = cls;
559 554
560 GNUNET_assert (NULL != ch->last_control_qe); 555 GNUNET_assert(NULL != ch->last_control_qe);
561 ch->last_control_qe = NULL; 556 ch->last_control_qe = NULL;
562 ch->retry_time = GNUNET_TIME_STD_BACKOFF (ch->retry_time); 557 ch->retry_time = GNUNET_TIME_STD_BACKOFF(ch->retry_time);
563 LOG (GNUNET_ERROR_TYPE_DEBUG, 558 LOG(GNUNET_ERROR_TYPE_DEBUG,
564 "Sent CADET_CHANNEL_OPEN on %s, retrying in %s\n", 559 "Sent CADET_CHANNEL_OPEN on %s, retrying in %s\n",
565 GCCH_2s (ch), 560 GCCH_2s(ch),
566 GNUNET_STRINGS_relative_time_to_string (ch->retry_time, GNUNET_YES)); 561 GNUNET_STRINGS_relative_time_to_string(ch->retry_time, GNUNET_YES));
567 ch->retry_control_task = 562 ch->retry_control_task =
568 GNUNET_SCHEDULER_add_delayed (ch->retry_time, &send_channel_open, ch); 563 GNUNET_SCHEDULER_add_delayed(ch->retry_time, &send_channel_open, ch);
569} 564}
570 565
571 566
@@ -575,27 +570,27 @@ channel_open_sent_cb (void *cls,
575 * @param cls Channel for which to send. 570 * @param cls Channel for which to send.
576 */ 571 */
577static void 572static void
578send_channel_open (void *cls) 573send_channel_open(void *cls)
579{ 574{
580 struct CadetChannel *ch = cls; 575 struct CadetChannel *ch = cls;
581 struct GNUNET_CADET_ChannelOpenMessage msgcc; 576 struct GNUNET_CADET_ChannelOpenMessage msgcc;
582 577
583 ch->retry_control_task = NULL; 578 ch->retry_control_task = NULL;
584 LOG (GNUNET_ERROR_TYPE_DEBUG, 579 LOG(GNUNET_ERROR_TYPE_DEBUG,
585 "Sending CHANNEL_OPEN message for %s\n", 580 "Sending CHANNEL_OPEN message for %s\n",
586 GCCH_2s (ch)); 581 GCCH_2s(ch));
587 msgcc.header.size = htons (sizeof (msgcc)); 582 msgcc.header.size = htons(sizeof(msgcc));
588 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN); 583 msgcc.header.type = htons(GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN);
589 //TODO This will be removed in a major release, because this will be a protocol breaking change. We set the deprecated "reliable" bit here that was removed. 584 //TODO This will be removed in a major release, because this will be a protocol breaking change. We set the deprecated "reliable" bit here that was removed.
590 msgcc.opt = 2; 585 msgcc.opt = 2;
591 msgcc.h_port = ch->h_port; 586 msgcc.h_port = ch->h_port;
592 msgcc.ctn = ch->ctn; 587 msgcc.ctn = ch->ctn;
593 ch->state = CADET_CHANNEL_OPEN_SENT; 588 ch->state = CADET_CHANNEL_OPEN_SENT;
594 if (NULL != ch->last_control_qe) 589 if (NULL != ch->last_control_qe)
595 GCT_send_cancel (ch->last_control_qe); 590 GCT_send_cancel(ch->last_control_qe);
596 ch->last_control_qe = 591 ch->last_control_qe =
597 GCT_send (ch->t, &msgcc.header, &channel_open_sent_cb, ch); 592 GCT_send(ch->t, &msgcc.header, &channel_open_sent_cb, ch);
598 GNUNET_assert (NULL == ch->retry_control_task); 593 GNUNET_assert(NULL == ch->retry_control_task);
599} 594}
600 595
601 596
@@ -610,13 +605,13 @@ send_channel_open (void *cls)
610 * @param ch the channel for which the tunnel is now ready 605 * @param ch the channel for which the tunnel is now ready
611 */ 606 */
612void 607void
613GCCH_tunnel_up (struct CadetChannel *ch) 608GCCH_tunnel_up(struct CadetChannel *ch)
614{ 609{
615 GNUNET_assert (NULL == ch->retry_control_task); 610 GNUNET_assert(NULL == ch->retry_control_task);
616 LOG (GNUNET_ERROR_TYPE_DEBUG, 611 LOG(GNUNET_ERROR_TYPE_DEBUG,
617 "Tunnel up, sending CHANNEL_OPEN on %s now\n", 612 "Tunnel up, sending CHANNEL_OPEN on %s now\n",
618 GCCH_2s (ch)); 613 GCCH_2s(ch));
619 ch->retry_control_task = GNUNET_SCHEDULER_add_now (&send_channel_open, ch); 614 ch->retry_control_task = GNUNET_SCHEDULER_add_now(&send_channel_open, ch);
620} 615}
621 616
622 617
@@ -631,22 +626,22 @@ GCCH_tunnel_up (struct CadetChannel *ch)
631 * @return handle to the new channel 626 * @return handle to the new channel
632 */ 627 */
633struct CadetChannel * 628struct CadetChannel *
634GCCH_channel_local_new (struct CadetClient *owner, 629GCCH_channel_local_new(struct CadetClient *owner,
635 struct GNUNET_CADET_ClientChannelNumber ccn, 630 struct GNUNET_CADET_ClientChannelNumber ccn,
636 struct CadetPeer *destination, 631 struct CadetPeer *destination,
637 const struct GNUNET_HashCode *port, 632 const struct GNUNET_HashCode *port,
638 uint32_t options) 633 uint32_t options)
639{ 634{
640 struct CadetChannel *ch; 635 struct CadetChannel *ch;
641 struct CadetChannelClient *ccco; 636 struct CadetChannelClient *ccco;
642 637
643 ccco = GNUNET_new (struct CadetChannelClient); 638 ccco = GNUNET_new(struct CadetChannelClient);
644 ccco->c = owner; 639 ccco->c = owner;
645 ccco->ccn = ccn; 640 ccco->ccn = ccn;
646 ccco->client_ready = GNUNET_YES; 641 ccco->client_ready = GNUNET_YES;
647 642
648 ch = GNUNET_new (struct CadetChannel); 643 ch = GNUNET_new(struct CadetChannel);
649 ch->mid_recv.mid = htonl (1); /* The OPEN_ACK counts as message 0! */ 644 ch->mid_recv.mid = htonl(1); /* The OPEN_ACK counts as message 0! */
650 ch->nobuffer = GNUNET_NO; 645 ch->nobuffer = GNUNET_NO;
651 ch->reliable = GNUNET_YES; 646 ch->reliable = GNUNET_YES;
652 ch->out_of_order = GNUNET_NO; 647 ch->out_of_order = GNUNET_NO;
@@ -654,44 +649,44 @@ GCCH_channel_local_new (struct CadetClient *owner,
654 (ch->nobuffer) ? 1 : 4; /* FIXME: 4!? Do not hardcode! */ 649 (ch->nobuffer) ? 1 : 4; /* FIXME: 4!? Do not hardcode! */
655 ch->owner = ccco; 650 ch->owner = ccco;
656 ch->port = *port; 651 ch->port = *port;
657 GCCH_hash_port (&ch->h_port, port, GCP_get_id (destination)); 652 GCCH_hash_port(&ch->h_port, port, GCP_get_id(destination));
658 if (0 == GNUNET_memcmp (&my_full_id, GCP_get_id (destination))) 653 if (0 == GNUNET_memcmp(&my_full_id, GCP_get_id(destination)))
659 {
660 struct OpenPort *op;
661
662 ch->is_loopback = GNUNET_YES;
663 op = GNUNET_CONTAINER_multihashmap_get (open_ports, &ch->h_port);
664 if (NULL == op)
665 { 654 {
666 /* port closed, wait for it to possibly open */ 655 struct OpenPort *op;
667 ch->state = CADET_CHANNEL_LOOSE; 656
668 (void) GNUNET_CONTAINER_multihashmap_put ( 657 ch->is_loopback = GNUNET_YES;
669 loose_channels, 658 op = GNUNET_CONTAINER_multihashmap_get(open_ports, &ch->h_port);
670 &ch->h_port, 659 if (NULL == op)
671 ch, 660 {
672 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 661 /* port closed, wait for it to possibly open */
673 LOG (GNUNET_ERROR_TYPE_DEBUG, 662 ch->state = CADET_CHANNEL_LOOSE;
674 "Created loose incoming loopback channel to port %s\n", 663 (void)GNUNET_CONTAINER_multihashmap_put(
675 GNUNET_h2s (&ch->port)); 664 loose_channels,
665 &ch->h_port,
666 ch,
667 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
668 LOG(GNUNET_ERROR_TYPE_DEBUG,
669 "Created loose incoming loopback channel to port %s\n",
670 GNUNET_h2s(&ch->port));
671 }
672 else
673 {
674 GCCH_bind(ch, op->c, &op->port);
675 }
676 } 676 }
677 else 677 else
678 { 678 {
679 GCCH_bind (ch, op->c, &op->port); 679 ch->t = GCP_get_tunnel(destination, GNUNET_YES);
680 ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME;
681 ch->ctn = GCT_add_channel(ch->t, ch);
680 } 682 }
681 } 683 GNUNET_STATISTICS_update(stats, "# channels", 1, GNUNET_NO);
682 else 684 LOG(GNUNET_ERROR_TYPE_DEBUG,
683 { 685 "Created channel to port %s at peer %s for %s using %s\n",
684 ch->t = GCP_get_tunnel (destination, GNUNET_YES); 686 GNUNET_h2s(port),
685 ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME; 687 GCP_2s(destination),
686 ch->ctn = GCT_add_channel (ch->t, ch); 688 GSC_2s(owner),
687 } 689 (GNUNET_YES == ch->is_loopback) ? "loopback" : GCT_2s(ch->t));
688 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO);
689 LOG (GNUNET_ERROR_TYPE_DEBUG,
690 "Created channel to port %s at peer %s for %s using %s\n",
691 GNUNET_h2s (port),
692 GCP_2s (destination),
693 GSC_2s (owner),
694 (GNUNET_YES == ch->is_loopback) ? "loopback" : GCT_2s (ch->t));
695 return ch; 690 return ch;
696} 691}
697 692
@@ -703,16 +698,16 @@ GCCH_channel_local_new (struct CadetClient *owner,
703 * @param cls the channel to drop 698 * @param cls the channel to drop
704 */ 699 */
705static void 700static void
706timeout_closed_cb (void *cls) 701timeout_closed_cb(void *cls)
707{ 702{
708 struct CadetChannel *ch = cls; 703 struct CadetChannel *ch = cls;
709 704
710 ch->retry_control_task = NULL; 705 ch->retry_control_task = NULL;
711 LOG (GNUNET_ERROR_TYPE_DEBUG, 706 LOG(GNUNET_ERROR_TYPE_DEBUG,
712 "Closing incoming channel to port %s from peer %s due to timeout\n", 707 "Closing incoming channel to port %s from peer %s due to timeout\n",
713 GNUNET_h2s (&ch->port), 708 GNUNET_h2s(&ch->port),
714 GCP_2s (GCT_get_destination (ch->t))); 709 GCP_2s(GCT_get_destination(ch->t)));
715 channel_destroy (ch); 710 channel_destroy(ch);
716} 711}
717 712
718 713
@@ -726,15 +721,15 @@ timeout_closed_cb (void *cls)
726 * @return handle to the new channel 721 * @return handle to the new channel
727 */ 722 */
728struct CadetChannel * 723struct CadetChannel *
729GCCH_channel_incoming_new (struct CadetTunnel *t, 724GCCH_channel_incoming_new(struct CadetTunnel *t,
730 struct GNUNET_CADET_ChannelTunnelNumber ctn, 725 struct GNUNET_CADET_ChannelTunnelNumber ctn,
731 const struct GNUNET_HashCode *h_port, 726 const struct GNUNET_HashCode *h_port,
732 uint32_t options) 727 uint32_t options)
733{ 728{
734 struct CadetChannel *ch; 729 struct CadetChannel *ch;
735 struct OpenPort *op; 730 struct OpenPort *op;
736 731
737 ch = GNUNET_new (struct CadetChannel); 732 ch = GNUNET_new(struct CadetChannel);
738 ch->h_port = *h_port; 733 ch->h_port = *h_port;
739 ch->t = t; 734 ch->t = t;
740 ch->ctn = ctn; 735 ch->ctn = ctn;
@@ -744,32 +739,32 @@ GCCH_channel_incoming_new (struct CadetTunnel *t,
744 ch->out_of_order = GNUNET_NO; 739 ch->out_of_order = GNUNET_NO;
745 ch->max_pending_messages = 740 ch->max_pending_messages =
746 (ch->nobuffer) ? 1 : 4; /* FIXME: 4!? Do not hardcode! */ 741 (ch->nobuffer) ? 1 : 4; /* FIXME: 4!? Do not hardcode! */
747 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO); 742 GNUNET_STATISTICS_update(stats, "# channels", 1, GNUNET_NO);
748 743
749 op = GNUNET_CONTAINER_multihashmap_get (open_ports, h_port); 744 op = GNUNET_CONTAINER_multihashmap_get(open_ports, h_port);
750 if (NULL == op) 745 if (NULL == op)
751 { 746 {
752 /* port closed, wait for it to possibly open */ 747 /* port closed, wait for it to possibly open */
753 ch->state = CADET_CHANNEL_LOOSE; 748 ch->state = CADET_CHANNEL_LOOSE;
754 (void) GNUNET_CONTAINER_multihashmap_put ( 749 (void)GNUNET_CONTAINER_multihashmap_put(
755 loose_channels, 750 loose_channels,
756 &ch->h_port, 751 &ch->h_port,
757 ch, 752 ch,
758 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 753 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
759 GNUNET_assert (NULL == ch->retry_control_task); 754 GNUNET_assert(NULL == ch->retry_control_task);
760 ch->retry_control_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT_CLOSED_PORT, 755 ch->retry_control_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT_CLOSED_PORT,
761 &timeout_closed_cb, 756 &timeout_closed_cb,
762 ch); 757 ch);
763 LOG (GNUNET_ERROR_TYPE_DEBUG, 758 LOG(GNUNET_ERROR_TYPE_DEBUG,
764 "Created loose incoming channel to port %s from peer %s\n", 759 "Created loose incoming channel to port %s from peer %s\n",
765 GNUNET_h2s (&ch->port), 760 GNUNET_h2s(&ch->port),
766 GCP_2s (GCT_get_destination (ch->t))); 761 GCP_2s(GCT_get_destination(ch->t)));
767 } 762 }
768 else 763 else
769 { 764 {
770 GCCH_bind (ch, op->c, &op->port); 765 GCCH_bind(ch, op->c, &op->port);
771 } 766 }
772 GNUNET_STATISTICS_update (stats, "# channels", 1, GNUNET_NO); 767 GNUNET_STATISTICS_update(stats, "# channels", 1, GNUNET_NO);
773 return ch; 768 return ch;
774} 769}
775 770
@@ -784,12 +779,12 @@ GCCH_channel_incoming_new (struct CadetTunnel *t,
784 * if transmission failed 779 * if transmission failed
785 */ 780 */
786static void 781static void
787send_ack_cb (void *cls, 782send_ack_cb(void *cls,
788 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid) 783 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
789{ 784{
790 struct CadetChannel *ch = cls; 785 struct CadetChannel *ch = cls;
791 786
792 GNUNET_assert (NULL != ch->last_control_qe); 787 GNUNET_assert(NULL != ch->last_control_qe);
793 ch->last_control_qe = NULL; 788 ch->last_control_qe = NULL;
794} 789}
795 790
@@ -800,25 +795,25 @@ send_ack_cb (void *cls,
800 * @param ch channel to send the #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK for 795 * @param ch channel to send the #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK for
801 */ 796 */
802static void 797static void
803send_channel_data_ack (struct CadetChannel *ch) 798send_channel_data_ack(struct CadetChannel *ch)
804{ 799{
805 struct GNUNET_CADET_ChannelDataAckMessage msg; 800 struct GNUNET_CADET_ChannelDataAckMessage msg;
806 801
807 if (GNUNET_NO == ch->reliable) 802 if (GNUNET_NO == ch->reliable)
808 return; /* no ACKs */ 803 return; /* no ACKs */
809 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK); 804 msg.header.type = htons(GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK);
810 msg.header.size = htons (sizeof (msg)); 805 msg.header.size = htons(sizeof(msg));
811 msg.ctn = ch->ctn; 806 msg.ctn = ch->ctn;
812 msg.mid.mid = htonl (ntohl (ch->mid_recv.mid)); 807 msg.mid.mid = htonl(ntohl(ch->mid_recv.mid));
813 msg.futures = GNUNET_htonll (ch->mid_futures); 808 msg.futures = GNUNET_htonll(ch->mid_futures);
814 LOG (GNUNET_ERROR_TYPE_DEBUG, 809 LOG(GNUNET_ERROR_TYPE_DEBUG,
815 "Sending DATA_ACK %u:%llX via %s\n", 810 "Sending DATA_ACK %u:%llX via %s\n",
816 (unsigned int) ntohl (msg.mid.mid), 811 (unsigned int)ntohl(msg.mid.mid),
817 (unsigned long long) ch->mid_futures, 812 (unsigned long long)ch->mid_futures,
818 GCCH_2s (ch)); 813 GCCH_2s(ch));
819 if (NULL != ch->last_control_qe) 814 if (NULL != ch->last_control_qe)
820 GCT_send_cancel (ch->last_control_qe); 815 GCT_send_cancel(ch->last_control_qe);
821 ch->last_control_qe = GCT_send (ch->t, &msg.header, &send_ack_cb, ch); 816 ch->last_control_qe = GCT_send(ch->t, &msg.header, &send_ack_cb, ch);
822} 817}
823 818
824 819
@@ -829,23 +824,23 @@ send_channel_data_ack (struct CadetChannel *ch)
829 * @param cls the `struct CadetChannel` 824 * @param cls the `struct CadetChannel`
830 */ 825 */
831static void 826static void
832send_open_ack (void *cls) 827send_open_ack(void *cls)
833{ 828{
834 struct CadetChannel *ch = cls; 829 struct CadetChannel *ch = cls;
835 struct GNUNET_CADET_ChannelOpenAckMessage msg; 830 struct GNUNET_CADET_ChannelOpenAckMessage msg;
836 831
837 ch->retry_control_task = NULL; 832 ch->retry_control_task = NULL;
838 LOG (GNUNET_ERROR_TYPE_DEBUG, 833 LOG(GNUNET_ERROR_TYPE_DEBUG,
839 "Sending CHANNEL_OPEN_ACK on %s\n", 834 "Sending CHANNEL_OPEN_ACK on %s\n",
840 GCCH_2s (ch)); 835 GCCH_2s(ch));
841 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK); 836 msg.header.type = htons(GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK);
842 msg.header.size = htons (sizeof (msg)); 837 msg.header.size = htons(sizeof(msg));
843 msg.reserved = htonl (0); 838 msg.reserved = htonl(0);
844 msg.ctn = ch->ctn; 839 msg.ctn = ch->ctn;
845 msg.port = ch->port; 840 msg.port = ch->port;
846 if (NULL != ch->last_control_qe) 841 if (NULL != ch->last_control_qe)
847 GCT_send_cancel (ch->last_control_qe); 842 GCT_send_cancel(ch->last_control_qe);
848 ch->last_control_qe = GCT_send (ch->t, &msg.header, &send_ack_cb, ch); 843 ch->last_control_qe = GCT_send(ch->t, &msg.header, &send_ack_cb, ch);
849} 844}
850 845
851 846
@@ -858,28 +853,28 @@ send_open_ack (void *cls)
858 * @param cti identifier of the connection that delivered the message 853 * @param cti identifier of the connection that delivered the message
859 */ 854 */
860void 855void
861GCCH_handle_duplicate_open ( 856GCCH_handle_duplicate_open(
862 struct CadetChannel *ch, 857 struct CadetChannel *ch,
863 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti) 858 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti)
864{ 859{
865 if (NULL == ch->dest) 860 if (NULL == ch->dest)
866 { 861 {
867 LOG (GNUNET_ERROR_TYPE_DEBUG, 862 LOG(GNUNET_ERROR_TYPE_DEBUG,
868 "Ignoring duplicate CHANNEL_OPEN on %s: port is closed\n", 863 "Ignoring duplicate CHANNEL_OPEN on %s: port is closed\n",
869 GCCH_2s (ch)); 864 GCCH_2s(ch));
870 return; 865 return;
871 } 866 }
872 if (NULL != ch->retry_control_task) 867 if (NULL != ch->retry_control_task)
873 { 868 {
874 LOG (GNUNET_ERROR_TYPE_DEBUG, 869 LOG(GNUNET_ERROR_TYPE_DEBUG,
875 "Ignoring duplicate CHANNEL_OPEN on %s: control message is pending\n", 870 "Ignoring duplicate CHANNEL_OPEN on %s: control message is pending\n",
876 GCCH_2s (ch)); 871 GCCH_2s(ch));
877 return; 872 return;
878 } 873 }
879 LOG (GNUNET_ERROR_TYPE_DEBUG, 874 LOG(GNUNET_ERROR_TYPE_DEBUG,
880 "Retransmitting CHANNEL_OPEN_ACK on %s\n", 875 "Retransmitting CHANNEL_OPEN_ACK on %s\n",
881 GCCH_2s (ch)); 876 GCCH_2s(ch));
882 ch->retry_control_task = GNUNET_SCHEDULER_add_now (&send_open_ack, ch); 877 ch->retry_control_task = GNUNET_SCHEDULER_add_now(&send_open_ack, ch);
883} 878}
884 879
885 880
@@ -891,7 +886,7 @@ GCCH_handle_duplicate_open (
891 * #GNUNET_NO to send to dest 886 * #GNUNET_NO to send to dest
892 */ 887 */
893static void 888static void
894send_ack_to_client (struct CadetChannel *ch, int to_owner) 889send_ack_to_client(struct CadetChannel *ch, int to_owner)
895{ 890{
896 struct GNUNET_MQ_Envelope *env; 891 struct GNUNET_MQ_Envelope *env;
897 struct GNUNET_CADET_LocalAck *ack; 892 struct GNUNET_CADET_LocalAck *ack;
@@ -899,22 +894,22 @@ send_ack_to_client (struct CadetChannel *ch, int to_owner)
899 894
900 ccc = (GNUNET_YES == to_owner) ? ch->owner : ch->dest; 895 ccc = (GNUNET_YES == to_owner) ? ch->owner : ch->dest;
901 if (NULL == ccc) 896 if (NULL == ccc)
902 { 897 {
903 /* This can happen if we are just getting ACKs after 898 /* This can happen if we are just getting ACKs after
904 our local client already disconnected. */ 899 our local client already disconnected. */
905 GNUNET_assert (GNUNET_YES == ch->destroy); 900 GNUNET_assert(GNUNET_YES == ch->destroy);
906 return; 901 return;
907 } 902 }
908 env = GNUNET_MQ_msg (ack, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK); 903 env = GNUNET_MQ_msg(ack, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
909 ack->ccn = ccc->ccn; 904 ack->ccn = ccc->ccn;
910 LOG (GNUNET_ERROR_TYPE_DEBUG, 905 LOG(GNUNET_ERROR_TYPE_DEBUG,
911 "Sending CADET_LOCAL_ACK to %s (%s) at ccn %X (%u/%u pending)\n", 906 "Sending CADET_LOCAL_ACK to %s (%s) at ccn %X (%u/%u pending)\n",
912 GSC_2s (ccc->c), 907 GSC_2s(ccc->c),
913 (GNUNET_YES == to_owner) ? "owner" : "dest", 908 (GNUNET_YES == to_owner) ? "owner" : "dest",
914 ntohl (ack->ccn.channel_of_client), 909 ntohl(ack->ccn.channel_of_client),
915 ch->pending_messages, 910 ch->pending_messages,
916 ch->max_pending_messages); 911 ch->max_pending_messages);
917 GSC_send_to_client (ccc->c, env); 912 GSC_send_to_client(ccc->c, env);
918} 913}
919 914
920 915
@@ -928,58 +923,58 @@ send_ack_to_client (struct CadetChannel *ch, int to_owner)
928 * @param port the port @a is listening on 923 * @param port the port @a is listening on
929 */ 924 */
930void 925void
931GCCH_bind (struct CadetChannel *ch, 926GCCH_bind(struct CadetChannel *ch,
932 struct CadetClient *c, 927 struct CadetClient *c,
933 const struct GNUNET_HashCode *port) 928 const struct GNUNET_HashCode *port)
934{ 929{
935 uint32_t options; 930 uint32_t options;
936 struct CadetChannelClient *cccd; 931 struct CadetChannelClient *cccd;
937 932
938 LOG (GNUNET_ERROR_TYPE_DEBUG, 933 LOG(GNUNET_ERROR_TYPE_DEBUG,
939 "Binding %s from %s to port %s of %s\n", 934 "Binding %s from %s to port %s of %s\n",
940 GCCH_2s (ch), 935 GCCH_2s(ch),
941 GCT_2s (ch->t), 936 GCT_2s(ch->t),
942 GNUNET_h2s (&ch->port), 937 GNUNET_h2s(&ch->port),
943 GSC_2s (c)); 938 GSC_2s(c));
944 if (NULL != ch->retry_control_task) 939 if (NULL != ch->retry_control_task)
945 { 940 {
946 /* there might be a timeout task here */ 941 /* there might be a timeout task here */
947 GNUNET_SCHEDULER_cancel (ch->retry_control_task); 942 GNUNET_SCHEDULER_cancel(ch->retry_control_task);
948 ch->retry_control_task = NULL; 943 ch->retry_control_task = NULL;
949 } 944 }
950 options = 0; 945 options = 0;
951 cccd = GNUNET_new (struct CadetChannelClient); 946 cccd = GNUNET_new(struct CadetChannelClient);
952 GNUNET_assert (NULL == ch->dest); 947 GNUNET_assert(NULL == ch->dest);
953 ch->dest = cccd; 948 ch->dest = cccd;
954 ch->port = *port; 949 ch->port = *port;
955 cccd->c = c; 950 cccd->c = c;
956 cccd->client_ready = GNUNET_YES; 951 cccd->client_ready = GNUNET_YES;
957 cccd->ccn = GSC_bind (c, 952 cccd->ccn = GSC_bind(c,
958 ch, 953 ch,
959 (GNUNET_YES == ch->is_loopback) 954 (GNUNET_YES == ch->is_loopback)
960 ? GCP_get (&my_full_id, GNUNET_YES) 955 ? GCP_get(&my_full_id, GNUNET_YES)
961 : GCT_get_destination (ch->t), 956 : GCT_get_destination(ch->t),
962 port, 957 port,
963 options); 958 options);
964 GNUNET_assert (ntohl (cccd->ccn.channel_of_client) < 959 GNUNET_assert(ntohl(cccd->ccn.channel_of_client) <
965 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI); 960 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
966 ch->mid_recv.mid = htonl (1); /* The OPEN counts as message 0! */ 961 ch->mid_recv.mid = htonl(1); /* The OPEN counts as message 0! */
967 if (GNUNET_YES == ch->is_loopback) 962 if (GNUNET_YES == ch->is_loopback)
968 { 963 {
969 ch->state = CADET_CHANNEL_OPEN_SENT; 964 ch->state = CADET_CHANNEL_OPEN_SENT;
970 GCCH_handle_channel_open_ack (ch, NULL, port); 965 GCCH_handle_channel_open_ack(ch, NULL, port);
971 } 966 }
972 else 967 else
973 { 968 {
974 /* notify other peer that we accepted the connection */ 969 /* notify other peer that we accepted the connection */
975 ch->state = CADET_CHANNEL_READY; 970 ch->state = CADET_CHANNEL_READY;
976 ch->retry_control_task = GNUNET_SCHEDULER_add_now (&send_open_ack, ch); 971 ch->retry_control_task = GNUNET_SCHEDULER_add_now(&send_open_ack, ch);
977 } 972 }
978 /* give client it's initial supply of ACKs */ 973 /* give client it's initial supply of ACKs */
979 GNUNET_assert (ntohl (cccd->ccn.channel_of_client) < 974 GNUNET_assert(ntohl(cccd->ccn.channel_of_client) <
980 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI); 975 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
981 for (unsigned int i = 0; i < ch->max_pending_messages; i++) 976 for (unsigned int i = 0; i < ch->max_pending_messages; i++)
982 send_ack_to_client (ch, GNUNET_NO); 977 send_ack_to_client(ch, GNUNET_NO);
983} 978}
984 979
985 980
@@ -991,7 +986,7 @@ GCCH_bind (struct CadetChannel *ch,
991 * @param cls the `struct CadetChannel` where one of the ends is now dead 986 * @param cls the `struct CadetChannel` where one of the ends is now dead
992 */ 987 */
993static void 988static void
994signal_remote_destroy_cb (void *cls) 989signal_remote_destroy_cb(void *cls)
995{ 990{
996 struct CadetChannel *ch = cls; 991 struct CadetChannel *ch = cls;
997 struct CadetChannelClient *ccc; 992 struct CadetChannelClient *ccc;
@@ -999,8 +994,8 @@ signal_remote_destroy_cb (void *cls)
999 /* Find which end is left... */ 994 /* Find which end is left... */
1000 ch->retry_control_task = NULL; 995 ch->retry_control_task = NULL;
1001 ccc = (NULL != ch->owner) ? ch->owner : ch->dest; 996 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1002 GSC_handle_remote_channel_destroy (ccc->c, ccc->ccn, ch); 997 GSC_handle_remote_channel_destroy(ccc->c, ccc->ccn, ch);
1003 channel_destroy (ch); 998 channel_destroy(ch);
1004} 999}
1005 1000
1006 1001
@@ -1013,72 +1008,74 @@ signal_remote_destroy_cb (void *cls)
1013 * @param ccn client number of the client @a c 1008 * @param ccn client number of the client @a c
1014 */ 1009 */
1015void 1010void
1016GCCH_channel_local_destroy (struct CadetChannel *ch, 1011GCCH_channel_local_destroy(struct CadetChannel *ch,
1017 struct CadetClient *c, 1012 struct CadetClient *c,
1018 struct GNUNET_CADET_ClientChannelNumber ccn) 1013 struct GNUNET_CADET_ClientChannelNumber ccn)
1019{ 1014{
1020 LOG (GNUNET_ERROR_TYPE_DEBUG, 1015 LOG(GNUNET_ERROR_TYPE_DEBUG,
1021 "%s asks for destruction of %s\n", 1016 "%s asks for destruction of %s\n",
1022 GSC_2s (c), 1017 GSC_2s(c),
1023 GCCH_2s (ch)); 1018 GCCH_2s(ch));
1024 GNUNET_assert (NULL != c); 1019 GNUNET_assert(NULL != c);
1025 if ((NULL != ch->owner) && (c == ch->owner->c) && 1020 if ((NULL != ch->owner) && (c == ch->owner->c) &&
1026 (ccn.channel_of_client == ch->owner->ccn.channel_of_client)) 1021 (ccn.channel_of_client == ch->owner->ccn.channel_of_client))
1027 { 1022 {
1028 free_channel_client (ch->owner); 1023 free_channel_client(ch->owner);
1029 ch->owner = NULL; 1024 ch->owner = NULL;
1030 } 1025 }
1031 else if ((NULL != ch->dest) && (c == ch->dest->c) && 1026 else if ((NULL != ch->dest) && (c == ch->dest->c) &&
1032 (ccn.channel_of_client == ch->dest->ccn.channel_of_client)) 1027 (ccn.channel_of_client == ch->dest->ccn.channel_of_client))
1033 { 1028 {
1034 free_channel_client (ch->dest); 1029 free_channel_client(ch->dest);
1035 ch->dest = NULL; 1030 ch->dest = NULL;
1036 } 1031 }
1037 else 1032 else
1038 { 1033 {
1039 GNUNET_assert (0); 1034 GNUNET_assert(0);
1040 } 1035 }
1041 1036
1042 if (GNUNET_YES == ch->destroy) 1037 if (GNUNET_YES == ch->destroy)
1043 { 1038 {
1044 /* other end already destroyed, with the local client gone, no need 1039 /* other end already destroyed, with the local client gone, no need
1045 to finish transmissions, just destroy immediately. */ 1040 to finish transmissions, just destroy immediately. */
1046 channel_destroy (ch); 1041 channel_destroy(ch);
1047 return; 1042 return;
1048 } 1043 }
1049 if ((NULL != ch->head_sent) && ((NULL != ch->owner) || (NULL != ch->dest))) 1044 if ((NULL != ch->head_sent) && ((NULL != ch->owner) || (NULL != ch->dest)))
1050 { 1045 {
1051 /* Wait for other end to destroy us as well, 1046 /* Wait for other end to destroy us as well,
1052 and otherwise allow send queue to be transmitted first */ 1047 and otherwise allow send queue to be transmitted first */
1053 ch->destroy = GNUNET_YES; 1048 ch->destroy = GNUNET_YES;
1054 return; 1049 return;
1055 } 1050 }
1056 if ((GNUNET_YES == ch->is_loopback) && 1051 if ((GNUNET_YES == ch->is_loopback) &&
1057 ((NULL != ch->owner) || (NULL != ch->dest))) 1052 ((NULL != ch->owner) || (NULL != ch->dest)))
1058 { 1053 {
1059 if (NULL != ch->retry_control_task) 1054 if (NULL != ch->retry_control_task)
1060 GNUNET_SCHEDULER_cancel (ch->retry_control_task); 1055 GNUNET_SCHEDULER_cancel(ch->retry_control_task);
1061 ch->retry_control_task = 1056 ch->retry_control_task =
1062 GNUNET_SCHEDULER_add_now (&signal_remote_destroy_cb, ch); 1057 GNUNET_SCHEDULER_add_now(&signal_remote_destroy_cb, ch);
1063 return; 1058 return;
1064 } 1059 }
1065 if (GNUNET_NO == ch->is_loopback) 1060 if (GNUNET_NO == ch->is_loopback)
1066 {
1067 /* If the we ever sent the CHANNEL_CREATE, we need to send a destroy message. */
1068 switch (ch->state)
1069 { 1061 {
1070 case CADET_CHANNEL_NEW: 1062 /* If the we ever sent the CHANNEL_CREATE, we need to send a destroy message. */
1071 /* We gave up on a channel that we created as a client to a remote 1063 switch (ch->state)
1072 target, but that never went anywhere. Nothing to do here. */ 1064 {
1073 break; 1065 case CADET_CHANNEL_NEW:
1074 case CADET_CHANNEL_LOOSE: 1066 /* We gave up on a channel that we created as a client to a remote
1075 break; 1067 target, but that never went anywhere. Nothing to do here. */
1076 default: 1068 break;
1077 GCT_send_channel_destroy (ch->t, ch->ctn); 1069
1070 case CADET_CHANNEL_LOOSE:
1071 break;
1072
1073 default:
1074 GCT_send_channel_destroy(ch->t, ch->ctn);
1075 }
1078 } 1076 }
1079 }
1080 /* Nothing left to do, just finish destruction */ 1077 /* Nothing left to do, just finish destruction */
1081 channel_destroy (ch); 1078 channel_destroy(ch);
1082} 1079}
1083 1080
1084 1081
@@ -1092,57 +1089,60 @@ GCCH_channel_local_destroy (struct CadetChannel *ch,
1092 * @param port port number (needed to verify receiver knows the port) 1089 * @param port port number (needed to verify receiver knows the port)
1093 */ 1090 */
1094void 1091void
1095GCCH_handle_channel_open_ack ( 1092GCCH_handle_channel_open_ack(
1096 struct CadetChannel *ch, 1093 struct CadetChannel *ch,
1097 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti, 1094 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1098 const struct GNUNET_HashCode *port) 1095 const struct GNUNET_HashCode *port)
1099{ 1096{
1100 switch (ch->state) 1097 switch (ch->state)
1101 {
1102 case CADET_CHANNEL_NEW:
1103 /* this should be impossible */
1104 GNUNET_break (0);
1105 break;
1106 case CADET_CHANNEL_LOOSE:
1107 /* This makes no sense. */
1108 GNUNET_break_op (0);
1109 break;
1110 case CADET_CHANNEL_OPEN_SENT:
1111 if (NULL == ch->owner)
1112 {
1113 /* We're not the owner, wrong direction! */
1114 GNUNET_break_op (0);
1115 return;
1116 }
1117 if (0 != GNUNET_memcmp (&ch->port, port))
1118 {
1119 /* Other peer failed to provide the right port,
1120 refuse connection. */
1121 GNUNET_break_op (0);
1122 return;
1123 }
1124 LOG (GNUNET_ERROR_TYPE_DEBUG,
1125 "Received CHANNEL_OPEN_ACK for waiting %s, entering READY state\n",
1126 GCCH_2s (ch));
1127 if (NULL != ch->retry_control_task) /* can be NULL if ch->is_loopback */
1128 { 1098 {
1129 GNUNET_SCHEDULER_cancel (ch->retry_control_task); 1099 case CADET_CHANNEL_NEW:
1130 ch->retry_control_task = NULL; 1100 /* this should be impossible */
1101 GNUNET_break(0);
1102 break;
1103
1104 case CADET_CHANNEL_LOOSE:
1105 /* This makes no sense. */
1106 GNUNET_break_op(0);
1107 break;
1108
1109 case CADET_CHANNEL_OPEN_SENT:
1110 if (NULL == ch->owner)
1111 {
1112 /* We're not the owner, wrong direction! */
1113 GNUNET_break_op(0);
1114 return;
1115 }
1116 if (0 != GNUNET_memcmp(&ch->port, port))
1117 {
1118 /* Other peer failed to provide the right port,
1119 refuse connection. */
1120 GNUNET_break_op(0);
1121 return;
1122 }
1123 LOG(GNUNET_ERROR_TYPE_DEBUG,
1124 "Received CHANNEL_OPEN_ACK for waiting %s, entering READY state\n",
1125 GCCH_2s(ch));
1126 if (NULL != ch->retry_control_task) /* can be NULL if ch->is_loopback */
1127 {
1128 GNUNET_SCHEDULER_cancel(ch->retry_control_task);
1129 ch->retry_control_task = NULL;
1130 }
1131 ch->state = CADET_CHANNEL_READY;
1132 /* On first connect, send client as many ACKs as we allow messages
1133 to be buffered! */
1134 for (unsigned int i = 0; i < ch->max_pending_messages; i++)
1135 send_ack_to_client(ch, GNUNET_YES);
1136 break;
1137
1138 case CADET_CHANNEL_READY:
1139 /* duplicate ACK, maybe we retried the CREATE. Ignore. */
1140 LOG(GNUNET_ERROR_TYPE_DEBUG,
1141 "Received duplicate channel OPEN_ACK for %s\n",
1142 GCCH_2s(ch));
1143 GNUNET_STATISTICS_update(stats, "# duplicate CREATE_ACKs", 1, GNUNET_NO);
1144 break;
1131 } 1145 }
1132 ch->state = CADET_CHANNEL_READY;
1133 /* On first connect, send client as many ACKs as we allow messages
1134 to be buffered! */
1135 for (unsigned int i = 0; i < ch->max_pending_messages; i++)
1136 send_ack_to_client (ch, GNUNET_YES);
1137 break;
1138 case CADET_CHANNEL_READY:
1139 /* duplicate ACK, maybe we retried the CREATE. Ignore. */
1140 LOG (GNUNET_ERROR_TYPE_DEBUG,
1141 "Received duplicate channel OPEN_ACK for %s\n",
1142 GCCH_2s (ch));
1143 GNUNET_STATISTICS_update (stats, "# duplicate CREATE_ACKs", 1, GNUNET_NO);
1144 break;
1145 }
1146} 1146}
1147 1147
1148 1148
@@ -1155,28 +1155,28 @@ GCCH_handle_channel_open_ack (
1155 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO 1155 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
1156 */ 1156 */
1157static int 1157static int
1158is_before (void *cls, 1158is_before(void *cls,
1159 struct CadetOutOfOrderMessage *m1, 1159 struct CadetOutOfOrderMessage *m1,
1160 struct CadetOutOfOrderMessage *m2) 1160 struct CadetOutOfOrderMessage *m2)
1161{ 1161{
1162 int *duplicate = cls; 1162 int *duplicate = cls;
1163 uint32_t v1 = ntohl (m1->mid.mid); 1163 uint32_t v1 = ntohl(m1->mid.mid);
1164 uint32_t v2 = ntohl (m2->mid.mid); 1164 uint32_t v2 = ntohl(m2->mid.mid);
1165 uint32_t delta; 1165 uint32_t delta;
1166 1166
1167 delta = v2 - v1; 1167 delta = v2 - v1;
1168 if (0 == delta) 1168 if (0 == delta)
1169 *duplicate = GNUNET_YES; 1169 *duplicate = GNUNET_YES;
1170 if (delta > (uint32_t) INT_MAX) 1170 if (delta > (uint32_t)INT_MAX)
1171 { 1171 {
1172 /* in overflow range, we can safely assume we wrapped around */ 1172 /* in overflow range, we can safely assume we wrapped around */
1173 return GNUNET_NO; 1173 return GNUNET_NO;
1174 } 1174 }
1175 else 1175 else
1176 { 1176 {
1177 /* result is small, thus v2 > v1, thus m1 < m2 */ 1177 /* result is small, thus v2 > v1, thus m1 < m2 */
1178 return GNUNET_YES; 1178 return GNUNET_YES;
1179 } 1179 }
1180} 1180}
1181 1181
1182 1182
@@ -1189,7 +1189,7 @@ is_before (void *cls,
1189 * @param msg message that was received 1189 * @param msg message that was received
1190 */ 1190 */
1191void 1191void
1192GCCH_handle_channel_plaintext_data ( 1192GCCH_handle_channel_plaintext_data(
1193 struct CadetChannel *ch, 1193 struct CadetChannel *ch,
1194 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti, 1194 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1195 const struct GNUNET_CADET_ChannelAppDataMessage *msg) 1195 const struct GNUNET_CADET_ChannelAppDataMessage *msg)
@@ -1205,239 +1205,239 @@ GCCH_handle_channel_plaintext_data (
1205 uint32_t mid_msg; 1205 uint32_t mid_msg;
1206 uint32_t delta; 1206 uint32_t delta;
1207 1207
1208 GNUNET_assert (GNUNET_NO == ch->is_loopback); 1208 GNUNET_assert(GNUNET_NO == ch->is_loopback);
1209 if ((NULL == ch->owner) && (NULL == ch->dest)) 1209 if ((NULL == ch->owner) && (NULL == ch->dest))
1210 { 1210 {
1211 /* This client is gone, but we still have messages to send to 1211 /* This client is gone, but we still have messages to send to
1212 the other end (which is why @a ch is not yet dead). However, 1212 the other end (which is why @a ch is not yet dead). However,
1213 we cannot pass messages to our client anymore. */ 1213 we cannot pass messages to our client anymore. */
1214 LOG (GNUNET_ERROR_TYPE_DEBUG, 1214 LOG(GNUNET_ERROR_TYPE_DEBUG,
1215 "Dropping incoming payload on %s as this end is already closed\n", 1215 "Dropping incoming payload on %s as this end is already closed\n",
1216 GCCH_2s (ch)); 1216 GCCH_2s(ch));
1217 /* send back DESTROY notification to stop further retransmissions! */ 1217 /* send back DESTROY notification to stop further retransmissions! */
1218 if (GNUNET_YES == ch->destroy) 1218 if (GNUNET_YES == ch->destroy)
1219 GCT_send_channel_destroy (ch->t, ch->ctn); 1219 GCT_send_channel_destroy(ch->t, ch->ctn);
1220 return; 1220 return;
1221 } 1221 }
1222 payload_size = ntohs (msg->header.size) - sizeof (*msg); 1222 payload_size = ntohs(msg->header.size) - sizeof(*msg);
1223 env = GNUNET_MQ_msg_extra (ld, 1223 env = GNUNET_MQ_msg_extra(ld,
1224 payload_size, 1224 payload_size,
1225 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA); 1225 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1226 ld->ccn = (NULL == ch->dest) ? ch->owner->ccn : ch->dest->ccn; 1226 ld->ccn = (NULL == ch->dest) ? ch->owner->ccn : ch->dest->ccn;
1227 GNUNET_memcpy (&ld[1], &msg[1], payload_size); 1227 GNUNET_memcpy(&ld[1], &msg[1], payload_size);
1228 ccc = (NULL != ch->owner) ? ch->owner : ch->dest; 1228 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1229 if (GNUNET_YES == ccc->client_ready) 1229 if (GNUNET_YES == ccc->client_ready)
1230 {
1231 /*
1232 * We ad-hoc send the message if
1233 * - The channel is out-of-order
1234 * - The channel is reliable and MID matches next expected MID
1235 * - The channel is unreliable and MID is before lowest seen MID
1236 */
1237 if ((GNUNET_YES == ch->out_of_order) ||
1238 ((msg->mid.mid == ch->mid_recv.mid) && (GNUNET_YES == ch->reliable)) ||
1239 ((GNUNET_NO == ch->reliable) &&
1240 (ntohl (msg->mid.mid) >= ntohl (ch->mid_recv.mid)) &&
1241 ((NULL == ccc->head_recv) ||
1242 (ntohl (msg->mid.mid) < ntohl (ccc->head_recv->mid.mid)))))
1243 { 1230 {
1244 LOG (GNUNET_ERROR_TYPE_DEBUG, 1231 /*
1245 "Giving %u bytes of payload with MID %u from %s to client %s\n", 1232 * We ad-hoc send the message if
1246 (unsigned int) payload_size, 1233 * - The channel is out-of-order
1247 ntohl (msg->mid.mid), 1234 * - The channel is reliable and MID matches next expected MID
1248 GCCH_2s (ch), 1235 * - The channel is unreliable and MID is before lowest seen MID
1249 GSC_2s (ccc->c)); 1236 */
1250 ccc->client_ready = GNUNET_NO; 1237 if ((GNUNET_YES == ch->out_of_order) ||
1251 GSC_send_to_client (ccc->c, env); 1238 ((msg->mid.mid == ch->mid_recv.mid) && (GNUNET_YES == ch->reliable)) ||
1252 if (GNUNET_NO == ch->out_of_order) 1239 ((GNUNET_NO == ch->reliable) &&
1253 ch->mid_recv.mid = htonl (1 + ntohl (msg->mid.mid)); 1240 (ntohl(msg->mid.mid) >= ntohl(ch->mid_recv.mid)) &&
1254 else 1241 ((NULL == ccc->head_recv) ||
1255 ch->mid_recv.mid = htonl (1 + ntohl (ch->mid_recv.mid)); 1242 (ntohl(msg->mid.mid) < ntohl(ccc->head_recv->mid.mid)))))
1256 ch->mid_futures >>= 1; 1243 {
1257 if ((GNUNET_YES == ch->out_of_order) && (GNUNET_NO == ch->reliable)) 1244 LOG(GNUNET_ERROR_TYPE_DEBUG,
1258 { 1245 "Giving %u bytes of payload with MID %u from %s to client %s\n",
1259 /* possibly shift by more if we skipped messages */ 1246 (unsigned int)payload_size,
1260 uint64_t delta = htonl (msg->mid.mid) - 1 - ntohl (ch->mid_recv.mid); 1247 ntohl(msg->mid.mid),
1261 1248 GCCH_2s(ch),
1262 if (delta > 63) 1249 GSC_2s(ccc->c));
1263 ch->mid_futures = 0; 1250 ccc->client_ready = GNUNET_NO;
1264 else 1251 GSC_send_to_client(ccc->c, env);
1265 ch->mid_futures >>= delta; 1252 if (GNUNET_NO == ch->out_of_order)
1266 ch->mid_recv.mid = htonl (1 + ntohl (msg->mid.mid)); 1253 ch->mid_recv.mid = htonl(1 + ntohl(msg->mid.mid));
1267 } 1254 else
1268 send_channel_data_ack (ch); 1255 ch->mid_recv.mid = htonl(1 + ntohl(ch->mid_recv.mid));
1269 return; 1256 ch->mid_futures >>= 1;
1257 if ((GNUNET_YES == ch->out_of_order) && (GNUNET_NO == ch->reliable))
1258 {
1259 /* possibly shift by more if we skipped messages */
1260 uint64_t delta = htonl(msg->mid.mid) - 1 - ntohl(ch->mid_recv.mid);
1261
1262 if (delta > 63)
1263 ch->mid_futures = 0;
1264 else
1265 ch->mid_futures >>= delta;
1266 ch->mid_recv.mid = htonl(1 + ntohl(msg->mid.mid));
1267 }
1268 send_channel_data_ack(ch);
1269 return;
1270 }
1270 } 1271 }
1271 }
1272 1272
1273 if (GNUNET_YES == ch->reliable) 1273 if (GNUNET_YES == ch->reliable)
1274 {
1275 /* check if message ought to be dropped because it is ancient/too distant/duplicate */
1276 mid_min = ntohl (ch->mid_recv.mid);
1277 mid_max = mid_min + ch->max_pending_messages;
1278 mid_msg = ntohl (msg->mid.mid);
1279 if (((uint32_t) (mid_msg - mid_min) > ch->max_pending_messages) ||
1280 ((uint32_t) (mid_max - mid_msg) > ch->max_pending_messages))
1281 {
1282 LOG (GNUNET_ERROR_TYPE_DEBUG,
1283 "%s at %u drops ancient or far-future message %u\n",
1284 GCCH_2s (ch),
1285 (unsigned int) mid_min,
1286 ntohl (msg->mid.mid));
1287
1288 GNUNET_STATISTICS_update (stats,
1289 "# duplicate DATA (ancient or future)",
1290 1,
1291 GNUNET_NO);
1292 GNUNET_MQ_discard (env);
1293 send_channel_data_ack (ch);
1294 return;
1295 }
1296 /* mark bit for future ACKs */
1297 delta = mid_msg - mid_min - 1; /* overflow/underflow are OK here */
1298 if (delta < 64)
1299 { 1274 {
1300 if (0 != (ch->mid_futures & (1LLU << delta))) 1275 /* check if message ought to be dropped because it is ancient/too distant/duplicate */
1301 { 1276 mid_min = ntohl(ch->mid_recv.mid);
1302 /* Duplicate within the queue, drop also */ 1277 mid_max = mid_min + ch->max_pending_messages;
1303 LOG (GNUNET_ERROR_TYPE_DEBUG, 1278 mid_msg = ntohl(msg->mid.mid);
1304 "Duplicate payload of %u bytes on %s (mid %u) dropped\n", 1279 if (((uint32_t)(mid_msg - mid_min) > ch->max_pending_messages) ||
1305 (unsigned int) payload_size, 1280 ((uint32_t)(mid_max - mid_msg) > ch->max_pending_messages))
1306 GCCH_2s (ch), 1281 {
1307 ntohl (msg->mid.mid)); 1282 LOG(GNUNET_ERROR_TYPE_DEBUG,
1308 GNUNET_STATISTICS_update (stats, "# duplicate DATA", 1, GNUNET_NO); 1283 "%s at %u drops ancient or far-future message %u\n",
1309 GNUNET_MQ_discard (env); 1284 GCCH_2s(ch),
1310 send_channel_data_ack (ch); 1285 (unsigned int)mid_min,
1311 return; 1286 ntohl(msg->mid.mid));
1312 } 1287
1313 ch->mid_futures |= (1LLU << delta); 1288 GNUNET_STATISTICS_update(stats,
1314 LOG (GNUNET_ERROR_TYPE_DEBUG, 1289 "# duplicate DATA (ancient or future)",
1315 "Marked bit %llX for mid %u (base: %u); now: %llX\n", 1290 1,
1316 (1LLU << delta), 1291 GNUNET_NO);
1317 mid_msg, 1292 GNUNET_MQ_discard(env);
1318 mid_min, 1293 send_channel_data_ack(ch);
1319 ch->mid_futures); 1294 return;
1295 }
1296 /* mark bit for future ACKs */
1297 delta = mid_msg - mid_min - 1; /* overflow/underflow are OK here */
1298 if (delta < 64)
1299 {
1300 if (0 != (ch->mid_futures & (1LLU << delta)))
1301 {
1302 /* Duplicate within the queue, drop also */
1303 LOG(GNUNET_ERROR_TYPE_DEBUG,
1304 "Duplicate payload of %u bytes on %s (mid %u) dropped\n",
1305 (unsigned int)payload_size,
1306 GCCH_2s(ch),
1307 ntohl(msg->mid.mid));
1308 GNUNET_STATISTICS_update(stats, "# duplicate DATA", 1, GNUNET_NO);
1309 GNUNET_MQ_discard(env);
1310 send_channel_data_ack(ch);
1311 return;
1312 }
1313 ch->mid_futures |= (1LLU << delta);
1314 LOG(GNUNET_ERROR_TYPE_DEBUG,
1315 "Marked bit %llX for mid %u (base: %u); now: %llX\n",
1316 (1LLU << delta),
1317 mid_msg,
1318 mid_min,
1319 ch->mid_futures);
1320 }
1320 } 1321 }
1321 }
1322 else /* ! ch->reliable */ 1322 else /* ! ch->reliable */
1323 {
1324 struct CadetOutOfOrderMessage *next_msg;
1325
1326 /**
1327 * We always send if possible in this case.
1328 * It is guaranteed that the queued MID < received MID
1329 **/
1330 if ((NULL != ccc->head_recv) && (GNUNET_YES == ccc->client_ready))
1331 {
1332 next_msg = ccc->head_recv;
1333 LOG (GNUNET_ERROR_TYPE_DEBUG,
1334 "Giving queued MID %u from %s to client %s\n",
1335 ntohl (next_msg->mid.mid),
1336 GCCH_2s (ch),
1337 GSC_2s (ccc->c));
1338 ccc->client_ready = GNUNET_NO;
1339 GSC_send_to_client (ccc->c, next_msg->env);
1340 ch->mid_recv.mid = htonl (1 + ntohl (next_msg->mid.mid));
1341 ch->mid_futures >>= 1;
1342 send_channel_data_ack (ch);
1343 GNUNET_CONTAINER_DLL_remove (ccc->head_recv, ccc->tail_recv, next_msg);
1344 ccc->num_recv--;
1345 /* Do not process duplicate MID */
1346 if (msg->mid.mid == next_msg->mid.mid) /* Duplicate */
1347 {
1348 /* Duplicate within the queue, drop */
1349 LOG (GNUNET_ERROR_TYPE_DEBUG,
1350 "Message on %s (mid %u) dropped, duplicate\n",
1351 GCCH_2s (ch),
1352 ntohl (msg->mid.mid));
1353 GNUNET_free (next_msg);
1354 GNUNET_MQ_discard (env);
1355 return;
1356 }
1357 GNUNET_free (next_msg);
1358 }
1359
1360 if (ntohl (msg->mid.mid) < ntohl (ch->mid_recv.mid)) /* Old */
1361 {
1362 /* Duplicate within the queue, drop */
1363 LOG (GNUNET_ERROR_TYPE_DEBUG,
1364 "Message on %s (mid %u) dropped, old.\n",
1365 GCCH_2s (ch),
1366 ntohl (msg->mid.mid));
1367 GNUNET_MQ_discard (env);
1368 return;
1369 }
1370
1371 /* Channel is unreliable, so we do not ACK. But we also cannot
1372 allow buffering everything, so check if we have space... */
1373 if (ccc->num_recv >= ch->max_pending_messages)
1374 { 1323 {
1375 struct CadetOutOfOrderMessage *drop; 1324 struct CadetOutOfOrderMessage *next_msg;
1376 1325
1377 /* Yep, need to drop. Drop the oldest message in 1326 /**
1378 the buffer. */ 1327 * We always send if possible in this case.
1379 LOG (GNUNET_ERROR_TYPE_DEBUG, 1328 * It is guaranteed that the queued MID < received MID
1380 "Queue full due slow client on %s, dropping oldest message\n", 1329 **/
1381 GCCH_2s (ch)); 1330 if ((NULL != ccc->head_recv) && (GNUNET_YES == ccc->client_ready))
1382 GNUNET_STATISTICS_update (stats, 1331 {
1383 "# messages dropped due to slow client", 1332 next_msg = ccc->head_recv;
1384 1, 1333 LOG(GNUNET_ERROR_TYPE_DEBUG,
1385 GNUNET_NO); 1334 "Giving queued MID %u from %s to client %s\n",
1386 drop = ccc->head_recv; 1335 ntohl(next_msg->mid.mid),
1387 GNUNET_assert (NULL != drop); 1336 GCCH_2s(ch),
1388 GNUNET_CONTAINER_DLL_remove (ccc->head_recv, ccc->tail_recv, drop); 1337 GSC_2s(ccc->c));
1389 ccc->num_recv--; 1338 ccc->client_ready = GNUNET_NO;
1390 GNUNET_MQ_discard (drop->env); 1339 GSC_send_to_client(ccc->c, next_msg->env);
1391 GNUNET_free (drop); 1340 ch->mid_recv.mid = htonl(1 + ntohl(next_msg->mid.mid));
1341 ch->mid_futures >>= 1;
1342 send_channel_data_ack(ch);
1343 GNUNET_CONTAINER_DLL_remove(ccc->head_recv, ccc->tail_recv, next_msg);
1344 ccc->num_recv--;
1345 /* Do not process duplicate MID */
1346 if (msg->mid.mid == next_msg->mid.mid) /* Duplicate */
1347 {
1348 /* Duplicate within the queue, drop */
1349 LOG(GNUNET_ERROR_TYPE_DEBUG,
1350 "Message on %s (mid %u) dropped, duplicate\n",
1351 GCCH_2s(ch),
1352 ntohl(msg->mid.mid));
1353 GNUNET_free(next_msg);
1354 GNUNET_MQ_discard(env);
1355 return;
1356 }
1357 GNUNET_free(next_msg);
1358 }
1359
1360 if (ntohl(msg->mid.mid) < ntohl(ch->mid_recv.mid)) /* Old */
1361 {
1362 /* Duplicate within the queue, drop */
1363 LOG(GNUNET_ERROR_TYPE_DEBUG,
1364 "Message on %s (mid %u) dropped, old.\n",
1365 GCCH_2s(ch),
1366 ntohl(msg->mid.mid));
1367 GNUNET_MQ_discard(env);
1368 return;
1369 }
1370
1371 /* Channel is unreliable, so we do not ACK. But we also cannot
1372 allow buffering everything, so check if we have space... */
1373 if (ccc->num_recv >= ch->max_pending_messages)
1374 {
1375 struct CadetOutOfOrderMessage *drop;
1376
1377 /* Yep, need to drop. Drop the oldest message in
1378 the buffer. */
1379 LOG(GNUNET_ERROR_TYPE_DEBUG,
1380 "Queue full due slow client on %s, dropping oldest message\n",
1381 GCCH_2s(ch));
1382 GNUNET_STATISTICS_update(stats,
1383 "# messages dropped due to slow client",
1384 1,
1385 GNUNET_NO);
1386 drop = ccc->head_recv;
1387 GNUNET_assert(NULL != drop);
1388 GNUNET_CONTAINER_DLL_remove(ccc->head_recv, ccc->tail_recv, drop);
1389 ccc->num_recv--;
1390 GNUNET_MQ_discard(drop->env);
1391 GNUNET_free(drop);
1392 }
1392 } 1393 }
1393 }
1394 1394
1395 /* Insert message into sorted out-of-order queue */ 1395 /* Insert message into sorted out-of-order queue */
1396 com = GNUNET_new (struct CadetOutOfOrderMessage); 1396 com = GNUNET_new(struct CadetOutOfOrderMessage);
1397 com->mid = msg->mid; 1397 com->mid = msg->mid;
1398 com->env = env; 1398 com->env = env;
1399 duplicate = GNUNET_NO; 1399 duplicate = GNUNET_NO;
1400 GNUNET_CONTAINER_DLL_insert_sorted (struct CadetOutOfOrderMessage, 1400 GNUNET_CONTAINER_DLL_insert_sorted(struct CadetOutOfOrderMessage,
1401 is_before, 1401 is_before,
1402 &duplicate, 1402 &duplicate,
1403 ccc->head_recv, 1403 ccc->head_recv,
1404 ccc->tail_recv, 1404 ccc->tail_recv,
1405 com); 1405 com);
1406 ccc->num_recv++; 1406 ccc->num_recv++;
1407 if (GNUNET_YES == duplicate) 1407 if (GNUNET_YES == duplicate)
1408 { 1408 {
1409 /* Duplicate within the queue, drop also (this is not covered by 1409 /* Duplicate within the queue, drop also (this is not covered by
1410 the case above if "delta" >= 64, which could be the case if 1410 the case above if "delta" >= 64, which could be the case if
1411 max_pending_messages is also >= 64 or if our client is unready 1411 max_pending_messages is also >= 64 or if our client is unready
1412 and we are seeing retransmissions of the message our client is 1412 and we are seeing retransmissions of the message our client is
1413 blocked on. */ 1413 blocked on. */
1414 LOG (GNUNET_ERROR_TYPE_DEBUG, 1414 LOG(GNUNET_ERROR_TYPE_DEBUG,
1415 "Duplicate payload of %u bytes on %s (mid %u) dropped\n", 1415 "Duplicate payload of %u bytes on %s (mid %u) dropped\n",
1416 (unsigned int) payload_size, 1416 (unsigned int)payload_size,
1417 GCCH_2s (ch), 1417 GCCH_2s(ch),
1418 ntohl (msg->mid.mid)); 1418 ntohl(msg->mid.mid));
1419 GNUNET_STATISTICS_update (stats, "# duplicate DATA", 1, GNUNET_NO); 1419 GNUNET_STATISTICS_update(stats, "# duplicate DATA", 1, GNUNET_NO);
1420 GNUNET_CONTAINER_DLL_remove (ccc->head_recv, ccc->tail_recv, com); 1420 GNUNET_CONTAINER_DLL_remove(ccc->head_recv, ccc->tail_recv, com);
1421 ccc->num_recv--; 1421 ccc->num_recv--;
1422 GNUNET_MQ_discard (com->env); 1422 GNUNET_MQ_discard(com->env);
1423 GNUNET_free (com); 1423 GNUNET_free(com);
1424 send_channel_data_ack (ch); 1424 send_channel_data_ack(ch);
1425 return; 1425 return;
1426 } 1426 }
1427 LOG (GNUNET_ERROR_TYPE_DEBUG, 1427 LOG(GNUNET_ERROR_TYPE_DEBUG,
1428 "Queued %s payload of %u bytes on %s-%X(%p) (mid %u, need %u first)\n", 1428 "Queued %s payload of %u bytes on %s-%X(%p) (mid %u, need %u first)\n",
1429 (GNUNET_YES == ccc->client_ready) ? "out-of-order" : "client-not-ready", 1429 (GNUNET_YES == ccc->client_ready) ? "out-of-order" : "client-not-ready",
1430 (unsigned int) payload_size, 1430 (unsigned int)payload_size,
1431 GCCH_2s (ch), 1431 GCCH_2s(ch),
1432 ntohl (ccc->ccn.channel_of_client), 1432 ntohl(ccc->ccn.channel_of_client),
1433 ccc, 1433 ccc,
1434 ntohl (msg->mid.mid), 1434 ntohl(msg->mid.mid),
1435 ntohl (ch->mid_recv.mid)); 1435 ntohl(ch->mid_recv.mid));
1436 /* NOTE: this ACK we _could_ skip, as the packet is out-of-order and 1436 /* NOTE: this ACK we _could_ skip, as the packet is out-of-order and
1437 the sender may already be transmitting the previous one. Needs 1437 the sender may already be transmitting the previous one. Needs
1438 experimental evaluation to see if/when this ACK helps or 1438 experimental evaluation to see if/when this ACK helps or
1439 hurts. (We might even want another option.) */ 1439 hurts. (We might even want another option.) */
1440 send_channel_data_ack (ch); 1440 send_channel_data_ack(ch);
1441} 1441}
1442 1442
1443 1443
@@ -1452,8 +1452,8 @@ GCCH_handle_channel_plaintext_data (
1452 * if transmission failed 1452 * if transmission failed
1453 */ 1453 */
1454static void 1454static void
1455data_sent_cb (void *cls, 1455data_sent_cb(void *cls,
1456 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid); 1456 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid);
1457 1457
1458 1458
1459/** 1459/**
@@ -1463,19 +1463,19 @@ data_sent_cb (void *cls,
1463 * @param cls the `struct CadetChannel` where we need to retransmit 1463 * @param cls the `struct CadetChannel` where we need to retransmit
1464 */ 1464 */
1465static void 1465static void
1466retry_transmission (void *cls) 1466retry_transmission(void *cls)
1467{ 1467{
1468 struct CadetChannel *ch = cls; 1468 struct CadetChannel *ch = cls;
1469 struct CadetReliableMessage *crm = ch->head_sent; 1469 struct CadetReliableMessage *crm = ch->head_sent;
1470 1470
1471 ch->retry_data_task = NULL; 1471 ch->retry_data_task = NULL;
1472 GNUNET_assert (NULL == crm->qe); 1472 GNUNET_assert(NULL == crm->qe);
1473 LOG (GNUNET_ERROR_TYPE_DEBUG, 1473 LOG(GNUNET_ERROR_TYPE_DEBUG,
1474 "Retrying transmission on %s of message %u\n", 1474 "Retrying transmission on %s of message %u\n",
1475 GCCH_2s (ch), 1475 GCCH_2s(ch),
1476 (unsigned int) ntohl (crm->data_message->mid.mid)); 1476 (unsigned int)ntohl(crm->data_message->mid.mid));
1477 crm->qe = GCT_send (ch->t, &crm->data_message->header, &data_sent_cb, crm); 1477 crm->qe = GCT_send(ch->t, &crm->data_message->header, &data_sent_cb, crm);
1478 GNUNET_assert (NULL == ch->retry_data_task); 1478 GNUNET_assert(NULL == ch->retry_data_task);
1479} 1479}
1480 1480
1481 1481
@@ -1488,36 +1488,36 @@ retry_transmission (void *cls)
1488 * @param crm the message that got acknowledged 1488 * @param crm the message that got acknowledged
1489 */ 1489 */
1490static void 1490static void
1491handle_matching_ack (struct CadetChannel *ch, 1491handle_matching_ack(struct CadetChannel *ch,
1492 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti, 1492 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1493 struct CadetReliableMessage *crm) 1493 struct CadetReliableMessage *crm)
1494{ 1494{
1495 GNUNET_CONTAINER_DLL_remove (ch->head_sent, ch->tail_sent, crm); 1495 GNUNET_CONTAINER_DLL_remove(ch->head_sent, ch->tail_sent, crm);
1496 ch->pending_messages--; 1496 ch->pending_messages--;
1497 GNUNET_assert (ch->pending_messages < ch->max_pending_messages); 1497 GNUNET_assert(ch->pending_messages < ch->max_pending_messages);
1498 LOG (GNUNET_ERROR_TYPE_DEBUG, 1498 LOG(GNUNET_ERROR_TYPE_DEBUG,
1499 "Received DATA_ACK on %s for message %u (%u ACKs pending)\n", 1499 "Received DATA_ACK on %s for message %u (%u ACKs pending)\n",
1500 GCCH_2s (ch), 1500 GCCH_2s(ch),
1501 (unsigned int) ntohl (crm->data_message->mid.mid), 1501 (unsigned int)ntohl(crm->data_message->mid.mid),
1502 ch->pending_messages); 1502 ch->pending_messages);
1503 if (NULL != crm->qe) 1503 if (NULL != crm->qe)
1504 { 1504 {
1505 GCT_send_cancel (crm->qe); 1505 GCT_send_cancel(crm->qe);
1506 crm->qe = NULL; 1506 crm->qe = NULL;
1507 } 1507 }
1508 if ((1 == crm->num_transmissions) && (NULL != cti)) 1508 if ((1 == crm->num_transmissions) && (NULL != cti))
1509 {
1510 GCC_ack_observed (cti);
1511 if (0 == GNUNET_memcmp (cti, &crm->connection_taken))
1512 { 1509 {
1513 GCC_latency_observed (cti, 1510 GCC_ack_observed(cti);
1514 GNUNET_TIME_absolute_get_duration ( 1511 if (0 == GNUNET_memcmp(cti, &crm->connection_taken))
1515 crm->first_transmission_time)); 1512 {
1513 GCC_latency_observed(cti,
1514 GNUNET_TIME_absolute_get_duration(
1515 crm->first_transmission_time));
1516 }
1516 } 1517 }
1517 } 1518 GNUNET_free(crm->data_message);
1518 GNUNET_free (crm->data_message); 1519 GNUNET_free(crm);
1519 GNUNET_free (crm); 1520 send_ack_to_client(ch, (NULL == ch->owner) ? GNUNET_NO : GNUNET_YES);
1520 send_ack_to_client (ch, (NULL == ch->owner) ? GNUNET_NO : GNUNET_YES);
1521} 1521}
1522 1522
1523 1523
@@ -1530,7 +1530,7 @@ handle_matching_ack (struct CadetChannel *ch,
1530 * @param ack details about what was received 1530 * @param ack details about what was received
1531 */ 1531 */
1532void 1532void
1533GCCH_handle_channel_plaintext_data_ack ( 1533GCCH_handle_channel_plaintext_data_ack(
1534 struct CadetChannel *ch, 1534 struct CadetChannel *ch,
1535 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti, 1535 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
1536 const struct GNUNET_CADET_ChannelDataAckMessage *ack) 1536 const struct GNUNET_CADET_ChannelDataAckMessage *ack)
@@ -1542,72 +1542,72 @@ GCCH_handle_channel_plaintext_data_ack (
1542 uint64_t mid_mask; 1542 uint64_t mid_mask;
1543 unsigned int delta; 1543 unsigned int delta;
1544 1544
1545 GNUNET_break (GNUNET_NO == ch->is_loopback); 1545 GNUNET_break(GNUNET_NO == ch->is_loopback);
1546 if (GNUNET_NO == ch->reliable) 1546 if (GNUNET_NO == ch->reliable)
1547 { 1547 {
1548 /* not expecting ACKs on unreliable channel, odd */ 1548 /* not expecting ACKs on unreliable channel, odd */
1549 GNUNET_break_op (0); 1549 GNUNET_break_op(0);
1550 return; 1550 return;
1551 } 1551 }
1552 /* mid_base is the MID of the next message that the 1552 /* mid_base is the MID of the next message that the
1553 other peer expects (i.e. that is missing!), everything 1553 other peer expects (i.e. that is missing!), everything
1554 LOWER (but excluding mid_base itself) was received. */ 1554 LOWER (but excluding mid_base itself) was received. */
1555 mid_base = ntohl (ack->mid.mid); 1555 mid_base = ntohl(ack->mid.mid);
1556 mid_mask = GNUNET_htonll (ack->futures); 1556 mid_mask = GNUNET_htonll(ack->futures);
1557 found = GNUNET_NO; 1557 found = GNUNET_NO;
1558 for (crm = ch->head_sent; NULL != crm; crm = crmn) 1558 for (crm = ch->head_sent; NULL != crm; crm = crmn)
1559 {
1560 crmn = crm->next;
1561 delta = (unsigned int) (ntohl (crm->data_message->mid.mid) - mid_base);
1562 if (delta >= UINT_MAX - ch->max_pending_messages)
1563 { 1559 {
1564 /* overflow, means crm was a bit in the past, so this ACK counts for it. */ 1560 crmn = crm->next;
1565 LOG (GNUNET_ERROR_TYPE_DEBUG, 1561 delta = (unsigned int)(ntohl(crm->data_message->mid.mid) - mid_base);
1566 "Got DATA_ACK with base %u satisfying past message %u on %s\n", 1562 if (delta >= UINT_MAX - ch->max_pending_messages)
1567 (unsigned int) mid_base, 1563 {
1568 ntohl (crm->data_message->mid.mid), 1564 /* overflow, means crm was a bit in the past, so this ACK counts for it. */
1569 GCCH_2s (ch)); 1565 LOG(GNUNET_ERROR_TYPE_DEBUG,
1570 handle_matching_ack (ch, cti, crm); 1566 "Got DATA_ACK with base %u satisfying past message %u on %s\n",
1571 found = GNUNET_YES; 1567 (unsigned int)mid_base,
1572 continue; 1568 ntohl(crm->data_message->mid.mid),
1569 GCCH_2s(ch));
1570 handle_matching_ack(ch, cti, crm);
1571 found = GNUNET_YES;
1572 continue;
1573 }
1574 delta--;
1575 if (delta >= 64)
1576 continue;
1577 LOG(GNUNET_ERROR_TYPE_DEBUG,
1578 "Testing bit %llX for mid %u (base: %u)\n",
1579 (1LLU << delta),
1580 ntohl(crm->data_message->mid.mid),
1581 mid_base);
1582 if (0 != (mid_mask & (1LLU << delta)))
1583 {
1584 LOG(GNUNET_ERROR_TYPE_DEBUG,
1585 "Got DATA_ACK with mask for %u on %s\n",
1586 ntohl(crm->data_message->mid.mid),
1587 GCCH_2s(ch));
1588 handle_matching_ack(ch, cti, crm);
1589 found = GNUNET_YES;
1590 }
1573 } 1591 }
1574 delta--; 1592 if (GNUNET_NO == found)
1575 if (delta >= 64)
1576 continue;
1577 LOG (GNUNET_ERROR_TYPE_DEBUG,
1578 "Testing bit %llX for mid %u (base: %u)\n",
1579 (1LLU << delta),
1580 ntohl (crm->data_message->mid.mid),
1581 mid_base);
1582 if (0 != (mid_mask & (1LLU << delta)))
1583 { 1593 {
1584 LOG (GNUNET_ERROR_TYPE_DEBUG, 1594 /* ACK for message we already dropped, might have been a
1585 "Got DATA_ACK with mask for %u on %s\n", 1595 duplicate ACK? Ignore. */
1586 ntohl (crm->data_message->mid.mid), 1596 LOG(GNUNET_ERROR_TYPE_DEBUG,
1587 GCCH_2s (ch)); 1597 "Duplicate DATA_ACK on %s, ignoring\n",
1588 handle_matching_ack (ch, cti, crm); 1598 GCCH_2s(ch));
1589 found = GNUNET_YES; 1599 GNUNET_STATISTICS_update(stats, "# duplicate DATA_ACKs", 1, GNUNET_NO);
1600 return;
1590 } 1601 }
1591 }
1592 if (GNUNET_NO == found)
1593 {
1594 /* ACK for message we already dropped, might have been a
1595 duplicate ACK? Ignore. */
1596 LOG (GNUNET_ERROR_TYPE_DEBUG,
1597 "Duplicate DATA_ACK on %s, ignoring\n",
1598 GCCH_2s (ch));
1599 GNUNET_STATISTICS_update (stats, "# duplicate DATA_ACKs", 1, GNUNET_NO);
1600 return;
1601 }
1602 if (NULL != ch->retry_data_task) 1602 if (NULL != ch->retry_data_task)
1603 { 1603 {
1604 GNUNET_SCHEDULER_cancel (ch->retry_data_task); 1604 GNUNET_SCHEDULER_cancel(ch->retry_data_task);
1605 ch->retry_data_task = NULL; 1605 ch->retry_data_task = NULL;
1606 } 1606 }
1607 if ((NULL != ch->head_sent) && (NULL == ch->head_sent->qe)) 1607 if ((NULL != ch->head_sent) && (NULL == ch->head_sent->qe))
1608 ch->retry_data_task = GNUNET_SCHEDULER_add_at (ch->head_sent->next_retry, 1608 ch->retry_data_task = GNUNET_SCHEDULER_add_at(ch->head_sent->next_retry,
1609 &retry_transmission, 1609 &retry_transmission,
1610 ch); 1610 ch);
1611} 1611}
1612 1612
1613 1613
@@ -1621,34 +1621,34 @@ GCCH_handle_channel_plaintext_data_ack (
1621 * NULL if we are simulating receiving a destroy due to shutdown 1621 * NULL if we are simulating receiving a destroy due to shutdown
1622 */ 1622 */
1623void 1623void
1624GCCH_handle_remote_destroy ( 1624GCCH_handle_remote_destroy(
1625 struct CadetChannel *ch, 1625 struct CadetChannel *ch,
1626 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti) 1626 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti)
1627{ 1627{
1628 struct CadetChannelClient *ccc; 1628 struct CadetChannelClient *ccc;
1629 1629
1630 GNUNET_assert (GNUNET_NO == ch->is_loopback); 1630 GNUNET_assert(GNUNET_NO == ch->is_loopback);
1631 LOG (GNUNET_ERROR_TYPE_DEBUG, 1631 LOG(GNUNET_ERROR_TYPE_DEBUG,
1632 "Received remote channel DESTROY for %s\n", 1632 "Received remote channel DESTROY for %s\n",
1633 GCCH_2s (ch)); 1633 GCCH_2s(ch));
1634 if (GNUNET_YES == ch->destroy) 1634 if (GNUNET_YES == ch->destroy)
1635 { 1635 {
1636 /* Local client already gone, this is instant-death. */ 1636 /* Local client already gone, this is instant-death. */
1637 channel_destroy (ch); 1637 channel_destroy(ch);
1638 return; 1638 return;
1639 } 1639 }
1640 ccc = (NULL != ch->owner) ? ch->owner : ch->dest; 1640 ccc = (NULL != ch->owner) ? ch->owner : ch->dest;
1641 if ((NULL != ccc) && (NULL != ccc->head_recv)) 1641 if ((NULL != ccc) && (NULL != ccc->head_recv))
1642 { 1642 {
1643 LOG (GNUNET_ERROR_TYPE_WARNING, 1643 LOG(GNUNET_ERROR_TYPE_WARNING,
1644 "Lost end of transmission due to remote shutdown on %s\n", 1644 "Lost end of transmission due to remote shutdown on %s\n",
1645 GCCH_2s (ch)); 1645 GCCH_2s(ch));
1646 /* FIXME: change API to notify client about truncated transmission! */ 1646 /* FIXME: change API to notify client about truncated transmission! */
1647 } 1647 }
1648 ch->destroy = GNUNET_YES; 1648 ch->destroy = GNUNET_YES;
1649 if (NULL != ccc) 1649 if (NULL != ccc)
1650 GSC_handle_remote_channel_destroy (ccc->c, ccc->ccn, ch); 1650 GSC_handle_remote_channel_destroy(ccc->c, ccc->ccn, ch);
1651 channel_destroy (ch); 1651 channel_destroy(ch);
1652} 1652}
1653 1653
1654 1654
@@ -1661,9 +1661,9 @@ GCCH_handle_remote_destroy (
1661 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO 1661 * @return #GNUNET_YES if @e1 < @e2, otherwise #GNUNET_NO
1662 */ 1662 */
1663static int 1663static int
1664cmp_crm_by_next_retry (void *cls, 1664cmp_crm_by_next_retry(void *cls,
1665 struct CadetReliableMessage *crm1, 1665 struct CadetReliableMessage *crm1,
1666 struct CadetReliableMessage *crm2) 1666 struct CadetReliableMessage *crm2)
1667{ 1667{
1668 if (crm1->next_retry.abs_value_us < crm2->next_retry.abs_value_us) 1668 if (crm1->next_retry.abs_value_us < crm2->next_retry.abs_value_us)
1669 return GNUNET_YES; 1669 return GNUNET_YES;
@@ -1682,75 +1682,75 @@ cmp_crm_by_next_retry (void *cls,
1682 * if transmission failed 1682 * if transmission failed
1683 */ 1683 */
1684static void 1684static void
1685data_sent_cb (void *cls, 1685data_sent_cb(void *cls,
1686 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid) 1686 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
1687{ 1687{
1688 struct CadetReliableMessage *crm = cls; 1688 struct CadetReliableMessage *crm = cls;
1689 struct CadetChannel *ch = crm->ch; 1689 struct CadetChannel *ch = crm->ch;
1690 1690
1691 GNUNET_assert (GNUNET_NO == ch->is_loopback); 1691 GNUNET_assert(GNUNET_NO == ch->is_loopback);
1692 GNUNET_assert (NULL != crm->qe); 1692 GNUNET_assert(NULL != crm->qe);
1693 crm->qe = NULL; 1693 crm->qe = NULL;
1694 GNUNET_CONTAINER_DLL_remove (ch->head_sent, ch->tail_sent, crm); 1694 GNUNET_CONTAINER_DLL_remove(ch->head_sent, ch->tail_sent, crm);
1695 if (GNUNET_NO == ch->reliable) 1695 if (GNUNET_NO == ch->reliable)
1696 { 1696 {
1697 GNUNET_free (crm->data_message); 1697 GNUNET_free(crm->data_message);
1698 GNUNET_free (crm); 1698 GNUNET_free(crm);
1699 ch->pending_messages--; 1699 ch->pending_messages--;
1700 send_ack_to_client (ch, (NULL == ch->owner) ? GNUNET_NO : GNUNET_YES); 1700 send_ack_to_client(ch, (NULL == ch->owner) ? GNUNET_NO : GNUNET_YES);
1701 return; 1701 return;
1702 } 1702 }
1703 if (NULL == cid) 1703 if (NULL == cid)
1704 { 1704 {
1705 /* There was an error sending. */ 1705 /* There was an error sending. */
1706 crm->num_transmissions = GNUNET_SYSERR; 1706 crm->num_transmissions = GNUNET_SYSERR;
1707 } 1707 }
1708 else if (GNUNET_SYSERR != crm->num_transmissions) 1708 else if (GNUNET_SYSERR != crm->num_transmissions)
1709 {
1710 /* Increment transmission counter, and possibly store @a cid
1711 if this was the first transmission. */
1712 crm->num_transmissions++;
1713 if (1 == crm->num_transmissions)
1714 { 1709 {
1715 crm->first_transmission_time = GNUNET_TIME_absolute_get (); 1710 /* Increment transmission counter, and possibly store @a cid
1716 crm->connection_taken = *cid; 1711 if this was the first transmission. */
1717 GCC_ack_expected (cid); 1712 crm->num_transmissions++;
1713 if (1 == crm->num_transmissions)
1714 {
1715 crm->first_transmission_time = GNUNET_TIME_absolute_get();
1716 crm->connection_taken = *cid;
1717 GCC_ack_expected(cid);
1718 }
1718 } 1719 }
1719 }
1720 if ((0 == crm->retry_delay.rel_value_us) && (NULL != cid)) 1720 if ((0 == crm->retry_delay.rel_value_us) && (NULL != cid))
1721 { 1721 {
1722 struct CadetConnection *cc = GCC_lookup (cid); 1722 struct CadetConnection *cc = GCC_lookup(cid);
1723 1723
1724 if (NULL != cc) 1724 if (NULL != cc)
1725 crm->retry_delay = GCC_get_metrics (cc)->aged_latency; 1725 crm->retry_delay = GCC_get_metrics(cc)->aged_latency;
1726 else 1726 else
1727 crm->retry_delay = ch->retry_time; 1727 crm->retry_delay = ch->retry_time;
1728 } 1728 }
1729 crm->retry_delay = GNUNET_TIME_STD_BACKOFF (crm->retry_delay); 1729 crm->retry_delay = GNUNET_TIME_STD_BACKOFF(crm->retry_delay);
1730 crm->retry_delay = GNUNET_TIME_relative_max (crm->retry_delay, MIN_RTT_DELAY); 1730 crm->retry_delay = GNUNET_TIME_relative_max(crm->retry_delay, MIN_RTT_DELAY);
1731 crm->next_retry = GNUNET_TIME_relative_to_absolute (crm->retry_delay); 1731 crm->next_retry = GNUNET_TIME_relative_to_absolute(crm->retry_delay);
1732 1732
1733 GNUNET_CONTAINER_DLL_insert_sorted (struct CadetReliableMessage, 1733 GNUNET_CONTAINER_DLL_insert_sorted(struct CadetReliableMessage,
1734 cmp_crm_by_next_retry, 1734 cmp_crm_by_next_retry,
1735 NULL, 1735 NULL,
1736 ch->head_sent, 1736 ch->head_sent,
1737 ch->tail_sent, 1737 ch->tail_sent,
1738 crm); 1738 crm);
1739 LOG (GNUNET_ERROR_TYPE_DEBUG, 1739 LOG(GNUNET_ERROR_TYPE_DEBUG,
1740 "Message %u sent, next transmission on %s in %s\n", 1740 "Message %u sent, next transmission on %s in %s\n",
1741 (unsigned int) ntohl (crm->data_message->mid.mid), 1741 (unsigned int)ntohl(crm->data_message->mid.mid),
1742 GCCH_2s (ch), 1742 GCCH_2s(ch),
1743 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining ( 1743 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_remaining(
1744 ch->head_sent->next_retry), 1744 ch->head_sent->next_retry),
1745 GNUNET_YES)); 1745 GNUNET_YES));
1746 if (NULL == ch->head_sent->qe) 1746 if (NULL == ch->head_sent->qe)
1747 { 1747 {
1748 if (NULL != ch->retry_data_task) 1748 if (NULL != ch->retry_data_task)
1749 GNUNET_SCHEDULER_cancel (ch->retry_data_task); 1749 GNUNET_SCHEDULER_cancel(ch->retry_data_task);
1750 ch->retry_data_task = GNUNET_SCHEDULER_add_at (ch->head_sent->next_retry, 1750 ch->retry_data_task = GNUNET_SCHEDULER_add_at(ch->head_sent->next_retry,
1751 &retry_transmission, 1751 &retry_transmission,
1752 ch); 1752 ch);
1753 } 1753 }
1754} 1754}
1755 1755
1756 1756
@@ -1769,100 +1769,100 @@ data_sent_cb (void *cls,
1769 * #GNUNET_SYSERR in case of an error. 1769 * #GNUNET_SYSERR in case of an error.
1770 */ 1770 */
1771int 1771int
1772GCCH_handle_local_data (struct CadetChannel *ch, 1772GCCH_handle_local_data(struct CadetChannel *ch,
1773 struct GNUNET_CADET_ClientChannelNumber sender_ccn, 1773 struct GNUNET_CADET_ClientChannelNumber sender_ccn,
1774 const char *buf, 1774 const char *buf,
1775 size_t buf_len) 1775 size_t buf_len)
1776{ 1776{
1777 struct CadetReliableMessage *crm; 1777 struct CadetReliableMessage *crm;
1778 1778
1779 if (ch->pending_messages >= ch->max_pending_messages) 1779 if (ch->pending_messages >= ch->max_pending_messages)
1780 {
1781 GNUNET_break (0); /* Fails: #5370 */
1782 return GNUNET_SYSERR;
1783 }
1784 if (GNUNET_YES == ch->destroy)
1785 {
1786 /* we are going down, drop messages */
1787 return GNUNET_OK;
1788 }
1789 ch->pending_messages++;
1790
1791 if (GNUNET_YES == ch->is_loopback)
1792 {
1793 struct CadetChannelClient *receiver;
1794 struct GNUNET_MQ_Envelope *env;
1795 struct GNUNET_CADET_LocalData *ld;
1796 int ack_to_owner;
1797
1798 env =
1799 GNUNET_MQ_msg_extra (ld, buf_len, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1800 if ((NULL != ch->owner) &&
1801 (sender_ccn.channel_of_client == ch->owner->ccn.channel_of_client))
1802 {
1803 receiver = ch->dest;
1804 ack_to_owner = GNUNET_YES;
1805 }
1806 else if ((NULL != ch->dest) &&
1807 (sender_ccn.channel_of_client == ch->dest->ccn.channel_of_client))
1808 {
1809 receiver = ch->owner;
1810 ack_to_owner = GNUNET_NO;
1811 }
1812 else
1813 { 1780 {
1814 GNUNET_break (0); 1781 GNUNET_break(0); /* Fails: #5370 */
1815 return GNUNET_SYSERR; 1782 return GNUNET_SYSERR;
1816 } 1783 }
1817 GNUNET_assert (NULL != receiver); 1784 if (GNUNET_YES == ch->destroy)
1818 ld->ccn = receiver->ccn;
1819 GNUNET_memcpy (&ld[1], buf, buf_len);
1820 if (GNUNET_YES == receiver->client_ready)
1821 { 1785 {
1822 ch->pending_messages--; 1786 /* we are going down, drop messages */
1823 GSC_send_to_client (receiver->c, env); 1787 return GNUNET_OK;
1824 send_ack_to_client (ch, ack_to_owner);
1825 } 1788 }
1826 else 1789 ch->pending_messages++;
1790
1791 if (GNUNET_YES == ch->is_loopback)
1827 { 1792 {
1828 struct CadetOutOfOrderMessage *oom; 1793 struct CadetChannelClient *receiver;
1829 1794 struct GNUNET_MQ_Envelope *env;
1830 oom = GNUNET_new (struct CadetOutOfOrderMessage); 1795 struct GNUNET_CADET_LocalData *ld;
1831 oom->env = env; 1796 int ack_to_owner;
1832 GNUNET_CONTAINER_DLL_insert_tail (receiver->head_recv, 1797
1833 receiver->tail_recv, 1798 env =
1834 oom); 1799 GNUNET_MQ_msg_extra(ld, buf_len, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1835 receiver->num_recv++; 1800 if ((NULL != ch->owner) &&
1801 (sender_ccn.channel_of_client == ch->owner->ccn.channel_of_client))
1802 {
1803 receiver = ch->dest;
1804 ack_to_owner = GNUNET_YES;
1805 }
1806 else if ((NULL != ch->dest) &&
1807 (sender_ccn.channel_of_client == ch->dest->ccn.channel_of_client))
1808 {
1809 receiver = ch->owner;
1810 ack_to_owner = GNUNET_NO;
1811 }
1812 else
1813 {
1814 GNUNET_break(0);
1815 return GNUNET_SYSERR;
1816 }
1817 GNUNET_assert(NULL != receiver);
1818 ld->ccn = receiver->ccn;
1819 GNUNET_memcpy(&ld[1], buf, buf_len);
1820 if (GNUNET_YES == receiver->client_ready)
1821 {
1822 ch->pending_messages--;
1823 GSC_send_to_client(receiver->c, env);
1824 send_ack_to_client(ch, ack_to_owner);
1825 }
1826 else
1827 {
1828 struct CadetOutOfOrderMessage *oom;
1829
1830 oom = GNUNET_new(struct CadetOutOfOrderMessage);
1831 oom->env = env;
1832 GNUNET_CONTAINER_DLL_insert_tail(receiver->head_recv,
1833 receiver->tail_recv,
1834 oom);
1835 receiver->num_recv++;
1836 }
1837 return GNUNET_OK;
1836 } 1838 }
1837 return GNUNET_OK;
1838 }
1839 1839
1840 /* Everything is correct, send the message. */ 1840 /* Everything is correct, send the message. */
1841 crm = GNUNET_malloc (sizeof (*crm)); 1841 crm = GNUNET_malloc(sizeof(*crm));
1842 crm->ch = ch; 1842 crm->ch = ch;
1843 crm->data_message = GNUNET_malloc ( 1843 crm->data_message = GNUNET_malloc(
1844 sizeof (struct GNUNET_CADET_ChannelAppDataMessage) + buf_len); 1844 sizeof(struct GNUNET_CADET_ChannelAppDataMessage) + buf_len);
1845 crm->data_message->header.size = 1845 crm->data_message->header.size =
1846 htons (sizeof (struct GNUNET_CADET_ChannelAppDataMessage) + buf_len); 1846 htons(sizeof(struct GNUNET_CADET_ChannelAppDataMessage) + buf_len);
1847 crm->data_message->header.type = 1847 crm->data_message->header.type =
1848 htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA); 1848 htons(GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA);
1849 ch->mid_send.mid = htonl (ntohl (ch->mid_send.mid) + 1); 1849 ch->mid_send.mid = htonl(ntohl(ch->mid_send.mid) + 1);
1850 crm->data_message->mid = ch->mid_send; 1850 crm->data_message->mid = ch->mid_send;
1851 crm->data_message->ctn = ch->ctn; 1851 crm->data_message->ctn = ch->ctn;
1852 GNUNET_memcpy (&crm->data_message[1], buf, buf_len); 1852 GNUNET_memcpy(&crm->data_message[1], buf, buf_len);
1853 GNUNET_CONTAINER_DLL_insert_tail (ch->head_sent, ch->tail_sent, crm); 1853 GNUNET_CONTAINER_DLL_insert_tail(ch->head_sent, ch->tail_sent, crm);
1854 LOG (GNUNET_ERROR_TYPE_DEBUG, 1854 LOG(GNUNET_ERROR_TYPE_DEBUG,
1855 "Sending message %u from local client to %s with %u bytes\n", 1855 "Sending message %u from local client to %s with %u bytes\n",
1856 ntohl (crm->data_message->mid.mid), 1856 ntohl(crm->data_message->mid.mid),
1857 GCCH_2s (ch), 1857 GCCH_2s(ch),
1858 buf_len); 1858 buf_len);
1859 if (NULL != ch->retry_data_task) 1859 if (NULL != ch->retry_data_task)
1860 { 1860 {
1861 GNUNET_SCHEDULER_cancel (ch->retry_data_task); 1861 GNUNET_SCHEDULER_cancel(ch->retry_data_task);
1862 ch->retry_data_task = NULL; 1862 ch->retry_data_task = NULL;
1863 } 1863 }
1864 crm->qe = GCT_send (ch->t, &crm->data_message->header, &data_sent_cb, crm); 1864 crm->qe = GCT_send(ch->t, &crm->data_message->header, &data_sent_cb, crm);
1865 GNUNET_assert (NULL == ch->retry_data_task); 1865 GNUNET_assert(NULL == ch->retry_data_task);
1866 return GNUNET_OK; 1866 return GNUNET_OK;
1867} 1867}
1868 1868
@@ -1875,8 +1875,8 @@ GCCH_handle_local_data (struct CadetChannel *ch,
1875 * @param client_ccn ccn of the client sending the ack 1875 * @param client_ccn ccn of the client sending the ack
1876 */ 1876 */
1877void 1877void
1878GCCH_handle_local_ack (struct CadetChannel *ch, 1878GCCH_handle_local_ack(struct CadetChannel *ch,
1879 struct GNUNET_CADET_ClientChannelNumber client_ccn) 1879 struct GNUNET_CADET_ClientChannelNumber client_ccn)
1880{ 1880{
1881 struct CadetChannelClient *ccc; 1881 struct CadetChannelClient *ccc;
1882 struct CadetOutOfOrderMessage *com; 1882 struct CadetOutOfOrderMessage *com;
@@ -1888,87 +1888,87 @@ GCCH_handle_local_ack (struct CadetChannel *ch,
1888 (ch->dest->ccn.channel_of_client == client_ccn.channel_of_client)) 1888 (ch->dest->ccn.channel_of_client == client_ccn.channel_of_client))
1889 ccc = ch->dest; 1889 ccc = ch->dest;
1890 else 1890 else
1891 GNUNET_assert (0); 1891 GNUNET_assert(0);
1892 ccc->client_ready = GNUNET_YES; 1892 ccc->client_ready = GNUNET_YES;
1893 com = ccc->head_recv; 1893 com = ccc->head_recv;
1894 if (NULL == com) 1894 if (NULL == com)
1895 {
1896 LOG (GNUNET_ERROR_TYPE_DEBUG,
1897 "Got LOCAL_ACK, %s-%X ready to receive more data, but none pending on %s-%X(%p)!\n",
1898 GSC_2s (ccc->c),
1899 ntohl (client_ccn.channel_of_client),
1900 GCCH_2s (ch),
1901 ntohl (ccc->ccn.channel_of_client),
1902 ccc);
1903 return; /* none pending */
1904 }
1905 if (GNUNET_YES == ch->is_loopback)
1906 {
1907 int to_owner;
1908
1909 /* Messages are always in-order, just send */
1910 GNUNET_CONTAINER_DLL_remove (ccc->head_recv, ccc->tail_recv, com);
1911 ccc->num_recv--;
1912 GSC_send_to_client (ccc->c, com->env);
1913 /* Notify sender that we can receive more */
1914 if ((NULL != ch->owner) &&
1915 (ccc->ccn.channel_of_client == ch->owner->ccn.channel_of_client))
1916 { 1895 {
1917 to_owner = GNUNET_NO; 1896 LOG(GNUNET_ERROR_TYPE_DEBUG,
1897 "Got LOCAL_ACK, %s-%X ready to receive more data, but none pending on %s-%X(%p)!\n",
1898 GSC_2s(ccc->c),
1899 ntohl(client_ccn.channel_of_client),
1900 GCCH_2s(ch),
1901 ntohl(ccc->ccn.channel_of_client),
1902 ccc);
1903 return; /* none pending */
1918 } 1904 }
1919 else 1905 if (GNUNET_YES == ch->is_loopback)
1920 { 1906 {
1921 GNUNET_assert ((NULL != ch->dest) && (ccc->ccn.channel_of_client == 1907 int to_owner;
1922 ch->dest->ccn.channel_of_client)); 1908
1923 to_owner = GNUNET_YES; 1909 /* Messages are always in-order, just send */
1910 GNUNET_CONTAINER_DLL_remove(ccc->head_recv, ccc->tail_recv, com);
1911 ccc->num_recv--;
1912 GSC_send_to_client(ccc->c, com->env);
1913 /* Notify sender that we can receive more */
1914 if ((NULL != ch->owner) &&
1915 (ccc->ccn.channel_of_client == ch->owner->ccn.channel_of_client))
1916 {
1917 to_owner = GNUNET_NO;
1918 }
1919 else
1920 {
1921 GNUNET_assert((NULL != ch->dest) && (ccc->ccn.channel_of_client ==
1922 ch->dest->ccn.channel_of_client));
1923 to_owner = GNUNET_YES;
1924 }
1925 send_ack_to_client(ch, to_owner);
1926 GNUNET_free(com);
1927 return;
1924 } 1928 }
1925 send_ack_to_client (ch, to_owner);
1926 GNUNET_free (com);
1927 return;
1928 }
1929 1929
1930 if ((com->mid.mid != ch->mid_recv.mid) && (GNUNET_NO == ch->out_of_order) && 1930 if ((com->mid.mid != ch->mid_recv.mid) && (GNUNET_NO == ch->out_of_order) &&
1931 (GNUNET_YES == ch->reliable)) 1931 (GNUNET_YES == ch->reliable))
1932 { 1932 {
1933 LOG (GNUNET_ERROR_TYPE_DEBUG, 1933 LOG(GNUNET_ERROR_TYPE_DEBUG,
1934 "Got LOCAL_ACK, %s-%X ready to receive more data (but next one is out-of-order %u vs. %u)!\n", 1934 "Got LOCAL_ACK, %s-%X ready to receive more data (but next one is out-of-order %u vs. %u)!\n",
1935 GSC_2s (ccc->c), 1935 GSC_2s(ccc->c),
1936 ntohl (ccc->ccn.channel_of_client), 1936 ntohl(ccc->ccn.channel_of_client),
1937 ntohl (com->mid.mid), 1937 ntohl(com->mid.mid),
1938 ntohl (ch->mid_recv.mid)); 1938 ntohl(ch->mid_recv.mid));
1939 return; /* missing next one in-order */ 1939 return; /* missing next one in-order */
1940 } 1940 }
1941 1941
1942 LOG (GNUNET_ERROR_TYPE_DEBUG, 1942 LOG(GNUNET_ERROR_TYPE_DEBUG,
1943 "Got LOCAL_ACK, giving payload message %u to %s-%X on %s\n", 1943 "Got LOCAL_ACK, giving payload message %u to %s-%X on %s\n",
1944 ntohl (com->mid.mid), 1944 ntohl(com->mid.mid),
1945 GSC_2s (ccc->c), 1945 GSC_2s(ccc->c),
1946 ntohl (ccc->ccn.channel_of_client), 1946 ntohl(ccc->ccn.channel_of_client),
1947 GCCH_2s (ch)); 1947 GCCH_2s(ch));
1948 1948
1949 /* all good, pass next message to client */ 1949 /* all good, pass next message to client */
1950 GNUNET_CONTAINER_DLL_remove (ccc->head_recv, ccc->tail_recv, com); 1950 GNUNET_CONTAINER_DLL_remove(ccc->head_recv, ccc->tail_recv, com);
1951 ccc->num_recv--; 1951 ccc->num_recv--;
1952 /* FIXME: if unreliable, this is not aggressive 1952 /* FIXME: if unreliable, this is not aggressive
1953 enough, as it would be OK to have lost some! */ 1953 enough, as it would be OK to have lost some! */
1954 1954
1955 ch->mid_recv.mid = htonl (1 + ntohl (com->mid.mid)); 1955 ch->mid_recv.mid = htonl(1 + ntohl(com->mid.mid));
1956 ch->mid_futures >>= 1; /* equivalent to division by 2 */ 1956 ch->mid_futures >>= 1; /* equivalent to division by 2 */
1957 ccc->client_ready = GNUNET_NO; 1957 ccc->client_ready = GNUNET_NO;
1958 GSC_send_to_client (ccc->c, com->env); 1958 GSC_send_to_client(ccc->c, com->env);
1959 GNUNET_free (com); 1959 GNUNET_free(com);
1960 send_channel_data_ack (ch); 1960 send_channel_data_ack(ch);
1961 if (NULL != ccc->head_recv) 1961 if (NULL != ccc->head_recv)
1962 return; 1962 return;
1963 if (GNUNET_NO == ch->destroy) 1963 if (GNUNET_NO == ch->destroy)
1964 return; 1964 return;
1965 GCT_send_channel_destroy (ch->t, ch->ctn); 1965 GCT_send_channel_destroy(ch->t, ch->ctn);
1966 channel_destroy (ch); 1966 channel_destroy(ch);
1967} 1967}
1968 1968
1969 1969
1970#define LOG2(level, ...) \ 1970#define LOG2(level, ...) \
1971 GNUNET_log_from_nocheck (level, "cadet-chn", __VA_ARGS__) 1971 GNUNET_log_from_nocheck(level, "cadet-chn", __VA_ARGS__)
1972 1972
1973 1973
1974/** 1974/**
@@ -1978,46 +1978,46 @@ GCCH_handle_local_ack (struct CadetChannel *ch,
1978 * @param level Debug level to use. 1978 * @param level Debug level to use.
1979 */ 1979 */
1980void 1980void
1981GCCH_debug (struct CadetChannel *ch, enum GNUNET_ErrorType level) 1981GCCH_debug(struct CadetChannel *ch, enum GNUNET_ErrorType level)
1982{ 1982{
1983#if ! defined(GNUNET_CULL_LOGGING) 1983#if !defined(GNUNET_CULL_LOGGING)
1984 int do_log; 1984 int do_log;
1985 1985
1986 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK), 1986 do_log = GNUNET_get_log_call_status(level & (~GNUNET_ERROR_TYPE_BULK),
1987 "cadet-chn", 1987 "cadet-chn",
1988 __FILE__, 1988 __FILE__,
1989 __FUNCTION__, 1989 __FUNCTION__,
1990 __LINE__); 1990 __LINE__);
1991 if (0 == do_log) 1991 if (0 == do_log)
1992 return; 1992 return;
1993 1993
1994 if (NULL == ch) 1994 if (NULL == ch)
1995 { 1995 {
1996 LOG2 (level, "CHN *** DEBUG NULL CHANNEL ***\n"); 1996 LOG2(level, "CHN *** DEBUG NULL CHANNEL ***\n");
1997 return; 1997 return;
1998 } 1998 }
1999 LOG2 (level, "CHN %s:%X (%p)\n", GCT_2s (ch->t), ch->ctn, ch); 1999 LOG2(level, "CHN %s:%X (%p)\n", GCT_2s(ch->t), ch->ctn, ch);
2000 if (NULL != ch->owner) 2000 if (NULL != ch->owner)
2001 { 2001 {
2002 LOG2 (level, 2002 LOG2(level,
2003 "CHN origin %s ready %s local-id: %u\n", 2003 "CHN origin %s ready %s local-id: %u\n",
2004 GSC_2s (ch->owner->c), 2004 GSC_2s(ch->owner->c),
2005 ch->owner->client_ready ? "YES" : "NO", 2005 ch->owner->client_ready ? "YES" : "NO",
2006 ntohl (ch->owner->ccn.channel_of_client)); 2006 ntohl(ch->owner->ccn.channel_of_client));
2007 } 2007 }
2008 if (NULL != ch->dest) 2008 if (NULL != ch->dest)
2009 { 2009 {
2010 LOG2 (level, 2010 LOG2(level,
2011 "CHN destination %s ready %s local-id: %u\n", 2011 "CHN destination %s ready %s local-id: %u\n",
2012 GSC_2s (ch->dest->c), 2012 GSC_2s(ch->dest->c),
2013 ch->dest->client_ready ? "YES" : "NO", 2013 ch->dest->client_ready ? "YES" : "NO",
2014 ntohl (ch->dest->ccn.channel_of_client)); 2014 ntohl(ch->dest->ccn.channel_of_client));
2015 } 2015 }
2016 LOG2 (level, 2016 LOG2(level,
2017 "CHN Message IDs recv: %d (%LLX), send: %d\n", 2017 "CHN Message IDs recv: %d (%LLX), send: %d\n",
2018 ntohl (ch->mid_recv.mid), 2018 ntohl(ch->mid_recv.mid),
2019 (unsigned long long) ch->mid_futures, 2019 (unsigned long long)ch->mid_futures,
2020 ntohl (ch->mid_send.mid)); 2020 ntohl(ch->mid_send.mid));
2021#endif 2021#endif
2022} 2022}
2023 2023
diff --git a/src/cadet/gnunet-service-cadet_channel.h b/src/cadet/gnunet-service-cadet_channel.h
index 2eb28262d..a64d4058e 100644
--- a/src/cadet/gnunet-service-cadet_channel.h
+++ b/src/cadet/gnunet-service-cadet_channel.h
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file cadet/gnunet-service-cadet_channel.h 22 * @file cadet/gnunet-service-cadet_channel.h
@@ -44,7 +44,7 @@ struct CadetChannel;
44 44
45 45
46/** 46/**
47 * Hash the @a port and @a initiator and @a listener to 47 * Hash the @a port and @a initiator and @a listener to
48 * calculate the "challenge" @a h_port we send to the other 48 * calculate the "challenge" @a h_port we send to the other
49 * peer on #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN. 49 * peer on #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN.
50 * 50 *
@@ -53,9 +53,9 @@ struct CadetChannel;
53 * @param listener peer that is listining on @a port 53 * @param listener peer that is listining on @a port
54 */ 54 */
55void 55void
56GCCH_hash_port (struct GNUNET_HashCode *h_port, 56GCCH_hash_port(struct GNUNET_HashCode *h_port,
57 const struct GNUNET_HashCode *port, 57 const struct GNUNET_HashCode *port,
58 const struct GNUNET_PeerIdentity *listener); 58 const struct GNUNET_PeerIdentity *listener);
59 59
60 60
61/** 61/**
@@ -66,7 +66,7 @@ GCCH_hash_port (struct GNUNET_HashCode *h_port,
66 * @return Static string with the channel IDs. 66 * @return Static string with the channel IDs.
67 */ 67 */
68const char * 68const char *
69GCCH_2s (const struct CadetChannel *ch); 69GCCH_2s(const struct CadetChannel *ch);
70 70
71 71
72/** 72/**
@@ -76,8 +76,8 @@ GCCH_2s (const struct CadetChannel *ch);
76 * @param level Debug level to use. 76 * @param level Debug level to use.
77 */ 77 */
78void 78void
79GCCH_debug (struct CadetChannel *ch, 79GCCH_debug(struct CadetChannel *ch,
80 enum GNUNET_ErrorType level); 80 enum GNUNET_ErrorType level);
81 81
82 82
83/** 83/**
@@ -88,7 +88,7 @@ GCCH_debug (struct CadetChannel *ch,
88 * @return ID used to identify the channel with the remote peer. 88 * @return ID used to identify the channel with the remote peer.
89 */ 89 */
90struct GNUNET_CADET_ChannelTunnelNumber 90struct GNUNET_CADET_ChannelTunnelNumber
91GCCH_get_id (const struct CadetChannel *ch); 91GCCH_get_id(const struct CadetChannel *ch);
92 92
93 93
94/** 94/**
@@ -102,11 +102,11 @@ GCCH_get_id (const struct CadetChannel *ch);
102 * @return handle to the new channel 102 * @return handle to the new channel
103 */ 103 */
104struct CadetChannel * 104struct CadetChannel *
105GCCH_channel_local_new (struct CadetClient *owner, 105GCCH_channel_local_new(struct CadetClient *owner,
106 struct GNUNET_CADET_ClientChannelNumber owner_id, 106 struct GNUNET_CADET_ClientChannelNumber owner_id,
107 struct CadetPeer *destination, 107 struct CadetPeer *destination,
108 const struct GNUNET_HashCode *port, 108 const struct GNUNET_HashCode *port,
109 uint32_t options); 109 uint32_t options);
110 110
111 111
112/** 112/**
@@ -119,9 +119,9 @@ GCCH_channel_local_new (struct CadetClient *owner,
119 * @param port port number @a c is listening on 119 * @param port port number @a c is listening on
120 */ 120 */
121void 121void
122GCCH_bind (struct CadetChannel *ch, 122GCCH_bind(struct CadetChannel *ch,
123 struct CadetClient *c, 123 struct CadetClient *c,
124 const struct GNUNET_HashCode *port); 124 const struct GNUNET_HashCode *port);
125 125
126 126
127/** 127/**
@@ -133,9 +133,9 @@ GCCH_bind (struct CadetChannel *ch,
133 * @param ccn client number of the client @a c 133 * @param ccn client number of the client @a c
134 */ 134 */
135void 135void
136GCCH_channel_local_destroy (struct CadetChannel *ch, 136GCCH_channel_local_destroy(struct CadetChannel *ch,
137 struct CadetClient *c, 137 struct CadetClient *c,
138 struct GNUNET_CADET_ClientChannelNumber ccn); 138 struct GNUNET_CADET_ClientChannelNumber ccn);
139 139
140 140
141/** 141/**
@@ -149,7 +149,7 @@ GCCH_channel_local_destroy (struct CadetChannel *ch,
149 * @param ch the channel for which the tunnel is now ready 149 * @param ch the channel for which the tunnel is now ready
150 */ 150 */
151void 151void
152GCCH_tunnel_up (struct CadetChannel *ch); 152GCCH_tunnel_up(struct CadetChannel *ch);
153 153
154 154
155/** 155/**
@@ -163,10 +163,10 @@ GCCH_tunnel_up (struct CadetChannel *ch);
163 * @return handle to the new channel 163 * @return handle to the new channel
164 */ 164 */
165struct CadetChannel * 165struct CadetChannel *
166GCCH_channel_incoming_new (struct CadetTunnel *t, 166GCCH_channel_incoming_new(struct CadetTunnel *t,
167 struct GNUNET_CADET_ChannelTunnelNumber chid, 167 struct GNUNET_CADET_ChannelTunnelNumber chid,
168 const struct GNUNET_HashCode *h_port, 168 const struct GNUNET_HashCode *h_port,
169 uint32_t options); 169 uint32_t options);
170 170
171 171
172/** 172/**
@@ -178,8 +178,8 @@ GCCH_channel_incoming_new (struct CadetTunnel *t,
178 * @param cti identifier of the connection that delivered the message 178 * @param cti identifier of the connection that delivered the message
179 */ 179 */
180void 180void
181GCCH_handle_duplicate_open (struct CadetChannel *ch, 181GCCH_handle_duplicate_open(struct CadetChannel *ch,
182 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti); 182 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti);
183 183
184 184
185 185
@@ -191,9 +191,9 @@ GCCH_handle_duplicate_open (struct CadetChannel *ch,
191 * @param msg message that was received 191 * @param msg message that was received
192 */ 192 */
193void 193void
194GCCH_handle_channel_plaintext_data (struct CadetChannel *ch, 194GCCH_handle_channel_plaintext_data(struct CadetChannel *ch,
195 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti, 195 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
196 const struct GNUNET_CADET_ChannelAppDataMessage *msg); 196 const struct GNUNET_CADET_ChannelAppDataMessage *msg);
197 197
198 198
199/** 199/**
@@ -205,9 +205,9 @@ GCCH_handle_channel_plaintext_data (struct CadetChannel *ch,
205 * @param ack details about what was received 205 * @param ack details about what was received
206 */ 206 */
207void 207void
208GCCH_handle_channel_plaintext_data_ack (struct CadetChannel *ch, 208GCCH_handle_channel_plaintext_data_ack(struct CadetChannel *ch,
209 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti, 209 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
210 const struct GNUNET_CADET_ChannelDataAckMessage *ack); 210 const struct GNUNET_CADET_ChannelDataAckMessage *ack);
211 211
212 212
213/** 213/**
@@ -220,9 +220,9 @@ GCCH_handle_channel_plaintext_data_ack (struct CadetChannel *ch,
220 * @param port port number (needed to verify receiver knows the port) 220 * @param port port number (needed to verify receiver knows the port)
221 */ 221 */
222void 222void
223GCCH_handle_channel_open_ack (struct CadetChannel *ch, 223GCCH_handle_channel_open_ack(struct CadetChannel *ch,
224 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti, 224 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
225 const struct GNUNET_HashCode *port); 225 const struct GNUNET_HashCode *port);
226 226
227 227
228/** 228/**
@@ -242,8 +242,8 @@ GCCH_handle_channel_open_ack (struct CadetChannel *ch,
242 * NULL during shutdown 242 * NULL during shutdown
243 */ 243 */
244void 244void
245GCCH_handle_remote_destroy (struct CadetChannel *ch, 245GCCH_handle_remote_destroy(struct CadetChannel *ch,
246 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti); 246 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti);
247 247
248 248
249/** 249/**
@@ -261,10 +261,10 @@ GCCH_handle_remote_destroy (struct CadetChannel *ch,
261 * #GNUNET_SYSERR in case of an error. 261 * #GNUNET_SYSERR in case of an error.
262 */ 262 */
263int 263int
264GCCH_handle_local_data (struct CadetChannel *ch, 264GCCH_handle_local_data(struct CadetChannel *ch,
265 struct GNUNET_CADET_ClientChannelNumber sender_ccn, 265 struct GNUNET_CADET_ClientChannelNumber sender_ccn,
266 const char *buf, 266 const char *buf,
267 size_t buf_len); 267 size_t buf_len);
268 268
269 269
270/** 270/**
@@ -274,7 +274,7 @@ GCCH_handle_local_data (struct CadetChannel *ch,
274 * @param client_ccn ccn of the client sending the ack 274 * @param client_ccn ccn of the client sending the ack
275 */ 275 */
276void 276void
277GCCH_handle_local_ack (struct CadetChannel *ch, 277GCCH_handle_local_ack(struct CadetChannel *ch,
278 struct GNUNET_CADET_ClientChannelNumber client_ccn); 278 struct GNUNET_CADET_ClientChannelNumber client_ccn);
279 279
280#endif 280#endif
diff --git a/src/cadet/gnunet-service-cadet_connection.c b/src/cadet/gnunet-service-cadet_connection.c
index 8849e563f..e3c1a4124 100644
--- a/src/cadet/gnunet-service-cadet_connection.c
+++ b/src/cadet/gnunet-service-cadet_connection.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file cadet/gnunet-service-cadet_connection.c 22 * @file cadet/gnunet-service-cadet_connection.c
@@ -35,7 +35,7 @@
35#include "cadet_protocol.h" 35#include "cadet_protocol.h"
36 36
37 37
38#define LOG(level, ...) GNUNET_log_from (level, "cadet-con", __VA_ARGS__) 38#define LOG(level, ...) GNUNET_log_from(level, "cadet-con", __VA_ARGS__)
39 39
40 40
41/** 41/**
@@ -43,14 +43,13 @@
43 * TODO: replace by 2 RTT if/once we have connection-level RTT data! 43 * TODO: replace by 2 RTT if/once we have connection-level RTT data!
44 */ 44 */
45#define INITIAL_CONNECTION_CREATE_RETRY_DELAY \ 45#define INITIAL_CONNECTION_CREATE_RETRY_DELAY \
46 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 200) 46 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 200)
47 47
48 48
49/** 49/**
50 * All the states a connection can be in. 50 * All the states a connection can be in.
51 */ 51 */
52enum CadetConnectionState 52enum CadetConnectionState {
53{
54 /** 53 /**
55 * Uninitialized status, we have not yet even gotten the message queue. 54 * Uninitialized status, we have not yet even gotten the message queue.
56 */ 55 */
@@ -76,16 +75,13 @@ enum CadetConnectionState
76 * Connection confirmed, ready to carry traffic. 75 * Connection confirmed, ready to carry traffic.
77 */ 76 */
78 CADET_CONNECTION_READY 77 CADET_CONNECTION_READY
79
80}; 78};
81 79
82 80
83/** 81/**
84 * Low-level connection to a destination. 82 * Low-level connection to a destination.
85 */ 83 */
86struct CadetConnection 84struct CadetConnection {
87{
88
89 /** 85 /**
90 * ID of the connection. 86 * ID of the connection.
91 */ 87 */
@@ -185,10 +181,10 @@ struct CadetConnection
185 * @return NULL if connection was not found 181 * @return NULL if connection was not found
186 */ 182 */
187struct CadetConnection * 183struct CadetConnection *
188GCC_lookup (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid) 184GCC_lookup(const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
189{ 185{
190 return GNUNET_CONTAINER_multishortmap_get (connections, 186 return GNUNET_CONTAINER_multishortmap_get(connections,
191 &cid->connection_of_tunnel); 187 &cid->connection_of_tunnel);
192} 188}
193 189
194 190
@@ -201,9 +197,9 @@ GCC_lookup (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
201 * @param new_mqm_ready new `mqm_ready` state for @a cc 197 * @param new_mqm_ready new `mqm_ready` state for @a cc
202 */ 198 */
203static void 199static void
204update_state (struct CadetConnection *cc, 200update_state(struct CadetConnection *cc,
205 enum CadetConnectionState new_state, 201 enum CadetConnectionState new_state,
206 int new_mqm_ready) 202 int new_mqm_ready)
207{ 203{
208 int old_ready; 204 int old_ready;
209 int new_ready; 205 int new_ready;
@@ -217,7 +213,7 @@ update_state (struct CadetConnection *cc,
217 cc->state = new_state; 213 cc->state = new_state;
218 cc->mqm_ready = new_mqm_ready; 214 cc->mqm_ready = new_mqm_ready;
219 if (old_ready != new_ready) 215 if (old_ready != new_ready)
220 cc->ready_cb (cc->ready_cb_cls, new_ready); 216 cc->ready_cb(cc->ready_cb_cls, new_ready);
221} 217}
222 218
223 219
@@ -228,34 +224,34 @@ update_state (struct CadetConnection *cc,
228 * @param cc connection to destroy 224 * @param cc connection to destroy
229 */ 225 */
230static void 226static void
231GCC_destroy (struct CadetConnection *cc) 227GCC_destroy(struct CadetConnection *cc)
232{ 228{
233 LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying %s\n", GCC_2s (cc)); 229 LOG(GNUNET_ERROR_TYPE_DEBUG, "Destroying %s\n", GCC_2s(cc));
234 if (NULL != cc->mq_man) 230 if (NULL != cc->mq_man)
235 { 231 {
236 GCP_request_mq_cancel (cc->mq_man, NULL); 232 GCP_request_mq_cancel(cc->mq_man, NULL);
237 cc->mq_man = NULL; 233 cc->mq_man = NULL;
238 } 234 }
239 if (NULL != cc->task) 235 if (NULL != cc->task)
240 { 236 {
241 GNUNET_SCHEDULER_cancel (cc->task); 237 GNUNET_SCHEDULER_cancel(cc->task);
242 cc->task = NULL; 238 cc->task = NULL;
243 } 239 }
244 if (NULL != cc->keepalive_qe) 240 if (NULL != cc->keepalive_qe)
245 { 241 {
246 GCT_send_cancel (cc->keepalive_qe); 242 GCT_send_cancel(cc->keepalive_qe);
247 cc->keepalive_qe = NULL; 243 cc->keepalive_qe = NULL;
248 } 244 }
249 GCPP_del_connection (cc->path, cc->off, cc); 245 GCPP_del_connection(cc->path, cc->off, cc);
250 for (unsigned int i = 0; i < cc->off; i++) 246 for (unsigned int i = 0; i < cc->off; i++)
251 GCP_remove_connection (GCPP_get_peer_at_offset (cc->path, i), cc); 247 GCP_remove_connection(GCPP_get_peer_at_offset(cc->path, i), cc);
252 GNUNET_assert ( 248 GNUNET_assert(
253 GNUNET_YES == 249 GNUNET_YES ==
254 GNUNET_CONTAINER_multishortmap_remove (connections, 250 GNUNET_CONTAINER_multishortmap_remove(connections,
255 &GCC_get_id (cc) 251 &GCC_get_id(cc)
256 ->connection_of_tunnel, 252 ->connection_of_tunnel,
257 cc)); 253 cc));
258 GNUNET_free (cc); 254 GNUNET_free(cc);
259} 255}
260 256
261 257
@@ -268,14 +264,14 @@ GCC_destroy (struct CadetConnection *cc)
268 * @param cc connection to destroy 264 * @param cc connection to destroy
269 */ 265 */
270void 266void
271GCC_destroy_without_core (struct CadetConnection *cc) 267GCC_destroy_without_core(struct CadetConnection *cc)
272{ 268{
273 if (NULL != cc->ct) 269 if (NULL != cc->ct)
274 { 270 {
275 GCT_connection_lost (cc->ct); 271 GCT_connection_lost(cc->ct);
276 cc->ct = NULL; 272 cc->ct = NULL;
277 } 273 }
278 GCC_destroy (cc); 274 GCC_destroy(cc);
279} 275}
280 276
281 277
@@ -287,22 +283,22 @@ GCC_destroy_without_core (struct CadetConnection *cc)
287 * @param cc connection to destroy 283 * @param cc connection to destroy
288 */ 284 */
289void 285void
290GCC_destroy_without_tunnel (struct CadetConnection *cc) 286GCC_destroy_without_tunnel(struct CadetConnection *cc)
291{ 287{
292 cc->ct = NULL; 288 cc->ct = NULL;
293 if ((CADET_CONNECTION_SENDING_CREATE != cc->state) && (NULL != cc->mq_man)) 289 if ((CADET_CONNECTION_SENDING_CREATE != cc->state) && (NULL != cc->mq_man))
294 { 290 {
295 struct GNUNET_MQ_Envelope *env; 291 struct GNUNET_MQ_Envelope *env;
296 struct GNUNET_CADET_ConnectionDestroyMessage *destroy_msg; 292 struct GNUNET_CADET_ConnectionDestroyMessage *destroy_msg;
297 293
298 /* Need to notify next hop that we are down. */ 294 /* Need to notify next hop that we are down. */
299 env = 295 env =
300 GNUNET_MQ_msg (destroy_msg, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY); 296 GNUNET_MQ_msg(destroy_msg, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
301 destroy_msg->cid = cc->cid; 297 destroy_msg->cid = cc->cid;
302 GCP_request_mq_cancel (cc->mq_man, env); 298 GCP_request_mq_cancel(cc->mq_man, env);
303 cc->mq_man = NULL; 299 cc->mq_man = NULL;
304 } 300 }
305 GCC_destroy (cc); 301 GCC_destroy(cc);
306} 302}
307 303
308 304
@@ -313,7 +309,7 @@ GCC_destroy_without_tunnel (struct CadetConnection *cc)
313 * @return corresponding entry in the tunnel's connection list 309 * @return corresponding entry in the tunnel's connection list
314 */ 310 */
315struct CadetTConnection * 311struct CadetTConnection *
316GCC_get_ct (struct CadetConnection *cc) 312GCC_get_ct(struct CadetConnection *cc)
317{ 313{
318 return cc->ct; 314 return cc->ct;
319} 315}
@@ -326,7 +322,7 @@ GCC_get_ct (struct CadetConnection *cc)
326 * @return the metrics 322 * @return the metrics
327 */ 323 */
328const struct CadetConnectionMetrics * 324const struct CadetConnectionMetrics *
329GCC_get_metrics (struct CadetConnection *cc) 325GCC_get_metrics(struct CadetConnection *cc)
330{ 326{
331 return &cc->metrics; 327 return &cc->metrics;
332} 328}
@@ -339,7 +335,7 @@ GCC_get_metrics (struct CadetConnection *cc)
339 * @param cls the `struct CadetConnection` to keep alive. 335 * @param cls the `struct CadetConnection` to keep alive.
340 */ 336 */
341static void 337static void
342send_keepalive (void *cls); 338send_keepalive(void *cls);
343 339
344 340
345/** 341/**
@@ -351,15 +347,15 @@ send_keepalive (void *cls);
351 * if transmission failed 347 * if transmission failed
352 */ 348 */
353static void 349static void
354keepalive_done (void *cls, 350keepalive_done(void *cls,
355 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid) 351 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
356{ 352{
357 struct CadetConnection *cc = cls; 353 struct CadetConnection *cc = cls;
358 354
359 cc->keepalive_qe = NULL; 355 cc->keepalive_qe = NULL;
360 if ((GNUNET_YES == cc->mqm_ready) && (NULL == cc->task)) 356 if ((GNUNET_YES == cc->mqm_ready) && (NULL == cc->task))
361 cc->task = 357 cc->task =
362 GNUNET_SCHEDULER_add_delayed (keepalive_period, &send_keepalive, cc); 358 GNUNET_SCHEDULER_add_delayed(keepalive_period, &send_keepalive, cc);
363} 359}
364 360
365 361
@@ -370,31 +366,31 @@ keepalive_done (void *cls,
370 * @param cls the `struct CadetConnection` to keep alive. 366 * @param cls the `struct CadetConnection` to keep alive.
371 */ 367 */
372static void 368static void
373send_keepalive (void *cls) 369send_keepalive(void *cls)
374{ 370{
375 struct CadetConnection *cc = cls; 371 struct CadetConnection *cc = cls;
376 struct GNUNET_MessageHeader msg; 372 struct GNUNET_MessageHeader msg;
377 373
378 cc->task = NULL; 374 cc->task = NULL;
379 if (CADET_TUNNEL_KEY_OK != GCT_get_estate (cc->ct->t)) 375 if (CADET_TUNNEL_KEY_OK != GCT_get_estate(cc->ct->t))
380 { 376 {
381 /* Tunnel not yet ready, wait with keepalives... */ 377 /* Tunnel not yet ready, wait with keepalives... */
382 cc->task = 378 cc->task =
383 GNUNET_SCHEDULER_add_delayed (keepalive_period, &send_keepalive, cc); 379 GNUNET_SCHEDULER_add_delayed(keepalive_period, &send_keepalive, cc);
384 return; 380 return;
385 } 381 }
386 GNUNET_assert (NULL != cc->ct); 382 GNUNET_assert(NULL != cc->ct);
387 GNUNET_assert (GNUNET_YES == cc->mqm_ready); 383 GNUNET_assert(GNUNET_YES == cc->mqm_ready);
388 GNUNET_assert (NULL == cc->keepalive_qe); 384 GNUNET_assert(NULL == cc->keepalive_qe);
389 LOG (GNUNET_ERROR_TYPE_INFO, 385 LOG(GNUNET_ERROR_TYPE_INFO,
390 "Sending KEEPALIVE on behalf of %s via %s\n", 386 "Sending KEEPALIVE on behalf of %s via %s\n",
391 GCC_2s (cc), 387 GCC_2s(cc),
392 GCT_2s (cc->ct->t)); 388 GCT_2s(cc->ct->t));
393 GNUNET_STATISTICS_update (stats, "# keepalives sent", 1, GNUNET_NO); 389 GNUNET_STATISTICS_update(stats, "# keepalives sent", 1, GNUNET_NO);
394 msg.size = htons (sizeof (msg)); 390 msg.size = htons(sizeof(msg));
395 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE); 391 msg.type = htons(GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE);
396 392
397 cc->keepalive_qe = GCT_send (cc->ct->t, &msg, &keepalive_done, cc); 393 cc->keepalive_qe = GCT_send(cc->ct->t, &msg, &keepalive_done, cc);
398} 394}
399 395
400 396
@@ -405,11 +401,11 @@ send_keepalive (void *cls)
405 * @param cid connection identifier where we expect an ACK 401 * @param cid connection identifier where we expect an ACK
406 */ 402 */
407void 403void
408GCC_ack_expected (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid) 404GCC_ack_expected(const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
409{ 405{
410 struct CadetConnection *cc; 406 struct CadetConnection *cc;
411 407
412 cc = GCC_lookup (cid); 408 cc = GCC_lookup(cid);
413 if (NULL == cc) 409 if (NULL == cc)
414 return; /* whopise, connection alredy down? */ 410 return; /* whopise, connection alredy down? */
415 cc->metrics.num_acked_transmissions++; 411 cc->metrics.num_acked_transmissions++;
@@ -425,11 +421,11 @@ GCC_ack_expected (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
425 * may have gotten back to us via a different connection). 421 * may have gotten back to us via a different connection).
426 */ 422 */
427void 423void
428GCC_ack_observed (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid) 424GCC_ack_observed(const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
429{ 425{
430 struct CadetConnection *cc; 426 struct CadetConnection *cc;
431 427
432 cc = GCC_lookup (cid); 428 cc = GCC_lookup(cid);
433 if (NULL == cc) 429 if (NULL == cc)
434 return; /* whopise, connection alredy down? */ 430 return; /* whopise, connection alredy down? */
435 cc->metrics.num_successes++; 431 cc->metrics.num_successes++;
@@ -445,17 +441,17 @@ GCC_ack_observed (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
445 * @param latency the observed latency 441 * @param latency the observed latency
446 */ 442 */
447void 443void
448GCC_latency_observed (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid, 444GCC_latency_observed(const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
449 struct GNUNET_TIME_Relative latency) 445 struct GNUNET_TIME_Relative latency)
450{ 446{
451 struct CadetConnection *cc; 447 struct CadetConnection *cc;
452 double weight; 448 double weight;
453 double result; 449 double result;
454 450
455 cc = GCC_lookup (cid); 451 cc = GCC_lookup(cid);
456 if (NULL == cc) 452 if (NULL == cc)
457 return; /* whopise, connection alredy down? */ 453 return; /* whopise, connection alredy down? */
458 GNUNET_STATISTICS_update (stats, "# latencies observed", 1, GNUNET_NO); 454 GNUNET_STATISTICS_update(stats, "# latencies observed", 1, GNUNET_NO);
459 cc->latency_datapoints++; 455 cc->latency_datapoints++;
460 if (cc->latency_datapoints >= 7) 456 if (cc->latency_datapoints >= 7)
461 weight = 7.0; 457 weight = 7.0;
@@ -467,7 +463,7 @@ GCC_latency_observed (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
467 result = (weight * cc->metrics.aged_latency.rel_value_us) + 463 result = (weight * cc->metrics.aged_latency.rel_value_us) +
468 1.0 * latency.rel_value_us; 464 1.0 * latency.rel_value_us;
469 result /= (weight + 1.0); 465 result /= (weight + 1.0);
470 cc->metrics.aged_latency.rel_value_us = (uint64_t) result; 466 cc->metrics.aged_latency.rel_value_us = (uint64_t)result;
471} 467}
472 468
473 469
@@ -479,26 +475,26 @@ GCC_latency_observed (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
479 * @param cc the connection that got the ACK. 475 * @param cc the connection that got the ACK.
480 */ 476 */
481void 477void
482GCC_handle_connection_create_ack (struct CadetConnection *cc) 478GCC_handle_connection_create_ack(struct CadetConnection *cc)
483{ 479{
484 LOG (GNUNET_ERROR_TYPE_DEBUG, 480 LOG(GNUNET_ERROR_TYPE_DEBUG,
485 "Received CADET_CONNECTION_CREATE_ACK for %s in state %d (%s)\n", 481 "Received CADET_CONNECTION_CREATE_ACK for %s in state %d (%s)\n",
486 GCC_2s (cc), 482 GCC_2s(cc),
487 cc->state, 483 cc->state,
488 (GNUNET_YES == cc->mqm_ready) ? "MQM ready" : "MQM busy"); 484 (GNUNET_YES == cc->mqm_ready) ? "MQM ready" : "MQM busy");
489 if (CADET_CONNECTION_READY == cc->state) 485 if (CADET_CONNECTION_READY == cc->state)
490 return; /* Duplicate ACK, ignore */ 486 return; /* Duplicate ACK, ignore */
491 if (NULL != cc->task) 487 if (NULL != cc->task)
492 { 488 {
493 GNUNET_SCHEDULER_cancel (cc->task); 489 GNUNET_SCHEDULER_cancel(cc->task);
494 cc->task = NULL; 490 cc->task = NULL;
495 } 491 }
496 cc->metrics.age = GNUNET_TIME_absolute_get (); 492 cc->metrics.age = GNUNET_TIME_absolute_get();
497 update_state (cc, CADET_CONNECTION_READY, cc->mqm_ready); 493 update_state(cc, CADET_CONNECTION_READY, cc->mqm_ready);
498 if ((NULL == cc->keepalive_qe) && (GNUNET_YES == cc->mqm_ready) && 494 if ((NULL == cc->keepalive_qe) && (GNUNET_YES == cc->mqm_ready) &&
499 (NULL == cc->task)) 495 (NULL == cc->task))
500 cc->task = 496 cc->task =
501 GNUNET_SCHEDULER_add_delayed (keepalive_period, &send_keepalive, cc); 497 GNUNET_SCHEDULER_add_delayed(keepalive_period, &send_keepalive, cc);
502} 498}
503 499
504 500
@@ -509,24 +505,24 @@ GCC_handle_connection_create_ack (struct CadetConnection *cc)
509 * @param msg the key exchange message 505 * @param msg the key exchange message
510 */ 506 */
511void 507void
512GCC_handle_kx (struct CadetConnection *cc, 508GCC_handle_kx(struct CadetConnection *cc,
513 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg) 509 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
514{ 510{
515 LOG (GNUNET_ERROR_TYPE_DEBUG, 511 LOG(GNUNET_ERROR_TYPE_DEBUG,
516 "Received KX message with ephermal %s on CC %s in state %d\n", 512 "Received KX message with ephermal %s on CC %s in state %d\n",
517 GNUNET_e2s (&msg->ephemeral_key), 513 GNUNET_e2s(&msg->ephemeral_key),
518 GNUNET_sh2s (&cc->cid.connection_of_tunnel), 514 GNUNET_sh2s(&cc->cid.connection_of_tunnel),
519 cc->state); 515 cc->state);
520 if (CADET_CONNECTION_SENT == cc->state) 516 if (CADET_CONNECTION_SENT == cc->state)
521 { 517 {
522 /* We didn't get the CADET_CONNECTION_CREATE_ACK, but instead got payload. That's fine, 518 /* We didn't get the CADET_CONNECTION_CREATE_ACK, but instead got payload. That's fine,
523 clearly something is working, so pretend we got an ACK. */ 519 clearly something is working, so pretend we got an ACK. */
524 LOG (GNUNET_ERROR_TYPE_DEBUG, 520 LOG(GNUNET_ERROR_TYPE_DEBUG,
525 "Faking connection CADET_CONNECTION_CREATE_ACK for %s due to KX\n", 521 "Faking connection CADET_CONNECTION_CREATE_ACK for %s due to KX\n",
526 GCC_2s (cc)); 522 GCC_2s(cc));
527 GCC_handle_connection_create_ack (cc); 523 GCC_handle_connection_create_ack(cc);
528 } 524 }
529 GCT_handle_kx (cc->ct, msg); 525 GCT_handle_kx(cc->ct, msg);
530} 526}
531 527
532 528
@@ -537,24 +533,24 @@ GCC_handle_kx (struct CadetConnection *cc,
537 * @param msg the key exchange message 533 * @param msg the key exchange message
538 */ 534 */
539void 535void
540GCC_handle_kx_auth (struct CadetConnection *cc, 536GCC_handle_kx_auth(struct CadetConnection *cc,
541 const struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg) 537 const struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg)
542{ 538{
543 LOG (GNUNET_ERROR_TYPE_DEBUG, 539 LOG(GNUNET_ERROR_TYPE_DEBUG,
544 "Received KX AUTH message with ephermal %s on CC %s in state %d\n", 540 "Received KX AUTH message with ephermal %s on CC %s in state %d\n",
545 GNUNET_e2s (&msg->kx.ephemeral_key), 541 GNUNET_e2s(&msg->kx.ephemeral_key),
546 GNUNET_sh2s (&cc->cid.connection_of_tunnel), 542 GNUNET_sh2s(&cc->cid.connection_of_tunnel),
547 cc->state); 543 cc->state);
548 if (CADET_CONNECTION_SENT == cc->state) 544 if (CADET_CONNECTION_SENT == cc->state)
549 { 545 {
550 /* We didn't get the CADET_CONNECTION_CREATE_ACK, but instead got payload. That's fine, 546 /* We didn't get the CADET_CONNECTION_CREATE_ACK, but instead got payload. That's fine,
551 clearly something is working, so pretend we got an ACK. */ 547 clearly something is working, so pretend we got an ACK. */
552 LOG (GNUNET_ERROR_TYPE_DEBUG, 548 LOG(GNUNET_ERROR_TYPE_DEBUG,
553 "Faking connection CADET_CONNECTION_CREATE_ACK for %s due to KX\n", 549 "Faking connection CADET_CONNECTION_CREATE_ACK for %s due to KX\n",
554 GCC_2s (cc)); 550 GCC_2s(cc));
555 GCC_handle_connection_create_ack (cc); 551 GCC_handle_connection_create_ack(cc);
556 } 552 }
557 GCT_handle_kx_auth (cc->ct, msg); 553 GCT_handle_kx_auth(cc->ct, msg);
558} 554}
559 555
560 556
@@ -565,20 +561,20 @@ GCC_handle_kx_auth (struct CadetConnection *cc,
565 * @param msg the encrypted message to decrypt 561 * @param msg the encrypted message to decrypt
566 */ 562 */
567void 563void
568GCC_handle_encrypted (struct CadetConnection *cc, 564GCC_handle_encrypted(struct CadetConnection *cc,
569 const struct GNUNET_CADET_TunnelEncryptedMessage *msg) 565 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
570{ 566{
571 if (CADET_CONNECTION_SENT == cc->state) 567 if (CADET_CONNECTION_SENT == cc->state)
572 { 568 {
573 /* We didn't get the CREATE_ACK, but instead got payload. That's fine, 569 /* We didn't get the CREATE_ACK, but instead got payload. That's fine,
574 clearly something is working, so pretend we got an ACK. */ 570 clearly something is working, so pretend we got an ACK. */
575 LOG (GNUNET_ERROR_TYPE_DEBUG, 571 LOG(GNUNET_ERROR_TYPE_DEBUG,
576 "Faking connection ACK for %s due to ENCRYPTED payload\n", 572 "Faking connection ACK for %s due to ENCRYPTED payload\n",
577 GCC_2s (cc)); 573 GCC_2s(cc));
578 GCC_handle_connection_create_ack (cc); 574 GCC_handle_connection_create_ack(cc);
579 } 575 }
580 cc->metrics.last_use = GNUNET_TIME_absolute_get (); 576 cc->metrics.last_use = GNUNET_TIME_absolute_get();
581 GCT_handle_encrypted (cc->ct, msg); 577 GCT_handle_encrypted(cc->ct, msg);
582} 578}
583 579
584 580
@@ -589,7 +585,7 @@ GCC_handle_encrypted (struct CadetConnection *cc,
589 * @param cls the `struct CadetConnection` to initiate 585 * @param cls the `struct CadetConnection` to initiate
590 */ 586 */
591static void 587static void
592send_create (void *cls) 588send_create(void *cls)
593{ 589{
594 struct CadetConnection *cc = cls; 590 struct CadetConnection *cc = cls;
595 struct GNUNET_CADET_ConnectionCreateMessage *create_msg; 591 struct GNUNET_CADET_ConnectionCreateMessage *create_msg;
@@ -597,27 +593,27 @@ send_create (void *cls)
597 struct GNUNET_MQ_Envelope *env; 593 struct GNUNET_MQ_Envelope *env;
598 594
599 cc->task = NULL; 595 cc->task = NULL;
600 GNUNET_assert (GNUNET_YES == cc->mqm_ready); 596 GNUNET_assert(GNUNET_YES == cc->mqm_ready);
601 env = 597 env =
602 GNUNET_MQ_msg_extra (create_msg, 598 GNUNET_MQ_msg_extra(create_msg,
603 (2 + cc->off) * sizeof (struct GNUNET_PeerIdentity), 599 (2 + cc->off) * sizeof(struct GNUNET_PeerIdentity),
604 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE); 600 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
605 //TODO This will be removed in a major release, because this will be a protocol breaking change. We set the deprecated 'reliable' bit here that was removed. 601 //TODO This will be removed in a major release, because this will be a protocol breaking change. We set the deprecated 'reliable' bit here that was removed.
606 create_msg->options = 2; 602 create_msg->options = 2;
607 create_msg->cid = cc->cid; 603 create_msg->cid = cc->cid;
608 pids = (struct GNUNET_PeerIdentity *) &create_msg[1]; 604 pids = (struct GNUNET_PeerIdentity *)&create_msg[1];
609 pids[0] = my_full_id; 605 pids[0] = my_full_id;
610 for (unsigned int i = 0; i <= cc->off; i++) 606 for (unsigned int i = 0; i <= cc->off; i++)
611 pids[i + 1] = *GCP_get_id (GCPP_get_peer_at_offset (cc->path, i)); 607 pids[i + 1] = *GCP_get_id(GCPP_get_peer_at_offset(cc->path, i));
612 LOG (GNUNET_ERROR_TYPE_DEBUG, 608 LOG(GNUNET_ERROR_TYPE_DEBUG,
613 "Sending CADET_CONNECTION_CREATE message for %s with %u hops\n", 609 "Sending CADET_CONNECTION_CREATE message for %s with %u hops\n",
614 GCC_2s (cc), 610 GCC_2s(cc),
615 cc->off + 2); 611 cc->off + 2);
616 cc->env = env; 612 cc->env = env;
617 cc->retry_delay = GNUNET_TIME_STD_BACKOFF (cc->retry_delay); 613 cc->retry_delay = GNUNET_TIME_STD_BACKOFF(cc->retry_delay);
618 cc->create_at = GNUNET_TIME_relative_to_absolute (cc->retry_delay); 614 cc->create_at = GNUNET_TIME_relative_to_absolute(cc->retry_delay);
619 update_state (cc, CADET_CONNECTION_SENT, GNUNET_NO); 615 update_state(cc, CADET_CONNECTION_SENT, GNUNET_NO);
620 GCP_send (cc->mq_man, env); 616 GCP_send(cc->mq_man, env);
621} 617}
622 618
623 619
@@ -627,29 +623,29 @@ send_create (void *cls)
627 * @param cls the `struct CadetConnection` to initiate 623 * @param cls the `struct CadetConnection` to initiate
628 */ 624 */
629static void 625static void
630send_create_ack (void *cls) 626send_create_ack(void *cls)
631{ 627{
632 struct CadetConnection *cc = cls; 628 struct CadetConnection *cc = cls;
633 struct GNUNET_CADET_ConnectionCreateAckMessage *ack_msg; 629 struct GNUNET_CADET_ConnectionCreateAckMessage *ack_msg;
634 struct GNUNET_MQ_Envelope *env; 630 struct GNUNET_MQ_Envelope *env;
635 631
636 cc->task = NULL; 632 cc->task = NULL;
637 LOG (GNUNET_ERROR_TYPE_DEBUG, 633 LOG(GNUNET_ERROR_TYPE_DEBUG,
638 "Sending CONNECTION_CREATE_ACK message for %s\n", 634 "Sending CONNECTION_CREATE_ACK message for %s\n",
639 GCC_2s (cc)); 635 GCC_2s(cc));
640 GNUNET_assert (GNUNET_YES == cc->mqm_ready); 636 GNUNET_assert(GNUNET_YES == cc->mqm_ready);
641 env = 637 env =
642 GNUNET_MQ_msg (ack_msg, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK); 638 GNUNET_MQ_msg(ack_msg, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK);
643 ack_msg->cid = cc->cid; 639 ack_msg->cid = cc->cid;
644 cc->env = env; 640 cc->env = env;
645 cc->retry_delay = GNUNET_TIME_STD_BACKOFF (cc->retry_delay); 641 cc->retry_delay = GNUNET_TIME_STD_BACKOFF(cc->retry_delay);
646 cc->create_ack_at = GNUNET_TIME_relative_to_absolute (cc->retry_delay); 642 cc->create_ack_at = GNUNET_TIME_relative_to_absolute(cc->retry_delay);
647 if (CADET_CONNECTION_CREATE_RECEIVED == cc->state) 643 if (CADET_CONNECTION_CREATE_RECEIVED == cc->state)
648 update_state (cc, CADET_CONNECTION_READY, GNUNET_NO); 644 update_state(cc, CADET_CONNECTION_READY, GNUNET_NO);
649 if (CADET_CONNECTION_READY == cc->state) 645 if (CADET_CONNECTION_READY == cc->state)
650 cc->task = 646 cc->task =
651 GNUNET_SCHEDULER_add_delayed (keepalive_period, &send_keepalive, cc); 647 GNUNET_SCHEDULER_add_delayed(keepalive_period, &send_keepalive, cc);
652 GCP_send (cc->mq_man, env); 648 GCP_send(cc->mq_man, env);
653} 649}
654 650
655 651
@@ -661,31 +657,31 @@ send_create_ack (void *cls)
661 * @param cc connection that got the duplicate CREATE 657 * @param cc connection that got the duplicate CREATE
662 */ 658 */
663void 659void
664GCC_handle_duplicate_create (struct CadetConnection *cc) 660GCC_handle_duplicate_create(struct CadetConnection *cc)
665{ 661{
666 if (GNUNET_YES == cc->mqm_ready) 662 if (GNUNET_YES == cc->mqm_ready)
667 { 663 {
668 LOG (GNUNET_ERROR_TYPE_DEBUG, 664 LOG(GNUNET_ERROR_TYPE_DEBUG,
669 "Got duplicate CREATE for %s, scheduling another ACK (%s)\n", 665 "Got duplicate CREATE for %s, scheduling another ACK (%s)\n",
670 GCC_2s (cc), 666 GCC_2s(cc),
671 (GNUNET_YES == cc->mqm_ready) ? "MQM ready" : "MQM busy"); 667 (GNUNET_YES == cc->mqm_ready) ? "MQM ready" : "MQM busy");
672 /* Revert back to the state of having only received the 'CREATE', 668 /* Revert back to the state of having only received the 'CREATE',
673 and immediately proceed to send the CREATE_ACK. */ 669 and immediately proceed to send the CREATE_ACK. */
674 update_state (cc, CADET_CONNECTION_CREATE_RECEIVED, cc->mqm_ready); 670 update_state(cc, CADET_CONNECTION_CREATE_RECEIVED, cc->mqm_ready);
675 if (NULL != cc->task) 671 if (NULL != cc->task)
676 GNUNET_SCHEDULER_cancel (cc->task); 672 GNUNET_SCHEDULER_cancel(cc->task);
677 cc->task = 673 cc->task =
678 GNUNET_SCHEDULER_add_at (cc->create_ack_at, &send_create_ack, cc); 674 GNUNET_SCHEDULER_add_at(cc->create_ack_at, &send_create_ack, cc);
679 } 675 }
680 else 676 else
681 { 677 {
682 /* We are currently sending something else back, which 678 /* We are currently sending something else back, which
683 can only be an ACK or payload, either of which would 679 can only be an ACK or payload, either of which would
684 do. So actually no need to do anything. */ 680 do. So actually no need to do anything. */
685 LOG (GNUNET_ERROR_TYPE_DEBUG, 681 LOG(GNUNET_ERROR_TYPE_DEBUG,
686 "Got duplicate CREATE for %s. MQ is busy, not queueing another ACK\n", 682 "Got duplicate CREATE for %s. MQ is busy, not queueing another ACK\n",
687 GCC_2s (cc)); 683 GCC_2s(cc));
688 } 684 }
689} 685}
690 686
691 687
@@ -700,63 +696,67 @@ GCC_handle_duplicate_create (struct CadetConnection *cc)
700 * and the last envelope was discarded 696 * and the last envelope was discarded
701 */ 697 */
702static void 698static void
703manage_first_hop_mq (void *cls, int available) 699manage_first_hop_mq(void *cls, int available)
704{ 700{
705 struct CadetConnection *cc = cls; 701 struct CadetConnection *cc = cls;
706 702
707 if (GNUNET_YES != available) 703 if (GNUNET_YES != available)
708 {
709 /* Connection is down, for now... */
710 LOG (GNUNET_ERROR_TYPE_DEBUG, "Core MQ for %s went down\n", GCC_2s (cc));
711 update_state (cc, CADET_CONNECTION_NEW, GNUNET_NO);
712 cc->retry_delay = INITIAL_CONNECTION_CREATE_RETRY_DELAY;
713 if (NULL != cc->task)
714 { 704 {
715 GNUNET_SCHEDULER_cancel (cc->task); 705 /* Connection is down, for now... */
716 cc->task = NULL; 706 LOG(GNUNET_ERROR_TYPE_DEBUG, "Core MQ for %s went down\n", GCC_2s(cc));
707 update_state(cc, CADET_CONNECTION_NEW, GNUNET_NO);
708 cc->retry_delay = INITIAL_CONNECTION_CREATE_RETRY_DELAY;
709 if (NULL != cc->task)
710 {
711 GNUNET_SCHEDULER_cancel(cc->task);
712 cc->task = NULL;
713 }
714 return;
717 } 715 }
718 return;
719 }
720 716
721 update_state (cc, cc->state, GNUNET_YES); 717 update_state(cc, cc->state, GNUNET_YES);
722 LOG (GNUNET_ERROR_TYPE_DEBUG, 718 LOG(GNUNET_ERROR_TYPE_DEBUG,
723 "Core MQ for %s became available in state %d\n", 719 "Core MQ for %s became available in state %d\n",
724 GCC_2s (cc), 720 GCC_2s(cc),
725 cc->state); 721 cc->state);
726 switch (cc->state) 722 switch (cc->state)
727 {
728 case CADET_CONNECTION_NEW:
729 /* Transmit immediately */
730 cc->task = GNUNET_SCHEDULER_add_at (cc->create_at, &send_create, cc);
731 break;
732 case CADET_CONNECTION_SENDING_CREATE:
733 /* Should not be possible to be called in this state. */
734 GNUNET_assert (0);
735 break;
736 case CADET_CONNECTION_SENT:
737 /* Retry a bit later... */
738 cc->task = GNUNET_SCHEDULER_add_at (cc->create_at, &send_create, cc);
739 break;
740 case CADET_CONNECTION_CREATE_RECEIVED:
741 /* We got the 'CREATE' (incoming connection), should send the CREATE_ACK */
742 cc->metrics.age = GNUNET_TIME_absolute_get ();
743 cc->task =
744 GNUNET_SCHEDULER_add_at (cc->create_ack_at, &send_create_ack, cc);
745 break;
746 case CADET_CONNECTION_READY:
747 if ((NULL == cc->keepalive_qe) && (GNUNET_YES == cc->mqm_ready) &&
748 (NULL == cc->task))
749 { 723 {
750 LOG (GNUNET_ERROR_TYPE_DEBUG, 724 case CADET_CONNECTION_NEW:
751 "Scheduling keepalive for %s in %s\n", 725 /* Transmit immediately */
752 GCC_2s (cc), 726 cc->task = GNUNET_SCHEDULER_add_at(cc->create_at, &send_create, cc);
753 GNUNET_STRINGS_relative_time_to_string (keepalive_period, 727 break;
754 GNUNET_YES)); 728
729 case CADET_CONNECTION_SENDING_CREATE:
730 /* Should not be possible to be called in this state. */
731 GNUNET_assert(0);
732 break;
733
734 case CADET_CONNECTION_SENT:
735 /* Retry a bit later... */
736 cc->task = GNUNET_SCHEDULER_add_at(cc->create_at, &send_create, cc);
737 break;
738
739 case CADET_CONNECTION_CREATE_RECEIVED:
740 /* We got the 'CREATE' (incoming connection), should send the CREATE_ACK */
741 cc->metrics.age = GNUNET_TIME_absolute_get();
755 cc->task = 742 cc->task =
756 GNUNET_SCHEDULER_add_delayed (keepalive_period, &send_keepalive, cc); 743 GNUNET_SCHEDULER_add_at(cc->create_ack_at, &send_create_ack, cc);
744 break;
745
746 case CADET_CONNECTION_READY:
747 if ((NULL == cc->keepalive_qe) && (GNUNET_YES == cc->mqm_ready) &&
748 (NULL == cc->task))
749 {
750 LOG(GNUNET_ERROR_TYPE_DEBUG,
751 "Scheduling keepalive for %s in %s\n",
752 GCC_2s(cc),
753 GNUNET_STRINGS_relative_time_to_string(keepalive_period,
754 GNUNET_YES));
755 cc->task =
756 GNUNET_SCHEDULER_add_delayed(keepalive_period, &send_keepalive, cc);
757 }
758 break;
757 } 759 }
758 break;
759 }
760} 760}
761 761
762 762
@@ -775,44 +775,44 @@ manage_first_hop_mq (void *cls, int available)
775 * @return handle to the connection 775 * @return handle to the connection
776 */ 776 */
777static struct CadetConnection * 777static struct CadetConnection *
778connection_create (struct CadetPeer *destination, 778connection_create(struct CadetPeer *destination,
779 struct CadetPeerPath *path, 779 struct CadetPeerPath *path,
780 unsigned int off, 780 unsigned int off,
781 struct CadetTConnection *ct, 781 struct CadetTConnection *ct,
782 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid, 782 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
783 enum CadetConnectionState init_state, 783 enum CadetConnectionState init_state,
784 GCC_ReadyCallback ready_cb, 784 GCC_ReadyCallback ready_cb,
785 void *ready_cb_cls) 785 void *ready_cb_cls)
786{ 786{
787 struct CadetConnection *cc; 787 struct CadetConnection *cc;
788 struct CadetPeer *first_hop; 788 struct CadetPeer *first_hop;
789 789
790 cc = GNUNET_new (struct CadetConnection); 790 cc = GNUNET_new(struct CadetConnection);
791 cc->state = init_state; 791 cc->state = init_state;
792 cc->ct = ct; 792 cc->ct = ct;
793 cc->cid = *cid; 793 cc->cid = *cid;
794 cc->retry_delay = 794 cc->retry_delay =
795 GNUNET_TIME_relative_multiply (INITIAL_CONNECTION_CREATE_RETRY_DELAY, off); 795 GNUNET_TIME_relative_multiply(INITIAL_CONNECTION_CREATE_RETRY_DELAY, off);
796 GNUNET_assert (GNUNET_OK == 796 GNUNET_assert(GNUNET_OK ==
797 GNUNET_CONTAINER_multishortmap_put ( 797 GNUNET_CONTAINER_multishortmap_put(
798 connections, 798 connections,
799 &GCC_get_id (cc)->connection_of_tunnel, 799 &GCC_get_id(cc)->connection_of_tunnel,
800 cc, 800 cc,
801 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 801 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
802 cc->ready_cb = ready_cb; 802 cc->ready_cb = ready_cb;
803 cc->ready_cb_cls = ready_cb_cls; 803 cc->ready_cb_cls = ready_cb_cls;
804 cc->path = path; 804 cc->path = path;
805 cc->off = off; 805 cc->off = off;
806 LOG (GNUNET_ERROR_TYPE_DEBUG, 806 LOG(GNUNET_ERROR_TYPE_DEBUG,
807 "Creating %s using path %s (offset: %u)\n", 807 "Creating %s using path %s (offset: %u)\n",
808 GCC_2s (cc), 808 GCC_2s(cc),
809 GCPP_2s (path), 809 GCPP_2s(path),
810 off); 810 off);
811 GCPP_add_connection (path, off, cc); 811 GCPP_add_connection(path, off, cc);
812 for (unsigned int i = 0; i < off; i++) 812 for (unsigned int i = 0; i < off; i++)
813 GCP_add_connection (GCPP_get_peer_at_offset (path, i), cc); 813 GCP_add_connection(GCPP_get_peer_at_offset(path, i), cc);
814 first_hop = GCPP_get_peer_at_offset (path, 0); 814 first_hop = GCPP_get_peer_at_offset(path, 0);
815 cc->mq_man = GCP_request_mq (first_hop, &manage_first_hop_mq, cc); 815 cc->mq_man = GCP_request_mq(first_hop, &manage_first_hop_mq, cc);
816 return cc; 816 return cc;
817} 817}
818 818
@@ -831,64 +831,64 @@ connection_create (struct CadetPeer *destination,
831 * a connection that takes precedence on @a path 831 * a connection that takes precedence on @a path
832 */ 832 */
833struct CadetConnection * 833struct CadetConnection *
834GCC_create_inbound (struct CadetPeer *destination, 834GCC_create_inbound(struct CadetPeer *destination,
835 struct CadetPeerPath *path, 835 struct CadetPeerPath *path,
836 struct CadetTConnection *ct, 836 struct CadetTConnection *ct,
837 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid, 837 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
838 GCC_ReadyCallback ready_cb, 838 GCC_ReadyCallback ready_cb,
839 void *ready_cb_cls) 839 void *ready_cb_cls)
840{ 840{
841 struct CadetConnection *cc; 841 struct CadetConnection *cc;
842 unsigned int off; 842 unsigned int off;
843 843
844 off = GCPP_find_peer (path, destination); 844 off = GCPP_find_peer(path, destination);
845 GNUNET_assert (UINT_MAX != off); 845 GNUNET_assert(UINT_MAX != off);
846 cc = GCPP_get_connection (path, destination, off); 846 cc = GCPP_get_connection(path, destination, off);
847 if (NULL != cc) 847 if (NULL != cc)
848 {
849 int cmp;
850
851 cmp = GNUNET_memcmp (cid, &cc->cid);
852 if (0 == cmp)
853 {
854 /* Two peers picked the SAME random connection identifier at the
855 same time for the same path? Must be malicious. Drop
856 connection (existing and inbound), even if it is the only
857 one. */
858 GNUNET_break_op (0);
859 GCT_connection_lost (cc->ct);
860 GCC_destroy_without_tunnel (cc);
861 return NULL;
862 }
863 if (0 < cmp)
864 { 848 {
865 /* drop existing */ 849 int cmp;
866 LOG (GNUNET_ERROR_TYPE_DEBUG, 850
867 "Got two connections on %s, dropping my existing %s\n", 851 cmp = GNUNET_memcmp(cid, &cc->cid);
868 GCPP_2s (path), 852 if (0 == cmp)
869 GCC_2s (cc)); 853 {
870 GCT_connection_lost (cc->ct); 854 /* Two peers picked the SAME random connection identifier at the
871 GCC_destroy_without_tunnel (cc); 855 same time for the same path? Must be malicious. Drop
856 connection (existing and inbound), even if it is the only
857 one. */
858 GNUNET_break_op(0);
859 GCT_connection_lost(cc->ct);
860 GCC_destroy_without_tunnel(cc);
861 return NULL;
862 }
863 if (0 < cmp)
864 {
865 /* drop existing */
866 LOG(GNUNET_ERROR_TYPE_DEBUG,
867 "Got two connections on %s, dropping my existing %s\n",
868 GCPP_2s(path),
869 GCC_2s(cc));
870 GCT_connection_lost(cc->ct);
871 GCC_destroy_without_tunnel(cc);
872 }
873 else
874 {
875 /* keep existing */
876 LOG(GNUNET_ERROR_TYPE_DEBUG,
877 "Got two connections on %s, keeping my existing %s\n",
878 GCPP_2s(path),
879 GCC_2s(cc));
880 return NULL;
881 }
872 } 882 }
873 else 883
874 { 884 return connection_create(destination,
875 /* keep existing */ 885 path,
876 LOG (GNUNET_ERROR_TYPE_DEBUG, 886 off,
877 "Got two connections on %s, keeping my existing %s\n", 887 ct,
878 GCPP_2s (path), 888 cid,
879 GCC_2s (cc)); 889 CADET_CONNECTION_CREATE_RECEIVED,
880 return NULL; 890 ready_cb,
881 } 891 ready_cb_cls);
882 }
883
884 return connection_create (destination,
885 path,
886 off,
887 ct,
888 cid,
889 CADET_CONNECTION_CREATE_RECEIVED,
890 ready_cb,
891 ready_cb_cls);
892} 892}
893 893
894 894
@@ -905,24 +905,24 @@ GCC_create_inbound (struct CadetPeer *destination,
905 * @return handle to the connection 905 * @return handle to the connection
906 */ 906 */
907struct CadetConnection * 907struct CadetConnection *
908GCC_create (struct CadetPeer *destination, 908GCC_create(struct CadetPeer *destination,
909 struct CadetPeerPath *path, 909 struct CadetPeerPath *path,
910 unsigned int off, 910 unsigned int off,
911 struct CadetTConnection *ct, 911 struct CadetTConnection *ct,
912 GCC_ReadyCallback ready_cb, 912 GCC_ReadyCallback ready_cb,
913 void *ready_cb_cls) 913 void *ready_cb_cls)
914{ 914{
915 struct GNUNET_CADET_ConnectionTunnelIdentifier cid; 915 struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
916 916
917 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE, &cid, sizeof (cid)); 917 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_NONCE, &cid, sizeof(cid));
918 return connection_create (destination, 918 return connection_create(destination,
919 path, 919 path,
920 off, 920 off,
921 ct, 921 ct,
922 &cid, 922 &cid,
923 CADET_CONNECTION_NEW, 923 CADET_CONNECTION_NEW,
924 ready_cb, 924 ready_cb,
925 ready_cb_cls); 925 ready_cb_cls);
926} 926}
927 927
928 928
@@ -937,21 +937,21 @@ GCC_create (struct CadetPeer *destination,
937 * yet have a #GNUNET_MQ_notify_sent() callback attached to it 937 * yet have a #GNUNET_MQ_notify_sent() callback attached to it
938 */ 938 */
939void 939void
940GCC_transmit (struct CadetConnection *cc, struct GNUNET_MQ_Envelope *env) 940GCC_transmit(struct CadetConnection *cc, struct GNUNET_MQ_Envelope *env)
941{ 941{
942 LOG (GNUNET_ERROR_TYPE_DEBUG, 942 LOG(GNUNET_ERROR_TYPE_DEBUG,
943 "Scheduling message for transmission on %s\n", 943 "Scheduling message for transmission on %s\n",
944 GCC_2s (cc)); 944 GCC_2s(cc));
945 GNUNET_assert (GNUNET_YES == cc->mqm_ready); 945 GNUNET_assert(GNUNET_YES == cc->mqm_ready);
946 GNUNET_assert (CADET_CONNECTION_READY == cc->state); 946 GNUNET_assert(CADET_CONNECTION_READY == cc->state);
947 cc->metrics.last_use = GNUNET_TIME_absolute_get (); 947 cc->metrics.last_use = GNUNET_TIME_absolute_get();
948 cc->mqm_ready = GNUNET_NO; 948 cc->mqm_ready = GNUNET_NO;
949 if (NULL != cc->task) 949 if (NULL != cc->task)
950 { 950 {
951 GNUNET_SCHEDULER_cancel (cc->task); 951 GNUNET_SCHEDULER_cancel(cc->task);
952 cc->task = NULL; 952 cc->task = NULL;
953 } 953 }
954 GCP_send (cc->mq_man, env); 954 GCP_send(cc->mq_man, env);
955} 955}
956 956
957 957
@@ -963,7 +963,7 @@ GCC_transmit (struct CadetConnection *cc, struct GNUNET_MQ_Envelope *env)
963 * @return path to @a cc 963 * @return path to @a cc
964 */ 964 */
965struct CadetPeerPath * 965struct CadetPeerPath *
966GCC_get_path (struct CadetConnection *cc, unsigned int *off) 966GCC_get_path(struct CadetConnection *cc, unsigned int *off)
967{ 967{
968 *off = cc->off; 968 *off = cc->off;
969 return cc->path; 969 return cc->path;
@@ -977,7 +977,7 @@ GCC_get_path (struct CadetConnection *cc, unsigned int *off)
977 * @return unique number of the connection 977 * @return unique number of the connection
978 */ 978 */
979const struct GNUNET_CADET_ConnectionTunnelIdentifier * 979const struct GNUNET_CADET_ConnectionTunnelIdentifier *
980GCC_get_id (struct CadetConnection *cc) 980GCC_get_id(struct CadetConnection *cc)
981{ 981{
982 return &cc->cid; 982 return &cc->cid;
983} 983}
@@ -989,7 +989,7 @@ GCC_get_id (struct CadetConnection *cc)
989 * @param cc Connection. 989 * @param cc Connection.
990 */ 990 */
991const char * 991const char *
992GCC_2s (const struct CadetConnection *cc) 992GCC_2s(const struct CadetConnection *cc)
993{ 993{
994 static char buf[128]; 994 static char buf[128];
995 995
@@ -997,24 +997,24 @@ GCC_2s (const struct CadetConnection *cc)
997 return "Connection(NULL)"; 997 return "Connection(NULL)";
998 998
999 if (NULL != cc->ct) 999 if (NULL != cc->ct)
1000 { 1000 {
1001 GNUNET_snprintf (buf, 1001 GNUNET_snprintf(buf,
1002 sizeof (buf), 1002 sizeof(buf),
1003 "Connection %s (%s)", 1003 "Connection %s (%s)",
1004 GNUNET_sh2s (&cc->cid.connection_of_tunnel), 1004 GNUNET_sh2s(&cc->cid.connection_of_tunnel),
1005 GCT_2s (cc->ct->t)); 1005 GCT_2s(cc->ct->t));
1006 return buf; 1006 return buf;
1007 } 1007 }
1008 GNUNET_snprintf (buf, 1008 GNUNET_snprintf(buf,
1009 sizeof (buf), 1009 sizeof(buf),
1010 "Connection %s", 1010 "Connection %s",
1011 GNUNET_sh2s (&cc->cid.connection_of_tunnel)); 1011 GNUNET_sh2s(&cc->cid.connection_of_tunnel));
1012 return buf; 1012 return buf;
1013} 1013}
1014 1014
1015 1015
1016#define LOG2(level, ...) \ 1016#define LOG2(level, ...) \
1017 GNUNET_log_from_nocheck (level, "cadet-con", __VA_ARGS__) 1017 GNUNET_log_from_nocheck(level, "cadet-con", __VA_ARGS__)
1018 1018
1019 1019
1020/** 1020/**
@@ -1024,30 +1024,30 @@ GCC_2s (const struct CadetConnection *cc)
1024 * @param level Debug level to use. 1024 * @param level Debug level to use.
1025 */ 1025 */
1026void 1026void
1027GCC_debug (struct CadetConnection *cc, enum GNUNET_ErrorType level) 1027GCC_debug(struct CadetConnection *cc, enum GNUNET_ErrorType level)
1028{ 1028{
1029#if ! defined(GNUNET_CULL_LOGGING) 1029#if !defined(GNUNET_CULL_LOGGING)
1030 int do_log; 1030 int do_log;
1031 1031
1032 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK), 1032 do_log = GNUNET_get_log_call_status(level & (~GNUNET_ERROR_TYPE_BULK),
1033 "cadet-con", 1033 "cadet-con",
1034 __FILE__, 1034 __FILE__,
1035 __FUNCTION__, 1035 __FUNCTION__,
1036 __LINE__); 1036 __LINE__);
1037 if (0 == do_log) 1037 if (0 == do_log)
1038 return; 1038 return;
1039 if (NULL == cc) 1039 if (NULL == cc)
1040 { 1040 {
1041 LOG2 (level, "Connection (NULL)\n"); 1041 LOG2(level, "Connection (NULL)\n");
1042 return; 1042 return;
1043 } 1043 }
1044 LOG2 (level, 1044 LOG2(level,
1045 "%s to %s via path %s in state %d is %s\n", 1045 "%s to %s via path %s in state %d is %s\n",
1046 GCC_2s (cc), 1046 GCC_2s(cc),
1047 GCP_2s (cc->destination), 1047 GCP_2s(cc->destination),
1048 GCPP_2s (cc->path), 1048 GCPP_2s(cc->path),
1049 cc->state, 1049 cc->state,
1050 (GNUNET_YES == cc->mqm_ready) ? "ready" : "busy"); 1050 (GNUNET_YES == cc->mqm_ready) ? "ready" : "busy");
1051#endif 1051#endif
1052} 1052}
1053 1053
diff --git a/src/cadet/gnunet-service-cadet_connection.h b/src/cadet/gnunet-service-cadet_connection.h
index d3de3347c..47b654cbb 100644
--- a/src/cadet/gnunet-service-cadet_connection.h
+++ b/src/cadet/gnunet-service-cadet_connection.h
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file cadet/gnunet-service-cadet_connection.h 22 * @file cadet/gnunet-service-cadet_connection.h
@@ -57,7 +57,7 @@ typedef void
57 * @param cc connection to destroy 57 * @param cc connection to destroy
58 */ 58 */
59void 59void
60GCC_destroy_without_core (struct CadetConnection *cc); 60GCC_destroy_without_core(struct CadetConnection *cc);
61 61
62 62
63/** 63/**
@@ -68,7 +68,7 @@ GCC_destroy_without_core (struct CadetConnection *cc);
68 * @param cc connection to destroy 68 * @param cc connection to destroy
69 */ 69 */
70void 70void
71GCC_destroy_without_tunnel (struct CadetConnection *cc); 71GCC_destroy_without_tunnel(struct CadetConnection *cc);
72 72
73 73
74/** 74/**
@@ -78,7 +78,7 @@ GCC_destroy_without_tunnel (struct CadetConnection *cc);
78 * @return NULL if connection was not found 78 * @return NULL if connection was not found
79 */ 79 */
80struct CadetConnection * 80struct CadetConnection *
81GCC_lookup (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid); 81GCC_lookup(const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid);
82 82
83 83
84/** 84/**
@@ -94,12 +94,12 @@ GCC_lookup (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid);
94 * @return handle to the connection 94 * @return handle to the connection
95 */ 95 */
96struct CadetConnection * 96struct CadetConnection *
97GCC_create (struct CadetPeer *destination, 97GCC_create(struct CadetPeer *destination,
98 struct CadetPeerPath *path, 98 struct CadetPeerPath *path,
99 unsigned int off, 99 unsigned int off,
100 struct CadetTConnection *ct, 100 struct CadetTConnection *ct,
101 GCC_ReadyCallback ready_cb, 101 GCC_ReadyCallback ready_cb,
102 void *ready_cb_cls); 102 void *ready_cb_cls);
103 103
104 104
105/** 105/**
@@ -116,12 +116,12 @@ GCC_create (struct CadetPeer *destination,
116 * a connection that takes precedence on @a path 116 * a connection that takes precedence on @a path
117 */ 117 */
118struct CadetConnection * 118struct CadetConnection *
119GCC_create_inbound (struct CadetPeer *destination, 119GCC_create_inbound(struct CadetPeer *destination,
120 struct CadetPeerPath *path, 120 struct CadetPeerPath *path,
121 struct CadetTConnection *ct, 121 struct CadetTConnection *ct,
122 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid, 122 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
123 GCC_ReadyCallback ready_cb, 123 GCC_ReadyCallback ready_cb,
124 void *ready_cb_cls); 124 void *ready_cb_cls);
125 125
126 126
127/** 127/**
@@ -137,8 +137,8 @@ GCC_create_inbound (struct CadetPeer *destination,
137 * connection identifier of this connection... 137 * connection identifier of this connection...
138 */ 138 */
139void 139void
140GCC_transmit (struct CadetConnection *cc, 140GCC_transmit(struct CadetConnection *cc,
141 struct GNUNET_MQ_Envelope *env); 141 struct GNUNET_MQ_Envelope *env);
142 142
143 143
144/** 144/**
@@ -147,7 +147,7 @@ GCC_transmit (struct CadetConnection *cc,
147 * @param cc the connection that got the ACK. 147 * @param cc the connection that got the ACK.
148 */ 148 */
149void 149void
150GCC_handle_connection_create_ack (struct CadetConnection *cc); 150GCC_handle_connection_create_ack(struct CadetConnection *cc);
151 151
152 152
153/** 153/**
@@ -158,7 +158,7 @@ GCC_handle_connection_create_ack (struct CadetConnection *cc);
158 * @param cc connection that got the duplicate CREATE 158 * @param cc connection that got the duplicate CREATE
159 */ 159 */
160void 160void
161GCC_handle_duplicate_create (struct CadetConnection *cc); 161GCC_handle_duplicate_create(struct CadetConnection *cc);
162 162
163 163
164/** 164/**
@@ -168,8 +168,8 @@ GCC_handle_duplicate_create (struct CadetConnection *cc);
168 * @param msg the key exchange message 168 * @param msg the key exchange message
169 */ 169 */
170void 170void
171GCC_handle_kx (struct CadetConnection *cc, 171GCC_handle_kx(struct CadetConnection *cc,
172 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg); 172 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg);
173 173
174 174
175/** 175/**
@@ -179,16 +179,14 @@ GCC_handle_kx (struct CadetConnection *cc,
179 * @param msg the key exchange message 179 * @param msg the key exchange message
180 */ 180 */
181void 181void
182GCC_handle_kx_auth (struct CadetConnection *cc, 182GCC_handle_kx_auth(struct CadetConnection *cc,
183 const struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg); 183 const struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg);
184 184
185 185
186/** 186/**
187 * Performance metrics for a connection. 187 * Performance metrics for a connection.
188 */ 188 */
189struct CadetConnectionMetrics 189struct CadetConnectionMetrics {
190{
191
192 /** 190 /**
193 * Our current best estimate of the latency, based on a weighted 191 * Our current best estimate of the latency, based on a weighted
194 * average of at least @a latency_datapoints values. 192 * average of at least @a latency_datapoints values.
@@ -221,7 +219,6 @@ struct CadetConnectionMetrics
221 * connections.) 219 * connections.)
222 */ 220 */
223 unsigned long long num_successes; 221 unsigned long long num_successes;
224
225}; 222};
226 223
227 224
@@ -232,7 +229,7 @@ struct CadetConnectionMetrics
232 * @return the metrics 229 * @return the metrics
233 */ 230 */
234const struct CadetConnectionMetrics * 231const struct CadetConnectionMetrics *
235GCC_get_metrics (struct CadetConnection *cc); 232GCC_get_metrics(struct CadetConnection *cc);
236 233
237 234
238/** 235/**
@@ -242,8 +239,8 @@ GCC_get_metrics (struct CadetConnection *cc);
242 * @param msg the encrypted message to decrypt 239 * @param msg the encrypted message to decrypt
243 */ 240 */
244void 241void
245GCC_handle_encrypted (struct CadetConnection *cc, 242GCC_handle_encrypted(struct CadetConnection *cc,
246 const struct GNUNET_CADET_TunnelEncryptedMessage *msg); 243 const struct GNUNET_CADET_TunnelEncryptedMessage *msg);
247 244
248 245
249/** 246/**
@@ -253,7 +250,7 @@ GCC_handle_encrypted (struct CadetConnection *cc,
253 * @param cid connection identifier where we expect an ACK 250 * @param cid connection identifier where we expect an ACK
254 */ 251 */
255void 252void
256GCC_ack_expected (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid); 253GCC_ack_expected(const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid);
257 254
258 255
259/** 256/**
@@ -265,7 +262,7 @@ GCC_ack_expected (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid);
265 * may have gotten back to us via a different connection). 262 * may have gotten back to us via a different connection).
266 */ 263 */
267void 264void
268GCC_ack_observed (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid); 265GCC_ack_observed(const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid);
269 266
270 267
271/** 268/**
@@ -277,8 +274,8 @@ GCC_ack_observed (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid);
277 * @param latency the observed latency 274 * @param latency the observed latency
278 */ 275 */
279void 276void
280GCC_latency_observed (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti, 277GCC_latency_observed(const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
281 struct GNUNET_TIME_Relative latency); 278 struct GNUNET_TIME_Relative latency);
282 279
283 280
284/** 281/**
@@ -288,7 +285,7 @@ GCC_latency_observed (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cti,
288 * @return corresponding entry in the tunnel's connection list 285 * @return corresponding entry in the tunnel's connection list
289 */ 286 */
290struct CadetTConnection * 287struct CadetTConnection *
291GCC_get_ct (struct CadetConnection *cc); 288GCC_get_ct(struct CadetConnection *cc);
292 289
293 290
294/** 291/**
@@ -299,8 +296,8 @@ GCC_get_ct (struct CadetConnection *cc);
299 * @return path to @a cc 296 * @return path to @a cc
300 */ 297 */
301struct CadetPeerPath * 298struct CadetPeerPath *
302GCC_get_path (struct CadetConnection *cc, 299GCC_get_path(struct CadetConnection *cc,
303 unsigned int *off); 300 unsigned int *off);
304 301
305 302
306/** 303/**
@@ -310,7 +307,7 @@ GCC_get_path (struct CadetConnection *cc,
310 * @return unique number of the connection 307 * @return unique number of the connection
311 */ 308 */
312const struct GNUNET_CADET_ConnectionTunnelIdentifier * 309const struct GNUNET_CADET_ConnectionTunnelIdentifier *
313GCC_get_id (struct CadetConnection *cc); 310GCC_get_id(struct CadetConnection *cc);
314 311
315 312
316/** 313/**
@@ -319,7 +316,7 @@ GCC_get_id (struct CadetConnection *cc);
319 * @param cc Connection. 316 * @param cc Connection.
320 */ 317 */
321const char * 318const char *
322GCC_2s (const struct CadetConnection *cc); 319GCC_2s(const struct CadetConnection *cc);
323 320
324 321
325/** 322/**
@@ -329,8 +326,8 @@ GCC_2s (const struct CadetConnection *cc);
329 * @param level Debug level to use. 326 * @param level Debug level to use.
330 */ 327 */
331void 328void
332GCC_debug (struct CadetConnection *cc, 329GCC_debug(struct CadetConnection *cc,
333 enum GNUNET_ErrorType level); 330 enum GNUNET_ErrorType level);
334 331
335 332
336#endif 333#endif
diff --git a/src/cadet/gnunet-service-cadet_core.c b/src/cadet/gnunet-service-cadet_core.c
index ec70a968b..a08c80aae 100644
--- a/src/cadet/gnunet-service-cadet_core.c
+++ b/src/cadet/gnunet-service-cadet_core.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file cadet/gnunet-service-cadet_core.c 22 * @file cadet/gnunet-service-cadet_core.c
@@ -39,7 +39,7 @@
39#include "gnunet_statistics_service.h" 39#include "gnunet_statistics_service.h"
40#include "cadet_protocol.h" 40#include "cadet_protocol.h"
41 41
42#define LOG(level, ...) GNUNET_log_from (level, "cadet-cor", __VA_ARGS__) 42#define LOG(level, ...) GNUNET_log_from(level, "cadet-cor", __VA_ARGS__)
43 43
44/** 44/**
45 * Information we keep per direction for a route. 45 * Information we keep per direction for a route.
@@ -52,9 +52,7 @@ struct RouteDirection;
52 * routes that have the current maximum of messages in the buffer (in 52 * routes that have the current maximum of messages in the buffer (in
53 * case we have to purge). 53 * case we have to purge).
54 */ 54 */
55struct Rung 55struct Rung {
56{
57
58 /** 56 /**
59 * Rung of RouteDirections with one more buffer entry each. 57 * Rung of RouteDirections with one more buffer entry each.
60 */ 58 */
@@ -91,9 +89,7 @@ struct Rung
91/** 89/**
92 * Information we keep per direction for a route. 90 * Information we keep per direction for a route.
93 */ 91 */
94struct RouteDirection 92struct RouteDirection {
95{
96
97 /** 93 /**
98 * DLL of other route directions within the same `struct Rung`. 94 * DLL of other route directions within the same `struct Rung`.
99 */ 95 */
@@ -149,9 +145,7 @@ struct RouteDirection
149 * the complete `struct CadetPath` that is formed by the individual 145 * the complete `struct CadetPath` that is formed by the individual
150 * routes. 146 * routes.
151 */ 147 */
152struct CadetRoute 148struct CadetRoute {
153{
154
155 /** 149 /**
156 * Information about the next hop on this route. 150 * Information about the next hop on this route.
157 */ 151 */
@@ -237,10 +231,10 @@ static struct GNUNET_SCHEDULER_Task *timeout_task;
237 * @param cid hash generated from the connection identifier 231 * @param cid hash generated from the connection identifier
238 */ 232 */
239static struct CadetRoute * 233static struct CadetRoute *
240get_route (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid) 234get_route(const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
241{ 235{
242 return GNUNET_CONTAINER_multishortmap_get (routes, 236 return GNUNET_CONTAINER_multishortmap_get(routes,
243 &cid->connection_of_tunnel); 237 &cid->connection_of_tunnel);
244} 238}
245 239
246 240
@@ -250,22 +244,22 @@ get_route (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
250 * @param dir direction to lower in rung. 244 * @param dir direction to lower in rung.
251 */ 245 */
252static void 246static void
253lower_rung (struct RouteDirection *dir) 247lower_rung(struct RouteDirection *dir)
254{ 248{
255 struct Rung *rung = dir->rung; 249 struct Rung *rung = dir->rung;
256 struct Rung *prev; 250 struct Rung *prev;
257 251
258 GNUNET_CONTAINER_DLL_remove (rung->rd_head, rung->rd_tail, dir); 252 GNUNET_CONTAINER_DLL_remove(rung->rd_head, rung->rd_tail, dir);
259 prev = rung->prev; 253 prev = rung->prev;
260 GNUNET_assert (NULL != prev); 254 GNUNET_assert(NULL != prev);
261 if (prev->rung_off != rung->rung_off - 1) 255 if (prev->rung_off != rung->rung_off - 1)
262 { 256 {
263 prev = GNUNET_new (struct Rung); 257 prev = GNUNET_new(struct Rung);
264 prev->rung_off = rung->rung_off - 1; 258 prev->rung_off = rung->rung_off - 1;
265 GNUNET_CONTAINER_DLL_insert_after (rung_head, rung_tail, rung->prev, prev); 259 GNUNET_CONTAINER_DLL_insert_after(rung_head, rung_tail, rung->prev, prev);
266 } 260 }
267 GNUNET_assert (NULL != prev); 261 GNUNET_assert(NULL != prev);
268 GNUNET_CONTAINER_DLL_insert (prev->rd_head, prev->rd_tail, dir); 262 GNUNET_CONTAINER_DLL_insert(prev->rd_head, prev->rd_tail, dir);
269 dir->rung = prev; 263 dir->rung = prev;
270} 264}
271 265
@@ -278,13 +272,13 @@ lower_rung (struct RouteDirection *dir)
278 * @param env envelope to discard 272 * @param env envelope to discard
279 */ 273 */
280static void 274static void
281discard_buffer (struct RouteDirection *dir, struct GNUNET_MQ_Envelope *env) 275discard_buffer(struct RouteDirection *dir, struct GNUNET_MQ_Envelope *env)
282{ 276{
283 GNUNET_MQ_dll_remove (&dir->env_head, &dir->env_tail, env); 277 GNUNET_MQ_dll_remove(&dir->env_head, &dir->env_tail, env);
284 cur_buffers--; 278 cur_buffers--;
285 GNUNET_MQ_discard (env); 279 GNUNET_MQ_discard(env);
286 lower_rung (dir); 280 lower_rung(dir);
287 GNUNET_STATISTICS_set (stats, "# buffer use", cur_buffers, GNUNET_NO); 281 GNUNET_STATISTICS_set(stats, "# buffer use", cur_buffers, GNUNET_NO);
288} 282}
289 283
290 284
@@ -292,24 +286,24 @@ discard_buffer (struct RouteDirection *dir, struct GNUNET_MQ_Envelope *env)
292 * Discard all messages from the highest rung, to make space. 286 * Discard all messages from the highest rung, to make space.
293 */ 287 */
294static void 288static void
295discard_all_from_rung_tail () 289discard_all_from_rung_tail()
296{ 290{
297 struct Rung *tail = rung_tail; 291 struct Rung *tail = rung_tail;
298 struct RouteDirection *dir; 292 struct RouteDirection *dir;
299 293
300 while (NULL != (dir = tail->rd_head)) 294 while (NULL != (dir = tail->rd_head))
301 { 295 {
302 LOG (GNUNET_ERROR_TYPE_DEBUG, 296 LOG(GNUNET_ERROR_TYPE_DEBUG,
303 "Queue full due new message %s on connection %s, dropping old message\n", 297 "Queue full due new message %s on connection %s, dropping old message\n",
304 GNUNET_sh2s (&dir->my_route->cid.connection_of_tunnel)); 298 GNUNET_sh2s(&dir->my_route->cid.connection_of_tunnel));
305 GNUNET_STATISTICS_update (stats, 299 GNUNET_STATISTICS_update(stats,
306 "# messages dropped due to full buffer", 300 "# messages dropped due to full buffer",
307 1, 301 1,
308 GNUNET_NO); 302 GNUNET_NO);
309 discard_buffer (dir, dir->env_head); 303 discard_buffer(dir, dir->env_head);
310 } 304 }
311 GNUNET_CONTAINER_DLL_remove (rung_head, rung_tail, tail); 305 GNUNET_CONTAINER_DLL_remove(rung_head, rung_tail, tail);
312 GNUNET_free (tail); 306 GNUNET_free(tail);
313} 307}
314 308
315 309
@@ -323,10 +317,10 @@ discard_all_from_rung_tail ()
323 * @param msg the message to forward 317 * @param msg the message to forward
324 */ 318 */
325static void 319static void
326route_message (struct CadetPeer *prev, 320route_message(struct CadetPeer *prev,
327 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid, 321 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
328 const struct GNUNET_MessageHeader *msg, 322 const struct GNUNET_MessageHeader *msg,
329 const enum GNUNET_MQ_PriorityPreferences priority) 323 const enum GNUNET_MQ_PriorityPreferences priority)
330{ 324{
331 struct CadetRoute *route; 325 struct CadetRoute *route;
332 struct RouteDirection *dir; 326 struct RouteDirection *dir;
@@ -334,133 +328,133 @@ route_message (struct CadetPeer *prev,
334 struct Rung *nxt; 328 struct Rung *nxt;
335 struct GNUNET_MQ_Envelope *env; 329 struct GNUNET_MQ_Envelope *env;
336 330
337 route = get_route (cid); 331 route = get_route(cid);
338 if (NULL == route) 332 if (NULL == route)
339 {
340 struct GNUNET_MQ_Envelope *env;
341 struct GNUNET_CADET_ConnectionBrokenMessage *bm;
342
343 LOG (GNUNET_ERROR_TYPE_DEBUG,
344 "Failed to route message of type %u from %s on connection %s: no route\n",
345 ntohs (msg->type),
346 GCP_2s (prev),
347 GNUNET_sh2s (&cid->connection_of_tunnel));
348 switch (ntohs (msg->type))
349 { 333 {
350 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY: 334 struct GNUNET_MQ_Envelope *env;
351 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN: 335 struct GNUNET_CADET_ConnectionBrokenMessage *bm;
352 /* No need to respond to these! */ 336
337 LOG(GNUNET_ERROR_TYPE_DEBUG,
338 "Failed to route message of type %u from %s on connection %s: no route\n",
339 ntohs(msg->type),
340 GCP_2s(prev),
341 GNUNET_sh2s(&cid->connection_of_tunnel));
342 switch (ntohs(msg->type))
343 {
344 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
345 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
346 /* No need to respond to these! */
347 return;
348 }
349 env = GNUNET_MQ_msg(bm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
350 bm->cid = *cid;
351 bm->peer1 = my_full_id;
352 GCP_send_ooo(prev, env);
353 return; 353 return;
354 } 354 }
355 env = GNUNET_MQ_msg (bm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN); 355 route->last_use = GNUNET_TIME_absolute_get();
356 bm->cid = *cid; 356 GNUNET_CONTAINER_heap_update_cost(route->hn, route->last_use.abs_value_us);
357 bm->peer1 = my_full_id;
358 GCP_send_ooo (prev, env);
359 return;
360 }
361 route->last_use = GNUNET_TIME_absolute_get ();
362 GNUNET_CONTAINER_heap_update_cost (route->hn, route->last_use.abs_value_us);
363 dir = (prev == route->prev.hop) ? &route->next : &route->prev; 357 dir = (prev == route->prev.hop) ? &route->next : &route->prev;
364 if (GNUNET_YES == dir->is_ready) 358 if (GNUNET_YES == dir->is_ready)
365 { 359 {
366 LOG (GNUNET_ERROR_TYPE_DEBUG, 360 LOG(GNUNET_ERROR_TYPE_DEBUG,
367 "Routing message of type %u from %s to %s on connection %s\n", 361 "Routing message of type %u from %s to %s on connection %s\n",
368 ntohs (msg->type), 362 ntohs(msg->type),
369 GCP_2s (prev), 363 GCP_2s(prev),
370 GNUNET_i2s (GCP_get_id (dir->hop)), 364 GNUNET_i2s(GCP_get_id(dir->hop)),
371 GNUNET_sh2s (&cid->connection_of_tunnel)); 365 GNUNET_sh2s(&cid->connection_of_tunnel));
372 dir->is_ready = GNUNET_NO; 366 dir->is_ready = GNUNET_NO;
373 GCP_send (dir->mqm, GNUNET_MQ_msg_copy (msg)); 367 GCP_send(dir->mqm, GNUNET_MQ_msg_copy(msg));
374 return; 368 return;
375 } 369 }
376 /* Check if low latency is required and if the previous message was 370 /* Check if low latency is required and if the previous message was
377 unreliable; if so, make sure we only queue one message per 371 unreliable; if so, make sure we only queue one message per
378 direction (no buffering). */ 372 direction (no buffering). */
379 if ((0 != (priority & GNUNET_MQ_PREF_LOW_LATENCY)) && 373 if ((0 != (priority & GNUNET_MQ_PREF_LOW_LATENCY)) &&
380 (NULL != dir->env_head) && 374 (NULL != dir->env_head) &&
381 (0 == 375 (0 ==
382 (GNUNET_MQ_env_get_options (dir->env_head) & GNUNET_MQ_PREF_UNRELIABLE))) 376 (GNUNET_MQ_env_get_options(dir->env_head) & GNUNET_MQ_PREF_UNRELIABLE)))
383 discard_buffer (dir, dir->env_head); 377 discard_buffer(dir, dir->env_head);
384 /* Check for duplicates */ 378 /* Check for duplicates */
385 for (const struct GNUNET_MQ_Envelope *env = dir->env_head; NULL != env; 379 for (const struct GNUNET_MQ_Envelope *env = dir->env_head; NULL != env;
386 env = GNUNET_MQ_env_next (env)) 380 env = GNUNET_MQ_env_next(env))
387 {
388 const struct GNUNET_MessageHeader *hdr = GNUNET_MQ_env_get_msg (env);
389
390 if ((hdr->size == msg->size) && (0 == memcmp (hdr, msg, ntohs (msg->size))))
391 { 381 {
392 LOG (GNUNET_ERROR_TYPE_DEBUG, 382 const struct GNUNET_MessageHeader *hdr = GNUNET_MQ_env_get_msg(env);
393 "Received duplicate of message already in buffer, dropping\n"); 383
394 GNUNET_STATISTICS_update (stats, 384 if ((hdr->size == msg->size) && (0 == memcmp(hdr, msg, ntohs(msg->size))))
395 "# messages dropped due to duplicate in buffer", 385 {
396 1, 386 LOG(GNUNET_ERROR_TYPE_DEBUG,
397 GNUNET_NO); 387 "Received duplicate of message already in buffer, dropping\n");
398 return; 388 GNUNET_STATISTICS_update(stats,
389 "# messages dropped due to duplicate in buffer",
390 1,
391 GNUNET_NO);
392 return;
393 }
399 } 394 }
400 }
401 395
402 rung = dir->rung; 396 rung = dir->rung;
403 if (cur_buffers == max_buffers) 397 if (cur_buffers == max_buffers)
404 {
405 /* Need to make room. */
406 if (NULL != rung->next)
407 {
408 /* Easy case, drop messages from route directions in highest rung */
409 discard_all_from_rung_tail ();
410 }
411 else
412 { 398 {
413 /* We are in the highest rung, drop our own! */ 399 /* Need to make room. */
414 LOG (GNUNET_ERROR_TYPE_DEBUG, 400 if (NULL != rung->next)
415 "Queue full due new message %s on connection %s, dropping old message\n", 401 {
416 GNUNET_sh2s (&dir->my_route->cid.connection_of_tunnel)); 402 /* Easy case, drop messages from route directions in highest rung */
417 GNUNET_STATISTICS_update (stats, 403 discard_all_from_rung_tail();
418 "# messages dropped due to full buffer", 404 }
419 1, 405 else
420 GNUNET_NO); 406 {
421 discard_buffer (dir, dir->env_head); 407 /* We are in the highest rung, drop our own! */
422 rung = dir->rung; 408 LOG(GNUNET_ERROR_TYPE_DEBUG,
409 "Queue full due new message %s on connection %s, dropping old message\n",
410 GNUNET_sh2s(&dir->my_route->cid.connection_of_tunnel));
411 GNUNET_STATISTICS_update(stats,
412 "# messages dropped due to full buffer",
413 1,
414 GNUNET_NO);
415 discard_buffer(dir, dir->env_head);
416 rung = dir->rung;
417 }
423 } 418 }
424 }
425 /* remove 'dir' from current rung */ 419 /* remove 'dir' from current rung */
426 GNUNET_CONTAINER_DLL_remove (rung->rd_head, rung->rd_tail, dir); 420 GNUNET_CONTAINER_DLL_remove(rung->rd_head, rung->rd_tail, dir);
427 /* make 'nxt' point to the next higher rung, create if necessary */ 421 /* make 'nxt' point to the next higher rung, create if necessary */
428 nxt = rung->next; 422 nxt = rung->next;
429 if ((NULL == nxt) || (rung->rung_off + 1 != nxt->rung_off)) 423 if ((NULL == nxt) || (rung->rung_off + 1 != nxt->rung_off))
430 { 424 {
431 nxt = GNUNET_new (struct Rung); 425 nxt = GNUNET_new(struct Rung);
432 nxt->rung_off = rung->rung_off + 1; 426 nxt->rung_off = rung->rung_off + 1;
433 GNUNET_CONTAINER_DLL_insert_after (rung_head, rung_tail, rung, nxt); 427 GNUNET_CONTAINER_DLL_insert_after(rung_head, rung_tail, rung, nxt);
434 } 428 }
435 /* insert 'dir' into next higher rung */ 429 /* insert 'dir' into next higher rung */
436 GNUNET_CONTAINER_DLL_insert (nxt->rd_head, nxt->rd_tail, dir); 430 GNUNET_CONTAINER_DLL_insert(nxt->rd_head, nxt->rd_tail, dir);
437 dir->rung = nxt; 431 dir->rung = nxt;
438 432
439 /* add message into 'dir' buffer */ 433 /* add message into 'dir' buffer */
440 LOG (GNUNET_ERROR_TYPE_DEBUG, 434 LOG(GNUNET_ERROR_TYPE_DEBUG,
441 "Queueing new message of type %u from %s to %s on connection %s\n", 435 "Queueing new message of type %u from %s to %s on connection %s\n",
442 ntohs (msg->type), 436 ntohs(msg->type),
443 GCP_2s (prev), 437 GCP_2s(prev),
444 GNUNET_i2s (GCP_get_id (dir->hop)), 438 GNUNET_i2s(GCP_get_id(dir->hop)),
445 GNUNET_sh2s (&cid->connection_of_tunnel)); 439 GNUNET_sh2s(&cid->connection_of_tunnel));
446 env = GNUNET_MQ_msg_copy (msg); 440 env = GNUNET_MQ_msg_copy(msg);
447 GNUNET_MQ_env_set_options (env, priority); 441 GNUNET_MQ_env_set_options(env, priority);
448 if ((0 != (priority & GNUNET_MQ_PREF_LOW_LATENCY)) && 442 if ((0 != (priority & GNUNET_MQ_PREF_LOW_LATENCY)) &&
449 (0 != (priority & GNUNET_MQ_PREF_OUT_OF_ORDER)) && 443 (0 != (priority & GNUNET_MQ_PREF_OUT_OF_ORDER)) &&
450 (NULL != dir->env_head) && 444 (NULL != dir->env_head) &&
451 (0 == (GNUNET_MQ_env_get_options (dir->env_head) & 445 (0 == (GNUNET_MQ_env_get_options(dir->env_head) &
452 GNUNET_MQ_PREF_LOW_LATENCY))) 446 GNUNET_MQ_PREF_LOW_LATENCY)))
453 GNUNET_MQ_dll_insert_head (&dir->env_head, &dir->env_tail, env); 447 GNUNET_MQ_dll_insert_head(&dir->env_head, &dir->env_tail, env);
454 else 448 else
455 GNUNET_MQ_dll_insert_tail (&dir->env_head, &dir->env_tail, env); 449 GNUNET_MQ_dll_insert_tail(&dir->env_head, &dir->env_tail, env);
456 cur_buffers++; 450 cur_buffers++;
457 GNUNET_STATISTICS_set (stats, "# buffer use", cur_buffers, GNUNET_NO); 451 GNUNET_STATISTICS_set(stats, "# buffer use", cur_buffers, GNUNET_NO);
458 /* Clean up 'rung' if now empty (and not head) */ 452 /* Clean up 'rung' if now empty (and not head) */
459 if ((NULL == rung->rd_head) && (rung != rung_head)) 453 if ((NULL == rung->rd_head) && (rung != rung_head))
460 { 454 {
461 GNUNET_CONTAINER_DLL_remove (rung_head, rung_tail, rung); 455 GNUNET_CONTAINER_DLL_remove(rung_head, rung_tail, rung);
462 GNUNET_free (rung); 456 GNUNET_free(rung);
463 } 457 }
464} 458}
465 459
466 460
@@ -473,16 +467,16 @@ route_message (struct CadetPeer *prev,
473 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise. 467 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
474 */ 468 */
475static int 469static int
476check_connection_create (void *cls, 470check_connection_create(void *cls,
477 const struct GNUNET_CADET_ConnectionCreateMessage *msg) 471 const struct GNUNET_CADET_ConnectionCreateMessage *msg)
478{ 472{
479 uint16_t size = ntohs (msg->header.size) - sizeof (*msg); 473 uint16_t size = ntohs(msg->header.size) - sizeof(*msg);
480 474
481 if (0 != (size % sizeof (struct GNUNET_PeerIdentity))) 475 if (0 != (size % sizeof(struct GNUNET_PeerIdentity)))
482 { 476 {
483 GNUNET_break_op (0); 477 GNUNET_break_op(0);
484 return GNUNET_NO; 478 return GNUNET_NO;
485 } 479 }
486 return GNUNET_YES; 480 return GNUNET_YES;
487} 481}
488 482
@@ -493,24 +487,24 @@ check_connection_create (void *cls,
493 * @param dir direction to destroy (do NOT free memory of 'dir' itself) 487 * @param dir direction to destroy (do NOT free memory of 'dir' itself)
494 */ 488 */
495static void 489static void
496destroy_direction (struct RouteDirection *dir) 490destroy_direction(struct RouteDirection *dir)
497{ 491{
498 struct GNUNET_MQ_Envelope *env; 492 struct GNUNET_MQ_Envelope *env;
499 493
500 while (NULL != (env = dir->env_head)) 494 while (NULL != (env = dir->env_head))
501 { 495 {
502 GNUNET_STATISTICS_update (stats, 496 GNUNET_STATISTICS_update(stats,
503 "# messages dropped due to route destruction", 497 "# messages dropped due to route destruction",
504 1, 498 1,
505 GNUNET_NO); 499 GNUNET_NO);
506 discard_buffer (dir, env); 500 discard_buffer(dir, env);
507 } 501 }
508 if (NULL != dir->mqm) 502 if (NULL != dir->mqm)
509 { 503 {
510 GCP_request_mq_cancel (dir->mqm, NULL); 504 GCP_request_mq_cancel(dir->mqm, NULL);
511 dir->mqm = NULL; 505 dir->mqm = NULL;
512 } 506 }
513 GNUNET_CONTAINER_DLL_remove (rung_head->rd_head, rung_head->rd_tail, dir); 507 GNUNET_CONTAINER_DLL_remove(rung_head->rd_head, rung_head->rd_tail, dir);
514} 508}
515 509
516 510
@@ -520,26 +514,26 @@ destroy_direction (struct RouteDirection *dir)
520 * @param route route to destroy 514 * @param route route to destroy
521 */ 515 */
522static void 516static void
523destroy_route (struct CadetRoute *route) 517destroy_route(struct CadetRoute *route)
524{ 518{
525 LOG (GNUNET_ERROR_TYPE_DEBUG, 519 LOG(GNUNET_ERROR_TYPE_DEBUG,
526 "Destroying route from %s to %s of connection %s\n", 520 "Destroying route from %s to %s of connection %s\n",
527 GNUNET_i2s (GCP_get_id (route->prev.hop)), 521 GNUNET_i2s(GCP_get_id(route->prev.hop)),
528 GNUNET_i2s2 (GCP_get_id (route->next.hop)), 522 GNUNET_i2s2(GCP_get_id(route->next.hop)),
529 GNUNET_sh2s (&route->cid.connection_of_tunnel)); 523 GNUNET_sh2s(&route->cid.connection_of_tunnel));
530 GNUNET_assert (route == GNUNET_CONTAINER_heap_remove_node (route->hn)); 524 GNUNET_assert(route == GNUNET_CONTAINER_heap_remove_node(route->hn));
531 GNUNET_assert ( 525 GNUNET_assert(
532 GNUNET_YES == 526 GNUNET_YES ==
533 GNUNET_CONTAINER_multishortmap_remove (routes, 527 GNUNET_CONTAINER_multishortmap_remove(routes,
534 &route->cid.connection_of_tunnel, 528 &route->cid.connection_of_tunnel,
535 route)); 529 route));
536 GNUNET_STATISTICS_set (stats, 530 GNUNET_STATISTICS_set(stats,
537 "# routes", 531 "# routes",
538 GNUNET_CONTAINER_multishortmap_size (routes), 532 GNUNET_CONTAINER_multishortmap_size(routes),
539 GNUNET_NO); 533 GNUNET_NO);
540 destroy_direction (&route->prev); 534 destroy_direction(&route->prev);
541 destroy_direction (&route->next); 535 destroy_direction(&route->next);
542 GNUNET_free (route); 536 GNUNET_free(route);
543} 537}
544 538
545 539
@@ -552,30 +546,30 @@ destroy_route (struct CadetRoute *route)
552 * @param peer2 another one of the peers where a link is broken 546 * @param peer2 another one of the peers where a link is broken
553 */ 547 */
554static void 548static void
555send_broken (struct RouteDirection *target, 549send_broken(struct RouteDirection *target,
556 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid, 550 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
557 const struct GNUNET_PeerIdentity *peer1, 551 const struct GNUNET_PeerIdentity *peer1,
558 const struct GNUNET_PeerIdentity *peer2) 552 const struct GNUNET_PeerIdentity *peer2)
559{ 553{
560 struct GNUNET_MQ_Envelope *env; 554 struct GNUNET_MQ_Envelope *env;
561 struct GNUNET_CADET_ConnectionBrokenMessage *bm; 555 struct GNUNET_CADET_ConnectionBrokenMessage *bm;
562 556
563 if (NULL == target->mqm) 557 if (NULL == target->mqm)
564 return; /* Can't send notification, connection is down! */ 558 return; /* Can't send notification, connection is down! */
565 LOG (GNUNET_ERROR_TYPE_DEBUG, 559 LOG(GNUNET_ERROR_TYPE_DEBUG,
566 "Notifying %s about BROKEN route at %s-%s of connection %s\n", 560 "Notifying %s about BROKEN route at %s-%s of connection %s\n",
567 GCP_2s (target->hop), 561 GCP_2s(target->hop),
568 GNUNET_i2s (peer1), 562 GNUNET_i2s(peer1),
569 GNUNET_i2s2 (peer2), 563 GNUNET_i2s2(peer2),
570 GNUNET_sh2s (&cid->connection_of_tunnel)); 564 GNUNET_sh2s(&cid->connection_of_tunnel));
571 565
572 env = GNUNET_MQ_msg (bm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN); 566 env = GNUNET_MQ_msg(bm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
573 bm->cid = *cid; 567 bm->cid = *cid;
574 if (NULL != peer1) 568 if (NULL != peer1)
575 bm->peer1 = *peer1; 569 bm->peer1 = *peer1;
576 if (NULL != peer2) 570 if (NULL != peer2)
577 bm->peer2 = *peer2; 571 bm->peer2 = *peer2;
578 GCP_request_mq_cancel (target->mqm, env); 572 GCP_request_mq_cancel(target->mqm, env);
579 target->mqm = NULL; 573 target->mqm = NULL;
580} 574}
581 575
@@ -588,31 +582,31 @@ send_broken (struct RouteDirection *target,
588 * @param cls NULL 582 * @param cls NULL
589 */ 583 */
590static void 584static void
591timeout_cb (void *cls) 585timeout_cb(void *cls)
592{ 586{
593 struct CadetRoute *r; 587 struct CadetRoute *r;
594 struct GNUNET_TIME_Relative linger; 588 struct GNUNET_TIME_Relative linger;
595 struct GNUNET_TIME_Absolute exp; 589 struct GNUNET_TIME_Absolute exp;
596 590
597 timeout_task = NULL; 591 timeout_task = NULL;
598 linger = GNUNET_TIME_relative_multiply (keepalive_period, 3); 592 linger = GNUNET_TIME_relative_multiply(keepalive_period, 3);
599 while (NULL != (r = GNUNET_CONTAINER_heap_peek (route_heap))) 593 while (NULL != (r = GNUNET_CONTAINER_heap_peek(route_heap)))
600 {
601 exp = GNUNET_TIME_absolute_add (r->last_use, linger);
602 if (0 != GNUNET_TIME_absolute_get_remaining (exp).rel_value_us)
603 { 594 {
604 /* Route not yet timed out, wait until it does. */ 595 exp = GNUNET_TIME_absolute_add(r->last_use, linger);
605 timeout_task = GNUNET_SCHEDULER_add_at (exp, &timeout_cb, NULL); 596 if (0 != GNUNET_TIME_absolute_get_remaining(exp).rel_value_us)
606 return; 597 {
598 /* Route not yet timed out, wait until it does. */
599 timeout_task = GNUNET_SCHEDULER_add_at(exp, &timeout_cb, NULL);
600 return;
601 }
602 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
603 "Sending BROKEN due to timeout (%s was last use, %s linger)\n",
604 GNUNET_STRINGS_absolute_time_to_string(r->last_use),
605 GNUNET_STRINGS_relative_time_to_string(linger, GNUNET_YES));
606 send_broken(&r->prev, &r->cid, NULL, NULL);
607 send_broken(&r->next, &r->cid, NULL, NULL);
608 destroy_route(r);
607 } 609 }
608 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
609 "Sending BROKEN due to timeout (%s was last use, %s linger)\n",
610 GNUNET_STRINGS_absolute_time_to_string (r->last_use),
611 GNUNET_STRINGS_relative_time_to_string (linger, GNUNET_YES));
612 send_broken (&r->prev, &r->cid, NULL, NULL);
613 send_broken (&r->next, &r->cid, NULL, NULL);
614 destroy_route (r);
615 }
616 /* No more routes left, so no need for a #timeout_task */ 610 /* No more routes left, so no need for a #timeout_task */
617} 611}
618 612
@@ -630,32 +624,32 @@ timeout_cb (void *cls)
630 * and the last envelope was discarded 624 * and the last envelope was discarded
631 */ 625 */
632static void 626static void
633dir_ready_cb (void *cls, int ready) 627dir_ready_cb(void *cls, int ready)
634{ 628{
635 struct RouteDirection *dir = cls; 629 struct RouteDirection *dir = cls;
636 struct CadetRoute *route = dir->my_route; 630 struct CadetRoute *route = dir->my_route;
637 struct RouteDirection *odir; 631 struct RouteDirection *odir;
638 632
639 if (GNUNET_YES == ready) 633 if (GNUNET_YES == ready)
640 {
641 struct GNUNET_MQ_Envelope *env;
642
643 dir->is_ready = GNUNET_YES;
644 if (NULL != (env = dir->env_head))
645 { 634 {
646 GNUNET_MQ_dll_remove (&dir->env_head, &dir->env_tail, env); 635 struct GNUNET_MQ_Envelope *env;
647 cur_buffers--; 636
648 GNUNET_STATISTICS_set (stats, "# buffer use", cur_buffers, GNUNET_NO); 637 dir->is_ready = GNUNET_YES;
649 lower_rung (dir); 638 if (NULL != (env = dir->env_head))
650 dir->is_ready = GNUNET_NO; 639 {
651 GCP_send (dir->mqm, env); 640 GNUNET_MQ_dll_remove(&dir->env_head, &dir->env_tail, env);
641 cur_buffers--;
642 GNUNET_STATISTICS_set(stats, "# buffer use", cur_buffers, GNUNET_NO);
643 lower_rung(dir);
644 dir->is_ready = GNUNET_NO;
645 GCP_send(dir->mqm, env);
646 }
647 return;
652 } 648 }
653 return;
654 }
655 odir = (dir == &route->next) ? &route->prev : &route->next; 649 odir = (dir == &route->next) ? &route->prev : &route->next;
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending BROKEN due to MQ going down\n"); 650 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending BROKEN due to MQ going down\n");
657 send_broken (&route->next, &route->cid, GCP_get_id (odir->hop), &my_full_id); 651 send_broken(&route->next, &route->cid, GCP_get_id(odir->hop), &my_full_id);
658 destroy_route (route); 652 destroy_route(route);
659} 653}
660 654
661 655
@@ -667,16 +661,16 @@ dir_ready_cb (void *cls, int ready)
667 * @param hop next hop on in the @a dir 661 * @param hop next hop on in the @a dir
668 */ 662 */
669static void 663static void
670dir_init (struct RouteDirection *dir, 664dir_init(struct RouteDirection *dir,
671 struct CadetRoute *route, 665 struct CadetRoute *route,
672 struct CadetPeer *hop) 666 struct CadetPeer *hop)
673{ 667{
674 dir->hop = hop; 668 dir->hop = hop;
675 dir->my_route = route; 669 dir->my_route = route;
676 dir->mqm = GCP_request_mq (hop, &dir_ready_cb, dir); 670 dir->mqm = GCP_request_mq(hop, &dir_ready_cb, dir);
677 GNUNET_CONTAINER_DLL_insert (rung_head->rd_head, rung_head->rd_tail, dir); 671 GNUNET_CONTAINER_DLL_insert(rung_head->rd_head, rung_head->rd_tail, dir);
678 dir->rung = rung_head; 672 dir->rung = rung_head;
679 GNUNET_assert (GNUNET_YES == dir->is_ready); 673 GNUNET_assert(GNUNET_YES == dir->is_ready);
680} 674}
681 675
682 676
@@ -691,7 +685,7 @@ dir_init (struct RouteDirection *dir,
691 * or NULL. 685 * or NULL.
692 */ 686 */
693static void 687static void
694send_broken_without_mqm ( 688send_broken_without_mqm(
695 struct CadetPeer *target, 689 struct CadetPeer *target,
696 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid, 690 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
697 const struct GNUNET_PeerIdentity *failure_at) 691 const struct GNUNET_PeerIdentity *failure_at)
@@ -699,12 +693,12 @@ send_broken_without_mqm (
699 struct GNUNET_MQ_Envelope *env; 693 struct GNUNET_MQ_Envelope *env;
700 struct GNUNET_CADET_ConnectionBrokenMessage *bm; 694 struct GNUNET_CADET_ConnectionBrokenMessage *bm;
701 695
702 env = GNUNET_MQ_msg (bm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN); 696 env = GNUNET_MQ_msg(bm, GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
703 bm->cid = *cid; 697 bm->cid = *cid;
704 bm->peer1 = my_full_id; 698 bm->peer1 = my_full_id;
705 if (NULL != failure_at) 699 if (NULL != failure_at)
706 bm->peer2 = *failure_at; 700 bm->peer2 = *failure_at;
707 GCP_send_ooo (target, env); 701 GCP_send_ooo(target, env);
708} 702}
709 703
710 704
@@ -715,193 +709,193 @@ send_broken_without_mqm (
715 * @param msg Message itself. 709 * @param msg Message itself.
716 */ 710 */
717static void 711static void
718handle_connection_create ( 712handle_connection_create(
719 void *cls, 713 void *cls,
720 const struct GNUNET_CADET_ConnectionCreateMessage *msg) 714 const struct GNUNET_CADET_ConnectionCreateMessage *msg)
721{ 715{
722 struct CadetPeer *sender = cls; 716 struct CadetPeer *sender = cls;
723 struct CadetPeer *next; 717 struct CadetPeer *next;
724 const struct GNUNET_PeerIdentity *pids = 718 const struct GNUNET_PeerIdentity *pids =
725 (const struct GNUNET_PeerIdentity *) &msg[1]; 719 (const struct GNUNET_PeerIdentity *)&msg[1];
726 struct CadetRoute *route; 720 struct CadetRoute *route;
727 uint16_t size = ntohs (msg->header.size) - sizeof (*msg); 721 uint16_t size = ntohs(msg->header.size) - sizeof(*msg);
728 unsigned int path_length; 722 unsigned int path_length;
729 unsigned int off; 723 unsigned int off;
730 724
731 path_length = size / sizeof (struct GNUNET_PeerIdentity); 725 path_length = size / sizeof(struct GNUNET_PeerIdentity);
732 if (0 == path_length) 726 if (0 == path_length)
733 { 727 {
734 LOG (GNUNET_ERROR_TYPE_DEBUG, 728 LOG(GNUNET_ERROR_TYPE_DEBUG,
735 "Dropping CADET_CONNECTION_CREATE with empty path\n"); 729 "Dropping CADET_CONNECTION_CREATE with empty path\n");
736 GNUNET_break_op (0); 730 GNUNET_break_op(0);
737 return; 731 return;
738 } 732 }
739 LOG (GNUNET_ERROR_TYPE_DEBUG, 733 LOG(GNUNET_ERROR_TYPE_DEBUG,
740 "Handling CADET_CONNECTION_CREATE from %s for CID %s with %u hops\n", 734 "Handling CADET_CONNECTION_CREATE from %s for CID %s with %u hops\n",
741 GCP_2s (sender), 735 GCP_2s(sender),
742 GNUNET_sh2s (&msg->cid.connection_of_tunnel), 736 GNUNET_sh2s(&msg->cid.connection_of_tunnel),
743 path_length); 737 path_length);
744 /* Check for loops */ 738 /* Check for loops */
745 { 739 {
746 struct GNUNET_CONTAINER_MultiPeerMap *map; 740 struct GNUNET_CONTAINER_MultiPeerMap *map;
747 741
748 map = GNUNET_CONTAINER_multipeermap_create (path_length * 2, GNUNET_YES); 742 map = GNUNET_CONTAINER_multipeermap_create(path_length * 2, GNUNET_YES);
749 GNUNET_assert (NULL != map); 743 GNUNET_assert(NULL != map);
750 for (unsigned int i = 0; i < path_length; i++) 744 for (unsigned int i = 0; i < path_length; i++)
751 {
752 LOG (GNUNET_ERROR_TYPE_DEBUG,
753 "CADET_CONNECTION_CREATE has peer %s at offset %u\n",
754 GNUNET_i2s (&pids[i]),
755 i);
756 if (GNUNET_SYSERR == GNUNET_CONTAINER_multipeermap_put (
757 map,
758 &pids[i],
759 NULL,
760 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
761 { 745 {
762 /* bogus request */ 746 LOG(GNUNET_ERROR_TYPE_DEBUG,
763 GNUNET_CONTAINER_multipeermap_destroy (map); 747 "CADET_CONNECTION_CREATE has peer %s at offset %u\n",
764 LOG (GNUNET_ERROR_TYPE_DEBUG, 748 GNUNET_i2s(&pids[i]),
765 "Dropping CADET_CONNECTION_CREATE with cyclic path\n"); 749 i);
766 GNUNET_break_op (0); 750 if (GNUNET_SYSERR == GNUNET_CONTAINER_multipeermap_put(
767 return; 751 map,
752 &pids[i],
753 NULL,
754 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
755 {
756 /* bogus request */
757 GNUNET_CONTAINER_multipeermap_destroy(map);
758 LOG(GNUNET_ERROR_TYPE_DEBUG,
759 "Dropping CADET_CONNECTION_CREATE with cyclic path\n");
760 GNUNET_break_op(0);
761 return;
762 }
768 } 763 }
769 } 764 GNUNET_CONTAINER_multipeermap_destroy(map);
770 GNUNET_CONTAINER_multipeermap_destroy (map);
771 } 765 }
772 /* Initiator is at offset 0, find us */ 766 /* Initiator is at offset 0, find us */
773 for (off = 1; off < path_length; off++) 767 for (off = 1; off < path_length; off++)
774 if (0 == GNUNET_memcmp (&my_full_id, &pids[off])) 768 if (0 == GNUNET_memcmp(&my_full_id, &pids[off]))
775 break; 769 break;
776 if (off == path_length) 770 if (off == path_length)
777 { 771 {
778 LOG (GNUNET_ERROR_TYPE_DEBUG, 772 LOG(GNUNET_ERROR_TYPE_DEBUG,
779 "Dropping CADET_CONNECTION_CREATE without us in the path\n"); 773 "Dropping CADET_CONNECTION_CREATE without us in the path\n");
780 GNUNET_break_op (0); 774 GNUNET_break_op(0);
781 return; 775 return;
782 } 776 }
783 /* Check previous hop */ 777 /* Check previous hop */
784 if (sender != GCP_get (&pids[off - 1], GNUNET_NO)) 778 if (sender != GCP_get(&pids[off - 1], GNUNET_NO))
785 {
786 LOG (GNUNET_ERROR_TYPE_DEBUG,
787 "Dropping CADET_CONNECTION_CREATE without sender at previous hop in the path\n");
788 GNUNET_break_op (0);
789 return;
790 }
791 if (NULL != (route = get_route (&msg->cid)))
792 {
793 /* Duplicate CREATE, pass it on, previous one might have been lost! */
794
795 LOG (GNUNET_ERROR_TYPE_DEBUG,
796 "Passing on duplicate CADET_CONNECTION_CREATE message on connection %s\n",
797 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
798 route_message (sender,
799 &msg->cid,
800 &msg->header,
801 GNUNET_MQ_PRIO_CRITICAL_CONTROL |
802 GNUNET_MQ_PREF_LOW_LATENCY);
803 return;
804 }
805 if (off == path_length - 1)
806 {
807 /* We are the destination, create connection */
808 struct CadetConnection *cc;
809 struct CadetPeerPath *path;
810 struct CadetPeer *origin;
811
812 cc = GCC_lookup (&msg->cid);
813 if (NULL != cc)
814 { 779 {
815 LOG (GNUNET_ERROR_TYPE_DEBUG, 780 LOG(GNUNET_ERROR_TYPE_DEBUG,
816 "Received duplicate CADET_CONNECTION_CREATE message on connection %s\n", 781 "Dropping CADET_CONNECTION_CREATE without sender at previous hop in the path\n");
817 GNUNET_sh2s (&msg->cid.connection_of_tunnel)); 782 GNUNET_break_op(0);
818 GCC_handle_duplicate_create (cc);
819 return; 783 return;
820 } 784 }
821 785 if (NULL != (route = get_route(&msg->cid)))
822 origin = GCP_get (&pids[0], GNUNET_YES);
823 LOG (GNUNET_ERROR_TYPE_DEBUG,
824 "I am destination for CADET_CONNECTION_CREATE message from %s for connection %s, building inverse path\n",
825 GCP_2s (origin),
826 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
827 path = GCPP_get_path_from_route (path_length - 1, pids);
828 if (GNUNET_OK !=
829 GCT_add_inbound_connection (GCP_get_tunnel (origin, GNUNET_YES),
830 &msg->cid,
831 path))
832 { 786 {
833 /* Send back BROKEN: duplicate connection on the same path, 787 /* Duplicate CREATE, pass it on, previous one might have been lost! */
834 we will use the other one. */ 788
835 LOG (GNUNET_ERROR_TYPE_DEBUG, 789 LOG(GNUNET_ERROR_TYPE_DEBUG,
836 "Received CADET_CONNECTION_CREATE from %s for %s, but %s already has a connection. Sending BROKEN\n", 790 "Passing on duplicate CADET_CONNECTION_CREATE message on connection %s\n",
837 GCP_2s (sender), 791 GNUNET_sh2s(&msg->cid.connection_of_tunnel));
838 GNUNET_sh2s (&msg->cid.connection_of_tunnel), 792 route_message(sender,
839 GCPP_2s (path)); 793 &msg->cid,
840 send_broken_without_mqm (sender, &msg->cid, NULL); 794 &msg->header,
795 GNUNET_MQ_PRIO_CRITICAL_CONTROL |
796 GNUNET_MQ_PREF_LOW_LATENCY);
797 return;
798 }
799 if (off == path_length - 1)
800 {
801 /* We are the destination, create connection */
802 struct CadetConnection *cc;
803 struct CadetPeerPath *path;
804 struct CadetPeer *origin;
805
806 cc = GCC_lookup(&msg->cid);
807 if (NULL != cc)
808 {
809 LOG(GNUNET_ERROR_TYPE_DEBUG,
810 "Received duplicate CADET_CONNECTION_CREATE message on connection %s\n",
811 GNUNET_sh2s(&msg->cid.connection_of_tunnel));
812 GCC_handle_duplicate_create(cc);
813 return;
814 }
815
816 origin = GCP_get(&pids[0], GNUNET_YES);
817 LOG(GNUNET_ERROR_TYPE_DEBUG,
818 "I am destination for CADET_CONNECTION_CREATE message from %s for connection %s, building inverse path\n",
819 GCP_2s(origin),
820 GNUNET_sh2s(&msg->cid.connection_of_tunnel));
821 path = GCPP_get_path_from_route(path_length - 1, pids);
822 if (GNUNET_OK !=
823 GCT_add_inbound_connection(GCP_get_tunnel(origin, GNUNET_YES),
824 &msg->cid,
825 path))
826 {
827 /* Send back BROKEN: duplicate connection on the same path,
828 we will use the other one. */
829 LOG(GNUNET_ERROR_TYPE_DEBUG,
830 "Received CADET_CONNECTION_CREATE from %s for %s, but %s already has a connection. Sending BROKEN\n",
831 GCP_2s(sender),
832 GNUNET_sh2s(&msg->cid.connection_of_tunnel),
833 GCPP_2s(path));
834 send_broken_without_mqm(sender, &msg->cid, NULL);
835 return;
836 }
841 return; 837 return;
842 } 838 }
843 return;
844 }
845 /* We are merely a hop on the way, check if we can support the route */ 839 /* We are merely a hop on the way, check if we can support the route */
846 next = GCP_get (&pids[off + 1], GNUNET_NO); 840 next = GCP_get(&pids[off + 1], GNUNET_NO);
847 if ((NULL == next) || (GNUNET_NO == GCP_has_core_connection (next))) 841 if ((NULL == next) || (GNUNET_NO == GCP_has_core_connection(next)))
848 { 842 {
849 /* unworkable, send back BROKEN notification */ 843 /* unworkable, send back BROKEN notification */
850 LOG (GNUNET_ERROR_TYPE_DEBUG, 844 LOG(GNUNET_ERROR_TYPE_DEBUG,
851 "Received CADET_CONNECTION_CREATE from %s for %s. Next hop %s:%u is down. Sending BROKEN\n", 845 "Received CADET_CONNECTION_CREATE from %s for %s. Next hop %s:%u is down. Sending BROKEN\n",
852 GCP_2s (sender), 846 GCP_2s(sender),
853 GNUNET_sh2s (&msg->cid.connection_of_tunnel), 847 GNUNET_sh2s(&msg->cid.connection_of_tunnel),
854 GNUNET_i2s (&pids[off + 1]), 848 GNUNET_i2s(&pids[off + 1]),
855 off + 1); 849 off + 1);
856 send_broken_without_mqm (sender, &msg->cid, &pids[off + 1]); 850 send_broken_without_mqm(sender, &msg->cid, &pids[off + 1]);
857 return; 851 return;
858 } 852 }
859 if (max_routes <= GNUNET_CONTAINER_multishortmap_size (routes)) 853 if (max_routes <= GNUNET_CONTAINER_multishortmap_size(routes))
860 { 854 {
861 LOG (GNUNET_ERROR_TYPE_DEBUG, 855 LOG(GNUNET_ERROR_TYPE_DEBUG,
862 "Received CADET_CONNECTION_CREATE from %s for %s. We have reached our route limit. Sending BROKEN\n", 856 "Received CADET_CONNECTION_CREATE from %s for %s. We have reached our route limit. Sending BROKEN\n",
863 GCP_2s (sender), 857 GCP_2s(sender),
864 GNUNET_sh2s (&msg->cid.connection_of_tunnel)); 858 GNUNET_sh2s(&msg->cid.connection_of_tunnel));
865 send_broken_without_mqm (sender, &msg->cid, &pids[off - 1]); 859 send_broken_without_mqm(sender, &msg->cid, &pids[off - 1]);
866 return; 860 return;
867 } 861 }
868 862
869 /* Workable route, create routing entry */ 863 /* Workable route, create routing entry */
870 LOG (GNUNET_ERROR_TYPE_DEBUG, 864 LOG(GNUNET_ERROR_TYPE_DEBUG,
871 "Received CADET_CONNECTION_CREATE from %s for %s. Next hop %s:%u is up. Creating route\n", 865 "Received CADET_CONNECTION_CREATE from %s for %s. Next hop %s:%u is up. Creating route\n",
872 GCP_2s (sender), 866 GCP_2s(sender),
873 GNUNET_sh2s (&msg->cid.connection_of_tunnel), 867 GNUNET_sh2s(&msg->cid.connection_of_tunnel),
874 GNUNET_i2s (&pids[off + 1]), 868 GNUNET_i2s(&pids[off + 1]),
875 off + 1); 869 off + 1);
876 route = GNUNET_new (struct CadetRoute); 870 route = GNUNET_new(struct CadetRoute);
877 route->cid = msg->cid; 871 route->cid = msg->cid;
878 route->last_use = GNUNET_TIME_absolute_get (); 872 route->last_use = GNUNET_TIME_absolute_get();
879 dir_init (&route->prev, route, sender); 873 dir_init(&route->prev, route, sender);
880 dir_init (&route->next, route, next); 874 dir_init(&route->next, route, next);
881 GNUNET_assert (GNUNET_OK == 875 GNUNET_assert(GNUNET_OK ==
882 GNUNET_CONTAINER_multishortmap_put ( 876 GNUNET_CONTAINER_multishortmap_put(
883 routes, 877 routes,
884 &route->cid.connection_of_tunnel, 878 &route->cid.connection_of_tunnel,
885 route, 879 route,
886 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 880 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
887 GNUNET_STATISTICS_set (stats, 881 GNUNET_STATISTICS_set(stats,
888 "# routes", 882 "# routes",
889 GNUNET_CONTAINER_multishortmap_size (routes), 883 GNUNET_CONTAINER_multishortmap_size(routes),
890 GNUNET_NO); 884 GNUNET_NO);
891 route->hn = GNUNET_CONTAINER_heap_insert (route_heap, 885 route->hn = GNUNET_CONTAINER_heap_insert(route_heap,
892 route, 886 route,
893 route->last_use.abs_value_us); 887 route->last_use.abs_value_us);
894 if (NULL == timeout_task) 888 if (NULL == timeout_task)
895 timeout_task = 889 timeout_task =
896 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (keepalive_period, 890 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(keepalive_period,
897 3), 891 3),
898 &timeout_cb, 892 &timeout_cb,
899 NULL); 893 NULL);
900 /* also pass CREATE message along to next hop */ 894 /* also pass CREATE message along to next hop */
901 route_message (sender, 895 route_message(sender,
902 &msg->cid, 896 &msg->cid,
903 &msg->header, 897 &msg->header,
904 GNUNET_MQ_PRIO_CRITICAL_CONTROL | GNUNET_MQ_PREF_LOW_LATENCY); 898 GNUNET_MQ_PRIO_CRITICAL_CONTROL | GNUNET_MQ_PREF_LOW_LATENCY);
905} 899}
906 900
907 901
@@ -912,7 +906,7 @@ handle_connection_create (
912 * @param msg Message itself. 906 * @param msg Message itself.
913 */ 907 */
914static void 908static void
915handle_connection_create_ack ( 909handle_connection_create_ack(
916 void *cls, 910 void *cls,
917 const struct GNUNET_CADET_ConnectionCreateAckMessage *msg) 911 const struct GNUNET_CADET_ConnectionCreateAckMessage *msg)
918{ 912{
@@ -920,31 +914,31 @@ handle_connection_create_ack (
920 struct CadetConnection *cc; 914 struct CadetConnection *cc;
921 915
922 /* First, check if ACK belongs to a connection that ends here. */ 916 /* First, check if ACK belongs to a connection that ends here. */
923 cc = GCC_lookup (&msg->cid); 917 cc = GCC_lookup(&msg->cid);
924 if (NULL != cc) 918 if (NULL != cc)
925 {
926 /* verify ACK came from the right direction */
927 unsigned int len;
928 struct CadetPeerPath *path = GCC_get_path (cc, &len);
929
930 if (peer != GCPP_get_peer_at_offset (path, 0))
931 { 919 {
932 /* received ACK from unexpected direction, ignore! */ 920 /* verify ACK came from the right direction */
933 GNUNET_break_op (0); 921 unsigned int len;
922 struct CadetPeerPath *path = GCC_get_path(cc, &len);
923
924 if (peer != GCPP_get_peer_at_offset(path, 0))
925 {
926 /* received ACK from unexpected direction, ignore! */
927 GNUNET_break_op(0);
928 return;
929 }
930 LOG(GNUNET_ERROR_TYPE_DEBUG,
931 "Received CONNECTION_CREATE_ACK for connection %s.\n",
932 GNUNET_sh2s(&msg->cid.connection_of_tunnel));
933 GCC_handle_connection_create_ack(cc);
934 return; 934 return;
935 } 935 }
936 LOG (GNUNET_ERROR_TYPE_DEBUG,
937 "Received CONNECTION_CREATE_ACK for connection %s.\n",
938 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
939 GCC_handle_connection_create_ack (cc);
940 return;
941 }
942 936
943 /* We're just an intermediary peer, route the message along its path */ 937 /* We're just an intermediary peer, route the message along its path */
944 route_message (peer, 938 route_message(peer,
945 &msg->cid, 939 &msg->cid,
946 &msg->header, 940 &msg->header,
947 GNUNET_MQ_PRIO_CRITICAL_CONTROL | GNUNET_MQ_PREF_LOW_LATENCY); 941 GNUNET_MQ_PRIO_CRITICAL_CONTROL | GNUNET_MQ_PREF_LOW_LATENCY);
948} 942}
949 943
950 944
@@ -956,7 +950,7 @@ handle_connection_create_ack (
956 * @deprecated duplicate logic with #handle_destroy(); dedup! 950 * @deprecated duplicate logic with #handle_destroy(); dedup!
957 */ 951 */
958static void 952static void
959handle_connection_broken ( 953handle_connection_broken(
960 void *cls, 954 void *cls,
961 const struct GNUNET_CADET_ConnectionBrokenMessage *msg) 955 const struct GNUNET_CADET_ConnectionBrokenMessage *msg)
962{ 956{
@@ -965,36 +959,36 @@ handle_connection_broken (
965 struct CadetRoute *route; 959 struct CadetRoute *route;
966 960
967 /* First, check if message belongs to a connection that ends here. */ 961 /* First, check if message belongs to a connection that ends here. */
968 cc = GCC_lookup (&msg->cid); 962 cc = GCC_lookup(&msg->cid);
969 if (NULL != cc) 963 if (NULL != cc)
970 {
971 /* verify message came from the right direction */
972 unsigned int len;
973 struct CadetPeerPath *path = GCC_get_path (cc, &len);
974
975 if (peer != GCPP_get_peer_at_offset (path, 0))
976 { 964 {
977 /* received message from unexpected direction, ignore! */ 965 /* verify message came from the right direction */
978 GNUNET_break_op (0); 966 unsigned int len;
967 struct CadetPeerPath *path = GCC_get_path(cc, &len);
968
969 if (peer != GCPP_get_peer_at_offset(path, 0))
970 {
971 /* received message from unexpected direction, ignore! */
972 GNUNET_break_op(0);
973 return;
974 }
975 LOG(GNUNET_ERROR_TYPE_DEBUG,
976 "Received CONNECTION_BROKEN for connection %s. Destroying it.\n",
977 GNUNET_sh2s(&msg->cid.connection_of_tunnel));
978 GCC_destroy_without_core(cc);
979
980 /* FIXME: also destroy the path up to the specified link! */
979 return; 981 return;
980 } 982 }
981 LOG (GNUNET_ERROR_TYPE_DEBUG,
982 "Received CONNECTION_BROKEN for connection %s. Destroying it.\n",
983 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
984 GCC_destroy_without_core (cc);
985
986 /* FIXME: also destroy the path up to the specified link! */
987 return;
988 }
989 983
990 /* We're just an intermediary peer, route the message along its path */ 984 /* We're just an intermediary peer, route the message along its path */
991 route_message (peer, 985 route_message(peer,
992 &msg->cid, 986 &msg->cid,
993 &msg->header, 987 &msg->header,
994 GNUNET_MQ_PREF_LOW_LATENCY | GNUNET_MQ_PRIO_CRITICAL_CONTROL); 988 GNUNET_MQ_PREF_LOW_LATENCY | GNUNET_MQ_PRIO_CRITICAL_CONTROL);
995 route = get_route (&msg->cid); 989 route = get_route(&msg->cid);
996 if (NULL != route) 990 if (NULL != route)
997 destroy_route (route); 991 destroy_route(route);
998 /* FIXME: also destroy paths we MAY have up to the specified link! */ 992 /* FIXME: also destroy paths we MAY have up to the specified link! */
999} 993}
1000 994
@@ -1006,7 +1000,7 @@ handle_connection_broken (
1006 * @param msg Message itself. 1000 * @param msg Message itself.
1007 */ 1001 */
1008static void 1002static void
1009handle_connection_destroy ( 1003handle_connection_destroy(
1010 void *cls, 1004 void *cls,
1011 const struct GNUNET_CADET_ConnectionDestroyMessage *msg) 1005 const struct GNUNET_CADET_ConnectionDestroyMessage *msg)
1012{ 1006{
@@ -1015,38 +1009,38 @@ handle_connection_destroy (
1015 struct CadetRoute *route; 1009 struct CadetRoute *route;
1016 1010
1017 /* First, check if message belongs to a connection that ends here. */ 1011 /* First, check if message belongs to a connection that ends here. */
1018 cc = GCC_lookup (&msg->cid); 1012 cc = GCC_lookup(&msg->cid);
1019 if (NULL != cc) 1013 if (NULL != cc)
1020 {
1021 /* verify message came from the right direction */
1022 unsigned int len;
1023 struct CadetPeerPath *path = GCC_get_path (cc, &len);
1024
1025 if (peer != GCPP_get_peer_at_offset (path, 0))
1026 { 1014 {
1027 /* received message from unexpected direction, ignore! */ 1015 /* verify message came from the right direction */
1028 GNUNET_break_op (0); 1016 unsigned int len;
1017 struct CadetPeerPath *path = GCC_get_path(cc, &len);
1018
1019 if (peer != GCPP_get_peer_at_offset(path, 0))
1020 {
1021 /* received message from unexpected direction, ignore! */
1022 GNUNET_break_op(0);
1023 return;
1024 }
1025 LOG(GNUNET_ERROR_TYPE_DEBUG,
1026 "Received CONNECTION_DESTROY for connection %s. Destroying connection.\n",
1027 GNUNET_sh2s(&msg->cid.connection_of_tunnel));
1028
1029 GCC_destroy_without_core(cc);
1029 return; 1030 return;
1030 } 1031 }
1031 LOG (GNUNET_ERROR_TYPE_DEBUG,
1032 "Received CONNECTION_DESTROY for connection %s. Destroying connection.\n",
1033 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
1034
1035 GCC_destroy_without_core (cc);
1036 return;
1037 }
1038 1032
1039 /* We're just an intermediary peer, route the message along its path */ 1033 /* We're just an intermediary peer, route the message along its path */
1040 LOG (GNUNET_ERROR_TYPE_DEBUG, 1034 LOG(GNUNET_ERROR_TYPE_DEBUG,
1041 "Received CONNECTION_DESTROY for connection %s. Destroying route.\n", 1035 "Received CONNECTION_DESTROY for connection %s. Destroying route.\n",
1042 GNUNET_sh2s (&msg->cid.connection_of_tunnel)); 1036 GNUNET_sh2s(&msg->cid.connection_of_tunnel));
1043 route_message (peer, 1037 route_message(peer,
1044 &msg->cid, 1038 &msg->cid,
1045 &msg->header, 1039 &msg->header,
1046 GNUNET_MQ_PREF_LOW_LATENCY | GNUNET_MQ_PRIO_CRITICAL_CONTROL); 1040 GNUNET_MQ_PREF_LOW_LATENCY | GNUNET_MQ_PRIO_CRITICAL_CONTROL);
1047 route = get_route (&msg->cid); 1041 route = get_route(&msg->cid);
1048 if (NULL != route) 1042 if (NULL != route)
1049 destroy_route (route); 1043 destroy_route(route);
1050} 1044}
1051 1045
1052 1046
@@ -1057,41 +1051,41 @@ handle_connection_destroy (
1057 * @param msg Message itself. 1051 * @param msg Message itself.
1058 */ 1052 */
1059static void 1053static void
1060handle_tunnel_kx (void *cls, 1054handle_tunnel_kx(void *cls,
1061 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg) 1055 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
1062{ 1056{
1063 struct CadetPeer *peer = cls; 1057 struct CadetPeer *peer = cls;
1064 struct CadetConnection *cc; 1058 struct CadetConnection *cc;
1065 1059
1066 /* First, check if message belongs to a connection that ends here. */ 1060 /* First, check if message belongs to a connection that ends here. */
1067 LOG (GNUNET_ERROR_TYPE_DEBUG, 1061 LOG(GNUNET_ERROR_TYPE_DEBUG,
1068 "Routing KX with ephemeral %s on CID %s\n", 1062 "Routing KX with ephemeral %s on CID %s\n",
1069 GNUNET_e2s (&msg->ephemeral_key), 1063 GNUNET_e2s(&msg->ephemeral_key),
1070 GNUNET_sh2s (&msg->cid.connection_of_tunnel)); 1064 GNUNET_sh2s(&msg->cid.connection_of_tunnel));
1071 1065
1072 1066
1073 cc = GCC_lookup (&msg->cid); 1067 cc = GCC_lookup(&msg->cid);
1074 if (NULL != cc) 1068 if (NULL != cc)
1075 {
1076 /* verify message came from the right direction */
1077 unsigned int len;
1078 struct CadetPeerPath *path = GCC_get_path (cc, &len);
1079
1080 if (peer != GCPP_get_peer_at_offset (path, 0))
1081 { 1069 {
1082 /* received message from unexpected direction, ignore! */ 1070 /* verify message came from the right direction */
1083 GNUNET_break_op (0); 1071 unsigned int len;
1072 struct CadetPeerPath *path = GCC_get_path(cc, &len);
1073
1074 if (peer != GCPP_get_peer_at_offset(path, 0))
1075 {
1076 /* received message from unexpected direction, ignore! */
1077 GNUNET_break_op(0);
1078 return;
1079 }
1080 GCC_handle_kx(cc, msg);
1084 return; 1081 return;
1085 } 1082 }
1086 GCC_handle_kx (cc, msg);
1087 return;
1088 }
1089 1083
1090 /* We're just an intermediary peer, route the message along its path */ 1084 /* We're just an intermediary peer, route the message along its path */
1091 route_message (peer, 1085 route_message(peer,
1092 &msg->cid, 1086 &msg->cid,
1093 &msg->header, 1087 &msg->header,
1094 GNUNET_MQ_PRIO_CRITICAL_CONTROL | GNUNET_MQ_PREF_LOW_LATENCY); 1088 GNUNET_MQ_PRIO_CRITICAL_CONTROL | GNUNET_MQ_PREF_LOW_LATENCY);
1095} 1089}
1096 1090
1097 1091
@@ -1102,7 +1096,7 @@ handle_tunnel_kx (void *cls,
1102 * @param msg Message itself. 1096 * @param msg Message itself.
1103 */ 1097 */
1104static void 1098static void
1105handle_tunnel_kx_auth ( 1099handle_tunnel_kx_auth(
1106 void *cls, 1100 void *cls,
1107 const struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg) 1101 const struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg)
1108{ 1102{
@@ -1110,28 +1104,28 @@ handle_tunnel_kx_auth (
1110 struct CadetConnection *cc; 1104 struct CadetConnection *cc;
1111 1105
1112 /* First, check if message belongs to a connection that ends here. */ 1106 /* First, check if message belongs to a connection that ends here. */
1113 cc = GCC_lookup (&msg->kx.cid); 1107 cc = GCC_lookup(&msg->kx.cid);
1114 if (NULL != cc) 1108 if (NULL != cc)
1115 {
1116 /* verify message came from the right direction */
1117 unsigned int len;
1118 struct CadetPeerPath *path = GCC_get_path (cc, &len);
1119
1120 if (peer != GCPP_get_peer_at_offset (path, 0))
1121 { 1109 {
1122 /* received message from unexpected direction, ignore! */ 1110 /* verify message came from the right direction */
1123 GNUNET_break_op (0); 1111 unsigned int len;
1112 struct CadetPeerPath *path = GCC_get_path(cc, &len);
1113
1114 if (peer != GCPP_get_peer_at_offset(path, 0))
1115 {
1116 /* received message from unexpected direction, ignore! */
1117 GNUNET_break_op(0);
1118 return;
1119 }
1120 GCC_handle_kx_auth(cc, msg);
1124 return; 1121 return;
1125 } 1122 }
1126 GCC_handle_kx_auth (cc, msg);
1127 return;
1128 }
1129 1123
1130 /* We're just an intermediary peer, route the message along its path */ 1124 /* We're just an intermediary peer, route the message along its path */
1131 route_message (peer, 1125 route_message(peer,
1132 &msg->kx.cid, 1126 &msg->kx.cid,
1133 &msg->kx.header, 1127 &msg->kx.header,
1134 GNUNET_MQ_PRIO_CRITICAL_CONTROL | GNUNET_MQ_PREF_LOW_LATENCY); 1128 GNUNET_MQ_PRIO_CRITICAL_CONTROL | GNUNET_MQ_PREF_LOW_LATENCY);
1135} 1129}
1136 1130
1137 1131
@@ -1144,8 +1138,8 @@ handle_tunnel_kx_auth (
1144 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise. 1138 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
1145 */ 1139 */
1146static int 1140static int
1147check_tunnel_encrypted (void *cls, 1141check_tunnel_encrypted(void *cls,
1148 const struct GNUNET_CADET_TunnelEncryptedMessage *msg) 1142 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
1149{ 1143{
1150 return GNUNET_YES; 1144 return GNUNET_YES;
1151} 1145}
@@ -1158,31 +1152,31 @@ check_tunnel_encrypted (void *cls,
1158 * @param msg Message itself. 1152 * @param msg Message itself.
1159 */ 1153 */
1160static void 1154static void
1161handle_tunnel_encrypted (void *cls, 1155handle_tunnel_encrypted(void *cls,
1162 const struct GNUNET_CADET_TunnelEncryptedMessage *msg) 1156 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
1163{ 1157{
1164 struct CadetPeer *peer = cls; 1158 struct CadetPeer *peer = cls;
1165 struct CadetConnection *cc; 1159 struct CadetConnection *cc;
1166 1160
1167 /* First, check if message belongs to a connection that ends here. */ 1161 /* First, check if message belongs to a connection that ends here. */
1168 cc = GCC_lookup (&msg->cid); 1162 cc = GCC_lookup(&msg->cid);
1169 if (NULL != cc) 1163 if (NULL != cc)
1170 {
1171 /* verify message came from the right direction */
1172 unsigned int len;
1173 struct CadetPeerPath *path = GCC_get_path (cc, &len);
1174
1175 if (peer != GCPP_get_peer_at_offset (path, 0))
1176 { 1164 {
1177 /* received message from unexpected direction, ignore! */ 1165 /* verify message came from the right direction */
1178 GNUNET_break_op (0); 1166 unsigned int len;
1167 struct CadetPeerPath *path = GCC_get_path(cc, &len);
1168
1169 if (peer != GCPP_get_peer_at_offset(path, 0))
1170 {
1171 /* received message from unexpected direction, ignore! */
1172 GNUNET_break_op(0);
1173 return;
1174 }
1175 GCC_handle_encrypted(cc, msg);
1179 return; 1176 return;
1180 } 1177 }
1181 GCC_handle_encrypted (cc, msg);
1182 return;
1183 }
1184 /* We're just an intermediary peer, route the message along its path */ 1178 /* We're just an intermediary peer, route the message along its path */
1185 route_message (peer, &msg->cid, &msg->header, GNUNET_MQ_PRIO_BEST_EFFORT); 1179 route_message(peer, &msg->cid, &msg->header, GNUNET_MQ_PRIO_BEST_EFFORT);
1186} 1180}
1187 1181
1188 1182
@@ -1199,14 +1193,14 @@ handle_tunnel_encrypted (void *cls,
1199 * @param my_identity ID of this peer, NULL if we failed 1193 * @param my_identity ID of this peer, NULL if we failed
1200 */ 1194 */
1201static void 1195static void
1202core_init_cb (void *cls, const struct GNUNET_PeerIdentity *my_identity) 1196core_init_cb(void *cls, const struct GNUNET_PeerIdentity *my_identity)
1203{ 1197{
1204 if (NULL == my_identity) 1198 if (NULL == my_identity)
1205 { 1199 {
1206 GNUNET_break (0); 1200 GNUNET_break(0);
1207 return; 1201 return;
1208 } 1202 }
1209 GNUNET_break (0 == GNUNET_memcmp (my_identity, &my_full_id)); 1203 GNUNET_break(0 == GNUNET_memcmp(my_identity, &my_full_id));
1210} 1204}
1211 1205
1212 1206
@@ -1217,17 +1211,17 @@ core_init_cb (void *cls, const struct GNUNET_PeerIdentity *my_identity)
1217 * @param peer peer identity this notification is about 1211 * @param peer peer identity this notification is about
1218 */ 1212 */
1219static void * 1213static void *
1220core_connect_cb (void *cls, 1214core_connect_cb(void *cls,
1221 const struct GNUNET_PeerIdentity *peer, 1215 const struct GNUNET_PeerIdentity *peer,
1222 struct GNUNET_MQ_Handle *mq) 1216 struct GNUNET_MQ_Handle *mq)
1223{ 1217{
1224 struct CadetPeer *cp; 1218 struct CadetPeer *cp;
1225 1219
1226 LOG (GNUNET_ERROR_TYPE_DEBUG, 1220 LOG(GNUNET_ERROR_TYPE_DEBUG,
1227 "CORE connection to peer %s was established.\n", 1221 "CORE connection to peer %s was established.\n",
1228 GNUNET_i2s (peer)); 1222 GNUNET_i2s(peer));
1229 cp = GCP_get (peer, GNUNET_YES); 1223 cp = GCP_get(peer, GNUNET_YES);
1230 GCP_set_mq (cp, mq); 1224 GCP_set_mq(cp, mq);
1231 return cp; 1225 return cp;
1232} 1226}
1233 1227
@@ -1239,16 +1233,16 @@ core_connect_cb (void *cls,
1239 * @param peer peer identity this notification is about 1233 * @param peer peer identity this notification is about
1240 */ 1234 */
1241static void 1235static void
1242core_disconnect_cb (void *cls, 1236core_disconnect_cb(void *cls,
1243 const struct GNUNET_PeerIdentity *peer, 1237 const struct GNUNET_PeerIdentity *peer,
1244 void *peer_cls) 1238 void *peer_cls)
1245{ 1239{
1246 struct CadetPeer *cp = peer_cls; 1240 struct CadetPeer *cp = peer_cls;
1247 1241
1248 LOG (GNUNET_ERROR_TYPE_DEBUG, 1242 LOG(GNUNET_ERROR_TYPE_DEBUG,
1249 "CORE connection to peer %s went down.\n", 1243 "CORE connection to peer %s went down.\n",
1250 GNUNET_i2s (peer)); 1244 GNUNET_i2s(peer));
1251 GCP_set_mq (cp, NULL); 1245 GCP_set_mq(cp, NULL);
1252} 1246}
1253 1247
1254 1248
@@ -1258,57 +1252,57 @@ core_disconnect_cb (void *cls,
1258 * @param c Configuration. 1252 * @param c Configuration.
1259 */ 1253 */
1260void 1254void
1261GCO_init (const struct GNUNET_CONFIGURATION_Handle *c) 1255GCO_init(const struct GNUNET_CONFIGURATION_Handle *c)
1262{ 1256{
1263 struct GNUNET_MQ_MessageHandler handlers[] = 1257 struct GNUNET_MQ_MessageHandler handlers[] =
1264 {GNUNET_MQ_hd_var_size (connection_create, 1258 { GNUNET_MQ_hd_var_size(connection_create,
1265 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 1259 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE,
1266 struct GNUNET_CADET_ConnectionCreateMessage, 1260 struct GNUNET_CADET_ConnectionCreateMessage,
1261 NULL),
1262 GNUNET_MQ_hd_fixed_size(connection_create_ack,
1263 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK,
1264 struct GNUNET_CADET_ConnectionCreateAckMessage,
1267 NULL), 1265 NULL),
1268 GNUNET_MQ_hd_fixed_size (connection_create_ack, 1266 GNUNET_MQ_hd_fixed_size(connection_broken,
1269 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK, 1267 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
1270 struct GNUNET_CADET_ConnectionCreateAckMessage, 1268 struct GNUNET_CADET_ConnectionBrokenMessage,
1271 NULL),
1272 GNUNET_MQ_hd_fixed_size (connection_broken,
1273 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
1274 struct GNUNET_CADET_ConnectionBrokenMessage,
1275 NULL),
1276 GNUNET_MQ_hd_fixed_size (connection_destroy,
1277 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
1278 struct GNUNET_CADET_ConnectionDestroyMessage,
1279 NULL),
1280 GNUNET_MQ_hd_fixed_size (tunnel_kx,
1281 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX,
1282 struct GNUNET_CADET_TunnelKeyExchangeMessage,
1283 NULL),
1284 GNUNET_MQ_hd_fixed_size (tunnel_kx_auth,
1285 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX_AUTH,
1286 struct GNUNET_CADET_TunnelKeyExchangeAuthMessage,
1287 NULL),
1288 GNUNET_MQ_hd_var_size (tunnel_encrypted,
1289 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED,
1290 struct GNUNET_CADET_TunnelEncryptedMessage,
1291 NULL), 1269 NULL),
1292 GNUNET_MQ_handler_end ()}; 1270 GNUNET_MQ_hd_fixed_size(connection_destroy,
1293 1271 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
1294 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c, 1272 struct GNUNET_CADET_ConnectionDestroyMessage,
1295 "CADET", 1273 NULL),
1296 "MAX_ROUTES", 1274 GNUNET_MQ_hd_fixed_size(tunnel_kx,
1297 &max_routes)) 1275 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX,
1276 struct GNUNET_CADET_TunnelKeyExchangeMessage,
1277 NULL),
1278 GNUNET_MQ_hd_fixed_size(tunnel_kx_auth,
1279 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX_AUTH,
1280 struct GNUNET_CADET_TunnelKeyExchangeAuthMessage,
1281 NULL),
1282 GNUNET_MQ_hd_var_size(tunnel_encrypted,
1283 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED,
1284 struct GNUNET_CADET_TunnelEncryptedMessage,
1285 NULL),
1286 GNUNET_MQ_handler_end() };
1287
1288 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(c,
1289 "CADET",
1290 "MAX_ROUTES",
1291 &max_routes))
1298 max_routes = 5000; 1292 max_routes = 5000;
1299 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c, 1293 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(c,
1300 "CADET", 1294 "CADET",
1301 "MAX_MSGS_QUEUE", 1295 "MAX_MSGS_QUEUE",
1302 &max_buffers)) 1296 &max_buffers))
1303 max_buffers = 10000; 1297 max_buffers = 10000;
1304 routes = GNUNET_CONTAINER_multishortmap_create (1024, GNUNET_NO); 1298 routes = GNUNET_CONTAINER_multishortmap_create(1024, GNUNET_NO);
1305 route_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN); 1299 route_heap = GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
1306 core = GNUNET_CORE_connect (c, 1300 core = GNUNET_CORE_connect(c,
1307 NULL, 1301 NULL,
1308 &core_init_cb, 1302 &core_init_cb,
1309 &core_connect_cb, 1303 &core_connect_cb,
1310 &core_disconnect_cb, 1304 &core_disconnect_cb,
1311 handlers); 1305 handlers);
1312} 1306}
1313 1307
1314 1308
@@ -1316,23 +1310,23 @@ GCO_init (const struct GNUNET_CONFIGURATION_Handle *c)
1316 * Shut down the CORE subsystem. 1310 * Shut down the CORE subsystem.
1317 */ 1311 */
1318void 1312void
1319GCO_shutdown () 1313GCO_shutdown()
1320{ 1314{
1321 if (NULL != core) 1315 if (NULL != core)
1322 { 1316 {
1323 GNUNET_CORE_disconnect (core); 1317 GNUNET_CORE_disconnect(core);
1324 core = NULL; 1318 core = NULL;
1325 } 1319 }
1326 GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (routes)); 1320 GNUNET_assert(0 == GNUNET_CONTAINER_multishortmap_size(routes));
1327 GNUNET_CONTAINER_multishortmap_destroy (routes); 1321 GNUNET_CONTAINER_multishortmap_destroy(routes);
1328 routes = NULL; 1322 routes = NULL;
1329 GNUNET_CONTAINER_heap_destroy (route_heap); 1323 GNUNET_CONTAINER_heap_destroy(route_heap);
1330 route_heap = NULL; 1324 route_heap = NULL;
1331 if (NULL != timeout_task) 1325 if (NULL != timeout_task)
1332 { 1326 {
1333 GNUNET_SCHEDULER_cancel (timeout_task); 1327 GNUNET_SCHEDULER_cancel(timeout_task);
1334 timeout_task = NULL; 1328 timeout_task = NULL;
1335 } 1329 }
1336} 1330}
1337 1331
1338/* end of gnunet-cadet-service_core.c */ 1332/* end of gnunet-cadet-service_core.c */
diff --git a/src/cadet/gnunet-service-cadet_core.h b/src/cadet/gnunet-service-cadet_core.h
index ec676c679..b049dc2af 100644
--- a/src/cadet/gnunet-service-cadet_core.h
+++ b/src/cadet/gnunet-service-cadet_core.h
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file cadet/gnunet-service-cadet_core.h 22 * @file cadet/gnunet-service-cadet_core.h
@@ -47,14 +47,14 @@ extern "C"
47 * @param c Configuration. 47 * @param c Configuration.
48 */ 48 */
49void 49void
50GCO_init (const struct GNUNET_CONFIGURATION_Handle *c); 50GCO_init(const struct GNUNET_CONFIGURATION_Handle *c);
51 51
52 52
53/** 53/**
54 * Shut down the CORE subsystem. 54 * Shut down the CORE subsystem.
55 */ 55 */
56void 56void
57GCO_shutdown (void); 57GCO_shutdown(void);
58 58
59 59
60#if 0 /* keep Emacsens' auto-indent happy */ 60#if 0 /* keep Emacsens' auto-indent happy */
diff --git a/src/cadet/gnunet-service-cadet_dht.c b/src/cadet/gnunet-service-cadet_dht.c
index 0b87eeab7..c208c7f29 100644
--- a/src/cadet/gnunet-service-cadet_dht.c
+++ b/src/cadet/gnunet-service-cadet_dht.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file cadet/gnunet-service-cadet_dht.c 21 * @file cadet/gnunet-service-cadet_dht.c
22 * @brief Information we track per peer. 22 * @brief Information we track per peer.
@@ -40,29 +40,27 @@
40 * notifications when our HELLO is ready, so this is just the maximum 40 * notifications when our HELLO is ready, so this is just the maximum
41 * we wait for the first notification. 41 * we wait for the first notification.
42 */ 42 */
43#define STARTUP_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500) 43#define STARTUP_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500)
44 44
45/** 45/**
46 * How long do we wait after we get an updated HELLO before publishing? 46 * How long do we wait after we get an updated HELLO before publishing?
47 * Allows for the HELLO to be updated again quickly, for example in 47 * Allows for the HELLO to be updated again quickly, for example in
48 * case multiple addresses changed and we got a partial update. 48 * case multiple addresses changed and we got a partial update.
49 */ 49 */
50#define CHANGE_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100) 50#define CHANGE_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100)
51 51
52 52
53#define LOG(level, ...) GNUNET_log_from (level,"cadet-dht",__VA_ARGS__) 53#define LOG(level, ...) GNUNET_log_from(level, "cadet-dht", __VA_ARGS__)
54 54
55 55
56/** 56/**
57 * Handle for DHT searches. 57 * Handle for DHT searches.
58 */ 58 */
59struct GCD_search_handle 59struct GCD_search_handle {
60{
61 /** 60 /**
62 * DHT_GET handle. 61 * DHT_GET handle.
63 */ 62 */
64 struct GNUNET_DHT_GetHandle *dhtget; 63 struct GNUNET_DHT_GetHandle *dhtget;
65
66}; 64};
67 65
68 66
@@ -109,35 +107,35 @@ static struct GNUNET_TIME_Relative announce_delay;
109 * @param data pointer to the result data 107 * @param data pointer to the result data
110 */ 108 */
111static void 109static void
112dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp, 110dht_get_id_handler(void *cls, struct GNUNET_TIME_Absolute exp,
113 const struct GNUNET_HashCode *key, 111 const struct GNUNET_HashCode *key,
114 const struct GNUNET_PeerIdentity *get_path, 112 const struct GNUNET_PeerIdentity *get_path,
115 unsigned int get_path_length, 113 unsigned int get_path_length,
116 const struct GNUNET_PeerIdentity *put_path, 114 const struct GNUNET_PeerIdentity *put_path,
117 unsigned int put_path_length, 115 unsigned int put_path_length,
118 enum GNUNET_BLOCK_Type type, 116 enum GNUNET_BLOCK_Type type,
119 size_t size, 117 size_t size,
120 const void *data) 118 const void *data)
121{ 119{
122 const struct GNUNET_HELLO_Message *hello = data; 120 const struct GNUNET_HELLO_Message *hello = data;
123 struct CadetPeer *peer; 121 struct CadetPeer *peer;
124 122
125 GCPP_try_path_from_dht (get_path, 123 GCPP_try_path_from_dht(get_path,
126 get_path_length, 124 get_path_length,
127 put_path, 125 put_path,
128 put_path_length); 126 put_path_length);
129 if ( (size >= sizeof (struct GNUNET_HELLO_Message)) && 127 if ((size >= sizeof(struct GNUNET_HELLO_Message)) &&
130 (ntohs (hello->header.size) == size) && 128 (ntohs(hello->header.size) == size) &&
131 (size == GNUNET_HELLO_size (hello)) ) 129 (size == GNUNET_HELLO_size(hello)))
132 { 130 {
133 peer = GCP_get (&put_path[0], 131 peer = GCP_get(&put_path[0],
134 GNUNET_YES); 132 GNUNET_YES);
135 LOG (GNUNET_ERROR_TYPE_DEBUG, 133 LOG(GNUNET_ERROR_TYPE_DEBUG,
136 "Got HELLO for %s\n", 134 "Got HELLO for %s\n",
137 GCP_2s (peer)); 135 GCP_2s(peer));
138 GCP_set_hello (peer, 136 GCP_set_hello(peer,
139 hello); 137 hello);
140 } 138 }
141} 139}
142 140
143 141
@@ -147,7 +145,7 @@ dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
147 * @param cls closure 145 * @param cls closure
148 */ 146 */
149static void 147static void
150announce_id (void *cls) 148announce_id(void *cls)
151{ 149{
152 struct GNUNET_HashCode phash; 150 struct GNUNET_HashCode phash;
153 const struct GNUNET_HELLO_Message *hello; 151 const struct GNUNET_HELLO_Message *hello;
@@ -155,58 +153,58 @@ announce_id (void *cls)
155 struct GNUNET_TIME_Absolute expiration; 153 struct GNUNET_TIME_Absolute expiration;
156 struct GNUNET_TIME_Relative next_put; 154 struct GNUNET_TIME_Relative next_put;
157 155
158 hello = GCH_get_mine (); 156 hello = GCH_get_mine();
159 size = (NULL != hello) ? GNUNET_HELLO_size (hello) : 0; 157 size = (NULL != hello) ? GNUNET_HELLO_size(hello) : 0;
160 if (0 == size) 158 if (0 == size)
161 { 159 {
162 expiration = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), 160 expiration = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(),
163 announce_delay); 161 announce_delay);
164 announce_delay = GNUNET_TIME_STD_BACKOFF (announce_delay); 162 announce_delay = GNUNET_TIME_STD_BACKOFF(announce_delay);
165 } 163 }
166 else 164 else
167 { 165 {
168 expiration = GNUNET_HELLO_get_last_expiration (hello); 166 expiration = GNUNET_HELLO_get_last_expiration(hello);
169 announce_delay = GNUNET_TIME_UNIT_SECONDS; 167 announce_delay = GNUNET_TIME_UNIT_SECONDS;
170 } 168 }
171 169
172 /* Call again in id_announce_time, unless HELLO expires first, 170 /* Call again in id_announce_time, unless HELLO expires first,
173 * but wait at least 1s. */ 171 * but wait at least 1s. */
174 next_put 172 next_put
175 = GNUNET_TIME_absolute_get_remaining (expiration); 173 = GNUNET_TIME_absolute_get_remaining(expiration);
176 next_put 174 next_put
177 = GNUNET_TIME_relative_min (next_put, 175 = GNUNET_TIME_relative_min(next_put,
178 id_announce_time); 176 id_announce_time);
179 next_put 177 next_put
180 = GNUNET_TIME_relative_max (next_put, 178 = GNUNET_TIME_relative_max(next_put,
181 GNUNET_TIME_UNIT_SECONDS); 179 GNUNET_TIME_UNIT_SECONDS);
182 announce_id_task 180 announce_id_task
183 = GNUNET_SCHEDULER_add_delayed (next_put, 181 = GNUNET_SCHEDULER_add_delayed(next_put,
184 &announce_id, 182 &announce_id,
185 cls); 183 cls);
186 GNUNET_STATISTICS_update (stats, 184 GNUNET_STATISTICS_update(stats,
187 "# DHT announce", 185 "# DHT announce",
188 1, 186 1,
189 GNUNET_NO); 187 GNUNET_NO);
190 memset (&phash, 188 memset(&phash,
191 0, 189 0,
192 sizeof (phash)); 190 sizeof(phash));
193 GNUNET_memcpy (&phash, 191 GNUNET_memcpy(&phash,
194 &my_full_id, 192 &my_full_id,
195 sizeof (my_full_id)); 193 sizeof(my_full_id));
196 LOG (GNUNET_ERROR_TYPE_DEBUG, 194 LOG(GNUNET_ERROR_TYPE_DEBUG,
197 "Announcing my HELLO (%u bytes) in the DHT\n", 195 "Announcing my HELLO (%u bytes) in the DHT\n",
198 size); 196 size);
199 GNUNET_DHT_put (dht_handle, /* DHT handle */ 197 GNUNET_DHT_put(dht_handle, /* DHT handle */
200 &phash, /* Key to use */ 198 &phash, /* Key to use */
201 dht_replication_level, /* Replication level */ 199 dht_replication_level, /* Replication level */
202 GNUNET_DHT_RO_RECORD_ROUTE 200 GNUNET_DHT_RO_RECORD_ROUTE
203 | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */ 201 | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
204 GNUNET_BLOCK_TYPE_DHT_HELLO, /* Block type */ 202 GNUNET_BLOCK_TYPE_DHT_HELLO, /* Block type */
205 size, /* Size of the data */ 203 size, /* Size of the data */
206 (const char *) hello, /* Data itself */ 204 (const char *)hello, /* Data itself */
207 expiration, /* Data expiration */ 205 expiration, /* Data expiration */
208 NULL, /* Continuation */ 206 NULL, /* Continuation */
209 NULL); /* Continuation closure */ 207 NULL); /* Continuation closure */
210} 208}
211 209
212 210
@@ -215,15 +213,15 @@ announce_id (void *cls)
215 * changes. Re-triggers the DHT PUT immediately. 213 * changes. Re-triggers the DHT PUT immediately.
216 */ 214 */
217void 215void
218GCD_hello_update () 216GCD_hello_update()
219{ 217{
220 if (NULL == announce_id_task) 218 if (NULL == announce_id_task)
221 return; /* too early */ 219 return; /* too early */
222 GNUNET_SCHEDULER_cancel (announce_id_task); 220 GNUNET_SCHEDULER_cancel(announce_id_task);
223 announce_id_task 221 announce_id_task
224 = GNUNET_SCHEDULER_add_delayed (CHANGE_DELAY, 222 = GNUNET_SCHEDULER_add_delayed(CHANGE_DELAY,
225 &announce_id, 223 &announce_id,
226 NULL); 224 NULL);
227} 225}
228 226
229 227
@@ -233,42 +231,42 @@ GCD_hello_update ()
233 * @param c Configuration. 231 * @param c Configuration.
234 */ 232 */
235void 233void
236GCD_init (const struct GNUNET_CONFIGURATION_Handle *c) 234GCD_init(const struct GNUNET_CONFIGURATION_Handle *c)
237{ 235{
238 if (GNUNET_OK != 236 if (GNUNET_OK !=
239 GNUNET_CONFIGURATION_get_value_number (c, 237 GNUNET_CONFIGURATION_get_value_number(c,
240 "CADET", 238 "CADET",
241 "DHT_REPLICATION_LEVEL", 239 "DHT_REPLICATION_LEVEL",
242 &dht_replication_level)) 240 &dht_replication_level))
243 { 241 {
244 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING, 242 GNUNET_log_config_invalid(GNUNET_ERROR_TYPE_WARNING,
245 "CADET", 243 "CADET",
246 "DHT_REPLICATION_LEVEL", 244 "DHT_REPLICATION_LEVEL",
247 "USING DEFAULT"); 245 "USING DEFAULT");
248 dht_replication_level = 3; 246 dht_replication_level = 3;
249 } 247 }
250 248
251 if (GNUNET_OK != 249 if (GNUNET_OK !=
252 GNUNET_CONFIGURATION_get_value_time (c, 250 GNUNET_CONFIGURATION_get_value_time(c,
253 "CADET", 251 "CADET",
254 "ID_ANNOUNCE_TIME", 252 "ID_ANNOUNCE_TIME",
255 &id_announce_time)) 253 &id_announce_time))
256 { 254 {
257 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 255 GNUNET_log_config_invalid(GNUNET_ERROR_TYPE_ERROR,
258 "CADET", 256 "CADET",
259 "ID_ANNOUNCE_TIME", 257 "ID_ANNOUNCE_TIME",
260 "MISSING"); 258 "MISSING");
261 GNUNET_SCHEDULER_shutdown (); 259 GNUNET_SCHEDULER_shutdown();
262 return; 260 return;
263 } 261 }
264 262
265 dht_handle = GNUNET_DHT_connect (c, 263 dht_handle = GNUNET_DHT_connect(c,
266 64); 264 64);
267 GNUNET_break (NULL != dht_handle); 265 GNUNET_break(NULL != dht_handle);
268 announce_delay = GNUNET_TIME_UNIT_SECONDS; 266 announce_delay = GNUNET_TIME_UNIT_SECONDS;
269 announce_id_task = GNUNET_SCHEDULER_add_delayed (STARTUP_DELAY, 267 announce_id_task = GNUNET_SCHEDULER_add_delayed(STARTUP_DELAY,
270 &announce_id, 268 &announce_id,
271 NULL); 269 NULL);
272} 270}
273 271
274 272
@@ -276,18 +274,18 @@ GCD_init (const struct GNUNET_CONFIGURATION_Handle *c)
276 * Shut down the DHT subsystem. 274 * Shut down the DHT subsystem.
277 */ 275 */
278void 276void
279GCD_shutdown (void) 277GCD_shutdown(void)
280{ 278{
281 if (NULL != dht_handle) 279 if (NULL != dht_handle)
282 { 280 {
283 GNUNET_DHT_disconnect (dht_handle); 281 GNUNET_DHT_disconnect(dht_handle);
284 dht_handle = NULL; 282 dht_handle = NULL;
285 } 283 }
286 if (NULL != announce_id_task) 284 if (NULL != announce_id_task)
287 { 285 {
288 GNUNET_SCHEDULER_cancel (announce_id_task); 286 GNUNET_SCHEDULER_cancel(announce_id_task);
289 announce_id_task = NULL; 287 announce_id_task = NULL;
290 } 288 }
291} 289}
292 290
293 291
@@ -298,37 +296,37 @@ GCD_shutdown (void)
298 * @return handle to abort search 296 * @return handle to abort search
299 */ 297 */
300struct GCD_search_handle * 298struct GCD_search_handle *
301GCD_search (const struct GNUNET_PeerIdentity *peer_id) 299GCD_search(const struct GNUNET_PeerIdentity *peer_id)
302{ 300{
303 struct GNUNET_HashCode phash; 301 struct GNUNET_HashCode phash;
304 struct GCD_search_handle *h; 302 struct GCD_search_handle *h;
305 303
306 GNUNET_STATISTICS_update (stats, 304 GNUNET_STATISTICS_update(stats,
307 "# DHT search", 305 "# DHT search",
308 1, 306 1,
309 GNUNET_NO); 307 GNUNET_NO);
310 memset (&phash, 308 memset(&phash,
311 0, 309 0,
312 sizeof (phash)); 310 sizeof(phash));
313 GNUNET_memcpy (&phash, 311 GNUNET_memcpy(&phash,
314 peer_id, 312 peer_id,
315 sizeof (*peer_id)); 313 sizeof(*peer_id));
316 314
317 h = GNUNET_new (struct GCD_search_handle); 315 h = GNUNET_new(struct GCD_search_handle);
318 h->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */ 316 h->dhtget = GNUNET_DHT_get_start(dht_handle, /* handle */
319 GNUNET_BLOCK_TYPE_DHT_HELLO, /* type */ 317 GNUNET_BLOCK_TYPE_DHT_HELLO, /* type */
320 &phash, /* key to search */ 318 &phash, /* key to search */
321 dht_replication_level, /* replication level */ 319 dht_replication_level, /* replication level */
322 GNUNET_DHT_RO_RECORD_ROUTE | 320 GNUNET_DHT_RO_RECORD_ROUTE |
323 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, 321 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
324 NULL, /* xquery */ 322 NULL, /* xquery */
325 0, /* xquery bits */ 323 0, /* xquery bits */
326 &dht_get_id_handler, 324 &dht_get_id_handler,
327 h); 325 h);
328 LOG (GNUNET_ERROR_TYPE_DEBUG, 326 LOG(GNUNET_ERROR_TYPE_DEBUG,
329 "Starting DHT GET for peer %s (%p)\n", 327 "Starting DHT GET for peer %s (%p)\n",
330 GNUNET_i2s (peer_id), 328 GNUNET_i2s(peer_id),
331 h); 329 h);
332 return h; 330 return h;
333} 331}
334 332
@@ -339,13 +337,13 @@ GCD_search (const struct GNUNET_PeerIdentity *peer_id)
339 * @param h handle to search to stop 337 * @param h handle to search to stop
340 */ 338 */
341void 339void
342GCD_search_stop (struct GCD_search_handle *h) 340GCD_search_stop(struct GCD_search_handle *h)
343{ 341{
344 LOG (GNUNET_ERROR_TYPE_DEBUG, 342 LOG(GNUNET_ERROR_TYPE_DEBUG,
345 "Stopping DHT GET %p\n", 343 "Stopping DHT GET %p\n",
346 h); 344 h);
347 GNUNET_DHT_get_stop (h->dhtget); 345 GNUNET_DHT_get_stop(h->dhtget);
348 GNUNET_free (h); 346 GNUNET_free(h);
349} 347}
350 348
351/* end of gnunet-service-cadet_dht.c */ 349/* end of gnunet-service-cadet_dht.c */
diff --git a/src/cadet/gnunet-service-cadet_dht.h b/src/cadet/gnunet-service-cadet_dht.h
index 03f045938..4cb6e9fff 100644
--- a/src/cadet/gnunet-service-cadet_dht.h
+++ b/src/cadet/gnunet-service-cadet_dht.h
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file cadet/gnunet-service-cadet_dht.h 22 * @file cadet/gnunet-service-cadet_dht.h
@@ -52,14 +52,14 @@ struct GCD_search_handle;
52 * @param c Configuration. 52 * @param c Configuration.
53 */ 53 */
54void 54void
55GCD_init (const struct GNUNET_CONFIGURATION_Handle *c); 55GCD_init(const struct GNUNET_CONFIGURATION_Handle *c);
56 56
57 57
58/** 58/**
59 * Shut down the DHT subsystem. 59 * Shut down the DHT subsystem.
60 */ 60 */
61void 61void
62GCD_shutdown (void); 62GCD_shutdown(void);
63 63
64 64
65/** 65/**
@@ -67,7 +67,7 @@ GCD_shutdown (void);
67 * changes. Re-triggers the DHT PUT immediately. 67 * changes. Re-triggers the DHT PUT immediately.
68 */ 68 */
69void 69void
70GCD_hello_update (void); 70GCD_hello_update(void);
71 71
72/** 72/**
73 * Search DHT for paths to @a peeR_id 73 * Search DHT for paths to @a peeR_id
@@ -76,7 +76,7 @@ GCD_hello_update (void);
76 * @return handle to abort search 76 * @return handle to abort search
77 */ 77 */
78struct GCD_search_handle * 78struct GCD_search_handle *
79GCD_search (const struct GNUNET_PeerIdentity *peer_id); 79GCD_search(const struct GNUNET_PeerIdentity *peer_id);
80 80
81 81
82/** 82/**
@@ -85,7 +85,7 @@ GCD_search (const struct GNUNET_PeerIdentity *peer_id);
85 * @param h handle to search to stop 85 * @param h handle to search to stop
86 */ 86 */
87void 87void
88GCD_search_stop (struct GCD_search_handle *h); 88GCD_search_stop(struct GCD_search_handle *h);
89 89
90 90
91#if 0 /* keep Emacsens' auto-indent happy */ 91#if 0 /* keep Emacsens' auto-indent happy */
diff --git a/src/cadet/gnunet-service-cadet_hello.c b/src/cadet/gnunet-service-cadet_hello.c
index fe2477c34..7d6d2b0ce 100644
--- a/src/cadet/gnunet-service-cadet_hello.c
+++ b/src/cadet/gnunet-service-cadet_hello.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file cadet/gnunet-service-cadet_hello.c 21 * @file cadet/gnunet-service-cadet_hello.c
22 * @brief spread knowledge about how to contact us (get HELLO from peerinfo), 22 * @brief spread knowledge about how to contact us (get HELLO from peerinfo),
@@ -35,7 +35,7 @@
35#include "gnunet-service-cadet_hello.h" 35#include "gnunet-service-cadet_hello.h"
36#include "gnunet-service-cadet_peer.h" 36#include "gnunet-service-cadet_peer.h"
37 37
38#define LOG(level, ...) GNUNET_log_from(level,"cadet-hll",__VA_ARGS__) 38#define LOG(level, ...) GNUNET_log_from(level, "cadet-hll", __VA_ARGS__)
39 39
40/** 40/**
41 * Hello message of local peer. 41 * Hello message of local peer.
@@ -62,34 +62,34 @@ static struct GNUNET_PEERINFO_NotifyContext *nc;
62 * @param err_msg Error message. 62 * @param err_msg Error message.
63 */ 63 */
64static void 64static void
65got_hello (void *cls, 65got_hello(void *cls,
66 const struct GNUNET_PeerIdentity *id, 66 const struct GNUNET_PeerIdentity *id,
67 const struct GNUNET_HELLO_Message *hello, 67 const struct GNUNET_HELLO_Message *hello,
68 const char *err_msg) 68 const char *err_msg)
69{ 69{
70 struct CadetPeer *peer; 70 struct CadetPeer *peer;
71 71
72 if ( (NULL == id) || 72 if ((NULL == id) ||
73 (NULL == hello) ) 73 (NULL == hello))
74 return;
75 if (0 == GNUNET_memcmp (id,
76 &my_full_id))
77 {
78 GNUNET_free_non_null (mine);
79 mine = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (&hello->header);
80 GCD_hello_update ();
81 return; 74 return;
82 } 75 if (0 == GNUNET_memcmp(id,
83 76 &my_full_id))
84 LOG (GNUNET_ERROR_TYPE_DEBUG, 77 {
85 "Hello for %s (%d bytes), expires on %s\n", 78 GNUNET_free_non_null(mine);
86 GNUNET_i2s (id), 79 mine = (struct GNUNET_HELLO_Message *)GNUNET_copy_message(&hello->header);
87 GNUNET_HELLO_size (hello), 80 GCD_hello_update();
88 GNUNET_STRINGS_absolute_time_to_string (GNUNET_HELLO_get_last_expiration (hello))); 81 return;
89 peer = GCP_get (id, 82 }
90 GNUNET_YES); 83
91 GCP_set_hello (peer, 84 LOG(GNUNET_ERROR_TYPE_DEBUG,
92 hello); 85 "Hello for %s (%d bytes), expires on %s\n",
86 GNUNET_i2s(id),
87 GNUNET_HELLO_size(hello),
88 GNUNET_STRINGS_absolute_time_to_string(GNUNET_HELLO_get_last_expiration(hello)));
89 peer = GCP_get(id,
90 GNUNET_YES);
91 GCP_set_hello(peer,
92 hello);
93} 93}
94 94
95 95
@@ -99,14 +99,14 @@ got_hello (void *cls,
99 * @param c Configuration. 99 * @param c Configuration.
100 */ 100 */
101void 101void
102GCH_init (const struct GNUNET_CONFIGURATION_Handle *c) 102GCH_init(const struct GNUNET_CONFIGURATION_Handle *c)
103{ 103{
104 GNUNET_assert (NULL == nc); 104 GNUNET_assert(NULL == nc);
105 peerinfo = GNUNET_PEERINFO_connect (c); 105 peerinfo = GNUNET_PEERINFO_connect(c);
106 nc = GNUNET_PEERINFO_notify (c, 106 nc = GNUNET_PEERINFO_notify(c,
107 GNUNET_NO, 107 GNUNET_NO,
108 &got_hello, 108 &got_hello,
109 NULL); 109 NULL);
110} 110}
111 111
112 112
@@ -114,23 +114,23 @@ GCH_init (const struct GNUNET_CONFIGURATION_Handle *c)
114 * Shut down the hello subsystem. 114 * Shut down the hello subsystem.
115 */ 115 */
116void 116void
117GCH_shutdown () 117GCH_shutdown()
118{ 118{
119 if (NULL != nc) 119 if (NULL != nc)
120 { 120 {
121 GNUNET_PEERINFO_notify_cancel (nc); 121 GNUNET_PEERINFO_notify_cancel(nc);
122 nc = NULL; 122 nc = NULL;
123 } 123 }
124 if (NULL != peerinfo) 124 if (NULL != peerinfo)
125 { 125 {
126 GNUNET_PEERINFO_disconnect (peerinfo); 126 GNUNET_PEERINFO_disconnect(peerinfo);
127 peerinfo = NULL; 127 peerinfo = NULL;
128 } 128 }
129 if (NULL != mine) 129 if (NULL != mine)
130 { 130 {
131 GNUNET_free (mine); 131 GNUNET_free(mine);
132 mine = NULL; 132 mine = NULL;
133 } 133 }
134} 134}
135 135
136 136
@@ -140,7 +140,7 @@ GCH_shutdown ()
140 * @return Own hello message. 140 * @return Own hello message.
141 */ 141 */
142const struct GNUNET_HELLO_Message * 142const struct GNUNET_HELLO_Message *
143GCH_get_mine (void) 143GCH_get_mine(void)
144{ 144{
145 return mine; 145 return mine;
146} 146}
diff --git a/src/cadet/gnunet-service-cadet_hello.h b/src/cadet/gnunet-service-cadet_hello.h
index 821cd4ff4..99b6c807d 100644
--- a/src/cadet/gnunet-service-cadet_hello.h
+++ b/src/cadet/gnunet-service-cadet_hello.h
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file cadet/gnunet-service-cadet_hello.h 22 * @file cadet/gnunet-service-cadet_hello.h
@@ -49,14 +49,14 @@ extern "C"
49 * @param c Configuration. 49 * @param c Configuration.
50 */ 50 */
51void 51void
52GCH_init (const struct GNUNET_CONFIGURATION_Handle *c); 52GCH_init(const struct GNUNET_CONFIGURATION_Handle *c);
53 53
54 54
55/** 55/**
56 * Shut down the hello subsystem. 56 * Shut down the hello subsystem.
57 */ 57 */
58void 58void
59GCH_shutdown (void); 59GCH_shutdown(void);
60 60
61 61
62/** 62/**
@@ -65,7 +65,7 @@ GCH_shutdown (void);
65 * @return Own hello message. 65 * @return Own hello message.
66 */ 66 */
67const struct GNUNET_HELLO_Message * 67const struct GNUNET_HELLO_Message *
68GCH_get_mine (void); 68GCH_get_mine(void);
69 69
70 70
71#if 0 /* keep Emacsens' auto-indent happy */ 71#if 0 /* keep Emacsens' auto-indent happy */
diff --git a/src/cadet/gnunet-service-cadet_paths.c b/src/cadet/gnunet-service-cadet_paths.c
index bdc92668e..149ac659a 100644
--- a/src/cadet/gnunet-service-cadet_paths.c
+++ b/src/cadet/gnunet-service-cadet_paths.c
@@ -11,15 +11,15 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file cadet/gnunet-service-cadet_paths.c 21 * @file cadet/gnunet-service-cadet_paths.c
22 * @brief Information we track per path. 22 * @brief Information we track per path.
23 * @author Bartlomiej Polot 23 * @author Bartlomiej Polot
24 * @author Christian Grothoff 24 * @author Christian Grothoff
25 */ 25 */
@@ -30,15 +30,13 @@
30#include "gnunet-service-cadet_paths.h" 30#include "gnunet-service-cadet_paths.h"
31 31
32 32
33#define LOG(level, ...) GNUNET_log_from(level,"cadet-pat",__VA_ARGS__) 33#define LOG(level, ...) GNUNET_log_from(level, "cadet-pat", __VA_ARGS__)
34 34
35 35
36/** 36/**
37 * Information regarding a possible path to reach a peer. 37 * Information regarding a possible path to reach a peer.
38 */ 38 */
39struct CadetPeerPath 39struct CadetPeerPath {
40{
41
42 /** 40 /**
43 * Array of all the peers on the path. If @e hn is non-NULL, the 41 * Array of all the peers on the path. If @e hn is non-NULL, the
44 * last one is our owner. 42 * last one is our owner.
@@ -61,7 +59,6 @@ struct CadetPeerPath
61 * Length of the @e entries array. 59 * Length of the @e entries array.
62 */ 60 */
63 unsigned int entries_length; 61 unsigned int entries_length;
64
65}; 62};
66 63
67 64
@@ -71,18 +68,18 @@ struct CadetPeerPath
71 * @param path path to calculate the score for 68 * @param path path to calculate the score for
72 */ 69 */
73static void 70static void
74recalculate_path_desirability (struct CadetPeerPath *path) 71recalculate_path_desirability(struct CadetPeerPath *path)
75{ 72{
76 double result = 0.0; 73 double result = 0.0;
77 74
78 for (unsigned int i=0;i<path->entries_length;i++) 75 for (unsigned int i = 0; i < path->entries_length; i++)
79 { 76 {
80 struct CadetPeer *cp = path->entries[i]->peer; 77 struct CadetPeer *cp = path->entries[i]->peer;
81 78
82 result += GCP_get_desirability_of_path (cp, 79 result += GCP_get_desirability_of_path(cp,
83 i); 80 i);
84 } 81 }
85 path->desirability = (GNUNET_CONTAINER_HeapCostType) result; 82 path->desirability = (GNUNET_CONTAINER_HeapCostType)result;
86} 83}
87 84
88 85
@@ -100,7 +97,7 @@ recalculate_path_desirability (struct CadetPeerPath *path)
100 * @return desirability of the path, larger is more desirable 97 * @return desirability of the path, larger is more desirable
101 */ 98 */
102GNUNET_CONTAINER_HeapCostType 99GNUNET_CONTAINER_HeapCostType
103GCPP_get_desirability (const struct CadetPeerPath *path) 100GCPP_get_desirability(const struct CadetPeerPath *path)
104{ 101{
105 return path->desirability; 102 return path->desirability;
106} 103}
@@ -117,15 +114,15 @@ GCPP_get_desirability (const struct CadetPeerPath *path)
117 * otherwise connection from us to @a destination via @a path 114 * otherwise connection from us to @a destination via @a path
118 */ 115 */
119struct CadetConnection * 116struct CadetConnection *
120GCPP_get_connection (struct CadetPeerPath *path, 117GCPP_get_connection(struct CadetPeerPath *path,
121 struct CadetPeer *destination, 118 struct CadetPeer *destination,
122 unsigned int off) 119 unsigned int off)
123{ 120{
124 struct CadetPeerPathEntry *entry; 121 struct CadetPeerPathEntry *entry;
125 122
126 GNUNET_assert (off < path->entries_length); 123 GNUNET_assert(off < path->entries_length);
127 entry = path->entries[off]; 124 entry = path->entries[off];
128 GNUNET_assert (entry->peer == destination); 125 GNUNET_assert(entry->peer == destination);
129 return entry->cc; 126 return entry->cc;
130} 127}
131 128
@@ -139,21 +136,21 @@ GCPP_get_connection (struct CadetPeerPath *path,
139 * @param cc the connection to remember 136 * @param cc the connection to remember
140 */ 137 */
141void 138void
142GCPP_add_connection (struct CadetPeerPath *path, 139GCPP_add_connection(struct CadetPeerPath *path,
143 unsigned int off, 140 unsigned int off,
144 struct CadetConnection *cc) 141 struct CadetConnection *cc)
145{ 142{
146 struct CadetPeerPathEntry *entry; 143 struct CadetPeerPathEntry *entry;
147 144
148 LOG (GNUNET_ERROR_TYPE_DEBUG, 145 LOG(GNUNET_ERROR_TYPE_DEBUG,
149 "Adding %s to path %s at offset %u\n", 146 "Adding %s to path %s at offset %u\n",
150 GCC_2s (cc), 147 GCC_2s(cc),
151 GCPP_2s (path), 148 GCPP_2s(path),
152 off); 149 off);
153 GNUNET_assert (off < path->entries_length); 150 GNUNET_assert(off < path->entries_length);
154 entry = path->entries[off]; 151 entry = path->entries[off];
155 GNUNET_assert (NULL == entry->cc); 152 GNUNET_assert(NULL == entry->cc);
156 GNUNET_assert (NULL != cc); 153 GNUNET_assert(NULL != cc);
157 entry->cc = cc; 154 entry->cc = cc;
158} 155}
159 156
@@ -168,20 +165,20 @@ GCPP_add_connection (struct CadetPeerPath *path,
168 * @param cc the connection to forget 165 * @param cc the connection to forget
169 */ 166 */
170void 167void
171GCPP_del_connection (struct CadetPeerPath *path, 168GCPP_del_connection(struct CadetPeerPath *path,
172 unsigned int off, 169 unsigned int off,
173 struct CadetConnection *cc) 170 struct CadetConnection *cc)
174{ 171{
175 struct CadetPeerPathEntry *entry; 172 struct CadetPeerPathEntry *entry;
176 173
177 LOG (GNUNET_ERROR_TYPE_DEBUG, 174 LOG(GNUNET_ERROR_TYPE_DEBUG,
178 "Removing connection %s to path %s at offset %u\n", 175 "Removing connection %s to path %s at offset %u\n",
179 GCC_2s (cc), 176 GCC_2s(cc),
180 GCPP_2s (path), 177 GCPP_2s(path),
181 off); 178 off);
182 GNUNET_assert (off < path->entries_length); 179 GNUNET_assert(off < path->entries_length);
183 entry = path->entries[off]; 180 entry = path->entries[off];
184 GNUNET_assert (cc == entry->cc); 181 GNUNET_assert(cc == entry->cc);
185 entry->cc = NULL; 182 entry->cc = NULL;
186} 183}
187 184
@@ -196,42 +193,42 @@ GCPP_del_connection (struct CadetPeerPath *path,
196 * @param stop_at the path length at which to stop trying 193 * @param stop_at the path length at which to stop trying
197 */ 194 */
198static void 195static void
199attach_path (struct CadetPeerPath *path, unsigned int stop_at) 196attach_path(struct CadetPeerPath *path, unsigned int stop_at)
200{ 197{
201 GNUNET_assert (NULL == path->hn); 198 GNUNET_assert(NULL == path->hn);
202 199
203 /* Try to attach this path to a peer, working backwards from the end. */ 200 /* Try to attach this path to a peer, working backwards from the end. */
204 while (path->entries_length > stop_at) 201 while (path->entries_length > stop_at)
205 { 202 {
206 unsigned int end = path->entries_length - 1; 203 unsigned int end = path->entries_length - 1;
207 struct CadetPeerPathEntry *entry = path->entries[end]; 204 struct CadetPeerPathEntry *entry = path->entries[end];
208 int force = GNUNET_NO; 205 int force = GNUNET_NO;
209 206
210 recalculate_path_desirability (path); 207 recalculate_path_desirability(path);
211 /* If the entry already has a connection using it, force attach. */ 208 /* If the entry already has a connection using it, force attach. */
212 if (NULL != entry->cc) 209 if (NULL != entry->cc)
213 force = GNUNET_YES; 210 force = GNUNET_YES;
214 path->hn = GCP_attach_path (entry->peer, 211 path->hn = GCP_attach_path(entry->peer,
215 path, 212 path,
216 end, 213 end,
217 force); 214 force);
218 if (NULL != path->hn) 215 if (NULL != path->hn)
219 break; 216 break;
220 217
221 /* Attach failed, trim this entry from the path. */ 218 /* Attach failed, trim this entry from the path. */
222 GNUNET_assert (NULL == entry->cc); 219 GNUNET_assert(NULL == entry->cc);
223 GCP_path_entry_remove (entry->peer, 220 GCP_path_entry_remove(entry->peer,
224 entry, 221 entry,
225 end); 222 end);
226 GNUNET_free (entry); 223 GNUNET_free(entry);
227 path->entries[end] = NULL; 224 path->entries[end] = NULL;
228 path->entries_length--; 225 path->entries_length--;
229 } 226 }
230 227
231 /* Shrink array to actual path length. */ 228 /* Shrink array to actual path length. */
232 GNUNET_array_grow (path->entries, 229 GNUNET_array_grow(path->entries,
233 path->entries_length, 230 path->entries_length,
234 path->entries_length); 231 path->entries_length);
235} 232}
236 233
237 234
@@ -243,33 +240,33 @@ attach_path (struct CadetPeerPath *path, unsigned int stop_at)
243 * @param path the path that is being released 240 * @param path the path that is being released
244 */ 241 */
245void 242void
246GCPP_release (struct CadetPeerPath *path) 243GCPP_release(struct CadetPeerPath *path)
247{ 244{
248 struct CadetPeerPathEntry *entry; 245 struct CadetPeerPathEntry *entry;
249 246
250 LOG (GNUNET_ERROR_TYPE_DEBUG, 247 LOG(GNUNET_ERROR_TYPE_DEBUG,
251 "Owner releases path %s\n", 248 "Owner releases path %s\n",
252 GCPP_2s (path)); 249 GCPP_2s(path));
253 path->hn = NULL; 250 path->hn = NULL;
254 entry = path->entries[path->entries_length - 1]; 251 entry = path->entries[path->entries_length - 1];
255 GNUNET_assert (path == entry->path); 252 GNUNET_assert(path == entry->path);
256 GNUNET_assert (NULL == entry->cc); 253 GNUNET_assert(NULL == entry->cc);
257 /* cut 'off' end of path */ 254 /* cut 'off' end of path */
258 GCP_path_entry_remove (entry->peer, 255 GCP_path_entry_remove(entry->peer,
259 entry, 256 entry,
260 path->entries_length - 1); 257 path->entries_length - 1);
261 GNUNET_free (entry); 258 GNUNET_free(entry);
262 path->entries[path->entries_length - 1] = NULL; 259 path->entries[path->entries_length - 1] = NULL;
263 path->entries_length--; 260 path->entries_length--;
264 /* see if new peer at the end likes this path any better */ 261 /* see if new peer at the end likes this path any better */
265 attach_path (path, 0); 262 attach_path(path, 0);
266 if (NULL == path->hn) 263 if (NULL == path->hn)
267 { 264 {
268 /* nobody wants us, discard the path */ 265 /* nobody wants us, discard the path */
269 GNUNET_assert (0 == path->entries_length); 266 GNUNET_assert(0 == path->entries_length);
270 GNUNET_assert (NULL == path->entries); 267 GNUNET_assert(NULL == path->entries);
271 GNUNET_free (path); 268 GNUNET_free(path);
272 } 269 }
273} 270}
274 271
275 272
@@ -282,40 +279,38 @@ GCPP_release (struct CadetPeerPath *path)
282 * @param delta change in the score to apply 279 * @param delta change in the score to apply
283 */ 280 */
284void 281void
285GCPP_update_score (struct CadetPeerPath *path, 282GCPP_update_score(struct CadetPeerPath *path,
286 unsigned int off, 283 unsigned int off,
287 int delta) 284 int delta)
288{ 285{
289 struct CadetPeerPathEntry *entry; 286 struct CadetPeerPathEntry *entry;
290 287
291 GNUNET_assert (off < path->entries_length); 288 GNUNET_assert(off < path->entries_length);
292 entry = path->entries[off]; 289 entry = path->entries[off];
293 290
294 /* Add delta, with checks for overflows */ 291 /* Add delta, with checks for overflows */
295 if (delta >= 0) 292 if (delta >= 0)
296 { 293 {
297 if (delta + entry->score < entry->score) 294 if (delta + entry->score < entry->score)
298 entry->score = INT_MAX; 295 entry->score = INT_MAX;
299 else 296 else
300 entry->score += delta; 297 entry->score += delta;
301 } 298 }
302 else 299 else
303 { 300 {
304 if (delta + entry->score > entry->score) 301 if (delta + entry->score > entry->score)
305 entry->score = INT_MIN; 302 entry->score = INT_MIN;
306 else 303 else
307 entry->score += delta; 304 entry->score += delta;
308 } 305 }
309 recalculate_path_desirability (path); 306 recalculate_path_desirability(path);
310} 307}
311 308
312 309
313/** 310/**
314 * Closure for #find_peer_at() and #check_match(). 311 * Closure for #find_peer_at() and #check_match().
315 */ 312 */
316struct CheckMatchContext 313struct CheckMatchContext {
317{
318
319 /** 314 /**
320 * Set to a matching path, if any. 315 * Set to a matching path, if any.
321 */ 316 */
@@ -330,7 +325,6 @@ struct CheckMatchContext
330 * How long is the @e cpath array? 325 * How long is the @e cpath array?
331 */ 326 */
332 unsigned int cpath_length; 327 unsigned int cpath_length;
333
334}; 328};
335 329
336 330
@@ -345,38 +339,38 @@ struct CheckMatchContext
345 * @return #GNUNET_YES (continue to iterate), or if found #GNUNET_NO 339 * @return #GNUNET_YES (continue to iterate), or if found #GNUNET_NO
346 */ 340 */
347static int 341static int
348check_match (void *cls, 342check_match(void *cls,
349 struct CadetPeerPath *path, 343 struct CadetPeerPath *path,
350 unsigned int off) 344 unsigned int off)
351{ 345{
352 struct CheckMatchContext *cm_ctx = cls; 346 struct CheckMatchContext *cm_ctx = cls;
353 347
354 GNUNET_assert (path->entries_length > off); 348 GNUNET_assert(path->entries_length > off);
355 if ( (path->entries_length != off + 1) && 349 if ((path->entries_length != off + 1) &&
356 (off + 1 != cm_ctx->cpath_length) ) 350 (off + 1 != cm_ctx->cpath_length))
357 {
358 LOG (GNUNET_ERROR_TYPE_DEBUG,
359 "check_match mismatch because path %s is too long (%u vs. %u vs. %u)\n",
360 GCPP_2s (path),
361 path->entries_length,
362 off + 1,
363 cm_ctx->cpath_length);
364 return GNUNET_YES; /* too long, goes somewhere else already, thus cannot be useful */
365 }
366 for (unsigned int i=0;i<off;i++)
367 if (cm_ctx->cpath[i] !=
368 GCPP_get_peer_at_offset (path,
369 i))
370 { 351 {
371 LOG (GNUNET_ERROR_TYPE_DEBUG, 352 LOG(GNUNET_ERROR_TYPE_DEBUG,
372 "check_match path %s mismatches at offset %u\n", 353 "check_match mismatch because path %s is too long (%u vs. %u vs. %u)\n",
373 GCPP_2s (path), 354 GCPP_2s(path),
374 i); 355 path->entries_length,
375 return GNUNET_YES; /* mismatch, ignore */ 356 off + 1,
357 cm_ctx->cpath_length);
358 return GNUNET_YES; /* too long, goes somewhere else already, thus cannot be useful */
376 } 359 }
377 LOG (GNUNET_ERROR_TYPE_DEBUG, 360 for (unsigned int i = 0; i < off; i++)
378 "check_match found match with path %s\n", 361 if (cm_ctx->cpath[i] !=
379 GCPP_2s (path)); 362 GCPP_get_peer_at_offset(path,
363 i))
364 {
365 LOG(GNUNET_ERROR_TYPE_DEBUG,
366 "check_match path %s mismatches at offset %u\n",
367 GCPP_2s(path),
368 i);
369 return GNUNET_YES; /* mismatch, ignore */
370 }
371 LOG(GNUNET_ERROR_TYPE_DEBUG,
372 "check_match found match with path %s\n",
373 GCPP_2s(path));
380 cm_ctx->match = path; 374 cm_ctx->match = path;
381 return GNUNET_NO; /* match, we are done! */ 375 return GNUNET_NO; /* match, we are done! */
382} 376}
@@ -393,68 +387,70 @@ check_match (void *cls,
393 * paths already 387 * paths already
394 */ 388 */
395static void 389static void
396extend_path (struct CadetPeerPath *path, 390extend_path(struct CadetPeerPath *path,
397 struct CadetPeer **peers, 391 struct CadetPeer **peers,
398 unsigned int num_peers, 392 unsigned int num_peers,
399 int force) 393 int force)
400{ 394{
401 unsigned int old_len = path->entries_length; 395 unsigned int old_len = path->entries_length;
402 int i; 396 int i;
403 397
404 /* Expand path */ 398 /* Expand path */
405 GNUNET_array_grow (path->entries, 399 GNUNET_array_grow(path->entries,
406 path->entries_length, 400 path->entries_length,
407 old_len + num_peers); 401 old_len + num_peers);
408 for (i=num_peers-1;i >= 0;i--) 402 for (i = num_peers - 1; i >= 0; i--)
409 { 403 {
410 struct CadetPeerPathEntry *entry = GNUNET_new (struct CadetPeerPathEntry); 404 struct CadetPeerPathEntry *entry = GNUNET_new(struct CadetPeerPathEntry);
411 405
412 path->entries[old_len + i] = entry; 406 path->entries[old_len + i] = entry;
413 entry->peer = peers[i]; 407 entry->peer = peers[i];
414 entry->path = path; 408 entry->path = path;
415 } 409 }
416 for (i=num_peers-1;i >= 0;i--) 410 for (i = num_peers - 1; i >= 0; i--)
417 { 411 {
418 struct CadetPeerPathEntry *entry = path->entries[old_len + i]; 412 struct CadetPeerPathEntry *entry = path->entries[old_len + i];
419 413
420 GCP_path_entry_add (entry->peer, 414 GCP_path_entry_add(entry->peer,
421 entry, 415 entry,
422 old_len + i); 416 old_len + i);
423 } 417 }
424 418
425 /* If we extend an existing path, detach it from the 419 /* If we extend an existing path, detach it from the
426 old owner and re-attach to the new one */ 420 old owner and re-attach to the new one */
427 GCP_detach_path (path->entries[old_len-1]->peer, 421 GCP_detach_path(path->entries[old_len - 1]->peer,
428 path, 422 path,
429 path->hn); 423 path->hn);
430 path->hn = NULL; 424 path->hn = NULL;
431 path->entries_length = old_len + num_peers; 425 path->entries_length = old_len + num_peers;
432 if (GNUNET_YES == force) 426 if (GNUNET_YES == force)
433 { 427 {
434 int end = path->entries_length - 1; 428 int end = path->entries_length - 1;
435 429
436 path->hn = GCP_attach_path (path->entries[end]->peer, 430 path->hn = GCP_attach_path(path->entries[end]->peer,
437 path, 431 path,
438 end, 432 end,
439 GNUNET_YES); 433 GNUNET_YES);
440 } else { 434 }
441 attach_path (path, old_len); 435 else
442 } 436 {
437 attach_path(path, old_len);
438 }
443 if (NULL == path->hn) 439 if (NULL == path->hn)
444 { 440 {
445 /* none of the peers is interested in this path; 441 /* none of the peers is interested in this path;
446 re-attach. */ 442 re-attach. */
447 GNUNET_assert (old_len == path->entries_length); 443 GNUNET_assert(old_len == path->entries_length);
448 path->hn = GCP_attach_path (path->entries[old_len - 1]->peer, 444 path->hn = GCP_attach_path(path->entries[old_len - 1]->peer,
449 path, 445 path,
450 old_len - 1, 446 old_len - 1,
451 GNUNET_YES); 447 GNUNET_YES);
452 GNUNET_assert (NULL != path->hn); 448 GNUNET_assert(NULL != path->hn);
453 return; 449 return;
454 } 450 }
455 LOG (GNUNET_ERROR_TYPE_DEBUG, 451 LOG(GNUNET_ERROR_TYPE_DEBUG,
456 "Extended path %s\n", 452 "Extended path %s\n",
457 GCPP_2s (path)); 453 GCPP_2s(path));
458} 454}
459 455
460 456
@@ -471,10 +467,10 @@ extend_path (struct CadetPeerPath *path,
471 * @return a path through the network 467 * @return a path through the network
472 */ 468 */
473void 469void
474GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path, 470GCPP_try_path_from_dht(const struct GNUNET_PeerIdentity *get_path,
475 unsigned int get_path_length, 471 unsigned int get_path_length,
476 const struct GNUNET_PeerIdentity *put_path, 472 const struct GNUNET_PeerIdentity *put_path,
477 unsigned int put_path_length) 473 unsigned int put_path_length)
478{ 474{
479 struct CadetPeer *cpath[get_path_length + put_path_length]; 475 struct CadetPeer *cpath[get_path_length + put_path_length];
480 struct CheckMatchContext cm_ctx; 476 struct CheckMatchContext cm_ctx;
@@ -484,42 +480,42 @@ GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
484 480
485 /* precompute 'cpath' so we can avoid doing the lookups lots of times */ 481 /* precompute 'cpath' so we can avoid doing the lookups lots of times */
486 skip = 0; 482 skip = 0;
487 memset (cpath, 483 memset(cpath,
488 0, 484 0,
489 sizeof (cpath)); /* Just to trigger harder errors later. */ 485 sizeof(cpath)); /* Just to trigger harder errors later. */
490 total_len = get_path_length + put_path_length; 486 total_len = get_path_length + put_path_length;
491 for (unsigned int off=0;off<total_len;off++) 487 for (unsigned int off = 0; off < total_len; off++)
492 {
493 const struct GNUNET_PeerIdentity *pid;
494
495 pid = (off < get_path_length)
496 ? &get_path[get_path_length - off - 1]
497 : &put_path[get_path_length + put_path_length - off - 1];
498 /* Check that I am not in the path */
499 if (0 == GNUNET_memcmp (&my_full_id,
500 pid))
501 { 488 {
502 skip = off + 1; 489 const struct GNUNET_PeerIdentity *pid;
503 continue; 490
491 pid = (off < get_path_length)
492 ? &get_path[get_path_length - off - 1]
493 : &put_path[get_path_length + put_path_length - off - 1];
494 /* Check that I am not in the path */
495 if (0 == GNUNET_memcmp(&my_full_id,
496 pid))
497 {
498 skip = off + 1;
499 continue;
500 }
501 cpath[off - skip] = GCP_get(pid,
502 GNUNET_YES);
503 /* Check that no peer is twice on the path */
504 for (unsigned int i = 0; i < off - skip; i++)
505 {
506 if (cpath[i] == cpath[off - skip])
507 {
508 skip = off - i;
509 break;
510 }
511 }
504 } 512 }
505 cpath[off - skip] = GCP_get (pid, 513 if (skip >= total_len)
506 GNUNET_YES);
507 /* Check that no peer is twice on the path */
508 for (unsigned int i=0;i<off - skip;i++)
509 { 514 {
510 if (cpath[i] == cpath[off - skip]) 515 LOG(GNUNET_ERROR_TYPE_DEBUG,
511 { 516 "Path discovered from DHT is one big cycle?\n");
512 skip = off - i; 517 return;
513 break;
514 }
515 } 518 }
516 }
517 if (skip >= total_len)
518 {
519 LOG (GNUNET_ERROR_TYPE_DEBUG,
520 "Path discovered from DHT is one big cycle?\n");
521 return;
522 }
523 total_len -= skip; 519 total_len -= skip;
524 520
525 /* First figure out if this path is a subset of an existing path, an 521 /* First figure out if this path is a subset of an existing path, an
@@ -527,73 +523,73 @@ GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
527 cm_ctx.cpath_length = total_len; 523 cm_ctx.cpath_length = total_len;
528 cm_ctx.cpath = cpath; 524 cm_ctx.cpath = cpath;
529 cm_ctx.match = NULL; 525 cm_ctx.match = NULL;
530 for (int i=total_len-1;i>=0;i--) 526 for (int i = total_len - 1; i >= 0; i--)
531 {
532 GCP_iterate_paths_at (cpath[i],
533 (unsigned int) i,
534 &check_match,
535 &cm_ctx);
536 if (NULL != cm_ctx.match)
537 { 527 {
538 if (i == total_len - 1) 528 GCP_iterate_paths_at(cpath[i],
539 { 529 (unsigned int)i,
540 /* Existing path includes this one, nothing to do! */ 530 &check_match,
541 LOG (GNUNET_ERROR_TYPE_DEBUG, 531 &cm_ctx);
542 "Path discovered from DHT is already known\n"); 532 if (NULL != cm_ctx.match)
543 return; 533 {
544 } 534 if (i == total_len - 1)
545 if (cm_ctx.match->entries_length == i + 1) 535 {
546 { 536 /* Existing path includes this one, nothing to do! */
547 /* Existing path ends in the middle of new path, extend it! */ 537 LOG(GNUNET_ERROR_TYPE_DEBUG,
548 LOG (GNUNET_ERROR_TYPE_DEBUG, 538 "Path discovered from DHT is already known\n");
549 "Trying to extend existing path %s by additional links discovered from DHT\n", 539 return;
550 GCPP_2s (cm_ctx.match)); 540 }
551 extend_path (cm_ctx.match, 541 if (cm_ctx.match->entries_length == i + 1)
552 &cpath[i + 1], 542 {
553 total_len - i - 1, 543 /* Existing path ends in the middle of new path, extend it! */
554 GNUNET_NO); 544 LOG(GNUNET_ERROR_TYPE_DEBUG,
555 return; 545 "Trying to extend existing path %s by additional links discovered from DHT\n",
556 } 546 GCPP_2s(cm_ctx.match));
547 extend_path(cm_ctx.match,
548 &cpath[i + 1],
549 total_len - i - 1,
550 GNUNET_NO);
551 return;
552 }
553 }
557 } 554 }
558 }
559 555
560 /* No match at all, create completely new path */ 556 /* No match at all, create completely new path */
561 path = GNUNET_new (struct CadetPeerPath); 557 path = GNUNET_new(struct CadetPeerPath);
562 path->entries_length = total_len; 558 path->entries_length = total_len;
563 path->entries = GNUNET_new_array (path->entries_length, 559 path->entries = GNUNET_new_array(path->entries_length,
564 struct CadetPeerPathEntry *); 560 struct CadetPeerPathEntry *);
565 for (int i=path->entries_length-1;i>=0;i--) 561 for (int i = path->entries_length - 1; i >= 0; i--)
566 { 562 {
567 struct CadetPeerPathEntry *entry = GNUNET_new (struct CadetPeerPathEntry); 563 struct CadetPeerPathEntry *entry = GNUNET_new(struct CadetPeerPathEntry);
568 564
569 path->entries[i] = entry; 565 path->entries[i] = entry;
570 entry->peer = cpath[i]; 566 entry->peer = cpath[i];
571 entry->path = path; 567 entry->path = path;
572 } 568 }
573 for (int i=path->entries_length-1;i>=0;i--) 569 for (int i = path->entries_length - 1; i >= 0; i--)
574 { 570 {
575 struct CadetPeerPathEntry *entry = path->entries[i]; 571 struct CadetPeerPathEntry *entry = path->entries[i];
576 572
577 GCP_path_entry_add (entry->peer, 573 GCP_path_entry_add(entry->peer,
578 entry, 574 entry,
579 i); 575 i);
580 } 576 }
581 577
582 /* Finally, try to attach it */ 578 /* Finally, try to attach it */
583 attach_path (path, 0); 579 attach_path(path, 0);
584 if (NULL == path->hn) 580 if (NULL == path->hn)
585 { 581 {
586 /* None of the peers on the path care about it. */ 582 /* None of the peers on the path care about it. */
587 LOG (GNUNET_ERROR_TYPE_DEBUG, 583 LOG(GNUNET_ERROR_TYPE_DEBUG,
588 "Path discovered from DHT is not interesting to us\n"); 584 "Path discovered from DHT is not interesting to us\n");
589 GNUNET_assert (0 == path->entries_length); 585 GNUNET_assert(0 == path->entries_length);
590 GNUNET_assert (NULL == path->entries); 586 GNUNET_assert(NULL == path->entries);
591 GNUNET_free (path); 587 GNUNET_free(path);
592 return; 588 return;
593 } 589 }
594 LOG (GNUNET_ERROR_TYPE_DEBUG, 590 LOG(GNUNET_ERROR_TYPE_DEBUG,
595 "Created new path %s based on information from DHT\n", 591 "Created new path %s based on information from DHT\n",
596 GCPP_2s (path)); 592 GCPP_2s(path));
597} 593}
598 594
599 595
@@ -605,8 +601,8 @@ GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
605 * @return corresponding path object 601 * @return corresponding path object
606 */ 602 */
607struct CadetPeerPath * 603struct CadetPeerPath *
608GCPP_get_path_from_route (unsigned int path_length, 604GCPP_get_path_from_route(unsigned int path_length,
609 const struct GNUNET_PeerIdentity *pids) 605 const struct GNUNET_PeerIdentity *pids)
610{ 606{
611 struct CheckMatchContext cm_ctx; 607 struct CheckMatchContext cm_ctx;
612 struct CadetPeer *cpath[path_length]; 608 struct CadetPeer *cpath[path_length];
@@ -614,79 +610,79 @@ GCPP_get_path_from_route (unsigned int path_length,
614 610
615 /* precompute inverted 'cpath' so we can avoid doing the lookups and 611 /* precompute inverted 'cpath' so we can avoid doing the lookups and
616 have the correct order */ 612 have the correct order */
617 for (unsigned int off=0;off<path_length;off++) 613 for (unsigned int off = 0; off < path_length; off++)
618 cpath[off] = GCP_get (&pids[path_length - 1 - off], 614 cpath[off] = GCP_get(&pids[path_length - 1 - off],
619 GNUNET_YES); 615 GNUNET_YES);
620 616
621 /* First figure out if this path is a subset of an existing path, an 617 /* First figure out if this path is a subset of an existing path, an
622 extension of an existing path, or a new path. */ 618 extension of an existing path, or a new path. */
623 cm_ctx.cpath = cpath; 619 cm_ctx.cpath = cpath;
624 cm_ctx.cpath_length = path_length; 620 cm_ctx.cpath_length = path_length;
625 cm_ctx.match = NULL; 621 cm_ctx.match = NULL;
626 for (int i=path_length-1;i>=0;i--) 622 for (int i = path_length - 1; i >= 0; i--)
627 {
628 GCP_iterate_paths_at (cpath[i],
629 (unsigned int) i,
630 &check_match,
631 &cm_ctx);
632 if (NULL != cm_ctx.match)
633 { 623 {
634 if (i == path_length - 1) 624 GCP_iterate_paths_at(cpath[i],
635 { 625 (unsigned int)i,
636 /* Existing path includes this one, return the match! */ 626 &check_match,
637 LOG (GNUNET_ERROR_TYPE_DEBUG, 627 &cm_ctx);
638 "Returning existing path %s as inverse for incoming connection\n", 628 if (NULL != cm_ctx.match)
639 GCPP_2s (cm_ctx.match)); 629 {
640 return cm_ctx.match; 630 if (i == path_length - 1)
641 } 631 {
642 if (cm_ctx.match->entries_length == i + 1) 632 /* Existing path includes this one, return the match! */
643 { 633 LOG(GNUNET_ERROR_TYPE_DEBUG,
644 /* Existing path ends in the middle of new path, extend it! */ 634 "Returning existing path %s as inverse for incoming connection\n",
645 LOG (GNUNET_ERROR_TYPE_DEBUG, 635 GCPP_2s(cm_ctx.match));
646 "Extending existing path %s to create inverse for incoming connection\n", 636 return cm_ctx.match;
647 GCPP_2s (cm_ctx.match)); 637 }
648 extend_path (cm_ctx.match, 638 if (cm_ctx.match->entries_length == i + 1)
649 &cpath[i + 1], 639 {
650 path_length - i - 1, 640 /* Existing path ends in the middle of new path, extend it! */
651 GNUNET_YES); 641 LOG(GNUNET_ERROR_TYPE_DEBUG,
652 /* Check that extension was successful */ 642 "Extending existing path %s to create inverse for incoming connection\n",
653 GNUNET_assert (cm_ctx.match->entries_length == path_length); 643 GCPP_2s(cm_ctx.match));
654 return cm_ctx.match; 644 extend_path(cm_ctx.match,
655 } 645 &cpath[i + 1],
656 /* Eh, we found a match but couldn't use it? Something is wrong. */ 646 path_length - i - 1,
657 GNUNET_break (0); 647 GNUNET_YES);
648 /* Check that extension was successful */
649 GNUNET_assert(cm_ctx.match->entries_length == path_length);
650 return cm_ctx.match;
651 }
652 /* Eh, we found a match but couldn't use it? Something is wrong. */
653 GNUNET_break(0);
654 }
658 } 655 }
659 }
660 656
661 /* No match at all, create completely new path */ 657 /* No match at all, create completely new path */
662 path = GNUNET_new (struct CadetPeerPath); 658 path = GNUNET_new(struct CadetPeerPath);
663 path->entries_length = path_length; 659 path->entries_length = path_length;
664 path->entries = GNUNET_new_array (path->entries_length, 660 path->entries = GNUNET_new_array(path->entries_length,
665 struct CadetPeerPathEntry *); 661 struct CadetPeerPathEntry *);
666 for (int i=path_length-1;i>=0;i--) 662 for (int i = path_length - 1; i >= 0; i--)
667 { 663 {
668 struct CadetPeerPathEntry *entry = GNUNET_new (struct CadetPeerPathEntry); 664 struct CadetPeerPathEntry *entry = GNUNET_new(struct CadetPeerPathEntry);
669 665
670 path->entries[i] = entry; 666 path->entries[i] = entry;
671 entry->peer = cpath[i]; 667 entry->peer = cpath[i];
672 entry->path = path; 668 entry->path = path;
673 } 669 }
674 for (int i=path_length-1;i>=0;i--) 670 for (int i = path_length - 1; i >= 0; i--)
675 { 671 {
676 struct CadetPeerPathEntry *entry = path->entries[i]; 672 struct CadetPeerPathEntry *entry = path->entries[i];
677 673
678 GCP_path_entry_add (entry->peer, 674 GCP_path_entry_add(entry->peer,
679 entry, 675 entry,
680 i); 676 i);
681 } 677 }
682 recalculate_path_desirability (path); 678 recalculate_path_desirability(path);
683 LOG (GNUNET_ERROR_TYPE_DEBUG, 679 LOG(GNUNET_ERROR_TYPE_DEBUG,
684 "Created new path %s to create inverse for incoming connection\n", 680 "Created new path %s to create inverse for incoming connection\n",
685 GCPP_2s (path)); 681 GCPP_2s(path));
686 path->hn = GCP_attach_path (cpath[path_length - 1], 682 path->hn = GCP_attach_path(cpath[path_length - 1],
687 path, 683 path,
688 path_length - 1, 684 path_length - 1,
689 GNUNET_YES); 685 GNUNET_YES);
690 return path; 686 return path;
691} 687}
692 688
@@ -699,7 +695,7 @@ GCPP_get_path_from_route (unsigned int path_length,
699 * @return number of peers on the path 695 * @return number of peers on the path
700 */ 696 */
701unsigned int 697unsigned int
702GCPP_get_length (struct CadetPeerPath *path) 698GCPP_get_length(struct CadetPeerPath *path)
703{ 699{
704 return path->entries_length; 700 return path->entries_length;
705} 701}
@@ -713,14 +709,14 @@ GCPP_get_length (struct CadetPeerPath *path)
713 * @return offset of @a cp on @a path, or UINT_MAX if not found 709 * @return offset of @a cp on @a path, or UINT_MAX if not found
714 */ 710 */
715unsigned int 711unsigned int
716GCPP_find_peer (struct CadetPeerPath *path, 712GCPP_find_peer(struct CadetPeerPath *path,
717 struct CadetPeer *cp) 713 struct CadetPeer *cp)
718{ 714{
719 for (unsigned int off = 0; 715 for (unsigned int off = 0;
720 off < path->entries_length; 716 off < path->entries_length;
721 off++) 717 off++)
722 if (cp == GCPP_get_peer_at_offset (path, 718 if (cp == GCPP_get_peer_at_offset(path,
723 off)) 719 off))
724 return off; 720 return off;
725 return UINT_MAX; 721 return UINT_MAX;
726} 722}
@@ -734,10 +730,10 @@ GCPP_find_peer (struct CadetPeerPath *path,
734 * @return the peer at offset @a off 730 * @return the peer at offset @a off
735 */ 731 */
736struct CadetPeer * 732struct CadetPeer *
737GCPP_get_peer_at_offset (struct CadetPeerPath *path, 733GCPP_get_peer_at_offset(struct CadetPeerPath *path,
738 unsigned int off) 734 unsigned int off)
739{ 735{
740 GNUNET_assert (off < path->entries_length); 736 GNUNET_assert(off < path->entries_length);
741 return path->entries[off]->peer; 737 return path->entries[off]->peer;
742} 738}
743 739
@@ -749,7 +745,7 @@ GCPP_get_peer_at_offset (struct CadetPeerPath *path,
749 * @return string, to be freed by caller (unlike other *_2s APIs!) 745 * @return string, to be freed by caller (unlike other *_2s APIs!)
750 */ 746 */
751const char * 747const char *
752GCPP_2s (struct CadetPeerPath *path) 748GCPP_2s(struct CadetPeerPath *path)
753{ 749{
754 static char buf[2048]; 750 static char buf[2048];
755 size_t off; 751 size_t off;
@@ -759,27 +755,27 @@ GCPP_2s (struct CadetPeerPath *path)
759 for (unsigned int i = 0; 755 for (unsigned int i = 0;
760 i < path->entries_length; 756 i < path->entries_length;
761 i++) 757 i++)
762 { 758 {
763 if ( (path->entries_length > max_plen) && 759 if ((path->entries_length > max_plen) &&
764 (i == max_plen / 2) ) 760 (i == max_plen / 2))
765 off += GNUNET_snprintf (&buf[off], 761 off += GNUNET_snprintf(&buf[off],
766 sizeof (buf) - off, 762 sizeof(buf) - off,
767 "...-"); 763 "...-");
768 if ( (path->entries_length > max_plen) && 764 if ((path->entries_length > max_plen) &&
769 (i > max_plen / 2) && 765 (i > max_plen / 2) &&
770 (i < path->entries_length - max_plen / 2) ) 766 (i < path->entries_length - max_plen / 2))
771 continue; 767 continue;
772 off += GNUNET_snprintf (&buf[off], 768 off += GNUNET_snprintf(&buf[off],
773 sizeof (buf) - off, 769 sizeof(buf) - off,
774 "%s%s", 770 "%s%s",
775 GNUNET_i2s (GCP_get_id (GCPP_get_peer_at_offset (path, 771 GNUNET_i2s(GCP_get_id(GCPP_get_peer_at_offset(path,
776 i))), 772 i))),
777 (i == path->entries_length -1) ? "" : "-"); 773 (i == path->entries_length - 1) ? "" : "-");
778 } 774 }
779 GNUNET_snprintf (&buf[off], 775 GNUNET_snprintf(&buf[off],
780 sizeof (buf) - off, 776 sizeof(buf) - off,
781 "(%p)", 777 "(%p)",
782 path); 778 path);
783 return buf; 779 return buf;
784} 780}
785 781
diff --git a/src/cadet/gnunet-service-cadet_paths.h b/src/cadet/gnunet-service-cadet_paths.h
index ca2c79ca4..399f91516 100644
--- a/src/cadet/gnunet-service-cadet_paths.h
+++ b/src/cadet/gnunet-service-cadet_paths.h
@@ -12,12 +12,12 @@
12 WITHOUT ANY WARRANTY; without even the implied warranty of 12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Affero General Public License for more details. 14 Affero General Public License for more details.
15 15
16 You should have received a copy of the GNU Affero General Public License 16 You should have received a copy of the GNU Affero General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18
19 SPDX-License-Identifier: AGPL3.0-or-later 19 SPDX-License-Identifier: AGPL3.0-or-later
20*/ 20 */
21 21
22/** 22/**
23 * @file cadet/gnunet-service-cadet-new_paths.h 23 * @file cadet/gnunet-service-cadet-new_paths.h
@@ -43,10 +43,10 @@
43 * @param put_path_length length of the @a put_path 43 * @param put_path_length length of the @a put_path
44 */ 44 */
45void 45void
46GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path, 46GCPP_try_path_from_dht(const struct GNUNET_PeerIdentity *get_path,
47 unsigned int get_path_length, 47 unsigned int get_path_length,
48 const struct GNUNET_PeerIdentity *put_path, 48 const struct GNUNET_PeerIdentity *put_path,
49 unsigned int put_path_length); 49 unsigned int put_path_length);
50 50
51 51
52/** 52/**
@@ -57,8 +57,8 @@ GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
57 * @return corresponding path object 57 * @return corresponding path object
58 */ 58 */
59struct CadetPeerPath * 59struct CadetPeerPath *
60GCPP_get_path_from_route (unsigned int path_length, 60GCPP_get_path_from_route(unsigned int path_length,
61 const struct GNUNET_PeerIdentity *pids); 61 const struct GNUNET_PeerIdentity *pids);
62 62
63 63
64/** 64/**
@@ -69,7 +69,7 @@ GCPP_get_path_from_route (unsigned int path_length,
69 * @return number of peers on the path 69 * @return number of peers on the path
70 */ 70 */
71unsigned int 71unsigned int
72GCPP_get_length (struct CadetPeerPath *path); 72GCPP_get_length(struct CadetPeerPath *path);
73 73
74 74
75/** 75/**
@@ -83,9 +83,9 @@ GCPP_get_length (struct CadetPeerPath *path);
83 * otherwise connection from us to @a destination via @a path 83 * otherwise connection from us to @a destination via @a path
84 */ 84 */
85struct CadetConnection * 85struct CadetConnection *
86GCPP_get_connection (struct CadetPeerPath *path, 86GCPP_get_connection(struct CadetPeerPath *path,
87 struct CadetPeer *destination, 87 struct CadetPeer *destination,
88 unsigned int off); 88 unsigned int off);
89 89
90 90
91/** 91/**
@@ -97,9 +97,9 @@ GCPP_get_connection (struct CadetPeerPath *path,
97 * @param cc the connection to remember 97 * @param cc the connection to remember
98 */ 98 */
99void 99void
100GCPP_add_connection (struct CadetPeerPath *path, 100GCPP_add_connection(struct CadetPeerPath *path,
101 unsigned int off, 101 unsigned int off,
102 struct CadetConnection *cc); 102 struct CadetConnection *cc);
103 103
104 104
105/** 105/**
@@ -111,9 +111,9 @@ GCPP_add_connection (struct CadetPeerPath *path,
111 * @param cc the connection to forget 111 * @param cc the connection to forget
112 */ 112 */
113void 113void
114GCPP_del_connection (struct CadetPeerPath *path, 114GCPP_del_connection(struct CadetPeerPath *path,
115 unsigned int off, 115 unsigned int off,
116 struct CadetConnection *cc); 116 struct CadetConnection *cc);
117 117
118 118
119/** 119/**
@@ -124,8 +124,8 @@ GCPP_del_connection (struct CadetPeerPath *path,
124 * @return offset of @a cp on @a path, or UINT_MAX if not found 124 * @return offset of @a cp on @a path, or UINT_MAX if not found
125 */ 125 */
126unsigned int 126unsigned int
127GCPP_find_peer (struct CadetPeerPath *path, 127GCPP_find_peer(struct CadetPeerPath *path,
128 struct CadetPeer *cp); 128 struct CadetPeer *cp);
129 129
130 130
131/** 131/**
@@ -142,7 +142,7 @@ GCPP_find_peer (struct CadetPeerPath *path,
142 * @return desirability of the path, larger is more desirable 142 * @return desirability of the path, larger is more desirable
143 */ 143 */
144GNUNET_CONTAINER_HeapCostType 144GNUNET_CONTAINER_HeapCostType
145GCPP_get_desirability (const struct CadetPeerPath *path); 145GCPP_get_desirability(const struct CadetPeerPath *path);
146 146
147 147
148/** 148/**
@@ -154,7 +154,7 @@ GCPP_get_desirability (const struct CadetPeerPath *path);
154 * @param path the path that is being released 154 * @param path the path that is being released
155 */ 155 */
156void 156void
157GCPP_release (struct CadetPeerPath *path); 157GCPP_release(struct CadetPeerPath *path);
158 158
159 159
160/** 160/**
@@ -165,8 +165,8 @@ GCPP_release (struct CadetPeerPath *path);
165 * @return peer at offset @a off 165 * @return peer at offset @a off
166 */ 166 */
167struct CadetPeer * 167struct CadetPeer *
168GCPP_get_peer_at_offset (struct CadetPeerPath *path, 168GCPP_get_peer_at_offset(struct CadetPeerPath *path,
169 unsigned int off); 169 unsigned int off);
170 170
171 171
172/** 172/**
@@ -176,7 +176,7 @@ GCPP_get_peer_at_offset (struct CadetPeerPath *path,
176 * @return string, statically allocated 176 * @return string, statically allocated
177 */ 177 */
178const char * 178const char *
179GCPP_2s (struct CadetPeerPath *p); 179GCPP_2s(struct CadetPeerPath *p);
180 180
181 181
182#endif 182#endif
diff --git a/src/cadet/gnunet-service-cadet_peer.c b/src/cadet/gnunet-service-cadet_peer.c
index d6cc6f914..23eb6b225 100644
--- a/src/cadet/gnunet-service-cadet_peer.c
+++ b/src/cadet/gnunet-service-cadet_peer.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file cadet/gnunet-service-cadet_peer.c 22 * @file cadet/gnunet-service-cadet_peer.c
@@ -46,7 +46,7 @@
46#include "gnunet-service-cadet_tunnels.h" 46#include "gnunet-service-cadet_tunnels.h"
47 47
48 48
49#define LOG(level, ...) GNUNET_log_from(level,"cadet-per",__VA_ARGS__) 49#define LOG(level, ...) GNUNET_log_from(level, "cadet-per", __VA_ARGS__)
50 50
51 51
52/** 52/**
@@ -69,9 +69,7 @@
69 * Data structure used to track whom we have to notify about changes 69 * Data structure used to track whom we have to notify about changes
70 * to our message queue. 70 * to our message queue.
71 */ 71 */
72struct GCP_MessageQueueManager 72struct GCP_MessageQueueManager {
73{
74
75 /** 73 /**
76 * Kept in a DLL. 74 * Kept in a DLL.
77 */ 75 */
@@ -101,15 +99,13 @@ struct GCP_MessageQueueManager
101 * Envelope this manager would like to transmit once it is its turn. 99 * Envelope this manager would like to transmit once it is its turn.
102 */ 100 */
103 struct GNUNET_MQ_Envelope *env; 101 struct GNUNET_MQ_Envelope *env;
104
105}; 102};
106 103
107 104
108/** 105/**
109 * Struct containing all information regarding a given peer 106 * Struct containing all information regarding a given peer
110 */ 107 */
111struct CadetPeer 108struct CadetPeer {
112{
113 /** 109 /**
114 * ID of the peer 110 * ID of the peer
115 */ 111 */
@@ -229,7 +225,6 @@ struct CadetPeer
229 * The arrays should be grown as needed. 225 * The arrays should be grown as needed.
230 */ 226 */
231 unsigned int path_dll_length; 227 unsigned int path_dll_length;
232
233}; 228};
234 229
235 230
@@ -240,23 +235,23 @@ struct CadetPeer
240 * @return Static string for it's ID. 235 * @return Static string for it's ID.
241 */ 236 */
242const char * 237const char *
243GCP_2s (const struct CadetPeer *cp) 238GCP_2s(const struct CadetPeer *cp)
244{ 239{
245 static char buf[5]; 240 static char buf[5];
246 char *ret; 241 char *ret;
247 242
248 if ((NULL == cp) || 243 if ((NULL == cp) ||
249 (0 == GNUNET_is_zero (&cp->pid.public_key))) 244 (0 == GNUNET_is_zero(&cp->pid.public_key)))
250 return "NULL"; 245 return "NULL";
251 246
252 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&cp->pid.public_key); 247 ret = GNUNET_CRYPTO_eddsa_public_key_to_string(&cp->pid.public_key);
253 if (NULL == ret) 248 if (NULL == ret)
254 return "NULL"; 249 return "NULL";
255 250
256 GNUNET_strlcpy (buf, 251 GNUNET_strlcpy(buf,
257 ret, 252 ret,
258 sizeof (buf)); 253 sizeof(buf));
259 GNUNET_free (ret); 254 GNUNET_free(ret);
260 return buf; 255 return buf;
261} 256}
262 257
@@ -277,8 +272,8 @@ GCP_2s (const struct CadetPeer *cp)
277 * positive scores mean path is more desirable 272 * positive scores mean path is more desirable
278 */ 273 */
279double 274double
280GCP_get_desirability_of_path (struct CadetPeer *cp, 275GCP_get_desirability_of_path(struct CadetPeer *cp,
281 unsigned int off) 276 unsigned int off)
282{ 277{
283 unsigned int num_alts = cp->num_paths; 278 unsigned int num_alts = cp->num_paths;
284 unsigned int off_sum; 279 unsigned int off_sum;
@@ -286,20 +281,20 @@ GCP_get_desirability_of_path (struct CadetPeer *cp,
286 double path_delta; 281 double path_delta;
287 double weight_alts; 282 double weight_alts;
288 283
289 GNUNET_assert (num_alts >= 1); /* 'path' should be in there! */ 284 GNUNET_assert(num_alts >= 1); /* 'path' should be in there! */
290 GNUNET_assert (0 != cp->path_dll_length); 285 GNUNET_assert(0 != cp->path_dll_length);
291 286
292 /* We maintain 'off_sum' in 'peer' and thereby 287 /* We maintain 'off_sum' in 'peer' and thereby
293 avoid the SLOW recalculation each time. Kept here 288 avoid the SLOW recalculation each time. Kept here
294 just to document what is going on. */ 289 just to document what is going on. */
295#if SLOW 290#if SLOW
296 off_sum = 0; 291 off_sum = 0;
297 for (unsigned int j=0;j<cp->path_dll_length;j++) 292 for (unsigned int j = 0; j < cp->path_dll_length; j++)
298 for (struct CadetPeerPathEntry *pe = cp->path_heads[j]; 293 for (struct CadetPeerPathEntry *pe = cp->path_heads[j];
299 NULL != pe; 294 NULL != pe;
300 pe = pe->next) 295 pe = pe->next)
301 off_sum += j; 296 off_sum += j;
302 GNUNET_assert (off_sum == cp->off_sum); 297 GNUNET_assert(off_sum == cp->off_sum);
303#else 298#else
304 off_sum = cp->off_sum; 299 off_sum = cp->off_sum;
305#endif 300#endif
@@ -307,8 +302,8 @@ GCP_get_desirability_of_path (struct CadetPeer *cp,
307 path_delta = off - avg_sum; 302 path_delta = off - avg_sum;
308 /* path_delta positiv: path off of peer above average (bad path for peer), 303 /* path_delta positiv: path off of peer above average (bad path for peer),
309 path_delta negativ: path off of peer below average (good path for peer) */ 304 path_delta negativ: path off of peer below average (good path for peer) */
310 if (path_delta <= - 1.0) 305 if (path_delta <= -1.0)
311 weight_alts = - num_alts / path_delta; /* discount alternative paths */ 306 weight_alts = -num_alts / path_delta; /* discount alternative paths */
312 else if (path_delta >= 1.0) 307 else if (path_delta >= 1.0)
313 weight_alts = num_alts * path_delta; /* overcount alternative paths */ 308 weight_alts = num_alts * path_delta; /* overcount alternative paths */
314 else 309 else
@@ -328,63 +323,63 @@ GCP_get_desirability_of_path (struct CadetPeer *cp,
328 * @param cls peer to clean up 323 * @param cls peer to clean up
329 */ 324 */
330static void 325static void
331destroy_peer (void *cls) 326destroy_peer(void *cls)
332{ 327{
333 struct CadetPeer *cp = cls; 328 struct CadetPeer *cp = cls;
334 329
335 LOG (GNUNET_ERROR_TYPE_DEBUG, 330 LOG(GNUNET_ERROR_TYPE_DEBUG,
336 "Destroying state about peer %s\n", 331 "Destroying state about peer %s\n",
337 GCP_2s (cp)); 332 GCP_2s(cp));
338 cp->destroy_task = NULL; 333 cp->destroy_task = NULL;
339 GNUNET_assert (NULL == cp->t); 334 GNUNET_assert(NULL == cp->t);
340 GNUNET_assert (NULL == cp->core_mq); 335 GNUNET_assert(NULL == cp->core_mq);
341 GNUNET_assert (0 == cp->num_paths); 336 GNUNET_assert(0 == cp->num_paths);
342 for (unsigned int i=0;i<cp->path_dll_length;i++) 337 for (unsigned int i = 0; i < cp->path_dll_length; i++)
343 GNUNET_assert (NULL == cp->path_heads[i]); 338 GNUNET_assert(NULL == cp->path_heads[i]);
344 GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (cp->connections)); 339 GNUNET_assert(0 == GNUNET_CONTAINER_multishortmap_size(cp->connections));
345 GNUNET_assert (GNUNET_YES == 340 GNUNET_assert(GNUNET_YES ==
346 GNUNET_CONTAINER_multipeermap_remove (peers, 341 GNUNET_CONTAINER_multipeermap_remove(peers,
347 &cp->pid, 342 &cp->pid,
348 cp)); 343 cp));
349 GNUNET_free_non_null (cp->path_heads); 344 GNUNET_free_non_null(cp->path_heads);
350 GNUNET_free_non_null (cp->path_tails); 345 GNUNET_free_non_null(cp->path_tails);
351 cp->path_dll_length = 0; 346 cp->path_dll_length = 0;
352 if (NULL != cp->search_h) 347 if (NULL != cp->search_h)
353 { 348 {
354 GCD_search_stop (cp->search_h); 349 GCD_search_stop(cp->search_h);
355 cp->search_h = NULL; 350 cp->search_h = NULL;
356 } 351 }
357 /* FIXME: clean up search_delayedXXX! */ 352 /* FIXME: clean up search_delayedXXX! */
358 353
359 if (NULL != cp->hello_offer) 354 if (NULL != cp->hello_offer)
360 { 355 {
361 GNUNET_TRANSPORT_offer_hello_cancel (cp->hello_offer); 356 GNUNET_TRANSPORT_offer_hello_cancel(cp->hello_offer);
362 cp->hello_offer = NULL; 357 cp->hello_offer = NULL;
363 } 358 }
364 if (NULL != cp->connectivity_suggestion) 359 if (NULL != cp->connectivity_suggestion)
365 { 360 {
366 GNUNET_ATS_connectivity_suggest_cancel (cp->connectivity_suggestion); 361 GNUNET_ATS_connectivity_suggest_cancel(cp->connectivity_suggestion);
367 cp->connectivity_suggestion = NULL; 362 cp->connectivity_suggestion = NULL;
368 } 363 }
369 GNUNET_CONTAINER_multishortmap_destroy (cp->connections); 364 GNUNET_CONTAINER_multishortmap_destroy(cp->connections);
370 if (NULL != cp->path_heap) 365 if (NULL != cp->path_heap)
371 { 366 {
372 GNUNET_CONTAINER_heap_destroy (cp->path_heap); 367 GNUNET_CONTAINER_heap_destroy(cp->path_heap);
373 cp->path_heap = NULL; 368 cp->path_heap = NULL;
374 } 369 }
375 if (NULL != cp->heap_cleanup_task) 370 if (NULL != cp->heap_cleanup_task)
376 { 371 {
377 GNUNET_SCHEDULER_cancel (cp->heap_cleanup_task); 372 GNUNET_SCHEDULER_cancel(cp->heap_cleanup_task);
378 cp->heap_cleanup_task = NULL; 373 cp->heap_cleanup_task = NULL;
379 } 374 }
380 GNUNET_free_non_null (cp->hello); 375 GNUNET_free_non_null(cp->hello);
381 /* Peer should not be freed if paths exist; if there are no paths, 376 /* Peer should not be freed if paths exist; if there are no paths,
382 there ought to be no connections, and without connections, no 377 there ought to be no connections, and without connections, no
383 notifications. Thus we can assert that mqm_head is empty at this 378 notifications. Thus we can assert that mqm_head is empty at this
384 point. */ 379 point. */
385 GNUNET_assert (NULL == cp->mqm_head); 380 GNUNET_assert(NULL == cp->mqm_head);
386 GNUNET_assert (NULL == cp->mqm_ready_ptr); 381 GNUNET_assert(NULL == cp->mqm_ready_ptr);
387 GNUNET_free (cp); 382 GNUNET_free(cp);
388} 383}
389 384
390 385
@@ -394,64 +389,64 @@ destroy_peer (void *cls)
394 * @param cp the more-active peer 389 * @param cp the more-active peer
395 */ 390 */
396static void 391static void
397consider_peer_activate (struct CadetPeer *cp) 392consider_peer_activate(struct CadetPeer *cp)
398{ 393{
399 uint32_t strength; 394 uint32_t strength;
400 395
401 LOG (GNUNET_ERROR_TYPE_DEBUG, 396 LOG(GNUNET_ERROR_TYPE_DEBUG,
402 "Updating peer %s activation state (%u connections)%s%s\n", 397 "Updating peer %s activation state (%u connections)%s%s\n",
403 GCP_2s (cp), 398 GCP_2s(cp),
404 GNUNET_CONTAINER_multishortmap_size (cp->connections), 399 GNUNET_CONTAINER_multishortmap_size(cp->connections),
405 (NULL == cp->t) ? "" : " with tunnel", 400 (NULL == cp->t) ? "" : " with tunnel",
406 (NULL == cp->core_mq) ? "" : " with CORE link"); 401 (NULL == cp->core_mq) ? "" : " with CORE link");
407 if (NULL != cp->destroy_task) 402 if (NULL != cp->destroy_task)
408 {
409 /* It's active, do not destory! */
410 GNUNET_SCHEDULER_cancel (cp->destroy_task);
411 cp->destroy_task = NULL;
412 }
413 if ( (0 == GNUNET_CONTAINER_multishortmap_size (cp->connections)) &&
414 (NULL == cp->t) )
415 {
416 /* We're just on a path or directly connected; don't bother too much */
417 if (NULL != cp->connectivity_suggestion)
418 { 403 {
419 GNUNET_ATS_connectivity_suggest_cancel (cp->connectivity_suggestion); 404 /* It's active, do not destory! */
420 cp->connectivity_suggestion = NULL; 405 GNUNET_SCHEDULER_cancel(cp->destroy_task);
406 cp->destroy_task = NULL;
421 } 407 }
422 if (NULL != cp->search_h) 408 if ((0 == GNUNET_CONTAINER_multishortmap_size(cp->connections)) &&
409 (NULL == cp->t))
423 { 410 {
424 GCD_search_stop (cp->search_h); 411 /* We're just on a path or directly connected; don't bother too much */
425 cp->search_h = NULL; 412 if (NULL != cp->connectivity_suggestion)
413 {
414 GNUNET_ATS_connectivity_suggest_cancel(cp->connectivity_suggestion);
415 cp->connectivity_suggestion = NULL;
416 }
417 if (NULL != cp->search_h)
418 {
419 GCD_search_stop(cp->search_h);
420 cp->search_h = NULL;
421 }
422 return;
426 } 423 }
427 return;
428 }
429 if (NULL == cp->core_mq) 424 if (NULL == cp->core_mq)
430 { 425 {
431 /* Lacks direct connection, try to create one by querying the DHT */ 426 /* Lacks direct connection, try to create one by querying the DHT */
432 if ( (NULL == cp->search_h) && 427 if ((NULL == cp->search_h) &&
433 (DESIRED_CONNECTIONS_PER_TUNNEL > cp->num_paths) ) 428 (DESIRED_CONNECTIONS_PER_TUNNEL > cp->num_paths))
434 cp->search_h 429 cp->search_h
435 = GCD_search (&cp->pid); 430 = GCD_search(&cp->pid);
436 } 431 }
437 else 432 else
438 {
439 /* Have direct connection, stop DHT search if active */
440 if (NULL != cp->search_h)
441 { 433 {
442 GCD_search_stop (cp->search_h); 434 /* Have direct connection, stop DHT search if active */
443 cp->search_h = NULL; 435 if (NULL != cp->search_h)
436 {
437 GCD_search_stop(cp->search_h);
438 cp->search_h = NULL;
439 }
444 } 440 }
445 }
446 441
447 /* If we have a tunnel, our urge for connections is much bigger */ 442 /* If we have a tunnel, our urge for connections is much bigger */
448 strength = (NULL != cp->t) ? 32 : 1; 443 strength = (NULL != cp->t) ? 32 : 1;
449 if (NULL != cp->connectivity_suggestion) 444 if (NULL != cp->connectivity_suggestion)
450 GNUNET_ATS_connectivity_suggest_cancel (cp->connectivity_suggestion); 445 GNUNET_ATS_connectivity_suggest_cancel(cp->connectivity_suggestion);
451 cp->connectivity_suggestion 446 cp->connectivity_suggestion
452 = GNUNET_ATS_connectivity_suggest (ats_ch, 447 = GNUNET_ATS_connectivity_suggest(ats_ch,
453 &cp->pid, 448 &cp->pid,
454 strength); 449 strength);
455} 450}
456 451
457 452
@@ -461,7 +456,7 @@ consider_peer_activate (struct CadetPeer *cp)
461 * @param cp peer to clean up 456 * @param cp peer to clean up
462 */ 457 */
463static void 458static void
464consider_peer_destroy (struct CadetPeer *cp); 459consider_peer_destroy(struct CadetPeer *cp);
465 460
466 461
467/** 462/**
@@ -471,15 +466,15 @@ consider_peer_destroy (struct CadetPeer *cp);
471 * @param cls a `struct CadetPeer`. 466 * @param cls a `struct CadetPeer`.
472 */ 467 */
473static void 468static void
474drop_paths (void *cls) 469drop_paths(void *cls)
475{ 470{
476 struct CadetPeer *cp = cls; 471 struct CadetPeer *cp = cls;
477 struct CadetPeerPath *path; 472 struct CadetPeerPath *path;
478 473
479 cp->destroy_task = NULL; 474 cp->destroy_task = NULL;
480 while (NULL != (path = GNUNET_CONTAINER_heap_remove_root (cp->path_heap))) 475 while (NULL != (path = GNUNET_CONTAINER_heap_remove_root(cp->path_heap)))
481 GCPP_release (path); 476 GCPP_release(path);
482 consider_peer_destroy (cp); 477 consider_peer_destroy(cp);
483} 478}
484 479
485 480
@@ -489,43 +484,43 @@ drop_paths (void *cls)
489 * @param cp peer to clean up 484 * @param cp peer to clean up
490 */ 485 */
491static void 486static void
492consider_peer_destroy (struct CadetPeer *cp) 487consider_peer_destroy(struct CadetPeer *cp)
493{ 488{
494 struct GNUNET_TIME_Relative exp; 489 struct GNUNET_TIME_Relative exp;
495 490
496 if (NULL != cp->destroy_task) 491 if (NULL != cp->destroy_task)
497 { 492 {
498 GNUNET_SCHEDULER_cancel (cp->destroy_task); 493 GNUNET_SCHEDULER_cancel(cp->destroy_task);
499 cp->destroy_task = NULL; 494 cp->destroy_task = NULL;
500 } 495 }
501 if (NULL != cp->t) 496 if (NULL != cp->t)
502 return; /* still relevant! */ 497 return; /* still relevant! */
503 if (NULL != cp->core_mq) 498 if (NULL != cp->core_mq)
504 return; /* still relevant! */ 499 return; /* still relevant! */
505 if (0 != GNUNET_CONTAINER_multishortmap_size (cp->connections)) 500 if (0 != GNUNET_CONTAINER_multishortmap_size(cp->connections))
506 return; /* still relevant! */ 501 return; /* still relevant! */
507 if ( (NULL != cp->path_heap) && 502 if ((NULL != cp->path_heap) &&
508 (0 < GNUNET_CONTAINER_heap_get_size (cp->path_heap)) ) 503 (0 < GNUNET_CONTAINER_heap_get_size(cp->path_heap)))
509 { 504 {
510 cp->destroy_task = GNUNET_SCHEDULER_add_delayed (IDLE_PATH_TIMEOUT, 505 cp->destroy_task = GNUNET_SCHEDULER_add_delayed(IDLE_PATH_TIMEOUT,
511 &drop_paths, 506 &drop_paths,
512 cp); 507 cp);
513 return; 508 return;
514 } 509 }
515 if (0 != cp->num_paths) 510 if (0 != cp->num_paths)
516 return; /* still relevant! */ 511 return; /* still relevant! */
517 if (NULL != cp->hello) 512 if (NULL != cp->hello)
518 { 513 {
519 /* relevant only until HELLO expires */ 514 /* relevant only until HELLO expires */
520 exp = GNUNET_TIME_absolute_get_remaining (GNUNET_HELLO_get_last_expiration (cp->hello)); 515 exp = GNUNET_TIME_absolute_get_remaining(GNUNET_HELLO_get_last_expiration(cp->hello));
521 cp->destroy_task = GNUNET_SCHEDULER_add_delayed (exp, 516 cp->destroy_task = GNUNET_SCHEDULER_add_delayed(exp,
522 &destroy_peer, 517 &destroy_peer,
523 cp); 518 cp);
524 return; 519 return;
525 } 520 }
526 cp->destroy_task = GNUNET_SCHEDULER_add_delayed (IDLE_PEER_TIMEOUT, 521 cp->destroy_task = GNUNET_SCHEDULER_add_delayed(IDLE_PEER_TIMEOUT,
527 &destroy_peer, 522 &destroy_peer,
528 cp); 523 cp);
529} 524}
530 525
531 526
@@ -536,60 +531,60 @@ consider_peer_destroy (struct CadetPeer *cp)
536 * @param mq message queue to set (can be NULL) 531 * @param mq message queue to set (can be NULL)
537 */ 532 */
538void 533void
539GCP_set_mq (struct CadetPeer *cp, 534GCP_set_mq(struct CadetPeer *cp,
540 struct GNUNET_MQ_Handle *mq) 535 struct GNUNET_MQ_Handle *mq)
541{ 536{
542 LOG (GNUNET_ERROR_TYPE_DEBUG, 537 LOG(GNUNET_ERROR_TYPE_DEBUG,
543 "Message queue for peer %s is now %p\n", 538 "Message queue for peer %s is now %p\n",
544 GCP_2s (cp), 539 GCP_2s(cp),
545 mq); 540 mq);
546 cp->core_mq = mq; 541 cp->core_mq = mq;
547 for (struct GCP_MessageQueueManager *mqm = cp->mqm_head, *next; 542 for (struct GCP_MessageQueueManager *mqm = cp->mqm_head, *next;
548 NULL != mqm; 543 NULL != mqm;
549 mqm = next) 544 mqm = next)
550 {
551 /* Save next pointer in case mqm gets freed by the callback */
552 next = mqm->next;
553 if (NULL == mq)
554 { 545 {
555 if (NULL != mqm->env) 546 /* Save next pointer in case mqm gets freed by the callback */
556 { 547 next = mqm->next;
557 GNUNET_MQ_discard (mqm->env); 548 if (NULL == mq)
558 mqm->env = NULL; 549 {
559 mqm->cb (mqm->cb_cls, 550 if (NULL != mqm->env)
560 GNUNET_SYSERR); 551 {
561 } 552 GNUNET_MQ_discard(mqm->env);
553 mqm->env = NULL;
554 mqm->cb(mqm->cb_cls,
555 GNUNET_SYSERR);
556 }
557 else
558 {
559 mqm->cb(mqm->cb_cls,
560 GNUNET_NO);
561 }
562 }
562 else 563 else
563 { 564 {
564 mqm->cb (mqm->cb_cls, 565 GNUNET_assert(NULL == mqm->env);
565 GNUNET_NO); 566 mqm->cb(mqm->cb_cls,
566 } 567 GNUNET_YES);
568 }
567 } 569 }
568 else 570 if ((NULL != mq) ||
571 (NULL != cp->t))
572 consider_peer_activate(cp);
573 else
574 consider_peer_destroy(cp);
575
576 if ((NULL != mq) &&
577 (NULL != cp->t))
569 { 578 {
570 GNUNET_assert (NULL == mqm->env); 579 /* have a new, direct path to the target, notify tunnel */
571 mqm->cb (mqm->cb_cls, 580 struct CadetPeerPath *path;
572 GNUNET_YES); 581
582 path = GCPP_get_path_from_route(1,
583 &cp->pid);
584 GCT_consider_path(cp->t,
585 path,
586 0);
573 } 587 }
574 }
575 if ( (NULL != mq) ||
576 (NULL != cp->t) )
577 consider_peer_activate (cp);
578 else
579 consider_peer_destroy (cp);
580
581 if ( (NULL != mq) &&
582 (NULL != cp->t) )
583 {
584 /* have a new, direct path to the target, notify tunnel */
585 struct CadetPeerPath *path;
586
587 path = GCPP_get_path_from_route (1,
588 &cp->pid);
589 GCT_consider_path (cp->t,
590 path,
591 0);
592 }
593} 588}
594 589
595 590
@@ -600,12 +595,12 @@ GCP_set_mq (struct CadetPeer *cp,
600 * @return #GNUNET_YES or #GNUNET_NO with the decision to drop. 595 * @return #GNUNET_YES or #GNUNET_NO with the decision to drop.
601 */ 596 */
602static int 597static int
603should_I_drop (void) 598should_I_drop(void)
604{ 599{
605 if (0 == drop_percent) 600 if (0 == drop_percent)
606 return GNUNET_NO; 601 return GNUNET_NO;
607 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 602 if (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK,
608 101) < drop_percent) 603 101) < drop_percent)
609 return GNUNET_YES; 604 return GNUNET_YES;
610 return GNUNET_NO; 605 return GNUNET_NO;
611} 606}
@@ -618,7 +613,7 @@ should_I_drop (void)
618 * @param cls the `struct CadetPeeer` where we made progress 613 * @param cls the `struct CadetPeeer` where we made progress
619 */ 614 */
620static void 615static void
621mqm_send_done (void *cls); 616mqm_send_done(void *cls);
622 617
623 618
624/** 619/**
@@ -627,68 +622,69 @@ mqm_send_done (void *cls);
627 * @param mqm mqm to transmit message for now 622 * @param mqm mqm to transmit message for now
628 */ 623 */
629static void 624static void
630mqm_execute (struct GCP_MessageQueueManager *mqm) 625mqm_execute(struct GCP_MessageQueueManager *mqm)
631{ 626{
632 struct CadetPeer *cp = mqm->cp; 627 struct CadetPeer *cp = mqm->cp;
633 628
634 /* Move ready pointer to the next entry that might be ready. */ 629 /* Move ready pointer to the next entry that might be ready. */
635 if ( (mqm == cp->mqm_ready_ptr) && 630 if ((mqm == cp->mqm_ready_ptr) &&
636 (NULL != mqm->next) ) 631 (NULL != mqm->next))
637 cp->mqm_ready_ptr = mqm->next; 632 cp->mqm_ready_ptr = mqm->next;
638 /* Move entry to the end of the DLL, to be fair. */ 633 /* Move entry to the end of the DLL, to be fair. */
639 if (mqm != cp->mqm_tail) 634 if (mqm != cp->mqm_tail)
640 { 635 {
641 GNUNET_CONTAINER_DLL_remove (cp->mqm_head, 636 GNUNET_CONTAINER_DLL_remove(cp->mqm_head,
642 cp->mqm_tail, 637 cp->mqm_tail,
643 mqm); 638 mqm);
644 GNUNET_CONTAINER_DLL_insert_tail (cp->mqm_head, 639 GNUNET_CONTAINER_DLL_insert_tail(cp->mqm_head,
645 cp->mqm_tail, 640 cp->mqm_tail,
646 mqm); 641 mqm);
647 } 642 }
648 cp->mqm_ready_counter--; 643 cp->mqm_ready_counter--;
649 if (GNUNET_YES == should_I_drop ()) 644 if (GNUNET_YES == should_I_drop())
650 { 645 {
651 LOG (GNUNET_ERROR_TYPE_DEBUG, 646 LOG(GNUNET_ERROR_TYPE_DEBUG,
652 "DROPPING message to peer %s from MQM %p\n", 647 "DROPPING message to peer %s from MQM %p\n",
653 GCP_2s (cp), 648 GCP_2s(cp),
654 mqm); 649 mqm);
655 GNUNET_MQ_discard (mqm->env); 650 GNUNET_MQ_discard(mqm->env);
656 mqm->env = NULL; 651 mqm->env = NULL;
657 mqm_send_done (cp); 652 mqm_send_done(cp);
658 } 653 }
659 else 654 else
660 {
661 { 655 {
662 const struct GNUNET_MessageHeader *mh;
663
664 mh = GNUNET_MQ_env_get_msg (mqm->env);
665 switch (ntohs (mh->type))
666 { 656 {
667 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX: 657 const struct GNUNET_MessageHeader *mh;
668 { 658
669 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg 659 mh = GNUNET_MQ_env_get_msg(mqm->env);
670 = (const struct GNUNET_CADET_TunnelKeyExchangeMessage *) mh; 660 switch (ntohs(mh->type))
671 LOG (GNUNET_ERROR_TYPE_DEBUG, 661 {
672 "P2P forwarding KX with ephemeral %s to %s on CID %s\n", 662 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX:
673 GNUNET_e2s (&msg->ephemeral_key), 663 {
674 GCP_2s (cp), 664 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg
675 GNUNET_sh2s (&msg->cid.connection_of_tunnel)); 665 = (const struct GNUNET_CADET_TunnelKeyExchangeMessage *)mh;
676 } 666 LOG(GNUNET_ERROR_TYPE_DEBUG,
677 break; 667 "P2P forwarding KX with ephemeral %s to %s on CID %s\n",
678 default: 668 GNUNET_e2s(&msg->ephemeral_key),
679 break; 669 GCP_2s(cp),
670 GNUNET_sh2s(&msg->cid.connection_of_tunnel));
671 }
672 break;
673
674 default:
675 break;
676 }
680 } 677 }
678 LOG(GNUNET_ERROR_TYPE_DEBUG,
679 "Sending to peer %s from MQM %p\n",
680 GCP_2s(cp),
681 mqm);
682 GNUNET_MQ_send(cp->core_mq,
683 mqm->env);
684 mqm->env = NULL;
681 } 685 }
682 LOG (GNUNET_ERROR_TYPE_DEBUG, 686 mqm->cb(mqm->cb_cls,
683 "Sending to peer %s from MQM %p\n", 687 GNUNET_YES);
684 GCP_2s (cp),
685 mqm);
686 GNUNET_MQ_send (cp->core_mq,
687 mqm->env);
688 mqm->env = NULL;
689 }
690 mqm->cb (mqm->cb_cls,
691 GNUNET_YES);
692} 688}
693 689
694 690
@@ -700,18 +696,18 @@ mqm_execute (struct GCP_MessageQueueManager *mqm)
700 * @param cp peer to try to send the next ready message to 696 * @param cp peer to try to send the next ready message to
701 */ 697 */
702static void 698static void
703send_next_ready (struct CadetPeer *cp) 699send_next_ready(struct CadetPeer *cp)
704{ 700{
705 struct GCP_MessageQueueManager *mqm; 701 struct GCP_MessageQueueManager *mqm;
706 702
707 if (0 == cp->mqm_ready_counter) 703 if (0 == cp->mqm_ready_counter)
708 return; 704 return;
709 while ( (NULL != (mqm = cp->mqm_ready_ptr)) && 705 while ((NULL != (mqm = cp->mqm_ready_ptr)) &&
710 (NULL == mqm->env) ) 706 (NULL == mqm->env))
711 cp->mqm_ready_ptr = mqm->next; 707 cp->mqm_ready_ptr = mqm->next;
712 if (NULL == mqm) 708 if (NULL == mqm)
713 return; /* nothing to do */ 709 return; /* nothing to do */
714 mqm_execute (mqm); 710 mqm_execute(mqm);
715} 711}
716 712
717 713
@@ -722,14 +718,14 @@ send_next_ready (struct CadetPeer *cp)
722 * @param cls the `struct CadetPeeer` where we made progress 718 * @param cls the `struct CadetPeeer` where we made progress
723 */ 719 */
724static void 720static void
725mqm_send_done (void *cls) 721mqm_send_done(void *cls)
726{ 722{
727 struct CadetPeer *cp = cls; 723 struct CadetPeer *cp = cls;
728 724
729 LOG (GNUNET_ERROR_TYPE_DEBUG, 725 LOG(GNUNET_ERROR_TYPE_DEBUG,
730 "Sending to peer %s completed\n", 726 "Sending to peer %s completed\n",
731 GCP_2s (cp)); 727 GCP_2s(cp));
732 send_next_ready (cp); 728 send_next_ready(cp);
733} 729}
734 730
735 731
@@ -741,30 +737,30 @@ mqm_send_done (void *cls)
741 * yet have a #GNUNET_MQ_notify_sent() callback attached to it 737 * yet have a #GNUNET_MQ_notify_sent() callback attached to it
742 */ 738 */
743void 739void
744GCP_send (struct GCP_MessageQueueManager *mqm, 740GCP_send(struct GCP_MessageQueueManager *mqm,
745 struct GNUNET_MQ_Envelope *env) 741 struct GNUNET_MQ_Envelope *env)
746{ 742{
747 struct CadetPeer *cp = mqm->cp; 743 struct CadetPeer *cp = mqm->cp;
748 744
749 GNUNET_assert (NULL != env); 745 GNUNET_assert(NULL != env);
750 LOG (GNUNET_ERROR_TYPE_DEBUG, 746 LOG(GNUNET_ERROR_TYPE_DEBUG,
751 "Queueing message to peer %s in MQM %p\n", 747 "Queueing message to peer %s in MQM %p\n",
752 GCP_2s (cp), 748 GCP_2s(cp),
753 mqm); 749 mqm);
754 GNUNET_assert (NULL != cp->core_mq); 750 GNUNET_assert(NULL != cp->core_mq);
755 GNUNET_assert (NULL == mqm->env); 751 GNUNET_assert(NULL == mqm->env);
756 GNUNET_MQ_notify_sent (env, 752 GNUNET_MQ_notify_sent(env,
757 &mqm_send_done, 753 &mqm_send_done,
758 cp); 754 cp);
759 mqm->env = env; 755 mqm->env = env;
760 cp->mqm_ready_counter++; 756 cp->mqm_ready_counter++;
761 if (mqm != cp->mqm_ready_ptr) 757 if (mqm != cp->mqm_ready_ptr)
762 cp->mqm_ready_ptr = cp->mqm_head; 758 cp->mqm_ready_ptr = cp->mqm_head;
763 if (1 == cp->mqm_ready_counter) 759 if (1 == cp->mqm_ready_counter)
764 cp->mqm_ready_ptr = mqm; 760 cp->mqm_ready_ptr = mqm;
765 if (0 != GNUNET_MQ_get_length (cp->core_mq)) 761 if (0 != GNUNET_MQ_get_length(cp->core_mq))
766 return; 762 return;
767 send_next_ready (cp); 763 send_next_ready(cp);
768} 764}
769 765
770 766
@@ -777,18 +773,18 @@ GCP_send (struct GCP_MessageQueueManager *mqm,
777 * @return #GNUNET_OK (continue to iterate) 773 * @return #GNUNET_OK (continue to iterate)
778 */ 774 */
779static int 775static int
780destroy_iterator_cb (void *cls, 776destroy_iterator_cb(void *cls,
781 const struct GNUNET_PeerIdentity *pid, 777 const struct GNUNET_PeerIdentity *pid,
782 void *value) 778 void *value)
783{ 779{
784 struct CadetPeer *cp = value; 780 struct CadetPeer *cp = value;
785 781
786 if (NULL != cp->destroy_task) 782 if (NULL != cp->destroy_task)
787 { 783 {
788 GNUNET_SCHEDULER_cancel (cp->destroy_task); 784 GNUNET_SCHEDULER_cancel(cp->destroy_task);
789 cp->destroy_task = NULL; 785 cp->destroy_task = NULL;
790 } 786 }
791 destroy_peer (cp); 787 destroy_peer(cp);
792 return GNUNET_OK; 788 return GNUNET_OK;
793} 789}
794 790
@@ -799,13 +795,13 @@ destroy_iterator_cb (void *cls,
799 * connections are down. 795 * connections are down.
800 */ 796 */
801void 797void
802GCP_destroy_all_peers () 798GCP_destroy_all_peers()
803{ 799{
804 LOG (GNUNET_ERROR_TYPE_DEBUG, 800 LOG(GNUNET_ERROR_TYPE_DEBUG,
805 "Destroying all peers now\n"); 801 "Destroying all peers now\n");
806 GNUNET_CONTAINER_multipeermap_iterate (peers, 802 GNUNET_CONTAINER_multipeermap_iterate(peers,
807 &destroy_iterator_cb, 803 &destroy_iterator_cb,
808 NULL); 804 NULL);
809} 805}
810 806
811 807
@@ -816,17 +812,17 @@ GCP_destroy_all_peers ()
816 * @param cp peer to drop paths to 812 * @param cp peer to drop paths to
817 */ 813 */
818void 814void
819GCP_drop_owned_paths (struct CadetPeer *cp) 815GCP_drop_owned_paths(struct CadetPeer *cp)
820{ 816{
821 struct CadetPeerPath *path; 817 struct CadetPeerPath *path;
822 818
823 LOG (GNUNET_ERROR_TYPE_DEBUG, 819 LOG(GNUNET_ERROR_TYPE_DEBUG,
824 "Destroying all paths to %s\n", 820 "Destroying all paths to %s\n",
825 GCP_2s (cp)); 821 GCP_2s(cp));
826 while (NULL != (path = 822 while (NULL != (path =
827 GNUNET_CONTAINER_heap_remove_root (cp->path_heap))) 823 GNUNET_CONTAINER_heap_remove_root(cp->path_heap)))
828 GCPP_release (path); 824 GCPP_release(path);
829 GNUNET_CONTAINER_heap_destroy (cp->path_heap); 825 GNUNET_CONTAINER_heap_destroy(cp->path_heap);
830 cp->path_heap = NULL; 826 cp->path_heap = NULL;
831} 827}
832 828
@@ -839,55 +835,55 @@ GCP_drop_owned_paths (struct CadetPeer *cp)
839 * @param off offset of this peer on the path 835 * @param off offset of this peer on the path
840 */ 836 */
841void 837void
842GCP_path_entry_add (struct CadetPeer *cp, 838GCP_path_entry_add(struct CadetPeer *cp,
843 struct CadetPeerPathEntry *entry, 839 struct CadetPeerPathEntry *entry,
844 unsigned int off) 840 unsigned int off)
845{ 841{
846 GNUNET_assert (cp == GCPP_get_peer_at_offset (entry->path, 842 GNUNET_assert(cp == GCPP_get_peer_at_offset(entry->path,
847 off)); 843 off));
848 LOG (GNUNET_ERROR_TYPE_DEBUG, 844 LOG(GNUNET_ERROR_TYPE_DEBUG,
849 "Discovered that peer %s is on path %s at offset %u\n", 845 "Discovered that peer %s is on path %s at offset %u\n",
850 GCP_2s (cp), 846 GCP_2s(cp),
851 GCPP_2s (entry->path), 847 GCPP_2s(entry->path),
852 off); 848 off);
853 if (off >= cp->path_dll_length) 849 if (off >= cp->path_dll_length)
854 { 850 {
855 unsigned int len = cp->path_dll_length; 851 unsigned int len = cp->path_dll_length;
856 852
857 GNUNET_array_grow (cp->path_heads, 853 GNUNET_array_grow(cp->path_heads,
858 len, 854 len,
859 off + 4); 855 off + 4);
860 GNUNET_array_grow (cp->path_tails, 856 GNUNET_array_grow(cp->path_tails,
861 cp->path_dll_length, 857 cp->path_dll_length,
862 off + 4); 858 off + 4);
863 } 859 }
864 GNUNET_CONTAINER_DLL_insert (cp->path_heads[off], 860 GNUNET_CONTAINER_DLL_insert(cp->path_heads[off],
865 cp->path_tails[off], 861 cp->path_tails[off],
866 entry); 862 entry);
867 cp->off_sum += off; 863 cp->off_sum += off;
868 cp->num_paths++; 864 cp->num_paths++;
869 865
870 /* If we have a tunnel to this peer, tell the tunnel that there is a 866 /* If we have a tunnel to this peer, tell the tunnel that there is a
871 new path available. */ 867 new path available. */
872 if (NULL != cp->t) 868 if (NULL != cp->t)
873 GCT_consider_path (cp->t, 869 GCT_consider_path(cp->t,
874 entry->path, 870 entry->path,
875 off); 871 off);
876 872
877 if ( (NULL != cp->search_h) && 873 if ((NULL != cp->search_h) &&
878 (DESIRED_CONNECTIONS_PER_TUNNEL <= cp->num_paths) ) 874 (DESIRED_CONNECTIONS_PER_TUNNEL <= cp->num_paths))
879 { 875 {
880 /* Now I have enough paths, stop search */ 876 /* Now I have enough paths, stop search */
881 GCD_search_stop (cp->search_h); 877 GCD_search_stop(cp->search_h);
882 cp->search_h = NULL; 878 cp->search_h = NULL;
883 } 879 }
884 if (NULL != cp->destroy_task) 880 if (NULL != cp->destroy_task)
885 { 881 {
886 /* paths changed, this resets the destroy timeout counter 882 /* paths changed, this resets the destroy timeout counter
887 and aborts a destroy task that may no longer be valid 883 and aborts a destroy task that may no longer be valid
888 to have (as we now have more paths via this peer). */ 884 to have (as we now have more paths via this peer). */
889 consider_peer_destroy (cp); 885 consider_peer_destroy(cp);
890 } 886 }
891} 887}
892 888
893 889
@@ -899,32 +895,32 @@ GCP_path_entry_add (struct CadetPeer *cp,
899 * @param off offset of this peer on the path 895 * @param off offset of this peer on the path
900 */ 896 */
901void 897void
902GCP_path_entry_remove (struct CadetPeer *cp, 898GCP_path_entry_remove(struct CadetPeer *cp,
903 struct CadetPeerPathEntry *entry, 899 struct CadetPeerPathEntry *entry,
904 unsigned int off) 900 unsigned int off)
905{ 901{
906 LOG (GNUNET_ERROR_TYPE_DEBUG, 902 LOG(GNUNET_ERROR_TYPE_DEBUG,
907 "Removing knowledge about peer %s beging on path %s at offset %u\n", 903 "Removing knowledge about peer %s beging on path %s at offset %u\n",
908 GCP_2s (cp), 904 GCP_2s(cp),
909 GCPP_2s (entry->path), 905 GCPP_2s(entry->path),
910 off); 906 off);
911 GNUNET_CONTAINER_DLL_remove (cp->path_heads[off], 907 GNUNET_CONTAINER_DLL_remove(cp->path_heads[off],
912 cp->path_tails[off], 908 cp->path_tails[off],
913 entry); 909 entry);
914 GNUNET_assert (0 < cp->num_paths); 910 GNUNET_assert(0 < cp->num_paths);
915 cp->off_sum -= off; 911 cp->off_sum -= off;
916 cp->num_paths--; 912 cp->num_paths--;
917 if ( (NULL == cp->core_mq) && 913 if ((NULL == cp->core_mq) &&
918 (NULL != cp->t) && 914 (NULL != cp->t) &&
919 (NULL == cp->search_h) && 915 (NULL == cp->search_h) &&
920 (DESIRED_CONNECTIONS_PER_TUNNEL > cp->num_paths) ) 916 (DESIRED_CONNECTIONS_PER_TUNNEL > cp->num_paths))
921 cp->search_h 917 cp->search_h
922 = GCD_search (&cp->pid); 918 = GCD_search(&cp->pid);
923 if (NULL == cp->destroy_task) 919 if (NULL == cp->destroy_task)
924 { 920 {
925 /* paths changed, we might now be ready for destruction, check again */ 921 /* paths changed, we might now be ready for destruction, check again */
926 consider_peer_destroy (cp); 922 consider_peer_destroy(cp);
927 } 923 }
928} 924}
929 925
930 926
@@ -935,32 +931,32 @@ GCP_path_entry_remove (struct CadetPeer *cp,
935 * @param cls the `struct CadetPeer` to maintain the path heap for 931 * @param cls the `struct CadetPeer` to maintain the path heap for
936 */ 932 */
937static void 933static void
938path_heap_cleanup (void *cls) 934path_heap_cleanup(void *cls)
939{ 935{
940 struct CadetPeer *cp = cls; 936 struct CadetPeer *cp = cls;
941 struct CadetPeerPath *root; 937 struct CadetPeerPath *root;
942 938
943 cp->heap_cleanup_task = NULL; 939 cp->heap_cleanup_task = NULL;
944 while (GNUNET_CONTAINER_heap_get_size (cp->path_heap) >= 940 while (GNUNET_CONTAINER_heap_get_size(cp->path_heap) >=
945 2 * DESIRED_CONNECTIONS_PER_TUNNEL) 941 2 * DESIRED_CONNECTIONS_PER_TUNNEL)
946 { 942 {
947 /* Now we have way too many, drop least desirable UNLESS it is in use! 943 /* Now we have way too many, drop least desirable UNLESS it is in use!
948 (Note that this intentionally keeps highly desireable, but currently 944 (Note that this intentionally keeps highly desireable, but currently
949 unused paths around in the hope that we might be able to switch, even 945 unused paths around in the hope that we might be able to switch, even
950 if the number of paths exceeds the threshold.) */ 946 if the number of paths exceeds the threshold.) */
951 root = GNUNET_CONTAINER_heap_peek (cp->path_heap); 947 root = GNUNET_CONTAINER_heap_peek(cp->path_heap);
952 GNUNET_assert (NULL != root); 948 GNUNET_assert(NULL != root);
953 if (NULL != 949 if (NULL !=
954 GCPP_get_connection (root, 950 GCPP_get_connection(root,
955 cp, 951 cp,
956 GCPP_get_length (root) - 1)) 952 GCPP_get_length(root) - 1))
957 break; /* can't fix */ 953 break; /* can't fix */
958 /* Got plenty of paths to this destination, and this is a low-quality 954 /* Got plenty of paths to this destination, and this is a low-quality
959 one that we don't care about. Allow it to die. */ 955 one that we don't care about. Allow it to die. */
960 GNUNET_assert (root == 956 GNUNET_assert(root ==
961 GNUNET_CONTAINER_heap_remove_root (cp->path_heap)); 957 GNUNET_CONTAINER_heap_remove_root(cp->path_heap));
962 GCPP_release (root); 958 GCPP_release(root);
963 } 959 }
964} 960}
965 961
966 962
@@ -976,66 +972,66 @@ path_heap_cleanup (void *cls)
976 * otherwise the node in the peer's path heap for the @a path. 972 * otherwise the node in the peer's path heap for the @a path.
977 */ 973 */
978struct GNUNET_CONTAINER_HeapNode * 974struct GNUNET_CONTAINER_HeapNode *
979GCP_attach_path (struct CadetPeer *cp, 975GCP_attach_path(struct CadetPeer *cp,
980 struct CadetPeerPath *path, 976 struct CadetPeerPath *path,
981 unsigned int off, 977 unsigned int off,
982 int force) 978 int force)
983{ 979{
984 GNUNET_CONTAINER_HeapCostType desirability; 980 GNUNET_CONTAINER_HeapCostType desirability;
985 struct CadetPeerPath *root; 981 struct CadetPeerPath *root;
986 GNUNET_CONTAINER_HeapCostType root_desirability; 982 GNUNET_CONTAINER_HeapCostType root_desirability;
987 struct GNUNET_CONTAINER_HeapNode *hn; 983 struct GNUNET_CONTAINER_HeapNode *hn;
988 984
989 GNUNET_assert (off == GCPP_get_length (path) - 1); 985 GNUNET_assert(off == GCPP_get_length(path) - 1);
990 GNUNET_assert (cp == GCPP_get_peer_at_offset (path, 986 GNUNET_assert(cp == GCPP_get_peer_at_offset(path,
991 off)); 987 off));
992 if (NULL == cp->path_heap) 988 if (NULL == cp->path_heap)
993 {
994 /* #GCP_drop_owned_paths() was already called, we cannot take new ones! */
995 GNUNET_assert (GNUNET_NO == force);
996 return NULL;
997 }
998 desirability = GCPP_get_desirability (path);
999 if (GNUNET_NO == force)
1000 {
1001 /* FIXME: desirability is not yet initialized; tricky! */
1002 if (GNUNET_NO ==
1003 GNUNET_CONTAINER_heap_peek2 (cp->path_heap,
1004 (void **) &root,
1005 &root_desirability))
1006 { 989 {
1007 root = NULL; 990 /* #GCP_drop_owned_paths() was already called, we cannot take new ones! */
1008 root_desirability = 0; 991 GNUNET_assert(GNUNET_NO == force);
992 return NULL;
1009 } 993 }
1010 994 desirability = GCPP_get_desirability(path);
1011 if ( (DESIRED_CONNECTIONS_PER_TUNNEL > cp->num_paths) && 995 if (GNUNET_NO == force)
1012 (desirability < root_desirability) )
1013 { 996 {
1014 LOG (GNUNET_ERROR_TYPE_DEBUG, 997 /* FIXME: desirability is not yet initialized; tricky! */
1015 "Decided to not attach path %s to peer %s due to undesirability\n", 998 if (GNUNET_NO ==
1016 GCPP_2s (path), 999 GNUNET_CONTAINER_heap_peek2(cp->path_heap,
1017 GCP_2s (cp)); 1000 (void **)&root,
1018 return NULL; 1001 &root_desirability))
1002 {
1003 root = NULL;
1004 root_desirability = 0;
1005 }
1006
1007 if ((DESIRED_CONNECTIONS_PER_TUNNEL > cp->num_paths) &&
1008 (desirability < root_desirability))
1009 {
1010 LOG(GNUNET_ERROR_TYPE_DEBUG,
1011 "Decided to not attach path %s to peer %s due to undesirability\n",
1012 GCPP_2s(path),
1013 GCP_2s(cp));
1014 return NULL;
1015 }
1019 } 1016 }
1020 }
1021 1017
1022 LOG (GNUNET_ERROR_TYPE_DEBUG, 1018 LOG(GNUNET_ERROR_TYPE_DEBUG,
1023 "Attaching path %s to peer %s (%s)\n", 1019 "Attaching path %s to peer %s (%s)\n",
1024 GCPP_2s (path), 1020 GCPP_2s(path),
1025 GCP_2s (cp), 1021 GCP_2s(cp),
1026 (GNUNET_NO == force) ? "desirable" : "forced"); 1022 (GNUNET_NO == force) ? "desirable" : "forced");
1027 1023
1028 /* Yes, we'd like to add this path, add to our heap */ 1024 /* Yes, we'd like to add this path, add to our heap */
1029 hn = GNUNET_CONTAINER_heap_insert (cp->path_heap, 1025 hn = GNUNET_CONTAINER_heap_insert(cp->path_heap,
1030 path, 1026 path,
1031 desirability); 1027 desirability);
1032 1028
1033 /* Consider maybe dropping other paths because of the new one */ 1029 /* Consider maybe dropping other paths because of the new one */
1034 if ( (GNUNET_CONTAINER_heap_get_size (cp->path_heap) >= 1030 if ((GNUNET_CONTAINER_heap_get_size(cp->path_heap) >=
1035 2 * DESIRED_CONNECTIONS_PER_TUNNEL) && 1031 2 * DESIRED_CONNECTIONS_PER_TUNNEL) &&
1036 (NULL != cp->heap_cleanup_task) ) 1032 (NULL != cp->heap_cleanup_task))
1037 cp->heap_cleanup_task = GNUNET_SCHEDULER_add_now (&path_heap_cleanup, 1033 cp->heap_cleanup_task = GNUNET_SCHEDULER_add_now(&path_heap_cleanup,
1038 cp); 1034 cp);
1039 return hn; 1035 return hn;
1040} 1036}
1041 1037
@@ -1050,16 +1046,16 @@ GCP_attach_path (struct CadetPeer *cp,
1050 * @param hn note in @a cp's path heap that must be deleted 1046 * @param hn note in @a cp's path heap that must be deleted
1051 */ 1047 */
1052void 1048void
1053GCP_detach_path (struct CadetPeer *cp, 1049GCP_detach_path(struct CadetPeer *cp,
1054 struct CadetPeerPath *path, 1050 struct CadetPeerPath *path,
1055 struct GNUNET_CONTAINER_HeapNode *hn) 1051 struct GNUNET_CONTAINER_HeapNode *hn)
1056{ 1052{
1057 LOG (GNUNET_ERROR_TYPE_DEBUG, 1053 LOG(GNUNET_ERROR_TYPE_DEBUG,
1058 "Detatching path %s from peer %s\n", 1054 "Detatching path %s from peer %s\n",
1059 GCPP_2s (path), 1055 GCPP_2s(path),
1060 GCP_2s (cp)); 1056 GCP_2s(cp));
1061 GNUNET_assert (path == 1057 GNUNET_assert(path ==
1062 GNUNET_CONTAINER_heap_remove_node (hn)); 1058 GNUNET_CONTAINER_heap_remove_node(hn));
1063} 1059}
1064 1060
1065 1061
@@ -1070,23 +1066,23 @@ GCP_detach_path (struct CadetPeer *cp,
1070 * @param cc the connection to add 1066 * @param cc the connection to add
1071 */ 1067 */
1072void 1068void
1073GCP_add_connection (struct CadetPeer *cp, 1069GCP_add_connection(struct CadetPeer *cp,
1074 struct CadetConnection *cc) 1070 struct CadetConnection *cc)
1075{ 1071{
1076 LOG (GNUNET_ERROR_TYPE_DEBUG, 1072 LOG(GNUNET_ERROR_TYPE_DEBUG,
1077 "Adding %s to peer %s\n", 1073 "Adding %s to peer %s\n",
1078 GCC_2s (cc), 1074 GCC_2s(cc),
1079 GCP_2s (cp)); 1075 GCP_2s(cp));
1080 GNUNET_assert (GNUNET_OK == 1076 GNUNET_assert(GNUNET_OK ==
1081 GNUNET_CONTAINER_multishortmap_put (cp->connections, 1077 GNUNET_CONTAINER_multishortmap_put(cp->connections,
1082 &GCC_get_id (cc)->connection_of_tunnel, 1078 &GCC_get_id(cc)->connection_of_tunnel,
1083 cc, 1079 cc,
1084 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 1080 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1085 if (NULL != cp->destroy_task) 1081 if (NULL != cp->destroy_task)
1086 { 1082 {
1087 GNUNET_SCHEDULER_cancel (cp->destroy_task); 1083 GNUNET_SCHEDULER_cancel(cp->destroy_task);
1088 cp->destroy_task = NULL; 1084 cp->destroy_task = NULL;
1089 } 1085 }
1090} 1086}
1091 1087
1092 1088
@@ -1097,18 +1093,18 @@ GCP_add_connection (struct CadetPeer *cp,
1097 * @param cc the connection to remove 1093 * @param cc the connection to remove
1098 */ 1094 */
1099void 1095void
1100GCP_remove_connection (struct CadetPeer *cp, 1096GCP_remove_connection(struct CadetPeer *cp,
1101 struct CadetConnection *cc) 1097 struct CadetConnection *cc)
1102{ 1098{
1103 LOG (GNUNET_ERROR_TYPE_DEBUG, 1099 LOG(GNUNET_ERROR_TYPE_DEBUG,
1104 "Removing connection %s from peer %s\n", 1100 "Removing connection %s from peer %s\n",
1105 GCC_2s (cc), 1101 GCC_2s(cc),
1106 GCP_2s (cp)); 1102 GCP_2s(cp));
1107 GNUNET_assert (GNUNET_YES == 1103 GNUNET_assert(GNUNET_YES ==
1108 GNUNET_CONTAINER_multishortmap_remove (cp->connections, 1104 GNUNET_CONTAINER_multishortmap_remove(cp->connections,
1109 &GCC_get_id (cc)->connection_of_tunnel, 1105 &GCC_get_id(cc)->connection_of_tunnel,
1110 cc)); 1106 cc));
1111 consider_peer_destroy (cp); 1107 consider_peer_destroy(cp);
1112} 1108}
1113 1109
1114 1110
@@ -1124,30 +1120,30 @@ GCP_remove_connection (struct CadetPeer *cp,
1124 * NULL if unknown and not requested @a create 1120 * NULL if unknown and not requested @a create
1125 */ 1121 */
1126struct CadetPeer * 1122struct CadetPeer *
1127GCP_get (const struct GNUNET_PeerIdentity *peer_id, 1123GCP_get(const struct GNUNET_PeerIdentity *peer_id,
1128 int create) 1124 int create)
1129{ 1125{
1130 struct CadetPeer *cp; 1126 struct CadetPeer *cp;
1131 1127
1132 cp = GNUNET_CONTAINER_multipeermap_get (peers, 1128 cp = GNUNET_CONTAINER_multipeermap_get(peers,
1133 peer_id); 1129 peer_id);
1134 if (NULL != cp) 1130 if (NULL != cp)
1135 return cp; 1131 return cp;
1136 if (GNUNET_NO == create) 1132 if (GNUNET_NO == create)
1137 return NULL; 1133 return NULL;
1138 cp = GNUNET_new (struct CadetPeer); 1134 cp = GNUNET_new(struct CadetPeer);
1139 cp->pid = *peer_id; 1135 cp->pid = *peer_id;
1140 cp->connections = GNUNET_CONTAINER_multishortmap_create (32, 1136 cp->connections = GNUNET_CONTAINER_multishortmap_create(32,
1141 GNUNET_YES); 1137 GNUNET_YES);
1142 cp->path_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN); 1138 cp->path_heap = GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
1143 GNUNET_assert (GNUNET_YES == 1139 GNUNET_assert(GNUNET_YES ==
1144 GNUNET_CONTAINER_multipeermap_put (peers, 1140 GNUNET_CONTAINER_multipeermap_put(peers,
1145 &cp->pid, 1141 &cp->pid,
1146 cp, 1142 cp,
1147 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 1143 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1148 LOG (GNUNET_ERROR_TYPE_DEBUG, 1144 LOG(GNUNET_ERROR_TYPE_DEBUG,
1149 "Creating peer %s\n", 1145 "Creating peer %s\n",
1150 GCP_2s (cp)); 1146 GCP_2s(cp));
1151 return cp; 1147 return cp;
1152} 1148}
1153 1149
@@ -1159,7 +1155,7 @@ GCP_get (const struct GNUNET_PeerIdentity *peer_id,
1159 * @return the peer identity 1155 * @return the peer identity
1160 */ 1156 */
1161const struct GNUNET_PeerIdentity * 1157const struct GNUNET_PeerIdentity *
1162GCP_get_id (struct CadetPeer *cp) 1158GCP_get_id(struct CadetPeer *cp)
1163{ 1159{
1164 return &cp->pid; 1160 return &cp->pid;
1165} 1161}
@@ -1172,12 +1168,12 @@ GCP_get_id (struct CadetPeer *cp)
1172 * @param cls Closure for @c iter. 1168 * @param cls Closure for @c iter.
1173 */ 1169 */
1174void 1170void
1175GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, 1171GCP_iterate_all(GNUNET_CONTAINER_PeerMapIterator iter,
1176 void *cls) 1172 void *cls)
1177{ 1173{
1178 GNUNET_CONTAINER_multipeermap_iterate (peers, 1174 GNUNET_CONTAINER_multipeermap_iterate(peers,
1179 iter, 1175 iter,
1180 cls); 1176 cls);
1181} 1177}
1182 1178
1183 1179
@@ -1188,7 +1184,7 @@ GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
1188 * @return Number of known paths. 1184 * @return Number of known paths.
1189 */ 1185 */
1190unsigned int 1186unsigned int
1191GCP_count_paths (const struct CadetPeer *cp) 1187GCP_count_paths(const struct CadetPeer *cp)
1192{ 1188{
1193 return cp->num_paths; 1189 return cp->num_paths;
1194} 1190}
@@ -1203,45 +1199,45 @@ GCP_count_paths (const struct CadetPeer *cp)
1203 * @return Number of iterated paths. 1199 * @return Number of iterated paths.
1204 */ 1200 */
1205unsigned int 1201unsigned int
1206GCP_iterate_paths (struct CadetPeer *cp, 1202GCP_iterate_paths(struct CadetPeer *cp,
1207 GCP_PathIterator callback, 1203 GCP_PathIterator callback,
1208 void *callback_cls) 1204 void *callback_cls)
1209{ 1205{
1210 unsigned int ret = 0; 1206 unsigned int ret = 0;
1211 1207
1212 LOG (GNUNET_ERROR_TYPE_DEBUG, 1208 LOG(GNUNET_ERROR_TYPE_DEBUG,
1213 "Iterating over paths to peer %s%s\n", 1209 "Iterating over paths to peer %s%s\n",
1214 GCP_2s (cp), 1210 GCP_2s(cp),
1215 (NULL == cp->core_mq) ? "" : " including direct link"); 1211 (NULL == cp->core_mq) ? "" : " including direct link");
1216 if (NULL != cp->core_mq) 1212 if (NULL != cp->core_mq)
1217 {
1218 /* FIXME: this branch seems to duplicate the
1219 i=0 case below (direct link). Leave out!??? -CG */
1220 struct CadetPeerPath *path;
1221
1222 path = GCPP_get_path_from_route (1,
1223 &cp->pid);
1224 ret++;
1225 if (GNUNET_NO ==
1226 callback (callback_cls,
1227 path,
1228 0))
1229 return ret;
1230 }
1231 for (unsigned int i=0;i<cp->path_dll_length;i++)
1232 {
1233 for (struct CadetPeerPathEntry *pe = cp->path_heads[i];
1234 NULL != pe;
1235 pe = pe->next)
1236 { 1213 {
1214 /* FIXME: this branch seems to duplicate the
1215 i=0 case below (direct link). Leave out!??? -CG */
1216 struct CadetPeerPath *path;
1217
1218 path = GCPP_get_path_from_route(1,
1219 &cp->pid);
1237 ret++; 1220 ret++;
1238 if (GNUNET_NO == 1221 if (GNUNET_NO ==
1239 callback (callback_cls, 1222 callback(callback_cls,
1240 pe->path, 1223 path,
1241 i)) 1224 0))
1242 return ret; 1225 return ret;
1243 } 1226 }
1244 } 1227 for (unsigned int i = 0; i < cp->path_dll_length; i++)
1228 {
1229 for (struct CadetPeerPathEntry *pe = cp->path_heads[i];
1230 NULL != pe;
1231 pe = pe->next)
1232 {
1233 ret++;
1234 if (GNUNET_NO ==
1235 callback(callback_cls,
1236 pe->path,
1237 i))
1238 return ret;
1239 }
1240 }
1245 return ret; 1241 return ret;
1246} 1242}
1247 1243
@@ -1254,29 +1250,29 @@ GCP_iterate_paths (struct CadetPeer *cp,
1254 * @return Number of iterated paths. 1250 * @return Number of iterated paths.
1255 */ 1251 */
1256unsigned int 1252unsigned int
1257GCP_iterate_indirect_paths (struct CadetPeer *cp, 1253GCP_iterate_indirect_paths(struct CadetPeer *cp,
1258 GCP_PathIterator callback, 1254 GCP_PathIterator callback,
1259 void *callback_cls) 1255 void *callback_cls)
1260{ 1256{
1261 unsigned int ret = 0; 1257 unsigned int ret = 0;
1262 1258
1263 LOG (GNUNET_ERROR_TYPE_DEBUG, 1259 LOG(GNUNET_ERROR_TYPE_DEBUG,
1264 "Iterating over paths to peer %s without direct link\n", 1260 "Iterating over paths to peer %s without direct link\n",
1265 GCP_2s (cp)); 1261 GCP_2s(cp));
1266 for (unsigned int i=1;i<cp->path_dll_length;i++) 1262 for (unsigned int i = 1; i < cp->path_dll_length; i++)
1267 {
1268 for (struct CadetPeerPathEntry *pe = cp->path_heads[i];
1269 NULL != pe;
1270 pe = pe->next)
1271 { 1263 {
1272 ret++; 1264 for (struct CadetPeerPathEntry *pe = cp->path_heads[i];
1273 if (GNUNET_NO == 1265 NULL != pe;
1274 callback (callback_cls, 1266 pe = pe->next)
1275 pe->path, 1267 {
1276 i)) 1268 ret++;
1277 return ret; 1269 if (GNUNET_NO ==
1270 callback(callback_cls,
1271 pe->path,
1272 i))
1273 return ret;
1274 }
1278 } 1275 }
1279 }
1280 return ret; 1276 return ret;
1281} 1277}
1282 1278
@@ -1292,32 +1288,32 @@ GCP_iterate_indirect_paths (struct CadetPeer *cp,
1292 * @return Number of iterated paths. 1288 * @return Number of iterated paths.
1293 */ 1289 */
1294unsigned int 1290unsigned int
1295GCP_iterate_paths_at (struct CadetPeer *cp, 1291GCP_iterate_paths_at(struct CadetPeer *cp,
1296 unsigned int dist, 1292 unsigned int dist,
1297 GCP_PathIterator callback, 1293 GCP_PathIterator callback,
1298 void *callback_cls) 1294 void *callback_cls)
1299{ 1295{
1300 unsigned int ret = 0; 1296 unsigned int ret = 0;
1301 1297
1302 if (dist >= cp->path_dll_length) 1298 if (dist >= cp->path_dll_length)
1303 { 1299 {
1304 LOG (GNUNET_ERROR_TYPE_DEBUG, 1300 LOG(GNUNET_ERROR_TYPE_DEBUG,
1305 "Asked to look for paths at distance %u, but maximum for me is < %u\n", 1301 "Asked to look for paths at distance %u, but maximum for me is < %u\n",
1306 dist, 1302 dist,
1307 cp->path_dll_length); 1303 cp->path_dll_length);
1308 return 0; 1304 return 0;
1309 } 1305 }
1310 for (struct CadetPeerPathEntry *pe = cp->path_heads[dist]; 1306 for (struct CadetPeerPathEntry *pe = cp->path_heads[dist];
1311 NULL != pe; 1307 NULL != pe;
1312 pe = pe->next) 1308 pe = pe->next)
1313 { 1309 {
1314 if (GNUNET_NO == 1310 if (GNUNET_NO ==
1315 callback (callback_cls, 1311 callback(callback_cls,
1316 pe->path, 1312 pe->path,
1317 dist)) 1313 dist))
1318 return ret; 1314 return ret;
1319 ret++; 1315 ret++;
1320 } 1316 }
1321 return ret; 1317 return ret;
1322} 1318}
1323 1319
@@ -1330,16 +1326,16 @@ GCP_iterate_paths_at (struct CadetPeer *cp,
1330 * @return Tunnel towards peer. 1326 * @return Tunnel towards peer.
1331 */ 1327 */
1332struct CadetTunnel * 1328struct CadetTunnel *
1333GCP_get_tunnel (struct CadetPeer *cp, 1329GCP_get_tunnel(struct CadetPeer *cp,
1334 int create) 1330 int create)
1335{ 1331{
1336 if (NULL == cp) 1332 if (NULL == cp)
1337 return NULL; 1333 return NULL;
1338 if ( (NULL != cp->t) || 1334 if ((NULL != cp->t) ||
1339 (GNUNET_NO == create) ) 1335 (GNUNET_NO == create))
1340 return cp->t; 1336 return cp->t;
1341 cp->t = GCT_create_tunnel (cp); 1337 cp->t = GCT_create_tunnel(cp);
1342 consider_peer_activate (cp); 1338 consider_peer_activate(cp);
1343 return cp->t; 1339 return cp->t;
1344} 1340}
1345 1341
@@ -1351,7 +1347,7 @@ GCP_get_tunnel (struct CadetPeer *cp,
1351 * @param cls the `struct CadetPeer` where the offer completed 1347 * @param cls the `struct CadetPeer` where the offer completed
1352 */ 1348 */
1353static void 1349static void
1354hello_offer_done (void *cls) 1350hello_offer_done(void *cls)
1355{ 1351{
1356 struct CadetPeer *cp = cls; 1352 struct CadetPeer *cp = cls;
1357 1353
@@ -1367,39 +1363,39 @@ hello_offer_done (void *cls)
1367 * @param hello the HELLO to remember 1363 * @param hello the HELLO to remember
1368 */ 1364 */
1369void 1365void
1370GCP_set_hello (struct CadetPeer *cp, 1366GCP_set_hello(struct CadetPeer *cp,
1371 const struct GNUNET_HELLO_Message *hello) 1367 const struct GNUNET_HELLO_Message *hello)
1372{ 1368{
1373 struct GNUNET_HELLO_Message *mrg; 1369 struct GNUNET_HELLO_Message *mrg;
1374 1370
1375 LOG (GNUNET_ERROR_TYPE_DEBUG, 1371 LOG(GNUNET_ERROR_TYPE_DEBUG,
1376 "Got %u byte HELLO for peer %s\n", 1372 "Got %u byte HELLO for peer %s\n",
1377 (unsigned int) GNUNET_HELLO_size (hello), 1373 (unsigned int)GNUNET_HELLO_size(hello),
1378 GCP_2s (cp)); 1374 GCP_2s(cp));
1379 if (NULL != cp->hello_offer) 1375 if (NULL != cp->hello_offer)
1380 { 1376 {
1381 GNUNET_TRANSPORT_offer_hello_cancel (cp->hello_offer); 1377 GNUNET_TRANSPORT_offer_hello_cancel(cp->hello_offer);
1382 cp->hello_offer = NULL; 1378 cp->hello_offer = NULL;
1383 } 1379 }
1384 if (NULL != cp->hello) 1380 if (NULL != cp->hello)
1385 { 1381 {
1386 mrg = GNUNET_HELLO_merge (hello, 1382 mrg = GNUNET_HELLO_merge(hello,
1387 cp->hello); 1383 cp->hello);
1388 GNUNET_free (cp->hello); 1384 GNUNET_free(cp->hello);
1389 cp->hello = mrg; 1385 cp->hello = mrg;
1390 } 1386 }
1391 else 1387 else
1392 { 1388 {
1393 cp->hello = GNUNET_memdup (hello, 1389 cp->hello = GNUNET_memdup(hello,
1394 GNUNET_HELLO_size (hello)); 1390 GNUNET_HELLO_size(hello));
1395 } 1391 }
1396 cp->hello_offer 1392 cp->hello_offer
1397 = GNUNET_TRANSPORT_offer_hello (cfg, 1393 = GNUNET_TRANSPORT_offer_hello(cfg,
1398 GNUNET_HELLO_get_header (cp->hello) , 1394 GNUNET_HELLO_get_header(cp->hello),
1399 &hello_offer_done, 1395 &hello_offer_done,
1400 cp); 1396 cp);
1401 /* New HELLO means cp's destruction time may change... */ 1397 /* New HELLO means cp's destruction time may change... */
1402 consider_peer_destroy (cp); 1398 consider_peer_destroy(cp);
1403} 1399}
1404 1400
1405 1401
@@ -1411,16 +1407,16 @@ GCP_set_hello (struct CadetPeer *cp,
1411 * @param t the dead tunnel 1407 * @param t the dead tunnel
1412 */ 1408 */
1413void 1409void
1414GCP_drop_tunnel (struct CadetPeer *cp, 1410GCP_drop_tunnel(struct CadetPeer *cp,
1415 struct CadetTunnel *t) 1411 struct CadetTunnel *t)
1416{ 1412{
1417 LOG (GNUNET_ERROR_TYPE_DEBUG, 1413 LOG(GNUNET_ERROR_TYPE_DEBUG,
1418 "Dropping tunnel %s to peer %s\n", 1414 "Dropping tunnel %s to peer %s\n",
1419 GCT_2s (t), 1415 GCT_2s(t),
1420 GCP_2s (cp)); 1416 GCP_2s(cp));
1421 GNUNET_assert (cp->t == t); 1417 GNUNET_assert(cp->t == t);
1422 cp->t = NULL; 1418 cp->t = NULL;
1423 consider_peer_destroy (cp); 1419 consider_peer_destroy(cp);
1424} 1420}
1425 1421
1426 1422
@@ -1431,7 +1427,7 @@ GCP_drop_tunnel (struct CadetPeer *cp,
1431 * @return #GNUNET_YES if @a cp has a core-level connection 1427 * @return #GNUNET_YES if @a cp has a core-level connection
1432 */ 1428 */
1433int 1429int
1434GCP_has_core_connection (struct CadetPeer *cp) 1430GCP_has_core_connection(struct CadetPeer *cp)
1435{ 1431{
1436 return (NULL != cp->core_mq) ? GNUNET_YES : GNUNET_NO; 1432 return (NULL != cp->core_mq) ? GNUNET_YES : GNUNET_NO;
1437} 1433}
@@ -1446,26 +1442,26 @@ GCP_has_core_connection (struct CadetPeer *cp)
1446 * @return handle to cancel request 1442 * @return handle to cancel request
1447 */ 1443 */
1448struct GCP_MessageQueueManager * 1444struct GCP_MessageQueueManager *
1449GCP_request_mq (struct CadetPeer *cp, 1445GCP_request_mq(struct CadetPeer *cp,
1450 GCP_MessageQueueNotificationCallback cb, 1446 GCP_MessageQueueNotificationCallback cb,
1451 void *cb_cls) 1447 void *cb_cls)
1452{ 1448{
1453 struct GCP_MessageQueueManager *mqm; 1449 struct GCP_MessageQueueManager *mqm;
1454 1450
1455 mqm = GNUNET_new (struct GCP_MessageQueueManager); 1451 mqm = GNUNET_new(struct GCP_MessageQueueManager);
1456 mqm->cb = cb; 1452 mqm->cb = cb;
1457 mqm->cb_cls = cb_cls; 1453 mqm->cb_cls = cb_cls;
1458 mqm->cp = cp; 1454 mqm->cp = cp;
1459 GNUNET_CONTAINER_DLL_insert (cp->mqm_head, 1455 GNUNET_CONTAINER_DLL_insert(cp->mqm_head,
1460 cp->mqm_tail, 1456 cp->mqm_tail,
1461 mqm); 1457 mqm);
1462 LOG (GNUNET_ERROR_TYPE_DEBUG, 1458 LOG(GNUNET_ERROR_TYPE_DEBUG,
1463 "Creating MQM %p for peer %s\n", 1459 "Creating MQM %p for peer %s\n",
1464 mqm, 1460 mqm,
1465 GCP_2s (cp)); 1461 GCP_2s(cp));
1466 if (NULL != cp->core_mq) 1462 if (NULL != cp->core_mq)
1467 cb (cb_cls, 1463 cb(cb_cls,
1468 GNUNET_YES); 1464 GNUNET_YES);
1469 return mqm; 1465 return mqm;
1470} 1466}
1471 1467
@@ -1477,39 +1473,39 @@ GCP_request_mq (struct CadetPeer *cp,
1477 * @param last_env final message to transmit, or NULL 1473 * @param last_env final message to transmit, or NULL
1478 */ 1474 */
1479void 1475void
1480GCP_request_mq_cancel (struct GCP_MessageQueueManager *mqm, 1476GCP_request_mq_cancel(struct GCP_MessageQueueManager *mqm,
1481 struct GNUNET_MQ_Envelope *last_env) 1477 struct GNUNET_MQ_Envelope *last_env)
1482{ 1478{
1483 struct CadetPeer *cp = mqm->cp; 1479 struct CadetPeer *cp = mqm->cp;
1484 1480
1485 LOG (GNUNET_ERROR_TYPE_DEBUG, 1481 LOG(GNUNET_ERROR_TYPE_DEBUG,
1486 "Destroying MQM %p for peer %s%s\n", 1482 "Destroying MQM %p for peer %s%s\n",
1487 mqm, 1483 mqm,
1488 GCP_2s (cp), 1484 GCP_2s(cp),
1489 (NULL == last_env) ? "" : " with last ditch transmission"); 1485 (NULL == last_env) ? "" : " with last ditch transmission");
1490 if (NULL != mqm->env) 1486 if (NULL != mqm->env)
1491 GNUNET_MQ_discard (mqm->env); 1487 GNUNET_MQ_discard(mqm->env);
1492 if (NULL != last_env) 1488 if (NULL != last_env)
1493 {
1494 if (NULL != cp->core_mq)
1495 {
1496 GNUNET_MQ_notify_sent (last_env,
1497 &mqm_send_done,
1498 cp);
1499 GNUNET_MQ_send (cp->core_mq,
1500 last_env);
1501 }
1502 else
1503 { 1489 {
1504 GNUNET_MQ_discard (last_env); 1490 if (NULL != cp->core_mq)
1491 {
1492 GNUNET_MQ_notify_sent(last_env,
1493 &mqm_send_done,
1494 cp);
1495 GNUNET_MQ_send(cp->core_mq,
1496 last_env);
1497 }
1498 else
1499 {
1500 GNUNET_MQ_discard(last_env);
1501 }
1505 } 1502 }
1506 }
1507 if (cp->mqm_ready_ptr == mqm) 1503 if (cp->mqm_ready_ptr == mqm)
1508 cp->mqm_ready_ptr = mqm->next; 1504 cp->mqm_ready_ptr = mqm->next;
1509 GNUNET_CONTAINER_DLL_remove (cp->mqm_head, 1505 GNUNET_CONTAINER_DLL_remove(cp->mqm_head,
1510 cp->mqm_tail, 1506 cp->mqm_tail,
1511 mqm); 1507 mqm);
1512 GNUNET_free (mqm); 1508 GNUNET_free(mqm);
1513} 1509}
1514 1510
1515 1511
@@ -1523,27 +1519,27 @@ GCP_request_mq_cancel (struct GCP_MessageQueueManager *mqm,
1523 * @param env envelope with the message to send 1519 * @param env envelope with the message to send
1524 */ 1520 */
1525void 1521void
1526GCP_send_ooo (struct CadetPeer *cp, 1522GCP_send_ooo(struct CadetPeer *cp,
1527 struct GNUNET_MQ_Envelope *env) 1523 struct GNUNET_MQ_Envelope *env)
1528{ 1524{
1529 LOG (GNUNET_ERROR_TYPE_DEBUG, 1525 LOG(GNUNET_ERROR_TYPE_DEBUG,
1530 "Sending message to %s out of management\n", 1526 "Sending message to %s out of management\n",
1531 GCP_2s (cp)); 1527 GCP_2s(cp));
1532 if (NULL == cp->core_mq) 1528 if (NULL == cp->core_mq)
1533 { 1529 {
1534 GNUNET_MQ_discard (env); 1530 GNUNET_MQ_discard(env);
1535 return; 1531 return;
1536 } 1532 }
1537 if (GNUNET_MQ_get_length (cp->core_mq) > MAX_OOO_QUEUE_SIZE) 1533 if (GNUNET_MQ_get_length(cp->core_mq) > MAX_OOO_QUEUE_SIZE)
1538 { 1534 {
1539 GNUNET_MQ_discard (env); 1535 GNUNET_MQ_discard(env);
1540 return; 1536 return;
1541 } 1537 }
1542 GNUNET_MQ_notify_sent (env, 1538 GNUNET_MQ_notify_sent(env,
1543 &mqm_send_done, 1539 &mqm_send_done,
1544 cp); 1540 cp);
1545 GNUNET_MQ_send (cp->core_mq, 1541 GNUNET_MQ_send(cp->core_mq,
1546 env); 1542 env);
1547} 1543}
1548 1544
1549 1545
diff --git a/src/cadet/gnunet-service-cadet_peer.h b/src/cadet/gnunet-service-cadet_peer.h
index 4eee7e9e8..09c6f64c9 100644
--- a/src/cadet/gnunet-service-cadet_peer.h
+++ b/src/cadet/gnunet-service-cadet_peer.h
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file cadet/gnunet-service-cadet_peer.h 22 * @file cadet/gnunet-service-cadet_peer.h
@@ -39,7 +39,7 @@
39 * @return Static string for it's ID. 39 * @return Static string for it's ID.
40 */ 40 */
41const char * 41const char *
42GCP_2s (const struct CadetPeer *peer); 42GCP_2s(const struct CadetPeer *peer);
43 43
44 44
45/** 45/**
@@ -54,8 +54,8 @@ GCP_2s (const struct CadetPeer *peer);
54 * NULL if unknown and not requested @a create 54 * NULL if unknown and not requested @a create
55 */ 55 */
56struct CadetPeer * 56struct CadetPeer *
57GCP_get (const struct GNUNET_PeerIdentity *peer_id, 57GCP_get(const struct GNUNET_PeerIdentity *peer_id,
58 int create); 58 int create);
59 59
60 60
61/** 61/**
@@ -68,8 +68,8 @@ GCP_get (const struct GNUNET_PeerIdentity *peer_id,
68 * positive scores mean path is more desirable 68 * positive scores mean path is more desirable
69 */ 69 */
70double 70double
71GCP_get_desirability_of_path (struct CadetPeer *cp, 71GCP_get_desirability_of_path(struct CadetPeer *cp,
72 unsigned int off); 72 unsigned int off);
73 73
74 74
75/** 75/**
@@ -79,7 +79,7 @@ GCP_get_desirability_of_path (struct CadetPeer *cp,
79 * @return the peer identity 79 * @return the peer identity
80 */ 80 */
81const struct GNUNET_PeerIdentity * 81const struct GNUNET_PeerIdentity *
82GCP_get_id (struct CadetPeer *cp); 82GCP_get_id(struct CadetPeer *cp);
83 83
84 84
85/** 85/**
@@ -89,8 +89,8 @@ GCP_get_id (struct CadetPeer *cp);
89 * @param cls Closure for @c iter. 89 * @param cls Closure for @c iter.
90 */ 90 */
91void 91void
92GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, 92GCP_iterate_all(GNUNET_CONTAINER_PeerMapIterator iter,
93 void *cls); 93 void *cls);
94 94
95 95
96/** 96/**
@@ -100,7 +100,7 @@ GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
100 * @return Number of known paths. 100 * @return Number of known paths.
101 */ 101 */
102unsigned int 102unsigned int
103GCP_count_paths (const struct CadetPeer *cp); 103GCP_count_paths(const struct CadetPeer *cp);
104 104
105 105
106/** 106/**
@@ -110,7 +110,7 @@ GCP_count_paths (const struct CadetPeer *cp);
110 * @param cp peer to drop paths to 110 * @param cp peer to drop paths to
111 */ 111 */
112void 112void
113GCP_drop_owned_paths (struct CadetPeer *cp); 113GCP_drop_owned_paths(struct CadetPeer *cp);
114 114
115 115
116/** 116/**
@@ -137,9 +137,9 @@ typedef int
137 * @return Number of iterated paths. 137 * @return Number of iterated paths.
138 */ 138 */
139unsigned int 139unsigned int
140GCP_iterate_paths (struct CadetPeer *cp, 140GCP_iterate_paths(struct CadetPeer *cp,
141 GCP_PathIterator callback, 141 GCP_PathIterator callback,
142 void *callback_cls); 142 void *callback_cls);
143 143
144/** 144/**
145 * Iterate over the paths to a peer without direct link. 145 * Iterate over the paths to a peer without direct link.
@@ -150,9 +150,9 @@ GCP_iterate_paths (struct CadetPeer *cp,
150 * @return Number of iterated paths. 150 * @return Number of iterated paths.
151 */ 151 */
152unsigned int 152unsigned int
153GCP_iterate_indirect_paths (struct CadetPeer *cp, 153GCP_iterate_indirect_paths(struct CadetPeer *cp,
154 GCP_PathIterator callback, 154 GCP_PathIterator callback,
155 void *callback_cls); 155 void *callback_cls);
156 156
157 157
158/** 158/**
@@ -166,10 +166,10 @@ GCP_iterate_indirect_paths (struct CadetPeer *cp,
166 * @return Number of iterated paths. 166 * @return Number of iterated paths.
167 */ 167 */
168unsigned int 168unsigned int
169GCP_iterate_paths_at (struct CadetPeer *cp, 169GCP_iterate_paths_at(struct CadetPeer *cp,
170 unsigned int dist, 170 unsigned int dist,
171 GCP_PathIterator callback, 171 GCP_PathIterator callback,
172 void *callback_cls); 172 void *callback_cls);
173 173
174 174
175/** 175/**
@@ -180,9 +180,9 @@ GCP_iterate_paths_at (struct CadetPeer *cp,
180 * @param off offset of this peer on the path 180 * @param off offset of this peer on the path
181 */ 181 */
182void 182void
183GCP_path_entry_remove (struct CadetPeer *cp, 183GCP_path_entry_remove(struct CadetPeer *cp,
184 struct CadetPeerPathEntry *entry, 184 struct CadetPeerPathEntry *entry,
185 unsigned int off); 185 unsigned int off);
186 186
187 187
188/** 188/**
@@ -193,9 +193,9 @@ GCP_path_entry_remove (struct CadetPeer *cp,
193 * @param off offset of this peer on the path 193 * @param off offset of this peer on the path
194 */ 194 */
195void 195void
196GCP_path_entry_add (struct CadetPeer *cp, 196GCP_path_entry_add(struct CadetPeer *cp,
197 struct CadetPeerPathEntry *entry, 197 struct CadetPeerPathEntry *entry,
198 unsigned int off); 198 unsigned int off);
199 199
200 200
201/** 201/**
@@ -206,8 +206,8 @@ GCP_path_entry_add (struct CadetPeer *cp,
206 * @return Tunnel towards peer. 206 * @return Tunnel towards peer.
207 */ 207 */
208struct CadetTunnel * 208struct CadetTunnel *
209GCP_get_tunnel (struct CadetPeer *cp, 209GCP_get_tunnel(struct CadetPeer *cp,
210 int create); 210 int create);
211 211
212 212
213/** 213/**
@@ -218,8 +218,8 @@ GCP_get_tunnel (struct CadetPeer *cp,
218 * @param t the dead tunnel 218 * @param t the dead tunnel
219 */ 219 */
220void 220void
221GCP_drop_tunnel (struct CadetPeer *cp, 221GCP_drop_tunnel(struct CadetPeer *cp,
222 struct CadetTunnel *t); 222 struct CadetTunnel *t);
223 223
224 224
225/** 225/**
@@ -234,10 +234,10 @@ GCP_drop_tunnel (struct CadetPeer *cp,
234 * otherwise the node in the peer's path heap for the @a path. 234 * otherwise the node in the peer's path heap for the @a path.
235 */ 235 */
236struct GNUNET_CONTAINER_HeapNode * 236struct GNUNET_CONTAINER_HeapNode *
237GCP_attach_path (struct CadetPeer *cp, 237GCP_attach_path(struct CadetPeer *cp,
238 struct CadetPeerPath *path, 238 struct CadetPeerPath *path,
239 unsigned int off, 239 unsigned int off,
240 int force); 240 int force);
241 241
242 242
243/** 243/**
@@ -250,9 +250,9 @@ GCP_attach_path (struct CadetPeer *cp,
250 * @param hn note in @a cp's path heap that must be deleted 250 * @param hn note in @a cp's path heap that must be deleted
251 */ 251 */
252void 252void
253GCP_detach_path (struct CadetPeer *cp, 253GCP_detach_path(struct CadetPeer *cp,
254 struct CadetPeerPath *path, 254 struct CadetPeerPath *path,
255 struct GNUNET_CONTAINER_HeapNode *hn); 255 struct GNUNET_CONTAINER_HeapNode *hn);
256 256
257 257
258/** 258/**
@@ -262,8 +262,8 @@ GCP_detach_path (struct CadetPeer *cp,
262 * @param cc the connection to add 262 * @param cc the connection to add
263 */ 263 */
264void 264void
265GCP_add_connection (struct CadetPeer *cp, 265GCP_add_connection(struct CadetPeer *cp,
266 struct CadetConnection *cc); 266 struct CadetConnection *cc);
267 267
268 268
269/** 269/**
@@ -273,8 +273,8 @@ GCP_add_connection (struct CadetPeer *cp,
273 * @param cc the connection to remove 273 * @param cc the connection to remove
274 */ 274 */
275void 275void
276GCP_remove_connection (struct CadetPeer *cp, 276GCP_remove_connection(struct CadetPeer *cp,
277 struct CadetConnection *cc); 277 struct CadetConnection *cc);
278 278
279 279
280/** 280/**
@@ -285,8 +285,8 @@ GCP_remove_connection (struct CadetPeer *cp,
285 * @param hello the HELLO to remember 285 * @param hello the HELLO to remember
286 */ 286 */
287void 287void
288GCP_set_hello (struct CadetPeer *cp, 288GCP_set_hello(struct CadetPeer *cp,
289 const struct GNUNET_HELLO_Message *hello); 289 const struct GNUNET_HELLO_Message *hello);
290 290
291 291
292/** 292/**
@@ -295,7 +295,7 @@ GCP_set_hello (struct CadetPeer *cp,
295 * connections are down. 295 * connections are down.
296 */ 296 */
297void 297void
298GCP_destroy_all_peers (void); 298GCP_destroy_all_peers(void);
299 299
300 300
301/** 301/**
@@ -334,9 +334,9 @@ typedef void
334 * @return handle to cancel request 334 * @return handle to cancel request
335 */ 335 */
336struct GCP_MessageQueueManager * 336struct GCP_MessageQueueManager *
337GCP_request_mq (struct CadetPeer *cp, 337GCP_request_mq(struct CadetPeer *cp,
338 GCP_MessageQueueNotificationCallback cb, 338 GCP_MessageQueueNotificationCallback cb,
339 void *cb_cls); 339 void *cb_cls);
340 340
341 341
342/** 342/**
@@ -346,7 +346,7 @@ GCP_request_mq (struct CadetPeer *cp,
346 * @return #GNUNET_YES if @a cp has a core-level connection 346 * @return #GNUNET_YES if @a cp has a core-level connection
347 */ 347 */
348int 348int
349GCP_has_core_connection (struct CadetPeer *cp); 349GCP_has_core_connection(struct CadetPeer *cp);
350 350
351 351
352/** 352/**
@@ -361,8 +361,8 @@ GCP_has_core_connection (struct CadetPeer *cp);
361 * yet have a #GNUNET_MQ_notify_sent() callback attached to it 361 * yet have a #GNUNET_MQ_notify_sent() callback attached to it
362 */ 362 */
363void 363void
364GCP_send (struct GCP_MessageQueueManager *mqm, 364GCP_send(struct GCP_MessageQueueManager *mqm,
365 struct GNUNET_MQ_Envelope *env); 365 struct GNUNET_MQ_Envelope *env);
366 366
367 367
368/** 368/**
@@ -375,8 +375,8 @@ GCP_send (struct GCP_MessageQueueManager *mqm,
375 * @param env envelope with the message to send 375 * @param env envelope with the message to send
376 */ 376 */
377void 377void
378GCP_send_ooo (struct CadetPeer *cp, 378GCP_send_ooo(struct CadetPeer *cp,
379 struct GNUNET_MQ_Envelope *env); 379 struct GNUNET_MQ_Envelope *env);
380 380
381 381
382/** 382/**
@@ -388,8 +388,8 @@ GCP_send_ooo (struct CadetPeer *cp,
388 * @param last_env final message to transmit, or NULL 388 * @param last_env final message to transmit, or NULL
389 */ 389 */
390void 390void
391GCP_request_mq_cancel (struct GCP_MessageQueueManager *mqm, 391GCP_request_mq_cancel(struct GCP_MessageQueueManager *mqm,
392 struct GNUNET_MQ_Envelope *last_env); 392 struct GNUNET_MQ_Envelope *last_env);
393 393
394 394
395/** 395/**
@@ -399,8 +399,8 @@ GCP_request_mq_cancel (struct GCP_MessageQueueManager *mqm,
399 * @param mq message queue to set (can be NULL) 399 * @param mq message queue to set (can be NULL)
400 */ 400 */
401void 401void
402GCP_set_mq (struct CadetPeer *cp, 402GCP_set_mq(struct CadetPeer *cp,
403 struct GNUNET_MQ_Handle *mq); 403 struct GNUNET_MQ_Handle *mq);
404 404
405 405
406#endif 406#endif
diff --git a/src/cadet/gnunet-service-cadet_tunnels.c b/src/cadet/gnunet-service-cadet_tunnels.c
index 11be2bce0..c73f8cdbe 100644
--- a/src/cadet/gnunet-service-cadet_tunnels.c
+++ b/src/cadet/gnunet-service-cadet_tunnels.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file cadet/gnunet-service-cadet_tunnels.c 21 * @file cadet/gnunet-service-cadet_tunnels.c
22 * @brief Information we track per tunnel. 22 * @brief Information we track per tunnel.
@@ -42,7 +42,7 @@
42#include "gnunet-service-cadet_paths.h" 42#include "gnunet-service-cadet_paths.h"
43 43
44 44
45#define LOG(level, ...) GNUNET_log_from(level,"cadet-tun",__VA_ARGS__) 45#define LOG(level, ...) GNUNET_log_from(level, "cadet-tun", __VA_ARGS__)
46 46
47/** 47/**
48 * How often do we try to decrypt payload with unverified key 48 * How often do we try to decrypt payload with unverified key
@@ -77,8 +77,7 @@
77/** 77/**
78 * Struct to old keys for skipped messages while advancing the Axolotl ratchet. 78 * Struct to old keys for skipped messages while advancing the Axolotl ratchet.
79 */ 79 */
80struct CadetTunnelSkippedKey 80struct CadetTunnelSkippedKey {
81{
82 /** 81 /**
83 * DLL next. 82 * DLL next.
84 */ 83 */
@@ -114,8 +113,7 @@ struct CadetTunnelSkippedKey
114/** 113/**
115 * Axolotl data, according to https://github.com/trevp/axolotl/wiki . 114 * Axolotl data, according to https://github.com/trevp/axolotl/wiki .
116 */ 115 */
117struct CadetTunnelAxolotl 116struct CadetTunnelAxolotl {
118{
119 /** 117 /**
120 * A (double linked) list of stored message keys and associated header keys 118 * A (double linked) list of stored message keys and associated header keys
121 * for "skipped" messages, i.e. messages that have not been 119 * for "skipped" messages, i.e. messages that have not been
@@ -248,15 +246,13 @@ struct CadetTunnelAxolotl
248 * @e ratchet_allowed is #GNUNET_YES, we advance the ratchet. 246 * @e ratchet_allowed is #GNUNET_YES, we advance the ratchet.
249 */ 247 */
250 unsigned int ratchet_counter; 248 unsigned int ratchet_counter;
251
252}; 249};
253 250
254 251
255/** 252/**
256 * Struct used to save messages in a non-ready tunnel to send once connected. 253 * Struct used to save messages in a non-ready tunnel to send once connected.
257 */ 254 */
258struct CadetTunnelQueueEntry 255struct CadetTunnelQueueEntry {
259{
260 /** 256 /**
261 * We are entries in a DLL 257 * We are entries in a DLL
262 */ 258 */
@@ -298,8 +294,7 @@ struct CadetTunnelQueueEntry
298/** 294/**
299 * Struct containing all information regarding a tunnel to a peer. 295 * Struct containing all information regarding a tunnel to a peer.
300 */ 296 */
301struct CadetTunnel 297struct CadetTunnel {
302{
303 /** 298 /**
304 * Destination of the tunnel. 299 * Destination of the tunnel.
305 */ 300 */
@@ -454,7 +449,6 @@ struct CadetTunnel
454 * Force triggering KX_AUTH independent of @e estate. 449 * Force triggering KX_AUTH independent of @e estate.
455 */ 450 */
456 int kx_auth_requested; 451 int kx_auth_requested;
457
458}; 452};
459 453
460 454
@@ -465,19 +459,19 @@ struct CadetTunnel
465 * @return #GNUNET_YES for Alice, #GNUNET_NO for Betty, #GNUNET_SYSERR if talking to myself 459 * @return #GNUNET_YES for Alice, #GNUNET_NO for Betty, #GNUNET_SYSERR if talking to myself
466 */ 460 */
467static int 461static int
468alice_or_betty (const struct GNUNET_PeerIdentity *other) 462alice_or_betty(const struct GNUNET_PeerIdentity *other)
469{ 463{
470 if (0 > GNUNET_memcmp (&my_full_id, 464 if (0 > GNUNET_memcmp(&my_full_id,
471 other)) 465 other))
472 return GNUNET_YES; 466 return GNUNET_YES;
473 else if (0 < GNUNET_memcmp (&my_full_id, 467 else if (0 < GNUNET_memcmp(&my_full_id,
474 other)) 468 other))
475 return GNUNET_NO; 469 return GNUNET_NO;
476 else 470 else
477 { 471 {
478 GNUNET_break_op (0); 472 GNUNET_break_op(0);
479 return GNUNET_SYSERR; 473 return GNUNET_SYSERR;
480 } 474 }
481} 475}
482 476
483 477
@@ -488,20 +482,20 @@ alice_or_betty (const struct GNUNET_PeerIdentity *other)
488 * @param ct connection to move to unready status 482 * @param ct connection to move to unready status
489 */ 483 */
490static void 484static void
491mark_connection_unready (struct CadetTConnection *ct) 485mark_connection_unready(struct CadetTConnection *ct)
492{ 486{
493 struct CadetTunnel *t = ct->t; 487 struct CadetTunnel *t = ct->t;
494 488
495 GNUNET_assert (GNUNET_YES == ct->is_ready); 489 GNUNET_assert(GNUNET_YES == ct->is_ready);
496 GNUNET_CONTAINER_DLL_remove (t->connection_ready_head, 490 GNUNET_CONTAINER_DLL_remove(t->connection_ready_head,
497 t->connection_ready_tail, 491 t->connection_ready_tail,
498 ct); 492 ct);
499 GNUNET_assert (0 < t->num_ready_connections); 493 GNUNET_assert(0 < t->num_ready_connections);
500 t->num_ready_connections--; 494 t->num_ready_connections--;
501 ct->is_ready = GNUNET_NO; 495 ct->is_ready = GNUNET_NO;
502 GNUNET_CONTAINER_DLL_insert (t->connection_busy_head, 496 GNUNET_CONTAINER_DLL_insert(t->connection_busy_head,
503 t->connection_busy_tail, 497 t->connection_busy_tail,
504 ct); 498 ct);
505 t->num_busy_connections++; 499 t->num_busy_connections++;
506} 500}
507 501
@@ -514,16 +508,16 @@ mark_connection_unready (struct CadetTConnection *ct)
514 * @return Static string the destination peer's ID. 508 * @return Static string the destination peer's ID.
515 */ 509 */
516const char * 510const char *
517GCT_2s (const struct CadetTunnel *t) 511GCT_2s(const struct CadetTunnel *t)
518{ 512{
519 static char buf[64]; 513 static char buf[64];
520 514
521 if (NULL == t) 515 if (NULL == t)
522 return "Tunnel(NULL)"; 516 return "Tunnel(NULL)";
523 GNUNET_snprintf (buf, 517 GNUNET_snprintf(buf,
524 sizeof (buf), 518 sizeof(buf),
525 "Tunnel %s", 519 "Tunnel %s",
526 GNUNET_i2s (GCP_get_id (t->destination))); 520 GNUNET_i2s(GCP_get_id(t->destination)));
527 return buf; 521 return buf;
528} 522}
529 523
@@ -536,31 +530,37 @@ GCT_2s (const struct CadetTunnel *t)
536 * @return String representation. 530 * @return String representation.
537 */ 531 */
538static const char * 532static const char *
539estate2s (enum CadetTunnelEState es) 533estate2s(enum CadetTunnelEState es)
540{ 534{
541 static char buf[32]; 535 static char buf[32];
542 536
543 switch (es) 537 switch (es)
544 { 538 {
545 case CADET_TUNNEL_KEY_UNINITIALIZED: 539 case CADET_TUNNEL_KEY_UNINITIALIZED:
546 return "CADET_TUNNEL_KEY_UNINITIALIZED"; 540 return "CADET_TUNNEL_KEY_UNINITIALIZED";
547 case CADET_TUNNEL_KEY_AX_RECV: 541
548 return "CADET_TUNNEL_KEY_AX_RECV"; 542 case CADET_TUNNEL_KEY_AX_RECV:
549 case CADET_TUNNEL_KEY_AX_SENT: 543 return "CADET_TUNNEL_KEY_AX_RECV";
550 return "CADET_TUNNEL_KEY_AX_SENT"; 544
551 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV: 545 case CADET_TUNNEL_KEY_AX_SENT:
552 return "CADET_TUNNEL_KEY_AX_SENT_AND_RECV"; 546 return "CADET_TUNNEL_KEY_AX_SENT";
553 case CADET_TUNNEL_KEY_AX_AUTH_SENT: 547
554 return "CADET_TUNNEL_KEY_AX_AUTH_SENT"; 548 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV:
555 case CADET_TUNNEL_KEY_OK: 549 return "CADET_TUNNEL_KEY_AX_SENT_AND_RECV";
556 return "CADET_TUNNEL_KEY_OK"; 550
557 default: 551 case CADET_TUNNEL_KEY_AX_AUTH_SENT:
558 GNUNET_snprintf (buf, 552 return "CADET_TUNNEL_KEY_AX_AUTH_SENT";
559 sizeof (buf), 553
560 "%u (UNKNOWN STATE)", 554 case CADET_TUNNEL_KEY_OK:
561 es); 555 return "CADET_TUNNEL_KEY_OK";
562 return buf; 556
563 } 557 default:
558 GNUNET_snprintf(buf,
559 sizeof(buf),
560 "%u (UNKNOWN STATE)",
561 es);
562 return buf;
563 }
564} 564}
565 565
566 566
@@ -571,7 +571,7 @@ estate2s (enum CadetTunnelEState es)
571 * @return the destination of the tunnel 571 * @return the destination of the tunnel
572 */ 572 */
573struct CadetPeer * 573struct CadetPeer *
574GCT_get_destination (struct CadetTunnel *t) 574GCT_get_destination(struct CadetTunnel *t)
575{ 575{
576 return t->destination; 576 return t->destination;
577} 577}
@@ -585,9 +585,9 @@ GCT_get_destination (struct CadetTunnel *t)
585 * @return Number of channels. 585 * @return Number of channels.
586 */ 586 */
587unsigned int 587unsigned int
588GCT_count_channels (struct CadetTunnel *t) 588GCT_count_channels(struct CadetTunnel *t)
589{ 589{
590 return GNUNET_CONTAINER_multihashmap32_size (t->channels); 590 return GNUNET_CONTAINER_multihashmap32_size(t->channels);
591} 591}
592 592
593 593
@@ -599,11 +599,11 @@ GCT_count_channels (struct CadetTunnel *t)
599 * @return NULL if channel does not exist 599 * @return NULL if channel does not exist
600 */ 600 */
601struct CadetChannel * 601struct CadetChannel *
602lookup_channel (struct CadetTunnel *t, 602lookup_channel(struct CadetTunnel *t,
603 struct GNUNET_CADET_ChannelTunnelNumber ctn) 603 struct GNUNET_CADET_ChannelTunnelNumber ctn)
604{ 604{
605 return GNUNET_CONTAINER_multihashmap32_get (t->channels, 605 return GNUNET_CONTAINER_multihashmap32_get(t->channels,
606 ntohl (ctn.cn)); 606 ntohl(ctn.cn));
607} 607}
608 608
609 609
@@ -615,7 +615,7 @@ lookup_channel (struct CadetTunnel *t,
615 * @return Number of connections created, either being established or ready. 615 * @return Number of connections created, either being established or ready.
616 */ 616 */
617unsigned int 617unsigned int
618GCT_count_any_connections (const struct CadetTunnel *t) 618GCT_count_any_connections(const struct CadetTunnel *t)
619{ 619{
620 return t->num_ready_connections + t->num_busy_connections; 620 return t->num_ready_connections + t->num_busy_connections;
621} 621}
@@ -629,12 +629,12 @@ GCT_count_any_connections (const struct CadetTunnel *t)
629 * @return NULL if we have no connection that is ready 629 * @return NULL if we have no connection that is ready
630 */ 630 */
631static struct CadetTConnection * 631static struct CadetTConnection *
632get_ready_connection (struct CadetTunnel *t) 632get_ready_connection(struct CadetTunnel *t)
633{ 633{
634 struct CadetTConnection *hd = t->connection_ready_head; 634 struct CadetTConnection *hd = t->connection_ready_head;
635 635
636 GNUNET_assert ( (NULL == hd) || 636 GNUNET_assert((NULL == hd) ||
637 (GNUNET_YES == hd->is_ready) ); 637 (GNUNET_YES == hd->is_ready));
638 return hd; 638 return hd;
639} 639}
640 640
@@ -647,7 +647,7 @@ get_ready_connection (struct CadetTunnel *t)
647 * @return Tunnel's encryption state. 647 * @return Tunnel's encryption state.
648 */ 648 */
649enum CadetTunnelEState 649enum CadetTunnelEState
650GCT_get_estate (struct CadetTunnel *t) 650GCT_get_estate(struct CadetTunnel *t)
651{ 651{
652 return t->estate; 652 return t->estate;
653} 653}
@@ -662,7 +662,7 @@ GCT_get_estate (struct CadetTunnel *t)
662 * @param cls the `struct CadetTunnel` to process messages on 662 * @param cls the `struct CadetTunnel` to process messages on
663 */ 663 */
664static void 664static void
665trigger_transmissions (void *cls); 665trigger_transmissions(void *cls);
666 666
667 667
668/* ************************************** start core crypto ***************************** */ 668/* ************************************** start core crypto ***************************** */
@@ -674,12 +674,12 @@ trigger_transmissions (void *cls);
674 * @param ax key material to update 674 * @param ax key material to update
675 */ 675 */
676static void 676static void
677new_ephemeral (struct CadetTunnelAxolotl *ax) 677new_ephemeral(struct CadetTunnelAxolotl *ax)
678{ 678{
679 LOG (GNUNET_ERROR_TYPE_DEBUG, 679 LOG(GNUNET_ERROR_TYPE_DEBUG,
680 "Creating new ephemeral ratchet key (DHRs)\n"); 680 "Creating new ephemeral ratchet key (DHRs)\n");
681 GNUNET_assert (GNUNET_OK == 681 GNUNET_assert(GNUNET_OK ==
682 GNUNET_CRYPTO_ecdhe_key_create2 (&ax->DHRs)); 682 GNUNET_CRYPTO_ecdhe_key_create2(&ax->DHRs));
683} 683}
684 684
685 685
@@ -693,31 +693,31 @@ new_ephemeral (struct CadetTunnelAxolotl *ax)
693 * @param hmac[out] Destination to store the HMAC. 693 * @param hmac[out] Destination to store the HMAC.
694 */ 694 */
695static void 695static void
696t_hmac (const void *plaintext, 696t_hmac(const void *plaintext,
697 size_t size, 697 size_t size,
698 uint32_t iv, 698 uint32_t iv,
699 const struct GNUNET_CRYPTO_SymmetricSessionKey *key, 699 const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
700 struct GNUNET_ShortHashCode *hmac) 700 struct GNUNET_ShortHashCode *hmac)
701{ 701{
702 static const char ctx[] = "cadet authentication key"; 702 static const char ctx[] = "cadet authentication key";
703 struct GNUNET_CRYPTO_AuthKey auth_key; 703 struct GNUNET_CRYPTO_AuthKey auth_key;
704 struct GNUNET_HashCode hash; 704 struct GNUNET_HashCode hash;
705 705
706 GNUNET_CRYPTO_hmac_derive_key (&auth_key, 706 GNUNET_CRYPTO_hmac_derive_key(&auth_key,
707 key, 707 key,
708 &iv, sizeof (iv), 708 &iv, sizeof(iv),
709 key, sizeof (*key), 709 key, sizeof(*key),
710 ctx, sizeof (ctx), 710 ctx, sizeof(ctx),
711 NULL); 711 NULL);
712 /* Two step: GNUNET_ShortHash is only 256 bits, 712 /* Two step: GNUNET_ShortHash is only 256 bits,
713 GNUNET_HashCode is 512, so we truncate. */ 713 GNUNET_HashCode is 512, so we truncate. */
714 GNUNET_CRYPTO_hmac (&auth_key, 714 GNUNET_CRYPTO_hmac(&auth_key,
715 plaintext, 715 plaintext,
716 size, 716 size,
717 &hash); 717 &hash);
718 GNUNET_memcpy (hmac, 718 GNUNET_memcpy(hmac,
719 &hash, 719 &hash,
720 sizeof (*hmac)); 720 sizeof(*hmac));
721} 721}
722 722
723 723
@@ -730,22 +730,22 @@ t_hmac (const void *plaintext,
730 * @param len Length of @a source. 730 * @param len Length of @a source.
731 */ 731 */
732static void 732static void
733t_ax_hmac_hash (const struct GNUNET_CRYPTO_SymmetricSessionKey *key, 733t_ax_hmac_hash(const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
734 struct GNUNET_HashCode *hash, 734 struct GNUNET_HashCode *hash,
735 const void *source, 735 const void *source,
736 unsigned int len) 736 unsigned int len)
737{ 737{
738 static const char ctx[] = "axolotl HMAC-HASH"; 738 static const char ctx[] = "axolotl HMAC-HASH";
739 struct GNUNET_CRYPTO_AuthKey auth_key; 739 struct GNUNET_CRYPTO_AuthKey auth_key;
740 740
741 GNUNET_CRYPTO_hmac_derive_key (&auth_key, 741 GNUNET_CRYPTO_hmac_derive_key(&auth_key,
742 key, 742 key,
743 ctx, sizeof (ctx), 743 ctx, sizeof(ctx),
744 NULL); 744 NULL);
745 GNUNET_CRYPTO_hmac (&auth_key, 745 GNUNET_CRYPTO_hmac(&auth_key,
746 source, 746 source,
747 len, 747 len,
748 hash); 748 hash);
749} 749}
750 750
751 751
@@ -758,22 +758,22 @@ t_ax_hmac_hash (const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
758 * @param len Length of @a source. 758 * @param len Length of @a source.
759 */ 759 */
760static void 760static void
761t_hmac_derive_key (const struct GNUNET_CRYPTO_SymmetricSessionKey *key, 761t_hmac_derive_key(const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
762 struct GNUNET_CRYPTO_SymmetricSessionKey *out, 762 struct GNUNET_CRYPTO_SymmetricSessionKey *out,
763 const void *source, 763 const void *source,
764 unsigned int len) 764 unsigned int len)
765{ 765{
766 static const char ctx[] = "axolotl derive key"; 766 static const char ctx[] = "axolotl derive key";
767 struct GNUNET_HashCode h; 767 struct GNUNET_HashCode h;
768 768
769 t_ax_hmac_hash (key, 769 t_ax_hmac_hash(key,
770 &h, 770 &h,
771 source, 771 source,
772 len); 772 len);
773 GNUNET_CRYPTO_kdf (out, sizeof (*out), 773 GNUNET_CRYPTO_kdf(out, sizeof(*out),
774 ctx, sizeof (ctx), 774 ctx, sizeof(ctx),
775 &h, sizeof (h), 775 &h, sizeof(h),
776 NULL); 776 NULL);
777} 777}
778 778
779 779
@@ -786,78 +786,78 @@ t_hmac_derive_key (const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
786 * @param size Size of the buffers at @a src and @a dst 786 * @param size Size of the buffers at @a src and @a dst
787 */ 787 */
788static void 788static void
789t_ax_encrypt (struct CadetTunnelAxolotl *ax, 789t_ax_encrypt(struct CadetTunnelAxolotl *ax,
790 void *dst, 790 void *dst,
791 const void *src, 791 const void *src,
792 size_t size) 792 size_t size)
793{ 793{
794 struct GNUNET_CRYPTO_SymmetricSessionKey MK; 794 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
795 struct GNUNET_CRYPTO_SymmetricInitializationVector iv; 795 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
796 size_t out_size; 796 size_t out_size;
797 797
798 ax->ratchet_counter++; 798 ax->ratchet_counter++;
799 if ( (GNUNET_YES == ax->ratchet_allowed) && 799 if ((GNUNET_YES == ax->ratchet_allowed) &&
800 ( (ratchet_messages <= ax->ratchet_counter) || 800 ((ratchet_messages <= ax->ratchet_counter) ||
801 (0 == GNUNET_TIME_absolute_get_remaining (ax->ratchet_expiration).rel_value_us)) ) 801 (0 == GNUNET_TIME_absolute_get_remaining(ax->ratchet_expiration).rel_value_us)))
802 { 802 {
803 ax->ratchet_flag = GNUNET_YES; 803 ax->ratchet_flag = GNUNET_YES;
804 } 804 }
805 if (GNUNET_YES == ax->ratchet_flag) 805 if (GNUNET_YES == ax->ratchet_flag)
806 { 806 {
807 /* Advance ratchet */ 807 /* Advance ratchet */
808 struct GNUNET_CRYPTO_SymmetricSessionKey keys[3]; 808 struct GNUNET_CRYPTO_SymmetricSessionKey keys[3];
809 struct GNUNET_HashCode dh; 809 struct GNUNET_HashCode dh;
810 struct GNUNET_HashCode hmac; 810 struct GNUNET_HashCode hmac;
811 static const char ctx[] = "axolotl ratchet"; 811 static const char ctx[] = "axolotl ratchet";
812 812
813 new_ephemeral (ax); 813 new_ephemeral(ax);
814 ax->HKs = ax->NHKs; 814 ax->HKs = ax->NHKs;
815 815
816 /* RK, NHKs, CKs = KDF( HMAC-HASH(RK, DH(DHRs, DHRr)) ) */ 816 /* RK, NHKs, CKs = KDF( HMAC-HASH(RK, DH(DHRs, DHRr)) ) */
817 GNUNET_CRYPTO_ecc_ecdh (&ax->DHRs, 817 GNUNET_CRYPTO_ecc_ecdh(&ax->DHRs,
818 &ax->DHRr, 818 &ax->DHRr,
819 &dh); 819 &dh);
820 t_ax_hmac_hash (&ax->RK, 820 t_ax_hmac_hash(&ax->RK,
821 &hmac, 821 &hmac,
822 &dh, 822 &dh,
823 sizeof (dh)); 823 sizeof(dh));
824 GNUNET_CRYPTO_kdf (keys, sizeof (keys), 824 GNUNET_CRYPTO_kdf(keys, sizeof(keys),
825 ctx, sizeof (ctx), 825 ctx, sizeof(ctx),
826 &hmac, sizeof (hmac), 826 &hmac, sizeof(hmac),
827 NULL); 827 NULL);
828 ax->RK = keys[0]; 828 ax->RK = keys[0];
829 ax->NHKs = keys[1]; 829 ax->NHKs = keys[1];
830 ax->CKs = keys[2]; 830 ax->CKs = keys[2];
831 831
832 ax->PNs = ax->Ns; 832 ax->PNs = ax->Ns;
833 ax->Ns = 0; 833 ax->Ns = 0;
834 ax->ratchet_flag = GNUNET_NO; 834 ax->ratchet_flag = GNUNET_NO;
835 ax->ratchet_allowed = GNUNET_NO; 835 ax->ratchet_allowed = GNUNET_NO;
836 ax->ratchet_counter = 0; 836 ax->ratchet_counter = 0;
837 ax->ratchet_expiration 837 ax->ratchet_expiration
838 = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), 838 = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(),
839 ratchet_time); 839 ratchet_time);
840 } 840 }
841 841
842 t_hmac_derive_key (&ax->CKs, 842 t_hmac_derive_key(&ax->CKs,
843 &MK, 843 &MK,
844 "0", 844 "0",
845 1); 845 1);
846 GNUNET_CRYPTO_symmetric_derive_iv (&iv, 846 GNUNET_CRYPTO_symmetric_derive_iv(&iv,
847 &MK, 847 &MK,
848 NULL, 0, 848 NULL, 0,
849 NULL); 849 NULL);
850 850
851 out_size = GNUNET_CRYPTO_symmetric_encrypt (src, 851 out_size = GNUNET_CRYPTO_symmetric_encrypt(src,
852 size, 852 size,
853 &MK, 853 &MK,
854 &iv, 854 &iv,
855 dst); 855 dst);
856 GNUNET_assert (size == out_size); 856 GNUNET_assert(size == out_size);
857 t_hmac_derive_key (&ax->CKs, 857 t_hmac_derive_key(&ax->CKs,
858 &ax->CKs, 858 &ax->CKs,
859 "1", 859 "1",
860 1); 860 1);
861} 861}
862 862
863 863
@@ -870,34 +870,34 @@ t_ax_encrypt (struct CadetTunnelAxolotl *ax,
870 * @param size Size of the @a src and @a dst buffers 870 * @param size Size of the @a src and @a dst buffers
871 */ 871 */
872static void 872static void
873t_ax_decrypt (struct CadetTunnelAxolotl *ax, 873t_ax_decrypt(struct CadetTunnelAxolotl *ax,
874 void *dst, 874 void *dst,
875 const void *src, 875 const void *src,
876 size_t size) 876 size_t size)
877{ 877{
878 struct GNUNET_CRYPTO_SymmetricSessionKey MK; 878 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
879 struct GNUNET_CRYPTO_SymmetricInitializationVector iv; 879 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
880 size_t out_size; 880 size_t out_size;
881 881
882 t_hmac_derive_key (&ax->CKr, 882 t_hmac_derive_key(&ax->CKr,
883 &MK, 883 &MK,
884 "0", 884 "0",
885 1); 885 1);
886 GNUNET_CRYPTO_symmetric_derive_iv (&iv, 886 GNUNET_CRYPTO_symmetric_derive_iv(&iv,
887 &MK, 887 &MK,
888 NULL, 0, 888 NULL, 0,
889 NULL); 889 NULL);
890 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader)); 890 GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
891 out_size = GNUNET_CRYPTO_symmetric_decrypt (src, 891 out_size = GNUNET_CRYPTO_symmetric_decrypt(src,
892 size, 892 size,
893 &MK, 893 &MK,
894 &iv, 894 &iv,
895 dst); 895 dst);
896 GNUNET_assert (out_size == size); 896 GNUNET_assert(out_size == size);
897 t_hmac_derive_key (&ax->CKr, 897 t_hmac_derive_key(&ax->CKr,
898 &ax->CKr, 898 &ax->CKr,
899 "1", 899 "1",
900 1); 900 1);
901} 901}
902 902
903 903
@@ -908,22 +908,22 @@ t_ax_decrypt (struct CadetTunnelAxolotl *ax,
908 * @param[in|out] msg Message whose header to encrypt. 908 * @param[in|out] msg Message whose header to encrypt.
909 */ 909 */
910static void 910static void
911t_h_encrypt (struct CadetTunnelAxolotl *ax, 911t_h_encrypt(struct CadetTunnelAxolotl *ax,
912 struct GNUNET_CADET_TunnelEncryptedMessage *msg) 912 struct GNUNET_CADET_TunnelEncryptedMessage *msg)
913{ 913{
914 struct GNUNET_CRYPTO_SymmetricInitializationVector iv; 914 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
915 size_t out_size; 915 size_t out_size;
916 916
917 GNUNET_CRYPTO_symmetric_derive_iv (&iv, 917 GNUNET_CRYPTO_symmetric_derive_iv(&iv,
918 &ax->HKs, 918 &ax->HKs,
919 NULL, 0, 919 NULL, 0,
920 NULL); 920 NULL);
921 out_size = GNUNET_CRYPTO_symmetric_encrypt (&msg->ax_header, 921 out_size = GNUNET_CRYPTO_symmetric_encrypt(&msg->ax_header,
922 sizeof (struct GNUNET_CADET_AxHeader), 922 sizeof(struct GNUNET_CADET_AxHeader),
923 &ax->HKs, 923 &ax->HKs,
924 &iv, 924 &iv,
925 &msg->ax_header); 925 &msg->ax_header);
926 GNUNET_assert (sizeof (struct GNUNET_CADET_AxHeader) == out_size); 926 GNUNET_assert(sizeof(struct GNUNET_CADET_AxHeader) == out_size);
927} 927}
928 928
929 929
@@ -935,23 +935,23 @@ t_h_encrypt (struct CadetTunnelAxolotl *ax,
935 * @param dst Where to decrypt header to. 935 * @param dst Where to decrypt header to.
936 */ 936 */
937static void 937static void
938t_h_decrypt (struct CadetTunnelAxolotl *ax, 938t_h_decrypt(struct CadetTunnelAxolotl *ax,
939 const struct GNUNET_CADET_TunnelEncryptedMessage *src, 939 const struct GNUNET_CADET_TunnelEncryptedMessage *src,
940 struct GNUNET_CADET_TunnelEncryptedMessage *dst) 940 struct GNUNET_CADET_TunnelEncryptedMessage *dst)
941{ 941{
942 struct GNUNET_CRYPTO_SymmetricInitializationVector iv; 942 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
943 size_t out_size; 943 size_t out_size;
944 944
945 GNUNET_CRYPTO_symmetric_derive_iv (&iv, 945 GNUNET_CRYPTO_symmetric_derive_iv(&iv,
946 &ax->HKr, 946 &ax->HKr,
947 NULL, 0, 947 NULL, 0,
948 NULL); 948 NULL);
949 out_size = GNUNET_CRYPTO_symmetric_decrypt (&src->ax_header.Ns, 949 out_size = GNUNET_CRYPTO_symmetric_decrypt(&src->ax_header.Ns,
950 sizeof (struct GNUNET_CADET_AxHeader), 950 sizeof(struct GNUNET_CADET_AxHeader),
951 &ax->HKr, 951 &ax->HKr,
952 &iv, 952 &iv,
953 &dst->ax_header.Ns); 953 &dst->ax_header.Ns);
954 GNUNET_assert (sizeof (struct GNUNET_CADET_AxHeader) == out_size); 954 GNUNET_assert(sizeof(struct GNUNET_CADET_AxHeader) == out_size);
955} 955}
956 956
957 957
@@ -962,13 +962,13 @@ t_h_decrypt (struct CadetTunnelAxolotl *ax,
962 * @param key Key to delete. 962 * @param key Key to delete.
963 */ 963 */
964static void 964static void
965delete_skipped_key (struct CadetTunnelAxolotl *ax, 965delete_skipped_key(struct CadetTunnelAxolotl *ax,
966 struct CadetTunnelSkippedKey *key) 966 struct CadetTunnelSkippedKey *key)
967{ 967{
968 GNUNET_CONTAINER_DLL_remove (ax->skipped_head, 968 GNUNET_CONTAINER_DLL_remove(ax->skipped_head,
969 ax->skipped_tail, 969 ax->skipped_tail,
970 key); 970 key);
971 GNUNET_free (key); 971 GNUNET_free(key);
972 ax->skipped--; 972 ax->skipped--;
973} 973}
974 974
@@ -984,10 +984,10 @@ delete_skipped_key (struct CadetTunnelAxolotl *ax,
984 * @return Size of the decrypted data, -1 if an error was encountered. 984 * @return Size of the decrypted data, -1 if an error was encountered.
985 */ 985 */
986static ssize_t 986static ssize_t
987try_old_ax_keys (struct CadetTunnelAxolotl *ax, 987try_old_ax_keys(struct CadetTunnelAxolotl *ax,
988 void *dst, 988 void *dst,
989 const struct GNUNET_CADET_TunnelEncryptedMessage *src, 989 const struct GNUNET_CADET_TunnelEncryptedMessage *src,
990 size_t size) 990 size_t size)
991{ 991{
992 struct CadetTunnelSkippedKey *key; 992 struct CadetTunnelSkippedKey *key;
993 struct GNUNET_ShortHashCode *hmac; 993 struct GNUNET_ShortHashCode *hmac;
@@ -999,70 +999,70 @@ try_old_ax_keys (struct CadetTunnelAxolotl *ax,
999 size_t len; 999 size_t len;
1000 unsigned int N; 1000 unsigned int N;
1001 1001
1002 LOG (GNUNET_ERROR_TYPE_DEBUG, 1002 LOG(GNUNET_ERROR_TYPE_DEBUG,
1003 "Trying skipped keys\n"); 1003 "Trying skipped keys\n");
1004 hmac = &plaintext_header.hmac; 1004 hmac = &plaintext_header.hmac;
1005 esize = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage); 1005 esize = size - sizeof(struct GNUNET_CADET_TunnelEncryptedMessage);
1006 1006
1007 /* Find a correct Header Key */ 1007 /* Find a correct Header Key */
1008 valid_HK = NULL; 1008 valid_HK = NULL;
1009 for (key = ax->skipped_head; NULL != key; key = key->next) 1009 for (key = ax->skipped_head; NULL != key; key = key->next)
1010 { 1010 {
1011 t_hmac (&src->ax_header, 1011 t_hmac(&src->ax_header,
1012 sizeof (struct GNUNET_CADET_AxHeader) + esize, 1012 sizeof(struct GNUNET_CADET_AxHeader) + esize,
1013 0, 1013 0,
1014 &key->HK, 1014 &key->HK,
1015 hmac); 1015 hmac);
1016 if (0 == GNUNET_memcmp (hmac, 1016 if (0 == GNUNET_memcmp(hmac,
1017 &src->hmac)) 1017 &src->hmac))
1018 { 1018 {
1019 valid_HK = &key->HK; 1019 valid_HK = &key->HK;
1020 break; 1020 break;
1021 }
1021 } 1022 }
1022 }
1023 if (NULL == key) 1023 if (NULL == key)
1024 return -1; 1024 return -1;
1025 1025
1026 /* Should've been checked in -cadet_connection.c handle_cadet_encrypted. */ 1026 /* Should've been checked in -cadet_connection.c handle_cadet_encrypted. */
1027 GNUNET_assert (size > sizeof (struct GNUNET_CADET_TunnelEncryptedMessage)); 1027 GNUNET_assert(size > sizeof(struct GNUNET_CADET_TunnelEncryptedMessage));
1028 len = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage); 1028 len = size - sizeof(struct GNUNET_CADET_TunnelEncryptedMessage);
1029 GNUNET_assert (len >= sizeof (struct GNUNET_MessageHeader)); 1029 GNUNET_assert(len >= sizeof(struct GNUNET_MessageHeader));
1030 1030
1031 /* Decrypt header */ 1031 /* Decrypt header */
1032 GNUNET_CRYPTO_symmetric_derive_iv (&iv, 1032 GNUNET_CRYPTO_symmetric_derive_iv(&iv,
1033 &key->HK, 1033 &key->HK,
1034 NULL, 0, 1034 NULL, 0,
1035 NULL); 1035 NULL);
1036 res = GNUNET_CRYPTO_symmetric_decrypt (&src->ax_header.Ns, 1036 res = GNUNET_CRYPTO_symmetric_decrypt(&src->ax_header.Ns,
1037 sizeof (struct GNUNET_CADET_AxHeader), 1037 sizeof(struct GNUNET_CADET_AxHeader),
1038 &key->HK, 1038 &key->HK,
1039 &iv, 1039 &iv,
1040 &plaintext_header.ax_header.Ns); 1040 &plaintext_header.ax_header.Ns);
1041 GNUNET_assert (sizeof (struct GNUNET_CADET_AxHeader) == res); 1041 GNUNET_assert(sizeof(struct GNUNET_CADET_AxHeader) == res);
1042 1042
1043 /* Find the correct message key */ 1043 /* Find the correct message key */
1044 N = ntohl (plaintext_header.ax_header.Ns); 1044 N = ntohl(plaintext_header.ax_header.Ns);
1045 while ( (NULL != key) && 1045 while ((NULL != key) &&
1046 (N != key->Kn) ) 1046 (N != key->Kn))
1047 key = key->next; 1047 key = key->next;
1048 if ( (NULL == key) || 1048 if ((NULL == key) ||
1049 (0 != GNUNET_memcmp (&key->HK, 1049 (0 != GNUNET_memcmp(&key->HK,
1050 valid_HK)) ) 1050 valid_HK)))
1051 return -1; 1051 return -1;
1052 1052
1053 /* Decrypt payload */ 1053 /* Decrypt payload */
1054 GNUNET_CRYPTO_symmetric_derive_iv (&iv, 1054 GNUNET_CRYPTO_symmetric_derive_iv(&iv,
1055 &key->MK, 1055 &key->MK,
1056 NULL, 1056 NULL,
1057 0, 1057 0,
1058 NULL); 1058 NULL);
1059 res = GNUNET_CRYPTO_symmetric_decrypt (&src[1], 1059 res = GNUNET_CRYPTO_symmetric_decrypt(&src[1],
1060 len, 1060 len,
1061 &key->MK, 1061 &key->MK,
1062 &iv, 1062 &iv,
1063 dst); 1063 dst);
1064 delete_skipped_key (ax, 1064 delete_skipped_key(ax,
1065 key); 1065 key);
1066 return res; 1066 return res;
1067} 1067}
1068 1068
@@ -1074,26 +1074,26 @@ try_old_ax_keys (struct CadetTunnelAxolotl *ax,
1074 * @param HKr Header Key to use. 1074 * @param HKr Header Key to use.
1075 */ 1075 */
1076static void 1076static void
1077store_skipped_key (struct CadetTunnelAxolotl *ax, 1077store_skipped_key(struct CadetTunnelAxolotl *ax,
1078 const struct GNUNET_CRYPTO_SymmetricSessionKey *HKr) 1078 const struct GNUNET_CRYPTO_SymmetricSessionKey *HKr)
1079{ 1079{
1080 struct CadetTunnelSkippedKey *key; 1080 struct CadetTunnelSkippedKey *key;
1081 1081
1082 key = GNUNET_new (struct CadetTunnelSkippedKey); 1082 key = GNUNET_new(struct CadetTunnelSkippedKey);
1083 key->timestamp = GNUNET_TIME_absolute_get (); 1083 key->timestamp = GNUNET_TIME_absolute_get();
1084 key->Kn = ax->Nr; 1084 key->Kn = ax->Nr;
1085 key->HK = ax->HKr; 1085 key->HK = ax->HKr;
1086 t_hmac_derive_key (&ax->CKr, 1086 t_hmac_derive_key(&ax->CKr,
1087 &key->MK, 1087 &key->MK,
1088 "0", 1088 "0",
1089 1); 1089 1);
1090 t_hmac_derive_key (&ax->CKr, 1090 t_hmac_derive_key(&ax->CKr,
1091 &ax->CKr, 1091 &ax->CKr,
1092 "1", 1092 "1",
1093 1); 1093 1);
1094 GNUNET_CONTAINER_DLL_insert (ax->skipped_head, 1094 GNUNET_CONTAINER_DLL_insert(ax->skipped_head,
1095 ax->skipped_tail, 1095 ax->skipped_tail,
1096 key); 1096 key);
1097 ax->skipped++; 1097 ax->skipped++;
1098 ax->Nr++; 1098 ax->Nr++;
1099} 1099}
@@ -1110,41 +1110,41 @@ store_skipped_key (struct CadetTunnelAxolotl *ax,
1110 * #GNUNET_SYSERR if an error ocurred (@a Np not expected). 1110 * #GNUNET_SYSERR if an error ocurred (@a Np not expected).
1111 */ 1111 */
1112static int 1112static int
1113store_ax_keys (struct CadetTunnelAxolotl *ax, 1113store_ax_keys(struct CadetTunnelAxolotl *ax,
1114 const struct GNUNET_CRYPTO_SymmetricSessionKey *HKr, 1114 const struct GNUNET_CRYPTO_SymmetricSessionKey *HKr,
1115 uint32_t Np) 1115 uint32_t Np)
1116{ 1116{
1117 int gap; 1117 int gap;
1118 1118
1119 gap = Np - ax->Nr; 1119 gap = Np - ax->Nr;
1120 LOG (GNUNET_ERROR_TYPE_DEBUG, 1120 LOG(GNUNET_ERROR_TYPE_DEBUG,
1121 "Storing skipped keys [%u, %u)\n", 1121 "Storing skipped keys [%u, %u)\n",
1122 ax->Nr, 1122 ax->Nr,
1123 Np); 1123 Np);
1124 if (MAX_KEY_GAP < gap) 1124 if (MAX_KEY_GAP < gap)
1125 { 1125 {
1126 /* Avoid DoS (forcing peer to do more than #MAX_KEY_GAP HMAC operations) */ 1126 /* Avoid DoS (forcing peer to do more than #MAX_KEY_GAP HMAC operations) */
1127 /* TODO: start new key exchange on return */ 1127 /* TODO: start new key exchange on return */
1128 GNUNET_break_op (0); 1128 GNUNET_break_op(0);
1129 LOG (GNUNET_ERROR_TYPE_WARNING, 1129 LOG(GNUNET_ERROR_TYPE_WARNING,
1130 "Got message %u, expected %u+\n", 1130 "Got message %u, expected %u+\n",
1131 Np, 1131 Np,
1132 ax->Nr); 1132 ax->Nr);
1133 return GNUNET_SYSERR; 1133 return GNUNET_SYSERR;
1134 } 1134 }
1135 if (0 > gap) 1135 if (0 > gap)
1136 { 1136 {
1137 /* Delayed message: don't store keys, flag to try old keys. */ 1137 /* Delayed message: don't store keys, flag to try old keys. */
1138 return GNUNET_SYSERR; 1138 return GNUNET_SYSERR;
1139 } 1139 }
1140 1140
1141 while (ax->Nr < Np) 1141 while (ax->Nr < Np)
1142 store_skipped_key (ax, 1142 store_skipped_key(ax,
1143 HKr); 1143 HKr);
1144 1144
1145 while (ax->skipped > MAX_SKIPPED_KEYS) 1145 while (ax->skipped > MAX_SKIPPED_KEYS)
1146 delete_skipped_key (ax, 1146 delete_skipped_key(ax,
1147 ax->skipped_tail); 1147 ax->skipped_tail);
1148 return GNUNET_OK; 1148 return GNUNET_OK;
1149} 1149}
1150 1150
@@ -1160,10 +1160,10 @@ store_ax_keys (struct CadetTunnelAxolotl *ax,
1160 * @return Size of the decrypted data, -1 if an error was encountered. 1160 * @return Size of the decrypted data, -1 if an error was encountered.
1161 */ 1161 */
1162static ssize_t 1162static ssize_t
1163t_ax_decrypt_and_validate (struct CadetTunnelAxolotl *ax, 1163t_ax_decrypt_and_validate(struct CadetTunnelAxolotl *ax,
1164 void *dst, 1164 void *dst,
1165 const struct GNUNET_CADET_TunnelEncryptedMessage *src, 1165 const struct GNUNET_CADET_TunnelEncryptedMessage *src,
1166 size_t size) 1166 size_t size)
1167{ 1167{
1168 struct GNUNET_ShortHashCode msg_hmac; 1168 struct GNUNET_ShortHashCode msg_hmac;
1169 struct GNUNET_HashCode hmac; 1169 struct GNUNET_HashCode hmac;
@@ -1172,93 +1172,93 @@ t_ax_decrypt_and_validate (struct CadetTunnelAxolotl *ax,
1172 uint32_t PNp; 1172 uint32_t PNp;
1173 size_t esize; /* Size of encryped payload */ 1173 size_t esize; /* Size of encryped payload */
1174 1174
1175 esize = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage); 1175 esize = size - sizeof(struct GNUNET_CADET_TunnelEncryptedMessage);
1176 1176
1177 /* Try current HK */ 1177 /* Try current HK */
1178 t_hmac (&src->ax_header, 1178 t_hmac(&src->ax_header,
1179 sizeof (struct GNUNET_CADET_AxHeader) + esize, 1179 sizeof(struct GNUNET_CADET_AxHeader) + esize,
1180 0, &ax->HKr, 1180 0, &ax->HKr,
1181 &msg_hmac); 1181 &msg_hmac);
1182 if (0 != GNUNET_memcmp (&msg_hmac, 1182 if (0 != GNUNET_memcmp(&msg_hmac,
1183 &src->hmac)) 1183 &src->hmac))
1184 {
1185 static const char ctx[] = "axolotl ratchet";
1186 struct GNUNET_CRYPTO_SymmetricSessionKey keys[3]; /* RKp, NHKp, CKp */
1187 struct GNUNET_CRYPTO_SymmetricSessionKey HK;
1188 struct GNUNET_HashCode dh;
1189 struct GNUNET_CRYPTO_EcdhePublicKey *DHRp;
1190
1191 /* Try Next HK */
1192 t_hmac (&src->ax_header,
1193 sizeof (struct GNUNET_CADET_AxHeader) + esize,
1194 0,
1195 &ax->NHKr,
1196 &msg_hmac);
1197 if (0 != GNUNET_memcmp (&msg_hmac,
1198 &src->hmac))
1199 { 1184 {
1200 /* Try the skipped keys, if that fails, we're out of luck. */ 1185 static const char ctx[] = "axolotl ratchet";
1201 return try_old_ax_keys (ax, 1186 struct GNUNET_CRYPTO_SymmetricSessionKey keys[3]; /* RKp, NHKp, CKp */
1202 dst, 1187 struct GNUNET_CRYPTO_SymmetricSessionKey HK;
1203 src, 1188 struct GNUNET_HashCode dh;
1204 size); 1189 struct GNUNET_CRYPTO_EcdhePublicKey *DHRp;
1205 } 1190
1206 HK = ax->HKr; 1191 /* Try Next HK */
1207 ax->HKr = ax->NHKr; 1192 t_hmac(&src->ax_header,
1208 t_h_decrypt (ax, 1193 sizeof(struct GNUNET_CADET_AxHeader) + esize,
1209 src, 1194 0,
1210 &plaintext_header); 1195 &ax->NHKr,
1211 Np = ntohl (plaintext_header.ax_header.Ns); 1196 &msg_hmac);
1212 PNp = ntohl (plaintext_header.ax_header.PNs); 1197 if (0 != GNUNET_memcmp(&msg_hmac,
1213 DHRp = &plaintext_header.ax_header.DHRs; 1198 &src->hmac))
1214 store_ax_keys (ax, 1199 {
1215 &HK, 1200 /* Try the skipped keys, if that fails, we're out of luck. */
1216 PNp); 1201 return try_old_ax_keys(ax,
1217 1202 dst,
1218 /* RKp, NHKp, CKp = KDF (HMAC-HASH (RK, DH (DHRp, DHRs))) */ 1203 src,
1219 GNUNET_CRYPTO_ecc_ecdh (&ax->DHRs, 1204 size);
1220 DHRp, 1205 }
1221 &dh); 1206 HK = ax->HKr;
1222 t_ax_hmac_hash (&ax->RK, 1207 ax->HKr = ax->NHKr;
1223 &hmac, 1208 t_h_decrypt(ax,
1224 &dh, sizeof (dh)); 1209 src,
1225 GNUNET_CRYPTO_kdf (keys, sizeof (keys), 1210 &plaintext_header);
1226 ctx, sizeof (ctx), 1211 Np = ntohl(plaintext_header.ax_header.Ns);
1227 &hmac, sizeof (hmac), 1212 PNp = ntohl(plaintext_header.ax_header.PNs);
1228 NULL); 1213 DHRp = &plaintext_header.ax_header.DHRs;
1229 1214 store_ax_keys(ax,
1230 /* Commit "purported" keys */ 1215 &HK,
1231 ax->RK = keys[0]; 1216 PNp);
1232 ax->NHKr = keys[1]; 1217
1233 ax->CKr = keys[2]; 1218 /* RKp, NHKp, CKp = KDF (HMAC-HASH (RK, DH (DHRp, DHRs))) */
1234 ax->DHRr = *DHRp; 1219 GNUNET_CRYPTO_ecc_ecdh(&ax->DHRs,
1235 ax->Nr = 0; 1220 DHRp,
1236 ax->ratchet_allowed = GNUNET_YES; 1221 &dh);
1237 } 1222 t_ax_hmac_hash(&ax->RK,
1223 &hmac,
1224 &dh, sizeof(dh));
1225 GNUNET_CRYPTO_kdf(keys, sizeof(keys),
1226 ctx, sizeof(ctx),
1227 &hmac, sizeof(hmac),
1228 NULL);
1229
1230 /* Commit "purported" keys */
1231 ax->RK = keys[0];
1232 ax->NHKr = keys[1];
1233 ax->CKr = keys[2];
1234 ax->DHRr = *DHRp;
1235 ax->Nr = 0;
1236 ax->ratchet_allowed = GNUNET_YES;
1237 }
1238 else 1238 else
1239 { 1239 {
1240 t_h_decrypt (ax, 1240 t_h_decrypt(ax,
1241 src, 1241 src,
1242 &plaintext_header); 1242 &plaintext_header);
1243 Np = ntohl (plaintext_header.ax_header.Ns); 1243 Np = ntohl(plaintext_header.ax_header.Ns);
1244 PNp = ntohl (plaintext_header.ax_header.PNs); 1244 PNp = ntohl(plaintext_header.ax_header.PNs);
1245 } 1245 }
1246 if ( (Np != ax->Nr) && 1246 if ((Np != ax->Nr) &&
1247 (GNUNET_OK != store_ax_keys (ax, 1247 (GNUNET_OK != store_ax_keys(ax,
1248 &ax->HKr, 1248 &ax->HKr,
1249 Np)) ) 1249 Np)))
1250 { 1250 {
1251 /* Try the skipped keys, if that fails, we're out of luck. */ 1251 /* Try the skipped keys, if that fails, we're out of luck. */
1252 return try_old_ax_keys (ax, 1252 return try_old_ax_keys(ax,
1253 dst, 1253 dst,
1254 src, 1254 src,
1255 size); 1255 size);
1256 } 1256 }
1257 1257
1258 t_ax_decrypt (ax, 1258 t_ax_decrypt(ax,
1259 dst, 1259 dst,
1260 &src[1], 1260 &src[1],
1261 esize); 1261 esize);
1262 ax->Nr = Np + 1; 1262 ax->Nr = Np + 1;
1263 return esize; 1263 return esize;
1264} 1264}
@@ -1274,13 +1274,13 @@ t_ax_decrypt_and_validate (struct CadetTunnelAxolotl *ax,
1274 * @return #GNUNET_OK (continue to iterate) 1274 * @return #GNUNET_OK (continue to iterate)
1275 */ 1275 */
1276static int 1276static int
1277notify_tunnel_up_cb (void *cls, 1277notify_tunnel_up_cb(void *cls,
1278 uint32_t key, 1278 uint32_t key,
1279 void *value) 1279 void *value)
1280{ 1280{
1281 struct CadetChannel *ch = value; 1281 struct CadetChannel *ch = value;
1282 1282
1283 GCCH_tunnel_up (ch); 1283 GCCH_tunnel_up(ch);
1284 return GNUNET_OK; 1284 return GNUNET_OK;
1285} 1285}
1286 1286
@@ -1293,35 +1293,35 @@ notify_tunnel_up_cb (void *cls,
1293 * @param state New encryption state. 1293 * @param state New encryption state.
1294 */ 1294 */
1295void 1295void
1296GCT_change_estate (struct CadetTunnel *t, 1296GCT_change_estate(struct CadetTunnel *t,
1297 enum CadetTunnelEState state) 1297 enum CadetTunnelEState state)
1298{ 1298{
1299 enum CadetTunnelEState old = t->estate; 1299 enum CadetTunnelEState old = t->estate;
1300 1300
1301 t->estate = state; 1301 t->estate = state;
1302 LOG (GNUNET_ERROR_TYPE_DEBUG, 1302 LOG(GNUNET_ERROR_TYPE_DEBUG,
1303 "%s estate changed from %s to %s\n", 1303 "%s estate changed from %s to %s\n",
1304 GCT_2s (t), 1304 GCT_2s(t),
1305 estate2s (old), 1305 estate2s(old),
1306 estate2s (state)); 1306 estate2s(state));
1307 1307
1308 if ( (CADET_TUNNEL_KEY_OK != old) && 1308 if ((CADET_TUNNEL_KEY_OK != old) &&
1309 (CADET_TUNNEL_KEY_OK == t->estate) ) 1309 (CADET_TUNNEL_KEY_OK == t->estate))
1310 { 1310 {
1311 if (NULL != t->kx_task) 1311 if (NULL != t->kx_task)
1312 { 1312 {
1313 GNUNET_SCHEDULER_cancel (t->kx_task); 1313 GNUNET_SCHEDULER_cancel(t->kx_task);
1314 t->kx_task = NULL; 1314 t->kx_task = NULL;
1315 }
1316 /* notify all channels that have been waiting */
1317 GNUNET_CONTAINER_multihashmap32_iterate(t->channels,
1318 &notify_tunnel_up_cb,
1319 t);
1320 if (NULL != t->send_task)
1321 GNUNET_SCHEDULER_cancel(t->send_task);
1322 t->send_task = GNUNET_SCHEDULER_add_now(&trigger_transmissions,
1323 t);
1315 } 1324 }
1316 /* notify all channels that have been waiting */
1317 GNUNET_CONTAINER_multihashmap32_iterate (t->channels,
1318 &notify_tunnel_up_cb,
1319 t);
1320 if (NULL != t->send_task)
1321 GNUNET_SCHEDULER_cancel (t->send_task);
1322 t->send_task = GNUNET_SCHEDULER_add_now (&trigger_transmissions,
1323 t);
1324 }
1325} 1325}
1326 1326
1327 1327
@@ -1334,63 +1334,63 @@ GCT_change_estate (struct CadetTunnel *t,
1334 * @param ax axolotl key context to use 1334 * @param ax axolotl key context to use
1335 */ 1335 */
1336static void 1336static void
1337send_kx (struct CadetTunnel *t, 1337send_kx(struct CadetTunnel *t,
1338 struct CadetTConnection *ct, 1338 struct CadetTConnection *ct,
1339 struct CadetTunnelAxolotl *ax) 1339 struct CadetTunnelAxolotl *ax)
1340{ 1340{
1341 struct CadetConnection *cc; 1341 struct CadetConnection *cc;
1342 struct GNUNET_MQ_Envelope *env; 1342 struct GNUNET_MQ_Envelope *env;
1343 struct GNUNET_CADET_TunnelKeyExchangeMessage *msg; 1343 struct GNUNET_CADET_TunnelKeyExchangeMessage *msg;
1344 enum GNUNET_CADET_KX_Flags flags; 1344 enum GNUNET_CADET_KX_Flags flags;
1345 1345
1346 if (GNUNET_YES != alice_or_betty (GCP_get_id (t->destination))) 1346 if (GNUNET_YES != alice_or_betty(GCP_get_id(t->destination)))
1347 return; /* only Alice may send KX */ 1347 return; /* only Alice may send KX */
1348 if ( (NULL == ct) || 1348 if ((NULL == ct) ||
1349 (GNUNET_NO == ct->is_ready) ) 1349 (GNUNET_NO == ct->is_ready))
1350 ct = get_ready_connection (t); 1350 ct = get_ready_connection(t);
1351 if (NULL == ct) 1351 if (NULL == ct)
1352 { 1352 {
1353 LOG (GNUNET_ERROR_TYPE_DEBUG, 1353 LOG(GNUNET_ERROR_TYPE_DEBUG,
1354 "Wanted to send %s in state %s, but no connection is ready, deferring\n", 1354 "Wanted to send %s in state %s, but no connection is ready, deferring\n",
1355 GCT_2s (t), 1355 GCT_2s(t),
1356 estate2s (t->estate)); 1356 estate2s(t->estate));
1357 t->next_kx_attempt = GNUNET_TIME_absolute_get (); 1357 t->next_kx_attempt = GNUNET_TIME_absolute_get();
1358 return; 1358 return;
1359 } 1359 }
1360 cc = ct->cc; 1360 cc = ct->cc;
1361 env = GNUNET_MQ_msg (msg, 1361 env = GNUNET_MQ_msg(msg,
1362 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX); 1362 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX);
1363 flags = GNUNET_CADET_KX_FLAG_FORCE_REPLY; /* always for KX */ 1363 flags = GNUNET_CADET_KX_FLAG_FORCE_REPLY; /* always for KX */
1364 msg->flags = htonl (flags); 1364 msg->flags = htonl(flags);
1365 msg->cid = *GCC_get_id (cc); 1365 msg->cid = *GCC_get_id(cc);
1366 GNUNET_CRYPTO_ecdhe_key_get_public (&ax->kx_0, 1366 GNUNET_CRYPTO_ecdhe_key_get_public(&ax->kx_0,
1367 &msg->ephemeral_key); 1367 &msg->ephemeral_key);
1368#if DEBUG_KX 1368#if DEBUG_KX
1369 msg->ephemeral_key_XXX = ax->kx_0; 1369 msg->ephemeral_key_XXX = ax->kx_0;
1370 msg->private_key_XXX = *my_private_key; 1370 msg->private_key_XXX = *my_private_key;
1371#endif 1371#endif
1372 LOG (GNUNET_ERROR_TYPE_DEBUG, 1372 LOG(GNUNET_ERROR_TYPE_DEBUG,
1373 "Sending KX message to %s with ephemeral %s on CID %s\n", 1373 "Sending KX message to %s with ephemeral %s on CID %s\n",
1374 GCT_2s (t), 1374 GCT_2s(t),
1375 GNUNET_e2s (&msg->ephemeral_key), 1375 GNUNET_e2s(&msg->ephemeral_key),
1376 GNUNET_sh2s (&msg->cid.connection_of_tunnel)); 1376 GNUNET_sh2s(&msg->cid.connection_of_tunnel));
1377 GNUNET_CRYPTO_ecdhe_key_get_public (&ax->DHRs, 1377 GNUNET_CRYPTO_ecdhe_key_get_public(&ax->DHRs,
1378 &msg->ratchet_key); 1378 &msg->ratchet_key);
1379 mark_connection_unready (ct); 1379 mark_connection_unready(ct);
1380 t->kx_retry_delay = GNUNET_TIME_STD_BACKOFF (t->kx_retry_delay); 1380 t->kx_retry_delay = GNUNET_TIME_STD_BACKOFF(t->kx_retry_delay);
1381 t->next_kx_attempt = GNUNET_TIME_relative_to_absolute (t->kx_retry_delay); 1381 t->next_kx_attempt = GNUNET_TIME_relative_to_absolute(t->kx_retry_delay);
1382 if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate) 1382 if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
1383 GCT_change_estate (t, 1383 GCT_change_estate(t,
1384 CADET_TUNNEL_KEY_AX_SENT); 1384 CADET_TUNNEL_KEY_AX_SENT);
1385 else if (CADET_TUNNEL_KEY_AX_RECV == t->estate) 1385 else if (CADET_TUNNEL_KEY_AX_RECV == t->estate)
1386 GCT_change_estate (t, 1386 GCT_change_estate(t,
1387 CADET_TUNNEL_KEY_AX_SENT_AND_RECV); 1387 CADET_TUNNEL_KEY_AX_SENT_AND_RECV);
1388 GCC_transmit (cc, 1388 GCC_transmit(cc,
1389 env); 1389 env);
1390 GNUNET_STATISTICS_update (stats, 1390 GNUNET_STATISTICS_update(stats,
1391 "# KX transmitted", 1391 "# KX transmitted",
1392 1, 1392 1,
1393 GNUNET_NO); 1393 GNUNET_NO);
1394} 1394}
1395 1395
1396 1396
@@ -1405,76 +1405,76 @@ send_kx (struct CadetTunnel *t,
1405 * (set if we would like to transmit right now, but cannot) 1405 * (set if we would like to transmit right now, but cannot)
1406 */ 1406 */
1407static void 1407static void
1408send_kx_auth (struct CadetTunnel *t, 1408send_kx_auth(struct CadetTunnel *t,
1409 struct CadetTConnection *ct, 1409 struct CadetTConnection *ct,
1410 struct CadetTunnelAxolotl *ax, 1410 struct CadetTunnelAxolotl *ax,
1411 int force_reply) 1411 int force_reply)
1412{ 1412{
1413 struct CadetConnection *cc; 1413 struct CadetConnection *cc;
1414 struct GNUNET_MQ_Envelope *env; 1414 struct GNUNET_MQ_Envelope *env;
1415 struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg; 1415 struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg;
1416 enum GNUNET_CADET_KX_Flags flags; 1416 enum GNUNET_CADET_KX_Flags flags;
1417 1417
1418 if ( (NULL == ct) || 1418 if ((NULL == ct) ||
1419 (GNUNET_NO == ct->is_ready) ) 1419 (GNUNET_NO == ct->is_ready))
1420 ct = get_ready_connection (t); 1420 ct = get_ready_connection(t);
1421 if (NULL == ct) 1421 if (NULL == ct)
1422 { 1422 {
1423 LOG (GNUNET_ERROR_TYPE_DEBUG, 1423 LOG(GNUNET_ERROR_TYPE_DEBUG,
1424 "Wanted to send KX_AUTH on %s, but no connection is ready, deferring\n", 1424 "Wanted to send KX_AUTH on %s, but no connection is ready, deferring\n",
1425 GCT_2s (t)); 1425 GCT_2s(t));
1426 t->next_kx_attempt = GNUNET_TIME_absolute_get (); 1426 t->next_kx_attempt = GNUNET_TIME_absolute_get();
1427 t->kx_auth_requested = GNUNET_YES; /* queue KX_AUTH independent of estate */ 1427 t->kx_auth_requested = GNUNET_YES; /* queue KX_AUTH independent of estate */
1428 return; 1428 return;
1429 } 1429 }
1430 t->kx_auth_requested = GNUNET_NO; /* clear flag */ 1430 t->kx_auth_requested = GNUNET_NO; /* clear flag */
1431 cc = ct->cc; 1431 cc = ct->cc;
1432 env = GNUNET_MQ_msg (msg, 1432 env = GNUNET_MQ_msg(msg,
1433 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX_AUTH); 1433 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX_AUTH);
1434 flags = GNUNET_CADET_KX_FLAG_NONE; 1434 flags = GNUNET_CADET_KX_FLAG_NONE;
1435 if (GNUNET_YES == force_reply) 1435 if (GNUNET_YES == force_reply)
1436 flags |= GNUNET_CADET_KX_FLAG_FORCE_REPLY; 1436 flags |= GNUNET_CADET_KX_FLAG_FORCE_REPLY;
1437 msg->kx.flags = htonl (flags); 1437 msg->kx.flags = htonl(flags);
1438 msg->kx.cid = *GCC_get_id (cc); 1438 msg->kx.cid = *GCC_get_id(cc);
1439 GNUNET_CRYPTO_ecdhe_key_get_public (&ax->kx_0, 1439 GNUNET_CRYPTO_ecdhe_key_get_public(&ax->kx_0,
1440 &msg->kx.ephemeral_key); 1440 &msg->kx.ephemeral_key);
1441 GNUNET_CRYPTO_ecdhe_key_get_public (&ax->DHRs, 1441 GNUNET_CRYPTO_ecdhe_key_get_public(&ax->DHRs,
1442 &msg->kx.ratchet_key); 1442 &msg->kx.ratchet_key);
1443#if DEBUG_KX 1443#if DEBUG_KX
1444 msg->kx.ephemeral_key_XXX = ax->kx_0; 1444 msg->kx.ephemeral_key_XXX = ax->kx_0;
1445 msg->kx.private_key_XXX = *my_private_key; 1445 msg->kx.private_key_XXX = *my_private_key;
1446 msg->r_ephemeral_key_XXX = ax->last_ephemeral; 1446 msg->r_ephemeral_key_XXX = ax->last_ephemeral;
1447#endif 1447#endif
1448 LOG (GNUNET_ERROR_TYPE_DEBUG, 1448 LOG(GNUNET_ERROR_TYPE_DEBUG,
1449 "Sending KX_AUTH message to %s with ephemeral %s on CID %s\n", 1449 "Sending KX_AUTH message to %s with ephemeral %s on CID %s\n",
1450 GCT_2s (t), 1450 GCT_2s(t),
1451 GNUNET_e2s (&msg->kx.ephemeral_key), 1451 GNUNET_e2s(&msg->kx.ephemeral_key),
1452 GNUNET_sh2s (&msg->kx.cid.connection_of_tunnel)); 1452 GNUNET_sh2s(&msg->kx.cid.connection_of_tunnel));
1453 1453
1454 /* Compute authenticator (this is the main difference to #send_kx()) */ 1454 /* Compute authenticator (this is the main difference to #send_kx()) */
1455 GNUNET_CRYPTO_hash (&ax->RK, 1455 GNUNET_CRYPTO_hash(&ax->RK,
1456 sizeof (ax->RK), 1456 sizeof(ax->RK),
1457 &msg->auth); 1457 &msg->auth);
1458 /* Compute when to be triggered again; actual job will 1458 /* Compute when to be triggered again; actual job will
1459 be scheduled via #connection_ready_cb() */ 1459 be scheduled via #connection_ready_cb() */
1460 t->kx_retry_delay 1460 t->kx_retry_delay
1461 = GNUNET_TIME_STD_BACKOFF (t->kx_retry_delay); 1461 = GNUNET_TIME_STD_BACKOFF(t->kx_retry_delay);
1462 t->next_kx_attempt 1462 t->next_kx_attempt
1463 = GNUNET_TIME_relative_to_absolute (t->kx_retry_delay); 1463 = GNUNET_TIME_relative_to_absolute(t->kx_retry_delay);
1464 1464
1465 /* Send via cc, mark it as unready */ 1465 /* Send via cc, mark it as unready */
1466 mark_connection_unready (ct); 1466 mark_connection_unready(ct);
1467 1467
1468 /* Update state machine, unless we are already OK */ 1468 /* Update state machine, unless we are already OK */
1469 if (CADET_TUNNEL_KEY_OK != t->estate) 1469 if (CADET_TUNNEL_KEY_OK != t->estate)
1470 GCT_change_estate (t, 1470 GCT_change_estate(t,
1471 CADET_TUNNEL_KEY_AX_AUTH_SENT); 1471 CADET_TUNNEL_KEY_AX_AUTH_SENT);
1472 GCC_transmit (cc, 1472 GCC_transmit(cc,
1473 env); 1473 env);
1474 GNUNET_STATISTICS_update (stats, 1474 GNUNET_STATISTICS_update(stats,
1475 "# KX_AUTH transmitted", 1475 "# KX_AUTH transmitted",
1476 1, 1476 1,
1477 GNUNET_NO); 1477 GNUNET_NO);
1478} 1478}
1479 1479
1480 1480
@@ -1484,14 +1484,14 @@ send_kx_auth (struct CadetTunnel *t,
1484 * @param ax state to free, but not memory of @a ax itself 1484 * @param ax state to free, but not memory of @a ax itself
1485 */ 1485 */
1486static void 1486static void
1487cleanup_ax (struct CadetTunnelAxolotl *ax) 1487cleanup_ax(struct CadetTunnelAxolotl *ax)
1488{ 1488{
1489 while (NULL != ax->skipped_head) 1489 while (NULL != ax->skipped_head)
1490 delete_skipped_key (ax, 1490 delete_skipped_key(ax,
1491 ax->skipped_head); 1491 ax->skipped_head);
1492 GNUNET_assert (0 == ax->skipped); 1492 GNUNET_assert(0 == ax->skipped);
1493 GNUNET_CRYPTO_ecdhe_key_clear (&ax->kx_0); 1493 GNUNET_CRYPTO_ecdhe_key_clear(&ax->kx_0);
1494 GNUNET_CRYPTO_ecdhe_key_clear (&ax->DHRs); 1494 GNUNET_CRYPTO_ecdhe_key_clear(&ax->DHRs);
1495} 1495}
1496 1496
1497 1497
@@ -1509,105 +1509,105 @@ cleanup_ax (struct CadetTunnelAxolotl *ax)
1509 * #GNUNET_SYSERR on hard errors (i.e. @a pid is #my_full_id) 1509 * #GNUNET_SYSERR on hard errors (i.e. @a pid is #my_full_id)
1510 */ 1510 */
1511static int 1511static int
1512update_ax_by_kx (struct CadetTunnelAxolotl *ax, 1512update_ax_by_kx(struct CadetTunnelAxolotl *ax,
1513 const struct GNUNET_PeerIdentity *pid, 1513 const struct GNUNET_PeerIdentity *pid,
1514 const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral_key, 1514 const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral_key,
1515 const struct GNUNET_CRYPTO_EcdhePublicKey *ratchet_key) 1515 const struct GNUNET_CRYPTO_EcdhePublicKey *ratchet_key)
1516{ 1516{
1517 struct GNUNET_HashCode key_material[3]; 1517 struct GNUNET_HashCode key_material[3];
1518 struct GNUNET_CRYPTO_SymmetricSessionKey keys[5]; 1518 struct GNUNET_CRYPTO_SymmetricSessionKey keys[5];
1519 const char salt[] = "CADET Axolotl salt"; 1519 const char salt[] = "CADET Axolotl salt";
1520 int am_I_alice; 1520 int am_I_alice;
1521 1521
1522 if (GNUNET_SYSERR == (am_I_alice = alice_or_betty (pid))) 1522 if (GNUNET_SYSERR == (am_I_alice = alice_or_betty(pid)))
1523 { 1523 {
1524 GNUNET_break_op (0); 1524 GNUNET_break_op(0);
1525 return GNUNET_SYSERR; 1525 return GNUNET_SYSERR;
1526 } 1526 }
1527 if (0 == GNUNET_memcmp (&ax->DHRr, 1527 if (0 == GNUNET_memcmp(&ax->DHRr,
1528 ratchet_key)) 1528 ratchet_key))
1529 { 1529 {
1530 GNUNET_STATISTICS_update (stats, 1530 GNUNET_STATISTICS_update(stats,
1531 "# Ratchet key already known", 1531 "# Ratchet key already known",
1532 1, 1532 1,
1533 GNUNET_NO); 1533 GNUNET_NO);
1534 LOG (GNUNET_ERROR_TYPE_DEBUG, 1534 LOG(GNUNET_ERROR_TYPE_DEBUG,
1535 "Ratchet key already known. Ignoring KX.\n"); 1535 "Ratchet key already known. Ignoring KX.\n");
1536 return GNUNET_NO; 1536 return GNUNET_NO;
1537 } 1537 }
1538 1538
1539 ax->DHRr = *ratchet_key; 1539 ax->DHRr = *ratchet_key;
1540 ax->last_ephemeral = *ephemeral_key; 1540 ax->last_ephemeral = *ephemeral_key;
1541 /* ECDH A B0 */ 1541 /* ECDH A B0 */
1542 if (GNUNET_YES == am_I_alice) 1542 if (GNUNET_YES == am_I_alice)
1543 { 1543 {
1544 GNUNET_CRYPTO_eddsa_ecdh (my_private_key, /* a */ 1544 GNUNET_CRYPTO_eddsa_ecdh(my_private_key, /* a */
1545 ephemeral_key, /* B0 */ 1545 ephemeral_key, /* B0 */
1546 &key_material[0]); 1546 &key_material[0]);
1547 } 1547 }
1548 else 1548 else
1549 { 1549 {
1550 GNUNET_CRYPTO_ecdh_eddsa (&ax->kx_0, /* b0 */ 1550 GNUNET_CRYPTO_ecdh_eddsa(&ax->kx_0, /* b0 */
1551 &pid->public_key, /* A */ 1551 &pid->public_key, /* A */
1552 &key_material[0]); 1552 &key_material[0]);
1553 } 1553 }
1554 /* ECDH A0 B */ 1554 /* ECDH A0 B */
1555 if (GNUNET_YES == am_I_alice) 1555 if (GNUNET_YES == am_I_alice)
1556 { 1556 {
1557 GNUNET_CRYPTO_ecdh_eddsa (&ax->kx_0, /* a0 */ 1557 GNUNET_CRYPTO_ecdh_eddsa(&ax->kx_0, /* a0 */
1558 &pid->public_key, /* B */ 1558 &pid->public_key, /* B */
1559 &key_material[1]); 1559 &key_material[1]);
1560 } 1560 }
1561 else 1561 else
1562 { 1562 {
1563 GNUNET_CRYPTO_eddsa_ecdh (my_private_key, /* b */ 1563 GNUNET_CRYPTO_eddsa_ecdh(my_private_key, /* b */
1564 ephemeral_key, /* A0 */ 1564 ephemeral_key, /* A0 */
1565 &key_material[1]); 1565 &key_material[1]);
1566 } 1566 }
1567 1567
1568 /* ECDH A0 B0 */ 1568 /* ECDH A0 B0 */
1569 GNUNET_CRYPTO_ecc_ecdh (&ax->kx_0, /* a0 or b0 */ 1569 GNUNET_CRYPTO_ecc_ecdh(&ax->kx_0, /* a0 or b0 */
1570 ephemeral_key, /* B0 or A0 */ 1570 ephemeral_key, /* B0 or A0 */
1571 &key_material[2]); 1571 &key_material[2]);
1572 /* KDF */ 1572 /* KDF */
1573 GNUNET_CRYPTO_kdf (keys, sizeof (keys), 1573 GNUNET_CRYPTO_kdf(keys, sizeof(keys),
1574 salt, sizeof (salt), 1574 salt, sizeof(salt),
1575 &key_material, sizeof (key_material), 1575 &key_material, sizeof(key_material),
1576 NULL); 1576 NULL);
1577 1577
1578 if (0 == memcmp (&ax->RK, 1578 if (0 == memcmp(&ax->RK,
1579 &keys[0], 1579 &keys[0],
1580 sizeof (ax->RK))) 1580 sizeof(ax->RK)))
1581 { 1581 {
1582 LOG (GNUNET_ERROR_TYPE_DEBUG, 1582 LOG(GNUNET_ERROR_TYPE_DEBUG,
1583 "Root key already known. Ignoring KX.\n"); 1583 "Root key already known. Ignoring KX.\n");
1584 GNUNET_STATISTICS_update (stats, 1584 GNUNET_STATISTICS_update(stats,
1585 "# Root key already known", 1585 "# Root key already known",
1586 1, 1586 1,
1587 GNUNET_NO); 1587 GNUNET_NO);
1588 return GNUNET_NO; 1588 return GNUNET_NO;
1589 } 1589 }
1590 1590
1591 ax->RK = keys[0]; 1591 ax->RK = keys[0];
1592 if (GNUNET_YES == am_I_alice) 1592 if (GNUNET_YES == am_I_alice)
1593 { 1593 {
1594 ax->HKr = keys[1]; 1594 ax->HKr = keys[1];
1595 ax->NHKs = keys[2]; 1595 ax->NHKs = keys[2];
1596 ax->NHKr = keys[3]; 1596 ax->NHKr = keys[3];
1597 ax->CKr = keys[4]; 1597 ax->CKr = keys[4];
1598 ax->ratchet_flag = GNUNET_YES; 1598 ax->ratchet_flag = GNUNET_YES;
1599 } 1599 }
1600 else 1600 else
1601 { 1601 {
1602 ax->HKs = keys[1]; 1602 ax->HKs = keys[1];
1603 ax->NHKr = keys[2]; 1603 ax->NHKr = keys[2];
1604 ax->NHKs = keys[3]; 1604 ax->NHKs = keys[3];
1605 ax->CKs = keys[4]; 1605 ax->CKs = keys[4];
1606 ax->ratchet_flag = GNUNET_NO; 1606 ax->ratchet_flag = GNUNET_NO;
1607 ax->ratchet_expiration 1607 ax->ratchet_expiration
1608 = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), 1608 = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(),
1609 ratchet_time); 1609 ratchet_time);
1610 } 1610 }
1611 return GNUNET_OK; 1611 return GNUNET_OK;
1612} 1612}
1613 1613
@@ -1618,24 +1618,25 @@ update_ax_by_kx (struct CadetTunnelAxolotl *ax,
1618 * @param cls the `struct CadetTunnel` to do KX for. 1618 * @param cls the `struct CadetTunnel` to do KX for.
1619 */ 1619 */
1620static void 1620static void
1621retry_kx (void *cls) 1621retry_kx(void *cls)
1622{ 1622{
1623 struct CadetTunnel *t = cls; 1623 struct CadetTunnel *t = cls;
1624 struct CadetTunnelAxolotl *ax; 1624 struct CadetTunnelAxolotl *ax;
1625 1625
1626 t->kx_task = NULL; 1626 t->kx_task = NULL;
1627 LOG (GNUNET_ERROR_TYPE_DEBUG, 1627 LOG(GNUNET_ERROR_TYPE_DEBUG,
1628 "Trying to make KX progress on %s in state %s\n", 1628 "Trying to make KX progress on %s in state %s\n",
1629 GCT_2s (t), 1629 GCT_2s(t),
1630 estate2s (t->estate)); 1630 estate2s(t->estate));
1631 switch (t->estate) 1631 switch (t->estate)
1632 { 1632 {
1633 case CADET_TUNNEL_KEY_UNINITIALIZED: /* first attempt */ 1633 case CADET_TUNNEL_KEY_UNINITIALIZED: /* first attempt */
1634 case CADET_TUNNEL_KEY_AX_SENT: /* trying again */ 1634 case CADET_TUNNEL_KEY_AX_SENT: /* trying again */
1635 send_kx (t, 1635 send_kx(t,
1636 NULL, 1636 NULL,
1637 &t->ax); 1637 &t->ax);
1638 break; 1638 break;
1639
1639 case CADET_TUNNEL_KEY_AX_RECV: 1640 case CADET_TUNNEL_KEY_AX_RECV:
1640 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV: 1641 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV:
1641 /* We are responding, so only require reply 1642 /* We are responding, so only require reply
@@ -1648,16 +1649,17 @@ retry_kx (void *cls)
1648 else 1649 else
1649 { 1650 {
1650 /* How can this be? */ 1651 /* How can this be? */
1651 GNUNET_break (0); 1652 GNUNET_break(0);
1652 ax = &t->ax; 1653 ax = &t->ax;
1653 } 1654 }
1654 send_kx_auth (t, 1655 send_kx_auth(t,
1655 NULL, 1656 NULL,
1656 ax, 1657 ax,
1657 (0 == GCT_count_channels (t)) 1658 (0 == GCT_count_channels(t))
1658 ? GNUNET_NO 1659 ? GNUNET_NO
1659 : GNUNET_YES); 1660 : GNUNET_YES);
1660 break; 1661 break;
1662
1661 case CADET_TUNNEL_KEY_AX_AUTH_SENT: 1663 case CADET_TUNNEL_KEY_AX_AUTH_SENT:
1662 /* We are responding, so only require reply 1664 /* We are responding, so only require reply
1663 if WE have a channel waiting. */ 1665 if WE have a channel waiting. */
@@ -1669,16 +1671,17 @@ retry_kx (void *cls)
1669 else 1671 else
1670 { 1672 {
1671 /* How can this be? */ 1673 /* How can this be? */
1672 GNUNET_break (0); 1674 GNUNET_break(0);
1673 ax = &t->ax; 1675 ax = &t->ax;
1674 } 1676 }
1675 send_kx_auth (t, 1677 send_kx_auth(t,
1676 NULL, 1678 NULL,
1677 ax, 1679 ax,
1678 (0 == GCT_count_channels (t)) 1680 (0 == GCT_count_channels(t))
1679 ? GNUNET_NO 1681 ? GNUNET_NO
1680 : GNUNET_YES); 1682 : GNUNET_YES);
1681 break; 1683 break;
1684
1682 case CADET_TUNNEL_KEY_OK: 1685 case CADET_TUNNEL_KEY_OK:
1683 /* Must have been the *other* peer asking us to 1686 /* Must have been the *other* peer asking us to
1684 respond with a KX_AUTH. */ 1687 respond with a KX_AUTH. */
@@ -1692,10 +1695,10 @@ retry_kx (void *cls)
1692 /* Sending AX_AUTH in response to AX_AUTH */ 1695 /* Sending AX_AUTH in response to AX_AUTH */
1693 ax = &t->ax; 1696 ax = &t->ax;
1694 } 1697 }
1695 send_kx_auth (t, 1698 send_kx_auth(t,
1696 NULL, 1699 NULL,
1697 ax, 1700 ax,
1698 GNUNET_NO); 1701 GNUNET_NO);
1699 break; 1702 break;
1700 } 1703 }
1701} 1704}
@@ -1710,101 +1713,101 @@ retry_kx (void *cls)
1710 * @param msg the key exchange message 1713 * @param msg the key exchange message
1711 */ 1714 */
1712void 1715void
1713GCT_handle_kx (struct CadetTConnection *ct, 1716GCT_handle_kx(struct CadetTConnection *ct,
1714 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg) 1717 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
1715{ 1718{
1716 struct CadetTunnel *t = ct->t; 1719 struct CadetTunnel *t = ct->t;
1717 int ret; 1720 int ret;
1718 1721
1719 GNUNET_STATISTICS_update (stats, 1722 GNUNET_STATISTICS_update(stats,
1720 "# KX received", 1723 "# KX received",
1721 1, 1724 1,
1722 GNUNET_NO); 1725 GNUNET_NO);
1723 if (GNUNET_YES == 1726 if (GNUNET_YES ==
1724 alice_or_betty (GCP_get_id (t->destination))) 1727 alice_or_betty(GCP_get_id(t->destination)))
1725 { 1728 {
1726 /* Betty/Bob is not allowed to send KX! */ 1729 /* Betty/Bob is not allowed to send KX! */
1727 GNUNET_break_op (0); 1730 GNUNET_break_op(0);
1728 return; 1731 return;
1729 } 1732 }
1730 LOG (GNUNET_ERROR_TYPE_DEBUG, 1733 LOG(GNUNET_ERROR_TYPE_DEBUG,
1731 "Received KX message from %s with ephemeral %s from %s on connection %s\n", 1734 "Received KX message from %s with ephemeral %s from %s on connection %s\n",
1732 GCT_2s (t), 1735 GCT_2s(t),
1733 GNUNET_e2s (&msg->ephemeral_key), 1736 GNUNET_e2s(&msg->ephemeral_key),
1734 GNUNET_i2s (GCP_get_id (t->destination)), 1737 GNUNET_i2s(GCP_get_id(t->destination)),
1735 GCC_2s (ct->cc)); 1738 GCC_2s(ct->cc));
1736#if 1 1739#if 1
1737 if ( (0 == 1740 if ((0 ==
1738 memcmp (&t->ax.DHRr, 1741 memcmp(&t->ax.DHRr,
1739 &msg->ratchet_key, 1742 &msg->ratchet_key,
1740 sizeof (msg->ratchet_key))) && 1743 sizeof(msg->ratchet_key))) &&
1741 (0 == 1744 (0 ==
1742 memcmp (&t->ax.last_ephemeral, 1745 memcmp(&t->ax.last_ephemeral,
1743 &msg->ephemeral_key, 1746 &msg->ephemeral_key,
1744 sizeof (msg->ephemeral_key))) ) 1747 sizeof(msg->ephemeral_key))))
1745 1748
1746 { 1749 {
1747 GNUNET_STATISTICS_update (stats, 1750 GNUNET_STATISTICS_update(stats,
1748 "# Duplicate KX received", 1751 "# Duplicate KX received",
1749 1, 1752 1,
1750 GNUNET_NO); 1753 GNUNET_NO);
1751 send_kx_auth (t, 1754 send_kx_auth(t,
1752 ct, 1755 ct,
1753 &t->ax, 1756 &t->ax,
1754 GNUNET_NO); 1757 GNUNET_NO);
1755 return; 1758 return;
1756 } 1759 }
1757#endif 1760#endif
1758 /* We only keep ONE unverified KX around, so if there is an existing one, 1761 /* We only keep ONE unverified KX around, so if there is an existing one,
1759 clean it up. */ 1762 clean it up. */
1760 if (NULL != t->unverified_ax) 1763 if (NULL != t->unverified_ax)
1761 { 1764 {
1762 if ( (0 == 1765 if ((0 ==
1763 memcmp (&t->unverified_ax->DHRr, 1766 memcmp(&t->unverified_ax->DHRr,
1764 &msg->ratchet_key, 1767 &msg->ratchet_key,
1765 sizeof (msg->ratchet_key))) && 1768 sizeof(msg->ratchet_key))) &&
1766 (0 == 1769 (0 ==
1767 memcmp (&t->unverified_ax->last_ephemeral, 1770 memcmp(&t->unverified_ax->last_ephemeral,
1768 &msg->ephemeral_key, 1771 &msg->ephemeral_key,
1769 sizeof (msg->ephemeral_key))) ) 1772 sizeof(msg->ephemeral_key))))
1770 { 1773 {
1771 GNUNET_STATISTICS_update (stats, 1774 GNUNET_STATISTICS_update(stats,
1772 "# Duplicate unverified KX received", 1775 "# Duplicate unverified KX received",
1773 1, 1776 1,
1774 GNUNET_NO); 1777 GNUNET_NO);
1775#if 1 1778#if 1
1776 send_kx_auth (t, 1779 send_kx_auth(t,
1777 ct, 1780 ct,
1778 t->unverified_ax, 1781 t->unverified_ax,
1779 GNUNET_NO); 1782 GNUNET_NO);
1780 return; 1783 return;
1781#endif 1784#endif
1785 }
1786 LOG(GNUNET_ERROR_TYPE_DEBUG,
1787 "Dropping old unverified KX state.\n");
1788 GNUNET_STATISTICS_update(stats,
1789 "# Unverified KX dropped for fresh KX",
1790 1,
1791 GNUNET_NO);
1792 GNUNET_break(NULL == t->unverified_ax->skipped_head);
1793 memset(t->unverified_ax,
1794 0,
1795 sizeof(struct CadetTunnelAxolotl));
1782 } 1796 }
1783 LOG (GNUNET_ERROR_TYPE_DEBUG,
1784 "Dropping old unverified KX state.\n");
1785 GNUNET_STATISTICS_update (stats,
1786 "# Unverified KX dropped for fresh KX",
1787 1,
1788 GNUNET_NO);
1789 GNUNET_break (NULL == t->unverified_ax->skipped_head);
1790 memset (t->unverified_ax,
1791 0,
1792 sizeof (struct CadetTunnelAxolotl));
1793 }
1794 else 1797 else
1795 { 1798 {
1796 LOG (GNUNET_ERROR_TYPE_DEBUG, 1799 LOG(GNUNET_ERROR_TYPE_DEBUG,
1797 "Creating fresh unverified KX for %s\n", 1800 "Creating fresh unverified KX for %s\n",
1798 GCT_2s (t)); 1801 GCT_2s(t));
1799 GNUNET_STATISTICS_update (stats, 1802 GNUNET_STATISTICS_update(stats,
1800 "# Fresh KX setup", 1803 "# Fresh KX setup",
1801 1, 1804 1,
1802 GNUNET_NO); 1805 GNUNET_NO);
1803 t->unverified_ax = GNUNET_new (struct CadetTunnelAxolotl); 1806 t->unverified_ax = GNUNET_new(struct CadetTunnelAxolotl);
1804 } 1807 }
1805 /* Set as the 'current' RK/DHRr the one we are currently using, 1808 /* Set as the 'current' RK/DHRr the one we are currently using,
1806 so that the duplicate-detection logic of 1809 so that the duplicate-detection logic of
1807 #update_ax_by_kx can work. */ 1810 #update_ax_by_kx can work. */
1808 t->unverified_ax->RK = t->ax.RK; 1811 t->unverified_ax->RK = t->ax.RK;
1809 t->unverified_ax->DHRr = t->ax.DHRr; 1812 t->unverified_ax->DHRr = t->ax.DHRr;
1810 t->unverified_ax->DHRs = t->ax.DHRs; 1813 t->unverified_ax->DHRs = t->ax.DHRs;
@@ -1812,101 +1815,101 @@ GCT_handle_kx (struct CadetTConnection *ct,
1812 t->unverified_attempts = 0; 1815 t->unverified_attempts = 0;
1813 1816
1814 /* Update 'ax' by the new key material */ 1817 /* Update 'ax' by the new key material */
1815 ret = update_ax_by_kx (t->unverified_ax, 1818 ret = update_ax_by_kx(t->unverified_ax,
1816 GCP_get_id (t->destination), 1819 GCP_get_id(t->destination),
1817 &msg->ephemeral_key, 1820 &msg->ephemeral_key,
1818 &msg->ratchet_key); 1821 &msg->ratchet_key);
1819 GNUNET_break (GNUNET_SYSERR != ret); 1822 GNUNET_break(GNUNET_SYSERR != ret);
1820 if (GNUNET_OK != ret) 1823 if (GNUNET_OK != ret)
1821 { 1824 {
1822 GNUNET_STATISTICS_update (stats, 1825 GNUNET_STATISTICS_update(stats,
1823 "# Useless KX", 1826 "# Useless KX",
1824 1, 1827 1,
1825 GNUNET_NO); 1828 GNUNET_NO);
1826 return; /* duplicate KX, nothing to do */ 1829 return; /* duplicate KX, nothing to do */
1827 } 1830 }
1828 /* move ahead in our state machine */ 1831 /* move ahead in our state machine */
1829 if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate) 1832 if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
1830 GCT_change_estate (t, 1833 GCT_change_estate(t,
1831 CADET_TUNNEL_KEY_AX_RECV); 1834 CADET_TUNNEL_KEY_AX_RECV);
1832 else if (CADET_TUNNEL_KEY_AX_SENT == t->estate) 1835 else if (CADET_TUNNEL_KEY_AX_SENT == t->estate)
1833 GCT_change_estate (t, 1836 GCT_change_estate(t,
1834 CADET_TUNNEL_KEY_AX_SENT_AND_RECV); 1837 CADET_TUNNEL_KEY_AX_SENT_AND_RECV);
1835 1838
1836 /* KX is still not done, try again our end. */ 1839 /* KX is still not done, try again our end. */
1837 if (CADET_TUNNEL_KEY_OK != t->estate) 1840 if (CADET_TUNNEL_KEY_OK != t->estate)
1838 { 1841 {
1839 if (NULL != t->kx_task) 1842 if (NULL != t->kx_task)
1840 GNUNET_SCHEDULER_cancel (t->kx_task); 1843 GNUNET_SCHEDULER_cancel(t->kx_task);
1841 t->kx_task 1844 t->kx_task
1842 = GNUNET_SCHEDULER_add_now (&retry_kx, 1845 = GNUNET_SCHEDULER_add_now(&retry_kx,
1843 t); 1846 t);
1844 } 1847 }
1845} 1848}
1846 1849
1847 1850
1848#if DEBUG_KX 1851#if DEBUG_KX
1849static void 1852static void
1850check_ee (const struct GNUNET_CRYPTO_EcdhePrivateKey *e1, 1853check_ee(const struct GNUNET_CRYPTO_EcdhePrivateKey *e1,
1851 const struct GNUNET_CRYPTO_EcdhePrivateKey *e2) 1854 const struct GNUNET_CRYPTO_EcdhePrivateKey *e2)
1852{ 1855{
1853 struct GNUNET_CRYPTO_EcdhePublicKey p1; 1856 struct GNUNET_CRYPTO_EcdhePublicKey p1;
1854 struct GNUNET_CRYPTO_EcdhePublicKey p2; 1857 struct GNUNET_CRYPTO_EcdhePublicKey p2;
1855 struct GNUNET_HashCode hc1; 1858 struct GNUNET_HashCode hc1;
1856 struct GNUNET_HashCode hc2; 1859 struct GNUNET_HashCode hc2;
1857 1860
1858 GNUNET_CRYPTO_ecdhe_key_get_public (e1, 1861 GNUNET_CRYPTO_ecdhe_key_get_public(e1,
1859 &p1); 1862 &p1);
1860 GNUNET_CRYPTO_ecdhe_key_get_public (e2, 1863 GNUNET_CRYPTO_ecdhe_key_get_public(e2,
1861 &p2); 1864 &p2);
1862 GNUNET_assert (GNUNET_OK == 1865 GNUNET_assert(GNUNET_OK ==
1863 GNUNET_CRYPTO_ecc_ecdh (e1, 1866 GNUNET_CRYPTO_ecc_ecdh(e1,
1864 &p2, 1867 &p2,
1865 &hc1)); 1868 &hc1));
1866 GNUNET_assert (GNUNET_OK == 1869 GNUNET_assert(GNUNET_OK ==
1867 GNUNET_CRYPTO_ecc_ecdh (e2, 1870 GNUNET_CRYPTO_ecc_ecdh(e2,
1868 &p1, 1871 &p1,
1869 &hc2)); 1872 &hc2));
1870 GNUNET_break (0 == GNUNET_memcmp (&hc1, 1873 GNUNET_break(0 == GNUNET_memcmp(&hc1,
1871 &hc2)); 1874 &hc2));
1872} 1875}
1873 1876
1874 1877
1875static void 1878static void
1876check_ed (const struct GNUNET_CRYPTO_EcdhePrivateKey *e1, 1879check_ed(const struct GNUNET_CRYPTO_EcdhePrivateKey *e1,
1877 const struct GNUNET_CRYPTO_EddsaPrivateKey *e2) 1880 const struct GNUNET_CRYPTO_EddsaPrivateKey *e2)
1878{ 1881{
1879 struct GNUNET_CRYPTO_EcdhePublicKey p1; 1882 struct GNUNET_CRYPTO_EcdhePublicKey p1;
1880 struct GNUNET_CRYPTO_EddsaPublicKey p2; 1883 struct GNUNET_CRYPTO_EddsaPublicKey p2;
1881 struct GNUNET_HashCode hc1; 1884 struct GNUNET_HashCode hc1;
1882 struct GNUNET_HashCode hc2; 1885 struct GNUNET_HashCode hc2;
1883 1886
1884 GNUNET_CRYPTO_ecdhe_key_get_public (e1, 1887 GNUNET_CRYPTO_ecdhe_key_get_public(e1,
1885 &p1); 1888 &p1);
1886 GNUNET_CRYPTO_eddsa_key_get_public (e2, 1889 GNUNET_CRYPTO_eddsa_key_get_public(e2,
1887 &p2); 1890 &p2);
1888 GNUNET_assert (GNUNET_OK == 1891 GNUNET_assert(GNUNET_OK ==
1889 GNUNET_CRYPTO_ecdh_eddsa (e1, 1892 GNUNET_CRYPTO_ecdh_eddsa(e1,
1890 &p2, 1893 &p2,
1891 &hc1)); 1894 &hc1));
1892 GNUNET_assert (GNUNET_OK == 1895 GNUNET_assert(GNUNET_OK ==
1893 GNUNET_CRYPTO_eddsa_ecdh (e2, 1896 GNUNET_CRYPTO_eddsa_ecdh(e2,
1894 &p1, 1897 &p1,
1895 &hc2)); 1898 &hc2));
1896 GNUNET_break (0 == GNUNET_memcmp (&hc1, 1899 GNUNET_break(0 == GNUNET_memcmp(&hc1,
1897 &hc2)); 1900 &hc2));
1898} 1901}
1899 1902
1900 1903
1901static void 1904static void
1902test_crypto_bug (const struct GNUNET_CRYPTO_EcdhePrivateKey *e1, 1905test_crypto_bug(const struct GNUNET_CRYPTO_EcdhePrivateKey *e1,
1903 const struct GNUNET_CRYPTO_EcdhePrivateKey *e2, 1906 const struct GNUNET_CRYPTO_EcdhePrivateKey *e2,
1904 const struct GNUNET_CRYPTO_EddsaPrivateKey *d1, 1907 const struct GNUNET_CRYPTO_EddsaPrivateKey *d1,
1905 const struct GNUNET_CRYPTO_EddsaPrivateKey *d2) 1908 const struct GNUNET_CRYPTO_EddsaPrivateKey *d2)
1906{ 1909{
1907 check_ee (e1, e2); 1910 check_ee(e1, e2);
1908 check_ed (e1, d2); 1911 check_ed(e1, d2);
1909 check_ed (e2, d1); 1912 check_ed(e2, d1);
1910} 1913}
1911 1914
1912#endif 1915#endif
@@ -1919,133 +1922,135 @@ test_crypto_bug (const struct GNUNET_CRYPTO_EcdhePrivateKey *e1,
1919 * @param msg the key exchange message 1922 * @param msg the key exchange message
1920 */ 1923 */
1921void 1924void
1922GCT_handle_kx_auth (struct CadetTConnection *ct, 1925GCT_handle_kx_auth(struct CadetTConnection *ct,
1923 const struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg) 1926 const struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg)
1924{ 1927{
1925 struct CadetTunnel *t = ct->t; 1928 struct CadetTunnel *t = ct->t;
1926 struct CadetTunnelAxolotl ax_tmp; 1929 struct CadetTunnelAxolotl ax_tmp;
1927 struct GNUNET_HashCode kx_auth; 1930 struct GNUNET_HashCode kx_auth;
1928 int ret; 1931 int ret;
1929 1932
1930 GNUNET_STATISTICS_update (stats, 1933 GNUNET_STATISTICS_update(stats,
1931 "# KX_AUTH received", 1934 "# KX_AUTH received",
1932 1, 1935 1,
1933 GNUNET_NO); 1936 GNUNET_NO);
1934 if ( (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate) || 1937 if ((CADET_TUNNEL_KEY_UNINITIALIZED == t->estate) ||
1935 (CADET_TUNNEL_KEY_AX_RECV == t->estate) ) 1938 (CADET_TUNNEL_KEY_AX_RECV == t->estate))
1936 { 1939 {
1937 /* Confusing, we got a KX_AUTH before we even send our own 1940 /* Confusing, we got a KX_AUTH before we even send our own
1938 KX. This should not happen. We'll send our own KX ASAP anyway, 1941 KX. This should not happen. We'll send our own KX ASAP anyway,
1939 so let's ignore this here. */ 1942 so let's ignore this here. */
1940 GNUNET_break_op (0); 1943 GNUNET_break_op(0);
1941 return; 1944 return;
1942 } 1945 }
1943 LOG (GNUNET_ERROR_TYPE_DEBUG, 1946 LOG(GNUNET_ERROR_TYPE_DEBUG,
1944 "Handling KX_AUTH message from %s with ephemeral %s\n", 1947 "Handling KX_AUTH message from %s with ephemeral %s\n",
1945 GCT_2s (t), 1948 GCT_2s(t),
1946 GNUNET_e2s (&msg->kx.ephemeral_key)); 1949 GNUNET_e2s(&msg->kx.ephemeral_key));
1947 /* We do everything in ax_tmp until we've checked the authentication 1950 /* We do everything in ax_tmp until we've checked the authentication
1948 so we don't clobber anything we care about by accident. */ 1951 so we don't clobber anything we care about by accident. */
1949 ax_tmp = t->ax; 1952 ax_tmp = t->ax;
1950 1953
1951 /* Update 'ax' by the new key material */ 1954 /* Update 'ax' by the new key material */
1952 ret = update_ax_by_kx (&ax_tmp, 1955 ret = update_ax_by_kx(&ax_tmp,
1953 GCP_get_id (t->destination), 1956 GCP_get_id(t->destination),
1954 &msg->kx.ephemeral_key, 1957 &msg->kx.ephemeral_key,
1955 &msg->kx.ratchet_key); 1958 &msg->kx.ratchet_key);
1956 if (GNUNET_OK != ret) 1959 if (GNUNET_OK != ret)
1957 {
1958 if (GNUNET_NO == ret)
1959 GNUNET_STATISTICS_update (stats,
1960 "# redundant KX_AUTH received",
1961 1,
1962 GNUNET_NO);
1963 else
1964 GNUNET_break (0); /* connect to self!? */
1965 return;
1966 }
1967 GNUNET_CRYPTO_hash (&ax_tmp.RK,
1968 sizeof (ax_tmp.RK),
1969 &kx_auth);
1970 if (0 != GNUNET_memcmp (&kx_auth,
1971 &msg->auth))
1972 {
1973 /* This KX_AUTH is not using the latest KX/KX_AUTH data
1974 we transmitted to the sender, refuse it, try KX again. */
1975 GNUNET_STATISTICS_update (stats,
1976 "# KX_AUTH not using our last KX received (auth failure)",
1977 1,
1978 GNUNET_NO);
1979 LOG (GNUNET_ERROR_TYPE_WARNING,
1980 "KX AUTH mismatch!\n");
1981#if DEBUG_KX
1982 { 1960 {
1983 struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key; 1961 if (GNUNET_NO == ret)
1984 1962 GNUNET_STATISTICS_update(stats,
1985 GNUNET_CRYPTO_ecdhe_key_get_public (&ax_tmp.kx_0, 1963 "# redundant KX_AUTH received",
1986 &ephemeral_key); 1964 1,
1987 if (0 != GNUNET_memcmp (&ephemeral_key, 1965 GNUNET_NO);
1988 &msg->r_ephemeral_key_XXX))
1989 {
1990 LOG (GNUNET_ERROR_TYPE_WARNING,
1991 "My ephemeral is %s!\n",
1992 GNUNET_e2s (&ephemeral_key));
1993 LOG (GNUNET_ERROR_TYPE_WARNING,
1994 "Response is for ephemeral %s!\n",
1995 GNUNET_e2s (&msg->r_ephemeral_key_XXX));
1996 }
1997 else 1966 else
1967 GNUNET_break(0); /* connect to self!? */
1968 return;
1969 }
1970 GNUNET_CRYPTO_hash(&ax_tmp.RK,
1971 sizeof(ax_tmp.RK),
1972 &kx_auth);
1973 if (0 != GNUNET_memcmp(&kx_auth,
1974 &msg->auth))
1975 {
1976 /* This KX_AUTH is not using the latest KX/KX_AUTH data
1977 we transmitted to the sender, refuse it, try KX again. */
1978 GNUNET_STATISTICS_update(stats,
1979 "# KX_AUTH not using our last KX received (auth failure)",
1980 1,
1981 GNUNET_NO);
1982 LOG(GNUNET_ERROR_TYPE_WARNING,
1983 "KX AUTH mismatch!\n");
1984#if DEBUG_KX
1998 { 1985 {
1999 test_crypto_bug (&ax_tmp.kx_0, 1986 struct GNUNET_CRYPTO_EcdhePublicKey ephemeral_key;
2000 &msg->kx.ephemeral_key_XXX, 1987
2001 my_private_key, 1988 GNUNET_CRYPTO_ecdhe_key_get_public(&ax_tmp.kx_0,
2002 &msg->kx.private_key_XXX); 1989 &ephemeral_key);
1990 if (0 != GNUNET_memcmp(&ephemeral_key,
1991 &msg->r_ephemeral_key_XXX))
1992 {
1993 LOG(GNUNET_ERROR_TYPE_WARNING,
1994 "My ephemeral is %s!\n",
1995 GNUNET_e2s(&ephemeral_key));
1996 LOG(GNUNET_ERROR_TYPE_WARNING,
1997 "Response is for ephemeral %s!\n",
1998 GNUNET_e2s(&msg->r_ephemeral_key_XXX));
1999 }
2000 else
2001 {
2002 test_crypto_bug(&ax_tmp.kx_0,
2003 &msg->kx.ephemeral_key_XXX,
2004 my_private_key,
2005 &msg->kx.private_key_XXX);
2006 }
2003 } 2007 }
2004 }
2005#endif 2008#endif
2006 if (NULL == t->kx_task) 2009 if (NULL == t->kx_task)
2007 t->kx_task 2010 t->kx_task
2008 = GNUNET_SCHEDULER_add_at (t->next_kx_attempt, 2011 = GNUNET_SCHEDULER_add_at(t->next_kx_attempt,
2009 &retry_kx, 2012 &retry_kx,
2010 t); 2013 t);
2011 return; 2014 return;
2012 } 2015 }
2013 /* Yep, we're good. */ 2016 /* Yep, we're good. */
2014 t->ax = ax_tmp; 2017 t->ax = ax_tmp;
2015 if (NULL != t->unverified_ax) 2018 if (NULL != t->unverified_ax)
2016 { 2019 {
2017 /* We got some "stale" KX before, drop that. */ 2020 /* We got some "stale" KX before, drop that. */
2018 cleanup_ax (t->unverified_ax); 2021 cleanup_ax(t->unverified_ax);
2019 GNUNET_free (t->unverified_ax); 2022 GNUNET_free(t->unverified_ax);
2020 t->unverified_ax = NULL; 2023 t->unverified_ax = NULL;
2021 } 2024 }
2022 2025
2023 /* move ahead in our state machine */ 2026 /* move ahead in our state machine */
2024 switch (t->estate) 2027 switch (t->estate)
2025 { 2028 {
2026 case CADET_TUNNEL_KEY_UNINITIALIZED: 2029 case CADET_TUNNEL_KEY_UNINITIALIZED:
2027 case CADET_TUNNEL_KEY_AX_RECV: 2030 case CADET_TUNNEL_KEY_AX_RECV:
2028 /* Checked above, this is impossible. */ 2031 /* Checked above, this is impossible. */
2029 GNUNET_assert (0); 2032 GNUNET_assert(0);
2030 break; 2033 break;
2031 case CADET_TUNNEL_KEY_AX_SENT: /* This is the normal case */ 2034
2032 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV: /* both peers started KX */ 2035 case CADET_TUNNEL_KEY_AX_SENT: /* This is the normal case */
2033 case CADET_TUNNEL_KEY_AX_AUTH_SENT: /* both peers now did KX_AUTH */ 2036 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV: /* both peers started KX */
2034 GCT_change_estate (t, 2037 case CADET_TUNNEL_KEY_AX_AUTH_SENT: /* both peers now did KX_AUTH */
2035 CADET_TUNNEL_KEY_OK); 2038 GCT_change_estate(t,
2036 break; 2039 CADET_TUNNEL_KEY_OK);
2037 case CADET_TUNNEL_KEY_OK: 2040 break;
2038 /* Did not expect another KX_AUTH, but so what, still acceptable. 2041
2039 Nothing to do here. */ 2042 case CADET_TUNNEL_KEY_OK:
2040 break; 2043 /* Did not expect another KX_AUTH, but so what, still acceptable.
2041 } 2044 Nothing to do here. */
2042 if (0 != (GNUNET_CADET_KX_FLAG_FORCE_REPLY & ntohl (msg->kx.flags))) 2045 break;
2043 { 2046 }
2044 send_kx_auth (t, 2047 if (0 != (GNUNET_CADET_KX_FLAG_FORCE_REPLY & ntohl(msg->kx.flags)))
2045 NULL, 2048 {
2046 &t->ax, 2049 send_kx_auth(t,
2047 GNUNET_NO); 2050 NULL,
2048 } 2051 &t->ax,
2052 GNUNET_NO);
2053 }
2049} 2054}
2050 2055
2051 2056
@@ -2060,7 +2065,7 @@ GCT_handle_kx_auth (struct CadetTConnection *ct,
2060 * @return unused number that can uniquely identify a channel in the tunnel 2065 * @return unused number that can uniquely identify a channel in the tunnel
2061 */ 2066 */
2062static struct GNUNET_CADET_ChannelTunnelNumber 2067static struct GNUNET_CADET_ChannelTunnelNumber
2063get_next_free_ctn (struct CadetTunnel *t) 2068get_next_free_ctn(struct CadetTunnel *t)
2064{ 2069{
2065#define HIGH_BIT 0x8000000 2070#define HIGH_BIT 0x8000000
2066 struct GNUNET_CADET_ChannelTunnelNumber ret; 2071 struct GNUNET_CADET_ChannelTunnelNumber ret;
@@ -2068,23 +2073,23 @@ get_next_free_ctn (struct CadetTunnel *t)
2068 int cmp; 2073 int cmp;
2069 uint32_t highbit; 2074 uint32_t highbit;
2070 2075
2071 cmp = GNUNET_memcmp (&my_full_id, 2076 cmp = GNUNET_memcmp(&my_full_id,
2072 GCP_get_id (GCT_get_destination (t))); 2077 GCP_get_id(GCT_get_destination(t)));
2073 if (0 < cmp) 2078 if (0 < cmp)
2074 highbit = HIGH_BIT; 2079 highbit = HIGH_BIT;
2075 else if (0 > cmp) 2080 else if (0 > cmp)
2076 highbit = 0; 2081 highbit = 0;
2077 else 2082 else
2078 GNUNET_assert (0); // loopback must never go here! 2083 GNUNET_assert(0); // loopback must never go here!
2079 ctn = ntohl (t->next_ctn.cn); 2084 ctn = ntohl(t->next_ctn.cn);
2080 while (NULL != 2085 while (NULL !=
2081 GNUNET_CONTAINER_multihashmap32_get (t->channels, 2086 GNUNET_CONTAINER_multihashmap32_get(t->channels,
2082 ctn | highbit)) 2087 ctn | highbit))
2083 { 2088 {
2084 ctn = ((ctn + 1) & (~ HIGH_BIT)); 2089 ctn = ((ctn + 1) & (~HIGH_BIT));
2085 } 2090 }
2086 t->next_ctn.cn = htonl ((ctn + 1) & (~ HIGH_BIT)); 2091 t->next_ctn.cn = htonl((ctn + 1) & (~HIGH_BIT));
2087 ret.cn = htonl (ctn | highbit); 2092 ret.cn = htonl(ctn | highbit);
2088 return ret; 2093 return ret;
2089} 2094}
2090 2095
@@ -2099,50 +2104,53 @@ get_next_free_ctn (struct CadetTunnel *t)
2099 * @return unique number identifying @a ch within @a t 2104 * @return unique number identifying @a ch within @a t
2100 */ 2105 */
2101struct GNUNET_CADET_ChannelTunnelNumber 2106struct GNUNET_CADET_ChannelTunnelNumber
2102GCT_add_channel (struct CadetTunnel *t, 2107GCT_add_channel(struct CadetTunnel *t,
2103 struct CadetChannel *ch) 2108 struct CadetChannel *ch)
2104{ 2109{
2105 struct GNUNET_CADET_ChannelTunnelNumber ctn; 2110 struct GNUNET_CADET_ChannelTunnelNumber ctn;
2106 2111
2107 ctn = get_next_free_ctn (t); 2112 ctn = get_next_free_ctn(t);
2108 if (NULL != t->destroy_task) 2113 if (NULL != t->destroy_task)
2109 { 2114 {
2110 GNUNET_SCHEDULER_cancel (t->destroy_task); 2115 GNUNET_SCHEDULER_cancel(t->destroy_task);
2111 t->destroy_task = NULL; 2116 t->destroy_task = NULL;
2112 } 2117 }
2113 GNUNET_assert (GNUNET_YES == 2118 GNUNET_assert(GNUNET_YES ==
2114 GNUNET_CONTAINER_multihashmap32_put (t->channels, 2119 GNUNET_CONTAINER_multihashmap32_put(t->channels,
2115 ntohl (ctn.cn), 2120 ntohl(ctn.cn),
2116 ch, 2121 ch,
2117 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 2122 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2118 LOG (GNUNET_ERROR_TYPE_DEBUG, 2123 LOG(GNUNET_ERROR_TYPE_DEBUG,
2119 "Adding %s to %s\n", 2124 "Adding %s to %s\n",
2120 GCCH_2s (ch), 2125 GCCH_2s(ch),
2121 GCT_2s (t)); 2126 GCT_2s(t));
2122 switch (t->estate) 2127 switch (t->estate)
2123 { 2128 {
2124 case CADET_TUNNEL_KEY_UNINITIALIZED: 2129 case CADET_TUNNEL_KEY_UNINITIALIZED:
2125 /* waiting for connection to start KX */ 2130 /* waiting for connection to start KX */
2126 break; 2131 break;
2127 case CADET_TUNNEL_KEY_AX_RECV: 2132
2128 case CADET_TUNNEL_KEY_AX_SENT: 2133 case CADET_TUNNEL_KEY_AX_RECV:
2129 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV: 2134 case CADET_TUNNEL_KEY_AX_SENT:
2130 /* we're currently waiting for KX to complete */ 2135 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV:
2131 break; 2136 /* we're currently waiting for KX to complete */
2132 case CADET_TUNNEL_KEY_AX_AUTH_SENT: 2137 break;
2133 /* waiting for OTHER peer to send us data, 2138
2134 we might need to prompt more aggressively! */ 2139 case CADET_TUNNEL_KEY_AX_AUTH_SENT:
2135 if (NULL == t->kx_task) 2140 /* waiting for OTHER peer to send us data,
2136 t->kx_task 2141 we might need to prompt more aggressively! */
2137 = GNUNET_SCHEDULER_add_at (t->next_kx_attempt, 2142 if (NULL == t->kx_task)
2138 &retry_kx, 2143 t->kx_task
2139 t); 2144 = GNUNET_SCHEDULER_add_at(t->next_kx_attempt,
2140 break; 2145 &retry_kx,
2141 case CADET_TUNNEL_KEY_OK: 2146 t);
2142 /* We are ready. Tell the new channel that we are up. */ 2147 break;
2143 GCCH_tunnel_up (ch); 2148
2144 break; 2149 case CADET_TUNNEL_KEY_OK:
2145 } 2150 /* We are ready. Tell the new channel that we are up. */
2151 GCCH_tunnel_up(ch);
2152 break;
2153 }
2146 return ctn; 2154 return ctn;
2147} 2155}
2148 2156
@@ -2154,25 +2162,25 @@ GCT_add_channel (struct CadetTunnel *t,
2154 * @param ct binding of connection to tunnel of the connection that was lost. 2162 * @param ct binding of connection to tunnel of the connection that was lost.
2155 */ 2163 */
2156void 2164void
2157GCT_connection_lost (struct CadetTConnection *ct) 2165GCT_connection_lost(struct CadetTConnection *ct)
2158{ 2166{
2159 struct CadetTunnel *t = ct->t; 2167 struct CadetTunnel *t = ct->t;
2160 2168
2161 if (GNUNET_YES == ct->is_ready) 2169 if (GNUNET_YES == ct->is_ready)
2162 { 2170 {
2163 GNUNET_CONTAINER_DLL_remove (t->connection_ready_head, 2171 GNUNET_CONTAINER_DLL_remove(t->connection_ready_head,
2164 t->connection_ready_tail, 2172 t->connection_ready_tail,
2165 ct); 2173 ct);
2166 t->num_ready_connections--; 2174 t->num_ready_connections--;
2167 } 2175 }
2168 else 2176 else
2169 { 2177 {
2170 GNUNET_CONTAINER_DLL_remove (t->connection_busy_head, 2178 GNUNET_CONTAINER_DLL_remove(t->connection_busy_head,
2171 t->connection_busy_tail, 2179 t->connection_busy_tail,
2172 ct); 2180 ct);
2173 t->num_busy_connections--; 2181 t->num_busy_connections--;
2174 } 2182 }
2175 GNUNET_free (ct); 2183 GNUNET_free(ct);
2176} 2184}
2177 2185
2178 2186
@@ -2183,15 +2191,15 @@ GCT_connection_lost (struct CadetTConnection *ct)
2183 * @param ct connection to clean up 2191 * @param ct connection to clean up
2184 */ 2192 */
2185static void 2193static void
2186destroy_t_connection (void *cls, 2194destroy_t_connection(void *cls,
2187 struct CadetTConnection *ct) 2195 struct CadetTConnection *ct)
2188{ 2196{
2189 struct CadetTunnel *t = cls; 2197 struct CadetTunnel *t = cls;
2190 struct CadetConnection *cc = ct->cc; 2198 struct CadetConnection *cc = ct->cc;
2191 2199
2192 GNUNET_assert (ct->t == t); 2200 GNUNET_assert(ct->t == t);
2193 GCT_connection_lost (ct); 2201 GCT_connection_lost(ct);
2194 GCC_destroy_without_tunnel (cc); 2202 GCC_destroy_without_tunnel(cc);
2195} 2203}
2196 2204
2197 2205
@@ -2201,56 +2209,56 @@ destroy_t_connection (void *cls,
2201 * @param cls the idle tunnel 2209 * @param cls the idle tunnel
2202 */ 2210 */
2203static void 2211static void
2204destroy_tunnel (void *cls) 2212destroy_tunnel(void *cls)
2205{ 2213{
2206 struct CadetTunnel *t = cls; 2214 struct CadetTunnel *t = cls;
2207 struct CadetTunnelQueueEntry *tq; 2215 struct CadetTunnelQueueEntry *tq;
2208 2216
2209 t->destroy_task = NULL; 2217 t->destroy_task = NULL;
2210 LOG (GNUNET_ERROR_TYPE_DEBUG, 2218 LOG(GNUNET_ERROR_TYPE_DEBUG,
2211 "Destroying idle %s\n", 2219 "Destroying idle %s\n",
2212 GCT_2s (t)); 2220 GCT_2s(t));
2213 GNUNET_assert (0 == GCT_count_channels (t)); 2221 GNUNET_assert(0 == GCT_count_channels(t));
2214 GCT_iterate_connections (t, 2222 GCT_iterate_connections(t,
2215 &destroy_t_connection, 2223 &destroy_t_connection,
2216 t); 2224 t);
2217 GNUNET_assert (NULL == t->connection_ready_head); 2225 GNUNET_assert(NULL == t->connection_ready_head);
2218 GNUNET_assert (NULL == t->connection_busy_head); 2226 GNUNET_assert(NULL == t->connection_busy_head);
2219 while (NULL != (tq = t->tq_head)) 2227 while (NULL != (tq = t->tq_head))
2220 { 2228 {
2221 if (NULL != tq->cont) 2229 if (NULL != tq->cont)
2222 tq->cont (tq->cont_cls, 2230 tq->cont(tq->cont_cls,
2223 NULL); 2231 NULL);
2224 GCT_send_cancel (tq); 2232 GCT_send_cancel(tq);
2225 } 2233 }
2226 GCP_drop_tunnel (t->destination, 2234 GCP_drop_tunnel(t->destination,
2227 t); 2235 t);
2228 GNUNET_CONTAINER_multihashmap32_destroy (t->channels); 2236 GNUNET_CONTAINER_multihashmap32_destroy(t->channels);
2229 if (NULL != t->maintain_connections_task) 2237 if (NULL != t->maintain_connections_task)
2230 { 2238 {
2231 GNUNET_SCHEDULER_cancel (t->maintain_connections_task); 2239 GNUNET_SCHEDULER_cancel(t->maintain_connections_task);
2232 t->maintain_connections_task = NULL; 2240 t->maintain_connections_task = NULL;
2233 } 2241 }
2234 if (NULL != t->send_task) 2242 if (NULL != t->send_task)
2235 { 2243 {
2236 GNUNET_SCHEDULER_cancel (t->send_task); 2244 GNUNET_SCHEDULER_cancel(t->send_task);
2237 t->send_task = NULL; 2245 t->send_task = NULL;
2238 } 2246 }
2239 if (NULL != t->kx_task) 2247 if (NULL != t->kx_task)
2240 { 2248 {
2241 GNUNET_SCHEDULER_cancel (t->kx_task); 2249 GNUNET_SCHEDULER_cancel(t->kx_task);
2242 t->kx_task = NULL; 2250 t->kx_task = NULL;
2243 } 2251 }
2244 GNUNET_MST_destroy (t->mst); 2252 GNUNET_MST_destroy(t->mst);
2245 GNUNET_MQ_destroy (t->mq); 2253 GNUNET_MQ_destroy(t->mq);
2246 if (NULL != t->unverified_ax) 2254 if (NULL != t->unverified_ax)
2247 { 2255 {
2248 cleanup_ax (t->unverified_ax); 2256 cleanup_ax(t->unverified_ax);
2249 GNUNET_free (t->unverified_ax); 2257 GNUNET_free(t->unverified_ax);
2250 } 2258 }
2251 cleanup_ax (&t->ax); 2259 cleanup_ax(&t->ax);
2252 GNUNET_assert (NULL == t->destroy_task); 2260 GNUNET_assert(NULL == t->destroy_task);
2253 GNUNET_free (t); 2261 GNUNET_free(t);
2254} 2262}
2255 2263
2256 2264
@@ -2262,27 +2270,27 @@ destroy_tunnel (void *cls)
2262 * @param ctn unique number identifying @a ch within @a t 2270 * @param ctn unique number identifying @a ch within @a t
2263 */ 2271 */
2264void 2272void
2265GCT_remove_channel (struct CadetTunnel *t, 2273GCT_remove_channel(struct CadetTunnel *t,
2266 struct CadetChannel *ch, 2274 struct CadetChannel *ch,
2267 struct GNUNET_CADET_ChannelTunnelNumber ctn) 2275 struct GNUNET_CADET_ChannelTunnelNumber ctn)
2268{ 2276{
2269 LOG (GNUNET_ERROR_TYPE_DEBUG, 2277 LOG(GNUNET_ERROR_TYPE_DEBUG,
2270 "Removing %s from %s\n", 2278 "Removing %s from %s\n",
2271 GCCH_2s (ch), 2279 GCCH_2s(ch),
2272 GCT_2s (t)); 2280 GCT_2s(t));
2273 GNUNET_assert (GNUNET_YES == 2281 GNUNET_assert(GNUNET_YES ==
2274 GNUNET_CONTAINER_multihashmap32_remove (t->channels, 2282 GNUNET_CONTAINER_multihashmap32_remove(t->channels,
2275 ntohl (ctn.cn), 2283 ntohl(ctn.cn),
2276 ch)); 2284 ch));
2277 if ( (0 == 2285 if ((0 ==
2278 GCT_count_channels (t)) && 2286 GCT_count_channels(t)) &&
2279 (NULL == t->destroy_task) ) 2287 (NULL == t->destroy_task))
2280 { 2288 {
2281 t->destroy_task 2289 t->destroy_task
2282 = GNUNET_SCHEDULER_add_delayed (IDLE_DESTROY_DELAY, 2290 = GNUNET_SCHEDULER_add_delayed(IDLE_DESTROY_DELAY,
2283 &destroy_tunnel, 2291 &destroy_tunnel,
2284 t); 2292 t);
2285 } 2293 }
2286} 2294}
2287 2295
2288 2296
@@ -2295,14 +2303,14 @@ GCT_remove_channel (struct CadetTunnel *t,
2295 * @return #GNUNET_OK (continue to iterate) 2303 * @return #GNUNET_OK (continue to iterate)
2296 */ 2304 */
2297static int 2305static int
2298destroy_remaining_channels (void *cls, 2306destroy_remaining_channels(void *cls,
2299 uint32_t key, 2307 uint32_t key,
2300 void *value) 2308 void *value)
2301{ 2309{
2302 struct CadetChannel *ch = value; 2310 struct CadetChannel *ch = value;
2303 2311
2304 GCCH_handle_remote_destroy (ch, 2312 GCCH_handle_remote_destroy(ch,
2305 NULL); 2313 NULL);
2306 return GNUNET_OK; 2314 return GNUNET_OK;
2307} 2315}
2308 2316
@@ -2313,20 +2321,20 @@ destroy_remaining_channels (void *cls,
2313 * @param t tunnel to destroy 2321 * @param t tunnel to destroy
2314 */ 2322 */
2315void 2323void
2316GCT_destroy_tunnel_now (struct CadetTunnel *t) 2324GCT_destroy_tunnel_now(struct CadetTunnel *t)
2317{ 2325{
2318 GNUNET_assert (GNUNET_YES == shutting_down); 2326 GNUNET_assert(GNUNET_YES == shutting_down);
2319 GNUNET_CONTAINER_multihashmap32_iterate (t->channels, 2327 GNUNET_CONTAINER_multihashmap32_iterate(t->channels,
2320 &destroy_remaining_channels, 2328 &destroy_remaining_channels,
2321 t); 2329 t);
2322 GNUNET_assert (0 == 2330 GNUNET_assert(0 ==
2323 GCT_count_channels (t)); 2331 GCT_count_channels(t));
2324 if (NULL != t->destroy_task) 2332 if (NULL != t->destroy_task)
2325 { 2333 {
2326 GNUNET_SCHEDULER_cancel (t->destroy_task); 2334 GNUNET_SCHEDULER_cancel(t->destroy_task);
2327 t->destroy_task = NULL; 2335 t->destroy_task = NULL;
2328 } 2336 }
2329 destroy_tunnel (t); 2337 destroy_tunnel(t);
2330} 2338}
2331 2339
2332 2340
@@ -2338,40 +2346,40 @@ GCT_destroy_tunnel_now (struct CadetTunnel *t)
2338 * @param ct connection to use for transmission (is ready) 2346 * @param ct connection to use for transmission (is ready)
2339 */ 2347 */
2340static void 2348static void
2341try_send_normal_payload (struct CadetTunnel *t, 2349try_send_normal_payload(struct CadetTunnel *t,
2342 struct CadetTConnection *ct) 2350 struct CadetTConnection *ct)
2343{ 2351{
2344 struct CadetTunnelQueueEntry *tq; 2352 struct CadetTunnelQueueEntry *tq;
2345 2353
2346 GNUNET_assert (GNUNET_YES == ct->is_ready); 2354 GNUNET_assert(GNUNET_YES == ct->is_ready);
2347 tq = t->tq_head; 2355 tq = t->tq_head;
2348 if (NULL == tq) 2356 if (NULL == tq)
2349 { 2357 {
2350 /* no messages pending right now */ 2358 /* no messages pending right now */
2351 LOG (GNUNET_ERROR_TYPE_DEBUG, 2359 LOG(GNUNET_ERROR_TYPE_DEBUG,
2352 "Not sending payload of %s on ready %s (nothing pending)\n", 2360 "Not sending payload of %s on ready %s (nothing pending)\n",
2353 GCT_2s (t), 2361 GCT_2s(t),
2354 GCC_2s (ct->cc)); 2362 GCC_2s(ct->cc));
2355 return; 2363 return;
2356 } 2364 }
2357 /* ready to send message 'tq' on tunnel 'ct' */ 2365 /* ready to send message 'tq' on tunnel 'ct' */
2358 GNUNET_assert (t == tq->t); 2366 GNUNET_assert(t == tq->t);
2359 GNUNET_CONTAINER_DLL_remove (t->tq_head, 2367 GNUNET_CONTAINER_DLL_remove(t->tq_head,
2360 t->tq_tail, 2368 t->tq_tail,
2361 tq); 2369 tq);
2362 if (NULL != tq->cid) 2370 if (NULL != tq->cid)
2363 *tq->cid = *GCC_get_id (ct->cc); 2371 *tq->cid = *GCC_get_id(ct->cc);
2364 mark_connection_unready (ct); 2372 mark_connection_unready(ct);
2365 LOG (GNUNET_ERROR_TYPE_DEBUG, 2373 LOG(GNUNET_ERROR_TYPE_DEBUG,
2366 "Sending payload of %s on %s\n", 2374 "Sending payload of %s on %s\n",
2367 GCT_2s (t), 2375 GCT_2s(t),
2368 GCC_2s (ct->cc)); 2376 GCC_2s(ct->cc));
2369 GCC_transmit (ct->cc, 2377 GCC_transmit(ct->cc,
2370 tq->env); 2378 tq->env);
2371 if (NULL != tq->cont) 2379 if (NULL != tq->cont)
2372 tq->cont (tq->cont_cls, 2380 tq->cont(tq->cont_cls,
2373 GCC_get_id (ct->cc)); 2381 GCC_get_id(ct->cc));
2374 GNUNET_free (tq); 2382 GNUNET_free(tq);
2375} 2383}
2376 2384
2377 2385
@@ -2384,95 +2392,97 @@ try_send_normal_payload (struct CadetTunnel *t,
2384 * #GNUNET_NO if connection are no longer ready 2392 * #GNUNET_NO if connection are no longer ready
2385 */ 2393 */
2386static void 2394static void
2387connection_ready_cb (void *cls, 2395connection_ready_cb(void *cls,
2388 int is_ready) 2396 int is_ready)
2389{ 2397{
2390 struct CadetTConnection *ct = cls; 2398 struct CadetTConnection *ct = cls;
2391 struct CadetTunnel *t = ct->t; 2399 struct CadetTunnel *t = ct->t;
2392 2400
2393 if (GNUNET_NO == is_ready) 2401 if (GNUNET_NO == is_ready)
2394 { 2402 {
2395 LOG (GNUNET_ERROR_TYPE_DEBUG, 2403 LOG(GNUNET_ERROR_TYPE_DEBUG,
2396 "%s no longer ready for %s\n", 2404 "%s no longer ready for %s\n",
2397 GCC_2s (ct->cc), 2405 GCC_2s(ct->cc),
2398 GCT_2s (t)); 2406 GCT_2s(t));
2399 mark_connection_unready (ct); 2407 mark_connection_unready(ct);
2400 return; 2408 return;
2401 } 2409 }
2402 GNUNET_assert (GNUNET_NO == ct->is_ready); 2410 GNUNET_assert(GNUNET_NO == ct->is_ready);
2403 GNUNET_CONTAINER_DLL_remove (t->connection_busy_head, 2411 GNUNET_CONTAINER_DLL_remove(t->connection_busy_head,
2404 t->connection_busy_tail, 2412 t->connection_busy_tail,
2405 ct); 2413 ct);
2406 GNUNET_assert (0 < t->num_busy_connections); 2414 GNUNET_assert(0 < t->num_busy_connections);
2407 t->num_busy_connections--; 2415 t->num_busy_connections--;
2408 ct->is_ready = GNUNET_YES; 2416 ct->is_ready = GNUNET_YES;
2409 GNUNET_CONTAINER_DLL_insert_tail (t->connection_ready_head, 2417 GNUNET_CONTAINER_DLL_insert_tail(t->connection_ready_head,
2410 t->connection_ready_tail, 2418 t->connection_ready_tail,
2411 ct); 2419 ct);
2412 t->num_ready_connections++; 2420 t->num_ready_connections++;
2413 2421
2414 LOG (GNUNET_ERROR_TYPE_DEBUG, 2422 LOG(GNUNET_ERROR_TYPE_DEBUG,
2415 "%s now ready for %s in state %s\n", 2423 "%s now ready for %s in state %s\n",
2416 GCC_2s (ct->cc), 2424 GCC_2s(ct->cc),
2417 GCT_2s (t), 2425 GCT_2s(t),
2418 estate2s (t->estate)); 2426 estate2s(t->estate));
2419 switch (t->estate) 2427 switch (t->estate)
2420 {
2421 case CADET_TUNNEL_KEY_UNINITIALIZED:
2422 /* Do not begin KX if WE have no channels waiting! */
2423 if (0 != GNUNET_TIME_absolute_get_remaining (t->next_kx_attempt).rel_value_us)
2424 return; /* wait for timeout before retrying */
2425 /* We are uninitialized, just transmit immediately,
2426 without undue delay. */
2427 if (NULL != t->kx_task)
2428 {
2429 GNUNET_SCHEDULER_cancel (t->kx_task);
2430 t->kx_task = NULL;
2431 }
2432 send_kx (t,
2433 ct,
2434 &t->ax);
2435 if ( (0 ==
2436 GCT_count_channels (t)) &&
2437 (NULL == t->destroy_task) )
2438 {
2439 t->destroy_task
2440 = GNUNET_SCHEDULER_add_delayed (IDLE_DESTROY_DELAY,
2441 &destroy_tunnel,
2442 t);
2443 }
2444 break;
2445 case CADET_TUNNEL_KEY_AX_RECV:
2446 case CADET_TUNNEL_KEY_AX_SENT:
2447 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV:
2448 case CADET_TUNNEL_KEY_AX_AUTH_SENT:
2449 /* we're currently waiting for KX to complete, schedule job */
2450 if (NULL == t->kx_task)
2451 t->kx_task
2452 = GNUNET_SCHEDULER_add_at (t->next_kx_attempt,
2453 &retry_kx,
2454 t);
2455 break;
2456 case CADET_TUNNEL_KEY_OK:
2457 if (GNUNET_YES == t->kx_auth_requested)
2458 { 2428 {
2459 if (0 != GNUNET_TIME_absolute_get_remaining (t->next_kx_attempt).rel_value_us) 2429 case CADET_TUNNEL_KEY_UNINITIALIZED:
2460 return; /* wait for timeout */ 2430 /* Do not begin KX if WE have no channels waiting! */
2431 if (0 != GNUNET_TIME_absolute_get_remaining(t->next_kx_attempt).rel_value_us)
2432 return; /* wait for timeout before retrying */
2433 /* We are uninitialized, just transmit immediately,
2434 without undue delay. */
2461 if (NULL != t->kx_task) 2435 if (NULL != t->kx_task)
2462 { 2436 {
2463 GNUNET_SCHEDULER_cancel (t->kx_task); 2437 GNUNET_SCHEDULER_cancel(t->kx_task);
2464 t->kx_task = NULL; 2438 t->kx_task = NULL;
2465 } 2439 }
2466 send_kx_auth (t, 2440 send_kx(t,
2467 ct, 2441 ct,
2468 &t->ax, 2442 &t->ax);
2469 GNUNET_NO); 2443 if ((0 ==
2470 return; 2444 GCT_count_channels(t)) &&
2445 (NULL == t->destroy_task))
2446 {
2447 t->destroy_task
2448 = GNUNET_SCHEDULER_add_delayed(IDLE_DESTROY_DELAY,
2449 &destroy_tunnel,
2450 t);
2451 }
2452 break;
2453
2454 case CADET_TUNNEL_KEY_AX_RECV:
2455 case CADET_TUNNEL_KEY_AX_SENT:
2456 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV:
2457 case CADET_TUNNEL_KEY_AX_AUTH_SENT:
2458 /* we're currently waiting for KX to complete, schedule job */
2459 if (NULL == t->kx_task)
2460 t->kx_task
2461 = GNUNET_SCHEDULER_add_at(t->next_kx_attempt,
2462 &retry_kx,
2463 t);
2464 break;
2465
2466 case CADET_TUNNEL_KEY_OK:
2467 if (GNUNET_YES == t->kx_auth_requested)
2468 {
2469 if (0 != GNUNET_TIME_absolute_get_remaining(t->next_kx_attempt).rel_value_us)
2470 return; /* wait for timeout */
2471 if (NULL != t->kx_task)
2472 {
2473 GNUNET_SCHEDULER_cancel(t->kx_task);
2474 t->kx_task = NULL;
2475 }
2476 send_kx_auth(t,
2477 ct,
2478 &t->ax,
2479 GNUNET_NO);
2480 return;
2481 }
2482 try_send_normal_payload(t,
2483 ct);
2484 break;
2471 } 2485 }
2472 try_send_normal_payload (t,
2473 ct);
2474 break;
2475 }
2476} 2486}
2477 2487
2478 2488
@@ -2485,7 +2495,7 @@ connection_ready_cb (void *cls,
2485 * @param cls the `struct CadetTunnel` to process messages on 2495 * @param cls the `struct CadetTunnel` to process messages on
2486 */ 2496 */
2487static void 2497static void
2488trigger_transmissions (void *cls) 2498trigger_transmissions(void *cls)
2489{ 2499{
2490 struct CadetTunnel *t = cls; 2500 struct CadetTunnel *t = cls;
2491 struct CadetTConnection *ct; 2501 struct CadetTConnection *ct;
@@ -2493,11 +2503,11 @@ trigger_transmissions (void *cls)
2493 t->send_task = NULL; 2503 t->send_task = NULL;
2494 if (NULL == t->tq_head) 2504 if (NULL == t->tq_head)
2495 return; /* no messages pending right now */ 2505 return; /* no messages pending right now */
2496 ct = get_ready_connection (t); 2506 ct = get_ready_connection(t);
2497 if (NULL == ct) 2507 if (NULL == ct)
2498 return; /* no connections ready */ 2508 return; /* no connections ready */
2499 try_send_normal_payload (t, 2509 try_send_normal_payload(t,
2500 ct); 2510 ct);
2501} 2511}
2502 2512
2503 2513
@@ -2505,9 +2515,7 @@ trigger_transmissions (void *cls)
2505 * Closure for #evaluate_connection. Used to assemble summary information 2515 * Closure for #evaluate_connection. Used to assemble summary information
2506 * about the existing connections so we can evaluate a new path. 2516 * about the existing connections so we can evaluate a new path.
2507 */ 2517 */
2508struct EvaluationSummary 2518struct EvaluationSummary {
2509{
2510
2511 /** 2519 /**
2512 * Minimum length of any of our connections, `UINT_MAX` if we have none. 2520 * Minimum length of any of our connections, `UINT_MAX` if we have none.
2513 */ 2521 */
@@ -2548,7 +2556,6 @@ struct EvaluationSummary
2548 * Set to #GNUNET_YES if we have a connection over @e path already. 2556 * Set to #GNUNET_YES if we have a connection over @e path already.
2549 */ 2557 */
2550 int duplicate; 2558 int duplicate;
2551
2552}; 2559};
2553 2560
2554 2561
@@ -2560,8 +2567,8 @@ struct EvaluationSummary
2560 * @param ct a connection to include in the summary 2567 * @param ct a connection to include in the summary
2561 */ 2568 */
2562static void 2569static void
2563evaluate_connection (void *cls, 2570evaluate_connection(void *cls,
2564 struct CadetTConnection *ct) 2571 struct CadetTConnection *ct)
2565{ 2572{
2566 struct EvaluationSummary *es = cls; 2573 struct EvaluationSummary *es = cls;
2567 struct CadetConnection *cc = ct->cc; 2574 struct CadetConnection *cc = ct->cc;
@@ -2574,79 +2581,79 @@ evaluate_connection (void *cls,
2574 double score; 2581 double score;
2575 double success_rate; 2582 double success_rate;
2576 2583
2577 ps = GCC_get_path (cc, 2584 ps = GCC_get_path(cc,
2578 &ct_length); 2585 &ct_length);
2579 LOG (GNUNET_ERROR_TYPE_DEBUG, 2586 LOG(GNUNET_ERROR_TYPE_DEBUG,
2580 "Evaluating path %s of existing %s\n", 2587 "Evaluating path %s of existing %s\n",
2581 GCPP_2s (ps), 2588 GCPP_2s(ps),
2582 GCC_2s (cc)); 2589 GCC_2s(cc));
2583 if (ps == es->path) 2590 if (ps == es->path)
2584 {
2585 LOG (GNUNET_ERROR_TYPE_DEBUG,
2586 "Ignoring duplicate path %s.\n",
2587 GCPP_2s (es->path));
2588 es->duplicate = GNUNET_YES;
2589 return;
2590 }
2591 if (NULL != es->path)
2592 {
2593 int duplicate = GNUNET_YES;
2594
2595 for (unsigned int i=0;i<ct_length;i++)
2596 {
2597 GNUNET_assert (GCPP_get_length (es->path) > i);
2598 if (GCPP_get_peer_at_offset (es->path,
2599 i) !=
2600 GCPP_get_peer_at_offset (ps,
2601 i))
2602 {
2603 duplicate = GNUNET_NO;
2604 break;
2605 }
2606 }
2607 if (GNUNET_YES == duplicate)
2608 { 2591 {
2609 LOG (GNUNET_ERROR_TYPE_DEBUG, 2592 LOG(GNUNET_ERROR_TYPE_DEBUG,
2610 "Ignoring overlapping path %s.\n", 2593 "Ignoring duplicate path %s.\n",
2611 GCPP_2s (es->path)); 2594 GCPP_2s(es->path));
2612 es->duplicate = GNUNET_YES; 2595 es->duplicate = GNUNET_YES;
2613 return; 2596 return;
2614 } 2597 }
2615 else 2598 if (NULL != es->path)
2616 { 2599 {
2617 LOG (GNUNET_ERROR_TYPE_DEBUG, 2600 int duplicate = GNUNET_YES;
2618 "Known path %s differs from proposed path\n", 2601
2619 GCPP_2s (ps)); 2602 for (unsigned int i = 0; i < ct_length; i++)
2603 {
2604 GNUNET_assert(GCPP_get_length(es->path) > i);
2605 if (GCPP_get_peer_at_offset(es->path,
2606 i) !=
2607 GCPP_get_peer_at_offset(ps,
2608 i))
2609 {
2610 duplicate = GNUNET_NO;
2611 break;
2612 }
2613 }
2614 if (GNUNET_YES == duplicate)
2615 {
2616 LOG(GNUNET_ERROR_TYPE_DEBUG,
2617 "Ignoring overlapping path %s.\n",
2618 GCPP_2s(es->path));
2619 es->duplicate = GNUNET_YES;
2620 return;
2621 }
2622 else
2623 {
2624 LOG(GNUNET_ERROR_TYPE_DEBUG,
2625 "Known path %s differs from proposed path\n",
2626 GCPP_2s(ps));
2627 }
2620 } 2628 }
2621 }
2622 2629
2623 ct_desirability = GCPP_get_desirability (ps); 2630 ct_desirability = GCPP_get_desirability(ps);
2624 metrics = GCC_get_metrics (cc); 2631 metrics = GCC_get_metrics(cc);
2625 uptime = GNUNET_TIME_absolute_get_duration (metrics->age); 2632 uptime = GNUNET_TIME_absolute_get_duration(metrics->age);
2626 last_use = GNUNET_TIME_absolute_get_duration (metrics->last_use); 2633 last_use = GNUNET_TIME_absolute_get_duration(metrics->last_use);
2627 /* We add 1.0 here to avoid division by zero. */ 2634 /* We add 1.0 here to avoid division by zero. */
2628 success_rate = (metrics->num_acked_transmissions + 1.0) / (metrics->num_successes + 1.0); 2635 success_rate = (metrics->num_acked_transmissions + 1.0) / (metrics->num_successes + 1.0);
2629 score 2636 score
2630 = ct_desirability 2637 = ct_desirability
2631 + 100.0 / (1.0 + ct_length) /* longer paths = better */ 2638 + 100.0 / (1.0 + ct_length) /* longer paths = better */
2632 + sqrt (uptime.rel_value_us / 60000000LL) /* larger uptime = better */ 2639 + sqrt(uptime.rel_value_us / 60000000LL) /* larger uptime = better */
2633 - last_use.rel_value_us / 1000L; /* longer idle = worse */ 2640 - last_use.rel_value_us / 1000L; /* longer idle = worse */
2634 score *= success_rate; /* weigh overall by success rate */ 2641 score *= success_rate; /* weigh overall by success rate */
2635 2642
2636 if ( (NULL == es->worst) || 2643 if ((NULL == es->worst) ||
2637 (score < es->worst_score) ) 2644 (score < es->worst_score))
2638 { 2645 {
2639 es->worst = ct; 2646 es->worst = ct;
2640 es->worst_score = score; 2647 es->worst_score = score;
2641 } 2648 }
2642 es->min_length = GNUNET_MIN (es->min_length, 2649 es->min_length = GNUNET_MIN(es->min_length,
2643 ct_length); 2650 ct_length);
2644 es->max_length = GNUNET_MAX (es->max_length, 2651 es->max_length = GNUNET_MAX(es->max_length,
2645 ct_length); 2652 ct_length);
2646 es->min_desire = GNUNET_MIN (es->min_desire, 2653 es->min_desire = GNUNET_MIN(es->min_desire,
2647 ct_desirability); 2654 ct_desirability);
2648 es->max_desire = GNUNET_MAX (es->max_desire, 2655 es->max_desire = GNUNET_MAX(es->max_desire,
2649 ct_desirability); 2656 ct_desirability);
2650} 2657}
2651 2658
2652 2659
@@ -2660,17 +2667,17 @@ evaluate_connection (void *cls,
2660 * @return #GNUNET_YES (should keep iterating) 2667 * @return #GNUNET_YES (should keep iterating)
2661 */ 2668 */
2662static int 2669static int
2663consider_path_cb (void *cls, 2670consider_path_cb(void *cls,
2664 struct CadetPeerPath *path, 2671 struct CadetPeerPath *path,
2665 unsigned int off) 2672 unsigned int off)
2666{ 2673{
2667 struct CadetTunnel *t = cls; 2674 struct CadetTunnel *t = cls;
2668 struct EvaluationSummary es; 2675 struct EvaluationSummary es;
2669 struct CadetTConnection *ct; 2676 struct CadetTConnection *ct;
2670 2677
2671 GNUNET_assert (off < GCPP_get_length (path)); 2678 GNUNET_assert(off < GCPP_get_length(path));
2672 GNUNET_assert (GCPP_get_peer_at_offset (path, 2679 GNUNET_assert(GCPP_get_peer_at_offset(path,
2673 off) == t->destination); 2680 off) == t->destination);
2674 es.min_length = UINT_MAX; 2681 es.min_length = UINT_MAX;
2675 es.max_length = 0; 2682 es.max_length = 0;
2676 es.max_desire = 0; 2683 es.max_desire = 0;
@@ -2680,16 +2687,16 @@ consider_path_cb (void *cls,
2680 es.worst = NULL; 2687 es.worst = NULL;
2681 2688
2682 /* Compute evaluation summary over existing connections. */ 2689 /* Compute evaluation summary over existing connections. */
2683 LOG (GNUNET_ERROR_TYPE_DEBUG, 2690 LOG(GNUNET_ERROR_TYPE_DEBUG,
2684 "Evaluating proposed path %s for target %s\n", 2691 "Evaluating proposed path %s for target %s\n",
2685 GCPP_2s (path), 2692 GCPP_2s(path),
2686 GCT_2s (t)); 2693 GCT_2s(t));
2687 /* FIXME: suspect this does not ACTUALLY iterate 2694 /* FIXME: suspect this does not ACTUALLY iterate
2688 over all existing paths, otherwise dup detection 2695 over all existing paths, otherwise dup detection
2689 should work!!! */ 2696 should work!!! */
2690 GCT_iterate_connections (t, 2697 GCT_iterate_connections(t,
2691 &evaluate_connection, 2698 &evaluate_connection,
2692 &es); 2699 &es);
2693 if (GNUNET_YES == es.duplicate) 2700 if (GNUNET_YES == es.duplicate)
2694 return GNUNET_YES; 2701 return GNUNET_YES;
2695 2702
@@ -2700,54 +2707,54 @@ consider_path_cb (void *cls,
2700 /* We iterate by increasing path length; if we have enough paths and 2707 /* We iterate by increasing path length; if we have enough paths and
2701 this one is more than twice as long than what we are currently 2708 this one is more than twice as long than what we are currently
2702 using, then ignore all of these super-long ones! */ 2709 using, then ignore all of these super-long ones! */
2703 if ( (GCT_count_any_connections (t) > DESIRED_CONNECTIONS_PER_TUNNEL) && 2710 if ((GCT_count_any_connections(t) > DESIRED_CONNECTIONS_PER_TUNNEL) &&
2704 (es.min_length * 2 < off) && 2711 (es.min_length * 2 < off) &&
2705 (es.max_length < off) ) 2712 (es.max_length < off))
2706 { 2713 {
2707 LOG (GNUNET_ERROR_TYPE_DEBUG, 2714 LOG(GNUNET_ERROR_TYPE_DEBUG,
2708 "Ignoring paths of length %u, they are way too long.\n", 2715 "Ignoring paths of length %u, they are way too long.\n",
2709 es.min_length * 2); 2716 es.min_length * 2);
2710 return GNUNET_NO; 2717 return GNUNET_NO;
2711 } 2718 }
2712 /* If we have enough paths and this one looks no better, ignore it. */ 2719 /* If we have enough paths and this one looks no better, ignore it. */
2713 if ( (GCT_count_any_connections (t) >= DESIRED_CONNECTIONS_PER_TUNNEL) && 2720 if ((GCT_count_any_connections(t) >= DESIRED_CONNECTIONS_PER_TUNNEL) &&
2714 (es.min_length < GCPP_get_length (path)) && 2721 (es.min_length < GCPP_get_length(path)) &&
2715 (es.min_desire > GCPP_get_desirability (path)) && 2722 (es.min_desire > GCPP_get_desirability(path)) &&
2716 (es.max_length < off) ) 2723 (es.max_length < off))
2717 { 2724 {
2718 LOG (GNUNET_ERROR_TYPE_DEBUG, 2725 LOG(GNUNET_ERROR_TYPE_DEBUG,
2719 "Ignoring path (%u/%llu) to %s, got something better already.\n", 2726 "Ignoring path (%u/%llu) to %s, got something better already.\n",
2720 GCPP_get_length (path), 2727 GCPP_get_length(path),
2721 (unsigned long long) GCPP_get_desirability (path), 2728 (unsigned long long)GCPP_get_desirability(path),
2722 GCP_2s (t->destination)); 2729 GCP_2s(t->destination));
2723 return GNUNET_YES; 2730 return GNUNET_YES;
2724 } 2731 }
2725 2732
2726 /* Path is interesting (better by some metric, or we don't have 2733 /* Path is interesting (better by some metric, or we don't have
2727 enough paths yet). */ 2734 enough paths yet). */
2728 ct = GNUNET_new (struct CadetTConnection); 2735 ct = GNUNET_new(struct CadetTConnection);
2729 ct->created = GNUNET_TIME_absolute_get (); 2736 ct->created = GNUNET_TIME_absolute_get();
2730 ct->t = t; 2737 ct->t = t;
2731 ct->cc = GCC_create (t->destination, 2738 ct->cc = GCC_create(t->destination,
2732 path, 2739 path,
2733 off, 2740 off,
2734 ct, 2741 ct,
2735 &connection_ready_cb, 2742 &connection_ready_cb,
2736 ct); 2743 ct);
2737 2744
2738 /* FIXME: schedule job to kill connection (and path?) if it takes 2745 /* FIXME: schedule job to kill connection (and path?) if it takes
2739 too long to get ready! (And track performance data on how long 2746 too long to get ready! (And track performance data on how long
2740 other connections took with the tunnel!) 2747 other connections took with the tunnel!)
2741 => Note: to be done within 'connection'-logic! */ 2748 => Note: to be done within 'connection'-logic! */
2742 GNUNET_CONTAINER_DLL_insert (t->connection_busy_head, 2749 GNUNET_CONTAINER_DLL_insert(t->connection_busy_head,
2743 t->connection_busy_tail, 2750 t->connection_busy_tail,
2744 ct); 2751 ct);
2745 t->num_busy_connections++; 2752 t->num_busy_connections++;
2746 LOG (GNUNET_ERROR_TYPE_DEBUG, 2753 LOG(GNUNET_ERROR_TYPE_DEBUG,
2747 "Found interesting path %s for %s, created %s\n", 2754 "Found interesting path %s for %s, created %s\n",
2748 GCPP_2s (path), 2755 GCPP_2s(path),
2749 GCT_2s (t), 2756 GCT_2s(t),
2750 GCC_2s (ct->cc)); 2757 GCC_2s(ct->cc));
2751 return GNUNET_YES; 2758 return GNUNET_YES;
2752} 2759}
2753 2760
@@ -2766,16 +2773,16 @@ consider_path_cb (void *cls,
2766 * @param cls the `struct CadetTunnel` 2773 * @param cls the `struct CadetTunnel`
2767 */ 2774 */
2768static void 2775static void
2769maintain_connections_cb (void *cls) 2776maintain_connections_cb(void *cls)
2770{ 2777{
2771 struct CadetTunnel *t = cls; 2778 struct CadetTunnel *t = cls;
2772 struct GNUNET_TIME_Relative delay; 2779 struct GNUNET_TIME_Relative delay;
2773 struct EvaluationSummary es; 2780 struct EvaluationSummary es;
2774 2781
2775 t->maintain_connections_task = NULL; 2782 t->maintain_connections_task = NULL;
2776 LOG (GNUNET_ERROR_TYPE_DEBUG, 2783 LOG(GNUNET_ERROR_TYPE_DEBUG,
2777 "Performing connection maintenance for %s.\n", 2784 "Performing connection maintenance for %s.\n",
2778 GCT_2s (t)); 2785 GCT_2s(t));
2779 2786
2780 es.min_length = UINT_MAX; 2787 es.min_length = UINT_MAX;
2781 es.max_length = 0; 2788 es.max_length = 0;
@@ -2784,21 +2791,21 @@ maintain_connections_cb (void *cls)
2784 es.path = NULL; 2791 es.path = NULL;
2785 es.worst = NULL; 2792 es.worst = NULL;
2786 es.duplicate = GNUNET_NO; 2793 es.duplicate = GNUNET_NO;
2787 GCT_iterate_connections (t, 2794 GCT_iterate_connections(t,
2788 &evaluate_connection, 2795 &evaluate_connection,
2789 &es); 2796 &es);
2790 if ( (NULL != es.worst) && 2797 if ((NULL != es.worst) &&
2791 (GCT_count_any_connections (t) > DESIRED_CONNECTIONS_PER_TUNNEL) ) 2798 (GCT_count_any_connections(t) > DESIRED_CONNECTIONS_PER_TUNNEL))
2792 { 2799 {
2793 /* Clear out worst-performing connection 'es.worst'. */ 2800 /* Clear out worst-performing connection 'es.worst'. */
2794 destroy_t_connection (t, 2801 destroy_t_connection(t,
2795 es.worst); 2802 es.worst);
2796 } 2803 }
2797 2804
2798 /* Consider additional paths */ 2805 /* Consider additional paths */
2799 (void) GCP_iterate_paths (t->destination, 2806 (void)GCP_iterate_paths(t->destination,
2800 &consider_path_cb, 2807 &consider_path_cb,
2801 t); 2808 t);
2802 2809
2803 /* FIXME: calculate when to try again based on how well we are doing; 2810 /* FIXME: calculate when to try again based on how well we are doing;
2804 in particular, if we have to few connections, we might be able 2811 in particular, if we have to few connections, we might be able
@@ -2810,9 +2817,9 @@ maintain_connections_cb (void *cls)
2810 and then reduce the frequency accordingly. */ 2817 and then reduce the frequency accordingly. */
2811 delay = GNUNET_TIME_UNIT_MINUTES; 2818 delay = GNUNET_TIME_UNIT_MINUTES;
2812 t->maintain_connections_task 2819 t->maintain_connections_task
2813 = GNUNET_SCHEDULER_add_delayed (delay, 2820 = GNUNET_SCHEDULER_add_delayed(delay,
2814 &maintain_connections_cb, 2821 &maintain_connections_cb,
2815 t); 2822 t);
2816} 2823}
2817 2824
2818 2825
@@ -2825,18 +2832,18 @@ maintain_connections_cb (void *cls)
2825 * @param off offset of the destination on path @a path 2832 * @param off offset of the destination on path @a path
2826 */ 2833 */
2827void 2834void
2828GCT_consider_path (struct CadetTunnel *t, 2835GCT_consider_path(struct CadetTunnel *t,
2829 struct CadetPeerPath *p, 2836 struct CadetPeerPath *p,
2830 unsigned int off) 2837 unsigned int off)
2831{ 2838{
2832 LOG (GNUNET_ERROR_TYPE_DEBUG, 2839 LOG(GNUNET_ERROR_TYPE_DEBUG,
2833 "Considering %s for %s (offset %u)\n", 2840 "Considering %s for %s (offset %u)\n",
2834 GCPP_2s (p), 2841 GCPP_2s(p),
2835 GCT_2s (t), 2842 GCT_2s(t),
2836 off); 2843 off);
2837 (void) consider_path_cb (t, 2844 (void)consider_path_cb(t,
2838 p, 2845 p,
2839 off); 2846 off);
2840} 2847}
2841 2848
2842 2849
@@ -2847,18 +2854,18 @@ GCT_consider_path (struct CadetTunnel *t,
2847 * @param msg the message we received on the tunnel 2854 * @param msg the message we received on the tunnel
2848 */ 2855 */
2849static void 2856static void
2850handle_plaintext_keepalive (void *cls, 2857handle_plaintext_keepalive(void *cls,
2851 const struct GNUNET_MessageHeader *msg) 2858 const struct GNUNET_MessageHeader *msg)
2852{ 2859{
2853 struct CadetTunnel *t = cls; 2860 struct CadetTunnel *t = cls;
2854 2861
2855 LOG (GNUNET_ERROR_TYPE_DEBUG, 2862 LOG(GNUNET_ERROR_TYPE_DEBUG,
2856 "Received KEEPALIVE on %s\n", 2863 "Received KEEPALIVE on %s\n",
2857 GCT_2s (t)); 2864 GCT_2s(t));
2858 GNUNET_STATISTICS_update (stats, 2865 GNUNET_STATISTICS_update(stats,
2859 "# keepalives received", 2866 "# keepalives received",
2860 1, 2867 1,
2861 GNUNET_NO); 2868 GNUNET_NO);
2862} 2869}
2863 2870
2864 2871
@@ -2870,8 +2877,8 @@ handle_plaintext_keepalive (void *cls,
2870 * @return #GNUNET_OK (any variable-size payload goes) 2877 * @return #GNUNET_OK (any variable-size payload goes)
2871 */ 2878 */
2872static int 2879static int
2873check_plaintext_data (void *cls, 2880check_plaintext_data(void *cls,
2874 const struct GNUNET_CADET_ChannelAppDataMessage *msg) 2881 const struct GNUNET_CADET_ChannelAppDataMessage *msg)
2875{ 2882{
2876 return GNUNET_OK; 2883 return GNUNET_OK;
2877} 2884}
@@ -2885,29 +2892,29 @@ check_plaintext_data (void *cls,
2885 * @param msg the message we received on the tunnel 2892 * @param msg the message we received on the tunnel
2886 */ 2893 */
2887static void 2894static void
2888handle_plaintext_data (void *cls, 2895handle_plaintext_data(void *cls,
2889 const struct GNUNET_CADET_ChannelAppDataMessage *msg) 2896 const struct GNUNET_CADET_ChannelAppDataMessage *msg)
2890{ 2897{
2891 struct CadetTunnel *t = cls; 2898 struct CadetTunnel *t = cls;
2892 struct CadetChannel *ch; 2899 struct CadetChannel *ch;
2893 2900
2894 ch = lookup_channel (t, 2901 ch = lookup_channel(t,
2895 msg->ctn); 2902 msg->ctn);
2896 if (NULL == ch) 2903 if (NULL == ch)
2897 { 2904 {
2898 /* We don't know about such a channel, might have been destroyed on our 2905 /* We don't know about such a channel, might have been destroyed on our
2899 end in the meantime, or never existed. Send back a DESTROY. */ 2906 end in the meantime, or never existed. Send back a DESTROY. */
2900 LOG (GNUNET_ERROR_TYPE_DEBUG, 2907 LOG(GNUNET_ERROR_TYPE_DEBUG,
2901 "Received %u bytes of application data for unknown channel %u, sending DESTROY\n", 2908 "Received %u bytes of application data for unknown channel %u, sending DESTROY\n",
2902 (unsigned int) (ntohs (msg->header.size) - sizeof (*msg)), 2909 (unsigned int)(ntohs(msg->header.size) - sizeof(*msg)),
2903 ntohl (msg->ctn.cn)); 2910 ntohl(msg->ctn.cn));
2904 GCT_send_channel_destroy (t, 2911 GCT_send_channel_destroy(t,
2905 msg->ctn); 2912 msg->ctn);
2906 return; 2913 return;
2907 } 2914 }
2908 GCCH_handle_channel_plaintext_data (ch, 2915 GCCH_handle_channel_plaintext_data(ch,
2909 GCC_get_id (t->current_ct->cc), 2916 GCC_get_id(t->current_ct->cc),
2910 msg); 2917 msg);
2911} 2918}
2912 2919
2913 2920
@@ -2920,28 +2927,28 @@ handle_plaintext_data (void *cls,
2920 * @param ack the message we received on the tunnel 2927 * @param ack the message we received on the tunnel
2921 */ 2928 */
2922static void 2929static void
2923handle_plaintext_data_ack (void *cls, 2930handle_plaintext_data_ack(void *cls,
2924 const struct GNUNET_CADET_ChannelDataAckMessage *ack) 2931 const struct GNUNET_CADET_ChannelDataAckMessage *ack)
2925{ 2932{
2926 struct CadetTunnel *t = cls; 2933 struct CadetTunnel *t = cls;
2927 struct CadetChannel *ch; 2934 struct CadetChannel *ch;
2928 2935
2929 ch = lookup_channel (t, 2936 ch = lookup_channel(t,
2930 ack->ctn); 2937 ack->ctn);
2931 if (NULL == ch) 2938 if (NULL == ch)
2932 { 2939 {
2933 /* We don't know about such a channel, might have been destroyed on our 2940 /* We don't know about such a channel, might have been destroyed on our
2934 end in the meantime, or never existed. Send back a DESTROY. */ 2941 end in the meantime, or never existed. Send back a DESTROY. */
2935 LOG (GNUNET_ERROR_TYPE_DEBUG, 2942 LOG(GNUNET_ERROR_TYPE_DEBUG,
2936 "Received DATA_ACK for unknown channel %u, sending DESTROY\n", 2943 "Received DATA_ACK for unknown channel %u, sending DESTROY\n",
2937 ntohl (ack->ctn.cn)); 2944 ntohl(ack->ctn.cn));
2938 GCT_send_channel_destroy (t, 2945 GCT_send_channel_destroy(t,
2939 ack->ctn); 2946 ack->ctn);
2940 return; 2947 return;
2941 } 2948 }
2942 GCCH_handle_channel_plaintext_data_ack (ch, 2949 GCCH_handle_channel_plaintext_data_ack(ch,
2943 GCC_get_id (t->current_ct->cc), 2950 GCC_get_id(t->current_ct->cc),
2944 ack); 2951 ack);
2945} 2952}
2946 2953
2947 2954
@@ -2953,43 +2960,43 @@ handle_plaintext_data_ack (void *cls,
2953 * @param copen the message we received on the tunnel 2960 * @param copen the message we received on the tunnel
2954 */ 2961 */
2955static void 2962static void
2956handle_plaintext_channel_open (void *cls, 2963handle_plaintext_channel_open(void *cls,
2957 const struct GNUNET_CADET_ChannelOpenMessage *copen) 2964 const struct GNUNET_CADET_ChannelOpenMessage *copen)
2958{ 2965{
2959 struct CadetTunnel *t = cls; 2966 struct CadetTunnel *t = cls;
2960 struct CadetChannel *ch; 2967 struct CadetChannel *ch;
2961 2968
2962 ch = GNUNET_CONTAINER_multihashmap32_get (t->channels, 2969 ch = GNUNET_CONTAINER_multihashmap32_get(t->channels,
2963 ntohl (copen->ctn.cn)); 2970 ntohl(copen->ctn.cn));
2964 if (NULL != ch) 2971 if (NULL != ch)
2965 { 2972 {
2966 LOG (GNUNET_ERROR_TYPE_DEBUG, 2973 LOG(GNUNET_ERROR_TYPE_DEBUG,
2967 "Received duplicate channel CHANNEL_OPEN on h_port %s from %s (%s), resending ACK\n", 2974 "Received duplicate channel CHANNEL_OPEN on h_port %s from %s (%s), resending ACK\n",
2968 GNUNET_h2s (&copen->h_port), 2975 GNUNET_h2s(&copen->h_port),
2969 GCT_2s (t), 2976 GCT_2s(t),
2970 GCCH_2s (ch)); 2977 GCCH_2s(ch));
2971 GCCH_handle_duplicate_open (ch, 2978 GCCH_handle_duplicate_open(ch,
2972 GCC_get_id (t->current_ct->cc)); 2979 GCC_get_id(t->current_ct->cc));
2973 return; 2980 return;
2974 } 2981 }
2975 LOG (GNUNET_ERROR_TYPE_DEBUG, 2982 LOG(GNUNET_ERROR_TYPE_DEBUG,
2976 "Received CHANNEL_OPEN on h_port %s from %s\n", 2983 "Received CHANNEL_OPEN on h_port %s from %s\n",
2977 GNUNET_h2s (&copen->h_port), 2984 GNUNET_h2s(&copen->h_port),
2978 GCT_2s (t)); 2985 GCT_2s(t));
2979 ch = GCCH_channel_incoming_new (t, 2986 ch = GCCH_channel_incoming_new(t,
2980 copen->ctn, 2987 copen->ctn,
2981 &copen->h_port, 2988 &copen->h_port,
2982 ntohl (copen->opt)); 2989 ntohl(copen->opt));
2983 if (NULL != t->destroy_task) 2990 if (NULL != t->destroy_task)
2984 { 2991 {
2985 GNUNET_SCHEDULER_cancel (t->destroy_task); 2992 GNUNET_SCHEDULER_cancel(t->destroy_task);
2986 t->destroy_task = NULL; 2993 t->destroy_task = NULL;
2987 } 2994 }
2988 GNUNET_assert (GNUNET_OK == 2995 GNUNET_assert(GNUNET_OK ==
2989 GNUNET_CONTAINER_multihashmap32_put (t->channels, 2996 GNUNET_CONTAINER_multihashmap32_put(t->channels,
2990 ntohl (copen->ctn.cn), 2997 ntohl(copen->ctn.cn),
2991 ch, 2998 ch,
2992 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 2999 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2993} 3000}
2994 3001
2995 3002
@@ -3000,22 +3007,22 @@ handle_plaintext_channel_open (void *cls,
3000 * @param ctn ID of the channel to destroy 3007 * @param ctn ID of the channel to destroy
3001 */ 3008 */
3002void 3009void
3003GCT_send_channel_destroy (struct CadetTunnel *t, 3010GCT_send_channel_destroy(struct CadetTunnel *t,
3004 struct GNUNET_CADET_ChannelTunnelNumber ctn) 3011 struct GNUNET_CADET_ChannelTunnelNumber ctn)
3005{ 3012{
3006 struct GNUNET_CADET_ChannelDestroyMessage msg; 3013 struct GNUNET_CADET_ChannelDestroyMessage msg;
3007 3014
3008 LOG (GNUNET_ERROR_TYPE_DEBUG, 3015 LOG(GNUNET_ERROR_TYPE_DEBUG,
3009 "Sending DESTORY message for channel ID %u\n", 3016 "Sending DESTORY message for channel ID %u\n",
3010 ntohl (ctn.cn)); 3017 ntohl(ctn.cn));
3011 msg.header.size = htons (sizeof (msg)); 3018 msg.header.size = htons(sizeof(msg));
3012 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY); 3019 msg.header.type = htons(GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
3013 msg.reserved = htonl (0); 3020 msg.reserved = htonl(0);
3014 msg.ctn = ctn; 3021 msg.ctn = ctn;
3015 GCT_send (t, 3022 GCT_send(t,
3016 &msg.header, 3023 &msg.header,
3017 NULL, 3024 NULL,
3018 NULL); 3025 NULL);
3019} 3026}
3020 3027
3021 3028
@@ -3028,32 +3035,32 @@ GCT_send_channel_destroy (struct CadetTunnel *t,
3028 * @param cm the message we received on the tunnel 3035 * @param cm the message we received on the tunnel
3029 */ 3036 */
3030static void 3037static void
3031handle_plaintext_channel_open_ack (void *cls, 3038handle_plaintext_channel_open_ack(void *cls,
3032 const struct GNUNET_CADET_ChannelOpenAckMessage *cm) 3039 const struct GNUNET_CADET_ChannelOpenAckMessage *cm)
3033{ 3040{
3034 struct CadetTunnel *t = cls; 3041 struct CadetTunnel *t = cls;
3035 struct CadetChannel *ch; 3042 struct CadetChannel *ch;
3036 3043
3037 ch = lookup_channel (t, 3044 ch = lookup_channel(t,
3038 cm->ctn); 3045 cm->ctn);
3039 if (NULL == ch) 3046 if (NULL == ch)
3040 { 3047 {
3041 /* We don't know about such a channel, might have been destroyed on our 3048 /* We don't know about such a channel, might have been destroyed on our
3042 end in the meantime, or never existed. Send back a DESTROY. */ 3049 end in the meantime, or never existed. Send back a DESTROY. */
3043 LOG (GNUNET_ERROR_TYPE_DEBUG, 3050 LOG(GNUNET_ERROR_TYPE_DEBUG,
3044 "Received channel OPEN_ACK for unknown channel %u, sending DESTROY\n", 3051 "Received channel OPEN_ACK for unknown channel %u, sending DESTROY\n",
3045 ntohl (cm->ctn.cn)); 3052 ntohl(cm->ctn.cn));
3046 GCT_send_channel_destroy (t, 3053 GCT_send_channel_destroy(t,
3047 cm->ctn); 3054 cm->ctn);
3048 return; 3055 return;
3049 } 3056 }
3050 LOG (GNUNET_ERROR_TYPE_DEBUG, 3057 LOG(GNUNET_ERROR_TYPE_DEBUG,
3051 "Received channel OPEN_ACK on channel %s from %s\n", 3058 "Received channel OPEN_ACK on channel %s from %s\n",
3052 GCCH_2s (ch), 3059 GCCH_2s(ch),
3053 GCT_2s (t)); 3060 GCT_2s(t));
3054 GCCH_handle_channel_open_ack (ch, 3061 GCCH_handle_channel_open_ack(ch,
3055 GCC_get_id (t->current_ct->cc), 3062 GCC_get_id(t->current_ct->cc),
3056 &cm->port); 3063 &cm->port);
3057} 3064}
3058 3065
3059 3066
@@ -3065,29 +3072,29 @@ handle_plaintext_channel_open_ack (void *cls,
3065 * @param cm the message we received on the tunnel 3072 * @param cm the message we received on the tunnel
3066 */ 3073 */
3067static void 3074static void
3068handle_plaintext_channel_destroy (void *cls, 3075handle_plaintext_channel_destroy(void *cls,
3069 const struct GNUNET_CADET_ChannelDestroyMessage *cm) 3076 const struct GNUNET_CADET_ChannelDestroyMessage *cm)
3070{ 3077{
3071 struct CadetTunnel *t = cls; 3078 struct CadetTunnel *t = cls;
3072 struct CadetChannel *ch; 3079 struct CadetChannel *ch;
3073 3080
3074 ch = lookup_channel (t, 3081 ch = lookup_channel(t,
3075 cm->ctn); 3082 cm->ctn);
3076 if (NULL == ch) 3083 if (NULL == ch)
3077 { 3084 {
3078 /* We don't know about such a channel, might have been destroyed on our 3085 /* We don't know about such a channel, might have been destroyed on our
3079 end in the meantime, or never existed. */ 3086 end in the meantime, or never existed. */
3080 LOG (GNUNET_ERROR_TYPE_DEBUG, 3087 LOG(GNUNET_ERROR_TYPE_DEBUG,
3081 "Received channel DESTORY for unknown channel %u. Ignoring.\n", 3088 "Received channel DESTORY for unknown channel %u. Ignoring.\n",
3082 ntohl (cm->ctn.cn)); 3089 ntohl(cm->ctn.cn));
3083 return; 3090 return;
3084 } 3091 }
3085 LOG (GNUNET_ERROR_TYPE_DEBUG, 3092 LOG(GNUNET_ERROR_TYPE_DEBUG,
3086 "Received channel DESTROY on %s from %s\n", 3093 "Received channel DESTROY on %s from %s\n",
3087 GCCH_2s (ch), 3094 GCCH_2s(ch),
3088 GCT_2s (t)); 3095 GCT_2s(t));
3089 GCCH_handle_remote_destroy (ch, 3096 GCCH_handle_remote_destroy(ch,
3090 GCC_get_id (t->current_ct->cc)); 3097 GCC_get_id(t->current_ct->cc));
3091} 3098}
3092 3099
3093 3100
@@ -3102,14 +3109,14 @@ handle_plaintext_channel_destroy (void *cls,
3102 * #GNUNET_SYSERR to stop further processing with error 3109 * #GNUNET_SYSERR to stop further processing with error
3103 */ 3110 */
3104static int 3111static int
3105handle_decrypted (void *cls, 3112handle_decrypted(void *cls,
3106 const struct GNUNET_MessageHeader *msg) 3113 const struct GNUNET_MessageHeader *msg)
3107{ 3114{
3108 struct CadetTunnel *t = cls; 3115 struct CadetTunnel *t = cls;
3109 3116
3110 GNUNET_assert (NULL != t->current_ct); 3117 GNUNET_assert(NULL != t->current_ct);
3111 GNUNET_MQ_inject_message (t->mq, 3118 GNUNET_MQ_inject_message(t->mq,
3112 msg); 3119 msg);
3113 return GNUNET_OK; 3120 return GNUNET_OK;
3114} 3121}
3115 3122
@@ -3122,10 +3129,10 @@ handle_decrypted (void *cls,
3122 * @param error error code 3129 * @param error error code
3123 */ 3130 */
3124static void 3131static void
3125decrypted_error_cb (void *cls, 3132decrypted_error_cb(void *cls,
3126 enum GNUNET_MQ_Error error) 3133 enum GNUNET_MQ_Error error)
3127{ 3134{
3128 GNUNET_break_op (0); 3135 GNUNET_break_op(0);
3129} 3136}
3130 3137
3131 3138
@@ -3137,55 +3144,55 @@ decrypted_error_cb (void *cls,
3137 * @return new tunnel to @a destination 3144 * @return new tunnel to @a destination
3138 */ 3145 */
3139struct CadetTunnel * 3146struct CadetTunnel *
3140GCT_create_tunnel (struct CadetPeer *destination) 3147GCT_create_tunnel(struct CadetPeer *destination)
3141{ 3148{
3142 struct CadetTunnel *t = GNUNET_new (struct CadetTunnel); 3149 struct CadetTunnel *t = GNUNET_new(struct CadetTunnel);
3143 struct GNUNET_MQ_MessageHandler handlers[] = { 3150 struct GNUNET_MQ_MessageHandler handlers[] = {
3144 GNUNET_MQ_hd_fixed_size (plaintext_keepalive, 3151 GNUNET_MQ_hd_fixed_size(plaintext_keepalive,
3145 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE, 3152 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE,
3146 struct GNUNET_MessageHeader, 3153 struct GNUNET_MessageHeader,
3147 t), 3154 t),
3148 GNUNET_MQ_hd_var_size (plaintext_data, 3155 GNUNET_MQ_hd_var_size(plaintext_data,
3149 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA, 3156 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA,
3150 struct GNUNET_CADET_ChannelAppDataMessage, 3157 struct GNUNET_CADET_ChannelAppDataMessage,
3151 t), 3158 t),
3152 GNUNET_MQ_hd_fixed_size (plaintext_data_ack, 3159 GNUNET_MQ_hd_fixed_size(plaintext_data_ack,
3153 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK, 3160 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK,
3154 struct GNUNET_CADET_ChannelDataAckMessage, 3161 struct GNUNET_CADET_ChannelDataAckMessage,
3155 t), 3162 t),
3156 GNUNET_MQ_hd_fixed_size (plaintext_channel_open, 3163 GNUNET_MQ_hd_fixed_size(plaintext_channel_open,
3157 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN, 3164 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN,
3158 struct GNUNET_CADET_ChannelOpenMessage, 3165 struct GNUNET_CADET_ChannelOpenMessage,
3159 t), 3166 t),
3160 GNUNET_MQ_hd_fixed_size (plaintext_channel_open_ack, 3167 GNUNET_MQ_hd_fixed_size(plaintext_channel_open_ack,
3161 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK, 3168 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK,
3162 struct GNUNET_CADET_ChannelOpenAckMessage, 3169 struct GNUNET_CADET_ChannelOpenAckMessage,
3163 t), 3170 t),
3164 GNUNET_MQ_hd_fixed_size (plaintext_channel_destroy, 3171 GNUNET_MQ_hd_fixed_size(plaintext_channel_destroy,
3165 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY, 3172 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
3166 struct GNUNET_CADET_ChannelDestroyMessage, 3173 struct GNUNET_CADET_ChannelDestroyMessage,
3167 t), 3174 t),
3168 GNUNET_MQ_handler_end () 3175 GNUNET_MQ_handler_end()
3169 }; 3176 };
3170 3177
3171 t->kx_retry_delay = INITIAL_KX_RETRY_DELAY; 3178 t->kx_retry_delay = INITIAL_KX_RETRY_DELAY;
3172 new_ephemeral (&t->ax); 3179 new_ephemeral(&t->ax);
3173 GNUNET_assert (GNUNET_OK == 3180 GNUNET_assert(GNUNET_OK ==
3174 GNUNET_CRYPTO_ecdhe_key_create2 (&t->ax.kx_0)); 3181 GNUNET_CRYPTO_ecdhe_key_create2(&t->ax.kx_0));
3175 t->destination = destination; 3182 t->destination = destination;
3176 t->channels = GNUNET_CONTAINER_multihashmap32_create (8); 3183 t->channels = GNUNET_CONTAINER_multihashmap32_create(8);
3177 t->maintain_connections_task 3184 t->maintain_connections_task
3178 = GNUNET_SCHEDULER_add_now (&maintain_connections_cb, 3185 = GNUNET_SCHEDULER_add_now(&maintain_connections_cb,
3179 t); 3186 t);
3180 t->mq = GNUNET_MQ_queue_for_callbacks (NULL, 3187 t->mq = GNUNET_MQ_queue_for_callbacks(NULL,
3181 NULL, 3188 NULL,
3182 NULL, 3189 NULL,
3183 NULL, 3190 NULL,
3184 handlers, 3191 handlers,
3185 &decrypted_error_cb, 3192 &decrypted_error_cb,
3186 t); 3193 t);
3187 t->mst = GNUNET_MST_create (&handle_decrypted, 3194 t->mst = GNUNET_MST_create(&handle_decrypted,
3188 t); 3195 t);
3189 return t; 3196 return t;
3190} 3197}
3191 3198
@@ -3201,42 +3208,42 @@ GCT_create_tunnel (struct CadetPeer *destination)
3201 * #GNUNET_SYSERR on failure (duplicate connection) 3208 * #GNUNET_SYSERR on failure (duplicate connection)
3202 */ 3209 */
3203int 3210int
3204GCT_add_inbound_connection (struct CadetTunnel *t, 3211GCT_add_inbound_connection(struct CadetTunnel *t,
3205 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid, 3212 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
3206 struct CadetPeerPath *path) 3213 struct CadetPeerPath *path)
3207{ 3214{
3208 struct CadetTConnection *ct; 3215 struct CadetTConnection *ct;
3209 3216
3210 ct = GNUNET_new (struct CadetTConnection); 3217 ct = GNUNET_new(struct CadetTConnection);
3211 ct->created = GNUNET_TIME_absolute_get (); 3218 ct->created = GNUNET_TIME_absolute_get();
3212 ct->t = t; 3219 ct->t = t;
3213 ct->cc = GCC_create_inbound (t->destination, 3220 ct->cc = GCC_create_inbound(t->destination,
3214 path, 3221 path,
3215 ct, 3222 ct,
3216 cid, 3223 cid,
3217 &connection_ready_cb, 3224 &connection_ready_cb,
3218 ct); 3225 ct);
3219 if (NULL == ct->cc) 3226 if (NULL == ct->cc)
3220 { 3227 {
3221 LOG (GNUNET_ERROR_TYPE_DEBUG, 3228 LOG(GNUNET_ERROR_TYPE_DEBUG,
3222 "%s refused inbound %s (duplicate)\n", 3229 "%s refused inbound %s (duplicate)\n",
3223 GCT_2s (t), 3230 GCT_2s(t),
3224 GCC_2s (ct->cc)); 3231 GCC_2s(ct->cc));
3225 GNUNET_free (ct); 3232 GNUNET_free(ct);
3226 return GNUNET_SYSERR; 3233 return GNUNET_SYSERR;
3227 } 3234 }
3228 /* FIXME: schedule job to kill connection (and path?) if it takes 3235 /* FIXME: schedule job to kill connection (and path?) if it takes
3229 too long to get ready! (And track performance data on how long 3236 too long to get ready! (And track performance data on how long
3230 other connections took with the tunnel!) 3237 other connections took with the tunnel!)
3231 => Note: to be done within 'connection'-logic! */ 3238 => Note: to be done within 'connection'-logic! */
3232 GNUNET_CONTAINER_DLL_insert (t->connection_busy_head, 3239 GNUNET_CONTAINER_DLL_insert(t->connection_busy_head,
3233 t->connection_busy_tail, 3240 t->connection_busy_tail,
3234 ct); 3241 ct);
3235 t->num_busy_connections++; 3242 t->num_busy_connections++;
3236 LOG (GNUNET_ERROR_TYPE_DEBUG, 3243 LOG(GNUNET_ERROR_TYPE_DEBUG,
3237 "%s has new %s\n", 3244 "%s has new %s\n",
3238 GCT_2s (t), 3245 GCT_2s(t),
3239 GCC_2s (ct->cc)); 3246 GCC_2s(ct->cc));
3240 return GNUNET_OK; 3247 return GNUNET_OK;
3241} 3248}
3242 3249
@@ -3248,174 +3255,177 @@ GCT_add_inbound_connection (struct CadetTunnel *t,
3248 * @param msg the encrypted message to decrypt 3255 * @param msg the encrypted message to decrypt
3249 */ 3256 */
3250void 3257void
3251GCT_handle_encrypted (struct CadetTConnection *ct, 3258GCT_handle_encrypted(struct CadetTConnection *ct,
3252 const struct GNUNET_CADET_TunnelEncryptedMessage *msg) 3259 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
3253{ 3260{
3254 struct CadetTunnel *t = ct->t; 3261 struct CadetTunnel *t = ct->t;
3255 uint16_t size = ntohs (msg->header.size); 3262 uint16_t size = ntohs(msg->header.size);
3256 char cbuf [size] GNUNET_ALIGN; 3263 char cbuf [size] GNUNET_ALIGN;
3257 ssize_t decrypted_size; 3264 ssize_t decrypted_size;
3258 3265
3259 LOG (GNUNET_ERROR_TYPE_DEBUG, 3266 LOG(GNUNET_ERROR_TYPE_DEBUG,
3260 "%s received %u bytes of encrypted data in state %d\n", 3267 "%s received %u bytes of encrypted data in state %d\n",
3261 GCT_2s (t), 3268 GCT_2s(t),
3262 (unsigned int) size, 3269 (unsigned int)size,
3263 t->estate); 3270 t->estate);
3264 3271
3265 switch (t->estate) 3272 switch (t->estate)
3266 { 3273 {
3267 case CADET_TUNNEL_KEY_UNINITIALIZED: 3274 case CADET_TUNNEL_KEY_UNINITIALIZED:
3268 case CADET_TUNNEL_KEY_AX_RECV: 3275 case CADET_TUNNEL_KEY_AX_RECV:
3269 /* We did not even SEND our KX, how can the other peer 3276 /* We did not even SEND our KX, how can the other peer
3270 send us encrypted data? Must have been that we went 3277 send us encrypted data? Must have been that we went
3271 down and the other peer still things we are up. 3278 down and the other peer still things we are up.
3272 Let's send it KX back. */ 3279 Let's send it KX back. */
3273 GNUNET_STATISTICS_update (stats, 3280 GNUNET_STATISTICS_update(stats,
3274 "# received encrypted without any KX", 3281 "# received encrypted without any KX",
3275 1, 3282 1,
3276 GNUNET_NO); 3283 GNUNET_NO);
3277 if (NULL != t->kx_task) 3284 if (NULL != t->kx_task)
3278 { 3285 {
3279 GNUNET_SCHEDULER_cancel (t->kx_task); 3286 GNUNET_SCHEDULER_cancel(t->kx_task);
3280 t->kx_task = NULL; 3287 t->kx_task = NULL;
3281 } 3288 }
3282 send_kx (t, 3289 send_kx(t,
3283 ct, 3290 ct,
3284 &t->ax); 3291 &t->ax);
3285 return; 3292 return;
3286 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV: 3293
3287 /* We send KX, and other peer send KX to us at the same time. 3294 case CADET_TUNNEL_KEY_AX_SENT_AND_RECV:
3288 Neither KX is AUTH'ed, so let's try KX_AUTH this time. */ 3295 /* We send KX, and other peer send KX to us at the same time.
3289 GNUNET_STATISTICS_update (stats, 3296 Neither KX is AUTH'ed, so let's try KX_AUTH this time. */
3290 "# received encrypted without KX_AUTH", 3297 GNUNET_STATISTICS_update(stats,
3291 1, 3298 "# received encrypted without KX_AUTH",
3292 GNUNET_NO); 3299 1,
3293 if (NULL != t->kx_task) 3300 GNUNET_NO);
3294 { 3301 if (NULL != t->kx_task)
3295 GNUNET_SCHEDULER_cancel (t->kx_task); 3302 {
3296 t->kx_task = NULL; 3303 GNUNET_SCHEDULER_cancel(t->kx_task);
3297 } 3304 t->kx_task = NULL;
3298 send_kx_auth (t, 3305 }
3299 ct, 3306 send_kx_auth(t,
3300 &t->ax, 3307 ct,
3301 GNUNET_YES); 3308 &t->ax,
3302 return; 3309 GNUNET_YES);
3303 case CADET_TUNNEL_KEY_AX_SENT: 3310 return;
3304 /* We did not get the KX of the other peer, but that 3311
3305 might have been lost. Send our KX again immediately. */ 3312 case CADET_TUNNEL_KEY_AX_SENT:
3306 GNUNET_STATISTICS_update (stats, 3313 /* We did not get the KX of the other peer, but that
3307 "# received encrypted without KX", 3314 might have been lost. Send our KX again immediately. */
3308 1, 3315 GNUNET_STATISTICS_update(stats,
3309 GNUNET_NO); 3316 "# received encrypted without KX",
3310 if (NULL != t->kx_task) 3317 1,
3311 { 3318 GNUNET_NO);
3312 GNUNET_SCHEDULER_cancel (t->kx_task); 3319 if (NULL != t->kx_task)
3313 t->kx_task = NULL; 3320 {
3314 } 3321 GNUNET_SCHEDULER_cancel(t->kx_task);
3315 send_kx (t, 3322 t->kx_task = NULL;
3316 ct, 3323 }
3317 &t->ax); 3324 send_kx(t,
3318 return; 3325 ct,
3319 case CADET_TUNNEL_KEY_AX_AUTH_SENT: 3326 &t->ax);
3327 return;
3328
3329 case CADET_TUNNEL_KEY_AX_AUTH_SENT:
3320 /* Great, first payload, we might graduate to OK! */ 3330 /* Great, first payload, we might graduate to OK! */
3321 case CADET_TUNNEL_KEY_OK: 3331 case CADET_TUNNEL_KEY_OK:
3322 /* We are up and running, all good. */ 3332 /* We are up and running, all good. */
3323 break; 3333 break;
3324 } 3334 }
3325 3335
3326 decrypted_size = -1; 3336 decrypted_size = -1;
3327 if (CADET_TUNNEL_KEY_OK == t->estate) 3337 if (CADET_TUNNEL_KEY_OK == t->estate)
3328 { 3338 {
3329 /* We have well-established key material available, 3339 /* We have well-established key material available,
3330 try that. (This is the common case.) */ 3340 try that. (This is the common case.) */
3331 decrypted_size = t_ax_decrypt_and_validate (&t->ax, 3341 decrypted_size = t_ax_decrypt_and_validate(&t->ax,
3332 cbuf, 3342 cbuf,
3333 msg, 3343 msg,
3334 size); 3344 size);
3335 }
3336
3337 if ( (-1 == decrypted_size) &&
3338 (NULL != t->unverified_ax) )
3339 {
3340 /* We have un-authenticated KX material available. We should try
3341 this as a back-up option, in case the sender crashed and
3342 switched keys. */
3343 decrypted_size = t_ax_decrypt_and_validate (t->unverified_ax,
3344 cbuf,
3345 msg,
3346 size);
3347 if (-1 != decrypted_size)
3348 {
3349 /* It worked! Treat this as authentication of the AX data! */
3350 cleanup_ax (&t->ax);
3351 t->ax = *t->unverified_ax;
3352 GNUNET_free (t->unverified_ax);
3353 t->unverified_ax = NULL;
3354 } 3345 }
3355 if (CADET_TUNNEL_KEY_AX_AUTH_SENT == t->estate) 3346
3347 if ((-1 == decrypted_size) &&
3348 (NULL != t->unverified_ax))
3356 { 3349 {
3357 /* First time it worked, move tunnel into production! */ 3350 /* We have un-authenticated KX material available. We should try
3358 GCT_change_estate (t, 3351 this as a back-up option, in case the sender crashed and
3359 CADET_TUNNEL_KEY_OK); 3352 switched keys. */
3360 if (NULL != t->send_task) 3353 decrypted_size = t_ax_decrypt_and_validate(t->unverified_ax,
3361 GNUNET_SCHEDULER_cancel (t->send_task); 3354 cbuf,
3362 t->send_task = GNUNET_SCHEDULER_add_now (&trigger_transmissions, 3355 msg,
3363 t); 3356 size);
3357 if (-1 != decrypted_size)
3358 {
3359 /* It worked! Treat this as authentication of the AX data! */
3360 cleanup_ax(&t->ax);
3361 t->ax = *t->unverified_ax;
3362 GNUNET_free(t->unverified_ax);
3363 t->unverified_ax = NULL;
3364 }
3365 if (CADET_TUNNEL_KEY_AX_AUTH_SENT == t->estate)
3366 {
3367 /* First time it worked, move tunnel into production! */
3368 GCT_change_estate(t,
3369 CADET_TUNNEL_KEY_OK);
3370 if (NULL != t->send_task)
3371 GNUNET_SCHEDULER_cancel(t->send_task);
3372 t->send_task = GNUNET_SCHEDULER_add_now(&trigger_transmissions,
3373 t);
3374 }
3364 } 3375 }
3365 }
3366 if (NULL != t->unverified_ax) 3376 if (NULL != t->unverified_ax)
3367 { 3377 {
3368 /* We had unverified KX material that was useless; so increment 3378 /* We had unverified KX material that was useless; so increment
3369 counter and eventually move to ignore it. Note that we even do 3379 counter and eventually move to ignore it. Note that we even do
3370 this increment if we successfully decrypted with the old KX 3380 this increment if we successfully decrypted with the old KX
3371 material and thus didn't even both with the new one. This is 3381 material and thus didn't even both with the new one. This is
3372 the ideal case, as a malicious injection of bogus KX data 3382 the ideal case, as a malicious injection of bogus KX data
3373 basically only causes us to increment a counter a few times. */ 3383 basically only causes us to increment a counter a few times. */
3374 t->unverified_attempts++; 3384 t->unverified_attempts++;
3375 LOG (GNUNET_ERROR_TYPE_DEBUG, 3385 LOG(GNUNET_ERROR_TYPE_DEBUG,
3376 "Failed to decrypt message with unverified KX data %u times\n", 3386 "Failed to decrypt message with unverified KX data %u times\n",
3377 t->unverified_attempts); 3387 t->unverified_attempts);
3378 if (t->unverified_attempts > MAX_UNVERIFIED_ATTEMPTS) 3388 if (t->unverified_attempts > MAX_UNVERIFIED_ATTEMPTS)
3379 { 3389 {
3380 cleanup_ax (t->unverified_ax); 3390 cleanup_ax(t->unverified_ax);
3381 GNUNET_free (t->unverified_ax); 3391 GNUNET_free(t->unverified_ax);
3382 t->unverified_ax = NULL; 3392 t->unverified_ax = NULL;
3393 }
3383 } 3394 }
3384 }
3385 3395
3386 if (-1 == decrypted_size) 3396 if (-1 == decrypted_size)
3387 { 3397 {
3388 /* Decryption failed for good, complain. */ 3398 /* Decryption failed for good, complain. */
3389 LOG (GNUNET_ERROR_TYPE_WARNING, 3399 LOG(GNUNET_ERROR_TYPE_WARNING,
3390 "%s failed to decrypt and validate encrypted data, retrying KX\n", 3400 "%s failed to decrypt and validate encrypted data, retrying KX\n",
3391 GCT_2s (t)); 3401 GCT_2s(t));
3392 GNUNET_STATISTICS_update (stats, 3402 GNUNET_STATISTICS_update(stats,
3393 "# unable to decrypt", 3403 "# unable to decrypt",
3394 1, 3404 1,
3395 GNUNET_NO); 3405 GNUNET_NO);
3396 if (NULL != t->kx_task) 3406 if (NULL != t->kx_task)
3397 { 3407 {
3398 GNUNET_SCHEDULER_cancel (t->kx_task); 3408 GNUNET_SCHEDULER_cancel(t->kx_task);
3399 t->kx_task = NULL; 3409 t->kx_task = NULL;
3410 }
3411 send_kx(t,
3412 ct,
3413 &t->ax);
3414 return;
3400 } 3415 }
3401 send_kx (t, 3416 GNUNET_STATISTICS_update(stats,
3402 ct, 3417 "# decrypted bytes",
3403 &t->ax); 3418 decrypted_size,
3404 return; 3419 GNUNET_NO);
3405 }
3406 GNUNET_STATISTICS_update (stats,
3407 "# decrypted bytes",
3408 decrypted_size,
3409 GNUNET_NO);
3410 3420
3411 /* The MST will ultimately call #handle_decrypted() on each message. */ 3421 /* The MST will ultimately call #handle_decrypted() on each message. */
3412 t->current_ct = ct; 3422 t->current_ct = ct;
3413 GNUNET_break_op (GNUNET_OK == 3423 GNUNET_break_op(GNUNET_OK ==
3414 GNUNET_MST_from_buffer (t->mst, 3424 GNUNET_MST_from_buffer(t->mst,
3415 cbuf, 3425 cbuf,
3416 decrypted_size, 3426 decrypted_size,
3417 GNUNET_YES, 3427 GNUNET_YES,
3418 GNUNET_NO)); 3428 GNUNET_NO));
3419 t->current_ct = NULL; 3429 t->current_ct = NULL;
3420} 3430}
3421 3431
@@ -3431,10 +3441,10 @@ GCT_handle_encrypted (struct CadetTConnection *ct,
3431 * @return Handle to cancel message 3441 * @return Handle to cancel message
3432 */ 3442 */
3433struct CadetTunnelQueueEntry * 3443struct CadetTunnelQueueEntry *
3434GCT_send (struct CadetTunnel *t, 3444GCT_send(struct CadetTunnel *t,
3435 const struct GNUNET_MessageHeader *message, 3445 const struct GNUNET_MessageHeader *message,
3436 GCT_SendContinuation cont, 3446 GCT_SendContinuation cont,
3437 void *cont_cls) 3447 void *cont_cls)
3438{ 3448{
3439 struct CadetTunnelQueueEntry *tq; 3449 struct CadetTunnelQueueEntry *tq;
3440 uint16_t payload_size; 3450 uint16_t payload_size;
@@ -3442,55 +3452,55 @@ GCT_send (struct CadetTunnel *t,
3442 struct GNUNET_CADET_TunnelEncryptedMessage *ax_msg; 3452 struct GNUNET_CADET_TunnelEncryptedMessage *ax_msg;
3443 3453
3444 if (CADET_TUNNEL_KEY_OK != t->estate) 3454 if (CADET_TUNNEL_KEY_OK != t->estate)
3445 { 3455 {
3446 GNUNET_break (0); 3456 GNUNET_break(0);
3447 return NULL; 3457 return NULL;
3448 } 3458 }
3449 payload_size = ntohs (message->size); 3459 payload_size = ntohs(message->size);
3450 LOG (GNUNET_ERROR_TYPE_DEBUG, 3460 LOG(GNUNET_ERROR_TYPE_DEBUG,
3451 "Encrypting %u bytes for %s\n", 3461 "Encrypting %u bytes for %s\n",
3452 (unsigned int) payload_size, 3462 (unsigned int)payload_size,
3453 GCT_2s (t)); 3463 GCT_2s(t));
3454 env = GNUNET_MQ_msg_extra (ax_msg, 3464 env = GNUNET_MQ_msg_extra(ax_msg,
3455 payload_size,
3456 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED);
3457 t_ax_encrypt (&t->ax,
3458 &ax_msg[1],
3459 message,
3460 payload_size);
3461 GNUNET_STATISTICS_update (stats,
3462 "# encrypted bytes",
3463 payload_size, 3465 payload_size,
3464 GNUNET_NO); 3466 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED);
3465 ax_msg->ax_header.Ns = htonl (t->ax.Ns++); 3467 t_ax_encrypt(&t->ax,
3466 ax_msg->ax_header.PNs = htonl (t->ax.PNs); 3468 &ax_msg[1],
3469 message,
3470 payload_size);
3471 GNUNET_STATISTICS_update(stats,
3472 "# encrypted bytes",
3473 payload_size,
3474 GNUNET_NO);
3475 ax_msg->ax_header.Ns = htonl(t->ax.Ns++);
3476 ax_msg->ax_header.PNs = htonl(t->ax.PNs);
3467 /* FIXME: we should do this once, not once per message; 3477 /* FIXME: we should do this once, not once per message;
3468 this is a point multiplication, and DHRs does not 3478 this is a point multiplication, and DHRs does not
3469 change all the time. */ 3479 change all the time. */
3470 GNUNET_CRYPTO_ecdhe_key_get_public (&t->ax.DHRs, 3480 GNUNET_CRYPTO_ecdhe_key_get_public(&t->ax.DHRs,
3471 &ax_msg->ax_header.DHRs); 3481 &ax_msg->ax_header.DHRs);
3472 t_h_encrypt (&t->ax, 3482 t_h_encrypt(&t->ax,
3473 ax_msg); 3483 ax_msg);
3474 t_hmac (&ax_msg->ax_header, 3484 t_hmac(&ax_msg->ax_header,
3475 sizeof (struct GNUNET_CADET_AxHeader) + payload_size, 3485 sizeof(struct GNUNET_CADET_AxHeader) + payload_size,
3476 0, 3486 0,
3477 &t->ax.HKs, 3487 &t->ax.HKs,
3478 &ax_msg->hmac); 3488 &ax_msg->hmac);
3479 3489
3480 tq = GNUNET_malloc (sizeof (*tq)); 3490 tq = GNUNET_malloc(sizeof(*tq));
3481 tq->t = t; 3491 tq->t = t;
3482 tq->env = env; 3492 tq->env = env;
3483 tq->cid = &ax_msg->cid; /* will initialize 'ax_msg->cid' once we know the connection */ 3493 tq->cid = &ax_msg->cid; /* will initialize 'ax_msg->cid' once we know the connection */
3484 tq->cont = cont; 3494 tq->cont = cont;
3485 tq->cont_cls = cont_cls; 3495 tq->cont_cls = cont_cls;
3486 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head, 3496 GNUNET_CONTAINER_DLL_insert_tail(t->tq_head,
3487 t->tq_tail, 3497 t->tq_tail,
3488 tq); 3498 tq);
3489 if (NULL != t->send_task) 3499 if (NULL != t->send_task)
3490 GNUNET_SCHEDULER_cancel (t->send_task); 3500 GNUNET_SCHEDULER_cancel(t->send_task);
3491 t->send_task 3501 t->send_task
3492 = GNUNET_SCHEDULER_add_now (&trigger_transmissions, 3502 = GNUNET_SCHEDULER_add_now(&trigger_transmissions,
3493 t); 3503 t);
3494 return tq; 3504 return tq;
3495} 3505}
3496 3506
@@ -3505,15 +3515,15 @@ GCT_send (struct CadetTunnel *t,
3505 * @param tq Handle to the queue entry to cancel. 3515 * @param tq Handle to the queue entry to cancel.
3506 */ 3516 */
3507void 3517void
3508GCT_send_cancel (struct CadetTunnelQueueEntry *tq) 3518GCT_send_cancel(struct CadetTunnelQueueEntry *tq)
3509{ 3519{
3510 struct CadetTunnel *t = tq->t; 3520 struct CadetTunnel *t = tq->t;
3511 3521
3512 GNUNET_CONTAINER_DLL_remove (t->tq_head, 3522 GNUNET_CONTAINER_DLL_remove(t->tq_head,
3513 t->tq_tail, 3523 t->tq_tail,
3514 tq); 3524 tq);
3515 GNUNET_MQ_discard (tq->env); 3525 GNUNET_MQ_discard(tq->env);
3516 GNUNET_free (tq); 3526 GNUNET_free(tq);
3517} 3527}
3518 3528
3519 3529
@@ -3525,35 +3535,35 @@ GCT_send_cancel (struct CadetTunnelQueueEntry *tq)
3525 * @param iter_cls Closure for @c iter. 3535 * @param iter_cls Closure for @c iter.
3526 */ 3536 */
3527void 3537void
3528GCT_iterate_connections (struct CadetTunnel *t, 3538GCT_iterate_connections(struct CadetTunnel *t,
3529 GCT_ConnectionIterator iter, 3539 GCT_ConnectionIterator iter,
3530 void *iter_cls) 3540 void *iter_cls)
3531{ 3541{
3532 struct CadetTConnection *n; 3542 struct CadetTConnection *n;
3543
3533 for (struct CadetTConnection *ct = t->connection_ready_head; 3544 for (struct CadetTConnection *ct = t->connection_ready_head;
3534 NULL != ct; 3545 NULL != ct;
3535 ct = n) 3546 ct = n)
3536 { 3547 {
3537 n = ct->next; 3548 n = ct->next;
3538 iter (iter_cls, 3549 iter(iter_cls,
3539 ct); 3550 ct);
3540 } 3551 }
3541 for (struct CadetTConnection *ct = t->connection_busy_head; 3552 for (struct CadetTConnection *ct = t->connection_busy_head;
3542 NULL != ct; 3553 NULL != ct;
3543 ct = n) 3554 ct = n)
3544 { 3555 {
3545 n = ct->next; 3556 n = ct->next;
3546 iter (iter_cls, 3557 iter(iter_cls,
3547 ct); 3558 ct);
3548 } 3559 }
3549} 3560}
3550 3561
3551 3562
3552/** 3563/**
3553 * Closure for #iterate_channels_cb. 3564 * Closure for #iterate_channels_cb.
3554 */ 3565 */
3555struct ChanIterCls 3566struct ChanIterCls {
3556{
3557 /** 3567 /**
3558 * Function to call. 3568 * Function to call.
3559 */ 3569 */
@@ -3575,15 +3585,15 @@ struct ChanIterCls
3575 * @return #GNUNET_OK 3585 * @return #GNUNET_OK
3576 */ 3586 */
3577static int 3587static int
3578iterate_channels_cb (void *cls, 3588iterate_channels_cb(void *cls,
3579 uint32_t key, 3589 uint32_t key,
3580 void *value) 3590 void *value)
3581{ 3591{
3582 struct ChanIterCls *ctx = cls; 3592 struct ChanIterCls *ctx = cls;
3583 struct CadetChannel *ch = value; 3593 struct CadetChannel *ch = value;
3584 3594
3585 ctx->iter (ctx->iter_cls, 3595 ctx->iter(ctx->iter_cls,
3586 ch); 3596 ch);
3587 return GNUNET_OK; 3597 return GNUNET_OK;
3588} 3598}
3589 3599
@@ -3596,18 +3606,17 @@ iterate_channels_cb (void *cls,
3596 * @param iter_cls Closure for @c iter. 3606 * @param iter_cls Closure for @c iter.
3597 */ 3607 */
3598void 3608void
3599GCT_iterate_channels (struct CadetTunnel *t, 3609GCT_iterate_channels(struct CadetTunnel *t,
3600 GCT_ChannelIterator iter, 3610 GCT_ChannelIterator iter,
3601 void *iter_cls) 3611 void *iter_cls)
3602{ 3612{
3603 struct ChanIterCls ctx; 3613 struct ChanIterCls ctx;
3604 3614
3605 ctx.iter = iter; 3615 ctx.iter = iter;
3606 ctx.iter_cls = iter_cls; 3616 ctx.iter_cls = iter_cls;
3607 GNUNET_CONTAINER_multihashmap32_iterate (t->channels, 3617 GNUNET_CONTAINER_multihashmap32_iterate(t->channels,
3608 &iterate_channels_cb, 3618 &iterate_channels_cb,
3609 &ctx); 3619 &ctx);
3610
3611} 3620}
3612 3621
3613 3622
@@ -3620,19 +3629,19 @@ GCT_iterate_channels (struct CadetTunnel *t,
3620 * @return #GNUNET_OK (continue iteration) 3629 * @return #GNUNET_OK (continue iteration)
3621 */ 3630 */
3622static int 3631static int
3623debug_channel (void *cls, 3632debug_channel(void *cls,
3624 uint32_t key, 3633 uint32_t key,
3625 void *value) 3634 void *value)
3626{ 3635{
3627 const enum GNUNET_ErrorType *level = cls; 3636 const enum GNUNET_ErrorType *level = cls;
3628 struct CadetChannel *ch = value; 3637 struct CadetChannel *ch = value;
3629 3638
3630 GCCH_debug (ch, *level); 3639 GCCH_debug(ch, *level);
3631 return GNUNET_OK; 3640 return GNUNET_OK;
3632} 3641}
3633 3642
3634 3643
3635#define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-tun",__VA_ARGS__) 3644#define LOG2(level, ...) GNUNET_log_from_nocheck(level, "cadet-tun", __VA_ARGS__)
3636 3645
3637 3646
3638/** 3647/**
@@ -3642,41 +3651,41 @@ debug_channel (void *cls,
3642 * @param level Debug level to use. 3651 * @param level Debug level to use.
3643 */ 3652 */
3644void 3653void
3645GCT_debug (const struct CadetTunnel *t, 3654GCT_debug(const struct CadetTunnel *t,
3646 enum GNUNET_ErrorType level) 3655 enum GNUNET_ErrorType level)
3647{ 3656{
3648#if !defined(GNUNET_CULL_LOGGING) 3657#if !defined(GNUNET_CULL_LOGGING)
3649 struct CadetTConnection *iter_c; 3658 struct CadetTConnection *iter_c;
3650 int do_log; 3659 int do_log;
3651 3660
3652 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK), 3661 do_log = GNUNET_get_log_call_status(level & (~GNUNET_ERROR_TYPE_BULK),
3653 "cadet-tun", 3662 "cadet-tun",
3654 __FILE__, __FUNCTION__, __LINE__); 3663 __FILE__, __FUNCTION__, __LINE__);
3655 if (0 == do_log) 3664 if (0 == do_log)
3656 return; 3665 return;
3657 3666
3658 LOG2 (level, 3667 LOG2(level,
3659 "TTT TUNNEL TOWARDS %s in estate %s tq_len: %u #cons: %u\n", 3668 "TTT TUNNEL TOWARDS %s in estate %s tq_len: %u #cons: %u\n",
3660 GCT_2s (t), 3669 GCT_2s(t),
3661 estate2s (t->estate), 3670 estate2s(t->estate),
3662 t->tq_len, 3671 t->tq_len,
3663 GCT_count_any_connections (t)); 3672 GCT_count_any_connections(t));
3664 LOG2 (level, 3673 LOG2(level,
3665 "TTT channels:\n"); 3674 "TTT channels:\n");
3666 GNUNET_CONTAINER_multihashmap32_iterate (t->channels, 3675 GNUNET_CONTAINER_multihashmap32_iterate(t->channels,
3667 &debug_channel, 3676 &debug_channel,
3668 &level); 3677 &level);
3669 LOG2 (level, 3678 LOG2(level,
3670 "TTT connections:\n"); 3679 "TTT connections:\n");
3671 for (iter_c = t->connection_ready_head; NULL != iter_c; iter_c = iter_c->next) 3680 for (iter_c = t->connection_ready_head; NULL != iter_c; iter_c = iter_c->next)
3672 GCC_debug (iter_c->cc, 3681 GCC_debug(iter_c->cc,
3673 level); 3682 level);
3674 for (iter_c = t->connection_busy_head; NULL != iter_c; iter_c = iter_c->next) 3683 for (iter_c = t->connection_busy_head; NULL != iter_c; iter_c = iter_c->next)
3675 GCC_debug (iter_c->cc, 3684 GCC_debug(iter_c->cc,
3676 level); 3685 level);
3677 3686
3678 LOG2 (level, 3687 LOG2(level,
3679 "TTT TUNNEL END\n"); 3688 "TTT TUNNEL END\n");
3680#endif 3689#endif
3681} 3690}
3682 3691
diff --git a/src/cadet/gnunet-service-cadet_tunnels.h b/src/cadet/gnunet-service-cadet_tunnels.h
index 47d9f98bf..0df35cd23 100644
--- a/src/cadet/gnunet-service-cadet_tunnels.h
+++ b/src/cadet/gnunet-service-cadet_tunnels.h
@@ -12,12 +12,12 @@
12 WITHOUT ANY WARRANTY; without even the implied warranty of 12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Affero General Public License for more details. 14 Affero General Public License for more details.
15 15
16 You should have received a copy of the GNU Affero General Public License 16 You should have received a copy of the GNU Affero General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18
19 SPDX-License-Identifier: AGPL3.0-or-later 19 SPDX-License-Identifier: AGPL3.0-or-later
20*/ 20 */
21 21
22/** 22/**
23 * @file cadet/gnunet-service-cadet_tunnels.h 23 * @file cadet/gnunet-service-cadet_tunnels.h
@@ -41,8 +41,7 @@
41/** 41/**
42 * All the encryption states a tunnel can be in. 42 * All the encryption states a tunnel can be in.
43 */ 43 */
44enum CadetTunnelEState 44enum CadetTunnelEState {
45{
46 /** 45 /**
47 * Uninitialized status, we need to send KX. We will stay 46 * Uninitialized status, we need to send KX. We will stay
48 * in this state until the first connection is up. 47 * in this state until the first connection is up.
@@ -79,7 +78,6 @@ enum CadetTunnelEState
79 * Handshake completed: session key available. 78 * Handshake completed: session key available.
80 */ 79 */
81 CADET_TUNNEL_KEY_OK 80 CADET_TUNNEL_KEY_OK
82
83}; 81};
84 82
85 83
@@ -91,7 +89,7 @@ enum CadetTunnelEState
91 * @return Static string the destination peer's ID. 89 * @return Static string the destination peer's ID.
92 */ 90 */
93const char * 91const char *
94GCT_2s (const struct CadetTunnel *t); 92GCT_2s(const struct CadetTunnel *t);
95 93
96 94
97/** 95/**
@@ -102,7 +100,7 @@ GCT_2s (const struct CadetTunnel *t);
102 * @return new tunnel to @a destination 100 * @return new tunnel to @a destination
103 */ 101 */
104struct CadetTunnel * 102struct CadetTunnel *
105GCT_create_tunnel (struct CadetPeer *destination); 103GCT_create_tunnel(struct CadetPeer *destination);
106 104
107 105
108/** 106/**
@@ -111,7 +109,7 @@ GCT_create_tunnel (struct CadetPeer *destination);
111 * @param t tunnel to destroy 109 * @param t tunnel to destroy
112 */ 110 */
113void 111void
114GCT_destroy_tunnel_now (struct CadetTunnel *t); 112GCT_destroy_tunnel_now(struct CadetTunnel *t);
115 113
116 114
117/** 115/**
@@ -124,9 +122,9 @@ GCT_destroy_tunnel_now (struct CadetTunnel *t);
124 * #GNUNET_SYSERR on failure (duplicate connection) 122 * #GNUNET_SYSERR on failure (duplicate connection)
125 */ 123 */
126int 124int
127GCT_add_inbound_connection (struct CadetTunnel *t, 125GCT_add_inbound_connection(struct CadetTunnel *t,
128 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid, 126 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
129 struct CadetPeerPath *path); 127 struct CadetPeerPath *path);
130 128
131 129
132/** 130/**
@@ -136,7 +134,7 @@ GCT_add_inbound_connection (struct CadetTunnel *t,
136 * @param ct binding of connection to tunnel of the connection that was lost. 134 * @param ct binding of connection to tunnel of the connection that was lost.
137 */ 135 */
138void 136void
139GCT_connection_lost (struct CadetTConnection *ct); 137GCT_connection_lost(struct CadetTConnection *ct);
140 138
141 139
142/** 140/**
@@ -146,7 +144,7 @@ GCT_connection_lost (struct CadetTConnection *ct);
146 * @return the destination of the tunnel 144 * @return the destination of the tunnel
147 */ 145 */
148struct CadetPeer * 146struct CadetPeer *
149GCT_get_destination (struct CadetTunnel *t); 147GCT_get_destination(struct CadetTunnel *t);
150 148
151 149
152/** 150/**
@@ -158,9 +156,9 @@ GCT_get_destination (struct CadetTunnel *t);
158 * @param off offset of the destination on path @a path 156 * @param off offset of the destination on path @a path
159 */ 157 */
160void 158void
161GCT_consider_path (struct CadetTunnel *t, 159GCT_consider_path(struct CadetTunnel *t,
162 struct CadetPeerPath *p, 160 struct CadetPeerPath *p,
163 unsigned int off); 161 unsigned int off);
164 162
165 163
166/** 164/**
@@ -171,8 +169,8 @@ GCT_consider_path (struct CadetTunnel *t,
171 * @return unique number identifying @a ch within @a t 169 * @return unique number identifying @a ch within @a t
172 */ 170 */
173struct GNUNET_CADET_ChannelTunnelNumber 171struct GNUNET_CADET_ChannelTunnelNumber
174GCT_add_channel (struct CadetTunnel *t, 172GCT_add_channel(struct CadetTunnel *t,
175 struct CadetChannel *ch); 173 struct CadetChannel *ch);
176 174
177 175
178/** 176/**
@@ -183,9 +181,9 @@ GCT_add_channel (struct CadetTunnel *t,
183 * @param ctn unique number identifying @a ch within @a t 181 * @param ctn unique number identifying @a ch within @a t
184 */ 182 */
185void 183void
186GCT_remove_channel (struct CadetTunnel *t, 184GCT_remove_channel(struct CadetTunnel *t,
187 struct CadetChannel *ch, 185 struct CadetChannel *ch,
188 struct GNUNET_CADET_ChannelTunnelNumber ctn); 186 struct GNUNET_CADET_ChannelTunnelNumber ctn);
189 187
190 188
191/** 189/**
@@ -195,8 +193,8 @@ GCT_remove_channel (struct CadetTunnel *t,
195 * @param ctn ID of the channel to destroy 193 * @param ctn ID of the channel to destroy
196 */ 194 */
197void 195void
198GCT_send_channel_destroy (struct CadetTunnel *t, 196GCT_send_channel_destroy(struct CadetTunnel *t,
199 struct GNUNET_CADET_ChannelTunnelNumber ctn); 197 struct GNUNET_CADET_ChannelTunnelNumber ctn);
200 198
201 199
202/** 200/**
@@ -223,10 +221,10 @@ typedef void
223 * @return Handle to cancel message. 221 * @return Handle to cancel message.
224 */ 222 */
225struct CadetTunnelQueueEntry * 223struct CadetTunnelQueueEntry *
226GCT_send (struct CadetTunnel *t, 224GCT_send(struct CadetTunnel *t,
227 const struct GNUNET_MessageHeader *message, 225 const struct GNUNET_MessageHeader *message,
228 GCT_SendContinuation cont, 226 GCT_SendContinuation cont,
229 void *cont_cls); 227 void *cont_cls);
230 228
231 229
232/** 230/**
@@ -239,7 +237,7 @@ GCT_send (struct CadetTunnel *t,
239 * @param q Handle to the queue entry to cancel. 237 * @param q Handle to the queue entry to cancel.
240 */ 238 */
241void 239void
242GCT_send_cancel (struct CadetTunnelQueueEntry *q); 240GCT_send_cancel(struct CadetTunnelQueueEntry *q);
243 241
244 242
245/** 243/**
@@ -249,7 +247,7 @@ GCT_send_cancel (struct CadetTunnelQueueEntry *q);
249 * @return number of channels using the tunnel 247 * @return number of channels using the tunnel
250 */ 248 */
251unsigned int 249unsigned int
252GCT_count_channels (struct CadetTunnel *t); 250GCT_count_channels(struct CadetTunnel *t);
253 251
254 252
255/** 253/**
@@ -259,7 +257,7 @@ GCT_count_channels (struct CadetTunnel *t);
259 * @return number of connections available for the tunnel 257 * @return number of connections available for the tunnel
260 */ 258 */
261unsigned int 259unsigned int
262GCT_count_any_connections (const struct CadetTunnel *t); 260GCT_count_any_connections(const struct CadetTunnel *t);
263 261
264 262
265/** 263/**
@@ -281,9 +279,9 @@ typedef void
281 * @param iter_cls Closure for @c iter. 279 * @param iter_cls Closure for @c iter.
282 */ 280 */
283void 281void
284GCT_iterate_connections (struct CadetTunnel *t, 282GCT_iterate_connections(struct CadetTunnel *t,
285 GCT_ConnectionIterator iter, 283 GCT_ConnectionIterator iter,
286 void *iter_cls); 284 void *iter_cls);
287 285
288 286
289/** 287/**
@@ -305,9 +303,9 @@ typedef void
305 * @param iter_cls Closure for @c iter. 303 * @param iter_cls Closure for @c iter.
306 */ 304 */
307void 305void
308GCT_iterate_channels (struct CadetTunnel *t, 306GCT_iterate_channels(struct CadetTunnel *t,
309 GCT_ChannelIterator iter, 307 GCT_ChannelIterator iter,
310 void *iter_cls); 308 void *iter_cls);
311 309
312 310
313/** 311/**
@@ -318,7 +316,7 @@ GCT_iterate_channels (struct CadetTunnel *t,
318 * @return Tunnel's encryption state. 316 * @return Tunnel's encryption state.
319 */ 317 */
320enum CadetTunnelEState 318enum CadetTunnelEState
321GCT_get_estate (struct CadetTunnel *t); 319GCT_get_estate(struct CadetTunnel *t);
322 320
323 321
324/** 322/**
@@ -328,8 +326,8 @@ GCT_get_estate (struct CadetTunnel *t);
328 * @param msg the key exchange message 326 * @param msg the key exchange message
329 */ 327 */
330void 328void
331GCT_handle_kx (struct CadetTConnection *ct, 329GCT_handle_kx(struct CadetTConnection *ct,
332 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg); 330 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg);
333 331
334 332
335/** 333/**
@@ -339,8 +337,8 @@ GCT_handle_kx (struct CadetTConnection *ct,
339 * @param msg the key exchange message 337 * @param msg the key exchange message
340 */ 338 */
341void 339void
342GCT_handle_kx_auth (struct CadetTConnection *ct, 340GCT_handle_kx_auth(struct CadetTConnection *ct,
343 const struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg); 341 const struct GNUNET_CADET_TunnelKeyExchangeAuthMessage *msg);
344 342
345 343
346/** 344/**
@@ -350,8 +348,8 @@ GCT_handle_kx_auth (struct CadetTConnection *ct,
350 * @param msg the encrypted message to decrypt 348 * @param msg the encrypted message to decrypt
351 */ 349 */
352void 350void
353GCT_handle_encrypted (struct CadetTConnection *ct, 351GCT_handle_encrypted(struct CadetTConnection *ct,
354 const struct GNUNET_CADET_TunnelEncryptedMessage *msg); 352 const struct GNUNET_CADET_TunnelEncryptedMessage *msg);
355 353
356 354
357/** 355/**
@@ -361,8 +359,8 @@ GCT_handle_encrypted (struct CadetTConnection *ct,
361 * @param level Debug level to use. 359 * @param level Debug level to use.
362 */ 360 */
363void 361void
364GCT_debug (const struct CadetTunnel *t, 362GCT_debug(const struct CadetTunnel *t,
365 enum GNUNET_ErrorType level); 363 enum GNUNET_ErrorType level);
366 364
367 365
368#endif 366#endif
diff --git a/src/cadet/test_cadet.c b/src/cadet/test_cadet.c
index 5a28b439e..de5e4eef0 100644
--- a/src/cadet/test_cadet.c
+++ b/src/cadet/test_cadet.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file cadet/test_cadet.c 21 * @file cadet/test_cadet.c
22 * @author Bart Polot 22 * @author Bart Polot
@@ -34,8 +34,7 @@
34/** 34/**
35 * Ugly workaround to unify data handlers on incoming and outgoing channels. 35 * Ugly workaround to unify data handlers on incoming and outgoing channels.
36 */ 36 */
37struct CadetTestChannelWrapper 37struct CadetTestChannelWrapper {
38{
39 /** 38 /**
40 * Channel pointer. 39 * Channel pointer.
41 */ 40 */
@@ -50,17 +49,17 @@ struct CadetTestChannelWrapper
50/** 49/**
51 * How long until we give up on connecting the peers? 50 * How long until we give up on connecting the peers?
52 */ 51 */
53#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120) 52#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120)
54 53
55/** 54/**
56 * Time to wait by default for stuff that should be rather fast. 55 * Time to wait by default for stuff that should be rather fast.
57 */ 56 */
58#define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20) 57#define SHORT_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 20)
59 58
60/** 59/**
61 * How fast do we send messages? 60 * How fast do we send messages?
62 */ 61 */
63#define SEND_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 10) 62#define SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 10)
64 63
65/** 64/**
66 * DIFFERENT TESTS TO RUN 65 * DIFFERENT TESTS TO RUN
@@ -112,7 +111,7 @@ static int ok_goal;
112/** 111/**
113 * Size of each test packet's payload 112 * Size of each test packet's payload
114 */ 113 */
115static size_t size_payload = sizeof (uint32_t); 114static size_t size_payload = sizeof(uint32_t);
116 115
117/** 116/**
118 * Operation to get peer ids. 117 * Operation to get peer ids.
@@ -260,7 +259,7 @@ static unsigned int msg_dropped;
260 * or the last peer in the line (for other tests). 259 * or the last peer in the line (for other tests).
261 */ 260 */
262static struct GNUNET_CADET_Channel * 261static struct GNUNET_CADET_Channel *
263get_target_channel () 262get_target_channel()
264{ 263{
265 if (SPEED == test && GNUNET_YES == test_backwards) 264 if (SPEED == test && GNUNET_YES == test_backwards)
266 return outgoing_ch; 265 return outgoing_ch;
@@ -273,29 +272,29 @@ get_target_channel ()
273 * Show the results of the test (banwidth acheived) and log them to GAUGER 272 * Show the results of the test (banwidth acheived) and log them to GAUGER
274 */ 273 */
275static void 274static void
276show_end_data (void) 275show_end_data(void)
277{ 276{
278 static struct GNUNET_TIME_Absolute end_time; 277 static struct GNUNET_TIME_Absolute end_time;
279 static struct GNUNET_TIME_Relative total_time; 278 static struct GNUNET_TIME_Relative total_time;
280 279
281 end_time = GNUNET_TIME_absolute_get (); 280 end_time = GNUNET_TIME_absolute_get();
282 total_time = GNUNET_TIME_absolute_get_difference (start_time, end_time); 281 total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
283 fprintf (stderr, 282 fprintf(stderr,
284 "\nResults of test \"%s\"\n", 283 "\nResults of test \"%s\"\n",
285 test_name); 284 test_name);
286 fprintf (stderr, 285 fprintf(stderr,
287 "Test time %s\n", 286 "Test time %s\n",
288 GNUNET_STRINGS_relative_time_to_string (total_time, GNUNET_YES)); 287 GNUNET_STRINGS_relative_time_to_string(total_time, GNUNET_YES));
289 fprintf (stderr, 288 fprintf(stderr,
290 "Test bandwidth: %f kb/s\n", 289 "Test bandwidth: %f kb/s\n",
291 4 * total_packets * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms 290 4 * total_packets * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms
292 fprintf (stderr, 291 fprintf(stderr,
293 "Test throughput: %f packets/s\n\n", 292 "Test throughput: %f packets/s\n\n",
294 total_packets * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms 293 total_packets * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms
295 GAUGER ("CADET", 294 GAUGER("CADET",
296 test_name, 295 test_name,
297 total_packets * 1000.0 / (total_time.rel_value_us / 1000), 296 total_packets * 1000.0 / (total_time.rel_value_us / 1000),
298 "packets/s"); 297 "packets/s");
299} 298}
300 299
301 300
@@ -306,30 +305,30 @@ show_end_data (void)
306 * @param tc Task Context. 305 * @param tc Task Context.
307 */ 306 */
308static void 307static void
309disconnect_cadet_peers (void *cls) 308disconnect_cadet_peers(void *cls)
310{ 309{
311 long line = (long) cls; 310 long line = (long)cls;
312 311
313 disconnect_task = NULL; 312 disconnect_task = NULL;
314 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 313 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
315 "disconnecting cadet service of peers, called from line %ld\n", 314 "disconnecting cadet service of peers, called from line %ld\n",
316 line); 315 line);
317 for (unsigned int i = 0; i < 2; i++) 316 for (unsigned int i = 0; i < 2; i++)
318 { 317 {
319 GNUNET_TESTBED_operation_done (t_op[i]); 318 GNUNET_TESTBED_operation_done(t_op[i]);
320 } 319 }
321 if (NULL != outgoing_ch) 320 if (NULL != outgoing_ch)
322 { 321 {
323 GNUNET_CADET_channel_destroy (outgoing_ch); 322 GNUNET_CADET_channel_destroy(outgoing_ch);
324 outgoing_ch = NULL; 323 outgoing_ch = NULL;
325 } 324 }
326 if (NULL != incoming_ch) 325 if (NULL != incoming_ch)
327 { 326 {
328 GNUNET_CADET_channel_destroy (incoming_ch); 327 GNUNET_CADET_channel_destroy(incoming_ch);
329 incoming_ch = NULL; 328 incoming_ch = NULL;
330 } 329 }
331 GNUNET_CADET_TEST_cleanup (test_ctx); 330 GNUNET_CADET_TEST_cleanup(test_ctx);
332 GNUNET_SCHEDULER_shutdown (); 331 GNUNET_SCHEDULER_shutdown();
333} 332}
334 333
335 334
@@ -340,27 +339,27 @@ disconnect_cadet_peers (void *cls)
340 * @param tc Task Context. 339 * @param tc Task Context.
341 */ 340 */
342static void 341static void
343shutdown_task (void *cls) 342shutdown_task(void *cls)
344{ 343{
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 344 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
346 "Ending test.\n"); 345 "Ending test.\n");
347 if (NULL != send_next_msg_task) 346 if (NULL != send_next_msg_task)
348 { 347 {
349 GNUNET_SCHEDULER_cancel (send_next_msg_task); 348 GNUNET_SCHEDULER_cancel(send_next_msg_task);
350 send_next_msg_task = NULL; 349 send_next_msg_task = NULL;
351 } 350 }
352 if (NULL != test_task) 351 if (NULL != test_task)
353 { 352 {
354 GNUNET_SCHEDULER_cancel (test_task); 353 GNUNET_SCHEDULER_cancel(test_task);
355 test_task = NULL; 354 test_task = NULL;
356 } 355 }
357 if (NULL != disconnect_task) 356 if (NULL != disconnect_task)
358 { 357 {
359 GNUNET_SCHEDULER_cancel (disconnect_task); 358 GNUNET_SCHEDULER_cancel(disconnect_task);
360 disconnect_task = 359 disconnect_task =
361 GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, 360 GNUNET_SCHEDULER_add_now(&disconnect_cadet_peers,
362 (void *) __LINE__); 361 (void *)__LINE__);
363 } 362 }
364} 363}
365 364
366 365
@@ -374,26 +373,26 @@ shutdown_task (void *cls)
374 * operation has executed successfully. 373 * operation has executed successfully.
375 */ 374 */
376static void 375static void
377stats_cont (void *cls, 376stats_cont(void *cls,
378 struct GNUNET_TESTBED_Operation *op, 377 struct GNUNET_TESTBED_Operation *op,
379 const char *emsg) 378 const char *emsg)
380{ 379{
381 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 380 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
382 "KA sent: %u, KA received: %u\n", 381 "KA sent: %u, KA received: %u\n",
383 ka_sent, 382 ka_sent,
384 ka_received); 383 ka_received);
385 if ((KEEPALIVE == test || REOPEN == test) && 384 if ((KEEPALIVE == test || REOPEN == test) &&
386 ((ka_sent < 2) || (ka_sent > ka_received + 1))) 385 ((ka_sent < 2) || (ka_sent > ka_received + 1)))
387 { 386 {
388 GNUNET_break (0); 387 GNUNET_break(0);
389 ok--; 388 ok--;
390 } 389 }
391 GNUNET_TESTBED_operation_done (stats_op); 390 GNUNET_TESTBED_operation_done(stats_op);
392 391
393 if (NULL != disconnect_task) 392 if (NULL != disconnect_task)
394 GNUNET_SCHEDULER_cancel (disconnect_task); 393 GNUNET_SCHEDULER_cancel(disconnect_task);
395 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, 394 disconnect_task = GNUNET_SCHEDULER_add_now(&disconnect_cadet_peers,
396 cls); 395 cls);
397} 396}
398 397
399 398
@@ -409,12 +408,12 @@ stats_cont (void *cls,
409 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration 408 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
410 */ 409 */
411static int 410static int
412stats_iterator (void *cls, 411stats_iterator(void *cls,
413 const struct GNUNET_TESTBED_Peer *peer, 412 const struct GNUNET_TESTBED_Peer *peer,
414 const char *subsystem, 413 const char *subsystem,
415 const char *name, 414 const char *name,
416 uint64_t value, 415 uint64_t value,
417 int is_persistent) 416 int is_persistent)
418{ 417{
419 static const char *s_sent = "# keepalives sent"; 418 static const char *s_sent = "# keepalives sent";
420 static const char *s_recv = "# keepalives received"; 419 static const char *s_recv = "# keepalives received";
@@ -422,16 +421,16 @@ stats_iterator (void *cls,
422 static const char *cdrops = "# messages dropped due to slow client"; 421 static const char *cdrops = "# messages dropped due to slow client";
423 uint32_t i; 422 uint32_t i;
424 423
425 i = GNUNET_TESTBED_get_index (peer); 424 i = GNUNET_TESTBED_get_index(peer);
426 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "STATS PEER %u - %s [%s]: %llu\n", i, 425 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "STATS PEER %u - %s [%s]: %llu\n", i,
427 subsystem, name, (unsigned long long) value); 426 subsystem, name, (unsigned long long)value);
428 if (0 == strncmp (s_sent, name, strlen (s_sent)) && 0 == i) 427 if (0 == strncmp(s_sent, name, strlen(s_sent)) && 0 == i)
429 ka_sent = value; 428 ka_sent = value;
430 if (0 == strncmp (s_recv, name, strlen (s_recv)) && peers_requested - 1 == i) 429 if (0 == strncmp(s_recv, name, strlen(s_recv)) && peers_requested - 1 == i)
431 ka_received = value; 430 ka_received = value;
432 if (0 == strncmp (rdrops, name, strlen (rdrops))) 431 if (0 == strncmp(rdrops, name, strlen(rdrops)))
433 msg_dropped += value; 432 msg_dropped += value;
434 if (0 == strncmp (cdrops, name, strlen (cdrops))) 433 if (0 == strncmp(cdrops, name, strlen(cdrops)))
435 msg_dropped += value; 434 msg_dropped += value;
436 435
437 return GNUNET_OK; 436 return GNUNET_OK;
@@ -444,26 +443,26 @@ stats_iterator (void *cls,
444 * @param cls Closure (line from which the task was scheduled). 443 * @param cls Closure (line from which the task was scheduled).
445 */ 444 */
446static void 445static void
447gather_stats_and_exit (void *cls) 446gather_stats_and_exit(void *cls)
448{ 447{
449 long l = (long) cls; 448 long l = (long)cls;
450 449
451 disconnect_task = NULL; 450 disconnect_task = NULL;
452 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 451 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
453 "gathering statistics from line %ld\n", 452 "gathering statistics from line %ld\n",
454 l); 453 l);
455 if (NULL != outgoing_ch) 454 if (NULL != outgoing_ch)
456 { 455 {
457 GNUNET_CADET_channel_destroy (outgoing_ch); 456 GNUNET_CADET_channel_destroy(outgoing_ch);
458 outgoing_ch = NULL; 457 outgoing_ch = NULL;
459 } 458 }
460 stats_op = GNUNET_TESTBED_get_statistics (peers_running, 459 stats_op = GNUNET_TESTBED_get_statistics(peers_running,
461 testbed_peers, 460 testbed_peers,
462 "cadet", 461 "cadet",
463 NULL, 462 NULL,
464 &stats_iterator, 463 &stats_iterator,
465 stats_cont, 464 stats_cont,
466 cls); 465 cls);
467} 466}
468 467
469 468
@@ -475,7 +474,7 @@ gather_stats_and_exit (void *cls)
475 * @param channel Channel to send the message on. 474 * @param channel Channel to send the message on.
476 */ 475 */
477static void 476static void
478send_test_message (struct GNUNET_CADET_Channel *channel); 477send_test_message(struct GNUNET_CADET_Channel *channel);
479 478
480/** 479/**
481 * Check if payload is sane (size contains payload). 480 * Check if payload is sane (size contains payload).
@@ -486,8 +485,8 @@ send_test_message (struct GNUNET_CADET_Channel *channel);
486 * #GNUNET_SYSERR to close it (signal serious error). 485 * #GNUNET_SYSERR to close it (signal serious error).
487 */ 486 */
488static int 487static int
489check_data (void *cls, 488check_data(void *cls,
490 const struct GNUNET_MessageHeader *message); 489 const struct GNUNET_MessageHeader *message);
491 490
492/** 491/**
493 * Function is called whenever a message is received. 492 * Function is called whenever a message is received.
@@ -496,8 +495,8 @@ check_data (void *cls,
496 * @param message the actual message 495 * @param message the actual message
497 */ 496 */
498static void 497static void
499handle_data (void *cls, 498handle_data(void *cls,
500 const struct GNUNET_MessageHeader *message); 499 const struct GNUNET_MessageHeader *message);
501 500
502/** 501/**
503 * Function called whenever an MQ-channel is destroyed, unless the destruction 502 * Function called whenever an MQ-channel is destroyed, unless the destruction
@@ -511,8 +510,8 @@ handle_data (void *cls,
511 * @param channel Connection to the other end (henceforth invalid). 510 * @param channel Connection to the other end (henceforth invalid).
512 */ 511 */
513static void 512static void
514disconnect_handler (void *cls, 513disconnect_handler(void *cls,
515 const struct GNUNET_CADET_Channel *channel); 514 const struct GNUNET_CADET_Channel *channel);
516 515
517 516
518/** 517/**
@@ -521,37 +520,37 @@ disconnect_handler (void *cls,
521 * @param cls Closure (line from which the task was scheduled). 520 * @param cls Closure (line from which the task was scheduled).
522 */ 521 */
523static void 522static void
524reconnect_op (void *cls) 523reconnect_op(void *cls)
525{ 524{
526 struct GNUNET_MQ_MessageHandler handlers[] = { 525 struct GNUNET_MQ_MessageHandler handlers[] = {
527 GNUNET_MQ_hd_var_size (data, 526 GNUNET_MQ_hd_var_size(data,
528 GNUNET_MESSAGE_TYPE_DUMMY, 527 GNUNET_MESSAGE_TYPE_DUMMY,
529 struct GNUNET_MessageHeader, 528 struct GNUNET_MessageHeader,
530 NULL), 529 NULL),
531 GNUNET_MQ_handler_end () 530 GNUNET_MQ_handler_end()
532 }; 531 };
533 long l = (long) cls; 532 long l = (long)cls;
534 struct CadetTestChannelWrapper *ch; 533 struct CadetTestChannelWrapper *ch;
535 534
536 reconnect_task = NULL; 535 reconnect_task = NULL;
537 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 536 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
538 "reconnecting from line %ld\n", 537 "reconnecting from line %ld\n",
539 l); 538 l);
540 if (NULL != outgoing_ch) 539 if (NULL != outgoing_ch)
541 { 540 {
542 GNUNET_CADET_channel_destroy (outgoing_ch); 541 GNUNET_CADET_channel_destroy(outgoing_ch);
543 outgoing_ch = NULL; 542 outgoing_ch = NULL;
544 } 543 }
545 ch = GNUNET_new (struct CadetTestChannelWrapper); 544 ch = GNUNET_new(struct CadetTestChannelWrapper);
546 outgoing_ch = GNUNET_CADET_channel_create (h1, 545 outgoing_ch = GNUNET_CADET_channel_create(h1,
547 ch, 546 ch,
548 p_id[1], 547 p_id[1],
549 &port, 548 &port,
550 NULL, 549 NULL,
551 &disconnect_handler, 550 &disconnect_handler,
552 handlers); 551 handlers);
553 ch->ch = outgoing_ch; 552 ch->ch = outgoing_ch;
554 send_test_message (outgoing_ch); 553 send_test_message(outgoing_ch);
555} 554}
556 555
557/** 556/**
@@ -566,47 +565,47 @@ reconnect_op (void *cls)
566 * @param channel Connection to the other end (henceforth invalid). 565 * @param channel Connection to the other end (henceforth invalid).
567 */ 566 */
568static void 567static void
569disconnect_handler (void *cls, 568disconnect_handler(void *cls,
570 const struct GNUNET_CADET_Channel *channel) 569 const struct GNUNET_CADET_Channel *channel)
571{ 570{
572 struct CadetTestChannelWrapper *ch_w = cls; 571 struct CadetTestChannelWrapper *ch_w = cls;
573 572
574 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 573 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
575 "Channel disconnected at %d\n", 574 "Channel disconnected at %d\n",
576 ok); 575 ok);
577 GNUNET_assert (ch_w->ch == channel); 576 GNUNET_assert(ch_w->ch == channel);
578 if (channel == incoming_ch) 577 if (channel == incoming_ch)
579 {
580 ok++;
581 incoming_ch = NULL;
582 }
583 else if (outgoing_ch == channel)
584 {
585 if (P2P_SIGNAL == test)
586 { 578 {
587 ok++; 579 ok++;
580 incoming_ch = NULL;
581 }
582 else if (outgoing_ch == channel)
583 {
584 if (P2P_SIGNAL == test)
585 {
586 ok++;
587 }
588 outgoing_ch = NULL;
588 } 589 }
589 outgoing_ch = NULL;
590 }
591 else 590 else
592 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 591 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
593 "Unknown channel! %p\n", 592 "Unknown channel! %p\n",
594 channel); 593 channel);
595 if (NULL != disconnect_task && REOPEN != test) 594 if (NULL != disconnect_task && REOPEN != test)
596 { 595 {
597 GNUNET_SCHEDULER_cancel (disconnect_task); 596 GNUNET_SCHEDULER_cancel(disconnect_task);
598 disconnect_task = 597 disconnect_task =
599 GNUNET_SCHEDULER_add_now (&gather_stats_and_exit, 598 GNUNET_SCHEDULER_add_now(&gather_stats_and_exit,
600 (void *) __LINE__); 599 (void *)__LINE__);
601 } 600 }
602 else if (NULL != reconnect_task && REOPEN == test) 601 else if (NULL != reconnect_task && REOPEN == test)
603 { 602 {
604 GNUNET_SCHEDULER_cancel (reconnect_task); 603 GNUNET_SCHEDULER_cancel(reconnect_task);
605 reconnect_task = 604 reconnect_task =
606 GNUNET_SCHEDULER_add_now (&reconnect_op, 605 GNUNET_SCHEDULER_add_now(&reconnect_op,
607 (void *) __LINE__); 606 (void *)__LINE__);
608 } 607 }
609 GNUNET_free (ch_w); 608 GNUNET_free(ch_w);
610} 609}
611 610
612 611
@@ -616,18 +615,18 @@ disconnect_handler (void *cls,
616 * @param line Line in the code the abort is requested from (__LINE__). 615 * @param line Line in the code the abort is requested from (__LINE__).
617 */ 616 */
618static void 617static void
619abort_test (long line) 618abort_test(long line)
620{ 619{
621 if (NULL != disconnect_task) 620 if (NULL != disconnect_task)
622 { 621 {
623 GNUNET_SCHEDULER_cancel (disconnect_task); 622 GNUNET_SCHEDULER_cancel(disconnect_task);
624 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 623 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
625 "Aborting test from %ld\n", 624 "Aborting test from %ld\n",
626 line); 625 line);
627 disconnect_task = 626 disconnect_task =
628 GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, 627 GNUNET_SCHEDULER_add_now(&disconnect_cadet_peers,
629 (void *) line); 628 (void *)line);
630 } 629 }
631} 630}
632 631
633 632
@@ -639,7 +638,7 @@ abort_test (long line)
639 * @param channel Channel to send the message on. 638 * @param channel Channel to send the message on.
640 */ 639 */
641static void 640static void
642send_test_message (struct GNUNET_CADET_Channel *channel) 641send_test_message(struct GNUNET_CADET_Channel *channel)
643{ 642{
644 struct GNUNET_MQ_Envelope *env; 643 struct GNUNET_MQ_Envelope *env;
645 struct GNUNET_MessageHeader *msg; 644 struct GNUNET_MessageHeader *msg;
@@ -647,64 +646,64 @@ send_test_message (struct GNUNET_CADET_Channel *channel)
647 int payload; 646 int payload;
648 int size; 647 int size;
649 648
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 649 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
651 "Sending test message on channel %p\n", 650 "Sending test message on channel %p\n",
652 channel); 651 channel);
653 size = size_payload; 652 size = size_payload;
654 if (GNUNET_NO == initialized) 653 if (GNUNET_NO == initialized)
655 { 654 {
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending INITIALIZER\n"); 655 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending INITIALIZER\n");
657 size += 1000; 656 size += 1000;
658 payload = data_sent; 657 payload = data_sent;
659 if (SPEED_ACK == test) // FIXME unify SPEED_ACK with an initializer 658 if (SPEED_ACK == test) // FIXME unify SPEED_ACK with an initializer
660 data_sent++; 659 data_sent++;
661 } 660 }
662 else if (SPEED == test || SPEED_ACK == test) 661 else if (SPEED == test || SPEED_ACK == test)
663 { 662 {
664 if (get_target_channel() == channel) 663 if (get_target_channel() == channel)
664 {
665 payload = ack_sent;
666 size += ack_sent;
667 ack_sent++;
668 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
669 "Sending ACK %u [%d bytes]\n",
670 payload, size);
671 }
672 else
673 {
674 payload = data_sent;
675 size += data_sent;
676 data_sent++;
677 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
678 "Sending DATA %u [%d bytes]\n",
679 data_sent, size);
680 }
681 }
682 else if (FORWARD == test)
665 { 683 {
666 payload = ack_sent; 684 payload = ack_sent;
667 size += ack_sent;
668 ack_sent++;
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
670 "Sending ACK %u [%d bytes]\n",
671 payload, size);
672 } 685 }
673 else 686 else if (P2P_SIGNAL == test)
674 { 687 {
675 payload = data_sent; 688 payload = data_sent;
676 size += data_sent;
677 data_sent++;
678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
679 "Sending DATA %u [%d bytes]\n",
680 data_sent, size);
681 } 689 }
682 }
683 else if (FORWARD == test)
684 {
685 payload = ack_sent;
686 }
687 else if (P2P_SIGNAL == test)
688 {
689 payload = data_sent;
690 }
691 else if (REOPEN == test) 690 else if (REOPEN == test)
692 { 691 {
693 payload = data_sent; 692 payload = data_sent;
694 data_sent++; 693 data_sent++;
695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 694 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
696 "Sending DATA %u [%d bytes]\n", 695 "Sending DATA %u [%d bytes]\n",
697 data_sent, size); 696 data_sent, size);
698 } 697 }
699 else 698 else
700 { 699 {
701 GNUNET_assert (0); 700 GNUNET_assert(0);
702 } 701 }
703 env = GNUNET_MQ_msg_extra (msg, size, GNUNET_MESSAGE_TYPE_DUMMY); 702 env = GNUNET_MQ_msg_extra(msg, size, GNUNET_MESSAGE_TYPE_DUMMY);
704 703
705 data = (uint32_t *) &msg[1]; 704 data = (uint32_t *)&msg[1];
706 *data = htonl (payload); 705 *data = htonl(payload);
707 GNUNET_MQ_send (GNUNET_CADET_get_mq (channel), env); 706 GNUNET_MQ_send(GNUNET_CADET_get_mq(channel), env);
708} 707}
709 708
710 709
@@ -715,30 +714,30 @@ send_test_message (struct GNUNET_CADET_Channel *channel)
715 * @param cls Closure (unused). 714 * @param cls Closure (unused).
716 */ 715 */
717static void 716static void
718send_next_msg (void *cls) 717send_next_msg(void *cls)
719{ 718{
720 struct GNUNET_CADET_Channel *channel; 719 struct GNUNET_CADET_Channel *channel;
721 720
722 send_next_msg_task = NULL; 721 send_next_msg_task = NULL;
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 722 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
724 "Sending next message: %d\n", 723 "Sending next message: %d\n",
725 data_sent); 724 data_sent);
726 725
727 channel = GNUNET_YES == test_backwards ? incoming_ch : outgoing_ch; 726 channel = GNUNET_YES == test_backwards ? incoming_ch : outgoing_ch;
728 GNUNET_assert (NULL != channel); 727 GNUNET_assert(NULL != channel);
729 GNUNET_assert (SPEED == test); 728 GNUNET_assert(SPEED == test);
730 send_test_message (channel); 729 send_test_message(channel);
731 if (data_sent < total_packets) 730 if (data_sent < total_packets)
732 { 731 {
733 /* SPEED test: Send all messages as soon as possible */ 732 /* SPEED test: Send all messages as soon as possible */
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 733 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
735 "Scheduling message %d\n", 734 "Scheduling message %d\n",
736 data_sent + 1); 735 data_sent + 1);
737 send_next_msg_task = 736 send_next_msg_task =
738 GNUNET_SCHEDULER_add_delayed (SEND_INTERVAL, 737 GNUNET_SCHEDULER_add_delayed(SEND_INTERVAL,
739 &send_next_msg, 738 &send_next_msg,
740 NULL); 739 NULL);
741 } 740 }
742} 741}
743 742
744 743
@@ -749,20 +748,20 @@ send_next_msg (void *cls)
749 * @param line Code line number to log if a timeout occurs. 748 * @param line Code line number to log if a timeout occurs.
750 */ 749 */
751static void 750static void
752reschedule_timeout_task (long line) 751reschedule_timeout_task(long line)
753{ 752{
754 if ((ok % 10) == 0) 753 if ((ok % 10) == 0)
755 {
756 if (NULL != disconnect_task)
757 { 754 {
758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 755 if (NULL != disconnect_task)
759 "reschedule timeout every 10 messages\n"); 756 {
760 GNUNET_SCHEDULER_cancel (disconnect_task); 757 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
761 disconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, 758 "reschedule timeout every 10 messages\n");
762 &gather_stats_and_exit, 759 GNUNET_SCHEDULER_cancel(disconnect_task);
763 (void *) line); 760 disconnect_task = GNUNET_SCHEDULER_add_delayed(short_time,
761 &gather_stats_and_exit,
762 (void *)line);
763 }
764 } 764 }
765 }
766} 765}
767 766
768 767
@@ -775,8 +774,8 @@ reschedule_timeout_task (long line)
775 * #GNUNET_SYSERR to close it (signal serious error). 774 * #GNUNET_SYSERR to close it (signal serious error).
776 */ 775 */
777static int 776static int
778check_data (void *cls, 777check_data(void *cls,
779 const struct GNUNET_MessageHeader *message) 778 const struct GNUNET_MessageHeader *message)
780{ 779{
781 return GNUNET_OK; /* all is well-formed */ 780 return GNUNET_OK; /* all is well-formed */
782} 781}
@@ -789,8 +788,8 @@ check_data (void *cls,
789 * @param message the actual message 788 * @param message the actual message
790 */ 789 */
791static void 790static void
792handle_data (void *cls, 791handle_data(void *cls,
793 const struct GNUNET_MessageHeader *message) 792 const struct GNUNET_MessageHeader *message)
794{ 793{
795 struct CadetTestChannelWrapper *ch = cls; 794 struct CadetTestChannelWrapper *ch = cls;
796 struct GNUNET_CADET_Channel *channel = ch->ch; 795 struct GNUNET_CADET_Channel *channel = ch->ch;
@@ -799,101 +798,101 @@ handle_data (void *cls,
799 int *counter; 798 int *counter;
800 799
801 ok++; 800 ok++;
802 GNUNET_CADET_receive_done (channel); 801 GNUNET_CADET_receive_done(channel);
803 counter = get_target_channel () == channel ? &data_received : &ack_received; 802 counter = get_target_channel() == channel ? &data_received : &ack_received;
804 803
805 reschedule_timeout_task ((long) __LINE__); 804 reschedule_timeout_task((long)__LINE__);
806 805
807 if (channel == outgoing_ch) 806 if (channel == outgoing_ch)
808 { 807 {
809 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 808 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
810 "Root client got a message.\n"); 809 "Root client got a message.\n");
811 } 810 }
812 else if (channel == incoming_ch) 811 else if (channel == incoming_ch)
813 { 812 {
814 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 813 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
815 "Leaf client got a message.\n"); 814 "Leaf client got a message.\n");
816 } 815 }
817 else
818 {
819 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
820 "Unknown channel %p.\n",
821 channel);
822 GNUNET_assert (0);
823 }
824
825 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
826 " ok: (%d/%d)\n",
827 ok,
828 ok_goal);
829 data = (uint32_t *) &message[1];
830 payload = ntohl (*data);
831 if (payload == *counter)
832 {
833 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
834 " payload as expected: %u\n",
835 payload);
836 }
837 else 816 else
838 {
839 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
840 " payload %u, expected: %u\n",
841 payload, *counter);
842 }
843
844 if (GNUNET_NO == initialized)
845 {
846 initialized = GNUNET_YES;
847 start_time = GNUNET_TIME_absolute_get ();
848 if (SPEED == test)
849 { 817 {
850 GNUNET_assert (incoming_ch == channel); 818 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
851 send_next_msg_task = GNUNET_SCHEDULER_add_now (&send_next_msg, 819 "Unknown channel %p.\n",
852 NULL); 820 channel);
853 return; 821 GNUNET_assert(0);
854 } 822 }
855 }
856 823
857 (*counter)++; 824 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
858 if (get_target_channel () == channel) /* Got "data" */ 825 " ok: (%d/%d)\n",
859 { 826 ok,
860 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received data %u\n", data_received); 827 ok_goal);
861 if (SPEED != test || (ok_goal - 2) == ok) 828 data = (uint32_t *)&message[1];
829 payload = ntohl(*data);
830 if (payload == *counter)
862 { 831 {
863 /* Send ACK */ 832 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
864 send_test_message (channel); 833 " payload as expected: %u\n",
865 return; 834 payload);
866 } 835 }
867 else 836 else
868 { 837 {
869 if (data_received < total_packets) 838 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
870 return; 839 " payload %u, expected: %u\n",
840 payload, *counter);
871 } 841 }
872 } 842
873 else /* Got "ack" */ 843 if (GNUNET_NO == initialized)
874 {
875 if (SPEED_ACK == test || SPEED == test)
876 { 844 {
877 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received ack %u\n", ack_received); 845 initialized = GNUNET_YES;
878 /* Send more data */ 846 start_time = GNUNET_TIME_absolute_get();
879 send_test_message (channel); 847 if (SPEED == test)
880 if (ack_received < total_packets && SPEED != test) 848 {
881 return; 849 GNUNET_assert(incoming_ch == channel);
882 if (ok == 2 && SPEED == test) 850 send_next_msg_task = GNUNET_SCHEDULER_add_now(&send_next_msg,
883 return; 851 NULL);
884 show_end_data (); 852 return;
853 }
885 } 854 }
886 if (test == P2P_SIGNAL) 855
856 (*counter)++;
857 if (get_target_channel() == channel) /* Got "data" */
887 { 858 {
888 GNUNET_CADET_channel_destroy (incoming_ch); 859 GNUNET_log(GNUNET_ERROR_TYPE_INFO, " received data %u\n", data_received);
889 incoming_ch = NULL; 860 if (SPEED != test || (ok_goal - 2) == ok)
861 {
862 /* Send ACK */
863 send_test_message(channel);
864 return;
865 }
866 else
867 {
868 if (data_received < total_packets)
869 return;
870 }
890 } 871 }
891 else 872 else /* Got "ack" */
892 { 873 {
893 GNUNET_CADET_channel_destroy (outgoing_ch); 874 if (SPEED_ACK == test || SPEED == test)
894 outgoing_ch = NULL; 875 {
876 GNUNET_log(GNUNET_ERROR_TYPE_INFO, " received ack %u\n", ack_received);
877 /* Send more data */
878 send_test_message(channel);
879 if (ack_received < total_packets && SPEED != test)
880 return;
881 if (ok == 2 && SPEED == test)
882 return;
883 show_end_data();
884 }
885 if (test == P2P_SIGNAL)
886 {
887 GNUNET_CADET_channel_destroy(incoming_ch);
888 incoming_ch = NULL;
889 }
890 else
891 {
892 GNUNET_CADET_channel_destroy(outgoing_ch);
893 outgoing_ch = NULL;
894 }
895 } 895 }
896 }
897} 896}
898 897
899 898
@@ -910,66 +909,66 @@ handle_data (void *cls,
910 * received on the @a channel. 909 * received on the @a channel.
911 */ 910 */
912static void * 911static void *
913connect_handler (void *cls, 912connect_handler(void *cls,
914 struct GNUNET_CADET_Channel *channel, 913 struct GNUNET_CADET_Channel *channel,
915 const struct GNUNET_PeerIdentity *source) 914 const struct GNUNET_PeerIdentity *source)
916{ 915{
917 struct CadetTestChannelWrapper *ch; 916 struct CadetTestChannelWrapper *ch;
918 long peer = (long) cls; 917 long peer = (long)cls;
919 918
920 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 919 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
921 "Incoming channel from %s to %ld: %p\n", 920 "Incoming channel from %s to %ld: %p\n",
922 GNUNET_i2s (source), 921 GNUNET_i2s(source),
923 peer, 922 peer,
924 channel); 923 channel);
925 ok++; 924 ok++;
926 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 925 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
927 " ok: %d\n", 926 " ok: %d\n",
928 ok); 927 ok);
929 if (peer == peers_requested - 1) 928 if (peer == peers_requested - 1)
930 {
931 if (NULL != incoming_ch)
932 { 929 {
933 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 930 if (NULL != incoming_ch)
934 "Duplicate incoming channel for client %lu\n", 931 {
935 (long) cls); 932 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
936 GNUNET_assert (0); 933 "Duplicate incoming channel for client %lu\n",
934 (long)cls);
935 GNUNET_assert(0);
936 }
937 incoming_ch = channel;
937 } 938 }
938 incoming_ch = channel;
939 }
940 else 939 else
941 { 940 {
942 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 941 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
943 "Incoming channel for unexpected peer #%lu\n", 942 "Incoming channel for unexpected peer #%lu\n",
944 (long) cls); 943 (long)cls);
945 GNUNET_assert (0); 944 GNUNET_assert(0);
946 } 945 }
947 if (NULL != disconnect_task && REOPEN != test) 946 if (NULL != disconnect_task && REOPEN != test)
948 { 947 {
949 GNUNET_SCHEDULER_cancel (disconnect_task); 948 GNUNET_SCHEDULER_cancel(disconnect_task);
950 disconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, 949 disconnect_task = GNUNET_SCHEDULER_add_delayed(short_time,
951 &gather_stats_and_exit, 950 &gather_stats_and_exit,
952 (void *) __LINE__); 951 (void *)__LINE__);
953 } 952 }
954 else if ((NULL != disconnect_task) && (REOPEN == test)) 953 else if ((NULL != disconnect_task) && (REOPEN == test))
955 { 954 {
956 GNUNET_SCHEDULER_cancel (disconnect_task); 955 GNUNET_SCHEDULER_cancel(disconnect_task);
957 disconnect_task = GNUNET_SCHEDULER_add_delayed ( 956 disconnect_task = GNUNET_SCHEDULER_add_delayed(
958 GNUNET_TIME_relative_multiply (short_time, 2), 957 GNUNET_TIME_relative_multiply(short_time, 2),
959 &gather_stats_and_exit, 958 &gather_stats_and_exit,
960 (void *) __LINE__); 959 (void *)__LINE__);
961 } 960 }
962 961
963 if ((NULL != reconnect_task) && (REOPEN == test)) 962 if ((NULL != reconnect_task) && (REOPEN == test))
964 { 963 {
965 GNUNET_SCHEDULER_cancel (reconnect_task); 964 GNUNET_SCHEDULER_cancel(reconnect_task);
966 reconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, 965 reconnect_task = GNUNET_SCHEDULER_add_delayed(short_time,
967 &reconnect_op, 966 &reconnect_op,
968 (void *) __LINE__); 967 (void *)__LINE__);
969 } 968 }
970 969
971 /* TODO: cannot return channel as-is, in order to unify the data handlers */ 970 /* TODO: cannot return channel as-is, in order to unify the data handlers */
972 ch = GNUNET_new (struct CadetTestChannelWrapper); 971 ch = GNUNET_new(struct CadetTestChannelWrapper);
973 ch->ch = channel; 972 ch->ch = channel;
974 973
975 return ch; 974 return ch;
@@ -985,44 +984,44 @@ connect_handler (void *cls,
985 * @param cls Closure (unused). 984 * @param cls Closure (unused).
986 */ 985 */
987static void 986static void
988start_test (void *cls) 987start_test(void *cls)
989{ 988{
990 struct GNUNET_MQ_MessageHandler handlers[] = { 989 struct GNUNET_MQ_MessageHandler handlers[] = {
991 GNUNET_MQ_hd_var_size (data, 990 GNUNET_MQ_hd_var_size(data,
992 GNUNET_MESSAGE_TYPE_DUMMY, 991 GNUNET_MESSAGE_TYPE_DUMMY,
993 struct GNUNET_MessageHeader, 992 struct GNUNET_MessageHeader,
994 NULL), 993 NULL),
995 GNUNET_MQ_handler_end () 994 GNUNET_MQ_handler_end()
996 }; 995 };
997 struct CadetTestChannelWrapper *ch; 996 struct CadetTestChannelWrapper *ch;
998 997
999 test_task = NULL; 998 test_task = NULL;
1000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "start_test: %s\n", test_name); 999 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "start_test: %s\n", test_name);
1001 if (NULL != disconnect_task) 1000 if (NULL != disconnect_task)
1002 { 1001 {
1003 GNUNET_SCHEDULER_cancel (disconnect_task); 1002 GNUNET_SCHEDULER_cancel(disconnect_task);
1004 disconnect_task = NULL; 1003 disconnect_task = NULL;
1005 } 1004 }
1006 1005
1007 if (SPEED_REL == test) 1006 if (SPEED_REL == test)
1008 { 1007 {
1009 test = SPEED; 1008 test = SPEED;
1010 } 1009 }
1011 1010
1012 ch = GNUNET_new (struct CadetTestChannelWrapper); 1011 ch = GNUNET_new(struct CadetTestChannelWrapper);
1013 outgoing_ch = GNUNET_CADET_channel_create (h1, 1012 outgoing_ch = GNUNET_CADET_channel_create(h1,
1014 ch, 1013 ch,
1015 p_id[1], 1014 p_id[1],
1016 &port, 1015 &port,
1017 NULL, 1016 NULL,
1018 &disconnect_handler, 1017 &disconnect_handler,
1019 handlers); 1018 handlers);
1020 1019
1021 ch->ch = outgoing_ch; 1020 ch->ch = outgoing_ch;
1022 1021
1023 disconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, 1022 disconnect_task = GNUNET_SCHEDULER_add_delayed(short_time,
1024 &gather_stats_and_exit, 1023 &gather_stats_and_exit,
1025 (void *) __LINE__); 1024 (void *)__LINE__);
1026 if (KEEPALIVE == test) 1025 if (KEEPALIVE == test)
1027 return; /* Don't send any data. */ 1026 return; /* Don't send any data. */
1028 1027
@@ -1030,22 +1029,21 @@ start_test (void *cls)
1030 data_sent = 0; 1029 data_sent = 0;
1031 ack_received = 0; 1030 ack_received = 0;
1032 ack_sent = 0; 1031 ack_sent = 0;
1033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1032 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1034 "Sending data initializer on channel %p...\n", 1033 "Sending data initializer on channel %p...\n",
1035 outgoing_ch); 1034 outgoing_ch);
1036 send_test_message (outgoing_ch); 1035 send_test_message(outgoing_ch);
1037 if (REOPEN == test) 1036 if (REOPEN == test)
1038 { 1037 {
1039 reconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, 1038 reconnect_task = GNUNET_SCHEDULER_add_delayed(short_time,
1040 &reconnect_op, 1039 &reconnect_op,
1041 (void *) __LINE__); 1040 (void *)__LINE__);
1042 GNUNET_SCHEDULER_cancel (disconnect_task); 1041 GNUNET_SCHEDULER_cancel(disconnect_task);
1043 disconnect_task = GNUNET_SCHEDULER_add_delayed ( 1042 disconnect_task = GNUNET_SCHEDULER_add_delayed(
1044 GNUNET_TIME_relative_multiply (short_time, 2), 1043 GNUNET_TIME_relative_multiply(short_time, 2),
1045 &gather_stats_and_exit, 1044 &gather_stats_and_exit,
1046 (void *) __LINE__); 1045 (void *)__LINE__);
1047 } 1046 }
1048
1049} 1047}
1050 1048
1051 1049
@@ -1059,35 +1057,35 @@ start_test (void *cls)
1059 * NULL if the operation is successfull 1057 * NULL if the operation is successfull
1060 */ 1058 */
1061static void 1059static void
1062pi_cb (void *cls, 1060pi_cb(void *cls,
1063 struct GNUNET_TESTBED_Operation *op, 1061 struct GNUNET_TESTBED_Operation *op,
1064 const struct GNUNET_TESTBED_PeerInformation *pinfo, 1062 const struct GNUNET_TESTBED_PeerInformation *pinfo,
1065 const char *emsg) 1063 const char *emsg)
1066{ 1064{
1067 long i = (long) cls; 1065 long i = (long)cls;
1068 1066
1069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1067 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1070 "ID callback for %ld\n", 1068 "ID callback for %ld\n",
1071 i); 1069 i);
1072 if ( (NULL == pinfo) || 1070 if ((NULL == pinfo) ||
1073 (NULL != emsg) ) 1071 (NULL != emsg))
1074 { 1072 {
1075 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1073 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1076 "pi_cb: %s\n", 1074 "pi_cb: %s\n",
1077 emsg); 1075 emsg);
1078 abort_test (__LINE__); 1076 abort_test(__LINE__);
1079 return; 1077 return;
1080 } 1078 }
1081 p_id[i] = pinfo->result.id; 1079 p_id[i] = pinfo->result.id;
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1080 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1083 "id: %s\n", 1081 "id: %s\n",
1084 GNUNET_i2s (p_id[i])); 1082 GNUNET_i2s(p_id[i]));
1085 p_ids++; 1083 p_ids++;
1086 if (p_ids < 2) 1084 if (p_ids < 2)
1087 return; 1085 return;
1088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1086 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1089 "Got all IDs, starting test\n"); 1087 "Got all IDs, starting test\n");
1090 test_task = GNUNET_SCHEDULER_add_now (&start_test, NULL); 1088 test_task = GNUNET_SCHEDULER_add_now(&start_test, NULL);
1091} 1089}
1092 1090
1093 1091
@@ -1101,34 +1099,34 @@ pi_cb (void *cls,
1101 * @param cadets Handle to each of the CADETs of the peers. 1099 * @param cadets Handle to each of the CADETs of the peers.
1102 */ 1100 */
1103static void 1101static void
1104tmain (void *cls, 1102tmain(void *cls,
1105 struct GNUNET_CADET_TEST_Context *ctx, 1103 struct GNUNET_CADET_TEST_Context *ctx,
1106 unsigned int num_peers, 1104 unsigned int num_peers,
1107 struct GNUNET_TESTBED_Peer **peers, 1105 struct GNUNET_TESTBED_Peer **peers,
1108 struct GNUNET_CADET_Handle **cadets) 1106 struct GNUNET_CADET_Handle **cadets)
1109{ 1107{
1110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n"); 1108 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "test main\n");
1111 ok = 0; 1109 ok = 0;
1112 test_ctx = ctx; 1110 test_ctx = ctx;
1113 peers_running = num_peers; 1111 peers_running = num_peers;
1114 GNUNET_assert (peers_running == peers_requested); 1112 GNUNET_assert(peers_running == peers_requested);
1115 testbed_peers = peers; 1113 testbed_peers = peers;
1116 h1 = cadets[0]; 1114 h1 = cadets[0];
1117 h2 = cadets[num_peers - 1]; 1115 h2 = cadets[num_peers - 1];
1118 disconnect_task = GNUNET_SCHEDULER_add_delayed (short_time, 1116 disconnect_task = GNUNET_SCHEDULER_add_delayed(short_time,
1119 &disconnect_cadet_peers, 1117 &disconnect_cadet_peers,
1120 (void *) __LINE__); 1118 (void *)__LINE__);
1121 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, 1119 GNUNET_SCHEDULER_add_shutdown(&shutdown_task,
1122 NULL); 1120 NULL);
1123 t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0], 1121 t_op[0] = GNUNET_TESTBED_peer_get_information(peers[0],
1124 GNUNET_TESTBED_PIT_IDENTITY, 1122 GNUNET_TESTBED_PIT_IDENTITY,
1125 &pi_cb, 1123 &pi_cb,
1126 (void *) 0L); 1124 (void *)0L);
1127 t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1], 1125 t_op[1] = GNUNET_TESTBED_peer_get_information(peers[num_peers - 1],
1128 GNUNET_TESTBED_PIT_IDENTITY, 1126 GNUNET_TESTBED_PIT_IDENTITY,
1129 &pi_cb, 1127 &pi_cb,
1130 (void *) 1L); 1128 (void *)1L);
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n"); 1129 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
1132} 1130}
1133 1131
1134 1132
@@ -1136,179 +1134,179 @@ tmain (void *cls,
1136 * Main: start test 1134 * Main: start test
1137 */ 1135 */
1138int 1136int
1139main (int argc, char *argv[]) 1137main(int argc, char *argv[])
1140{ 1138{
1141 static const struct GNUNET_HashCode *ports[2]; 1139 static const struct GNUNET_HashCode *ports[2];
1142 struct GNUNET_MQ_MessageHandler handlers[] = { 1140 struct GNUNET_MQ_MessageHandler handlers[] = {
1143 GNUNET_MQ_hd_var_size (data, 1141 GNUNET_MQ_hd_var_size(data,
1144 GNUNET_MESSAGE_TYPE_DUMMY, 1142 GNUNET_MESSAGE_TYPE_DUMMY,
1145 struct GNUNET_MessageHeader, 1143 struct GNUNET_MessageHeader,
1146 NULL), 1144 NULL),
1147 GNUNET_MQ_handler_end () 1145 GNUNET_MQ_handler_end()
1148 }; 1146 };
1149 const char *config_file; 1147 const char *config_file;
1150 char port_id[] = "test port"; 1148 char port_id[] = "test port";
1151 struct GNUNET_GETOPT_CommandLineOption options[] = { 1149 struct GNUNET_GETOPT_CommandLineOption options[] = {
1152 GNUNET_GETOPT_option_relative_time ('t', 1150 GNUNET_GETOPT_option_relative_time('t',
1153 "time", 1151 "time",
1154 "short_time", 1152 "short_time",
1155 gettext_noop ("set short timeout"), 1153 gettext_noop("set short timeout"),
1156 &short_time), 1154 &short_time),
1157 GNUNET_GETOPT_option_uint ('m', 1155 GNUNET_GETOPT_option_uint('m',
1158 "messages", 1156 "messages",
1159 "NUM_MESSAGES", 1157 "NUM_MESSAGES",
1160 gettext_noop ("set number of messages to send"), 1158 gettext_noop("set number of messages to send"),
1161 &total_packets), 1159 &total_packets),
1162 1160
1163 GNUNET_GETOPT_OPTION_END 1161 GNUNET_GETOPT_OPTION_END
1164 }; 1162 };
1165 1163
1166 1164
1167 initialized = GNUNET_NO; 1165 initialized = GNUNET_NO;
1168 GNUNET_log_setup ("test", "DEBUG", NULL); 1166 GNUNET_log_setup("test", "DEBUG", NULL);
1169 1167
1170 total_packets = TOTAL_PACKETS; 1168 total_packets = TOTAL_PACKETS;
1171 short_time = SHORT_TIME; 1169 short_time = SHORT_TIME;
1172 if (-1 == GNUNET_GETOPT_run (argv[0], options, argc, argv)) 1170 if (-1 == GNUNET_GETOPT_run(argv[0], options, argc, argv))
1173 { 1171 {
1174 fprintf (stderr, "test failed: problem with CLI parameters\n"); 1172 fprintf(stderr, "test failed: problem with CLI parameters\n");
1175 exit (1); 1173 exit(1);
1176 } 1174 }
1177 1175
1178 config_file = "test_cadet.conf"; 1176 config_file = "test_cadet.conf";
1179 GNUNET_CRYPTO_hash (port_id, sizeof (port_id), &port); 1177 GNUNET_CRYPTO_hash(port_id, sizeof(port_id), &port);
1180 1178
1181 /* Find out requested size */ 1179 /* Find out requested size */
1182 if (strstr (argv[0], "_2_") != NULL) 1180 if (strstr(argv[0], "_2_") != NULL)
1183 { 1181 {
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DIRECT CONNECTIONs\n"); 1182 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DIRECT CONNECTIONs\n");
1185 peers_requested = 2; 1183 peers_requested = 2;
1186 } 1184 }
1187 else if (strstr (argv[0], "_5_") != NULL) 1185 else if (strstr(argv[0], "_5_") != NULL)
1188 { 1186 {
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "5 PEER LINE\n"); 1187 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "5 PEER LINE\n");
1190 peers_requested = 5; 1188 peers_requested = 5;
1191 } 1189 }
1192 else if (strstr (argv[0], "_6_") != NULL) 1190 else if (strstr(argv[0], "_6_") != NULL)
1193 { 1191 {
1194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "6 PEER LINE\n"); 1192 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "6 PEER LINE\n");
1195 peers_requested = 6; 1193 peers_requested = 6;
1196 } 1194 }
1197 else 1195 else
1198 { 1196 {
1199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "SIZE UNKNOWN, USING 2\n"); 1197 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "SIZE UNKNOWN, USING 2\n");
1200 peers_requested = 2; 1198 peers_requested = 2;
1201 } 1199 }
1202 1200
1203 /* Find out requested test */ 1201 /* Find out requested test */
1204 if (strstr (argv[0], "_forward") != NULL) 1202 if (strstr(argv[0], "_forward") != NULL)
1205 {
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n");
1207 test = FORWARD;
1208 test_name = "unicast";
1209 ok_goal = 4;
1210 }
1211 else if (strstr (argv[0], "_signal") != NULL)
1212 {
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
1214 test = P2P_SIGNAL;
1215 test_name = "signal";
1216 ok_goal = 4;
1217 }
1218 else if (strstr (argv[0], "_speed_ack") != NULL)
1219 {
1220 /* Test is supposed to generate the following callbacks:
1221 * 1 incoming channel (@dest)
1222 * total_packets received data packet (@dest)
1223 * total_packets received data packet (@orig)
1224 * 1 received channel destroy (@dest) FIXME #5818
1225 */
1226 ok_goal = total_packets * 2 + 2;
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
1228 test = SPEED_ACK;
1229 test_name = "speed ack";
1230 }
1231 else if (strstr (argv[0], "_speed") != NULL)
1232 {
1233 /* Test is supposed to generate the following callbacks:
1234 * 1 incoming channel (@dest)
1235 * 1 initial packet (@dest)
1236 * total_packets received data packet (@dest)
1237 * 1 received data packet (@orig)
1238 * 1 received channel destroy (@dest) FIXME #5818
1239 */
1240 ok_goal = total_packets + 4;
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
1242 if (strstr (argv[0], "_reliable") != NULL)
1243 { 1203 {
1244 test = SPEED_REL; 1204 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n");
1245 test_name = "speed reliable"; 1205 test = FORWARD;
1246 config_file = "test_cadet_drop.conf"; 1206 test_name = "unicast";
1207 ok_goal = 4;
1247 } 1208 }
1248 else 1209 else if (strstr(argv[0], "_signal") != NULL)
1249 { 1210 {
1250 test = SPEED; 1211 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
1251 test_name = "speed"; 1212 test = P2P_SIGNAL;
1213 test_name = "signal";
1214 ok_goal = 4;
1215 }
1216 else if (strstr(argv[0], "_speed_ack") != NULL)
1217 {
1218 /* Test is supposed to generate the following callbacks:
1219 * 1 incoming channel (@dest)
1220 * total_packets received data packet (@dest)
1221 * total_packets received data packet (@orig)
1222 * 1 received channel destroy (@dest) FIXME #5818
1223 */
1224 ok_goal = total_packets * 2 + 2;
1225 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
1226 test = SPEED_ACK;
1227 test_name = "speed ack";
1228 }
1229 else if (strstr(argv[0], "_speed") != NULL)
1230 {
1231 /* Test is supposed to generate the following callbacks:
1232 * 1 incoming channel (@dest)
1233 * 1 initial packet (@dest)
1234 * total_packets received data packet (@dest)
1235 * 1 received data packet (@orig)
1236 * 1 received channel destroy (@dest) FIXME #5818
1237 */
1238 ok_goal = total_packets + 4;
1239 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
1240 if (strstr(argv[0], "_reliable") != NULL)
1241 {
1242 test = SPEED_REL;
1243 test_name = "speed reliable";
1244 config_file = "test_cadet_drop.conf";
1245 }
1246 else
1247 {
1248 test = SPEED;
1249 test_name = "speed";
1250 }
1251 }
1252 else if (strstr(argv[0], "_keepalive") != NULL)
1253 {
1254 test = KEEPALIVE;
1255 test_name = "keepalive";
1256 /* Test is supposed to generate the following callbacks:
1257 * 1 incoming channel (@dest)
1258 * [wait]
1259 * 1 received channel destroy (@dest) FIXME #5818
1260 */
1261 ok_goal = 1;
1262 }
1263 else if (strstr(argv[0], "_reopen") != NULL)
1264 {
1265 test = REOPEN;
1266 test_name = "reopen";
1267 ///* Test is supposed to generate the following callbacks:
1268 // * 1 incoming channel (@dest)
1269 // * [wait]
1270 // * 1 received channel destroy (@dest) FIXME #5818
1271 // */
1272 ok_goal = 6;
1252 } 1273 }
1253 }
1254 else if (strstr (argv[0], "_keepalive") != NULL)
1255 {
1256 test = KEEPALIVE;
1257 test_name = "keepalive";
1258 /* Test is supposed to generate the following callbacks:
1259 * 1 incoming channel (@dest)
1260 * [wait]
1261 * 1 received channel destroy (@dest) FIXME #5818
1262 */
1263 ok_goal = 1;
1264 }
1265 else if (strstr (argv[0], "_reopen") != NULL)
1266 {
1267 test = REOPEN;
1268 test_name = "reopen";
1269 ///* Test is supposed to generate the following callbacks:
1270 // * 1 incoming channel (@dest)
1271 // * [wait]
1272 // * 1 received channel destroy (@dest) FIXME #5818
1273 // */
1274 ok_goal = 6;
1275 }
1276 else 1274 else
1277 { 1275 {
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n"); 1276 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
1279 test = SETUP; 1277 test = SETUP;
1280 ok_goal = 0; 1278 ok_goal = 0;
1281 } 1279 }
1282 1280
1283 if (strstr (argv[0], "backwards") != NULL) 1281 if (strstr(argv[0], "backwards") != NULL)
1284 { 1282 {
1285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n"); 1283 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
1286 test_backwards = GNUNET_YES; 1284 test_backwards = GNUNET_YES;
1287 GNUNET_asprintf (&test_name, "backwards %s", test_name); 1285 GNUNET_asprintf(&test_name, "backwards %s", test_name);
1288 } 1286 }
1289 1287
1290 p_ids = 0; 1288 p_ids = 0;
1291 ports[0] = &port; 1289 ports[0] = &port;
1292 ports[1] = NULL; 1290 ports[1] = NULL;
1293 GNUNET_CADET_TEST_ruN ("test_cadet_small", 1291 GNUNET_CADET_TEST_ruN("test_cadet_small",
1294 config_file, 1292 config_file,
1295 peers_requested, 1293 peers_requested,
1296 &tmain, 1294 &tmain,
1297 NULL, /* tmain cls */ 1295 NULL, /* tmain cls */
1298 &connect_handler, 1296 &connect_handler,
1299 NULL, 1297 NULL,
1300 &disconnect_handler, 1298 &disconnect_handler,
1301 handlers, 1299 handlers,
1302 ports); 1300 ports);
1303 if (NULL != strstr (argv[0], "_reliable")) 1301 if (NULL != strstr(argv[0], "_reliable"))
1304 msg_dropped = 0; /* dropped should be retransmitted */ 1302 msg_dropped = 0; /* dropped should be retransmitted */
1305 1303
1306 if (ok_goal > ok - msg_dropped) 1304 if (ok_goal > ok - msg_dropped)
1307 { 1305 {
1308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "FAILED! (%d/%d)\n", ok, ok_goal); 1306 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "FAILED! (%d/%d)\n", ok, ok_goal);
1309 return 1; 1307 return 1;
1310 } 1308 }
1311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n"); 1309 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "success\n");
1312 return 0; 1310 return 0;
1313} 1311}
1314 1312
diff --git a/src/cadet/test_cadet_flow.c b/src/cadet/test_cadet_flow.c
index 87e7b9a03..5278dc00f 100644
--- a/src/cadet/test_cadet_flow.c
+++ b/src/cadet/test_cadet_flow.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file cadet/test_cadet_flow.c 21 * @file cadet/test_cadet_flow.c
22 * @author Bart Polot 22 * @author Bart Polot
@@ -34,8 +34,7 @@
34/** 34/**
35 * Ugly workaround to unify data handlers on incoming and outgoing channels. 35 * Ugly workaround to unify data handlers on incoming and outgoing channels.
36 */ 36 */
37struct CadetTestChannelWrapper 37struct CadetTestChannelWrapper {
38{
39 /** 38 /**
40 * Channel pointer. 39 * Channel pointer.
41 */ 40 */
@@ -50,17 +49,17 @@ struct CadetTestChannelWrapper
50/** 49/**
51 * How long until we give up on connecting the peers? 50 * How long until we give up on connecting the peers?
52 */ 51 */
53#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120) 52#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120)
54 53
55/** 54/**
56 * Time to wait by default for stuff that should be rather fast. 55 * Time to wait by default for stuff that should be rather fast.
57 */ 56 */
58#define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20) 57#define SHORT_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 20)
59 58
60/** 59/**
61 * How fast do we send messages? 60 * How fast do we send messages?
62 */ 61 */
63#define SEND_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 10) 62#define SEND_INTERVAL GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 10)
64 63
65 64
66/** 65/**
@@ -76,7 +75,7 @@ static struct GNUNET_TIME_Relative short_time;
76/** 75/**
77 * Size of each test packet's payload 76 * Size of each test packet's payload
78 */ 77 */
79static size_t size_payload = sizeof (uint32_t); 78static size_t size_payload = sizeof(uint32_t);
80 79
81/** 80/**
82 * Operation to get peer ids. 81 * Operation to get peer ids.
@@ -209,29 +208,29 @@ static unsigned int msg_dropped;
209 * Show the results of the test (banwidth acheived) and log them to GAUGER 208 * Show the results of the test (banwidth acheived) and log them to GAUGER
210 */ 209 */
211static void 210static void
212show_end_data (void) 211show_end_data(void)
213{ 212{
214 static struct GNUNET_TIME_Absolute end_time; 213 static struct GNUNET_TIME_Absolute end_time;
215 static struct GNUNET_TIME_Relative total_time; 214 static struct GNUNET_TIME_Relative total_time;
216 215
217 end_time = GNUNET_TIME_absolute_get (); 216 end_time = GNUNET_TIME_absolute_get();
218 total_time = GNUNET_TIME_absolute_get_difference (start_time, end_time); 217 total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
219 fprintf (stderr, 218 fprintf(stderr,
220 "\nResults of test \"%s\"\n", 219 "\nResults of test \"%s\"\n",
221 test_name); 220 test_name);
222 fprintf (stderr, 221 fprintf(stderr,
223 "Test time %s\n", 222 "Test time %s\n",
224 GNUNET_STRINGS_relative_time_to_string (total_time, GNUNET_YES)); 223 GNUNET_STRINGS_relative_time_to_string(total_time, GNUNET_YES));
225 fprintf (stderr, 224 fprintf(stderr,
226 "Test bandwidth: %f kb/s\n", 225 "Test bandwidth: %f kb/s\n",
227 4 * total_packets * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms 226 4 * total_packets * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms
228 fprintf (stderr, 227 fprintf(stderr,
229 "Test throughput: %f packets/s\n\n", 228 "Test throughput: %f packets/s\n\n",
230 total_packets * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms 229 total_packets * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms
231 GAUGER ("CADET", 230 GAUGER("CADET",
232 test_name, 231 test_name,
233 total_packets * 1000.0 / (total_time.rel_value_us / 1000), 232 total_packets * 1000.0 / (total_time.rel_value_us / 1000),
234 "packets/s"); 233 "packets/s");
235} 234}
236 235
237 236
@@ -242,33 +241,33 @@ show_end_data (void)
242 * @param tc Task Context. 241 * @param tc Task Context.
243 */ 242 */
244static void 243static void
245shutdown_task (void *cls) 244shutdown_task(void *cls)
246{ 245{
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 246 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
248 "Ending test.\n"); 247 "Ending test.\n");
249 if (NULL != send_next_msg_task) 248 if (NULL != send_next_msg_task)
250 { 249 {
251 GNUNET_SCHEDULER_cancel (send_next_msg_task); 250 GNUNET_SCHEDULER_cancel(send_next_msg_task);
252 send_next_msg_task = NULL; 251 send_next_msg_task = NULL;
253 } 252 }
254 if (NULL != test_task) 253 if (NULL != test_task)
255 { 254 {
256 GNUNET_SCHEDULER_cancel (test_task); 255 GNUNET_SCHEDULER_cancel(test_task);
257 test_task = NULL; 256 test_task = NULL;
258 } 257 }
259 for (unsigned int i = 0; i < 2; i++) 258 for (unsigned int i = 0; i < 2; i++)
260 GNUNET_TESTBED_operation_done (t_op[i]); 259 GNUNET_TESTBED_operation_done(t_op[i]);
261 if (NULL != outgoing_ch) 260 if (NULL != outgoing_ch)
262 { 261 {
263 GNUNET_CADET_channel_destroy (outgoing_ch); 262 GNUNET_CADET_channel_destroy(outgoing_ch);
264 outgoing_ch = NULL; 263 outgoing_ch = NULL;
265 } 264 }
266 if (NULL != incoming_ch) 265 if (NULL != incoming_ch)
267 { 266 {
268 GNUNET_CADET_channel_destroy (incoming_ch); 267 GNUNET_CADET_channel_destroy(incoming_ch);
269 incoming_ch = NULL; 268 incoming_ch = NULL;
270 } 269 }
271 GNUNET_CADET_TEST_cleanup (test_ctx); 270 GNUNET_CADET_TEST_cleanup(test_ctx);
272} 271}
273 272
274 273
@@ -282,25 +281,25 @@ shutdown_task (void *cls)
282 * operation has executed successfully. 281 * operation has executed successfully.
283 */ 282 */
284static void 283static void
285stats_cont (void *cls, 284stats_cont(void *cls,
286 struct GNUNET_TESTBED_Operation *op, 285 struct GNUNET_TESTBED_Operation *op,
287 const char *emsg) 286 const char *emsg)
288{ 287{
289 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 288 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
290 "KA sent: %u, KA received: %u\n", 289 "KA sent: %u, KA received: %u\n",
291 ka_sent, 290 ka_sent,
292 ka_received); 291 ka_received);
293 if ((KEEPALIVE == test) && ((ka_sent < 2) || (ka_sent > ka_received + 1))) 292 if ((KEEPALIVE == test) && ((ka_sent < 2) || (ka_sent > ka_received + 1)))
294 { 293 {
295 GNUNET_break (0); 294 GNUNET_break(0);
296 ok--; 295 ok--;
297 } 296 }
298 GNUNET_TESTBED_operation_done (stats_op); 297 GNUNET_TESTBED_operation_done(stats_op);
299 298
300 if (NULL != disconnect_task) 299 if (NULL != disconnect_task)
301 GNUNET_SCHEDULER_cancel (disconnect_task); 300 GNUNET_SCHEDULER_cancel(disconnect_task);
302 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, 301 disconnect_task = GNUNET_SCHEDULER_add_now(&disconnect_cadet_peers,
303 cls); 302 cls);
304} 303}
305 304
306 305
@@ -316,12 +315,12 @@ stats_cont (void *cls,
316 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration 315 * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
317 */ 316 */
318static int 317static int
319stats_iterator (void *cls, 318stats_iterator(void *cls,
320 const struct GNUNET_TESTBED_Peer *peer, 319 const struct GNUNET_TESTBED_Peer *peer,
321 const char *subsystem, 320 const char *subsystem,
322 const char *name, 321 const char *name,
323 uint64_t value, 322 uint64_t value,
324 int is_persistent) 323 int is_persistent)
325{ 324{
326 static const char *s_sent = "# keepalives sent"; 325 static const char *s_sent = "# keepalives sent";
327 static const char *s_recv = "# keepalives received"; 326 static const char *s_recv = "# keepalives received";
@@ -329,16 +328,16 @@ stats_iterator (void *cls,
329 static const char *cdrops = "# messages dropped due to slow client"; 328 static const char *cdrops = "# messages dropped due to slow client";
330 uint32_t i; 329 uint32_t i;
331 330
332 i = GNUNET_TESTBED_get_index (peer); 331 i = GNUNET_TESTBED_get_index(peer);
333 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "STATS PEER %u - %s [%s]: %llu\n", i, 332 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "STATS PEER %u - %s [%s]: %llu\n", i,
334 subsystem, name, (unsigned long long) value); 333 subsystem, name, (unsigned long long)value);
335 if (0 == strncmp (s_sent, name, strlen (s_sent)) && 0 == i) 334 if (0 == strncmp(s_sent, name, strlen(s_sent)) && 0 == i)
336 ka_sent = value; 335 ka_sent = value;
337 if (0 == strncmp (s_recv, name, strlen (s_recv)) && peers_requested - 1 == i) 336 if (0 == strncmp(s_recv, name, strlen(s_recv)) && peers_requested - 1 == i)
338 ka_received = value; 337 ka_received = value;
339 if (0 == strncmp (rdrops, name, strlen (rdrops))) 338 if (0 == strncmp(rdrops, name, strlen(rdrops)))
340 msg_dropped += value; 339 msg_dropped += value;
341 if (0 == strncmp (cdrops, name, strlen (cdrops))) 340 if (0 == strncmp(cdrops, name, strlen(cdrops)))
342 msg_dropped += value; 341 msg_dropped += value;
343 342
344 return GNUNET_OK; 343 return GNUNET_OK;
@@ -351,26 +350,26 @@ stats_iterator (void *cls,
351 * @param cls Closure (line from which the task was scheduled). 350 * @param cls Closure (line from which the task was scheduled).
352 */ 351 */
353static void 352static void
354gather_stats_and_exit (void *cls) 353gather_stats_and_exit(void *cls)
355{ 354{
356 long l = (long) cls; 355 long l = (long)cls;
357 356
358 disconnect_task = NULL; 357 disconnect_task = NULL;
359 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 358 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
360 "gathering statistics from line %ld\n", 359 "gathering statistics from line %ld\n",
361 l); 360 l);
362 if (NULL != outgoing_ch) 361 if (NULL != outgoing_ch)
363 { 362 {
364 GNUNET_CADET_channel_destroy (outgoing_ch); 363 GNUNET_CADET_channel_destroy(outgoing_ch);
365 outgoing_ch = NULL; 364 outgoing_ch = NULL;
366 } 365 }
367 stats_op = GNUNET_TESTBED_get_statistics (peers_running, 366 stats_op = GNUNET_TESTBED_get_statistics(peers_running,
368 testbed_peers, 367 testbed_peers,
369 "cadet", 368 "cadet",
370 NULL, 369 NULL,
371 &stats_iterator, 370 &stats_iterator,
372 stats_cont, 371 stats_cont,
373 cls); 372 cls);
374} 373}
375 374
376 375
@@ -380,18 +379,18 @@ gather_stats_and_exit (void *cls)
380 * @param line Line in the code the abort is requested from (__LINE__). 379 * @param line Line in the code the abort is requested from (__LINE__).
381 */ 380 */
382static void 381static void
383abort_test (long line) 382abort_test(long line)
384{ 383{
385 if (NULL != disconnect_task) 384 if (NULL != disconnect_task)
386 { 385 {
387 GNUNET_SCHEDULER_cancel (disconnect_task); 386 GNUNET_SCHEDULER_cancel(disconnect_task);
388 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 387 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
389 "Aborting test from %ld\n", 388 "Aborting test from %ld\n",
390 line); 389 line);
391 disconnect_task = 390 disconnect_task =
392 GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, 391 GNUNET_SCHEDULER_add_now(&disconnect_cadet_peers,
393 (void *) line); 392 (void *)line);
394 } 393 }
395} 394}
396 395
397 396
@@ -403,7 +402,7 @@ abort_test (long line)
403 * @param channel Channel to send the message on. 402 * @param channel Channel to send the message on.
404 */ 403 */
405static void 404static void
406send_test_message (struct GNUNET_CADET_Channel *channel) 405send_test_message(struct GNUNET_CADET_Channel *channel)
407{ 406{
408 struct GNUNET_MQ_Envelope *env; 407 struct GNUNET_MQ_Envelope *env;
409 struct GNUNET_MessageHeader *msg; 408 struct GNUNET_MessageHeader *msg;
@@ -411,56 +410,56 @@ send_test_message (struct GNUNET_CADET_Channel *channel)
411 int payload; 410 int payload;
412 int size; 411 int size;
413 412
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 413 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
415 "Sending test message on channel %p\n", 414 "Sending test message on channel %p\n",
416 channel); 415 channel);
417 size = size_payload; 416 size = size_payload;
418 if (GNUNET_NO == initialized) 417 if (GNUNET_NO == initialized)
419 { 418 {
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending INITIALIZER\n"); 419 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending INITIALIZER\n");
421 size += 1000; 420 size += 1000;
422 payload = data_sent; 421 payload = data_sent;
423 if (SPEED_ACK == test) // FIXME unify SPEED_ACK with an initializer 422 if (SPEED_ACK == test) // FIXME unify SPEED_ACK with an initializer
424 data_sent++; 423 data_sent++;
425 } 424 }
426 else if (SPEED == test || SPEED_ACK == test) 425 else if (SPEED == test || SPEED_ACK == test)
427 { 426 {
428 if (get_target_channel() == channel) 427 if (get_target_channel() == channel)
428 {
429 payload = ack_sent;
430 size += ack_sent;
431 ack_sent++;
432 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
433 "Sending ACK %u [%d bytes]\n",
434 payload, size);
435 }
436 else
437 {
438 payload = data_sent;
439 size += data_sent;
440 data_sent++;
441 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
442 "Sending DATA %u [%d bytes]\n",
443 data_sent, size);
444 }
445 }
446 else if (FORWARD == test)
429 { 447 {
430 payload = ack_sent; 448 payload = ack_sent;
431 size += ack_sent;
432 ack_sent++;
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434 "Sending ACK %u [%d bytes]\n",
435 payload, size);
436 } 449 }
437 else 450 else if (P2P_SIGNAL == test)
438 { 451 {
439 payload = data_sent; 452 payload = data_sent;
440 size += data_sent;
441 data_sent++;
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
443 "Sending DATA %u [%d bytes]\n",
444 data_sent, size);
445 } 453 }
446 }
447 else if (FORWARD == test)
448 {
449 payload = ack_sent;
450 }
451 else if (P2P_SIGNAL == test)
452 {
453 payload = data_sent;
454 }
455 else 454 else
456 { 455 {
457 GNUNET_assert (0); 456 GNUNET_assert(0);
458 } 457 }
459 env = GNUNET_MQ_msg_extra (msg, size, GNUNET_MESSAGE_TYPE_DUMMY); 458 env = GNUNET_MQ_msg_extra(msg, size, GNUNET_MESSAGE_TYPE_DUMMY);
460 459
461 data = (uint32_t *) &msg[1]; 460 data = (uint32_t *)&msg[1];
462 *data = htonl (payload); 461 *data = htonl(payload);
463 GNUNET_MQ_send (GNUNET_CADET_get_mq (channel), env); 462 GNUNET_MQ_send(GNUNET_CADET_get_mq(channel), env);
464} 463}
465 464
466 465
@@ -471,30 +470,30 @@ send_test_message (struct GNUNET_CADET_Channel *channel)
471 * @param cls Closure (unused). 470 * @param cls Closure (unused).
472 */ 471 */
473static void 472static void
474send_next_msg (void *cls) 473send_next_msg(void *cls)
475{ 474{
476 struct GNUNET_CADET_Channel *channel; 475 struct GNUNET_CADET_Channel *channel;
477 476
478 send_next_msg_task = NULL; 477 send_next_msg_task = NULL;
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 478 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
480 "Sending next message: %d\n", 479 "Sending next message: %d\n",
481 data_sent); 480 data_sent);
482 481
483 channel = GNUNET_YES == test_backwards ? incoming_ch : outgoing_ch; 482 channel = GNUNET_YES == test_backwards ? incoming_ch : outgoing_ch;
484 GNUNET_assert (NULL != channel); 483 GNUNET_assert(NULL != channel);
485 GNUNET_assert (SPEED == test); 484 GNUNET_assert(SPEED == test);
486 send_test_message (channel); 485 send_test_message(channel);
487 if (data_sent < total_packets) 486 if (data_sent < total_packets)
488 { 487 {
489 /* SPEED test: Send all messages as soon as possible */ 488 /* SPEED test: Send all messages as soon as possible */
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 489 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
491 "Scheduling message %d\n", 490 "Scheduling message %d\n",
492 data_sent + 1); 491 data_sent + 1);
493 send_next_msg_task = 492 send_next_msg_task =
494 GNUNET_SCHEDULER_add_delayed (SEND_INTERVAL, 493 GNUNET_SCHEDULER_add_delayed(SEND_INTERVAL,
495 &send_next_msg, 494 &send_next_msg,
496 NULL); 495 NULL);
497 } 496 }
498} 497}
499 498
500 499
@@ -507,8 +506,8 @@ send_next_msg (void *cls)
507 * #GNUNET_SYSERR to close it (signal serious error). 506 * #GNUNET_SYSERR to close it (signal serious error).
508 */ 507 */
509static int 508static int
510check_data (void *cls, 509check_data(void *cls,
511 const struct GNUNET_MessageHeader *message) 510 const struct GNUNET_MessageHeader *message)
512{ 511{
513 return GNUNET_OK; /* all is well-formed */ 512 return GNUNET_OK; /* all is well-formed */
514} 513}
@@ -521,8 +520,8 @@ check_data (void *cls,
521 * @param message the actual message 520 * @param message the actual message
522 */ 521 */
523static void 522static void
524handle_data (void *cls, 523handle_data(void *cls,
525 const struct GNUNET_MessageHeader *message) 524 const struct GNUNET_MessageHeader *message)
526{ 525{
527 struct CadetTestChannelWrapper *ch = cls; 526 struct CadetTestChannelWrapper *ch = cls;
528 struct GNUNET_CADET_Channel *channel = ch->ch; 527 struct GNUNET_CADET_Channel *channel = ch->ch;
@@ -530,73 +529,73 @@ handle_data (void *cls,
530 uint32_t payload; 529 uint32_t payload;
531 int *counter; 530 int *counter;
532 531
533 GNUNET_CADET_receive_done (channel); 532 GNUNET_CADET_receive_done(channel);
534 counter = get_target_channel () == channel ? &data_received : &ack_received; 533 counter = get_target_channel() == channel ? &data_received : &ack_received;
535 if (channel == outgoing_ch) 534 if (channel == outgoing_ch)
536 { 535 {
537 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 536 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
538 "Root client got a message.\n"); 537 "Root client got a message.\n");
539 } 538 }
540 else if (channel == incoming_ch) 539 else if (channel == incoming_ch)
541 { 540 {
542 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 541 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
543 "Leaf client got a message.\n"); 542 "Leaf client got a message.\n");
544 } 543 }
545 else 544 else
546 { 545 {
547 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 546 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
548 "Unknown channel %p.\n", 547 "Unknown channel %p.\n",
549 channel); 548 channel);
550 GNUNET_assert (0); 549 GNUNET_assert(0);
551 } 550 }
552 551
553 data = (uint32_t *) &message[1]; 552 data = (uint32_t *)&message[1];
554 payload = ntohl (*data); 553 payload = ntohl(*data);
555 if (payload == *counter) 554 if (payload == *counter)
556 { 555 {
557 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 556 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
558 "Payload as expected: %u\n", 557 "Payload as expected: %u\n",
559 payload); 558 payload);
560 } 559 }
561 else 560 else
562 {
563 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
564 "Received payload %u, expected: %u\n",
565 payload, *counter);
566 }
567 (*counter)++;
568 if (get_target_channel () == channel) /* Got "data" */
569 {
570 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
571 " received data %u\n",
572 data_received);
573 if (data_received < total_packets)
574 return;
575 }
576 else /* Got "ack" */
577 {
578 if (SPEED_ACK == test || SPEED == test)
579 { 561 {
580 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received ack %u\n", ack_received); 562 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
581 /* Send more data */ 563 "Received payload %u, expected: %u\n",
582 send_test_message (channel); 564 payload, *counter);
583 if (ack_received < total_packets && SPEED != test)
584 return;
585 if (ok == 2 && SPEED == test)
586 return;
587 show_end_data ();
588 } 565 }
589 if (test == P2P_SIGNAL) 566 (*counter)++;
567 if (get_target_channel() == channel) /* Got "data" */
590 { 568 {
591 GNUNET_CADET_channel_destroy (incoming_ch); 569 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
592 incoming_ch = NULL; 570 " received data %u\n",
571 data_received);
572 if (data_received < total_packets)
573 return;
593 } 574 }
594 else 575 else /* Got "ack" */
595 { 576 {
596 GNUNET_CADET_channel_destroy (outgoing_ch); 577 if (SPEED_ACK == test || SPEED == test)
597 outgoing_ch = NULL; 578 {
579 GNUNET_log(GNUNET_ERROR_TYPE_INFO, " received ack %u\n", ack_received);
580 /* Send more data */
581 send_test_message(channel);
582 if (ack_received < total_packets && SPEED != test)
583 return;
584 if (ok == 2 && SPEED == test)
585 return;
586 show_end_data();
587 }
588 if (test == P2P_SIGNAL)
589 {
590 GNUNET_CADET_channel_destroy(incoming_ch);
591 incoming_ch = NULL;
592 }
593 else
594 {
595 GNUNET_CADET_channel_destroy(outgoing_ch);
596 outgoing_ch = NULL;
597 }
598 } 598 }
599 }
600} 599}
601 600
602 601
@@ -613,37 +612,37 @@ handle_data (void *cls,
613 * received on the @a channel. 612 * received on the @a channel.
614 */ 613 */
615static void * 614static void *
616connect_handler (void *cls, 615connect_handler(void *cls,
617 struct GNUNET_CADET_Channel *channel, 616 struct GNUNET_CADET_Channel *channel,
618 const struct GNUNET_PeerIdentity *source) 617 const struct GNUNET_PeerIdentity *source)
619{ 618{
620 struct CadetTestChannelWrapper *ch; 619 struct CadetTestChannelWrapper *ch;
621 long peer = (long) cls; 620 long peer = (long)cls;
622 621
623 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 622 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
624 "Incoming channel from %s to %ld: %p\n", 623 "Incoming channel from %s to %ld: %p\n",
625 GNUNET_i2s (source), 624 GNUNET_i2s(source),
626 peer, 625 peer,
627 channel); 626 channel);
628 if (peer == peers_requested - 1) 627 if (peer == peers_requested - 1)
629 {
630 if (NULL != incoming_ch)
631 { 628 {
632 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 629 if (NULL != incoming_ch)
633 "Duplicate incoming channel for client %lu\n", 630 {
634 (long) cls); 631 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
635 GNUNET_assert (0); 632 "Duplicate incoming channel for client %lu\n",
633 (long)cls);
634 GNUNET_assert(0);
635 }
636 incoming_ch = channel;
636 } 637 }
637 incoming_ch = channel;
638 }
639 else 638 else
640 { 639 {
641 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 640 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
642 "Incoming channel for unexpected peer #%lu\n", 641 "Incoming channel for unexpected peer #%lu\n",
643 (long) cls); 642 (long)cls);
644 GNUNET_assert (0); 643 GNUNET_assert(0);
645 } 644 }
646 ch = GNUNET_new (struct CadetTestChannelWrapper); 645 ch = GNUNET_new(struct CadetTestChannelWrapper);
647 ch->ch = channel; 646 ch->ch = channel;
648 647
649 return ch; 648 return ch;
@@ -662,28 +661,28 @@ connect_handler (void *cls,
662 * @param channel Connection to the other end (henceforth invalid). 661 * @param channel Connection to the other end (henceforth invalid).
663 */ 662 */
664static void 663static void
665disconnect_handler (void *cls, 664disconnect_handler(void *cls,
666 const struct GNUNET_CADET_Channel *channel) 665 const struct GNUNET_CADET_Channel *channel)
667{ 666{
668 struct CadetTestChannelWrapper *ch_w = cls; 667 struct CadetTestChannelWrapper *ch_w = cls;
669 668
670 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 669 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
671 "Channel disconnected at %d\n", 670 "Channel disconnected at %d\n",
672 ok); 671 ok);
673 GNUNET_assert (ch_w->ch == channel); 672 GNUNET_assert(ch_w->ch == channel);
674 if (channel == incoming_ch) 673 if (channel == incoming_ch)
675 incoming_ch = NULL; 674 incoming_ch = NULL;
676 else if (outgoing_ch == channel) 675 else if (outgoing_ch == channel)
677 outgoing_ch = NULL; 676 outgoing_ch = NULL;
678 else 677 else
679 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 678 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
680 "Disconnect on unknown channel %p\n", 679 "Disconnect on unknown channel %p\n",
681 channel); 680 channel);
682 if (NULL != disconnect_task) 681 if (NULL != disconnect_task)
683 GNUNET_SCHEDULER_cancel (disconnect_task); 682 GNUNET_SCHEDULER_cancel(disconnect_task);
684 disconnect_task = GNUNET_SCHEDULER_add_now (&gather_stats_and_exit, 683 disconnect_task = GNUNET_SCHEDULER_add_now(&gather_stats_and_exit,
685 (void *) __LINE__); 684 (void *)__LINE__);
686 GNUNET_free (ch_w); 685 GNUNET_free(ch_w);
687} 686}
688 687
689 688
@@ -696,41 +695,41 @@ disconnect_handler (void *cls,
696 * @param cls Closure (unused). 695 * @param cls Closure (unused).
697 */ 696 */
698static void 697static void
699start_test (void *cls) 698start_test(void *cls)
700{ 699{
701 struct GNUNET_MQ_MessageHandler handlers[] = { 700 struct GNUNET_MQ_MessageHandler handlers[] = {
702 GNUNET_MQ_hd_var_size (data, 701 GNUNET_MQ_hd_var_size(data,
703 GNUNET_MESSAGE_TYPE_DUMMY, 702 GNUNET_MESSAGE_TYPE_DUMMY,
704 struct GNUNET_MessageHeader, 703 struct GNUNET_MessageHeader,
705 NULL), 704 NULL),
706 GNUNET_MQ_handler_end () 705 GNUNET_MQ_handler_end()
707 }; 706 };
708 struct CadetTestChannelWrapper *ch; 707 struct CadetTestChannelWrapper *ch;
709 enum GNUNET_CADET_ChannelOption flags; 708 enum GNUNET_CADET_ChannelOption flags;
710 709
711 test_task = NULL; 710 test_task = NULL;
712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 711 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
713 "In start_test\n"); 712 "In start_test\n");
714 start_time = GNUNET_TIME_absolute_get (); 713 start_time = GNUNET_TIME_absolute_get();
715 ch = GNUNET_new (struct CadetTestChannelWrapper); 714 ch = GNUNET_new(struct CadetTestChannelWrapper);
716 outgoing_ch = GNUNET_CADET_channel_create (h1, 715 outgoing_ch = GNUNET_CADET_channel_create(h1,
717 ch, 716 ch,
718 p_id[1], 717 p_id[1],
719 &port, 718 &port,
720 flags, 719 flags,
721 NULL, 720 NULL,
722 &disconnect_handler, 721 &disconnect_handler,
723 handlers); 722 handlers);
724 ch->ch = outgoing_ch; 723 ch->ch = outgoing_ch;
725 GNUNET_assert (NULL == disconnect_task); 724 GNUNET_assert(NULL == disconnect_task);
726 disconnect_task 725 disconnect_task
727 = GNUNET_SCHEDULER_add_delayed (short_time, 726 = GNUNET_SCHEDULER_add_delayed(short_time,
728 &gather_stats_and_exit, 727 &gather_stats_and_exit,
729 (void *) __LINE__); 728 (void *)__LINE__);
730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 729 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
731 "Sending data initializer on channel %p...\n", 730 "Sending data initializer on channel %p...\n",
732 outgoing_ch); 731 outgoing_ch);
733 send_test_message (outgoing_ch); 732 send_test_message(outgoing_ch);
734} 733}
735 734
736 735
@@ -744,36 +743,36 @@ start_test (void *cls)
744 * NULL if the operation is successfull 743 * NULL if the operation is successfull
745 */ 744 */
746static void 745static void
747pi_cb (void *cls, 746pi_cb(void *cls,
748 struct GNUNET_TESTBED_Operation *op, 747 struct GNUNET_TESTBED_Operation *op,
749 const struct GNUNET_TESTBED_PeerInformation *pinfo, 748 const struct GNUNET_TESTBED_PeerInformation *pinfo,
750 const char *emsg) 749 const char *emsg)
751{ 750{
752 long i = (long) cls; 751 long i = (long)cls;
753 752
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 753 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
755 "ID callback for %ld\n", 754 "ID callback for %ld\n",
756 i); 755 i);
757 if ( (NULL == pinfo) || 756 if ((NULL == pinfo) ||
758 (NULL != emsg) ) 757 (NULL != emsg))
759 { 758 {
760 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 759 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
761 "pi_cb: %s\n", 760 "pi_cb: %s\n",
762 emsg); 761 emsg);
763 abort_test (__LINE__); 762 abort_test(__LINE__);
764 return; 763 return;
765 } 764 }
766 p_id[i] = pinfo->result.id; 765 p_id[i] = pinfo->result.id;
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 766 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
768 "id: %s\n", 767 "id: %s\n",
769 GNUNET_i2s (p_id[i])); 768 GNUNET_i2s(p_id[i]));
770 p_ids++; 769 p_ids++;
771 if (p_ids < 2) 770 if (p_ids < 2)
772 return; 771 return;
773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 772 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
774 "Got all IDs, starting test\n"); 773 "Got all IDs, starting test\n");
775 test_task = GNUNET_SCHEDULER_add_now (&start_test, 774 test_task = GNUNET_SCHEDULER_add_now(&start_test,
776 NULL); 775 NULL);
777} 776}
778 777
779 778
@@ -787,33 +786,33 @@ pi_cb (void *cls,
787 * @param cadets Handle to each of the CADETs of the peers. 786 * @param cadets Handle to each of the CADETs of the peers.
788 */ 787 */
789static void 788static void
790tmain (void *cls, 789tmain(void *cls,
791 struct GNUNET_CADET_TEST_Context *ctx, 790 struct GNUNET_CADET_TEST_Context *ctx,
792 unsigned int num_peers, 791 unsigned int num_peers,
793 struct GNUNET_TESTBED_Peer **peers, 792 struct GNUNET_TESTBED_Peer **peers,
794 struct GNUNET_CADET_Handle **cadets) 793 struct GNUNET_CADET_Handle **cadets)
795{ 794{
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 795 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
797 "test main\n"); 796 "test main\n");
798 test_ctx = ctx; 797 test_ctx = ctx;
799 peers_running = num_peers; 798 peers_running = num_peers;
800 GNUNET_assert (peers_running == peers_requested); 799 GNUNET_assert(peers_running == peers_requested);
801 testbed_peers = peers; 800 testbed_peers = peers;
802 h1 = cadets[0]; 801 h1 = cadets[0];
803 h2 = cadets[num_peers - 1]; 802 h2 = cadets[num_peers - 1];
804 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, 803 GNUNET_SCHEDULER_add_shutdown(&shutdown_task,
805 NULL); 804 NULL);
806 p_ids = 0; 805 p_ids = 0;
807 t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0], 806 t_op[0] = GNUNET_TESTBED_peer_get_information(peers[0],
808 GNUNET_TESTBED_PIT_IDENTITY, 807 GNUNET_TESTBED_PIT_IDENTITY,
809 &pi_cb, 808 &pi_cb,
810 (void *) 0L); 809 (void *)0L);
811 t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1], 810 t_op[1] = GNUNET_TESTBED_peer_get_information(peers[num_peers - 1],
812 GNUNET_TESTBED_PIT_IDENTITY, 811 GNUNET_TESTBED_PIT_IDENTITY,
813 &pi_cb, 812 &pi_cb,
814 (void *) 1L); 813 (void *)1L);
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 814 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
816 "requested peer ids\n"); 815 "requested peer ids\n");
817} 816}
818 817
819 818
@@ -821,67 +820,67 @@ tmain (void *cls,
821 * Main: start test 820 * Main: start test
822 */ 821 */
823int 822int
824main (int argc, 823main(int argc,
825 char *argv[]) 824 char *argv[])
826{ 825{
827 static const struct GNUNET_HashCode *ports[2]; 826 static const struct GNUNET_HashCode *ports[2];
828 struct GNUNET_MQ_MessageHandler handlers[] = { 827 struct GNUNET_MQ_MessageHandler handlers[] = {
829 GNUNET_MQ_hd_var_size (data, 828 GNUNET_MQ_hd_var_size(data,
830 GNUNET_MESSAGE_TYPE_DUMMY, 829 GNUNET_MESSAGE_TYPE_DUMMY,
831 struct GNUNET_MessageHeader, 830 struct GNUNET_MessageHeader,
832 NULL), 831 NULL),
833 GNUNET_MQ_handler_end () 832 GNUNET_MQ_handler_end()
834 }; 833 };
835 const char *config_file = "test_cadet.conf"; 834 const char *config_file = "test_cadet.conf";
836 char port_id[] = "test port"; 835 char port_id[] = "test port";
837 struct GNUNET_GETOPT_CommandLineOption options[] = { 836 struct GNUNET_GETOPT_CommandLineOption options[] = {
838 GNUNET_GETOPT_option_relative_time ('t', 837 GNUNET_GETOPT_option_relative_time('t',
839 "time", 838 "time",
840 "short_time", 839 "short_time",
841 gettext_noop ("set short timeout"), 840 gettext_noop("set short timeout"),
842 &short_time), 841 &short_time),
843 GNUNET_GETOPT_option_uint ('m', 842 GNUNET_GETOPT_option_uint('m',
844 "messages", 843 "messages",
845 "NUM_MESSAGES", 844 "NUM_MESSAGES",
846 gettext_noop ("set number of messages to send"), 845 gettext_noop("set number of messages to send"),
847 &total_packets), 846 &total_packets),
848 GNUNET_GETOPT_option_uint ('p', 847 GNUNET_GETOPT_option_uint('p',
849 "peers", 848 "peers",
850 "NUM_PEERS", 849 "NUM_PEERS",
851 gettext_noop ("number of peers to launch"), 850 gettext_noop("number of peers to launch"),
852 &peers_requested), 851 &peers_requested),
853 GNUNET_GETOPT_OPTION_END 852 GNUNET_GETOPT_OPTION_END
854 }; 853 };
855 854
856 GNUNET_log_setup ("test-cadet-flow", 855 GNUNET_log_setup("test-cadet-flow",
857 "DEBUG", 856 "DEBUG",
858 NULL); 857 NULL);
859 total_packets = TOTAL_PACKETS; 858 total_packets = TOTAL_PACKETS;
860 short_time = SHORT_TIME; 859 short_time = SHORT_TIME;
861 if (-1 == GNUNET_GETOPT_run (argv[0], 860 if (-1 == GNUNET_GETOPT_run(argv[0],
862 options, 861 options,
863 argc, 862 argc,
864 argv)) 863 argv))
865 { 864 {
866 fprintf (stderr, 865 fprintf(stderr,
867 "test failed: problem with CLI parameters\n"); 866 "test failed: problem with CLI parameters\n");
868 return 1; 867 return 1;
869 } 868 }
870 GNUNET_CRYPTO_hash (port_id, 869 GNUNET_CRYPTO_hash(port_id,
871 sizeof (port_id), 870 sizeof(port_id),
872 &port); 871 &port);
873 ports[0] = &port; 872 ports[0] = &port;
874 ports[1] = NULL; 873 ports[1] = NULL;
875 GNUNET_CADET_TEST_ruN ("test_cadet_flow", 874 GNUNET_CADET_TEST_ruN("test_cadet_flow",
876 config_file, 875 config_file,
877 peers_requested, 876 peers_requested,
878 &tmain, 877 &tmain,
879 NULL, /* tmain cls */ 878 NULL, /* tmain cls */
880 &connect_handler, 879 &connect_handler,
881 NULL, 880 NULL,
882 &disconnect_handler, 881 &disconnect_handler,
883 handlers, 882 handlers,
884 ports); 883 ports);
885 return 0; 884 return 0;
886} 885}
887 886
diff --git a/src/cadet/test_cadet_local_mq.c b/src/cadet/test_cadet_local_mq.c
index b2fed5bd6..7ae0a90c4 100644
--- a/src/cadet/test_cadet_local_mq.c
+++ b/src/cadet/test_cadet_local_mq.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file cadet/test_cadet_local_mq.c 22 * @file cadet/test_cadet_local_mq.c
@@ -36,8 +36,7 @@
36/** 36/**
37 * Test message structure. 37 * Test message structure.
38 */ 38 */
39struct GNUNET_CADET_TestMsg 39struct GNUNET_CADET_TestMsg {
40{
41 /** 40 /**
42 * Type: #TEST_MESSAGE_TYPE 41 * Type: #TEST_MESSAGE_TYPE
43 * 42 *
@@ -74,46 +73,46 @@ static struct GNUNET_SCHEDULER_Task *connect_task;
74 * @param cls Closue (unused). 73 * @param cls Closue (unused).
75 */ 74 */
76static void 75static void
77do_connect (void *cls); 76do_connect(void *cls);
78 77
79 78
80/** 79/**
81 * Shutdown nicely 80 * Shutdown nicely
82 */ 81 */
83static void 82static void
84do_shutdown (void *cls) 83do_shutdown(void *cls)
85{ 84{
86 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 85 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
87 "shutdown\n"); 86 "shutdown\n");
88 if (NULL != abort_task) 87 if (NULL != abort_task)
89 { 88 {
90 GNUNET_SCHEDULER_cancel (abort_task); 89 GNUNET_SCHEDULER_cancel(abort_task);
91 abort_task = NULL; 90 abort_task = NULL;
92 } 91 }
93 if (NULL != ch) 92 if (NULL != ch)
94 { 93 {
95 GNUNET_CADET_channel_destroy (ch); 94 GNUNET_CADET_channel_destroy(ch);
96 ch = NULL; 95 ch = NULL;
97 } 96 }
98 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 97 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
99 "Disconnect client 1\n"); 98 "Disconnect client 1\n");
100 if (NULL != cadet_peer_1) 99 if (NULL != cadet_peer_1)
101 { 100 {
102 GNUNET_CADET_disconnect (cadet_peer_1); 101 GNUNET_CADET_disconnect(cadet_peer_1);
103 cadet_peer_1 = NULL; 102 cadet_peer_1 = NULL;
104 } 103 }
105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 104 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
106 "Disconnect client 2\n"); 105 "Disconnect client 2\n");
107 if (NULL != cadet_peer_2) 106 if (NULL != cadet_peer_2)
108 { 107 {
109 GNUNET_CADET_disconnect (cadet_peer_2); 108 GNUNET_CADET_disconnect(cadet_peer_2);
110 cadet_peer_2 = NULL; 109 cadet_peer_2 = NULL;
111 } 110 }
112 if (NULL != connect_task) 111 if (NULL != connect_task)
113 { 112 {
114 GNUNET_SCHEDULER_cancel (connect_task); 113 GNUNET_SCHEDULER_cancel(connect_task);
115 connect_task = NULL; 114 connect_task = NULL;
116 } 115 }
117} 116}
118 117
119 118
@@ -121,14 +120,14 @@ do_shutdown (void *cls)
121 * Something went wrong and timed out. Kill everything and set error flag 120 * Something went wrong and timed out. Kill everything and set error flag
122 */ 121 */
123static void 122static void
124do_abort (void *cls) 123do_abort(void *cls)
125{ 124{
126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 125 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
127 "ABORT from line %ld\n", 126 "ABORT from line %ld\n",
128 (long) cls); 127 (long)cls);
129 result = GNUNET_SYSERR; 128 result = GNUNET_SYSERR;
130 abort_task = NULL; 129 abort_task = NULL;
131 GNUNET_SCHEDULER_shutdown (); 130 GNUNET_SCHEDULER_shutdown();
132} 131}
133 132
134/** 133/**
@@ -144,14 +143,14 @@ do_abort (void *cls)
144 * received on the @a channel. 143 * received on the @a channel.
145 */ 144 */
146static void * 145static void *
147connected (void *cls, 146connected(void *cls,
148 struct GNUNET_CADET_Channel *channel, 147 struct GNUNET_CADET_Channel *channel,
149 const struct GNUNET_PeerIdentity *source) 148 const struct GNUNET_PeerIdentity *source)
150{ 149{
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 150 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
152 "connected %s, cls: %p\n", 151 "connected %s, cls: %p\n",
153 GNUNET_i2s(source), 152 GNUNET_i2s(source),
154 cls); 153 cls);
155 return channel; 154 return channel;
156} 155}
157 156
@@ -167,12 +166,12 @@ connected (void *cls,
167 * @param channel Connection to the other end (henceforth invalid). 166 * @param channel Connection to the other end (henceforth invalid).
168 */ 167 */
169static void 168static void
170disconnected (void *cls, 169disconnected(void *cls,
171 const struct GNUNET_CADET_Channel *channel) 170 const struct GNUNET_CADET_Channel *channel)
172{ 171{
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 172 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
174 "disconnected channel %p, cls: %p\n", 173 "disconnected channel %p, cls: %p\n",
175 channel, cls); 174 channel, cls);
176 if (channel == ch) 175 if (channel == ch)
177 ch = NULL; 176 ch = NULL;
178} 177}
@@ -185,19 +184,19 @@ disconnected (void *cls,
185 * @param msg A message with the details of the new incoming channel 184 * @param msg A message with the details of the new incoming channel
186 */ 185 */
187static void 186static void
188handle_data_received (void *cls, 187handle_data_received(void *cls,
189 const struct GNUNET_CADET_TestMsg *msg) 188 const struct GNUNET_CADET_TestMsg *msg)
190{ 189{
191 uint64_t payload; 190 uint64_t payload;
192 191
193 payload = GNUNET_ntohll (msg->payload); 192 payload = GNUNET_ntohll(msg->payload);
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 193 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
195 "Data callback payload %llu with cls: %p! Shutting down.\n", 194 "Data callback payload %llu with cls: %p! Shutting down.\n",
196 (unsigned long long) payload, 195 (unsigned long long)payload,
197 cls); 196 cls);
198 GNUNET_assert (42 == payload); 197 GNUNET_assert(42 == payload);
199 got_data = GNUNET_YES; 198 got_data = GNUNET_YES;
200 GNUNET_SCHEDULER_shutdown (); 199 GNUNET_SCHEDULER_shutdown();
201} 200}
202 201
203 202
@@ -207,10 +206,10 @@ handle_data_received (void *cls,
207 * @param cls Closure (unused). 206 * @param cls Closure (unused).
208 */ 207 */
209static void 208static void
210message_sent (void *cls) 209message_sent(void *cls)
211{ 210{
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 211 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
213 "message sent\n"); 212 "message sent\n");
214} 213}
215 214
216 215
@@ -220,7 +219,7 @@ message_sent (void *cls)
220 * @param cls Closure (unused). 219 * @param cls Closure (unused).
221 */ 220 */
222static void 221static void
223do_connect (void *cls) 222do_connect(void *cls)
224{ 223{
225 struct GNUNET_PeerIdentity id; 224 struct GNUNET_PeerIdentity id;
226 struct GNUNET_MQ_Handle *mq; 225 struct GNUNET_MQ_Handle *mq;
@@ -228,32 +227,32 @@ do_connect (void *cls)
228 struct GNUNET_CADET_TestMsg *msg; 227 struct GNUNET_CADET_TestMsg *msg;
229 228
230 struct GNUNET_MQ_MessageHandler handlers[] = { 229 struct GNUNET_MQ_MessageHandler handlers[] = {
231 GNUNET_MQ_hd_fixed_size (data_received, 230 GNUNET_MQ_hd_fixed_size(data_received,
232 TEST_MESSAGE_TYPE, 231 TEST_MESSAGE_TYPE,
233 struct GNUNET_CADET_TestMsg, 232 struct GNUNET_CADET_TestMsg,
234 cadet_peer_1), 233 cadet_peer_1),
235 GNUNET_MQ_handler_end () 234 GNUNET_MQ_handler_end()
236 }; 235 };
237 236
238 connect_task = NULL; 237 connect_task = NULL;
239 GNUNET_TESTING_peer_get_identity (me, &id); 238 GNUNET_TESTING_peer_get_identity(me, &id);
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 239 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
241 "creating channel\n"); 240 "creating channel\n");
242 ch = GNUNET_CADET_channel_create (cadet_peer_1, /* cadet handle */ 241 ch = GNUNET_CADET_channel_create(cadet_peer_1, /* cadet handle */
243 NULL, /* channel cls */ 242 NULL, /* channel cls */
244 &id, /* destination */ 243 &id, /* destination */
245 GC_u2h (TEST_MESSAGE_TYPE), /* port */ 244 GC_u2h(TEST_MESSAGE_TYPE), /* port */
246 NULL, /* window change */ 245 NULL, /* window change */
247 &disconnected, /* disconnect handler */ 246 &disconnected, /* disconnect handler */
248 handlers /* traffic handlers */ 247 handlers /* traffic handlers */
249 ); 248 );
250 env = GNUNET_MQ_msg (msg, TEST_MESSAGE_TYPE); 249 env = GNUNET_MQ_msg(msg, TEST_MESSAGE_TYPE);
251 msg->payload = GNUNET_htonll (42); 250 msg->payload = GNUNET_htonll(42);
252 mq = GNUNET_CADET_get_mq (ch); 251 mq = GNUNET_CADET_get_mq(ch);
253 GNUNET_MQ_notify_sent (env, &message_sent, NULL); 252 GNUNET_MQ_notify_sent(env, &message_sent, NULL);
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 253 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
255 "sending message\n"); 254 "sending message\n");
256 GNUNET_MQ_send (mq, env); 255 GNUNET_MQ_send(mq, env);
257} 256}
258 257
259 258
@@ -265,53 +264,53 @@ do_connect (void *cls)
265 * @param peer Testing peer handle. 264 * @param peer Testing peer handle.
266 */ 265 */
267static void 266static void
268run (void *cls, 267run(void *cls,
269 const struct GNUNET_CONFIGURATION_Handle *cfg, 268 const struct GNUNET_CONFIGURATION_Handle *cfg,
270 struct GNUNET_TESTING_Peer *peer) 269 struct GNUNET_TESTING_Peer *peer)
271{ 270{
272 struct GNUNET_MQ_MessageHandler handlers[] = { 271 struct GNUNET_MQ_MessageHandler handlers[] = {
273 GNUNET_MQ_hd_fixed_size (data_received, 272 GNUNET_MQ_hd_fixed_size(data_received,
274 TEST_MESSAGE_TYPE, 273 TEST_MESSAGE_TYPE,
275 struct GNUNET_CADET_TestMsg, 274 struct GNUNET_CADET_TestMsg,
276 cadet_peer_2), 275 cadet_peer_2),
277 GNUNET_MQ_handler_end () 276 GNUNET_MQ_handler_end()
278 }; 277 };
279 struct GNUNET_TIME_Relative delay; 278 struct GNUNET_TIME_Relative delay;
280 279
281 me = peer; 280 me = peer;
282 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, 281 GNUNET_SCHEDULER_add_shutdown(&do_shutdown,
283 NULL); 282 NULL);
284 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15); 283 delay = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 15);
285 abort_task = GNUNET_SCHEDULER_add_delayed (delay, 284 abort_task = GNUNET_SCHEDULER_add_delayed(delay,
286 &do_abort, 285 &do_abort,
287 (void *) (long) __LINE__); 286 (void *)(long)__LINE__);
288 cadet_peer_1 = GNUNET_CADET_connect (cfg); 287 cadet_peer_1 = GNUNET_CADET_connect(cfg);
289 cadet_peer_2 = GNUNET_CADET_connect (cfg); 288 cadet_peer_2 = GNUNET_CADET_connect(cfg);
290 289
291 if ( (NULL == cadet_peer_1) || 290 if ((NULL == cadet_peer_1) ||
292 (NULL == cadet_peer_2) ) 291 (NULL == cadet_peer_2))
293 { 292 {
294 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 293 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
295 "Couldn't connect to cadet\n"); 294 "Couldn't connect to cadet\n");
296 result = GNUNET_SYSERR; 295 result = GNUNET_SYSERR;
297 GNUNET_SCHEDULER_shutdown (); 296 GNUNET_SCHEDULER_shutdown();
298 return; 297 return;
299 } 298 }
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CADET 1: %p\n", cadet_peer_1); 299 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "CADET 1: %p\n", cadet_peer_1);
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CADET 2: %p\n", cadet_peer_2); 300 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "CADET 2: %p\n", cadet_peer_2);
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "handlers 2: %p\n", handlers); 301 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "handlers 2: %p\n", handlers);
303 GNUNET_CADET_open_port (cadet_peer_2, /* cadet handle */ 302 GNUNET_CADET_open_port(cadet_peer_2, /* cadet handle */
304 GC_u2h (TEST_PORT_ID), /* port id */ 303 GC_u2h(TEST_PORT_ID), /* port id */
305 &connected, /* connect handler */ 304 &connected, /* connect handler */
306 (void *) 2L, /* handle for #connected */ 305 (void *)2L, /* handle for #connected */
307 NULL, /* window size handler */ 306 NULL, /* window size handler */
308 &disconnected, /* disconnect handler */ 307 &disconnected, /* disconnect handler */
309 handlers); /* traffic handlers */ 308 handlers); /* traffic handlers */
310 delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2); 309 delay = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2);
311 if (NULL == connect_task) 310 if (NULL == connect_task)
312 connect_task = GNUNET_SCHEDULER_add_delayed (delay, 311 connect_task = GNUNET_SCHEDULER_add_delayed(delay,
313 &do_connect, 312 &do_connect,
314 NULL); 313 NULL);
315} 314}
316 315
317 316
@@ -319,16 +318,16 @@ run (void *cls,
319 * Main 318 * Main
320 */ 319 */
321int 320int
322main (int argc, char *argv[]) 321main(int argc, char *argv[])
323{ 322{
324 if (0 != GNUNET_TESTING_peer_run ("test-cadet-local", 323 if (0 != GNUNET_TESTING_peer_run("test-cadet-local",
325 "test_cadet.conf", 324 "test_cadet.conf",
326 &run, NULL)) 325 &run, NULL))
327 { 326 {
328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run failed\n"); 327 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "run failed\n");
329 return 2; 328 return 2;
330 } 329 }
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Final result: %d\n", result); 330 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Final result: %d\n", result);
332 return (result == GNUNET_OK) ? 0 : 1; 331 return (result == GNUNET_OK) ? 0 : 1;
333} 332}
334 333