1518678f8Sopenharmony_ci/*
2518678f8Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
3518678f8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4518678f8Sopenharmony_ci * you may not use this file except in compliance with the License.
5518678f8Sopenharmony_ci * You may obtain a copy of the License at
6518678f8Sopenharmony_ci *
7518678f8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8518678f8Sopenharmony_ci *
9518678f8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10518678f8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11518678f8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12518678f8Sopenharmony_ci * See the License for the specific language governing permissions and
13518678f8Sopenharmony_ci * limitations under the License.
14518678f8Sopenharmony_ci */
15518678f8Sopenharmony_ci
16518678f8Sopenharmony_ci#ifndef OHOS_DHCP_ARP_CHECKER_H
17518678f8Sopenharmony_ci#define OHOS_DHCP_ARP_CHECKER_H
18518678f8Sopenharmony_ci
19518678f8Sopenharmony_ci#include <arpa/inet.h>
20518678f8Sopenharmony_ci#include <netinet/if_ether.h>
21518678f8Sopenharmony_ci#include <string>
22518678f8Sopenharmony_ci
23518678f8Sopenharmony_cinamespace OHOS {
24518678f8Sopenharmony_cinamespace DHCP {
25518678f8Sopenharmony_ciconstexpr int32_t IPV4_ALEN = 4;
26518678f8Sopenharmony_ciconstexpr int32_t INTEGER_MAX = 0x7FFFFFFF;
27518678f8Sopenharmony_ci
28518678f8Sopenharmony_cistruct ArpPacket {
29518678f8Sopenharmony_ci    uint16_t ar_hrd; // hardware type
30518678f8Sopenharmony_ci    uint16_t ar_pro; // protocol type
31518678f8Sopenharmony_ci    uint8_t ar_hln; // length of hardware address
32518678f8Sopenharmony_ci    uint8_t ar_pln; // length of protocol address
33518678f8Sopenharmony_ci    uint16_t ar_op; // opcode
34518678f8Sopenharmony_ci    uint8_t ar_sha[ETH_ALEN]; // sender hardware address
35518678f8Sopenharmony_ci    uint8_t ar_spa[IPV4_ALEN]; // sender protocol address
36518678f8Sopenharmony_ci    uint8_t ar_tha[ETH_ALEN]; // target hardware address
37518678f8Sopenharmony_ci    uint8_t ar_tpa[IPV4_ALEN]; // target protocol address
38518678f8Sopenharmony_ci} __attribute__ ((__packed__));
39518678f8Sopenharmony_ci
40518678f8Sopenharmony_ciclass DhcpArpChecker {
41518678f8Sopenharmony_cipublic:
42518678f8Sopenharmony_ci    DhcpArpChecker();
43518678f8Sopenharmony_ci    ~DhcpArpChecker();
44518678f8Sopenharmony_ci    bool Start(std::string& ifname, std::string& hwAddr, std::string& senderIp, std::string& targetIp);
45518678f8Sopenharmony_ci    void Stop();
46518678f8Sopenharmony_ci    bool DoArpCheck(int32_t timeoutMillis, bool isFillSenderIp, uint64_t &timeCost);
47518678f8Sopenharmony_ci    void GetGwMacAddrList(int32_t timeoutMillis, bool isFillSenderIp, std::vector<std::string>& gwMacLists);
48518678f8Sopenharmony_ci
49518678f8Sopenharmony_ciprivate:
50518678f8Sopenharmony_ci    bool SetArpPacket(ArpPacket& arpPacket, bool isFillSenderIp);
51518678f8Sopenharmony_ci    int32_t CreateSocket(const char *iface, uint16_t protocol);
52518678f8Sopenharmony_ci    int32_t SendData(uint8_t *buff, int32_t count, uint8_t *destHwaddr);
53518678f8Sopenharmony_ci    int32_t RecvData(uint8_t *buff, int32_t count, int32_t timeoutMillis);
54518678f8Sopenharmony_ci    int32_t CloseSocket(void);
55518678f8Sopenharmony_ci    bool SetNonBlock(int32_t fd);
56518678f8Sopenharmony_ci    void SaveGwMacAddr(std::string gwMacAddr, std::vector<std::string>& gwMacLists);
57518678f8Sopenharmony_ciprivate:
58518678f8Sopenharmony_ci    bool m_isSocketCreated;
59518678f8Sopenharmony_ci    struct in_addr m_localIpAddr;
60518678f8Sopenharmony_ci    struct in_addr m_targetIpAddr;
61518678f8Sopenharmony_ci    uint8_t m_localMacAddr[ETH_ALEN];
62518678f8Sopenharmony_ci    uint8_t m_l2Broadcast[ETH_ALEN];
63518678f8Sopenharmony_ci    int32_t m_socketFd;
64518678f8Sopenharmony_ci    uint16_t m_ifaceIndex;
65518678f8Sopenharmony_ci    uint16_t m_protocol;
66518678f8Sopenharmony_ci};
67518678f8Sopenharmony_ci}
68518678f8Sopenharmony_ci}
69518678f8Sopenharmony_ci#endif
70