Skip to contentSkip to conversation

gweb/gresolv.c: make use of res_ninit optional and subject to __RES

PATCH Superseded Alexander Kanavin 2 participants 3 messages Mar 11, 2024 – Sep 4, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gweb/gresolv.c: make use of res_ninit optional and subject to __RES

From: Khem Raj <[email protected]>

Not all libc implementation have those functions, and the way to determine if they do is to check __RES which is explained in resolv.h thusly:

/*
* Revision information. This is the release date in YYYYMMDD format.
* It can change every day so the right thing to do with it is use it
* in preprocessor commands such as "#if (__RES > 19931104)". Do not
* compare for equality; rather, use it to determine whether your resolver
* is new enough to contain a certain feature.
*/

Indeed, it needs to be at least 19991006.

The portion of the patch that implements a fallback is ported from Alpine Linux:
http://git.alpinelinux.org/cgit/aports/plain/testing/connman/libresolv.patch

---
 gweb/gresolv.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
gweb/gresolv.c +21 -0
index 8101d718..9f1477c3 100644
@@ -879,7 +879,9 @@ GResolv *g_resolv_new(int index)
 	resolv->index = index;
 	resolv->nameserver_list = NULL;
 
+#if (__RES >= 19991006)
 	res_ninit(&resolv->res);
+#endif
 
 	return resolv;
 }
@@ -920,7 +922,9 @@ void g_resolv_unref(GResolv *resolv)
 
 	flush_nameservers(resolv);
 
+#if (__RES >= 19991006)
 	res_nclose(&resolv->res);
+#endif
 
 	g_free(resolv);
 }
@@ -1024,6 +1028,7 @@ guint g_resolv_lookup_hostname(GResolv *resolv, const char *hostname,
 	debug(resolv, "hostname %s", hostname);
 
 	if (!resolv->nameserver_list) {
+#if (__RES >= 19991006)
 		int i;
 
 		for (i = 0; i < resolv->res.nscount; i++) {
@@ -1043,6 +1048,22 @@ guint g_resolv_lookup_hostname(GResolv *resolv, const char *hostname,
 			if (inet_ntop(family, sa_addr, buf, sizeof(buf)))
 				g_resolv_add_nameserver(resolv, buf, 53, 0);
 		}
+#else
+                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);
+                        }
+                        fclose(f);
+                }
+#endif
 
 		if (!resolv->nameserver_list)
 			g_resolv_add_nameserver(resolv, "127.0.0.1", 53, 0);
Signature · 1 line
2.39.2
#1 lore raw

Hi Alex,

Quoted 62 lines

Not all libc implementation have those functions, and the way to determine if they do is to check __RES which is explained in resolv.h thusly:

/*
* Revision information. This is the release date in YYYYMMDD format.
* It can change every day so the right thing to do with it is use it
* in preprocessor commands such as "#if (__RES > 19931104)". Do not
* compare for equality; rather, use it to determine whether your resolver
* is new enough to contain a certain feature.
*/

Indeed, it needs to be at least 19991006.

The portion of the patch that implements a fallback is ported from Alpine Linux:
http://git.alpinelinux.org/cgit/aports/plain/testing/connman/libresolv.patch
---
gweb/gresolv.c | 21 +++++++++++++++++++++

1 file changed, 21 insertions(+)
gweb/gresolv.c +11 -0
index 8101d718..9f1477c3 100644
@@ -879,7 +879,9 @@ GResolv *g_resolv_new(int index)
 resolv->index = index;
 resolv->nameserver_list = NULL;
 
+#if (__RES >= 19991006)
 res_ninit(&resolv->res);
+#endif
 
 return resolv;
 }
@@ -920,7 +922,9 @@ void g_resolv_unref(GResolv *resolv)
 
 flush_nameservers(resolv);
 
+#if (__RES >= 19991006)
 res_nclose(&resolv->res);
+#endif
 
 g_free(resolv);
 }
@@ -1024,6 +1028,7 @@ guint g_resolv_lookup_hostname(GResolv *resolv, const char *hostname,
 debug(resolv, "hostname %s", hostname);
 
 if (!resolv->nameserver_list) {
+#if (__RES >= 19991006)
 int i;
 
 for (i = 0; i < resolv->res.nscount; i++) {
@@ -1043,6 +1048,22 @@ guint g_resolv_lookup_hostname(GResolv *resolv, const char *hostname,
 if (inet_ntop(family, sa_addr, buf, sizeof(buf)))
 g_resolv_add_nameserver(resolv, buf, 53, 0);
 }
+#else
+                FILE *f = fopen("/etc/resolv.conf", "r");
+                if (f) {
+                        char line[256], *s;
+                        int i;
+                        while (fgets(line, sizeof(line), f)) {

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

Quoted 10 lines
gweb/gresolv.c +10 -0
+                                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);
+                        }
+                        fclose(f);
+                }
+#endif

we have a bit of coding style issues here. And this should really be a helper function.

Regards

Marcel

#2 lore raw
Quoted 1 line · Marcel Holtmann

we have a bit of coding style issues here. And this should really be a helper function.

Hello Marcel,

I sent a v2 where things are moved to their own function and spaces -> tabs. Let me know if something else is needed.

Alex

Signature · 11 lines
Alexander Kanavin
Linutronix GmbH | Bahnhofstrasse 3 | D-88690 Uhldingen-Mühlhofen
Phone: +49 7556 25 999 39; Fax.: +49 7556 25 999 99

Hinweise zum Datenschutz finden Sie hier (Informations on data privacy
can be found here): https://linutronix.de/legal/data-protection.php

Linutronix GmbH | Firmensitz (Registered Office): Uhldingen-Mühlhofen |
Registergericht (Registration Court): Amtsgericht Freiburg i.Br., HRB700
806 | Geschäftsführer (Managing Directors): Heinz Egger, Thomas Gleixner
Tiffany Silva, Sean Fennelly, Jeffrey Schneiderman