How to ping to all three interfaces- ethernet,GSM and Wifi-sta

sara_patel
Posts: 5
Joined: Fri Apr 28, 2023 5:42 am

How to ping to all three interfaces- ethernet,GSM and Wifi-sta

Postby sara_patel » Fri Apr 28, 2023 6:07 am

Hello plz help me to ping at IP "8.8.8.8" from three interfaces.
I have tried with the following code
but that gives be error of ping handle being null
void ping_all_interfaces()
{
// Get the interface keys
uint32_t a32InterfaceKey[3];
uint8_t u8KeyIndex = 0U;
esp_netif_t *ifscan = esp_netif_next(NULL);
while (ifscan != NULL && u8KeyIndex < 3) {
a32InterfaceKey[u8KeyIndex++] = (uint32_t) esp_netif_get_ifkey(ifscan);
ifscan = esp_netif_next(ifscan);
}

// Set the target address
ip_addr_t target_addr;
memset(&target_addr, 0, sizeof(target_addr));
struct in_addr addr;
inet_pton(AF_INET, "8.8.8.8", &addr);
inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr);

// Create ping sessions for all three interfaces
for (int i = 0; i < 3; i++) {
esp_ping_config_t ping_config = ESP_PING_DEFAULT_CONFIG();
ping_config.target_addr = target_addr;
ping_config.count = ESP_PING_COUNT_INFINITE;
ping_config.interface = &a32InterfaceKey;
esp_ping_handle_t ping;
esp_ping_callbacks_t cbs;
cbs.on_ping_success = on_ping_success;
cbs.on_ping_timeout = on_ping_timeout;
cbs.on_ping_end = on_ping_end;
cbs.cb_args = NULL;
if (esp_ping_new_session(&ping_config, &cbs, &ping) == ESP_OK) {
if (esp_ping_start(ping) == ESP_OK) {
ESP_LOGI(TAG, "Started ping session for interface %d", i);
} else {
ESP_LOGE(TAG, "Failed to start ping session for interface %d", i);
}
} else {
ESP_LOGE(TAG, "Failed to create ping session for interface %d", i);
}
}
}

the moment I remove the " ping_config.interface = &a32InterfaceKey; " from the code it is working
Please help me ASAP

MicroController
Posts: 1734
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: How to ping to all three interfaces- ethernet,GSM and Wifi-sta

Postby MicroController » Sat Apr 29, 2023 9:28 pm

I believe instead of "ping_config.interface = &a32InterfaceKey;" you actually meant to use

Code: Select all

ping_config.interface = a32InterfaceKey[i];

sara_patel
Posts: 5
Joined: Fri Apr 28, 2023 5:42 am

Re: How to ping to all three interfaces- ethernet,GSM and Wifi-sta

Postby sara_patel » Mon May 01, 2023 10:34 am

thanks but that is also not working - I get the following error
ping handle can't be null
E (77602) PING: Failed to start ping session for interface 0

sara_patel
Posts: 5
Joined: Fri Apr 28, 2023 5:42 am

Re: How to ping to all three interfaces- ethernet,GSM and Wifi-sta

Postby sara_patel » Wed May 03, 2023 12:22 pm

Anyone plz help on this - after adding interface index key i get the send error as 0
on esp-ping_start

MikeMyhre
Posts: 54
Joined: Sat Nov 05, 2022 3:32 am

Re: How to ping to all three interfaces- ethernet,GSM and Wifi-sta

Postby MikeMyhre » Sun Aug 11, 2024 1:39 am

sara_patel wrote:
Wed May 03, 2023 12:22 pm
Anyone plz help on this - after adding interface index key i get the send error as 0
on esp-ping_start
I believe that the interface index is a number between 0 and 3. 0 means NETIF_NO_INDEX,
I am using the KSZ8863 switch part that provides two ethernet ports and one to the ESP32 core. If wifi is active, it may insert an interface either before or after the ethernet.

1 means the ESP32 stack, 2 and 3 indicate the other switch taps for port 1 and port 2 that can be accessed via the open() and read() write() tap devices like this:
eth_tap_fd_p1 = open("/dev/net/tap", O_NONBLOCK);
if (eth_tap_fd_p1 < 0) {
ESP_LOGE(TAG, "Unable to open RX P1 L2 TAP interface: errno %i", errno);
goto err;
}
// open TX port
eth_tap_fd_p2 = open("/dev/net/tap", O_NONBLOCK);
if (eth_tap_fd_p2 < 0) {
ESP_LOGE(TAG, "Unable to open TX P2 L2 TAP interface: errno %i", errno);
goto err;
}
ESP_LOGI("process_l2","ports open");

sohaib.q
Posts: 3
Joined: Fri Jun 21, 2024 2:26 pm

Re: How to ping to all three interfaces- ethernet,GSM and Wifi-sta

Postby sohaib.q » Thu Aug 22, 2024 8:36 am

Hi @sara_patel,
I just wanted to know if you were able to ping through all the interfaces or not.
I am doing a related thing, I have Wi-Fi and LTE (PPP) interfaces for internet connection and I want to ping each interface separately. As of now, I'm unable to do that.
Any help would be appreciated. Thanks

MikeMyhre
Posts: 54
Joined: Sat Nov 05, 2022 3:32 am

Re: How to ping to all three interfaces- ethernet,GSM and Wifi-sta

Postby MikeMyhre » Fri Aug 23, 2024 2:16 am

Yes. I have pings working in IDF version 5.2.1
I had to use a different ping code.

Here is my thread discussing it.
viewtopic.php?f=13&t=39160&p=136011#p136011

MikeMyhre
Posts: 54
Joined: Sat Nov 05, 2022 3:32 am

Re: How to ping to all three interfaces- ethernet,GSM and Wifi-sta

Postby MikeMyhre » Fri Aug 23, 2024 2:20 am

My ping code:

Code: Select all

/* ICMP echo example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/

#include <stdio.h>
#include <string.h>
#include "sdkconfig.h"
#include "lwip/inet.h"
#include "lwip/netdb.h"
#include "lwip/sockets.h"
#include "esp_console.h"
#include "esp_log.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "argtable3/argtable3.h"
//#include "protocol_examples_common.h"
#include "ping/ping_sock.h"
#include "switch_config.h"

esp_ping_handle_t ping_hdl;

uint8_t ping_last_hop_mac[2][6];


static void cmd_ping_on_ping_success(esp_ping_handle_t hdl, void *args)
{
    uint8_t ttl;
    uint16_t seqno;
    uint32_t elapsed_time, recv_len;
    ip_addr_t target_addr;
    esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
    esp_ping_get_profile(hdl, ESP_PING_PROF_TTL, &ttl, sizeof(ttl));
    esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
    esp_ping_get_profile(hdl, ESP_PING_PROF_SIZE, &recv_len, sizeof(recv_len));
    esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &elapsed_time, sizeof(elapsed_time));
    printf("%" PRIu32 " bytes from %s icmp_seq=%" PRIu16 " ttl=%" PRIu16 " time=%" PRIu32 " ms\n",
           recv_len, ipaddr_ntoa((ip_addr_t*)&target_addr), seqno, ttl, elapsed_time);
}

static void cmd_ping_on_ping_timeout(esp_ping_handle_t hdl, void *args)
{
    uint16_t seqno;
    ip_addr_t target_addr;
    esp_ping_get_profile(hdl, ESP_PING_PROF_SEQNO, &seqno, sizeof(seqno));
    esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
    printf("From %s icmp_seq=%d timeout\n",ipaddr_ntoa((ip_addr_t*)&target_addr), seqno);
}

static void cmd_ping_on_ping_end(esp_ping_handle_t hdl, void *args)
{
    ip_addr_t target_addr;
    uint32_t transmitted;
    uint32_t received;
    uint32_t total_time_ms;
    uint32_t loss;

    esp_ping_get_profile(hdl, ESP_PING_PROF_REQUEST, &transmitted, sizeof(transmitted));
    esp_ping_get_profile(hdl, ESP_PING_PROF_REPLY, &received, sizeof(received));
    esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
    esp_ping_get_profile(hdl, ESP_PING_PROF_DURATION, &total_time_ms, sizeof(total_time_ms));

    if (transmitted > 0) {
        loss = (uint32_t)((1 - ((float)received) / transmitted) * 100);
    } else {
        loss = 0;
    }

    int port = (int)args;
    ESP_LOGI("PING","Interface %d",port);
    ESP_LOG_BUFFER_HEX("PING", ping_last_hop_mac[port], 6);
    if (IP_IS_V4(&target_addr)) {
        printf("\n--- %s ping statistics ---\n", inet_ntoa(*ip_2_ip4(&target_addr)));
    } else {
        printf("\n--- %s ping statistics ---\n", inet6_ntoa(*ip_2_ip6(&target_addr)));
    }
    printf("%" PRIu32 " packets transmitted, %" PRIu32 " received, %" PRIu32 "%% packet loss, time %" PRIu32 "ms\n",
           transmitted, received, loss, total_time_ms);
    // delete the ping sessions, so that we clean up all resources and can create a new ping session
    // we don't have to call delete function in the callback, instead we can call delete function from other tasks
    esp_ping_delete_session(hdl);
}

static struct {
    struct arg_dbl *timeout;
    struct arg_dbl *interval;
    struct arg_int *data_size;
    struct arg_int *count;
    struct arg_int *tos;
    struct arg_int *ttl;
    struct arg_str *host;
    struct arg_end *end;
} ping_args;

int do_ping_cmd( char port, char *pingaddr )
{
    esp_ping_config_t config = ESP_PING_DEFAULT_CONFIG();
    config.interface =  port + 2;

    // parse IP address
    ip_addr_t target_addr;
    memset(&target_addr, 0, sizeof(target_addr));

    target_addr.type = 0;
    target_addr.u_addr.ip4.addr = esp_ip4addr_aton(pingaddr); //"8.8.8.8"); //0x8080808;

    config.target_addr = target_addr;

    /* set callback functions */
    esp_ping_callbacks_t cbs = {
        .cb_args = port,
        .on_ping_success = cmd_ping_on_ping_success,
        .on_ping_timeout = cmd_ping_on_ping_timeout,
        .on_ping_end = cmd_ping_on_ping_end
    };
    esp_ping_new_session(&config, &cbs, &ping_hdl);
    esp_ping_start(ping_hdl);

    return 0;
}


Who is online

Users browsing this forum: atx823, MicroController and 201 guests