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; |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 28 | Entry(std::string dest, std::string gtw, std::string intf) : |
| 29 | destination(dest), gateway(gtw), interface(intf) |
| 30 | { |
| 31 | } |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 32 | |
| 33 | bool operator==(const Entry& rhs) |
| 34 | { |
| 35 | return this->destination == rhs.destination && |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 36 | this->gateway == rhs.gateway && this->interface == rhs.interface; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 37 | } |
| 38 | }; |
| 39 | |
| 40 | // Map of network address and the route entry |
| 41 | using Map = std::map<std::string, struct Entry>; |
| 42 | |
| 43 | class Table |
| 44 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 45 | public: |
| 46 | Table(); |
| 47 | ~Table() = default; |
| 48 | Table(const Table&) = default; |
| 49 | Table& operator=(const Table&) = default; |
| 50 | Table(Table&&) = default; |
| 51 | Table& operator=(Table&&) = default; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 52 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 53 | /** |
| 54 | * @brief gets the list of routes. |
| 55 | * |
| 56 | * @returns list of routes. |
| 57 | */ |
| 58 | Map getRoutes(); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 59 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 60 | /** |
| 61 | * @brief gets the default gateway. |
| 62 | * |
| 63 | * @returns the default gateway. |
| 64 | */ |
| 65 | std::string getDefaultGateway() const |
| 66 | { |
| 67 | return defaultGateway; |
| 68 | }; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 69 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 70 | /** |
| 71 | * @brief get the gateway for the network. |
| 72 | * @param[in] addressFamily - ip address family(AF_INET/AF_INET6) |
| 73 | * @param[in] ipaddress - ip address. |
| 74 | * @param[in] prefix - prefix length. |
| 75 | * @returns the gatway for the given network. |
| 76 | */ |
| 77 | std::string getGateway(int addressFamily, const std::string& ipaddress, |
| 78 | uint8_t prefix) const; |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 79 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 80 | private: |
| 81 | /** |
| 82 | * @brief read the routing data from the socket and fill the buffer. |
| 83 | * |
| 84 | * @param[in] bufPtr - unique pointer to confidentiality algorithm |
| 85 | * instance |
| 86 | */ |
| 87 | int readNetLinkSock(int sockFd, std::array<char, BUFSIZE>& buff); |
| 88 | /** |
| 89 | * @brief Parse the route and add it to the route list. |
| 90 | * |
| 91 | * @param[in] nlHdr - net link message header. |
| 92 | */ |
| 93 | void parseRoutes(const struct nlmsghdr* nlHdr); |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 94 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 95 | std::string defaultGateway; // default gateway |
| 96 | Map routeList; // List of routes |
Ratan Gupta | 233524c | 2017-05-27 11:47:31 +0530 | [diff] [blame] | 97 | }; |
| 98 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame^] | 99 | } // namespace route |
| 100 | } // namespace network |
| 101 | } // namespace phosphor |