- struct GNUNET_DNS_Handle *h;
-
- h = GNUNET_malloc (sizeof (struct GNUNET_DNS_Handle));
- h->cfg = cfg;
- h->process_answer_cb = cb;
- h->process_answer_cb_cls = cb_cls;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to service-dns\n");
- h->dns_connection = GNUNET_CLIENT_connect ("dns", h->cfg);
- /* This would most likely be a misconfiguration */
- GNUNET_assert (NULL != h->dns_connection);
- GNUNET_CLIENT_receive (h->dns_connection,
- &dns_answer_handler, NULL,
- GNUNET_TIME_UNIT_FOREVER_REL);
- /* If a packet is already in the list, schedule to send it */
- if (h->dns_transmit_handle == NULL && h->head != NULL)
- h->dns_transmit_handle =
- GNUNET_CLIENT_notify_transmit_ready (h->dns_connection,
- ntohs (h->head->pkt.hdr.size),
- GNUNET_TIME_UNIT_FOREVER_REL,
- GNUNET_YES, &send_query, h);
- else if (h->dns_transmit_handle == NULL && h->restart_hijack == 1)
+ struct GNUNET_DNS_Handle *dh = cls;
+ struct ReplyQueueEntry *qe;
+ size_t len;
+
+ dh->dns_transmit_handle = NULL;
+ if (NULL == buf)
+ {
+ disconnect (dh);
+ dh->reconnect_task =
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
+ &reconnect, dh);
+ return 0;
+ }
+ qe = dh->rq_head;
+ if (NULL == qe)
+ return 0;
+ len = ntohs (qe->msg->size);
+ if (len > size)
+ {
+ dh->dns_transmit_handle =
+ GNUNET_CLIENT_notify_transmit_ready (dh->dns_connection,
+ len,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ GNUNET_NO,
+ &send_response, dh);
+ return 0;
+ }
+ memcpy (buf, qe->msg, len);
+ GNUNET_CONTAINER_DLL_remove (dh->rq_head,
+ dh->rq_tail,
+ qe);
+ GNUNET_free (qe);
+ if (GNUNET_NO == dh->in_receive)
+ {
+ dh->in_receive = GNUNET_YES;
+ GNUNET_CLIENT_receive (dh->dns_connection,
+ &request_handler, dh,
+ GNUNET_TIME_UNIT_FOREVER_REL);
+ }
+ if (NULL != (qe = dh->rq_head))
+ {
+ dh->dns_transmit_handle =
+ GNUNET_CLIENT_notify_transmit_ready (dh->dns_connection,
+ ntohs (qe->msg->size),
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ GNUNET_NO,
+ &send_response, dh);
+ }
+ return len;
+}
+
+
+/**
+ * Add the given reply to our transmission queue and trigger sending if needed.
+ *
+ * @param dh handle with the connection
+ * @param qe reply to queue
+ */
+static void
+queue_reply (struct GNUNET_DNS_Handle *dh,
+ struct ReplyQueueEntry *qe)
+{
+ if (NULL == dh->dns_connection)