-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from bmorcelli/main
PingSweep
- Loading branch information
Showing
5 changed files
with
72 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
{ | ||
"idf.portWin": "COM3" | ||
"idf.portWin": "COM4", | ||
"files.associations": { | ||
"iosfwd": "cpp", | ||
"iostream": "cpp" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,142 +1,71 @@ | ||
/* 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_event.h" | ||
#include "nvs_flash.h" | ||
#include "argtable3/argtable3.h" | ||
//#include "protocol_examples_common.h" | ||
#include "ping/ping_sock.h" | ||
#include "display.h" | ||
#include "globals.h" | ||
#include "scan_hosts.h" | ||
#include "display.h" | ||
#include "mykeyboard.h" | ||
#include "wifi_common.h" | ||
|
||
//#define IPADDR_NONE ((uint32_t)0xffffffffUL) | ||
|
||
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)); | ||
tft.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); | ||
} | ||
|
||
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)); | ||
tft.printf("From %s icmp_seq=%d timeout\n",ipaddr_ntoa((ip_addr_t*)&target_addr), seqno); | ||
void logPingResult(IPAddress host, bool responded) { | ||
char buffer[64]; // Tamanho do buffer ajustável conforme necessário | ||
if (responded) { | ||
sprintf(buffer, "Host %s respondeu ao ping.", host.toString().c_str()); | ||
} else { | ||
sprintf(buffer, "Host %s não respondeu ao ping.", host.toString().c_str()); | ||
} | ||
log_d("%s",buffer); | ||
} | ||
|
||
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; | ||
bool pingHost(IPAddress host) { | ||
int count = Ping.ping(host, 1); // Ping com 1 tentativa | ||
bool responded = (count > 0); | ||
logPingResult(host, responded); | ||
return responded; | ||
} | ||
|
||
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)); | ||
void local_scan_setup() { | ||
if(!wifiConnected) wifiConnectMenu(); | ||
|
||
IPAddress gatewayIP; | ||
IPAddress subnetMask; | ||
std::vector<IPAddress> responderam; | ||
drawMainBorder(); | ||
tft.setTextSize(FP); | ||
tft.setCursor(8,30); | ||
|
||
gatewayIP = WiFi.gatewayIP(); | ||
subnetMask = WiFi.subnetMask(); | ||
|
||
IPAddress network = WiFi.localIP(); | ||
network[3] = 0; // Define o endereço de rede para o primeiro host | ||
|
||
int numHosts = 254 - subnetMask[3]; // Calcula o número de hosts possíveis na rede | ||
tft.println("Probing " + String(numHosts) + " hosts (" + String(numHosts/40 + 1).substring(0,4) + " lines)"); | ||
tft.fillRect(0,38,WIDTH,LH*(numHosts/40 + 1), BGCOLOR); | ||
tft.drawRect(0,38,WIDTH,LH*(numHosts/40 + 1), FGCOLOR); | ||
tft.fillRect(6,38,WIDTH-12,LH*(numHosts/40 + 1), BGCOLOR); | ||
|
||
for (int i = 1; i <= numHosts; i++) { | ||
IPAddress currentIP = network; | ||
currentIP[3] = i; | ||
|
||
if (pingHost(currentIP)) { | ||
tft.print("x"); | ||
responderam.push_back(currentIP); | ||
} else tft.print("."); | ||
|
||
if(checkEscPress()) i=256; //ends for loop | ||
|
||
if (transmitted > 0) { | ||
loss = (uint32_t)((1 - ((float)received) / transmitted) * 100); | ||
} else { | ||
loss = 0; | ||
} | ||
if (IP_IS_V4(&target_addr)) { | ||
//tft.printf("\n--- %s ping statistics ---\n", inet_ntoa(*ip_2_ip4(&target_addr))); | ||
} else { | ||
//tft.printf("\n--- %s ping statistics ---\n", inet6_ntoa(*ip_2_ip6(&target_addr))); | ||
options = {}; | ||
log_d("Hosts que responderam ao ping:"); | ||
for (IPAddress ip : responderam) { | ||
String txt = "..." + String(ip[2]) + "." + String(ip[3]); | ||
options.push_back({ txt.c_str(), [=](){ displayInfo(ip.toString().c_str()); }}); | ||
} | ||
tft.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); | ||
} | ||
|
||
int do_ping_cmd() | ||
{ | ||
esp_ping_config_t config = ESP_PING_DEFAULT_CONFIG(); | ||
|
||
ip_addr_t target_addr; | ||
memset(&target_addr, 0, sizeof(target_addr)); | ||
|
||
|
||
esp_ping_handle_t ping; | ||
//esp_ping_new_session(&config, &cbs, &ping); | ||
esp_ping_start(ping); | ||
|
||
return 0; | ||
} | ||
|
||
|
||
|
||
static esp_console_repl_t *s_repl = NULL; | ||
|
||
/* handle 'quit' command */ | ||
int do_cmd_quit() | ||
{ | ||
tft.printf("ByeBye\r\n"); | ||
s_repl->del(s_repl); | ||
return 0; | ||
} | ||
|
||
esp_err_t register_quit() | ||
{ | ||
esp_console_cmd_t command = { | ||
.command = "quit", | ||
.help = "Quit REPL environment", | ||
// .func = &do_cmd_quit | ||
}; | ||
return esp_console_cmd_register(&command); | ||
} | ||
|
||
void local_scan_setup() | ||
{ | ||
ip_addr_t target_addr; | ||
esp_ping_config_t config = ESP_PING_DEFAULT_CONFIG(); | ||
esp_ping_callbacks_t cbs = { | ||
.on_ping_success = cmd_ping_on_ping_success, | ||
.on_ping_timeout = cmd_ping_on_ping_timeout, | ||
.on_ping_end = cmd_ping_on_ping_end, | ||
// .cb_args = NULL, | ||
}; | ||
|
||
for (int i = 1; i <= 254; i++) { | ||
char ip_addr_str[16]; | ||
tft.printf(ip_addr_str, "192.168.0.%d", i); | ||
ip_addr_t addr; | ||
ipaddr_aton(ip_addr_str, &addr); | ||
config.target_addr = addr; | ||
|
||
esp_ping_handle_t ping; | ||
memset(&target_addr, 0, sizeof(target_addr)); | ||
esp_ping_new_session(&config, &cbs, &ping); | ||
esp_ping_start(ping); | ||
} | ||
loopOptions(options); | ||
|
||
while(!checkEscPress()) yield(); | ||
|
||
} |
Oops, something went wrong.