| 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 dbus::ScopedFileDescriptor lifeline_fd, | 53 FirewallHole::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 |
| 64 // static | 71 // static |
| 65 void FirewallHole::Open(PortType type, | 72 void FirewallHole::Open(PortType type, |
| 66 uint16_t port, | 73 uint16_t port, |
| 67 const std::string& interface, | 74 const std::string& interface, |
| 68 const OpenCallback& callback) { | 75 const OpenCallback& callback) { |
| 69 dbus::ScopedFileDescriptor lifeline_local(new dbus::FileDescriptor()); | 76 ScopedFileDescriptor lifeline_local(new dbus::FileDescriptor()); |
| 70 dbus::ScopedFileDescriptor lifeline_remote(new dbus::FileDescriptor()); | 77 ScopedFileDescriptor lifeline_remote(new dbus::FileDescriptor()); |
| 71 | 78 |
| 72 // This closure shares pointers with the one below. PostTaskAndReply | 79 // This closure shares pointers with the one below. PostTaskAndReply |
| 73 // guarantees that it will always be deleted first. | 80 // guarantees that it will always be deleted first. |
| 74 base::Closure create_lifeline_closure = base::Bind( | 81 base::Closure create_lifeline_closure = base::Bind( |
| 75 &CreateValidLifeline, lifeline_local.get(), lifeline_remote.get()); | 82 &CreateValidLifeline, lifeline_local.get(), lifeline_remote.get()); |
| 76 | 83 |
| 77 base::WorkerPool::PostTaskAndReply( | 84 base::WorkerPool::PostTaskAndReply( |
| 78 FROM_HERE, create_lifeline_closure, | 85 FROM_HERE, create_lifeline_closure, |
| 79 base::Bind(&FirewallHole::RequestPortAccess, type, port, interface, | 86 base::Bind(&FirewallHole::RequestPortAccess, type, port, interface, |
| 80 base::Passed(&lifeline_local), base::Passed(&lifeline_remote), | 87 base::Passed(&lifeline_local), base::Passed(&lifeline_remote), |
| (...skipping 14 matching lines...) Expand all Loading... |
| 95 return; | 102 return; |
| 96 case PortType::UDP: | 103 case PortType::UDP: |
| 97 client->ReleaseUdpPort(port_, interface_, port_released_closure); | 104 client->ReleaseUdpPort(port_, interface_, port_released_closure); |
| 98 return; | 105 return; |
| 99 } | 106 } |
| 100 } | 107 } |
| 101 | 108 |
| 102 void FirewallHole::RequestPortAccess(PortType type, | 109 void FirewallHole::RequestPortAccess(PortType type, |
| 103 uint16_t port, | 110 uint16_t port, |
| 104 const std::string& interface, | 111 const std::string& interface, |
| 105 dbus::ScopedFileDescriptor lifeline_local, | 112 ScopedFileDescriptor lifeline_local, |
| 106 dbus::ScopedFileDescriptor lifeline_remote, | 113 ScopedFileDescriptor lifeline_remote, |
| 107 const OpenCallback& callback) { | 114 const OpenCallback& callback) { |
| 108 if (!lifeline_local->is_valid() || !lifeline_remote->is_valid()) { | 115 if (!lifeline_local->is_valid() || !lifeline_remote->is_valid()) { |
| 109 callback.Run(nullptr); | 116 callback.Run(nullptr); |
| 110 return; | 117 return; |
| 111 } | 118 } |
| 112 | 119 |
| 113 base::Callback<void(bool)> access_granted_closure = | 120 base::Callback<void(bool)> access_granted_closure = |
| 114 base::Bind(&FirewallHole::PortAccessGranted, type, port, interface, | 121 base::Bind(&FirewallHole::PortAccessGranted, type, port, interface, |
| 115 base::Passed(&lifeline_local), callback); | 122 base::Passed(&lifeline_local), callback); |
| 116 | 123 |
| 117 PermissionBrokerClient* client = | 124 PermissionBrokerClient* client = |
| 118 DBusThreadManager::Get()->GetPermissionBrokerClient(); | 125 DBusThreadManager::Get()->GetPermissionBrokerClient(); |
| 119 DCHECK(client) << "Could not get permission broker client."; | 126 DCHECK(client) << "Could not get permission broker client."; |
| 120 | 127 |
| 121 switch (type) { | 128 switch (type) { |
| 122 case PortType::TCP: | 129 case PortType::TCP: |
| 123 client->RequestTcpPortAccess(port, interface, *lifeline_remote, | 130 client->RequestTcpPortAccess(port, interface, *lifeline_remote, |
| 124 access_granted_closure); | 131 access_granted_closure); |
| 125 return; | 132 return; |
| 126 case PortType::UDP: | 133 case PortType::UDP: |
| 127 client->RequestUdpPortAccess(port, interface, *lifeline_remote, | 134 client->RequestUdpPortAccess(port, interface, *lifeline_remote, |
| 128 access_granted_closure); | 135 access_granted_closure); |
| 129 return; | 136 return; |
| 130 } | 137 } |
| 131 } | 138 } |
| 132 | 139 |
| 133 void FirewallHole::PortAccessGranted(PortType type, | 140 void FirewallHole::PortAccessGranted(PortType type, |
| 134 uint16_t port, | 141 uint16_t port, |
| 135 const std::string& interface, | 142 const std::string& interface, |
| 136 dbus::ScopedFileDescriptor lifeline_fd, | 143 ScopedFileDescriptor lifeline_fd, |
| 137 const FirewallHole::OpenCallback& callback, | 144 const FirewallHole::OpenCallback& callback, |
| 138 bool success) { | 145 bool success) { |
| 139 if (success) { | 146 if (success) { |
| 140 callback.Run(make_scoped_ptr( | 147 callback.Run(make_scoped_ptr( |
| 141 new FirewallHole(type, port, interface, lifeline_fd.Pass()))); | 148 new FirewallHole(type, port, interface, lifeline_fd.Pass()))); |
| 142 } else { | 149 } else { |
| 143 callback.Run(nullptr); | 150 callback.Run(nullptr); |
| 144 } | 151 } |
| 145 } | 152 } |
| 146 | 153 |
| 147 FirewallHole::FirewallHole(PortType type, | 154 FirewallHole::FirewallHole(PortType type, |
| 148 uint16_t port, | 155 uint16_t port, |
| 149 const std::string& interface, | 156 const std::string& interface, |
| 150 dbus::ScopedFileDescriptor lifeline_fd) | 157 ScopedFileDescriptor lifeline_fd) |
| 151 : type_(type), | 158 : type_(type), |
| 152 port_(port), | 159 port_(port), |
| 153 interface_(interface), | 160 interface_(interface), |
| 154 lifeline_fd_(lifeline_fd.Pass()) { | 161 lifeline_fd_(lifeline_fd.Pass()) { |
| 155 } | 162 } |
| 156 | 163 |
| 157 } // namespace chromeos | 164 } // namespace chromeos |
| OLD | NEW |