• 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 COMMUNICATION_NETMANAGER_BASE_DNS_CONFIG_CLIENT_H
17 #define COMMUNICATION_NETMANAGER_BASE_DNS_CONFIG_CLIENT_H
18 
19 #include <arpa/inet.h>
20 #include <netdb.h>
21 #include <stdint.h>
22 
23 #include "securec.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #define MAX_SERVER_NUM 5
30 #define MAX_SERVER_NUM_EXT 8
31 #define MAX_SERVER_LENGTH 50
32 #define DNS_SOCKET_PATH "/dev/unix/socket/dnsproxyd"
33 #define DNS_SOCKET_NAME "dnsproxyd"
34 #define MAX_RESULTS 32
35 #define MAX_CANON_NAME 256
36 #define MAX_HOST_NAME_LEN 256
37 #define MAX_KEY_LENGTH (MAX_HOST_NAME_LEN + 1 + MAX_SERVER_LENGTH + 1 + sizeof(uint32_t) * 4 + 3 + 1)
38 #define DEFAULT_TIMEOUT 5000
39 #define DEFAULT_RETRY 2
40 #define DEFAULT_SERVER_LENTH 16
41 #define DEFAULT_SERVER_NAME 114
42 
43 #define MAX_DNS_CACHE_SIZE 50
44 #define MIN_DNS_REPORT_PERIOD 10 // 10s
45 #define LOOP_BACK_ADDR1 "127.0.0.1"
46 #define LOOP_BACK_ADDR2 "0.0.0.0"
47 #define MIN_REPORT_INTERVAL 1
48 #define MIN_QUERY_REPORT_INTERVAL 5 // MIN STATISTIC DNS REPORT INTERVAL
49 #define FIRST_RETURN_SLOW_THRESHOLD 500
50 #define QUERY_CALLBACK_RETURN_SLOW_THRESHOLD 1500
51 #define FAIL_CAUSE_REPORT_INTERVAL (15 * 60)
52 #define MIN_APP_UID 100000
53 #define UID_PUSH 7023
54 #define UID_ACCOUNT 7008
55 #define TOTAL_FAIL_CAUSE_COUNT 8
56 #define FAIL_CAUSE_NONE 0
57 #define FAIL_CAUSE_QUERY_FAIL 1
58 #define FAIL_CAUSE_FIRST_RETURN_SLOW 2
59 #define FAIL_CAUSE_CALLBACK_RETURN_SLOW 3
60 #define FAIL_CAUSE_USE_BACKUP_DNS_SERVER 4
61 #define FAIL_CAUSE_RETURN_LOOPBACK_ADDR 5
62 #define FAIL_CAUSE_RETURN_CNAME 6
63 #define FAIL_CAUSE_RETURN_NO_ANSWER 7
64 #define FAIL_CAUSE_INTERFACE_NOT_DEFAULT 8
65 
66 enum CommandType {
67     GET_CONFIG = 1,
68     GET_CACHE = 2,
69     SET_CACHE = 3,
70     JUDGE_IPV6 = 4,
71     POST_DNS_RESULT = 5,
72     GET_DEFAULT_NETWORK = 6,
73     BIND_SOCKET = 7,
74     POST_DNS_QUERY_RESULT = 8,    // for musl and c-ares
75     POST_DNS_ABNORMAL_RESULT = 9, // for musl and c-ares
76     GET_CONFIG_EXT = 10, // for musl and c-ares
77 };
78 
79 struct RequestInfo {
80     uint32_t uid;
81     uint32_t command;
82     uint32_t netId;
83 };
84 
85 struct ResolvConfig {
86     int32_t error;
87     int32_t timeoutMs;
88     uint32_t retryCount;
89     uint32_t nonPublicNum;
90     char nameservers[MAX_SERVER_NUM][MAX_SERVER_LENGTH + 1];
91 };
92 
93 struct ResolvConfigExt {
94     int32_t error;
95     int32_t timeoutMs;
96     uint32_t retryCount;
97     uint32_t nonPublicNum;
98     char nameservers[MAX_SERVER_NUM_EXT][MAX_SERVER_LENGTH + 1];
99 };
100 
101 typedef union {
102     struct sockaddr sa;
103     struct sockaddr_in6 sin6;
104     struct sockaddr_in sin;
105 } AlignedSockAddr;
106 
107 struct AddrInfo {
108     uint32_t aiFlags;
109     uint32_t aiFamily;
110     uint32_t aiSockType;
111     uint32_t aiProtocol;
112     uint32_t aiAddrLen;
113     AlignedSockAddr aiAddr;
114     char aiCanonName[MAX_CANON_NAME + 1];
115 };
116 
117 struct ParamWrapper {
118     char *host;
119     char *serv;
120     struct addrinfo *hint;
121 };
122 
123 typedef int32_t (*FuncNetDnsqueryHook)(int32_t, int32_t, int32_t);
124 
125 struct QueryParam {
126     int32_t type;
127     int32_t netId;
128     int32_t mark;
129     int32_t flags;
130     FuncNetDnsqueryHook qHook;
131 };
132 
133 struct FamilyQueryInfo {
134     int32_t retCode;
135     char *serverAddr;
136     uint8_t isNoAnswer;
137     uint8_t cname;
138 };
139 
140 struct FamilyQueryInfoExt {
141     int32_t retCode;
142     char serverAddr[MAX_SERVER_LENGTH + 1];
143     uint8_t isNoAnswer;
144     uint8_t cname;
145 };
146 
147 struct DnsProcessInfo {
148     long long queryTime;
149     char *hostname;
150     int32_t retCode;
151     uint32_t firstQueryEndDuration;
152     uint32_t firstQueryEnd2AppDuration;
153     uint16_t firstReturnType; /* a or aaaa */
154     uint8_t isFromCache;
155     uint8_t sourceFrom;
156     struct FamilyQueryInfo ipv4QueryInfo;
157     struct FamilyQueryInfo ipv6QueryInfo;
158 };
159 
160 struct DnsProcessInfoExt {
161     long long queryTime;
162     char hostname[MAX_HOST_NAME_LEN + 1];
163     char srcAddr[MAX_SERVER_LENGTH + 1];
164     int32_t retCode;
165     uint32_t firstQueryEndDuration;
166     uint32_t firstQueryEnd2AppDuration;
167     uint16_t firstReturnType; /* a or aaaa */
168     uint8_t isFromCache;
169     uint8_t sourceFrom;
170     struct FamilyQueryInfoExt ipv4QueryInfo;
171     struct FamilyQueryInfoExt ipv6QueryInfo;
172 };
173 
174 struct PostDnsQueryParam {
175     uint32_t netId;
176     uint32_t uid;
177     uint32_t pid;
178     uint8_t addrSize;
179     struct DnsProcessInfoExt processInfo;
180 };
181 
182 struct DnsCacheInfo {
183     uint8_t addrSize;
184     struct DnsProcessInfoExt dnsProcessInfo;
185     struct AddrInfo addrInfo[MAX_RESULTS];
186 };
187 #ifdef __cplusplus
188 }
189 #endif
190 #endif // COMMUNICATION_NETMANAGER_BASE_1_DNS_CONFIG_CLIENT_H
191