22// Copyright 2024 Nym Technologies SA <contact@nymtech.net>
33// SPDX-License-Identifier: GPL-3.0-only
44
5- use std:: { fmt , net:: IpAddr } ;
5+ use std:: net:: IpAddr ;
66
77#[ cfg( target_os = "linux" ) ]
88use nym_routing:: RouteManagerHandle ;
@@ -31,142 +31,13 @@ pub use self::imp::Error;
3131pub use imp:: flush_resolver_cache;
3232
3333/// DNS configuration
34- #[ derive( Debug , Clone , PartialEq ) ]
34+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
3535pub struct DnsConfig {
36- config : InnerDnsConfig ,
37- }
38-
39- impl Default for DnsConfig {
40- fn default ( ) -> Self {
41- Self {
42- config : InnerDnsConfig :: Default ,
43- }
44- }
45- }
46-
47- impl DnsConfig {
48- /// Use the specified addresses for DNS resolution
49- pub fn from_addresses ( tunnel_config : & [ IpAddr ] , non_tunnel_config : & [ IpAddr ] ) -> Self {
50- DnsConfig {
51- config : InnerDnsConfig :: Override {
52- tunnel_config : tunnel_config. to_owned ( ) ,
53- non_tunnel_config : non_tunnel_config. to_owned ( ) ,
54- } ,
55- }
56- }
57- }
36+ /// DNS server addresses
37+ pub addresses : Vec < IpAddr > ,
5838
59- impl DnsConfig {
60- pub fn resolve (
61- & self ,
62- default_tun_config : & [ IpAddr ] ,
63- #[ cfg( not( any( target_os = "android" , target_os = "ios" ) ) ) ] port : u16 ,
64- ) -> ResolvedDnsConfig {
65- match & self . config {
66- InnerDnsConfig :: Default => ResolvedDnsConfig {
67- tunnel_config : default_tun_config. to_owned ( ) ,
68- non_tunnel_config : vec ! [ ] ,
69- #[ cfg( not( any( target_os = "android" , target_os = "ios" ) ) ) ]
70- port,
71- } ,
72- InnerDnsConfig :: Override {
73- tunnel_config,
74- non_tunnel_config,
75- } => ResolvedDnsConfig {
76- tunnel_config : tunnel_config. to_owned ( ) ,
77- non_tunnel_config : non_tunnel_config. to_owned ( ) ,
78- #[ cfg( not( any( target_os = "android" , target_os = "ios" ) ) ) ]
79- port,
80- } ,
81- }
82- }
83- }
84-
85- #[ derive( Debug , Clone , PartialEq ) ]
86- enum InnerDnsConfig {
87- /// Use gateway addresses from the tunnel config
88- Default ,
89- /// Use the specified addresses for DNS resolution
90- Override {
91- /// Addresses to configure on the tunnel interface
92- tunnel_config : Vec < IpAddr > ,
93- /// Addresses to allow on non-tunnel interface.
94- /// For the most part, the tunnel state machine will not handle any of this configuration
95- /// on non-tunnel interface, only allow them in the firewall.
96- non_tunnel_config : Vec < IpAddr > ,
97- } ,
98- }
99-
100- /// DNS configuration with `DnsConfig::Default` resolved
101- #[ derive( Debug , Clone , PartialEq , Eq ) ]
102- pub struct ResolvedDnsConfig {
103- /// Addresses to configure on the tunnel interface
104- tunnel_config : Vec < IpAddr > ,
105- /// Addresses to allow on non-tunnel interface.
106- /// For the most part, the tunnel state machine will not handle any of this configuration
107- /// on non-tunnel interface, only allow them in the firewall.
108- non_tunnel_config : Vec < IpAddr > ,
10939 /// Port to use
110- #[ cfg( not( any( target_os = "android" , target_os = "ios" ) ) ) ]
111- port : u16 ,
112- }
113-
114- impl fmt:: Display for ResolvedDnsConfig {
115- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
116- f. write_str ( "Tunnel DNS: " ) ?;
117- Self :: fmt_addr_set ( f, & self . tunnel_config ) ?;
118-
119- f. write_str ( " Non-tunnel DNS: " ) ?;
120- Self :: fmt_addr_set ( f, & self . non_tunnel_config ) ?;
121-
122- #[ cfg( not( any( target_os = "android" , target_os = "ios" ) ) ) ]
123- write ! ( f, " Port: {}" , self . port) ?;
124-
125- Ok ( ( ) )
126- }
127- }
128-
129- impl ResolvedDnsConfig {
130- fn fmt_addr_set ( f : & mut fmt:: Formatter < ' _ > , addrs : & [ IpAddr ] ) -> fmt:: Result {
131- f. write_str ( "{" ) ?;
132- for ( i, addr) in addrs. iter ( ) . enumerate ( ) {
133- if i > 0 {
134- f. write_str ( ", " ) ?;
135- }
136- write ! ( f, "{addr}" ) ?;
137- }
138- f. write_str ( "}" )
139- }
140-
141- /// Addresses to configure on the tunnel interface
142- pub fn tunnel_config ( & self ) -> & [ IpAddr ] {
143- & self . tunnel_config
144- }
145-
146- /// Addresses to allow on non-tunnel interface.
147- /// For the most part, the tunnel state machine will not handle any of this configuration
148- /// on non-tunnel interface, only allow them in the firewall.
149- pub fn non_tunnel_config ( & self ) -> & [ IpAddr ] {
150- & self . non_tunnel_config
151- }
152-
153- /// Consume `self` and return a vector of all addresses
154- pub fn addresses ( self ) -> impl Iterator < Item = IpAddr > {
155- self . non_tunnel_config . into_iter ( ) . chain ( self . tunnel_config )
156- }
157-
158- /// Return whether the config contains only (and at least one) loopback addresses, and zero
159- /// non-loopback addresses
160- pub fn is_loopback ( & self ) -> bool {
161- let ( loopback_addrs, non_loopback_addrs) = self
162- . tunnel_config
163- . iter ( )
164- . chain ( self . non_tunnel_config . iter ( ) )
165- . copied ( )
166- . partition :: < Vec < _ > , _ > ( |ip| ip. is_loopback ( ) ) ;
167-
168- !loopback_addrs. is_empty ( ) && non_loopback_addrs. is_empty ( )
169- }
40+ pub port : u16 ,
17041}
17142
17243/// Sets and monitors system DNS settings. Makes sure the desired DNS servers are being used.
@@ -188,8 +59,8 @@ impl DnsMonitor {
18859 }
18960
19061 /// Set DNS to the given servers. And start monitoring the system for changes.
191- pub async fn set ( & mut self , interface : & str , config : ResolvedDnsConfig ) -> Result < ( ) , Error > {
192- tracing:: info!( "Setting DNS servers on interface '{interface}': {config}" ) ;
62+ pub async fn set ( & mut self , interface : & str , config : DnsConfig ) -> Result < ( ) , Error > {
63+ tracing:: info!( "Setting DNS servers on interface '{interface}': {config:? }" ) ;
19364 self . inner . set ( interface, config) . await
19465 }
19566
@@ -216,8 +87,7 @@ trait DnsMonitorT: Sized {
21687 #[ cfg( target_os = "linux" ) ] route_manager : RouteManagerHandle ,
21788 ) -> Result < Self , Self :: Error > ;
21889
219- async fn set ( & mut self , interface : & str , servers : ResolvedDnsConfig )
220- -> Result < ( ) , Self :: Error > ;
90+ async fn set ( & mut self , interface : & str , servers : DnsConfig ) -> Result < ( ) , Self :: Error > ;
22191
22292 async fn reset ( & mut self ) -> Result < ( ) , Self :: Error > ;
22393
0 commit comments