Skip to content

Commit

Permalink
Cleanup socket address conversion in libjingle glue code.
Browse files Browse the repository at this point in the history
When converting between talk_base::SocketAddress and net::IpEndPoint IPv6 
addresses were not supported and errors were not always handled correctly.
Both of these problems fixed in this CL.

Review URL: https://chromiumcodereview.appspot.com/13529031

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@206616 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
sergeyu@chromium.org committed Jun 15, 2013
1 parent 75ce659 commit 340b128
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
7 changes: 5 additions & 2 deletions content/renderer/p2p/ipc_socket_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,14 @@ bool IpcPacketSocket::Init(P2PSocketType type, P2PSocketClient* client,
state_ = IS_OPENING;

net::IPEndPoint local_endpoint;
if (!jingle_glue::SocketAddressToIPEndPoint(local_address, &local_endpoint)) {
if (!jingle_glue::SocketAddressToIPEndPoint(
local_address, &local_endpoint)) {
return false;
}

net::IPEndPoint remote_endpoint;
if (!jingle_glue::SocketAddressToIPEndPoint(
if (!remote_address.IsNil() &&
!jingle_glue::SocketAddressToIPEndPoint(
remote_address, &remote_endpoint)) {
return false;
}
Expand Down Expand Up @@ -236,6 +238,7 @@ int IpcPacketSocket::SendTo(const void *data, size_t data_size,
net::IPEndPoint address_chrome;
if (!jingle_glue::SocketAddressToIPEndPoint(address, &address_chrome)) {
NOTREACHED();
error_ = EINVAL;
return -1;
}

Expand Down
30 changes: 12 additions & 18 deletions jingle/glue/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,20 @@

namespace jingle_glue {

bool IPEndPointToSocketAddress(const net::IPEndPoint& address_chrome,
talk_base::SocketAddress* address_lj) {
if (address_chrome.GetFamily() != net::ADDRESS_FAMILY_IPV4) {
LOG(ERROR) << "Only IPv4 addresses are supported.";
return false;
}
uint32 ip_as_int = talk_base::NetworkToHost32(
*reinterpret_cast<const uint32*>(&address_chrome.address()[0]));
*address_lj = talk_base::SocketAddress(ip_as_int, address_chrome.port());
return true;
bool IPEndPointToSocketAddress(const net::IPEndPoint& ip_endpoint,
talk_base::SocketAddress* address) {
sockaddr_storage addr;
socklen_t len = sizeof(addr);
return ip_endpoint.ToSockAddr(reinterpret_cast<sockaddr*>(&addr), &len) &&
talk_base::SocketAddressFromSockAddrStorage(addr, address);
}

bool SocketAddressToIPEndPoint(const talk_base::SocketAddress& address_lj,
net::IPEndPoint* address_chrome) {
uint32 ip = talk_base::HostToNetwork32(address_lj.ip());
net::IPAddressNumber address;
address.resize(net::kIPv4AddressSize);
memcpy(&address[0], &ip, net::kIPv4AddressSize);
*address_chrome = net::IPEndPoint(address, address_lj.port());
return true;
bool SocketAddressToIPEndPoint(const talk_base::SocketAddress& address,
net::IPEndPoint* ip_endpoint) {
sockaddr_storage addr;
int size = address.ToSockAddrStorage(&addr);
return (size > 0) &&
ip_endpoint->FromSockAddr(reinterpret_cast<sockaddr*>(&addr), size);
}

std::string SerializeP2PCandidate(const cricket::Candidate& candidate) {
Expand Down
8 changes: 4 additions & 4 deletions jingle/glue/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ namespace jingle_glue {
// Chromium and libjingle represent socket addresses differently. The
// following two functions are used to convert addresses from one
// representation to another.
bool IPEndPointToSocketAddress(const net::IPEndPoint& address_chrome,
talk_base::SocketAddress* address_lj);
bool SocketAddressToIPEndPoint(const talk_base::SocketAddress& address_lj,
net::IPEndPoint* address_chrome);
bool IPEndPointToSocketAddress(const net::IPEndPoint& ip_endpoint,
talk_base::SocketAddress* address);
bool SocketAddressToIPEndPoint(const talk_base::SocketAddress& address,
net::IPEndPoint* ip_endpoint);

// Helper functions to serialize and deserialize P2P candidates.
std::string SerializeP2PCandidate(const cricket::Candidate& candidate);
Expand Down

0 comments on commit 340b128

Please sign in to comment.