Ratan Gupta | 37a7a07 | 2016-12-14 00:51:23 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <arpa/inet.h> |
| 4 | #include <unistd.h> |
| 5 | |
| 6 | #include <string> |
| 7 | #include <tuple> |
| 8 | #include <vector> |
| 9 | |
| 10 | namespace udpsocket |
| 11 | { |
| 12 | |
| 13 | using buffer = std::vector<uint8_t>; |
| 14 | /** @class Channel |
| 15 | * |
| 16 | * @brief Provides encapsulation for UDP socket operations like Read, Peek, |
| 17 | * Write, Remote peer's IP Address and Port. |
| 18 | */ |
| 19 | class Channel |
| 20 | { |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 21 | public: |
| 22 | struct SockAddr_t |
| 23 | { |
| 24 | union |
Ratan Gupta | 37a7a07 | 2016-12-14 00:51:23 +0530 | [diff] [blame] | 25 | { |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 26 | sockaddr sockAddr; |
| 27 | sockaddr_in6 inAddr; |
Ratan Gupta | 37a7a07 | 2016-12-14 00:51:23 +0530 | [diff] [blame] | 28 | }; |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 29 | socklen_t addrSize; |
| 30 | }; |
Ratan Gupta | 37a7a07 | 2016-12-14 00:51:23 +0530 | [diff] [blame] | 31 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 32 | /** |
| 33 | * @brief Constructor |
| 34 | * |
| 35 | * Initialize the IPMI socket object with the socket descriptor |
| 36 | * |
| 37 | * @param [in] File Descriptor for the socket |
| 38 | * @param [in] Timeout parameter for the select call |
| 39 | * |
| 40 | * @return None |
| 41 | */ |
| 42 | Channel(int insockfd, timeval& inTimeout) |
| 43 | { |
| 44 | sockfd = insockfd; |
| 45 | timeout = inTimeout; |
| 46 | } |
Ratan Gupta | 37a7a07 | 2016-12-14 00:51:23 +0530 | [diff] [blame] | 47 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 48 | /** |
| 49 | * @brief Fetch the IP address of the remote peer |
| 50 | * |
| 51 | * Returns the IP address of the remote peer which is connected to this |
| 52 | * socket |
| 53 | * |
| 54 | * @return IP address of the remote peer |
| 55 | */ |
| 56 | std::string getRemoteAddress() const; |
Ratan Gupta | 37a7a07 | 2016-12-14 00:51:23 +0530 | [diff] [blame] | 57 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 58 | /** |
| 59 | * @brief Fetch the port number of the remote peer |
| 60 | * |
| 61 | * Returns the port number of the remote peer |
| 62 | * |
| 63 | * @return Port number |
| 64 | * |
| 65 | */ |
| 66 | auto getPort() const |
| 67 | { |
| 68 | return address.inAddr.sin6_port; |
| 69 | } |
Ratan Gupta | 37a7a07 | 2016-12-14 00:51:23 +0530 | [diff] [blame] | 70 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 71 | /** |
| 72 | * @brief Read the incoming packet |
| 73 | * |
| 74 | * Reads the data available on the socket |
| 75 | * |
| 76 | * @return A tuple with return code and vector with the buffer |
| 77 | * In case of success, the vector is populated with the data |
| 78 | * available on the socket and return code is 0. |
| 79 | * In case of error, the return code is < 0 and vector is set |
| 80 | * to size 0. |
| 81 | */ |
| 82 | std::tuple<int, buffer> read(); |
Ratan Gupta | 37a7a07 | 2016-12-14 00:51:23 +0530 | [diff] [blame] | 83 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 84 | /** |
| 85 | * @brief Write the outgoing packet |
| 86 | * |
| 87 | * Writes the data in the vector to the socket |
| 88 | * |
| 89 | * @param [in] inBuffer |
| 90 | * The vector would be the buffer of data to write to the socket. |
| 91 | * |
| 92 | * @return In case of success the return code is 0 and return code is |
| 93 | * < 0 in case of failure. |
| 94 | */ |
| 95 | int write(buffer& inBuffer); |
Ratan Gupta | 37a7a07 | 2016-12-14 00:51:23 +0530 | [diff] [blame] | 96 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 97 | ~Channel() = default; |
| 98 | Channel(const Channel& right) = delete; |
| 99 | Channel& operator=(const Channel& right) = delete; |
| 100 | Channel(Channel&&) = default; |
| 101 | Channel& operator=(Channel&&) = default; |
Ratan Gupta | 37a7a07 | 2016-12-14 00:51:23 +0530 | [diff] [blame] | 102 | |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 103 | private: |
| 104 | /* |
| 105 | * The socket descriptor is the UDP server socket for the IPMI port. |
| 106 | * The same socket descriptor is used for multiple ipmi clients and the |
| 107 | * life of the descriptor is lifetime of the net-ipmid server. So we |
| 108 | * do not need to close the socket descriptor in the cleanup of the |
| 109 | * udpsocket class. |
| 110 | */ |
| 111 | int sockfd; |
| 112 | SockAddr_t address; |
| 113 | timeval timeout; |
Ratan Gupta | 37a7a07 | 2016-12-14 00:51:23 +0530 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | } // namespace udpsocket |