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