Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(345)

Side by Side Diff: net/base/ip_address.h

Issue 1408803010: Add IPAddress class as a replacement for the IPAddressNumber typedef. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove duplicated implementations Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | net/base/ip_address.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
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_NET_H_
6 #define NET_BASE_IP_ADDRESS_NET_H_
7
8 #include <vector>
9
10 #include "base/basictypes.h"
tfarina 2015/12/12 01:21:15 hum, we should include <stdint.h> instead now.
11 #include "base/compiler_specific.h"
12 #include "base/strings/string_piece.h"
13 #include "net/base/net_export.h"
14
15 namespace net {
16
17 class NET_EXPORT IPAddress {
18 public:
19 static const size_t kIPv4AddressSize;
20 static const size_t kIPv6AddressSize;
21
22 // Creates a zero-sized, invalid address.
23 IPAddress();
24
25 // Copies the input address to |ip_address_|. The input is expected to be in
26 // network byte order.
27 IPAddress(const uint8_t* address, size_t address_len);
28 ~IPAddress();
29
30 // Returns true if the IP has |kIPv4AddressSize| elements.
31 bool IsIPv4() const;
32
33 // Returns true if the IP has |kIPv6AddressSize| elements.
34 bool IsIPv6() const;
35
36 // Returns true if an IP address hostname is in a range reserved by the IANA.
37 // Works with both IPv4 and IPv6 addresses, and only compares against a given
38 // protocols's reserved ranges.
39 bool IsReserved() const;
40
41 // Returns true if |ip_address_| is an IPv4-mapped IPv6 address.
42 bool IsIPv4Mapped() const;
43
44 // The size in bytes of |ip_address_|.
45 size_t size() const { return ip_address_.size(); }
46
47 // Returns the canonical string representation of an IP address.
48 // For example: "192.168.0.1" or "::1". The IP address must be
49 // valid, calling this on an invalid address will result in a crash.
50 std::string ToString() const;
51
52 // Parses an IP address literal (either IPv4 or IPv6) to its numeric value.
53 // Returns true on success and fills |ip_address| with the numeric value.
54 static bool FromIPLiteral(const base::StringPiece& ip_literal,
55 IPAddress* ip_address) WARN_UNUSED_RESULT;
56
57 // Returns the underlying byte vector.
58 const std::vector<uint8_t>& bytes() const { return ip_address_; };
59
60 bool operator==(const IPAddress& that) const;
61 bool operator<(const IPAddress& that) const;
62
63 private:
64 // IPv4 addresses will have length kIPv4AddressSize, whereas IPv6 address
65 // will have length kIPv6AddressSize.
66 std::vector<uint8_t> ip_address_;
67 };
68
69 } // namespace net
70
71 #endif // NET_BASE_IP_ADDRESS_NET_H_
OLDNEW
« no previous file with comments | « no previous file | net/base/ip_address.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698