• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-2023 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_PARSE_H
17 #define DNS_LOOKUP_PARSE_H
18 
19 #include <arpa/inet.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <features.h>
23 #include <limits.h>
24 #include <netdb.h>
25 #include <netinet/in.h>
26 #include <poll.h>
27 #include <stddef.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <syscall.h>
33 #include <sys/socket.h>
34 #include <time.h>
35 #include <unistd.h>
36 
37 #include "dns_param_cache.h"
38 #include "netnative_log_wrapper.h"
39 #include "securec.h"
40 #include "singleton.h"
41 
42 namespace OHOS {
43 namespace nmd {
44 static constexpr int32_t LOOKUP_NAME_ZERO = 0;
45 static constexpr int32_t LOOKUP_NAME_ONE = 1;
46 
47 static constexpr int32_t MAXADDRS = 48;
48 static constexpr int32_t HOSTS_MAXNS = 3;
49 
50 static constexpr int32_t SERVER_FAILURE = 0;
51 static constexpr int32_t BUFF_MAX_LEN = 280;
52 static constexpr int32_t PACKET_LINE = 512;
53 
54 static constexpr int32_t RR_A = 1;
55 static constexpr int32_t RR_AAAA = 28;
56 
57 static constexpr int32_t HOST_MAX_LEN = 255;
58 static constexpr int32_t HOST_MAX_LEN_MINUS_ONE = 254;
59 static constexpr int32_t HOST_MAX_LEN_MINUS_TWO = 253;
60 
61 static constexpr int32_t ADDR_A4_LEN = 4;
62 static constexpr int32_t ADDR_A6_LEN = 16;
63 
64 static constexpr int32_t FAMILY_TYPE = 2;
65 
66 static constexpr uint8_t DOT = '.';
67 static constexpr const char *ADDR_BUF = "\0\0\0\0\0\0\0\0\0\0\xff\xff";
68 
69 struct GetAnswers {
70     int32_t queriesNum;
71     int32_t answersSize;
72     uint32_t nns;
73     int32_t fd;
74     int32_t attempts;
75     int32_t timeOut;
76     socklen_t saLen;
77 };
78 
79 enum {
80     ARG_INDEX_0 = 0,
81     ARG_INDEX_1,
82     ARG_INDEX_2,
83     ARG_INDEX_3,
84     ARG_INDEX_4,
85     ARG_INDEX_5,
86     ARG_INDEX_6,
87     ARG_INDEX_7,
88     ARG_INDEX_8,
89     ARG_INDEX_9,
90 };
91 
92 struct AddrData {
93     int32_t family;
94     uint32_t scopeid;
95     uint8_t addr[ADDR_A6_LEN];
96     int32_t sortKey;
97 };
98 
99 struct ServData {
100     uint16_t port;
101     uint8_t proto;
102     uint8_t sockType;
103 };
104 
105 struct ResolvConf {
106     AddrData ns[HOSTS_MAXNS];
107     uint32_t nns;
108     uint32_t attempts;
109     uint32_t nDots;
110     uint32_t timeOut;
111 };
112 
113 struct DpcCtx {
114     AddrData *addrs;
115     char *canon;
116     int32_t cnt;
117 };
118 
119 enum DnsResultCode {
120     DNS_ERR_NONE = 0,
121     DNS_ERR_INTERNAL = -1,
122 };
123 
124 union {
125     sockaddr_in sin;
126     sockaddr_in6 sin6;
127 } sockAddr = {{0}}, nSockAddr[HOSTS_MAXNS] = {{{0}}};
128 
129 class DnsLookUpParse {
130 public:
131     DnsLookUpParse() = default;
132     ~DnsLookUpParse() = default;
133 
134     int32_t LookupIpLiteral(struct AddrData buf[ARG_INDEX_1], const std::string name, int32_t family);
135     int32_t GetResolvConf(struct ResolvConf *conf, char *search, size_t search_sz, uint16_t netId);
136     int32_t ResMSendRc(int32_t queriesNum, const uint8_t *const *queries, const int *qlens, uint8_t *const *answers,
137                        int32_t *alens, int32_t asize, const ResolvConf *conf, uint16_t netId);
138     static int32_t DnsParse(const uint8_t *, int32_t, int32_t (*)(void *, int32_t, const void *, int32_t, const void *),
139                             void *);
140     int32_t DnsExpand(const uint8_t *base, const uint8_t *end, const uint8_t *src, char *dest, int32_t space);
141     int32_t ResMkQuery(int32_t op, const std::string dname, int32_t mineClass, int32_t type, const uint8_t *data,
142                        int32_t datalen, const uint8_t *newrr, uint8_t *buf, int32_t buflen);
143     static int32_t IsValidHostname(const std::string host);
144     static int32_t DnsParseCallback(void *c, int32_t rr, const void *data, int32_t len, const void *packet);
145 
146 private:
147     static uint64_t mTime();
148     static void GetNsFromConf(const ResolvConf *conf, uint32_t &nns, int32_t &family, socklen_t &saLen);
149     void SetSocAddr(int32_t fd, uint32_t &nns);
150     void SearchNameServer(GetAnswers *getAnswers, int32_t *answersLens, const uint8_t *const *queries,
151                           const int32_t *queriesLens);
152     int32_t DnsGetAnswers(GetAnswers getAnswers, const uint8_t *const *queries, const int32_t *queriesLens,
153                           uint8_t *const *answers, int32_t *answersLens, int32_t servFailRetry);
154     int32_t DnsSendQueries(GetAnswers getAnswers, const uint8_t *const *queries, const int32_t *queriesLens,
155                            uint8_t *const *answers, int32_t *answersLens);
156     int32_t LookupIpLiteralForIPV6(const char *hostName, int32_t &family, struct AddrData buf[ARG_INDEX_1]);
157 };
158 } // namespace nmd
159 } // namespace OHOS
160 #endif // DNS_LOOKUP_PARSE_H
161