View Javadoc
1   /*
2    * Copyright 2016 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty5.util.internal;
17  
18  import java.io.IOException;
19  import java.net.InetAddress;
20  import java.net.InetSocketAddress;
21  import java.net.NetworkInterface;
22  import java.net.ServerSocket;
23  import java.net.Socket;
24  import java.net.SocketAddress;
25  import java.net.SocketException;
26  import java.net.SocketPermission;
27  import java.net.UnknownHostException;
28  import java.nio.channels.DatagramChannel;
29  import java.nio.channels.ServerSocketChannel;
30  import java.nio.channels.SocketChannel;
31  import java.security.AccessController;
32  import java.security.PrivilegedAction;
33  import java.security.PrivilegedActionException;
34  import java.security.PrivilegedExceptionAction;
35  import java.util.Collections;
36  import java.util.Enumeration;
37  
38  /**
39   * Provides socket operations with privileges enabled. This is necessary for applications that use the
40   * {@link SecurityManager} to restrict {@link SocketPermission} to their application. By asserting that these
41   * operations are privileged, the operations can proceed even if some code in the calling chain lacks the appropriate
42   * {@link SocketPermission}.
43   */
44  public final class SocketUtils {
45  
46      private static final Enumeration<Object> EMPTY = Collections.enumeration(Collections.emptyList());
47  
48      private SocketUtils() {
49      }
50  
51      @SuppressWarnings("unchecked")
52      private static <T> Enumeration<T> empty() {
53          return (Enumeration<T>) EMPTY;
54      }
55  
56      public static void connect(final Socket socket, final SocketAddress remoteAddress, final int timeout)
57              throws IOException {
58          try {
59              AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
60                  socket.connect(remoteAddress, timeout);
61                  return null;
62              });
63          } catch (PrivilegedActionException e) {
64              throw (IOException) e.getCause();
65          }
66      }
67  
68      public static void bind(final Socket socket, final SocketAddress bindpoint) throws IOException {
69          try {
70              AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
71                  socket.bind(bindpoint);
72                  return null;
73              });
74          } catch (PrivilegedActionException e) {
75              throw (IOException) e.getCause();
76          }
77      }
78  
79      public static boolean connect(final SocketChannel socketChannel, final SocketAddress remoteAddress)
80              throws IOException {
81          try {
82              return AccessController.doPrivileged((PrivilegedExceptionAction<Boolean>) () ->
83                      socketChannel.connect(remoteAddress));
84          } catch (PrivilegedActionException e) {
85              throw (IOException) e.getCause();
86          }
87      }
88  
89      public static void bind(final SocketChannel socketChannel, final SocketAddress address) throws IOException {
90          try {
91              AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
92                  socketChannel.bind(address);
93                  return null;
94              });
95          } catch (PrivilegedActionException e) {
96              throw (IOException) e.getCause();
97          }
98      }
99  
100     public static SocketChannel accept(final ServerSocketChannel serverSocketChannel) throws IOException {
101         try {
102             return AccessController.doPrivileged(
103                     (PrivilegedExceptionAction<SocketChannel>) serverSocketChannel::accept);
104         } catch (PrivilegedActionException e) {
105             throw (IOException) e.getCause();
106         }
107     }
108 
109     public static void bind(final DatagramChannel networkChannel, final SocketAddress address) throws IOException {
110         try {
111             AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
112                 networkChannel.bind(address);
113                 return null;
114             });
115         } catch (PrivilegedActionException e) {
116             throw (IOException) e.getCause();
117         }
118     }
119 
120     public static SocketAddress localSocketAddress(final ServerSocket socket) {
121         return AccessController.doPrivileged((PrivilegedAction<SocketAddress>) socket::getLocalSocketAddress);
122     }
123 
124     public static InetAddress addressByName(final String hostname) throws UnknownHostException {
125         try {
126             return AccessController.doPrivileged((PrivilegedExceptionAction<InetAddress>) () ->
127                     InetAddress.getByName(hostname));
128         } catch (PrivilegedActionException e) {
129             throw (UnknownHostException) e.getCause();
130         }
131     }
132 
133     public static InetAddress[] allAddressesByName(final String hostname) throws UnknownHostException {
134         try {
135             return AccessController.doPrivileged((PrivilegedExceptionAction<InetAddress[]>) () ->
136                     InetAddress.getAllByName(hostname));
137         } catch (PrivilegedActionException e) {
138             throw (UnknownHostException) e.getCause();
139         }
140     }
141 
142     public static InetSocketAddress socketAddress(final String hostname, final int port) {
143         return AccessController.doPrivileged((PrivilegedAction<InetSocketAddress>) () ->
144                 new InetSocketAddress(hostname, port));
145     }
146 
147     public static Enumeration<InetAddress> addressesFromNetworkInterface(final NetworkInterface intf) {
148         Enumeration<InetAddress> addresses =
149                 AccessController.doPrivileged((PrivilegedAction<Enumeration<InetAddress>>) intf::getInetAddresses);
150         // Android seems to sometimes return null even if this is not a valid return value by the api docs.
151         // Just return an empty Enumeration in this case.
152         // See https://github.com/netty/netty/issues/10045
153         if (addresses == null) {
154             return empty();
155         }
156         return addresses;
157     }
158 
159     public static InetAddress loopbackAddress() {
160         return AccessController.doPrivileged((PrivilegedAction<InetAddress>) InetAddress::getLoopbackAddress);
161     }
162 
163     public static byte[] hardwareAddressFromNetworkInterface(final NetworkInterface intf) throws SocketException {
164         try {
165             return AccessController.doPrivileged((PrivilegedExceptionAction<byte[]>) intf::getHardwareAddress);
166         } catch (PrivilegedActionException e) {
167             throw (SocketException) e.getCause();
168         }
169     }
170 }