1 #pragma once
2 
3 #include "types.hpp"
4 
5 #include <linux/netlink.h>
6 #include <net/ethernet.h>
7 
8 #include <cstdint>
9 #include <optional>
10 #include <sdbusplus/bus.hpp>
11 #include <sdbusplus/server/object.hpp>
12 #include <string>
13 #include <string_view>
14 #include <vector>
15 #include <xyz/openbmc_project/Network/Neighbor/server.hpp>
16 #include <xyz/openbmc_project/Object/Delete/server.hpp>
17 
18 namespace phosphor
19 {
20 namespace network
21 {
22 
23 using NeighborIntf = sdbusplus::xyz::openbmc_project::Network::server::Neighbor;
24 
25 using NeighborObj = sdbusplus::server::object::object<
26     NeighborIntf, sdbusplus::xyz::openbmc_project::Object::server::Delete>;
27 
28 class EthernetInterface;
29 
30 /* @class NeighborFilter
31  */
32 struct NeighborFilter
33 {
34     unsigned interface;
35     uint16_t state;
36 
37     /* @brief Creates an empty filter */
38     NeighborFilter() : interface(0), state(~UINT16_C(0))
39     {
40     }
41 };
42 
43 /** @class NeighborInfo
44  *  @brief Information about a neighbor from the kernel
45  */
46 struct NeighborInfo
47 {
48     unsigned interface;
49     InAddrAny address;
50     std::optional<ether_addr> mac;
51     uint16_t state;
52 };
53 
54 /** @brief Returns a list of the current system neighbor table
55  */
56 std::vector<NeighborInfo> getCurrentNeighbors(const NeighborFilter& filter);
57 
58 /** @class Neighbor
59  *  @brief OpenBMC network neighbor implementation.
60  *  @details A concrete implementation for the
61  *  xyz.openbmc_project.Network.Neighbor dbus interface.
62  */
63 class Neighbor : public NeighborObj
64 {
65   public:
66     using State = NeighborIntf::State;
67 
68     Neighbor() = delete;
69     Neighbor(const Neighbor&) = delete;
70     Neighbor& operator=(const Neighbor&) = delete;
71     Neighbor(Neighbor&&) = delete;
72     Neighbor& operator=(Neighbor&&) = delete;
73     virtual ~Neighbor() = default;
74 
75     /** @brief Constructor to put object onto bus at a dbus path.
76      *  @param[in] bus - Bus to attach to.
77      *  @param[in] objPath - Path to attach at.
78      *  @param[in] parent - Parent object.
79      *  @param[in] ipAddress - IP address.
80      *  @param[in] macAddress - Low level MAC address.
81      *  @param[in] state - The state of the neighbor entry.
82      */
83     Neighbor(sdbusplus::bus::bus& bus, const char* objPath,
84              EthernetInterface& parent, const std::string& ipAddress,
85              const std::string& macAddress, State state);
86 
87     /** @brief Delete this d-bus object.
88      */
89     void delete_() override;
90 
91   private:
92     /** @brief Parent Object. */
93     EthernetInterface& parent;
94 };
95 
96 namespace detail
97 {
98 
99 void parseNeighbor(const NeighborFilter& filter, const nlmsghdr& hdr,
100                    std::string_view msg, std::vector<NeighborInfo>& neighbors);
101 
102 } // namespace detail
103 
104 } // namespace network
105 } // namespace phosphor
106