+/**
+ * Connect to the peerinfo service.
+ *
+ * @param cfg configuration to use
+ * @return NULL on error (configuration related, actual connection
+ * establishment may happen asynchronously).
+ */
+struct GNUNET_PEERINFO_Handle *
+GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+ struct GNUNET_CLIENT_Connection *client;
+ struct GNUNET_PEERINFO_Handle *ret;
+
+ client = GNUNET_CLIENT_connect ("peerinfo", cfg);
+ if (client == NULL)
+ return NULL;
+ ret = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_Handle));
+ ret->client = client;
+ ret->cfg = cfg;
+ return ret;
+}
+
+
+/**
+ * Disconnect from the peerinfo service. Note that all iterators must
+ * have completed or have been cancelled by the time this function is
+ * called (otherwise, calling this function is a serious error).
+ * Furthermore, if 'GNUNET_PEERINFO_add_peer' operations are still
+ * pending, they will be cancelled silently on disconnect.
+ *
+ * @param h handle to disconnect
+ */
+void
+GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
+{
+ struct TransmissionQueueEntry *tqe;
+
+ while (NULL != (tqe = h->tq_head))
+ {
+ GNUNET_CONTAINER_DLL_remove (h->tq_head,
+ h->tq_tail,
+ tqe);
+ if (tqe->cont != NULL)
+ tqe->cont (tqe->cont_cls, GNUNET_SYSERR);
+ GNUNET_free (tqe);
+ }
+ if (h->th != NULL)
+ {
+ GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
+ h->th = NULL;
+ }
+ GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
+ GNUNET_free (h);
+}
+
+
+/**
+ * Check if we have a request pending in the transmission queue and are
+ * able to transmit it right now. If so, schedule transmission.
+ *
+ * @param h handle to the service
+ */
+static void
+trigger_transmit (struct GNUNET_PEERINFO_Handle *h);
+
+
+/**
+ * Close the existing connection to PEERINFO and reconnect.
+ *
+ * @param h handle to the service
+ */
+static void
+reconnect (struct GNUNET_PEERINFO_Handle *h)
+{
+ GNUNET_CLIENT_disconnect (h->client, GNUNET_SYSERR);
+ h->th = NULL;
+ h->client = GNUNET_CLIENT_connect ("peerinfo", h->cfg);
+ GNUNET_assert (h->client != NULL);
+}
+
+
+/**
+ * Transmit the request at the head of the transmission queue
+ * and trigger continuation (if any).
+ *
+ * @param cls the 'struct GNUNET_PEERINFO_Handle' (with the queue)
+ * @param size size of the buffer (0 on error)
+ * @param buf where to copy the message
+ * @return number of bytes copied to buf
+ */