Commit 83bb333f authored by Alessandro Rubini's avatar Alessandro Rubini

net: optional syslog implementation

This costs 1K in binary size, and it currently only prints an alive
message every 10 seconds.

The time format is still wrong, so the syslog daemon adds its own
timestamp.
Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent d2be0d66
......@@ -83,6 +83,14 @@ config CMD_CONFIG
reports the current configuration. This adds half a kilobyte
to the binary size (100b for the code plus the .config file).
config SYSLOG
depends on IP
boolean "Include syslog client support"
help
This enable a UDP syslog client, configured by a shell command.
The user (or init script) must use "syslog <ipaddr> <macaddr>"
to enable it. The special "off" ipaddr disables syslog.
#
# This is a set of configuration options that should not be changed by
# normal users. If the "developer" menu is used, the binary is tainted.
......
......@@ -47,6 +47,12 @@ static struct wrpc_socket __static_rdate_socket = {
};
static struct wrpc_socket *rdate_socket;
/* syslog is selected by Kconfig, so we have weak aliases here */
void __attribute__((weak)) syslog_init(void)
{ }
void __attribute__((weak)) syslog_poll(void)
{ }
unsigned int ipv4_checksum(unsigned short *buf, int shorts)
{
......@@ -83,6 +89,8 @@ void ipv4_init(void)
saddr.ethertype = htons(0x0800);
icmp_socket = ptpd_netif_create_socket(&__static_icmp_socket, &saddr,
PTPD_SOCK_RAW_ETHERNET, 0);
syslog_init();
}
static int bootp_retry = 0;
......@@ -162,6 +170,8 @@ void ipv4_poll(void)
icmp_poll();
rdate_poll();
syslog_poll();
}
void getIP(unsigned char *IP)
......
......@@ -3,3 +3,5 @@ obj-y += lib/usleep.o
obj-$(CONFIG_WR_NODE) += lib/net.o lib/udp.o
obj-$(CONFIG_WR_NODE) += lib/arp.o lib/icmp.o lib/ipv4.o lib/bootp.o
obj-$(CONFIG_SYSLOG) += lib/syslog.o
#include <string.h>
#include <wrc.h>
#include <wrpc.h>
#include "endpoint.h"
#include "minic.h"
#include "shell.h"
#include "pps_gen.h"
#include "ipv4.h"
/* syslog: a tx-only socket: no queue is there */
static struct wrpc_socket __static_syslog_socket = {
.queue.buff = NULL,
.queue.size = 0,
};
static struct wrpc_socket *syslog_socket;
static struct wr_udp_addr syslog_addr;
unsigned char syslog_mac[6];
void syslog_init(void)
{
syslog_socket = ptpd_netif_create_socket(&__static_syslog_socket, NULL,
PTPD_SOCK_UDP, 514 /* time */);
syslog_addr.sport = syslog_addr.dport = htons(514);
}
static uint32_t tics;
static int cmd_syslog(const char *args[])
{
if (args[0] && !strcmp(args[0], "off")) {
syslog_addr.daddr = 0;
return 0;
}
if (!args[1]) {
pp_printf("use: syslog <ipaddr> <macaddr> (or just \"off\"\n");
return -1;
}
decode_ip(args[0], (void *)&syslog_addr.daddr);
decode_mac(args[1], syslog_mac);
print_ip("Syslog parameters: ", (void *)&syslog_addr.daddr, ", ");
print_mac("", syslog_mac, "\n");
tics = 0; /* send the first frame immediately to the new host */
return 0;
}
DEFINE_WRC_COMMAND(mac) = {
.name = "syslog",
.exec = cmd_syslog,
};
void syslog_poll(void)
{
struct wr_sockaddr addr;
char buf[128];
uint64_t secs;
int len;
if (needIP)
return;
if (!syslog_addr.daddr)
return;
if (!tics) /* first time ever */
tics = timer_get_tics() - 1;
if (time_before(timer_get_tics(), tics))
return;
getIP((void *)&syslog_addr.saddr);
shw_pps_gen_get_time(&secs, NULL);
len = pp_sprintf(buf + UDP_END, /* 8 == user + 6 == info */
"<14> %s wr-node: Alive and Well", format_time(secs));
len += UDP_END;
memcpy(&syslog_addr.saddr, ip, 4);
fill_udp((void *)buf, len, &syslog_addr);
memcpy(&addr.mac, syslog_mac, 6);
ptpd_netif_sendto(syslog_socket, &addr, buf, len, 0);
tics += 10 * TICS_PER_SECOND;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment