10000 extmod/modnetwork: Increase max hostname length to 32. · micropython/micropython@b329fdc · GitHub
[go: up one dir, main page]

Skip to content

Commit b329fdc

Browse files
jimmodpgeorge
authored andcommitted
extmod/modnetwork: Increase max hostname length to 32.
This changes from the previous limit of 15 characters. Although DHCP and mDNS allow for up to 63, ESP32 and ESP8266 only allow 32, so this seems like a reasonable limit to enforce across all ports (and avoids wasting the additional memory). Also clarifies that `MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN` does not include the null terminator (which was unclear before). This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
1 parent d6c55a4 commit b329fdc

File tree

6 files changed

+15
-8
lines changed

6 files changed

+15
-8
lines changed

docs/library/network.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ The following are functions available in the network module.
188188
during connection. For this reason, you must set the hostname before
189189
activating/connecting your network interfaces.
190190

191+
The length of the hostname is limited to 32 characters.
192+
:term:`MicroPython ports <MicroPython port>` may choose to set a lower
193+
limit for memory reasons. If the given name does not fit, a `ValueError`
194+
is raised.
195+
191196
The default hostname is typically the name of the board.
192197

193198
.. function:: phy_mode([mode])

extmod/modnetwork.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#if MICROPY_PY_NETWORK
3636

3737
#include "shared/netutils/netutils.h"
38-
#include "modnetwork.h"
38+
#include "extmod/modnetwork.h"
3939

4040
#if MICROPY_PY_NETWORK_CYW43
4141
// So that CYW43_LINK_xxx constants are available to MICROPY_PORT_NETWORK_INTERFACES.
@@ -56,7 +56,7 @@ char mod_network_country_code[2] = "XX";
5656
#error "MICROPY_PY_NETWORK_HOSTNAME_DEFAULT must be set in mpconfigport.h or mpconfigboard.h"
5757
#endif
5858

59-
char mod_network_hostname[MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN] = MICROPY_PY_NETWORK_HOSTNAME_DEFAULT;
59+
char mod_network_hostname[MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN + 1] = MICROPY_PY_NETWORK_HOSTNAME_DEFAULT;
6060

6161
#ifdef MICROPY_PORT_NETWORK_INTERFACES
6262

@@ -122,7 +122,7 @@ STATIC mp_obj_t network_hostname(size_t n_args, const mp_obj_t *args) {
122122
} else {
123123
size_t len;
124124
const char *str = mp_obj_str_get_data(args[0], &len);
125-
if (len >= MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
125+
if (len > MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
126126
mp_raise_ValueError(NULL);
127127
}
128128
strcpy(mod_network_hostname, str);

extmod/modnetwork.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@
5656
extern char mod_network_country_code[2];
5757

5858
#ifndef MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN
59-
#define MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN (16)
59+
// Doesn't include the null terminator.
60+
#define MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN (32)
6061
#endif
6162

62-
extern char mod_network_hostname 8000 [MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN];
63+
// This is a null-terminated string.
64+
extern char mod_network_hostname[MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN + 1];
6365

6466
#if MICROPY_PY_LWIP
6567
struct netif;

extmod/network_cyw43.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ STATIC mp_obj_t network_cyw43_config(size_t n_args, const mp_obj_t *args, mp_map
500500
// TODO: Deprecated. Use network.hostname(name) instead.
501501
size_t len;
502502
const char *str = mp_obj_str_get_data(e->value, &len);
503-
if (len >= MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
503+
if (len > MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
504504
mp_raise_ValueError(NULL);
505505
}
506506
strcpy(mod_network_hostname, str);

ports/esp32/network_wlan.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_
524524
// TODO: Deprecated. Use network.hostname(name) instead.
525525
size_t len;
526526
const char *str = mp_obj_str_get_data(kwargs->table[i].value, &len);
527-
if (len >= MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
527+
if (len > MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
528528
mp_raise_ValueError(NULL);
529529
}
530530
strcpy(mod_network_hostname, str);

ports/esp8266/network_wlan.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
404404
// TODO: Deprecated. Use network.hostname(name) instead.
405405
size_t len;
406406
const char *str = mp_obj_str_get_data(kwargs->table[i].value, &len);
407-
if (len >= MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
407+
if (len > MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
408408
mp_raise_ValueError(NULL);
409409
}
410410
strcpy(mod_network_hostname, str);

0 commit comments

Comments
 (0)
0