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