/**************************************************************************/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};4142enum {43IPV4_MAX_STRING_LENGTH = 16,44IPV6_MAX_STRING_LENGTH = 40,45};4647bool valid;48bool wildcard;4950protected:51static bool _parse_ipv6(const String &p_string, IPAddress &r_ip);52static bool _parse_ipv4(const String &p_string, int p_start, uint8_t *r_dest);5354public:55//operator Variant() const;56bool operator==(const IPAddress &p_ip) const {57if (p_ip.valid != valid) {58return false;59}60if (!valid) {61return false;62}63for (int i = 0; i < 4; i++) {64if (field32[i] != p_ip.field32[i]) {65return false;66}67}68return true;69}7071bool operator!=(const IPAddress &p_ip) const {72if (p_ip.valid != valid) {73return true;74}75if (!valid) {76return true;77}78for (int i = 0; i < 4; i++) {79if (field32[i] != p_ip.field32[i]) {80return true;81}82}83return false;84}8586static bool is_valid_ip_address(const String &p_string);8788void clear();89bool is_wildcard() const { return wildcard; }90bool is_valid() const { return valid; }91bool is_ipv4() const;92const uint8_t *get_ipv4() const;93void set_ipv4(const uint8_t *p_ip);9495const uint8_t *get_ipv6() const;96void set_ipv6(const uint8_t *p_buf);9798explicit operator String() const;99IPAddress(const String &p_string);100IPAddress(uint32_t p_a, uint32_t p_b, uint32_t p_c, uint32_t p_d, bool is_v6 = false);101IPAddress() { clear(); }102};103104// Zero-constructing IPAddress initializes field, valid, and wildcard to 0 (and thus empty).105template <>106struct is_zero_constructible<IPAddress> : std::true_type {};107108109