diff options
Diffstat (limited to 'src/core/dnsmasq')
| -rw-r--r-- | src/core/dnsmasq/nm-dnsmasq-manager.c | 355 | ||||
| -rw-r--r-- | src/core/dnsmasq/nm-dnsmasq-manager.h | 45 | ||||
| -rw-r--r-- | src/core/dnsmasq/nm-dnsmasq-utils.c | 102 | ||||
| -rw-r--r-- | src/core/dnsmasq/nm-dnsmasq-utils.h | 16 | ||||
| -rw-r--r-- | src/core/dnsmasq/tests/meson.build | 16 | ||||
| -rw-r--r-- | src/core/dnsmasq/tests/test-dnsmasq-utils.c | 174 |
6 files changed, 708 insertions, 0 deletions
diff --git a/src/core/dnsmasq/nm-dnsmasq-manager.c b/src/core/dnsmasq/nm-dnsmasq-manager.c new file mode 100644 index 00000000..82c002b0 --- /dev/null +++ b/src/core/dnsmasq/nm-dnsmasq-manager.c @@ -0,0 +1,355 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2008 - 2012 Red Hat, Inc. + */ + +#include "src/core/nm-default-daemon.h" + +#include "nm-dnsmasq-manager.h" + +#include <sys/types.h> +#include <sys/wait.h> +#include <signal.h> +#include <unistd.h> +#include <arpa/inet.h> +#include <stdlib.h> + +#include "nm-dnsmasq-utils.h" +#include "nm-utils.h" +#include "NetworkManagerUtils.h" +#include "nm-core-internal.h" + +#define CONFDIR NMCONFDIR "/dnsmasq-shared.d" + +/*****************************************************************************/ + +enum { + STATE_CHANGED, + LAST_SIGNAL, +}; + +static guint signals[LAST_SIGNAL] = {0}; + +typedef struct { + char *iface; + char *pidfile; + GPid pid; + guint dm_watch_id; +} NMDnsMasqManagerPrivate; + +struct _NMDnsMasqManager { + GObject parent; + NMDnsMasqManagerPrivate _priv; +}; + +struct _NMDnsMasqManagerClass { + GObjectClass parent; +}; + +G_DEFINE_TYPE(NMDnsMasqManager, nm_dnsmasq_manager, G_TYPE_OBJECT) + +#define NM_DNSMASQ_MANAGER_GET_PRIVATE(self) \ + _NM_GET_PRIVATE(self, NMDnsMasqManager, NM_IS_DNSMASQ_MANAGER) + +/*****************************************************************************/ + +#define _NMLOG_DOMAIN LOGD_SHARING +#define _NMLOG(level, ...) __NMLOG_DEFAULT(level, _NMLOG_DOMAIN, "dnsmasq-manager", __VA_ARGS__) + +/*****************************************************************************/ + +static void +dm_watch_cb(GPid pid, int status, gpointer user_data) +{ + NMDnsMasqManager * manager = NM_DNSMASQ_MANAGER(user_data); + NMDnsMasqManagerPrivate *priv = NM_DNSMASQ_MANAGER_GET_PRIVATE(manager); + guint err; + + if (WIFEXITED(status)) { + err = WEXITSTATUS(status); + if (err != 0) { + _LOGW("dnsmasq exited with error: %s", nm_utils_dnsmasq_status_to_string(err, NULL, 0)); + } + } else if (WIFSTOPPED(status)) { + _LOGW("dnsmasq stopped unexpectedly with signal %d", WSTOPSIG(status)); + } else if (WIFSIGNALED(status)) { + _LOGW("dnsmasq died with signal %d", WTERMSIG(status)); + } else { + _LOGW("dnsmasq died from an unknown cause"); + } + + priv->pid = 0; + priv->dm_watch_id = 0; + + g_signal_emit(manager, signals[STATE_CHANGED], 0, NM_DNSMASQ_STATUS_DEAD); +} + +static GPtrArray * +create_dm_cmd_line(const char * iface, + const NMIP4Config *ip4_config, + const char * pidfile, + gboolean announce_android_metered, + GError ** error) +{ + gs_unref_ptrarray GPtrArray *cmd = NULL; + nm_auto_free_gstring GString *s = NULL; + char first[INET_ADDRSTRLEN]; + char last[INET_ADDRSTRLEN]; + char listen_address_s[INET_ADDRSTRLEN]; + char tmpaddr[INET_ADDRSTRLEN]; + gs_free char * error_desc = NULL; + const char * dm_binary; + const NMPlatformIP4Address * listen_address; + guint i, n; + + listen_address = nm_ip4_config_get_first_address(ip4_config); + + g_return_val_if_fail(listen_address, NULL); + + dm_binary = nm_utils_find_helper("dnsmasq", DNSMASQ_PATH, error); + if (!dm_binary) + return NULL; + + cmd = g_ptr_array_new_with_free_func(g_free); + + nm_strv_ptrarray_add_string_dup(cmd, dm_binary); + + if (nm_logging_enabled(LOGL_TRACE, LOGD_SHARING) || getenv("NM_DNSMASQ_DEBUG")) { + nm_strv_ptrarray_add_string_dup(cmd, "--log-dhcp"); + nm_strv_ptrarray_add_string_dup(cmd, "--log-queries"); + } + + /* dnsmasq may read from its default config file location, which if that + * location is a valid config file, it will combine with the options here + * and cause undesirable side-effects. Like sending bogus IP addresses + * as the gateway or whatever. So tell dnsmasq not to use any config file + * at all. + */ + nm_strv_ptrarray_add_string_dup(cmd, "--conf-file=/dev/null"); + + nm_strv_ptrarray_add_string_dup(cmd, "--no-hosts"); + nm_strv_ptrarray_add_string_dup(cmd, "--keep-in-foreground"); + nm_strv_ptrarray_add_string_dup(cmd, "--bind-interfaces"); + nm_strv_ptrarray_add_string_dup(cmd, "--except-interface=lo"); + nm_strv_ptrarray_add_string_dup(cmd, "--clear-on-reload"); + + /* Use strict order since in the case of VPN connections, the VPN's + * nameservers will be first in resolv.conf, and those need to be tried + * first by dnsmasq to successfully resolve names from the VPN. + */ + nm_strv_ptrarray_add_string_dup(cmd, "--strict-order"); + + _nm_utils_inet4_ntop(listen_address->address, listen_address_s); + + nm_strv_ptrarray_add_string_concat(cmd, "--listen-address=", listen_address_s); + + if (!nm_dnsmasq_utils_get_range(listen_address, first, last, &error_desc)) { + g_set_error_literal(error, NM_MANAGER_ERROR, NM_MANAGER_ERROR_FAILED, error_desc); + _LOGW("failed to find DHCP address ranges: %s", error_desc); + return NULL; + } + + nm_strv_ptrarray_add_string_printf(cmd, "--dhcp-range=%s,%s,60m", first, last); + + if (nm_ip4_config_best_default_route_get(ip4_config)) { + nm_strv_ptrarray_add_string_concat(cmd, "--dhcp-option=option:router,", listen_address_s); + } + + if ((n = nm_ip4_config_get_num_nameservers(ip4_config))) { + nm_gstring_prepare(&s); + g_string_append(s, "--dhcp-option=option:dns-server"); + for (i = 0; i < n; i++) { + g_string_append_c(s, ','); + g_string_append( + s, + _nm_utils_inet4_ntop(nm_ip4_config_get_nameserver(ip4_config, i), tmpaddr)); + } + nm_strv_ptrarray_take_gstring(cmd, &s); + } + + if ((n = nm_ip4_config_get_num_searches(ip4_config))) { + nm_gstring_prepare(&s); + g_string_append(s, "--dhcp-option=option:domain-search"); + for (i = 0; i < n; i++) { + g_string_append_c(s, ','); + g_string_append(s, nm_ip4_config_get_search(ip4_config, i)); + } + nm_strv_ptrarray_take_gstring(cmd, &s); + } + + if (announce_android_metered) { + /* force option 43 to announce ANDROID_METERED. Do this, even if the client + * did not ask for this option. See https://www.lorier.net/docs/android-metered.html */ + nm_strv_ptrarray_add_string_dup(cmd, "--dhcp-option-force=43,ANDROID_METERED"); + } + + nm_strv_ptrarray_add_string_dup(cmd, "--dhcp-lease-max=50"); + + nm_strv_ptrarray_add_string_printf(cmd, + "--dhcp-leasefile=%s/dnsmasq-%s.leases", + NMSTATEDIR, + iface); + + nm_strv_ptrarray_add_string_concat(cmd, "--pid-file=", pidfile); + + /* dnsmasq exits if the conf dir is not present */ + if (g_file_test(CONFDIR, G_FILE_TEST_IS_DIR)) + nm_strv_ptrarray_add_string_dup(cmd, "--conf-dir=" CONFDIR); + + g_ptr_array_add(cmd, NULL); + return g_steal_pointer(&cmd); +} + +static void +kill_existing_by_pidfile(const char *pidfile) +{ + char * contents = NULL; + pid_t pid; + char proc_path[250]; + char * cmdline_contents = NULL; + guint64 start_time; + const char *exe; + + if (!pidfile || !g_file_get_contents(pidfile, &contents, NULL, NULL)) + return; + + pid = _nm_utils_ascii_str_to_int64(contents, 10, 1, G_MAXUINT64, 0); + if (pid == 0) + goto out; + + start_time = nm_utils_get_start_time_for_pid(pid, NULL, NULL); + if (start_time == 0) + goto out; + + nm_sprintf_buf(proc_path, "/proc/%lld/cmdline", (long long) pid); + if (!g_file_get_contents(proc_path, &cmdline_contents, NULL, NULL)) + goto out; + + exe = strrchr(cmdline_contents, '/'); + if ((exe && strcmp(&exe[1], "dnsmasq") == 0) || (strcmp(cmdline_contents, DNSMASQ_PATH) == 0)) { + nm_utils_kill_process_sync(pid, start_time, SIGKILL, LOGD_SHARING, "dnsmasq", 0, 0, 500); + } + +out: + unlink(pidfile); + g_free(cmdline_contents); + g_free(contents); +} + +gboolean +nm_dnsmasq_manager_start(NMDnsMasqManager *manager, + NMIP4Config * ip4_config, + gboolean announce_android_metered, + GError ** error) +{ + NMDnsMasqManagerPrivate *priv; + gs_unref_ptrarray GPtrArray *dm_cmd = NULL; + gs_free char * cmd_str = NULL; + + g_return_val_if_fail(NM_IS_DNSMASQ_MANAGER(manager), FALSE); + g_return_val_if_fail(!error || !*error, FALSE); + g_return_val_if_fail(nm_ip4_config_get_num_addresses(ip4_config) > 0, FALSE); + + priv = NM_DNSMASQ_MANAGER_GET_PRIVATE(manager); + + kill_existing_by_pidfile(priv->pidfile); + + dm_cmd = + create_dm_cmd_line(priv->iface, ip4_config, priv->pidfile, announce_android_metered, error); + if (!dm_cmd) + return FALSE; + + _LOGI("starting dnsmasq..."); + _LOGD("command line: %s", (cmd_str = g_strjoinv(" ", (char **) dm_cmd->pdata))); + + priv->pid = 0; + if (!g_spawn_async(NULL, + (char **) dm_cmd->pdata, + NULL, + G_SPAWN_DO_NOT_REAP_CHILD, + nm_utils_setpgid, + NULL, + &priv->pid, + error)) + return FALSE; + + nm_assert(priv->pid > 0); + + _LOGD("dnsmasq started with pid %d", priv->pid); + + priv->dm_watch_id = g_child_watch_add(priv->pid, (GChildWatchFunc) dm_watch_cb, manager); + + return TRUE; +} + +void +nm_dnsmasq_manager_stop(NMDnsMasqManager *manager) +{ + NMDnsMasqManagerPrivate *priv; + + g_return_if_fail(NM_IS_DNSMASQ_MANAGER(manager)); + + priv = NM_DNSMASQ_MANAGER_GET_PRIVATE(manager); + + nm_clear_g_source(&priv->dm_watch_id); + + if (priv->pid) { + nm_utils_kill_child_async(priv->pid, SIGTERM, LOGD_SHARING, "dnsmasq", 2000, NULL, NULL); + priv->pid = 0; + } + + unlink(priv->pidfile); +} + +/*****************************************************************************/ + +static void +nm_dnsmasq_manager_init(NMDnsMasqManager *manager) +{} + +NMDnsMasqManager * +nm_dnsmasq_manager_new(const char *iface) +{ + NMDnsMasqManager * manager; + NMDnsMasqManagerPrivate *priv; + + manager = g_object_new(NM_TYPE_DNSMASQ_MANAGER, NULL); + + priv = NM_DNSMASQ_MANAGER_GET_PRIVATE(manager); + priv->iface = g_strdup(iface); + priv->pidfile = g_strdup_printf(RUNSTATEDIR "/nm-dnsmasq-%s.pid", iface); + + return manager; +} + +static void +finalize(GObject *object) +{ + NMDnsMasqManagerPrivate *priv = NM_DNSMASQ_MANAGER_GET_PRIVATE(object); + + nm_dnsmasq_manager_stop(NM_DNSMASQ_MANAGER(object)); + + g_free(priv->iface); + g_free(priv->pidfile); + + G_OBJECT_CLASS(nm_dnsmasq_manager_parent_class)->finalize(object); +} + +static void +nm_dnsmasq_manager_class_init(NMDnsMasqManagerClass *manager_class) +{ + GObjectClass *object_class = G_OBJECT_CLASS(manager_class); + + object_class->finalize = finalize; + + signals[STATE_CHANGED] = g_signal_new(NM_DNS_MASQ_MANAGER_STATE_CHANGED, + G_OBJECT_CLASS_TYPE(object_class), + G_SIGNAL_RUN_FIRST, + 0, + NULL, + NULL, + g_cclosure_marshal_VOID__UINT, + G_TYPE_NONE, + 1, + G_TYPE_UINT); +} diff --git a/src/core/dnsmasq/nm-dnsmasq-manager.h b/src/core/dnsmasq/nm-dnsmasq-manager.h new file mode 100644 index 00000000..272d1cea --- /dev/null +++ b/src/core/dnsmasq/nm-dnsmasq-manager.h @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2008 Red Hat, Inc. + */ + +#ifndef __NETWORKMANAGER_DNSMASQ_MANAGER_H__ +#define __NETWORKMANAGER_DNSMASQ_MANAGER_H__ + +#include "nm-ip4-config.h" + +#define NM_TYPE_DNSMASQ_MANAGER (nm_dnsmasq_manager_get_type()) +#define NM_DNSMASQ_MANAGER(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), NM_TYPE_DNSMASQ_MANAGER, NMDnsMasqManager)) +#define NM_DNSMASQ_MANAGER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), NM_TYPE_DNSMASQ_MANAGER, NMDnsMasqManagerClass)) +#define NM_IS_DNSMASQ_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), NM_TYPE_DNSMASQ_MANAGER)) +#define NM_IS_DNSMASQ_MANAGER_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), NM_TYPE_DNSMASQ_MANAGER)) +#define NM_DNSMASQ_MANAGER_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), NM_TYPE_DNSMASQ_MANAGER, NMDnsMasqManagerClass)) + +/* signals */ +#define NM_DNS_MASQ_MANAGER_STATE_CHANGED "state-changed" + +typedef enum { + NM_DNSMASQ_STATUS_UNKNOWN, + NM_DNSMASQ_STATUS_DEAD, + NM_DNSMASQ_STATUS_RUNNING, +} NMDnsMasqStatus; + +typedef struct _NMDnsMasqManager NMDnsMasqManager; +typedef struct _NMDnsMasqManagerClass NMDnsMasqManagerClass; + +GType nm_dnsmasq_manager_get_type(void); + +NMDnsMasqManager *nm_dnsmasq_manager_new(const char *iface); + +gboolean nm_dnsmasq_manager_start(NMDnsMasqManager *manager, + NMIP4Config * ip4_config, + gboolean announce_android_metered, + GError ** error); + +void nm_dnsmasq_manager_stop(NMDnsMasqManager *manager); + +#endif /* __NETWORKMANAGER_DNSMASQ_MANAGER_H__ */ diff --git a/src/core/dnsmasq/nm-dnsmasq-utils.c b/src/core/dnsmasq/nm-dnsmasq-utils.c new file mode 100644 index 00000000..ea23a78d --- /dev/null +++ b/src/core/dnsmasq/nm-dnsmasq-utils.c @@ -0,0 +1,102 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2013 Red Hat, Inc. + */ + +#include "src/core/nm-default-daemon.h" + +#include <arpa/inet.h> + +#include "nm-dnsmasq-utils.h" +#include "platform/nm-platform.h" +#include "nm-utils.h" + +gboolean +nm_dnsmasq_utils_get_range(const NMPlatformIP4Address *addr, + char * out_first, + char * out_last, + char ** out_error_desc) +{ + guint32 host = addr->address; + guint8 prefix = addr->plen; + guint32 netmask; + guint32 first, last, mid, reserved; + const guint32 NUM = 256; + + g_return_val_if_fail(out_first, FALSE); + g_return_val_if_fail(out_last, FALSE); + + if (prefix > 30) { + if (out_error_desc) + *out_error_desc = g_strdup_printf("Address prefix %d is too small for DHCP.", prefix); + return FALSE; + } + + if (prefix < 24) { + /* if the subnet is larger then /24, we partition it and treat it + * like it would be a /24. + * + * Hence, the resulting range will always be between x.x.x.1/24 + * and x.x.x.254/24, with x.x.x.0 being the network address of the + * host. + * + * In this case, only a /24 portion of the subnet is used. + * No particular reason for that, but it's unlikely that a user + * would use NetworkManager's shared method when having hundred + * of DHCP clients. So, restrict the range to the same /24 in + * which the host address lies. + */ + prefix = 24; + } + + netmask = _nm_utils_ip4_prefix_to_netmask(prefix); + + /* treat addresses in host-order from here on. */ + netmask = ntohl(netmask); + host = ntohl(host); + + /* if host is the network or broadcast address, coerce it to + * one above or below. Usually, we wouldn't expect the user + * to pick such an address. */ + if (host == (host & netmask)) + host++; + else if (host == (host | ~netmask)) + host--; + + /* Exclude the network and broadcast address. */ + first = (host & netmask) + 1; + last = (host | ~netmask) - 1; + + /* Depending on whether host is above or below the middle of + * the subnet, the larger part if handed out. + * + * If the host is in the lower half, the range starts + * at the lower end with the host (plus reserved), until the + * broadcast address + * + * If the host is in the upper half, the range starts above + * the network-address and goes up until the host (except reserved). + * + * reserved is up to 8 addresses, 10% of the determined range. + */ + mid = (host & netmask) | (((first + last) / 2) & ~netmask); + if (host > mid) { + /* use lower range */ + reserved = NM_MIN(((host - first) / 10), 8); + last = host - 1 - reserved; + first = NM_MAX(first, last > NUM ? last - NUM : 0); + } else { + /* use upper range */ + reserved = NM_MIN(((last - host) / 10), 8); + first = host + 1 + reserved; + last = NM_MIN(last, first < 0xFFFFFFFF - NUM ? first + NUM : 0xFFFFFFFF); + } + + first = htonl(first); + last = htonl(last); + + _nm_utils_inet4_ntop(first, out_first); + _nm_utils_inet4_ntop(last, out_last); + + return TRUE; +} diff --git a/src/core/dnsmasq/nm-dnsmasq-utils.h b/src/core/dnsmasq/nm-dnsmasq-utils.h new file mode 100644 index 00000000..57bbc4c4 --- /dev/null +++ b/src/core/dnsmasq/nm-dnsmasq-utils.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2013 Red Hat, Inc. + */ + +#ifndef __NETWORKMANAGER_DNSMASQ_UTILS_H__ +#define __NETWORKMANAGER_DNSMASQ_UTILS_H__ + +#include "platform/nm-platform.h" + +gboolean nm_dnsmasq_utils_get_range(const NMPlatformIP4Address *addr, + char * out_first, + char * out_last, + char ** out_error_desc); + +#endif /* __NETWORKMANAGER_DNSMASQ_UTILS_H__ */ diff --git a/src/core/dnsmasq/tests/meson.build b/src/core/dnsmasq/tests/meson.build new file mode 100644 index 00000000..565c18ad --- /dev/null +++ b/src/core/dnsmasq/tests/meson.build @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later + +test_unit = 'test-dnsmasq-utils' + +exe = executable( + test_unit, + test_unit + '.c', + dependencies: libNetworkManagerTest_dep, + c_args: test_c_flags, +) + +test( + 'dnsmasq/' + test_unit, + test_script, + args: test_args + [exe.full_path()], +) diff --git a/src/core/dnsmasq/tests/test-dnsmasq-utils.c b/src/core/dnsmasq/tests/test-dnsmasq-utils.c new file mode 100644 index 00000000..4c1279f0 --- /dev/null +++ b/src/core/dnsmasq/tests/test-dnsmasq-utils.c @@ -0,0 +1,174 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2013 Red Hat, Inc. + */ + +#include "src/core/nm-default-daemon.h" + +#include <arpa/inet.h> + +#include "dnsmasq/nm-dnsmasq-utils.h" + +#include "nm-test-utils-core.h" + +static void +test_address_ranges(void) +{ +#define _test_address_range(addr, plen, expected_first, expected_last) \ + G_STMT_START \ + { \ + char *_error_desc = NULL; \ + char _first[INET_ADDRSTRLEN]; \ + char _last[INET_ADDRSTRLEN]; \ + \ + if (!nm_dnsmasq_utils_get_range(nmtst_platform_ip4_address((addr ""), NULL, (plen)), \ + _first, \ + _last, \ + &_error_desc)) \ + g_assert_not_reached(); \ + g_assert(!_error_desc); \ + g_assert_cmpstr(_first, ==, (expected_first "")); \ + g_assert_cmpstr(_last, ==, (expected_last "")); \ + g_assert_cmpint( \ + (ntohl(nmtst_inet4_from_string(_last)) - ntohl(nmtst_inet4_from_string(_first))), \ + <=, \ + 244); \ + } \ + G_STMT_END + +#define _test_address_range_fail(addr, plen) \ + G_STMT_START \ + { \ + char *_error_desc = NULL; \ + char _first[INET_ADDRSTRLEN]; \ + char _last[INET_ADDRSTRLEN]; \ + \ + if (nm_dnsmasq_utils_get_range(nmtst_platform_ip4_address((addr ""), NULL, (plen)), \ + _first, \ + _last, \ + &_error_desc)) \ + g_assert_not_reached(); \ + g_assert(_error_desc); \ + g_free(_error_desc); \ + } \ + G_STMT_END + + _test_address_range_fail("1.2.3.1", 31); + + _test_address_range("0.0.0.0", 30, "0.0.0.2", "0.0.0.2"); + _test_address_range("0.0.0.1", 30, "0.0.0.2", "0.0.0.2"); + _test_address_range("0.0.0.2", 30, "0.0.0.1", "0.0.0.1"); + _test_address_range("0.0.0.3", 30, "0.0.0.1", "0.0.0.1"); + _test_address_range("1.2.3.0", 30, "1.2.3.2", "1.2.3.2"); + _test_address_range("1.2.3.1", 30, "1.2.3.2", "1.2.3.2"); + _test_address_range("1.2.3.2", 30, "1.2.3.1", "1.2.3.1"); + _test_address_range("1.2.3.3", 30, "1.2.3.1", "1.2.3.1"); + _test_address_range("1.2.3.4", 30, "1.2.3.6", "1.2.3.6"); + _test_address_range("1.2.3.5", 30, "1.2.3.6", "1.2.3.6"); + _test_address_range("1.2.3.6", 30, "1.2.3.5", "1.2.3.5"); + _test_address_range("1.2.3.7", 30, "1.2.3.5", "1.2.3.5"); + _test_address_range("1.2.3.8", 30, "1.2.3.10", "1.2.3.10"); + _test_address_range("1.2.3.9", 30, "1.2.3.10", "1.2.3.10"); + _test_address_range("255.255.255.0", 30, "255.255.255.2", "255.255.255.2"); + _test_address_range("255.255.255.1", 30, "255.255.255.2", "255.255.255.2"); + _test_address_range("255.255.255.2", 30, "255.255.255.1", "255.255.255.1"); + _test_address_range("255.255.255.3", 30, "255.255.255.1", "255.255.255.1"); + _test_address_range("255.255.255.248", 30, "255.255.255.250", "255.255.255.250"); + _test_address_range("255.255.255.249", 30, "255.255.255.250", "255.255.255.250"); + _test_address_range("255.255.255.250", 30, "255.255.255.249", "255.255.255.249"); + _test_address_range("255.255.255.251", 30, "255.255.255.249", "255.255.255.249"); + _test_address_range("255.255.255.252", 30, "255.255.255.254", "255.255.255.254"); + _test_address_range("255.255.255.253", 30, "255.255.255.254", "255.255.255.254"); + _test_address_range("255.255.255.254", 30, "255.255.255.253", "255.255.255.253"); + _test_address_range("255.255.255.255", 30, "255.255.255.253", "255.255.255.253"); + + _test_address_range("0.0.0.0", 29, "0.0.0.2", "0.0.0.6"); + _test_address_range("0.0.0.1", 29, "0.0.0.2", "0.0.0.6"); + _test_address_range("0.0.0.2", 29, "0.0.0.3", "0.0.0.6"); + _test_address_range("0.0.0.3", 29, "0.0.0.4", "0.0.0.6"); + _test_address_range("0.0.0.4", 29, "0.0.0.1", "0.0.0.3"); + _test_address_range("0.0.0.5", 29, "0.0.0.1", "0.0.0.4"); + _test_address_range("0.0.0.6", 29, "0.0.0.1", "0.0.0.5"); + _test_address_range("0.0.0.7", 29, "0.0.0.1", "0.0.0.5"); + _test_address_range("0.0.0.8", 29, "0.0.0.10", "0.0.0.14"); + _test_address_range("0.0.0.9", 29, "0.0.0.10", "0.0.0.14"); + _test_address_range("1.2.3.0", 29, "1.2.3.2", "1.2.3.6"); + _test_address_range("1.2.3.1", 29, "1.2.3.2", "1.2.3.6"); + _test_address_range("1.2.3.2", 29, "1.2.3.3", "1.2.3.6"); + _test_address_range("1.2.3.3", 29, "1.2.3.4", "1.2.3.6"); + _test_address_range("1.2.3.4", 29, "1.2.3.1", "1.2.3.3"); + _test_address_range("1.2.3.5", 29, "1.2.3.1", "1.2.3.4"); + _test_address_range("1.2.3.6", 29, "1.2.3.1", "1.2.3.5"); + _test_address_range("1.2.3.7", 29, "1.2.3.1", "1.2.3.5"); + _test_address_range("1.2.3.8", 29, "1.2.3.10", "1.2.3.14"); + _test_address_range("1.2.3.9", 29, "1.2.3.10", "1.2.3.14"); + _test_address_range("255.255.255.248", 29, "255.255.255.250", "255.255.255.254"); + _test_address_range("255.255.255.249", 29, "255.255.255.250", "255.255.255.254"); + _test_address_range("255.255.255.250", 29, "255.255.255.251", "255.255.255.254"); + _test_address_range("255.255.255.251", 29, "255.255.255.252", "255.255.255.254"); + _test_address_range("255.255.255.252", 29, "255.255.255.249", "255.255.255.251"); + _test_address_range("255.255.255.253", 29, "255.255.255.249", "255.255.255.252"); + _test_address_range("255.255.255.254", 29, "255.255.255.249", "255.255.255.253"); + _test_address_range("255.255.255.255", 29, "255.255.255.249", "255.255.255.253"); + + _test_address_range("1.2.3.1", 29, "1.2.3.2", "1.2.3.6"); + _test_address_range("1.2.3.1", 28, "1.2.3.3", "1.2.3.14"); + _test_address_range("1.2.3.1", 26, "1.2.3.8", "1.2.3.62"); + + _test_address_range("192.167.255.255", 24, "192.167.255.1", "192.167.255.245"); + _test_address_range("192.168.0.0", 24, "192.168.0.10", "192.168.0.254"); + _test_address_range("192.168.0.1", 24, "192.168.0.10", "192.168.0.254"); + _test_address_range("192.168.0.2", 24, "192.168.0.11", "192.168.0.254"); + _test_address_range("192.168.0.99", 24, "192.168.0.108", "192.168.0.254"); + _test_address_range("192.168.0.126", 24, "192.168.0.135", "192.168.0.254"); + _test_address_range("192.168.0.127", 24, "192.168.0.136", "192.168.0.254"); + _test_address_range("192.168.0.128", 24, "192.168.0.1", "192.168.0.119"); + _test_address_range("192.168.0.129", 24, "192.168.0.1", "192.168.0.120"); + _test_address_range("192.168.0.130", 24, "192.168.0.1", "192.168.0.121"); + _test_address_range("192.168.0.254", 24, "192.168.0.1", "192.168.0.245"); + _test_address_range("192.168.0.255", 24, "192.168.0.1", "192.168.0.245"); + _test_address_range("192.168.1.0", 24, "192.168.1.10", "192.168.1.254"); + _test_address_range("192.168.1.1", 24, "192.168.1.10", "192.168.1.254"); + _test_address_range("192.168.1.2", 24, "192.168.1.11", "192.168.1.254"); + _test_address_range("192.168.1.10", 24, "192.168.1.19", "192.168.1.254"); + _test_address_range("192.168.15.253", 24, "192.168.15.1", "192.168.15.244"); + _test_address_range("192.168.15.254", 24, "192.168.15.1", "192.168.15.245"); + _test_address_range("192.168.15.255", 24, "192.168.15.1", "192.168.15.245"); + _test_address_range("192.168.16.0", 24, "192.168.16.10", "192.168.16.254"); + _test_address_range("192.168.16.1", 24, "192.168.16.10", "192.168.16.254"); + + _test_address_range("192.167.255.255", 20, "192.167.255.1", "192.167.255.245"); + _test_address_range("192.168.0.0", 20, "192.168.0.10", "192.168.0.254"); + _test_address_range("192.168.0.1", 20, "192.168.0.10", "192.168.0.254"); + _test_address_range("192.168.0.2", 20, "192.168.0.11", "192.168.0.254"); + _test_address_range("192.168.0.126", 20, "192.168.0.135", "192.168.0.254"); + _test_address_range("192.168.0.127", 20, "192.168.0.136", "192.168.0.254"); + _test_address_range("192.168.0.128", 20, "192.168.0.1", "192.168.0.119"); + _test_address_range("192.168.0.129", 20, "192.168.0.1", "192.168.0.120"); + _test_address_range("192.168.0.130", 20, "192.168.0.1", "192.168.0.121"); + _test_address_range("192.168.0.254", 20, "192.168.0.1", "192.168.0.245"); + _test_address_range("192.168.0.255", 20, "192.168.0.1", "192.168.0.245"); + _test_address_range("192.168.1.0", 20, "192.168.1.10", "192.168.1.254"); + _test_address_range("192.168.1.1", 20, "192.168.1.10", "192.168.1.254"); + _test_address_range("192.168.1.2", 20, "192.168.1.11", "192.168.1.254"); + _test_address_range("192.168.1.10", 20, "192.168.1.19", "192.168.1.254"); + _test_address_range("192.168.15.253", 20, "192.168.15.1", "192.168.15.244"); + _test_address_range("192.168.15.254", 20, "192.168.15.1", "192.168.15.245"); + _test_address_range("192.168.15.255", 20, "192.168.15.1", "192.168.15.245"); + _test_address_range("192.168.16.0", 20, "192.168.16.10", "192.168.16.254"); + _test_address_range("192.168.16.1", 20, "192.168.16.10", "192.168.16.254"); +} + +/*****************************************************************************/ + +NMTST_DEFINE(); + +int +main(int argc, char **argv) +{ + nmtst_init_assert_logging(&argc, &argv, "INFO", "DEFAULT"); + + g_test_add_func("/dnsmasq/address-ranges", test_address_ranges); + + return g_test_run(); +} |