+/**
+ * This function is called whenever an encrypted HELLO message is
+ * received.
+ *
+ * @param cls closure
+ * @param other the other peer involved (sender or receiver, NULL
+ * for loopback messages where we are both sender and receiver)
+ * @param message the actual HELLO message
+ * @param atsi performance data
+ * @return GNUNET_OK to keep the connection open,
+ * GNUNET_SYSERR to close it (signal serious error)
+ */
+static int
+handle_encrypted_hello (void *cls,
+ const struct GNUNET_PeerIdentity * other,
+ const struct GNUNET_MessageHeader *
+ message,
+ const struct GNUNET_TRANSPORT_ATS_Information *atsi)
+{
+ struct Peer *peer;
+ struct GNUNET_PeerIdentity pid;
+
+#if DEBUG_TOPOLOGY
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Received encrypted `%s' from peer `%s'",
+ "HELLO",
+ GNUNET_i2s (other));
+#endif
+ if (GNUNET_OK !=
+ GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message*) message,
+ &pid))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+ GNUNET_STATISTICS_update (stats,
+ gettext_noop ("# HELLO messages received"),
+ 1,
+ GNUNET_NO);
+ peer = GNUNET_CONTAINER_multihashmap_get (peers,
+ &pid.hashPubKey);
+ if (peer == NULL)
+ {
+ if ( (GNUNET_YES == friends_only) ||
+ (friend_count < minimum_friend_count) )
+ return GNUNET_OK;
+ }
+ else
+ {
+ if ( (GNUNET_YES != peer->is_friend) &&
+ (GNUNET_YES == friends_only) )
+ return GNUNET_OK;
+ if ( (GNUNET_YES != peer->is_friend) &&
+ (friend_count < minimum_friend_count) )
+ return GNUNET_OK;
+ }
+ if (transport != NULL)
+ GNUNET_TRANSPORT_offer_hello (transport,
+ message);
+ return GNUNET_OK;
+}
+
+
+/**
+ * Function to fill send buffer with HELLO.
+ *
+ * @param cls 'struct Peer' of the target peer
+ * @param size number of bytes available in buf
+ * @param buf where the callee should write the message
+ * @return number of bytes written to buf
+ */
+static size_t
+hello_advertising_ready (void *cls,
+ size_t size,
+ void *buf)
+{
+ struct Peer *pl = cls;
+ struct FindAdvHelloContext fah;
+ size_t want;
+
+ pl->hello_req = NULL;
+ /* find applicable HELLOs */
+ fah.peer = pl;
+ fah.result = NULL;
+ fah.max_size = size;
+ fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
+ GNUNET_CONTAINER_multihashmap_iterate (peers,
+ &find_advertisable_hello,
+ &fah);
+ want = 0;
+ if (fah.result != NULL)
+ {
+ want = GNUNET_HELLO_size (fah.result->hello);
+ GNUNET_assert (want <= size);
+ memcpy (buf, fah.result->hello, want);
+ GNUNET_CONTAINER_bloomfilter_add (fah.result->filter,
+ &pl->pid.hashPubKey);
+#if DEBUG_TOPOLOGY
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Sending `%s' with %u bytes",
+ "HELLO",
+ (unsigned int) want);
+#endif
+ GNUNET_STATISTICS_update (stats,
+ gettext_noop ("# HELLO messages gossipped"),
+ 1,
+ GNUNET_NO);
+ }
+ pl->next_hello_allowed = GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
+ pl->hello_delay_task
+ = GNUNET_SCHEDULER_add_now (&schedule_next_hello,
+ pl);
+ return want;
+}
+
+
+/**
+ * Last task run during shutdown. Disconnects us from
+ * the transport and core.
+ *
+ * @param cls unused, NULL
+ * @param tc scheduler context
+ */
+static void
+cleaning_task (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ if (NULL != peerinfo_notify)
+ {
+ GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
+ peerinfo_notify = NULL;
+ }
+ GNUNET_TRANSPORT_disconnect (transport);
+ transport = NULL;
+ GNUNET_CONTAINER_multihashmap_iterate (peers,
+ &free_peer,
+ NULL);
+ GNUNET_CONTAINER_multihashmap_destroy (peers);
+ if (handle != NULL)
+ {
+ GNUNET_CORE_disconnect (handle);
+ handle = NULL;
+ }
+ whitelist_peers ();
+ if (stats != NULL)
+ {
+ GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
+ stats = NULL;
+ }
+}
+
+