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