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 * Considerations for later:
27 * - check that hostkey used by transport (for HELLOs) is the
28 * same as the hostkey that we are using!
29 * - add code to send PINGs if we are about to time-out otherwise
30 * - optimize lookup (many O(n) list traversals
31 * could ideally be changed to O(1) hash map lookups)
34 #include "gnunet_constants.h"
35 #include "gnunet_util_lib.h"
36 #include "gnunet_hello_lib.h"
37 #include "gnunet_peerinfo_service.h"
38 #include "gnunet_protocols.h"
39 #include "gnunet_signatures.h"
40 #include "gnunet_statistics_service.h"
41 #include "gnunet_transport_service.h"
45 #define DEBUG_HANDSHAKE GNUNET_NO
47 #define DEBUG_CORE_QUOTA GNUNET_YES
50 * Receive and send buffer windows grow over time. For
51 * how long can 'unused' bandwidth accumulate before we
52 * need to cap it? (specified in seconds).
54 #define MAX_WINDOW_TIME_S (5 * 60)
57 * How many messages do we queue up at most for optional
58 * notifications to a client? (this can cause notifications
59 * about outgoing messages to be dropped).
61 #define MAX_NOTIFY_QUEUE 1024
64 * Minimum bandwidth (out) to assign to any connected peer.
65 * Should be rather low; values larger than DEFAULT_BW_IN_OUT make no
68 #define MIN_BANDWIDTH_PER_PEER GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT
71 * After how much time past the "official" expiration time do
72 * we discard messages? Should not be zero since we may
73 * intentionally defer transmission until close to the deadline
74 * and then may be slightly past the deadline due to inaccuracy
75 * in sleep and our own CPU consumption.
77 #define PAST_EXPIRATION_DISCARD_TIME GNUNET_TIME_UNIT_SECONDS
80 * What is the maximum delay for a SET_KEY message?
82 #define MAX_SET_KEY_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
85 * How long do we wait for SET_KEY confirmation initially?
87 #define INITIAL_SET_KEY_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (MAX_SET_KEY_DELAY, 1)
90 * What is the maximum delay for a PING message?
92 #define MAX_PING_DELAY GNUNET_TIME_relative_multiply (MAX_SET_KEY_DELAY, 2)
95 * What is the maximum delay for a PONG message?
97 #define MAX_PONG_DELAY GNUNET_TIME_relative_multiply (MAX_PING_DELAY, 2)
100 * What is the minimum frequency for a PING message?
102 #define MIN_PING_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
105 * How often do we recalculate bandwidth quotas?
107 #define QUOTA_UPDATE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
110 * What is the priority for a SET_KEY message?
112 #define SET_KEY_PRIORITY 0xFFFFFF
115 * What is the priority for a PING message?
117 #define PING_PRIORITY 0xFFFFFF
120 * What is the priority for a PONG message?
122 #define PONG_PRIORITY 0xFFFFFF
125 * How many messages do we queue per peer at most? Must be at
128 #define MAX_PEER_QUEUE_SIZE 16
131 * How many non-mandatory messages do we queue per client at most?
133 #define MAX_CLIENT_QUEUE_SIZE 32
136 * What is the maximum age of a message for us to consider
137 * processing it? Note that this looks at the timestamp used
138 * by the other peer, so clock skew between machines does
139 * come into play here. So this should be picked high enough
140 * so that a little bit of clock skew does not prevent peers
141 * from connecting to us.
143 #define MAX_MESSAGE_AGE GNUNET_TIME_UNIT_DAYS
147 * State machine for our P2P encryption handshake. Everyone starts in
148 * "DOWN", if we receive the other peer's key (other peer initiated)
149 * we start in state RECEIVED (since we will immediately send our
150 * own); otherwise we start in SENT. If we get back a PONG from
151 * within either state, we move up to CONFIRMED (the PONG will always
152 * be sent back encrypted with the key we sent to the other peer).
154 enum PeerStateMachine
158 PEER_STATE_KEY_RECEIVED,
159 PEER_STATE_KEY_CONFIRMED
164 * Number of bytes (at the beginning) of "struct EncryptedMessage"
165 * that are NOT encrypted.
167 #define ENCRYPTED_HEADER_SIZE (sizeof(struct GNUNET_MessageHeader) + sizeof(uint32_t))
171 * Encapsulation for encrypted messages exchanged between
172 * peers. Followed by the actual encrypted data.
174 struct EncryptedMessage
177 * Message type is either CORE_ENCRYPTED_MESSAGE.
179 struct GNUNET_MessageHeader header;
182 * Random value used for IV generation. ENCRYPTED_HEADER_SIZE must
183 * be set to the offset of the *next* field.
185 uint32_t iv_seed GNUNET_PACKED;
188 * Hash of the plaintext (starting at 'sequence_number'), used to
189 * verify message integrity. Everything after this hash (including
190 * this hash itself) will be encrypted.
192 GNUNET_HashCode hmac;
195 * Sequence number, in network byte order. This field
196 * must be the first encrypted/decrypted field and the
197 * first byte that is hashed for the plaintext hash.
199 uint32_t sequence_number GNUNET_PACKED;
202 * Desired bandwidth (how much we should send to this peer / how
203 * much is the sender willing to receive)?
205 struct GNUNET_BANDWIDTH_Value32NBO inbound_bw_limit;
208 * Timestamp. Used to prevent reply of ancient messages
209 * (recent messages are caught with the sequence number).
211 struct GNUNET_TIME_AbsoluteNBO timestamp;
217 * We're sending an (encrypted) PING to the other peer to check if he
218 * can decrypt. The other peer should respond with a PONG with the
219 * same content, except this time encrypted with the receiver's key.
224 * Message type is CORE_PING.
226 struct GNUNET_MessageHeader header;
231 uint32_t iv_seed GNUNET_PACKED;
234 * Random number chosen to make reply harder.
236 uint32_t challenge GNUNET_PACKED;
239 * Intended target of the PING, used primarily to check
240 * that decryption actually worked.
242 struct GNUNET_PeerIdentity target;
248 * Response to a PING. Includes data from the original PING
249 * plus initial bandwidth quota information.
254 * Message type is CORE_PONG.
256 struct GNUNET_MessageHeader header;
261 uint32_t iv_seed GNUNET_PACKED;
264 * Random number proochosen to make reply harder. Must be
265 * first field after header (this is where we start to encrypt!).
267 uint32_t challenge GNUNET_PACKED;
272 uint32_t reserved GNUNET_PACKED;
275 * Desired bandwidth (how much we should send to this
276 * peer / how much is the sender willing to receive).
278 struct GNUNET_BANDWIDTH_Value32NBO inbound_bw_limit;
281 * Intended target of the PING, used primarily to check
282 * that decryption actually worked.
284 struct GNUNET_PeerIdentity target;
289 * Message transmitted to set (or update) a session key.
295 * Message type is either CORE_SET_KEY.
297 struct GNUNET_MessageHeader header;
300 * Status of the sender (should be in "enum PeerStateMachine"), nbo.
302 int32_t sender_status GNUNET_PACKED;
305 * Purpose of the signature, will be
306 * GNUNET_SIGNATURE_PURPOSE_SET_KEY.
308 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
311 * At what time was this key created?
313 struct GNUNET_TIME_AbsoluteNBO creation_time;
316 * The encrypted session key.
318 struct GNUNET_CRYPTO_RsaEncryptedData encrypted_key;
321 * Who is the intended recipient?
323 struct GNUNET_PeerIdentity target;
326 * Signature of the stuff above (starting at purpose).
328 struct GNUNET_CRYPTO_RsaSignature signature;
334 * Message waiting for transmission. This struct
335 * is followed by the actual content of the message.
341 * We keep messages in a doubly linked list.
343 struct MessageEntry *next;
346 * We keep messages in a doubly linked list.
348 struct MessageEntry *prev;
351 * By when are we supposed to transmit this message?
353 struct GNUNET_TIME_Absolute deadline;
356 * By when are we supposed to transmit this message (after
359 struct GNUNET_TIME_Absolute slack_deadline;
362 * How important is this message to us?
364 unsigned int priority;
367 * If this is a SET_KEY message, what was our connection status when this
368 * message was queued?
370 enum PeerStateMachine sender_status;
373 * Is this a SET_KEY message?
378 * How long is the message? (number of bytes following
379 * the "struct MessageEntry", but not including the
380 * size of "struct MessageEntry" itself!)
385 * Was this message selected for transmission in the
386 * current round? GNUNET_YES or GNUNET_NO.
391 * Did we give this message some slack (delayed sending) previously
392 * (and hence should not give it any more slack)? GNUNET_YES or
403 * We keep neighbours in a linked list (for now).
405 struct Neighbour *next;
408 * Unencrypted messages destined for this peer.
410 struct MessageEntry *messages;
413 * Head of the batched, encrypted message queue (already ordered,
414 * transmit starting with the head).
416 struct MessageEntry *encrypted_head;
419 * Tail of the batched, encrypted message queue (already ordered,
420 * append new messages to tail)
422 struct MessageEntry *encrypted_tail;
425 * Handle for pending requests for transmission to this peer
426 * with the transport service. NULL if no request is pending.
428 struct GNUNET_TRANSPORT_TransmitHandle *th;
431 * Public key of the neighbour, NULL if we don't have it yet.
433 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key;
436 * We received a PING message before we got the "public_key"
437 * (or the SET_KEY). We keep it here until we have a key
438 * to decrypt it. NULL if no PING is pending.
440 struct PingMessage *pending_ping;
443 * We received a PONG message before we got the "public_key"
444 * (or the SET_KEY). We keep it here until we have a key
445 * to decrypt it. NULL if no PONG is pending.
447 struct PongMessage *pending_pong;
450 * Non-NULL if we are currently looking up HELLOs for this peer.
453 struct GNUNET_PEERINFO_IteratorContext *pitr;
456 * SetKeyMessage to transmit, NULL if we are not currently trying
459 struct SetKeyMessage *skm;
462 * Identity of the neighbour.
464 struct GNUNET_PeerIdentity peer;
467 * Key we use to encrypt our messages for the other peer
468 * (initialized by us when we do the handshake).
470 struct GNUNET_CRYPTO_AesSessionKey encrypt_key;
473 * Key we use to decrypt messages from the other peer
474 * (given to us by the other peer during the handshake).
476 struct GNUNET_CRYPTO_AesSessionKey decrypt_key;
479 * Key we use to authenticate messages sent to the other peer
480 * (derived from the encrypt_key during the handshake)
482 struct GNUNET_CRYPTO_AuthKey encrypt_auth_key;
485 * Key we use to authenticate messages sent from the other peer
486 * (derived from the decrypt_key during the handshake)
488 struct GNUNET_CRYPTO_AuthKey decrypt_auth_key;
491 * ID of task used for re-trying plaintext scheduling.
493 GNUNET_SCHEDULER_TaskIdentifier retry_plaintext_task;
496 * ID of task used for re-trying SET_KEY and PING message.
498 GNUNET_SCHEDULER_TaskIdentifier retry_set_key_task;
501 * ID of task used for updating bandwidth quota for this neighbour.
503 GNUNET_SCHEDULER_TaskIdentifier quota_update_task;
506 * ID of task used for sending keep-alive pings.
508 GNUNET_SCHEDULER_TaskIdentifier keep_alive_task;
511 * ID of task used for cleaning up dead neighbour entries.
513 GNUNET_SCHEDULER_TaskIdentifier dead_clean_task;
516 * At what time did we generate our encryption key?
518 struct GNUNET_TIME_Absolute encrypt_key_created;
521 * At what time did the other peer generate the decryption key?
523 struct GNUNET_TIME_Absolute decrypt_key_created;
526 * At what time did we initially establish (as in, complete session
527 * key handshake) this connection? Should be zero if status != KEY_CONFIRMED.
529 struct GNUNET_TIME_Absolute time_established;
532 * At what time did we last receive an encrypted message from the
533 * other peer? Should be zero if status != KEY_CONFIRMED.
535 struct GNUNET_TIME_Absolute last_activity;
538 * Last latency observed from this peer.
540 struct GNUNET_TIME_Relative last_latency;
543 * At what frequency are we currently re-trying SET_KEY messages?
545 struct GNUNET_TIME_Relative set_key_retry_frequency;
548 * Tracking bandwidth for sending to this peer.
550 struct GNUNET_BANDWIDTH_Tracker available_send_window;
553 * Tracking bandwidth for receiving from this peer.
555 struct GNUNET_BANDWIDTH_Tracker available_recv_window;
558 * How valueable were the messages of this peer recently?
560 unsigned long long current_preference;
563 * Bit map indicating which of the 32 sequence numbers before the last
564 * were received (good for accepting out-of-order packets and
565 * estimating reliability of the connection)
567 unsigned int last_packets_bitmap;
570 * last sequence number received on this connection (highest)
572 uint32_t last_sequence_number_received;
575 * last sequence number transmitted
577 uint32_t last_sequence_number_sent;
580 * Available bandwidth in for this peer (current target).
582 struct GNUNET_BANDWIDTH_Value32NBO bw_in;
585 * Available bandwidth out for this peer (current target).
587 struct GNUNET_BANDWIDTH_Value32NBO bw_out;
590 * Internal bandwidth limit set for this peer (initially typically
591 * set to "-1"). Actual "bw_out" is MIN of
592 * "bpm_out_internal_limit" and "bw_out_external_limit".
594 struct GNUNET_BANDWIDTH_Value32NBO bw_out_internal_limit;
597 * External bandwidth limit set for this peer by the
598 * peer that we are communicating with. "bw_out" is MIN of
599 * "bw_out_internal_limit" and "bw_out_external_limit".
601 struct GNUNET_BANDWIDTH_Value32NBO bw_out_external_limit;
604 * What was our PING challenge number (for this peer)?
606 uint32_t ping_challenge;
609 * What was the last distance to this peer as reported by the transports?
611 uint32_t last_distance;
614 * What is our connection status?
616 enum PeerStateMachine status;
619 * Are we currently connected to this neighbour?
627 * Data structure for each client connected to the core service.
632 * Clients are kept in a linked list.
637 * Handle for the client with the server API.
639 struct GNUNET_SERVER_Client *client_handle;
642 * Array of the types of messages this peer cares
643 * about (with "tcnt" entries). Allocated as part
644 * of this client struct, do not free!
646 const uint16_t *types;
649 * Options for messages this client cares about,
650 * see GNUNET_CORE_OPTION_ values.
655 * Number of types of incoming messages this client
656 * specifically cares about. Size of the "types" array.
666 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
671 static struct GNUNET_PeerIdentity my_identity;
676 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
681 struct GNUNET_SCHEDULER_Handle *sched;
684 * Handle to peerinfo service.
686 static struct GNUNET_PEERINFO_Handle *peerinfo;
689 * Our message stream tokenizer (for encrypted payload).
691 static struct GNUNET_SERVER_MessageStreamTokenizer *mst;
696 const struct GNUNET_CONFIGURATION_Handle *cfg;
701 static struct GNUNET_TRANSPORT_Handle *transport;
704 * Linked list of our clients.
706 static struct Client *clients;
709 * Context for notifications we need to send to our clients.
711 static struct GNUNET_SERVER_NotificationContext *notifier;
714 * We keep neighbours in a linked list (for now).
716 static struct Neighbour *neighbours;
719 * For creating statistics.
721 static struct GNUNET_STATISTICS_Handle *stats;
724 * Sum of all preferences among all neighbours.
726 static unsigned long long preference_sum;
729 * Total number of neighbours we have.
731 static unsigned int neighbour_count;
734 * How much inbound bandwidth are we supposed to be using per second?
735 * FIXME: this value is not used!
737 static unsigned long long bandwidth_target_in_bps;
740 * How much outbound bandwidth are we supposed to be using per second?
742 static unsigned long long bandwidth_target_out_bps;
745 * Derive an authentication key from "set key" information
748 derive_auth_key (struct GNUNET_CRYPTO_AuthKey *akey,
749 const struct GNUNET_CRYPTO_AesSessionKey *skey,
750 const struct GNUNET_TIME_Absolute creation_time,
751 const struct GNUNET_PeerIdentity *identity)
753 static char ctx[] = "authentication key";
755 GNUNET_CRYPTO_hmac_derive_key (akey,
759 &identity->hashPubKey.bits,
760 sizeof(identity->hashPubKey.bits),
762 sizeof(creation_time),
769 * Derive an IV from packet information
772 derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
773 const struct GNUNET_CRYPTO_AesSessionKey *skey, uint32_t seed,
774 const struct GNUNET_PeerIdentity *identity)
776 static char ctx[] = "initialization vector";
778 GNUNET_CRYPTO_aes_derive_iv (iv,
782 &identity->hashPubKey.bits,
783 sizeof(identity->hashPubKey.bits),
789 * Derive an IV from pong packet information
792 derive_pong_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
793 const struct GNUNET_CRYPTO_AesSessionKey *skey, uint32_t seed,
794 uint32_t challenge, const struct GNUNET_PeerIdentity *identity)
796 static char ctx[] = "pong initialization vector";
798 GNUNET_CRYPTO_aes_derive_iv (iv,
802 &identity->hashPubKey.bits,
803 sizeof(identity->hashPubKey.bits),
812 * A preference value for a neighbour was update. Update
813 * the preference sum accordingly.
815 * @param inc how much was a preference value increased?
818 update_preference_sum (unsigned long long inc)
821 unsigned long long os;
824 preference_sum += inc;
825 if (preference_sum >= os)
827 /* overflow! compensate by cutting all values in half! */
832 n->current_preference /= 2;
833 preference_sum += n->current_preference;
836 GNUNET_STATISTICS_set (stats, gettext_noop ("# total peer preference"), preference_sum, GNUNET_NO);
841 * Find the entry for the given neighbour.
843 * @param peer identity of the neighbour
844 * @return NULL if we are not connected, otherwise the
847 static struct Neighbour *
848 find_neighbour (const struct GNUNET_PeerIdentity *peer)
850 struct Neighbour *ret;
853 while ((ret != NULL) &&
854 (0 != memcmp (&ret->peer,
855 peer, sizeof (struct GNUNET_PeerIdentity))))
862 * Send a message to one of our clients.
864 * @param client target for the message
865 * @param msg message to transmit
866 * @param can_drop could this message be dropped if the
867 * client's queue is getting too large?
870 send_to_client (struct Client *client,
871 const struct GNUNET_MessageHeader *msg,
874 #if DEBUG_CORE_CLIENT
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876 "Preparing to send %u bytes of message of type %u to client.\n",
877 (unsigned int) ntohs (msg->size),
878 (unsigned int) ntohs (msg->type));
880 GNUNET_SERVER_notification_context_unicast (notifier,
881 client->client_handle,
888 * Send a message to all of our current clients that have
889 * the right options set.
891 * @param msg message to multicast
892 * @param can_drop can this message be discarded if the queue is too long
893 * @param options mask to use
896 send_to_all_clients (const struct GNUNET_MessageHeader *msg,
905 if (0 != (c->options & options))
907 #if DEBUG_CORE_CLIENT
908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
909 "Sending message of type %u to client.\n",
910 (unsigned int) ntohs (msg->type));
912 send_to_client (c, msg, can_drop);
920 * Function called by transport telling us that a peer
923 * @param n the peer that changed status
926 handle_peer_status_change (struct Neighbour *n)
928 struct PeerStatusNotifyMessage psnm;
930 if (! n->is_connected)
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
934 "Peer `%4s' changed status\n",
935 GNUNET_i2s (&n->peer));
937 psnm.header.size = htons (sizeof (struct PeerStatusNotifyMessage));
938 psnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_STATUS_CHANGE);
939 psnm.distance = htonl (n->last_distance);
940 psnm.latency = GNUNET_TIME_relative_hton (n->last_latency);
941 psnm.timeout = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_add (n->last_activity,
942 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
943 psnm.bandwidth_in = n->bw_in;
944 psnm.bandwidth_out = n->bw_out;
946 send_to_all_clients (&psnm.header,
948 GNUNET_CORE_OPTION_SEND_STATUS_CHANGE);
949 GNUNET_STATISTICS_update (stats,
950 gettext_noop ("# peer status changes"),
956 * Handle CORE_ITERATE_PEERS request.
959 handle_client_iterate_peers (void *cls,
960 struct GNUNET_SERVER_Client *client,
961 const struct GNUNET_MessageHeader *message)
964 struct ConnectNotifyMessage cnm;
965 struct GNUNET_MessageHeader done_msg;
966 struct GNUNET_SERVER_TransmitContext *tc;
968 /* notify new client about existing neighbours */
969 cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
970 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
971 done_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
972 done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
973 tc = GNUNET_SERVER_transmit_context_create (client);
977 if (n->status == PEER_STATE_KEY_CONFIRMED)
979 #if DEBUG_CORE_CLIENT
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981 "Sending `%s' message to client.\n", "NOTIFY_CONNECT");
983 cnm.distance = htonl (n->last_distance);
984 cnm.latency = GNUNET_TIME_relative_hton (n->last_latency);
986 GNUNET_SERVER_transmit_context_append_message (tc, &cnm.header);
987 /*send_to_client (c, &cnm.header, GNUNET_NO);*/
992 GNUNET_SERVER_transmit_context_append_message (tc, &done_msg);
993 GNUNET_SERVER_transmit_context_run (tc,
994 GNUNET_TIME_UNIT_FOREVER_REL);
999 * Handle CORE_INIT request.
1002 handle_client_init (void *cls,
1003 struct GNUNET_SERVER_Client *client,
1004 const struct GNUNET_MessageHeader *message)
1006 const struct InitMessage *im;
1007 struct InitReplyMessage irm;
1010 const uint16_t *types;
1012 struct Neighbour *n;
1013 struct ConnectNotifyMessage cnm;
1016 #if DEBUG_CORE_CLIENT
1017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1018 "Client connecting to core service with `%s' message\n",
1021 /* check that we don't have an entry already */
1025 if (client == c->client_handle)
1028 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1033 msize = ntohs (message->size);
1034 if (msize < sizeof (struct InitMessage))
1037 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1040 GNUNET_SERVER_notification_context_add (notifier, client);
1041 im = (const struct InitMessage *) message;
1042 types = (const uint16_t *) &im[1];
1043 msize -= sizeof (struct InitMessage);
1044 c = GNUNET_malloc (sizeof (struct Client) + msize);
1045 c->client_handle = client;
1048 c->tcnt = msize / sizeof (uint16_t);
1049 c->types = (const uint16_t *) &c[1];
1050 wtypes = (uint16_t *) &c[1];
1051 for (i=0;i<c->tcnt;i++)
1052 wtypes[i] = ntohs (types[i]);
1053 c->options = ntohl (im->options);
1054 #if DEBUG_CORE_CLIENT
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1056 "Client %p is interested in %u message types\n",
1058 (unsigned int) c->tcnt);
1060 /* send init reply message */
1061 irm.header.size = htons (sizeof (struct InitReplyMessage));
1062 irm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY);
1063 irm.reserved = htonl (0);
1064 memcpy (&irm.publicKey,
1066 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1067 #if DEBUG_CORE_CLIENT
1068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1069 "Sending `%s' message to client.\n", "INIT_REPLY");
1071 send_to_client (c, &irm.header, GNUNET_NO);
1072 if (0 != (c->options & GNUNET_CORE_OPTION_SEND_CONNECT))
1074 /* notify new client about existing neighbours */
1075 cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
1076 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
1080 if (n->status == PEER_STATE_KEY_CONFIRMED)
1082 #if DEBUG_CORE_CLIENT
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 "Sending `%s' message to client.\n", "NOTIFY_CONNECT");
1086 cnm.distance = htonl (n->last_distance);
1087 cnm.latency = GNUNET_TIME_relative_hton (n->last_latency);
1089 send_to_client (c, &cnm.header, GNUNET_NO);
1094 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1099 * A client disconnected, clean up.
1101 * @param cls closure
1102 * @param client identification of the client
1105 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
1108 struct Client *prev;
1112 #if DEBUG_CORE_CLIENT
1113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1114 "Client %p has disconnected from core service.\n",
1121 if (client == pos->client_handle)
1124 clients = pos->next;
1126 prev->next = pos->next;
1133 /* client never sent INIT */
1138 * Handle REQUEST_INFO request.
1141 handle_client_request_info (void *cls,
1142 struct GNUNET_SERVER_Client *client,
1143 const struct GNUNET_MessageHeader *message)
1145 const struct RequestInfoMessage *rcm;
1146 struct Neighbour *n;
1147 struct ConfigurationInfoMessage cim;
1148 int32_t want_reserv;
1150 unsigned long long old_preference;
1151 struct GNUNET_SERVER_TransmitContext *tc;
1153 #if DEBUG_CORE_CLIENT
1154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1155 "Core service receives `%s' request.\n", "REQUEST_INFO");
1157 rcm = (const struct RequestInfoMessage *) message;
1158 n = find_neighbour (&rcm->peer);
1159 memset (&cim, 0, sizeof (cim));
1162 want_reserv = ntohl (rcm->reserve_inbound);
1163 if (n->bw_out_internal_limit.value__ != rcm->limit_outbound.value__)
1165 n->bw_out_internal_limit = rcm->limit_outbound;
1166 if (n->bw_out.value__ != GNUNET_BANDWIDTH_value_min (n->bw_out_internal_limit,
1167 n->bw_out_external_limit).value__)
1169 n->bw_out = GNUNET_BANDWIDTH_value_min (n->bw_out_internal_limit,
1170 n->bw_out_external_limit);
1171 GNUNET_BANDWIDTH_tracker_update_quota (&n->available_recv_window,
1173 GNUNET_TRANSPORT_set_quota (transport,
1177 GNUNET_TIME_UNIT_FOREVER_REL,
1179 handle_peer_status_change (n);
1182 if (want_reserv < 0)
1184 got_reserv = want_reserv;
1186 else if (want_reserv > 0)
1188 if (GNUNET_BANDWIDTH_tracker_get_delay (&n->available_recv_window,
1189 want_reserv).value == 0)
1190 got_reserv = want_reserv;
1192 got_reserv = 0; /* all or nothing */
1196 GNUNET_BANDWIDTH_tracker_consume (&n->available_recv_window,
1198 old_preference = n->current_preference;
1199 n->current_preference += GNUNET_ntohll(rcm->preference_change);
1200 if (old_preference > n->current_preference)
1202 /* overflow; cap at maximum value */
1203 n->current_preference = ULLONG_MAX;
1205 update_preference_sum (n->current_preference - old_preference);
1206 #if DEBUG_CORE_QUOTA
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208 "Received reservation request for %d bytes for peer `%4s', reserved %d bytes\n",
1210 GNUNET_i2s (&rcm->peer),
1213 cim.reserved_amount = htonl (got_reserv);
1214 cim.bw_in = n->bw_in;
1215 cim.bw_out = n->bw_out;
1216 cim.preference = n->current_preference;
1218 cim.header.size = htons (sizeof (struct ConfigurationInfoMessage));
1219 cim.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO);
1220 cim.peer = rcm->peer;
1222 #if DEBUG_CORE_CLIENT
1223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1224 "Sending `%s' message to client.\n", "CONFIGURATION_INFO");
1226 tc = GNUNET_SERVER_transmit_context_create (client);
1227 GNUNET_SERVER_transmit_context_append_message (tc, &cim.header);
1228 GNUNET_SERVER_transmit_context_run (tc,
1229 GNUNET_TIME_UNIT_FOREVER_REL);
1234 * Free the given entry for the neighbour (it has
1235 * already been removed from the list at this point).
1237 * @param n neighbour to free
1240 free_neighbour (struct Neighbour *n)
1242 struct MessageEntry *m;
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1246 "Destroying neighbour entry for peer `%4s'\n",
1247 GNUNET_i2s (&n->peer));
1249 if (n->pitr != NULL)
1251 GNUNET_PEERINFO_iterate_cancel (n->pitr);
1256 GNUNET_free (n->skm);
1259 while (NULL != (m = n->messages))
1261 n->messages = m->next;
1264 while (NULL != (m = n->encrypted_head))
1266 GNUNET_CONTAINER_DLL_remove (n->encrypted_head,
1273 GNUNET_TRANSPORT_notify_transmit_ready_cancel (n->th);
1276 if (n->retry_plaintext_task != GNUNET_SCHEDULER_NO_TASK)
1277 GNUNET_SCHEDULER_cancel (sched, n->retry_plaintext_task);
1278 if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
1279 GNUNET_SCHEDULER_cancel (sched, n->retry_set_key_task);
1280 if (n->quota_update_task != GNUNET_SCHEDULER_NO_TASK)
1281 GNUNET_SCHEDULER_cancel (sched, n->quota_update_task);
1282 if (n->dead_clean_task != GNUNET_SCHEDULER_NO_TASK)
1283 GNUNET_SCHEDULER_cancel (sched, n->dead_clean_task);
1284 if (n->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
1285 GNUNET_SCHEDULER_cancel (sched, n->keep_alive_task);
1286 if (n->status == PEER_STATE_KEY_CONFIRMED)
1287 GNUNET_STATISTICS_update (stats, gettext_noop ("# established sessions"), -1, GNUNET_NO);
1288 GNUNET_free_non_null (n->public_key);
1289 GNUNET_free_non_null (n->pending_ping);
1290 GNUNET_free_non_null (n->pending_pong);
1296 * Check if we have encrypted messages for the specified neighbour
1297 * pending, and if so, check with the transport about sending them
1300 * @param n neighbour to check.
1302 static void process_encrypted_neighbour_queue (struct Neighbour *n);
1306 * Encrypt size bytes from in and write the result to out. Use the
1307 * key for outbound traffic of the given neighbour.
1309 * @param n neighbour we are sending to
1310 * @param iv initialization vector to use
1311 * @param in ciphertext
1312 * @param out plaintext
1313 * @param size size of in/out
1314 * @return GNUNET_OK on success
1317 do_encrypt (struct Neighbour *n,
1318 const struct GNUNET_CRYPTO_AesInitializationVector * iv,
1319 const void *in, void *out, size_t size)
1321 if (size != (uint16_t) size)
1326 GNUNET_assert (size ==
1327 GNUNET_CRYPTO_aes_encrypt (in,
1331 GNUNET_STATISTICS_update (stats, gettext_noop ("# bytes encrypted"), size, GNUNET_NO);
1333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1334 "Encrypted %u bytes for `%4s' using key %u, IV %u\n",
1335 (unsigned int) size,
1336 GNUNET_i2s (&n->peer),
1337 (unsigned int) n->encrypt_key.crc32,
1338 GNUNET_CRYPTO_crc32_n (iv, sizeof(iv)));
1345 * Consider freeing the given neighbour since we may not need
1346 * to keep it around anymore.
1348 * @param n neighbour to consider discarding
1351 consider_free_neighbour (struct Neighbour *n);
1355 * Task triggered when a neighbour entry is about to time out
1356 * (and we should prevent this by sending a PING).
1358 * @param cls the 'struct Neighbour'
1359 * @param tc scheduler context (not used)
1362 send_keep_alive (void *cls,
1363 const struct GNUNET_SCHEDULER_TaskContext *tc)
1365 struct Neighbour *n = cls;
1366 struct GNUNET_TIME_Relative retry;
1367 struct GNUNET_TIME_Relative left;
1368 struct MessageEntry *me;
1369 struct PingMessage pp;
1370 struct PingMessage *pm;
1371 struct GNUNET_CRYPTO_AesInitializationVector iv;
1373 n->keep_alive_task = GNUNET_SCHEDULER_NO_TASK;
1375 me = GNUNET_malloc (sizeof (struct MessageEntry) +
1376 sizeof (struct PingMessage));
1377 me->deadline = GNUNET_TIME_relative_to_absolute (MAX_PING_DELAY);
1378 me->priority = PING_PRIORITY;
1379 me->size = sizeof (struct PingMessage);
1380 GNUNET_CONTAINER_DLL_insert_after (n->encrypted_head,
1384 pm = (struct PingMessage *) &me[1];
1385 pm->header.size = htons (sizeof (struct PingMessage));
1386 pm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PING);
1387 pm->iv_seed = htonl (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1389 derive_iv (&iv, &n->encrypt_key, pm->iv_seed, &n->peer);
1390 pp.challenge = htonl (n->ping_challenge);
1391 pp.target = n->peer;
1393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1394 "Encrypting `%s' message with challenge %u for `%4s' using key %u, IV %u (salt %u).\n",
1396 (unsigned int) n->ping_challenge,
1397 GNUNET_i2s (&n->peer),
1398 (unsigned int) n->encrypt_key.crc32,
1399 GNUNET_CRYPTO_crc32_n (&iv, sizeof(iv)),
1406 sizeof (struct PingMessage) -
1407 ((void *) &pm->challenge - (void *) pm));
1408 process_encrypted_neighbour_queue (n);
1409 /* reschedule PING job */
1410 left = GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_add (n->last_activity,
1411 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1412 retry = GNUNET_TIME_relative_max (GNUNET_TIME_relative_divide (left, 2),
1413 MIN_PING_FREQUENCY);
1415 = GNUNET_SCHEDULER_add_delayed (sched,
1424 * Task triggered when a neighbour entry might have gotten stale.
1426 * @param cls the 'struct Neighbour'
1427 * @param tc scheduler context (not used)
1430 consider_free_task (void *cls,
1431 const struct GNUNET_SCHEDULER_TaskContext *tc)
1433 struct Neighbour *n = cls;
1435 n->dead_clean_task = GNUNET_SCHEDULER_NO_TASK;
1436 consider_free_neighbour (n);
1441 * Consider freeing the given neighbour since we may not need
1442 * to keep it around anymore.
1444 * @param n neighbour to consider discarding
1447 consider_free_neighbour (struct Neighbour *n)
1449 struct Neighbour *pos;
1450 struct Neighbour *prev;
1451 struct GNUNET_TIME_Relative left;
1453 if ( (n->th != NULL) ||
1454 (n->pitr != NULL) ||
1455 (GNUNET_YES == n->is_connected) )
1456 return; /* no chance */
1458 left = GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_add (n->last_activity,
1459 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1462 if (n->dead_clean_task != GNUNET_SCHEDULER_NO_TASK)
1463 GNUNET_SCHEDULER_cancel (sched, n->dead_clean_task);
1464 n->dead_clean_task = GNUNET_SCHEDULER_add_delayed (sched,
1466 &consider_free_task,
1470 /* actually free the neighbour... */
1479 neighbours = n->next;
1481 prev->next = n->next;
1482 GNUNET_assert (neighbour_count > 0);
1484 GNUNET_STATISTICS_set (stats,
1485 gettext_noop ("# neighbour entries allocated"),
1493 * Function called when the transport service is ready to
1494 * receive an encrypted message for the respective peer
1496 * @param cls neighbour to use message from
1497 * @param size number of bytes we can transmit
1498 * @param buf where to copy the message
1499 * @return number of bytes transmitted
1502 notify_encrypted_transmit_ready (void *cls, size_t size, void *buf)
1504 struct Neighbour *n = cls;
1505 struct MessageEntry *m;
1510 m = n->encrypted_head;
1514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1515 "Encrypted message queue empty, no messages added to buffer for `%4s'\n",
1516 GNUNET_i2s (&n->peer));
1520 GNUNET_CONTAINER_DLL_remove (n->encrypted_head,
1527 GNUNET_assert (size >= m->size);
1528 memcpy (cbuf, &m[1], m->size);
1530 GNUNET_BANDWIDTH_tracker_consume (&n->available_send_window,
1533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1534 "Copied message of type %u and size %u into transport buffer for `%4s'\n",
1535 (unsigned int) ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
1537 GNUNET_i2s (&n->peer));
1539 process_encrypted_neighbour_queue (n);
1544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1545 "Transmission of message of type %u and size %u failed\n",
1546 (unsigned int) ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
1547 (unsigned int) m->size);
1551 consider_free_neighbour (n);
1557 * Check if we have plaintext messages for the specified neighbour
1558 * pending, and if so, consider batching and encrypting them (and
1559 * then trigger processing of the encrypted queue if needed).
1561 * @param n neighbour to check.
1563 static void process_plaintext_neighbour_queue (struct Neighbour *n);
1567 * Check if we have encrypted messages for the specified neighbour
1568 * pending, and if so, check with the transport about sending them
1571 * @param n neighbour to check.
1574 process_encrypted_neighbour_queue (struct Neighbour *n)
1576 struct MessageEntry *m;
1579 return; /* request already pending */
1580 m = n->encrypted_head;
1583 /* encrypted queue empty, try plaintext instead */
1584 process_plaintext_neighbour_queue (n);
1588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1589 "Asking transport for transmission of %u bytes to `%4s' in next %llu ms\n",
1590 (unsigned int) m->size,
1591 GNUNET_i2s (&n->peer),
1592 (unsigned long long) GNUNET_TIME_absolute_get_remaining (m->deadline).
1596 GNUNET_TRANSPORT_notify_transmit_ready (transport, &n->peer,
1599 GNUNET_TIME_absolute_get_remaining
1601 ¬ify_encrypted_transmit_ready,
1605 /* message request too large or duplicate request */
1607 /* discard encrypted message */
1608 GNUNET_CONTAINER_DLL_remove (n->encrypted_head,
1612 process_encrypted_neighbour_queue (n);
1618 * Decrypt size bytes from in and write the result to out. Use the
1619 * key for inbound traffic of the given neighbour. This function does
1620 * NOT do any integrity-checks on the result.
1622 * @param n neighbour we are receiving from
1623 * @param iv initialization vector to use
1624 * @param in ciphertext
1625 * @param out plaintext
1626 * @param size size of in/out
1627 * @return GNUNET_OK on success
1630 do_decrypt (struct Neighbour *n,
1631 const struct GNUNET_CRYPTO_AesInitializationVector * iv,
1632 const void *in, void *out, size_t size)
1634 if (size != (uint16_t) size)
1639 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
1640 (n->status != PEER_STATE_KEY_CONFIRMED))
1642 GNUNET_break_op (0);
1643 return GNUNET_SYSERR;
1646 GNUNET_CRYPTO_aes_decrypt (in,
1653 return GNUNET_SYSERR;
1655 GNUNET_STATISTICS_update (stats, gettext_noop ("# bytes decrypted"), size, GNUNET_NO);
1657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1658 "Decrypted %u bytes from `%4s' using key %u, IV %u\n",
1659 (unsigned int) size,
1660 GNUNET_i2s (&n->peer),
1661 (unsigned int) n->decrypt_key.crc32,
1662 GNUNET_CRYPTO_crc32_n (iv, sizeof(*iv)));
1669 * Select messages for transmission. This heuristic uses a combination
1670 * of earliest deadline first (EDF) scheduling (with bounded horizon)
1671 * and priority-based discard (in case no feasible schedule exist) and
1672 * speculative optimization (defer any kind of transmission until
1673 * we either create a batch of significant size, 25% of max, or until
1674 * we are close to a deadline). Furthermore, when scheduling the
1675 * heuristic also packs as many messages into the batch as possible,
1676 * starting with those with the earliest deadline. Yes, this is fun.
1678 * @param n neighbour to select messages from
1679 * @param size number of bytes to select for transmission
1680 * @param retry_time set to the time when we should try again
1681 * (only valid if this function returns zero)
1682 * @return number of bytes selected, or 0 if we decided to
1683 * defer scheduling overall; in that case, retry_time is set.
1686 select_messages (struct Neighbour *n,
1687 size_t size, struct GNUNET_TIME_Relative *retry_time)
1689 struct MessageEntry *pos;
1690 struct MessageEntry *min;
1691 struct MessageEntry *last;
1692 unsigned int min_prio;
1693 struct GNUNET_TIME_Absolute t;
1694 struct GNUNET_TIME_Absolute now;
1695 struct GNUNET_TIME_Relative delta;
1697 struct GNUNET_TIME_Relative slack; /* how long could we wait before missing deadlines? */
1700 unsigned int queue_size;
1701 int discard_low_prio;
1703 GNUNET_assert (NULL != n->messages);
1704 now = GNUNET_TIME_absolute_get ();
1705 /* last entry in linked list of messages processed */
1707 /* should we remove the entry with the lowest
1708 priority from consideration for scheduling at the
1719 discard_low_prio = GNUNET_YES;
1720 while (GNUNET_YES == discard_low_prio)
1723 min_prio = UINT_MAX;
1724 discard_low_prio = GNUNET_NO;
1725 /* calculate number of bytes available for transmission at time "t" */
1726 avail = GNUNET_BANDWIDTH_tracker_get_available (&n->available_send_window);
1728 /* how many bytes have we (hypothetically) scheduled so far */
1730 /* maximum time we can wait before transmitting anything
1731 and still make all of our deadlines */
1732 slack = GNUNET_TIME_UNIT_FOREVER_REL;
1734 /* note that we use "*2" here because we want to look
1735 a bit further into the future; much more makes no
1736 sense since new message might be scheduled in the
1738 while ((pos != NULL) && (off < size * 2))
1740 if (pos->do_transmit == GNUNET_YES)
1742 /* already removed from consideration */
1746 if (discard_low_prio == GNUNET_NO)
1748 delta = GNUNET_TIME_absolute_get_difference (t, pos->deadline);
1749 if (delta.value > 0)
1751 // FIXME: HUH? Check!
1753 avail += GNUNET_BANDWIDTH_value_get_available_until (n->bw_out,
1756 if (avail < pos->size)
1758 // FIXME: HUH? Check!
1759 discard_low_prio = GNUNET_YES; /* we could not schedule this one! */
1764 /* update slack, considering both its absolute deadline
1765 and relative deadlines caused by other messages
1766 with their respective load */
1767 slack = GNUNET_TIME_relative_min (slack,
1768 GNUNET_BANDWIDTH_value_get_delay_for (n->bw_out,
1770 if (pos->deadline.value <= now.value)
1773 slack = GNUNET_TIME_UNIT_ZERO;
1775 else if (GNUNET_YES == pos->got_slack)
1777 /* should be soon now! */
1778 slack = GNUNET_TIME_relative_min (slack,
1779 GNUNET_TIME_absolute_get_remaining (pos->slack_deadline));
1784 GNUNET_TIME_relative_min (slack,
1785 GNUNET_TIME_absolute_get_difference (now, pos->deadline));
1786 pos->got_slack = GNUNET_YES;
1787 pos->slack_deadline = GNUNET_TIME_absolute_min (pos->deadline,
1788 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_MAX_CORK_DELAY));
1793 t = GNUNET_TIME_absolute_max (pos->deadline, t); // HUH? Check!
1794 if (pos->priority <= min_prio)
1796 /* update min for discard */
1797 min_prio = pos->priority;
1802 if (discard_low_prio)
1804 GNUNET_assert (min != NULL);
1805 /* remove lowest-priority entry from consideration */
1806 min->do_transmit = GNUNET_YES; /* means: discard (for now) */
1810 /* guard against sending "tiny" messages with large headers without
1812 if ( (slack.value > GNUNET_CONSTANTS_MAX_CORK_DELAY.value) &&
1814 (queue_size <= MAX_PEER_QUEUE_SIZE - 2) )
1816 /* less than 25% of message would be filled with deadlines still
1817 being met if we delay by one second or more; so just wait for
1818 more data; but do not wait longer than 1s (since we don't want
1819 to delay messages for a really long time either). */
1820 *retry_time = GNUNET_CONSTANTS_MAX_CORK_DELAY;
1821 /* reset do_transmit values for next time */
1824 pos->do_transmit = GNUNET_NO;
1828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1829 "Deferring transmission for %llums due to underfull message buffer size (%u/%u)\n",
1830 (unsigned long long) retry_time->value,
1832 (unsigned int) size);
1836 /* select marked messages (up to size) for transmission */
1841 if ((pos->size <= size) && (pos->do_transmit == GNUNET_NO))
1843 pos->do_transmit = GNUNET_YES; /* mark for transmission */
1847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1848 "Selecting message of size %u for transmission\n",
1849 (unsigned int) pos->size);
1855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1856 "Not selecting message of size %u for transmission at this time (maximum is %u)\n",
1857 (unsigned int) pos->size,
1860 pos->do_transmit = GNUNET_NO; /* mark for not transmitting! */
1865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1866 "Selected %llu/%llu bytes of %u/%u plaintext messages for transmission to `%4s'.\n",
1867 (unsigned long long) off, (unsigned long long) tsize,
1868 queue_size, (unsigned int) MAX_PEER_QUEUE_SIZE,
1869 GNUNET_i2s (&n->peer));
1876 * Batch multiple messages into a larger buffer.
1878 * @param n neighbour to take messages from
1879 * @param buf target buffer
1880 * @param size size of buf
1881 * @param deadline set to transmission deadline for the result
1882 * @param retry_time set to the time when we should try again
1883 * (only valid if this function returns zero)
1884 * @param priority set to the priority of the batch
1885 * @return number of bytes written to buf (can be zero)
1888 batch_message (struct Neighbour *n,
1891 struct GNUNET_TIME_Absolute *deadline,
1892 struct GNUNET_TIME_Relative *retry_time,
1893 unsigned int *priority)
1895 char ntmb[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
1896 struct NotifyTrafficMessage *ntm = (struct NotifyTrafficMessage*) ntmb;
1897 struct MessageEntry *pos;
1898 struct MessageEntry *prev;
1899 struct MessageEntry *next;
1904 *deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
1905 *retry_time = GNUNET_TIME_UNIT_FOREVER_REL;
1906 if (0 == select_messages (n, size, retry_time))
1909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1910 "No messages selected, will try again in %llu ms\n",
1915 ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND);
1916 ntm->distance = htonl (n->last_distance);
1917 ntm->latency = GNUNET_TIME_relative_hton (n->last_latency);
1918 ntm->peer = n->peer;
1921 while ((pos != NULL) && (size >= sizeof (struct GNUNET_MessageHeader)))
1924 if (GNUNET_YES == pos->do_transmit)
1926 GNUNET_assert (pos->size <= size);
1927 /* do notifications */
1928 /* FIXME: track if we have *any* client that wants
1929 full notifications and only do this if that is
1931 if (pos->size < GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct NotifyTrafficMessage))
1933 memcpy (&ntm[1], &pos[1], pos->size);
1934 ntm->header.size = htons (sizeof (struct NotifyTrafficMessage) +
1935 sizeof (struct GNUNET_MessageHeader));
1936 send_to_all_clients (&ntm->header,
1938 GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND);
1942 /* message too large for 'full' notifications, we do at
1943 least the 'hdr' type */
1946 sizeof (struct GNUNET_MessageHeader));
1948 ntm->header.size = htons (sizeof (struct NotifyTrafficMessage) +
1950 send_to_all_clients (&ntm->header,
1952 GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND);
1954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1955 "Encrypting %u bytes with message of type %u and size %u\n",
1957 (unsigned int) ntohs(((const struct GNUNET_MessageHeader*)&pos[1])->type),
1958 (unsigned int) ntohs(((const struct GNUNET_MessageHeader*)&pos[1])->size));
1960 /* copy for encrypted transmission */
1961 memcpy (&buf[ret], &pos[1], pos->size);
1964 *priority += pos->priority;
1966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1967 "Adding plaintext message of size %u with deadline %llu ms to batch\n",
1968 (unsigned int) pos->size,
1969 (unsigned long long) GNUNET_TIME_absolute_get_remaining (pos->deadline).value);
1971 deadline->value = GNUNET_MIN (deadline->value, pos->deadline.value);
1985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1986 "Deadline for message batch is %llu ms\n",
1987 GNUNET_TIME_absolute_get_remaining (*deadline).value);
1994 * Remove messages with deadlines that have long expired from
1997 * @param n neighbour to inspect
2000 discard_expired_messages (struct Neighbour *n)
2002 struct MessageEntry *prev;
2003 struct MessageEntry *next;
2004 struct MessageEntry *pos;
2005 struct GNUNET_TIME_Absolute now;
2006 struct GNUNET_TIME_Relative delta;
2008 now = GNUNET_TIME_absolute_get ();
2014 delta = GNUNET_TIME_absolute_get_difference (pos->deadline, now);
2015 if (delta.value > PAST_EXPIRATION_DISCARD_TIME.value)
2018 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2019 "Message is %llu ms past due, discarding.\n",
2036 * Signature of the main function of a task.
2038 * @param cls closure
2039 * @param tc context information (why was this task triggered now)
2042 retry_plaintext_processing (void *cls,
2043 const struct GNUNET_SCHEDULER_TaskContext *tc)
2045 struct Neighbour *n = cls;
2047 n->retry_plaintext_task = GNUNET_SCHEDULER_NO_TASK;
2048 process_plaintext_neighbour_queue (n);
2053 * Send our key (and encrypted PING) to the other peer.
2055 * @param n the other peer
2057 static void send_key (struct Neighbour *n);
2060 * Task that will retry "send_key" if our previous attempt failed
2064 set_key_retry_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2066 struct Neighbour *n = cls;
2069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2070 "Retrying key transmission to `%4s'\n",
2071 GNUNET_i2s (&n->peer));
2073 n->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
2074 n->set_key_retry_frequency =
2075 GNUNET_TIME_relative_multiply (n->set_key_retry_frequency, 2);
2081 * Check if we have plaintext messages for the specified neighbour
2082 * pending, and if so, consider batching and encrypting them (and
2083 * then trigger processing of the encrypted queue if needed).
2085 * @param n neighbour to check.
2088 process_plaintext_neighbour_queue (struct Neighbour *n)
2090 char pbuf[GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE + sizeof (struct EncryptedMessage)]; /* plaintext */
2093 struct EncryptedMessage *em; /* encrypted message */
2094 struct EncryptedMessage *ph; /* plaintext header */
2095 struct MessageEntry *me;
2096 unsigned int priority;
2097 struct GNUNET_TIME_Absolute deadline;
2098 struct GNUNET_TIME_Relative retry_time;
2099 struct GNUNET_CRYPTO_AesInitializationVector iv;
2101 if (n->retry_plaintext_task != GNUNET_SCHEDULER_NO_TASK)
2103 GNUNET_SCHEDULER_cancel (sched, n->retry_plaintext_task);
2104 n->retry_plaintext_task = GNUNET_SCHEDULER_NO_TASK;
2108 case PEER_STATE_DOWN:
2111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2112 "Not yet connected to `%4s', deferring processing of plaintext messages.\n",
2113 GNUNET_i2s(&n->peer));
2116 case PEER_STATE_KEY_SENT:
2117 if (n->retry_set_key_task == GNUNET_SCHEDULER_NO_TASK)
2118 n->retry_set_key_task
2119 = GNUNET_SCHEDULER_add_delayed (sched,
2120 n->set_key_retry_frequency,
2121 &set_key_retry_task, n);
2123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2124 "Not yet connected to `%4s', deferring processing of plaintext messages.\n",
2125 GNUNET_i2s(&n->peer));
2128 case PEER_STATE_KEY_RECEIVED:
2129 if (n->retry_set_key_task == GNUNET_SCHEDULER_NO_TASK)
2130 n->retry_set_key_task
2131 = GNUNET_SCHEDULER_add_delayed (sched,
2132 n->set_key_retry_frequency,
2133 &set_key_retry_task, n);
2135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2136 "Not yet connected to `%4s', deferring processing of plaintext messages.\n",
2137 GNUNET_i2s(&n->peer));
2140 case PEER_STATE_KEY_CONFIRMED:
2141 /* ready to continue */
2144 discard_expired_messages (n);
2145 if (n->messages == NULL)
2148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2149 "Plaintext message queue for `%4s' is empty.\n",
2150 GNUNET_i2s(&n->peer));
2152 return; /* no pending messages */
2154 if (n->encrypted_head != NULL)
2157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2158 "Encrypted message queue for `%4s' is still full, delaying plaintext processing.\n",
2159 GNUNET_i2s(&n->peer));
2161 return; /* wait for messages already encrypted to be
2164 ph = (struct EncryptedMessage *) pbuf;
2165 deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
2167 used = sizeof (struct EncryptedMessage);
2168 used += batch_message (n,
2170 GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE,
2171 &deadline, &retry_time, &priority);
2172 if (used == sizeof (struct EncryptedMessage))
2175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2176 "No messages selected for transmission to `%4s' at this time, will try again later.\n",
2177 GNUNET_i2s(&n->peer));
2179 /* no messages selected for sending, try again later... */
2180 n->retry_plaintext_task =
2181 GNUNET_SCHEDULER_add_delayed (sched,
2183 &retry_plaintext_processing, n);
2186 #if DEBUG_CORE_QUOTA
2187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2188 "Sending %u b/s as new limit to peer `%4s'\n",
2189 (unsigned int) ntohl (n->bw_in.value__),
2190 GNUNET_i2s (&n->peer));
2192 ph->iv_seed = htonl (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
2193 ph->sequence_number = htonl (++n->last_sequence_number_sent);
2194 ph->inbound_bw_limit = n->bw_in;
2195 ph->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
2197 /* setup encryption message header */
2198 me = GNUNET_malloc (sizeof (struct MessageEntry) + used);
2199 me->deadline = deadline;
2200 me->priority = priority;
2202 em = (struct EncryptedMessage *) &me[1];
2203 em->header.size = htons (used);
2204 em->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE);
2205 em->iv_seed = ph->iv_seed;
2206 esize = used - ENCRYPTED_HEADER_SIZE;
2207 GNUNET_CRYPTO_hmac (&n->encrypt_auth_key,
2208 &ph->sequence_number,
2209 esize - sizeof (GNUNET_HashCode),
2211 derive_iv (&iv, &n->encrypt_key, ph->iv_seed, &n->peer);
2213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2214 "Hashed %u bytes of plaintext (`%s') using IV `%d'\n",
2215 (unsigned int) (esize - sizeof (GNUNET_HashCode)),
2216 GNUNET_h2s (&ph->hmac),
2221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2222 "Encrypting %u bytes of plaintext messages for `%4s' for transmission in %llums.\n",
2223 (unsigned int) esize,
2224 GNUNET_i2s(&n->peer),
2225 (unsigned long long) GNUNET_TIME_absolute_get_remaining (deadline).value);
2227 GNUNET_assert (GNUNET_OK ==
2232 /* append to transmission list */
2233 GNUNET_CONTAINER_DLL_insert_after (n->encrypted_head,
2237 process_encrypted_neighbour_queue (n);
2242 * Function that recalculates the bandwidth quota for the
2243 * given neighbour and transmits it to the transport service.
2245 * @param cls neighbour for the quota update
2249 neighbour_quota_update (void *cls,
2250 const struct GNUNET_SCHEDULER_TaskContext *tc);
2254 * Schedule the task that will recalculate the bandwidth
2255 * quota for this peer (and possibly force a disconnect of
2256 * idle peers by calculating a bandwidth of zero).
2259 schedule_quota_update (struct Neighbour *n)
2261 GNUNET_assert (n->quota_update_task ==
2262 GNUNET_SCHEDULER_NO_TASK);
2263 n->quota_update_task
2264 = GNUNET_SCHEDULER_add_delayed (sched,
2265 QUOTA_UPDATE_FREQUENCY,
2266 &neighbour_quota_update,
2272 * Initialize a new 'struct Neighbour'.
2274 * @param pid ID of the new neighbour
2275 * @return handle for the new neighbour
2277 static struct Neighbour *
2278 create_neighbour (const struct GNUNET_PeerIdentity *pid)
2280 struct Neighbour *n;
2281 struct GNUNET_TIME_Absolute now;
2284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2285 "Creating neighbour entry for peer `%4s'\n",
2288 n = GNUNET_malloc (sizeof (struct Neighbour));
2289 n->next = neighbours;
2292 GNUNET_STATISTICS_set (stats, gettext_noop ("# neighbour entries allocated"), neighbour_count, GNUNET_NO);
2294 GNUNET_CRYPTO_aes_create_session_key (&n->encrypt_key);
2295 now = GNUNET_TIME_absolute_get ();
2296 n->encrypt_key_created = now;
2297 n->last_activity = now;
2298 n->set_key_retry_frequency = INITIAL_SET_KEY_RETRY_FREQUENCY;
2299 n->bw_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
2300 n->bw_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
2301 n->bw_out_internal_limit = GNUNET_BANDWIDTH_value_init (UINT32_MAX);
2302 n->bw_out_external_limit = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
2303 n->ping_challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
2305 derive_auth_key (&n->encrypt_auth_key, &n->encrypt_key, now, &n->peer);
2306 neighbour_quota_update (n, NULL);
2307 consider_free_neighbour (n);
2313 * Handle CORE_SEND request.
2316 * @param client the client issuing the request
2317 * @param message the "struct SendMessage"
2320 handle_client_send (void *cls,
2321 struct GNUNET_SERVER_Client *client,
2322 const struct GNUNET_MessageHeader *message)
2324 const struct SendMessage *sm;
2325 struct Neighbour *n;
2326 struct MessageEntry *prev;
2327 struct MessageEntry *pos;
2328 struct MessageEntry *e;
2329 struct MessageEntry *min_prio_entry;
2330 struct MessageEntry *min_prio_prev;
2331 unsigned int min_prio;
2332 unsigned int queue_size;
2335 msize = ntohs (message->size);
2337 sizeof (struct SendMessage) + sizeof (struct GNUNET_MessageHeader))
2339 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "about to assert fail, msize is %d, should be at least %d\n", msize, sizeof (struct SendMessage) + sizeof (struct GNUNET_MessageHeader));
2342 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2345 sm = (const struct SendMessage *) message;
2346 msize -= sizeof (struct SendMessage);
2347 if (0 == memcmp (&sm->peer, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
2349 /* FIXME: should we not allow loopback-injection here? */
2352 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2355 n = find_neighbour (&sm->peer);
2357 n = create_neighbour (&sm->peer);
2359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2360 "Core received `%s' request, queueing %u bytes of plaintext data for transmission to `%4s'.\n",
2362 (unsigned int) msize,
2363 GNUNET_i2s (&sm->peer));
2365 /* bound queue size */
2366 discard_expired_messages (n);
2367 min_prio = UINT32_MAX;
2368 min_prio_entry = NULL;
2369 min_prio_prev = NULL;
2375 if (pos->priority <= min_prio)
2377 min_prio_entry = pos;
2378 min_prio_prev = prev;
2379 min_prio = pos->priority;
2385 if (queue_size >= MAX_PEER_QUEUE_SIZE)
2388 if (ntohl(sm->priority) <= min_prio)
2390 /* discard new entry */
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2393 "Queue full (%u/%u), discarding new request (%u bytes of type %u)\n",
2395 (unsigned int) MAX_PEER_QUEUE_SIZE,
2396 (unsigned int) msize,
2397 (unsigned int) ntohs (message->type));
2400 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2403 GNUNET_assert (min_prio_entry != NULL);
2404 /* discard "min_prio_entry" */
2406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2407 "Queue full, discarding existing older request\n");
2409 if (min_prio_prev == NULL)
2410 n->messages = min_prio_entry->next;
2412 min_prio_prev->next = min_prio_entry->next;
2413 GNUNET_free (min_prio_entry);
2417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2418 "Adding transmission request for `%4s' of size %u to queue\n",
2419 GNUNET_i2s (&sm->peer),
2420 (unsigned int) msize);
2422 e = GNUNET_malloc (sizeof (struct MessageEntry) + msize);
2423 e->deadline = GNUNET_TIME_absolute_ntoh (sm->deadline);
2424 e->priority = ntohl (sm->priority);
2426 memcpy (&e[1], &sm[1], msize);
2428 /* insert, keep list sorted by deadline */
2431 while ((pos != NULL) && (pos->deadline.value < e->deadline.value))
2442 /* consider scheduling now */
2443 process_plaintext_neighbour_queue (n);
2445 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2450 * Function called when the transport service is ready to
2451 * receive a message. Only resets 'n->th' to NULL.
2453 * @param cls neighbour to use message from
2454 * @param size number of bytes we can transmit
2455 * @param buf where to copy the message
2456 * @return number of bytes transmitted
2459 notify_transport_connect_done (void *cls, size_t size, void *buf)
2461 struct Neighbour *n = cls;
2463 if (GNUNET_YES != n->is_connected)
2465 /* transport should only call us to transmit a message after
2466 * telling us about a successful connection to the respective peer */
2467 n->th = NULL; /* If this happens because of a timeout, reset n-th so another message may be sent for this peer! */
2469 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Timeout on notify connect!\n");
2476 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2477 _("Failed to connect to `%4s': transport failed to connect\n"),
2478 GNUNET_i2s (&n->peer));
2481 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2482 _("TRANSPORT connection to peer `%4s' is up, trying to establish CORE connection\n"),
2483 GNUNET_i2s (&n->peer));
2484 if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
2485 GNUNET_SCHEDULER_cancel (sched,
2486 n->retry_set_key_task);
2487 n->retry_set_key_task = GNUNET_SCHEDULER_add_now (sched,
2488 &set_key_retry_task,
2495 * Handle CORE_REQUEST_CONNECT request.
2498 * @param client the client issuing the request
2499 * @param message the "struct ConnectMessage"
2502 handle_client_request_connect (void *cls,
2503 struct GNUNET_SERVER_Client *client,
2504 const struct GNUNET_MessageHeader *message)
2506 const struct ConnectMessage *cm = (const struct ConnectMessage*) message;
2507 struct Neighbour *n;
2508 struct GNUNET_TIME_Relative timeout;
2510 if (0 == memcmp (&cm->peer, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
2513 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2516 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2517 n = find_neighbour (&cm->peer);
2519 n = create_neighbour (&cm->peer);
2520 if ( (GNUNET_YES == n->is_connected) ||
2523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2524 "Core received `%s' request for `%4s', already connected!\n",
2526 GNUNET_i2s (&cm->peer));
2527 return; /* already connected, or at least trying */
2529 GNUNET_STATISTICS_update (stats, gettext_noop ("# connection requests received"), 1, GNUNET_NO);
2531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2532 "Core received `%s' request for `%4s', will try to establish connection\n",
2534 GNUNET_i2s (&cm->peer));
2536 timeout = GNUNET_TIME_relative_ntoh (cm->timeout);
2537 /* ask transport to connect to the peer */
2538 n->th = GNUNET_TRANSPORT_notify_transmit_ready (transport,
2540 sizeof (struct GNUNET_MessageHeader), 0,
2542 ¬ify_transport_connect_done,
2544 GNUNET_break (NULL != n->th);
2549 * PEERINFO is giving us a HELLO for a peer. Add the public key to
2550 * the neighbour's struct and retry send_key. Or, if we did not get a
2551 * HELLO, just do nothing.
2553 * @param cls the 'struct Neighbour' to retry sending the key for
2554 * @param peer the peer for which this is the HELLO
2555 * @param hello HELLO message of that peer
2558 process_hello_retry_send_key (void *cls,
2559 const struct GNUNET_PeerIdentity *peer,
2560 const struct GNUNET_HELLO_Message *hello)
2562 struct Neighbour *n = cls;
2567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2568 "Entered `%s' and `%s' is NULL!\n",
2569 "process_hello_retry_send_key",
2573 if (n->public_key != NULL)
2575 if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
2577 GNUNET_SCHEDULER_cancel (sched, n->retry_set_key_task);
2578 n->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
2580 GNUNET_STATISTICS_update (stats,
2581 gettext_noop ("# SET_KEY messages deferred (need public key)"),
2589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2590 "Failed to obtain public key for peer `%4s', delaying processing of SET_KEY\n",
2591 GNUNET_i2s (&n->peer));
2593 GNUNET_STATISTICS_update (stats,
2594 gettext_noop ("# Delayed connecting due to lack of public key"),
2597 if (GNUNET_SCHEDULER_NO_TASK == n->retry_set_key_task)
2598 n->retry_set_key_task
2599 = GNUNET_SCHEDULER_add_delayed (sched,
2600 n->set_key_retry_frequency,
2601 &set_key_retry_task, n);
2607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2608 "Entered `%s' for peer `%4s'\n",
2609 "process_hello_retry_send_key",
2612 if (n->public_key != NULL)
2614 /* already have public key, why are we here? */
2620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2621 "Received new `%s' message for `%4s', initiating key exchange.\n",
2626 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2627 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, n->public_key))
2629 GNUNET_STATISTICS_update (stats,
2630 gettext_noop ("# Error extracting public key from HELLO"),
2633 GNUNET_free (n->public_key);
2634 n->public_key = NULL;
2636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2637 "GNUNET_HELLO_get_key returned awfully\n");
2645 * Send our key (and encrypted PING) to the other peer.
2647 * @param n the other peer
2650 send_key (struct Neighbour *n)
2652 struct MessageEntry *pos;
2653 struct SetKeyMessage *sm;
2654 struct MessageEntry *me;
2655 struct PingMessage pp;
2656 struct PingMessage *pm;
2657 struct GNUNET_CRYPTO_AesInitializationVector iv;
2659 if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
2661 GNUNET_SCHEDULER_cancel (sched, n->retry_set_key_task);
2662 n->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
2664 if (n->pitr != NULL)
2667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2668 "Key exchange in progress with `%4s'.\n",
2669 GNUNET_i2s (&n->peer));
2671 return; /* already in progress */
2673 if (GNUNET_YES != n->is_connected)
2676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2677 "Not yet connected to peer `%4s'!\n",
2678 GNUNET_i2s (&n->peer));
2682 GNUNET_STATISTICS_update (stats,
2683 gettext_noop ("# Asking transport to connect (for SET_KEY)"),
2686 n->th = GNUNET_TRANSPORT_notify_transmit_ready (transport,
2688 sizeof (struct SetKeyMessage) + sizeof (struct PingMessage),
2690 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2691 ¬ify_encrypted_transmit_ready,
2697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2698 "Asked to perform key exchange with `%4s'.\n",
2699 GNUNET_i2s (&n->peer));
2701 if (n->public_key == NULL)
2703 /* lookup n's public key, then try again */
2705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2706 "Lacking public key for `%4s', trying to obtain one (send_key).\n",
2707 GNUNET_i2s (&n->peer));
2709 GNUNET_assert (n->pitr == NULL);
2710 n->pitr = GNUNET_PEERINFO_iterate (peerinfo,
2712 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 20),
2713 &process_hello_retry_send_key, n);
2716 pos = n->encrypted_head;
2719 if (GNUNET_YES == pos->is_setkey)
2721 if (pos->sender_status == n->status)
2724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2725 "`%s' message for `%4s' queued already\n",
2727 GNUNET_i2s (&n->peer));
2729 goto trigger_processing;
2731 GNUNET_CONTAINER_DLL_remove (n->encrypted_head,
2736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2737 "Removing queued `%s' message for `%4s', will create a new one\n",
2739 GNUNET_i2s (&n->peer));
2749 case PEER_STATE_DOWN:
2750 n->status = PEER_STATE_KEY_SENT;
2752 case PEER_STATE_KEY_SENT:
2754 case PEER_STATE_KEY_RECEIVED:
2756 case PEER_STATE_KEY_CONFIRMED:
2764 /* first, set key message */
2765 me = GNUNET_malloc (sizeof (struct MessageEntry) +
2766 sizeof (struct SetKeyMessage) +
2767 sizeof (struct PingMessage));
2768 me->deadline = GNUNET_TIME_relative_to_absolute (MAX_SET_KEY_DELAY);
2769 me->priority = SET_KEY_PRIORITY;
2770 me->size = sizeof (struct SetKeyMessage) + sizeof (struct PingMessage);
2771 me->is_setkey = GNUNET_YES;
2772 me->got_slack = GNUNET_YES; /* do not defer this one! */
2773 me->sender_status = n->status;
2774 GNUNET_CONTAINER_DLL_insert_after (n->encrypted_head,
2778 sm = (struct SetKeyMessage *) &me[1];
2779 sm->header.size = htons (sizeof (struct SetKeyMessage));
2780 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SET_KEY);
2781 sm->sender_status = htonl ((int32_t) ((n->status == PEER_STATE_DOWN) ?
2782 PEER_STATE_KEY_SENT : n->status));
2784 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2785 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
2786 sizeof (struct GNUNET_CRYPTO_RsaEncryptedData) +
2787 sizeof (struct GNUNET_PeerIdentity));
2788 sm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_SET_KEY);
2789 sm->creation_time = GNUNET_TIME_absolute_hton (n->encrypt_key_created);
2790 sm->target = n->peer;
2791 GNUNET_assert (GNUNET_OK ==
2792 GNUNET_CRYPTO_rsa_encrypt (&n->encrypt_key,
2794 GNUNET_CRYPTO_AesSessionKey),
2796 &sm->encrypted_key));
2797 GNUNET_assert (GNUNET_OK ==
2798 GNUNET_CRYPTO_rsa_sign (my_private_key, &sm->purpose,
2800 pm = (struct PingMessage *) &sm[1];
2801 pm->header.size = htons (sizeof (struct PingMessage));
2802 pm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PING);
2803 pm->iv_seed = htonl (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
2804 derive_iv (&iv, &n->encrypt_key, pm->iv_seed, &n->peer);
2805 pp.challenge = htonl (n->ping_challenge);
2806 pp.target = n->peer;
2808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2809 "Encrypting `%s' and `%s' messages with challenge %u for `%4s' using key %u, IV %u (salt %u).\n",
2811 (unsigned int) n->ping_challenge,
2812 GNUNET_i2s (&n->peer),
2813 (unsigned int) n->encrypt_key.crc32,
2814 GNUNET_CRYPTO_crc32_n (&iv, sizeof(iv)),
2821 sizeof (struct PingMessage) -
2822 ((void *) &pm->challenge - (void *) pm));
2823 GNUNET_STATISTICS_update (stats,
2824 gettext_noop ("# SET_KEY and PING messages created"),
2828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2829 "Have %llu ms left for `%s' transmission.\n",
2830 (unsigned long long) GNUNET_TIME_absolute_get_remaining (me->deadline).value,
2834 /* trigger queue processing */
2835 process_encrypted_neighbour_queue (n);
2836 if ( (n->status != PEER_STATE_KEY_CONFIRMED) &&
2837 (GNUNET_SCHEDULER_NO_TASK == n->retry_set_key_task) )
2838 n->retry_set_key_task
2839 = GNUNET_SCHEDULER_add_delayed (sched,
2840 n->set_key_retry_frequency,
2841 &set_key_retry_task, n);
2846 * We received a SET_KEY message. Validate and update
2847 * our key material and status.
2849 * @param n the neighbour from which we received message m
2850 * @param m the set key message we received
2853 handle_set_key (struct Neighbour *n,
2854 const struct SetKeyMessage *m);
2858 * PEERINFO is giving us a HELLO for a peer. Add the public key to
2859 * the neighbour's struct and retry handling the set_key message. Or,
2860 * if we did not get a HELLO, just free the set key message.
2862 * @param cls pointer to the set key message
2863 * @param peer the peer for which this is the HELLO
2864 * @param hello HELLO message of that peer
2867 process_hello_retry_handle_set_key (void *cls,
2868 const struct GNUNET_PeerIdentity *peer,
2869 const struct GNUNET_HELLO_Message *hello)
2871 struct Neighbour *n = cls;
2872 struct SetKeyMessage *sm = n->skm;
2878 if (n->public_key != NULL)
2881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2882 "Received `%s' for `%4s', continuing processing of `%s' message.\n",
2884 GNUNET_i2s (&n->peer),
2887 handle_set_key (n, sm);
2891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2892 _("Ignoring `%s' message due to lack of public key for peer `%4s' (failed to obtain one).\n"),
2894 GNUNET_i2s (&n->peer));
2899 if (n->public_key != NULL)
2900 return; /* multiple HELLOs match!? */
2902 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2903 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, n->public_key))
2905 GNUNET_break_op (0);
2906 GNUNET_free (n->public_key);
2907 n->public_key = NULL;
2913 * We received a PING message. Validate and transmit
2916 * @param n sender of the PING
2917 * @param m the encrypted PING message itself
2920 handle_ping (struct Neighbour *n, const struct PingMessage *m)
2922 struct PingMessage t;
2923 struct PongMessage tx;
2924 struct PongMessage *tp;
2925 struct MessageEntry *me;
2926 struct GNUNET_CRYPTO_AesInitializationVector iv;
2929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2930 "Core service receives `%s' request from `%4s'.\n",
2931 "PING", GNUNET_i2s (&n->peer));
2933 derive_iv (&iv, &n->decrypt_key, m->iv_seed, &my_identity);
2939 sizeof (struct PingMessage) -
2940 ((void *) &m->challenge - (void *) m)))
2943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2944 "Decrypted `%s' to `%4s' with challenge %u decrypted using key %u, IV %u (salt %u)\n",
2946 GNUNET_i2s (&t.target),
2947 (unsigned int) ntohl (t.challenge),
2948 (unsigned int) n->decrypt_key.crc32,
2949 GNUNET_CRYPTO_crc32_n (&iv, sizeof(iv)),
2952 GNUNET_STATISTICS_update (stats,
2953 gettext_noop ("# PING messages decrypted"),
2956 if (0 != memcmp (&t.target,
2957 &my_identity, sizeof (struct GNUNET_PeerIdentity)))
2959 GNUNET_break_op (0);
2962 me = GNUNET_malloc (sizeof (struct MessageEntry) +
2963 sizeof (struct PongMessage));
2964 GNUNET_CONTAINER_DLL_insert_after (n->encrypted_head,
2968 me->deadline = GNUNET_TIME_relative_to_absolute (MAX_PONG_DELAY);
2969 me->priority = PONG_PRIORITY;
2970 me->size = sizeof (struct PongMessage);
2971 tx.reserved = htonl (0);
2972 tx.inbound_bw_limit = n->bw_in;
2973 tx.challenge = t.challenge;
2974 tx.target = t.target;
2975 tp = (struct PongMessage *) &me[1];
2976 tp->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PONG);
2977 tp->header.size = htons (sizeof (struct PongMessage));
2978 tp->iv_seed = htonl (GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
2979 derive_pong_iv (&iv, &n->encrypt_key, tp->iv_seed, t.challenge, &n->peer);
2984 sizeof (struct PongMessage) -
2985 ((void *) &tp->challenge - (void *) tp));
2986 GNUNET_STATISTICS_update (stats,
2987 gettext_noop ("# PONG messages created"),
2991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2992 "Encrypting `%s' with challenge %u using key %u, IV %u (salt %u)\n",
2994 (unsigned int) ntohl (t.challenge),
2995 (unsigned int) n->encrypt_key.crc32,
2996 GNUNET_CRYPTO_crc32_n (&iv, sizeof(iv)),
2999 /* trigger queue processing */
3000 process_encrypted_neighbour_queue (n);
3005 * We received a PONG message. Validate and update our status.
3007 * @param n sender of the PONG
3008 * @param m the encrypted PONG message itself
3011 handle_pong (struct Neighbour *n,
3012 const struct PongMessage *m)
3014 struct PongMessage t;
3015 struct ConnectNotifyMessage cnm;
3016 struct GNUNET_CRYPTO_AesInitializationVector iv;
3019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3020 "Core service receives `%s' response from `%4s'.\n",
3021 "PONG", GNUNET_i2s (&n->peer));
3023 /* mark as garbage, just to be sure */
3024 memset (&t, 255, sizeof (t));
3025 derive_pong_iv (&iv, &n->decrypt_key, m->iv_seed, htonl (n->ping_challenge),
3032 sizeof (struct PongMessage) -
3033 ((void *) &m->challenge - (void *) m)))
3035 GNUNET_break_op (0);
3038 GNUNET_STATISTICS_update (stats,
3039 gettext_noop ("# PONG messages decrypted"),
3043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3044 "Decrypted `%s' from `%4s' with challenge %u using key %u, IV %u (salt %u)\n",
3046 GNUNET_i2s (&t.target),
3047 (unsigned int) ntohl (t.challenge),
3048 (unsigned int) n->decrypt_key.crc32,
3049 GNUNET_CRYPTO_crc32_n (&iv, sizeof(iv)),
3052 if (0 != ntohl (t.reserved))
3054 GNUNET_break_op (0);
3057 if ((0 != memcmp (&t.target,
3059 sizeof (struct GNUNET_PeerIdentity))) ||
3060 (n->ping_challenge != ntohl (t.challenge)))
3062 /* PONG malformed */
3064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3065 "Received malformed `%s' wanted sender `%4s' with challenge %u\n",
3067 GNUNET_i2s (&n->peer),
3068 (unsigned int) n->ping_challenge);
3069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3070 "Received malformed `%s' received from `%4s' with challenge %u\n",
3071 "PONG", GNUNET_i2s (&t.target),
3072 (unsigned int) ntohl (t.challenge));
3074 GNUNET_break_op (0);
3079 case PEER_STATE_DOWN:
3080 GNUNET_break (0); /* should be impossible */
3082 case PEER_STATE_KEY_SENT:
3083 GNUNET_break (0); /* should be impossible, how did we decrypt? */
3085 case PEER_STATE_KEY_RECEIVED:
3086 GNUNET_STATISTICS_update (stats,
3087 gettext_noop ("# Session keys confirmed via PONG"),
3090 n->status = PEER_STATE_KEY_CONFIRMED;
3091 if (n->bw_out_external_limit.value__ != t.inbound_bw_limit.value__)
3093 n->bw_out_external_limit = t.inbound_bw_limit;
3094 n->bw_out = GNUNET_BANDWIDTH_value_min (n->bw_out_external_limit,
3095 n->bw_out_internal_limit);
3096 GNUNET_BANDWIDTH_tracker_update_quota (&n->available_send_window,
3098 GNUNET_TRANSPORT_set_quota (transport,
3102 GNUNET_TIME_UNIT_FOREVER_REL,
3106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3107 "Confirmed key via `%s' message for peer `%4s'\n",
3108 "PONG", GNUNET_i2s (&n->peer));
3110 if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
3112 GNUNET_SCHEDULER_cancel (sched, n->retry_set_key_task);
3113 n->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
3115 cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
3116 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
3117 cnm.distance = htonl (n->last_distance);
3118 cnm.latency = GNUNET_TIME_relative_hton (n->last_latency);
3120 send_to_all_clients (&cnm.header, GNUNET_YES, GNUNET_CORE_OPTION_SEND_CONNECT);
3121 process_encrypted_neighbour_queue (n);
3123 case PEER_STATE_KEY_CONFIRMED:
3124 n->last_activity = GNUNET_TIME_absolute_get ();
3125 if (n->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
3126 GNUNET_SCHEDULER_cancel (sched, n->keep_alive_task);
3128 = GNUNET_SCHEDULER_add_delayed (sched,
3129 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 2),
3132 handle_peer_status_change (n);
3142 * We received a SET_KEY message. Validate and update
3143 * our key material and status.
3145 * @param n the neighbour from which we received message m
3146 * @param m the set key message we received
3149 handle_set_key (struct Neighbour *n, const struct SetKeyMessage *m)
3151 struct SetKeyMessage *m_cpy;
3152 struct GNUNET_TIME_Absolute t;
3153 struct GNUNET_CRYPTO_AesSessionKey k;
3154 struct PingMessage *ping;
3155 struct PongMessage *pong;
3156 enum PeerStateMachine sender_status;
3159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3160 "Core service receives `%s' request from `%4s'.\n",
3161 "SET_KEY", GNUNET_i2s (&n->peer));
3163 if (n->public_key == NULL)
3165 if (n->pitr != NULL)
3168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3169 "Ignoring `%s' message due to lack of public key for peer (still trying to obtain one).\n",
3175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3176 "Lacking public key for peer, trying to obtain one (handle_set_key).\n");
3178 m_cpy = GNUNET_malloc (sizeof (struct SetKeyMessage));
3179 memcpy (m_cpy, m, sizeof (struct SetKeyMessage));
3180 /* lookup n's public key, then try again */
3181 GNUNET_assert (n->skm == NULL);
3183 n->pitr = GNUNET_PEERINFO_iterate (peerinfo,
3185 GNUNET_TIME_UNIT_MINUTES,
3186 &process_hello_retry_handle_set_key, n);
3187 GNUNET_STATISTICS_update (stats,
3188 gettext_noop ("# SET_KEY messages deferred (need public key)"),
3193 if (0 != memcmp (&m->target,
3195 sizeof (struct GNUNET_PeerIdentity)))
3197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3198 _("Received `%s' message that was for `%s', not for me. Ignoring.\n"),
3200 GNUNET_i2s (&m->target));
3203 if ((ntohl (m->purpose.size) !=
3204 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
3205 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
3206 sizeof (struct GNUNET_CRYPTO_RsaEncryptedData) +
3207 sizeof (struct GNUNET_PeerIdentity)) ||
3209 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_SET_KEY,
3210 &m->purpose, &m->signature, n->public_key)))
3212 /* invalid signature */
3213 GNUNET_break_op (0);
3216 t = GNUNET_TIME_absolute_ntoh (m->creation_time);
3217 if (((n->status == PEER_STATE_KEY_RECEIVED) ||
3218 (n->status == PEER_STATE_KEY_CONFIRMED)) &&
3219 (t.value < n->decrypt_key_created.value))
3221 /* this could rarely happen due to massive re-ordering of
3222 messages on the network level, but is most likely either
3223 a bug or some adversary messing with us. Report. */
3224 GNUNET_break_op (0);
3228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3229 "Decrypting key material.\n");
3231 if ((GNUNET_CRYPTO_rsa_decrypt (my_private_key,
3234 sizeof (struct GNUNET_CRYPTO_AesSessionKey))
3235 != sizeof (struct GNUNET_CRYPTO_AesSessionKey)) ||
3236 (GNUNET_OK != GNUNET_CRYPTO_aes_check_session_key (&k)))
3238 /* failed to decrypt !? */
3239 GNUNET_break_op (0);
3242 GNUNET_STATISTICS_update (stats,
3243 gettext_noop ("# SET_KEY messages decrypted"),
3247 derive_auth_key(&n->decrypt_auth_key, &n->decrypt_key, t, &my_identity);
3248 if (n->decrypt_key_created.value != t.value)
3250 /* fresh key, reset sequence numbers */
3251 n->last_sequence_number_received = 0;
3252 n->last_packets_bitmap = 0;
3253 n->decrypt_key_created = t;
3255 derive_auth_key(&n->decrypt_auth_key, &k, n->decrypt_key_created, &my_identity);
3256 sender_status = (enum PeerStateMachine) ntohl (m->sender_status);
3259 case PEER_STATE_DOWN:
3260 n->status = PEER_STATE_KEY_RECEIVED;
3262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3263 "Responding to `%s' with my own key.\n", "SET_KEY");
3267 case PEER_STATE_KEY_SENT:
3268 case PEER_STATE_KEY_RECEIVED:
3269 n->status = PEER_STATE_KEY_RECEIVED;
3270 if ((sender_status != PEER_STATE_KEY_RECEIVED) &&
3271 (sender_status != PEER_STATE_KEY_CONFIRMED))
3274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3275 "Responding to `%s' with my own key (other peer has status %u).\n",
3277 (unsigned int) sender_status);
3282 case PEER_STATE_KEY_CONFIRMED:
3283 if ((sender_status != PEER_STATE_KEY_RECEIVED) &&
3284 (sender_status != PEER_STATE_KEY_CONFIRMED))
3287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3288 "Responding to `%s' with my own key (other peer has status %u), I was already fully up.\n",
3290 (unsigned int) sender_status);
3299 if (n->pending_ping != NULL)
3301 ping = n->pending_ping;
3302 n->pending_ping = NULL;
3303 handle_ping (n, ping);
3306 if (n->pending_pong != NULL)
3308 pong = n->pending_pong;
3309 n->pending_pong = NULL;
3310 handle_pong (n, pong);
3317 * Send a P2P message to a client.
3319 * @param sender who sent us the message?
3320 * @param client who should we give the message to?
3321 * @param m contains the message to transmit
3322 * @param msize number of bytes in buf to transmit
3325 send_p2p_message_to_client (struct Neighbour *sender,
3326 struct Client *client,
3327 const void *m, size_t msize)
3329 char buf[msize + sizeof (struct NotifyTrafficMessage)];
3330 struct NotifyTrafficMessage *ntm;
3333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3334 "Core service passes message from `%4s' of type %u to client.\n",
3335 GNUNET_i2s(&sender->peer),
3336 (unsigned int) ntohs (((const struct GNUNET_MessageHeader *) m)->type));
3338 ntm = (struct NotifyTrafficMessage *) buf;
3339 ntm->header.size = htons (msize + sizeof (struct NotifyTrafficMessage));
3340 ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND);
3341 ntm->distance = htonl (sender->last_distance);
3342 ntm->latency = GNUNET_TIME_relative_hton (sender->last_latency);
3343 ntm->peer = sender->peer;
3344 memcpy (&ntm[1], m, msize);
3345 send_to_client (client, &ntm->header, GNUNET_YES);
3350 * Deliver P2P message to interested clients.
3352 * @param cls always NULL
3353 * @param client who sent us the message (struct Neighbour)
3354 * @param m the message
3357 deliver_message (void *cls,
3359 const struct GNUNET_MessageHeader *m)
3361 struct Neighbour *sender = client;
3362 size_t msize = ntohs (m->size);
3364 struct Client *cpos;
3370 type = ntohs (m->type);
3372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3373 "Received encapsulated message of type %u and size %u from `%4s'\n",
3374 (unsigned int) type,
3376 GNUNET_i2s (&sender->peer));
3378 GNUNET_snprintf (buf,
3380 gettext_noop ("# bytes of messages of type %u received"),
3381 (unsigned int) type);
3382 GNUNET_STATISTICS_set (stats,
3386 dropped = GNUNET_YES;
3388 while (cpos != NULL)
3390 deliver_full = GNUNET_NO;
3391 if (0 != (cpos->options & GNUNET_CORE_OPTION_SEND_FULL_INBOUND))
3392 deliver_full = GNUNET_YES;
3395 for (tpos = 0; tpos < cpos->tcnt; tpos++)
3397 if (type != cpos->types[tpos])
3399 deliver_full = GNUNET_YES;
3403 if (GNUNET_YES == deliver_full)
3405 send_p2p_message_to_client (sender, cpos, m, msize);
3406 dropped = GNUNET_NO;
3408 else if (cpos->options & GNUNET_CORE_OPTION_SEND_HDR_INBOUND)
3410 send_p2p_message_to_client (sender, cpos, m,
3411 sizeof (struct GNUNET_MessageHeader));
3415 if (dropped == GNUNET_YES)
3418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3419 "Message of type %u from `%4s' not delivered to any client.\n",
3420 (unsigned int) type,
3421 GNUNET_i2s (&sender->peer));
3423 GNUNET_STATISTICS_update (stats,
3424 gettext_noop ("# messages not delivered to any client"),
3431 * We received an encrypted message. Decrypt, validate and
3432 * pass on to the appropriate clients.
3435 handle_encrypted_message (struct Neighbour *n,
3436 const struct EncryptedMessage *m)
3438 size_t size = ntohs (m->header.size);
3440 struct EncryptedMessage *pt; /* plaintext */
3443 struct GNUNET_TIME_Absolute t;
3444 struct GNUNET_CRYPTO_AesInitializationVector iv;
3447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3448 "Core service receives `%s' request from `%4s'.\n",
3449 "ENCRYPTED_MESSAGE", GNUNET_i2s (&n->peer));
3451 derive_iv (&iv, &n->decrypt_key, m->iv_seed, &my_identity);
3457 &buf[ENCRYPTED_HEADER_SIZE],
3458 size - ENCRYPTED_HEADER_SIZE))
3460 pt = (struct EncryptedMessage *) buf;
3462 GNUNET_CRYPTO_hmac (&n->decrypt_auth_key,
3463 &pt->sequence_number,
3464 size - ENCRYPTED_HEADER_SIZE - sizeof (GNUNET_HashCode), &ph);
3466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3467 "V-Hashed %u bytes of plaintext (`%s') using IV `%d'\n",
3468 (unsigned int) (size - ENCRYPTED_HEADER_SIZE - sizeof (GNUNET_HashCode)),
3472 if (0 != memcmp (&ph,
3474 sizeof (GNUNET_HashCode)))
3476 /* checksum failed */
3477 GNUNET_break_op (0);
3481 /* validate sequence number */
3482 snum = ntohl (pt->sequence_number);
3483 if (n->last_sequence_number_received == snum)
3485 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3486 "Received duplicate message, ignoring.\n");
3487 /* duplicate, ignore */
3488 GNUNET_STATISTICS_set (stats,
3489 gettext_noop ("# bytes dropped (duplicates)"),
3494 if ((n->last_sequence_number_received > snum) &&
3495 (n->last_sequence_number_received - snum > 32))
3497 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3498 "Received ancient out of sequence message, ignoring.\n");
3499 /* ancient out of sequence, ignore */
3500 GNUNET_STATISTICS_set (stats,
3501 gettext_noop ("# bytes dropped (out of sequence)"),
3506 if (n->last_sequence_number_received > snum)
3508 unsigned int rotbit =
3509 1 << (n->last_sequence_number_received - snum - 1);
3510 if ((n->last_packets_bitmap & rotbit) != 0)
3512 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3513 "Received duplicate message, ignoring.\n");
3514 GNUNET_STATISTICS_set (stats,
3515 gettext_noop ("# bytes dropped (duplicates)"),
3518 /* duplicate, ignore */
3521 n->last_packets_bitmap |= rotbit;
3523 if (n->last_sequence_number_received < snum)
3525 n->last_packets_bitmap <<= (snum - n->last_sequence_number_received);
3526 n->last_sequence_number_received = snum;
3529 /* check timestamp */
3530 t = GNUNET_TIME_absolute_ntoh (pt->timestamp);
3531 if (GNUNET_TIME_absolute_get_duration (t).value > MAX_MESSAGE_AGE.value)
3533 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3535 ("Message received far too old (%llu ms). Content ignored.\n"),
3536 GNUNET_TIME_absolute_get_duration (t).value);
3537 GNUNET_STATISTICS_set (stats,
3538 gettext_noop ("# bytes dropped (ancient message)"),
3544 /* process decrypted message(s) */
3545 if (n->bw_out_external_limit.value__ != pt->inbound_bw_limit.value__)
3547 #if DEBUG_CORE_SET_QUOTA
3548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3549 "Received %u b/s as new inbound limit for peer `%4s'\n",
3550 (unsigned int) ntohl (pt->inbound_bw_limit.value__),
3551 GNUNET_i2s (&n->peer));
3553 n->bw_out_external_limit = pt->inbound_bw_limit;
3554 n->bw_out = GNUNET_BANDWIDTH_value_min (n->bw_out_external_limit,
3555 n->bw_out_internal_limit);
3556 GNUNET_BANDWIDTH_tracker_update_quota (&n->available_send_window,
3558 GNUNET_TRANSPORT_set_quota (transport,
3562 GNUNET_TIME_UNIT_FOREVER_REL,
3565 n->last_activity = GNUNET_TIME_absolute_get ();
3566 if (n->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
3567 GNUNET_SCHEDULER_cancel (sched, n->keep_alive_task);
3569 = GNUNET_SCHEDULER_add_delayed (sched,
3570 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 2),
3573 GNUNET_STATISTICS_set (stats,
3574 gettext_noop ("# bytes of payload decrypted"),
3575 size - sizeof (struct EncryptedMessage),
3577 handle_peer_status_change (n);
3578 if (GNUNET_OK != GNUNET_SERVER_mst_receive (mst,
3580 &buf[sizeof (struct EncryptedMessage)],
3581 size - sizeof (struct EncryptedMessage),
3582 GNUNET_YES, GNUNET_NO))
3583 GNUNET_break_op (0);
3588 * Function called by the transport for each received message.
3590 * @param cls closure
3591 * @param peer (claimed) identity of the other peer
3592 * @param message the message
3593 * @param latency estimated latency for communicating with the
3594 * given peer (round-trip)
3595 * @param distance in overlay hops, as given by transport plugin
3598 handle_transport_receive (void *cls,
3599 const struct GNUNET_PeerIdentity *peer,
3600 const struct GNUNET_MessageHeader *message,
3601 struct GNUNET_TIME_Relative latency,
3602 unsigned int distance)
3604 struct Neighbour *n;
3605 struct GNUNET_TIME_Absolute now;
3612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3613 "Received message of type %u from `%4s', demultiplexing.\n",
3614 (unsigned int) ntohs (message->type),
3617 if (0 == memcmp (peer, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
3622 n = find_neighbour (peer);
3624 n = create_neighbour (peer);
3625 changed = (latency.value != n->last_latency.value) || (distance != n->last_distance);
3626 n->last_latency = latency;
3627 n->last_distance = distance;
3628 up = (n->status == PEER_STATE_KEY_CONFIRMED);
3629 type = ntohs (message->type);
3630 size = ntohs (message->size);
3633 case GNUNET_MESSAGE_TYPE_CORE_SET_KEY:
3634 if (size != sizeof (struct SetKeyMessage))
3636 GNUNET_break_op (0);
3639 GNUNET_STATISTICS_update (stats, gettext_noop ("# session keys received"), 1, GNUNET_NO);
3640 handle_set_key (n, (const struct SetKeyMessage *) message);
3642 case GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE:
3643 if (size < sizeof (struct EncryptedMessage) +
3644 sizeof (struct GNUNET_MessageHeader))
3646 GNUNET_break_op (0);
3649 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
3650 (n->status != PEER_STATE_KEY_CONFIRMED))
3652 GNUNET_break_op (0);
3655 handle_encrypted_message (n, (const struct EncryptedMessage *) message);
3657 case GNUNET_MESSAGE_TYPE_CORE_PING:
3658 if (size != sizeof (struct PingMessage))
3660 GNUNET_break_op (0);
3663 GNUNET_STATISTICS_update (stats, gettext_noop ("# PING messages received"), 1, GNUNET_NO);
3664 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
3665 (n->status != PEER_STATE_KEY_CONFIRMED))
3668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3669 "Core service receives `%s' request from `%4s' but have not processed key; marking as pending.\n",
3670 "PING", GNUNET_i2s (&n->peer));
3672 GNUNET_free_non_null (n->pending_ping);
3673 n->pending_ping = GNUNET_malloc (sizeof (struct PingMessage));
3674 memcpy (n->pending_ping, message, sizeof (struct PingMessage));
3677 handle_ping (n, (const struct PingMessage *) message);
3679 case GNUNET_MESSAGE_TYPE_CORE_PONG:
3680 if (size != sizeof (struct PongMessage))
3682 GNUNET_break_op (0);
3685 GNUNET_STATISTICS_update (stats, gettext_noop ("# PONG messages received"), 1, GNUNET_NO);
3686 if ( (n->status != PEER_STATE_KEY_RECEIVED) &&
3687 (n->status != PEER_STATE_KEY_CONFIRMED) )
3690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3691 "Core service receives `%s' request from `%4s' but have not processed key; marking as pending.\n",
3692 "PONG", GNUNET_i2s (&n->peer));
3694 GNUNET_free_non_null (n->pending_pong);
3695 n->pending_pong = GNUNET_malloc (sizeof (struct PongMessage));
3696 memcpy (n->pending_pong, message, sizeof (struct PongMessage));
3699 handle_pong (n, (const struct PongMessage *) message);
3702 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3703 _("Unsupported message of type %u received.\n"),
3704 (unsigned int) type);
3707 if (n->status == PEER_STATE_KEY_CONFIRMED)
3709 now = GNUNET_TIME_absolute_get ();
3710 n->last_activity = now;
3711 changed = GNUNET_YES;
3714 GNUNET_STATISTICS_update (stats, gettext_noop ("# established sessions"), 1, GNUNET_NO);
3715 n->time_established = now;
3717 if (n->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
3718 GNUNET_SCHEDULER_cancel (sched, n->keep_alive_task);
3720 = GNUNET_SCHEDULER_add_delayed (sched,
3721 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, 2),
3726 handle_peer_status_change (n);
3731 * Function that recalculates the bandwidth quota for the
3732 * given neighbour and transmits it to the transport service.
3734 * @param cls neighbour for the quota update
3738 neighbour_quota_update (void *cls,
3739 const struct GNUNET_SCHEDULER_TaskContext *tc)
3741 struct Neighbour *n = cls;
3742 struct GNUNET_BANDWIDTH_Value32NBO q_in;
3745 unsigned long long distributable;
3746 uint64_t need_per_peer;
3747 uint64_t need_per_second;
3750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3751 "Neighbour quota update calculation running for peer `%4s'\n",
3752 GNUNET_i2s (&n->peer));
3754 n->quota_update_task = GNUNET_SCHEDULER_NO_TASK;
3755 /* calculate relative preference among all neighbours;
3756 divides by a bit more to avoid division by zero AND to
3757 account for possibility of new neighbours joining any time
3758 AND to convert to double... */
3759 if (preference_sum == 0)
3761 pref_rel = 1.0 / (double) neighbour_count;
3765 pref_rel = n->current_preference / preference_sum;
3767 need_per_peer = GNUNET_BANDWIDTH_value_get_available_until (MIN_BANDWIDTH_PER_PEER,
3768 GNUNET_TIME_UNIT_SECONDS);
3769 need_per_second = need_per_peer * neighbour_count;
3771 if (bandwidth_target_out_bps > need_per_second)
3772 distributable = bandwidth_target_out_bps - need_per_second;
3773 share = distributable * pref_rel;
3774 if (share + need_per_peer > UINT32_MAX)
3775 q_in = GNUNET_BANDWIDTH_value_init (UINT32_MAX);
3777 q_in = GNUNET_BANDWIDTH_value_init (need_per_peer + (uint32_t) share);
3778 /* check if we want to disconnect for good due to inactivity */
3779 if ( (GNUNET_TIME_absolute_get_duration (n->last_activity).value > GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.value) &&
3780 (GNUNET_TIME_absolute_get_duration (n->time_established).value > GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.value) )
3783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3784 "Forcing disconnect of `%4s' due to inactivity\n",
3785 GNUNET_i2s (&n->peer));
3787 q_in = GNUNET_BANDWIDTH_value_init (0); /* force disconnect */
3789 #if DEBUG_CORE_QUOTA
3790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3791 "Current quota for `%4s' is %u/%llu b/s in (old: %u b/s) / %u out (%u internal)\n",
3792 GNUNET_i2s (&n->peer),
3793 (unsigned int) ntohl (q_in.value__),
3794 bandwidth_target_out_bps,
3795 (unsigned int) ntohl (n->bw_in.value__),
3796 (unsigned int) ntohl (n->bw_out.value__),
3797 (unsigned int) ntohl (n->bw_out_internal_limit.value__));
3799 if (n->bw_in.value__ != q_in.value__)
3802 if (GNUNET_YES == n->is_connected)
3803 GNUNET_TRANSPORT_set_quota (transport,
3807 GNUNET_TIME_UNIT_FOREVER_REL,
3809 handle_peer_status_change (n);
3811 schedule_quota_update (n);
3816 * Function called by transport to notify us that
3817 * a peer connected to us (on the network level).
3819 * @param cls closure
3820 * @param peer the peer that connected
3821 * @param latency current latency of the connection
3822 * @param distance in overlay hops, as given by transport plugin
3825 handle_transport_notify_connect (void *cls,
3826 const struct GNUNET_PeerIdentity *peer,
3827 struct GNUNET_TIME_Relative latency,
3828 unsigned int distance)
3830 struct Neighbour *n;
3832 if (0 == memcmp (peer, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
3837 n = find_neighbour (peer);
3840 if (GNUNET_YES == n->is_connected)
3842 /* duplicate connect notification!? */
3849 n = create_neighbour (peer);
3851 GNUNET_STATISTICS_update (stats,
3852 gettext_noop ("# peers connected (transport)"),
3855 n->is_connected = GNUNET_YES;
3856 n->last_latency = latency;
3857 n->last_distance = distance;
3858 GNUNET_BANDWIDTH_tracker_init (&n->available_send_window,
3861 GNUNET_BANDWIDTH_tracker_init (&n->available_recv_window,
3865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3866 "Received connection from `%4s'.\n",
3867 GNUNET_i2s (&n->peer));
3869 GNUNET_TRANSPORT_set_quota (transport,
3873 GNUNET_TIME_UNIT_FOREVER_REL,
3880 * Function called by transport telling us that a peer
3883 * @param cls closure
3884 * @param peer the peer that disconnected
3887 handle_transport_notify_disconnect (void *cls,
3888 const struct GNUNET_PeerIdentity *peer)
3890 struct DisconnectNotifyMessage cnm;
3891 struct Neighbour *n;
3892 struct GNUNET_TIME_Relative left;
3895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3896 "Peer `%4s' disconnected from us; received notification from transport.\n", GNUNET_i2s (peer));
3899 n = find_neighbour (peer);
3905 GNUNET_break (n->is_connected);
3906 if (n->status == PEER_STATE_KEY_CONFIRMED)
3908 cnm.header.size = htons (sizeof (struct DisconnectNotifyMessage));
3909 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT);
3911 send_to_all_clients (&cnm.header, GNUNET_YES, GNUNET_CORE_OPTION_SEND_DISCONNECT);
3913 n->is_connected = GNUNET_NO;
3914 GNUNET_STATISTICS_update (stats,
3915 gettext_noop ("# peers connected (transport)"),
3918 if (n->dead_clean_task != GNUNET_SCHEDULER_NO_TASK)
3919 GNUNET_SCHEDULER_cancel (sched,
3920 n->dead_clean_task);
3921 left = GNUNET_TIME_relative_subtract (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
3922 GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT);
3923 n->last_activity = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (),
3925 n->dead_clean_task = GNUNET_SCHEDULER_add_delayed (sched,
3926 GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
3927 &consider_free_task,
3933 * Last task run during shutdown. Disconnects us from
3937 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3939 struct Neighbour *n;
3943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3944 "Core service shutting down.\n");
3946 GNUNET_assert (transport != NULL);
3947 GNUNET_TRANSPORT_disconnect (transport);
3949 while (NULL != (n = neighbours))
3951 neighbours = n->next;
3952 GNUNET_assert (neighbour_count > 0);
3956 GNUNET_STATISTICS_set (stats, gettext_noop ("# neighbour entries allocated"), neighbour_count, GNUNET_NO);
3957 GNUNET_SERVER_notification_context_destroy (notifier);
3959 while (NULL != (c = clients))
3960 handle_client_disconnect (NULL, c->client_handle);
3961 if (my_private_key != NULL)
3962 GNUNET_CRYPTO_rsa_key_free (my_private_key);
3964 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
3965 if (peerinfo != NULL)
3966 GNUNET_PEERINFO_disconnect (peerinfo);
3968 GNUNET_SERVER_mst_destroy (mst);
3973 * Initiate core service.
3975 * @param cls closure
3976 * @param s scheduler to use
3977 * @param server the initialized server
3978 * @param c configuration to use
3982 struct GNUNET_SCHEDULER_Handle *s,
3983 struct GNUNET_SERVER_Handle *server,
3984 const struct GNUNET_CONFIGURATION_Handle *c)
3986 static const struct GNUNET_SERVER_MessageHandler handlers[] = {
3987 {&handle_client_init, NULL,
3988 GNUNET_MESSAGE_TYPE_CORE_INIT, 0},
3989 {&handle_client_request_info, NULL,
3990 GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO,
3991 sizeof (struct RequestInfoMessage)},
3992 {&handle_client_iterate_peers, NULL,
3993 GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS,
3994 sizeof (struct GNUNET_MessageHeader)},
3995 {&handle_client_send, NULL,
3996 GNUNET_MESSAGE_TYPE_CORE_SEND, 0},
3997 {&handle_client_request_connect, NULL,
3998 GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT,
3999 sizeof (struct ConnectMessage)},
4006 /* parse configuration */
4009 GNUNET_CONFIGURATION_get_value_number (c,
4012 &bandwidth_target_in_bps)) ||
4014 GNUNET_CONFIGURATION_get_value_number (c,
4017 &bandwidth_target_out_bps)) ||
4019 GNUNET_CONFIGURATION_get_value_filename (c,
4021 "HOSTKEY", &keyfile)))
4023 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4025 ("Core service is lacking key configuration settings. Exiting.\n"));
4026 GNUNET_SCHEDULER_shutdown (s);
4029 peerinfo = GNUNET_PEERINFO_connect (sched, cfg);
4030 if (NULL == peerinfo)
4032 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4033 _("Could not access PEERINFO service. Exiting.\n"));
4034 GNUNET_SCHEDULER_shutdown (s);
4035 GNUNET_free (keyfile);
4038 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
4039 GNUNET_free (keyfile);
4040 if (my_private_key == NULL)
4042 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4043 _("Core service could not access hostkey. Exiting.\n"));
4044 GNUNET_PEERINFO_disconnect (peerinfo);
4045 GNUNET_SCHEDULER_shutdown (s);
4048 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
4049 GNUNET_CRYPTO_hash (&my_public_key,
4050 sizeof (my_public_key), &my_identity.hashPubKey);
4051 /* setup notification */
4052 notifier = GNUNET_SERVER_notification_context_create (server,
4054 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
4055 /* setup transport connection */
4056 transport = GNUNET_TRANSPORT_connect (sched,
4060 &handle_transport_receive,
4061 &handle_transport_notify_connect,
4062 &handle_transport_notify_disconnect);
4063 GNUNET_assert (NULL != transport);
4064 stats = GNUNET_STATISTICS_create (sched, "core", cfg);
4065 mst = GNUNET_SERVER_mst_create (&deliver_message,
4067 GNUNET_SCHEDULER_add_delayed (sched,
4068 GNUNET_TIME_UNIT_FOREVER_REL,
4069 &cleaning_task, NULL);
4070 /* process client requests */
4071 GNUNET_SERVER_add_handlers (server, handlers);
4072 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4073 _("Core service of `%4s' ready.\n"), GNUNET_i2s (&my_identity));
4079 * The main function for the transport service.
4081 * @param argc number of arguments from the command line
4082 * @param argv command line arguments
4083 * @return 0 ok, 1 on error
4086 main (int argc, char *const *argv)
4088 return (GNUNET_OK ==
4089 GNUNET_SERVICE_run (argc,
4092 GNUNET_SERVICE_OPTION_NONE,
4093 &run, NULL)) ? 0 : 1;
4096 /* end of gnunet-service-core.c */