Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <lilka.h>
- #include "ipapp.h"
- #include "servicemanager.h"
- #include "services/network.h"
- extern "C" {
- #include "lwip/etharp.h"
- #include "lwip/netif.h"
- #include "lwip/ip_addr.h"
- }
- IPApp::IPApp() : App("IP Address") {}
- void IPApp::run() {
- struct netif* netif = netif_list; // Зазвичай перший — ваш WiFi STA
- // Визначаємо свою підмережу
- IPAddress localIP = WiFi.localIP();
- uint8_t subnet[4] = { localIP[0], localIP[1], localIP[2], 0 };
- // 1. Відправляємо ARP-запити до всіх адрес у підмережі
- for (int i = 1; i <= 254; ++i) {
- ip4_addr_t target_ip;
- subnet[3] = i;
- IPAddress ip(subnet[0], subnet[1], subnet[2], subnet[3]);
- ip4addr_aton(ip.toString().c_str(), &target_ip);
- etharp_request(netif, &target_ip);
- vTaskDelay(10 / portTICK_PERIOD_MS); // невелика затримка між запитами
- }
- // 2. Чекаємо ~1 секунду для отримання відповідей
- vTaskDelay(1000 / portTICK_PERIOD_MS);
- // 3. Збираємо знайдені IP–MAC
- String info = "ARP scan results:\n";
- for (int i = 1; i <= 254; ++i) {
- subnet[3] = i;
- IPAddress ip(subnet[0], subnet[1], subnet[2], subnet[3]);
- ip4_addr_t target_ip;
- ip4addr_aton(ip.toString().c_str(), &target_ip);
- struct eth_addr *eth_ret;
- const ip4_addr_t *ip_ret;
- if (etharp_find_addr(netif, &target_ip, ð_ret, &ip_ret) >= 0) {
- char macbuf[18];
- snprintf(macbuf, sizeof(macbuf), "%02X:%02X:%02X:%02X:%02X:%02X",
- eth_ret->addr[0], eth_ret->addr[1], eth_ret->addr[2],
- eth_ret->addr[3], eth_ret->addr[4], eth_ret->addr[5]);
- info += ip.toString() + " - " + String(macbuf) + "\n";
- }
- }
- if (info == "ARP scan results:\n") {
- info += "No devices found.";
- }
- // 4. Виводимо результат
- lilka::Alert alert("Мережа", info);
- alert.draw(canvas);
- queueDraw();
- while (!alert.isFinished()) {
- alert.update();
- vTaskDelay(LILKA_UI_UPDATE_DELAY_MS / portTICK_PERIOD_MS);
- lilka::State state = lilka::controller.getState();
- if (state.b.pressed) {
- return;
- }
- }
- }
- #ifndef IPAPP_H
- #define IPAPP_H
- #include <lilka.h>
- #include "app.h"
- #include "services/network.h"
- class IPApp : public App {
- public:
- IPApp();
- private:
- void run() override;
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement