• 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 #ifndef DNS_LOOKUP_NAME_H
17 #define DNS_LOOKUP_NAME_H
18 
19 #include <arpa/inet.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <features.h>
24 #include <netdb.h>
25 #include <netinet/in.h>
26 #include <resolv.h>
27 #include <stddef.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/socket.h>
33 #include <unistd.h>
34 
35 #include "dns_lookup_parse.h"
36 #include "netnative_log_wrapper.h"
37 
38 namespace OHOS {
39 namespace nmd {
40 static constexpr int32_t PORT_NUM = 65535;
41 static constexpr int32_t SECOND_ADDR_IN_BUFF = 2;
42 static constexpr int32_t CANON_LINE = 256;
43 static constexpr int32_t ADDR_FIRST_BIT = 1;
44 static constexpr int32_t MAXSERVS = 2;
45 
46 struct AddrInfoBuf {
47     addrinfo ai;
48     union sa {
49         struct sockaddr_in sin;
50         struct sockaddr_in6 sin6;
51     } sa;
52     volatile int32_t lock[1];
53     int16_t slot;
54     int16_t ref;
55 };
56 
57 struct ScokAddrCopy {
58     int32_t lookUpNameFd;
59     sockaddr_in6 sa6;
60     sockaddr_in6 da6;
61     sockaddr_in sa4;
62     socklen_t saLen;
63     socklen_t daLen;
64     int32_t dLabel;
65     int32_t family;
66 };
67 
68 class DnsLookUpName {
69 public:
70     DnsLookUpName() = default;
71     ~DnsLookUpName() = default;
72 
73     /**
74      * look up server
75      *
76      * @param buf is search server fo port proto sockType
77      * @param name hostname
78      * @param proto socket protocol
79      * @param socktype socket type
80      * @param flags how to deal with ip or hostname
81      * @return int32_t 0-success or cnt num, <0-failed
82      */
83     int32_t LookUpServer(ServData buf[MAXSERVS], const std::string name, int32_t proto, int32_t socktype,
84                          int32_t flags);
85 
86     /**
87      * look up name or ip
88      *
89      * @param buf is search name's data
90      * @param canon
91      * @param name host name
92      * @param family address family
93      * @param flags how to deal with ip or hostname
94      * @param netId network ID
95      * @return int 0-success <0-failed
96      */
97     int32_t LookUpName(AddrData buf[MAXADDRS], char canon[CANON_LINE], const std::string &name, int32_t family,
98                        int32_t flags, uint16_t netId);
99 
100 private:
101     static int32_t NameFromNull(AddrData buf[SECOND_ADDR_IN_BUFF], const std::string name, int32_t family,
102                                 int32_t flags);
103     static int32_t NameFromNumeric(AddrData buf[ADDR_FIRST_BIT], const std::string name, int32_t family);
104     static int32_t NameFromDnsSearch(AddrData buf[MAXADDRS], char canon[CANON_LINE], const std::string name,
105                                      int32_t family, uint16_t netId);
106     static int32_t NameFromDns(AddrData buf[MAXADDRS], char canon[CANON_LINE], const std::string name, int32_t family,
107                                const ResolvConf *conf, uint16_t netId);
108     static const struct policy *PolicyOf(const in6_addr *in6Addr);
109     static int32_t LabelOf(const in6_addr *in6Addr);
110     static int32_t ScopeOf(const in6_addr *in6Addr);
111     static int32_t PreFixMatch(const in6_addr *s, const in6_addr *d);
112     static int32_t AddrCmp(const void *addrA, const void *addrB);
113     static int32_t SwitchSocketType(int32_t socktype, const std::string name, int32_t &proto, ServData *buf);
114     static int32_t CheckNameParam(const std::string name, int32_t &flags, int32_t &family, char *canon);
115     static bool UpdateBuf(int32_t flags, int32_t family, AddrData *buf, int32_t &cnt);
116     static void RefreshBuf(AddrData *buf, int32_t num, int32_t &cnt);
117     void SockAddrCopy(ScokAddrCopy addrBuff, void *da, void *sa, int32_t &dScope, int32_t &preFixLen, uint32_t &key);
118     static int32_t FindName(AddrData *buf, char *canon, const std::string name, int32_t family, int32_t flags,
119                             uint16_t netId);
120     void LookUpNameParam(AddrData *buf, int32_t cnt, int32_t netId);
121     int32_t MemcpySockaddr(sockaddr_in6 &sa6, sockaddr_in6 &da6, sockaddr_in &da4, AddrData *buf, uint32_t cnt);
122 };
123 } // namespace nmd
124 } // namespace OHOS
125 #endif // DNS_LOOKUP_NAME_H
126