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  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /** @file SocketAddress.h SocketAddress class */
18 /** \addtogroup netsocket
19  * @{*/
20 
21 #ifndef SOCKET_ADDRESS_H
22 #define SOCKET_ADDRESS_H
23 
24 #include <memory>
25 #include "nsapi_types.h"
26 #include "mbed_toolchain.h"
27 
28 // Predeclared classes
29 class NetworkStack;
30 class NetworkInterface;
31 
32 /** SocketAddress class
33  *
34  * Representation of an IP address and port pair.
35  */
37 public:
38  /** Create an unspecified SocketAddress
39  */
40  constexpr SocketAddress() = default;
41 
42  /** Create a SocketAddress from a raw IP address and port
43  *
44  * @note To construct from a host name, use @ref NetworkInterface::gethostbyname
45  *
46  * @param addr Raw IP address
47  * @param port Optional 16-bit port, defaults to 0
48  */
49  SocketAddress(const nsapi_addr_t &addr, uint16_t port = 0);
50 
51  /** Create a SocketAddress from an IP address and port
52  *
53  * @param addr Null-terminated representation of the IP address
54  * @param port Optional 16-bit port, defaults to 0
55  */
56  SocketAddress(const char *addr, uint16_t port = 0);
57 
58  /** Create a SocketAddress from raw IP bytes, IP version, and port
59  *
60  * @param bytes Raw IP address in big-endian order
61  * @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6
62  * @param port Optional 16-bit port, defaults to 0
63  */
64  SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port = 0);
65 
66  /** Create a SocketAddress from another SocketAddress
67  *
68  * @param addr SocketAddress to copy
69  */
70  SocketAddress(const SocketAddress &addr);
71 
72  /** Destructor */
73  ~SocketAddress() = default;
74 
75  /** Set the IP address
76  *
77  * @param addr Null-terminated represention of the IP address
78  * @return True if address is a valid representation of an IP address,
79  * otherwise False and SocketAddress is set to null
80  */
81  bool set_ip_address(const char *addr);
82 
83  /** Set the raw IP bytes and IP version
84  *
85  * @param bytes Raw IP address in big-endian order
86  * @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6
87  */
88  void set_ip_bytes(const void *bytes, nsapi_version_t version);
89 
90  /** Set the raw IP address
91  *
92  * @param addr Raw IP address
93  */
94  void set_addr(const nsapi_addr_t &addr);
95 
96  /** Set the port
97  *
98  * @param port 16-bit port
99  */
100  void set_port(uint16_t port)
101  {
102  _port = port;
103  }
104 
105  /** Get the human-readable IP address
106  *
107  * Allocates memory for a string and converts binary address to
108  * human-readable format. String is freed in the destructor.
109  *
110  * @return Null-terminated representation of the IP Address
111  */
112  const char *get_ip_address() const;
113 
114  /** Get the raw IP bytes
115  *
116  * @return Raw IP address in big-endian order
117  */
118  const void *get_ip_bytes() const
119  {
120  return _addr.bytes;
121  }
122 
123  /** Get the IP address version
124  *
125  * @return IP address version, NSAPI_IPv4 or NSAPI_IPv6
126  */
127  nsapi_version_t get_ip_version() const
128  {
129  return _addr.version;
130  }
131 
132  /** Get the raw IP address
133  *
134  * @return Raw IP address
135  */
137  {
138  return _addr;
139  }
140 
141  /** Get the port
142  *
143  * @return The 16-bit port
144  */
145  uint16_t get_port() const
146  {
147  return _port;
148  }
149 
150  /** Test if address is zero
151  *
152  * @return True if address is not zero
153  */
154  explicit operator bool() const;
155 
156  /** Copy address from another SocketAddress
157  *
158  * @param addr SocketAddress to copy
159  *
160  * @retval SocketAddress reference to this address
161  */
162  SocketAddress &operator=(const SocketAddress &addr);
163 
164  /** Compare two addresses for equality
165  *
166  * @return True if both addresses are equal
167  */
168  friend bool operator==(const SocketAddress &a, const SocketAddress &b);
169 
170  /** Compare two addresses for equality
171  *
172  * @return True if both addresses are not equal
173  */
174  friend bool operator!=(const SocketAddress &a, const SocketAddress &b);
175 
176 private:
177  mutable std::unique_ptr<char[]> _ip_address;
178  nsapi_addr_t _addr{};
179  uint16_t _port = 0;
180 };
181 
182 
183 #endif
184 
185 /** @}*/
friend bool operator==(const SocketAddress &a, const SocketAddress &b)
Compare two addresses for equality.
NetworkStack class.
Definition: NetworkStack.h:41
~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:36
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:201
uint16_t get_port() const
Get the port.
nsapi_version_t version
IP version.
Definition: nsapi_types.h:196
IP address structure for passing IP addresses by value.
Definition: nsapi_types.h:190
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.