[libvirt] [PATCH v3 6/7] Implement _nss_libvirt_gethostbyname4_r

Michal Privoznik mprivozn at redhat.com
Tue Mar 1 10:42:11 UTC 2016


This function is a different beast compared to previous ones.
But yet again, nothing surprising is happening here.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 tools/nss/libvirt_nss.c    | 92 ++++++++++++++++++++++++++++++++++++++++++++++
 tools/nss/libvirt_nss.h    |  4 ++
 tools/nss/libvirt_nss.syms |  1 +
 3 files changed, 97 insertions(+)

diff --git a/tools/nss/libvirt_nss.c b/tools/nss/libvirt_nss.c
index 2f9cd34..df23cc6 100644
--- a/tools/nss/libvirt_nss.c
+++ b/tools/nss/libvirt_nss.c
@@ -382,3 +382,95 @@ _nss_libvirt_gethostbyname3_r(const char *name, int af, struct hostent *result,
     VIR_FREE(addr);
     return ret;
 }
+
+enum nss_status
+_nss_libvirt_gethostbyname4_r(const char *name, struct gaih_addrtuple **pat,
+                              char *buffer, size_t buflen, int *errnop,
+                              int *herrnop, int32_t *ttlp)
+{
+    enum nss_status ret = NSS_STATUS_UNAVAIL;
+    leaseAddress *addr = NULL;
+    size_t naddr, i;
+    bool found = false;
+    int r;
+    size_t nameLen, need, idx;
+    struct gaih_addrtuple *r_tuple, *r_tuple_first = NULL;
+    char *r_name;
+
+    if ((r = findLease(name, AF_UNSPEC, &addr, &naddr, &found, errnop)) < 0) {
+        /* Error occurred. Return immediately. */
+        if (*errnop == EAGAIN) {
+            *herrnop = TRY_AGAIN;
+            return NSS_STATUS_TRYAGAIN;
+        } else {
+            *herrnop = NO_RECOVERY;
+            return NSS_STATUS_UNAVAIL;
+        }
+    }
+
+    if (!found) {
+        /* NOT found */
+        *errnop = ESRCH;
+        *herrnop = HOST_NOT_FOUND;
+        return NSS_STATUS_NOTFOUND;
+    } else if (!naddr) {
+        /* Found, but no data */
+        *errnop = ENXIO;
+        *herrnop = NO_DATA;
+        return NSS_STATUS_UNAVAIL;
+    }
+
+    /* Found and have data */
+
+    nameLen = strlen(name);
+    /* We need space for:
+     * a) name
+     * b) address */
+    need = ALIGN(nameLen + 1) + ALIGN(sizeof(struct gaih_addrtuple));
+
+    if (buflen < need) {
+        *errnop = ENOMEM;
+        *herrnop = TRY_AGAIN;
+        ret = NSS_STATUS_TRYAGAIN;
+        goto cleanup;
+    }
+
+    /* First, append name */
+    r_name = buffer;
+    memcpy(r_name, name, nameLen + 1);
+    idx = ALIGN(nameLen + 1);
+
+
+    /* Second, append addresses */
+    r_tuple_first = (struct gaih_addrtuple*) (buffer + idx);
+    for (i = 0; i < naddr; i++) {
+        int family = addr[i].af;
+
+        r_tuple = (struct gaih_addrtuple*) (buffer + idx);
+        r_tuple->next = i == naddr - 1 ? NULL : (struct gaih_addrtuple*) ((char*) r_tuple + ALIGN(sizeof(struct gaih_addrtuple)));
+        r_tuple->name = r_name;
+        r_tuple->family = family;
+        r_tuple->scopeid = 0;
+        memcpy(r_tuple->addr, addr[i].addr, FAMILY_ADDRESS_SIZE(family));
+
+        idx += ALIGN(sizeof(struct gaih_addrtuple));
+    }
+
+    /* At this point, idx == need */
+    DEBUG("Done idx:%zd need:%zd", idx, need);
+
+    if (*pat)
+        **pat = *r_tuple_first;
+    else
+        *pat = r_tuple_first;
+
+    if (ttlp)
+        *ttlp = 0;
+
+    /* Explicitly reset all error variables */
+    *errnop = 0;
+    *herrnop = NETDB_SUCCESS;
+    ret = NSS_STATUS_SUCCESS;
+ cleanup:
+    return ret;
+}
diff --git a/tools/nss/libvirt_nss.h b/tools/nss/libvirt_nss.h
index dd037f5..589c1e6 100644
--- a/tools/nss/libvirt_nss.h
+++ b/tools/nss/libvirt_nss.h
@@ -45,4 +45,8 @@ enum nss_status
 _nss_libvirt_gethostbyname3_r(const char *name, int af, struct hostent *result,
                               char *buffer, size_t buflen, int *errnop,
                               int *herrnop, int32_t *ttlp, char **canonp);
+enum nss_status
+_nss_libvirt_gethostbyname4_r(const char *name, struct gaih_addrtuple **pat,
+                              char *buffer, size_t buflen, int *errnop,
+                              int *herrnop, int32_t *ttlp);
 #endif /* __LIBVIRT_NSS_H__ */
diff --git a/tools/nss/libvirt_nss.syms b/tools/nss/libvirt_nss.syms
index b88b8be..5a793e4 100644
--- a/tools/nss/libvirt_nss.syms
+++ b/tools/nss/libvirt_nss.syms
@@ -7,5 +7,6 @@ global:
     _nss_libvirt_gethostbyname_r;
     _nss_libvirt_gethostbyname2_r;
     _nss_libvirt_gethostbyname3_r;
+    _nss_libvirt_gethostbyname4_r;
 local: *;
 };
-- 
2.4.10




More information about the libvir-list mailing list