[PATCH v1] Print ip and port associated with descriptor with -yy

zubin.mithra at gmail.com zubin.mithra at gmail.com
Mon Aug 4 14:05:44 UTC 2014


From: Zubin Mithra <zubin.mithra at gmail.com>

* defs.h: Add header files netinet/in.h, sys/socket.h and
  arpa/inet.h.
(init): change type of show_fd_path to unsigned int.
* util.c (print_remote_ipport): New function.
(printsockdetails): New function.
(printfd): Modify to use printsockdetails.

Signed-off-by: Zubin Mithra <zubin.mithra at gmail.com>
---
 defs.h   |  5 +++-
 strace.c |  4 ++--
 util.c   | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 86 insertions(+), 4 deletions(-)

diff --git a/defs.h b/defs.h
index 1a3b483..f0fb79a 100644
--- a/defs.h
+++ b/defs.h
@@ -67,6 +67,9 @@
 #include <time.h>
 #include <sys/time.h>
 #include <sys/syscall.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
 
 #ifndef HAVE_STRERROR
 const char *strerror(int);
@@ -562,7 +565,7 @@ extern bool iflag;
 extern bool count_wallclock;
 extern unsigned int qflag;
 extern bool not_failing_only;
-extern bool show_fd_path;
+extern unsigned int show_fd_path;
 extern bool hide_log_until_execve;
 /* are we filtering traces based on paths? */
 extern const char **paths_selected;
diff --git a/strace.c b/strace.c
index 4154cde..2bc5c67 100644
--- a/strace.c
+++ b/strace.c
@@ -129,7 +129,7 @@ static int post_attach_sigstop = TCB_IGNORE_ONE_SIGSTOP;
 bool not_failing_only = 0;
 
 /* Show path associated with fd arguments */
-bool show_fd_path = 0;
+unsigned int show_fd_path = 0;
 
 static bool detach_on_execve = 0;
 /* Are we "strace PROG" and need to skip detach on first execve? */
@@ -1734,7 +1734,7 @@ init(int argc, char *argv[])
 			xflag++;
 			break;
 		case 'y':
-			show_fd_path = 1;
+			show_fd_path++;
 			break;
 		case 'v':
 			qualify("abbrev=none");
diff --git a/util.c b/util.c
index 33482d5..502f572 100644
--- a/util.c
+++ b/util.c
@@ -404,13 +404,92 @@ printnum_int(struct tcb *tcp, long addr, const char *fmt)
 	tprints("]");
 }
 
+static bool
+print_remote_ipport(int linenr, const char *line, int inodenr)
+{
+	unsigned long rxq, txq, time_len, retr, inode;
+	int local_port, rem_port, d, state, uid, timer_run, timeout;
+	char rem_addr[128], local_addr[128], more[512];
+	struct sockaddr_in remaddr;
+	if (linenr == 0)
+		return 1;
+
+	sscanf(line,
+	"%d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %X %lX:%lX %X:%lX %lX %d %d %ld %512s\n",
+	&d, local_addr, &local_port, rem_addr, &rem_port, &state,
+	&txq, &rxq, &timer_run, &time_len, &retr, &uid, &timeout, &inode, more);
+
+	if (inodenr != inode)
+		return 1;
+
+	if (strlen(rem_addr) > 8) {
+		// We have an ipv6 address
+		struct in6_addr in6;
+		sscanf(rem_addr, "%08X%08X%08X%08X",
+			&in6.s6_addr32[0], &in6.s6_addr32[1],
+			&in6.s6_addr32[2], &in6.s6_addr32[3]);
+		tprintf("%s:%d", inet_ntoa(remaddr.sin_addr),
+			rem_port);
+	  }
+	  else {
+		// We have an ipv4 address
+		sscanf(rem_addr, "%X", &((struct sockaddr_in *) &remaddr)->sin_addr.s_addr);
+		tprintf("%s:%d", inet_ntoa(remaddr.sin_addr), rem_port);
+	  }
+	  return 0;
+}
+
+/* Given an inode number of a socket, print out the details
+ * of the remote ip address and remote port */
+int
+printsockdetails(int inodenr)
+{
+	FILE *fp;
+	char buffer[8192];
+	int linenr = 0;
+	bool flag = 1;
+
+	fp = fopen("/proc/net/tcp", "r");
+	if (fp == NULL) {
+		return -1;
+	}
+
+	do {
+		if (fgets(buffer, sizeof(buffer), fp))
+			flag = print_remote_ipport(linenr++, buffer, inodenr);
+		if (!flag) break;
+	} while (!feof(fp));
+	fclose(fp);
+	if (flag)
+		return -1;
+	return 0;
+}
+
 void
 printfd(struct tcb *tcp, int fd)
 {
 	char path[PATH_MAX + 1];
 
-	if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0)
+	if (show_fd_path == 1 && getfdpath(tcp, fd, path, sizeof(path)) >= 0)
 		tprintf("%d<%s>", fd, path);
+	else if (show_fd_path > 1 && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
+		char *ptr = NULL;
+		int inodenr;
+		ptr = strstr(path, "socket:[");
+		if (ptr != path) {
+			tprintf("%d<%s>", fd, path);
+		}
+		else {
+			int retval;
+			ptr = path + 8;
+			path[strlen(path)-1] = '\0';
+			inodenr = strtol(ptr, NULL, 10);
+			tprintf("%d<", fd);
+			retval = printsockdetails(inodenr);
+			if (retval == -1) tprintf("socket:[%d]",inodenr);
+			tprints(">");
+		}
+	}
 	else
 		tprintf("%d", fd);
 }
-- 
1.8.4





More information about the Strace-devel mailing list