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