ip_address.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #include <stdio.h>
  36. #include <string.h>
  37. IPAddress::operator String() const {
  38. if (wildcard) {
  39. return "*";
  40. }
  41. if (!valid) {
  42. return "";
  43. }
  44. if (is_ipv4()) {
  45. // IPv4 address mapped to IPv6
  46. return itos(field8[12]) + "." + itos(field8[13]) + "." + itos(field8[14]) + "." + itos(field8[15]);
  47. }
  48. String ret;
  49. for (int i = 0; i < 8; i++) {
  50. if (i > 0) {
  51. ret = ret + ":";
  52. }
  53. uint16_t num = (field8[i * 2] << 8) + field8[i * 2 + 1];
  54. ret = ret + String::num_int64(num, 16);
  55. }
  56. return ret;
  57. }
  58. static void _parse_hex(const String &p_string, int p_start, uint8_t *p_dst) {
  59. uint16_t ret = 0;
  60. for (int i = p_start; i < p_start + 4; i++) {
  61. if (i >= p_string.length()) {
  62. break;
  63. }
  64. int n = 0;
  65. char32_t c = p_string[i];
  66. if (is_digit(c)) {
  67. n = c - '0';
  68. } else if (c >= 'a' && c <= 'f') {
  69. n = 10 + (c - 'a');
  70. } else if (c >= 'A' && c <= 'F') {
  71. n = 10 + (c - 'A');
  72. } else if (c == ':') {
  73. break;
  74. } else {
  75. ERR_FAIL_MSG("Invalid character in IPv6 address: " + p_string + ".");
  76. }
  77. ret = ret << 4;
  78. ret += n;
  79. }
  80. p_dst[0] = ret >> 8;
  81. p_dst[1] = ret & 0xff;
  82. }
  83. void IPAddress::_parse_ipv6(const String &p_string) {
  84. static const int parts_total = 8;
  85. int parts[parts_total] = { 0 };
  86. int parts_count = 0;
  87. bool part_found = false;
  88. bool part_skip = false;
  89. bool part_ipv4 = false;
  90. int parts_idx = 0;
  91. for (int i = 0; i < p_string.length(); i++) {
  92. char32_t c = p_string[i];
  93. if (c == ':') {
  94. if (i == 0) {
  95. continue; // next must be a ":"
  96. }
  97. if (!part_found) {
  98. part_skip = true;
  99. parts[parts_idx++] = -1;
  100. }
  101. part_found = false;
  102. } else if (c == '.') {
  103. part_ipv4 = true;
  104. } else if (is_hex_digit(c)) {
  105. if (!part_found) {
  106. parts[parts_idx++] = i;
  107. part_found = true;
  108. ++parts_count;
  109. }
  110. } else {
  111. ERR_FAIL_MSG("Invalid character in IPv6 address: " + p_string + ".");
  112. }
  113. }
  114. int parts_extra = 0;
  115. if (part_skip) {
  116. parts_extra = parts_total - parts_count;
  117. }
  118. int idx = 0;
  119. for (int i = 0; i < parts_idx; i++) {
  120. if (parts[i] == -1) {
  121. for (int j = 0; j < parts_extra; j++) {
  122. field16[idx++] = 0;
  123. }
  124. continue;
  125. }
  126. if (part_ipv4 && i == parts_idx - 1) {
  127. _parse_ipv4(p_string, parts[i], (uint8_t *)&field16[idx]); // should be the last one
  128. } else {
  129. _parse_hex(p_string, parts[i], (uint8_t *)&(field16[idx++]));
  130. }
  131. }
  132. }
  133. void IPAddress::_parse_ipv4(const String &p_string, int p_start, uint8_t *p_ret) {
  134. String ip;
  135. if (p_start != 0) {
  136. ip = p_string.substr(p_start, p_string.length() - p_start);
  137. } else {
  138. ip = p_string;
  139. }
  140. int slices = ip.get_slice_count(".");
  141. ERR_FAIL_COND_MSG(slices != 4, "Invalid IP address string: " + ip + ".");
  142. for (int i = 0; i < 4; i++) {
  143. p_ret[i] = ip.get_slicec('.', i).to_int();
  144. }
  145. }
  146. void IPAddress::clear() {
  147. memset(&field8[0], 0, sizeof(field8));
  148. valid = false;
  149. wildcard = false;
  150. }
  151. bool IPAddress::is_ipv4() const {
  152. return (field32[0] == 0 && field32[1] == 0 && field16[4] == 0 && field16[5] == 0xffff);
  153. }
  154. const uint8_t *IPAddress::get_ipv4() const {
  155. 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.
  156. return &(field8[12]);
  157. }
  158. void IPAddress::set_ipv4(const uint8_t *p_ip) {
  159. clear();
  160. valid = true;
  161. field16[5] = 0xffff;
  162. field32[3] = *((const uint32_t *)p_ip);
  163. }
  164. const uint8_t *IPAddress::get_ipv6() const {
  165. return field8;
  166. }
  167. void IPAddress::set_ipv6(const uint8_t *p_buf) {
  168. clear();
  169. valid = true;
  170. for (int i = 0; i < 16; i++) {
  171. field8[i] = p_buf[i];
  172. }
  173. }
  174. IPAddress::IPAddress(const String &p_string) {
  175. clear();
  176. if (p_string == "*") {
  177. // Wildcard (not a valid IP)
  178. wildcard = true;
  179. } else if (p_string.contains(":")) {
  180. // IPv6
  181. _parse_ipv6(p_string);
  182. valid = true;
  183. } else if (p_string.get_slice_count(".") == 4) {
  184. // IPv4 (mapped to IPv6 internally)
  185. field16[5] = 0xffff;
  186. _parse_ipv4(p_string, 0, &field8[12]);
  187. valid = true;
  188. } else {
  189. ERR_PRINT("Invalid IP address.");
  190. }
  191. }
  192. _FORCE_INLINE_ static void _32_to_buf(uint8_t *p_dst, uint32_t p_n) {
  193. p_dst[0] = (p_n >> 24) & 0xff;
  194. p_dst[1] = (p_n >> 16) & 0xff;
  195. p_dst[2] = (p_n >> 8) & 0xff;
  196. p_dst[3] = (p_n >> 0) & 0xff;
  197. }
  198. IPAddress::IPAddress(uint32_t p_a, uint32_t p_b, uint32_t p_c, uint32_t p_d, bool is_v6) {
  199. clear();
  200. valid = true;
  201. if (!is_v6) {
  202. // Mapped to IPv6
  203. field16[5] = 0xffff;
  204. field8[12] = p_a;
  205. field8[13] = p_b;
  206. field8[14] = p_c;
  207. field8[15] = p_d;
  208. } else {
  209. _32_to_buf(&field8[0], p_a);
  210. _32_to_buf(&field8[4], p_b);
  211. _32_to_buf(&field8[8], p_c);
  212. _32_to_buf(&field8[12], p_d);
  213. }
  214. }