blob: 69235924c3df7da1986627a21f5a39a04e0f11b5 [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>
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;
Patrick Venture6d669962018-10-13 09:57:36 -070028 Entry(const std::string& dest, const std::string& gtw,
29 const std::string& intf) :
30 destination(dest),
31 gateway(gtw), interface(intf)
Gunnar Mills57d9c502018-09-14 14:42:34 -050032 {
33 }
Ratan Gupta233524c2017-05-27 11:47:31 +053034
35 bool operator==(const Entry& rhs)
36 {
37 return this->destination == rhs.destination &&
Gunnar Mills57d9c502018-09-14 14:42:34 -050038 this->gateway == rhs.gateway && this->interface == rhs.interface;
Ratan Gupta233524c2017-05-27 11:47:31 +053039 }
40};
41
42// Map of network address and the route entry
43using Map = std::map<std::string, struct Entry>;
44
45class Table
46{
Gunnar Mills57d9c502018-09-14 14:42:34 -050047 public:
48 Table();
49 ~Table() = default;
50 Table(const Table&) = default;
51 Table& operator=(const Table&) = default;
52 Table(Table&&) = default;
53 Table& operator=(Table&&) = default;
Ratan Gupta233524c2017-05-27 11:47:31 +053054
Gunnar Mills57d9c502018-09-14 14:42:34 -050055 /**
56 * @brief gets the list of routes.
57 *
58 * @returns list of routes.
59 */
60 Map getRoutes();
Ratan Gupta233524c2017-05-27 11:47:31 +053061
Gunnar Mills57d9c502018-09-14 14:42:34 -050062 /**
William A. Kennington IIId3c249c2019-02-01 21:12:02 -080063 * @brief gets the default v4 gateway.
Gunnar Mills57d9c502018-09-14 14:42:34 -050064 *
William A. Kennington IIId3c249c2019-02-01 21:12:02 -080065 * @returns the default v4 gateway.
Gunnar Mills57d9c502018-09-14 14:42:34 -050066 */
67 std::string getDefaultGateway() const
68 {
69 return defaultGateway;
70 };
Ratan Gupta233524c2017-05-27 11:47:31 +053071
Gunnar Mills57d9c502018-09-14 14:42:34 -050072 /**
William A. Kennington IIId3c249c2019-02-01 21:12:02 -080073 * @brief gets the default v6 gateway.
74 *
75 * @returns the default v6 gateway.
76 */
77 std::string getDefaultGateway6() const
78 {
79 return defaultGateway6;
80 };
81
Gunnar Mills57d9c502018-09-14 14:42:34 -050082 private:
83 /**
84 * @brief read the routing data from the socket and fill the buffer.
85 *
86 * @param[in] bufPtr - unique pointer to confidentiality algorithm
87 * instance
88 */
89 int readNetLinkSock(int sockFd, std::array<char, BUFSIZE>& buff);
90 /**
91 * @brief Parse the route and add it to the route list.
92 *
93 * @param[in] nlHdr - net link message header.
94 */
95 void parseRoutes(const struct nlmsghdr* nlHdr);
Ratan Gupta233524c2017-05-27 11:47:31 +053096
William A. Kennington IIId3c249c2019-02-01 21:12:02 -080097 std::string defaultGateway; // default gateway
98 std::string defaultGateway6; // default gateway
99 Map routeList; // List of routes
Ratan Gupta233524c2017-05-27 11:47:31 +0530100};
101
Gunnar Mills57d9c502018-09-14 14:42:34 -0500102} // namespace route
103} // namespace network
104} // namespace phosphor