Mbed OS Reference
Loading...
Searching...
No Matches
SocketAddress.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/** @file SocketAddress.h SocketAddress class */
19/** \addtogroup NetSocket
20 * @{*/
21
22#ifndef SOCKET_ADDRESS_H
23#define SOCKET_ADDRESS_H
24
25#include <memory>
26#include "nsapi_types.h"
27#include "mbed_toolchain.h"
28
29// Predeclared classes
30class NetworkStack;
32
33/**
34 * @brief Representation of an IP (v4 or v6) address and port pair.
35 *
36 * Each SocketAddress contains the IP address bytes and the port number. Also, when the IP address
37 * is first used as a string (e.g. for \c get_ip_address() ), a character string is allocated on the heap via
38 * a unique_ptr inside the SocketAddress. This character string as long as the SocketAddress does, but not longer.
39 */
41public:
42 /**
43 * @brief Create an empty SocketAddress
44 */
45 constexpr SocketAddress() = default;
46
47 /** Create a SocketAddress from a raw IP address and port
48 *
49 * @note To construct from a host name, use @ref NetworkInterface::gethostbyname
50 *
51 * @param addr Raw IP address
52 * @param port Optional 16-bit port, defaults to 0
53 */
54 SocketAddress(const nsapi_addr_t &addr, uint16_t port = 0);
55
56 /**
57 * @brief Create a SocketAddress from an IP address and port
58 *
59 * If the string is not a parseable IP address, an empty SocketAddress is returned (see
60 * \c is_empty() below).
61 *
62 * @param addr Null-terminated string representation of the IP address
63 * @param port Optional 16-bit port, defaults to 0
64 */
65 SocketAddress(const char *addr, uint16_t port = 0);
66
67 /** Create a SocketAddress from raw IP bytes, IP version, and port
68 *
69 * @param bytes Raw IP address in big-endian order
70 * @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6
71 * @param port Optional 16-bit port, defaults to 0
72 */
73 SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port = 0);
74
75 /**
76 * Create a SocketAddress from another SocketAddress
77 *
78 * @param addr SocketAddress to copy
79 */
81
82 /**
83 * @brief Create a SocketAddress by moving another SocketAddress.
84 *
85 * This transfers ownership of the string data, if allocated.
86 *
87 * @param addr SocketAddress to move from
88 */
89 SocketAddress(SocketAddress &&addr) = default;
90
91 /** Destructor */
92 ~SocketAddress() = default;
93
94 /** Set the IP address
95 *
96 * @param addr Null-terminated represention of the IP address
97 * @return True if address is a valid representation of an IP address,
98 * otherwise False and SocketAddress is set to null
99 */
100 bool set_ip_address(const char *addr);
101
102 /** Set the raw IP bytes and IP version
103 *
104 * @param bytes Raw IP address in big-endian order
105 * @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6
106 */
107 void set_ip_bytes(const void *bytes, nsapi_version_t version);
108
109 /** Set the raw IP address
110 *
111 * @param addr Raw IP address
112 */
113 void set_addr(const nsapi_addr_t &addr);
114
115 /** Set the port
116 *
117 * @param port 16-bit port
118 */
119 void set_port(uint16_t port)
120 {
121 _port = port;
122 }
123
124 /** Get the human-readable IP address
125 *
126 * Allocates memory for a string and converts binary address to
127 * human-readable format. String is freed in the destructor.
128 *
129 * @return Null-terminated representation of the IP Address
130 */
131 const char *get_ip_address() const;
132
133 /** Get the raw IP bytes
134 *
135 * @return Raw IP address in big-endian order
136 */
137 const void *get_ip_bytes() const
138 {
139 return _addr.bytes;
140 }
141
142 /** Get the IP address version
143 *
144 * @return IP address version, NSAPI_IPv4 or NSAPI_IPv6
145 */
146 nsapi_version_t get_ip_version() const
147 {
148 return _addr.version;
149 }
150
151 /** Get the raw IP address
152 *
153 * @return Raw IP address
154 */
156 {
157 return _addr;
158 }
159
160 /** Get the port
161 *
162 * @return The 16-bit port
163 */
164 uint16_t get_port() const
165 {
166 return _port;
167 }
168
169 /**
170 * @brief Test if address is zero or empty
171 *
172 * @return True if address is not empty and not zero
173 */
174 explicit operator bool() const;
175
176 /**
177 * @return True iff this IP address is empty (does not contain a valid value).
178 */
179 bool is_empty() const
180 {
181 return _addr.version == NSAPI_UNSPEC;
182 }
183
184 /** Copy address from another SocketAddress
185 *
186 * @param addr SocketAddress to copy
187 *
188 * @retval SocketAddress reference to this address
189 */
191
192 /** Compare two addresses for equality
193 *
194 * @return True if both addresses are equal
195 */
196 friend bool operator==(const SocketAddress &a, const SocketAddress &b);
197
198 /** Compare two addresses for equality
199 *
200 * @return True if both addresses are not equal
201 */
202 friend bool operator!=(const SocketAddress &a, const SocketAddress &b);
203
204private:
205 mutable std::unique_ptr<char[]> _ip_address;
206 nsapi_addr_t _addr{};
207 uint16_t _port = 0;
208};
209
210
211#endif
212
213/** @}*/
Common interface that is shared between network devices.
NetworkStack class.
Representation of an IP (v4 or v6) address and port pair.
void set_addr(const nsapi_addr_t &addr)
Set the raw IP address.
const void * get_ip_bytes() const
Get the raw IP bytes.
SocketAddress & operator=(const SocketAddress &addr)
Copy address from another SocketAddress.
SocketAddress(const char *addr, uint16_t port=0)
Create a SocketAddress from an IP address and port.
uint16_t get_port() const
Get the port.
bool is_empty() const
~SocketAddress()=default
Destructor.
const char * get_ip_address() const
Get the human-readable IP address.
bool set_ip_address(const char *addr)
Set the IP address.
void set_port(uint16_t port)
Set the port.
SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port=0)
Create a SocketAddress from raw IP bytes, IP version, and port.
friend bool operator==(const SocketAddress &a, const SocketAddress &b)
Compare two addresses for equality.
SocketAddress(SocketAddress &&addr)=default
Create a SocketAddress by moving another SocketAddress.
nsapi_addr_t get_addr() const
Get the raw IP address.
SocketAddress(const SocketAddress &addr)
Create a SocketAddress from another SocketAddress.
constexpr SocketAddress()=default
Create an empty SocketAddress.
nsapi_version_t get_ip_version() const
Get the IP address version.
SocketAddress(const nsapi_addr_t &addr, uint16_t port=0)
Create a SocketAddress from a raw IP address and port.
void set_ip_bytes(const void *bytes, nsapi_version_t version)
Set the raw IP bytes and IP version.
friend bool operator!=(const SocketAddress &a, const SocketAddress &b)
Compare two addresses for equality.
@ NSAPI_UNSPEC
IP address structure for passing IP addresses by value.
nsapi_version_t version
IP version.
uint8_t bytes[16]
IP address The raw bytes of the IP address stored in big-endian format.