• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <thread>
23 #include "dhcp_ipv6_dns_repository.h"
24 namespace OHOS {
25 namespace DHCP {
26 
27 class DhcpIpv6Client {
28 public:
29     DhcpIpv6Client(std::string ifname);
30     virtual ~DhcpIpv6Client();
31 
32     bool IsRunning();
33     void SetCallback(std::function<void(const std::string ifname, DhcpIpv6Info &info)> callback);
34     void *DhcpIpv6Start();
35     void DhcpIPV6Stop(void);
36     void Reset();
37     void RunIpv6ThreadFunc();
38     int StartIpv6();
39     int StartIpv6Thread(const std::string &ifname, bool isIpv6);
40 public:
41 private:
42     int32_t createKernelSocket(void);
43     void GetIpv6Prefix(const char* ipv6Addr, char* ipv6PrefixBuf, uint8_t prefixLen);
44     int GetIpFromS6Address(void* addr, int family, char* buf, int buflen);
45     int GetAddrScope(void *addr);
46     int GetAddrType(const struct in6_addr *addr);
47     AddrType AddIpv6Address(char *ipv6addr, int len);
48     unsigned int ipv6AddrScope2Type(unsigned int scope);
49     void onIpv6DnsAddEvent(void* data, int len, int ifaIndex);
50     void OnIpv6RouteUpdateEvent(char* gateway, char* dst, int ifaIndex, bool isAdd = true);
51     void OnIpv6AddressUpdateEvent(char *addr, int addrlen, int prefixLen,
52                                 int ifaIndex, int scope, bool isUpdate);
53     void setSocketFilter(void* addr);
54     void handleKernelEvent(const uint8_t* data, int len);
55     void parseNdUserOptMessage(void* msg, int len);
56     void ParseAddrMessage(void *msg);
57     void parseNDRouteMessage(void* msg);
58     void parseNewneighMessage(void* msg);
59     void getIpv6RouteAddr();
60     void fillRouteData(char* buff, int &len);
61     bool IsEui64ModeIpv6Address(const char *ipv6addr, int len, const unsigned char *ifaceMac, int macLen);
62     void SetAcceptRa(const std::string &content);
63     void PublishIpv6Result();
64     bool IsGlobalIpv6Address(const char *ipv6addr, int len);
65     bool IsUniqueLocalIpv6Address(const char *ipv6addr, int len);
66 
67     std::mutex ipv6CallbackMutex_;
68     std::function<void(const std::string ifname, DhcpIpv6Info &info)> onIpv6AddressChanged_ { nullptr };
69     std::string interfaceName;
70     struct DhcpIpv6Info dhcpIpv6Info;
71     int32_t ipv6SocketFd = 0;
72     std::unique_ptr<std::thread> ipv6Thread_ = nullptr;
73     std::atomic<bool> runFlag_ { false };
74     std::unique_ptr<DnsServerRepository> dhcpIpv6DnsRepository_ = nullptr;
75 };
76 }  // namespace DHCP
77 }  // namespace OHOS
78 
79 #endif