@@ -35,33 +35,31 @@ public InetUtils(IDomainNameResolver domainNameResolver, IOptionsMonitor<InetOpt
3535
3636 public virtual HostInfo FindFirstNonLoopbackHostInfo ( )
3737 {
38- InetOptions inetOptions = _optionsMonitor . CurrentValue ;
39-
40- IPAddress ? address = FindFirstNonLoopbackAddress ( inetOptions ) ;
38+ InetOptions options = _optionsMonitor . CurrentValue ;
39+ IPAddress ? address = FindFirstNonLoopbackAddress ( options ) ;
4140
4241 if ( address != null )
4342 {
44- return ConvertAddress ( address , inetOptions ) ;
43+ return ConvertAddress ( address , options ) ;
4544 }
4645
47- return new HostInfo ( inetOptions . DefaultHostname ! , inetOptions . DefaultIPAddress ! ) ;
46+ return new HostInfo ( options . DefaultHostname ! , options . DefaultIPAddress ! ) ;
4847 }
4948
5049 public IPAddress ? FindFirstNonLoopbackAddress ( )
5150 {
5251 return FindFirstNonLoopbackAddress ( _optionsMonitor . CurrentValue ) ;
5352 }
5453
55- private IPAddress ? FindFirstNonLoopbackAddress ( InetOptions inetOptions )
54+ private IPAddress ? FindFirstNonLoopbackAddress ( InetOptions options )
5655 {
5756 IPAddress ? result = null ;
5857
5958 try
6059 {
6160 int lowest = int . MaxValue ;
62- NetworkInterface [ ] networkInterfaces = NetworkInterface . GetAllNetworkInterfaces ( ) ;
6361
64- foreach ( NetworkInterface networkInterface in networkInterfaces )
62+ foreach ( NetworkInterface networkInterface in NetworkInterface . GetAllNetworkInterfaces ( ) )
6563 {
6664 if ( networkInterface is { OperationalStatus : OperationalStatus . Up , IsReceiveOnly : false } )
6765 {
@@ -73,24 +71,7 @@ public virtual HostInfo FindFirstNonLoopbackHostInfo()
7371 if ( iPv4Properties . Index < lowest || result == null )
7472 {
7573 lowest = iPv4Properties . Index ;
76- }
77- else
78- {
79- continue ;
80- }
81-
82- if ( ! IgnoreInterface ( networkInterface . Name , inetOptions ) )
83- {
84- foreach ( UnicastIPAddressInformation addressInfo in properties . UnicastAddresses )
85- {
86- IPAddress address = addressInfo . Address ;
87-
88- if ( IsInet4Address ( address ) && ! IsLoopbackAddress ( address ) && IsPreferredAddress ( address , inetOptions ) )
89- {
90- LogNonLoopbackInterfaceFound ( networkInterface . Name ) ;
91- result = address ;
92- }
93- }
74+ result = GetLastNonLoopbackInterfaceAddress ( networkInterface , properties , options ) ?? result ;
9475 }
9576 }
9677 }
@@ -100,12 +81,28 @@ public virtual HostInfo FindFirstNonLoopbackHostInfo()
10081 LogCannotGetNonLoopbackAddress ( exception ) ;
10182 }
10283
103- if ( result != null )
84+ return result ?? GetHostAddress ( ) ;
85+ }
86+
87+ private IPAddress ? GetLastNonLoopbackInterfaceAddress ( NetworkInterface networkInterface , IPInterfaceProperties properties , InetOptions options )
88+ {
89+ IPAddress ? result = null ;
90+
91+ if ( ! IgnoreInterface ( networkInterface . Name , options ) )
10492 {
105- return result ;
93+ foreach ( UnicastIPAddressInformation addressInfo in properties . UnicastAddresses )
94+ {
95+ IPAddress address = addressInfo . Address ;
96+
97+ if ( IsInet4Address ( address ) && ! IsLoopbackAddress ( address ) && IsPreferredAddress ( address , options ) )
98+ {
99+ LogNonLoopbackInterfaceFound ( networkInterface . Name ) ;
100+ result = address ;
101+ }
102+ }
106103 }
107104
108- return GetHostAddress ( ) ;
105+ return result ;
109106 }
110107
111108 private static bool IsInet4Address ( IPAddress address )
@@ -118,9 +115,9 @@ private static bool IsLoopbackAddress(IPAddress address)
118115 return IPAddress . IsLoopback ( address ) ;
119116 }
120117
121- internal bool IsPreferredAddress ( IPAddress address , InetOptions inetOptions )
118+ internal bool IsPreferredAddress ( IPAddress address , InetOptions options )
122119 {
123- if ( inetOptions . UseOnlySiteLocalInterfaces )
120+ if ( options . UseOnlySiteLocalInterfaces )
124121 {
125122 bool siteLocalAddress = IsSiteLocalAddress ( address ) ;
126123
@@ -132,7 +129,7 @@ internal bool IsPreferredAddress(IPAddress address, InetOptions inetOptions)
132129 return siteLocalAddress ;
133130 }
134131
135- string [ ] preferredNetworks = inetOptions . GetPreferredNetworks ( ) . ToArray ( ) ;
132+ string [ ] preferredNetworks = options . GetPreferredNetworks ( ) . ToArray ( ) ;
136133
137134 if ( preferredNetworks . Length == 0 )
138135 {
@@ -154,32 +151,30 @@ internal bool IsPreferredAddress(IPAddress address, InetOptions inetOptions)
154151 return false ;
155152 }
156153
157- internal bool IgnoreInterface ( string interfaceName , InetOptions inetOptions )
154+ internal bool IgnoreInterface ( string interfaceName , InetOptions options )
158155 {
159- if ( string . IsNullOrEmpty ( interfaceName ) )
160- {
161- return false ;
162- }
163-
164- foreach ( string regex in inetOptions . GetIgnoredInterfaces ( ) )
156+ if ( ! string . IsNullOrEmpty ( interfaceName ) )
165157 {
166- var matcher = new Regex ( regex , InetRegexOptions , RegexMatchTimeout ) ;
167-
168- if ( matcher . IsMatch ( interfaceName ) )
158+ foreach ( string regex in options . GetIgnoredInterfaces ( ) )
169159 {
170- LogIgnoringInterface ( interfaceName ) ;
171- return true ;
160+ var matcher = new Regex ( regex , InetRegexOptions , RegexMatchTimeout ) ;
161+
162+ if ( matcher . IsMatch ( interfaceName ) )
163+ {
164+ LogIgnoringInterface ( interfaceName ) ;
165+ return true ;
166+ }
172167 }
173168 }
174169
175170 return false ;
176171 }
177172
178- internal HostInfo ConvertAddress ( IPAddress address , InetOptions inetOptions )
173+ internal HostInfo ConvertAddress ( IPAddress address , InetOptions options )
179174 {
180175 string hostname ;
181176
182- if ( ! inetOptions . SkipReverseDnsLookup )
177+ if ( ! options . SkipReverseDnsLookup )
183178 {
184179 try
185180 {
@@ -195,29 +190,21 @@ internal HostInfo ConvertAddress(IPAddress address, InetOptions inetOptions)
195190 }
196191 else
197192 {
198- hostname = inetOptions . DefaultHostname ! ;
193+ hostname = options . DefaultHostname ! ;
199194 }
200195
201196 return new HostInfo ( hostname , address . ToString ( ) ) ;
202197 }
203198
204199 private IPAddress ? ResolveHostAddress ( string hostName )
205200 {
206- IPAddress ? result = null ;
207-
208201 try
209202 {
210- IPAddress [ ] results = Dns . GetHostAddresses ( hostName ) ;
211-
212- if ( results . Length > 0 )
203+ foreach ( IPAddress address in Dns . GetHostAddresses ( hostName ) )
213204 {
214- foreach ( IPAddress address in results )
205+ if ( address . AddressFamily == AddressFamily . InterNetwork )
215206 {
216- if ( address . AddressFamily == AddressFamily . InterNetwork )
217- {
218- result = address ;
219- break ;
220- }
207+ return address ;
221208 }
222209 }
223210 }
@@ -226,7 +213,7 @@ internal HostInfo ConvertAddress(IPAddress address, InetOptions inetOptions)
226213 LogUnableToResolveHostAddress ( exception ) ;
227214 }
228215
229- return result ;
216+ return null ;
230217 }
231218
232219 private string ? ResolveHostName ( )
0 commit comments