2012-07-15 09:54:00 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Matthieu Herrb <matthieu@herrb.eu>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include <sys/types.h>
|
2012-07-30 20:49:29 +02:00
|
|
|
#include <sys/ioctl.h>
|
2012-07-15 09:54:00 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <signal.h>
|
2012-07-30 20:49:29 +02:00
|
|
|
#include <stdbool.h>
|
2012-07-15 09:54:00 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
int sock = -1;
|
|
|
|
int verbose = 0;
|
|
|
|
struct sockaddr *server;
|
|
|
|
socklen_t serverlen;
|
|
|
|
unsigned int seq = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2012-07-31 11:24:50 +02:00
|
|
|
errx(2, "usage: echoc [-i ms][-t ms][-v] server");
|
2012-07-15 09:54:00 +02:00
|
|
|
}
|
|
|
|
|
2012-07-30 20:49:29 +02:00
|
|
|
static void
|
2012-07-31 08:26:05 +02:00
|
|
|
send_packet(int unused)
|
2012-07-30 20:49:29 +02:00
|
|
|
{
|
|
|
|
if (sendto(sock, &seq, sizeof(seq), 0, server,
|
|
|
|
serverlen) != sizeof(seq)) {
|
|
|
|
if (verbose)
|
|
|
|
warn("sendto");
|
|
|
|
}
|
2012-07-31 08:44:18 +02:00
|
|
|
if (verbose)
|
2012-07-30 20:49:29 +02:00
|
|
|
printf("sent %d\n", seq);
|
|
|
|
seq++;
|
|
|
|
}
|
|
|
|
|
2012-07-15 09:54:00 +02:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char name[NI_MAXHOST];
|
2012-07-15 11:29:43 +02:00
|
|
|
char buf[80];
|
2012-07-15 09:54:00 +02:00
|
|
|
struct sockaddr_storage client;
|
|
|
|
struct addrinfo hints, *res, *res0;
|
2012-07-31 08:26:05 +02:00
|
|
|
struct itimerval itv;
|
2012-07-31 11:26:45 +02:00
|
|
|
struct timeval last_ts;
|
|
|
|
struct timeval now, diff;
|
2012-07-15 11:29:43 +02:00
|
|
|
struct tm *tm;
|
2012-07-15 09:54:00 +02:00
|
|
|
struct pollfd pfd[1];
|
|
|
|
socklen_t addrlen;
|
2012-07-31 11:24:50 +02:00
|
|
|
long interval = 100; /* default interval (ms) */
|
|
|
|
long timeout = 500; /* default timeout (ms) */
|
2012-07-15 09:54:00 +02:00
|
|
|
int ch;
|
|
|
|
int nfds, received = 0;
|
2012-07-31 09:10:55 +02:00
|
|
|
int error, buffer, last = -1;
|
2012-07-31 11:26:45 +02:00
|
|
|
int disconnected;
|
2012-07-15 09:54:00 +02:00
|
|
|
extern int optind;
|
|
|
|
|
|
|
|
setbuf(stdout, NULL);
|
2012-07-31 11:24:50 +02:00
|
|
|
while ((ch = getopt(argc, argv, "i:t:v")) != -1) {
|
2012-07-15 09:54:00 +02:00
|
|
|
switch (ch) {
|
2012-07-31 11:24:50 +02:00
|
|
|
case 'i':
|
|
|
|
interval = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
timeout = atoi(optarg);
|
|
|
|
break;
|
2012-07-15 09:54:00 +02:00
|
|
|
case 'v':
|
2012-07-30 20:49:29 +02:00
|
|
|
verbose++;
|
2012-07-15 09:54:00 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc != 1)
|
|
|
|
usage();
|
|
|
|
|
2012-07-31 11:24:50 +02:00
|
|
|
if (interval <= 0 || timeout <= 0)
|
|
|
|
errx(2, "interval and timeout must be > 0");
|
|
|
|
|
|
|
|
/* force timeout >= interval */
|
|
|
|
if (timeout < interval) {
|
|
|
|
timeout = interval;
|
|
|
|
warnx("adjusting timeout to %ld ms "
|
|
|
|
"(must be greater than interval)", timeout);
|
|
|
|
}
|
2012-07-15 09:54:00 +02:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = PF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
|
|
error = getaddrinfo(argv[0], "echo", &hints, &res0);
|
|
|
|
if (error)
|
2012-07-31 11:27:03 +02:00
|
|
|
errx(1, "%s: %s", argv[0], gai_strerror(error));
|
2012-07-15 09:54:00 +02:00
|
|
|
|
|
|
|
for (res = res0; res != NULL; res = res->ai_next) {
|
|
|
|
sock = socket(res->ai_family, res->ai_socktype,
|
|
|
|
res->ai_protocol);
|
|
|
|
if (sock != -1) {
|
|
|
|
server = res->ai_addr;
|
|
|
|
serverlen = res->ai_addrlen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sock == -1)
|
|
|
|
err(1, "socket");
|
|
|
|
|
2012-07-31 08:44:18 +02:00
|
|
|
disconnected = 0;
|
2012-07-15 09:54:00 +02:00
|
|
|
memset(&client, 0, sizeof(client));
|
2012-07-30 20:49:29 +02:00
|
|
|
|
|
|
|
/* timer values */
|
2012-07-31 11:24:50 +02:00
|
|
|
itv.it_interval.tv_usec = interval*1000;
|
2012-07-31 08:26:05 +02:00
|
|
|
itv.it_interval.tv_sec = 0;
|
2012-07-31 11:24:50 +02:00
|
|
|
itv.it_value.tv_usec = interval*1000;
|
2012-07-31 08:26:05 +02:00
|
|
|
itv.it_value.tv_sec = 0;
|
|
|
|
if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
|
|
|
|
err(2, "setitimer");
|
|
|
|
|
|
|
|
signal(SIGALRM, send_packet);
|
2012-07-30 20:49:29 +02:00
|
|
|
|
2012-07-31 11:26:45 +02:00
|
|
|
gettimeofday(&last_ts, NULL);
|
2012-07-30 20:49:29 +02:00
|
|
|
|
2012-07-15 09:54:00 +02:00
|
|
|
while (1) {
|
2012-07-31 11:26:45 +02:00
|
|
|
/* poll() loop to handle interruptions by SIGALRM */
|
2012-07-30 20:49:29 +02:00
|
|
|
while (1) {
|
|
|
|
pfd[0].fd = sock;
|
|
|
|
pfd[0].events = POLLIN;
|
2012-07-31 11:24:50 +02:00
|
|
|
nfds = poll(pfd, 1, timeout);
|
2012-07-31 08:26:05 +02:00
|
|
|
gettimeofday(&now, NULL);
|
2012-07-31 11:26:45 +02:00
|
|
|
timersub(&now, &last_ts, &diff);
|
2012-07-30 20:49:29 +02:00
|
|
|
if (nfds > 0)
|
|
|
|
break;
|
|
|
|
if (nfds == -1 && errno != EINTR)
|
|
|
|
warn("poll error");
|
2012-07-31 11:24:50 +02:00
|
|
|
if (diff.tv_sec > 0 || diff.tv_usec > timeout*1000) {
|
2012-07-30 20:49:29 +02:00
|
|
|
disconnected++;
|
|
|
|
nfds = 0;
|
|
|
|
break;
|
|
|
|
}
|
2012-07-15 09:54:00 +02:00
|
|
|
}
|
2012-07-30 20:49:29 +02:00
|
|
|
if ((nfds == 0)) {
|
2012-07-31 08:44:18 +02:00
|
|
|
if (verbose)
|
|
|
|
printf("%d packet(s) dropped in %ld.%06ld s\n",
|
|
|
|
seq - last, diff.tv_sec, diff.tv_usec);
|
|
|
|
|
2012-07-30 20:49:29 +02:00
|
|
|
if (disconnected == 1) {
|
2012-07-31 14:15:27 +02:00
|
|
|
tm = localtime((time_t *)&last_ts.tv_sec);
|
2012-07-15 09:54:00 +02:00
|
|
|
strftime(buf, sizeof(buf), "%F %T", tm);
|
2012-07-30 20:49:29 +02:00
|
|
|
printf("%s.%06ld: lost connection\n",
|
2012-07-31 14:15:27 +02:00
|
|
|
buf, last_ts.tv_usec);
|
2012-07-15 09:54:00 +02:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-30 20:49:29 +02:00
|
|
|
addrlen = sizeof(client);
|
2012-07-15 09:54:00 +02:00
|
|
|
if ((received = recvfrom(sock, &buffer, sizeof(buffer),
|
|
|
|
MSG_DONTWAIT,
|
|
|
|
(struct sockaddr *) &client,
|
|
|
|
&addrlen)) != sizeof(buffer)) {
|
|
|
|
warn("recvfrom");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose && (serverlen != addrlen ||
|
|
|
|
memcmp(&client, server, addrlen) != 0)) {
|
|
|
|
if ((error = getnameinfo((struct sockaddr *)&client,
|
|
|
|
addrlen, name, sizeof(name),
|
|
|
|
NULL, 0, NI_DGRAM)) != 0) {
|
|
|
|
warnx("%s", gai_strerror(error));
|
|
|
|
} else {
|
|
|
|
warnx("received data from unknown host %s",
|
|
|
|
name);
|
|
|
|
}
|
|
|
|
}
|
2012-07-31 08:44:18 +02:00
|
|
|
if (disconnected) {
|
2012-07-30 20:49:29 +02:00
|
|
|
tm = localtime((time_t *)&now.tv_sec);
|
2012-07-15 09:54:00 +02:00
|
|
|
strftime(buf, sizeof(buf), "%F %T", tm);
|
2012-07-31 14:17:35 +02:00
|
|
|
printf("%s.%06ld: connection is back "
|
2012-07-31 14:38:58 +02:00
|
|
|
"dropped %d packets\n",
|
2012-07-31 14:17:35 +02:00
|
|
|
buf, now.tv_usec, seq - last);
|
2012-07-31 08:44:18 +02:00
|
|
|
disconnected = 0;
|
2012-07-15 09:54:00 +02:00
|
|
|
}
|
|
|
|
last = buffer;
|
2012-07-31 11:26:45 +02:00
|
|
|
gettimeofday(&last_ts, NULL);
|
2012-07-31 08:44:18 +02:00
|
|
|
if (verbose)
|
|
|
|
printf("received %d %ld.%06ld\n", buffer,
|
|
|
|
(long)diff.tv_sec, diff.tv_usec);
|
2012-07-15 09:54:00 +02:00
|
|
|
}
|
|
|
|
close(sock);
|
|
|
|
exit(0);
|
|
|
|
}
|