Pinging a subnet fails (ping_sock.h) : memory leak?

bobo1974
Posts: 26
Joined: Fri Feb 08, 2019 2:14 pm

Pinging a subnet fails (ping_sock.h) : memory leak?

Postby bobo1974 » Sun Sep 12, 2021 7:16 pm

I want to ping a portion of the subnet where the ESP32 is located.
Note I am using the ping_sock.h IDF library directly, even if I am using the Arduino IDE (with board manager 2.0.0), hence posting here.
I am using the official code from ICMP Echo documentation (https://docs.espressif.com/projects/esp ... light=icmp), and pinging one host works fine.

Now, to ping the subnet I created a loop where I do a full ping procedure (ie creating and destroying a “ping session” as per documentation) on say 20 members of the subnet, just increasing the last digit.


Issue is the program crashes after ~10 pings with :

Code: Select all

E (4774) ping_sock: esp_ping_new_session(270): create socket failed: -1 
I suspected it is a memory issue so I printed the heap memory and this is the result below. Heap is decreasing.
Am I doing something wrong?

Code: Select all

11:31:15.981 -> .Pinging Subnet
11:31:15.981 -> i: 1
11:31:15.981 -> xPortGetFreeHeapSize :141432
11:31:16.171 -> From 192.168.68.119 icmp_seq=1 timeout
11:31:16.171 -> From 192.168.68.119: 1 packets transmitted, 0 received, time 199ms
11:31:16.171 -> Failure
11:31:16.171 -> i: 2
11:31:16.171 -> xPortGetFreeHeapSize :137656
11:31:16.266 -> 64 bytes from 192.168.68.118 icmp_seq=1 ttl=64 time=63 ms
11:31:16.266 -> From 192.168.68.118: 1 packets transmitted, 1 received, time 63ms
11:31:16.266 -> Success:1
……
11:31:17.167 -> i: 9
11:31:17.167 -> xPortGetFreeHeapSize :121736
11:31:17.359 -> 64 bytes from 192.168.68.111 icmp_seq=1 ttl=255 time=197 ms
11:31:17.359 -> From 192.168.68.111: 1 packets transmitted, 1 received, time 197ms
11:31:17.359 -> Success:4
11:31:17.359 -> i: 10
11:31:17.359 -> xPortGetFreeHeapSize :119564
11:31:17.596 -> From 192.168.68.110 icmp_seq=1 timeout
11:31:17.596 -> From 192.168.68.110: 1 packets transmitted, 0 received, time 199ms
11:31:17.596 -> Failure
11:31:17.596 -> i: 11
11:31:17.596 -> xPortGetFreeHeapSize :117028
11:31:17.596 -> E (4774) ping_sock: esp_ping_new_session(270): create socket failed: -1
11:31:17.596 -> E (4774) ping_sock: esp_ping_start(340): ping handle can't be null 

Code: Select all

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

  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));
  esp_ping_get_profile(hdl, ESP_PING_PROF_IPADDR, &target_addr, sizeof(target_addr));
  printf("From %s: %d packets transmitted, %d received, time %dms\n", inet_ntoa(target_addr.u_addr.ip4), transmitted, received, total_time_ms);

  if (received >= 1) {
    pingResults = 2; // success
  }
  if ((transmitted >= 1) && (received == 0)) {
    pingResults = 1; // timeout
  }
}

bool pingSubnet() {
  int successPingCount = 0;
  Serial.println("Pinging Subnet");
  IPAddress gwIP = WiFi.gatewayIP();
  IPAddress staIP = WiFi.localIP();
  IPAddress mask = WiFi.subnetMask();
  int ia[4] = { 0, 0, 0, 0 };
  ia[0] = staIP[0];  ia[1] = staIP[1];  ia[2] = staIP[2];  ia[3] = staIP[3];
  
  struct ip_addr target_addr;
  esp_ping_handle_t ping;
  esp_ping_callbacks_t cbs;
  
  for (int i = 1; i <= ICMPRANGE; i++) {// ping n IPs before currentIP.
    memset(&target_addr, 0, sizeof(target_addr));
    memset(&ping, 0, sizeof(ping));
    memset(&cbs, 0, sizeof(cbs));
    esp_ping_config_t ping_config = ESP_PING_DEFAULT_CONFIG();
    ping_config.count = 1;
    ping_config.timeout_ms = 200;
    ping_config.interval_ms = 1;
    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
    ia[3] = staIP[3] - i;
    IP4_ADDR(ip_2_ip4(&target_addr), ia[0], ia[1], ia[2], ia[3]);
    ping_config.target_addr = target_addr;
    Serial.print("i: "); Serial.println(i);
    printf("xPortGetFreeHeapSize :%d\n", xPortGetFreeHeapSize() );
    pingResults = 0;
      ping_config.target_addr = target_addr;
      esp_ping_new_session(&ping_config, &cbs, &ping);
      esp_ping_start(ping);

      while (pingResults == 0) {
        delay(10);
      }

      esp_ping_stop(ping);
      esp_ping_delete_session(&ping);
      delay(300);
      if (pingResults == 2) {
      successPingCount++;
      Serial.print("Success:"); Serial.println(successPingCount); 
      } else {
        Serial.println("Failure");
      }
    }
  }
    return false;
}

bobo1974
Posts: 26
Joined: Fri Feb 08, 2019 2:14 pm

Re: Pinging a subnet fails (ping_sock.h) : memory leak?

Postby bobo1974 » Tue Sep 14, 2021 4:27 pm

I have solved this issue by using ping code from this link:
https://github.com/espressif/esp-idf/issues/5202

I think it had to do with the ip address manipulation, not the ping_sock code.

Who is online

Users browsing this forum: No registered users and 200 guests