MyServerSocketFactory.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package de.tudarmstadt.informatik.hostage.net;
  2. import java.io.FileDescriptor;
  3. import java.io.IOException;
  4. import java.lang.reflect.Field;
  5. import java.net.InetAddress;
  6. import java.net.InetSocketAddress;
  7. import java.net.ServerSocket;
  8. import java.net.SocketImpl;
  9. import javax.net.ServerSocketFactory;
  10. import de.tudarmstadt.informatik.hostage.system.Device;
  11. import de.tudarmstadt.informatik.hostage.system.PrivilegedPort;
  12. import de.tudarmstadt.informatik.hostage.system.PrivilegedPort.TYPE;
  13. public class MyServerSocketFactory extends ServerSocketFactory {
  14. @Override
  15. public ServerSocket createServerSocket(int port) throws IOException {
  16. ServerSocket socket = null;
  17. if (port > 1023) {
  18. socket = new ServerSocket();
  19. socket.setReuseAddress(true);
  20. socket.bind(new InetSocketAddress(port));
  21. } else if (Device.isPPInstalled()) {
  22. FileDescriptor fd = new PrivilegedPort(TYPE.TCP, port).getFD();
  23. socket = new ServerSocket();
  24. try {
  25. SocketImpl impl = getImpl(socket);
  26. injectFD(fd, impl);
  27. injectImpl(impl, socket);
  28. setBound(socket);
  29. } catch (NoSuchFieldException e) {
  30. } catch (IllegalAccessException e) {
  31. } catch (IllegalArgumentException e) {
  32. }
  33. }
  34. return socket;
  35. }
  36. @Override
  37. public ServerSocket createServerSocket(int port, int backlog) throws IOException {
  38. return createServerSocket(port);
  39. }
  40. @Override
  41. public ServerSocket createServerSocket(int port, int backlog, InetAddress iAddress) throws IOException {
  42. return createServerSocket(port);
  43. }
  44. private SocketImpl getImpl(ServerSocket socket) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
  45. Field implField = socket.getClass().getDeclaredField("impl");
  46. implField.setAccessible(true);
  47. return (SocketImpl) implField.get(socket);
  48. }
  49. private void injectFD(FileDescriptor fd, SocketImpl impl) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
  50. Class<?> plainServerSocketImplClazz = impl.getClass();
  51. Class<?> plainSocketImplClazz = plainServerSocketImplClazz.getSuperclass();
  52. Class<?> socketImplClazz = plainSocketImplClazz.getSuperclass();
  53. Field fdField = socketImplClazz.getDeclaredField("fd");
  54. fdField.setAccessible(true);
  55. fdField.set(impl, fd);
  56. }
  57. private void injectImpl(SocketImpl impl, ServerSocket socket) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
  58. Field implField = socket.getClass().getDeclaredField("impl");
  59. implField.setAccessible(true);
  60. implField.set(socket, impl);
  61. }
  62. private void setBound(ServerSocket socket) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
  63. Field boundField = socket.getClass().getDeclaredField("isBound");
  64. boundField.setAccessible(true);
  65. boundField.set(socket, true);
  66. }
  67. }