2025-04-25

[siwg917] 공유기 mac 얻기, arp table에서 확인.



1. arp 요청함수.

#include "lwip/netif.h"
#include "lwip/ip4_addr.h"
#include "lwip/etharp.h"

#define ARP_RETRY_COUNT 10
#define ARP_WAIT_MS 100

void request_and_print_gateway_mac(const sl_net_wifi_client_profile_t *profile, struct netif *netif)
{
ip4_addr_t gateway_ip;
IP4_ADDR(&gateway_ip,
profile->ip.ip.v4.gateway.bytes[0],
profile->ip.ip.v4.gateway.bytes[1],
profile->ip.ip.v4.gateway.bytes[2],
profile->ip.ip.v4.gateway.bytes[3]);

// 1. ARP 요청 전송
if (etharp_request(netif, &gateway_ip) != ERR_OK) {
printf("Failed to send ARP request to gateway\r\n");
return;
}

// 2. ARP 응답 대기 및 MAC 주소 찾기
struct eth_addr *eth_ret;
const ip4_addr_t *resolved_ip;

for (int i = 0; i < ARP_RETRY_COUNT; i++) {
if (etharp_find_addr(netif, &gateway_ip, &eth_ret, &resolved_ip) >= 0) {
printf("Gateway MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n",
eth_ret->addr[0], eth_ret->addr[1], eth_ret->addr[2],
eth_ret->addr[3], eth_ret->addr[4], eth_ret->addr[5]);
return;
}
osDelay(100); // FreeRTOS 기반 대기
}

printf("Gateway MAC: [Not found in ARP cache after ARP request]\r\n");
}


2. 업데이트된 arp테이블에서 mac 보기. (함수일부)
void display_client_network_info(sl_net_wifi_client_profile_t profile)
{

printf("\r\n=== Wi-Fi Client Network Information ===\r\n");
printf("SSID: %s\r\n", profile.config.ssid.value);
printf("Security: %d\r\n", profile.config.security);

// IP 정보 표시
printf("\r\n--- IP Configuration ---\r\n");
printf("IP Management Mode: %s\r\n",
profile.ip.mode == SL_IP_MANAGEMENT_DHCP ? "DHCP" :
profile.ip.mode == SL_IP_MANAGEMENT_STATIC_IP ? "Static IP" : "Link Local");

if (profile.ip.type & SL_IPV4) {
printf("IPv4 Address: %d.%d.%d.%d\r\n",
profile.ip.ip.v4.ip_address.bytes[0],
profile.ip.ip.v4.ip_address.bytes[1],
profile.ip.ip.v4.ip_address.bytes[2],
profile.ip.ip.v4.ip_address.bytes[3]);

printf("Subnet Mask: %d.%d.%d.%d\r\n",
profile.ip.ip.v4.netmask.bytes[0],
profile.ip.ip.v4.netmask.bytes[1],
profile.ip.ip.v4.netmask.bytes[2],
profile.ip.ip.v4.netmask.bytes[3]);

printf("Gateway: %d.%d.%d.%d\r\n",
profile.ip.ip.v4.gateway.bytes[0],
profile.ip.ip.v4.gateway.bytes[1],
profile.ip.ip.v4.gateway.bytes[2],
profile.ip.ip.v4.gateway.bytes[3]);

// Gateway MAC 주소 표시
struct netif *netif = netif_default; // 또는 Wi-Fi에 해당하는 netif 지정
ip4_addr_t gateway_ip;
struct eth_addr *eth_ret;
const ip4_addr_t *resolved_ip;

IP4_ADDR(&gateway_ip,
profile.ip.ip.v4.gateway.bytes[0],
profile.ip.ip.v4.gateway.bytes[1],
profile.ip.ip.v4.gateway.bytes[2],
profile.ip.ip.v4.gateway.bytes[3]);

if (etharp_find_addr(netif, &gateway_ip, &eth_ret, &resolved_ip) >= 0) {
printf("Gateway MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n",
eth_ret->addr[0], eth_ret->addr[1], eth_ret->addr[2],
eth_ret->addr[3], eth_ret->addr[4], eth_ret->addr[5]);
} else {
printf("Gateway MAC: [Not found in ARP cache]\r\n");
}
}
}

3. 실행위치
static void application_start(void *argument)
{
UNUSED_PARAMETER(argument);
sl_status_t status;
sl_ip_address_t ip_address = { 0 };
sl_net_wifi_client_profile_t profile = { 0 };
sl_wifi_performance_profile_t performance_profile = { .profile = ASSOCIATED_POWER_SAVE_LOW_LATENCY };
status = sl_net_init(SL_NET_WIFI_CLIENT_INTERFACE, &client_configuration, &wifi_client_context, NULL);
if (status != SL_STATUS_OK) {
printf("Failed to start Wi-Fi Client interface: 0x%lx\r\n", status);
return;
}
printf("Wi-Fi Client interface success\r\n");

#ifdef SLI_SI91X_MCU_INTERFACE
uint8_t xtal_enable = 1;
status = sl_si91x_m4_ta_secure_handshake(SL_SI91X_ENABLE_XTAL, 1, &xtal_enable, 0, NULL);
if (status != SL_STATUS_OK) {
printf("Failed to bring m4_ta_secure_handshake: 0x%lx\r\n", status);
return;
}
printf("m4_ta_secure_handshake Success\r\n");

댓글 없음:

댓글 쓰기

[siwg917] 공유기 mac 얻기, arp table에서 확인.

1. arp 요청함수. #include "lwip/netif.h" #include "lwip/ip4_addr.h" #include "lwip/etharp.h" #define ARP_RETRY...