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 INCLUDE_INTERFACE_MANAGER_H
17#define INCLUDE_INTERFACE_MANAGER_H
18
19#include "interface_type.h"
20#include <ostream>
21#include <string>
22#include <vector>
23#include "net/if_arp.h"
24
25namespace OHOS {
26namespace nmd {
27static const uint32_t INTERFACE_ERR_MAX_LEN = 256;
28
29class InterfaceManager {
30public:
31    InterfaceManager() = default;
32    ~InterfaceManager() = default;
33    /**
34     * Set network device mtu
35     *
36     * @param interfaceName Network device name
37     * @param mtuValue Value of mtu
38     * @return Returns 0, set network device mtu successfully, otherwise it will fail
39     */
40    static int SetMtu(const char *interfaceName, const char *mtuValue);
41
42    /**
43     * Get network device mtu
44     *
45     * @param interfaceName Network device name
46     * @return Returns value of mtu
47     */
48    static int GetMtu(const char *interfaceName);
49
50    /**
51     * Add local IP address to network
52     *
53     * @param interfaceName Network device name
54     * @param addr Network IP address
55     * @param prefixLen Length of the network number of the subnet mask
56     * @return Returns 0, add local IP address to network successfully, otherwise it will fail
57     */
58    static int AddAddress(const char *interfaceName, const char *addr, int prefixLen);
59
60    /**
61     * Delete local IP address to network
62     *
63     * @param interfaceName Network device name
64     * @param addr Network IP address
65     * @param prefixLen Length of the network number of the subnet mask
66     * @return Returns 0, delete local IP address to network successfully, otherwise it will fail
67     */
68    static int DelAddress(const char *interfaceName, const char *addr, int prefixLen);
69
70    /**
71     * Delete local IP address to network
72     *
73     * @param interfaceName Network device name
74     * @param addr Network IP address
75     * @param prefixLen Length of the network number of the subnet mask
76     * @param netCapabilities Net capabilities in string format
77     * @return Returns 0, delete local IP address to network successfully, otherwise it will fail
78     */
79    static int DelAddress(const char *interfaceName, const char *addr, int prefixLen,
80                          const std::string &netCapabilities);
81
82    /**
83     * Get the network interface names
84     *
85     * @return Network interface names
86     */
87    static std::vector<std::string> GetInterfaceNames();
88
89    /**
90     * Get the network interface config
91     *
92     * @param ifName Network device name
93     * @return Interface configuration parcel
94     */
95    static InterfaceConfigurationParcel GetIfaceConfig(const std::string &ifName);
96
97    /**
98     * Set network interface config
99     *
100     * @param ifaceConfig Interface configuration parcel
101     * @return Returns 1, set network interface config successfully, otherwise it will fail
102     */
103    static int SetIfaceConfig(const nmd::InterfaceConfigurationParcel &ifaceConfig);
104
105    /**
106     * Set network interface ip address
107     *
108     * @param ifaceName Network port device name
109     * @param ipAddress Ip address
110     * @return Returns 0, set IP address to network successfully, otherwise it will fail
111     */
112    static int SetIpAddress(const std::string &ifaceName, const std::string &ipAddress);
113
114    /**
115     * Set iface up
116     *
117     * @param ifaceName Network port device name
118     * @return Returns 0, set up to network successfully, otherwise it will fail
119     */
120    static int SetIffUp(const std::string &ifaceName);
121    static int32_t AddStaticArp(const std::string &ipAddr, const std::string &macAddr,
122                                const std::string &ifName);
123    static int32_t DelStaticArp(const std::string &ipAddr, const std::string &macAddr,
124                                const std::string &ifName);
125
126private:
127    static int ModifyAddress(uint32_t action, const char *interfaceName, const char *addr, int prefixLen);
128    static int32_t AssembleArp(const std::string &ipAddr, const std::string &macAddr,
129                               const std::string &ifName, arpreq &req);
130    static int32_t MacStringToArray(const std::string &macAddr, sockaddr &macSock);
131};
132} // namespace nmd
133} // namespace OHOS
134#endif // INCLUDE_INTERFACE_MANAGER_H
135