@@ -53,84 +53,11 @@ namespace Inet {
53
53
54
54
chip::System::ObjectPool<DNSResolver, INET_CONFIG_NUM_DNS_RESOLVERS> DNSResolver::sPool ;
55
55
56
- /* *
57
- * This method revolves a host name into a list of IP addresses.
58
- *
59
- * @note
60
- * Even if the operation completes successfully,
61
- * the result might be a zero-length list of IP addresses.
62
- * Most of the error generated are returned via the
63
- * application callback.
64
- *
65
- * @param[in] hostName A pointer to a C string representing the host name
66
- * to be queried.
67
- * @param[in] hostNameLen The string length of host name.
68
- * @param[in] maxAddrs The maximum number of addresses to store in the DNS
69
- * table.
70
- * @param[in] options An integer value controlling how host name address
71
- * resolution is performed. Values are from the #DNSOptions
72
- * enumeration.
73
- * @param[in] addrArray A pointer to the DNS table.
74
- * @param[in] onComplete A pointer to the callback function when a DNS
75
- * request is complete.
76
- * @param[in] appState A pointer to the application state to be passed to
77
- * onComplete when a DNS request is complete.
78
- *
79
- * @retval CHIP_NO_ERROR if a DNS request is handled
80
- * successfully.
81
- *
82
- * @retval CHIP_ERROR_NOT_IMPLEMENTED if DNS resolution is not enabled on
83
- * the underlying platform.
84
- *
85
- * @retval _other_ if other POSIX network or OS error
86
- * was returned by the underlying DNS
87
- * resolver implementation.
88
- *
89
- */
90
- CHIP_ERROR DNSResolver::Resolve (const char * hostName, uint16_t hostNameLen, uint8_t options, uint8_t maxAddrs,
91
- IPAddress * addrArray, DNSResolver::OnResolveCompleteFunct onComplete, void * appState)
92
- {
93
- CHIP_ERROR res = CHIP_NO_ERROR;
94
-
95
- #if !CHIP_SYSTEM_CONFIG_USE_SOCKETS && !LWIP_DNS
96
- Release ();
97
- return CHIP_ERROR_NOT_IMPLEMENTED;
98
- #endif // !CHIP_SYSTEM_CONFIG_USE_SOCKETS && !LWIP_DNS
99
-
100
- uint8_t addrFamilyOption = (options & kDNSOption_AddrFamily_Mask );
101
- uint8_t optionFlags = (options & kDNSOption_Flags_Mask );
102
-
103
- // Check that the supplied options are valid.
104
- if ((addrFamilyOption != kDNSOption_AddrFamily_Any
105
- #if INET_CONFIG_ENABLE_IPV4
106
- && addrFamilyOption != kDNSOption_AddrFamily_IPv4Only && addrFamilyOption != kDNSOption_AddrFamily_IPv4Preferred
107
- #endif
108
- && addrFamilyOption != kDNSOption_AddrFamily_IPv6Only && addrFamilyOption != kDNSOption_AddrFamily_IPv6Preferred ) ||
109
- (optionFlags & ~kDNSOption_ValidFlags ) != 0 )
110
- {
111
- Release ();
112
- return CHIP_ERROR_INVALID_ARGUMENT;
113
- }
114
-
115
- #if CHIP_SYSTEM_CONFIG_USE_SOCKETS || (CHIP_SYSTEM_CONFIG_USE_LWIP && LWIP_DNS)
116
-
117
- // TODO: Eliminate the need for a local buffer when running on LwIP by changing
118
- // the LwIP DNS interface to support non-nul terminated strings.
119
-
120
- char hostNameBuf[NL_DNS_HOSTNAME_MAX_LEN + 1 ]; // DNS limits hostnames to 253 max characters.
121
-
122
- memcpy (hostNameBuf, hostName, hostNameLen);
123
- hostNameBuf[hostNameLen] = 0 ;
124
-
125
- AppState = appState;
126
- AddrArray = addrArray;
127
- MaxAddrs = maxAddrs;
128
- NumAddrs = 0 ;
129
- DNSOptions = options;
130
- OnComplete = onComplete;
131
-
132
56
#if CHIP_SYSTEM_CONFIG_USE_LWIP
133
57
58
+ CHIP_ERROR DNSResolver::ResolveImpl (char * hostNameBuf)
59
+ {
60
+ #if LWIP_DNS
134
61
#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
135
62
136
63
u8_t lwipAddrType;
@@ -206,47 +133,14 @@ CHIP_ERROR DNSResolver::Resolve(const char * hostName, uint16_t hostNameLen, uin
206
133
}
207
134
208
135
return res;
209
-
210
- #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
211
-
212
- #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
213
-
214
- struct addrinfo gaiHints;
215
- struct addrinfo * gaiResults = nullptr ;
216
- int gaiReturnCode;
217
-
218
- // Configure the hints argument for getaddrinfo()
219
- InitAddrInfoHints (gaiHints);
220
-
221
- // Call getaddrinfo() to perform the name resolution.
222
- gaiReturnCode = getaddrinfo (hostNameBuf, nullptr , &gaiHints, &gaiResults);
223
-
224
- // Process the return code and results list returned by getaddrinfo(). If the call
225
- // was successful this will copy the resultant addresses into the caller's array.
226
- res = ProcessGetAddrInfoResult (gaiReturnCode, gaiResults);
227
-
228
- // Invoke the caller's completion function.
229
- onComplete (appState, res, NumAddrs, addrArray);
230
-
231
- // Release DNSResolver object.
136
+ #else // LWIP_DNS
232
137
Release ();
233
-
234
- return CHIP_NO_ERROR;
235
-
236
- #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
237
- #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || (CHIP_SYSTEM_CONFIG_USE_LWIP && LWIP_DNS)
138
+ return CHIP_ERROR_NOT_IMPLEMENTED;
139
+ #endif // LWIP_DNS
238
140
}
239
141
240
- /* *
241
- * This method cancels DNS requests that are in progress.
242
- *
243
- * @retval CHIP_NO_ERROR.
244
- *
245
- */
246
142
CHIP_ERROR DNSResolver::Cancel ()
247
143
{
248
- #if CHIP_SYSTEM_CONFIG_USE_LWIP
249
-
250
144
// NOTE: LwIP does not support canceling DNS requests that are in progress. As a consequence,
251
145
// we can't release the DNSResolver object until LwIP calls us back (because LwIP retains a
252
146
// pointer to the DNSResolver object while the request is active). However, now that the
@@ -270,26 +164,9 @@ CHIP_ERROR DNSResolver::Cancel()
270
164
// Unlock LwIP stack
271
165
UNLOCK_TCPIP_CORE ();
272
166
273
- #endif // CHIP_SYSTEM_CONFIG_USE_LWIP
274
-
275
- #if CHIP_SYSTEM_CONFIG_USE_SOCKETS
276
- #if INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS
277
- // NOTE: DNS lookups can be canceled only when using the asynchronous mode.
278
-
279
- InetLayer & inet = Layer ();
280
-
281
- OnComplete = nullptr ;
282
- AppState = nullptr ;
283
- inet.mAsyncDNSResolver .Cancel (*this );
284
-
285
- #endif // INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS
286
- #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
287
-
288
167
return CHIP_NO_ERROR;
289
168
}
290
169
291
- #if CHIP_SYSTEM_CONFIG_USE_LWIP
292
-
293
170
/* *
294
171
* This method is called by InetLayer on success, failure, or timeout of a
295
172
* DNS request.
@@ -347,6 +224,47 @@ void DNSResolver::LwIPHandleResolveComplete(const char * name, ip_addr_t * ipadd
347
224
348
225
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
349
226
227
+ CHIP_ERROR DNSResolver::ResolveImpl (char * hostNameBuf)
228
+ {
229
+ struct addrinfo gaiHints;
230
+ struct addrinfo * gaiResults = nullptr ;
231
+ int gaiReturnCode;
232
+
233
+ // Configure the hints argument for getaddrinfo()
234
+ InitAddrInfoHints (gaiHints);
235
+
236
+ // Call getaddrinfo() to perform the name resolution.
237
+ gaiReturnCode = getaddrinfo (hostNameBuf, nullptr , &gaiHints, &gaiResults);
238
+
239
+ // Process the return code and results list returned by getaddrinfo(). If the call
240
+ // was successful this will copy the resultant addresses into the caller's array.
241
+ CHIP_ERROR res = ProcessGetAddrInfoResult (gaiReturnCode, gaiResults);
242
+
243
+ // Invoke the caller's completion function.
244
+ OnComplete (AppState, res, NumAddrs, AddrArray);
245
+
246
+ // Release DNSResolver object.
247
+ Release ();
248
+
249
+ return CHIP_NO_ERROR;
250
+ }
251
+
252
+ CHIP_ERROR DNSResolver::Cancel ()
253
+ {
254
+ #if INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS
255
+ // NOTE: DNS lookups can be canceled only when using the asynchronous mode.
256
+
257
+ InetLayer & inet = Layer ();
258
+
259
+ OnComplete = nullptr ;
260
+ AppState = nullptr ;
261
+ inet.mAsyncDNSResolver .Cancel (*this );
262
+
263
+ #endif // INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS
264
+
265
+ return CHIP_NO_ERROR;
266
+ }
267
+
350
268
void DNSResolver::InitAddrInfoHints (struct addrinfo & hints)
351
269
{
352
270
memset (&hints, 0 , sizeof (hints));
@@ -523,7 +441,6 @@ uint8_t DNSResolver::CountAddresses(int family, const struct addrinfo * addrs)
523
441
}
524
442
525
443
#if INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS
526
-
527
444
void DNSResolver::HandleAsyncResolveComplete ()
528
445
{
529
446
// Copy the resolved address to the application supplied buffer, but only if the request hasn't been canceled.
@@ -535,7 +452,41 @@ void DNSResolver::HandleAsyncResolveComplete()
535
452
Release ();
536
453
}
537
454
#endif // INET_CONFIG_ENABLE_ASYNC_DNS_SOCKETS
455
+
538
456
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
539
457
458
+ CHIP_ERROR DNSResolver::Resolve (const char * hostName, uint16_t hostNameLen, uint8_t options, uint8_t maxAddrs,
459
+ IPAddress * addrArray, DNSResolver::OnResolveCompleteFunct onComplete, void * appState)
460
+ {
461
+ uint8_t addrFamilyOption = (options & kDNSOption_AddrFamily_Mask );
462
+ uint8_t optionFlags = (options & kDNSOption_Flags_Mask );
463
+
464
+ // Check that the supplied options are valid.
465
+ if ((addrFamilyOption != kDNSOption_AddrFamily_Any
466
+ #if INET_CONFIG_ENABLE_IPV4
467
+ && addrFamilyOption != kDNSOption_AddrFamily_IPv4Only && addrFamilyOption != kDNSOption_AddrFamily_IPv4Preferred
468
+ #endif
469
+ && addrFamilyOption != kDNSOption_AddrFamily_IPv6Only && addrFamilyOption != kDNSOption_AddrFamily_IPv6Preferred ) ||
470
+ (optionFlags & ~kDNSOption_ValidFlags ) != 0 )
471
+ {
472
+ Release ();
473
+ return CHIP_ERROR_INVALID_ARGUMENT;
474
+ }
475
+
476
+ char hostNameBuf[NL_DNS_HOSTNAME_MAX_LEN + 1 ]; // DNS limits hostnames to 253 max characters.
477
+
478
+ memcpy (hostNameBuf, hostName, hostNameLen);
479
+ hostNameBuf[hostNameLen] = 0 ;
480
+
481
+ AppState = appState;
482
+ AddrArray = addrArray;
483
+ MaxAddrs = maxAddrs;
484
+ NumAddrs = 0 ;
485
+ DNSOptions = options;
486
+ OnComplete = onComplete;
487
+
488
+ return ResolveImpl (hostNameBuf);
489
+ }
490
+
540
491
} // namespace Inet
541
492
} // namespace chip
0 commit comments