blob: 5996b6246275a4bba75fbaaf4b69989b477c4204 [file] [log] [blame]
Tom Josephaaeb29e2016-08-10 06:36:33 -05001#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
Tom Josephaaeb29e2016-08-10 06:36:33 -050013/** @class Channel
14 *
15 * @brief Provides encapsulation for UDP socket operations like Read, Peek,
16 * Write, Remote peer's IP Address and Port.
17 */
18class Channel
19{
20 public:
21 struct SockAddr_t
22 {
23 union
24 {
25 sockaddr sockAddr;
Tom Josephc35524e2016-08-29 08:17:59 -050026 sockaddr_in6 inAddr;
Tom Josephaaeb29e2016-08-10 06:36:33 -050027 };
Tom Josephc0f5b5d2017-02-09 17:47:50 +053028 socklen_t addrSize;
Tom Josephaaeb29e2016-08-10 06:36:33 -050029 };
30
31 /**
32 * @brief Constructor
33 *
34 * Initialize the IPMI socket object with the socket descriptor
35 *
36 * @param [in] File Descriptor for the socket
37 * @param [in] Timeout parameter for the select call
38 *
39 * @return None
40 */
41 Channel(int insockfd, timeval& inTimeout)
42 {
43 sockfd = insockfd;
44 timeout = inTimeout;
45 }
46
47 /**
48 * @brief Fetch the IP address of the remote peer
49 *
50 * Returns the IP address of the remote peer which is connected to this
51 * socket
52 *
53 * @return IP address of the remote peer
54 */
55 std::string getRemoteAddress() const;
56
57 /**
58 * @brief Fetch the port number of the remote peer
59 *
60 * Returns the port number of the remote peer
61 *
62 * @return Port number
63 *
64 */
65 auto getPort() const
66 {
Tom Josephc35524e2016-08-29 08:17:59 -050067 return address.inAddr.sin6_port;
Tom Josephaaeb29e2016-08-10 06:36:33 -050068 }
69
70 /**
71 * @brief Read the incoming packet
72 *
73 * Reads the data available on the socket
74 *
75 * @return A tuple with return code and vector with the buffer
76 * In case of success, the vector is populated with the data
77 * available on the socket and return code is 0.
78 * In case of error, the return code is < 0 and vector is set
79 * to size 0.
80 */
Vernon Mauery70fd29c2017-11-30 13:11:43 -080081 std::tuple<int, std::vector<uint8_t>> read();
Tom Josephaaeb29e2016-08-10 06:36:33 -050082
83 /**
84 * @brief Write the outgoing packet
85 *
86 * Writes the data in the vector to the socket
87 *
88 * @param [in] inBuffer
89 * The vector would be the buffer of data to write to the socket.
90 *
91 * @return In case of success the return code is 0 and return code is
92 * < 0 in case of failure.
93 */
Vernon Mauery70fd29c2017-11-30 13:11:43 -080094 int write(const std::vector<uint8_t>& inBuffer);
Tom Josephaaeb29e2016-08-10 06:36:33 -050095
96 /**
97 * @brief Returns file descriptor for the socket
98 */
99 auto getHandle(void) const
100 {
101 return sockfd;
102 }
103
104 ~Channel() = default;
105 Channel(const Channel& right) = delete;
106 Channel& operator=(const Channel& right) = delete;
107 Channel(Channel&&) = default;
108 Channel& operator=(Channel&&) = default;
109
110 private:
111 /*
112 * The socket descriptor is the UDP server socket for the IPMI port.
113 * The same socket descriptor is used for multiple ipmi clients and the
114 * life of the descriptor is lifetime of the net-ipmid server. So we
115 * do not need to close the socket descriptor in the cleanup of the
116 * udpsocket class.
117 */
118 int sockfd;
119 SockAddr_t address;
120 timeval timeout;
121};
122
123} // namespace udpsocket