Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(218)

Side by Side Diff: webrtc/base/physicalsocketserver.cc

Issue 2646863005: Fixing logic for using android_setsocknetwork() with bind(). (Closed)
Patch Set: comment formatting Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/networkmonitor.h ('k') | webrtc/sdk/android/src/jni/androidnetworkmonitor_jni.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 #include "webrtc/base/physicalsocketserver.h" 10 #include "webrtc/base/physicalsocketserver.h"
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 if (result >= 0) { 182 if (result >= 0) {
183 SocketAddressFromSockAddrStorage(addr_storage, &address); 183 SocketAddressFromSockAddrStorage(addr_storage, &address);
184 } else { 184 } else {
185 LOG(LS_WARNING) << "GetRemoteAddress: unable to get remote addr, socket=" 185 LOG(LS_WARNING) << "GetRemoteAddress: unable to get remote addr, socket="
186 << s_; 186 << s_;
187 } 187 }
188 return address; 188 return address;
189 } 189 }
190 190
191 int PhysicalSocket::Bind(const SocketAddress& bind_addr) { 191 int PhysicalSocket::Bind(const SocketAddress& bind_addr) {
192 SocketAddress copied_bind_addr = bind_addr;
193 // If a network binder is available, use it to bind a socket to an interface
194 // instead of bind(), since this is more reliable on an OS with a weak host
195 // model.
196 if (ss_->network_binder()) {
197 NetworkBindingResult result =
198 ss_->network_binder()->BindSocketToNetwork(s_, bind_addr.ipaddr());
199 if (result == NetworkBindingResult::SUCCESS) {
200 // Since the network binder handled binding the socket to the desired
201 // network interface, we don't need to (and shouldn't) include an IP in
202 // the bind() call; bind() just needs to assign a port.
203 copied_bind_addr.SetIP(GetAnyIP(copied_bind_addr.ipaddr().family()));
204 } else if (result == NetworkBindingResult::NOT_IMPLEMENTED) {
205 LOG(LS_INFO) << "Can't bind socket to network because "
206 "network binding is not implemented for this OS.";
207 } else {
208 LOG(LS_WARNING) << "Binding socket to network address "
209 << bind_addr.ipaddr().ToString()
210 << " failed; result: " << static_cast<int>(result);
211 // If a network binding was attempted and failed, we should stop here and
212 // not try to use the socket. Otherwise, we may end up sending packets
213 // with an invalid source address.
214 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7026
215 return -1;
pthatcher1 2017/02/10 18:12:19 Should we return different errors based on the res
Taylor Brandstetter 2017/02/10 18:57:52 Bind either returns -1 or 0, and I wasn't planning
216 }
217 }
192 sockaddr_storage addr_storage; 218 sockaddr_storage addr_storage;
193 size_t len = bind_addr.ToSockAddrStorage(&addr_storage); 219 size_t len = copied_bind_addr.ToSockAddrStorage(&addr_storage);
194 sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage); 220 sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
195 int err = ::bind(s_, addr, static_cast<int>(len)); 221 int err = ::bind(s_, addr, static_cast<int>(len));
196 UpdateLastError(); 222 UpdateLastError();
197 #if !defined(NDEBUG) 223 #if !defined(NDEBUG)
198 if (0 == err) { 224 if (0 == err) {
199 dbg_addr_ = "Bound @ "; 225 dbg_addr_ = "Bound @ ";
200 dbg_addr_.append(GetLocalAddress().ToString()); 226 dbg_addr_.append(GetLocalAddress().ToString());
201 } 227 }
202 #endif 228 #endif
203 if (ss_->network_binder()) {
204 int result =
205 ss_->network_binder()->BindSocketToNetwork(s_, bind_addr.ipaddr());
206 if (result < 0) {
207 LOG(LS_INFO) << "Binding socket to network address "
208 << bind_addr.ipaddr().ToString() << " result " << result;
209 }
210 }
211 return err; 229 return err;
212 } 230 }
213 231
214 int PhysicalSocket::Connect(const SocketAddress& addr) { 232 int PhysicalSocket::Connect(const SocketAddress& addr) {
215 // TODO(pthatcher): Implicit creation is required to reconnect... 233 // TODO(pthatcher): Implicit creation is required to reconnect...
216 // ...but should we make it more explicit? 234 // ...but should we make it more explicit?
217 if (state_ != CS_CLOSED) { 235 if (state_ != CS_CLOSED) {
218 SetError(EALREADY); 236 SetError(EALREADY);
219 return SOCKET_ERROR; 237 return SOCKET_ERROR;
220 } 238 }
(...skipping 1376 matching lines...) Expand 10 before | Expand all | Expand 10 after
1597 break; 1615 break;
1598 } 1616 }
1599 } 1617 }
1600 1618
1601 // Done 1619 // Done
1602 return true; 1620 return true;
1603 } 1621 }
1604 #endif // WEBRTC_WIN 1622 #endif // WEBRTC_WIN
1605 1623
1606 } // namespace rtc 1624 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/networkmonitor.h ('k') | webrtc/sdk/android/src/jni/androidnetworkmonitor_jni.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698