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