Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 00:01:50 2018 +0000
Revision:
0:6ad07c9019fd
Codigo de tales para todos los pasculaes

Who changed what in which revision?

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