ip_address.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #include "ip_address.h"
  31. /*
  32. IPAddress::operator Variant() const {
  33. return operator String();
  34. }*/
  35. IPAddress::operator String() const {
  36. if (wildcard) {
  37. return "*";
  38. }
  39. if (!valid) {
  40. return "";
  41. }
  42. if (is_ipv4()) {
  43. // IPv4 address mapped to IPv6.
  44. return itos(field8[12]) + "." + itos(field8[13]) + "." + itos(field8[14]) + "." + itos(field8[15]);
  45. }
  46. String ret;
  47. for (int i = 0; i < 8; i++) {
  48. if (i > 0) {
  49. ret = ret + ":";
  50. }
  51. uint16_t num = (field8[i * 2] << 8) + field8[i * 2 + 1];
  52. ret = ret + String::num_int64(num, 16);
  53. }
  54. return ret;
  55. }
  56. bool IPAddress::_parse_ipv6(const String &p_string, IPAddress &r_ip) {
  57. int len = p_string.length();
  58. const char32_t *buf = p_string.ptr();
  59. int cur = 0;
  60. int shift = -1;
  61. for (int i = 0; i < len; i++) {
  62. for (int j = i; j < len; j++) {
  63. char32_t c = buf[j];
  64. if (c == ':') {
  65. if (j + 1 == len) {
  66. return false; // Can't end with a column (unless part of shortening).
  67. }
  68. if (buf[j + 1] == ':') {
  69. if (shift > -1) {
  70. return false; // Only one shortening allowed.
  71. } else if (j == 0) {
  72. shift = cur;
  73. } else {
  74. shift = cur + 1;
  75. }
  76. j++;
  77. } else if (i == j) {
  78. return false; // Stray column.
  79. }
  80. i = j;
  81. break;
  82. }
  83. if (j - i > 3) {
  84. return false;
  85. }
  86. if (c >= '0' && c <= '9') {
  87. r_ip.field16[cur] = r_ip.field16[cur] << 4;
  88. r_ip.field16[cur] |= c - '0';
  89. } else if (c >= 'a' && c <= 'f') {
  90. r_ip.field16[cur] = r_ip.field16[cur] << 4;
  91. r_ip.field16[cur] |= 10 + (c - 'a');
  92. } else if (c >= 'A' && c <= 'F') {
  93. r_ip.field16[cur] = r_ip.field16[cur] << 4;
  94. r_ip.field16[cur] |= 10 + (c - 'A');
  95. } else if (c == '.') {
  96. // IPv4 mapped IPv6 (e.g. "::FFFF:127.0.0.1").
  97. if (cur < 1 || r_ip.field16[cur - 1] != 0xFFFF) {
  98. return false; // IPv6 part must end with FFFF.
  99. }
  100. if (shift < 0 && cur != 6) {
  101. return false; // Needs 5 zeros, and FFFF "0:0:0:0:0:FFFF:127.0.0.1".
  102. }
  103. // Only empty bytes allowed before FFFF.
  104. r_ip.field16[cur] = 0;
  105. r_ip.field16[cur - 1] = 0;
  106. while (cur > 0) {
  107. cur--;
  108. if (r_ip.field16[cur] != 0) {
  109. return false;
  110. }
  111. }
  112. r_ip.field16[5] = 0xFFFF;
  113. return _parse_ipv4(p_string, i, &r_ip.field8[12]);
  114. } else {
  115. return false;
  116. }
  117. if (j + 1 == len) {
  118. i = j;
  119. }
  120. }
  121. r_ip.field16[cur] = BSWAP16(r_ip.field16[cur]);
  122. cur += 1;
  123. if (cur > 8 || (cur == 8 && i + 1 != len)) {
  124. return false;
  125. }
  126. }
  127. if (shift < 0) {
  128. return cur == 8; // Should have parsed 8 16-bits ints.
  129. } else if (shift > 7) {
  130. return false; // Can't shorten more than this.
  131. } else if (shift == cur) {
  132. return true; // Nothing to do, end is assumed zeroized.
  133. }
  134. // Shift bytes.
  135. int pad = 8 - cur;
  136. int blank_end = shift + pad;
  137. for (int i = 7; i > shift; i--) {
  138. if (i < blank_end) {
  139. r_ip.field16[i] = 0;
  140. } else {
  141. r_ip.field16[i] = r_ip.field16[i - pad];
  142. }
  143. }
  144. return true;
  145. }
  146. bool IPAddress::_parse_ipv4(const String &p_string, int p_start, uint8_t *r_dest) {
  147. int len = p_string.length();
  148. const char32_t *buf = p_string.ptr();
  149. int cur = 0;
  150. uint16_t next = 0;
  151. bool parsed = false;
  152. for (int i = p_start; i < len; i++) {
  153. char32_t c = buf[i];
  154. if (c == '.') {
  155. if (!parsed) {
  156. return false;
  157. }
  158. parsed = false;
  159. r_dest[cur] = next;
  160. next = 0;
  161. cur++;
  162. if (cur > 3) {
  163. return false;
  164. }
  165. } else if (c >= '0' && c <= '9') {
  166. parsed = true;
  167. next *= 10;
  168. next += c - '0';
  169. if (next > 255) {
  170. return false;
  171. }
  172. } else {
  173. return false; // Invalid char.
  174. }
  175. }
  176. if (!parsed) {
  177. return false;
  178. }
  179. r_dest[cur] = next;
  180. return parsed && cur == 3;
  181. }
  182. void IPAddress::clear() {
  183. memset(&field8[0], 0, sizeof(field8));
  184. valid = false;
  185. wildcard = false;
  186. }
  187. bool IPAddress::is_ipv4() const {
  188. return (field32[0] == 0 && field32[1] == 0 && field16[4] == 0 && field16[5] == 0xffff);
  189. }
  190. const uint8_t *IPAddress::get_ipv4() const {
  191. 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.
  192. return &(field8[12]);
  193. }
  194. void IPAddress::set_ipv4(const uint8_t *p_ip) {
  195. clear();
  196. valid = true;
  197. field16[5] = 0xffff;
  198. field32[3] = *((const uint32_t *)p_ip);
  199. }
  200. const uint8_t *IPAddress::get_ipv6() const {
  201. return field8;
  202. }
  203. void IPAddress::set_ipv6(const uint8_t *p_buf) {
  204. clear();
  205. valid = true;
  206. for (int i = 0; i < 16; i++) {
  207. field8[i] = p_buf[i];
  208. }
  209. }
  210. bool IPAddress::is_valid_ip_address(const String &p_string) {
  211. IPAddress addr;
  212. if (p_string.length() < IPV6_MAX_STRING_LENGTH && p_string.contains_char(':')) {
  213. return _parse_ipv6(p_string, addr);
  214. } else if (p_string.length() < IPV4_MAX_STRING_LENGTH) { // Try IPv4.
  215. return _parse_ipv4(p_string, 0, &addr.field8[12]);
  216. }
  217. return false;
  218. }
  219. IPAddress::IPAddress(const String &p_string) {
  220. clear();
  221. if (p_string == "*") {
  222. // Wildcard (not a valid IP).
  223. wildcard = true;
  224. } else if (p_string.length() < IPV6_MAX_STRING_LENGTH && p_string.contains_char(':')) {
  225. // IPv6.
  226. valid = _parse_ipv6(p_string, *this);
  227. ERR_FAIL_COND_MSG(!valid, "Invalid IPv6 address: " + p_string);
  228. } else if (p_string.length() < IPV4_MAX_STRING_LENGTH) {
  229. // IPv4 (mapped to IPv6 internally).
  230. field16[5] = 0xffff;
  231. valid = _parse_ipv4(p_string, 0, &field8[12]);
  232. ERR_FAIL_COND_MSG(!valid, "Invalid IPv4 address: " + p_string);
  233. } else {
  234. ERR_PRINT("Invalid IP address.");
  235. }
  236. }
  237. _FORCE_INLINE_ static void _32_to_buf(uint8_t *p_dst, uint32_t p_n) {
  238. p_dst[0] = (p_n >> 24) & 0xff;
  239. p_dst[1] = (p_n >> 16) & 0xff;
  240. p_dst[2] = (p_n >> 8) & 0xff;
  241. p_dst[3] = (p_n >> 0) & 0xff;
  242. }
  243. IPAddress::IPAddress(uint32_t p_a, uint32_t p_b, uint32_t p_c, uint32_t p_d, bool is_v6) {
  244. clear();
  245. valid = true;
  246. if (!is_v6) {
  247. // Mapped to IPv6.
  248. field16[5] = 0xffff;
  249. field8[12] = p_a;
  250. field8[13] = p_b;
  251. field8[14] = p_c;
  252. field8[15] = p_d;
  253. } else {
  254. _32_to_buf(&field8[0], p_a);
  255. _32_to_buf(&field8[4], p_b);
  256. _32_to_buf(&field8[8], p_c);
  257. _32_to_buf(&field8[12], p_d);
  258. }
  259. }