1// Copyright 2014 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 EXTENSIONS_COMMON_PERMISSIONS_SOCKET_PERMISSION_ENTRY_H_
5#define EXTENSIONS_COMMON_PERMISSIONS_SOCKET_PERMISSION_ENTRY_H_
6
7#include <string>
8#include <vector>
9
10#include "content/public/common/socket_permission_request.h"
11#include "ipc/ipc_param_traits.h"
12
13namespace ipc_fuzzer {
14template <class T>
15struct FuzzTraits;
16template <class T>
17struct GenerateTraits;
18}  // namespace ipc_fuzzer
19
20namespace extensions {
21
22// Internal representation of a socket permission for a specific operation, such
23// as UDP "bind", host 127.0.0.1, port *.
24class SocketPermissionEntry {
25 public:
26  enum HostType { ANY_HOST, HOSTS_IN_DOMAINS, SPECIFIC_HOSTS, };
27
28  SocketPermissionEntry();
29  ~SocketPermissionEntry();
30
31  // operators <, == are needed by container std::set and algorithms
32  // std::set_includes and std::set_differences.
33  bool operator<(const SocketPermissionEntry& rhs) const;
34  bool operator==(const SocketPermissionEntry& rhs) const;
35
36  bool Check(const content::SocketPermissionRequest& request) const;
37
38  // Parse a host:port pattern for a given operation type.
39  //   <pattern> := '' |
40  //                <host> |
41  //                ':' <port> |
42  //                <host> ':' <port> |
43  //
44  //   <host> := '*' |
45  //             '*.' <anychar except '/' and '*'>+ |
46  //             <anychar except '/' and '*'>+
47  //
48  //   <port> := '*' |
49  //             <port number between 0 and 65535>)
50  static bool ParseHostPattern(
51      content::SocketPermissionRequest::OperationType type,
52      const std::string& pattern,
53      SocketPermissionEntry* entry);
54
55  static bool ParseHostPattern(
56      content::SocketPermissionRequest::OperationType type,
57      const std::vector<std::string>& pattern_tokens,
58      SocketPermissionEntry* entry);
59
60  // Returns true if the permission type can be bound to a host or port.
61  bool IsAddressBoundType() const;
62
63  std::string GetHostPatternAsString() const;
64  HostType GetHostType() const;
65
66  const content::SocketPermissionRequest& pattern() const { return pattern_; }
67  bool match_subdomains() const { return match_subdomains_; }
68
69 private:
70  // Friend so ParamTraits can serialize us.
71  friend struct IPC::ParamTraits<SocketPermissionEntry>;
72  friend struct ipc_fuzzer::FuzzTraits<SocketPermissionEntry>;
73  friend struct ipc_fuzzer::GenerateTraits<SocketPermissionEntry>;
74
75  // The permission type, host and port.
76  content::SocketPermissionRequest pattern_;
77
78  // True if there was a wildcard in the host name.
79  bool match_subdomains_;
80};
81
82}  // namespace extensions
83
84#endif  // EXTENSIONS_COMMON_PERMISSIONS_SOCKET_PERMISSION_ENTRY_H_
85