n-dhcp4: log incoming packets

Add log messages for incoming packets.

https://github.com/nettools/n-dhcp4/pull/8
This commit is contained in:
Beniamino Galvani 2019-11-13 10:57:20 +01:00
parent 87a26ea594
commit 440f541672
2 changed files with 44 additions and 0 deletions

View file

@ -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:

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;