Also convert it's header and change it's caller includes.
This patch is done so the libc resolver will be able to retrieve the dns from
OSv dns API.
libc/network/{__dns.c => __dns.cc} | 30 ++++++++++++++++--------------
libc/network/{__dns.h => __dns.hh} | 8 ++++++++
libc/network/__ipparse.c | 2 +-
libc/network/getaddrinfo.c | 2 +-
libc/network/getnameinfo.c | 2 +-
5 files changed, 27 insertions(+), 17 deletions(-)
rename libc/network/{__dns.c => __dns.cc} (90%)
rename libc/network/{__dns.h => __dns.hh} (85%)
diff --git a/libc/network/__dns.c b/libc/network/__dns.cc
similarity index 90%
rename from libc/network/__dns.c
rename to libc/network/__dns.cc
index 68ef63c..8986077 100644
--- a/libc/network/__dns.c
+++ b/libc/network/__dns.cc
@@ -11,7 +11,7 @@
#include <ctype.h>
#include <unistd.h>
#include <pthread.h>
-#include "__dns.h"
+#include "__dns.hh"
#include <stdio.h>
#define TIMEOUT 5
@@ -33,7 +33,7 @@ int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt)
union {
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
- } sa = {0}, ns[3] = {{0}};
+ } sa = {{0}}, ns[3] = {{{0}}};
socklen_t sl = 0;
int nns = 0;
int family = AF_UNSPEC;
@@ -64,7 +64,7 @@ int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt)
/* Make a reasonably unpredictable id */
clock_gettime(CLOCK_REALTIME, &ts);
- id = ts.tv_nsec + ts.tv_nsec/65536UL & 0xffff;
+ id = (ts.tv_nsec + ts.tv_nsec/65536UL) & 0xffff;
/* Get nameservers from resolv.conf, fallback to localhost */
f = fopen("/etc/resolv.conf", "r");
@@ -95,7 +95,7 @@ int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt)
//pthread_cleanup_push(cleanup, (void *)(intptr_t)fd);
//pthread_setcancelstate(cs, 0);
- if (bind(fd, (void *)&sa, sl) < 0) {
+ if (bind(fd, (const sockaddr*)&sa, sl) < 0) {
errcode = EAI_SYSTEM;
goto out;
}
@@ -108,10 +108,11 @@ int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt)
/* Query all configured namservers in parallel */
for (i=0; i<rrcnt; i++) if (rr[i]) for (j=0; j<nns; j++) {
- q[0] = id+i >> 8;
+ q[0] = (id+i) >> 8;
q[1] = id+i;
q[ql-3] = rr[i];
- sendto(fd, q, ql, MSG_NOSIGNAL, (void *)&ns[j], sl);
+ sendto(fd, q, ql, MSG_NOSIGNAL,
+ (const sockaddr*)&ns[j], sl);
}
/* Wait for a response, or until time to retry */
@@ -119,15 +120,16 @@ int __dns_doqueries(unsigned char *dest, const char *name, int *rr, int rrcnt)
/* Process any and all replies */
while (got+failed < rrcnt && (rlen = recvfrom(fd, r, 512, 0,
- (void *)&sa, (socklen_t[1]){sl})) >= 2)
+ (sockaddr*)&sa, (socklen_t *)&sl)) >= 2)
{
/* Ignore replies from addresses we didn't send to */
for (i=0; i<nns; i++) if (!memcmp(ns+i, &sa, sl)) break;
if (i==nns) continue;
/* Compute index of the query from id */
- i = r[0]*256+r[1] - id & 0xffff;
- if ((unsigned)i >= rrcnt || !rr[i]) continue;
+ i = (r[0]*256+r[1] - id) & 0xffff;
+ if ((unsigned int)i >= (unsigned int) rrcnt || !rr[i])
+ continue;
/* Interpret the result code */
switch (r[3] & 15) {
@@ -181,8 +183,8 @@ int __dns_query(unsigned char *r, const void *a, int family, int ptr)
int rr[2], rrcnt = 1;
if (ptr) {
- if (family == AF_INET6) mkptr6(buf, a);
- else mkptr4(buf, a);
+ if (family == AF_INET6) mkptr6(buf, (unsigned char *) a);
+ else mkptr4(buf, (unsigned char *) a);
rr[0] = RR_PTR;
a = buf;
} else if (family == AF_INET6) {
@@ -192,7 +194,7 @@ int __dns_query(unsigned char *r, const void *a, int family, int ptr)
if (family != AF_INET) rr[rrcnt++] = RR_AAAA;
}
- return __dns_doqueries(r, a, rr, rrcnt);
+ return __dns_doqueries(r, (const char *)a, rr, rrcnt);
}
@@ -248,10 +250,10 @@ int __dns_get_rr(void *dest, size_t stride, size_t maxlen, size_t limit, const u
p += 1 + !!*p;
len = p[8]*256 + p[9];
if (p+len > r+512) return -1;
- if (p[1]==rr && len <= maxlen) {
+ if (p[1]==rr && (size_t)len <= maxlen) {
if (dec && decname(tmp, r, p+10)<0) return -1;
if (dest && limit) {
- if (dec) strcpy(dest, tmp);
+ if (dec) strcpy((char *)dest, tmp);
else memcpy(dest, p+10, len);
dest = (char *)dest + stride;
limit--;
diff --git a/libc/network/__dns.h b/libc/network/__dns.hh
similarity index 85%
rename from libc/network/__dns.h
rename to libc/network/__dns.hh
index 9a3f740..b9b3945 100644
--- a/libc/network/__dns.h
+++ b/libc/network/__dns.hh
@@ -5,6 +5,10 @@
#define RR_PTR 12
#define RR_AAAA 28
+#ifdef __cplusplus
+extern "C" {
+#endif
+
int __dns_count_addrs(const unsigned char *, int);
int __dns_get_rr(void *, size_t, size_t, size_t, const unsigned char *, int, int);
@@ -12,3 +16,7 @@ int __dns_query(unsigned char *, const void *, int, int);
int __ipparse(void *, int, const char *);
int __dns_doqueries(unsigned char *, const char *, int *, int);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/libc/network/__ipparse.c b/libc/network/__ipparse.c
index b0647aa..0e3c0a4 100644
--- a/libc/network/__ipparse.c
+++ b/libc/network/__ipparse.c
@@ -3,7 +3,7 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
-#include "__dns.h"
+#include "__dns.hh"
#include <stdio.h>
int __ipparse(void *dest, int family, const char *s0)
diff --git a/libc/network/getaddrinfo.c b/libc/network/getaddrinfo.c
index 749b60f..b0d10e0 100644
--- a/libc/network/getaddrinfo.c
+++ b/libc/network/getaddrinfo.c
@@ -6,7 +6,7 @@
#include <unistd.h>
#include <string.h>
#include <ctype.h>
-#include "__dns.h"
+#include "__dns.hh"
#include <stdio.h>
static int is_valid(const char *host)
diff --git a/libc/network/getnameinfo.c b/libc/network/getnameinfo.c
index b756e6e..fc5acf6 100644
--- a/libc/network/getnameinfo.c
+++ b/libc/network/getnameinfo.c
@@ -8,7 +8,7 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
-#include "__dns.h"
+#include "__dns.hh"
int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl,
char *restrict node, socklen_t nodelen,
--
1.8.1.2