blob: 2bc7a33ce7606d7dc887ed48243f4aef2d71cb01 [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
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{
21 public:
22 struct SockAddr_t
23 {
24 union
25 {
26 sockaddr sockAddr;
Tom Josephc35524e2016-08-29 08:17:59 -050027 sockaddr_in6 inAddr;
Tom Josephaaeb29e2016-08-10 06:36:33 -050028 };
Tom Josephc0f5b5d2017-02-09 17:47:50 +053029 socklen_t addrSize;
Tom Josephaaeb29e2016-08-10 06:36:33 -050030 };
31
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 }
47
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;
57
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 {
Tom Josephc35524e2016-08-29 08:17:59 -050068 return address.inAddr.sin6_port;
Tom Josephaaeb29e2016-08-10 06:36:33 -050069 }
70
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();
83
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);
96
97 /**
98 * @brief Returns file descriptor for the socket
99 */
100 auto getHandle(void) const
101 {
102 return sockfd;
103 }
104
105 ~Channel() = default;
106 Channel(const Channel& right) = delete;
107 Channel& operator=(const Channel& right) = delete;
108 Channel(Channel&&) = default;
109 Channel& operator=(Channel&&) = default;
110
111 private:
112 /*
113 * The socket descriptor is the UDP server socket for the IPMI port.
114 * The same socket descriptor is used for multiple ipmi clients and the
115 * life of the descriptor is lifetime of the net-ipmid server. So we
116 * do not need to close the socket descriptor in the cleanup of the
117 * udpsocket class.
118 */
119 int sockfd;
120 SockAddr_t address;
121 timeval timeout;
122};
123
124} // namespace udpsocket