summaryrefslogtreecommitdiffstats
path: root/socket_channel.hpp
blob: 5996b6246275a4bba75fbaaf4b69989b477c4204 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#pragma once

#include <arpa/inet.h>
#include <unistd.h>

#include <string>
#include <tuple>
#include <vector>

namespace udpsocket
{

/** @class Channel
 *
 *  @brief Provides encapsulation for UDP socket operations like Read, Peek,
 *         Write, Remote peer's IP Address and Port.
 */
class Channel
{
    public:
        struct SockAddr_t
        {
            union
            {
                sockaddr sockAddr;
                sockaddr_in6 inAddr;
            };
            socklen_t addrSize;
        };

        /**
         * @brief Constructor
         *
         * Initialize the IPMI socket object with the socket descriptor
         *
         * @param [in] File Descriptor for the socket
         * @param [in] Timeout parameter for the select call
         *
         * @return None
         */
        Channel(int insockfd, timeval& inTimeout)
        {
            sockfd = insockfd;
            timeout = inTimeout;
        }

        /**
         * @brief Fetch the IP address of the remote peer
         *
         * Returns the IP address of the remote peer which is connected to this
         * socket
         *
         * @return IP address of the remote peer
         */
        std::string getRemoteAddress() const;

        /**
         * @brief Fetch the port number of the remote peer
         *
         * Returns the port number of the remote peer
         *
         * @return Port number
         *
         */
        auto getPort() const
        {
            return address.inAddr.sin6_port;
        }

        /**
         * @brief Read the incoming packet
         *
         * Reads the data available on the socket
         *
         * @return A tuple with return code and vector with the buffer
         *         In case of success, the vector is populated with the data
         *         available on the socket and return code is 0.
         *         In case of error, the return code is < 0 and vector is set
         *         to size 0.
         */
        std::tuple<int, std::vector<uint8_t>> read();

        /**
         *  @brief Write the outgoing packet
         *
         *  Writes the data in the vector to the socket
         *
         *  @param [in] inBuffer
         *      The vector would be the buffer of data to write to the socket.
         *
         *  @return In case of success the return code is 0 and return code is
         *          < 0 in case of failure.
         */
        int write(const std::vector<uint8_t>& inBuffer);

        /**
         * @brief Returns file descriptor for the socket
         */
        auto getHandle(void) const
        {
            return sockfd;
        }

        ~Channel() = default;
        Channel(const Channel& right) = delete;
        Channel& operator=(const Channel& right) = delete;
        Channel(Channel&&) = default;
        Channel& operator=(Channel&&) = default;

    private:
        /*
         * The socket descriptor is the UDP server socket for the IPMI port.
         * The same socket descriptor is used for multiple ipmi clients and the
         * life of the descriptor is lifetime of the net-ipmid server. So we
         * do not need to close the socket descriptor in the cleanup of the
         * udpsocket class.
         */
        int sockfd;
        SockAddr_t address;
        timeval timeout;
};

} // namespace udpsocket
OpenPOWER on IntegriCloud