123
SocketAddress.h@19:543c2d21e510, 2022-06-06 (annotated)
- Committer:
- advxolltm
- Date:
- Mon Jun 06 16:36:52 2022 +0000
- Revision:
- 19:543c2d21e510
- Parent:
- 14:7648334eb41b
123
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 14:7648334eb41b | 1 | /* |
hudakz | 14:7648334eb41b | 2 | * Copyright (c) 2015 ARM Limited |
hudakz | 14:7648334eb41b | 3 | * |
hudakz | 14:7648334eb41b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
hudakz | 14:7648334eb41b | 5 | * you may not use this file except in compliance with the License. |
hudakz | 14:7648334eb41b | 6 | * You may obtain a copy of the License at |
hudakz | 14:7648334eb41b | 7 | * |
hudakz | 14:7648334eb41b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
hudakz | 14:7648334eb41b | 9 | * |
hudakz | 14:7648334eb41b | 10 | * Unless required by applicable law or agreed to in writing, software |
hudakz | 14:7648334eb41b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
hudakz | 14:7648334eb41b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
hudakz | 14:7648334eb41b | 13 | * See the License for the specific language governing permissions and |
hudakz | 14:7648334eb41b | 14 | * limitations under the License. |
hudakz | 14:7648334eb41b | 15 | */ |
hudakz | 14:7648334eb41b | 16 | |
hudakz | 14:7648334eb41b | 17 | /** @file SocketAddress.h SocketAddress class */ |
hudakz | 14:7648334eb41b | 18 | /** \addtogroup netsocket |
hudakz | 14:7648334eb41b | 19 | * @{*/ |
hudakz | 14:7648334eb41b | 20 | |
hudakz | 14:7648334eb41b | 21 | #ifndef SOCKET_ADDRESS_H |
hudakz | 14:7648334eb41b | 22 | #define SOCKET_ADDRESS_H |
hudakz | 14:7648334eb41b | 23 | |
hudakz | 14:7648334eb41b | 24 | #include "nsapi_types.h" |
hudakz | 14:7648334eb41b | 25 | #include "mbed_toolchain.h" |
hudakz | 14:7648334eb41b | 26 | |
hudakz | 14:7648334eb41b | 27 | // Predeclared classes |
hudakz | 14:7648334eb41b | 28 | class NetworkStack; |
hudakz | 14:7648334eb41b | 29 | class NetworkInterface; |
hudakz | 14:7648334eb41b | 30 | |
hudakz | 14:7648334eb41b | 31 | /** SocketAddress class |
hudakz | 14:7648334eb41b | 32 | * |
hudakz | 14:7648334eb41b | 33 | * Representation of an IP address and port pair. |
hudakz | 14:7648334eb41b | 34 | */ |
hudakz | 14:7648334eb41b | 35 | class SocketAddress { |
hudakz | 14:7648334eb41b | 36 | public: |
hudakz | 14:7648334eb41b | 37 | /** Create a SocketAddress from a hostname and port |
hudakz | 14:7648334eb41b | 38 | * |
hudakz | 14:7648334eb41b | 39 | * The hostname may be either a domain name or an IP address. If the |
hudakz | 14:7648334eb41b | 40 | * hostname is an IP address, no network transactions will be performed. |
hudakz | 14:7648334eb41b | 41 | * |
hudakz | 14:7648334eb41b | 42 | * On failure, the IP address and port will be set to zero |
hudakz | 14:7648334eb41b | 43 | * |
hudakz | 14:7648334eb41b | 44 | * @tparam S Type of the Network stack |
hudakz | 14:7648334eb41b | 45 | * @param stack Network stack to use for DNS resolution |
hudakz | 14:7648334eb41b | 46 | * @param host Hostname to resolve |
hudakz | 14:7648334eb41b | 47 | * @param port Optional 16-bit port, defaults to 0 |
hudakz | 14:7648334eb41b | 48 | * @deprecated |
hudakz | 14:7648334eb41b | 49 | * Constructors hide possible errors. Replaced by |
hudakz | 14:7648334eb41b | 50 | * NetworkInterface::gethostbyname. |
hudakz | 14:7648334eb41b | 51 | */ |
hudakz | 14:7648334eb41b | 52 | template <typename S> |
hudakz | 14:7648334eb41b | 53 | MBED_DEPRECATED_SINCE("mbed-os-5.1.3", |
hudakz | 14:7648334eb41b | 54 | "Constructors hide possible errors. Replaced by " |
hudakz | 14:7648334eb41b | 55 | "NetworkInterface::gethostbyname.") |
hudakz | 14:7648334eb41b | 56 | SocketAddress(S *stack, const char *host, uint16_t port = 0) |
hudakz | 14:7648334eb41b | 57 | { |
hudakz | 14:7648334eb41b | 58 | _SocketAddress(nsapi_create_stack(stack), host, port); |
hudakz | 14:7648334eb41b | 59 | } |
hudakz | 14:7648334eb41b | 60 | |
hudakz | 14:7648334eb41b | 61 | /** Create a SocketAddress from a raw IP address and port |
hudakz | 14:7648334eb41b | 62 | * |
hudakz | 14:7648334eb41b | 63 | * @note To construct from a host name, use NetworkInterface::gethostbyname |
hudakz | 14:7648334eb41b | 64 | * |
hudakz | 14:7648334eb41b | 65 | * @param addr Raw IP address |
hudakz | 14:7648334eb41b | 66 | * @param port Optional 16-bit port, defaults to 0 |
hudakz | 14:7648334eb41b | 67 | */ |
hudakz | 14:7648334eb41b | 68 | SocketAddress(nsapi_addr_t addr = nsapi_addr_t(), uint16_t port = 0); |
hudakz | 14:7648334eb41b | 69 | |
hudakz | 14:7648334eb41b | 70 | /** Create a SocketAddress from an IP address and port |
hudakz | 14:7648334eb41b | 71 | * |
hudakz | 14:7648334eb41b | 72 | * @param addr Null-terminated representation of the IP address |
hudakz | 14:7648334eb41b | 73 | * @param port Optional 16-bit port, defaults to 0 |
hudakz | 14:7648334eb41b | 74 | */ |
hudakz | 14:7648334eb41b | 75 | SocketAddress(const char *addr, uint16_t port = 0); |
hudakz | 14:7648334eb41b | 76 | |
hudakz | 14:7648334eb41b | 77 | /** Create a SocketAddress from raw IP bytes, IP version, and port |
hudakz | 14:7648334eb41b | 78 | * |
hudakz | 14:7648334eb41b | 79 | * @param bytes Raw IP address in big-endian order |
hudakz | 14:7648334eb41b | 80 | * @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6 |
hudakz | 14:7648334eb41b | 81 | * @param port Optional 16-bit port, defaults to 0 |
hudakz | 14:7648334eb41b | 82 | */ |
hudakz | 14:7648334eb41b | 83 | SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port = 0); |
hudakz | 14:7648334eb41b | 84 | |
hudakz | 14:7648334eb41b | 85 | /** Create a SocketAddress from another SocketAddress |
hudakz | 14:7648334eb41b | 86 | * |
hudakz | 14:7648334eb41b | 87 | * @param addr SocketAddress to copy |
hudakz | 14:7648334eb41b | 88 | */ |
hudakz | 14:7648334eb41b | 89 | SocketAddress(const SocketAddress &addr); |
hudakz | 14:7648334eb41b | 90 | |
hudakz | 14:7648334eb41b | 91 | /** Destructor */ |
hudakz | 14:7648334eb41b | 92 | ~SocketAddress(); |
hudakz | 14:7648334eb41b | 93 | |
hudakz | 14:7648334eb41b | 94 | /** Set the IP address |
hudakz | 14:7648334eb41b | 95 | * |
hudakz | 14:7648334eb41b | 96 | * @param addr Null-terminated represention of the IP address |
hudakz | 14:7648334eb41b | 97 | * @return True if address is a valid representation of an IP address, |
hudakz | 14:7648334eb41b | 98 | * otherwise False and SocketAddress is set to null |
hudakz | 14:7648334eb41b | 99 | */ |
hudakz | 14:7648334eb41b | 100 | bool set_ip_address(const char *addr); |
hudakz | 14:7648334eb41b | 101 | |
hudakz | 14:7648334eb41b | 102 | /** Set the raw IP bytes and IP version |
hudakz | 14:7648334eb41b | 103 | * |
hudakz | 14:7648334eb41b | 104 | * @param bytes Raw IP address in big-endian order |
hudakz | 14:7648334eb41b | 105 | * @param version IP address version, NSAPI_IPv4 or NSAPI_IPv6 |
hudakz | 14:7648334eb41b | 106 | */ |
hudakz | 14:7648334eb41b | 107 | void set_ip_bytes(const void *bytes, nsapi_version_t version); |
hudakz | 14:7648334eb41b | 108 | |
hudakz | 14:7648334eb41b | 109 | /** Set the raw IP address |
hudakz | 14:7648334eb41b | 110 | * |
hudakz | 14:7648334eb41b | 111 | * @param addr Raw IP address |
hudakz | 14:7648334eb41b | 112 | */ |
hudakz | 14:7648334eb41b | 113 | void set_addr(nsapi_addr_t addr); |
hudakz | 14:7648334eb41b | 114 | |
hudakz | 14:7648334eb41b | 115 | /** Set the port |
hudakz | 14:7648334eb41b | 116 | * |
hudakz | 14:7648334eb41b | 117 | * @param port 16-bit port |
hudakz | 14:7648334eb41b | 118 | */ |
hudakz | 14:7648334eb41b | 119 | void set_port(uint16_t port); |
hudakz | 14:7648334eb41b | 120 | |
hudakz | 14:7648334eb41b | 121 | /** Get the human-readable IP address |
hudakz | 14:7648334eb41b | 122 | * |
hudakz | 14:7648334eb41b | 123 | * Allocates memory for a string and converts binary address to |
hudakz | 14:7648334eb41b | 124 | * human-readable format. String is freed in the destructor. |
hudakz | 14:7648334eb41b | 125 | * |
hudakz | 14:7648334eb41b | 126 | * @return Null-terminated representation of the IP Address |
hudakz | 14:7648334eb41b | 127 | */ |
hudakz | 14:7648334eb41b | 128 | const char *get_ip_address() const; |
hudakz | 14:7648334eb41b | 129 | |
hudakz | 14:7648334eb41b | 130 | /** Get the raw IP bytes |
hudakz | 14:7648334eb41b | 131 | * |
hudakz | 14:7648334eb41b | 132 | * @return Raw IP address in big-endian order |
hudakz | 14:7648334eb41b | 133 | */ |
hudakz | 14:7648334eb41b | 134 | const void *get_ip_bytes() const; |
hudakz | 14:7648334eb41b | 135 | |
hudakz | 14:7648334eb41b | 136 | /** Get the IP address version |
hudakz | 14:7648334eb41b | 137 | * |
hudakz | 14:7648334eb41b | 138 | * @return IP address version, NSAPI_IPv4 or NSAPI_IPv6 |
hudakz | 14:7648334eb41b | 139 | */ |
hudakz | 14:7648334eb41b | 140 | nsapi_version_t get_ip_version() const; |
hudakz | 14:7648334eb41b | 141 | |
hudakz | 14:7648334eb41b | 142 | /** Get the raw IP address |
hudakz | 14:7648334eb41b | 143 | * |
hudakz | 14:7648334eb41b | 144 | * @return Raw IP address |
hudakz | 14:7648334eb41b | 145 | */ |
hudakz | 14:7648334eb41b | 146 | nsapi_addr_t get_addr() const; |
hudakz | 14:7648334eb41b | 147 | |
hudakz | 14:7648334eb41b | 148 | /** Get the port |
hudakz | 14:7648334eb41b | 149 | * |
hudakz | 14:7648334eb41b | 150 | * @return The 16-bit port |
hudakz | 14:7648334eb41b | 151 | */ |
hudakz | 14:7648334eb41b | 152 | uint16_t get_port() const; |
hudakz | 14:7648334eb41b | 153 | |
hudakz | 14:7648334eb41b | 154 | /** Test if address is zero |
hudakz | 14:7648334eb41b | 155 | * |
hudakz | 14:7648334eb41b | 156 | * @return True if address is not zero |
hudakz | 14:7648334eb41b | 157 | */ |
hudakz | 14:7648334eb41b | 158 | operator bool() const; |
hudakz | 14:7648334eb41b | 159 | |
hudakz | 14:7648334eb41b | 160 | /** Copy address from another SocketAddress |
hudakz | 14:7648334eb41b | 161 | * |
hudakz | 14:7648334eb41b | 162 | * @param addr SocketAddress to copy |
hudakz | 14:7648334eb41b | 163 | */ |
hudakz | 14:7648334eb41b | 164 | SocketAddress &operator=(const SocketAddress &addr); |
hudakz | 14:7648334eb41b | 165 | |
hudakz | 14:7648334eb41b | 166 | /** Compare two addresses for equality |
hudakz | 14:7648334eb41b | 167 | * |
hudakz | 14:7648334eb41b | 168 | * @return True if both addresses are equal |
hudakz | 14:7648334eb41b | 169 | */ |
hudakz | 14:7648334eb41b | 170 | friend bool operator==(const SocketAddress &a, const SocketAddress &b); |
hudakz | 14:7648334eb41b | 171 | |
hudakz | 14:7648334eb41b | 172 | /** Compare two addresses for equality |
hudakz | 14:7648334eb41b | 173 | * |
hudakz | 14:7648334eb41b | 174 | * @return True if both addresses are not equal |
hudakz | 14:7648334eb41b | 175 | */ |
hudakz | 14:7648334eb41b | 176 | friend bool operator!=(const SocketAddress &a, const SocketAddress &b); |
hudakz | 14:7648334eb41b | 177 | |
hudakz | 14:7648334eb41b | 178 | private: |
hudakz | 14:7648334eb41b | 179 | //void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port); |
hudakz | 14:7648334eb41b | 180 | |
hudakz | 14:7648334eb41b | 181 | /** Initialize memory */ |
hudakz | 14:7648334eb41b | 182 | void mem_init(void); |
hudakz | 14:7648334eb41b | 183 | |
hudakz | 14:7648334eb41b | 184 | mutable char *_ip_address; |
hudakz | 14:7648334eb41b | 185 | nsapi_addr_t _addr; |
hudakz | 14:7648334eb41b | 186 | uint16_t _port; |
hudakz | 14:7648334eb41b | 187 | }; |
hudakz | 14:7648334eb41b | 188 | |
hudakz | 14:7648334eb41b | 189 | |
hudakz | 14:7648334eb41b | 190 | #endif |
hudakz | 14:7648334eb41b | 191 | |
hudakz | 14:7648334eb41b | 192 | /** @}*/ |