• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "nweb_pre_dns_adapter.h"
17 
18 #include <securec.h>
19 #include <vector>
20 
21 #include "nweb_log.h"
22 #include "ohos_adapter_helper.h"
23 
24 namespace OHOS::NWeb {
25     std::map<std::string, addrinfo *> g_AddrInfoMap;
26 
PreDnsInThread()27     void PreDnsInThread()
28     {
29         WVLOG_I("PreDns start");
30         struct addrinfo hints = {0};
31         hints.ai_family = AF_UNSPEC;
32         hints.ai_flags = AI_ADDRCONFIG;
33         hints.ai_socktype = SOCK_STREAM;
34         std::vector<std::string> hostInfo;
35 
36         OHOS::NWeb::OhosWebDnsDataBaseAdapter &dnsDatabaseAdapter =
37             OHOS::NWeb::OhosAdapterHelper::GetInstance().GetWebDnsDataBaseInstance();
38         dnsDatabaseAdapter.GetHostnames(hostInfo);
39         dnsDatabaseAdapter.ClearAllHostname();
40         if (hostInfo.empty()) {
41             WVLOG_I("PreDns no hostinfo.");
42             return;
43         }
44 
45         for (std::vector<std::string>::iterator it = hostInfo.begin(); it != hostInfo.end(); it++) {
46             WVLOG_I("PreDns hostname: %{public}s", (*it).c_str());
47             addrinfo *addrInfo;
48             int error = ::getaddrinfo((*it).c_str(), nullptr, &hints, &addrInfo);
49             if (!addrInfo) {
50                 WVLOG_E("PreDns get address failed, error: %{public}d", error);
51                 continue;
52             }
53             g_AddrInfoMap.emplace(*it, addrInfo);
54         }
55         WVLOG_I("PreDns end");
56     }
57 
58 } // namespace OHOS::NWeb
59