Chromium Code Reviews| Index: runtime/bin/socket_linux.cc |
| diff --git a/runtime/bin/socket_linux.cc b/runtime/bin/socket_linux.cc |
| index c36a66e3e26ff76e49bed10a17dba9f4e60dca59..e08270f811af8c0ac16d2df7702dbeba8ffc8c44 100644 |
| --- a/runtime/bin/socket_linux.cc |
| +++ b/runtime/bin/socket_linux.cc |
| @@ -11,6 +11,7 @@ |
| #include <string.h> // NOLINT |
| #include <sys/stat.h> // NOLINT |
| #include <unistd.h> // NOLINT |
| +#include <net/if.h> // NOLINT |
| #include <netinet/tcp.h> // NOLINT |
| #include <ifaddrs.h> // NOLINT |
| @@ -39,6 +40,21 @@ SocketAddress::SocketAddress(struct sockaddr* sa) { |
| } |
| +bool Socket::FormatNumericAddress(RawAddr* addr, char* address, int len) { |
| + socklen_t salen = SocketAddress::GetAddrLength(addr); |
| + if (TEMP_FAILURE_RETRY(getnameinfo(&addr->addr, |
| + salen, |
| + address, |
| + len, |
| + NULL, |
| + 0, |
| + NI_NUMERICHOST)) != 0) { |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| + |
| bool Socket::Initialize() { |
| // Nothing to do on Linux. |
| return true; |
| @@ -106,6 +122,21 @@ int Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { |
| } |
| +int Socket::RecvFrom(intptr_t fd, void* buffer, intptr_t num_bytes, |
| + RawAddr* addr) { |
| + ASSERT(fd >= 0); |
| + socklen_t addr_len = sizeof(addr->ss); |
| + ssize_t read_bytes = |
| + recvfrom(fd, buffer, num_bytes, 0, &addr->addr, &addr_len); |
|
Anders Johnsen
2013/11/29 12:36:54
TEMP_FAILURE_RETRY?
Søren Gjesse
2013/12/12 11:38:46
Done.
|
| + if (read_bytes == -1 && errno == EWOULDBLOCK) { |
| + // If the read would block we need to retry and therefore return 0 |
| + // as the number of bytes written. |
| + read_bytes = 0; |
| + } |
| + return read_bytes; |
| +} |
| + |
| + |
| int Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { |
| ASSERT(fd >= 0); |
| ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes)); |
| @@ -119,6 +150,23 @@ int Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { |
| } |
| +int Socket::SendTo(intptr_t fd, const void* buffer, intptr_t num_bytes, |
| + RawAddr addr) { |
| + ASSERT(fd >= 0); |
| + ssize_t written_bytes = |
| + TEMP_FAILURE_RETRY( |
| + sendto(fd, buffer, num_bytes, 0, |
| + &addr.addr, SocketAddress::GetAddrLength(&addr))); |
| + ASSERT(EAGAIN == EWOULDBLOCK); |
| + if (written_bytes == -1 && errno == EWOULDBLOCK) { |
| + // If the would block we need to retry and therefore return 0 as |
| + // the number of bytes written. |
| + written_bytes = 0; |
| + } |
| + return written_bytes; |
| +} |
| + |
| + |
| intptr_t Socket::GetPort(intptr_t fd) { |
| ASSERT(fd >= 0); |
| RawAddr raw; |
| @@ -256,6 +304,36 @@ bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { |
| } |
| +intptr_t Socket::CreateBindDatagram( |
| + RawAddr* addr, intptr_t port, bool reuseAddress) { |
| + intptr_t fd; |
| + |
| + fd = TEMP_FAILURE_RETRY( |
| + socket(addr->addr.sa_family, SOCK_DGRAM, IPPROTO_UDP)); |
| + if (fd < 0) return -1; |
| + |
| + FDUtils::SetCloseOnExec(fd); |
| + |
| + if (reuseAddress) { |
| + int optval = 1; |
| + TEMP_FAILURE_RETRY( |
| + setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
| + } |
| + |
| + SocketAddress::SetAddrPort(addr, port); |
| + if (TEMP_FAILURE_RETRY( |
| + bind(fd, |
| + &addr->addr, |
| + SocketAddress::GetAddrLength(addr))) < 0) { |
| + TEMP_FAILURE_RETRY(close(fd)); |
| + return -1; |
| + } |
| + |
| + Socket::SetNonBlocking(fd); |
| + return fd; |
| +} |
| + |
| + |
| static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) { |
| if (ifa->ifa_addr == NULL) { |
| // OpenVPN's virtual device tun0. |
| @@ -409,6 +487,21 @@ bool Socket::SetBlocking(intptr_t fd) { |
| } |
| +bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { |
| + int on; |
| + socklen_t len = sizeof(on); |
| + int err = TEMP_FAILURE_RETRY(getsockopt(fd, |
| + IPPROTO_TCP, |
| + TCP_NODELAY, |
| + reinterpret_cast<void *>(&on), |
| + &len)); |
| + if (err == 0) { |
| + *enabled = on == 1; |
| + } |
| + return err == 0; |
| +} |
| + |
| + |
| bool Socket::SetNoDelay(intptr_t fd, bool enabled) { |
| int on = enabled ? 1 : 0; |
| return TEMP_FAILURE_RETRY(setsockopt(fd, |
| @@ -418,6 +511,140 @@ bool Socket::SetNoDelay(intptr_t fd, bool enabled) { |
| sizeof(on))) == 0; |
| } |
| + |
| +bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) { |
| + uint8_t on; |
| + socklen_t len = sizeof(on); |
| + bool ok; |
| + if (protocol == SocketAddress::TYPE_IPV4) { |
| + ok = TEMP_FAILURE_RETRY(getsockopt(fd, |
|
Anders Johnsen
2013/11/29 12:36:54
Set ipv4/ipv6 values above and only have one call?
Søren Gjesse
2013/12/12 11:38:46
Done.
|
| + IPPROTO_IP, |
| + IP_MULTICAST_LOOP, |
| + reinterpret_cast<char *>(&on), |
| + &len)) == 0; |
| + } else { |
| + ASSERT(protocol == SocketAddress::TYPE_IPV6); |
| + ok = TEMP_FAILURE_RETRY(getsockopt(fd, |
| + IPPROTO_IPV6, |
| + IPV6_MULTICAST_LOOP, |
| + reinterpret_cast<char *>(&on), |
| + &len)) == 0; |
| + } |
| + if (ok) { |
| + *enabled = (on == 1); |
| + } |
| + return ok; |
| +} |
| + |
| + |
| +bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) { |
| + int on = enabled ? 1 : 0; |
| + if (protocol == SocketAddress::TYPE_IPV4) { |
| + return TEMP_FAILURE_RETRY(setsockopt(fd, |
| + IPPROTO_IP, |
| + IP_MULTICAST_LOOP, |
| + reinterpret_cast<char *>(&on), |
| + sizeof(on))) == 0; |
| + } else { |
| + ASSERT(protocol == SocketAddress::TYPE_IPV6); |
| + return TEMP_FAILURE_RETRY(setsockopt(fd, |
| + IPPROTO_IPV6, |
| + IPV6_MULTICAST_LOOP, |
| + reinterpret_cast<char *>(&on), |
| + sizeof(on))) == 0; |
| + } |
| +} |
| + |
| + |
| +bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) { |
| + uint8_t v; |
| + socklen_t len = sizeof(v); |
| + bool ok; |
| + if (protocol == SocketAddress::TYPE_IPV4) { |
| + ok = TEMP_FAILURE_RETRY(getsockopt(fd, |
| + IPPROTO_IP, |
| + IP_MULTICAST_TTL, |
| + reinterpret_cast<char *>(&v), |
| + &len)) == 0; |
| + } else { |
| + ASSERT(protocol == SocketAddress::TYPE_IPV6); |
| + ok = TEMP_FAILURE_RETRY(getsockopt(fd, |
| + IPPROTO_IPV6, |
| + IPV6_MULTICAST_HOPS, |
| + reinterpret_cast<char *>(&v), |
| + &len)) == 0; |
| + } |
| + if (ok) { |
| + *value = v; |
| + } |
| + return ok; |
| +} |
| + |
| + |
| +bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) { |
| + if (protocol == SocketAddress::TYPE_IPV4) { |
| + uint8_t v = value; |
| + return TEMP_FAILURE_RETRY(setsockopt(fd, |
| + IPPROTO_IP, |
| + IP_MULTICAST_TTL, |
| + reinterpret_cast<char *>(&v), |
| + sizeof(v))) == 0; |
| + } else { |
| + ASSERT(protocol == SocketAddress::TYPE_IPV6); |
| + int v = value; |
| + return TEMP_FAILURE_RETRY(setsockopt(fd, |
| + IPPROTO_IPV6, |
| + IPV6_MULTICAST_HOPS, |
| + reinterpret_cast<char *>(&v), |
| + sizeof(v))) == 0; |
| + } |
| +} |
| + |
| + |
| +bool Socket::GetBroadcast(intptr_t fd, bool* enabled) { |
| + int on; |
| + socklen_t len = sizeof(on); |
| + int err = TEMP_FAILURE_RETRY(getsockopt(fd, |
| + SOL_SOCKET, |
| + SO_BROADCAST, |
| + reinterpret_cast<char *>(&on), |
| + &len)); |
| + if (err == 0) { |
| + *enabled = on == 1; |
| + } |
| + return err == 0; |
| +} |
| + |
| + |
| +bool Socket::SetBroadcast(intptr_t fd, bool enabled) { |
| + int on = enabled ? 1 : 0; |
| + return TEMP_FAILURE_RETRY(setsockopt(fd, |
| + SOL_SOCKET, |
| + SO_BROADCAST, |
| + reinterpret_cast<char *>(&on), |
| + sizeof(on))) == 0; |
| +} |
| + |
| + |
| +bool Socket::JoinMulticast(intptr_t fd, RawAddr* addr, int interfaceIndex) { |
| + int proto = addr->addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; |
| + struct group_req mreq; |
| + mreq.gr_interface = interfaceIndex; |
| + memmove(&mreq.gr_group, &addr->ss, SocketAddress::GetAddrLength(addr)); |
| + return TEMP_FAILURE_RETRY(setsockopt( |
| + fd, proto, MCAST_JOIN_GROUP, &mreq, sizeof(mreq))) == 0; |
| +} |
| + |
| + |
| +bool Socket::LeaveMulticast(intptr_t fd, RawAddr* addr, int interfaceIndex) { |
| + int proto = addr->addr.sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; |
| + struct group_req mreq; |
| + mreq.gr_interface = interfaceIndex; |
| + memmove(&mreq.gr_group, &addr->ss, SocketAddress::GetAddrLength(addr)); |
| + return TEMP_FAILURE_RETRY(setsockopt( |
| + fd, proto, MCAST_LEAVE_GROUP, &mreq, sizeof(mreq))) == 0; |
| +} |
| + |
| } // namespace bin |
| } // namespace dart |