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