-remove trailing whitespace
[oweals/gnunet.git] / src / dns / gnunet-service-dns.c
index 5db14c56a3afb28d5533e03c9922a8577c409539..53c19d01cdacbb2e7c638ade8c7f3314c973224b 100644 (file)
@@ -34,7 +34,7 @@
  * each request will be given one of 128 random source ports, and the
  * 128 random source ports will also change "often" (less often if the
  * system is very busy, each time if we are mostly idle).  At the same
- * time, the system will never use more than 256 UDP sockets.  
+ * time, the system will never use more than 256 UDP sockets.
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_dns_service.h"
 #include "gnunet_dnsparser_lib.h"
 #include "gnunet_dnsstub_lib.h"
-#include "gnunet_mesh_service.h"
 #include "gnunet_statistics_service.h"
 #include "gnunet_tun_lib.h"
 
+/**
+ * Port number for DNS
+ */
+#define DNS_PORT 53
+
 
 /**
  * Generic logging shorthand
@@ -84,7 +88,7 @@ enum RequestPhase
    * Global Internet query is now pending.
    */
   RP_INTERNET_DNS,
-  
+
   /**
    * Client (or global DNS request) has resulted in a response.
    * Forward to all POST-RESOLUTION clients.  If client list is empty,
@@ -107,22 +111,22 @@ enum RequestPhase
 
 /**
  * Entry we keep for each client.
- */ 
+ */
 struct ClientRecord
 {
   /**
    * Kept in doubly-linked list.
-   */ 
+   */
   struct ClientRecord *next;
 
   /**
    * Kept in doubly-linked list.
-   */ 
+   */
   struct ClientRecord *prev;
 
   /**
    * Handle to the client.
-   */ 
+   */
   struct GNUNET_SERVER_Client *client;
 
   /**
@@ -135,7 +139,7 @@ struct ClientRecord
 
 /**
  * Entry we keep for each active request.
- */ 
+ */
 struct RequestRecord
 {
 
@@ -176,7 +180,7 @@ struct RequestRecord
 
   /**
    * Number of bytes in payload.
-   */ 
+   */
   size_t payload_length;
 
   /**
@@ -192,50 +196,6 @@ struct RequestRecord
 };
 
 
-/**
- * State we keep for each DNS tunnel that terminates at this node.
- */
-struct TunnelState
-{
-
-  /**
-   * Associated MESH tunnel.
-   */
-  struct GNUNET_MESH_Tunnel *tunnel;
-
-  /**
-   * Active request for sending a reply.
-   */
-  struct GNUNET_MESH_TransmitHandle *th;
-
-  /**
-   * DNS reply ready for transmission.
-   */
-  char *reply;
-
-  /**
-   * Socket we are using to transmit this request (must match if we receive
-   * a response).
-   */
-  struct GNUNET_DNSSTUB_RequestSocket *rs;
-
-  /**
-   * Number of bytes in 'reply'.
-   */
-  size_t reply_length;
-
-  /**
-   * Original DNS request ID as used by the client.
-   */
-  uint16_t original_id;
-
-  /**
-   * DNS request ID that we used for forwarding.
-   */
-  uint16_t my_id;
-};
-
-
 /**
  * Global return value from 'main'.
  */
@@ -276,11 +236,6 @@ static struct ClientRecord *clients_tail;
  */
 static struct GNUNET_SERVER_NotificationContext *nc;
 
-/**
- * Array of all open requests from tunnels.
- */
-static struct TunnelState *tunnels[UINT16_MAX + 1];
-
 /**
  * Array of all open requests.
  */
@@ -291,12 +246,6 @@ static struct RequestRecord requests[UINT16_MAX + 1];
  */
 static uint64_t request_id_gen;
 
-/**
- * Handle to the MESH service (for receiving DNS queries), or NULL 
- * if we are not a DNS exit.
- */
-static struct GNUNET_MESH_Handle *mesh;
-
 /**
  * Handle to the DNS Stub resolver.
  */
@@ -332,7 +281,7 @@ cleanup_task (void *cls GNUNET_UNUSED,
 {
   unsigned int i;
 
-  GNUNET_HELPER_stop (hijacker);
+  GNUNET_HELPER_stop (hijacker, GNUNET_NO);
   hijacker = NULL;
   for (i=0;i<7;i++)
     GNUNET_free_non_null (helper_argv[i]);
@@ -350,11 +299,6 @@ cleanup_task (void *cls GNUNET_UNUSED,
     GNUNET_DNSSTUB_stop (dnsstub);
     dnsstub = NULL;
   }
-  if (NULL != mesh)
-  {
-    GNUNET_MESH_disconnect(mesh);
-    mesh = NULL;
-  }
 }
 
 
@@ -373,7 +317,7 @@ request_done (struct RequestRecord *rr)
 
   GNUNET_array_grow (rr->client_wait_list,
                     rr->client_wait_list_length,
-                    0); 
+                    0);
   if (RP_RESPONSE_MONITOR != rr->phase)
   {
     /* no response, drop */
@@ -386,7 +330,7 @@ request_done (struct RequestRecord *rr)
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Transmitting response for request %llu\n",
-       (unsigned long long) rr->request_id);  
+       (unsigned long long) rr->request_id);
   /* send response via hijacker */
   reply_len = sizeof (struct GNUNET_MessageHeader);
   reply_len += sizeof (struct GNUNET_TUN_Layer2PacketHeader);
@@ -401,7 +345,7 @@ request_done (struct RequestRecord *rr)
   default:
     GNUNET_break (0);
     cleanup_rr (rr);
-    return;   
+    return;
   }
   reply_len += sizeof (struct GNUNET_TUN_UdpHeader);
   reply_len += rr->payload_length;
@@ -410,7 +354,7 @@ request_done (struct RequestRecord *rr)
     /* response too big, drop */
     GNUNET_break (0); /* how can this be? */
     cleanup_rr(rr);
-    return;    
+    return;
   }
   {
     char buf[reply_len] GNUNET_ALIGN;
@@ -430,7 +374,7 @@ request_done (struct RequestRecord *rr)
 
       tun.flags = htons (0);
       if (rr->src_addr.ss_family == AF_INET)
-       tun.proto = htons (ETH_P_IPV4); 
+       tun.proto = htons (ETH_P_IPV4);
       else
        tun.proto = htons (ETH_P_IPV6);
       memcpy (&buf[off], &tun, sizeof (struct GNUNET_TUN_Layer2PacketHeader));
@@ -539,14 +483,14 @@ send_request_to_client (struct RequestRecord *rr,
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Sending information about request %llu to local client\n",
-       (unsigned long long) rr->request_id);  
+       (unsigned long long) rr->request_id);
   req = (struct GNUNET_DNS_Request*) buf;
   req->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST);
   req->header.size = htons (sizeof (buf));
   req->reserved = htonl (0);
   req->request_id = rr->request_id;
   memcpy (&req[1], rr->payload, rr->payload_length);
-  GNUNET_SERVER_notification_context_unicast (nc, 
+  GNUNET_SERVER_notification_context_unicast (nc,
                                              client,
                                              &req->header,
                                              GNUNET_NO);
@@ -597,8 +541,8 @@ next_phase (struct RequestRecord *rr)
       nz = (int) j;
       break;
     }
-  }  
-  if (-1 != nz) 
+  }
+  if (-1 != nz)
   {
     send_request_to_client (rr, rr->client_wait_list[nz]->client);
     return;
@@ -607,7 +551,7 @@ next_phase (struct RequestRecord *rr)
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Request %llu now in phase %d\n",
        rr->request_id,
-       rr->phase);  
+       rr->phase);
   switch (rr->phase)
   {
   case RP_INIT:
@@ -703,7 +647,7 @@ next_phase (struct RequestRecord *rr)
  *
  * @param cls unused
  * @param client handle of client that disconnected
- */ 
+ */
 static void
 client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
 {
@@ -730,7 +674,7 @@ client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
          if (rr->client_wait_list[j] == cr)
          {
            rr->client_wait_list[j] = NULL;
-           next_phase (rr); 
+           next_phase (rr);
          }
        }
       }
@@ -741,53 +685,6 @@ client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
 }
 
 
-/**
- * We got a reply from DNS for a request of a MESH tunnel.  Send it
- * via the tunnel (after changing the request ID back).
- *
- * @param cls the 'struct TunnelState'
- * @param size number of bytes available in buf
- * @param buf where to copy the reply
- * @return number of bytes written to buf
- */
-static size_t
-transmit_reply_to_mesh (void *cls,
-                       size_t size,
-                       void *buf)
-{
-  struct TunnelState *ts = cls;
-  size_t off;
-  size_t ret;
-  char *cbuf = buf;
-  struct GNUNET_MessageHeader hdr;
-  struct GNUNET_TUN_DnsHeader dns;
-
-  ts->th = NULL;
-  GNUNET_assert (ts->reply != NULL);
-  if (size == 0)
-    return 0;
-  ret = sizeof (struct GNUNET_MessageHeader) + ts->reply_length; 
-  GNUNET_assert (ret <= size);
-  hdr.size = htons (ret);
-  hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET);
-  memcpy (&dns, ts->reply, sizeof (dns));
-  dns.id = ts->original_id;
-  off = 0;
-  memcpy (&cbuf[off], &hdr, sizeof (hdr));
-  off += sizeof (hdr);
-  memcpy (&cbuf[off], &dns, sizeof (dns));
-  off += sizeof (dns);
-  memcpy (&cbuf[off], &ts->reply[sizeof (dns)], ts->reply_length - sizeof (dns));
-  off += ts->reply_length - sizeof (dns);
-  GNUNET_free (ts->reply);
-  ts->reply = NULL;
-  ts->reply_length = 0;  
-  GNUNET_assert (ret == off);
-  return ret;
-}
-
-
-
 /**
  * Callback called from DNSSTUB resolver when a resolution
  * succeeded.
@@ -804,50 +701,21 @@ process_dns_result (void *cls,
                    size_t r)
 {
   struct RequestRecord *rr;
-  struct TunnelState *ts;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Processing DNS result from stub resolver\n");
   GNUNET_assert (NULL == cls);
-  /* Handle case that this is a reply to a request from a MESH DNS tunnel */
-  ts = tunnels[dns->id];
-  if ( (NULL == ts) ||
-       (ts->rs != rs) )
-    ts = NULL; /* DNS responder address missmatch */
-  if (NULL != ts)
-  {
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-        "Got a response from the stub resolver for DNS request received via MESH!\n");
-    tunnels[dns->id] = NULL;
-    GNUNET_free_non_null (ts->reply);
-    ts->reply = GNUNET_malloc (r);
-    ts->reply_length = r;
-    memcpy (ts->reply, dns, r);
-    if (NULL != ts->th)
-      GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
-    ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel,
-                                               GNUNET_NO,
-                                               GNUNET_TIME_UNIT_FOREVER_REL,
-                                               NULL,
-                                               sizeof (struct GNUNET_MessageHeader) + r,
-                                               &transmit_reply_to_mesh,
-                                               ts);
-  }
-  /* Handle case that this is a reply to a local request (intercepted from TUN interface) */
   rr = &requests[dns->id];
   if ( (rr->phase != RP_INTERNET_DNS) ||
        (rr->rs != rs) )
   {
-    if (NULL == ts)
-    {
-      /* unexpected / bogus reply */
-      GNUNET_STATISTICS_update (stats,
-                               gettext_noop ("# External DNS response discarded (no matching request)"),
-                               1, GNUNET_NO);
-    }
+    /* unexpected / bogus reply */
+    GNUNET_STATISTICS_update (stats,
+                             gettext_noop ("# External DNS response discarded (no matching request)"),
+                             1, GNUNET_NO);
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Received DNS reply that does not match any pending request.  Dropping.\n"); 
-    return; 
+               "Received DNS reply that does not match any pending request.  Dropping.\n");
+    return;
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Got a response from the stub resolver for DNS request %llu intercepted locally!\n",
@@ -868,7 +736,7 @@ process_dns_result (void *cls,
  * @param message the init message (unused)
  */
 static void
-handle_client_init (void *cls GNUNET_UNUSED, 
+handle_client_init (void *cls GNUNET_UNUSED,
                    struct GNUNET_SERVER_Client *client,
                    const struct GNUNET_MessageHeader *message)
 {
@@ -877,7 +745,7 @@ handle_client_init (void *cls GNUNET_UNUSED,
 
   cr = GNUNET_malloc (sizeof (struct ClientRecord));
   cr->client = client;
-  cr->flags = (enum GNUNET_DNS_Flags) ntohl (reg->flags);  
+  cr->flags = (enum GNUNET_DNS_Flags) ntohl (reg->flags);
   GNUNET_SERVER_client_keep (client);
   GNUNET_CONTAINER_DLL_insert (clients_head,
                               clients_tail,
@@ -895,7 +763,7 @@ handle_client_init (void *cls GNUNET_UNUSED,
  * @param message the response
  */
 static void
-handle_client_response (void *cls GNUNET_UNUSED, 
+handle_client_response (void *cls GNUNET_UNUSED,
                        struct GNUNET_SERVER_Client *client,
                        const struct GNUNET_MessageHeader *message)
 {
@@ -948,7 +816,7 @@ handle_client_response (void *cls GNUNET_UNUSED,
       {
        GNUNET_break (0);
        GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
-       next_phase (rr); 
+       next_phase (rr);
        return;
       }
       GNUNET_free_non_null (rr->payload);
@@ -967,7 +835,7 @@ handle_client_response (void *cls GNUNET_UNUSED,
       /* if query changed to answer, move past DNS resolution phase... */
       if ( (RP_QUERY == rr->phase) &&
           (rr->payload_length > sizeof (struct GNUNET_TUN_DnsHeader)) &&
-          ((struct GNUNET_DNSPARSER_Flags*)&(((struct GNUNET_TUN_DnsHeader*) rr->payload)->flags))->query_or_response == 1)
+          ((struct GNUNET_TUN_DnsFlags*)&(((struct GNUNET_TUN_DnsHeader*) rr->payload)->flags))->query_or_response == 1)
       {
        rr->phase = RP_INTERNET_DNS;
        GNUNET_array_grow (rr->client_wait_list,
@@ -976,9 +844,9 @@ handle_client_response (void *cls GNUNET_UNUSED,
       }
       break;
     }
-    next_phase (rr); 
+    next_phase (rr);
     GNUNET_SERVER_receive_done (client, GNUNET_OK);
-    return;      
+    return;
   }
   /* odd, client was not on our list for the request, that ought
      to be an error */
@@ -1066,8 +934,9 @@ process_helper_messages (void *cls GNUNET_UNUSED, void *client,
                ntohs (tun->proto));
     return GNUNET_OK;
   }
-  if (msize <= sizeof (struct GNUNET_TUN_UdpHeader) + sizeof (struct GNUNET_TUN_DnsHeader))
-  {    
+  if ( (msize <= sizeof (struct GNUNET_TUN_UdpHeader) + sizeof (struct GNUNET_TUN_DnsHeader)) ||
+       (DNS_PORT != ntohs (udp->destination_port)) )
+  {
     /* non-DNS packet received on TUN, ignore */
     GNUNET_STATISTICS_update (stats,
                              gettext_noop ("# Non-DNS UDP packet received via TUN interface"),
@@ -1145,109 +1014,6 @@ process_helper_messages (void *cls GNUNET_UNUSED, void *client,
 }
 
 
-/**
- * Process a request via mesh to perform a DNS query.
- *
- * @param cls closure, NULL
- * @param tunnel connection to the other end
- * @param tunnel_ctx pointer to our 'struct TunnelState *'
- * @param sender who sent the message
- * @param message the actual message
- * @param atsi performance data for the connection
- * @return GNUNET_OK to keep the connection open,
- *         GNUNET_SYSERR to close it (signal serious error)
- */
-static int
-receive_dns_request (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
-                     void **tunnel_ctx,
-                     const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
-                     const struct GNUNET_MessageHeader *message,
-                     const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
-{
-  struct TunnelState *ts = *tunnel_ctx;
-  const struct GNUNET_TUN_DnsHeader *dns;
-  size_t mlen = ntohs (message->size);
-  size_t dlen = mlen - sizeof (struct GNUNET_MessageHeader);
-  char buf[dlen] GNUNET_ALIGN;
-  struct GNUNET_TUN_DnsHeader *dout;
-  if (dlen < sizeof (struct GNUNET_TUN_DnsHeader))
-  {
-    GNUNET_break_op (0);
-    return GNUNET_SYSERR;
-  }
-  dns = (const struct GNUNET_TUN_DnsHeader *) &message[1];
-  ts->original_id = dns->id;
-  if (tunnels[ts->my_id] == ts)
-    tunnels[ts->my_id] = NULL;
-  ts->my_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 
-                                                  UINT16_MAX + 1);
-  tunnels[ts->my_id] = ts;
-  memcpy (buf, dns, dlen);
-  dout = (struct GNUNET_TUN_DnsHeader *) buf;
-  dout->id = ts->my_id;
-  ts->rs = GNUNET_DNSSTUB_resolve2 (dnsstub,
-                                   buf, dlen,
-                                   &process_dns_result,
-                                   NULL);
-  if (NULL == ts->rs)
-    return GNUNET_SYSERR;
-  return GNUNET_OK;
-}
-
-
-/**
- * Callback from GNUNET_MESH for new tunnels.
- *
- * @param cls closure
- * @param tunnel new handle to the tunnel
- * @param initiator peer that started the tunnel
- * @param ats performance information for the tunnel
- * @return initial tunnel context for the tunnel
- */
-static void *
-accept_dns_tunnel (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
-                  const struct GNUNET_PeerIdentity *initiator GNUNET_UNUSED,
-                  const struct GNUNET_ATS_Information *ats GNUNET_UNUSED)
-{
-  struct TunnelState *ts = GNUNET_malloc (sizeof (struct TunnelState));
-
-  GNUNET_STATISTICS_update (stats,
-                           gettext_noop ("# Inbound MESH tunnels created"),
-                           1, GNUNET_NO);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Received inbound tunnel from `%s'\n",
-             GNUNET_i2s (initiator));
-  ts->tunnel = tunnel;
-  return ts;
-}
-
-
-/**
- * Function called by mesh whenever an inbound tunnel is destroyed.
- * Should clean up any associated state.
- *
- * @param cls closure (set from GNUNET_MESH_connect)
- * @param tunnel connection to the other end (henceforth invalid)
- * @param tunnel_ctx place where local state associated
- *                   with the tunnel is stored
- */
-static void
-destroy_dns_tunnel (void *cls GNUNET_UNUSED, 
-                   const struct GNUNET_MESH_Tunnel *tunnel,
-                   void *tunnel_ctx)
-{
-  struct TunnelState *ts = tunnel_ctx;
-
-  if (tunnels[ts->my_id] == ts)
-    tunnels[ts->my_id] = NULL;
-  if (NULL != ts->th)
-    GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
-  GNUNET_free_non_null (ts->reply);
-  GNUNET_free (ts);
-}
-
-
 /**
  * @param cls closure
  * @param server the initialized server
@@ -1259,7 +1025,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
 {
   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
     /* callback, cls, type, size */
-    {&handle_client_init, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT, 
+    {&handle_client_init, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT,
      sizeof (struct GNUNET_DNS_Register)},
     {&handle_client_response, NULL, GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE, 0},
     {NULL, NULL, 0, 0}
@@ -1277,7 +1043,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   cfg = cfg_;
   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-dns");
   if (GNUNET_YES !=
-      GNUNET_OS_check_helper_binary (binary))
+      GNUNET_OS_check_helper_binary (binary, GNUNET_YES, NULL)) // TODO: once we have a windows-testcase, add test parameters here
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("`%s' must be installed SUID, refusing to run\n"),
@@ -1292,17 +1058,15 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup_task,
                                 cls);
   dns_exit = NULL;
-  if ( (GNUNET_YES ==
-       GNUNET_CONFIGURATION_get_value_yesno (cfg_, "dns", "PROVIDE_EXIT")) &&
-       ( (GNUNET_OK !=
-         GNUNET_CONFIGURATION_get_value_string (cfg, "dns", 
+  if ( ( (GNUNET_OK !=
+         GNUNET_CONFIGURATION_get_value_string (cfg, "dns",
                                                 "DNS_EXIT",
                                                 &dns_exit)) ||
         ( (1 != inet_pton (AF_INET, dns_exit, &dns_exit4)) &&
           (1 != inet_pton (AF_INET6, dns_exit, &dns_exit6)) ) ) )
   {
     GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, "dns", "DNS_EXIT",
-               _("need a valid IPv4 or IPv6 address\n"));
+                              _("need a valid IPv4 or IPv6 address\n"));
     GNUNET_free_non_null (dns_exit);
     dns_exit = NULL;
   }
@@ -1359,24 +1123,6 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
   }
   helper_argv[5] = ipv4mask;
   helper_argv[6] = NULL;
-
-  if (NULL != dns_exit)
-  {
-    static struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
-      {&receive_dns_request, GNUNET_MESSAGE_TYPE_VPN_DNS_TO_INTERNET, 0},
-      {NULL, 0, 0}
-    };
-    static GNUNET_MESH_ApplicationType mesh_types[] = {
-      GNUNET_APPLICATION_TYPE_INTERNET_RESOLVER,
-      GNUNET_APPLICATION_TYPE_END
-    };
-    mesh = GNUNET_MESH_connect (cfg,
-                               NULL,
-                               &accept_dns_tunnel, 
-                               &destroy_dns_tunnel,
-                               mesh_handlers,
-                               mesh_types);
-  }
   hijacker = GNUNET_HELPER_start (GNUNET_NO,
                                  "gnunet-helper-dns",
                                  helper_argv,
@@ -1411,7 +1157,7 @@ main (int argc, char *const *argv)
             strerror (errno));
   }
   else if (sgid != rgid)
-  {    
+  {
     if (-1 ==  setregid (sgid, sgid))
       fprintf (stderr, "setregid failed: %s\n", strerror (errno));
   }