Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Ping timeout (IDFGH-10552) #11792

Closed
3 tasks done
baldhead69 opened this issue Jul 3, 2023 · 6 comments
Closed
3 tasks done

Ping timeout (IDFGH-10552) #11792

baldhead69 opened this issue Jul 3, 2023 · 6 comments
Assignees
Labels
Resolution: Won't Do This will not be worked on Status: Done Issue is done internally

Comments

@baldhead69
Copy link

baldhead69 commented Jul 3, 2023

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

General issue report

Hi,

I am trying to ping my AP to confirm that the esp-idf icmp lib( lwIP ) is working, but a timeout occur.

My desktop is connected to same access point and pinging from desktop to access point is ok.
Pinging to esp32-s3 from desktop is ok too.

What may be the problem ?

Code:

////////////////////////////////////////////////////////////////////////////////////////////
//

static void test_on_ping_success(esp_ping_handle_t hdl, void *args)
{
    // optionally, get callback arguments
    // const char* str = (const char*) args;
    // printf("%s\r\n", str); // "foo"
    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("%lu bytes from %s icmp_seq=%u ttl=%u time=%lu ms\n", recv_len, inet_ntoa(target_addr.u_addr.ip4), seqno, ttl, elapsed_time);
}

////////////////////////////////////////////////////////////////////////////////////////////
//

static void test_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", inet_ntoa(target_addr.u_addr.ip4), seqno);
}

////////////////////////////////////////////////////////////////////////////////////////////
//

static void test_on_ping_end(esp_ping_handle_t hdl, void *args)
{
    uint32_t transmitted;
    uint32_t received;
    uint32_t total_time_ms;

    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_DURATION, &total_time_ms, sizeof(total_time_ms));
    printf("%lu packets transmitted, %lu received, time %lums\n", transmitted, received, total_time_ms);
}

////////////////////////////////////////////////////////////////////////////////////////////
//

void initialize_ping()
{
/*    
    // convert URL to IP address
    ip_addr_t target_addr;
    struct addrinfo hint;
    struct addrinfo *res = NULL;
    memset(&hint, 0, sizeof(hint));
    memset(&target_addr, 0, sizeof(target_addr));
    
    int ret = getaddrinfo(MY_PING_URL, NULL, &hint, &res);
    if( ret != 0 )
    {
        printf( "%s: Unknown host [ %s ]\n", MY_PING_TAG, MY_PING_URL );
        printf( "ret = %d\n", ret );
        return;
    }

    struct in_addr addr4 = ((struct sockaddr_in *) (res->ai_addr))->sin_addr;
    inet_addr_to_ip4addr(ip_2_ip4(&target_addr), &addr4);
    freeaddrinfo(res);

    esp_ping_config_t ping_config = ESP_PING_DEFAULT_CONFIG();
    ping_config.target_addr = target_addr;          // target IP address
    ping_config.count = ESP_PING_COUNT_INFINITE;    // ping in infinite mode, esp_ping_stop can stop it
*/

    ip_addr_t target_addr;

    //target_addr.u_addr.ip4.addr = 3372329152;  // u32_t. 192.168.1.201. Tem que escrever 201.1.168.192.    

    int ret = inet_aton( "192.168.1.201", &target_addr.u_addr.ip4 );
    if( ret == 0 )
    {
        printf( "%s: cp could not be converted to addr\n", MY_PING_TAG );        
    }
    printf( "target_addr.u_addr.ip4 = %lu\n", target_addr.u_addr.ip4.addr );


    ret = inet_aton( "192.168.1.1", &target_addr.u_addr.ip4 );
    if( ret == 0 )
    {
        printf( "%s: cp could not be converted to addr\n", MY_PING_TAG );        
    }
    printf( "target_addr.u_addr.ip4 = %lu\n", target_addr.u_addr.ip4.addr );

    esp_ping_config_t ping_config = ESP_PING_DEFAULT_CONFIG();
    ping_config.target_addr = target_addr;  // target IP address
    ping_config.count = ESP_PING_COUNT_INFINITE;    // ping in infinite mode, esp_ping_stop can stop it
    ping_config.timeout_ms = 5000;

    // set callback functions
    esp_ping_callbacks_t cbs;
    cbs.on_ping_success = test_on_ping_success;
    cbs.on_ping_timeout = test_on_ping_timeout;
    cbs.on_ping_end = test_on_ping_end;
    cbs.cb_args = NULL;  // arguments that will feed to all callback functions, can be NULL
    
    esp_ping_handle_t ping;
    esp_ping_new_session(&ping_config, &cbs, &ping);
    esp_ping_start(ping);
}

esp-idf monitor:

From 192.168.1.1 icmp_seq=1 timeout
to != NULL
tolen != 0
IS_SOCK_ADDR_LEN_VALID = YES
IS_SOCK_ADDR_TYPE_VALID = NO
to->sa_family = 0
IS_SOCK_ADDR_ALIGNED = YES
I (9154) MY_APP: MALLOC_CAP_INTERNAL = 161852
I (9154) MY_APP: MALLOC_CAP_SPIRAM = 6789832

From 192.168.1.1 icmp_seq=2 timeout
to != NULL
tolen != 0
IS_SOCK_ADDR_LEN_VALID = YES
IS_SOCK_ADDR_TYPE_VALID = NO
to->sa_family = 0
IS_SOCK_ADDR_ALIGNED = YES
I (14154) MY_APP: MALLOC_CAP_INTERNAL = 162128
I (14154) MY_APP: MALLOC_CAP_SPIRAM = 6789832

From 192.168.1.1 icmp_seq=3 timeout
to != NULL
tolen != 0
IS_SOCK_ADDR_LEN_VALID = YES
IS_SOCK_ADDR_TYPE_VALID = NO
to->sa_family = 0
IS_SOCK_ADDR_ALIGNED = YES
I (19154) MY_APP: MALLOC_CAP_INTERNAL = 162128
I (19154) MY_APP: MALLOC_CAP_SPIRAM = 6789832

From 192.168.1.1 icmp_seq=4 timeout
to != NULL
tolen != 0
IS_SOCK_ADDR_LEN_VALID = YES
IS_SOCK_ADDR_TYPE_VALID = NO
to->sa_family = 0
IS_SOCK_ADDR_ALIGNED = YES
I (24154) MY_APP: MALLOC_CAP_INTERNAL = 162128
I (24154) MY_APP: MALLOC_CAP_SPIRAM = 6789832

From 192.168.1.1 icmp_seq=5 timeout
to != NULL
tolen != 0
IS_SOCK_ADDR_LEN_VALID = YES
IS_SOCK_ADDR_TYPE_VALID = NO
to->sa_family = 0
IS_SOCK_ADDR_ALIGNED = YES
I (29154) MY_APP: MALLOC_CAP_INTERNAL = 162128
I (29154) MY_APP: MALLOC_CAP_SPIRAM = 6789832

From 192.168.1.1 icmp_seq=6 timeout
to != NULL
tolen != 0
IS_SOCK_ADDR_LEN_VALID = YES
IS_SOCK_ADDR_TYPE_VALID = NO
to->sa_family = 0
IS_SOCK_ADDR_ALIGNED = YES
I (34154) MY_APP: MALLOC_CAP_INTERNAL = 162128
I (34154) MY_APP: MALLOC_CAP_SPIRAM = 6789832

From 192.168.1.1 icmp_seq=7 timeout
to != NULL
tolen != 0
IS_SOCK_ADDR_LEN_VALID = YES
IS_SOCK_ADDR_TYPE_VALID = NO
to->sa_family = 0
IS_SOCK_ADDR_ALIGNED = YES
I (39154) MY_APP: MALLOC_CAP_INTERNAL = 162128
I (39154) MY_APP: MALLOC_CAP_SPIRAM = 6789832

From 192.168.1.1 icmp_seq=8 timeout
to != NULL
tolen != 0
IS_SOCK_ADDR_LEN_VALID = YES
IS_SOCK_ADDR_TYPE_VALID = NO
to->sa_family = 0
IS_SOCK_ADDR_ALIGNED = YES
I (44154) MY_APP: MALLOC_CAP_INTERNAL = 162128
I (44154) MY_APP: MALLOC_CAP_SPIRAM = 6789832

From 192.168.1.1 icmp_seq=9 timeout
to != NULL
tolen != 0
IS_SOCK_ADDR_LEN_VALID = YES
IS_SOCK_ADDR_TYPE_VALID = NO
to->sa_family = 0
IS_SOCK_ADDR_ALIGNED = YES
I (49154) MY_APP: MALLOC_CAP_INTERNAL = 162128
I (49154) MY_APP: MALLOC_CAP_SPIRAM = 6789832

From 192.168.1.1 icmp_seq=10 timeout
to != NULL
tolen != 0
IS_SOCK_ADDR_LEN_VALID = YES
IS_SOCK_ADDR_TYPE_VALID = NO
to->sa_family = 0
IS_SOCK_ADDR_ALIGNED = YES
I (54154) MY_APP: MALLOC_CAP_INTERNAL = 162128
I (54154) MY_APP: MALLOC_CAP_SPIRAM = 6789832

sockets.c file:

ssize_t
lwip_sendto(int s, const void *data, size_t size, int flags,
            const struct sockaddr *to, socklen_t tolen)
{
  struct lwip_sock *sock;
  err_t err;
  u16_t short_size;
  u16_t remote_port;
  struct netbuf buf;

  sock = get_socket(s);
  if (!sock) {
    return -1;
  }

  if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) {
#if LWIP_TCP
    done_socket(sock);
    return lwip_send(s, data, size, flags);
#else /* LWIP_TCP */
    LWIP_UNUSED_ARG(flags);
    sock_set_errno(sock, err_to_errno(ERR_ARG));
    done_socket(sock);
    return -1;
#endif /* LWIP_TCP */
  }

  if (size > LWIP_MIN(0xFFFF, SSIZE_MAX)) {
    /* cannot fit into one datagram (at least for us) */
    sock_set_errno(sock, EMSGSIZE);
    done_socket(sock);
    return -1;
  }
  short_size = (u16_t)size;



  printf( (to == NULL) ? "to == NULL" : "to != NULL" );
  printf( "\n" );

  printf( (tolen == 0) ? "tolen == 0" : "tolen != 0" );
  printf( "\n" );

  printf( (IS_SOCK_ADDR_LEN_VALID(tolen)) ? "IS_SOCK_ADDR_LEN_VALID = YES" : "IS_SOCK_ADDR_LEN_VALID = NO" );
  printf( "\n" );

  printf( (IS_SOCK_ADDR_TYPE_VALID(to)) ? "IS_SOCK_ADDR_TYPE_VALID = YES" : "IS_SOCK_ADDR_TYPE_VALID = NO" );
  printf( "\nto->sa_family = %u", to->sa_family );
  printf( "\n" );

  printf( (IS_SOCK_ADDR_ALIGNED(to)) ? "IS_SOCK_ADDR_ALIGNED = YES" : "IS_SOCK_ADDR_ALIGNED = NO" );
  printf( "\n" );




  LWIP_ERROR("lwip_sendto: invalid address", (((to == NULL) && (tolen == 0)) ||
             (IS_SOCK_ADDR_LEN_VALID(tolen) &&
              ((to != NULL) && (IS_SOCK_ADDR_TYPE_VALID(to) && IS_SOCK_ADDR_ALIGNED(to))))),
             sock_set_errno(sock, err_to_errno(ERR_ARG)); done_socket(sock); return -1;);
  LWIP_UNUSED_ARG(tolen);

  /* initialize a buffer */
  buf.p = buf.ptr = NULL;
#if LWIP_CHECKSUM_ON_COPY
  buf.flags = 0;
#endif /* LWIP_CHECKSUM_ON_COPY */
  if (to) {
    SOCKADDR_TO_IPADDR_PORT(to, &buf.addr, remote_port);
  } else {
    remote_port = 0;
    ip_addr_set_any(NETCONNTYPE_ISIPV6(netconn_type(sock->conn)), &buf.addr);
  }
  netbuf_fromport(&buf) = remote_port;


  LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_sendto(%d, data=%p, short_size=%"U16_F", flags=0x%x to=",
                              s, data, short_size, flags));
  ip_addr_debug_print_val(SOCKETS_DEBUG, buf.addr);
  LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F"\n", remote_port));

  /* make the buffer point to the data that should be sent */
#if LWIP_NETIF_TX_SINGLE_PBUF
  /* Allocate a new netbuf and copy the data into it. */
  if (netbuf_alloc(&buf, short_size) == NULL) {
    err = ERR_MEM;
  } else {
#if LWIP_CHECKSUM_ON_COPY
    if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_RAW) {
      u16_t chksum = LWIP_CHKSUM_COPY(buf.p->payload, data, short_size);
      netbuf_set_chksum(&buf, chksum);
    } else
#endif /* LWIP_CHECKSUM_ON_COPY */
    {
      MEMCPY(buf.p->payload, data, short_size);
    }
    err = ERR_OK;
  }
#else /* LWIP_NETIF_TX_SINGLE_PBUF */
  err = netbuf_ref(&buf, data, short_size);
#endif /* LWIP_NETIF_TX_SINGLE_PBUF */
  if (err == ERR_OK) {
#if LWIP_IPV4 && LWIP_IPV6
    /* Dual-stack: Unmap IPv4 mapped IPv6 addresses */
    if (IP_IS_V6_VAL(buf.addr) && ip6_addr_isipv4mappedipv6(ip_2_ip6(&buf.addr))) {
      unmap_ipv4_mapped_ipv6(ip_2_ip4(&buf.addr), ip_2_ip6(&buf.addr));
      IP_SET_TYPE_VAL(buf.addr, IPADDR_TYPE_V4);
    }
#endif /* LWIP_IPV4 && LWIP_IPV6 */

    /* send the data */
    err = netconn_send(sock->conn, &buf);
  }

  /* deallocated the buffer */
  netbuf_free(&buf);

  sock_set_errno(sock, err_to_errno(err));
  done_socket(sock);
  return (err == ERR_OK ? short_size : -1);
}


From 192.168.1.1 icmp_seq=4 timeout
to != NULL
tolen != 0
IS_SOCK_ADDR_LEN_VALID = YES
IS_SOCK_ADDR_TYPE_VALID = NO
to->sa_family = 0
IS_SOCK_ADDR_ALIGNED = YES
I (24154) MY_APP: MALLOC_CAP_INTERNAL = 162128
I (24154) MY_APP: MALLOC_CAP_SPIRAM = 6789832

Notes:
esp-idf v5.0.2

Thank's.

@espressif-bot espressif-bot added the Status: Opened Issue is new label Jul 3, 2023
@github-actions github-actions bot changed the title Ping timeout Ping timeout (IDFGH-10552) Jul 3, 2023
@nopnop2002
Copy link

nopnop2002 commented Jul 4, 2023

try this.

https://github.com/nopnop2002/esp-idf-ping

I (7438) PING: 64 bytes from 18.65.206.121 icmp_seq=1 ttl=246 time=20 ms
I (8438) PING: 64 bytes from 18.65.206.121 icmp_seq=2 ttl=246 time=25 ms
I (9438) PING: 64 bytes from 18.65.206.121 icmp_seq=3 ttl=246 time=24 ms
I (10438) PING: 64 bytes from 18.65.206.121 icmp_seq=4 ttl=246 time=25 ms

@baldhead69
Copy link
Author

baldhead69 commented Jul 4, 2023

@nopnop2002

Your project worked, thank's.

I will take a better look at your code.

My project have a websocket server running on it.

Could it be that the websocket server is causing the error in my project ?

I (3777) main: Connection success
I (3787) main: target host is own gateway
I (3787) PING: ip_info.gw=192.168.1.1
I (3797) PING: gateway_addr.u_addr.ip4=192.168.1.1
to != NULL
tolen != 0
IS_SOCK_ADDR_LEN_VALID = YES
IS_SOCK_ADDR_TYPE_VALID = YES
to->sa_family = 2
IS_SOCK_ADDR_ALIGNED = YES
I (3817) PING: ping start
I (3817) main: initialize_ping success
I (3827) PING: 64 bytes from 192.168.1.1 icmp_seq=1 ttl=64 time=9 ms
to != NULL
tolen != 0
IS_SOCK_ADDR_LEN_VALID = YES
IS_SOCK_ADDR_TYPE_VALID = YES
to->sa_family = 2
IS_SOCK_ADDR_ALIGNED = YES
I (4807) PING: 64 bytes from 192.168.1.1 icmp_seq=2 ttl=64 time=8 ms
to != NULL
tolen != 0
IS_SOCK_ADDR_LEN_VALID = YES
IS_SOCK_ADDR_TYPE_VALID = YES
to->sa_family = 2
IS_SOCK_ADDR_ALIGNED = YES
I (5797) PING: 64 bytes from 192.168.1.1 icmp_seq=3 ttl=64 time=2 ms
to != NULL
tolen != 0
IS_SOCK_ADDR_LEN_VALID = YES
IS_SOCK_ADDR_TYPE_VALID = YES
to->sa_family = 2
IS_SOCK_ADDR_ALIGNED = YES

@baldhead69
Copy link
Author

baldhead69 commented Jul 4, 2023

Hi,

Problem solved.

I changed( put it ) this in my code:

  • ESP_ERROR_CHECK( esp_wifi_set_ps( WIFI_PS_NONE ) );
  • target_addr.type = IPADDR_TYPE_V4;

But i think that the responsible for the non-functioning was only the lack of this: "target_addr.type = IPADDR_TYPE_V4;"

Thank's again @nopnop2002.

Please espressif, fix the example once and for all.
https://docs.espressif.com/projects/esp-idf/en/v5.0.2/esp32s3/api-reference/protocols/icmp_echo.html?highlight=icmp

@david-cermak
Copy link
Collaborator

@baldhead69 do yo have IPv6 capable local network?

But i think that the responsible for the non-functioning was only the lack of this: "target_addr.type = IPADDR_TYPE_V4;"

I don't think we should restrict the example to support the IPv4 only networks. In case users don't want to use IPv6 they could simply disable it and the example would work as IPv4 only. Do you see any issue with that?

@baldhead69
Copy link
Author

baldhead69 commented Oct 11, 2023

@baldhead69 do yo have IPv6 capable local network?

But i think that the responsible for the non-functioning was only the lack of this: "target_addr.type = IPADDR_TYPE_V4;"

I don't think we should restrict the example to support the IPv4 only networks. In case users don't want to use IPv6 they could simply disable it and the example would work as IPv4 only. Do you see any issue with that?

Hi @david-cermak ,

In principle this problem is solved for me.

Could you help me with this ?

https://www.esp32.com/viewtopic.php?f=13&t=34682&sid=ae1b5c32da704235f4abd52edef48051&start=30

#11903

no one from espressif answers anything

@david-cermak
Copy link
Collaborator

In principle this problem is solved for me.

okay, closing. Also seeing that your linked issue has been addressed, too.

@espressif-bot espressif-bot added Status: Done Issue is done internally Resolution: Won't Do This will not be worked on and removed Status: Opened Issue is new labels Oct 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Won't Do This will not be worked on Status: Done Issue is done internally
Projects
None yet
Development

No branches or pull requests

4 participants