mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-05-22 17:08:11 +02:00
We use clang-format for automatic formatting of our source files. Since clang-format is actively maintained software, the actual formatting depends on the used version of clang-format. That is unfortunate and painful, but really unavoidable unless clang-format would be strictly bug-compatible. So the version that we must use is from the current Fedora release, which is also tested by our gitlab-ci. Previously, we were using Fedora 34 with clang-tools-extra-12.0.1-1.fc34.x86_64. As Fedora 35 comes along, we need to update our formatting as Fedora 35 comes with version "13.0.0~rc1-1.fc35". An alternative would be to freeze on version 12, but that has different problems (like, it's cumbersome to rebuild clang 12 on Fedora 35 and it would be cumbersome for our developers which are on Fedora 35 to use a clang that they cannot easily install). The (differently painful) solution is to reformat from time to time, as we switch to a new Fedora (and thus clang) version. Usually we would expect that such a reformatting brings minor changes. But this time, the changes are huge. That is mentioned in the release notes [1] as Makes PointerAligment: Right working with AlignConsecutiveDeclarations. (Fixes https://llvm.org/PR27353) [1] https://releases.llvm.org/13.0.0/tools/clang/docs/ReleaseNotes.html#clang-format
103 lines
3.3 KiB
C
103 lines
3.3 KiB
C
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
/*
|
|
* Copyright (C) 2013 Red Hat, Inc.
|
|
*/
|
|
|
|
#include "src/core/nm-default-daemon.h"
|
|
|
|
#include "nm-dnsmasq-utils.h"
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include "libnm-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;
|
|
}
|