How to get the gateway (router) MAC address when i using eth connection ?
Posted: Tue Mar 07, 2023 5:49 pm
or, it's possible to read arp table ?
ESP32 Official Forum
http://forum.esp32.com/
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(Ð_DRIVER_CONFIG, ð_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();
}
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.
Give it a try:rudi ;-) wrote: ↑Wed Mar 08, 2023 12:33 pmTo 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"
Code: Select all
#include <esp_eth.h>
#include <esp_eth_netif_glue.h>
#include <netif/etharp.h>
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");
}
summary to your edit post, can say:A001FF wrote: ↑Wed Mar 08, 2023 12:13 pmmany thanks rudi,
but I using Arduino IDE, and with
etharp_get_entry(index_entry, &ip, &netif, ðaddr)
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