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 "nsapi_types.h"
25 #include "mbed_toolchain.h"
26 
27 // Predeclared classes
28 class NetworkStack;
29 class NetworkInterface;
30 
31 /** SocketAddress class
32  *
33  * Representation of an IP address and port pair.
34  */
36 public:
37  /** Create a SocketAddress from a hostname and port
38  *
39  * The hostname may be either a domain name or an IP address. If the
40  * hostname is an IP address, no network transactions will be performed.
41  *
42  * On failure, the IP address and port will be set to zero
43  *
44  * @tparam S Type of the Network stack
45  * @param stack Network stack to use for DNS resolution
46  * @param host Hostname to resolve
47  * @param port Optional 16-bit port, defaults to 0
48  * @deprecated
49  * Constructors hide possible errors. Replaced by
50  * NetworkInterface::gethostbyname.
51  */
52  template <typename S>
53  MBED_DEPRECATED_SINCE("mbed-os-5.1.3",
54  "Constructors hide possible errors. Replaced by "
55  "NetworkInterface::gethostbyname.")
56  SocketAddress(S *stack, const char *host, uint16_t port = 0)
57  {
58  _SocketAddress(nsapi_create_stack(stack), host, port);
59  }
60 
61  /** Create a SocketAddress from a raw IP address and port
62  *
63  * @note To construct from a host name, use NetworkInterface::gethostbyname
64  *
65  * @param addr Raw IP address
66  * @param port Optional 16-bit port, defaults to 0
67  */
68  SocketAddress(nsapi_addr_t addr = nsapi_addr_t(), uint16_t port = 0);
69 
70  /** Create a SocketAddress from an IP address and port
71  *
72  * @param addr Null-terminated representation of the IP address
73  * @param port Optional 16-bit port, defaults to 0
74  */
75  SocketAddress(const char *addr, uint16_t port = 0);
76 
77  /** Create a SocketAddress from raw IP bytes, IP version, and port
78  *
79  * @param bytes Raw IP address in big-endian order
80  * @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6
81  * @param port Optional 16-bit port, defaults to 0
82  */
83  SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port = 0);
84 
85  /** Create a SocketAddress from another SocketAddress
86  *
87  * @param addr SocketAddress to copy
88  */
89  SocketAddress(const SocketAddress &addr);
90 
91  /** Destructor */
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(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  /** Get the human-readable IP address
122  *
123  * Allocates memory for a string and converts binary address to
124  * human-readable format. String is freed in the destructor.
125  *
126  * @return Null-terminated representation of the IP Address
127  */
128  const char *get_ip_address() const;
129 
130  /** Get the raw IP bytes
131  *
132  * @return Raw IP address in big-endian order
133  */
134  const void *get_ip_bytes() const;
135 
136  /** Get the IP address version
137  *
138  * @return IP address version, NSAPI_IPv4 or NSAPI_IPv6
139  */
140  nsapi_version_t get_ip_version() const;
141 
142  /** Get the raw IP address
143  *
144  * @return Raw IP address
145  */
146  nsapi_addr_t get_addr() const;
147 
148  /** Get the port
149  *
150  * @return The 16-bit port
151  */
152  uint16_t get_port() const;
153 
154  /** Test if address is zero
155  *
156  * @return True if address is not zero
157  */
158  operator bool() const;
159 
160  /** Copy address from another SocketAddress
161  *
162  * @param addr SocketAddress to copy
163  */
164  SocketAddress &operator=(const SocketAddress &addr);
165 
166  /** Compare two addresses for equality
167  *
168  * @return True if both addresses are equal
169  */
170  friend bool operator==(const SocketAddress &a, const SocketAddress &b);
171 
172  /** Compare two addresses for equality
173  *
174  * @return True if both addresses are not equal
175  */
176  friend bool operator!=(const SocketAddress &a, const SocketAddress &b);
177 
178 private:
179  void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);
180 
181  /** Initialize memory */
182  void mem_init(void);
183 
184  mutable char *_ip_address;
185  nsapi_addr_t _addr;
186  uint16_t _port;
187 };
188 
189 
190 #endif
191 
192 /** @}*/
NetworkStack * nsapi_create_stack(nsapi_stack_t *stack)
Convert a raw nsapi_stack_t object into a C++ NetworkStack object.
friend bool operator==(const SocketAddress &a, const SocketAddress &b)
Compare two addresses for equality.
NetworkStack class.
Definition: NetworkStack.h:40
friend bool operator!=(const SocketAddress &a, const SocketAddress &b)
Compare two addresses for equality.
void set_addr(nsapi_addr_t addr)
Set the raw IP address.
bool set_ip_address(const char *addr)
Set the IP address.
~SocketAddress()
Destructor.
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.
struct nsapi_addr nsapi_addr_t
IP address structure for passing IP addresses by value.
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:35
Common interface that is shared between network devices.
uint16_t get_port() const
Get the port.
IP address structure for passing IP addresses by value.
Definition: nsapi_types.h:187
nsapi_addr_t get_addr() const
Get the raw IP address.
SocketAddress(S *stack, const char *host, uint16_t port=0)
Create a SocketAddress from a hostname and port.
Definition: SocketAddress.h:56
const void * get_ip_bytes() const
Get the raw IP bytes.
#define MBED_DEPRECATED_SINCE(D, M)
MBED_DEPRECATED("message string") Mark a function declaration as deprecated, if it used then a warnin...
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.