4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
authoralanb
Fri, 18 Jun 2010 16:16:51 +0100
changeset 5807 d34ed576e234
parent 5806 94be45a27f37
child 5808 3a1f603c5ca7
4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info Reviewed-by: chegar
jdk/src/share/classes/sun/nio/ch/DatagramSocketAdaptor.java
jdk/test/java/nio/channels/DatagramChannel/AdaptDatagramSocket.java
--- a/jdk/src/share/classes/sun/nio/ch/DatagramSocketAdaptor.java	Thu Jun 17 14:42:30 2010 -0700
+++ b/jdk/src/share/classes/sun/nio/ch/DatagramSocketAdaptor.java	Fri Jun 18 16:16:51 2010 +0100
@@ -171,10 +171,9 @@
 
     // Must hold dc.blockingLock()
     //
-    private void receive(ByteBuffer bb) throws IOException {
+    private SocketAddress receive(ByteBuffer bb) throws IOException {
         if (timeout == 0) {
-            dc.receive(bb);
-            return;
+            return dc.receive(bb);
         }
 
         // Implement timeout with a selector
@@ -183,8 +182,9 @@
         dc.configureBlocking(false);
         try {
             int n;
-            if (dc.receive(bb) != null)
-                return;
+            SocketAddress sender;
+            if ((sender = dc.receive(bb)) != null)
+                return sender;
             sel = Util.getTemporarySelector(dc);
             sk = dc.register(sel, SelectionKey.OP_READ);
             long to = timeout;
@@ -194,8 +194,8 @@
                 long st = System.currentTimeMillis();
                 int ns = sel.select(to);
                 if (ns > 0 && sk.isReadable()) {
-                    if (dc.receive(bb) != null)
-                        return;
+                    if ((sender = dc.receive(bb)) != null)
+                        return sender;
                 }
                 sel.selectedKeys().remove(sk);
                 to -= System.currentTimeMillis() - st;
@@ -222,7 +222,8 @@
                     ByteBuffer bb = ByteBuffer.wrap(p.getData(),
                                                     p.getOffset(),
                                                     p.getLength());
-                    receive(bb);
+                    SocketAddress sender = receive(bb);
+                    p.setSocketAddress(sender);
                     p.setLength(bb.position() - p.getOffset());
                 }
             } catch (IOException x) {
--- a/jdk/test/java/nio/channels/DatagramChannel/AdaptDatagramSocket.java	Thu Jun 17 14:42:30 2010 -0700
+++ b/jdk/test/java/nio/channels/DatagramChannel/AdaptDatagramSocket.java	Fri Jun 18 16:16:51 2010 +0100
@@ -22,6 +22,7 @@
  */
 
 /* @test
+ * @bug 4313882 4981129
  * @summary Unit test for datagram-socket-channel adaptors
  * @library ..
  */
@@ -93,6 +94,11 @@
             if (ip.getData()[ip.getOffset() + i]
                 != op.getData()[op.getOffset() + i])
                 throw new Exception("Incorrect data received");
+
+        if (!(ip.getSocketAddress().equals(dst))) {
+            throw new Exception("Incorrect sender address, expected: " + dst
+                + " actual: " + ip.getSocketAddress());
+        }
     }
 
     static void test(InetSocketAddress dst,