diff --git a/net/socket/udp_socket_starboard.cc b/net/socket/udp_socket_starboard.cc index 7e51b873276a..e3d8b0043112 100644 --- a/net/socket/udp_socket_starboard.cc +++ b/net/socket/udp_socket_starboard.cc @@ -162,7 +162,7 @@ int UDPSocketStarboard::GetLocalAddress(IPEndPoint* address) const { int UDPSocketStarboard::Read(IOBuffer* buf, int buf_len, CompletionOnceCallback callback) { - return RecvFrom(buf, buf_len, NULL, std::move(callback)); + return RecvFrom(buf, buf_len, nullptr, std::move(callback)); } int UDPSocketStarboard::RecvFrom(IOBuffer* buf, @@ -271,17 +271,34 @@ int UDPSocketStarboard::InternalConnect(const IPEndPoint& address) { DCHECK(!remote_address_.get()); int rv = 0; - // Cobalt does random bind despite bind_type_ because we do not connect - // UDP sockets but Chromium does. And if a socket does recvfrom() without - // any sendto() before, it needs to be bound to have a local port. - rv = RandomBind(address.GetFamily() == ADDRESS_FAMILY_IPV4 ? - IPAddress::IPv4AllZeros() : IPAddress::IPv6AllZeros()); + if (bind_type_ == DatagramSocket::RANDOM_BIND) { + // Construct IPAddress of appropriate size (IPv4 or IPv6) of 0s, + // representing INADDR_ANY or in6addr_any. + rv = RandomBind(address.GetFamily() == ADDRESS_FAMILY_IPV4 + ? IPAddress::IPv4AllZeros() + : IPAddress::IPv6AllZeros()); + } if (rv != OK) return rv; + SbSocketAddress storage; + if (!address.ToSbSocketAddress(&storage)) { + return ERR_ADDRESS_INVALID; + } + remote_address_.reset(new IPEndPoint(address)); + SbSocketError result = SbSocketConnect(socket_, &storage); + if (result != kSbSocketOk) { + return MapLastSocketError(socket_); + } + + LOG(INFO) << __FUNCTION__ + << " UDP CONNECT SUCCEEDED " + "================================================ " + << address; + return OK; } @@ -459,8 +476,8 @@ int UDPSocketStarboard::InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address) { SbSocketAddress sb_address; - int bytes_transferred = - SbSocketReceiveFrom(socket_, buf->data(), buf_len, &sb_address); + int bytes_transferred = SbSocketReceiveFrom(socket_, buf->data(), buf_len, + address ? &sb_address : nullptr); int result; if (bytes_transferred >= 0) { result = bytes_transferred; @@ -468,7 +485,7 @@ int UDPSocketStarboard::InternalRecvFrom(IOBuffer* buf, // platform's implementation. if (address && !address->FromSbSocketAddress(&sb_address)) { result = ERR_ADDRESS_INVALID; - } else if (bytes_transferred == buf_len) { + } else if (bytes_transferred > buf_len) { result = ERR_MSG_TOO_BIG; } } else { @@ -477,7 +494,8 @@ int UDPSocketStarboard::InternalRecvFrom(IOBuffer* buf, if (result != ERR_IO_PENDING) { IPEndPoint log_address; - if (result < 0 || !log_address.FromSbSocketAddress(&sb_address)) { + if (result < 0 || !address || + !log_address.FromSbSocketAddress(&sb_address)) { LogRead(result, buf->data(), NULL); } else { LogRead(result, buf->data(), &log_address);