ip_address.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**************************************************************************/
  2. /* ip_address.h */
  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. #pragma once
  31. #include "core/string/ustring.h"
  32. struct [[nodiscard]] IPAddress {
  33. private:
  34. union {
  35. uint8_t field8[16];
  36. uint16_t field16[8];
  37. uint32_t field32[4];
  38. };
  39. bool valid;
  40. bool wildcard;
  41. protected:
  42. void _parse_ipv6(const String &p_string);
  43. void _parse_ipv4(const String &p_string, int p_start, uint8_t *p_ret);
  44. public:
  45. //operator Variant() const;
  46. bool operator==(const IPAddress &p_ip) const {
  47. if (p_ip.valid != valid) {
  48. return false;
  49. }
  50. if (!valid) {
  51. return false;
  52. }
  53. for (int i = 0; i < 4; i++) {
  54. if (field32[i] != p_ip.field32[i]) {
  55. return false;
  56. }
  57. }
  58. return true;
  59. }
  60. bool operator!=(const IPAddress &p_ip) const {
  61. if (p_ip.valid != valid) {
  62. return true;
  63. }
  64. if (!valid) {
  65. return true;
  66. }
  67. for (int i = 0; i < 4; i++) {
  68. if (field32[i] != p_ip.field32[i]) {
  69. return true;
  70. }
  71. }
  72. return false;
  73. }
  74. void clear();
  75. bool is_wildcard() const { return wildcard; }
  76. bool is_valid() const { return valid; }
  77. bool is_ipv4() const;
  78. const uint8_t *get_ipv4() const;
  79. void set_ipv4(const uint8_t *p_ip);
  80. const uint8_t *get_ipv6() const;
  81. void set_ipv6(const uint8_t *p_buf);
  82. operator String() const;
  83. IPAddress(const String &p_string);
  84. IPAddress(uint32_t p_a, uint32_t p_b, uint32_t p_c, uint32_t p_d, bool is_v6 = false);
  85. IPAddress() { clear(); }
  86. };
  87. // Zero-constructing IPAddress initializes field, valid, and wildcard to 0 (and thus empty).
  88. template <>
  89. struct is_zero_constructible<IPAddress> : std::true_type {};