aboutsummaryrefslogtreecommitdiff
path: root/src/cadet
diff options
context:
space:
mode:
Diffstat (limited to 'src/cadet')
-rw-r--r--src/cadet/Makefile.am37
-rw-r--r--src/cadet/cadet.h92
-rw-r--r--src/cadet/cadet_api.c420
-rw-r--r--src/cadet/cadet_common.c61
-rw-r--r--src/cadet/cadet_protocol.h253
-rw-r--r--src/cadet/gnunet-cadet.c13
-rw-r--r--src/cadet/gnunet-service-cadet-new.c1350
-rw-r--r--src/cadet/gnunet-service-cadet-new.h264
-rw-r--r--src/cadet/gnunet-service-cadet-new_channel.c1077
-rw-r--r--src/cadet/gnunet-service-cadet-new_channel.h191
-rw-r--r--src/cadet/gnunet-service-cadet-new_connection.c665
-rw-r--r--src/cadet/gnunet-service-cadet-new_connection.h196
-rw-r--r--src/cadet/gnunet-service-cadet-new_core.c903
-rw-r--r--src/cadet/gnunet-service-cadet-new_core.h69
-rw-r--r--src/cadet/gnunet-service-cadet-new_dht.c315
-rw-r--r--src/cadet/gnunet-service-cadet-new_dht.h93
-rw-r--r--src/cadet/gnunet-service-cadet-new_hello.c142
-rw-r--r--src/cadet/gnunet-service-cadet-new_hello.h80
-rw-r--r--src/cadet/gnunet-service-cadet-new_paths.c557
-rw-r--r--src/cadet/gnunet-service-cadet-new_paths.h182
-rw-r--r--src/cadet/gnunet-service-cadet-new_peer.c1056
-rw-r--r--src/cadet/gnunet-service-cadet-new_peer.h368
-rw-r--r--src/cadet/gnunet-service-cadet-new_tunnels.c2249
-rw-r--r--src/cadet/gnunet-service-cadet-new_tunnels.h357
-rw-r--r--src/cadet/gnunet-service-cadet_channel.c237
-rw-r--r--src/cadet/gnunet-service-cadet_channel.h16
-rw-r--r--src/cadet/gnunet-service-cadet_connection.c518
-rw-r--r--src/cadet/gnunet-service-cadet_connection.h37
-rw-r--r--src/cadet/gnunet-service-cadet_dht.c90
-rw-r--r--src/cadet/gnunet-service-cadet_hello.h5
-rw-r--r--src/cadet/gnunet-service-cadet_local.c207
-rw-r--r--src/cadet/gnunet-service-cadet_local.h32
-rw-r--r--src/cadet/gnunet-service-cadet_peer.c194
-rw-r--r--src/cadet/gnunet-service-cadet_peer.h15
-rw-r--r--src/cadet/gnunet-service-cadet_tunnel.c255
-rw-r--r--src/cadet/gnunet-service-cadet_tunnel.h8
-rw-r--r--src/cadet/test_cadet.conf5
-rw-r--r--src/cadet/test_cadet_local.c5
38 files changed, 11566 insertions, 1048 deletions
diff --git a/src/cadet/Makefile.am b/src/cadet/Makefile.am
index d823b4872..53d17dd9c 100644
--- a/src/cadet/Makefile.am
+++ b/src/cadet/Makefile.am
@@ -22,7 +22,9 @@ plugindir = $(libdir)/gnunet
22AM_CLFAGS = -g 22AM_CLFAGS = -g
23 23
24libexec_PROGRAMS = \ 24libexec_PROGRAMS = \
25 gnunet-service-cadet $(EXP_LIBEXEC) 25 gnunet-service-cadet \
26 gnunet-service-cadet-new \
27 $(EXP_LIBEXEC)
26 28
27bin_PROGRAMS = \ 29bin_PROGRAMS = \
28 gnunet-cadet 30 gnunet-cadet
@@ -46,6 +48,27 @@ gnunet_cadet_LDADD = \
46 libgnunetcadet.la \ 48 libgnunetcadet.la \
47 $(top_builddir)/src/util/libgnunetutil.la 49 $(top_builddir)/src/util/libgnunetutil.la
48 50
51gnunet_service_cadet_new_SOURCES = \
52 gnunet-service-cadet-new.c gnunet-service-cadet-new.h \
53 gnunet-service-cadet-new_channel.c gnunet-service-cadet-new_channel.h \
54 gnunet-service-cadet-new_connection.c gnunet-service-cadet-new_connection.h \
55 gnunet-service-cadet-new_core.c gnunet-service-cadet-new_core.h \
56 gnunet-service-cadet-new_dht.c gnunet-service-cadet-new_dht.h \
57 gnunet-service-cadet-new_hello.c gnunet-service-cadet-new_hello.h \
58 gnunet-service-cadet-new_tunnels.c gnunet-service-cadet-new_tunnels.h \
59 gnunet-service-cadet-new_paths.c gnunet-service-cadet-new_paths.h \
60 gnunet-service-cadet-new_peer.c gnunet-service-cadet-new_peer.h
61gnunet_service_cadet_new_LDADD = \
62 $(top_builddir)/src/util/libgnunetutil.la \
63 $(top_builddir)/src/ats/libgnunetats.la \
64 $(top_builddir)/src/core/libgnunetcore.la \
65 $(top_builddir)/src/dht/libgnunetdht.la \
66 $(top_builddir)/src/statistics/libgnunetstatistics.la \
67 $(top_builddir)/src/transport/libgnunettransport.la \
68 $(top_builddir)/src/peerinfo/libgnunetpeerinfo.la \
69 $(top_builddir)/src/hello/libgnunethello.la \
70 $(top_builddir)/src/block/libgnunetblock.la
71
49gnunet_service_cadet_SOURCES = \ 72gnunet_service_cadet_SOURCES = \
50 gnunet-service-cadet_tunnel.c gnunet-service-cadet_tunnel.h \ 73 gnunet-service-cadet_tunnel.c gnunet-service-cadet_tunnel.h \
51 gnunet-service-cadet_connection.c gnunet-service-cadet_connection.h \ 74 gnunet-service-cadet_connection.c gnunet-service-cadet_connection.h \
@@ -87,6 +110,11 @@ libgnunetcadettest_a_LIBADD = \
87 110
88if HAVE_TESTING 111if HAVE_TESTING
89check_PROGRAMS = \ 112check_PROGRAMS = \
113 test_cadet_2_speed_reliable_backwards \
114 test_cadet_5_speed \
115 test_cadet_5_speed_ack \
116 test_cadet_5_speed_reliable \
117 test_cadet_5_speed_reliable_backwards \
90 test_cadet_single \ 118 test_cadet_single \
91 test_cadet_local \ 119 test_cadet_local \
92 test_cadet_2_forward \ 120 test_cadet_2_forward \
@@ -96,15 +124,10 @@ check_PROGRAMS = \
96 test_cadet_2_speed_ack \ 124 test_cadet_2_speed_ack \
97 test_cadet_2_speed_backwards \ 125 test_cadet_2_speed_backwards \
98 test_cadet_2_speed_reliable \ 126 test_cadet_2_speed_reliable \
99 test_cadet_2_speed_reliable_backwards \
100 test_cadet_5_forward \ 127 test_cadet_5_forward \
101 test_cadet_5_signal \ 128 test_cadet_5_signal \
102 test_cadet_5_keepalive \ 129 test_cadet_5_keepalive \
103 test_cadet_5_speed \ 130 test_cadet_5_speed_backwards
104 test_cadet_5_speed_ack \
105 test_cadet_5_speed_backwards \
106 test_cadet_5_speed_reliable \
107 test_cadet_5_speed_reliable_backwards
108endif 131endif
109 132
110ld_cadet_test_lib = \ 133ld_cadet_test_lib = \
diff --git a/src/cadet/cadet.h b/src/cadet/cadet.h
index 049f3a85a..c16fb2917 100644
--- a/src/cadet/cadet.h
+++ b/src/cadet/cadet.h
@@ -57,6 +57,7 @@ extern "C"
57#include "gnunet_util_lib.h" 57#include "gnunet_util_lib.h"
58#include "gnunet_peer_lib.h" 58#include "gnunet_peer_lib.h"
59#include "gnunet_core_service.h" 59#include "gnunet_core_service.h"
60#include "gnunet_cadet_service.h"
60#include "gnunet_protocols.h" 61#include "gnunet_protocols.h"
61#include <gnunet_cadet_service.h> 62#include <gnunet_cadet_service.h>
62 63
@@ -64,13 +65,29 @@ extern "C"
64/************************** CONSTANTS ******************************/ 65/************************** CONSTANTS ******************************/
65/******************************************************************************/ 66/******************************************************************************/
66 67
68/**
69 * Minimum value for channel IDs of local clients.
70 */
67#define GNUNET_CADET_LOCAL_CHANNEL_ID_CLI 0x80000000 71#define GNUNET_CADET_LOCAL_CHANNEL_ID_CLI 0x80000000
68#define GNUNET_CADET_LOCAL_CHANNEL_ID_SERV 0xB0000000
69 72
70#define HIGH_PID 0xFFFF0000 73/**
71#define LOW_PID 0x0000FFFF 74 * FIXME.
75 */
76#define HIGH_PID 0xFF000000
77
78/**
79 * FIXME.
80 */
81#define LOW_PID 0x00FFFFFF
72 82
73#define PID_OVERFLOW(pid, max) (pid > HIGH_PID && max < LOW_PID) 83
84/**
85 * Test if the two PIDs (of type `uint32_t`) are in the range where we
86 * have to worry about overflows. This is the case when @a pid is
87 * large and @a max is small, useful when comparing @a pid smaller
88 * than @a max.
89 */
90#define PID_OVERFLOW(pid, max) (((pid) > HIGH_PID) && ((max) < LOW_PID))
74 91
75/******************************************************************************/ 92/******************************************************************************/
76/************************** MESSAGES ******************************/ 93/************************** MESSAGES ******************************/
@@ -80,6 +97,22 @@ GNUNET_NETWORK_STRUCT_BEGIN
80 97
81 98
82/** 99/**
100 * Number uniquely identifying a channel of a client.
101 */
102struct GNUNET_CADET_ClientChannelNumber
103{
104 /**
105 * Values for channel numbering.
106 * Local channel numbers given by the service (incoming) are
107 * smaller than #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI.
108 * Local channel numbers given by the client (created) are
109 * larger than #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI.
110 */
111 uint32_t channel_of_client GNUNET_PACKED;
112};
113
114
115/**
83 * Message for a client to create and destroy channels. 116 * Message for a client to create and destroy channels.
84 */ 117 */
85struct GNUNET_CADET_PortMessage 118struct GNUNET_CADET_PortMessage
@@ -98,31 +131,23 @@ struct GNUNET_CADET_PortMessage
98 struct GNUNET_HashCode port GNUNET_PACKED; 131 struct GNUNET_HashCode port GNUNET_PACKED;
99}; 132};
100 133
101/**
102 * Type for channel numbering.
103 * - Local channel numbers given by the service (incoming) are >= 0xB0000000
104 * - Local channel numbers given by the client (created) are >= 0x80000000
105 * - Global channel numbers are < 0x80000000
106 */
107typedef uint32_t CADET_ChannelNumber;
108
109 134
110/** 135/**
111 * Message for a client to create channels. 136 * Message for a client to create channels.
112 */ 137 */
113struct GNUNET_CADET_ChannelCreateMessage 138struct GNUNET_CADET_ChannelOpenMessageMessage
114{ 139{
115 /** 140 /**
116 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_TUNNEL_CREATE 141 * Type: #GNUNET_MESSAGE_TYPE_CADET_LOCAL_TUNNEL_CREATE
117 * 142 *
118 * Size: sizeof(struct GNUNET_CADET_ChannelCreateMessage) 143 * Size: sizeof(struct GNUNET_CADET_ChannelOpenMessageMessage)
119 */ 144 */
120 struct GNUNET_MessageHeader header; 145 struct GNUNET_MessageHeader header;
121 146
122 /** 147 /**
123 * ID of a channel controlled by this client. 148 * ID of a channel controlled by this client.
124 */ 149 */
125 CADET_ChannelNumber channel_id GNUNET_PACKED; 150 struct GNUNET_CADET_ClientChannelNumber channel_id;
126 151
127 /** 152 /**
128 * Channel's peer 153 * Channel's peer
@@ -152,11 +177,11 @@ struct GNUNET_CADET_ChannelDestroyMessage
152 * Size: sizeof(struct GNUNET_CADET_ChannelDestroyMessage) 177 * Size: sizeof(struct GNUNET_CADET_ChannelDestroyMessage)
153 */ 178 */
154 struct GNUNET_MessageHeader header; 179 struct GNUNET_MessageHeader header;
155 180
156 /** 181 /**
157 * ID of a channel controlled by this client. 182 * ID of a channel controlled by this client.
158 */ 183 */
159 CADET_ChannelNumber channel_id GNUNET_PACKED; 184 struct GNUNET_CADET_ClientChannelNumber channel_id;
160}; 185};
161 186
162 187
@@ -173,7 +198,7 @@ struct GNUNET_CADET_LocalData
173 /** 198 /**
174 * ID of the channel 199 * ID of the channel
175 */ 200 */
176 uint32_t id GNUNET_PACKED; 201 struct GNUNET_CADET_ClientChannelNumber id;
177 202
178 /** 203 /**
179 * Payload follows 204 * Payload follows
@@ -195,7 +220,7 @@ struct GNUNET_CADET_LocalAck
195 /** 220 /**
196 * ID of the channel allowed to send more data. 221 * ID of the channel allowed to send more data.
197 */ 222 */
198 CADET_ChannelNumber channel_id GNUNET_PACKED; 223 struct GNUNET_CADET_ClientChannelNumber channel_id;
199 224
200}; 225};
201 226
@@ -214,7 +239,7 @@ struct GNUNET_CADET_LocalInfo
214 /** 239 /**
215 * ID of the channel allowed to send more data. 240 * ID of the channel allowed to send more data.
216 */ 241 */
217 CADET_ChannelNumber channel_id GNUNET_PACKED; 242 struct GNUNET_CADET_ClientChannelNumber channel_id;
218 243
219 /** 244 /**
220 * ID of the owner of the channel (can be local peer). 245 * ID of the owner of the channel (can be local peer).
@@ -258,6 +283,7 @@ struct GNUNET_CADET_LocalInfoPeer
258 * (each path ends in destination) */ 283 * (each path ends in destination) */
259}; 284};
260 285
286
261/** 287/**
262 * Message to inform the client about one of the tunnels in the service. 288 * Message to inform the client about one of the tunnels in the service.
263 */ 289 */
@@ -294,7 +320,7 @@ struct GNUNET_CADET_LocalInfoTunnel
294 */ 320 */
295 uint16_t cstate GNUNET_PACKED; 321 uint16_t cstate GNUNET_PACKED;
296 322
297 /* If TUNNEL (no 'S'): GNUNET_PeerIdentity connection_ids[connections] */ 323 /* If TUNNEL (no 'S'): struct GNUNET_CADET_ConnectionTunnelIdentifier connection_ids[connections] */
298 /* If TUNNEL (no 'S'): uint32_t channel_ids[channels] */ 324 /* If TUNNEL (no 'S'): uint32_t channel_ids[channels] */
299}; 325};
300 326
@@ -350,25 +376,6 @@ GC_min_pid (uint32_t a, uint32_t b);
350 376
351 377
352/** 378/**
353 * Convert a 256 bit CadetHash into a 512 HashCode to use in GNUNET_h2s,
354 * multihashmap, and other HashCode-based functions.
355 *
356 * @param id A 256 bit hash to expand.
357 *
358 * @return A HashCode containing the original 256 bit hash right-padded with 0.
359 */
360const struct GNUNET_HashCode *
361GC_h2hc (const struct GNUNET_CADET_Hash *id);
362
363/**
364 * Get a string from a Cadet Hash (256 bits).
365 * WARNING: Not reentrant (based on GNUNET_h2s).
366 */
367const char *
368GC_h2s (const struct GNUNET_CADET_Hash *id);
369
370
371/**
372 * Allocate a string with a hexdump of any binary data. 379 * Allocate a string with a hexdump of any binary data.
373 * 380 *
374 * @param bin Arbitrary binary data. 381 * @param bin Arbitrary binary data.
@@ -378,7 +385,10 @@ GC_h2s (const struct GNUNET_CADET_Hash *id);
378 * @return The size of the output. 385 * @return The size of the output.
379 */ 386 */
380size_t 387size_t
381GC_bin2s (void *bin, unsigned int len, char **output); 388GC_bin2s (void *bin,
389 unsigned int len,
390 char **output);
391
382 392
383/** 393/**
384 * Convert a message type into a string to help debug 394 * Convert a message type into a string to help debug
diff --git a/src/cadet/cadet_api.c b/src/cadet/cadet_api.c
index d75815588..8f1274d63 100644
--- a/src/cadet/cadet_api.c
+++ b/src/cadet/cadet_api.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V. 3 Copyright (C) 2011, 2017 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -19,7 +19,7 @@
19*/ 19*/
20/** 20/**
21 * @file cadet/cadet_api.c 21 * @file cadet/cadet_api.c
22 * @brief cadet api: client implementation of new cadet service 22 * @brief cadet api: client implementation of cadet service
23 * @author Bartlomiej Polot 23 * @author Bartlomiej Polot
24 */ 24 */
25 25
@@ -41,46 +41,48 @@
41 */ 41 */
42struct GNUNET_CADET_TransmitHandle 42struct GNUNET_CADET_TransmitHandle
43{ 43{
44 /** 44 /**
45 * Double Linked list 45 * Double Linked list
46 */ 46 */
47 struct GNUNET_CADET_TransmitHandle *next; 47 struct GNUNET_CADET_TransmitHandle *next;
48 48
49 /** 49 /**
50 * Double Linked list 50 * Double Linked list
51 */ 51 */
52 struct GNUNET_CADET_TransmitHandle *prev; 52 struct GNUNET_CADET_TransmitHandle *prev;
53 53
54 /** 54 /**
55 * Channel this message is sent on / for (may be NULL for control messages). 55 * Channel this message is sent on / for (may be NULL for control messages).
56 */ 56 */
57 struct GNUNET_CADET_Channel *channel; 57 struct GNUNET_CADET_Channel *channel;
58 58
59 /** 59 /**
60 * Request data task. 60 * Request data task.
61 */ 61 */
62 struct GNUNET_SCHEDULER_Task *request_data_task; 62 struct GNUNET_SCHEDULER_Task *request_data_task;
63 63
64 /** 64 /**
65 * Callback to obtain the message to transmit, or NULL if we 65 * Callback to obtain the message to transmit, or NULL if we
66 * got the message in 'data'. Notice that messages built 66 * got the message in 'data'. Notice that messages built
67 * by 'notify' need to be encapsulated with information about 67 * by 'notify' need to be encapsulated with information about
68 * the 'target'. 68 * the 'target'.
69 */ 69 */
70 GNUNET_CONNECTION_TransmitReadyNotify notify; 70 GNUNET_CONNECTION_TransmitReadyNotify notify;
71 71
72 /** 72 /**
73 * Closure for 'notify' 73 * Closure for 'notify'
74 */ 74 */
75 void *notify_cls; 75 void *notify_cls;
76 76
77 /** 77 /**
78 * Size of the payload. 78 * Size of the payload.
79 */ 79 */
80 size_t size; 80 size_t size;
81}; 81};
82 82
83union CadetInfoCB { 83
84union CadetInfoCB
85{
84 86
85 /** 87 /**
86 * Channel callback. 88 * Channel callback.
@@ -114,14 +116,14 @@ union CadetInfoCB {
114 */ 116 */
115struct GNUNET_CADET_Handle 117struct GNUNET_CADET_Handle
116{ 118{
117 /** 119 /**
118 * Message queue (if available). 120 * Message queue (if available).
119 */ 121 */
120 struct GNUNET_MQ_Handle *mq; 122 struct GNUNET_MQ_Handle *mq;
121 123
122 /** 124 /**
123 * Set of handlers used for processing incoming messages in the channels 125 * Set of handlers used for processing incoming messages in the channels
124 */ 126 */
125 const struct GNUNET_CADET_MessageHandler *message_handlers; 127 const struct GNUNET_CADET_MessageHandler *message_handlers;
126 128
127 /** 129 /**
@@ -134,40 +136,40 @@ struct GNUNET_CADET_Handle
134 */ 136 */
135 struct GNUNET_CONTAINER_MultiHashMap *ports; 137 struct GNUNET_CONTAINER_MultiHashMap *ports;
136 138
137 /** 139 /**
138 * Double linked list of the channels this client is connected to, head. 140 * Double linked list of the channels this client is connected to, head.
139 */ 141 */
140 struct GNUNET_CADET_Channel *channels_head; 142 struct GNUNET_CADET_Channel *channels_head;
141 143
142 /** 144 /**
143 * Double linked list of the channels this client is connected to, tail. 145 * Double linked list of the channels this client is connected to, tail.
144 */ 146 */
145 struct GNUNET_CADET_Channel *channels_tail; 147 struct GNUNET_CADET_Channel *channels_tail;
146 148
147 /** 149 /**
148 * Callback for inbound channel disconnection 150 * Callback for inbound channel disconnection
149 */ 151 */
150 GNUNET_CADET_ChannelEndHandler *cleaner; 152 GNUNET_CADET_ChannelEndHandler *cleaner;
151 153
152 /** 154 /**
153 * Closure for all the handlers given by the client 155 * Closure for all the handlers given by the client
154 */ 156 */
155 void *cls; 157 void *cls;
156 158
157 /** 159 /**
158 * Messages to send to the service, head. 160 * Messages to send to the service, head.
159 */ 161 */
160 struct GNUNET_CADET_TransmitHandle *th_head; 162 struct GNUNET_CADET_TransmitHandle *th_head;
161 163
162 /** 164 /**
163 * Messages to send to the service, tail. 165 * Messages to send to the service, tail.
164 */ 166 */
165 struct GNUNET_CADET_TransmitHandle *th_tail; 167 struct GNUNET_CADET_TransmitHandle *th_tail;
166 168
167 /** 169 /**
168 * chid of the next channel to create (to avoid reusing IDs often) 170 * child of the next channel to create (to avoid reusing IDs often)
169 */ 171 */
170 CADET_ChannelNumber next_chid; 172 struct GNUNET_CADET_ClientChannelNumber next_chid;
171 173
172 /** 174 /**
173 * Configuration given by the client, in case of reconnection 175 * Configuration given by the client, in case of reconnection
@@ -201,9 +203,9 @@ struct GNUNET_CADET_Handle
201 */ 203 */
202struct GNUNET_CADET_Peer 204struct GNUNET_CADET_Peer
203{ 205{
204 /** 206 /**
205 * ID of the peer in short form 207 * ID of the peer in short form
206 */ 208 */
207 GNUNET_PEER_Id id; 209 GNUNET_PEER_Id id;
208 210
209 /** 211 /**
@@ -218,34 +220,34 @@ struct GNUNET_CADET_Peer
218 */ 220 */
219struct GNUNET_CADET_Channel 221struct GNUNET_CADET_Channel
220{ 222{
221 /** 223 /**
222 * DLL next 224 * DLL next
223 */ 225 */
224 struct GNUNET_CADET_Channel *next; 226 struct GNUNET_CADET_Channel *next;
225 227
226 /** 228 /**
227 * DLL prev 229 * DLL prev
228 */ 230 */
229 struct GNUNET_CADET_Channel *prev; 231 struct GNUNET_CADET_Channel *prev;
230 232
231 /** 233 /**
232 * Handle to the cadet this channel belongs to 234 * Handle to the cadet this channel belongs to
233 */ 235 */
234 struct GNUNET_CADET_Handle *cadet; 236 struct GNUNET_CADET_Handle *cadet;
235 237
236 /** 238 /**
237 * Local ID of the channel 239 * Local ID of the channel
238 */ 240 */
239 CADET_ChannelNumber chid; 241 struct GNUNET_CADET_ClientChannelNumber chid;
240 242
241 /** 243 /**
242 * Channel's port, if any. 244 * Channel's port, if any.
243 */ 245 */
244 struct GNUNET_CADET_Port *port; 246 struct GNUNET_CADET_Port *port;
245 247
246 /** 248 /**
247 * Other end of the channel. 249 * Other end of the channel.
248 */ 250 */
249 GNUNET_PEER_Id peer; 251 GNUNET_PEER_Id peer;
250 252
251 /** 253 /**
@@ -253,46 +255,47 @@ struct GNUNET_CADET_Channel
253 */ 255 */
254 void *ctx; 256 void *ctx;
255 257
256 /** 258 /**
257 * Size of packet queued in this channel 259 * Size of packet queued in this channel
258 */ 260 */
259 unsigned int packet_size; 261 unsigned int packet_size;
260 262
261 /** 263 /**
262 * Channel options: reliability, etc. 264 * Channel options: reliability, etc.
263 */ 265 */
264 enum GNUNET_CADET_ChannelOption options; 266 enum GNUNET_CADET_ChannelOption options;
265 267
266 /** 268 /**
267 * Are we allowed to send to the service? 269 * Are we allowed to send to the service?
268 */ 270 */
269 int allow_send; 271 int allow_send;
270 272
271}; 273};
272 274
275
273/** 276/**
274 * Opaque handle to a port. 277 * Opaque handle to a port.
275 */ 278 */
276struct GNUNET_CADET_Port 279struct GNUNET_CADET_Port
277{ 280{
278 /** 281 /**
279 * Handle to the CADET session this port belongs to. 282 * Handle to the CADET session this port belongs to.
280 */ 283 */
281 struct GNUNET_CADET_Handle *cadet; 284 struct GNUNET_CADET_Handle *cadet;
282 285
283 /** 286 /**
284 * Port ID. 287 * Port ID.
285 */ 288 */
286 struct GNUNET_HashCode *hash; 289 struct GNUNET_HashCode *hash;
287 290
288 /** 291 /**
289 * Callback handler for incoming channels on this port. 292 * Callback handler for incoming channels on this port.
290 */ 293 */
291 GNUNET_CADET_InboundChannelNotificationHandler *handler; 294 GNUNET_CADET_InboundChannelNotificationHandler *handler;
292 295
293 /** 296 /**
294 * Closure for @a handler. 297 * Closure for @a handler.
295 */ 298 */
296 void *cls; 299 void *cls;
297}; 300};
298 301
@@ -356,22 +359,20 @@ find_port (const struct GNUNET_CADET_Handle *h,
356 359
357/** 360/**
358 * Get the channel handler for the channel specified by id from the given handle 361 * Get the channel handler for the channel specified by id from the given handle
362 *
359 * @param h Cadet handle 363 * @param h Cadet handle
360 * @param chid ID of the wanted channel 364 * @param chid ID of the wanted channel
361 * @return handle to the required channel or NULL if not found 365 * @return handle to the required channel or NULL if not found
362 */ 366 */
363static struct GNUNET_CADET_Channel * 367static struct GNUNET_CADET_Channel *
364retrieve_channel (struct GNUNET_CADET_Handle *h, CADET_ChannelNumber chid) 368retrieve_channel (struct GNUNET_CADET_Handle *h,
369 struct GNUNET_CADET_ClientChannelNumber chid)
365{ 370{
366 struct GNUNET_CADET_Channel *ch; 371 struct GNUNET_CADET_Channel *ch;
367 372
368 ch = h->channels_head; 373 for (ch = h->channels_head; NULL != ch; ch = ch->next)
369 while (ch != NULL) 374 if (ch->chid.channel_of_client == chid.channel_of_client)
370 {
371 if (ch->chid == chid)
372 return ch; 375 return ch;
373 ch = ch->next;
374 }
375 return NULL; 376 return NULL;
376} 377}
377 378
@@ -385,21 +386,27 @@ retrieve_channel (struct GNUNET_CADET_Handle *h, CADET_ChannelNumber chid)
385 * @return Handle to the created channel. 386 * @return Handle to the created channel.
386 */ 387 */
387static struct GNUNET_CADET_Channel * 388static struct GNUNET_CADET_Channel *
388create_channel (struct GNUNET_CADET_Handle *h, CADET_ChannelNumber chid) 389create_channel (struct GNUNET_CADET_Handle *h,
390 struct GNUNET_CADET_ClientChannelNumber chid)
389{ 391{
390 struct GNUNET_CADET_Channel *ch; 392 struct GNUNET_CADET_Channel *ch;
391 393
392 ch = GNUNET_new (struct GNUNET_CADET_Channel); 394 ch = GNUNET_new (struct GNUNET_CADET_Channel);
393 GNUNET_CONTAINER_DLL_insert (h->channels_head, h->channels_tail, ch); 395 GNUNET_CONTAINER_DLL_insert (h->channels_head,
396 h->channels_tail,
397 ch);
394 ch->cadet = h; 398 ch->cadet = h;
395 if (0 == chid) 399 if (0 == chid.channel_of_client)
396 { 400 {
397 ch->chid = h->next_chid; 401 ch->chid = h->next_chid;
398 while (NULL != retrieve_channel (h, h->next_chid)) 402 while (NULL != retrieve_channel (h,
403 h->next_chid))
399 { 404 {
400 h->next_chid++; 405 h->next_chid.channel_of_client
401 h->next_chid &= ~GNUNET_CADET_LOCAL_CHANNEL_ID_SERV; 406 = htonl (1 + ntohl (h->next_chid.channel_of_client));
402 h->next_chid |= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI; 407 if (0 == ntohl (h->next_chid.channel_of_client))
408 h->next_chid.channel_of_client
409 = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
403 } 410 }
404 } 411 }
405 else 412 else
@@ -440,7 +447,9 @@ destroy_channel (struct GNUNET_CADET_Channel *ch, int call_cleaner)
440 } 447 }
441 h = ch->cadet; 448 h = ch->cadet;
442 449
443 GNUNET_CONTAINER_DLL_remove (h->channels_head, h->channels_tail, ch); 450 GNUNET_CONTAINER_DLL_remove (h->channels_head,
451 h->channels_tail,
452 ch);
444 453
445 /* signal channel destruction */ 454 /* signal channel destruction */
446 if ( (NULL != h->cleaner) && (0 != ch->peer) && (GNUNET_YES == call_cleaner) ) 455 if ( (NULL != h->cleaner) && (0 != ch->peer) && (GNUNET_YES == call_cleaner) )
@@ -466,7 +475,7 @@ destroy_channel (struct GNUNET_CADET_Channel *ch, int call_cleaner)
466 if (0 != ch->peer) 475 if (0 != ch->peer)
467 GNUNET_PEER_change_rc (ch->peer, -1); 476 GNUNET_PEER_change_rc (ch->peer, -1);
468 GNUNET_free (ch); 477 GNUNET_free (ch);
469 return; 478
470} 479}
471 480
472 481
@@ -516,13 +525,15 @@ send_ack (struct GNUNET_CADET_Channel *ch)
516 struct GNUNET_CADET_LocalAck *msg; 525 struct GNUNET_CADET_LocalAck *msg;
517 struct GNUNET_MQ_Envelope *env; 526 struct GNUNET_MQ_Envelope *env;
518 527
519 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK); 528 env = GNUNET_MQ_msg (msg,
520 529 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
521 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on channel %X\n", ch->chid);
522 msg->channel_id = htonl (ch->chid);
523 GNUNET_MQ_send (ch->cadet->mq, env);
524 530
525 return; 531 LOG (GNUNET_ERROR_TYPE_DEBUG,
532 "Sending ACK on channel %X\n",
533 ch->chid.channel_of_client);
534 msg->channel_id = ch->chid;
535 GNUNET_MQ_send (ch->cadet->mq,
536 env);
526} 537}
527 538
528 539
@@ -555,13 +566,16 @@ request_data (void *cls)
555 th->channel->packet_size = 0; 566 th->channel->packet_size = 0;
556 remove_from_queue (th); 567 remove_from_queue (th);
557 568
558 env = GNUNET_MQ_msg_extra (msg, th->size, 569 env = GNUNET_MQ_msg_extra (msg,
570 th->size,
559 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA); 571 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
560 msg->id = htonl (th->channel->chid); 572 msg->id = th->channel->chid;
561 osize = th->notify (th->notify_cls, th->size, &msg[1]); 573 osize = th->notify (th->notify_cls,
574 th->size,
575 &msg[1]);
562 GNUNET_assert (osize == th->size); 576 GNUNET_assert (osize == th->size);
563 GNUNET_MQ_send (th->channel->cadet->mq, env); 577 GNUNET_MQ_send (th->channel->cadet->mq,
564 578 env);
565 GNUNET_free (th); 579 GNUNET_free (th);
566} 580}
567 581
@@ -574,19 +588,21 @@ request_data (void *cls)
574 */ 588 */
575static void 589static void
576handle_channel_created (void *cls, 590handle_channel_created (void *cls,
577 const struct GNUNET_CADET_ChannelCreateMessage *msg) 591 const struct GNUNET_CADET_ChannelOpenMessageMessage *msg)
578{ 592{
579 struct GNUNET_CADET_Handle *h = cls; 593 struct GNUNET_CADET_Handle *h = cls;
580 struct GNUNET_CADET_Channel *ch; 594 struct GNUNET_CADET_Channel *ch;
581 struct GNUNET_CADET_Port *port; 595 struct GNUNET_CADET_Port *port;
582 const struct GNUNET_HashCode *port_number; 596 const struct GNUNET_HashCode *port_number;
583 CADET_ChannelNumber chid; 597 struct GNUNET_CADET_ClientChannelNumber chid;
584 598
585 chid = ntohl (msg->channel_id); 599 chid = msg->channel_id;
586 port_number = &msg->port; 600 port_number = &msg->port;
587 LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming channel %X [%s]\n", 601 LOG (GNUNET_ERROR_TYPE_DEBUG,
588 chid, GNUNET_h2s (port_number)); 602 "Creating incoming channel %X [%s]\n",
589 if (chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV) 603 ntohl (chid.channel_of_client),
604 GNUNET_h2s (port_number));
605 if (ntohl (chid.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
590 { 606 {
591 GNUNET_break (0); 607 GNUNET_break (0);
592 return; 608 return;
@@ -636,18 +652,24 @@ handle_channel_destroy (void *cls,
636{ 652{
637 struct GNUNET_CADET_Handle *h = cls; 653 struct GNUNET_CADET_Handle *h = cls;
638 struct GNUNET_CADET_Channel *ch; 654 struct GNUNET_CADET_Channel *ch;
639 CADET_ChannelNumber chid; 655 struct GNUNET_CADET_ClientChannelNumber chid;
640 656
641 chid = ntohl (msg->channel_id); 657 chid = msg->channel_id;
642 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X Destroy from service\n", chid); 658 LOG (GNUNET_ERROR_TYPE_DEBUG,
643 ch = retrieve_channel (h, chid); 659 "Channel %X Destroy from service\n",
660 ntohl (chid.channel_of_client));
661 ch = retrieve_channel (h,
662 chid);
644 663
645 if (NULL == ch) 664 if (NULL == ch)
646 { 665 {
647 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X unknown\n", chid); 666 LOG (GNUNET_ERROR_TYPE_DEBUG,
667 "channel %X unknown\n",
668 ntohl (chid.channel_of_client));
648 return; 669 return;
649 } 670 }
650 destroy_channel (ch, GNUNET_YES); 671 destroy_channel (ch,
672 GNUNET_YES);
651} 673}
652 674
653 675
@@ -674,7 +696,8 @@ check_local_data (void *cls,
674 return GNUNET_SYSERR; 696 return GNUNET_SYSERR;
675 } 697 }
676 698
677 ch = retrieve_channel (h, ntohl (message->id)); 699 ch = retrieve_channel (h,
700 message->id);
678 if (NULL == ch) 701 if (NULL == ch)
679 { 702 {
680 GNUNET_break_op (0); 703 GNUNET_break_op (0);
@@ -702,14 +725,17 @@ handle_local_data (void *cls,
702 unsigned int i; 725 unsigned int i;
703 uint16_t type; 726 uint16_t type;
704 727
705 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n"); 728 LOG (GNUNET_ERROR_TYPE_DEBUG,
706 ch = retrieve_channel (h, ntohl (message->id)); 729 "Got a data message!\n");
730 ch = retrieve_channel (h, message->id);
707 GNUNET_assert (NULL != ch); 731 GNUNET_assert (NULL != ch);
708 732
709 payload = (struct GNUNET_MessageHeader *) &message[1]; 733 payload = (struct GNUNET_MessageHeader *) &message[1];
710 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on channel %s [%X]\n", 734 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on channel %s [%X]\n",
711 GC_f2s (ch->chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV), 735 GC_f2s (ntohl (ch->chid.channel_of_client) >=
712 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)), ntohl (message->id)); 736 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI),
737 GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)),
738 ntohl (message->id.channel_of_client));
713 739
714 type = ntohs (payload->type); 740 type = ntohs (payload->type);
715 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %s\n", GC_m2s (type)); 741 LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %s\n", GC_m2s (type));
@@ -751,17 +777,21 @@ handle_local_ack (void *cls,
751{ 777{
752 struct GNUNET_CADET_Handle *h = cls; 778 struct GNUNET_CADET_Handle *h = cls;
753 struct GNUNET_CADET_Channel *ch; 779 struct GNUNET_CADET_Channel *ch;
754 CADET_ChannelNumber chid; 780 struct GNUNET_CADET_ClientChannelNumber chid;
755 781
756 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n"); 782 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
757 chid = ntohl (message->channel_id); 783 chid = message->channel_id;
758 ch = retrieve_channel (h, chid); 784 ch = retrieve_channel (h, chid);
759 if (NULL == ch) 785 if (NULL == ch)
760 { 786 {
761 LOG (GNUNET_ERROR_TYPE_DEBUG, "ACK on unknown channel %X\n", chid); 787 LOG (GNUNET_ERROR_TYPE_DEBUG,
788 "ACK on unknown channel %X\n",
789 ntohl (chid.channel_of_client));
762 return; 790 return;
763 } 791 }
764 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X!\n", ch->chid); 792 LOG (GNUNET_ERROR_TYPE_DEBUG,
793 " on channel %X!\n",
794 ntohl (ch->chid.channel_of_client));
765 ch->allow_send = GNUNET_YES; 795 ch->allow_send = GNUNET_YES;
766 if (0 < ch->packet_size) 796 if (0 < ch->packet_size)
767 { 797 {
@@ -1132,9 +1162,12 @@ handle_get_tunnels (void *cls,
1132{ 1162{
1133 struct GNUNET_CADET_Handle *h = cls; 1163 struct GNUNET_CADET_Handle *h = cls;
1134 1164
1135 h->info_cb.tunnels_cb (h->info_cls, &msg->destination, 1165 h->info_cb.tunnels_cb (h->info_cls,
1136 ntohl (msg->channels), ntohl (msg->connections), 1166 &msg->destination,
1137 ntohs (msg->estate), ntohs (msg->cstate)); 1167 ntohl (msg->channels),
1168 ntohl (msg->connections),
1169 ntohs (msg->estate),
1170 ntohs (msg->cstate));
1138 1171
1139} 1172}
1140 1173
@@ -1170,13 +1203,14 @@ check_get_tunnel (void *cls,
1170 if (esize > msize) 1203 if (esize > msize)
1171 { 1204 {
1172 GNUNET_break_op (0); 1205 GNUNET_break_op (0);
1173 h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0); 1206 h->info_cb.tunnel_cb (h->info_cls,
1207 NULL, 0, 0, NULL, NULL, 0, 0);
1174 goto clean_cls; 1208 goto clean_cls;
1175 } 1209 }
1176 ch_n = ntohl (msg->channels); 1210 ch_n = ntohl (msg->channels);
1177 c_n = ntohl (msg->connections); 1211 c_n = ntohl (msg->connections);
1178 esize += ch_n * sizeof (CADET_ChannelNumber); 1212 esize += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1179 esize += c_n * sizeof (struct GNUNET_CADET_Hash); 1213 esize += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1180 if (msize != esize) 1214 if (msize != esize)
1181 { 1215 {
1182 GNUNET_break_op (0); 1216 GNUNET_break_op (0);
@@ -1186,7 +1220,8 @@ check_get_tunnel (void *cls,
1186 (unsigned int) esize, 1220 (unsigned int) esize,
1187 ch_n, 1221 ch_n,
1188 c_n); 1222 c_n);
1189 h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0); 1223 h->info_cb.tunnel_cb (h->info_cls,
1224 NULL, 0, 0, NULL, NULL, 0, 0);
1190 goto clean_cls; 1225 goto clean_cls;
1191 } 1226 }
1192 1227
@@ -1212,18 +1247,23 @@ handle_get_tunnel (void *cls,
1212 struct GNUNET_CADET_Handle *h = cls; 1247 struct GNUNET_CADET_Handle *h = cls;
1213 unsigned int ch_n; 1248 unsigned int ch_n;
1214 unsigned int c_n; 1249 unsigned int c_n;
1215 struct GNUNET_CADET_Hash *conns; 1250 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conns;
1216 CADET_ChannelNumber *chns; 1251 const struct GNUNET_CADET_ChannelTunnelNumber *chns;
1217 1252
1218 ch_n = ntohl (msg->channels); 1253 ch_n = ntohl (msg->channels);
1219 c_n = ntohl (msg->connections); 1254 c_n = ntohl (msg->connections);
1220 1255
1221 /* Call Callback with tunnel info. */ 1256 /* Call Callback with tunnel info. */
1222 conns = (struct GNUNET_CADET_Hash *) &msg[1]; 1257 conns = (const struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
1223 chns = (CADET_ChannelNumber *) &conns[c_n]; 1258 chns = (const struct GNUNET_CADET_ChannelTunnelNumber *) &conns[c_n];
1224 h->info_cb.tunnel_cb (h->info_cls, &msg->destination, 1259 h->info_cb.tunnel_cb (h->info_cls,
1225 ch_n, c_n, chns, conns, 1260 &msg->destination,
1226 ntohs (msg->estate), ntohs (msg->cstate)); 1261 ch_n,
1262 c_n,
1263 chns,
1264 conns,
1265 ntohs (msg->estate),
1266 ntohs (msg->cstate));
1227} 1267}
1228 1268
1229 1269
@@ -1241,8 +1281,8 @@ do_reconnect (struct GNUNET_CADET_Handle *h)
1241{ 1281{
1242 struct GNUNET_MQ_MessageHandler handlers[] = { 1282 struct GNUNET_MQ_MessageHandler handlers[] = {
1243 GNUNET_MQ_hd_fixed_size (channel_created, 1283 GNUNET_MQ_hd_fixed_size (channel_created,
1244 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE, 1284 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN,
1245 struct GNUNET_CADET_ChannelCreateMessage, 1285 struct GNUNET_CADET_ChannelOpenMessageMessage,
1246 h), 1286 h),
1247 GNUNET_MQ_hd_fixed_size (channel_destroy, 1287 GNUNET_MQ_hd_fixed_size (channel_destroy,
1248 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY, 1288 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
@@ -1274,7 +1314,7 @@ do_reconnect (struct GNUNET_CADET_Handle *h)
1274 h), 1314 h),
1275 // FIXME 1315 // FIXME
1276// GNUNET_MQ_hd_fixed_Y size (channel_destroyed, 1316// GNUNET_MQ_hd_fixed_Y size (channel_destroyed,
1277// GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK, 1317// GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED,
1278// struct GNUNET_CADET_ChannelDestroyMessage); 1318// struct GNUNET_CADET_ChannelDestroyMessage);
1279 GNUNET_MQ_handler_end () 1319 GNUNET_MQ_handler_end ()
1280 }; 1320 };
@@ -1282,7 +1322,7 @@ do_reconnect (struct GNUNET_CADET_Handle *h)
1282 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n"); 1322 LOG (GNUNET_ERROR_TYPE_DEBUG, "Connecting to CADET\n");
1283 1323
1284 GNUNET_assert (NULL == h->mq); 1324 GNUNET_assert (NULL == h->mq);
1285 h->mq = GNUNET_CLIENT_connecT (h->cfg, 1325 h->mq = GNUNET_CLIENT_connect (h->cfg,
1286 "cadet", 1326 "cadet",
1287 handlers, 1327 handlers,
1288 &handle_mq_error, 1328 &handle_mq_error,
@@ -1343,9 +1383,10 @@ reconnect (struct GNUNET_CADET_Handle *h)
1343/******************************************************************************/ 1383/******************************************************************************/
1344 1384
1345struct GNUNET_CADET_Handle * 1385struct GNUNET_CADET_Handle *
1346GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls, 1386GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1347 GNUNET_CADET_ChannelEndHandler cleaner, 1387 void *cls,
1348 const struct GNUNET_CADET_MessageHandler *handlers) 1388 GNUNET_CADET_ChannelEndHandler cleaner,
1389 const struct GNUNET_CADET_MessageHandler *handlers)
1349{ 1390{
1350 struct GNUNET_CADET_Handle *h; 1391 struct GNUNET_CADET_Handle *h;
1351 1392
@@ -1364,7 +1405,7 @@ GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
1364 } 1405 }
1365 h->cls = cls; 1406 h->cls = cls;
1366 h->message_handlers = handlers; 1407 h->message_handlers = handlers;
1367 h->next_chid = GNUNET_CADET_LOCAL_CHANNEL_ID_CLI; 1408 h->next_chid.channel_of_client = htonl (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
1368 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS; 1409 h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
1369 h->reconnect_task = NULL; 1410 h->reconnect_task = NULL;
1370 1411
@@ -1384,21 +1425,24 @@ GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1384 struct GNUNET_CADET_Channel *aux; 1425 struct GNUNET_CADET_Channel *aux;
1385 struct GNUNET_CADET_TransmitHandle *th; 1426 struct GNUNET_CADET_TransmitHandle *th;
1386 1427
1387 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET DISCONNECT\n"); 1428 LOG (GNUNET_ERROR_TYPE_DEBUG,
1388 1429 "CADET DISCONNECT\n");
1389 ch = handle->channels_head; 1430 ch = handle->channels_head;
1390 while (NULL != ch) 1431 while (NULL != ch)
1391 { 1432 {
1392 aux = ch->next; 1433 aux = ch->next;
1393 if (ch->chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV) 1434 if (ntohl (ch->chid.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1394 { 1435 {
1395 GNUNET_break (0); 1436 GNUNET_break (0);
1396 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X not destroyed\n", ch->chid); 1437 LOG (GNUNET_ERROR_TYPE_DEBUG,
1438 "channel %X not destroyed\n",
1439 ntohl (ch->chid.channel_of_client));
1397 } 1440 }
1398 destroy_channel (ch, GNUNET_YES); 1441 destroy_channel (ch,
1442 GNUNET_YES);
1399 ch = aux; 1443 ch = aux;
1400 } 1444 }
1401 while ( (th = handle->th_head) != NULL) 1445 while (NULL != (th = handle->th_head))
1402 { 1446 {
1403 struct GNUNET_MessageHeader *msg; 1447 struct GNUNET_MessageHeader *msg;
1404 1448
@@ -1409,7 +1453,7 @@ GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
1409 msg = (struct GNUNET_MessageHeader *) &th[1]; 1453 msg = (struct GNUNET_MessageHeader *) &th[1];
1410 switch (ntohs(msg->type)) 1454 switch (ntohs(msg->type))
1411 { 1455 {
1412 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE: 1456 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
1413 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY: 1457 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1414 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN: 1458 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN:
1415 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE: 1459 case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE:
@@ -1531,27 +1575,30 @@ GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
1531 const struct GNUNET_HashCode *port, 1575 const struct GNUNET_HashCode *port,
1532 enum GNUNET_CADET_ChannelOption options) 1576 enum GNUNET_CADET_ChannelOption options)
1533{ 1577{
1534 struct GNUNET_CADET_ChannelCreateMessage *msg; 1578 struct GNUNET_CADET_ChannelOpenMessageMessage *msg;
1535 struct GNUNET_MQ_Envelope *env; 1579 struct GNUNET_MQ_Envelope *env;
1536 struct GNUNET_CADET_Channel *ch; 1580 struct GNUNET_CADET_Channel *ch;
1581 struct GNUNET_CADET_ClientChannelNumber chid;
1537 1582
1538 LOG (GNUNET_ERROR_TYPE_DEBUG, 1583 LOG (GNUNET_ERROR_TYPE_DEBUG,
1539 "Creating new channel to %s:%u\n", 1584 "Creating new channel to %s:%u\n",
1540 GNUNET_i2s (peer), port); 1585 GNUNET_i2s (peer), port);
1541 ch = create_channel (h, 0); 1586 chid.channel_of_client = htonl (0);
1587 ch = create_channel (h, chid);
1542 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", ch); 1588 LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", ch);
1543 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", ch->chid); 1589 LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n",
1590 ntohl (ch->chid.channel_of_client));
1544 ch->ctx = channel_ctx; 1591 ch->ctx = channel_ctx;
1545 ch->peer = GNUNET_PEER_intern (peer); 1592 ch->peer = GNUNET_PEER_intern (peer);
1546 1593
1547 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE); 1594 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN);
1548 msg->channel_id = htonl (ch->chid); 1595 msg->channel_id = ch->chid;
1549 msg->port = *port; 1596 msg->port = *port;
1550 msg->peer = *peer; 1597 msg->peer = *peer;
1551 msg->opt = htonl (options); 1598 msg->opt = htonl (options);
1552 ch->allow_send = GNUNET_NO; 1599 ch->allow_send = GNUNET_NO;
1553 GNUNET_MQ_send (h->mq, env); 1600 GNUNET_MQ_send (h->mq,
1554 1601 env);
1555 return ch; 1602 return ch;
1556} 1603}
1557 1604
@@ -1590,7 +1637,7 @@ GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
1590 } 1637 }
1591 1638
1592 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY); 1639 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1593 msg->channel_id = htonl (channel->chid); 1640 msg->channel_id = channel->chid;
1594 GNUNET_MQ_send (h->mq, env); 1641 GNUNET_MQ_send (h->mq, env);
1595 1642
1596 destroy_channel (channel, GNUNET_YES); 1643 destroy_channel (channel, GNUNET_YES);
@@ -1617,7 +1664,7 @@ GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
1617 { 1664 {
1618 case GNUNET_CADET_OPTION_NOBUFFER: 1665 case GNUNET_CADET_OPTION_NOBUFFER:
1619 case GNUNET_CADET_OPTION_RELIABLE: 1666 case GNUNET_CADET_OPTION_RELIABLE:
1620 case GNUNET_CADET_OPTION_OOORDER: 1667 case GNUNET_CADET_OPTION_OUT_OF_ORDER:
1621 if (0 != (option & channel->options)) 1668 if (0 != (option & channel->options))
1622 bool_flag = GNUNET_YES; 1669 bool_flag = GNUNET_YES;
1623 else 1670 else
@@ -1651,7 +1698,8 @@ GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel,
1651 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY\n"); 1698 LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY\n");
1652 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", channel->chid); 1699 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", channel->chid);
1653 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", channel->allow_send); 1700 LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", channel->allow_send);
1654 if (channel->chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV) 1701 if (ntohl (channel->chid.channel_of_client) >=
1702 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1655 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n"); 1703 LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
1656 else 1704 else
1657 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n"); 1705 LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
@@ -1946,7 +1994,7 @@ GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
1946 1994
1947 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL); 1995 env = GNUNET_MQ_msg (msg, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
1948 msg->peer = *initiator; 1996 msg->peer = *initiator;
1949 msg->channel_id = htonl (channel_number); 1997 msg->channel_id.channel_of_client = htonl (channel_number);
1950 GNUNET_MQ_send (h->mq, env); 1998 GNUNET_MQ_send (h->mq, env);
1951 1999
1952 h->info_cb.channel_cb = callback; 2000 h->info_cb.channel_cb = callback;
diff --git a/src/cadet/cadet_common.c b/src/cadet/cadet_common.c
index a9d9a35be..95a3144e4 100644
--- a/src/cadet/cadet_common.c
+++ b/src/cadet/cadet_common.c
@@ -51,11 +51,20 @@ GC_f2s (int fwd)
51 } 51 }
52} 52}
53 53
54
55/**
56 * Test if @a bigger is larger than @a smaller.
57 * Considers the case that @a bigger just overflowed
58 * and is thus tiny while @a smaller is still below
59 * `UINT32_MAX`.
60 */
54int 61int
55GC_is_pid_bigger (uint32_t bigger, uint32_t smaller) 62GC_is_pid_bigger (uint32_t bigger,
63 uint32_t smaller)
56{ 64{
57 return (GNUNET_YES == PID_OVERFLOW (smaller, bigger) || 65 return (PID_OVERFLOW (smaller, bigger) ||
58 (bigger > smaller && GNUNET_NO == PID_OVERFLOW (bigger, smaller))); 66 ( (bigger > smaller) &&
67 (! PID_OVERFLOW (bigger, smaller))) );
59} 68}
60 69
61 70
@@ -77,28 +86,6 @@ GC_min_pid (uint32_t a, uint32_t b)
77} 86}
78 87
79 88
80const struct GNUNET_HashCode *
81GC_h2hc (const struct GNUNET_CADET_Hash *id)
82{
83 static struct GNUNET_HashCode hc;
84 GNUNET_memcpy (&hc, id, sizeof (*id));
85
86 return &hc;
87}
88
89
90const char *
91GC_h2s (const struct GNUNET_CADET_Hash *id)
92{
93 static char s[53];
94
95 GNUNET_memcpy (s, GNUNET_h2s_full (GC_h2hc (id)), 52);
96 s[52] = '\0';
97
98 return s;
99}
100
101
102/** 89/**
103 * Allocate a string with a hexdump of any binary data. 90 * Allocate a string with a hexdump of any binary data.
104 * 91 *
@@ -159,7 +146,7 @@ GC_m2s (uint16_t m)
159 /** 146 /**
160 * Request the modification of an existing path 147 * Request the modification of an existing path
161 */ 148 */
162 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK: 149 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK:
163 s = "CONN_ACK"; 150 s = "CONN_ACK";
164 break; 151 break;
165 152
@@ -173,35 +160,35 @@ GC_m2s (uint16_t m)
173 /** 160 /**
174 * At some point, the route will spontaneously change 161 * At some point, the route will spontaneously change
175 */ 162 */
176 case GNUNET_MESSAGE_TYPE_CADET_PATH_CHANGED: 163 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_PATH_CHANGED_UNIMPLEMENTED:
177 s = "PATH_CHNGD"; 164 s = "PATH_CHNGD";
178 break; 165 break;
179 166
180 /** 167 /**
181 * Transport payload data. 168 * Transport payload data.
182 */ 169 */
183 case GNUNET_MESSAGE_TYPE_CADET_DATA: 170 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA:
184 s = "DATA"; 171 s = "DATA";
185 break; 172 break;
186 173
187 /** 174 /**
188 * Confirm receipt of payload data. 175 * Confirm receipt of payload data.
189 */ 176 */
190 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK: 177 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK:
191 s = "DATA_ACK"; 178 s = "DATA_ACK";
192 break; 179 break;
193 180
194 /** 181 /**
195 * Key exchange message. 182 * Key exchange message.
196 */ 183 */
197 case GNUNET_MESSAGE_TYPE_CADET_KX: 184 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX:
198 s = "KX"; 185 s = "KX";
199 break; 186 break;
200 187
201 /** 188 /**
202 * Encrypted. 189 * Encrypted.
203 */ 190 */
204 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED: 191 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED:
205 s = "ENCRYPTED"; 192 s = "ENCRYPTED";
206 break; 193 break;
207 194
@@ -215,21 +202,21 @@ GC_m2s (uint16_t m)
215 /** 202 /**
216 * ACK for a data packet. 203 * ACK for a data packet.
217 */ 204 */
218 case GNUNET_MESSAGE_TYPE_CADET_ACK: 205 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK:
219 s = "ACK"; 206 s = "ACK";
220 break; 207 break;
221 208
222 /** 209 /**
223 * POLL for ACK. 210 * POLL for ACK.
224 */ 211 */
225 case GNUNET_MESSAGE_TYPE_CADET_POLL: 212 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL:
226 s = "POLL"; 213 s = "POLL";
227 break; 214 break;
228 215
229 /** 216 /**
230 * Announce origin is still alive. 217 * Announce origin is still alive.
231 */ 218 */
232 case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE: 219 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE:
233 s = "KEEPALIVE"; 220 s = "KEEPALIVE";
234 break; 221 break;
235 222
@@ -250,7 +237,7 @@ GC_m2s (uint16_t m)
250 /** 237 /**
251 * Ask the cadet service to create a new tunnel 238 * Ask the cadet service to create a new tunnel
252 */ 239 */
253 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE: 240 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
254 s = "CHAN_CREAT"; 241 s = "CHAN_CREAT";
255 break; 242 break;
256 243
@@ -264,14 +251,14 @@ GC_m2s (uint16_t m)
264 /** 251 /**
265 * Confirm the creation of a channel. 252 * Confirm the creation of a channel.
266 */ 253 */
267 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK: 254 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK:
268 s = "CHAN_ACK"; 255 s = "CHAN_ACK";
269 break; 256 break;
270 257
271 /** 258 /**
272 * Confirm the creation of a channel. 259 * Confirm the creation of a channel.
273 */ 260 */
274 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK: 261 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED:
275 s = "CHAN_NACK"; 262 s = "CHAN_NACK";
276 break; 263 break;
277 264
diff --git a/src/cadet/cadet_protocol.h b/src/cadet/cadet_protocol.h
index d034c63b0..cf32e0d6d 100644
--- a/src/cadet/cadet_protocol.h
+++ b/src/cadet/cadet_protocol.h
@@ -56,12 +56,12 @@ GNUNET_NETWORK_STRUCT_BEGIN
56/** 56/**
57 * Message for cadet connection creation. 57 * Message for cadet connection creation.
58 */ 58 */
59struct GNUNET_CADET_ConnectionCreate 59struct GNUNET_CADET_ConnectionCreateMessage
60{ 60{
61 /** 61 /**
62 * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE 62 * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE
63 * 63 *
64 * Size: sizeof (struct GNUNET_CADET_ConnectionCreate) + 64 * Size: sizeof (struct GNUNET_CADET_ConnectionCreateMessage) +
65 * path_length * sizeof (struct GNUNET_PeerIdentity) 65 * path_length * sizeof (struct GNUNET_PeerIdentity)
66 */ 66 */
67 struct GNUNET_MessageHeader header; 67 struct GNUNET_MessageHeader header;
@@ -70,11 +70,11 @@ struct GNUNET_CADET_ConnectionCreate
70 * For alignment. 70 * For alignment.
71 */ 71 */
72 uint32_t reserved GNUNET_PACKED; 72 uint32_t reserved GNUNET_PACKED;
73 73
74 /** 74 /**
75 * ID of the connection 75 * ID of the connection
76 */ 76 */
77 struct GNUNET_CADET_Hash cid; 77 struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
78 78
79 /** 79 /**
80 * path_length structs defining the *whole* path from the origin [0] to the 80 * path_length structs defining the *whole* path from the origin [0] to the
@@ -87,10 +87,10 @@ struct GNUNET_CADET_ConnectionCreate
87/** 87/**
88 * Message for ack'ing a connection 88 * Message for ack'ing a connection
89 */ 89 */
90struct GNUNET_CADET_ConnectionACK 90struct GNUNET_CADET_ConnectionCreateMessageAckMessage
91{ 91{
92 /** 92 /**
93 * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK 93 * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK
94 */ 94 */
95 struct GNUNET_MessageHeader header; 95 struct GNUNET_MessageHeader header;
96 96
@@ -102,7 +102,7 @@ struct GNUNET_CADET_ConnectionACK
102 /** 102 /**
103 * ID of the connection. 103 * ID of the connection.
104 */ 104 */
105 struct GNUNET_CADET_Hash cid; 105 struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
106 106
107}; 107};
108 108
@@ -110,10 +110,10 @@ struct GNUNET_CADET_ConnectionACK
110/** 110/**
111 * Message for notifying a disconnection in a path 111 * Message for notifying a disconnection in a path
112 */ 112 */
113struct GNUNET_CADET_ConnectionBroken 113struct GNUNET_CADET_ConnectionBrokenMessage
114{ 114{
115 /** 115 /**
116 * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN 116 * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN.
117 */ 117 */
118 struct GNUNET_MessageHeader header; 118 struct GNUNET_MessageHeader header;
119 119
@@ -125,7 +125,7 @@ struct GNUNET_CADET_ConnectionBroken
125 /** 125 /**
126 * ID of the connection. 126 * ID of the connection.
127 */ 127 */
128 struct GNUNET_CADET_Hash cid; 128 struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
129 129
130 /** 130 /**
131 * ID of the endpoint 131 * ID of the endpoint
@@ -142,7 +142,7 @@ struct GNUNET_CADET_ConnectionBroken
142/** 142/**
143 * Message to destroy a connection. 143 * Message to destroy a connection.
144 */ 144 */
145struct GNUNET_CADET_ConnectionDestroy 145struct GNUNET_CADET_ConnectionDestroyMessage
146{ 146{
147 /** 147 /**
148 * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY 148 * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY
@@ -157,60 +157,31 @@ struct GNUNET_CADET_ConnectionDestroy
157 /** 157 /**
158 * ID of the connection. 158 * ID of the connection.
159 */ 159 */
160 struct GNUNET_CADET_Hash cid; 160 struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
161}; 161};
162 162
163 163
164/** 164/******************************************************************************/
165 * Message to acknowledge cadet encrypted traffic. 165/******************************* TUNNEL ***********************************/
166 */ 166/******************************************************************************/
167struct GNUNET_CADET_ACK
168{
169 /**
170 * Type: #GNUNET_MESSAGE_TYPE_CADET_ACK
171 */
172 struct GNUNET_MessageHeader header;
173
174 /**
175 * Maximum packet ID authorized.
176 */
177 uint32_t ack GNUNET_PACKED;
178
179 /**
180 * ID of the connection.
181 */
182 struct GNUNET_CADET_Hash cid;
183};
184
185 167
186/** 168/**
187 * Message to query a peer about its Flow Control status regarding a tunnel. 169 * Unique identifier (counter) for an encrypted message in a channel.
170 * Used to match #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK
171 * and #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL messages
172 * against the respective #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED
173 * messages.
188 */ 174 */
189struct GNUNET_CADET_Poll 175struct CadetEncryptedMessageIdentifier
190{ 176{
191 /** 177 /**
192 * Type: #GNUNET_MESSAGE_TYPE_CADET_POLL 178 * This number is incremented by one per message. It may wrap around.
193 */ 179 * In network byte order.
194 struct GNUNET_MessageHeader header;
195
196 /**
197 * Last packet sent.
198 */ 180 */
199 uint32_t pid GNUNET_PACKED; 181 uint32_t pid GNUNET_PACKED;
200
201 /**
202 * ID of the connection.
203 */
204 struct GNUNET_CADET_Hash cid;
205
206}; 182};
207 183
208 184
209
210/******************************************************************************/
211/******************************* TUNNEL ***********************************/
212/******************************************************************************/
213
214/** 185/**
215 * Flags to be used in GNUNET_CADET_KX. 186 * Flags to be used in GNUNET_CADET_KX.
216 */ 187 */
@@ -231,10 +202,10 @@ enum GNUNET_CADET_KX_Flags {
231/** 202/**
232 * Message for a Key eXchange for a tunnel. 203 * Message for a Key eXchange for a tunnel.
233 */ 204 */
234struct GNUNET_CADET_KX 205struct GNUNET_CADET_TunnelKeyExchangeMessage
235{ 206{
236 /** 207 /**
237 * Type: #GNUNET_MESSAGE_TYPE_CADET_KX. 208 * Type: #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX.
238 */ 209 */
239 struct GNUNET_MessageHeader header; 210 struct GNUNET_MessageHeader header;
240 211
@@ -247,7 +218,7 @@ struct GNUNET_CADET_KX
247 /** 218 /**
248 * ID of the connection. 219 * ID of the connection.
249 */ 220 */
250 struct GNUNET_CADET_Hash cid; 221 struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
251 222
252 /** 223 /**
253 * Sender's ephemeral public ECC key encoded in a 224 * Sender's ephemeral public ECC key encoded in a
@@ -262,35 +233,42 @@ struct GNUNET_CADET_KX
262 * using 'gcry_sexp_sprint'. 233 * using 'gcry_sexp_sprint'.
263 */ 234 */
264 struct GNUNET_CRYPTO_EcdhePublicKey ratchet_key; 235 struct GNUNET_CRYPTO_EcdhePublicKey ratchet_key;
236
237#ifdef NEW_CADET
238 /**
239 * Proof that sender could compute the 3-DH, in lieu of a signature.
240 */
241 struct GNUNET_HashCode triple_dh_proof;
242#endif
265}; 243};
266 244
267 245
268/** 246/**
269 * Axolotl tunnel message. 247 * Axolotl tunnel message.
270 */ 248 */
271struct GNUNET_CADET_Encrypted 249struct GNUNET_CADET_TunnelEncryptedMessage
272{ 250{
273 /** 251 /**
274 * Type: #GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED 252 * Type: #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED
275 */ 253 */
276 struct GNUNET_MessageHeader header; 254 struct GNUNET_MessageHeader header;
277 255
278 /** 256 /**
279 * ID of the packet (hop by hop). 257 * ID of the packet (hop by hop).
280 */ 258 */
281 uint32_t pid GNUNET_PACKED; 259 struct CadetEncryptedMessageIdentifier cemi;
282 260
283 /** 261 /**
284 * ID of the connection. 262 * ID of the connection.
285 */ 263 */
286 struct GNUNET_CADET_Hash cid; 264 struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
287 265
288 /** 266 /**
289 * MAC of the encrypted message, used to verify message integrity. 267 * MAC of the encrypted message, used to verify message integrity.
290 * Everything after this value will be encrypted with the header key 268 * Everything after this value will be encrypted with the header key
291 * and authenticated. 269 * and authenticated.
292 */ 270 */
293 struct GNUNET_CADET_Hash hmac; 271 struct GNUNET_ShortHashCode hmac;
294 272
295 /**************** AX_HEADER start ****************/ 273 /**************** AX_HEADER start ****************/
296 274
@@ -317,18 +295,69 @@ struct GNUNET_CADET_Encrypted
317}; 295};
318 296
319 297
298/**
299 * Message to query a peer about its Flow Control status regarding a tunnel.
300 *
301 * It is NOT yet clear if we need this.
302 */
303struct GNUNET_CADET_ConnectionHopByHopPollMessage
304{
305 /**
306 * Type: #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL
307 */
308 struct GNUNET_MessageHeader header;
309
310 /**
311 * Last packet sent.
312 */
313 struct CadetEncryptedMessageIdentifier cemi;
314
315 /**
316 * ID of the connection.
317 */
318 struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
319
320};
321
322
323/**
324 * Message to acknowledge cadet encrypted traffic, used for
325 * flow-control on a hop-by-hop basis on the connection-level. Note
326 * that we do use the @e cemi from the tunnel layer as the connection
327 * layer's header is included/shared with the tunnel layer messages,
328 * and we only do flow control for the payload.
329 */
330struct GNUNET_CADET_ConnectionEncryptedAckMessage
331{
332 /**
333 * Type: #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK
334 */
335 struct GNUNET_MessageHeader header;
336
337 /**
338 * Maximum packet ID authorized.
339 */
340 struct CadetEncryptedMessageIdentifier cemi;
341
342 /**
343 * ID of the connection.
344 */
345 struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
346};
347
320 348
321/******************************************************************************/ 349/******************************************************************************/
322/******************************* CHANNEL ***********************************/ 350/******************************* CHANNEL ***********************************/
323/******************************************************************************/ 351/******************************************************************************/
324 352
353
325/** 354/**
326 * Message to create a Channel. 355 * Message to create a Channel.
327 */ 356 */
328struct GNUNET_CADET_ChannelCreate 357struct GNUNET_CADET_ChannelOpenMessage
329{ 358{
330 /** 359 /**
331 * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE 360 * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN
332 */ 361 */
333 struct GNUNET_MessageHeader header; 362 struct GNUNET_MessageHeader header;
334 363
@@ -343,33 +372,42 @@ struct GNUNET_CADET_ChannelCreate
343 struct GNUNET_HashCode port; 372 struct GNUNET_HashCode port;
344 373
345 /** 374 /**
346 * ID of the channel 375 * ID of the channel within the tunnel.
347 */ 376 */
348 CADET_ChannelNumber chid GNUNET_PACKED; 377 struct GNUNET_CADET_ChannelTunnelNumber chid;
349}; 378};
350 379
351 380
352/** 381/**
353 * Message to manage a Channel (ACK, NACK, Destroy). 382 * Message to manage a Channel (ACK, NACK, Destroy).
354 */ 383 */
355struct GNUNET_CADET_ChannelManage 384struct GNUNET_CADET_ChannelManageMessage
356{ 385{
357 /** 386 /**
358 * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_{ACK|NACK|DESTROY} 387 * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_{ACK|NACK|DESTROY}
359 */ 388 */
360 struct GNUNET_MessageHeader header; 389 struct GNUNET_MessageHeader header;
361 390
391#ifdef NEW_CADET
392 /**
393 * For alignment.
394 */
395 uint32_t reserved GNUNET_PACKED;
396#endif
397
362 /** 398 /**
363 * ID of the channel 399 * ID of the channel
364 */ 400 */
365 CADET_ChannelNumber chid GNUNET_PACKED; 401 struct GNUNET_CADET_ChannelTunnelNumber chid;
366}; 402};
367 403
368 404
405#ifndef NEW_CADET
406
369/** 407/**
370 * Message for cadet data traffic. 408 * Message for cadet data traffic.
371 */ 409 */
372struct GNUNET_CADET_Data 410struct GNUNET_CADET_ChannelAppDataMessage
373{ 411{
374 /** 412 /**
375 * Type: #GNUNET_MESSAGE_TYPE_CADET_UNICAST, 413 * Type: #GNUNET_MESSAGE_TYPE_CADET_UNICAST,
@@ -380,12 +418,13 @@ struct GNUNET_CADET_Data
380 /** 418 /**
381 * Unique ID of the payload message 419 * Unique ID of the payload message
382 */ 420 */
421 /* NEW: struct ChannelMessageIdentifier */
383 uint32_t mid GNUNET_PACKED; 422 uint32_t mid GNUNET_PACKED;
384 423
385 /** 424 /**
386 * ID of the channel 425 * ID of the channel
387 */ 426 */
388 CADET_ChannelNumber chid GNUNET_PACKED; 427 struct GNUNET_CADET_ChannelTunnelNumber chid;
389 428
390 /** 429 /**
391 * Payload follows 430 * Payload follows
@@ -396,17 +435,17 @@ struct GNUNET_CADET_Data
396/** 435/**
397 * Message to acknowledge end-to-end data. 436 * Message to acknowledge end-to-end data.
398 */ 437 */
399struct GNUNET_CADET_DataACK 438struct GNUNET_CADET_ChannelDataAckMessage
400{ 439{
401 /** 440 /**
402 * Type: GNUNET_MESSAGE_TYPE_CADET_DATA_ACK 441 * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK
403 */ 442 */
404 struct GNUNET_MessageHeader header; 443 struct GNUNET_MessageHeader header;
405 444
406 /** 445 /**
407 * ID of the channel 446 * ID of the channel
408 */ 447 */
409 CADET_ChannelNumber chid GNUNET_PACKED; 448 struct GNUNET_CADET_ChannelTunnelNumber chid;
410 449
411 /** 450 /**
412 * Bitfield of already-received newer messages 451 * Bitfield of already-received newer messages
@@ -418,10 +457,82 @@ struct GNUNET_CADET_DataACK
418 /** 457 /**
419 * Last message ID received. 458 * Last message ID received.
420 */ 459 */
460 /* NEW: struct ChannelMessageIdentifier */
461 uint32_t mid GNUNET_PACKED;
462};
463
464#else
465
466
467/**
468 * Number used to uniquely identify messages in a CADET Channel.
469 */
470struct ChannelMessageIdentifier
471{
472 /**
473 * Unique ID of the message, cycles around, in NBO.
474 */
421 uint32_t mid GNUNET_PACKED; 475 uint32_t mid GNUNET_PACKED;
422}; 476};
423 477
424 478
479/**
480 * Message for cadet data traffic.
481 */
482struct GNUNET_CADET_ChannelAppDataMessage
483{
484 /**
485 * Type: #GNUNET_MESSAGE_TYPE_CADET_UNICAST,
486 * #GNUNET_MESSAGE_TYPE_CADET_TO_ORIGIN
487 */
488 struct GNUNET_MessageHeader header;
489
490 /**
491 * Unique ID of the payload message.
492 */
493 struct ChannelMessageIdentifier mid;
494
495 /**
496 * ID of the channel
497 */
498 struct GNUNET_CADET_ChannelTunnelNumber gid;
499
500 /**
501 * Payload follows
502 */
503};
504
505
506/**
507 * Message to acknowledge end-to-end data.
508 */
509struct GNUNET_CADET_ChannelDataAckMessage
510{
511 /**
512 * Type: #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK
513 */
514 struct GNUNET_MessageHeader header;
515
516 /**
517 * ID of the channel
518 */
519 struct GNUNET_CADET_ChannelTunnelNumber gid;
520
521 /**
522 * Bitfield of already-received messages past @e mid.
523 * pid + 1 @ LSB
524 * pid + 64 @ MSB
525 */
526 uint64_t futures GNUNET_PACKED;
527
528 /**
529 * Last message ID received.
530 */
531 struct ChannelMessageIdentifier mid;
532};
533
534
535#endif
425 536
426GNUNET_NETWORK_STRUCT_END 537GNUNET_NETWORK_STRUCT_END
427 538
diff --git a/src/cadet/gnunet-cadet.c b/src/cadet/gnunet-cadet.c
index 5afb64e24..80010ec54 100644
--- a/src/cadet/gnunet-cadet.c
+++ b/src/cadet/gnunet-cadet.c
@@ -727,8 +727,8 @@ tunnel_callback (void *cls,
727 const struct GNUNET_PeerIdentity *peer, 727 const struct GNUNET_PeerIdentity *peer,
728 unsigned int n_channels, 728 unsigned int n_channels,
729 unsigned int n_connections, 729 unsigned int n_connections,
730 uint32_t *channels, 730 const struct GNUNET_CADET_ChannelTunnelNumber *channels,
731 struct GNUNET_CADET_Hash *connections, 731 const struct GNUNET_CADET_ConnectionTunnelIdentifier *connections,
732 unsigned int estate, 732 unsigned int estate,
733 unsigned int cstate) 733 unsigned int cstate)
734{ 734{
@@ -739,10 +739,10 @@ tunnel_callback (void *cls,
739 FPRINTF (stdout, "Tunnel %s\n", GNUNET_i2s_full (peer)); 739 FPRINTF (stdout, "Tunnel %s\n", GNUNET_i2s_full (peer));
740 FPRINTF (stdout, "\t%u channels\n", n_channels); 740 FPRINTF (stdout, "\t%u channels\n", n_channels);
741 for (i = 0; i < n_channels; i++) 741 for (i = 0; i < n_channels; i++)
742 FPRINTF (stdout, "\t\t%X\n", ntohl (channels[i])); 742 FPRINTF (stdout, "\t\t%X\n", ntohl (channels[i].cn));
743 FPRINTF (stdout, "\t%u connections\n", n_connections); 743 FPRINTF (stdout, "\t%u connections\n", n_connections);
744 for (i = 0; i < n_connections; i++) 744 for (i = 0; i < n_connections; i++)
745 FPRINTF (stdout, "\t\t%s\n", GC_h2s (&connections[i])); 745 FPRINTF (stdout, "\t\t%s\n", GNUNET_sh2s (&connections[i].connection_of_tunnel));
746 FPRINTF (stdout, "\tencryption state: %s\n", enc_2s (estate)); 746 FPRINTF (stdout, "\tencryption state: %s\n", enc_2s (estate));
747 FPRINTF (stdout, "\tconnection state: %s\n", conn_2s (cstate)); 747 FPRINTF (stdout, "\tconnection state: %s\n", conn_2s (cstate));
748 } 748 }
@@ -827,7 +827,10 @@ show_tunnel (void *cls)
827 GNUNET_SCHEDULER_shutdown (); 827 GNUNET_SCHEDULER_shutdown ();
828 return; 828 return;
829 } 829 }
830 GNUNET_CADET_get_tunnel (mh, &pid, tunnel_callback, NULL); 830 GNUNET_CADET_get_tunnel (mh,
831 &pid,
832 &tunnel_callback,
833 NULL);
831} 834}
832 835
833 836
diff --git a/src/cadet/gnunet-service-cadet-new.c b/src/cadet/gnunet-service-cadet-new.c
new file mode 100644
index 000000000..2f6cc7b11
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new.c
@@ -0,0 +1,1350 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2001-2013, 2017 GNUnet e.V.
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21/**
22 * @file cadet/gnunet-service-cadet-new.c
23 * @brief GNUnet CADET service with encryption
24 * @author Bartlomiej Polot
25 * @author Christian Grothoff
26 *
27 * Dictionary:
28 * - peer: other cadet instance. If there is direct connection it's a neighbor.
29 * - path: series of directly connected peer from one peer to another.
30 * - connection: path which is being used in a tunnel.
31 * - tunnel: encrypted connection to a peer, neighbor or not.
32 * - channel: logical link between two clients, on the same or different peers.
33 * have properties like reliability.
34 */
35
36#include "platform.h"
37#include "gnunet_util_lib.h"
38#include "cadet.h"
39#include "gnunet_statistics_service.h"
40#include "gnunet-service-cadet-new.h"
41#include "gnunet-service-cadet-new_channel.h"
42#include "gnunet-service-cadet-new_connection.h"
43#include "gnunet-service-cadet-new_core.h"
44#include "gnunet-service-cadet-new_dht.h"
45#include "gnunet-service-cadet-new_hello.h"
46#include "gnunet-service-cadet-new_tunnels.h"
47#include "gnunet-service-cadet-new_peer.h"
48#include "gnunet-service-cadet-new_paths.h"
49
50#define LOG(level, ...) GNUNET_log (level,__VA_ARGS__)
51
52
53/**
54 * Struct containing information about a client of the service
55 */
56struct CadetClient
57{
58 /**
59 * Linked list next
60 */
61 struct CadetClient *next;
62
63 /**
64 * Linked list prev
65 */
66 struct CadetClient *prev;
67
68 /**
69 * Tunnels that belong to this client, indexed by local id
70 */
71 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
72
73 /**
74 * Tunnels this client has accepted, indexed by incoming local id
75 */
76 struct GNUNET_CONTAINER_MultiHashMap32 *incoming_channels;
77
78 /**
79 * Channel ID for the next incoming channel.
80 */
81 struct GNUNET_CADET_ClientChannelNumber next_chid;
82
83 /**
84 * Handle to communicate with the client
85 */
86 struct GNUNET_MQ_Handle *mq;
87
88 /**
89 * Client handle.
90 */
91 struct GNUNET_SERVICE_Client *client;
92
93 /**
94 * Ports that this client has declared interest in.
95 * Indexed by port, contains *Client.
96 */
97 struct GNUNET_CONTAINER_MultiHashMap *ports;
98
99 /**
100 * Whether the client is active or shutting down (don't send confirmations
101 * to a client that is shutting down).
102 */
103 int shutting_down;
104
105 /**
106 * ID of the client, mainly for debug messages
107 */
108 unsigned int id;
109};
110
111/******************************************************************************/
112/*********************** GLOBAL VARIABLES ****************************/
113/******************************************************************************/
114
115/****************************** Global variables ******************************/
116
117/**
118 * Handle to the statistics service.
119 */
120struct GNUNET_STATISTICS_Handle *stats;
121
122/**
123 * Handle to communicate with ATS.
124 */
125struct GNUNET_ATS_ConnectivityHandle *ats_ch;
126
127/**
128 * Local peer own ID.
129 */
130struct GNUNET_PeerIdentity my_full_id;
131
132/**
133 * Own private key.
134 */
135struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
136
137/**
138 * Signal that shutdown is happening: prevent recover measures.
139 */
140int shutting_down;
141
142/**
143 * DLL with all the clients, head.
144 */
145static struct CadetClient *clients_head;
146
147/**
148 * DLL with all the clients, tail.
149 */
150static struct CadetClient *clients_tail;
151
152/**
153 * Next ID to assign to a client.
154 */
155static unsigned int next_client_id;
156
157/**
158 * All ports clients of this peer have opened.
159 */
160struct GNUNET_CONTAINER_MultiHashMap *open_ports;
161
162/**
163 * Map from ports to channels where the ports were closed at the
164 * time we got the inbound connection.
165 * Indexed by port, contains `struct CadetChannel`.
166 */
167struct GNUNET_CONTAINER_MultiHashMap *loose_channels;
168
169/**
170 * Map from PIDs to `struct CadetPeer` entries.
171 */
172struct GNUNET_CONTAINER_MultiPeerMap *peers;
173
174/**
175 * Map from `struct GNUNET_CADET_ConnectionTunnelIdentifier`
176 * hash codes to `struct CadetConnection` objects.
177 */
178struct GNUNET_CONTAINER_MultiShortmap *connections;
179
180/**
181 * How many messages are needed to trigger an AXOLOTL ratchet advance.
182 */
183unsigned long long ratchet_messages;
184
185/**
186 * How long until we trigger a ratched advance due to time.
187 */
188struct GNUNET_TIME_Relative ratchet_time;
189
190
191
192/**
193 * Send a message to a client.
194 *
195 * @param c client to get the message
196 * @param env envelope with the message
197 */
198void
199GSC_send_to_client (struct CadetClient *c,
200 struct GNUNET_MQ_Envelope *env)
201{
202 GNUNET_MQ_send (c->mq,
203 env);
204}
205
206
207/**
208 * Return identifier for a client as a string.
209 *
210 * @param c client to identify
211 * @return string for debugging
212 */
213const char *
214GSC_2s (struct CadetClient *c)
215{
216 static char buf[32];
217
218 if (NULL == c)
219 return "Client(NULL)";
220 GNUNET_snprintf (buf,
221 sizeof (buf),
222 "Client(%u)",
223 c->id);
224 return buf;
225}
226
227
228/**
229 * Obtain the next LID to use for incoming connections to
230 * the given client.
231 *
232 * @param c client handle
233 */
234static struct GNUNET_CADET_ClientChannelNumber
235client_get_next_lid (struct CadetClient *c)
236{
237 struct GNUNET_CADET_ClientChannelNumber ccn = c->next_chid;
238
239 /* increment until we have a free one... */
240 while (NULL !=
241 GNUNET_CONTAINER_multihashmap32_get (c->incoming_channels,
242 ntohl (ccn.channel_of_client)))
243 {
244 ccn.channel_of_client
245 = htonl (1 + (ntohl (ccn.channel_of_client)));
246 if (ntohl (ccn.channel_of_client) >=
247 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
248 ccn.channel_of_client = htonl (0);
249 }
250 c->next_chid.channel_of_client
251 = htonl (1 + (ntohl (ccn.channel_of_client)));
252 return ccn;
253}
254
255
256/**
257 * Bind incoming channel to this client, and notify client
258 * about incoming connection.
259 *
260 * @param c client to bind to
261 * @param ch channel to be bound
262 * @param dest peer that establishes the connection
263 * @param port port number
264 * @param options options
265 * @return local channel number assigned to the new client
266 */
267struct GNUNET_CADET_ClientChannelNumber
268GSC_bind (struct CadetClient *c,
269 struct CadetChannel *ch,
270 struct CadetPeer *dest,
271 const struct GNUNET_HashCode *port,
272 uint32_t options)
273{
274 struct GNUNET_MQ_Envelope *env;
275 struct GNUNET_CADET_ChannelOpenMessageMessage *msg;
276 struct GNUNET_CADET_ClientChannelNumber lid;
277
278 lid = client_get_next_lid (c);
279 GNUNET_assert (GNUNET_YES ==
280 GNUNET_CONTAINER_multihashmap32_put (c->incoming_channels,
281 ntohl (lid.channel_of_client),
282 ch,
283 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
284
285 /* notify local client about incoming connection! */
286 env = GNUNET_MQ_msg (msg,
287 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN);
288 msg->channel_id = lid;
289 msg->port = *port;
290 msg->opt = htonl (options);
291 msg->peer = *GCP_get_id (dest);
292 GSC_send_to_client (c,
293 env);
294 return lid;
295}
296
297
298/******************************************************************************/
299/************************ MAIN FUNCTIONS ****************************/
300/******************************************************************************/
301
302/**
303 * Task run during shutdown.
304 *
305 * @param cls unused
306 */
307static void
308shutdown_task (void *cls)
309{
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
311 "shutting down\n");
312 shutting_down = GNUNET_YES;
313 GCO_shutdown ();
314 if (NULL != stats)
315 {
316 GNUNET_STATISTICS_destroy (stats,
317 GNUNET_NO);
318 stats = NULL;
319 }
320 if (NULL != open_ports)
321 {
322 GNUNET_CONTAINER_multihashmap_destroy (open_ports);
323 open_ports = NULL;
324 }
325 if (NULL != loose_channels)
326 {
327 GNUNET_CONTAINER_multihashmap_destroy (loose_channels);
328 loose_channels = NULL;
329 }
330 /* All channels, connections and CORE must be down before this point. */
331 GCP_destroy_all_peers ();
332 if (NULL != peers)
333 {
334 GNUNET_CONTAINER_multipeermap_destroy (peers);
335 peers = NULL;
336 }
337 if (NULL != connections)
338 {
339 GNUNET_CONTAINER_multishortmap_destroy (connections);
340 connections = NULL;
341 }
342 if (NULL != ats_ch)
343 {
344 GNUNET_ATS_connectivity_done (ats_ch);
345 ats_ch = NULL;
346 }
347 GCD_shutdown ();
348 GCH_shutdown ();
349 GNUNET_free_non_null (my_private_key);
350 my_private_key = NULL;
351}
352
353
354/**
355 * We had a remote connection @a value to port @a port before
356 * client @a cls opened port @a port. Bind them now.
357 *
358 * @param cls the `struct CadetClient`
359 * @param port the port
360 * @param value the `struct CadetChannel`
361 * @return #GNUNET_YES (iterate over all such channels)
362 */
363static int
364bind_loose_channel (void *cls,
365 const struct GNUNET_HashCode *port,
366 void *value)
367{
368 struct CadetClient *c = cls;
369 struct CadetChannel *ch = value;
370
371 GCCH_bind (ch,
372 c);
373 GNUNET_assert (GNUNET_YES ==
374 GNUNET_CONTAINER_multihashmap_remove (loose_channels,
375 port,
376 value));
377 return GNUNET_YES;
378}
379
380
381/**
382 * Handler for port open requests.
383 *
384 * @param cls Identification of the client.
385 * @param pmsg The actual message.
386 */
387static void
388handle_port_open (void *cls,
389 const struct GNUNET_CADET_PortMessage *pmsg)
390{
391 struct CadetClient *c = cls;
392
393 LOG (GNUNET_ERROR_TYPE_DEBUG,
394 "Open port %s requested by client %u\n",
395 GNUNET_h2s (&pmsg->port),
396 c->id);
397 if (NULL == c->ports)
398 c->ports = GNUNET_CONTAINER_multihashmap_create (4,
399 GNUNET_NO);
400 if (GNUNET_OK !=
401 GNUNET_CONTAINER_multihashmap_put (c->ports,
402 &pmsg->port,
403 c,
404 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
405 {
406 GNUNET_break (0);
407 GNUNET_SERVICE_client_drop (c->client);
408 return;
409 }
410 /* store in global hashmap */
411 /* FIXME only allow one client to have the port open,
412 * have a backup hashmap with waiting clients */
413 GNUNET_CONTAINER_multihashmap_put (open_ports,
414 &pmsg->port,
415 c,
416 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
417 GNUNET_CONTAINER_multihashmap_get_multiple (loose_channels,
418 &pmsg->port,
419 &bind_loose_channel,
420 c);
421 GNUNET_SERVICE_client_continue (c->client);
422}
423
424
425/**
426 * Handler for port close requests.
427 *
428 * @param cls Identification of the client.
429 * @param pmsg The actual message.
430 */
431static void
432handle_port_close (void *cls,
433 const struct GNUNET_CADET_PortMessage *pmsg)
434{
435 struct CadetClient *c = cls;
436
437 LOG (GNUNET_ERROR_TYPE_DEBUG,
438 "Open port %s requested by client %u\n",
439 GNUNET_h2s (&pmsg->port),
440 c->id);
441 if (GNUNET_YES !=
442 GNUNET_CONTAINER_multihashmap_remove (c->ports,
443 &pmsg->port,
444 c))
445 {
446 GNUNET_break (0);
447 GNUNET_SERVICE_client_drop (c->client);
448 return;
449 }
450 GNUNET_assert (GNUNET_YES ==
451 GNUNET_CONTAINER_multihashmap_remove (open_ports,
452 &pmsg->port,
453 c));
454
455 GNUNET_SERVICE_client_continue (c->client);
456}
457
458
459/**
460 * Handler for requests of new channels.
461 *
462 * @param cls Identification of the client.
463 * @param ccm The actual message.
464 */
465static void
466handle_channel_create (void *cls,
467 const struct GNUNET_CADET_ChannelOpenMessageMessage *ccm)
468{
469 struct CadetClient *c = cls;
470 struct CadetChannel *ch;
471 struct GNUNET_CADET_ClientChannelNumber chid;
472 struct CadetPeer *dst;
473
474 chid = ccm->channel_id;
475 if (ntohl (chid.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
476 {
477 /* Channel ID not in allowed range. */
478 GNUNET_break (0);
479 GNUNET_SERVICE_client_drop (c->client);
480 return;
481 }
482 ch = GNUNET_CONTAINER_multihashmap32_get (c->own_channels,
483 ntohl (chid.channel_of_client));
484 if (NULL != ch)
485 {
486 /* Channel ID already in use. Not allowed. */
487 GNUNET_break (0);
488 GNUNET_SERVICE_client_drop (c->client);
489 return;
490 }
491
492 dst = GCP_get (&ccm->peer,
493 GNUNET_YES);
494
495 /* Create channel */
496 ch = GCCH_channel_local_new (c,
497 chid,
498 dst,
499 &ccm->port,
500 ntohl (ccm->opt));
501 if (NULL == ch)
502 {
503 GNUNET_break (0);
504 GNUNET_SERVICE_client_drop (c->client);
505 return;
506 }
507 GNUNET_assert (GNUNET_YES ==
508 GNUNET_CONTAINER_multihashmap32_put (c->own_channels,
509 ntohl (chid.channel_of_client),
510 ch,
511 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
512
513 LOG (GNUNET_ERROR_TYPE_DEBUG,
514 "New channel %s to %s at port %s requested by client %u\n",
515 GCCH_2s (ch),
516 GNUNET_i2s (&ccm->peer),
517 GNUNET_h2s (&ccm->port),
518 c->id);
519 GNUNET_SERVICE_client_continue (c->client);
520}
521
522
523/**
524 * Return the map which we use for client @a c for a channel ID of @a chid
525 *
526 * @param c client to find map for
527 * @param chid chid to find map for
528 * @return applicable map we use
529 */
530static struct GNUNET_CONTAINER_MultiHashMap32 *
531get_map_by_chid (struct CadetClient *c,
532 struct GNUNET_CADET_ClientChannelNumber chid)
533{
534 return (ntohl (chid.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
535 ? c->own_channels
536 : c->incoming_channels;
537}
538
539
540/**
541 * Handler for requests of deleting tunnels
542 *
543 * @param cls client identification of the client
544 * @param msg the actual message
545 */
546static void
547handle_channel_destroy (void *cls,
548 const struct GNUNET_CADET_ChannelDestroyMessage *msg)
549{
550 struct CadetClient *c = cls;
551 struct GNUNET_CADET_ClientChannelNumber chid;
552 struct GNUNET_CONTAINER_MultiHashMap32 *map;
553 struct CadetChannel *ch;
554
555 /* Retrieve tunnel */
556 chid = msg->channel_id;
557 map = get_map_by_chid (c,
558 chid);
559 ch = GNUNET_CONTAINER_multihashmap32_get (map,
560 ntohl (chid.channel_of_client));
561 if (NULL == ch)
562 {
563 /* Client attempted to destroy unknown channel */
564 GNUNET_break (0);
565 GNUNET_SERVICE_client_drop (c->client);
566 return;
567 }
568 LOG (GNUNET_ERROR_TYPE_INFO,
569 "Client %u is destroying channel %s\n",
570 c->id,
571 GCCH_2s (ch));
572 GNUNET_assert (GNUNET_YES ==
573 GNUNET_CONTAINER_multihashmap32_remove (map,
574 ntohl (chid.channel_of_client),
575 ch));
576 GCCH_channel_local_destroy (ch);
577 GNUNET_SERVICE_client_continue (c->client);
578}
579
580
581/**
582 * Check for client traffic data message is well-formed
583 *
584 * @param cls identification of the client
585 * @param msg the actual message
586 * @return #GNUNET_OK if @a msg is OK, #GNUNET_SYSERR if not
587 */
588static int
589check_data (void *cls,
590 const struct GNUNET_CADET_LocalData *msg)
591{
592 const struct GNUNET_MessageHeader *payload;
593 size_t payload_size;
594 size_t payload_claimed_size;
595
596 /* Sanity check for message size */
597 payload_size = ntohs (msg->header.size) - sizeof (*msg);
598 if ( (payload_size < sizeof (struct GNUNET_MessageHeader)) ||
599 (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE < payload_size) )
600 {
601 GNUNET_break (0);
602 return GNUNET_SYSERR;
603 }
604 payload = (struct GNUNET_MessageHeader *) &msg[1];
605 payload_claimed_size = ntohs (payload->size);
606 if (payload_size != payload_claimed_size)
607 {
608 GNUNET_break (0);
609 return GNUNET_SYSERR;
610 }
611 return GNUNET_OK;
612}
613
614
615/**
616 * Handler for client traffic
617 *
618 * @param cls identification of the client
619 * @param msg the actual message
620 */
621static void
622handle_data (void *cls,
623 const struct GNUNET_CADET_LocalData *msg)
624{
625 struct CadetClient *c = cls;
626 struct GNUNET_CONTAINER_MultiHashMap32 *map;
627 struct GNUNET_CADET_ClientChannelNumber chid;
628 struct CadetChannel *ch;
629 const struct GNUNET_MessageHeader *payload;
630
631 chid = msg->id;
632 map = get_map_by_chid (c,
633 chid);
634 ch = GNUNET_CONTAINER_multihashmap32_get (map,
635 ntohl (chid.channel_of_client));
636 if (NULL == ch)
637 {
638 /* Channel does not exist! */
639 GNUNET_break (0);
640 GNUNET_SERVICE_client_drop (c->client);
641 return;
642 }
643
644 payload = (const struct GNUNET_MessageHeader *) &msg[1];
645 LOG (GNUNET_ERROR_TYPE_DEBUG,
646 "Received %u bytes payload from client %u for channel %s\n",
647 ntohs (payload->size),
648 c->id,
649 GCCH_2s (ch));
650 if (GNUNET_OK !=
651 GCCH_handle_local_data (ch,
652 payload))
653 {
654 GNUNET_SERVICE_client_drop (c->client);
655 return;
656 }
657 GNUNET_SERVICE_client_continue (c->client);
658}
659
660
661/**
662 * Handler for client's ACKs for payload traffic.
663 *
664 * @param cls identification of the client.
665 * @param msg The actual message.
666 */
667static void
668handle_ack (void *cls,
669 const struct GNUNET_CADET_LocalAck *msg)
670{
671 struct CadetClient *c = cls;
672 struct GNUNET_CONTAINER_MultiHashMap32 *map;
673 struct GNUNET_CADET_ClientChannelNumber chid;
674 struct CadetChannel *ch;
675
676 chid = msg->channel_id;
677 map = get_map_by_chid (c,
678 chid);
679 ch = GNUNET_CONTAINER_multihashmap32_get (map,
680 ntohl (chid.channel_of_client));
681 if (NULL == ch)
682 {
683 /* Channel does not exist! */
684 GNUNET_break (0);
685 GNUNET_SERVICE_client_drop (c->client);
686 return;
687 }
688 LOG (GNUNET_ERROR_TYPE_DEBUG,
689 "Got a local ACK from client %u for channel %s\n",
690 c->id,
691 GCCH_2s (ch));
692 GCCH_handle_local_ack (ch);
693 GNUNET_SERVICE_client_continue (c->client);
694}
695
696
697/**
698 * Iterator over all peers to send a monitoring client info about each peer.
699 *
700 * @param cls Closure ().
701 * @param peer Peer ID (tunnel remote peer).
702 * @param value Peer info.
703 * @return #GNUNET_YES, to keep iterating.
704 */
705static int
706get_all_peers_iterator (void *cls,
707 const struct GNUNET_PeerIdentity *peer,
708 void *value)
709{
710 struct CadetClient *c = cls;
711 struct CadetPeer *p = value;
712 struct GNUNET_MQ_Envelope *env;
713 struct GNUNET_CADET_LocalInfoPeer *msg;
714
715 env = GNUNET_MQ_msg (msg,
716 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
717 msg->destination = *peer;
718 msg->paths = htons (GCP_count_paths (p));
719 msg->tunnel = htons (NULL != GCP_get_tunnel (p,
720 GNUNET_NO));
721 GNUNET_MQ_send (c->mq,
722 env);
723 return GNUNET_YES;
724}
725
726
727/**
728 * Handler for client's INFO PEERS request.
729 *
730 * @param cls Identification of the client.
731 * @param message The actual message.
732 */
733static void
734handle_get_peers (void *cls,
735 const struct GNUNET_MessageHeader *message)
736{
737 struct CadetClient *c = cls;
738 struct GNUNET_MQ_Envelope *env;
739 struct GNUNET_MessageHeader *reply;
740
741 GCP_iterate_all (&get_all_peers_iterator,
742 c);
743 env = GNUNET_MQ_msg (reply,
744 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
745 GNUNET_MQ_send (c->mq,
746 env);
747 GNUNET_SERVICE_client_continue (c->client);
748}
749
750
751/**
752 * Iterator over all paths of a peer to build an InfoPeer message.
753 * Message contains blocks of peers, first not included.
754 *
755 * @param cls message queue for transmission
756 * @param path Path itself
757 * @param off offset of the peer on @a path
758 * @return #GNUNET_YES if should keep iterating.
759 * #GNUNET_NO otherwise.
760 */
761static int
762path_info_iterator (void *cls,
763 struct CadetPeerPath *path,
764 unsigned int off)
765{
766 struct GNUNET_MQ_Handle *mq = cls;
767 struct GNUNET_MQ_Envelope *env;
768 struct GNUNET_MessageHeader *resp;
769 struct GNUNET_PeerIdentity *id;
770 uint16_t path_size;
771 unsigned int i;
772 unsigned int path_length;
773
774 path_length = GCPP_get_length (path);
775 path_size = sizeof (struct GNUNET_PeerIdentity) * (path_length - 1);
776 if (sizeof (*resp) + path_size > UINT16_MAX)
777 {
778 LOG (GNUNET_ERROR_TYPE_WARNING,
779 "Path of %u entries is too long for info message\n",
780 path_length);
781 return GNUNET_YES;
782 }
783 env = GNUNET_MQ_msg_extra (resp,
784 path_size,
785 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
786 id = (struct GNUNET_PeerIdentity *) &resp[1];
787
788 /* Don't copy first peer. First peer is always the local one. Last
789 * peer is always the destination (leave as 0, EOL).
790 */
791 for (i = 0; i < off; i++)
792 id[i] = *GCP_get_id (GCPP_get_peer_at_offset (path,
793 i + 1));
794 GNUNET_MQ_send (mq,
795 env);
796 return GNUNET_YES;
797}
798
799
800/**
801 * Handler for client's SHOW_PEER request.
802 *
803 * @param cls Identification of the client.
804 * @param msg The actual message.
805 */
806static void
807handle_show_peer (void *cls,
808 const struct GNUNET_CADET_LocalInfo *msg)
809{
810 struct CadetClient *c = cls;
811 struct CadetPeer *p;
812 struct GNUNET_MQ_Envelope *env;
813 struct GNUNET_MessageHeader *resp;
814
815 p = GCP_get (&msg->peer,
816 GNUNET_NO);
817 if (NULL != p)
818 GCP_iterate_paths (p,
819 &path_info_iterator,
820 c->mq);
821 /* Send message with 0/0 to indicate the end */
822 env = GNUNET_MQ_msg (resp,
823 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER_END);
824 GNUNET_MQ_send (c->mq,
825 env);
826 GNUNET_SERVICE_client_continue (c->client);
827}
828
829
830/**
831 * Iterator over all tunnels to send a monitoring client info about each tunnel.
832 *
833 * @param cls Closure ().
834 * @param peer Peer ID (tunnel remote peer).
835 * @param value a `struct CadetPeer`
836 * @return #GNUNET_YES, to keep iterating.
837 */
838static int
839get_all_tunnels_iterator (void *cls,
840 const struct GNUNET_PeerIdentity *peer,
841 void *value)
842{
843 struct CadetClient *c = cls;
844 struct CadetPeer *p = value;
845 struct GNUNET_MQ_Envelope *env;
846 struct GNUNET_CADET_LocalInfoTunnel *msg;
847 struct CadetTunnel *t;
848
849 t = GCP_get_tunnel (p,
850 GNUNET_NO);
851 if (NULL == t)
852 return GNUNET_YES;
853 env = GNUNET_MQ_msg (msg,
854 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
855 msg->destination = *peer;
856 msg->channels = htonl (GCT_count_channels (t));
857 msg->connections = htonl (GCT_count_any_connections (t));
858 msg->cstate = htons ((uint16_t) GCT_get_cstate (t));
859 msg->estate = htons ((uint16_t) GCT_get_estate (t));
860 GNUNET_MQ_send (c->mq,
861 env);
862 return GNUNET_YES;
863}
864
865
866/**
867 * Handler for client's INFO TUNNELS request.
868 *
869 * @param cls client Identification of the client.
870 * @param message The actual message.
871 */
872static void
873handle_get_tunnels (void *cls,
874 const struct GNUNET_MessageHeader *message)
875{
876 struct CadetClient *c = cls;
877 struct GNUNET_MQ_Envelope *env;
878 struct GNUNET_MessageHeader *reply;
879
880 GCP_iterate_all (&get_all_tunnels_iterator,
881 c);
882 env = GNUNET_MQ_msg (reply,
883 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
884 GNUNET_MQ_send (c->mq,
885 env);
886 GNUNET_SERVICE_client_continue (c->client);
887}
888
889
890/**
891 * FIXME.
892 */
893static void
894iter_connection (void *cls,
895 struct CadetConnection *c)
896{
897 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
898 struct GNUNET_CADET_ConnectionTunnelIdentifier *h;
899
900 h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
901 h[msg->connections++] = *(GCC_get_id (c));
902}
903
904
905/**
906 * FIXME.
907 */
908static void
909iter_channel (void *cls,
910 struct CadetChannel *ch)
911{
912 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
913 struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
914 struct GNUNET_CADET_ChannelTunnelNumber *chn
915 = (struct GNUNET_CADET_ChannelTunnelNumber *) &h[msg->connections];
916
917 chn[msg->channels++] = GCCH_get_id (ch);
918}
919
920
921/**
922 * Handler for client's SHOW_TUNNEL request.
923 *
924 * @param cls Identification of the client.
925 * @param msg The actual message.
926 */
927static void
928handle_show_tunnel (void *cls,
929 const struct GNUNET_CADET_LocalInfo *msg)
930{
931 struct CadetClient *c = cls;
932 struct GNUNET_MQ_Envelope *env;
933 struct GNUNET_CADET_LocalInfoTunnel *resp;
934 struct CadetTunnel *t;
935 struct CadetPeer *p;
936 unsigned int ch_n;
937 unsigned int c_n;
938
939 p = GCP_get (&msg->peer,
940 GNUNET_NO);
941 t = GCP_get_tunnel (p,
942 GNUNET_NO);
943 if (NULL == t)
944 {
945 /* We don't know the tunnel */
946 struct GNUNET_MQ_Envelope *env;
947 struct GNUNET_CADET_LocalInfoTunnel *warn;
948
949 LOG (GNUNET_ERROR_TYPE_INFO,
950 "Tunnel to %s unknown\n",
951 GNUNET_i2s_full (&msg->peer));
952 env = GNUNET_MQ_msg (warn,
953 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
954 warn->destination = msg->peer;
955 GNUNET_MQ_send (c->mq,
956 env);
957 GNUNET_SERVICE_client_continue (c->client);
958 return;
959 }
960
961 /* Initialize context */
962 ch_n = GCT_count_channels (t);
963 c_n = GCT_count_any_connections (t);
964 env = GNUNET_MQ_msg_extra (resp,
965 c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier) +
966 ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber),
967 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
968 resp->destination = msg->peer;
969 /* Do not reorder! #iter_channel needs counters in HBO! */
970 GCT_iterate_connections (t,
971 &iter_connection,
972 resp);
973 GCT_iterate_channels (t,
974 &iter_channel,
975 resp);
976 resp->connections = htonl (resp->connections);
977 resp->channels = htonl (resp->channels);
978 resp->cstate = htons (GCT_get_cstate (t));
979 resp->estate = htons (GCT_get_estate (t));
980 GNUNET_MQ_send (c->mq,
981 env);
982 GNUNET_SERVICE_client_continue (c->client);
983}
984
985
986/**
987 * Iterator over all peers to dump info for each peer.
988 *
989 * @param cls Closure (unused).
990 * @param peer Peer ID (tunnel remote peer).
991 * @param value Peer info.
992 *
993 * @return #GNUNET_YES, to keep iterating.
994 */
995static int
996show_peer_iterator (void *cls,
997 const struct GNUNET_PeerIdentity *peer,
998 void *value)
999{
1000 struct CadetPeer *p = value;
1001 struct CadetTunnel *t;
1002
1003 t = GCP_get_tunnel (p,
1004 GNUNET_NO);
1005 if (NULL != t)
1006 GCT_debug (t,
1007 GNUNET_ERROR_TYPE_ERROR);
1008 LOG (GNUNET_ERROR_TYPE_ERROR, "\n");
1009 return GNUNET_YES;
1010}
1011
1012
1013/**
1014 * Handler for client's INFO_DUMP request.
1015 *
1016 * @param cls Identification of the client.
1017 * @param message The actual message.
1018 */
1019static void
1020handle_info_dump (void *cls,
1021 const struct GNUNET_MessageHeader *message)
1022{
1023 struct CadetClient *c = cls;
1024
1025 LOG (GNUNET_ERROR_TYPE_INFO,
1026 "Received dump info request from client %u\n",
1027 c->id);
1028
1029 LOG (GNUNET_ERROR_TYPE_ERROR,
1030 "*************************** DUMP START ***************************\n");
1031 for (struct CadetClient *ci = clients_head; NULL != ci; ci = ci->next)
1032 {
1033 LOG (GNUNET_ERROR_TYPE_ERROR,
1034 "Client %u (%p), handle: %p, ports: %u, own channels: %u, incoming channels: %u\n",
1035 ci->id,
1036 ci,
1037 ci->client,
1038 (NULL != c->ports)
1039 ? GNUNET_CONTAINER_multihashmap_size (ci->ports)
1040 : 0,
1041 GNUNET_CONTAINER_multihashmap32_size (ci->own_channels),
1042 GNUNET_CONTAINER_multihashmap32_size (ci->incoming_channels));
1043 }
1044 LOG (GNUNET_ERROR_TYPE_ERROR, "***************************\n");
1045 GCP_iterate_all (&show_peer_iterator,
1046 NULL);
1047
1048 LOG (GNUNET_ERROR_TYPE_ERROR,
1049 "**************************** DUMP END ****************************\n");
1050
1051 GNUNET_SERVICE_client_continue (c->client);
1052}
1053
1054
1055
1056/**
1057 * Callback called when a client connects to the service.
1058 *
1059 * @param cls closure for the service
1060 * @param client the new client that connected to the service
1061 * @param mq the message queue used to send messages to the client
1062 * @return @a c
1063 */
1064static void *
1065client_connect_cb (void *cls,
1066 struct GNUNET_SERVICE_Client *client,
1067 struct GNUNET_MQ_Handle *mq)
1068{
1069 struct CadetClient *c;
1070
1071 c = GNUNET_new (struct CadetClient);
1072 c->client = client;
1073 c->mq = mq;
1074 c->id = next_client_id++; /* overflow not important: just for debug */
1075 c->own_channels
1076 = GNUNET_CONTAINER_multihashmap32_create (32);
1077 c->incoming_channels
1078 = GNUNET_CONTAINER_multihashmap32_create (32);
1079 GNUNET_CONTAINER_DLL_insert (clients_head,
1080 clients_tail,
1081 c);
1082 GNUNET_STATISTICS_update (stats,
1083 "# clients",
1084 +1,
1085 GNUNET_NO);
1086 return c;
1087}
1088
1089
1090/**
1091 * Iterator for deleting each channel whose client endpoint disconnected.
1092 *
1093 * @param cls Closure (client that has disconnected).
1094 * @param key The local channel id (used to access the hashmap).
1095 * @param value The value stored at the key (channel to destroy).
1096 * @return #GNUNET_OK, keep iterating.
1097 */
1098static int
1099own_channel_destroy_iterator (void *cls,
1100 uint32_t key,
1101 void *value)
1102{
1103 struct CadetClient *c = cls;
1104 struct CadetChannel *ch = value;
1105
1106 GNUNET_assert (GNUNET_YES ==
1107 GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1108 key,
1109 ch));
1110 LOG (GNUNET_ERROR_TYPE_DEBUG,
1111 "Destroying own channel %s, due to client %u shutdown.\n",
1112 GCCH_2s (ch),
1113 c->id);
1114 GCCH_channel_local_destroy (ch);
1115 return GNUNET_OK;
1116}
1117
1118
1119/**
1120 * Iterator for deleting each channel whose client endpoint disconnected.
1121 *
1122 * @param cls Closure (client that has disconnected).
1123 * @param key The local channel id (used to access the hashmap).
1124 * @param value The value stored at the key (channel to destroy).
1125 * @return #GNUNET_OK, keep iterating.
1126 */
1127static int
1128incoming_channel_destroy_iterator (void *cls,
1129 uint32_t key,
1130 void *value)
1131{
1132 struct CadetChannel *ch = value;
1133 struct CadetClient *c = cls;
1134
1135 GNUNET_assert (GNUNET_YES ==
1136 GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1137 key,
1138 ch));
1139
1140 LOG (GNUNET_ERROR_TYPE_DEBUG,
1141 "Destroying incoming channel %s due to client %u shutdown.\n",
1142 GCCH_2s (ch),
1143 c->id);
1144 GCCH_channel_incoming_destroy (ch);
1145 return GNUNET_OK;
1146}
1147
1148
1149/**
1150 * Remove client's ports from the global hashmap on disconnect.
1151 *
1152 * @param cls Closure (unused).
1153 * @param key Port.
1154 * @param value the `struct CadetClient` to remove
1155 * @return #GNUNET_OK, keep iterating.
1156 */
1157static int
1158client_release_ports (void *cls,
1159 const struct GNUNET_HashCode *key,
1160 void *value)
1161{
1162 struct CadetClient *c = value;
1163
1164 GNUNET_assert (GNUNET_YES ==
1165 GNUNET_CONTAINER_multihashmap_remove (open_ports,
1166 key,
1167 value));
1168 GNUNET_assert (GNUNET_YES ==
1169 GNUNET_CONTAINER_multihashmap_remove (c->ports,
1170 key,
1171 value));
1172 return GNUNET_OK;
1173}
1174
1175
1176/**
1177 * Callback called when a client disconnected from the service
1178 *
1179 * @param cls closure for the service
1180 * @param client the client that disconnected
1181 * @param internal_cls should be equal to @a c
1182 */
1183static void
1184client_disconnect_cb (void *cls,
1185 struct GNUNET_SERVICE_Client *client,
1186 void *internal_cls)
1187{
1188 struct CadetClient *c = internal_cls;
1189
1190 GNUNET_assert (c->client == client);
1191 c->shutting_down = GNUNET_YES;
1192 if (NULL != c->own_channels)
1193 {
1194 GNUNET_CONTAINER_multihashmap32_iterate (c->own_channels,
1195 &own_channel_destroy_iterator,
1196 c);
1197 GNUNET_CONTAINER_multihashmap32_destroy (c->own_channels);
1198 }
1199 if (NULL != c->incoming_channels)
1200 {
1201 GNUNET_CONTAINER_multihashmap32_iterate (c->incoming_channels,
1202 &incoming_channel_destroy_iterator,
1203 c);
1204 GNUNET_CONTAINER_multihashmap32_destroy (c->incoming_channels);
1205 }
1206 if (NULL != c->ports)
1207 {
1208 GNUNET_CONTAINER_multihashmap_iterate (c->ports,
1209 &client_release_ports,
1210 c);
1211 GNUNET_CONTAINER_multihashmap_destroy (c->ports);
1212 }
1213 GNUNET_CONTAINER_DLL_remove (clients_head,
1214 clients_tail,
1215 c);
1216 GNUNET_STATISTICS_update (stats,
1217 "# clients",
1218 -1,
1219 GNUNET_NO);
1220 GNUNET_free (c);
1221}
1222
1223
1224/**
1225 * Setup CADET internals.
1226 *
1227 * @param cls closure
1228 * @param server the initialized server
1229 * @param c configuration to use
1230 */
1231static void
1232run (void *cls,
1233 const struct GNUNET_CONFIGURATION_Handle *c,
1234 struct GNUNET_SERVICE_Handle *service)
1235{
1236 if (GNUNET_OK !=
1237 GNUNET_CONFIGURATION_get_value_number (c,
1238 "CADET",
1239 "RATCHET_MESSAGES",
1240 &ratchet_messages))
1241 {
1242 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1243 "CADET",
1244 "RATCHET_MESSAGES",
1245 "needs to be a number");
1246 ratchet_messages = 64;
1247 }
1248 if (GNUNET_OK !=
1249 GNUNET_CONFIGURATION_get_value_time (c,
1250 "CADET",
1251 "RATCHET_TIME",
1252 &ratchet_time))
1253 {
1254 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1255 "CADET",
1256 "RATCHET_TIME",
1257 "need delay value");
1258 ratchet_time = GNUNET_TIME_UNIT_HOURS;
1259 }
1260
1261 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (c);
1262 if (NULL == my_private_key)
1263 {
1264 GNUNET_break (0);
1265 GNUNET_SCHEDULER_shutdown ();
1266 return;
1267 }
1268 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
1269 &my_full_id.public_key);
1270 stats = GNUNET_STATISTICS_create ("cadet",
1271 c);
1272 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1273 NULL);
1274 ats_ch = GNUNET_ATS_connectivity_init (c);
1275 /* FIXME: optimize code to allow GNUNET_YES here! */
1276 open_ports = GNUNET_CONTAINER_multihashmap_create (16,
1277 GNUNET_NO);
1278 loose_channels = GNUNET_CONTAINER_multihashmap_create (16,
1279 GNUNET_NO);
1280 peers = GNUNET_CONTAINER_multipeermap_create (16,
1281 GNUNET_YES);
1282 connections = GNUNET_CONTAINER_multishortmap_create (256,
1283 GNUNET_YES);
1284 GCH_init (c);
1285 GCD_init (c);
1286 GCO_init (c);
1287 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1288 "CADET starting at peer %s\n",
1289 GNUNET_i2s (&my_full_id));
1290
1291}
1292
1293
1294/**
1295 * Define "main" method using service macro.
1296 */
1297GNUNET_SERVICE_MAIN
1298("cadet",
1299 GNUNET_SERVICE_OPTION_NONE,
1300 &run,
1301 &client_connect_cb,
1302 &client_disconnect_cb,
1303 NULL,
1304 GNUNET_MQ_hd_fixed_size (port_open,
1305 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN,
1306 struct GNUNET_CADET_PortMessage,
1307 NULL),
1308 GNUNET_MQ_hd_fixed_size (port_close,
1309 GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1310 struct GNUNET_CADET_PortMessage,
1311 NULL),
1312 GNUNET_MQ_hd_fixed_size (channel_create,
1313 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN,
1314 struct GNUNET_CADET_ChannelOpenMessageMessage,
1315 NULL),
1316 GNUNET_MQ_hd_fixed_size (channel_destroy,
1317 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1318 struct GNUNET_CADET_ChannelDestroyMessage,
1319 NULL),
1320 GNUNET_MQ_hd_var_size (data,
1321 GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA,
1322 struct GNUNET_CADET_LocalData,
1323 NULL),
1324 GNUNET_MQ_hd_fixed_size (ack,
1325 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK,
1326 struct GNUNET_CADET_LocalAck,
1327 NULL),
1328 GNUNET_MQ_hd_fixed_size (get_peers,
1329 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS,
1330 struct GNUNET_MessageHeader,
1331 NULL),
1332 GNUNET_MQ_hd_fixed_size (show_peer,
1333 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER,
1334 struct GNUNET_CADET_LocalInfo,
1335 NULL),
1336 GNUNET_MQ_hd_fixed_size (get_tunnels,
1337 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS,
1338 struct GNUNET_MessageHeader,
1339 NULL),
1340 GNUNET_MQ_hd_fixed_size (show_tunnel,
1341 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL,
1342 struct GNUNET_CADET_LocalInfo,
1343 NULL),
1344 GNUNET_MQ_hd_fixed_size (info_dump,
1345 GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP,
1346 struct GNUNET_MessageHeader,
1347 NULL),
1348 GNUNET_MQ_handler_end ());
1349
1350/* end of gnunet-service-cadet-new.c */
diff --git a/src/cadet/gnunet-service-cadet-new.h b/src/cadet/gnunet-service-cadet-new.h
new file mode 100644
index 000000000..9f4667e23
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new.h
@@ -0,0 +1,264 @@
1
2/*
3 This file is part of GNUnet.
4 Copyright (C) 2001-2017 GNUnet e.V.
5
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
10
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22/**
23 * @file cadet/gnunet-service-cadet-new.h
24 * @brief Information we track per peer.
25 * @author Bartlomiej Polot
26 * @author Christian Grothoff
27 */
28#ifndef GNUNET_SERVICE_CADET_H
29#define GNUNET_SERVICE_CADET_H
30
31#include "gnunet_util_lib.h"
32#define NEW_CADET 1
33
34/**
35 * A client to the CADET service. Each client gets a unique handle.
36 */
37struct CadetClient;
38
39/**
40 * A peer in the GNUnet network. Each peer we care about must have one globally
41 * unique such handle within this process.
42 */
43struct CadetPeer;
44
45/**
46 * Tunnel from us to another peer. There can only be at most one
47 * tunnel per peer.
48 */
49struct CadetTunnel;
50
51/**
52 * Entry in the message queue of a `struct CadetTunnel`.
53 */
54struct CadetTunnelQueueEntry;
55
56/**
57 * A path of peer in the GNUnet network. There must only be at most
58 * once such path. Paths may share disjoint prefixes, but must all
59 * end at a unique suffix. Paths must also not be proper subsets of
60 * other existing paths.
61 */
62struct CadetPeerPath;
63
64/**
65 * Entry in a peer path.
66 */
67struct CadetPeerPathEntry
68{
69 /**
70 * DLL of paths where the same @e peer is at the same offset.
71 */
72 struct CadetPeerPathEntry *next;
73
74 /**
75 * DLL of paths where the same @e peer is at the same offset.
76 */
77 struct CadetPeerPathEntry *prev;
78
79 /**
80 * The peer at this offset of the path.
81 */
82 struct CadetPeer *peer;
83
84 /**
85 * Path this entry belongs to.
86 */
87 struct CadetPeerPath *path;
88
89 /**
90 * Connection using this path, or NULL for none.
91 */
92 struct CadetConnection *cc;
93
94 /**
95 * Path's historic score up to this point. Basically, how often did
96 * we succeed or fail to use the path up to this entry in a
97 * connection. Positive values indicate good experiences, negative
98 * values bad experiences. Code updating the score must guard
99 * against overflows.
100 */
101 int score;
102
103};
104
105/**
106 * Entry in list of connections used by tunnel, with metadata.
107 */
108struct CadetTConnection
109{
110 /**
111 * Next in DLL.
112 */
113 struct CadetTConnection *next;
114
115 /**
116 * Prev in DLL.
117 */
118 struct CadetTConnection *prev;
119
120 /**
121 * Connection handle.
122 */
123 struct CadetConnection *cc;
124
125 /**
126 * Tunnel this connection belongs to.
127 */
128 struct CadetTunnel *t;
129
130 /**
131 * Creation time, to keep oldest connection alive.
132 */
133 struct GNUNET_TIME_Absolute created;
134
135 /**
136 * Connection throughput, to keep fastest connection alive.
137 */
138 uint32_t throughput;
139
140 /**
141 * Is the connection currently ready for transmission?
142 */
143 int is_ready;
144};
145
146
147/**
148 * Active path through the network (used by a tunnel). There may
149 * be at most one connection per path.
150 */
151struct CadetConnection;
152
153/**
154 * Description of a segment of a `struct CadetConnection` at the
155 * intermediate peers. Routes are basically entries in a peer's
156 * routing table for forwarding traffic. At both endpoints, the
157 * routes are terminated by a `struct CadetConnection`, which knows
158 * the complete `struct CadetPath` that is formed by the individual
159 * routes.
160 */
161struct CadetRoute;
162
163/**
164 * Logical end-to-end conenction between clients. There can be
165 * any number of channels between clients.
166 */
167struct CadetChannel;
168
169/**
170 * Handle to the statistics service.
171 */
172extern struct GNUNET_STATISTICS_Handle *stats;
173
174/**
175 * Handle to communicate with ATS.
176 */
177extern struct GNUNET_ATS_ConnectivityHandle *ats_ch;
178
179/**
180 * Local peer own ID.
181 */
182extern struct GNUNET_PeerIdentity my_full_id;
183
184/**
185 * Own private key.
186 */
187extern struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
188
189/**
190 * All ports clients of this peer have opened.
191 */
192extern struct GNUNET_CONTAINER_MultiHashMap *open_ports;
193
194/**
195 * Map from `struct GNUNET_CADET_ConnectionTunnelIdentifier`
196 * hash codes to `struct CadetConnection` objects.
197 */
198extern struct GNUNET_CONTAINER_MultiShortmap *connections;
199
200/**
201 * Map from ports to channels where the ports were closed at the
202 * time we got the inbound connection.
203 * Indexed by port, contains `struct CadetChannel`.
204 */
205extern struct GNUNET_CONTAINER_MultiHashMap *loose_channels;
206
207/**
208 * Map from PIDs to `struct CadetPeer` entries.
209 */
210extern struct GNUNET_CONTAINER_MultiPeerMap *peers;
211
212/**
213 * How many messages are needed to trigger an AXOLOTL ratchet advance.
214 */
215extern unsigned long long ratchet_messages;
216
217/**
218 * How long until we trigger a ratched advance due to time.
219 */
220extern struct GNUNET_TIME_Relative ratchet_time;
221
222
223
224/**
225 * Send a message to a client.
226 *
227 * @param c client to get the message
228 * @param env envelope with the message
229 */
230void
231GSC_send_to_client (struct CadetClient *c,
232 struct GNUNET_MQ_Envelope *env);
233
234
235/**
236 * Bind incoming channel to this client, and notify client
237 * about incoming connection.
238 *
239 * @param c client to bind to
240 * @param ch channel to be bound
241 * @param dest peer that establishes the connection
242 * @param port port number
243 * @param options options
244 * @return local channel number assigned to the new client
245 */
246struct GNUNET_CADET_ClientChannelNumber
247GSC_bind (struct CadetClient *c,
248 struct CadetChannel *ch,
249 struct CadetPeer *dest,
250 const struct GNUNET_HashCode *port,
251 uint32_t options);
252
253
254/**
255 * Return identifier for a client as a string.
256 *
257 * @param c client to identify
258 * @return string for debugging
259 */
260const char *
261GSC_2s (struct CadetClient *c);
262
263
264#endif
diff --git a/src/cadet/gnunet-service-cadet-new_channel.c b/src/cadet/gnunet-service-cadet-new_channel.c
new file mode 100644
index 000000000..5acd098b6
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new_channel.c
@@ -0,0 +1,1077 @@
1
2/*
3 This file is part of GNUnet.
4 Copyright (C) 2001-2017 GNUnet e.V.
5
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
10
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22/**
23 * @file cadet/gnunet-service-cadet-new_channel.c
24 * @brief logical links between CADET clients
25 * @author Bartlomiej Polot
26 * @author Christian Grothoff
27 *
28 * TODO:
29 * - estimate max bandwidth using bursts and use to optimize
30 * transmission rate(s)
31 */
32
33#include "platform.h"
34#include "gnunet_util_lib.h"
35#include "cadet.h"
36#include "gnunet_statistics_service.h"
37#include "gnunet-service-cadet-new.h"
38#include "gnunet-service-cadet-new_channel.h"
39#include "gnunet-service-cadet-new_connection.h"
40#include "gnunet-service-cadet-new_tunnels.h"
41#include "gnunet-service-cadet-new_peer.h"
42#include "gnunet-service-cadet-new_paths.h"
43
44#define LOG(level, ...) GNUNET_log (level,__VA_ARGS__)
45
46/**
47 * How long do we initially wait before retransmitting?
48 */
49#define CADET_INITIAL_RETRANSMIT_TIME GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 250)
50
51/**
52 * How long do we wait before dropping state about incoming
53 * connection to closed port?
54 */
55#define TIMEOUT_CLOSED_PORT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
56
57
58/**
59 * All the states a connection can be in.
60 */
61enum CadetChannelState
62{
63 /**
64 * Uninitialized status, should never appear in operation.
65 */
66 CADET_CHANNEL_NEW,
67
68 /**
69 * Connection create message sent, waiting for ACK.
70 */
71 CADET_CHANNEL_CREATE_SENT,
72
73 /**
74 * Connection confirmed, ready to carry traffic.
75 */
76 CADET_CHANNEL_READY
77};
78
79
80/**
81 * Info needed to retry a message in case it gets lost.
82 * Note that we DO use this structure also for unreliable
83 * messages.
84 */
85struct CadetReliableMessage
86{
87 /**
88 * Double linked list, FIFO style
89 */
90 struct CadetReliableMessage *next;
91
92 /**
93 * Double linked list, FIFO style
94 */
95 struct CadetReliableMessage *prev;
96
97 /**
98 * Which channel is this message in?
99 */
100 struct CadetChannel *ch;
101
102 /**
103 * Entry in the tunnels queue for this message, NULL if it has left
104 * the tunnel. Used to cancel transmission in case we receive an
105 * ACK in time.
106 */
107 struct CadetTunnelQueueEntry *qe;
108
109 /**
110 * How soon should we retry if we fail to get an ACK?
111 * Messages in the queue are sorted by this value.
112 */
113 struct GNUNET_TIME_Absolute next_retry;
114
115 /**
116 * How long do we wait for an ACK after transmission?
117 * Use for the back-off calculation.
118 */
119 struct GNUNET_TIME_Relative retry_delay;
120
121 /**
122 * Data message we are trying to send.
123 */
124 struct GNUNET_CADET_ChannelAppDataMessage data_message;
125
126 /* followed by variable-size payload */
127};
128
129
130/**
131 * List of received out-of-order data messages.
132 */
133struct CadetOutOfOrderMessage
134{
135 /**
136 * Double linked list, FIFO style
137 */
138 struct CadetOutOfOrderMessage *next;
139
140 /**
141 * Double linked list, FIFO style
142 */
143 struct CadetOutOfOrderMessage *prev;
144
145 /**
146 * ID of the message (ACK needed to free)
147 */
148 struct ChannelMessageIdentifier mid;
149
150 /**
151 * The envelope with the payload of the out-of-order message
152 */
153 struct GNUNET_MQ_Envelope *env;
154
155};
156
157
158/**
159 * Struct containing all information regarding a channel to a remote client.
160 */
161struct CadetChannel
162{
163 /**
164 * Tunnel this channel is in.
165 */
166 struct CadetTunnel *t;
167
168 /**
169 * Last entry in the tunnel's queue relating to control messages
170 * (#GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN or
171 * #GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK). Used to cancel
172 * transmission in case we receive updated information.
173 */
174 struct CadetTunnelQueueEntry *last_control_qe;
175
176 /**
177 * Client owner of the tunnel, if any.
178 * (Used if this channel represends the initiating end of the tunnel.)
179 */
180 struct CadetClient *owner;
181
182 /**
183 * Client destination of the tunnel, if any.
184 * (Used if this channel represents the listening end of the tunnel.)
185 */
186 struct CadetClient *dest;
187
188 /**
189 * Head of DLL of messages sent and not yet ACK'd.
190 */
191 struct CadetReliableMessage *head_sent;
192
193 /**
194 * Tail of DLL of messages sent and not yet ACK'd.
195 */
196 struct CadetReliableMessage *tail_sent;
197
198 /**
199 * Head of DLL of messages received out of order or while client was unready.
200 */
201 struct CadetOutOfOrderMessage *head_recv;
202
203 /**
204 * Tail DLL of messages received out of order or while client was unready.
205 */
206 struct CadetOutOfOrderMessage *tail_recv;
207
208 /**
209 * Task to resend/poll in case no ACK is received.
210 */
211 struct GNUNET_SCHEDULER_Task *retry_task;
212
213 /**
214 * Last time the channel was used
215 */
216 struct GNUNET_TIME_Absolute timestamp;
217
218 /**
219 * Destination port of the channel.
220 */
221 struct GNUNET_HashCode port;
222
223 /**
224 * Counter for exponential backoff.
225 */
226 struct GNUNET_TIME_Relative retry_time;
227
228 /**
229 * How long does it usually take to get an ACK.
230 */
231 struct GNUNET_TIME_Relative expected_delay;
232
233 /**
234 * Bitfield of already-received messages past @e mid_recv.
235 */
236 uint64_t mid_futures;
237
238 /**
239 * Next MID expected for incoming traffic.
240 */
241 struct ChannelMessageIdentifier mid_recv;
242
243 /**
244 * Next MID to use for outgoing traffic.
245 */
246 struct ChannelMessageIdentifier mid_send;
247
248 /**
249 * Total (reliable) messages pending ACK for this channel.
250 */
251 unsigned int pending_messages;
252
253 /**
254 * Maximum (reliable) messages pending ACK for this channel
255 * before we throttle the client.
256 */
257 unsigned int max_pending_messages;
258
259 /**
260 * Number identifying this channel in its tunnel.
261 */
262 struct GNUNET_CADET_ChannelTunnelNumber gid;
263
264 /**
265 * Local tunnel number for local client owning the channel.
266 * ( >= #GNUNET_CADET_LOCAL_CHANNEL_ID_CLI or 0 )
267 */
268 struct GNUNET_CADET_ClientChannelNumber lid;
269
270 /**
271 * Channel state.
272 */
273 enum CadetChannelState state;
274
275 /**
276 * Can we send data to the client?
277 */
278 int client_ready;
279
280 /**
281 * Can the client send data to us?
282 */
283 int client_allowed;
284
285 /**
286 * Is the tunnel bufferless (minimum latency)?
287 */
288 int nobuffer;
289
290 /**
291 * Is the tunnel reliable?
292 */
293 int reliable;
294
295 /**
296 * Is the tunnel out-of-order?
297 */
298 int out_of_order;
299
300 /**
301 * Flag to signal the destruction of the channel. If this is set to
302 * #GNUNET_YES the channel will be destroyed once the queue is
303 * empty.
304 */
305 int destroy;
306
307};
308
309
310
311/**
312 * Get the static string for identification of the channel.
313 *
314 * @param ch Channel.
315 *
316 * @return Static string with the channel IDs.
317 */
318const char *
319GCCH_2s (const struct CadetChannel *ch)
320{
321 static char buf[128];
322
323 if (NULL == ch)
324 return "(NULL Channel)";
325 GNUNET_snprintf (buf,
326 sizeof (buf),
327 "%s:%s gid:%X (%X)",
328 GCT_2s (ch->t),
329 GNUNET_h2s (&ch->port),
330 ch->gid,
331 ntohl (ch->lid.channel_of_client));
332 return buf;
333}
334
335
336/**
337 * Get the channel's public ID.
338 *
339 * @param ch Channel.
340 *
341 * @return ID used to identify the channel with the remote peer.
342 */
343struct GNUNET_CADET_ChannelTunnelNumber
344GCCH_get_id (const struct CadetChannel *ch)
345{
346 return ch->gid;
347}
348
349
350/**
351 * Destroy the given channel.
352 *
353 * @param ch channel to destroy
354 */
355static void
356channel_destroy (struct CadetChannel *ch)
357{
358 struct CadetReliableMessage *crm;
359 struct CadetOutOfOrderMessage *com;
360
361 while (NULL != (crm = ch->head_sent))
362 {
363 GNUNET_assert (ch == crm->ch);
364 if (NULL != crm->qe)
365 {
366 GCT_send_cancel (crm->qe);
367 crm->qe = NULL;
368 }
369 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
370 ch->tail_sent,
371 crm);
372 GNUNET_free (crm);
373 }
374 while (NULL != (com = ch->head_recv))
375 {
376 GNUNET_CONTAINER_DLL_remove (ch->head_recv,
377 ch->tail_recv,
378 com);
379 GNUNET_MQ_discard (com->env);
380 GNUNET_free (com);
381 }
382 if (NULL != ch->last_control_qe)
383 {
384 GCT_send_cancel (ch->last_control_qe);
385 ch->last_control_qe = NULL;
386 }
387 if (NULL != ch->retry_task)
388 {
389 GNUNET_SCHEDULER_cancel (ch->retry_task);
390 ch->retry_task = NULL;
391 }
392 GCT_remove_channel (ch->t,
393 ch,
394 ch->gid);
395 GNUNET_free (ch);
396}
397
398
399/**
400 * Send a channel create message.
401 *
402 * @param cls Channel for which to send.
403 */
404static void
405send_create (void *cls);
406
407
408/**
409 * Function called once the tunnel confirms that we sent the
410 * create message. Delays for a bit until we retry.
411 *
412 * @param cls our `struct CadetChannel`.
413 */
414static void
415create_sent_cb (void *cls)
416{
417 struct CadetChannel *ch = cls;
418
419 ch->last_control_qe = NULL;
420 ch->retry_time = GNUNET_TIME_STD_BACKOFF (ch->retry_time);
421 ch->retry_task = GNUNET_SCHEDULER_add_delayed (ch->retry_time,
422 &send_create,
423 ch);
424}
425
426
427/**
428 * Send a channel create message.
429 *
430 * @param cls Channel for which to send.
431 */
432static void
433send_create (void *cls)
434{
435 struct CadetChannel *ch = cls;
436 struct GNUNET_CADET_ChannelOpenMessage msgcc;
437 uint32_t options;
438
439 options = 0;
440 if (ch->nobuffer)
441 options |= GNUNET_CADET_OPTION_NOBUFFER;
442 if (ch->reliable)
443 options |= GNUNET_CADET_OPTION_RELIABLE;
444 if (ch->out_of_order)
445 options |= GNUNET_CADET_OPTION_OUT_OF_ORDER;
446 msgcc.header.size = htons (sizeof (msgcc));
447 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN);
448 msgcc.opt = htonl (options);
449 msgcc.port = ch->port;
450 msgcc.chid = ch->gid;
451 ch->state = CADET_CHANNEL_CREATE_SENT;
452 ch->last_control_qe = GCT_send (ch->t,
453 &msgcc.header,
454 &create_sent_cb,
455 ch);
456}
457
458
459/**
460 * Create a new channel.
461 *
462 * @param owner local client owning the channel
463 * @param owner_id local chid of this channel at the @a owner
464 * @param destination peer to which we should build the channel
465 * @param port desired port at @a destination
466 * @param options options for the channel
467 * @return handle to the new channel
468 */
469struct CadetChannel *
470GCCH_channel_local_new (struct CadetClient *owner,
471 struct GNUNET_CADET_ClientChannelNumber owner_id,
472 struct CadetPeer *destination,
473 const struct GNUNET_HashCode *port,
474 uint32_t options)
475{
476 struct CadetChannel *ch;
477
478 ch = GNUNET_new (struct CadetChannel);
479 ch->max_pending_messages = 32; /* FIXME: allow control via options
480 or adjust dynamically... */
481 ch->owner = owner;
482 ch->lid = owner_id;
483 ch->port = *port;
484 ch->t = GCP_get_tunnel (destination,
485 GNUNET_YES);
486 ch->gid = GCT_add_channel (ch->t,
487 ch);
488 ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME;
489 ch->nobuffer = (0 != (options & GNUNET_CADET_OPTION_NOBUFFER));
490 ch->reliable = (0 != (options & GNUNET_CADET_OPTION_RELIABLE));
491 ch->out_of_order = (0 != (options & GNUNET_CADET_OPTION_OUT_OF_ORDER));
492 ch->retry_task = GNUNET_SCHEDULER_add_now (&send_create,
493 ch);
494 GNUNET_STATISTICS_update (stats,
495 "# channels",
496 1,
497 GNUNET_NO);
498 return ch;
499}
500
501
502/**
503 * We had an incoming channel to a port that is closed.
504 * It has not been opened for a while, drop it.
505 *
506 * @param cls the channel to drop
507 */
508static void
509timeout_closed_cb (void *cls)
510{
511 struct CadetChannel *ch = cls;
512
513 ch->retry_task = NULL;
514 channel_destroy (ch);
515}
516
517
518/**
519 * Create a new channel.
520 *
521 * @param t tunnel to the remote peer
522 * @param gid identifier of this channel in the tunnel
523 * @param port desired local port
524 * @param options options for the channel
525 * @return handle to the new channel
526 */
527struct CadetChannel *
528GCCH_channel_incoming_new (struct CadetTunnel *t,
529 struct GNUNET_CADET_ChannelTunnelNumber gid,
530 const struct GNUNET_HashCode *port,
531 uint32_t options)
532{
533 struct CadetChannel *ch;
534 struct CadetClient *c;
535
536 ch = GNUNET_new (struct CadetChannel);
537 ch->max_pending_messages = 32; /* FIXME: allow control via options
538 or adjust dynamically... */
539 ch->port = *port;
540 ch->t = t;
541 ch->gid = gid;
542 ch->retry_time = CADET_INITIAL_RETRANSMIT_TIME;
543 ch->nobuffer = (0 != (options & GNUNET_CADET_OPTION_NOBUFFER));
544 ch->reliable = (0 != (options & GNUNET_CADET_OPTION_RELIABLE));
545 ch->out_of_order = (0 != (options & GNUNET_CADET_OPTION_OUT_OF_ORDER));
546 GNUNET_STATISTICS_update (stats,
547 "# channels",
548 1,
549 GNUNET_NO);
550
551 c = GNUNET_CONTAINER_multihashmap_get (open_ports,
552 port);
553 if (NULL == c)
554 {
555 /* port closed, wait for it to possibly open */
556 (void) GNUNET_CONTAINER_multihashmap_put (loose_channels,
557 port,
558 ch,
559 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
560 ch->retry_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT_CLOSED_PORT,
561 &timeout_closed_cb,
562 ch);
563 }
564 else
565 {
566 GCCH_bind (ch,
567 c);
568 }
569 GNUNET_STATISTICS_update (stats,
570 "# channels",
571 1,
572 GNUNET_NO);
573 return ch;
574}
575
576
577/**
578 * Function called once the tunnel confirms that we sent the
579 * ACK message. Just remembers it was sent, we do not expect
580 * ACKs for ACKs ;-).
581 *
582 * @param cls our `struct CadetChannel`.
583 */
584static void
585send_ack_cb (void *cls)
586{
587 struct CadetChannel *ch = cls;
588
589 ch->last_control_qe = NULL;
590}
591
592
593/**
594 * Compute and send the current ACK to the other peer.
595 *
596 * @param ch channel to send the ACK for
597 */
598static void
599send_channel_ack (struct CadetChannel *ch)
600{
601 struct GNUNET_CADET_ChannelDataAckMessage msg;
602
603 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK);
604 msg.header.size = htons (sizeof (msg));
605 msg.gid = ch->gid;
606 msg.mid.mid = htonl (ntohl (ch->mid_recv.mid) - 1);
607 msg.futures = GNUNET_htonll (ch->mid_futures);
608 if (NULL != ch->last_control_qe)
609 GCT_send_cancel (ch->last_control_qe);
610 ch->last_control_qe = GCT_send (ch->t,
611 &msg.header,
612 &send_ack_cb,
613 ch);
614}
615
616
617/**
618 * Send our initial ACK to the client confirming that the
619 * connection is up.
620 *
621 * @param cls the `struct CadetChannel`
622 */
623static void
624send_connect_ack (void *cls)
625{
626 struct CadetChannel *ch = cls;
627
628 ch->retry_task = NULL;
629 send_channel_ack (ch);
630}
631
632
633/**
634 * A client is bound to the port that we have a channel
635 * open to. Send the acknowledgement for the connection
636 * request and establish the link with the client.
637 *
638 * @param ch open incoming channel
639 * @param c client listening on the respective port
640 */
641void
642GCCH_bind (struct CadetChannel *ch,
643 struct CadetClient *c)
644{
645 uint32_t options;
646
647 if (NULL != ch->retry_task)
648 {
649 /* there might be a timeout task here */
650 GNUNET_SCHEDULER_cancel (ch->retry_task);
651 ch->retry_task = NULL;
652 }
653 options = 0;
654 if (ch->nobuffer)
655 options |= GNUNET_CADET_OPTION_NOBUFFER;
656 if (ch->reliable)
657 options |= GNUNET_CADET_OPTION_RELIABLE;
658 if (ch->out_of_order)
659 options |= GNUNET_CADET_OPTION_OUT_OF_ORDER;
660 ch->dest = c;
661 ch->lid = GSC_bind (c,
662 ch,
663 GCT_get_destination (ch->t),
664 &ch->port,
665 options);
666 ch->mid_recv.mid = htonl (1); /* The CONNECT counts as message 0! */
667
668 /* notify other peer that we accepted the connection */
669 ch->retry_task = GNUNET_SCHEDULER_add_now (&send_connect_ack,
670 ch);
671}
672
673
674/**
675 * Destroy locally created channel. Called by the
676 * local client, so no need to tell the client.
677 *
678 * @param ch channel to destroy
679 */
680void
681GCCH_channel_local_destroy (struct CadetChannel *ch)
682{
683 if (GNUNET_YES == ch->destroy)
684 {
685 /* other end already destroyed, with the local client gone, no need
686 to finish transmissions, just destroy immediately. */
687 channel_destroy (ch);
688 return;
689 }
690 if (NULL != ch->head_sent)
691 {
692 /* allow send queue to train first */
693 ch->destroy = GNUNET_YES;
694 return;
695 }
696 /* Nothing left to do, just finish destruction */
697 channel_destroy (ch);
698}
699
700
701/**
702 * Destroy channel that was incoming. Called by the
703 * local client, so no need to tell the client.
704 *
705 * @param ch channel to destroy
706 */
707void
708GCCH_channel_incoming_destroy (struct CadetChannel *ch)
709{
710 if (GNUNET_YES == ch->destroy)
711 {
712 /* other end already destroyed, with the remote client gone, no need
713 to finish transmissions, just destroy immediately. */
714 channel_destroy (ch);
715 return;
716 }
717 if (NULL != ch->head_recv)
718 {
719 /* allow local client to see all data first */
720 ch->destroy = GNUNET_YES;
721 return;
722 }
723 /* Nothing left to do, just finish destruction */
724 channel_destroy (ch);
725}
726
727
728/**
729 * Destroy channel, based on the other peer closing the
730 * connection. Also needs to remove this channel from
731 * the tunnel.
732 *
733 * FIXME: need to make it possible to defer destruction until we have
734 * received all messages up to the destroy, and right now the destroy
735 * message (and this API) fails to give is the information we need!
736 *
737 * FIXME: also need to know if the other peer got a destroy from
738 * us before!
739 *
740 * @param ch channel to destroy
741 */
742void
743GCCH_channel_remote_destroy (struct CadetChannel *ch)
744{
745 GNUNET_break (0); // FIXME!
746}
747
748
749/**
750 * Function called once the tunnel has sent one of our messages.
751 * If the message is unreliable, simply frees the `crm`. If the
752 * message was reliable, calculate retransmission time and
753 * wait for ACK (or retransmit).
754 *
755 * @param cls the `struct CadetReliableMessage` that was sent
756 */
757static void
758data_sent_cb (void *cls);
759
760
761/**
762 * We need to retry a transmission, the last one took too long to
763 * be acknowledged.
764 *
765 * @param cls the `struct CadetChannel` where we need to retransmit
766 */
767static void
768retry_transmission (void *cls)
769{
770 struct CadetChannel *ch = cls;
771 struct CadetReliableMessage *crm = ch->head_sent;
772
773 GNUNET_assert (NULL == crm->qe);
774 crm->qe = GCT_send (ch->t,
775 &crm->data_message.header,
776 &data_sent_cb,
777 crm);
778}
779
780
781/**
782 * Check if we can now allow the client to transmit, and if so,
783 * let the client know about it.
784 *
785 * @param ch channel to check
786 */
787static void
788GCCH_check_allow_client (struct CadetChannel *ch)
789{
790 struct GNUNET_MQ_Envelope *env;
791 struct GNUNET_CADET_LocalAck *msg;
792
793 if (GNUNET_YES == ch->client_allowed)
794 return; /* client already allowed! */
795 if (CADET_CHANNEL_READY != ch->state)
796 {
797 /* destination did not yet ACK our CREATE! */
798 LOG (GNUNET_ERROR_TYPE_DEBUG,
799 "Channel %s not yet ready, throttling client until ACK.\n",
800 GCCH_2s (ch));
801 return;
802 }
803 if (ch->pending_messages > ch->max_pending_messages)
804 {
805 /* Too many messages in queue. */
806 LOG (GNUNET_ERROR_TYPE_DEBUG,
807 "Message queue still too long on channel %s, throttling client until ACK.\n",
808 GCCH_2s (ch));
809 return;
810 }
811 if ( (NULL != ch->head_sent) &&
812 (64 <= ntohl (ch->mid_send.mid) - ntohl (ch->head_sent->data_message.mid.mid)) )
813 {
814 LOG (GNUNET_ERROR_TYPE_DEBUG,
815 "Gap in ACKs too big on channel %s, throttling client until ACK.\n",
816 GCCH_2s (ch));
817 return;
818 }
819 ch->client_allowed = GNUNET_YES;
820
821
822 LOG (GNUNET_ERROR_TYPE_DEBUG,
823 "Sending local ack to channel %s client\n",
824 GCCH_2s (ch));
825 env = GNUNET_MQ_msg (msg,
826 GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
827 msg->channel_id = ch->lid;
828 GSC_send_to_client (ch->owner ? ch->owner : ch->dest,
829 env);
830}
831
832
833/**
834 * Function called once the tunnel has sent one of our messages.
835 * If the message is unreliable, simply frees the `crm`. If the
836 * message was reliable, calculate retransmission time and
837 * wait for ACK (or retransmit).
838 *
839 * @param cls the `struct CadetReliableMessage` that was sent
840 */
841static void
842data_sent_cb (void *cls)
843{
844 struct CadetReliableMessage *crm = cls;
845 struct CadetChannel *ch = crm->ch;
846 struct CadetReliableMessage *off;
847
848 crm->qe = NULL;
849 GNUNET_CONTAINER_DLL_remove (ch->head_sent,
850 ch->tail_sent,
851 crm);
852 if (GNUNET_NO == ch->reliable)
853 {
854 GNUNET_free (crm);
855 ch->pending_messages--;
856 GCCH_check_allow_client (ch);
857 return;
858 }
859 if (0 == crm->retry_delay.rel_value_us)
860 crm->retry_delay = ch->expected_delay;
861 crm->next_retry = GNUNET_TIME_relative_to_absolute (crm->retry_delay);
862
863 /* find position for re-insertion into the DLL */
864 if ( (NULL == ch->head_sent) ||
865 (crm->next_retry.abs_value_us < ch->head_sent->next_retry.abs_value_us) )
866 {
867 /* insert at HEAD, also (re)schedule retry task! */
868 GNUNET_CONTAINER_DLL_insert (ch->head_sent,
869 ch->tail_sent,
870 crm);
871 if (NULL != ch->retry_task)
872 GNUNET_SCHEDULER_cancel (ch->retry_task);
873 ch->retry_task = GNUNET_SCHEDULER_add_delayed (crm->retry_delay,
874 &retry_transmission,
875 ch);
876 return;
877 }
878 for (off = ch->head_sent; NULL != off; off = off->next)
879 if (crm->next_retry.abs_value_us < off->next_retry.abs_value_us)
880 break;
881 if (NULL == off)
882 {
883 /* insert at tail */
884 GNUNET_CONTAINER_DLL_insert_tail (ch->head_sent,
885 ch->tail_sent,
886 crm);
887 }
888 else
889 {
890 /* insert before off */
891 GNUNET_CONTAINER_DLL_insert_after (ch->head_sent,
892 ch->tail_sent,
893 off->prev,
894 crm);
895 }
896}
897
898
899/**
900 * Handle data given by a client.
901 *
902 * Check whether the client is allowed to send in this tunnel, save if
903 * channel is reliable and send an ACK to the client if there is still
904 * buffer space in the tunnel.
905 *
906 * @param ch Channel.
907 * @param message payload to transmit.
908 * @return #GNUNET_OK if everything goes well,
909 * #GNUNET_SYSERR in case of an error.
910 */
911int
912GCCH_handle_local_data (struct CadetChannel *ch,
913 const struct GNUNET_MessageHeader *message)
914{
915 uint16_t payload_size = ntohs (message->size);
916 struct CadetReliableMessage *crm;
917
918 if (GNUNET_NO == ch->client_allowed)
919 {
920 GNUNET_break_op (0);
921 return GNUNET_SYSERR;
922 }
923 ch->client_allowed = GNUNET_NO;
924 ch->pending_messages++;
925
926 /* Everything is correct, send the message. */
927 crm = GNUNET_malloc (sizeof (*crm) + payload_size);
928 crm->ch = ch;
929 crm->data_message.header.size = htons (sizeof (struct GNUNET_CADET_ChannelAppDataMessage) + payload_size);
930 crm->data_message.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA);
931 ch->mid_send.mid = htonl (ntohl (ch->mid_send.mid) + 1);
932 crm->data_message.mid = ch->mid_send;
933 crm->data_message.gid = ch->gid;
934 GNUNET_memcpy (&crm[1],
935 message,
936 payload_size);
937 GNUNET_CONTAINER_DLL_insert (ch->head_sent,
938 ch->tail_sent,
939 crm);
940 LOG (GNUNET_ERROR_TYPE_DEBUG,
941 "Sending %u bytes from local client to channel %s\n",
942 payload_size,
943 GCCH_2s (ch));
944 crm->qe = GCT_send (ch->t,
945 &crm->data_message.header,
946 &data_sent_cb,
947 crm);
948 GCCH_check_allow_client (ch);
949 return GNUNET_OK;
950}
951
952
953/**
954 * Try to deliver messages to the local client, if it is ready for more.
955 *
956 * @param ch channel to process
957 */
958static void
959send_client_buffered_data (struct CadetChannel *ch)
960{
961 struct CadetOutOfOrderMessage *com;
962
963 if (GNUNET_NO == ch->client_ready)
964 return; /* client not ready */
965 com = ch->head_recv;
966 if (NULL == com)
967 return; /* none pending */
968 if ( (com->mid.mid != ch->mid_recv.mid) &&
969 (GNUNET_NO == ch->out_of_order) )
970 return; /* missing next one in-order */
971
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973 "Passing payload message to client on channel %s\n",
974 GCCH_2s (ch));
975
976 /* all good, pass next message to client */
977 GNUNET_CONTAINER_DLL_remove (ch->head_recv,
978 ch->tail_recv,
979 com);
980 ch->mid_recv.mid = htonl (1 + ntohl (com->mid.mid));
981 ch->mid_futures >>= 1; /* equivalent to division by 2 */
982 GSC_send_to_client (ch->owner ? ch->owner : ch->dest,
983 com->env);
984 GNUNET_free (com);
985 if ( (0xFFULL == (ch->mid_futures & 0xFFULL)) &&
986 (GNUNET_YES == ch->reliable) )
987 {
988 /* The next 15 messages were also already received (0xFF), this
989 suggests that the sender may be blocked on flow control
990 urgently waiting for an ACK from us. (As we have an inherent
991 maximum of 64 bits, and 15 is getting too close for comfort.)
992 So we should send one now. */
993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
994 "Sender on channel %s likely blocked on flow-control, sending ACK now.\n",
995 GCCH_2s (ch));
996 if (GNUNET_YES == ch->reliable)
997 send_channel_ack (ch);
998 }
999
1000 if (NULL != ch->head_recv)
1001 return;
1002 if (GNUNET_NO == ch->destroy)
1003 return;
1004 channel_destroy (ch);
1005}
1006
1007
1008/**
1009 * Handle ACK from client on local channel.
1010 *
1011 * @param ch channel to destroy
1012 */
1013void
1014GCCH_handle_local_ack (struct CadetChannel *ch)
1015{
1016 ch->client_ready = GNUNET_YES;
1017 send_client_buffered_data (ch);
1018}
1019
1020
1021#define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-chn",__VA_ARGS__)
1022
1023
1024/**
1025 * Log channel info.
1026 *
1027 * @param ch Channel.
1028 * @param level Debug level to use.
1029 */
1030void
1031GCCH_debug (struct CadetChannel *ch,
1032 enum GNUNET_ErrorType level)
1033{
1034 int do_log;
1035
1036 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
1037 "cadet-chn",
1038 __FILE__, __FUNCTION__, __LINE__);
1039 if (0 == do_log)
1040 return;
1041
1042 if (NULL == ch)
1043 {
1044 LOG2 (level, "CHN *** DEBUG NULL CHANNEL ***\n");
1045 return;
1046 }
1047 LOG2 (level,
1048 "CHN Channel %s:%X (%p)\n",
1049 GCT_2s (ch->t),
1050 ch->gid,
1051 ch);
1052 if (NULL != ch->owner)
1053 {
1054 LOG2 (level,
1055 "CHN origin %s ready %s local-id: %u\n",
1056 GSC_2s (ch->owner),
1057 ch->client_ready ? "YES" : "NO",
1058 ntohl (ch->lid.channel_of_client));
1059 }
1060 if (NULL != ch->dest)
1061 {
1062 LOG2 (level,
1063 "CHN destination %s ready %s local-id: %u\n",
1064 GSC_2s (ch->dest),
1065 ch->client_ready ? "YES" : "NO",
1066 ntohl (ch->lid.channel_of_client));
1067 }
1068 LOG2 (level,
1069 "CHN Message IDs recv: %d (%LLX), send: %d\n",
1070 ntohl (ch->mid_recv.mid),
1071 (unsigned long long) ch->mid_futures,
1072 ntohl (ch->mid_send.mid));
1073}
1074
1075
1076
1077/* end of gnunet-service-cadet-new_channel.c */
diff --git a/src/cadet/gnunet-service-cadet-new_channel.h b/src/cadet/gnunet-service-cadet-new_channel.h
new file mode 100644
index 000000000..8caa254d1
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new_channel.h
@@ -0,0 +1,191 @@
1
2/*
3 This file is part of GNUnet.
4 Copyright (C) 2001-2017 GNUnet e.V.
5
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
10
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22/**
23 * @file cadet/gnunet-service-cadet-new_channel.h
24 * @brief GNUnet CADET service with encryption
25 * @author Bartlomiej Polot
26 * @author Christian Grothoff
27 */
28#ifndef GNUNET_SERVICE_CADET_CHANNEL_H
29#define GNUNET_SERVICE_CADET_CHANNEL_H
30
31#include "gnunet-service-cadet-new.h"
32#include "gnunet-service-cadet-new_peer.h"
33
34
35/**
36 * A channel is a bidirectional connection between two CADET
37 * clients. Communiation can be reliable, unreliable, in-order
38 * or out-of-order. One client is the "local" client, this
39 * one initiated the connection. The other client is the
40 * "incoming" client, this one listened on a port to accept
41 * the connection from the "local" client.
42 */
43struct CadetChannel;
44
45
46/**
47 * Get the static string for identification of the channel.
48 *
49 * @param ch Channel.
50 *
51 * @return Static string with the channel IDs.
52 */
53const char *
54GCCH_2s (const struct CadetChannel *ch);
55
56
57/**
58 * Log channel info.
59 *
60 * @param ch Channel.
61 * @param level Debug level to use.
62 */
63void
64GCCH_debug (struct CadetChannel *ch,
65 enum GNUNET_ErrorType level);
66
67
68/**
69 * Get the channel's public ID.
70 *
71 * @param ch Channel.
72 *
73 * @return ID used to identify the channel with the remote peer.
74 */
75struct GNUNET_CADET_ChannelTunnelNumber
76GCCH_get_id (const struct CadetChannel *ch);
77
78
79/**
80 * Create a new channel.
81 *
82 * @param owner local client owning the channel
83 * @param owner_id local chid of this channel at the @a owner
84 * @param destination peer to which we should build the channel
85 * @param port desired port at @a destination
86 * @param options options for the channel
87 * @return handle to the new channel
88 */
89struct CadetChannel *
90GCCH_channel_local_new (struct CadetClient *owner,
91 struct GNUNET_CADET_ClientChannelNumber owner_id,
92 struct CadetPeer *destination,
93 const struct GNUNET_HashCode *port,
94 uint32_t options);
95
96
97/**
98 * A client is bound to the port that we have a channel
99 * open to. Send the acknowledgement for the connection
100 * request and establish the link with the client.
101 *
102 * @param ch open incoming channel
103 * @param c client listening on the respective port
104 */
105void
106GCCH_bind (struct CadetChannel *ch,
107 struct CadetClient *c);
108
109
110
111/**
112 * Destroy locally created channel. Called by the
113 * local client, so no need to tell the client.
114 *
115 * @param ch channel to destroy
116 */
117void
118GCCH_channel_local_destroy (struct CadetChannel *ch);
119
120
121/**
122 * Create a new channel.
123 *
124 * @param t tunnel to the remote peer
125 * @param gid identifier of this channel in the tunnel
126 * @param origin peer to who initiated the channel
127 * @param port desired local port
128 * @param options options for the channel
129 * @return handle to the new channel
130 */
131struct CadetChannel *
132GCCH_channel_incoming_new (struct CadetTunnel *t,
133 struct GNUNET_CADET_ChannelTunnelNumber gid,
134 const struct GNUNET_HashCode *port,
135 uint32_t options);
136
137
138/**
139 * Destroy channel that was incoming. Called by the
140 * local client, so no need to tell the client.
141 *
142 * @param ch channel to destroy
143 */
144void
145GCCH_channel_incoming_destroy (struct CadetChannel *ch);
146
147
148/**
149 * Destroy channel, based on the other peer closing the
150 * connection. Also needs to remove this channel from
151 * the tunnel.
152 *
153 * FIXME: need to make it possible to defer destruction until we have
154 * received all messages up to the destroy, and right now the destroy
155 * message (and this API) fails to give is the information we need!
156 *
157 * FIXME: also need to know if the other peer got a destroy from
158 * us before!
159 *
160 * @param ch channel to destroy
161 */
162void
163GCCH_channel_remote_destroy (struct CadetChannel *ch);
164
165
166/**
167 * Handle data given by a client.
168 *
169 * Check whether the client is allowed to send in this tunnel, save if
170 * channel is reliable and send an ACK to the client if there is still
171 * buffer space in the tunnel.
172 *
173 * @param ch Channel.
174 * @param message payload to transmit.
175 * @return #GNUNET_OK if everything goes well,
176 * #GNUNET_SYSERR in case of an error.
177 */
178int
179GCCH_handle_local_data (struct CadetChannel *ch,
180 const struct GNUNET_MessageHeader *message);
181
182
183/**
184 * Handle ACK from client on local channel.
185 *
186 * @param ch channel to destroy
187 */
188void
189GCCH_handle_local_ack (struct CadetChannel *ch);
190
191#endif
diff --git a/src/cadet/gnunet-service-cadet-new_connection.c b/src/cadet/gnunet-service-cadet-new_connection.c
new file mode 100644
index 000000000..bf88d78e1
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new_connection.c
@@ -0,0 +1,665 @@
1
2/*
3 This file is part of GNUnet.
4 Copyright (C) 2001-2017 GNUnet e.V.
5
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
10
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22/**
23 * @file cadet/gnunet-service-cadet-new_connection.c
24 * @brief management of CORE-level end-to-end connections; establishes
25 * end-to-end routes and transmits messages along the route
26 * @author Bartlomiej Polot
27 * @author Christian Grothoff
28 *
29 * TODO:
30 * - keepalive messages
31 * - keep performance metrics (?)
32 */
33#include "platform.h"
34#include "gnunet-service-cadet-new_channel.h"
35#include "gnunet-service-cadet-new_connection.h"
36#include "gnunet-service-cadet-new_paths.h"
37#include "gnunet-service-cadet-new_peer.h"
38#include "gnunet-service-cadet-new_tunnels.h"
39#include "gnunet_cadet_service.h"
40#include "cadet_protocol.h"
41
42
43/**
44 * All the states a connection can be in.
45 */
46enum CadetConnectionState
47{
48 /**
49 * Uninitialized status, we have not yet even gotten the message queue.
50 */
51 CADET_CONNECTION_NEW,
52
53 /**
54 * Connection create message in queue, awaiting transmission by CORE.
55 */
56 CADET_CONNECTION_SENDING_CREATE,
57
58 /**
59 * Connection create message sent, waiting for ACK.
60 */
61 CADET_CONNECTION_SENT,
62
63 /**
64 * We are an inbound connection, and received a CREATE. Need to
65 * send an CREATE_ACK back.
66 */
67 CADET_CONNECTION_CREATE_RECEIVED,
68
69 /**
70 * Connection confirmed, ready to carry traffic.
71 */
72 CADET_CONNECTION_READY
73
74};
75
76
77/**
78 * Low-level connection to a destination.
79 */
80struct CadetConnection
81{
82
83 /**
84 * ID of the connection.
85 */
86 struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
87
88 /**
89 * To which peer does this connection go?
90 */
91 struct CadetPeer *destination;
92
93 /**
94 * Which tunnel is using this connection?
95 */
96 struct CadetTConnection *ct;
97
98 /**
99 * Path we are using to our destination.
100 */
101 struct CadetPeerPath *path;
102
103 /**
104 * Pending message, NULL if we are ready to transmit.
105 */
106 struct GNUNET_MQ_Envelope *env;
107
108 /**
109 * Handle for calling #GCP_request_mq_cancel() once we are finished.
110 */
111 struct GCP_MessageQueueManager *mq_man;
112
113 /**
114 * Task for connection maintenance.
115 */
116 struct GNUNET_SCHEDULER_Task *task;
117
118 /**
119 * Function to call once we are ready to transmit.
120 */
121 GCC_ReadyCallback ready_cb;
122
123 /**
124 * Closure for @e ready_cb.
125 */
126 void *ready_cb_cls;
127
128 /**
129 * How long do we wait before we try again with a CREATE message?
130 */
131 struct GNUNET_TIME_Relative retry_delay;
132
133 /**
134 * State of the connection.
135 */
136 enum CadetConnectionState state;
137
138 /**
139 * Offset of our @e destination in @e path.
140 */
141 unsigned int off;
142
143 /**
144 * Are we ready to transmit via @e mq_man right now?
145 */
146 int mqm_ready;
147
148};
149
150
151/**
152 * Destroy a connection.
153 *
154 * @param cc connection to destroy
155 */
156void
157GCC_destroy (struct CadetConnection *cc)
158{
159 struct GNUNET_MQ_Envelope *env = NULL;
160
161 if (CADET_CONNECTION_SENDING_CREATE != cc->state)
162 {
163 struct GNUNET_CADET_ConnectionDestroyMessage *destroy_msg;
164
165 /* Need to notify next hop that we are down. */
166 env = GNUNET_MQ_msg (destroy_msg,
167 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY);
168 destroy_msg->cid = cc->cid;
169 }
170 GCP_request_mq_cancel (cc->mq_man,
171 env);
172 cc->mq_man = NULL;
173 GCPP_del_connection (cc->path,
174 cc->off,
175 cc);
176 GNUNET_assert (GNUNET_YES ==
177 GNUNET_CONTAINER_multishortmap_remove (connections,
178 &GCC_get_id (cc)->connection_of_tunnel,
179 cc));
180 GNUNET_free (cc);
181}
182
183
184/**
185 * Return the tunnel associated with this connection.
186 *
187 * @param cc connection to query
188 * @return corresponding entry in the tunnel's connection list
189 */
190struct CadetTConnection *
191GCC_get_ct (struct CadetConnection *cc)
192{
193 return cc->ct;
194}
195
196
197/**
198 * A connection ACK was received for this connection, implying
199 * that the end-to-end connection is up. Process it.
200 *
201 * @param cc the connection that got the ACK.
202 */
203void
204GCC_handle_connection_ack (struct CadetConnection *cc)
205{
206 if (NULL != cc->task)
207 {
208 GNUNET_SCHEDULER_cancel (cc->task);
209 cc->task = NULL;
210 }
211#if FIXME_KEEPALIVE
212 cc->task = GNUNET_SCHEDULER_add_delayed (cc->keepalive_period,
213 &send_keepalive,
214 cc);
215#endif
216 cc->state = CADET_CONNECTION_READY;
217 if (GNUNET_YES == cc->mqm_ready)
218 cc->ready_cb (cc->ready_cb_cls,
219 GNUNET_YES);
220}
221
222
223/**
224 * Handle KX message.
225 *
226 * @param cc connection that received encrypted message
227 * @param msg the key exchange message
228 */
229void
230GCC_handle_kx (struct CadetConnection *cc,
231 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
232{
233 if (CADET_CONNECTION_SENT == cc->state)
234 {
235 /* We didn't get the CREATE_ACK, but instead got payload. That's fine,
236 clearly something is working, so pretend we got an ACK. */
237 GCC_handle_connection_ack (cc);
238 }
239 GCT_handle_kx (cc->ct,
240 msg);
241}
242
243
244/**
245 * Handle encrypted message.
246 *
247 * @param cc connection that received encrypted message
248 * @param msg the encrypted message to decrypt
249 */
250void
251GCC_handle_encrypted (struct CadetConnection *cc,
252 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
253{
254 if (CADET_CONNECTION_SENT == cc->state)
255 {
256 /* We didn't get the CREATE_ACK, but instead got payload. That's fine,
257 clearly something is working, so pretend we got an ACK. */
258 GCC_handle_connection_ack (cc);
259 }
260 GCT_handle_encrypted (cc->ct,
261 msg);
262}
263
264
265/**
266 * Send a CREATE message to the first hop.
267 *
268 * @param cls the `struct CadetConnection` to initiate
269 */
270static void
271send_create (void *cls)
272{
273 struct CadetConnection *cc = cls;
274 struct GNUNET_CADET_ConnectionCreateMessage *create_msg;
275 struct GNUNET_PeerIdentity *pids;
276 struct GNUNET_MQ_Envelope *env;
277 unsigned int path_length;
278
279 cc->task = NULL;
280 GNUNET_assert (GNUNET_YES == cc->mqm_ready);
281 path_length = GCPP_get_length (cc->path);
282 env = GNUNET_MQ_msg_extra (create_msg,
283 path_length * sizeof (struct GNUNET_PeerIdentity),
284 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
285 create_msg->cid = cc->cid;
286 pids = (struct GNUNET_PeerIdentity *) &create_msg[1];
287 for (unsigned int i=0;i<path_length;i++)
288 pids[i] = *GCP_get_id (GCPP_get_peer_at_offset (cc->path,
289 i));
290 cc->env = env;
291 cc->mqm_ready = GNUNET_NO;
292 cc->state = CADET_CONNECTION_SENT;
293 GCP_send (cc->mq_man,
294 env);
295}
296
297
298/**
299 * Send a CREATE_ACK message towards the origin.
300 *
301 * @param cls the `struct CadetConnection` to initiate
302 */
303static void
304send_create_ack (void *cls)
305{
306 struct CadetConnection *cc = cls;
307 struct GNUNET_CADET_ConnectionCreateMessage *create_msg;
308 struct GNUNET_PeerIdentity *pids;
309 struct GNUNET_MQ_Envelope *env;
310 unsigned int path_length;
311
312 cc->task = NULL;
313 GNUNET_assert (GNUNET_YES == cc->mqm_ready);
314 path_length = GCPP_get_length (cc->path);
315 env = GNUNET_MQ_msg_extra (create_msg,
316 path_length * sizeof (struct GNUNET_PeerIdentity),
317 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
318 create_msg->cid = cc->cid;
319 pids = (struct GNUNET_PeerIdentity *) &create_msg[1];
320 for (unsigned int i=0;i<path_length;i++)
321 pids[i] = *GCP_get_id (GCPP_get_peer_at_offset (cc->path,
322 i));
323 cc->env = env;
324 cc->mqm_ready = GNUNET_NO;
325 cc->state = CADET_CONNECTION_READY;
326 GCP_send (cc->mq_man,
327 env);
328}
329
330
331/**
332 * We got a #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE for a
333 * connection that we already have. Either our ACK got lost
334 * or something is fishy. Consider retransmitting the ACK.
335 *
336 * @param cc connection that got the duplicate CREATE
337 */
338void
339GCC_handle_duplicate_create (struct CadetConnection *cc)
340{
341 if (GNUNET_YES == cc->mqm_ready)
342 {
343 /* Tell tunnel that we are not ready for transmission anymore
344 (until CREATE_ACK is done) */
345 cc->ready_cb (cc->ready_cb_cls,
346 GNUNET_NO);
347
348 /* Revert back to the state of having only received the 'CREATE',
349 and immediately proceed to send the CREATE_ACK. */
350 cc->state = CADET_CONNECTION_CREATE_RECEIVED;
351 cc->task = GNUNET_SCHEDULER_add_now (&send_create_ack,
352 cc);
353 }
354 else
355 {
356 /* We are currently sending something else back, which
357 can only be an ACK or payload, either of which would
358 do. So actually no need to do anything. */
359 }
360}
361
362
363/**
364 * There has been a change in the message queue existence for our
365 * peer at the first hop. Adjust accordingly.
366 *
367 * @param cls the `struct CadetConnection`
368 * @param available #GNUNET_YES if sending is now possible,
369 * #GNUNET_NO if sending is no longer possible
370 * #GNUNET_SYSERR if sending is no longer possible
371 * and the last envelope was discarded
372 */
373static void
374manage_first_hop_mq (void *cls,
375 int available)
376{
377 struct CadetConnection *cc = cls;
378
379 if (GNUNET_YES != available)
380 {
381 /* Connection is down, for now... */
382 cc->mqm_ready = GNUNET_NO;
383 cc->state = CADET_CONNECTION_NEW;
384 cc->retry_delay = GNUNET_TIME_UNIT_ZERO;
385 if (NULL != cc->task)
386 {
387 GNUNET_SCHEDULER_cancel (cc->task);
388 cc->task = NULL;
389 }
390 cc->ready_cb (cc->ready_cb_cls,
391 GNUNET_NO);
392 return;
393 }
394
395 cc->mqm_ready = GNUNET_YES;
396 switch (cc->state)
397 {
398 case CADET_CONNECTION_NEW:
399 /* Transmit immediately */
400 cc->task = GNUNET_SCHEDULER_add_now (&send_create,
401 cc);
402 break;
403 case CADET_CONNECTION_SENDING_CREATE:
404 /* Should not be possible to be called in this state. */
405 GNUNET_assert (0);
406 break;
407 case CADET_CONNECTION_SENT:
408 /* Retry a bit later... */
409 cc->retry_delay = GNUNET_TIME_STD_BACKOFF (cc->retry_delay);
410 cc->task = GNUNET_SCHEDULER_add_delayed (cc->retry_delay,
411 &send_create,
412 cc);
413 break;
414 case CADET_CONNECTION_CREATE_RECEIVED:
415 /* We got the 'CREATE' (incoming connection), should send the CREATE_ACK */
416 cc->task = GNUNET_SCHEDULER_add_now (&send_create_ack,
417 cc);
418 break;
419 case CADET_CONNECTION_READY:
420 cc->ready_cb (cc->ready_cb_cls,
421 GNUNET_YES);
422 break;
423 }
424}
425
426
427/**
428 * Create a connection to @a destination via @a path and notify @a cb
429 * whenever we are ready for more data. Shared logic independent of
430 * who is initiating the connection.
431 *
432 * @param destination where to go
433 * @param path which path to take (may not be the full path)
434 * @param ct which tunnel uses this connection
435 * @param init_state initial state for the connection
436 * @param ready_cb function to call when ready to transmit
437 * @param ready_cb_cls closure for @a cb
438 * @return handle to the connection
439 */
440static struct CadetConnection *
441connection_create (struct CadetPeer *destination,
442 struct CadetPeerPath *path,
443 struct CadetTConnection *ct,
444 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
445 enum CadetConnectionState init_state,
446 GCC_ReadyCallback ready_cb,
447 void *ready_cb_cls)
448{
449 struct CadetConnection *cc;
450 struct CadetPeer *first_hop;
451 unsigned int off;
452
453 off = GCPP_find_peer (path,
454 destination);
455 GNUNET_assert (UINT_MAX > off);
456 cc = GNUNET_new (struct CadetConnection);
457 cc->state = init_state;
458 cc->ct = ct;
459 cc->cid = *cid;
460 GNUNET_assert (GNUNET_OK ==
461 GNUNET_CONTAINER_multishortmap_put (connections,
462 &GCC_get_id (cc)->connection_of_tunnel,
463 cc,
464 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
465 cc->ready_cb = ready_cb;
466 cc->ready_cb_cls = ready_cb_cls;
467 cc->path = path;
468 cc->off = off;
469 GCPP_add_connection (path,
470 off,
471 cc);
472 for (unsigned int i=0;i<off;i++)
473 GCP_add_connection (GCPP_get_peer_at_offset (path,
474 off),
475 cc);
476
477 first_hop = GCPP_get_peer_at_offset (path,
478 0);
479 cc->mq_man = GCP_request_mq (first_hop,
480 &manage_first_hop_mq,
481 cc);
482 return cc;
483}
484
485
486/**
487 * Create a connection to @a destination via @a path and
488 * notify @a cb whenever we are ready for more data. This
489 * is an inbound tunnel, so we must use the existing @a cid
490 *
491 * @param destination where to go
492 * @param path which path to take (may not be the full path)
493 * @param ct which tunnel uses this connection
494 * @param ready_cb function to call when ready to transmit
495 * @param ready_cb_cls closure for @a cb
496 * @return handle to the connection
497 */
498struct CadetConnection *
499GCC_create_inbound (struct CadetPeer *destination,
500 struct CadetPeerPath *path,
501 struct CadetTConnection *ct,
502 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
503 GCC_ReadyCallback ready_cb,
504 void *ready_cb_cls)
505{
506 return connection_create (destination,
507 path,
508 ct,
509 cid,
510 CADET_CONNECTION_CREATE_RECEIVED,
511 ready_cb,
512 ready_cb_cls);
513}
514
515
516/**
517 * Create a connection to @a destination via @a path and
518 * notify @a cb whenever we are ready for more data.
519 *
520 * @param destination where to go
521 * @param path which path to take (may not be the full path)
522 * @param ct tunnel that uses the connection
523 * @param ready_cb function to call when ready to transmit
524 * @param ready_cb_cls closure for @a cb
525 * @return handle to the connection
526 */
527struct CadetConnection *
528GCC_create (struct CadetPeer *destination,
529 struct CadetPeerPath *path,
530 struct CadetTConnection *ct,
531 GCC_ReadyCallback ready_cb,
532 void *ready_cb_cls)
533{
534 struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
535
536 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
537 &cid,
538 sizeof (cid));
539 return connection_create (destination,
540 path,
541 ct,
542 &cid,
543 CADET_CONNECTION_NEW,
544 ready_cb,
545 ready_cb_cls);
546}
547
548
549/**
550 * Transmit message @a msg via connection @a cc. Must only be called
551 * (once) after the connection has signalled that it is ready via the
552 * `ready_cb`. Clients can also use #GCC_is_ready() to check if the
553 * connection is right now ready for transmission.
554 *
555 * @param cc connection identification
556 * @param env envelope with message to transmit; must NOT
557 * yet have a #GNUNET_MQ_notify_sent() callback attached to it
558 */
559void
560GCC_transmit (struct CadetConnection *cc,
561 struct GNUNET_MQ_Envelope *env)
562{
563 GNUNET_assert (GNUNET_YES == cc->mqm_ready);
564 GNUNET_assert (CADET_CONNECTION_READY == cc->state);
565 cc->mqm_ready = GNUNET_NO;
566 GCP_send (cc->mq_man,
567 env);
568}
569
570
571/**
572 * Obtain the path used by this connection.
573 *
574 * @param cc connection
575 * @return path to @a cc
576 */
577struct CadetPeerPath *
578GCC_get_path (struct CadetConnection *cc)
579{
580 return cc->path;
581}
582
583
584/**
585 * Obtain unique ID for the connection.
586 *
587 * @param cc connection.
588 * @return unique number of the connection
589 */
590const struct GNUNET_CADET_ConnectionTunnelIdentifier *
591GCC_get_id (struct CadetConnection *cc)
592{
593 return &cc->cid;
594}
595
596
597/**
598 * Get a (static) string for a connection.
599 *
600 * @param cc Connection.
601 */
602const char *
603GCC_2s (const struct CadetConnection *cc)
604{
605 static char buf[128];
606
607 if (NULL == cc)
608 return "Connection(NULL)";
609
610 if (NULL != cc->ct)
611 {
612 GNUNET_snprintf (buf,
613 sizeof (buf),
614 "Connection(%s(Tunnel(%s)))",
615 GNUNET_sh2s (&cc->cid.connection_of_tunnel),
616 GCT_2s (cc->ct->t));
617 return buf;
618 }
619 GNUNET_snprintf (buf,
620 sizeof (buf),
621 "Connection(%s(Tunnel(NULL)))",
622 GNUNET_sh2s (&cc->cid.connection_of_tunnel));
623 return buf;
624}
625
626
627#define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-con",__VA_ARGS__)
628
629
630/**
631 * Log connection info.
632 *
633 * @param cc connection
634 * @param level Debug level to use.
635 */
636void
637GCC_debug (struct CadetConnection *cc,
638 enum GNUNET_ErrorType level)
639{
640 int do_log;
641 char *s;
642
643 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
644 "cadet-con",
645 __FILE__, __FUNCTION__, __LINE__);
646 if (0 == do_log)
647 return;
648 if (NULL == cc)
649 {
650 LOG2 (level,
651 "Connection (NULL)\n");
652 return;
653 }
654 s = GCPP_2s (cc->path);
655 LOG2 (level,
656 "Connection %s to %s via path %s in state %d is %s\n",
657 GCC_2s (cc),
658 GCP_2s (cc->destination),
659 s,
660 cc->state,
661 (GNUNET_YES == cc->mqm_ready) ? "ready" : "busy");
662 GNUNET_free (s);
663}
664
665/* end of gnunet-service-cadet-new_connection.c */
diff --git a/src/cadet/gnunet-service-cadet-new_connection.h b/src/cadet/gnunet-service-cadet-new_connection.h
new file mode 100644
index 000000000..99426776d
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new_connection.h
@@ -0,0 +1,196 @@
1
2/*
3 This file is part of GNUnet.
4 Copyright (C) 2001-2017 GNUnet e.V.
5
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
10
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22/**
23 * @file cadet/gnunet-service-cadet-new_connection.h
24 * @brief
25 * @author Bartlomiej Polot
26 * @author Christian Grothoff
27 */
28#ifndef GNUNET_SERVICE_CADET_CONNECTION_H
29#define GNUNET_SERVICE_CADET_CONNECTION_H
30
31#include "gnunet_util_lib.h"
32#include "gnunet-service-cadet-new.h"
33#include "gnunet-service-cadet-new_peer.h"
34#include "cadet_protocol.h"
35
36
37/**
38 * Function called to notify tunnel about change in our readyness.
39 *
40 * @param cls closure
41 * @param is_ready #GNUNET_YES if the connection is now ready for transmission,
42 * #GNUNET_NO if the connection is no longer ready for transmission
43 */
44typedef void
45(*GCC_ReadyCallback)(void *cls,
46 int is_ready);
47
48
49/**
50 * Destroy a connection.
51 *
52 * @param cc connection to destroy
53 */
54void
55GCC_destroy (struct CadetConnection *cc);
56
57
58/**
59 * Create a connection to @a destination via @a path and
60 * notify @a cb whenever we are ready for more data.
61 *
62 * @param destination where to go
63 * @param path which path to take (may not be the full path)
64 * @param ct which tunnel uses this connection
65 * @param ready_cb function to call when ready to transmit
66 * @param ready_cb_cls closure for @a cb
67 * @return handle to the connection
68 */
69struct CadetConnection *
70GCC_create (struct CadetPeer *destination,
71 struct CadetPeerPath *path,
72 struct CadetTConnection *ct,
73 GCC_ReadyCallback ready_cb,
74 void *ready_cb_cls);
75
76
77/**
78 * Create a connection to @a destination via @a path and
79 * notify @a cb whenever we are ready for more data. This
80 * is an inbound tunnel, so we must use the existing @a cid
81 *
82 * @param destination where to go
83 * @param path which path to take (may not be the full path)
84 * @param ct which tunnel uses this connection
85 * @param ready_cb function to call when ready to transmit
86 * @param ready_cb_cls closure for @a cb
87 * @return handle to the connection
88 */
89struct CadetConnection *
90GCC_create_inbound (struct CadetPeer *destination,
91 struct CadetPeerPath *path,
92 struct CadetTConnection *ct,
93 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
94 GCC_ReadyCallback ready_cb,
95 void *ready_cb_cls);
96
97
98/**
99 * Transmit message @a msg via connection @a cc. Must only be called
100 * (once) after the connection has signalled that it is ready via the
101 * `ready_cb`. Clients can also use #GCC_is_ready() to check if the
102 * connection is right now ready for transmission.
103 *
104 * @param cc connection identification
105 * @param env envelope with message to transmit;
106 * the #GNUNET_MQ_notify_send() must not have yet been used
107 * for the envelope. Also, the message better match the
108 * connection identifier of this connection...
109 */
110void
111GCC_transmit (struct CadetConnection *cc,
112 struct GNUNET_MQ_Envelope *env);
113
114
115/**
116 * An ACK was received for this connection, process it.
117 *
118 * @param cc the connection that got the ACK.
119 */
120void
121GCC_handle_connection_ack (struct CadetConnection *cc);
122
123
124/**
125 * Handle KX message.
126 *
127 * @param cc connection that received encrypted message
128 * @param msg the key exchange message
129 */
130void
131GCC_handle_kx (struct CadetConnection *cc,
132 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg);
133
134
135/**
136 * Handle encrypted message.
137 *
138 * @param cc connection that received encrypted message
139 * @param msg the encrypted message to decrypt
140 */
141void
142GCC_handle_encrypted (struct CadetConnection *cc,
143 const struct GNUNET_CADET_TunnelEncryptedMessage *msg);
144
145
146/**
147 * Return the tunnel associated with this connection.
148 *
149 * @param cc connection to query
150 * @return corresponding entry in the tunnel's connection list
151 */
152struct CadetTConnection *
153GCC_get_ct (struct CadetConnection *cc);
154
155
156/**
157 * Obtain the path used by this connection.
158 *
159 * @param cc connection
160 * @return path to @a cc
161 */
162struct CadetPeerPath *
163GCC_get_path (struct CadetConnection *cc);
164
165
166/**
167 * Obtain unique ID for the connection.
168 *
169 * @param cc connection.
170 * @return unique number of the connection
171 */
172const struct GNUNET_CADET_ConnectionTunnelIdentifier *
173GCC_get_id (struct CadetConnection *cc);
174
175
176/**
177 * Get a (static) string for a connection.
178 *
179 * @param cc Connection.
180 */
181const char *
182GCC_2s (const struct CadetConnection *cc);
183
184
185/**
186 * Log connection info.
187 *
188 * @param cc connection
189 * @param level Debug level to use.
190 */
191void
192GCC_debug (struct CadetConnection *cc,
193 enum GNUNET_ErrorType level);
194
195
196#endif
diff --git a/src/cadet/gnunet-service-cadet-new_core.c b/src/cadet/gnunet-service-cadet-new_core.c
new file mode 100644
index 000000000..9ce4418de
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new_core.c
@@ -0,0 +1,903 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2017 GNUnet e.V.
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21/**
22 * @file cadet/gnunet-service-cadet_core.c
23 * @brief cadet service; interaction with CORE service
24 * @author Bartlomiej Polot
25 * @author Christian Grothoff
26 *
27 * All functions in this file should use the prefix GCO (Gnunet Cadet cOre (bottom))
28 *
29 * TODO:
30 * - pass encrypted ACK to connection (!)
31 * - given BROKEN messages, destroy paths (?)
32 * -
33 * - handle POLL (if needed)
34 */
35#include "platform.h"
36#include "gnunet-service-cadet-new_core.h"
37#include "gnunet-service-cadet-new_paths.h"
38#include "gnunet-service-cadet-new_peer.h"
39#include "gnunet-service-cadet-new_connection.h"
40#include "gnunet-service-cadet-new_tunnels.h"
41#include "gnunet_core_service.h"
42#include "cadet_protocol.h"
43
44/**
45 * Number of messages we are willing to buffer per route.
46 */
47#define ROUTE_BUFFER_SIZE 8
48
49
50/**
51 * Information we keep per direction for a route.
52 */
53struct RouteDirection
54{
55 /**
56 * Target peer.
57 */
58 struct CadetPeer *hop;
59
60 /**
61 * Route this direction is part of.
62 */
63 struct CadetRoute *my_route;
64
65 /**
66 * Message queue manager for @e hop.
67 */
68 struct GCP_MessageQueueManager *mqm;
69
70 /**
71 * Cyclic message buffer to @e hop.
72 */
73 struct GNUNET_MQ_Envelope *out_buffer[ROUTE_BUFFER_SIZE];
74
75 /**
76 * Next write offset to use to append messages to @e out_buffer.
77 */
78 unsigned int out_wpos;
79
80 /**
81 * Next read offset to use to retrieve messages from @e out_buffer.
82 */
83 unsigned int out_rpos;
84
85 /**
86 * Is @e mqm currently ready for transmission?
87 */
88 int is_ready;
89
90};
91
92
93/**
94 * Description of a segment of a `struct CadetConnection` at the
95 * intermediate peers. Routes are basically entries in a peer's
96 * routing table for forwarding traffic. At both endpoints, the
97 * routes are terminated by a `struct CadetConnection`, which knows
98 * the complete `struct CadetPath` that is formed by the individual
99 * routes.
100 */
101struct CadetRoute
102{
103
104 /**
105 * Information about the next hop on this route.
106 */
107 struct RouteDirection next;
108
109 /**
110 * Information about the previous hop on this route.
111 */
112 struct RouteDirection prev;
113
114 /**
115 * Unique identifier for the connection that uses this route.
116 */
117 struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
118
119 /**
120 * When was this route last in use?
121 */
122 struct GNUNET_TIME_Absolute last_use;
123
124};
125
126
127/**
128 * Handle to the CORE service.
129 */
130static struct GNUNET_CORE_Handle *core;
131
132/**
133 * Routes on which this peer is an intermediate.
134 */
135static struct GNUNET_CONTAINER_MultiShortmap *routes;
136
137
138/**
139 * Get the route corresponding to a hash.
140 *
141 * @param cid hash generated from the connection identifier
142 */
143static struct CadetRoute *
144get_route (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
145{
146 return GNUNET_CONTAINER_multishortmap_get (routes,
147 &cid->connection_of_tunnel);
148}
149
150
151/**
152 * We message @a msg from @a prev. Find its route by @a cid and
153 * forward to the next hop. Drop and signal broken route if we do not
154 * have a route.
155 *
156 * @param prev previous hop (sender)
157 * @param cid connection identifier, tells us which route to use
158 * @param msg the message to forward
159 */
160static void
161route_message (struct CadetPeer *prev,
162 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
163 const struct GNUNET_MessageHeader *msg)
164{
165 struct CadetRoute *route;
166 struct RouteDirection *dir;
167 struct GNUNET_MQ_Envelope *env;
168
169 route = get_route (cid);
170 if (NULL == route)
171 {
172 struct GNUNET_MQ_Envelope *env;
173 struct GNUNET_CADET_ConnectionBrokenMessage *bm;
174
175 env = GNUNET_MQ_msg (bm,
176 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
177 bm->cid = *cid;
178 bm->peer1 = my_full_id;
179 GCP_send_ooo (prev,
180 env);
181 return;
182 }
183 dir = (prev == route->prev.hop) ? &route->next : &route->prev;
184 if (GNUNET_YES == dir->is_ready)
185 {
186 dir->is_ready = GNUNET_NO;
187 GCP_send (dir->mqm,
188 GNUNET_MQ_msg_copy (msg));
189 return;
190 }
191 env = dir->out_buffer[dir->out_wpos];
192 if (NULL != env)
193 {
194 /* Queue full, drop earliest message in queue */
195 GNUNET_assert (dir->out_rpos == dir->out_wpos);
196 GNUNET_MQ_discard (env);
197 dir->out_rpos++;
198 if (ROUTE_BUFFER_SIZE == dir->out_rpos)
199 dir->out_rpos = 0;
200 }
201 env = GNUNET_MQ_msg_copy (msg);
202 dir->out_buffer[dir->out_wpos] = env;
203 dir->out_wpos++;
204 if (ROUTE_BUFFER_SIZE == dir->out_wpos)
205 dir->out_wpos = 0;
206}
207
208
209/**
210 * Check if the create_connection message has the appropriate size.
211 *
212 * @param cls Closure (unused).
213 * @param msg Message to check.
214 *
215 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
216 */
217static int
218check_connection_create (void *cls,
219 const struct GNUNET_CADET_ConnectionCreateMessage *msg)
220{
221 uint16_t size = ntohs (msg->header.size) - sizeof (*msg);
222
223 if (0 != (size % sizeof (struct GNUNET_PeerIdentity)))
224 {
225 GNUNET_break_op (0);
226 return GNUNET_NO;
227 }
228 return GNUNET_YES;
229}
230
231
232/**
233 * Free internal data of a route direction.
234 *
235 * @param dir direction to destroy (do NOT free memory of 'dir' itself)
236 */
237static void
238destroy_direction (struct RouteDirection *dir)
239{
240 for (unsigned int i=0;i<ROUTE_BUFFER_SIZE;i++)
241 if (NULL != dir->out_buffer[i])
242 {
243 GNUNET_MQ_discard (dir->out_buffer[i]);
244 dir->out_buffer[i] = NULL;
245 }
246 if (NULL != dir->mqm)
247 {
248 GCP_request_mq_cancel (dir->mqm,
249 NULL);
250 dir->mqm = NULL;
251 }
252}
253
254
255/**
256 * Destroy our state for @a route.
257 *
258 * @param route route to destroy
259 */
260static void
261destroy_route (struct CadetRoute *route)
262{
263 destroy_direction (&route->prev);
264 destroy_direction (&route->next);
265 GNUNET_free (route);
266}
267
268
269/**
270 * Send message that a route is broken between @a peer1 and @a peer2.
271 *
272 * @param target where to send the message
273 * @param cid connection identifier to use
274 * @param peer1 one of the peers where a link is broken
275 * @param peer2 another one of the peers where a link is broken
276 */
277static void
278send_broken (struct RouteDirection *target,
279 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
280 const struct GNUNET_PeerIdentity *peer1,
281 const struct GNUNET_PeerIdentity *peer2)
282{
283 struct GNUNET_MQ_Envelope *env;
284 struct GNUNET_CADET_ConnectionBrokenMessage *bm;
285
286 env = GNUNET_MQ_msg (bm,
287 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
288 bm->cid = *cid;
289 if (NULL != peer1)
290 bm->peer1 = *peer1;
291 if (NULL != peer2)
292 bm->peer2 = *peer2;
293 GCP_request_mq_cancel (target->mqm,
294 env);
295 target->mqm = NULL;
296}
297
298
299/**
300 * Function called when the message queue to the previous hop
301 * becomes available/unavailable. We expect this function to
302 * be called immediately when we register, and then again
303 * later if the connection ever goes down.
304 *
305 * @param cls the `struct RouteDirection`
306 * @param available #GNUNET_YES if sending is now possible,
307 * #GNUNET_NO if sending is no longer possible
308 * #GNUNET_SYSERR if sending is no longer possible
309 * and the last envelope was discarded
310 */
311static void
312dir_ready_cb (void *cls,
313 int ready)
314{
315 struct RouteDirection *dir = cls;
316 struct CadetRoute *route = dir->my_route;
317 struct RouteDirection *odir;
318
319 if (GNUNET_YES == ready)
320 {
321 struct GNUNET_MQ_Envelope *env;
322
323 dir->is_ready = GNUNET_YES;
324 if (NULL != (env = dir->out_buffer[dir->out_rpos]))
325 {
326 dir->out_buffer[dir->out_rpos] = NULL;
327 dir->out_rpos++;
328 if (ROUTE_BUFFER_SIZE == dir->out_rpos)
329 dir->out_rpos = 0;
330 dir->is_ready = GNUNET_NO;
331 GCP_send (dir->mqm,
332 env);
333 }
334 return;
335 }
336 odir = (dir == &route->next) ? &route->prev : &route->next;
337 send_broken (&route->next,
338 &route->cid,
339 GCP_get_id (odir->hop),
340 &my_full_id);
341 destroy_route (route);
342}
343
344
345/**
346 * Initialize one of the directions of a route.
347 *
348 * @param route route the direction belongs to
349 * @param dir direction to initialize
350 * @param hop next hop on in the @a dir
351 */
352static void
353dir_init (struct RouteDirection *dir,
354 struct CadetRoute *route,
355 struct CadetPeer *hop)
356{
357 dir->hop = hop;
358 dir->my_route = route;
359 dir->mqm = GCP_request_mq (hop,
360 &dir_ready_cb,
361 dir);
362 GNUNET_assert (GNUNET_YES == dir->is_ready);
363}
364
365
366/**
367 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE
368 *
369 * @param cls Closure (CadetPeer for neighbor that sent the message).
370 * @param msg Message itself.
371 */
372static void
373handle_connection_create (void *cls,
374 const struct GNUNET_CADET_ConnectionCreateMessage *msg)
375{
376 struct CadetPeer *sender = cls;
377 struct CadetPeer *next;
378 const struct GNUNET_PeerIdentity *pids = (const struct GNUNET_PeerIdentity *) &msg[1];
379 struct CadetRoute *route;
380 uint16_t size = ntohs (msg->header.size) - sizeof (*msg);
381 unsigned int path_length;
382 unsigned int off;
383
384 path_length = size / sizeof (struct GNUNET_PeerIdentity);
385 /* Initiator is at offset 0. */
386 for (off=1;off<path_length;off++)
387 if (0 == memcmp (&my_full_id,
388 &pids[off],
389 sizeof (struct GNUNET_PeerIdentity)))
390 break;
391 if (off == path_length)
392 {
393 /* We are not on the path, bogus request */
394 GNUNET_break_op (0);
395 return;
396 }
397 /* Check previous hop */
398 if (sender != GCP_get (&pids[off - 1],
399 GNUNET_NO))
400 {
401 /* sender is not on the path, not allowed */
402 GNUNET_break_op (0);
403 return;
404 }
405 if (NULL !=
406 get_route (&msg->cid))
407 {
408 /* Duplicate CREATE, pass it on, previous one might have been lost! */
409 route_message (sender,
410 &msg->cid,
411 &msg->header);
412 return;
413 }
414 if (off == path_length - 1)
415 {
416 /* We are the destination, create connection */
417 struct CadetConnection *cc;
418 struct CadetPeerPath *path;
419 struct CadetPeer *origin;
420
421 cc = GNUNET_CONTAINER_multishortmap_get (connections,
422 &msg->cid.connection_of_tunnel);
423 if (NULL != cc)
424 {
425 /* Duplicate CREATE, likely our ACK got lost, retransmit the ACK! */
426 GNUNET_break (0); // FIXME: not implemented!
427 return;
428 }
429
430 path = GCPP_get_path_from_route (path_length,
431 pids);
432 origin = GCP_get (&pids[0],
433 GNUNET_YES);
434 GCT_add_inbound_connection (GCT_create_tunnel (origin),
435 &msg->cid,
436 path);
437 return;
438 }
439 /* We are merely a hop on the way, check if we can support the route */
440 next = GCP_get (&pids[off + 1],
441 GNUNET_NO);
442 if ( (NULL == next) ||
443 (GNUNET_NO == GCP_has_core_connection (next)) )
444 {
445 /* unworkable, send back BROKEN notification */
446 struct GNUNET_MQ_Envelope *env;
447 struct GNUNET_CADET_ConnectionBrokenMessage *bm;
448
449 env = GNUNET_MQ_msg (bm,
450 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
451 bm->cid = msg->cid;
452 bm->peer1 = pids[off + 1];
453 bm->peer2 = my_full_id;
454 GCP_send_ooo (sender,
455 env);
456 return;
457 }
458
459 /* Workable route, create routing entry */
460 route = GNUNET_new (struct CadetRoute);
461 route->cid = msg->cid;
462 dir_init (&route->prev,
463 route,
464 sender);
465 dir_init (&route->next,
466 route,
467 next);
468 GNUNET_assert (GNUNET_OK ==
469 GNUNET_CONTAINER_multishortmap_put (routes,
470 &route->cid.connection_of_tunnel,
471 route,
472 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
473}
474
475
476/**
477 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK
478 *
479 * @param cls Closure (CadetPeer for neighbor that sent the message).
480 * @param msg Message itself.
481 */
482static void
483handle_connection_create_ack (void *cls,
484 const struct GNUNET_CADET_ConnectionCreateMessageAckMessage *msg)
485{
486 struct CadetPeer *peer = cls;
487 struct CadetConnection *cc;
488
489 /* First, check if ACK belongs to a connection that ends here. */
490 cc = GNUNET_CONTAINER_multishortmap_get (connections,
491 &msg->cid.connection_of_tunnel);
492 if (NULL != cc)
493 {
494 /* verify ACK came from the right direction */
495 struct CadetPeerPath *path = GCC_get_path (cc);
496
497 if (peer !=
498 GCPP_get_peer_at_offset (path,
499 0))
500 {
501 /* received ACK from unexpected direction, ignore! */
502 GNUNET_break_op (0);
503 return;
504 }
505 GCC_handle_connection_ack (cc);
506 return;
507 }
508
509 /* We're just an intermediary peer, route the message along its path */
510 route_message (peer,
511 &msg->cid,
512 &msg->header);
513}
514
515
516/**
517 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN
518 *
519 * @param cls Closure (CadetPeer for neighbor that sent the message).
520 * @param msg Message itself.
521 * @deprecated duplicate logic with #handle_destroy(); dedup!
522 */
523static void
524handle_connection_broken (void *cls,
525 const struct GNUNET_CADET_ConnectionBrokenMessage *msg)
526{
527 struct CadetPeer *peer = cls;
528 struct CadetConnection *cc;
529 struct CadetRoute *route;
530
531 /* First, check if message belongs to a connection that ends here. */
532 cc = GNUNET_CONTAINER_multishortmap_get (connections,
533 &msg->cid.connection_of_tunnel);
534 if (NULL != cc)
535 {
536 /* verify message came from the right direction */
537 struct CadetPeerPath *path = GCC_get_path (cc);
538
539 if (peer !=
540 GCPP_get_peer_at_offset (path,
541 0))
542 {
543 /* received message from unexpected direction, ignore! */
544 GNUNET_break_op (0);
545 return;
546 }
547 GCC_destroy (cc);
548
549 /* FIXME: also destroy the path up to the specified link! */
550 return;
551 }
552
553 /* We're just an intermediary peer, route the message along its path */
554 route = get_route (&msg->cid);
555 route_message (peer,
556 &msg->cid,
557 &msg->header);
558 destroy_route (route);
559 /* FIXME: also destroy paths we MAY have up to the specified link! */
560}
561
562
563/**
564 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY
565 *
566 * @param cls Closure (CadetPeer for neighbor that sent the message).
567 * @param msg Message itself.
568 */
569static void
570handle_connection_destroy (void *cls,
571 const struct GNUNET_CADET_ConnectionDestroyMessage *msg)
572{
573 struct CadetPeer *peer = cls;
574 struct CadetConnection *cc;
575 struct CadetRoute *route;
576
577 /* First, check if message belongs to a connection that ends here. */
578 cc = GNUNET_CONTAINER_multishortmap_get (connections,
579 &msg->cid.connection_of_tunnel);
580 if (NULL != cc)
581 {
582 /* verify message came from the right direction */
583 struct CadetPeerPath *path = GCC_get_path (cc);
584
585 if (peer !=
586 GCPP_get_peer_at_offset (path,
587 0))
588 {
589 /* received message from unexpected direction, ignore! */
590 GNUNET_break_op (0);
591 return;
592 }
593 GCC_destroy (cc);
594 return;
595 }
596
597 /* We're just an intermediary peer, route the message along its path */
598 route = get_route (&msg->cid);
599 route_message (peer,
600 &msg->cid,
601 &msg->header);
602 destroy_route (route);
603}
604
605
606/**
607 * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_HOP_BY_HOP_ENCRYPTED_ACK.
608 *
609 * @param cls Closure (CadetPeer for neighbor that sent the message).
610 * @param msg Message itself.
611 */
612static void
613handle_hop_by_hop_encrypted_ack (void *cls,
614 const struct GNUNET_CADET_ConnectionEncryptedAckMessage *msg)
615{
616 struct CadetPeer *peer = cls;
617 struct CadetConnection *cc;
618
619 /* First, check if message belongs to a connection that ends here. */
620 cc = GNUNET_CONTAINER_multishortmap_get (connections,
621 &msg->cid.connection_of_tunnel);
622 if (NULL != cc)
623 {
624 /* verify message came from the right direction */
625 struct CadetPeerPath *path = GCC_get_path (cc);
626
627 if (peer !=
628 GCPP_get_peer_at_offset (path,
629 0))
630 {
631 /* received message from unexpected direction, ignore! */
632 GNUNET_break_op (0);
633 return;
634 }
635#if FIXME
636 GCC_handle_ack (peer,
637 msg);
638#endif
639 return;
640 }
641
642 /* We're just an intermediary peer, route the message along its path */
643 route_message (peer,
644 &msg->cid,
645 &msg->header);
646}
647
648
649/**
650 * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL
651 *
652 * @param cls Closure (CadetPeer for neighbor that sent the message).
653 * @param msg Message itself.
654 */
655static void
656handle_poll (void *cls,
657 const struct GNUNET_CADET_ConnectionHopByHopPollMessage *msg)
658{
659 struct CadetPeer *peer = cls;
660
661#if FIXME
662 GCC_handle_poll (peer,
663 msg);
664#endif
665}
666
667
668/**
669 * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX
670 *
671 * @param cls Closure (CadetPeer for neighbor that sent the message).
672 * @param msg Message itself.
673 */
674static void
675handle_tunnel_kx (void *cls,
676 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
677{
678 struct CadetPeer *peer = cls;
679 struct CadetConnection *cc;
680
681 /* First, check if message belongs to a connection that ends here. */
682 cc = GNUNET_CONTAINER_multishortmap_get (connections,
683 &msg->cid.connection_of_tunnel);
684 if (NULL != cc)
685 {
686 /* verify message came from the right direction */
687 struct CadetPeerPath *path = GCC_get_path (cc);
688
689 if (peer !=
690 GCPP_get_peer_at_offset (path,
691 0))
692 {
693 /* received message from unexpected direction, ignore! */
694 GNUNET_break_op (0);
695 return;
696 }
697 GCC_handle_kx (cc,
698 msg);
699 return;
700 }
701
702 /* We're just an intermediary peer, route the message along its path */
703 route_message (peer,
704 &msg->cid,
705 &msg->header);
706}
707
708
709/**
710 * Check if the encrypted message has the appropriate size.
711 *
712 * @param cls Closure (unused).
713 * @param msg Message to check.
714 *
715 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
716 */
717static int
718check_tunnel_encrypted (void *cls,
719 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
720{
721 return GNUNET_YES;
722}
723
724
725/**
726 * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED.
727 *
728 * @param cls Closure (CadetPeer for neighbor that sent the message).
729 * @param msg Message itself.
730 */
731static void
732handle_tunnel_encrypted (void *cls,
733 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
734{
735 struct CadetPeer *peer = cls;
736 struct CadetConnection *cc;
737
738 /* First, check if message belongs to a connection that ends here. */
739 cc = GNUNET_CONTAINER_multishortmap_get (connections,
740 &msg->cid.connection_of_tunnel);
741 if (NULL != cc)
742 {
743 /* verify message came from the right direction */
744 struct CadetPeerPath *path = GCC_get_path (cc);
745
746 if (peer !=
747 GCPP_get_peer_at_offset (path,
748 0))
749 {
750 /* received message from unexpected direction, ignore! */
751 GNUNET_break_op (0);
752 return;
753 }
754 GCC_handle_encrypted (cc,
755 msg);
756 return;
757 }
758
759 /* We're just an intermediary peer, route the message along its path */
760 route_message (peer,
761 &msg->cid,
762 &msg->header);
763}
764
765
766/**
767 * Function called after #GNUNET_CORE_connect has succeeded (or failed
768 * for good). Note that the private key of the peer is intentionally
769 * not exposed here; if you need it, your process should try to read
770 * the private key file directly (which should work if you are
771 * authorized...). Implementations of this function must not call
772 * #GNUNET_CORE_disconnect (other than by scheduling a new task to
773 * do this later).
774 *
775 * @param cls closure
776 * @param my_identity ID of this peer, NULL if we failed
777 */
778static void
779core_init_cb (void *cls,
780 const struct GNUNET_PeerIdentity *my_identity)
781{
782 if (NULL == my_identity)
783 {
784 GNUNET_break (0);
785 return;
786 }
787 GNUNET_break (0 ==
788 memcmp (my_identity,
789 &my_full_id,
790 sizeof (struct GNUNET_PeerIdentity)));
791}
792
793
794/**
795 * Method called whenever a given peer connects.
796 *
797 * @param cls closure
798 * @param peer peer identity this notification is about
799 */
800static void *
801core_connect_cb (void *cls,
802 const struct GNUNET_PeerIdentity *peer,
803 struct GNUNET_MQ_Handle *mq)
804{
805 struct CadetPeer *cp;
806
807 cp = GCP_get (peer,
808 GNUNET_YES);
809 GCP_set_mq (cp,
810 mq);
811 return cp;
812}
813
814
815/**
816 * Method called whenever a peer disconnects.
817 *
818 * @param cls closure
819 * @param peer peer identity this notification is about
820 */
821static void
822core_disconnect_cb (void *cls,
823 const struct GNUNET_PeerIdentity *peer,
824 void *peer_cls)
825{
826 struct CadetPeer *cp = peer_cls;
827
828 GCP_set_mq (cp,
829 NULL);
830}
831
832
833/**
834 * Initialize the CORE subsystem.
835 *
836 * @param c Configuration.
837 */
838void
839GCO_init (const struct GNUNET_CONFIGURATION_Handle *c)
840{
841 struct GNUNET_MQ_MessageHandler handlers[] = {
842 GNUNET_MQ_hd_var_size (connection_create,
843 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE,
844 struct GNUNET_CADET_ConnectionCreateMessage,
845 NULL),
846 GNUNET_MQ_hd_fixed_size (connection_create_ack,
847 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK,
848 struct GNUNET_CADET_ConnectionCreateMessageAckMessage,
849 NULL),
850 GNUNET_MQ_hd_fixed_size (connection_broken,
851 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
852 struct GNUNET_CADET_ConnectionBrokenMessage,
853 NULL),
854 GNUNET_MQ_hd_fixed_size (connection_destroy,
855 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
856 struct GNUNET_CADET_ConnectionDestroyMessage,
857 NULL),
858 GNUNET_MQ_hd_fixed_size (hop_by_hop_encrypted_ack,
859 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK,
860 struct GNUNET_CADET_ConnectionEncryptedAckMessage,
861 NULL),
862 GNUNET_MQ_hd_fixed_size (poll,
863 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL,
864 struct GNUNET_CADET_ConnectionHopByHopPollMessage,
865 NULL),
866 GNUNET_MQ_hd_fixed_size (tunnel_kx,
867 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX,
868 struct GNUNET_CADET_TunnelKeyExchangeMessage,
869 NULL),
870 GNUNET_MQ_hd_var_size (tunnel_encrypted,
871 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED,
872 struct GNUNET_CADET_TunnelEncryptedMessage,
873 NULL),
874 GNUNET_MQ_handler_end ()
875 };
876
877 routes = GNUNET_CONTAINER_multishortmap_create (1024,
878 GNUNET_NO);
879 core = GNUNET_CORE_connect (c,
880 NULL,
881 &core_init_cb,
882 &core_connect_cb,
883 &core_disconnect_cb,
884 handlers);
885}
886
887
888/**
889 * Shut down the CORE subsystem.
890 */
891void
892GCO_shutdown ()
893{
894 if (NULL != core)
895 {
896 GNUNET_CORE_disconnect (core);
897 core = NULL;
898 }
899 GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (routes));
900 GNUNET_CONTAINER_multishortmap_destroy (routes);
901}
902
903/* end of gnunet-cadet-service_core.c */
diff --git a/src/cadet/gnunet-service-cadet-new_core.h b/src/cadet/gnunet-service-cadet-new_core.h
new file mode 100644
index 000000000..65b0a6ba5
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new_core.h
@@ -0,0 +1,69 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2017 GNUnet e.V.
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21/**
22 * @file cadet/gnunet-service-cadet_core.h
23 * @brief cadet service; interaction with CORE service
24 * @author Bartlomiej Polot
25 * @author Christian Grothoff
26 *
27 * All functions in this file should use the prefix GCO (Gnunet Cadet cOre (bottom))
28 */
29
30#ifndef GNUNET_SERVICE_CADET_CORE_H
31#define GNUNET_SERVICE_CADET_CORE_H
32
33#ifdef __cplusplus
34extern "C"
35{
36#if 0 /* keep Emacsens' auto-indent happy */
37}
38#endif
39#endif
40
41#include "gnunet_util_lib.h"
42
43
44/**
45 * Initialize the CORE subsystem.
46 *
47 * @param c Configuration.
48 */
49void
50GCO_init (const struct GNUNET_CONFIGURATION_Handle *c);
51
52
53/**
54 * Shut down the CORE subsystem.
55 */
56void
57GCO_shutdown (void);
58
59
60#if 0 /* keep Emacsens' auto-indent happy */
61{
62#endif
63#ifdef __cplusplus
64}
65#endif
66
67/* ifndef GNUNET_CADET_SERVICE_CORE_H */
68#endif
69/* end of gnunet-cadet-service_core.h */
diff --git a/src/cadet/gnunet-service-cadet-new_dht.c b/src/cadet/gnunet-service-cadet-new_dht.c
new file mode 100644
index 000000000..3cf3f0301
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new_dht.c
@@ -0,0 +1,315 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2013, 2017 GNUnet e.V.
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20/**
21 * @file cadet/gnunet-service-cadet-new_dht.c
22 * @brief Information we track per peer.
23 * @author Bartlomiej Polot
24 * @author Christian Grothoff
25 */
26
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include "gnunet_dht_service.h"
30#include "gnunet_statistics_service.h"
31#include "gnunet-service-cadet-new.h"
32#include "gnunet-service-cadet-new_dht.h"
33#include "gnunet-service-cadet-new_hello.h"
34#include "gnunet-service-cadet-new_peer.h"
35#include "gnunet-service-cadet-new_paths.h"
36
37#define LOG(level, ...) GNUNET_log_from (level,"cadet-dht",__VA_ARGS__)
38
39
40/**
41 * Handle for DHT searches.
42 */
43struct GCD_search_handle
44{
45 /**
46 * DHT_GET handle.
47 */
48 struct GNUNET_DHT_GetHandle *dhtget;
49
50};
51
52
53/**
54 * Handle to use DHT.
55 */
56static struct GNUNET_DHT_Handle *dht_handle;
57
58/**
59 * How often to PUT own ID in the DHT.
60 */
61static struct GNUNET_TIME_Relative id_announce_time;
62
63/**
64 * DHT replication level, see DHT API: #GNUNET_DHT_get_start(), #GNUNET_DHT_put().
65 */
66static unsigned long long dht_replication_level;
67
68/**
69 * Task to periodically announce itself in the network.
70 */
71static struct GNUNET_SCHEDULER_Task *announce_id_task;
72
73/**
74 * Delay for the next ID announce.
75 */
76static struct GNUNET_TIME_Relative announce_delay;
77
78
79/**
80 * Function to process paths received for a new peer addition. The recorded
81 * paths form the initial tunnel, which can be optimized later.
82 * Called on each result obtained for the DHT search.
83 *
84 * @param cls closure
85 * @param exp when will this value expire
86 * @param key key of the result
87 * @param get_path path of the get request
88 * @param get_path_length lenght of @a get_path
89 * @param put_path path of the put request
90 * @param put_path_length length of the @a put_path
91 * @param type type of the result
92 * @param size number of bytes in data
93 * @param data pointer to the result data
94 */
95static void
96dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
97 const struct GNUNET_HashCode *key,
98 const struct GNUNET_PeerIdentity *get_path,
99 unsigned int get_path_length,
100 const struct GNUNET_PeerIdentity *put_path,
101 unsigned int put_path_length,
102 enum GNUNET_BLOCK_Type type,
103 size_t size,
104 const void *data)
105{
106 const struct GNUNET_HELLO_Message *hello = data;
107 struct CadetPeer *peer;
108
109 GCPP_try_path_from_dht (get_path,
110 get_path_length,
111 put_path,
112 put_path_length);
113 if ( (size >= sizeof (struct GNUNET_HELLO_Message)) &&
114 (ntohs (hello->header.size) == size) &&
115 (size == GNUNET_HELLO_size (hello)) )
116 {
117 peer = GCP_get (&put_path[0],
118 GNUNET_YES);
119 LOG (GNUNET_ERROR_TYPE_DEBUG,
120 "Got HELLO for %s\n",
121 GCP_2s (peer));
122 GCP_set_hello (peer,
123 hello);
124 }
125}
126
127
128/**
129 * Periodically announce self id in the DHT
130 *
131 * @param cls closure
132 */
133static void
134announce_id (void *cls)
135{
136 struct GNUNET_HashCode phash;
137 const struct GNUNET_HELLO_Message *hello;
138 size_t size;
139 struct GNUNET_TIME_Absolute expiration;
140 struct GNUNET_TIME_Relative next_put;
141
142 hello = GCH_get_mine ();
143 size = (NULL != hello) ? GNUNET_HELLO_size (hello) : 0;
144 if (0 == size)
145 {
146 expiration = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
147 announce_delay);
148 announce_delay = GNUNET_TIME_STD_BACKOFF (announce_delay);
149 }
150 else
151 {
152 expiration = GNUNET_HELLO_get_last_expiration (hello);
153 announce_delay = GNUNET_TIME_UNIT_SECONDS;
154 }
155
156 /* Call again in id_announce_time, unless HELLO expires first,
157 * but wait at least 1s. */
158 next_put
159 = GNUNET_TIME_absolute_get_remaining (expiration);
160 next_put
161 = GNUNET_TIME_relative_min (next_put,
162 id_announce_time);
163 next_put
164 = GNUNET_TIME_relative_max (next_put,
165 GNUNET_TIME_UNIT_SECONDS);
166 announce_id_task
167 = GNUNET_SCHEDULER_add_delayed (next_put,
168 &announce_id,
169 cls);
170 GNUNET_STATISTICS_update (stats,
171 "# DHT announce",
172 1,
173 GNUNET_NO);
174 memset (&phash,
175 0,
176 sizeof (phash));
177 GNUNET_memcpy (&phash,
178 &my_full_id,
179 sizeof (my_full_id));
180 LOG (GNUNET_ERROR_TYPE_DEBUG,
181 "Announcing my HELLO (%u bytes) in the DHT\n",
182 size);
183 GNUNET_DHT_put (dht_handle, /* DHT handle */
184 &phash, /* Key to use */
185 dht_replication_level, /* Replication level */
186 GNUNET_DHT_RO_RECORD_ROUTE
187 | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
188 GNUNET_BLOCK_TYPE_DHT_HELLO, /* Block type */
189 size, /* Size of the data */
190 (const char *) hello, /* Data itself */
191 expiration, /* Data expiration */
192 NULL, /* Continuation */
193 NULL); /* Continuation closure */
194}
195
196
197/**
198 * Initialize the DHT subsystem.
199 *
200 * @param c Configuration.
201 */
202void
203GCD_init (const struct GNUNET_CONFIGURATION_Handle *c)
204{
205 LOG (GNUNET_ERROR_TYPE_DEBUG,
206 "init\n");
207 if (GNUNET_OK !=
208 GNUNET_CONFIGURATION_get_value_number (c,
209 "CADET",
210 "DHT_REPLICATION_LEVEL",
211 &dht_replication_level))
212 {
213 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
214 "CADET",
215 "DHT_REPLICATION_LEVEL",
216 "USING DEFAULT");
217 dht_replication_level = 3;
218 }
219
220 if (GNUNET_OK !=
221 GNUNET_CONFIGURATION_get_value_time (c,
222 "CADET",
223 "ID_ANNOUNCE_TIME",
224 &id_announce_time))
225 {
226 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
227 "CADET",
228 "ID_ANNOUNCE_TIME",
229 "MISSING");
230 GNUNET_SCHEDULER_shutdown ();
231 return;
232 }
233
234 dht_handle = GNUNET_DHT_connect (c,
235 64);
236 GNUNET_break (NULL != dht_handle);
237 announce_delay = GNUNET_TIME_UNIT_SECONDS;
238 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id,
239 NULL);
240}
241
242
243/**
244 * Shut down the DHT subsystem.
245 */
246void
247GCD_shutdown (void)
248{
249 if (NULL != dht_handle)
250 {
251 GNUNET_DHT_disconnect (dht_handle);
252 dht_handle = NULL;
253 }
254 if (NULL != announce_id_task)
255 {
256 GNUNET_SCHEDULER_cancel (announce_id_task);
257 announce_id_task = NULL;
258 }
259}
260
261
262/**
263 * Search DHT for paths to @a peeR_id
264 *
265 * @param peer_id peer to search for
266 * @return handle to abort search
267 */
268struct GCD_search_handle *
269GCD_search (const struct GNUNET_PeerIdentity *peer_id)
270{
271 struct GNUNET_HashCode phash;
272 struct GCD_search_handle *h;
273
274 LOG (GNUNET_ERROR_TYPE_DEBUG,
275 "Starting DHT GET for peer %s\n",
276 GNUNET_i2s (peer_id));
277 GNUNET_STATISTICS_update (stats,
278 "# DHT search",
279 1,
280 GNUNET_NO);
281 memset (&phash,
282 0,
283 sizeof (phash));
284 GNUNET_memcpy (&phash,
285 peer_id,
286 sizeof (*peer_id));
287
288 h = GNUNET_new (struct GCD_search_handle);
289 h->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
290 GNUNET_BLOCK_TYPE_DHT_HELLO, /* type */
291 &phash, /* key to search */
292 dht_replication_level, /* replication level */
293 GNUNET_DHT_RO_RECORD_ROUTE |
294 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
295 NULL, /* xquery */
296 0, /* xquery bits */
297 &dht_get_id_handler,
298 h);
299 return h;
300}
301
302
303/**
304 * Stop DHT search started with #GCD_search().
305 *
306 * @param h handle to search to stop
307 */
308void
309GCD_search_stop (struct GCD_search_handle *h)
310{
311 GNUNET_DHT_get_stop (h->dhtget);
312 GNUNET_free (h);
313}
314
315/* end of gnunet-service-cadet_dht.c */
diff --git a/src/cadet/gnunet-service-cadet-new_dht.h b/src/cadet/gnunet-service-cadet-new_dht.h
new file mode 100644
index 000000000..81f16ae99
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new_dht.h
@@ -0,0 +1,93 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2013, 2017 GNUnet e.V.
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21/**
22 * @file cadet/gnunet-service-cadet_dht.h
23 * @brief cadet service; dealing with DHT requests and results
24 * @author Bartlomiej Polot
25 * @author Christian Grothoff
26 *
27 * All functions in this file should use the prefix GCD (Gnunet Cadet Dht)
28 */
29#ifndef GNUNET_SERVICE_CADET_DHT_H
30#define GNUNET_SERVICE_CADET_DHT_H
31
32#ifdef __cplusplus
33extern "C"
34{
35#if 0 /* keep Emacsens' auto-indent happy */
36}
37#endif
38#endif
39
40#include "platform.h"
41#include "gnunet_util_lib.h"
42
43/**
44 * Handle for DHT search operation.
45 */
46struct GCD_search_handle;
47
48
49/**
50 * Initialize the DHT subsystem.
51 *
52 * @param c Configuration.
53 */
54void
55GCD_init (const struct GNUNET_CONFIGURATION_Handle *c);
56
57
58/**
59 * Shut down the DHT subsystem.
60 */
61void
62GCD_shutdown (void);
63
64
65/**
66 * Search DHT for paths to @a peeR_id
67 *
68 * @param peer_id peer to search for
69 * @return handle to abort search
70 */
71struct GCD_search_handle *
72GCD_search (const struct GNUNET_PeerIdentity *peer_id);
73
74
75/**
76 * Stop DHT search started with #GCD_search().
77 *
78 * @param h handle to search to stop
79 */
80void
81GCD_search_stop (struct GCD_search_handle *h);
82
83
84#if 0 /* keep Emacsens' auto-indent happy */
85{
86#endif
87#ifdef __cplusplus
88}
89#endif
90
91/* ifndef GNUNET_CADET_SERVICE_DHT_H */
92#endif
93/* end of gnunet-service-cadet_dht.h */
diff --git a/src/cadet/gnunet-service-cadet-new_hello.c b/src/cadet/gnunet-service-cadet-new_hello.c
new file mode 100644
index 000000000..9d4635021
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new_hello.c
@@ -0,0 +1,142 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2014, 2017 GNUnet e.V.
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "platform.h"
22#include "gnunet_util_lib.h"
23
24#include "gnunet_statistics_service.h"
25#include "gnunet_peerinfo_service.h"
26#include "cadet_protocol.h"
27#include "gnunet-service-cadet-new.h"
28#include "gnunet-service-cadet-new_hello.h"
29#include "gnunet-service-cadet-new_peer.h"
30
31#define LOG(level, ...) GNUNET_log_from(level,"cadet-hll",__VA_ARGS__)
32
33/**
34 * Hello message of local peer.
35 */
36static struct GNUNET_HELLO_Message *mine;
37
38/**
39 * Handle to peerinfo service.
40 */
41static struct GNUNET_PEERINFO_Handle *peerinfo;
42
43/**
44 * Iterator context.
45 */
46static struct GNUNET_PEERINFO_NotifyContext* nc;
47
48
49/**
50 * Process each hello message received from peerinfo.
51 *
52 * @param cls Closure (unused).
53 * @param peer Identity of the peer.
54 * @param hello Hello of the peer.
55 * @param err_msg Error message.
56 */
57static void
58got_hello (void *cls,
59 const struct GNUNET_PeerIdentity *id,
60 const struct GNUNET_HELLO_Message *hello,
61 const char *err_msg)
62{
63 struct CadetPeer *peer;
64
65 if ( (NULL == id) ||
66 (NULL == hello) )
67 return;
68 if (0 == memcmp (id,
69 &my_full_id,
70 sizeof (struct GNUNET_PeerIdentity)))
71 {
72 GNUNET_free_non_null (mine);
73 mine = (struct GNUNET_HELLO_Message *) GNUNET_copy_message (&hello->header);
74 return;
75 }
76
77 LOG (GNUNET_ERROR_TYPE_DEBUG,
78 "Hello for %s (%d bytes), expires on %s\n",
79 GNUNET_i2s (id),
80 GNUNET_HELLO_size (hello),
81 GNUNET_STRINGS_absolute_time_to_string (GNUNET_HELLO_get_last_expiration (hello)));
82 peer = GCP_get (id,
83 GNUNET_YES);
84 GCP_set_hello (peer,
85 hello);
86
87}
88
89
90/**
91 * Initialize the hello subsystem.
92 *
93 * @param c Configuration.
94 */
95void
96GCH_init (const struct GNUNET_CONFIGURATION_Handle *c)
97{
98 GNUNET_assert (NULL == nc);
99 peerinfo = GNUNET_PEERINFO_connect (c);
100 nc = GNUNET_PEERINFO_notify (c,
101 GNUNET_NO,
102 &got_hello,
103 NULL);
104}
105
106
107/**
108 * Shut down the hello subsystem.
109 */
110void
111GCH_shutdown ()
112{
113 if (NULL != nc)
114 {
115 GNUNET_PEERINFO_notify_cancel (nc);
116 nc = NULL;
117 }
118 if (NULL != peerinfo)
119 {
120 GNUNET_PEERINFO_disconnect (peerinfo);
121 peerinfo = NULL;
122 }
123 if (NULL != mine)
124 {
125 GNUNET_free (mine);
126 mine = NULL;
127 }
128}
129
130
131/**
132 * Get own hello message.
133 *
134 * @return Own hello message.
135 */
136const struct GNUNET_HELLO_Message *
137GCH_get_mine (void)
138{
139 return mine;
140}
141
142/* end of gnunet-service-cadet-new_hello.c */
diff --git a/src/cadet/gnunet-service-cadet-new_hello.h b/src/cadet/gnunet-service-cadet-new_hello.h
new file mode 100644
index 000000000..4291ae985
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new_hello.h
@@ -0,0 +1,80 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2014, 2017 GNUnet e.V.
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21/**
22 * @file cadet/gnunet-service-cadet_hello.h
23 * @brief cadet service; dealing with hello messages
24 * @author Bartlomiej Polot
25 * @author Christian Grothoff
26 *
27 * All functions in this file should use the prefix GCH (Gnunet Cadet Hello)
28 */
29
30#ifndef GNUNET_SERVICE_CADET_HELLO_H
31#define GNUNET_SERVICE_CADET_HELLO_H
32
33#ifdef __cplusplus
34extern "C"
35{
36#if 0 /* keep Emacsens' auto-indent happy */
37}
38#endif
39#endif
40
41#include "platform.h"
42#include "gnunet_util_lib.h"
43#include "gnunet_hello_lib.h"
44
45
46/**
47 * Initialize the hello subsystem.
48 *
49 * @param c Configuration.
50 */
51void
52GCH_init (const struct GNUNET_CONFIGURATION_Handle *c);
53
54
55/**
56 * Shut down the hello subsystem.
57 */
58void
59GCH_shutdown (void);
60
61
62/**
63 * Get own hello message.
64 *
65 * @return Own hello message.
66 */
67const struct GNUNET_HELLO_Message *
68GCH_get_mine (void);
69
70
71#if 0 /* keep Emacsens' auto-indent happy */
72{
73#endif
74#ifdef __cplusplus
75}
76#endif
77
78/* ifndef GNUNET_CADET_SERVICE_HELLO_H */
79#endif
80/* end of gnunet-cadet-service_hello.h */
diff --git a/src/cadet/gnunet-service-cadet-new_paths.c b/src/cadet/gnunet-service-cadet-new_paths.c
new file mode 100644
index 000000000..3cfce337c
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new_paths.c
@@ -0,0 +1,557 @@
1
2/*
3 This file is part of GNUnet.
4 Copyright (C) 2001-2017 GNUnet e.V.
5
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
10
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22/**
23 * @file cadet/gnunet-service-cadet-new_paths.c
24 * @brief Information we track per path.
25 * @author Bartlomiej Polot
26 * @author Christian Grothoff
27 *
28 * TODO:
29 * - path desirability score calculations are not done
30 * (and will be tricky to have during path changes)
31 */
32#include "platform.h"
33#include "gnunet-service-cadet-new_peer.h"
34#include "gnunet-service-cadet-new_paths.h"
35
36
37/**
38 * Information regarding a possible path to reach a peer.
39 */
40struct CadetPeerPath
41{
42
43 /**
44 * Array of all the peers on the path. If @e hn is non-NULL, the
45 * last one is our owner.
46 */
47 struct CadetPeerPathEntry *entries;
48
49 /**
50 * Node of this path in the owner's heap. Used to update our position
51 * in the heap whenever our @e desirability changes.
52 */
53 struct GNUNET_CONTAINER_HeapNode *hn;
54
55 /**
56 * Connections using this path, by destination peer
57 * (each hop of the path could correspond to an
58 * active connection).
59 */
60 struct GNUNET_CONTAINER_MultiPeerMap *connections;
61
62 /**
63 * Desirability of the path. How unique is it for the various peers
64 * on it?
65 */
66 GNUNET_CONTAINER_HeapCostType desirability;
67
68 /**
69 * Length of the @e entries array.
70 */
71 unsigned int entries_length;
72
73};
74
75
76/**
77 * Return how much we like keeping the path. This is an aggregate
78 * score based on various factors, including the age of the path
79 * (older == better), and the value of this path to all of its ajacent
80 * peers. For example, long paths that end at a peer that we have no
81 * shorter way to reach are very desirable, while long paths that end
82 * at a peer for which we have a shorter way as well are much less
83 * desirable. Higher values indicate more valuable paths. The
84 * returned value should be used to decide which paths to remember.
85 *
86 * @param path path to return the length for
87 * @return desirability of the path, larger is more desirable
88 */
89GNUNET_CONTAINER_HeapCostType
90GCPP_get_desirability (const struct CadetPeerPath *path)
91{
92 return path->desirability;
93}
94
95
96/**
97 * Return connection to @a destination using @a path, or return
98 * NULL if no such connection exists.
99 *
100 * @param path path to traverse
101 * @param destination destination node to get to, must be on path
102 * @param off offset of @a destination on @a path
103 * @return NULL if we have no existing connection
104 * otherwise connection from us to @a destination via @a path
105 */
106struct CadetConnection *
107GCPP_get_connection (struct CadetPeerPath *path,
108 struct CadetPeer *destination,
109 unsigned int off)
110{
111 struct CadetPeerPathEntry *entry;
112
113 GNUNET_assert (off < path->entries_length);
114 entry = &path->entries[off];
115 GNUNET_assert (entry->peer == destination);
116 return entry->cc;
117}
118
119
120/**
121 * Notify @a path that it is used for connection @a cc
122 * which ends at the path's offset @a off.
123 *
124 * @param path the path to remember the @a cc
125 * @param off the offset where the @a cc ends
126 * @param cc the connection to remember
127 */
128void
129GCPP_add_connection (struct CadetPeerPath *path,
130 unsigned int off,
131 struct CadetConnection *cc)
132{
133 struct CadetPeerPathEntry *entry;
134
135 GNUNET_assert (off < path->entries_length);
136 entry = &path->entries[off];
137 GNUNET_assert (NULL == entry->cc);
138 entry->cc = cc;
139}
140
141
142
143/**
144 * Notify @a path that it is no longer used for connection @a cc which
145 * ended at the path's offset @a off.
146 *
147 * @param path the path to forget the @a cc
148 * @param off the offset where the @a cc ended
149 * @param cc the connection to forget
150 */
151void
152GCPP_del_connection (struct CadetPeerPath *path,
153 unsigned int off,
154 struct CadetConnection *cc)
155{
156 struct CadetPeerPathEntry *entry;
157
158 GNUNET_assert (off < path->entries_length);
159 entry = &path->entries[off];
160 GNUNET_assert (cc == entry->cc);
161 entry->cc = NULL;
162}
163
164
165/**
166 * This path is no longer needed, free resources.
167 *
168 * @param path path resources to free
169 */
170static void
171path_destroy (struct CadetPeerPath *path)
172{
173 GNUNET_assert (0 ==
174 GNUNET_CONTAINER_multipeermap_size (path->connections));
175 GNUNET_CONTAINER_multipeermap_destroy (path->connections);
176 GNUNET_free (path->entries);
177 GNUNET_free (path);
178}
179
180
181/**
182 * The owning peer of this path is no longer interested in maintaining
183 * it, so the path should be discarded or shortened (in case a
184 * previous peer on the path finds the path desirable).
185 *
186 * @param path the path that is being released
187 */
188void
189GCPP_release (struct CadetPeerPath *path)
190{
191 struct CadetPeerPathEntry *entry;
192
193 path->hn = NULL;
194 entry = &path->entries[path->entries_length - 1];
195 while (1)
196 {
197 /* cut 'off' end of path, verifying it is not in use */
198 GNUNET_assert (NULL ==
199 GNUNET_CONTAINER_multipeermap_get (path->connections,
200 GCP_get_id (entry->peer)));
201 GCP_path_entry_remove (entry->peer,
202 entry,
203 path->entries_length - 1);
204 path->entries_length--; /* We don't bother shrinking the 'entries' array,
205 as it's probably not worth it. */
206 if (0 == path->entries_length)
207 break; /* the end */
208
209 /* see if new peer at the end likes this path any better */
210 entry = &path->entries[path->entries_length - 1];
211 path->hn = GCP_attach_path (entry->peer,
212 path,
213 path->entries_length);
214 if (NULL != path->hn)
215 return; /* yep, got attached, we are done. */
216 }
217
218 /* nobody wants us, discard the path */
219 path_destroy (path);
220}
221
222
223/**
224 * Updates the score for an entry on the path based
225 * on our experiences with using @a path.
226 *
227 * @param path the path to update
228 * @param off offset of the entry to update
229 * @param delta change in the score to apply
230 */
231void
232GCPP_update_score (struct CadetPeerPath *path,
233 unsigned int off,
234 int delta)
235{
236 struct CadetPeerPathEntry *entry;
237
238 GNUNET_assert (off < path->entries_length);
239 entry = &path->entries[off];
240
241 /* Add delta, with checks for overflows */
242 if (delta >= 0)
243 {
244 if (delta + entry->score < entry->score)
245 entry->score = INT_MAX;
246 else
247 entry->score += delta;
248 }
249 else
250 {
251 if (delta + entry->score > entry->score)
252 entry->score = INT_MIN;
253 else
254 entry->score += delta;
255 }
256
257 /* FIXME: update path desirability! */
258}
259
260
261/**
262 * Closure for #find_peer_at() and #check_match().
263 */
264struct CheckMatchContext
265{
266
267 /**
268 * Set to a matching path, if any.
269 */
270 struct CadetPeerPath *match;
271
272 /**
273 * Array the combined paths.
274 */
275 struct CadetPeer **cpath;
276
277};
278
279
280/**
281 * Check if the given path is identical on all of the
282 * hops until @a off, and not longer than @a off. If the
283 * @a path matches, store it in `match`.
284 *
285 * @param cls the `struct CheckMatchContext` to check against
286 * @param path the path to check
287 * @param off offset to check at
288 * @return #GNUNET_YES (continue to iterate), or if found #GNUNET_NO
289 */
290static int
291check_match (void *cls,
292 struct CadetPeerPath *path,
293 unsigned int off)
294{
295 struct CheckMatchContext *cm_ctx = cls;
296
297 if (path->entries_length > off)
298 return GNUNET_YES; /* too long, cannot be useful */
299 for (unsigned int i=0;i<off;i++)
300 if (cm_ctx->cpath[i] !=
301 GCPP_get_peer_at_offset (path,
302 i))
303 return GNUNET_YES; /* missmatch, ignore */
304 cm_ctx->match = path;
305 return GNUNET_NO; /* match, we are done! */
306}
307
308
309/**
310 * Extend path @a path by the @a num_peers from the @a peers
311 * array, assuming the owners past the current owner want it.
312 *
313 * @param path path to extend
314 * @param peers list of peers beyond the end of @a path
315 * @param num_peers length of the @a peers array
316 */
317static void
318extend_path (struct CadetPeerPath *path,
319 struct CadetPeer **peers,
320 unsigned int num_peers)
321{
322 unsigned int old_len = path->entries_length;
323 struct GNUNET_CONTAINER_HeapNode *hn;
324 int i;
325
326 /* If we extend an existing path, detach it from the
327 old owner and re-attach to the new one */
328 hn = NULL;
329 for (i=num_peers-1;i>=0;i--)
330 {
331 /* FIXME: note that path->desirability is used, but not yet updated here! */
332 hn = GCP_attach_path (peers[i],
333 path,
334 old_len + (unsigned int) i);
335 if (NULL != hn)
336 break;
337 }
338 if (NULL == hn)
339 return; /* none of the peers is interested in this path */
340 GCP_detach_path (path->entries[old_len-1].peer,
341 path,
342 path->hn);
343 path->hn = hn;
344 GNUNET_array_grow (path->entries,
345 path->entries_length,
346 old_len + i);
347 for (;i >= 0;i--)
348 {
349 struct CadetPeerPathEntry *entry = &path->entries[old_len + i];
350
351 entry->peer = peers[i];
352 entry->path = path;
353 GCP_path_entry_add (entry->peer,
354 entry,
355 old_len + i);
356 }
357}
358
359
360/**
361 * Create a peer path based on the result of a DHT lookup. If we
362 * already know this path, or one that is longer, simply return NULL.
363 * Otherwise, we try to extend an existing path, or create a new one
364 * if applicable.
365 *
366 * @param get_path path of the get request
367 * @param get_path_length lenght of @a get_path
368 * @param put_path path of the put request
369 * @param put_path_length length of the @a put_path
370 * @return a path through the network
371 */
372void
373GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
374 unsigned int get_path_length,
375 const struct GNUNET_PeerIdentity *put_path,
376 unsigned int put_path_length)
377{
378 struct CheckMatchContext cm_ctx;
379 struct CadetPeer *cpath[get_path_length + put_path_length];
380 struct CadetPeerPath *path;
381 struct GNUNET_CONTAINER_HeapNode *hn;
382 int i;
383
384 /* precompute 'cpath' so we can avoid doing the lookups lots of times */
385 for (unsigned int off=0;off<get_path_length + put_path_length;off++)
386 {
387 const struct GNUNET_PeerIdentity *pid;
388
389 pid = (off < get_path_length)
390 ? &get_path[get_path_length - off]
391 : &put_path[get_path_length + put_path_length - off];
392 cpath[off] = GCP_get (pid,
393 GNUNET_YES);
394 }
395
396 /* First figure out if this path is a subset of an existing path, an
397 extension of an existing path, or a new path. */
398 cm_ctx.cpath = cpath;
399 cm_ctx.match = NULL;
400 for (i=get_path_length + put_path_length-1;i>=0;i--)
401 {
402 GCP_iterate_paths_at (cpath[i],
403 (unsigned int) i,
404 &check_match,
405 &cm_ctx);
406 if (NULL != cm_ctx.match)
407 {
408 if (i == get_path_length + put_path_length - 1)
409 {
410 /* Existing path includes this one, nothing to do! */
411 return;
412 }
413 if (cm_ctx.match->entries_length == i + 1)
414 {
415 /* Existing path ends in the middle of new path, extend it! */
416 extend_path (cm_ctx.match,
417 &cpath[i],
418 get_path_length + put_path_length - i);
419 return;
420 }
421 }
422 }
423
424 /* No match at all, create completely new path */
425 path = GNUNET_new (struct CadetPeerPath);
426
427 /* First, try to attach it */
428 hn = NULL;
429 for (i=get_path_length + put_path_length-1;i>=0;i--)
430 {
431 path->entries_length = i;
432 /* FIXME: note that path->desirability is used, but not yet initialized here! */
433 hn = GCP_attach_path (cpath[i],
434 path,
435 (unsigned int) i);
436 if (NULL != hn)
437 break;
438 }
439 if (NULL == hn)
440 {
441 /* None of the peers on the path care about it. */
442 GNUNET_free (path);
443 return;
444 }
445 path->hn = hn;
446 path->entries_length = i;
447 path->entries = GNUNET_new_array (path->entries_length,
448 struct CadetPeerPathEntry);
449 for (;i>=0;i--)
450 {
451 struct CadetPeerPathEntry *entry = &path->entries[i];
452
453 entry->peer = cpath[i];
454 entry->path = path;
455 GCP_path_entry_add (entry->peer,
456 entry,
457 i);
458 }
459}
460
461
462/**
463 * We got an incoming connection, obtain the corresponding path.
464 *
465 * @param path_length number of segments on the @a path
466 * @param path through the network, in reverse order (we are at the end!)
467 * @return corresponding path object
468 */
469struct CadetPeerPath *
470GCPP_get_path_from_route (unsigned int path_length,
471 const struct GNUNET_PeerIdentity *pids)
472{
473 GNUNET_assert (0); // FIXME!
474 return NULL;
475}
476
477
478/**
479 * Return the length of the path. Excludes one end of the
480 * path, so the loopback path has length 0.
481 *
482 * @param path path to return the length for
483 * @return number of peers on the path
484 */
485unsigned int
486GCPP_get_length (struct CadetPeerPath *path)
487{
488 return path->entries_length;
489}
490
491
492/**
493 * Find peer's offset on path.
494 *
495 * @param path path to search
496 * @param cp peer to look for
497 * @return offset of @a cp on @a path, or UINT_MAX if not found
498 */
499unsigned int
500GCPP_find_peer (struct CadetPeerPath *path,
501 struct CadetPeer *cp)
502{
503 for (unsigned int off = 0;
504 off < path->entries_length;
505 off++)
506 if (cp == GCPP_get_peer_at_offset (path,
507 off))
508 return off;
509 return UINT_MAX;
510}
511
512
513/**
514 * Obtain the peer at offset @a off in @a path.
515 *
516 * @param path peer path to inspect
517 * @param off offset to return, must be smaller than path length
518 * @return the peer at offset @a off
519 */
520struct CadetPeer *
521GCPP_get_peer_at_offset (struct CadetPeerPath *path,
522 unsigned int off)
523{
524 return path->entries[off].peer;
525}
526
527
528/**
529 * Convert a path to a human-readable string.
530 *
531 * @param path path to convert
532 * @return string, to be freed by caller (unlike other *_2s APIs!)
533 */
534char *
535GCPP_2s (struct CadetPeerPath *path)
536{
537 char *s;
538 char *old;
539
540 old = GNUNET_strdup ("");
541 for (unsigned int i = 0;
542 i < path->entries_length;
543 i++)
544 {
545 GNUNET_asprintf (&s,
546 "%s %s",
547 old,
548 GCP_2s (GCPP_get_peer_at_offset (path,
549 i)));
550 GNUNET_free_non_null (old);
551 old = s;
552 }
553 return old;
554}
555
556
557/* end of gnunet-service-cadet-new_paths.c */
diff --git a/src/cadet/gnunet-service-cadet-new_paths.h b/src/cadet/gnunet-service-cadet-new_paths.h
new file mode 100644
index 000000000..5714368c7
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new_paths.h
@@ -0,0 +1,182 @@
1
2/*
3 This file is part of GNUnet.
4 Copyright (C) 2001-2017 GNUnet e.V.
5
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
10
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22/**
23 * @file cadet/gnunet-service-cadet-new_paths.h
24 * @brief Information we track per path.
25 * @author Bartlomiej Polot
26 * @author Christian Grothoff
27 */
28#ifndef GNUNET_SERVICE_CADET_PATHS_H
29#define GNUNET_SERVICE_CADET_PATHS_H
30
31#include "gnunet_util_lib.h"
32#include "gnunet-service-cadet-new.h"
33
34/**
35 * Create a peer path based on the result of a DHT lookup. If we
36 * already know this path, or one that is longer, simply return NULL.
37 * Otherwise, we try to extend an existing path, or create a new one
38 * if applicable.
39 *
40 * @param get_path path of the get request
41 * @param get_path_length lenght of @a get_path
42 * @param put_path path of the put request
43 * @param put_path_length length of the @a put_path
44 */
45void
46GCPP_try_path_from_dht (const struct GNUNET_PeerIdentity *get_path,
47 unsigned int get_path_length,
48 const struct GNUNET_PeerIdentity *put_path,
49 unsigned int put_path_length);
50
51
52/**
53 * We got an incoming connection, obtain the corresponding path.
54 *
55 * @param path_length number of segments on the @a path
56 * @param path through the network, in reverse order (we are at the end!)
57 * @return corresponding path object
58 */
59struct CadetPeerPath *
60GCPP_get_path_from_route (unsigned int path_length,
61 const struct GNUNET_PeerIdentity *pids);
62
63
64/**
65 * Return the length of the path. Excludes one end of the
66 * path, so the loopback path has length 0.
67 *
68 * @param path path to return the length for
69 * @return number of peers on the path
70 */
71unsigned int
72GCPP_get_length (struct CadetPeerPath *path);
73
74
75/**
76 * Return connection to @a destination using @a path, or return
77 * NULL if no such connection exists.
78 *
79 * @param path path to traverse
80 * @param destination destination node to get to, must be on path
81 * @param off offset of @a destination on @a path
82 * @return NULL if we have no existing connection
83 * otherwise connection from us to @a destination via @a path
84 */
85struct CadetConnection *
86GCPP_get_connection (struct CadetPeerPath *path,
87 struct CadetPeer *destination,
88 unsigned int off);
89
90
91/**
92 * Notify @a path that it is used for connection @a cc
93 * which ends at the path's offset @a off.
94 *
95 * @param path the path to remember the @a cc
96 * @param off the offset where the @a cc ends
97 * @param cc the connection to remember
98 */
99void
100GCPP_add_connection (struct CadetPeerPath *path,
101 unsigned int off,
102 struct CadetConnection *cc);
103
104
105/**
106 * Notify @a path that it is no longer used for connection @a cc which
107 * ended at the path's offset @a off.
108 *
109 * @param path the path to forget the @a cc
110 * @param off the offset where the @a cc ended
111 * @param cc the connection to forget
112 */
113void
114GCPP_del_connection (struct CadetPeerPath *path,
115 unsigned int off,
116 struct CadetConnection *cc);
117
118
119/**
120 * Find peer's offset on path.
121 *
122 * @param path path to search
123 * @param cp peer to look for
124 * @return offset of @a cp on @a path, or UINT_MAX if not found
125 */
126unsigned int
127GCPP_find_peer (struct CadetPeerPath *path,
128 struct CadetPeer *cp);
129
130
131/**
132 * Return how much we like keeping the path. This is an aggregate
133 * score based on various factors, including the age of the path
134 * (older == better), and the value of this path to all of its ajacent
135 * peers. For example, long paths that end at a peer that we have no
136 * shorter way to reach are very desirable, while long paths that end
137 * at a peer for which we have a shorter way as well are much less
138 * desirable. Higher values indicate more valuable paths. The
139 * returned value should be used to decide which paths to remember.
140 *
141 * @param path path to return the length for
142 * @return desirability of the path, larger is more desirable
143 */
144GNUNET_CONTAINER_HeapCostType
145GCPP_get_desirability (const struct CadetPeerPath *path);
146
147
148/**
149 * The given peer @a cp used to own this @a path. However, it is no
150 * longer interested in maintaining it, so the path should be
151 * discarded or shortened (in case a previous peer on the path finds
152 * the path desirable).
153 *
154 * @param path the path that is being released
155 */
156void
157GCPP_release (struct CadetPeerPath *path);
158
159
160/**
161 * Obtain the peer at offset @a off in @a path.
162 *
163 * @param path peer path to inspect
164 * @param off offset to return, must be smaller than path length
165 * @return peer at offset @a off
166 */
167struct CadetPeer *
168GCPP_get_peer_at_offset (struct CadetPeerPath *path,
169 unsigned int off);
170
171
172/**
173 * Convert a path to a human-readable string.
174 *
175 * @param path path to convert
176 * @return string, to be freed by caller (unlike other *_2s APIs!)
177 */
178char *
179GCPP_2s (struct CadetPeerPath *p);
180
181
182#endif
diff --git a/src/cadet/gnunet-service-cadet-new_peer.c b/src/cadet/gnunet-service-cadet-new_peer.c
new file mode 100644
index 000000000..47f725e09
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new_peer.c
@@ -0,0 +1,1056 @@
1
2/*
3 This file is part of GNUnet.
4 Copyright (C) 2001-2017 GNUnet e.V.
5
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
10
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22/**
23 * @file cadet/gnunet-service-cadet-new_peer.c
24 * @brief Information we track per peer.
25 * @author Bartlomiej Polot
26 * @author Christian Grothoff
27 *
28 * TODO:
29 * - implement GCP_set_hello() / do HELLO advertising properly
30 * - optimize stopping/restarting DHT search to situations
31 * where we actually need it (i.e. not if we have a direct connection,
32 * or if we already have plenty of good short ones, or maybe even
33 * to take a break if we have some connections and have searched a lot (?))
34 * - optimize MQM ready scans (O(n) -> O(1))
35 */
36#include "platform.h"
37#include "gnunet_util_lib.h"
38#include "gnunet_signatures.h"
39#include "gnunet_transport_service.h"
40#include "gnunet_ats_service.h"
41#include "gnunet_core_service.h"
42#include "gnunet_statistics_service.h"
43#include "cadet_protocol.h"
44#include "cadet_path.h"
45#include "gnunet-service-cadet-new.h"
46#include "gnunet-service-cadet-new_connection.h"
47#include "gnunet-service-cadet-new_dht.h"
48#include "gnunet-service-cadet-new_peer.h"
49#include "gnunet-service-cadet-new_paths.h"
50#include "gnunet-service-cadet-new_tunnels.h"
51
52/**
53 * How long do we wait until tearing down an idle peer?
54 */
55#define IDLE_PEER_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
56
57/**
58 * How long do we keep paths around if we no longer care about the peer?
59 */
60#define IDLE_PATH_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 2)
61
62
63
64
65/**
66 * Data structure used to track whom we have to notify about changes
67 * to our message queue.
68 */
69struct GCP_MessageQueueManager
70{
71
72 /**
73 * Kept in a DLL.
74 */
75 struct GCP_MessageQueueManager *next;
76
77 /**
78 * Kept in a DLL.
79 */
80 struct GCP_MessageQueueManager *prev;
81
82 /**
83 * Function to call with updated message queue object.
84 */
85 GCP_MessageQueueNotificationCallback cb;
86
87 /**
88 * Closure for @e cb.
89 */
90 void *cb_cls;
91
92 /**
93 * The peer this is for.
94 */
95 struct CadetPeer *cp;
96
97 /**
98 * Envelope this manager would like to transmit once it is its turn.
99 */
100 struct GNUNET_MQ_Envelope *env;
101
102};
103
104
105/**
106 * Struct containing all information regarding a given peer
107 */
108struct CadetPeer
109{
110 /**
111 * ID of the peer
112 */
113 struct GNUNET_PeerIdentity pid;
114
115 /**
116 * Last time we heard from this peer
117 */
118 struct GNUNET_TIME_Absolute last_contact;
119
120 /**
121 * Array of DLLs of paths traversing the peer, organized by the
122 * offset of the peer on the larger path.
123 */
124 struct CadetPeerPathEntry **path_heads;
125
126 /**
127 * Array of DLL of paths traversing the peer, organized by the
128 * offset of the peer on the larger path.
129 */
130 struct CadetPeerPathEntry **path_tails;
131
132 /**
133 * Notifications to call when @e core_mq changes.
134 */
135 struct GCP_MessageQueueManager *mqm_head;
136
137 /**
138 * Notifications to call when @e core_mq changes.
139 */
140 struct GCP_MessageQueueManager *mqm_tail;
141
142 /**
143 * MIN-heap of paths owned by this peer (they also end at this
144 * peer). Ordered by desirability.
145 */
146 struct GNUNET_CONTAINER_Heap *path_heap;
147
148 /**
149 * Handle to stop the DHT search for paths to this peer
150 */
151 struct GCD_search_handle *search_h;
152
153 /**
154 * Task to stop the DHT search for paths to this peer
155 */
156 struct GNUNET_SCHEDULER_Task *search_delayedXXX;
157
158 /**
159 * Task to destroy this entry.
160 */
161 struct GNUNET_SCHEDULER_Task *destroy_task;
162
163 /**
164 * Tunnel to this peer, if any.
165 */
166 struct CadetTunnel *t;
167
168 /**
169 * Connections that go through this peer; indexed by tid.
170 */
171 struct GNUNET_CONTAINER_MultiShortmap *connections;
172
173 /**
174 * Handle for core transmissions.
175 */
176 struct GNUNET_MQ_Handle *core_mq;
177
178 /**
179 * Hello message of the peer.
180 */
181 struct GNUNET_HELLO_Message *hello;
182
183 /**
184 * Handle to us offering the HELLO to the transport.
185 */
186 struct GNUNET_TRANSPORT_OfferHelloHandle *hello_offer;
187
188 /**
189 * Handle to our ATS request asking ATS to suggest an address
190 * to TRANSPORT for this peer (to establish a direct link).
191 */
192 struct GNUNET_ATS_ConnectivitySuggestHandle *connectivity_suggestion;
193
194 /**
195 * How many messages are in the queue to this peer.
196 */
197 unsigned int queue_n;
198
199 /**
200 * How many paths do we have to this peer (in all @e path_heads DLLs combined).
201 */
202 unsigned int num_paths;
203
204 /**
205 * Number of message queue managers of this peer that have a message in waiting.
206 *
207 * Used to quickly see if we need to bother scanning the @e msm_head DLL.
208 * TODO: could be replaced by another DLL that would then allow us to avoid
209 * the O(n)-scan of the DLL for ready entries!
210 */
211 unsigned int mqm_ready_counter;
212
213 /**
214 * Current length of the @e path_heads and @path_tails arrays.
215 * The arrays should be grown as needed.
216 */
217 unsigned int path_dll_length;
218
219};
220
221
222/**
223 * Get the static string for a peer ID.
224 *
225 * @param peer Peer.
226 *
227 * @return Static string for it's ID.
228 */
229const char *
230GCP_2s (const struct CadetPeer *peer)
231{
232 if (NULL == peer)
233 return "PEER(NULL)";
234 return GNUNET_i2s (&peer->pid);
235}
236
237
238/**
239 * This peer is no longer be needed, clean it up now.
240 *
241 * @param cls peer to clean up
242 */
243static void
244destroy_peer (void *cls)
245{
246 struct CadetPeer *cp = cls;
247
248 cp->destroy_task = NULL;
249 GNUNET_assert (NULL == cp->t);
250 GNUNET_assert (NULL == cp->core_mq);
251 GNUNET_assert (0 == cp->path_dll_length);
252 GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (cp->connections));
253 GNUNET_assert (GNUNET_YES ==
254 GNUNET_CONTAINER_multipeermap_remove (peers,
255 &cp->pid,
256 cp));
257 GNUNET_free_non_null (cp->path_heads);
258 GNUNET_free_non_null (cp->path_tails);
259 cp->path_dll_length = 0;
260 if (NULL != cp->search_h)
261 {
262 GCD_search_stop (cp->search_h);
263 cp->search_h = NULL;
264 }
265 /* FIXME: clean up search_delayedXXX! */
266
267 if (NULL != cp->hello_offer)
268 {
269 GNUNET_TRANSPORT_offer_hello_cancel (cp->hello_offer);
270 cp->hello_offer = NULL;
271 }
272 if (NULL != cp->connectivity_suggestion)
273 {
274 GNUNET_ATS_connectivity_suggest_cancel (cp->connectivity_suggestion);
275 cp->connectivity_suggestion = NULL;
276 }
277 GNUNET_CONTAINER_multishortmap_destroy (cp->connections);
278 GNUNET_CONTAINER_heap_destroy (cp->path_heap);
279 GNUNET_free_non_null (cp->hello);
280 /* Peer should not be freed if paths exist; if there are no paths,
281 there ought to be no connections, and without connections, no
282 notifications. Thus we can assert that mqm_head is empty at this
283 point. */
284 GNUNET_assert (NULL == cp->mqm_head);
285 GNUNET_free (cp);
286}
287
288
289/**
290 * Set the message queue to @a mq for peer @a cp and notify watchers.
291 *
292 * @param cp peer to modify
293 * @param mq message queue to set (can be NULL)
294 */
295void
296GCP_set_mq (struct CadetPeer *cp,
297 struct GNUNET_MQ_Handle *mq)
298{
299 cp->core_mq = mq;
300
301 for (struct GCP_MessageQueueManager *mqm = cp->mqm_head;
302 NULL != mqm;
303 mqm = mqm->next)
304 {
305 if (NULL == mq)
306 {
307 if (NULL != mqm->env)
308 {
309 GNUNET_MQ_discard (mqm->env);
310 mqm->env = NULL;
311 mqm->cb (mqm->cb_cls,
312 GNUNET_SYSERR);
313 }
314 else
315 {
316 mqm->cb (mqm->cb_cls,
317 GNUNET_NO);
318 }
319 }
320 else
321 {
322 GNUNET_assert (NULL == mqm->env);
323 mqm->cb (mqm->cb_cls,
324 GNUNET_YES);
325 }
326 }
327}
328
329
330/**
331 * Transmit current envelope from this @a mqm.
332 *
333 * @param mqm mqm to transmit message for now
334 */
335static void
336mqm_execute (struct GCP_MessageQueueManager *mqm)
337{
338 struct CadetPeer *cp = mqm->cp;
339
340 /* Move entry to the end of the DLL, to be fair. */
341 if (mqm != cp->mqm_tail)
342 {
343 GNUNET_CONTAINER_DLL_remove (cp->mqm_head,
344 cp->mqm_tail,
345 mqm);
346 GNUNET_CONTAINER_DLL_insert_tail (cp->mqm_head,
347 cp->mqm_tail,
348 mqm);
349 }
350 GNUNET_MQ_send (cp->core_mq,
351 mqm->env);
352 mqm->env = NULL;
353 cp->mqm_ready_counter--;
354}
355
356
357/**
358 * Function called when CORE took one of the messages from
359 * a message queue manager and transmitted it.
360 *
361 * @param cls the `struct CadetPeeer` where we made progress
362 */
363static void
364mqm_send_done (void *cls)
365{
366 struct CadetPeer *cp = cls;
367
368 if (0 == cp->mqm_ready_counter)
369 return; /* nothing to do */
370 for (struct GCP_MessageQueueManager *mqm = cp->mqm_head;
371 NULL != mqm;
372 mqm = mqm->next)
373 {
374 if (NULL == mqm->env)
375 continue;
376 mqm_execute (mqm);
377 return;
378 }
379}
380
381
382/**
383 * Send the message in @a env to @a cp.
384 *
385 * @param mqm the message queue manager to use for transmission
386 * @param env envelope with the message to send; must NOT
387 * yet have a #GNUNET_MQ_notify_sent() callback attached to it
388 */
389void
390GCP_send (struct GCP_MessageQueueManager *mqm,
391 struct GNUNET_MQ_Envelope *env)
392{
393 struct CadetPeer *cp = mqm->cp;
394
395 GNUNET_assert (NULL != cp->core_mq);
396 GNUNET_assert (NULL == mqm->env);
397 GNUNET_MQ_notify_sent (env,
398 &mqm_send_done,
399 cp);
400 mqm->env = env;
401 cp->mqm_ready_counter++;
402 if (0 != GNUNET_MQ_get_length (cp->core_mq))
403 return;
404 mqm_execute (mqm);
405}
406
407
408/**
409 * Function called to destroy a peer now.
410 *
411 * @param cls NULL
412 * @param pid identity of the peer (unused)
413 * @param value the `struct CadetPeer` to clean up
414 * @return #GNUNET_OK (continue to iterate)
415 */
416static int
417destroy_iterator_cb (void *cls,
418 const struct GNUNET_PeerIdentity *pid,
419 void *value)
420{
421 struct CadetPeer *cp = value;
422
423 if (NULL != cp->destroy_task)
424 {
425 GNUNET_SCHEDULER_cancel (cp->destroy_task);
426 cp->destroy_task = NULL;
427 }
428 destroy_peer (cp);
429 return GNUNET_OK;
430}
431
432
433/**
434 * Clean up all entries about all peers.
435 * Must only be called after all tunnels, CORE-connections and
436 * connections are down.
437 */
438void
439GCP_destroy_all_peers ()
440{
441 GNUNET_CONTAINER_multipeermap_iterate (peers,
442 &destroy_iterator_cb,
443 NULL);
444}
445
446
447/**
448 * This peer may no longer be needed, consider cleaning it up.
449 *
450 * @param cp peer to clean up
451 */
452static void
453consider_peer_destroy (struct CadetPeer *cp);
454
455
456/**
457 * We really no longere care about a peer, stop hogging memory with paths to it.
458 * Afterwards, see if there is more to be cleaned up about this peer.
459 *
460 * @param cls a `struct CadetPeer`.
461 */
462static void
463drop_paths (void *cls)
464{
465 struct CadetPeer *cp = cls;
466 struct CadetPeerPath *path;
467
468 cp->destroy_task = NULL;
469 while (NULL != (path = GNUNET_CONTAINER_heap_remove_root (cp->path_heap)))
470 GCPP_release (path);
471 consider_peer_destroy (cp);
472}
473
474
475/**
476 * This peer may no longer be needed, consider cleaning it up.
477 *
478 * @param cp peer to clean up
479 */
480static void
481consider_peer_destroy (struct CadetPeer *cp)
482{
483 struct GNUNET_TIME_Relative exp;
484
485 if (NULL != cp->destroy_task)
486 {
487 GNUNET_SCHEDULER_cancel (cp->destroy_task);
488 cp->destroy_task = NULL;
489 }
490 if (NULL != cp->t)
491 return; /* still relevant! */
492 if (NULL != cp->core_mq)
493 return; /* still relevant! */
494 if (0 != GNUNET_CONTAINER_multishortmap_size (cp->connections))
495 return; /* still relevant! */
496 if (0 < GNUNET_CONTAINER_heap_get_size (cp->path_heap))
497 {
498 cp->destroy_task = GNUNET_SCHEDULER_add_delayed (IDLE_PATH_TIMEOUT,
499 &drop_paths,
500 cp);
501 return;
502 }
503 if (0 < cp->path_dll_length)
504 return; /* still relevant! */
505 if (NULL != cp->hello)
506 {
507 /* relevant only until HELLO expires */
508 exp = GNUNET_TIME_absolute_get_remaining (GNUNET_HELLO_get_last_expiration (cp->hello));
509 cp->destroy_task = GNUNET_SCHEDULER_add_delayed (exp,
510 &destroy_peer,
511 cp);
512 return;
513 }
514 cp->destroy_task = GNUNET_SCHEDULER_add_delayed (IDLE_PEER_TIMEOUT,
515 &destroy_peer,
516 cp);
517}
518
519
520/**
521 * Add an entry to the DLL of all of the paths that this peer is on.
522 *
523 * @param cp peer to modify
524 * @param entry an entry on a path
525 * @param off offset of this peer on the path
526 */
527void
528GCP_path_entry_add (struct CadetPeer *cp,
529 struct CadetPeerPathEntry *entry,
530 unsigned int off)
531{
532 if (off >= cp->path_dll_length)
533 {
534 unsigned int len = cp->path_dll_length;
535
536 GNUNET_array_grow (cp->path_heads,
537 len,
538 off + 4);
539 GNUNET_array_grow (cp->path_tails,
540 cp->path_dll_length,
541 off + 4);
542 }
543 GNUNET_CONTAINER_DLL_insert (cp->path_heads[off],
544 cp->path_tails[off],
545 entry);
546 cp->num_paths++;
547
548 /* If we have a tunnel to this peer, tell the tunnel that there is a
549 new path available. */
550 if (NULL != cp->t)
551 GCT_consider_path (cp->t,
552 entry->path,
553 off);
554}
555
556
557/**
558 * Remove an entry from the DLL of all of the paths that this peer is on.
559 *
560 * @param cp peer to modify
561 * @param entry an entry on a path
562 * @param off offset of this peer on the path
563 */
564void
565GCP_path_entry_remove (struct CadetPeer *cp,
566 struct CadetPeerPathEntry *entry,
567 unsigned int off)
568{
569 GNUNET_CONTAINER_DLL_remove (cp->path_heads[off],
570 cp->path_tails[off],
571 entry);
572 GNUNET_assert (0 < cp->num_paths);
573 cp->num_paths--;
574}
575
576
577/**
578 * Try adding a @a path to this @a peer. If the peer already
579 * has plenty of paths, return NULL.
580 *
581 * @param cp peer to which the @a path leads to
582 * @param path a path looking for an owner; may not be fully initialized yet!
583 * @param off offset of @a cp in @a path
584 * @return NULL if this peer does not care to become a new owner,
585 * otherwise the node in the peer's path heap for the @a path.
586 */
587struct GNUNET_CONTAINER_HeapNode *
588GCP_attach_path (struct CadetPeer *cp,
589 struct CadetPeerPath *path,
590 unsigned int off)
591{
592 GNUNET_CONTAINER_HeapCostType desirability;
593 struct CadetPeerPath *root;
594 GNUNET_CONTAINER_HeapCostType root_desirability;
595 struct GNUNET_CONTAINER_HeapNode *hn;
596
597 /* FIXME: desirability is not yet initialized; tricky! */
598 desirability = GCPP_get_desirability (path);
599 if (GNUNET_NO ==
600 GNUNET_CONTAINER_heap_peek2 (cp->path_heap,
601 (void **) &root,
602 &root_desirability))
603 {
604 root = NULL;
605 root_desirability = 0;
606 }
607
608 if ( (DESIRED_CONNECTIONS_PER_TUNNEL > cp->num_paths) &&
609 (desirability < root_desirability) )
610 return NULL;
611
612 /* Yes, we'd like to add this path, add to our heap */
613 hn = GNUNET_CONTAINER_heap_insert (cp->path_heap,
614 (void *) cp,
615 desirability);
616
617 /* Consider maybe dropping other paths because of the new one */
618 if (GNUNET_CONTAINER_heap_get_size (cp->path_heap) >=
619 2 * DESIRED_CONNECTIONS_PER_TUNNEL)
620 {
621 /* Now we have way too many, drop least desirable UNLESS it is in use!
622 (Note that this intentionally keeps highly desireable, but currently
623 unused paths around in the hope that we might be able to switch, even
624 if the number of paths exceeds the threshold.) */
625 root = GNUNET_CONTAINER_heap_peek (cp->path_heap);
626 if (NULL ==
627 GCPP_get_connection (root,
628 cp,
629 GCPP_get_length (root) - 1))
630 {
631 /* Got plenty of paths to this destination, and this is a low-quality
632 one that we don't care, allow it to die. */
633 GNUNET_assert (root ==
634 GNUNET_CONTAINER_heap_remove_root (cp->path_heap));
635 GCPP_release (root);
636 }
637 }
638 return hn;
639}
640
641
642/**
643 * This peer can no longer own @a path as the path
644 * has been extended and a peer further down the line
645 * is now the new owner.
646 *
647 * @param cp old owner of the @a path
648 * @param path path where the ownership is lost
649 * @param hn note in @a cp's path heap that must be deleted
650 */
651void
652GCP_detach_path (struct CadetPeer *cp,
653 struct CadetPeerPath *path,
654 struct GNUNET_CONTAINER_HeapNode *hn)
655{
656 GNUNET_assert (path ==
657 GNUNET_CONTAINER_heap_remove_node (hn));
658}
659
660
661/**
662 * Add a @a connection to this @a cp.
663 *
664 * @param cp peer via which the @a connection goes
665 * @param cc the connection to add
666 */
667void
668GCP_add_connection (struct CadetPeer *cp,
669 struct CadetConnection *cc)
670{
671 GNUNET_assert (GNUNET_OK ==
672 GNUNET_CONTAINER_multishortmap_put (cp->connections,
673 &GCC_get_id (cc)->connection_of_tunnel,
674 cc,
675 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
676}
677
678
679/**
680 * Remove a @a connection that went via this @a cp.
681 *
682 * @param cp peer via which the @a connection went
683 * @param cc the connection to remove
684 */
685void
686GCP_remove_connection (struct CadetPeer *cp,
687 struct CadetConnection *cc)
688{
689 GNUNET_assert (GNUNET_YES ==
690 GNUNET_CONTAINER_multishortmap_remove (cp->connections,
691 &GCC_get_id (cc)->connection_of_tunnel,
692 cc));
693}
694
695
696/**
697 * This peer is now on more "active" duty, activate processes related to it.
698 *
699 * @param cp the more-active peer
700 */
701static void
702consider_peer_activate (struct CadetPeer *cp)
703{
704 uint32_t strength;
705
706 if (NULL != cp->destroy_task)
707 {
708 /* It's active, do not destory! */
709 GNUNET_SCHEDULER_cancel (cp->destroy_task);
710 cp->destroy_task = NULL;
711 }
712 if ( (0 == GNUNET_CONTAINER_multishortmap_size (cp->connections)) &&
713 (NULL == cp->t) )
714 {
715 /* We're just on a path or directly connected; don't bother too much */
716 if (NULL != cp->connectivity_suggestion)
717 {
718 GNUNET_ATS_connectivity_suggest_cancel (cp->connectivity_suggestion);
719 cp->connectivity_suggestion = NULL;
720 }
721 if (NULL != cp->search_h)
722 {
723 GCD_search_stop (cp->search_h);
724 cp->search_h = NULL;
725 }
726 return;
727 }
728 if (NULL == cp->core_mq)
729 {
730 /* Lacks direct connection, try to create one by querying the DHT */
731 if ( (NULL == cp->search_h) &&
732 (DESIRED_CONNECTIONS_PER_TUNNEL < cp->num_paths) )
733 cp->search_h
734 = GCD_search (&cp->pid);
735 }
736 else
737 {
738 /* Have direct connection, stop DHT search if active */
739 if (NULL != cp->search_h)
740 {
741 GCD_search_stop (cp->search_h);
742 cp->search_h = NULL;
743 }
744 }
745
746 /* If we have a tunnel, our urge for connections is much bigger */
747 strength = (NULL != cp->t) ? 32 : 1;
748 if (NULL != cp->connectivity_suggestion)
749 GNUNET_ATS_connectivity_suggest_cancel (cp->connectivity_suggestion);
750 cp->connectivity_suggestion
751 = GNUNET_ATS_connectivity_suggest (ats_ch,
752 &cp->pid,
753 strength);
754}
755
756
757/**
758 * Retrieve the CadetPeer stucture associated with the
759 * peer. Optionally create one and insert it in the appropriate
760 * structures if the peer is not known yet.
761 *
762 * @param peer_id Full identity of the peer.
763 * @param create #GNUNET_YES if a new peer should be created if unknown.
764 * #GNUNET_NO to return NULL if peer is unknown.
765 * @return Existing or newly created peer structure.
766 * NULL if unknown and not requested @a create
767 */
768struct CadetPeer *
769GCP_get (const struct GNUNET_PeerIdentity *peer_id,
770 int create)
771{
772 struct CadetPeer *cp;
773
774 cp = GNUNET_CONTAINER_multipeermap_get (peers,
775 peer_id);
776 if (NULL != cp)
777 return cp;
778 if (GNUNET_NO == create)
779 return NULL;
780 cp = GNUNET_new (struct CadetPeer);
781 cp->pid = *peer_id;
782 cp->connections = GNUNET_CONTAINER_multishortmap_create (32,
783 GNUNET_YES);
784 cp->path_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
785 GNUNET_assert (GNUNET_YES ==
786 GNUNET_CONTAINER_multipeermap_put (peers,
787 &cp->pid,
788 cp,
789 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
790 return cp;
791}
792
793
794/**
795 * Obtain the peer identity for a `struct CadetPeer`.
796 *
797 * @param cp our peer handle
798 * @return the peer identity
799 */
800const struct GNUNET_PeerIdentity *
801GCP_get_id (struct CadetPeer *cp)
802{
803 return &cp->pid;
804}
805
806
807/**
808 * Iterate over all known peers.
809 *
810 * @param iter Iterator.
811 * @param cls Closure for @c iter.
812 */
813void
814GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
815 void *cls)
816{
817 GNUNET_CONTAINER_multipeermap_iterate (peers,
818 iter,
819 cls);
820}
821
822
823/**
824 * Count the number of known paths toward the peer.
825 *
826 * @param peer Peer to get path info.
827 * @return Number of known paths.
828 */
829unsigned int
830GCP_count_paths (const struct CadetPeer *peer)
831{
832 return peer->num_paths;
833}
834
835
836/**
837 * Iterate over the paths to a peer.
838 *
839 * @param peer Peer to get path info.
840 * @param callback Function to call for every path.
841 * @param callback_cls Closure for @a callback.
842 * @return Number of iterated paths.
843 */
844unsigned int
845GCP_iterate_paths (struct CadetPeer *peer,
846 GCP_PathIterator callback,
847 void *callback_cls)
848{
849 unsigned int ret = 0;
850
851 for (unsigned int i=0;i<peer->path_dll_length;i++)
852 {
853 for (struct CadetPeerPathEntry *pe = peer->path_heads[i];
854 NULL != pe;
855 pe = pe->next)
856 {
857 if (GNUNET_NO ==
858 callback (callback_cls,
859 pe->path,
860 i))
861 return ret;
862 ret++;
863 }
864 }
865 return ret;
866}
867
868
869/**
870 * Iterate over the paths to @a peer where
871 * @a peer is at distance @a dist from us.
872 *
873 * @param peer Peer to get path info.
874 * @param dist desired distance of @a peer to us on the path
875 * @param callback Function to call for every path.
876 * @param callback_cls Closure for @a callback.
877 * @return Number of iterated paths.
878 */
879unsigned int
880GCP_iterate_paths_at (struct CadetPeer *peer,
881 unsigned int dist,
882 GCP_PathIterator callback,
883 void *callback_cls)
884{
885 unsigned int ret = 0;
886
887 if (dist<peer->path_dll_length)
888 return 0;
889 for (struct CadetPeerPathEntry *pe = peer->path_heads[dist];
890 NULL != pe;
891 pe = pe->next)
892 {
893 if (GNUNET_NO ==
894 callback (callback_cls,
895 pe->path,
896 dist))
897 return ret;
898 ret++;
899 }
900 return ret;
901}
902
903
904/**
905 * Get the tunnel towards a peer.
906 *
907 * @param peer Peer to get from.
908 * @param create #GNUNET_YES to create a tunnel if we do not have one
909 * @return Tunnel towards peer.
910 */
911struct CadetTunnel *
912GCP_get_tunnel (struct CadetPeer *peer,
913 int create)
914{
915 if (NULL == peer)
916 return NULL;
917 if ( (NULL != peer->t) ||
918 (GNUNET_NO == create) )
919 return peer->t;
920 peer->t = GCT_create_tunnel (peer);
921 consider_peer_activate (peer);
922 return peer->t;
923}
924
925
926/**
927 * We got a HELLO for a @a peer, remember it, and possibly
928 * trigger adequate actions (like trying to connect).
929 *
930 * @param peer the peer we got a HELLO for
931 * @param hello the HELLO to remember
932 */
933void
934GCP_set_hello (struct CadetPeer *peer,
935 const struct GNUNET_HELLO_Message *hello)
936{
937 /* FIXME: keep HELLO, possibly offer to TRANSPORT... */
938
939 consider_peer_destroy (peer);
940}
941
942
943/**
944 * The tunnel to the given peer no longer exists, remove it from our
945 * data structures, and possibly clean up the peer itself.
946 *
947 * @param peer the peer affected
948 * @param t the dead tunnel
949 */
950void
951GCP_drop_tunnel (struct CadetPeer *peer,
952 struct CadetTunnel *t)
953{
954 GNUNET_assert (peer->t == t);
955 peer->t = NULL;
956 consider_peer_destroy (peer);
957}
958
959
960/**
961 * Test if @a cp has a core-level connection
962 *
963 * @param cp peer to test
964 * @return #GNUNET_YES if @a cp has a core-level connection
965 */
966int
967GCP_has_core_connection (struct CadetPeer *cp)
968{
969 return (NULL != cp->core_mq) ? GNUNET_YES : GNUNET_NO;
970}
971
972
973/**
974 * Start message queue change notifications.
975 *
976 * @param cp peer to notify for
977 * @param cb function to call if mq becomes available or unavailable
978 * @param cb_cls closure for @a cb
979 * @return handle to cancel request
980 */
981struct GCP_MessageQueueManager *
982GCP_request_mq (struct CadetPeer *cp,
983 GCP_MessageQueueNotificationCallback cb,
984 void *cb_cls)
985{
986 struct GCP_MessageQueueManager *mqm;
987
988 mqm = GNUNET_new (struct GCP_MessageQueueManager);
989 mqm->cb = cb;
990 mqm->cb_cls = cb_cls;
991 mqm->cp = cp;
992 GNUNET_CONTAINER_DLL_insert (cp->mqm_head,
993 cp->mqm_tail,
994 mqm);
995 if (NULL != cp->core_mq)
996 cb (cb_cls,
997 GNUNET_YES);
998 return mqm;
999}
1000
1001
1002/**
1003 * Stops message queue change notifications.
1004 *
1005 * @param mqm handle matching request to cancel
1006 * @param last_env final message to transmit, or NULL
1007 */
1008void
1009GCP_request_mq_cancel (struct GCP_MessageQueueManager *mqm,
1010 struct GNUNET_MQ_Envelope *last_env)
1011{
1012 struct CadetPeer *cp = mqm->cp;
1013
1014 if (NULL != mqm->env)
1015 GNUNET_MQ_discard (mqm->env);
1016 if (NULL != last_env)
1017 {
1018 if (NULL != cp->core_mq)
1019 GNUNET_MQ_send (cp->core_mq,
1020 last_env);
1021 else
1022 GNUNET_MQ_discard (last_env);
1023 }
1024 GNUNET_CONTAINER_DLL_remove (cp->mqm_head,
1025 cp->mqm_tail,
1026 mqm);
1027 GNUNET_free (mqm);
1028}
1029
1030
1031/**
1032 * Send the message in @a env to @a cp, overriding queueing logic.
1033 * This function should only be used to send error messages outside
1034 * of flow and congestion control, similar to ICMP. Note that
1035 * the envelope may be silently discarded as well.
1036 *
1037 * @param cp peer to send the message to
1038 * @param env envelope with the message to send
1039 */
1040void
1041GCP_send_ooo (struct CadetPeer *cp,
1042 struct GNUNET_MQ_Envelope *env)
1043{
1044 if (NULL == cp->core_mq)
1045 {
1046 GNUNET_MQ_discard (env);
1047 return;
1048 }
1049 GNUNET_MQ_send (cp->core_mq,
1050 env);
1051}
1052
1053
1054
1055
1056/* end of gnunet-service-cadet-new_peer.c */
diff --git a/src/cadet/gnunet-service-cadet-new_peer.h b/src/cadet/gnunet-service-cadet-new_peer.h
new file mode 100644
index 000000000..c633f47e5
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new_peer.h
@@ -0,0 +1,368 @@
1
2/*
3 This file is part of GNUnet.
4 Copyright (C) 2001-2017 GNUnet e.V.
5
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
10
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22/**
23 * @file cadet/gnunet-service-cadet-new_peer.h
24 * @brief Information we track per peer.
25 * @author Bartlomiej Polot
26 * @author Christian Grothoff
27 */
28#ifndef GNUNET_SERVICE_CADET_PEER_H
29#define GNUNET_SERVICE_CADET_PEER_H
30
31#include "gnunet-service-cadet-new.h"
32#include "gnunet_hello_lib.h"
33
34
35/**
36 * Get the static string for a peer ID.
37 *
38 * @param peer Peer.
39 *
40 * @return Static string for it's ID.
41 */
42const char *
43GCP_2s (const struct CadetPeer *peer);
44
45
46/**
47 * Retrieve the CadetPeer stucture associated with the
48 * peer. Optionally create one and insert it in the appropriate
49 * structures if the peer is not known yet.
50 *
51 * @param peer_id Full identity of the peer.
52 * @param create #GNUNET_YES if a new peer should be created if unknown.
53 * #GNUNET_NO to return NULL if peer is unknown.
54 * @return Existing or newly created peer structure.
55 * NULL if unknown and not requested @a create
56 */
57struct CadetPeer *
58GCP_get (const struct GNUNET_PeerIdentity *peer_id,
59 int create);
60
61
62/**
63 * Obtain the peer identity for a `struct CadetPeer`.
64 *
65 * @param cp our peer handle
66 * @return the peer identity
67 */
68const struct GNUNET_PeerIdentity *
69GCP_get_id (struct CadetPeer *cp);
70
71
72/**
73 * Iterate over all known peers.
74 *
75 * @param iter Iterator.
76 * @param cls Closure for @c iter.
77 */
78void
79GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
80 void *cls);
81
82
83/**
84 * Count the number of known paths toward the peer.
85 *
86 * @param cp Peer to get path info.
87 * @return Number of known paths.
88 */
89unsigned int
90GCP_count_paths (const struct CadetPeer *cp);
91
92
93/**
94 * Peer path iterator.
95 *
96 * @param cls Closure.
97 * @param path Path itself
98 * @param off offset of the target peer in @a path
99 * @return #GNUNET_YES if should keep iterating.
100 * #GNUNET_NO otherwise.
101 */
102typedef int
103(*GCP_PathIterator) (void *cls,
104 struct CadetPeerPath *path,
105 unsigned int off);
106
107
108/**
109 * Iterate over the paths to a peer.
110 *
111 * @param cp Peer to get path info.
112 * @param callback Function to call for every path.
113 * @param callback_cls Closure for @a callback.
114 * @return Number of iterated paths.
115 */
116unsigned int
117GCP_iterate_paths (struct CadetPeer *cp,
118 GCP_PathIterator callback,
119 void *callback_cls);
120
121
122/**
123 * Iterate over the paths to @a peer where
124 * @a peer is at distance @a dist from us.
125 *
126 * @param peer Peer to get path info.
127 * @param dist desired distance of @a peer to us on the path
128 * @param callback Function to call for every path.
129 * @param callback_cls Closure for @a callback.
130 * @return Number of iterated paths.
131 */
132unsigned int
133GCP_iterate_paths_at (struct CadetPeer *peer,
134 unsigned int dist,
135 GCP_PathIterator callback,
136 void *callback_cls);
137
138
139/**
140 * Remove an entry from the DLL of all of the paths that this peer is on.
141 *
142 * @param cp peer to modify
143 * @param entry an entry on a path
144 * @param off offset of this peer on the path
145 */
146void
147GCP_path_entry_remove (struct CadetPeer *cp,
148 struct CadetPeerPathEntry *entry,
149 unsigned int off);
150
151
152/**
153 * Add an entry to the DLL of all of the paths that this peer is on.
154 *
155 * @param cp peer to modify
156 * @param entry an entry on a path
157 * @param off offset of this peer on the path
158 */
159void
160GCP_path_entry_add (struct CadetPeer *cp,
161 struct CadetPeerPathEntry *entry,
162 unsigned int off);
163
164
165/**
166 * Get the tunnel towards a peer.
167 *
168 * @param cp Peer to get from.
169 * @param create #GNUNET_YES to create a tunnel if we do not have one
170 * @return Tunnel towards peer.
171 */
172struct CadetTunnel *
173GCP_get_tunnel (struct CadetPeer *cp,
174 int create);
175
176
177/**
178 * The tunnel to the given peer no longer exists, remove it from our
179 * data structures, and possibly clean up the peer itself.
180 *
181 * @param cp the peer affected
182 * @param t the dead tunnel
183 */
184void
185GCP_drop_tunnel (struct CadetPeer *cp,
186 struct CadetTunnel *t);
187
188
189/**
190 * Try adding a @a path to this @a cp. If the peer already
191 * has plenty of paths, return NULL.
192 *
193 * @param cp peer to which the @a path leads to
194 * @param path a path looking for an owner; may not be fully initialized yet!
195 * @param off offset of @a cp in @a path
196 * @return NULL if this peer does not care to become a new owner,
197 * otherwise the node in the peer's path heap for the @a path.
198 */
199struct GNUNET_CONTAINER_HeapNode *
200GCP_attach_path (struct CadetPeer *cp,
201 struct CadetPeerPath *path,
202 unsigned int off);
203
204
205/**
206 * This peer can no longer own @a path as the path
207 * has been extended and a peer further down the line
208 * is now the new owner.
209 *
210 * @param cp old owner of the @a path
211 * @param path path where the ownership is lost
212 * @param hn note in @a cp's path heap that must be deleted
213 */
214void
215GCP_detach_path (struct CadetPeer *cp,
216 struct CadetPeerPath *path,
217 struct GNUNET_CONTAINER_HeapNode *hn);
218
219
220/**
221 * Add a @a connection to this @a cp.
222 *
223 * @param cp peer via which the @a connection goes
224 * @param cc the connection to add
225 */
226void
227GCP_add_connection (struct CadetPeer *cp,
228 struct CadetConnection *cc);
229
230
231/**
232 * Remove a @a connection that went via this @a cp.
233 *
234 * @param cp peer via which the @a connection went
235 * @param cc the connection to remove
236 */
237void
238GCP_remove_connection (struct CadetPeer *cp,
239 struct CadetConnection *cc);
240
241
242/**
243 * We got a HELLO for a @a cp, remember it, and possibly
244 * trigger adequate actions (like trying to connect).
245 *
246 * @param cp the peer we got a HELLO for
247 * @param hello the HELLO to remember
248 */
249void
250GCP_set_hello (struct CadetPeer *cp,
251 const struct GNUNET_HELLO_Message *hello);
252
253
254/**
255 * Clean up all entries about all peers.
256 * Must only be called after all tunnels, CORE-connections and
257 * connections are down.
258 */
259void
260GCP_destroy_all_peers (void);
261
262
263/**
264 * Data structure used to track whom we have to notify about changes
265 * in our ability to transmit to a given peer.
266 *
267 * All queue managers will be given equal chance for sending messages
268 * to @a cp. This construct this guarantees fairness for access to @a
269 * cp among the different message queues. Each connection or route
270 * will have its respective message queue managers for each direction.
271 */
272struct GCP_MessageQueueManager;
273
274
275/**
276 * Function to call with updated message queue object.
277 *
278 * @param cls closure
279 * @param available #GNUNET_YES if sending is now possible,
280 * #GNUNET_NO if sending is no longer possible
281 * #GNUNET_SYSERR if sending is no longer possible
282 * and the last envelope was discarded
283 */
284typedef void
285(*GCP_MessageQueueNotificationCallback)(void *cls,
286 int available);
287
288
289/**
290 * Start message queue change notifications. Will create a new slot
291 * to manage the message queue to the given @a cp.
292 *
293 * @param cp peer to notify for
294 * @param cb function to call if mq becomes available or unavailable
295 * @param cb_cls closure for @a cb
296 * @return handle to cancel request
297 */
298struct GCP_MessageQueueManager *
299GCP_request_mq (struct CadetPeer *cp,
300 GCP_MessageQueueNotificationCallback cb,
301 void *cb_cls);
302
303
304/**
305 * Test if @a cp has a core-level connection
306 *
307 * @param cp peer to test
308 * @return #GNUNET_YES if @a cp has a core-level connection
309 */
310int
311GCP_has_core_connection (struct CadetPeer *cp);
312
313
314/**
315 * Send the message in @a env via a @a mqm. Must only be called at
316 * most once after the respective
317 * #GCP_MessageQueueNotificationCallback was called with `available`
318 * set to #GNUNET_YES, and not after the callback was called with
319 * `available` set to #GNUNET_NO or #GNUNET_SYSERR.
320 *
321 * @param mqm message queue manager for the transmission
322 * @param env envelope with the message to send; must NOT
323 * yet have a #GNUNET_MQ_notify_sent() callback attached to it
324 */
325void
326GCP_send (struct GCP_MessageQueueManager *mqm,
327 struct GNUNET_MQ_Envelope *env);
328
329
330/**
331 * Send the message in @a env to @a cp, overriding queueing logic.
332 * This function should only be used to send error messages outside
333 * of flow and congestion control, similar to ICMP. Note that
334 * the envelope may be silently discarded as well.
335 *
336 * @param cp peer to send the message to
337 * @param env envelope with the message to send
338 */
339void
340GCP_send_ooo (struct CadetPeer *cp,
341 struct GNUNET_MQ_Envelope *env);
342
343
344/**
345 * Stops message queue change notifications and sends a last message.
346 * In practice, this is implemented by sending that @a last_env
347 * message immediately (if any), ignoring queue order.
348 *
349 * @param mqm handle matching request to cancel
350 * @param last_env final message to transmit, or NULL
351 */
352void
353GCP_request_mq_cancel (struct GCP_MessageQueueManager *mqm,
354 struct GNUNET_MQ_Envelope *last_env);
355
356
357/**
358 * Set the message queue to @a mq for peer @a cp and notify watchers.
359 *
360 * @param cp peer to modify
361 * @param mq message queue to set (can be NULL)
362 */
363void
364GCP_set_mq (struct CadetPeer *cp,
365 struct GNUNET_MQ_Handle *mq);
366
367
368#endif
diff --git a/src/cadet/gnunet-service-cadet-new_tunnels.c b/src/cadet/gnunet-service-cadet-new_tunnels.c
new file mode 100644
index 000000000..23b270b82
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new_tunnels.c
@@ -0,0 +1,2249 @@
1
2/*
3 This file is part of GNUnet.
4 Copyright (C) 2013, 2017 GNUnet e.V.
5
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
10
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22/**
23 * @file cadet/gnunet-service-cadet-new_tunnels.c
24 * @brief Information we track per tunnel.
25 * @author Bartlomiej Polot
26 * @author Christian Grothoff
27 *
28 * FIXME:
29 * - when managing connections, distinguish those that
30 * have (recently) had traffic from those that were
31 * never ready (or not recently)
32 * - implement sending and receiving KX messages
33 * - implement processing of incoming decrypted plaintext messages
34 * - clean up KX logic!
35 */
36#include "platform.h"
37#include "gnunet_util_lib.h"
38#include "gnunet_statistics_service.h"
39#include "gnunet_signatures.h"
40#include "cadet_protocol.h"
41#include "cadet_path.h"
42#include "gnunet-service-cadet-new.h"
43#include "gnunet-service-cadet-new_channel.h"
44#include "gnunet-service-cadet-new_connection.h"
45#include "gnunet-service-cadet-new_tunnels.h"
46#include "gnunet-service-cadet-new_peer.h"
47#include "gnunet-service-cadet-new_paths.h"
48
49
50#define LOG(level, ...) GNUNET_log_from(level,"cadet-tun",__VA_ARGS__)
51
52
53/**
54 * How long do we wait until tearing down an idle tunnel?
55 */
56#define IDLE_DESTROY_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90)
57
58/**
59 * Yuck, replace by 'offsetof' expression?
60 * FIXME.
61 */
62#define AX_HEADER_SIZE (sizeof (uint32_t) * 2\
63 + sizeof (struct GNUNET_CRYPTO_EcdhePublicKey))
64
65
66/**
67 * Maximum number of skipped keys we keep in memory per tunnel.
68 */
69#define MAX_SKIPPED_KEYS 64
70
71/**
72 * Maximum number of keys (and thus ratchet steps) we are willing to
73 * skip before we decide this is either a bogus packet or a DoS-attempt.
74 */
75#define MAX_KEY_GAP 256
76
77
78/**
79 * Struct to old keys for skipped messages while advancing the Axolotl ratchet.
80 */
81struct CadetTunnelSkippedKey
82{
83 /**
84 * DLL next.
85 */
86 struct CadetTunnelSkippedKey *next;
87
88 /**
89 * DLL prev.
90 */
91 struct CadetTunnelSkippedKey *prev;
92
93 /**
94 * When was this key stored (for timeout).
95 */
96 struct GNUNET_TIME_Absolute timestamp;
97
98 /**
99 * Header key.
100 */
101 struct GNUNET_CRYPTO_SymmetricSessionKey HK;
102
103 /**
104 * Message key.
105 */
106 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
107
108 /**
109 * Key number for a given HK.
110 */
111 unsigned int Kn;
112};
113
114
115/**
116 * Axolotl data, according to https://github.com/trevp/axolotl/wiki .
117 */
118struct CadetTunnelAxolotl
119{
120 /**
121 * A (double linked) list of stored message keys and associated header keys
122 * for "skipped" messages, i.e. messages that have not been
123 * received despite the reception of more recent messages, (head).
124 */
125 struct CadetTunnelSkippedKey *skipped_head;
126
127 /**
128 * Skipped messages' keys DLL, tail.
129 */
130 struct CadetTunnelSkippedKey *skipped_tail;
131
132 /**
133 * 32-byte root key which gets updated by DH ratchet.
134 */
135 struct GNUNET_CRYPTO_SymmetricSessionKey RK;
136
137 /**
138 * 32-byte header key (send).
139 */
140 struct GNUNET_CRYPTO_SymmetricSessionKey HKs;
141
142 /**
143 * 32-byte header key (recv)
144 */
145 struct GNUNET_CRYPTO_SymmetricSessionKey HKr;
146
147 /**
148 * 32-byte next header key (send).
149 */
150 struct GNUNET_CRYPTO_SymmetricSessionKey NHKs;
151
152 /**
153 * 32-byte next header key (recv).
154 */
155 struct GNUNET_CRYPTO_SymmetricSessionKey NHKr;
156
157 /**
158 * 32-byte chain keys (used for forward-secrecy updating, send).
159 */
160 struct GNUNET_CRYPTO_SymmetricSessionKey CKs;
161
162 /**
163 * 32-byte chain keys (used for forward-secrecy updating, recv).
164 */
165 struct GNUNET_CRYPTO_SymmetricSessionKey CKr;
166
167 /**
168 * ECDH for key exchange (A0 / B0).
169 */
170 struct GNUNET_CRYPTO_EcdhePrivateKey *kx_0;
171
172 /**
173 * ECDH Ratchet key (send).
174 */
175 struct GNUNET_CRYPTO_EcdhePrivateKey *DHRs;
176
177 /**
178 * ECDH Ratchet key (recv).
179 */
180 struct GNUNET_CRYPTO_EcdhePublicKey DHRr;
181
182 /**
183 * When does this ratchet expire and a new one is triggered.
184 */
185 struct GNUNET_TIME_Absolute ratchet_expiration;
186
187 /**
188 * Number of elements in @a skipped_head <-> @a skipped_tail.
189 */
190 unsigned int skipped;
191
192 /**
193 * Message number (reset to 0 with each new ratchet, next message to send).
194 */
195 uint32_t Ns;
196
197 /**
198 * Message number (reset to 0 with each new ratchet, next message to recv).
199 */
200 uint32_t Nr;
201
202 /**
203 * Previous message numbers (# of msgs sent under prev ratchet)
204 */
205 uint32_t PNs;
206
207 /**
208 * True (#GNUNET_YES) if we have to send a new ratchet key in next msg.
209 */
210 int ratchet_flag;
211
212 /**
213 * Number of messages recieved since our last ratchet advance.
214 * - If this counter = 0, we cannot send a new ratchet key in next msg.
215 * - If this counter > 0, we can (but don't yet have to) send a new key.
216 */
217 unsigned int ratchet_allowed;
218
219 /**
220 * Number of messages recieved since our last ratchet advance.
221 * - If this counter = 0, we cannot send a new ratchet key in next msg.
222 * - If this counter > 0, we can (but don't yet have to) send a new key.
223 */
224 unsigned int ratchet_counter;
225
226};
227
228
229/**
230 * Struct used to save messages in a non-ready tunnel to send once connected.
231 */
232struct CadetTunnelQueueEntry
233{
234 /**
235 * We are entries in a DLL
236 */
237 struct CadetTunnelQueueEntry *next;
238
239 /**
240 * We are entries in a DLL
241 */
242 struct CadetTunnelQueueEntry *prev;
243
244 /**
245 * Tunnel these messages belong in.
246 */
247 struct CadetTunnel *t;
248
249 /**
250 * Continuation to call once sent (on the channel layer).
251 */
252 GNUNET_SCHEDULER_TaskCallback cont;
253
254 /**
255 * Closure for @c cont.
256 */
257 void *cont_cls;
258
259 /**
260 * Envelope of message to send follows.
261 */
262 struct GNUNET_MQ_Envelope *env;
263
264 /**
265 * Where to put the connection identifier into the payload
266 * of the message in @e env once we have it?
267 */
268 struct GNUNET_CADET_ConnectionTunnelIdentifier *cid;
269};
270
271
272/**
273 * Struct containing all information regarding a tunnel to a peer.
274 */
275struct CadetTunnel
276{
277 /**
278 * Destination of the tunnel.
279 */
280 struct CadetPeer *destination;
281
282 /**
283 * Peer's ephemeral key, to recreate @c e_key and @c d_key when own
284 * ephemeral key changes.
285 */
286 struct GNUNET_CRYPTO_EcdhePublicKey peers_ephemeral_key;
287
288 /**
289 * Encryption ("our") key. It is only "confirmed" if kx_ctx is NULL.
290 */
291 struct GNUNET_CRYPTO_SymmetricSessionKey e_key;
292
293 /**
294 * Decryption ("their") key. It is only "confirmed" if kx_ctx is NULL.
295 */
296 struct GNUNET_CRYPTO_SymmetricSessionKey d_key;
297
298 /**
299 * Axolotl info.
300 */
301 struct CadetTunnelAxolotl ax;
302
303 /**
304 * State of the tunnel connectivity.
305 */
306 enum CadetTunnelCState cstate;
307
308 /**
309 * State of the tunnel encryption.
310 */
311 enum CadetTunnelEState estate;
312
313 /**
314 * Task to start the rekey process.
315 */
316 struct GNUNET_SCHEDULER_Task *rekey_task;
317
318 /**
319 * Tokenizer for decrypted messages.
320 */
321 struct GNUNET_MessageStreamTokenizer *mst;
322
323 /**
324 * Dispatcher for decrypted messages only (do NOT use for sending!).
325 */
326 struct GNUNET_MQ_Handle *mq;
327
328 /**
329 * DLL of connections that are actively used to reach the destination peer.
330 */
331 struct CadetTConnection *connection_head;
332
333 /**
334 * DLL of connections that are actively used to reach the destination peer.
335 */
336 struct CadetTConnection *connection_tail;
337
338 /**
339 * Channels inside this tunnel. Maps
340 * `struct GNUNET_CADET_ChannelTunnelNumber` to a `struct CadetChannel`.
341 */
342 struct GNUNET_CONTAINER_MultiHashMap32 *channels;
343
344 /**
345 * Channel ID for the next created channel in this tunnel.
346 */
347 struct GNUNET_CADET_ChannelTunnelNumber next_chid;
348
349 /**
350 * Queued messages, to transmit once tunnel gets connected.
351 */
352 struct CadetTunnelQueueEntry *tq_head;
353
354 /**
355 * Queued messages, to transmit once tunnel gets connected.
356 */
357 struct CadetTunnelQueueEntry *tq_tail;
358
359 /**
360 * Task scheduled if there are no more channels using the tunnel.
361 */
362 struct GNUNET_SCHEDULER_Task *destroy_task;
363
364 /**
365 * Task to trim connections if too many are present.
366 */
367 struct GNUNET_SCHEDULER_Task *maintain_connections_task;
368
369 /**
370 * Ephemeral message in the queue (to avoid queueing more than one).
371 */
372 struct CadetConnectionQueue *ephm_hKILL;
373
374 /**
375 * Pong message in the queue.
376 */
377 struct CadetConnectionQueue *pong_hKILL;
378
379 /**
380 * Number of connections in the @e connection_head DLL.
381 */
382 unsigned int num_connections;
383
384 /**
385 * Number of entries in the @e tq_head DLL.
386 */
387 unsigned int tq_len;
388};
389
390
391/**
392 * Get the static string for the peer this tunnel is directed.
393 *
394 * @param t Tunnel.
395 *
396 * @return Static string the destination peer's ID.
397 */
398const char *
399GCT_2s (const struct CadetTunnel *t)
400{
401 static char buf[64];
402
403 if (NULL == t)
404 return "T(NULL)";
405
406 GNUNET_snprintf (buf,
407 sizeof (buf),
408 "T(%s)",
409 GCP_2s (t->destination));
410 return buf;
411}
412
413
414/**
415 * Return the peer to which this tunnel goes.
416 *
417 * @param t a tunnel
418 * @return the destination of the tunnel
419 */
420struct CadetPeer *
421GCT_get_destination (struct CadetTunnel *t)
422{
423 return t->destination;
424}
425
426
427/**
428 * Count channels of a tunnel.
429 *
430 * @param t Tunnel on which to count.
431 *
432 * @return Number of channels.
433 */
434unsigned int
435GCT_count_channels (struct CadetTunnel *t)
436{
437 return GNUNET_CONTAINER_multihashmap32_size (t->channels);
438}
439
440
441/**
442 * Lookup a channel by its @a chid.
443 *
444 * @param t tunnel to look in
445 * @param chid number of channel to find
446 * @return NULL if channel does not exist
447 */
448struct CadetChannel *
449lookup_channel (struct CadetTunnel *t,
450 struct GNUNET_CADET_ChannelTunnelNumber chid)
451{
452 return GNUNET_CONTAINER_multihashmap32_get (t->channels,
453 ntohl (chid.cn));
454}
455
456
457/**
458 * Count all created connections of a tunnel. Not necessarily ready connections!
459 *
460 * @param t Tunnel on which to count.
461 *
462 * @return Number of connections created, either being established or ready.
463 */
464unsigned int
465GCT_count_any_connections (struct CadetTunnel *t)
466{
467 return t->num_connections;
468}
469
470
471/**
472 * Get the connectivity state of a tunnel.
473 *
474 * @param t Tunnel.
475 *
476 * @return Tunnel's connectivity state.
477 */
478enum CadetTunnelCState
479GCT_get_cstate (struct CadetTunnel *t)
480{
481 return t->cstate;
482}
483
484
485/**
486 * Get the encryption state of a tunnel.
487 *
488 * @param t Tunnel.
489 *
490 * @return Tunnel's encryption state.
491 */
492enum CadetTunnelEState
493GCT_get_estate (struct CadetTunnel *t)
494{
495 return t->estate;
496}
497
498
499/**
500 * Create a new Axolotl ephemeral (ratchet) key.
501 *
502 * @param t Tunnel.
503 */
504static void
505new_ephemeral (struct CadetTunnel *t)
506{
507 GNUNET_free_non_null (t->ax.DHRs);
508 t->ax.DHRs = GNUNET_CRYPTO_ecdhe_key_create ();
509}
510
511
512/* ************************************** start core crypto ***************************** */
513
514
515/**
516 * Calculate HMAC.
517 *
518 * @param plaintext Content to HMAC.
519 * @param size Size of @c plaintext.
520 * @param iv Initialization vector for the message.
521 * @param key Key to use.
522 * @param hmac[out] Destination to store the HMAC.
523 */
524static void
525t_hmac (const void *plaintext,
526 size_t size,
527 uint32_t iv,
528 const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
529 struct GNUNET_ShortHashCode *hmac)
530{
531 static const char ctx[] = "cadet authentication key";
532 struct GNUNET_CRYPTO_AuthKey auth_key;
533 struct GNUNET_HashCode hash;
534
535 GNUNET_CRYPTO_hmac_derive_key (&auth_key,
536 key,
537 &iv, sizeof (iv),
538 key, sizeof (*key),
539 ctx, sizeof (ctx),
540 NULL);
541 /* Two step: CADET_Hash is only 256 bits, HashCode is 512. */
542 GNUNET_CRYPTO_hmac (&auth_key,
543 plaintext,
544 size,
545 &hash);
546 GNUNET_memcpy (hmac,
547 &hash,
548 sizeof (*hmac));
549}
550
551
552/**
553 * Perform a HMAC.
554 *
555 * @param key Key to use.
556 * @param hash[out] Resulting HMAC.
557 * @param source Source key material (data to HMAC).
558 * @param len Length of @a source.
559 */
560static void
561t_ax_hmac_hash (const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
562 struct GNUNET_HashCode *hash,
563 const void *source,
564 unsigned int len)
565{
566 static const char ctx[] = "axolotl HMAC-HASH";
567 struct GNUNET_CRYPTO_AuthKey auth_key;
568
569 GNUNET_CRYPTO_hmac_derive_key (&auth_key,
570 key,
571 ctx, sizeof (ctx),
572 NULL);
573 GNUNET_CRYPTO_hmac (&auth_key,
574 source,
575 len,
576 hash);
577}
578
579
580/**
581 * Derive a symmetric encryption key from an HMAC-HASH.
582 *
583 * @param key Key to use for the HMAC.
584 * @param[out] out Key to generate.
585 * @param source Source key material (data to HMAC).
586 * @param len Length of @a source.
587 */
588static void
589t_hmac_derive_key (const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
590 struct GNUNET_CRYPTO_SymmetricSessionKey *out,
591 const void *source,
592 unsigned int len)
593{
594 static const char ctx[] = "axolotl derive key";
595 struct GNUNET_HashCode h;
596
597 t_ax_hmac_hash (key,
598 &h,
599 source,
600 len);
601 GNUNET_CRYPTO_kdf (out, sizeof (*out),
602 ctx, sizeof (ctx),
603 &h, sizeof (h),
604 NULL);
605}
606
607
608/**
609 * Encrypt data with the axolotl tunnel key.
610 *
611 * @param t Tunnel whose key to use.
612 * @param dst Destination with @a size bytes for the encrypted data.
613 * @param src Source of the plaintext. Can overlap with @c dst, must contain @a size bytes
614 * @param size Size of the buffers at @a src and @a dst
615 */
616static void
617t_ax_encrypt (struct CadetTunnel *t,
618 void *dst,
619 const void *src,
620 size_t size)
621{
622 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
623 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
624 struct CadetTunnelAxolotl *ax;
625 size_t out_size;
626
627 ax = &t->ax;
628 ax->ratchet_counter++;
629 if ( (GNUNET_YES == ax->ratchet_allowed) &&
630 ( (ratchet_messages <= ax->ratchet_counter) ||
631 (0 == GNUNET_TIME_absolute_get_remaining (ax->ratchet_expiration).rel_value_us)) )
632 {
633 ax->ratchet_flag = GNUNET_YES;
634 }
635 if (GNUNET_YES == ax->ratchet_flag)
636 {
637 /* Advance ratchet */
638 struct GNUNET_CRYPTO_SymmetricSessionKey keys[3];
639 struct GNUNET_HashCode dh;
640 struct GNUNET_HashCode hmac;
641 static const char ctx[] = "axolotl ratchet";
642
643 new_ephemeral (t);
644 ax->HKs = ax->NHKs;
645
646 /* RK, NHKs, CKs = KDF( HMAC-HASH(RK, DH(DHRs, DHRr)) ) */
647 GNUNET_CRYPTO_ecc_ecdh (ax->DHRs,
648 &ax->DHRr,
649 &dh);
650 t_ax_hmac_hash (&ax->RK,
651 &hmac,
652 &dh,
653 sizeof (dh));
654 GNUNET_CRYPTO_kdf (keys, sizeof (keys),
655 ctx, sizeof (ctx),
656 &hmac, sizeof (hmac),
657 NULL);
658 ax->RK = keys[0];
659 ax->NHKs = keys[1];
660 ax->CKs = keys[2];
661
662 ax->PNs = ax->Ns;
663 ax->Ns = 0;
664 ax->ratchet_flag = GNUNET_NO;
665 ax->ratchet_allowed = GNUNET_NO;
666 ax->ratchet_counter = 0;
667 ax->ratchet_expiration
668 = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
669 ratchet_time);
670 }
671
672 t_hmac_derive_key (&ax->CKs,
673 &MK,
674 "0",
675 1);
676 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
677 &MK,
678 NULL, 0,
679 NULL);
680
681 out_size = GNUNET_CRYPTO_symmetric_encrypt (src,
682 size,
683 &MK,
684 &iv,
685 dst);
686 GNUNET_assert (size == out_size);
687 t_hmac_derive_key (&ax->CKs,
688 &ax->CKs,
689 "1",
690 1);
691}
692
693
694/**
695 * Decrypt data with the axolotl tunnel key.
696 *
697 * @param t Tunnel whose key to use.
698 * @param dst Destination for the decrypted data, must contain @a size bytes.
699 * @param src Source of the ciphertext. Can overlap with @c dst, must contain @a size bytes.
700 * @param size Size of the @a src and @a dst buffers
701 */
702static void
703t_ax_decrypt (struct CadetTunnel *t,
704 void *dst,
705 const void *src,
706 size_t size)
707{
708 struct GNUNET_CRYPTO_SymmetricSessionKey MK;
709 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
710 struct CadetTunnelAxolotl *ax;
711 size_t out_size;
712
713 ax = &t->ax;
714 t_hmac_derive_key (&ax->CKr,
715 &MK,
716 "0",
717 1);
718 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
719 &MK,
720 NULL, 0,
721 NULL);
722 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
723 out_size = GNUNET_CRYPTO_symmetric_decrypt (src,
724 size,
725 &MK,
726 &iv,
727 dst);
728 GNUNET_assert (out_size == size);
729 t_hmac_derive_key (&ax->CKr,
730 &ax->CKr,
731 "1",
732 1);
733}
734
735
736/**
737 * Encrypt header with the axolotl header key.
738 *
739 * @param t Tunnel whose key to use.
740 * @param msg Message whose header to encrypt.
741 */
742static void
743t_h_encrypt (struct CadetTunnel *t,
744 struct GNUNET_CADET_TunnelEncryptedMessage *msg)
745{
746 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
747 struct CadetTunnelAxolotl *ax;
748 size_t out_size;
749
750 ax = &t->ax;
751 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
752 &ax->HKs,
753 NULL, 0,
754 NULL);
755 out_size = GNUNET_CRYPTO_symmetric_encrypt (&msg->Ns,
756 AX_HEADER_SIZE,
757 &ax->HKs,
758 &iv,
759 &msg->Ns);
760 GNUNET_assert (AX_HEADER_SIZE == out_size);
761}
762
763
764/**
765 * Decrypt header with the current axolotl header key.
766 *
767 * @param t Tunnel whose current ax HK to use.
768 * @param src Message whose header to decrypt.
769 * @param dst Where to decrypt header to.
770 */
771static void
772t_h_decrypt (struct CadetTunnel *t,
773 const struct GNUNET_CADET_TunnelEncryptedMessage *src,
774 struct GNUNET_CADET_TunnelEncryptedMessage *dst)
775{
776 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
777 struct CadetTunnelAxolotl *ax;
778 size_t out_size;
779
780 ax = &t->ax;
781 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
782 &ax->HKr,
783 NULL, 0,
784 NULL);
785 out_size = GNUNET_CRYPTO_symmetric_decrypt (&src->Ns,
786 AX_HEADER_SIZE,
787 &ax->HKr,
788 &iv,
789 &dst->Ns);
790 GNUNET_assert (AX_HEADER_SIZE == out_size);
791}
792
793
794/**
795 * Delete a key from the list of skipped keys.
796 *
797 * @param t Tunnel to delete from.
798 * @param key Key to delete.
799 */
800static void
801delete_skipped_key (struct CadetTunnel *t,
802 struct CadetTunnelSkippedKey *key)
803{
804 GNUNET_CONTAINER_DLL_remove (t->ax.skipped_head,
805 t->ax.skipped_tail,
806 key);
807 GNUNET_free (key);
808 t->ax.skipped--;
809}
810
811
812/**
813 * Decrypt and verify data with the appropriate tunnel key and verify that the
814 * data has not been altered since it was sent by the remote peer.
815 *
816 * @param t Tunnel whose key to use.
817 * @param dst Destination for the plaintext.
818 * @param src Source of the message. Can overlap with @c dst.
819 * @param size Size of the message.
820 * @return Size of the decrypted data, -1 if an error was encountered.
821 */
822static ssize_t
823try_old_ax_keys (struct CadetTunnel *t,
824 void *dst,
825 const struct GNUNET_CADET_TunnelEncryptedMessage *src,
826 size_t size)
827{
828 struct CadetTunnelSkippedKey *key;
829 struct GNUNET_ShortHashCode *hmac;
830 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
831 struct GNUNET_CADET_TunnelEncryptedMessage plaintext_header;
832 struct GNUNET_CRYPTO_SymmetricSessionKey *valid_HK;
833 size_t esize;
834 size_t res;
835 size_t len;
836 unsigned int N;
837
838 LOG (GNUNET_ERROR_TYPE_DEBUG,
839 "Trying skipped keys\n");
840 hmac = &plaintext_header.hmac;
841 esize = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
842
843 /* Find a correct Header Key */
844 valid_HK = NULL;
845 for (key = t->ax.skipped_head; NULL != key; key = key->next)
846 {
847 t_hmac (&src->Ns,
848 AX_HEADER_SIZE + esize,
849 0,
850 &key->HK,
851 hmac);
852 if (0 == memcmp (hmac,
853 &src->hmac,
854 sizeof (*hmac)))
855 {
856 valid_HK = &key->HK;
857 break;
858 }
859 }
860 if (NULL == key)
861 return -1;
862
863 /* Should've been checked in -cadet_connection.c handle_cadet_encrypted. */
864 GNUNET_assert (size > sizeof (struct GNUNET_CADET_TunnelEncryptedMessage));
865 len = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
866 GNUNET_assert (len >= sizeof (struct GNUNET_MessageHeader));
867
868 /* Decrypt header */
869 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
870 &key->HK,
871 NULL, 0,
872 NULL);
873 res = GNUNET_CRYPTO_symmetric_decrypt (&src->Ns,
874 AX_HEADER_SIZE,
875 &key->HK,
876 &iv,
877 &plaintext_header.Ns);
878 GNUNET_assert (AX_HEADER_SIZE == res);
879
880 /* Find the correct message key */
881 N = ntohl (plaintext_header.Ns);
882 while ( (NULL != key) &&
883 (N != key->Kn) )
884 key = key->next;
885 if ( (NULL == key) ||
886 (0 != memcmp (&key->HK,
887 valid_HK,
888 sizeof (*valid_HK))) )
889 return -1;
890
891 /* Decrypt payload */
892 GNUNET_CRYPTO_symmetric_derive_iv (&iv,
893 &key->MK,
894 NULL,
895 0,
896 NULL);
897 res = GNUNET_CRYPTO_symmetric_decrypt (&src[1],
898 len,
899 &key->MK,
900 &iv,
901 dst);
902 delete_skipped_key (t,
903 key);
904 return res;
905}
906
907
908/**
909 * Delete a key from the list of skipped keys.
910 *
911 * @param t Tunnel to delete from.
912 * @param HKr Header Key to use.
913 */
914static void
915store_skipped_key (struct CadetTunnel *t,
916 const struct GNUNET_CRYPTO_SymmetricSessionKey *HKr)
917{
918 struct CadetTunnelSkippedKey *key;
919
920 key = GNUNET_new (struct CadetTunnelSkippedKey);
921 key->timestamp = GNUNET_TIME_absolute_get ();
922 key->Kn = t->ax.Nr;
923 key->HK = t->ax.HKr;
924 t_hmac_derive_key (&t->ax.CKr,
925 &key->MK,
926 "0",
927 1);
928 t_hmac_derive_key (&t->ax.CKr,
929 &t->ax.CKr,
930 "1",
931 1);
932 GNUNET_CONTAINER_DLL_insert (t->ax.skipped_head,
933 t->ax.skipped_tail,
934 key);
935 t->ax.skipped++;
936 t->ax.Nr++;
937}
938
939
940/**
941 * Stage skipped AX keys and calculate the message key.
942 * Stores each HK and MK for skipped messages.
943 *
944 * @param t Tunnel where to stage the keys.
945 * @param HKr Header key.
946 * @param Np Received meesage number.
947 * @return #GNUNET_OK if keys were stored.
948 * #GNUNET_SYSERR if an error ocurred (Np not expected).
949 */
950static int
951store_ax_keys (struct CadetTunnel *t,
952 const struct GNUNET_CRYPTO_SymmetricSessionKey *HKr,
953 uint32_t Np)
954{
955 int gap;
956
957 gap = Np - t->ax.Nr;
958 LOG (GNUNET_ERROR_TYPE_DEBUG,
959 "Storing skipped keys [%u, %u)\n",
960 t->ax.Nr,
961 Np);
962 if (MAX_KEY_GAP < gap)
963 {
964 /* Avoid DoS (forcing peer to do 2^33 chain HMAC operations) */
965 /* TODO: start new key exchange on return */
966 GNUNET_break_op (0);
967 LOG (GNUNET_ERROR_TYPE_WARNING,
968 "Got message %u, expected %u+\n",
969 Np,
970 t->ax.Nr);
971 return GNUNET_SYSERR;
972 }
973 if (0 > gap)
974 {
975 /* Delayed message: don't store keys, flag to try old keys. */
976 return GNUNET_SYSERR;
977 }
978
979 while (t->ax.Nr < Np)
980 store_skipped_key (t,
981 HKr);
982
983 while (t->ax.skipped > MAX_SKIPPED_KEYS)
984 delete_skipped_key (t,
985 t->ax.skipped_tail);
986 return GNUNET_OK;
987}
988
989
990/**
991 * Decrypt and verify data with the appropriate tunnel key and verify that the
992 * data has not been altered since it was sent by the remote peer.
993 *
994 * @param t Tunnel whose key to use.
995 * @param dst Destination for the plaintext.
996 * @param src Source of the message. Can overlap with @c dst.
997 * @param size Size of the message.
998 * @return Size of the decrypted data, -1 if an error was encountered.
999 */
1000static ssize_t
1001t_ax_decrypt_and_validate (struct CadetTunnel *t,
1002 void *dst,
1003 const struct GNUNET_CADET_TunnelEncryptedMessage *src,
1004 size_t size)
1005{
1006 struct CadetTunnelAxolotl *ax;
1007 struct GNUNET_ShortHashCode msg_hmac;
1008 struct GNUNET_HashCode hmac;
1009 struct GNUNET_CADET_TunnelEncryptedMessage plaintext_header;
1010 uint32_t Np;
1011 uint32_t PNp;
1012 size_t esize; /* Size of encryped payload */
1013
1014 esize = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
1015 ax = &t->ax;
1016
1017 /* Try current HK */
1018 t_hmac (&src->Ns,
1019 AX_HEADER_SIZE + esize,
1020 0, &ax->HKr,
1021 &msg_hmac);
1022 if (0 != memcmp (&msg_hmac,
1023 &src->hmac,
1024 sizeof (msg_hmac)))
1025 {
1026 static const char ctx[] = "axolotl ratchet";
1027 struct GNUNET_CRYPTO_SymmetricSessionKey keys[3]; /* RKp, NHKp, CKp */
1028 struct GNUNET_CRYPTO_SymmetricSessionKey HK;
1029 struct GNUNET_HashCode dh;
1030 struct GNUNET_CRYPTO_EcdhePublicKey *DHRp;
1031
1032 /* Try Next HK */
1033 t_hmac (&src->Ns,
1034 AX_HEADER_SIZE + esize,
1035 0,
1036 &ax->NHKr,
1037 &msg_hmac);
1038 if (0 != memcmp (&msg_hmac,
1039 &src->hmac,
1040 sizeof (msg_hmac)))
1041 {
1042 /* Try the skipped keys, if that fails, we're out of luck. */
1043 return try_old_ax_keys (t,
1044 dst,
1045 src,
1046 size);
1047 }
1048 HK = ax->HKr;
1049 ax->HKr = ax->NHKr;
1050 t_h_decrypt (t,
1051 src,
1052 &plaintext_header);
1053 Np = ntohl (plaintext_header.Ns);
1054 PNp = ntohl (plaintext_header.PNs);
1055 DHRp = &plaintext_header.DHRs;
1056 store_ax_keys (t,
1057 &HK,
1058 PNp);
1059
1060 /* RKp, NHKp, CKp = KDF (HMAC-HASH (RK, DH (DHRp, DHRs))) */
1061 GNUNET_CRYPTO_ecc_ecdh (ax->DHRs,
1062 DHRp,
1063 &dh);
1064 t_ax_hmac_hash (&ax->RK,
1065 &hmac,
1066 &dh, sizeof (dh));
1067 GNUNET_CRYPTO_kdf (keys, sizeof (keys),
1068 ctx, sizeof (ctx),
1069 &hmac, sizeof (hmac),
1070 NULL);
1071
1072 /* Commit "purported" keys */
1073 ax->RK = keys[0];
1074 ax->NHKr = keys[1];
1075 ax->CKr = keys[2];
1076 ax->DHRr = *DHRp;
1077 ax->Nr = 0;
1078 ax->ratchet_allowed = GNUNET_YES;
1079 }
1080 else
1081 {
1082 t_h_decrypt (t,
1083 src,
1084 &plaintext_header);
1085 Np = ntohl (plaintext_header.Ns);
1086 PNp = ntohl (plaintext_header.PNs);
1087 }
1088 if ( (Np != ax->Nr) &&
1089 (GNUNET_OK != store_ax_keys (t,
1090 &ax->HKr,
1091 Np)) )
1092 {
1093 /* Try the skipped keys, if that fails, we're out of luck. */
1094 return try_old_ax_keys (t,
1095 dst,
1096 src,
1097 size);
1098 }
1099
1100 t_ax_decrypt (t,
1101 dst,
1102 &src[1],
1103 esize);
1104 ax->Nr = Np + 1;
1105 return esize;
1106}
1107
1108
1109/**
1110 * Send a KX message.
1111 *
1112 * FIXME: does not take care of sender-authentication yet!
1113 *
1114 * @param t Tunnel on which to send it.
1115 * @param force_reply Force the other peer to reply with a KX message.
1116 */
1117static void
1118send_kx (struct CadetTunnel *t,
1119 int force_reply)
1120{
1121 struct CadetTunnelAxolotl *ax = &t->ax;
1122 struct CadetConnection *c;
1123 struct GNUNET_MQ_Envelope *env;
1124 struct GNUNET_CADET_TunnelKeyExchangeMessage *msg;
1125 enum GNUNET_CADET_KX_Flags flags;
1126
1127#if FIXME
1128 if (NULL != t->ephm_h)
1129 {
1130 LOG (GNUNET_ERROR_TYPE_INFO,
1131 " already queued, nop\n");
1132 return;
1133 }
1134#endif
1135 c = NULL; // FIXME: figure out where to transmit...
1136
1137 // GNUNET_assert (GNUNET_NO == GCT_is_loopback (t));
1138 env = GNUNET_MQ_msg (msg,
1139 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX);
1140 flags = GNUNET_CADET_KX_FLAG_NONE;
1141 if (GNUNET_YES == force_reply)
1142 flags |= GNUNET_CADET_KX_FLAG_FORCE_REPLY;
1143 msg->flags = htonl (flags);
1144 msg->cid = *GCC_get_id (c);
1145 GNUNET_CRYPTO_ecdhe_key_get_public (ax->kx_0,
1146 &msg->ephemeral_key);
1147 GNUNET_CRYPTO_ecdhe_key_get_public (ax->DHRs,
1148 &msg->ratchet_key);
1149
1150 // FIXME: send 'env'.
1151#if FIXME
1152 t->ephm_h = GCC_send_prebuilt_message (&msg.header,
1153 UINT16_MAX,
1154 zero,
1155 c,
1156 GCC_is_origin (c, GNUNET_YES),
1157 GNUNET_YES, &ephm_sent, t);
1158 if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
1159 GCT_change_estate (t, CADET_TUNNEL_KEY_SENT);
1160#endif
1161}
1162
1163
1164/**
1165 * Handle KX message.
1166 *
1167 * FIXME: sender-authentication in KX is missing!
1168 *
1169 * @param ct connection/tunnel combo that received encrypted message
1170 * @param msg the key exchange message
1171 */
1172void
1173GCT_handle_kx (struct CadetTConnection *ct,
1174 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
1175{
1176 struct CadetTunnel *t = ct->t;
1177 struct CadetTunnelAxolotl *ax = &t->ax;
1178 struct GNUNET_HashCode key_material[3];
1179 struct GNUNET_CRYPTO_SymmetricSessionKey keys[5];
1180 const char salt[] = "CADET Axolotl salt";
1181 const struct GNUNET_PeerIdentity *pid;
1182 int am_I_alice;
1183
1184 pid = GCP_get_id (t->destination);
1185 if (0 > GNUNET_CRYPTO_cmp_peer_identity (&my_full_id,
1186 pid))
1187 am_I_alice = GNUNET_YES;
1188 else if (0 < GNUNET_CRYPTO_cmp_peer_identity (&my_full_id,
1189 pid))
1190 am_I_alice = GNUNET_NO;
1191 else
1192 {
1193 GNUNET_break_op (0);
1194 return;
1195 }
1196
1197 if (0 != (GNUNET_CADET_KX_FLAG_FORCE_REPLY & ntohl (msg->flags)))
1198 {
1199 if (NULL != t->rekey_task)
1200 {
1201 GNUNET_SCHEDULER_cancel (t->rekey_task);
1202 t->rekey_task = NULL;
1203 }
1204 send_kx (t,
1205 GNUNET_NO);
1206 }
1207
1208 if (0 == memcmp (&ax->DHRr,
1209 &msg->ratchet_key,
1210 sizeof (msg->ratchet_key)))
1211 {
1212 LOG (GNUNET_ERROR_TYPE_INFO,
1213 " known ratchet key, exit\n");
1214 return;
1215 }
1216
1217 ax->DHRr = msg->ratchet_key;
1218
1219 /* ECDH A B0 */
1220 if (GNUNET_YES == am_I_alice)
1221 {
1222 GNUNET_CRYPTO_eddsa_ecdh (my_private_key, /* A */
1223 &msg->ephemeral_key, /* B0 */
1224 &key_material[0]);
1225 }
1226 else
1227 {
1228 GNUNET_CRYPTO_ecdh_eddsa (ax->kx_0, /* B0 */
1229 &pid->public_key, /* A */
1230 &key_material[0]);
1231 }
1232
1233 /* ECDH A0 B */
1234 if (GNUNET_YES == am_I_alice)
1235 {
1236 GNUNET_CRYPTO_ecdh_eddsa (ax->kx_0, /* A0 */
1237 &pid->public_key, /* B */
1238 &key_material[1]);
1239 }
1240 else
1241 {
1242 GNUNET_CRYPTO_eddsa_ecdh (my_private_key, /* A */
1243 &msg->ephemeral_key, /* B0 */
1244 &key_material[1]);
1245
1246
1247 }
1248
1249 /* ECDH A0 B0 */
1250 /* (This is the triple-DH, we could probably safely skip this,
1251 as A0/B0 are already in the key material.) */
1252 GNUNET_CRYPTO_ecc_ecdh (ax->kx_0, /* A0 or B0 */
1253 &msg->ephemeral_key, /* B0 or A0 */
1254 &key_material[2]);
1255
1256 /* KDF */
1257 GNUNET_CRYPTO_kdf (keys, sizeof (keys),
1258 salt, sizeof (salt),
1259 &key_material, sizeof (key_material),
1260 NULL);
1261
1262 if (0 == memcmp (&ax->RK,
1263 &keys[0],
1264 sizeof (ax->RK)))
1265 {
1266 LOG (GNUNET_ERROR_TYPE_INFO,
1267 " known handshake key, exit\n");
1268 return;
1269 }
1270 ax->RK = keys[0];
1271 if (GNUNET_YES == am_I_alice)
1272 {
1273 ax->HKr = keys[1];
1274 ax->NHKs = keys[2];
1275 ax->NHKr = keys[3];
1276 ax->CKr = keys[4];
1277 ax->ratchet_flag = GNUNET_YES;
1278 }
1279 else
1280 {
1281 ax->HKs = keys[1];
1282 ax->NHKr = keys[2];
1283 ax->NHKs = keys[3];
1284 ax->CKs = keys[4];
1285 ax->ratchet_flag = GNUNET_NO;
1286 ax->ratchet_allowed = GNUNET_NO;
1287 ax->ratchet_counter = 0;
1288 ax->ratchet_expiration
1289 = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
1290 ratchet_time);
1291 }
1292 ax->PNs = 0;
1293 ax->Nr = 0;
1294 ax->Ns = 0;
1295
1296#if FIXME
1297 /* After KX is done, update state machine and begin transmissions... */
1298 GCT_change_estate (t,
1299 CADET_TUNNEL_KEY_PING);
1300 send_queued_data (t);
1301#endif
1302}
1303
1304
1305/* ************************************** end core crypto ***************************** */
1306
1307
1308/**
1309 * Add a channel to a tunnel.
1310 *
1311 * @param t Tunnel.
1312 * @param ch Channel
1313 * @return unique number identifying @a ch within @a t
1314 */
1315struct GNUNET_CADET_ChannelTunnelNumber
1316GCT_add_channel (struct CadetTunnel *t,
1317 struct CadetChannel *ch)
1318{
1319 struct GNUNET_CADET_ChannelTunnelNumber ret;
1320 uint32_t chid;
1321
1322 chid = ntohl (t->next_chid.cn);
1323 while (NULL !=
1324 GNUNET_CONTAINER_multihashmap32_get (t->channels,
1325 chid))
1326 chid++;
1327 GNUNET_assert (GNUNET_YES ==
1328 GNUNET_CONTAINER_multihashmap32_put (t->channels,
1329 chid,
1330 ch,
1331 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1332 t->next_chid.cn = htonl (chid + 1);
1333 ret.cn = htonl (chid);
1334 return ret;
1335}
1336
1337
1338/**
1339 * This tunnel is no longer used, destroy it.
1340 *
1341 * @param cls the idle tunnel
1342 */
1343static void
1344destroy_tunnel (void *cls)
1345{
1346 struct CadetTunnel *t = cls;
1347 struct CadetTConnection *ct;
1348 struct CadetTunnelQueueEntry *tqe;
1349
1350 t->destroy_task = NULL;
1351 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (t->channels));
1352 while (NULL != (ct = t->connection_head))
1353 {
1354 GNUNET_assert (ct->t == t);
1355 GNUNET_CONTAINER_DLL_remove (t->connection_head,
1356 t->connection_tail,
1357 ct);
1358 GCC_destroy (ct->cc);
1359 GNUNET_free (ct);
1360 }
1361 while (NULL != (tqe = t->tq_head))
1362 {
1363 GNUNET_CONTAINER_DLL_remove (t->tq_head,
1364 t->tq_tail,
1365 tqe);
1366 GNUNET_MQ_discard (tqe->env);
1367 GNUNET_free (tqe);
1368 }
1369 GCP_drop_tunnel (t->destination,
1370 t);
1371 GNUNET_CONTAINER_multihashmap32_destroy (t->channels);
1372 if (NULL != t->maintain_connections_task)
1373 {
1374 GNUNET_SCHEDULER_cancel (t->maintain_connections_task);
1375 t->maintain_connections_task = NULL;
1376 }
1377 GNUNET_MST_destroy (t->mst);
1378 GNUNET_MQ_destroy (t->mq);
1379 GNUNET_free (t);
1380}
1381
1382
1383/**
1384 * A connection is @a is_ready for transmission. Looks at our message
1385 * queue and if there is a message, sends it out via the connection.
1386 *
1387 * @param cls the `struct CadetTConnection` that is @a is_ready
1388 * @param is_ready #GNUNET_YES if connection are now ready,
1389 * #GNUNET_NO if connection are no longer ready
1390 */
1391static void
1392connection_ready_cb (void *cls,
1393 int is_ready)
1394{
1395 struct CadetTConnection *ct = cls;
1396 struct CadetTunnel *t = ct->t;
1397 struct CadetTunnelQueueEntry *tq = t->tq_head;
1398
1399 if (GNUNET_NO == ct->is_ready)
1400 {
1401 ct->is_ready = GNUNET_NO;
1402 return;
1403 }
1404 ct->is_ready = GNUNET_YES;
1405 if (NULL == tq)
1406 return; /* no messages pending right now */
1407
1408 /* ready to send message 'tq' on tunnel 'ct' */
1409 GNUNET_assert (t == tq->t);
1410 GNUNET_CONTAINER_DLL_remove (t->tq_head,
1411 t->tq_tail,
1412 tq);
1413 if (NULL != tq->cid)
1414 *tq->cid = *GCC_get_id (ct->cc);
1415 ct->is_ready = GNUNET_NO;
1416 GCC_transmit (ct->cc,
1417 tq->env);
1418 tq->cont (tq->cont_cls);
1419 GNUNET_free (tq);
1420}
1421
1422
1423/**
1424 * Called when either we have a new connection, or a new message in the
1425 * queue, or some existing connection has transmission capacity. Looks
1426 * at our message queue and if there is a message, picks a connection
1427 * to send it on.
1428 *
1429 * FIXME: yuck... Need better selection logic!
1430 *
1431 * @param t tunnel to process messages on
1432 */
1433static void
1434trigger_transmissions (struct CadetTunnel *t)
1435{
1436 struct CadetTConnection *ct;
1437
1438 if (NULL == t->tq_head)
1439 return; /* no messages pending right now */
1440 for (ct = t->connection_head;
1441 NULL != ct;
1442 ct = ct->next)
1443 if (GNUNET_YES == ct->is_ready)
1444 break;
1445 if (NULL == ct)
1446 return; /* no connections ready */
1447
1448 /* FIXME: a bit hackish to do it like this... */
1449 connection_ready_cb (ct,
1450 GNUNET_YES);
1451}
1452
1453
1454/**
1455 * Function called to maintain the connections underlying our tunnel.
1456 * Tries to maintain (incl. tear down) connections for the tunnel, and
1457 * if there is a significant change, may trigger transmissions.
1458 *
1459 * Basically, needs to check if there are connections that perform
1460 * badly, and if so eventually kill them and trigger a replacement.
1461 * The strategy is to open one more connection than
1462 * #DESIRED_CONNECTIONS_PER_TUNNEL, and then periodically kick out the
1463 * least-performing one, and then inquire for new ones.
1464 *
1465 * @param cls the `struct CadetTunnel`
1466 */
1467static void
1468maintain_connections_cb (void *cls)
1469{
1470 struct CadetTunnel *t = cls;
1471
1472 GNUNET_break (0); // FIXME: implement!
1473}
1474
1475
1476/**
1477 * Consider using the path @a p for the tunnel @a t.
1478 * The tunnel destination is at offset @a off in path @a p.
1479 *
1480 * @param cls our tunnel
1481 * @param path a path to our destination
1482 * @param off offset of the destination on path @a path
1483 * @return #GNUNET_YES (should keep iterating)
1484 */
1485static int
1486consider_path_cb (void *cls,
1487 struct CadetPeerPath *path,
1488 unsigned int off)
1489{
1490 struct CadetTunnel *t = cls;
1491 unsigned int min_length = UINT_MAX;
1492 GNUNET_CONTAINER_HeapCostType max_desire = 0;
1493 struct CadetTConnection *ct;
1494
1495 /* Check if we care about the new path. */
1496 for (ct = t->connection_head;
1497 NULL != ct;
1498 ct = ct->next)
1499 {
1500 struct CadetPeerPath *ps;
1501
1502 ps = GCC_get_path (ct->cc);
1503 if (ps == path)
1504 return GNUNET_YES; /* duplicate */
1505 min_length = GNUNET_MIN (min_length,
1506 GCPP_get_length (ps));
1507 max_desire = GNUNET_MAX (max_desire,
1508 GCPP_get_desirability (ps));
1509 }
1510
1511 /* FIXME: not sure we should really just count
1512 'num_connections' here, as they may all have
1513 consistently failed to connect. */
1514
1515 /* We iterate by increasing path length; if we have enough paths and
1516 this one is more than twice as long than what we are currently
1517 using, then ignore all of these super-long ones! */
1518 if ( (t->num_connections > DESIRED_CONNECTIONS_PER_TUNNEL) &&
1519 (min_length * 2 < off) )
1520 {
1521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1522 "Ignoring paths of length %u, they are way too long.\n",
1523 min_length * 2);
1524 return GNUNET_NO;
1525 }
1526 /* If we have enough paths and this one looks no better, ignore it. */
1527 if ( (t->num_connections >= DESIRED_CONNECTIONS_PER_TUNNEL) &&
1528 (min_length < GCPP_get_length (path)) &&
1529 (max_desire > GCPP_get_desirability (path)) )
1530 {
1531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1532 "Ignoring path (%u/%llu) to %s, got something better already.\n",
1533 GCPP_get_length (path),
1534 (unsigned long long) GCPP_get_desirability (path),
1535 GCP_2s (t->destination));
1536 return GNUNET_YES;
1537 }
1538
1539 /* Path is interesting (better by some metric, or we don't have
1540 enough paths yet). */
1541 ct = GNUNET_new (struct CadetTConnection);
1542 ct->created = GNUNET_TIME_absolute_get ();
1543 ct->t = t;
1544 ct->cc = GCC_create (t->destination,
1545 path,
1546 ct,
1547 &connection_ready_cb,
1548 ct);
1549 /* FIXME: schedule job to kill connection (and path?) if it takes
1550 too long to get ready! (And track performance data on how long
1551 other connections took with the tunnel!)
1552 => Note: to be done within 'connection'-logic! */
1553 GNUNET_CONTAINER_DLL_insert (t->connection_head,
1554 t->connection_tail,
1555 ct);
1556 t->num_connections++;
1557 return GNUNET_YES;
1558}
1559
1560
1561/**
1562 * Consider using the path @a p for the tunnel @a t.
1563 * The tunnel destination is at offset @a off in path @a p.
1564 *
1565 * @param cls our tunnel
1566 * @param path a path to our destination
1567 * @param off offset of the destination on path @a path
1568 */
1569void
1570GCT_consider_path (struct CadetTunnel *t,
1571 struct CadetPeerPath *p,
1572 unsigned int off)
1573{
1574 (void) consider_path_cb (t,
1575 p,
1576 off);
1577}
1578
1579
1580/**
1581 *
1582 *
1583 * @param cls the `struct CadetTunnel` for which we decrypted the message
1584 * @param msg the message we received on the tunnel
1585 */
1586static void
1587handle_plaintext_keepalive (void *cls,
1588 const struct GNUNET_MessageHeader *msg)
1589{
1590 struct CadetTunnel *t = cls;
1591 GNUNET_break (0); // FIXME
1592}
1593
1594
1595/**
1596 * Check that @a msg is well-formed.
1597 *
1598 * @param cls the `struct CadetTunnel` for which we decrypted the message
1599 * @param msg the message we received on the tunnel
1600 * @return #GNUNET_OK (any variable-size payload goes)
1601 */
1602static int
1603check_plaintext_data (void *cls,
1604 const struct GNUNET_CADET_ChannelAppDataMessage *msg)
1605{
1606 return GNUNET_OK;
1607}
1608
1609
1610/**
1611 *
1612 *
1613 * @param cls the `struct CadetTunnel` for which we decrypted the message
1614 * @param msg the message we received on the tunnel
1615 */
1616static void
1617handle_plaintext_data (void *cls,
1618 const struct GNUNET_CADET_ChannelAppDataMessage *msg)
1619{
1620 struct CadetTunnel *t = cls;
1621 GNUNET_break (0); // FIXME!
1622}
1623
1624
1625/**
1626 *
1627 *
1628 * @param cls the `struct CadetTunnel` for which we decrypted the message
1629 * @param ack the message we received on the tunnel
1630 */
1631static void
1632handle_plaintext_data_ack (void *cls,
1633 const struct GNUNET_CADET_ChannelDataAckMessage *ack)
1634{
1635 struct CadetTunnel *t = cls;
1636 GNUNET_break (0); // FIXME!
1637}
1638
1639
1640/**
1641 *
1642 *
1643 * @param cls the `struct CadetTunnel` for which we decrypted the message
1644 * @param cc the message we received on the tunnel
1645 */
1646static void
1647handle_plaintext_channel_create (void *cls,
1648 const struct GNUNET_CADET_ChannelOpenMessage *cc)
1649{
1650 struct CadetTunnel *t = cls;
1651 GNUNET_break (0); // FIXME!
1652}
1653
1654
1655/**
1656 *
1657 *
1658 * @param cls the `struct CadetTunnel` for which we decrypted the message
1659 * @param cm the message we received on the tunnel
1660 */
1661static void
1662handle_plaintext_channel_nack (void *cls,
1663 const struct GNUNET_CADET_ChannelManageMessage *cm)
1664{
1665 struct CadetTunnel *t = cls;
1666 GNUNET_break (0); // FIXME!
1667}
1668
1669
1670/**
1671 *
1672 *
1673 * @param cls the `struct CadetTunnel` for which we decrypted the message
1674 * @param cm the message we received on the tunnel
1675 */
1676static void
1677handle_plaintext_channel_ack (void *cls,
1678 const struct GNUNET_CADET_ChannelManageMessage *cm)
1679{
1680 struct CadetTunnel *t = cls;
1681 GNUNET_break (0); // FIXME!
1682}
1683
1684
1685/**
1686 * We received a message saying that a channel should be destroyed.
1687 * Pass it on to the correct channel.
1688 *
1689 * @param cls the `struct CadetTunnel` for which we decrypted the message
1690 * @param cm the message we received on the tunnel
1691 */
1692static void
1693handle_plaintext_channel_destroy (void *cls,
1694 const struct GNUNET_CADET_ChannelManageMessage *cm)
1695{
1696 struct CadetTunnel *t = cls;
1697 struct CadetChannel *cc = lookup_channel (t,
1698 cm->chid);
1699
1700 GCCH_channel_remote_destroy (cc);
1701}
1702
1703
1704/**
1705 * Handles a message we decrypted, by injecting it into
1706 * our message queue (which will do the dispatching).
1707 *
1708 * @param cls the `struct CadetTunnel` that got the message
1709 * @param msg the message
1710 * @return #GNUNET_OK (continue to process)
1711 */
1712static int
1713handle_decrypted (void *cls,
1714 const struct GNUNET_MessageHeader *msg)
1715{
1716 struct CadetTunnel *t = cls;
1717
1718 GNUNET_MQ_inject_message (t->mq,
1719 msg);
1720 return GNUNET_OK;
1721}
1722
1723
1724/**
1725 * Function called if we had an error processing
1726 * an incoming decrypted message.
1727 *
1728 * @param cls the `struct CadetTunnel`
1729 * @param error error code
1730 */
1731static void
1732decrypted_error_cb (void *cls,
1733 enum GNUNET_MQ_Error error)
1734{
1735 GNUNET_break_op (0);
1736}
1737
1738
1739/**
1740 * Create a tunnel to @a destionation. Must only be called
1741 * from within #GCP_get_tunnel().
1742 *
1743 * @param destination where to create the tunnel to
1744 * @return new tunnel to @a destination
1745 */
1746struct CadetTunnel *
1747GCT_create_tunnel (struct CadetPeer *destination)
1748{
1749 struct GNUNET_MQ_MessageHandler handlers[] = {
1750 GNUNET_MQ_hd_fixed_size (plaintext_keepalive,
1751 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE,
1752 struct GNUNET_MessageHeader,
1753 NULL),
1754 GNUNET_MQ_hd_var_size (plaintext_data,
1755 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA,
1756 struct GNUNET_CADET_ChannelAppDataMessage,
1757 NULL),
1758 GNUNET_MQ_hd_fixed_size (plaintext_data_ack,
1759 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK,
1760 struct GNUNET_CADET_ChannelDataAckMessage,
1761 NULL),
1762 GNUNET_MQ_hd_fixed_size (plaintext_channel_create,
1763 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN,
1764 struct GNUNET_CADET_ChannelOpenMessage,
1765 NULL),
1766 GNUNET_MQ_hd_fixed_size (plaintext_channel_nack,
1767 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED,
1768 struct GNUNET_CADET_ChannelManageMessage,
1769 NULL),
1770 GNUNET_MQ_hd_fixed_size (plaintext_channel_ack,
1771 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK,
1772 struct GNUNET_CADET_ChannelManageMessage,
1773 NULL),
1774 GNUNET_MQ_hd_fixed_size (plaintext_channel_destroy,
1775 GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1776 struct GNUNET_CADET_ChannelManageMessage,
1777 NULL),
1778 GNUNET_MQ_handler_end ()
1779 };
1780 struct CadetTunnel *t;
1781
1782 t = GNUNET_new (struct CadetTunnel);
1783 t->destination = destination;
1784 t->channels = GNUNET_CONTAINER_multihashmap32_create (8);
1785 (void) GCP_iterate_paths (destination,
1786 &consider_path_cb,
1787 t);
1788 t->maintain_connections_task
1789 = GNUNET_SCHEDULER_add_now (&maintain_connections_cb,
1790 t);
1791 t->mq = GNUNET_MQ_queue_for_callbacks (NULL,
1792 NULL,
1793 NULL,
1794 NULL,
1795 handlers,
1796 &decrypted_error_cb,
1797 t);
1798 t->mst = GNUNET_MST_create (&handle_decrypted,
1799 t);
1800 return t;
1801}
1802
1803
1804/**
1805 * Remove a channel from a tunnel.
1806 *
1807 * @param t Tunnel.
1808 * @param ch Channel
1809 * @param gid unique number identifying @a ch within @a t
1810 */
1811void
1812GCT_remove_channel (struct CadetTunnel *t,
1813 struct CadetChannel *ch,
1814 struct GNUNET_CADET_ChannelTunnelNumber gid)
1815{
1816 GNUNET_assert (GNUNET_YES ==
1817 GNUNET_CONTAINER_multihashmap32_remove (t->channels,
1818 ntohl (gid.cn),
1819 ch));
1820 if (0 ==
1821 GNUNET_CONTAINER_multihashmap32_size (t->channels))
1822 {
1823 t->destroy_task = GNUNET_SCHEDULER_add_delayed (IDLE_DESTROY_DELAY,
1824 &destroy_tunnel,
1825 t);
1826 }
1827}
1828
1829
1830/**
1831 * Change the tunnel encryption state.
1832 * If the encryption state changes to OK, stop the rekey task.
1833 *
1834 * @param t Tunnel whose encryption state to change, or NULL.
1835 * @param state New encryption state.
1836 */
1837void
1838GCT_change_estate (struct CadetTunnel *t,
1839 enum CadetTunnelEState state)
1840{
1841 enum CadetTunnelEState old = t->estate;
1842
1843 t->estate = state;
1844 LOG (GNUNET_ERROR_TYPE_DEBUG,
1845 "Tunnel %s estate changed from %d to %d\n",
1846 GCT_2s (t),
1847 old,
1848 state);
1849
1850 if ( (CADET_TUNNEL_KEY_OK != old) &&
1851 (CADET_TUNNEL_KEY_OK == t->estate) )
1852 {
1853 if (NULL != t->rekey_task)
1854 {
1855 GNUNET_SCHEDULER_cancel (t->rekey_task);
1856 t->rekey_task = NULL;
1857 }
1858#if FIXME
1859 /* Send queued data if tunnel is not loopback */
1860 if (myid != GCP_get_short_id (t->peer))
1861 send_queued_data (t);
1862#endif
1863 }
1864}
1865
1866
1867/**
1868 * Add a @a connection to the @a tunnel.
1869 *
1870 * @param t a tunnel
1871 * @param cid connection identifer to use for the connection
1872 * @param path path to use for the connection
1873 */
1874void
1875GCT_add_inbound_connection (struct CadetTunnel *t,
1876 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
1877 struct CadetPeerPath *path)
1878{
1879 struct CadetConnection *cc;
1880 struct CadetTConnection *ct;
1881
1882 ct = GNUNET_new (struct CadetTConnection);
1883 ct->created = GNUNET_TIME_absolute_get ();
1884 ct->t = t;
1885 ct->cc = GCC_create_inbound (t->destination,
1886 path,
1887 ct,
1888 cid,
1889 &connection_ready_cb,
1890 t);
1891 /* FIXME: schedule job to kill connection (and path?) if it takes
1892 too long to get ready! (And track performance data on how long
1893 other connections took with the tunnel!)
1894 => Note: to be done within 'connection'-logic! */
1895 GNUNET_CONTAINER_DLL_insert (t->connection_head,
1896 t->connection_tail,
1897 ct);
1898 t->num_connections++;
1899}
1900
1901
1902/**
1903 * Handle encrypted message.
1904 *
1905 * @param ct connection/tunnel combo that received encrypted message
1906 * @param msg the encrypted message to decrypt
1907 */
1908void
1909GCT_handle_encrypted (struct CadetTConnection *ct,
1910 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
1911{
1912 struct CadetTunnel *t = ct->t;
1913 uint16_t size = ntohs (msg->header.size);
1914 char cbuf [size] GNUNET_ALIGN;
1915 ssize_t decrypted_size;
1916
1917 GNUNET_STATISTICS_update (stats,
1918 "# received encrypted",
1919 1,
1920 GNUNET_NO);
1921
1922 decrypted_size = t_ax_decrypt_and_validate (t,
1923 cbuf,
1924 msg,
1925 size);
1926
1927 if (-1 == decrypted_size)
1928 {
1929 GNUNET_STATISTICS_update (stats,
1930 "# unable to decrypt",
1931 1,
1932 GNUNET_NO);
1933 if (CADET_TUNNEL_KEY_PING <= t->estate)
1934 {
1935 GNUNET_break_op (0);
1936 LOG (GNUNET_ERROR_TYPE_WARNING,
1937 "Wrong crypto, tunnel %s\n",
1938 GCT_2s (t));
1939 GCT_debug (t,
1940 GNUNET_ERROR_TYPE_WARNING);
1941 }
1942 return;
1943 }
1944
1945 GCT_change_estate (t,
1946 CADET_TUNNEL_KEY_OK);
1947 /* The MST will ultimately call #handle_decrypted() on each message. */
1948 GNUNET_break_op (GNUNET_OK ==
1949 GNUNET_MST_from_buffer (t->mst,
1950 cbuf,
1951 decrypted_size,
1952 GNUNET_YES,
1953 GNUNET_NO));
1954}
1955
1956
1957/**
1958 * Sends an already built message on a tunnel, encrypting it and
1959 * choosing the best connection if not provided.
1960 *
1961 * @param message Message to send. Function modifies it.
1962 * @param t Tunnel on which this message is transmitted.
1963 * @param cont Continuation to call once message is really sent.
1964 * @param cont_cls Closure for @c cont.
1965 * @return Handle to cancel message. NULL if @c cont is NULL.
1966 */
1967struct CadetTunnelQueueEntry *
1968GCT_send (struct CadetTunnel *t,
1969 const struct GNUNET_MessageHeader *message,
1970 GNUNET_SCHEDULER_TaskCallback cont,
1971 void *cont_cls)
1972{
1973 struct CadetTunnelQueueEntry *tq;
1974 uint16_t payload_size;
1975 struct GNUNET_MQ_Envelope *env;
1976 struct GNUNET_CADET_TunnelEncryptedMessage *ax_msg;
1977
1978 /* FIXME: what about KX not yet being ready? (see "is_ready()" check in old code!) */
1979
1980 payload_size = ntohs (message->size);
1981 env = GNUNET_MQ_msg_extra (ax_msg,
1982 payload_size,
1983 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED);
1984 t_ax_encrypt (t,
1985 &ax_msg[1],
1986 message,
1987 payload_size);
1988 ax_msg->Ns = htonl (t->ax.Ns++);
1989 ax_msg->PNs = htonl (t->ax.PNs);
1990 GNUNET_CRYPTO_ecdhe_key_get_public (t->ax.DHRs,
1991 &ax_msg->DHRs);
1992 t_h_encrypt (t,
1993 ax_msg);
1994 t_hmac (&ax_msg->Ns,
1995 AX_HEADER_SIZE + payload_size,
1996 0,
1997 &t->ax.HKs,
1998 &ax_msg->hmac);
1999 // ax_msg->pid = htonl (GCC_get_pid (c, fwd)); // FIXME: connection flow-control not (re)implemented yet!
2000
2001 tq = GNUNET_malloc (sizeof (*tq));
2002 tq->t = t;
2003 tq->env = env;
2004 tq->cid = &ax_msg->cid;
2005 tq->cont = cont;
2006 tq->cont_cls = cont_cls;
2007 GNUNET_CONTAINER_DLL_insert_tail (t->tq_head,
2008 t->tq_tail,
2009 tq);
2010 trigger_transmissions (t);
2011 return tq;
2012}
2013
2014
2015/**
2016 * Cancel a previously sent message while it's in the queue.
2017 *
2018 * ONLY can be called before the continuation given to the send
2019 * function is called. Once the continuation is called, the message is
2020 * no longer in the queue!
2021 *
2022 * @param q Handle to the queue entry to cancel.
2023 */
2024void
2025GCT_send_cancel (struct CadetTunnelQueueEntry *q)
2026{
2027 struct CadetTunnel *t = q->t;
2028
2029 GNUNET_CONTAINER_DLL_remove (t->tq_head,
2030 t->tq_tail,
2031 q);
2032 GNUNET_free (q);
2033}
2034
2035
2036/**
2037 * Iterate over all connections of a tunnel.
2038 *
2039 * @param t Tunnel whose connections to iterate.
2040 * @param iter Iterator.
2041 * @param iter_cls Closure for @c iter.
2042 */
2043void
2044GCT_iterate_connections (struct CadetTunnel *t,
2045 GCT_ConnectionIterator iter,
2046 void *iter_cls)
2047{
2048 for (struct CadetTConnection *ct = t->connection_head;
2049 NULL != ct;
2050 ct = ct->next)
2051 iter (iter_cls,
2052 ct->cc);
2053}
2054
2055
2056/**
2057 * Closure for #iterate_channels_cb.
2058 */
2059struct ChanIterCls
2060{
2061 /**
2062 * Function to call.
2063 */
2064 GCT_ChannelIterator iter;
2065
2066 /**
2067 * Closure for @e iter.
2068 */
2069 void *iter_cls;
2070};
2071
2072
2073/**
2074 * Helper function for #GCT_iterate_channels.
2075 *
2076 * @param cls the `struct ChanIterCls`
2077 * @param key unused
2078 * @param value a `struct CadetChannel`
2079 * @return #GNUNET_OK
2080 */
2081static int
2082iterate_channels_cb (void *cls,
2083 uint32_t key,
2084 void *value)
2085{
2086 struct ChanIterCls *ctx = cls;
2087 struct CadetChannel *ch = value;
2088
2089 ctx->iter (ctx->iter_cls,
2090 ch);
2091 return GNUNET_OK;
2092}
2093
2094
2095/**
2096 * Iterate over all channels of a tunnel.
2097 *
2098 * @param t Tunnel whose channels to iterate.
2099 * @param iter Iterator.
2100 * @param iter_cls Closure for @c iter.
2101 */
2102void
2103GCT_iterate_channels (struct CadetTunnel *t,
2104 GCT_ChannelIterator iter,
2105 void *iter_cls)
2106{
2107 struct ChanIterCls ctx;
2108
2109 ctx.iter = iter;
2110 ctx.iter_cls = iter_cls;
2111 GNUNET_CONTAINER_multihashmap32_iterate (t->channels,
2112 &iterate_channels_cb,
2113 &ctx);
2114
2115}
2116
2117
2118/**
2119 * Call #GCCH_debug() on a channel.
2120 *
2121 * @param cls points to the log level to use
2122 * @param key unused
2123 * @param value the `struct CadetChannel` to dump
2124 * @return #GNUNET_OK (continue iteration)
2125 */
2126static int
2127debug_channel (void *cls,
2128 uint32_t key,
2129 void *value)
2130{
2131 const enum GNUNET_ErrorType *level = cls;
2132 struct CadetChannel *ch = value;
2133
2134 GCCH_debug (ch, *level);
2135 return GNUNET_OK;
2136}
2137
2138
2139/**
2140 * Get string description for tunnel connectivity state.
2141 *
2142 * @param cs Tunnel state.
2143 *
2144 * @return String representation.
2145 */
2146static const char *
2147cstate2s (enum CadetTunnelCState cs)
2148{
2149 static char buf[32];
2150
2151 switch (cs)
2152 {
2153 case CADET_TUNNEL_NEW:
2154 return "CADET_TUNNEL_NEW";
2155 case CADET_TUNNEL_SEARCHING:
2156 return "CADET_TUNNEL_SEARCHING";
2157 case CADET_TUNNEL_WAITING:
2158 return "CADET_TUNNEL_WAITING";
2159 case CADET_TUNNEL_READY:
2160 return "CADET_TUNNEL_READY";
2161 case CADET_TUNNEL_SHUTDOWN:
2162 return "CADET_TUNNEL_SHUTDOWN";
2163 default:
2164 SPRINTF (buf, "%u (UNKNOWN STATE)", cs);
2165 return buf;
2166 }
2167}
2168
2169
2170/**
2171 * Get string description for tunnel encryption state.
2172 *
2173 * @param es Tunnel state.
2174 *
2175 * @return String representation.
2176 */
2177static const char *
2178estate2s (enum CadetTunnelEState es)
2179{
2180 static char buf[32];
2181
2182 switch (es)
2183 {
2184 case CADET_TUNNEL_KEY_UNINITIALIZED:
2185 return "CADET_TUNNEL_KEY_UNINITIALIZED";
2186 case CADET_TUNNEL_KEY_SENT:
2187 return "CADET_TUNNEL_KEY_SENT";
2188 case CADET_TUNNEL_KEY_PING:
2189 return "CADET_TUNNEL_KEY_PING";
2190 case CADET_TUNNEL_KEY_OK:
2191 return "CADET_TUNNEL_KEY_OK";
2192 case CADET_TUNNEL_KEY_REKEY:
2193 return "CADET_TUNNEL_KEY_REKEY";
2194 default:
2195 SPRINTF (buf, "%u (UNKNOWN STATE)", es);
2196 return buf;
2197 }
2198}
2199
2200
2201#define LOG2(level, ...) GNUNET_log_from_nocheck(level,"cadet-tun",__VA_ARGS__)
2202
2203
2204/**
2205 * Log all possible info about the tunnel state.
2206 *
2207 * @param t Tunnel to debug.
2208 * @param level Debug level to use.
2209 */
2210void
2211GCT_debug (const struct CadetTunnel *t,
2212 enum GNUNET_ErrorType level)
2213{
2214 struct CadetTConnection *iter_c;
2215 int do_log;
2216
2217 do_log = GNUNET_get_log_call_status (level & (~GNUNET_ERROR_TYPE_BULK),
2218 "cadet-tun",
2219 __FILE__, __FUNCTION__, __LINE__);
2220 if (0 == do_log)
2221 return;
2222
2223 LOG2 (level,
2224 "TTT TUNNEL TOWARDS %s in cstate %s, estate %s tq_len: %u #cons: %u\n",
2225 GCT_2s (t),
2226 cstate2s (t->cstate),
2227 estate2s (t->estate),
2228 t->tq_len,
2229 t->num_connections);
2230#if DUMP_KEYS_TO_STDERR
2231 ax_debug (t->ax, level);
2232#endif
2233 LOG2 (level,
2234 "TTT channels:\n");
2235 GNUNET_CONTAINER_multihashmap32_iterate (t->channels,
2236 &debug_channel,
2237 &level);
2238 LOG2 (level,
2239 "TTT connections:\n");
2240 for (iter_c = t->connection_head; NULL != iter_c; iter_c = iter_c->next)
2241 GCC_debug (iter_c->cc,
2242 level);
2243
2244 LOG2 (level,
2245 "TTT TUNNEL END\n");
2246}
2247
2248
2249/* end of gnunet-service-cadet-new_tunnels.c */
diff --git a/src/cadet/gnunet-service-cadet-new_tunnels.h b/src/cadet/gnunet-service-cadet-new_tunnels.h
new file mode 100644
index 000000000..82e4b0da6
--- /dev/null
+++ b/src/cadet/gnunet-service-cadet-new_tunnels.h
@@ -0,0 +1,357 @@
1
2/*
3 This file is part of GNUnet.
4 Copyright (C) 2001-2017 GNUnet e.V.
5
6 GNUnet is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 3, or (at your
9 option) any later version.
10
11 GNUnet is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNUnet; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22/**
23 * @file cadet/gnunet-service-cadet-new_tunnels.h
24 * @brief Information we track per tunnel.
25 * @author Bartlomiej Polot
26 * @author Christian Grothoff
27 */
28#ifndef GNUNET_SERVICE_CADET_TUNNELS_H
29#define GNUNET_SERVICE_CADET_TUNNELS_H
30
31#include "gnunet-service-cadet-new.h"
32#include "cadet_protocol.h"
33
34
35/**
36 * How many connections would we like to have per tunnel?
37 */
38#define DESIRED_CONNECTIONS_PER_TUNNEL 3
39
40
41/**
42 * All the connectivity states a tunnel can be in.
43 */
44enum CadetTunnelCState
45{
46 /**
47 * Uninitialized status, should never appear in operation.
48 */
49 CADET_TUNNEL_NEW,
50
51 /**
52 * No path to the peer known yet.
53 */
54 CADET_TUNNEL_SEARCHING,
55
56 /**
57 * Request sent, not yet answered.
58 */
59 CADET_TUNNEL_WAITING,
60
61 /**
62 * Peer connected and ready to accept data.
63 */
64 CADET_TUNNEL_READY,
65
66 /**
67 * Tunnel being shut down, don't try to keep it alive.
68 */
69 CADET_TUNNEL_SHUTDOWN
70};
71
72
73
74/**
75 * All the encryption states a tunnel can be in.
76 */
77enum CadetTunnelEState
78{
79 /**
80 * Uninitialized status, should never appear in operation.
81 */
82 CADET_TUNNEL_KEY_UNINITIALIZED,
83
84 /**
85 * Ephemeral key sent, waiting for peer's key.
86 */
87 CADET_TUNNEL_KEY_SENT,
88
89 /**
90 * In OTR: New ephemeral key and ping sent, waiting for pong.
91 *
92 * This means that we DO have the peer's ephemeral key, otherwise the
93 * state would be KEY_SENT. We DO NOT have a valid session key (either no
94 * previous key or previous key expired).
95 *
96 *
97 * In Axolotl: Key sent and received but no deciphered traffic yet.
98 *
99 * This means that we can send traffic (otherwise we would never complete
100 * the handshake), but we don't have complete confirmation. Since the first
101 * traffic MUST be a complete channel creation 3-way handshake, no payload
102 * will be sent before confirmation.
103 */
104 CADET_TUNNEL_KEY_PING,
105
106 /**
107 * Handshake completed: session key available.
108 */
109 CADET_TUNNEL_KEY_OK,
110
111 /**
112 * New ephemeral key and ping sent, waiting for pong. Unlike KEY_PING,
113 * we still have a valid session key and therefore we *can* still send
114 * traffic on the tunnel.
115 */
116 CADET_TUNNEL_KEY_REKEY
117};
118
119
120/**
121 * Get the static string for the peer this tunnel is directed.
122 *
123 * @param t Tunnel.
124 *
125 * @return Static string the destination peer's ID.
126 */
127const char *
128GCT_2s (const struct CadetTunnel *t);
129
130
131/**
132 * Create a tunnel to @a destionation. Must only be called
133 * from within #GCP_get_tunnel().
134 *
135 * @param destination where to create the tunnel to
136 * @return new tunnel to @a destination
137 */
138struct CadetTunnel *
139GCT_create_tunnel (struct CadetPeer *destination);
140
141
142/**
143 * Add a @a connection to the @a tunnel.
144 *
145 * @param t a tunnel
146 * @param cid connection identifer to use for the connection
147 * @param path path to use for the connection
148 */
149void
150GCT_add_inbound_connection (struct CadetTunnel *t,
151 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
152 struct CadetPeerPath *path);
153
154
155/**
156 * Return the peer to which this tunnel goes.
157 *
158 * @param t a tunnel
159 * @return the destination of the tunnel
160 */
161struct CadetPeer *
162GCT_get_destination (struct CadetTunnel *t);
163
164
165/**
166 * Consider using the path @a p for the tunnel @a t.
167 * The tunnel destination is at offset @a off in path @a p.
168 *
169 * @param cls our tunnel
170 * @param path a path to our destination
171 * @param off offset of the destination on path @a path
172 */
173void
174GCT_consider_path (struct CadetTunnel *t,
175 struct CadetPeerPath *p,
176 unsigned int off);
177
178
179/**
180 * Add a channel to a tunnel.
181 *
182 * @param t Tunnel.
183 * @param ch Channel
184 * @return unique number identifying @a ch within @a t
185 */
186struct GNUNET_CADET_ChannelTunnelNumber
187GCT_add_channel (struct CadetTunnel *t,
188 struct CadetChannel *ch);
189
190
191/**
192 * Remove a channel from a tunnel.
193 *
194 * @param t Tunnel.
195 * @param ch Channel
196 * @param gid unique number identifying @a ch within @a t
197 */
198void
199GCT_remove_channel (struct CadetTunnel *t,
200 struct CadetChannel *ch,
201 struct GNUNET_CADET_ChannelTunnelNumber gid);
202
203
204/**
205 * Sends an already built message on a tunnel, encrypting it and
206 * choosing the best connection if not provided.
207 *
208 * @param message Message to send. Function modifies it.
209 * @param t Tunnel on which this message is transmitted.
210 * @param cont Continuation to call once message is really sent.
211 * @param cont_cls Closure for @c cont.
212 * @return Handle to cancel message. NULL if @c cont is NULL.
213 */
214struct CadetTunnelQueueEntry *
215GCT_send (struct CadetTunnel *t,
216 const struct GNUNET_MessageHeader *message,
217 GNUNET_SCHEDULER_TaskCallback cont,
218 void *cont_cls);
219
220
221/**
222 * Cancel a previously sent message while it's in the queue.
223 *
224 * ONLY can be called before the continuation given to the send
225 * function is called. Once the continuation is called, the message is
226 * no longer in the queue!
227 *
228 * @param q Handle to the queue entry to cancel.
229 */
230void
231GCT_send_cancel (struct CadetTunnelQueueEntry *q);
232
233
234/**
235 * Return the number of channels using a tunnel.
236 *
237 * @param t tunnel to count obtain the number of channels for
238 * @return number of channels using the tunnel
239 */
240unsigned int
241GCT_count_channels (struct CadetTunnel *t);
242
243
244/**
245 * Return the number of connections available for a tunnel.
246 *
247 * @param t tunnel to count obtain the number of connections for
248 * @return number of connections available for the tunnel
249 */
250unsigned int
251GCT_count_any_connections (struct CadetTunnel *t);
252
253
254/**
255 * Iterator over connections.
256 *
257 * @param cls closure
258 * @param c one of the connections
259 */
260typedef void
261(*GCT_ConnectionIterator) (void *cls,
262 struct CadetConnection *c);
263
264
265/**
266 * Iterate over all connections of a tunnel.
267 *
268 * @param t Tunnel whose connections to iterate.
269 * @param iter Iterator.
270 * @param iter_cls Closure for @c iter.
271 */
272void
273GCT_iterate_connections (struct CadetTunnel *t,
274 GCT_ConnectionIterator iter,
275 void *iter_cls);
276
277
278/**
279 * Iterator over channels.
280 *
281 * @param cls closure
282 * @param ch one of the channels
283 */
284typedef void
285(*GCT_ChannelIterator) (void *cls,
286 struct CadetChannel *ch);
287
288
289/**
290 * Iterate over all channels of a tunnel.
291 *
292 * @param t Tunnel whose channels to iterate.
293 * @param iter Iterator.
294 * @param iter_cls Closure for @c iter.
295 */
296void
297GCT_iterate_channels (struct CadetTunnel *t,
298 GCT_ChannelIterator iter,
299 void *iter_cls);
300
301
302/**
303 * Get the connectivity state of a tunnel.
304 *
305 * @param t Tunnel.
306 *
307 * @return Tunnel's connectivity state.
308 */
309enum CadetTunnelCState
310GCT_get_cstate (struct CadetTunnel *t);
311
312
313/**
314 * Get the encryption state of a tunnel.
315 *
316 * @param t Tunnel.
317 *
318 * @return Tunnel's encryption state.
319 */
320enum CadetTunnelEState
321GCT_get_estate (struct CadetTunnel *t);
322
323
324/**
325 * Handle KX message.
326 *
327 * @param ct connection/tunnel combo that received encrypted message
328 * @param msg the key exchange message
329 */
330void
331GCT_handle_kx (struct CadetTConnection *ct,
332 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg);
333
334
335/**
336 * Handle encrypted message.
337 *
338 * @param ct connection/tunnel combo that received encrypted message
339 * @param msg the encrypted message to decrypt
340 */
341void
342GCT_handle_encrypted (struct CadetTConnection *ct,
343 const struct GNUNET_CADET_TunnelEncryptedMessage *msg);
344
345
346/**
347 * Log all possible info about the tunnel state.
348 *
349 * @param t Tunnel to debug.
350 * @param level Debug level to use.
351 */
352void
353GCT_debug (const struct CadetTunnel *t,
354 enum GNUNET_ErrorType level);
355
356
357#endif
diff --git a/src/cadet/gnunet-service-cadet_channel.c b/src/cadet/gnunet-service-cadet_channel.c
index 22349aa80..dee0c37d7 100644
--- a/src/cadet/gnunet-service-cadet_channel.c
+++ b/src/cadet/gnunet-service-cadet_channel.c
@@ -58,7 +58,7 @@ enum CadetChannelState
58 /** 58 /**
59 * Connection confirmed, ready to carry traffic. 59 * Connection confirmed, ready to carry traffic.
60 */ 60 */
61 CADET_CHANNEL_READY, 61 CADET_CHANNEL_READY
62}; 62};
63 63
64 64
@@ -125,7 +125,7 @@ struct CadetReliableMessage
125 */ 125 */
126 struct GNUNET_TIME_Absolute timestamp; 126 struct GNUNET_TIME_Absolute timestamp;
127 127
128 /* struct GNUNET_CADET_Data with payload */ 128 /* struct GNUNET_CADET_ChannelAppDataMessage with payload */
129}; 129};
130 130
131 131
@@ -216,19 +216,19 @@ struct CadetChannel
216 /** 216 /**
217 * Global channel number ( < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI) 217 * Global channel number ( < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
218 */ 218 */
219 CADET_ChannelNumber gid; 219 struct GNUNET_CADET_ChannelTunnelNumber gid;
220 220
221 /** 221 /**
222 * Local tunnel number for root (owner) client. 222 * Local tunnel number for root (owner) client.
223 * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI or 0 ) 223 * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI or 0 )
224 */ 224 */
225 CADET_ChannelNumber lid_root; 225 struct GNUNET_CADET_ClientChannelNumber lid_root;
226 226
227 /** 227 /**
228 * Local tunnel number for local destination clients (incoming number) 228 * Local tunnel number for local destination clients (incoming number)
229 * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV or 0). 229 * ( >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV or 0).
230 */ 230 */
231 CADET_ChannelNumber lid_dest; 231 struct GNUNET_CADET_ClientChannelNumber lid_dest;
232 232
233 /** 233 /**
234 * Channel state. 234 * Channel state.
@@ -368,7 +368,7 @@ is_loopback (const struct CadetChannel *ch)
368 * @param rel Reliability data for retransmission. 368 * @param rel Reliability data for retransmission.
369 */ 369 */
370static struct CadetReliableMessage * 370static struct CadetReliableMessage *
371copy_message (const struct GNUNET_CADET_Data *msg, uint32_t mid, 371copy_message (const struct GNUNET_CADET_ChannelAppDataMessage *msg, uint32_t mid,
372 struct CadetChannelReliability *rel) 372 struct CadetChannelReliability *rel)
373{ 373{
374 struct CadetReliableMessage *copy; 374 struct CadetReliableMessage *copy;
@@ -378,7 +378,7 @@ copy_message (const struct GNUNET_CADET_Data *msg, uint32_t mid,
378 copy = GNUNET_malloc (sizeof (*copy) + size); 378 copy = GNUNET_malloc (sizeof (*copy) + size);
379 copy->mid = mid; 379 copy->mid = mid;
380 copy->rel = rel; 380 copy->rel = rel;
381 copy->type = GNUNET_MESSAGE_TYPE_CADET_DATA; 381 copy->type = GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA;
382 GNUNET_memcpy (&copy[1], msg, size); 382 GNUNET_memcpy (&copy[1], msg, size);
383 383
384 return copy; 384 return copy;
@@ -393,7 +393,7 @@ copy_message (const struct GNUNET_CADET_Data *msg, uint32_t mid,
393 * @param rel Reliability data to the corresponding direction. 393 * @param rel Reliability data to the corresponding direction.
394 */ 394 */
395static void 395static void
396add_buffered_data (const struct GNUNET_CADET_Data *msg, 396add_buffered_data (const struct GNUNET_CADET_ChannelAppDataMessage *msg,
397 struct CadetChannelReliability *rel) 397 struct CadetChannelReliability *rel)
398{ 398{
399 struct CadetReliableMessage *copy; 399 struct CadetReliableMessage *copy;
@@ -513,11 +513,11 @@ channel_get_options (struct CadetChannel *ch)
513static void 513static void
514send_destroy (struct CadetChannel *ch, int local_only) 514send_destroy (struct CadetChannel *ch, int local_only)
515{ 515{
516 struct GNUNET_CADET_ChannelManage msg; 516 struct GNUNET_CADET_ChannelManageMessage msg;
517 517
518 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY); 518 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
519 msg.header.size = htons (sizeof (msg)); 519 msg.header.size = htons (sizeof (msg));
520 msg.chid = htonl (ch->gid); 520 msg.chid = ch->gid;
521 521
522 /* If root is not NULL, notify. 522 /* If root is not NULL, notify.
523 * If it's NULL, check lid_root. When a local destroy comes in, root 523 * If it's NULL, check lid_root. When a local destroy comes in, root
@@ -526,12 +526,12 @@ send_destroy (struct CadetChannel *ch, int local_only)
526 */ 526 */
527 if (NULL != ch->root) 527 if (NULL != ch->root)
528 GML_send_channel_destroy (ch->root, ch->lid_root); 528 GML_send_channel_destroy (ch->root, ch->lid_root);
529 else if (0 == ch->lid_root && GNUNET_NO == local_only) 529 else if (0 == ch->lid_root.channel_of_client && GNUNET_NO == local_only)
530 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL); 530 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
531 531
532 if (NULL != ch->dest) 532 if (NULL != ch->dest)
533 GML_send_channel_destroy (ch->dest, ch->lid_dest); 533 GML_send_channel_destroy (ch->dest, ch->lid_dest);
534 else if (0 == ch->lid_dest && GNUNET_NO == local_only) 534 else if (0 == ch->lid_dest.channel_of_client && GNUNET_NO == local_only)
535 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES, NULL); 535 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_YES, NULL);
536} 536}
537 537
@@ -552,7 +552,10 @@ send_client_create (struct CadetChannel *ch)
552 opt = 0; 552 opt = 0;
553 opt |= GNUNET_YES == ch->reliable ? GNUNET_CADET_OPTION_RELIABLE : 0; 553 opt |= GNUNET_YES == ch->reliable ? GNUNET_CADET_OPTION_RELIABLE : 0;
554 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_CADET_OPTION_NOBUFFER : 0; 554 opt |= GNUNET_YES == ch->nobuffer ? GNUNET_CADET_OPTION_NOBUFFER : 0;
555 GML_send_channel_create (ch->dest, ch->lid_dest, &ch->port, opt, 555 GML_send_channel_create (ch->dest,
556 ch->lid_dest,
557 &ch->port,
558 opt,
556 GCT_get_destination (ch->t)); 559 GCT_get_destination (ch->t));
557 560
558} 561}
@@ -570,7 +573,7 @@ send_client_create (struct CadetChannel *ch)
570 */ 573 */
571static void 574static void
572send_client_data (struct CadetChannel *ch, 575send_client_data (struct CadetChannel *ch,
573 const struct GNUNET_CADET_Data *msg, 576 const struct GNUNET_CADET_ChannelAppDataMessage *msg,
574 int fwd) 577 int fwd)
575{ 578{
576 if (fwd) 579 if (fwd)
@@ -628,7 +631,7 @@ send_client_buffered_data (struct CadetChannel *ch,
628 { 631 {
629 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable) 632 if (copy->mid == rel->mid_recv || GNUNET_NO == ch->reliable)
630 { 633 {
631 struct GNUNET_CADET_Data *msg = (struct GNUNET_CADET_Data *) &copy[1]; 634 struct GNUNET_CADET_ChannelAppDataMessage *msg = (struct GNUNET_CADET_ChannelAppDataMessage *) &copy[1];
632 635
633 LOG (GNUNET_ERROR_TYPE_DEBUG, " have %u! now expecting %u\n", 636 LOG (GNUNET_ERROR_TYPE_DEBUG, " have %u! now expecting %u\n",
634 copy->mid, rel->mid_recv + 1); 637 copy->mid, rel->mid_recv + 1);
@@ -728,7 +731,7 @@ channel_retransmit_message (void *cls)
728 struct CadetChannelReliability *rel = cls; 731 struct CadetChannelReliability *rel = cls;
729 struct CadetReliableMessage *copy; 732 struct CadetReliableMessage *copy;
730 struct CadetChannel *ch; 733 struct CadetChannel *ch;
731 struct GNUNET_CADET_Data *payload; 734 struct GNUNET_CADET_ChannelAppDataMessage *payload;
732 int fwd; 735 int fwd;
733 736
734 rel->retry_task = NULL; 737 rel->retry_task = NULL;
@@ -740,7 +743,7 @@ channel_retransmit_message (void *cls)
740 return; 743 return;
741 } 744 }
742 745
743 payload = (struct GNUNET_CADET_Data *) &copy[1]; 746 payload = (struct GNUNET_CADET_ChannelAppDataMessage *) &copy[1];
744 fwd = (rel == ch->root_rel); 747 fwd = (rel == ch->root_rel);
745 748
746 /* Message not found in the queue that we are going to use. */ 749 /* Message not found in the queue that we are going to use. */
@@ -805,7 +808,7 @@ ch_message_sent (void *cls,
805 808
806 switch (chq->type) 809 switch (chq->type)
807 { 810 {
808 case GNUNET_MESSAGE_TYPE_CADET_DATA: 811 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA:
809 LOG (GNUNET_ERROR_TYPE_DEBUG, "data MID %u sent\n", copy->mid); 812 LOG (GNUNET_ERROR_TYPE_DEBUG, "data MID %u sent\n", copy->mid);
810 GNUNET_assert (chq == copy->chq); 813 GNUNET_assert (chq == copy->chq);
811 copy->timestamp = GNUNET_TIME_absolute_get (); 814 copy->timestamp = GNUNET_TIME_absolute_get ();
@@ -841,16 +844,16 @@ ch_message_sent (void *cls,
841 break; 844 break;
842 845
843 846
844 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK: 847 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK:
845 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE: 848 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
846 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK: 849 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK:
847 LOG (GNUNET_ERROR_TYPE_DEBUG, "sent %s\n", GC_m2s (chq->type)); 850 LOG (GNUNET_ERROR_TYPE_DEBUG, "sent %s\n", GC_m2s (chq->type));
848 rel = chq->rel; 851 rel = chq->rel;
849 GNUNET_assert (rel->uniq == chq); 852 GNUNET_assert (rel->uniq == chq);
850 rel->uniq = NULL; 853 rel->uniq = NULL;
851 854
852 if (CADET_CHANNEL_READY != rel->ch->state 855 if (CADET_CHANNEL_READY != rel->ch->state
853 && GNUNET_MESSAGE_TYPE_CADET_DATA_ACK != type 856 && GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK != type
854 && GNUNET_NO == rel->ch->destroy) 857 && GNUNET_NO == rel->ch->destroy)
855 { 858 {
856 GNUNET_assert (NULL == rel->retry_task); 859 GNUNET_assert (NULL == rel->retry_task);
@@ -879,11 +882,11 @@ ch_message_sent (void *cls,
879static void 882static void
880send_create (struct CadetChannel *ch) 883send_create (struct CadetChannel *ch)
881{ 884{
882 struct GNUNET_CADET_ChannelCreate msgcc; 885 struct GNUNET_CADET_ChannelOpenMessage msgcc;
883 886
884 msgcc.header.size = htons (sizeof (msgcc)); 887 msgcc.header.size = htons (sizeof (msgcc));
885 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE); 888 msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN);
886 msgcc.chid = htonl (ch->gid); 889 msgcc.chid = ch->gid;
887 msgcc.port = ch->port; 890 msgcc.port = ch->port;
888 msgcc.opt = htonl (channel_get_options (ch)); 891 msgcc.opt = htonl (channel_get_options (ch));
889 892
@@ -900,14 +903,15 @@ send_create (struct CadetChannel *ch)
900static void 903static void
901send_ack (struct CadetChannel *ch, int fwd) 904send_ack (struct CadetChannel *ch, int fwd)
902{ 905{
903 struct GNUNET_CADET_ChannelManage msg; 906 struct GNUNET_CADET_ChannelManageMessage msg;
904 907
905 msg.header.size = htons (sizeof (msg)); 908 msg.header.size = htons (sizeof (msg));
906 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK); 909 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK);
907 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending channel %s ack for channel %s\n", 910 LOG (GNUNET_ERROR_TYPE_DEBUG,
911 " sending channel %s ack for channel %s\n",
908 GC_f2s (fwd), GCCH_2s (ch)); 912 GC_f2s (fwd), GCCH_2s (ch));
909 913
910 msg.chid = htonl (ch->gid); 914 msg.chid =ch->gid;
911 GCCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL); 915 GCCH_send_prebuilt_message (&msg.header, ch, !fwd, NULL);
912} 916}
913 917
@@ -925,8 +929,9 @@ fire_and_forget (const struct GNUNET_MessageHeader *msg,
925 struct CadetChannel *ch, 929 struct CadetChannel *ch,
926 int force) 930 int force)
927{ 931{
928 GNUNET_break (NULL == GCT_send_prebuilt_message (msg, ch->t, NULL, 932 GNUNET_break (NULL ==
929 force, NULL, NULL)); 933 GCT_send_prebuilt_message (msg, ch->t, NULL,
934 force, NULL, NULL));
930} 935}
931 936
932 937
@@ -938,15 +943,15 @@ fire_and_forget (const struct GNUNET_MessageHeader *msg,
938static void 943static void
939send_nack (struct CadetChannel *ch) 944send_nack (struct CadetChannel *ch)
940{ 945{
941 struct GNUNET_CADET_ChannelManage msg; 946 struct GNUNET_CADET_ChannelManageMessage msg;
942 947
943 msg.header.size = htons (sizeof (msg)); 948 msg.header.size = htons (sizeof (msg));
944 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK); 949 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED);
945 LOG (GNUNET_ERROR_TYPE_DEBUG, 950 LOG (GNUNET_ERROR_TYPE_DEBUG,
946 " sending channel NACK for channel %s\n", 951 " sending channel NACK for channel %s\n",
947 GCCH_2s (ch)); 952 GCCH_2s (ch));
948 953
949 msg.chid = htonl (ch->gid); 954 msg.chid = ch->gid;
950 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL); 955 GCCH_send_prebuilt_message (&msg.header, ch, GNUNET_NO, NULL);
951} 956}
952 957
@@ -1019,7 +1024,7 @@ channel_rel_free_all (struct CadetChannelReliability *rel)
1019 */ 1024 */
1020static unsigned int 1025static unsigned int
1021channel_rel_free_sent (struct CadetChannelReliability *rel, 1026channel_rel_free_sent (struct CadetChannelReliability *rel,
1022 const struct GNUNET_CADET_DataACK *msg) 1027 const struct GNUNET_CADET_ChannelDataAckMessage *msg)
1023{ 1028{
1024 struct CadetReliableMessage *copy; 1029 struct CadetReliableMessage *copy;
1025 struct CadetReliableMessage *next; 1030 struct CadetReliableMessage *next;
@@ -1252,7 +1257,7 @@ channel_save_copy (struct CadetChannel *ch,
1252static struct CadetChannel * 1257static struct CadetChannel *
1253channel_new (struct CadetTunnel *t, 1258channel_new (struct CadetTunnel *t,
1254 struct CadetClient *owner, 1259 struct CadetClient *owner,
1255 CADET_ChannelNumber lid_root) 1260 struct GNUNET_CADET_ClientChannelNumber lid_root)
1256{ 1261{
1257 struct CadetChannel *ch; 1262 struct CadetChannel *ch;
1258 1263
@@ -1295,35 +1300,35 @@ handle_loopback (struct CadetChannel *ch,
1295 1300
1296 switch (type) 1301 switch (type)
1297 { 1302 {
1298 case GNUNET_MESSAGE_TYPE_CADET_DATA: 1303 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA:
1299 /* Don't send hop ACK, wait for client to ACK */ 1304 /* Don't send hop ACK, wait for client to ACK */
1300 LOG (GNUNET_ERROR_TYPE_DEBUG, "SEND loopback %u (%u)\n", 1305 LOG (GNUNET_ERROR_TYPE_DEBUG, "SEND loopback %u (%u)\n",
1301 ntohl (((struct GNUNET_CADET_Data *) msgh)->mid), ntohs (msgh->size)); 1306 ntohl (((struct GNUNET_CADET_ChannelAppDataMessage *) msgh)->mid), ntohs (msgh->size));
1302 GCCH_handle_data (ch, (struct GNUNET_CADET_Data *) msgh, fwd); 1307 GCCH_handle_data (ch, (struct GNUNET_CADET_ChannelAppDataMessage *) msgh, fwd);
1303 break; 1308 break;
1304 1309
1305 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK: 1310 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK:
1306 GCCH_handle_data_ack (ch, (struct GNUNET_CADET_DataACK *) msgh, fwd); 1311 GCCH_handle_data_ack (ch, (struct GNUNET_CADET_ChannelDataAckMessage *) msgh, fwd);
1307 break; 1312 break;
1308 1313
1309 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE: 1314 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
1310 GCCH_handle_create (ch->t, 1315 GCCH_handle_create (ch->t,
1311 (struct GNUNET_CADET_ChannelCreate *) msgh); 1316 (struct GNUNET_CADET_ChannelOpenMessage *) msgh);
1312 break; 1317 break;
1313 1318
1314 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK: 1319 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK:
1315 GCCH_handle_ack (ch, 1320 GCCH_handle_ack (ch,
1316 (struct GNUNET_CADET_ChannelManage *) msgh, 1321 (struct GNUNET_CADET_ChannelManageMessage *) msgh,
1317 fwd); 1322 fwd);
1318 break; 1323 break;
1319 1324
1320 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK: 1325 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED:
1321 GCCH_handle_nack (ch); 1326 GCCH_handle_nack (ch);
1322 break; 1327 break;
1323 1328
1324 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY: 1329 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1325 GCCH_handle_destroy (ch, 1330 GCCH_handle_destroy (ch,
1326 (struct GNUNET_CADET_ChannelManage *) msgh, 1331 (struct GNUNET_CADET_ChannelManageMessage *) msgh,
1327 fwd); 1332 fwd);
1328 break; 1333 break;
1329 1334
@@ -1393,7 +1398,7 @@ GCCH_destroy (struct CadetChannel *ch)
1393 * 1398 *
1394 * @return ID used to identify the channel with the remote peer. 1399 * @return ID used to identify the channel with the remote peer.
1395 */ 1400 */
1396CADET_ChannelNumber 1401struct GNUNET_CADET_ChannelTunnelNumber
1397GCCH_get_id (const struct CadetChannel *ch) 1402GCCH_get_id (const struct CadetChannel *ch)
1398{ 1403{
1399 return ch->gid; 1404 return ch->gid;
@@ -1518,7 +1523,7 @@ GCCH_is_terminal (struct CadetChannel *ch, int fwd)
1518void 1523void
1519GCCH_send_data_ack (struct CadetChannel *ch, int fwd) 1524GCCH_send_data_ack (struct CadetChannel *ch, int fwd)
1520{ 1525{
1521 struct GNUNET_CADET_DataACK msg; 1526 struct GNUNET_CADET_ChannelDataAckMessage msg;
1522 struct CadetChannelReliability *rel; 1527 struct CadetChannelReliability *rel;
1523 struct CadetReliableMessage *copy; 1528 struct CadetReliableMessage *copy;
1524 unsigned int delta; 1529 unsigned int delta;
@@ -1531,15 +1536,15 @@ GCCH_send_data_ack (struct CadetChannel *ch, int fwd)
1531 rel = fwd ? ch->dest_rel : ch->root_rel; 1536 rel = fwd ? ch->dest_rel : ch->root_rel;
1532 ack = rel->mid_recv - 1; 1537 ack = rel->mid_recv - 1;
1533 1538
1534 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_DATA_ACK); 1539 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK);
1535 msg.header.size = htons (sizeof (msg)); 1540 msg.header.size = htons (sizeof (msg));
1536 msg.chid = htonl (ch->gid); 1541 msg.chid = ch->gid;
1537 msg.mid = htonl (ack); 1542 msg.mid = htonl (ack);
1538 1543
1539 msg.futures = 0LL; 1544 msg.futures = 0LL;
1540 for (copy = rel->head_recv; NULL != copy; copy = copy->next) 1545 for (copy = rel->head_recv; NULL != copy; copy = copy->next)
1541 { 1546 {
1542 if (copy->type != GNUNET_MESSAGE_TYPE_CADET_DATA) 1547 if (copy->type != GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA)
1543 { 1548 {
1544 LOG (GNUNET_ERROR_TYPE_DEBUG, " Type %s, expected DATA\n", 1549 LOG (GNUNET_ERROR_TYPE_DEBUG, " Type %s, expected DATA\n",
1545 GC_m2s (copy->type)); 1550 GC_m2s (copy->type));
@@ -1657,7 +1662,7 @@ GCCH_debug (struct CadetChannel *ch, enum GNUNET_ErrorType level)
1657 { 1662 {
1658 LOG2 (level, "CHN cli %s\n", GML_2s (ch->root)); 1663 LOG2 (level, "CHN cli %s\n", GML_2s (ch->root));
1659 LOG2 (level, "CHN ready %s\n", ch->root_rel->client_ready ? "YES" : "NO"); 1664 LOG2 (level, "CHN ready %s\n", ch->root_rel->client_ready ? "YES" : "NO");
1660 LOG2 (level, "CHN id %X\n", ch->lid_root); 1665 LOG2 (level, "CHN id %X\n", ch->lid_root.channel_of_client);
1661 LOG2 (level, "CHN recv %d\n", ch->root_rel->n_recv); 1666 LOG2 (level, "CHN recv %d\n", ch->root_rel->n_recv);
1662 LOG2 (level, "CHN MID r: %d, s: %d\n", 1667 LOG2 (level, "CHN MID r: %d, s: %d\n",
1663 ch->root_rel->mid_recv, ch->root_rel->mid_send); 1668 ch->root_rel->mid_recv, ch->root_rel->mid_send);
@@ -1733,17 +1738,18 @@ GCCH_handle_local_ack (struct CadetChannel *ch, int fwd)
1733 * @param message Data message. 1738 * @param message Data message.
1734 * @param size Size of data. 1739 * @param size Size of data.
1735 * 1740 *
1736 * @return GNUNET_OK if everything goes well, GNUNET_SYSERR in case of en error. 1741 * @return #GNUNET_OK if everything goes well, #GNUNET_SYSERR in case of en error.
1737 */ 1742 */
1738int 1743int
1739GCCH_handle_local_data (struct CadetChannel *ch, 1744GCCH_handle_local_data (struct CadetChannel *ch,
1740 struct CadetClient *c, int fwd, 1745 struct CadetClient *c,
1746 int fwd,
1741 const struct GNUNET_MessageHeader *message, 1747 const struct GNUNET_MessageHeader *message,
1742 size_t size) 1748 size_t size)
1743{ 1749{
1744 struct CadetChannelReliability *rel; 1750 struct CadetChannelReliability *rel;
1745 struct GNUNET_CADET_Data *payload; 1751 struct GNUNET_CADET_ChannelAppDataMessage *payload;
1746 uint16_t p2p_size = sizeof(struct GNUNET_CADET_Data) + size; 1752 uint16_t p2p_size = sizeof(struct GNUNET_CADET_ChannelAppDataMessage) + size;
1747 unsigned char cbuf[p2p_size]; 1753 unsigned char cbuf[p2p_size];
1748 unsigned char buffer; 1754 unsigned char buffer;
1749 1755
@@ -1769,13 +1775,13 @@ GCCH_handle_local_data (struct CadetChannel *ch,
1769 rel->client_allowed = GNUNET_NO; 1775 rel->client_allowed = GNUNET_NO;
1770 1776
1771 /* Ok, everything is correct, send the message. */ 1777 /* Ok, everything is correct, send the message. */
1772 payload = (struct GNUNET_CADET_Data *) cbuf; 1778 payload = (struct GNUNET_CADET_ChannelAppDataMessage *) cbuf;
1773 payload->mid = htonl (rel->mid_send); 1779 payload->mid = htonl (rel->mid_send);
1774 rel->mid_send++; 1780 rel->mid_send++;
1775 GNUNET_memcpy (&payload[1], message, size); 1781 GNUNET_memcpy (&payload[1], message, size);
1776 payload->header.size = htons (p2p_size); 1782 payload->header.size = htons (p2p_size);
1777 payload->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_DATA); 1783 payload->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA);
1778 payload->chid = htonl (ch->gid); 1784 payload->chid = ch->gid;
1779 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n"); 1785 LOG (GNUNET_ERROR_TYPE_DEBUG, " sending on channel...\n");
1780 GCCH_send_prebuilt_message (&payload->header, ch, fwd, NULL); 1786 GCCH_send_prebuilt_message (&payload->header, ch, fwd, NULL);
1781 1787
@@ -1840,16 +1846,16 @@ GCCH_handle_local_destroy (struct CadetChannel *ch,
1840 */ 1846 */
1841int 1847int
1842GCCH_handle_local_create (struct CadetClient *c, 1848GCCH_handle_local_create (struct CadetClient *c,
1843 struct GNUNET_CADET_ChannelCreateMessage *msg) 1849 struct GNUNET_CADET_ChannelOpenMessageMessage *msg)
1844{ 1850{
1845 struct CadetChannel *ch; 1851 struct CadetChannel *ch;
1846 struct CadetTunnel *t; 1852 struct CadetTunnel *t;
1847 struct CadetPeer *peer; 1853 struct CadetPeer *peer;
1848 CADET_ChannelNumber chid; 1854 struct GNUNET_CADET_ClientChannelNumber chid;
1849 1855
1850 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n", 1856 LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n",
1851 GNUNET_i2s (&msg->peer), GNUNET_h2s (&msg->port)); 1857 GNUNET_i2s (&msg->peer), GNUNET_h2s (&msg->port));
1852 chid = ntohl (msg->channel_id); 1858 chid = msg->channel_id;
1853 1859
1854 /* Sanity check for duplicate channel IDs */ 1860 /* Sanity check for duplicate channel IDs */
1855 if (NULL != GML_channel_get (c, chid)) 1861 if (NULL != GML_channel_get (c, chid))
@@ -1908,7 +1914,7 @@ GCCH_handle_local_create (struct CadetClient *c,
1908 */ 1914 */
1909void 1915void
1910GCCH_handle_data (struct CadetChannel *ch, 1916GCCH_handle_data (struct CadetChannel *ch,
1911 const struct GNUNET_CADET_Data *msg, 1917 const struct GNUNET_CADET_ChannelAppDataMessage *msg,
1912 int fwd) 1918 int fwd)
1913{ 1919{
1914 struct CadetChannelReliability *rel; 1920 struct CadetChannelReliability *rel;
@@ -1966,24 +1972,26 @@ GCCH_handle_data (struct CadetChannel *ch,
1966 1972
1967 mid = ntohl (msg->mid); 1973 mid = ntohl (msg->mid);
1968 LOG (GNUNET_ERROR_TYPE_INFO, "<== %s (%s %4u) on chan %s (%p) %s [%5u]\n", 1974 LOG (GNUNET_ERROR_TYPE_INFO, "<== %s (%s %4u) on chan %s (%p) %s [%5u]\n",
1969 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_DATA), GC_m2s (payload_type), mid, 1975 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA), GC_m2s (payload_type), mid,
1970 GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size)); 1976 GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
1971 1977
1972 if (GNUNET_NO == ch->reliable || 1978 if ( (GNUNET_NO == ch->reliable) ||
1973 ( !GC_is_pid_bigger (rel->mid_recv, mid) && 1979 ( (! GC_is_pid_bigger (rel->mid_recv, mid)) &&
1974 GC_is_pid_bigger (rel->mid_recv + 64, mid) ) ) 1980 GC_is_pid_bigger (rel->mid_recv + 64, mid) ) )
1975 { 1981 {
1976 if (GNUNET_YES == ch->reliable) 1982 if (GNUNET_YES == ch->reliable)
1977 { 1983 {
1978 /* Is this the exact next expected messasge? */ 1984 /* Is this the exact next expected messasge? */
1979 if (mid == rel->mid_recv) 1985 if (mid == rel->mid_recv)
1980 { 1986 {
1981 LOG (GNUNET_ERROR_TYPE_DEBUG, "as expected, sending to client\n"); 1987 LOG (GNUNET_ERROR_TYPE_DEBUG,
1988 "as expected, sending to client\n");
1982 send_client_data (ch, msg, fwd); 1989 send_client_data (ch, msg, fwd);
1983 } 1990 }
1984 else 1991 else
1985 { 1992 {
1986 LOG (GNUNET_ERROR_TYPE_DEBUG, "save for later\n"); 1993 LOG (GNUNET_ERROR_TYPE_DEBUG,
1994 "save for later\n");
1987 add_buffered_data (msg, rel); 1995 add_buffered_data (msg, rel);
1988 } 1996 }
1989 } 1997 }
@@ -2001,7 +2009,7 @@ GCCH_handle_data (struct CadetChannel *ch,
2001 if (GC_is_pid_bigger (rel->mid_recv, mid)) 2009 if (GC_is_pid_bigger (rel->mid_recv, mid))
2002 { 2010 {
2003 GNUNET_break_op (0); 2011 GNUNET_break_op (0);
2004 LOG (GNUNET_ERROR_TYPE_INFO, 2012 LOG (GNUNET_ERROR_TYPE_WARNING,
2005 "MID %u on channel %s not expected (window: %u - %u). Dropping!\n", 2013 "MID %u on channel %s not expected (window: %u - %u). Dropping!\n",
2006 mid, GCCH_2s (ch), rel->mid_recv, rel->mid_recv + 63); 2014 mid, GCCH_2s (ch), rel->mid_recv, rel->mid_recv + 63);
2007 } 2015 }
@@ -2036,7 +2044,7 @@ GCCH_handle_data (struct CadetChannel *ch,
2036 */ 2044 */
2037void 2045void
2038GCCH_handle_data_ack (struct CadetChannel *ch, 2046GCCH_handle_data_ack (struct CadetChannel *ch,
2039 const struct GNUNET_CADET_DataACK *msg, 2047 const struct GNUNET_CADET_ChannelDataAckMessage *msg,
2040 int fwd) 2048 int fwd)
2041{ 2049{
2042 struct CadetChannelReliability *rel; 2050 struct CadetChannelReliability *rel;
@@ -2061,7 +2069,7 @@ GCCH_handle_data_ack (struct CadetChannel *ch,
2061 ack = ntohl (msg->mid); 2069 ack = ntohl (msg->mid);
2062 LOG (GNUNET_ERROR_TYPE_INFO, 2070 LOG (GNUNET_ERROR_TYPE_INFO,
2063 "<== %s (0x%010lX %4u) on chan %s (%p) %s [%5u]\n", 2071 "<== %s (0x%010lX %4u) on chan %s (%p) %s [%5u]\n",
2064 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_DATA_ACK), msg->futures, ack, 2072 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK), msg->futures, ack,
2065 GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size)); 2073 GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
2066 2074
2067 if (GNUNET_YES == fwd) 2075 if (GNUNET_YES == fwd)
@@ -2143,22 +2151,23 @@ GCCH_handle_data_ack (struct CadetChannel *ch,
2143 */ 2151 */
2144struct CadetChannel * 2152struct CadetChannel *
2145GCCH_handle_create (struct CadetTunnel *t, 2153GCCH_handle_create (struct CadetTunnel *t,
2146 const struct GNUNET_CADET_ChannelCreate *msg) 2154 const struct GNUNET_CADET_ChannelOpenMessage *msg)
2147{ 2155{
2148 CADET_ChannelNumber chid; 2156 struct GNUNET_CADET_ClientChannelNumber chid;
2157 struct GNUNET_CADET_ChannelTunnelNumber gid;
2149 struct CadetChannel *ch; 2158 struct CadetChannel *ch;
2150 struct CadetClient *c; 2159 struct CadetClient *c;
2151 int new_channel; 2160 int new_channel;
2152 const struct GNUNET_HashCode *port; 2161 const struct GNUNET_HashCode *port;
2153 2162
2154 chid = ntohl (msg->chid); 2163 gid = msg->chid;
2155 2164 ch = GCT_get_channel (t, gid);
2156 ch = GCT_get_channel (t, chid);
2157 if (NULL == ch) 2165 if (NULL == ch)
2158 { 2166 {
2159 /* Create channel */ 2167 /* Create channel */
2160 ch = channel_new (t, NULL, 0); 2168 chid.channel_of_client = htonl (0);
2161 ch->gid = chid; 2169 ch = channel_new (t, NULL, chid);
2170 ch->gid = gid;
2162 channel_set_options (ch, ntohl (msg->opt)); 2171 channel_set_options (ch, ntohl (msg->opt));
2163 new_channel = GNUNET_YES; 2172 new_channel = GNUNET_YES;
2164 } 2173 }
@@ -2170,7 +2179,7 @@ GCCH_handle_create (struct CadetTunnel *t,
2170 2179
2171 LOG (GNUNET_ERROR_TYPE_INFO, 2180 LOG (GNUNET_ERROR_TYPE_INFO,
2172 "<== %s ( 0x%08X %4u) on chan %s (%p) %s [%5u]\n", 2181 "<== %s ( 0x%08X %4u) on chan %s (%p) %s [%5u]\n",
2173 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE), chid, port, 2182 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN), chid, port,
2174 GCCH_2s (ch), ch, GC_f2s (GNUNET_YES), ntohs (msg->header.size)); 2183 GCCH_2s (ch), ch, GC_f2s (GNUNET_YES), ntohs (msg->header.size));
2175 2184
2176 if (GNUNET_YES == new_channel || GCT_is_loopback (t)) 2185 if (GNUNET_YES == new_channel || GCT_is_loopback (t))
@@ -2243,7 +2252,7 @@ GCCH_handle_nack (struct CadetChannel *ch)
2243{ 2252{
2244 LOG (GNUNET_ERROR_TYPE_INFO, 2253 LOG (GNUNET_ERROR_TYPE_INFO,
2245 "<== %s ( 0x%08X %4u) on chan %s (%p) %s [%5u]\n", 2254 "<== %s ( 0x%08X %4u) on chan %s (%p) %s [%5u]\n",
2246 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK), ch->gid, 0, 2255 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED), ch->gid, 0,
2247 GCCH_2s (ch), ch, "---", 0); 2256 GCCH_2s (ch), ch, "---", 0);
2248 2257
2249 send_client_nack (ch); 2258 send_client_nack (ch);
@@ -2263,12 +2272,12 @@ GCCH_handle_nack (struct CadetChannel *ch)
2263 */ 2272 */
2264void 2273void
2265GCCH_handle_ack (struct CadetChannel *ch, 2274GCCH_handle_ack (struct CadetChannel *ch,
2266 const struct GNUNET_CADET_ChannelManage *msg, 2275 const struct GNUNET_CADET_ChannelManageMessage *msg,
2267 int fwd) 2276 int fwd)
2268{ 2277{
2269 LOG (GNUNET_ERROR_TYPE_INFO, 2278 LOG (GNUNET_ERROR_TYPE_INFO,
2270 "<== %s ( 0x%08X %4u) on chan %s (%p) %s [%5u]\n", 2279 "<== %s ( 0x%08X %4u) on chan %s (%p) %s [%5u]\n",
2271 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK), ch->gid, 0, 2280 GC_m2s (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK), ch->gid, 0,
2272 GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size)); 2281 GCCH_2s (ch), ch, GC_f2s (fwd), ntohs (msg->header.size));
2273 2282
2274 /* If this is a remote (non-loopback) channel, find 'fwd'. */ 2283 /* If this is a remote (non-loopback) channel, find 'fwd'. */
@@ -2299,7 +2308,7 @@ GCCH_handle_ack (struct CadetChannel *ch,
2299 */ 2308 */
2300void 2309void
2301GCCH_handle_destroy (struct CadetChannel *ch, 2310GCCH_handle_destroy (struct CadetChannel *ch,
2302 const struct GNUNET_CADET_ChannelManage *msg, 2311 const struct GNUNET_CADET_ChannelManageMessage *msg,
2303 int fwd) 2312 int fwd)
2304{ 2313{
2305 struct CadetChannelReliability *rel; 2314 struct CadetChannelReliability *rel;
@@ -2374,13 +2383,13 @@ GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2374 data_id = 0; 2383 data_id = 0;
2375 switch (type) 2384 switch (type)
2376 { 2385 {
2377 case GNUNET_MESSAGE_TYPE_CADET_DATA: 2386 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA:
2378 { 2387 {
2379 struct GNUNET_CADET_Data *data_msg; 2388 struct GNUNET_CADET_ChannelAppDataMessage *data_msg;
2380 struct GNUNET_MessageHeader *payload_msg; 2389 struct GNUNET_MessageHeader *payload_msg;
2381 uint16_t payload_type; 2390 uint16_t payload_type;
2382 2391
2383 data_msg = (struct GNUNET_CADET_Data *) message; 2392 data_msg = (struct GNUNET_CADET_ChannelAppDataMessage *) message;
2384 data_id = ntohl (data_msg->mid); 2393 data_id = ntohl (data_msg->mid);
2385 payload_msg = (struct GNUNET_MessageHeader *) &data_msg[1]; 2394 payload_msg = (struct GNUNET_MessageHeader *) &data_msg[1];
2386 payload_type = ntohs (payload_msg->type); 2395 payload_type = ntohs (payload_msg->type);
@@ -2388,28 +2397,29 @@ GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2388 info[31] = '\0'; 2397 info[31] = '\0';
2389 break; 2398 break;
2390 } 2399 }
2391 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK: 2400 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK:
2392 { 2401 {
2393 struct GNUNET_CADET_DataACK *ack_msg; 2402 struct GNUNET_CADET_ChannelDataAckMessage *ack_msg;
2394 ack_msg = (struct GNUNET_CADET_DataACK *) message; 2403 ack_msg = (struct GNUNET_CADET_ChannelDataAckMessage *) message;
2395 data_id = ntohl (ack_msg->mid); 2404 data_id = ntohl (ack_msg->mid);
2396 SPRINTF (info, "0x%010lX", ack_msg->futures); 2405 SPRINTF (info, "0x%010lX",
2406 (unsigned long int) ack_msg->futures);
2397 break; 2407 break;
2398 } 2408 }
2399 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE: 2409 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
2400 { 2410 {
2401 struct GNUNET_CADET_ChannelCreate *cc_msg; 2411 struct GNUNET_CADET_ChannelOpenMessage *cc_msg;
2402 cc_msg = (struct GNUNET_CADET_ChannelCreate *) message; 2412 cc_msg = (struct GNUNET_CADET_ChannelOpenMessage *) message;
2403 SPRINTF (info, " 0x%08X", ntohl (cc_msg->chid)); 2413 SPRINTF (info, " 0x%08X", ntohl (cc_msg->chid.cn));
2404 break; 2414 break;
2405 } 2415 }
2406 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK: 2416 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK:
2407 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK: 2417 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED:
2408 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY: 2418 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
2409 { 2419 {
2410 struct GNUNET_CADET_ChannelManage *m_msg; 2420 struct GNUNET_CADET_ChannelManageMessage *m_msg;
2411 m_msg = (struct GNUNET_CADET_ChannelManage *) message; 2421 m_msg = (struct GNUNET_CADET_ChannelManageMessage *) message;
2412 SPRINTF (info, " 0x%08X", ntohl (m_msg->chid)); 2422 SPRINTF (info, " 0x%08X", ntohl (m_msg->chid.cn));
2413 break; 2423 break;
2414 } 2424 }
2415 default: 2425 default:
@@ -2428,7 +2438,7 @@ GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2428 2438
2429 switch (type) 2439 switch (type)
2430 { 2440 {
2431 case GNUNET_MESSAGE_TYPE_CADET_DATA: 2441 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA:
2432 if (GNUNET_YES == ch->reliable) 2442 if (GNUNET_YES == ch->reliable)
2433 { 2443 {
2434 chq = GNUNET_new (struct CadetChannelQueue); 2444 chq = GNUNET_new (struct CadetChannelQueue);
@@ -2474,9 +2484,9 @@ GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2474 break; 2484 break;
2475 2485
2476 2486
2477 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK: 2487 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK:
2478 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE: 2488 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
2479 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK: 2489 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK:
2480 chq = GNUNET_new (struct CadetChannelQueue); 2490 chq = GNUNET_new (struct CadetChannelQueue);
2481 chq->type = type; 2491 chq->type = type;
2482 chq->rel = fwd ? ch->root_rel : ch->dest_rel; 2492 chq->rel = fwd ? ch->root_rel : ch->dest_rel;
@@ -2495,22 +2505,23 @@ GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2495 } 2505 }
2496 } 2506 }
2497 2507
2508 chq->rel->uniq = chq;
2498 chq->tq = GCT_send_prebuilt_message (message, ch->t, NULL, GNUNET_YES, 2509 chq->tq = GCT_send_prebuilt_message (message, ch->t, NULL, GNUNET_YES,
2499 &ch_message_sent, chq); 2510 &ch_message_sent, chq);
2500 if (NULL == chq->tq) 2511 if (NULL == chq->tq)
2501 { 2512 {
2502 GNUNET_break (0); 2513 GNUNET_break (0);
2514 chq->rel->uniq = NULL;
2503 GCT_debug (ch->t, GNUNET_ERROR_TYPE_ERROR); 2515 GCT_debug (ch->t, GNUNET_ERROR_TYPE_ERROR);
2504 GNUNET_free (chq); 2516 GNUNET_free (chq);
2505 chq = NULL; 2517 chq = NULL;
2506 return; 2518 return;
2507 } 2519 }
2508 chq->rel->uniq = chq;
2509 break; 2520 break;
2510 2521
2511 2522
2512 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY: 2523 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
2513 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK: 2524 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED:
2514 fire_and_forget (message, ch, GNUNET_YES); 2525 fire_and_forget (message, ch, GNUNET_YES);
2515 break; 2526 break;
2516 2527
@@ -2538,9 +2549,13 @@ GCCH_2s (const struct CadetChannel *ch)
2538 if (NULL == ch) 2549 if (NULL == ch)
2539 return "(NULL Channel)"; 2550 return "(NULL Channel)";
2540 2551
2541 SPRINTF (buf, "%s:%s gid:%X (%X / %X)", 2552 SPRINTF (buf,
2542 GCT_2s (ch->t), GNUNET_h2s (&ch->port), 2553 "%s:%s gid:%X (%X / %X)",
2543 ch->gid, ch->lid_root, ch->lid_dest); 2554 GCT_2s (ch->t),
2555 GNUNET_h2s (&ch->port),
2556 ntohl (ch->gid.cn),
2557 ntohl (ch->lid_root.channel_of_client),
2558 ntohl (ch->lid_dest.channel_of_client));
2544 2559
2545 return buf; 2560 return buf;
2546} 2561}
diff --git a/src/cadet/gnunet-service-cadet_channel.h b/src/cadet/gnunet-service-cadet_channel.h
index eeea02712..1eeebf34b 100644
--- a/src/cadet/gnunet-service-cadet_channel.h
+++ b/src/cadet/gnunet-service-cadet_channel.h
@@ -69,7 +69,7 @@ GCCH_destroy (struct CadetChannel *ch);
69 * 69 *
70 * @return ID used to identify the channel with the remote peer. 70 * @return ID used to identify the channel with the remote peer.
71 */ 71 */
72CADET_ChannelNumber 72struct GNUNET_CADET_ChannelTunnelNumber
73GCCH_get_id (const struct CadetChannel *ch); 73GCCH_get_id (const struct CadetChannel *ch);
74 74
75/** 75/**
@@ -224,7 +224,7 @@ GCCH_handle_local_destroy (struct CadetChannel *ch,
224 */ 224 */
225int 225int
226GCCH_handle_local_create (struct CadetClient *c, 226GCCH_handle_local_create (struct CadetClient *c,
227 struct GNUNET_CADET_ChannelCreateMessage *msg); 227 struct GNUNET_CADET_ChannelOpenMessageMessage *msg);
228 228
229/** 229/**
230 * Handler for cadet network payload traffic. 230 * Handler for cadet network payload traffic.
@@ -238,7 +238,7 @@ GCCH_handle_local_create (struct CadetClient *c,
238 */ 238 */
239void 239void
240GCCH_handle_data (struct CadetChannel *ch, 240GCCH_handle_data (struct CadetChannel *ch,
241 const struct GNUNET_CADET_Data *msg, 241 const struct GNUNET_CADET_ChannelAppDataMessage *msg,
242 int fwd); 242 int fwd);
243 243
244 244
@@ -254,7 +254,7 @@ GCCH_handle_data (struct CadetChannel *ch,
254 */ 254 */
255void 255void
256GCCH_handle_data_ack (struct CadetChannel *ch, 256GCCH_handle_data_ack (struct CadetChannel *ch,
257 const struct GNUNET_CADET_DataACK *msg, 257 const struct GNUNET_CADET_ChannelDataAckMessage *msg,
258 int fwd); 258 int fwd);
259 259
260 260
@@ -268,7 +268,7 @@ GCCH_handle_data_ack (struct CadetChannel *ch,
268 */ 268 */
269struct CadetChannel * 269struct CadetChannel *
270GCCH_handle_create (struct CadetTunnel *t, 270GCCH_handle_create (struct CadetTunnel *t,
271 const struct GNUNET_CADET_ChannelCreate *msg); 271 const struct GNUNET_CADET_ChannelOpenMessage *msg);
272 272
273 273
274/** 274/**
@@ -294,7 +294,7 @@ GCCH_handle_nack (struct CadetChannel *ch);
294 */ 294 */
295void 295void
296GCCH_handle_ack (struct CadetChannel *ch, 296GCCH_handle_ack (struct CadetChannel *ch,
297 const struct GNUNET_CADET_ChannelManage *msg, 297 const struct GNUNET_CADET_ChannelManageMessage *msg,
298 int fwd); 298 int fwd);
299 299
300 300
@@ -310,7 +310,7 @@ GCCH_handle_ack (struct CadetChannel *ch,
310 */ 310 */
311void 311void
312GCCH_handle_destroy (struct CadetChannel *ch, 312GCCH_handle_destroy (struct CadetChannel *ch,
313 const struct GNUNET_CADET_ChannelManage *msg, 313 const struct GNUNET_CADET_ChannelManageMessage *msg,
314 int fwd); 314 int fwd);
315 315
316 316
@@ -347,6 +347,8 @@ const char *
347GCCH_2s (const struct CadetChannel *ch); 347GCCH_2s (const struct CadetChannel *ch);
348 348
349 349
350
351
350#if 0 /* keep Emacsens' auto-indent happy */ 352#if 0 /* keep Emacsens' auto-indent happy */
351{ 353{
352#endif 354#endif
diff --git a/src/cadet/gnunet-service-cadet_connection.c b/src/cadet/gnunet-service-cadet_connection.c
index 1c500f716..931b32b95 100644
--- a/src/cadet/gnunet-service-cadet_connection.c
+++ b/src/cadet/gnunet-service-cadet_connection.c
@@ -112,17 +112,17 @@ struct CadetFlowControl
112 /** 112 /**
113 * ID of the next packet to send. 113 * ID of the next packet to send.
114 */ 114 */
115 uint32_t next_pid; 115 struct CadetEncryptedMessageIdentifier next_pid;
116 116
117 /** 117 /**
118 * ID of the last packet sent towards the peer. 118 * ID of the last packet sent towards the peer.
119 */ 119 */
120 uint32_t last_pid_sent; 120 struct CadetEncryptedMessageIdentifier last_pid_sent;
121 121
122 /** 122 /**
123 * ID of the last packet received from the peer. 123 * ID of the last packet received from the peer.
124 */ 124 */
125 uint32_t last_pid_recv; 125 struct CadetEncryptedMessageIdentifier last_pid_recv;
126 126
127 /** 127 /**
128 * Bitmap of past 32 messages received: 128 * Bitmap of past 32 messages received:
@@ -132,14 +132,15 @@ struct CadetFlowControl
132 uint32_t recv_bitmap; 132 uint32_t recv_bitmap;
133 133
134 /** 134 /**
135 * Last ACK sent to the peer (peer can't send more than this PID). 135 * Last ACK sent to the peer (peer is not allowed to send
136 * messages with PIDs higher than this value).
136 */ 137 */
137 uint32_t last_ack_sent; 138 struct CadetEncryptedMessageIdentifier last_ack_sent;
138 139
139 /** 140 /**
140 * Last ACK sent towards the origin (for traffic towards leaf node). 141 * Last ACK sent towards the origin (for traffic towards leaf node).
141 */ 142 */
142 uint32_t last_ack_recv; 143 struct CadetEncryptedMessageIdentifier last_ack_recv;
143 144
144 /** 145 /**
145 * Task to poll the peer in case of a lost ACK causes stall. 146 * Task to poll the peer in case of a lost ACK causes stall.
@@ -217,7 +218,7 @@ struct CadetConnection
217 /** 218 /**
218 * ID of the connection. 219 * ID of the connection.
219 */ 220 */
220 struct GNUNET_CADET_Hash id; 221 struct GNUNET_CADET_ConnectionTunnelIdentifier id;
221 222
222 /** 223 /**
223 * Path being used for the tunnel. At the origin of the connection 224 * Path being used for the tunnel. At the origin of the connection
@@ -321,7 +322,7 @@ extern struct GNUNET_PeerIdentity my_full_id;
321/** 322/**
322 * Connections known, indexed by cid (CadetConnection). 323 * Connections known, indexed by cid (CadetConnection).
323 */ 324 */
324static struct GNUNET_CONTAINER_MultiHashMap *connections; 325static struct GNUNET_CONTAINER_MultiShortmap *connections;
325 326
326/** 327/**
327 * How many connections are we willing to maintain. 328 * How many connections are we willing to maintain.
@@ -358,7 +359,8 @@ static void
358fc_debug (struct CadetFlowControl *fc) 359fc_debug (struct CadetFlowControl *fc)
359{ 360{
360 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n", 361 LOG (GNUNET_ERROR_TYPE_DEBUG, " IN: %u/%u\n",
361 fc->last_pid_recv, fc->last_ack_sent); 362 ntohl (fc->last_pid_recv.pid),
363 ntohl (fc->last_ack_sent.pid));
362 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n", 364 LOG (GNUNET_ERROR_TYPE_DEBUG, " OUT: %u/%u\n",
363 fc->last_pid_sent, fc->last_ack_recv); 365 fc->last_pid_sent, fc->last_ack_recv);
364 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n", 366 LOG (GNUNET_ERROR_TYPE_DEBUG, " QUEUE: %u/%u\n",
@@ -452,11 +454,11 @@ GCC_state2s (enum CadetConnectionState s)
452static void 454static void
453fc_init (struct CadetFlowControl *fc) 455fc_init (struct CadetFlowControl *fc)
454{ 456{
455 fc->next_pid = (uint32_t) 0; 457 fc->next_pid.pid = 0;
456 fc->last_pid_sent = (uint32_t) -1; 458 fc->last_pid_sent.pid = htonl (UINT32_MAX);
457 fc->last_pid_recv = (uint32_t) -1; 459 fc->last_pid_recv.pid = htonl (UINT32_MAX);
458 fc->last_ack_sent = (uint32_t) 0; 460 fc->last_ack_sent.pid = (uint32_t) 0;
459 fc->last_ack_recv = (uint32_t) 0; 461 fc->last_ack_recv.pid = (uint32_t) 0;
460 fc->poll_task = NULL; 462 fc->poll_task = NULL;
461 fc->poll_time = GNUNET_TIME_UNIT_SECONDS; 463 fc->poll_time = GNUNET_TIME_UNIT_SECONDS;
462 fc->queue_n = 0; 464 fc->queue_n = 0;
@@ -472,9 +474,10 @@ fc_init (struct CadetFlowControl *fc)
472 * @return conntection with the given ID @cid or NULL if not found. 474 * @return conntection with the given ID @cid or NULL if not found.
473 */ 475 */
474static struct CadetConnection * 476static struct CadetConnection *
475connection_get (const struct GNUNET_CADET_Hash *cid) 477connection_get (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid)
476{ 478{
477 return GNUNET_CONTAINER_multihashmap_get (connections, GC_h2hc (cid)); 479 return GNUNET_CONTAINER_multishortmap_get (connections,
480 &cid->connection_of_tunnel);
478} 481}
479 482
480 483
@@ -540,12 +543,16 @@ send_poll (void *cls);
540 * @param force Don't optimize out. 543 * @param force Don't optimize out.
541 */ 544 */
542static void 545static void
543send_ack (struct CadetConnection *c, unsigned int buffer, int fwd, int force) 546send_ack (struct CadetConnection *c,
547 unsigned int buffer,
548 int fwd,
549 int force)
544{ 550{
551 static struct CadetEncryptedMessageIdentifier zero;
545 struct CadetFlowControl *next_fc; 552 struct CadetFlowControl *next_fc;
546 struct CadetFlowControl *prev_fc; 553 struct CadetFlowControl *prev_fc;
547 struct GNUNET_CADET_ACK msg; 554 struct GNUNET_CADET_ConnectionEncryptedAckMessage msg;
548 uint32_t ack; 555 struct CadetEncryptedMessageIdentifier ack_cemi;
549 int delta; 556 int delta;
550 557
551 GCC_check_connections (); 558 GCC_check_connections ();
@@ -558,24 +565,28 @@ send_ack (struct CadetConnection *c, unsigned int buffer, int fwd, int force)
558 GC_f2s (fwd), GCC_2s (c)); 565 GC_f2s (fwd), GCC_2s (c));
559 566
560 /* Check if we need to transmit the ACK. */ 567 /* Check if we need to transmit the ACK. */
561 delta = prev_fc->last_ack_sent - prev_fc->last_pid_recv; 568 delta = ntohl (prev_fc->last_ack_sent.pid) - ntohl (prev_fc->last_pid_recv.pid);
562 if (3 < delta && buffer < delta && GNUNET_NO == force) 569 if (3 < delta && buffer < delta && GNUNET_NO == force)
563 { 570 {
564 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, delta > 3\n"); 571 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, delta > 3\n");
565 LOG (GNUNET_ERROR_TYPE_DEBUG, 572 LOG (GNUNET_ERROR_TYPE_DEBUG,
566 " last pid recv: %u, last ack sent: %u\n", 573 " last pid recv: %u, last ack sent: %u\n",
567 prev_fc->last_pid_recv, prev_fc->last_ack_sent); 574 ntohl (prev_fc->last_pid_recv.pid),
575 ntohl (prev_fc->last_ack_sent.pid));
568 GCC_check_connections (); 576 GCC_check_connections ();
569 return; 577 return;
570 } 578 }
571 579
572 /* Ok, ACK might be necessary, what PID to ACK? */ 580 /* Ok, ACK might be necessary, what PID to ACK? */
573 ack = prev_fc->last_pid_recv + buffer; 581 ack_cemi.pid = htonl (ntohl (prev_fc->last_pid_recv.pid) + buffer);
574 LOG (GNUNET_ERROR_TYPE_DEBUG, 582 LOG (GNUNET_ERROR_TYPE_DEBUG,
575 " ACK %u, last PID %u, last ACK %u, qmax %u, q %u\n", 583 " ACK %u, last PID %u, last ACK %u, qmax %u, q %u\n",
576 ack, prev_fc->last_pid_recv, prev_fc->last_ack_sent, 584 ntohl (ack_cemi.pid),
585 ntohl (prev_fc->last_pid_recv.pid),
586 ntohl (prev_fc->last_ack_sent.pid),
577 next_fc->queue_max, next_fc->queue_n); 587 next_fc->queue_max, next_fc->queue_n);
578 if (ack == prev_fc->last_ack_sent && GNUNET_NO == force) 588 if ( (ack_cemi.pid == prev_fc->last_ack_sent.pid) &&
589 (GNUNET_NO == force) )
579 { 590 {
580 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n"); 591 LOG (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not needed\n");
581 GCC_check_connections (); 592 GCC_check_connections ();
@@ -585,7 +596,8 @@ send_ack (struct CadetConnection *c, unsigned int buffer, int fwd, int force)
585 /* Check if message is already in queue */ 596 /* Check if message is already in queue */
586 if (NULL != prev_fc->ack_msg) 597 if (NULL != prev_fc->ack_msg)
587 { 598 {
588 if (GC_is_pid_bigger (ack, prev_fc->last_ack_sent)) 599 if (GC_is_pid_bigger (ntohl (ack_cemi.pid),
600 ntohl (prev_fc->last_ack_sent.pid)))
589 { 601 {
590 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n"); 602 LOG (GNUNET_ERROR_TYPE_DEBUG, " canceling old ACK\n");
591 GCC_cancel (prev_fc->ack_msg); 603 GCC_cancel (prev_fc->ack_msg);
@@ -598,17 +610,22 @@ send_ack (struct CadetConnection *c, unsigned int buffer, int fwd, int force)
598 return; 610 return;
599 } 611 }
600 } 612 }
601 613 GNUNET_break (GC_is_pid_bigger (ntohl (ack_cemi.pid),
602 prev_fc->last_ack_sent = ack; 614 ntohl (prev_fc->last_ack_sent.pid)));
615 prev_fc->last_ack_sent = ack_cemi;
603 616
604 /* Build ACK message and send on conn */ 617 /* Build ACK message and send on conn */
605 msg.header.size = htons (sizeof (msg)); 618 msg.header.size = htons (sizeof (msg));
606 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_ACK); 619 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK);
607 msg.ack = htonl (ack); 620 msg.cemi = ack_cemi;
608 msg.cid = c->id; 621 msg.cid = c->id;
609 622
610 prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header, UINT16_MAX, ack, 623 prev_fc->ack_msg = GCC_send_prebuilt_message (&msg.header,
611 c, !fwd, GNUNET_YES, 624 UINT16_MAX,
625 zero,
626 c,
627 !fwd,
628 GNUNET_YES,
612 NULL, NULL); 629 NULL, NULL);
613 GNUNET_assert (NULL != prev_fc->ack_msg); 630 GNUNET_assert (NULL != prev_fc->ack_msg);
614 GCC_check_connections (); 631 GCC_check_connections ();
@@ -672,8 +689,12 @@ update_perf (struct CadetConnection *c,
672 */ 689 */
673static void 690static void
674conn_message_sent (void *cls, 691conn_message_sent (void *cls,
675 struct CadetConnection *c, int fwd, int sent, 692 struct CadetConnection *c,
676 uint16_t type, uint16_t payload_type, uint32_t pid, 693 int fwd,
694 int sent,
695 uint16_t type,
696 uint16_t payload_type,
697 struct CadetEncryptedMessageIdentifier pid,
677 size_t size, 698 size_t size,
678 struct GNUNET_TIME_Relative wait) 699 struct GNUNET_TIME_Relative wait)
679{ 700{
@@ -684,8 +705,11 @@ conn_message_sent (void *cls,
684 GCC_check_connections (); 705 GCC_check_connections ();
685 LOG (GNUNET_ERROR_TYPE_INFO, 706 LOG (GNUNET_ERROR_TYPE_INFO,
686 ">>> %s (%s %4u) on conn %s (%p) %s [%5u] in queue %s\n", 707 ">>> %s (%s %4u) on conn %s (%p) %s [%5u] in queue %s\n",
687 GC_m2s (type), GC_m2s (payload_type), pid, GCC_2s (c), c, 708 GC_m2s (type), GC_m2s (payload_type),
688 GC_f2s(fwd), size, 709 ntohl (pid.pid),
710 GCC_2s (c),
711 c,
712 GC_f2s (fwd), size,
689 GNUNET_STRINGS_relative_time_to_string (wait, GNUNET_YES)); 713 GNUNET_STRINGS_relative_time_to_string (wait, GNUNET_YES));
690 714
691 /* If c is NULL, nothing to update. */ 715 /* If c is NULL, nothing to update. */
@@ -703,7 +727,8 @@ conn_message_sent (void *cls,
703 727
704 LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s pid %u\n", 728 LOG (GNUNET_ERROR_TYPE_DEBUG, " %ssent %s %s pid %u\n",
705 sent ? "" : "not ", GC_f2s (fwd), 729 sent ? "" : "not ", GC_f2s (fwd),
706 GC_m2s (type), GC_m2s (payload_type), pid); 730 GC_m2s (type), GC_m2s (payload_type),
731 ntohl (pid.pid));
707 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG); 732 GCC_debug (c, GNUNET_ERROR_TYPE_DEBUG);
708 733
709 /* Update flow control info. */ 734 /* Update flow control info. */
@@ -722,7 +747,7 @@ conn_message_sent (void *cls,
722 } 747 }
723 else /* CONN_CREATE or CONN_ACK */ 748 else /* CONN_CREATE or CONN_ACK */
724 { 749 {
725 GNUNET_assert (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED != type); 750 GNUNET_assert (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED != type);
726 forced = GNUNET_YES; 751 forced = GNUNET_YES;
727 } 752 }
728 753
@@ -742,18 +767,19 @@ conn_message_sent (void *cls,
742 switch (type) 767 switch (type)
743 { 768 {
744 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE: 769 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
745 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK: 770 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK:
746 c->maintenance_q = NULL; 771 c->maintenance_q = NULL;
747 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */ 772 /* Don't trigger a keepalive for sent ACKs, only SYN and SYNACKs */
748 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd) 773 if (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE == type || !fwd)
749 schedule_next_keepalive (c, fwd); 774 schedule_next_keepalive (c, fwd);
750 break; 775 break;
751 776
752 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED: 777 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED:
753 if (GNUNET_YES == sent) 778 if (GNUNET_YES == sent)
754 { 779 {
755 fc->last_pid_sent = pid; 780 fc->last_pid_sent = pid;
756 if (GC_is_pid_bigger (fc->last_pid_sent + 1, fc->last_ack_recv)) 781 if (GC_is_pid_bigger (ntohl (fc->last_pid_sent.pid) + 1,
782 ntohl (fc->last_ack_recv.pid)) )
757 GCC_start_poll (c, fwd); 783 GCC_start_poll (c, fwd);
758 GCC_send_ack (c, fwd, GNUNET_NO); 784 GCC_send_ack (c, fwd, GNUNET_NO);
759 connection_reset_timeout (c, fwd); 785 connection_reset_timeout (c, fwd);
@@ -764,23 +790,23 @@ conn_message_sent (void *cls,
764 { 790 {
765 fc->queue_n--; 791 fc->queue_n--;
766 LOG (GNUNET_ERROR_TYPE_DEBUG, 792 LOG (GNUNET_ERROR_TYPE_DEBUG,
767 "! accounting pid %u\n", 793 "! accounting pid %u\n",
768 fc->last_pid_sent); 794 ntohl (fc->last_pid_sent.pid));
769 } 795 }
770 else 796 else
771 { 797 {
772 LOG (GNUNET_ERROR_TYPE_DEBUG, 798 LOG (GNUNET_ERROR_TYPE_DEBUG,
773 "! forced, Q_N not accounting pid %u\n", 799 "! forced, Q_N not accounting pid %u\n",
774 fc->last_pid_sent); 800 ntohl (fc->last_pid_sent.pid));
775 } 801 }
776 break; 802 break;
777 803
778 case GNUNET_MESSAGE_TYPE_CADET_KX: 804 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX:
779 if (GNUNET_YES == sent) 805 if (GNUNET_YES == sent)
780 connection_reset_timeout (c, fwd); 806 connection_reset_timeout (c, fwd);
781 break; 807 break;
782 808
783 case GNUNET_MESSAGE_TYPE_CADET_POLL: 809 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL:
784 fc->poll_msg = NULL; 810 fc->poll_msg = NULL;
785 if (2 == c->destroy) 811 if (2 == c->destroy)
786 { 812 {
@@ -801,7 +827,7 @@ conn_message_sent (void *cls,
801 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task); 827 LOG (GNUNET_ERROR_TYPE_DEBUG, " task %u\n", fc->poll_task);
802 break; 828 break;
803 829
804 case GNUNET_MESSAGE_TYPE_CADET_ACK: 830 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK:
805 fc->ack_msg = NULL; 831 fc->ack_msg = NULL;
806 break; 832 break;
807 833
@@ -905,7 +931,7 @@ check_neighbours (const struct CadetConnection *c)
905 */ 931 */
906static int 932static int
907check_connection (void *cls, 933check_connection (void *cls,
908 const struct GNUNET_HashCode *key, 934 const struct GNUNET_ShortHashCode *key,
909 void *value) 935 void *value)
910{ 936{
911 struct CadetConnection *c = value; 937 struct CadetConnection *c = value;
@@ -925,9 +951,9 @@ GCC_check_connections ()
925 return; 951 return;
926 if (NULL == connections) 952 if (NULL == connections)
927 return; 953 return;
928 GNUNET_CONTAINER_multihashmap_iterate (connections, 954 GNUNET_CONTAINER_multishortmap_iterate (connections,
929 &check_connection, 955 &check_connection,
930 NULL); 956 NULL);
931} 957}
932 958
933 959
@@ -953,9 +979,11 @@ get_hop (struct CadetConnection *c, int fwd)
953 * @param ooo_pid PID of the out-of-order message. 979 * @param ooo_pid PID of the out-of-order message.
954 */ 980 */
955static uint32_t 981static uint32_t
956get_recv_bitmask (uint32_t last_pid_recv, uint32_t ooo_pid) 982get_recv_bitmask (struct CadetEncryptedMessageIdentifier last_pid_recv,
983 struct CadetEncryptedMessageIdentifier ooo_pid)
957{ 984{
958 return 1 << (last_pid_recv - ooo_pid); 985 // FIXME: should assert that the delta is in range...
986 return 1 << (ntohl (last_pid_recv.pid) - ntohl (ooo_pid.pid));
959} 987}
960 988
961 989
@@ -967,14 +995,18 @@ get_recv_bitmask (uint32_t last_pid_recv, uint32_t ooo_pid)
967 * @param last_pid_recv Last in-order PID received. 995 * @param last_pid_recv Last in-order PID received.
968 */ 996 */
969static int 997static int
970is_ooo_ok (uint32_t last_pid_recv, uint32_t ooo_pid, uint32_t ooo_bitmap) 998is_ooo_ok (struct CadetEncryptedMessageIdentifier last_pid_recv,
999 struct CadetEncryptedMessageIdentifier ooo_pid,
1000 uint32_t ooo_bitmap)
971{ 1001{
972 uint32_t mask; 1002 uint32_t mask;
973 1003
974 if (GC_is_pid_bigger (last_pid_recv - 31, ooo_pid)) 1004 if (GC_is_pid_bigger (ntohl (last_pid_recv.pid) - 31,
1005 ntohl (ooo_pid.pid)))
975 return GNUNET_NO; 1006 return GNUNET_NO;
976 1007
977 mask = get_recv_bitmask (last_pid_recv, ooo_pid); 1008 mask = get_recv_bitmask (last_pid_recv,
1009 ooo_pid);
978 if (0 != (ooo_bitmap & mask)) 1010 if (0 != (ooo_bitmap & mask))
979 return GNUNET_NO; 1011 return GNUNET_NO;
980 1012
@@ -1021,10 +1053,11 @@ is_fwd (const struct CadetConnection *c,
1021static void 1053static void
1022send_connection_ack (struct CadetConnection *c, int fwd) 1054send_connection_ack (struct CadetConnection *c, int fwd)
1023{ 1055{
1024 struct GNUNET_CADET_ConnectionACK msg; 1056 static struct CadetEncryptedMessageIdentifier zero;
1057 struct GNUNET_CADET_ConnectionCreateMessageAckMessage msg;
1025 struct CadetTunnel *t; 1058 struct CadetTunnel *t;
1026 const uint16_t size = sizeof (struct GNUNET_CADET_ConnectionACK); 1059 const uint16_t size = sizeof (struct GNUNET_CADET_ConnectionCreateMessageAckMessage);
1027 const uint16_t type = GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK; 1060 const uint16_t type = GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK;
1028 1061
1029 GCC_check_connections (); 1062 GCC_check_connections ();
1030 t = c->t; 1063 t = c->t;
@@ -1038,9 +1071,12 @@ send_connection_ack (struct CadetConnection *c, int fwd)
1038 msg.cid = c->id; 1071 msg.cid = c->id;
1039 1072
1040 GNUNET_assert (NULL == c->maintenance_q); 1073 GNUNET_assert (NULL == c->maintenance_q);
1041 c->maintenance_q = GCP_send (get_hop (c, fwd), &msg.header, 1074 c->maintenance_q = GCP_send (get_hop (c, fwd),
1042 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK, 0, 1075 &msg.header,
1043 c, fwd, 1076 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK,
1077 zero,
1078 c,
1079 fwd,
1044 &conn_message_sent, NULL); 1080 &conn_message_sent, NULL);
1045 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (conn`ACK)\n", 1081 LOG (GNUNET_ERROR_TYPE_DEBUG, " C_P+ %p %u (conn`ACK)\n",
1046 c, c->pending_messages); 1082 c, c->pending_messages);
@@ -1068,17 +1104,23 @@ send_broken (struct CadetConnection *c,
1068 const struct GNUNET_PeerIdentity *id2, 1104 const struct GNUNET_PeerIdentity *id2,
1069 int fwd) 1105 int fwd)
1070{ 1106{
1071 struct GNUNET_CADET_ConnectionBroken msg; 1107 static struct CadetEncryptedMessageIdentifier zero;
1108 struct GNUNET_CADET_ConnectionBrokenMessage msg;
1072 1109
1073 GCC_check_connections (); 1110 GCC_check_connections ();
1074 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken)); 1111 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBrokenMessage));
1075 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN); 1112 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1076 msg.cid = c->id; 1113 msg.cid = c->id;
1077 msg.reserved = htonl (0); 1114 msg.reserved = htonl (0);
1078 msg.peer1 = *id1; 1115 msg.peer1 = *id1;
1079 msg.peer2 = *id2; 1116 msg.peer2 = *id2;
1080 (void) GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c, fwd, 1117 (void) GCC_send_prebuilt_message (&msg.header,
1081 GNUNET_YES, NULL, NULL); 1118 UINT16_MAX,
1119 zero,
1120 c,
1121 fwd,
1122 GNUNET_YES,
1123 NULL, NULL);
1082 GCC_check_connections (); 1124 GCC_check_connections ();
1083} 1125}
1084 1126
@@ -1093,18 +1135,19 @@ send_broken (struct CadetConnection *c,
1093 * @param neighbor Peer to notify (neighbor who sent the connection). 1135 * @param neighbor Peer to notify (neighbor who sent the connection).
1094 */ 1136 */
1095static void 1137static void
1096send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id, 1138send_broken_unknown (const struct GNUNET_CADET_ConnectionTunnelIdentifier *connection_id,
1097 const struct GNUNET_PeerIdentity *id1, 1139 const struct GNUNET_PeerIdentity *id1,
1098 const struct GNUNET_PeerIdentity *id2, 1140 const struct GNUNET_PeerIdentity *id2,
1099 struct CadetPeer *neighbor) 1141 struct CadetPeer *neighbor)
1100{ 1142{
1101 struct GNUNET_CADET_ConnectionBroken msg; 1143 static struct CadetEncryptedMessageIdentifier zero;
1144 struct GNUNET_CADET_ConnectionBrokenMessage msg;
1102 1145
1103 GCC_check_connections (); 1146 GCC_check_connections ();
1104 LOG (GNUNET_ERROR_TYPE_INFO, "--> BROKEN on unknown connection %s\n", 1147 LOG (GNUNET_ERROR_TYPE_INFO, "--> BROKEN on unknown connection %s\n",
1105 GNUNET_h2s (GC_h2hc (connection_id))); 1148 GNUNET_sh2s (&connection_id->connection_of_tunnel));
1106 1149
1107 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBroken)); 1150 msg.header.size = htons (sizeof (struct GNUNET_CADET_ConnectionBrokenMessage));
1108 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN); 1151 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN);
1109 msg.cid = *connection_id; 1152 msg.cid = *connection_id;
1110 msg.reserved = htonl (0); 1153 msg.reserved = htonl (0);
@@ -1113,9 +1156,12 @@ send_broken_unknown (const struct GNUNET_CADET_Hash *connection_id,
1113 msg.peer2 = *id2; 1156 msg.peer2 = *id2;
1114 else 1157 else
1115 memset (&msg.peer2, 0, sizeof (msg.peer2)); 1158 memset (&msg.peer2, 0, sizeof (msg.peer2));
1116 GNUNET_assert (NULL != GCP_send (neighbor, &msg.header, 1159 GNUNET_assert (NULL != GCP_send (neighbor,
1117 UINT16_MAX, 2, 1160 &msg.header,
1118 NULL, GNUNET_SYSERR, /* connection, fwd */ 1161 UINT16_MAX,
1162 zero,
1163 NULL,
1164 GNUNET_SYSERR, /* connection, fwd */
1119 NULL, NULL)); /* continuation */ 1165 NULL, NULL)); /* continuation */
1120 GCC_check_connections (); 1166 GCC_check_connections ();
1121} 1167}
@@ -1153,7 +1199,7 @@ send_connection_keepalive (struct CadetConnection *c, int fwd)
1153 1199
1154 GNUNET_assert (NULL != c->t); 1200 GNUNET_assert (NULL != c->t);
1155 msg.size = htons (sizeof (msg)); 1201 msg.size = htons (sizeof (msg));
1156 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE); 1202 msg.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE);
1157 1203
1158 GNUNET_assert (NULL == 1204 GNUNET_assert (NULL ==
1159 GCT_send_prebuilt_message (&msg, c->t, c, 1205 GCT_send_prebuilt_message (&msg, c->t, c,
@@ -1410,8 +1456,9 @@ connection_cancel_queues (struct CadetConnection *c,
1410static void 1456static void
1411send_poll (void *cls) 1457send_poll (void *cls)
1412{ 1458{
1459 static struct CadetEncryptedMessageIdentifier zero;
1413 struct CadetFlowControl *fc = cls; 1460 struct CadetFlowControl *fc = cls;
1414 struct GNUNET_CADET_Poll msg; 1461 struct GNUNET_CADET_ConnectionHopByHopPollMessage msg;
1415 struct CadetConnection *c; 1462 struct CadetConnection *c;
1416 int fwd; 1463 int fwd;
1417 1464
@@ -1422,14 +1469,20 @@ send_poll (void *cls)
1422 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n", 1469 LOG (GNUNET_ERROR_TYPE_DEBUG, "Polling connection %s %s\n",
1423 GCC_2s (c), GC_f2s (fwd)); 1470 GCC_2s (c), GC_f2s (fwd));
1424 1471
1425 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_POLL); 1472 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL);
1426 msg.header.size = htons (sizeof (msg)); 1473 msg.header.size = htons (sizeof (msg));
1427 msg.cid = c->id; 1474 msg.cid = c->id;
1428 msg.pid = htonl (fc->last_pid_sent); 1475 msg.cemi = fc->last_pid_sent;
1429 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", fc->last_pid_sent); 1476 LOG (GNUNET_ERROR_TYPE_DEBUG, " last pid sent: %u\n", ntohl (fc->last_pid_sent.pid));
1430 fc->poll_msg = 1477 fc->poll_msg
1431 GCC_send_prebuilt_message (&msg.header, UINT16_MAX, fc->last_pid_sent, c, 1478 = GCC_send_prebuilt_message (&msg.header,
1432 fc == &c->fwd_fc, GNUNET_YES, NULL, NULL); 1479 UINT16_MAX,
1480 zero,
1481 c,
1482 fc == &c->fwd_fc,
1483 GNUNET_YES,
1484 NULL,
1485 NULL);
1433 GNUNET_assert (NULL != fc->poll_msg); 1486 GNUNET_assert (NULL != fc->poll_msg);
1434 GCC_check_connections (); 1487 GCC_check_connections ();
1435} 1488}
@@ -1829,7 +1882,7 @@ add_to_peer (struct CadetConnection *c,
1829static void 1882static void
1830log_message (const struct GNUNET_MessageHeader *message, 1883log_message (const struct GNUNET_MessageHeader *message,
1831 const struct CadetPeer *peer, 1884 const struct CadetPeer *peer,
1832 const struct GNUNET_CADET_Hash *conn_id) 1885 const struct GNUNET_CADET_ConnectionTunnelIdentifier *conn_id)
1833{ 1886{
1834 uint16_t size; 1887 uint16_t size;
1835 uint16_t type; 1888 uint16_t type;
@@ -1840,7 +1893,7 @@ log_message (const struct GNUNET_MessageHeader *message,
1840 switch (type) 1893 switch (type)
1841 { 1894 {
1842 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE: 1895 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
1843 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK: 1896 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK:
1844 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN: 1897 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
1845 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY: 1898 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
1846 arrow = "=="; 1899 arrow = "==";
@@ -1848,9 +1901,13 @@ log_message (const struct GNUNET_MessageHeader *message,
1848 default: 1901 default:
1849 arrow = "--"; 1902 arrow = "--";
1850 } 1903 }
1851 LOG (GNUNET_ERROR_TYPE_INFO, "<%s %s on conn %s from %s, %6u bytes\n", 1904 LOG (GNUNET_ERROR_TYPE_INFO,
1852 arrow, GC_m2s (type), GNUNET_h2s (GC_h2hc (conn_id)), 1905 "<%s %s on conn %s from %s, %6u bytes\n",
1853 GCP_2s(peer), (unsigned int) size); 1906 arrow,
1907 GC_m2s (type),
1908 GNUNET_sh2s (&conn_id->connection_of_tunnel),
1909 GCP_2s(peer),
1910 (unsigned int) size);
1854} 1911}
1855 1912
1856/******************************************************************************/ 1913/******************************************************************************/
@@ -1865,9 +1922,10 @@ log_message (const struct GNUNET_MessageHeader *message,
1865 */ 1922 */
1866void 1923void
1867GCC_handle_create (struct CadetPeer *peer, 1924GCC_handle_create (struct CadetPeer *peer,
1868 const struct GNUNET_CADET_ConnectionCreate *msg) 1925 const struct GNUNET_CADET_ConnectionCreateMessage *msg)
1869{ 1926{
1870 const struct GNUNET_CADET_Hash *cid; 1927 static struct CadetEncryptedMessageIdentifier zero;
1928 const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid;
1871 struct GNUNET_PeerIdentity *id; 1929 struct GNUNET_PeerIdentity *id;
1872 struct CadetPeerPath *path; 1930 struct CadetPeerPath *path;
1873 struct CadetPeer *dest_peer; 1931 struct CadetPeer *dest_peer;
@@ -1880,7 +1938,7 @@ GCC_handle_create (struct CadetPeer *peer,
1880 size = ntohs (msg->header.size); 1938 size = ntohs (msg->header.size);
1881 1939
1882 /* Calculate hops */ 1940 /* Calculate hops */
1883 size -= sizeof (struct GNUNET_CADET_ConnectionCreate); 1941 size -= sizeof (struct GNUNET_CADET_ConnectionCreateMessage);
1884 if (0 != size % sizeof (struct GNUNET_PeerIdentity)) 1942 if (0 != size % sizeof (struct GNUNET_PeerIdentity))
1885 { 1943 {
1886 GNUNET_break_op (0); 1944 GNUNET_break_op (0);
@@ -1989,8 +2047,12 @@ GCC_handle_create (struct CadetPeer *peer,
1989 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n"); 2047 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
1990 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO); 2048 GCP_add_path (dest_peer, path_duplicate (path), GNUNET_NO);
1991 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO); 2049 GCP_add_path_to_origin (orig_peer, path_duplicate (path), GNUNET_NO);
1992 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, 2050 (void) GCC_send_prebuilt_message (&msg->header,
1993 GNUNET_YES, GNUNET_YES, NULL, NULL); 2051 0,
2052 zero,
2053 c,
2054 GNUNET_YES, GNUNET_YES,
2055 NULL, NULL);
1994 } 2056 }
1995 path_destroy (path); 2057 path_destroy (path);
1996 GCC_check_connections (); 2058 GCC_check_connections ();
@@ -2005,8 +2067,9 @@ GCC_handle_create (struct CadetPeer *peer,
2005 */ 2067 */
2006void 2068void
2007GCC_handle_confirm (struct CadetPeer *peer, 2069GCC_handle_confirm (struct CadetPeer *peer,
2008 const struct GNUNET_CADET_ConnectionACK *msg) 2070 const struct GNUNET_CADET_ConnectionCreateMessageAckMessage *msg)
2009{ 2071{
2072 static struct CadetEncryptedMessageIdentifier zero;
2010 struct CadetConnection *c; 2073 struct CadetConnection *c;
2011 enum CadetConnectionState oldstate; 2074 enum CadetConnectionState oldstate;
2012 int fwd; 2075 int fwd;
@@ -2114,7 +2177,10 @@ GCC_handle_confirm (struct CadetPeer *peer,
2114 else 2177 else
2115 { 2178 {
2116 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n"); 2179 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2117 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd, 2180 (void) GCC_send_prebuilt_message (&msg->header, 0,
2181 zero,
2182 c,
2183 fwd,
2118 GNUNET_YES, NULL, NULL); 2184 GNUNET_YES, NULL, NULL);
2119 } 2185 }
2120 GCC_check_connections (); 2186 GCC_check_connections ();
@@ -2129,8 +2195,9 @@ GCC_handle_confirm (struct CadetPeer *peer,
2129 */ 2195 */
2130void 2196void
2131GCC_handle_broken (struct CadetPeer *peer, 2197GCC_handle_broken (struct CadetPeer *peer,
2132 const struct GNUNET_CADET_ConnectionBroken *msg) 2198 const struct GNUNET_CADET_ConnectionBrokenMessage *msg)
2133{ 2199{
2200 static struct CadetEncryptedMessageIdentifier zero;
2134 struct CadetConnection *c; 2201 struct CadetConnection *c;
2135 struct CadetTunnel *t; 2202 struct CadetTunnel *t;
2136 int fwd; 2203 int fwd;
@@ -2183,7 +2250,8 @@ GCC_handle_broken (struct CadetPeer *peer,
2183 } 2250 }
2184 else 2251 else
2185 { 2252 {
2186 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd, 2253 (void) GCC_send_prebuilt_message (&msg->header, 0,
2254 zero, c, fwd,
2187 GNUNET_YES, NULL, NULL); 2255 GNUNET_YES, NULL, NULL);
2188 connection_cancel_queues (c, !fwd); 2256 connection_cancel_queues (c, !fwd);
2189 } 2257 }
@@ -2200,8 +2268,9 @@ GCC_handle_broken (struct CadetPeer *peer,
2200 */ 2268 */
2201void 2269void
2202GCC_handle_destroy (struct CadetPeer *peer, 2270GCC_handle_destroy (struct CadetPeer *peer,
2203 const struct GNUNET_CADET_ConnectionDestroy *msg) 2271 const struct GNUNET_CADET_ConnectionDestroyMessage *msg)
2204{ 2272{
2273 static struct CadetEncryptedMessageIdentifier zero;
2205 struct CadetConnection *c; 2274 struct CadetConnection *c;
2206 int fwd; 2275 int fwd;
2207 2276
@@ -2233,7 +2302,8 @@ GCC_handle_destroy (struct CadetPeer *peer,
2233 2302
2234 if (GNUNET_NO == GCC_is_terminal (c, fwd)) 2303 if (GNUNET_NO == GCC_is_terminal (c, fwd))
2235 { 2304 {
2236 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd, 2305 (void) GCC_send_prebuilt_message (&msg->header, 0,
2306 zero, c, fwd,
2237 GNUNET_YES, NULL, NULL); 2307 GNUNET_YES, NULL, NULL);
2238 } 2308 }
2239 else if (0 == c->pending_messages) 2309 else if (0 == c->pending_messages)
@@ -2262,11 +2332,11 @@ GCC_handle_destroy (struct CadetPeer *peer,
2262 */ 2332 */
2263void 2333void
2264GCC_handle_ack (struct CadetPeer *peer, 2334GCC_handle_ack (struct CadetPeer *peer,
2265 const struct GNUNET_CADET_ACK *msg) 2335 const struct GNUNET_CADET_ConnectionEncryptedAckMessage *msg)
2266{ 2336{
2267 struct CadetConnection *c; 2337 struct CadetConnection *c;
2268 struct CadetFlowControl *fc; 2338 struct CadetFlowControl *fc;
2269 uint32_t ack; 2339 struct CadetEncryptedMessageIdentifier ack;
2270 int fwd; 2340 int fwd;
2271 2341
2272 GCC_check_connections (); 2342 GCC_check_connections ();
@@ -2303,15 +2373,19 @@ GCC_handle_ack (struct CadetPeer *peer,
2303 return; 2373 return;
2304 } 2374 }
2305 2375
2306 ack = ntohl (msg->ack); 2376 ack = msg->cemi;
2307 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n", 2377 LOG (GNUNET_ERROR_TYPE_DEBUG, " %s ACK %u (was %u)\n",
2308 GC_f2s (fwd), ack, fc->last_ack_recv); 2378 GC_f2s (fwd),
2309 if (GC_is_pid_bigger (ack, fc->last_ack_recv)) 2379 ntohl (ack.pid),
2380 ntohl (fc->last_ack_recv.pid));
2381 if (GC_is_pid_bigger (ntohl (ack.pid),
2382 ntohl (fc->last_ack_recv.pid)))
2310 fc->last_ack_recv = ack; 2383 fc->last_ack_recv = ack;
2311 2384
2312 /* Cancel polling if the ACK is big enough. */ 2385 /* Cancel polling if the ACK is big enough. */
2313 if (NULL != fc->poll_task && 2386 if ( (NULL != fc->poll_task) &
2314 GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent)) 2387 GC_is_pid_bigger (ntohl (fc->last_ack_recv.pid),
2388 ntohl (fc->last_pid_sent.pid)))
2315 { 2389 {
2316 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n"); 2390 LOG (GNUNET_ERROR_TYPE_DEBUG, " Cancel poll\n");
2317 GNUNET_SCHEDULER_cancel (fc->poll_task); 2391 GNUNET_SCHEDULER_cancel (fc->poll_task);
@@ -2331,11 +2405,11 @@ GCC_handle_ack (struct CadetPeer *peer,
2331 */ 2405 */
2332void 2406void
2333GCC_handle_poll (struct CadetPeer *peer, 2407GCC_handle_poll (struct CadetPeer *peer,
2334 const struct GNUNET_CADET_Poll *msg) 2408 const struct GNUNET_CADET_ConnectionHopByHopPollMessage *msg)
2335{ 2409{
2336 struct CadetConnection *c; 2410 struct CadetConnection *c;
2337 struct CadetFlowControl *fc; 2411 struct CadetFlowControl *fc;
2338 uint32_t pid; 2412 struct CadetEncryptedMessageIdentifier pid;
2339 int fwd; 2413 int fwd;
2340 2414
2341 GCC_check_connections (); 2415 GCC_check_connections ();
@@ -2347,7 +2421,7 @@ GCC_handle_poll (struct CadetPeer *peer,
2347 GNUNET_NO); 2421 GNUNET_NO);
2348 LOG (GNUNET_ERROR_TYPE_DEBUG, 2422 LOG (GNUNET_ERROR_TYPE_DEBUG,
2349 "POLL message on unknown connection %s!\n", 2423 "POLL message on unknown connection %s!\n",
2350 GNUNET_h2s (GC_h2hc (&msg->cid))); 2424 GNUNET_sh2s (&msg->cid.connection_of_tunnel));
2351 send_broken_unknown (&msg->cid, 2425 send_broken_unknown (&msg->cid,
2352 &my_full_id, 2426 &my_full_id,
2353 NULL, 2427 NULL,
@@ -2377,8 +2451,11 @@ GCC_handle_poll (struct CadetPeer *peer,
2377 return; 2451 return;
2378 } 2452 }
2379 2453
2380 pid = ntohl (msg->pid); 2454 pid = msg->cemi;
2381 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u, OLD %u\n", pid, fc->last_pid_recv); 2455 LOG (GNUNET_ERROR_TYPE_DEBUG,
2456 " PID %u, OLD %u\n",
2457 ntohl (pid.pid),
2458 ntohl (fc->last_pid_recv.pid));
2382 fc->last_pid_recv = pid; 2459 fc->last_pid_recv = pid;
2383 fwd = fc == &c->bck_fc; 2460 fwd = fc == &c->bck_fc;
2384 GCC_send_ack (c, fwd, GNUNET_YES); 2461 GCC_send_ack (c, fwd, GNUNET_YES);
@@ -2402,10 +2479,10 @@ GCC_handle_poll (struct CadetPeer *peer,
2402 */ 2479 */
2403static int 2480static int
2404check_message (const struct GNUNET_MessageHeader *message, 2481check_message (const struct GNUNET_MessageHeader *message,
2405 const struct GNUNET_CADET_Hash* cid, 2482 const struct GNUNET_CADET_ConnectionTunnelIdentifier* cid,
2406 struct CadetConnection *c, 2483 struct CadetConnection *c,
2407 struct CadetPeer *sender, 2484 struct CadetPeer *sender,
2408 uint32_t pid) 2485 struct CadetEncryptedMessageIdentifier pid)
2409{ 2486{
2410 struct CadetFlowControl *fc; 2487 struct CadetFlowControl *fc;
2411 struct CadetPeer *hop; 2488 struct CadetPeer *hop;
@@ -2421,7 +2498,8 @@ check_message (const struct GNUNET_MessageHeader *message,
2421 LOG (GNUNET_ERROR_TYPE_DEBUG, 2498 LOG (GNUNET_ERROR_TYPE_DEBUG,
2422 "%s on unknown connection %s\n", 2499 "%s on unknown connection %s\n",
2423 GC_m2s (ntohs (message->type)), 2500 GC_m2s (ntohs (message->type)),
2424 GNUNET_h2s (GC_h2hc (cid))); 2501 GNUNET_sh2s (&cid->connection_of_tunnel));
2502 GNUNET_break_op (0);
2425 send_broken_unknown (cid, 2503 send_broken_unknown (cid,
2426 &my_full_id, 2504 &my_full_id,
2427 NULL, 2505 NULL,
@@ -2453,43 +2531,59 @@ check_message (const struct GNUNET_MessageHeader *message,
2453 2531
2454 /* Check PID for payload messages */ 2532 /* Check PID for payload messages */
2455 type = ntohs (message->type); 2533 type = ntohs (message->type);
2456 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type) 2534 if (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED == type)
2457 { 2535 {
2458 fc = fwd ? &c->bck_fc : &c->fwd_fc; 2536 fc = fwd ? &c->bck_fc : &c->fwd_fc;
2459 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected %u - %u)\n", 2537 LOG (GNUNET_ERROR_TYPE_DEBUG, " PID %u (expected in interval [%u,%u])\n",
2460 pid, fc->last_pid_recv + 1, fc->last_ack_sent); 2538 ntohl (pid.pid),
2461 if (GC_is_pid_bigger (pid, fc->last_ack_sent)) 2539 ntohl (fc->last_pid_recv.pid) + 1,
2540 ntohl (fc->last_ack_sent.pid));
2541 if (GC_is_pid_bigger (ntohl (pid.pid),
2542 ntohl (fc->last_ack_sent.pid)))
2462 { 2543 {
2463 GNUNET_break_op (0); 2544 GNUNET_STATISTICS_update (stats,
2464 GNUNET_STATISTICS_update (stats, "# unsolicited message", 1, GNUNET_NO); 2545 "# unsolicited message",
2465 LOG (GNUNET_ERROR_TYPE_WARNING, "Received PID %u, (prev %u), ACK %u\n", 2546 1,
2466 pid, fc->last_pid_recv, fc->last_ack_sent); 2547 GNUNET_NO);
2548 LOG (GNUNET_ERROR_TYPE_WARNING,
2549 "Received PID %u, (prev %u), ACK %u\n",
2550 pid, fc->last_pid_recv, fc->last_ack_sent);
2467 return GNUNET_SYSERR; 2551 return GNUNET_SYSERR;
2468 } 2552 }
2469 if (GC_is_pid_bigger (pid, fc->last_pid_recv)) 2553 if (GC_is_pid_bigger (ntohl (pid.pid),
2554 ntohl (fc->last_pid_recv.pid)))
2470 { 2555 {
2471 unsigned int delta; 2556 unsigned int delta;
2472 2557
2473 delta = pid - fc->last_pid_recv; 2558 delta = ntohl (pid.pid) - ntohl (fc->last_pid_recv.pid);
2474 fc->last_pid_recv = pid; 2559 fc->last_pid_recv = pid;
2475 fc->recv_bitmap <<= delta; 2560 fc->recv_bitmap <<= delta;
2476 fc->recv_bitmap |= 1; 2561 fc->recv_bitmap |= 1;
2477 } 2562 }
2478 else 2563 else
2479 { 2564 {
2480 GNUNET_STATISTICS_update (stats, "# out of order PID", 1, GNUNET_NO); 2565 GNUNET_STATISTICS_update (stats,
2481 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv, pid, fc->recv_bitmap)) 2566 "# out of order PID",
2567 1,
2568 GNUNET_NO);
2569 if (GNUNET_NO == is_ooo_ok (fc->last_pid_recv,
2570 pid,
2571 fc->recv_bitmap))
2482 { 2572 {
2483 LOG (GNUNET_ERROR_TYPE_WARNING, "PID %u unexpected (%u+), dropping!\n", 2573 LOG (GNUNET_ERROR_TYPE_WARNING,
2484 pid, fc->last_pid_recv - 31); 2574 "PID %u unexpected (%u+), dropping!\n",
2575 ntohl (pid.pid),
2576 ntohl (fc->last_pid_recv.pid) - 31);
2485 return GNUNET_SYSERR; 2577 return GNUNET_SYSERR;
2486 } 2578 }
2487 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv, pid); 2579 fc->recv_bitmap |= get_recv_bitmask (fc->last_pid_recv,
2580 pid);
2488 } 2581 }
2489 } 2582 }
2490 2583
2491 /* Count as connection confirmation. */ 2584 /* Count as connection confirmation. */
2492 if (CADET_CONNECTION_SENT == c->state || CADET_CONNECTION_ACK == c->state) 2585 if ( (CADET_CONNECTION_SENT == c->state) ||
2586 (CADET_CONNECTION_ACK == c->state) )
2493 { 2587 {
2494 connection_change_state (c, CADET_CONNECTION_READY); 2588 connection_change_state (c, CADET_CONNECTION_READY);
2495 if (NULL != c->t) 2589 if (NULL != c->t)
@@ -2512,9 +2606,10 @@ check_message (const struct GNUNET_MessageHeader *message,
2512 */ 2606 */
2513void 2607void
2514GCC_handle_kx (struct CadetPeer *peer, 2608GCC_handle_kx (struct CadetPeer *peer,
2515 const struct GNUNET_CADET_KX *msg) 2609 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
2516{ 2610{
2517 const struct GNUNET_CADET_Hash* cid; 2611 static struct CadetEncryptedMessageIdentifier zero;
2612 const struct GNUNET_CADET_ConnectionTunnelIdentifier* cid;
2518 struct CadetConnection *c; 2613 struct CadetConnection *c;
2519 int fwd; 2614 int fwd;
2520 2615
@@ -2527,7 +2622,7 @@ GCC_handle_kx (struct CadetPeer *peer,
2527 cid, 2622 cid,
2528 c, 2623 c,
2529 peer, 2624 peer,
2530 0); 2625 zero);
2531 2626
2532 /* If something went wrong, discard message. */ 2627 /* If something went wrong, discard message. */
2533 if (GNUNET_SYSERR == fwd) 2628 if (GNUNET_SYSERR == fwd)
@@ -2555,7 +2650,8 @@ GCC_handle_kx (struct CadetPeer *peer,
2555 /* Message not for us: forward to next hop */ 2650 /* Message not for us: forward to next hop */
2556 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n"); 2651 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2557 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO); 2652 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2558 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd, 2653 (void) GCC_send_prebuilt_message (&msg->header, 0,
2654 zero, c, fwd,
2559 GNUNET_NO, NULL, NULL); 2655 GNUNET_NO, NULL, NULL);
2560 GCC_check_connections (); 2656 GCC_check_connections ();
2561} 2657}
@@ -2569,16 +2665,17 @@ GCC_handle_kx (struct CadetPeer *peer,
2569 */ 2665 */
2570void 2666void
2571GCC_handle_encrypted (struct CadetPeer *peer, 2667GCC_handle_encrypted (struct CadetPeer *peer,
2572 const struct GNUNET_CADET_Encrypted *msg) 2668 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
2573{ 2669{
2574 const struct GNUNET_CADET_Hash* cid; 2670 static struct CadetEncryptedMessageIdentifier zero;
2671 const struct GNUNET_CADET_ConnectionTunnelIdentifier* cid;
2575 struct CadetConnection *c; 2672 struct CadetConnection *c;
2576 uint32_t pid; 2673 struct CadetEncryptedMessageIdentifier pid;
2577 int fwd; 2674 int fwd;
2578 2675
2579 GCC_check_connections (); 2676 GCC_check_connections ();
2580 cid = &msg->cid; 2677 cid = &msg->cid;
2581 pid = ntohl (msg->pid); 2678 pid = msg->cemi;
2582 log_message (&msg->header, peer, cid); 2679 log_message (&msg->header, peer, cid);
2583 2680
2584 c = connection_get (cid); 2681 c = connection_get (cid);
@@ -2591,7 +2688,6 @@ GCC_handle_encrypted (struct CadetPeer *peer,
2591 /* If something went wrong, discard message. */ 2688 /* If something went wrong, discard message. */
2592 if (GNUNET_SYSERR == fwd) 2689 if (GNUNET_SYSERR == fwd)
2593 { 2690 {
2594 GNUNET_break_op (0);
2595 GCC_check_connections (); 2691 GCC_check_connections ();
2596 return; 2692 return;
2597 } 2693 }
@@ -2615,7 +2711,8 @@ GCC_handle_encrypted (struct CadetPeer *peer,
2615 /* Message not for us: forward to next hop */ 2711 /* Message not for us: forward to next hop */
2616 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n"); 2712 LOG (GNUNET_ERROR_TYPE_DEBUG, " not for us, retransmitting...\n");
2617 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO); 2713 GNUNET_STATISTICS_update (stats, "# messages forwarded", 1, GNUNET_NO);
2618 (void) GCC_send_prebuilt_message (&msg->header, 0, 0, c, fwd, 2714 (void) GCC_send_prebuilt_message (&msg->header, 0,
2715 zero, c, fwd,
2619 GNUNET_NO, NULL, NULL); 2716 GNUNET_NO, NULL, NULL);
2620 GCC_check_connections (); 2717 GCC_check_connections ();
2621} 2718}
@@ -2661,7 +2758,8 @@ GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2661 } 2758 }
2662 create_connection_time = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS, 2759 create_connection_time = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
2663 refresh_connection_time); 2760 refresh_connection_time);
2664 connections = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO); 2761 connections = GNUNET_CONTAINER_multishortmap_create (1024,
2762 GNUNET_YES);
2665} 2763}
2666 2764
2667 2765
@@ -2676,7 +2774,7 @@ GCC_init (const struct GNUNET_CONFIGURATION_Handle *c)
2676 */ 2774 */
2677static int 2775static int
2678shutdown_iterator (void *cls, 2776shutdown_iterator (void *cls,
2679 const struct GNUNET_HashCode *key, 2777 const struct GNUNET_ShortHashCode *key,
2680 void *value) 2778 void *value)
2681{ 2779{
2682 struct CadetConnection *c = value; 2780 struct CadetConnection *c = value;
@@ -2695,10 +2793,10 @@ GCC_shutdown (void)
2695{ 2793{
2696 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connections\n"); 2794 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down connections\n");
2697 GCC_check_connections (); 2795 GCC_check_connections ();
2698 GNUNET_CONTAINER_multihashmap_iterate (connections, 2796 GNUNET_CONTAINER_multishortmap_iterate (connections,
2699 &shutdown_iterator, 2797 &shutdown_iterator,
2700 NULL); 2798 NULL);
2701 GNUNET_CONTAINER_multihashmap_destroy (connections); 2799 GNUNET_CONTAINER_multishortmap_destroy (connections);
2702 connections = NULL; 2800 connections = NULL;
2703} 2801}
2704 2802
@@ -2715,7 +2813,7 @@ GCC_shutdown (void)
2715 * NULL in case of error: own id not in path, wrong neighbors, ... 2813 * NULL in case of error: own id not in path, wrong neighbors, ...
2716*/ 2814*/
2717struct CadetConnection * 2815struct CadetConnection *
2718GCC_new (const struct GNUNET_CADET_Hash *cid, 2816GCC_new (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
2719 struct CadetTunnel *t, 2817 struct CadetTunnel *t,
2720 struct CadetPeerPath *path, 2818 struct CadetPeerPath *path,
2721 unsigned int own_pos) 2819 unsigned int own_pos)
@@ -2729,9 +2827,10 @@ GCC_new (const struct GNUNET_CADET_Hash *cid,
2729 c = GNUNET_new (struct CadetConnection); 2827 c = GNUNET_new (struct CadetConnection);
2730 c->id = *cid; 2828 c->id = *cid;
2731 GNUNET_assert (GNUNET_OK == 2829 GNUNET_assert (GNUNET_OK ==
2732 GNUNET_CONTAINER_multihashmap_put (connections, 2830 GNUNET_CONTAINER_multishortmap_put (connections,
2733 GCC_get_h (c), c, 2831 &c->id.connection_of_tunnel,
2734 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 2832 c,
2833 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2735 fc_init (&c->fwd_fc); 2834 fc_init (&c->fwd_fc);
2736 fc_init (&c->bck_fc); 2835 fc_init (&c->bck_fc);
2737 c->fwd_fc.c = c; 2836 c->fwd_fc.c = c;
@@ -2832,9 +2931,9 @@ GCC_destroy (struct CadetConnection *c)
2832 if (GNUNET_NO == c->was_removed) 2931 if (GNUNET_NO == c->was_removed)
2833 { 2932 {
2834 GNUNET_break (GNUNET_YES == 2933 GNUNET_break (GNUNET_YES ==
2835 GNUNET_CONTAINER_multihashmap_remove (connections, 2934 GNUNET_CONTAINER_multishortmap_remove (connections,
2836 GCC_get_h (c), 2935 &c->id.connection_of_tunnel,
2837 c)); 2936 c));
2838 } 2937 }
2839 GNUNET_STATISTICS_update (stats, 2938 GNUNET_STATISTICS_update (stats,
2840 "# connections", 2939 "# connections",
@@ -2852,7 +2951,7 @@ GCC_destroy (struct CadetConnection *c)
2852 * 2951 *
2853 * @return ID of the connection. 2952 * @return ID of the connection.
2854 */ 2953 */
2855const struct GNUNET_CADET_Hash * 2954const struct GNUNET_CADET_ConnectionTunnelIdentifier *
2856GCC_get_id (const struct CadetConnection *c) 2955GCC_get_id (const struct CadetConnection *c)
2857{ 2956{
2858 return &c->id; 2957 return &c->id;
@@ -2860,20 +2959,6 @@ GCC_get_id (const struct CadetConnection *c)
2860 2959
2861 2960
2862/** 2961/**
2863 * Get the connection ID.
2864 *
2865 * @param c Connection to get the ID from.
2866 *
2867 * @return ID of the connection.
2868 */
2869const struct GNUNET_HashCode *
2870GCC_get_h (const struct CadetConnection *c)
2871{
2872 return GC_h2hc (&c->id);
2873}
2874
2875
2876/**
2877 * Get the connection path. 2962 * Get the connection path.
2878 * 2963 *
2879 * @param c Connection to get the path from. 2964 * @param c Connection to get the path from.
@@ -2953,12 +3038,13 @@ GCC_get_allowed (struct CadetConnection *c, int fwd)
2953 struct CadetFlowControl *fc; 3038 struct CadetFlowControl *fc;
2954 3039
2955 fc = fwd ? &c->fwd_fc : &c->bck_fc; 3040 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2956 if (CADET_CONNECTION_READY != c->state 3041 if ( (CADET_CONNECTION_READY != c->state) ||
2957 || GC_is_pid_bigger (fc->last_pid_recv, fc->last_ack_sent)) 3042 GC_is_pid_bigger (ntohl (fc->last_pid_recv.pid),
3043 ntohl (fc->last_ack_sent.pid)) )
2958 { 3044 {
2959 return 0; 3045 return 0;
2960 } 3046 }
2961 return (fc->last_ack_sent - fc->last_pid_recv); 3047 return (ntohl (fc->last_ack_sent.pid) - ntohl (fc->last_pid_recv.pid));
2962} 3048}
2963 3049
2964 3050
@@ -2986,18 +3072,17 @@ GCC_get_qn (struct CadetConnection *c, int fwd)
2986 * 3072 *
2987 * @param c Connection. 3073 * @param c Connection.
2988 * @param fwd Is query about FWD traffic? 3074 * @param fwd Is query about FWD traffic?
2989 *
2990 * @return Next PID to use. 3075 * @return Next PID to use.
2991 */ 3076 */
2992uint32_t 3077struct CadetEncryptedMessageIdentifier
2993GCC_get_pid (struct CadetConnection *c, int fwd) 3078GCC_get_pid (struct CadetConnection *c, int fwd)
2994{ 3079{
2995 struct CadetFlowControl *fc; 3080 struct CadetFlowControl *fc;
2996 uint32_t pid; 3081 struct CadetEncryptedMessageIdentifier pid;
2997 3082
2998 fc = fwd ? &c->fwd_fc : &c->bck_fc; 3083 fc = fwd ? &c->fwd_fc : &c->bck_fc;
2999 pid = fc->next_pid; 3084 pid = fc->next_pid;
3000 fc->next_pid++; 3085 fc->next_pid.pid = htonl (1 + ntohl (pid.pid));
3001 return pid; 3086 return pid;
3002} 3087}
3003 3088
@@ -3072,9 +3157,9 @@ GCC_neighbor_disconnected (struct CadetConnection *c, struct CadetPeer *peer)
3072 GNUNET_assert (GNUNET_NO == c->was_removed); 3157 GNUNET_assert (GNUNET_NO == c->was_removed);
3073 c->was_removed = GNUNET_YES; 3158 c->was_removed = GNUNET_YES;
3074 GNUNET_break (GNUNET_YES == 3159 GNUNET_break (GNUNET_YES ==
3075 GNUNET_CONTAINER_multihashmap_remove (connections, 3160 GNUNET_CONTAINER_multishortmap_remove (connections,
3076 GCC_get_h (c), 3161 &c->id.connection_of_tunnel,
3077 c)); 3162 c));
3078 /* Cancel queue in the direction that just died. */ 3163 /* Cancel queue in the direction that just died. */
3079 connection_cancel_queues (c, ! fwd); 3164 connection_cancel_queues (c, ! fwd);
3080 GCC_stop_poll (c, ! fwd); 3165 GCC_stop_poll (c, ! fwd);
@@ -3142,8 +3227,10 @@ GCC_is_sendable (struct CadetConnection *c, int fwd)
3142 fc = fwd ? &c->fwd_fc : &c->bck_fc; 3227 fc = fwd ? &c->fwd_fc : &c->bck_fc;
3143 LOG (GNUNET_ERROR_TYPE_DEBUG, 3228 LOG (GNUNET_ERROR_TYPE_DEBUG,
3144 " last ack recv: %u, last pid sent: %u\n", 3229 " last ack recv: %u, last pid sent: %u\n",
3145 fc->last_ack_recv, fc->last_pid_sent); 3230 ntohl (fc->last_ack_recv.pid),
3146 if (GC_is_pid_bigger (fc->last_ack_recv, fc->last_pid_sent)) 3231 ntohl (fc->last_pid_sent.pid));
3232 if (GC_is_pid_bigger (ntohl (fc->last_ack_recv.pid),
3233 ntohl (fc->last_pid_sent.pid)))
3147 { 3234 {
3148 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n"); 3235 LOG (GNUNET_ERROR_TYPE_DEBUG, " sendable\n");
3149 return GNUNET_YES; 3236 return GNUNET_YES;
@@ -3188,7 +3275,8 @@ GCC_is_direct (struct CadetConnection *c)
3188 */ 3275 */
3189struct CadetConnectionQueue * 3276struct CadetConnectionQueue *
3190GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message, 3277GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3191 uint16_t payload_type, uint32_t payload_id, 3278 uint16_t payload_type,
3279 struct CadetEncryptedMessageIdentifier payload_id,
3192 struct CadetConnection *c, int fwd, int force, 3280 struct CadetConnection *c, int fwd, int force,
3193 GCC_sent cont, void *cont_cls) 3281 GCC_sent cont, void *cont_cls)
3194{ 3282{
@@ -3214,27 +3302,30 @@ GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3214 GC_f2s(fwd), size); 3302 GC_f2s(fwd), size);
3215 switch (type) 3303 switch (type)
3216 { 3304 {
3217 case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED: 3305 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED:
3218 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u, PIDsnt: %u, ACKrcv: %u\n", 3306 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u, PIDsnt: %u, ACKrcv: %u\n",
3219 fc, fc->queue_n, fc->last_pid_sent, fc->last_ack_recv); 3307 fc,
3308 fc->queue_n,
3309 ntohl (fc->last_pid_sent.pid),
3310 ntohl (fc->last_ack_recv.pid));
3220 if (GNUNET_NO == force) 3311 if (GNUNET_NO == force)
3221 { 3312 {
3222 fc->queue_n++; 3313 fc->queue_n++;
3223 } 3314 }
3224 break; 3315 break;
3225 3316
3226 case GNUNET_MESSAGE_TYPE_CADET_KX: 3317 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX:
3227 /* nothing to do here */ 3318 /* nothing to do here */
3228 break; 3319 break;
3229 3320
3230 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE: 3321 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
3231 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK: 3322 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK:
3232 /* Should've only be used for restransmissions. */ 3323 /* Should've only be used for restransmissions. */
3233 GNUNET_break (0 == payload_type); 3324 GNUNET_break (0 == payload_type);
3234 break; 3325 break;
3235 3326
3236 case GNUNET_MESSAGE_TYPE_CADET_ACK: 3327 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK:
3237 case GNUNET_MESSAGE_TYPE_CADET_POLL: 3328 case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL:
3238 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY: 3329 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3239 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN: 3330 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3240 GNUNET_assert (GNUNET_YES == force); 3331 GNUNET_assert (GNUNET_YES == force);
@@ -3252,7 +3343,7 @@ GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3252 GNUNET_break (0); 3343 GNUNET_break (0);
3253 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n", 3344 LOG (GNUNET_ERROR_TYPE_DEBUG, "queue full: %u/%u\n",
3254 fc->queue_n, fc->queue_max); 3345 fc->queue_n, fc->queue_max);
3255 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == type) 3346 if (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED == type)
3256 { 3347 {
3257 fc->queue_n--; 3348 fc->queue_n--;
3258 } 3349 }
@@ -3264,21 +3355,25 @@ GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3264 c->pending_messages++; 3355 c->pending_messages++;
3265 3356
3266 q = GNUNET_new (struct CadetConnectionQueue); 3357 q = GNUNET_new (struct CadetConnectionQueue);
3358 q->cont = cont;
3359 q->cont_cls = cont_cls;
3267 q->forced = force; 3360 q->forced = force;
3268 q->peer_q = GCP_send (get_hop (c, fwd), message, 3361 GNUNET_CONTAINER_DLL_insert (fc->q_head, fc->q_tail, q);
3269 payload_type, payload_id, 3362 q->peer_q = GCP_send (get_hop (c, fwd),
3270 c, fwd, 3363 message,
3364 payload_type,
3365 payload_id,
3366 c,
3367 fwd,
3271 &conn_message_sent, q); 3368 &conn_message_sent, q);
3272 if (NULL == q->peer_q) 3369 if (NULL == q->peer_q)
3273 { 3370 {
3274 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c)); 3371 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3372 GNUNET_CONTAINER_DLL_remove (fc->q_head, fc->q_tail, q);
3275 GNUNET_free (q); 3373 GNUNET_free (q);
3276 GCC_check_connections (); 3374 GCC_check_connections ();
3277 return NULL; 3375 return NULL;
3278 } 3376 }
3279 q->cont = cont;
3280 q->cont_cls = cont_cls;
3281 GNUNET_CONTAINER_DLL_insert (fc->q_head, fc->q_tail, q);
3282 GCC_check_connections (); 3377 GCC_check_connections ();
3283 return q; 3378 return q;
3284} 3379}
@@ -3313,19 +3408,21 @@ GCC_cancel (struct CadetConnectionQueue *q)
3313void 3408void
3314GCC_send_create (struct CadetConnection *c) 3409GCC_send_create (struct CadetConnection *c)
3315{ 3410{
3411 static struct CadetEncryptedMessageIdentifier zero;
3316 enum CadetTunnelCState state; 3412 enum CadetTunnelCState state;
3317 size_t size; 3413 size_t size;
3318 3414
3319 GCC_check_connections (); 3415 GCC_check_connections ();
3320 size = sizeof (struct GNUNET_CADET_ConnectionCreate); 3416 size = sizeof (struct GNUNET_CADET_ConnectionCreateMessage);
3321 size += c->path->length * sizeof (struct GNUNET_PeerIdentity); 3417 size += c->path->length * sizeof (struct GNUNET_PeerIdentity);
3322 { 3418 {
3323 /* Allocate message on the stack */ 3419 /* Allocate message on the stack */
3324 unsigned char cbuf[size]; 3420 unsigned char cbuf[size];
3325 struct GNUNET_CADET_ConnectionCreate *msg; 3421 struct GNUNET_CADET_ConnectionCreateMessage *msg;
3326 struct GNUNET_PeerIdentity *peers; 3422 struct GNUNET_PeerIdentity *peers;
3327 3423
3328 msg = (struct GNUNET_CADET_ConnectionCreate *) cbuf; 3424
3425 msg = (struct GNUNET_CADET_ConnectionCreateMessage *) cbuf;
3329 msg->header.size = htons (size); 3426 msg->header.size = htons (size);
3330 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE); 3427 msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE);
3331 msg->reserved = htonl (0); 3428 msg->reserved = htonl (0);
@@ -3338,7 +3435,8 @@ GCC_send_create (struct CadetConnection *c)
3338 GNUNET_assert (NULL == c->maintenance_q); 3435 GNUNET_assert (NULL == c->maintenance_q);
3339 c->maintenance_q = GCP_send (get_next_hop (c), 3436 c->maintenance_q = GCP_send (get_next_hop (c),
3340 &msg->header, 3437 &msg->header,
3341 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 0, 3438 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE,
3439 zero,
3342 c, GNUNET_YES, 3440 c, GNUNET_YES,
3343 &conn_message_sent, NULL); 3441 &conn_message_sent, NULL);
3344 } 3442 }
@@ -3435,7 +3533,8 @@ GCC_send_ack (struct CadetConnection *c, int fwd, int force)
3435void 3533void
3436GCC_send_destroy (struct CadetConnection *c) 3534GCC_send_destroy (struct CadetConnection *c)
3437{ 3535{
3438 struct GNUNET_CADET_ConnectionDestroy msg; 3536 static struct CadetEncryptedMessageIdentifier zero;
3537 struct GNUNET_CADET_ConnectionDestroyMessage msg;
3439 3538
3440 if (GNUNET_YES == c->destroy) 3539 if (GNUNET_YES == c->destroy)
3441 return; 3540 return;
@@ -3449,10 +3548,16 @@ GCC_send_destroy (struct CadetConnection *c)
3449 GCC_2s (c)); 3548 GCC_2s (c));
3450 3549
3451 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES)) 3550 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_YES))
3452 (void) GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c, 3551 (void) GCC_send_prebuilt_message (&msg.header,
3552 UINT16_MAX,
3553 zero,
3554 c,
3453 GNUNET_YES, GNUNET_YES, NULL, NULL); 3555 GNUNET_YES, GNUNET_YES, NULL, NULL);
3454 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO)) 3556 if (GNUNET_NO == GCC_is_terminal (c, GNUNET_NO))
3455 (void) GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, c, 3557 (void) GCC_send_prebuilt_message (&msg.header,
3558 UINT16_MAX,
3559 zero,
3560 c,
3456 GNUNET_NO, GNUNET_YES, NULL, NULL); 3561 GNUNET_NO, GNUNET_YES, NULL, NULL);
3457 mark_destroyed (c); 3562 mark_destroyed (c);
3458 GCC_check_connections (); 3563 GCC_check_connections ();
@@ -3538,10 +3643,11 @@ GCC_2s (const struct CadetConnection *c)
3538 static char buf[128]; 3643 static char buf[128];
3539 3644
3540 SPRINTF (buf, "%s (->%s)", 3645 SPRINTF (buf, "%s (->%s)",
3541 GNUNET_h2s (GC_h2hc (GCC_get_id (c))), GCT_2s (c->t)); 3646 GNUNET_sh2s (&GCC_get_id (c)->connection_of_tunnel),
3647 GCT_2s (c->t));
3542 return buf; 3648 return buf;
3543 } 3649 }
3544 return GNUNET_h2s (GC_h2hc (&c->id)); 3650 return GNUNET_sh2s (&c->id.connection_of_tunnel);
3545} 3651}
3546 3652
3547 3653
@@ -3582,9 +3688,11 @@ GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3582 LOG2 (level, "CCC FWD flow control:\n"); 3688 LOG2 (level, "CCC FWD flow control:\n");
3583 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max); 3689 LOG2 (level, "CCC queue: %u/%u\n", c->fwd_fc.queue_n, c->fwd_fc.queue_max);
3584 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n", 3690 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3585 c->fwd_fc.last_pid_sent, c->fwd_fc.last_pid_recv); 3691 ntohl (c->fwd_fc.last_pid_sent.pid),
3692 ntohl (c->fwd_fc.last_pid_recv.pid));
3586 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n", 3693 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3587 c->fwd_fc.last_ack_sent, c->fwd_fc.last_ack_recv); 3694 ntohl (c->fwd_fc.last_ack_sent.pid),
3695 ntohl (c->fwd_fc.last_ack_recv.pid));
3588 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap); 3696 LOG2 (level, "CCC recv PID bitmap: %X\n", c->fwd_fc.recv_bitmap);
3589 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n", 3697 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3590 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg); 3698 c->fwd_fc.poll_task, c->fwd_fc.poll_msg, c->fwd_fc.ack_msg);
@@ -3592,9 +3700,11 @@ GCC_debug (const struct CadetConnection *c, enum GNUNET_ErrorType level)
3592 LOG2 (level, "CCC BCK flow control:\n"); 3700 LOG2 (level, "CCC BCK flow control:\n");
3593 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max); 3701 LOG2 (level, "CCC queue: %u/%u\n", c->bck_fc.queue_n, c->bck_fc.queue_max);
3594 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n", 3702 LOG2 (level, "CCC last PID sent: %5u, recv: %5u\n",
3595 c->bck_fc.last_pid_sent, c->bck_fc.last_pid_recv); 3703 ntohl (c->bck_fc.last_pid_sent.pid),
3704 ntohl (c->bck_fc.last_pid_recv.pid));
3596 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n", 3705 LOG2 (level, "CCC last ACK sent: %5u, recv: %5u\n",
3597 c->bck_fc.last_ack_sent, c->bck_fc.last_ack_recv); 3706 ntohl (c->bck_fc.last_ack_sent.pid),
3707 ntohl (c->bck_fc.last_ack_recv.pid));
3598 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap); 3708 LOG2 (level, "CCC recv PID bitmap: %X\n", c->bck_fc.recv_bitmap);
3599 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n", 3709 LOG2 (level, "CCC poll: task %d, msg %p, msg_ack %p)\n",
3600 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg); 3710 c->bck_fc.poll_task, c->bck_fc.poll_msg, c->bck_fc.ack_msg);
diff --git a/src/cadet/gnunet-service-cadet_connection.h b/src/cadet/gnunet-service-cadet_connection.h
index 18f33ce7c..1c3160dfd 100644
--- a/src/cadet/gnunet-service-cadet_connection.h
+++ b/src/cadet/gnunet-service-cadet_connection.h
@@ -125,7 +125,7 @@ typedef void
125 */ 125 */
126void 126void
127GCC_handle_create (struct CadetPeer *peer, 127GCC_handle_create (struct CadetPeer *peer,
128 const struct GNUNET_CADET_ConnectionCreate *msg); 128 const struct GNUNET_CADET_ConnectionCreateMessage *msg);
129 129
130 130
131/** 131/**
@@ -136,7 +136,7 @@ GCC_handle_create (struct CadetPeer *peer,
136 */ 136 */
137void 137void
138GCC_handle_confirm (struct CadetPeer *peer, 138GCC_handle_confirm (struct CadetPeer *peer,
139 const struct GNUNET_CADET_ConnectionACK *msg); 139 const struct GNUNET_CADET_ConnectionCreateMessageAckMessage *msg);
140 140
141 141
142/** 142/**
@@ -147,7 +147,7 @@ GCC_handle_confirm (struct CadetPeer *peer,
147 */ 147 */
148void 148void
149GCC_handle_broken (struct CadetPeer *peer, 149GCC_handle_broken (struct CadetPeer *peer,
150 const struct GNUNET_CADET_ConnectionBroken *msg); 150 const struct GNUNET_CADET_ConnectionBrokenMessage *msg);
151 151
152/** 152/**
153 * Handler for notifications of destroyed connections. 153 * Handler for notifications of destroyed connections.
@@ -157,7 +157,7 @@ GCC_handle_broken (struct CadetPeer *peer,
157 */ 157 */
158void 158void
159GCC_handle_destroy (struct CadetPeer *peer, 159GCC_handle_destroy (struct CadetPeer *peer,
160 const struct GNUNET_CADET_ConnectionDestroy *msg); 160 const struct GNUNET_CADET_ConnectionDestroyMessage *msg);
161 161
162/** 162/**
163 * Handler for cadet network traffic hop-by-hop acks. 163 * Handler for cadet network traffic hop-by-hop acks.
@@ -167,7 +167,7 @@ GCC_handle_destroy (struct CadetPeer *peer,
167 */ 167 */
168void 168void
169GCC_handle_ack (struct CadetPeer *peer, 169GCC_handle_ack (struct CadetPeer *peer,
170 const struct GNUNET_CADET_ACK *msg); 170 const struct GNUNET_CADET_ConnectionEncryptedAckMessage *msg);
171 171
172/** 172/**
173 * Handler for cadet network traffic hop-by-hop data counter polls. 173 * Handler for cadet network traffic hop-by-hop data counter polls.
@@ -177,7 +177,7 @@ GCC_handle_ack (struct CadetPeer *peer,
177 */ 177 */
178void 178void
179GCC_handle_poll (struct CadetPeer *peer, 179GCC_handle_poll (struct CadetPeer *peer,
180 const struct GNUNET_CADET_Poll *msg); 180 const struct GNUNET_CADET_ConnectionHopByHopPollMessage *msg);
181 181
182/** 182/**
183 * Handler for key exchange traffic (Axolotl KX). 183 * Handler for key exchange traffic (Axolotl KX).
@@ -187,7 +187,7 @@ GCC_handle_poll (struct CadetPeer *peer,
187 */ 187 */
188void 188void
189GCC_handle_kx (struct CadetPeer *peer, 189GCC_handle_kx (struct CadetPeer *peer,
190 const struct GNUNET_CADET_KX *msg); 190 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg);
191 191
192/** 192/**
193 * Handler for encrypted cadet network traffic (channel mgmt, data). 193 * Handler for encrypted cadet network traffic (channel mgmt, data).
@@ -197,7 +197,7 @@ GCC_handle_kx (struct CadetPeer *peer,
197 */ 197 */
198void 198void
199GCC_handle_encrypted (struct CadetPeer *peer, 199GCC_handle_encrypted (struct CadetPeer *peer,
200 const struct GNUNET_CADET_Encrypted *msg); 200 const struct GNUNET_CADET_TunnelEncryptedMessage *msg);
201 201
202/** 202/**
203 * Core handler for axolotl key exchange traffic. 203 * Core handler for axolotl key exchange traffic.
@@ -277,7 +277,7 @@ GCC_shutdown (void);
277 * NULL in case of error: own id not in path, wrong neighbors, ... 277 * NULL in case of error: own id not in path, wrong neighbors, ...
278 */ 278 */
279struct CadetConnection * 279struct CadetConnection *
280GCC_new (const struct GNUNET_CADET_Hash *cid, 280GCC_new (const struct GNUNET_CADET_ConnectionTunnelIdentifier *cid,
281 struct CadetTunnel *t, 281 struct CadetTunnel *t,
282 struct CadetPeerPath *path, 282 struct CadetPeerPath *path,
283 unsigned int own_pos); 283 unsigned int own_pos);
@@ -300,22 +300,11 @@ GCC_destroy (struct CadetConnection *c);
300 * 300 *
301 * @return ID of the connection. 301 * @return ID of the connection.
302 */ 302 */
303const struct GNUNET_CADET_Hash * 303const struct GNUNET_CADET_ConnectionTunnelIdentifier *
304GCC_get_id (const struct CadetConnection *c); 304GCC_get_id (const struct CadetConnection *c);
305 305
306 306
307/** 307/**
308 * Get a hash for the connection ID.
309 *
310 * @param c Connection to get the hash.
311 *
312 * @return Hash expanded from the ID of the connection.
313 */
314const struct GNUNET_HashCode *
315GCC_get_h (const struct CadetConnection *c);
316
317
318/**
319 * Get the connection path. 308 * Get the connection path.
320 * 309 *
321 * @param c Connection to get the path from. 310 * @param c Connection to get the path from.
@@ -383,10 +372,9 @@ GCC_get_qn (struct CadetConnection *c, int fwd);
383 * 372 *
384 * @param c Connection. 373 * @param c Connection.
385 * @param fwd Is query about FWD traffic? 374 * @param fwd Is query about FWD traffic?
386 *
387 * @return Next PID to use. 375 * @return Next PID to use.
388 */ 376 */
389uint32_t 377struct CadetEncryptedMessageIdentifier
390GCC_get_pid (struct CadetConnection *c, int fwd); 378GCC_get_pid (struct CadetConnection *c, int fwd);
391 379
392/** 380/**
@@ -508,7 +496,8 @@ GCC_cancel (struct CadetConnectionQueue *q);
508 */ 496 */
509struct CadetConnectionQueue * 497struct CadetConnectionQueue *
510GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message, 498GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
511 uint16_t payload_type, uint32_t payload_id, 499 uint16_t payload_type,
500 struct CadetEncryptedMessageIdentifier payload_id,
512 struct CadetConnection *c, int fwd, int force, 501 struct CadetConnection *c, int fwd, int force,
513 GCC_sent cont, void *cont_cls); 502 GCC_sent cont, void *cont_cls);
514 503
diff --git a/src/cadet/gnunet-service-cadet_dht.c b/src/cadet/gnunet-service-cadet_dht.c
index 9b11ebf18..22673b167 100644
--- a/src/cadet/gnunet-service-cadet_dht.c
+++ b/src/cadet/gnunet-service-cadet_dht.c
@@ -42,16 +42,24 @@
42 */ 42 */
43struct GCD_search_handle 43struct GCD_search_handle
44{ 44{
45 /** DHT_GET handle. */ 45 /**
46 * DHT_GET handle.
47 */
46 struct GNUNET_DHT_GetHandle *dhtget; 48 struct GNUNET_DHT_GetHandle *dhtget;
47 49
48 /** Provided callback to call when a path is found. */ 50 /**
51 * Provided callback to call when a path is found.
52 */
49 GCD_search_callback callback; 53 GCD_search_callback callback;
50 54
51 /** Provided closure. */ 55 /**
56 * Provided closure.
57 */
52 void *cls; 58 void *cls;
53 59
54 /** Peer ID searched for */ 60 /**
61 * Peer ID searched for
62 */
55 GNUNET_PEER_Id peer_id; 63 GNUNET_PEER_Id peer_id;
56}; 64};
57 65
@@ -224,13 +232,16 @@ announce_id (void *cls)
224 announce_id_task = NULL; 232 announce_id_task = NULL;
225 LOG (GNUNET_ERROR_TYPE_DEBUG, "Announce ID\n"); 233 LOG (GNUNET_ERROR_TYPE_DEBUG, "Announce ID\n");
226 hello = GCH_get_mine (); 234 hello = GCH_get_mine ();
227 size = NULL != hello ? GNUNET_HELLO_size (hello) : 0; 235 size = (NULL != hello) ? GNUNET_HELLO_size (hello) : 0;
228 if (NULL == hello || 0 == size) 236 if ( (NULL == hello) || (0 == size) )
229 { 237 {
230 /* Peerinfo gave us no hello yet, try again soon. */ 238 /* Peerinfo gave us no hello yet, try again soon. */
231 LOG (GNUNET_ERROR_TYPE_INFO, " no hello, waiting!\n"); 239 LOG (GNUNET_ERROR_TYPE_INFO,
232 GNUNET_STATISTICS_update (stats, "# DHT announce skipped (no hello)", 240 " no hello, waiting!\n");
233 1, GNUNET_NO); 241 GNUNET_STATISTICS_update (stats,
242 "# DHT announce skipped (no hello)",
243 1,
244 GNUNET_NO);
234 expiration = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), 245 expiration = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
235 announce_delay); 246 announce_delay);
236 announce_delay = GNUNET_TIME_STD_BACKOFF (announce_delay); 247 announce_delay = GNUNET_TIME_STD_BACKOFF (announce_delay);
@@ -241,29 +252,43 @@ announce_id (void *cls)
241 announce_delay = GNUNET_TIME_UNIT_SECONDS; 252 announce_delay = GNUNET_TIME_UNIT_SECONDS;
242 } 253 }
243 254
244 LOG (GNUNET_ERROR_TYPE_DEBUG, "Hello %p size: %u\n", hello, size); 255 LOG (GNUNET_ERROR_TYPE_DEBUG,
245 GNUNET_STATISTICS_update (stats, "# DHT announce", 256 "Hello %p size: %u\n",
246 1, GNUNET_NO); 257 hello,
247 memset (&phash, 0, sizeof (phash)); 258 size);
248 GNUNET_memcpy (&phash, &my_full_id, sizeof (my_full_id)); 259 if (NULL != hello)
249 GNUNET_DHT_put (dht_handle, /* DHT handle */ 260 {
250 &phash, /* Key to use */ 261 GNUNET_STATISTICS_update (stats,
251 dht_replication_level, /* Replication level */ 262 "# DHT announce",
252 GNUNET_DHT_RO_RECORD_ROUTE 263 1, GNUNET_NO);
253 | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */ 264 memset (&phash,
254 GNUNET_BLOCK_TYPE_DHT_HELLO, /* Block type */ 265 0,
255 size, /* Size of the data */ 266 sizeof (phash));
256 (const char *) hello, /* Data itself */ 267 GNUNET_memcpy (&phash,
257 expiration, /* Data expiration */ 268 &my_full_id,
258 NULL, /* Continuation */ 269 sizeof (my_full_id));
259 NULL); /* Continuation closure */ 270 GNUNET_DHT_put (dht_handle, /* DHT handle */
260 271 &phash, /* Key to use */
272 dht_replication_level, /* Replication level */
273 GNUNET_DHT_RO_RECORD_ROUTE
274 | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
275 GNUNET_BLOCK_TYPE_DHT_HELLO, /* Block type */
276 size, /* Size of the data */
277 (const char *) hello, /* Data itself */
278 expiration, /* Data expiration */
279 NULL, /* Continuation */
280 NULL); /* Continuation closure */
281 }
261 /* Call again in id_announce_time, unless HELLO expires first, 282 /* Call again in id_announce_time, unless HELLO expires first,
262 * but wait at least 1s. */ 283 * but wait at least 1s. */
263 next_put = GNUNET_TIME_absolute_get_remaining (expiration); 284 next_put = GNUNET_TIME_absolute_get_remaining (expiration);
264 next_put = GNUNET_TIME_relative_min (next_put, id_announce_time); 285 next_put = GNUNET_TIME_relative_min (next_put,
265 next_put = GNUNET_TIME_relative_max (next_put, GNUNET_TIME_UNIT_SECONDS); 286 id_announce_time);
266 announce_id_task = GNUNET_SCHEDULER_add_delayed (next_put, &announce_id, cls); 287 next_put = GNUNET_TIME_relative_max (next_put,
288 GNUNET_TIME_UNIT_SECONDS);
289 announce_id_task = GNUNET_SCHEDULER_add_delayed (next_put,
290 &announce_id,
291 cls);
267} 292}
268 293
269/** 294/**
@@ -378,8 +403,11 @@ GCD_search (const struct GNUNET_PeerIdentity *peer_id,
378 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, 403 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
379 NULL, /* xquery */ 404 NULL, /* xquery */
380 0, /* xquery bits */ 405 0, /* xquery bits */
381 &dht_get_id_handler, h); 406 &dht_get_id_handler,
382 GNUNET_CONTAINER_multihashmap32_put (get_requests, h->peer_id, h, 407 h);
408 GNUNET_CONTAINER_multihashmap32_put (get_requests,
409 h->peer_id,
410 h,
383 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); 411 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
384 return h; 412 return h;
385} 413}
diff --git a/src/cadet/gnunet-service-cadet_hello.h b/src/cadet/gnunet-service-cadet_hello.h
index 5f535b496..34121e1e0 100644
--- a/src/cadet/gnunet-service-cadet_hello.h
+++ b/src/cadet/gnunet-service-cadet_hello.h
@@ -50,11 +50,13 @@ extern "C"
50void 50void
51GCH_init (const struct GNUNET_CONFIGURATION_Handle *c); 51GCH_init (const struct GNUNET_CONFIGURATION_Handle *c);
52 52
53
53/** 54/**
54 * Shut down the hello subsystem. 55 * Shut down the hello subsystem.
55 */ 56 */
56void 57void
57GCH_shutdown (); 58GCH_shutdown (void);
59
58 60
59/** 61/**
60 * Get own hello message. 62 * Get own hello message.
@@ -64,6 +66,7 @@ GCH_shutdown ();
64const struct GNUNET_HELLO_Message * 66const struct GNUNET_HELLO_Message *
65GCH_get_mine (void); 67GCH_get_mine (void);
66 68
69
67#if 0 /* keep Emacsens' auto-indent happy */ 70#if 0 /* keep Emacsens' auto-indent happy */
68{ 71{
69#endif 72#endif
diff --git a/src/cadet/gnunet-service-cadet_local.c b/src/cadet/gnunet-service-cadet_local.c
index 9be1224c1..e1f6ac4c3 100644
--- a/src/cadet/gnunet-service-cadet_local.c
+++ b/src/cadet/gnunet-service-cadet_local.c
@@ -61,7 +61,7 @@ struct CadetClient
61 * Tunnels that belong to this client, indexed by local id 61 * Tunnels that belong to this client, indexed by local id
62 */ 62 */
63 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels; 63 struct GNUNET_CONTAINER_MultiHashMap32 *own_channels;
64 64
65 /** 65 /**
66 * Tunnels this client has accepted, indexed by incoming local id 66 * Tunnels this client has accepted, indexed by incoming local id
67 */ 67 */
@@ -70,7 +70,7 @@ struct CadetClient
70 /** 70 /**
71 * Channel ID for the next incoming channel. 71 * Channel ID for the next incoming channel.
72 */ 72 */
73 CADET_ChannelNumber next_chid; 73 struct GNUNET_CADET_ClientChannelNumber next_chid;
74 74
75 /** 75 /**
76 * Handle to communicate with the client 76 * Handle to communicate with the client
@@ -188,7 +188,9 @@ channel_destroy_iterator (void *cls,
188 " Channel %s destroy, due to client %s shutdown.\n", 188 " Channel %s destroy, due to client %s shutdown.\n",
189 GCCH_2s (ch), GML_2s (c)); 189 GCCH_2s (ch), GML_2s (c));
190 190
191 GCCH_handle_local_destroy (ch, c, key < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV); 191 GCCH_handle_local_destroy (ch,
192 c,
193 key < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
192 return GNUNET_OK; 194 return GNUNET_OK;
193} 195}
194 196
@@ -247,7 +249,6 @@ client_new (struct GNUNET_SERVER_Client *client)
247 c = GNUNET_new (struct CadetClient); 249 c = GNUNET_new (struct CadetClient);
248 c->handle = client; 250 c->handle = client;
249 c->id = next_client_id++; /* overflow not important: just for debug */ 251 c->id = next_client_id++; /* overflow not important: just for debug */
250 c->next_chid = GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
251 252
252 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32); 253 c->own_channels = GNUNET_CONTAINER_multihashmap32_create (32);
253 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32); 254 c->incoming_channels = GNUNET_CONTAINER_multihashmap32_create (32);
@@ -439,7 +440,7 @@ handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
439 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); 440 LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
440 441
441 /* Message size sanity check */ 442 /* Message size sanity check */
442 if (sizeof (struct GNUNET_CADET_ChannelCreateMessage) 443 if (sizeof (struct GNUNET_CADET_ChannelOpenMessageMessage)
443 != ntohs (message->size)) 444 != ntohs (message->size))
444 { 445 {
445 GNUNET_break (0); 446 GNUNET_break (0);
@@ -449,7 +450,7 @@ handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client,
449 450
450 if (GNUNET_OK != 451 if (GNUNET_OK !=
451 GCCH_handle_local_create (c, 452 GCCH_handle_local_create (c,
452 (struct GNUNET_CADET_ChannelCreateMessage *) 453 (struct GNUNET_CADET_ChannelOpenMessageMessage *)
453 message)) 454 message))
454 { 455 {
455 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); 456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
@@ -474,7 +475,7 @@ handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
474 struct GNUNET_CADET_ChannelDestroyMessage *msg; 475 struct GNUNET_CADET_ChannelDestroyMessage *msg;
475 struct CadetClient *c; 476 struct CadetClient *c;
476 struct CadetChannel *ch; 477 struct CadetChannel *ch;
477 CADET_ChannelNumber chid; 478 struct GNUNET_CADET_ClientChannelNumber chid;
478 479
479 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a DESTROY CHANNEL from client!\n"); 480 LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a DESTROY CHANNEL from client!\n");
480 481
@@ -499,7 +500,7 @@ handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
499 msg = (struct GNUNET_CADET_ChannelDestroyMessage *) message; 500 msg = (struct GNUNET_CADET_ChannelDestroyMessage *) message;
500 501
501 /* Retrieve tunnel */ 502 /* Retrieve tunnel */
502 chid = ntohl (msg->channel_id); 503 chid = msg->channel_id;
503 ch = GML_channel_get (c, chid); 504 ch = GML_channel_get (c, chid);
504 505
505 LOG (GNUNET_ERROR_TYPE_INFO, "Client %u is destroying channel %X\n", 506 LOG (GNUNET_ERROR_TYPE_INFO, "Client %u is destroying channel %X\n",
@@ -515,7 +516,9 @@ handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
515 return; 516 return;
516 } 517 }
517 518
518 GCCH_handle_local_destroy (ch, c, chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV); 519 GCCH_handle_local_destroy (ch,
520 c,
521 ntohl (chid.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
519 522
520 GNUNET_SERVER_receive_done (client, GNUNET_OK); 523 GNUNET_SERVER_receive_done (client, GNUNET_OK);
521 return; 524 return;
@@ -537,7 +540,7 @@ handle_data (void *cls, struct GNUNET_SERVER_Client *client,
537 struct GNUNET_CADET_LocalData *msg; 540 struct GNUNET_CADET_LocalData *msg;
538 struct CadetClient *c; 541 struct CadetClient *c;
539 struct CadetChannel *ch; 542 struct CadetChannel *ch;
540 CADET_ChannelNumber chid; 543 struct GNUNET_CADET_ClientChannelNumber chid;
541 size_t message_size; 544 size_t message_size;
542 size_t payload_size; 545 size_t payload_size;
543 size_t payload_claimed_size; 546 size_t payload_claimed_size;
@@ -583,12 +586,12 @@ handle_data (void *cls, struct GNUNET_SERVER_Client *client,
583 return; 586 return;
584 } 587 }
585 588
586 chid = ntohl (msg->id); 589 chid = msg->id;
587 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u bytes (%u payload) by client %u\n", 590 LOG (GNUNET_ERROR_TYPE_DEBUG, " %u bytes (%u payload) by client %u\n",
588 payload_size, payload_claimed_size, c->id); 591 payload_size, payload_claimed_size, c->id);
589 592
590 /* Channel exists? */ 593 /* Channel exists? */
591 fwd = chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV; 594 fwd = ntohl (chid.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
592 ch = GML_channel_get (c, chid); 595 ch = GML_channel_get (c, chid);
593 if (NULL == ch) 596 if (NULL == ch)
594 { 597 {
@@ -626,7 +629,7 @@ handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
626 struct GNUNET_CADET_LocalAck *msg; 629 struct GNUNET_CADET_LocalAck *msg;
627 struct CadetChannel *ch; 630 struct CadetChannel *ch;
628 struct CadetClient *c; 631 struct CadetClient *c;
629 CADET_ChannelNumber chid; 632 struct GNUNET_CADET_ClientChannelNumber chid;
630 int fwd; 633 int fwd;
631 634
632 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n"); 635 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
@@ -644,13 +647,16 @@ handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
644 msg = (struct GNUNET_CADET_LocalAck *) message; 647 msg = (struct GNUNET_CADET_LocalAck *) message;
645 648
646 /* Channel exists? */ 649 /* Channel exists? */
647 chid = ntohl (msg->channel_id); 650 chid = msg->channel_id;
648 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", chid); 651 LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n",
652 ntohl (chid.channel_of_client));
649 ch = GML_channel_get (c, chid); 653 ch = GML_channel_get (c, chid);
650 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch); 654 LOG (GNUNET_ERROR_TYPE_DEBUG, " -- ch %p\n", ch);
651 if (NULL == ch) 655 if (NULL == ch)
652 { 656 {
653 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %X unknown.\n", chid); 657 LOG (GNUNET_ERROR_TYPE_DEBUG,
658 "Channel %X unknown.\n",
659 ntohl (chid.channel_of_client));
654 LOG (GNUNET_ERROR_TYPE_DEBUG, " for client %u.\n", c->id); 660 LOG (GNUNET_ERROR_TYPE_DEBUG, " for client %u.\n", c->id);
655 GNUNET_STATISTICS_update (stats, 661 GNUNET_STATISTICS_update (stats,
656 "# client ack messages on unknown channel", 662 "# client ack messages on unknown channel",
@@ -661,12 +667,10 @@ handle_ack (void *cls, struct GNUNET_SERVER_Client *client,
661 667
662 /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */ 668 /* If client is root, the ACK is going FWD, therefore this is "BCK ACK". */
663 /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */ 669 /* If client is dest, the ACK is going BCK, therefore this is "FWD ACK" */
664 fwd = chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV; 670 fwd = ntohl (chid.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
665 671
666 GCCH_handle_local_ack (ch, fwd); 672 GCCH_handle_local_ack (ch, fwd);
667 GNUNET_SERVER_receive_done (client, GNUNET_OK); 673 GNUNET_SERVER_receive_done (client, GNUNET_OK);
668
669 return;
670} 674}
671 675
672 676
@@ -961,8 +965,9 @@ static void
961iter_connection (void *cls, struct CadetConnection *c) 965iter_connection (void *cls, struct CadetConnection *c)
962{ 966{
963 struct GNUNET_CADET_LocalInfoTunnel *msg = cls; 967 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
964 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1]; 968 struct GNUNET_CADET_ConnectionTunnelIdentifier *h;
965 969
970 h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
966 h[msg->connections] = *(GCC_get_id (c)); 971 h[msg->connections] = *(GCC_get_id (c));
967 msg->connections++; 972 msg->connections++;
968} 973}
@@ -971,10 +976,10 @@ static void
971iter_channel (void *cls, struct CadetChannel *ch) 976iter_channel (void *cls, struct CadetChannel *ch)
972{ 977{
973 struct GNUNET_CADET_LocalInfoTunnel *msg = cls; 978 struct GNUNET_CADET_LocalInfoTunnel *msg = cls;
974 struct GNUNET_CADET_Hash *h = (struct GNUNET_CADET_Hash *) &msg[1]; 979 struct GNUNET_CADET_ConnectionTunnelIdentifier *h = (struct GNUNET_CADET_ConnectionTunnelIdentifier *) &msg[1];
975 CADET_ChannelNumber *chn = (CADET_ChannelNumber *) &h[msg->connections]; 980 struct GNUNET_CADET_ChannelTunnelNumber *chn = (struct GNUNET_CADET_ChannelTunnelNumber *) &h[msg->connections];
976 981
977 chn[msg->channels] = htonl (GCCH_get_id (ch)); 982 chn[msg->channels] = GCCH_get_id (ch);
978 msg->channels++; 983 msg->channels++;
979} 984}
980 985
@@ -1039,8 +1044,8 @@ handle_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
1039 c_n = GCT_count_any_connections (t); 1044 c_n = GCT_count_any_connections (t);
1040 1045
1041 size = sizeof (struct GNUNET_CADET_LocalInfoTunnel); 1046 size = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
1042 size += c_n * sizeof (struct GNUNET_CADET_Hash); 1047 size += c_n * sizeof (struct GNUNET_CADET_ConnectionTunnelIdentifier);
1043 size += ch_n * sizeof (CADET_ChannelNumber); 1048 size += ch_n * sizeof (struct GNUNET_CADET_ChannelTunnelNumber);
1044 1049
1045 resp = GNUNET_malloc (size); 1050 resp = GNUNET_malloc (size);
1046 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL); 1051 resp->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
@@ -1123,8 +1128,8 @@ static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
1123 sizeof (struct GNUNET_CADET_PortMessage)}, 1128 sizeof (struct GNUNET_CADET_PortMessage)},
1124 {&handle_port_close, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE, 1129 {&handle_port_close, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE,
1125 sizeof (struct GNUNET_CADET_PortMessage)}, 1130 sizeof (struct GNUNET_CADET_PortMessage)},
1126 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE, 1131 {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN,
1127 sizeof (struct GNUNET_CADET_ChannelCreateMessage)}, 1132 sizeof (struct GNUNET_CADET_ChannelOpenMessageMessage)},
1128 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY, 1133 {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY,
1129 sizeof (struct GNUNET_CADET_ChannelDestroyMessage)}, 1134 sizeof (struct GNUNET_CADET_ChannelDestroyMessage)},
1130 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0}, 1135 {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0},
@@ -1214,26 +1219,16 @@ GML_shutdown (void)
1214 * @return non-NULL if channel exists in the clients lists 1219 * @return non-NULL if channel exists in the clients lists
1215 */ 1220 */
1216struct CadetChannel * 1221struct CadetChannel *
1217GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid) 1222GML_channel_get (struct CadetClient *c,
1223 struct GNUNET_CADET_ClientChannelNumber chid)
1218{ 1224{
1219 struct GNUNET_CONTAINER_MultiHashMap32 *map; 1225 struct GNUNET_CONTAINER_MultiHashMap32 *map;
1220 1226
1221 if (0 == (chid & GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)) 1227 if (ntohl (chid.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1222 {
1223 GNUNET_break_op (0);
1224 LOG (GNUNET_ERROR_TYPE_DEBUG, "CHID %X not a local chid\n", chid);
1225 return NULL;
1226 }
1227
1228 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
1229 map = c->incoming_channels;
1230 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1231 map = c->own_channels; 1228 map = c->own_channels;
1232 else 1229 else
1233 { 1230 map = c->incoming_channels;
1234 GNUNET_break (0); 1231
1235 map = NULL;
1236 }
1237 if (NULL == map) 1232 if (NULL == map)
1238 { 1233 {
1239 GNUNET_break (0); 1234 GNUNET_break (0);
@@ -1242,7 +1237,8 @@ GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
1242 GML_2s (c), chid); 1237 GML_2s (c), chid);
1243 return NULL; 1238 return NULL;
1244 } 1239 }
1245 return GNUNET_CONTAINER_multihashmap32_get (map, chid); 1240 return GNUNET_CONTAINER_multihashmap32_get (map,
1241 chid.channel_of_client);
1246} 1242}
1247 1243
1248 1244
@@ -1255,17 +1251,19 @@ GML_channel_get (struct CadetClient *c, CADET_ChannelNumber chid)
1255 */ 1251 */
1256void 1252void
1257GML_channel_add (struct CadetClient *client, 1253GML_channel_add (struct CadetClient *client,
1258 uint32_t chid, 1254 struct GNUNET_CADET_ClientChannelNumber chid,
1259 struct CadetChannel *ch) 1255 struct CadetChannel *ch)
1260{ 1256{
1261 if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV) 1257 if (ntohl (chid.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1262 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels, chid, ch, 1258 GNUNET_CONTAINER_multihashmap32_put (client->own_channels,
1263 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 1259 chid.channel_of_client,
1264 else if (chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI) 1260 ch,
1265 GNUNET_CONTAINER_multihashmap32_put (client->own_channels, chid, ch,
1266 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 1261 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1267 else 1262 else
1268 GNUNET_break (0); 1263 GNUNET_CONTAINER_multihashmap32_put (client->incoming_channels,
1264 chid.channel_of_client,
1265 ch,
1266 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
1269} 1267}
1270 1268
1271 1269
@@ -1278,19 +1276,17 @@ GML_channel_add (struct CadetClient *client,
1278 */ 1276 */
1279void 1277void
1280GML_channel_remove (struct CadetClient *client, 1278GML_channel_remove (struct CadetClient *client,
1281 uint32_t chid, 1279 struct GNUNET_CADET_ClientChannelNumber chid,
1282 struct CadetChannel *ch) 1280 struct CadetChannel *ch)
1283{ 1281{
1284 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= chid) 1282 if (ntohl (chid.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1285 GNUNET_break (GNUNET_YES == 1283 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
1286 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels, 1284 chid.channel_of_client,
1287 chid, ch)); 1285 ch);
1288 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= chid)
1289 GNUNET_break (GNUNET_YES ==
1290 GNUNET_CONTAINER_multihashmap32_remove (client->own_channels,
1291 chid, ch));
1292 else 1286 else
1293 GNUNET_break (0); 1287 GNUNET_CONTAINER_multihashmap32_remove (client->incoming_channels,
1288 chid.channel_of_client,
1289 ch);
1294} 1290}
1295 1291
1296 1292
@@ -1301,18 +1297,26 @@ GML_channel_remove (struct CadetClient *client,
1301 * 1297 *
1302 * @return LID of a channel free to use. 1298 * @return LID of a channel free to use.
1303 */ 1299 */
1304CADET_ChannelNumber 1300struct GNUNET_CADET_ClientChannelNumber
1305GML_get_next_chid (struct CadetClient *c) 1301GML_get_next_chid (struct CadetClient *c)
1306{ 1302{
1307 CADET_ChannelNumber chid; 1303 struct GNUNET_CADET_ClientChannelNumber chid;
1308 1304
1309 while (NULL != GML_channel_get (c, c->next_chid)) 1305 while (NULL != GML_channel_get (c,
1306 c->next_chid))
1310 { 1307 {
1311 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", c->next_chid); 1308 LOG (GNUNET_ERROR_TYPE_DEBUG,
1312 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV; 1309 "Channel %u exists...\n",
1310 c->next_chid);
1311 c->next_chid.channel_of_client
1312 = htonl (1 + (ntohl (c->next_chid.channel_of_client)));
1313 if (ntohl (c->next_chid.channel_of_client) >=
1314 GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1315 c->next_chid.channel_of_client = htonl (0);
1313 } 1316 }
1314 chid = c->next_chid; 1317 chid = c->next_chid;
1315 c->next_chid = (c->next_chid + 1) | GNUNET_CADET_LOCAL_CHANNEL_ID_SERV; 1318 c->next_chid.channel_of_client
1319 = htonl (1 + (ntohl (c->next_chid.channel_of_client)));
1316 1320
1317 return chid; 1321 return chid;
1318} 1322}
@@ -1330,9 +1334,11 @@ GML_client_get (struct GNUNET_SERVER_Client *client)
1330{ 1334{
1331 if (NULL == client) 1335 if (NULL == client)
1332 return NULL; 1336 return NULL;
1333 return GNUNET_SERVER_client_get_user_context (client, struct CadetClient); 1337 return GNUNET_SERVER_client_get_user_context (client,
1338 struct CadetClient);
1334} 1339}
1335 1340
1341
1336/** 1342/**
1337 * Find a client that has opened a port 1343 * Find a client that has opened a port
1338 * 1344 *
@@ -1357,27 +1363,25 @@ GML_client_get_by_port (const struct GNUNET_HashCode *port)
1357void 1363void
1358GML_client_delete_channel (struct CadetClient *c, 1364GML_client_delete_channel (struct CadetClient *c,
1359 struct CadetChannel *ch, 1365 struct CadetChannel *ch,
1360 CADET_ChannelNumber id) 1366 struct GNUNET_CADET_ClientChannelNumber id)
1361{ 1367{
1362 int res; 1368 int res;
1363 1369
1364 if (GNUNET_CADET_LOCAL_CHANNEL_ID_SERV <= id) 1370 if (ntohl (id.channel_of_client) >= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI)
1365 {
1366 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1367 id, ch);
1368 if (GNUNET_YES != res)
1369 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
1370 }
1371 else if (GNUNET_CADET_LOCAL_CHANNEL_ID_CLI <= id)
1372 { 1371 {
1373 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels, 1372 res = GNUNET_CONTAINER_multihashmap32_remove (c->own_channels,
1374 id, ch); 1373 id.channel_of_client,
1374 ch);
1375 if (GNUNET_YES != res) 1375 if (GNUNET_YES != res)
1376 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n"); 1376 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_tunnel root KO\n");
1377 } 1377 }
1378 else 1378 else
1379 { 1379 {
1380 GNUNET_break (0); 1380 res = GNUNET_CONTAINER_multihashmap32_remove (c->incoming_channels,
1381 id.channel_of_client,
1382 ch);
1383 if (GNUNET_YES != res)
1384 LOG (GNUNET_ERROR_TYPE_DEBUG, "client_delete_channel dest KO\n");
1381 } 1385 }
1382} 1386}
1383 1387
@@ -1390,17 +1394,21 @@ GML_client_delete_channel (struct CadetClient *c,
1390 * @param id Channel ID to use 1394 * @param id Channel ID to use
1391 */ 1395 */
1392void 1396void
1393GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id) 1397GML_send_ack (struct CadetClient *c,
1398 struct GNUNET_CADET_ClientChannelNumber id)
1394{ 1399{
1395 struct GNUNET_CADET_LocalAck msg; 1400 struct GNUNET_CADET_LocalAck msg;
1396 1401
1397 LOG (GNUNET_ERROR_TYPE_DEBUG, 1402 LOG (GNUNET_ERROR_TYPE_DEBUG,
1398 "send local %s ack on %X towards %p\n", 1403 "send local %s ack on %X towards %p\n",
1399 id < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV ? "FWD" : "BCK", id, c); 1404 ntohl (id.channel_of_client) < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI
1405 ? "FWD" : "BCK",
1406 ntohl (id.channel_of_client),
1407 c);
1400 1408
1401 msg.header.size = htons (sizeof (msg)); 1409 msg.header.size = htons (sizeof (msg));
1402 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK); 1410 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
1403 msg.channel_id = htonl (id); 1411 msg.channel_id = id;
1404 GNUNET_SERVER_notification_context_unicast (nc, 1412 GNUNET_SERVER_notification_context_unicast (nc,
1405 c->handle, 1413 c->handle,
1406 &msg.header, 1414 &msg.header,
@@ -1421,14 +1429,16 @@ GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id)
1421 */ 1429 */
1422void 1430void
1423GML_send_channel_create (struct CadetClient *c, 1431GML_send_channel_create (struct CadetClient *c,
1424 uint32_t id, struct GNUNET_HashCode *port, 1432 struct GNUNET_CADET_ClientChannelNumber id,
1425 uint32_t opt, const struct GNUNET_PeerIdentity *peer) 1433 const struct GNUNET_HashCode *port,
1434 uint32_t opt,
1435 const struct GNUNET_PeerIdentity *peer)
1426{ 1436{
1427 struct GNUNET_CADET_ChannelCreateMessage msg; 1437 struct GNUNET_CADET_ChannelOpenMessageMessage msg;
1428 1438
1429 msg.header.size = htons (sizeof (msg)); 1439 msg.header.size = htons (sizeof (msg));
1430 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE); 1440 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN);
1431 msg.channel_id = htonl (id); 1441 msg.channel_id = id;
1432 msg.port = *port; 1442 msg.port = *port;
1433 msg.opt = htonl (opt); 1443 msg.opt = htonl (opt);
1434 msg.peer = *peer; 1444 msg.peer = *peer;
@@ -1444,17 +1454,19 @@ GML_send_channel_create (struct CadetClient *c,
1444 * @param id Channel ID to use 1454 * @param id Channel ID to use
1445 */ 1455 */
1446void 1456void
1447GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id) 1457GML_send_channel_nack (struct CadetClient *c,
1458 struct GNUNET_CADET_ClientChannelNumber id)
1448{ 1459{
1449 struct GNUNET_CADET_LocalAck msg; 1460 struct GNUNET_CADET_LocalAck msg;
1450 1461
1451 LOG (GNUNET_ERROR_TYPE_DEBUG, 1462 LOG (GNUNET_ERROR_TYPE_DEBUG,
1452 "send local nack on %X towards %p\n", 1463 "send local nack on %X towards %p\n",
1453 id, c); 1464 ntohl (id.channel_of_client),
1465 c);
1454 1466
1455 msg.header.size = htons (sizeof (msg)); 1467 msg.header.size = htons (sizeof (msg));
1456 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK); 1468 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED);
1457 msg.channel_id = htonl (id); 1469 msg.channel_id = id;
1458 GNUNET_SERVER_notification_context_unicast (nc, 1470 GNUNET_SERVER_notification_context_unicast (nc,
1459 c->handle, 1471 c->handle,
1460 &msg.header, 1472 &msg.header,
@@ -1469,7 +1481,8 @@ GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id)
1469 * @param id ID of the channel that is destroyed. 1481 * @param id ID of the channel that is destroyed.
1470 */ 1482 */
1471void 1483void
1472GML_send_channel_destroy (struct CadetClient *c, uint32_t id) 1484GML_send_channel_destroy (struct CadetClient *c,
1485 struct GNUNET_CADET_ClientChannelNumber id)
1473{ 1486{
1474 struct GNUNET_CADET_ChannelDestroyMessage msg; 1487 struct GNUNET_CADET_ChannelDestroyMessage msg;
1475 1488
@@ -1482,7 +1495,7 @@ GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
1482 return; 1495 return;
1483 msg.header.size = htons (sizeof (msg)); 1496 msg.header.size = htons (sizeof (msg));
1484 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY); 1497 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1485 msg.channel_id = htonl (id); 1498 msg.channel_id = id;
1486 GNUNET_SERVER_notification_context_unicast (nc, c->handle, 1499 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1487 &msg.header, GNUNET_NO); 1500 &msg.header, GNUNET_NO);
1488} 1501}
@@ -1497,11 +1510,11 @@ GML_send_channel_destroy (struct CadetClient *c, uint32_t id)
1497 */ 1510 */
1498void 1511void
1499GML_send_data (struct CadetClient *c, 1512GML_send_data (struct CadetClient *c,
1500 const struct GNUNET_CADET_Data *msg, 1513 const struct GNUNET_CADET_ChannelAppDataMessage *msg,
1501 CADET_ChannelNumber id) 1514 struct GNUNET_CADET_ClientChannelNumber id)
1502{ 1515{
1503 struct GNUNET_CADET_LocalData *copy; 1516 struct GNUNET_CADET_LocalData *copy;
1504 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_Data); 1517 uint16_t size = ntohs (msg->header.size) - sizeof (struct GNUNET_CADET_ChannelAppDataMessage);
1505 char cbuf[size + sizeof (struct GNUNET_CADET_LocalData)]; 1518 char cbuf[size + sizeof (struct GNUNET_CADET_LocalData)];
1506 1519
1507 if (size < sizeof (struct GNUNET_MessageHeader)) 1520 if (size < sizeof (struct GNUNET_MessageHeader))
@@ -1518,7 +1531,7 @@ GML_send_data (struct CadetClient *c,
1518 GNUNET_memcpy (&copy[1], &msg[1], size); 1531 GNUNET_memcpy (&copy[1], &msg[1], size);
1519 copy->header.size = htons (sizeof (struct GNUNET_CADET_LocalData) + size); 1532 copy->header.size = htons (sizeof (struct GNUNET_CADET_LocalData) + size);
1520 copy->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA); 1533 copy->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
1521 copy->id = htonl (id); 1534 copy->id = id;
1522 GNUNET_SERVER_notification_context_unicast (nc, c->handle, 1535 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1523 &copy->header, GNUNET_NO); 1536 &copy->header, GNUNET_NO);
1524} 1537}
diff --git a/src/cadet/gnunet-service-cadet_local.h b/src/cadet/gnunet-service-cadet_local.h
index f89745092..bf691f9c3 100644
--- a/src/cadet/gnunet-service-cadet_local.h
+++ b/src/cadet/gnunet-service-cadet_local.h
@@ -80,7 +80,8 @@ GML_shutdown (void);
80 * @return non-NULL if channel exists in the clients lists 80 * @return non-NULL if channel exists in the clients lists
81 */ 81 */
82struct CadetChannel * 82struct CadetChannel *
83GML_channel_get (struct CadetClient *c, uint32_t chid); 83GML_channel_get (struct CadetClient *c,
84 struct GNUNET_CADET_ClientChannelNumber chid);
84 85
85/** 86/**
86 * Add a channel to a client 87 * Add a channel to a client
@@ -91,7 +92,7 @@ GML_channel_get (struct CadetClient *c, uint32_t chid);
91 */ 92 */
92void 93void
93GML_channel_add (struct CadetClient *client, 94GML_channel_add (struct CadetClient *client,
94 uint32_t chid, 95 struct GNUNET_CADET_ClientChannelNumber chid,
95 struct CadetChannel *ch); 96 struct CadetChannel *ch);
96 97
97/** 98/**
@@ -103,7 +104,7 @@ GML_channel_add (struct CadetClient *client,
103 */ 104 */
104void 105void
105GML_channel_remove (struct CadetClient *client, 106GML_channel_remove (struct CadetClient *client,
106 uint32_t chid, 107 struct GNUNET_CADET_ClientChannelNumber chid,
107 struct CadetChannel *ch); 108 struct CadetChannel *ch);
108 109
109/** 110/**
@@ -113,7 +114,7 @@ GML_channel_remove (struct CadetClient *client,
113 * 114 *
114 * @return LID of a channel free to use. 115 * @return LID of a channel free to use.
115 */ 116 */
116CADET_ChannelNumber 117struct GNUNET_CADET_ClientChannelNumber
117GML_get_next_chid (struct CadetClient *c); 118GML_get_next_chid (struct CadetClient *c);
118 119
119/** 120/**
@@ -146,7 +147,7 @@ GML_client_get_by_port (const struct GNUNET_HashCode *port);
146void 147void
147GML_client_delete_channel (struct CadetClient *c, 148GML_client_delete_channel (struct CadetClient *c,
148 struct CadetChannel *ch, 149 struct CadetChannel *ch,
149 CADET_ChannelNumber id); 150 struct GNUNET_CADET_ClientChannelNumber id);
150 151
151/** 152/**
152 * Build a local ACK message and send it to a local client, if needed. 153 * Build a local ACK message and send it to a local client, if needed.
@@ -157,7 +158,8 @@ GML_client_delete_channel (struct CadetClient *c,
157 * @param id Channel ID to use 158 * @param id Channel ID to use
158 */ 159 */
159void 160void
160GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id); 161GML_send_ack (struct CadetClient *c,
162 struct GNUNET_CADET_ClientChannelNumber id);
161 163
162/** 164/**
163 * Notify the appropriate client that a new incoming channel was created. 165 * Notify the appropriate client that a new incoming channel was created.
@@ -170,8 +172,10 @@ GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id);
170 */ 172 */
171void 173void
172GML_send_channel_create (struct CadetClient *c, 174GML_send_channel_create (struct CadetClient *c,
173 uint32_t id, struct GNUNET_HashCode *port, 175 struct GNUNET_CADET_ClientChannelNumber id,
174 uint32_t opt, const struct GNUNET_PeerIdentity *peer); 176 const struct GNUNET_HashCode *port,
177 uint32_t opt,
178 const struct GNUNET_PeerIdentity *peer);
175 179
176/** 180/**
177 * Build a local channel NACK message and send it to a local client. 181 * Build a local channel NACK message and send it to a local client.
@@ -180,7 +184,9 @@ GML_send_channel_create (struct CadetClient *c,
180 * @param id Channel ID to use 184 * @param id Channel ID to use
181 */ 185 */
182void 186void
183GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id); 187GML_send_channel_nack (struct CadetClient *c,
188 struct GNUNET_CADET_ClientChannelNumber id);
189
184 190
185/** 191/**
186 * Notify a client that a channel is no longer valid. 192 * Notify a client that a channel is no longer valid.
@@ -189,7 +195,9 @@ GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id);
189 * @param id ID of the channel that is destroyed. 195 * @param id ID of the channel that is destroyed.
190 */ 196 */
191void 197void
192GML_send_channel_destroy (struct CadetClient *c, uint32_t id); 198GML_send_channel_destroy (struct CadetClient *c,
199 struct GNUNET_CADET_ClientChannelNumber id);
200
193 201
194/** 202/**
195 * Modify the cadet message ID from global to local and send to client. 203 * Modify the cadet message ID from global to local and send to client.
@@ -200,8 +208,8 @@ GML_send_channel_destroy (struct CadetClient *c, uint32_t id);
200 */ 208 */
201void 209void
202GML_send_data (struct CadetClient *c, 210GML_send_data (struct CadetClient *c,
203 const struct GNUNET_CADET_Data *msg, 211 const struct GNUNET_CADET_ChannelAppDataMessage *msg,
204 CADET_ChannelNumber id); 212 struct GNUNET_CADET_ClientChannelNumber id);
205 213
206/** 214/**
207 * Get the static string to represent a client. 215 * Get the static string to represent a client.
diff --git a/src/cadet/gnunet-service-cadet_peer.c b/src/cadet/gnunet-service-cadet_peer.c
index c312d56bf..3f8b7bbb8 100644
--- a/src/cadet/gnunet-service-cadet_peer.c
+++ b/src/cadet/gnunet-service-cadet_peer.c
@@ -73,6 +73,11 @@ struct CadetPeerQueue {
73 void *cont_cls; 73 void *cont_cls;
74 74
75 /** 75 /**
76 * Task to asynchronously run the drop continuation.
77 */
78 struct GNUNET_SCHEDULER_Task *drop_task;
79
80 /**
76 * Time when message was queued for sending. 81 * Time when message was queued for sending.
77 */ 82 */
78 struct GNUNET_TIME_Absolute queue_timestamp; 83 struct GNUNET_TIME_Absolute queue_timestamp;
@@ -98,9 +103,9 @@ struct CadetPeerQueue {
98 uint16_t payload_type; 103 uint16_t payload_type;
99 104
100 /** 105 /**
101 *ID of the payload (PID, ACK #, ...). 106 * ID of the payload (PID, ACK #, ...).
102 */ 107 */
103 uint16_t payload_id; 108 struct CadetEncryptedMessageIdentifier payload_id;
104 109
105 /** 110 /**
106 * Connection this message was sent on. 111 * Connection this message was sent on.
@@ -160,7 +165,7 @@ struct CadetPeer
160 /** 165 /**
161 * Connections that go through this peer; indexed by tid. 166 * Connections that go through this peer; indexed by tid.
162 */ 167 */
163 struct GNUNET_CONTAINER_MultiHashMap *connections; 168 struct GNUNET_CONTAINER_MultiShortmap *connections;
164 169
165 /** 170 /**
166 * Handle for core transmissions. 171 * Handle for core transmissions.
@@ -263,7 +268,7 @@ static int in_shutdown;
263 */ 268 */
264static int 269static int
265notify_broken (void *cls, 270notify_broken (void *cls,
266 const struct GNUNET_HashCode *key, 271 const struct GNUNET_ShortHashCode *key,
267 void *value) 272 void *value)
268{ 273{
269 struct CadetPeer *peer = cls; 274 struct CadetPeer *peer = cls;
@@ -368,7 +373,8 @@ core_connect_handler (void *cls,
368 373
369 /* Create the connections hashmap */ 374 /* Create the connections hashmap */
370 GNUNET_assert (NULL == neighbor->connections); 375 GNUNET_assert (NULL == neighbor->connections);
371 neighbor->connections = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO); 376 neighbor->connections = GNUNET_CONTAINER_multishortmap_create (16,
377 GNUNET_YES);
372 GNUNET_STATISTICS_update (stats, 378 GNUNET_STATISTICS_update (stats,
373 "# peers", 379 "# peers",
374 1, 380 1,
@@ -420,11 +426,11 @@ core_disconnect_handler (void *cls,
420 direct_path = pop_direct_path (p); 426 direct_path = pop_direct_path (p);
421 if (NULL != p->connections) 427 if (NULL != p->connections)
422 { 428 {
423 GNUNET_CONTAINER_multihashmap_iterate (p->connections, 429 GNUNET_CONTAINER_multishortmap_iterate (p->connections,
424 &notify_broken, 430 &notify_broken,
425 p); 431 p);
426 GNUNET_CONTAINER_multihashmap_destroy (p->connections); 432 GNUNET_CONTAINER_multishortmap_destroy (p->connections);
427 p->connections = NULL; 433 p->connections = NULL;
428 } 434 }
429 GNUNET_STATISTICS_update (stats, 435 GNUNET_STATISTICS_update (stats,
430 "# peers", 436 "# peers",
@@ -450,7 +456,7 @@ core_disconnect_handler (void *cls,
450 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise. 456 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
451 */ 457 */
452static int 458static int
453check_create (void *cls, const struct GNUNET_CADET_ConnectionCreate *msg) 459check_create (void *cls, const struct GNUNET_CADET_ConnectionCreateMessage *msg)
454{ 460{
455 uint16_t size; 461 uint16_t size;
456 462
@@ -470,7 +476,7 @@ check_create (void *cls, const struct GNUNET_CADET_ConnectionCreate *msg)
470 * @param msg Message itself. 476 * @param msg Message itself.
471 */ 477 */
472static void 478static void
473handle_create (void *cls, const struct GNUNET_CADET_ConnectionCreate *msg) 479handle_create (void *cls, const struct GNUNET_CADET_ConnectionCreateMessage *msg)
474{ 480{
475 struct CadetPeer *peer = cls; 481 struct CadetPeer *peer = cls;
476 GCC_handle_create (peer, msg); 482 GCC_handle_create (peer, msg);
@@ -478,13 +484,13 @@ handle_create (void *cls, const struct GNUNET_CADET_ConnectionCreate *msg)
478 484
479 485
480/** 486/**
481 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK 487 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK
482 * 488 *
483 * @param cls Closure (CadetPeer for neighbor that sent the message). 489 * @param cls Closure (CadetPeer for neighbor that sent the message).
484 * @param msg Message itself. 490 * @param msg Message itself.
485 */ 491 */
486static void 492static void
487handle_confirm (void *cls, const struct GNUNET_CADET_ConnectionACK *msg) 493handle_confirm (void *cls, const struct GNUNET_CADET_ConnectionCreateMessageAckMessage *msg)
488{ 494{
489 struct CadetPeer *peer = cls; 495 struct CadetPeer *peer = cls;
490 GCC_handle_confirm (peer, msg); 496 GCC_handle_confirm (peer, msg);
@@ -498,7 +504,7 @@ handle_confirm (void *cls, const struct GNUNET_CADET_ConnectionACK *msg)
498 * @param msg Message itself. 504 * @param msg Message itself.
499 */ 505 */
500static void 506static void
501handle_broken (void *cls, const struct GNUNET_CADET_ConnectionBroken *msg) 507handle_broken (void *cls, const struct GNUNET_CADET_ConnectionBrokenMessage *msg)
502{ 508{
503 struct CadetPeer *peer = cls; 509 struct CadetPeer *peer = cls;
504 GCC_handle_broken (peer, msg); 510 GCC_handle_broken (peer, msg);
@@ -512,7 +518,7 @@ handle_broken (void *cls, const struct GNUNET_CADET_ConnectionBroken *msg)
512 * @param msg Message itself. 518 * @param msg Message itself.
513 */ 519 */
514static void 520static void
515handle_destroy (void *cls, const struct GNUNET_CADET_ConnectionDestroy *msg) 521handle_destroy (void *cls, const struct GNUNET_CADET_ConnectionDestroyMessage *msg)
516{ 522{
517 struct CadetPeer *peer = cls; 523 struct CadetPeer *peer = cls;
518 GCC_handle_destroy (peer, msg); 524 GCC_handle_destroy (peer, msg);
@@ -520,13 +526,13 @@ handle_destroy (void *cls, const struct GNUNET_CADET_ConnectionDestroy *msg)
520 526
521 527
522/** 528/**
523 * Handle for #GNUNET_MESSAGE_TYPE_CADET_ACK 529 * Handle for #GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK
524 * 530 *
525 * @param cls Closure (CadetPeer for neighbor that sent the message). 531 * @param cls Closure (CadetPeer for neighbor that sent the message).
526 * @param msg Message itself. 532 * @param msg Message itself.
527 */ 533 */
528static void 534static void
529handle_ack (void *cls, const struct GNUNET_CADET_ACK *msg) 535handle_ack (void *cls, const struct GNUNET_CADET_ConnectionEncryptedAckMessage *msg)
530{ 536{
531 struct CadetPeer *peer = cls; 537 struct CadetPeer *peer = cls;
532 GCC_handle_ack (peer, msg); 538 GCC_handle_ack (peer, msg);
@@ -534,13 +540,13 @@ handle_ack (void *cls, const struct GNUNET_CADET_ACK *msg)
534 540
535 541
536/** 542/**
537 * Handle for #GNUNET_MESSAGE_TYPE_CADET_POLL 543 * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL
538 * 544 *
539 * @param cls Closure (CadetPeer for neighbor that sent the message). 545 * @param cls Closure (CadetPeer for neighbor that sent the message).
540 * @param msg Message itself. 546 * @param msg Message itself.
541 */ 547 */
542static void 548static void
543handle_poll (void *cls, const struct GNUNET_CADET_Poll *msg) 549handle_poll (void *cls, const struct GNUNET_CADET_ConnectionHopByHopPollMessage *msg)
544{ 550{
545 struct CadetPeer *peer = cls; 551 struct CadetPeer *peer = cls;
546 GCC_handle_poll (peer, msg); 552 GCC_handle_poll (peer, msg);
@@ -548,13 +554,13 @@ handle_poll (void *cls, const struct GNUNET_CADET_Poll *msg)
548 554
549 555
550/** 556/**
551 * Handle for #GNUNET_MESSAGE_TYPE_CADET_KX 557 * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX
552 * 558 *
553 * @param cls Closure (CadetPeer for neighbor that sent the message). 559 * @param cls Closure (CadetPeer for neighbor that sent the message).
554 * @param msg Message itself. 560 * @param msg Message itself.
555 */ 561 */
556static void 562static void
557handle_kx (void *cls, const struct GNUNET_CADET_KX *msg) 563handle_kx (void *cls, const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
558{ 564{
559 struct CadetPeer *peer = cls; 565 struct CadetPeer *peer = cls;
560 GCC_handle_kx (peer, msg); 566 GCC_handle_kx (peer, msg);
@@ -570,13 +576,13 @@ handle_kx (void *cls, const struct GNUNET_CADET_KX *msg)
570 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise. 576 * @return #GNUNET_YES if size is correct, #GNUNET_NO otherwise.
571 */ 577 */
572static int 578static int
573check_encrypted (void *cls, const struct GNUNET_CADET_Encrypted *msg) 579check_encrypted (void *cls, const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
574{ 580{
575 uint16_t size; 581 uint16_t size;
576 uint16_t minimum_size; 582 uint16_t minimum_size;
577 583
578 size = ntohs (msg->header.size); 584 size = ntohs (msg->header.size);
579 minimum_size = sizeof (struct GNUNET_CADET_Encrypted) 585 minimum_size = sizeof (struct GNUNET_CADET_TunnelEncryptedMessage)
580 + sizeof (struct GNUNET_MessageHeader); 586 + sizeof (struct GNUNET_MessageHeader);
581 587
582 if (size < minimum_size) 588 if (size < minimum_size)
@@ -588,13 +594,13 @@ check_encrypted (void *cls, const struct GNUNET_CADET_Encrypted *msg)
588} 594}
589 595
590/** 596/**
591 * Handle for #GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED. 597 * Handle for #GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED.
592 * 598 *
593 * @param cls Closure (CadetPeer for neighbor that sent the message). 599 * @param cls Closure (CadetPeer for neighbor that sent the message).
594 * @param msg Message itself. 600 * @param msg Message itself.
595 */ 601 */
596static void 602static void
597handle_encrypted (void *cls, const struct GNUNET_CADET_Encrypted *msg) 603handle_encrypted (void *cls, const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
598{ 604{
599 struct CadetPeer *peer = cls; 605 struct CadetPeer *peer = cls;
600 GCC_handle_encrypted (peer, msg); 606 GCC_handle_encrypted (peer, msg);
@@ -618,39 +624,39 @@ connect_to_core (const struct GNUNET_CONFIGURATION_Handle *c)
618 struct GNUNET_MQ_MessageHandler core_handlers[] = { 624 struct GNUNET_MQ_MessageHandler core_handlers[] = {
619 GNUNET_MQ_hd_var_size (create, 625 GNUNET_MQ_hd_var_size (create,
620 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE, 626 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE,
621 struct GNUNET_CADET_ConnectionCreate, 627 struct GNUNET_CADET_ConnectionCreateMessage,
622 NULL), 628 NULL),
623 GNUNET_MQ_hd_fixed_size (confirm, 629 GNUNET_MQ_hd_fixed_size (confirm,
624 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK, 630 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK,
625 struct GNUNET_CADET_ConnectionACK, 631 struct GNUNET_CADET_ConnectionCreateMessageAckMessage,
626 NULL), 632 NULL),
627 GNUNET_MQ_hd_fixed_size (broken, 633 GNUNET_MQ_hd_fixed_size (broken,
628 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN, 634 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN,
629 struct GNUNET_CADET_ConnectionBroken, 635 struct GNUNET_CADET_ConnectionBrokenMessage,
630 NULL), 636 NULL),
631 GNUNET_MQ_hd_fixed_size (destroy, 637 GNUNET_MQ_hd_fixed_size (destroy,
632 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY, 638 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY,
633 struct GNUNET_CADET_ConnectionDestroy, 639 struct GNUNET_CADET_ConnectionDestroyMessage,
634 NULL), 640 NULL),
635 GNUNET_MQ_hd_fixed_size (ack, 641 GNUNET_MQ_hd_fixed_size (ack,
636 GNUNET_MESSAGE_TYPE_CADET_ACK, 642 GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK,
637 struct GNUNET_CADET_ACK, 643 struct GNUNET_CADET_ConnectionEncryptedAckMessage,
638 NULL), 644 NULL),
639 GNUNET_MQ_hd_fixed_size (poll, 645 GNUNET_MQ_hd_fixed_size (poll,
640 GNUNET_MESSAGE_TYPE_CADET_POLL, 646 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL,
641 struct GNUNET_CADET_Poll, 647 struct GNUNET_CADET_ConnectionHopByHopPollMessage,
642 NULL), 648 NULL),
643 GNUNET_MQ_hd_fixed_size (kx, 649 GNUNET_MQ_hd_fixed_size (kx,
644 GNUNET_MESSAGE_TYPE_CADET_KX, 650 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX,
645 struct GNUNET_CADET_KX, 651 struct GNUNET_CADET_TunnelKeyExchangeMessage,
646 NULL), 652 NULL),
647 GNUNET_MQ_hd_var_size (encrypted, 653 GNUNET_MQ_hd_var_size (encrypted,
648 GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED, 654 GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED,
649 struct GNUNET_CADET_Encrypted, 655 struct GNUNET_CADET_TunnelEncryptedMessage,
650 NULL), 656 NULL),
651 GNUNET_MQ_handler_end () 657 GNUNET_MQ_handler_end ()
652 }; 658 };
653 core_handle = GNUNET_CORE_connecT (c, NULL, 659 core_handle = GNUNET_CORE_connect (c, NULL,
654 &core_init_notify, 660 &core_init_notify,
655 &core_connect_handler, 661 &core_connect_handler,
656 &core_disconnect_handler, 662 &core_disconnect_handler,
@@ -681,7 +687,7 @@ core_init_notify (void *cls,
681 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n")); 687 LOG (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
682 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (core_identity)); 688 LOG (GNUNET_ERROR_TYPE_ERROR, " core id %s\n", GNUNET_i2s (core_identity));
683 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id)); 689 LOG (GNUNET_ERROR_TYPE_ERROR, " my id %s\n", GNUNET_i2s (&my_full_id));
684 GNUNET_CORE_disconnecT (core_handle); 690 GNUNET_CORE_disconnect (core_handle);
685 connect_to_core (c); 691 connect_to_core (c);
686 return; 692 return;
687 } 693 }
@@ -729,7 +735,7 @@ get_priority (struct CadetPeerQueue *q)
729 } 735 }
730 736
731 /* Bulky payload has lower priority, control traffic has higher. */ 737 /* Bulky payload has lower priority, control traffic has higher. */
732 if (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED == q->type) 738 if (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED == q->type)
733 return low; 739 return low;
734 return high; 740 return high;
735} 741}
@@ -795,8 +801,8 @@ peer_destroy (struct CadetPeer *peer)
795 GCT_destroy_empty (peer->tunnel); 801 GCT_destroy_empty (peer->tunnel);
796 if (NULL != peer->connections) 802 if (NULL != peer->connections)
797 { 803 {
798 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (peer->connections)); 804 GNUNET_assert (0 == GNUNET_CONTAINER_multishortmap_size (peer->connections));
799 GNUNET_CONTAINER_multihashmap_destroy (peer->connections); 805 GNUNET_CONTAINER_multishortmap_destroy (peer->connections);
800 peer->connections = NULL; 806 peer->connections = NULL;
801 } 807 }
802 if (NULL != peer->hello_offer) 808 if (NULL != peer->hello_offer)
@@ -1053,8 +1059,8 @@ search_handler (void *cls, const struct CadetPeerPath *path)
1053static int 1059static int
1054is_connection_management (uint16_t type) 1060is_connection_management (uint16_t type)
1055{ 1061{
1056 return type == GNUNET_MESSAGE_TYPE_CADET_ACK || 1062 return type == GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK ||
1057 type == GNUNET_MESSAGE_TYPE_CADET_POLL; 1063 type == GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL;
1058} 1064}
1059 1065
1060 1066
@@ -1104,8 +1110,11 @@ call_peer_cont (struct CadetPeerQueue *q, int sent)
1104 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO)); 1110 GNUNET_STRINGS_relative_time_to_string (wait_time, GNUNET_NO));
1105 q->cont (q->cont_cls, 1111 q->cont (q->cont_cls,
1106 q->c, q->c_fwd, sent, 1112 q->c, q->c_fwd, sent,
1107 q->type, q->payload_type, q->payload_id, 1113 q->type,
1114 q->payload_type,
1115 q->payload_id,
1108 q->size, wait_time); 1116 q->size, wait_time);
1117 q->cont = NULL;
1109 } 1118 }
1110 GNUNET_CONTAINER_DLL_remove (q->peer->q_head, q->peer->q_tail, q); 1119 GNUNET_CONTAINER_DLL_remove (q->peer->q_head, q->peer->q_tail, q);
1111} 1120}
@@ -1131,6 +1140,22 @@ mq_sent (void *cls)
1131 1140
1132 1141
1133/** 1142/**
1143 * Finish the drop operation.
1144 *
1145 * @param cls queue entry to finish drop for
1146 */
1147static void
1148drop_cb (void *cls)
1149{
1150 struct CadetPeerQueue *q = cls;
1151
1152 GNUNET_MQ_discard (q->env);
1153 call_peer_cont (q, GNUNET_YES);
1154 GNUNET_free (q);
1155}
1156
1157
1158/**
1134 * @brief Send a message to another peer (using CORE). 1159 * @brief Send a message to another peer (using CORE).
1135 * 1160 *
1136 * @param peer Peer towards which to queue the message. 1161 * @param peer Peer towards which to queue the message.
@@ -1150,7 +1175,7 @@ struct CadetPeerQueue *
1150GCP_send (struct CadetPeer *peer, 1175GCP_send (struct CadetPeer *peer,
1151 const struct GNUNET_MessageHeader *message, 1176 const struct GNUNET_MessageHeader *message,
1152 uint16_t payload_type, 1177 uint16_t payload_type,
1153 uint32_t payload_id, 1178 struct CadetEncryptedMessageIdentifier payload_id,
1154 struct CadetConnection *c, 1179 struct CadetConnection *c,
1155 int fwd, 1180 int fwd,
1156 GCP_sent cont, 1181 GCP_sent cont,
@@ -1165,7 +1190,8 @@ GCP_send (struct CadetPeer *peer,
1165 size = ntohs (message->size); 1190 size = ntohs (message->size);
1166 LOG (GNUNET_ERROR_TYPE_DEBUG, 1191 LOG (GNUNET_ERROR_TYPE_DEBUG,
1167 "que %s (%s %4u) on conn %s (%p) %s towards %s (size %u)\n", 1192 "que %s (%s %4u) on conn %s (%p) %s towards %s (size %u)\n",
1168 GC_m2s (type), GC_m2s (payload_type), payload_id, 1193 GC_m2s (type), GC_m2s (payload_type),
1194 ntohl (payload_id.pid),
1169 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size); 1195 GCC_2s (c), c, GC_f2s (fwd), GCP_2s (peer), size);
1170 1196
1171 if (NULL == peer->connections) 1197 if (NULL == peer->connections)
@@ -1191,7 +1217,8 @@ GCP_send (struct CadetPeer *peer,
1191 q->payload_id = payload_id; 1217 q->payload_id = payload_id;
1192 q->c = c; 1218 q->c = c;
1193 q->c_fwd = fwd; 1219 q->c_fwd = fwd;
1194 GNUNET_MQ_notify_sent (q->env, mq_sent, q); 1220 GNUNET_MQ_notify_sent (q->env, &mq_sent, q);
1221 GNUNET_CONTAINER_DLL_insert (peer->q_head, peer->q_tail, q);
1195 1222
1196 if (GNUNET_YES == q->management_traffic) 1223 if (GNUNET_YES == q->management_traffic)
1197 { 1224 {
@@ -1201,19 +1228,21 @@ GCP_send (struct CadetPeer *peer,
1201 { 1228 {
1202 if (GNUNET_YES == should_I_drop ()) 1229 if (GNUNET_YES == should_I_drop ())
1203 { 1230 {
1204 LOG (GNUNET_ERROR_TYPE_WARNING, "DD %s (%s %u) on conn %s %s\n", 1231 LOG (GNUNET_ERROR_TYPE_WARNING,
1205 GC_m2s (q->type), GC_m2s (q->payload_type), 1232 "DD %s (%s %u) on conn %s %s (random drop for testing)\n",
1206 q->payload_id, GCC_2s (c), GC_f2s (q->c_fwd)); 1233 GC_m2s (q->type),
1207 GNUNET_MQ_discard (q->env); 1234 GC_m2s (q->payload_type),
1208 call_peer_cont (q, GNUNET_YES); 1235 ntohl (q->payload_id.pid),
1209 GNUNET_free (q); 1236 GCC_2s (c),
1210 return NULL; 1237 GC_f2s (q->c_fwd));
1238 q->drop_task = GNUNET_SCHEDULER_add_now (&drop_cb,
1239 q);
1240 return q;
1211 } 1241 }
1212 GNUNET_MQ_send (peer->core_mq, q->env); 1242 GNUNET_MQ_send (peer->core_mq, q->env);
1213 peer->queue_n++; 1243 peer->queue_n++;
1214 } 1244 }
1215 1245
1216 GNUNET_CONTAINER_DLL_insert (peer->q_head, peer->q_tail, q);
1217 GCC_check_connections (); 1246 GCC_check_connections ();
1218 return q; 1247 return q;
1219} 1248}
@@ -1231,9 +1260,18 @@ GCP_send (struct CadetPeer *peer,
1231void 1260void
1232GCP_send_cancel (struct CadetPeerQueue *q) 1261GCP_send_cancel (struct CadetPeerQueue *q)
1233{ 1262{
1234 call_peer_cont (q, GNUNET_NO); 1263 if (NULL != q->drop_task)
1264 {
1265 GNUNET_SCHEDULER_cancel (q->drop_task);
1266 q->drop_task = NULL;
1267 GNUNET_MQ_discard (q->env);
1268 }
1269 else
1270 {
1235 GNUNET_MQ_send_cancel (q->env); 1271 GNUNET_MQ_send_cancel (q->env);
1236 GNUNET_free (q); 1272 }
1273 call_peer_cont (q, GNUNET_NO);
1274 GNUNET_free (q);
1237} 1275}
1238 1276
1239 1277
@@ -1294,12 +1332,12 @@ GCP_shutdown (void)
1294 in_shutdown = GNUNET_YES; 1332 in_shutdown = GNUNET_YES;
1295 if (NULL != core_handle) 1333 if (NULL != core_handle)
1296 { 1334 {
1297 GNUNET_CORE_disconnecT (core_handle); 1335 GNUNET_CORE_disconnect (core_handle);
1298 core_handle = NULL; 1336 core_handle = NULL;
1299 } 1337 }
1300 GNUNET_PEER_change_rc (myid, -1); 1338 GNUNET_PEER_change_rc (myid, -1);
1301 /* With MQ API, CORE calls the disconnect handler for every peer 1339 /* With MQ API, CORE calls the disconnect handler for every peer
1302 * after calling GNUNET_CORE_disconnecT, shutdown must occur *after* that. 1340 * after calling GNUNET_CORE_disconnect, shutdown must occur *after* that.
1303 */ 1341 */
1304 GNUNET_CONTAINER_multipeermap_iterate (peers, 1342 GNUNET_CONTAINER_multipeermap_iterate (peers,
1305 &shutdown_peer, 1343 &shutdown_peer,
@@ -1543,14 +1581,14 @@ GCP_add_connection (struct CadetPeer *peer,
1543 GCP_2s (peer)); 1581 GCP_2s (peer));
1544 GNUNET_assert (NULL != peer->connections); 1582 GNUNET_assert (NULL != peer->connections);
1545 GNUNET_assert (GNUNET_OK == 1583 GNUNET_assert (GNUNET_OK ==
1546 GNUNET_CONTAINER_multihashmap_put (peer->connections, 1584 GNUNET_CONTAINER_multishortmap_put (peer->connections,
1547 GCC_get_h (c), 1585 &GCC_get_id (c)->connection_of_tunnel,
1548 c, 1586 c,
1549 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 1587 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1550 LOG (GNUNET_ERROR_TYPE_DEBUG, 1588 LOG (GNUNET_ERROR_TYPE_DEBUG,
1551 "Peer %s has now %u connections.\n", 1589 "Peer %s has now %u connections.\n",
1552 GCP_2s (peer), 1590 GCP_2s (peer),
1553 GNUNET_CONTAINER_multihashmap_size (peer->connections)); 1591 GNUNET_CONTAINER_multishortmap_size (peer->connections));
1554} 1592}
1555 1593
1556 1594
@@ -1761,13 +1799,13 @@ void
1761GCP_check_connection (const struct CadetPeer *peer, 1799GCP_check_connection (const struct CadetPeer *peer,
1762 const struct CadetConnection *c) 1800 const struct CadetConnection *c)
1763{ 1801{
1764 GNUNET_assert (NULL != peer); 1802 GNUNET_assert (NULL != peer);
1765 GNUNET_assert (NULL != peer->connections); 1803 GNUNET_assert (NULL != peer->connections);
1766 return; 1804 return; // ????
1767 GNUNET_assert (GNUNET_YES == 1805 GNUNET_assert (GNUNET_YES ==
1768 GNUNET_CONTAINER_multihashmap_contains_value (peer->connections, 1806 GNUNET_CONTAINER_multishortmap_contains_value (peer->connections,
1769 GCC_get_h (c), 1807 &GCC_get_id (c)->connection_of_tunnel,
1770 c)); 1808 c));
1771} 1809}
1772 1810
1773 1811
@@ -1791,13 +1829,13 @@ GCP_remove_connection (struct CadetPeer *peer,
1791 (NULL == peer->connections) ) 1829 (NULL == peer->connections) )
1792 return; 1830 return;
1793 GNUNET_assert (GNUNET_YES == 1831 GNUNET_assert (GNUNET_YES ==
1794 GNUNET_CONTAINER_multihashmap_remove (peer->connections, 1832 GNUNET_CONTAINER_multishortmap_remove (peer->connections,
1795 GCC_get_h (c), 1833 &GCC_get_id (c)->connection_of_tunnel,
1796 c)); 1834 c));
1797 LOG (GNUNET_ERROR_TYPE_DEBUG, 1835 LOG (GNUNET_ERROR_TYPE_DEBUG,
1798 "Peer %s remains with %u connections.\n", 1836 "Peer %s remains with %u connections.\n",
1799 GCP_2s (peer), 1837 GCP_2s (peer),
1800 GNUNET_CONTAINER_multihashmap_size (peer->connections)); 1838 GNUNET_CONTAINER_multishortmap_size (peer->connections));
1801} 1839}
1802 1840
1803 1841
diff --git a/src/cadet/gnunet-service-cadet_peer.h b/src/cadet/gnunet-service-cadet_peer.h
index 093cfa21a..1e206e10f 100644
--- a/src/cadet/gnunet-service-cadet_peer.h
+++ b/src/cadet/gnunet-service-cadet_peer.h
@@ -69,8 +69,12 @@ struct CadetPeerQueue;
69 */ 69 */
70typedef void 70typedef void
71(*GCP_sent) (void *cls, 71(*GCP_sent) (void *cls,
72 struct CadetConnection *c, int fwd, int sent, 72 struct CadetConnection *c,
73 uint16_t type, uint16_t payload_type, uint32_t pid, 73 int fwd,
74 int sent,
75 uint16_t type,
76 uint16_t payload_type,
77 struct CadetEncryptedMessageIdentifier pid,
74 size_t size, 78 size_t size,
75 struct GNUNET_TIME_Relative wait); 79 struct GNUNET_TIME_Relative wait);
76 80
@@ -122,6 +126,7 @@ GCP_shutdown (void);
122struct CadetPeer * 126struct CadetPeer *
123GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create); 127GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create);
124 128
129
125/** 130/**
126 * Retrieve the CadetPeer stucture associated with the peer. Optionally create 131 * Retrieve the CadetPeer stucture associated with the peer. Optionally create
127 * one and insert it in the appropriate structures if the peer is not known yet. 132 * one and insert it in the appropriate structures if the peer is not known yet.
@@ -136,6 +141,7 @@ GCP_get (const struct GNUNET_PeerIdentity *peer_id, int create);
136struct CadetPeer * 141struct CadetPeer *
137GCP_get_short (const GNUNET_PEER_Id peer, int create); 142GCP_get_short (const GNUNET_PEER_Id peer, int create);
138 143
144
139/** 145/**
140 * Try to establish a new connection to this peer (in its tunnel). 146 * Try to establish a new connection to this peer (in its tunnel).
141 * If the peer doesn't have any path to it yet, try to get one. 147 * If the peer doesn't have any path to it yet, try to get one.
@@ -164,7 +170,7 @@ struct CadetPeerQueue *
164GCP_send (struct CadetPeer *peer, 170GCP_send (struct CadetPeer *peer,
165 const struct GNUNET_MessageHeader *message, 171 const struct GNUNET_MessageHeader *message,
166 uint16_t payload_type, 172 uint16_t payload_type,
167 uint32_t payload_id, 173 struct CadetEncryptedMessageIdentifier payload_id,
168 struct CadetConnection *c, 174 struct CadetConnection *c,
169 int fwd, 175 int fwd,
170 GCP_sent cont, 176 GCP_sent cont,
@@ -440,7 +446,8 @@ GCP_iterate_paths (struct CadetPeer *peer,
440 * @param cls Closure for @c iter. 446 * @param cls Closure for @c iter.
441 */ 447 */
442void 448void
443GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls); 449GCP_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter,
450 void *cls);
444 451
445 452
446/** 453/**
diff --git a/src/cadet/gnunet-service-cadet_tunnel.c b/src/cadet/gnunet-service-cadet_tunnel.c
index 5b07e42c0..65775ce66 100644
--- a/src/cadet/gnunet-service-cadet_tunnel.c
+++ b/src/cadet/gnunet-service-cadet_tunnel.c
@@ -63,7 +63,7 @@ struct CadetTChannel
63 63
64 64
65/** 65/**
66 * Connection list and metadata. 66 * Entry in list of connections used by tunnel, with metadata.
67 */ 67 */
68struct CadetTConnection 68struct CadetTConnection
69{ 69{
@@ -243,6 +243,7 @@ struct CadetTunnelAxolotl
243 struct GNUNET_TIME_Absolute ratchet_expiration; 243 struct GNUNET_TIME_Absolute ratchet_expiration;
244}; 244};
245 245
246
246/** 247/**
247 * Struct containing all information regarding a tunnel to a peer. 248 * Struct containing all information regarding a tunnel to a peer.
248 */ 249 */
@@ -309,7 +310,7 @@ struct CadetTunnel
309 /** 310 /**
310 * Channel ID for the next created channel. 311 * Channel ID for the next created channel.
311 */ 312 */
312 CADET_ChannelNumber next_chid; 313 struct GNUNET_CADET_ChannelTunnelNumber next_chid;
313 314
314 /** 315 /**
315 * Destroy flag: if true, destroy on last message. 316 * Destroy flag: if true, destroy on last message.
@@ -648,7 +649,7 @@ new_ephemeral (struct CadetTunnel *t)
648static void 649static void
649t_hmac (const void *plaintext, size_t size, 650t_hmac (const void *plaintext, size_t size,
650 uint32_t iv, const struct GNUNET_CRYPTO_SymmetricSessionKey *key, 651 uint32_t iv, const struct GNUNET_CRYPTO_SymmetricSessionKey *key,
651 struct GNUNET_CADET_Hash *hmac) 652 struct GNUNET_ShortHashCode *hmac)
652{ 653{
653 static const char ctx[] = "cadet authentication key"; 654 static const char ctx[] = "cadet authentication key";
654 struct GNUNET_CRYPTO_AuthKey auth_key; 655 struct GNUNET_CRYPTO_AuthKey auth_key;
@@ -842,7 +843,7 @@ t_ax_decrypt (struct CadetTunnel *t, void *dst, const void *src, size_t size)
842 * @param msg Message whose header to encrypt. 843 * @param msg Message whose header to encrypt.
843 */ 844 */
844static void 845static void
845t_h_encrypt (struct CadetTunnel *t, struct GNUNET_CADET_Encrypted *msg) 846t_h_encrypt (struct CadetTunnel *t, struct GNUNET_CADET_TunnelEncryptedMessage *msg)
846{ 847{
847 struct GNUNET_CRYPTO_SymmetricInitializationVector iv; 848 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
848 struct CadetTunnelAxolotl *ax; 849 struct CadetTunnelAxolotl *ax;
@@ -873,8 +874,8 @@ t_h_encrypt (struct CadetTunnel *t, struct GNUNET_CADET_Encrypted *msg)
873 * @param dst Where to decrypt header to. 874 * @param dst Where to decrypt header to.
874 */ 875 */
875static void 876static void
876t_h_decrypt (struct CadetTunnel *t, const struct GNUNET_CADET_Encrypted *src, 877t_h_decrypt (struct CadetTunnel *t, const struct GNUNET_CADET_TunnelEncryptedMessage *src,
877 struct GNUNET_CADET_Encrypted *dst) 878 struct GNUNET_CADET_TunnelEncryptedMessage *dst)
878{ 879{
879 struct GNUNET_CRYPTO_SymmetricInitializationVector iv; 880 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
880 struct CadetTunnelAxolotl *ax; 881 struct CadetTunnelAxolotl *ax;
@@ -912,12 +913,12 @@ t_h_decrypt (struct CadetTunnel *t, const struct GNUNET_CADET_Encrypted *src,
912 */ 913 */
913static int 914static int
914try_old_ax_keys (struct CadetTunnel *t, void *dst, 915try_old_ax_keys (struct CadetTunnel *t, void *dst,
915 const struct GNUNET_CADET_Encrypted *src, size_t size) 916 const struct GNUNET_CADET_TunnelEncryptedMessage *src, size_t size)
916{ 917{
917 struct CadetTunnelSkippedKey *key; 918 struct CadetTunnelSkippedKey *key;
918 struct GNUNET_CADET_Hash *hmac; 919 struct GNUNET_ShortHashCode *hmac;
919 struct GNUNET_CRYPTO_SymmetricInitializationVector iv; 920 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
920 struct GNUNET_CADET_Encrypted plaintext_header; 921 struct GNUNET_CADET_TunnelEncryptedMessage plaintext_header;
921 struct GNUNET_CRYPTO_SymmetricSessionKey *valid_HK; 922 struct GNUNET_CRYPTO_SymmetricSessionKey *valid_HK;
922 size_t esize; 923 size_t esize;
923 size_t res; 924 size_t res;
@@ -926,7 +927,7 @@ try_old_ax_keys (struct CadetTunnel *t, void *dst,
926 927
927 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trying old keys\n"); 928 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trying old keys\n");
928 hmac = &plaintext_header.hmac; 929 hmac = &plaintext_header.hmac;
929 esize = size - sizeof (struct GNUNET_CADET_Encrypted); 930 esize = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
930 931
931 /* Find a correct Header Key */ 932 /* Find a correct Header Key */
932 for (key = t->ax->skipped_head; NULL != key; key = key->next) 933 for (key = t->ax->skipped_head; NULL != key; key = key->next)
@@ -947,8 +948,8 @@ try_old_ax_keys (struct CadetTunnel *t, void *dst,
947 return -1; 948 return -1;
948 949
949 /* Should've been checked in -cadet_connection.c handle_cadet_encrypted. */ 950 /* Should've been checked in -cadet_connection.c handle_cadet_encrypted. */
950 GNUNET_assert (size > sizeof (struct GNUNET_CADET_Encrypted)); 951 GNUNET_assert (size > sizeof (struct GNUNET_CADET_TunnelEncryptedMessage));
951 len = size - sizeof (struct GNUNET_CADET_Encrypted); 952 len = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
952 GNUNET_assert (len >= sizeof (struct GNUNET_MessageHeader)); 953 GNUNET_assert (len >= sizeof (struct GNUNET_MessageHeader));
953 954
954 /* Decrypt header */ 955 /* Decrypt header */
@@ -1091,19 +1092,19 @@ store_ax_keys (struct CadetTunnel *t,
1091 */ 1092 */
1092static int 1093static int
1093t_ax_decrypt_and_validate (struct CadetTunnel *t, void *dst, 1094t_ax_decrypt_and_validate (struct CadetTunnel *t, void *dst,
1094 const struct GNUNET_CADET_Encrypted *src, 1095 const struct GNUNET_CADET_TunnelEncryptedMessage *src,
1095 size_t size) 1096 size_t size)
1096{ 1097{
1097 struct CadetTunnelAxolotl *ax; 1098 struct CadetTunnelAxolotl *ax;
1098 struct GNUNET_CADET_Hash msg_hmac; 1099 struct GNUNET_ShortHashCode msg_hmac;
1099 struct GNUNET_HashCode hmac; 1100 struct GNUNET_HashCode hmac;
1100 struct GNUNET_CADET_Encrypted plaintext_header; 1101 struct GNUNET_CADET_TunnelEncryptedMessage plaintext_header;
1101 uint32_t Np; 1102 uint32_t Np;
1102 uint32_t PNp; 1103 uint32_t PNp;
1103 size_t esize; /* Size of encryped payload */ 1104 size_t esize; /* Size of encryped payload */
1104 size_t osize; /* Size of output (decrypted payload) */ 1105 size_t osize; /* Size of output (decrypted payload) */
1105 1106
1106 esize = size - sizeof (struct GNUNET_CADET_Encrypted); 1107 esize = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
1107 ax = t->ax; 1108 ax = t->ax;
1108 if (NULL == ax) 1109 if (NULL == ax)
1109 return -1; 1110 return -1;
@@ -1321,12 +1322,11 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1321 struct CadetTunnelQueue *existing_q) 1322 struct CadetTunnelQueue *existing_q)
1322{ 1323{
1323 struct GNUNET_MessageHeader *msg; 1324 struct GNUNET_MessageHeader *msg;
1324 struct GNUNET_CADET_Encrypted *ax_msg; 1325 struct GNUNET_CADET_TunnelEncryptedMessage *ax_msg;
1325 struct CadetTunnelQueue *tq; 1326 struct CadetTunnelQueue *tq;
1326 size_t size = ntohs (message->size); 1327 size_t size = ntohs (message->size);
1327 char cbuf[sizeof (struct GNUNET_CADET_Encrypted) + size] GNUNET_ALIGN; 1328 char cbuf[sizeof (struct GNUNET_CADET_TunnelEncryptedMessage) + size] GNUNET_ALIGN;
1328 size_t esize; 1329 size_t esize;
1329 uint32_t mid;
1330 uint16_t type; 1330 uint16_t type;
1331 int fwd; 1331 int fwd;
1332 1332
@@ -1352,10 +1352,10 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1352 1352
1353 GNUNET_assert (GNUNET_NO == GCT_is_loopback (t)); 1353 GNUNET_assert (GNUNET_NO == GCT_is_loopback (t));
1354 1354
1355 ax_msg = (struct GNUNET_CADET_Encrypted *) cbuf; 1355 ax_msg = (struct GNUNET_CADET_TunnelEncryptedMessage *) cbuf;
1356 msg = &ax_msg->header; 1356 msg = &ax_msg->header;
1357 msg->size = htons (sizeof (struct GNUNET_CADET_Encrypted) + size); 1357 msg->size = htons (sizeof (struct GNUNET_CADET_TunnelEncryptedMessage) + size);
1358 msg->type = htons (GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED); 1358 msg->type = htons (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED);
1359 esize = t_ax_encrypt (t, &ax_msg[1], message, size); 1359 esize = t_ax_encrypt (t, &ax_msg[1], message, size);
1360 ax_msg->Ns = htonl (t->ax->Ns++); 1360 ax_msg->Ns = htonl (t->ax->Ns++);
1361 ax_msg->PNs = htonl (t->ax->PNs); 1361 ax_msg->PNs = htonl (t->ax->PNs);
@@ -1378,34 +1378,22 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1378 } 1378 }
1379 fwd = GCC_is_origin (c, GNUNET_YES); 1379 fwd = GCC_is_origin (c, GNUNET_YES);
1380 ax_msg->cid = *GCC_get_id (c); 1380 ax_msg->cid = *GCC_get_id (c);
1381 ax_msg->pid = htonl (GCC_get_pid (c, fwd)); 1381 ax_msg->cemi = GCC_get_pid (c, fwd);
1382 1382
1383 mid = 0; 1383 type = htons (message->type);
1384 type = ntohs (message->type); 1384 LOG (GNUNET_ERROR_TYPE_DEBUG,
1385 switch (type) 1385 "Sending message of type %s with CEMI %u and CID %s\n",
1386 { 1386 GC_m2s (type),
1387 case GNUNET_MESSAGE_TYPE_CADET_DATA: 1387 htonl (ax_msg->cemi.pid),
1388 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK: 1388 GNUNET_sh2s (&ax_msg->cid.connection_of_tunnel));
1389 if (GNUNET_MESSAGE_TYPE_CADET_DATA == type)
1390 mid = ntohl (((struct GNUNET_CADET_Data *) message)->mid);
1391 else
1392 mid = ntohl (((struct GNUNET_CADET_DataACK *) message)->mid);
1393 /* Fall thru */
1394 case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
1395 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
1396 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1397 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
1398 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
1399 break;
1400 default:
1401 GNUNET_break (0);
1402 LOG (GNUNET_ERROR_TYPE_ERROR, "type %s not valid\n", GC_m2s (type));
1403 }
1404 LOG (GNUNET_ERROR_TYPE_DEBUG, "type %s\n", GC_m2s (type));
1405 1389
1406 if (NULL == cont) 1390 if (NULL == cont)
1407 { 1391 {
1408 (void) GCC_send_prebuilt_message (msg, type, mid, c, fwd, 1392 (void) GCC_send_prebuilt_message (msg,
1393 type,
1394 ax_msg->cemi,
1395 c,
1396 fwd,
1409 force, NULL, NULL); 1397 force, NULL, NULL);
1410 return NULL; 1398 return NULL;
1411 } 1399 }
@@ -1418,11 +1406,16 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1418 tq = existing_q; 1406 tq = existing_q;
1419 tq->tqd = NULL; 1407 tq->tqd = NULL;
1420 } 1408 }
1421 tq->cq = GCC_send_prebuilt_message (msg, type, mid, c, fwd, force,
1422 &tun_message_sent, tq);
1423 GNUNET_assert (NULL != tq->cq);
1424 tq->cont = cont; 1409 tq->cont = cont;
1425 tq->cont_cls = cont_cls; 1410 tq->cont_cls = cont_cls;
1411 tq->cq = GCC_send_prebuilt_message (msg,
1412 type,
1413 ax_msg->cemi,
1414 c,
1415 fwd,
1416 force,
1417 &tun_message_sent, tq);
1418 GNUNET_assert (NULL != tq->cq);
1426 1419
1427 return tq; 1420 return tq;
1428} 1421}
@@ -1562,17 +1555,19 @@ destroy_iterator (void *cls,
1562 * @param gid ID of the channel. 1555 * @param gid ID of the channel.
1563 */ 1556 */
1564static void 1557static void
1565send_channel_destroy (struct CadetTunnel *t, unsigned int gid) 1558send_channel_destroy (struct CadetTunnel *t,
1559 struct GNUNET_CADET_ChannelTunnelNumber gid)
1566{ 1560{
1567 struct GNUNET_CADET_ChannelManage msg; 1561 struct GNUNET_CADET_ChannelManageMessage msg;
1568 1562
1569 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY); 1563 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
1570 msg.header.size = htons (sizeof (msg)); 1564 msg.header.size = htons (sizeof (msg));
1571 msg.chid = htonl (gid); 1565 msg.chid = gid;
1572 1566
1573 LOG (GNUNET_ERROR_TYPE_DEBUG, 1567 LOG (GNUNET_ERROR_TYPE_DEBUG,
1574 "WARNING destroying unknown channel %u on tunnel %s\n", 1568 "WARNING destroying unknown channel %u on tunnel %s\n",
1575 gid, GCT_2s (t)); 1569 ntohl (gid.cn),
1570 GCT_2s (t));
1576 send_prebuilt_message (&msg.header, t, NULL, GNUNET_YES, NULL, NULL, NULL); 1571 send_prebuilt_message (&msg.header, t, NULL, GNUNET_YES, NULL, NULL, NULL);
1577} 1572}
1578 1573
@@ -1589,7 +1584,7 @@ send_channel_destroy (struct CadetTunnel *t, unsigned int gid)
1589 */ 1584 */
1590static void 1585static void
1591handle_data (struct CadetTunnel *t, 1586handle_data (struct CadetTunnel *t,
1592 const struct GNUNET_CADET_Data *msg, 1587 const struct GNUNET_CADET_ChannelAppDataMessage *msg,
1593 int fwd) 1588 int fwd)
1594{ 1589{
1595 struct CadetChannel *ch; 1590 struct CadetChannel *ch;
@@ -1600,7 +1595,7 @@ handle_data (struct CadetTunnel *t,
1600 /* Check size */ 1595 /* Check size */
1601 size = ntohs (msg->header.size); 1596 size = ntohs (msg->header.size);
1602 if (size < 1597 if (size <
1603 sizeof (struct GNUNET_CADET_Data) + 1598 sizeof (struct GNUNET_CADET_ChannelAppDataMessage) +
1604 sizeof (struct GNUNET_MessageHeader)) 1599 sizeof (struct GNUNET_MessageHeader))
1605 { 1600 {
1606 GNUNET_break (0); 1601 GNUNET_break (0);
@@ -1613,13 +1608,17 @@ handle_data (struct CadetTunnel *t,
1613 1608
1614 1609
1615 /* Check channel */ 1610 /* Check channel */
1616 ch = GCT_get_channel (t, ntohl (msg->chid)); 1611 ch = GCT_get_channel (t, msg->chid);
1617 if (NULL == ch) 1612 if (NULL == ch)
1618 { 1613 {
1619 GNUNET_STATISTICS_update (stats, "# data on unknown channel", 1614 GNUNET_STATISTICS_update (stats,
1620 1, GNUNET_NO); 1615 "# data on unknown channel",
1621 LOG (GNUNET_ERROR_TYPE_DEBUG, "channel 0x%X unknown\n", ntohl (msg->chid)); 1616 1,
1622 send_channel_destroy (t, ntohl (msg->chid)); 1617 GNUNET_NO);
1618 LOG (GNUNET_ERROR_TYPE_DEBUG,
1619 "channel 0x%X unknown\n",
1620 ntohl (msg->chid.cn));
1621 send_channel_destroy (t, msg->chid);
1623 return; 1622 return;
1624 } 1623 }
1625 1624
@@ -1639,7 +1638,7 @@ handle_data (struct CadetTunnel *t,
1639 */ 1638 */
1640static void 1639static void
1641handle_data_ack (struct CadetTunnel *t, 1640handle_data_ack (struct CadetTunnel *t,
1642 const struct GNUNET_CADET_DataACK *msg, 1641 const struct GNUNET_CADET_ChannelDataAckMessage *msg,
1643 int fwd) 1642 int fwd)
1644{ 1643{
1645 struct CadetChannel *ch; 1644 struct CadetChannel *ch;
@@ -1647,20 +1646,20 @@ handle_data_ack (struct CadetTunnel *t,
1647 1646
1648 /* Check size */ 1647 /* Check size */
1649 size = ntohs (msg->header.size); 1648 size = ntohs (msg->header.size);
1650 if (size != sizeof (struct GNUNET_CADET_DataACK)) 1649 if (size != sizeof (struct GNUNET_CADET_ChannelDataAckMessage))
1651 { 1650 {
1652 GNUNET_break (0); 1651 GNUNET_break (0);
1653 return; 1652 return;
1654 } 1653 }
1655 1654
1656 /* Check channel */ 1655 /* Check channel */
1657 ch = GCT_get_channel (t, ntohl (msg->chid)); 1656 ch = GCT_get_channel (t, msg->chid);
1658 if (NULL == ch) 1657 if (NULL == ch)
1659 { 1658 {
1660 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel", 1659 GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
1661 1, GNUNET_NO); 1660 1, GNUNET_NO);
1662 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n", 1661 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
1663 ntohl (msg->chid)); 1662 ntohl (msg->chid.cn));
1664 return; 1663 return;
1665 } 1664 }
1666 1665
@@ -1676,21 +1675,21 @@ handle_data_ack (struct CadetTunnel *t,
1676 */ 1675 */
1677static void 1676static void
1678handle_ch_create (struct CadetTunnel *t, 1677handle_ch_create (struct CadetTunnel *t,
1679 const struct GNUNET_CADET_ChannelCreate *msg) 1678 const struct GNUNET_CADET_ChannelOpenMessage *msg)
1680{ 1679{
1681 struct CadetChannel *ch; 1680 struct CadetChannel *ch;
1682 size_t size; 1681 size_t size;
1683 1682
1684 /* Check size */ 1683 /* Check size */
1685 size = ntohs (msg->header.size); 1684 size = ntohs (msg->header.size);
1686 if (size != sizeof (struct GNUNET_CADET_ChannelCreate)) 1685 if (size != sizeof (struct GNUNET_CADET_ChannelOpenMessage))
1687 { 1686 {
1688 GNUNET_break_op (0); 1687 GNUNET_break_op (0);
1689 return; 1688 return;
1690 } 1689 }
1691 1690
1692 /* Check channel */ 1691 /* Check channel */
1693 ch = GCT_get_channel (t, ntohl (msg->chid)); 1692 ch = GCT_get_channel (t, msg->chid);
1694 if (NULL != ch && ! GCT_is_loopback (t)) 1693 if (NULL != ch && ! GCT_is_loopback (t))
1695 { 1694 {
1696 /* Probably a retransmission, safe to ignore */ 1695 /* Probably a retransmission, safe to ignore */
@@ -1711,27 +1710,28 @@ handle_ch_create (struct CadetTunnel *t,
1711 */ 1710 */
1712static void 1711static void
1713handle_ch_nack (struct CadetTunnel *t, 1712handle_ch_nack (struct CadetTunnel *t,
1714 const struct GNUNET_CADET_ChannelManage *msg) 1713 const struct GNUNET_CADET_ChannelManageMessage *msg)
1715{ 1714{
1716 struct CadetChannel *ch; 1715 struct CadetChannel *ch;
1717 size_t size; 1716 size_t size;
1718 1717
1719 /* Check size */ 1718 /* Check size */
1720 size = ntohs (msg->header.size); 1719 size = ntohs (msg->header.size);
1721 if (size != sizeof (struct GNUNET_CADET_ChannelManage)) 1720 if (size != sizeof (struct GNUNET_CADET_ChannelManageMessage))
1722 { 1721 {
1723 GNUNET_break (0); 1722 GNUNET_break (0);
1724 return; 1723 return;
1725 } 1724 }
1726 1725
1727 /* Check channel */ 1726 /* Check channel */
1728 ch = GCT_get_channel (t, ntohl (msg->chid)); 1727 ch = GCT_get_channel (t, msg->chid);
1729 if (NULL == ch) 1728 if (NULL == ch)
1730 { 1729 {
1731 GNUNET_STATISTICS_update (stats, "# channel NACK on unknown channel", 1730 GNUNET_STATISTICS_update (stats, "# channel NACK on unknown channel",
1732 1, GNUNET_NO); 1731 1, GNUNET_NO);
1733 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n", 1732 LOG (GNUNET_ERROR_TYPE_DEBUG,
1734 ntohl (msg->chid)); 1733 "WARNING channel %u unknown\n",
1734 ntohl (msg->chid.cn));
1735 return; 1735 return;
1736 } 1736 }
1737 1737
@@ -1751,7 +1751,7 @@ handle_ch_nack (struct CadetTunnel *t,
1751 */ 1751 */
1752static void 1752static void
1753handle_ch_ack (struct CadetTunnel *t, 1753handle_ch_ack (struct CadetTunnel *t,
1754 const struct GNUNET_CADET_ChannelManage *msg, 1754 const struct GNUNET_CADET_ChannelManageMessage *msg,
1755 int fwd) 1755 int fwd)
1756{ 1756{
1757 struct CadetChannel *ch; 1757 struct CadetChannel *ch;
@@ -1759,20 +1759,23 @@ handle_ch_ack (struct CadetTunnel *t,
1759 1759
1760 /* Check size */ 1760 /* Check size */
1761 size = ntohs (msg->header.size); 1761 size = ntohs (msg->header.size);
1762 if (size != sizeof (struct GNUNET_CADET_ChannelManage)) 1762 if (size != sizeof (struct GNUNET_CADET_ChannelManageMessage))
1763 { 1763 {
1764 GNUNET_break (0); 1764 GNUNET_break (0);
1765 return; 1765 return;
1766 } 1766 }
1767 1767
1768 /* Check channel */ 1768 /* Check channel */
1769 ch = GCT_get_channel (t, ntohl (msg->chid)); 1769 ch = GCT_get_channel (t, msg->chid);
1770 if (NULL == ch) 1770 if (NULL == ch)
1771 { 1771 {
1772 GNUNET_STATISTICS_update (stats, "# channel ack on unknown channel", 1772 GNUNET_STATISTICS_update (stats,
1773 1, GNUNET_NO); 1773 "# channel ack on unknown channel",
1774 LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n", 1774 1,
1775 ntohl (msg->chid)); 1775 GNUNET_NO);
1776 LOG (GNUNET_ERROR_TYPE_DEBUG,
1777 "WARNING channel %u unknown\n",
1778 ntohl (msg->chid.cn));
1776 return; 1779 return;
1777 } 1780 }
1778 1781
@@ -1792,7 +1795,7 @@ handle_ch_ack (struct CadetTunnel *t,
1792 */ 1795 */
1793static void 1796static void
1794handle_ch_destroy (struct CadetTunnel *t, 1797handle_ch_destroy (struct CadetTunnel *t,
1795 const struct GNUNET_CADET_ChannelManage *msg, 1798 const struct GNUNET_CADET_ChannelManageMessage *msg,
1796 int fwd) 1799 int fwd)
1797{ 1800{
1798 struct CadetChannel *ch; 1801 struct CadetChannel *ch;
@@ -1800,14 +1803,14 @@ handle_ch_destroy (struct CadetTunnel *t,
1800 1803
1801 /* Check size */ 1804 /* Check size */
1802 size = ntohs (msg->header.size); 1805 size = ntohs (msg->header.size);
1803 if (size != sizeof (struct GNUNET_CADET_ChannelManage)) 1806 if (size != sizeof (struct GNUNET_CADET_ChannelManageMessage))
1804 { 1807 {
1805 GNUNET_break (0); 1808 GNUNET_break (0);
1806 return; 1809 return;
1807 } 1810 }
1808 1811
1809 /* Check channel */ 1812 /* Check channel */
1810 ch = GCT_get_channel (t, ntohl (msg->chid)); 1813 ch = GCT_get_channel (t, msg->chid);
1811 if (NULL == ch) 1814 if (NULL == ch)
1812 { 1815 {
1813 /* Probably a retransmission, safe to ignore */ 1816 /* Probably a retransmission, safe to ignore */
@@ -1877,34 +1880,34 @@ handle_decrypted (struct CadetTunnel *t,
1877 1880
1878 switch (type) 1881 switch (type)
1879 { 1882 {
1880 case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE: 1883 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE:
1881 /* Do nothing, connection aleady got updated. */ 1884 /* Do nothing, connection aleady got updated. */
1882 GNUNET_STATISTICS_update (stats, "# keepalives received", 1, GNUNET_NO); 1885 GNUNET_STATISTICS_update (stats, "# keepalives received", 1, GNUNET_NO);
1883 break; 1886 break;
1884 1887
1885 case GNUNET_MESSAGE_TYPE_CADET_DATA: 1888 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA:
1886 /* Don't send hop ACK, wait for client to ACK */ 1889 /* Don't send hop ACK, wait for client to ACK */
1887 handle_data (t, (struct GNUNET_CADET_Data *) msgh, fwd); 1890 handle_data (t, (struct GNUNET_CADET_ChannelAppDataMessage *) msgh, fwd);
1888 break; 1891 break;
1889 1892
1890 case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK: 1893 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK:
1891 handle_data_ack (t, (struct GNUNET_CADET_DataACK *) msgh, fwd); 1894 handle_data_ack (t, (struct GNUNET_CADET_ChannelDataAckMessage *) msgh, fwd);
1892 break; 1895 break;
1893 1896
1894 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE: 1897 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
1895 handle_ch_create (t, (struct GNUNET_CADET_ChannelCreate *) msgh); 1898 handle_ch_create (t, (struct GNUNET_CADET_ChannelOpenMessage *) msgh);
1896 break; 1899 break;
1897 1900
1898 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK: 1901 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED:
1899 handle_ch_nack (t, (struct GNUNET_CADET_ChannelManage *) msgh); 1902 handle_ch_nack (t, (struct GNUNET_CADET_ChannelManageMessage *) msgh);
1900 break; 1903 break;
1901 1904
1902 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK: 1905 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK:
1903 handle_ch_ack (t, (struct GNUNET_CADET_ChannelManage *) msgh, fwd); 1906 handle_ch_ack (t, (struct GNUNET_CADET_ChannelManageMessage *) msgh, fwd);
1904 break; 1907 break;
1905 1908
1906 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY: 1909 case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
1907 handle_ch_destroy (t, (struct GNUNET_CADET_ChannelManage *) msgh, fwd); 1910 handle_ch_destroy (t, (struct GNUNET_CADET_ChannelManageMessage *) msgh, fwd);
1908 break; 1911 break;
1909 1912
1910 default: 1913 default:
@@ -1931,7 +1934,7 @@ handle_decrypted (struct CadetTunnel *t,
1931 */ 1934 */
1932void 1935void
1933GCT_handle_encrypted (struct CadetTunnel *t, 1936GCT_handle_encrypted (struct CadetTunnel *t,
1934 const struct GNUNET_CADET_Encrypted *msg) 1937 const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
1935{ 1938{
1936 uint16_t size = ntohs (msg->header.size); 1939 uint16_t size = ntohs (msg->header.size);
1937 char cbuf [size]; 1940 char cbuf [size];
@@ -1990,7 +1993,7 @@ GCT_handle_encrypted (struct CadetTunnel *t,
1990 */ 1993 */
1991void 1994void
1992GCT_handle_kx (struct CadetTunnel *t, 1995GCT_handle_kx (struct CadetTunnel *t,
1993 const struct GNUNET_CADET_KX *msg) 1996 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg)
1994{ 1997{
1995 struct CadetTunnelAxolotl *ax; 1998 struct CadetTunnelAxolotl *ax;
1996 struct GNUNET_HashCode key_material[3]; 1999 struct GNUNET_HashCode key_material[3];
@@ -2143,9 +2146,9 @@ GCT_init (const struct GNUNET_CONFIGURATION_Handle *c,
2143 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n"); 2146 LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
2144 2147
2145 expected_overhead = 0; 2148 expected_overhead = 0;
2146 expected_overhead += sizeof (struct GNUNET_CADET_Encrypted); 2149 expected_overhead += sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
2147 expected_overhead += sizeof (struct GNUNET_CADET_Data); 2150 expected_overhead += sizeof (struct GNUNET_CADET_ChannelAppDataMessage);
2148 expected_overhead += sizeof (struct GNUNET_CADET_ACK); 2151 expected_overhead += sizeof (struct GNUNET_CADET_ConnectionEncryptedAckMessage);
2149 GNUNET_assert (GNUNET_CONSTANTS_CADET_P2P_OVERHEAD == expected_overhead); 2152 GNUNET_assert (GNUNET_CONSTANTS_CADET_P2P_OVERHEAD == expected_overhead);
2150 2153
2151 if (GNUNET_OK != 2154 if (GNUNET_OK !=
@@ -2200,7 +2203,7 @@ GCT_new (struct CadetPeer *destination)
2200 struct CadetTunnel *t; 2203 struct CadetTunnel *t;
2201 2204
2202 t = GNUNET_new (struct CadetTunnel); 2205 t = GNUNET_new (struct CadetTunnel);
2203 t->next_chid = 0; 2206 t->next_chid.cn = 0;
2204 t->peer = destination; 2207 t->peer = destination;
2205 2208
2206 if (GNUNET_OK != 2209 if (GNUNET_OK !=
@@ -2442,7 +2445,8 @@ GCT_remove_connection (struct CadetTunnel *t,
2442 * @param ch Channel. 2445 * @param ch Channel.
2443 */ 2446 */
2444void 2447void
2445GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch) 2448GCT_add_channel (struct CadetTunnel *t,
2449 struct CadetChannel *ch)
2446{ 2450{
2447 struct CadetTChannel *aux; 2451 struct CadetTChannel *aux;
2448 2452
@@ -2510,7 +2514,8 @@ GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch)
2510 * @return channel handler, NULL if doesn't exist 2514 * @return channel handler, NULL if doesn't exist
2511 */ 2515 */
2512struct CadetChannel * 2516struct CadetChannel *
2513GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid) 2517GCT_get_channel (struct CadetTunnel *t,
2518 struct GNUNET_CADET_ChannelTunnelNumber chid)
2514{ 2519{
2515 struct CadetTChannel *iter; 2520 struct CadetTChannel *iter;
2516 2521
@@ -2519,7 +2524,7 @@ GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid)
2519 2524
2520 for (iter = t->channel_head; NULL != iter; iter = iter->next) 2525 for (iter = t->channel_head; NULL != iter; iter = iter->next)
2521 { 2526 {
2522 if (GCCH_get_id (iter->ch) == chid) 2527 if (GCCH_get_id (iter->ch).cn == chid.cn)
2523 break; 2528 break;
2524 } 2529 }
2525 2530
@@ -2659,7 +2664,7 @@ GCT_destroy (struct CadetTunnel *t)
2659 2664
2660 mh = (struct GNUNET_MessageHeader *) &t->tq_head[1]; 2665 mh = (struct GNUNET_MessageHeader *) &t->tq_head[1];
2661 type = ntohs (mh->type); 2666 type = ntohs (mh->type);
2662 if (0 == keepalives_queued && GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE == type) 2667 if (0 == keepalives_queued && GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE == type)
2663 { 2668 {
2664 keepalives_queued = 1; 2669 keepalives_queued = 1;
2665 LOG (GNUNET_ERROR_TYPE_DEBUG, 2670 LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -2727,7 +2732,7 @@ struct CadetConnection *
2727GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *path) 2732GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *path)
2728{ 2733{
2729 struct CadetConnection *c; 2734 struct CadetConnection *c;
2730 struct GNUNET_CADET_Hash cid; 2735 struct GNUNET_CADET_ConnectionTunnelIdentifier cid;
2731 unsigned int own_pos; 2736 unsigned int own_pos;
2732 2737
2733 if (NULL == t || NULL == path) 2738 if (NULL == t || NULL == path)
@@ -2966,11 +2971,11 @@ GCT_get_destination (struct CadetTunnel *t)
2966 * 2971 *
2967 * @return GID of a channel free to use. 2972 * @return GID of a channel free to use.
2968 */ 2973 */
2969CADET_ChannelNumber 2974struct GNUNET_CADET_ChannelTunnelNumber
2970GCT_get_next_chid (struct CadetTunnel *t) 2975GCT_get_next_chid (struct CadetTunnel *t)
2971{ 2976{
2972 CADET_ChannelNumber chid; 2977 struct GNUNET_CADET_ChannelTunnelNumber chid;
2973 CADET_ChannelNumber mask; 2978 struct GNUNET_CADET_ChannelTunnelNumber mask;
2974 int result; 2979 int result;
2975 2980
2976 /* Set bit 30 depending on the ID relationship. Bit 31 is always 0 for GID. 2981 /* Set bit 30 depending on the ID relationship. Bit 31 is always 0 for GID.
@@ -2979,20 +2984,22 @@ GCT_get_next_chid (struct CadetTunnel *t)
2979 */ 2984 */
2980 result = GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, GCP_get_id (t->peer)); 2985 result = GNUNET_CRYPTO_cmp_peer_identity (&my_full_id, GCP_get_id (t->peer));
2981 if (0 > result) 2986 if (0 > result)
2982 mask = 0x40000000; 2987 mask.cn = htonl (0x40000000);
2983 else 2988 else
2984 mask = 0x0; 2989 mask.cn = 0x0;
2985 t->next_chid |= mask; 2990 t->next_chid.cn |= mask.cn;
2986 2991
2987 while (NULL != GCT_get_channel (t, t->next_chid)) 2992 while (NULL != GCT_get_channel (t, t->next_chid))
2988 { 2993 {
2989 LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel %u exists...\n", t->next_chid); 2994 LOG (GNUNET_ERROR_TYPE_DEBUG,
2990 t->next_chid = (t->next_chid + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI; 2995 "Channel %u exists...\n",
2991 t->next_chid |= mask; 2996 t->next_chid.cn);
2997 t->next_chid.cn = htonl ((ntohl (t->next_chid.cn) + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
2998 t->next_chid.cn |= mask.cn;
2992 } 2999 }
2993 chid = t->next_chid; 3000 chid = t->next_chid;
2994 t->next_chid = (t->next_chid + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI; 3001 t->next_chid.cn = (t->next_chid.cn + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
2995 t->next_chid |= mask; 3002 t->next_chid.cn |= mask.cn;
2996 3003
2997 return chid; 3004 return chid;
2998} 3005}
@@ -3185,7 +3192,8 @@ GCT_has_queued_traffic (struct CadetTunnel *t)
3185 */ 3192 */
3186struct CadetTunnelQueue * 3193struct CadetTunnelQueue *
3187GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message, 3194GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3188 struct CadetTunnel *t, struct CadetConnection *c, 3195 struct CadetTunnel *t,
3196 struct CadetConnection *c,
3189 int force, GCT_sent cont, void *cont_cls) 3197 int force, GCT_sent cont, void *cont_cls)
3190{ 3198{
3191 return send_prebuilt_message (message, t, c, force, cont, cont_cls, NULL); 3199 return send_prebuilt_message (message, t, c, force, cont, cont_cls, NULL);
@@ -3201,8 +3209,9 @@ GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3201void 3209void
3202GCT_send_kx (struct CadetTunnel *t, int force_reply) 3210GCT_send_kx (struct CadetTunnel *t, int force_reply)
3203{ 3211{
3212 static struct CadetEncryptedMessageIdentifier zero;
3204 struct CadetConnection *c; 3213 struct CadetConnection *c;
3205 struct GNUNET_CADET_KX msg; 3214 struct GNUNET_CADET_TunnelKeyExchangeMessage msg;
3206 enum GNUNET_CADET_KX_Flags flags; 3215 enum GNUNET_CADET_KX_Flags flags;
3207 3216
3208 LOG (GNUNET_ERROR_TYPE_INFO, "==> { KX} on %s\n", GCT_2s (t)); 3217 LOG (GNUNET_ERROR_TYPE_INFO, "==> { KX} on %s\n", GCT_2s (t));
@@ -3225,7 +3234,7 @@ GCT_send_kx (struct CadetTunnel *t, int force_reply)
3225 } 3234 }
3226 3235
3227 msg.header.size = htons (sizeof (msg)); 3236 msg.header.size = htons (sizeof (msg));
3228 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_KX); 3237 msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX);
3229 flags = GNUNET_CADET_KX_FLAG_NONE; 3238 flags = GNUNET_CADET_KX_FLAG_NONE;
3230 if (GNUNET_YES == force_reply) 3239 if (GNUNET_YES == force_reply)
3231 flags |= GNUNET_CADET_KX_FLAG_FORCE_REPLY; 3240 flags |= GNUNET_CADET_KX_FLAG_FORCE_REPLY;
@@ -3234,8 +3243,11 @@ GCT_send_kx (struct CadetTunnel *t, int force_reply)
3234 GNUNET_CRYPTO_ecdhe_key_get_public (t->ax->kx_0, &msg.ephemeral_key); 3243 GNUNET_CRYPTO_ecdhe_key_get_public (t->ax->kx_0, &msg.ephemeral_key);
3235 GNUNET_CRYPTO_ecdhe_key_get_public (t->ax->DHRs, &msg.ratchet_key); 3244 GNUNET_CRYPTO_ecdhe_key_get_public (t->ax->DHRs, &msg.ratchet_key);
3236 3245
3237 t->ephm_h = GCC_send_prebuilt_message (&msg.header, UINT16_MAX, 0, 3246 t->ephm_h = GCC_send_prebuilt_message (&msg.header,
3238 c, GCC_is_origin (c, GNUNET_YES), 3247 UINT16_MAX,
3248 zero,
3249 c,
3250 GCC_is_origin (c, GNUNET_YES),
3239 GNUNET_YES, &ephm_sent, t); 3251 GNUNET_YES, &ephm_sent, t);
3240 if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate) 3252 if (CADET_TUNNEL_KEY_UNINITIALIZED == t->estate)
3241 GCT_change_estate (t, CADET_TUNNEL_KEY_SENT); 3253 GCT_change_estate (t, CADET_TUNNEL_KEY_SENT);
@@ -3486,4 +3498,3 @@ GCT_iterate_channels (struct CadetTunnel *t, GCT_chan_iter iter, void *cls)
3486 for (cht = t->channel_head; NULL != cht; cht = cht->next) 3498 for (cht = t->channel_head; NULL != cht; cht = cht->next)
3487 iter (cls, cht->ch); 3499 iter (cls, cht->ch);
3488} 3500}
3489
diff --git a/src/cadet/gnunet-service-cadet_tunnel.h b/src/cadet/gnunet-service-cadet_tunnel.h
index e3ca57e9c..c10815a3b 100644
--- a/src/cadet/gnunet-service-cadet_tunnel.h
+++ b/src/cadet/gnunet-service-cadet_tunnel.h
@@ -291,7 +291,7 @@ GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch);
291 * @return channel handler, NULL if doesn't exist 291 * @return channel handler, NULL if doesn't exist
292 */ 292 */
293struct CadetChannel * 293struct CadetChannel *
294GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid); 294GCT_get_channel (struct CadetTunnel *t, struct GNUNET_CADET_ChannelTunnelNumber chid);
295 295
296 296
297/** 297/**
@@ -304,7 +304,7 @@ GCT_get_channel (struct CadetTunnel *t, CADET_ChannelNumber chid);
304 */ 304 */
305void 305void
306GCT_handle_encrypted (struct CadetTunnel *t, 306GCT_handle_encrypted (struct CadetTunnel *t,
307 const struct GNUNET_CADET_Encrypted *msg); 307 const struct GNUNET_CADET_TunnelEncryptedMessage *msg);
308 308
309 309
310/** 310/**
@@ -315,7 +315,7 @@ GCT_handle_encrypted (struct CadetTunnel *t,
315 */ 315 */
316void 316void
317GCT_handle_kx (struct CadetTunnel *t, 317GCT_handle_kx (struct CadetTunnel *t,
318 const struct GNUNET_CADET_KX *msg); 318 const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg);
319 319
320 320
321/** 321/**
@@ -427,7 +427,7 @@ GCT_get_destination (struct CadetTunnel *t);
427 * 427 *
428 * @return ID of a channel free to use. 428 * @return ID of a channel free to use.
429 */ 429 */
430CADET_ChannelNumber 430struct GNUNET_CADET_ChannelTunnelNumber
431GCT_get_next_chid (struct CadetTunnel *t); 431GCT_get_next_chid (struct CadetTunnel *t);
432 432
433 433
diff --git a/src/cadet/test_cadet.conf b/src/cadet/test_cadet.conf
index b6ed64c58..e0c00858e 100644
--- a/src/cadet/test_cadet.conf
+++ b/src/cadet/test_cadet.conf
@@ -59,11 +59,6 @@ USE_EPHEMERAL_KEYS = NO
59[PATHS] 59[PATHS]
60GNUNET_TEST_HOME = /tmp/test-cadet/ 60GNUNET_TEST_HOME = /tmp/test-cadet/
61 61
62[nat]
63RETURN_LOCAL_ADDRESSES = YES
64DISABLEV6 = YES
65USE_LOCALADDR = YES
66
67[peerinfo] 62[peerinfo]
68NO_IO = YES 63NO_IO = YES
69 64
diff --git a/src/cadet/test_cadet_local.c b/src/cadet/test_cadet_local.c
index c226563d5..d15785cb0 100644
--- a/src/cadet/test_cadet_local.c
+++ b/src/cadet/test_cadet_local.c
@@ -178,7 +178,8 @@ inbound_channel (void *cls,
178 * with the channel is stored 178 * with the channel is stored
179 */ 179 */
180static void 180static void
181channel_end (void *cls, const struct GNUNET_CADET_Channel *channel, 181channel_end (void *cls,
182 const struct GNUNET_CADET_Channel *channel,
182 void *channel_ctx) 183 void *channel_ctx)
183{ 184{
184 long id = (long) cls; 185 long id = (long) cls;
@@ -191,6 +192,8 @@ channel_end (void *cls, const struct GNUNET_CADET_Channel *channel,
191 GNUNET_CADET_notify_transmit_ready_cancel (mth); 192 GNUNET_CADET_notify_transmit_ready_cancel (mth);
192 mth = NULL; 193 mth = NULL;
193 } 194 }
195 if (channel == ch)
196 ch = NULL;
194 if (GNUNET_NO == got_data) 197 if (GNUNET_NO == got_data)
195 { 198 {
196 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply ( 199 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (