Browse Source

improve porthack
fixed udp (tested, working)

Mihai Plasoianu 10 years ago
parent
commit
f4ffa7b86d

+ 5 - 10
native/bind.c

@@ -29,8 +29,7 @@ int ipc_sock() {
 	addr.sun_family = AF_UNIX;
 	strncpy(&addr.sun_path[1], UNIX_PATH, strlen(UNIX_PATH));
 
-	if (connect(fd, (struct sockaddr*) &addr,
-			sizeof(sa_family_t) + strlen(UNIX_PATH) + 1) == -1) {
+	if (connect(fd, (struct sockaddr*) &addr, sizeof(sa_family_t) + strlen(UNIX_PATH) + 1) == -1) {
 		perror("Unable to connect local socket");
 		return -1;
 	}
@@ -43,8 +42,7 @@ int net_sock(int type, int port) {
 	int reuseaddr = 1;
 	struct sockaddr_in addr;
 
-	if ((fd = socket(AF_INET, (type == 1 ? SOCK_STREAM : SOCK_DGRAM), 0))
-			== -1) {
+	if ((fd = socket(AF_INET, (type == 1 ? SOCK_STREAM : SOCK_DGRAM), 0)) == -1) {
 		perror("Unable to create net socket");
 		return -1;
 	}
@@ -54,12 +52,9 @@ int net_sock(int type, int port) {
 	addr.sin_addr.s_addr = INADDR_ANY;
 	addr.sin_port = htons(port);
 
-	if (type == 1) {
-		if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
-				sizeof(reuseaddr)) == -1) {
-			perror("Unable to set socket options");
-			return -1;
-		}
+	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr)) == -1) {
+		perror("Unable to set socket options");
+		return -1;
 	}
 
 	if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {

+ 1 - 17
src/de/tudarmstadt/informatik/hostage/net/MyDatagramSocketFactory.java

@@ -5,7 +5,6 @@ import java.io.IOException;
 import java.lang.reflect.Field;
 import java.net.DatagramSocket;
 import java.net.DatagramSocketImpl;
-import java.net.InetSocketAddress;
 
 import de.tudarmstadt.informatik.hostage.system.Device;
 import de.tudarmstadt.informatik.hostage.system.PrivilegedPort;
@@ -16,16 +15,13 @@ public class MyDatagramSocketFactory {
 	public DatagramSocket createDatagramSocket(int port) throws IOException {
 		DatagramSocket socket = null;
 		if (port > 1023) {
-			socket = new DatagramSocket();
-			socket.bind(new InetSocketAddress(port));
+			socket = new DatagramSocket(port);
 		} else if (Device.isPPInstalled()) {
 			FileDescriptor fd = new PrivilegedPort(TYPE.UDP, port).getFD();
 			socket = new DatagramSocket();
 			try {
 				DatagramSocketImpl impl = getImpl(socket);
 				injectFD(fd, impl);
-				injectImpl(impl, socket);
-				setBound(socket);
 			} catch (NoSuchFieldException e) {
 			} catch (IllegalAccessException e) {
 			} catch (IllegalArgumentException e) {
@@ -48,16 +44,4 @@ public class MyDatagramSocketFactory {
 		fdField.set(impl, fd);
 	}
 
-	private void injectImpl(DatagramSocketImpl impl, DatagramSocket socket) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
-		Field implField = socket.getClass().getDeclaredField("impl");
-		implField.setAccessible(true);
-		implField.set(socket, impl);
-	}
-
-	private void setBound(DatagramSocket socket) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
-		Field boundField = socket.getClass().getDeclaredField("isBound");
-		boundField.setAccessible(true);
-		boundField.set(socket, true);
-	}
-
 }

+ 0 - 7
src/de/tudarmstadt/informatik/hostage/net/MyServerSocketFactory.java

@@ -29,7 +29,6 @@ public class MyServerSocketFactory extends ServerSocketFactory {
 			try {
 				SocketImpl impl = getImpl(socket);
 				injectFD(fd, impl);
-				injectImpl(impl, socket);
 				setBound(socket);
 			} catch (NoSuchFieldException e) {
 			} catch (IllegalAccessException e) {
@@ -64,12 +63,6 @@ public class MyServerSocketFactory extends ServerSocketFactory {
 		fdField.set(impl, fd);
 	}
 
-	private void injectImpl(SocketImpl impl, ServerSocket socket) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
-		Field implField = socket.getClass().getDeclaredField("impl");
-		implField.setAccessible(true);
-		implField.set(socket, impl);
-	}
-
 	private void setBound(ServerSocket socket) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
 		Field boundField = socket.getClass().getDeclaredField("isBound");
 		boundField.setAccessible(true);