2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file core/gnunet-service-core.c
23 * @brief high-level P2P messaging
24 * @author Christian Grothoff
26 * Type map implementation:
27 * - track type maps for neighbours (can wait)
28 * - only notify clients about peers with matching type maps (can wait)
30 * Considerations for later:
31 * - check that hostkey used by transport (for HELLOs) is the
32 * same as the hostkey that we are using!
36 #include "gnunet_constants.h"
37 #include "gnunet_util_lib.h"
38 #include "gnunet_hello_lib.h"
39 #include "gnunet_peerinfo_service.h"
40 #include "gnunet_protocols.h"
41 #include "gnunet_signatures.h"
42 #include "gnunet_statistics_service.h"
43 #include "gnunet_transport_service.h"
47 #define DEBUG_HANDSHAKE GNUNET_EXTRA_LOGGING
49 #define DEBUG_CORE_QUOTA GNUNET_EXTRA_LOGGING
52 * Receive and send buffer windows grow over time. For
53 * how long can 'unused' bandwidth accumulate before we
54 * need to cap it? (specified in seconds).
56 #define MAX_WINDOW_TIME_S (5 * 60)
59 * How many messages do we queue up at most for optional
60 * notifications to a client? (this can cause notifications
61 * about outgoing messages to be dropped).
63 #define MAX_NOTIFY_QUEUE 1024
66 * Minimum bandwidth (out) to assign to any connected peer.
67 * Should be rather low; values larger than DEFAULT_BW_IN_OUT make no
70 #define MIN_BANDWIDTH_PER_PEER GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT
73 * After how much time past the "official" expiration time do
74 * we discard messages? Should not be zero since we may
75 * intentionally defer transmission until close to the deadline
76 * and then may be slightly past the deadline due to inaccuracy
77 * in sleep and our own CPU consumption.
79 #define PAST_EXPIRATION_DISCARD_TIME GNUNET_TIME_UNIT_SECONDS
82 * What is the maximum delay for a SET_KEY message?
84 #define MAX_SET_KEY_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
87 * How long do we wait for SET_KEY confirmation initially?
89 #define INITIAL_SET_KEY_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (MAX_SET_KEY_DELAY, 1)
92 * What is the maximum delay for a PING message?
94 #define MAX_PING_DELAY GNUNET_TIME_relative_multiply (MAX_SET_KEY_DELAY, 2)
97 * What is the maximum delay for a PONG message?
99 #define MAX_PONG_DELAY GNUNET_TIME_relative_multiply (MAX_PING_DELAY, 2)
102 * What is the minimum frequency for a PING message?
104 #define MIN_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
107 * How often do we recalculate bandwidth quotas?
109 #define QUOTA_UPDATE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
112 * What is the priority for a SET_KEY message?
114 #define SET_KEY_PRIORITY 0xFFFFFF
117 * What is the priority for a PING message?
119 #define PING_PRIORITY 0xFFFFFF
122 * What is the priority for a PONG message?
124 #define PONG_PRIORITY 0xFFFFFF
127 * How many messages do we queue per peer at most? Must be at
130 #define MAX_PEER_QUEUE_SIZE 16
133 * How many non-mandatory messages do we queue per client at most?
135 #define MAX_CLIENT_QUEUE_SIZE 32
138 * What is the maximum age of a message for us to consider
139 * processing it? Note that this looks at the timestamp used
140 * by the other peer, so clock skew between machines does
141 * come into play here. So this should be picked high enough
142 * so that a little bit of clock skew does not prevent peers
143 * from connecting to us.
145 #define MAX_MESSAGE_AGE GNUNET_TIME_UNIT_DAYS
149 * State machine for our P2P encryption handshake. Everyone starts in
150 * "DOWN", if we receive the other peer's key (other peer initiated)
151 * we start in state RECEIVED (since we will immediately send our
152 * own); otherwise we start in SENT. If we get back a PONG from
153 * within either state, we move up to CONFIRMED (the PONG will always
154 * be sent back encrypted with the key we sent to the other peer).
156 enum PeerStateMachine
164 * We've sent our session key.
169 * We've received the other peers session key.
171 PEER_STATE_KEY_RECEIVED,
174 * The other peer has confirmed our session key with a message
175 * encrypted with his session key (which we got). Session is now fully up.
177 PEER_STATE_KEY_CONFIRMED
182 * Encapsulation for encrypted messages exchanged between
183 * peers. Followed by the actual encrypted data.
185 struct EncryptedMessage
188 * Message type is either CORE_ENCRYPTED_MESSAGE.
190 struct GNUNET_MessageHeader header;
193 * Random value used for IV generation.
195 uint32_t iv_seed GNUNET_PACKED;
198 * MAC of the encrypted message (starting at 'sequence_number'),
199 * used to verify message integrity. Everything after this value
200 * (excluding this value itself) will be encrypted and authenticated.
201 * ENCRYPTED_HEADER_SIZE must be set to the offset of the *next* field.
203 GNUNET_HashCode hmac;
206 * Sequence number, in network byte order. This field
207 * must be the first encrypted/decrypted field
209 uint32_t sequence_number GNUNET_PACKED;
212 * Desired bandwidth (how much we should send to this peer / how
213 * much is the sender willing to receive)?
215 struct GNUNET_BANDWIDTH_Value32NBO inbound_bw_limit;
218 * Timestamp. Used to prevent reply of ancient messages
219 * (recent messages are caught with the sequence number).
221 struct GNUNET_TIME_AbsoluteNBO timestamp;
227 * Number of bytes (at the beginning) of "struct EncryptedMessage"
228 * that are NOT encrypted.
230 #define ENCRYPTED_HEADER_SIZE (offsetof(struct EncryptedMessage, sequence_number))
234 * We're sending an (encrypted) PING to the other peer to check if he
235 * can decrypt. The other peer should respond with a PONG with the
236 * same content, except this time encrypted with the receiver's key.
241 * Message type is CORE_PING.
243 struct GNUNET_MessageHeader header;
248 uint32_t iv_seed GNUNET_PACKED;
251 * Intended target of the PING, used primarily to check
252 * that decryption actually worked.
254 struct GNUNET_PeerIdentity target;
257 * Random number chosen to make reply harder.
259 uint32_t challenge GNUNET_PACKED;
265 * Response to a PING. Includes data from the original PING
266 * plus initial bandwidth quota information.
271 * Message type is CORE_PONG.
273 struct GNUNET_MessageHeader header;
278 uint32_t iv_seed GNUNET_PACKED;
281 * Random number to make faking the reply harder. Must be
282 * first field after header (this is where we start to encrypt!).
284 uint32_t challenge GNUNET_PACKED;
287 * Desired bandwidth (how much we should send to this
288 * peer / how much is the sender willing to receive).
290 struct GNUNET_BANDWIDTH_Value32NBO inbound_bw_limit;
293 * Intended target of the PING, used primarily to check
294 * that decryption actually worked.
296 struct GNUNET_PeerIdentity target;
301 * Message transmitted to set (or update) a session key.
307 * Message type is either CORE_SET_KEY.
309 struct GNUNET_MessageHeader header;
312 * Status of the sender (should be in "enum PeerStateMachine"), nbo.
314 int32_t sender_status GNUNET_PACKED;
317 * Purpose of the signature, will be
318 * GNUNET_SIGNATURE_PURPOSE_SET_KEY.
320 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
323 * At what time was this key created?
325 struct GNUNET_TIME_AbsoluteNBO creation_time;
328 * The encrypted session key.
330 struct GNUNET_CRYPTO_RsaEncryptedData encrypted_key;
333 * Who is the intended recipient?
335 struct GNUNET_PeerIdentity target;
338 * Signature of the stuff above (starting at purpose).
340 struct GNUNET_CRYPTO_RsaSignature signature;
346 * Message waiting for transmission. This struct
347 * is followed by the actual content of the message.
353 * We keep messages in a doubly linked list.
355 struct MessageEntry *next;
358 * We keep messages in a doubly linked list.
360 struct MessageEntry *prev;
363 * By when are we supposed to transmit this message?
365 struct GNUNET_TIME_Absolute deadline;
368 * By when are we supposed to transmit this message (after
371 struct GNUNET_TIME_Absolute slack_deadline;
374 * How important is this message to us?
376 unsigned int priority;
379 * If this is a SET_KEY message, what was our connection status when this
380 * message was queued?
382 enum PeerStateMachine sender_status;
385 * Is this a SET_KEY message?
390 * How long is the message? (number of bytes following
391 * the "struct MessageEntry", but not including the
392 * size of "struct MessageEntry" itself!)
397 * Was this message selected for transmission in the
398 * current round? GNUNET_YES or GNUNET_NO.
403 * Did we give this message some slack (delayed sending) previously
404 * (and hence should not give it any more slack)? GNUNET_YES or
413 * Record kept for each request for transmission issued by a
414 * client that is still pending.
416 struct ClientActiveRequest;
419 * Data kept per neighbouring peer.
425 * Unencrypted messages destined for this peer.
427 struct MessageEntry *messages;
430 * Head of the batched, encrypted message queue (already ordered,
431 * transmit starting with the head).
433 struct MessageEntry *encrypted_head;
436 * Tail of the batched, encrypted message queue (already ordered,
437 * append new messages to tail)
439 struct MessageEntry *encrypted_tail;
442 * Head of list of requests from clients for transmission to
445 struct ClientActiveRequest *active_client_request_head;
448 * Tail of list of requests from clients for transmission to
451 struct ClientActiveRequest *active_client_request_tail;
454 * Handle for pending requests for transmission to this peer
455 * with the transport service. NULL if no request is pending.
457 struct GNUNET_TRANSPORT_TransmitHandle *th;
460 * Public key of the neighbour, NULL if we don't have it yet.
462 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key;
465 * We received a PING message before we got the "public_key"
466 * (or the SET_KEY). We keep it here until we have a key
467 * to decrypt it. NULL if no PING is pending.
469 struct PingMessage *pending_ping;
472 * We received a PONG message before we got the "public_key"
473 * (or the SET_KEY). We keep it here until we have a key
474 * to decrypt it. NULL if no PONG is pending.
476 struct PongMessage *pending_pong;
479 * Non-NULL if we are currently looking up HELLOs for this peer.
482 struct GNUNET_PEERINFO_IteratorContext *pitr;
485 * SetKeyMessage to transmit, NULL if we are not currently trying
488 struct SetKeyMessage *skm;
491 * Performance data for the peer.
493 struct GNUNET_TRANSPORT_ATS_Information *ats;
496 * Identity of the neighbour.
498 struct GNUNET_PeerIdentity peer;
501 * Key we use to encrypt our messages for the other peer
502 * (initialized by us when we do the handshake).
504 struct GNUNET_CRYPTO_AesSessionKey encrypt_key;
507 * Key we use to decrypt messages from the other peer
508 * (given to us by the other peer during the handshake).
510 struct GNUNET_CRYPTO_AesSessionKey decrypt_key;
513 * ID of task used for re-trying plaintext scheduling.
515 GNUNET_SCHEDULER_TaskIdentifier retry_plaintext_task;
518 * ID of task used for re-trying SET_KEY and PING message.
520 GNUNET_SCHEDULER_TaskIdentifier retry_set_key_task;
523 * ID of task used for updating bandwidth quota for this neighbour.
525 GNUNET_SCHEDULER_TaskIdentifier quota_update_task;
528 * ID of task used for sending keep-alive pings.
530 GNUNET_SCHEDULER_TaskIdentifier keep_alive_task;
533 * ID of task used for cleaning up dead neighbour entries.
535 GNUNET_SCHEDULER_TaskIdentifier dead_clean_task;
538 * At what time did we generate our encryption key?
540 struct GNUNET_TIME_Absolute encrypt_key_created;
543 * At what time did the other peer generate the decryption key?
545 struct GNUNET_TIME_Absolute decrypt_key_created;
548 * At what time did we initially establish (as in, complete session
549 * key handshake) this connection? Should be zero if status != KEY_CONFIRMED.
551 struct GNUNET_TIME_Absolute time_established;
554 * At what time did we last receive an encrypted message from the
555 * other peer? Should be zero if status != KEY_CONFIRMED.
557 struct GNUNET_TIME_Absolute last_activity;
560 * At what frequency are we currently re-trying SET_KEY messages?
562 struct GNUNET_TIME_Relative set_key_retry_frequency;
565 * Tracking bandwidth for sending to this peer.
567 struct GNUNET_BANDWIDTH_Tracker available_send_window;
570 * Tracking bandwidth for receiving from this peer.
572 struct GNUNET_BANDWIDTH_Tracker available_recv_window;
575 * How valueable were the messages of this peer recently?
577 unsigned long long current_preference;
580 * Number of entries in 'ats'.
582 unsigned int ats_count;
585 * Bit map indicating which of the 32 sequence numbers before the last
586 * were received (good for accepting out-of-order packets and
587 * estimating reliability of the connection)
589 unsigned int last_packets_bitmap;
592 * last sequence number received on this connection (highest)
594 uint32_t last_sequence_number_received;
597 * last sequence number transmitted
599 uint32_t last_sequence_number_sent;
602 * Available bandwidth in for this peer (current target).
604 struct GNUNET_BANDWIDTH_Value32NBO bw_in;
607 * Available bandwidth out for this peer (current target).
609 struct GNUNET_BANDWIDTH_Value32NBO bw_out;
612 * Internal bandwidth limit set for this peer (initially typically
613 * set to "-1"). Actual "bw_out" is MIN of
614 * "bpm_out_internal_limit" and "bw_out_external_limit".
616 struct GNUNET_BANDWIDTH_Value32NBO bw_out_internal_limit;
619 * External bandwidth limit set for this peer by the
620 * peer that we are communicating with. "bw_out" is MIN of
621 * "bw_out_internal_limit" and "bw_out_external_limit".
623 struct GNUNET_BANDWIDTH_Value32NBO bw_out_external_limit;
626 * What was our PING challenge number (for this peer)?
628 uint32_t ping_challenge;
631 * What is our connection status?
633 enum PeerStateMachine status;
636 * Are we currently connected to this neighbour?
644 * Data structure for each client connected to the core service.
649 * Clients are kept in a linked list.
654 * Handle for the client with the server API.
656 struct GNUNET_SERVER_Client *client_handle;
659 * Array of the types of messages this peer cares
660 * about (with "tcnt" entries). Allocated as part
661 * of this client struct, do not free!
663 const uint16_t *types;
666 * Map of peer identities to active transmission requests of this
667 * client to the peer (of type 'struct ClientActiveRequest').
669 struct GNUNET_CONTAINER_MultiHashMap *requests;
672 * Options for messages this client cares about,
673 * see GNUNET_CORE_OPTION_ values.
678 * Number of types of incoming messages this client
679 * specifically cares about. Size of the "types" array.
687 * Record kept for each request for transmission issued by a
688 * client that is still pending.
690 struct ClientActiveRequest
694 * Active requests are kept in a doubly-linked list of
695 * the respective target peer.
697 struct ClientActiveRequest *next;
700 * Active requests are kept in a doubly-linked list of
701 * the respective target peer.
703 struct ClientActiveRequest *prev;
706 * Handle to the client.
708 struct Client *client;
711 * By what time would the client want to see this message out?
713 struct GNUNET_TIME_Absolute deadline;
716 * How important is this request.
721 * How many more requests does this client have?
726 * How many bytes does the client intend to send?
731 * Unique request ID (in big endian).
742 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
747 static struct GNUNET_PeerIdentity my_identity;
752 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
755 * Bitmap of message types this peer is able to handle.
757 static uint32_t my_type_map[(UINT16_MAX + 1) / 32];
760 * Handle to peerinfo service.
762 static struct GNUNET_PEERINFO_Handle *peerinfo;
765 * Our message stream tokenizer (for encrypted payload).
767 static struct GNUNET_SERVER_MessageStreamTokenizer *mst;
772 const struct GNUNET_CONFIGURATION_Handle *cfg;
777 static struct GNUNET_TRANSPORT_Handle *transport;
780 * Linked list of our clients.
782 static struct Client *clients;
785 * Context for notifications we need to send to our clients.
787 static struct GNUNET_SERVER_NotificationContext *notifier;
790 * Map of peer identities to 'struct Neighbour'.
792 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
795 * Neighbour entry for "this" peer.
797 static struct Neighbour self;
800 * For creating statistics.
802 static struct GNUNET_STATISTICS_Handle *stats;
805 * Sum of all preferences among all neighbours.
807 static unsigned long long preference_sum;
810 * How much inbound bandwidth are we supposed to be using per second?
812 static unsigned long long bandwidth_target_in_bps;
815 * How much outbound bandwidth are we supposed to be using per second?
817 static unsigned long long bandwidth_target_out_bps;
820 * Derive an authentication key from "set key" information
823 derive_auth_key (struct GNUNET_CRYPTO_AuthKey *akey,
824 const struct GNUNET_CRYPTO_AesSessionKey *skey, uint32_t seed,
825 struct GNUNET_TIME_Absolute creation_time)
827 static const char ctx[] = "authentication key";
828 struct GNUNET_TIME_AbsoluteNBO ctbe;
831 ctbe = GNUNET_TIME_absolute_hton (creation_time);
832 GNUNET_CRYPTO_hmac_derive_key (akey, skey, &seed, sizeof (seed), &skey->key,
833 sizeof (skey->key), &ctbe, sizeof (ctbe), ctx,
839 * Derive an IV from packet information
842 derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
843 const struct GNUNET_CRYPTO_AesSessionKey *skey, uint32_t seed,
844 const struct GNUNET_PeerIdentity *identity)
846 static const char ctx[] = "initialization vector";
848 GNUNET_CRYPTO_aes_derive_iv (iv, skey, &seed, sizeof (seed),
849 &identity->hashPubKey.bits,
850 sizeof (identity->hashPubKey.bits), ctx,
855 * Derive an IV from pong packet information
858 derive_pong_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
859 const struct GNUNET_CRYPTO_AesSessionKey *skey, uint32_t seed,
860 uint32_t challenge, const struct GNUNET_PeerIdentity *identity)
862 static const char ctx[] = "pong initialization vector";
864 GNUNET_CRYPTO_aes_derive_iv (iv, skey, &seed, sizeof (seed),
865 &identity->hashPubKey.bits,
866 sizeof (identity->hashPubKey.bits), &challenge,
867 sizeof (challenge), ctx, sizeof (ctx), NULL);
872 * At what time should the connection to the given neighbour
873 * time out (given no further activity?)
875 * @param n neighbour in question
876 * @return absolute timeout
878 static struct GNUNET_TIME_Absolute
879 get_neighbour_timeout (struct Neighbour *n)
881 return GNUNET_TIME_absolute_add (n->last_activity,
882 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
887 * Helper function for update_preference_sum.
890 update_preference (void *cls, const GNUNET_HashCode * key, void *value)
892 unsigned long long *ps = cls;
893 struct Neighbour *n = value;
895 n->current_preference /= 2;
896 *ps += n->current_preference;
902 * A preference value for a neighbour was update. Update
903 * the preference sum accordingly.
905 * @param inc how much was a preference value increased?
908 update_preference_sum (unsigned long long inc)
910 unsigned long long os;
913 preference_sum += inc;
914 if (preference_sum >= os)
916 /* overflow! compensate by cutting all values in half! */
918 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &update_preference,
920 GNUNET_STATISTICS_set (stats, gettext_noop ("# total peer preference"),
921 preference_sum, GNUNET_NO);
926 * Find the entry for the given neighbour.
928 * @param peer identity of the neighbour
929 * @return NULL if we are not connected, otherwise the
932 static struct Neighbour *
933 find_neighbour (const struct GNUNET_PeerIdentity *peer)
935 return GNUNET_CONTAINER_multihashmap_get (neighbours, &peer->hashPubKey);
940 * Send a message to one of our clients.
942 * @param client target for the message
943 * @param msg message to transmit
944 * @param can_drop could this message be dropped if the
945 * client's queue is getting too large?
948 send_to_client (struct Client *client, const struct GNUNET_MessageHeader *msg,
951 #if DEBUG_CORE_CLIENT
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
953 "Preparing to send %u bytes of message of type %u to client.\n",
954 (unsigned int) ntohs (msg->size),
955 (unsigned int) ntohs (msg->type));
957 GNUNET_SERVER_notification_context_unicast (notifier, client->client_handle,
963 * Send a message to all of our current clients that have
964 * the right options set.
966 * @param msg message to multicast
967 * @param can_drop can this message be discarded if the queue is too long
968 * @param options mask to use
971 send_to_all_clients (const struct GNUNET_MessageHeader *msg, int can_drop,
979 if (0 != (c->options & options))
981 #if DEBUG_CORE_CLIENT > 1
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
983 "Sending message of type %u to client.\n",
984 (unsigned int) ntohs (msg->type));
986 send_to_client (c, msg, can_drop);
994 * Function called by transport telling us that a peer
997 * @param n the peer that changed status
1000 handle_peer_status_change (struct Neighbour *n)
1002 struct PeerStatusNotifyMessage *psnm;
1003 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
1004 struct GNUNET_TRANSPORT_ATS_Information *ats;
1007 if ((!n->is_connected) || (n->status != PEER_STATE_KEY_CONFIRMED))
1010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' changed status\n",
1011 GNUNET_i2s (&n->peer));
1014 sizeof (struct PeerStatusNotifyMessage) +
1015 n->ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
1016 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1019 /* recovery strategy: throw away performance data */
1020 GNUNET_array_grow (n->ats, n->ats_count, 0);
1022 sizeof (struct PeerStatusNotifyMessage) +
1023 n->ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
1025 psnm = (struct PeerStatusNotifyMessage *) buf;
1026 psnm->header.size = htons (size);
1027 psnm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_STATUS_CHANGE);
1028 psnm->timeout = GNUNET_TIME_absolute_hton (get_neighbour_timeout (n));
1029 psnm->bandwidth_in = n->bw_in;
1030 psnm->bandwidth_out = n->bw_out;
1031 psnm->peer = n->peer;
1032 psnm->ats_count = htonl (n->ats_count);
1034 memcpy (ats, n->ats,
1035 n->ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
1036 ats[n->ats_count].type = htonl (0);
1037 ats[n->ats_count].value = htonl (0);
1038 send_to_all_clients (&psnm->header, GNUNET_YES,
1039 GNUNET_CORE_OPTION_SEND_STATUS_CHANGE);
1040 GNUNET_STATISTICS_update (stats, gettext_noop ("# peer status changes"), 1,
1046 * Go over our message queue and if it is not too long, go
1047 * over the pending requests from clients for this
1048 * neighbour and send some clients a 'READY' notification.
1050 * @param n which peer to process
1053 schedule_peer_messages (struct Neighbour *n)
1055 struct SendMessageReady smr;
1056 struct ClientActiveRequest *car;
1057 struct ClientActiveRequest *pos;
1059 struct MessageEntry *mqe;
1060 unsigned int queue_size;
1062 /* check if neighbour queue is empty enough! */
1072 if (queue_size >= MAX_PEER_QUEUE_SIZE)
1074 #if DEBUG_CORE_CLIENT
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1076 "Not considering client transmission requests: queue full\n");
1078 return; /* queue still full */
1080 /* find highest priority request */
1081 pos = n->active_client_request_head;
1085 if ((car == NULL) || (pos->priority > car->priority))
1092 car = n->active_client_request_head;
1095 return; /* no pending requests */
1096 #if DEBUG_CORE_CLIENT
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1098 "Permitting client transmission request to `%s'\n",
1099 GNUNET_i2s (&n->peer));
1102 GNUNET_CONTAINER_DLL_remove (n->active_client_request_head,
1103 n->active_client_request_tail, car);
1104 GNUNET_assert (GNUNET_YES ==
1105 GNUNET_CONTAINER_multihashmap_remove (c->requests,
1106 &n->peer.hashPubKey,
1108 smr.header.size = htons (sizeof (struct SendMessageReady));
1109 smr.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_READY);
1110 smr.size = htons (car->msize);
1111 smr.smr_id = car->smr_id;
1113 send_to_client (c, &smr.header, GNUNET_NO);
1119 * Compute a type map message for this peer.
1121 * @return this peers current type map message.
1123 static struct GNUNET_MessageHeader *
1124 compute_type_map_message ()
1128 struct GNUNET_MessageHeader *hdr;
1130 #ifdef compressBound
1131 dlen = compressBound (sizeof (my_type_map));
1133 dlen = sizeof (my_type_map) + (sizeof (my_type_map) / 100) + 20;
1134 /* documentation says 100.1% oldSize + 12 bytes, but we
1135 * should be able to overshoot by more to be safe */
1137 hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader));
1138 hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader));
1139 tmp = (char *) &hdr[1];
1141 compress2 ((Bytef *) tmp, &dlen, (const Bytef *) my_type_map,
1142 sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map)))
1144 dlen = sizeof (my_type_map);
1145 memcpy (tmp, my_type_map, sizeof (my_type_map));
1146 hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP);
1150 hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
1157 * Send a type map message to the neighbour.
1159 * @param cls the type map message
1160 * @param key neighbour's identity
1161 * @param value 'struct Neighbour' of the target
1162 * @return always GNUNET_OK
1165 send_type_map_to_neighbour (void *cls, const GNUNET_HashCode * key, void *value)
1167 struct GNUNET_MessageHeader *hdr = cls;
1168 struct Neighbour *n = value;
1169 struct MessageEntry *m;
1174 size = ntohs (hdr->size);
1175 m = GNUNET_malloc (sizeof (struct MessageEntry) + size);
1176 memcpy (&m[1], hdr, size);
1177 m->deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
1178 m->slack_deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
1179 m->priority = UINT_MAX;
1180 m->sender_status = n->status;
1182 m->next = n->messages;
1190 * Send my type map to all connected peers (it got changed).
1193 broadcast_my_type_map ()
1195 struct GNUNET_MessageHeader *hdr;
1197 if (NULL == neighbours)
1199 hdr = compute_type_map_message ();
1200 GNUNET_CONTAINER_multihashmap_iterate (neighbours,
1201 &send_type_map_to_neighbour, hdr);
1207 * Handle CORE_SEND_REQUEST message.
1210 handle_client_send_request (void *cls, struct GNUNET_SERVER_Client *client,
1211 const struct GNUNET_MessageHeader *message)
1213 const struct SendMessageRequest *req;
1214 struct Neighbour *n;
1216 struct ClientActiveRequest *car;
1218 req = (const struct SendMessageRequest *) message;
1220 memcmp (&req->peer, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1223 n = find_neighbour (&req->peer);
1224 if ((n == NULL) || (GNUNET_YES != n->is_connected) ||
1225 (n->status != PEER_STATE_KEY_CONFIRMED))
1227 /* neighbour must have disconnected since request was issued,
1228 * ignore (client will realize it once it processes the
1229 * disconnect notification) */
1230 #if DEBUG_CORE_CLIENT
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1232 "Dropped client request for transmission (am disconnected)\n");
1234 GNUNET_STATISTICS_update (stats,
1236 ("# send requests dropped (disconnected)"), 1,
1238 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1242 while ((c != NULL) && (c->client_handle != client))
1246 /* client did not send INIT first! */
1248 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1251 if (c->requests == NULL)
1252 c->requests = GNUNET_CONTAINER_multihashmap_create (16);
1253 #if DEBUG_CORE_CLIENT
1254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1255 "Received client transmission request. queueing\n");
1257 car = GNUNET_CONTAINER_multihashmap_get (c->requests, &req->peer.hashPubKey);
1260 /* create new entry */
1261 car = GNUNET_malloc (sizeof (struct ClientActiveRequest));
1262 GNUNET_assert (GNUNET_OK ==
1263 GNUNET_CONTAINER_multihashmap_put (c->requests,
1264 &req->peer.hashPubKey,
1266 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1267 GNUNET_CONTAINER_DLL_insert (n->active_client_request_head,
1268 n->active_client_request_tail, car);
1271 car->deadline = GNUNET_TIME_absolute_ntoh (req->deadline);
1272 car->priority = ntohl (req->priority);
1273 car->queue_size = ntohl (req->queue_size);
1274 car->msize = ntohs (req->size);
1275 car->smr_id = req->smr_id;
1276 schedule_peer_messages (n);
1277 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1282 * Notify client about an existing connection to one of our neighbours.
1285 notify_client_about_neighbour (void *cls, const GNUNET_HashCode * key,
1288 struct Client *c = cls;
1289 struct Neighbour *n = value;
1291 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
1292 struct GNUNET_TRANSPORT_ATS_Information *ats;
1293 struct ConnectNotifyMessage *cnm;
1296 sizeof (struct ConnectNotifyMessage) +
1297 (n->ats_count) * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
1298 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1301 /* recovery strategy: throw away performance data */
1302 GNUNET_array_grow (n->ats, n->ats_count, 0);
1304 sizeof (struct ConnectNotifyMessage) +
1305 (n->ats_count) * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
1307 cnm = (struct ConnectNotifyMessage *) buf;
1308 cnm->header.size = htons (size);
1309 cnm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
1310 cnm->ats_count = htonl (n->ats_count);
1312 memcpy (ats, n->ats,
1313 sizeof (struct GNUNET_TRANSPORT_ATS_Information) * n->ats_count);
1314 ats[n->ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1315 ats[n->ats_count].value = htonl (0);
1316 if (n->status == PEER_STATE_KEY_CONFIRMED)
1318 #if DEBUG_CORE_CLIENT
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message to client.\n",
1322 cnm->peer = n->peer;
1323 send_to_client (c, &cnm->header, GNUNET_NO);
1331 * Handle CORE_INIT request.
1334 handle_client_init (void *cls, struct GNUNET_SERVER_Client *client,
1335 const struct GNUNET_MessageHeader *message)
1337 const struct InitMessage *im;
1338 struct InitReplyMessage irm;
1341 const uint16_t *types;
1345 #if DEBUG_CORE_CLIENT
1346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1347 "Client connecting to core service with `%s' message\n", "INIT");
1349 /* check that we don't have an entry already */
1353 if (client == c->client_handle)
1356 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1361 msize = ntohs (message->size);
1362 if (msize < sizeof (struct InitMessage))
1365 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1368 GNUNET_SERVER_notification_context_add (notifier, client);
1369 im = (const struct InitMessage *) message;
1370 types = (const uint16_t *) &im[1];
1371 msize -= sizeof (struct InitMessage);
1372 c = GNUNET_malloc (sizeof (struct Client) + msize);
1373 c->client_handle = client;
1376 c->tcnt = msize / sizeof (uint16_t);
1377 c->types = (const uint16_t *) &c[1];
1378 wtypes = (uint16_t *) & c[1];
1379 for (i = 0; i < c->tcnt; i++)
1381 wtypes[i] = ntohs (types[i]);
1382 my_type_map[wtypes[i] / 32] |= (1 << (wtypes[i] % 32));
1385 broadcast_my_type_map ();
1386 c->options = ntohl (im->options);
1387 #if DEBUG_CORE_CLIENT
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1389 "Client %p is interested in %u message types\n", c,
1390 (unsigned int) c->tcnt);
1392 /* send init reply message */
1393 irm.header.size = htons (sizeof (struct InitReplyMessage));
1394 irm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY);
1395 irm.reserved = htonl (0);
1396 irm.my_identity = my_identity;
1397 #if DEBUG_CORE_CLIENT
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message to client.\n",
1401 send_to_client (c, &irm.header, GNUNET_NO);
1402 if (0 != (c->options & GNUNET_CORE_OPTION_SEND_CONNECT))
1404 /* notify new client about existing neighbours */
1405 GNUNET_CONTAINER_multihashmap_iterate (neighbours,
1406 ¬ify_client_about_neighbour, c);
1408 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1413 * Free client request records.
1416 * @param key identity of peer for which this is an active request
1417 * @param value the 'struct ClientActiveRequest' to free
1418 * @return GNUNET_YES (continue iteration)
1421 destroy_active_client_request (void *cls, const GNUNET_HashCode * key,
1424 struct ClientActiveRequest *car = value;
1425 struct Neighbour *n;
1426 struct GNUNET_PeerIdentity peer;
1428 peer.hashPubKey = *key;
1429 n = find_neighbour (&peer);
1430 GNUNET_assert (NULL != n);
1431 GNUNET_CONTAINER_DLL_remove (n->active_client_request_head,
1432 n->active_client_request_tail, car);
1439 * A client disconnected, clean up.
1441 * @param cls closure
1442 * @param client identification of the client
1445 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
1448 struct Client *prev;
1450 const uint16_t *wtypes;
1454 #if DEBUG_CORE_CLIENT
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1456 "Client %p has disconnected from core service.\n", client);
1462 if (client == pos->client_handle)
1469 /* client never sent INIT */
1473 clients = pos->next;
1475 prev->next = pos->next;
1476 if (pos->requests != NULL)
1478 GNUNET_CONTAINER_multihashmap_iterate (pos->requests,
1479 &destroy_active_client_request,
1481 GNUNET_CONTAINER_multihashmap_destroy (pos->requests);
1485 /* rebuild my_type_map */
1486 memset (my_type_map, 0, sizeof (my_type_map));
1487 for (pos = clients; NULL != pos; pos = pos->next)
1489 wtypes = (const uint16_t *) &pos[1];
1490 for (i = 0; i < pos->tcnt; i++)
1491 my_type_map[wtypes[i] / 32] |= (1 << (wtypes[i] % 32));
1493 broadcast_my_type_map ();
1498 * Helper function for handle_client_iterate_peers.
1500 * @param cls the 'struct GNUNET_SERVER_TransmitContext' to queue replies
1501 * @param key identity of the connected peer
1502 * @param value the 'struct Neighbour' for the peer
1503 * @return GNUNET_OK (continue to iterate)
1506 queue_connect_message (void *cls, const GNUNET_HashCode * key, void *value)
1508 struct GNUNET_SERVER_TransmitContext *tc = cls;
1509 struct Neighbour *n = value;
1510 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
1511 struct GNUNET_TRANSPORT_ATS_Information *ats;
1513 struct ConnectNotifyMessage *cnm;
1515 cnm = (struct ConnectNotifyMessage *) buf;
1516 if (n->status != PEER_STATE_KEY_CONFIRMED)
1519 sizeof (struct ConnectNotifyMessage) +
1520 (n->ats_count) * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
1521 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1524 /* recovery strategy: throw away performance data */
1525 GNUNET_array_grow (n->ats, n->ats_count, 0);
1527 sizeof (struct PeerStatusNotifyMessage) +
1528 n->ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
1530 cnm = (struct ConnectNotifyMessage *) buf;
1531 cnm->header.size = htons (size);
1532 cnm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
1533 cnm->ats_count = htonl (n->ats_count);
1535 memcpy (ats, n->ats,
1536 n->ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
1537 ats[n->ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1538 ats[n->ats_count].value = htonl (0);
1539 #if DEBUG_CORE_CLIENT
1540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message to client.\n",
1543 cnm->peer = n->peer;
1544 GNUNET_SERVER_transmit_context_append_message (tc, &cnm->header);
1550 * Handle CORE_ITERATE_PEERS request.
1553 * @param client client sending the iteration request
1554 * @param message iteration request message
1557 handle_client_iterate_peers (void *cls, struct GNUNET_SERVER_Client *client,
1558 const struct GNUNET_MessageHeader *message)
1560 struct GNUNET_MessageHeader done_msg;
1561 struct GNUNET_SERVER_TransmitContext *tc;
1564 /* notify new client about existing neighbours */
1566 msize = ntohs (message->size);
1567 tc = GNUNET_SERVER_transmit_context_create (client);
1568 if (msize == sizeof (struct GNUNET_MessageHeader))
1569 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &queue_connect_message,
1574 done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
1575 done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END);
1576 GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
1577 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1582 * Handle CORE_PEER_CONNECTED request. Notify client about existing neighbours.
1585 * @param client client sending the iteration request
1586 * @param message iteration request message
1589 handle_client_have_peer (void *cls, struct GNUNET_SERVER_Client *client,
1590 const struct GNUNET_MessageHeader *message)
1592 struct GNUNET_MessageHeader done_msg;
1593 struct GNUNET_SERVER_TransmitContext *tc;
1594 struct GNUNET_PeerIdentity *peer;
1596 tc = GNUNET_SERVER_transmit_context_create (client);
1597 peer = (struct GNUNET_PeerIdentity *) &message[1];
1598 GNUNET_CONTAINER_multihashmap_get_multiple (neighbours, &peer->hashPubKey,
1599 &queue_connect_message, tc);
1600 done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
1601 done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END);
1602 GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
1603 GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL);
1608 * Handle REQUEST_INFO request.
1611 * @param client client sending the request
1612 * @param message iteration request message
1615 handle_client_request_info (void *cls, struct GNUNET_SERVER_Client *client,
1616 const struct GNUNET_MessageHeader *message)
1618 const struct RequestInfoMessage *rcm;
1620 struct Neighbour *n;
1621 struct ConfigurationInfoMessage cim;
1622 int32_t want_reserv;
1624 unsigned long long old_preference;
1625 struct GNUNET_TIME_Relative rdelay;
1627 rdelay = GNUNET_TIME_relative_get_zero ();
1628 #if DEBUG_CORE_CLIENT
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core service receives `%s' request.\n",
1635 if (client == pos->client_handle)
1642 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1646 rcm = (const struct RequestInfoMessage *) message;
1647 n = find_neighbour (&rcm->peer);
1648 memset (&cim, 0, sizeof (cim));
1649 if ((n != NULL) && (GNUNET_YES == n->is_connected))
1651 want_reserv = ntohl (rcm->reserve_inbound);
1652 if (n->bw_out_internal_limit.value__ != rcm->limit_outbound.value__)
1654 n->bw_out_internal_limit = rcm->limit_outbound;
1655 if (n->bw_out.value__ !=
1656 GNUNET_BANDWIDTH_value_min (n->bw_out_internal_limit,
1657 n->bw_out_external_limit).value__)
1660 GNUNET_BANDWIDTH_value_min (n->bw_out_internal_limit,
1661 n->bw_out_external_limit);
1662 GNUNET_BANDWIDTH_tracker_update_quota (&n->available_recv_window,
1664 GNUNET_TRANSPORT_set_quota (transport, &n->peer, n->bw_in, n->bw_out);
1665 handle_peer_status_change (n);
1668 if (want_reserv < 0)
1670 got_reserv = want_reserv;
1672 else if (want_reserv > 0)
1675 GNUNET_BANDWIDTH_tracker_get_delay (&n->available_recv_window,
1677 if (rdelay.rel_value == 0)
1678 got_reserv = want_reserv;
1680 got_reserv = 0; /* all or nothing */
1684 GNUNET_BANDWIDTH_tracker_consume (&n->available_recv_window, got_reserv);
1685 old_preference = n->current_preference;
1686 n->current_preference += GNUNET_ntohll (rcm->preference_change);
1687 if (old_preference > n->current_preference)
1689 /* overflow; cap at maximum value */
1690 n->current_preference = ULLONG_MAX;
1692 update_preference_sum (n->current_preference - old_preference);
1693 #if DEBUG_CORE_QUOTA
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "Received reservation request for %d bytes for peer `%4s', reserved %d bytes, suggesting delay of %llu ms\n",
1696 (int) want_reserv, GNUNET_i2s (&rcm->peer), (int) got_reserv,
1697 (unsigned long long) rdelay.rel_value);
1699 cim.reserved_amount = htonl (got_reserv);
1700 cim.reserve_delay = GNUNET_TIME_relative_hton (rdelay);
1701 cim.bw_out = n->bw_out;
1702 cim.preference = n->current_preference;
1706 /* Technically, this COULD happen (due to asynchronous behavior),
1707 * but it should be rare, so we should generate an info event
1708 * to help diagnosis of serious errors that might be masked by this */
1709 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1711 ("Client asked for preference change with peer `%s', which is not connected!\n"),
1712 GNUNET_i2s (&rcm->peer));
1713 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1716 cim.header.size = htons (sizeof (struct ConfigurationInfoMessage));
1717 cim.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO);
1718 cim.peer = rcm->peer;
1719 cim.rim_id = rcm->rim_id;
1720 #if DEBUG_CORE_CLIENT
1721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message to client.\n",
1722 "CONFIGURATION_INFO");
1724 send_to_client (pos, &cim.header, GNUNET_NO);
1725 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1730 * Free the given entry for the neighbour (it has
1731 * already been removed from the list at this point).
1733 * @param n neighbour to free
1736 free_neighbour (struct Neighbour *n)
1738 struct MessageEntry *m;
1739 struct ClientActiveRequest *car;
1742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1743 "Destroying neighbour entry for peer `%4s'\n",
1744 GNUNET_i2s (&n->peer));
1746 if (n->pitr != NULL)
1748 GNUNET_PEERINFO_iterate_cancel (n->pitr);
1753 GNUNET_free (n->skm);
1756 while (NULL != (m = n->messages))
1758 n->messages = m->next;
1761 while (NULL != (m = n->encrypted_head))
1763 GNUNET_CONTAINER_DLL_remove (n->encrypted_head, n->encrypted_tail, m);
1766 while (NULL != (car = n->active_client_request_head))
1768 GNUNET_CONTAINER_DLL_remove (n->active_client_request_head,
1769 n->active_client_request_tail, car);
1770 GNUNET_assert (GNUNET_YES ==
1771 GNUNET_CONTAINER_multihashmap_remove (car->client->requests,
1772 &n->peer.hashPubKey,
1778 GNUNET_TRANSPORT_notify_transmit_ready_cancel (n->th);
1781 if (n->retry_plaintext_task != GNUNET_SCHEDULER_NO_TASK)
1782 GNUNET_SCHEDULER_cancel (n->retry_plaintext_task);
1783 if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
1784 GNUNET_SCHEDULER_cancel (n->retry_set_key_task);
1785 if (n->quota_update_task != GNUNET_SCHEDULER_NO_TASK)
1786 GNUNET_SCHEDULER_cancel (n->quota_update_task);
1787 if (n->dead_clean_task != GNUNET_SCHEDULER_NO_TASK)
1788 GNUNET_SCHEDULER_cancel (n->dead_clean_task);
1789 if (n->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
1790 GNUNET_SCHEDULER_cancel (n->keep_alive_task);
1791 if (n->status == PEER_STATE_KEY_CONFIRMED)
1792 GNUNET_STATISTICS_update (stats, gettext_noop ("# established sessions"),
1794 GNUNET_array_grow (n->ats, n->ats_count, 0);
1795 GNUNET_free_non_null (n->public_key);
1796 GNUNET_free_non_null (n->pending_ping);
1797 GNUNET_free_non_null (n->pending_pong);
1803 * Check if we have encrypted messages for the specified neighbour
1804 * pending, and if so, check with the transport about sending them
1807 * @param n neighbour to check.
1810 process_encrypted_neighbour_queue (struct Neighbour *n);
1814 * Encrypt size bytes from in and write the result to out. Use the
1815 * key for outbound traffic of the given neighbour.
1817 * @param n neighbour we are sending to
1818 * @param iv initialization vector to use
1819 * @param in ciphertext
1820 * @param out plaintext
1821 * @param size size of in/out
1822 * @return GNUNET_OK on success
1825 do_encrypt (struct Neighbour *n,
1826 const struct GNUNET_CRYPTO_AesInitializationVector *iv,
1827 const void *in, void *out, size_t size)
1829 if (size != (uint16_t) size)
1834 GNUNET_assert (size ==
1835 GNUNET_CRYPTO_aes_encrypt (in, (uint16_t) size,
1836 &n->encrypt_key, iv, out));
1837 GNUNET_STATISTICS_update (stats, gettext_noop ("# bytes encrypted"), size,
1840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1841 "Encrypted %u bytes for `%4s' using key %u, IV %u\n",
1842 (unsigned int) size, GNUNET_i2s (&n->peer),
1843 (unsigned int) n->encrypt_key.crc32, GNUNET_CRYPTO_crc32_n (iv,
1852 * Consider freeing the given neighbour since we may not need
1853 * to keep it around anymore.
1855 * @param n neighbour to consider discarding
1858 consider_free_neighbour (struct Neighbour *n);
1862 * Task triggered when a neighbour entry is about to time out
1863 * (and we should prevent this by sending a PING).
1865 * @param cls the 'struct Neighbour'
1866 * @param tc scheduler context (not used)
1869 send_keep_alive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1871 struct Neighbour *n = cls;
1872 struct GNUNET_TIME_Relative retry;
1873 struct GNUNET_TIME_Relative left;
1874 struct MessageEntry *me;
1875 struct PingMessage pp;
1876 struct PingMessage *pm;
1877 struct GNUNET_CRYPTO_AesInitializationVector iv;
1879 n->keep_alive_task = GNUNET_SCHEDULER_NO_TASK;
1881 me = GNUNET_malloc (sizeof (struct MessageEntry) +
1882 sizeof (struct PingMessage));
1883 me->deadline = GNUNET_TIME_relative_to_absolute (MAX_PING_DELAY);
1884 me->priority = PING_PRIORITY;
1885 me->size = sizeof (struct PingMessage);
1886 GNUNET_CONTAINER_DLL_insert_after (n->encrypted_head, n->encrypted_tail,
1887 n->encrypted_tail, me);
1888 pm = (struct PingMessage *) &me[1];
1889 pm->header.size = htons (sizeof (struct PingMessage));
1890 pm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PING);
1892 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1893 derive_iv (&iv, &n->encrypt_key, pm->iv_seed, &n->peer);
1894 pp.challenge = n->ping_challenge;
1895 pp.target = n->peer;
1897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1898 "Encrypting `%s' message with challenge %u for `%4s' using key %u, IV %u (salt %u).\n",
1899 "PING", (unsigned int) n->ping_challenge, GNUNET_i2s (&n->peer),
1900 (unsigned int) n->encrypt_key.crc32, GNUNET_CRYPTO_crc32_n (&iv,
1905 do_encrypt (n, &iv, &pp.target, &pm->target,
1906 sizeof (struct PingMessage) - ((void *) &pm->target -
1908 process_encrypted_neighbour_queue (n);
1909 /* reschedule PING job */
1910 left = GNUNET_TIME_absolute_get_remaining (get_neighbour_timeout (n));
1912 GNUNET_TIME_relative_max (GNUNET_TIME_relative_divide (left, 2),
1913 MIN_PING_FREQUENCY);
1914 n->keep_alive_task =
1915 GNUNET_SCHEDULER_add_delayed (retry, &send_keep_alive, n);
1921 * Task triggered when a neighbour entry might have gotten stale.
1923 * @param cls the 'struct Neighbour'
1924 * @param tc scheduler context (not used)
1927 consider_free_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1929 struct Neighbour *n = cls;
1931 n->dead_clean_task = GNUNET_SCHEDULER_NO_TASK;
1932 consider_free_neighbour (n);
1937 * Consider freeing the given neighbour since we may not need
1938 * to keep it around anymore.
1940 * @param n neighbour to consider discarding
1943 consider_free_neighbour (struct Neighbour *n)
1945 struct GNUNET_TIME_Relative left;
1947 if ((n->th != NULL) || (n->pitr != NULL) || (GNUNET_YES == n->is_connected))
1948 return; /* no chance */
1950 left = GNUNET_TIME_absolute_get_remaining (get_neighbour_timeout (n));
1951 if (left.rel_value > 0)
1953 if (n->dead_clean_task != GNUNET_SCHEDULER_NO_TASK)
1954 GNUNET_SCHEDULER_cancel (n->dead_clean_task);
1955 n->dead_clean_task =
1956 GNUNET_SCHEDULER_add_delayed (left, &consider_free_task, n);
1959 /* actually free the neighbour... */
1960 GNUNET_assert (GNUNET_YES ==
1961 GNUNET_CONTAINER_multihashmap_remove (neighbours,
1962 &n->peer.hashPubKey, n));
1963 GNUNET_STATISTICS_set (stats, gettext_noop ("# neighbour entries allocated"),
1964 GNUNET_CONTAINER_multihashmap_size (neighbours),
1971 * Function called when the transport service is ready to
1972 * receive an encrypted message for the respective peer
1974 * @param cls neighbour to use message from
1975 * @param size number of bytes we can transmit
1976 * @param buf where to copy the message
1977 * @return number of bytes transmitted
1980 notify_encrypted_transmit_ready (void *cls, size_t size, void *buf)
1982 struct Neighbour *n = cls;
1983 struct MessageEntry *m;
1988 m = n->encrypted_head;
1992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1993 "Encrypted message queue empty, no messages added to buffer for `%4s'\n",
1994 GNUNET_i2s (&n->peer));
1998 GNUNET_CONTAINER_DLL_remove (n->encrypted_head, n->encrypted_tail, m);
2003 GNUNET_assert (size >= m->size);
2004 memcpy (cbuf, &m[1], m->size);
2006 GNUNET_BANDWIDTH_tracker_consume (&n->available_send_window, m->size);
2008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2009 "Copied message of type %u and size %u into transport buffer for `%4s'\n",
2011 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
2012 (unsigned int) ret, GNUNET_i2s (&n->peer));
2014 process_encrypted_neighbour_queue (n);
2019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2020 "Transmission of message of type %u and size %u failed\n",
2022 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
2023 (unsigned int) m->size);
2027 consider_free_neighbour (n);
2028 GNUNET_STATISTICS_update (stats,
2030 ("# encrypted bytes given to transport"), ret,
2037 * Check if we have plaintext messages for the specified neighbour
2038 * pending, and if so, consider batching and encrypting them (and
2039 * then trigger processing of the encrypted queue if needed).
2041 * @param n neighbour to check.
2044 process_plaintext_neighbour_queue (struct Neighbour *n);
2048 * Check if we have encrypted messages for the specified neighbour
2049 * pending, and if so, check with the transport about sending them
2052 * @param n neighbour to check.
2055 process_encrypted_neighbour_queue (struct Neighbour *n)
2057 struct MessageEntry *m;
2060 return; /* request already pending */
2061 if (GNUNET_YES != n->is_connected)
2066 m = n->encrypted_head;
2069 /* encrypted queue empty, try plaintext instead */
2070 process_plaintext_neighbour_queue (n);
2074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2075 "Asking transport for transmission of %u bytes to `%4s' in next %llu ms\n",
2076 (unsigned int) m->size, GNUNET_i2s (&n->peer),
2077 (unsigned long long)
2078 GNUNET_TIME_absolute_get_remaining (m->deadline).rel_value);
2081 GNUNET_TRANSPORT_notify_transmit_ready (transport, &n->peer, m->size,
2083 GNUNET_TIME_absolute_get_remaining
2085 ¬ify_encrypted_transmit_ready,
2089 /* message request too large or duplicate request */
2091 /* discard encrypted message */
2092 GNUNET_CONTAINER_DLL_remove (n->encrypted_head, n->encrypted_tail, m);
2094 process_encrypted_neighbour_queue (n);
2100 * Decrypt size bytes from in and write the result to out. Use the
2101 * key for inbound traffic of the given neighbour. This function does
2102 * NOT do any integrity-checks on the result.
2104 * @param n neighbour we are receiving from
2105 * @param iv initialization vector to use
2106 * @param in ciphertext
2107 * @param out plaintext
2108 * @param size size of in/out
2109 * @return GNUNET_OK on success
2112 do_decrypt (struct Neighbour *n,
2113 const struct GNUNET_CRYPTO_AesInitializationVector *iv,
2114 const void *in, void *out, size_t size)
2116 if (size != (uint16_t) size)
2121 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
2122 (n->status != PEER_STATE_KEY_CONFIRMED))
2124 GNUNET_break_op (0);
2125 return GNUNET_SYSERR;
2128 GNUNET_CRYPTO_aes_decrypt (in, (uint16_t) size, &n->decrypt_key, iv, out))
2131 return GNUNET_SYSERR;
2133 GNUNET_STATISTICS_update (stats, gettext_noop ("# bytes decrypted"), size,
2136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2137 "Decrypted %u bytes from `%4s' using key %u, IV %u\n",
2138 (unsigned int) size, GNUNET_i2s (&n->peer),
2139 (unsigned int) n->decrypt_key.crc32, GNUNET_CRYPTO_crc32_n (iv,
2148 * Select messages for transmission. This heuristic uses a combination
2149 * of earliest deadline first (EDF) scheduling (with bounded horizon)
2150 * and priority-based discard (in case no feasible schedule exist) and
2151 * speculative optimization (defer any kind of transmission until
2152 * we either create a batch of significant size, 25% of max, or until
2153 * we are close to a deadline). Furthermore, when scheduling the
2154 * heuristic also packs as many messages into the batch as possible,
2155 * starting with those with the earliest deadline. Yes, this is fun.
2157 * @param n neighbour to select messages from
2158 * @param size number of bytes to select for transmission
2159 * @param retry_time set to the time when we should try again
2160 * (only valid if this function returns zero)
2161 * @return number of bytes selected, or 0 if we decided to
2162 * defer scheduling overall; in that case, retry_time is set.
2165 select_messages (struct Neighbour *n, size_t size,
2166 struct GNUNET_TIME_Relative *retry_time)
2168 struct MessageEntry *pos;
2169 struct MessageEntry *min;
2170 struct MessageEntry *last;
2171 unsigned int min_prio;
2172 struct GNUNET_TIME_Absolute t;
2173 struct GNUNET_TIME_Absolute now;
2174 struct GNUNET_TIME_Relative delta;
2176 struct GNUNET_TIME_Relative slack; /* how long could we wait before missing deadlines? */
2179 unsigned int queue_size;
2180 int discard_low_prio;
2182 GNUNET_assert (NULL != n->messages);
2183 now = GNUNET_TIME_absolute_get ();
2184 /* last entry in linked list of messages processed */
2186 /* should we remove the entry with the lowest
2187 * priority from consideration for scheduling at the
2188 * end of the loop? */
2198 discard_low_prio = GNUNET_YES;
2199 while (GNUNET_YES == discard_low_prio)
2202 min_prio = UINT_MAX;
2203 discard_low_prio = GNUNET_NO;
2204 /* calculate number of bytes available for transmission at time "t" */
2205 avail = GNUNET_BANDWIDTH_tracker_get_available (&n->available_send_window);
2207 /* how many bytes have we (hypothetically) scheduled so far */
2209 /* maximum time we can wait before transmitting anything
2210 * and still make all of our deadlines */
2211 slack = GNUNET_TIME_UNIT_FOREVER_REL;
2213 /* note that we use "*2" here because we want to look
2214 * a bit further into the future; much more makes no
2215 * sense since new message might be scheduled in the
2217 while ((pos != NULL) && (off < size * 2))
2219 if (pos->do_transmit == GNUNET_YES)
2221 /* already removed from consideration */
2225 if (discard_low_prio == GNUNET_NO)
2227 delta = GNUNET_TIME_absolute_get_difference (t, pos->deadline);
2228 if (delta.rel_value > 0)
2230 // FIXME: HUH? Check!
2233 GNUNET_BANDWIDTH_value_get_available_until (n->bw_out, delta);
2235 if (avail < pos->size)
2237 // FIXME: HUH? Check!
2238 discard_low_prio = GNUNET_YES; /* we could not schedule this one! */
2243 /* update slack, considering both its absolute deadline
2244 * and relative deadlines caused by other messages
2245 * with their respective load */
2247 GNUNET_TIME_relative_min (slack,
2248 GNUNET_BANDWIDTH_value_get_delay_for
2249 (n->bw_out, avail));
2250 if (pos->deadline.abs_value <= now.abs_value)
2253 slack = GNUNET_TIME_UNIT_ZERO;
2255 else if (GNUNET_YES == pos->got_slack)
2257 /* should be soon now! */
2259 GNUNET_TIME_relative_min (slack,
2260 GNUNET_TIME_absolute_get_remaining
2261 (pos->slack_deadline));
2266 GNUNET_TIME_relative_min (slack,
2267 GNUNET_TIME_absolute_get_difference
2268 (now, pos->deadline));
2269 pos->got_slack = GNUNET_YES;
2270 pos->slack_deadline =
2271 GNUNET_TIME_absolute_min (pos->deadline,
2272 GNUNET_TIME_relative_to_absolute
2273 (GNUNET_CONSTANTS_MAX_CORK_DELAY));
2278 t = GNUNET_TIME_absolute_max (pos->deadline, t); // HUH? Check!
2279 if (pos->priority <= min_prio)
2281 /* update min for discard */
2282 min_prio = pos->priority;
2287 if (discard_low_prio)
2289 GNUNET_assert (min != NULL);
2290 /* remove lowest-priority entry from consideration */
2291 min->do_transmit = GNUNET_YES; /* means: discard (for now) */
2295 /* guard against sending "tiny" messages with large headers without
2296 * urgent deadlines */
2297 if ((slack.rel_value > GNUNET_CONSTANTS_MAX_CORK_DELAY.rel_value) &&
2298 (size > 4 * off) && (queue_size <= MAX_PEER_QUEUE_SIZE - 2))
2300 /* less than 25% of message would be filled with deadlines still
2301 * being met if we delay by one second or more; so just wait for
2302 * more data; but do not wait longer than 1s (since we don't want
2303 * to delay messages for a really long time either). */
2304 *retry_time = GNUNET_CONSTANTS_MAX_CORK_DELAY;
2305 /* reset do_transmit values for next time */
2308 pos->do_transmit = GNUNET_NO;
2311 GNUNET_STATISTICS_update (stats,
2313 ("# transmissions delayed due to corking"), 1,
2316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2317 "Deferring transmission for %llums due to underfull message buffer size (%u/%u)\n",
2318 (unsigned long long) retry_time->rel_value, (unsigned int) off,
2319 (unsigned int) size);
2323 /* select marked messages (up to size) for transmission */
2328 if ((pos->size <= size) && (pos->do_transmit == GNUNET_NO))
2330 pos->do_transmit = GNUNET_YES; /* mark for transmission */
2334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2335 "Selecting message of size %u for transmission\n",
2336 (unsigned int) pos->size);
2342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2343 "Not selecting message of size %u for transmission at this time (maximum is %u)\n",
2344 (unsigned int) pos->size, size);
2346 pos->do_transmit = GNUNET_NO; /* mark for not transmitting! */
2351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2352 "Selected %llu/%llu bytes of %u/%u plaintext messages for transmission to `%4s'.\n",
2353 (unsigned long long) off, (unsigned long long) tsize, queue_size,
2354 (unsigned int) MAX_PEER_QUEUE_SIZE, GNUNET_i2s (&n->peer));
2361 * Batch multiple messages into a larger buffer.
2363 * @param n neighbour to take messages from
2364 * @param buf target buffer
2365 * @param size size of buf
2366 * @param deadline set to transmission deadline for the result
2367 * @param retry_time set to the time when we should try again
2368 * (only valid if this function returns zero)
2369 * @param priority set to the priority of the batch
2370 * @return number of bytes written to buf (can be zero)
2373 batch_message (struct Neighbour *n, char *buf, size_t size,
2374 struct GNUNET_TIME_Absolute *deadline,
2375 struct GNUNET_TIME_Relative *retry_time, unsigned int *priority)
2377 char ntmb[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
2378 struct NotifyTrafficMessage *ntm = (struct NotifyTrafficMessage *) ntmb;
2379 struct MessageEntry *pos;
2380 struct MessageEntry *prev;
2381 struct MessageEntry *next;
2386 *deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
2387 *retry_time = GNUNET_TIME_UNIT_FOREVER_REL;
2388 if (0 == select_messages (n, size, retry_time))
2391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2392 "No messages selected, will try again in %llu ms\n",
2393 retry_time->rel_value);
2397 ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND);
2398 ntm->ats_count = htonl (0);
2399 ntm->ats.type = htonl (0);
2400 ntm->ats.value = htonl (0);
2401 ntm->peer = n->peer;
2404 while ((pos != NULL) && (size >= sizeof (struct GNUNET_MessageHeader)))
2407 if (GNUNET_YES == pos->do_transmit)
2409 GNUNET_assert (pos->size <= size);
2410 /* do notifications */
2411 /* FIXME: track if we have *any* client that wants
2412 * full notifications and only do this if that is
2415 GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct NotifyTrafficMessage))
2417 memcpy (&ntm[1], &pos[1], pos->size);
2419 htons (sizeof (struct NotifyTrafficMessage) +
2420 sizeof (struct GNUNET_MessageHeader));
2421 send_to_all_clients (&ntm->header, GNUNET_YES,
2422 GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND);
2426 /* message too large for 'full' notifications, we do at
2427 * least the 'hdr' type */
2428 memcpy (&ntm[1], &pos[1], sizeof (struct GNUNET_MessageHeader));
2431 htons (sizeof (struct NotifyTrafficMessage) + pos->size);
2432 send_to_all_clients (&ntm->header, GNUNET_YES,
2433 GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND);
2435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2436 "Encrypting %u bytes with message of type %u and size %u\n",
2439 ntohs (((const struct GNUNET_MessageHeader *) &pos[1])->type),
2441 ntohs (((const struct GNUNET_MessageHeader *)
2444 /* copy for encrypted transmission */
2445 memcpy (&buf[ret], &pos[1], pos->size);
2448 *priority += pos->priority;
2450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2451 "Adding plaintext message of size %u with deadline %llu ms to batch\n",
2452 (unsigned int) pos->size,
2453 (unsigned long long)
2454 GNUNET_TIME_absolute_get_remaining (pos->deadline).rel_value);
2456 deadline->abs_value =
2457 GNUNET_MIN (deadline->abs_value, pos->deadline.abs_value);
2471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2472 "Deadline for message batch is %llu ms\n",
2473 GNUNET_TIME_absolute_get_remaining (*deadline).rel_value);
2480 * Remove messages with deadlines that have long expired from
2483 * @param n neighbour to inspect
2486 discard_expired_messages (struct Neighbour *n)
2488 struct MessageEntry *prev;
2489 struct MessageEntry *next;
2490 struct MessageEntry *pos;
2491 struct GNUNET_TIME_Absolute now;
2492 struct GNUNET_TIME_Relative delta;
2494 unsigned int queue_length;
2497 now = GNUNET_TIME_absolute_get ();
2505 delta = GNUNET_TIME_absolute_get_difference (pos->deadline, now);
2506 if (delta.rel_value > PAST_EXPIRATION_DISCARD_TIME.rel_value)
2509 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2510 "Message is %llu ms past due, discarding.\n",
2517 GNUNET_STATISTICS_update (stats,
2519 ("# messages discarded (expired prior to transmission)"),
2528 if ( (GNUNET_YES == disc) &&
2529 (queue_length == MAX_PEER_QUEUE_SIZE) )
2530 schedule_peer_messages (n);
2535 * Signature of the main function of a task.
2537 * @param cls closure
2538 * @param tc context information (why was this task triggered now)
2541 retry_plaintext_processing (void *cls,
2542 const struct GNUNET_SCHEDULER_TaskContext *tc)
2544 struct Neighbour *n = cls;
2546 n->retry_plaintext_task = GNUNET_SCHEDULER_NO_TASK;
2547 process_plaintext_neighbour_queue (n);
2552 * Send our key (and encrypted PING) to the other peer.
2554 * @param n the other peer
2557 send_key (struct Neighbour *n);
2560 * Task that will retry "send_key" if our previous attempt failed
2564 set_key_retry_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2566 struct Neighbour *n = cls;
2569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Retrying key transmission to `%4s'\n",
2570 GNUNET_i2s (&n->peer));
2572 n->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
2573 n->set_key_retry_frequency =
2574 GNUNET_TIME_relative_multiply (n->set_key_retry_frequency, 2);
2580 * Check if we have plaintext messages for the specified neighbour
2581 * pending, and if so, consider batching and encrypting them (and
2582 * then trigger processing of the encrypted queue if needed).
2584 * @param n neighbour to check.
2587 process_plaintext_neighbour_queue (struct Neighbour *n)
2589 char pbuf[GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE + sizeof (struct EncryptedMessage)]; /* plaintext */
2591 struct EncryptedMessage *em; /* encrypted message */
2592 struct EncryptedMessage *ph; /* plaintext header */
2593 struct MessageEntry *me;
2594 unsigned int priority;
2595 struct GNUNET_TIME_Absolute deadline;
2596 struct GNUNET_TIME_Relative retry_time;
2597 struct GNUNET_CRYPTO_AesInitializationVector iv;
2598 struct GNUNET_CRYPTO_AuthKey auth_key;
2600 if (n->retry_plaintext_task != GNUNET_SCHEDULER_NO_TASK)
2602 GNUNET_SCHEDULER_cancel (n->retry_plaintext_task);
2603 n->retry_plaintext_task = GNUNET_SCHEDULER_NO_TASK;
2607 case PEER_STATE_DOWN:
2610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2611 "Not yet connected to `%4s', deferring processing of plaintext messages.\n",
2612 GNUNET_i2s (&n->peer));
2615 case PEER_STATE_KEY_SENT:
2616 if (n->retry_set_key_task == GNUNET_SCHEDULER_NO_TASK)
2617 n->retry_set_key_task =
2618 GNUNET_SCHEDULER_add_delayed (n->set_key_retry_frequency,
2619 &set_key_retry_task, n);
2621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2622 "Not yet connected to `%4s', deferring processing of plaintext messages.\n",
2623 GNUNET_i2s (&n->peer));
2626 case PEER_STATE_KEY_RECEIVED:
2627 if (n->retry_set_key_task == GNUNET_SCHEDULER_NO_TASK)
2628 n->retry_set_key_task =
2629 GNUNET_SCHEDULER_add_delayed (n->set_key_retry_frequency,
2630 &set_key_retry_task, n);
2632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2633 "Not yet connected to `%4s', deferring processing of plaintext messages.\n",
2634 GNUNET_i2s (&n->peer));
2637 case PEER_STATE_KEY_CONFIRMED:
2638 /* ready to continue */
2641 discard_expired_messages (n);
2642 if (n->messages == NULL)
2645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2646 "Plaintext message queue for `%4s' is empty.\n",
2647 GNUNET_i2s (&n->peer));
2649 return; /* no pending messages */
2651 if (n->encrypted_head != NULL)
2654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2655 "Encrypted message queue for `%4s' is still full, delaying plaintext processing.\n",
2656 GNUNET_i2s (&n->peer));
2658 return; /* wait for messages already encrypted to be
2659 * processed first! */
2661 ph = (struct EncryptedMessage *) pbuf;
2662 deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
2664 used = sizeof (struct EncryptedMessage);
2666 batch_message (n, &pbuf[used],
2667 GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE, &deadline,
2668 &retry_time, &priority);
2669 if (used == sizeof (struct EncryptedMessage))
2672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2673 "No messages selected for transmission to `%4s' at this time, will try again later.\n",
2674 GNUNET_i2s (&n->peer));
2676 /* no messages selected for sending, try again later... */
2677 n->retry_plaintext_task =
2678 GNUNET_SCHEDULER_add_delayed (retry_time, &retry_plaintext_processing,
2682 #if DEBUG_CORE_QUOTA
2683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2684 "Sending %u b/s as new limit to peer `%4s'\n",
2685 (unsigned int) ntohl (n->bw_in.value__), GNUNET_i2s (&n->peer));
2688 htonl (GNUNET_CRYPTO_random_u32
2689 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
2690 ph->sequence_number = htonl (++n->last_sequence_number_sent);
2691 ph->inbound_bw_limit = n->bw_in;
2692 ph->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
2694 /* setup encryption message header */
2695 me = GNUNET_malloc (sizeof (struct MessageEntry) + used);
2696 me->deadline = deadline;
2697 me->priority = priority;
2699 em = (struct EncryptedMessage *) &me[1];
2700 em->header.size = htons (used);
2701 em->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE);
2702 em->iv_seed = ph->iv_seed;
2703 derive_iv (&iv, &n->encrypt_key, ph->iv_seed, &n->peer);
2706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2707 "Encrypting %u bytes of plaintext messages for `%4s' for transmission in %llums.\n",
2708 (unsigned int) used - ENCRYPTED_HEADER_SIZE,
2709 GNUNET_i2s (&n->peer),
2710 (unsigned long long)
2711 GNUNET_TIME_absolute_get_remaining (deadline).rel_value);
2713 GNUNET_assert (GNUNET_OK ==
2714 do_encrypt (n, &iv, &ph->sequence_number, &em->sequence_number,
2715 used - ENCRYPTED_HEADER_SIZE));
2716 derive_auth_key (&auth_key, &n->encrypt_key, ph->iv_seed,
2717 n->encrypt_key_created);
2718 GNUNET_CRYPTO_hmac (&auth_key, &em->sequence_number,
2719 used - ENCRYPTED_HEADER_SIZE, &em->hmac);
2721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2722 "Authenticated %u bytes of ciphertext %u: `%s'\n",
2723 used - ENCRYPTED_HEADER_SIZE,
2724 GNUNET_CRYPTO_crc32_n (&em->sequence_number,
2725 used - ENCRYPTED_HEADER_SIZE),
2726 GNUNET_h2s (&em->hmac));
2728 /* append to transmission list */
2729 GNUNET_CONTAINER_DLL_insert_after (n->encrypted_head, n->encrypted_tail,
2730 n->encrypted_tail, me);
2731 process_encrypted_neighbour_queue (n);
2732 schedule_peer_messages (n);
2737 * Function that recalculates the bandwidth quota for the
2738 * given neighbour and transmits it to the transport service.
2740 * @param cls neighbour for the quota update
2744 neighbour_quota_update (void *cls,
2745 const struct GNUNET_SCHEDULER_TaskContext *tc);
2749 * Schedule the task that will recalculate the bandwidth
2750 * quota for this peer (and possibly force a disconnect of
2751 * idle peers by calculating a bandwidth of zero).
2754 schedule_quota_update (struct Neighbour *n)
2756 GNUNET_assert (n->quota_update_task == GNUNET_SCHEDULER_NO_TASK);
2757 n->quota_update_task =
2758 GNUNET_SCHEDULER_add_delayed (QUOTA_UPDATE_FREQUENCY,
2759 &neighbour_quota_update, n);
2764 * Initialize a new 'struct Neighbour'.
2766 * @param pid ID of the new neighbour
2767 * @return handle for the new neighbour
2769 static struct Neighbour *
2770 create_neighbour (const struct GNUNET_PeerIdentity *pid)
2772 struct Neighbour *n;
2773 struct GNUNET_TIME_Absolute now;
2776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2777 "Creating neighbour entry for peer `%4s'\n", GNUNET_i2s (pid));
2779 n = GNUNET_malloc (sizeof (struct Neighbour));
2781 GNUNET_CRYPTO_aes_create_session_key (&n->encrypt_key);
2782 now = GNUNET_TIME_absolute_get ();
2783 n->encrypt_key_created = now;
2784 n->last_activity = now;
2785 n->set_key_retry_frequency = INITIAL_SET_KEY_RETRY_FREQUENCY;
2786 n->bw_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
2787 n->bw_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
2788 n->bw_out_internal_limit = GNUNET_BANDWIDTH_value_init (UINT32_MAX);
2789 n->bw_out_external_limit = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
2791 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
2792 GNUNET_assert (GNUNET_OK ==
2793 GNUNET_CONTAINER_multihashmap_put (neighbours,
2794 &n->peer.hashPubKey, n,
2795 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2796 GNUNET_STATISTICS_set (stats, gettext_noop ("# neighbour entries allocated"),
2797 GNUNET_CONTAINER_multihashmap_size (neighbours),
2799 neighbour_quota_update (n, NULL);
2800 consider_free_neighbour (n);
2806 * Handle CORE_SEND request.
2809 * @param client the client issuing the request
2810 * @param message the "struct SendMessage"
2813 handle_client_send (void *cls, struct GNUNET_SERVER_Client *client,
2814 const struct GNUNET_MessageHeader *message)
2816 const struct SendMessage *sm;
2817 struct Neighbour *n;
2818 struct MessageEntry *prev;
2819 struct MessageEntry *pos;
2820 struct MessageEntry *e;
2821 struct MessageEntry *min_prio_entry;
2822 struct MessageEntry *min_prio_prev;
2823 unsigned int min_prio;
2824 unsigned int queue_size;
2827 msize = ntohs (message->size);
2829 sizeof (struct SendMessage) + sizeof (struct GNUNET_MessageHeader))
2831 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2832 "msize is %u, should be at least %u (in %s:%d)\n", msize,
2833 sizeof (struct SendMessage) +
2834 sizeof (struct GNUNET_MessageHeader), __FILE__, __LINE__);
2837 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2840 sm = (const struct SendMessage *) message;
2841 msize -= sizeof (struct SendMessage);
2843 memcmp (&sm->peer, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
2846 GNUNET_SERVER_mst_receive (mst, &self, (const char *) &sm[1], msize,
2847 GNUNET_YES, GNUNET_NO);
2849 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2852 n = find_neighbour (&sm->peer);
2853 if ((n == NULL) || (GNUNET_YES != n->is_connected) ||
2854 (n->status != PEER_STATE_KEY_CONFIRMED))
2856 /* attempt to send message to peer that is not connected anymore
2857 * (can happen due to asynchrony) */
2858 GNUNET_STATISTICS_update (stats,
2860 ("# messages discarded (disconnected)"), 1,
2863 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2868 "Core received `%s' request, queueing %u bytes of plaintext data for transmission to `%4s'.\n",
2869 "SEND", (unsigned int) msize, GNUNET_i2s (&sm->peer));
2871 discard_expired_messages (n);
2872 /* bound queue size */
2873 /* NOTE: this entire block to bound the queue size should be
2874 * obsolete with the new client-request code and the
2875 * 'schedule_peer_messages' mechanism; we still have this code in
2876 * here for now as a sanity check for the new mechanmism;
2877 * ultimately, we should probably simply reject SEND messages that
2878 * are not 'approved' (or provide a new core API for very unreliable
2879 * delivery that always sends with priority 0). Food for thought. */
2880 min_prio = UINT32_MAX;
2881 min_prio_entry = NULL;
2882 min_prio_prev = NULL;
2888 if (pos->priority <= min_prio)
2890 min_prio_entry = pos;
2891 min_prio_prev = prev;
2892 min_prio = pos->priority;
2898 if (queue_size >= MAX_PEER_QUEUE_SIZE)
2901 if (ntohl (sm->priority) <= min_prio)
2903 /* discard new entry; this should no longer happen! */
2906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2907 "Queue full (%u/%u), discarding new request (%u bytes of type %u)\n",
2908 queue_size, (unsigned int) MAX_PEER_QUEUE_SIZE,
2909 (unsigned int) msize, (unsigned int) ntohs (message->type));
2911 GNUNET_STATISTICS_update (stats,
2912 gettext_noop ("# discarded CORE_SEND requests"),
2916 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2919 GNUNET_assert (min_prio_entry != NULL);
2920 /* discard "min_prio_entry" */
2922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2923 "Queue full, discarding existing older request\n");
2925 GNUNET_STATISTICS_update (stats,
2927 ("# discarded lower priority CORE_SEND requests"),
2929 if (min_prio_prev == NULL)
2930 n->messages = min_prio_entry->next;
2932 min_prio_prev->next = min_prio_entry->next;
2933 GNUNET_free (min_prio_entry);
2937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2938 "Adding transmission request for `%4s' of size %u to queue\n",
2939 GNUNET_i2s (&sm->peer), (unsigned int) msize);
2941 GNUNET_break (0 == ntohl (sm->reserved));
2942 e = GNUNET_malloc (sizeof (struct MessageEntry) + msize);
2943 e->deadline = GNUNET_TIME_absolute_ntoh (sm->deadline);
2944 e->priority = ntohl (sm->priority);
2946 if (GNUNET_YES != (int) ntohl (sm->cork))
2947 e->got_slack = GNUNET_YES;
2948 memcpy (&e[1], &sm[1], msize);
2950 /* insert, keep list sorted by deadline */
2953 while ((pos != NULL) && (pos->deadline.abs_value < e->deadline.abs_value))
2964 /* consider scheduling now */
2965 process_plaintext_neighbour_queue (n);
2967 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2972 * PEERINFO is giving us a HELLO for a peer. Add the public key to
2973 * the neighbour's struct and retry send_key. Or, if we did not get a
2974 * HELLO, just do nothing.
2976 * @param cls the 'struct Neighbour' to retry sending the key for
2977 * @param peer the peer for which this is the HELLO
2978 * @param hello HELLO message of that peer
2979 * @param err_msg NULL if successful, otherwise contains error message
2982 process_hello_retry_send_key (void *cls, const struct GNUNET_PeerIdentity *peer,
2983 const struct GNUNET_HELLO_Message *hello,
2984 const char *err_msg)
2986 struct Neighbour *n = cls;
2988 if (err_msg != NULL)
2990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2991 _("Error in communication with PEERINFO service\n"));
2998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Entered `%s' and `%s' is NULL!\n",
2999 "process_hello_retry_send_key", "peer");
3002 if (n->public_key != NULL)
3004 if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
3006 GNUNET_SCHEDULER_cancel (n->retry_set_key_task);
3007 n->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
3009 GNUNET_STATISTICS_update (stats,
3011 ("# SET_KEY messages deferred (need public key)"),
3018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3019 "Failed to obtain public key for peer `%4s', delaying processing of SET_KEY\n",
3020 GNUNET_i2s (&n->peer));
3022 GNUNET_STATISTICS_update (stats,
3024 ("# Delayed connecting due to lack of public key"),
3026 if (GNUNET_SCHEDULER_NO_TASK == n->retry_set_key_task)
3027 n->retry_set_key_task =
3028 GNUNET_SCHEDULER_add_delayed (n->set_key_retry_frequency,
3029 &set_key_retry_task, n);
3035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Entered `%s' for peer `%4s'\n",
3036 "process_hello_retry_send_key", GNUNET_i2s (peer));
3038 if (n->public_key != NULL)
3040 /* already have public key, why are we here? */
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3047 "Received new `%s' message for `%4s', initiating key exchange.\n",
3048 "HELLO", GNUNET_i2s (peer));
3051 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3052 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, n->public_key))
3054 GNUNET_STATISTICS_update (stats,
3056 ("# Error extracting public key from HELLO"), 1,
3058 GNUNET_free (n->public_key);
3059 n->public_key = NULL;
3061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3062 "GNUNET_HELLO_get_key returned awfully\n");
3070 * Send our key (and encrypted PING) to the other peer.
3072 * @param n the other peer
3075 send_key (struct Neighbour *n)
3077 struct MessageEntry *pos;
3078 struct SetKeyMessage *sm;
3079 struct MessageEntry *me;
3080 struct PingMessage pp;
3081 struct PingMessage *pm;
3082 struct GNUNET_CRYPTO_AesInitializationVector iv;
3084 if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
3086 GNUNET_SCHEDULER_cancel (n->retry_set_key_task);
3087 n->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
3089 if (n->pitr != NULL)
3092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3093 "Key exchange in progress with `%4s'.\n",
3094 GNUNET_i2s (&n->peer));
3096 return; /* already in progress */
3098 if (GNUNET_YES != n->is_connected)
3100 GNUNET_STATISTICS_update (stats,
3102 ("# Asking transport to connect (for SET_KEY)"),
3104 GNUNET_TRANSPORT_try_connect (transport, &n->peer);
3108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3109 "Asked to perform key exchange with `%4s'.\n",
3110 GNUNET_i2s (&n->peer));
3112 if (n->public_key == NULL)
3114 /* lookup n's public key, then try again */
3116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3117 "Lacking public key for `%4s', trying to obtain one (send_key).\n",
3118 GNUNET_i2s (&n->peer));
3120 GNUNET_assert (n->pitr == NULL);
3122 GNUNET_PEERINFO_iterate (peerinfo, &n->peer,
3123 GNUNET_TIME_relative_multiply
3124 (GNUNET_TIME_UNIT_SECONDS, 20),
3125 &process_hello_retry_send_key, n);
3128 pos = n->encrypted_head;
3131 if (GNUNET_YES == pos->is_setkey)
3133 if (pos->sender_status == n->status)
3136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3137 "`%s' message for `%4s' queued already\n", "SET_KEY",
3138 GNUNET_i2s (&n->peer));
3140 goto trigger_processing;
3142 GNUNET_CONTAINER_DLL_remove (n->encrypted_head, n->encrypted_tail, pos);
3145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3146 "Removing queued `%s' message for `%4s', will create a new one\n",
3147 "SET_KEY", GNUNET_i2s (&n->peer));
3157 case PEER_STATE_DOWN:
3158 n->status = PEER_STATE_KEY_SENT;
3160 case PEER_STATE_KEY_SENT:
3162 case PEER_STATE_KEY_RECEIVED:
3164 case PEER_STATE_KEY_CONFIRMED:
3172 /* first, set key message */
3173 me = GNUNET_malloc (sizeof (struct MessageEntry) +
3174 sizeof (struct SetKeyMessage) +
3175 sizeof (struct PingMessage));
3176 me->deadline = GNUNET_TIME_relative_to_absolute (MAX_SET_KEY_DELAY);
3177 me->priority = SET_KEY_PRIORITY;
3178 me->size = sizeof (struct SetKeyMessage) + sizeof (struct PingMessage);
3179 me->is_setkey = GNUNET_YES;
3180 me->got_slack = GNUNET_YES; /* do not defer this one! */
3181 me->sender_status = n->status;
3182 GNUNET_CONTAINER_DLL_insert_after (n->encrypted_head, n->encrypted_tail,
3183 n->encrypted_tail, me);
3184 sm = (struct SetKeyMessage *) &me[1];
3185 sm->header.size = htons (sizeof (struct SetKeyMessage));
3186 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SET_KEY);
3190 PEER_STATE_DOWN) ? PEER_STATE_KEY_SENT : n->status));
3192 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
3193 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
3194 sizeof (struct GNUNET_CRYPTO_RsaEncryptedData) +
3195 sizeof (struct GNUNET_PeerIdentity));
3196 sm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_SET_KEY);
3197 sm->creation_time = GNUNET_TIME_absolute_hton (n->encrypt_key_created);
3198 sm->target = n->peer;
3199 GNUNET_assert (GNUNET_OK ==
3200 GNUNET_CRYPTO_rsa_encrypt (&n->encrypt_key,
3202 GNUNET_CRYPTO_AesSessionKey),
3203 n->public_key, &sm->encrypted_key));
3204 GNUNET_assert (GNUNET_OK ==
3205 GNUNET_CRYPTO_rsa_sign (my_private_key, &sm->purpose,
3207 pm = (struct PingMessage *) &sm[1];
3208 pm->header.size = htons (sizeof (struct PingMessage));
3209 pm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PING);
3211 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
3212 derive_iv (&iv, &n->encrypt_key, pm->iv_seed, &n->peer);
3213 pp.challenge = n->ping_challenge;
3214 pp.target = n->peer;
3216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3217 "Encrypting `%s' and `%s' messages with challenge %u for `%4s' using key %u, IV %u (salt %u).\n",
3218 "SET_KEY", "PING", (unsigned int) n->ping_challenge,
3219 GNUNET_i2s (&n->peer), (unsigned int) n->encrypt_key.crc32,
3220 GNUNET_CRYPTO_crc32_n (&iv, sizeof (iv)), pm->iv_seed);
3222 do_encrypt (n, &iv, &pp.target, &pm->target,
3223 sizeof (struct PingMessage) - ((void *) &pm->target -
3225 GNUNET_STATISTICS_update (stats,
3227 ("# SET_KEY and PING messages created"), 1,
3230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3231 "Have %llu ms left for `%s' transmission.\n",
3232 (unsigned long long)
3233 GNUNET_TIME_absolute_get_remaining (me->deadline).rel_value,
3237 /* trigger queue processing */
3238 process_encrypted_neighbour_queue (n);
3239 if ((n->status != PEER_STATE_KEY_CONFIRMED) &&
3240 (GNUNET_SCHEDULER_NO_TASK == n->retry_set_key_task))
3241 n->retry_set_key_task =
3242 GNUNET_SCHEDULER_add_delayed (n->set_key_retry_frequency,
3243 &set_key_retry_task, n);
3248 * We received a SET_KEY message. Validate and update
3249 * our key material and status.
3251 * @param n the neighbour from which we received message m
3252 * @param m the set key message we received
3253 * @param ats performance data
3254 * @param ats_count number of entries in ats (excluding 0-termination)
3257 handle_set_key (struct Neighbour *n, const struct SetKeyMessage *m,
3258 const struct GNUNET_TRANSPORT_ATS_Information *ats,
3259 uint32_t ats_count);
3264 * PEERINFO is giving us a HELLO for a peer. Add the public key to
3265 * the neighbour's struct and retry handling the set_key message. Or,
3266 * if we did not get a HELLO, just free the set key message.
3268 * @param cls pointer to the set key message
3269 * @param peer the peer for which this is the HELLO
3270 * @param hello HELLO message of that peer
3271 * @param err_msg NULL if successful, otherwise contains error message
3274 process_hello_retry_handle_set_key (void *cls,
3275 const struct GNUNET_PeerIdentity *peer,
3276 const struct GNUNET_HELLO_Message *hello,
3277 const char *err_msg)
3279 struct Neighbour *n = cls;
3280 struct SetKeyMessage *sm = n->skm;
3282 if (err_msg != NULL)
3284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3285 _("Error in communication with PEERINFO service\n"));
3293 if (n->public_key != NULL)
3296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3297 "Received `%s' for `%4s', continuing processing of `%s' message.\n",
3298 "HELLO", GNUNET_i2s (&n->peer), "SET_KEY");
3300 handle_set_key (n, sm, NULL, 0);
3304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3306 ("Ignoring `%s' message due to lack of public key for peer `%4s' (failed to obtain one).\n"),
3307 "SET_KEY", GNUNET_i2s (&n->peer));
3312 if (n->public_key != NULL)
3313 return; /* multiple HELLOs match!? */
3315 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
3316 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, n->public_key))
3318 GNUNET_break_op (0);
3319 GNUNET_free (n->public_key);
3320 n->public_key = NULL;
3326 * Merge the given performance data with the data we currently
3327 * track for the given neighbour.
3329 * @param n neighbour
3330 * @param ats new performance data
3331 * @param ats_count number of records in ats
3334 update_neighbour_performance (struct Neighbour *n,
3335 const struct GNUNET_TRANSPORT_ATS_Information
3336 *ats, uint32_t ats_count)
3343 for (i = 0; i < ats_count; i++)
3345 for (j = 0; j < n->ats_count; j++)
3347 if (n->ats[j].type == ats[i].type)
3349 n->ats[j].value = ats[i].value;
3353 if (j == n->ats_count)
3355 GNUNET_array_append (n->ats, n->ats_count, ats[i]);
3362 * We received a PING message. Validate and transmit
3365 * @param n sender of the PING
3366 * @param m the encrypted PING message itself
3367 * @param ats performance data
3368 * @param ats_count number of entries in ats (excluding 0-termination)
3371 handle_ping (struct Neighbour *n, const struct PingMessage *m,
3372 const struct GNUNET_TRANSPORT_ATS_Information *ats,
3375 struct PingMessage t;
3376 struct PongMessage tx;
3377 struct PongMessage *tp;
3378 struct MessageEntry *me;
3379 struct GNUNET_CRYPTO_AesInitializationVector iv;
3382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3383 "Core service receives `%s' request from `%4s'.\n", "PING",
3384 GNUNET_i2s (&n->peer));
3386 derive_iv (&iv, &n->decrypt_key, m->iv_seed, &my_identity);
3388 do_decrypt (n, &iv, &m->target, &t.target,
3389 sizeof (struct PingMessage) - ((void *) &m->target -
3393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3394 "Decrypted `%s' to `%4s' with challenge %u decrypted using key %u, IV %u (salt %u)\n",
3395 "PING", GNUNET_i2s (&t.target), (unsigned int) t.challenge,
3396 (unsigned int) n->decrypt_key.crc32, GNUNET_CRYPTO_crc32_n (&iv,
3401 GNUNET_STATISTICS_update (stats, gettext_noop ("# PING messages decrypted"),
3404 memcmp (&t.target, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
3409 GNUNET_snprintf (sender, sizeof (sender), "%8s", GNUNET_i2s (&n->peer));
3410 GNUNET_snprintf (peer, sizeof (peer), "%8s", GNUNET_i2s (&t.target));
3411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3413 ("Received PING from `%s' for different identity: I am `%s', PONG identity: `%s'\n"),
3414 sender, GNUNET_i2s (&my_identity), peer);
3415 GNUNET_break_op (0);
3418 update_neighbour_performance (n, ats, ats_count);
3419 me = GNUNET_malloc (sizeof (struct MessageEntry) +
3420 sizeof (struct PongMessage));
3421 GNUNET_CONTAINER_DLL_insert_after (n->encrypted_head, n->encrypted_tail,
3422 n->encrypted_tail, me);
3423 me->deadline = GNUNET_TIME_relative_to_absolute (MAX_PONG_DELAY);
3424 me->priority = PONG_PRIORITY;
3425 me->size = sizeof (struct PongMessage);
3426 tx.inbound_bw_limit = n->bw_in;
3427 tx.challenge = t.challenge;
3428 tx.target = t.target;
3429 tp = (struct PongMessage *) &me[1];
3430 tp->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PONG);
3431 tp->header.size = htons (sizeof (struct PongMessage));
3433 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
3434 derive_pong_iv (&iv, &n->encrypt_key, tp->iv_seed, t.challenge, &n->peer);
3435 do_encrypt (n, &iv, &tx.challenge, &tp->challenge,
3436 sizeof (struct PongMessage) - ((void *) &tp->challenge -
3438 GNUNET_STATISTICS_update (stats, gettext_noop ("# PONG messages created"), 1,
3441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3442 "Encrypting `%s' with challenge %u using key %u, IV %u (salt %u)\n",
3443 "PONG", (unsigned int) t.challenge,
3444 (unsigned int) n->encrypt_key.crc32, GNUNET_CRYPTO_crc32_n (&iv,
3449 /* trigger queue processing */
3450 process_encrypted_neighbour_queue (n);
3455 * We received a PONG message. Validate and update our status.
3457 * @param n sender of the PONG
3458 * @param m the encrypted PONG message itself
3459 * @param ats performance data
3460 * @param ats_count number of entries in ats (excluding 0-termination)
3463 handle_pong (struct Neighbour *n, const struct PongMessage *m,
3464 const struct GNUNET_TRANSPORT_ATS_Information *ats,
3467 struct PongMessage t;
3468 struct ConnectNotifyMessage *cnm;
3469 struct GNUNET_CRYPTO_AesInitializationVector iv;
3470 char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
3471 struct GNUNET_TRANSPORT_ATS_Information *mats;
3475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3476 "Core service receives `%s' response from `%4s'.\n", "PONG",
3477 GNUNET_i2s (&n->peer));
3479 /* mark as garbage, just to be sure */
3480 memset (&t, 255, sizeof (t));
3481 derive_pong_iv (&iv, &n->decrypt_key, m->iv_seed, n->ping_challenge,
3484 do_decrypt (n, &iv, &m->challenge, &t.challenge,
3485 sizeof (struct PongMessage) - ((void *) &m->challenge -
3488 GNUNET_break_op (0);
3491 GNUNET_STATISTICS_update (stats, gettext_noop ("# PONG messages decrypted"),
3494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3495 "Decrypted `%s' from `%4s' with challenge %u using key %u, IV %u (salt %u)\n",
3496 "PONG", GNUNET_i2s (&t.target), (unsigned int) t.challenge,
3497 (unsigned int) n->decrypt_key.crc32, GNUNET_CRYPTO_crc32_n (&iv,
3502 if ((0 != memcmp (&t.target, &n->peer, sizeof (struct GNUNET_PeerIdentity)))
3503 || (n->ping_challenge != t.challenge))
3505 /* PONG malformed */
3507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3508 "Received malformed `%s' wanted sender `%4s' with challenge %u\n",
3509 "PONG", GNUNET_i2s (&n->peer),
3510 (unsigned int) n->ping_challenge);
3511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3512 "Received malformed `%s' received from `%4s' with challenge %u\n",
3513 "PONG", GNUNET_i2s (&t.target), (unsigned int) t.challenge);
3515 GNUNET_break_op (n->ping_challenge != t.challenge);
3520 case PEER_STATE_DOWN:
3521 GNUNET_break (0); /* should be impossible */
3523 case PEER_STATE_KEY_SENT:
3524 GNUNET_break (0); /* should be impossible, how did we decrypt? */
3526 case PEER_STATE_KEY_RECEIVED:
3527 GNUNET_STATISTICS_update (stats,
3529 ("# Session keys confirmed via PONG"), 1,
3531 n->status = PEER_STATE_KEY_CONFIRMED;
3533 struct GNUNET_MessageHeader *hdr;
3535 hdr = compute_type_map_message ();
3536 send_type_map_to_neighbour (hdr, &n->peer.hashPubKey, n);
3539 if (n->bw_out_external_limit.value__ != t.inbound_bw_limit.value__)
3541 n->bw_out_external_limit = t.inbound_bw_limit;
3543 GNUNET_BANDWIDTH_value_min (n->bw_out_external_limit,
3544 n->bw_out_internal_limit);
3545 GNUNET_BANDWIDTH_tracker_update_quota (&n->available_send_window,
3547 GNUNET_TRANSPORT_set_quota (transport, &n->peer, n->bw_in, n->bw_out);
3550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3551 "Confirmed key via `%s' message for peer `%4s'\n", "PONG",
3552 GNUNET_i2s (&n->peer));
3554 if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
3556 GNUNET_SCHEDULER_cancel (n->retry_set_key_task);
3557 n->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
3559 update_neighbour_performance (n, ats, ats_count);
3561 sizeof (struct ConnectNotifyMessage) +
3562 (n->ats_count) * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
3563 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
3566 /* recovery strategy: throw away performance data */
3567 GNUNET_array_grow (n->ats, n->ats_count, 0);
3569 sizeof (struct PeerStatusNotifyMessage) +
3570 n->ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
3572 cnm = (struct ConnectNotifyMessage *) buf;
3573 cnm->header.size = htons (size);
3574 cnm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
3575 cnm->ats_count = htonl (n->ats_count);
3576 cnm->peer = n->peer;
3578 memcpy (mats, n->ats,
3579 n->ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
3580 mats[n->ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
3581 mats[n->ats_count].value = htonl (0);
3582 send_to_all_clients (&cnm->header, GNUNET_NO,
3583 GNUNET_CORE_OPTION_SEND_CONNECT);
3584 process_encrypted_neighbour_queue (n);
3586 case PEER_STATE_KEY_CONFIRMED:
3587 n->last_activity = GNUNET_TIME_absolute_get ();
3588 if (n->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
3589 GNUNET_SCHEDULER_cancel (n->keep_alive_task);
3590 n->keep_alive_task =
3591 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
3592 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
3593 2), &send_keep_alive, n);
3594 handle_peer_status_change (n);
3604 * We received a SET_KEY message. Validate and update
3605 * our key material and status.
3607 * @param n the neighbour from which we received message m
3608 * @param m the set key message we received
3609 * @param ats performance data
3610 * @param ats_count number of entries in ats (excluding 0-termination)
3613 handle_set_key (struct Neighbour *n, const struct SetKeyMessage *m,
3614 const struct GNUNET_TRANSPORT_ATS_Information *ats,
3617 struct SetKeyMessage *m_cpy;
3618 struct GNUNET_TIME_Absolute t;
3619 struct GNUNET_CRYPTO_AesSessionKey k;
3620 struct PingMessage *ping;
3621 struct PongMessage *pong;
3622 enum PeerStateMachine sender_status;
3625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3626 "Core service receives `%s' request from `%4s'.\n", "SET_KEY",
3627 GNUNET_i2s (&n->peer));
3629 if (n->public_key == NULL)
3631 if (n->pitr != NULL)
3634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3635 "Ignoring `%s' message due to lack of public key for peer (still trying to obtain one).\n",
3641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3642 "Lacking public key for peer, trying to obtain one (handle_set_key).\n");
3644 m_cpy = GNUNET_malloc (sizeof (struct SetKeyMessage));
3645 memcpy (m_cpy, m, sizeof (struct SetKeyMessage));
3646 /* lookup n's public key, then try again */
3647 GNUNET_assert (n->skm == NULL);
3650 GNUNET_PEERINFO_iterate (peerinfo, &n->peer, GNUNET_TIME_UNIT_MINUTES,
3651 &process_hello_retry_handle_set_key, n);
3652 GNUNET_STATISTICS_update (stats,
3654 ("# SET_KEY messages deferred (need public key)"),
3659 memcmp (&m->target, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
3661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3663 ("Received `%s' message that was for `%s', not for me. Ignoring.\n"),
3664 "SET_KEY", GNUNET_i2s (&m->target));
3667 if ((ntohl (m->purpose.size) !=
3668 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
3669 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
3670 sizeof (struct GNUNET_CRYPTO_RsaEncryptedData) +
3671 sizeof (struct GNUNET_PeerIdentity)) ||
3673 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_SET_KEY, &m->purpose,
3674 &m->signature, n->public_key)))
3676 /* invalid signature */
3677 GNUNET_break_op (0);
3680 t = GNUNET_TIME_absolute_ntoh (m->creation_time);
3681 if (((n->status == PEER_STATE_KEY_RECEIVED) ||
3682 (n->status == PEER_STATE_KEY_CONFIRMED)) &&
3683 (t.abs_value < n->decrypt_key_created.abs_value))
3685 /* this could rarely happen due to massive re-ordering of
3686 * messages on the network level, but is most likely either
3687 * a bug or some adversary messing with us. Report. */
3688 GNUNET_break_op (0);
3692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Decrypting key material.\n");
3694 if ((GNUNET_CRYPTO_rsa_decrypt
3695 (my_private_key, &m->encrypted_key, &k,
3696 sizeof (struct GNUNET_CRYPTO_AesSessionKey)) !=
3697 sizeof (struct GNUNET_CRYPTO_AesSessionKey)) ||
3698 (GNUNET_OK != GNUNET_CRYPTO_aes_check_session_key (&k)))
3700 /* failed to decrypt !? */
3701 GNUNET_break_op (0);
3704 GNUNET_STATISTICS_update (stats,
3705 gettext_noop ("# SET_KEY messages decrypted"), 1,
3708 if (n->decrypt_key_created.abs_value != t.abs_value)
3710 /* fresh key, reset sequence numbers */
3711 n->last_sequence_number_received = 0;
3712 n->last_packets_bitmap = 0;
3713 n->decrypt_key_created = t;
3715 update_neighbour_performance (n, ats, ats_count);
3716 sender_status = (enum PeerStateMachine) ntohl (m->sender_status);
3719 case PEER_STATE_DOWN:
3720 n->status = PEER_STATE_KEY_RECEIVED;
3722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3723 "Responding to `%s' with my own key.\n", "SET_KEY");
3727 case PEER_STATE_KEY_SENT:
3728 case PEER_STATE_KEY_RECEIVED:
3729 n->status = PEER_STATE_KEY_RECEIVED;
3730 if ((sender_status != PEER_STATE_KEY_RECEIVED) &&
3731 (sender_status != PEER_STATE_KEY_CONFIRMED))
3734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3735 "Responding to `%s' with my own key (other peer has status %u).\n",
3736 "SET_KEY", (unsigned int) sender_status);
3741 case PEER_STATE_KEY_CONFIRMED:
3742 if ((sender_status != PEER_STATE_KEY_RECEIVED) &&
3743 (sender_status != PEER_STATE_KEY_CONFIRMED))
3746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3747 "Responding to `%s' with my own key (other peer has status %u), I was already fully up.\n",
3748 "SET_KEY", (unsigned int) sender_status);
3757 if (n->pending_ping != NULL)
3759 ping = n->pending_ping;
3760 n->pending_ping = NULL;
3761 handle_ping (n, ping, NULL, 0);
3764 if (n->pending_pong != NULL)
3766 pong = n->pending_pong;
3767 n->pending_pong = NULL;
3768 handle_pong (n, pong, NULL, 0);
3775 * Send a P2P message to a client.
3777 * @param sender who sent us the message?
3778 * @param client who should we give the message to?
3779 * @param m contains the message to transmit
3780 * @param msize number of bytes in buf to transmit
3783 send_p2p_message_to_client (struct Neighbour *sender, struct Client *client,
3784 const void *m, size_t msize)
3787 msize + sizeof (struct NotifyTrafficMessage) +
3788 (sender->ats_count) * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
3790 struct NotifyTrafficMessage *ntm;
3791 struct GNUNET_TRANSPORT_ATS_Information *ats;
3793 GNUNET_assert (GNUNET_YES == sender->is_connected);
3794 GNUNET_break (sender->status == PEER_STATE_KEY_CONFIRMED);
3795 if (size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
3798 /* recovery strategy: throw performance data away... */
3799 GNUNET_array_grow (sender->ats, sender->ats_count, 0);
3801 msize + sizeof (struct NotifyTrafficMessage) +
3802 (sender->ats_count) * sizeof (struct GNUNET_TRANSPORT_ATS_Information);
3805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3806 "Core service passes message from `%4s' of type %u to client.\n",
3807 GNUNET_i2s (&sender->peer),
3809 ntohs (((const struct GNUNET_MessageHeader *) m)->type));
3811 ntm = (struct NotifyTrafficMessage *) buf;
3812 ntm->header.size = htons (size);
3813 ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND);
3814 ntm->ats_count = htonl (sender->ats_count);
3815 ntm->peer = sender->peer;
3817 memcpy (ats, sender->ats,
3818 sizeof (struct GNUNET_TRANSPORT_ATS_Information) * sender->ats_count);
3819 ats[sender->ats_count].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
3820 ats[sender->ats_count].value = htonl (0);
3821 memcpy (&ats[sender->ats_count + 1], m, msize);
3822 send_to_client (client, &ntm->header, GNUNET_YES);
3827 * Deliver P2P message to interested clients.
3829 * @param cls always NULL
3830 * @param client who sent us the message (struct Neighbour)
3831 * @param m the message
3834 deliver_message (void *cls, void *client, const struct GNUNET_MessageHeader *m)
3836 struct Neighbour *sender = client;
3837 size_t msize = ntohs (m->size);
3839 struct Client *cpos;
3845 GNUNET_break (sender->status == PEER_STATE_KEY_CONFIRMED);
3846 type = ntohs (m->type);
3848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3849 "Received encapsulated message of type %u and size %u from `%4s'\n",
3850 (unsigned int) type, ntohs (m->size), GNUNET_i2s (&sender->peer));
3852 GNUNET_snprintf (buf, sizeof (buf),
3853 gettext_noop ("# bytes of messages of type %u received"),
3854 (unsigned int) type);
3855 GNUNET_STATISTICS_update (stats, buf, msize, GNUNET_NO);
3856 if ((GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP == type) ||
3857 (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP == type))
3859 /* FIXME: update message type map for 'Neighbour' */
3862 dropped = GNUNET_YES;
3864 while (cpos != NULL)
3866 deliver_full = GNUNET_NO;
3867 if (0 != (cpos->options & GNUNET_CORE_OPTION_SEND_FULL_INBOUND))
3868 deliver_full = GNUNET_YES;
3871 for (tpos = 0; tpos < cpos->tcnt; tpos++)
3873 if (type != cpos->types[tpos])
3875 deliver_full = GNUNET_YES;
3879 if (GNUNET_YES == deliver_full)
3881 send_p2p_message_to_client (sender, cpos, m, msize);
3882 dropped = GNUNET_NO;
3884 else if (cpos->options & GNUNET_CORE_OPTION_SEND_HDR_INBOUND)
3886 send_p2p_message_to_client (sender, cpos, m,
3887 sizeof (struct GNUNET_MessageHeader));
3891 if (dropped == GNUNET_YES)
3894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3895 "Message of type %u from `%4s' not delivered to any client.\n",
3896 (unsigned int) type, GNUNET_i2s (&sender->peer));
3898 GNUNET_STATISTICS_update (stats,
3900 ("# messages not delivered to any client"), 1,
3907 * We received an encrypted message. Decrypt, validate and
3908 * pass on to the appropriate clients.
3910 * @param n target of the message
3911 * @param m encrypted message
3912 * @param ats performance data
3913 * @param ats_count number of entries in ats (excluding 0-termination)
3916 handle_encrypted_message (struct Neighbour *n, const struct EncryptedMessage *m,
3917 const struct GNUNET_TRANSPORT_ATS_Information *ats,
3920 size_t size = ntohs (m->header.size);
3922 struct EncryptedMessage *pt; /* plaintext */
3925 struct GNUNET_TIME_Absolute t;
3926 struct GNUNET_CRYPTO_AesInitializationVector iv;
3927 struct GNUNET_CRYPTO_AuthKey auth_key;
3930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3931 "Core service receives `%s' request from `%4s'.\n",
3932 "ENCRYPTED_MESSAGE", GNUNET_i2s (&n->peer));
3935 derive_auth_key (&auth_key, &n->decrypt_key, m->iv_seed,
3936 n->decrypt_key_created);
3937 GNUNET_CRYPTO_hmac (&auth_key, &m->sequence_number,
3938 size - ENCRYPTED_HEADER_SIZE, &ph);
3940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3941 "Re-Authenticated %u bytes of ciphertext (`%u'): `%s'\n",
3942 (unsigned int) size - ENCRYPTED_HEADER_SIZE,
3943 GNUNET_CRYPTO_crc32_n (&m->sequence_number,
3944 size - ENCRYPTED_HEADER_SIZE),
3948 if (0 != memcmp (&ph, &m->hmac, sizeof (GNUNET_HashCode)))
3950 /* checksum failed */
3951 GNUNET_break_op (0);
3954 derive_iv (&iv, &n->decrypt_key, m->iv_seed, &my_identity);
3957 do_decrypt (n, &iv, &m->sequence_number, &buf[ENCRYPTED_HEADER_SIZE],
3958 size - ENCRYPTED_HEADER_SIZE))
3960 pt = (struct EncryptedMessage *) buf;
3962 /* validate sequence number */
3963 snum = ntohl (pt->sequence_number);
3964 if (n->last_sequence_number_received == snum)
3966 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3967 "Received duplicate message, ignoring.\n");
3968 /* duplicate, ignore */
3969 GNUNET_STATISTICS_update (stats,
3970 gettext_noop ("# bytes dropped (duplicates)"),
3974 if ((n->last_sequence_number_received > snum) &&
3975 (n->last_sequence_number_received - snum > 32))
3977 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3978 "Received ancient out of sequence message, ignoring.\n");
3979 /* ancient out of sequence, ignore */
3980 GNUNET_STATISTICS_update (stats,
3982 ("# bytes dropped (out of sequence)"), size,
3986 if (n->last_sequence_number_received > snum)
3988 unsigned int rotbit = 1 << (n->last_sequence_number_received - snum - 1);
3990 if ((n->last_packets_bitmap & rotbit) != 0)
3992 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3993 "Received duplicate message, ignoring.\n");
3994 GNUNET_STATISTICS_update (stats,
3995 gettext_noop ("# bytes dropped (duplicates)"),
3997 /* duplicate, ignore */
4000 n->last_packets_bitmap |= rotbit;
4002 if (n->last_sequence_number_received < snum)
4004 int shift = (snum - n->last_sequence_number_received);
4006 if (shift >= 8 * sizeof (n->last_packets_bitmap))
4007 n->last_packets_bitmap = 0;
4009 n->last_packets_bitmap <<= shift;
4010 n->last_sequence_number_received = snum;
4013 /* check timestamp */
4014 t = GNUNET_TIME_absolute_ntoh (pt->timestamp);
4015 if (GNUNET_TIME_absolute_get_duration (t).rel_value >
4016 MAX_MESSAGE_AGE.rel_value)
4018 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4019 _("Message received far too old (%llu ms). Content ignored.\n"),
4020 GNUNET_TIME_absolute_get_duration (t).rel_value);
4021 GNUNET_STATISTICS_update (stats,
4023 ("# bytes dropped (ancient message)"), size,
4028 /* process decrypted message(s) */
4029 if (n->bw_out_external_limit.value__ != pt->inbound_bw_limit.value__)
4031 #if DEBUG_CORE_SET_QUOTA
4032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4033 "Received %u b/s as new inbound limit for peer `%4s'\n",
4034 (unsigned int) ntohl (pt->inbound_bw_limit.value__),
4035 GNUNET_i2s (&n->peer));
4037 n->bw_out_external_limit = pt->inbound_bw_limit;
4039 GNUNET_BANDWIDTH_value_min (n->bw_out_external_limit,
4040 n->bw_out_internal_limit);
4041 GNUNET_BANDWIDTH_tracker_update_quota (&n->available_send_window,
4043 GNUNET_TRANSPORT_set_quota (transport, &n->peer, n->bw_in, n->bw_out);
4045 n->last_activity = GNUNET_TIME_absolute_get ();
4046 if (n->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
4047 GNUNET_SCHEDULER_cancel (n->keep_alive_task);
4048 n->keep_alive_task =
4049 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
4050 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
4051 2), &send_keep_alive, n);
4052 GNUNET_STATISTICS_update (stats,
4053 gettext_noop ("# bytes of payload decrypted"),
4054 size - sizeof (struct EncryptedMessage), GNUNET_NO);
4055 handle_peer_status_change (n);
4056 update_neighbour_performance (n, ats, ats_count);
4058 GNUNET_SERVER_mst_receive (mst, n, &buf[sizeof (struct EncryptedMessage)],
4059 size - sizeof (struct EncryptedMessage),
4060 GNUNET_YES, GNUNET_NO))
4061 GNUNET_break_op (0);
4066 * Function called by the transport for each received message.
4068 * @param cls closure
4069 * @param peer (claimed) identity of the other peer
4070 * @param message the message
4071 * @param ats performance data
4072 * @param ats_count number of entries in ats (excluding 0-termination)
4075 handle_transport_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
4076 const struct GNUNET_MessageHeader *message,
4077 const struct GNUNET_TRANSPORT_ATS_Information *ats,
4080 struct Neighbour *n;
4081 struct GNUNET_TIME_Absolute now;
4088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4089 "Received message of type %u from `%4s', demultiplexing.\n",
4090 (unsigned int) ntohs (message->type), GNUNET_i2s (peer));
4092 if (0 == memcmp (peer, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
4097 n = find_neighbour (peer);
4098 if ((n == NULL) || (GNUNET_NO == n->is_connected))
4100 /* received message from peer that is not connected!? */
4104 changed = GNUNET_NO;
4105 up = (n->status == PEER_STATE_KEY_CONFIRMED);
4106 type = ntohs (message->type);
4107 size = ntohs (message->size);
4110 case GNUNET_MESSAGE_TYPE_CORE_SET_KEY:
4111 if (size != sizeof (struct SetKeyMessage))
4113 GNUNET_break_op (0);
4116 GNUNET_STATISTICS_update (stats, gettext_noop ("# session keys received"),
4118 handle_set_key (n, (const struct SetKeyMessage *) message, ats, ats_count);
4120 case GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE:
4122 sizeof (struct EncryptedMessage) + sizeof (struct GNUNET_MessageHeader))
4124 GNUNET_break_op (0);
4127 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
4128 (n->status != PEER_STATE_KEY_CONFIRMED))
4130 GNUNET_STATISTICS_update (stats,
4132 ("# failed to decrypt message (no session key)"),
4137 handle_encrypted_message (n, (const struct EncryptedMessage *) message, ats,
4140 case GNUNET_MESSAGE_TYPE_CORE_PING:
4141 if (size != sizeof (struct PingMessage))
4143 GNUNET_break_op (0);
4146 GNUNET_STATISTICS_update (stats, gettext_noop ("# PING messages received"),
4148 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
4149 (n->status != PEER_STATE_KEY_CONFIRMED))
4152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4153 "Core service receives `%s' request from `%4s' but have not processed key; marking as pending.\n",
4154 "PING", GNUNET_i2s (&n->peer));
4156 GNUNET_free_non_null (n->pending_ping);
4157 n->pending_ping = GNUNET_malloc (sizeof (struct PingMessage));
4158 memcpy (n->pending_ping, message, sizeof (struct PingMessage));
4161 handle_ping (n, (const struct PingMessage *) message, ats, ats_count);
4163 case GNUNET_MESSAGE_TYPE_CORE_PONG:
4164 if (size != sizeof (struct PongMessage))
4166 GNUNET_break_op (0);
4169 GNUNET_STATISTICS_update (stats, gettext_noop ("# PONG messages received"),
4171 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
4172 (n->status != PEER_STATE_KEY_CONFIRMED))
4175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4176 "Core service receives `%s' request from `%4s' but have not processed key; marking as pending.\n",
4177 "PONG", GNUNET_i2s (&n->peer));
4179 GNUNET_free_non_null (n->pending_pong);
4180 n->pending_pong = GNUNET_malloc (sizeof (struct PongMessage));
4181 memcpy (n->pending_pong, message, sizeof (struct PongMessage));
4184 handle_pong (n, (const struct PongMessage *) message, ats, ats_count);
4187 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4188 _("Unsupported message of type %u received.\n"),
4189 (unsigned int) type);
4192 if (n->status == PEER_STATE_KEY_CONFIRMED)
4194 now = GNUNET_TIME_absolute_get ();
4195 n->last_activity = now;
4196 changed = GNUNET_YES;
4199 GNUNET_STATISTICS_update (stats, gettext_noop ("# established sessions"),
4201 n->time_established = now;
4203 if (n->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
4204 GNUNET_SCHEDULER_cancel (n->keep_alive_task);
4205 n->keep_alive_task =
4206 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
4207 (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
4208 2), &send_keep_alive, n);
4211 handle_peer_status_change (n);
4216 * Function that recalculates the bandwidth quota for the
4217 * given neighbour and transmits it to the transport service.
4219 * @param cls neighbour for the quota update
4223 neighbour_quota_update (void *cls,
4224 const struct GNUNET_SCHEDULER_TaskContext *tc)
4226 struct Neighbour *n = cls;
4227 struct GNUNET_BANDWIDTH_Value32NBO q_in;
4228 struct GNUNET_BANDWIDTH_Value32NBO q_out;
4229 struct GNUNET_BANDWIDTH_Value32NBO q_out_min;
4232 unsigned long long distributable;
4233 uint64_t need_per_peer;
4234 uint64_t need_per_second;
4235 unsigned int neighbour_count;
4238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4239 "Neighbour quota update calculation running for peer `%4s'\n",
4240 GNUNET_i2s (&n->peer));
4242 n->quota_update_task = GNUNET_SCHEDULER_NO_TASK;
4243 /* calculate relative preference among all neighbours;
4244 * divides by a bit more to avoid division by zero AND to
4245 * account for possibility of new neighbours joining any time
4246 * AND to convert to double... */
4247 neighbour_count = GNUNET_CONTAINER_multihashmap_size (neighbours);
4248 if (neighbour_count == 0)
4250 if (preference_sum == 0)
4252 pref_rel = 1.0 / (double) neighbour_count;
4256 pref_rel = (double) n->current_preference / preference_sum;
4259 GNUNET_BANDWIDTH_value_get_available_until (MIN_BANDWIDTH_PER_PEER,
4260 GNUNET_TIME_UNIT_SECONDS);
4261 need_per_second = need_per_peer * neighbour_count;
4263 /* calculate inbound bandwidth per peer */
4265 if (bandwidth_target_in_bps > need_per_second)
4266 distributable = bandwidth_target_in_bps - need_per_second;
4267 share = distributable * pref_rel;
4268 if (share + need_per_peer > UINT32_MAX)
4269 q_in = GNUNET_BANDWIDTH_value_init (UINT32_MAX);
4271 q_in = GNUNET_BANDWIDTH_value_init (need_per_peer + (uint32_t) share);
4273 /* calculate outbound bandwidth per peer */
4275 if (bandwidth_target_out_bps > need_per_second)
4276 distributable = bandwidth_target_out_bps - need_per_second;
4277 share = distributable * pref_rel;
4278 if (share + need_per_peer > UINT32_MAX)
4279 q_out = GNUNET_BANDWIDTH_value_init (UINT32_MAX);
4281 q_out = GNUNET_BANDWIDTH_value_init (need_per_peer + (uint32_t) share);
4282 n->bw_out_internal_limit = q_out;
4285 GNUNET_BANDWIDTH_value_min (n->bw_out_external_limit,
4286 n->bw_out_internal_limit);
4287 GNUNET_BANDWIDTH_tracker_update_quota (&n->available_send_window, n->bw_out);
4289 /* check if we want to disconnect for good due to inactivity */
4290 if ((GNUNET_TIME_absolute_get_duration (get_neighbour_timeout (n)).rel_value >
4292 (GNUNET_TIME_absolute_get_duration (n->time_established).rel_value >
4293 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value))
4296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4297 "Forcing disconnect of `%4s' due to inactivity\n",
4298 GNUNET_i2s (&n->peer));
4300 GNUNET_STATISTICS_update (stats,
4301 gettext_noop ("# peers disconnected due to inactivity"), 1,
4303 q_in = GNUNET_BANDWIDTH_value_init (0); /* force disconnect */
4305 #if DEBUG_CORE_QUOTA
4306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4307 "Current quota for `%4s' is %u/%llu b/s in (old: %u b/s) / %u out (%u internal)\n",
4308 GNUNET_i2s (&n->peer), (unsigned int) ntohl (q_in.value__),
4309 bandwidth_target_out_bps, (unsigned int) ntohl (n->bw_in.value__),
4310 (unsigned int) ntohl (n->bw_out.value__),
4311 (unsigned int) ntohl (n->bw_out_internal_limit.value__));
4313 if ((n->bw_in.value__ != q_in.value__) ||
4314 (n->bw_out.value__ != q_out_min.value__))
4316 if (n->bw_in.value__ != q_in.value__)
4318 if (n->bw_out.value__ != q_out_min.value__)
4319 n->bw_out = q_out_min;
4320 if (GNUNET_YES == n->is_connected)
4321 GNUNET_TRANSPORT_set_quota (transport, &n->peer, n->bw_in, n->bw_out);
4322 handle_peer_status_change (n);
4324 schedule_quota_update (n);
4329 * Function called by transport to notify us that
4330 * a peer connected to us (on the network level).
4332 * @param cls closure
4333 * @param peer the peer that connected
4334 * @param ats performance data
4335 * @param ats_count number of entries in ats (excluding 0-termination)
4338 handle_transport_notify_connect (void *cls,
4339 const struct GNUNET_PeerIdentity *peer,
4340 const struct GNUNET_TRANSPORT_ATS_Information
4341 *ats, uint32_t ats_count)
4343 struct Neighbour *n;
4345 if (0 == memcmp (peer, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
4350 n = find_neighbour (peer);
4353 if (GNUNET_YES == n->is_connected)
4355 /* duplicate connect notification!? */
4362 n = create_neighbour (peer);
4364 GNUNET_STATISTICS_update (stats,
4365 gettext_noop ("# peers connected (transport)"), 1,
4367 n->is_connected = GNUNET_YES;
4368 update_neighbour_performance (n, ats, ats_count);
4369 GNUNET_BANDWIDTH_tracker_init (&n->available_send_window, n->bw_out,
4371 GNUNET_BANDWIDTH_tracker_init (&n->available_recv_window, n->bw_in,
4374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received connection from `%4s'.\n",
4375 GNUNET_i2s (&n->peer));
4377 GNUNET_TRANSPORT_set_quota (transport, &n->peer, n->bw_in, n->bw_out);
4383 * Function called by transport telling us that a peer
4386 * @param cls closure
4387 * @param peer the peer that disconnected
4390 handle_transport_notify_disconnect (void *cls,
4391 const struct GNUNET_PeerIdentity *peer)
4393 struct DisconnectNotifyMessage cnm;
4394 struct Neighbour *n;
4395 struct ClientActiveRequest *car;
4396 struct GNUNET_TIME_Relative left;
4399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4400 "Peer `%4s' disconnected from us; received notification from transport.\n",
4403 n = find_neighbour (peer);
4409 GNUNET_break (n->is_connected == GNUNET_YES);
4410 if (n->status == PEER_STATE_KEY_CONFIRMED)
4412 GNUNET_STATISTICS_update (stats,
4413 gettext_noop ("# peers disconnected due to transport disconnect"), 1,
4415 cnm.header.size = htons (sizeof (struct DisconnectNotifyMessage));
4416 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT);
4417 cnm.reserved = htonl (0);
4419 send_to_all_clients (&cnm.header, GNUNET_NO,
4420 GNUNET_CORE_OPTION_SEND_DISCONNECT);
4421 GNUNET_STATISTICS_update (stats, gettext_noop ("# established sessions"),
4425 /* On transport disconnect transport doesn't cancel requests, so must do so here. */
4428 GNUNET_TRANSPORT_notify_transmit_ready_cancel (n->th);
4431 if (GNUNET_SCHEDULER_NO_TASK != n->keep_alive_task)
4433 GNUNET_SCHEDULER_cancel (n->keep_alive_task);
4434 n->keep_alive_task = GNUNET_SCHEDULER_NO_TASK;
4436 n->is_connected = GNUNET_NO;
4437 n->status = PEER_STATE_DOWN;
4438 while (NULL != (car = n->active_client_request_head))
4440 GNUNET_CONTAINER_DLL_remove (n->active_client_request_head,
4441 n->active_client_request_tail, car);
4442 GNUNET_assert (GNUNET_YES ==
4443 GNUNET_CONTAINER_multihashmap_remove (car->client->requests,
4444 &n->peer.hashPubKey,
4449 GNUNET_STATISTICS_update (stats,
4450 gettext_noop ("# peers connected (transport)"), -1,
4452 if (n->dead_clean_task != GNUNET_SCHEDULER_NO_TASK)
4453 GNUNET_SCHEDULER_cancel (n->dead_clean_task);
4455 GNUNET_TIME_relative_subtract (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
4456 GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT);
4458 GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (), left);
4459 n->dead_clean_task =
4460 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
4461 &consider_free_task, n);
4466 * Wrapper around 'free_neighbour'; helper for 'cleaning_task'.
4469 free_neighbour_helper (void *cls, const GNUNET_HashCode * key, void *value)
4471 struct Neighbour *n = value;
4479 * Last task run during shutdown. Disconnects us from
4483 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core service shutting down.\n");
4490 GNUNET_assert (transport != NULL);
4491 GNUNET_TRANSPORT_disconnect (transport);
4493 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &free_neighbour_helper,
4495 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
4497 GNUNET_STATISTICS_set (stats, gettext_noop ("# neighbour entries allocated"),
4499 GNUNET_SERVER_notification_context_destroy (notifier);
4501 while (NULL != (c = clients))
4502 handle_client_disconnect (NULL, c->client_handle);
4503 if (my_private_key != NULL)
4504 GNUNET_CRYPTO_rsa_key_free (my_private_key);
4506 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
4507 if (peerinfo != NULL)
4508 GNUNET_PEERINFO_disconnect (peerinfo);
4510 GNUNET_SERVER_mst_destroy (mst);
4515 * Initiate core service.
4517 * @param cls closure
4518 * @param server the initialized server
4519 * @param c configuration to use
4522 run (void *cls, struct GNUNET_SERVER_Handle *server,
4523 const struct GNUNET_CONFIGURATION_Handle *c)
4525 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
4526 {&handle_client_init, NULL,
4527 GNUNET_MESSAGE_TYPE_CORE_INIT, 0},
4528 {&handle_client_iterate_peers, NULL,
4529 GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS,
4530 sizeof (struct GNUNET_MessageHeader)},
4531 {&handle_client_have_peer, NULL,
4532 GNUNET_MESSAGE_TYPE_CORE_PEER_CONNECTED,
4533 sizeof (struct GNUNET_MessageHeader) +
4534 sizeof (struct GNUNET_PeerIdentity)},
4535 {&handle_client_request_info, NULL,
4536 GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO,
4537 sizeof (struct RequestInfoMessage)},
4538 {&handle_client_send_request, NULL,
4539 GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST,
4540 sizeof (struct SendMessageRequest)},
4541 {&handle_client_send, NULL,
4542 GNUNET_MESSAGE_TYPE_CORE_SEND, 0},
4548 /* parse configuration */
4550 GNUNET_CONFIGURATION_get_value_number (c, "CORE", "TOTAL_QUOTA_IN",
4551 &bandwidth_target_in_bps)) ||
4553 GNUNET_CONFIGURATION_get_value_number (c, "CORE", "TOTAL_QUOTA_OUT",
4554 &bandwidth_target_out_bps)) ||
4556 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
4559 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4561 ("Core service is lacking key configuration settings. Exiting.\n"));
4562 GNUNET_SCHEDULER_shutdown ();
4565 peerinfo = GNUNET_PEERINFO_connect (cfg);
4566 if (NULL == peerinfo)
4568 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4569 _("Could not access PEERINFO service. Exiting.\n"));
4570 GNUNET_SCHEDULER_shutdown ();
4571 GNUNET_free (keyfile);
4574 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4575 GNUNET_free (keyfile);
4576 if (my_private_key == NULL)
4578 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4579 _("Core service could not access hostkey. Exiting.\n"));
4580 GNUNET_PEERINFO_disconnect (peerinfo);
4581 GNUNET_SCHEDULER_shutdown ();
4584 neighbours = GNUNET_CONTAINER_multihashmap_create (128);
4585 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4586 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
4587 &my_identity.hashPubKey);
4588 self.public_key = &my_public_key;
4589 self.peer = my_identity;
4590 self.last_activity = GNUNET_TIME_UNIT_FOREVER_ABS;
4591 self.status = PEER_STATE_KEY_CONFIRMED;
4592 self.is_connected = GNUNET_YES;
4593 /* setup notification */
4595 GNUNET_SERVER_notification_context_create (server, MAX_NOTIFY_QUEUE);
4596 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
4597 /* setup transport connection */
4599 GNUNET_TRANSPORT_connect (cfg, &my_identity, NULL,
4600 &handle_transport_receive,
4601 &handle_transport_notify_connect,
4602 &handle_transport_notify_disconnect);
4603 GNUNET_assert (NULL != transport);
4604 stats = GNUNET_STATISTICS_create ("core", cfg);
4605 mst = GNUNET_SERVER_mst_create (&deliver_message, NULL);
4606 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
4608 /* process client requests */
4609 GNUNET_SERVER_add_handlers (server, handlers);
4610 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Core service of `%4s' ready.\n"),
4611 GNUNET_i2s (&my_identity));
4617 * The main function for the transport service.
4619 * @param argc number of arguments from the command line
4620 * @param argv command line arguments
4621 * @return 0 ok, 1 on error
4624 main (int argc, char *const *argv)
4626 return (GNUNET_OK ==
4627 GNUNET_SERVICE_run (argc, argv, "core", GNUNET_SERVICE_OPTION_NONE,
4628 &run, NULL)) ? 0 : 1;
4631 /* end of gnunet-service-core.c */