1 /*
2  * Copyright (C) 2021-2022 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_ADDRESS_POOL_H
17 #define OHOS_DHCP_ADDRESS_POOL_H
18 
19 #include <stdint.h>
20 #include <map>
21 #include "dhcp_binding.h"
22 #include "dhcp_s_define.h"
23 #include "dhcp_option.h"
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 typedef struct DhcpAddressPool DhcpAddressPool;
29 typedef AddressBinding *(*QueryBind)(uint8_t macAddr[DHCP_HWADDR_LENGTH], PDhcpOptionList cliOptins);
30 typedef AddressBinding *(*AddBind)(uint8_t macAddr[DHCP_HWADDR_LENGTH], PDhcpOptionList cliOptins);
31 typedef uint32_t (*Distribute)(DhcpAddressPool *pool, uint8_t macAddr[DHCP_HWADDR_LENGTH]);
32 
33 typedef struct {
34     uint32_t beginAddress;
35     uint32_t endAddress;
36 } IpAddressRange;
37 
38 struct DhcpAddressPool {
39     char ifname[IFACE_NAME_SIZE];
40     uint32_t netmask;
41     IpAddressRange addressRange;
42     uint32_t serverId;
43     uint32_t gateway;
44     uint32_t leaseTime;
45     uint32_t renewalTime;
46     uint32_t rebindingTime;
47     uint32_t distribution;
48     Distribute distribue;
49     QueryBind binding;
50     AddBind newBinding;
51     std::map<uint32_t, AddressBinding> leaseTable;
52     DhcpOptionList fixedOptions;
53     int initialized;
54 };
55 
56 int InitAddressPool(DhcpAddressPool *pool, const char *ifname, PDhcpOptionList options);
57 void FreeAddressPool(DhcpAddressPool *pool);
58 AddressBinding *FindBindingByIp(uint32_t bingdingIp);
59 int IsReserved(uint8_t macAddr[DHCP_HWADDR_LENGTH]);
60 int IsReservedIp(DhcpAddressPool *pool, uint32_t ipAddress);
61 int AddBinding(AddressBinding *binding);
62 int AddReservedBinding(uint8_t macAddr[DHCP_HWADDR_LENGTH]);
63 int RemoveReservedBinding(uint8_t macAddr[DHCP_HWADDR_LENGTH]);
64 int RemoveBinding(uint8_t macAddr[DHCP_HWADDR_LENGTH]);
65 int ReleaseBinding(uint8_t macAddr[DHCP_HWADDR_LENGTH]);
66 int AddLease(DhcpAddressPool *pool, AddressBinding *lease);
67 AddressBinding *GetLease(DhcpAddressPool *pool, uint32_t ipAddress);
68 int UpdateLease(DhcpAddressPool *pool, AddressBinding *lease);
69 int RemoveLease(DhcpAddressPool *pool, AddressBinding *lease);
70 int LoadBindingRecoders(DhcpAddressPool *pool);
71 int SaveBindingRecoders(const DhcpAddressPool *pool, int force);
72 AddressBinding *QueryBinding(uint8_t macAddr[DHCP_HWADDR_LENGTH], PDhcpOptionList cliOptins);
73 void SetDistributeMode(int mode);
74 int GetDistributeMode(void);
75 int DeleteMacInLease(DhcpAddressPool *pool, AddressBinding *lease);
76 
77 #ifdef __cplusplus
78 }
79 #endif
80 #endif
81