diff options
author | Elliott Hughes <enh@google.com> | 2011-01-09 10:04:02 -0800 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2011-01-09 10:04:02 -0800 |
commit | 139545096c077c172af9e8dd7122b56172715ce9 (patch) | |
tree | 20e7724c5197c0ab99aea173d83ca0f79e17159e /luni/src | |
parent | a68d1154a7e60c12d661b6a03756dee039c99094 (diff) | |
parent | 2c9f1e5b63c624793d01038a2b0c72e853c017df (diff) | |
download | libcore-139545096c077c172af9e8dd7122b56172715ce9.zip libcore-139545096c077c172af9e8dd7122b56172715ce9.tar.gz libcore-139545096c077c172af9e8dd7122b56172715ce9.tar.bz2 |
am 2c9f1e5b: am 90a81872: am 3a9a00ad: Fix getifaddrs(3).
* commit '2c9f1e5b63c624793d01038a2b0c72e853c017df':
Fix getifaddrs(3).
Diffstat (limited to 'luni/src')
-rw-r--r-- | luni/src/main/native/NativeCrypto.cpp | 1 | ||||
-rw-r--r-- | luni/src/main/native/ifaddrs-android.cpp | 217 | ||||
-rw-r--r-- | luni/src/main/native/ifaddrs-android.h | 186 | ||||
-rw-r--r-- | luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp | 1 | ||||
-rw-r--r-- | luni/src/main/native/sub.mk | 1 | ||||
-rw-r--r-- | luni/src/test/java/libcore/java/net/NetworkInterfaceTest.java | 37 |
6 files changed, 262 insertions, 181 deletions
diff --git a/luni/src/main/native/NativeCrypto.cpp b/luni/src/main/native/NativeCrypto.cpp index 0ce14a6..555ef41 100644 --- a/luni/src/main/native/NativeCrypto.cpp +++ b/luni/src/main/native/NativeCrypto.cpp @@ -38,7 +38,6 @@ #include "JNIHelp.h" #include "JniConstants.h" #include "JniException.h" -#include "LocalArray.h" #include "NetFd.h" #include "NetworkUtilities.h" #include "ScopedLocalRef.h" diff --git a/luni/src/main/native/ifaddrs-android.cpp b/luni/src/main/native/ifaddrs-android.cpp new file mode 100644 index 0000000..4193bdf --- /dev/null +++ b/luni/src/main/native/ifaddrs-android.cpp @@ -0,0 +1,217 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifdef HAVE_ANDROID_OS + +#include "ifaddrs-android.h" + +#include <arpa/inet.h> +#include <cstring> +#include <errno.h> +#include <net/if.h> +#include <netinet/in.h> +#include <new> +#include <sys/ioctl.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <stdio.h> +#include <linux/netlink.h> +#include <linux/rtnetlink.h> + +#include "ScopedFd.h" +#include "UniquePtr.h" + +ifaddrs::ifaddrs(ifaddrs* next, sockaddr* ifa_addr, sockaddr* ifa_netmask) +: ifa_next(next), ifa_name(NULL), ifa_flags(0), ifa_addr(ifa_addr), ifa_netmask(ifa_netmask) +{ +} + +ifaddrs::~ifaddrs() { + delete ifa_next; + delete[] ifa_name; + delete ifa_addr; + delete ifa_netmask; +} + +static bool sendNetlinkMessage(int fd, const void* data, size_t byteCount) { + ssize_t sentByteCount = TEMP_FAILURE_RETRY(send(fd, data, byteCount, 0)); + return (sentByteCount == static_cast<ssize_t>(byteCount)); +} + +static ssize_t recvNetlinkMessage(int fd, char* buf, size_t byteCount) { + return TEMP_FAILURE_RETRY(recv(fd, buf, byteCount, 0)); +} + +// Returns a pointer to the first byte in the address data (which is +// stored in network byte order). +static uint8_t* sockaddrBytes(int family, sockaddr_storage* ss) { + if (family == AF_INET) { + sockaddr_in* ss4 = reinterpret_cast<sockaddr_in*>(ss); + return reinterpret_cast<uint8_t*>(&ss4->sin_addr); + } else if (family == AF_INET6) { + sockaddr_in6* ss6 = reinterpret_cast<sockaddr_in6*>(ss); + return reinterpret_cast<uint8_t*>(&ss6->sin6_addr); + } + return NULL; +} + +// Netlink gives us the address family in the header, and the +// sockaddr_in or sockaddr_in6 bytes as the payload. We need to +// stitch the two bits together into the sockaddr that's part of +// our portable interface. +static sockaddr* toSocketAddress(int family, void* data, size_t byteCount) { + // Set the address proper... + sockaddr_storage* ss = new sockaddr_storage; + memset(ss, 0, sizeof(*ss)); + ss->ss_family = family; + uint8_t* dst = sockaddrBytes(family, ss); + memcpy(dst, data, byteCount); + return reinterpret_cast<sockaddr*>(ss); +} + +// Netlink gives us the prefix length as a bit count. We need to turn +// that into a BSD-compatible netmask represented by a sockaddr*. +static sockaddr* toNetmask(int family, size_t prefixLength) { + // ...and work out the netmask from the prefix length. + sockaddr_storage* ss = new sockaddr_storage; + memset(ss, 0, sizeof(*ss)); + ss->ss_family = family; + uint8_t* dst = sockaddrBytes(family, ss); + memset(dst, 0xff, prefixLength / 8); + if ((prefixLength % 8) != 0) { + dst[prefixLength/8] = (0xff << (8 - (prefixLength % 8))); + } + return reinterpret_cast<sockaddr*>(ss); +} + +// Sadly, we can't keep the interface index for portability with BSD. +// We'll have to keep the name instead, and re-query the index when +// we need it later. +static bool setNameAndFlagsByIndex(ifaddrs* ifa, int interfaceIndex) { + // Get the name. + char buf[IFNAMSIZ]; + char* name = if_indextoname(interfaceIndex, buf); + if (name == NULL) { + return false; + } + ifa->ifa_name = new char[strlen(name) + 1]; + strcpy(ifa->ifa_name, name); + + // Get the flags. + ScopedFd fd(socket(AF_INET, SOCK_DGRAM, 0)); + if (fd.get() == -1) { + return false; + } + ifreq ifr; + memset(&ifr, 0, sizeof(ifr)); + strcpy(ifr.ifr_name, name); + int rc = ioctl(fd.get(), SIOCGIFFLAGS, &ifr); + if (rc == -1) { + return false; + } + ifa->ifa_flags = ifr.ifr_flags; + return true; +} + +// Source-compatible with the BSD function. +int getifaddrs(ifaddrs** result) { + // Simplify cleanup for callers. + *result = NULL; + + // Create a netlink socket. + ScopedFd fd(socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)); + if (fd.get() < 0) { + return -1; + } + + // Ask for the address information. + struct { + nlmsghdr netlinkHeader; + ifaddrmsg msg; + } request; + memset(&request, 0, sizeof(request)); + request.netlinkHeader.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST | NLM_F_MATCH; + request.netlinkHeader.nlmsg_type = RTM_GETADDR; + request.netlinkHeader.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(request))); + request.msg.ifa_family = AF_UNSPEC; // All families. + request.msg.ifa_index = 0; // All interfaces. + if (!sendNetlinkMessage(fd.get(), &request, request.netlinkHeader.nlmsg_len)) { + return -1; + } + + // Read the responses. + const size_t bufferSize = 65536; + UniquePtr<char[]> buf(new char[bufferSize]); + ssize_t bytesRead; + while ((bytesRead = recvNetlinkMessage(fd.get(), &buf[0], bufferSize)) > 0) { + nlmsghdr* hdr = reinterpret_cast<nlmsghdr*>(&buf[0]); + for (; NLMSG_OK(hdr, (size_t)bytesRead); hdr = NLMSG_NEXT(hdr, bytesRead)) { + switch (hdr->nlmsg_type) { + case NLMSG_DONE: + return 0; + case NLMSG_ERROR: + return -1; + case RTM_NEWADDR: + { + // A given RTM_NEWADDR payload may contain multiple addresses. The while loop + // below iterates through them. These locals contain the best address we've + // seen so far. + int ifa_index = -1; + UniquePtr<sockaddr> ifa_addr; + UniquePtr<sockaddr> ifa_netmask; + + ifaddrmsg* address = reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(hdr)); + rtattr* rta = IFA_RTA(address); + size_t ifaPayloadLength = IFA_PAYLOAD(hdr); + while (RTA_OK(rta, ifaPayloadLength)) { + // We can't just use IFA_ADDRESS because it's the destination address for + // a point-to-point interface; we can't just use IFA_LOCAL because we don't + // always have it. That is: we want IFA_LOCAL if we get it, but IFA_ADDRESS + // otherwise. We take advantage of the fact that the kernel returns + // IFA_LOCAL (if available) second. + if (rta->rta_type == IFA_LOCAL || rta->rta_type == IFA_ADDRESS) { + int family = address->ifa_family; + if (family == AF_INET || family == AF_INET6) { + ifa_index = address->ifa_index; + ifa_addr.reset(toSocketAddress(family, RTA_DATA(rta), RTA_PAYLOAD(rta))); + ifa_netmask.reset(toNetmask(family, address->ifa_prefixlen)); + } + } + rta = RTA_NEXT(rta, ifaPayloadLength); + } + + // Did we get a usable address? If so, thread it on our list. + if (ifa_index != -1) { + *result = new ifaddrs(*result, ifa_addr.release(), ifa_netmask.release()); + if (!setNameAndFlagsByIndex(*result, ifa_index)) { + return -1; + } + } + } + break; + } + } + } + // We only get here if recv fails before we see a NLMSG_DONE. + return -1; +} + +// Source-compatible with the BSD function. +void freeifaddrs(ifaddrs* addresses) { + delete addresses; +} + +#endif diff --git a/luni/src/main/native/ifaddrs-android.h b/luni/src/main/native/ifaddrs-android.h index 446d8d2..c89d08a 100644 --- a/luni/src/main/native/ifaddrs-android.h +++ b/luni/src/main/native/ifaddrs-android.h @@ -17,25 +17,11 @@ #ifndef IFADDRS_ANDROID_H_included #define IFADDRS_ANDROID_H_included -#include <arpa/inet.h> -#include <cstring> -#include <errno.h> -#include <net/if.h> -#include <netinet/in.h> -#include <new> -#include <sys/ioctl.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <stdio.h> -#include <linux/netlink.h> -#include <linux/rtnetlink.h> - -#include "LocalArray.h" -#include "ScopedFd.h" +struct sockaddr; // Android (bionic) doesn't have getifaddrs(3)/freeifaddrs(3). -// We fake it here, so java_net_NetworkInterface.cpp can use that API -// with all the non-portable code being in this file. +// We fake it here, so java_net_NetworkInterface.cpp can use that API. +// This code should move into bionic, though. // Source-compatible subset of the BSD struct. struct ifaddrs { @@ -54,88 +40,9 @@ struct ifaddrs { // Interface netmask. sockaddr* ifa_netmask; - ifaddrs(ifaddrs* next) - : ifa_next(next), ifa_name(NULL), ifa_flags(0), ifa_addr(NULL), ifa_netmask(NULL) - { - } - - ~ifaddrs() { - delete ifa_next; - delete[] ifa_name; - delete ifa_addr; - delete ifa_netmask; - } - - // Sadly, we can't keep the interface index for portability with BSD. - // We'll have to keep the name instead, and re-query the index when - // we need it later. - bool setNameAndFlagsByIndex(int interfaceIndex) { - // Get the name. - char buf[IFNAMSIZ]; - char* name = if_indextoname(interfaceIndex, buf); - if (name == NULL) { - return false; - } - ifa_name = new char[strlen(name) + 1]; - strcpy(ifa_name, name); - - // Get the flags. - ScopedFd fd(socket(AF_INET, SOCK_DGRAM, 0)); - if (fd.get() == -1) { - return false; - } - ifreq ifr; - memset(&ifr, 0, sizeof(ifr)); - strcpy(ifr.ifr_name, name); - int rc = ioctl(fd.get(), SIOCGIFFLAGS, &ifr); - if (rc == -1) { - return false; - } - ifa_flags = ifr.ifr_flags; - return true; - } + ifaddrs(ifaddrs* next, sockaddr* ifa_addr, sockaddr* ifa_netmask); - // Netlink gives us the address family in the header, and the - // sockaddr_in or sockaddr_in6 bytes as the payload. We need to - // stitch the two bits together into the sockaddr that's part of - // our portable interface. - void setAddress(int family, void* data, size_t byteCount) { - // Set the address proper... - sockaddr_storage* ss = new sockaddr_storage; - memset(ss, 0, sizeof(*ss)); - ifa_addr = reinterpret_cast<sockaddr*>(ss); - ss->ss_family = family; - uint8_t* dst = sockaddrBytes(family, ss); - memcpy(dst, data, byteCount); - } - - // Netlink gives us the prefix length as a bit count. We need to turn - // that into a BSD-compatible netmask represented by a sockaddr*. - void setNetmask(int family, size_t prefixLength) { - // ...and work out the netmask from the prefix length. - sockaddr_storage* ss = new sockaddr_storage; - memset(ss, 0, sizeof(*ss)); - ifa_netmask = reinterpret_cast<sockaddr*>(ss); - ss->ss_family = family; - uint8_t* dst = sockaddrBytes(family, ss); - memset(dst, 0xff, prefixLength / 8); - if ((prefixLength % 8) != 0) { - dst[prefixLength/8] = (0xff << (8 - (prefixLength % 8))); - } - } - - // Returns a pointer to the first byte in the address data (which is - // stored in network byte order). - uint8_t* sockaddrBytes(int family, sockaddr_storage* ss) { - if (family == AF_INET) { - sockaddr_in* ss4 = reinterpret_cast<sockaddr_in*>(ss); - return reinterpret_cast<uint8_t*>(&ss4->sin_addr); - } else if (family == AF_INET6) { - sockaddr_in6* ss6 = reinterpret_cast<sockaddr_in6*>(ss); - return reinterpret_cast<uint8_t*>(&ss6->sin6_addr); - } - return NULL; - } + ~ifaddrs(); private: // Disallow copy and assignment. @@ -143,86 +50,7 @@ private: void operator=(const ifaddrs&); }; -// FIXME: use iovec instead. -struct addrReq_struct { - nlmsghdr netlinkHeader; - ifaddrmsg msg; -}; - -inline bool sendNetlinkMessage(int fd, const void* data, size_t byteCount) { - ssize_t sentByteCount = TEMP_FAILURE_RETRY(send(fd, data, byteCount, 0)); - return (sentByteCount == static_cast<ssize_t>(byteCount)); -} - -inline ssize_t recvNetlinkMessage(int fd, char* buf, size_t byteCount) { - return TEMP_FAILURE_RETRY(recv(fd, buf, byteCount, 0)); -} - -// Source-compatible with the BSD function. -inline int getifaddrs(ifaddrs** result) { - // Simplify cleanup for callers. - *result = NULL; - - // Create a netlink socket. - ScopedFd fd(socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)); - if (fd.get() < 0) { - return -1; - } - - // Ask for the address information. - addrReq_struct addrRequest; - memset(&addrRequest, 0, sizeof(addrRequest)); - addrRequest.netlinkHeader.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH; - addrRequest.netlinkHeader.nlmsg_type = RTM_GETADDR; - addrRequest.netlinkHeader.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(addrRequest))); - addrRequest.msg.ifa_family = AF_UNSPEC; // All families. - addrRequest.msg.ifa_index = 0; // All interfaces. - if (!sendNetlinkMessage(fd.get(), &addrRequest, addrRequest.netlinkHeader.nlmsg_len)) { - return -1; - } - - // Read the responses. - LocalArray<0> buf(65536); // We don't necessarily have std::vector. - ssize_t bytesRead; - while ((bytesRead = recvNetlinkMessage(fd.get(), &buf[0], buf.size())) > 0) { - nlmsghdr* hdr = reinterpret_cast<nlmsghdr*>(&buf[0]); - for (; NLMSG_OK(hdr, (size_t)bytesRead); hdr = NLMSG_NEXT(hdr, bytesRead)) { - switch (hdr->nlmsg_type) { - case NLMSG_DONE: - return 0; - case NLMSG_ERROR: - return -1; - case RTM_NEWADDR: - { - ifaddrmsg* address = reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(hdr)); - rtattr* rta = IFA_RTA(address); - size_t ifaPayloadLength = IFA_PAYLOAD(hdr); - while (RTA_OK(rta, ifaPayloadLength)) { - if (rta->rta_type == IFA_LOCAL) { - int family = address->ifa_family; - if (family == AF_INET || family == AF_INET6) { - *result = new ifaddrs(*result); - if (!(*result)->setNameAndFlagsByIndex(address->ifa_index)) { - return -1; - } - (*result)->setAddress(family, RTA_DATA(rta), RTA_PAYLOAD(rta)); - (*result)->setNetmask(family, address->ifa_prefixlen); - } - } - rta = RTA_NEXT(rta, ifaPayloadLength); - } - } - break; - } - } - } - // We only get here if recv fails before we see a NLMSG_DONE. - return -1; -} - -// Source-compatible with the BSD function. -inline void freeifaddrs(ifaddrs* addresses) { - delete addresses; -} +int getifaddrs(ifaddrs** result); +void freeifaddrs(ifaddrs* addresses); #endif // IFADDRS_ANDROID_H_included diff --git a/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp b/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp index 48f1a99..03d1e69 100644 --- a/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp +++ b/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp @@ -20,7 +20,6 @@ #include "JNIHelp.h" #include "JniConstants.h" #include "JniException.h" -#include "LocalArray.h" #include "NetFd.h" #include "NetworkUtilities.h" #include "ScopedPrimitiveArray.h" diff --git a/luni/src/main/native/sub.mk b/luni/src/main/native/sub.mk index 85f3a4c..4f94dd1 100644 --- a/luni/src/main/native/sub.mk +++ b/luni/src/main/native/sub.mk @@ -23,6 +23,7 @@ LOCAL_SRC_FILES := \ Register.cpp \ TimeZones.cpp \ cbigint.cpp \ + ifaddrs-android.cpp \ java_io_Console.cpp \ java_io_File.cpp \ java_io_FileDescriptor.cpp \ diff --git a/luni/src/test/java/libcore/java/net/NetworkInterfaceTest.java b/luni/src/test/java/libcore/java/net/NetworkInterfaceTest.java new file mode 100644 index 0000000..fe89adb --- /dev/null +++ b/luni/src/test/java/libcore/java/net/NetworkInterfaceTest.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package libcore.java.net; + +import java.net.*; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import junit.framework.TestCase; + +public class NetworkInterfaceTest extends TestCase { + // http://code.google.com/p/android/issues/detail?id=13784 + public void testIPv6() throws Exception { + NetworkInterface lo = NetworkInterface.getByName("lo"); + Set<InetAddress> actual = new HashSet<InetAddress>(Collections.list(lo.getInetAddresses())); + + Set<InetAddress> expected = new HashSet<InetAddress>(); + expected.add(Inet4Address.LOOPBACK); + expected.add(Inet6Address.getByAddress("localhost", new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, null)); + + assertEquals(expected, actual); + } +} |