1 /* 2 * Copyright (c) 2016, The OpenThread Authors. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. Neither the name of the copyright holder nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /** 30 * @file 31 * This file includes definitions for IPv6 sockets. 32 */ 33 34 #ifndef NET_SOCKET_HPP_ 35 #define NET_SOCKET_HPP_ 36 37 #include "openthread-core-config.h" 38 39 #include "common/clearable.hpp" 40 #include "common/equatable.hpp" 41 #include "net/ip6_address.hpp" 42 #include "net/ip6_types.hpp" 43 44 namespace ot { 45 46 class ThreadLinkInfo; 47 48 namespace Ip6 { 49 50 /** 51 * @addtogroup core-ip6-ip6 52 * 53 * @{ 54 */ 55 56 /** 57 * Implements message information for an IPv6 message. 58 */ 59 class MessageInfo : public otMessageInfo, public Clearable<MessageInfo> 60 { 61 public: 62 /** 63 * Initializes the object. 64 */ MessageInfo(void)65 MessageInfo(void) { Clear(); } 66 67 /** 68 * Returns a reference to the local socket address. 69 * 70 * @returns A reference to the local socket address. 71 */ GetSockAddr(void)72 Address &GetSockAddr(void) { return AsCoreType(&mSockAddr); } 73 74 /** 75 * Returns a reference to the local socket address. 76 * 77 * @returns A reference to the local socket address. 78 */ GetSockAddr(void) const79 const Address &GetSockAddr(void) const { return AsCoreType(&mSockAddr); } 80 81 /** 82 * Sets the local socket address. 83 * 84 * @param[in] aAddress The IPv6 address. 85 */ SetSockAddr(const Address & aAddress)86 void SetSockAddr(const Address &aAddress) { mSockAddr = aAddress; } 87 88 /** 89 * Gets the local socket port. 90 * 91 * @returns The local socket port. 92 */ GetSockPort(void) const93 uint16_t GetSockPort(void) const { return mSockPort; } 94 95 /** 96 * Gets the local socket port. 97 * 98 * @param[in] aPort The port value. 99 */ SetSockPort(uint16_t aPort)100 void SetSockPort(uint16_t aPort) { mSockPort = aPort; } 101 102 /** 103 * Returns a reference to the peer socket address. 104 * 105 * @returns A reference to the peer socket address. 106 */ GetPeerAddr(void)107 Address &GetPeerAddr(void) { return AsCoreType(&mPeerAddr); } 108 109 /** 110 * Returns a reference to the peer socket address. 111 * 112 * @returns A reference to the peer socket address. 113 */ GetPeerAddr(void) const114 const Address &GetPeerAddr(void) const { return AsCoreType(&mPeerAddr); } 115 116 /** 117 * Sets the peer's socket address. 118 * 119 * @param[in] aAddress The IPv6 address. 120 */ SetPeerAddr(const Address & aAddress)121 void SetPeerAddr(const Address &aAddress) { mPeerAddr = aAddress; } 122 123 /** 124 * Gets the peer socket port. 125 * 126 * @returns The peer socket port. 127 */ GetPeerPort(void) const128 uint16_t GetPeerPort(void) const { return mPeerPort; } 129 130 /** 131 * Gets the peer socket port. 132 * 133 * @param[in] aPort The port value. 134 */ SetPeerPort(uint16_t aPort)135 void SetPeerPort(uint16_t aPort) { mPeerPort = aPort; } 136 137 /** 138 * Gets the Hop Limit. 139 * 140 * @returns The Hop Limit. 141 */ GetHopLimit(void) const142 uint8_t GetHopLimit(void) const { return mHopLimit; } 143 144 /** 145 * Sets the Hop Limit. 146 * 147 * @param[in] aHopLimit The Hop Limit. 148 */ SetHopLimit(uint8_t aHopLimit)149 void SetHopLimit(uint8_t aHopLimit) { mHopLimit = aHopLimit; } 150 151 /** 152 * Returns whether multicast may be looped back. 153 * 154 * @retval TRUE If message may be looped back. 155 * @retval FALSE If message must not be looped back. 156 */ GetMulticastLoop(void) const157 bool GetMulticastLoop(void) const { return mMulticastLoop; } 158 159 /** 160 * Sets whether multicast may be looped back. 161 * 162 * @param[in] aMulticastLoop Whether allow looping back multicast. 163 */ SetMulticastLoop(bool aMulticastLoop)164 void SetMulticastLoop(bool aMulticastLoop) { mMulticastLoop = aMulticastLoop; } 165 166 /** 167 * Gets the ECN status. 168 * 169 * @returns The ECN status, as represented in the IP header. 170 */ GetEcn(void) const171 Ecn GetEcn(void) const { return static_cast<Ecn>(mEcn); } 172 173 /** 174 * Sets the ECN status. 175 * 176 * @param[in] aEcn The ECN status, as represented in the IP header. 177 */ SetEcn(Ecn aEcn)178 void SetEcn(Ecn aEcn) { mEcn = aEcn; } 179 180 /** 181 * Indicates whether peer is via the host interface. 182 * 183 * @retval TRUE if the peer is via the host interface. 184 * @retval FALSE if the peer is via the Thread interface. 185 */ IsHostInterface(void) const186 bool IsHostInterface(void) const { return mIsHostInterface; } 187 188 /** 189 * Indicates whether or not to apply hop limit 0. 190 * 191 * @retval TRUE if applying hop limit 0 when `mHopLimit` field is 0. 192 * @retval FALSE if applying default `OPENTHREAD_CONFIG_IP6_HOP_LIMIT_DEFAULT` when `mHopLimit` field is 0. 193 */ ShouldAllowZeroHopLimit(void) const194 bool ShouldAllowZeroHopLimit(void) const { return mAllowZeroHopLimit; } 195 196 /** 197 * Sets whether the peer is via the host interface. 198 * 199 * @param[in] aIsHost TRUE if the peer is via the host interface, FALSE otherwise. 200 */ SetIsHostInterface(bool aIsHost)201 void SetIsHostInterface(bool aIsHost) { mIsHostInterface = aIsHost; } 202 203 /** 204 * Checks if the peer address and port match those of another `MessageInfo`. 205 * 206 * @param[in] aOther The other `MessageInfo` to compare with. 207 * 208 * @retval TRUE The peer address and port of the two `MessageInfo` objects match. 209 * @retval FALSE The peer address and port of the two `MessageInfo` objects do not match. 210 */ 211 bool HasSamePeerAddrAndPort(const MessageInfo &aOther) const; 212 }; 213 214 /** 215 * Implements a socket address. 216 */ 217 class SockAddr : public otSockAddr, public Clearable<SockAddr>, public Unequatable<SockAddr> 218 { 219 public: 220 static constexpr uint16_t kInfoStringSize = OT_IP6_SOCK_ADDR_STRING_SIZE; ///< Info string size (`ToString()`). 221 222 /** 223 * Defines the fixed-length `String` object returned from `ToString()`. 224 */ 225 typedef String<kInfoStringSize> InfoString; 226 227 /** 228 * Initializes the socket address (all fields are set to zero). 229 */ SockAddr(void)230 SockAddr(void) { Clear(); } 231 232 /** 233 * Initializes the socket address with a given port number. 234 * 235 * @param[in] aPort A port number. 236 */ SockAddr(uint16_t aPort)237 explicit SockAddr(uint16_t aPort) 238 { 239 mPort = aPort; 240 GetAddress().Clear(); 241 } 242 243 /** 244 * Initializes the socket address with a given address and port number. 245 * 246 * @param[in] aAddress An IPv6 address. 247 * @param[in] aPort A port number. 248 */ SockAddr(const Address & aAddress,uint16_t aPort)249 SockAddr(const Address &aAddress, uint16_t aPort) 250 { 251 mAddress = aAddress; 252 mPort = aPort; 253 } 254 255 /** 256 * Returns a reference to the IPv6 address. 257 * 258 * @returns A reference to the IPv6 address. 259 */ GetAddress(void)260 Address &GetAddress(void) { return AsCoreType(&mAddress); } 261 262 /** 263 * Returns a reference to the IPv6 address. 264 * 265 * @returns A reference to the IPv6 address. 266 */ GetAddress(void) const267 const Address &GetAddress(void) const { return AsCoreType(&mAddress); } 268 269 /** 270 * Sets the IPv6 address. 271 * 272 * @param[in] aAddress The IPv6 address. 273 */ SetAddress(const Address & aAddress)274 void SetAddress(const Address &aAddress) { mAddress = aAddress; } 275 276 /** 277 * Returns the socket address port number. 278 * 279 * @returns The port number 280 */ GetPort(void) const281 uint16_t GetPort(void) const { return mPort; } 282 283 /** 284 * Sets the socket address port number. 285 * 286 * @param[in] aPort The port number. 287 */ SetPort(uint16_t aPort)288 void SetPort(uint16_t aPort) { mPort = aPort; } 289 290 /** 291 * Overloads operator `==` to evaluate whether or not two `SockAddr` instances are equal (same address 292 * and port number). 293 * 294 * @param[in] aOther The other `SockAddr` instance to compare with. 295 * 296 * @retval TRUE If the two `SockAddr` instances are equal. 297 * @retval FALSE If the two `SockAddr` instances not equal. 298 */ operator ==(const SockAddr & aOther) const299 bool operator==(const SockAddr &aOther) const 300 { 301 return (GetPort() == aOther.GetPort()) && (GetAddress() == aOther.GetAddress()); 302 } 303 304 /** 305 * Converts the socket address to a string. 306 * 307 * The string is formatted as "[<ipv6 address>]:<port number>". 308 * 309 * @returns An `InfoString` containing the string representation of the `SockAddr` 310 */ 311 InfoString ToString(void) const; 312 313 /** 314 * Converts a given IPv6 socket address to a human-readable string. 315 * 316 * The IPv6 socket address string is formatted as "[<ipv6 address>]:<port>". 317 * 318 * If the resulting string does not fit in @p aBuffer (within its @p aSize characters), the string will be 319 * truncated but the outputted string is always null-terminated. 320 * 321 * @param[out] aBuffer A pointer to a char array to output the string (MUST NOT be NULL). 322 * @param[in] aSize The size of @p aBuffer (in bytes). 323 */ 324 void ToString(char *aBuffer, uint16_t aSize) const; 325 326 private: 327 void ToString(StringWriter &aWriter) const; 328 }; 329 330 /** 331 * @} 332 */ 333 334 } // namespace Ip6 335 336 DefineCoreType(otMessageInfo, Ip6::MessageInfo); 337 DefineCoreType(otSockAddr, Ip6::SockAddr); 338 339 } // namespace ot 340 341 #endif // NET_SOCKET_HPP_ 342