Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 1 | #pragma once |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 2 | #include <netinet/in.h> |
| 3 | #include <sys/socket.h> |
| 4 | #include <sys/types.h> |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 5 | |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 6 | #include <boost/asio/ip/udp.hpp> |
| 7 | #include <memory> |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 8 | #include <optional> |
| 9 | #include <phosphor-logging/log.hpp> |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <tuple> |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 12 | #include <variant> |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
| 15 | namespace udpsocket |
| 16 | { |
Rajashekar Gade Reddy | 9979e99 | 2020-02-07 19:18:34 +0530 | [diff] [blame] | 17 | static constexpr uint8_t v4v6Index = 12; |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 18 | |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 19 | /** @class Channel |
| 20 | * |
| 21 | * @brief Provides encapsulation for UDP socket operations like Read, Peek, |
| 22 | * Write, Remote peer's IP Address and Port. |
| 23 | */ |
| 24 | class Channel |
| 25 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 26 | public: |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 27 | Channel() = delete; |
| 28 | ~Channel() = default; |
| 29 | Channel(const Channel& right) = delete; |
| 30 | Channel& operator=(const Channel& right) = delete; |
| 31 | Channel(Channel&&) = delete; |
| 32 | Channel& operator=(Channel&&) = delete; |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 33 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 34 | /** |
| 35 | * @brief Constructor |
| 36 | * |
| 37 | * Initialize the IPMI socket object with the socket descriptor |
| 38 | * |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 39 | * @param [in] pointer to a boost::asio udp socket object |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 40 | * |
| 41 | * @return None |
| 42 | */ |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 43 | explicit Channel(std::shared_ptr<boost::asio::ip::udp::socket> socket) : |
| 44 | socket(socket) |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 45 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 46 | } |
Rajashekar Gade Reddy | 9979e99 | 2020-02-07 19:18:34 +0530 | [diff] [blame] | 47 | /** |
| 48 | * @brief Check if ip address is ipv4 mapped ipv6 |
| 49 | * |
| 50 | * @param v6Addr : in6_addr obj |
| 51 | * |
| 52 | * @return true if ipv4 mapped ipv6 else return false |
| 53 | */ |
| 54 | bool isIpv4InIpv6(const struct in6_addr& v6Addr) const |
| 55 | { |
| 56 | constexpr uint8_t prefix[v4v6Index] = {0, 0, 0, 0, 0, 0, |
| 57 | 0, 0, 0, 0, 0xff, 0xff}; |
| 58 | return 0 == std::memcmp(&v6Addr.s6_addr[0], &prefix[0], sizeof(prefix)); |
| 59 | } |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 60 | /** |
| 61 | * @brief Fetch the IP address of the remote peer |
| 62 | * |
Rajashekar Gade Reddy | 9979e99 | 2020-02-07 19:18:34 +0530 | [diff] [blame] | 63 | * @param remoteIpv4Addr : ipv4 address is assigned to it. |
| 64 | * |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 65 | * Returns the IP address of the remote peer which is connected to this |
| 66 | * socket |
| 67 | * |
| 68 | * @return IP address of the remote peer |
| 69 | */ |
Rajashekar Gade Reddy | 9979e99 | 2020-02-07 19:18:34 +0530 | [diff] [blame] | 70 | std::string getRemoteAddress(uint32_t& remoteIpv4Addr) const |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 71 | { |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 72 | const char* retval = nullptr; |
| 73 | if (sockAddrSize == sizeof(sockaddr_in)) |
| 74 | { |
| 75 | char ipv4addr[INET_ADDRSTRLEN]; |
Rajashekar Gade Reddy | 9979e99 | 2020-02-07 19:18:34 +0530 | [diff] [blame] | 76 | const sockaddr_in* sa = |
| 77 | reinterpret_cast<const sockaddr_in*>(&remoteSockAddr); |
| 78 | remoteIpv4Addr = sa->sin_addr.s_addr; |
| 79 | retval = |
| 80 | inet_ntop(AF_INET, &(sa->sin_addr), ipv4addr, sizeof(ipv4addr)); |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 81 | } |
| 82 | else if (sockAddrSize == sizeof(sockaddr_in6)) |
| 83 | { |
| 84 | char ipv6addr[INET6_ADDRSTRLEN]; |
Rajashekar Gade Reddy | 9979e99 | 2020-02-07 19:18:34 +0530 | [diff] [blame] | 85 | const sockaddr_in6* sa = |
| 86 | reinterpret_cast<const sockaddr_in6*>(&remoteSockAddr); |
| 87 | |
| 88 | if (isIpv4InIpv6(sa->sin6_addr)) |
| 89 | { |
| 90 | std::copy_n(&sa->sin6_addr.s6_addr[v4v6Index], |
| 91 | sizeof(remoteIpv4Addr), |
| 92 | reinterpret_cast<uint8_t*>(&remoteIpv4Addr)); |
| 93 | } |
| 94 | retval = inet_ntop(AF_INET6, &(sa->sin6_addr), ipv6addr, |
| 95 | sizeof(ipv6addr)); |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 96 | } |
Rajashekar Gade Reddy | 9979e99 | 2020-02-07 19:18:34 +0530 | [diff] [blame] | 97 | |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 98 | if (retval) |
| 99 | { |
| 100 | return retval; |
| 101 | } |
| 102 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 103 | "Error in inet_ntop", |
| 104 | phosphor::logging::entry("ERROR=%s", strerror(errno))); |
| 105 | return std::string(); |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 106 | } |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 107 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 108 | /** |
| 109 | * @brief Fetch the port number of the remote peer |
| 110 | * |
| 111 | * Returns the port number of the remote peer |
| 112 | * |
| 113 | * @return Port number |
| 114 | * |
| 115 | */ |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 116 | uint16_t getPort() const |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 117 | { |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 118 | if (sockAddrSize == sizeof(sockaddr_in)) |
| 119 | { |
| 120 | return ntohs(reinterpret_cast<const sockaddr_in*>(&remoteSockAddr) |
| 121 | ->sin_port); |
| 122 | } |
| 123 | if (sockAddrSize == sizeof(sockaddr_in6)) |
| 124 | { |
| 125 | return ntohs(reinterpret_cast<const sockaddr_in6*>(&remoteSockAddr) |
| 126 | ->sin6_port); |
| 127 | } |
| 128 | return 0; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 129 | } |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 130 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 131 | /** |
| 132 | * @brief Read the incoming packet |
| 133 | * |
| 134 | * Reads the data available on the socket |
| 135 | * |
| 136 | * @return A tuple with return code and vector with the buffer |
| 137 | * In case of success, the vector is populated with the data |
| 138 | * available on the socket and return code is 0. |
| 139 | * In case of error, the return code is < 0 and vector is set |
| 140 | * to size 0. |
| 141 | */ |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 142 | std::tuple<int, std::vector<uint8_t>> read() |
| 143 | { |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 144 | // cannot use the standard asio reading mechanism because it does not |
| 145 | // provide a mechanism to reach down into the depths and use a msghdr |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 146 | std::vector<uint8_t> packet(socket->available()); |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 147 | iovec iov = {packet.data(), packet.size()}; |
| 148 | char msgCtrl[1024]; |
| 149 | msghdr msg = {&remoteSockAddr, sizeof(remoteSockAddr), &iov, 1, |
| 150 | msgCtrl, sizeof(msgCtrl), 0}; |
| 151 | |
| 152 | ssize_t bytesReceived = recvmsg(socket->native_handle(), &msg, 0); |
| 153 | // Read of the packet failed |
| 154 | if (bytesReceived < 0) |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 155 | { |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 156 | // something bad happened; bail |
| 157 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 158 | "Error in recvmsg", |
| 159 | phosphor::logging::entry("ERROR=%s", strerror(errno))); |
| 160 | return std::make_tuple(-errno, std::vector<uint8_t>()); |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 161 | } |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 162 | // save the size of either ipv4 or i4v6 sockaddr |
| 163 | sockAddrSize = msg.msg_namelen; |
| 164 | |
| 165 | // extract the destination address from the message |
| 166 | cmsghdr* cmsg; |
| 167 | for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != 0; |
| 168 | cmsg = CMSG_NXTHDR(&msg, cmsg)) |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 169 | { |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 170 | if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_PKTINFO) |
| 171 | { |
| 172 | // save local address from the pktinfo4 |
| 173 | pktinfo4 = *reinterpret_cast<in_pktinfo*>(CMSG_DATA(cmsg)); |
| 174 | } |
| 175 | if (cmsg->cmsg_level == IPPROTO_IPV6 && |
| 176 | cmsg->cmsg_type == IPV6_PKTINFO) |
| 177 | { |
| 178 | // save local address from the pktinfo6 |
| 179 | pktinfo6 = *reinterpret_cast<in6_pktinfo*>(CMSG_DATA(cmsg)); |
| 180 | } |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 181 | } |
| 182 | return std::make_tuple(0, packet); |
| 183 | } |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 184 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 185 | /** |
| 186 | * @brief Write the outgoing packet |
| 187 | * |
| 188 | * Writes the data in the vector to the socket |
| 189 | * |
| 190 | * @param [in] inBuffer |
| 191 | * The vector would be the buffer of data to write to the socket. |
| 192 | * |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 193 | * @return In case of success the return code is the number of bytes |
| 194 | * written and return code is < 0 in case of failure. |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 195 | */ |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 196 | int write(const std::vector<uint8_t>& inBuffer) |
| 197 | { |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 198 | // in order to make sure packets go back out from the same |
| 199 | // IP address they came in on, sendmsg must be used instead |
| 200 | // of the boost::asio::ip::send or sendto |
| 201 | iovec iov = {const_cast<uint8_t*>(inBuffer.data()), inBuffer.size()}; |
| 202 | char msgCtrl[1024]; |
| 203 | msghdr msg = {&remoteSockAddr, sockAddrSize, &iov, 1, |
| 204 | msgCtrl, sizeof(msgCtrl), 0}; |
| 205 | int cmsg_space = 0; |
| 206 | cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
| 207 | if (pktinfo6) |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 208 | { |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 209 | cmsg->cmsg_level = IPPROTO_IPV6; |
| 210 | cmsg->cmsg_type = IPV6_PKTINFO; |
| 211 | cmsg->cmsg_len = CMSG_LEN(sizeof(in6_pktinfo)); |
| 212 | *reinterpret_cast<in6_pktinfo*>(CMSG_DATA(cmsg)) = *pktinfo6; |
| 213 | cmsg_space += CMSG_SPACE(sizeof(in6_pktinfo)); |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 214 | } |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 215 | else if (pktinfo4) |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 216 | { |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 217 | cmsg->cmsg_level = IPPROTO_IP; |
| 218 | cmsg->cmsg_type = IP_PKTINFO; |
| 219 | cmsg->cmsg_len = CMSG_LEN(sizeof(in_pktinfo)); |
| 220 | *reinterpret_cast<in_pktinfo*>(CMSG_DATA(cmsg)) = *pktinfo4; |
| 221 | cmsg_space += CMSG_SPACE(sizeof(in_pktinfo)); |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 222 | } |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 223 | msg.msg_controllen = cmsg_space; |
| 224 | int ret = sendmsg(socket->native_handle(), &msg, 0); |
| 225 | if (ret < 0) |
| 226 | { |
| 227 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 228 | "Error in sendmsg", |
| 229 | phosphor::logging::entry("ERROR=%s", strerror(errno))); |
| 230 | } |
| 231 | return ret; |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 232 | } |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 233 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 234 | /** |
| 235 | * @brief Returns file descriptor for the socket |
| 236 | */ |
| 237 | auto getHandle(void) const |
| 238 | { |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 239 | return socket->native_handle(); |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 240 | } |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 241 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 242 | private: |
Vernon Mauery | 7a0142c | 2018-11-09 08:38:16 -0800 | [diff] [blame] | 243 | std::shared_ptr<boost::asio::ip::udp::socket> socket; |
Vernon Mauery | d92bc32 | 2019-03-15 15:24:30 -0700 | [diff] [blame] | 244 | sockaddr_storage remoteSockAddr; |
| 245 | socklen_t sockAddrSize; |
| 246 | std::optional<in_pktinfo> pktinfo4; |
| 247 | std::optional<in6_pktinfo> pktinfo6; |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | } // namespace udpsocket |