/**************************************************************************/1/* ip_address.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 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/**************************************************************************/2930#pragma once3132#include "core/string/ustring.h"3334struct [[nodiscard]] IPAddress {35private:36union {37uint8_t field8[16];38uint16_t field16[8];39uint32_t field32[4];40};4142bool valid;43bool wildcard;4445protected:46void _parse_ipv6(const String &p_string);47void _parse_ipv4(const String &p_string, int p_start, uint8_t *p_ret);4849public:50//operator Variant() const;51bool operator==(const IPAddress &p_ip) const {52if (p_ip.valid != valid) {53return false;54}55if (!valid) {56return false;57}58for (int i = 0; i < 4; i++) {59if (field32[i] != p_ip.field32[i]) {60return false;61}62}63return true;64}6566bool operator!=(const IPAddress &p_ip) const {67if (p_ip.valid != valid) {68return true;69}70if (!valid) {71return true;72}73for (int i = 0; i < 4; i++) {74if (field32[i] != p_ip.field32[i]) {75return true;76}77}78return false;79}8081void clear();82bool is_wildcard() const { return wildcard; }83bool is_valid() const { return valid; }84bool is_ipv4() const;85const uint8_t *get_ipv4() const;86void set_ipv4(const uint8_t *p_ip);8788const uint8_t *get_ipv6() const;89void set_ipv6(const uint8_t *p_buf);9091explicit operator String() const;92IPAddress(const String &p_string);93IPAddress(uint32_t p_a, uint32_t p_b, uint32_t p_c, uint32_t p_d, bool is_v6 = false);94IPAddress() { clear(); }95};9697// Zero-constructing IPAddress initializes field, valid, and wildcard to 0 (and thus empty).98template <>99struct is_zero_constructible<IPAddress> : std::true_type {};100101102