BA
/
BaBoRo_test2
mbed-os/features/netsocket/SocketAddress.cpp@0:02dd72d1d465, 2018-04-24 (annotated)
- Committer:
- borlanic
- Date:
- Tue Apr 24 11:45:18 2018 +0000
- Revision:
- 0:02dd72d1d465
BaBoRo_test2 - backup 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:02dd72d1d465 | 1 | /* Socket |
borlanic | 0:02dd72d1d465 | 2 | * Copyright (c) 2015 ARM Limited |
borlanic | 0:02dd72d1d465 | 3 | * |
borlanic | 0:02dd72d1d465 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
borlanic | 0:02dd72d1d465 | 5 | * you may not use this file except in compliance with the License. |
borlanic | 0:02dd72d1d465 | 6 | * You may obtain a copy of the License at |
borlanic | 0:02dd72d1d465 | 7 | * |
borlanic | 0:02dd72d1d465 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
borlanic | 0:02dd72d1d465 | 9 | * |
borlanic | 0:02dd72d1d465 | 10 | * Unless required by applicable law or agreed to in writing, software |
borlanic | 0:02dd72d1d465 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
borlanic | 0:02dd72d1d465 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
borlanic | 0:02dd72d1d465 | 13 | * See the License for the specific language governing permissions and |
borlanic | 0:02dd72d1d465 | 14 | * limitations under the License. |
borlanic | 0:02dd72d1d465 | 15 | */ |
borlanic | 0:02dd72d1d465 | 16 | |
borlanic | 0:02dd72d1d465 | 17 | #include "SocketAddress.h" |
borlanic | 0:02dd72d1d465 | 18 | #include "NetworkInterface.h" |
borlanic | 0:02dd72d1d465 | 19 | #include "NetworkStack.h" |
borlanic | 0:02dd72d1d465 | 20 | #include <string.h> |
borlanic | 0:02dd72d1d465 | 21 | #include "mbed.h" |
borlanic | 0:02dd72d1d465 | 22 | |
borlanic | 0:02dd72d1d465 | 23 | |
borlanic | 0:02dd72d1d465 | 24 | static bool ipv4_is_valid(const char *addr) |
borlanic | 0:02dd72d1d465 | 25 | { |
borlanic | 0:02dd72d1d465 | 26 | int i = 0; |
borlanic | 0:02dd72d1d465 | 27 | |
borlanic | 0:02dd72d1d465 | 28 | // Check each digit for [0-9.] |
borlanic | 0:02dd72d1d465 | 29 | for (; addr[i]; i++) { |
borlanic | 0:02dd72d1d465 | 30 | if (!(addr[i] >= '0' && addr[i] <= '9') && addr[i] != '.') { |
borlanic | 0:02dd72d1d465 | 31 | return false; |
borlanic | 0:02dd72d1d465 | 32 | } |
borlanic | 0:02dd72d1d465 | 33 | } |
borlanic | 0:02dd72d1d465 | 34 | |
borlanic | 0:02dd72d1d465 | 35 | // Ending with '.' garuntees host |
borlanic | 0:02dd72d1d465 | 36 | if (i > 0 && addr[i-1] == '.') { |
borlanic | 0:02dd72d1d465 | 37 | return false; |
borlanic | 0:02dd72d1d465 | 38 | } |
borlanic | 0:02dd72d1d465 | 39 | |
borlanic | 0:02dd72d1d465 | 40 | return true; |
borlanic | 0:02dd72d1d465 | 41 | } |
borlanic | 0:02dd72d1d465 | 42 | |
borlanic | 0:02dd72d1d465 | 43 | static bool ipv6_is_valid(const char *addr) |
borlanic | 0:02dd72d1d465 | 44 | { |
borlanic | 0:02dd72d1d465 | 45 | // Check each digit for [0-9a-fA-F:] |
borlanic | 0:02dd72d1d465 | 46 | // Must also have at least 2 colons |
borlanic | 0:02dd72d1d465 | 47 | int colons = 0; |
borlanic | 0:02dd72d1d465 | 48 | for (int i = 0; addr[i]; i++) { |
borlanic | 0:02dd72d1d465 | 49 | if (!(addr[i] >= '0' && addr[i] <= '9') && |
borlanic | 0:02dd72d1d465 | 50 | !(addr[i] >= 'a' && addr[i] <= 'f') && |
borlanic | 0:02dd72d1d465 | 51 | !(addr[i] >= 'A' && addr[i] <= 'F') && |
borlanic | 0:02dd72d1d465 | 52 | addr[i] != ':') { |
borlanic | 0:02dd72d1d465 | 53 | return false; |
borlanic | 0:02dd72d1d465 | 54 | } |
borlanic | 0:02dd72d1d465 | 55 | if (addr[i] == ':') { |
borlanic | 0:02dd72d1d465 | 56 | colons++; |
borlanic | 0:02dd72d1d465 | 57 | } |
borlanic | 0:02dd72d1d465 | 58 | } |
borlanic | 0:02dd72d1d465 | 59 | |
borlanic | 0:02dd72d1d465 | 60 | return colons >= 2; |
borlanic | 0:02dd72d1d465 | 61 | } |
borlanic | 0:02dd72d1d465 | 62 | |
borlanic | 0:02dd72d1d465 | 63 | static void ipv4_from_address(uint8_t *bytes, const char *addr) |
borlanic | 0:02dd72d1d465 | 64 | { |
borlanic | 0:02dd72d1d465 | 65 | int count = 0; |
borlanic | 0:02dd72d1d465 | 66 | int i = 0; |
borlanic | 0:02dd72d1d465 | 67 | |
borlanic | 0:02dd72d1d465 | 68 | for (; count < NSAPI_IPv4_BYTES; count++) { |
borlanic | 0:02dd72d1d465 | 69 | unsigned d; |
borlanic | 0:02dd72d1d465 | 70 | // Not using %hh, since it might be missing in newlib-based toolchains. |
borlanic | 0:02dd72d1d465 | 71 | // See also: https://git.io/vxiw5 |
borlanic | 0:02dd72d1d465 | 72 | int scanned = sscanf(&addr[i], "%u", &d); |
borlanic | 0:02dd72d1d465 | 73 | if (scanned < 1) { |
borlanic | 0:02dd72d1d465 | 74 | return; |
borlanic | 0:02dd72d1d465 | 75 | } |
borlanic | 0:02dd72d1d465 | 76 | |
borlanic | 0:02dd72d1d465 | 77 | bytes[count] = static_cast<uint8_t>(d); |
borlanic | 0:02dd72d1d465 | 78 | |
borlanic | 0:02dd72d1d465 | 79 | for (; addr[i] != '.'; i++) { |
borlanic | 0:02dd72d1d465 | 80 | if (!addr[i]) { |
borlanic | 0:02dd72d1d465 | 81 | return; |
borlanic | 0:02dd72d1d465 | 82 | } |
borlanic | 0:02dd72d1d465 | 83 | } |
borlanic | 0:02dd72d1d465 | 84 | |
borlanic | 0:02dd72d1d465 | 85 | i++; |
borlanic | 0:02dd72d1d465 | 86 | } |
borlanic | 0:02dd72d1d465 | 87 | } |
borlanic | 0:02dd72d1d465 | 88 | |
borlanic | 0:02dd72d1d465 | 89 | static int ipv6_scan_chunk(uint16_t *shorts, const char *chunk) { |
borlanic | 0:02dd72d1d465 | 90 | int count = 0; |
borlanic | 0:02dd72d1d465 | 91 | int i = 0; |
borlanic | 0:02dd72d1d465 | 92 | |
borlanic | 0:02dd72d1d465 | 93 | for (; count < NSAPI_IPv6_BYTES/2; count++) { |
borlanic | 0:02dd72d1d465 | 94 | unsigned short s; |
borlanic | 0:02dd72d1d465 | 95 | int scanned = sscanf(&chunk[i], "%hx", &s); |
borlanic | 0:02dd72d1d465 | 96 | if (scanned < 1) { |
borlanic | 0:02dd72d1d465 | 97 | return count; |
borlanic | 0:02dd72d1d465 | 98 | } |
borlanic | 0:02dd72d1d465 | 99 | |
borlanic | 0:02dd72d1d465 | 100 | shorts[count] = s; |
borlanic | 0:02dd72d1d465 | 101 | |
borlanic | 0:02dd72d1d465 | 102 | for (; chunk[i] != ':'; i++) { |
borlanic | 0:02dd72d1d465 | 103 | if (!chunk[i]) { |
borlanic | 0:02dd72d1d465 | 104 | return count+1; |
borlanic | 0:02dd72d1d465 | 105 | } |
borlanic | 0:02dd72d1d465 | 106 | } |
borlanic | 0:02dd72d1d465 | 107 | |
borlanic | 0:02dd72d1d465 | 108 | i++; |
borlanic | 0:02dd72d1d465 | 109 | } |
borlanic | 0:02dd72d1d465 | 110 | |
borlanic | 0:02dd72d1d465 | 111 | return count; |
borlanic | 0:02dd72d1d465 | 112 | } |
borlanic | 0:02dd72d1d465 | 113 | |
borlanic | 0:02dd72d1d465 | 114 | static void ipv6_from_address(uint8_t *bytes, const char *addr) |
borlanic | 0:02dd72d1d465 | 115 | { |
borlanic | 0:02dd72d1d465 | 116 | // Start with zeroed address |
borlanic | 0:02dd72d1d465 | 117 | uint16_t shorts[NSAPI_IPv6_BYTES/2]; |
borlanic | 0:02dd72d1d465 | 118 | int suffix = 0; |
borlanic | 0:02dd72d1d465 | 119 | |
borlanic | 0:02dd72d1d465 | 120 | // Find double colons and scan suffix |
borlanic | 0:02dd72d1d465 | 121 | for (int i = 0; addr[i]; i++) { |
borlanic | 0:02dd72d1d465 | 122 | if (addr[i] == ':' && addr[i+1] == ':') { |
borlanic | 0:02dd72d1d465 | 123 | suffix = ipv6_scan_chunk(shorts, &addr[i+2]); |
borlanic | 0:02dd72d1d465 | 124 | break; |
borlanic | 0:02dd72d1d465 | 125 | } |
borlanic | 0:02dd72d1d465 | 126 | } |
borlanic | 0:02dd72d1d465 | 127 | |
borlanic | 0:02dd72d1d465 | 128 | // Move suffix to end |
borlanic | 0:02dd72d1d465 | 129 | memmove(&shorts[NSAPI_IPv6_BYTES/2-suffix], &shorts[0], |
borlanic | 0:02dd72d1d465 | 130 | suffix*sizeof(uint16_t)); |
borlanic | 0:02dd72d1d465 | 131 | memset(&shorts[0], 0, |
borlanic | 0:02dd72d1d465 | 132 | (NSAPI_IPv6_BYTES/2-suffix)*sizeof(uint16_t)); |
borlanic | 0:02dd72d1d465 | 133 | |
borlanic | 0:02dd72d1d465 | 134 | // Scan prefix |
borlanic | 0:02dd72d1d465 | 135 | ipv6_scan_chunk(shorts, &addr[0]); |
borlanic | 0:02dd72d1d465 | 136 | |
borlanic | 0:02dd72d1d465 | 137 | // Flip bytes |
borlanic | 0:02dd72d1d465 | 138 | for (int i = 0; i < NSAPI_IPv6_BYTES/2; i++) { |
borlanic | 0:02dd72d1d465 | 139 | bytes[2*i+0] = (uint8_t)(shorts[i] >> 8); |
borlanic | 0:02dd72d1d465 | 140 | bytes[2*i+1] = (uint8_t)(shorts[i] >> 0); |
borlanic | 0:02dd72d1d465 | 141 | } |
borlanic | 0:02dd72d1d465 | 142 | } |
borlanic | 0:02dd72d1d465 | 143 | |
borlanic | 0:02dd72d1d465 | 144 | static void ipv4_to_address(char *addr, const uint8_t *bytes) |
borlanic | 0:02dd72d1d465 | 145 | { |
borlanic | 0:02dd72d1d465 | 146 | sprintf(addr, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]); |
borlanic | 0:02dd72d1d465 | 147 | } |
borlanic | 0:02dd72d1d465 | 148 | |
borlanic | 0:02dd72d1d465 | 149 | static void ipv6_to_address(char *addr, const uint8_t *bytes) |
borlanic | 0:02dd72d1d465 | 150 | { |
borlanic | 0:02dd72d1d465 | 151 | for (int i = 0; i < NSAPI_IPv6_BYTES/2; i++) { |
borlanic | 0:02dd72d1d465 | 152 | sprintf(&addr[5*i], "%02x%02x", bytes[2*i], bytes[2*i+1]); |
borlanic | 0:02dd72d1d465 | 153 | addr[5*i+4] = ':'; |
borlanic | 0:02dd72d1d465 | 154 | } |
borlanic | 0:02dd72d1d465 | 155 | addr[NSAPI_IPv6_SIZE-1] = '\0'; |
borlanic | 0:02dd72d1d465 | 156 | } |
borlanic | 0:02dd72d1d465 | 157 | |
borlanic | 0:02dd72d1d465 | 158 | |
borlanic | 0:02dd72d1d465 | 159 | SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port) |
borlanic | 0:02dd72d1d465 | 160 | { |
borlanic | 0:02dd72d1d465 | 161 | _ip_address[0] = '\0'; |
borlanic | 0:02dd72d1d465 | 162 | set_addr(addr); |
borlanic | 0:02dd72d1d465 | 163 | set_port(port); |
borlanic | 0:02dd72d1d465 | 164 | } |
borlanic | 0:02dd72d1d465 | 165 | |
borlanic | 0:02dd72d1d465 | 166 | SocketAddress::SocketAddress(const char *addr, uint16_t port) |
borlanic | 0:02dd72d1d465 | 167 | { |
borlanic | 0:02dd72d1d465 | 168 | _ip_address[0] = '\0'; |
borlanic | 0:02dd72d1d465 | 169 | set_ip_address(addr); |
borlanic | 0:02dd72d1d465 | 170 | set_port(port); |
borlanic | 0:02dd72d1d465 | 171 | } |
borlanic | 0:02dd72d1d465 | 172 | |
borlanic | 0:02dd72d1d465 | 173 | SocketAddress::SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port) |
borlanic | 0:02dd72d1d465 | 174 | { |
borlanic | 0:02dd72d1d465 | 175 | _ip_address[0] = '\0'; |
borlanic | 0:02dd72d1d465 | 176 | set_ip_bytes(bytes, version); |
borlanic | 0:02dd72d1d465 | 177 | set_port(port); |
borlanic | 0:02dd72d1d465 | 178 | } |
borlanic | 0:02dd72d1d465 | 179 | |
borlanic | 0:02dd72d1d465 | 180 | SocketAddress::SocketAddress(const SocketAddress &addr) |
borlanic | 0:02dd72d1d465 | 181 | { |
borlanic | 0:02dd72d1d465 | 182 | _ip_address[0] = '\0'; |
borlanic | 0:02dd72d1d465 | 183 | set_addr(addr.get_addr()); |
borlanic | 0:02dd72d1d465 | 184 | set_port(addr.get_port()); |
borlanic | 0:02dd72d1d465 | 185 | } |
borlanic | 0:02dd72d1d465 | 186 | |
borlanic | 0:02dd72d1d465 | 187 | bool SocketAddress::set_ip_address(const char *addr) |
borlanic | 0:02dd72d1d465 | 188 | { |
borlanic | 0:02dd72d1d465 | 189 | _ip_address[0] = '\0'; |
borlanic | 0:02dd72d1d465 | 190 | |
borlanic | 0:02dd72d1d465 | 191 | if (addr && ipv4_is_valid(addr)) { |
borlanic | 0:02dd72d1d465 | 192 | _addr.version = NSAPI_IPv4; |
borlanic | 0:02dd72d1d465 | 193 | ipv4_from_address(_addr.bytes, addr); |
borlanic | 0:02dd72d1d465 | 194 | return true; |
borlanic | 0:02dd72d1d465 | 195 | } else if (addr && ipv6_is_valid(addr)) { |
borlanic | 0:02dd72d1d465 | 196 | _addr.version = NSAPI_IPv6; |
borlanic | 0:02dd72d1d465 | 197 | ipv6_from_address(_addr.bytes, addr); |
borlanic | 0:02dd72d1d465 | 198 | return true; |
borlanic | 0:02dd72d1d465 | 199 | } else { |
borlanic | 0:02dd72d1d465 | 200 | _addr = nsapi_addr_t(); |
borlanic | 0:02dd72d1d465 | 201 | return false; |
borlanic | 0:02dd72d1d465 | 202 | } |
borlanic | 0:02dd72d1d465 | 203 | } |
borlanic | 0:02dd72d1d465 | 204 | |
borlanic | 0:02dd72d1d465 | 205 | void SocketAddress::set_ip_bytes(const void *bytes, nsapi_version_t version) |
borlanic | 0:02dd72d1d465 | 206 | { |
borlanic | 0:02dd72d1d465 | 207 | nsapi_addr_t addr; |
borlanic | 0:02dd72d1d465 | 208 | |
borlanic | 0:02dd72d1d465 | 209 | addr = nsapi_addr_t(); |
borlanic | 0:02dd72d1d465 | 210 | addr.version = version; |
borlanic | 0:02dd72d1d465 | 211 | if (version == NSAPI_IPv6) { |
borlanic | 0:02dd72d1d465 | 212 | memcpy(addr.bytes, bytes, NSAPI_IPv6_BYTES); |
borlanic | 0:02dd72d1d465 | 213 | } else if (version == NSAPI_IPv4) { |
borlanic | 0:02dd72d1d465 | 214 | memcpy(addr.bytes, bytes, NSAPI_IPv4_BYTES); |
borlanic | 0:02dd72d1d465 | 215 | } |
borlanic | 0:02dd72d1d465 | 216 | set_addr(addr); |
borlanic | 0:02dd72d1d465 | 217 | } |
borlanic | 0:02dd72d1d465 | 218 | |
borlanic | 0:02dd72d1d465 | 219 | void SocketAddress::set_addr(nsapi_addr_t addr) |
borlanic | 0:02dd72d1d465 | 220 | { |
borlanic | 0:02dd72d1d465 | 221 | _ip_address[0] = '\0'; |
borlanic | 0:02dd72d1d465 | 222 | _addr = addr; |
borlanic | 0:02dd72d1d465 | 223 | } |
borlanic | 0:02dd72d1d465 | 224 | |
borlanic | 0:02dd72d1d465 | 225 | void SocketAddress::set_port(uint16_t port) |
borlanic | 0:02dd72d1d465 | 226 | { |
borlanic | 0:02dd72d1d465 | 227 | _port = port; |
borlanic | 0:02dd72d1d465 | 228 | } |
borlanic | 0:02dd72d1d465 | 229 | |
borlanic | 0:02dd72d1d465 | 230 | const char *SocketAddress::get_ip_address() const |
borlanic | 0:02dd72d1d465 | 231 | { |
borlanic | 0:02dd72d1d465 | 232 | if (_addr.version == NSAPI_UNSPEC) { |
borlanic | 0:02dd72d1d465 | 233 | return NULL; |
borlanic | 0:02dd72d1d465 | 234 | } |
borlanic | 0:02dd72d1d465 | 235 | |
borlanic | 0:02dd72d1d465 | 236 | if (!_ip_address[0]) { |
borlanic | 0:02dd72d1d465 | 237 | if (_addr.version == NSAPI_IPv4) { |
borlanic | 0:02dd72d1d465 | 238 | ipv4_to_address(_ip_address, _addr.bytes); |
borlanic | 0:02dd72d1d465 | 239 | } else if (_addr.version == NSAPI_IPv6) { |
borlanic | 0:02dd72d1d465 | 240 | ipv6_to_address(_ip_address, _addr.bytes); |
borlanic | 0:02dd72d1d465 | 241 | } |
borlanic | 0:02dd72d1d465 | 242 | } |
borlanic | 0:02dd72d1d465 | 243 | |
borlanic | 0:02dd72d1d465 | 244 | return _ip_address; |
borlanic | 0:02dd72d1d465 | 245 | } |
borlanic | 0:02dd72d1d465 | 246 | |
borlanic | 0:02dd72d1d465 | 247 | const void *SocketAddress::get_ip_bytes() const |
borlanic | 0:02dd72d1d465 | 248 | { |
borlanic | 0:02dd72d1d465 | 249 | return _addr.bytes; |
borlanic | 0:02dd72d1d465 | 250 | } |
borlanic | 0:02dd72d1d465 | 251 | |
borlanic | 0:02dd72d1d465 | 252 | nsapi_version_t SocketAddress::get_ip_version() const |
borlanic | 0:02dd72d1d465 | 253 | { |
borlanic | 0:02dd72d1d465 | 254 | return _addr.version; |
borlanic | 0:02dd72d1d465 | 255 | } |
borlanic | 0:02dd72d1d465 | 256 | |
borlanic | 0:02dd72d1d465 | 257 | nsapi_addr_t SocketAddress::get_addr() const |
borlanic | 0:02dd72d1d465 | 258 | { |
borlanic | 0:02dd72d1d465 | 259 | return _addr; |
borlanic | 0:02dd72d1d465 | 260 | } |
borlanic | 0:02dd72d1d465 | 261 | |
borlanic | 0:02dd72d1d465 | 262 | uint16_t SocketAddress::get_port() const |
borlanic | 0:02dd72d1d465 | 263 | { |
borlanic | 0:02dd72d1d465 | 264 | return _port; |
borlanic | 0:02dd72d1d465 | 265 | } |
borlanic | 0:02dd72d1d465 | 266 | |
borlanic | 0:02dd72d1d465 | 267 | SocketAddress::operator bool() const |
borlanic | 0:02dd72d1d465 | 268 | { |
borlanic | 0:02dd72d1d465 | 269 | if (_addr.version == NSAPI_IPv4) { |
borlanic | 0:02dd72d1d465 | 270 | for (int i = 0; i < NSAPI_IPv4_BYTES; i++) { |
borlanic | 0:02dd72d1d465 | 271 | if (_addr.bytes[i]) { |
borlanic | 0:02dd72d1d465 | 272 | return true; |
borlanic | 0:02dd72d1d465 | 273 | } |
borlanic | 0:02dd72d1d465 | 274 | } |
borlanic | 0:02dd72d1d465 | 275 | |
borlanic | 0:02dd72d1d465 | 276 | return false; |
borlanic | 0:02dd72d1d465 | 277 | } else if (_addr.version == NSAPI_IPv6) { |
borlanic | 0:02dd72d1d465 | 278 | for (int i = 0; i < NSAPI_IPv6_BYTES; i++) { |
borlanic | 0:02dd72d1d465 | 279 | if (_addr.bytes[i]) { |
borlanic | 0:02dd72d1d465 | 280 | return true; |
borlanic | 0:02dd72d1d465 | 281 | } |
borlanic | 0:02dd72d1d465 | 282 | } |
borlanic | 0:02dd72d1d465 | 283 | |
borlanic | 0:02dd72d1d465 | 284 | return false; |
borlanic | 0:02dd72d1d465 | 285 | } else { |
borlanic | 0:02dd72d1d465 | 286 | return false; |
borlanic | 0:02dd72d1d465 | 287 | } |
borlanic | 0:02dd72d1d465 | 288 | } |
borlanic | 0:02dd72d1d465 | 289 | |
borlanic | 0:02dd72d1d465 | 290 | bool operator==(const SocketAddress &a, const SocketAddress &b) |
borlanic | 0:02dd72d1d465 | 291 | { |
borlanic | 0:02dd72d1d465 | 292 | if (!a && !b) { |
borlanic | 0:02dd72d1d465 | 293 | return true; |
borlanic | 0:02dd72d1d465 | 294 | } else if (a._addr.version != b._addr.version) { |
borlanic | 0:02dd72d1d465 | 295 | return false; |
borlanic | 0:02dd72d1d465 | 296 | } else if (a._addr.version == NSAPI_IPv4) { |
borlanic | 0:02dd72d1d465 | 297 | return memcmp(a._addr.bytes, b._addr.bytes, NSAPI_IPv4_BYTES) == 0; |
borlanic | 0:02dd72d1d465 | 298 | } else if (a._addr.version == NSAPI_IPv6) { |
borlanic | 0:02dd72d1d465 | 299 | return memcmp(a._addr.bytes, b._addr.bytes, NSAPI_IPv6_BYTES) == 0; |
borlanic | 0:02dd72d1d465 | 300 | } |
borlanic | 0:02dd72d1d465 | 301 | |
borlanic | 0:02dd72d1d465 | 302 | MBED_UNREACHABLE; |
borlanic | 0:02dd72d1d465 | 303 | } |
borlanic | 0:02dd72d1d465 | 304 | |
borlanic | 0:02dd72d1d465 | 305 | bool operator!=(const SocketAddress &a, const SocketAddress &b) |
borlanic | 0:02dd72d1d465 | 306 | { |
borlanic | 0:02dd72d1d465 | 307 | return !(a == b); |
borlanic | 0:02dd72d1d465 | 308 | } |
borlanic | 0:02dd72d1d465 | 309 | |
borlanic | 0:02dd72d1d465 | 310 | void SocketAddress::_SocketAddress(NetworkStack *iface, const char *host, uint16_t port) |
borlanic | 0:02dd72d1d465 | 311 | { |
borlanic | 0:02dd72d1d465 | 312 | _ip_address[0] = '\0'; |
borlanic | 0:02dd72d1d465 | 313 | |
borlanic | 0:02dd72d1d465 | 314 | // gethostbyname must check for literals, so can call it directly |
borlanic | 0:02dd72d1d465 | 315 | int err = iface->gethostbyname(host, this); |
borlanic | 0:02dd72d1d465 | 316 | _port = port; |
borlanic | 0:02dd72d1d465 | 317 | if (err) { |
borlanic | 0:02dd72d1d465 | 318 | _addr = nsapi_addr_t(); |
borlanic | 0:02dd72d1d465 | 319 | _port = 0; |
borlanic | 0:02dd72d1d465 | 320 | } |
borlanic | 0:02dd72d1d465 | 321 | } |