1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "dns_lookup_name.h"
17
18 #include "fwmark_client.h"
19 #include "net_manager_constants.h"
20
21 namespace OHOS {
22 namespace nmd {
23 using namespace NetManagerStandard;
24 static constexpr int32_t HOST_NAME_LEN = 10;
25 static constexpr int32_t TEMPORARY_FAILURE = 2;
26 static constexpr int32_t NO_ERROR = 3;
27
28 static constexpr uint32_t ADDR_SECOND_LAST_BIT = 15;
29 static constexpr int32_t PREFIX_SIZE = 128;
30 static constexpr int32_t PREFIX_LEN = 8;
31 static constexpr int32_t BUFF_NUM = 2;
32
33 static constexpr int32_t ALENS_MAX_LEN = 4;
34 static constexpr int32_t SEARCH_MAX_LEN = 256;
35
36 static constexpr int32_t DAS_USABLE = 0x40000000;
37 static constexpr int32_t DAS_MATCHINGSCOPE = 0x20000000;
38 static constexpr int32_t DAS_MATCHINGLABEL = 0x10000000;
39 static constexpr int32_t DAS_PREC_SHIFT = 20;
40 static constexpr int32_t DAS_SCOPE_SHIFT = 16;
41 static constexpr int32_t DAS_PREFIX_SHIFT = 8;
42 static constexpr int32_t DAS_ORDER_SHIFT = 0;
43 static constexpr int32_t NAMESERVICES_LEN = 12;
44 static constexpr int32_t ADDR_LEN = 4;
45 static constexpr int32_t CNT_NUM = 2;
46
47 static constexpr int32_t SCOPEOF_RESULT_2 = 2;
48 static constexpr int32_t SCOPEOF_RESULT_5 = 5;
49 static constexpr int32_t SCOPEOF_RESULT_14 = 14;
50 static constexpr int32_t DSCOPE_MAX_LEN = 15;
51
52 static const struct policy {
53 uint8_t addr[ADDR_A6_LEN];
54 uint8_t len;
55 uint8_t mask;
56 uint8_t prec;
57 uint8_t label;
58 } DEF_POLICY[] = {
59 {{'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\1'}, 15, 0xff, 50, 0},
60 {"\0\0\0\0\0\0\0\0\0\0\xff\xff", 11, 0xff, 35, 4},
61 {"\x20\2", 1, 0xff, 30, 2},
62 {"\x20\1", 3, 0xff, 5, 5},
63 {"\xfc", 0, 0xfe, 3, 13},
64 {"", 0, 0, 40, 1},
65 };
66
NameFromNull(AddrData buf[SECOND_ADDR_IN_BUFF],const std::string name,int32_t family,int32_t flags)67 int32_t DnsLookUpName::NameFromNull(AddrData buf[SECOND_ADDR_IN_BUFF], const std::string name, int32_t family,
68 int32_t flags)
69 {
70 if (!name.empty()) {
71 return DNS_ERR_NONE;
72 }
73 int32_t cnt = 0;
74 if (static_cast<uint32_t>(flags) & AI_PASSIVE) {
75 if (family != AF_INET6) {
76 buf[cnt++] = (AddrData){.family = AF_INET};
77 }
78 if (family != AF_INET) {
79 buf[cnt++] = (AddrData){.family = AF_INET6};
80 }
81 } else {
82 if (family != AF_INET6) {
83 buf[cnt++] = (AddrData){.family = AF_INET, .addr = {127, 0, 0, 1}};
84 }
85 if (family != AF_INET) {
86 buf[cnt] = (AddrData){.family = AF_INET6};
87 buf[cnt].addr[ADDR_SECOND_LAST_BIT] = ADDR_FIRST_BIT;
88 ++cnt;
89 }
90 }
91 NETNATIVE_LOG_D("NameFromNull cnt %{public}d", cnt);
92 return cnt;
93 }
94
NameFromNumeric(AddrData buf[ADDR_FIRST_BIT],const std::string name,int32_t family)95 int32_t DnsLookUpName::NameFromNumeric(AddrData buf[ADDR_FIRST_BIT], const std::string name, int32_t family)
96 {
97 return DnsLookUpParse().LookupIpLiteral(buf, name, family);
98 }
99
NameFromDns(AddrData buf[MAXADDRS],char canon[CANON_LINE],const std::string name,int32_t family,const ResolvConf * conf,uint16_t netId)100 int32_t DnsLookUpName::NameFromDns(AddrData buf[MAXADDRS], char canon[CANON_LINE], const std::string name,
101 int32_t family, const ResolvConf *conf, uint16_t netId)
102 {
103 static const struct {
104 int32_t af;
105 int32_t rr;
106 } afrr[BUFF_NUM] = {
107 {.af = AF_INET6, .rr = RR_A},
108 {.af = AF_INET, .rr = RR_AAAA},
109 };
110 uint8_t queriesBuf[ARG_INDEX_2][BUFF_MAX_LEN], answersBuf[ARG_INDEX_2][PACKET_LINE];
111 int32_t queriesLens[ARG_INDEX_2], answersLens[ARG_INDEX_2];
112 int32_t queriesNum = 0;
113 for (int32_t i = 0; i < BUFF_NUM; i++) {
114 if (family != afrr[i].af) {
115 queriesLens[queriesNum] = DnsLookUpParse().ResMkQuery(0, name, 1, afrr[i].rr, nullptr, 0, nullptr,
116 queriesBuf[queriesNum], sizeof(*queriesBuf));
117 if (queriesLens[queriesNum] == -1) {
118 NETNATIVE_LOGE("NameFromDns failed to make query");
119 return EAI_NONAME;
120 }
121 queriesNum++;
122 }
123 }
124 const uint8_t *queries[ARG_INDEX_2] = {queriesBuf[ARG_INDEX_0], queriesBuf[ARG_INDEX_1]};
125 uint8_t *answers[ARG_INDEX_2] = {answersBuf[ARG_INDEX_0], answersBuf[ARG_INDEX_1]};
126 if (DnsLookUpParse().ResMSendRc(queriesNum, queries, queriesLens, answers, answersLens, sizeof(*answersBuf), conf,
127 netId) < 0) {
128 NETNATIVE_LOGE("NameFromDns failed return to send or recv data");
129 return EAI_SYSTEM;
130 }
131
132 DpcCtx ctx = {.addrs = buf, .canon = canon};
133 for (int32_t i = 0; i < queriesNum; i++) {
134 DnsLookUpParse().DnsParse(answersBuf[i], answersLens[i], DnsLookUpParse().DnsParseCallback, &ctx);
135 }
136
137 if (ctx.cnt) {
138 return ctx.cnt;
139 }
140 if (answersLens[ARG_INDEX_0] < ALENS_MAX_LEN ||
141 (answersBuf[ARG_INDEX_0][ARG_INDEX_3] & ADDR_SECOND_LAST_BIT) == TEMPORARY_FAILURE) {
142 NETNATIVE_LOGE("NameFromDns failed try again");
143 return EAI_AGAIN;
144 }
145 if ((answersBuf[ARG_INDEX_0][ARG_INDEX_3] & ADDR_SECOND_LAST_BIT) == SERVER_FAILURE) {
146 NETNATIVE_LOGE("NameFromDns failed return server failure");
147 return EAI_NONAME;
148 }
149 if ((answersBuf[ARG_INDEX_0][ARG_INDEX_3] & ADDR_SECOND_LAST_BIT) == NO_ERROR) {
150 return DNS_ERR_NONE;
151 }
152 NETNATIVE_LOGE("NameFromDns failed non-recoverable failure in name res.");
153 return EAI_FAIL;
154 }
155
NameFromDnsSearch(AddrData buf[MAXADDRS],char canon[CANON_LINE],const std::string name,int32_t family,uint16_t netId)156 int32_t DnsLookUpName::NameFromDnsSearch(AddrData buf[MAXADDRS], char canon[CANON_LINE], const std::string name,
157 int32_t family, uint16_t netId)
158 {
159 char search[SEARCH_MAX_LEN] = {0};
160 ResolvConf conf{};
161 if (DnsLookUpParse().GetResolvConf(&conf, search, sizeof(search), netId) < 0) {
162 NETNATIVE_LOGE("Get resolv from conf failed");
163 return EAI_NONAME;
164 }
165 if (name.empty()) {
166 NETNATIVE_LOGE("the param name is empty.");
167 return EAI_NONAME;
168 }
169
170 size_t nameLen;
171 size_t dots;
172 for (dots = nameLen = 0; name[nameLen]; nameLen++) {
173 if (name[nameLen] == DOT) {
174 dots++;
175 }
176 }
177 if (dots >= conf.nDots || name[nameLen - LOOKUP_NAME_ONE] == DOT) {
178 *search = LOOKUP_NAME_ZERO;
179 }
180
181 if (name[nameLen - LOOKUP_NAME_ONE] == DOT) {
182 nameLen--;
183 }
184 if (!nameLen || name[nameLen - LOOKUP_NAME_ONE] == DOT || nameLen >= SEARCH_MAX_LEN) {
185 NETNATIVE_LOGE("name is unknown");
186 return EAI_NONAME;
187 }
188 if (strcpy_s(canon, name.length() + 1, name.c_str()) != 0) {
189 return EAI_AGAIN;
190 }
191 canon[nameLen] = DOT;
192 char *pos, *temp;
193 for (pos = search; *pos; pos = temp) {
194 for (; isspace(*pos); pos++) {
195 };
196 for (temp = pos; *temp && !isspace(*temp); temp++) {
197 };
198 if (temp == pos) {
199 break;
200 }
201 if (temp - pos < static_cast<long>(SEARCH_MAX_LEN - LOOKUP_NAME_ONE) - static_cast<long>(nameLen)) {
202 uint32_t cannonAddLen = nameLen + LOOKUP_NAME_ONE;
203 uint32_t posLen = temp - pos;
204 if (memcpy_s(canon + cannonAddLen, posLen, pos, posLen) != 0) {
205 NETNATIVE_LOGE("memcpy_s faild");
206 return NETMANAGER_ERR_MEMCPY_FAIL;
207 }
208 canon[temp - pos + LOOKUP_NAME_ONE + nameLen] = LOOKUP_NAME_ZERO;
209 int32_t cnt = NameFromDns(buf, canon, canon, family, &conf, netId);
210 if (cnt) {
211 return cnt;
212 }
213 }
214 }
215
216 canon[nameLen] = LOOKUP_NAME_ZERO;
217 return NameFromDns(buf, canon, name, family, &conf, netId);
218 }
219
PolicyOf(const in6_addr * in6Addr)220 const struct policy *DnsLookUpName::PolicyOf(const in6_addr *in6Addr)
221 {
222 for (int32_t i = 0;; i++) {
223 if (memcmp(in6Addr->s6_addr, DEF_POLICY[i].addr, DEF_POLICY[i].len)) {
224 continue;
225 }
226 if ((in6Addr->s6_addr[DEF_POLICY[i].len] & DEF_POLICY[i].mask) != DEF_POLICY[i].addr[DEF_POLICY[i].len]) {
227 continue;
228 }
229 return DEF_POLICY + i;
230 }
231 return {};
232 }
233
LabelOf(const in6_addr * in6Addr)234 int32_t DnsLookUpName::LabelOf(const in6_addr *in6Addr)
235 {
236 return PolicyOf(in6Addr)->label;
237 }
238
ScopeOf(const in6_addr * in6Addr)239 int32_t DnsLookUpName::ScopeOf(const in6_addr *in6Addr)
240 {
241 if (IN6_IS_ADDR_MULTICAST(in6Addr)) {
242 return in6Addr->s6_addr[ARG_INDEX_1] & DSCOPE_MAX_LEN;
243 }
244 if (IN6_IS_ADDR_LINKLOCAL(in6Addr)) {
245 return SCOPEOF_RESULT_2;
246 }
247 if (IN6_IS_ADDR_LOOPBACK(in6Addr)) {
248 return SCOPEOF_RESULT_2;
249 }
250 if (IN6_IS_ADDR_SITELOCAL(in6Addr)) {
251 return SCOPEOF_RESULT_5;
252 }
253 return SCOPEOF_RESULT_14;
254 }
255
PreFixMatch(const in6_addr * s,const in6_addr * d)256 int32_t DnsLookUpName::PreFixMatch(const in6_addr *s, const in6_addr *d)
257 {
258 int32_t i;
259 for (i = 0; i < PREFIX_SIZE &&
260 !((s->s6_addr[i / PREFIX_LEN] ^ d->s6_addr[i / PREFIX_LEN]) & (PREFIX_SIZE >> (i % PREFIX_LEN)));
261 i++) {
262 };
263 return i;
264 }
265
AddrCmp(const void * addrA,const void * addrB)266 int32_t DnsLookUpName::AddrCmp(const void *addrA, const void *addrB)
267 {
268 const auto *a = static_cast<const AddrData *>(addrA);
269 const auto *b = static_cast<const AddrData *>(addrB);
270 return b->sortKey - a->sortKey;
271 }
272
CheckNameParam(const std::string name,int32_t & flags,int32_t & family,char * canon)273 int32_t DnsLookUpName::CheckNameParam(const std::string name, int32_t &flags, int32_t &family, char *canon)
274 {
275 if (!name.empty()) {
276 size_t len = name.length() > HOST_MAX_LEN ? HOST_MAX_LEN : name.length();
277 if (len - 1 >= HOST_MAX_LEN_MINUS_ONE) {
278 NETNATIVE_LOGE("name is too long : %{public}d", static_cast<int32_t>(len));
279 return EAI_NONAME;
280 }
281 canon = const_cast<char *>(name.c_str());
282 }
283
284 if (static_cast<uint32_t>(flags) & AI_V4MAPPED) {
285 if (family == AF_INET6) {
286 family = AF_UNSPEC;
287 } else {
288 flags -= AI_V4MAPPED;
289 }
290 }
291 return DNS_ERR_NONE;
292 }
293
RefreshBuf(AddrData * buf,int32_t num,int32_t & cnt)294 void DnsLookUpName::RefreshBuf(AddrData *buf, int32_t num, int32_t &cnt)
295 {
296 int32_t j = 0;
297 for (j = 0; num < cnt; num++) {
298 if (buf[num].family == AF_INET6) {
299 buf[j++] = buf[num];
300 }
301 }
302 cnt = j;
303 }
304
UpdateBuf(int32_t flags,int32_t family,AddrData * buf,int32_t & cnt)305 bool DnsLookUpName::UpdateBuf(int32_t flags, int32_t family, AddrData *buf, int32_t &cnt)
306 {
307 if ((static_cast<uint32_t>(flags) & AI_V4MAPPED)) {
308 int32_t i = 0;
309 if (!(static_cast<uint32_t>(flags) & AI_ALL)) {
310 for (; i < cnt && buf[i].family != AF_INET6; i++) {
311 };
312 if (i < cnt) {
313 RefreshBuf(buf, i, cnt);
314 }
315 }
316 for (i = 0; i < cnt; i++) {
317 if (buf[i].family != AF_INET) {
318 continue;
319 }
320 int32_t ret = memcpy_s(buf[i].addr + NAMESERVICES_LEN, ADDR_LEN, buf[i].addr, ADDR_LEN);
321 ret += memcpy_s(buf[i].addr, NAMESERVICES_LEN, ADDR_BUF, NAMESERVICES_LEN);
322 if (ret != 0) {
323 NETNATIVE_LOGE("memcpy_s faild");
324 return false;
325 }
326 buf[i].family = AF_INET6;
327 }
328 }
329 if (cnt < CNT_NUM || family == AF_INET) {
330 NETNATIVE_LOGE("cnt is less two or there are only IPv4 results, cnt : %{public}d", cnt);
331 return false;
332 }
333 int32_t i = 0;
334 for (i = 0; i < cnt; i++) {
335 if (buf[i].family != AF_INET) {
336 break;
337 }
338 }
339 return i != cnt;
340 }
341
SockAddrCopy(ScokAddrCopy addrBuff,void * da,void * sa,int32_t & dScope,int32_t & preFixLen,uint32_t & key)342 void DnsLookUpName::SockAddrCopy(ScokAddrCopy addrBuff, void *da, void *sa, int32_t &dScope, int32_t &preFixLen,
343 uint32_t &key)
344 {
345 if (!connect(addrBuff.lookUpNameFd, static_cast<sockaddr *>(da), addrBuff.daLen)) {
346 key = key | DAS_USABLE;
347 int32_t res = getsockname(addrBuff.lookUpNameFd, static_cast<sockaddr *>(sa), &addrBuff.saLen);
348 if (res) {
349 (void)close(addrBuff.lookUpNameFd);
350 return;
351 }
352 if (addrBuff.family == AF_INET) {
353 if (memcpy_s(addrBuff.sa6.sin6_addr.s6_addr + NAMESERVICES_LEN, ADDR_A4_LEN, &addrBuff.sa4.sin_addr,
354 ADDR_A4_LEN) != 0) {
355 NETNATIVE_LOGE("memcpy_s faild");
356 return;
357 }
358 }
359 if (dScope == ScopeOf(&addrBuff.sa6.sin6_addr)) {
360 key = key | DAS_MATCHINGSCOPE;
361 }
362 if (addrBuff.dLabel == LabelOf(&addrBuff.sa6.sin6_addr)) {
363 key = key | DAS_MATCHINGLABEL;
364 }
365 preFixLen = PreFixMatch(&addrBuff.sa6.sin6_addr, &addrBuff.da6.sin6_addr);
366 }
367 (void)close(addrBuff.lookUpNameFd);
368 }
369
FindName(AddrData * buf,char * canon,const std::string name,int32_t family,int32_t flags,uint16_t netId)370 int32_t DnsLookUpName::FindName(AddrData *buf, char *canon, const std::string name, int32_t family, int32_t flags,
371 uint16_t netId)
372 {
373 int32_t cnt = NameFromNull(buf, name, family, flags);
374 if (!cnt) {
375 cnt = NameFromNumeric(buf, name, family);
376 }
377 if (!cnt && !(static_cast<uint32_t>(flags) & AI_NUMERICHOST)) {
378 cnt = NameFromDnsSearch(buf, canon, name.c_str(), family, netId);
379 }
380 NETNATIVE_LOG_D("FindName cnt : %{public}d", cnt);
381 return cnt;
382 }
383
MemcpySockaddr(sockaddr_in6 & sa6,sockaddr_in6 & da6,sockaddr_in & da4,AddrData * buf,uint32_t cnt)384 int32_t DnsLookUpName::MemcpySockaddr(sockaddr_in6 &sa6, sockaddr_in6 &da6, sockaddr_in &da4, AddrData *buf,
385 uint32_t cnt)
386 {
387 int32_t ret = memcpy_s(sa6.sin6_addr.s6_addr, NAMESERVICES_LEN, ADDR_BUF, NAMESERVICES_LEN);
388 ret += memcpy_s(da6.sin6_addr.s6_addr + NAMESERVICES_LEN, ADDR_A4_LEN, buf[cnt].addr, ADDR_A4_LEN);
389 ret += memcpy_s(da6.sin6_addr.s6_addr, NAMESERVICES_LEN, ADDR_BUF, NAMESERVICES_LEN);
390 ret += memcpy_s(da6.sin6_addr.s6_addr + NAMESERVICES_LEN, ADDR_A4_LEN, buf[cnt].addr, ADDR_A4_LEN);
391 ret += memcpy_s(&da4.sin_addr, ADDR_A4_LEN, buf[cnt].addr, ADDR_A4_LEN);
392 if (ret != 0) {
393 NETNATIVE_LOGE("memcpy_s faild");
394 return -1;
395 }
396 return 0;
397 }
398
LookUpNameParam(AddrData * buf,int32_t cnt,int32_t netId)399 void DnsLookUpName::LookUpNameParam(AddrData *buf, int32_t cnt, int32_t netId)
400 {
401 for (int32_t i = 0; i < cnt; i++) {
402 int32_t family = buf[i].family;
403 uint32_t key = 0;
404 sockaddr_in6 sa6 = {0};
405 sockaddr_in6 da6 = {
406 .sin6_family = AF_INET6,
407 .sin6_port = PORT_NUM,
408 .sin6_scope_id = buf[i].scopeid,
409 };
410 sockaddr_in sa4 = {0};
411 sockaddr_in da4 = {.sin_family = AF_INET, .sin_port = PORT_NUM};
412 void *sa, *da;
413 socklen_t saLen, daLen;
414 if (family == AF_INET6) {
415 if (memcpy_s(da6.sin6_addr.s6_addr, ADDR_A6_LEN, buf[i].addr, ADDR_A6_LEN) != 0) {
416 NETNATIVE_LOGE("memcpy_s faild");
417 return;
418 }
419 da = &da6;
420 daLen = sizeof(da6);
421 sa = &sa6;
422 saLen = sizeof(da6);
423 } else {
424 if (MemcpySockaddr(sa6, da6, da4, buf, i) < 0) {
425 return;
426 }
427 da = &da4;
428 daLen = sizeof(da4);
429 sa = &sa4;
430 saLen = sizeof(sa4);
431 }
432 const policy *dPolicy = PolicyOf(&da6.sin6_addr);
433 int32_t dScope = ScopeOf(&da6.sin6_addr);
434 int32_t dLabel = dPolicy->label;
435 uint32_t dPrec = dPolicy->prec;
436 int32_t preFixLen = 0;
437 int32_t lookUpNameFd = socket(family, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
438 if (lookUpNameFd >= 0) {
439 ScokAddrCopy addrBuff = {
440 .lookUpNameFd = lookUpNameFd,
441 .sa6 = sa6,
442 .da6 = da6,
443 .sa4 = sa4,
444 .saLen = saLen,
445 .daLen = daLen,
446 .dLabel = dLabel,
447 .family = family,
448 };
449 SockAddrCopy(addrBuff, da, sa, dScope, preFixLen, key);
450 }
451 key |= dPrec << DAS_PREC_SHIFT;
452 key |= static_cast<uint32_t>(DSCOPE_MAX_LEN - dScope) << DAS_SCOPE_SHIFT;
453 key |= static_cast<uint32_t>(preFixLen) << DAS_PREFIX_SHIFT;
454 key |= static_cast<uint32_t>(MAXADDRS - i) << DAS_ORDER_SHIFT;
455 buf[i].sortKey = static_cast<int32_t>(key);
456 }
457 }
458
LookUpName(AddrData buf[MAXADDRS],char canon[CANON_LINE],const std::string & name,int32_t family,int32_t flags,uint16_t netId)459 int32_t DnsLookUpName::LookUpName(AddrData buf[MAXADDRS], char canon[CANON_LINE], const std::string &name,
460 int32_t family, int32_t flags, uint16_t netId)
461 {
462 *canon = 0;
463 int32_t error = CheckNameParam(name, flags, family, canon);
464 if (error < 0) {
465 return error;
466 }
467 int32_t cnt = FindName(buf, canon, name, family, flags, netId);
468 if (cnt <= 0) {
469 NETNATIVE_LOGE("find name failed: %{public}d", cnt);
470 return cnt ? cnt : EAI_NONAME;
471 }
472
473 if (!UpdateBuf(flags, family, buf, cnt)) {
474 return cnt;
475 }
476
477 LookUpNameParam(buf, cnt, netId);
478 qsort(buf, cnt, sizeof(*buf), AddrCmp);
479
480 return cnt;
481 }
482
SwitchSocketType(int32_t sockType,const std::string name,int32_t & proto,ServData * buf)483 int32_t DnsLookUpName::SwitchSocketType(int32_t sockType, const std::string name, int32_t &proto, ServData *buf)
484 {
485 switch (sockType) {
486 case SOCK_STREAM:
487 switch (proto) {
488 case 0:
489 proto = IPPROTO_TCP;
490 [[fallthrough]];
491 case IPPROTO_TCP:
492 break;
493 default:
494 return EAI_SERVICE;
495 }
496 break;
497 case SOCK_DGRAM:
498 switch (proto) {
499 case 0:
500 proto = IPPROTO_UDP;
501 [[fallthrough]];
502 case IPPROTO_UDP:
503 break;
504 default:
505 return EAI_SERVICE;
506 }
507 [[fallthrough]];
508 case 0:
509 break;
510 default:
511 if (!name.empty()) {
512 return EAI_SERVICE;
513 }
514 buf[0].port = 0;
515 buf[0].proto = proto;
516 buf[0].sockType = sockType;
517 break;
518 }
519 return DNS_ERR_NONE;
520 }
521
LookUpServer(ServData buf[MAXSERVS],const std::string name,int32_t proto,int32_t sockType,int32_t flags)522 int32_t DnsLookUpName::LookUpServer(ServData buf[MAXSERVS], const std::string name, int32_t proto, int32_t sockType,
523 int32_t flags)
524 {
525 auto end = std::make_unique<char>(name.length());
526 unsigned long port = 0;
527 int32_t error = SwitchSocketType(sockType, name, proto, buf);
528 if (error < 0) {
529 return error;
530 }
531 const char *serv = (name.length() > 0 ? name.c_str() : nullptr);
532 if (serv) {
533 if (!*serv) {
534 return EAI_SERVICE;
535 }
536 port = strtoul(serv, reinterpret_cast<char **>(&end), HOST_NAME_LEN);
537 }
538 if (!*end) {
539 if (port > PORT_NUM) {
540 return EAI_SERVICE;
541 }
542 int32_t cnt = 0;
543 if (proto != IPPROTO_UDP) {
544 buf[cnt].port = port;
545 buf[cnt].sockType = SOCK_STREAM;
546 buf[cnt++].proto = IPPROTO_TCP;
547 }
548 if (proto != IPPROTO_TCP) {
549 buf[cnt].port = port;
550 buf[cnt].sockType = SOCK_DGRAM;
551 buf[cnt++].proto = IPPROTO_UDP;
552 }
553 return cnt;
554 }
555
556 if (static_cast<uint32_t>(flags) & AI_NUMERICSERV) {
557 return EAI_NONAME;
558 }
559 return EAI_SERVICE;
560 }
561 } // namespace nmd
562 } // namespace OHOS
563