OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROMEOS_NETWORK_FIREWALL_HOLE_H_ | 5 #ifndef CHROMEOS_NETWORK_FIREWALL_HOLE_H_ |
6 #define CHROMEOS_NETWORK_FIREWALL_HOLE_H_ | 6 #define CHROMEOS_NETWORK_FIREWALL_HOLE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "chromeos/chromeos_export.h" | 13 #include "chromeos/chromeos_export.h" |
14 | 14 #include "dbus/file_descriptor.h" |
15 namespace dbus { | |
16 class FileDescriptor; | |
17 } | |
18 | 15 |
19 namespace chromeos { | 16 namespace chromeos { |
20 | 17 |
21 // This class works with the Chrome OS permission broker to open a port in the | 18 // This class works with the Chrome OS permission broker to open a port in the |
22 // system firewall. It is closed on destruction. | 19 // system firewall. It is closed on destruction. |
23 class CHROMEOS_EXPORT FirewallHole { | 20 class CHROMEOS_EXPORT FirewallHole { |
24 public: | 21 public: |
25 enum class PortType { | 22 enum class PortType { |
26 UDP, | 23 UDP, |
27 TCP, | 24 TCP, |
28 }; | 25 }; |
29 | 26 |
30 typedef base::Callback<void(scoped_ptr<FirewallHole>)> OpenCallback; | 27 typedef base::Callback<void(scoped_ptr<FirewallHole>)> OpenCallback; |
31 | 28 |
32 // This provides a simple way to pass around file descriptors since they must | |
33 // be closed on a thread that is allowed to perform I/O. | |
34 struct FileDescriptorDeleter { | |
35 void CHROMEOS_EXPORT operator()(dbus::FileDescriptor* fd); | |
36 }; | |
37 typedef scoped_ptr<dbus::FileDescriptor, FileDescriptorDeleter> | |
38 ScopedFileDescriptor; | |
39 | |
40 // Opens a port on the system firewall for the given network interface (or all | 29 // Opens a port on the system firewall for the given network interface (or all |
41 // interfaces if |interface| is ""). The hole will be closed when the object | 30 // interfaces if |interface| is ""). The hole will be closed when the object |
42 // provided to the callback is destroyed. | 31 // provided to the callback is destroyed. |
43 static void Open(PortType type, | 32 static void Open(PortType type, |
44 uint16_t port, | 33 uint16_t port, |
45 const std::string& interface, | 34 const std::string& interface, |
46 const OpenCallback& callback); | 35 const OpenCallback& callback); |
47 | 36 |
48 ~FirewallHole(); | 37 ~FirewallHole(); |
49 | 38 |
50 private: | 39 private: |
51 static void RequestPortAccess(PortType type, | 40 static void RequestPortAccess(PortType type, |
52 uint16_t port, | 41 uint16_t port, |
53 const std::string& interface, | 42 const std::string& interface, |
54 ScopedFileDescriptor lifeline_local, | 43 dbus::ScopedFileDescriptor lifeline_local, |
55 ScopedFileDescriptor lifeline_remote, | 44 dbus::ScopedFileDescriptor lifeline_remote, |
56 const OpenCallback& callback); | 45 const OpenCallback& callback); |
57 | 46 |
58 static void PortAccessGranted(PortType type, | 47 static void PortAccessGranted(PortType type, |
59 uint16_t port, | 48 uint16_t port, |
60 const std::string& interface, | 49 const std::string& interface, |
61 ScopedFileDescriptor lifeline_fd, | 50 dbus::ScopedFileDescriptor lifeline_fd, |
62 const FirewallHole::OpenCallback& callback, | 51 const FirewallHole::OpenCallback& callback, |
63 bool success); | 52 bool success); |
64 | 53 |
65 FirewallHole(PortType type, | 54 FirewallHole(PortType type, |
66 uint16_t port, | 55 uint16_t port, |
67 const std::string& interface, | 56 const std::string& interface, |
68 ScopedFileDescriptor lifeline_fd); | 57 dbus::ScopedFileDescriptor lifeline_fd); |
69 | 58 |
70 const PortType type_; | 59 const PortType type_; |
71 const uint16_t port_; | 60 const uint16_t port_; |
72 const std::string interface_; | 61 const std::string interface_; |
73 | 62 |
74 // A file descriptor used by firewalld to track the lifetime of this process. | 63 // A file descriptor used by firewalld to track the lifetime of this process. |
75 ScopedFileDescriptor lifeline_fd_; | 64 dbus::ScopedFileDescriptor lifeline_fd_; |
76 }; | 65 }; |
77 | 66 |
78 } // namespace chromeos | 67 } // namespace chromeos |
79 | 68 |
80 #endif // CHROMEOS_NETWORK_FIREWALL_HOLE_H_ | 69 #endif // CHROMEOS_NETWORK_FIREWALL_HOLE_H_ |
OLD | NEW |