Skip to content

Commit

Permalink
Add support for quiet (-q) mode operation
Browse files Browse the repository at this point in the history
Signed-off-by: Joachim Wiberg <[email protected]>
  • Loading branch information
troglobit committed Mar 5, 2024
1 parent 496b985 commit 921949d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

#include "common.h"

int verbose = 1;


int ip_address_parse(const char *string, struct ip_address *ip)
{
int ret;
Expand Down
4 changes: 4 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <netinet/ip.h>
#include <netinet/ip6.h>

#define logit(fmt, args...) if (verbose) printf(fmt, ##args)

struct ip_address {
int family;
union {
Expand All @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion mreceive.8
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...
Expand Down Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions mreceive.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down Expand Up @@ -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], '-'))) {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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) {
Expand All @@ -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++;
}
Expand Down
5 changes: 4 additions & 1 deletion msend.8
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions msend.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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\
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 921949d

Please sign in to comment.