Add length & port control options

This commit is contained in:
Matthieu Herrb 2015-09-29 15:28:22 +02:00
parent 9614711996
commit d84b68a63a

27
echoc.c
View file

@ -35,18 +35,22 @@ int verbose = 0;
struct sockaddr *server; struct sockaddr *server;
socklen_t serverlen; socklen_t serverlen;
unsigned int seq = 0; unsigned int seq = 0;
size_t len = 10;
static void static void
usage(void) usage(void)
{ {
errx(2, "usage: echoc [-c nbr][-i ms][-t ms][-v] server"); errx(2, "usage: echoc [-c nbr][-i ms][-l len][-p port][-t ms][-v] server");
} }
static void static void
send_packet(int unused) send_packet(int unused)
{ {
if (sendto(sock, &seq, sizeof(seq), 0, server, char buf[len];
serverlen) != sizeof(seq)) {
snprintf(buf, len, "%d", seq);
if (sendto(sock, buf, len, 0, server,
serverlen) != len) {
if (verbose) if (verbose)
warn("sendto"); warn("sendto");
} }
@ -59,7 +63,8 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char name[NI_MAXHOST]; char name[NI_MAXHOST];
char buf[80]; char *buf;
char *port = "echo";
struct sockaddr_storage client; struct sockaddr_storage client;
struct addrinfo hints, *res, *res0; struct addrinfo hints, *res, *res0;
struct itimerval itv; struct itimerval itv;
@ -78,7 +83,7 @@ main(int argc, char *argv[])
extern int optind; extern int optind;
setbuf(stdout, NULL); setbuf(stdout, NULL);
while ((ch = getopt(argc, argv, "c:i:t:v")) != -1) { while ((ch = getopt(argc, argv, "c:i:l:p:t:v")) != -1) {
switch (ch) { switch (ch) {
case 'c': case 'c':
we_count++; we_count++;
@ -87,6 +92,12 @@ main(int argc, char *argv[])
case 'i': case 'i':
interval = atoi(optarg); interval = atoi(optarg);
break; break;
case 'l':
len = atoi(optarg);
break;
case 'p':
port = optarg;
break;
case 't': case 't':
timeout = atoi(optarg); timeout = atoi(optarg);
break; break;
@ -117,7 +128,7 @@ main(int argc, char *argv[])
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC; hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM; hints.ai_socktype = SOCK_DGRAM;
error = getaddrinfo(argv[0], "echo", &hints, &res0); error = getaddrinfo(argv[0], port, &hints, &res0);
if (error) if (error)
errx(1, "%s: %s", argv[0], gai_strerror(error)); errx(1, "%s: %s", argv[0], gai_strerror(error));
@ -148,6 +159,10 @@ main(int argc, char *argv[])
gettimeofday(&last_ts, NULL); gettimeofday(&last_ts, NULL);
buf = malloc(len);
if (buf == NULL)
err(2, "malloc receive buffer");
while (1) { while (1) {
/* poll() loop to handle interruptions by SIGALRM */ /* poll() loop to handle interruptions by SIGALRM */
while (1) { while (1) {