Advertisement
nene1234

all network work

Jul 3rd, 2025
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include <lilka.h>
  2. #include "ipapp.h"
  3. #include "servicemanager.h"
  4. #include "services/network.h"
  5.  
  6. extern "C" {
  7. #include "lwip/etharp.h"
  8. #include "lwip/netif.h"
  9. #include "lwip/ip_addr.h"
  10. }
  11.  
  12. IPApp::IPApp() : App("IP Address") {}
  13.  
  14. void IPApp::run() {
  15.     struct netif* netif = netif_list; // Зазвичай перший — ваш WiFi STA
  16.  
  17.     // Визначаємо свою підмережу
  18.     IPAddress localIP = WiFi.localIP();
  19.     uint8_t subnet[4] = { localIP[0], localIP[1], localIP[2], 0 };
  20.  
  21.     // 1. Відправляємо ARP-запити до всіх адрес у підмережі
  22.     for (int i = 1; i <= 254; ++i) {
  23.         ip4_addr_t target_ip;
  24.         subnet[3] = i;
  25.         IPAddress ip(subnet[0], subnet[1], subnet[2], subnet[3]);
  26.         ip4addr_aton(ip.toString().c_str(), &target_ip);
  27.         etharp_request(netif, &target_ip);
  28.         vTaskDelay(10 / portTICK_PERIOD_MS); // невелика затримка між запитами
  29.     }
  30.  
  31.     // 2. Чекаємо ~1 секунду для отримання відповідей
  32.     vTaskDelay(1000 / portTICK_PERIOD_MS);
  33.  
  34.     // 3. Збираємо знайдені IP–MAC
  35.     String info = "ARP scan results:\n";
  36.     for (int i = 1; i <= 254; ++i) {
  37.         subnet[3] = i;
  38.         IPAddress ip(subnet[0], subnet[1], subnet[2], subnet[3]);
  39.         ip4_addr_t target_ip;
  40.         ip4addr_aton(ip.toString().c_str(), &target_ip);
  41.         struct eth_addr *eth_ret;
  42.         const ip4_addr_t *ip_ret;
  43.         if (etharp_find_addr(netif, &target_ip, &eth_ret, &ip_ret) >= 0) {
  44.             char macbuf[18];
  45.             snprintf(macbuf, sizeof(macbuf), "%02X:%02X:%02X:%02X:%02X:%02X",
  46.                 eth_ret->addr[0], eth_ret->addr[1], eth_ret->addr[2],
  47.                 eth_ret->addr[3], eth_ret->addr[4], eth_ret->addr[5]);
  48.             info += ip.toString() + " - " + String(macbuf) + "\n";
  49.         }
  50.     }
  51.     if (info == "ARP scan results:\n") {
  52.         info += "No devices found.";
  53.     }
  54.  
  55.     // 4. Виводимо результат
  56.     lilka::Alert alert("Мережа", info);
  57.     alert.draw(canvas);
  58.     queueDraw();
  59.     while (!alert.isFinished()) {
  60.         alert.update();
  61.         vTaskDelay(LILKA_UI_UPDATE_DELAY_MS / portTICK_PERIOD_MS);
  62.         lilka::State state = lilka::controller.getState();
  63.         if (state.b.pressed) {
  64.             return;
  65.         }
  66.     }
  67. }
  68.  
  69.  
  70. #ifndef IPAPP_H
  71. #define IPAPP_H
  72.  
  73. #include <lilka.h>
  74. #include "app.h"
  75. #include "services/network.h"
  76.  
  77. class IPApp : public App {
  78. public:
  79.     IPApp();
  80. private:
  81.     void run() override;
  82. };
  83.  
  84. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement