How to get the gateway (router) MAC address when i using eth connection ?

A001FF
Posts: 12
Joined: Sat Apr 02, 2022 3:41 pm

How to get the gateway (router) MAC address when i using eth connection ?

Postby A001FF » Tue Mar 07, 2023 5:49 pm

or, it's possible to read arp table ?

User avatar
rudi ;-)
Posts: 1731
Joined: Fri Nov 13, 2015 3:25 pm

Re: How to get the gateway (router) MAC address when i using eth connection ?

Postby rudi ;-) » Wed Mar 08, 2023 11:05 am

To get the MAC address of the gateway (router) when using eth connection with Espressif ESP-IDF framework for the ESP32, you can use the following code:

Code: Select all

#include "esp_system.h"
#include "esp_netif.h"
#include "esp_eth.h"
#include "esp_log.h"
#include "esp_netif_dhcpc.h"

esp_eth_handle_t eth_handle = NULL;
esp_netif_t *eth_netif = NULL;

/* Function to get the default gateway MAC address */
void get_gateway_mac(void)
{
    esp_netif_ip_info_t ip_info;
    esp_netif_get_ip_info(eth_netif, &ip_info);
    /* Get the ARP table from the netif */
    arp_table_t *arp_table = esp_netif_get_arp_cache(eth_netif);
    /* Find the ARP entry with the gateway IP */
    arp_entry_t *arp_entry = arp_table_find(arp_table, &ip_info.gw);
    if (arp_entry != NULL) {
        ESP_LOGI(TAG, "Gateway MAC address: %02x:%02x:%02x:%02x:%02x:%02x",
                arp_entry->mac[0], arp_entry->mac[1], arp_entry->mac[2],
                arp_entry->mac[3], arp_entry->mac[4], arp_entry->mac[5]);
    } else {
        ESP_LOGW(TAG, "ARP entry not found for gateway IP");
    }
}

void app_main()
{
    /* Initialize the network interface */
    esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
    eth_netif = esp_netif_new(&netif_cfg);
    ESP_ERROR_CHECK(esp_eth_driver_install(&ETH_DRIVER_CONFIG, &eth_handle));
    ESP_ERROR_CHECK(esp_eth_start(eth_handle));
    ESP_ERROR_CHECK(esp_netif_set_default(eth_netif));

    /* Get the gateway MAC address */
    get_gateway_mac();
}
This code initializes the network interface using Ethernet, gets the IP address information and the ARP cache from the network interface, and finds the ARP entry with the gateway IP address to get the MAC address. If the ARP entry is found, it logs the MAC address to the console. If the ARP entry is not found, it logs a warning message to the console.

A001FF wrote:
Tue Mar 07, 2023 5:49 pm
or, it's possible to read arp table ?

It is also possible to read the ARP table by using the `arp_table_t *arp_table = esp_netif_get_arp_cache(eth_netif)` function, which returns a pointer to the ARP table. You can then use the `arp_table_find` function to find a specific ARP entry by IP address.



"AskRudi"
-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

A001FF
Posts: 12
Joined: Sat Apr 02, 2022 3:41 pm

Re: How to get the gateway (router) MAC address when i using eth connection ?

Postby A001FF » Wed Mar 08, 2023 12:13 pm

many thanks rudi,
but I using Arduino IDE, and with
etharp_get_entry(index_entry, &ip, &netif, &ethaddr)
ARP table is empty, but ethernet is working.

LE: it's working ! I have entrys in ARP table.
But is need to I ping esp32 from network.
I thought that MAC router appears in the ARP table after the DHCP process :)
So I still can't find out the router MAC :)
Last edited by A001FF on Wed Mar 08, 2023 12:37 pm, edited 1 time in total.

User avatar
rudi ;-)
Posts: 1731
Joined: Fri Nov 13, 2015 3:25 pm

Re: How to get the gateway (router) MAC address when i using eth connection ?

Postby rudi ;-) » Wed Mar 08, 2023 12:33 pm

A001FF wrote:
Wed Mar 08, 2023 12:13 pm
many thanks rudi,
but I using Arduino IDE, and with
etharp_get_entry(index_entry, &ip, &netif, &ethaddr)
ARP table is empty, but ethernet is working.
To get the router MAC address, you can send an ARP request to the router IP address and then wait for the router to respond. Once you receive the ARP response, it will contain the MAC address of the router.

To read the ARP table, you can use the etharp_get_entry() function as you mentioned. If the ARP table is empty, it might be because there are no entries in the table yet or because the function is not being called correctly. You may need to provide additional information such as the index_entry, ip, and netif parameters to properly retrieve entries in the table.

Please note that I'm not an Arduino Profi, and I'm not sooooo familiar with the specifics of the Arduino IDE or your network setup, so I can only provide general advice and not an exapmple - but hopes this helps for the basic to get on.

"AskRudi"
-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

User avatar
rudi ;-)
Posts: 1731
Joined: Fri Nov 13, 2015 3:25 pm

Re: How to get the gateway (router) MAC address when i using eth connection ?

Postby rudi ;-) » Wed Mar 08, 2023 12:38 pm

rudi ;-) wrote:
Wed Mar 08, 2023 12:33 pm
A001FF wrote:
Wed Mar 08, 2023 12:13 pm
many thanks rudi,
but I using Arduino IDE, and with
etharp_get_entry(index_entry, &ip, &netif, &ethaddr)
ARP table is empty, but ethernet is working.
To get the router MAC address, you can send an ARP request to the router IP address and then wait for the router to respond. Once you receive the ARP response, it will contain the MAC address of the router.

To read the ARP table, you can use the etharp_get_entry() function as you mentioned. If the ARP table is empty, it might be because there are no entries in the table yet or because the function is not being called correctly. You may need to provide additional information such as the index_entry, ip, and netif parameters to properly retrieve entries in the table.

Please note that I'm not an Arduino Profi, and I'm not sooooo familiar with the specifics of the Arduino IDE or your network setup, so I can only provide general advice and not an exapmple - but hopes this helps for the basic to get on.

"AskRudi"
Give it a try:

To get the MAC address of a router using the `etharp_get_entry` function on an ESP32 with Arduino, you can start by including the following libraries at the beginning of your sketch:

```

Code: Select all

#include <esp_eth.h>
#include <esp_eth_netif_glue.h>
#include <netif/etharp.h>
```

Next, you'll need to initialize the Ethernet network interface by calling the `esp_eth_init` function with the appropriate configuration parameters. Once that's done, you can use the `etharp_get_entry` function to retrieve the MAC address of the target IPv4 address, like so:

```

Code: Select all

struct eth_addr mac_addr;
ip4_addr_t ip_addr;
ip4addr_aton("192.168.1.1", &ip_addr);  // replace with your router's IP address
if (etharp_get_entry(&esp_netif_default, &ip_addr, &mac_addr) == ERR_OK) {
  Serial.print("Router MAC address: ");
  for (int i = 0; i < ETHARP_HWADDR_LEN; i++) {
    Serial.print(mac_addr.addr[i], HEX);
    if (i < ETHARP_HWADDR_LEN - 1) {
      Serial.print(":");
    }
  }
  Serial.println();
} else {
  Serial.println("Error retrieving MAC address");
}
```

Note that this code assumes that you have already connected to the network using the ESP32's built-in Ethernet interface, and that the router is reachable over the network. If you encounter any issues with this code, please consult the ESP-IDF documentation or the ESP32 forum again for further assistance.


"AskRudi"
-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

A001FF
Posts: 12
Joined: Sat Apr 02, 2022 3:41 pm

Re: How to get the gateway (router) MAC address when i using eth connection ?

Postby A001FF » Wed Mar 08, 2023 12:39 pm

Yes Rudi !
You're right.
Many thanks again.

User avatar
rudi ;-)
Posts: 1731
Joined: Fri Nov 13, 2015 3:25 pm

Re: How to get the gateway (router) MAC address when i using eth connection ?

Postby rudi ;-) » Wed Mar 08, 2023 12:56 pm

A001FF wrote:
Wed Mar 08, 2023 12:39 pm
Yes Rudi !
You're right.
Many thanks again.
cheers :)

You're welcome!
Thank you for your great feedback - Happy to help.

best wishes

rudi ;-)
-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

User avatar
rudi ;-)
Posts: 1731
Joined: Fri Nov 13, 2015 3:25 pm

Re: How to get the gateway (router) MAC address when i using eth connection ?

Postby rudi ;-) » Wed Mar 08, 2023 1:56 pm

A001FF wrote:
Wed Mar 08, 2023 12:13 pm
many thanks rudi,
but I using Arduino IDE, and with
etharp_get_entry(index_entry, &ip, &netif, &ethaddr)
ARP table is empty, but ethernet is working.

LE: it's working ! I have entrys in ARP table.
But is need to I ping esp32 from network.
I thought that MAC router appears in the ARP table after the DHCP process :)
So I still can't find out the router MAC :)
summary to your edit post, can say:

To obtain the MAC address of the router (gateway) using eth connection, you can try the following steps:

1. Send an ARP request to the broadcast address asking for the MAC address of the router. You can do this using the etharp_request function.

2. The router will reply to your ARP request with its MAC address.

3. You can then read the ARP table to obtain the MAC address of the gateway (router).

Note,
If the ARP table is empty, it may indicate that the device has not received any ARP responses. In this case, try pinging the router from the device and then check the ARP table again. Typically, the ARP table will only be populated with entries for devices that have communicated with the local device.


"AskRudi"
-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

Who is online

Users browsing this forum: Baidu [Spider] and 120 guests