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 #include "dbus/file_descriptor.h" | 14 |
| 15 namespace dbus { |
| 16 class FileDescriptor; |
| 17 } |
15 | 18 |
16 namespace chromeos { | 19 namespace chromeos { |
17 | 20 |
18 // This class works with the Chrome OS permission broker to open a port in the | 21 // This class works with the Chrome OS permission broker to open a port in the |
19 // system firewall. It is closed on destruction. | 22 // system firewall. It is closed on destruction. |
20 class CHROMEOS_EXPORT FirewallHole { | 23 class CHROMEOS_EXPORT FirewallHole { |
21 public: | 24 public: |
22 enum class PortType { | 25 enum class PortType { |
23 UDP, | 26 UDP, |
24 TCP, | 27 TCP, |
25 }; | 28 }; |
26 | 29 |
27 typedef base::Callback<void(scoped_ptr<FirewallHole>)> OpenCallback; | 30 typedef base::Callback<void(scoped_ptr<FirewallHole>)> OpenCallback; |
28 | 31 |
| 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 |
29 // Opens a port on the system firewall for the given network interface (or all | 40 // Opens a port on the system firewall for the given network interface (or all |
30 // interfaces if |interface| is ""). The hole will be closed when the object | 41 // interfaces if |interface| is ""). The hole will be closed when the object |
31 // provided to the callback is destroyed. | 42 // provided to the callback is destroyed. |
32 static void Open(PortType type, | 43 static void Open(PortType type, |
33 uint16_t port, | 44 uint16_t port, |
34 const std::string& interface, | 45 const std::string& interface, |
35 const OpenCallback& callback); | 46 const OpenCallback& callback); |
36 | 47 |
37 ~FirewallHole(); | 48 ~FirewallHole(); |
38 | 49 |
39 private: | 50 private: |
40 static void RequestPortAccess(PortType type, | 51 static void RequestPortAccess(PortType type, |
41 uint16_t port, | 52 uint16_t port, |
42 const std::string& interface, | 53 const std::string& interface, |
43 dbus::ScopedFileDescriptor lifeline_local, | 54 ScopedFileDescriptor lifeline_local, |
44 dbus::ScopedFileDescriptor lifeline_remote, | 55 ScopedFileDescriptor lifeline_remote, |
45 const OpenCallback& callback); | 56 const OpenCallback& callback); |
46 | 57 |
47 static void PortAccessGranted(PortType type, | 58 static void PortAccessGranted(PortType type, |
48 uint16_t port, | 59 uint16_t port, |
49 const std::string& interface, | 60 const std::string& interface, |
50 dbus::ScopedFileDescriptor lifeline_fd, | 61 ScopedFileDescriptor lifeline_fd, |
51 const FirewallHole::OpenCallback& callback, | 62 const FirewallHole::OpenCallback& callback, |
52 bool success); | 63 bool success); |
53 | 64 |
54 FirewallHole(PortType type, | 65 FirewallHole(PortType type, |
55 uint16_t port, | 66 uint16_t port, |
56 const std::string& interface, | 67 const std::string& interface, |
57 dbus::ScopedFileDescriptor lifeline_fd); | 68 ScopedFileDescriptor lifeline_fd); |
58 | 69 |
59 const PortType type_; | 70 const PortType type_; |
60 const uint16_t port_; | 71 const uint16_t port_; |
61 const std::string interface_; | 72 const std::string interface_; |
62 | 73 |
63 // A file descriptor used by firewalld to track the lifetime of this process. | 74 // A file descriptor used by firewalld to track the lifetime of this process. |
64 dbus::ScopedFileDescriptor lifeline_fd_; | 75 ScopedFileDescriptor lifeline_fd_; |
65 }; | 76 }; |
66 | 77 |
67 } // namespace chromeos | 78 } // namespace chromeos |
68 | 79 |
69 #endif // CHROMEOS_NETWORK_FIREWALL_HOLE_H_ | 80 #endif // CHROMEOS_NETWORK_FIREWALL_HOLE_H_ |
OLD | NEW |