summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/library/network.rst5
-rw-r--r--extmod/modnetwork.c6
-rw-r--r--extmod/modnetwork.h6
-rw-r--r--extmod/network_cyw43.c2
-rw-r--r--ports/esp32/network_wlan.c2
-rw-r--r--ports/esp8266/network_wlan.c2
6 files changed, 15 insertions, 8 deletions
diff --git a/docs/library/network.rst b/docs/library/network.rst
index a14d6192e..cc5088429 100644
--- a/docs/library/network.rst
+++ b/docs/library/network.rst
@@ -188,6 +188,11 @@ The following are functions available in the network module.
during connection. For this reason, you must set the hostname before
activating/connecting your network interfaces.
+ The length of the hostname is limited to 32 characters.
+ :term:`MicroPython ports <MicroPython port>` may choose to set a lower
+ limit for memory reasons. If the given name does not fit, a `ValueError`
+ is raised.
+
The default hostname is typically the name of the board.
.. function:: phy_mode([mode])
diff --git a/extmod/modnetwork.c b/extmod/modnetwork.c
index 7c1b91de4..378b45b9c 100644
--- a/extmod/modnetwork.c
+++ b/extmod/modnetwork.c
@@ -35,7 +35,7 @@
#if MICROPY_PY_NETWORK
#include "shared/netutils/netutils.h"
-#include "modnetwork.h"
+#include "extmod/modnetwork.h"
#if MICROPY_PY_NETWORK_CYW43
// So that CYW43_LINK_xxx constants are available to MICROPY_PORT_NETWORK_INTERFACES.
@@ -56,7 +56,7 @@ char mod_network_country_code[2] = "XX";
#error "MICROPY_PY_NETWORK_HOSTNAME_DEFAULT must be set in mpconfigport.h or mpconfigboard.h"
#endif
-char mod_network_hostname[MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN] = MICROPY_PY_NETWORK_HOSTNAME_DEFAULT;
+char mod_network_hostname[MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN + 1] = MICROPY_PY_NETWORK_HOSTNAME_DEFAULT;
#ifdef MICROPY_PORT_NETWORK_INTERFACES
@@ -122,7 +122,7 @@ STATIC mp_obj_t network_hostname(size_t n_args, const mp_obj_t *args) {
} else {
size_t len;
const char *str = mp_obj_str_get_data(args[0], &len);
- if (len >= MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
+ if (len > MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
mp_raise_ValueError(NULL);
}
strcpy(mod_network_hostname, str);
diff --git a/extmod/modnetwork.h b/extmod/modnetwork.h
index e775612fe..e9769e309 100644
--- a/extmod/modnetwork.h
+++ b/extmod/modnetwork.h
@@ -56,10 +56,12 @@
extern char mod_network_country_code[2];
#ifndef MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN
-#define MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN (16)
+// Doesn't include the null terminator.
+#define MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN (32)
#endif
-extern char mod_network_hostname[MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN];
+// This is a null-terminated string.
+extern char mod_network_hostname[MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN + 1];
#if MICROPY_PY_LWIP
struct netif;
diff --git a/extmod/network_cyw43.c b/extmod/network_cyw43.c
index 168bd3d52..f26a835c1 100644
--- a/extmod/network_cyw43.c
+++ b/extmod/network_cyw43.c
@@ -500,7 +500,7 @@ STATIC mp_obj_t network_cyw43_config(size_t n_args, const mp_obj_t *args, mp_map
// TODO: Deprecated. Use network.hostname(name) instead.
size_t len;
const char *str = mp_obj_str_get_data(e->value, &len);
- if (len >= MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
+ if (len > MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
mp_raise_ValueError(NULL);
}
strcpy(mod_network_hostname, str);
diff --git a/ports/esp32/network_wlan.c b/ports/esp32/network_wlan.c
index 58af9f3bc..f06143bf7 100644
--- a/ports/esp32/network_wlan.c
+++ b/ports/esp32/network_wlan.c
@@ -524,7 +524,7 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_
// TODO: Deprecated. Use network.hostname(name) instead.
size_t len;
const char *str = mp_obj_str_get_data(kwargs->table[i].value, &len);
- if (len >= MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
+ if (len > MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
mp_raise_ValueError(NULL);
}
strcpy(mod_network_hostname, str);
diff --git a/ports/esp8266/network_wlan.c b/ports/esp8266/network_wlan.c
index 348d7f635..012cc970b 100644
--- a/ports/esp8266/network_wlan.c
+++ b/ports/esp8266/network_wlan.c
@@ -404,7 +404,7 @@ STATIC mp_obj_t esp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs
// TODO: Deprecated. Use network.hostname(name) instead.
size_t len;
const char *str = mp_obj_str_get_data(kwargs->table[i].value, &len);
- if (len >= MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
+ if (len > MICROPY_PY_NETWORK_HOSTNAME_MAX_LEN) {
mp_raise_ValueError(NULL);
}
strcpy(mod_network_hostname, str);