Fetch the kernel routing table
It would be used to get the gateway for
a specific network.
It would also be used to get the default
gateway for the system.
Change-Id: I1f7f81e1d6ea6c3f4385f454ad229d4074341c44
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/routing_table.hpp b/routing_table.hpp
new file mode 100644
index 0000000..e137e23
--- /dev/null
+++ b/routing_table.hpp
@@ -0,0 +1,107 @@
+#pragma once
+
+#include <asm/types.h>
+#include <sys/user.h>
+#include <sys/socket.h>
+#include <linux/netlink.h>
+
+#include <iostream>
+#include <list>
+#include <string>
+#include <map>
+
+namespace phosphor
+{
+namespace network
+{
+namespace route
+{
+constexpr auto BUFSIZE = PAGE_SIZE;
+
+struct Entry
+{
+ // destination network
+ std::string destination;
+ // gateway for this network.
+ std::string gateway;
+ // interface for this route
+ std::string interface;
+ Entry(std::string dest,
+ std::string gtw,
+ 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 gateway.
+ *
+ * @returns the default gateway.
+ */
+ std::string getDefaultGateway() const
+ {
+ return defaultGateway;
+ };
+
+ /**
+ * @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
+ Map routeList; //List of routes
+
+};
+
+}// namespace route
+}// namespace network
+}// namespace phosphor