blob: 9ce0523413b132b4a7c04a7ec7aae41d7d3dbd06 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "rtc_base/win32.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2612
13#include <winsock2.h>
14#include <ws2tcpip.h>
Jonas Olssona4d87372019-07-05 17:08:3315
henrike@webrtc.orgf0488722014-05-13 18:00:2616#include <algorithm>
17
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "rtc_base/arraysize.h"
Steve Anton10542f22019-01-11 17:11:0019#include "rtc_base/byte_order.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3120#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 17:11:0021#include "rtc_base/string_utils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2622
23namespace rtc {
24
25// Helper function declarations for inet_ntop/inet_pton.
26static const char* inet_ntop_v4(const void* src, char* dst, socklen_t size);
27static const char* inet_ntop_v6(const void* src, char* dst, socklen_t size);
28static int inet_pton_v4(const char* src, void* dst);
29static int inet_pton_v6(const char* src, void* dst);
30
31// Implementation of inet_ntop (create a printable representation of an
32// ip address). XP doesn't have its own inet_ntop, and
33// WSAAddressToString requires both IPv6 to be installed and for Winsock
34// to be initialized.
Yves Gerey665174f2018-06-19 13:03:0535const char* win32_inet_ntop(int af,
36 const void* src,
37 char* dst,
38 socklen_t size) {
henrike@webrtc.orgf0488722014-05-13 18:00:2639 if (!src || !dst) {
deadbeef37f5ecf2017-02-27 22:06:4140 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:2641 }
42 switch (af) {
43 case AF_INET: {
44 return inet_ntop_v4(src, dst, size);
45 }
46 case AF_INET6: {
47 return inet_ntop_v6(src, dst, size);
48 }
49 }
deadbeef37f5ecf2017-02-27 22:06:4150 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:2651}
52
53// As above, but for inet_pton. Implements inet_pton for v4 and v6.
54// Note that our inet_ntop will output normal 'dotted' v4 addresses only.
55int win32_inet_pton(int af, const char* src, void* dst) {
56 if (!src || !dst) {
57 return 0;
58 }
59 if (af == AF_INET) {
60 return inet_pton_v4(src, dst);
61 } else if (af == AF_INET6) {
62 return inet_pton_v6(src, dst);
63 }
64 return -1;
65}
66
67// Helper function for inet_ntop for IPv4 addresses.
68// Outputs "dotted-quad" decimal notation.
69const char* inet_ntop_v4(const void* src, char* dst, socklen_t size) {
70 if (size < INET_ADDRSTRLEN) {
deadbeef37f5ecf2017-02-27 22:06:4171 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:2672 }
73 const struct in_addr* as_in_addr =
74 reinterpret_cast<const struct in_addr*>(src);
Niels Mölleraba06332018-10-16 13:14:1575 snprintf(dst, size, "%d.%d.%d.%d", as_in_addr->S_un.S_un_b.s_b1,
76 as_in_addr->S_un.S_un_b.s_b2, as_in_addr->S_un.S_un_b.s_b3,
77 as_in_addr->S_un.S_un_b.s_b4);
henrike@webrtc.orgf0488722014-05-13 18:00:2678 return dst;
79}
80
81// Helper function for inet_ntop for IPv6 addresses.
82const char* inet_ntop_v6(const void* src, char* dst, socklen_t size) {
83 if (size < INET6_ADDRSTRLEN) {
deadbeef37f5ecf2017-02-27 22:06:4184 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:2685 }
Peter Boström0c4e06b2015-10-07 10:23:2186 const uint16_t* as_shorts = reinterpret_cast<const uint16_t*>(src);
henrike@webrtc.orgf0488722014-05-13 18:00:2687 int runpos[8];
88 int current = 1;
guoweis@webrtc.orgb08f4042015-02-17 19:00:4289 int max = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:2690 int maxpos = -1;
tfarina5237aaf2015-11-11 07:44:3091 int run_array_size = arraysize(runpos);
henrike@webrtc.orgf0488722014-05-13 18:00:2692 // Run over the address marking runs of 0s.
93 for (int i = 0; i < run_array_size; ++i) {
94 if (as_shorts[i] == 0) {
95 runpos[i] = current;
96 if (current > max) {
97 maxpos = i;
98 max = current;
99 }
100 ++current;
101 } else {
102 runpos[i] = -1;
guoweis@webrtc.orgb08f4042015-02-17 19:00:42103 current = 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26104 }
105 }
106
guoweis@webrtc.orgb08f4042015-02-17 19:00:42107 if (max > 0) {
henrike@webrtc.orgf0488722014-05-13 18:00:26108 int tmpmax = maxpos;
109 // Run back through, setting -1 for all but the longest run.
110 for (int i = run_array_size - 1; i >= 0; i--) {
111 if (i > tmpmax) {
112 runpos[i] = -1;
113 } else if (runpos[i] == -1) {
114 // We're less than maxpos, we hit a -1, so the 'good' run is done.
115 // Setting tmpmax -1 means all remaining positions get set to -1.
116 tmpmax = -1;
117 }
118 }
119 }
120
121 char* cursor = dst;
122 // Print IPv4 compatible and IPv4 mapped addresses using the IPv4 helper.
123 // These addresses have an initial run of either eight zero-bytes followed
124 // by 0xFFFF, or an initial run of ten zero-bytes.
Yves Gerey665174f2018-06-19 13:03:05125 if (runpos[0] == 1 &&
126 (maxpos == 5 || (maxpos == 4 && as_shorts[5] == 0xFFFF))) {
henrike@webrtc.orgf0488722014-05-13 18:00:26127 *cursor++ = ':';
128 *cursor++ = ':';
129 if (maxpos == 4) {
Niels Mölleraba06332018-10-16 13:14:15130 cursor += snprintf(cursor, INET6_ADDRSTRLEN - 2, "ffff:");
henrike@webrtc.orgf0488722014-05-13 18:00:26131 }
132 const struct in_addr* as_v4 =
133 reinterpret_cast<const struct in_addr*>(&(as_shorts[6]));
134 inet_ntop_v4(as_v4, cursor,
135 static_cast<socklen_t>(INET6_ADDRSTRLEN - (cursor - dst)));
136 } else {
137 for (int i = 0; i < run_array_size; ++i) {
138 if (runpos[i] == -1) {
Niels Mölleraba06332018-10-16 13:14:15139 cursor += snprintf(cursor, INET6_ADDRSTRLEN - (cursor - dst), "%x",
140 NetworkToHost16(as_shorts[i]));
henrike@webrtc.orgf0488722014-05-13 18:00:26141 if (i != 7 && runpos[i + 1] != 1) {
142 *cursor++ = ':';
143 }
144 } else if (runpos[i] == 1) {
145 // Entered the run; print the colons and skip the run.
146 *cursor++ = ':';
147 *cursor++ = ':';
148 i += (max - 1);
149 }
150 }
151 }
152 return dst;
153}
154
155// Helper function for inet_pton for IPv4 addresses.
Artem Titov96e3b992021-07-26 14:03:14156// `src` points to a character string containing an IPv4 network address in
henrike@webrtc.orgf0488722014-05-13 18:00:26157// dotted-decimal format, "ddd.ddd.ddd.ddd", where ddd is a decimal number
158// of up to three digits in the range 0 to 255.
159// The address is converted and copied to dst,
160// which must be sizeof(struct in_addr) (4) bytes (32 bits) long.
161int inet_pton_v4(const char* src, void* dst) {
162 const int kIpv4AddressSize = 4;
163 int found = 0;
164 const char* src_pos = src;
165 unsigned char result[kIpv4AddressSize] = {0};
166
167 while (*src_pos != '\0') {
168 // strtol won't treat whitespace characters in the begining as an error,
169 // so check to ensure this is started with digit before passing to strtol.
170 if (!isdigit(*src_pos)) {
171 return 0;
172 }
173 char* end_pos;
174 long value = strtol(src_pos, &end_pos, 10);
175 if (value < 0 || value > 255 || src_pos == end_pos) {
176 return 0;
177 }
178 ++found;
179 if (found > kIpv4AddressSize) {
180 return 0;
181 }
182 result[found - 1] = static_cast<unsigned char>(value);
183 src_pos = end_pos;
184 if (*src_pos == '.') {
185 // There's more.
186 ++src_pos;
187 } else if (*src_pos != '\0') {
188 // If it's neither '.' nor '\0' then return fail.
189 return 0;
190 }
191 }
192 if (found != kIpv4AddressSize) {
193 return 0;
194 }
195 memcpy(dst, result, sizeof(result));
196 return 1;
197}
198
199// Helper function for inet_pton for IPv6 addresses.
200int inet_pton_v6(const char* src, void* dst) {
201 // sscanf will pick any other invalid chars up, but it parses 0xnnnn as hex.
202 // Check for literal x in the input string.
203 const char* readcursor = src;
204 char c = *readcursor++;
205 while (c) {
206 if (c == 'x') {
207 return 0;
208 }
209 c = *readcursor++;
210 }
211 readcursor = src;
212
213 struct in6_addr an_addr;
214 memset(&an_addr, 0, sizeof(an_addr));
215
Peter Boström0c4e06b2015-10-07 10:23:21216 uint16_t* addr_cursor = reinterpret_cast<uint16_t*>(&an_addr.s6_addr[0]);
217 uint16_t* addr_end = reinterpret_cast<uint16_t*>(&an_addr.s6_addr[16]);
henrike@webrtc.orgf0488722014-05-13 18:00:26218 bool seencompressed = false;
219
220 // Addresses that start with "::" (i.e., a run of initial zeros) or
221 // "::ffff:" can potentially be IPv4 mapped or compatibility addresses.
222 // These have dotted-style IPv4 addresses on the end (e.g. "::192.168.7.1").
Yves Gerey665174f2018-06-19 13:03:05223 if (*readcursor == ':' && *(readcursor + 1) == ':' &&
henrike@webrtc.orgf0488722014-05-13 18:00:26224 *(readcursor + 2) != 0) {
225 // Check for periods, which we'll take as a sign of v4 addresses.
226 const char* addrstart = readcursor + 2;
Niels Möllerd1892522018-10-17 11:39:01227 if (strchr(addrstart, '.')) {
228 const char* colon = strchr(addrstart, ':');
henrike@webrtc.orgf0488722014-05-13 18:00:26229 if (colon) {
Peter Boström0c4e06b2015-10-07 10:23:21230 uint16_t a_short;
henrike@webrtc.orgf0488722014-05-13 18:00:26231 int bytesread = 0;
232 if (sscanf(addrstart, "%hx%n", &a_short, &bytesread) != 1 ||
233 a_short != 0xFFFF || bytesread != 4) {
234 // Colons + periods means has to be ::ffff:a.b.c.d. But it wasn't.
235 return 0;
236 } else {
237 an_addr.s6_addr[10] = 0xFF;
238 an_addr.s6_addr[11] = 0xFF;
239 addrstart = colon + 1;
240 }
241 }
242 struct in_addr v4;
243 if (inet_pton_v4(addrstart, &v4.s_addr)) {
244 memcpy(&an_addr.s6_addr[12], &v4, sizeof(v4));
245 memcpy(dst, &an_addr, sizeof(an_addr));
246 return 1;
247 } else {
248 // Invalid v4 address.
249 return 0;
250 }
251 }
252 }
253
254 // For addresses without a trailing IPv4 component ('normal' IPv6 addresses).
255 while (*readcursor != 0 && addr_cursor < addr_end) {
256 if (*readcursor == ':') {
257 if (*(readcursor + 1) == ':') {
258 if (seencompressed) {
259 // Can only have one compressed run of zeroes ("::") per address.
260 return 0;
261 }
262 // Hit a compressed run. Count colons to figure out how much of the
263 // address is skipped.
264 readcursor += 2;
265 const char* coloncounter = readcursor;
266 int coloncount = 0;
267 if (*coloncounter == 0) {
268 // Special case - trailing ::.
269 addr_cursor = addr_end;
270 } else {
271 while (*coloncounter) {
272 if (*coloncounter == ':') {
273 ++coloncount;
274 }
275 ++coloncounter;
276 }
277 // (coloncount + 1) is the number of shorts left in the address.
deadbeef966963a2017-05-08 18:35:56278 // If this number is greater than the number of available shorts, the
279 // address is malformed.
280 if (coloncount + 1 > addr_end - addr_cursor) {
281 return 0;
282 }
henrike@webrtc.orgf0488722014-05-13 18:00:26283 addr_cursor = addr_end - (coloncount + 1);
284 seencompressed = true;
285 }
286 } else {
287 ++readcursor;
288 }
289 } else {
Peter Boström0c4e06b2015-10-07 10:23:21290 uint16_t word;
henrike@webrtc.orgf0488722014-05-13 18:00:26291 int bytesread = 0;
deadbeef966963a2017-05-08 18:35:56292 if (sscanf(readcursor, "%4hx%n", &word, &bytesread) != 1) {
henrike@webrtc.orgf0488722014-05-13 18:00:26293 return 0;
294 } else {
295 *addr_cursor = HostToNetwork16(word);
296 ++addr_cursor;
297 readcursor += bytesread;
298 if (*readcursor != ':' && *readcursor != '\0') {
299 return 0;
300 }
301 }
302 }
303 }
304
305 if (*readcursor != '\0' || addr_cursor < addr_end) {
306 // Catches addresses too short or too long.
307 return 0;
308 }
309 memcpy(dst, &an_addr, sizeof(an_addr));
310 return 1;
311}
312
henrike@webrtc.orgf0488722014-05-13 18:00:26313} // namespace rtc