Skip to content

Commit

Permalink
net: dhcpv4: set log backend net ip via option
Browse files Browse the repository at this point in the history
Adds the option to set the log_backend_net ip via dhcpv4
option 7.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
  • Loading branch information
maass-hamburg committed Feb 22, 2024
1 parent 154cd47 commit b4d90b2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
7 changes: 7 additions & 0 deletions subsys/logging/backends/Kconfig.net
Expand Up @@ -49,6 +49,13 @@ config LOG_BACKEND_NET_AUTOSTART
started by the application later on. Otherwise the logging
thread might block.

config LOG_BACKEND_NET_USE_DHCPV4_OPTION
bool "Use DHCPv4 Log Server Option to configure syslog server"
depends on NET_DHCPV4
help
When enabled the syslog server IP address is read from the DHCPv4
Log Server Option (7).

backend = NET
backend-str = net
source "subsys/logging/Kconfig.template.log_format_config"
Expand Down
46 changes: 46 additions & 0 deletions subsys/net/lib/dhcpv4/dhcpv4.c
Expand Up @@ -28,6 +28,10 @@ LOG_MODULE_REGISTER(net_dhcpv4, CONFIG_NET_DHCPV4_LOG_LEVEL);
#include <zephyr/net/dhcpv4.h>
#include <zephyr/net/dns_resolve.h>

#include <zephyr/logging/log_backend.h>
#include <zephyr/logging/log_backend_net.h>
#include <zephyr/logging/log_ctrl.h>

#include "dhcpv4_internal.h"
#include "ipv4.h"
#include "net_stats.h"
Expand All @@ -44,6 +48,11 @@ static struct k_work_delayable timeout_work;

static struct net_mgmt_event_callback mgmt4_cb;

#if defined(CONFIG_LOG_BACKEND_NET_USE_DHCPV4_OPTION) && \
defined(CONFIG_LOG_BACKEND_NET_AUTOSTART)
extern const struct log_backend *log_backend_net_get(void);
#endif

#if defined(CONFIG_NET_DHCPV4_OPTION_CALLBACKS)
static sys_slist_t option_callbacks;
#endif
Expand Down Expand Up @@ -810,6 +819,43 @@ static bool dhcpv4_parse_options(struct net_pkt *pkt,
break;
}
#endif
#if defined(CONFIG_LOG_BACKEND_NET_USE_DHCPV4_OPTION)
case DHCPV4_OPTIONS_LOG_SERVER: {
struct sockaddr_in log_server = { 0 };

/* Log server option may present 1 or more
* addresses. Each 4 bytes in length. Log
* servers should be listed in order
* of preference. Hence we choose the first
* and skip the rest.
*/
if (length % 4 != 0U) {
NET_ERR("options_log_server, bad length");
return false;
}

if (net_pkt_read(pkt, log_server.sin_addr.s4_addr, 4) < 0 ||
net_pkt_skip(pkt, length - 4U) < 0) {

Check failure on line 838 in subsys/net/lib/dhcpv4/dhcpv4.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

CODE_INDENT

subsys/net/lib/dhcpv4/dhcpv4.c:838 code indent should use tabs where possible

Check warning on line 838 in subsys/net/lib/dhcpv4/dhcpv4.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

SPACE_BEFORE_TAB

subsys/net/lib/dhcpv4/dhcpv4.c:838 please, no space before tabs

Check warning on line 838 in subsys/net/lib/dhcpv4/dhcpv4.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LEADING_SPACE

subsys/net/lib/dhcpv4/dhcpv4.c:838 please, no spaces at the start of a line
NET_ERR("options_log_server, short packet");
return false;
}

log_server.sin_family = AF_INET;
log_backend_net_set_ip((struct sockaddr *)&log_server);

#ifdef CONFIG_LOG_BACKEND_NET_AUTOSTART
const struct log_backend *backend = log_backend_net_get();

if (!log_backend_is_active(backend)) {
log_backend_activate(backend, backend->cb->ctx);
}
#endif

NET_DBG("options_log_server: %s", net_sprint_ipv4_addr(&log_server));

break;
}
#endif
case DHCPV4_OPTIONS_LEASE_TIME:
if (length != 4U) {
NET_ERR("options_lease_time, bad length");
Expand Down
1 change: 1 addition & 0 deletions subsys/net/lib/dhcpv4/dhcpv4_internal.h
Expand Up @@ -54,6 +54,7 @@ struct dhcp_msg {
#define DHCPV4_OPTIONS_SUBNET_MASK 1
#define DHCPV4_OPTIONS_ROUTER 3
#define DHCPV4_OPTIONS_DNS_SERVER 6
#define DHCPV4_OPTIONS_LOG_SERVER 7
#define DHCPV4_OPTIONS_HOST_NAME 12
#define DHCPV4_OPTIONS_REQ_IPADDR 50
#define DHCPV4_OPTIONS_LEASE_TIME 51
Expand Down

0 comments on commit b4d90b2

Please sign in to comment.