network.hostname: max length of hostname
Checks
-
I agree to follow the MicroPython Code of Conduct to ensure a safe and respectful space for everyone.
-
I've searched for existing issues regarding this feature, and didn't find any.
Description
Form the docs: the max length depends on the port (and its tcp/ip stack).
So, without spelunking into each network low-level libraries or make tests on each specific board, we don't know the limit.
Proposal: calling hostname(None) returns the accepted max length of hostname.
Code Size
No response
Implementation
- I intend to implement this feature and would submit a Pull Request if desirable.
extmod/modnetwork: Add global hostname/LWIP-mDNS.
This PR proposes to globally move hostname definition to the network module, so that it will be defined for all nic boards by using the network.hostname() method. The default host name is set to mp-host and the hostname length is controlled by the MICROPY_PY_HOSTNAME_LENGTH constant which is defaulted to 32 if not defined.
>>> import network
>>> network.hostname("my-host")
>>> network.hostname()
my-host
>>>
Note: This PR would replace some of the modifications done specifically for the cyw43 board only (PR #8918), but keeps the ioctl functions up and running using this global hostname definition.
If the nic board uses the LWIP library, we take advantage of the integrated mDNS server (if the LWIP_MDNS_RESPONDER constant is defined) to a) declare hostname for the nic board; b) append three functions to the nic object to add, rename and remove a service to the mDNS server for that nic.
>>> import network
>>> network.hostname("my-host")
>>> nic = network.WIZNET5K()
>>> nic.active(True)
>>> nic.ifconfig("dhcp")
>>> # nic.ifconfig(("192.168.1.100", "255.255.255.0", "0.0.0.0", "0.0.0.0")) # for fixed IP
>>> svc_token = nic.mdns_add_service("", "_http", network.DNSSD_PROTO_TCP, 80)
>>> nic.mdns_rename_service(svc_token, "my_http_server")
>>> nic.mdns_remove_service(svc_token)
From now on, on the network, you can perform a ping my-host.local to get a response from your micropython board.
I could not test these modifications for all nic boards, I have only WIZNET5K boards available. The pico_w board compiles though.
The actual only limitation is to define the hostname via network.hostname before creating the nic object. Renaming the hostname after the creation of the nic object does not reflect this name change. To make this mDNS hostname change, it requires to global change to the I don't dare to go further for the time being. I need guidance.mod_network_nic_type_t and probably put all the socket oriented functions (that are probably sitting there for older versions before the use of LWIP library) into another struct (like mod_network_custom_fun_type_t for example) pointed by this mod_network_nic_type_t to save ROM space for nic boards that do not use these specific functions, make sure that all nic board objects really derive from this mod_network_nic_type_t and add a void * netif_ptr member to the mod_network_nic_type_t that would point to the netif of the nic board if available, so that the mod_network could access this parameter to use LWIP mDNS functions. This netif_ptr member will be set by the nic board driver so that boards that have multiple netif(s) will set the right one (for the cyw43 for instance). But
Comments and help are more than welcome as I am quite new to micropython firmware and I don't know all the internals as well as you guys.