aboutsummaryrefslogtreecommitdiff
path: root/src/messenger/gnunet-service-messenger_tunnel.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/messenger/gnunet-service-messenger_tunnel.c')
-rw-r--r--src/messenger/gnunet-service-messenger_tunnel.c155
1 files changed, 95 insertions, 60 deletions
diff --git a/src/messenger/gnunet-service-messenger_tunnel.c b/src/messenger/gnunet-service-messenger_tunnel.c
index c82a42cd0..5262ba79b 100644
--- a/src/messenger/gnunet-service-messenger_tunnel.c
+++ b/src/messenger/gnunet-service-messenger_tunnel.c
@@ -37,9 +37,10 @@ struct GNUNET_MESSENGER_SrvTunnel*
37create_tunnel (struct GNUNET_MESSENGER_SrvRoom *room, 37create_tunnel (struct GNUNET_MESSENGER_SrvRoom *room,
38 const struct GNUNET_PeerIdentity *door) 38 const struct GNUNET_PeerIdentity *door)
39{ 39{
40 GNUNET_assert((room) && (door)); 40 GNUNET_assert ((room) && (door));
41 41
42 struct GNUNET_MESSENGER_SrvTunnel *tunnel = GNUNET_new(struct GNUNET_MESSENGER_SrvTunnel); 42 struct GNUNET_MESSENGER_SrvTunnel *tunnel = GNUNET_new (struct
43 GNUNET_MESSENGER_SrvTunnel);
43 44
44 tunnel->room = room; 45 tunnel->room = room;
45 tunnel->channel = NULL; 46 tunnel->channel = NULL;
@@ -50,15 +51,16 @@ create_tunnel (struct GNUNET_MESSENGER_SrvRoom *room,
50 51
51 tunnel->peer_message = NULL; 52 tunnel->peer_message = NULL;
52 53
53 init_message_state(&(tunnel->state)); 54 init_message_state (&(tunnel->state));
54 55
55 return tunnel; 56 return tunnel;
56} 57}
57 58
59
58void 60void
59destroy_tunnel (struct GNUNET_MESSENGER_SrvTunnel *tunnel) 61destroy_tunnel (struct GNUNET_MESSENGER_SrvTunnel *tunnel)
60{ 62{
61 GNUNET_assert(tunnel); 63 GNUNET_assert (tunnel);
62 64
63 if (tunnel->channel) 65 if (tunnel->channel)
64 GNUNET_CADET_channel_destroy (tunnel->channel); 66 GNUNET_CADET_channel_destroy (tunnel->channel);
@@ -66,18 +68,19 @@ destroy_tunnel (struct GNUNET_MESSENGER_SrvTunnel *tunnel)
66 GNUNET_PEER_change_rc (tunnel->peer, -1); 68 GNUNET_PEER_change_rc (tunnel->peer, -1);
67 69
68 if (tunnel->peer_message) 70 if (tunnel->peer_message)
69 GNUNET_free(tunnel->peer_message); 71 GNUNET_free (tunnel->peer_message);
70 72
71 clear_message_state(&(tunnel->state)); 73 clear_message_state (&(tunnel->state));
72 74
73 GNUNET_free(tunnel); 75 GNUNET_free (tunnel);
74} 76}
75 77
78
76void 79void
77bind_tunnel (struct GNUNET_MESSENGER_SrvTunnel *tunnel, 80bind_tunnel (struct GNUNET_MESSENGER_SrvTunnel *tunnel,
78 struct GNUNET_CADET_Channel *channel) 81 struct GNUNET_CADET_Channel *channel)
79{ 82{
80 GNUNET_assert(tunnel); 83 GNUNET_assert (tunnel);
81 84
82 if (tunnel->channel) 85 if (tunnel->channel)
83 delayed_disconnect_channel (tunnel->channel); 86 delayed_disconnect_channel (tunnel->channel);
@@ -85,6 +88,7 @@ bind_tunnel (struct GNUNET_MESSENGER_SrvTunnel *tunnel,
85 tunnel->channel = channel; 88 tunnel->channel = channel;
86} 89}
87 90
91
88extern void 92extern void
89callback_room_disconnect (struct GNUNET_MESSENGER_SrvRoom *room, 93callback_room_disconnect (struct GNUNET_MESSENGER_SrvRoom *room,
90 void *cls); 94 void *cls);
@@ -103,6 +107,7 @@ callback_tunnel_disconnect (void *cls,
103 } 107 }
104} 108}
105 109
110
106extern int 111extern int
107callback_verify_room_message (struct GNUNET_MESSENGER_SrvRoom *room, 112callback_verify_room_message (struct GNUNET_MESSENGER_SrvRoom *room,
108 void *cls, 113 void *cls,
@@ -115,7 +120,7 @@ check_tunnel_message (void *cls,
115{ 120{
116 struct GNUNET_MESSENGER_SrvTunnel *tunnel = cls; 121 struct GNUNET_MESSENGER_SrvTunnel *tunnel = cls;
117 122
118 if (!tunnel) 123 if (! tunnel)
119 return GNUNET_SYSERR; 124 return GNUNET_SYSERR;
120 125
121 const uint16_t length = ntohs (header->size) - sizeof(*header); 126 const uint16_t length = ntohs (header->size) - sizeof(*header);
@@ -123,17 +128,20 @@ check_tunnel_message (void *cls,
123 128
124 struct GNUNET_MESSENGER_Message message; 129 struct GNUNET_MESSENGER_Message message;
125 130
126 if (length < get_message_kind_size(GNUNET_MESSENGER_KIND_UNKNOWN, GNUNET_YES)) 131 if (length < get_message_kind_size (GNUNET_MESSENGER_KIND_UNKNOWN,
132 GNUNET_YES))
127 { 133 {
128 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Tunnel error: Message too short! (%d)\n", length); 134 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
135 "Tunnel error: Message too short! (%d)\n", length);
129 return GNUNET_SYSERR; 136 return GNUNET_SYSERR;
130 } 137 }
131 138
132 uint16_t padding = 0; 139 uint16_t padding = 0;
133 140
134 if (GNUNET_YES != decode_message (&message, length, buffer, GNUNET_YES, &padding)) 141 if (GNUNET_YES != decode_message (&message, length, buffer, GNUNET_YES,
142 &padding))
135 { 143 {
136 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Tunnel error: Decoding failed!\n"); 144 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel error: Decoding failed!\n");
137 return GNUNET_SYSERR; 145 return GNUNET_SYSERR;
138 } 146 }
139 147
@@ -143,6 +151,7 @@ check_tunnel_message (void *cls,
143 return callback_verify_room_message (tunnel->room, cls, &message, &hash); 151 return callback_verify_room_message (tunnel->room, cls, &message, &hash);
144} 152}
145 153
154
146extern int 155extern int
147update_room_message (struct GNUNET_MESSENGER_SrvRoom *room, 156update_room_message (struct GNUNET_MESSENGER_SrvRoom *room,
148 struct GNUNET_MESSENGER_Message *message, 157 struct GNUNET_MESSENGER_Message *message,
@@ -157,26 +166,31 @@ static void
157update_tunnel_last_message (struct GNUNET_MESSENGER_SrvTunnel *tunnel, 166update_tunnel_last_message (struct GNUNET_MESSENGER_SrvTunnel *tunnel,
158 const struct GNUNET_HashCode *hash) 167 const struct GNUNET_HashCode *hash)
159{ 168{
160 struct GNUNET_MESSENGER_OperationStore *operation_store = get_srv_room_operation_store(tunnel->room); 169 struct GNUNET_MESSENGER_OperationStore *operation_store =
170 get_srv_room_operation_store (tunnel->room);
161 171
162 const int requested = (GNUNET_MESSENGER_OP_REQUEST == get_store_operation_type(operation_store, hash)? 172 const int requested = (GNUNET_MESSENGER_OP_REQUEST ==
163 GNUNET_YES : GNUNET_NO 173 get_store_operation_type (operation_store, hash)?
164 ); 174 GNUNET_YES : GNUNET_NO
175 );
165 176
166 struct GNUNET_MESSENGER_MessageStore *message_store = get_srv_room_message_store(tunnel->room); 177 struct GNUNET_MESSENGER_MessageStore *message_store =
178 get_srv_room_message_store (tunnel->room);
167 179
168 const struct GNUNET_MESSENGER_Message *message = get_store_message(message_store, hash); 180 const struct GNUNET_MESSENGER_Message *message = get_store_message (
181 message_store, hash);
169 182
170 if (message) 183 if (message)
171 update_message_state(&(tunnel->state), requested, message, hash); 184 update_message_state (&(tunnel->state), requested, message, hash);
172} 185}
173 186
187
174void 188void
175handle_tunnel_message (void *cls, const struct GNUNET_MessageHeader *header) 189handle_tunnel_message (void *cls, const struct GNUNET_MessageHeader *header)
176{ 190{
177 struct GNUNET_MESSENGER_SrvTunnel *tunnel = cls; 191 struct GNUNET_MESSENGER_SrvTunnel *tunnel = cls;
178 192
179 if (!tunnel) 193 if (! tunnel)
180 return; 194 return;
181 195
182 const uint16_t length = ntohs (header->size) - sizeof(*header); 196 const uint16_t length = ntohs (header->size) - sizeof(*header);
@@ -190,12 +204,12 @@ handle_tunnel_message (void *cls, const struct GNUNET_MessageHeader *header)
190 decode_message (&message, length, buffer, GNUNET_YES, &padding); 204 decode_message (&message, length, buffer, GNUNET_YES, &padding);
191 hash_message (&message, length - padding, buffer, &hash); 205 hash_message (&message, length - padding, buffer, &hash);
192 206
193 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Got message of kind: %s!\n", 207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message of kind: %s!\n",
194 GNUNET_MESSENGER_name_of_kind(message.header.kind)); 208 GNUNET_MESSENGER_name_of_kind (message.header.kind));
195 209
196 const int new_message = update_room_message ( 210 const int new_message = update_room_message (
197 tunnel->room, copy_message (&message), &hash 211 tunnel->room, copy_message (&message), &hash
198 ); 212 );
199 213
200 if (GNUNET_YES != new_message) 214 if (GNUNET_YES != new_message)
201 goto receive_done; 215 goto receive_done;
@@ -213,7 +227,8 @@ handle_tunnel_message (void *cls, const struct GNUNET_MessageHeader *header)
213 forward_message = recv_message_peer (tunnel->room, tunnel, &message, &hash); 227 forward_message = recv_message_peer (tunnel->room, tunnel, &message, &hash);
214 break; 228 break;
215 case GNUNET_MESSENGER_KIND_REQUEST: 229 case GNUNET_MESSENGER_KIND_REQUEST:
216 forward_message = recv_message_request (tunnel->room, tunnel, &message, &hash); 230 forward_message = recv_message_request (tunnel->room, tunnel, &message,
231 &hash);
217 break; 232 break;
218 default: 233 default:
219 break; 234 break;
@@ -226,15 +241,16 @@ handle_tunnel_message (void *cls, const struct GNUNET_MessageHeader *header)
226 } 241 }
227 242
228receive_done: 243receive_done:
229 cleanup_message(&message); 244 cleanup_message (&message);
230 245
231 GNUNET_CADET_receive_done (tunnel->channel); 246 GNUNET_CADET_receive_done (tunnel->channel);
232} 247}
233 248
249
234int 250int
235connect_tunnel (struct GNUNET_MESSENGER_SrvTunnel *tunnel) 251connect_tunnel (struct GNUNET_MESSENGER_SrvTunnel *tunnel)
236{ 252{
237 GNUNET_assert(tunnel); 253 GNUNET_assert (tunnel);
238 254
239 if (tunnel->channel) 255 if (tunnel->channel)
240 return GNUNET_NO; 256 return GNUNET_NO;
@@ -244,21 +260,28 @@ connect_tunnel (struct GNUNET_MESSENGER_SrvTunnel *tunnel)
244 struct GNUNET_CADET_Handle *cadet = get_srv_room_cadet (tunnel->room); 260 struct GNUNET_CADET_Handle *cadet = get_srv_room_cadet (tunnel->room);
245 const struct GNUNET_HashCode *key = get_srv_room_key (tunnel->room); 261 const struct GNUNET_HashCode *key = get_srv_room_key (tunnel->room);
246 262
247 struct GNUNET_MQ_MessageHandler handlers[] = { GNUNET_MQ_hd_var_size(tunnel_message, GNUNET_MESSAGE_TYPE_CADET_CLI, 263 struct GNUNET_MQ_MessageHandler handlers[] = { GNUNET_MQ_hd_var_size (
248 struct GNUNET_MessageHeader, NULL), 264 tunnel_message,
249 GNUNET_MQ_handler_end() }; 265 GNUNET_MESSAGE_TYPE_CADET_CLI,
266 struct
267 GNUNET_MessageHeader, NULL),
268 GNUNET_MQ_handler_end () };
250 269
251 struct GNUNET_HashCode port; 270 struct GNUNET_HashCode port;
252 convert_messenger_key_to_port(key, &port); 271 convert_messenger_key_to_port (key, &port);
253 tunnel->channel = GNUNET_CADET_channel_create (cadet, tunnel, door, &port, NULL, callback_tunnel_disconnect, handlers); 272 tunnel->channel = GNUNET_CADET_channel_create (cadet, tunnel, door, &port,
273 NULL,
274 callback_tunnel_disconnect,
275 handlers);
254 276
255 return GNUNET_YES; 277 return GNUNET_YES;
256} 278}
257 279
280
258void 281void
259disconnect_tunnel (struct GNUNET_MESSENGER_SrvTunnel *tunnel) 282disconnect_tunnel (struct GNUNET_MESSENGER_SrvTunnel *tunnel)
260{ 283{
261 GNUNET_assert(tunnel); 284 GNUNET_assert (tunnel);
262 285
263 if (tunnel->channel) 286 if (tunnel->channel)
264 { 287 {
@@ -268,14 +291,16 @@ disconnect_tunnel (struct GNUNET_MESSENGER_SrvTunnel *tunnel)
268 } 291 }
269} 292}
270 293
294
271int 295int
272is_tunnel_connected (const struct GNUNET_MESSENGER_SrvTunnel *tunnel) 296is_tunnel_connected (const struct GNUNET_MESSENGER_SrvTunnel *tunnel)
273{ 297{
274 GNUNET_assert(tunnel); 298 GNUNET_assert (tunnel);
275 299
276 return (tunnel->channel ? GNUNET_YES : GNUNET_NO); 300 return (tunnel->channel ? GNUNET_YES : GNUNET_NO);
277} 301}
278 302
303
279struct GNUNET_MESSENGER_MessageSent 304struct GNUNET_MESSENGER_MessageSent
280{ 305{
281 struct GNUNET_MESSENGER_SrvTunnel *tunnel; 306 struct GNUNET_MESSENGER_SrvTunnel *tunnel;
@@ -290,21 +315,23 @@ callback_tunnel_sent (void *cls)
290 if (sent->tunnel) 315 if (sent->tunnel)
291 update_tunnel_last_message (sent->tunnel, &(sent->hash)); 316 update_tunnel_last_message (sent->tunnel, &(sent->hash));
292 317
293 GNUNET_free(sent); 318 GNUNET_free (sent);
294} 319}
295 320
321
296void 322void
297send_tunnel_envelope (struct GNUNET_MESSENGER_SrvTunnel *tunnel, 323send_tunnel_envelope (struct GNUNET_MESSENGER_SrvTunnel *tunnel,
298 struct GNUNET_MQ_Envelope *env, 324 struct GNUNET_MQ_Envelope *env,
299 const struct GNUNET_HashCode *hash) 325 const struct GNUNET_HashCode *hash)
300{ 326{
301 GNUNET_assert((tunnel) && (env) && (hash)); 327 GNUNET_assert ((tunnel) && (env) && (hash));
302 328
303 struct GNUNET_MQ_Handle *mq = GNUNET_CADET_get_mq (tunnel->channel); 329 struct GNUNET_MQ_Handle *mq = GNUNET_CADET_get_mq (tunnel->channel);
304 330
305 struct GNUNET_MESSENGER_MessageSent *sent = GNUNET_new(struct GNUNET_MESSENGER_MessageSent); 331 struct GNUNET_MESSENGER_MessageSent *sent = GNUNET_new (struct
332 GNUNET_MESSENGER_MessageSent);
306 333
307 GNUNET_memcpy(&(sent->hash), hash, sizeof(struct GNUNET_HashCode)); 334 GNUNET_memcpy (&(sent->hash), hash, sizeof(struct GNUNET_HashCode));
308 335
309 sent->tunnel = tunnel; 336 sent->tunnel = tunnel;
310 337
@@ -312,85 +339,93 @@ send_tunnel_envelope (struct GNUNET_MESSENGER_SrvTunnel *tunnel,
312 GNUNET_MQ_send (mq, env); 339 GNUNET_MQ_send (mq, env);
313} 340}
314 341
342
315int 343int
316send_tunnel_message (struct GNUNET_MESSENGER_SrvTunnel *tunnel, 344send_tunnel_message (struct GNUNET_MESSENGER_SrvTunnel *tunnel,
317 void *handle, 345 void *handle,
318 struct GNUNET_MESSENGER_Message *message) 346 struct GNUNET_MESSENGER_Message *message)
319{ 347{
320 GNUNET_assert((tunnel) && (handle)); 348 GNUNET_assert ((tunnel) && (handle));
321 349
322 if (!message) 350 if (! message)
323 return GNUNET_NO; 351 return GNUNET_NO;
324 352
325 struct GNUNET_HashCode hash; 353 struct GNUNET_HashCode hash;
326 struct GNUNET_MQ_Envelope *env = pack_srv_room_message ( 354 struct GNUNET_MQ_Envelope *env = pack_srv_room_message (
327 tunnel->room, (struct GNUNET_MESSENGER_SrvHandle*) handle, 355 tunnel->room, (struct GNUNET_MESSENGER_SrvHandle*) handle,
328 message, &hash, GNUNET_MESSENGER_PACK_MODE_ENVELOPE 356 message, &hash, GNUNET_MESSENGER_PACK_MODE_ENVELOPE
329 ); 357 );
330 358
331 destroy_message(message); 359 destroy_message (message);
332 360
333 if (!env) 361 if (! env)
334 return GNUNET_NO; 362 return GNUNET_NO;
335 363
336 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Sending tunnel message: %s\n", 364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending tunnel message: %s\n",
337 GNUNET_h2s(&hash)); 365 GNUNET_h2s (&hash));
338 366
339 send_tunnel_envelope (tunnel, env, &hash); 367 send_tunnel_envelope (tunnel, env, &hash);
340 return GNUNET_YES; 368 return GNUNET_YES;
341} 369}
342 370
371
343void 372void
344forward_tunnel_message (struct GNUNET_MESSENGER_SrvTunnel *tunnel, 373forward_tunnel_message (struct GNUNET_MESSENGER_SrvTunnel *tunnel,
345 const struct GNUNET_MESSENGER_Message *message, 374 const struct GNUNET_MESSENGER_Message *message,
346 const struct GNUNET_HashCode *hash) 375 const struct GNUNET_HashCode *hash)
347{ 376{
348 GNUNET_assert((tunnel) && (message) && (hash)); 377 GNUNET_assert ((tunnel) && (message) && (hash));
349 378
350 struct GNUNET_MESSENGER_Message *copy = copy_message(message); 379 struct GNUNET_MESSENGER_Message *copy = copy_message (message);
351 struct GNUNET_MQ_Envelope *env = pack_message (copy, NULL, NULL, GNUNET_MESSENGER_PACK_MODE_ENVELOPE, NULL); 380 struct GNUNET_MQ_Envelope *env = pack_message (copy, NULL, NULL,
381 GNUNET_MESSENGER_PACK_MODE_ENVELOPE,
382 NULL);
352 383
353 destroy_message(copy); 384 destroy_message (copy);
354 385
355 if (!env) 386 if (! env)
356 return; 387 return;
357 388
358 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Forwarding tunnel message: %s\n", 389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Forwarding tunnel message: %s\n",
359 GNUNET_h2s(hash)); 390 GNUNET_h2s (hash));
360 391
361 send_tunnel_envelope (tunnel, env, hash); 392 send_tunnel_envelope (tunnel, env, hash);
362} 393}
363 394
395
364const struct GNUNET_HashCode* 396const struct GNUNET_HashCode*
365get_tunnel_peer_message (const struct GNUNET_MESSENGER_SrvTunnel *tunnel) 397get_tunnel_peer_message (const struct GNUNET_MESSENGER_SrvTunnel *tunnel)
366{ 398{
367 GNUNET_assert(tunnel); 399 GNUNET_assert (tunnel);
368 400
369 return tunnel->peer_message; 401 return tunnel->peer_message;
370} 402}
371 403
404
372void 405void
373get_tunnel_peer_identity (const struct GNUNET_MESSENGER_SrvTunnel *tunnel, 406get_tunnel_peer_identity (const struct GNUNET_MESSENGER_SrvTunnel *tunnel,
374 struct GNUNET_PeerIdentity *peer) 407 struct GNUNET_PeerIdentity *peer)
375{ 408{
376 GNUNET_assert(tunnel); 409 GNUNET_assert (tunnel);
377 410
378 GNUNET_PEER_resolve(tunnel->peer, peer); 411 GNUNET_PEER_resolve (tunnel->peer, peer);
379} 412}
380 413
414
381uint32_t 415uint32_t
382get_tunnel_messenger_version (const struct GNUNET_MESSENGER_SrvTunnel *tunnel) 416get_tunnel_messenger_version (const struct GNUNET_MESSENGER_SrvTunnel *tunnel)
383{ 417{
384 GNUNET_assert(tunnel); 418 GNUNET_assert (tunnel);
385 419
386 return tunnel->messenger_version; 420 return tunnel->messenger_version;
387} 421}
388 422
423
389int 424int
390update_tunnel_messenger_version (struct GNUNET_MESSENGER_SrvTunnel *tunnel, 425update_tunnel_messenger_version (struct GNUNET_MESSENGER_SrvTunnel *tunnel,
391 uint32_t version) 426 uint32_t version)
392{ 427{
393 GNUNET_assert(tunnel); 428 GNUNET_assert (tunnel);
394 429
395 if (version != GNUNET_MESSENGER_VERSION) 430 if (version != GNUNET_MESSENGER_VERSION)
396 return GNUNET_SYSERR; 431 return GNUNET_SYSERR;