Skip to contentSkip to conversation

resolve: musl does not implement res_ninit

PATCH In review Fabrice Fontaine 2 participants 2 messages Sep 29, 2023 – Nov 7, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 resolve: musl does not implement res_ninit

From: Khem Raj <[email protected]>

ported from
http://git.alpinelinux.org/cgit/aports/plain/testing/connman/libresolv.patch

Upstream-Status: Pending

---
 gweb/gresolv.c | 34 +++++++++++++---------------------
 1 file changed, 13 insertions(+), 21 deletions(-)
gweb/gresolv.c +12 -21
index 954e7cf..2a9bc51 100644
@@ -878,8 +879,6 @@ GResolv *g_resolv_new(int index)
 	resolv->index = index;
 	resolv->nameserver_list = NULL;
 
-	res_ninit(&resolv->res);
-
 	return resolv;
 }
 
@@ -919,8 +918,6 @@ void g_resolv_unref(GResolv *resolv)
 
 	flush_nameservers(resolv);
 
-	res_nclose(&resolv->res);
-
 	g_free(resolv);
 }
 
@@ -1023,24 +1020,19 @@ guint g_resolv_lookup_hostname(GResolv *resolv, const char *hostname,
 	debug(resolv, "hostname %s", hostname);
 
 	if (!resolv->nameserver_list) {
-		int i;
-
-		for (i = 0; i < resolv->res.nscount; i++) {
-			char buf[100];
-			int family = resolv->res.nsaddr_list[i].sin_family;
-			void *sa_addr = &resolv->res.nsaddr_list[i].sin_addr;
-
-			if (family != AF_INET &&
-					resolv->res._u._ext.nsaddrs[i]) {
-				family = AF_INET6;
-				sa_addr = &resolv->res._u._ext.nsaddrs[i]->sin6_addr;
+		FILE *f = fopen("/etc/resolv.conf", "r");
+		if (f) {
+			char line[256], *s;
+			int i;
+			while (fgets(line, sizeof(line), f)) {
+				if (strncmp(line, "nameserver", 10) || !isspace(line[10]))
+					continue;
+				for (s = &line[11]; isspace(s[0]); s++);
+				for (i = 0; s[i] && !isspace(s[i]); i++);
+				s[i] = 0;
+				g_resolv_add_nameserver(resolv, s, 53, 0);
 			}
-
-			if (family != AF_INET && family != AF_INET6)
-				continue;
-
-			if (inet_ntop(family, sa_addr, buf, sizeof(buf)))
-				g_resolv_add_nameserver(resolv, buf, 53, 0);
+			fclose(f);
 		}
 
 		if (!resolv->nameserver_list)
#1 lore raw

Hi Fabrice,

Quoted 58 lines

ported from
http://git.alpinelinux.org/cgit/aports/plain/testing/connman/libresolv.patch

Upstream-Status: Pending

---
gweb/gresolv.c | 34 +++++++++++++---------------------

1 file changed, 13 insertions(+), 21 deletions(-)
gweb/gresolv.c +10 -15
index 954e7cf..2a9bc51 100644
@@ -878,8 +879,6 @@ GResolv *g_resolv_new(int index)
 resolv->index = index;
 resolv->nameserver_list = NULL;
 
- res_ninit(&resolv->res);
-
 return resolv;
 }
 
@@ -919,8 +918,6 @@ void g_resolv_unref(GResolv *resolv)
 
 flush_nameservers(resolv);
 
- res_nclose(&resolv->res);
-
 g_free(resolv);
 }
 
@@ -1023,24 +1020,19 @@ guint g_resolv_lookup_hostname(GResolv *resolv, const char *hostname,
 debug(resolv, "hostname %s", hostname);
 
 if (!resolv->nameserver_list) {
- int i;
-
- for (i = 0; i < resolv->res.nscount; i++) {
- char buf[100];
- int family = resolv->res.nsaddr_list[i].sin_family;
- void *sa_addr = &resolv->res.nsaddr_list[i].sin_addr;
-
- if (family != AF_INET &&
- resolv->res._u._ext.nsaddrs[i]) {
- family = AF_INET6;
- sa_addr = &resolv->res._u._ext.nsaddrs[i]->sin6_addr;
+ FILE *f = fopen("/etc/resolv.conf", "r");
+ if (f) {
+ char line[256], *s;
+ int i;
+ while (fgets(line, sizeof(line), f)) {
+ if (strncmp(line, "nameserver", 10) || !isspace(line[10]))
+ continue;
+ for (s = &line[11]; isspace(s[0]); s++);
+ for (i = 0; s[i] && !isspace(s[i]); i++);
+ s[i] = 0;

gweb/gresolv.c guint g_resolv_lookup_hostname(GResolv *resolv, const char *hostname, · line 1036

Quoted 10 lines
gweb/gresolv.c +2 -6
+ g_resolv_add_nameserver(resolv, s, 53, 0);
 }
-
- if (family != AF_INET && family != AF_INET6)
- continue;
-
- if (inet_ntop(family, sa_addr, buf, sizeof(buf)))
- g_resolv_add_nameserver(resolv, buf, 53, 0);
+ fclose(f);
 }

I rather not hardcode /etc/resolv.conf here. Can we make this more flexible and only fallback on systems that don’t support res_ninit.

Regards

Marcel