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