1 /* 2 * Copyright (c) 2024 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 NETSYS_DNS_PROXY_REQUEST_SOCKET_H 17 #define NETSYS_DNS_PROXY_REQUEST_SOCKET_H 18 19 #include "dns_config_client.h" 20 #include <chrono> 21 #include <cstdint> 22 #include <cstddef> 23 #include <list> 24 #include <map> 25 #include <memory> 26 #include <netinet/ip.h> 27 #include <sys/epoll.h> 28 29 namespace OHOS::nmd { 30 static constexpr uint32_t MAX_REQUESTDATA_LEN = 512; 31 static constexpr int32_t EPOLL_TIMEOUT = 3000; 32 33 struct RecvBuff { 34 char questionsBuff[MAX_REQUESTDATA_LEN]; 35 int32_t questionLen; 36 }; 37 38 class DnsProxyRequestSocket final { 39 public: 40 DnsProxyRequestSocket(int32_t sock, std::unique_ptr<AlignedSockAddr> &&clientSock, 41 std::unique_ptr<RecvBuff> &&recvBuff); 42 DnsProxyRequestSocket(const DnsProxyRequestSocket &) = delete; 43 DnsProxyRequestSocket &operator=(const DnsProxyRequestSocket &) = delete; 44 DnsProxyRequestSocket(DnsProxyRequestSocket &&other) = delete; 45 DnsProxyRequestSocket &operator=(DnsProxyRequestSocket &&other) = delete; 46 [[nodiscard]] int32_t GetSock() const; 47 [[nodiscard]] size_t GetIdx() const; 48 [[nodiscard]] AlignedSockAddr &GetAddr(); 49 void IncreaseIdx(); 50 void ResetIdx(); 51 epoll_event *GetEventPtr(); 52 AlignedSockAddr &GetClientSock(); 53 RecvBuff &GetRecvBuff(); 54 void SetLruIterator(std::list<std::map<int32_t, DnsProxyRequestSocket>::iterator>::iterator iterator); 55 [[nodiscard]] std::list<std::map<int32_t, DnsProxyRequestSocket>::iterator>::iterator GetLruIterator() const; 56 ~DnsProxyRequestSocket(); 57 std::chrono::system_clock::time_point endTime; 58 59 private: 60 int32_t sock; 61 size_t dnsServerIdx = 0; 62 epoll_event event{}; 63 AlignedSockAddr addrParse{}; 64 std::unique_ptr<AlignedSockAddr> clientSock; 65 std::unique_ptr<RecvBuff> recvBuff; 66 std::list<std::map<int32_t, DnsProxyRequestSocket>::iterator>::iterator lruIterator; 67 }; 68 } // namespace OHOS::nmd 69 #endif // NETSYS_DNS_PROXY_REQUEST_SOCKET_H 70