| 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 #include "chromeos/network/firewall_hole.h" | 5 #include "chromeos/network/firewall_hole.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <unistd.h> | 8 #include <unistd.h> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 case FirewallHole::PortType::UDP: | 43 case FirewallHole::PortType::UDP: |
| 44 return "UDP"; | 44 return "UDP"; |
| 45 } | 45 } |
| 46 NOTREACHED(); | 46 NOTREACHED(); |
| 47 return nullptr; | 47 return nullptr; |
| 48 } | 48 } |
| 49 | 49 |
| 50 void PortReleased(FirewallHole::PortType type, | 50 void PortReleased(FirewallHole::PortType type, |
| 51 uint16_t port, | 51 uint16_t port, |
| 52 const std::string& interface, | 52 const std::string& interface, |
| 53 FirewallHole::ScopedFileDescriptor lifeline_fd, | 53 dbus::ScopedFileDescriptor lifeline_fd, |
| 54 bool success) { | 54 bool success) { |
| 55 if (!success) { | 55 if (!success) { |
| 56 LOG(WARNING) << "Failed to release firewall hole for " | 56 LOG(WARNING) << "Failed to release firewall hole for " |
| 57 << PortTypeToString(type) << " port " << port << " on " | 57 << PortTypeToString(type) << " port " << port << " on " |
| 58 << interface << "."; | 58 << interface << "."; |
| 59 } | 59 } |
| 60 } | 60 } |
| 61 | 61 |
| 62 } // namespace | 62 } // namespace |
| 63 | 63 |
| 64 void CHROMEOS_EXPORT FirewallHole::FileDescriptorDeleter::operator()( | |
| 65 dbus::FileDescriptor* fd) { | |
| 66 base::WorkerPool::PostTask( | |
| 67 FROM_HERE, base::Bind(&base::DeletePointer<dbus::FileDescriptor>, fd), | |
| 68 false); | |
| 69 } | |
| 70 | |
| 71 // static | 64 // static |
| 72 void FirewallHole::Open(PortType type, | 65 void FirewallHole::Open(PortType type, |
| 73 uint16_t port, | 66 uint16_t port, |
| 74 const std::string& interface, | 67 const std::string& interface, |
| 75 const OpenCallback& callback) { | 68 const OpenCallback& callback) { |
| 76 ScopedFileDescriptor lifeline_local(new dbus::FileDescriptor()); | 69 dbus::ScopedFileDescriptor lifeline_local(new dbus::FileDescriptor()); |
| 77 ScopedFileDescriptor lifeline_remote(new dbus::FileDescriptor()); | 70 dbus::ScopedFileDescriptor lifeline_remote(new dbus::FileDescriptor()); |
| 78 | 71 |
| 79 // This closure shares pointers with the one below. PostTaskAndReply | 72 // This closure shares pointers with the one below. PostTaskAndReply |
| 80 // guarantees that it will always be deleted first. | 73 // guarantees that it will always be deleted first. |
| 81 base::Closure create_lifeline_closure = base::Bind( | 74 base::Closure create_lifeline_closure = base::Bind( |
| 82 &CreateValidLifeline, lifeline_local.get(), lifeline_remote.get()); | 75 &CreateValidLifeline, lifeline_local.get(), lifeline_remote.get()); |
| 83 | 76 |
| 84 base::WorkerPool::PostTaskAndReply( | 77 base::WorkerPool::PostTaskAndReply( |
| 85 FROM_HERE, create_lifeline_closure, | 78 FROM_HERE, create_lifeline_closure, |
| 86 base::Bind(&FirewallHole::RequestPortAccess, type, port, interface, | 79 base::Bind(&FirewallHole::RequestPortAccess, type, port, interface, |
| 87 base::Passed(&lifeline_local), base::Passed(&lifeline_remote), | 80 base::Passed(&lifeline_local), base::Passed(&lifeline_remote), |
| (...skipping 14 matching lines...) Expand all Loading... |
| 102 return; | 95 return; |
| 103 case PortType::UDP: | 96 case PortType::UDP: |
| 104 client->ReleaseUdpPort(port_, interface_, port_released_closure); | 97 client->ReleaseUdpPort(port_, interface_, port_released_closure); |
| 105 return; | 98 return; |
| 106 } | 99 } |
| 107 } | 100 } |
| 108 | 101 |
| 109 void FirewallHole::RequestPortAccess(PortType type, | 102 void FirewallHole::RequestPortAccess(PortType type, |
| 110 uint16_t port, | 103 uint16_t port, |
| 111 const std::string& interface, | 104 const std::string& interface, |
| 112 ScopedFileDescriptor lifeline_local, | 105 dbus::ScopedFileDescriptor lifeline_local, |
| 113 ScopedFileDescriptor lifeline_remote, | 106 dbus::ScopedFileDescriptor lifeline_remote, |
| 114 const OpenCallback& callback) { | 107 const OpenCallback& callback) { |
| 115 if (!lifeline_local->is_valid() || !lifeline_remote->is_valid()) { | 108 if (!lifeline_local->is_valid() || !lifeline_remote->is_valid()) { |
| 116 callback.Run(nullptr); | 109 callback.Run(nullptr); |
| 117 return; | 110 return; |
| 118 } | 111 } |
| 119 | 112 |
| 120 base::Callback<void(bool)> access_granted_closure = | 113 base::Callback<void(bool)> access_granted_closure = |
| 121 base::Bind(&FirewallHole::PortAccessGranted, type, port, interface, | 114 base::Bind(&FirewallHole::PortAccessGranted, type, port, interface, |
| 122 base::Passed(&lifeline_local), callback); | 115 base::Passed(&lifeline_local), callback); |
| 123 | 116 |
| 124 PermissionBrokerClient* client = | 117 PermissionBrokerClient* client = |
| 125 DBusThreadManager::Get()->GetPermissionBrokerClient(); | 118 DBusThreadManager::Get()->GetPermissionBrokerClient(); |
| 126 DCHECK(client) << "Could not get permission broker client."; | 119 DCHECK(client) << "Could not get permission broker client."; |
| 127 | 120 |
| 128 switch (type) { | 121 switch (type) { |
| 129 case PortType::TCP: | 122 case PortType::TCP: |
| 130 client->RequestTcpPortAccess(port, interface, *lifeline_remote, | 123 client->RequestTcpPortAccess(port, interface, *lifeline_remote, |
| 131 access_granted_closure); | 124 access_granted_closure); |
| 132 return; | 125 return; |
| 133 case PortType::UDP: | 126 case PortType::UDP: |
| 134 client->RequestUdpPortAccess(port, interface, *lifeline_remote, | 127 client->RequestUdpPortAccess(port, interface, *lifeline_remote, |
| 135 access_granted_closure); | 128 access_granted_closure); |
| 136 return; | 129 return; |
| 137 } | 130 } |
| 138 } | 131 } |
| 139 | 132 |
| 140 void FirewallHole::PortAccessGranted(PortType type, | 133 void FirewallHole::PortAccessGranted(PortType type, |
| 141 uint16_t port, | 134 uint16_t port, |
| 142 const std::string& interface, | 135 const std::string& interface, |
| 143 ScopedFileDescriptor lifeline_fd, | 136 dbus::ScopedFileDescriptor lifeline_fd, |
| 144 const FirewallHole::OpenCallback& callback, | 137 const FirewallHole::OpenCallback& callback, |
| 145 bool success) { | 138 bool success) { |
| 146 if (success) { | 139 if (success) { |
| 147 callback.Run(make_scoped_ptr( | 140 callback.Run(make_scoped_ptr( |
| 148 new FirewallHole(type, port, interface, lifeline_fd.Pass()))); | 141 new FirewallHole(type, port, interface, lifeline_fd.Pass()))); |
| 149 } else { | 142 } else { |
| 150 callback.Run(nullptr); | 143 callback.Run(nullptr); |
| 151 } | 144 } |
| 152 } | 145 } |
| 153 | 146 |
| 154 FirewallHole::FirewallHole(PortType type, | 147 FirewallHole::FirewallHole(PortType type, |
| 155 uint16_t port, | 148 uint16_t port, |
| 156 const std::string& interface, | 149 const std::string& interface, |
| 157 ScopedFileDescriptor lifeline_fd) | 150 dbus::ScopedFileDescriptor lifeline_fd) |
| 158 : type_(type), | 151 : type_(type), |
| 159 port_(port), | 152 port_(port), |
| 160 interface_(interface), | 153 interface_(interface), |
| 161 lifeline_fd_(lifeline_fd.Pass()) { | 154 lifeline_fd_(lifeline_fd.Pass()) { |
| 162 } | 155 } |
| 163 | 156 |
| 164 } // namespace chromeos | 157 } // namespace chromeos |
| OLD | NEW |