Skip to content

Commit

Permalink
chg: Use static memory buffer for MAC address conversion
Browse files Browse the repository at this point in the history
This patch uses several buffers in a round-robin fashion to avoid overwriting a just-returned result.
The number of buffers internally used is choosen at 16 to allow for enough spare buffers even
when more complicated situations need to be processed.
  • Loading branch information
BenBE committed Feb 25, 2019
1 parent d058b97 commit 108d87e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 32 deletions.
18 changes: 3 additions & 15 deletions ddhcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,7 @@ void ddhcp_dhcp_process(uint8_t* buffer, ssize_t len, struct sockaddr_in6 sender
void ddhcp_dhcp_renewlease(struct ddhcp_mcast_packet* packet, ddhcp_config* config) {
DEBUG("ddhcp_dhcp_renewlease(request,config)\n");

#if LOG_LEVEL_LIMIT >= LOG_DEBUG
char* hwaddr = hwaddr2c(packet->renew_payload->chaddr);
DEBUG("ddhcp_dhcp_renewlease(...): Request for xid: %u chaddr: %s\n", packet->renew_payload->xid, hwaddr);
free(hwaddr);
#endif
DEBUG("ddhcp_dhcp_renewlease(...): Request for xid: %u chaddr: %s\n", packet->renew_payload->xid, hwaddr2c(packet->renew_payload->chaddr));

int ret = dhcp_rhdl_request(&(packet->renew_payload->address), config);

Expand Down Expand Up @@ -232,11 +228,7 @@ void ddhcp_dhcp_leaseack(struct ddhcp_mcast_packet* request, ddhcp_config* confi
// Stub functions
DEBUG("ddhcp_dhcp_leaseack(request,config)\n");

#if LOG_LEVEL_LIMIT >= LOG_DEBUG
char* hwaddr = hwaddr2c(request->renew_payload->chaddr);
DEBUG("ddhcp_dhcp_leaseack(...): ACK for xid: %u chaddr: %s\n", request->renew_payload->xid, hwaddr);
free(hwaddr);
#endif
DEBUG("ddhcp_dhcp_leaseack(...): ACK for xid: %u chaddr: %s\n", request->renew_payload->xid, hwaddr2c(request->renew_payload->chaddr));

dhcp_packet* packet = dhcp_packet_list_find(&config->dhcp_packet_cache, request->renew_payload->xid, request->renew_payload->chaddr);

Expand All @@ -257,11 +249,7 @@ void ddhcp_dhcp_leasenak(struct ddhcp_mcast_packet* request, ddhcp_config* confi
// Stub functions
DEBUG("ddhcp_dhcp_leasenak(request,config)\n");

#if LOG_LEVEL_LIMIT >= LOG_DEBUG
char* hwaddr = hwaddr2c(request->renew_payload->chaddr);
DEBUG("ddhcp_dhcp_leaseack(...): NAK for xid: %u chaddr: %s\n", request->renew_payload->xid, hwaddr);
free(hwaddr);
#endif
DEBUG("ddhcp_dhcp_leaseack(...): NAK for xid: %u chaddr: %s\n", request->renew_payload->xid, hwaddr2c(request->renew_payload->chaddr));

dhcp_packet* packet = dhcp_packet_list_find(&config->dhcp_packet_cache, request->renew_payload->xid, request->renew_payload->chaddr);

Expand Down
6 changes: 1 addition & 5 deletions dhcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,7 @@ int dhcp_hdl_request(int socket, struct dhcp_packet* request, ddhcp_config* conf
payload.xid = request->xid;
payload.lease_seconds = 0;

#if LOG_LEVEL_LIMIT >= LOG_DEBUG
char* hwaddr = hwaddr2c(payload.chaddr);
DEBUG("dhcp_hdl_request(...): Save request for xid: %u chaddr: %s\n", payload.xid, hwaddr);
free(hwaddr);
#endif
DEBUG("dhcp_hdl_request(...): Save request for xid: %u chaddr: %s\n", payload.xid, hwaddr2c(payload.chaddr));

// Send packet
ddhcp_mcast_packet* packet = new_ddhcp_packet(DDHCP_MSG_RENEWLEASE, config);
Expand Down
6 changes: 1 addition & 5 deletions hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
#include <sys/wait.h>

void hook(uint8_t type, struct in_addr* address, uint8_t* chaddr, ddhcp_config* config) {
#if LOG_LEVEL_LIMIT >= LOG_DEBUG
char* hwaddr = hwaddr2c(chaddr);
DEBUG("hook(type:%i,addr:%s,chaddr:%s,config)\n", type, inet_ntoa(*address), hwaddr);
free(hwaddr);
#endif
DEBUG("hook(type:%i,addr:%s,chaddr:%s,config)\n", type, inet_ntoa(*address), hwaddr2c(chaddr));

if (!config->hook_command) {
DEBUG("hook(...): No hook command set\n");
Expand Down
25 changes: 18 additions & 7 deletions tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,26 @@ dhcp_option* parse_option() {
return option;
}

//! Number of pre-allocated string buffers
#define hwaddr_strcount 2u
//! Maximum string length required to format an MAC address
#define hwaddr_strlen 18u
static char hwaddr_strbuf[hwaddr_strcount][hwaddr_strlen] = { 0 };
static uint8_t hwaddr_stridx = 0;

//! The function hwaddr2c formats a hwaddr (AKA MAC address) passed in the first argument
//! into one (of several) static buffers. The buffer is chosen in a round-robin-like fashion
//! by cycling through a list of hwaddr_strcount buffers, each of hwaddr_strlen bytes in size.
//! If the last of the pre-allocated buffers has been reached filling up new buffers restarts
//! with the first on.
//!
//! \note The memory returned SHOULD NOT be referenced by any long-living pointers as it
//! can be overwritten at any time when the next hwaddr is converted by calling this function.
char* hwaddr2c(uint8_t* hwaddr) {
char* str = calloc(18, sizeof(char));
char* str = hwaddr_strbuf[hwaddr_stridx];
hwaddr_stridx = (hwaddr_stridx + 1u) % hwaddr_strcount;

if (!str) {
FATAL("hwaddr2c(...): Failed to allocate buffer.\n");
return NULL;
}

snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
snprintf(str, hwaddr_strlen, "%02X:%02X:%02X:%02X:%02X:%02X",
hwaddr[0], hwaddr[1], hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);

return str;
Expand Down

0 comments on commit 108d87e

Please sign in to comment.