• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 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_PORT_UTIL_H_
6 #define NET_BASE_PORT_UTIL_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include "base/containers/span.h"
12 #include "base/strings/string_piece.h"
13 #include "net/base/net_export.h"
14 
15 namespace net {
16 
17 // Checks if |port| is in the valid range (0 to 65535, though 0 is technically
18 // reserved).  Should be used before casting a port to a uint16_t.
19 NET_EXPORT bool IsPortValid(int port);
20 
21 // Returns true if the port is in the range [0, 1023]. These ports are
22 // registered by IANA and typically need root access to listen on.
23 NET_EXPORT bool IsWellKnownPort(int port);
24 
25 // Checks if the port is allowed for the specified scheme.  Ports set as allowed
26 // with SetExplicitlyAllowedPorts() or by using ScopedPortException() will be
27 // considered allowed for any scheme.
28 NET_EXPORT bool IsPortAllowedForScheme(int port, base::StringPiece url_scheme);
29 
30 // Returns the number of explicitly allowed ports; for testing.
31 NET_EXPORT_PRIVATE size_t GetCountOfExplicitlyAllowedPorts();
32 
33 // Set the list of ports to be allowed that otherwise would not be. This
34 // replaces the list of allowed ports with the list passed to this function. An
35 // empty list will remove all ports. This will reset any ScopedPortExceptions
36 // currently active, so it's best to avoid calling this when any of those are
37 // active.
38 NET_EXPORT void SetExplicitlyAllowedPorts(
39     base::span<const uint16_t> allowed_ports);
40 
41 // Returns true for ports which are permitted to be passed to
42 // SetExplicitlyAllowedPorts(). This is not currently enforced by
43 // SetExplicitlyAllowedPorts() itself, as there are still callers that pass
44 // other ports.
45 NET_EXPORT bool IsAllowablePort(int port);
46 
47 class NET_EXPORT ScopedPortException {
48  public:
49   explicit ScopedPortException(int port);
50   ScopedPortException(const ScopedPortException&) = delete;
51   ScopedPortException& operator=(const ScopedPortException&) = delete;
52   ~ScopedPortException();
53 
54  private:
55   int port_;
56 };
57 
58 // Adds a port to the set permitted by GetAllowablePorts(). Cannot be nested.
59 class NET_EXPORT ScopedAllowablePortForTesting {
60  public:
61   explicit ScopedAllowablePortForTesting(int port);
62   ScopedAllowablePortForTesting(const ScopedAllowablePortForTesting&) = delete;
63   ScopedAllowablePortForTesting& operator=(
64       const ScopedAllowablePortForTesting&) = delete;
65   ~ScopedAllowablePortForTesting();
66 };
67 
68 }  // namespace net
69 
70 #endif  // NET_BASE_PORT_UTIL_H_
71