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 INCLUDE_DNS_PROXY_LISTEN_H 17 #define INCLUDE_DNS_PROXY_LISTEN_H 18 19 #include <iostream> 20 #include <netinet/in.h> 21 #include <vector> 22 23 namespace OHOS { 24 namespace nmd { 25 class DnsProxyListen { 26 public: 27 DnsProxyListen(); 28 ~DnsProxyListen(); 29 30 /** 31 * Begin dns proxy listen 32 * 33 */ 34 void OnListen(); 35 36 /** 37 * Close dns proxy listen 38 */ 39 void OffListen(); 40 41 /** 42 * Dns proxy listen obj 43 * 44 */ 45 void StartListen(); 46 47 /** 48 * Set the Parse Net Id objectse 49 * 50 * @param netId network ID 51 */ 52 void SetParseNetId(uint16_t netId); 53 54 private: 55 static constexpr const uint32_t MAX_REQUESTDATA_LEN = 512; 56 struct RecvBuff { 57 char questionsBuff[MAX_REQUESTDATA_LEN]; 58 int32_t questionLen; 59 }; 60 static void DnsProxyGetPacket(int32_t clientSocket, RecvBuff recvBuff, sockaddr_in proxyAddr); 61 static void DnsParseBySocket(int32_t clientSocket, std::vector<std::string> servers, RecvBuff recvBuff, 62 sockaddr_in proxyAddr); 63 static void DnsSendRecvParseData(int32_t clientSocket, char *requestData, int32_t resLen, sockaddr_in proxyAddr); 64 static bool CheckDnsResponse(char* recBuff, size_t recLen); 65 static bool DnsThreadClose(); 66 int32_t proxySockFd_; 67 static uint16_t netId_; 68 static bool proxyListenSwitch_; 69 }; 70 } // namespace nmd 71 } // namespace OHOS 72 #endif // INCLUDE_DNS_PROXY_LISTEN_H 73