xref: /openbmc/slpd-lite/sock_channel.hpp (revision 537ff140)
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 {
21   public:
22     struct SockAddr_t
23     {
24         union
25         {
26             sockaddr sockAddr;
27             sockaddr_in6 inAddr;
28         };
29         socklen_t addrSize;
30     };
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      */
Channel(int insockfd,timeval & inTimeout)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      */
getPort() const66     auto getPort() const
67     {
68         return address.inAddr.sin6_port;
69     }
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     ~Channel() = default;
98     Channel(const Channel& right) = delete;
99     Channel& operator=(const Channel& right) = delete;
100     Channel(Channel&&) = default;
101     Channel& operator=(Channel&&) = default;
102 
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;
114 };
115 
116 } // namespace udpsocket
117