Browse Source

inject local port in datagram socket. more clarity in port binder

Fabio Arnold 9 years ago
parent
commit
4b1da2643b

+ 17 - 9
native/bind.c

@@ -8,6 +8,7 @@
 #include <sys/uio.h>
 #include <sys/un.h>
 #include <unistd.h>
+#include <netdb.h>
 
 #ifdef ANDROID
 #include <android/log.h>
@@ -58,29 +59,36 @@ int ipc_sock() {
 int net_sock(int type, int port) {
 	int fd;
 	int reuseaddr = 1;
-	struct sockaddr_in addr;
 
-	if ((fd = socket(AF_INET, (type == 1 ? SOCK_STREAM : SOCK_DGRAM), (type == 1 ? IPPROTO_IP : IPPROTO_UDP))) == -1) {
+	struct addrinfo hints, *res;
+
+	// first, load up address structs with getaddrinfo():
+	memset(&hints, 0, sizeof hints);
+	hints.ai_family = AF_INET; // use IPv4
+	hints.ai_socktype = (type == 1 ? SOCK_STREAM : SOCK_DGRAM); // TCP or UDP
+	hints.ai_flags = AI_PASSIVE; // fill in my IP for me
+
+	char service_name[256];
+	sprintf(service_name, "%d", port);
+	getaddrinfo(NULL, service_name, &hints, &res);
+
+	if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) {
 		perror("Unable to create net socket");
 		return -1;
 	}
 
-	memset(&addr, 0, sizeof(addr));
-	addr.sin_family = AF_INET;
-	addr.sin_addr.s_addr = INADDR_ANY;
-	addr.sin_port = htons(port);
-
 	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) {
+	if (bind(fd, res->ai_addr, res->ai_addrlen) == -1) {
 		perror("Unable to bind net socket");
 		return -1;
 	}
+	printf("Hello from the new port binder\n");
 
-	if (type == 1) {
+	if (type == 1) { // TCP
 		if (listen(fd, 5) == -1) {
 			perror("Unable to listen net socket");
 			return -1;

+ 12 - 0
src/de/tudarmstadt/informatik/hostage/net/MyDatagramSocketFactory.java

@@ -5,6 +5,7 @@ import java.io.IOException;
 import java.lang.reflect.Field;
 import java.net.DatagramSocket;
 import java.net.DatagramSocketImpl;
+import java.util.NoSuchElementException;
 
 import de.tudarmstadt.informatik.hostage.system.Device;
 import de.tudarmstadt.informatik.hostage.system.PrivilegedPort;
@@ -14,6 +15,7 @@ public class MyDatagramSocketFactory {
 
 	public DatagramSocket createDatagramSocket(int port) throws IOException {
 		DatagramSocket socket = null;
+		//port = 1024;
 		if (port > 1023) {
 			socket = new DatagramSocket(port);
 		} else if (Device.isPorthackInstalled()) {
@@ -22,6 +24,7 @@ public class MyDatagramSocketFactory {
 			try {
 				DatagramSocketImpl impl = getImpl(socket);
 				injectFD(fd, impl);
+				injectLocalPort(port, impl);
 				setBound(socket);
 			} catch (NoSuchFieldException e) {
 			} catch (IllegalAccessException e) {
@@ -45,6 +48,15 @@ public class MyDatagramSocketFactory {
 		fdField.set(impl, fd);
 	}
 
+	private void injectLocalPort(int port , DatagramSocketImpl impl)
+			throws NoSuchElementException, NoSuchFieldException, IllegalAccessException {
+		Class<?> plainDatagramSocketImplClazz = impl.getClass();
+		Class<?> datagramSocketImplClazz = plainDatagramSocketImplClazz.getSuperclass();
+		Field localPortField = datagramSocketImplClazz.getDeclaredField("localPort");
+		localPortField.setAccessible(true);
+		localPortField.set(impl, port);
+	}
+
 	private void setBound(DatagramSocket socket) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException {
 		Field boundField = socket.getClass().getDeclaredField("isBound");
 		boundField.setAccessible(true);