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