Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <asm/types.h> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 4 | #include <linux/netlink.h> |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 5 | #include <sys/socket.h> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 6 | |
| 7 | #include <iostream> |
| 8 | #include <list> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 9 | #include <map> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 10 | #include <string> |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 11 | #include <vector> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 12 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | namespace network |
| 16 | { |
| 17 | namespace route |
| 18 | { |
Ratan Gupta | b46497f | 2017-06-09 12:00:34 +0530 | [diff] [blame] | 19 | constexpr auto BUFSIZE = 4096; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 20 | |
| 21 | struct 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 Venture | 6d66996 | 2018-10-13 09:57:36 -0700 | [diff] [blame] | 29 | Entry(const std::string& dest, const std::string& gtw, |
| 30 | const std::string& intf) : |
| 31 | destination(dest), |
| 32 | gateway(gtw), interface(intf) |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 33 | { |
| 34 | } |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 35 | |
| 36 | bool operator==(const Entry& rhs) |
| 37 | { |
| 38 | return this->destination == rhs.destination && |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 39 | this->gateway == rhs.gateway && this->interface == rhs.interface; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 40 | } |
| 41 | }; |
| 42 | |
| 43 | // Map of network address and the route entry |
| 44 | using Map = std::map<std::string, struct Entry>; |
| 45 | |
| 46 | class Table |
| 47 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 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; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 55 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 56 | /** |
| 57 | * @brief gets the list of routes. |
| 58 | * |
| 59 | * @returns list of routes. |
| 60 | */ |
| 61 | Map getRoutes(); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 62 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 63 | /** |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 64 | * @brief gets the default v4 gateway. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 65 | * |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 66 | * @returns the default v4 gateway list. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 67 | */ |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 68 | std::map<std::string, std::string> getDefaultGateway() const |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 69 | { |
| 70 | return defaultGateway; |
| 71 | }; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 72 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 73 | /** |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 74 | * @brief gets the default v6 gateway. |
| 75 | * |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 76 | * @returns the default v6 gateway list. |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 77 | */ |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 78 | std::map<std::string, std::string> getDefaultGateway6() const |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 79 | { |
| 80 | return defaultGateway6; |
| 81 | }; |
| 82 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 83 | 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 Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 97 | |
Ravi Teja | a5a0944 | 2020-07-17 00:57:33 -0500 | [diff] [blame] | 98 | 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 Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 101 | }; |
| 102 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 103 | } // namespace route |
| 104 | } // namespace network |
| 105 | } // namespace phosphor |