Fix decoding of replies after addition of the -l option

This commit is contained in:
Matthieu Herrb 2015-09-30 11:10:16 +02:00
parent 50fe2999f8
commit ae13384faa
2 changed files with 24 additions and 14 deletions

View file

@ -5,7 +5,7 @@ SRCS= echoc.c
OBJS= $(SRCS:%.c=%.o) OBJS= $(SRCS:%.c=%.o)
LIBS= LIBS= -lbsd
all: $(PROG) all: $(PROG)

36
echoc.c
View file

@ -18,6 +18,9 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/time.h> #include <sys/time.h>
#include <limits.h>
#include <bsd/stdlib.h>
#include <err.h> #include <err.h>
#include <errno.h> #include <errno.h>
#include <netdb.h> #include <netdb.h>
@ -46,8 +49,11 @@ usage(void)
static void static void
send_packet(int unused) send_packet(int unused)
{ {
char buf[len]; char *buf;
buf = malloc(len);
if (buf == NULL)
return;
snprintf(buf, len, "%d", seq); snprintf(buf, len, "%d", seq);
if (sendto(sock, buf, len, 0, server, if (sendto(sock, buf, len, 0, server,
serverlen) != len) { serverlen) != len) {
@ -63,7 +69,9 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char name[NI_MAXHOST]; char name[NI_MAXHOST];
char *buf; char *recvbuf;
const char *errstr;
char date[80];
char *port = "echo"; char *port = "echo";
struct sockaddr_storage client; struct sockaddr_storage client;
struct addrinfo hints, *res, *res0; struct addrinfo hints, *res, *res0;
@ -77,7 +85,7 @@ main(int argc, char *argv[])
long timeout = 500; /* default timeout (ms) */ long timeout = 500; /* default timeout (ms) */
int ch; int ch;
int nfds, received = 0; int nfds, received = 0;
int error, buffer, last = -1; int error, last = -1;
int disconnected; int disconnected;
int nofragment = 0; int nofragment = 0;
int we_count = 0, counter; /* don't loop forever */ int we_count = 0, counter; /* don't loop forever */
@ -163,8 +171,8 @@ main(int argc, char *argv[])
gettimeofday(&last_ts, NULL); gettimeofday(&last_ts, NULL);
buf = malloc(len); recvbuf = malloc(len);
if (buf == NULL) if (recvbuf == NULL)
err(2, "malloc receive buffer"); err(2, "malloc receive buffer");
#ifdef IP_MTU_DISCOVER #ifdef IP_MTU_DISCOVER
@ -201,17 +209,17 @@ main(int argc, char *argv[])
if (disconnected == 1) { if (disconnected == 1) {
tm = localtime((time_t *)&last_ts.tv_sec); tm = localtime((time_t *)&last_ts.tv_sec);
strftime(buf, sizeof(buf), "%F %T", tm); strftime(date, sizeof(date), "%F %T", tm);
printf("%s.%06ld: lost connection\n", printf("%s.%06ld: lost connection\n",
buf, last_ts.tv_usec); date, last_ts.tv_usec);
} }
continue; continue;
} }
addrlen = sizeof(client); addrlen = sizeof(client);
if ((received = recvfrom(sock, &buffer, sizeof(buffer), if ((received = recvfrom(sock, recvbuf, len,
MSG_DONTWAIT, MSG_DONTWAIT,
(struct sockaddr *) &client, (struct sockaddr *) &client,
&addrlen)) != sizeof(buffer)) { &addrlen)) != len) {
warn("recvfrom"); warn("recvfrom");
} }
@ -228,16 +236,18 @@ main(int argc, char *argv[])
} }
if (disconnected) { if (disconnected) {
tm = localtime((time_t *)&now.tv_sec); tm = localtime((time_t *)&now.tv_sec);
strftime(buf, sizeof(buf), "%F %T", tm); strftime(date, sizeof(date), "%F %T", tm);
printf("%s.%06ld: connection is back " printf("%s.%06ld: connection is back "
"dropped %d packets\n", "dropped %d packets\n",
buf, now.tv_usec, seq - last); date, now.tv_usec, seq - last);
disconnected = 0; disconnected = 0;
} }
last = buffer; last = strtonum(recvbuf, 0, INT_MAX, &errstr);
if (errstr)
errx(1, "invalid reply %s", errstr);
memcpy(&last_ts, &now, sizeof(struct timeval)); memcpy(&last_ts, &now, sizeof(struct timeval));
if (verbose) if (verbose)
printf("received %d %ld.%06ld\n", buffer, printf("received %d %ld.%06ld\n", last,
(long)diff.tv_sec, diff.tv_usec); (long)diff.tv_sec, diff.tv_usec);
if (we_count) { if (we_count) {
counter--; counter--;