1 /* 2 * Copyright (C) 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 #ifndef OHOS_DHCP_IP6_H 16 #define OHOS_DHCP_IP6_H 17 18 #include <mutex> 19 #include <string> 20 #include <sys/types.h> 21 #include <stdint.h> 22 #ifndef OHOS_ARCH_LITE 23 #include "dhcp_client_def.h" 24 #include "common_timer_errors.h" 25 #include "timer.h" 26 #endif 27 #include "dhcp_thread.h" 28 29 namespace OHOS { 30 namespace DHCP { 31 const int DHCP_INET6_ADDRSTRLEN = 128; 32 33 struct DhcpIpv6Info { 34 char linkIpv6Addr[DHCP_INET6_ADDRSTRLEN]; 35 char globalIpv6Addr[DHCP_INET6_ADDRSTRLEN]; 36 char ipv6SubnetAddr[DHCP_INET6_ADDRSTRLEN]; 37 char randIpv6Addr[DHCP_INET6_ADDRSTRLEN]; 38 char routeAddr[DHCP_INET6_ADDRSTRLEN]; 39 char dnsAddr[DHCP_INET6_ADDRSTRLEN]; 40 char dnsAddr2[DHCP_INET6_ADDRSTRLEN]; 41 char uniqueLocalAddr1[DHCP_INET6_ADDRSTRLEN]; 42 char uniqueLocalAddr2[DHCP_INET6_ADDRSTRLEN]; 43 unsigned int status; // 1 ipv4 getted, 2 dns getted, 3 ipv4 and dns getted 44 std::vector<std::string> vectorDnsAddr; 45 }; 46 47 class DhcpIpv6Client { 48 public: 49 DhcpIpv6Client(std::string ifname); 50 virtual ~DhcpIpv6Client(); 51 52 bool IsRunning(); 53 void SetCallback(std::function<void(const std::string ifname, DhcpIpv6Info &info)> callback); 54 void *DhcpIpv6Start(); 55 void DhcpIPV6Stop(void); 56 void Reset(); 57 void RunIpv6ThreadFunc(); 58 void AddIpv6Address(char *ipv6addr, int len); 59 int StartIpv6(); 60 int StartIpv6Thread(const std::string &ifname, bool isIpv6); 61 #ifndef OHOS_ARCH_LITE 62 void Ipv6TimerCallback(); 63 void StartIpv6Timer(void); 64 void StopIpv6Timer(void); 65 #endif 66 public: 67 private: 68 int32_t createKernelSocket(void); 69 void GetIpv6Prefix(const char* ipv6Addr, char* ipv6PrefixBuf, uint8_t prefixLen); 70 int GetIpFromS6Address(void* addr, int family, char* buf, int buflen); 71 int getAddrScope(const struct in6_addr *addr); 72 int getAddrType(const struct in6_addr *addr); 73 unsigned int ipv6AddrScope2Type(unsigned int scope); 74 void onIpv6DnsAddEvent(void* data, int len, int ifaIndex); 75 void onIpv6RouteAddEvent(char* gateway, char* dst, int ifaIndex); 76 void onIpv6AddressAddEvent(void* data, int prefixLen, int ifaIndex); 77 void setSocketFilter(void* addr); 78 void handleKernelEvent(const uint8_t* data, int len); 79 void parseNdUserOptMessage(void* msg, int len); 80 void ParseAddrMessage(void *msg); 81 void parseNDRouteMessage(void* msg); 82 void parseNewneighMessage(void* msg); 83 void getIpv6RouteAddr(); 84 void fillRouteData(char* buff, int &len); 85 bool IsEui64ModeIpv6Address(char *ipv6addr, int len); 86 void SetAcceptRa(const std::string &content); 87 void PublishIpv6Result(); 88 89 std::mutex ipv6CallbackMutex_; 90 std::function<void(const std::string ifname, DhcpIpv6Info &info)> onIpv6AddressChanged_ { nullptr }; 91 std::string interfaceName; 92 struct DhcpIpv6Info dhcpIpv6Info; 93 int32_t ipv6SocketFd = 0; 94 std::unique_ptr<DhcpThread> ipv6Thread_ = nullptr; 95 std::atomic<bool> runFlag_ { false }; 96 #ifndef OHOS_ARCH_LITE 97 uint64_t ipv6TimerId_ { 0 }; 98 std::mutex ipv6TimerMutex; 99 #endif 100 }; 101 } // namespace DHCP 102 } // namespace OHOS 103 104 #endif