← journal

1.1.1.1 is a palindrome

i have been incredibly fortunate enough to maintain nmrs, a rust library for talking to networkmanager over d-bus. this past july it became the networking backend for cosmic settings on pop!_os.

not long after it was merged in, someone opened this issue:

WireGuard import: IPv4 DNS/Address octets stored in reversed byte order (e.g. 10.2.0.1 → 1.0.2.10)

the idea here is that when you import a wireguard config from something like proton or mullvad, the tunnel comes up and the handshake succeeds, but dns doesn’t resolve. in essence, the user would be under the impression that there’s no internet.

the obvious first step was to see what nmcli did with the same file, which the reporter graciously did:

$ nmcli connection import type wireguard file wg-DE-742.conf
$ nmcli -g ipv4.dns connection show wg-DE-742
10.2.0.1

$ nmcli -g ipv4.dns connection show <cosmic-created>
1.0.2.10

alright, so networkmanager was fine, which meant the bug was in nmrs. (despite the issue title, the interface address was never affected, only dns.)

the problem child

this is the code in nmrs that set the dns servers on a connection:

pub fn ipv4_dns(mut self, servers: Vec<Ipv4Addr>) -> Self {
    let dns_u32: Vec<u32> = servers.into_iter().map(u32::from).collect();

    if let Some(ipv4) = self.settings.get_mut("ipv4") {
        ipv4.insert("dns", Value::from(dns_u32));
    }
    self
}

networkmanager wants an array of u32 and i have an Ipv4Addr. std has a From impl for exactly that! so that’s what got used.

yet, the networkmanager docs describe ipv4.dns as:

Array of IP addresses of DNS servers (as network-byte-order integers)

and the rust docs for u32::from(Ipv4Addr) say:

Uses Ipv4Addr::to_bits to convert an IPv4 address to a host byte order u32.

one can quickly see that those two descriptions don’t agree! i couldn’t blame myself for missing it, because the number you get back looks right (to be frank, i did not even think about this edge case).

10.2.0.1 becomes 0x0A020001: 0A is 10, 02 is 2, 00 is 0, 01 is 1, all in order. so even after the report, i sort of had to stew in this mess. it was extremely confusing.

the problem is that networkmanager never reads it as a number. it reads the four bytes that number is stored as, one at a time, and expects them to be the octets in order. a little-endian machine stores the lowest byte first, so 0x0A020001 sits in memory backwards:

address          10.2.0.1
u32::from        0x0A020001
bytes in memory  01 00 02 0a
nm reads         1.0.2.10

from_ne_bytes

the fix is to build the integer from the octets directly:

let dns_u32: Vec<u32> = servers
    .into_iter()
    .map(|server| u32::from_ne_bytes(server.octets()))
    .collect();

i used from_ne_bytes over from_le_bytes or a swap_bytes() on purpose. d-bus carries the integer value across intact no matter what the endianness of either side is. the reinterpretation as bytes happens inside networkmanager, and networkmanager is on the same machine as nmrs. so the byte order that really matters is whatever the local machine’s is. i figure that from_le_bytes would fix x86 machines and produce the same bug for anyone on a big-endian one.1

my exercise in hubris

that line had been in nmrs since december of 2025, and the tests covering it passed for seven months. this is one of them:

#[test]
fn configures_ipv4_dns() {
    let dns: Vec<Ipv4Addr> = vec!["8.8.8.8".parse().unwrap(), "1.1.1.1".parse().unwrap()];
    let settings = ConnectionBuilder::new("802-3-ethernet", "eth0")
        .ipv4_auto()
        .ipv4_dns(dns.clone())
        .build();

    let ipv4 = settings.get("ipv4").unwrap();
    let value = ipv4.get("dns").unwrap();
    assert_eq!(value.value_signature().to_string(), "au");
    assert_eq!(
        value,
        &Value::from(dns.into_iter().map(u32::from).collect::<Vec<_>>())
    );
}

i confidently merged this in without thinking twice about it. in earnest, much of the work being done around the time was fueled by my newfound excitement with claude code.

but i can’t imagine this would have been caught early on regardless, because:

  • the assertion calls u32::from, which is the exact call under test. this checks that the function is equal to itself.

  • 8.8.8.8 reversed is 8.8.8.8. 1.1.1.1 reversed is 1.1.1.1. so even if i had hand-written the expected bytes, the test would have passed in either byte order.

i think this is also why nobody reported it for so long. i can’t imagine many people are questioning the legitimacy of 1.1.1.1, 8.8.8.8, 9.9.9.9 and even 1.0.0.1 as valid dns addresses. perhaps the happenstance that 10.2.0.1 (which i guess is a bit easier to recognize as not being a palindrome?) led someone to notice this bug.

(i actually think this bug was noticed because i finally found a downstream project with enough users that could point it out as quick as they did. i have been the unabashed reviewer/author of this entire project’s code and haven’t had much of an opportunity to get a second pair of eyes at any point during its development aside from the occasional soulless parrot (LLMs)).

anyway, the rewritten test decodes the payload the way networkmanager would, instead of re-running my own conversion, and uses addresses that aren’t symmetric:

fn decode_ipv4_dns(value: &Value<'_>) -> Vec<Ipv4Addr> {
    <Vec<u32>>::try_from(value.try_clone().unwrap())
        .unwrap()
        .into_iter()
        .map(|raw| Ipv4Addr::from(raw.to_ne_bytes()))
        .collect()
}

and there’s a second one that pins the literal wire value, so the test doesn’t depend on any conversion i wrote:

#[test]
#[cfg(target_endian = "little")]
fn ipv4_dns_uses_network_byte_order() {
    let settings = ConnectionBuilder::new("802-3-ethernet", "eth0")
        .ipv4_auto()
        .ipv4_dns(vec!["10.2.0.1".parse().unwrap()])
        .build();

    let raw = <Vec<u32>>::try_from(settings["ipv4"].get("dns").unwrap().try_clone().unwrap())
        .unwrap();

    assert_eq!(raw, vec![0x0100_020A]);
    assert_ne!(raw, vec![u32::from(Ipv4Addr::new(10, 2, 0, 1))]);
}

fin

thanks to rockschtar for an issue that was basically a diagnosis.

write better tests. have someone review your code.

i guess it was inevitable that i would find myself wanting to make another entry. i thought about writing it around my current career prospects, or marrying my beautiful wife, or talking about how much AI has ruined our lives again.

i am glad that instead i chose to talk about this silly bug i dealt with a few months ago.

thanks!

Footnotes

  1. writing this, i realize that ipv4.dns is deprecated in favor of the string-based dns-data and nmrs still writes the legacy one. perhaps that is the real long-term fix… ↩