From 76424950e373d3b04ac3dd13019151bfba3e8423 Mon Sep 17 00:00:00 2001 From: sumuel Date: Mon, 17 Aug 2026 20:44:55 +0000 Subject: Add the files --- src/detection/dns/dns.h | 6 ++ src/detection/dns/dns_apple.c | 106 +++++++++++++++++++++++++++++ src/detection/dns/dns_linux.c | 143 ++++++++++++++++++++++++++++++++++++++++ src/detection/dns/dns_windows.c | 63 ++++++++++++++++++ 4 files changed, 318 insertions(+) create mode 100644 src/detection/dns/dns.h create mode 100644 src/detection/dns/dns_apple.c create mode 100644 src/detection/dns/dns_linux.c create mode 100644 src/detection/dns/dns_windows.c (limited to 'src/detection/dns') diff --git a/src/detection/dns/dns.h b/src/detection/dns/dns.h new file mode 100644 index 0000000..3653f62 --- /dev/null +++ b/src/detection/dns/dns.h @@ -0,0 +1,6 @@ +#pragma once + +#include "fastfetch.h" +#include "modules/dns/option.h" + +const char* ffDetectDNS(FFDNSOptions* options, FFlist* results /* list of FFstrbuf */); diff --git a/src/detection/dns/dns_apple.c b/src/detection/dns/dns_apple.c new file mode 100644 index 0000000..e490ffe --- /dev/null +++ b/src/detection/dns/dns_apple.c @@ -0,0 +1,106 @@ +#include "detection/dns/dns.h" + +#include "common/io.h" +#include "common/mallocHelper.h" +#include "common/strutil.h" +#include "common/apple/cf_helpers.h" +#include "common/debug.h" + +#include + +static const char* detectDnsFromConf(const char* path, FFDNSOptions* options, FFlist* results) { + FF_DEBUG("Attempting to read DNS config from %s", path); + + FF_AUTO_CLOSE_FILE FILE* file = fopen(path, "r"); + if (!file) { + FF_DEBUG("Failed to open %s: %s", path, strerror(errno)); + return "fopen(path, r) failed"; + } + + if (results->length > 0) { + FF_DEBUG("Clearing existing DNS entries (%u entries)", results->length); + FF_LIST_FOR_EACH (FFstrbuf, item, *results) { + ffStrbufDestroy(item); + } + ffListClear(results); + } + + FF_AUTO_FREE char* line = NULL; + size_t len = 0; + + while (getline(&line, &len, file) != -1) { + if (ffStrStartsWith(line, "nameserver")) { + char* nameserver = line + strlen("nameserver"); + while (*nameserver == ' ' || *nameserver == '\t') { + nameserver++; + } + if (*nameserver == '\0') { + continue; + } + + char* comment = strchr(nameserver, '#'); + if (comment) { + *comment = '\0'; + } + + if ((ffStrContainsC(nameserver, ':') && !(options->showType & FF_DNS_TYPE_IPV6_BIT)) || + (ffStrContainsC(nameserver, '.') && !(options->showType & FF_DNS_TYPE_IPV4_BIT))) { + continue; + } + + FFstrbuf* item = FF_LIST_ADD(FFstrbuf, *results); + ffStrbufInitS(item, nameserver); + ffStrbufTrimRightSpace(item); + FF_DEBUG("Found DNS server: %s", item->chars); + } + } + + FF_DEBUG("Found %u DNS servers in %s", results->length, path); + return NULL; +} + +const char* ffDetectDNS(FFDNSOptions* options, FFlist* results) { + // Handle macOS-specific DNS configurations + FF_DEBUG("Using SystemConfiguration framework for macOS"); + + // Create a reference to the dynamic store + FF_CFTYPE_AUTO_RELEASE SCDynamicStoreRef store = SCDynamicStoreCreate(NULL, CFSTR("fastfetch"), NULL, NULL); + if (store) { + // Get the network global IPv4 and IPv6 configuration + FF_CFTYPE_AUTO_RELEASE CFStringRef key = SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL, kSCDynamicStoreDomainState, kSCEntNetDNS); + if (key) { + FF_CFTYPE_AUTO_RELEASE CFDictionaryRef dict = SCDynamicStoreCopyValue(store, key); + if (dict) { + // Get the DNS server addresses array + CFArrayRef dnsServers = CFDictionaryGetValue(dict, kSCPropNetDNSServerAddresses); + + if (dnsServers) { + FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); + for (CFIndex i = 0; i < CFArrayGetCount(dnsServers); i++) { + if (ffCfStrGetString(CFArrayGetValueAtIndex(dnsServers, i), &buffer) == NULL) { + // Check if the address matches our filter + if ((ffStrbufContainC(&buffer, ':') && !(options->showType & FF_DNS_TYPE_IPV6_BIT)) || + (ffStrbufContainC(&buffer, '.') && !(options->showType & FF_DNS_TYPE_IPV4_BIT))) { + continue; + } + + // Add to results + FFstrbuf* item = FF_LIST_ADD(FFstrbuf, *results); + ffStrbufInitMove(item, &buffer); + FF_DEBUG("Found DNS server on macOS: %s", item->chars); + } + } + } + } + } + } + + // If we didn't find any servers, try resolv.conf as fallback + if (results->length > 0) { + return NULL; + } + + FF_DEBUG("No DNS servers found via SystemConfiguration, trying resolv.conf"); + // Try standard resolv.conf location on macOS as a fallback + return detectDnsFromConf("/var/run/resolv.conf", options, results); +} diff --git a/src/detection/dns/dns_linux.c b/src/detection/dns/dns_linux.c new file mode 100644 index 0000000..41e008c --- /dev/null +++ b/src/detection/dns/dns_linux.c @@ -0,0 +1,143 @@ +#include "detection/dns/dns.h" + +#include "common/io.h" +#include "common/mallocHelper.h" +#include "common/strutil.h" +#include "common/debug.h" + +#ifdef __HAIKU__ + #define RESOLV_CONF "/system/settings/network/resolv.conf" +#else + #define RESOLV_CONF "/etc/resolv.conf" +#endif + +static const char* detectDnsFromConf(const char* path, FFDNSOptions* options, FFlist* results) { + FF_DEBUG("Attempting to read DNS config from %s", path); + + FF_AUTO_CLOSE_FILE FILE* file = fopen(path, "r"); + if (!file) { + FF_DEBUG("Failed to open %s: %s", path, strerror(errno)); + return "fopen(path, r) failed"; + } + + if (results->length > 0) { + FF_DEBUG("Clearing existing DNS entries (%u entries)", results->length); + FF_LIST_FOR_EACH (FFstrbuf, item, *results) { + ffStrbufDestroy(item); + } + ffListClear(results); + } + + FF_AUTO_FREE char* line = NULL; + size_t len = 0; + + while (getline(&line, &len, file) != -1) { + if (ffStrStartsWith(line, "nameserver")) { + char* nameserver = line + strlen("nameserver"); + while (*nameserver == ' ' || *nameserver == '\t') { + nameserver++; + } + if (*nameserver == '\0') { + continue; + } + + char* comment = strchr(nameserver, '#'); + if (comment) { + *comment = '\0'; + } + + if ((ffStrContainsC(nameserver, ':') && !(options->showType & FF_DNS_TYPE_IPV6_BIT)) || + (ffStrContainsC(nameserver, '.') && !(options->showType & FF_DNS_TYPE_IPV4_BIT))) { + continue; + } + + FFstrbuf* item = FF_LIST_ADD(FFstrbuf, *results); + ffStrbufInitS(item, nameserver); + ffStrbufTrimRightSpace(item); + FF_DEBUG("Found DNS server: %s", item->chars); + } + } + + FF_DEBUG("Found %u DNS servers in %s", results->length, path); + return NULL; +} + +const char* ffDetectDNS(FFDNSOptions* options, FFlist* results) { + FF_DEBUG("Starting DNS detection"); + + const char* error = detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT RESOLV_CONF, options, results); + if (error != NULL) { + FF_DEBUG("Error detecting DNS: %s", error); + return error; + } + +#if __linux__ && !__ANDROID__ + // Handle different DNS management services + if (results->length == 1) { + const FFstrbuf* firstEntry = FF_LIST_FIRST(FFstrbuf, *results); + + if (ffStrbufEqualS(firstEntry, "127.0.0.53")) { + FF_DEBUG("Detected systemd-resolved (127.0.0.53), checking actual DNS servers"); + // Managed by systemd-resolved + if (detectDnsFromConf("/run/systemd/resolve/resolv.conf", options, results) == NULL) { + return NULL; + } + } else if (ffStrbufEqualS(firstEntry, "127.0.0.1")) { + FF_DEBUG("Detected possible NetworkManager (127.0.0.1), checking actual DNS servers"); + // Managed by NetworkManager + if (detectDnsFromConf("/var/run/NetworkManager/resolv.conf", options, results) == NULL) { + return NULL; + } + } + } + + // Check other possible DNS configuration files + if (results->length == 0) { + FF_DEBUG("No DNS servers found, trying alternative config files"); + + // Try resolvconf + FF_DEBUG("Trying resolvconf configuration"); + if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/run/resolvconf/resolv.conf", options, results) == NULL && results->length > 0) { + return NULL; + } + + // Try dnsmasq + FF_DEBUG("Trying dnsmasq configuration"); + if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/var/run/dnsmasq/resolv.conf", options, results) == NULL && results->length > 0) { + return NULL; + } + + // Try openresolv + FF_DEBUG("Trying openresolv configuration"); + if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/etc/resolv.conf.openresolv", options, results) == NULL && results->length > 0) { + return NULL; + } + } +#elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || defined(__OpenBSD__) + // Handle BSD-specific DNS configurations + if (results->length == 0) { + FF_DEBUG("No DNS servers found, trying BSD-specific config files"); + + // FreeBSD and other BSDs may use resolvconf service + FF_DEBUG("Trying BSD resolvconf configuration"); + if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/var/run/resolvconf/resolv.conf", options, results) == NULL && results->length > 0) { + return NULL; + } + + // Some BSDs store DNS configuration here + FF_DEBUG("Trying BSD nameserver configuration"); + if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/var/run/nameserver", options, results) == NULL && results->length > 0) { + return NULL; + } + + // Try common BSD paths + FF_DEBUG("Trying BSD common paths"); + if (detectDnsFromConf(FASTFETCH_TARGET_DIR_ROOT "/etc/nameserver", options, results) == NULL && results->length > 0) { + return NULL; + } + } +#endif + + FF_DEBUG("DNS detection completed with %u servers found", results->length); + return NULL; +} diff --git a/src/detection/dns/dns_windows.c b/src/detection/dns/dns_windows.c new file mode 100644 index 0000000..b28eefb --- /dev/null +++ b/src/detection/dns/dns_windows.c @@ -0,0 +1,63 @@ +#include "detection/dns/dns.h" +#include "common/netif.h" +#include "common/mallocHelper.h" + +#include +#include + +const char* ffDetectDNS(FFDNSOptions* options, FFlist* results) { + IP_ADAPTER_ADDRESSES* FF_AUTO_FREE adapter_addresses = NULL; + + // Multiple attempts in case interfaces change while + // we are in the middle of querying them. + DWORD adapter_addresses_buffer_size = 0; + for (int attempts = 0;; ++attempts) { + if (adapter_addresses_buffer_size) { + adapter_addresses = (IP_ADAPTER_ADDRESSES*) realloc(adapter_addresses, adapter_addresses_buffer_size); + assert(adapter_addresses); + } + + DWORD error = GetAdaptersAddresses( + options->showType & FF_DNS_TYPE_IPV4_BIT + ? options->showType & FF_DNS_TYPE_IPV6_BIT ? AF_UNSPEC : AF_INET + : AF_INET6, + GAA_FLAG_SKIP_UNICAST | GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_FRIENDLY_NAME, + NULL, + adapter_addresses, + &adapter_addresses_buffer_size); + + if (error == ERROR_SUCCESS) { + break; + } else if (ERROR_BUFFER_OVERFLOW == error && attempts < 4) { + continue; + } else { + return "GetAdaptersAddresses() failed"; + } + } + + uint32_t defaultRouteIfIndex = ffNetifGetDefaultRouteV4()->ifIndex; + // Iterate through all of the adapters + for (IP_ADAPTER_ADDRESSES* adapter = adapter_addresses; adapter; adapter = adapter->Next) { + if (adapter->IfIndex != defaultRouteIfIndex) { + continue; + } + if (adapter->OperStatus != IfOperStatusUp) { + continue; + } + + for (IP_ADAPTER_DNS_SERVER_ADDRESS_XP* ifa = adapter->FirstDnsServerAddress; ifa; ifa = ifa->Next) { + FFstrbuf* item = FF_LIST_ADD(FFstrbuf, *results); + if (ifa->Address.lpSockaddr->sa_family == AF_INET) { + SOCKADDR_IN* ipv4 = (SOCKADDR_IN*) ifa->Address.lpSockaddr; + ffStrbufInitA(item, INET_ADDRSTRLEN); + item->length = (uint32_t) (RtlIpv4AddressToStringA(&ipv4->sin_addr, item->chars) - item->chars); + } else if (ifa->Address.lpSockaddr->sa_family == AF_INET6) { + SOCKADDR_IN6* ipv6 = (SOCKADDR_IN6*) ifa->Address.lpSockaddr; + ffStrbufInitA(item, INET6_ADDRSTRLEN); + item->length = (uint32_t) (RtlIpv6AddressToStringA(&ipv6->sin6_addr, item->chars) - item->chars); + } + } + break; + } + return NULL; +} -- cgit v1.2.3