6060import java .nio .channels .SelectionKey ;
6161import java .nio .channels .spi .AbstractSelectableChannel ;
6262import java .nio .channels .spi .SelectorProvider ;
63- import java .security .AccessController ;
64- import java .security .PrivilegedExceptionAction ;
6563import java .util .Collections ;
6664import java .util .HashMap ;
6765import java .util .HashSet ;
@@ -293,8 +291,7 @@ public DatagramSocket socket() {
293291 public SocketAddress getLocalAddress () throws IOException {
294292 synchronized (stateLock ) {
295293 ensureOpen ();
296- // Perform security check before returning address
297- return Net .getRevealedLocalAddress (localAddress );
294+ return localAddress ;
298295 }
299296 }
300297
@@ -573,24 +570,16 @@ public SocketAddress receive(ByteBuffer dst) throws IOException {
573570 SocketAddress remote = beginRead (blocking , false );
574571 configureSocketNonBlockingIfVirtualThread ();
575572 boolean connected = (remote != null );
576- @ SuppressWarnings ("removal" )
577- SecurityManager sm = System .getSecurityManager ();
578- if (connected || (sm == null )) {
579- // connected or no security manager
580- int n = receive (dst , connected );
581- if (blocking ) {
582- while (IOStatus .okayToRetry (n ) && isOpen ()) {
583- park (Net .POLLIN );
584- n = receive (dst , connected );
585- }
586- }
587- if (n > 0 || (n == 0 && isOpen ())) {
588- // sender address is in socket address buffer
589- sender = sourceSocketAddress ();
573+ int n = receive (dst , connected );
574+ if (blocking ) {
575+ while (IOStatus .okayToRetry (n ) && isOpen ()) {
576+ park (Net .POLLIN );
577+ n = receive (dst , connected );
590578 }
591- } else {
592- // security manager and unconnected
593- sender = untrustedReceive (dst );
579+ }
580+ if (n > 0 || (n == 0 && isOpen ())) {
581+ // sender address is in socket address buffer
582+ sender = sourceSocketAddress ();
594583 }
595584 return sender ;
596585 } finally {
@@ -601,49 +590,6 @@ public SocketAddress receive(ByteBuffer dst) throws IOException {
601590 }
602591 }
603592
604- /**
605- * Receives a datagram into an untrusted buffer. When there is a security
606- * manager set, and the socket is not connected, datagrams have to be received
607- * into a buffer that is not accessible to the user. The datagram is copied
608- * into the user's buffer when the sender address is accepted by the security
609- * manager.
610- */
611- private SocketAddress untrustedReceive (ByteBuffer dst ) throws IOException {
612- @ SuppressWarnings ("removal" )
613- SecurityManager sm = System .getSecurityManager ();
614- assert readLock .isHeldByCurrentThread ()
615- && sm != null && remoteAddress == null ;
616-
617- boolean blocking = isBlocking ();
618- for (;;) {
619- int n ;
620- ByteBuffer bb = Util .getTemporaryDirectBuffer (dst .remaining ());
621- try {
622- n = receive (bb , false );
623- if (n >= 0 ) {
624- // sender address is in socket address buffer
625- InetSocketAddress isa = sourceSocketAddress ();
626- try {
627- sm .checkAccept (isa .getAddress ().getHostAddress (), isa .getPort ());
628- bb .flip ();
629- dst .put (bb );
630- return isa ;
631- } catch (SecurityException se ) {
632- // ignore datagram
633- }
634- }
635- } finally {
636- Util .releaseTemporaryDirectBuffer (bb );
637- }
638-
639- if (blocking && IOStatus .okayToRetry (n ) && isOpen ()) {
640- park (Net .POLLIN );
641- } else {
642- return null ;
643- }
644- }
645- }
646-
647593 /**
648594 * Receives a datagram.
649595 *
@@ -675,58 +621,30 @@ void blockingReceive(DatagramPacket p, long nanos) throws IOException {
675621 bufLength = DatagramPackets .getBufLength (p );
676622 }
677623
678- long startNanos = System .nanoTime ();
679- long remainingNanos = nanos ;
680- SocketAddress sender = null ;
624+ boolean completed = false ;
681625 try {
682626 SocketAddress remote = beginRead (true , false );
683627 boolean connected = (remote != null );
684- do {
685- ByteBuffer dst = tryBlockingReceive (connected , bufLength , remainingNanos );
686-
628+ ByteBuffer dst = tryBlockingReceive (connected , bufLength , nanos );
629+ if (dst != null ) {
687630 // if datagram received then get sender and copy to DatagramPacket
688- if (dst != null ) {
689- try {
690- // sender address is in socket address buffer
691- sender = sourceSocketAddress ();
692-
693- // check sender when security manager set and not connected
694- @ SuppressWarnings ("removal" )
695- SecurityManager sm = System .getSecurityManager ();
696- if (sm != null && !connected ) {
697- InetSocketAddress isa = (InetSocketAddress ) sender ;
698- try {
699- sm .checkAccept (isa .getAddress ().getHostAddress (), isa .getPort ());
700- } catch (SecurityException e ) {
701- sender = null ;
702- }
703- }
704-
705- if (sender != null ) {
706- // copy bytes to the DatagramPacket, and set length and sender
707- synchronized (p ) {
708- // re-read p.bufLength in case DatagramPacket changed
709- int len = Math .min (dst .limit (), DatagramPackets .getBufLength (p ));
710- dst .get (p .getData (), p .getOffset (), len );
711- DatagramPackets .setLength (p , len );
712- p .setSocketAddress (sender );
713- }
714- } else {
715- // need to retry, adjusting timeout if needed
716- if (nanos > 0 ) {
717- remainingNanos = nanos - (System .nanoTime () - startNanos );
718- if (remainingNanos <= 0 ) {
719- throw new SocketTimeoutException ("Receive timed out" );
720- }
721- }
722- }
723- } finally {
724- Util .offerFirstTemporaryDirectBuffer (dst );
631+ try {
632+ SocketAddress sender = sourceSocketAddress ();
633+ synchronized (p ) {
634+ // copy bytes to the DatagramPacket, and set length and sender.
635+ // Need to re-read p.bufLength in case DatagramPacket changed
636+ int len = Math .min (dst .limit (), DatagramPackets .getBufLength (p ));
637+ dst .get (p .getData (), p .getOffset (), len );
638+ DatagramPackets .setLength (p , len );
639+ p .setSocketAddress (sender );
725640 }
641+ } finally {
642+ Util .offerFirstTemporaryDirectBuffer (dst );
726643 }
727- } while (sender == null && isOpen ());
644+ completed = true ;
645+ }
728646 } finally {
729- endRead (true , ( sender != null ) );
647+ endRead (true , completed );
730648 }
731649 } finally {
732650 readLock .unlock ();
@@ -884,16 +802,7 @@ public int send(ByteBuffer src, SocketAddress target)
884802 completed = (n > 0 );
885803 } else {
886804 // not connected
887- @ SuppressWarnings ("removal" )
888- SecurityManager sm = System .getSecurityManager ();
889805 InetAddress ia = isa .getAddress ();
890- if (sm != null ) {
891- if (ia .isMulticastAddress ()) {
892- sm .checkMulticast (ia );
893- } else {
894- sm .checkConnect (ia .getHostAddress (), isa .getPort ());
895- }
896- }
897806 if (ia .isLinkLocalAddress ())
898807 isa = IPAddressUtil .toScopedAddress (isa );
899808 if (isa .getPort () == 0 )
@@ -1344,10 +1253,6 @@ private void bindInternal(SocketAddress local) throws IOException {
13441253 } else {
13451254 isa = Net .checkAddress (local , family );
13461255 }
1347- @ SuppressWarnings ("removal" )
1348- SecurityManager sm = System .getSecurityManager ();
1349- if (sm != null )
1350- sm .checkListen (isa .getPort ());
13511256
13521257 Net .bind (family , fd , isa .getAddress (), isa .getPort ());
13531258 localAddress = Net .localAddress (fd );
@@ -1373,17 +1278,6 @@ public DatagramChannel connect(SocketAddress sa) throws IOException {
13731278 */
13741279 DatagramChannel connect (SocketAddress sa , boolean check ) throws IOException {
13751280 InetSocketAddress isa = Net .checkAddress (sa , family );
1376- @ SuppressWarnings ("removal" )
1377- SecurityManager sm = System .getSecurityManager ();
1378- if (sm != null ) {
1379- InetAddress ia = isa .getAddress ();
1380- if (ia .isMulticastAddress ()) {
1381- sm .checkMulticast (ia );
1382- } else {
1383- sm .checkConnect (ia .getHostAddress (), isa .getPort ());
1384- sm .checkAccept (ia .getHostAddress (), isa .getPort ());
1385- }
1386- }
13871281
13881282 readLock .lock ();
13891283 try {
@@ -1589,17 +1483,13 @@ private void repairSocket(InetSocketAddress target)
15891483 /**
15901484 * Defines static methods to access AbstractSelectableChannel non-public members.
15911485 */
1592- @ SuppressWarnings ("removal" )
15931486 private static class AbstractSelectableChannels {
15941487 private static final Method FOREACH ;
15951488 static {
15961489 try {
1597- PrivilegedExceptionAction <Method > pae = () -> {
1598- Method m = AbstractSelectableChannel .class .getDeclaredMethod ("forEach" , Consumer .class );
1599- m .setAccessible (true );
1600- return m ;
1601- };
1602- FOREACH = AccessController .doPrivileged (pae );
1490+ Method m = AbstractSelectableChannel .class .getDeclaredMethod ("forEach" , Consumer .class );
1491+ m .setAccessible (true );
1492+ FOREACH = m ;
16031493 } catch (Exception e ) {
16041494 throw new InternalError (e );
16051495 }
@@ -1646,11 +1536,6 @@ private MembershipKey innerJoin(InetAddress group,
16461536 throw new IllegalArgumentException ("Source address is different type to group" );
16471537 }
16481538
1649- @ SuppressWarnings ("removal" )
1650- SecurityManager sm = System .getSecurityManager ();
1651- if (sm != null )
1652- sm .checkMulticast (group );
1653-
16541539 synchronized (stateLock ) {
16551540 ensureOpen ();
16561541
@@ -2051,10 +1936,7 @@ private static class DatagramPackets {
20511936 private static final VarHandle BUF_LENGTH ;
20521937 static {
20531938 try {
2054- PrivilegedExceptionAction <MethodHandles .Lookup > pa = () ->
2055- MethodHandles .privateLookupIn (DatagramPacket .class , MethodHandles .lookup ());
2056- @ SuppressWarnings ("removal" )
2057- MethodHandles .Lookup l = AccessController .doPrivileged (pa );
1939+ MethodHandles .Lookup l = MethodHandles .privateLookupIn (DatagramPacket .class , MethodHandles .lookup ());
20581940 LENGTH = l .findVarHandle (DatagramPacket .class , "length" , int .class );
20591941 BUF_LENGTH = l .findVarHandle (DatagramPacket .class , "bufLength" , int .class );
20601942 } catch (Exception e ) {
0 commit comments