From 691dc6e106dcc364f2cea9859656ff1aa523b9a4 Mon Sep 17 00:00:00 2001 From: agx Date: Tue, 14 May 2013 10:17:32 +0200 Subject: Simplify NetworkInterface.c --- debian/patches/kfreebsd-support-jdk.diff | 442 ++++--------------------------- 1 file changed, 48 insertions(+), 394 deletions(-) diff --git a/debian/patches/kfreebsd-support-jdk.diff b/debian/patches/kfreebsd-support-jdk.diff index fc270aa..b7230ff 100644 --- a/debian/patches/kfreebsd-support-jdk.diff +++ b/debian/patches/kfreebsd-support-jdk.diff @@ -846,358 +846,6 @@ index ec2427d..a7687a7 100644 case EINVAL: /* * On some Linuxes, when bound to the loopback interface, connect -diff --git openjdk/jdk/src/solaris/native/java/net/NetworkInterface.c openjdk/jdk/src/solaris/native/java/net/NetworkInterface.c -index 04c723c..bfd9d79 100644 ---- openjdk/jdk/src/solaris/native/java/net/NetworkInterface.c -+++ openjdk/jdk/src/solaris/native/java/net/NetworkInterface.c -@@ -69,6 +69,17 @@ - #endif - #endif - -+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#endif -+ - #include "jvm.h" - #include "jni_util.h" - #include "net_util.h" -@@ -1174,7 +1185,11 @@ static int getIndex(int sock, const char *name){ - return -1; - } - -+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) -+ return if2.ifr_index; -+#else - return if2.ifr_ifindex; -+#endif - } - - /** -@@ -1675,6 +1690,304 @@ static int getFlags(int sock, const char *ifname) { - return lifr.lifr_flags; - } - -+/** BSD **/ -+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) -+/* Open socket for further ioct calls, try v4 socket first and -+ * if it falls return v6 socket -+ */ -+ -+#ifdef AF_INET6 -+static int openSocketWithFallback(JNIEnv *env, const char *ifname){ -+ int sock; -+ struct ifreq if2; -+ -+ if ((sock = JVM_Socket(AF_INET, SOCK_DGRAM, 0)) < 0) { -+ if (errno == EPROTONOSUPPORT){ -+ if ( (sock = JVM_Socket(AF_INET6, SOCK_DGRAM, 0)) < 0 ){ -+ NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV6 Socket creation failed"); -+ return -1; -+ } -+ } -+ else{ // errno is not NOSUPPORT -+ NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", "IPV4 Socket creation failed"); -+ return -1; -+ } -+ } -+ -+ return sock; -+} -+ -+#else -+static int openSocketWithFallback(JNIEnv *env, const char *ifname){ -+ return openSocket(env,AF_INET); -+} -+#endif -+ -+/* -+ * Enumerates and returns all IPv4 interfaces -+ */ -+static netif *enumIPv4Interfaces(JNIEnv *env, int sock, netif *ifs) { -+ struct ifaddrs *ifa, *origifa; -+ -+ if (getifaddrs(&origifa) != 0) { -+ NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", -+ "getifaddrs() function failed"); -+ return ifs; -+ } -+ -+ for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) { -+ -+ /* -+ * Skip non-AF_INET entries. -+ */ -+ if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET) -+ continue; -+ -+ /* -+ * Add to the list. -+ */ -+ ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr, AF_INET, 0); -+ -+ /* -+ * If an exception occurred then free the list. -+ */ -+ if ((*env)->ExceptionOccurred(env)) { -+ freeifaddrs(origifa); -+ freeif(ifs); -+ return NULL; -+ } -+ } -+ -+ /* -+ * Free socket and buffer -+ */ -+ freeifaddrs(origifa); -+ return ifs; -+} -+ -+ -+/* -+ * Enumerates and returns all IPv6 interfaces on Linux -+ */ -+ -+#ifdef AF_INET6 -+/* -+ * Determines the prefix on BSD for IPv6 interfaces. -+ */ -+static -+int prefix(void *val, int size) { -+ u_char *name = (u_char *)val; -+ int byte, bit, plen = 0; -+ -+ for (byte = 0; byte < size; byte++, plen += 8) -+ if (name[byte] != 0xff) -+ break; -+ if (byte == size) -+ return (plen); -+ for (bit = 7; bit != 0; bit--, plen++) -+ if (!(name[byte] & (1 << bit))) -+ break; -+ for (; bit != 0; bit--) -+ if (name[byte] & (1 << bit)) -+ return (0); -+ byte++; -+ for (; byte < size; byte++) -+ if (name[byte]) -+ return (0); -+ return (plen); -+} -+ -+/* -+ * Enumerates and returns all IPv6 interfaces on BSD -+ */ -+static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) { -+ struct ifaddrs *ifa, *origifa; -+ struct sockaddr_in6 *sin6; -+ struct in6_ifreq ifr6; -+ -+ if (getifaddrs(&origifa) != 0) { -+ NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", -+ "getifaddrs() function failed"); -+ return ifs; -+ } -+ -+ for (ifa = origifa; ifa != NULL; ifa = ifa->ifa_next) { -+ -+ /* -+ * Skip non-AF_INET6 entries. -+ */ -+ if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET6) -+ continue; -+ -+ memset(&ifr6, 0, sizeof(ifr6)); -+ strcpy(ifr6.ifr_name, ifa->ifa_name); -+ memcpy(&ifr6.ifr_addr, ifa->ifa_addr, MIN(sizeof(ifr6.ifr_addr), ifa->ifa_addr->sa_len)); -+ -+ if (ioctl(sock, SIOCGIFNETMASK_IN6, (caddr_t)&ifr6) < 0) { -+ NET_ThrowByNameWithLastError(env , JNU_JAVANETPKG "SocketException", -+ "ioctl SIOCGIFNETMASK_IN6 failed"); -+ freeifaddrs(origifa); -+ freeif(ifs); -+ return NULL; -+ } -+ -+ /* Add to the list. */ -+ sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr; -+ ifs = addif(env, sock, ifa->ifa_name, ifs, ifa->ifa_addr, AF_INET6, -+ prefix(&sin6->sin6_addr, sizeof(struct in6_addr))); -+ -+ /* If an exception occurred then free the list. */ -+ if ((*env)->ExceptionOccurred(env)) { -+ freeifaddrs(origifa); -+ freeif(ifs); -+ return NULL; -+ } -+ } -+ -+ /* -+ * Free socket and ifaddrs buffer -+ */ -+ freeifaddrs(origifa); -+ return ifs; -+} -+#endif -+ -+static int getIndex(int sock, const char *name){ -+ /* -+ * Try to get the interface index -+ * (Not supported on Solaris 2.6 or 7) -+ */ -+ struct ifreq if2; -+ strcpy(if2.ifr_name, name); -+ -+ if (ioctl(sock, SIOCGIFINDEX, (char *)&if2) < 0) { -+ return -1; -+ } -+ -+ return if2.ifr_index; -+} -+ -+/** -+ * Returns the IPv4 broadcast address of a named interface, if it exists. -+ * Returns 0 if it doesn't have one. -+ */ -+static struct sockaddr *getBroadcast(JNIEnv *env, int sock, const char *ifname, struct sockaddr *brdcast_store) { -+ struct sockaddr *ret = NULL; -+ struct ifreq if2; -+ -+ memset((char *) &if2, 0, sizeof(if2)); -+ strcpy(if2.ifr_name, ifname); -+ -+ /* Let's make sure the interface does have a broadcast address */ -+ if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0) { -+ NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFFLAGS failed"); -+ return ret; -+ } -+ -+ if (if2.ifr_flags & IFF_BROADCAST) { -+ /* It does, let's retrieve it*/ -+ if (ioctl(sock, SIOCGIFBRDADDR, (char *)&if2) < 0) { -+ NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFBRDADDR failed"); -+ return ret; -+ } -+ -+ ret = brdcast_store; -+ memcpy(ret, &if2.ifr_broadaddr, sizeof(struct sockaddr)); -+ } -+ -+ return ret; -+} -+ -+/** -+ * Returns the IPv4 subnet prefix length (aka subnet mask) for the named -+ * interface, if it has one, otherwise return -1. -+ */ -+static short getSubnet(JNIEnv *env, int sock, const char *ifname) { -+ unsigned int mask; -+ short ret; -+ struct ifreq if2; -+ -+ memset((char *) &if2, 0, sizeof(if2)); -+ strcpy(if2.ifr_name, ifname); -+ -+ if (ioctl(sock, SIOCGIFNETMASK, (char *)&if2) < 0) { -+ NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFNETMASK failed"); -+ return -1; -+ } -+ -+ mask = ntohl(((struct sockaddr_in*)&(if2.ifr_addr))->sin_addr.s_addr); -+ ret = 0; -+ while (mask) { -+ mask <<= 1; -+ ret++; -+ } -+ -+ return ret; -+} -+ -+/** -+ * Get the Hardware address (usually MAC address) for the named interface. -+ * return puts the data in buf, and returns the length, in byte, of the -+ * MAC address. Returns -1 if there is no hardware address on that interface. -+ */ -+static int getMacAddress(JNIEnv *env, int sock, const char* ifname, const struct in_addr* addr, unsigned char *buf) { -+ struct ifaddrs *ifa0, *ifa; -+ struct sockaddr *saddr; -+ int i; -+ -+ /* Grab the interface list */ -+ if (!getifaddrs(&ifa0)) { -+ /* Cycle through the interfaces */ -+ for (i = 0, ifa = ifa0; ifa != NULL; ifa = ifa->ifa_next, i++) { -+ saddr = ifa->ifa_addr; -+ /* Link layer contains the MAC address */ -+ if (saddr->sa_family == AF_LINK && !strcmp(ifname, ifa->ifa_name)) { -+ struct sockaddr_dl *sadl = (struct sockaddr_dl *) saddr; -+ /* Check the address is the correct length */ -+ if (sadl->sdl_alen == ETHER_ADDR_LEN) { -+ memcpy(buf, (sadl->sdl_data + sadl->sdl_nlen), ETHER_ADDR_LEN); -+ freeifaddrs(ifa0); -+ return ETHER_ADDR_LEN; -+ } -+ } -+ } -+ freeifaddrs(ifa0); -+ } -+ -+ return -1; -+} -+ -+static int getMTU(JNIEnv *env, int sock, const char *ifname) { -+ struct ifreq if2; -+ -+ memset((char *) &if2, 0, sizeof(if2)); -+ strcpy(if2.ifr_name, ifname); -+ -+ if (ioctl(sock, SIOCGIFMTU, (char *)&if2) < 0) { -+ NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "IOCTL SIOCGIFMTU failed"); -+ return -1; -+ } -+ -+ return if2.ifr_mtu; -+} -+ -+static int getFlags(int sock, const char *ifname) { -+ struct ifreq if2; -+ int ret = -1; -+ -+ memset((char *) &if2, 0, sizeof(if2)); -+ strcpy(if2.ifr_name, ifname); -+ -+ if (ioctl(sock, SIOCGIFFLAGS, (char *)&if2) < 0){ -+ return -1; -+ } -+ -+ return if2.ifr_flags; -+} -+ -+#endif -+ -+ - - #endif - -diff --git openjdk/jdk/src/solaris/native/java/net/NetworkInterface.c.orig openjdk/jdk/src/solaris/native/java/net/NetworkInterface.c.orig -index 6cddea3..04c723c 100644 ---- openjdk/jdk/src/solaris/native/java/net/NetworkInterface.c.orig -+++ openjdk/jdk/src/solaris/native/java/net/NetworkInterface.c.orig -@@ -1127,7 +1127,7 @@ static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) { - uint8_t ipv6addr[16]; - - if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) { -- while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n", -+ while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %x %x %x %x %20s\n", - addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4], addr6p[5], addr6p[6], addr6p[7], - &if_idx, &plen, &scope, &dad_status, devname) != EOF) { - diff --git openjdk/jdk/src/solaris/native/java/net/PlainDatagramSocketImpl.c openjdk/jdk/src/solaris/native/java/net/PlainDatagramSocketImpl.c index 382ec4c..81ffc41 100644 --- openjdk/jdk/src/solaris/native/java/net/PlainDatagramSocketImpl.c @@ -2322,12 +1970,11 @@ index 25973d9..3fd1f3f 100644 int dbgsysTlsAlloc() { pthread_key_t key; --- -1.7.10.4 - ---- openjdk/jdk/src/solaris/native/java/net/NetworkInterface.c.orig 2013-05-13 22:15:57.256306117 +0200 -+++ openjdk/jdk/src/solaris/native/java/net/NetworkInterface.c 2013-05-13 22:25:08.558318954 +0200 -@@ -56,7 +56,7 @@ +diff --git a/src/solaris/native/java/net/NetworkInterface.c b/src/solaris/native/java/net/NetworkInterface.c +index de94820..9b4ff93 100644 +--- openjdk/jdk/src/solaris/native/java/net/NetworkInterface.c.orig ++++ openjdk/jdk/src/solaris/native/java/net/NetworkInterface.c +@@ -56,17 +56,17 @@ #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6" #endif @@ -2336,52 +1983,59 @@ index 25973d9..3fd1f3f 100644 #include #include #include -@@ -69,17 +69,6 @@ +-#if defined(__APPLE__) ++#if defined(__APPLE__) || defined(__FreeBSD_kernel__) + #include + #include + #include + #include +-#include #endif ++#include #endif --#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) --#include --#include --#include --#include --#include --#include --#include --#include --#endif -- #include "jvm.h" - #include "jni_util.h" - #include "net_util.h" -@@ -1180,11 +1169,7 @@ - return -1; - } +@@ -1590,7 +1590,11 @@ static int getMacAddress(JNIEnv *env, int sock, const char *ifname, const struc + * try the old way. + */ + memset(&lif, 0, sizeof(lif)); ++#if defined(__FreeBSD_kernel__) ++ strcpy(lif.lifr_name, ifname); ++#else + strlcpy(lif.lifr_name, ifname, sizeof(lif.lifr_name)); ++#endif --#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) -- return if2.ifr_index; --#else - return if2.ifr_ifindex; --#endif - } + if (ioctl(sock, SIOCGLIFHWADDR, &lif) != -1) { + struct sockaddr_dl *sp; +@@ -1675,7 +1679,7 @@ static int getFlags(int sock, const char *ifname) { - /** -@@ -1686,7 +1671,7 @@ - } /** BSD **/ --#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) -+#ifdef _ALLBSD_SOURCE || defined(__FreeBSD_kernel__) +-#ifdef _ALLBSD_SOURCE ++#if defined(_ALLBSD_SOURCE) || defined(__FreeBSD_kernel__) /* Open socket for further ioct calls, try v4 socket first and * if it falls return v6 socket */ -@@ -1986,9 +1971,6 @@ +@@ -1804,7 +1808,11 @@ static netif *enumIPv6Interfaces(JNIEnv *env, int sock, netif *ifs) { + continue; - #endif + memset(&ifr6, 0, sizeof(ifr6)); ++#if defined(__FreeBSD_kernel__) ++ strcpy(ifr6.ifr_name, ifa->ifa_name); ++#else + strlcpy(ifr6.ifr_name, ifa->ifa_name, sizeof(ifr6.ifr_name)); ++#endif + memcpy(&ifr6.ifr_addr, ifa->ifa_addr, MIN(sizeof(ifr6.ifr_addr), ifa->ifa_addr->sa_len)); + + if (ioctl(sock, SIOCGIFNETMASK_IN6, (caddr_t)&ifr6) < 0) { +--- openjdk/jdk/src/solaris/native/com/sun/management/UnixOperatingSystem_md.c.orig 2013-05-14 16:34:07.922316686 +0200 ++++ openjdk/jdk/src/solaris/native/com/sun/management/UnixOperatingSystem_md.c 2013-05-14 16:35:22.598286385 +0200 +@@ -212,7 +212,7 @@ + + JVM_Close(fd); + return (jlong) psinfo.pr_size * 1024; +-#elif defined(__linux__) ++#elif defined(__linux__) || defined(__FreeBSD_kernel__) + FILE *fp; + unsigned long vsize = 0; -- --/** BSD **/ --#ifdef _ALLBSD_SOURCE - /* Open socket for further ioct calls, try v4 socket first and - * if it falls return v6 socket - */ -- cgit v1.2.3