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_IP6_H 16 #define OHOS_IP6_H 17 18 #include <string> 19 #include <sys/types.h> 20 #include <stdint.h> 21 22 namespace OHOS { 23 namespace Wifi { 24 const int DHCP_INET6_ADDRSTRLEN = 128; 25 26 struct DhcpIpv6Info { 27 char linkIpv6Addr[DHCP_INET6_ADDRSTRLEN]; 28 char globalIpv6Addr[DHCP_INET6_ADDRSTRLEN]; 29 char ipv6SubnetAddr[DHCP_INET6_ADDRSTRLEN]; 30 char randIpv6Addr[DHCP_INET6_ADDRSTRLEN]; 31 char routeAddr[DHCP_INET6_ADDRSTRLEN]; 32 char dnsAddr[DHCP_INET6_ADDRSTRLEN]; 33 char dnsAddr2[DHCP_INET6_ADDRSTRLEN]; 34 int status; // 1 ipv4 getted, 2 dns getted, 3 ipv4 and dns getted 35 }; 36 37 class DhcpIpv6Client { 38 public: DhcpIpv6Client()39 DhcpIpv6Client() 40 { 41 interfaceName = ""; 42 } 43 void *DhcpIpv6Start(const char* param); 44 void DhcpIPV6Stop(void); 45 int StartIpv6(const char *ifname); IsRunning()46 bool IsRunning() 47 { 48 return runFlag; 49 } SetCallback(std::function<void (const std::string ifname,DhcpIpv6Info & info)> callback)50 void SetCallback(std::function<void(const std::string ifname, DhcpIpv6Info &info)> callback) 51 { 52 onIpv6AddressChanged = callback; 53 } 54 void Reset(); 55 private: 56 int32_t createKernelSocket(void); 57 void GetIpv6Prefix(const char* ipv6Addr, char* ipv6PrefixBuf, uint8_t prefixLen); 58 int GetIpFromS6Address(void* addr, int family, char* buf, int buflen); 59 int getAddrScope(const struct in6_addr *addr); 60 int getAddrType(const struct in6_addr *addr); 61 unsigned int ipv6AddrScope2Type(unsigned int scope); 62 void onIpv6DnsAddEvent(void* data, int len, int ifaIndex); 63 void onIpv6RouteAddEvent(char* gateway, char* dst, int ifaIndex); 64 void onIpv6AddressAddEvent(void* data, int prefixLen, int ifaIndex); 65 void setSocketFilter(void* addr); 66 void handleKernelEvent(const uint8_t* data, int len); 67 void parseNdUserOptMessage(void* msg, int len); 68 void parseNDRouteMessage(void* msg); 69 void parseNewneighMessage(void* msg); 70 void getIpv6RouteAddr(); 71 void fillRouteData(char* buff, int &len); 72 73 std::function<void(const std::string ifname, DhcpIpv6Info &info)> onIpv6AddressChanged; 74 std::string interfaceName; 75 bool runFlag = false; 76 struct DhcpIpv6Info dhcpIpv6Info; 77 int32_t ipv6SocketFd = 0; 78 }; 79 } // namespace Wifi 80 } // namespace OHOS 81 82 #endif