|
@@ -10,21 +10,11 @@ import java.net.SocketImpl;
|
|
|
|
|
|
import javax.net.ServerSocketFactory;
|
|
|
|
|
|
-import de.tudarmstadt.informatik.hostage.system.P;
|
|
|
-import de.tudarmstadt.informatik.hostage.ui.MainActivity;
|
|
|
+import de.tudarmstadt.informatik.hostage.system.Device;
|
|
|
+import de.tudarmstadt.informatik.hostage.system.PrivilegedPort;
|
|
|
|
|
|
-/**
|
|
|
- * Server Socket Factory using file descriptors.
|
|
|
- *
|
|
|
- * @author Mihai Plasoianu
|
|
|
- * @author Lars Pandikow
|
|
|
- */
|
|
|
public class MyServerSocketFactory extends ServerSocketFactory {
|
|
|
|
|
|
- /**
|
|
|
- * This method creates and returns a ServerSocket. A custom SocketImpl is
|
|
|
- * injected into the ServerSocket.
|
|
|
- */
|
|
|
@Override
|
|
|
public ServerSocket createServerSocket(int port) throws IOException {
|
|
|
ServerSocket socket = null;
|
|
@@ -32,75 +22,53 @@ public class MyServerSocketFactory extends ServerSocketFactory {
|
|
|
socket = new ServerSocket();
|
|
|
socket.setReuseAddress(true);
|
|
|
socket.bind(new InetSocketAddress(port));
|
|
|
- } else if (MainActivity.porthackInstalled) {
|
|
|
- FileDescriptor fd = new P(port).bindAndGetFD();
|
|
|
+ } else if (Device.isPPInstalled()) {
|
|
|
+ FileDescriptor fd = new PrivilegedPort(port).getFD();
|
|
|
socket = new ServerSocket();
|
|
|
try {
|
|
|
SocketImpl impl = getImpl(socket);
|
|
|
injectFD(fd, impl);
|
|
|
injectImpl(impl, socket);
|
|
|
setBound(socket);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ } catch (ReflectiveOperationException e) {
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
}
|
|
|
}
|
|
|
return socket;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Must override.
|
|
|
- */
|
|
|
@Override
|
|
|
- public ServerSocket createServerSocket(int port, int backlog)
|
|
|
- throws IOException {
|
|
|
+ public ServerSocket createServerSocket(int port, int backlog) throws IOException {
|
|
|
return createServerSocket(port);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Must override.
|
|
|
- */
|
|
|
@Override
|
|
|
- public ServerSocket createServerSocket(int port, int backlog,
|
|
|
- InetAddress iAddress) throws IOException {
|
|
|
+ public ServerSocket createServerSocket(int port, int backlog, InetAddress iAddress) throws IOException {
|
|
|
return createServerSocket(port);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Extracts the SocketImpl out of a ServerSocket.
|
|
|
- */
|
|
|
- private SocketImpl getImpl(ServerSocket socket) throws Exception {
|
|
|
+ private SocketImpl getImpl(ServerSocket socket) throws ReflectiveOperationException, IllegalArgumentException {
|
|
|
Field implField = socket.getClass().getDeclaredField("impl");
|
|
|
implField.setAccessible(true);
|
|
|
return (SocketImpl) implField.get(socket);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Injects a FileDescriptor into a SocketImpl.
|
|
|
- */
|
|
|
- private void injectFD(FileDescriptor fd, SocketImpl impl) throws Exception {
|
|
|
+ private void injectFD(FileDescriptor fd, SocketImpl impl) throws ReflectiveOperationException, IllegalArgumentException {
|
|
|
Class<?> plainServerSocketImplClazz = impl.getClass();
|
|
|
- Class<?> plainSocketImplClazz = plainServerSocketImplClazz
|
|
|
- .getSuperclass();
|
|
|
+ Class<?> plainSocketImplClazz = plainServerSocketImplClazz.getSuperclass();
|
|
|
Class<?> socketImplClazz = plainSocketImplClazz.getSuperclass();
|
|
|
Field fdField = socketImplClazz.getDeclaredField("fd");
|
|
|
fdField.setAccessible(true);
|
|
|
fdField.set(impl, fd);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Injects a SocketImpl into a ServerSocket.
|
|
|
- */
|
|
|
- private void injectImpl(SocketImpl impl, ServerSocket socket)
|
|
|
- throws Exception {
|
|
|
+ private void injectImpl(SocketImpl impl, ServerSocket socket) throws ReflectiveOperationException, IllegalArgumentException {
|
|
|
Field implField = socket.getClass().getDeclaredField("impl");
|
|
|
implField.setAccessible(true);
|
|
|
implField.set(socket, impl);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Sets the isBound Field of a ServerSocket to true.
|
|
|
- */
|
|
|
- private void setBound(ServerSocket socket) throws Exception {
|
|
|
+ private void setBound(ServerSocket socket) throws ReflectiveOperationException, IllegalArgumentException {
|
|
|
Field boundField = socket.getClass().getDeclaredField("isBound");
|
|
|
boundField.setAccessible(true);
|
|
|
boundField.set(socket, true);
|