Commit 3722746c authored by jan.koester's avatar jan.koester
Browse files

test

parent 6fc3adeb
Loading
Loading
Loading
Loading
+29 −8
Changes for src/network.cpp: 29 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/if_packet.h>
@@ -300,9 +301,9 @@ void NetworkManager::applyAll() {
        }
    }

    // Write resolv.conf if we have DNS servers
    // Write resolv.yaml if we have DNS servers
    if (!_dnsServers.empty())
        writeResolvConf(_dnsServers);
        writeResolvYaml(_dnsServers);
}

void NetworkManager::reload() {
@@ -744,14 +745,34 @@ bool NetworkManager::doDhcp(const std::string &ifname) {
    return true;
}

// ─── resolv.conf ────────────────────────────────────────────────────────────
// ─── resolv.yaml ────────────────────────────────────────────────────────────

void NetworkManager::writeResolvConf(const std::vector<std::string> &servers) {
    std::ofstream f("/etc/resolv.conf");
    if (!f.is_open()) return;
    f << "# Generated by syslite\n";
// systempp's resolver reads /etc/resolv.yaml, not /etc/resolv.conf: a YAML
// sequence of "- nameserver: <address>" entries (see systempp's
// src/linux/netdb.cpp). Written to a temporary file and renamed into place,
// so a lookup never sees it half-written.
void NetworkManager::writeResolvYaml(const std::vector<std::string> &servers) {
    const std::string path = "/etc/resolv.yaml";
    const std::string tmp = path + ".syslite-tmp";
    std::vector<std::string> unique;
    for (auto &s : servers)
        f << "nameserver " << s << "\n";
        if (!s.empty() && std::find(unique.begin(), unique.end(), s) == unique.end())
            unique.push_back(s);
    {
        std::ofstream f(tmp, std::ios::trunc);
        if (!f.is_open()) return;
        f << "# Generated by syslite from the network configuration\n";
        for (auto &s : unique)
            f << "- nameserver: \"" << s << "\"\n";  // quoted: IPv6 addresses contain ':'
        f.close();
        if (!f) {
            ::unlink(tmp.c_str());
            return;
        }
    }
    ::chmod(tmp.c_str(), 0644);
    if (::rename(tmp.c_str(), path.c_str()) != 0)
        ::unlink(tmp.c_str());
}

// ─── Netlink device monitor (RTM_NEWLINK) ───────────────────────────────────
+2 −2
Changes for src/network.h: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -85,8 +85,8 @@ namespace syslite {
        // DHCP client (raw socket, minimal DHCPv4)
        bool doDhcp(const std::string &ifname);

        // Write /etc/resolv.conf
        void writeResolvConf(const std::vector<std::string> &servers);
        // Write /etc/resolv.yaml (systempp's resolver configuration)
        void writeResolvYaml(const std::vector<std::string> &servers);

        // Helper: get interface index
        int getIfIndex(const std::string &ifname);