1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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;
}
|