Advertisement
nene1234

arp scan menu work

Jul 3rd, 2025
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 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::scanNetwork(std::vector<String>& results) {
  15.     struct netif* netif = netif_list;
  16.     IPAddress localIP = WiFi.localIP();
  17.     uint8_t subnet[4] = { localIP[0], localIP[1], localIP[2], 0 };
  18.  
  19.     // Відправляємо ARP-запити до всіх адрес у підмережі
  20.     for (int i = 1; i <= 254; ++i) {
  21.         ip4_addr_t target_ip;
  22.         subnet[3] = i;
  23.         IPAddress ip(subnet[0], subnet[1], subnet[2], subnet[3]);
  24.         ip4addr_aton(ip.toString().c_str(), &target_ip);
  25.         etharp_request(netif, &target_ip);
  26.         vTaskDelay(10 / portTICK_PERIOD_MS);
  27.     }
  28.     vTaskDelay(1000 / portTICK_PERIOD_MS);
  29.  
  30.     // Збираємо знайдені IP–MAC
  31.     for (int i = 1; i <= 254; ++i) {
  32.         subnet[3] = i;
  33.         IPAddress ip(subnet[0], subnet[1], subnet[2], subnet[3]);
  34.         ip4_addr_t target_ip;
  35.         ip4addr_aton(ip.toString().c_str(), &target_ip);
  36.         struct eth_addr *eth_ret;
  37.         const ip4_addr_t *ip_ret;
  38.         if (etharp_find_addr(netif, &target_ip, &eth_ret, &ip_ret) >= 0) {
  39.             char macbuf[18];
  40.             snprintf(macbuf, sizeof(macbuf), "%02X:%02X:%02X:%02X:%02X:%02X",
  41.                 eth_ret->addr[0], eth_ret->addr[1], eth_ret->addr[2],
  42.                 eth_ret->addr[3], eth_ret->addr[4], eth_ret->addr[5]);
  43.             results.push_back(ip.toString() + " - " + String(macbuf));
  44.         }
  45.     }
  46. }
  47.  
  48. void IPApp::run() {
  49.     std::vector<String> arpResults;
  50.     scanNetwork(arpResults);
  51.  
  52.     lilka::Menu menu("ARP scan");
  53.     menu.addActivationButton(lilka::Button::B); // Back
  54.     if (arpResults.empty()) {
  55.         menu.addItem("No devices found");
  56.     } else {
  57.         for (const auto& entry : arpResults) {
  58.             menu.addItem(entry);
  59.         }
  60.     }
  61.     menu.addItem("Назад");
  62.     int count = (int)arpResults.size() + 1;
  63.     if (arpResults.empty()) count = 2;
  64.  
  65.     while (true) {
  66.         while (!menu.isFinished()) {
  67.             menu.update();
  68.             menu.draw(canvas);
  69.             queueDraw();
  70.         }
  71.         int cursor = menu.getCursor();
  72.         if (cursor == count - 1 || menu.getButton() == lilka::Button::B) {
  73.             return;
  74.         }
  75.         // Якщо вибрано "No devices found" — просто оновити меню (нічого не робимо)
  76.     }
  77. }
  78.  
  79.  
  80.  
  81. #ifndef IPAPP_H
  82. #define IPAPP_H
  83.  
  84. #include <lilka.h>
  85. #include "app.h"
  86. #include "services/network.h"
  87. #include <vector>
  88.  
  89. class IPApp : public App {
  90. public:
  91.     IPApp();
  92. private:
  93.     void run() override;
  94.     void scanNetwork(std::vector<String>& results);
  95.     lilka::Menu menu;
  96. };
  97.  
  98. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement