blob: 8b634c62ab83d2d1ec852a4776b9590a408a90e1 [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 <linux/netlink.h>
Gunnar Mills57d9c502018-09-14 14:42:34 -05005#include <sys/socket.h>
Ratan Gupta233524c2017-05-27 11:47:31 +05306
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>
Ravi Tejaa5a09442020-07-17 00:57:33 -050011#include <vector>
Ratan Gupta233524c2017-05-27 11:47:31 +053012
13namespace phosphor
14{
15namespace network
16{
17namespace route
18{
Ratan Guptab46497f2017-06-09 12:00:34 +053019constexpr auto BUFSIZE = 4096;
Ratan Gupta233524c2017-05-27 11:47:31 +053020
21struct Entry
22{
23 // destination network
24 std::string destination;
25 // gateway for this network.
26 std::string gateway;
27 // interface for this route
28 std::string interface;
Patrick Venture6d669962018-10-13 09:57:36 -070029 Entry(const std::string& dest, const std::string& gtw,
30 const std::string& intf) :
31 destination(dest),
32 gateway(gtw), interface(intf)
Gunnar Mills57d9c502018-09-14 14:42:34 -050033 {
34 }
Ratan Gupta233524c2017-05-27 11:47:31 +053035
36 bool operator==(const Entry& rhs)
37 {
38 return this->destination == rhs.destination &&
Gunnar Mills57d9c502018-09-14 14:42:34 -050039 this->gateway == rhs.gateway && this->interface == rhs.interface;
Ratan Gupta233524c2017-05-27 11:47:31 +053040 }
41};
42
43// Map of network address and the route entry
44using Map = std::map<std::string, struct Entry>;
45
46class Table
47{
Gunnar Mills57d9c502018-09-14 14:42:34 -050048 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;
Ratan Gupta233524c2017-05-27 11:47:31 +053055
Gunnar Mills57d9c502018-09-14 14:42:34 -050056 /**
57 * @brief gets the list of routes.
58 *
59 * @returns list of routes.
60 */
61 Map getRoutes();
Ratan Gupta233524c2017-05-27 11:47:31 +053062
Gunnar Mills57d9c502018-09-14 14:42:34 -050063 /**
William A. Kennington IIId3c249c2019-02-01 21:12:02 -080064 * @brief gets the default v4 gateway.
Gunnar Mills57d9c502018-09-14 14:42:34 -050065 *
Ravi Tejaa5a09442020-07-17 00:57:33 -050066 * @returns the default v4 gateway list.
Gunnar Mills57d9c502018-09-14 14:42:34 -050067 */
Ravi Tejaa5a09442020-07-17 00:57:33 -050068 std::map<std::string, std::string> getDefaultGateway() const
Gunnar Mills57d9c502018-09-14 14:42:34 -050069 {
70 return defaultGateway;
71 };
Ratan Gupta233524c2017-05-27 11:47:31 +053072
Gunnar Mills57d9c502018-09-14 14:42:34 -050073 /**
William A. Kennington IIId3c249c2019-02-01 21:12:02 -080074 * @brief gets the default v6 gateway.
75 *
Ravi Tejaa5a09442020-07-17 00:57:33 -050076 * @returns the default v6 gateway list.
William A. Kennington IIId3c249c2019-02-01 21:12:02 -080077 */
Ravi Tejaa5a09442020-07-17 00:57:33 -050078 std::map<std::string, std::string> getDefaultGateway6() const
William A. Kennington IIId3c249c2019-02-01 21:12:02 -080079 {
80 return defaultGateway6;
81 };
82
Gunnar Mills57d9c502018-09-14 14:42:34 -050083 private:
84 /**
85 * @brief read the routing data from the socket and fill the buffer.
86 *
87 * @param[in] bufPtr - unique pointer to confidentiality algorithm
88 * instance
89 */
90 int readNetLinkSock(int sockFd, std::array<char, BUFSIZE>& buff);
91 /**
92 * @brief Parse the route and add it to the route list.
93 *
94 * @param[in] nlHdr - net link message header.
95 */
96 void parseRoutes(const struct nlmsghdr* nlHdr);
Ratan Gupta233524c2017-05-27 11:47:31 +053097
Ravi Tejaa5a09442020-07-17 00:57:33 -050098 std::map<std::string, std::string> defaultGateway; // default gateway list
99 std::map<std::string, std::string> defaultGateway6; // default gateway list
100 Map routeList; // List of routes
Ratan Gupta233524c2017-05-27 11:47:31 +0530101};
102
Gunnar Mills57d9c502018-09-14 14:42:34 -0500103} // namespace route
104} // namespace network
105} // namespace phosphor