Skip to content

Commit 80f9f45

Browse files
committed
Refactor hostname handling and headers
Replace local ToHex helper with shared common::hex::ToCharUppercase and add required includes (cctype, cstdint, core/netif, common/utils/utils_hex). Sanitize SetHostname to copy only printable characters and zero-pad the remainder; add debug entry/exit/puts around BuildDefaultHostname. Update network JSON includes to use network_config.h and network_iface.h instead of network.h. Minor cleanup in rtl8201f phy comment.
1 parent d0bf8ff commit 80f9f45

3 files changed

Lines changed: 44 additions & 17 deletions

File tree

lib-network/src/emac/phy/rtl8201f/phy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void CustomizedLed()
147147
#if defined(GD32F4XX)
148148
#define RMSR_RX_TIMING_VAL 0x4
149149
#if defined(GD32F407)
150-
#define RMSR_TX_TIMING_VAL 0x2 // The GD32F407 is now running at 200MHz
150+
#define RMSR_TX_TIMING_VAL 0x2
151151
#elif defined(GD32F470)
152152
#define RMSR_TX_TIMING_VAL 0x1
153153
#else

lib-network/src/iface/iface.cpp

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@
2323
* THE SOFTWARE.
2424
*/
2525

26+
#include <cctype>
27+
#include <cstdint>
2628
#include <cstring>
2729
#include <cassert>
2830

29-
#include "network.h"
31+
#include "core/netif.h"
3032
#include "core/ip4/dhcp.h"
3133
#include "core/ip4/autoip.h"
3234
#include "net_config.h"
3335
#include "network_iface.h"
36+
#include "common/utils/utils_hex.h"
3437
#if !defined(CONFIG_NET_APPS_NO_MDNS)
3538
#include "apps/mdns.h"
3639
#endif
@@ -44,14 +47,9 @@ static char s_hostname[kHostnameSize];
4447
static char s_domain_name[kDomainnameSize];
4548
static uint32_t s_nameservers[kNameserversCount];
4649

47-
static constexpr char ToHex(char i)
48-
{
49-
return static_cast<char>(((i) < 10) ? '0' + i : 'A' + (i - 10));
50-
}
51-
5250
void CopyMacAddressTo(uint8_t* mac_address)
5351
{
54-
assert(mac_address != nullptr);
52+
assert(mac_address != nullptr);
5553
memcpy(mac_address, netif::HwAddr(), kMacSize);
5654
}
5755

@@ -74,6 +72,8 @@ const char* DomainName()
7472

7573
static void BuildDefaultHostname()
7674
{
75+
DEBUG_ENTRY();
76+
7777
uint32_t k = 0;
7878

7979
static constexpr uint32_t kSuffixLen = 6; // 3 bytes -> 6 hex chars
@@ -86,16 +86,19 @@ static void BuildDefaultHostname()
8686
s_hostname[k++] = HOST_NAME_PREFIX[i];
8787
}
8888

89-
const auto& hw = netif::global::netif_default.hwaddr; // expects at least 6 bytes
89+
const auto* hw = netif::global::netif_default.hwaddr; // expects at least 6 bytes
9090

91-
s_hostname[k++] = ToHex(hw[3] >> 4);
92-
s_hostname[k++] = ToHex(hw[3]);
93-
s_hostname[k++] = ToHex(hw[4] >> 4);
94-
s_hostname[k++] = ToHex(hw[4]);
95-
s_hostname[k++] = ToHex(hw[5] >> 4);
96-
s_hostname[k++] = ToHex(hw[5]);
91+
s_hostname[k++] = common::hex::ToCharUppercase(hw[3] >> 4);
92+
s_hostname[k++] = common::hex::ToCharUppercase(hw[3] & 0xF);
93+
s_hostname[k++] = common::hex::ToCharUppercase(hw[4] >> 4);
94+
s_hostname[k++] = common::hex::ToCharUppercase(hw[4] & 0xF);
95+
s_hostname[k++] = common::hex::ToCharUppercase(hw[5] >> 4);
96+
s_hostname[k++] = common::hex::ToCharUppercase(hw[5] & 0xF);
9797

9898
s_hostname[k] = '\0';
99+
100+
DEBUG_PUTS(s_hostname);
101+
DEBUG_EXIT();
99102
}
100103

101104
void SetHostnameAuto()
@@ -117,7 +120,30 @@ void SetHostname(const char* hostname)
117120
}
118121
else
119122
{
120-
strncpy(s_hostname, hostname, kHostnameSize - 1);
123+
auto n = kHostnameSize - 1;
124+
auto* dst = s_hostname;
125+
const auto* src = hostname;
126+
127+
while (n > 0 && *src != '\0')
128+
{
129+
const int kC = *src;
130+
if (isprint(kC))
131+
{
132+
*dst++ = *src++;
133+
--n;
134+
}
135+
else
136+
{
137+
src++;
138+
}
139+
}
140+
141+
while (n > 0)
142+
{
143+
*dst++ = '\0';
144+
--n;
145+
}
146+
121147
s_hostname[kHostnameSize - 1] = '\0';
122148
}
123149

lib-network/src/json/networkparams.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333
#include <netinet/in.h>
3434
#include <arpa/inet.h>
3535

36+
#include "network_config.h"
37+
#include "network_iface.h"
3638
#include "ip4/ip4_address.h"
37-
#include "network.h"
3839
#include "configstore.h"
3940
#include "json/networkparams.h"
4041
#include "json/networkparamsconst.h"

0 commit comments

Comments
 (0)