diff options
author | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 19:35:11 +0200 |
---|---|---|
committer | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 19:35:11 +0200 |
commit | ddfee3f564bff9c5d5719af3132d7869b8783ec4 (patch) | |
tree | e6fd7801fe6808797f3418bf081ab68d5a5ec27b /src/transport/gnunet-communicator-udp.c | |
parent | 852718c2473e41bc01ada0d53ad93c7da78e6ec8 (diff) | |
download | gnunet-ddfee3f564bff9c5d5719af3132d7869b8783ec4.tar.gz gnunet-ddfee3f564bff9c5d5719af3132d7869b8783ec4.zip |
BUILD: more more components into new structure; ftbfs fix
Diffstat (limited to 'src/transport/gnunet-communicator-udp.c')
-rw-r--r-- | src/transport/gnunet-communicator-udp.c | 3444 |
1 files changed, 0 insertions, 3444 deletions
diff --git a/src/transport/gnunet-communicator-udp.c b/src/transport/gnunet-communicator-udp.c deleted file mode 100644 index f9bbe91f6..000000000 --- a/src/transport/gnunet-communicator-udp.c +++ /dev/null | |||
@@ -1,3444 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2010-2014, 2018, 2019 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | : | ||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file transport/gnunet-communicator-udp.c | ||
23 | * @brief Transport plugin using UDP. | ||
24 | * @author Christian Grothoff | ||
25 | * | ||
26 | * TODO: | ||
27 | * - consider imposing transmission limits in the absence | ||
28 | * of ACKs; or: maybe this should be done at TNG service level? | ||
29 | * (at least the receiver might want to enforce limits on | ||
30 | * KX/DH operations per sender in here) (#5552) | ||
31 | * - overall, we should look more into flow control support | ||
32 | * (either in backchannel, or general solution in TNG service) | ||
33 | * - handle addresses discovered from broadcasts (#5551) | ||
34 | * (think: what was the story again on address validation? | ||
35 | * where is the API for that!?!) | ||
36 | * - support DNS names in BINDTO option (#5528) | ||
37 | * - support NAT connection reversal method (#5529) | ||
38 | * - support other UDP-specific NAT traversal methods (#) | ||
39 | */ | ||
40 | #include "platform.h" | ||
41 | #include "gnunet_common.h" | ||
42 | #include "gnunet_util_lib.h" | ||
43 | #include "gnunet_protocols.h" | ||
44 | #include "gnunet_signatures.h" | ||
45 | #include "gnunet_constants.h" | ||
46 | #include "gnunet_nat_service.h" | ||
47 | #include "gnunet_statistics_service.h" | ||
48 | #include "gnunet_transport_application_service.h" | ||
49 | #include "gnunet_transport_communication_service.h" | ||
50 | |||
51 | /** | ||
52 | * How often do we rekey based on time (at least) | ||
53 | */ | ||
54 | #define DEFAULT_REKEY_TIME_INTERVAL GNUNET_TIME_UNIT_DAYS | ||
55 | |||
56 | /** | ||
57 | * How long do we wait until we must have received the initial KX? | ||
58 | */ | ||
59 | #define PROTO_QUEUE_TIMEOUT GNUNET_TIME_UNIT_MINUTES | ||
60 | |||
61 | /** | ||
62 | * How often do we broadcast our presence on the LAN? | ||
63 | */ | ||
64 | #define BROADCAST_FREQUENCY GNUNET_TIME_UNIT_MINUTES | ||
65 | |||
66 | /** | ||
67 | * How often do we scan for changes to our network interfaces? | ||
68 | */ | ||
69 | #define INTERFACE_SCAN_FREQUENCY \ | ||
70 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) | ||
71 | |||
72 | /** | ||
73 | * How long do we believe our addresses to remain up (before | ||
74 | * the other peer should revalidate). | ||
75 | */ | ||
76 | #define ADDRESS_VALIDITY_PERIOD GNUNET_TIME_UNIT_HOURS | ||
77 | |||
78 | #define WORKING_QUEUE_INTERVALL \ | ||
79 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS,1) | ||
80 | |||
81 | /** | ||
82 | * AES key size. | ||
83 | */ | ||
84 | #define AES_KEY_SIZE (256 / 8) | ||
85 | |||
86 | /** | ||
87 | * AES (GCM) IV size. | ||
88 | */ | ||
89 | #define AES_IV_SIZE (96 / 8) | ||
90 | |||
91 | /** | ||
92 | * Size of the GCM tag. | ||
93 | */ | ||
94 | #define GCM_TAG_SIZE (128 / 8) | ||
95 | |||
96 | #define GENERATE_AT_ONCE 16 | ||
97 | |||
98 | /** | ||
99 | * If we fall below this number of available KCNs, | ||
100 | * we generate additional ACKs until we reach | ||
101 | * #KCN_TARGET. | ||
102 | * Should be large enough that we don't generate ACKs all | ||
103 | * the time and still have enough time for the ACK to | ||
104 | * arrive before the sender runs out. So really this | ||
105 | * should ideally be based on the RTT. | ||
106 | */ | ||
107 | #define KCN_THRESHOLD 96 | ||
108 | |||
109 | /** | ||
110 | * How many KCNs do we keep around *after* we hit | ||
111 | * the #KCN_THRESHOLD? Should be larger than | ||
112 | * #KCN_THRESHOLD so we do not generate just one | ||
113 | * ACK at the time. | ||
114 | */ | ||
115 | #define KCN_TARGET 128 | ||
116 | |||
117 | /** | ||
118 | * What is the maximum delta between KCN sequence numbers | ||
119 | * that we allow. Used to expire 'ancient' KCNs that likely | ||
120 | * were dropped by the network. Must be larger than | ||
121 | * KCN_TARGET (otherwise we generate new KCNs all the time), | ||
122 | * but not too large (otherwise packet loss may cause | ||
123 | * sender to fall back to KX needlessly when sender runs | ||
124 | * out of ACK'ed KCNs due to losses). | ||
125 | */ | ||
126 | #define MAX_SQN_DELTA 160 | ||
127 | |||
128 | /** | ||
129 | * How many shared master secrets do we keep around | ||
130 | * at most per sender? Should be large enough so | ||
131 | * that we generally have a chance of sending an ACK | ||
132 | * before the sender already rotated out the master | ||
133 | * secret. Generally values around #KCN_TARGET make | ||
134 | * sense. Might make sense to adapt to RTT if we had | ||
135 | * a good measurement... | ||
136 | */ | ||
137 | #define MAX_SECRETS 256 | ||
138 | |||
139 | /** | ||
140 | * Default value for how often we do rekey based on number of bytes transmitted? | ||
141 | * (additionally randomized). | ||
142 | */ | ||
143 | #define DEFAULT_REKEY_MAX_BYTES (1024LLU * 1024 * 1024 * 4LLU) | ||
144 | |||
145 | /** | ||
146 | * Address prefix used by the communicator. | ||
147 | */ | ||
148 | |||
149 | #define COMMUNICATOR_ADDRESS_PREFIX "udp" | ||
150 | |||
151 | /** | ||
152 | * Configuration section used by the communicator. | ||
153 | */ | ||
154 | #define COMMUNICATOR_CONFIG_SECTION "communicator-udp" | ||
155 | |||
156 | GNUNET_NETWORK_STRUCT_BEGIN | ||
157 | |||
158 | |||
159 | /** | ||
160 | * Signature we use to verify that the ephemeral key was really chosen by | ||
161 | * the specified sender. If possible, the receiver should respond with | ||
162 | * a `struct UDPAck` (possibly via backchannel). | ||
163 | */ | ||
164 | struct UdpHandshakeSignature | ||
165 | { | ||
166 | /** | ||
167 | * Purpose must be #GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_UDP_HANDSHAKE | ||
168 | */ | ||
169 | struct GNUNET_CRYPTO_EccSignaturePurpose purpose; | ||
170 | |||
171 | /** | ||
172 | * Identity of the inititor of the UDP connection (UDP client). | ||
173 | */ | ||
174 | struct GNUNET_PeerIdentity sender; | ||
175 | |||
176 | /** | ||
177 | * Presumed identity of the target of the UDP connection (UDP server) | ||
178 | */ | ||
179 | struct GNUNET_PeerIdentity receiver; | ||
180 | |||
181 | /** | ||
182 | * Ephemeral key used by the @e sender. | ||
183 | */ | ||
184 | struct GNUNET_CRYPTO_EcdhePublicKey ephemeral; | ||
185 | |||
186 | /** | ||
187 | * Monotonic time of @e sender, to possibly help detect replay attacks | ||
188 | * (if receiver persists times by sender). | ||
189 | */ | ||
190 | struct GNUNET_TIME_AbsoluteNBO monotonic_time; | ||
191 | }; | ||
192 | |||
193 | |||
194 | /** | ||
195 | * "Plaintext" header at beginning of KX message. Followed | ||
196 | * by encrypted `struct UDPConfirmation`. | ||
197 | */ | ||
198 | struct InitialKX | ||
199 | { | ||
200 | /** | ||
201 | * Ephemeral key for KX. | ||
202 | */ | ||
203 | struct GNUNET_CRYPTO_EcdhePublicKey ephemeral; | ||
204 | |||
205 | /** | ||
206 | * HMAC for the following encrypted message, using GCM. HMAC uses | ||
207 | * key derived from the handshake with sequence number zero. | ||
208 | */ | ||
209 | uint8_t gcm_tag[GCM_TAG_SIZE]; | ||
210 | |||
211 | }; | ||
212 | |||
213 | |||
214 | /** | ||
215 | * Encrypted continuation of UDP initial handshake, followed | ||
216 | * by message header with payload. | ||
217 | */ | ||
218 | struct UDPConfirmation | ||
219 | { | ||
220 | /** | ||
221 | * Sender's identity | ||
222 | */ | ||
223 | struct GNUNET_PeerIdentity sender; | ||
224 | |||
225 | /** | ||
226 | * Sender's signature of type #GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_UDP_HANDSHAKE | ||
227 | */ | ||
228 | struct GNUNET_CRYPTO_EddsaSignature sender_sig; | ||
229 | |||
230 | /** | ||
231 | * Monotonic time of @e sender, to possibly help detect replay attacks | ||
232 | * (if receiver persists times by sender). | ||
233 | */ | ||
234 | struct GNUNET_TIME_AbsoluteNBO monotonic_time; | ||
235 | |||
236 | /* followed by messages */ | ||
237 | |||
238 | /* padding may follow actual messages */ | ||
239 | }; | ||
240 | |||
241 | |||
242 | /** | ||
243 | * UDP key acknowledgement. May be sent via backchannel. Allows the | ||
244 | * sender to use `struct UDPBox` with the acknowledge key henceforth. | ||
245 | */ | ||
246 | struct UDPAck | ||
247 | { | ||
248 | /** | ||
249 | * Type is #GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK. | ||
250 | */ | ||
251 | struct GNUNET_MessageHeader header; | ||
252 | |||
253 | /** | ||
254 | * Sequence acknowledgement limit. Specifies current maximum sequence | ||
255 | * number supported by receiver. | ||
256 | */ | ||
257 | uint32_t sequence_ack GNUNET_PACKED; | ||
258 | |||
259 | /** | ||
260 | * CMAC of the base key being acknowledged. | ||
261 | */ | ||
262 | struct GNUNET_HashCode cmac; | ||
263 | }; | ||
264 | |||
265 | |||
266 | /** | ||
267 | * Signature we use to verify that the broadcast was really made by | ||
268 | * the peer that claims to have made it. Basically, affirms that the | ||
269 | * peer is really using this IP address (albeit possibly not in _our_ | ||
270 | * LAN). Makes it difficult for peers in the LAN to claim to | ||
271 | * be just any global peer -- an attacker must have at least | ||
272 | * shared a LAN with the peer they're pretending to be here. | ||
273 | */ | ||
274 | struct UdpBroadcastSignature | ||
275 | { | ||
276 | /** | ||
277 | * Purpose must be #GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_UDP_BROADCAST | ||
278 | */ | ||
279 | struct GNUNET_CRYPTO_EccSignaturePurpose purpose; | ||
280 | |||
281 | /** | ||
282 | * Identity of the inititor of the UDP broadcast. | ||
283 | */ | ||
284 | struct GNUNET_PeerIdentity sender; | ||
285 | |||
286 | /** | ||
287 | * Hash of the sender's UDP address. | ||
288 | */ | ||
289 | struct GNUNET_HashCode h_address; | ||
290 | }; | ||
291 | |||
292 | |||
293 | /** | ||
294 | * Broadcast by peer in LAN announcing its presence. Unusual in that | ||
295 | * we don't pad these to full MTU, as we cannot prevent being | ||
296 | * recognized in LAN as GNUnet peers if this feature is enabled | ||
297 | * anyway. Also, the entire message is in cleartext. | ||
298 | */ | ||
299 | struct UDPBroadcast | ||
300 | { | ||
301 | /** | ||
302 | * Sender's peer identity. | ||
303 | */ | ||
304 | struct GNUNET_PeerIdentity sender; | ||
305 | |||
306 | /** | ||
307 | * Sender's signature of type | ||
308 | * #GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_UDP_BROADCAST | ||
309 | */ | ||
310 | struct GNUNET_CRYPTO_EddsaSignature sender_sig; | ||
311 | }; | ||
312 | |||
313 | |||
314 | /** | ||
315 | * UDP message box. Always sent encrypted, only allowed after | ||
316 | * the receiver sent a `struct UDPAck` for the base key! | ||
317 | */ | ||
318 | struct UDPBox | ||
319 | { | ||
320 | /** | ||
321 | * Key and IV identification code. KDF applied to an acknowledged | ||
322 | * base key and a sequence number. Sequence numbers must be used | ||
323 | * monotonically increasing up to the maximum specified in | ||
324 | * `struct UDPAck`. Without further `struct UDPAck`s, the sender | ||
325 | * must fall back to sending handshakes! | ||
326 | */ | ||
327 | struct GNUNET_ShortHashCode kid; | ||
328 | |||
329 | /** | ||
330 | * 128-bit authentication tag for the following encrypted message, | ||
331 | * from GCM. MAC starts at the @e body_start that follows and | ||
332 | * extends until the end of the UDP payload. If the @e hmac is | ||
333 | * wrong, the receiver should check if the message might be a | ||
334 | * `struct UdpHandshakeSignature`. | ||
335 | */ | ||
336 | uint8_t gcm_tag[GCM_TAG_SIZE]; | ||
337 | |||
338 | }; | ||
339 | |||
340 | /** | ||
341 | * Plaintext of a rekey payload in a UDPBox. | ||
342 | */ | ||
343 | struct UDPRekey | ||
344 | { | ||
345 | /** | ||
346 | * Type is #GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_REKEY. | ||
347 | */ | ||
348 | struct GNUNET_MessageHeader header; | ||
349 | |||
350 | /** | ||
351 | * Ephemeral key to rekey with. | ||
352 | */ | ||
353 | struct GNUNET_CRYPTO_EcdhePublicKey ephemeral; | ||
354 | }; | ||
355 | |||
356 | GNUNET_NETWORK_STRUCT_END | ||
357 | |||
358 | /** | ||
359 | * Shared secret we generated for a particular sender or receiver. | ||
360 | */ | ||
361 | struct SharedSecret; | ||
362 | |||
363 | |||
364 | /** | ||
365 | * Pre-generated "kid" code (key and IV identification code) to | ||
366 | * quickly derive master key for a `struct UDPBox`. | ||
367 | */ | ||
368 | struct KeyCacheEntry | ||
369 | { | ||
370 | /** | ||
371 | * Kept in a DLL. | ||
372 | */ | ||
373 | struct KeyCacheEntry *next; | ||
374 | |||
375 | /** | ||
376 | * Kept in a DLL. | ||
377 | */ | ||
378 | struct KeyCacheEntry *prev; | ||
379 | |||
380 | /** | ||
381 | * Key and IV identification code. KDF applied to an acknowledged | ||
382 | * base key and a sequence number. Sequence numbers must be used | ||
383 | * monotonically increasing up to the maximum specified in | ||
384 | * `struct UDPAck`. Without further `struct UDPAck`s, the sender | ||
385 | * must fall back to sending handshakes! | ||
386 | */ | ||
387 | struct GNUNET_ShortHashCode kid; | ||
388 | |||
389 | /** | ||
390 | * Corresponding shared secret. | ||
391 | */ | ||
392 | struct SharedSecret *ss; | ||
393 | |||
394 | /** | ||
395 | * Sequence number used to derive this entry from master key. | ||
396 | */ | ||
397 | uint32_t sequence_number; | ||
398 | }; | ||
399 | |||
400 | |||
401 | /** | ||
402 | * Information we track per sender address we have recently been | ||
403 | * in contact with (decryption from sender). | ||
404 | */ | ||
405 | struct SenderAddress; | ||
406 | |||
407 | /** | ||
408 | * Information we track per receiving address we have recently been | ||
409 | * in contact with (encryption to receiver). | ||
410 | */ | ||
411 | struct ReceiverAddress; | ||
412 | |||
413 | /** | ||
414 | * Shared secret we generated for a particular sender or receiver. | ||
415 | */ | ||
416 | struct SharedSecret | ||
417 | { | ||
418 | /** | ||
419 | * Kept in a DLL. | ||
420 | */ | ||
421 | struct SharedSecret *next; | ||
422 | |||
423 | /** | ||
424 | * Kept in a DLL. | ||
425 | */ | ||
426 | struct SharedSecret *prev; | ||
427 | |||
428 | /** | ||
429 | * Kept in a DLL, sorted by sequence number. Only if we are decrypting. | ||
430 | */ | ||
431 | struct KeyCacheEntry *kce_head; | ||
432 | |||
433 | /** | ||
434 | * Kept in a DLL, sorted by sequence number. Only if we are decrypting. | ||
435 | */ | ||
436 | struct KeyCacheEntry *kce_tail; | ||
437 | |||
438 | /** | ||
439 | * Sender we use this shared secret with, or NULL. | ||
440 | */ | ||
441 | struct SenderAddress *sender; | ||
442 | |||
443 | /** | ||
444 | * Receiver we use this shared secret with, or NULL. | ||
445 | */ | ||
446 | struct ReceiverAddress *receiver; | ||
447 | |||
448 | /** | ||
449 | * Master shared secret. | ||
450 | */ | ||
451 | struct GNUNET_HashCode master; | ||
452 | |||
453 | /** | ||
454 | * CMAC is used to identify @e master in ACKs. | ||
455 | */ | ||
456 | struct GNUNET_HashCode cmac; | ||
457 | |||
458 | /** | ||
459 | * Up to which sequence number did we use this @e master already? | ||
460 | * (for encrypting only) | ||
461 | */ | ||
462 | uint32_t sequence_used; | ||
463 | |||
464 | /** | ||
465 | * Up to which sequence number did the other peer allow us to use | ||
466 | * this key, or up to which number did we allow the other peer to | ||
467 | * use this key? | ||
468 | */ | ||
469 | uint32_t sequence_allowed; | ||
470 | |||
471 | /** | ||
472 | * Number of active KCN entries. | ||
473 | */ | ||
474 | unsigned int active_kce_count; | ||
475 | |||
476 | /** | ||
477 | * Bytes sent with this shared secret | ||
478 | */ | ||
479 | size_t bytes_sent; | ||
480 | |||
481 | /** | ||
482 | * rekey initiated for this secret? | ||
483 | */ | ||
484 | int rekey_initiated; | ||
485 | |||
486 | /** | ||
487 | * Also precompute keys despite sufficient acks (for rekey) | ||
488 | */ | ||
489 | int override_available_acks; | ||
490 | }; | ||
491 | |||
492 | |||
493 | /** | ||
494 | * Information we track per sender address we have recently been | ||
495 | * in contact with (we decrypt messages from the sender). | ||
496 | */ | ||
497 | struct SenderAddress | ||
498 | { | ||
499 | /** | ||
500 | * To whom are we talking to. | ||
501 | */ | ||
502 | struct GNUNET_PeerIdentity target; | ||
503 | |||
504 | /** | ||
505 | * Entry in sender expiration heap. | ||
506 | */ | ||
507 | struct GNUNET_CONTAINER_HeapNode *hn; | ||
508 | |||
509 | /** | ||
510 | * Shared secrets we used with @e target, first used is head. | ||
511 | */ | ||
512 | struct SharedSecret *ss_head; | ||
513 | |||
514 | /** | ||
515 | * Shared secrets we used with @e target, last used is tail. | ||
516 | */ | ||
517 | struct SharedSecret *ss_tail; | ||
518 | |||
519 | /** | ||
520 | * Address of the other peer. | ||
521 | */ | ||
522 | struct sockaddr *address; | ||
523 | |||
524 | /** | ||
525 | * Length of the address. | ||
526 | */ | ||
527 | socklen_t address_len; | ||
528 | |||
529 | /** | ||
530 | * Timeout for this sender. | ||
531 | */ | ||
532 | struct GNUNET_TIME_Absolute timeout; | ||
533 | |||
534 | /** | ||
535 | * Length of the DLL at @a ss_head. | ||
536 | */ | ||
537 | unsigned int num_secrets; | ||
538 | |||
539 | /** | ||
540 | * Number of BOX keys from ACKs we have currently | ||
541 | * available for this sender. | ||
542 | */ | ||
543 | unsigned int acks_available; | ||
544 | |||
545 | /** | ||
546 | * Which network type does this queue use? | ||
547 | */ | ||
548 | enum GNUNET_NetworkType nt; | ||
549 | |||
550 | /** | ||
551 | * sender_destroy already called on sender. | ||
552 | */ | ||
553 | int sender_destroy_called; | ||
554 | |||
555 | /** | ||
556 | * ID of kce working queue task | ||
557 | */ | ||
558 | struct GNUNET_SCHEDULER_Task *kce_task; | ||
559 | |||
560 | /** | ||
561 | * Is the kce_task finished? | ||
562 | */ | ||
563 | int kce_task_finished; | ||
564 | |||
565 | /** | ||
566 | * When KCE finishes, send ACK if GNUNET_YES | ||
567 | */ | ||
568 | int kce_send_ack_on_finish; | ||
569 | }; | ||
570 | |||
571 | |||
572 | /** | ||
573 | * Information we track per receiving address we have recently been | ||
574 | * in contact with (encryption to receiver). | ||
575 | */ | ||
576 | struct ReceiverAddress | ||
577 | { | ||
578 | /** | ||
579 | * Timeout for this receiver address. | ||
580 | */ | ||
581 | struct GNUNET_TIME_Absolute rekey_timeout; | ||
582 | |||
583 | /** | ||
584 | * To whom are we talking to. | ||
585 | */ | ||
586 | struct GNUNET_PeerIdentity target; | ||
587 | |||
588 | /** | ||
589 | * Shared secrets we received from @e target, first used is head. | ||
590 | */ | ||
591 | struct SharedSecret *ss_head; | ||
592 | |||
593 | /** | ||
594 | * Shared secrets we received with @e target, last used is tail. | ||
595 | */ | ||
596 | struct SharedSecret *ss_tail; | ||
597 | |||
598 | /** | ||
599 | * Address of the receiver in the human-readable format | ||
600 | * with the #COMMUNICATOR_ADDRESS_PREFIX. | ||
601 | */ | ||
602 | char *foreign_addr; | ||
603 | |||
604 | /** | ||
605 | * Address of the other peer. | ||
606 | */ | ||
607 | struct sockaddr *address; | ||
608 | |||
609 | /** | ||
610 | * Length of the address. | ||
611 | */ | ||
612 | socklen_t address_len; | ||
613 | |||
614 | /** | ||
615 | * Entry in sender expiration heap. | ||
616 | */ | ||
617 | struct GNUNET_CONTAINER_HeapNode *hn; | ||
618 | |||
619 | /** | ||
620 | * KX message queue we are providing for the #ch. | ||
621 | */ | ||
622 | struct GNUNET_MQ_Handle *kx_mq; | ||
623 | |||
624 | /** | ||
625 | * Default message queue we are providing for the #ch. | ||
626 | */ | ||
627 | struct GNUNET_MQ_Handle *d_mq; | ||
628 | |||
629 | /** | ||
630 | * handle for KX queue with the #ch. | ||
631 | */ | ||
632 | struct GNUNET_TRANSPORT_QueueHandle *kx_qh; | ||
633 | |||
634 | /** | ||
635 | * handle for default queue with the #ch. | ||
636 | */ | ||
637 | struct GNUNET_TRANSPORT_QueueHandle *d_qh; | ||
638 | |||
639 | /** | ||
640 | * Timeout for this receiver address. | ||
641 | */ | ||
642 | struct GNUNET_TIME_Absolute timeout; | ||
643 | |||
644 | /** | ||
645 | * MTU we allowed transport for this receiver's KX queue. | ||
646 | */ | ||
647 | size_t kx_mtu; | ||
648 | |||
649 | /** | ||
650 | * MTU we allowed transport for this receiver's default queue. | ||
651 | */ | ||
652 | size_t d_mtu; | ||
653 | |||
654 | /** | ||
655 | * Length of the DLL at @a ss_head. | ||
656 | */ | ||
657 | unsigned int num_secrets; | ||
658 | |||
659 | /** | ||
660 | * Number of BOX keys from ACKs we have currently | ||
661 | * available for this receiver. | ||
662 | */ | ||
663 | unsigned int acks_available; | ||
664 | |||
665 | /** | ||
666 | * Which network type does this queue use? | ||
667 | */ | ||
668 | enum GNUNET_NetworkType nt; | ||
669 | |||
670 | /** | ||
671 | * receiver_destroy already called on receiver. | ||
672 | */ | ||
673 | int receiver_destroy_called; | ||
674 | }; | ||
675 | |||
676 | /** | ||
677 | * Interface we broadcast our presence on. | ||
678 | */ | ||
679 | struct BroadcastInterface | ||
680 | { | ||
681 | /** | ||
682 | * Kept in a DLL. | ||
683 | */ | ||
684 | struct BroadcastInterface *next; | ||
685 | |||
686 | /** | ||
687 | * Kept in a DLL. | ||
688 | */ | ||
689 | struct BroadcastInterface *prev; | ||
690 | |||
691 | /** | ||
692 | * Task for this broadcast interface. | ||
693 | */ | ||
694 | struct GNUNET_SCHEDULER_Task *broadcast_task; | ||
695 | |||
696 | /** | ||
697 | * Sender's address of the interface. | ||
698 | */ | ||
699 | struct sockaddr *sa; | ||
700 | |||
701 | /** | ||
702 | * Broadcast address to use on the interface. | ||
703 | */ | ||
704 | struct sockaddr *ba; | ||
705 | |||
706 | /** | ||
707 | * Message we broadcast on this interface. | ||
708 | */ | ||
709 | struct UDPBroadcast bcm; | ||
710 | |||
711 | /** | ||
712 | * If this is an IPv6 interface, this is the request | ||
713 | * we use to join/leave the group. | ||
714 | */ | ||
715 | struct ipv6_mreq mcreq; | ||
716 | |||
717 | /** | ||
718 | * Number of bytes in @e sa. | ||
719 | */ | ||
720 | socklen_t salen; | ||
721 | |||
722 | /** | ||
723 | * Was this interface found in the last #iface_proc() scan? | ||
724 | */ | ||
725 | int found; | ||
726 | }; | ||
727 | |||
728 | /** | ||
729 | * The rekey interval | ||
730 | */ | ||
731 | static struct GNUNET_TIME_Relative rekey_interval; | ||
732 | |||
733 | /** | ||
734 | * How often we do rekey based on number of bytes transmitted | ||
735 | */ | ||
736 | static unsigned long long rekey_max_bytes; | ||
737 | |||
738 | /** | ||
739 | * Cache of pre-generated key IDs. | ||
740 | */ | ||
741 | static struct GNUNET_CONTAINER_MultiShortmap *key_cache; | ||
742 | |||
743 | /** | ||
744 | * ID of read task | ||
745 | */ | ||
746 | static struct GNUNET_SCHEDULER_Task *read_task; | ||
747 | |||
748 | /** | ||
749 | * ID of timeout task | ||
750 | */ | ||
751 | static struct GNUNET_SCHEDULER_Task *timeout_task; | ||
752 | |||
753 | /** | ||
754 | * ID of master broadcast task | ||
755 | */ | ||
756 | static struct GNUNET_SCHEDULER_Task *broadcast_task; | ||
757 | |||
758 | /** | ||
759 | * For logging statistics. | ||
760 | */ | ||
761 | static struct GNUNET_STATISTICS_Handle *stats; | ||
762 | |||
763 | /** | ||
764 | * Our environment. | ||
765 | */ | ||
766 | static struct GNUNET_TRANSPORT_CommunicatorHandle *ch; | ||
767 | |||
768 | /** | ||
769 | * Receivers (map from peer identity to `struct ReceiverAddress`) | ||
770 | */ | ||
771 | static struct GNUNET_CONTAINER_MultiPeerMap *receivers; | ||
772 | |||
773 | /** | ||
774 | * Senders (map from peer identity to `struct SenderAddress`) | ||
775 | */ | ||
776 | static struct GNUNET_CONTAINER_MultiPeerMap *senders; | ||
777 | |||
778 | /** | ||
779 | * Expiration heap for senders (contains `struct SenderAddress`) | ||
780 | */ | ||
781 | static struct GNUNET_CONTAINER_Heap *senders_heap; | ||
782 | |||
783 | /** | ||
784 | * Expiration heap for receivers (contains `struct ReceiverAddress`) | ||
785 | */ | ||
786 | static struct GNUNET_CONTAINER_Heap *receivers_heap; | ||
787 | |||
788 | /** | ||
789 | * Broadcast interface tasks. Kept in a DLL. | ||
790 | */ | ||
791 | static struct BroadcastInterface *bi_head; | ||
792 | |||
793 | /** | ||
794 | * Broadcast interface tasks. Kept in a DLL. | ||
795 | */ | ||
796 | static struct BroadcastInterface *bi_tail; | ||
797 | |||
798 | /** | ||
799 | * Our socket. | ||
800 | */ | ||
801 | static struct GNUNET_NETWORK_Handle *udp_sock; | ||
802 | |||
803 | /** | ||
804 | * #GNUNET_YES if #udp_sock supports IPv6. | ||
805 | */ | ||
806 | static int have_v6_socket; | ||
807 | |||
808 | /** | ||
809 | * Our public key. | ||
810 | */ | ||
811 | static struct GNUNET_PeerIdentity my_identity; | ||
812 | |||
813 | /** | ||
814 | * Our private key. | ||
815 | */ | ||
816 | static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key; | ||
817 | |||
818 | /** | ||
819 | * Our configuration. | ||
820 | */ | ||
821 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
822 | |||
823 | /** | ||
824 | * Our handle to report addresses for validation to TRANSPORT. | ||
825 | */ | ||
826 | static struct GNUNET_TRANSPORT_ApplicationHandle *ah; | ||
827 | |||
828 | /** | ||
829 | * Network scanner to determine network types. | ||
830 | */ | ||
831 | static struct GNUNET_NT_InterfaceScanner *is; | ||
832 | |||
833 | /** | ||
834 | * Connection to NAT service. | ||
835 | */ | ||
836 | static struct GNUNET_NAT_Handle *nat; | ||
837 | |||
838 | /** | ||
839 | * Port number to which we are actually bound. | ||
840 | */ | ||
841 | static uint16_t my_port; | ||
842 | |||
843 | |||
844 | /** | ||
845 | * An interface went away, stop broadcasting on it. | ||
846 | * | ||
847 | * @param bi entity to close down | ||
848 | */ | ||
849 | static void | ||
850 | bi_destroy (struct BroadcastInterface *bi) | ||
851 | { | ||
852 | if (AF_INET6 == bi->sa->sa_family) | ||
853 | { | ||
854 | /* Leave the multicast group */ | ||
855 | if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt (udp_sock, | ||
856 | IPPROTO_IPV6, | ||
857 | IPV6_LEAVE_GROUP, | ||
858 | &bi->mcreq, | ||
859 | sizeof(bi->mcreq))) | ||
860 | { | ||
861 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); | ||
862 | } | ||
863 | } | ||
864 | GNUNET_CONTAINER_DLL_remove (bi_head, bi_tail, bi); | ||
865 | GNUNET_SCHEDULER_cancel (bi->broadcast_task); | ||
866 | GNUNET_free (bi->sa); | ||
867 | GNUNET_free (bi->ba); | ||
868 | GNUNET_free (bi); | ||
869 | } | ||
870 | |||
871 | |||
872 | /** | ||
873 | * Destroys a receiving state due to timeout or shutdown. | ||
874 | * | ||
875 | * @param receiver entity to close down | ||
876 | */ | ||
877 | static void | ||
878 | receiver_destroy (struct ReceiverAddress *receiver) | ||
879 | { | ||
880 | |||
881 | receiver->receiver_destroy_called = GNUNET_YES; | ||
882 | |||
883 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
884 | "Disconnecting receiver for peer `%s'\n", | ||
885 | GNUNET_i2s (&receiver->target)); | ||
886 | if (NULL != receiver->kx_qh) | ||
887 | { | ||
888 | GNUNET_TRANSPORT_communicator_mq_del (receiver->kx_qh); | ||
889 | receiver->kx_qh = NULL; | ||
890 | receiver->kx_mq = NULL; | ||
891 | } | ||
892 | if (NULL != receiver->d_qh) | ||
893 | { | ||
894 | GNUNET_TRANSPORT_communicator_mq_del (receiver->d_qh); | ||
895 | receiver->d_qh = NULL; | ||
896 | } | ||
897 | GNUNET_assert (GNUNET_YES == | ||
898 | GNUNET_CONTAINER_multipeermap_remove (receivers, | ||
899 | &receiver->target, | ||
900 | receiver)); | ||
901 | GNUNET_assert (receiver == GNUNET_CONTAINER_heap_remove_node (receiver->hn)); | ||
902 | GNUNET_STATISTICS_set (stats, | ||
903 | "# receivers active", | ||
904 | GNUNET_CONTAINER_multipeermap_size (receivers), | ||
905 | GNUNET_NO); | ||
906 | GNUNET_free (receiver->address); | ||
907 | GNUNET_free (receiver->foreign_addr); | ||
908 | GNUNET_free (receiver); | ||
909 | } | ||
910 | |||
911 | |||
912 | /** | ||
913 | * Free memory used by key cache entry. | ||
914 | * | ||
915 | * @param kce the key cache entry | ||
916 | */ | ||
917 | static void | ||
918 | kce_destroy (struct KeyCacheEntry *kce) | ||
919 | { | ||
920 | struct SharedSecret *ss = kce->ss; | ||
921 | |||
922 | ss->active_kce_count--; | ||
923 | ss->sender->acks_available--; | ||
924 | GNUNET_CONTAINER_DLL_remove (ss->kce_head, ss->kce_tail, kce); | ||
925 | GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multishortmap_remove (key_cache, | ||
926 | &kce->kid, | ||
927 | kce)); | ||
928 | GNUNET_free (kce); | ||
929 | } | ||
930 | |||
931 | |||
932 | /** | ||
933 | * Compute @a kid. | ||
934 | * | ||
935 | * @param msec master secret for HMAC calculation | ||
936 | * @param serial number for the @a smac calculation | ||
937 | * @param[out] kid where to write the key ID | ||
938 | */ | ||
939 | static void | ||
940 | get_kid (const struct GNUNET_HashCode *msec, | ||
941 | uint32_t serial, | ||
942 | struct GNUNET_ShortHashCode *kid) | ||
943 | { | ||
944 | uint32_t sid = htonl (serial); | ||
945 | |||
946 | GNUNET_CRYPTO_hkdf (kid, | ||
947 | sizeof(*kid), | ||
948 | GCRY_MD_SHA512, | ||
949 | GCRY_MD_SHA256, | ||
950 | &sid, | ||
951 | sizeof(sid), | ||
952 | msec, | ||
953 | sizeof(*msec), | ||
954 | "UDP-KID", | ||
955 | strlen ("UDP-KID"), | ||
956 | NULL, | ||
957 | 0); | ||
958 | } | ||
959 | |||
960 | |||
961 | /** | ||
962 | * Setup key cache entry for sequence number @a seq and shared secret @a ss. | ||
963 | * | ||
964 | * @param ss shared secret | ||
965 | * @param seq sequence number for the key cache entry | ||
966 | */ | ||
967 | static void | ||
968 | kce_generate (struct SharedSecret *ss, uint32_t seq) | ||
969 | { | ||
970 | struct KeyCacheEntry *kce; | ||
971 | |||
972 | GNUNET_assert (0 < seq); | ||
973 | kce = GNUNET_new (struct KeyCacheEntry); | ||
974 | kce->ss = ss; | ||
975 | kce->sequence_number = seq; | ||
976 | get_kid (&ss->master, seq, &kce->kid); | ||
977 | GNUNET_CONTAINER_DLL_insert (ss->kce_head, ss->kce_tail, kce); | ||
978 | ss->active_kce_count++; | ||
979 | ss->sender->acks_available++; | ||
980 | (void) GNUNET_CONTAINER_multishortmap_put ( | ||
981 | key_cache, | ||
982 | &kce->kid, | ||
983 | kce, | ||
984 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); | ||
985 | GNUNET_STATISTICS_set (stats, | ||
986 | "# KIDs active", | ||
987 | GNUNET_CONTAINER_multishortmap_size (key_cache), | ||
988 | GNUNET_NO); | ||
989 | } | ||
990 | |||
991 | |||
992 | /** | ||
993 | * Destroy @a ss and associated key cache entries. | ||
994 | * | ||
995 | * @param ss shared secret to destroy | ||
996 | * @param withoutKce If GNUNET_YES shared secrets with kce will not be destroyed. | ||
997 | */ | ||
998 | static int | ||
999 | secret_destroy (struct SharedSecret *ss) | ||
1000 | { | ||
1001 | struct SenderAddress *sender; | ||
1002 | struct ReceiverAddress *receiver; | ||
1003 | struct KeyCacheEntry *kce; | ||
1004 | |||
1005 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1006 | "secret %s destroy %u\n", | ||
1007 | GNUNET_h2s (&ss->master), | ||
1008 | ss->sequence_allowed); | ||
1009 | if (NULL != (sender = ss->sender)) | ||
1010 | { | ||
1011 | GNUNET_CONTAINER_DLL_remove (sender->ss_head, sender->ss_tail, ss); | ||
1012 | sender->num_secrets--; | ||
1013 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1014 | "%u sender->num_secrets\n", | ||
1015 | receiver->num_secrets); | ||
1016 | if (NULL != ss->sender->kce_task) | ||
1017 | { | ||
1018 | GNUNET_SCHEDULER_cancel (ss->sender->kce_task); | ||
1019 | ss->sender->kce_task = NULL; | ||
1020 | } | ||
1021 | } | ||
1022 | if (NULL != (receiver = ss->receiver)) | ||
1023 | { | ||
1024 | GNUNET_CONTAINER_DLL_remove (receiver->ss_head, receiver->ss_tail, ss); | ||
1025 | receiver->num_secrets--; | ||
1026 | receiver->acks_available -= (ss->sequence_allowed - ss->sequence_used); | ||
1027 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1028 | "%u receiver->num_secrets\n", | ||
1029 | receiver->num_secrets); | ||
1030 | } | ||
1031 | while (NULL != (kce = ss->kce_head)) | ||
1032 | kce_destroy (kce); | ||
1033 | GNUNET_STATISTICS_update (stats, "# Secrets active", -1, GNUNET_NO); | ||
1034 | GNUNET_STATISTICS_set (stats, | ||
1035 | "# KIDs active", | ||
1036 | GNUNET_CONTAINER_multishortmap_size (key_cache), | ||
1037 | GNUNET_NO); | ||
1038 | GNUNET_free (ss); | ||
1039 | return GNUNET_YES; | ||
1040 | } | ||
1041 | |||
1042 | |||
1043 | /** | ||
1044 | * Functions with this signature are called whenever we need | ||
1045 | * to close a sender's state due to timeout. | ||
1046 | * | ||
1047 | * @param sender entity to close down | ||
1048 | */ | ||
1049 | static void | ||
1050 | sender_destroy (struct SenderAddress *sender) | ||
1051 | { | ||
1052 | sender->sender_destroy_called = GNUNET_YES; | ||
1053 | GNUNET_assert ( | ||
1054 | GNUNET_YES == | ||
1055 | GNUNET_CONTAINER_multipeermap_remove (senders, &sender->target, sender)); | ||
1056 | GNUNET_assert (sender == GNUNET_CONTAINER_heap_remove_node (sender->hn)); | ||
1057 | GNUNET_STATISTICS_set (stats, | ||
1058 | "# senders active", | ||
1059 | GNUNET_CONTAINER_multipeermap_size (senders), | ||
1060 | GNUNET_NO); | ||
1061 | GNUNET_free (sender->address); | ||
1062 | GNUNET_free (sender); | ||
1063 | } | ||
1064 | |||
1065 | |||
1066 | /** | ||
1067 | * Compute @a key and @a iv. | ||
1068 | * | ||
1069 | * @param msec master secret for calculation | ||
1070 | * @param serial number for the @a smac calculation | ||
1071 | * @param[out] key where to write the decryption key | ||
1072 | * @param[out] iv where to write the IV | ||
1073 | */ | ||
1074 | static void | ||
1075 | get_iv_key (const struct GNUNET_HashCode *msec, | ||
1076 | uint32_t serial, | ||
1077 | char key[AES_KEY_SIZE], | ||
1078 | char iv[AES_IV_SIZE]) | ||
1079 | { | ||
1080 | uint32_t sid = htonl (serial); | ||
1081 | char res[AES_KEY_SIZE + AES_IV_SIZE]; | ||
1082 | |||
1083 | GNUNET_CRYPTO_hkdf (res, | ||
1084 | sizeof(res), | ||
1085 | GCRY_MD_SHA512, | ||
1086 | GCRY_MD_SHA256, | ||
1087 | &sid, | ||
1088 | sizeof(sid), | ||
1089 | msec, | ||
1090 | sizeof(*msec), | ||
1091 | "UDP-IV-KEY", | ||
1092 | strlen ("UDP-IV-KEY"), | ||
1093 | NULL, | ||
1094 | 0); | ||
1095 | memcpy (key, res, AES_KEY_SIZE); | ||
1096 | memcpy (iv, &res[AES_KEY_SIZE], AES_IV_SIZE); | ||
1097 | } | ||
1098 | |||
1099 | |||
1100 | /** | ||
1101 | * Increment sender timeout due to activity. | ||
1102 | * | ||
1103 | * @param sender address for which the timeout should be rescheduled | ||
1104 | */ | ||
1105 | static void | ||
1106 | reschedule_sender_timeout (struct SenderAddress *sender) | ||
1107 | { | ||
1108 | sender->timeout = | ||
1109 | GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); | ||
1110 | GNUNET_CONTAINER_heap_update_cost (sender->hn, sender->timeout.abs_value_us); | ||
1111 | } | ||
1112 | |||
1113 | |||
1114 | /** | ||
1115 | * Increment receiver timeout due to activity. | ||
1116 | * | ||
1117 | * @param receiver address for which the timeout should be rescheduled | ||
1118 | */ | ||
1119 | static void | ||
1120 | reschedule_receiver_timeout (struct ReceiverAddress *receiver) | ||
1121 | { | ||
1122 | receiver->timeout = | ||
1123 | GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); | ||
1124 | GNUNET_CONTAINER_heap_update_cost (receiver->hn, | ||
1125 | receiver->timeout.abs_value_us); | ||
1126 | } | ||
1127 | |||
1128 | |||
1129 | /** | ||
1130 | * Task run to check #receiver_heap and #sender_heap for timeouts. | ||
1131 | * | ||
1132 | * @param cls unused, NULL | ||
1133 | */ | ||
1134 | static void | ||
1135 | check_timeouts (void *cls) | ||
1136 | { | ||
1137 | struct GNUNET_TIME_Relative st; | ||
1138 | struct GNUNET_TIME_Relative rt; | ||
1139 | struct GNUNET_TIME_Relative delay; | ||
1140 | struct ReceiverAddress *receiver; | ||
1141 | struct SenderAddress *sender; | ||
1142 | |||
1143 | (void) cls; | ||
1144 | timeout_task = NULL; | ||
1145 | rt = GNUNET_TIME_UNIT_FOREVER_REL; | ||
1146 | while (NULL != (receiver = GNUNET_CONTAINER_heap_peek (receivers_heap))) | ||
1147 | { | ||
1148 | rt = GNUNET_TIME_absolute_get_remaining (receiver->timeout); | ||
1149 | if (0 != rt.rel_value_us) | ||
1150 | break; | ||
1151 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1152 | "Receiver timed out\n"); | ||
1153 | receiver_destroy (receiver); | ||
1154 | } | ||
1155 | st = GNUNET_TIME_UNIT_FOREVER_REL; | ||
1156 | while (NULL != (sender = GNUNET_CONTAINER_heap_peek (senders_heap))) | ||
1157 | { | ||
1158 | if (GNUNET_YES != sender->sender_destroy_called) | ||
1159 | { | ||
1160 | st = GNUNET_TIME_absolute_get_remaining (sender->timeout); | ||
1161 | if (0 != st.rel_value_us) | ||
1162 | break; | ||
1163 | sender_destroy (sender); | ||
1164 | } | ||
1165 | } | ||
1166 | delay = GNUNET_TIME_relative_min (rt, st); | ||
1167 | if (delay.rel_value_us < GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) | ||
1168 | timeout_task = GNUNET_SCHEDULER_add_delayed (delay, &check_timeouts, NULL); | ||
1169 | } | ||
1170 | |||
1171 | |||
1172 | /** | ||
1173 | * Calculate cmac from master in @a ss. | ||
1174 | * | ||
1175 | * @param[in,out] ss data structure to complete | ||
1176 | */ | ||
1177 | static void | ||
1178 | calculate_cmac (struct SharedSecret *ss) | ||
1179 | { | ||
1180 | GNUNET_CRYPTO_hkdf (&ss->cmac, | ||
1181 | sizeof(ss->cmac), | ||
1182 | GCRY_MD_SHA512, | ||
1183 | GCRY_MD_SHA256, | ||
1184 | "CMAC", | ||
1185 | strlen ("CMAC"), | ||
1186 | &ss->master, | ||
1187 | sizeof(ss->master), | ||
1188 | "UDP-CMAC", | ||
1189 | strlen ("UDP-CMAC"), | ||
1190 | NULL, | ||
1191 | 0); | ||
1192 | } | ||
1193 | |||
1194 | |||
1195 | /** | ||
1196 | * We received @a plaintext_len bytes of @a plaintext from a @a sender. | ||
1197 | * Pass it on to CORE. | ||
1198 | * | ||
1199 | * @param queue the queue that received the plaintext | ||
1200 | * @param plaintext the plaintext that was received | ||
1201 | * @param plaintext_len number of bytes of plaintext received | ||
1202 | */ | ||
1203 | static void | ||
1204 | pass_plaintext_to_core (struct SenderAddress *sender, | ||
1205 | const void *plaintext, | ||
1206 | size_t plaintext_len) | ||
1207 | { | ||
1208 | const struct GNUNET_MessageHeader *hdr = plaintext; | ||
1209 | const char *pos = plaintext; | ||
1210 | |||
1211 | while (ntohs (hdr->size) <= plaintext_len) | ||
1212 | { | ||
1213 | GNUNET_STATISTICS_update (stats, | ||
1214 | "# bytes given to core", | ||
1215 | ntohs (hdr->size), | ||
1216 | GNUNET_NO); | ||
1217 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1218 | "Giving %u bytes to TNG\n", ntohs (hdr->size)); | ||
1219 | GNUNET_assert (GNUNET_SYSERR != | ||
1220 | GNUNET_TRANSPORT_communicator_receive (ch, | ||
1221 | &sender->target, | ||
1222 | hdr, | ||
1223 | ADDRESS_VALIDITY_PERIOD, | ||
1224 | NULL /* no flow control possible */ | ||
1225 | , | ||
1226 | NULL)); | ||
1227 | /* move on to next message, if any */ | ||
1228 | plaintext_len -= ntohs (hdr->size); | ||
1229 | if (plaintext_len < sizeof(*hdr)) | ||
1230 | break; | ||
1231 | pos += ntohs (hdr->size); | ||
1232 | hdr = (const struct GNUNET_MessageHeader *) pos; | ||
1233 | // TODO for now..., we do not actually sen >1msg or have a way of telling | ||
1234 | // if we are done | ||
1235 | break; | ||
1236 | } | ||
1237 | GNUNET_STATISTICS_update (stats, | ||
1238 | "# bytes padding discarded", | ||
1239 | plaintext_len, | ||
1240 | GNUNET_NO); | ||
1241 | } | ||
1242 | |||
1243 | |||
1244 | /** | ||
1245 | * Setup @a cipher based on shared secret @a msec and | ||
1246 | * serial number @a serial. | ||
1247 | * | ||
1248 | * @param msec master shared secret | ||
1249 | * @param serial serial number of cipher to set up | ||
1250 | * @param cipher[out] cipher to initialize | ||
1251 | */ | ||
1252 | static void | ||
1253 | setup_cipher (const struct GNUNET_HashCode *msec, | ||
1254 | uint32_t serial, | ||
1255 | gcry_cipher_hd_t *cipher) | ||
1256 | { | ||
1257 | char key[AES_KEY_SIZE]; | ||
1258 | char iv[AES_IV_SIZE]; | ||
1259 | int rc; | ||
1260 | |||
1261 | GNUNET_assert (0 == | ||
1262 | gcry_cipher_open (cipher, | ||
1263 | GCRY_CIPHER_AES256 /* low level: go for speed */, | ||
1264 | GCRY_CIPHER_MODE_GCM, | ||
1265 | 0 /* flags */)); | ||
1266 | get_iv_key (msec, serial, key, iv); | ||
1267 | rc = gcry_cipher_setkey (*cipher, key, sizeof(key)); | ||
1268 | GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); | ||
1269 | rc = gcry_cipher_setiv (*cipher, iv, sizeof(iv)); | ||
1270 | GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY)); | ||
1271 | } | ||
1272 | |||
1273 | |||
1274 | /** | ||
1275 | * Try to decrypt @a buf using shared secret @a ss and key/iv | ||
1276 | * derived using @a serial. | ||
1277 | * | ||
1278 | * @param ss shared secret | ||
1279 | * @param tag GCM authentication tag | ||
1280 | * @param serial serial number to use | ||
1281 | * @param in_buf input buffer to decrypt | ||
1282 | * @param in_buf_size number of bytes in @a in_buf and available in @a out_buf | ||
1283 | * @param out_buf where to write the result | ||
1284 | * @return #GNUNET_OK on success | ||
1285 | */ | ||
1286 | static int | ||
1287 | try_decrypt (const struct SharedSecret *ss, | ||
1288 | const uint8_t *tag, | ||
1289 | uint32_t serial, | ||
1290 | const char *in_buf, | ||
1291 | size_t in_buf_size, | ||
1292 | char *out_buf) | ||
1293 | { | ||
1294 | gcry_cipher_hd_t cipher; | ||
1295 | |||
1296 | setup_cipher (&ss->master, serial, &cipher); | ||
1297 | GNUNET_assert ( | ||
1298 | 0 == | ||
1299 | gcry_cipher_decrypt (cipher, out_buf, in_buf_size, in_buf, in_buf_size)); | ||
1300 | if (0 != gcry_cipher_checktag (cipher, tag, GCM_TAG_SIZE)) | ||
1301 | { | ||
1302 | gcry_cipher_close (cipher); | ||
1303 | GNUNET_STATISTICS_update (stats, | ||
1304 | "# AEAD authentication failures", | ||
1305 | 1, | ||
1306 | GNUNET_NO); | ||
1307 | return GNUNET_SYSERR; | ||
1308 | } | ||
1309 | gcry_cipher_close (cipher); | ||
1310 | return GNUNET_OK; | ||
1311 | } | ||
1312 | |||
1313 | |||
1314 | /** | ||
1315 | * Setup shared secret for decryption. | ||
1316 | * | ||
1317 | * @param ephemeral ephemeral key we received from the other peer | ||
1318 | * @return new shared secret | ||
1319 | */ | ||
1320 | static struct SharedSecret * | ||
1321 | setup_shared_secret_dec (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral) | ||
1322 | { | ||
1323 | struct SharedSecret *ss; | ||
1324 | |||
1325 | ss = GNUNET_new (struct SharedSecret); | ||
1326 | GNUNET_CRYPTO_eddsa_kem_decaps (my_private_key, ephemeral, &ss->master); | ||
1327 | calculate_cmac (ss); | ||
1328 | return ss; | ||
1329 | } | ||
1330 | |||
1331 | |||
1332 | /** | ||
1333 | * Setup new shared secret for encryption using KEM. | ||
1334 | * | ||
1335 | * @param[out] ephemeral ephemeral key to be sent to other peer (encapsulated key from KEM) | ||
1336 | * @param[in,out] receiver queue to initialize encryption key for | ||
1337 | * @return new shared secret | ||
1338 | */ | ||
1339 | static struct SharedSecret * | ||
1340 | setup_shared_secret_ephemeral (struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral, | ||
1341 | struct ReceiverAddress *receiver) | ||
1342 | { | ||
1343 | struct SharedSecret *ss; | ||
1344 | struct GNUNET_HashCode k; | ||
1345 | |||
1346 | GNUNET_CRYPTO_eddsa_kem_encaps (&receiver->target.public_key, ephemeral, &k); | ||
1347 | ss = GNUNET_new (struct SharedSecret); | ||
1348 | memcpy (&ss->master, &k, sizeof (k)); | ||
1349 | calculate_cmac (ss); | ||
1350 | ss->receiver = receiver; | ||
1351 | GNUNET_CONTAINER_DLL_insert (receiver->ss_head, receiver->ss_tail, ss); | ||
1352 | receiver->num_secrets++; | ||
1353 | GNUNET_STATISTICS_update (stats, "# Secrets active", 1, GNUNET_NO); | ||
1354 | return ss; | ||
1355 | } | ||
1356 | |||
1357 | |||
1358 | /** | ||
1359 | * Setup the MQ for the @a receiver. If a queue exists, | ||
1360 | * the existing one is destroyed. Then the MTU is | ||
1361 | * recalculated and a fresh queue is initialized. | ||
1362 | * | ||
1363 | * @param receiver receiver to setup MQ for | ||
1364 | */ | ||
1365 | static void | ||
1366 | setup_receiver_mq (struct ReceiverAddress *receiver); | ||
1367 | |||
1368 | |||
1369 | /** | ||
1370 | * Best effort try to purge some secrets. | ||
1371 | * Ideally those, not ACKed. | ||
1372 | * | ||
1373 | * @param ss_list_tail the oldest secret in the list of interest. | ||
1374 | * @return number of deleted secrets. | ||
1375 | */ | ||
1376 | unsigned int | ||
1377 | purge_secrets (struct SharedSecret *ss_list_tail) | ||
1378 | { | ||
1379 | struct SharedSecret *pos; | ||
1380 | struct SharedSecret *ss_to_purge; | ||
1381 | unsigned int deleted = 0; | ||
1382 | |||
1383 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1384 | "Purging secrets.\n"); | ||
1385 | pos = ss_list_tail; | ||
1386 | while (NULL != pos) | ||
1387 | { | ||
1388 | ss_to_purge = pos; | ||
1389 | pos = pos->prev; | ||
1390 | |||
1391 | // FIXME we may also want to purge old unacked. | ||
1392 | if (rekey_max_bytes <= ss_to_purge->bytes_sent) | ||
1393 | { | ||
1394 | secret_destroy (ss_to_purge); | ||
1395 | deleted++; | ||
1396 | } | ||
1397 | } | ||
1398 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1399 | "Finished purging all, deleted %u.\n", deleted); | ||
1400 | return deleted; | ||
1401 | } | ||
1402 | |||
1403 | |||
1404 | static void | ||
1405 | add_acks (struct SharedSecret *ss, int acks_to_add) | ||
1406 | { | ||
1407 | |||
1408 | struct ReceiverAddress *receiver = ss->receiver; | ||
1409 | |||
1410 | GNUNET_assert (NULL != ss); | ||
1411 | GNUNET_assert (NULL != receiver); | ||
1412 | |||
1413 | if (NULL == receiver->d_qh) | ||
1414 | { | ||
1415 | receiver->d_qh = | ||
1416 | GNUNET_TRANSPORT_communicator_mq_add (ch, | ||
1417 | &receiver->target, | ||
1418 | receiver->foreign_addr, | ||
1419 | receiver->d_mtu, | ||
1420 | acks_to_add, | ||
1421 | 1, /* Priority */ | ||
1422 | receiver->nt, | ||
1423 | GNUNET_TRANSPORT_CS_OUTBOUND, | ||
1424 | receiver->d_mq); | ||
1425 | } | ||
1426 | else | ||
1427 | { | ||
1428 | GNUNET_TRANSPORT_communicator_mq_update (ch, | ||
1429 | receiver->d_qh, | ||
1430 | acks_to_add, | ||
1431 | 1); | ||
1432 | } | ||
1433 | |||
1434 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1435 | "Tell transport we have %u more acks!\n", | ||
1436 | acks_to_add); | ||
1437 | |||
1438 | // Until here for alternativ 1 | ||
1439 | |||
1440 | /* move ss to head to avoid discarding it anytime soon! */ | ||
1441 | |||
1442 | GNUNET_CONTAINER_DLL_remove (receiver->ss_head, receiver->ss_tail, ss); | ||
1443 | GNUNET_CONTAINER_DLL_insert (receiver->ss_head, receiver->ss_tail, ss); | ||
1444 | } | ||
1445 | |||
1446 | |||
1447 | /** | ||
1448 | * We received an ACK for @a pid. Check if it is for | ||
1449 | * the receiver in @a value and if so, handle it and | ||
1450 | * return #GNUNET_NO. Otherwise, return #GNUNET_YES. | ||
1451 | * | ||
1452 | * @param cls a `const struct UDPAck` | ||
1453 | * @param pid peer the ACK is from | ||
1454 | * @param value a `struct ReceiverAddress` | ||
1455 | * @return #GNUNET_YES to continue to iterate | ||
1456 | */ | ||
1457 | static int | ||
1458 | handle_ack (void *cls, const struct GNUNET_PeerIdentity *pid, void *value) | ||
1459 | { | ||
1460 | const struct UDPAck *ack = cls; | ||
1461 | struct ReceiverAddress *receiver = value; | ||
1462 | uint32_t acks_to_add; | ||
1463 | uint32_t allowed; | ||
1464 | |||
1465 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1466 | "in handle ack with cmac %s\n", | ||
1467 | GNUNET_h2s (&ack->cmac)); | ||
1468 | |||
1469 | (void) pid; | ||
1470 | for (struct SharedSecret *ss = receiver->ss_head; NULL != ss; ss = ss->next) | ||
1471 | { | ||
1472 | if (0 == memcmp (&ack->cmac, &ss->cmac, sizeof(struct GNUNET_HashCode))) | ||
1473 | { | ||
1474 | |||
1475 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1476 | "Found matching cmac\n"); | ||
1477 | |||
1478 | allowed = ntohl (ack->sequence_ack); | ||
1479 | |||
1480 | if (allowed <= ss->sequence_allowed) | ||
1481 | { | ||
1482 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1483 | "Ignoring ack, not giving us increased window\n."); | ||
1484 | return GNUNET_NO; | ||
1485 | } | ||
1486 | acks_to_add = (allowed - ss->sequence_allowed); | ||
1487 | GNUNET_assert (0 != acks_to_add); | ||
1488 | receiver->acks_available += (allowed - ss->sequence_allowed); | ||
1489 | ss->sequence_allowed = allowed; | ||
1490 | add_acks (ss, acks_to_add); | ||
1491 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1492 | "New sequence allows until %u (+%u). Acks available to us: %u. For secret %s\n", | ||
1493 | allowed, | ||
1494 | acks_to_add, | ||
1495 | receiver->acks_available, | ||
1496 | GNUNET_h2s (&ss->master)); | ||
1497 | return GNUNET_NO; | ||
1498 | } | ||
1499 | } | ||
1500 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1501 | "Matching cmac not found for ack!\n"); | ||
1502 | return GNUNET_YES; | ||
1503 | } | ||
1504 | |||
1505 | |||
1506 | /** | ||
1507 | * We established a shared secret with a sender. We should try to send | ||
1508 | * the sender an `struct UDPAck` at the next opportunity to allow the | ||
1509 | * sender to use @a ss longer (assuming we did not yet already | ||
1510 | * recently). | ||
1511 | * | ||
1512 | * @param ss shared secret to generate ACKs for | ||
1513 | */ | ||
1514 | static void | ||
1515 | consider_ss_ack (struct SharedSecret *ss) | ||
1516 | { | ||
1517 | struct UDPAck ack; | ||
1518 | GNUNET_assert (NULL != ss->sender); | ||
1519 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1520 | "Considering SS UDPAck %s\n", | ||
1521 | GNUNET_i2s_full (&ss->sender->target)); | ||
1522 | |||
1523 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1524 | "Sender has %u acks available.\n", | ||
1525 | ss->sender->acks_available); | ||
1526 | /* drop ancient KeyCacheEntries */ | ||
1527 | while ((NULL != ss->kce_head) && | ||
1528 | (MAX_SQN_DELTA < | ||
1529 | ss->kce_head->sequence_number - ss->kce_tail->sequence_number)) | ||
1530 | kce_destroy (ss->kce_tail); | ||
1531 | |||
1532 | |||
1533 | ack.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK); | ||
1534 | ack.header.size = htons (sizeof(ack)); | ||
1535 | ack.sequence_ack = htonl (ss->sequence_allowed); | ||
1536 | ack.cmac = ss->cmac; | ||
1537 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1538 | "Notifying transport with UDPAck %s, sequence %u and master %s\n", | ||
1539 | GNUNET_i2s_full (&ss->sender->target), | ||
1540 | ss->sequence_allowed, | ||
1541 | GNUNET_h2s (&(ss->master))); | ||
1542 | GNUNET_TRANSPORT_communicator_notify (ch, | ||
1543 | &ss->sender->target, | ||
1544 | COMMUNICATOR_ADDRESS_PREFIX, | ||
1545 | &ack.header); | ||
1546 | } | ||
1547 | |||
1548 | |||
1549 | static void | ||
1550 | kce_generate_cb (void *cls) | ||
1551 | { | ||
1552 | struct SharedSecret *ss = cls; | ||
1553 | ss->sender->kce_task = NULL; | ||
1554 | |||
1555 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1556 | "Precomputing %u keys for master %s\n", | ||
1557 | GENERATE_AT_ONCE, | ||
1558 | GNUNET_h2s (&(ss->master))); | ||
1559 | if ((ss->override_available_acks != GNUNET_YES) && | ||
1560 | (KCN_TARGET < ss->sender->acks_available)) | ||
1561 | return; | ||
1562 | for (int i = 0; i < GENERATE_AT_ONCE; i++) | ||
1563 | kce_generate (ss, ++ss->sequence_allowed); | ||
1564 | |||
1565 | /** | ||
1566 | * As long as we loose over 30% of max acks in reschedule, | ||
1567 | * We keep generating acks for this ss. | ||
1568 | */ | ||
1569 | if (KCN_TARGET > ss->sender->acks_available) | ||
1570 | { | ||
1571 | ss->sender->kce_task = GNUNET_SCHEDULER_add_delayed ( | ||
1572 | WORKING_QUEUE_INTERVALL, | ||
1573 | kce_generate_cb, | ||
1574 | ss); | ||
1575 | return; | ||
1576 | } | ||
1577 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1578 | "We have enough keys (ACKs: %u).\n", ss->sender->acks_available); | ||
1579 | ss->sender->kce_task_finished = GNUNET_YES; | ||
1580 | ss->override_available_acks = GNUNET_NO; | ||
1581 | if (ss->sender->kce_send_ack_on_finish == GNUNET_YES) | ||
1582 | consider_ss_ack (ss); | ||
1583 | } | ||
1584 | |||
1585 | |||
1586 | /** | ||
1587 | * Test if we have received a valid message in plaintext. | ||
1588 | * If so, handle it. | ||
1589 | * | ||
1590 | * @param sender peer to process inbound plaintext for | ||
1591 | * @param buf buffer we received | ||
1592 | * @param buf_size number of bytes in @a buf | ||
1593 | */ | ||
1594 | static void | ||
1595 | try_handle_plaintext (struct SenderAddress *sender, | ||
1596 | const void *buf, | ||
1597 | size_t buf_size) | ||
1598 | { | ||
1599 | const struct GNUNET_MessageHeader *hdr; | ||
1600 | const struct UDPAck *ack; | ||
1601 | const struct UDPRekey *rekey; | ||
1602 | struct SharedSecret *ss_rekey; | ||
1603 | const char *buf_pos = buf; | ||
1604 | size_t bytes_remaining = buf_size; | ||
1605 | uint16_t type; | ||
1606 | |||
1607 | hdr = (struct GNUNET_MessageHeader*) buf_pos; | ||
1608 | if (sizeof(*hdr) > bytes_remaining) | ||
1609 | { | ||
1610 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Plaintext too short, dropping...\n"); | ||
1611 | return; /* no data left */ | ||
1612 | } | ||
1613 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1614 | "try_handle_plaintext of size %lu (%u %lu) and type %u\n", | ||
1615 | bytes_remaining, | ||
1616 | ntohs (hdr->size), | ||
1617 | sizeof(*hdr), | ||
1618 | ntohs (hdr->type)); | ||
1619 | if (ntohs (hdr->size) > bytes_remaining) | ||
1620 | return; /* buffer too short for indicated message length */ | ||
1621 | type = ntohs (hdr->type); | ||
1622 | switch (type) | ||
1623 | { | ||
1624 | case GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_REKEY: | ||
1625 | rekey = (struct UDPRekey*) buf_pos; | ||
1626 | ss_rekey = setup_shared_secret_dec (&rekey->ephemeral); | ||
1627 | ss_rekey->sender = sender; | ||
1628 | GNUNET_CONTAINER_DLL_insert (sender->ss_head, sender->ss_tail, ss_rekey); | ||
1629 | sender->num_secrets++; | ||
1630 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
1631 | "Received rekey secret with cmac %s\n", | ||
1632 | GNUNET_h2s (&(ss_rekey->cmac))); | ||
1633 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1634 | "Received secret with master %s.\n", | ||
1635 | GNUNET_h2s (&(ss_rekey->master))); | ||
1636 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1637 | "We have %u sequence_allowed.\n", | ||
1638 | ss_rekey->sequence_allowed); | ||
1639 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1640 | "We have a sender %p\n", | ||
1641 | ss_rekey->sender); | ||
1642 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1643 | "We have %u acks available.\n", | ||
1644 | ss_rekey->sender->acks_available); | ||
1645 | ss_rekey->sender->kce_send_ack_on_finish = GNUNET_YES; | ||
1646 | ss_rekey->override_available_acks = GNUNET_YES; | ||
1647 | // FIXME | ||
1648 | ss_rekey->sender->kce_task = GNUNET_SCHEDULER_add_delayed ( | ||
1649 | WORKING_QUEUE_INTERVALL, | ||
1650 | kce_generate_cb, | ||
1651 | ss_rekey); | ||
1652 | // FIXME: Theoretically, this could be an Ack | ||
1653 | buf_pos += ntohs (hdr->size); | ||
1654 | bytes_remaining -= ntohs (hdr->size); | ||
1655 | pass_plaintext_to_core (sender, buf_pos, bytes_remaining); | ||
1656 | if (sender->num_secrets > MAX_SECRETS) | ||
1657 | { | ||
1658 | if (0 == purge_secrets (sender->ss_tail)) | ||
1659 | { | ||
1660 | // No secret purged. Delete oldest. | ||
1661 | secret_destroy (sender->ss_tail); | ||
1662 | } | ||
1663 | } | ||
1664 | break; | ||
1665 | case GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK: | ||
1666 | /* lookup master secret by 'cmac', then update sequence_max */ | ||
1667 | ack = (struct UDPAck*) buf_pos; | ||
1668 | GNUNET_CONTAINER_multipeermap_get_multiple (receivers, | ||
1669 | &sender->target, | ||
1670 | &handle_ack, | ||
1671 | (void *) ack); | ||
1672 | /* There could be more messages after the ACK, handle those as well */ | ||
1673 | buf_pos += ntohs (hdr->size); | ||
1674 | bytes_remaining -= ntohs (hdr->size); | ||
1675 | pass_plaintext_to_core (sender, buf_pos, bytes_remaining); | ||
1676 | break; | ||
1677 | |||
1678 | case GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_PAD: | ||
1679 | /* skip padding */ | ||
1680 | break; | ||
1681 | |||
1682 | default: | ||
1683 | pass_plaintext_to_core (sender, buf_pos, bytes_remaining); | ||
1684 | } | ||
1685 | return; | ||
1686 | } | ||
1687 | |||
1688 | |||
1689 | /** | ||
1690 | * We received a @a box with matching @a kce. Decrypt and process it. | ||
1691 | * | ||
1692 | * @param box the data we received | ||
1693 | * @param box_len number of bytes in @a box | ||
1694 | * @param kce key index to decrypt @a box | ||
1695 | */ | ||
1696 | static void | ||
1697 | decrypt_box (const struct UDPBox *box, | ||
1698 | size_t box_len, | ||
1699 | struct KeyCacheEntry *kce) | ||
1700 | { | ||
1701 | struct SharedSecret *ss = kce->ss; | ||
1702 | char out_buf[box_len - sizeof(*box)]; | ||
1703 | |||
1704 | GNUNET_assert (NULL != ss->sender); | ||
1705 | if (GNUNET_OK != try_decrypt (ss, | ||
1706 | box->gcm_tag, | ||
1707 | kce->sequence_number, | ||
1708 | (const char *) &box[1], | ||
1709 | sizeof(out_buf), | ||
1710 | out_buf)) | ||
1711 | { | ||
1712 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed decryption.\n"); | ||
1713 | GNUNET_STATISTICS_update (stats, | ||
1714 | "# Decryption failures with valid KCE", | ||
1715 | 1, | ||
1716 | GNUNET_NO); | ||
1717 | kce_destroy (kce); | ||
1718 | return; | ||
1719 | } | ||
1720 | kce_destroy (kce); | ||
1721 | kce = NULL; | ||
1722 | GNUNET_STATISTICS_update (stats, | ||
1723 | "# bytes decrypted with BOX", | ||
1724 | sizeof(out_buf), | ||
1725 | GNUNET_NO); | ||
1726 | GNUNET_STATISTICS_update (stats, | ||
1727 | "# messages decrypted with BOX", | ||
1728 | 1, | ||
1729 | GNUNET_NO); | ||
1730 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1731 | "decrypted UDPBox with kid %s\n", | ||
1732 | GNUNET_sh2s (&box->kid)); | ||
1733 | try_handle_plaintext (ss->sender, out_buf, sizeof(out_buf)); | ||
1734 | if ((KCN_THRESHOLD > ss->sender->acks_available) && | ||
1735 | (NULL == ss->sender->kce_task) && | ||
1736 | (GNUNET_YES == ss->sender->kce_task_finished)) | ||
1737 | { | ||
1738 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1739 | "Sender has %u ack left which is under threshold.\n", | ||
1740 | ss->sender->acks_available); | ||
1741 | ss->sender->kce_send_ack_on_finish = GNUNET_YES; | ||
1742 | ss->sender->kce_task = GNUNET_SCHEDULER_add_now ( | ||
1743 | kce_generate_cb, | ||
1744 | ss); | ||
1745 | } | ||
1746 | } | ||
1747 | |||
1748 | |||
1749 | /** | ||
1750 | * Closure for #find_sender_by_address() | ||
1751 | */ | ||
1752 | struct SearchContext | ||
1753 | { | ||
1754 | /** | ||
1755 | * Address we are looking for. | ||
1756 | */ | ||
1757 | const struct sockaddr *address; | ||
1758 | |||
1759 | /** | ||
1760 | * Number of bytes in @e address. | ||
1761 | */ | ||
1762 | socklen_t address_len; | ||
1763 | |||
1764 | /** | ||
1765 | * Return value to set if we found a match. | ||
1766 | */ | ||
1767 | struct SenderAddress *sender; | ||
1768 | }; | ||
1769 | |||
1770 | |||
1771 | /** | ||
1772 | * Find existing `struct SenderAddress` by matching addresses. | ||
1773 | * | ||
1774 | * @param cls a `struct SearchContext` | ||
1775 | * @param key ignored, must match already | ||
1776 | * @param value a `struct SenderAddress` | ||
1777 | * @return #GNUNET_YES if not found (continue to search), #GNUNET_NO if found | ||
1778 | */ | ||
1779 | static int | ||
1780 | find_sender_by_address (void *cls, | ||
1781 | const struct GNUNET_PeerIdentity *key, | ||
1782 | void *value) | ||
1783 | { | ||
1784 | struct SearchContext *sc = cls; | ||
1785 | struct SenderAddress *sender = value; | ||
1786 | |||
1787 | if ((sender->address_len == sc->address_len) && | ||
1788 | (0 == memcmp (sender->address, sc->address, sender->address_len))) | ||
1789 | { | ||
1790 | sc->sender = sender; | ||
1791 | return GNUNET_NO; /* stop iterating! */ | ||
1792 | } | ||
1793 | return GNUNET_YES; | ||
1794 | } | ||
1795 | |||
1796 | |||
1797 | /** | ||
1798 | * Create sender address for @a target. Note that we | ||
1799 | * might already have one, so a fresh one is only allocated | ||
1800 | * if one does not yet exist for @a address. | ||
1801 | * | ||
1802 | * @param target peer to generate address for | ||
1803 | * @param address target address | ||
1804 | * @param address_len number of bytes in @a address | ||
1805 | * @return data structure to keep track of key material for | ||
1806 | * decrypting data from @a target | ||
1807 | */ | ||
1808 | static struct SenderAddress * | ||
1809 | setup_sender (const struct GNUNET_PeerIdentity *target, | ||
1810 | const struct sockaddr *address, | ||
1811 | socklen_t address_len) | ||
1812 | { | ||
1813 | struct SenderAddress *sender; | ||
1814 | struct SearchContext sc = { .address = address, | ||
1815 | .address_len = address_len, | ||
1816 | .sender = NULL }; | ||
1817 | |||
1818 | GNUNET_CONTAINER_multipeermap_get_multiple (senders, | ||
1819 | target, | ||
1820 | &find_sender_by_address, | ||
1821 | &sc); | ||
1822 | if (NULL != sc.sender) | ||
1823 | { | ||
1824 | reschedule_sender_timeout (sc.sender); | ||
1825 | return sc.sender; | ||
1826 | } | ||
1827 | sender = GNUNET_new (struct SenderAddress); | ||
1828 | sender->target = *target; | ||
1829 | sender->address = GNUNET_memdup (address, address_len); | ||
1830 | sender->address_len = address_len; | ||
1831 | (void) GNUNET_CONTAINER_multipeermap_put ( | ||
1832 | senders, | ||
1833 | &sender->target, | ||
1834 | sender, | ||
1835 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); | ||
1836 | GNUNET_STATISTICS_set (stats, | ||
1837 | "# senders active", | ||
1838 | GNUNET_CONTAINER_multipeermap_size (receivers), | ||
1839 | GNUNET_NO); | ||
1840 | sender->timeout = | ||
1841 | GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); | ||
1842 | sender->hn = GNUNET_CONTAINER_heap_insert (senders_heap, | ||
1843 | sender, | ||
1844 | sender->timeout.abs_value_us); | ||
1845 | sender->nt = GNUNET_NT_scanner_get_type (is, address, address_len); | ||
1846 | if (NULL == timeout_task) | ||
1847 | timeout_task = GNUNET_SCHEDULER_add_now (&check_timeouts, NULL); | ||
1848 | return sender; | ||
1849 | } | ||
1850 | |||
1851 | |||
1852 | /** | ||
1853 | * Check signature from @a uc against @a ephemeral. | ||
1854 | * | ||
1855 | * @param ephemeral key that is signed | ||
1856 | * @param uc signature of claimant | ||
1857 | * @return #GNUNET_OK if signature is valid | ||
1858 | */ | ||
1859 | static int | ||
1860 | verify_confirmation (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral, | ||
1861 | const struct UDPConfirmation *uc) | ||
1862 | { | ||
1863 | struct UdpHandshakeSignature uhs; | ||
1864 | |||
1865 | uhs.purpose.purpose = htonl ( | ||
1866 | GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_UDP_HANDSHAKE); | ||
1867 | uhs.purpose.size = htonl (sizeof(uhs)); | ||
1868 | uhs.sender = uc->sender; | ||
1869 | uhs.receiver = my_identity; | ||
1870 | uhs.ephemeral = *ephemeral; | ||
1871 | uhs.monotonic_time = uc->monotonic_time; | ||
1872 | return GNUNET_CRYPTO_eddsa_verify ( | ||
1873 | GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_UDP_HANDSHAKE, | ||
1874 | &uhs, | ||
1875 | &uc->sender_sig, | ||
1876 | &uc->sender.public_key); | ||
1877 | } | ||
1878 | |||
1879 | |||
1880 | /** | ||
1881 | * Converts @a address to the address string format used by this | ||
1882 | * communicator in HELLOs. | ||
1883 | * | ||
1884 | * @param address the address to convert, must be AF_INET or AF_INET6. | ||
1885 | * @param address_len number of bytes in @a address | ||
1886 | * @return string representation of @a address | ||
1887 | */ | ||
1888 | static char * | ||
1889 | sockaddr_to_udpaddr_string (const struct sockaddr *address, | ||
1890 | socklen_t address_len) | ||
1891 | { | ||
1892 | char *ret; | ||
1893 | |||
1894 | switch (address->sa_family) | ||
1895 | { | ||
1896 | case AF_INET: | ||
1897 | GNUNET_asprintf (&ret, | ||
1898 | "%s-%s", | ||
1899 | COMMUNICATOR_ADDRESS_PREFIX, | ||
1900 | GNUNET_a2s (address, address_len)); | ||
1901 | break; | ||
1902 | |||
1903 | case AF_INET6: | ||
1904 | GNUNET_asprintf (&ret, | ||
1905 | "%s-%s", | ||
1906 | COMMUNICATOR_ADDRESS_PREFIX, | ||
1907 | GNUNET_a2s (address, address_len)); | ||
1908 | break; | ||
1909 | |||
1910 | default: | ||
1911 | GNUNET_assert (0); | ||
1912 | } | ||
1913 | return ret; | ||
1914 | } | ||
1915 | |||
1916 | |||
1917 | /** | ||
1918 | * Socket read task. | ||
1919 | * | ||
1920 | * @param cls NULL | ||
1921 | */ | ||
1922 | static void | ||
1923 | sock_read (void *cls) | ||
1924 | { | ||
1925 | struct sockaddr_storage sa; | ||
1926 | struct sockaddr_in *addr_verify; | ||
1927 | socklen_t salen = sizeof(sa); | ||
1928 | char buf[UINT16_MAX]; | ||
1929 | ssize_t rcvd; | ||
1930 | |||
1931 | (void) cls; | ||
1932 | read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
1933 | udp_sock, | ||
1934 | &sock_read, | ||
1935 | NULL); | ||
1936 | while (1) | ||
1937 | { | ||
1938 | rcvd = GNUNET_NETWORK_socket_recvfrom (udp_sock, | ||
1939 | buf, | ||
1940 | sizeof(buf), | ||
1941 | (struct sockaddr *) &sa, | ||
1942 | &salen); | ||
1943 | if (-1 == rcvd) | ||
1944 | { | ||
1945 | if (EAGAIN == errno) | ||
1946 | break; // We are done reading data | ||
1947 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "recv"); | ||
1948 | return; | ||
1949 | } | ||
1950 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1951 | "Read %lu bytes\n", rcvd); | ||
1952 | |||
1953 | /* first, see if it is a UDPBox */ | ||
1954 | if (rcvd > sizeof(struct UDPBox)) | ||
1955 | { | ||
1956 | const struct UDPBox *box; | ||
1957 | struct KeyCacheEntry *kce; | ||
1958 | |||
1959 | box = (const struct UDPBox *) buf; | ||
1960 | kce = GNUNET_CONTAINER_multishortmap_get (key_cache, &box->kid); | ||
1961 | if (NULL != kce) | ||
1962 | { | ||
1963 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1964 | "Found KCE with kid %s\n", | ||
1965 | GNUNET_sh2s (&box->kid)); | ||
1966 | decrypt_box (box, (size_t) rcvd, kce); | ||
1967 | continue; | ||
1968 | } | ||
1969 | } | ||
1970 | |||
1971 | /* next, check if it is a broadcast */ | ||
1972 | if (sizeof(struct UDPBroadcast) == rcvd) | ||
1973 | { | ||
1974 | const struct UDPBroadcast *ub; | ||
1975 | struct UdpBroadcastSignature uhs; | ||
1976 | struct GNUNET_PeerIdentity sender; | ||
1977 | |||
1978 | addr_verify = GNUNET_memdup (&sa, salen); | ||
1979 | addr_verify->sin_port = 0; | ||
1980 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1981 | "received UDPBroadcast from %s\n", | ||
1982 | GNUNET_a2s ((const struct sockaddr *) addr_verify, salen)); | ||
1983 | ub = (const struct UDPBroadcast *) buf; | ||
1984 | uhs.purpose.purpose = htonl ( | ||
1985 | GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_UDP_BROADCAST); | ||
1986 | uhs.purpose.size = htonl (sizeof(uhs)); | ||
1987 | uhs.sender = ub->sender; | ||
1988 | sender = ub->sender; | ||
1989 | if (0 == memcmp (&sender, &my_identity, sizeof (struct | ||
1990 | GNUNET_PeerIdentity))) | ||
1991 | { | ||
1992 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1993 | "Received our own broadcast\n"); | ||
1994 | GNUNET_free (addr_verify); | ||
1995 | continue; | ||
1996 | } | ||
1997 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1998 | "checking UDPBroadcastSignature for %s\n", | ||
1999 | GNUNET_i2s (&sender)); | ||
2000 | GNUNET_CRYPTO_hash ((struct sockaddr *) addr_verify, salen, | ||
2001 | &uhs.h_address); | ||
2002 | if (GNUNET_OK == | ||
2003 | GNUNET_CRYPTO_eddsa_verify ( | ||
2004 | GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_UDP_BROADCAST, | ||
2005 | &uhs, | ||
2006 | &ub->sender_sig, | ||
2007 | &ub->sender.public_key)) | ||
2008 | { | ||
2009 | char *addr_s; | ||
2010 | enum GNUNET_NetworkType nt; | ||
2011 | |||
2012 | addr_s = | ||
2013 | sockaddr_to_udpaddr_string ((const struct sockaddr *) &sa, salen); | ||
2014 | GNUNET_STATISTICS_update (stats, "# broadcasts received", 1, GNUNET_NO); | ||
2015 | /* use our own mechanism to determine network type */ | ||
2016 | nt = | ||
2017 | GNUNET_NT_scanner_get_type (is, (const struct sockaddr *) &sa, salen); | ||
2018 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2019 | "validating address %s received from UDPBroadcast\n", | ||
2020 | GNUNET_i2s (&sender)); | ||
2021 | GNUNET_TRANSPORT_application_validate (ah, &sender, nt, addr_s); | ||
2022 | GNUNET_free (addr_s); | ||
2023 | GNUNET_free (addr_verify); | ||
2024 | continue; | ||
2025 | } | ||
2026 | else | ||
2027 | { | ||
2028 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
2029 | "VerifyingPeer %s is verifying UDPBroadcast\n", | ||
2030 | GNUNET_i2s (&my_identity)); | ||
2031 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
2032 | "Verifying UDPBroadcast from %s failed\n", | ||
2033 | GNUNET_i2s (&ub->sender)); | ||
2034 | } | ||
2035 | GNUNET_free (addr_verify); | ||
2036 | /* continue with KX, mostly for statistics... */ | ||
2037 | } | ||
2038 | |||
2039 | |||
2040 | /* finally, test if it is a KX */ | ||
2041 | if (rcvd < sizeof(struct UDPConfirmation) + sizeof(struct InitialKX)) | ||
2042 | { | ||
2043 | GNUNET_STATISTICS_update (stats, | ||
2044 | "# messages dropped (no kid, too small for KX)", | ||
2045 | 1, | ||
2046 | GNUNET_NO); | ||
2047 | continue; | ||
2048 | } | ||
2049 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2050 | "Got KX\n"); | ||
2051 | { | ||
2052 | const struct InitialKX *kx; | ||
2053 | struct SharedSecret *ss; | ||
2054 | char pbuf[rcvd - sizeof(struct InitialKX)]; | ||
2055 | const struct UDPConfirmation *uc; | ||
2056 | struct SenderAddress *sender; | ||
2057 | |||
2058 | kx = (const struct InitialKX *) buf; | ||
2059 | ss = setup_shared_secret_dec (&kx->ephemeral); | ||
2060 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2061 | "Before DEC\n"); | ||
2062 | |||
2063 | if (GNUNET_OK != try_decrypt (ss, | ||
2064 | kx->gcm_tag, | ||
2065 | 0, | ||
2066 | &buf[sizeof(*kx)], | ||
2067 | sizeof(pbuf), | ||
2068 | pbuf)) | ||
2069 | { | ||
2070 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2071 | "Unable to decrypt tag, dropping...\n"); | ||
2072 | GNUNET_free (ss); | ||
2073 | GNUNET_STATISTICS_update ( | ||
2074 | stats, | ||
2075 | "# messages dropped (no kid, AEAD decryption failed)", | ||
2076 | 1, | ||
2077 | GNUNET_NO); | ||
2078 | continue; | ||
2079 | } | ||
2080 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2081 | "Before VERIFY\n"); | ||
2082 | |||
2083 | uc = (const struct UDPConfirmation *) pbuf; | ||
2084 | if (GNUNET_OK != verify_confirmation (&kx->ephemeral, uc)) | ||
2085 | { | ||
2086 | GNUNET_break_op (0); | ||
2087 | GNUNET_free (ss); | ||
2088 | GNUNET_STATISTICS_update (stats, | ||
2089 | "# messages dropped (sender signature invalid)", | ||
2090 | 1, | ||
2091 | GNUNET_NO); | ||
2092 | continue; | ||
2093 | } | ||
2094 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2095 | "Before SETUP_SENDER\n"); | ||
2096 | |||
2097 | calculate_cmac (ss); | ||
2098 | sender = setup_sender (&uc->sender, (const struct sockaddr *) &sa, salen); | ||
2099 | ss->sender = sender; | ||
2100 | GNUNET_CONTAINER_DLL_insert (sender->ss_head, sender->ss_tail, ss); | ||
2101 | if ((KCN_THRESHOLD > ss->sender->acks_available) && | ||
2102 | (NULL == ss->sender->kce_task) && | ||
2103 | (GNUNET_NO == ss->sender->kce_task_finished)) | ||
2104 | { | ||
2105 | // TODO This task must be per sender! FIXME: This is a nice todo, but I do not know what must be done here to fix. | ||
2106 | ss->sender->kce_send_ack_on_finish = GNUNET_YES; | ||
2107 | ss->sender->kce_task = GNUNET_SCHEDULER_add_now ( | ||
2108 | kce_generate_cb, | ||
2109 | ss); | ||
2110 | } | ||
2111 | sender->num_secrets++; | ||
2112 | GNUNET_STATISTICS_update (stats, "# Secrets active", 1, GNUNET_NO); | ||
2113 | GNUNET_STATISTICS_update (stats, | ||
2114 | "# messages decrypted without BOX", | ||
2115 | 1, | ||
2116 | GNUNET_NO); | ||
2117 | try_handle_plaintext (sender, &uc[1], sizeof(pbuf) - sizeof(*uc)); | ||
2118 | if (sender->num_secrets > MAX_SECRETS) | ||
2119 | { | ||
2120 | if (0 == purge_secrets (sender->ss_tail)) | ||
2121 | { | ||
2122 | // No secret purged. Delete oldest. | ||
2123 | secret_destroy (sender->ss_tail); | ||
2124 | } | ||
2125 | } | ||
2126 | } | ||
2127 | } | ||
2128 | } | ||
2129 | |||
2130 | |||
2131 | /** | ||
2132 | * Convert UDP bind specification to a `struct sockaddr *` | ||
2133 | * | ||
2134 | * @param bindto bind specification to convert | ||
2135 | * @param[out] sock_len set to the length of the address | ||
2136 | * @return converted bindto specification | ||
2137 | */ | ||
2138 | static struct sockaddr * | ||
2139 | udp_address_to_sockaddr (const char *bindto, socklen_t *sock_len) | ||
2140 | { | ||
2141 | struct sockaddr *in; | ||
2142 | unsigned int port; | ||
2143 | char dummy[2]; | ||
2144 | char *colon; | ||
2145 | char *cp; | ||
2146 | |||
2147 | if (1 == sscanf (bindto, "%u%1s", &port, dummy)) | ||
2148 | { | ||
2149 | /* interpreting value as just a PORT number */ | ||
2150 | if (port > UINT16_MAX) | ||
2151 | { | ||
2152 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
2153 | "BINDTO specification `%s' invalid: value too large for port\n", | ||
2154 | bindto); | ||
2155 | return NULL; | ||
2156 | } | ||
2157 | if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) || | ||
2158 | (GNUNET_YES == | ||
2159 | GNUNET_CONFIGURATION_get_value_yesno (cfg, | ||
2160 | COMMUNICATOR_CONFIG_SECTION, | ||
2161 | "DISABLE_V6"))) | ||
2162 | { | ||
2163 | struct sockaddr_in *i4; | ||
2164 | |||
2165 | i4 = GNUNET_malloc (sizeof(struct sockaddr_in)); | ||
2166 | i4->sin_family = AF_INET; | ||
2167 | i4->sin_port = htons ((uint16_t) port); | ||
2168 | *sock_len = sizeof(struct sockaddr_in); | ||
2169 | in = (struct sockaddr *) i4; | ||
2170 | } | ||
2171 | else | ||
2172 | { | ||
2173 | struct sockaddr_in6 *i6; | ||
2174 | |||
2175 | i6 = GNUNET_malloc (sizeof(struct sockaddr_in6)); | ||
2176 | i6->sin6_family = AF_INET6; | ||
2177 | i6->sin6_port = htons ((uint16_t) port); | ||
2178 | *sock_len = sizeof(struct sockaddr_in6); | ||
2179 | in = (struct sockaddr *) i6; | ||
2180 | } | ||
2181 | return in; | ||
2182 | } | ||
2183 | cp = GNUNET_strdup (bindto); | ||
2184 | colon = strrchr (cp, ':'); | ||
2185 | if (NULL != colon) | ||
2186 | { | ||
2187 | /* interpret value after colon as port */ | ||
2188 | *colon = '\0'; | ||
2189 | colon++; | ||
2190 | if (1 == sscanf (colon, "%u%1s", &port, dummy)) | ||
2191 | { | ||
2192 | /* interpreting value as just a PORT number */ | ||
2193 | if (port > UINT16_MAX) | ||
2194 | { | ||
2195 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
2196 | "BINDTO specification `%s' invalid: value too large for port\n", | ||
2197 | bindto); | ||
2198 | GNUNET_free (cp); | ||
2199 | return NULL; | ||
2200 | } | ||
2201 | } | ||
2202 | else | ||
2203 | { | ||
2204 | GNUNET_log ( | ||
2205 | GNUNET_ERROR_TYPE_ERROR, | ||
2206 | "BINDTO specification `%s' invalid: last ':' not followed by number\n", | ||
2207 | bindto); | ||
2208 | GNUNET_free (cp); | ||
2209 | return NULL; | ||
2210 | } | ||
2211 | } | ||
2212 | else | ||
2213 | { | ||
2214 | /* interpret missing port as 0, aka pick any free one */ | ||
2215 | port = 0; | ||
2216 | } | ||
2217 | { | ||
2218 | /* try IPv4 */ | ||
2219 | struct sockaddr_in v4; | ||
2220 | |||
2221 | memset (&v4, 0, sizeof(v4)); | ||
2222 | if (1 == inet_pton (AF_INET, cp, &v4.sin_addr)) | ||
2223 | { | ||
2224 | v4.sin_family = AF_INET; | ||
2225 | v4.sin_port = htons ((uint16_t) port); | ||
2226 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
2227 | v4.sin_len = sizeof(struct sockaddr_in); | ||
2228 | #endif | ||
2229 | in = GNUNET_memdup (&v4, sizeof(struct sockaddr_in)); | ||
2230 | *sock_len = sizeof(struct sockaddr_in); | ||
2231 | GNUNET_free (cp); | ||
2232 | return in; | ||
2233 | } | ||
2234 | } | ||
2235 | { | ||
2236 | /* try IPv6 */ | ||
2237 | struct sockaddr_in6 v6; | ||
2238 | const char *start; | ||
2239 | |||
2240 | memset (&v6, 0, sizeof(v6)); | ||
2241 | start = cp; | ||
2242 | if (('[' == *cp) && (']' == cp[strlen (cp) - 1])) | ||
2243 | { | ||
2244 | start++; /* skip over '[' */ | ||
2245 | cp[strlen (cp) - 1] = '\0'; /* eat ']' */ | ||
2246 | } | ||
2247 | if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr)) | ||
2248 | { | ||
2249 | v6.sin6_family = AF_INET6; | ||
2250 | v6.sin6_port = htons ((uint16_t) port); | ||
2251 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
2252 | v6.sin6_len = sizeof(sizeof(struct sockaddr_in6)); | ||
2253 | #endif | ||
2254 | in = GNUNET_memdup (&v6, sizeof(v6)); | ||
2255 | *sock_len = sizeof(v6); | ||
2256 | GNUNET_free (cp); | ||
2257 | return in; | ||
2258 | } | ||
2259 | } | ||
2260 | /* #5528 FIXME (feature!): maybe also try getnameinfo()? */ | ||
2261 | GNUNET_free (cp); | ||
2262 | return NULL; | ||
2263 | } | ||
2264 | |||
2265 | |||
2266 | /** | ||
2267 | * Pad @a dgram by @a pad_size using @a out_cipher. | ||
2268 | * | ||
2269 | * @param out_cipher cipher to use | ||
2270 | * @param dgram datagram to pad | ||
2271 | * @param pad_size number of bytes of padding to append | ||
2272 | */ | ||
2273 | static void | ||
2274 | do_pad (gcry_cipher_hd_t out_cipher, char *dgram, size_t pad_size) | ||
2275 | { | ||
2276 | char pad[pad_size]; | ||
2277 | |||
2278 | GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, pad, sizeof(pad)); | ||
2279 | if (sizeof(pad) > sizeof(struct GNUNET_MessageHeader)) | ||
2280 | { | ||
2281 | struct GNUNET_MessageHeader hdr = | ||
2282 | { .size = htons (sizeof(pad)), | ||
2283 | .type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_PAD) }; | ||
2284 | |||
2285 | memcpy (pad, &hdr, sizeof(hdr)); | ||
2286 | } | ||
2287 | GNUNET_assert ( | ||
2288 | 0 == | ||
2289 | gcry_cipher_encrypt (out_cipher, dgram, sizeof(pad), pad, sizeof(pad))); | ||
2290 | } | ||
2291 | |||
2292 | |||
2293 | static void | ||
2294 | send_msg_with_kx (const struct GNUNET_MessageHeader *msg, struct | ||
2295 | ReceiverAddress *receiver) | ||
2296 | { | ||
2297 | uint16_t msize = ntohs (msg->size); | ||
2298 | struct UdpHandshakeSignature uhs; | ||
2299 | struct UDPConfirmation uc; | ||
2300 | struct InitialKX kx; | ||
2301 | char dgram[receiver->kx_mtu + sizeof(uc) + sizeof(kx)]; | ||
2302 | size_t dpos; | ||
2303 | gcry_cipher_hd_t out_cipher; | ||
2304 | struct SharedSecret *ss; | ||
2305 | |||
2306 | if (msize > receiver->kx_mtu) | ||
2307 | { | ||
2308 | GNUNET_break (0); | ||
2309 | if (GNUNET_YES != receiver->receiver_destroy_called) | ||
2310 | receiver_destroy (receiver); | ||
2311 | return; | ||
2312 | } | ||
2313 | reschedule_receiver_timeout (receiver); | ||
2314 | |||
2315 | /* setup key material */ | ||
2316 | |||
2317 | ss = setup_shared_secret_ephemeral (&uhs.ephemeral, receiver); | ||
2318 | |||
2319 | if (receiver->num_secrets > MAX_SECRETS) | ||
2320 | { | ||
2321 | if (0 == purge_secrets (receiver->ss_tail)) | ||
2322 | { | ||
2323 | // No secret purged. Delete oldest. | ||
2324 | secret_destroy (receiver->ss_tail); | ||
2325 | } | ||
2326 | } | ||
2327 | |||
2328 | setup_cipher (&ss->master, 0, &out_cipher); | ||
2329 | /* compute 'uc' */ | ||
2330 | uc.sender = my_identity; | ||
2331 | uc.monotonic_time = | ||
2332 | GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_monotonic (cfg)); | ||
2333 | uhs.purpose.purpose = htonl ( | ||
2334 | GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_UDP_HANDSHAKE); | ||
2335 | uhs.purpose.size = htonl (sizeof(uhs)); | ||
2336 | uhs.sender = my_identity; | ||
2337 | uhs.receiver = receiver->target; | ||
2338 | uhs.monotonic_time = uc.monotonic_time; | ||
2339 | GNUNET_CRYPTO_eddsa_sign (my_private_key, | ||
2340 | &uhs, | ||
2341 | &uc.sender_sig); | ||
2342 | /* Leave space for kx */ | ||
2343 | dpos = sizeof(kx); | ||
2344 | /* Append encrypted uc to dgram */ | ||
2345 | GNUNET_assert (0 == gcry_cipher_encrypt (out_cipher, | ||
2346 | &dgram[dpos], | ||
2347 | sizeof(uc), | ||
2348 | &uc, | ||
2349 | sizeof(uc))); | ||
2350 | dpos += sizeof(uc); | ||
2351 | /* Append encrypted payload to dgram */ | ||
2352 | GNUNET_assert ( | ||
2353 | 0 == gcry_cipher_encrypt (out_cipher, &dgram[dpos], msize, msg, msize)); | ||
2354 | dpos += msize; | ||
2355 | do_pad (out_cipher, &dgram[dpos], sizeof(dgram) - dpos); | ||
2356 | /* Datagram starts with kx */ | ||
2357 | kx.ephemeral = uhs.ephemeral; | ||
2358 | GNUNET_assert ( | ||
2359 | 0 == gcry_cipher_gettag (out_cipher, kx.gcm_tag, sizeof(kx.gcm_tag))); | ||
2360 | gcry_cipher_close (out_cipher); | ||
2361 | memcpy (dgram, &kx, sizeof(kx)); | ||
2362 | if (-1 == GNUNET_NETWORK_socket_sendto (udp_sock, | ||
2363 | dgram, | ||
2364 | sizeof(dgram), | ||
2365 | receiver->address, | ||
2366 | receiver->address_len)) | ||
2367 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "send"); | ||
2368 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2369 | "Sending KX with payload size %u to %s\n", | ||
2370 | msize, | ||
2371 | GNUNET_a2s (receiver->address, | ||
2372 | receiver->address_len)); | ||
2373 | } | ||
2374 | |||
2375 | |||
2376 | /** | ||
2377 | * Signature of functions implementing the sending functionality of a | ||
2378 | * message queue. | ||
2379 | * | ||
2380 | * @param mq the message queue | ||
2381 | * @param msg the message to send | ||
2382 | * @param impl_state our `struct ReceiverAddress` | ||
2383 | */ | ||
2384 | static void | ||
2385 | mq_send_kx (struct GNUNET_MQ_Handle *mq, | ||
2386 | const struct GNUNET_MessageHeader *msg, | ||
2387 | void *impl_state) | ||
2388 | { | ||
2389 | struct ReceiverAddress *receiver = impl_state; | ||
2390 | |||
2391 | GNUNET_assert (mq == receiver->kx_mq); | ||
2392 | send_msg_with_kx (msg, receiver); | ||
2393 | GNUNET_MQ_impl_send_continue (mq); | ||
2394 | } | ||
2395 | |||
2396 | |||
2397 | static void | ||
2398 | create_rekey (struct ReceiverAddress *receiver, struct SharedSecret *ss, struct | ||
2399 | UDPRekey *rekey) | ||
2400 | { | ||
2401 | struct SharedSecret *ss_rekey; | ||
2402 | |||
2403 | ss->rekey_initiated = GNUNET_YES; | ||
2404 | /* setup key material */ | ||
2405 | ss_rekey = setup_shared_secret_ephemeral (&rekey->ephemeral, | ||
2406 | receiver); | ||
2407 | ss_rekey->sequence_allowed = 0; | ||
2408 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2409 | "Setup secret with k = %s\n", | ||
2410 | GNUNET_h2s (&(ss_rekey->master))); | ||
2411 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2412 | "Setup secret with H(k) = %s\n", | ||
2413 | GNUNET_h2s (&(ss_rekey->cmac))); | ||
2414 | |||
2415 | /* Append encrypted payload to dgram */ | ||
2416 | rekey->header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_REKEY); | ||
2417 | rekey->header.size = htons (sizeof (struct UDPRekey)); | ||
2418 | } | ||
2419 | |||
2420 | |||
2421 | /** | ||
2422 | * Signature of functions implementing the sending functionality of a | ||
2423 | * message queue. | ||
2424 | * | ||
2425 | * @param mq the message queue | ||
2426 | * @param msg the message to send | ||
2427 | * @param impl_state our `struct ReceiverAddress` | ||
2428 | */ | ||
2429 | static void | ||
2430 | mq_send_d (struct GNUNET_MQ_Handle *mq, | ||
2431 | const struct GNUNET_MessageHeader *msg, | ||
2432 | void *impl_state) | ||
2433 | { | ||
2434 | struct ReceiverAddress *receiver = impl_state; | ||
2435 | struct UDPRekey rekey; | ||
2436 | struct SharedSecret *ss; | ||
2437 | int inject_rekey = GNUNET_NO; | ||
2438 | uint16_t msize = ntohs (msg->size); | ||
2439 | |||
2440 | GNUNET_assert (mq == receiver->d_mq); | ||
2441 | if ((msize > receiver->d_mtu) || | ||
2442 | (0 == receiver->acks_available)) | ||
2443 | { | ||
2444 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2445 | "msize: %u, mtu: %lu, acks: %u\n", | ||
2446 | msize, | ||
2447 | receiver->d_mtu, | ||
2448 | receiver->acks_available); | ||
2449 | |||
2450 | GNUNET_break (0); | ||
2451 | if (GNUNET_YES != receiver->receiver_destroy_called) | ||
2452 | receiver_destroy (receiver); | ||
2453 | return; | ||
2454 | } | ||
2455 | reschedule_receiver_timeout (receiver); | ||
2456 | |||
2457 | if (receiver->num_secrets > MAX_SECRETS) | ||
2458 | { | ||
2459 | if (0 == purge_secrets (receiver->ss_tail)) | ||
2460 | { | ||
2461 | // No secret purged. Delete oldest. | ||
2462 | secret_destroy (receiver->ss_tail); | ||
2463 | } | ||
2464 | } | ||
2465 | /* begin "BOX" encryption method, scan for ACKs from tail! */ | ||
2466 | for (ss = receiver->ss_tail; NULL != ss; ss = ss->prev) | ||
2467 | { | ||
2468 | size_t payload_len = sizeof(struct UDPBox) + receiver->d_mtu; | ||
2469 | if (ss->sequence_used >= ss->sequence_allowed) | ||
2470 | { | ||
2471 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2472 | "Skipping ss because no acks to use.\n"); | ||
2473 | continue; | ||
2474 | } | ||
2475 | if (ss->bytes_sent >= rekey_max_bytes) | ||
2476 | { | ||
2477 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2478 | "Skipping ss because rekey bytes reached.\n"); | ||
2479 | // FIXME cleanup ss with too many bytes sent! | ||
2480 | continue; | ||
2481 | } | ||
2482 | if (ss->bytes_sent > rekey_max_bytes * 0.7) | ||
2483 | { | ||
2484 | if (ss->rekey_initiated == GNUNET_NO) | ||
2485 | { | ||
2486 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
2487 | "Injecting rekey for ss with byte sent %lu\n", | ||
2488 | (unsigned long) ss->bytes_sent); | ||
2489 | create_rekey (receiver, ss, &rekey); | ||
2490 | inject_rekey = GNUNET_YES; | ||
2491 | payload_len += sizeof (rekey); | ||
2492 | ss->rekey_initiated = GNUNET_YES; | ||
2493 | } | ||
2494 | } | ||
2495 | if (0 < ss->sequence_used) | ||
2496 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2497 | "Trying to send UDPBox with shared secrect %s sequence_used %u and ss->sequence_allowed %u\n", | ||
2498 | GNUNET_h2s (&ss->master), | ||
2499 | ss->sequence_used, | ||
2500 | ss->sequence_allowed); | ||
2501 | |||
2502 | char dgram[payload_len]; | ||
2503 | struct UDPBox *box; | ||
2504 | gcry_cipher_hd_t out_cipher; | ||
2505 | size_t dpos; | ||
2506 | |||
2507 | box = (struct UDPBox *) dgram; | ||
2508 | ss->sequence_used++; | ||
2509 | get_kid (&ss->master, ss->sequence_used, &box->kid); | ||
2510 | setup_cipher (&ss->master, ss->sequence_used, &out_cipher); | ||
2511 | /* Append encrypted payload to dgram */ | ||
2512 | dpos = sizeof(struct UDPBox); | ||
2513 | if (GNUNET_YES == inject_rekey) | ||
2514 | { | ||
2515 | GNUNET_assert ( | ||
2516 | 0 == gcry_cipher_encrypt (out_cipher, &dgram[dpos], sizeof (rekey), | ||
2517 | &rekey, sizeof (rekey))); | ||
2518 | dpos += sizeof (rekey); | ||
2519 | } | ||
2520 | GNUNET_assert ( | ||
2521 | 0 == gcry_cipher_encrypt (out_cipher, &dgram[dpos], msize, msg, msize)); | ||
2522 | dpos += msize; | ||
2523 | do_pad (out_cipher, &dgram[dpos], sizeof(dgram) - dpos); | ||
2524 | GNUNET_assert (0 == gcry_cipher_gettag (out_cipher, | ||
2525 | box->gcm_tag, | ||
2526 | sizeof(box->gcm_tag))); | ||
2527 | gcry_cipher_close (out_cipher); | ||
2528 | |||
2529 | if (-1 == GNUNET_NETWORK_socket_sendto (udp_sock, | ||
2530 | dgram, | ||
2531 | payload_len, // FIXME why always send sizeof dgram? | ||
2532 | receiver->address, | ||
2533 | receiver->address_len)) | ||
2534 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "send"); | ||
2535 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2536 | "Sending UDPBox with payload size %u, %u acks left, %lu bytes sent\n", | ||
2537 | msize, | ||
2538 | receiver->acks_available, | ||
2539 | (unsigned long) ss->bytes_sent); | ||
2540 | ss->bytes_sent += sizeof (dgram); | ||
2541 | receiver->acks_available--; | ||
2542 | GNUNET_MQ_impl_send_continue (mq); | ||
2543 | return; | ||
2544 | } | ||
2545 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2546 | "No suitable ss found, sending as KX...\n"); | ||
2547 | send_msg_with_kx (msg, receiver); | ||
2548 | GNUNET_MQ_impl_send_continue (mq); | ||
2549 | } | ||
2550 | |||
2551 | |||
2552 | /** | ||
2553 | * Signature of functions implementing the destruction of a message | ||
2554 | * queue. Implementations must not free @a mq, but should take care | ||
2555 | * of @a impl_state. | ||
2556 | * | ||
2557 | * @param mq the message queue to destroy | ||
2558 | * @param impl_state our `struct ReceiverAddress` | ||
2559 | */ | ||
2560 | static void | ||
2561 | mq_destroy_d (struct GNUNET_MQ_Handle *mq, void *impl_state) | ||
2562 | { | ||
2563 | struct ReceiverAddress *receiver = impl_state; | ||
2564 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2565 | "Default MQ destroyed\n"); | ||
2566 | if (mq == receiver->d_mq) | ||
2567 | { | ||
2568 | receiver->d_mq = NULL; | ||
2569 | if (GNUNET_YES != receiver->receiver_destroy_called) | ||
2570 | receiver_destroy (receiver); | ||
2571 | } | ||
2572 | } | ||
2573 | |||
2574 | |||
2575 | /** | ||
2576 | * Signature of functions implementing the destruction of a message | ||
2577 | * queue. Implementations must not free @a mq, but should take care | ||
2578 | * of @a impl_state. | ||
2579 | * | ||
2580 | * @param mq the message queue to destroy | ||
2581 | * @param impl_state our `struct ReceiverAddress` | ||
2582 | */ | ||
2583 | static void | ||
2584 | mq_destroy_kx (struct GNUNET_MQ_Handle *mq, void *impl_state) | ||
2585 | { | ||
2586 | struct ReceiverAddress *receiver = impl_state; | ||
2587 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2588 | "KX MQ destroyed\n"); | ||
2589 | if (mq == receiver->kx_mq) | ||
2590 | { | ||
2591 | receiver->kx_mq = NULL; | ||
2592 | if (GNUNET_YES != receiver->receiver_destroy_called) | ||
2593 | receiver_destroy (receiver); | ||
2594 | } | ||
2595 | } | ||
2596 | |||
2597 | |||
2598 | /** | ||
2599 | * Implementation function that cancels the currently sent message. | ||
2600 | * | ||
2601 | * @param mq message queue | ||
2602 | * @param impl_state our `struct RecvierAddress` | ||
2603 | */ | ||
2604 | static void | ||
2605 | mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state) | ||
2606 | { | ||
2607 | /* Cancellation is impossible with UDP; bail */ | ||
2608 | GNUNET_assert (0); | ||
2609 | } | ||
2610 | |||
2611 | |||
2612 | /** | ||
2613 | * Generic error handler, called with the appropriate | ||
2614 | * error code and the same closure specified at the creation of | ||
2615 | * the message queue. | ||
2616 | * Not every message queue implementation supports an error handler. | ||
2617 | * | ||
2618 | * @param cls our `struct ReceiverAddress` | ||
2619 | * @param error error code | ||
2620 | */ | ||
2621 | static void | ||
2622 | mq_error (void *cls, enum GNUNET_MQ_Error error) | ||
2623 | { | ||
2624 | struct ReceiverAddress *receiver = cls; | ||
2625 | |||
2626 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
2627 | "MQ error in queue to %s: %d\n", | ||
2628 | GNUNET_i2s (&receiver->target), | ||
2629 | (int) error); | ||
2630 | receiver_destroy (receiver); | ||
2631 | } | ||
2632 | |||
2633 | |||
2634 | /** | ||
2635 | * Setup the MQ for the @a receiver. If a queue exists, | ||
2636 | * the existing one is destroyed. Then the MTU is | ||
2637 | * recalculated and a fresh queue is initialized. | ||
2638 | * | ||
2639 | * @param receiver receiver to setup MQ for | ||
2640 | */ | ||
2641 | static void | ||
2642 | setup_receiver_mq (struct ReceiverAddress *receiver) | ||
2643 | { | ||
2644 | size_t base_mtu; | ||
2645 | |||
2646 | switch (receiver->address->sa_family) | ||
2647 | { | ||
2648 | case AF_INET: | ||
2649 | base_mtu = 1480 /* Ethernet MTU, 1500 - Ethernet header - VLAN tag */ | ||
2650 | - sizeof(struct GNUNET_TUN_IPv4Header) /* 20 */ | ||
2651 | - sizeof(struct GNUNET_TUN_UdpHeader) /* 8 */; | ||
2652 | break; | ||
2653 | |||
2654 | case AF_INET6: | ||
2655 | base_mtu = 1280 /* Minimum MTU required by IPv6 */ | ||
2656 | - sizeof(struct GNUNET_TUN_IPv6Header) /* 40 */ | ||
2657 | - sizeof(struct GNUNET_TUN_UdpHeader) /* 8 */; | ||
2658 | break; | ||
2659 | |||
2660 | default: | ||
2661 | GNUNET_assert (0); | ||
2662 | break; | ||
2663 | } | ||
2664 | /* MTU based on full KX messages */ | ||
2665 | receiver->kx_mtu = base_mtu - sizeof(struct InitialKX) /* 48 */ | ||
2666 | - sizeof(struct UDPConfirmation); /* 104 */ | ||
2667 | /* MTU based on BOXed messages */ | ||
2668 | receiver->d_mtu = base_mtu - sizeof(struct UDPBox); | ||
2669 | |||
2670 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2671 | "Setting up MQs and QHs\n"); | ||
2672 | /* => Effective MTU for CORE will range from 1080 (IPv6 + KX) to | ||
2673 | 1404 (IPv4 + Box) bytes, depending on circumstances... */ | ||
2674 | if (NULL == receiver->kx_mq) | ||
2675 | receiver->kx_mq = GNUNET_MQ_queue_for_callbacks (&mq_send_kx, | ||
2676 | &mq_destroy_kx, | ||
2677 | &mq_cancel, | ||
2678 | receiver, | ||
2679 | NULL, | ||
2680 | &mq_error, | ||
2681 | receiver); | ||
2682 | if (NULL == receiver->d_mq) | ||
2683 | receiver->d_mq = GNUNET_MQ_queue_for_callbacks (&mq_send_d, | ||
2684 | &mq_destroy_d, | ||
2685 | &mq_cancel, | ||
2686 | receiver, | ||
2687 | NULL, | ||
2688 | &mq_error, | ||
2689 | receiver); | ||
2690 | |||
2691 | receiver->kx_qh = | ||
2692 | GNUNET_TRANSPORT_communicator_mq_add (ch, | ||
2693 | &receiver->target, | ||
2694 | receiver->foreign_addr, | ||
2695 | receiver->kx_mtu, | ||
2696 | GNUNET_TRANSPORT_QUEUE_LENGTH_UNLIMITED, | ||
2697 | 0, /* Priority */ | ||
2698 | receiver->nt, | ||
2699 | GNUNET_TRANSPORT_CS_OUTBOUND, | ||
2700 | receiver->kx_mq); | ||
2701 | } | ||
2702 | |||
2703 | |||
2704 | /** | ||
2705 | * Function called by the transport service to initialize a | ||
2706 | * message queue given address information about another peer. | ||
2707 | * If and when the communication channel is established, the | ||
2708 | * communicator must call #GNUNET_TRANSPORT_communicator_mq_add() | ||
2709 | * to notify the service that the channel is now up. It is | ||
2710 | * the responsibility of the communicator to manage sane | ||
2711 | * retries and timeouts for any @a peer/@a address combination | ||
2712 | * provided by the transport service. Timeouts and retries | ||
2713 | * do not need to be signalled to the transport service. | ||
2714 | * | ||
2715 | * @param cls closure | ||
2716 | * @param peer identity of the other peer | ||
2717 | * @param address where to send the message, human-readable | ||
2718 | * communicator-specific format, 0-terminated, UTF-8 | ||
2719 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if the provided address is | ||
2720 | * invalid | ||
2721 | */ | ||
2722 | static int | ||
2723 | mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address) | ||
2724 | { | ||
2725 | struct ReceiverAddress *receiver; | ||
2726 | const char *path; | ||
2727 | struct sockaddr *in; | ||
2728 | socklen_t in_len; | ||
2729 | |||
2730 | if (0 != strncmp (address, | ||
2731 | COMMUNICATOR_ADDRESS_PREFIX "-", | ||
2732 | strlen (COMMUNICATOR_ADDRESS_PREFIX "-"))) | ||
2733 | { | ||
2734 | GNUNET_break_op (0); | ||
2735 | return GNUNET_SYSERR; | ||
2736 | } | ||
2737 | path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")]; | ||
2738 | in = udp_address_to_sockaddr (path, &in_len); | ||
2739 | |||
2740 | receiver = GNUNET_new (struct ReceiverAddress); | ||
2741 | receiver->address = in; | ||
2742 | receiver->address_len = in_len; | ||
2743 | receiver->target = *peer; | ||
2744 | receiver->nt = GNUNET_NT_scanner_get_type (is, in, in_len); | ||
2745 | (void) GNUNET_CONTAINER_multipeermap_put ( | ||
2746 | receivers, | ||
2747 | &receiver->target, | ||
2748 | receiver, | ||
2749 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); | ||
2750 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2751 | "Added %s to receivers\n", | ||
2752 | GNUNET_i2s_full (&receiver->target)); | ||
2753 | receiver->timeout = | ||
2754 | GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); | ||
2755 | receiver->hn = GNUNET_CONTAINER_heap_insert (receivers_heap, | ||
2756 | receiver, | ||
2757 | receiver->timeout.abs_value_us); | ||
2758 | GNUNET_STATISTICS_set (stats, | ||
2759 | "# receivers active", | ||
2760 | GNUNET_CONTAINER_multipeermap_size (receivers), | ||
2761 | GNUNET_NO); | ||
2762 | receiver->foreign_addr = | ||
2763 | sockaddr_to_udpaddr_string (receiver->address, receiver->address_len); | ||
2764 | setup_receiver_mq (receiver); | ||
2765 | if (NULL == timeout_task) | ||
2766 | timeout_task = GNUNET_SCHEDULER_add_now (&check_timeouts, NULL); | ||
2767 | return GNUNET_OK; | ||
2768 | } | ||
2769 | |||
2770 | |||
2771 | /** | ||
2772 | * Iterator over all receivers to clean up. | ||
2773 | * | ||
2774 | * @param cls NULL | ||
2775 | * @param target unused | ||
2776 | * @param value the queue to destroy | ||
2777 | * @return #GNUNET_OK to continue to iterate | ||
2778 | */ | ||
2779 | static int | ||
2780 | get_receiver_delete_it (void *cls, | ||
2781 | const struct GNUNET_PeerIdentity *target, | ||
2782 | void *value) | ||
2783 | { | ||
2784 | struct ReceiverAddress *receiver = value; | ||
2785 | |||
2786 | (void) cls; | ||
2787 | (void) target; | ||
2788 | receiver_destroy (receiver); | ||
2789 | return GNUNET_OK; | ||
2790 | } | ||
2791 | |||
2792 | |||
2793 | /** | ||
2794 | * Iterator over all senders to clean up. | ||
2795 | * | ||
2796 | * @param cls NULL | ||
2797 | * @param target unused | ||
2798 | * @param value the queue to destroy | ||
2799 | * @return #GNUNET_OK to continue to iterate | ||
2800 | */ | ||
2801 | static int | ||
2802 | get_sender_delete_it (void *cls, | ||
2803 | const struct GNUNET_PeerIdentity *target, | ||
2804 | void *value) | ||
2805 | { | ||
2806 | struct SenderAddress *sender = value; | ||
2807 | |||
2808 | (void) cls; | ||
2809 | (void) target; | ||
2810 | |||
2811 | |||
2812 | sender_destroy (sender); | ||
2813 | return GNUNET_OK; | ||
2814 | } | ||
2815 | |||
2816 | |||
2817 | /** | ||
2818 | * Shutdown the UNIX communicator. | ||
2819 | * | ||
2820 | * @param cls NULL (always) | ||
2821 | */ | ||
2822 | static void | ||
2823 | do_shutdown (void *cls) | ||
2824 | { | ||
2825 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2826 | "do_shutdown\n"); | ||
2827 | if (NULL != nat) | ||
2828 | { | ||
2829 | GNUNET_NAT_unregister (nat); | ||
2830 | nat = NULL; | ||
2831 | } | ||
2832 | while (NULL != bi_head) | ||
2833 | bi_destroy (bi_head); | ||
2834 | if (NULL != broadcast_task) | ||
2835 | { | ||
2836 | GNUNET_SCHEDULER_cancel (broadcast_task); | ||
2837 | broadcast_task = NULL; | ||
2838 | } | ||
2839 | if (NULL != timeout_task) | ||
2840 | { | ||
2841 | GNUNET_SCHEDULER_cancel (timeout_task); | ||
2842 | timeout_task = NULL; | ||
2843 | } | ||
2844 | if (NULL != read_task) | ||
2845 | { | ||
2846 | GNUNET_SCHEDULER_cancel (read_task); | ||
2847 | read_task = NULL; | ||
2848 | } | ||
2849 | if (NULL != udp_sock) | ||
2850 | { | ||
2851 | GNUNET_break (GNUNET_OK == | ||
2852 | GNUNET_NETWORK_socket_close (udp_sock)); | ||
2853 | udp_sock = NULL; | ||
2854 | } | ||
2855 | GNUNET_CONTAINER_multipeermap_iterate (receivers, | ||
2856 | &get_receiver_delete_it, | ||
2857 | NULL); | ||
2858 | GNUNET_CONTAINER_multipeermap_destroy (receivers); | ||
2859 | GNUNET_CONTAINER_multipeermap_iterate (senders, | ||
2860 | &get_sender_delete_it, | ||
2861 | NULL); | ||
2862 | GNUNET_CONTAINER_multipeermap_destroy (senders); | ||
2863 | GNUNET_CONTAINER_multishortmap_destroy (key_cache); | ||
2864 | GNUNET_CONTAINER_heap_destroy (senders_heap); | ||
2865 | GNUNET_CONTAINER_heap_destroy (receivers_heap); | ||
2866 | if (NULL != timeout_task) | ||
2867 | { | ||
2868 | GNUNET_SCHEDULER_cancel (timeout_task); | ||
2869 | timeout_task = NULL; | ||
2870 | } | ||
2871 | if (NULL != ch) | ||
2872 | { | ||
2873 | GNUNET_TRANSPORT_communicator_disconnect (ch); | ||
2874 | ch = NULL; | ||
2875 | } | ||
2876 | if (NULL != ah) | ||
2877 | { | ||
2878 | GNUNET_TRANSPORT_application_done (ah); | ||
2879 | ah = NULL; | ||
2880 | } | ||
2881 | if (NULL != stats) | ||
2882 | { | ||
2883 | GNUNET_STATISTICS_destroy (stats, GNUNET_NO); | ||
2884 | stats = NULL; | ||
2885 | } | ||
2886 | if (NULL != my_private_key) | ||
2887 | { | ||
2888 | GNUNET_free (my_private_key); | ||
2889 | my_private_key = NULL; | ||
2890 | } | ||
2891 | if (NULL != is) | ||
2892 | { | ||
2893 | GNUNET_NT_scanner_done (is); | ||
2894 | is = NULL; | ||
2895 | } | ||
2896 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2897 | "do_shutdown finished\n"); | ||
2898 | } | ||
2899 | |||
2900 | |||
2901 | /** | ||
2902 | * Function called when the transport service has received a | ||
2903 | * backchannel message for this communicator (!) via a different return | ||
2904 | * path. Should be an acknowledgement. | ||
2905 | * | ||
2906 | * @param cls closure, NULL | ||
2907 | * @param sender which peer sent the notification | ||
2908 | * @param msg payload | ||
2909 | */ | ||
2910 | static void | ||
2911 | enc_notify_cb (void *cls, | ||
2912 | const struct GNUNET_PeerIdentity *sender, | ||
2913 | const struct GNUNET_MessageHeader *msg) | ||
2914 | { | ||
2915 | const struct UDPAck *ack; | ||
2916 | |||
2917 | (void) cls; | ||
2918 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
2919 | "Storing UDPAck received from backchannel from %s\n", | ||
2920 | GNUNET_i2s_full (sender)); | ||
2921 | if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK) || | ||
2922 | (ntohs (msg->size) != sizeof(struct UDPAck))) | ||
2923 | { | ||
2924 | GNUNET_break_op (0); | ||
2925 | return; | ||
2926 | } | ||
2927 | ack = (const struct UDPAck *) msg; | ||
2928 | GNUNET_CONTAINER_multipeermap_get_multiple (receivers, | ||
2929 | sender, | ||
2930 | &handle_ack, | ||
2931 | (void *) ack); | ||
2932 | } | ||
2933 | |||
2934 | |||
2935 | /** | ||
2936 | * Signature of the callback passed to #GNUNET_NAT_register() for | ||
2937 | * a function to call whenever our set of 'valid' addresses changes. | ||
2938 | * | ||
2939 | * @param cls closure | ||
2940 | * @param app_ctx[in,out] location where the app can store stuff | ||
2941 | * on add and retrieve it on remove | ||
2942 | * @param add_remove #GNUNET_YES to add a new public IP address, | ||
2943 | * #GNUNET_NO to remove a previous (now invalid) one | ||
2944 | * @param ac address class the address belongs to | ||
2945 | * @param addr either the previous or the new public IP address | ||
2946 | * @param addrlen actual length of the @a addr | ||
2947 | */ | ||
2948 | static void | ||
2949 | nat_address_cb (void *cls, | ||
2950 | void **app_ctx, | ||
2951 | int add_remove, | ||
2952 | enum GNUNET_NAT_AddressClass ac, | ||
2953 | const struct sockaddr *addr, | ||
2954 | socklen_t addrlen) | ||
2955 | { | ||
2956 | char *my_addr; | ||
2957 | struct GNUNET_TRANSPORT_AddressIdentifier *ai; | ||
2958 | |||
2959 | if (GNUNET_YES == add_remove) | ||
2960 | { | ||
2961 | enum GNUNET_NetworkType nt; | ||
2962 | |||
2963 | GNUNET_asprintf (&my_addr, | ||
2964 | "%s-%s", | ||
2965 | COMMUNICATOR_ADDRESS_PREFIX, | ||
2966 | GNUNET_a2s (addr, addrlen)); | ||
2967 | nt = GNUNET_NT_scanner_get_type (is, addr, addrlen); | ||
2968 | ai = | ||
2969 | GNUNET_TRANSPORT_communicator_address_add (ch, | ||
2970 | my_addr, | ||
2971 | nt, | ||
2972 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
2973 | GNUNET_free (my_addr); | ||
2974 | *app_ctx = ai; | ||
2975 | } | ||
2976 | else | ||
2977 | { | ||
2978 | ai = *app_ctx; | ||
2979 | GNUNET_TRANSPORT_communicator_address_remove (ai); | ||
2980 | *app_ctx = NULL; | ||
2981 | } | ||
2982 | } | ||
2983 | |||
2984 | |||
2985 | /** | ||
2986 | * Broadcast our presence on one of our interfaces. | ||
2987 | * | ||
2988 | * @param cls a `struct BroadcastInterface` | ||
2989 | */ | ||
2990 | static void | ||
2991 | ifc_broadcast (void *cls) | ||
2992 | { | ||
2993 | struct BroadcastInterface *bi = cls; | ||
2994 | struct GNUNET_TIME_Relative delay; | ||
2995 | |||
2996 | delay = BROADCAST_FREQUENCY; | ||
2997 | delay.rel_value_us = | ||
2998 | GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, delay.rel_value_us); | ||
2999 | bi->broadcast_task = | ||
3000 | GNUNET_SCHEDULER_add_delayed (delay, &ifc_broadcast, bi); | ||
3001 | |||
3002 | switch (bi->sa->sa_family) | ||
3003 | { | ||
3004 | case AF_INET: { | ||
3005 | static int yes = 1; | ||
3006 | static int no = 0; | ||
3007 | ssize_t sent; | ||
3008 | |||
3009 | if (GNUNET_OK != | ||
3010 | GNUNET_NETWORK_socket_setsockopt (udp_sock, | ||
3011 | SOL_SOCKET, | ||
3012 | SO_BROADCAST, | ||
3013 | &yes, | ||
3014 | sizeof(int))) | ||
3015 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, | ||
3016 | "setsockopt"); | ||
3017 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3018 | "creating UDPBroadcast from %s\n", | ||
3019 | GNUNET_i2s (&(bi->bcm.sender))); | ||
3020 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3021 | "sending UDPBroadcast to add %s\n", | ||
3022 | GNUNET_a2s (bi->ba, bi->salen)); | ||
3023 | sent = GNUNET_NETWORK_socket_sendto (udp_sock, | ||
3024 | &bi->bcm, | ||
3025 | sizeof(bi->bcm), | ||
3026 | bi->ba, | ||
3027 | bi->salen); | ||
3028 | if (-1 == sent) | ||
3029 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, | ||
3030 | "sendto"); | ||
3031 | if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt (udp_sock, | ||
3032 | SOL_SOCKET, | ||
3033 | SO_BROADCAST, | ||
3034 | &no, | ||
3035 | sizeof(int))) | ||
3036 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, | ||
3037 | "setsockopt"); | ||
3038 | break; | ||
3039 | } | ||
3040 | |||
3041 | case AF_INET6: { | ||
3042 | ssize_t sent; | ||
3043 | struct sockaddr_in6 dst; | ||
3044 | |||
3045 | dst.sin6_family = AF_INET6; | ||
3046 | dst.sin6_port = htons (my_port); | ||
3047 | dst.sin6_addr = bi->mcreq.ipv6mr_multiaddr; | ||
3048 | dst.sin6_scope_id = ((struct sockaddr_in6 *) bi->ba)->sin6_scope_id; | ||
3049 | |||
3050 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3051 | "sending UDPBroadcast\n"); | ||
3052 | sent = GNUNET_NETWORK_socket_sendto (udp_sock, | ||
3053 | &bi->bcm, | ||
3054 | sizeof(bi->bcm), | ||
3055 | (const struct sockaddr *) &dst, | ||
3056 | sizeof(dst)); | ||
3057 | if (-1 == sent) | ||
3058 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto"); | ||
3059 | break; | ||
3060 | } | ||
3061 | |||
3062 | default: | ||
3063 | GNUNET_break (0); | ||
3064 | break; | ||
3065 | } | ||
3066 | } | ||
3067 | |||
3068 | |||
3069 | /** | ||
3070 | * Callback function invoked for each interface found. | ||
3071 | * Activates/deactivates broadcast interfaces. | ||
3072 | * | ||
3073 | * @param cls NULL | ||
3074 | * @param name name of the interface (can be NULL for unknown) | ||
3075 | * @param isDefault is this presumably the default interface | ||
3076 | * @param addr address of this interface (can be NULL for unknown or unassigned) | ||
3077 | * @param broadcast_addr the broadcast address (can be NULL for unknown or | ||
3078 | * unassigned) | ||
3079 | * @param netmask the network mask (can be NULL for unknown or unassigned) | ||
3080 | * @param addrlen length of the address | ||
3081 | * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort | ||
3082 | */ | ||
3083 | static int | ||
3084 | iface_proc (void *cls, | ||
3085 | const char *name, | ||
3086 | int isDefault, | ||
3087 | const struct sockaddr *addr, | ||
3088 | const struct sockaddr *broadcast_addr, | ||
3089 | const struct sockaddr *netmask, | ||
3090 | socklen_t addrlen) | ||
3091 | { | ||
3092 | struct BroadcastInterface *bi; | ||
3093 | enum GNUNET_NetworkType network; | ||
3094 | struct UdpBroadcastSignature ubs; | ||
3095 | |||
3096 | (void) cls; | ||
3097 | (void) netmask; | ||
3098 | if (NULL == addr) | ||
3099 | return GNUNET_YES; /* need to know our address! */ | ||
3100 | network = GNUNET_NT_scanner_get_type (is, addr, addrlen); | ||
3101 | if (GNUNET_NT_LOOPBACK == network) | ||
3102 | { | ||
3103 | /* Broadcasting on loopback does not make sense */ | ||
3104 | return GNUNET_YES; | ||
3105 | } | ||
3106 | for (bi = bi_head; NULL != bi; bi = bi->next) | ||
3107 | { | ||
3108 | if ((bi->salen == addrlen) && (0 == memcmp (addr, bi->sa, addrlen))) | ||
3109 | { | ||
3110 | bi->found = GNUNET_YES; | ||
3111 | return GNUNET_OK; | ||
3112 | } | ||
3113 | } | ||
3114 | |||
3115 | if ((AF_INET6 == addr->sa_family) && (NULL == broadcast_addr)) | ||
3116 | return GNUNET_OK; /* broadcast_addr is required for IPv6! */ | ||
3117 | if ((AF_INET6 == addr->sa_family) && (GNUNET_YES != have_v6_socket)) | ||
3118 | return GNUNET_OK; /* not using IPv6 */ | ||
3119 | |||
3120 | bi = GNUNET_new (struct BroadcastInterface); | ||
3121 | bi->sa = GNUNET_memdup (addr, | ||
3122 | addrlen); | ||
3123 | if ( (NULL != broadcast_addr) && | ||
3124 | (addrlen == sizeof (struct sockaddr_in)) ) | ||
3125 | { | ||
3126 | struct sockaddr_in *ba; | ||
3127 | |||
3128 | ba = GNUNET_memdup (broadcast_addr, | ||
3129 | addrlen); | ||
3130 | ba->sin_port = htons (2086); /* always GNUnet port, ignore configuration! */ | ||
3131 | bi->ba = (struct sockaddr *) ba; | ||
3132 | } | ||
3133 | bi->salen = addrlen; | ||
3134 | bi->found = GNUNET_YES; | ||
3135 | bi->bcm.sender = my_identity; | ||
3136 | ubs.purpose.purpose = htonl ( | ||
3137 | GNUNET_SIGNATURE_PURPOSE_COMMUNICATOR_UDP_BROADCAST); | ||
3138 | ubs.purpose.size = htonl (sizeof(ubs)); | ||
3139 | ubs.sender = my_identity; | ||
3140 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
3141 | "creating UDPBroadcastSignature for %s\n", | ||
3142 | GNUNET_a2s (addr, addrlen)); | ||
3143 | GNUNET_CRYPTO_hash (addr, addrlen, &ubs.h_address); | ||
3144 | GNUNET_CRYPTO_eddsa_sign (my_private_key, | ||
3145 | &ubs, | ||
3146 | &bi->bcm.sender_sig); | ||
3147 | if (NULL != bi->ba) | ||
3148 | { | ||
3149 | bi->broadcast_task = GNUNET_SCHEDULER_add_now (&ifc_broadcast, bi); | ||
3150 | GNUNET_CONTAINER_DLL_insert (bi_head, bi_tail, bi); | ||
3151 | } | ||
3152 | if ((AF_INET6 == addr->sa_family) && (NULL != broadcast_addr)) | ||
3153 | { | ||
3154 | /* Create IPv6 multicast request */ | ||
3155 | const struct sockaddr_in6 *s6 = | ||
3156 | (const struct sockaddr_in6 *) broadcast_addr; | ||
3157 | |||
3158 | GNUNET_assert ( | ||
3159 | 1 == inet_pton (AF_INET6, "FF05::13B", &bi->mcreq.ipv6mr_multiaddr)); | ||
3160 | |||
3161 | /* http://tools.ietf.org/html/rfc2553#section-5.2: | ||
3162 | * | ||
3163 | * IPV6_JOIN_GROUP | ||
3164 | * | ||
3165 | * Join a multicast group on a specified local interface. If the | ||
3166 | * interface index is specified as 0, the kernel chooses the local | ||
3167 | * interface. For example, some kernels look up the multicast | ||
3168 | * group in the normal IPv6 routing table and using the resulting | ||
3169 | * interface; we do this for each interface, so no need to use | ||
3170 | * zero (anymore...). | ||
3171 | */ | ||
3172 | bi->mcreq.ipv6mr_interface = s6->sin6_scope_id; | ||
3173 | |||
3174 | /* Join the multicast group */ | ||
3175 | if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt (udp_sock, | ||
3176 | IPPROTO_IPV6, | ||
3177 | IPV6_JOIN_GROUP, | ||
3178 | &bi->mcreq, | ||
3179 | sizeof(bi->mcreq))) | ||
3180 | { | ||
3181 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); | ||
3182 | } | ||
3183 | } | ||
3184 | return GNUNET_OK; | ||
3185 | } | ||
3186 | |||
3187 | |||
3188 | /** | ||
3189 | * Scan interfaces to broadcast our presence on the LAN. | ||
3190 | * | ||
3191 | * @param cls NULL, unused | ||
3192 | */ | ||
3193 | static void | ||
3194 | do_broadcast (void *cls) | ||
3195 | { | ||
3196 | struct BroadcastInterface *bin; | ||
3197 | |||
3198 | (void) cls; | ||
3199 | for (struct BroadcastInterface *bi = bi_head; NULL != bi; bi = bi->next) | ||
3200 | bi->found = GNUNET_NO; | ||
3201 | GNUNET_OS_network_interfaces_list (&iface_proc, NULL); | ||
3202 | for (struct BroadcastInterface *bi = bi_head; NULL != bi; bi = bin) | ||
3203 | { | ||
3204 | bin = bi->next; | ||
3205 | if (GNUNET_NO == bi->found) | ||
3206 | bi_destroy (bi); | ||
3207 | } | ||
3208 | broadcast_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_SCAN_FREQUENCY, | ||
3209 | &do_broadcast, | ||
3210 | NULL); | ||
3211 | } | ||
3212 | |||
3213 | |||
3214 | static void | ||
3215 | try_connection_reversal (void *cls, | ||
3216 | const struct sockaddr *addr, | ||
3217 | socklen_t addrlen) | ||
3218 | { | ||
3219 | /* FIXME: support reversal: #5529 */ | ||
3220 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
3221 | "No connection reversal implemented!"); | ||
3222 | } | ||
3223 | |||
3224 | |||
3225 | /** | ||
3226 | * Setup communicator and launch network interactions. | ||
3227 | * | ||
3228 | * @param cls NULL (always) | ||
3229 | * @param args remaining command-line arguments | ||
3230 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
3231 | * @param c configuration | ||
3232 | */ | ||
3233 | static void | ||
3234 | run (void *cls, | ||
3235 | char *const *args, | ||
3236 | const char *cfgfile, | ||
3237 | const struct GNUNET_CONFIGURATION_Handle *c) | ||
3238 | { | ||
3239 | char *bindto; | ||
3240 | struct sockaddr *in; | ||
3241 | socklen_t in_len; | ||
3242 | struct sockaddr_storage in_sto; | ||
3243 | socklen_t sto_len; | ||
3244 | |||
3245 | (void) cls; | ||
3246 | cfg = c; | ||
3247 | if (GNUNET_OK != | ||
3248 | GNUNET_CONFIGURATION_get_value_string (cfg, | ||
3249 | COMMUNICATOR_CONFIG_SECTION, | ||
3250 | "BINDTO", | ||
3251 | &bindto)) | ||
3252 | { | ||
3253 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
3254 | COMMUNICATOR_CONFIG_SECTION, | ||
3255 | "BINDTO"); | ||
3256 | return; | ||
3257 | } | ||
3258 | |||
3259 | if (GNUNET_OK != | ||
3260 | GNUNET_CONFIGURATION_get_value_time (cfg, | ||
3261 | COMMUNICATOR_CONFIG_SECTION, | ||
3262 | "REKEY_INTERVAL", | ||
3263 | &rekey_interval)) | ||
3264 | rekey_interval = DEFAULT_REKEY_TIME_INTERVAL; | ||
3265 | |||
3266 | if (GNUNET_OK != | ||
3267 | GNUNET_CONFIGURATION_get_value_size (cfg, | ||
3268 | COMMUNICATOR_CONFIG_SECTION, | ||
3269 | "REKEY_MAX_BYTES", | ||
3270 | &rekey_max_bytes)) | ||
3271 | rekey_max_bytes = DEFAULT_REKEY_MAX_BYTES; | ||
3272 | |||
3273 | in = udp_address_to_sockaddr (bindto, &in_len); | ||
3274 | if (NULL == in) | ||
3275 | { | ||
3276 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
3277 | "Failed to setup UDP socket address with path `%s'\n", | ||
3278 | bindto); | ||
3279 | GNUNET_free (bindto); | ||
3280 | return; | ||
3281 | } | ||
3282 | udp_sock = | ||
3283 | GNUNET_NETWORK_socket_create (in->sa_family, | ||
3284 | SOCK_DGRAM, | ||
3285 | IPPROTO_UDP); | ||
3286 | if (NULL == udp_sock) | ||
3287 | { | ||
3288 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); | ||
3289 | GNUNET_free (in); | ||
3290 | GNUNET_free (bindto); | ||
3291 | return; | ||
3292 | } | ||
3293 | if (AF_INET6 == in->sa_family) | ||
3294 | have_v6_socket = GNUNET_YES; | ||
3295 | if (GNUNET_OK != | ||
3296 | GNUNET_NETWORK_socket_bind (udp_sock, | ||
3297 | in, | ||
3298 | in_len)) | ||
3299 | { | ||
3300 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, | ||
3301 | "bind", | ||
3302 | bindto); | ||
3303 | GNUNET_NETWORK_socket_close (udp_sock); | ||
3304 | udp_sock = NULL; | ||
3305 | GNUNET_free (in); | ||
3306 | GNUNET_free (bindto); | ||
3307 | return; | ||
3308 | } | ||
3309 | |||
3310 | /* We might have bound to port 0, allowing the OS to figure it out; | ||
3311 | thus, get the real IN-address from the socket */ | ||
3312 | sto_len = sizeof(in_sto); | ||
3313 | if (0 != getsockname (GNUNET_NETWORK_get_fd (udp_sock), | ||
3314 | (struct sockaddr *) &in_sto, | ||
3315 | &sto_len)) | ||
3316 | { | ||
3317 | memcpy (&in_sto, in, in_len); | ||
3318 | sto_len = in_len; | ||
3319 | } | ||
3320 | GNUNET_free (in); | ||
3321 | GNUNET_free (bindto); | ||
3322 | in = (struct sockaddr *) &in_sto; | ||
3323 | in_len = sto_len; | ||
3324 | GNUNET_log_from_nocheck (GNUNET_ERROR_TYPE_DEBUG, | ||
3325 | "transport", | ||
3326 | "Bound to `%s'\n", | ||
3327 | GNUNET_a2s ((const struct sockaddr *) &in_sto, | ||
3328 | sto_len)); | ||
3329 | switch (in->sa_family) | ||
3330 | { | ||
3331 | case AF_INET: | ||
3332 | my_port = ntohs (((struct sockaddr_in *) in)->sin_port); | ||
3333 | break; | ||
3334 | |||
3335 | case AF_INET6: | ||
3336 | my_port = ntohs (((struct sockaddr_in6 *) in)->sin6_port); | ||
3337 | break; | ||
3338 | |||
3339 | default: | ||
3340 | GNUNET_break (0); | ||
3341 | my_port = 0; | ||
3342 | } | ||
3343 | stats = GNUNET_STATISTICS_create ("C-UDP", cfg); | ||
3344 | senders = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES); | ||
3345 | receivers = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES); | ||
3346 | senders_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN); | ||
3347 | receivers_heap = | ||
3348 | GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN); | ||
3349 | key_cache = GNUNET_CONTAINER_multishortmap_create (1024, GNUNET_YES); | ||
3350 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); | ||
3351 | is = GNUNET_NT_scanner_init (); | ||
3352 | my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg); | ||
3353 | if (NULL == my_private_key) | ||
3354 | { | ||
3355 | GNUNET_log ( | ||
3356 | GNUNET_ERROR_TYPE_ERROR, | ||
3357 | _ ( | ||
3358 | "Transport service is lacking key configuration settings. Exiting.\n")); | ||
3359 | GNUNET_SCHEDULER_shutdown (); | ||
3360 | return; | ||
3361 | } | ||
3362 | GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_identity.public_key); | ||
3363 | /* start reading */ | ||
3364 | read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, | ||
3365 | udp_sock, | ||
3366 | &sock_read, | ||
3367 | NULL); | ||
3368 | ch = GNUNET_TRANSPORT_communicator_connect (cfg, | ||
3369 | COMMUNICATOR_CONFIG_SECTION, | ||
3370 | COMMUNICATOR_ADDRESS_PREFIX, | ||
3371 | GNUNET_TRANSPORT_CC_UNRELIABLE, | ||
3372 | &mq_init, | ||
3373 | NULL, | ||
3374 | &enc_notify_cb, | ||
3375 | NULL); | ||
3376 | if (NULL == ch) | ||
3377 | { | ||
3378 | GNUNET_break (0); | ||
3379 | GNUNET_SCHEDULER_shutdown (); | ||
3380 | return; | ||
3381 | } | ||
3382 | ah = GNUNET_TRANSPORT_application_init (cfg); | ||
3383 | if (NULL == ah) | ||
3384 | { | ||
3385 | GNUNET_break (0); | ||
3386 | GNUNET_SCHEDULER_shutdown (); | ||
3387 | return; | ||
3388 | } | ||
3389 | /* start broadcasting */ | ||
3390 | if (GNUNET_YES != | ||
3391 | GNUNET_CONFIGURATION_get_value_yesno (cfg, | ||
3392 | COMMUNICATOR_CONFIG_SECTION, | ||
3393 | "DISABLE_BROADCAST")) | ||
3394 | { | ||
3395 | broadcast_task = GNUNET_SCHEDULER_add_now (&do_broadcast, NULL); | ||
3396 | } | ||
3397 | nat = GNUNET_NAT_register (cfg, | ||
3398 | COMMUNICATOR_CONFIG_SECTION, | ||
3399 | IPPROTO_UDP, | ||
3400 | 1 /* one address */, | ||
3401 | (const struct sockaddr **) &in, | ||
3402 | &in_len, | ||
3403 | &nat_address_cb, | ||
3404 | try_connection_reversal, | ||
3405 | NULL /* closure */); | ||
3406 | } | ||
3407 | |||
3408 | |||
3409 | /** | ||
3410 | * The main function for the UNIX communicator. | ||
3411 | * | ||
3412 | * @param argc number of arguments from the command line | ||
3413 | * @param argv command line arguments | ||
3414 | * @return 0 ok, 1 on error | ||
3415 | */ | ||
3416 | int | ||
3417 | main (int argc, char *const *argv) | ||
3418 | { | ||
3419 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
3420 | GNUNET_GETOPT_OPTION_END | ||
3421 | }; | ||
3422 | int ret; | ||
3423 | |||
3424 | GNUNET_log_from_nocheck (GNUNET_ERROR_TYPE_DEBUG, | ||
3425 | "transport", | ||
3426 | "Starting udp communicator\n"); | ||
3427 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | ||
3428 | return 2; | ||
3429 | |||
3430 | ret = (GNUNET_OK == GNUNET_PROGRAM_run (argc, | ||
3431 | argv, | ||
3432 | "gnunet-communicator-udp", | ||
3433 | _ ("GNUnet UDP communicator"), | ||
3434 | options, | ||
3435 | &run, | ||
3436 | NULL)) | ||
3437 | ? 0 | ||
3438 | : 1; | ||
3439 | GNUNET_free_nz ((void *) argv); | ||
3440 | return ret; | ||
3441 | } | ||
3442 | |||
3443 | |||
3444 | /* end of gnunet-communicator-udp.c */ | ||