about summary refs log tree commit diff
path: root/src/dns-manager/nm-dns-dnsmasq.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dns-manager/nm-dns-dnsmasq.c')
-rw-r--r--src/dns-manager/nm-dns-dnsmasq.c73
1 files changed, 19 insertions, 54 deletions
diff --git a/src/dns-manager/nm-dns-dnsmasq.c b/src/dns-manager/nm-dns-dnsmasq.c
index 1f6e5868..8367693d 100644
--- a/src/dns-manager/nm-dns-dnsmasq.c
+++ b/src/dns-manager/nm-dns-dnsmasq.c
@@ -30,6 +30,7 @@
 #include <glib/gi18n.h>
 
 #include "nm-dns-dnsmasq.h"
+#include "nm-utils.h"
 #include "nm-logging.h"
 #include "nm-ip4-config.h"
 #include "nm-ip6-config.h"
@@ -72,7 +73,7 @@ find_dnsmasq (void)
 static gboolean
 add_ip4_config (GString *str, NMIP4Config *ip4, gboolean split)
 {
-	char buf[INET_ADDRSTRLEN + 1];
+	char buf[INET_ADDRSTRLEN];
 	in_addr_t addr;
 	int nnameservers, i_nameserver, n, i;
 	gboolean added = FALSE;
@@ -87,9 +88,7 @@ add_ip4_config (GString *str, NMIP4Config *ip4, gboolean split)
 
 		for (i_nameserver = 0; i_nameserver < nnameservers; i_nameserver++) {
 			addr = nm_ip4_config_get_nameserver (ip4, i_nameserver);
-			memset (&buf[0], 0, sizeof (buf));
-			if (!inet_ntop (AF_INET, &addr, buf, sizeof (buf)))
-				return FALSE;
+			nm_utils_inet4_ntop (addr, buf);
 
 			/* searches are preferred over domains */
 			n = nm_ip4_config_get_num_searches (ip4);
@@ -127,59 +126,34 @@ add_ip4_config (GString *str, NMIP4Config *ip4, gboolean split)
 	/* If no searches or domains, just add the namservers */
 	if (!added) {
 		for (i = 0; i < nnameservers; i++) {
-			memset (&buf[0], 0, sizeof (buf));
 			addr = nm_ip4_config_get_nameserver (ip4, i);
-			if (inet_ntop (AF_INET, &addr, buf, sizeof (buf)))
-				g_string_append_printf (str, "server=%s\n", buf);
+			g_string_append_printf (str, "server=%s\n", nm_utils_inet4_ntop (addr, NULL));
 		}
 	}
 
 	return TRUE;
 }
 
-#define IP6_ADDR_BUFLEN (INET6_ADDRSTRLEN + 50)
-
 static char *
 ip6_addr_to_string (const struct in6_addr *addr, const char *iface)
 {
-	char *buf, *p;
-
-	/* allocate enough space for the address + interface name */
-	buf = g_malloc0 (IP6_ADDR_BUFLEN + 1);
+	char *buf;
 
-	/* inet_ntop is probably supposed to do this for us, but it doesn't */
 	if (IN6_IS_ADDR_V4MAPPED (addr)) {
-		if (!inet_ntop (AF_INET, &(addr->s6_addr32[3]), buf, IP6_ADDR_BUFLEN))
-			goto error;
-		return buf;
-	}
-
-	if (!inet_ntop (AF_INET6, addr, buf, IP6_ADDR_BUFLEN))
-		goto error;
-
-	/* In the case of addr being a link-local address, inet_ntop can either
-	 * return an address with scope identifier already in place (like
-	 * fe80::202:b3ff:fe8d:7aaf%wlan0) or it returns an address without
-	 * scope identifier at all (like fe80::202:b3ff:fe8d:7aaf)
-	 */
-	p = strchr (buf, '%');
-	if (p) {
-		/* If we got a scope identifier, we need to replace the '%'
-		 * with '@', since dnsmasq supports '%' in server= addresses
+		/* inet_ntop is probably supposed to do this for us, but it doesn't */
+		buf = g_malloc (INET_ADDRSTRLEN);
+		nm_utils_inet4_ntop (addr->s6_addr32[3], buf);
+	} else if (!iface || !iface[0] || !IN6_IS_ADDR_LINKLOCAL (addr)) {
+		buf = g_malloc (INET6_ADDRSTRLEN);
+		nm_utils_inet6_ntop (addr, buf);
+	} else {
+		/* If we got a scope identifier, we need use '%' instead of
+		 * '@', since dnsmasq supports '%' in server= addresses
 		 * only since version 2.58 and up
 		 */
-		*p = '@';
-	} else if (IN6_IS_ADDR_LINKLOCAL (addr)) {
-		/* If we got no scope identifier at all append the interface name */
-		strncat (buf, "@", IP6_ADDR_BUFLEN - strlen (buf));
-		strncat (buf, iface, IP6_ADDR_BUFLEN - strlen (buf));
+		buf = g_strconcat (nm_utils_inet6_ntop (addr, NULL), "@", iface, NULL);
 	}
-
 	return buf;
-
-error:
-	g_free (buf);
-	return NULL;
 }
 
 static gboolean
@@ -203,8 +177,6 @@ add_ip6_config (GString *str, NMIP6Config *ip6, gboolean split)
 		for (i_nameserver = 0; i_nameserver < nnameservers; i_nameserver++) {
 			addr = nm_ip6_config_get_nameserver (ip6, i_nameserver);
 			buf = ip6_addr_to_string (addr, iface);
-			if (!buf)
-				return FALSE;
 
 			/* searches are preferred over domains */
 			n = nm_ip6_config_get_num_searches (ip6);
@@ -225,9 +197,9 @@ add_ip6_config (GString *str, NMIP6Config *ip6, gboolean split)
 					added = TRUE;
 				}
 			}
-		}
 
-		g_free (buf);
+			g_free (buf);
+		}
 	}
 
 	/* If no searches or domains, just add the namservers */
@@ -386,12 +358,6 @@ child_quit (NMDnsPlugin *plugin, gint status)
 /****************************************************************/
 
 static gboolean
-init (NMDnsPlugin *plugin)
-{
-	return TRUE;
-}
-
-static gboolean
 is_caching (NMDnsPlugin *plugin)
 {
 	return TRUE;
@@ -405,10 +371,10 @@ get_name (NMDnsPlugin *plugin)
 
 /****************************************************************/
 
-NMDnsDnsmasq *
+NMDnsPlugin *
 nm_dns_dnsmasq_new (void)
 {
-	return (NMDnsDnsmasq *) g_object_new (NM_TYPE_DNS_DNSMASQ, NULL);
+	return g_object_new (NM_TYPE_DNS_DNSMASQ, NULL);
 }
 
 static void
@@ -434,7 +400,6 @@ nm_dns_dnsmasq_class_init (NMDnsDnsmasqClass *dns_class)
 
 	object_class->dispose = dispose;
 
-	plugin_class->init = init;
 	plugin_class->child_quit = child_quit;
 	plugin_class->is_caching = is_caching;
 	plugin_class->update = update;