Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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