Browse Source

more verbose port binder. fixed some warnings

Fabio Arnold 9 years ago
parent
commit
9b2574222e
1 changed files with 24 additions and 12 deletions
  1. 24 12
      native/bind.c

+ 24 - 12
native/bind.c

@@ -9,6 +9,16 @@
 #include <sys/un.h>
 #include <unistd.h>
 
+#ifdef ANDROID
+#include <android/log.h>
+#define LOG_TAG "PortBinder"
+#define LOGI(...) android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS)
+#define LOGE(...) android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS)
+#else
+#define LOGI printf
+#define LOGE printf
+#endif
+
 #define CONTROLLEN CMSG_LEN(sizeof(int))
 
 // LocalSocket uses the Linux abstract namespace instead of the filesystem.
@@ -85,12 +95,12 @@ int send_fd(int fd, int fd_to_send) {
 	struct iovec iov[1];
 	struct cmsghdr *cmptr;
 	struct msghdr msg;
-	char buf[2] = "FD";
+	char buf[] = "FD";
 
 	iov[0].iov_base = buf;
 	iov[0].iov_len = 2;
 
-	cmptr = malloc(CONTROLLEN);
+	cmptr = (struct cmsghdr *)malloc(CONTROLLEN);
 	cmptr->cmsg_level = SOL_SOCKET;
 	cmptr->cmsg_type = SCM_RIGHTS;
 	cmptr->cmsg_len = CONTROLLEN;
@@ -116,11 +126,13 @@ int main(int argc, char *argv[]) {
 	int ipc_fd, net_fd;
 
 	if (argc < 3) {
-		printf("usage: %s <protocol> <port>\n", argv[0]);
-		printf("where protocol is either TCP or UDP and port is between 1 and 65535\n");
+		LOGI("usage: %s <protocol> <port>\n", argv[0]);
+		LOGI("where protocol is either TCP or UDP and port is between 1 and 65535\n");
 		exit(EXIT_FAILURE);
 	}
 
+	LOGI("port binder reporting %s %s\n", argv[1], argv[2]);
+
 	if (strncmp(argv[1], TCP, 3) == 0) {
 		type = 1;
 	} else if (strncmp(argv[1], UDP, 3) == 0) {
@@ -134,25 +146,25 @@ int main(int argc, char *argv[]) {
 		exit(EXIT_FAILURE);
 	}
 
-	if ((ipc_fd = ipc_sock()) == -1) {
-		close(ipc_fd);
+	if ((net_fd = net_sock(type, port)) == -1) {
+		close(net_fd);
 		exit(EXIT_FAILURE);
 	}
-	printf("ipc_fd: %d\n", ipc_fd);
+	LOGI("net_fd: %d\n", net_fd);
 
-	if ((net_fd = net_sock(type, port)) == -1) {
-		close(ipc_fd);
+	if ((ipc_fd = ipc_sock()) == -1) {
 		close(net_fd);
+		close(ipc_fd);
 		exit(EXIT_FAILURE);
 	}
-	printf("net_fd: %d\n", net_fd);
+	LOGI("ipc_fd: %d\n", ipc_fd);
 
 	int status;
 	status = send_fd(ipc_fd, net_fd);
-	printf("send_fd: %d\n", status);
+	LOGI("send_fd: %d\n", status);
 
 	close(ipc_fd);
-	close(net_fd);
+	//close(net_fd);
 
 	if (status == -1) {
 		return (EXIT_FAILURE);