Skip to content

Commit

Permalink
logger: add support for multicast egress options
Browse files Browse the repository at this point in the history
Signed-off-by: Joachim Wiberg <[email protected]>
  • Loading branch information
troglobit committed Jan 2, 2025
1 parent d880e03 commit 837f994
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
12 changes: 12 additions & 0 deletions man/logger.1
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
.Op Fl H Ar HOSTNAME
.Op Fl I Ar PID
.Op Fl m Ar MSGID
.Op Fl o Ar OPTS
.Op Fl p Ar PRIO
.Op Fl P Ar PORT
.Op Fl r Ar SIZE:NUM
Expand Down Expand Up @@ -150,6 +151,17 @@ or sending remote in correctly formatted RFC5424 style.
.It Fl n
Open log file immediately
.Ql ( LOG_NDELAY ) .
.It Fl o Ar OPTS
Set multicast options, separate multiple options with comma:
.Bl -tag
.It Ar iface=IFNAME
Outbound interface when sending to a multicast group address. By
default the kernel relies on the routing table, falling back to the
default route if nothing more specific exists.
.It Ar ttl=<1..255>
IP TTL of outbound syslog messages when sending to a multicast group.
The default TTL for multicast is 1, meaning it is confined to the LAN.
.El
.It Fl P Ar port
Send the message to the specified
.Ar port
Expand Down
52 changes: 50 additions & 2 deletions src/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,42 @@ static int parse_prio(const char *arg, int *f, int *l)
return 0;
}

static int parse_opts(char *arg, char **iface, int *ttl)
{
char *subopts = arg;
char *value;
enum {
IFACE_OPT = 0,
TTL_OPT,
};
char *const token[] = {
[IFACE_OPT] = "iface",
[TTL_OPT] = "ttl",
NULL
};

while (*subopts != '\0') {
switch (getsubopt(&subopts, token, &value)) {
case IFACE_OPT:
if (!value)
return 1;
*iface = value;
break;
case TTL_OPT:
if (!value)
return 1;
*ttl = atoi(value);
if (*ttl < 1 || *ttl > 255)
return 1;
break;
default:
return 1;
}
}

return 0;
}

static int usage(int code)
{
printf("Usage: logger [OPTIONS] [MESSAGE]\n"
Expand All @@ -256,6 +292,9 @@ static int usage(int code)
#endif
" -m MSGID Log message using this RFC5424 style MSGID\n"
" -n Open log file immediately (LOG_NDELAY)\n"
" -o OPT Set outbound multicast options:\n"
" iface=IFNAME\n"
" ttl=1-255\n"
" -p PRIO Log message priority (numeric or facility.severity pair)\n"
" -P PORT Use PORT (or named UDP service) for remote server, default: syslog\n"
" -r S[:R] Enable log file rotation, default: 200 kB \e[4ms\e[0mize, 5 \e[4mr\e[0motations\n"
Expand Down Expand Up @@ -286,12 +325,14 @@ int main(int argc, char *argv[])
struct sockaddr sa;
int allow_kmsg = 0;
char buf[512] = "";
char *iface = NULL;
int log_opts = 0;
FILE *fp = NULL;
int c, num = 5;
int rotate = 0;
int ttl = 1;

while ((c = getopt(argc, argv, "46?bcd:f:h:H:iI:km:np:P:r:st:u:v")) != EOF) {
while ((c = getopt(argc, argv, "46?bcd:f:h:H:iI:km:no:p:P:r:st:u:v")) != EOF) {
switch (c) {
case '4':
family = AF_INET;
Expand Down Expand Up @@ -351,6 +392,11 @@ int main(int argc, char *argv[])
log_opts |= LOG_NDELAY;
break;

case 'o':
if (parse_opts(optarg, &iface, &ttl))
return usage(1);
break;

case 'p':
if (parse_prio(optarg, &facility, &severity))
return usage(1);
Expand Down Expand Up @@ -440,7 +486,9 @@ int main(int argc, char *argv[])
return fclose(fp);
}
} else if (host) {
log.log_host = &sa;
log.log_host = &sa;
log.log_iface = iface;
log.log_ttl = ttl;
if (nslookup(host, svcname, family, &sa))
return 1;
log_opts |= LOG_NDELAY;
Expand Down

0 comments on commit 837f994

Please sign in to comment.