1fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// Use of this source code is governed by a BSD-style license that can be
3fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// found in the LICENSE file.
4fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk
5fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk#ifndef NET_BASE_NET_UTIL_H_
6fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk#define NET_BASE_NET_UTIL_H_
7fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk#pragma once
8fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk
9fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk#if defined(OS_WIN)
10fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk#include <windows.h>
11fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk#include <ws2tcpip.h>
12fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk#elif defined(OS_POSIX)
13fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk#include <sys/socket.h>
14fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk#endif
15fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk
16fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk#include <list>
17fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk#include <string>
18fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk#include <set>
19fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk#include <vector>
20fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk
21fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk#include <cstdint>
22fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk
23fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monknamespace net {
24fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk
25fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// IPAddressNumber is used to represent an IP address's numeric value as an
26fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// array of bytes, from most significant to least significant. This is the
27fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// network byte ordering.
28fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk//
29fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// IPv4 addresses will have length 4, whereas IPv6 address will have length 16.
30fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monktypedef std::vector<unsigned char> IPAddressNumber;
31fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monktypedef std::vector<IPAddressNumber> IPAddressList;
32fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk
33fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// Parses an IP address literal (either IPv4 or IPv6) to its numeric value.
34fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// Returns true on success and fills |ip_number| with the numeric value.
35fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monkbool ParseIPLiteralToNumber(const std::string& ip_literal,
36fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk                            IPAddressNumber* ip_number);
37fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk
38fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// Parses an IP block specifier from CIDR notation to an
39fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// (IP address, prefix length) pair. Returns true on success and fills
40fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// |*ip_number| with the numeric value of the IP address and sets
41fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// |*prefix_length_in_bits| with the length of the prefix.
42fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk//
43fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// CIDR notation literals can use either IPv4 or IPv6 literals. Some examples:
44fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk//
45fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk//    10.10.3.1/20
46fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk//    a:b:c::/46
47fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk//    ::1/128
48fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monkbool ParseCIDRBlock(const std::string& cidr_literal,
49fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk                    IPAddressNumber* ip_number,
50fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk                    size_t* prefix_length_in_bits);
51fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk
52fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// Compares an IP address to see if it falls within the specified IP block.
53fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// Returns true if it does, false otherwise.
54fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk//
55fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// The IP block is given by (|ip_prefix|, |prefix_length_in_bits|) -- any
56fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// IP address whose |prefix_length_in_bits| most significant bits match
57fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// |ip_prefix| will be matched.
58fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk//
59fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// In cases when an IPv4 address is being compared to an IPv6 address prefix
60fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// and vice versa, the IPv4 addresses will be converted to IPv4-mapped
61fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk// (IPv6) addresses.
62fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monkbool IPNumberMatchesPrefix(const IPAddressNumber& ip_number,
63fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk                           const IPAddressNumber& ip_prefix,
64fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk                           size_t prefix_length_in_bits);
65fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk
66fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk}  // namespace net
67fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk
68fc93418c483ce474a1f4888b50f92574a1b81be3Jason Monk#endif  // NET_BASE_NET_UTIL_H_
69