diff options
author | szym@chromium.org <szym@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-07 21:44:56 +0000 |
---|---|---|
committer | szym@chromium.org <szym@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-07 21:44:56 +0000 |
commit | 7054e78fe6a2fcda72b06dc196b5f91cfdc75872 (patch) | |
tree | 2efdd9b07a784a17905d737df9b762d88a6cd1c4 /net/udp | |
parent | cd46545164adf645d744f3955b256cf89412cdc6 (diff) | |
download | chromium_src-7054e78fe6a2fcda72b06dc196b5f91cfdc75872.zip chromium_src-7054e78fe6a2fcda72b06dc196b5f91cfdc75872.tar.gz chromium_src-7054e78fe6a2fcda72b06dc196b5f91cfdc75872.tar.bz2 |
Reimplements net::AddressList without struct addrinfo.
net::AddressList extends std::vector<std::IPEndPoint> by canonical name. (Canonical name is planned to be removed as well.)
Removes dependency on sys_addrinfo.h throughout the codebase.
Introduces net::SockaddrStorage for convenience.
BUG=125696
TEST=green waterfall
Review URL: http://codereview.chromium.org/10309002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@135731 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/udp')
-rw-r--r-- | net/udp/udp_socket_libevent.cc | 58 | ||||
-rw-r--r-- | net/udp/udp_socket_unittest.cc | 3 | ||||
-rw-r--r-- | net/udp/udp_socket_win.cc | 48 |
3 files changed, 44 insertions, 65 deletions
diff --git a/net/udp/udp_socket_libevent.cc b/net/udp/udp_socket_libevent.cc index f8ef7e3..13196d2 100644 --- a/net/udp/udp_socket_libevent.cc +++ b/net/udp/udp_socket_libevent.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -95,13 +95,11 @@ int UDPSocketLibevent::GetPeerAddress(IPEndPoint* address) const { return ERR_SOCKET_NOT_CONNECTED; if (!remote_address_.get()) { - struct sockaddr_storage addr_storage; - socklen_t addr_len = sizeof(addr_storage); - struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); - if (getpeername(socket_, addr, &addr_len)) + SockaddrStorage storage; + if (getpeername(socket_, storage.addr, &storage.addr_len)) return MapSystemError(errno); scoped_ptr<IPEndPoint> address(new IPEndPoint()); - if (!address->FromSockAddr(addr, addr_len)) + if (!address->FromSockAddr(storage.addr, storage.addr_len)) return ERR_FAILED; remote_address_.reset(address.release()); } @@ -117,13 +115,11 @@ int UDPSocketLibevent::GetLocalAddress(IPEndPoint* address) const { return ERR_SOCKET_NOT_CONNECTED; if (!local_address_.get()) { - struct sockaddr_storage addr_storage; - socklen_t addr_len = sizeof(addr_storage); - struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); - if (getsockname(socket_, addr, &addr_len)) + SockaddrStorage storage; + if (getsockname(socket_, storage.addr, &storage.addr_len)) return MapSystemError(errno); scoped_ptr<IPEndPoint> address(new IPEndPoint()); - if (!address->FromSockAddr(addr, addr_len)) + if (!address->FromSockAddr(storage.addr, storage.addr_len)) return ERR_FAILED; local_address_.reset(address.release()); } @@ -240,13 +236,11 @@ int UDPSocketLibevent::InternalConnect(const IPEndPoint& address) { if (rv < 0) return rv; - 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)) + SockaddrStorage storage; + if (!address.ToSockAddr(storage.addr, &storage.addr_len)) return ERR_FAILED; - rv = HANDLE_EINTR(connect(socket_, addr, addr_len)); + rv = HANDLE_EINTR(connect(socket_, storage.addr, storage.addr_len)); if (rv < 0) return MapSystemError(errno); @@ -393,41 +387,37 @@ int UDPSocketLibevent::InternalRecvFrom(IOBuffer* buf, int buf_len, 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); + SockaddrStorage storage; bytes_transferred = HANDLE_EINTR(recvfrom(socket_, buf->data(), buf_len, flags, - addr, - &addr_len)); + storage.addr, + &storage.addr_len)); int result; if (bytes_transferred >= 0) { result = bytes_transferred; - if (address && !address->FromSockAddr(addr, addr_len)) + if (address && !address->FromSockAddr(storage.addr, storage.addr_len)) result = ERR_FAILED; } else { result = MapSystemError(errno); } if (result != ERR_IO_PENDING) - LogRead(result, buf->data(), addr_len, addr); + LogRead(result, buf->data(), storage.addr_len, storage.addr); return result; } int UDPSocketLibevent::InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address) { - struct sockaddr_storage addr_storage; - size_t addr_len = sizeof(addr_storage); - struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); - + SockaddrStorage storage; + struct sockaddr* addr = storage.addr; if (!address) { addr = NULL; - addr_len = 0; + storage.addr_len = 0; } else { - if (!address->ToSockAddr(addr, &addr_len)) { + if (!address->ToSockAddr(storage.addr, &storage.addr_len)) { int result = ERR_FAILED; LogWrite(result, NULL, NULL); return result; @@ -439,7 +429,7 @@ int UDPSocketLibevent::InternalSendTo(IOBuffer* buf, int buf_len, buf_len, 0, addr, - addr_len)); + storage.addr_len)); if (result < 0) result = MapSystemError(errno); if (result != ERR_IO_PENDING) @@ -448,12 +438,10 @@ int UDPSocketLibevent::InternalSendTo(IOBuffer* buf, int buf_len, } int UDPSocketLibevent::DoBind(const IPEndPoint& address) { - 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)) + SockaddrStorage storage; + if (!address.ToSockAddr(storage.addr, &storage.addr_len)) return ERR_UNEXPECTED; - int rv = bind(socket_, addr, addr_len); + int rv = bind(socket_, storage.addr, storage.addr_len); return rv < 0 ? MapSystemError(errno) : rv; } diff --git a/net/udp/udp_socket_unittest.cc b/net/udp/udp_socket_unittest.cc index 5165d3d..ea8791a 100644 --- a/net/udp/udp_socket_unittest.cc +++ b/net/udp/udp_socket_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -15,7 +15,6 @@ #include "net/base/net_log_unittest.h" #include "net/base/net_test_suite.h" #include "net/base/net_util.h" -#include "net/base/sys_addrinfo.h" #include "net/base/test_completion_callback.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/platform_test.h" diff --git a/net/udp/udp_socket_win.cc b/net/udp/udp_socket_win.cc index 06f5e3d..423ba00 100644 --- a/net/udp/udp_socket_win.cc +++ b/net/udp/udp_socket_win.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -94,14 +94,13 @@ int UDPSocketWin::GetPeerAddress(IPEndPoint* address) const { if (!is_connected()) return ERR_SOCKET_NOT_CONNECTED; + // TODO(szym): Simplify. http://crbug.com/126152 if (!remote_address_.get()) { - struct sockaddr_storage addr_storage; - int addr_len = sizeof(addr_storage); - struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); - if (getpeername(socket_, addr, &addr_len)) + SockaddrStorage storage; + if (getpeername(socket_, storage.addr, &storage.addr_len)) return MapSystemError(WSAGetLastError()); scoped_ptr<IPEndPoint> address(new IPEndPoint()); - if (!address->FromSockAddr(addr, addr_len)) + if (!address->FromSockAddr(storage.addr, storage.addr_len)) return ERR_FAILED; remote_address_.reset(address.release()); } @@ -116,14 +115,13 @@ int UDPSocketWin::GetLocalAddress(IPEndPoint* address) const { if (!is_connected()) return ERR_SOCKET_NOT_CONNECTED; + // TODO(szym): Simplify. http://crbug.com/126152 if (!local_address_.get()) { - struct sockaddr_storage addr_storage; - socklen_t addr_len = sizeof(addr_storage); - struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); - if (getsockname(socket_, addr, &addr_len)) + SockaddrStorage storage; + if (getsockname(socket_, storage.addr, &storage.addr_len)) return MapSystemError(WSAGetLastError()); scoped_ptr<IPEndPoint> address(new IPEndPoint()); - if (!address->FromSockAddr(addr, addr_len)) + if (!address->FromSockAddr(storage.addr, storage.addr_len)) return ERR_FAILED; local_address_.reset(address.release()); } @@ -218,13 +216,11 @@ int UDPSocketWin::InternalConnect(const IPEndPoint& address) { if (rv < 0) return rv; - 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)) + SockaddrStorage storage; + if (!address.ToSockAddr(storage.addr, &storage.addr_len)) return ERR_FAILED; - rv = connect(socket_, addr, addr_len); + rv = connect(socket_, storage.addr, storage.addr_len); if (rv < 0) return MapSystemError(WSAGetLastError()); @@ -401,16 +397,14 @@ int UDPSocketWin::InternalRecvFrom(IOBuffer* buf, int buf_len, int UDPSocketWin::InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address) { - struct sockaddr_storage addr_storage; - size_t addr_len = sizeof(addr_storage); - struct sockaddr* addr = reinterpret_cast<struct sockaddr*>(&addr_storage); - + SockaddrStorage storage; + struct sockaddr* addr = storage.addr; // Convert address. if (!address) { addr = NULL; - addr_len = 0; + storage.addr_len = 0; } else { - if (!address->ToSockAddr(addr, &addr_len)) { + if (!address->ToSockAddr(addr, &storage.addr_len)) { int result = ERR_FAILED; LogWrite(result, NULL, NULL); return result; @@ -425,7 +419,7 @@ int UDPSocketWin::InternalSendTo(IOBuffer* buf, int buf_len, DWORD num; AssertEventNotSignaled(write_overlapped_.hEvent); int rv = WSASendTo(socket_, &write_buffer, 1, &num, flags, - addr, addr_len, &write_overlapped_, NULL); + addr, storage.addr_len, &write_overlapped_, NULL); if (rv == 0) { if (ResetEventIfSignaled(write_overlapped_.hEvent)) { int result = num; @@ -446,12 +440,10 @@ int UDPSocketWin::InternalSendTo(IOBuffer* buf, int buf_len, } int UDPSocketWin::DoBind(const IPEndPoint& address) { - 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)) + SockaddrStorage storage; + if (!address.ToSockAddr(storage.addr, &storage.addr_len)) return ERR_UNEXPECTED; - int rv = bind(socket_, addr, addr_len); + int rv = bind(socket_, storage.addr, storage.addr_len); return rv < 0 ? MapSystemError(WSAGetLastError()) : rv; } |