aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjccollet <none@none>2008-08-25 14:38:20 +0200
committerjccollet <none@none>2008-08-25 14:38:20 +0200
commitf9cd8d961b5e3440a9a386a90f23f58769e45165 (patch)
tree23a2e674d93f35521f6370dc2f6a78df154bb645
parent9640c27fd830b24a00867c5254d5256bffad42d6 (diff)
6717876: Make java.net.NetworkInterface.getIndex() public
Summary: Make getIndex() and getByIndex() public. Required a name change in native code Reviewed-by: alanb, chegar, michaelm
-rw-r--r--make/java/net/mapfile-vers2
-rw-r--r--src/share/classes/java/net/NetworkInterface.java30
-rw-r--r--src/solaris/native/java/net/NetworkInterface.c4
-rw-r--r--src/solaris/native/java/net/PlainDatagramSocketImpl.c2
-rw-r--r--src/windows/native/java/net/NetworkInterface.c6
-rw-r--r--src/windows/native/java/net/NetworkInterface_winXP.c4
-rw-r--r--src/windows/native/java/net/TwoStacksPlainDatagramSocketImpl.c2
-rw-r--r--src/windows/native/java/net/net_util_md.h2
-rw-r--r--test/java/net/NetworkInterface/IndexTest.java58
9 files changed, 92 insertions, 18 deletions
diff --git a/make/java/net/mapfile-vers b/make/java/net/mapfile-vers
index 86cb351c9..e7da6186e 100644
--- a/make/java/net/mapfile-vers
+++ b/make/java/net/mapfile-vers
@@ -57,7 +57,7 @@ SUNWprivate_1.1 {
Java_java_net_Inet6AddressImpl_isReachable0;
Java_java_net_NetworkInterface_init;
Java_java_net_NetworkInterface_getByName0;
- Java_java_net_NetworkInterface_getByIndex;
+ Java_java_net_NetworkInterface_getByIndex0;
Java_java_net_NetworkInterface_getByInetAddress0;
Java_java_net_NetworkInterface_getAll;
Java_java_net_NetworkInterface_isUp0;
diff --git a/src/share/classes/java/net/NetworkInterface.java b/src/share/classes/java/net/NetworkInterface.java
index 7852fa363..a4f4bc9c6 100644
--- a/src/share/classes/java/net/NetworkInterface.java
+++ b/src/share/classes/java/net/NetworkInterface.java
@@ -203,11 +203,17 @@ public final class NetworkInterface {
}
/**
- * Get the index of this network interface.
+ * Returns the index of this network interface. The index is an integer greater
+ * or equal to zero, or {@code -1} for unknown. This is a system specific value
+ * and interfaces with the same name can have different indexes on different
+ * machines.
*
- * @return the index of this network interface
+ * @return the index of this network interface or {@code -1} if the index is
+ * unknown
+ * @see #getByIndex(int)
+ * @since 1.7
*/
- int getIndex() {
+ public int getIndex() {
return index;
}
@@ -249,11 +255,18 @@ public final class NetworkInterface {
* Get a network interface given its index.
*
* @param index an integer, the index of the interface
- * @return the NetworkInterface obtained from its index
- * @exception SocketException if an I/O error occurs.
+ * @return the NetworkInterface obtained from its index, or {@code null} if
+ * there is no interface with such an index on the system
+ * @throws SocketException if an I/O error occurs.
+ * @throws IllegalArgumentException if index has a negative value
+ * @see #getIndex()
+ * @since 1.7
*/
- native static NetworkInterface getByIndex(int index)
- throws SocketException;
+ public static NetworkInterface getByIndex(int index) throws SocketException {
+ if (index < 0)
+ throw new IllegalArgumentException("Interface index can't be negative");
+ return getByIndex0(index);
+ }
/**
* Convenience method to search for a network interface that
@@ -325,6 +338,9 @@ public final class NetworkInterface {
private native static NetworkInterface getByName0(String name)
throws SocketException;
+ private native static NetworkInterface getByIndex0(int index)
+ throws SocketException;
+
private native static NetworkInterface getByInetAddress0(InetAddress addr)
throws SocketException;
diff --git a/src/solaris/native/java/net/NetworkInterface.c b/src/solaris/native/java/net/NetworkInterface.c
index a54174483..14273f5c1 100644
--- a/src/solaris/native/java/net/NetworkInterface.c
+++ b/src/solaris/native/java/net/NetworkInterface.c
@@ -206,10 +206,10 @@ JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
/*
* Class: java_net_NetworkInterface
- * Method: getByIndex
+ * Method: getByIndex0
* Signature: (Ljava/lang/String;)Ljava/net/NetworkInterface;
*/
-JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex
+JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0
(JNIEnv *env, jclass cls, jint index) {
netif *ifs, *curr;
diff --git a/src/solaris/native/java/net/PlainDatagramSocketImpl.c b/src/solaris/native/java/net/PlainDatagramSocketImpl.c
index 7c68f7205..dd3895cdb 100644
--- a/src/solaris/native/java/net/PlainDatagramSocketImpl.c
+++ b/src/solaris/native/java/net/PlainDatagramSocketImpl.c
@@ -1741,7 +1741,7 @@ jobject getMulticastInterface(JNIEnv *env, jobject this, int fd, jint opt) {
* (for IF).
*/
if (index > 0) {
- ni = Java_java_net_NetworkInterface_getByIndex(env, ni_class,
+ ni = Java_java_net_NetworkInterface_getByIndex0(env, ni_class,
index);
if (ni == NULL) {
char errmsg[255];
diff --git a/src/windows/native/java/net/NetworkInterface.c b/src/windows/native/java/net/NetworkInterface.c
index cccf7d440..b59595c7e 100644
--- a/src/windows/native/java/net/NetworkInterface.c
+++ b/src/windows/native/java/net/NetworkInterface.c
@@ -762,17 +762,17 @@ JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0
/*
* Class: NetworkInterface
- * Method: getByIndex
+ * Method: getByIndex0
* Signature: (I)LNetworkInterface;
*/
-JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex
+JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0
(JNIEnv *env, jclass cls, jint index)
{
netif *ifList, *curr;
jobject netifObj = NULL;
if (os_supports_ipv6 && ipv6_available()) {
- return Java_java_net_NetworkInterface_getByIndex_XP (env, cls, index);
+ return Java_java_net_NetworkInterface_getByIndex0_XP (env, cls, index);
}
/* get the list of interfaces */
diff --git a/src/windows/native/java/net/NetworkInterface_winXP.c b/src/windows/native/java/net/NetworkInterface_winXP.c
index 87eb2eb9a..e2d878d78 100644
--- a/src/windows/native/java/net/NetworkInterface_winXP.c
+++ b/src/windows/native/java/net/NetworkInterface_winXP.c
@@ -576,10 +576,10 @@ JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0_XP
/*
* Class: NetworkInterface
- * Method: getByIndex
+ * Method: getByIndex0_XP
* Signature: (I)LNetworkInterface;
*/
-JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex_XP
+JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0_XP
(JNIEnv *env, jclass cls, jint index)
{
netif *ifList, *curr;
diff --git a/src/windows/native/java/net/TwoStacksPlainDatagramSocketImpl.c b/src/windows/native/java/net/TwoStacksPlainDatagramSocketImpl.c
index 4ada9f964..ddb7a477e 100644
--- a/src/windows/native/java/net/TwoStacksPlainDatagramSocketImpl.c
+++ b/src/windows/native/java/net/TwoStacksPlainDatagramSocketImpl.c
@@ -2090,7 +2090,7 @@ jobject getMulticastInterface(JNIEnv *env, jobject this, int fd, int fd1, jint o
* (for IF).
*/
if (index > 0) {
- ni = Java_java_net_NetworkInterface_getByIndex(env, ni_class,
+ ni = Java_java_net_NetworkInterface_getByIndex0(env, ni_class,
index);
if (ni == NULL) {
char errmsg[255];
diff --git a/src/windows/native/java/net/net_util_md.h b/src/windows/native/java/net/net_util_md.h
index 9b237c4dc..5b3ae84e4 100644
--- a/src/windows/native/java/net/net_util_md.h
+++ b/src/windows/native/java/net/net_util_md.h
@@ -329,7 +329,7 @@ extern jint NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout);
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByName0_XP
(JNIEnv *env, jclass cls, jstring name);
-JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex_XP
+JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByIndex0_XP
(JNIEnv *env, jclass cls, jint index);
JNIEXPORT jobject JNICALL Java_java_net_NetworkInterface_getByInetAddress0_XP
diff --git a/test/java/net/NetworkInterface/IndexTest.java b/test/java/net/NetworkInterface/IndexTest.java
new file mode 100644
index 000000000..fd6e4780d
--- /dev/null
+++ b/test/java/net/NetworkInterface/IndexTest.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+ * @bug 6717876
+ * @summary Make java.net.NetworkInterface.getIndex() public
+ */
+
+import java.net.*;
+import java.util.Enumeration;
+
+public class IndexTest {
+ public static void main(String[] args) throws Exception {
+ Enumeration<NetworkInterface> netifs = NetworkInterface.getNetworkInterfaces();
+ NetworkInterface nif = null;
+ while (netifs.hasMoreElements()) {
+ nif = netifs.nextElement();
+ int index = nif.getIndex();
+ if (index >= 0) {
+ NetworkInterface nif2 = NetworkInterface.getByIndex(index);
+ if (! nif.equals(nif2)) {
+ throw new RuntimeException("both interfaces should be equal");
+ }
+ }
+ }
+ try {
+ nif = NetworkInterface.getByIndex(-1);
+ throw new RuntimeException("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // OK
+ }
+ // In all likelyhood, this interface should not exist.
+ nif = NetworkInterface.getByIndex(Integer.MAX_VALUE - 1);
+ if (nif != null) {
+ throw new RuntimeException("getByIndex() should have returned null");
+ }
+ }
+}