aboutsummaryrefslogtreecommitdiff
path: root/src/transport/gnunet-communicator-udp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/gnunet-communicator-udp.c')
-rw-r--r--src/transport/gnunet-communicator-udp.c2292
1 files changed, 1141 insertions, 1151 deletions
diff --git a/src/transport/gnunet-communicator-udp.c b/src/transport/gnunet-communicator-udp.c
index 2c8893d75..228540b81 100644
--- a/src/transport/gnunet-communicator-udp.c
+++ b/src/transport/gnunet-communicator-udp.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file transport/gnunet-communicator-udp.c 22 * @file transport/gnunet-communicator-udp.c
@@ -67,7 +67,7 @@
67 * How often do we scan for changes to our network interfaces? 67 * How often do we scan for changes to our network interfaces?
68 */ 68 */
69#define INTERFACE_SCAN_FREQUENCY \ 69#define INTERFACE_SCAN_FREQUENCY \
70 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) 70 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
71 71
72/** 72/**
73 * How long do we believe our addresses to remain up (before 73 * How long do we believe our addresses to remain up (before
@@ -156,8 +156,7 @@ GNUNET_NETWORK_STRUCT_BEGIN
156 * the specified sender. If possible, the receiver should respond with 156 * the specified sender. If possible, the receiver should respond with
157 * a `struct UDPAck` (possibly via backchannel). 157 * a `struct UDPAck` (possibly via backchannel).
158 */ 158 */
159struct UdpHandshakeSignature 159struct UdpHandshakeSignature {
160{
161 /** 160 /**
162 * Purpose must be #GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE 161 * Purpose must be #GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE
163 */ 162 */
@@ -190,9 +189,7 @@ struct UdpHandshakeSignature
190 * "Plaintext" header at beginning of KX message. Followed 189 * "Plaintext" header at beginning of KX message. Followed
191 * by encrypted `struct UDPConfirmation`. 190 * by encrypted `struct UDPConfirmation`.
192 */ 191 */
193struct InitialKX 192struct InitialKX {
194{
195
196 /** 193 /**
197 * Ephemeral key for KX. 194 * Ephemeral key for KX.
198 */ 195 */
@@ -210,8 +207,7 @@ struct InitialKX
210 * Encrypted continuation of UDP initial handshake, followed 207 * Encrypted continuation of UDP initial handshake, followed
211 * by message header with payload. 208 * by message header with payload.
212 */ 209 */
213struct UDPConfirmation 210struct UDPConfirmation {
214{
215 /** 211 /**
216 * Sender's identity 212 * Sender's identity
217 */ 213 */
@@ -238,9 +234,7 @@ struct UDPConfirmation
238 * UDP key acknowledgement. May be sent via backchannel. Allows the 234 * UDP key acknowledgement. May be sent via backchannel. Allows the
239 * sender to use `struct UDPBox` with the acknowledge key henceforth. 235 * sender to use `struct UDPBox` with the acknowledge key henceforth.
240 */ 236 */
241struct UDPAck 237struct UDPAck {
242{
243
244 /** 238 /**
245 * Type is #GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK. 239 * Type is #GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK.
246 */ 240 */
@@ -267,8 +261,7 @@ struct UDPAck
267 * be just any global peer -- an attacker must have at least 261 * be just any global peer -- an attacker must have at least
268 * shared a LAN with the peer they're pretending to be here. 262 * shared a LAN with the peer they're pretending to be here.
269 */ 263 */
270struct UdpBroadcastSignature 264struct UdpBroadcastSignature {
271{
272 /** 265 /**
273 * Purpose must be #GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST 266 * Purpose must be #GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST
274 */ 267 */
@@ -292,9 +285,7 @@ struct UdpBroadcastSignature
292 * recognized in LAN as GNUnet peers if this feature is enabled 285 * recognized in LAN as GNUnet peers if this feature is enabled
293 * anyway. Also, the entire message is in cleartext. 286 * anyway. Also, the entire message is in cleartext.
294 */ 287 */
295struct UDPBroadcast 288struct UDPBroadcast {
296{
297
298 /** 289 /**
299 * Sender's peer identity. 290 * Sender's peer identity.
300 */ 291 */
@@ -312,9 +303,7 @@ struct UDPBroadcast
312 * UDP message box. Always sent encrypted, only allowed after 303 * UDP message box. Always sent encrypted, only allowed after
313 * the receiver sent a `struct UDPAck` for the base key! 304 * the receiver sent a `struct UDPAck` for the base key!
314 */ 305 */
315struct UDPBox 306struct UDPBox {
316{
317
318 /** 307 /**
319 * Key and IV identification code. KDF applied to an acknowledged 308 * Key and IV identification code. KDF applied to an acknowledged
320 * base key and a sequence number. Sequence numbers must be used 309 * base key and a sequence number. Sequence numbers must be used
@@ -347,9 +336,7 @@ struct SharedSecret;
347 * Pre-generated "kid" code (key and IV identification code) to 336 * Pre-generated "kid" code (key and IV identification code) to
348 * quickly derive master key for a `struct UDPBox`. 337 * quickly derive master key for a `struct UDPBox`.
349 */ 338 */
350struct KeyCacheEntry 339struct KeyCacheEntry {
351{
352
353 /** 340 /**
354 * Kept in a DLL. 341 * Kept in a DLL.
355 */ 342 */
@@ -396,8 +383,7 @@ struct ReceiverAddress;
396/** 383/**
397 * Shared secret we generated for a particular sender or receiver. 384 * Shared secret we generated for a particular sender or receiver.
398 */ 385 */
399struct SharedSecret 386struct SharedSecret {
400{
401 /** 387 /**
402 * Kept in a DLL. 388 * Kept in a DLL.
403 */ 389 */
@@ -462,9 +448,7 @@ struct SharedSecret
462 * Information we track per sender address we have recently been 448 * Information we track per sender address we have recently been
463 * in contact with (we decrypt messages from the sender). 449 * in contact with (we decrypt messages from the sender).
464 */ 450 */
465struct SenderAddress 451struct SenderAddress {
466{
467
468 /** 452 /**
469 * To whom are we talking to. 453 * To whom are we talking to.
470 */ 454 */
@@ -516,9 +500,7 @@ struct SenderAddress
516 * Information we track per receiving address we have recently been 500 * Information we track per receiving address we have recently been
517 * in contact with (encryption to receiver). 501 * in contact with (encryption to receiver).
518 */ 502 */
519struct ReceiverAddress 503struct ReceiverAddress {
520{
521
522 /** 504 /**
523 * To whom are we talking to. 505 * To whom are we talking to.
524 */ 506 */
@@ -596,9 +578,7 @@ struct ReceiverAddress
596/** 578/**
597 * Interface we broadcast our presence on. 579 * Interface we broadcast our presence on.
598 */ 580 */
599struct BroadcastInterface 581struct BroadcastInterface {
600{
601
602 /** 582 /**
603 * Kept in a DLL. 583 * Kept in a DLL.
604 */ 584 */
@@ -759,25 +739,25 @@ static uint16_t my_port;
759 * @param bi entity to close down 739 * @param bi entity to close down
760 */ 740 */
761static void 741static void
762bi_destroy (struct BroadcastInterface *bi) 742bi_destroy(struct BroadcastInterface *bi)
763{ 743{
764 if (AF_INET6 == bi->sa->sa_family) 744 if (AF_INET6 == bi->sa->sa_family)
765 {
766 /* Leave the multicast group */
767 if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt (udp_sock,
768 IPPROTO_IPV6,
769 IPV6_LEAVE_GROUP,
770 &bi->mcreq,
771 sizeof (bi->mcreq)))
772 { 745 {
773 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 746 /* Leave the multicast group */
747 if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt(udp_sock,
748 IPPROTO_IPV6,
749 IPV6_LEAVE_GROUP,
750 &bi->mcreq,
751 sizeof(bi->mcreq)))
752 {
753 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "setsockopt");
754 }
774 } 755 }
775 } 756 GNUNET_CONTAINER_DLL_remove(bi_head, bi_tail, bi);
776 GNUNET_CONTAINER_DLL_remove (bi_head, bi_tail, bi); 757 GNUNET_SCHEDULER_cancel(bi->broadcast_task);
777 GNUNET_SCHEDULER_cancel (bi->broadcast_task); 758 GNUNET_free(bi->sa);
778 GNUNET_free (bi->sa); 759 GNUNET_free_non_null(bi->ba);
779 GNUNET_free_non_null (bi->ba); 760 GNUNET_free(bi);
780 GNUNET_free (bi);
781} 761}
782 762
783 763
@@ -787,35 +767,35 @@ bi_destroy (struct BroadcastInterface *bi)
787 * @param receiver entity to close down 767 * @param receiver entity to close down
788 */ 768 */
789static void 769static void
790receiver_destroy (struct ReceiverAddress *receiver) 770receiver_destroy(struct ReceiverAddress *receiver)
791{ 771{
792 struct GNUNET_MQ_Handle *mq; 772 struct GNUNET_MQ_Handle *mq;
793 773
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 774 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
795 "Disconnecting receiver for peer `%s'\n", 775 "Disconnecting receiver for peer `%s'\n",
796 GNUNET_i2s (&receiver->target)); 776 GNUNET_i2s(&receiver->target));
797 if (NULL != (mq = receiver->mq)) 777 if (NULL != (mq = receiver->mq))
798 { 778 {
799 receiver->mq = NULL; 779 receiver->mq = NULL;
800 GNUNET_MQ_destroy (mq); 780 GNUNET_MQ_destroy(mq);
801 } 781 }
802 if (NULL != receiver->qh) 782 if (NULL != receiver->qh)
803 { 783 {
804 GNUNET_TRANSPORT_communicator_mq_del (receiver->qh); 784 GNUNET_TRANSPORT_communicator_mq_del(receiver->qh);
805 receiver->qh = NULL; 785 receiver->qh = NULL;
806 } 786 }
807 GNUNET_assert (GNUNET_YES == 787 GNUNET_assert(GNUNET_YES ==
808 GNUNET_CONTAINER_multipeermap_remove (receivers, 788 GNUNET_CONTAINER_multipeermap_remove(receivers,
809 &receiver->target, 789 &receiver->target,
810 receiver)); 790 receiver));
811 GNUNET_assert (receiver == GNUNET_CONTAINER_heap_remove_node (receiver->hn)); 791 GNUNET_assert(receiver == GNUNET_CONTAINER_heap_remove_node(receiver->hn));
812 GNUNET_STATISTICS_set (stats, 792 GNUNET_STATISTICS_set(stats,
813 "# receivers active", 793 "# receivers active",
814 GNUNET_CONTAINER_multipeermap_size (receivers), 794 GNUNET_CONTAINER_multipeermap_size(receivers),
815 GNUNET_NO); 795 GNUNET_NO);
816 GNUNET_free (receiver->address); 796 GNUNET_free(receiver->address);
817 GNUNET_free (receiver->foreign_addr); 797 GNUNET_free(receiver->foreign_addr);
818 GNUNET_free (receiver); 798 GNUNET_free(receiver);
819} 799}
820 800
821 801
@@ -825,16 +805,16 @@ receiver_destroy (struct ReceiverAddress *receiver)
825 * @param kce the key cache entry 805 * @param kce the key cache entry
826 */ 806 */
827static void 807static void
828kce_destroy (struct KeyCacheEntry *kce) 808kce_destroy(struct KeyCacheEntry *kce)
829{ 809{
830 struct SharedSecret *ss = kce->ss; 810 struct SharedSecret *ss = kce->ss;
831 811
832 ss->active_kce_count--; 812 ss->active_kce_count--;
833 GNUNET_CONTAINER_DLL_remove (ss->kce_head, ss->kce_tail, kce); 813 GNUNET_CONTAINER_DLL_remove(ss->kce_head, ss->kce_tail, kce);
834 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multishortmap_remove (key_cache, 814 GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multishortmap_remove(key_cache,
835 &kce->kid, 815 &kce->kid,
836 kce)); 816 kce));
837 GNUNET_free (kce); 817 GNUNET_free(kce);
838} 818}
839 819
840 820
@@ -846,24 +826,24 @@ kce_destroy (struct KeyCacheEntry *kce)
846 * @param kid[out] where to write the key ID 826 * @param kid[out] where to write the key ID
847 */ 827 */
848static void 828static void
849get_kid (const struct GNUNET_HashCode *msec, 829get_kid(const struct GNUNET_HashCode *msec,
850 uint32_t serial, 830 uint32_t serial,
851 struct GNUNET_ShortHashCode *kid) 831 struct GNUNET_ShortHashCode *kid)
852{ 832{
853 uint32_t sid = htonl (serial); 833 uint32_t sid = htonl(serial);
854 834
855 GNUNET_CRYPTO_hkdf (kid, 835 GNUNET_CRYPTO_hkdf(kid,
856 sizeof (*kid), 836 sizeof(*kid),
857 GCRY_MD_SHA512, 837 GCRY_MD_SHA512,
858 GCRY_MD_SHA256, 838 GCRY_MD_SHA256,
859 &sid, 839 &sid,
860 sizeof (sid), 840 sizeof(sid),
861 msec, 841 msec,
862 sizeof (*msec), 842 sizeof(*msec),
863 "UDP-KID", 843 "UDP-KID",
864 strlen ("UDP-KID"), 844 strlen("UDP-KID"),
865 NULL, 845 NULL,
866 0); 846 0);
867} 847}
868 848
869 849
@@ -874,26 +854,26 @@ get_kid (const struct GNUNET_HashCode *msec,
874 * @param seq sequence number for the key cache entry 854 * @param seq sequence number for the key cache entry
875 */ 855 */
876static void 856static void
877kce_generate (struct SharedSecret *ss, uint32_t seq) 857kce_generate(struct SharedSecret *ss, uint32_t seq)
878{ 858{
879 struct KeyCacheEntry *kce; 859 struct KeyCacheEntry *kce;
880 860
881 GNUNET_assert (0 < seq); 861 GNUNET_assert(0 < seq);
882 kce = GNUNET_new (struct KeyCacheEntry); 862 kce = GNUNET_new(struct KeyCacheEntry);
883 kce->ss = ss; 863 kce->ss = ss;
884 kce->sequence_number = seq; 864 kce->sequence_number = seq;
885 get_kid (&ss->master, seq, &kce->kid); 865 get_kid(&ss->master, seq, &kce->kid);
886 GNUNET_CONTAINER_DLL_insert (ss->kce_head, ss->kce_tail, kce); 866 GNUNET_CONTAINER_DLL_insert(ss->kce_head, ss->kce_tail, kce);
887 ss->active_kce_count++; 867 ss->active_kce_count++;
888 (void) GNUNET_CONTAINER_multishortmap_put ( 868 (void)GNUNET_CONTAINER_multishortmap_put(
889 key_cache, 869 key_cache,
890 &kce->kid, 870 &kce->kid,
891 kce, 871 kce,
892 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 872 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
893 GNUNET_STATISTICS_set (stats, 873 GNUNET_STATISTICS_set(stats,
894 "# KIDs active", 874 "# KIDs active",
895 GNUNET_CONTAINER_multishortmap_size (key_cache), 875 GNUNET_CONTAINER_multishortmap_size(key_cache),
896 GNUNET_NO); 876 GNUNET_NO);
897} 877}
898 878
899 879
@@ -903,31 +883,31 @@ kce_generate (struct SharedSecret *ss, uint32_t seq)
903 * @param ss shared secret to destroy 883 * @param ss shared secret to destroy
904 */ 884 */
905static void 885static void
906secret_destroy (struct SharedSecret *ss) 886secret_destroy(struct SharedSecret *ss)
907{ 887{
908 struct SenderAddress *sender; 888 struct SenderAddress *sender;
909 struct ReceiverAddress *receiver; 889 struct ReceiverAddress *receiver;
910 struct KeyCacheEntry *kce; 890 struct KeyCacheEntry *kce;
911 891
912 if (NULL != (sender = ss->sender)) 892 if (NULL != (sender = ss->sender))
913 { 893 {
914 GNUNET_CONTAINER_DLL_remove (sender->ss_head, sender->ss_tail, ss); 894 GNUNET_CONTAINER_DLL_remove(sender->ss_head, sender->ss_tail, ss);
915 sender->num_secrets--; 895 sender->num_secrets--;
916 } 896 }
917 if (NULL != (receiver = ss->receiver)) 897 if (NULL != (receiver = ss->receiver))
918 { 898 {
919 GNUNET_CONTAINER_DLL_remove (receiver->ss_head, receiver->ss_tail, ss); 899 GNUNET_CONTAINER_DLL_remove(receiver->ss_head, receiver->ss_tail, ss);
920 receiver->num_secrets--; 900 receiver->num_secrets--;
921 receiver->acks_available -= (ss->sequence_allowed - ss->sequence_used); 901 receiver->acks_available -= (ss->sequence_allowed - ss->sequence_used);
922 } 902 }
923 while (NULL != (kce = ss->kce_head)) 903 while (NULL != (kce = ss->kce_head))
924 kce_destroy (kce); 904 kce_destroy(kce);
925 GNUNET_STATISTICS_update (stats, "# Secrets active", -1, GNUNET_NO); 905 GNUNET_STATISTICS_update(stats, "# Secrets active", -1, GNUNET_NO);
926 GNUNET_STATISTICS_set (stats, 906 GNUNET_STATISTICS_set(stats,
927 "# KIDs active", 907 "# KIDs active",
928 GNUNET_CONTAINER_multishortmap_size (key_cache), 908 GNUNET_CONTAINER_multishortmap_size(key_cache),
929 GNUNET_NO); 909 GNUNET_NO);
930 GNUNET_free (ss); 910 GNUNET_free(ss);
931} 911}
932 912
933 913
@@ -938,18 +918,18 @@ secret_destroy (struct SharedSecret *ss)
938 * @param sender entity to close down 918 * @param sender entity to close down
939 */ 919 */
940static void 920static void
941sender_destroy (struct SenderAddress *sender) 921sender_destroy(struct SenderAddress *sender)
942{ 922{
943 GNUNET_assert ( 923 GNUNET_assert(
944 GNUNET_YES == 924 GNUNET_YES ==
945 GNUNET_CONTAINER_multipeermap_remove (senders, &sender->target, sender)); 925 GNUNET_CONTAINER_multipeermap_remove(senders, &sender->target, sender));
946 GNUNET_assert (sender == GNUNET_CONTAINER_heap_remove_node (sender->hn)); 926 GNUNET_assert(sender == GNUNET_CONTAINER_heap_remove_node(sender->hn));
947 GNUNET_STATISTICS_set (stats, 927 GNUNET_STATISTICS_set(stats,
948 "# senders active", 928 "# senders active",
949 GNUNET_CONTAINER_multipeermap_size (senders), 929 GNUNET_CONTAINER_multipeermap_size(senders),
950 GNUNET_NO); 930 GNUNET_NO);
951 GNUNET_free (sender->address); 931 GNUNET_free(sender->address);
952 GNUNET_free (sender); 932 GNUNET_free(sender);
953} 933}
954 934
955 935
@@ -962,28 +942,28 @@ sender_destroy (struct SenderAddress *sender)
962 * @param iv[out] where to write the IV 942 * @param iv[out] where to write the IV
963 */ 943 */
964static void 944static void
965get_iv_key (const struct GNUNET_HashCode *msec, 945get_iv_key(const struct GNUNET_HashCode *msec,
966 uint32_t serial, 946 uint32_t serial,
967 char key[AES_KEY_SIZE], 947 char key[AES_KEY_SIZE],
968 char iv[AES_IV_SIZE]) 948 char iv[AES_IV_SIZE])
969{ 949{
970 uint32_t sid = htonl (serial); 950 uint32_t sid = htonl(serial);
971 char res[AES_KEY_SIZE + AES_IV_SIZE]; 951 char res[AES_KEY_SIZE + AES_IV_SIZE];
972 952
973 GNUNET_CRYPTO_hkdf (res, 953 GNUNET_CRYPTO_hkdf(res,
974 sizeof (res), 954 sizeof(res),
975 GCRY_MD_SHA512, 955 GCRY_MD_SHA512,
976 GCRY_MD_SHA256, 956 GCRY_MD_SHA256,
977 &sid, 957 &sid,
978 sizeof (sid), 958 sizeof(sid),
979 msec, 959 msec,
980 sizeof (*msec), 960 sizeof(*msec),
981 "UDP-IV-KEY", 961 "UDP-IV-KEY",
982 strlen ("UDP-IV-KEY"), 962 strlen("UDP-IV-KEY"),
983 NULL, 963 NULL,
984 0); 964 0);
985 memcpy (key, res, AES_KEY_SIZE); 965 memcpy(key, res, AES_KEY_SIZE);
986 memcpy (iv, &res[AES_KEY_SIZE], AES_IV_SIZE); 966 memcpy(iv, &res[AES_KEY_SIZE], AES_IV_SIZE);
987} 967}
988 968
989 969
@@ -993,11 +973,11 @@ get_iv_key (const struct GNUNET_HashCode *msec,
993 * @param sender address for which the timeout should be rescheduled 973 * @param sender address for which the timeout should be rescheduled
994 */ 974 */
995static void 975static void
996reschedule_sender_timeout (struct SenderAddress *sender) 976reschedule_sender_timeout(struct SenderAddress *sender)
997{ 977{
998 sender->timeout = 978 sender->timeout =
999 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); 979 GNUNET_TIME_relative_to_absolute(GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1000 GNUNET_CONTAINER_heap_update_cost (sender->hn, sender->timeout.abs_value_us); 980 GNUNET_CONTAINER_heap_update_cost(sender->hn, sender->timeout.abs_value_us);
1001} 981}
1002 982
1003 983
@@ -1007,12 +987,12 @@ reschedule_sender_timeout (struct SenderAddress *sender)
1007 * @param receiver address for which the timeout should be rescheduled 987 * @param receiver address for which the timeout should be rescheduled
1008 */ 988 */
1009static void 989static void
1010reschedule_receiver_timeout (struct ReceiverAddress *receiver) 990reschedule_receiver_timeout(struct ReceiverAddress *receiver)
1011{ 991{
1012 receiver->timeout = 992 receiver->timeout =
1013 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); 993 GNUNET_TIME_relative_to_absolute(GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1014 GNUNET_CONTAINER_heap_update_cost (receiver->hn, 994 GNUNET_CONTAINER_heap_update_cost(receiver->hn,
1015 receiver->timeout.abs_value_us); 995 receiver->timeout.abs_value_us);
1016} 996}
1017 997
1018 998
@@ -1022,7 +1002,7 @@ reschedule_receiver_timeout (struct ReceiverAddress *receiver)
1022 * @param cls unused, NULL 1002 * @param cls unused, NULL
1023 */ 1003 */
1024static void 1004static void
1025check_timeouts (void *cls) 1005check_timeouts(void *cls)
1026{ 1006{
1027 struct GNUNET_TIME_Relative st; 1007 struct GNUNET_TIME_Relative st;
1028 struct GNUNET_TIME_Relative rt; 1008 struct GNUNET_TIME_Relative rt;
@@ -1030,27 +1010,27 @@ check_timeouts (void *cls)
1030 struct ReceiverAddress *receiver; 1010 struct ReceiverAddress *receiver;
1031 struct SenderAddress *sender; 1011 struct SenderAddress *sender;
1032 1012
1033 (void) cls; 1013 (void)cls;
1034 timeout_task = NULL; 1014 timeout_task = NULL;
1035 rt = GNUNET_TIME_UNIT_FOREVER_REL; 1015 rt = GNUNET_TIME_UNIT_FOREVER_REL;
1036 while (NULL != (receiver = GNUNET_CONTAINER_heap_peek (receivers_heap))) 1016 while (NULL != (receiver = GNUNET_CONTAINER_heap_peek(receivers_heap)))
1037 { 1017 {
1038 rt = GNUNET_TIME_absolute_get_remaining (receiver->timeout); 1018 rt = GNUNET_TIME_absolute_get_remaining(receiver->timeout);
1039 if (0 != rt.rel_value_us) 1019 if (0 != rt.rel_value_us)
1040 break; 1020 break;
1041 receiver_destroy (receiver); 1021 receiver_destroy(receiver);
1042 } 1022 }
1043 st = GNUNET_TIME_UNIT_FOREVER_REL; 1023 st = GNUNET_TIME_UNIT_FOREVER_REL;
1044 while (NULL != (sender = GNUNET_CONTAINER_heap_peek (senders_heap))) 1024 while (NULL != (sender = GNUNET_CONTAINER_heap_peek(senders_heap)))
1045 { 1025 {
1046 st = GNUNET_TIME_absolute_get_remaining (sender->timeout); 1026 st = GNUNET_TIME_absolute_get_remaining(sender->timeout);
1047 if (0 != st.rel_value_us) 1027 if (0 != st.rel_value_us)
1048 break; 1028 break;
1049 sender_destroy (sender); 1029 sender_destroy(sender);
1050 } 1030 }
1051 delay = GNUNET_TIME_relative_min (rt, st); 1031 delay = GNUNET_TIME_relative_min(rt, st);
1052 if (delay.rel_value_us < GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 1032 if (delay.rel_value_us < GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
1053 timeout_task = GNUNET_SCHEDULER_add_delayed (delay, &check_timeouts, NULL); 1033 timeout_task = GNUNET_SCHEDULER_add_delayed(delay, &check_timeouts, NULL);
1054} 1034}
1055 1035
1056 1036
@@ -1060,20 +1040,20 @@ check_timeouts (void *cls)
1060 * @param ss[in,out] data structure to complete 1040 * @param ss[in,out] data structure to complete
1061 */ 1041 */
1062static void 1042static void
1063calculate_cmac (struct SharedSecret *ss) 1043calculate_cmac(struct SharedSecret *ss)
1064{ 1044{
1065 GNUNET_CRYPTO_hkdf (&ss->cmac, 1045 GNUNET_CRYPTO_hkdf(&ss->cmac,
1066 sizeof (ss->cmac), 1046 sizeof(ss->cmac),
1067 GCRY_MD_SHA512, 1047 GCRY_MD_SHA512,
1068 GCRY_MD_SHA256, 1048 GCRY_MD_SHA256,
1069 "CMAC", 1049 "CMAC",
1070 strlen ("CMAC"), 1050 strlen("CMAC"),
1071 &ss->master, 1051 &ss->master,
1072 sizeof (ss->master), 1052 sizeof(ss->master),
1073 "UDP-CMAC", 1053 "UDP-CMAC",
1074 strlen ("UDP-CMAC"), 1054 strlen("UDP-CMAC"),
1075 NULL, 1055 NULL,
1076 0); 1056 0);
1077} 1057}
1078 1058
1079 1059
@@ -1086,36 +1066,36 @@ calculate_cmac (struct SharedSecret *ss)
1086 * @param plaintext_len number of bytes of plaintext received 1066 * @param plaintext_len number of bytes of plaintext received
1087 */ 1067 */
1088static void 1068static void
1089pass_plaintext_to_core (struct SenderAddress *sender, 1069pass_plaintext_to_core(struct SenderAddress *sender,
1090 const void *plaintext, 1070 const void *plaintext,
1091 size_t plaintext_len) 1071 size_t plaintext_len)
1092{ 1072{
1093 const struct GNUNET_MessageHeader *hdr = plaintext; 1073 const struct GNUNET_MessageHeader *hdr = plaintext;
1094 1074
1095 while (ntohs (hdr->size) < plaintext_len) 1075 while (ntohs(hdr->size) < plaintext_len)
1096 { 1076 {
1097 GNUNET_STATISTICS_update (stats, 1077 GNUNET_STATISTICS_update(stats,
1098 "# bytes given to core", 1078 "# bytes given to core",
1099 ntohs (hdr->size), 1079 ntohs(hdr->size),
1100 GNUNET_NO); 1080 GNUNET_NO);
1101 (void) 1081 (void)
1102 GNUNET_TRANSPORT_communicator_receive (ch, 1082 GNUNET_TRANSPORT_communicator_receive(ch,
1103 &sender->target, 1083 &sender->target,
1104 hdr, 1084 hdr,
1105 ADDRESS_VALIDITY_PERIOD, 1085 ADDRESS_VALIDITY_PERIOD,
1106 NULL /* no flow control possible */ 1086 NULL /* no flow control possible */
1107 , 1087 ,
1108 NULL); 1088 NULL);
1109 /* move on to next message, if any */ 1089 /* move on to next message, if any */
1110 plaintext_len -= ntohs (hdr->size); 1090 plaintext_len -= ntohs(hdr->size);
1111 if (plaintext_len < sizeof (*hdr)) 1091 if (plaintext_len < sizeof(*hdr))
1112 break; 1092 break;
1113 hdr = plaintext + ntohs (hdr->size); 1093 hdr = plaintext + ntohs(hdr->size);
1114 } 1094 }
1115 GNUNET_STATISTICS_update (stats, 1095 GNUNET_STATISTICS_update(stats,
1116 "# bytes padding discarded", 1096 "# bytes padding discarded",
1117 plaintext_len, 1097 plaintext_len,
1118 GNUNET_NO); 1098 GNUNET_NO);
1119} 1099}
1120 1100
1121 1101
@@ -1128,20 +1108,20 @@ pass_plaintext_to_core (struct SenderAddress *sender,
1128 * @param cipher[out] cipher to initialize 1108 * @param cipher[out] cipher to initialize
1129 */ 1109 */
1130static void 1110static void
1131setup_cipher (const struct GNUNET_HashCode *msec, 1111setup_cipher(const struct GNUNET_HashCode *msec,
1132 uint32_t serial, 1112 uint32_t serial,
1133 gcry_cipher_hd_t *cipher) 1113 gcry_cipher_hd_t *cipher)
1134{ 1114{
1135 char key[AES_KEY_SIZE]; 1115 char key[AES_KEY_SIZE];
1136 char iv[AES_IV_SIZE]; 1116 char iv[AES_IV_SIZE];
1137 1117
1138 gcry_cipher_open (cipher, 1118 gcry_cipher_open(cipher,
1139 GCRY_CIPHER_AES256 /* low level: go for speed */, 1119 GCRY_CIPHER_AES256 /* low level: go for speed */,
1140 GCRY_CIPHER_MODE_GCM, 1120 GCRY_CIPHER_MODE_GCM,
1141 0 /* flags */); 1121 0 /* flags */);
1142 get_iv_key (msec, serial, key, iv); 1122 get_iv_key(msec, serial, key, iv);
1143 gcry_cipher_setkey (*cipher, key, sizeof (key)); 1123 gcry_cipher_setkey(*cipher, key, sizeof(key));
1144 gcry_cipher_setiv (*cipher, iv, sizeof (iv)); 1124 gcry_cipher_setiv(*cipher, iv, sizeof(iv));
1145} 1125}
1146 1126
1147 1127
@@ -1158,29 +1138,29 @@ setup_cipher (const struct GNUNET_HashCode *msec,
1158 * @return #GNUNET_OK on success 1138 * @return #GNUNET_OK on success
1159 */ 1139 */
1160static int 1140static int
1161try_decrypt (const struct SharedSecret *ss, 1141try_decrypt(const struct SharedSecret *ss,
1162 const char tag[GCM_TAG_SIZE], 1142 const char tag[GCM_TAG_SIZE],
1163 uint32_t serial, 1143 uint32_t serial,
1164 const char *in_buf, 1144 const char *in_buf,
1165 size_t in_buf_size, 1145 size_t in_buf_size,
1166 char *out_buf) 1146 char *out_buf)
1167{ 1147{
1168 gcry_cipher_hd_t cipher; 1148 gcry_cipher_hd_t cipher;
1169 1149
1170 setup_cipher (&ss->master, serial, &cipher); 1150 setup_cipher(&ss->master, serial, &cipher);
1171 GNUNET_assert ( 1151 GNUNET_assert(
1172 0 == 1152 0 ==
1173 gcry_cipher_decrypt (cipher, out_buf, in_buf_size, in_buf, in_buf_size)); 1153 gcry_cipher_decrypt(cipher, out_buf, in_buf_size, in_buf, in_buf_size));
1174 if (0 != gcry_cipher_checktag (cipher, tag, GCM_TAG_SIZE)) 1154 if (0 != gcry_cipher_checktag(cipher, tag, GCM_TAG_SIZE))
1175 { 1155 {
1176 gcry_cipher_close (cipher); 1156 gcry_cipher_close(cipher);
1177 GNUNET_STATISTICS_update (stats, 1157 GNUNET_STATISTICS_update(stats,
1178 "# AEAD authentication failures", 1158 "# AEAD authentication failures",
1179 1, 1159 1,
1180 GNUNET_NO); 1160 GNUNET_NO);
1181 return GNUNET_SYSERR; 1161 return GNUNET_SYSERR;
1182 } 1162 }
1183 gcry_cipher_close (cipher); 1163 gcry_cipher_close(cipher);
1184 return GNUNET_OK; 1164 return GNUNET_OK;
1185} 1165}
1186 1166
@@ -1192,12 +1172,12 @@ try_decrypt (const struct SharedSecret *ss,
1192 * @return new shared secret 1172 * @return new shared secret
1193 */ 1173 */
1194static struct SharedSecret * 1174static struct SharedSecret *
1195setup_shared_secret_dec (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral) 1175setup_shared_secret_dec(const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral)
1196{ 1176{
1197 struct SharedSecret *ss; 1177 struct SharedSecret *ss;
1198 1178
1199 ss = GNUNET_new (struct SharedSecret); 1179 ss = GNUNET_new(struct SharedSecret);
1200 GNUNET_CRYPTO_eddsa_ecdh (my_private_key, ephemeral, &ss->master); 1180 GNUNET_CRYPTO_eddsa_ecdh(my_private_key, ephemeral, &ss->master);
1201 return ss; 1181 return ss;
1202} 1182}
1203 1183
@@ -1210,20 +1190,20 @@ setup_shared_secret_dec (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral)
1210 * @return new shared secret 1190 * @return new shared secret
1211 */ 1191 */
1212static struct SharedSecret * 1192static struct SharedSecret *
1213setup_shared_secret_enc (const struct GNUNET_CRYPTO_EcdhePrivateKey *ephemeral, 1193setup_shared_secret_enc(const struct GNUNET_CRYPTO_EcdhePrivateKey *ephemeral,
1214 struct ReceiverAddress *receiver) 1194 struct ReceiverAddress *receiver)
1215{ 1195{
1216 struct SharedSecret *ss; 1196 struct SharedSecret *ss;
1217 1197
1218 ss = GNUNET_new (struct SharedSecret); 1198 ss = GNUNET_new(struct SharedSecret);
1219 GNUNET_CRYPTO_ecdh_eddsa (ephemeral, 1199 GNUNET_CRYPTO_ecdh_eddsa(ephemeral,
1220 &receiver->target.public_key, 1200 &receiver->target.public_key,
1221 &ss->master); 1201 &ss->master);
1222 calculate_cmac (ss); 1202 calculate_cmac(ss);
1223 ss->receiver = receiver; 1203 ss->receiver = receiver;
1224 GNUNET_CONTAINER_DLL_insert (receiver->ss_head, receiver->ss_tail, ss); 1204 GNUNET_CONTAINER_DLL_insert(receiver->ss_head, receiver->ss_tail, ss);
1225 receiver->num_secrets++; 1205 receiver->num_secrets++;
1226 GNUNET_STATISTICS_update (stats, "# Secrets active", 1, GNUNET_NO); 1206 GNUNET_STATISTICS_update(stats, "# Secrets active", 1, GNUNET_NO);
1227 return ss; 1207 return ss;
1228} 1208}
1229 1209
@@ -1236,7 +1216,7 @@ setup_shared_secret_enc (const struct GNUNET_CRYPTO_EcdhePrivateKey *ephemeral,
1236 * @param receiver receiver to setup MQ for 1216 * @param receiver receiver to setup MQ for
1237 */ 1217 */
1238static void 1218static void
1239setup_receiver_mq (struct ReceiverAddress *receiver); 1219setup_receiver_mq(struct ReceiverAddress *receiver);
1240 1220
1241 1221
1242/** 1222/**
@@ -1250,36 +1230,36 @@ setup_receiver_mq (struct ReceiverAddress *receiver);
1250 * @return #GNUNET_YES to continue to iterate 1230 * @return #GNUNET_YES to continue to iterate
1251 */ 1231 */
1252static int 1232static int
1253handle_ack (void *cls, const struct GNUNET_PeerIdentity *pid, void *value) 1233handle_ack(void *cls, const struct GNUNET_PeerIdentity *pid, void *value)
1254{ 1234{
1255 const struct UDPAck *ack = cls; 1235 const struct UDPAck *ack = cls;
1256 struct ReceiverAddress *receiver = value; 1236 struct ReceiverAddress *receiver = value;
1257 1237
1258 (void) pid; 1238 (void)pid;
1259 for (struct SharedSecret *ss = receiver->ss_head; NULL != ss; ss = ss->next) 1239 for (struct SharedSecret *ss = receiver->ss_head; NULL != ss; ss = ss->next)
1260 {
1261 if (0 == memcmp (&ack->cmac, &ss->cmac, sizeof (struct GNUNET_HashCode)))
1262 { 1240 {
1263 uint32_t allowed; 1241 if (0 == memcmp(&ack->cmac, &ss->cmac, sizeof(struct GNUNET_HashCode)))
1264
1265 allowed = ntohl (ack->sequence_max);
1266
1267 if (allowed > ss->sequence_allowed)
1268 {
1269 receiver->acks_available += (allowed - ss->sequence_allowed);
1270 if ((allowed - ss->sequence_allowed) == receiver->acks_available)
1271 { 1242 {
1272 /* we just incremented from zero => MTU change! */ 1243 uint32_t allowed;
1273 setup_receiver_mq (receiver); 1244
1245 allowed = ntohl(ack->sequence_max);
1246
1247 if (allowed > ss->sequence_allowed)
1248 {
1249 receiver->acks_available += (allowed - ss->sequence_allowed);
1250 if ((allowed - ss->sequence_allowed) == receiver->acks_available)
1251 {
1252 /* we just incremented from zero => MTU change! */
1253 setup_receiver_mq(receiver);
1254 }
1255 ss->sequence_allowed = allowed;
1256 /* move ss to head to avoid discarding it anytime soon! */
1257 GNUNET_CONTAINER_DLL_remove(receiver->ss_head, receiver->ss_tail, ss);
1258 GNUNET_CONTAINER_DLL_insert(receiver->ss_head, receiver->ss_tail, ss);
1259 }
1260 return GNUNET_NO;
1274 } 1261 }
1275 ss->sequence_allowed = allowed;
1276 /* move ss to head to avoid discarding it anytime soon! */
1277 GNUNET_CONTAINER_DLL_remove (receiver->ss_head, receiver->ss_tail, ss);
1278 GNUNET_CONTAINER_DLL_insert (receiver->ss_head, receiver->ss_tail, ss);
1279 }
1280 return GNUNET_NO;
1281 } 1262 }
1282 }
1283 return GNUNET_YES; 1263 return GNUNET_YES;
1284} 1264}
1285 1265
@@ -1293,39 +1273,41 @@ handle_ack (void *cls, const struct GNUNET_PeerIdentity *pid, void *value)
1293 * @param buf_size number of bytes in @a buf 1273 * @param buf_size number of bytes in @a buf
1294 */ 1274 */
1295static void 1275static void
1296try_handle_plaintext (struct SenderAddress *sender, 1276try_handle_plaintext(struct SenderAddress *sender,
1297 const void *buf, 1277 const void *buf,
1298 size_t buf_size) 1278 size_t buf_size)
1299{ 1279{
1300 const struct GNUNET_MessageHeader *hdr = 1280 const struct GNUNET_MessageHeader *hdr =
1301 (const struct GNUNET_MessageHeader *) buf; 1281 (const struct GNUNET_MessageHeader *)buf;
1302 const struct UDPAck *ack = (const struct UDPAck *) buf; 1282 const struct UDPAck *ack = (const struct UDPAck *)buf;
1303 uint16_t type; 1283 uint16_t type;
1304 1284
1305 if (sizeof (*hdr) > buf_size) 1285 if (sizeof(*hdr) > buf_size)
1306 return; /* not even a header */ 1286 return; /* not even a header */
1307 if (ntohs (hdr->size) > buf_size) 1287 if (ntohs(hdr->size) > buf_size)
1308 return; /* not even a header */ 1288 return; /* not even a header */
1309 type = ntohs (hdr->type); 1289 type = ntohs(hdr->type);
1310 switch (type) 1290 switch (type)
1311 { 1291 {
1312 case GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK: 1292 case GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK:
1313 /* lookup master secret by 'cmac', then update sequence_max */ 1293 /* lookup master secret by 'cmac', then update sequence_max */
1314 GNUNET_CONTAINER_multipeermap_get_multiple (receivers, 1294 GNUNET_CONTAINER_multipeermap_get_multiple(receivers,
1315 &sender->target, 1295 &sender->target,
1316 &handle_ack, 1296 &handle_ack,
1317 (void *) ack); 1297 (void *)ack);
1318 /* There could be more messages after the ACK, handle those as well */ 1298 /* There could be more messages after the ACK, handle those as well */
1319 buf += ntohs (hdr->size); 1299 buf += ntohs(hdr->size);
1320 buf_size -= ntohs (hdr->size); 1300 buf_size -= ntohs(hdr->size);
1321 pass_plaintext_to_core (sender, buf, buf_size); 1301 pass_plaintext_to_core(sender, buf, buf_size);
1322 break; 1302 break;
1323 case GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_PAD: 1303
1324 /* skip padding */ 1304 case GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_PAD:
1325 break; 1305 /* skip padding */
1326 default: 1306 break;
1327 pass_plaintext_to_core (sender, buf, buf_size); 1307
1328 } 1308 default:
1309 pass_plaintext_to_core(sender, buf, buf_size);
1310 }
1329} 1311}
1330 1312
1331 1313
@@ -1338,29 +1320,29 @@ try_handle_plaintext (struct SenderAddress *sender,
1338 * @param ss shared secret to generate ACKs for 1320 * @param ss shared secret to generate ACKs for
1339 */ 1321 */
1340static void 1322static void
1341consider_ss_ack (struct SharedSecret *ss) 1323consider_ss_ack(struct SharedSecret *ss)
1342{ 1324{
1343 GNUNET_assert (NULL != ss->sender); 1325 GNUNET_assert(NULL != ss->sender);
1344 /* drop ancient KeyCacheEntries */ 1326 /* drop ancient KeyCacheEntries */
1345 while ((NULL != ss->kce_head) && 1327 while ((NULL != ss->kce_head) &&
1346 (MAX_SQN_DELTA < 1328 (MAX_SQN_DELTA <
1347 ss->kce_head->sequence_number - ss->kce_tail->sequence_number)) 1329 ss->kce_head->sequence_number - ss->kce_tail->sequence_number))
1348 kce_destroy (ss->kce_tail); 1330 kce_destroy(ss->kce_tail);
1349 if (ss->active_kce_count < KCN_THRESHOLD) 1331 if (ss->active_kce_count < KCN_THRESHOLD)
1350 { 1332 {
1351 struct UDPAck ack; 1333 struct UDPAck ack;
1352 1334
1353 while (ss->active_kce_count < KCN_TARGET) 1335 while (ss->active_kce_count < KCN_TARGET)
1354 kce_generate (ss, ++ss->sequence_allowed); 1336 kce_generate(ss, ++ss->sequence_allowed);
1355 ack.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK); 1337 ack.header.type = htons(GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK);
1356 ack.header.size = htons (sizeof (ack)); 1338 ack.header.size = htons(sizeof(ack));
1357 ack.sequence_max = htonl (ss->sequence_allowed); 1339 ack.sequence_max = htonl(ss->sequence_allowed);
1358 ack.cmac = ss->cmac; 1340 ack.cmac = ss->cmac;
1359 GNUNET_TRANSPORT_communicator_notify (ch, 1341 GNUNET_TRANSPORT_communicator_notify(ch,
1360 &ss->sender->target, 1342 &ss->sender->target,
1361 COMMUNICATOR_ADDRESS_PREFIX, 1343 COMMUNICATOR_ADDRESS_PREFIX,
1362 &ack.header); 1344 &ack.header);
1363 } 1345 }
1364} 1346}
1365 1347
1366 1348
@@ -1372,43 +1354,42 @@ consider_ss_ack (struct SharedSecret *ss)
1372 * @param kce key index to decrypt @a box 1354 * @param kce key index to decrypt @a box
1373 */ 1355 */
1374static void 1356static void
1375decrypt_box (const struct UDPBox *box, 1357decrypt_box(const struct UDPBox *box,
1376 size_t box_len, 1358 size_t box_len,
1377 struct KeyCacheEntry *kce) 1359 struct KeyCacheEntry *kce)
1378{ 1360{
1379 struct SharedSecret *ss = kce->ss; 1361 struct SharedSecret *ss = kce->ss;
1380 char out_buf[box_len - sizeof (*box)]; 1362 char out_buf[box_len - sizeof(*box)];
1381 1363
1382 GNUNET_assert (NULL != ss->sender); 1364 GNUNET_assert(NULL != ss->sender);
1383 if (GNUNET_OK != try_decrypt (ss, 1365 if (GNUNET_OK != try_decrypt(ss,
1384 box->gcm_tag, 1366 box->gcm_tag,
1385 kce->sequence_number, 1367 kce->sequence_number,
1386 (const char *) &box[1], 1368 (const char *)&box[1],
1387 sizeof (out_buf), 1369 sizeof(out_buf),
1388 out_buf)) 1370 out_buf))
1389 { 1371 {
1390 GNUNET_STATISTICS_update (stats, 1372 GNUNET_STATISTICS_update(stats,
1391 "# Decryption failures with valid KCE", 1373 "# Decryption failures with valid KCE",
1392 1, 1374 1,
1393 GNUNET_NO); 1375 GNUNET_NO);
1394 kce_destroy (kce); 1376 kce_destroy(kce);
1395 return; 1377 return;
1396 } 1378 }
1397 kce_destroy (kce); 1379 kce_destroy(kce);
1398 GNUNET_STATISTICS_update (stats, 1380 GNUNET_STATISTICS_update(stats,
1399 "# bytes decrypted with BOX", 1381 "# bytes decrypted with BOX",
1400 sizeof (out_buf), 1382 sizeof(out_buf),
1401 GNUNET_NO); 1383 GNUNET_NO);
1402 try_handle_plaintext (ss->sender, out_buf, sizeof (out_buf)); 1384 try_handle_plaintext(ss->sender, out_buf, sizeof(out_buf));
1403 consider_ss_ack (ss); 1385 consider_ss_ack(ss);
1404} 1386}
1405 1387
1406 1388
1407/** 1389/**
1408 * Closure for #find_sender_by_address() 1390 * Closure for #find_sender_by_address()
1409 */ 1391 */
1410struct SearchContext 1392struct SearchContext {
1411{
1412 /** 1393 /**
1413 * Address we are looking for. 1394 * Address we are looking for.
1414 */ 1395 */
@@ -1435,19 +1416,19 @@ struct SearchContext
1435 * @return #GNUNET_YES if not found (continue to search), #GNUNET_NO if found 1416 * @return #GNUNET_YES if not found (continue to search), #GNUNET_NO if found
1436 */ 1417 */
1437static int 1418static int
1438find_sender_by_address (void *cls, 1419find_sender_by_address(void *cls,
1439 const struct GNUNET_PeerIdentity *key, 1420 const struct GNUNET_PeerIdentity *key,
1440 void *value) 1421 void *value)
1441{ 1422{
1442 struct SearchContext *sc = cls; 1423 struct SearchContext *sc = cls;
1443 struct SenderAddress *sender = value; 1424 struct SenderAddress *sender = value;
1444 1425
1445 if ((sender->address_len == sc->address_len) && 1426 if ((sender->address_len == sc->address_len) &&
1446 (0 == memcmp (sender->address, sc->address, sender->address_len))) 1427 (0 == memcmp(sender->address, sc->address, sender->address_len)))
1447 { 1428 {
1448 sc->sender = sender; 1429 sc->sender = sender;
1449 return GNUNET_NO; /* stop iterating! */ 1430 return GNUNET_NO; /* stop iterating! */
1450 } 1431 }
1451 return GNUNET_YES; 1432 return GNUNET_YES;
1452} 1433}
1453 1434
@@ -1464,45 +1445,45 @@ find_sender_by_address (void *cls,
1464 * decrypting data from @a target 1445 * decrypting data from @a target
1465 */ 1446 */
1466static struct SenderAddress * 1447static struct SenderAddress *
1467setup_sender (const struct GNUNET_PeerIdentity *target, 1448setup_sender(const struct GNUNET_PeerIdentity *target,
1468 const struct sockaddr *address, 1449 const struct sockaddr *address,
1469 socklen_t address_len) 1450 socklen_t address_len)
1470{ 1451{
1471 struct SenderAddress *sender; 1452 struct SenderAddress *sender;
1472 struct SearchContext sc = {.address = address, 1453 struct SearchContext sc = { .address = address,
1473 .address_len = address_len, 1454 .address_len = address_len,
1474 .sender = NULL}; 1455 .sender = NULL };
1475 1456
1476 GNUNET_CONTAINER_multipeermap_get_multiple (senders, 1457 GNUNET_CONTAINER_multipeermap_get_multiple(senders,
1477 target, 1458 target,
1478 &find_sender_by_address, 1459 &find_sender_by_address,
1479 &sc); 1460 &sc);
1480 if (NULL != sc.sender) 1461 if (NULL != sc.sender)
1481 { 1462 {
1482 reschedule_sender_timeout (sc.sender); 1463 reschedule_sender_timeout(sc.sender);
1483 return sc.sender; 1464 return sc.sender;
1484 } 1465 }
1485 sender = GNUNET_new (struct SenderAddress); 1466 sender = GNUNET_new(struct SenderAddress);
1486 sender->target = *target; 1467 sender->target = *target;
1487 sender->address = GNUNET_memdup (address, address_len); 1468 sender->address = GNUNET_memdup(address, address_len);
1488 sender->address_len = address_len; 1469 sender->address_len = address_len;
1489 (void) GNUNET_CONTAINER_multipeermap_put ( 1470 (void)GNUNET_CONTAINER_multipeermap_put(
1490 senders, 1471 senders,
1491 &sender->target, 1472 &sender->target,
1492 sender, 1473 sender,
1493 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 1474 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1494 GNUNET_STATISTICS_set (stats, 1475 GNUNET_STATISTICS_set(stats,
1495 "# senders active", 1476 "# senders active",
1496 GNUNET_CONTAINER_multipeermap_size (receivers), 1477 GNUNET_CONTAINER_multipeermap_size(receivers),
1497 GNUNET_NO); 1478 GNUNET_NO);
1498 sender->timeout = 1479 sender->timeout =
1499 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); 1480 GNUNET_TIME_relative_to_absolute(GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1500 sender->hn = GNUNET_CONTAINER_heap_insert (senders_heap, 1481 sender->hn = GNUNET_CONTAINER_heap_insert(senders_heap,
1501 sender, 1482 sender,
1502 sender->timeout.abs_value_us); 1483 sender->timeout.abs_value_us);
1503 sender->nt = GNUNET_NT_scanner_get_type (is, address, address_len); 1484 sender->nt = GNUNET_NT_scanner_get_type(is, address, address_len);
1504 if (NULL == timeout_task) 1485 if (NULL == timeout_task)
1505 timeout_task = GNUNET_SCHEDULER_add_now (&check_timeouts, NULL); 1486 timeout_task = GNUNET_SCHEDULER_add_now(&check_timeouts, NULL);
1506 return sender; 1487 return sender;
1507} 1488}
1508 1489
@@ -1515,21 +1496,21 @@ setup_sender (const struct GNUNET_PeerIdentity *target,
1515 * @return #GNUNET_OK if signature is valid 1496 * @return #GNUNET_OK if signature is valid
1516 */ 1497 */
1517static int 1498static int
1518verify_confirmation (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral, 1499verify_confirmation(const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral,
1519 const struct UDPConfirmation *uc) 1500 const struct UDPConfirmation *uc)
1520{ 1501{
1521 struct UdpHandshakeSignature uhs; 1502 struct UdpHandshakeSignature uhs;
1522 1503
1523 uhs.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE); 1504 uhs.purpose.purpose = htonl(GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE);
1524 uhs.purpose.size = htonl (sizeof (uhs)); 1505 uhs.purpose.size = htonl(sizeof(uhs));
1525 uhs.sender = uc->sender; 1506 uhs.sender = uc->sender;
1526 uhs.receiver = my_identity; 1507 uhs.receiver = my_identity;
1527 uhs.ephemeral = *ephemeral; 1508 uhs.ephemeral = *ephemeral;
1528 uhs.monotonic_time = uc->monotonic_time; 1509 uhs.monotonic_time = uc->monotonic_time;
1529 return GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE, 1510 return GNUNET_CRYPTO_eddsa_verify(GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE,
1530 &uhs.purpose, 1511 &uhs.purpose,
1531 &uc->sender_sig, 1512 &uc->sender_sig,
1532 &uc->sender.public_key); 1513 &uc->sender.public_key);
1533} 1514}
1534 1515
1535 1516
@@ -1542,28 +1523,30 @@ verify_confirmation (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral,
1542 * @return string representation of @a address 1523 * @return string representation of @a address
1543 */ 1524 */
1544static char * 1525static char *
1545sockaddr_to_udpaddr_string (const struct sockaddr *address, 1526sockaddr_to_udpaddr_string(const struct sockaddr *address,
1546 socklen_t address_len) 1527 socklen_t address_len)
1547{ 1528{
1548 char *ret; 1529 char *ret;
1549 1530
1550 switch (address->sa_family) 1531 switch (address->sa_family)
1551 { 1532 {
1552 case AF_INET: 1533 case AF_INET:
1553 GNUNET_asprintf (&ret, 1534 GNUNET_asprintf(&ret,
1554 "%s-%s", 1535 "%s-%s",
1555 COMMUNICATOR_ADDRESS_PREFIX, 1536 COMMUNICATOR_ADDRESS_PREFIX,
1556 GNUNET_a2s (address, address_len)); 1537 GNUNET_a2s(address, address_len));
1557 break; 1538 break;
1558 case AF_INET6: 1539
1559 GNUNET_asprintf (&ret, 1540 case AF_INET6:
1560 "%s-%s", 1541 GNUNET_asprintf(&ret,
1561 COMMUNICATOR_ADDRESS_PREFIX, 1542 "%s-%s",
1562 GNUNET_a2s (address, address_len)); 1543 COMMUNICATOR_ADDRESS_PREFIX,
1563 break; 1544 GNUNET_a2s(address, address_len));
1564 default: 1545 break;
1565 GNUNET_assert (0); 1546
1566 } 1547 default:
1548 GNUNET_assert(0);
1549 }
1567 return ret; 1550 return ret;
1568} 1551}
1569 1552
@@ -1574,137 +1557,137 @@ sockaddr_to_udpaddr_string (const struct sockaddr *address,
1574 * @param cls NULL 1557 * @param cls NULL
1575 */ 1558 */
1576static void 1559static void
1577sock_read (void *cls) 1560sock_read(void *cls)
1578{ 1561{
1579 struct sockaddr_storage sa; 1562 struct sockaddr_storage sa;
1580 socklen_t salen = sizeof (sa); 1563 socklen_t salen = sizeof(sa);
1581 char buf[UINT16_MAX]; 1564 char buf[UINT16_MAX];
1582 ssize_t rcvd; 1565 ssize_t rcvd;
1583 1566
1584 (void) cls; 1567 (void)cls;
1585 read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 1568 read_task = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
1586 udp_sock, 1569 udp_sock,
1587 &sock_read, 1570 &sock_read,
1588 NULL); 1571 NULL);
1589 rcvd = GNUNET_NETWORK_socket_recvfrom (udp_sock, 1572 rcvd = GNUNET_NETWORK_socket_recvfrom(udp_sock,
1590 buf, 1573 buf,
1591 sizeof (buf), 1574 sizeof(buf),
1592 (struct sockaddr *) &sa, 1575 (struct sockaddr *)&sa,
1593 &salen); 1576 &salen);
1594 if (-1 == rcvd) 1577 if (-1 == rcvd)
1595 { 1578 {
1596 GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "recv"); 1579 GNUNET_log_strerror(GNUNET_ERROR_TYPE_DEBUG, "recv");
1597 return; 1580 return;
1598 } 1581 }
1599 1582
1600 /* first, see if it is a UDPBox */ 1583 /* first, see if it is a UDPBox */
1601 if (rcvd > sizeof (struct UDPBox)) 1584 if (rcvd > sizeof(struct UDPBox))
1602 {
1603 const struct UDPBox *box;
1604 struct KeyCacheEntry *kce;
1605
1606 box = (const struct UDPBox *) buf;
1607 kce = GNUNET_CONTAINER_multishortmap_get (key_cache, &box->kid);
1608 if (NULL != kce)
1609 { 1585 {
1610 decrypt_box (box, (size_t) rcvd, kce); 1586 const struct UDPBox *box;
1611 return; 1587 struct KeyCacheEntry *kce;
1588
1589 box = (const struct UDPBox *)buf;
1590 kce = GNUNET_CONTAINER_multishortmap_get(key_cache, &box->kid);
1591 if (NULL != kce)
1592 {
1593 decrypt_box(box, (size_t)rcvd, kce);
1594 return;
1595 }
1612 } 1596 }
1613 }
1614 1597
1615 /* next, check if it is a broadcast */ 1598 /* next, check if it is a broadcast */
1616 if (sizeof (struct UDPBroadcast) == rcvd) 1599 if (sizeof(struct UDPBroadcast) == rcvd)
1617 {
1618 const struct UDPBroadcast *ub;
1619 struct UdpBroadcastSignature uhs;
1620
1621 ub = (const struct UDPBroadcast *) buf;
1622 uhs.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST);
1623 uhs.purpose.size = htonl (sizeof (uhs));
1624 uhs.sender = ub->sender;
1625 GNUNET_CRYPTO_hash (&sa, salen, &uhs.h_address);
1626 if (GNUNET_OK ==
1627 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST,
1628 &uhs.purpose,
1629 &ub->sender_sig,
1630 &ub->sender.public_key))
1631 { 1600 {
1632 char *addr_s; 1601 const struct UDPBroadcast *ub;
1633 enum GNUNET_NetworkType nt; 1602 struct UdpBroadcastSignature uhs;
1634 1603
1635 addr_s = 1604 ub = (const struct UDPBroadcast *)buf;
1636 sockaddr_to_udpaddr_string ((const struct sockaddr *) &sa, salen); 1605 uhs.purpose.purpose = htonl(GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST);
1637 GNUNET_STATISTICS_update (stats, "# broadcasts received", 1, GNUNET_NO); 1606 uhs.purpose.size = htonl(sizeof(uhs));
1638 /* use our own mechanism to determine network type */ 1607 uhs.sender = ub->sender;
1639 nt = 1608 GNUNET_CRYPTO_hash(&sa, salen, &uhs.h_address);
1640 GNUNET_NT_scanner_get_type (is, (const struct sockaddr *) &sa, salen); 1609 if (GNUNET_OK ==
1641 GNUNET_TRANSPORT_application_validate (ah, &ub->sender, nt, addr_s); 1610 GNUNET_CRYPTO_eddsa_verify(GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST,
1642 GNUNET_free (addr_s); 1611 &uhs.purpose,
1643 return; 1612 &ub->sender_sig,
1613 &ub->sender.public_key))
1614 {
1615 char *addr_s;
1616 enum GNUNET_NetworkType nt;
1617
1618 addr_s =
1619 sockaddr_to_udpaddr_string((const struct sockaddr *)&sa, salen);
1620 GNUNET_STATISTICS_update(stats, "# broadcasts received", 1, GNUNET_NO);
1621 /* use our own mechanism to determine network type */
1622 nt =
1623 GNUNET_NT_scanner_get_type(is, (const struct sockaddr *)&sa, salen);
1624 GNUNET_TRANSPORT_application_validate(ah, &ub->sender, nt, addr_s);
1625 GNUNET_free(addr_s);
1626 return;
1627 }
1628 /* continue with KX, mostly for statistics... */
1644 } 1629 }
1645 /* continue with KX, mostly for statistics... */
1646 }
1647 1630
1648 1631
1649 /* finally, test if it is a KX */ 1632 /* finally, test if it is a KX */
1650 if (rcvd < sizeof (struct UDPConfirmation) + sizeof (struct InitialKX)) 1633 if (rcvd < sizeof(struct UDPConfirmation) + sizeof(struct InitialKX))
1651 { 1634 {
1652 GNUNET_STATISTICS_update (stats, 1635 GNUNET_STATISTICS_update(stats,
1653 "# messages dropped (no kid, too small for KX)", 1636 "# messages dropped (no kid, too small for KX)",
1654 1, 1637 1,
1655 GNUNET_NO); 1638 GNUNET_NO);
1656 return; 1639 return;
1657 } 1640 }
1658 1641
1659 { 1642 {
1660 const struct InitialKX *kx; 1643 const struct InitialKX *kx;
1661 struct SharedSecret *ss; 1644 struct SharedSecret *ss;
1662 char pbuf[rcvd - sizeof (struct InitialKX)]; 1645 char pbuf[rcvd - sizeof(struct InitialKX)];
1663 const struct UDPConfirmation *uc; 1646 const struct UDPConfirmation *uc;
1664 struct SenderAddress *sender; 1647 struct SenderAddress *sender;
1665 1648
1666 kx = (const struct InitialKX *) buf; 1649 kx = (const struct InitialKX *)buf;
1667 ss = setup_shared_secret_dec (&kx->ephemeral); 1650 ss = setup_shared_secret_dec(&kx->ephemeral);
1668 if (GNUNET_OK != try_decrypt (ss, 1651 if (GNUNET_OK != try_decrypt(ss,
1669 kx->gcm_tag, 1652 kx->gcm_tag,
1670 0, 1653 0,
1671 &buf[sizeof (*kx)], 1654 &buf[sizeof(*kx)],
1672 sizeof (pbuf), 1655 sizeof(pbuf),
1673 pbuf)) 1656 pbuf))
1674 { 1657 {
1675 GNUNET_free (ss); 1658 GNUNET_free(ss);
1676 GNUNET_STATISTICS_update ( 1659 GNUNET_STATISTICS_update(
1677 stats, 1660 stats,
1678 "# messages dropped (no kid, AEAD decryption failed)", 1661 "# messages dropped (no kid, AEAD decryption failed)",
1679 1, 1662 1,
1680 GNUNET_NO); 1663 GNUNET_NO);
1681 return; 1664 return;
1682 } 1665 }
1683 uc = (const struct UDPConfirmation *) pbuf; 1666 uc = (const struct UDPConfirmation *)pbuf;
1684 if (GNUNET_OK != verify_confirmation (&kx->ephemeral, uc)) 1667 if (GNUNET_OK != verify_confirmation(&kx->ephemeral, uc))
1685 { 1668 {
1686 GNUNET_break_op (0); 1669 GNUNET_break_op(0);
1687 GNUNET_free (ss); 1670 GNUNET_free(ss);
1688 GNUNET_STATISTICS_update (stats, 1671 GNUNET_STATISTICS_update(stats,
1689 "# messages dropped (sender signature invalid)", 1672 "# messages dropped (sender signature invalid)",
1690 1, 1673 1,
1691 GNUNET_NO); 1674 GNUNET_NO);
1692 return; 1675 return;
1693 } 1676 }
1694 calculate_cmac (ss); 1677 calculate_cmac(ss);
1695 sender = setup_sender (&uc->sender, (const struct sockaddr *) &sa, salen); 1678 sender = setup_sender(&uc->sender, (const struct sockaddr *)&sa, salen);
1696 ss->sender = sender; 1679 ss->sender = sender;
1697 GNUNET_CONTAINER_DLL_insert (sender->ss_head, sender->ss_tail, ss); 1680 GNUNET_CONTAINER_DLL_insert(sender->ss_head, sender->ss_tail, ss);
1698 sender->num_secrets++; 1681 sender->num_secrets++;
1699 GNUNET_STATISTICS_update (stats, "# Secrets active", 1, GNUNET_NO); 1682 GNUNET_STATISTICS_update(stats, "# Secrets active", 1, GNUNET_NO);
1700 GNUNET_STATISTICS_update (stats, 1683 GNUNET_STATISTICS_update(stats,
1701 "# messages decrypted without BOX", 1684 "# messages decrypted without BOX",
1702 1, 1685 1,
1703 GNUNET_NO); 1686 GNUNET_NO);
1704 try_handle_plaintext (sender, &uc[1], sizeof (pbuf) - sizeof (*uc)); 1687 try_handle_plaintext(sender, &uc[1], sizeof(pbuf) - sizeof(*uc));
1705 consider_ss_ack (ss); 1688 consider_ss_ack(ss);
1706 if (sender->num_secrets > MAX_SECRETS) 1689 if (sender->num_secrets > MAX_SECRETS)
1707 secret_destroy (sender->ss_tail); 1690 secret_destroy(sender->ss_tail);
1708 } 1691 }
1709} 1692}
1710 1693
@@ -1717,7 +1700,7 @@ sock_read (void *cls)
1717 * @return converted bindto specification 1700 * @return converted bindto specification
1718 */ 1701 */
1719static struct sockaddr * 1702static struct sockaddr *
1720udp_address_to_sockaddr (const char *bindto, socklen_t *sock_len) 1703udp_address_to_sockaddr(const char *bindto, socklen_t *sock_len)
1721{ 1704{
1722 struct sockaddr *in; 1705 struct sockaddr *in;
1723 unsigned int port; 1706 unsigned int port;
@@ -1725,88 +1708,88 @@ udp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
1725 char *colon; 1708 char *colon;
1726 char *cp; 1709 char *cp;
1727 1710
1728 if (1 == sscanf (bindto, "%u%1s", &port, dummy)) 1711 if (1 == sscanf(bindto, "%u%1s", &port, dummy))
1729 {
1730 /* interpreting value as just a PORT number */
1731 if (port > UINT16_MAX)
1732 {
1733 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1734 "BINDTO specification `%s' invalid: value too large for port\n",
1735 bindto);
1736 return NULL;
1737 }
1738 if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
1739 (GNUNET_YES ==
1740 GNUNET_CONFIGURATION_get_value_yesno (cfg,
1741 COMMUNICATOR_CONFIG_SECTION,
1742 "DISABLE_V6")))
1743 { 1712 {
1744 struct sockaddr_in *i4; 1713 /* interpreting value as just a PORT number */
1714 if (port > UINT16_MAX)
1715 {
1716 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1717 "BINDTO specification `%s' invalid: value too large for port\n",
1718 bindto);
1719 return NULL;
1720 }
1721 if ((GNUNET_NO == GNUNET_NETWORK_test_pf(PF_INET6)) ||
1722 (GNUNET_YES ==
1723 GNUNET_CONFIGURATION_get_value_yesno(cfg,
1724 COMMUNICATOR_CONFIG_SECTION,
1725 "DISABLE_V6")))
1726 {
1727 struct sockaddr_in *i4;
1745 1728
1746 i4 = GNUNET_malloc (sizeof (struct sockaddr_in)); 1729 i4 = GNUNET_malloc(sizeof(struct sockaddr_in));
1747 i4->sin_family = AF_INET; 1730 i4->sin_family = AF_INET;
1748 i4->sin_port = htons ((uint16_t) port); 1731 i4->sin_port = htons((uint16_t)port);
1749 *sock_len = sizeof (struct sockaddr_in); 1732 *sock_len = sizeof(struct sockaddr_in);
1750 in = (struct sockaddr *) i4; 1733 in = (struct sockaddr *)i4;
1751 } 1734 }
1752 else 1735 else
1753 { 1736 {
1754 struct sockaddr_in6 *i6; 1737 struct sockaddr_in6 *i6;
1755 1738
1756 i6 = GNUNET_malloc (sizeof (struct sockaddr_in6)); 1739 i6 = GNUNET_malloc(sizeof(struct sockaddr_in6));
1757 i6->sin6_family = AF_INET6; 1740 i6->sin6_family = AF_INET6;
1758 i6->sin6_port = htons ((uint16_t) port); 1741 i6->sin6_port = htons((uint16_t)port);
1759 *sock_len = sizeof (struct sockaddr_in6); 1742 *sock_len = sizeof(struct sockaddr_in6);
1760 in = (struct sockaddr *) i6; 1743 in = (struct sockaddr *)i6;
1744 }
1745 return in;
1761 } 1746 }
1762 return in; 1747 cp = GNUNET_strdup(bindto);
1763 } 1748 colon = strrchr(cp, ':');
1764 cp = GNUNET_strdup (bindto);
1765 colon = strrchr (cp, ':');
1766 if (NULL != colon) 1749 if (NULL != colon)
1767 {
1768 /* interpet value after colon as port */
1769 *colon = '\0';
1770 colon++;
1771 if (1 == sscanf (colon, "%u%1s", &port, dummy))
1772 { 1750 {
1773 /* interpreting value as just a PORT number */ 1751 /* interpet value after colon as port */
1774 if (port > UINT16_MAX) 1752 *colon = '\0';
1775 { 1753 colon++;
1776 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1754 if (1 == sscanf(colon, "%u%1s", &port, dummy))
1777 "BINDTO specification `%s' invalid: value too large for port\n", 1755 {
1778 bindto); 1756 /* interpreting value as just a PORT number */
1779 GNUNET_free (cp); 1757 if (port > UINT16_MAX)
1780 return NULL; 1758 {
1781 } 1759 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1760 "BINDTO specification `%s' invalid: value too large for port\n",
1761 bindto);
1762 GNUNET_free(cp);
1763 return NULL;
1764 }
1765 }
1766 else
1767 {
1768 GNUNET_log(
1769 GNUNET_ERROR_TYPE_ERROR,
1770 "BINDTO specification `%s' invalid: last ':' not followed by number\n",
1771 bindto);
1772 GNUNET_free(cp);
1773 return NULL;
1774 }
1782 } 1775 }
1783 else 1776 else
1784 { 1777 {
1785 GNUNET_log ( 1778 /* interpret missing port as 0, aka pick any free one */
1786 GNUNET_ERROR_TYPE_ERROR, 1779 port = 0;
1787 "BINDTO specification `%s' invalid: last ':' not followed by number\n",
1788 bindto);
1789 GNUNET_free (cp);
1790 return NULL;
1791 } 1780 }
1792 }
1793 else
1794 {
1795 /* interpret missing port as 0, aka pick any free one */
1796 port = 0;
1797 }
1798 { 1781 {
1799 /* try IPv4 */ 1782 /* try IPv4 */
1800 struct sockaddr_in v4; 1783 struct sockaddr_in v4;
1801 1784
1802 if (1 == inet_pton (AF_INET, cp, &v4)) 1785 if (1 == inet_pton(AF_INET, cp, &v4))
1803 { 1786 {
1804 v4.sin_port = htons ((uint16_t) port); 1787 v4.sin_port = htons((uint16_t)port);
1805 in = GNUNET_memdup (&v4, sizeof (v4)); 1788 in = GNUNET_memdup(&v4, sizeof(v4));
1806 *sock_len = sizeof (v4); 1789 *sock_len = sizeof(v4);
1807 GNUNET_free (cp); 1790 GNUNET_free(cp);
1808 return in; 1791 return in;
1809 } 1792 }
1810 } 1793 }
1811 { 1794 {
1812 /* try IPv6 */ 1795 /* try IPv6 */
@@ -1814,22 +1797,22 @@ udp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
1814 const char *start; 1797 const char *start;
1815 1798
1816 start = cp; 1799 start = cp;
1817 if (('[' == *cp) && (']' == cp[strlen (cp) - 1])) 1800 if (('[' == *cp) && (']' == cp[strlen(cp) - 1]))
1818 { 1801 {
1819 start++; /* skip over '[' */ 1802 start++; /* skip over '[' */
1820 cp[strlen (cp) - 1] = '\0'; /* eat ']' */ 1803 cp[strlen(cp) - 1] = '\0'; /* eat ']' */
1821 } 1804 }
1822 if (1 == inet_pton (AF_INET6, start, &v6)) 1805 if (1 == inet_pton(AF_INET6, start, &v6))
1823 { 1806 {
1824 v6.sin6_port = htons ((uint16_t) port); 1807 v6.sin6_port = htons((uint16_t)port);
1825 in = GNUNET_memdup (&v6, sizeof (v6)); 1808 in = GNUNET_memdup(&v6, sizeof(v6));
1826 *sock_len = sizeof (v6); 1809 *sock_len = sizeof(v6);
1827 GNUNET_free (cp); 1810 GNUNET_free(cp);
1828 return in; 1811 return in;
1829 } 1812 }
1830 } 1813 }
1831 /* #5528 FIXME (feature!): maybe also try getnameinfo()? */ 1814 /* #5528 FIXME (feature!): maybe also try getnameinfo()? */
1832 GNUNET_free (cp); 1815 GNUNET_free(cp);
1833 return NULL; 1816 return NULL;
1834} 1817}
1835 1818
@@ -1842,22 +1825,22 @@ udp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
1842 * @param pad_size number of bytes of padding to append 1825 * @param pad_size number of bytes of padding to append
1843 */ 1826 */
1844static void 1827static void
1845do_pad (gcry_cipher_hd_t out_cipher, char *dgram, size_t pad_size) 1828do_pad(gcry_cipher_hd_t out_cipher, char *dgram, size_t pad_size)
1846{ 1829{
1847 char pad[pad_size]; 1830 char pad[pad_size];
1848 1831
1849 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, pad, sizeof (pad)); 1832 GNUNET_CRYPTO_random_block(GNUNET_CRYPTO_QUALITY_WEAK, pad, sizeof(pad));
1850 if (sizeof (pad) > sizeof (struct GNUNET_MessageHeader)) 1833 if (sizeof(pad) > sizeof(struct GNUNET_MessageHeader))
1851 { 1834 {
1852 struct GNUNET_MessageHeader hdr = 1835 struct GNUNET_MessageHeader hdr =
1853 {.size = htons (sizeof (pad)), 1836 { .size = htons(sizeof(pad)),
1854 .type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_PAD)}; 1837 .type = htons(GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_PAD) };
1855 1838
1856 memcpy (pad, &hdr, sizeof (hdr)); 1839 memcpy(pad, &hdr, sizeof(hdr));
1857 } 1840 }
1858 GNUNET_assert ( 1841 GNUNET_assert(
1859 0 == 1842 0 ==
1860 gcry_cipher_encrypt (out_cipher, dgram, sizeof (pad), pad, sizeof (pad))); 1843 gcry_cipher_encrypt(out_cipher, dgram, sizeof(pad), pad, sizeof(pad)));
1861} 1844}
1862 1845
1863 1846
@@ -1870,123 +1853,123 @@ do_pad (gcry_cipher_hd_t out_cipher, char *dgram, size_t pad_size)
1870 * @param impl_state our `struct ReceiverAddress` 1853 * @param impl_state our `struct ReceiverAddress`
1871 */ 1854 */
1872static void 1855static void
1873mq_send (struct GNUNET_MQ_Handle *mq, 1856mq_send(struct GNUNET_MQ_Handle *mq,
1874 const struct GNUNET_MessageHeader *msg, 1857 const struct GNUNET_MessageHeader *msg,
1875 void *impl_state) 1858 void *impl_state)
1876{ 1859{
1877 struct ReceiverAddress *receiver = impl_state; 1860 struct ReceiverAddress *receiver = impl_state;
1878 uint16_t msize = ntohs (msg->size); 1861 uint16_t msize = ntohs(msg->size);
1879 1862
1880 GNUNET_assert (mq == receiver->mq); 1863 GNUNET_assert(mq == receiver->mq);
1881 if (msize > receiver->mtu) 1864 if (msize > receiver->mtu)
1882 { 1865 {
1883 GNUNET_break (0); 1866 GNUNET_break(0);
1884 receiver_destroy (receiver); 1867 receiver_destroy(receiver);
1885 return; 1868 return;
1886 } 1869 }
1887 reschedule_receiver_timeout (receiver); 1870 reschedule_receiver_timeout(receiver);
1888 1871
1889 if (0 == receiver->acks_available) 1872 if (0 == receiver->acks_available)
1890 { 1873 {
1891 /* use KX encryption method */ 1874 /* use KX encryption method */
1892 struct UdpHandshakeSignature uhs; 1875 struct UdpHandshakeSignature uhs;
1893 struct UDPConfirmation uc; 1876 struct UDPConfirmation uc;
1894 struct InitialKX kx; 1877 struct InitialKX kx;
1895 struct GNUNET_CRYPTO_EcdhePrivateKey epriv; 1878 struct GNUNET_CRYPTO_EcdhePrivateKey epriv;
1896 char dgram[receiver->mtu + sizeof (uc) + sizeof (kx)]; 1879 char dgram[receiver->mtu + sizeof(uc) + sizeof(kx)];
1897 size_t dpos; 1880 size_t dpos;
1898 gcry_cipher_hd_t out_cipher; 1881 gcry_cipher_hd_t out_cipher;
1899 struct SharedSecret *ss; 1882 struct SharedSecret *ss;
1900 1883
1901 /* setup key material */ 1884 /* setup key material */
1902 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecdhe_key_create2 (&epriv)); 1885 GNUNET_assert(GNUNET_OK == GNUNET_CRYPTO_ecdhe_key_create2(&epriv));
1903 1886
1904 ss = setup_shared_secret_enc (&epriv, receiver); 1887 ss = setup_shared_secret_enc(&epriv, receiver);
1905 setup_cipher (&ss->master, 0, &out_cipher); 1888 setup_cipher(&ss->master, 0, &out_cipher);
1906 /* compute 'uc' */ 1889 /* compute 'uc' */
1907 uc.sender = my_identity; 1890 uc.sender = my_identity;
1908 uc.monotonic_time = 1891 uc.monotonic_time =
1909 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_monotonic (cfg)); 1892 GNUNET_TIME_absolute_hton(GNUNET_TIME_absolute_get_monotonic(cfg));
1910 uhs.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE); 1893 uhs.purpose.purpose = htonl(GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE);
1911 uhs.purpose.size = htonl (sizeof (uhs)); 1894 uhs.purpose.size = htonl(sizeof(uhs));
1912 uhs.sender = my_identity; 1895 uhs.sender = my_identity;
1913 uhs.receiver = receiver->target; 1896 uhs.receiver = receiver->target;
1914 GNUNET_CRYPTO_ecdhe_key_get_public (&epriv, &uhs.ephemeral); 1897 GNUNET_CRYPTO_ecdhe_key_get_public(&epriv, &uhs.ephemeral);
1915 uhs.monotonic_time = uc.monotonic_time; 1898 uhs.monotonic_time = uc.monotonic_time;
1916 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (my_private_key, 1899 GNUNET_assert(GNUNET_OK == GNUNET_CRYPTO_eddsa_sign(my_private_key,
1917 &uhs.purpose, 1900 &uhs.purpose,
1918 &uc.sender_sig)); 1901 &uc.sender_sig));
1919 /* Leave space for kx */ 1902 /* Leave space for kx */
1920 dpos = sizeof (struct GNUNET_CRYPTO_EcdhePublicKey); 1903 dpos = sizeof(struct GNUNET_CRYPTO_EcdhePublicKey);
1921 /* Append encrypted uc to dgram */ 1904 /* Append encrypted uc to dgram */
1922 GNUNET_assert (0 == gcry_cipher_encrypt (out_cipher, 1905 GNUNET_assert(0 == gcry_cipher_encrypt(out_cipher,
1923 &dgram[dpos], 1906 &dgram[dpos],
1924 sizeof (uc), 1907 sizeof(uc),
1925 &uc, 1908 &uc,
1926 sizeof (uc))); 1909 sizeof(uc)));
1927 dpos += sizeof (uc); 1910 dpos += sizeof(uc);
1928 /* Append encrypted payload to dgram */ 1911 /* Append encrypted payload to dgram */
1929 GNUNET_assert ( 1912 GNUNET_assert(
1930 0 == gcry_cipher_encrypt (out_cipher, &dgram[dpos], msize, msg, msize)); 1913 0 == gcry_cipher_encrypt(out_cipher, &dgram[dpos], msize, msg, msize));
1931 dpos += msize; 1914 dpos += msize;
1932 do_pad (out_cipher, &dgram[dpos], sizeof (dgram) - dpos); 1915 do_pad(out_cipher, &dgram[dpos], sizeof(dgram) - dpos);
1933 /* Datagram starts with kx */ 1916 /* Datagram starts with kx */
1934 kx.ephemeral = uhs.ephemeral; 1917 kx.ephemeral = uhs.ephemeral;
1935 GNUNET_assert ( 1918 GNUNET_assert(
1936 0 == gcry_cipher_gettag (out_cipher, kx.gcm_tag, sizeof (kx.gcm_tag))); 1919 0 == gcry_cipher_gettag(out_cipher, kx.gcm_tag, sizeof(kx.gcm_tag)));
1937 gcry_cipher_close (out_cipher); 1920 gcry_cipher_close(out_cipher);
1938 memcpy (dgram, &kx, sizeof (kx)); 1921 memcpy(dgram, &kx, sizeof(kx));
1939 if (-1 == GNUNET_NETWORK_socket_sendto (udp_sock, 1922 if (-1 == GNUNET_NETWORK_socket_sendto(udp_sock,
1940 dgram, 1923 dgram,
1941 sizeof (dgram), 1924 sizeof(dgram),
1942 receiver->address, 1925 receiver->address,
1943 receiver->address_len)) 1926 receiver->address_len))
1944 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "send"); 1927 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "send");
1945 GNUNET_MQ_impl_send_continue (mq); 1928 GNUNET_MQ_impl_send_continue(mq);
1946 return; 1929 return;
1947 } /* End of KX encryption method */ 1930 } /* End of KX encryption method */
1948 1931
1949 /* begin "BOX" encryption method, scan for ACKs from tail! */ 1932 /* begin "BOX" encryption method, scan for ACKs from tail! */
1950 for (struct SharedSecret *ss = receiver->ss_tail; NULL != ss; ss = ss->prev) 1933 for (struct SharedSecret *ss = receiver->ss_tail; NULL != ss; ss = ss->prev)
1951 {
1952 if (ss->sequence_used < ss->sequence_allowed)
1953 { 1934 {
1954 char dgram[sizeof (struct UDPBox) + receiver->mtu]; 1935 if (ss->sequence_used < ss->sequence_allowed)
1955 struct UDPBox *box; 1936 {
1956 gcry_cipher_hd_t out_cipher; 1937 char dgram[sizeof(struct UDPBox) + receiver->mtu];
1957 size_t dpos; 1938 struct UDPBox *box;
1958 1939 gcry_cipher_hd_t out_cipher;
1959 box = (struct UDPBox *) dgram; 1940 size_t dpos;
1960 ss->sequence_used++; 1941
1961 get_kid (&ss->master, ss->sequence_used, &box->kid); 1942 box = (struct UDPBox *)dgram;
1962 setup_cipher (&ss->master, ss->sequence_used, &out_cipher); 1943 ss->sequence_used++;
1963 /* Append encrypted payload to dgram */ 1944 get_kid(&ss->master, ss->sequence_used, &box->kid);
1964 dpos = sizeof (struct UDPBox); 1945 setup_cipher(&ss->master, ss->sequence_used, &out_cipher);
1965 GNUNET_assert ( 1946 /* Append encrypted payload to dgram */
1966 0 == gcry_cipher_encrypt (out_cipher, &dgram[dpos], msize, msg, msize)); 1947 dpos = sizeof(struct UDPBox);
1967 dpos += msize; 1948 GNUNET_assert(
1968 do_pad (out_cipher, &dgram[dpos], sizeof (dgram) - dpos); 1949 0 == gcry_cipher_encrypt(out_cipher, &dgram[dpos], msize, msg, msize));
1969 GNUNET_assert (0 == gcry_cipher_gettag (out_cipher, 1950 dpos += msize;
1970 box->gcm_tag, 1951 do_pad(out_cipher, &dgram[dpos], sizeof(dgram) - dpos);
1971 sizeof (box->gcm_tag))); 1952 GNUNET_assert(0 == gcry_cipher_gettag(out_cipher,
1972 gcry_cipher_close (out_cipher); 1953 box->gcm_tag,
1973 if (-1 == GNUNET_NETWORK_socket_sendto (udp_sock, 1954 sizeof(box->gcm_tag)));
1974 dgram, 1955 gcry_cipher_close(out_cipher);
1975 sizeof (dgram), 1956 if (-1 == GNUNET_NETWORK_socket_sendto(udp_sock,
1976 receiver->address, 1957 dgram,
1977 receiver->address_len)) 1958 sizeof(dgram),
1978 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "send"); 1959 receiver->address,
1979 GNUNET_MQ_impl_send_continue (mq); 1960 receiver->address_len))
1980 receiver->acks_available--; 1961 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "send");
1981 if (0 == receiver->acks_available) 1962 GNUNET_MQ_impl_send_continue(mq);
1982 { 1963 receiver->acks_available--;
1983 /* We have no more ACKs => MTU change! */ 1964 if (0 == receiver->acks_available)
1984 setup_receiver_mq (receiver); 1965 {
1985 } 1966 /* We have no more ACKs => MTU change! */
1986 return; 1967 setup_receiver_mq(receiver);
1968 }
1969 return;
1970 }
1987 } 1971 }
1988 } 1972 GNUNET_assert(0);
1989 GNUNET_assert (0);
1990} 1973}
1991 1974
1992 1975
@@ -1999,15 +1982,15 @@ mq_send (struct GNUNET_MQ_Handle *mq,
1999 * @param impl_state our `struct ReceiverAddress` 1982 * @param impl_state our `struct ReceiverAddress`
2000 */ 1983 */
2001static void 1984static void
2002mq_destroy (struct GNUNET_MQ_Handle *mq, void *impl_state) 1985mq_destroy(struct GNUNET_MQ_Handle *mq, void *impl_state)
2003{ 1986{
2004 struct ReceiverAddress *receiver = impl_state; 1987 struct ReceiverAddress *receiver = impl_state;
2005 1988
2006 if (mq == receiver->mq) 1989 if (mq == receiver->mq)
2007 { 1990 {
2008 receiver->mq = NULL; 1991 receiver->mq = NULL;
2009 receiver_destroy (receiver); 1992 receiver_destroy(receiver);
2010 } 1993 }
2011} 1994}
2012 1995
2013 1996
@@ -2018,10 +2001,10 @@ mq_destroy (struct GNUNET_MQ_Handle *mq, void *impl_state)
2018 * @param impl_state our `struct RecvierAddress` 2001 * @param impl_state our `struct RecvierAddress`
2019 */ 2002 */
2020static void 2003static void
2021mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state) 2004mq_cancel(struct GNUNET_MQ_Handle *mq, void *impl_state)
2022{ 2005{
2023 /* Cancellation is impossible with UDP; bail */ 2006 /* Cancellation is impossible with UDP; bail */
2024 GNUNET_assert (0); 2007 GNUNET_assert(0);
2025} 2008}
2026 2009
2027 2010
@@ -2035,15 +2018,15 @@ mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
2035 * @param error error code 2018 * @param error error code
2036 */ 2019 */
2037static void 2020static void
2038mq_error (void *cls, enum GNUNET_MQ_Error error) 2021mq_error(void *cls, enum GNUNET_MQ_Error error)
2039{ 2022{
2040 struct ReceiverAddress *receiver = cls; 2023 struct ReceiverAddress *receiver = cls;
2041 2024
2042 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 2025 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2043 "MQ error in queue to %s: %d\n", 2026 "MQ error in queue to %s: %d\n",
2044 GNUNET_i2s (&receiver->target), 2027 GNUNET_i2s(&receiver->target),
2045 (int) error); 2028 (int)error);
2046 receiver_destroy (receiver); 2029 receiver_destroy(receiver);
2047} 2030}
2048 2031
2049 2032
@@ -2055,61 +2038,63 @@ mq_error (void *cls, enum GNUNET_MQ_Error error)
2055 * @param receiver receiver to setup MQ for 2038 * @param receiver receiver to setup MQ for
2056 */ 2039 */
2057static void 2040static void
2058setup_receiver_mq (struct ReceiverAddress *receiver) 2041setup_receiver_mq(struct ReceiverAddress *receiver)
2059{ 2042{
2060 size_t base_mtu; 2043 size_t base_mtu;
2061 2044
2062 if (NULL != receiver->qh) 2045 if (NULL != receiver->qh)
2063 { 2046 {
2064 GNUNET_TRANSPORT_communicator_mq_del (receiver->qh); 2047 GNUNET_TRANSPORT_communicator_mq_del(receiver->qh);
2065 receiver->qh = NULL; 2048 receiver->qh = NULL;
2066 } 2049 }
2067 GNUNET_assert (NULL == receiver->mq); 2050 GNUNET_assert(NULL == receiver->mq);
2068 switch (receiver->address->sa_family) 2051 switch (receiver->address->sa_family)
2069 { 2052 {
2070 case AF_INET: 2053 case AF_INET:
2071 base_mtu = 1480 /* Ethernet MTU, 1500 - Ethernet header - VLAN tag */ 2054 base_mtu = 1480 /* Ethernet MTU, 1500 - Ethernet header - VLAN tag */
2072 - sizeof (struct GNUNET_TUN_IPv4Header) /* 20 */ 2055 - sizeof(struct GNUNET_TUN_IPv4Header) /* 20 */
2073 - sizeof (struct GNUNET_TUN_UdpHeader) /* 8 */; 2056 - sizeof(struct GNUNET_TUN_UdpHeader) /* 8 */;
2074 break; 2057 break;
2075 case AF_INET6: 2058
2076 base_mtu = 1280 /* Minimum MTU required by IPv6 */ 2059 case AF_INET6:
2077 - sizeof (struct GNUNET_TUN_IPv6Header) /* 40 */ 2060 base_mtu = 1280 /* Minimum MTU required by IPv6 */
2078 - sizeof (struct GNUNET_TUN_UdpHeader) /* 8 */; 2061 - sizeof(struct GNUNET_TUN_IPv6Header) /* 40 */
2079 break; 2062 - sizeof(struct GNUNET_TUN_UdpHeader) /* 8 */;
2080 default: 2063 break;
2081 GNUNET_assert (0); 2064
2082 break; 2065 default:
2083 } 2066 GNUNET_assert(0);
2067 break;
2068 }
2084 if (0 == receiver->acks_available) 2069 if (0 == receiver->acks_available)
2085 { 2070 {
2086 /* MTU based on full KX messages */ 2071 /* MTU based on full KX messages */
2087 receiver->mtu = base_mtu - sizeof (struct InitialKX) /* 48 */ 2072 receiver->mtu = base_mtu - sizeof(struct InitialKX) /* 48 */
2088 - sizeof (struct UDPConfirmation); /* 104 */ 2073 - sizeof(struct UDPConfirmation); /* 104 */
2089 } 2074 }
2090 else 2075 else
2091 { 2076 {
2092 /* MTU based on BOXed messages */ 2077 /* MTU based on BOXed messages */
2093 receiver->mtu = base_mtu - sizeof (struct UDPBox); 2078 receiver->mtu = base_mtu - sizeof(struct UDPBox);
2094 } 2079 }
2095 /* => Effective MTU for CORE will range from 1080 (IPv6 + KX) to 2080 /* => Effective MTU for CORE will range from 1080 (IPv6 + KX) to
2096 1404 (IPv4 + Box) bytes, depending on circumstances... */ 2081 1404 (IPv4 + Box) bytes, depending on circumstances... */
2097 if (NULL == receiver->mq) 2082 if (NULL == receiver->mq)
2098 receiver->mq = GNUNET_MQ_queue_for_callbacks (&mq_send, 2083 receiver->mq = GNUNET_MQ_queue_for_callbacks(&mq_send,
2099 &mq_destroy, 2084 &mq_destroy,
2100 &mq_cancel, 2085 &mq_cancel,
2101 receiver, 2086 receiver,
2102 NULL, 2087 NULL,
2103 &mq_error, 2088 &mq_error,
2104 receiver); 2089 receiver);
2105 receiver->qh = 2090 receiver->qh =
2106 GNUNET_TRANSPORT_communicator_mq_add (ch, 2091 GNUNET_TRANSPORT_communicator_mq_add(ch,
2107 &receiver->target, 2092 &receiver->target,
2108 receiver->foreign_addr, 2093 receiver->foreign_addr,
2109 receiver->mtu, 2094 receiver->mtu,
2110 receiver->nt, 2095 receiver->nt,
2111 GNUNET_TRANSPORT_CS_OUTBOUND, 2096 GNUNET_TRANSPORT_CS_OUTBOUND,
2112 receiver->mq); 2097 receiver->mq);
2113} 2098}
2114 2099
2115 2100
@@ -2132,47 +2117,47 @@ setup_receiver_mq (struct ReceiverAddress *receiver)
2132 * invalid 2117 * invalid
2133 */ 2118 */
2134static int 2119static int
2135mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address) 2120mq_init(void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
2136{ 2121{
2137 struct ReceiverAddress *receiver; 2122 struct ReceiverAddress *receiver;
2138 const char *path; 2123 const char *path;
2139 struct sockaddr *in; 2124 struct sockaddr *in;
2140 socklen_t in_len; 2125 socklen_t in_len;
2141 2126
2142 if (0 != strncmp (address, 2127 if (0 != strncmp(address,
2143 COMMUNICATOR_ADDRESS_PREFIX "-", 2128 COMMUNICATOR_ADDRESS_PREFIX "-",
2144 strlen (COMMUNICATOR_ADDRESS_PREFIX "-"))) 2129 strlen(COMMUNICATOR_ADDRESS_PREFIX "-")))
2145 { 2130 {
2146 GNUNET_break_op (0); 2131 GNUNET_break_op(0);
2147 return GNUNET_SYSERR; 2132 return GNUNET_SYSERR;
2148 } 2133 }
2149 path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")]; 2134 path = &address[strlen(COMMUNICATOR_ADDRESS_PREFIX "-")];
2150 in = udp_address_to_sockaddr (path, &in_len); 2135 in = udp_address_to_sockaddr(path, &in_len);
2151 2136
2152 receiver = GNUNET_new (struct ReceiverAddress); 2137 receiver = GNUNET_new(struct ReceiverAddress);
2153 receiver->address = in; 2138 receiver->address = in;
2154 receiver->address_len = in_len; 2139 receiver->address_len = in_len;
2155 receiver->target = *peer; 2140 receiver->target = *peer;
2156 receiver->nt = GNUNET_NT_scanner_get_type (is, in, in_len); 2141 receiver->nt = GNUNET_NT_scanner_get_type(is, in, in_len);
2157 (void) GNUNET_CONTAINER_multipeermap_put ( 2142 (void)GNUNET_CONTAINER_multipeermap_put(
2158 receivers, 2143 receivers,
2159 &receiver->target, 2144 &receiver->target,
2160 receiver, 2145 receiver,
2161 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); 2146 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2162 receiver->timeout = 2147 receiver->timeout =
2163 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT); 2148 GNUNET_TIME_relative_to_absolute(GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2164 receiver->hn = GNUNET_CONTAINER_heap_insert (receivers_heap, 2149 receiver->hn = GNUNET_CONTAINER_heap_insert(receivers_heap,
2165 receiver, 2150 receiver,
2166 receiver->timeout.abs_value_us); 2151 receiver->timeout.abs_value_us);
2167 GNUNET_STATISTICS_set (stats, 2152 GNUNET_STATISTICS_set(stats,
2168 "# receivers active", 2153 "# receivers active",
2169 GNUNET_CONTAINER_multipeermap_size (receivers), 2154 GNUNET_CONTAINER_multipeermap_size(receivers),
2170 GNUNET_NO); 2155 GNUNET_NO);
2171 receiver->foreign_addr = 2156 receiver->foreign_addr =
2172 sockaddr_to_udpaddr_string (receiver->address, receiver->address_len); 2157 sockaddr_to_udpaddr_string(receiver->address, receiver->address_len);
2173 setup_receiver_mq (receiver); 2158 setup_receiver_mq(receiver);
2174 if (NULL == timeout_task) 2159 if (NULL == timeout_task)
2175 timeout_task = GNUNET_SCHEDULER_add_now (&check_timeouts, NULL); 2160 timeout_task = GNUNET_SCHEDULER_add_now(&check_timeouts, NULL);
2176 return GNUNET_OK; 2161 return GNUNET_OK;
2177} 2162}
2178 2163
@@ -2186,15 +2171,15 @@ mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
2186 * @return #GNUNET_OK to continue to iterate 2171 * @return #GNUNET_OK to continue to iterate
2187 */ 2172 */
2188static int 2173static int
2189get_receiver_delete_it (void *cls, 2174get_receiver_delete_it(void *cls,
2190 const struct GNUNET_PeerIdentity *target, 2175 const struct GNUNET_PeerIdentity *target,
2191 void *value) 2176 void *value)
2192{ 2177{
2193 struct ReceiverAddress *receiver = value; 2178 struct ReceiverAddress *receiver = value;
2194 2179
2195 (void) cls; 2180 (void)cls;
2196 (void) target; 2181 (void)target;
2197 receiver_destroy (receiver); 2182 receiver_destroy(receiver);
2198 return GNUNET_OK; 2183 return GNUNET_OK;
2199} 2184}
2200 2185
@@ -2208,15 +2193,15 @@ get_receiver_delete_it (void *cls,
2208 * @return #GNUNET_OK to continue to iterate 2193 * @return #GNUNET_OK to continue to iterate
2209 */ 2194 */
2210static int 2195static int
2211get_sender_delete_it (void *cls, 2196get_sender_delete_it(void *cls,
2212 const struct GNUNET_PeerIdentity *target, 2197 const struct GNUNET_PeerIdentity *target,
2213 void *value) 2198 void *value)
2214{ 2199{
2215 struct SenderAddress *sender = value; 2200 struct SenderAddress *sender = value;
2216 2201
2217 (void) cls; 2202 (void)cls;
2218 (void) target; 2203 (void)target;
2219 sender_destroy (sender); 2204 sender_destroy(sender);
2220 return GNUNET_OK; 2205 return GNUNET_OK;
2221} 2206}
2222 2207
@@ -2227,64 +2212,64 @@ get_sender_delete_it (void *cls,
2227 * @param cls NULL (always) 2212 * @param cls NULL (always)
2228 */ 2213 */
2229static void 2214static void
2230do_shutdown (void *cls) 2215do_shutdown(void *cls)
2231{ 2216{
2232 if (NULL != nat) 2217 if (NULL != nat)
2233 { 2218 {
2234 GNUNET_NAT_unregister (nat); 2219 GNUNET_NAT_unregister(nat);
2235 nat = NULL; 2220 nat = NULL;
2236 } 2221 }
2237 while (NULL != bi_head) 2222 while (NULL != bi_head)
2238 bi_destroy (bi_head); 2223 bi_destroy(bi_head);
2239 if (NULL != broadcast_task) 2224 if (NULL != broadcast_task)
2240 { 2225 {
2241 GNUNET_SCHEDULER_cancel (broadcast_task); 2226 GNUNET_SCHEDULER_cancel(broadcast_task);
2242 broadcast_task = NULL; 2227 broadcast_task = NULL;
2243 } 2228 }
2244 if (NULL != read_task) 2229 if (NULL != read_task)
2245 { 2230 {
2246 GNUNET_SCHEDULER_cancel (read_task); 2231 GNUNET_SCHEDULER_cancel(read_task);
2247 read_task = NULL; 2232 read_task = NULL;
2248 } 2233 }
2249 if (NULL != udp_sock) 2234 if (NULL != udp_sock)
2250 { 2235 {
2251 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (udp_sock)); 2236 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close(udp_sock));
2252 udp_sock = NULL; 2237 udp_sock = NULL;
2253 } 2238 }
2254 GNUNET_CONTAINER_multipeermap_iterate (receivers, 2239 GNUNET_CONTAINER_multipeermap_iterate(receivers,
2255 &get_receiver_delete_it, 2240 &get_receiver_delete_it,
2256 NULL); 2241 NULL);
2257 GNUNET_CONTAINER_multipeermap_destroy (receivers); 2242 GNUNET_CONTAINER_multipeermap_destroy(receivers);
2258 GNUNET_CONTAINER_multipeermap_iterate (senders, &get_sender_delete_it, NULL); 2243 GNUNET_CONTAINER_multipeermap_iterate(senders, &get_sender_delete_it, NULL);
2259 GNUNET_CONTAINER_multipeermap_destroy (senders); 2244 GNUNET_CONTAINER_multipeermap_destroy(senders);
2260 GNUNET_CONTAINER_multishortmap_destroy (key_cache); 2245 GNUNET_CONTAINER_multishortmap_destroy(key_cache);
2261 GNUNET_CONTAINER_heap_destroy (senders_heap); 2246 GNUNET_CONTAINER_heap_destroy(senders_heap);
2262 GNUNET_CONTAINER_heap_destroy (receivers_heap); 2247 GNUNET_CONTAINER_heap_destroy(receivers_heap);
2263 if (NULL != ch) 2248 if (NULL != ch)
2264 { 2249 {
2265 GNUNET_TRANSPORT_communicator_disconnect (ch); 2250 GNUNET_TRANSPORT_communicator_disconnect(ch);
2266 ch = NULL; 2251 ch = NULL;
2267 } 2252 }
2268 if (NULL != ah) 2253 if (NULL != ah)
2269 { 2254 {
2270 GNUNET_TRANSPORT_application_done (ah); 2255 GNUNET_TRANSPORT_application_done(ah);
2271 ah = NULL; 2256 ah = NULL;
2272 } 2257 }
2273 if (NULL != stats) 2258 if (NULL != stats)
2274 { 2259 {
2275 GNUNET_STATISTICS_destroy (stats, GNUNET_NO); 2260 GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
2276 stats = NULL; 2261 stats = NULL;
2277 } 2262 }
2278 if (NULL != my_private_key) 2263 if (NULL != my_private_key)
2279 { 2264 {
2280 GNUNET_free (my_private_key); 2265 GNUNET_free(my_private_key);
2281 my_private_key = NULL; 2266 my_private_key = NULL;
2282 } 2267 }
2283 if (NULL != is) 2268 if (NULL != is)
2284 { 2269 {
2285 GNUNET_NT_scanner_done (is); 2270 GNUNET_NT_scanner_done(is);
2286 is = NULL; 2271 is = NULL;
2287 } 2272 }
2288} 2273}
2289 2274
2290 2275
@@ -2298,24 +2283,24 @@ do_shutdown (void *cls)
2298 * @param msg payload 2283 * @param msg payload
2299 */ 2284 */
2300static void 2285static void
2301enc_notify_cb (void *cls, 2286enc_notify_cb(void *cls,
2302 const struct GNUNET_PeerIdentity *sender, 2287 const struct GNUNET_PeerIdentity *sender,
2303 const struct GNUNET_MessageHeader *msg) 2288 const struct GNUNET_MessageHeader *msg)
2304{ 2289{
2305 const struct UDPAck *ack; 2290 const struct UDPAck *ack;
2306 2291
2307 (void) cls; 2292 (void)cls;
2308 if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK) || 2293 if ((ntohs(msg->type) != GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK) ||
2309 (ntohs (msg->size) != sizeof (struct UDPAck))) 2294 (ntohs(msg->size) != sizeof(struct UDPAck)))
2310 { 2295 {
2311 GNUNET_break_op (0); 2296 GNUNET_break_op(0);
2312 return; 2297 return;
2313 } 2298 }
2314 ack = (const struct UDPAck *) msg; 2299 ack = (const struct UDPAck *)msg;
2315 GNUNET_CONTAINER_multipeermap_get_multiple (receivers, 2300 GNUNET_CONTAINER_multipeermap_get_multiple(receivers,
2316 sender, 2301 sender,
2317 &handle_ack, 2302 &handle_ack,
2318 (void *) ack); 2303 (void *)ack);
2319} 2304}
2320 2305
2321 2306
@@ -2333,39 +2318,39 @@ enc_notify_cb (void *cls,
2333 * @param addrlen actual length of the @a addr 2318 * @param addrlen actual length of the @a addr
2334 */ 2319 */
2335static void 2320static void
2336nat_address_cb (void *cls, 2321nat_address_cb(void *cls,
2337 void **app_ctx, 2322 void **app_ctx,
2338 int add_remove, 2323 int add_remove,
2339 enum GNUNET_NAT_AddressClass ac, 2324 enum GNUNET_NAT_AddressClass ac,
2340 const struct sockaddr *addr, 2325 const struct sockaddr *addr,
2341 socklen_t addrlen) 2326 socklen_t addrlen)
2342{ 2327{
2343 char *my_addr; 2328 char *my_addr;
2344 struct GNUNET_TRANSPORT_AddressIdentifier *ai; 2329 struct GNUNET_TRANSPORT_AddressIdentifier *ai;
2345 2330
2346 if (GNUNET_YES == add_remove) 2331 if (GNUNET_YES == add_remove)
2347 { 2332 {
2348 enum GNUNET_NetworkType nt; 2333 enum GNUNET_NetworkType nt;
2349 2334
2350 GNUNET_asprintf (&my_addr, 2335 GNUNET_asprintf(&my_addr,
2351 "%s-%s", 2336 "%s-%s",
2352 COMMUNICATOR_ADDRESS_PREFIX, 2337 COMMUNICATOR_ADDRESS_PREFIX,
2353 GNUNET_a2s (addr, addrlen)); 2338 GNUNET_a2s(addr, addrlen));
2354 nt = GNUNET_NT_scanner_get_type (is, addr, addrlen); 2339 nt = GNUNET_NT_scanner_get_type(is, addr, addrlen);
2355 ai = 2340 ai =
2356 GNUNET_TRANSPORT_communicator_address_add (ch, 2341 GNUNET_TRANSPORT_communicator_address_add(ch,
2357 my_addr, 2342 my_addr,
2358 nt, 2343 nt,
2359 GNUNET_TIME_UNIT_FOREVER_REL); 2344 GNUNET_TIME_UNIT_FOREVER_REL);
2360 GNUNET_free (my_addr); 2345 GNUNET_free(my_addr);
2361 *app_ctx = ai; 2346 *app_ctx = ai;
2362 } 2347 }
2363 else 2348 else
2364 { 2349 {
2365 ai = *app_ctx; 2350 ai = *app_ctx;
2366 GNUNET_TRANSPORT_communicator_address_remove (ai); 2351 GNUNET_TRANSPORT_communicator_address_remove(ai);
2367 *app_ctx = NULL; 2352 *app_ctx = NULL;
2368 } 2353 }
2369} 2354}
2370 2355
2371 2356
@@ -2375,67 +2360,69 @@ nat_address_cb (void *cls,
2375 * @param cls a `struct BroadcastInterface` 2360 * @param cls a `struct BroadcastInterface`
2376 */ 2361 */
2377static void 2362static void
2378ifc_broadcast (void *cls) 2363ifc_broadcast(void *cls)
2379{ 2364{
2380 struct BroadcastInterface *bi = cls; 2365 struct BroadcastInterface *bi = cls;
2381 struct GNUNET_TIME_Relative delay; 2366 struct GNUNET_TIME_Relative delay;
2382 2367
2383 delay = BROADCAST_FREQUENCY; 2368 delay = BROADCAST_FREQUENCY;
2384 delay.rel_value_us = 2369 delay.rel_value_us =
2385 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, delay.rel_value_us); 2370 GNUNET_CRYPTO_random_u64(GNUNET_CRYPTO_QUALITY_WEAK, delay.rel_value_us);
2386 bi->broadcast_task = 2371 bi->broadcast_task =
2387 GNUNET_SCHEDULER_add_delayed (INTERFACE_SCAN_FREQUENCY, &ifc_broadcast, bi); 2372 GNUNET_SCHEDULER_add_delayed(INTERFACE_SCAN_FREQUENCY, &ifc_broadcast, bi);
2388 2373
2389 switch (bi->sa->sa_family) 2374 switch (bi->sa->sa_family)
2390 { 2375 {
2391 case AF_INET: { 2376 case AF_INET: {
2392 static int yes = 1; 2377 static int yes = 1;
2393 static int no = 0; 2378 static int no = 0;
2394 ssize_t sent; 2379 ssize_t sent;
2395 2380
2396 if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt (udp_sock, 2381 if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt(udp_sock,
2397 SOL_SOCKET, 2382 SOL_SOCKET,
2398 SO_BROADCAST, 2383 SO_BROADCAST,
2399 &yes, 2384 &yes,
2400 sizeof (int))) 2385 sizeof(int)))
2401 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 2386 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "setsockopt");
2402 sent = GNUNET_NETWORK_socket_sendto (udp_sock, 2387 sent = GNUNET_NETWORK_socket_sendto(udp_sock,
2403 &bi->bcm, 2388 &bi->bcm,
2404 sizeof (bi->bcm), 2389 sizeof(bi->bcm),
2405 bi->ba, 2390 bi->ba,
2406 bi->salen); 2391 bi->salen);
2407 if (-1 == sent) 2392 if (-1 == sent)
2408 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto"); 2393 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "sendto");
2409 if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt (udp_sock, 2394 if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt(udp_sock,
2410 SOL_SOCKET, 2395 SOL_SOCKET,
2411 SO_BROADCAST, 2396 SO_BROADCAST,
2412 &no, 2397 &no,
2413 sizeof (int))) 2398 sizeof(int)))
2414 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 2399 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "setsockopt");
2415 break; 2400 break;
2416 } 2401 }
2417 case AF_INET6: { 2402
2418 ssize_t sent; 2403 case AF_INET6: {
2419 struct sockaddr_in6 dst; 2404 ssize_t sent;
2420 2405 struct sockaddr_in6 dst;
2421 dst.sin6_family = AF_INET6; 2406
2422 dst.sin6_port = htons (my_port); 2407 dst.sin6_family = AF_INET6;
2423 dst.sin6_addr = bi->mcreq.ipv6mr_multiaddr; 2408 dst.sin6_port = htons(my_port);
2424 dst.sin6_scope_id = ((struct sockaddr_in6 *) bi->ba)->sin6_scope_id; 2409 dst.sin6_addr = bi->mcreq.ipv6mr_multiaddr;
2425 2410 dst.sin6_scope_id = ((struct sockaddr_in6 *)bi->ba)->sin6_scope_id;
2426 sent = GNUNET_NETWORK_socket_sendto (udp_sock, 2411
2427 &bi->bcm, 2412 sent = GNUNET_NETWORK_socket_sendto(udp_sock,
2428 sizeof (bi->bcm), 2413 &bi->bcm,
2429 (const struct sockaddr *) &dst, 2414 sizeof(bi->bcm),
2430 sizeof (dst)); 2415 (const struct sockaddr *)&dst,
2431 if (-1 == sent) 2416 sizeof(dst));
2432 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto"); 2417 if (-1 == sent)
2433 break; 2418 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "sendto");
2434 } 2419 break;
2435 default: 2420 }
2436 GNUNET_break (0); 2421
2437 break; 2422 default:
2438 } 2423 GNUNET_break(0);
2424 break;
2425 }
2439} 2426}
2440 2427
2441 2428
@@ -2454,90 +2441,90 @@ ifc_broadcast (void *cls)
2454 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort 2441 * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
2455 */ 2442 */
2456static int 2443static int
2457iface_proc (void *cls, 2444iface_proc(void *cls,
2458 const char *name, 2445 const char *name,
2459 int isDefault, 2446 int isDefault,
2460 const struct sockaddr *addr, 2447 const struct sockaddr *addr,
2461 const struct sockaddr *broadcast_addr, 2448 const struct sockaddr *broadcast_addr,
2462 const struct sockaddr *netmask, 2449 const struct sockaddr *netmask,
2463 socklen_t addrlen) 2450 socklen_t addrlen)
2464{ 2451{
2465 struct BroadcastInterface *bi; 2452 struct BroadcastInterface *bi;
2466 enum GNUNET_NetworkType network; 2453 enum GNUNET_NetworkType network;
2467 struct UdpBroadcastSignature ubs; 2454 struct UdpBroadcastSignature ubs;
2468 2455
2469 (void) cls; 2456 (void)cls;
2470 (void) netmask; 2457 (void)netmask;
2471 if (NULL == addr) 2458 if (NULL == addr)
2472 return GNUNET_YES; /* need to know our address! */ 2459 return GNUNET_YES; /* need to know our address! */
2473 network = GNUNET_NT_scanner_get_type (is, addr, addrlen); 2460 network = GNUNET_NT_scanner_get_type(is, addr, addrlen);
2474 if (GNUNET_NT_LOOPBACK == network) 2461 if (GNUNET_NT_LOOPBACK == network)
2475 { 2462 {
2476 /* Broadcasting on loopback does not make sense */ 2463 /* Broadcasting on loopback does not make sense */
2477 return GNUNET_YES; 2464 return GNUNET_YES;
2478 } 2465 }
2479 for (bi = bi_head; NULL != bi; bi = bi->next) 2466 for (bi = bi_head; NULL != bi; bi = bi->next)
2480 {
2481 if ((bi->salen == addrlen) && (0 == memcmp (addr, bi->sa, addrlen)))
2482 { 2467 {
2483 bi->found = GNUNET_YES; 2468 if ((bi->salen == addrlen) && (0 == memcmp(addr, bi->sa, addrlen)))
2484 return GNUNET_OK; 2469 {
2470 bi->found = GNUNET_YES;
2471 return GNUNET_OK;
2472 }
2485 } 2473 }
2486 }
2487 2474
2488 if ((AF_INET6 == addr->sa_family) && (NULL == broadcast_addr)) 2475 if ((AF_INET6 == addr->sa_family) && (NULL == broadcast_addr))
2489 return GNUNET_OK; /* broadcast_addr is required for IPv6! */ 2476 return GNUNET_OK; /* broadcast_addr is required for IPv6! */
2490 if ((AF_INET6 == addr->sa_family) && (GNUNET_YES != have_v6_socket)) 2477 if ((AF_INET6 == addr->sa_family) && (GNUNET_YES != have_v6_socket))
2491 return GNUNET_OK; /* not using IPv6 */ 2478 return GNUNET_OK; /* not using IPv6 */
2492 2479
2493 bi = GNUNET_new (struct BroadcastInterface); 2480 bi = GNUNET_new(struct BroadcastInterface);
2494 bi->sa = GNUNET_memdup (addr, addrlen); 2481 bi->sa = GNUNET_memdup(addr, addrlen);
2495 if (NULL != broadcast_addr) 2482 if (NULL != broadcast_addr)
2496 bi->ba = GNUNET_memdup (broadcast_addr, addrlen); 2483 bi->ba = GNUNET_memdup(broadcast_addr, addrlen);
2497 bi->salen = addrlen; 2484 bi->salen = addrlen;
2498 bi->found = GNUNET_YES; 2485 bi->found = GNUNET_YES;
2499 bi->bcm.sender = my_identity; 2486 bi->bcm.sender = my_identity;
2500 ubs.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST); 2487 ubs.purpose.purpose = htonl(GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST);
2501 ubs.purpose.size = htonl (sizeof (ubs)); 2488 ubs.purpose.size = htonl(sizeof(ubs));
2502 ubs.sender = my_identity; 2489 ubs.sender = my_identity;
2503 GNUNET_CRYPTO_hash (addr, addrlen, &ubs.h_address); 2490 GNUNET_CRYPTO_hash(addr, addrlen, &ubs.h_address);
2504 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_sign (my_private_key, 2491 GNUNET_assert(GNUNET_OK == GNUNET_CRYPTO_eddsa_sign(my_private_key,
2505 &ubs.purpose, 2492 &ubs.purpose,
2506 &bi->bcm.sender_sig)); 2493 &bi->bcm.sender_sig));
2507 bi->broadcast_task = GNUNET_SCHEDULER_add_now (&ifc_broadcast, bi); 2494 bi->broadcast_task = GNUNET_SCHEDULER_add_now(&ifc_broadcast, bi);
2508 GNUNET_CONTAINER_DLL_insert (bi_head, bi_tail, bi); 2495 GNUNET_CONTAINER_DLL_insert(bi_head, bi_tail, bi);
2509 if ((AF_INET6 == addr->sa_family) && (NULL != broadcast_addr)) 2496 if ((AF_INET6 == addr->sa_family) && (NULL != broadcast_addr))
2510 {
2511 /* Create IPv6 multicast request */
2512 const struct sockaddr_in6 *s6 =
2513 (const struct sockaddr_in6 *) broadcast_addr;
2514
2515 GNUNET_assert (
2516 1 == inet_pton (AF_INET6, "FF05::13B", &bi->mcreq.ipv6mr_multiaddr));
2517
2518 /* http://tools.ietf.org/html/rfc2553#section-5.2:
2519 *
2520 * IPV6_JOIN_GROUP
2521 *
2522 * Join a multicast group on a specified local interface. If the
2523 * interface index is specified as 0, the kernel chooses the local
2524 * interface. For example, some kernels look up the multicast
2525 * group in the normal IPv6 routing table and using the resulting
2526 * interface; we do this for each interface, so no need to use
2527 * zero (anymore...).
2528 */
2529 bi->mcreq.ipv6mr_interface = s6->sin6_scope_id;
2530
2531 /* Join the multicast group */
2532 if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt (udp_sock,
2533 IPPROTO_IPV6,
2534 IPV6_JOIN_GROUP,
2535 &bi->mcreq,
2536 sizeof (bi->mcreq)))
2537 { 2497 {
2538 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt"); 2498 /* Create IPv6 multicast request */
2499 const struct sockaddr_in6 *s6 =
2500 (const struct sockaddr_in6 *)broadcast_addr;
2501
2502 GNUNET_assert(
2503 1 == inet_pton(AF_INET6, "FF05::13B", &bi->mcreq.ipv6mr_multiaddr));
2504
2505 /* http://tools.ietf.org/html/rfc2553#section-5.2:
2506 *
2507 * IPV6_JOIN_GROUP
2508 *
2509 * Join a multicast group on a specified local interface. If the
2510 * interface index is specified as 0, the kernel chooses the local
2511 * interface. For example, some kernels look up the multicast
2512 * group in the normal IPv6 routing table and using the resulting
2513 * interface; we do this for each interface, so no need to use
2514 * zero (anymore...).
2515 */
2516 bi->mcreq.ipv6mr_interface = s6->sin6_scope_id;
2517
2518 /* Join the multicast group */
2519 if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt(udp_sock,
2520 IPPROTO_IPV6,
2521 IPV6_JOIN_GROUP,
2522 &bi->mcreq,
2523 sizeof(bi->mcreq)))
2524 {
2525 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "setsockopt");
2526 }
2539 } 2527 }
2540 }
2541 return GNUNET_OK; 2528 return GNUNET_OK;
2542} 2529}
2543 2530
@@ -2548,23 +2535,23 @@ iface_proc (void *cls,
2548 * @param cls NULL, unused 2535 * @param cls NULL, unused
2549 */ 2536 */
2550static void 2537static void
2551do_broadcast (void *cls) 2538do_broadcast(void *cls)
2552{ 2539{
2553 struct BroadcastInterface *bin; 2540 struct BroadcastInterface *bin;
2554 2541
2555 (void) cls; 2542 (void)cls;
2556 for (struct BroadcastInterface *bi = bi_head; NULL != bi; bi = bi->next) 2543 for (struct BroadcastInterface *bi = bi_head; NULL != bi; bi = bi->next)
2557 bi->found = GNUNET_NO; 2544 bi->found = GNUNET_NO;
2558 GNUNET_OS_network_interfaces_list (&iface_proc, NULL); 2545 GNUNET_OS_network_interfaces_list(&iface_proc, NULL);
2559 for (struct BroadcastInterface *bi = bi_head; NULL != bi; bi = bin) 2546 for (struct BroadcastInterface *bi = bi_head; NULL != bi; bi = bin)
2560 { 2547 {
2561 bin = bi->next; 2548 bin = bi->next;
2562 if (GNUNET_NO == bi->found) 2549 if (GNUNET_NO == bi->found)
2563 bi_destroy (bi); 2550 bi_destroy(bi);
2564 } 2551 }
2565 broadcast_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_SCAN_FREQUENCY, 2552 broadcast_task = GNUNET_SCHEDULER_add_delayed(INTERFACE_SCAN_FREQUENCY,
2566 &do_broadcast, 2553 &do_broadcast,
2567 NULL); 2554 NULL);
2568} 2555}
2569 2556
2570 2557
@@ -2577,10 +2564,10 @@ do_broadcast (void *cls)
2577 * @param c configuration 2564 * @param c configuration
2578 */ 2565 */
2579static void 2566static void
2580run (void *cls, 2567run(void *cls,
2581 char *const *args, 2568 char *const *args,
2582 const char *cfgfile, 2569 const char *cfgfile,
2583 const struct GNUNET_CONFIGURATION_Handle *c) 2570 const struct GNUNET_CONFIGURATION_Handle *c)
2584{ 2571{
2585 char *bindto; 2572 char *bindto;
2586 struct sockaddr *in; 2573 struct sockaddr *in;
@@ -2588,141 +2575,143 @@ run (void *cls,
2588 struct sockaddr_storage in_sto; 2575 struct sockaddr_storage in_sto;
2589 socklen_t sto_len; 2576 socklen_t sto_len;
2590 2577
2591 (void) cls; 2578 (void)cls;
2592 cfg = c; 2579 cfg = c;
2593 if (GNUNET_OK != 2580 if (GNUNET_OK !=
2594 GNUNET_CONFIGURATION_get_value_filename (cfg, 2581 GNUNET_CONFIGURATION_get_value_filename(cfg,
2595 COMMUNICATOR_CONFIG_SECTION, 2582 COMMUNICATOR_CONFIG_SECTION,
2596 "BINDTO", 2583 "BINDTO",
2597 &bindto)) 2584 &bindto))
2598 { 2585 {
2599 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 2586 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
2600 COMMUNICATOR_CONFIG_SECTION, 2587 COMMUNICATOR_CONFIG_SECTION,
2601 "BINDTO"); 2588 "BINDTO");
2602 return; 2589 return;
2603 } 2590 }
2604 2591
2605 in = udp_address_to_sockaddr (bindto, &in_len); 2592 in = udp_address_to_sockaddr(bindto, &in_len);
2606 if (NULL == in) 2593 if (NULL == in)
2607 { 2594 {
2608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 2595 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
2609 "Failed to setup UDP socket address with path `%s'\n", 2596 "Failed to setup UDP socket address with path `%s'\n",
2610 bindto); 2597 bindto);
2611 GNUNET_free (bindto); 2598 GNUNET_free(bindto);
2612 return; 2599 return;
2613 } 2600 }
2614 udp_sock = 2601 udp_sock =
2615 GNUNET_NETWORK_socket_create (in->sa_family, SOCK_DGRAM, IPPROTO_UDP); 2602 GNUNET_NETWORK_socket_create(in->sa_family, SOCK_DGRAM, IPPROTO_UDP);
2616 if (NULL == udp_sock) 2603 if (NULL == udp_sock)
2617 { 2604 {
2618 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket"); 2605 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "socket");
2619 GNUNET_free (in); 2606 GNUNET_free(in);
2620 GNUNET_free (bindto); 2607 GNUNET_free(bindto);
2621 return; 2608 return;
2622 } 2609 }
2623 if (AF_INET6 == in->sa_family) 2610 if (AF_INET6 == in->sa_family)
2624 have_v6_socket = GNUNET_YES; 2611 have_v6_socket = GNUNET_YES;
2625 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (udp_sock, in, in_len)) 2612 if (GNUNET_OK != GNUNET_NETWORK_socket_bind(udp_sock, in, in_len))
2626 { 2613 {
2627 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "bind", bindto); 2614 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "bind", bindto);
2628 GNUNET_NETWORK_socket_close (udp_sock); 2615 GNUNET_NETWORK_socket_close(udp_sock);
2629 udp_sock = NULL; 2616 udp_sock = NULL;
2630 GNUNET_free (in); 2617 GNUNET_free(in);
2631 GNUNET_free (bindto); 2618 GNUNET_free(bindto);
2632 return; 2619 return;
2633 } 2620 }
2634 /* We might have bound to port 0, allowing the OS to figure it out; 2621 /* We might have bound to port 0, allowing the OS to figure it out;
2635 thus, get the real IN-address from the socket */ 2622 thus, get the real IN-address from the socket */
2636 sto_len = sizeof (in_sto); 2623 sto_len = sizeof(in_sto);
2637 if (0 != getsockname (GNUNET_NETWORK_get_fd (udp_sock), 2624 if (0 != getsockname(GNUNET_NETWORK_get_fd(udp_sock),
2638 (struct sockaddr *) &in_sto, 2625 (struct sockaddr *)&in_sto,
2639 &sto_len)) 2626 &sto_len))
2640 { 2627 {
2641 memcpy (&in_sto, in, in_len); 2628 memcpy(&in_sto, in, in_len);
2642 sto_len = in_len; 2629 sto_len = in_len;
2643 } 2630 }
2644 GNUNET_free (in); 2631 GNUNET_free(in);
2645 GNUNET_free (bindto); 2632 GNUNET_free(bindto);
2646 in = (struct sockaddr *) &in_sto; 2633 in = (struct sockaddr *)&in_sto;
2647 in_len = sto_len; 2634 in_len = sto_len;
2648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 2635 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
2649 "Bound to `%s'\n", 2636 "Bound to `%s'\n",
2650 GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len)); 2637 GNUNET_a2s((const struct sockaddr *)&in_sto, sto_len));
2651 switch (in->sa_family) 2638 switch (in->sa_family)
2652 { 2639 {
2653 case AF_INET: 2640 case AF_INET:
2654 my_port = ntohs (((struct sockaddr_in *) in)->sin_port); 2641 my_port = ntohs(((struct sockaddr_in *)in)->sin_port);
2655 break; 2642 break;
2656 case AF_INET6: 2643
2657 my_port = ntohs (((struct sockaddr_in6 *) in)->sin6_port); 2644 case AF_INET6:
2658 break; 2645 my_port = ntohs(((struct sockaddr_in6 *)in)->sin6_port);
2659 default: 2646 break;
2660 GNUNET_break (0); 2647
2661 my_port = 0; 2648 default:
2662 } 2649 GNUNET_break(0);
2663 stats = GNUNET_STATISTICS_create ("C-UDP", cfg); 2650 my_port = 0;
2664 senders = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES); 2651 }
2665 receivers = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES); 2652 stats = GNUNET_STATISTICS_create("C-UDP", cfg);
2666 senders_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN); 2653 senders = GNUNET_CONTAINER_multipeermap_create(32, GNUNET_YES);
2654 receivers = GNUNET_CONTAINER_multipeermap_create(32, GNUNET_YES);
2655 senders_heap = GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
2667 receivers_heap = 2656 receivers_heap =
2668 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN); 2657 GNUNET_CONTAINER_heap_create(GNUNET_CONTAINER_HEAP_ORDER_MIN);
2669 key_cache = GNUNET_CONTAINER_multishortmap_create (1024, GNUNET_YES); 2658 key_cache = GNUNET_CONTAINER_multishortmap_create(1024, GNUNET_YES);
2670 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL); 2659 GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL);
2671 is = GNUNET_NT_scanner_init (); 2660 is = GNUNET_NT_scanner_init();
2672 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg); 2661 my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration(cfg);
2673 if (NULL == my_private_key) 2662 if (NULL == my_private_key)
2674 { 2663 {
2675 GNUNET_log ( 2664 GNUNET_log(
2676 GNUNET_ERROR_TYPE_ERROR, 2665 GNUNET_ERROR_TYPE_ERROR,
2677 _ ( 2666 _(
2678 "Transport service is lacking key configuration settings. Exiting.\n")); 2667 "Transport service is lacking key configuration settings. Exiting.\n"));
2679 GNUNET_SCHEDULER_shutdown (); 2668 GNUNET_SCHEDULER_shutdown();
2680 return; 2669 return;
2681 } 2670 }
2682 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_identity.public_key); 2671 GNUNET_CRYPTO_eddsa_key_get_public(my_private_key, &my_identity.public_key);
2683 /* start reading */ 2672 /* start reading */
2684 read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, 2673 read_task = GNUNET_SCHEDULER_add_read_net(GNUNET_TIME_UNIT_FOREVER_REL,
2685 udp_sock, 2674 udp_sock,
2686 &sock_read, 2675 &sock_read,
2676 NULL);
2677 ch = GNUNET_TRANSPORT_communicator_connect(cfg,
2678 COMMUNICATOR_CONFIG_SECTION,
2679 COMMUNICATOR_ADDRESS_PREFIX,
2680 GNUNET_TRANSPORT_CC_UNRELIABLE,
2681 &mq_init,
2682 NULL,
2683 &enc_notify_cb,
2687 NULL); 2684 NULL);
2688 ch = GNUNET_TRANSPORT_communicator_connect (cfg,
2689 COMMUNICATOR_CONFIG_SECTION,
2690 COMMUNICATOR_ADDRESS_PREFIX,
2691 GNUNET_TRANSPORT_CC_UNRELIABLE,
2692 &mq_init,
2693 NULL,
2694 &enc_notify_cb,
2695 NULL);
2696 if (NULL == ch) 2685 if (NULL == ch)
2697 { 2686 {
2698 GNUNET_break (0); 2687 GNUNET_break(0);
2699 GNUNET_SCHEDULER_shutdown (); 2688 GNUNET_SCHEDULER_shutdown();
2700 return; 2689 return;
2701 } 2690 }
2702 ah = GNUNET_TRANSPORT_application_init (cfg); 2691 ah = GNUNET_TRANSPORT_application_init(cfg);
2703 if (NULL == ah) 2692 if (NULL == ah)
2704 { 2693 {
2705 GNUNET_break (0); 2694 GNUNET_break(0);
2706 GNUNET_SCHEDULER_shutdown (); 2695 GNUNET_SCHEDULER_shutdown();
2707 return; 2696 return;
2708 } 2697 }
2709 /* start broadcasting */ 2698 /* start broadcasting */
2710 if (GNUNET_YES != 2699 if (GNUNET_YES !=
2711 GNUNET_CONFIGURATION_get_value_yesno (cfg, 2700 GNUNET_CONFIGURATION_get_value_yesno(cfg,
2712 COMMUNICATOR_CONFIG_SECTION, 2701 COMMUNICATOR_CONFIG_SECTION,
2713 "DISABLE_BROADCAST")) 2702 "DISABLE_BROADCAST"))
2714 { 2703 {
2715 broadcast_task = GNUNET_SCHEDULER_add_now (&do_broadcast, NULL); 2704 broadcast_task = GNUNET_SCHEDULER_add_now(&do_broadcast, NULL);
2716 } 2705 }
2717 nat = GNUNET_NAT_register (cfg, 2706 nat = GNUNET_NAT_register(cfg,
2718 COMMUNICATOR_CONFIG_SECTION, 2707 COMMUNICATOR_CONFIG_SECTION,
2719 IPPROTO_UDP, 2708 IPPROTO_UDP,
2720 1 /* one address */, 2709 1 /* one address */,
2721 (const struct sockaddr **) &in, 2710 (const struct sockaddr **)&in,
2722 &in_len, 2711 &in_len,
2723 &nat_address_cb, 2712 &nat_address_cb,
2724 NULL /* FIXME: support reversal: #5529 */, 2713 NULL /* FIXME: support reversal: #5529 */,
2725 NULL /* closure */); 2714 NULL /* closure */);
2726} 2715}
2727 2716
2728 2717
@@ -2734,25 +2723,26 @@ run (void *cls,
2734 * @return 0 ok, 1 on error 2723 * @return 0 ok, 1 on error
2735 */ 2724 */
2736int 2725int
2737main (int argc, char *const *argv) 2726main(int argc, char *const *argv)
2738{ 2727{
2739 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 2728 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2740 GNUNET_GETOPT_OPTION_END}; 2729 GNUNET_GETOPT_OPTION_END
2730 };
2741 int ret; 2731 int ret;
2742 2732
2743 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 2733 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
2744 return 2; 2734 return 2;
2745 2735
2746 ret = (GNUNET_OK == GNUNET_PROGRAM_run (argc, 2736 ret = (GNUNET_OK == GNUNET_PROGRAM_run(argc,
2747 argv, 2737 argv,
2748 "gnunet-communicator-udp", 2738 "gnunet-communicator-udp",
2749 _ ("GNUnet UDP communicator"), 2739 _("GNUnet UDP communicator"),
2750 options, 2740 options,
2751 &run, 2741 &run,
2752 NULL)) 2742 NULL))
2753 ? 0 2743 ? 0
2754 : 1; 2744 : 1;
2755 GNUNET_free ((void *) argv); 2745 GNUNET_free((void *)argv);
2756 return ret; 2746 return ret;
2757} 2747}
2758 2748