2 This file is part of GNUnet.
3 (C) 2009 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 2, 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_transport_service.h"
44 #define DEBUG_HANDSHAKE GNUNET_NO
46 #define DEBUG_CORE_QUOTA GNUNET_YES
49 * Receive and send buffer windows grow over time. For
50 * how long can 'unused' bandwidth accumulate before we
51 * need to cap it? (specified in seconds).
53 #define MAX_WINDOW_TIME_S (5 * 60)
56 * How many messages do we queue up at most for optional
57 * notifications to a client? (this can cause notifications
58 * about outgoing messages to be dropped).
60 #define MAX_NOTIFY_QUEUE 16
63 * Minimum bandwidth (out) to assign to any connected peer.
64 * Should be rather low; values larger than DEFAULT_BW_IN_OUT make no
67 #define MIN_BANDWIDTH_PER_PEER GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT
70 * After how much time past the "official" expiration time do
71 * we discard messages? Should not be zero since we may
72 * intentionally defer transmission until close to the deadline
73 * and then may be slightly past the deadline due to inaccuracy
74 * in sleep and our own CPU consumption.
76 #define PAST_EXPIRATION_DISCARD_TIME GNUNET_TIME_UNIT_SECONDS
79 * How long do we delay messages to get larger packet sizes (CORKing)?
81 #define MAX_CORK_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
84 * What is the maximum delay for a SET_KEY message?
86 #define MAX_SET_KEY_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
89 * What how long do we wait for SET_KEY confirmation initially?
91 #define INITIAL_SET_KEY_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (MAX_SET_KEY_DELAY, 3)
94 * What is the maximum delay for a PING message?
96 #define MAX_PING_DELAY GNUNET_TIME_relative_multiply (MAX_SET_KEY_DELAY, 2)
99 * What is the maximum delay for a PONG message?
101 #define MAX_PONG_DELAY GNUNET_TIME_relative_multiply (MAX_PING_DELAY, 2)
104 * How often do we recalculate bandwidth quotas?
106 #define QUOTA_UPDATE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
109 * What is the priority for a SET_KEY message?
111 #define SET_KEY_PRIORITY 0xFFFFFF
114 * What is the priority for a PING message?
116 #define PING_PRIORITY 0xFFFFFF
119 * What is the priority for a PONG message?
121 #define PONG_PRIORITY 0xFFFFFF
124 * How many messages do we queue per peer at most? Must be at
127 #define MAX_PEER_QUEUE_SIZE 16
130 * How many non-mandatory messages do we queue per client at most?
132 #define MAX_CLIENT_QUEUE_SIZE 32
135 * What is the maximum age of a message for us to consider
136 * processing it? Note that this looks at the timestamp used
137 * by the other peer, so clock skew between machines does
138 * come into play here. So this should be picked high enough
139 * so that a little bit of clock skew does not prevent peers
140 * from connecting to us.
142 #define MAX_MESSAGE_AGE GNUNET_TIME_UNIT_DAYS
145 * What is the maximum size for encrypted messages? Note that this
146 * number imposes a clear limit on the maximum size of any message.
147 * Set to a value close to 64k but not so close that transports will
148 * have trouble with their headers.
150 #define MAX_ENCRYPTED_MESSAGE_SIZE (63 * 1024)
154 * State machine for our P2P encryption handshake. Everyone starts in
155 * "DOWN", if we receive the other peer's key (other peer initiated)
156 * we start in state RECEIVED (since we will immediately send our
157 * own); otherwise we start in SENT. If we get back a PONG from
158 * within either state, we move up to CONFIRMED (the PONG will always
159 * be sent back encrypted with the key we sent to the other peer).
161 enum PeerStateMachine
165 PEER_STATE_KEY_RECEIVED,
166 PEER_STATE_KEY_CONFIRMED
171 * Number of bytes (at the beginning) of "struct EncryptedMessage"
172 * that are NOT encrypted.
174 #define ENCRYPTED_HEADER_SIZE (sizeof(struct GNUNET_MessageHeader) + sizeof(uint32_t))
178 * Encapsulation for encrypted messages exchanged between
179 * peers. Followed by the actual encrypted data.
181 struct EncryptedMessage
184 * Message type is either CORE_ENCRYPTED_MESSAGE.
186 struct GNUNET_MessageHeader header;
189 * Random value used for IV generation. ENCRYPTED_HEADER_SIZE must
190 * be set to the offset of the *next* field.
192 uint32_t iv_seed GNUNET_PACKED;
195 * Hash of the plaintext (starting at 'sequence_number'), used to
196 * verify message integrity. Everything after this hash (including
197 * this hash itself) will be encrypted.
199 GNUNET_HashCode plaintext_hash;
202 * Sequence number, in network byte order. This field
203 * must be the first encrypted/decrypted field and the
204 * first byte that is hashed for the plaintext hash.
206 uint32_t sequence_number GNUNET_PACKED;
209 * Desired bandwidth (how much we should send to this peer / how
210 * much is the sender willing to receive)?
212 struct GNUNET_BANDWIDTH_Value32NBO inbound_bw_limit;
215 * Timestamp. Used to prevent reply of ancient messages
216 * (recent messages are caught with the sequence number).
218 struct GNUNET_TIME_AbsoluteNBO timestamp;
224 * We're sending an (encrypted) PING to the other peer to check if he
225 * can decrypt. The other peer should respond with a PONG with the
226 * same content, except this time encrypted with the receiver's key.
231 * Message type is CORE_PING.
233 struct GNUNET_MessageHeader header;
236 * Random number chosen to make reply harder.
238 uint32_t challenge GNUNET_PACKED;
241 * Intended target of the PING, used primarily to check
242 * that decryption actually worked.
244 struct GNUNET_PeerIdentity target;
250 * Response to a PING. Includes data from the original PING
251 * plus initial bandwidth quota information.
256 * Message type is CORE_PONG.
258 struct GNUNET_MessageHeader header;
261 * Random number proochosen to make reply harder. Must be
262 * first field after header (this is where we start to encrypt!).
264 uint32_t challenge GNUNET_PACKED;
269 uint32_t reserved GNUNET_PACKED;
272 * Desired bandwidth (how much we should send to this
273 * peer / how much is the sender willing to receive).
275 struct GNUNET_BANDWIDTH_Value32NBO inbound_bw_limit;
278 * Intended target of the PING, used primarily to check
279 * that decryption actually worked.
281 struct GNUNET_PeerIdentity target;
286 * Message transmitted to set (or update) a session key.
292 * Message type is either CORE_SET_KEY.
294 struct GNUNET_MessageHeader header;
297 * Status of the sender (should be in "enum PeerStateMachine"), nbo.
299 int32_t sender_status GNUNET_PACKED;
302 * Purpose of the signature, will be
303 * GNUNET_SIGNATURE_PURPOSE_SET_KEY.
305 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
308 * At what time was this key created?
310 struct GNUNET_TIME_AbsoluteNBO creation_time;
313 * The encrypted session key.
315 struct GNUNET_CRYPTO_RsaEncryptedData encrypted_key;
318 * Who is the intended recipient?
320 struct GNUNET_PeerIdentity target;
323 * Signature of the stuff above (starting at purpose).
325 struct GNUNET_CRYPTO_RsaSignature signature;
331 * Message waiting for transmission. This struct
332 * is followed by the actual content of the message.
338 * We keep messages in a doubly linked list.
340 struct MessageEntry *next;
343 * We keep messages in a doubly linked list.
345 struct MessageEntry *prev;
348 * By when are we supposed to transmit this message?
350 struct GNUNET_TIME_Absolute deadline;
353 * By when are we supposed to transmit this message (after
356 struct GNUNET_TIME_Absolute slack_deadline;
359 * How important is this message to us?
361 unsigned int priority;
364 * How long is the message? (number of bytes following
365 * the "struct MessageEntry", but not including the
366 * size of "struct MessageEntry" itself!)
371 * Was this message selected for transmission in the
372 * current round? GNUNET_YES or GNUNET_NO.
377 * Did we give this message some slack (delayed sending) previously
378 * (and hence should not give it any more slack)? GNUNET_YES or
389 * We keep neighbours in a linked list (for now).
391 struct Neighbour *next;
394 * Unencrypted messages destined for this peer.
396 struct MessageEntry *messages;
399 * Head of the batched, encrypted message queue (already ordered,
400 * transmit starting with the head).
402 struct MessageEntry *encrypted_head;
405 * Tail of the batched, encrypted message queue (already ordered,
406 * append new messages to tail)
408 struct MessageEntry *encrypted_tail;
411 * Handle for pending requests for transmission to this peer
412 * with the transport service. NULL if no request is pending.
414 struct GNUNET_TRANSPORT_TransmitHandle *th;
417 * Public key of the neighbour, NULL if we don't have it yet.
419 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key;
422 * We received a PING message before we got the "public_key"
423 * (or the SET_KEY). We keep it here until we have a key
424 * to decrypt it. NULL if no PING is pending.
426 struct PingMessage *pending_ping;
429 * We received a PONG message before we got the "public_key"
430 * (or the SET_KEY). We keep it here until we have a key
431 * to decrypt it. NULL if no PONG is pending.
433 struct PongMessage *pending_pong;
436 * Non-NULL if we are currently looking up HELLOs for this peer.
439 struct GNUNET_PEERINFO_IteratorContext *pitr;
442 * SetKeyMessage to transmit, NULL if we are not currently trying
445 struct SetKeyMessage *skm;
448 * Identity of the neighbour.
450 struct GNUNET_PeerIdentity peer;
453 * Key we use to encrypt our messages for the other peer
454 * (initialized by us when we do the handshake).
456 struct GNUNET_CRYPTO_AesSessionKey encrypt_key;
459 * Key we use to decrypt messages from the other peer
460 * (given to us by the other peer during the handshake).
462 struct GNUNET_CRYPTO_AesSessionKey decrypt_key;
465 * ID of task used for re-trying plaintext scheduling.
467 GNUNET_SCHEDULER_TaskIdentifier retry_plaintext_task;
470 * ID of task used for re-trying SET_KEY and PING message.
472 GNUNET_SCHEDULER_TaskIdentifier retry_set_key_task;
475 * ID of task used for updating bandwidth quota for this neighbour.
477 GNUNET_SCHEDULER_TaskIdentifier quota_update_task;
480 * ID of task used for cleaning up dead neighbour entries.
482 GNUNET_SCHEDULER_TaskIdentifier dead_clean_task;
485 * At what time did we generate our encryption key?
487 struct GNUNET_TIME_Absolute encrypt_key_created;
490 * At what time did the other peer generate the decryption key?
492 struct GNUNET_TIME_Absolute decrypt_key_created;
495 * At what time did we initially establish (as in, complete session
496 * key handshake) this connection? Should be zero if status != KEY_CONFIRMED.
498 struct GNUNET_TIME_Absolute time_established;
501 * At what time did we last receive an encrypted message from the
502 * other peer? Should be zero if status != KEY_CONFIRMED.
504 struct GNUNET_TIME_Absolute last_activity;
507 * Last latency observed from this peer.
509 struct GNUNET_TIME_Relative last_latency;
512 * At what frequency are we currently re-trying SET_KEY messages?
514 struct GNUNET_TIME_Relative set_key_retry_frequency;
517 * Tracking bandwidth for sending to this peer.
519 struct GNUNET_BANDWIDTH_Tracker available_send_window;
522 * Tracking bandwidth for receiving from this peer.
524 struct GNUNET_BANDWIDTH_Tracker available_recv_window;
527 * How valueable were the messages of this peer recently?
529 unsigned long long current_preference;
532 * Bit map indicating which of the 32 sequence numbers before the last
533 * were received (good for accepting out-of-order packets and
534 * estimating reliability of the connection)
536 unsigned int last_packets_bitmap;
539 * last sequence number received on this connection (highest)
541 uint32_t last_sequence_number_received;
544 * last sequence number transmitted
546 uint32_t last_sequence_number_sent;
549 * Available bandwidth in for this peer (current target).
551 struct GNUNET_BANDWIDTH_Value32NBO bw_in;
554 * Available bandwidth out for this peer (current target).
556 struct GNUNET_BANDWIDTH_Value32NBO bw_out;
559 * Internal bandwidth limit set for this peer (initially typically
560 * set to "-1"). Actual "bw_out" is MIN of
561 * "bpm_out_internal_limit" and "bw_out_external_limit".
563 struct GNUNET_BANDWIDTH_Value32NBO bw_out_internal_limit;
566 * External bandwidth limit set for this peer by the
567 * peer that we are communicating with. "bw_out" is MIN of
568 * "bw_out_internal_limit" and "bw_out_external_limit".
570 struct GNUNET_BANDWIDTH_Value32NBO bw_out_external_limit;
573 * What was our PING challenge number (for this peer)?
575 uint32_t ping_challenge;
578 * What was the last distance to this peer as reported by the transports?
580 uint32_t last_distance;
583 * What is our connection status?
585 enum PeerStateMachine status;
588 * Are we currently connected to this neighbour?
596 * Data structure for each client connected to the core service.
601 * Clients are kept in a linked list.
606 * Handle for the client with the server API.
608 struct GNUNET_SERVER_Client *client_handle;
611 * Array of the types of messages this peer cares
612 * about (with "tcnt" entries). Allocated as part
613 * of this client struct, do not free!
615 const uint16_t *types;
618 * Options for messages this client cares about,
619 * see GNUNET_CORE_OPTION_ values.
624 * Number of types of incoming messages this client
625 * specifically cares about. Size of the "types" array.
635 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
640 static struct GNUNET_PeerIdentity my_identity;
645 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
650 struct GNUNET_SCHEDULER_Handle *sched;
655 const struct GNUNET_CONFIGURATION_Handle *cfg;
660 static struct GNUNET_SERVER_Handle *server;
665 static struct GNUNET_TRANSPORT_Handle *transport;
668 * Linked list of our clients.
670 static struct Client *clients;
673 * Context for notifications we need to send to our clients.
675 static struct GNUNET_SERVER_NotificationContext *notifier;
678 * We keep neighbours in a linked list (for now).
680 static struct Neighbour *neighbours;
683 * Sum of all preferences among all neighbours.
685 static unsigned long long preference_sum;
688 * Total number of neighbours we have.
690 static unsigned int neighbour_count;
693 * How much inbound bandwidth are we supposed to be using per second?
694 * FIXME: this value is not used!
696 static unsigned long long bandwidth_target_in_bps;
699 * How much outbound bandwidth are we supposed to be using per second?
701 static unsigned long long bandwidth_target_out_bps;
706 * A preference value for a neighbour was update. Update
707 * the preference sum accordingly.
709 * @param inc how much was a preference value increased?
712 update_preference_sum (unsigned long long inc)
715 unsigned long long os;
718 preference_sum += inc;
719 if (preference_sum >= os)
721 /* overflow! compensate by cutting all values in half! */
726 n->current_preference /= 2;
727 preference_sum += n->current_preference;
734 * Find the entry for the given neighbour.
736 * @param peer identity of the neighbour
737 * @return NULL if we are not connected, otherwise the
740 static struct Neighbour *
741 find_neighbour (const struct GNUNET_PeerIdentity *peer)
743 struct Neighbour *ret;
746 while ((ret != NULL) &&
747 (0 != memcmp (&ret->peer,
748 peer, sizeof (struct GNUNET_PeerIdentity))))
755 * Send a message to one of our clients.
757 * @param client target for the message
758 * @param msg message to transmit
759 * @param can_drop could this message be dropped if the
760 * client's queue is getting too large?
763 send_to_client (struct Client *client,
764 const struct GNUNET_MessageHeader *msg,
767 #if DEBUG_CORE_CLIENT
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
769 "Preparing to send message of type %u to client.\n",
772 GNUNET_SERVER_notification_context_unicast (notifier,
773 client->client_handle,
780 * Send a message to all of our current clients that have
781 * the right options set.
783 * @param msg message to multicast
784 * @param can_drop can this message be discarded if the queue is too long
785 * @param options mask to use
788 send_to_all_clients (const struct GNUNET_MessageHeader *msg,
797 if (0 != (c->options & options))
798 send_to_client (c, msg, can_drop);
805 * Handle CORE_INIT request.
808 handle_client_init (void *cls,
809 struct GNUNET_SERVER_Client *client,
810 const struct GNUNET_MessageHeader *message)
812 const struct InitMessage *im;
813 struct InitReplyMessage irm;
816 const uint16_t *types;
819 struct ConnectNotifyMessage cnm;
822 #if DEBUG_CORE_CLIENT
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
824 "Client connecting to core service with `%s' message\n",
827 /* check that we don't have an entry already */
831 if (client == c->client_handle)
834 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
839 msize = ntohs (message->size);
840 if (msize < sizeof (struct InitMessage))
843 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
846 GNUNET_SERVER_notification_context_add (notifier, client);
847 im = (const struct InitMessage *) message;
848 types = (const uint16_t *) &im[1];
849 msize -= sizeof (struct InitMessage);
850 c = GNUNET_malloc (sizeof (struct Client) + msize);
851 c->client_handle = client;
854 c->tcnt = msize / sizeof (uint16_t);
855 c->types = (const uint16_t *) &c[1];
856 wtypes = (uint16_t *) &c[1];
857 for (i=0;i<c->tcnt;i++)
858 wtypes[i] = ntohs (types[i]);
859 c->options = ntohl (im->options);
860 #if DEBUG_CORE_CLIENT
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
862 "Client %p is interested in %u message types\n",
866 /* send init reply message */
867 irm.header.size = htons (sizeof (struct InitReplyMessage));
868 irm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY);
869 irm.reserved = htonl (0);
870 memcpy (&irm.publicKey,
872 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
873 #if DEBUG_CORE_CLIENT
874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
875 "Sending `%s' message to client.\n", "INIT_REPLY");
877 send_to_client (c, &irm.header, GNUNET_NO);
878 /* notify new client about existing neighbours */
879 cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
880 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
884 if (n->status == PEER_STATE_KEY_CONFIRMED)
886 #if DEBUG_CORE_CLIENT
887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
888 "Sending `%s' message to client.\n", "NOTIFY_CONNECT");
890 cnm.distance = htonl (n->last_distance);
891 cnm.latency = GNUNET_TIME_relative_hton (n->last_latency);
893 send_to_client (c, &cnm.header, GNUNET_NO);
897 GNUNET_SERVER_receive_done (client, GNUNET_OK);
902 * A client disconnected, clean up.
905 * @param client identification of the client
908 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
915 #if DEBUG_CORE_CLIENT
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917 "Client %p has disconnected from core service.\n",
924 if (client == pos->client_handle)
929 prev->next = pos->next;
936 /* client never sent INIT */
941 * Handle REQUEST_INFO request.
944 handle_client_request_info (void *cls,
945 struct GNUNET_SERVER_Client *client,
946 const struct GNUNET_MessageHeader *message)
948 const struct RequestInfoMessage *rcm;
950 struct ConfigurationInfoMessage cim;
953 unsigned long long old_preference;
954 struct GNUNET_SERVER_TransmitContext *tc;
956 #if DEBUG_CORE_CLIENT
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
958 "Core service receives `%s' request.\n", "REQUEST_INFO");
960 rcm = (const struct RequestInfoMessage *) message;
961 n = find_neighbour (&rcm->peer);
962 memset (&cim, 0, sizeof (cim));
965 want_reserv = ntohl (rcm->reserve_inbound);
966 if (n->bw_out_internal_limit.value__ != rcm->limit_outbound.value__)
968 n->bw_out_internal_limit = rcm->limit_outbound;
969 n->bw_out = GNUNET_BANDWIDTH_value_min (n->bw_out_internal_limit,
970 n->bw_out_external_limit);
971 GNUNET_BANDWIDTH_tracker_update_quota (&n->available_recv_window,
973 GNUNET_TRANSPORT_set_quota (transport,
977 GNUNET_TIME_UNIT_FOREVER_REL,
982 got_reserv = want_reserv;
984 else if (want_reserv > 0)
986 if (GNUNET_BANDWIDTH_tracker_get_delay (&n->available_recv_window,
987 want_reserv).value == 0)
988 got_reserv = want_reserv;
990 got_reserv = 0; /* all or nothing */
994 GNUNET_BANDWIDTH_tracker_consume (&n->available_recv_window,
996 old_preference = n->current_preference;
997 n->current_preference += GNUNET_ntohll(rcm->preference_change);
998 if (old_preference > n->current_preference)
1000 /* overflow; cap at maximum value */
1001 n->current_preference = (unsigned long long) -1;
1003 update_preference_sum (n->current_preference - old_preference);
1004 #if DEBUG_CORE_QUOTA
1005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1006 "Received reservation request for %d bytes for peer `%4s', reserved %d bytes\n",
1008 GNUNET_i2s (&rcm->peer),
1011 cim.reserved_amount = htonl (got_reserv);
1012 cim.bw_in = n->bw_in;
1013 cim.bw_out = n->bw_out;
1014 cim.preference = n->current_preference;
1016 cim.header.size = htons (sizeof (struct ConfigurationInfoMessage));
1017 cim.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO);
1018 cim.peer = rcm->peer;
1020 #if DEBUG_CORE_CLIENT
1021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1022 "Sending `%s' message to client.\n", "CONFIGURATION_INFO");
1024 tc = GNUNET_SERVER_transmit_context_create (client);
1025 GNUNET_SERVER_transmit_context_append_message (tc, &cim.header);
1026 GNUNET_SERVER_transmit_context_run (tc,
1027 GNUNET_TIME_UNIT_FOREVER_REL);
1032 * Free the given entry for the neighbour (it has
1033 * already been removed from the list at this point).
1035 * @param n neighbour to free
1038 free_neighbour (struct Neighbour *n)
1040 struct MessageEntry *m;
1042 if (n->pitr != NULL)
1044 GNUNET_PEERINFO_iterate_cancel (n->pitr);
1049 GNUNET_free (n->skm);
1052 while (NULL != (m = n->messages))
1054 n->messages = m->next;
1057 while (NULL != (m = n->encrypted_head))
1059 GNUNET_CONTAINER_DLL_remove (n->encrypted_head,
1066 GNUNET_TRANSPORT_notify_transmit_ready_cancel (n->th);
1069 if (n->retry_plaintext_task != GNUNET_SCHEDULER_NO_TASK)
1070 GNUNET_SCHEDULER_cancel (sched, n->retry_plaintext_task);
1071 if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
1072 GNUNET_SCHEDULER_cancel (sched, n->retry_set_key_task);
1073 if (n->quota_update_task != GNUNET_SCHEDULER_NO_TASK)
1074 GNUNET_SCHEDULER_cancel (sched, n->quota_update_task);
1075 if (n->dead_clean_task != GNUNET_SCHEDULER_NO_TASK)
1076 GNUNET_SCHEDULER_cancel (sched, n->dead_clean_task);
1077 GNUNET_free_non_null (n->public_key);
1078 GNUNET_free_non_null (n->pending_ping);
1079 GNUNET_free_non_null (n->pending_pong);
1085 * Consider freeing the given neighbour since we may not need
1086 * to keep it around anymore.
1088 * @param n neighbour to consider discarding
1091 consider_free_neighbour (struct Neighbour *n);
1095 * Task triggered when a neighbour entry might have gotten stale.
1097 * @param cls the 'struct Neighbour'
1098 * @param tc scheduler context (not used)
1101 consider_free_task (void *cls,
1102 const struct GNUNET_SCHEDULER_TaskContext *tc)
1104 struct Neighbour *n = cls;
1105 n->dead_clean_task = GNUNET_SCHEDULER_NO_TASK;
1106 consider_free_neighbour (n);
1111 * Consider freeing the given neighbour since we may not need
1112 * to keep it around anymore.
1114 * @param n neighbour to consider discarding
1117 consider_free_neighbour (struct Neighbour *n)
1119 struct Neighbour *pos;
1120 struct Neighbour *prev;
1121 struct GNUNET_TIME_Relative left;
1123 if ( (n->th != NULL) ||
1124 (n->pitr != NULL) ||
1125 (n->status == PEER_STATE_KEY_CONFIRMED) ||
1126 (GNUNET_YES == n->is_connected) )
1127 return; /* no chance */
1129 left = GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_add (n->last_activity,
1133 if (n->dead_clean_task != GNUNET_SCHEDULER_NO_TASK)
1134 GNUNET_SCHEDULER_cancel (sched, n->dead_clean_task);
1135 n->dead_clean_task = GNUNET_SCHEDULER_add_delayed (sched,
1137 &consider_free_task,
1141 /* actually free the neighbour... */
1150 neighbours = n->next;
1152 prev->next = n->next;
1153 GNUNET_assert (neighbour_count > 0);
1160 * Check if we have encrypted messages for the specified neighbour
1161 * pending, and if so, check with the transport about sending them
1164 * @param n neighbour to check.
1166 static void process_encrypted_neighbour_queue (struct Neighbour *n);
1170 * Function called when the transport service is ready to
1171 * receive an encrypted message for the respective peer
1173 * @param cls neighbour to use message from
1174 * @param size number of bytes we can transmit
1175 * @param buf where to copy the message
1176 * @return number of bytes transmitted
1179 notify_encrypted_transmit_ready (void *cls, size_t size, void *buf)
1181 struct Neighbour *n = cls;
1182 struct MessageEntry *m;
1187 GNUNET_assert (NULL != (m = n->encrypted_head));
1188 GNUNET_CONTAINER_DLL_remove (n->encrypted_head,
1195 GNUNET_assert (size >= m->size);
1196 memcpy (cbuf, &m[1], m->size);
1198 GNUNET_BANDWIDTH_tracker_consume (&n->available_send_window,
1201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1202 "Copied message of type %u and size %u into transport buffer for `%4s'\n",
1203 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
1204 ret, GNUNET_i2s (&n->peer));
1206 process_encrypted_neighbour_queue (n);
1211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1212 "Transmission of message of type %u and size %u failed\n",
1213 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
1218 consider_free_neighbour (n);
1224 * Check if we have plaintext messages for the specified neighbour
1225 * pending, and if so, consider batching and encrypting them (and
1226 * then trigger processing of the encrypted queue if needed).
1228 * @param n neighbour to check.
1230 static void process_plaintext_neighbour_queue (struct Neighbour *n);
1234 * Check if we have encrypted messages for the specified neighbour
1235 * pending, and if so, check with the transport about sending them
1238 * @param n neighbour to check.
1241 process_encrypted_neighbour_queue (struct Neighbour *n)
1243 struct MessageEntry *m;
1246 return; /* request already pending */
1247 m = n->encrypted_head;
1250 /* encrypted queue empty, try plaintext instead */
1251 process_plaintext_neighbour_queue (n);
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1256 "Asking transport for transmission of %u bytes to `%4s' in next %llu ms\n",
1258 GNUNET_i2s (&n->peer),
1259 GNUNET_TIME_absolute_get_remaining (m->deadline).
1263 GNUNET_TRANSPORT_notify_transmit_ready (transport, &n->peer,
1266 GNUNET_TIME_absolute_get_remaining
1268 ¬ify_encrypted_transmit_ready,
1272 /* message request too large or duplicate request */
1274 /* discard encrypted message */
1275 GNUNET_CONTAINER_DLL_remove (n->encrypted_head,
1279 process_encrypted_neighbour_queue (n);
1285 * Decrypt size bytes from in and write the result to out. Use the
1286 * key for inbound traffic of the given neighbour. This function does
1287 * NOT do any integrity-checks on the result.
1289 * @param n neighbour we are receiving from
1290 * @param iv initialization vector to use
1291 * @param in ciphertext
1292 * @param out plaintext
1293 * @param size size of in/out
1294 * @return GNUNET_OK on success
1297 do_decrypt (struct Neighbour *n,
1298 const GNUNET_HashCode * iv,
1299 const void *in, void *out, size_t size)
1301 if (size != (uint16_t) size)
1306 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
1307 (n->status != PEER_STATE_KEY_CONFIRMED))
1309 GNUNET_break_op (0);
1310 return GNUNET_SYSERR;
1313 GNUNET_CRYPTO_aes_decrypt (in,
1317 GNUNET_CRYPTO_AesInitializationVector *) iv,
1321 return GNUNET_SYSERR;
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1325 "Decrypted %u bytes from `%4s' using key %u\n",
1326 size, GNUNET_i2s (&n->peer), n->decrypt_key.crc32);
1333 * Encrypt size bytes from in and write the result to out. Use the
1334 * key for outbound traffic of the given neighbour.
1336 * @param n neighbour we are sending to
1337 * @param iv initialization vector to use
1338 * @param in ciphertext
1339 * @param out plaintext
1340 * @param size size of in/out
1341 * @return GNUNET_OK on success
1344 do_encrypt (struct Neighbour *n,
1345 const GNUNET_HashCode * iv,
1346 const void *in, void *out, size_t size)
1348 if (size != (uint16_t) size)
1353 GNUNET_assert (size ==
1354 GNUNET_CRYPTO_aes_encrypt (in,
1358 GNUNET_CRYPTO_AesInitializationVector
1361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1362 "Encrypted %u bytes for `%4s' using key %u\n", size,
1363 GNUNET_i2s (&n->peer), n->encrypt_key.crc32);
1370 * Select messages for transmission. This heuristic uses a combination
1371 * of earliest deadline first (EDF) scheduling (with bounded horizon)
1372 * and priority-based discard (in case no feasible schedule exist) and
1373 * speculative optimization (defer any kind of transmission until
1374 * we either create a batch of significant size, 25% of max, or until
1375 * we are close to a deadline). Furthermore, when scheduling the
1376 * heuristic also packs as many messages into the batch as possible,
1377 * starting with those with the earliest deadline. Yes, this is fun.
1379 * @param n neighbour to select messages from
1380 * @param size number of bytes to select for transmission
1381 * @param retry_time set to the time when we should try again
1382 * (only valid if this function returns zero)
1383 * @return number of bytes selected, or 0 if we decided to
1384 * defer scheduling overall; in that case, retry_time is set.
1387 select_messages (struct Neighbour *n,
1388 size_t size, struct GNUNET_TIME_Relative *retry_time)
1390 struct MessageEntry *pos;
1391 struct MessageEntry *min;
1392 struct MessageEntry *last;
1393 unsigned int min_prio;
1394 struct GNUNET_TIME_Absolute t;
1395 struct GNUNET_TIME_Absolute now;
1396 struct GNUNET_TIME_Relative delta;
1398 struct GNUNET_TIME_Relative slack; /* how long could we wait before missing deadlines? */
1401 unsigned int queue_size;
1402 int discard_low_prio;
1404 GNUNET_assert (NULL != n->messages);
1405 now = GNUNET_TIME_absolute_get ();
1406 /* last entry in linked list of messages processed */
1408 /* should we remove the entry with the lowest
1409 priority from consideration for scheduling at the
1420 discard_low_prio = GNUNET_YES;
1421 while (GNUNET_YES == discard_low_prio)
1425 discard_low_prio = GNUNET_NO;
1426 /* calculate number of bytes available for transmission at time "t" */
1427 avail = GNUNET_BANDWIDTH_tracker_get_available (&n->available_send_window);
1429 /* how many bytes have we (hypothetically) scheduled so far */
1431 /* maximum time we can wait before transmitting anything
1432 and still make all of our deadlines */
1433 slack = MAX_CORK_DELAY;
1435 /* note that we use "*2" here because we want to look
1436 a bit further into the future; much more makes no
1437 sense since new message might be scheduled in the
1439 while ((pos != NULL) && (off < size * 2))
1441 if (pos->do_transmit == GNUNET_YES)
1443 /* already removed from consideration */
1447 if (discard_low_prio == GNUNET_NO)
1449 delta = GNUNET_TIME_absolute_get_difference (t, pos->deadline);
1450 if (delta.value > 0)
1452 // FIXME: HUH? Check!
1454 avail += GNUNET_BANDWIDTH_value_get_available_until (n->bw_out,
1457 if (avail < pos->size)
1459 // FIXME: HUH? Check!
1460 discard_low_prio = GNUNET_YES; /* we could not schedule this one! */
1465 /* update slack, considering both its absolute deadline
1466 and relative deadlines caused by other messages
1467 with their respective load */
1468 slack = GNUNET_TIME_relative_min (slack,
1469 GNUNET_BANDWIDTH_value_get_delay_for (n->bw_out,
1471 if (pos->deadline.value <= now.value)
1474 slack = GNUNET_TIME_UNIT_ZERO;
1476 else if (GNUNET_YES == pos->got_slack)
1478 /* should be soon now! */
1479 slack = GNUNET_TIME_relative_min (slack,
1480 GNUNET_TIME_absolute_get_remaining (pos->slack_deadline));
1485 GNUNET_TIME_relative_min (slack,
1486 GNUNET_TIME_absolute_get_difference (now, pos->deadline));
1487 pos->got_slack = GNUNET_YES;
1488 pos->slack_deadline = GNUNET_TIME_absolute_min (pos->deadline,
1489 GNUNET_TIME_relative_to_absolute (MAX_CORK_DELAY));
1494 t = GNUNET_TIME_absolute_max (pos->deadline, t); // HUH? Check!
1495 if (pos->priority <= min_prio)
1497 /* update min for discard */
1498 min_prio = pos->priority;
1503 if (discard_low_prio)
1505 GNUNET_assert (min != NULL);
1506 /* remove lowest-priority entry from consideration */
1507 min->do_transmit = GNUNET_YES; /* means: discard (for now) */
1511 /* guard against sending "tiny" messages with large headers without
1513 if ( (slack.value > 0) &&
1515 (queue_size <= MAX_PEER_QUEUE_SIZE - 2) )
1517 /* less than 25% of message would be filled with deadlines still
1518 being met if we delay by one second or more; so just wait for
1519 more data; but do not wait longer than 1s (since we don't want
1520 to delay messages for a really long time either). */
1521 *retry_time = MAX_CORK_DELAY;
1522 /* reset do_transmit values for next time */
1525 pos->do_transmit = GNUNET_NO;
1529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1530 "Deferring transmission for %llums due to underfull message buffer size (%u/%u)\n",
1531 (unsigned long long) slack.value,
1533 (unsigned int) size);
1537 /* select marked messages (up to size) for transmission */
1542 if ((pos->size <= size) && (pos->do_transmit == GNUNET_NO))
1544 pos->do_transmit = GNUNET_YES; /* mark for transmission */
1549 pos->do_transmit = GNUNET_NO; /* mark for not transmitting! */
1553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1554 "Selected %u/%u bytes of %u/%u plaintext messages for transmission to `%4s'.\n",
1556 queue_size, MAX_PEER_QUEUE_SIZE,
1557 GNUNET_i2s (&n->peer));
1564 * Batch multiple messages into a larger buffer.
1566 * @param n neighbour to take messages from
1567 * @param buf target buffer
1568 * @param size size of buf
1569 * @param deadline set to transmission deadline for the result
1570 * @param retry_time set to the time when we should try again
1571 * (only valid if this function returns zero)
1572 * @param priority set to the priority of the batch
1573 * @return number of bytes written to buf (can be zero)
1576 batch_message (struct Neighbour *n,
1579 struct GNUNET_TIME_Absolute *deadline,
1580 struct GNUNET_TIME_Relative *retry_time,
1581 unsigned int *priority)
1583 char ntmb[GNUNET_SERVER_MAX_MESSAGE_SIZE];
1584 struct NotifyTrafficMessage *ntm = (struct NotifyTrafficMessage*) ntmb;
1585 struct MessageEntry *pos;
1586 struct MessageEntry *prev;
1587 struct MessageEntry *next;
1592 *deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
1593 *retry_time = GNUNET_TIME_UNIT_FOREVER_REL;
1594 if (0 == select_messages (n, size, retry_time))
1596 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1597 "No messages selected, will try again in %llu ms\n",
1601 ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND);
1602 ntm->distance = htonl (n->last_distance);
1603 ntm->latency = GNUNET_TIME_relative_hton (n->last_latency);
1604 ntm->peer = n->peer;
1608 while ((pos != NULL) && (size >= sizeof (struct GNUNET_MessageHeader)))
1611 if (GNUNET_YES == pos->do_transmit)
1613 GNUNET_assert (pos->size <= size);
1614 /* do notifications */
1615 /* FIXME: track if we have *any* client that wants
1616 full notifications and only do this if that is
1618 if (pos->size < GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct NotifyTrafficMessage))
1620 memcpy (&ntm[1], &pos[1], pos->size);
1621 ntm->header.size = htons (sizeof (struct NotifyTrafficMessage) +
1622 sizeof (struct GNUNET_MessageHeader));
1623 send_to_all_clients (&ntm->header,
1625 GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND);
1629 /* message too large for 'full' notifications, we do at
1630 least the 'hdr' type */
1633 sizeof (struct GNUNET_MessageHeader));
1635 ntm->header.size = htons (sizeof (struct NotifyTrafficMessage) +
1637 send_to_all_clients (&ntm->header,
1639 GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND);
1642 "Encrypting message of type %u\n",
1643 ntohs(((struct GNUNET_MessageHeader*)&pos[1])->type));
1645 /* copy for encrypted transmission */
1646 memcpy (&buf[ret], &pos[1], pos->size);
1649 *priority += pos->priority;
1651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1652 "Adding plaintext message of size %u with deadline %llu ms to batch\n",
1654 GNUNET_TIME_absolute_get_remaining (pos->deadline).value);
1656 deadline->value = GNUNET_MIN (deadline->value, pos->deadline.value);
1670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1671 "Deadline for message batch is %llu ms\n",
1672 GNUNET_TIME_absolute_get_remaining (*deadline).value);
1679 * Remove messages with deadlines that have long expired from
1682 * @param n neighbour to inspect
1685 discard_expired_messages (struct Neighbour *n)
1687 struct MessageEntry *prev;
1688 struct MessageEntry *next;
1689 struct MessageEntry *pos;
1690 struct GNUNET_TIME_Absolute now;
1691 struct GNUNET_TIME_Relative delta;
1693 now = GNUNET_TIME_absolute_get ();
1699 delta = GNUNET_TIME_absolute_get_difference (pos->deadline, now);
1700 if (delta.value > PAST_EXPIRATION_DISCARD_TIME.value)
1703 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1704 "Message is %llu ms past due, discarding.\n",
1721 * Signature of the main function of a task.
1723 * @param cls closure
1724 * @param tc context information (why was this task triggered now)
1727 retry_plaintext_processing (void *cls,
1728 const struct GNUNET_SCHEDULER_TaskContext *tc)
1730 struct Neighbour *n = cls;
1732 n->retry_plaintext_task = GNUNET_SCHEDULER_NO_TASK;
1733 process_plaintext_neighbour_queue (n);
1738 * Send our key (and encrypted PING) to the other peer.
1740 * @param n the other peer
1742 static void send_key (struct Neighbour *n);
1745 * Task that will retry "send_key" if our previous attempt failed
1749 set_key_retry_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1751 struct Neighbour *n = cls;
1753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1754 "Retrying key transmission to `%4s'\n",
1755 GNUNET_i2s (&n->peer));
1756 n->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
1757 n->set_key_retry_frequency =
1758 GNUNET_TIME_relative_multiply (n->set_key_retry_frequency, 2);
1764 * Check if we have plaintext messages for the specified neighbour
1765 * pending, and if so, consider batching and encrypting them (and
1766 * then trigger processing of the encrypted queue if needed).
1768 * @param n neighbour to check.
1771 process_plaintext_neighbour_queue (struct Neighbour *n)
1773 char pbuf[MAX_ENCRYPTED_MESSAGE_SIZE]; /* plaintext */
1776 struct EncryptedMessage *em; /* encrypted message */
1777 struct EncryptedMessage *ph; /* plaintext header */
1778 struct MessageEntry *me;
1779 unsigned int priority;
1780 struct GNUNET_TIME_Absolute deadline;
1781 struct GNUNET_TIME_Relative retry_time;
1784 if (n->retry_plaintext_task != GNUNET_SCHEDULER_NO_TASK)
1786 GNUNET_SCHEDULER_cancel (sched, n->retry_plaintext_task);
1787 n->retry_plaintext_task = GNUNET_SCHEDULER_NO_TASK;
1791 case PEER_STATE_DOWN:
1794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1795 "Not yet connected to `%4s', deferring processing of plaintext messages.\n",
1796 GNUNET_i2s(&n->peer));
1799 case PEER_STATE_KEY_SENT:
1800 if (n->retry_set_key_task == GNUNET_SCHEDULER_NO_TASK)
1801 n->retry_set_key_task
1802 = GNUNET_SCHEDULER_add_delayed (sched,
1803 n->set_key_retry_frequency,
1804 &set_key_retry_task, n);
1806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1807 "Not yet connected to `%4s', deferring processing of plaintext messages.\n",
1808 GNUNET_i2s(&n->peer));
1811 case PEER_STATE_KEY_RECEIVED:
1812 if (n->retry_set_key_task == GNUNET_SCHEDULER_NO_TASK)
1813 n->retry_set_key_task
1814 = GNUNET_SCHEDULER_add_delayed (sched,
1815 n->set_key_retry_frequency,
1816 &set_key_retry_task, n);
1818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1819 "Not yet connected to `%4s', deferring processing of plaintext messages.\n",
1820 GNUNET_i2s(&n->peer));
1823 case PEER_STATE_KEY_CONFIRMED:
1824 /* ready to continue */
1827 discard_expired_messages (n);
1828 if (n->messages == NULL)
1831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1832 "Plaintext message queue for `%4s' is empty.\n",
1833 GNUNET_i2s(&n->peer));
1835 return; /* no pending messages */
1837 if (n->encrypted_head != NULL)
1840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1841 "Encrypted message queue for `%4s' is still full, delaying plaintext processing.\n",
1842 GNUNET_i2s(&n->peer));
1844 return; /* wait for messages already encrypted to be
1847 ph = (struct EncryptedMessage *) pbuf;
1848 deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
1850 used = sizeof (struct EncryptedMessage);
1851 used += batch_message (n,
1853 MAX_ENCRYPTED_MESSAGE_SIZE - used,
1854 &deadline, &retry_time, &priority);
1855 if (used == sizeof (struct EncryptedMessage))
1858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1859 "No messages selected for transmission to `%4s' at this time, will try again later.\n",
1860 GNUNET_i2s(&n->peer));
1862 /* no messages selected for sending, try again later... */
1863 n->retry_plaintext_task =
1864 GNUNET_SCHEDULER_add_delayed (sched,
1866 &retry_plaintext_processing, n);
1869 #if DEBUG_CORE_QUOTA
1870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1871 "Sending %u b/s as new limit to peer `%4s'\n",
1872 (unsigned int) ntohl (n->bw_in.value__),
1873 GNUNET_i2s (&n->peer));
1875 ph->iv_seed = htonl (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, -1));
1876 ph->sequence_number = htonl (++n->last_sequence_number_sent);
1877 ph->inbound_bw_limit = n->bw_in;
1878 ph->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1880 /* setup encryption message header */
1881 me = GNUNET_malloc (sizeof (struct MessageEntry) + used);
1882 me->deadline = deadline;
1883 me->priority = priority;
1885 em = (struct EncryptedMessage *) &me[1];
1886 em->header.size = htons (used);
1887 em->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE);
1888 em->iv_seed = ph->iv_seed;
1889 esize = used - ENCRYPTED_HEADER_SIZE;
1890 GNUNET_CRYPTO_hash (&ph->sequence_number,
1891 esize - sizeof (GNUNET_HashCode),
1892 &ph->plaintext_hash);
1893 GNUNET_CRYPTO_hash (&ph->iv_seed, sizeof (uint32_t), &iv);
1896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1897 "Encrypting %u bytes of plaintext messages for `%4s' for transmission in %llums.\n",
1899 GNUNET_i2s(&n->peer),
1900 (unsigned long long) GNUNET_TIME_absolute_get_remaining (deadline).value);
1902 GNUNET_assert (GNUNET_OK ==
1905 &ph->plaintext_hash,
1906 &em->plaintext_hash, esize));
1907 /* append to transmission list */
1908 GNUNET_CONTAINER_DLL_insert_after (n->encrypted_head,
1912 process_encrypted_neighbour_queue (n);
1917 * Function that recalculates the bandwidth quota for the
1918 * given neighbour and transmits it to the transport service.
1920 * @param cls neighbour for the quota update
1924 neighbour_quota_update (void *cls,
1925 const struct GNUNET_SCHEDULER_TaskContext *tc);
1929 * Schedule the task that will recalculate the bandwidth
1930 * quota for this peer (and possibly force a disconnect of
1931 * idle peers by calculating a bandwidth of zero).
1934 schedule_quota_update (struct Neighbour *n)
1936 GNUNET_assert (n->quota_update_task ==
1937 GNUNET_SCHEDULER_NO_TASK);
1938 n->quota_update_task
1939 = GNUNET_SCHEDULER_add_delayed (sched,
1940 QUOTA_UPDATE_FREQUENCY,
1941 &neighbour_quota_update,
1947 * Initialize a new 'struct Neighbour'.
1949 * @param pid ID of the new neighbour
1950 * @return handle for the new neighbour
1952 static struct Neighbour *
1953 create_neighbour (const struct GNUNET_PeerIdentity *pid)
1955 struct Neighbour *n;
1956 struct GNUNET_TIME_Absolute now;
1958 n = GNUNET_malloc (sizeof (struct Neighbour));
1959 n->next = neighbours;
1963 GNUNET_CRYPTO_aes_create_session_key (&n->encrypt_key);
1964 now = GNUNET_TIME_absolute_get ();
1965 n->encrypt_key_created = now;
1966 n->last_activity = now;
1967 n->set_key_retry_frequency = INITIAL_SET_KEY_RETRY_FREQUENCY;
1968 n->bw_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
1969 n->bw_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
1970 n->bw_out_internal_limit = GNUNET_BANDWIDTH_value_init ((uint32_t) - 1);
1971 n->bw_out_external_limit = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
1972 n->ping_challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1974 neighbour_quota_update (n, NULL);
1980 * Handle CORE_SEND request.
1983 * @param client the client issuing the request
1984 * @param message the "struct SendMessage"
1987 handle_client_send (void *cls,
1988 struct GNUNET_SERVER_Client *client,
1989 const struct GNUNET_MessageHeader *message)
1991 const struct SendMessage *sm;
1992 struct Neighbour *n;
1993 struct MessageEntry *prev;
1994 struct MessageEntry *pos;
1995 struct MessageEntry *e;
1996 struct MessageEntry *min_prio_entry;
1997 struct MessageEntry *min_prio_prev;
1998 unsigned int min_prio;
1999 unsigned int queue_size;
2002 msize = ntohs (message->size);
2004 sizeof (struct SendMessage) + sizeof (struct GNUNET_MessageHeader))
2008 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2011 sm = (const struct SendMessage *) message;
2012 msize -= sizeof (struct SendMessage);
2013 n = find_neighbour (&sm->peer);
2015 n = create_neighbour (&sm->peer);
2017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2018 "Core received `%s' request, queueing %u bytes of plaintext data for transmission to `%4s'.\n",
2021 GNUNET_i2s (&sm->peer));
2023 /* bound queue size */
2024 discard_expired_messages (n);
2025 min_prio = (unsigned int) -1;
2026 min_prio_entry = NULL;
2027 min_prio_prev = NULL;
2033 if (pos->priority < min_prio)
2035 min_prio_entry = pos;
2036 min_prio_prev = prev;
2037 min_prio = pos->priority;
2043 if (queue_size >= MAX_PEER_QUEUE_SIZE)
2046 if (ntohl(sm->priority) <= min_prio)
2048 /* discard new entry */
2050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2051 "Queue full (%u/%u), discarding new request (%u bytes of type %u)\n",
2053 MAX_PEER_QUEUE_SIZE,
2055 ntohs (message->type));
2058 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2061 /* discard "min_prio_entry" */
2063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2064 "Queue full, discarding existing older request\n");
2066 if (min_prio_prev == NULL)
2067 n->messages = min_prio_entry->next;
2069 min_prio_prev->next = min_prio_entry->next;
2070 GNUNET_free (min_prio_entry);
2074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2075 "Adding transmission request for `%4s' of size %u to queue\n",
2076 GNUNET_i2s (&sm->peer),
2079 e = GNUNET_malloc (sizeof (struct MessageEntry) + msize);
2080 e->deadline = GNUNET_TIME_absolute_ntoh (sm->deadline);
2081 e->priority = ntohl (sm->priority);
2083 memcpy (&e[1], &sm[1], msize);
2085 /* insert, keep list sorted by deadline */
2088 while ((pos != NULL) && (pos->deadline.value < e->deadline.value))
2099 /* consider scheduling now */
2100 process_plaintext_neighbour_queue (n);
2102 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2107 * Function called when the transport service is ready to
2108 * receive a message. Only resets 'n->th' to NULL.
2110 * @param cls neighbour to use message from
2111 * @param size number of bytes we can transmit
2112 * @param buf where to copy the message
2113 * @return number of bytes transmitted
2116 notify_transport_connect_done (void *cls, size_t size, void *buf)
2118 struct Neighbour *n = cls;
2123 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2124 _("Failed to connect to `%4s': transport failed to connect\n"),
2125 GNUNET_i2s (&n->peer));
2134 * Handle CORE_REQUEST_CONNECT request.
2137 * @param client the client issuing the request
2138 * @param message the "struct ConnectMessage"
2141 handle_client_request_connect (void *cls,
2142 struct GNUNET_SERVER_Client *client,
2143 const struct GNUNET_MessageHeader *message)
2145 const struct ConnectMessage *cm = (const struct ConnectMessage*) message;
2146 struct Neighbour *n;
2147 struct GNUNET_TIME_Relative timeout;
2149 if (0 == memcmp (&cm->peer, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
2152 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2155 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2156 n = find_neighbour (&cm->peer);
2158 n = create_neighbour (&cm->peer);
2159 if ( (n->is_connected) ||
2161 return; /* already connected, or at least trying */
2163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2164 "Core received `%s' request for `%4s', will try to establish connection\n",
2166 GNUNET_i2s (&cm->peer));
2168 timeout = GNUNET_TIME_relative_ntoh (cm->timeout);
2169 /* ask transport to connect to the peer */
2170 n->th = GNUNET_TRANSPORT_notify_transmit_ready (transport,
2172 sizeof (struct GNUNET_MessageHeader), 0,
2174 ¬ify_transport_connect_done,
2176 GNUNET_break (NULL != n->th);
2181 * List of handlers for the messages understood by this
2184 static struct GNUNET_SERVER_MessageHandler handlers[] = {
2185 {&handle_client_init, NULL,
2186 GNUNET_MESSAGE_TYPE_CORE_INIT, 0},
2187 {&handle_client_request_info, NULL,
2188 GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO,
2189 sizeof (struct RequestInfoMessage)},
2190 {&handle_client_send, NULL,
2191 GNUNET_MESSAGE_TYPE_CORE_SEND, 0},
2192 {&handle_client_request_connect, NULL,
2193 GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT,
2194 sizeof (struct ConnectMessage)},
2200 * PEERINFO is giving us a HELLO for a peer. Add the public key to
2201 * the neighbour's struct and retry send_key. Or, if we did not get a
2202 * HELLO, just do nothing.
2204 * @param cls the 'struct Neighbour' to retry sending the key for
2205 * @param peer the peer for which this is the HELLO
2206 * @param hello HELLO message of that peer
2207 * @param trust amount of trust we currently have in that peer
2210 process_hello_retry_send_key (void *cls,
2211 const struct GNUNET_PeerIdentity *peer,
2212 const struct GNUNET_HELLO_Message *hello,
2215 struct Neighbour *n = cls;
2220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2221 "Entered `process_hello_retry_send_key' and `peer' is NULL!\n");
2224 if (n->public_key != NULL)
2230 if (GNUNET_SCHEDULER_NO_TASK == n->retry_set_key_task)
2231 n->retry_set_key_task
2232 = GNUNET_SCHEDULER_add_delayed (sched,
2233 n->set_key_retry_frequency,
2234 &set_key_retry_task, n);
2240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2241 "Entered `process_hello_retry_send_key' for peer `%4s'\n",
2244 if (n->public_key != NULL)
2247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2248 "already have public key for peer %s!! (so why are we here?)\n",
2255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2256 "Received new `%s' message for `%4s', initiating key exchange.\n",
2261 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2262 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, n->public_key))
2264 GNUNET_free (n->public_key);
2265 n->public_key = NULL;
2267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2268 "GNUNET_HELLO_get_key returned awfully\n");
2276 * Send our key (and encrypted PING) to the other peer.
2278 * @param n the other peer
2281 send_key (struct Neighbour *n)
2283 struct SetKeyMessage *sm;
2284 struct MessageEntry *me;
2285 struct PingMessage pp;
2286 struct PingMessage *pm;
2288 if ( (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK) ||
2292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2293 "Key exchange in progress with `%4s'.\n",
2294 GNUNET_i2s (&n->peer));
2296 return; /* already in progress */
2300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2301 "Asked to perform key exchange with `%4s'.\n",
2302 GNUNET_i2s (&n->peer));
2304 if (n->public_key == NULL)
2306 /* lookup n's public key, then try again */
2308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2309 "Lacking public key for `%4s', trying to obtain one (send_key).\n",
2310 GNUNET_i2s (&n->peer));
2312 GNUNET_assert (n->pitr == NULL);
2313 n->pitr = GNUNET_PEERINFO_iterate (cfg,
2317 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 20),
2318 &process_hello_retry_send_key, n);
2321 /* first, set key message */
2322 me = GNUNET_malloc (sizeof (struct MessageEntry) +
2323 sizeof (struct SetKeyMessage));
2324 me->deadline = GNUNET_TIME_relative_to_absolute (MAX_SET_KEY_DELAY);
2325 me->priority = SET_KEY_PRIORITY;
2326 me->size = sizeof (struct SetKeyMessage);
2327 GNUNET_CONTAINER_DLL_insert_after (n->encrypted_head,
2331 sm = (struct SetKeyMessage *) &me[1];
2332 sm->header.size = htons (sizeof (struct SetKeyMessage));
2333 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SET_KEY);
2334 sm->sender_status = htonl ((int32_t) ((n->status == PEER_STATE_DOWN) ?
2335 PEER_STATE_KEY_SENT : n->status));
2337 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2338 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
2339 sizeof (struct GNUNET_CRYPTO_RsaEncryptedData) +
2340 sizeof (struct GNUNET_PeerIdentity));
2341 sm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_SET_KEY);
2342 sm->creation_time = GNUNET_TIME_absolute_hton (n->encrypt_key_created);
2343 sm->target = n->peer;
2344 GNUNET_assert (GNUNET_OK ==
2345 GNUNET_CRYPTO_rsa_encrypt (&n->encrypt_key,
2347 GNUNET_CRYPTO_AesSessionKey),
2349 &sm->encrypted_key));
2350 GNUNET_assert (GNUNET_OK ==
2351 GNUNET_CRYPTO_rsa_sign (my_private_key, &sm->purpose,
2354 /* second, encrypted PING message */
2355 me = GNUNET_malloc (sizeof (struct MessageEntry) +
2356 sizeof (struct PingMessage));
2357 me->deadline = GNUNET_TIME_relative_to_absolute (MAX_PING_DELAY);
2358 me->priority = PING_PRIORITY;
2359 me->size = sizeof (struct PingMessage);
2360 n->encrypted_tail->next = me;
2361 n->encrypted_tail = me;
2362 pm = (struct PingMessage *) &me[1];
2363 pm->header.size = htons (sizeof (struct PingMessage));
2364 pm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PING);
2365 pp.challenge = htonl (n->ping_challenge);
2366 pp.target = n->peer;
2368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2369 "Encrypting `%s' and `%s' messages for `%4s'.\n",
2370 "SET_KEY", "PING", GNUNET_i2s (&n->peer));
2371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2372 "Sending `%s' to `%4s' with challenge %u encrypted using key %u\n",
2374 GNUNET_i2s (&n->peer), n->ping_challenge, n->encrypt_key.crc32);
2377 &n->peer.hashPubKey,
2380 sizeof (struct PingMessage) -
2381 sizeof (struct GNUNET_MessageHeader));
2385 case PEER_STATE_DOWN:
2386 n->status = PEER_STATE_KEY_SENT;
2388 case PEER_STATE_KEY_SENT:
2390 case PEER_STATE_KEY_RECEIVED:
2392 case PEER_STATE_KEY_CONFIRMED:
2399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2400 "Have %llu ms left for `%s' transmission.\n",
2401 (unsigned long long) GNUNET_TIME_absolute_get_remaining (me->deadline).value,
2404 /* trigger queue processing */
2405 process_encrypted_neighbour_queue (n);
2406 if ( (n->status != PEER_STATE_KEY_CONFIRMED) &&
2407 (GNUNET_SCHEDULER_NO_TASK == n->retry_set_key_task) )
2408 n->retry_set_key_task
2409 = GNUNET_SCHEDULER_add_delayed (sched,
2410 n->set_key_retry_frequency,
2411 &set_key_retry_task, n);
2416 * We received a SET_KEY message. Validate and update
2417 * our key material and status.
2419 * @param n the neighbour from which we received message m
2420 * @param m the set key message we received
2423 handle_set_key (struct Neighbour *n, const struct SetKeyMessage *m);
2427 * PEERINFO is giving us a HELLO for a peer. Add the public key to
2428 * the neighbour's struct and retry handling the set_key message. Or,
2429 * if we did not get a HELLO, just free the set key message.
2431 * @param cls pointer to the set key message
2432 * @param peer the peer for which this is the HELLO
2433 * @param hello HELLO message of that peer
2434 * @param trust amount of trust we currently have in that peer
2437 process_hello_retry_handle_set_key (void *cls,
2438 const struct GNUNET_PeerIdentity *peer,
2439 const struct GNUNET_HELLO_Message *hello,
2442 struct Neighbour *n = cls;
2443 struct SetKeyMessage *sm = n->skm;
2452 if (n->public_key != NULL)
2453 return; /* multiple HELLOs match!? */
2455 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2456 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, n->public_key))
2458 GNUNET_break_op (0);
2459 GNUNET_free (n->public_key);
2460 n->public_key = NULL;
2464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2465 "Received `%s' for `%4s', continuing processing of `%s' message.\n",
2466 "HELLO", GNUNET_i2s (peer), "SET_KEY");
2468 handle_set_key (n, sm);
2473 * We received a PING message. Validate and transmit
2476 * @param n sender of the PING
2477 * @param m the encrypted PING message itself
2480 handle_ping (struct Neighbour *n, const struct PingMessage *m)
2482 struct PingMessage t;
2483 struct PongMessage tx;
2484 struct PongMessage *tp;
2485 struct MessageEntry *me;
2488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2489 "Core service receives `%s' request from `%4s'.\n",
2490 "PING", GNUNET_i2s (&n->peer));
2494 &my_identity.hashPubKey,
2497 sizeof (struct PingMessage) -
2498 sizeof (struct GNUNET_MessageHeader)))
2501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2502 "Decrypted `%s' to `%4s' with challenge %u decrypted using key %u\n",
2504 GNUNET_i2s (&t.target),
2505 ntohl (t.challenge), n->decrypt_key.crc32);
2506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2507 "Target of `%s' request is `%4s'.\n",
2508 "PING", GNUNET_i2s (&t.target));
2510 if (0 != memcmp (&t.target,
2511 &my_identity, sizeof (struct GNUNET_PeerIdentity)))
2513 GNUNET_break_op (0);
2516 me = GNUNET_malloc (sizeof (struct MessageEntry) +
2517 sizeof (struct PongMessage));
2518 GNUNET_CONTAINER_DLL_insert_after (n->encrypted_head,
2522 me->deadline = GNUNET_TIME_relative_to_absolute (MAX_PONG_DELAY);
2523 me->priority = PONG_PRIORITY;
2524 me->size = sizeof (struct PongMessage);
2525 tx.reserved = htonl (0);
2526 tx.inbound_bw_limit = n->bw_in;
2527 tx.challenge = t.challenge;
2528 tx.target = t.target;
2529 tp = (struct PongMessage *) &me[1];
2530 tp->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PONG);
2531 tp->header.size = htons (sizeof (struct PongMessage));
2533 &my_identity.hashPubKey,
2536 sizeof (struct PongMessage) -
2537 sizeof (struct GNUNET_MessageHeader));
2539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2540 "Encrypting `%s' with challenge %u using key %u\n", "PONG",
2541 ntohl (t.challenge), n->encrypt_key.crc32);
2543 /* trigger queue processing */
2544 process_encrypted_neighbour_queue (n);
2549 * We received a PONG message. Validate and update our status.
2551 * @param n sender of the PONG
2552 * @param m the encrypted PONG message itself
2555 handle_pong (struct Neighbour *n,
2556 const struct PongMessage *m)
2558 struct PongMessage t;
2559 struct ConnectNotifyMessage cnm;
2562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2563 "Core service receives `%s' request from `%4s'.\n",
2564 "PONG", GNUNET_i2s (&n->peer));
2568 &n->peer.hashPubKey,
2571 sizeof (struct PongMessage) -
2572 sizeof (struct GNUNET_MessageHeader)))
2574 if (0 != ntohl (t.reserved))
2576 GNUNET_break_op (0);
2580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2581 "Decrypted `%s' from `%4s' with challenge %u using key %u\n",
2583 GNUNET_i2s (&t.target),
2584 ntohl (t.challenge), n->decrypt_key.crc32);
2586 if ((0 != memcmp (&t.target,
2588 sizeof (struct GNUNET_PeerIdentity))) ||
2589 (n->ping_challenge != ntohl (t.challenge)))
2591 /* PONG malformed */
2593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2594 "Received malformed `%s' wanted sender `%4s' with challenge %u\n",
2595 "PONG", GNUNET_i2s (&n->peer), n->ping_challenge);
2596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2597 "Received malformed `%s' received from `%4s' with challenge %u\n",
2598 "PONG", GNUNET_i2s (&t.target), ntohl (t.challenge));
2600 GNUNET_break_op (0);
2605 case PEER_STATE_DOWN:
2606 GNUNET_break (0); /* should be impossible */
2608 case PEER_STATE_KEY_SENT:
2609 GNUNET_break (0); /* should be impossible, how did we decrypt? */
2611 case PEER_STATE_KEY_RECEIVED:
2612 n->status = PEER_STATE_KEY_CONFIRMED;
2613 if (n->bw_out_external_limit.value__ != t.inbound_bw_limit.value__)
2615 n->bw_out_external_limit = t.inbound_bw_limit;
2616 n->bw_out = GNUNET_BANDWIDTH_value_min (n->bw_out_external_limit,
2617 n->bw_out_internal_limit);
2618 GNUNET_BANDWIDTH_tracker_update_quota (&n->available_send_window,
2620 GNUNET_TRANSPORT_set_quota (transport,
2624 GNUNET_TIME_UNIT_FOREVER_REL,
2628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2629 "Confirmed key via `%s' message for peer `%4s'\n",
2630 "PONG", GNUNET_i2s (&n->peer));
2632 if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
2634 GNUNET_SCHEDULER_cancel (sched, n->retry_set_key_task);
2635 n->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
2637 cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
2638 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
2639 cnm.distance = htonl (n->last_distance);
2640 cnm.latency = GNUNET_TIME_relative_hton (n->last_latency);
2642 send_to_all_clients (&cnm.header, GNUNET_YES, GNUNET_CORE_OPTION_SEND_CONNECT);
2643 process_encrypted_neighbour_queue (n);
2645 case PEER_STATE_KEY_CONFIRMED:
2646 /* duplicate PONG? */
2656 * We received a SET_KEY message. Validate and update
2657 * our key material and status.
2659 * @param n the neighbour from which we received message m
2660 * @param m the set key message we received
2663 handle_set_key (struct Neighbour *n, const struct SetKeyMessage *m)
2665 struct SetKeyMessage *m_cpy;
2666 struct GNUNET_TIME_Absolute t;
2667 struct GNUNET_CRYPTO_AesSessionKey k;
2668 struct PingMessage *ping;
2669 struct PongMessage *pong;
2670 enum PeerStateMachine sender_status;
2673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2674 "Core service receives `%s' request from `%4s'.\n",
2675 "SET_KEY", GNUNET_i2s (&n->peer));
2677 if (n->public_key == NULL)
2679 if (n->pitr != NULL)
2682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2683 "Ignoring `%s' message due to lack of public key for peer (still trying to obtain one).\n",
2689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2690 "Lacking public key for peer, trying to obtain one (handle_set_key).\n");
2692 m_cpy = GNUNET_malloc (sizeof (struct SetKeyMessage));
2693 memcpy (m_cpy, m, sizeof (struct SetKeyMessage));
2694 /* lookup n's public key, then try again */
2695 GNUNET_assert (n->skm == NULL);
2697 n->pitr = GNUNET_PEERINFO_iterate (cfg,
2701 GNUNET_TIME_UNIT_MINUTES,
2702 &process_hello_retry_handle_set_key, n);
2705 if (0 != memcmp (&m->target,
2707 sizeof (struct GNUNET_PeerIdentity)))
2709 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2710 _("Received `%s' message that was not for me. Ignoring.\n"),
2714 if ((ntohl (m->purpose.size) !=
2715 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2716 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
2717 sizeof (struct GNUNET_CRYPTO_RsaEncryptedData) +
2718 sizeof (struct GNUNET_PeerIdentity)) ||
2720 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_SET_KEY,
2721 &m->purpose, &m->signature, n->public_key)))
2723 /* invalid signature */
2724 GNUNET_break_op (0);
2727 t = GNUNET_TIME_absolute_ntoh (m->creation_time);
2728 if (((n->status == PEER_STATE_KEY_RECEIVED) ||
2729 (n->status == PEER_STATE_KEY_CONFIRMED)) &&
2730 (t.value < n->decrypt_key_created.value))
2732 /* this could rarely happen due to massive re-ordering of
2733 messages on the network level, but is most likely either
2734 a bug or some adversary messing with us. Report. */
2735 GNUNET_break_op (0);
2739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Decrypting key material.\n");
2741 if ((GNUNET_CRYPTO_rsa_decrypt (my_private_key,
2744 sizeof (struct GNUNET_CRYPTO_AesSessionKey))
2745 != sizeof (struct GNUNET_CRYPTO_AesSessionKey)) ||
2746 (GNUNET_OK != GNUNET_CRYPTO_aes_check_session_key (&k)))
2748 /* failed to decrypt !? */
2749 GNUNET_break_op (0);
2754 if (n->decrypt_key_created.value != t.value)
2756 /* fresh key, reset sequence numbers */
2757 n->last_sequence_number_received = 0;
2758 n->last_packets_bitmap = 0;
2759 n->decrypt_key_created = t;
2761 sender_status = (enum PeerStateMachine) ntohl (m->sender_status);
2764 case PEER_STATE_DOWN:
2765 n->status = PEER_STATE_KEY_RECEIVED;
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2768 "Responding to `%s' with my own key.\n", "SET_KEY");
2772 case PEER_STATE_KEY_SENT:
2773 case PEER_STATE_KEY_RECEIVED:
2774 n->status = PEER_STATE_KEY_RECEIVED;
2775 if ((sender_status != PEER_STATE_KEY_RECEIVED) &&
2776 (sender_status != PEER_STATE_KEY_CONFIRMED))
2779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2780 "Responding to `%s' with my own key (other peer has status %u).\n",
2781 "SET_KEY", sender_status);
2786 case PEER_STATE_KEY_CONFIRMED:
2787 if ((sender_status != PEER_STATE_KEY_RECEIVED) &&
2788 (sender_status != PEER_STATE_KEY_CONFIRMED))
2791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2792 "Responding to `%s' with my own key (other peer has status %u), I was already fully up.\n",
2793 "SET_KEY", sender_status);
2802 if (n->pending_ping != NULL)
2804 ping = n->pending_ping;
2805 n->pending_ping = NULL;
2806 handle_ping (n, ping);
2809 if (n->pending_pong != NULL)
2811 pong = n->pending_pong;
2812 n->pending_pong = NULL;
2813 handle_pong (n, pong);
2820 * Send a P2P message to a client.
2822 * @param sender who sent us the message?
2823 * @param client who should we give the message to?
2824 * @param m contains the message to transmit
2825 * @param msize number of bytes in buf to transmit
2828 send_p2p_message_to_client (struct Neighbour *sender,
2829 struct Client *client,
2830 const void *m, size_t msize)
2832 char buf[msize + sizeof (struct NotifyTrafficMessage)];
2833 struct NotifyTrafficMessage *ntm;
2836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2837 "Core service passes message from `%4s' of type %u to client.\n",
2838 GNUNET_i2s(&sender->peer),
2839 ntohs (((const struct GNUNET_MessageHeader *) m)->type));
2841 ntm = (struct NotifyTrafficMessage *) buf;
2842 ntm->header.size = htons (msize + sizeof (struct NotifyTrafficMessage));
2843 ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND);
2844 ntm->distance = htonl (sender->last_distance);
2845 ntm->latency = GNUNET_TIME_relative_hton (sender->last_latency);
2846 ntm->peer = sender->peer;
2847 memcpy (&ntm[1], m, msize);
2848 send_to_client (client, &ntm->header, GNUNET_YES);
2853 * Deliver P2P message to interested clients.
2855 * @param sender who sent us the message?
2856 * @param m the message
2857 * @param msize size of the message (including header)
2860 deliver_message (struct Neighbour *sender,
2861 const struct GNUNET_MessageHeader *m, size_t msize)
2863 struct Client *cpos;
2869 type = ntohs (m->type);
2871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2872 "Received encapsulated message of type %u from `%4s'\n",
2874 GNUNET_i2s (&sender->peer));
2876 dropped = GNUNET_YES;
2878 while (cpos != NULL)
2880 deliver_full = GNUNET_NO;
2881 if (0 != (cpos->options & GNUNET_CORE_OPTION_SEND_FULL_INBOUND))
2882 deliver_full = GNUNET_YES;
2885 for (tpos = 0; tpos < cpos->tcnt; tpos++)
2887 if (type != cpos->types[tpos])
2889 deliver_full = GNUNET_YES;
2893 if (GNUNET_YES == deliver_full)
2895 send_p2p_message_to_client (sender, cpos, m, msize);
2896 dropped = GNUNET_NO;
2898 else if (cpos->options & GNUNET_CORE_OPTION_SEND_HDR_INBOUND)
2900 send_p2p_message_to_client (sender, cpos, m,
2901 sizeof (struct GNUNET_MessageHeader));
2905 if (dropped == GNUNET_YES)
2908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2909 "Message of type %u from `%4s' not delivered to any client.\n",
2911 GNUNET_i2s (&sender->peer));
2913 /* FIXME: stats... */
2919 * Align P2P message and then deliver to interested clients.
2921 * @param sender who sent us the message?
2922 * @param buffer unaligned (!) buffer containing message
2923 * @param msize size of the message (including header)
2926 align_and_deliver (struct Neighbour *sender, const char *buffer, size_t msize)
2930 /* TODO: call to statistics? */
2931 memcpy (abuf, buffer, msize);
2932 deliver_message (sender, (const struct GNUNET_MessageHeader *) abuf, msize);
2937 * Deliver P2P messages to interested clients.
2939 * @param sender who sent us the message?
2940 * @param buffer buffer containing messages, can be modified
2941 * @param buffer_size size of the buffer (overall)
2942 * @param offset offset where messages in the buffer start
2945 deliver_messages (struct Neighbour *sender,
2946 const char *buffer, size_t buffer_size, size_t offset)
2948 struct GNUNET_MessageHeader *mhp;
2949 struct GNUNET_MessageHeader mh;
2953 while (offset + sizeof (struct GNUNET_MessageHeader) <= buffer_size)
2955 if (0 != offset % sizeof (uint16_t))
2957 /* outch, need to copy to access header */
2958 memcpy (&mh, &buffer[offset], sizeof (struct GNUNET_MessageHeader));
2963 /* can access header directly */
2964 mhp = (struct GNUNET_MessageHeader *) &buffer[offset];
2966 msize = ntohs (mhp->size);
2967 if (msize + offset > buffer_size)
2969 /* malformed message, header says it is larger than what
2970 would fit into the overall buffer */
2971 GNUNET_break_op (0);
2974 #if HAVE_UNALIGNED_64_ACCESS
2975 need_align = (0 != offset % 4) ? GNUNET_YES : GNUNET_NO;
2977 need_align = (0 != offset % 8) ? GNUNET_YES : GNUNET_NO;
2979 if (GNUNET_YES == need_align)
2980 align_and_deliver (sender, &buffer[offset], msize);
2982 deliver_message (sender,
2983 (const struct GNUNET_MessageHeader *)
2984 &buffer[offset], msize);
2991 * We received an encrypted message. Decrypt, validate and
2992 * pass on to the appropriate clients.
2995 handle_encrypted_message (struct Neighbour *n,
2996 const struct EncryptedMessage *m)
2998 size_t size = ntohs (m->header.size);
3000 struct EncryptedMessage *pt; /* plaintext */
3004 struct GNUNET_TIME_Absolute t;
3008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3009 "Core service receives `%s' request from `%4s'.\n",
3010 "ENCRYPTED_MESSAGE", GNUNET_i2s (&n->peer));
3012 GNUNET_CRYPTO_hash (&m->iv_seed, sizeof (uint32_t), &iv);
3018 &buf[ENCRYPTED_HEADER_SIZE],
3019 size - ENCRYPTED_HEADER_SIZE))
3021 pt = (struct EncryptedMessage *) buf;
3024 GNUNET_CRYPTO_hash (&pt->sequence_number,
3025 size - ENCRYPTED_HEADER_SIZE - sizeof (GNUNET_HashCode), &ph);
3026 if (0 != memcmp (&ph,
3027 &pt->plaintext_hash,
3028 sizeof (GNUNET_HashCode)))
3030 /* checksum failed */
3031 GNUNET_break_op (0);
3035 /* validate sequence number */
3036 snum = ntohl (pt->sequence_number);
3037 if (n->last_sequence_number_received == snum)
3039 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3040 "Received duplicate message, ignoring.\n");
3041 /* duplicate, ignore */
3044 if ((n->last_sequence_number_received > snum) &&
3045 (n->last_sequence_number_received - snum > 32))
3047 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3048 "Received ancient out of sequence message, ignoring.\n");
3049 /* ancient out of sequence, ignore */
3052 if (n->last_sequence_number_received > snum)
3054 unsigned int rotbit =
3055 1 << (n->last_sequence_number_received - snum - 1);
3056 if ((n->last_packets_bitmap & rotbit) != 0)
3058 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3059 "Received duplicate message, ignoring.\n");
3060 /* duplicate, ignore */
3063 n->last_packets_bitmap |= rotbit;
3065 if (n->last_sequence_number_received < snum)
3067 n->last_packets_bitmap <<= (snum - n->last_sequence_number_received);
3068 n->last_sequence_number_received = snum;
3071 /* check timestamp */
3072 t = GNUNET_TIME_absolute_ntoh (pt->timestamp);
3073 if (GNUNET_TIME_absolute_get_duration (t).value > MAX_MESSAGE_AGE.value)
3075 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3077 ("Message received far too old (%llu ms). Content ignored.\n"),
3078 GNUNET_TIME_absolute_get_duration (t).value);
3082 /* process decrypted message(s) */
3083 if (n->bw_out_external_limit.value__ != pt->inbound_bw_limit.value__)
3085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3086 "Received %u b/s as new inbound limit for peer `%4s'\n",
3087 (unsigned int) ntohl (pt->inbound_bw_limit.value__),
3088 GNUNET_i2s (&n->peer));
3089 n->bw_out_external_limit = pt->inbound_bw_limit;
3090 n->bw_out = GNUNET_BANDWIDTH_value_min (n->bw_out_external_limit,
3091 n->bw_out_internal_limit);
3092 GNUNET_BANDWIDTH_tracker_update_quota (&n->available_send_window,
3094 GNUNET_TRANSPORT_set_quota (transport,
3098 GNUNET_TIME_UNIT_FOREVER_REL,
3101 n->last_activity = GNUNET_TIME_absolute_get ();
3102 off = sizeof (struct EncryptedMessage);
3103 deliver_messages (n, buf, size, off);
3108 * Function called by the transport for each received message.
3110 * @param cls closure
3111 * @param peer (claimed) identity of the other peer
3112 * @param message the message
3113 * @param latency estimated latency for communicating with the
3114 * given peer (round-trip)
3115 * @param distance in overlay hops, as given by transport plugin
3118 handle_transport_receive (void *cls,
3119 const struct GNUNET_PeerIdentity *peer,
3120 const struct GNUNET_MessageHeader *message,
3121 struct GNUNET_TIME_Relative latency,
3122 unsigned int distance)
3124 struct Neighbour *n;
3125 struct GNUNET_TIME_Absolute now;
3131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3132 "Received message of type %u from `%4s', demultiplexing.\n",
3133 ntohs (message->type), GNUNET_i2s (peer));
3135 n = find_neighbour (peer);
3137 n = create_neighbour (peer);
3140 n->last_latency = latency;
3141 n->last_distance = distance;
3142 up = (n->status == PEER_STATE_KEY_CONFIRMED);
3143 type = ntohs (message->type);
3144 size = ntohs (message->size);
3147 "Received message of type %u from `%4s'\n",
3153 case GNUNET_MESSAGE_TYPE_CORE_SET_KEY:
3154 if (size != sizeof (struct SetKeyMessage))
3156 GNUNET_break_op (0);
3159 handle_set_key (n, (const struct SetKeyMessage *) message);
3161 case GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE:
3162 if (size < sizeof (struct EncryptedMessage) +
3163 sizeof (struct GNUNET_MessageHeader))
3165 GNUNET_break_op (0);
3168 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
3169 (n->status != PEER_STATE_KEY_CONFIRMED))
3171 GNUNET_break_op (0);
3174 handle_encrypted_message (n, (const struct EncryptedMessage *) message);
3176 case GNUNET_MESSAGE_TYPE_CORE_PING:
3177 if (size != sizeof (struct PingMessage))
3179 GNUNET_break_op (0);
3182 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
3183 (n->status != PEER_STATE_KEY_CONFIRMED))
3186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3187 "Core service receives `%s' request from `%4s' but have not processed key; marking as pending.\n",
3188 "PING", GNUNET_i2s (&n->peer));
3190 GNUNET_free_non_null (n->pending_ping);
3191 n->pending_ping = GNUNET_malloc (sizeof (struct PingMessage));
3192 memcpy (n->pending_ping, message, sizeof (struct PingMessage));
3195 handle_ping (n, (const struct PingMessage *) message);
3197 case GNUNET_MESSAGE_TYPE_CORE_PONG:
3198 if (size != sizeof (struct PongMessage))
3200 GNUNET_break_op (0);
3203 if ( (n->status != PEER_STATE_KEY_RECEIVED) &&
3204 (n->status != PEER_STATE_KEY_CONFIRMED) )
3207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3208 "Core service receives `%s' request from `%4s' but have not processed key; marking as pending.\n",
3209 "PONG", GNUNET_i2s (&n->peer));
3211 GNUNET_free_non_null (n->pending_pong);
3212 n->pending_pong = GNUNET_malloc (sizeof (struct PongMessage));
3213 memcpy (n->pending_pong, message, sizeof (struct PongMessage));
3216 handle_pong (n, (const struct PongMessage *) message);
3219 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3220 _("Unsupported message of type %u received.\n"), type);
3223 if (n->status == PEER_STATE_KEY_CONFIRMED)
3225 now = GNUNET_TIME_absolute_get ();
3226 n->last_activity = now;
3228 n->time_established = now;
3234 * Function that recalculates the bandwidth quota for the
3235 * given neighbour and transmits it to the transport service.
3237 * @param cls neighbour for the quota update
3241 neighbour_quota_update (void *cls,
3242 const struct GNUNET_SCHEDULER_TaskContext *tc)
3244 struct Neighbour *n = cls;
3245 struct GNUNET_BANDWIDTH_Value32NBO q_in;
3248 unsigned long long distributable;
3249 uint64_t need_per_peer;
3250 uint64_t need_per_second;
3252 n->quota_update_task = GNUNET_SCHEDULER_NO_TASK;
3253 /* calculate relative preference among all neighbours;
3254 divides by a bit more to avoid division by zero AND to
3255 account for possibility of new neighbours joining any time
3256 AND to convert to double... */
3257 if (preference_sum == 0)
3259 pref_rel = 1.0 / (double) neighbour_count;
3263 pref_rel = n->current_preference / preference_sum;
3265 need_per_peer = GNUNET_BANDWIDTH_value_get_available_until (MIN_BANDWIDTH_PER_PEER,
3266 GNUNET_TIME_UNIT_SECONDS);
3267 need_per_second = need_per_peer * neighbour_count;
3269 if (bandwidth_target_out_bps > need_per_second)
3270 distributable = bandwidth_target_out_bps - need_per_second;
3271 share = distributable * pref_rel;
3272 if (share + need_per_peer > ( (uint32_t)-1))
3273 q_in = GNUNET_BANDWIDTH_value_init ((uint32_t) -1);
3275 q_in = GNUNET_BANDWIDTH_value_init (need_per_peer + (uint32_t) share);
3276 /* check if we want to disconnect for good due to inactivity */
3277 if ( (GNUNET_TIME_absolute_get_duration (n->last_activity).value > GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.value) &&
3278 (GNUNET_TIME_absolute_get_duration (n->time_established).value > GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.value) )
3281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3282 "Forcing disconnect of `%4s' due to inactivity (?).\n",
3283 GNUNET_i2s (&n->peer));
3285 q_in = GNUNET_BANDWIDTH_value_init (0); /* force disconnect */
3287 #if DEBUG_CORE_QUOTA
3288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3289 "Current quota for `%4s' is %u/%llu b/s in (old: %u b/s) / %u out (%u internal)\n",
3290 GNUNET_i2s (&n->peer),
3291 (unsigned int) ntohl (q_in.value__),
3292 bandwidth_target_out_bps,
3293 (unsigned int) ntohl (n->bw_in.value__),
3294 (unsigned int) ntohl (n->bw_out.value__),
3295 (unsigned int) ntohl (n->bw_out_internal_limit.value__));
3297 if (n->bw_in.value__ != q_in.value__)
3300 GNUNET_TRANSPORT_set_quota (transport,
3304 GNUNET_TIME_UNIT_FOREVER_REL,
3307 schedule_quota_update (n);
3312 * Function called by transport to notify us that
3313 * a peer connected to us (on the network level).
3315 * @param cls closure
3316 * @param peer the peer that connected
3317 * @param latency current latency of the connection
3318 * @param distance in overlay hops, as given by transport plugin
3321 handle_transport_notify_connect (void *cls,
3322 const struct GNUNET_PeerIdentity *peer,
3323 struct GNUNET_TIME_Relative latency,
3324 unsigned int distance)
3326 struct Neighbour *n;
3327 struct GNUNET_TIME_Absolute now;
3328 struct ConnectNotifyMessage cnm;
3330 n = find_neighbour (peer);
3333 if (n->is_connected)
3335 /* duplicate connect notification!? */
3342 n = create_neighbour (peer);
3344 now = GNUNET_TIME_absolute_get ();
3345 n->is_connected = GNUNET_YES;
3346 n->last_latency = latency;
3347 n->last_distance = distance;
3348 GNUNET_BANDWIDTH_tracker_init (&n->available_send_window,
3351 GNUNET_BANDWIDTH_tracker_init (&n->available_recv_window,
3355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3356 "Received connection from `%4s'.\n",
3357 GNUNET_i2s (&n->peer));
3359 cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
3360 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_PRE_CONNECT);
3361 cnm.distance = htonl (n->last_distance);
3362 cnm.latency = GNUNET_TIME_relative_hton (n->last_latency);
3364 send_to_all_clients (&cnm.header, GNUNET_YES, GNUNET_CORE_OPTION_SEND_PRE_CONNECT);
3365 GNUNET_TRANSPORT_set_quota (transport,
3369 GNUNET_TIME_UNIT_FOREVER_REL,
3376 * Function called by transport telling us that a peer
3379 * @param cls closure
3380 * @param peer the peer that disconnected
3383 handle_transport_notify_disconnect (void *cls,
3384 const struct GNUNET_PeerIdentity *peer)
3386 struct DisconnectNotifyMessage cnm;
3387 struct Neighbour *n;
3390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3391 "Peer `%4s' disconnected from us.\n", GNUNET_i2s (peer));
3393 n = find_neighbour (peer);
3399 GNUNET_break (n->is_connected);
3400 cnm.header.size = htons (sizeof (struct DisconnectNotifyMessage));
3401 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT);
3403 send_to_all_clients (&cnm.header, GNUNET_YES, GNUNET_CORE_OPTION_SEND_DISCONNECT);
3404 n->is_connected = GNUNET_NO;
3409 * Last task run during shutdown. Disconnects us from
3413 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3415 struct Neighbour *n;
3419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3420 "Core service shutting down.\n");
3422 GNUNET_assert (transport != NULL);
3423 GNUNET_TRANSPORT_disconnect (transport);
3425 while (NULL != (n = neighbours))
3427 neighbours = n->next;
3428 GNUNET_assert (neighbour_count > 0);
3432 GNUNET_SERVER_notification_context_destroy (notifier);
3434 while (NULL != (c = clients))
3435 handle_client_disconnect (NULL, c->client_handle);
3436 if (my_private_key != NULL)
3437 GNUNET_CRYPTO_rsa_key_free (my_private_key);
3442 * Initiate core service.
3444 * @param cls closure
3445 * @param s scheduler to use
3446 * @param serv the initialized server
3447 * @param c configuration to use
3451 struct GNUNET_SCHEDULER_Handle *s,
3452 struct GNUNET_SERVER_Handle *serv,
3453 const struct GNUNET_CONFIGURATION_Handle *c)
3459 /* parse configuration */
3462 GNUNET_CONFIGURATION_get_value_number (c,
3465 &bandwidth_target_in_bps)) ||
3467 GNUNET_CONFIGURATION_get_value_number (c,
3470 &bandwidth_target_out_bps)) ||
3472 GNUNET_CONFIGURATION_get_value_filename (c,
3474 "HOSTKEY", &keyfile)))
3476 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3478 ("Core service is lacking key configuration settings. Exiting.\n"));
3479 GNUNET_SCHEDULER_shutdown (s);
3482 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3483 GNUNET_free (keyfile);
3484 if (my_private_key == NULL)
3486 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3487 _("Core service could not access hostkey. Exiting.\n"));
3488 GNUNET_SCHEDULER_shutdown (s);
3491 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3492 GNUNET_CRYPTO_hash (&my_public_key,
3493 sizeof (my_public_key), &my_identity.hashPubKey);
3494 /* setup notification */
3496 notifier = GNUNET_SERVER_notification_context_create (server,
3498 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
3499 /* setup transport connection */
3500 transport = GNUNET_TRANSPORT_connect (sched,
3503 &handle_transport_receive,
3504 &handle_transport_notify_connect,
3505 &handle_transport_notify_disconnect);
3506 GNUNET_assert (NULL != transport);
3507 GNUNET_SCHEDULER_add_delayed (sched,
3508 GNUNET_TIME_UNIT_FOREVER_REL,
3509 &cleaning_task, NULL);
3510 /* process client requests */
3511 GNUNET_SERVER_add_handlers (server, handlers);
3512 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3513 _("Core service of `%4s' ready.\n"), GNUNET_i2s (&my_identity));
3519 * The main function for the transport service.
3521 * @param argc number of arguments from the command line
3522 * @param argv command line arguments
3523 * @return 0 ok, 1 on error
3526 main (int argc, char *const *argv)
3528 return (GNUNET_OK ==
3529 GNUNET_SERVICE_run (argc,
3532 GNUNET_SERVICE_OPTION_NONE,
3533 &run, NULL)) ? 0 : 1;
3536 /* end of gnunet-service-core.c */