rwrap: Fix symbol binding on Solaris.
[obnox/cwrap/resolv_wrapper.git] / src / resolv_wrapper.c
index 8cc1e04..8d0d67e 100644 (file)
@@ -125,6 +125,10 @@ static void rwrap_log(enum rwrap_dbglvl_e dbglvl,
 }
 #endif /* NDEBUG RWRAP_LOG */
 
+#ifndef SAFE_FREE
+#define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
+#endif
+
 #define NEXT_KEY(buf, key) do {                                        \
        (key) = (buf) ? strpbrk((buf), " \t") : NULL;           \
        if ((key) != NULL) {                                    \
@@ -159,9 +163,9 @@ static ssize_t rwrap_fake_header(uint8_t **header_blob, size_t remaining,
 
        h = (HEADER *) hb;
        h->id = res_randomid();         /* random query ID */
-       h->qr = htons(1);               /* response flag */
-       h->rd = htons(1);               /* recursion desired */
-       h->ra = htons(1);               /* resursion available */
+       h->qr = 1;                      /* response flag */
+       h->rd = 1;                      /* recursion desired */
+       h->ra = 1;                      /* resursion available */
 
        h->qdcount = htons(1);          /* no. of questions */
        h->ancount = htons(answers);    /* no. of answers */
@@ -828,7 +832,12 @@ static int libc_res_init(void)
 static int libc_res_ninit(struct __res_state *state)
 {
 #if defined(HAVE_RES_NINIT)
+
+#if defined(HAVE_RES_NINIT_IN_LIBRESOLV)
+       rwrap_load_lib_function(RWRAP_LIBRESOLV, res_ninit);
+#else /* HAVE_RES_NINIT_IN_LIBRESOLV */
        rwrap_load_lib_function(RWRAP_LIBC, res_ninit);
+#endif /* HAVE_RES_NINIT_IN_LIBRESOLV */
 
        return rwrap.fns.libc_res_ninit(state);
 #elif defined(HAVE___RES_NINIT)
@@ -843,7 +852,12 @@ static int libc_res_ninit(struct __res_state *state)
 static void libc_res_nclose(struct __res_state *state)
 {
 #if defined(HAVE_RES_NCLOSE)
+
+#if defined(HAVE_RES_NCLOSE_IN_LIBRESOLV)
+       rwrap_load_lib_function(RWRAP_LIBRESOLV, res_nclose);
+#else /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
        rwrap_load_lib_function(RWRAP_LIBC, res_nclose);
+#endif /* HAVE_RES_NCLOSE_IN_LIBRESOLV */
 
        rwrap.fns.libc_res_nclose(state);
 #elif defined(HAVE___RES_NCLOSE)
@@ -966,6 +980,7 @@ static int rwrap_parse_resolv_conf(struct __res_state *state,
                                        .sin_family = AF_INET,
                                        .sin_addr = a,
                                        .sin_port = htons(53),
+                                       .sin_zero = { 0 },
                                };
 
                                state->nscount++;
@@ -980,6 +995,7 @@ static int rwrap_parse_resolv_conf(struct __res_state *state,
 
                                        sa6 = malloc(sizeof(*sa6));
                                        if (sa6 == NULL) {
+                                               fclose(fp);
                                                return -1;
                                        }
 
@@ -1019,9 +1035,11 @@ static int rwrap_parse_resolv_conf(struct __res_state *state,
                RWRAP_LOG(RWRAP_LOG_ERROR,
                          "Reading from %s failed",
                          resolv_conf);
+               fclose(fp);
                return -1;
        }
 
+       fclose(fp);
        return 0;
 }
 
@@ -1100,6 +1118,16 @@ int __res_init(void)
 
 static void rwrap_res_nclose(struct __res_state *state)
 {
+#ifdef HAVE_RESOLV_IPV6_NSADDRS
+       int i;
+
+       if (state != NULL) {
+               for (i = 0; i < state->_u._ext.nscount; i++) {
+                       SAFE_FREE(state->_u._ext.nsaddrs[i]);
+                       state->_u._ext.nssocks[i] = 0;
+               }
+       }
+#endif
        libc_res_nclose(state);
 }