Matt Menke | 75a706d | 2025-04-24 17:23:17 | [diff] [blame] | 1 | // Copyright 2025 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_BASE_IP_ADDRESS_UTIL_H_ |
| 6 | #define NET_BASE_IP_ADDRESS_UTIL_H_ |
| 7 | |
| 8 | #include "build/build_config.h" |
| 9 | |
| 10 | #if BUILDFLAG(IS_WIN) |
| 11 | #include <winsock2.h> |
| 12 | #include <ws2tcpip.h> |
| 13 | #else |
| 14 | #include <netinet/in.h> |
| 15 | #endif |
| 16 | |
| 17 | #include <stdint.h> |
| 18 | |
| 19 | #include "net/base/net_export.h" |
| 20 | |
| 21 | // This file contains helpers to convert a net::IPAddress to an in_addr or |
| 22 | // in6_addr. This is a separate file because using in_addr or in6_addr requires |
| 23 | // including winsock2.h on Windows, which brings in a massive number of |
| 24 | // dependencies. As a result, this file should not be included in any header |
| 25 | // files - it should only be used in CC files.. |
| 26 | |
| 27 | namespace net { |
| 28 | |
| 29 | class IPAddress; |
| 30 | |
| 31 | // Converts `ip_address` to an in_addr or in6_addr, respectively. CHECKs if |
| 32 | // `ip_address` is not of the requested type. Does not do IPv4/IPv6 conversions. |
| 33 | // Since in_addr values are arrays of bytes, rather than representing the entire |
| 34 | // address as a single value, there is no need to convert to network byte order. |
| 35 | NET_EXPORT in_addr ToInAddr(const IPAddress& ip_address); |
| 36 | NET_EXPORT in6_addr ToIn6Addr(const IPAddress& ip_address); |
| 37 | |
| 38 | } // namespace net |
| 39 | |
| 40 | #endif // NET_BASE_IP_ADDRESS_UTIL_H_ |