From 440f541672e8d1e45172d9e586cd31b74c2a5841 Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Wed, 13 Nov 2019 10:57:20 +0100 Subject: [PATCH] n-dhcp4: log incoming packets Add log messages for incoming packets. https://github.com/nettools/n-dhcp4/pull/8 --- shared/n-dhcp4/src/n-dhcp4-c-connection.c | 43 +++++++++++++++++++++++ shared/n-dhcp4/src/n-dhcp4-private.h | 1 + 2 files changed, 44 insertions(+) diff --git a/shared/n-dhcp4/src/n-dhcp4-c-connection.c b/shared/n-dhcp4/src/n-dhcp4-c-connection.c index 5c50dacfd8..8e3bbf58f0 100644 --- a/shared/n-dhcp4/src/n-dhcp4-c-connection.c +++ b/shared/n-dhcp4/src/n-dhcp4-c-connection.c @@ -955,6 +955,31 @@ 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) { @@ -1066,6 +1091,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 +1145,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: diff --git a/shared/n-dhcp4/src/n-dhcp4-private.h b/shared/n-dhcp4/src/n-dhcp4-private.h index a42a12e4eb..f5f00bce2d 100644 --- a/shared/n-dhcp4/src/n-dhcp4-private.h +++ b/shared/n-dhcp4/src/n-dhcp4-private.h @@ -11,6 +11,7 @@ #include #include #include +#include #include "n-dhcp4.h" typedef struct NDhcp4CConnection NDhcp4CConnection;