sys/net/uhcp: use LOG_ functions

Using `gnrc_border_router` with `uhcp` is quite noisy.
uhcpc will regularly refresh the prefix and print a bunch of status messages.

Allow the user to tone it down by setting a higher `LOG_LEVEL`.
For this, convert calls to `printf()` and `puts()` to `LOG_xxx()`.

This requires a dummy header for `uhcpd`.
This commit is contained in:
Benjamin Valentin 2020-09-28 11:00:18 +02:00 committed by Benjamin Valentin
parent f862166cbb
commit f163f1580b
5 changed files with 63 additions and 18 deletions

View File

@ -11,7 +11,7 @@ RIOT_INCLUDE=$(RIOTBASE)/sys/include
SRCS:=$(UHCP_DIR)/uhcp.c uhcpd.c SRCS:=$(UHCP_DIR)/uhcp.c uhcpd.c
HDRS:=$(RIOT_INCLUDE)/net/uhcp.h HDRS:=$(RIOT_INCLUDE)/net/uhcp.h
bin/uhcpd: $(SRCS) $(HDRS) bin/uhcpd: $(SRCS) $(HDRS)
$(CC) $(CFLAGS) $(CFLAGS_EXTRA) -I$(RIOT_INCLUDE) $(SRCS) -o $@ $(CC) $(CFLAGS) $(CFLAGS_EXTRA) -I$(RIOT_INCLUDE) -I. $(SRCS) -o $@
clean: clean:
rm -f bin/uhcpd rm -f bin/uhcpd

46
dist/tools/uhcpd/log.h vendored Normal file
View File

@ -0,0 +1,46 @@
/*
* Copyright (C) 2020 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup core_util
* @{
*
* @file
* @brief System logging dummy header
*
* This header offers a bunch of "LOG_*" functions that just use printf,
* without honouring a verbosity level.
*
* This is a dummy header for the purpose of sharing code that uses the
* LOG_xxx() functions between RIOT and host utilities without having to
* manually include all dependencies of the 'real' log module.
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*/
#ifndef LOG_H
#define LOG_H
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @name Logging convenience defines
* @{
*/
#define LOG_ERROR(...) printf(__VA_ARGS__)
#define LOG_WARNING(...) printf(__VA_ARGS__)
#define LOG_INFO(...) printf(__VA_ARGS__)
#define LOG_DEBUG(...) printf(__VA_ARGS__)
/** @} */
#endif /* LOG_H */
/** @} */

View File

@ -6,29 +6,28 @@
* directory for more details. * directory for more details.
*/ */
#include <stdio.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <string.h> #include <string.h>
#include "log.h"
#include "net/uhcp.h" #include "net/uhcp.h"
void uhcp_handle_udp(uint8_t *buf, size_t len, uint8_t *src, uint16_t port, uhcp_iface_t iface) void uhcp_handle_udp(uint8_t *buf, size_t len, uint8_t *src, uint16_t port, uhcp_iface_t iface)
{ {
char addr_str[INET6_ADDRSTRLEN]; char addr_str[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, src, addr_str, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, src, addr_str, INET6_ADDRSTRLEN);
printf("got packet from %s port %u\n", addr_str, (unsigned)port); LOG_INFO("got packet from %s port %u\n", addr_str, (unsigned)port);
if (len < sizeof(uhcp_req_t)) { if (len < sizeof(uhcp_req_t)) {
puts("error: packet too small."); LOG_ERROR("error: packet too small.\n");
return; return;
} }
uhcp_hdr_t *hdr = (uhcp_hdr_t *)buf; uhcp_hdr_t *hdr = (uhcp_hdr_t *)buf;
if (! (ntohl(hdr->uhcp_magic) == UHCP_MAGIC)) { if (! (ntohl(hdr->uhcp_magic) == UHCP_MAGIC)) {
puts("error: wrong magic number."); LOG_ERROR("error: wrong magic number.\n");
return; return;
} }
@ -37,14 +36,14 @@ void uhcp_handle_udp(uint8_t *buf, size_t len, uint8_t *src, uint16_t port, uhcp
type = hdr->ver_type & 0xF; type = hdr->ver_type & 0xF;
if (ver != UHCP_VER) { if (ver != UHCP_VER) {
puts("error: wrong protocol version."); LOG_ERROR("error: wrong protocol version.\n");
} }
switch(type) { switch(type) {
#ifdef UHCP_SERVER #ifdef UHCP_SERVER
case UHCP_REQ: case UHCP_REQ:
if (len < sizeof(uhcp_req_t)) { if (len < sizeof(uhcp_req_t)) {
puts("error: request too small\n"); LOG_ERROR("error: request too small\n");
} }
else { else {
uhcp_handle_req((uhcp_req_t*)hdr, src, port, iface); uhcp_handle_req((uhcp_req_t*)hdr, src, port, iface);
@ -58,7 +57,7 @@ void uhcp_handle_udp(uint8_t *buf, size_t len, uint8_t *src, uint16_t port, uhcp
if ((len < sizeof(uhcp_push_t)) if ((len < sizeof(uhcp_push_t))
|| (len < (sizeof(uhcp_push_t) + (push->prefix_len >> 3))) || (len < (sizeof(uhcp_push_t) + (push->prefix_len >> 3)))
) { ) {
puts("error: request too small\n"); LOG_ERROR("error: request too small\n");
} }
else { else {
uhcp_handle_push(push, src, port, iface); uhcp_handle_push(push, src, port, iface);
@ -67,7 +66,7 @@ void uhcp_handle_udp(uint8_t *buf, size_t len, uint8_t *src, uint16_t port, uhcp
} }
#endif #endif
default: default:
puts("error: unexpected type\n"); LOG_ERROR("error: unexpected type\n");
} }
} }
@ -87,7 +86,7 @@ void uhcp_handle_req(uhcp_req_t *req, uint8_t *src, uint16_t port, uhcp_iface_t
int res = udp_sendto(packet, sizeof(packet), src, port, iface); int res = udp_sendto(packet, sizeof(packet), src, port, iface);
if (res == -1) { if (res == -1) {
printf("uhcp_handle_req(): udp_sendto() res=%i\n", res); LOG_ERROR("uhcp_handle_req(): udp_sendto() res=%i\n", res);
} }
} }
#endif /* UHCP_SERVER */ #endif /* UHCP_SERVER */
@ -105,7 +104,7 @@ void uhcp_handle_push(uhcp_push_t *req, uint8_t *src, uint16_t port, uhcp_iface_
inet_ntop(AF_INET6, prefix, prefix_str, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, prefix, prefix_str, INET6_ADDRSTRLEN);
printf("uhcp: push from %s:%u prefix=%s/%u\n", addr_str, (unsigned)port, prefix_str, req->prefix_len); LOG_INFO("uhcp: push from %s:%u prefix=%s/%u\n", addr_str, (unsigned)port, prefix_str, req->prefix_len);
uhcp_handle_prefix(prefix, req->prefix_len, 0xFFFF, src, iface); uhcp_handle_prefix(prefix, req->prefix_len, 0xFFFF, src, iface);
} }
#endif #endif

View File

@ -7,8 +7,8 @@
*/ */
#include <arpa/inet.h> #include <arpa/inet.h>
#include <stdio.h>
#include "log.h"
#include "net/af.h" #include "net/af.h"
#include "net/sock/udp.h" #include "net/sock/udp.h"
#include "net/uhcp.h" #include "net/uhcp.h"
@ -39,14 +39,14 @@ void uhcp_client(uhcp_iface_t iface)
/* create listening socket */ /* create listening socket */
int res = sock_udp_create(&sock, &local, NULL, 0); int res = sock_udp_create(&sock, &local, NULL, 0);
if (res < 0) { if (res < 0) {
puts("uhcp_client(): cannot create listening socket"); LOG_ERROR("uhcp_client(): cannot create listening socket\n");
return; return;
} }
uint8_t buf[sizeof(uhcp_push_t) + 16]; uint8_t buf[sizeof(uhcp_push_t) + 16];
while(1) { while(1) {
puts("uhcp_client(): sending REQ..."); LOG_INFO("uhcp_client(): sending REQ...\n");
sock_udp_send(&sock, &req, sizeof(uhcp_req_t), &req_target); sock_udp_send(&sock, &req, sizeof(uhcp_req_t), &req_target);
res = sock_udp_recv(&sock, buf, sizeof(buf), 10U*US_PER_SEC, &remote); res = sock_udp_recv(&sock, buf, sizeof(buf), 10U*US_PER_SEC, &remote);
if (res > 0) { if (res > 0) {
@ -54,7 +54,7 @@ void uhcp_client(uhcp_iface_t iface)
xtimer_sleep(60); xtimer_sleep(60);
} }
else { else {
puts("uhcp_client(): no reply received"); LOG_ERROR("uhcp_client(): no reply received\n");
} }
} }
} }

View File

@ -146,7 +146,7 @@ void uhcp_handle_prefix(uint8_t *prefix, uint8_t prefix_len, uint16_t lifetime,
if ((_prefix_len == prefix_len) && if ((_prefix_len == prefix_len) &&
(ipv6_addr_match_prefix(&_prefix, (ipv6_addr_match_prefix(&_prefix,
(ipv6_addr_t *)prefix) >= prefix_len)) { (ipv6_addr_t *)prefix) >= prefix_len)) {
LOG_WARNING("gnrc_uhcpc: uhcp_handle_prefix(): got same prefix " LOG_INFO("gnrc_uhcpc: uhcp_handle_prefix(): got same prefix "
"again\n"); "again\n");
#ifdef MODULE_GNRC_SIXLOWPAN_CTX #ifdef MODULE_GNRC_SIXLOWPAN_CTX
if (gnrc_netif_is_6ln(gnrc_netif_get_by_pid(gnrc_wireless_interface))) { if (gnrc_netif_is_6ln(gnrc_netif_get_by_pid(gnrc_wireless_interface))) {