-verbose
[oweals/gnunet.git] / src / gns / nss / nss_gns.c
index 0fe8a75bafa3f364482cc1cf7f13b4faa97239f4..82fe1e739cc813fc6a527127f6d73a50eeb7ab89 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 
-#include "query.h"
+#include "nss_gns_query.h"
 
-#ifdef NSS_IPV4_ONLY
-#define _nss_mdns_gethostbyname2_r _nss_gns4_minimal_gethostbyname2_r
-#define _nss_mdns_gethostbyname_r  _nss_gns4_minimal_gethostbyname_r
-#define _nss_mdns_gethostbyaddr_r  _nss_gns4_minimal_gethostbyaddr_r
-#endif
-
-#ifdef NSS_IPV6_ONLY
-#define _nss_mdns_gethostbyname2_r _nss_gns6_gethostbyname2_r
-#define _nss_mdns_gethostbyname_r  _nss_gns6_gethostbyname_r
-#define _nss_mdns_gethostbyaddr_r  _nss_gns6_gethostbyaddr_r
-#endif
-
-#ifndef NSS_IPV4_ONLY
-#ifndef NSS_IPV6_ONLY
-#define _nss_mdns_gethostbyname2_r _nss_gns_gethostbyname2_r
-#define _nss_mdns_gethostbyname_r  _nss_gns_gethostbyname_r
-#define _nss_mdns_gethostbyaddr_r  _nss_gns_gethostbyaddr_r
-#endif
-#endif
-
-/* Maximum number of entries to return */
-#define MAX_ENTRIES 16
+#include <arpa/inet.h>
 
+/** macro to align idx to 32bit boundary */
 #define ALIGN(idx) do { \
   if (idx % sizeof(void*)) \
     idx += (sizeof(void*) - idx % sizeof(void*)); /* Align on 32 bit boundary */ \
 } while(0)
 
-struct userdata {
-    int count;
-    int data_len; /* only valid when doing reverse lookup */
-    union  {
-        ipv4_address_t ipv4[MAX_ENTRIES];
-        ipv6_address_t ipv6[MAX_ENTRIES];
-        char *name[MAX_ENTRIES];
-    } data;
-};
-
-#ifndef NSS_IPV6_ONLY
-static void ipv4_callback(const ipv4_address_t *ipv4, void *userdata) {
-    struct userdata *u = userdata;
-    assert(ipv4 && userdata);
-
-    if (u->count >= MAX_ENTRIES)
-        return;
-
-    u->data.ipv4[u->count++] = *ipv4;
-    u->data_len += sizeof(ipv4_address_t);
-}
-#endif
-
-#ifndef NSS_IPV4_ONLY
-static void ipv6_callback(const ipv6_address_t *ipv6, void *userdata) {
-    struct userdata *u = userdata;
-    assert(ipv6 && userdata);
-
-    if (u->count >= MAX_ENTRIES)
-        return;
-
-    u->data.ipv6[u->count++] = *ipv6;
-    u->data_len += sizeof(ipv6_address_t);
-}
-#endif
-
-static void name_callback(const char*name, void *userdata) {
-    struct userdata *u = userdata;
-    assert(name && userdata);
-
-    if (u->count >= MAX_ENTRIES)
-        return;
-
-    u->data.name[u->count++] = strdup(name);
-    u->data_len += strlen(name)+1;
-}
 
+/**
+ * function to check if name ends with a specific suffix
+ *
+ * @param name the name to check
+ * @param suffix the suffix to check for
+ * @return 1 if true
+ */
 static int ends_with(const char *name, const char* suffix) {
     size_t ln, ls;
     assert(name);
@@ -118,10 +59,29 @@ static int ends_with(const char *name, const char* suffix) {
     return strcasecmp(name+ln-ls, suffix) == 0;
 }
 
+
+/**
+ * Check if name is inside .gnunet or .zkey TLD
+ *
+ * @param name name to check
+ * @return 1 if true
+ */
 static int verify_name_allowed(const char *name) {
     return ends_with(name, ".gnunet") || ends_with(name, ".zkey"); 
 }
 
+/**
+ * The gethostbyname hook executed by nsswitch
+ *
+ * @param name the name to resolve
+ * @param af the address family to resolve
+ * @param result the result hostent
+ * @param buffer the result buffer
+ * @param buflen length of the buffer
+ * @param errnop idk
+ * @param h_errnop idk
+ * @return a nss_status code
+ */
 enum nss_status _nss_gns_gethostbyname2_r(
     const char *name,
     int af,
@@ -135,10 +95,8 @@ enum nss_status _nss_gns_gethostbyname2_r(
     enum nss_status status = NSS_STATUS_UNAVAIL;
     int i;
     size_t address_length, l, idx, astart;
-    void (*ipv4_func)(const ipv4_address_t *ipv4, void *userdata);
-    void (*ipv6_func)(const ipv6_address_t *ipv6, void *userdata);
     int name_allowed;
-
+    
     if (af == AF_UNSPEC)
 #ifdef NSS_IPV6_ONLY
         af = AF_INET6;
@@ -175,43 +133,23 @@ enum nss_status _nss_gns_gethostbyname2_r(
     u.count = 0;
     u.data_len = 0;
 
-#ifdef NSS_IPV6_ONLY
-    ipv4_func = NULL;
-#else
-    ipv4_func = af == AF_INET ? ipv4_callback : NULL;
-#endif
-
-#ifdef NSS_IPV4_ONLY
-    ipv6_func = NULL;
-#else
-    ipv6_func = af == AF_INET6 ? ipv6_callback : NULL;
-#endif
-
-#ifdef ENABLE_GNS
     name_allowed = verify_name_allowed(name);
     
-    if (gns_works && name_allowed) {
-        int r;
-
-        if ((r = gns_resolve_name(af, name, data)) < 0)
-            gns_works = 0;
-        else if (r == 0) {
-            if (af == AF_INET && ipv4_func)
-                ipv4_func((ipv4_address_t*) data, &u);
-            if (af == AF_INET6 && ipv6_func)
-                ipv6_func((ipv6_address_t*)data, &u);
-        } else
-            status = NSS_STATUS_NOTFOUND;
-    }
+    if (name_allowed) {
 
-#endif /* ENABLE_GNS */
+        if (!gns_resolve_name(af, name, &u) == 0)
+        {
+          status = NSS_STATUS_NOTFOUND;
+        }
+    }
 
     if (u.count == 0) {
         *errnop = ETIMEDOUT;
         *h_errnop = HOST_NOT_FOUND;
         goto finish;
     }
-    
+
+        
     /* Alias names */
     *((char**) buffer) = NULL;
     result->h_aliases = (char**) buffer;
@@ -255,6 +193,17 @@ finish:
     return status;
 }
 
+/**
+ * The gethostbyname hook executed by nsswitch
+ *
+ * @param name the name to resolve
+ * @param result the result hostent
+ * @param buffer the result buffer
+ * @param buflen length of the buffer
+ * @param errnop idk
+ * @param h_errnop idk
+ * @return a nss_status code
+ */
 enum nss_status _nss_gns_gethostbyname_r (
     const char *name,
     struct hostent *result,
@@ -273,6 +222,20 @@ enum nss_status _nss_gns_gethostbyname_r (
         h_errnop);
 }
 
+/**
+ * The gethostbyaddr hook executed by nsswitch
+ * We can't do this so we always return NSS_STATUS_UNAVAIL
+ *
+ * @param addr the address to resolve
+ * @param len the length of the address
+ * @param af the address family of the address
+ * @param result the result hostent
+ * @param buffer the result buffer
+ * @param buflen length of the buffer
+ * @param errnop idk
+ * @param h_errnop idk
+ * @return NSS_STATUS_UNAVAIL
+ */
 enum nss_status _nss_gns_gethostbyaddr_r(
     const void* addr,
     int len,
@@ -285,17 +248,11 @@ enum nss_status _nss_gns_gethostbyaddr_r(
 
     /* we dont do this */
     
-    struct userdata u;
     enum nss_status status = NSS_STATUS_UNAVAIL;
-    int r;
-    size_t address_length, idx, astart;
     
     *errnop = EINVAL;
     *h_errnop = NO_RECOVERY;
 
-    u.count = 0;
-    u.data_len = 0;
-
     /* Check for address types */
 
     *h_errnop = NO_RECOVERY;