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