summaryrefslogtreecommitdiff
path: root/src/dnsmasq/nm-dnsmasq-utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dnsmasq/nm-dnsmasq-utils.c')
-rw-r--r--src/dnsmasq/nm-dnsmasq-utils.c83
1 files changed, 62 insertions, 21 deletions
diff --git a/src/dnsmasq/nm-dnsmasq-utils.c b/src/dnsmasq/nm-dnsmasq-utils.c
index e4f4324b85..cee52c3def 100644
--- a/src/dnsmasq/nm-dnsmasq-utils.c
+++ b/src/dnsmasq/nm-dnsmasq-utils.c
@@ -35,11 +35,12 @@ nm_dnsmasq_utils_get_range (const NMPlatformIP4Address *addr,
{
guint32 host = addr->address;
guint8 prefix = addr->plen;
- guint32 netmask = nm_utils_ip4_prefix_to_netmask (prefix);
- guint32 first, last, reserved;
+ guint32 netmask;
+ guint32 first, last, mid, reserved;
+ const guint32 NUM = 256;
- g_return_val_if_fail (out_first != NULL, FALSE);
- g_return_val_if_fail (out_last != NULL, FALSE);
+ g_return_val_if_fail (out_first, FALSE);
+ g_return_val_if_fail (out_last, FALSE);
if (prefix > 30) {
if (out_error_desc)
@@ -47,29 +48,69 @@ nm_dnsmasq_utils_get_range (const NMPlatformIP4Address *addr,
return FALSE;
}
- /* Find the first available address *after* the local machine's IP */
- first = (host & netmask) + htonl (1);
+ 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 hundered
+ * 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);
- /* Shortcut: allow a max of 253 addresses; the - htonl(1) here is to assure
- * that we don't set 'last' to the broadcast address of the network. */
- if (prefix < 24)
- last = (host | ~nm_utils_ip4_prefix_to_netmask (24)) - htonl (1);
- else
- last = (host | ~netmask) - htonl(1);
+ /* 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--;
- /* Figure out which range (either above the host address or below it)
- * has more addresses. Reserve some addresses for static IPs.
+ /* 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.
*/
- if (ntohl (host) - ntohl (first) > ntohl (last) - ntohl (host)) {
- /* Range below the host's IP address */
- reserved = (guint32) ((ntohl (host) - ntohl (first)) / 10);
- last = host - htonl (MIN (reserved, 8)) - htonl (1);
+ 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 {
- /* Range above host's IP address */
- reserved = (guint32) ((ntohl (last) - ntohl (host)) / 10);
- first = host + htonl (MIN (reserved, 8)) + htonl (1);
+ /* 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);