FreeCalypso > hg > themwi-system-sw
comparison utils/tcpserv-dump.c @ 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 |
comparison
equal
deleted
inserted
replaced
215:67289fac8a44 | 216:6aa2cd650943 |
---|---|
18 #include <time.h> | 18 #include <time.h> |
19 #include <unistd.h> | 19 #include <unistd.h> |
20 | 20 |
21 #define MAXCONN 10 | 21 #define MAXCONN 10 |
22 | 22 |
23 static struct in_addr bind_ip; | |
23 static int tcp_port; | 24 static int tcp_port; |
24 static int listener, nconn, conns[MAXCONN], max_fd; | 25 static int listener, nconn, conns[MAXCONN], max_fd; |
25 static FILE *logf; | 26 static FILE *logf; |
26 static struct timeval curtime; | 27 static struct timeval curtime; |
27 static char fmt_time[32]; | 28 static char fmt_time[32]; |
96 int max_fd, rc, i; | 97 int max_fd, rc, i; |
97 struct sockaddr_in sin; | 98 struct sockaddr_in sin; |
98 socklen_t addrlen; | 99 socklen_t addrlen; |
99 fd_set fds; | 100 fd_set fds; |
100 | 101 |
101 if (argc != 3) { | 102 if (argc != 4) { |
102 fprintf(stderr, "usage: %s port logfile\n", argv[0]); | 103 fprintf(stderr, "usage: %s bind-ip port logfile\n", argv[0]); |
103 exit(1); | 104 exit(1); |
104 } | 105 } |
105 tcp_port = atoi(argv[1]); | 106 bind_ip.s_addr = inet_addr(argv[1]); |
107 if (bind_ip.s_addr == INADDR_NONE) { | |
108 fprintf(stderr, "error: invalid IP address argument \"%s\"\n", | |
109 argv[1]); | |
110 exit(1); | |
111 } | |
112 tcp_port = atoi(argv[2]); | |
106 listener = socket(AF_INET, SOCK_STREAM, 0); | 113 listener = socket(AF_INET, SOCK_STREAM, 0); |
107 if (listener < 0) { | 114 if (listener < 0) { |
108 perror("socket"); | 115 perror("socket"); |
109 exit(1); | 116 exit(1); |
110 } | 117 } |
111 sin.sin_family = AF_INET; | 118 sin.sin_family = AF_INET; |
112 sin.sin_addr.s_addr = INADDR_ANY; | 119 sin.sin_addr = bind_ip; |
113 sin.sin_port = htons(tcp_port); | 120 sin.sin_port = htons(tcp_port); |
114 rc = bind(listener, (struct sockaddr *) &sin, sizeof sin); | 121 rc = bind(listener, (struct sockaddr *) &sin, sizeof sin); |
115 if (rc < 0) { | 122 if (rc < 0) { |
116 perror("bind"); | 123 perror("bind"); |
117 exit(1); | 124 exit(1); |
118 } | 125 } |
119 logf = fopen(argv[2], "a"); | 126 logf = fopen(argv[3], "a"); |
120 if (!logf) { | 127 if (!logf) { |
121 perror(argv[2]); | 128 perror(argv[3]); |
122 exit(1); | 129 exit(1); |
123 } | 130 } |
124 rc = listen(listener, 5); | 131 rc = listen(listener, 5); |
125 if (rc < 0) { | 132 if (rc < 0) { |
126 perror("listen"); | 133 perror("listen"); |