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    *   http://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.netty.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.Enumeration;
36  
37  /**
38   * Provides socket operations with privileges enabled. This is necessary for applications that use the
39   * {@link SecurityManager} to restrict {@link SocketPermission} to their application. By asserting that these
40   * operations are privileged, the operations can proceed even if some code in the calling chain lacks the appropriate
41   * {@link SocketPermission}.
42   */
43  public final class SocketUtils {
44  
45      private SocketUtils() {
46      }
47  
48      public static void connect(final Socket socket, final SocketAddress remoteAddress, final int timeout)
49              throws IOException {
50          try {
51              AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
52                  @Override
53                  public Void run() throws IOException {
54                      socket.connect(remoteAddress, timeout);
55                      return null;
56                  }
57              });
58          } catch (PrivilegedActionException e) {
59              throw (IOException) e.getCause();
60          }
61      }
62  
63      public static void bind(final Socket socket, final SocketAddress bindpoint) throws IOException {
64          try {
65              AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
66                  @Override
67                  public Void run() throws IOException {
68                      socket.bind(bindpoint);
69                      return null;
70                  }
71              });
72          } catch (PrivilegedActionException e) {
73              throw (IOException) e.getCause();
74          }
75      }
76  
77      public static boolean connect(final SocketChannel socketChannel, final SocketAddress remoteAddress)
78              throws IOException {
79          try {
80              return AccessController.doPrivileged(new PrivilegedExceptionAction<Boolean>() {
81                  @Override
82                  public Boolean run() throws IOException {
83                      return socketChannel.connect(remoteAddress);
84                  }
85              });
86          } catch (PrivilegedActionException e) {
87              throw (IOException) e.getCause();
88          }
89      }
90  
91      public static void bind(final SocketChannel socketChannel, final SocketAddress address) throws IOException {
92          try {
93              AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
94                  @Override
95                  public Void run() throws IOException {
96                      socketChannel.bind(address);
97                      return null;
98                  }
99              });
100         } catch (PrivilegedActionException e) {
101             throw (IOException) e.getCause();
102         }
103     }
104 
105     public static SocketChannel accept(final ServerSocketChannel serverSocketChannel) throws IOException {
106         try {
107             return AccessController.doPrivileged(new PrivilegedExceptionAction<SocketChannel>() {
108                 @Override
109                 public SocketChannel run() throws IOException {
110                     return serverSocketChannel.accept();
111                 }
112             });
113         } catch (PrivilegedActionException e) {
114             throw (IOException) e.getCause();
115         }
116     }
117 
118     public static void bind(final DatagramChannel networkChannel, final SocketAddress address) throws IOException {
119         try {
120             AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
121                 @Override
122                 public Void run() throws IOException {
123                     networkChannel.bind(address);
124                     return null;
125                 }
126             });
127         } catch (PrivilegedActionException e) {
128             throw (IOException) e.getCause();
129         }
130     }
131 
132     public static SocketAddress localSocketAddress(final ServerSocket socket) {
133         return AccessController.doPrivileged(new PrivilegedAction<SocketAddress>() {
134             @Override
135             public SocketAddress run() {
136                 return socket.getLocalSocketAddress();
137             }
138         });
139     }
140 
141     public static InetAddress addressByName(final String hostname) throws UnknownHostException {
142         try {
143             return AccessController.doPrivileged(new PrivilegedExceptionAction<InetAddress>() {
144                 @Override
145                 public InetAddress run() throws UnknownHostException {
146                     return InetAddress.getByName(hostname);
147                 }
148             });
149         } catch (PrivilegedActionException e) {
150             throw (UnknownHostException) e.getCause();
151         }
152     }
153 
154     public static InetAddress[] allAddressesByName(final String hostname) throws UnknownHostException {
155         try {
156             return AccessController.doPrivileged(new PrivilegedExceptionAction<InetAddress[]>() {
157                 @Override
158                 public InetAddress[] run() throws UnknownHostException {
159                     return InetAddress.getAllByName(hostname);
160                 }
161             });
162         } catch (PrivilegedActionException e) {
163             throw (UnknownHostException) e.getCause();
164         }
165     }
166 
167     public static InetSocketAddress socketAddress(final String hostname, final int port) {
168         return AccessController.doPrivileged(new PrivilegedAction<InetSocketAddress>() {
169             @Override
170             public InetSocketAddress run() {
171                 return new InetSocketAddress(hostname, port);
172             }
173         });
174     }
175 
176     public static Enumeration<InetAddress> addressesFromNetworkInterface(final NetworkInterface intf) {
177         return AccessController.doPrivileged(new PrivilegedAction<Enumeration<InetAddress>>() {
178             @Override
179             public Enumeration<InetAddress> run() {
180                 return intf.getInetAddresses();
181             }
182         });
183     }
184 
185     public static byte[] hardwareAddressFromNetworkInterface(final NetworkInterface intf) throws SocketException {
186         try {
187             return AccessController.doPrivileged(new PrivilegedExceptionAction<byte[]>() {
188                 @Override
189                 public byte[] run() throws SocketException {
190                     return intf.getHardwareAddress();
191                 }
192             });
193         } catch (PrivilegedActionException e) {
194             throw (SocketException) e.getCause();
195         }
196     }
197 }