blob: e7596267fb12a53367da1fff3e915101ae98df50 [file] [log] [blame]
Ratan Gupta37a7a072016-12-14 00:51:23 +05301#pragma once
2
3#include <arpa/inet.h>
4#include <unistd.h>
5
6#include <string>
7#include <tuple>
8#include <vector>
9
10namespace udpsocket
11{
12
13using 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 */
19class Channel
20{
Patrick Venture537ff142018-11-01 16:37:09 -070021 public:
22 struct SockAddr_t
23 {
24 union
Ratan Gupta37a7a072016-12-14 00:51:23 +053025 {
Patrick Venture537ff142018-11-01 16:37:09 -070026 sockaddr sockAddr;
27 sockaddr_in6 inAddr;
Ratan Gupta37a7a072016-12-14 00:51:23 +053028 };
Patrick Venture537ff142018-11-01 16:37:09 -070029 socklen_t addrSize;
30 };
Ratan Gupta37a7a072016-12-14 00:51:23 +053031
Patrick Venture537ff142018-11-01 16:37:09 -070032 /**
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 Gupta37a7a072016-12-14 00:51:23 +053047
Patrick Venture537ff142018-11-01 16:37:09 -070048 /**
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 Gupta37a7a072016-12-14 00:51:23 +053057
Patrick Venture537ff142018-11-01 16:37:09 -070058 /**
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 Gupta37a7a072016-12-14 00:51:23 +053070
Patrick Venture537ff142018-11-01 16:37:09 -070071 /**
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 Gupta37a7a072016-12-14 00:51:23 +053083
Patrick Venture537ff142018-11-01 16:37:09 -070084 /**
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 Gupta37a7a072016-12-14 00:51:23 +053096
Patrick Venture537ff142018-11-01 16:37:09 -070097 ~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 Gupta37a7a072016-12-14 00:51:23 +0530102
Patrick Venture537ff142018-11-01 16:37:09 -0700103 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 Gupta37a7a072016-12-14 00:51:23 +0530114};
115
116} // namespace udpsocket