Mistake on this page?
Report an issue in GitHub or email us
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
30 class NetworkStack;
31 class NetworkInterface;
32 
33 /** SocketAddress class
34  *
35  * Representation of an IP address and port pair.
36  */
38 public:
39  /** Create an unspecified SocketAddress
40  */
41  constexpr SocketAddress() = default;
42 
43  /** Create a SocketAddress from a raw IP address and port
44  *
45  * @note To construct from a host name, use @ref NetworkInterface::gethostbyname
46  *
47  * @param addr Raw IP address
48  * @param port Optional 16-bit port, defaults to 0
49  */
50  SocketAddress(const nsapi_addr_t &addr, uint16_t port = 0);
51 
52  /** Create a SocketAddress from an IP address and port
53  *
54  * @param addr Null-terminated representation of the IP address
55  * @param port Optional 16-bit port, defaults to 0
56  */
57  SocketAddress(const char *addr, uint16_t port = 0);
58 
59  /** Create a SocketAddress from raw IP bytes, IP version, and port
60  *
61  * @param bytes Raw IP address in big-endian order
62  * @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6
63  * @param port Optional 16-bit port, defaults to 0
64  */
65  SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port = 0);
66 
67  /** Create a SocketAddress from another SocketAddress
68  *
69  * @param addr SocketAddress to copy
70  */
71  SocketAddress(const SocketAddress &addr);
72 
73  /** Destructor */
74  ~SocketAddress() = default;
75 
76  /** Set the IP address
77  *
78  * @param addr Null-terminated represention of the IP address
79  * @return True if address is a valid representation of an IP address,
80  * otherwise False and SocketAddress is set to null
81  */
82  bool set_ip_address(const char *addr);
83 
84  /** Set the raw IP bytes and IP version
85  *
86  * @param bytes Raw IP address in big-endian order
87  * @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6
88  */
89  void set_ip_bytes(const void *bytes, nsapi_version_t version);
90 
91  /** Set the raw IP address
92  *
93  * @param addr Raw IP address
94  */
95  void set_addr(const nsapi_addr_t &addr);
96 
97  /** Set the port
98  *
99  * @param port 16-bit port
100  */
101  void set_port(uint16_t port)
102  {
103  _port = port;
104  }
105 
106  /** Get the human-readable IP address
107  *
108  * Allocates memory for a string and converts binary address to
109  * human-readable format. String is freed in the destructor.
110  *
111  * @return Null-terminated representation of the IP Address
112  */
113  const char *get_ip_address() const;
114 
115  /** Get the raw IP bytes
116  *
117  * @return Raw IP address in big-endian order
118  */
119  const void *get_ip_bytes() const
120  {
121  return _addr.bytes;
122  }
123 
124  /** Get the IP address version
125  *
126  * @return IP address version, NSAPI_IPv4 or NSAPI_IPv6
127  */
128  nsapi_version_t get_ip_version() const
129  {
130  return _addr.version;
131  }
132 
133  /** Get the raw IP address
134  *
135  * @return Raw IP address
136  */
138  {
139  return _addr;
140  }
141 
142  /** Get the port
143  *
144  * @return The 16-bit port
145  */
146  uint16_t get_port() const
147  {
148  return _port;
149  }
150 
151  /** Test if address is zero
152  *
153  * @return True if address is not zero
154  */
155  explicit operator bool() const;
156 
157  /** Copy address from another SocketAddress
158  *
159  * @param addr SocketAddress to copy
160  *
161  * @retval SocketAddress reference to this address
162  */
163  SocketAddress &operator=(const SocketAddress &addr);
164 
165  /** Compare two addresses for equality
166  *
167  * @return True if both addresses are equal
168  */
169  friend bool operator==(const SocketAddress &a, const SocketAddress &b);
170 
171  /** Compare two addresses for equality
172  *
173  * @return True if both addresses are not equal
174  */
175  friend bool operator!=(const SocketAddress &a, const SocketAddress &b);
176 
177 private:
178  mutable std::unique_ptr<char[]> _ip_address;
179  nsapi_addr_t _addr{};
180  uint16_t _port = 0;
181 };
182 
183 
184 #endif
185 
186 /** @}*/
friend bool operator==(const SocketAddress &a, const SocketAddress &b)
Compare two addresses for equality.
NetworkStack class.
Definition: NetworkStack.h:42
~SocketAddress()=default
Destructor.
friend bool operator!=(const SocketAddress &a, const SocketAddress &b)
Compare two addresses for equality.
bool set_ip_address(const char *addr)
Set the IP address.
void set_ip_bytes(const void *bytes, nsapi_version_t version)
Set the raw IP bytes and IP version.
const char * get_ip_address() const
Get the human-readable IP address.
void set_port(uint16_t port)
Set the port.
constexpr SocketAddress()=default
Create an unspecified SocketAddress.
SocketAddress & operator=(const SocketAddress &addr)
Copy address from another SocketAddress.
nsapi_version_t get_ip_version() const
Get the IP address version.
SocketAddress class.
Definition: SocketAddress.h:37
Common interface that is shared between network devices.
uint8_t bytes[16]
IP address The raw bytes of the IP address stored in big-endian format.
Definition: nsapi_types.h:246
uint16_t get_port() const
Get the port.
nsapi_version_t version
IP version.
Definition: nsapi_types.h:241
IP address structure for passing IP addresses by value.
Definition: nsapi_types.h:235
nsapi_addr_t get_addr() const
Get the raw IP address.
const void * get_ip_bytes() const
Get the raw IP bytes.
void set_addr(const nsapi_addr_t &addr)
Set the raw IP address.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.