From 108d87e8cd9c97cbbeafd43a4144a244d6abb7e4 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Tue, 2 Oct 2018 03:07:29 +0200 Subject: [PATCH] chg: Use static memory buffer for MAC address conversion 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. --- ddhcp.c | 18 +++--------------- dhcp.c | 6 +----- hook.c | 6 +----- tools.c | 25 ++++++++++++++++++------- 4 files changed, 23 insertions(+), 32 deletions(-) diff --git a/ddhcp.c b/ddhcp.c index 53581ee..813f86a 100644 --- a/ddhcp.c +++ b/ddhcp.c @@ -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); @@ -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); @@ -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); diff --git a/dhcp.c b/dhcp.c index cee9dc8..cc580c8 100644 --- a/dhcp.c +++ b/dhcp.c @@ -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); diff --git a/hook.c b/hook.c index 617be84..8062197 100644 --- a/hook.c +++ b/hook.c @@ -7,11 +7,7 @@ #include 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"); diff --git a/tools.c b/tools.c index 1995535..fcded12 100644 --- a/tools.c +++ b/tools.c @@ -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;