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> |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 11 | |
| 12 | namespace phosphor |
| 13 | { |
| 14 | namespace network |
| 15 | { |
| 16 | namespace route |
| 17 | { |
Ratan Gupta | b46497f | 2017-06-09 12:00:34 +0530 | [diff] [blame] | 18 | constexpr auto BUFSIZE = 4096; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 19 | |
| 20 | struct 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 Venture | 6d66996 | 2018-10-13 09:57:36 -0700 | [diff] [blame] | 28 | Entry(const std::string& dest, const std::string& gtw, |
| 29 | const std::string& intf) : |
| 30 | destination(dest), |
| 31 | gateway(gtw), interface(intf) |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 32 | { |
| 33 | } |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 34 | |
| 35 | bool operator==(const Entry& rhs) |
| 36 | { |
| 37 | return this->destination == rhs.destination && |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 38 | this->gateway == rhs.gateway && this->interface == rhs.interface; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 39 | } |
| 40 | }; |
| 41 | |
| 42 | // Map of network address and the route entry |
| 43 | using Map = std::map<std::string, struct Entry>; |
| 44 | |
| 45 | class Table |
| 46 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 47 | 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 Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 54 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 55 | /** |
| 56 | * @brief gets the list of routes. |
| 57 | * |
| 58 | * @returns list of routes. |
| 59 | */ |
| 60 | Map getRoutes(); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 61 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 62 | /** |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 63 | * @brief gets the default v4 gateway. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 64 | * |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 65 | * @returns the default v4 gateway. |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 66 | */ |
| 67 | std::string getDefaultGateway() const |
| 68 | { |
| 69 | return defaultGateway; |
| 70 | }; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 71 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 72 | /** |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 73 | * @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 Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 82 | 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 Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 96 | |
William A. Kennington III | d3c249c | 2019-02-01 21:12:02 -0800 | [diff] [blame] | 97 | std::string defaultGateway; // default gateway |
| 98 | std::string defaultGateway6; // default gateway |
| 99 | Map routeList; // List of routes |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 100 | }; |
| 101 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 102 | } // namespace route |
| 103 | } // namespace network |
| 104 | } // namespace phosphor |