dhcp: merge branch 'bg/nettools-log'

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/337
This commit is contained in:
Beniamino Galvani 2019-11-22 10:36:20 +01:00
commit 234cb5a923
6 changed files with 142 additions and 0 deletions

View file

@ -513,6 +513,7 @@ static int n_dhcp4_c_connection_new_message(NDhcp4CConnection *connection,
n_dhcp4_c_connection_init_header(connection, header);
message->userdata.type = type;
message->userdata.message_type = message_type;
/*
* Note that some implementations expect the MESSAGE_TYPE option to be
@ -690,6 +691,7 @@ int n_dhcp4_c_connection_select_new(NDhcp4CConnection *connection,
*/
message->userdata.start_time = offer->userdata.start_time;
message->userdata.base_time = offer->userdata.base_time;
message->userdata.client_addr = client.s_addr;
n_dhcp4_incoming_get_xid(offer, &xid);
n_dhcp4_outgoing_set_xid(message, xid);
@ -763,6 +765,7 @@ int n_dhcp4_c_connection_renew_new(NDhcp4CConnection *connection,
if (r)
return r;
message->userdata.client_addr = connection->client_ip;
*requestp = message;
message = NULL;
return 0;
@ -796,6 +799,7 @@ int n_dhcp4_c_connection_rebind_new(NDhcp4CConnection *connection,
if (r)
return r;
message->userdata.client_addr = connection->client_ip;
*requestp = message;
message = NULL;
return 0;
@ -847,6 +851,7 @@ int n_dhcp4_c_connection_decline_new(NDhcp4CConnection *connection,
return r;
}
message->userdata.client_addr = client.s_addr;
*requestp = message;
message = NULL;
return 0;
@ -889,6 +894,7 @@ int n_dhcp4_c_connection_inform_new(NDhcp4CConnection *connection,
if (r)
return r;
message->userdata.client_addr = connection->client_ip;
*requestp = message;
message = NULL;
return 0;
@ -955,9 +961,36 @@ int n_dhcp4_c_connection_release_new(NDhcp4CConnection *connection,
return 0;
}
static const char *message_type_to_str(uint8_t type) {
switch (type) {
case N_DHCP4_MESSAGE_DISCOVER:
return "DISCOVER";
case N_DHCP4_MESSAGE_OFFER:
return "OFFER";
case N_DHCP4_MESSAGE_REQUEST:
return "REQUEST";
case N_DHCP4_MESSAGE_DECLINE:
return "DECLINE";
case N_DHCP4_MESSAGE_ACK:
return "ACK";
case N_DHCP4_MESSAGE_NAK:
return "NACK";
case N_DHCP4_MESSAGE_RELEASE:
return "RELEASE";
case N_DHCP4_MESSAGE_INFORM:
return "INFORM";
case N_DHCP4_MESSAGE_FORCERENEW:
return "FORCERENEW";
default:
return "UNKNOWN";
}
}
static int n_dhcp4_c_connection_send_request(NDhcp4CConnection *connection,
NDhcp4Outgoing *request,
uint64_t timestamp) {
char server_addr[INET_ADDRSTRLEN];
char client_addr[INET_ADDRSTRLEN];
int r;
/*
@ -1015,6 +1048,22 @@ static int n_dhcp4_c_connection_send_request(NDhcp4CConnection *connection,
c_assert(0);
}
if (request->userdata.client_addr == INADDR_ANY) {
n_dhcp4_c_log(connection->client_config, LOG_INFO,
"sent %s to %s",
message_type_to_str(request->userdata.message_type),
inet_ntop(AF_INET, &connection->server_ip,
server_addr, sizeof(server_addr)));
} else {
n_dhcp4_c_log(connection->client_config, LOG_INFO,
"sent %s of %s to %s",
message_type_to_str(request->userdata.message_type),
inet_ntop(AF_INET, &request->userdata.client_addr,
client_addr, sizeof(client_addr)),
inet_ntop(AF_INET, &connection->server_ip,
server_addr, sizeof(server_addr)));
}
++request->userdata.n_send;
return 0;
}
@ -1066,6 +1115,8 @@ int n_dhcp4_c_connection_dispatch_timer(NDhcp4CConnection *connection,
int n_dhcp4_c_connection_dispatch_io(NDhcp4CConnection *connection,
NDhcp4Incoming **messagep) {
_c_cleanup_(n_dhcp4_incoming_freep) NDhcp4Incoming *message = NULL;
char serv_addr[INET_ADDRSTRLEN];
char client_addr[INET_ADDRSTRLEN];
uint8_t type;
int r;
@ -1118,6 +1169,22 @@ int n_dhcp4_c_connection_dispatch_io(NDhcp4CConnection *connection,
if (r)
return r;
if (type == N_DHCP4_MESSAGE_OFFER || type == N_DHCP4_MESSAGE_ACK) {
n_dhcp4_c_log(connection->client_config, LOG_INFO,
"received %s of %s from %s",
message_type_to_str(type),
inet_ntop(AF_INET, &message->message.header.yiaddr,
client_addr, sizeof(client_addr)),
inet_ntop(AF_INET, &message->message.header.siaddr,
serv_addr, sizeof(serv_addr)));
} else {
n_dhcp4_c_log(connection->client_config, LOG_INFO,
"received %s from %s",
message_type_to_str(type),
inet_ntop(AF_INET, &message->message.header.siaddr,
serv_addr, sizeof(serv_addr)));
}
switch (type) {
case N_DHCP4_MESSAGE_OFFER:
case N_DHCP4_MESSAGE_ACK:

View file

@ -94,6 +94,9 @@ int n_dhcp4_client_config_dup(NDhcp4ClientConfig *config, NDhcp4ClientConfig **d
dup->n_mac = config->n_mac;
memcpy(dup->broadcast_mac, config->broadcast_mac, sizeof(dup->broadcast_mac));
dup->n_broadcast_mac = config->n_broadcast_mac;
dup->log.level = config->log.level;
dup->log.func = config->log.func;
dup->log.data = config->log.data;
r = n_dhcp4_client_config_set_client_id(dup,
config->client_id,
@ -248,6 +251,15 @@ _c_public_ int n_dhcp4_client_config_set_client_id(NDhcp4ClientConfig *config, c
return 0;
}
_c_public_ void n_dhcp4_client_config_set_log_level(NDhcp4ClientConfig *config, int level) {
config->log.level = level;
}
_c_public_ void n_dhcp4_client_config_set_log_func(NDhcp4ClientConfig *config, NDhcp4LogFunc func, void *data) {
config->log.func = func;
config->log.data = data;
}
/**
* n_dhcp4_c_event_node_new() - allocate new event
* @nodep: output argument for new event

View file

@ -11,6 +11,7 @@
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <syslog.h>
#include "n-dhcp4.h"
typedef struct NDhcp4CConnection NDhcp4CConnection;
@ -198,6 +199,8 @@ struct NDhcp4Outgoing {
struct {
uint8_t type;
uint8_t message_type;
uint32_t client_addr;
uint64_t start_time;
uint64_t base_time;
uint64_t send_time;
@ -239,6 +242,11 @@ struct NDhcp4ClientConfig {
size_t n_broadcast_mac;
uint8_t *client_id;
size_t n_client_id;
struct {
int level;
NDhcp4LogFunc func;
void *data;
} log;
};
#define N_DHCP4_CLIENT_CONFIG_NULL(_x) { \
@ -686,3 +694,19 @@ static inline uint64_t n_dhcp4_gettime(clockid_t clock) {
return ts.tv_sec * 1000ULL * 1000ULL * 1000ULL + ts.tv_nsec;
}
#define n_dhcp4_c_log(_config, _level, ...) \
do { \
const NDhcp4ClientConfig *__config = _config; \
\
if (_level <= __config->log.level && __config->log.func) { \
if (1) { \
_config->log.func(_level, \
__config->log.data, \
__VA_ARGS__); \
} else { \
/* To have the compiler check arguments */ \
printf(__VA_ARGS__); \
} \
} \
} while (0)

View file

@ -29,6 +29,8 @@ typedef struct NDhcp4ServerEvent NDhcp4ServerEvent;
typedef struct NDhcp4ServerIp NDhcp4ServerIp;
typedef struct NDhcp4ServerLease NDhcp4ServerLease;
typedef void (*NDhcp4LogFunc)(int level, void *data, const char *fmt, ...);
#define N_DHCP4_CLIENT_START_DELAY_RFC2131 (UINT64_C(9000))
enum {
@ -111,6 +113,8 @@ void n_dhcp4_client_config_set_request_broadcast(NDhcp4ClientConfig *config, boo
void n_dhcp4_client_config_set_mac(NDhcp4ClientConfig *config, const uint8_t *mac, size_t n_mac);
void n_dhcp4_client_config_set_broadcast_mac(NDhcp4ClientConfig *config, const uint8_t *mac, size_t n_mac);
int n_dhcp4_client_config_set_client_id(NDhcp4ClientConfig *config, const uint8_t *id, size_t n_id);
void n_dhcp4_client_config_set_log_level(NDhcp4ClientConfig *config, int level);
void n_dhcp4_client_config_set_log_func(NDhcp4ClientConfig *config, NDhcp4LogFunc func, void *data);
/* client-probe configs */

View file

@ -112,6 +112,19 @@ nm_log_level_from_syslog (int syslog_level)
}
}
static inline int
nm_log_level_to_syslog (NMLogLevel nm_level)
{
switch (nm_level) {
case LOGL_ERR: return 3; /* LOG_ERR */
case LOGL_WARN: return 4; /* LOG_WARN */
case LOGL_INFO: return 5; /* LOG_NOTICE */
case LOGL_DEBUG: return 6; /* LOG_INFO */
case LOGL_TRACE: return 7; /* LOG_DEBUG */
default: return 0; /* LOG_EMERG */
}
}
/*****************************************************************************/
struct timespec;

View file

@ -1053,6 +1053,26 @@ dhcp4_event_cb (GIOChannel *source,
return G_SOURCE_CONTINUE;
}
static void
nettools_log (int level, void *data, const char *fmt, ...)
{
NMDhcpNettools *self = data;
NMLogLevel nm_level;
gs_free char *msg = NULL;
va_list ap;
nm_level = nm_log_level_from_syslog (level);
if (nm_logging_enabled (nm_level, LOGD_DHCP4)) {
va_start (ap, fmt);
msg = g_strdup_vprintf (fmt, ap);
va_end (ap);
nm_log (nm_level, LOGD_DHCP4, NULL , NULL,
"dhcp4 (%s): %s",
nm_dhcp_client_get_iface (NM_DHCP_CLIENT (self)),
msg);
}
}
static gboolean
nettools_create (NMDhcpNettools *self,
const char *dhcp_anycast_addr,
@ -1122,6 +1142,8 @@ nettools_create (NMDhcpNettools *self,
return FALSE;
}
n_dhcp4_client_config_set_log_level (config, nm_log_level_to_syslog (nm_logging_get_level (LOGD_DHCP4)));
n_dhcp4_client_config_set_log_func (config, nettools_log, self);
n_dhcp4_client_config_set_ifindex (config, nm_dhcp_client_get_ifindex (NM_DHCP_CLIENT (self)));
n_dhcp4_client_config_set_transport (config, transport);
n_dhcp4_client_config_set_mac (config, hwaddr_arr, hwaddr_len);