Skip to content

Commit 2ef1dde

Browse files
nealjackkartben
authored andcommitted
mgmt: hawkbit: interface support for ip addresses and domain name
Previously, hawkbit interface only supported a url/hostname and a port, and internally it resolves to an IP address. This does not work for network layers that rely on NAT64, like OpenThread. Zephyr's implementation of `getaddrinfo` is not aware of NAT64. DNS will resolve an IPV4 address that needs to be converted to IPV6 with the NAT64 prefix. This commit alters the Hawkbit interface to allow providing an explicit domain name as a string via `server_domain`, and an already resolved IP address as `server_addr`. This commit changes the usage of `hawkbit_runtime_config.server_addr` to point to either an IP address or domain name. It adds a new Kconfig (`HAWKBIT_USE_DOMAIN_NAME`) to specify an explicit domain name and adds a new variable `hawkbit_runtime_config.server_domain`. If `HAWKBIT_USE_DOMAIN_NAME` is enabled and a user provides an IP address to `server_addr`, the user must provide a domain name to `server_domain`. Signed-off-by: Neal Jackson <[email protected]>
1 parent d718b46 commit 2ef1dde

File tree

3 files changed

+110
-9
lines changed

3 files changed

+110
-9
lines changed

include/zephyr/mgmt/hawkbit/config.h

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@
2929
* settings.
3030
*/
3131
struct hawkbit_runtime_config {
32-
/** Server address */
32+
/**
33+
* Server address (domain name or IP address if
34+
* CONFIG_HAWKBIT_USE_DOMAIN_NAME is enabled)
35+
*/
3336
char *server_addr;
37+
/** Server domain name */
38+
char *server_domain;
3439
/** Server port */
3540
uint16_t server_port;
3641
/** Security token */
@@ -56,6 +61,27 @@ int hawkbit_set_config(struct hawkbit_runtime_config *config);
5661
*/
5762
struct hawkbit_runtime_config hawkbit_get_config(void);
5863

64+
/**
65+
* @brief Set the hawkBit server hostname.
66+
*
67+
* @param domain_str Server hostname to set.
68+
* @retval 0 on success.
69+
* @retval -EINVAL if string length mismatch for server_domain
70+
* @retval -EAGAIN if probe is currently running.
71+
*/
72+
static inline int hawkbit_set_server_domain(char *domain_str)
73+
{
74+
struct hawkbit_runtime_config set_config = {
75+
.server_addr = NULL,
76+
.server_domain = domain_str,
77+
.server_port = 0,
78+
.auth_token = NULL,
79+
.tls_tag = 0,
80+
};
81+
82+
return hawkbit_set_config(&set_config);
83+
}
84+
5985
/**
6086
* @brief Set the hawkBit server address.
6187
*
@@ -68,6 +94,7 @@ static inline int hawkbit_set_server_addr(char *addr_str)
6894
{
6995
struct hawkbit_runtime_config set_config = {
7096
.server_addr = addr_str,
97+
.server_domain = NULL,
7198
.server_port = 0,
7299
.auth_token = NULL,
73100
.tls_tag = 0,
@@ -87,6 +114,7 @@ static inline int hawkbit_set_server_port(uint16_t port)
87114
{
88115
struct hawkbit_runtime_config set_config = {
89116
.server_addr = NULL,
117+
.server_domain = NULL,
90118
.server_port = port,
91119
.auth_token = NULL,
92120
.tls_tag = 0,
@@ -106,6 +134,7 @@ static inline int hawkbit_set_ddi_security_token(char *token)
106134
{
107135
struct hawkbit_runtime_config set_config = {
108136
.server_addr = NULL,
137+
.server_domain = NULL,
109138
.server_port = 0,
110139
.auth_token = token,
111140
.tls_tag = 0,
@@ -125,6 +154,7 @@ static inline int hawkbit_set_tls_tag(sec_tag_t tag)
125154
{
126155
struct hawkbit_runtime_config set_config = {
127156
.server_addr = NULL,
157+
.server_domain = NULL,
128158
.server_port = 0,
129159
.auth_token = NULL,
130160
.tls_tag = tag,
@@ -143,6 +173,16 @@ static inline char *hawkbit_get_server_addr(void)
143173
return hawkbit_get_config().server_addr;
144174
}
145175

176+
/**
177+
* @brief Get the hawkBit server hostname.
178+
*
179+
* @return Server hostname.
180+
*/
181+
static inline char *hawkbit_get_server_domain(void)
182+
{
183+
return hawkbit_get_config().server_domain;
184+
}
185+
146186
/**
147187
* @brief Get the hawkBit server port.
148188
*

subsys/mgmt/hawkbit/Kconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ config HAWKBIT_SET_SETTINGS_RUNTIME
7070
help
7171
Enable to set hawkbit settings at runtime.
7272

73+
config HAWKBIT_USE_DOMAIN_NAME
74+
bool "Use server_domain for domain name instead of server_addr"
75+
depends on HAWKBIT_SET_SETTINGS_RUNTIME
76+
help
77+
Enable to use the server_domain field for TLS and HTTP. If enabled,
78+
server_addr can accept an already resolved IP address, and the domain name
79+
can be provided via server_domain.
80+
81+
config HAWKBIT_DOMAIN_NAME_MAX_LEN
82+
int "The buffer size for storing the domain name string"
83+
default DNS_RESOLVER_MAX_QUERY_LEN if DNS_RESOLVER
84+
default 255
85+
depends on HAWKBIT_USE_DOMAIN_NAME
86+
help
87+
The size for the internal buffer used to hold the domain name string.
88+
7389
choice HAWKBIT_DDI_SECURITY
7490
prompt "hawkBit DDI API authentication modes"
7591
default HAWKBIT_DDI_NO_SECURITY

subsys/mgmt/hawkbit/hawkbit.c

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ static struct hawkbit_config {
8686
int32_t action_id;
8787
#ifdef CONFIG_HAWKBIT_SET_SETTINGS_RUNTIME
8888
char server_addr[SERVER_ADDR_LEN + 1];
89+
#ifdef CONFIG_HAWKBIT_USE_DOMAIN_NAME
90+
char server_domain[CONFIG_HAWKBIT_DOMAIN_NAME_MAX_LEN + 1];
91+
#endif
8992
char server_port[sizeof(STRINGIFY(__UINT16_MAX__))];
9093
#ifndef CONFIG_HAWKBIT_DDI_NO_SECURITY
9194
char ddi_security_token[DDI_SECURITY_TOKEN_SIZE + 1];
@@ -97,11 +100,17 @@ static struct hawkbit_config {
97100
} hb_cfg;
98101

99102
#ifdef CONFIG_HAWKBIT_SET_SETTINGS_RUNTIME
100-
#define HAWKBIT_SERVER hb_cfg.server_addr
103+
#ifdef CONFIG_HAWKBIT_USE_DOMAIN_NAME
104+
#define HAWKBIT_SERVER_DOMAIN hb_cfg.server_domain
105+
#else
106+
#define HAWKBIT_SERVER_DOMAIN hb_cfg.server_addr
107+
#endif /* CONFIG_HAWKBIT_USE_DOMAIN_NAME */
108+
#define HAWKBIT_SERVER_ADDR hb_cfg.server_addr
101109
#define HAWKBIT_PORT hb_cfg.server_port
102110
#define HAWKBIT_PORT_INT atoi(hb_cfg.server_port)
103111
#else
104-
#define HAWKBIT_SERVER CONFIG_HAWKBIT_SERVER
112+
#define HAWKBIT_SERVER_ADDR CONFIG_HAWKBIT_SERVER
113+
#define HAWKBIT_SERVER_DOMAIN CONFIG_HAWKBIT_SERVER
105114
#define HAWKBIT_PORT STRINGIFY(CONFIG_HAWKBIT_PORT)
106115
#define HAWKBIT_PORT_INT CONFIG_HAWKBIT_PORT
107116
#endif /* CONFIG_HAWKBIT_SET_SETTINGS_RUNTIME */
@@ -306,6 +315,22 @@ static int hawkbit_settings_set(const char *name, size_t len, settings_read_cb r
306315
return rc;
307316
}
308317

318+
#ifdef CONFIG_HAWKBIT_USE_DOMAIN_NAME
319+
if (settings_name_steq(name, "server_domain", &next) && !next) {
320+
if (len != sizeof(hb_cfg.server_domain)) {
321+
return -EINVAL;
322+
}
323+
324+
rc = read_cb(cb_arg, &hb_cfg.server_domain, sizeof(hb_cfg.server_domain));
325+
LOG_DBG("<%s> = %s", "hawkbit/server_domain", hb_cfg.server_domain);
326+
if (rc >= 0) {
327+
return 0;
328+
}
329+
330+
return rc;
331+
}
332+
#endif /* CONFIG_HAWKBIT_USE_DOMAIN_NAME */
333+
309334
if (settings_name_steq(name, "server_port", &next) && !next) {
310335
if (len != sizeof(uint16_t)) {
311336
return -EINVAL;
@@ -344,6 +369,9 @@ static int hawkbit_settings_set(const char *name, size_t len, settings_read_cb r
344369
}
345370
#else /* CONFIG_HAWKBIT_SET_SETTINGS_RUNTIME */
346371
if (settings_name_steq(name, "server_addr", NULL) ||
372+
#ifdef CONFIG_HAWKBIT_USE_DOMAIN_NAME
373+
settings_name_steq(name, "server_domain", NULL) ||
374+
#endif /* CONFIG_HAWKBIT_USE_DOMAIN_NAME */
347375
settings_name_steq(name, "server_port", NULL) ||
348376
settings_name_steq(name, "ddi_token", NULL)) {
349377
rc = read_cb(cb_arg, NULL, 0);
@@ -367,6 +395,9 @@ static int hawkbit_settings_export(int (*cb)(const char *name, const void *value
367395
(void)cb("hawkbit/action_id", &hb_cfg.action_id, sizeof(hb_cfg.action_id));
368396
#ifdef CONFIG_HAWKBIT_SET_SETTINGS_RUNTIME
369397
(void)cb("hawkbit/server_addr", &hb_cfg.server_addr, strlen(hb_cfg.server_addr) + 1);
398+
#ifdef CONFIG_HAWKBIT_USE_DOMAIN_NAME
399+
(void)cb("hawkbit/server_domain", &hb_cfg.server_domain, sizeof(hb_cfg.server_domain));
400+
#endif /* CONFIG_HAWKBIT_USE_DOMAIN_NAME */
370401
uint16_t hawkbit_port = atoi(hb_cfg.server_port);
371402
(void)cb("hawkbit/server_port", &hawkbit_port, sizeof(hawkbit_port));
372403
#ifndef CONFIG_HAWKBIT_DDI_NO_SECURITY
@@ -447,7 +478,7 @@ static bool start_http_client(int *hb_sock)
447478
}
448479

449480
while (resolve_attempts--) {
450-
ret = zsock_getaddrinfo(HAWKBIT_SERVER, HAWKBIT_PORT, &hints, &addr);
481+
ret = zsock_getaddrinfo(HAWKBIT_SERVER_ADDR, HAWKBIT_PORT, &hints, &addr);
451482
if (ret == 0) {
452483
break;
453484
}
@@ -477,8 +508,8 @@ static bool start_http_client(int *hb_sock)
477508
goto err_sock;
478509
}
479510

480-
if (zsock_setsockopt(*hb_sock, SOL_TLS, TLS_HOSTNAME, HAWKBIT_SERVER,
481-
sizeof(CONFIG_HAWKBIT_SERVER)) < 0) {
511+
if (zsock_setsockopt(*hb_sock, SOL_TLS, TLS_HOSTNAME, HAWKBIT_SERVER_DOMAIN,
512+
sizeof(HAWKBIT_SERVER_DOMAIN)) < 0) {
482513
goto err_sock;
483514
}
484515
#endif /* CONFIG_HAWKBIT_USE_TLS */
@@ -798,6 +829,20 @@ int hawkbit_set_config(struct hawkbit_runtime_config *config)
798829
sizeof(hb_cfg.server_addr));
799830
LOG_DBG("configured %s: %s", "hawkbit/server_addr", hb_cfg.server_addr);
800831
}
832+
#ifdef CONFIG_HAWKBIT_USE_DOMAIN_NAME
833+
if (config->server_domain != NULL) {
834+
if (strnlen(config->server_domain, CONFIG_HAWKBIT_DOMAIN_NAME_MAX_LEN + 1)
835+
> CONFIG_HAWKBIT_DOMAIN_NAME_MAX_LEN) {
836+
LOG_ERR("%s too long: %s", "hawkbit/server_domain",
837+
config->server_domain);
838+
return -EINVAL;
839+
}
840+
strncpy(hb_cfg.server_domain, config->server_domain,
841+
sizeof(hb_cfg.server_domain));
842+
LOG_DBG("configured %s: %s", "hawkbit/server_domain",
843+
hb_cfg.server_domain);
844+
}
845+
#endif /* CONFIG_HAWKBIT_USE_DOMAIN_NAME */
801846
if (config->server_port != 0) {
802847
snprintf(hb_cfg.server_port, sizeof(hb_cfg.server_port), "%u",
803848
config->server_port);
@@ -831,7 +876,7 @@ int hawkbit_set_config(struct hawkbit_runtime_config *config)
831876
struct hawkbit_runtime_config hawkbit_get_config(void)
832877
{
833878
struct hawkbit_runtime_config config = {
834-
.server_addr = HAWKBIT_SERVER,
879+
.server_addr = HAWKBIT_SERVER_ADDR,
835880
.server_port = HAWKBIT_PORT_INT,
836881
.auth_token = HAWKBIT_DDI_SECURITY_TOKEN,
837882
.tls_tag = HAWKBIT_CERT_TAG,
@@ -1059,7 +1104,7 @@ static bool send_request(struct hawkbit_context *hb_context, enum hawkbit_http_r
10591104
#endif /* CONFIG_HAWKBIT_DDI_NO_SECURITY */
10601105

10611106
http_req.url = url_buffer;
1062-
http_req.host = HAWKBIT_SERVER;
1107+
http_req.host = HAWKBIT_SERVER_DOMAIN;
10631108
http_req.port = HAWKBIT_PORT;
10641109
http_req.protocol = "HTTP/1.1";
10651110
http_req.response = response_cb;
@@ -1173,7 +1218,7 @@ void hawkbit_reboot(void)
11731218

11741219
static bool check_hawkbit_server(void)
11751220
{
1176-
if (strlen(HAWKBIT_SERVER) == 0) {
1221+
if (strlen(HAWKBIT_SERVER_ADDR) == 0) {
11771222
if (sizeof(CONFIG_HAWKBIT_SERVER) > 1) {
11781223
hawkbit_set_server_addr(CONFIG_HAWKBIT_SERVER);
11791224
} else {

0 commit comments

Comments
 (0)