src/java.base/share/classes/java/net/AbstractPlainDatagramSocketImpl.java
changeset 55375 96c7427456f9
parent 55081 dd321e3596c0
child 55693 9a97b1393e72
--- a/src/java.base/share/classes/java/net/AbstractPlainDatagramSocketImpl.java	Thu Jun 13 12:22:28 2019 +0530
+++ b/src/java.base/share/classes/java/net/AbstractPlainDatagramSocketImpl.java	Thu Jun 13 09:10:51 2019 +0100
@@ -33,6 +33,7 @@
 
 import sun.net.ResourceManager;
 import sun.net.ext.ExtendedSocketOptions;
+import sun.net.util.IPAddressUtil;
 import sun.security.action.GetPropertyAction;
 
 /**
@@ -110,6 +111,9 @@
      */
     protected synchronized void bind(int lport, InetAddress laddr)
         throws SocketException {
+        if (laddr.isLinkLocalAddress()) {
+            laddr = IPAddressUtil.toScopedAddress(laddr);
+        }
         bind0(lport, laddr);
     }
 
@@ -121,7 +125,19 @@
      * destination address to send the packet to.
      * @param p the packet to be sent.
      */
-    protected abstract void send(DatagramPacket p) throws IOException;
+    protected void send(DatagramPacket p) throws IOException {
+        InetAddress orig = p.getAddress();
+        if (orig.isLinkLocalAddress()) {
+            InetAddress scoped = IPAddressUtil.toScopedAddress(orig);
+            if (orig != scoped) {
+                p = new DatagramPacket(p.getData(), p.getOffset(),
+                                       p.getLength(), scoped, p.getPort());
+            }
+        }
+        send0(p);
+    }
+
+    protected abstract void send0(DatagramPacket p) throws IOException;
 
     /**
      * Connects a datagram socket to a remote destination. This associates the remote
@@ -131,6 +147,9 @@
      * @param port the remote port number
      */
     protected void connect(InetAddress address, int port) throws SocketException {
+        if (address.isLinkLocalAddress()) {
+            address = IPAddressUtil.toScopedAddress(address);
+        }
         connect0(address, port);
         connectedAddress = address;
         connectedPort = port;