From 921949dc39d06dddfae7a763724f3bb18453d685 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Tue, 5 Mar 2024 05:43:39 +0100 Subject: [PATCH] Add support for quiet (-q) mode operation Signed-off-by: Joachim Wiberg --- README.md | 5 +++++ common.c | 3 +++ common.h | 4 ++++ mreceive.8 | 5 ++++- mreceive.c | 14 +++++++++----- msend.8 | 5 ++++- msend.c | 8 ++++++-- 7 files changed, 35 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 46708ba..223a8c5 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,11 @@ group:port combination by the `msend` command. Specify the interval in milliseconds between two transmitted packets. The default value is 1000 milliseconds. +* `-q` + + Quiet mode, don't print sending or receiving messages. Errors are + still printed. + * `-text "text"` Specify a message text which is sent as the payload of the packets and diff --git a/common.c b/common.c index f68676a..cd08700 100644 --- a/common.c +++ b/common.c @@ -9,6 +9,9 @@ #include "common.h" +int verbose = 1; + + int ip_address_parse(const char *string, struct ip_address *ip) { int ret; diff --git a/common.h b/common.h index db29273..faeeab6 100644 --- a/common.h +++ b/common.h @@ -8,6 +8,8 @@ #include #include +#define logit(fmt, args...) if (verbose) printf(fmt, ##args) + struct ip_address { int family; union { @@ -25,6 +27,8 @@ struct sock { int fd; }; +extern int verbose; + int ip_address_parse(const char *string, struct ip_address *ip); int socket_create(struct sock *s, int family, int port, struct ip_address *saddr, const char *if_name); diff --git a/mreceive.8 b/mreceive.8 index 76c20c0..21fc563 100644 --- a/mreceive.8 +++ b/mreceive.8 @@ -11,7 +11,7 @@ .Nd receive UDP multicast messages and display them .Sh SYNOPSIS .Nm -.Op Fl 46hnv +.Op Fl 46hnvq .Op Fl g Ar GROUP .Op Fl i Ar ADDRESS .Op ... @@ -51,6 +51,9 @@ The default group is Specify the UDP port number used by the multicast group. The default port number is .Nm 4444 . +.It Fl q +Quiet mode, do not log to stdout every time a message is received. +Errors are stil logged. .It Fl i Ar ADDRESS Specify the IP addresses of one or more interfaces to receive multicast packets. The default value is diff --git a/mreceive.c b/mreceive.c index 6956308..cf8ba40 100644 --- a/mreceive.c +++ b/mreceive.c @@ -67,6 +67,7 @@ Usage: mreceive [-46hnv] [-g GROUP] [-i ADDR] ... [-i ADDR] [-I INTERFACE]\n\ -n Interpret the contents of the message as a number instead of\n\ a string of characters. Use this with `msend -n`\n\ -p PORT UDP port number used in the multicast packets. Default: 4444\n\ + -q Quiet, don't print every received packet, errors still printed\n\ -v Print version information.\n\n"); } @@ -116,6 +117,9 @@ int main(int argc, char *argv[]) TEST_PORT = atoi(argv[opt]); opt++; } + } else if (strcmp(argv[opt], "-q") == 0) { + opt++; + verbose = 0; } else if (strcmp(argv[opt], "-i") == 0) { opt++; if ((opt < argc) && !(strchr(argv[opt], '-'))) { @@ -186,7 +190,7 @@ int main(int argc, char *argv[]) if (ret) exit(1); - printf("Now receiving from multicast group: %s\n", TEST_ADDR); + logit("Now receiving from multicast group: %s\n", TEST_ADDR); for (i = 0;; i++) { char from_buf[INET6_ADDRSTRLEN]; @@ -224,9 +228,9 @@ int main(int argc, char *argv[]) numreceived = (unsigned int)buf[0] + ((unsigned int)(buf[1]) << 8) + ((unsigned int)(buf[2]) << 16) + ((unsigned int)(buf[3]) >> 24); - fprintf(stdout, "%5d\t%s:%5d\t%d.%03d\t%5u\n", counter, - from_buf, socket_get_port(&from), - curtime / 1000000, (curtime % 1000000) / 1000, numreceived); + logit("%5d\t%s:%5d\t%d.%03d\t%5u\n", counter, + from_buf, socket_get_port(&from), + curtime / 1000000, (curtime % 1000000) / 1000, numreceived); fflush(stdout); rcvCountNew = numreceived; if (rcvCountNew > rcvCountOld + 1) { @@ -244,7 +248,7 @@ int main(int argc, char *argv[]) } rcvCountOld = rcvCountNew; } else { - printf("Receive msg %d from %s:%d: %s\n", counter, from_buf, socket_get_port(&from), buf); + logit("Receive msg %d from %s:%d: %s\n", counter, from_buf, socket_get_port(&from), buf); } counter++; } diff --git a/msend.8 b/msend.8 index 6e137d6..6e53c3e 100644 --- a/msend.8 +++ b/msend.8 @@ -11,7 +11,7 @@ .Nd send UDP messages to a multicast group .Sh SYNOPSIS .Nm -.Op Fl 46hnv +.Op Fl 46hnvq .Op Fl c Ar NUM .Op Fl g Ar GROUP .Op Fl join @@ -72,6 +72,9 @@ The interface on which to send. Can be specified as an alternative to .It Fl P Ar PERIOD Specify the interval in milliseconds between two transmitted packets. The default value is 1000 milliseconds. +.It Fl q +Quiet mode, do not log to stdout every time a message is successfully +sent. Errors are stil logged. .It Fl text Ar 'text' Specify a message text which is sent as the payload of the packets and is displayed by the diff --git a/msend.c b/msend.c index 45b33b7..c6163e6 100644 --- a/msend.c +++ b/msend.c @@ -75,9 +75,9 @@ void timer_cb(int signo) if (NUM) { param.buf = (char *)(&counter); param.len = sizeof(counter); - printf("Sending msg %d, TTL %d, to %s:%d\n", counter, TTL_VALUE, TEST_ADDR, TEST_PORT); + logit("Sending msg %d, TTL %d, to %s:%d\n", counter, TTL_VALUE, TEST_ADDR, TEST_PORT); } else { - printf("Sending msg %d, TTL %d, to %s:%d: %s\n", counter, TTL_VALUE, TEST_ADDR, TEST_PORT, param.buf); + logit("Sending msg %d, TTL %d, to %s:%d: %s\n", counter, TTL_VALUE, TEST_ADDR, TEST_PORT, param.buf); } ret = mc_send(param.s, param.to, param.buf, @@ -113,6 +113,7 @@ Usage: msend [-46hnv] [-c NUM] [-g GROUP] [-p PORT] [-join] [-i ADDRESS]\n\ -n Encode -text argument as a number instead of a string.\n\ -p PORT UDP port number used in the multicast packets. Default: 4444\n\ -P PERIOD Interval in milliseconds between packets. Default 1000 msec\n\ + -q Quiet, don't print 'Sedning msg ...' for every packet\n\ -t TTL The TTL value (1-255) used in the packets. You must set\n\ this higher if you want to route the traffic, otherwise\n\ the first router will drop the packets! Default: 1\n\ @@ -209,6 +210,9 @@ int main(int argc, char *argv[]) SLEEP_TIME = atoi(argv[opt]); opt++; } + } else if (strcmp(argv[opt], "-q") == 0) { + opt++; + verbose = 0; } else if (strcmp(argv[opt], "-n") == 0) { opt++; NUM = 1;