Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Set hostname for Ethernet router configuration? #554

Open
indexds opened this issue Jan 26, 2025 · 6 comments
Open

Set hostname for Ethernet router configuration? #554

indexds opened this issue Jan 26, 2025 · 6 comments

Comments

@indexds
Copy link
Contributor

indexds commented Jan 26, 2025

Hi, is there a way to set a hostname so that the computer the esp is connected to gets that hostname attributed on the wired connection? I saw this function

use esp_idf_svc::sys::esp_netif_set_hostname;

But its wrapped version isn't public..

fn set_hostname(&mut self, hostname: &str) -> Result<(), EspError>

And unless I'm mistaken, the new_with_conf function never sets a hostname if the configuration is in router mode. Why is that?

I also tried calling the function directly, just before starting the netif, but it didn't seem to work.

esp!(unsafe { esp_netif_set_hostname(self.handle, hostname.as_ptr() as *const _) })?;

The program runs fine, but the name hasn't changed. I get a generic "Network 5" wired connection name on my computer.

@ivmarkov
Copy link
Collaborator

esp_netif_set_hostname is exposed but in a very different way from the raw ESP-IDF API.
With that said, esp_netif_set_hostname will be completely useless to you, as it only works if the ESP is configured as a DHCP client, not as a router.

The program runs fine, but the name hasn't changed. I get a generic "Network 5" wired connection name on my computer.

I think you are additionally confusing L2 with L3. The esp_netif_set_hostname is setting the "pingable" IP-level name of the host (and again, only via DHCP).

What you are looking at is the L2 wired connection through ethernet or USB, which I don't think has anything to do with the IP/DNS hostname.

@ivmarkov
Copy link
Collaborator

ivmarkov commented Jan 26, 2025

If you want a nice name for your ESP hostname, you need to run a local DNS proxy (and deliver the DNS proxy IP via the DHCP conf to your PC) which resolves whatever name you select for your ESP as its IP, but further forwards all other DNS requests to the actual DNS server (1.1.1.1, 8.8.8.8 etc.). Difficult.

Or.... you can run mDNS, and then you can get your hostname as foo.local or something resolvable by the PC.

@indexds
Copy link
Contributor Author

indexds commented Jan 26, 2025

Huh, I thought the dhcp hostname option was sent over when the handshake is happening.

So you're saying mDNS is the solution here. Seems right to me. But iirc in #529 we established there was a problem when ethernet is in router mode..

I'm not too hung up over this though. If it doesn't work it's fine, as it doesn't impact functionality at all. It's purely cosmetic.

@ivmarkov
Copy link
Collaborator

You can still fix the mDNS issue if you want though - comment here: #529 (comment)

This you need to patch:

Also since you are using a "router" eth, you might want to set the IF name of your netif to ETH_DEF, if you follow the conversation in that thread ^^^.

@indexds
Copy link
Contributor Author

indexds commented Jan 26, 2025

Right yeah, that's what I have currently:

    let mut eth_netif = EspEth::wrap_all(
        eth_driver,
        EspNetif::new_with_conf(&NetifConfiguration {
            flags: 0,
            key: "ETH_DEF".try_into().unwrap(),
            description: "eth".try_into().unwrap(),
            route_priority: 10,
            ip_configuration: Some(Configuration::Router(RouterConfiguration {
                subnet: Subnet {
                    gateway: ETH_GATEWAY,
                    mask: Mask(30),
                },
                dhcp_enabled: true, // adds dhcp_server flag
                dns: None,
                secondary_dns: None,
            })),
            stack: NetifStack::Eth,
            custom_mac: None,
            got_ip_event_id: None,
            lost_ip_event_id: None,
        })?,
    )?;

I think I had tried at the time to do this and it didn't work so I gave up, but my memory's fuzzy on this. Might try again in a bit.

@indexds
Copy link
Contributor Author

indexds commented Feb 2, 2025

Ok so I took the time to recompile everything properly. The ethernet code is the one pasted above, I added those lines afterward (mdns is kept in scope after this, never dropped.)

let mut mdns = EspMdns::take()?;
mdns.set_hostname("charizhard")?;

I also added this line to kconfig CONFIG_MDNS_PREDEF_NETIF_ETH=y

Wireshark is not seeing any answers to my mdns requests.

My suspicion at this point is that this CONFIG_MDNS_PREDEF_NETIF_ETH=y parameter might be ignored at compile time.

The way i'm doing it is by simply adding the line at the end of the sdkconfig generated by cargo-pio. Is that not the way to go about this?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
Status: Todo
Development

No branches or pull requests

2 participants