changeset 216:6aa2cd650943

tcpserv-dump: specify bind IP on the command line
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 24 Jul 2023 22:19:08 -0800
parents 67289fac8a44
children 9f6a148ceb25
files utils/tcpserv-dump.c
diffstat 1 files changed, 13 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/utils/tcpserv-dump.c	Mon Jul 24 21:49:09 2023 -0800
+++ b/utils/tcpserv-dump.c	Mon Jul 24 22:19:08 2023 -0800
@@ -20,6 +20,7 @@
 
 #define	MAXCONN	10
 
+static struct in_addr bind_ip;
 static int tcp_port;
 static int listener, nconn, conns[MAXCONN], max_fd;
 static FILE *logf;
@@ -98,27 +99,33 @@
 	socklen_t addrlen;
 	fd_set fds;
 
-	if (argc != 3) {
-		fprintf(stderr, "usage: %s port logfile\n", argv[0]);
+	if (argc != 4) {
+		fprintf(stderr, "usage: %s bind-ip port logfile\n", argv[0]);
 		exit(1);
 	}
-	tcp_port = atoi(argv[1]);
+	bind_ip.s_addr = inet_addr(argv[1]);
+	if (bind_ip.s_addr == INADDR_NONE) {
+		fprintf(stderr, "error: invalid IP address argument \"%s\"\n",
+			argv[1]);
+		exit(1);
+	}
+	tcp_port = atoi(argv[2]);
 	listener = socket(AF_INET, SOCK_STREAM, 0);
 	if (listener < 0) {
 		perror("socket");
 		exit(1);
 	}
 	sin.sin_family = AF_INET;
-	sin.sin_addr.s_addr = INADDR_ANY;
+	sin.sin_addr = bind_ip;
 	sin.sin_port = htons(tcp_port);
 	rc = bind(listener, (struct sockaddr *) &sin, sizeof sin);
 	if (rc < 0) {
 		perror("bind");
 		exit(1);
 	}
-	logf = fopen(argv[2], "a");
+	logf = fopen(argv[3], "a");
 	if (!logf) {
-		perror(argv[2]);
+		perror(argv[3]);
 		exit(1);
 	}
 	rc = listen(listener, 5);