3
3
#include "common/io/io.h"
4
4
#include "util/mallocHelper.h"
5
5
#include "util/stringUtils.h"
6
+ #include "util/debug.h"
6
7
7
8
#ifdef __HAIKU__
8
9
#define RESOLV_CONF "/system/settings/network/resolv.conf"
9
10
#else
10
11
#define RESOLV_CONF "/etc/resolv.conf"
11
12
#endif
12
13
13
- const char * ffDetectDNS ( FFDNSOptions * options , FFlist * results )
14
+ static const char * detectDnsFromConf ( const char * path , FFDNSOptions * options , FFlist * results )
14
15
{
15
- FF_AUTO_CLOSE_FILE FILE * file = fopen (FASTFETCH_TARGET_DIR_ROOT RESOLV_CONF , "r" );
16
+ FF_DEBUG ("Attempting to read DNS config from %s" , path );
17
+
18
+ FF_AUTO_CLOSE_FILE FILE * file = fopen (path , "r" );
16
19
if (!file )
17
- return "fopen (" FASTFETCH_TARGET_DIR_ROOT RESOLV_CONF ") failed" ;
20
+ {
21
+ FF_DEBUG ("Failed to open %s: %m" , path );
22
+ return "fopen(path, r) failed" ;
23
+ }
24
+
25
+ if (results -> length > 0 )
26
+ {
27
+ FF_DEBUG ("Clearing existing DNS entries (%u entries)" , results -> length );
28
+ FF_LIST_FOR_EACH (FFstrbuf , item , * results )
29
+ ffStrbufDestroy (item );
30
+ ffListClear (results );
31
+ }
18
32
19
33
FF_AUTO_FREE char * line = NULL ;
20
34
size_t len = 0 ;
@@ -38,7 +52,90 @@ const char* ffDetectDNS(FFDNSOptions* options, FFlist* results)
38
52
FFstrbuf * item = (FFstrbuf * ) ffListAdd (results );
39
53
ffStrbufInitS (item , nameserver );
40
54
ffStrbufTrimRightSpace (item );
55
+ FF_DEBUG ("Found DNS server: %s" , item -> chars );
56
+ }
57
+ }
58
+
59
+ FF_DEBUG ("Found %u DNS servers in %s" , results -> length , path );
60
+ return NULL ;
61
+ }
62
+
63
+ const char * ffDetectDNS (FFDNSOptions * options , FFlist * results )
64
+ {
65
+ FF_DEBUG ("Starting DNS detection" );
66
+
67
+ const char * error = detectDnsFromConf (FASTFETCH_TARGET_DIR_ROOT RESOLV_CONF , options , results );
68
+ if (error != NULL )
69
+ {
70
+ FF_DEBUG ("Error detecting DNS: %s" , error );
71
+ return error ;
72
+ }
73
+
74
+ #if __linux__ && !__ANDROID__
75
+ // Handle different DNS management services
76
+ if (results -> length == 1 )
77
+ {
78
+ const FFstrbuf * firstEntry = FF_LIST_GET (FFstrbuf , * results , 0 );
79
+
80
+ if (ffStrbufEqualS (firstEntry , "127.0.0.53" ))
81
+ {
82
+ FF_DEBUG ("Detected systemd-resolved (127.0.0.53), checking actual DNS servers" );
83
+ // Managed by systemd-resolved
84
+ if (detectDnsFromConf ("/run/systemd/resolve/resolv.conf" , options , results ) == NULL )
85
+ return NULL ;
41
86
}
87
+ else if (ffStrbufEqualS (firstEntry , "127.0.0.1" ))
88
+ {
89
+ FF_DEBUG ("Detected possible NetworkManager (127.0.0.1), checking actual DNS servers" );
90
+ // Managed by NetworkManager
91
+ if (detectDnsFromConf ("/var/run/NetworkManager/resolv.conf" , options , results ) == NULL )
92
+ return NULL ;
93
+ }
94
+ }
95
+
96
+ // Check other possible DNS configuration files
97
+ if (results -> length == 0 )
98
+ {
99
+ FF_DEBUG ("No DNS servers found, trying alternative config files" );
100
+
101
+ // Try resolvconf
102
+ FF_DEBUG ("Trying resolvconf configuration" );
103
+ if (detectDnsFromConf (FASTFETCH_TARGET_DIR_ROOT "/run/resolvconf/resolv.conf" , options , results ) == NULL && results -> length > 0 )
104
+ return NULL ;
105
+
106
+ // Try dnsmasq
107
+ FF_DEBUG ("Trying dnsmasq configuration" );
108
+ if (detectDnsFromConf (FASTFETCH_TARGET_DIR_ROOT "/var/run/dnsmasq/resolv.conf" , options , results ) == NULL && results -> length > 0 )
109
+ return NULL ;
110
+
111
+ // Try openresolv
112
+ FF_DEBUG ("Trying openresolv configuration" );
113
+ if (detectDnsFromConf (FASTFETCH_TARGET_DIR_ROOT "/etc/resolv.conf.openresolv" , options , results ) == NULL && results -> length > 0 )
114
+ return NULL ;
42
115
}
116
+ #elif defined(__FreeBSD__ ) || defined(__DragonFly__ ) || defined(__NetBSD__ ) || defined(__OpenBSD__ )
117
+ // Handle BSD-specific DNS configurations
118
+ if (results -> length == 0 )
119
+ {
120
+ FF_DEBUG ("No DNS servers found, trying BSD-specific config files" );
121
+
122
+ // FreeBSD and other BSDs may use resolvconf service
123
+ FF_DEBUG ("Trying BSD resolvconf configuration" );
124
+ if (detectDnsFromConf (FASTFETCH_TARGET_DIR_ROOT "/var/run/resolvconf/resolv.conf" , options , results ) == NULL && results -> length > 0 )
125
+ return NULL ;
126
+
127
+ // Some BSDs store DNS configuration here
128
+ FF_DEBUG ("Trying BSD nameserver configuration" );
129
+ if (detectDnsFromConf (FASTFETCH_TARGET_DIR_ROOT "/var/run/nameserver" , options , results ) == NULL && results -> length > 0 )
130
+ return NULL ;
131
+
132
+ // Try common BSD paths
133
+ FF_DEBUG ("Trying BSD common paths" );
134
+ if (detectDnsFromConf (FASTFETCH_TARGET_DIR_ROOT "/etc/nameserver" , options , results ) == NULL && results -> length > 0 )
135
+ return NULL ;
136
+ }
137
+ #endif
138
+
139
+ FF_DEBUG ("DNS detection completed with %u servers found" , results -> length );
43
140
return NULL ;
44
141
}
0 commit comments