summaryrefslogtreecommitdiffstats
path: root/routing_table.hpp
blob: 4943a4a32be95cc9c1a1098c02138e210ca52332 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma once

#include <asm/types.h>
#include <linux/netlink.h>
#include <sys/socket.h>

#include <iostream>
#include <list>
#include <map>
#include <string>

namespace phosphor
{
namespace network
{
namespace route
{
constexpr auto BUFSIZE = 4096;

struct Entry
{
    // destination network
    std::string destination;
    // gateway for this network.
    std::string gateway;
    // interface for this route
    std::string interface;
    Entry(const std::string& dest, const std::string& gtw,
          const std::string& intf) :
        destination(dest),
        gateway(gtw), interface(intf)
    {
    }

    bool operator==(const Entry& rhs)
    {
        return this->destination == rhs.destination &&
               this->gateway == rhs.gateway && this->interface == rhs.interface;
    }
};

// Map of network address and the route entry
using Map = std::map<std::string, struct Entry>;

class Table
{
  public:
    Table();
    ~Table() = default;
    Table(const Table&) = default;
    Table& operator=(const Table&) = default;
    Table(Table&&) = default;
    Table& operator=(Table&&) = default;

    /**
     * @brief gets the list of routes.
     *
     * @returns list of routes.
     */
    Map getRoutes();

    /**
     * @brief gets the default v4 gateway.
     *
     * @returns the default v4 gateway.
     */
    std::string getDefaultGateway() const
    {
        return defaultGateway;
    };

    /**
     * @brief gets the default v6 gateway.
     *
     * @returns the default v6 gateway.
     */
    std::string getDefaultGateway6() const
    {
        return defaultGateway6;
    };

    /**
     * @brief get the gateway for the network.
     * @param[in] addressFamily - ip address family(AF_INET/AF_INET6)
     * @param[in] ipaddress - ip address.
     * @param[in] prefix - prefix length.
     * @returns the gatway for the given network.
     */
    std::string getGateway(int addressFamily, const std::string& ipaddress,
                           uint8_t prefix) const;

  private:
    /**
     * @brief read the routing data from the socket and fill the buffer.
     *
     * @param[in] bufPtr - unique pointer to confidentiality algorithm
     *                     instance
     */
    int readNetLinkSock(int sockFd, std::array<char, BUFSIZE>& buff);
    /**
     * @brief Parse the route and add it to the route list.
     *
     * @param[in] nlHdr - net link message header.
     */
    void parseRoutes(const struct nlmsghdr* nlHdr);

    std::string defaultGateway;  // default gateway
    std::string defaultGateway6; // default gateway
    Map routeList;               // List of routes
};

} // namespace route
} // namespace network
} // namespace phosphor
OpenPOWER on IntegriCloud