ip_address.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*************************************************************************/
  2. /* ip_address.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #ifndef IP_ADDRESS_H
  31. #define IP_ADDRESS_H
  32. #include "core/ustring.h"
  33. struct IP_Address {
  34. private:
  35. union {
  36. uint8_t field8[16];
  37. uint16_t field16[8];
  38. uint32_t field32[4];
  39. };
  40. bool valid;
  41. bool wildcard;
  42. protected:
  43. void _parse_ipv6(const String &p_string);
  44. void _parse_ipv4(const String &p_string, int p_start, uint8_t *p_ret);
  45. public:
  46. //operator Variant() const;
  47. bool operator==(const IP_Address &p_ip) const {
  48. if (p_ip.valid != valid) return false;
  49. if (!valid) return false;
  50. for (int i = 0; i < 4; i++)
  51. if (field32[i] != p_ip.field32[i])
  52. return false;
  53. return true;
  54. }
  55. bool operator!=(const IP_Address &p_ip) const {
  56. if (p_ip.valid != valid) return true;
  57. if (!valid) return true;
  58. for (int i = 0; i < 4; i++)
  59. if (field32[i] != p_ip.field32[i])
  60. return true;
  61. return false;
  62. }
  63. void clear();
  64. bool is_wildcard() const { return wildcard; }
  65. bool is_valid() const { return valid; }
  66. bool is_ipv4() const;
  67. const uint8_t *get_ipv4() const;
  68. void set_ipv4(const uint8_t *p_ip);
  69. const uint8_t *get_ipv6() const;
  70. void set_ipv6(const uint8_t *p_buf);
  71. operator String() const;
  72. IP_Address(const String &p_string);
  73. IP_Address(uint32_t p_a, uint32_t p_b, uint32_t p_c, uint32_t p_d, bool is_v6 = false);
  74. IP_Address() { clear(); }
  75. };
  76. #endif // IP_ADDRESS_H