mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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