diff options
author | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-09 19:26:04 +0000 |
---|---|---|
committer | sergeyu@chromium.org <sergeyu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-09 19:26:04 +0000 |
commit | 43d4a026d2dfd4301e4cc25fa3f8f07c7e20278a (patch) | |
tree | 347b3162e50b452a805d7707d7f8eddbe1d71005 | |
parent | a8a6012b1346f64cec04ce8f4ca592cdae77ec2d (diff) | |
download | chromium_src-43d4a026d2dfd4301e4cc25fa3f8f07c7e20278a.zip chromium_src-43d4a026d2dfd4301e4cc25fa3f8f07c7e20278a.tar.gz chromium_src-43d4a026d2dfd4301e4cc25fa3f8f07c7e20278a.tar.bz2 |
Use IPEndPoint for UDP sockets.
BUG=None
TEST=Unittest
Review URL: http://codereview.chromium.org/6650018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@77498 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | net/base/ip_endpoint.cc | 17 | ||||
-rw-r--r-- | net/base/ip_endpoint.h | 5 | ||||
-rw-r--r-- | net/base/ip_endpoint_unittest.cc | 8 | ||||
-rw-r--r-- | net/udp/datagram_client_socket.h | 4 | ||||
-rw-r--r-- | net/udp/datagram_server_socket.h | 10 | ||||
-rw-r--r-- | net/udp/datagram_socket.h | 6 | ||||
-rw-r--r-- | net/udp/udp_client_socket.cc | 6 | ||||
-rw-r--r-- | net/udp/udp_client_socket.h | 6 | ||||
-rw-r--r-- | net/udp/udp_server_socket.cc | 16 | ||||
-rw-r--r-- | net/udp/udp_server_socket.h | 14 | ||||
-rw-r--r-- | net/udp/udp_socket_libevent.cc | 110 | ||||
-rw-r--r-- | net/udp/udp_socket_libevent.h | 34 | ||||
-rw-r--r-- | net/udp/udp_socket_unittest.cc | 83 |
13 files changed, 160 insertions, 159 deletions
diff --git a/net/base/ip_endpoint.cc b/net/base/ip_endpoint.cc index 1c03273..626d12e 100644 --- a/net/base/ip_endpoint.cc +++ b/net/base/ip_endpoint.cc @@ -29,7 +29,22 @@ IPEndPoint::IPEndPoint(const IPEndPoint& endpoint) { port_ = endpoint.port_; } -bool IPEndPoint::ToSockaddr(struct sockaddr* address, +int IPEndPoint::GetFamily() const { + switch (address_.size()) { + case kIPv4AddressSize: { + return AF_INET; + } + case kIPv6AddressSize: { + return AF_INET6; + } + default: { + NOTREACHED() << "Bad IP address"; + return AF_INET; + } + } +} + +bool IPEndPoint::ToSockAddr(struct sockaddr* address, size_t* address_length) const { DCHECK(address); DCHECK(address_length); diff --git a/net/base/ip_endpoint.h b/net/base/ip_endpoint.h index 2a60e11..af5de87 100644 --- a/net/base/ip_endpoint.h +++ b/net/base/ip_endpoint.h @@ -26,6 +26,9 @@ class IPEndPoint { const IPAddressNumber& address() const { return address_; } int port() const { return port_; } + // Returns AF_INET or AF_INET6 depending on the type of the address. + int GetFamily() const; + // Convert to a provided sockaddr struct. // |address| is the sockaddr to copy into. Should be at least // sizeof(struct sockaddr_storage) bytes. @@ -33,7 +36,7 @@ class IPEndPoint { // size of data in |address| available. On output, it is the size of // the address that was copied into |address|. // Returns true on success, false on failure. - bool ToSockaddr(struct sockaddr* address, size_t* address_length) const; + bool ToSockAddr(struct sockaddr* address, size_t* address_length) const; // Convert from a sockaddr struct. // |address| is the address. diff --git a/net/base/ip_endpoint_unittest.cc b/net/base/ip_endpoint_unittest.cc index b9fe48b..ae09ad3 100644 --- a/net/base/ip_endpoint_unittest.cc +++ b/net/base/ip_endpoint_unittest.cc @@ -71,7 +71,7 @@ TEST_F(IPEndPointTest, Copy) { } } -TEST_F(IPEndPointTest, ToFromSockaddr) { +TEST_F(IPEndPointTest, ToFromSockAddr) { for (int index = 0; index < test_count; ++index) { IPEndPoint ip_endpoint(tests[index].ip_address, index); @@ -79,7 +79,7 @@ TEST_F(IPEndPointTest, ToFromSockaddr) { struct sockaddr_storage addr; size_t addr_len = sizeof(addr); struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr); - EXPECT_TRUE(ip_endpoint.ToSockaddr(sockaddr, &addr_len)); + EXPECT_TRUE(ip_endpoint.ToSockAddr(sockaddr, &addr_len)); // Basic verification. size_t expected_size = tests[index].ipv6 ? @@ -95,14 +95,14 @@ TEST_F(IPEndPointTest, ToFromSockaddr) { } } -TEST_F(IPEndPointTest, ToSockaddrBufTooSmall) { +TEST_F(IPEndPointTest, ToSockAddrBufTooSmall) { for (int index = 0; index < test_count; ++index) { IPEndPoint ip_endpoint(tests[index].ip_address, index); struct sockaddr_storage addr; size_t addr_len = index; // size is too small! struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr); - EXPECT_FALSE(ip_endpoint.ToSockaddr(sockaddr, &addr_len)); + EXPECT_FALSE(ip_endpoint.ToSockAddr(sockaddr, &addr_len)); } } diff --git a/net/udp/datagram_client_socket.h b/net/udp/datagram_client_socket.h index e961952..da0f48c 100644 --- a/net/udp/datagram_client_socket.h +++ b/net/udp/datagram_client_socket.h @@ -11,7 +11,7 @@ namespace net { -class AddressList; +class IPEndPoint; class DatagramClientSocket : public DatagramSocket, public Socket { public: @@ -19,7 +19,7 @@ class DatagramClientSocket : public DatagramSocket, public Socket { // Initialize this socket as a client socket to server at |address|. // Returns a network error code. - virtual int Connect(const AddressList& address) = 0; + virtual int Connect(const IPEndPoint& address) = 0; }; } // namespace net diff --git a/net/udp/datagram_server_socket.h b/net/udp/datagram_server_socket.h index d4b0244..30286e5 100644 --- a/net/udp/datagram_server_socket.h +++ b/net/udp/datagram_server_socket.h @@ -13,7 +13,7 @@ namespace net { -class AddressList; +class IPEndPoint; class IOBuffer; // A UDP Socket. @@ -23,7 +23,7 @@ class DatagramServerSocket : public DatagramSocket { // Initialize this socket as a server socket listening at |address|. // Returns a network error code. - virtual int Listen(const AddressList& address) = 0; + virtual int Listen(const IPEndPoint& address) = 0; // Read from a socket and receive sender address information. // |buf| is the buffer to read data into. @@ -40,8 +40,7 @@ class DatagramServerSocket : public DatagramSocket { // and |address_length| alive until the callback is called. virtual int RecvFrom(IOBuffer* buf, int buf_len, - struct sockaddr* address, - socklen_t* address_length, + IPEndPoint* address, CompletionCallback* callback) = 0; // Send to a socket with a particular destination. @@ -55,8 +54,7 @@ class DatagramServerSocket : public DatagramSocket { // alive until the callback is called. virtual int SendTo(IOBuffer* buf, int buf_len, - const struct sockaddr* address, - socklen_t address_length, + const IPEndPoint& address, CompletionCallback* callback) = 0; }; diff --git a/net/udp/datagram_socket.h b/net/udp/datagram_socket.h index 0398f39..88a6bc1 100644 --- a/net/udp/datagram_socket.h +++ b/net/udp/datagram_socket.h @@ -8,7 +8,7 @@ namespace net { -class AddressList; +class IPEndPoint; // A datagram socket is an interface to a protocol which exchanges // datagrams, like UDP. @@ -20,11 +20,11 @@ class DatagramSocket { virtual void Close() = 0; // Copy the remote udp address into |address| and return a network error code. - virtual int GetPeerAddress(AddressList* address) const = 0; + virtual int GetPeerAddress(IPEndPoint* address) const = 0; // Copy the local udp address into |address| and return a network error code. // (similar to getsockname) - virtual int GetLocalAddress(AddressList* address) const = 0; + virtual int GetLocalAddress(IPEndPoint* address) const = 0; }; } // namespace net diff --git a/net/udp/udp_client_socket.cc b/net/udp/udp_client_socket.cc index cdbf756..912394b 100644 --- a/net/udp/udp_client_socket.cc +++ b/net/udp/udp_client_socket.cc @@ -17,7 +17,7 @@ UDPClientSocket::UDPClientSocket( UDPClientSocket::~UDPClientSocket() { } -int UDPClientSocket::Connect(const AddressList& address) { +int UDPClientSocket::Connect(const IPEndPoint& address) { return socket_.Connect(address); } @@ -37,11 +37,11 @@ void UDPClientSocket::Close() { socket_.Close(); } -int UDPClientSocket::GetPeerAddress(AddressList* address) const { +int UDPClientSocket::GetPeerAddress(IPEndPoint* address) const { return socket_.GetPeerAddress(address); } -int UDPClientSocket::GetLocalAddress(AddressList* address) const { +int UDPClientSocket::GetLocalAddress(IPEndPoint* address) const { return socket_.GetLocalAddress(address); } diff --git a/net/udp/udp_client_socket.h b/net/udp/udp_client_socket.h index 602d09b..e21478f 100644 --- a/net/udp/udp_client_socket.h +++ b/net/udp/udp_client_socket.h @@ -22,12 +22,12 @@ class UDPClientSocket : public DatagramClientSocket { virtual ~UDPClientSocket(); // Implement DatagramClientSocket: - virtual int Connect(const AddressList& address); + virtual int Connect(const IPEndPoint& address); virtual int Read(IOBuffer* buf, int buf_len, CompletionCallback* callback); virtual int Write(IOBuffer* buf, int buf_len, CompletionCallback* callback); virtual void Close(); - virtual int GetPeerAddress(AddressList* address) const; - virtual int GetLocalAddress(AddressList* address) const; + virtual int GetPeerAddress(IPEndPoint* address) const; + virtual int GetLocalAddress(IPEndPoint* address) const; virtual bool SetReceiveBufferSize(int32 size); virtual bool SetSendBufferSize(int32 size); diff --git a/net/udp/udp_server_socket.cc b/net/udp/udp_server_socket.cc index 4b1c175..acab7e3 100644 --- a/net/udp/udp_server_socket.cc +++ b/net/udp/udp_server_socket.cc @@ -14,35 +14,33 @@ UDPServerSocket::UDPServerSocket(net::NetLog* net_log, UDPServerSocket::~UDPServerSocket() { } -int UDPServerSocket::Listen(const AddressList& address) { +int UDPServerSocket::Listen(const IPEndPoint& address) { return socket_.Bind(address); } int UDPServerSocket::RecvFrom(IOBuffer* buf, int buf_len, - struct sockaddr* address, - socklen_t* address_length, + IPEndPoint* address, CompletionCallback* callback) { - return socket_.RecvFrom(buf, buf_len, address, address_length, callback); + return socket_.RecvFrom(buf, buf_len, address, callback); } int UDPServerSocket::SendTo(IOBuffer* buf, int buf_len, - const struct sockaddr* address, - socklen_t address_length, + const IPEndPoint& address, CompletionCallback* callback) { - return socket_.SendTo(buf, buf_len, address, address_length, callback); + return socket_.SendTo(buf, buf_len, address, callback); } void UDPServerSocket::Close() { socket_.Close(); } -int UDPServerSocket::GetPeerAddress(AddressList* address) const { +int UDPServerSocket::GetPeerAddress(IPEndPoint* address) const { return socket_.GetPeerAddress(address); } -int UDPServerSocket::GetLocalAddress(AddressList* address) const { +int UDPServerSocket::GetLocalAddress(IPEndPoint* address) const { return socket_.GetLocalAddress(address); } diff --git a/net/udp/udp_server_socket.h b/net/udp/udp_server_socket.h index 6c152a7..4eae911 100644 --- a/net/udp/udp_server_socket.h +++ b/net/udp/udp_server_socket.h @@ -12,7 +12,7 @@ namespace net { -class AddressList; +class IPEndPoint; class BoundNetLog; // A client socket that uses UDP as the transport layer. @@ -23,20 +23,18 @@ class UDPServerSocket : public DatagramServerSocket { virtual ~UDPServerSocket(); // Implement DatagramServerSocket: - virtual int Listen(const AddressList& address); + virtual int Listen(const IPEndPoint& address); virtual int RecvFrom(IOBuffer* buf, int buf_len, - struct sockaddr* address, - socklen_t* address_length, + IPEndPoint* address, CompletionCallback* callback); virtual int SendTo(IOBuffer* buf, int buf_len, - const struct sockaddr* address, - socklen_t address_length, + const IPEndPoint& address, CompletionCallback* callback); virtual void Close(); - virtual int GetPeerAddress(AddressList* address) const; - virtual int GetLocalAddress(AddressList* address) const; + virtual int GetPeerAddress(IPEndPoint* address) const; + virtual int GetLocalAddress(IPEndPoint* address) const; private: UDPSocket socket_; diff --git a/net/udp/udp_socket_libevent.cc b/net/udp/udp_socket_libevent.cc index 8c0ad4f..7c75ce3 100644 --- a/net/udp/udp_socket_libevent.cc +++ b/net/udp/udp_socket_libevent.cc @@ -14,6 +14,7 @@ #include "base/message_loop.h" #include "base/metrics/stats_counters.h" #include "net/base/io_buffer.h" +#include "net/base/ip_endpoint.h" #include "net/base/net_errors.h" #include "net/base/net_log.h" #include "net/base/net_util.h" @@ -82,10 +83,7 @@ UDPSocketLibevent::UDPSocketLibevent(net::NetLog* net_log, write_watcher_(this), read_buf_len_(0), recv_from_address_(NULL), - recv_from_address_length_(NULL), write_buf_len_(0), - send_to_address_(NULL), - send_to_address_length_(0), read_callback_(NULL), write_callback_(NULL), net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { @@ -110,7 +108,7 @@ void UDPSocketLibevent::Close() { socket_ = kInvalidSocket; } -int UDPSocketLibevent::GetPeerAddress(AddressList* address) const { +int UDPSocketLibevent::GetPeerAddress(IPEndPoint* address) const { DCHECK(CalledOnValidThread()); DCHECK(address); if (!is_connected()) @@ -122,18 +120,17 @@ int UDPSocketLibevent::GetPeerAddress(AddressList* address) const { struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); if (getpeername(socket_, addr, &addr_len)) return MapPosixError(errno); - remote_address_.reset(AddressList::CreateAddressListFromSockaddr( - addr, - addr_len, - SOCK_DGRAM, - IPPROTO_UDP)); + scoped_ptr<IPEndPoint> address(new IPEndPoint()); + if (!address->FromSockAddr(addr, addr_len)) + return ERR_FAILED; + remote_address_.reset(address.release()); } - address->Copy(remote_address_->head(), false); + *address = *remote_address_; return OK; } -int UDPSocketLibevent::GetLocalAddress(AddressList* address) const { +int UDPSocketLibevent::GetLocalAddress(IPEndPoint* address) const { DCHECK(CalledOnValidThread()); DCHECK(address); if (!is_connected()) @@ -145,14 +142,13 @@ int UDPSocketLibevent::GetLocalAddress(AddressList* address) const { struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); if (getsockname(socket_, addr, &addr_len)) return MapPosixError(errno); - local_address_.reset(AddressList::CreateAddressListFromSockaddr( - addr, - addr_len, - SOCK_DGRAM, - IPPROTO_UDP)); + scoped_ptr<IPEndPoint> address(new IPEndPoint()); + if (!address->FromSockAddr(addr, addr_len)) + return ERR_FAILED; + local_address_.reset(address.release()); } - address->Copy(local_address_->head(), false); + *address = *local_address_; return OK; } @@ -185,25 +181,18 @@ int UDPSocketLibevent::Read(IOBuffer* buf, int UDPSocketLibevent::RecvFrom(IOBuffer* buf, int buf_len, - struct sockaddr* address, - socklen_t* address_length, + IPEndPoint* address, CompletionCallback* callback) { DCHECK(!recv_from_address_); - DCHECK(!recv_from_address_length_); recv_from_address_ = address; - recv_from_address_length_ = address_length; return Read(buf, buf_len, callback); } int UDPSocketLibevent::SendTo(IOBuffer* buf, int buf_len, - const struct sockaddr* address, - socklen_t address_length, + const IPEndPoint& address, CompletionCallback* callback) { - DCHECK(!send_to_address_); - DCHECK(!send_to_address_length_); - send_to_address_ = address; - send_to_address_length_ = address_length; + send_to_address_.reset(new IPEndPoint(address)); return Write(buf, buf_len, callback); } @@ -238,35 +227,45 @@ int UDPSocketLibevent::Write(IOBuffer* buf, return ERR_IO_PENDING; } -int UDPSocketLibevent::Connect(const AddressList& address) { +int UDPSocketLibevent::Connect(const IPEndPoint& address) { DCHECK(!is_connected()); DCHECK(!remote_address_.get()); - const struct addrinfo* ai = address.head(); - int rv = CreateSocket(ai); + int rv = CreateSocket(address); if (rv < 0) return MapPosixError(rv); - rv = HANDLE_EINTR(connect(socket_, ai->ai_addr, ai->ai_addrlen)); + struct sockaddr_storage addr_storage; + size_t addr_len = sizeof(addr_storage); + struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); + if (!address.ToSockAddr(addr, &addr_len)) + return ERR_FAILED; + + rv = HANDLE_EINTR(connect(socket_, addr, addr_len)); if (rv < 0) return MapPosixError(rv); - remote_address_.reset(new AddressList(address)); + remote_address_.reset(new IPEndPoint(address)); return rv; } -int UDPSocketLibevent::Bind(const AddressList& address) { +int UDPSocketLibevent::Bind(const IPEndPoint& address) { DCHECK(!is_connected()); DCHECK(!local_address_.get()); - const struct addrinfo* ai = address.head(); - int rv = CreateSocket(ai); + int rv = CreateSocket(address); if (rv < 0) return MapPosixError(rv); - rv = bind(socket_, ai->ai_addr, ai->ai_addrlen); + struct sockaddr_storage addr_storage; + size_t addr_len = sizeof(addr_storage); + struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); + if (!address.ToSockAddr(addr, &addr_len)) + return ERR_FAILED; + + rv = bind(socket_, addr, addr_len); if (rv < 0) return MapPosixError(rv); - local_address_.reset(new AddressList(address)); + local_address_.reset(new IPEndPoint(address)); return rv; } @@ -278,7 +277,6 @@ void UDPSocketLibevent::DoReadCallback(int rv) { CompletionCallback* c = read_callback_; read_callback_ = NULL; recv_from_address_ = NULL; - recv_from_address_length_ = NULL; c->Run(rv); } @@ -289,8 +287,7 @@ void UDPSocketLibevent::DoWriteCallback(int rv) { // since Run may result in Write being called, clear write_callback_ up front. CompletionCallback* c = write_callback_; write_callback_ = NULL; - send_to_address_ = NULL; - send_to_address_length_ = 0; + send_to_address_.reset(); c->Run(rv); } @@ -305,8 +302,8 @@ void UDPSocketLibevent::DidCompleteRead() { } } -int UDPSocketLibevent::CreateSocket(const addrinfo* ai) { - socket_ = socket(ai->ai_family, SOCK_DGRAM, 0); +int UDPSocketLibevent::CreateSocket(const IPEndPoint& address) { + socket_ = socket(address.GetFamily(), SOCK_DGRAM, 0); if (socket_ == kInvalidSocket) return errno; if (SetNonBlocking(socket_)) { @@ -337,30 +334,51 @@ void UDPSocketLibevent::DidCompleteWrite() { int UDPSocketLibevent::InternalRead() { int bytes_transferred; int flags = 0; + + struct sockaddr_storage addr_storage; + socklen_t addr_len = sizeof(addr_storage); + struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); + bytes_transferred = HANDLE_EINTR(recvfrom(socket_, read_buf_->data(), read_buf_len_, flags, - recv_from_address_, - recv_from_address_length_)); + addr, + &addr_len)); int result; if (bytes_transferred >= 0) { result = bytes_transferred; static base::StatsCounter read_bytes("udp.read_bytes"); read_bytes.Add(bytes_transferred); + if (recv_from_address_) { + if (!recv_from_address_->FromSockAddr(addr, addr_len)) + result = ERR_FAILED; + } } else { result = MapPosixError(errno); } return result; } int UDPSocketLibevent::InternalWrite(IOBuffer* buf, int buf_len) { + struct sockaddr_storage addr_storage; + size_t addr_len = sizeof(addr_storage); + struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); + + if (!send_to_address_.get()) { + addr = NULL; + addr_len = 0; + } else { + if (!send_to_address_->ToSockAddr(addr, &addr_len)) + return ERR_FAILED; + } + return HANDLE_EINTR(sendto(socket_, buf->data(), buf_len, 0, - send_to_address_, - send_to_address_length_)); + addr, + addr_len)); } } // namespace net diff --git a/net/udp/udp_socket_libevent.h b/net/udp/udp_socket_libevent.h index bea4887..ebe01b2 100644 --- a/net/udp/udp_socket_libevent.h +++ b/net/udp/udp_socket_libevent.h @@ -29,8 +29,8 @@ #include "base/ref_counted.h" #include "base/scoped_ptr.h" #include "base/threading/non_thread_safe.h" -#include "net/base/address_list.h" #include "net/base/completion_callback.h" +#include "net/base/ip_endpoint.h" #include "net/base/net_log.h" #include "net/socket/client_socket.h" #include "net/udp/datagram_socket.h" @@ -47,22 +47,22 @@ class UDPSocketLibevent : public base::NonThreadSafe { // Connect the socket to connect with a certain |address|. // Returns a net error code. - int Connect(const AddressList& address); + int Connect(const IPEndPoint& address); // Bind the address/port for this socket to |address|. This is generally // only used on a server. // Returns a net error code. - int Bind(const AddressList& address); + int Bind(const IPEndPoint& address); // Close the socket. void Close(); // Copy the remote udp address into |address| and return a network error code. - int GetPeerAddress(AddressList* address) const; + int GetPeerAddress(IPEndPoint* address) const; // Copy the local udp address into |address| and return a network error code. // (similar to getsockname) - int GetLocalAddress(AddressList* address) const; + int GetLocalAddress(IPEndPoint* address) const; // IO: // Multiple outstanding read requests are not supported. @@ -93,8 +93,7 @@ class UDPSocketLibevent : public base::NonThreadSafe { // and |address_length| alive until the callback is called. int RecvFrom(IOBuffer* buf, int buf_len, - struct sockaddr* address, - socklen_t* address_length, + IPEndPoint* address, CompletionCallback* callback); // Send to a socket with a particular destination. @@ -108,14 +107,13 @@ class UDPSocketLibevent : public base::NonThreadSafe { // alive until the callback is called. int SendTo(IOBuffer* buf, int buf_len, - const struct sockaddr* address, - socklen_t address_length, + const IPEndPoint& address, CompletionCallback* callback); // Returns true if the socket is already connected or bound. bool is_connected() const { return socket_ != kInvalidSocket; } - AddressList* local_address() { return local_address_.get(); } + IPEndPoint* local_address() { return local_address_.get(); } private: static const int kInvalidSocket = -1; @@ -164,7 +162,7 @@ class UDPSocketLibevent : public base::NonThreadSafe { void DidCompleteWrite(); // Returns the OS error code (or 0 on success). - int CreateSocket(const struct addrinfo* ai); + int CreateSocket(const IPEndPoint& address); int InternalRead(); int InternalWrite(IOBuffer* buf, int buf_len); @@ -173,8 +171,8 @@ class UDPSocketLibevent : public base::NonThreadSafe { // These are mutable since they're just cached copies to make // GetPeerAddress/GetLocalAddress smarter. - mutable scoped_ptr<AddressList> local_address_; - mutable scoped_ptr<AddressList> remote_address_; + mutable scoped_ptr<IPEndPoint> local_address_; + mutable scoped_ptr<IPEndPoint> remote_address_; // The socket's libevent wrappers MessageLoopForIO::FileDescriptorWatcher read_socket_watcher_; @@ -184,17 +182,15 @@ class UDPSocketLibevent : public base::NonThreadSafe { ReadWatcher read_watcher_; WriteWatcher write_watcher_; - // The buffer used by OnSocketReady to retry Read requests + // The buffer used by InternalRead() to retry Read requests scoped_refptr<IOBuffer> read_buf_; int read_buf_len_; - struct sockaddr* recv_from_address_; - socklen_t* recv_from_address_length_; + IPEndPoint* recv_from_address_; - // The buffer used by OnSocketReady to retry Write requests + // The buffer used by InternalWrite() to retry Write requests scoped_refptr<IOBuffer> write_buf_; int write_buf_len_; - const struct sockaddr* send_to_address_; - socklen_t send_to_address_length_; + scoped_ptr<IPEndPoint> send_to_address_; // External callback; called when read is complete. CompletionCallback* read_callback_; diff --git a/net/udp/udp_socket_unittest.cc b/net/udp/udp_socket_unittest.cc index aaf3dfa..dd2bb711 100644 --- a/net/udp/udp_socket_unittest.cc +++ b/net/udp/udp_socket_unittest.cc @@ -8,6 +8,7 @@ #include "base/basictypes.h" #include "base/metrics/histogram.h" #include "net/base/io_buffer.h" +#include "net/base/ip_endpoint.h" #include "net/base/net_errors.h" #include "net/base/net_test_suite.h" #include "net/base/net_util.h" @@ -24,18 +25,14 @@ class UDPSocketTest : public PlatformTest { public: UDPSocketTest() : buffer_(new IOBufferWithSize(kMaxRead)) { - recv_from_address_length_ = sizeof(recv_from_storage_); - recv_from_address_ = - reinterpret_cast<struct sockaddr*>(&recv_from_storage_); } // Blocks until data is read from the socket. std::string RecvFromSocket(UDPServerSocket* socket) { TestCompletionCallback callback; - recv_from_address_length_ = sizeof(struct sockaddr_in6); - int rv = socket->RecvFrom(buffer_, kMaxRead, recv_from_address_, - &recv_from_address_length_, &callback); + int rv = socket->RecvFrom(buffer_, kMaxRead, &recv_from_address_, + &callback); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); if (rv < 0) @@ -45,20 +42,18 @@ class UDPSocketTest : public PlatformTest { // Loop until |msg| has been written to the socket or until an // error occurs. - // If |sockaddr| is non-NULL, then |sockaddr| and |sockaddr_len| are used - // for the destination to send to. Otherwise, will send to the last socket - // this server received from. + // If |address| is specified, then it is used for the destination + // to send to. Otherwise, will send to the last socket this server + // received from. + int SendToSocket(UDPServerSocket* socket, std::string msg) { + return SendToSocket(socket, msg, recv_from_address_); + } + int SendToSocket(UDPServerSocket* socket, std::string msg, - struct sockaddr* sockaddr, - socklen_t sockaddr_len) { + const IPEndPoint& address) { TestCompletionCallback callback; - if (sockaddr == NULL) { - sockaddr = recv_from_address_; - sockaddr_len = recv_from_address_length_; - } - int length = msg.length(); scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); scoped_refptr<DrainableIOBuffer> buffer( @@ -67,8 +62,7 @@ class UDPSocketTest : public PlatformTest { int bytes_sent = 0; while (buffer->BytesRemaining()) { int rv = socket->SendTo(buffer, buffer->BytesRemaining(), - sockaddr, sockaddr_len, - &callback); + address, &callback); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); if (rv <= 0) @@ -82,7 +76,6 @@ class UDPSocketTest : public PlatformTest { std::string ReadSocket(UDPClientSocket* socket) { TestCompletionCallback callback; - recv_from_address_length_ = sizeof(struct sockaddr_in6); int rv = socket->Read(buffer_, kMaxRead, &callback); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); @@ -117,18 +110,16 @@ class UDPSocketTest : public PlatformTest { private: static const int kMaxRead = 1024; scoped_refptr<IOBufferWithSize> buffer_; - struct sockaddr_storage recv_from_storage_; - struct sockaddr* recv_from_address_; - socklen_t recv_from_address_length_; + struct IPEndPoint recv_from_address_; }; // Creates and address from an ip/port and returns it in |address|. -void CreateUDPAddress(std::string ip_str, int port, AddressList* address) { +void CreateUDPAddress(std::string ip_str, int port, IPEndPoint* address) { IPAddressNumber ip_number; bool rv = ParseIPLiteralToNumber(ip_str, &ip_number); if (!rv) return; - *address = AddressList(ip_number, port, false); + *address = IPEndPoint(ip_number, port); } TEST_F(UDPSocketTest, Connect) { @@ -136,14 +127,14 @@ TEST_F(UDPSocketTest, Connect) { std::string simple_message("hello world!"); // Setup the server to listen. - AddressList bind_address; + IPEndPoint bind_address; CreateUDPAddress("0.0.0.0", kPort, &bind_address); UDPServerSocket server(NULL, NetLog::Source()); int rv = server.Listen(bind_address); EXPECT_EQ(OK, rv); // Setup the client. - AddressList server_address; + IPEndPoint server_address; CreateUDPAddress("127.0.0.1", kPort, &server_address); UDPClientSocket client(NULL, NetLog::Source()); rv = client.Connect(server_address); @@ -158,7 +149,7 @@ TEST_F(UDPSocketTest, Connect) { DCHECK(simple_message == str); // Server echoes reply. - rv = SendToSocket(&server, simple_message, NULL, 0); + rv = SendToSocket(&server, simple_message); EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); // Client waits for response. @@ -181,7 +172,7 @@ TEST_F(UDPSocketTest, VerifyConnectBindsAddr) { std::string foreign_message("BAD MESSAGE TO GET!!"); // Setup the first server to listen. - AddressList bind_address; + IPEndPoint bind_address; CreateUDPAddress("0.0.0.0", kPort1, &bind_address); UDPServerSocket server1(NULL, NetLog::Source()); int rv = server1.Listen(bind_address); @@ -194,7 +185,7 @@ TEST_F(UDPSocketTest, VerifyConnectBindsAddr) { EXPECT_EQ(OK, rv); // Setup the client, connected to server 1. - AddressList server_address; + IPEndPoint server_address; CreateUDPAddress("127.0.0.1", kPort1, &server_address); UDPClientSocket client(NULL, NetLog::Source()); rv = client.Connect(server_address); @@ -209,20 +200,18 @@ TEST_F(UDPSocketTest, VerifyConnectBindsAddr) { DCHECK(simple_message == str); // Get the client's address. - AddressList client_address; + IPEndPoint client_address; rv = client.GetLocalAddress(&client_address); EXPECT_EQ(OK, rv); // Server2 sends reply. rv = SendToSocket(&server2, foreign_message, - client_address.head()->ai_addr, - client_address.head()->ai_addrlen); + client_address); EXPECT_EQ(foreign_message.length(), static_cast<size_t>(rv)); // Server1 sends reply. rv = SendToSocket(&server1, simple_message, - client_address.head()->ai_addr, - client_address.head()->ai_addrlen); + client_address); EXPECT_EQ(simple_message.length(), static_cast<size_t>(rv)); // Client waits for response. @@ -244,43 +233,29 @@ TEST_F(UDPSocketTest, ClientGetLocalPeerAddresses) { for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) { net::IPAddressNumber ip_number; net::ParseIPLiteralToNumber(tests[i].remote_address, &ip_number); - net::AddressList remote_address(ip_number, 80, true); + net::IPEndPoint remote_address(ip_number, 80); net::ParseIPLiteralToNumber(tests[i].local_address, &ip_number); - net::AddressList local_address(ip_number, 80, true); + net::IPEndPoint local_address(ip_number, 80); UDPClientSocket client(NULL, NetLog::Source()); int rv = client.Connect(remote_address); EXPECT_LE(ERR_IO_PENDING, rv); - AddressList fetched_local_address; + IPEndPoint fetched_local_address; rv = client.GetLocalAddress(&fetched_local_address); EXPECT_EQ(OK, rv); - const struct addrinfo* a1 = local_address.head(); - const struct addrinfo* a2 = fetched_local_address.head(); - EXPECT_TRUE(a1 != NULL); - EXPECT_TRUE(a2 != NULL); - - EXPECT_EQ(a1->ai_family, a2->ai_family); - EXPECT_EQ(a1->ai_addrlen, a2->ai_addrlen); // TODO(mbelshe): figure out how to verify the IP and port. // The port is dynamically generated by the udp stack. // The IP is the real IP of the client, not necessarily // loopback. - //EXPECT_EQ(NetAddressToString(a1), NetAddressToString(a2)); + //EXPECT_EQ(local_address.address(), fetched_local_address.address()); - AddressList fetched_remote_address; + IPEndPoint fetched_remote_address; rv = client.GetPeerAddress(&fetched_remote_address); EXPECT_EQ(OK, rv); - a1 = remote_address.head(); - a2 = fetched_remote_address.head(); - EXPECT_TRUE(a1 != NULL); - EXPECT_TRUE(a2 != NULL); - - EXPECT_EQ(a1->ai_family, a2->ai_family); - EXPECT_EQ(a1->ai_addrlen, a2->ai_addrlen); - EXPECT_EQ(NetAddressToString(a1), NetAddressToString(a2)); + EXPECT_EQ(remote_address, fetched_remote_address); } } |