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