1/**************************************************************************/
2/* ip_address.cpp */
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#include "ip_address.h"
32/*
33IPAddress::operator Variant() const {
34 return operator String();
35}*/
36
37#include <stdio.h>
38#include <string.h>
39
40IPAddress::operator String() const {
41 if (wildcard) {
42 return "*";
43 }
44
45 if (!valid) {
46 return "";
47 }
48
49 if (is_ipv4()) {
50 // IPv4 address mapped to IPv6
51 return itos(field8[12]) + "." + itos(field8[13]) + "." + itos(field8[14]) + "." + itos(field8[15]);
52 }
53 String ret;
54 for (int i = 0; i < 8; i++) {
55 if (i > 0) {
56 ret = ret + ":";
57 }
58 uint16_t num = (field8[i * 2] << 8) + field8[i * 2 + 1];
59 ret = ret + String::num_int64(num, 16);
60 }
61
62 return ret;
63}
64
65static void _parse_hex(const String &p_string, int p_start, uint8_t *p_dst) {
66 uint16_t ret = 0;
67 for (int i = p_start; i < p_start + 4; i++) {
68 if (i >= p_string.length()) {
69 break;
70 }
71
72 int n = 0;
73 char32_t c = p_string[i];
74 if (is_digit(c)) {
75 n = c - '0';
76 } else if (c >= 'a' && c <= 'f') {
77 n = 10 + (c - 'a');
78 } else if (c >= 'A' && c <= 'F') {
79 n = 10 + (c - 'A');
80 } else if (c == ':') {
81 break;
82 } else {
83 ERR_FAIL_MSG("Invalid character in IPv6 address: " + p_string + ".");
84 }
85 ret = ret << 4;
86 ret += n;
87 }
88
89 p_dst[0] = ret >> 8;
90 p_dst[1] = ret & 0xff;
91}
92
93void IPAddress::_parse_ipv6(const String &p_string) {
94 static const int parts_total = 8;
95 int parts[parts_total] = { 0 };
96 int parts_count = 0;
97 bool part_found = false;
98 bool part_skip = false;
99 bool part_ipv4 = false;
100 int parts_idx = 0;
101
102 for (int i = 0; i < p_string.length(); i++) {
103 char32_t c = p_string[i];
104 if (c == ':') {
105 if (i == 0) {
106 continue; // next must be a ":"
107 }
108 if (!part_found) {
109 part_skip = true;
110 parts[parts_idx++] = -1;
111 }
112 part_found = false;
113 } else if (c == '.') {
114 part_ipv4 = true;
115
116 } else if (is_hex_digit(c)) {
117 if (!part_found) {
118 parts[parts_idx++] = i;
119 part_found = true;
120 ++parts_count;
121 }
122 } else {
123 ERR_FAIL_MSG("Invalid character in IPv6 address: " + p_string + ".");
124 }
125 }
126
127 int parts_extra = 0;
128 if (part_skip) {
129 parts_extra = parts_total - parts_count;
130 }
131
132 int idx = 0;
133 for (int i = 0; i < parts_idx; i++) {
134 if (parts[i] == -1) {
135 for (int j = 0; j < parts_extra; j++) {
136 field16[idx++] = 0;
137 }
138 continue;
139 }
140
141 if (part_ipv4 && i == parts_idx - 1) {
142 _parse_ipv4(p_string, parts[i], (uint8_t *)&field16[idx]); // should be the last one
143 } else {
144 _parse_hex(p_string, parts[i], (uint8_t *)&(field16[idx++]));
145 }
146 }
147}
148
149void IPAddress::_parse_ipv4(const String &p_string, int p_start, uint8_t *p_ret) {
150 String ip;
151 if (p_start != 0) {
152 ip = p_string.substr(p_start, p_string.length() - p_start);
153 } else {
154 ip = p_string;
155 }
156
157 int slices = ip.get_slice_count(".");
158 ERR_FAIL_COND_MSG(slices != 4, "Invalid IP address string: " + ip + ".");
159 for (int i = 0; i < 4; i++) {
160 p_ret[i] = ip.get_slicec('.', i).to_int();
161 }
162}
163
164void IPAddress::clear() {
165 memset(&field8[0], 0, sizeof(field8));
166 valid = false;
167 wildcard = false;
168}
169
170bool IPAddress::is_ipv4() const {
171 return (field32[0] == 0 && field32[1] == 0 && field16[4] == 0 && field16[5] == 0xffff);
172}
173
174const uint8_t *IPAddress::get_ipv4() const {
175 ERR_FAIL_COND_V_MSG(!is_ipv4(), &(field8[12]), "IPv4 requested, but current IP is IPv6."); // Not the correct IPv4 (it's an IPv6), but we don't want to return a null pointer risking an engine crash.
176 return &(field8[12]);
177}
178
179void IPAddress::set_ipv4(const uint8_t *p_ip) {
180 clear();
181 valid = true;
182 field16[5] = 0xffff;
183 field32[3] = *((const uint32_t *)p_ip);
184}
185
186const uint8_t *IPAddress::get_ipv6() const {
187 return field8;
188}
189
190void IPAddress::set_ipv6(const uint8_t *p_buf) {
191 clear();
192 valid = true;
193 for (int i = 0; i < 16; i++) {
194 field8[i] = p_buf[i];
195 }
196}
197
198IPAddress::IPAddress(const String &p_string) {
199 clear();
200
201 if (p_string == "*") {
202 // Wildcard (not a valid IP)
203 wildcard = true;
204
205 } else if (p_string.find(":") >= 0) {
206 // IPv6
207 _parse_ipv6(p_string);
208 valid = true;
209
210 } else if (p_string.get_slice_count(".") == 4) {
211 // IPv4 (mapped to IPv6 internally)
212 field16[5] = 0xffff;
213 _parse_ipv4(p_string, 0, &field8[12]);
214 valid = true;
215
216 } else {
217 ERR_PRINT("Invalid IP address.");
218 }
219}
220
221_FORCE_INLINE_ static void _32_to_buf(uint8_t *p_dst, uint32_t p_n) {
222 p_dst[0] = (p_n >> 24) & 0xff;
223 p_dst[1] = (p_n >> 16) & 0xff;
224 p_dst[2] = (p_n >> 8) & 0xff;
225 p_dst[3] = (p_n >> 0) & 0xff;
226}
227
228IPAddress::IPAddress(uint32_t p_a, uint32_t p_b, uint32_t p_c, uint32_t p_d, bool is_v6) {
229 clear();
230 valid = true;
231 if (!is_v6) {
232 // Mapped to IPv6
233 field16[5] = 0xffff;
234 field8[12] = p_a;
235 field8[13] = p_b;
236 field8[14] = p_c;
237 field8[15] = p_d;
238 } else {
239 _32_to_buf(&field8[0], p_a);
240 _32_to_buf(&field8[4], p_b);
241 _32_to_buf(&field8[8], p_c);
242 _32_to_buf(&field8[12], p_d);
243 }
244}
245