changeset 1830:3c5c596ee0fd

8195868: Address Internet Addresses Reviewed-by: chegar, rriggs, igerasim, skoivu, rhalade
author igerasim
date Sat, 24 Mar 2018 20:21:43 -0700
parents 7358ced14574
children 93b2632a22c7
files src/share/classes/java/net/InetAddress.java src/share/classes/java/net/NetworkInterface.java src/solaris/native/java/net/NetworkInterface.c
diffstat 3 files changed, 35 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/net/InetAddress.java	Fri Jul 13 11:22:19 2018 -0700
+++ b/src/share/classes/java/net/InetAddress.java	Sat Mar 24 20:21:43 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 2018, Oracle and/or its affiliates. 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
@@ -36,6 +36,7 @@
 import java.io.ObjectStreamException;
 import java.io.ObjectStreamField;
 import java.io.IOException;
+import java.io.InvalidObjectException;
 import java.io.ObjectInputStream;
 import java.io.ObjectInputStream.GetField;
 import java.io.ObjectOutputStream;
@@ -1578,8 +1579,11 @@
         }
         GetField gf = s.readFields();
         String host = (String)gf.get("hostName", null);
-        int address= gf.get("address", 0);
-        int family= gf.get("family", 0);
+        int address = gf.get("address", 0);
+        int family = gf.get("family", 0);
+        if (family != IPv4 && family != IPv6) {
+            throw new InvalidObjectException("invalid address family type: " + family);
+        }
         InetAddressHolder h = new InetAddressHolder(host, address, family);
         UNSAFE.putObject(this, FIELDS_OFFSET, h);
     }
--- a/src/share/classes/java/net/NetworkInterface.java	Fri Jul 13 11:22:19 2018 -0700
+++ b/src/share/classes/java/net/NetworkInterface.java	Sat Mar 24 20:21:43 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2018, Oracle and/or its affiliates. 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
@@ -281,8 +281,20 @@
         if (addr == null) {
             throw new NullPointerException();
         }
-        if (!(addr instanceof Inet4Address || addr instanceof Inet6Address)) {
-            throw new IllegalArgumentException ("invalid address type");
+        if (addr instanceof Inet4Address) {
+            Inet4Address inet4Address = (Inet4Address) addr;
+            if (inet4Address.holder.family != InetAddress.IPv4) {
+                throw new IllegalArgumentException("invalid family type: "
+                        + inet4Address.holder.family);
+            }
+        } else if (addr instanceof Inet6Address) {
+            Inet6Address inet6Address = (Inet6Address) addr;
+            if (inet6Address.holder.family != InetAddress.IPv6) {
+                throw new IllegalArgumentException("invalid family type: "
+                        + inet6Address.holder.family);
+            }
+        } else {
+            throw new IllegalArgumentException("invalid address type: " + addr);
         }
         return getByInetAddress0(addr);
     }
--- a/src/solaris/native/java/net/NetworkInterface.c	Fri Jul 13 11:22:19 2018 -0700
+++ b/src/solaris/native/java/net/NetworkInterface.c	Sat Mar 24 20:21:43 2018 -0700
@@ -276,9 +276,21 @@
     netif *ifs, *curr;
     jobject obj = NULL;
     jboolean match = JNI_FALSE;
-    int family = (getInetAddress_family(env, iaObj) == IPv4) ? AF_INET : AF_INET6;
+#ifdef AF_INET6
+    int family = getInetAddress_family(env, iaObj);
     JNU_CHECK_EXCEPTION_RETURN(env, NULL);
 
+    if (family == IPv4) {
+      family = AF_INET;
+    } else if (family == IPv6) {
+      family = AF_INET6;
+    } else {
+      return NULL; // Invalid family
+    }
+#else
+    int family = AF_INET;
+#endif
+
     ifs = enumInterfaces(env);
     if (ifs == NULL) {
         return NULL;