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

Side by Side Diff: net/socket/tcp_client_socket_libevent.cc

Issue 10803027: Fix TCPClientSocket::GetLocalAddress() to work when socket is not connected. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « net/socket/stream_socket.h ('k') | net/socket/tcp_client_socket_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "net/socket/tcp_client_socket.h" 5 #include "net/socket/tcp_client_socket.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <netdb.h> 9 #include <netdb.h>
10 #include <sys/socket.h> 10 #include <sys/socket.h>
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 if (current_address_index_ >= 0 || bind_address_.get()) { 173 if (current_address_index_ >= 0 || bind_address_.get()) {
174 // Cannot bind the socket if we are already bound connected or 174 // Cannot bind the socket if we are already bound connected or
175 // connecting. 175 // connecting.
176 return ERR_UNEXPECTED; 176 return ERR_UNEXPECTED;
177 } 177 }
178 178
179 SockaddrStorage storage; 179 SockaddrStorage storage;
180 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) 180 if (!address.ToSockAddr(storage.addr, &storage.addr_len))
181 return ERR_INVALID_ARGUMENT; 181 return ERR_INVALID_ARGUMENT;
182 182
183 // Create |bound_socket_| and try to bound it to |address|. 183 // Create |bound_socket_| and try to bind it to |address|.
184 int error = CreateSocket(address.GetFamily(), &bound_socket_); 184 int error = CreateSocket(address.GetFamily(), &bound_socket_);
185 if (error) 185 if (error)
186 return MapSystemError(error); 186 return MapSystemError(error);
187 187
188 if (HANDLE_EINTR(bind(bound_socket_, storage.addr, storage.addr_len))) { 188 if (HANDLE_EINTR(bind(bound_socket_, storage.addr, storage.addr_len))) {
189 error = errno; 189 error = errno;
190 if (HANDLE_EINTR(close(bound_socket_)) < 0) 190 if (HANDLE_EINTR(close(bound_socket_)) < 0)
191 PLOG(ERROR) << "close"; 191 PLOG(ERROR) << "close";
192 bound_socket_ = kInvalidSocket; 192 bound_socket_ = kInvalidSocket;
193 return MapSystemError(error); 193 return MapSystemError(error);
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 356
357 // Otherwise there is nothing to fall back to, so give up. 357 // Otherwise there is nothing to fall back to, so give up.
358 return result; 358 return result;
359 } 359 }
360 360
361 void TCPClientSocketLibevent::Disconnect() { 361 void TCPClientSocketLibevent::Disconnect() {
362 DCHECK(CalledOnValidThread()); 362 DCHECK(CalledOnValidThread());
363 363
364 DoDisconnect(); 364 DoDisconnect();
365 current_address_index_ = -1; 365 current_address_index_ = -1;
366 bind_address_.reset();
366 } 367 }
367 368
368 void TCPClientSocketLibevent::DoDisconnect() { 369 void TCPClientSocketLibevent::DoDisconnect() {
369 if (socket_ == kInvalidSocket) 370 if (socket_ == kInvalidSocket)
370 return; 371 return;
371 372
372 bool ok = read_socket_watcher_.StopWatchingFileDescriptor(); 373 bool ok = read_socket_watcher_.StopWatchingFileDescriptor();
373 DCHECK(ok); 374 DCHECK(ok);
374 ok = write_socket_watcher_.StopWatchingFileDescriptor(); 375 ok = write_socket_watcher_.StopWatchingFileDescriptor();
375 DCHECK(ok); 376 DCHECK(ok);
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after
711 DCHECK(address); 712 DCHECK(address);
712 if (!IsConnected()) 713 if (!IsConnected())
713 return ERR_SOCKET_NOT_CONNECTED; 714 return ERR_SOCKET_NOT_CONNECTED;
714 *address = addresses_[current_address_index_]; 715 *address = addresses_[current_address_index_];
715 return OK; 716 return OK;
716 } 717 }
717 718
718 int TCPClientSocketLibevent::GetLocalAddress(IPEndPoint* address) const { 719 int TCPClientSocketLibevent::GetLocalAddress(IPEndPoint* address) const {
719 DCHECK(CalledOnValidThread()); 720 DCHECK(CalledOnValidThread());
720 DCHECK(address); 721 DCHECK(address);
721 if (!IsConnected()) 722 if (socket_ == kInvalidSocket) {
723 if (bind_address_.get()) {
724 *address = *bind_address_;
725 return OK;
726 }
722 return ERR_SOCKET_NOT_CONNECTED; 727 return ERR_SOCKET_NOT_CONNECTED;
728 }
723 729
724 SockaddrStorage storage; 730 SockaddrStorage storage;
725 if (getsockname(socket_, storage.addr, &storage.addr_len)) 731 if (getsockname(socket_, storage.addr, &storage.addr_len))
726 return MapSystemError(errno); 732 return MapSystemError(errno);
727 if (!address->FromSockAddr(storage.addr, storage.addr_len)) 733 if (!address->FromSockAddr(storage.addr, storage.addr_len))
728 return ERR_FAILED; 734 return ERR_FAILED;
729 735
730 return OK; 736 return OK;
731 } 737 }
732 738
(...skipping 23 matching lines...) Expand all
756 762
757 base::TimeDelta TCPClientSocketLibevent::GetConnectTimeMicros() const { 763 base::TimeDelta TCPClientSocketLibevent::GetConnectTimeMicros() const {
758 return connect_time_micros_; 764 return connect_time_micros_;
759 } 765 }
760 766
761 NextProto TCPClientSocketLibevent::GetNegotiatedProtocol() const { 767 NextProto TCPClientSocketLibevent::GetNegotiatedProtocol() const {
762 return kProtoUnknown; 768 return kProtoUnknown;
763 } 769 }
764 770
765 } // namespace net 771 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/stream_socket.h ('k') | net/socket/tcp_client_socket_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698