port_util.h 2.5 KB

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