blob: 0768d8e8beb1d146c5489eee2b4a734b62d88699 [file] [log] [blame]
Ratan Gupta233524c2017-05-27 11:47:31 +05301#pragma once
2
3#include <asm/types.h>
Ratan Gupta233524c2017-05-27 11:47:31 +05304#include <sys/socket.h>
5#include <linux/netlink.h>
6
7#include <iostream>
8#include <list>
Ratan Gupta233524c2017-05-27 11:47:31 +05309#include <map>
Patrick Venture189d44e2018-07-09 12:30:59 -070010#include <string>
Ratan Gupta233524c2017-05-27 11:47:31 +053011
12namespace phosphor
13{
14namespace network
15{
16namespace route
17{
Ratan Guptab46497f2017-06-09 12:00:34 +053018constexpr auto BUFSIZE = 4096;
Ratan Gupta233524c2017-05-27 11:47:31 +053019
20struct Entry
21{
22 // destination network
23 std::string destination;
24 // gateway for this network.
25 std::string gateway;
26 // interface for this route
27 std::string interface;
28 Entry(std::string dest,
29 std::string gtw,
30 std::string intf) :
31 destination(dest),
32 gateway(gtw),
33 interface(intf){}
34
35 bool operator==(const Entry& rhs)
36 {
37 return this->destination == rhs.destination &&
38 this->gateway == rhs.gateway &&
39 this->interface == rhs.interface;
40 }
41};
42
43// Map of network address and the route entry
44using Map = std::map<std::string, struct Entry>;
45
46class Table
47{
48 public:
49 Table();
50 ~Table() = default;
51 Table(const Table&) = default;
52 Table& operator=(const Table&) = default;
53 Table(Table&&) = default;
54 Table& operator=(Table &&) = default;
55
56 /**
57 * @brief gets the list of routes.
58 *
59 * @returns list of routes.
60 */
61 Map getRoutes();
62
63 /**
64 * @brief gets the default gateway.
65 *
66 * @returns the default gateway.
67 */
68 std::string getDefaultGateway() const
69 {
70 return defaultGateway;
71 };
72
73 /**
74 * @brief get the gateway for the network.
75 * @param[in] addressFamily - ip address family(AF_INET/AF_INET6)
76 * @param[in] ipaddress - ip address.
77 * @param[in] prefix - prefix length.
78 * @returns the gatway for the given network.
79 */
80 std::string getGateway(int addressFamily,
81 const std::string& ipaddress,
82 uint8_t prefix) const;
83 private:
84
85 /**
86 * @brief read the routing data from the socket and fill the buffer.
87 *
88 * @param[in] bufPtr - unique pointer to confidentiality algorithm
89 * instance
90 */
91 int readNetLinkSock(int sockFd, std::array<char, BUFSIZE>& buff);
92 /**
93 * @brief Parse the route and add it to the route list.
94 *
95 * @param[in] nlHdr - net link message header.
96 */
97 void parseRoutes(const struct nlmsghdr* nlHdr);
98
99 std::string defaultGateway; // default gateway
100 Map routeList; //List of routes
101
102};
103
104}// namespace route
105}// namespace network
106}// namespace phosphor