• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 OHOS_DHCP_IPV6_INFO_H
17 #define OHOS_DHCP_IPV6_INFO_H
18 
19 #include <string>
20 #include <vector>
21 #include <map>
22 #include "securec.h"
23 namespace OHOS {
24 namespace DHCP {
25 inline const int DHCP_INET6_ADDRSTRLEN = 128;
26 inline const int DNS_DEFAULT_LIFETIME = 3600; // default lifetime for DNS server
27 enum AddrType {
28     UNKNOW = -1,
29     DEFAULT = 0,
30     GLOBAL,
31     SUBNET,
32     RAND,
33     UNIQUE,
34     UNIQUE2
35 };
36 struct DhcpIpv6Info {
37     char linkIpv6Addr[DHCP_INET6_ADDRSTRLEN];
38     char globalIpv6Addr[DHCP_INET6_ADDRSTRLEN];
39     char ipv6SubnetAddr[DHCP_INET6_ADDRSTRLEN];
40     char randIpv6Addr[DHCP_INET6_ADDRSTRLEN];
41     char routeAddr[DHCP_INET6_ADDRSTRLEN];
42     char dnsAddr[DHCP_INET6_ADDRSTRLEN];
43     char dnsAddr2[DHCP_INET6_ADDRSTRLEN];
44     char uniqueLocalAddr1[DHCP_INET6_ADDRSTRLEN];
45     char uniqueLocalAddr2[DHCP_INET6_ADDRSTRLEN];
46     unsigned int status;   // 1 ipv4 getted, 2 dns getted, 3 ipv4 and dns getted
47     std::vector<std::string> vectorDnsAddr;
48     uint64_t lifetime {DNS_DEFAULT_LIFETIME}; // min life time of valid lifetime & preferred lifetime
49     std::vector<std::string> defaultRouteAddr {};
50     std::map<std::string, int> IpAddrMap {};
ClearDhcpIpv6Info51     void Clear()
52     {
53         memset_s(linkIpv6Addr, DHCP_INET6_ADDRSTRLEN, 0, DHCP_INET6_ADDRSTRLEN);
54         memset_s(globalIpv6Addr, DHCP_INET6_ADDRSTRLEN, 0, DHCP_INET6_ADDRSTRLEN);
55         memset_s(ipv6SubnetAddr, DHCP_INET6_ADDRSTRLEN, 0, DHCP_INET6_ADDRSTRLEN);
56         memset_s(randIpv6Addr, DHCP_INET6_ADDRSTRLEN, 0, DHCP_INET6_ADDRSTRLEN);
57         memset_s(routeAddr, DHCP_INET6_ADDRSTRLEN, 0, DHCP_INET6_ADDRSTRLEN);
58         memset_s(dnsAddr, DHCP_INET6_ADDRSTRLEN, 0, DHCP_INET6_ADDRSTRLEN);
59         memset_s(dnsAddr2, DHCP_INET6_ADDRSTRLEN, 0, DHCP_INET6_ADDRSTRLEN);
60         memset_s(uniqueLocalAddr1, DHCP_INET6_ADDRSTRLEN, 0, DHCP_INET6_ADDRSTRLEN);
61         memset_s(uniqueLocalAddr2, DHCP_INET6_ADDRSTRLEN, 0, DHCP_INET6_ADDRSTRLEN);
62         status = 0;
63         vectorDnsAddr.clear();
64         lifetime = DNS_DEFAULT_LIFETIME;
65         defaultRouteAddr.clear();
66         IpAddrMap.clear();
67     }
68 };
69 
70 struct DnsServerEntry {
71     std::string address;  // dns server
72     uint64_t expiry;      // expiry time (ms)
73 };
74 class DhcpIpv6InfoManager {
75 public:
76     static bool AddRoute(DhcpIpv6Info &dhcpIpv6Info, std::string defaultRoute);
77     static bool RemoveRoute(DhcpIpv6Info &dhcpIpv6Info, std::string defaultRoute);
78     static bool UpdateAddr(DhcpIpv6Info &dhcpIpv6Info, std::string addr, AddrType type);
79     static bool RemoveAddr(DhcpIpv6Info &dhcpIpv6Info, std::string addr);
80 };
81 }  // namespace DHCP
82 }  // namespace OHOS
83 #endif /* OHOS_DHCP_IPV6_DEFINE_H */
84