ip_address.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*************************************************************************/
  2. /* ip_address.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "ip_address.h"
  30. /*
  31. IP_Address::operator Variant() const {
  32. return operator String();
  33. }*/
  34. #include <stdio.h>
  35. #include <string.h>
  36. IP_Address::operator String() const {
  37. if (!valid)
  38. return "";
  39. if (is_ipv4())
  40. // IPv4 address mapped to IPv6
  41. return itos(field8[12]) + "." + itos(field8[13]) + "." + itos(field8[14]) + "." + itos(field8[15]);
  42. String ret;
  43. for (int i = 0; i < 8; i++) {
  44. if (i > 0)
  45. ret = ret + ":";
  46. uint16_t num = (field8[i * 2] << 8) + field8[i * 2 + 1];
  47. ret = ret + String::num_int64(num, 16);
  48. };
  49. return ret;
  50. }
  51. static void _parse_hex(const String &p_string, int p_start, uint8_t *p_dst) {
  52. uint16_t ret = 0;
  53. for (int i = p_start; i < p_start + 4; i++) {
  54. if (i >= p_string.length()) {
  55. break;
  56. };
  57. int n = 0;
  58. CharType c = p_string[i];
  59. if (c >= '0' && c <= '9') {
  60. n = c - '0';
  61. } else if (c >= 'a' && c <= 'f') {
  62. n = 10 + (c - 'a');
  63. } else if (c >= 'A' && c <= 'F') {
  64. n = 10 + (c - 'A');
  65. } else if (c == ':') {
  66. break;
  67. } else {
  68. ERR_EXPLAIN("Invalid character in ipv6 address: " + p_string);
  69. ERR_FAIL();
  70. };
  71. ret = ret << 4;
  72. ret += n;
  73. };
  74. p_dst[0] = ret >> 8;
  75. p_dst[1] = ret & 0xff;
  76. };
  77. void IP_Address::_parse_ipv6(const String &p_string) {
  78. static const int parts_total = 8;
  79. int parts[parts_total] = { 0 };
  80. int parts_count = 0;
  81. bool part_found = false;
  82. bool part_skip = false;
  83. bool part_ipv4 = false;
  84. int parts_idx = 0;
  85. for (int i = 0; i < p_string.length(); i++) {
  86. CharType c = p_string[i];
  87. if (c == ':') {
  88. if (i == 0) {
  89. continue; // next must be a ":"
  90. };
  91. if (!part_found) {
  92. part_skip = true;
  93. parts[parts_idx++] = -1;
  94. };
  95. part_found = false;
  96. } else if (c == '.') {
  97. part_ipv4 = true;
  98. } else if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
  99. if (!part_found) {
  100. parts[parts_idx++] = i;
  101. part_found = true;
  102. ++parts_count;
  103. };
  104. } else {
  105. ERR_EXPLAIN("Invalid character in IPv6 address: " + p_string);
  106. ERR_FAIL();
  107. };
  108. };
  109. int parts_extra = 0;
  110. if (part_skip) {
  111. parts_extra = parts_total - parts_count;
  112. };
  113. int idx = 0;
  114. for (int i = 0; i < parts_idx; i++) {
  115. if (parts[i] == -1) {
  116. for (int j = 0; j < parts_extra; j++) {
  117. field16[idx++] = 0;
  118. };
  119. continue;
  120. };
  121. if (part_ipv4 && i == parts_idx - 1) {
  122. _parse_ipv4(p_string, parts[i], (uint8_t *)&field16[idx]); // should be the last one
  123. } else {
  124. _parse_hex(p_string, parts[i], (uint8_t *)&(field16[idx++]));
  125. };
  126. };
  127. };
  128. void IP_Address::_parse_ipv4(const String &p_string, int p_start, uint8_t *p_ret) {
  129. String ip;
  130. if (p_start != 0) {
  131. ip = p_string.substr(p_start, p_string.length() - p_start);
  132. } else {
  133. ip = p_string;
  134. };
  135. int slices = ip.get_slice_count(".");
  136. if (slices != 4) {
  137. ERR_EXPLAIN("Invalid IP Address String: " + ip);
  138. ERR_FAIL();
  139. }
  140. for (int i = 0; i < 4; i++) {
  141. p_ret[i] = ip.get_slicec('.', i).to_int();
  142. }
  143. };
  144. void IP_Address::clear() {
  145. memset(&field8[0], 0, sizeof(field8));
  146. valid = false;
  147. wildcard = false;
  148. };
  149. bool IP_Address::is_ipv4() const {
  150. return (field32[0] == 0 && field32[1] == 0 && field16[4] == 0 && field16[5] == 0xffff);
  151. }
  152. const uint8_t *IP_Address::get_ipv4() const {
  153. ERR_FAIL_COND_V(!is_ipv4(), 0);
  154. return &(field8[12]);
  155. }
  156. void IP_Address::set_ipv4(const uint8_t *p_ip) {
  157. clear();
  158. valid = true;
  159. field16[5] = 0xffff;
  160. field32[3] = *((const uint32_t *)p_ip);
  161. }
  162. const uint8_t *IP_Address::get_ipv6() const {
  163. return field8;
  164. }
  165. void IP_Address::set_ipv6(const uint8_t *p_buf) {
  166. clear();
  167. valid = true;
  168. for (int i = 0; i < 16; i++)
  169. field8[i] = p_buf[i];
  170. }
  171. IP_Address::IP_Address(const String &p_string) {
  172. clear();
  173. if (p_string == "*") {
  174. // Wildcard (not a vaild IP)
  175. wildcard = true;
  176. } else if (p_string.find(":") >= 0) {
  177. // IPv6
  178. _parse_ipv6(p_string);
  179. valid = true;
  180. } else if (p_string.get_slice_count(".") == 4) {
  181. // IPv4 (mapped to IPv6 internally)
  182. field16[5] = 0xffff;
  183. _parse_ipv4(p_string, 0, &field8[12]);
  184. valid = true;
  185. } else {
  186. ERR_PRINT("Invalid IP address");
  187. }
  188. }
  189. _FORCE_INLINE_ static void _32_to_buf(uint8_t *p_dst, uint32_t p_n) {
  190. p_dst[0] = (p_n >> 24) & 0xff;
  191. p_dst[1] = (p_n >> 16) & 0xff;
  192. p_dst[2] = (p_n >> 8) & 0xff;
  193. p_dst[3] = (p_n >> 0) & 0xff;
  194. };
  195. IP_Address::IP_Address(uint32_t p_a, uint32_t p_b, uint32_t p_c, uint32_t p_d, bool is_v6) {
  196. clear();
  197. valid = true;
  198. if (!is_v6) {
  199. // Mapped to IPv6
  200. field16[5] = 0xffff;
  201. field8[12] = p_a;
  202. field8[13] = p_b;
  203. field8[14] = p_c;
  204. field8[15] = p_d;
  205. } else {
  206. _32_to_buf(&field8[0], p_a);
  207. _32_to_buf(&field8[4], p_b);
  208. _32_to_buf(&field8[8], p_c);
  209. _32_to_buf(&field8[12], p_d);
  210. }
  211. }