ip_address.cpp 6.7 KB

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