| 1 | /**************************************************************************/ |
| 2 | /* ip_address.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "ip_address.h" |
| 32 | /* |
| 33 | IPAddress::operator Variant() const { |
| 34 | return operator String(); |
| 35 | }*/ |
| 36 | |
| 37 | IPAddress::operator String() const { |
| 38 | if (wildcard) { |
| 39 | return "*"; |
| 40 | } |
| 41 | |
| 42 | if (!valid) { |
| 43 | return ""; |
| 44 | } |
| 45 | |
| 46 | if (is_ipv4()) { |
| 47 | // IPv4 address mapped to IPv6. |
| 48 | return itos(field8[12]) + "." + itos(field8[13]) + "." + itos(field8[14]) + "." + itos(field8[15]); |
| 49 | } |
| 50 | String ret; |
| 51 | for (int i = 0; i < 8; i++) { |
| 52 | if (i > 0) { |
| 53 | ret = ret + ":"; |
| 54 | } |
| 55 | uint16_t num = (field8[i * 2] << 8) + field8[i * 2 + 1]; |
| 56 | ret = ret + String::num_int64(num, 16); |
| 57 | } |
| 58 | |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | bool IPAddress::_parse_ipv6(const String &p_string, IPAddress &r_ip) { |
| 63 | int len = p_string.length(); |
| 64 | const char32_t *buf = p_string.ptr(); |
| 65 | |
| 66 | int cur = 0; |
| 67 | int shift = -1; |
| 68 | for (int i = 0; i < len; i++) { |
| 69 | for (int j = i; j < len; j++) { |
| 70 | char32_t c = buf[j]; |
| 71 | if (c == ':') { |
| 72 | if (j + 1 == len) { |
| 73 | return false; // Can't end with a column (unless part of shortening). |
| 74 | } |
| 75 | if (buf[j + 1] == ':') { |
| 76 | if (shift > -1) { |
| 77 | return false; // Only one shortening allowed. |
| 78 | } else if (j == 0) { |
| 79 | shift = cur; |
| 80 | } else { |
| 81 | shift = cur + 1; |
| 82 | } |
| 83 | j++; |
| 84 | } else if (i == j) { |
| 85 | return false; // Stray column. |
| 86 | } |
| 87 | i = j; |
| 88 | break; |
| 89 | } |
| 90 | if (j - i > 3) { |
| 91 | return false; |
| 92 | } |
| 93 | if (c >= '0' && c <= '9') { |
| 94 | r_ip.field16[cur] = r_ip.field16[cur] << 4; |
| 95 | r_ip.field16[cur] |= c - '0'; |
| 96 | } else if (c >= 'a' && c <= 'f') { |
| 97 | r_ip.field16[cur] = r_ip.field16[cur] << 4; |
| 98 | r_ip.field16[cur] |= 10 + (c - 'a'); |
| 99 | } else if (c >= 'A' && c <= 'F') { |
| 100 | r_ip.field16[cur] = r_ip.field16[cur] << 4; |
| 101 | r_ip.field16[cur] |= 10 + (c - 'A'); |
| 102 | } else if (c == '.') { |
| 103 | // IPv4 mapped IPv6 (e.g. "::FFFF:127.0.0.1"). |
| 104 | if (cur < 1 || r_ip.field16[cur - 1] != 0xFFFF) { |
| 105 | return false; // IPv6 part must end with FFFF. |
| 106 | } |
| 107 | if (shift < 0 && cur != 6) { |
| 108 | return false; // Needs 5 zeros, and FFFF "0:0:0:0:0:FFFF:127.0.0.1". |
| 109 | } |
| 110 | // Only empty bytes allowed before FFFF. |
| 111 | r_ip.field16[cur] = 0; |
| 112 | r_ip.field16[cur - 1] = 0; |
| 113 | while (cur > 0) { |
| 114 | cur--; |
| 115 | if (r_ip.field16[cur] != 0) { |
| 116 | return false; |
| 117 | } |
| 118 | } |
| 119 | r_ip.field16[5] = 0xFFFF; |
| 120 | return _parse_ipv4(p_string, i, &r_ip.field8[12]); |
| 121 | } else { |
| 122 | return false; |
| 123 | } |
| 124 | if (j + 1 == len) { |
| 125 | i = j; |
| 126 | } |
| 127 | } |
| 128 | r_ip.field16[cur] = BSWAP16(r_ip.field16[cur]); |
| 129 | cur += 1; |
| 130 | if (cur > 8 || (cur == 8 && i + 1 != len)) { |
| 131 | return false; |
| 132 | } |
| 133 | } |
| 134 | if (shift < 0) { |
| 135 | return cur == 8; // Should have parsed 8 16-bits ints. |
| 136 | } else if (shift > 7) { |
| 137 | return false; // Can't shorten more than this. |
| 138 | } else if (shift == cur) { |
| 139 | return true; // Nothing to do, end is assumed zeroized. |
| 140 | } |
| 141 | // Shift bytes. |
| 142 | int pad = 8 - cur; |
| 143 | int blank_end = shift + pad; |
| 144 | for (int i = 7; i > shift; i--) { |
| 145 | if (i < blank_end) { |
| 146 | r_ip.field16[i] = 0; |
| 147 | } else { |
| 148 | r_ip.field16[i] = r_ip.field16[i - pad]; |
| 149 | } |
| 150 | } |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | bool IPAddress::_parse_ipv4(const String &p_string, int p_start, uint8_t *r_dest) { |
| 155 | int len = p_string.length(); |
| 156 | const char32_t *buf = p_string.ptr(); |
| 157 | |
| 158 | int cur = 0; |
| 159 | uint16_t next = 0; |
| 160 | bool parsed = false; |
| 161 | for (int i = p_start; i < len; i++) { |
| 162 | char32_t c = buf[i]; |
| 163 | if (c == '.') { |
| 164 | if (!parsed) { |
| 165 | return false; |
| 166 | } |
| 167 | parsed = false; |
| 168 | r_dest[cur] = next; |
| 169 | next = 0; |
| 170 | cur++; |
| 171 | if (cur > 3) { |
| 172 | return false; |
| 173 | } |
| 174 | } else if (c >= '0' && c <= '9') { |
| 175 | parsed = true; |
| 176 | next *= 10; |
| 177 | next += c - '0'; |
| 178 | if (next > 255) { |
| 179 | return false; |
| 180 | } |
| 181 | } else { |
| 182 | return false; // Invalid char. |
| 183 | } |
| 184 | } |
| 185 | if (!parsed) { |
| 186 | return false; |
| 187 | } |
| 188 | r_dest[cur] = next; |
| 189 | return parsed && cur == 3; |
| 190 | } |
| 191 | |
| 192 | void IPAddress::clear() { |
| 193 | memset(&field8[0], 0, sizeof(field8)); |
| 194 | valid = false; |
| 195 | wildcard = false; |
| 196 | } |
| 197 | |
| 198 | bool IPAddress::is_ipv4() const { |
| 199 | return (field32[0] == 0 && field32[1] == 0 && field16[4] == 0 && field16[5] == 0xffff); |
| 200 | } |
| 201 | |
| 202 | const uint8_t *IPAddress::get_ipv4() const { |
| 203 | ERR_FAIL_COND_V_MSG(!is_ipv4(), &(field8[12]), "IPv4 requested, but current IP is IPv6."); // Not the correct IPv4 (it's an IPv6), but we don't want to return a null pointer risking an engine crash. |
| 204 | return &(field8[12]); |
| 205 | } |
| 206 | |
| 207 | void IPAddress::set_ipv4(const uint8_t *p_ip) { |
| 208 | clear(); |
| 209 | valid = true; |
| 210 | field16[5] = 0xffff; |
| 211 | field32[3] = *((const uint32_t *)p_ip); |
| 212 | } |
| 213 | |
| 214 | const uint8_t *IPAddress::get_ipv6() const { |
| 215 | return field8; |
| 216 | } |
| 217 | |
| 218 | void IPAddress::set_ipv6(const uint8_t *p_buf) { |
| 219 | clear(); |
| 220 | valid = true; |
| 221 | for (int i = 0; i < 16; i++) { |
| 222 | field8[i] = p_buf[i]; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | bool IPAddress::is_valid_ip_address(const String &p_string) { |
| 227 | IPAddress addr; |
| 228 | if (p_string.length() < IPV6_MAX_STRING_LENGTH && p_string.contains_char(':')) { |
| 229 | return _parse_ipv6(p_string, addr); |
| 230 | } else if (p_string.length() < IPV4_MAX_STRING_LENGTH) { // Try IPv4. |
| 231 | return _parse_ipv4(p_string, 0, &addr.field8[12]); |
| 232 | } |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | IPAddress::IPAddress(const String &p_string) { |
| 237 | clear(); |
| 238 | |
| 239 | if (p_string == "*") { |
| 240 | // Wildcard (not a valid IP). |
| 241 | wildcard = true; |
| 242 | |
| 243 | } else if (p_string.length() < IPV6_MAX_STRING_LENGTH && p_string.contains_char(':')) { |
| 244 | // IPv6. |
| 245 | valid = _parse_ipv6(p_string, *this); |
| 246 | ERR_FAIL_COND_MSG(!valid, "Invalid IPv6 address: " + p_string); |
| 247 | |
| 248 | } else if (p_string.length() < IPV4_MAX_STRING_LENGTH) { |
| 249 | // IPv4 (mapped to IPv6 internally). |
| 250 | field16[5] = 0xffff; |
| 251 | valid = _parse_ipv4(p_string, 0, &field8[12]); |
| 252 | ERR_FAIL_COND_MSG(!valid, "Invalid IPv4 address: " + p_string); |
| 253 | |
| 254 | } else { |
| 255 | ERR_PRINT("Invalid IP address."); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | _FORCE_INLINE_ static void _32_to_buf(uint8_t *p_dst, uint32_t p_n) { |
| 260 | p_dst[0] = (p_n >> 24) & 0xff; |
| 261 | p_dst[1] = (p_n >> 16) & 0xff; |
| 262 | p_dst[2] = (p_n >> 8) & 0xff; |
| 263 | p_dst[3] = (p_n >> 0) & 0xff; |
| 264 | } |
| 265 | |
| 266 | IPAddress::IPAddress(uint32_t p_a, uint32_t p_b, uint32_t p_c, uint32_t p_d, bool is_v6) { |
| 267 | clear(); |
| 268 | valid = true; |
| 269 | if (!is_v6) { |
| 270 | // Mapped to IPv6. |
| 271 | field16[5] = 0xffff; |
| 272 | field8[12] = p_a; |
| 273 | field8[13] = p_b; |
| 274 | field8[14] = p_c; |
| 275 | field8[15] = p_d; |
| 276 | } else { |
| 277 | _32_to_buf(&field8[0], p_a); |
| 278 | _32_to_buf(&field8[4], p_b); |
| 279 | _32_to_buf(&field8[8], p_c); |
| 280 | _32_to_buf(&field8[12], p_d); |
| 281 | } |
| 282 | } |