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 * What is the maximum delay for a SET_KEY message?
81 #define MAX_SET_KEY_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
84 * What how long do we wait for SET_KEY confirmation initially?
86 #define INITIAL_SET_KEY_RETRY_FREQUENCY GNUNET_TIME_relative_multiply (MAX_SET_KEY_DELAY, 3)
89 * What is the maximum delay for a PING message?
91 #define MAX_PING_DELAY GNUNET_TIME_relative_multiply (MAX_SET_KEY_DELAY, 2)
94 * What is the maximum delay for a PONG message?
96 #define MAX_PONG_DELAY GNUNET_TIME_relative_multiply (MAX_PING_DELAY, 2)
99 * How often do we recalculate bandwidth quotas?
101 #define QUOTA_UPDATE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
104 * What is the priority for a SET_KEY message?
106 #define SET_KEY_PRIORITY 0xFFFFFF
109 * What is the priority for a PING message?
111 #define PING_PRIORITY 0xFFFFFF
114 * What is the priority for a PONG message?
116 #define PONG_PRIORITY 0xFFFFFF
119 * How many messages do we queue per peer at most?
121 #define MAX_PEER_QUEUE_SIZE 16
124 * How many non-mandatory messages do we queue per client at most?
126 #define MAX_CLIENT_QUEUE_SIZE 32
129 * What is the maximum age of a message for us to consider
130 * processing it? Note that this looks at the timestamp used
131 * by the other peer, so clock skew between machines does
132 * come into play here. So this should be picked high enough
133 * so that a little bit of clock skew does not prevent peers
134 * from connecting to us.
136 #define MAX_MESSAGE_AGE GNUNET_TIME_UNIT_DAYS
139 * What is the maximum size for encrypted messages? Note that this
140 * number imposes a clear limit on the maximum size of any message.
141 * Set to a value close to 64k but not so close that transports will
142 * have trouble with their headers.
144 #define MAX_ENCRYPTED_MESSAGE_SIZE (63 * 1024)
148 * State machine for our P2P encryption handshake. Everyone starts in
149 * "DOWN", if we receive the other peer's key (other peer initiated)
150 * we start in state RECEIVED (since we will immediately send our
151 * own); otherwise we start in SENT. If we get back a PONG from
152 * within either state, we move up to CONFIRMED (the PONG will always
153 * be sent back encrypted with the key we sent to the other peer).
155 enum PeerStateMachine
159 PEER_STATE_KEY_RECEIVED,
160 PEER_STATE_KEY_CONFIRMED
165 * Number of bytes (at the beginning) of "struct EncryptedMessage"
166 * that are NOT encrypted.
168 #define ENCRYPTED_HEADER_SIZE (sizeof(struct GNUNET_MessageHeader) + sizeof(uint32_t))
172 * Encapsulation for encrypted messages exchanged between
173 * peers. Followed by the actual encrypted data.
175 struct EncryptedMessage
178 * Message type is either CORE_ENCRYPTED_MESSAGE.
180 struct GNUNET_MessageHeader header;
183 * Random value used for IV generation. ENCRYPTED_HEADER_SIZE must
184 * be set to the offset of the *next* field.
186 uint32_t iv_seed GNUNET_PACKED;
189 * Hash of the plaintext (starting at 'sequence_number'), used to
190 * verify message integrity. Everything after this hash (including
191 * this hash itself) will be encrypted.
193 GNUNET_HashCode plaintext_hash;
196 * Sequence number, in network byte order. This field
197 * must be the first encrypted/decrypted field and the
198 * first byte that is hashed for the plaintext hash.
200 uint32_t sequence_number GNUNET_PACKED;
203 * Desired bandwidth (how much we should send to this peer / how
204 * much is the sender willing to receive)?
206 struct GNUNET_BANDWIDTH_Value32NBO inbound_bw_limit;
209 * Timestamp. Used to prevent reply of ancient messages
210 * (recent messages are caught with the sequence number).
212 struct GNUNET_TIME_AbsoluteNBO timestamp;
218 * We're sending an (encrypted) PING to the other peer to check if he
219 * can decrypt. The other peer should respond with a PONG with the
220 * same content, except this time encrypted with the receiver's key.
225 * Message type is CORE_PING.
227 struct GNUNET_MessageHeader header;
230 * Random number chosen to make reply harder.
232 uint32_t challenge GNUNET_PACKED;
235 * Intended target of the PING, used primarily to check
236 * that decryption actually worked.
238 struct GNUNET_PeerIdentity target;
244 * Response to a PING. Includes data from the original PING
245 * plus initial bandwidth quota information.
250 * Message type is CORE_PONG.
252 struct GNUNET_MessageHeader header;
255 * Random number proochosen to make reply harder. Must be
256 * first field after header (this is where we start to encrypt!).
258 uint32_t challenge GNUNET_PACKED;
263 uint32_t reserved GNUNET_PACKED;
266 * Desired bandwidth (how much we should send to this
267 * peer / how much is the sender willing to receive).
269 struct GNUNET_BANDWIDTH_Value32NBO inbound_bw_limit;
272 * Intended target of the PING, used primarily to check
273 * that decryption actually worked.
275 struct GNUNET_PeerIdentity target;
280 * Message transmitted to set (or update) a session key.
286 * Message type is either CORE_SET_KEY.
288 struct GNUNET_MessageHeader header;
291 * Status of the sender (should be in "enum PeerStateMachine"), nbo.
293 int32_t sender_status GNUNET_PACKED;
296 * Purpose of the signature, will be
297 * GNUNET_SIGNATURE_PURPOSE_SET_KEY.
299 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
302 * At what time was this key created?
304 struct GNUNET_TIME_AbsoluteNBO creation_time;
307 * The encrypted session key.
309 struct GNUNET_CRYPTO_RsaEncryptedData encrypted_key;
312 * Who is the intended recipient?
314 struct GNUNET_PeerIdentity target;
317 * Signature of the stuff above (starting at purpose).
319 struct GNUNET_CRYPTO_RsaSignature signature;
325 * Message waiting for transmission. This struct
326 * is followed by the actual content of the message.
332 * We keep messages in a doubly linked list.
334 struct MessageEntry *next;
337 * We keep messages in a doubly linked list.
339 struct MessageEntry *prev;
342 * By when are we supposed to transmit this message?
344 struct GNUNET_TIME_Absolute deadline;
347 * How important is this message to us?
349 unsigned int priority;
352 * How long is the message? (number of bytes following
353 * the "struct MessageEntry", but not including the
354 * size of "struct MessageEntry" itself!)
359 * Was this message selected for transmission in the
360 * current round? GNUNET_YES or GNUNET_NO.
365 * Did we give this message some slack (delayed sending) previously
366 * (and hence should not give it any more slack)? GNUNET_YES or
377 * We keep neighbours in a linked list (for now).
379 struct Neighbour *next;
382 * Unencrypted messages destined for this peer.
384 struct MessageEntry *messages;
387 * Head of the batched, encrypted message queue (already ordered,
388 * transmit starting with the head).
390 struct MessageEntry *encrypted_head;
393 * Tail of the batched, encrypted message queue (already ordered,
394 * append new messages to tail)
396 struct MessageEntry *encrypted_tail;
399 * Handle for pending requests for transmission to this peer
400 * with the transport service. NULL if no request is pending.
402 struct GNUNET_TRANSPORT_TransmitHandle *th;
405 * Public key of the neighbour, NULL if we don't have it yet.
407 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *public_key;
410 * We received a PING message before we got the "public_key"
411 * (or the SET_KEY). We keep it here until we have a key
412 * to decrypt it. NULL if no PING is pending.
414 struct PingMessage *pending_ping;
417 * We received a PONG message before we got the "public_key"
418 * (or the SET_KEY). We keep it here until we have a key
419 * to decrypt it. NULL if no PONG is pending.
421 struct PongMessage *pending_pong;
424 * Non-NULL if we are currently looking up HELLOs for this peer.
427 struct GNUNET_PEERINFO_IteratorContext *pitr;
430 * SetKeyMessage to transmit, NULL if we are not currently trying
433 struct SetKeyMessage *skm;
436 * Identity of the neighbour.
438 struct GNUNET_PeerIdentity peer;
441 * Key we use to encrypt our messages for the other peer
442 * (initialized by us when we do the handshake).
444 struct GNUNET_CRYPTO_AesSessionKey encrypt_key;
447 * Key we use to decrypt messages from the other peer
448 * (given to us by the other peer during the handshake).
450 struct GNUNET_CRYPTO_AesSessionKey decrypt_key;
453 * ID of task used for re-trying plaintext scheduling.
455 GNUNET_SCHEDULER_TaskIdentifier retry_plaintext_task;
458 * ID of task used for re-trying SET_KEY and PING message.
460 GNUNET_SCHEDULER_TaskIdentifier retry_set_key_task;
463 * ID of task used for updating bandwidth quota for this neighbour.
465 GNUNET_SCHEDULER_TaskIdentifier quota_update_task;
468 * ID of task used for cleaning up dead neighbour entries.
470 GNUNET_SCHEDULER_TaskIdentifier dead_clean_task;
473 * At what time did we generate our encryption key?
475 struct GNUNET_TIME_Absolute encrypt_key_created;
478 * At what time did the other peer generate the decryption key?
480 struct GNUNET_TIME_Absolute decrypt_key_created;
483 * At what time did we initially establish (as in, complete session
484 * key handshake) this connection? Should be zero if status != KEY_CONFIRMED.
486 struct GNUNET_TIME_Absolute time_established;
489 * At what time did we last receive an encrypted message from the
490 * other peer? Should be zero if status != KEY_CONFIRMED.
492 struct GNUNET_TIME_Absolute last_activity;
495 * Last latency observed from this peer.
497 struct GNUNET_TIME_Relative last_latency;
500 * At what frequency are we currently re-trying SET_KEY messages?
502 struct GNUNET_TIME_Relative set_key_retry_frequency;
505 * Tracking bandwidth for sending to this peer.
507 struct GNUNET_BANDWIDTH_Tracker available_send_window;
510 * Tracking bandwidth for receiving from this peer.
512 struct GNUNET_BANDWIDTH_Tracker available_recv_window;
515 * How valueable were the messages of this peer recently?
517 unsigned long long current_preference;
520 * Bit map indicating which of the 32 sequence numbers before the last
521 * were received (good for accepting out-of-order packets and
522 * estimating reliability of the connection)
524 unsigned int last_packets_bitmap;
527 * last sequence number received on this connection (highest)
529 uint32_t last_sequence_number_received;
532 * last sequence number transmitted
534 uint32_t last_sequence_number_sent;
537 * Available bandwidth in for this peer (current target).
539 struct GNUNET_BANDWIDTH_Value32NBO bw_in;
542 * Available bandwidth out for this peer (current target).
544 struct GNUNET_BANDWIDTH_Value32NBO bw_out;
547 * Internal bandwidth limit set for this peer (initially typically
548 * set to "-1"). Actual "bw_out" is MIN of
549 * "bpm_out_internal_limit" and "bw_out_external_limit".
551 struct GNUNET_BANDWIDTH_Value32NBO bw_out_internal_limit;
554 * External bandwidth limit set for this peer by the
555 * peer that we are communicating with. "bw_out" is MIN of
556 * "bw_out_internal_limit" and "bw_out_external_limit".
558 struct GNUNET_BANDWIDTH_Value32NBO bw_out_external_limit;
561 * What was our PING challenge number (for this peer)?
563 uint32_t ping_challenge;
566 * What was the last distance to this peer as reported by the transports?
568 uint32_t last_distance;
571 * What is our connection status?
573 enum PeerStateMachine status;
576 * Are we currently connected to this neighbour?
584 * Data structure for each client connected to the core service.
589 * Clients are kept in a linked list.
594 * Handle for the client with the server API.
596 struct GNUNET_SERVER_Client *client_handle;
599 * Array of the types of messages this peer cares
600 * about (with "tcnt" entries). Allocated as part
601 * of this client struct, do not free!
603 const uint16_t *types;
606 * Options for messages this client cares about,
607 * see GNUNET_CORE_OPTION_ values.
612 * Number of types of incoming messages this client
613 * specifically cares about. Size of the "types" array.
623 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
628 static struct GNUNET_PeerIdentity my_identity;
633 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
638 struct GNUNET_SCHEDULER_Handle *sched;
643 const struct GNUNET_CONFIGURATION_Handle *cfg;
648 static struct GNUNET_SERVER_Handle *server;
653 static struct GNUNET_TRANSPORT_Handle *transport;
656 * Linked list of our clients.
658 static struct Client *clients;
661 * Context for notifications we need to send to our clients.
663 static struct GNUNET_SERVER_NotificationContext *notifier;
666 * We keep neighbours in a linked list (for now).
668 static struct Neighbour *neighbours;
671 * Sum of all preferences among all neighbours.
673 static unsigned long long preference_sum;
676 * Total number of neighbours we have.
678 static unsigned int neighbour_count;
681 * How much inbound bandwidth are we supposed to be using per second?
682 * FIXME: this value is not used!
684 static unsigned long long bandwidth_target_in_bps;
687 * How much outbound bandwidth are we supposed to be using per second?
689 static unsigned long long bandwidth_target_out_bps;
694 * A preference value for a neighbour was update. Update
695 * the preference sum accordingly.
697 * @param inc how much was a preference value increased?
700 update_preference_sum (unsigned long long inc)
703 unsigned long long os;
706 preference_sum += inc;
707 if (preference_sum >= os)
709 /* overflow! compensate by cutting all values in half! */
714 n->current_preference /= 2;
715 preference_sum += n->current_preference;
722 * Find the entry for the given neighbour.
724 * @param peer identity of the neighbour
725 * @return NULL if we are not connected, otherwise the
728 static struct Neighbour *
729 find_neighbour (const struct GNUNET_PeerIdentity *peer)
731 struct Neighbour *ret;
734 while ((ret != NULL) &&
735 (0 != memcmp (&ret->peer,
736 peer, sizeof (struct GNUNET_PeerIdentity))))
743 * Send a message to one of our clients.
745 * @param client target for the message
746 * @param msg message to transmit
747 * @param can_drop could this message be dropped if the
748 * client's queue is getting too large?
751 send_to_client (struct Client *client,
752 const struct GNUNET_MessageHeader *msg,
755 #if DEBUG_CORE_CLIENT
756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
757 "Preparing to send message of type %u to client.\n",
760 GNUNET_SERVER_notification_context_unicast (notifier,
761 client->client_handle,
768 * Send a message to all of our current clients that have
769 * the right options set.
771 * @param msg message to multicast
772 * @param can_drop can this message be discarded if the queue is too long
773 * @param options mask to use
776 send_to_all_clients (const struct GNUNET_MessageHeader *msg,
785 if (0 != (c->options & options))
786 send_to_client (c, msg, can_drop);
793 * Handle CORE_INIT request.
796 handle_client_init (void *cls,
797 struct GNUNET_SERVER_Client *client,
798 const struct GNUNET_MessageHeader *message)
800 const struct InitMessage *im;
801 struct InitReplyMessage irm;
804 const uint16_t *types;
807 struct ConnectNotifyMessage cnm;
810 #if DEBUG_CORE_CLIENT
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
812 "Client connecting to core service with `%s' message\n",
815 /* check that we don't have an entry already */
819 if (client == c->client_handle)
822 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
827 msize = ntohs (message->size);
828 if (msize < sizeof (struct InitMessage))
831 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
834 GNUNET_SERVER_notification_context_add (notifier, client);
835 im = (const struct InitMessage *) message;
836 types = (const uint16_t *) &im[1];
837 msize -= sizeof (struct InitMessage);
838 c = GNUNET_malloc (sizeof (struct Client) + msize);
839 c->client_handle = client;
842 c->tcnt = msize / sizeof (uint16_t);
843 c->types = (const uint16_t *) &c[1];
844 wtypes = (uint16_t *) &c[1];
845 for (i=0;i<c->tcnt;i++)
846 wtypes[i] = ntohs (types[i]);
847 c->options = ntohl (im->options);
848 #if DEBUG_CORE_CLIENT
849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
850 "Client %p is interested in %u message types\n",
854 /* send init reply message */
855 irm.header.size = htons (sizeof (struct InitReplyMessage));
856 irm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_INIT_REPLY);
857 irm.reserved = htonl (0);
858 memcpy (&irm.publicKey,
860 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
861 #if DEBUG_CORE_CLIENT
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
863 "Sending `%s' message to client.\n", "INIT_REPLY");
865 send_to_client (c, &irm.header, GNUNET_NO);
866 /* notify new client about existing neighbours */
867 cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
868 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
872 if (n->status == PEER_STATE_KEY_CONFIRMED)
874 #if DEBUG_CORE_CLIENT
875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876 "Sending `%s' message to client.\n", "NOTIFY_CONNECT");
878 cnm.distance = htonl (n->last_distance);
879 cnm.latency = GNUNET_TIME_relative_hton (n->last_latency);
881 send_to_client (c, &cnm.header, GNUNET_NO);
885 GNUNET_SERVER_receive_done (client, GNUNET_OK);
890 * A client disconnected, clean up.
893 * @param client identification of the client
896 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
903 #if DEBUG_CORE_CLIENT
904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
905 "Client %p has disconnected from core service.\n",
912 if (client == pos->client_handle)
917 prev->next = pos->next;
924 /* client never sent INIT */
929 * Handle REQUEST_INFO request.
932 handle_client_request_info (void *cls,
933 struct GNUNET_SERVER_Client *client,
934 const struct GNUNET_MessageHeader *message)
936 const struct RequestInfoMessage *rcm;
938 struct ConfigurationInfoMessage cim;
941 unsigned long long old_preference;
942 struct GNUNET_SERVER_TransmitContext *tc;
944 #if DEBUG_CORE_CLIENT
945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
946 "Core service receives `%s' request.\n", "REQUEST_INFO");
948 rcm = (const struct RequestInfoMessage *) message;
949 n = find_neighbour (&rcm->peer);
950 memset (&cim, 0, sizeof (cim));
953 want_reserv = ntohl (rcm->reserve_inbound);
954 n->bw_out_internal_limit = rcm->limit_outbound;
955 n->bw_out = GNUNET_BANDWIDTH_value_min (n->bw_out_internal_limit,
956 n->bw_out_external_limit);
957 GNUNET_BANDWIDTH_tracker_update_quota (&n->available_recv_window,
961 got_reserv = want_reserv;
963 else if (want_reserv > 0)
965 if (GNUNET_BANDWIDTH_tracker_get_delay (&n->available_recv_window,
966 want_reserv).value == 0)
967 got_reserv = want_reserv;
969 got_reserv = 0; /* all or nothing */
973 GNUNET_BANDWIDTH_tracker_consume (&n->available_recv_window,
975 old_preference = n->current_preference;
976 n->current_preference += GNUNET_ntohll(rcm->preference_change);
977 if (old_preference > n->current_preference)
979 /* overflow; cap at maximum value */
980 n->current_preference = (unsigned long long) -1;
982 update_preference_sum (n->current_preference - old_preference);
984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
985 "Received reservation request for %d bytes for peer `%4s', reserved %d bytes\n",
987 GNUNET_i2s (&rcm->peer),
990 cim.reserved_amount = htonl (got_reserv);
991 cim.bw_in = n->bw_in;
992 cim.bw_out = n->bw_out;
993 cim.preference = n->current_preference;
995 cim.header.size = htons (sizeof (struct ConfigurationInfoMessage));
996 cim.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO);
997 cim.peer = rcm->peer;
999 #if DEBUG_CORE_CLIENT
1000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1001 "Sending `%s' message to client.\n", "CONFIGURATION_INFO");
1003 tc = GNUNET_SERVER_transmit_context_create (client);
1004 GNUNET_SERVER_transmit_context_append_message (tc, &cim.header);
1005 GNUNET_SERVER_transmit_context_run (tc,
1006 GNUNET_TIME_UNIT_FOREVER_REL);
1011 * Free the given entry for the neighbour (it has
1012 * already been removed from the list at this point).
1014 * @param n neighbour to free
1017 free_neighbour (struct Neighbour *n)
1019 struct MessageEntry *m;
1021 if (n->pitr != NULL)
1023 GNUNET_PEERINFO_iterate_cancel (n->pitr);
1028 GNUNET_free (n->skm);
1031 while (NULL != (m = n->messages))
1033 n->messages = m->next;
1036 while (NULL != (m = n->encrypted_head))
1038 GNUNET_CONTAINER_DLL_remove (n->encrypted_head,
1045 GNUNET_TRANSPORT_notify_transmit_ready_cancel (n->th);
1048 if (n->retry_plaintext_task != GNUNET_SCHEDULER_NO_TASK)
1049 GNUNET_SCHEDULER_cancel (sched, n->retry_plaintext_task);
1050 if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
1051 GNUNET_SCHEDULER_cancel (sched, n->retry_set_key_task);
1052 if (n->quota_update_task != GNUNET_SCHEDULER_NO_TASK)
1053 GNUNET_SCHEDULER_cancel (sched, n->quota_update_task);
1054 if (n->dead_clean_task != GNUNET_SCHEDULER_NO_TASK)
1055 GNUNET_SCHEDULER_cancel (sched, n->dead_clean_task);
1056 GNUNET_free_non_null (n->public_key);
1057 GNUNET_free_non_null (n->pending_ping);
1058 GNUNET_free_non_null (n->pending_pong);
1064 * Consider freeing the given neighbour since we may not need
1065 * to keep it around anymore.
1067 * @param n neighbour to consider discarding
1070 consider_free_neighbour (struct Neighbour *n);
1074 * Task triggered when a neighbour entry might have gotten stale.
1076 * @param cls the 'struct Neighbour'
1077 * @param tc scheduler context (not used)
1080 consider_free_task (void *cls,
1081 const struct GNUNET_SCHEDULER_TaskContext *tc)
1083 struct Neighbour *n = cls;
1084 n->dead_clean_task = GNUNET_SCHEDULER_NO_TASK;
1085 consider_free_neighbour (n);
1090 * Consider freeing the given neighbour since we may not need
1091 * to keep it around anymore.
1093 * @param n neighbour to consider discarding
1096 consider_free_neighbour (struct Neighbour *n)
1098 struct Neighbour *pos;
1099 struct Neighbour *prev;
1100 struct GNUNET_TIME_Relative left;
1102 if ( (n->th != NULL) ||
1103 (n->pitr != NULL) ||
1104 (n->status == PEER_STATE_KEY_CONFIRMED) ||
1105 (GNUNET_YES == n->is_connected) )
1106 return; /* no chance */
1108 left = GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_add (n->last_activity,
1112 if (n->dead_clean_task != GNUNET_SCHEDULER_NO_TASK)
1113 GNUNET_SCHEDULER_cancel (sched, n->dead_clean_task);
1114 n->dead_clean_task = GNUNET_SCHEDULER_add_delayed (sched,
1116 &consider_free_task,
1120 /* actually free the neighbour... */
1129 neighbours = n->next;
1131 prev->next = n->next;
1132 GNUNET_assert (neighbour_count > 0);
1139 * Check if we have encrypted messages for the specified neighbour
1140 * pending, and if so, check with the transport about sending them
1143 * @param n neighbour to check.
1145 static void process_encrypted_neighbour_queue (struct Neighbour *n);
1149 * Function called when the transport service is ready to
1150 * receive an encrypted message for the respective peer
1152 * @param cls neighbour to use message from
1153 * @param size number of bytes we can transmit
1154 * @param buf where to copy the message
1155 * @return number of bytes transmitted
1158 notify_encrypted_transmit_ready (void *cls, size_t size, void *buf)
1160 struct Neighbour *n = cls;
1161 struct MessageEntry *m;
1166 GNUNET_assert (NULL != (m = n->encrypted_head));
1167 GNUNET_CONTAINER_DLL_remove (n->encrypted_head,
1174 GNUNET_assert (size >= m->size);
1175 memcpy (cbuf, &m[1], m->size);
1177 GNUNET_BANDWIDTH_tracker_consume (&n->available_send_window,
1180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1181 "Copied message of type %u and size %u into transport buffer for `%4s'\n",
1182 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
1183 ret, GNUNET_i2s (&n->peer));
1185 process_encrypted_neighbour_queue (n);
1190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1191 "Transmission of message of type %u and size %u failed\n",
1192 ntohs (((struct GNUNET_MessageHeader *) &m[1])->type),
1197 consider_free_neighbour (n);
1203 * Check if we have plaintext messages for the specified neighbour
1204 * pending, and if so, consider batching and encrypting them (and
1205 * then trigger processing of the encrypted queue if needed).
1207 * @param n neighbour to check.
1209 static void process_plaintext_neighbour_queue (struct Neighbour *n);
1213 * Check if we have encrypted messages for the specified neighbour
1214 * pending, and if so, check with the transport about sending them
1217 * @param n neighbour to check.
1220 process_encrypted_neighbour_queue (struct Neighbour *n)
1222 struct MessageEntry *m;
1225 return; /* request already pending */
1226 m = n->encrypted_head;
1229 /* encrypted queue empty, try plaintext instead */
1230 process_plaintext_neighbour_queue (n);
1234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1235 "Asking transport for transmission of %u bytes to `%4s' in next %llu ms\n",
1237 GNUNET_i2s (&n->peer),
1238 GNUNET_TIME_absolute_get_remaining (m->deadline).
1242 GNUNET_TRANSPORT_notify_transmit_ready (transport, &n->peer,
1245 GNUNET_TIME_absolute_get_remaining
1247 ¬ify_encrypted_transmit_ready,
1251 /* message request too large or duplicate request */
1253 /* discard encrypted message */
1254 GNUNET_CONTAINER_DLL_remove (n->encrypted_head,
1258 process_encrypted_neighbour_queue (n);
1264 * Decrypt size bytes from in and write the result to out. Use the
1265 * key for inbound traffic of the given neighbour. This function does
1266 * NOT do any integrity-checks on the result.
1268 * @param n neighbour we are receiving from
1269 * @param iv initialization vector to use
1270 * @param in ciphertext
1271 * @param out plaintext
1272 * @param size size of in/out
1273 * @return GNUNET_OK on success
1276 do_decrypt (struct Neighbour *n,
1277 const GNUNET_HashCode * iv,
1278 const void *in, void *out, size_t size)
1280 if (size != (uint16_t) size)
1285 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
1286 (n->status != PEER_STATE_KEY_CONFIRMED))
1288 GNUNET_break_op (0);
1289 return GNUNET_SYSERR;
1292 GNUNET_CRYPTO_aes_decrypt (in,
1296 GNUNET_CRYPTO_AesInitializationVector *) iv,
1300 return GNUNET_SYSERR;
1303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1304 "Decrypted %u bytes from `%4s' using key %u\n",
1305 size, GNUNET_i2s (&n->peer), n->decrypt_key.crc32);
1312 * Encrypt size bytes from in and write the result to out. Use the
1313 * key for outbound traffic of the given neighbour.
1315 * @param n neighbour we are sending to
1316 * @param iv initialization vector to use
1317 * @param in ciphertext
1318 * @param out plaintext
1319 * @param size size of in/out
1320 * @return GNUNET_OK on success
1323 do_encrypt (struct Neighbour *n,
1324 const GNUNET_HashCode * iv,
1325 const void *in, void *out, size_t size)
1327 if (size != (uint16_t) size)
1332 GNUNET_assert (size ==
1333 GNUNET_CRYPTO_aes_encrypt (in,
1337 GNUNET_CRYPTO_AesInitializationVector
1340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1341 "Encrypted %u bytes for `%4s' using key %u\n", size,
1342 GNUNET_i2s (&n->peer), n->encrypt_key.crc32);
1349 * Select messages for transmission. This heuristic uses a combination
1350 * of earliest deadline first (EDF) scheduling (with bounded horizon)
1351 * and priority-based discard (in case no feasible schedule exist) and
1352 * speculative optimization (defer any kind of transmission until
1353 * we either create a batch of significant size, 25% of max, or until
1354 * we are close to a deadline). Furthermore, when scheduling the
1355 * heuristic also packs as many messages into the batch as possible,
1356 * starting with those with the earliest deadline. Yes, this is fun.
1358 * @param n neighbour to select messages from
1359 * @param size number of bytes to select for transmission
1360 * @param retry_time set to the time when we should try again
1361 * (only valid if this function returns zero)
1362 * @return number of bytes selected, or 0 if we decided to
1363 * defer scheduling overall; in that case, retry_time is set.
1366 select_messages (struct Neighbour *n,
1367 size_t size, struct GNUNET_TIME_Relative *retry_time)
1369 struct MessageEntry *pos;
1370 struct MessageEntry *min;
1371 struct MessageEntry *last;
1372 unsigned int min_prio;
1373 struct GNUNET_TIME_Absolute t;
1374 struct GNUNET_TIME_Absolute now;
1375 struct GNUNET_TIME_Relative delta;
1377 struct GNUNET_TIME_Relative slack; /* how long could we wait before missing deadlines? */
1379 int discard_low_prio;
1380 unsigned int queue_size;
1382 GNUNET_assert (NULL != n->messages);
1383 now = GNUNET_TIME_absolute_get ();
1384 /* last entry in linked list of messages processed */
1386 /* should we remove the entry with the lowest
1387 priority from consideration for scheduling at the
1396 discard_low_prio = GNUNET_YES;
1397 while (GNUNET_YES == discard_low_prio)
1401 discard_low_prio = GNUNET_NO;
1402 /* calculate number of bytes available for transmission at time "t" */
1403 avail = GNUNET_BANDWIDTH_tracker_get_available (&n->available_send_window);
1405 /* how many bytes have we (hypothetically) scheduled so far */
1407 /* maximum time we can wait before transmitting anything
1408 and still make all of our deadlines */
1409 slack = GNUNET_TIME_UNIT_FOREVER_REL;
1411 /* note that we use "*2" here because we want to look
1412 a bit further into the future; much more makes no
1413 sense since new message might be scheduled in the
1415 while ((pos != NULL) && (off < size * 2))
1417 if (pos->do_transmit == GNUNET_YES)
1419 /* already removed from consideration */
1423 if (discard_low_prio == GNUNET_NO)
1425 delta = GNUNET_TIME_absolute_get_difference (t, pos->deadline);
1426 if (delta.value > 0)
1428 // FIXME: HUH? Check!
1430 avail += GNUNET_BANDWIDTH_value_get_available_until (n->bw_out,
1433 if (avail < pos->size)
1435 // FIXME: HUH? Check!
1436 discard_low_prio = GNUNET_YES; /* we could not schedule this one! */
1441 /* update slack, considering both its absolute deadline
1442 and relative deadlines caused by other messages
1443 with their respective load */
1444 slack = GNUNET_TIME_relative_min (slack,
1445 GNUNET_BANDWIDTH_value_get_delay_for (n->bw_out,
1447 if ( (pos->deadline.value < now.value) ||
1448 (GNUNET_YES == pos->got_slack) )
1450 slack = GNUNET_TIME_UNIT_ZERO;
1455 GNUNET_TIME_relative_min (slack,
1456 GNUNET_TIME_absolute_get_difference (now, pos->deadline));
1457 pos->got_slack = GNUNET_YES;
1462 t = GNUNET_TIME_absolute_max (pos->deadline, t); // HUH? Check!
1463 if (pos->priority <= min_prio)
1465 /* update min for discard */
1466 min_prio = pos->priority;
1471 if (discard_low_prio)
1473 GNUNET_assert (min != NULL);
1474 /* remove lowest-priority entry from consideration */
1475 min->do_transmit = GNUNET_YES; /* means: discard (for now) */
1479 /* guard against sending "tiny" messages with large headers without
1481 if ( (slack.value > 1000) &&
1483 (queue_size < MAX_PEER_QUEUE_SIZE / 2) )
1485 /* less than 25% of message would be filled with deadlines still
1486 being met if we delay by one second or more; so just wait for
1487 more data; but do not wait longer than 1s (since we don't want
1488 to delay messages for a really long time either). */
1489 retry_time->value = 1000;
1490 /* reset do_transmit values for next time */
1493 pos->do_transmit = GNUNET_NO;
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1498 "Deferring transmission for 1s due to underfull message buffer size (%u/%u)\n",
1500 (unsigned int) size);
1504 /* select marked messages (up to size) for transmission */
1509 if ((pos->size <= size) && (pos->do_transmit == GNUNET_NO))
1511 pos->do_transmit = GNUNET_YES; /* mark for transmission */
1516 pos->do_transmit = GNUNET_NO; /* mark for not transmitting! */
1520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1521 "Selected %u bytes of plaintext messages for transmission to `%4s'.\n",
1522 off, GNUNET_i2s (&n->peer));
1529 * Batch multiple messages into a larger buffer.
1531 * @param n neighbour to take messages from
1532 * @param buf target buffer
1533 * @param size size of buf
1534 * @param deadline set to transmission deadline for the result
1535 * @param retry_time set to the time when we should try again
1536 * (only valid if this function returns zero)
1537 * @param priority set to the priority of the batch
1538 * @return number of bytes written to buf (can be zero)
1541 batch_message (struct Neighbour *n,
1544 struct GNUNET_TIME_Absolute *deadline,
1545 struct GNUNET_TIME_Relative *retry_time,
1546 unsigned int *priority)
1548 char ntmb[GNUNET_SERVER_MAX_MESSAGE_SIZE];
1549 struct NotifyTrafficMessage *ntm = (struct NotifyTrafficMessage*) ntmb;
1550 struct MessageEntry *pos;
1551 struct MessageEntry *prev;
1552 struct MessageEntry *next;
1557 *deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
1558 *retry_time = GNUNET_TIME_UNIT_FOREVER_REL;
1559 if (0 == select_messages (n, size, retry_time))
1561 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1562 "No messages selected, will try again in %llu ms\n",
1566 ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND);
1567 ntm->distance = htonl (n->last_distance);
1568 ntm->latency = GNUNET_TIME_relative_hton (n->last_latency);
1569 ntm->peer = n->peer;
1573 while ((pos != NULL) && (size >= sizeof (struct GNUNET_MessageHeader)))
1576 if (GNUNET_YES == pos->do_transmit)
1578 GNUNET_assert (pos->size <= size);
1579 /* do notifications */
1580 /* FIXME: track if we have *any* client that wants
1581 full notifications and only do this if that is
1583 if (pos->size < GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (struct NotifyTrafficMessage))
1585 memcpy (&ntm[1], &pos[1], pos->size);
1586 ntm->header.size = htons (sizeof (struct NotifyTrafficMessage) +
1587 sizeof (struct GNUNET_MessageHeader));
1588 send_to_all_clients (&ntm->header,
1590 GNUNET_CORE_OPTION_SEND_HDR_OUTBOUND);
1594 /* message too large for 'full' notifications, we do at
1595 least the 'hdr' type */
1598 sizeof (struct GNUNET_MessageHeader));
1600 ntm->header.size = htons (sizeof (struct NotifyTrafficMessage) +
1602 send_to_all_clients (&ntm->header,
1604 GNUNET_CORE_OPTION_SEND_FULL_OUTBOUND);
1607 "Encrypting message of type %u\n",
1608 ntohs(((struct GNUNET_MessageHeader*)&pos[1])->type));
1610 /* copy for encrypted transmission */
1611 memcpy (&buf[ret], &pos[1], pos->size);
1614 *priority += pos->priority;
1616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1617 "Adding plaintext message with deadline %llu ms to batch\n",
1618 GNUNET_TIME_absolute_get_remaining (pos->deadline).value);
1620 deadline->value = GNUNET_MIN (deadline->value, pos->deadline.value);
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1635 "Deadline for message batch is %llu ms\n",
1636 GNUNET_TIME_absolute_get_remaining (*deadline).value);
1643 * Remove messages with deadlines that have long expired from
1646 * @param n neighbour to inspect
1649 discard_expired_messages (struct Neighbour *n)
1651 struct MessageEntry *prev;
1652 struct MessageEntry *next;
1653 struct MessageEntry *pos;
1654 struct GNUNET_TIME_Absolute now;
1655 struct GNUNET_TIME_Relative delta;
1657 now = GNUNET_TIME_absolute_get ();
1663 delta = GNUNET_TIME_absolute_get_difference (pos->deadline, now);
1664 if (delta.value > PAST_EXPIRATION_DISCARD_TIME.value)
1667 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1668 "Message is %llu ms past due, discarding.\n",
1685 * Signature of the main function of a task.
1687 * @param cls closure
1688 * @param tc context information (why was this task triggered now)
1691 retry_plaintext_processing (void *cls,
1692 const struct GNUNET_SCHEDULER_TaskContext *tc)
1694 struct Neighbour *n = cls;
1696 n->retry_plaintext_task = GNUNET_SCHEDULER_NO_TASK;
1697 process_plaintext_neighbour_queue (n);
1702 * Send our key (and encrypted PING) to the other peer.
1704 * @param n the other peer
1706 static void send_key (struct Neighbour *n);
1709 * Task that will retry "send_key" if our previous attempt failed
1713 set_key_retry_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1715 struct Neighbour *n = cls;
1717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1718 "Retrying key transmission to `%4s'\n",
1719 GNUNET_i2s (&n->peer));
1720 n->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
1721 n->set_key_retry_frequency =
1722 GNUNET_TIME_relative_multiply (n->set_key_retry_frequency, 2);
1728 * Check if we have plaintext messages for the specified neighbour
1729 * pending, and if so, consider batching and encrypting them (and
1730 * then trigger processing of the encrypted queue if needed).
1732 * @param n neighbour to check.
1735 process_plaintext_neighbour_queue (struct Neighbour *n)
1737 char pbuf[MAX_ENCRYPTED_MESSAGE_SIZE]; /* plaintext */
1740 struct EncryptedMessage *em; /* encrypted message */
1741 struct EncryptedMessage *ph; /* plaintext header */
1742 struct MessageEntry *me;
1743 unsigned int priority;
1744 struct GNUNET_TIME_Absolute deadline;
1745 struct GNUNET_TIME_Relative retry_time;
1748 if (n->retry_plaintext_task != GNUNET_SCHEDULER_NO_TASK)
1750 GNUNET_SCHEDULER_cancel (sched, n->retry_plaintext_task);
1751 n->retry_plaintext_task = GNUNET_SCHEDULER_NO_TASK;
1755 case PEER_STATE_DOWN:
1758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1759 "Not yet connected to `%4s', deferring processing of plaintext messages.\n",
1760 GNUNET_i2s(&n->peer));
1763 case PEER_STATE_KEY_SENT:
1764 if (n->retry_set_key_task == GNUNET_SCHEDULER_NO_TASK)
1765 n->retry_set_key_task
1766 = GNUNET_SCHEDULER_add_delayed (sched,
1767 n->set_key_retry_frequency,
1768 &set_key_retry_task, n);
1770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1771 "Not yet connected to `%4s', deferring processing of plaintext messages.\n",
1772 GNUNET_i2s(&n->peer));
1775 case PEER_STATE_KEY_RECEIVED:
1776 if (n->retry_set_key_task == GNUNET_SCHEDULER_NO_TASK)
1777 n->retry_set_key_task
1778 = GNUNET_SCHEDULER_add_delayed (sched,
1779 n->set_key_retry_frequency,
1780 &set_key_retry_task, n);
1782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1783 "Not yet connected to `%4s', deferring processing of plaintext messages.\n",
1784 GNUNET_i2s(&n->peer));
1787 case PEER_STATE_KEY_CONFIRMED:
1788 /* ready to continue */
1791 discard_expired_messages (n);
1792 if (n->messages == NULL)
1795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1796 "Plaintext message queue for `%4s' is empty.\n",
1797 GNUNET_i2s(&n->peer));
1799 return; /* no pending messages */
1801 if (n->encrypted_head != NULL)
1804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1805 "Encrypted message queue for `%4s' is still full, delaying plaintext processing.\n",
1806 GNUNET_i2s(&n->peer));
1808 return; /* wait for messages already encrypted to be
1811 ph = (struct EncryptedMessage *) pbuf;
1812 deadline = GNUNET_TIME_UNIT_FOREVER_ABS;
1814 used = sizeof (struct EncryptedMessage);
1815 used += batch_message (n,
1817 MAX_ENCRYPTED_MESSAGE_SIZE - used,
1818 &deadline, &retry_time, &priority);
1819 if (used == sizeof (struct EncryptedMessage))
1822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1823 "No messages selected for transmission to `%4s' at this time, will try again later.\n",
1824 GNUNET_i2s(&n->peer));
1826 /* no messages selected for sending, try again later... */
1827 n->retry_plaintext_task =
1828 GNUNET_SCHEDULER_add_delayed (sched,
1830 &retry_plaintext_processing, n);
1833 #if DEBUG_CORE_QUOTA
1834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1835 "Sending %u b/s as new limit to peer `%4s'\n",
1836 (unsigned int) ntohl (n->bw_in.value__),
1837 GNUNET_i2s (&n->peer));
1839 ph->iv_seed = htonl (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, -1));
1840 ph->sequence_number = htonl (++n->last_sequence_number_sent);
1841 ph->inbound_bw_limit = n->bw_in;
1842 ph->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1844 /* setup encryption message header */
1845 me = GNUNET_malloc (sizeof (struct MessageEntry) + used);
1846 me->deadline = deadline;
1847 me->priority = priority;
1849 em = (struct EncryptedMessage *) &me[1];
1850 em->header.size = htons (used);
1851 em->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE);
1852 em->iv_seed = ph->iv_seed;
1853 esize = used - ENCRYPTED_HEADER_SIZE;
1854 GNUNET_CRYPTO_hash (&ph->sequence_number,
1855 esize - sizeof (GNUNET_HashCode),
1856 &ph->plaintext_hash);
1857 GNUNET_CRYPTO_hash (&ph->iv_seed, sizeof (uint32_t), &iv);
1860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1861 "Encrypting %u bytes of plaintext messages for `%4s' for transmission in %llums.\n",
1863 GNUNET_i2s(&n->peer),
1864 (unsigned long long) GNUNET_TIME_absolute_get_remaining (deadline).value);
1866 GNUNET_assert (GNUNET_OK ==
1869 &ph->plaintext_hash,
1870 &em->plaintext_hash, esize));
1871 /* append to transmission list */
1872 GNUNET_CONTAINER_DLL_insert_after (n->encrypted_head,
1876 process_encrypted_neighbour_queue (n);
1881 * Function that recalculates the bandwidth quota for the
1882 * given neighbour and transmits it to the transport service.
1884 * @param cls neighbour for the quota update
1888 neighbour_quota_update (void *cls,
1889 const struct GNUNET_SCHEDULER_TaskContext *tc);
1893 * Schedule the task that will recalculate the bandwidth
1894 * quota for this peer (and possibly force a disconnect of
1895 * idle peers by calculating a bandwidth of zero).
1898 schedule_quota_update (struct Neighbour *n)
1900 GNUNET_assert (n->quota_update_task ==
1901 GNUNET_SCHEDULER_NO_TASK);
1902 n->quota_update_task
1903 = GNUNET_SCHEDULER_add_delayed (sched,
1904 QUOTA_UPDATE_FREQUENCY,
1905 &neighbour_quota_update,
1911 * Initialize a new 'struct Neighbour'.
1913 * @param pid ID of the new neighbour
1914 * @return handle for the new neighbour
1916 static struct Neighbour *
1917 create_neighbour (const struct GNUNET_PeerIdentity *pid)
1919 struct Neighbour *n;
1920 struct GNUNET_TIME_Absolute now;
1922 n = GNUNET_malloc (sizeof (struct Neighbour));
1923 n->next = neighbours;
1927 GNUNET_CRYPTO_aes_create_session_key (&n->encrypt_key);
1928 now = GNUNET_TIME_absolute_get ();
1929 n->encrypt_key_created = now;
1930 n->last_activity = now;
1931 n->set_key_retry_frequency = INITIAL_SET_KEY_RETRY_FREQUENCY;
1932 n->bw_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
1933 n->bw_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
1934 n->bw_out_internal_limit = GNUNET_BANDWIDTH_value_init ((uint32_t) - 1);
1935 n->bw_out_external_limit = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
1936 n->ping_challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1938 schedule_quota_update (n);
1944 * Handle CORE_SEND request.
1947 * @param client the client issuing the request
1948 * @param message the "struct SendMessage"
1951 handle_client_send (void *cls,
1952 struct GNUNET_SERVER_Client *client,
1953 const struct GNUNET_MessageHeader *message)
1955 const struct SendMessage *sm;
1956 struct Neighbour *n;
1957 struct MessageEntry *prev;
1958 struct MessageEntry *pos;
1959 struct MessageEntry *e;
1960 struct MessageEntry *min_prio_entry;
1961 struct MessageEntry *min_prio_prev;
1962 unsigned int min_prio;
1963 unsigned int queue_size;
1966 msize = ntohs (message->size);
1968 sizeof (struct SendMessage) + sizeof (struct GNUNET_MessageHeader))
1972 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1975 sm = (const struct SendMessage *) message;
1976 msize -= sizeof (struct SendMessage);
1977 n = find_neighbour (&sm->peer);
1979 n = create_neighbour (&sm->peer);
1981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1982 "Core received `%s' request, queueing %u bytes of plaintext data for transmission to `%4s'.\n",
1985 GNUNET_i2s (&sm->peer));
1987 /* bound queue size */
1988 discard_expired_messages (n);
1989 min_prio = (unsigned int) -1;
1990 min_prio_entry = NULL;
1991 min_prio_prev = NULL;
1997 if (pos->priority < min_prio)
1999 min_prio_entry = pos;
2000 min_prio_prev = prev;
2001 min_prio = pos->priority;
2007 if (queue_size >= MAX_PEER_QUEUE_SIZE)
2010 if (ntohl(sm->priority) <= min_prio)
2012 /* discard new entry */
2014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2015 "Queue full (%u/%u), discarding new request (%u bytes of type %u)\n",
2017 MAX_PEER_QUEUE_SIZE,
2019 ntohs (message->type));
2022 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2025 /* discard "min_prio_entry" */
2027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2028 "Queue full, discarding existing older request\n");
2030 if (min_prio_prev == NULL)
2031 n->messages = min_prio_entry->next;
2033 min_prio_prev->next = min_prio_entry->next;
2034 GNUNET_free (min_prio_entry);
2038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2039 "Adding transmission request for `%4s' to queue\n",
2040 GNUNET_i2s (&sm->peer));
2042 e = GNUNET_malloc (sizeof (struct MessageEntry) + msize);
2043 e->deadline = GNUNET_TIME_absolute_ntoh (sm->deadline);
2044 e->priority = ntohl (sm->priority);
2046 memcpy (&e[1], &sm[1], msize);
2048 /* insert, keep list sorted by deadline */
2051 while ((pos != NULL) && (pos->deadline.value < e->deadline.value))
2062 /* consider scheduling now */
2063 process_plaintext_neighbour_queue (n);
2065 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2070 * Function called when the transport service is ready to
2071 * receive a message. Only resets 'n->th' to NULL.
2073 * @param cls neighbour to use message from
2074 * @param size number of bytes we can transmit
2075 * @param buf where to copy the message
2076 * @return number of bytes transmitted
2079 notify_transport_connect_done (void *cls, size_t size, void *buf)
2081 struct Neighbour *n = cls;
2086 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2087 _("Failed to connect to `%4s': transport failed to connect\n"),
2088 GNUNET_i2s (&n->peer));
2097 * Handle CORE_REQUEST_CONNECT request.
2100 * @param client the client issuing the request
2101 * @param message the "struct ConnectMessage"
2104 handle_client_request_connect (void *cls,
2105 struct GNUNET_SERVER_Client *client,
2106 const struct GNUNET_MessageHeader *message)
2108 const struct ConnectMessage *cm = (const struct ConnectMessage*) message;
2109 struct Neighbour *n;
2110 struct GNUNET_TIME_Relative timeout;
2112 if (0 == memcmp (&cm->peer, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
2115 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2118 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2119 n = find_neighbour (&cm->peer);
2121 n = create_neighbour (&cm->peer);
2122 if ( (n->is_connected) ||
2124 return; /* already connected, or at least trying */
2126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2127 "Core received `%s' request for `%4s', will try to establish connection\n",
2129 GNUNET_i2s (&cm->peer));
2131 timeout = GNUNET_TIME_relative_ntoh (cm->timeout);
2132 /* ask transport to connect to the peer */
2133 n->th = GNUNET_TRANSPORT_notify_transmit_ready (transport,
2135 sizeof (struct GNUNET_MessageHeader), 0,
2137 ¬ify_transport_connect_done,
2139 GNUNET_break (NULL != n->th);
2144 * List of handlers for the messages understood by this
2147 static struct GNUNET_SERVER_MessageHandler handlers[] = {
2148 {&handle_client_init, NULL,
2149 GNUNET_MESSAGE_TYPE_CORE_INIT, 0},
2150 {&handle_client_request_info, NULL,
2151 GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO,
2152 sizeof (struct RequestInfoMessage)},
2153 {&handle_client_send, NULL,
2154 GNUNET_MESSAGE_TYPE_CORE_SEND, 0},
2155 {&handle_client_request_connect, NULL,
2156 GNUNET_MESSAGE_TYPE_CORE_REQUEST_CONNECT,
2157 sizeof (struct ConnectMessage)},
2163 * PEERINFO is giving us a HELLO for a peer. Add the public key to
2164 * the neighbour's struct and retry send_key. Or, if we did not get a
2165 * HELLO, just do nothing.
2167 * @param cls the 'struct Neighbour' to retry sending the key for
2168 * @param peer the peer for which this is the HELLO
2169 * @param hello HELLO message of that peer
2170 * @param trust amount of trust we currently have in that peer
2173 process_hello_retry_send_key (void *cls,
2174 const struct GNUNET_PeerIdentity *peer,
2175 const struct GNUNET_HELLO_Message *hello,
2178 struct Neighbour *n = cls;
2183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2184 "Entered `process_hello_retry_send_key' and `peer' is NULL!\n");
2187 if (n->public_key != NULL)
2193 if (GNUNET_SCHEDULER_NO_TASK == n->retry_set_key_task)
2194 n->retry_set_key_task
2195 = GNUNET_SCHEDULER_add_delayed (sched,
2196 n->set_key_retry_frequency,
2197 &set_key_retry_task, n);
2203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2204 "Entered `process_hello_retry_send_key' for peer `%4s'\n",
2207 if (n->public_key != NULL)
2210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2211 "already have public key for peer %s!! (so why are we here?)\n",
2218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2219 "Received new `%s' message for `%4s', initiating key exchange.\n",
2224 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2225 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, n->public_key))
2227 GNUNET_free (n->public_key);
2228 n->public_key = NULL;
2230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2231 "GNUNET_HELLO_get_key returned awfully\n");
2239 * Send our key (and encrypted PING) to the other peer.
2241 * @param n the other peer
2244 send_key (struct Neighbour *n)
2246 struct SetKeyMessage *sm;
2247 struct MessageEntry *me;
2248 struct PingMessage pp;
2249 struct PingMessage *pm;
2251 if ( (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK) ||
2255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2256 "Key exchange in progress with `%4s'.\n",
2257 GNUNET_i2s (&n->peer));
2259 return; /* already in progress */
2263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2264 "Asked to perform key exchange with `%4s'.\n",
2265 GNUNET_i2s (&n->peer));
2267 if (n->public_key == NULL)
2269 /* lookup n's public key, then try again */
2271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2272 "Lacking public key for `%4s', trying to obtain one (send_key).\n",
2273 GNUNET_i2s (&n->peer));
2275 GNUNET_assert (n->pitr == NULL);
2276 n->pitr = GNUNET_PEERINFO_iterate (cfg,
2280 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 20),
2281 &process_hello_retry_send_key, n);
2284 /* first, set key message */
2285 me = GNUNET_malloc (sizeof (struct MessageEntry) +
2286 sizeof (struct SetKeyMessage));
2287 me->deadline = GNUNET_TIME_relative_to_absolute (MAX_SET_KEY_DELAY);
2288 me->priority = SET_KEY_PRIORITY;
2289 me->size = sizeof (struct SetKeyMessage);
2290 GNUNET_CONTAINER_DLL_insert_after (n->encrypted_head,
2294 sm = (struct SetKeyMessage *) &me[1];
2295 sm->header.size = htons (sizeof (struct SetKeyMessage));
2296 sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SET_KEY);
2297 sm->sender_status = htonl ((int32_t) ((n->status == PEER_STATE_DOWN) ?
2298 PEER_STATE_KEY_SENT : n->status));
2300 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2301 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
2302 sizeof (struct GNUNET_CRYPTO_RsaEncryptedData) +
2303 sizeof (struct GNUNET_PeerIdentity));
2304 sm->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_SET_KEY);
2305 sm->creation_time = GNUNET_TIME_absolute_hton (n->encrypt_key_created);
2306 sm->target = n->peer;
2307 GNUNET_assert (GNUNET_OK ==
2308 GNUNET_CRYPTO_rsa_encrypt (&n->encrypt_key,
2310 GNUNET_CRYPTO_AesSessionKey),
2312 &sm->encrypted_key));
2313 GNUNET_assert (GNUNET_OK ==
2314 GNUNET_CRYPTO_rsa_sign (my_private_key, &sm->purpose,
2317 /* second, encrypted PING message */
2318 me = GNUNET_malloc (sizeof (struct MessageEntry) +
2319 sizeof (struct PingMessage));
2320 me->deadline = GNUNET_TIME_relative_to_absolute (MAX_PING_DELAY);
2321 me->priority = PING_PRIORITY;
2322 me->size = sizeof (struct PingMessage);
2323 n->encrypted_tail->next = me;
2324 n->encrypted_tail = me;
2325 pm = (struct PingMessage *) &me[1];
2326 pm->header.size = htons (sizeof (struct PingMessage));
2327 pm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PING);
2328 pp.challenge = htonl (n->ping_challenge);
2329 pp.target = n->peer;
2331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2332 "Encrypting `%s' and `%s' messages for `%4s'.\n",
2333 "SET_KEY", "PING", GNUNET_i2s (&n->peer));
2334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2335 "Sending `%s' to `%4s' with challenge %u encrypted using key %u\n",
2337 GNUNET_i2s (&n->peer), n->ping_challenge, n->encrypt_key.crc32);
2340 &n->peer.hashPubKey,
2343 sizeof (struct PingMessage) -
2344 sizeof (struct GNUNET_MessageHeader));
2348 case PEER_STATE_DOWN:
2349 n->status = PEER_STATE_KEY_SENT;
2351 case PEER_STATE_KEY_SENT:
2353 case PEER_STATE_KEY_RECEIVED:
2355 case PEER_STATE_KEY_CONFIRMED:
2362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2363 "Have %llu ms left for `%s' transmission.\n",
2364 (unsigned long long) GNUNET_TIME_absolute_get_remaining (me->deadline).value,
2367 /* trigger queue processing */
2368 process_encrypted_neighbour_queue (n);
2369 if ( (n->status != PEER_STATE_KEY_CONFIRMED) &&
2370 (GNUNET_SCHEDULER_NO_TASK == n->retry_set_key_task) )
2371 n->retry_set_key_task
2372 = GNUNET_SCHEDULER_add_delayed (sched,
2373 n->set_key_retry_frequency,
2374 &set_key_retry_task, n);
2379 * We received a SET_KEY message. Validate and update
2380 * our key material and status.
2382 * @param n the neighbour from which we received message m
2383 * @param m the set key message we received
2386 handle_set_key (struct Neighbour *n, const struct SetKeyMessage *m);
2390 * PEERINFO is giving us a HELLO for a peer. Add the public key to
2391 * the neighbour's struct and retry handling the set_key message. Or,
2392 * if we did not get a HELLO, just free the set key message.
2394 * @param cls pointer to the set key message
2395 * @param peer the peer for which this is the HELLO
2396 * @param hello HELLO message of that peer
2397 * @param trust amount of trust we currently have in that peer
2400 process_hello_retry_handle_set_key (void *cls,
2401 const struct GNUNET_PeerIdentity *peer,
2402 const struct GNUNET_HELLO_Message *hello,
2405 struct Neighbour *n = cls;
2406 struct SetKeyMessage *sm = n->skm;
2415 if (n->public_key != NULL)
2416 return; /* multiple HELLOs match!? */
2418 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2419 if (GNUNET_OK != GNUNET_HELLO_get_key (hello, n->public_key))
2421 GNUNET_break_op (0);
2422 GNUNET_free (n->public_key);
2423 n->public_key = NULL;
2427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2428 "Received `%s' for `%4s', continuing processing of `%s' message.\n",
2429 "HELLO", GNUNET_i2s (peer), "SET_KEY");
2431 handle_set_key (n, sm);
2436 * We received a PING message. Validate and transmit
2439 * @param n sender of the PING
2440 * @param m the encrypted PING message itself
2443 handle_ping (struct Neighbour *n, const struct PingMessage *m)
2445 struct PingMessage t;
2446 struct PongMessage tx;
2447 struct PongMessage *tp;
2448 struct MessageEntry *me;
2451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2452 "Core service receives `%s' request from `%4s'.\n",
2453 "PING", GNUNET_i2s (&n->peer));
2457 &my_identity.hashPubKey,
2460 sizeof (struct PingMessage) -
2461 sizeof (struct GNUNET_MessageHeader)))
2464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2465 "Decrypted `%s' to `%4s' with challenge %u decrypted using key %u\n",
2467 GNUNET_i2s (&t.target),
2468 ntohl (t.challenge), n->decrypt_key.crc32);
2469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2470 "Target of `%s' request is `%4s'.\n",
2471 "PING", GNUNET_i2s (&t.target));
2473 if (0 != memcmp (&t.target,
2474 &my_identity, sizeof (struct GNUNET_PeerIdentity)))
2476 GNUNET_break_op (0);
2479 me = GNUNET_malloc (sizeof (struct MessageEntry) +
2480 sizeof (struct PongMessage));
2481 GNUNET_CONTAINER_DLL_insert_after (n->encrypted_head,
2485 me->deadline = GNUNET_TIME_relative_to_absolute (MAX_PONG_DELAY);
2486 me->priority = PONG_PRIORITY;
2487 me->size = sizeof (struct PongMessage);
2488 tx.reserved = htonl (0);
2489 tx.inbound_bw_limit = n->bw_in;
2490 tx.challenge = t.challenge;
2491 tx.target = t.target;
2492 tp = (struct PongMessage *) &me[1];
2493 tp->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PONG);
2494 tp->header.size = htons (sizeof (struct PongMessage));
2496 &my_identity.hashPubKey,
2499 sizeof (struct PongMessage) -
2500 sizeof (struct GNUNET_MessageHeader));
2502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2503 "Encrypting `%s' with challenge %u using key %u\n", "PONG",
2504 ntohl (t.challenge), n->encrypt_key.crc32);
2506 /* trigger queue processing */
2507 process_encrypted_neighbour_queue (n);
2512 * We received a PONG message. Validate and update our status.
2514 * @param n sender of the PONG
2515 * @param m the encrypted PONG message itself
2518 handle_pong (struct Neighbour *n,
2519 const struct PongMessage *m)
2521 struct PongMessage t;
2522 struct ConnectNotifyMessage cnm;
2525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2526 "Core service receives `%s' request from `%4s'.\n",
2527 "PONG", GNUNET_i2s (&n->peer));
2531 &n->peer.hashPubKey,
2534 sizeof (struct PongMessage) -
2535 sizeof (struct GNUNET_MessageHeader)))
2537 if (0 != ntohl (t.reserved))
2539 GNUNET_break_op (0);
2543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2544 "Decrypted `%s' from `%4s' with challenge %u using key %u\n",
2546 GNUNET_i2s (&t.target),
2547 ntohl (t.challenge), n->decrypt_key.crc32);
2549 if ((0 != memcmp (&t.target,
2551 sizeof (struct GNUNET_PeerIdentity))) ||
2552 (n->ping_challenge != ntohl (t.challenge)))
2554 /* PONG malformed */
2556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2557 "Received malformed `%s' wanted sender `%4s' with challenge %u\n",
2558 "PONG", GNUNET_i2s (&n->peer), n->ping_challenge);
2559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2560 "Received malformed `%s' received from `%4s' with challenge %u\n",
2561 "PONG", GNUNET_i2s (&t.target), ntohl (t.challenge));
2563 GNUNET_break_op (0);
2568 case PEER_STATE_DOWN:
2569 GNUNET_break (0); /* should be impossible */
2571 case PEER_STATE_KEY_SENT:
2572 GNUNET_break (0); /* should be impossible, how did we decrypt? */
2574 case PEER_STATE_KEY_RECEIVED:
2575 n->status = PEER_STATE_KEY_CONFIRMED;
2576 n->bw_out_external_limit = t.inbound_bw_limit;
2577 n->bw_out = GNUNET_BANDWIDTH_value_min (n->bw_out_external_limit,
2578 n->bw_out_internal_limit);
2579 GNUNET_BANDWIDTH_tracker_update_quota (&n->available_send_window,
2582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2583 "Confirmed key via `%s' message for peer `%4s'\n",
2584 "PONG", GNUNET_i2s (&n->peer));
2586 if (n->retry_set_key_task != GNUNET_SCHEDULER_NO_TASK)
2588 GNUNET_SCHEDULER_cancel (sched, n->retry_set_key_task);
2589 n->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
2591 cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
2592 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT);
2593 cnm.distance = htonl (n->last_distance);
2594 cnm.latency = GNUNET_TIME_relative_hton (n->last_latency);
2596 send_to_all_clients (&cnm.header, GNUNET_YES, GNUNET_CORE_OPTION_SEND_CONNECT);
2597 process_encrypted_neighbour_queue (n);
2599 case PEER_STATE_KEY_CONFIRMED:
2600 /* duplicate PONG? */
2610 * We received a SET_KEY message. Validate and update
2611 * our key material and status.
2613 * @param n the neighbour from which we received message m
2614 * @param m the set key message we received
2617 handle_set_key (struct Neighbour *n, const struct SetKeyMessage *m)
2619 struct SetKeyMessage *m_cpy;
2620 struct GNUNET_TIME_Absolute t;
2621 struct GNUNET_CRYPTO_AesSessionKey k;
2622 struct PingMessage *ping;
2623 struct PongMessage *pong;
2624 enum PeerStateMachine sender_status;
2627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2628 "Core service receives `%s' request from `%4s'.\n",
2629 "SET_KEY", GNUNET_i2s (&n->peer));
2631 if (n->public_key == NULL)
2633 if (n->pitr != NULL)
2636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2637 "Ignoring `%s' message due to lack of public key for peer (still trying to obtain one).\n",
2643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2644 "Lacking public key for peer, trying to obtain one (handle_set_key).\n");
2646 m_cpy = GNUNET_malloc (sizeof (struct SetKeyMessage));
2647 memcpy (m_cpy, m, sizeof (struct SetKeyMessage));
2648 /* lookup n's public key, then try again */
2649 GNUNET_assert (n->skm == NULL);
2651 n->pitr = GNUNET_PEERINFO_iterate (cfg,
2655 GNUNET_TIME_UNIT_MINUTES,
2656 &process_hello_retry_handle_set_key, n);
2659 if (0 != memcmp (&m->target,
2661 sizeof (struct GNUNET_PeerIdentity)))
2663 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2664 _("Received `%s' message that was not for me. Ignoring.\n"),
2668 if ((ntohl (m->purpose.size) !=
2669 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2670 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
2671 sizeof (struct GNUNET_CRYPTO_RsaEncryptedData) +
2672 sizeof (struct GNUNET_PeerIdentity)) ||
2674 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_SET_KEY,
2675 &m->purpose, &m->signature, n->public_key)))
2677 /* invalid signature */
2678 GNUNET_break_op (0);
2681 t = GNUNET_TIME_absolute_ntoh (m->creation_time);
2682 if (((n->status == PEER_STATE_KEY_RECEIVED) ||
2683 (n->status == PEER_STATE_KEY_CONFIRMED)) &&
2684 (t.value < n->decrypt_key_created.value))
2686 /* this could rarely happen due to massive re-ordering of
2687 messages on the network level, but is most likely either
2688 a bug or some adversary messing with us. Report. */
2689 GNUNET_break_op (0);
2693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Decrypting key material.\n");
2695 if ((GNUNET_CRYPTO_rsa_decrypt (my_private_key,
2698 sizeof (struct GNUNET_CRYPTO_AesSessionKey))
2699 != sizeof (struct GNUNET_CRYPTO_AesSessionKey)) ||
2700 (GNUNET_OK != GNUNET_CRYPTO_aes_check_session_key (&k)))
2702 /* failed to decrypt !? */
2703 GNUNET_break_op (0);
2708 if (n->decrypt_key_created.value != t.value)
2710 /* fresh key, reset sequence numbers */
2711 n->last_sequence_number_received = 0;
2712 n->last_packets_bitmap = 0;
2713 n->decrypt_key_created = t;
2715 sender_status = (enum PeerStateMachine) ntohl (m->sender_status);
2718 case PEER_STATE_DOWN:
2719 n->status = PEER_STATE_KEY_RECEIVED;
2721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2722 "Responding to `%s' with my own key.\n", "SET_KEY");
2726 case PEER_STATE_KEY_SENT:
2727 case PEER_STATE_KEY_RECEIVED:
2728 n->status = PEER_STATE_KEY_RECEIVED;
2729 if ((sender_status != PEER_STATE_KEY_RECEIVED) &&
2730 (sender_status != PEER_STATE_KEY_CONFIRMED))
2733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2734 "Responding to `%s' with my own key (other peer has status %u).\n",
2735 "SET_KEY", sender_status);
2740 case PEER_STATE_KEY_CONFIRMED:
2741 if ((sender_status != PEER_STATE_KEY_RECEIVED) &&
2742 (sender_status != PEER_STATE_KEY_CONFIRMED))
2745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2746 "Responding to `%s' with my own key (other peer has status %u), I was already fully up.\n",
2747 "SET_KEY", sender_status);
2756 if (n->pending_ping != NULL)
2758 ping = n->pending_ping;
2759 n->pending_ping = NULL;
2760 handle_ping (n, ping);
2763 if (n->pending_pong != NULL)
2765 pong = n->pending_pong;
2766 n->pending_pong = NULL;
2767 handle_pong (n, pong);
2774 * Send a P2P message to a client.
2776 * @param sender who sent us the message?
2777 * @param client who should we give the message to?
2778 * @param m contains the message to transmit
2779 * @param msize number of bytes in buf to transmit
2782 send_p2p_message_to_client (struct Neighbour *sender,
2783 struct Client *client,
2784 const void *m, size_t msize)
2786 char buf[msize + sizeof (struct NotifyTrafficMessage)];
2787 struct NotifyTrafficMessage *ntm;
2790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2791 "Core service passes message from `%4s' of type %u to client.\n",
2792 GNUNET_i2s(&sender->peer),
2793 ntohs (((const struct GNUNET_MessageHeader *) m)->type));
2795 ntm = (struct NotifyTrafficMessage *) buf;
2796 ntm->header.size = htons (msize + sizeof (struct NotifyTrafficMessage));
2797 ntm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND);
2798 ntm->distance = htonl (sender->last_distance);
2799 ntm->latency = GNUNET_TIME_relative_hton (sender->last_latency);
2800 ntm->peer = sender->peer;
2801 memcpy (&ntm[1], m, msize);
2802 send_to_client (client, &ntm->header, GNUNET_YES);
2807 * Deliver P2P message to interested clients.
2809 * @param sender who sent us the message?
2810 * @param m the message
2811 * @param msize size of the message (including header)
2814 deliver_message (struct Neighbour *sender,
2815 const struct GNUNET_MessageHeader *m, size_t msize)
2817 struct Client *cpos;
2823 type = ntohs (m->type);
2825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2826 "Received encapsulated message of type %u from `%4s'\n",
2828 GNUNET_i2s (&sender->peer));
2830 dropped = GNUNET_YES;
2832 while (cpos != NULL)
2834 deliver_full = GNUNET_NO;
2835 if (0 != (cpos->options & GNUNET_CORE_OPTION_SEND_FULL_INBOUND))
2836 deliver_full = GNUNET_YES;
2839 for (tpos = 0; tpos < cpos->tcnt; tpos++)
2841 if (type != cpos->types[tpos])
2843 deliver_full = GNUNET_YES;
2847 if (GNUNET_YES == deliver_full)
2849 send_p2p_message_to_client (sender, cpos, m, msize);
2850 dropped = GNUNET_NO;
2852 else if (cpos->options & GNUNET_CORE_OPTION_SEND_HDR_INBOUND)
2854 send_p2p_message_to_client (sender, cpos, m,
2855 sizeof (struct GNUNET_MessageHeader));
2859 if (dropped == GNUNET_YES)
2862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2863 "Message of type %u from `%4s' not delivered to any client.\n",
2865 GNUNET_i2s (&sender->peer));
2867 /* FIXME: stats... */
2873 * Align P2P message and then deliver to interested clients.
2875 * @param sender who sent us the message?
2876 * @param buffer unaligned (!) buffer containing message
2877 * @param msize size of the message (including header)
2880 align_and_deliver (struct Neighbour *sender, const char *buffer, size_t msize)
2884 /* TODO: call to statistics? */
2885 memcpy (abuf, buffer, msize);
2886 deliver_message (sender, (const struct GNUNET_MessageHeader *) abuf, msize);
2891 * Deliver P2P messages to interested clients.
2893 * @param sender who sent us the message?
2894 * @param buffer buffer containing messages, can be modified
2895 * @param buffer_size size of the buffer (overall)
2896 * @param offset offset where messages in the buffer start
2899 deliver_messages (struct Neighbour *sender,
2900 const char *buffer, size_t buffer_size, size_t offset)
2902 struct GNUNET_MessageHeader *mhp;
2903 struct GNUNET_MessageHeader mh;
2907 while (offset + sizeof (struct GNUNET_MessageHeader) <= buffer_size)
2909 if (0 != offset % sizeof (uint16_t))
2911 /* outch, need to copy to access header */
2912 memcpy (&mh, &buffer[offset], sizeof (struct GNUNET_MessageHeader));
2917 /* can access header directly */
2918 mhp = (struct GNUNET_MessageHeader *) &buffer[offset];
2920 msize = ntohs (mhp->size);
2921 if (msize + offset > buffer_size)
2923 /* malformed message, header says it is larger than what
2924 would fit into the overall buffer */
2925 GNUNET_break_op (0);
2928 #if HAVE_UNALIGNED_64_ACCESS
2929 need_align = (0 != offset % 4) ? GNUNET_YES : GNUNET_NO;
2931 need_align = (0 != offset % 8) ? GNUNET_YES : GNUNET_NO;
2933 if (GNUNET_YES == need_align)
2934 align_and_deliver (sender, &buffer[offset], msize);
2936 deliver_message (sender,
2937 (const struct GNUNET_MessageHeader *)
2938 &buffer[offset], msize);
2945 * We received an encrypted message. Decrypt, validate and
2946 * pass on to the appropriate clients.
2949 handle_encrypted_message (struct Neighbour *n,
2950 const struct EncryptedMessage *m)
2952 size_t size = ntohs (m->header.size);
2954 struct EncryptedMessage *pt; /* plaintext */
2958 struct GNUNET_TIME_Absolute t;
2962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2963 "Core service receives `%s' request from `%4s'.\n",
2964 "ENCRYPTED_MESSAGE", GNUNET_i2s (&n->peer));
2966 GNUNET_CRYPTO_hash (&m->iv_seed, sizeof (uint32_t), &iv);
2972 &buf[ENCRYPTED_HEADER_SIZE],
2973 size - ENCRYPTED_HEADER_SIZE))
2975 pt = (struct EncryptedMessage *) buf;
2978 GNUNET_CRYPTO_hash (&pt->sequence_number,
2979 size - ENCRYPTED_HEADER_SIZE - sizeof (GNUNET_HashCode), &ph);
2980 if (0 != memcmp (&ph,
2981 &pt->plaintext_hash,
2982 sizeof (GNUNET_HashCode)))
2984 /* checksum failed */
2985 GNUNET_break_op (0);
2989 /* validate sequence number */
2990 snum = ntohl (pt->sequence_number);
2991 if (n->last_sequence_number_received == snum)
2993 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2994 "Received duplicate message, ignoring.\n");
2995 /* duplicate, ignore */
2998 if ((n->last_sequence_number_received > snum) &&
2999 (n->last_sequence_number_received - snum > 32))
3001 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3002 "Received ancient out of sequence message, ignoring.\n");
3003 /* ancient out of sequence, ignore */
3006 if (n->last_sequence_number_received > snum)
3008 unsigned int rotbit =
3009 1 << (n->last_sequence_number_received - snum - 1);
3010 if ((n->last_packets_bitmap & rotbit) != 0)
3012 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3013 "Received duplicate message, ignoring.\n");
3014 /* duplicate, ignore */
3017 n->last_packets_bitmap |= rotbit;
3019 if (n->last_sequence_number_received < snum)
3021 n->last_packets_bitmap <<= (snum - n->last_sequence_number_received);
3022 n->last_sequence_number_received = snum;
3025 /* check timestamp */
3026 t = GNUNET_TIME_absolute_ntoh (pt->timestamp);
3027 if (GNUNET_TIME_absolute_get_duration (t).value > MAX_MESSAGE_AGE.value)
3029 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3031 ("Message received far too old (%llu ms). Content ignored.\n"),
3032 GNUNET_TIME_absolute_get_duration (t).value);
3036 /* process decrypted message(s) */
3037 #if DEBUG_CORE_QUOTA
3038 if (n->bw_out_external_limit.value__ != pt->inbound_bw_limit.value__)
3039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3040 "Received %u b/s as new inbound limit for peer `%4s'\n",
3041 (unsigned int) ntohl (pt->inbound_bw_limit.value__),
3042 GNUNET_i2s (&n->peer));
3044 n->bw_out_external_limit = pt->inbound_bw_limit;
3045 n->bw_out = GNUNET_BANDWIDTH_value_min (n->bw_out_external_limit,
3046 n->bw_out_internal_limit);
3047 GNUNET_BANDWIDTH_tracker_update_quota (&n->available_send_window,
3049 n->last_activity = GNUNET_TIME_absolute_get ();
3050 off = sizeof (struct EncryptedMessage);
3051 deliver_messages (n, buf, size, off);
3056 * Function called by the transport for each received message.
3058 * @param cls closure
3059 * @param peer (claimed) identity of the other peer
3060 * @param message the message
3061 * @param latency estimated latency for communicating with the
3062 * given peer (round-trip)
3063 * @param distance in overlay hops, as given by transport plugin
3066 handle_transport_receive (void *cls,
3067 const struct GNUNET_PeerIdentity *peer,
3068 const struct GNUNET_MessageHeader *message,
3069 struct GNUNET_TIME_Relative latency,
3070 unsigned int distance)
3072 struct Neighbour *n;
3073 struct GNUNET_TIME_Absolute now;
3079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3080 "Received message of type %u from `%4s', demultiplexing.\n",
3081 ntohs (message->type), GNUNET_i2s (peer));
3083 n = find_neighbour (peer);
3085 n = create_neighbour (peer);
3088 n->last_latency = latency;
3089 n->last_distance = distance;
3090 up = (n->status == PEER_STATE_KEY_CONFIRMED);
3091 type = ntohs (message->type);
3092 size = ntohs (message->size);
3095 "Received message of type %u from `%4s'\n",
3101 case GNUNET_MESSAGE_TYPE_CORE_SET_KEY:
3102 if (size != sizeof (struct SetKeyMessage))
3104 GNUNET_break_op (0);
3107 handle_set_key (n, (const struct SetKeyMessage *) message);
3109 case GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE:
3110 if (size < sizeof (struct EncryptedMessage) +
3111 sizeof (struct GNUNET_MessageHeader))
3113 GNUNET_break_op (0);
3116 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
3117 (n->status != PEER_STATE_KEY_CONFIRMED))
3119 GNUNET_break_op (0);
3122 handle_encrypted_message (n, (const struct EncryptedMessage *) message);
3124 case GNUNET_MESSAGE_TYPE_CORE_PING:
3125 if (size != sizeof (struct PingMessage))
3127 GNUNET_break_op (0);
3130 if ((n->status != PEER_STATE_KEY_RECEIVED) &&
3131 (n->status != PEER_STATE_KEY_CONFIRMED))
3134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3135 "Core service receives `%s' request from `%4s' but have not processed key; marking as pending.\n",
3136 "PING", GNUNET_i2s (&n->peer));
3138 GNUNET_free_non_null (n->pending_ping);
3139 n->pending_ping = GNUNET_malloc (sizeof (struct PingMessage));
3140 memcpy (n->pending_ping, message, sizeof (struct PingMessage));
3143 handle_ping (n, (const struct PingMessage *) message);
3145 case GNUNET_MESSAGE_TYPE_CORE_PONG:
3146 if (size != sizeof (struct PongMessage))
3148 GNUNET_break_op (0);
3151 if ( (n->status != PEER_STATE_KEY_RECEIVED) &&
3152 (n->status != PEER_STATE_KEY_CONFIRMED) )
3155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3156 "Core service receives `%s' request from `%4s' but have not processed key; marking as pending.\n",
3157 "PONG", GNUNET_i2s (&n->peer));
3159 GNUNET_free_non_null (n->pending_pong);
3160 n->pending_pong = GNUNET_malloc (sizeof (struct PongMessage));
3161 memcpy (n->pending_pong, message, sizeof (struct PongMessage));
3164 handle_pong (n, (const struct PongMessage *) message);
3167 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3168 _("Unsupported message of type %u received.\n"), type);
3171 if (n->status == PEER_STATE_KEY_CONFIRMED)
3173 now = GNUNET_TIME_absolute_get ();
3174 n->last_activity = now;
3176 n->time_established = now;
3182 * Function that recalculates the bandwidth quota for the
3183 * given neighbour and transmits it to the transport service.
3185 * @param cls neighbour for the quota update
3189 neighbour_quota_update (void *cls,
3190 const struct GNUNET_SCHEDULER_TaskContext *tc)
3192 struct Neighbour *n = cls;
3193 struct GNUNET_BANDWIDTH_Value32NBO q_in;
3196 unsigned long long distributable;
3197 uint64_t need_per_peer;
3198 uint64_t need_per_second;
3200 n->quota_update_task = GNUNET_SCHEDULER_NO_TASK;
3201 /* calculate relative preference among all neighbours;
3202 divides by a bit more to avoid division by zero AND to
3203 account for possibility of new neighbours joining any time
3204 AND to convert to double... */
3205 if (preference_sum == 0)
3207 pref_rel = 1.0 / (double) neighbour_count;
3211 pref_rel = n->current_preference / preference_sum;
3213 need_per_peer = GNUNET_BANDWIDTH_value_get_available_until (MIN_BANDWIDTH_PER_PEER,
3214 GNUNET_TIME_UNIT_SECONDS);
3215 need_per_second = need_per_peer * neighbour_count;
3217 if (bandwidth_target_out_bps > need_per_second)
3218 distributable = bandwidth_target_out_bps - need_per_second;
3219 share = distributable * pref_rel;
3220 if (share + need_per_peer > ( (uint32_t)-1))
3221 q_in = GNUNET_BANDWIDTH_value_init ((uint32_t) -1);
3223 q_in = GNUNET_BANDWIDTH_value_init (need_per_peer + (uint32_t) share);
3224 /* check if we want to disconnect for good due to inactivity */
3225 if ( (GNUNET_TIME_absolute_get_duration (n->last_activity).value > GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.value) &&
3226 (GNUNET_TIME_absolute_get_duration (n->time_established).value > GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.value) )
3229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3230 "Forcing disconnect of `%4s' due to inactivity (?).\n",
3231 GNUNET_i2s (&n->peer));
3233 q_in = GNUNET_BANDWIDTH_value_init (0); /* force disconnect */
3235 #if DEBUG_CORE_QUOTA
3236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3237 "Current quota for `%4s' is %u/%llu b/s in (old: %u b/s) / %u out (%u internal)\n",
3238 GNUNET_i2s (&n->peer),
3239 (unsigned int) ntohl (q_in.value__),
3240 bandwidth_target_out_bps,
3241 (unsigned int) ntohl (n->bw_in.value__),
3242 (unsigned int) ntohl (n->bw_out.value__),
3243 (unsigned int) ntohl (n->bw_out_internal_limit.value__));
3245 if (n->bw_in.value__ != q_in.value__)
3248 GNUNET_TRANSPORT_set_quota (transport,
3252 GNUNET_TIME_UNIT_FOREVER_REL,
3255 schedule_quota_update (n);
3260 * Function called by transport to notify us that
3261 * a peer connected to us (on the network level).
3263 * @param cls closure
3264 * @param peer the peer that connected
3265 * @param latency current latency of the connection
3266 * @param distance in overlay hops, as given by transport plugin
3269 handle_transport_notify_connect (void *cls,
3270 const struct GNUNET_PeerIdentity *peer,
3271 struct GNUNET_TIME_Relative latency,
3272 unsigned int distance)
3274 struct Neighbour *n;
3275 struct GNUNET_TIME_Absolute now;
3276 struct ConnectNotifyMessage cnm;
3278 n = find_neighbour (peer);
3281 if (n->is_connected)
3283 /* duplicate connect notification!? */
3290 n = create_neighbour (peer);
3292 now = GNUNET_TIME_absolute_get ();
3293 n->is_connected = GNUNET_YES;
3294 n->last_latency = latency;
3295 n->last_distance = distance;
3296 GNUNET_BANDWIDTH_tracker_init (&n->available_send_window,
3299 GNUNET_BANDWIDTH_tracker_init (&n->available_recv_window,
3303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3304 "Received connection from `%4s'.\n",
3305 GNUNET_i2s (&n->peer));
3307 cnm.header.size = htons (sizeof (struct ConnectNotifyMessage));
3308 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_PRE_CONNECT);
3309 cnm.distance = htonl (n->last_distance);
3310 cnm.latency = GNUNET_TIME_relative_hton (n->last_latency);
3312 send_to_all_clients (&cnm.header, GNUNET_YES, GNUNET_CORE_OPTION_SEND_PRE_CONNECT);
3318 * Function called by transport telling us that a peer
3321 * @param cls closure
3322 * @param peer the peer that disconnected
3325 handle_transport_notify_disconnect (void *cls,
3326 const struct GNUNET_PeerIdentity *peer)
3328 struct DisconnectNotifyMessage cnm;
3329 struct Neighbour *n;
3332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3333 "Peer `%4s' disconnected from us.\n", GNUNET_i2s (peer));
3335 n = find_neighbour (peer);
3341 GNUNET_break (n->is_connected);
3342 cnm.header.size = htons (sizeof (struct DisconnectNotifyMessage));
3343 cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT);
3345 send_to_all_clients (&cnm.header, GNUNET_YES, GNUNET_CORE_OPTION_SEND_DISCONNECT);
3346 n->is_connected = GNUNET_NO;
3351 * Last task run during shutdown. Disconnects us from
3355 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3357 struct Neighbour *n;
3361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3362 "Core service shutting down.\n");
3364 GNUNET_assert (transport != NULL);
3365 GNUNET_TRANSPORT_disconnect (transport);
3367 while (NULL != (n = neighbours))
3369 neighbours = n->next;
3370 GNUNET_assert (neighbour_count > 0);
3374 GNUNET_SERVER_notification_context_destroy (notifier);
3376 while (NULL != (c = clients))
3377 handle_client_disconnect (NULL, c->client_handle);
3378 if (my_private_key != NULL)
3379 GNUNET_CRYPTO_rsa_key_free (my_private_key);
3384 * Initiate core service.
3386 * @param cls closure
3387 * @param s scheduler to use
3388 * @param serv the initialized server
3389 * @param c configuration to use
3393 struct GNUNET_SCHEDULER_Handle *s,
3394 struct GNUNET_SERVER_Handle *serv,
3395 const struct GNUNET_CONFIGURATION_Handle *c)
3401 /* parse configuration */
3404 GNUNET_CONFIGURATION_get_value_number (c,
3407 &bandwidth_target_in_bps)) ||
3409 GNUNET_CONFIGURATION_get_value_number (c,
3412 &bandwidth_target_out_bps)) ||
3414 GNUNET_CONFIGURATION_get_value_filename (c,
3416 "HOSTKEY", &keyfile)))
3418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3420 ("Core service is lacking key configuration settings. Exiting.\n"));
3421 GNUNET_SCHEDULER_shutdown (s);
3424 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
3425 GNUNET_free (keyfile);
3426 if (my_private_key == NULL)
3428 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3429 _("Core service could not access hostkey. Exiting.\n"));
3430 GNUNET_SCHEDULER_shutdown (s);
3433 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
3434 GNUNET_CRYPTO_hash (&my_public_key,
3435 sizeof (my_public_key), &my_identity.hashPubKey);
3436 /* setup notification */
3438 notifier = GNUNET_SERVER_notification_context_create (server,
3440 GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
3441 /* setup transport connection */
3442 transport = GNUNET_TRANSPORT_connect (sched,
3445 &handle_transport_receive,
3446 &handle_transport_notify_connect,
3447 &handle_transport_notify_disconnect);
3448 GNUNET_assert (NULL != transport);
3449 GNUNET_SCHEDULER_add_delayed (sched,
3450 GNUNET_TIME_UNIT_FOREVER_REL,
3451 &cleaning_task, NULL);
3452 /* process client requests */
3453 GNUNET_SERVER_add_handlers (server, handlers);
3454 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3455 _("Core service of `%4s' ready.\n"), GNUNET_i2s (&my_identity));
3461 * The main function for the transport service.
3463 * @param argc number of arguments from the command line
3464 * @param argv command line arguments
3465 * @return 0 ok, 1 on error
3468 main (int argc, char *const *argv)
3470 return (GNUNET_OK ==
3471 GNUNET_SERVICE_run (argc,
3474 GNUNET_SERVICE_OPTION_NONE,
3475 &run, NULL)) ? 0 : 1;
3478 /* end of gnunet-service-core.c */