创建客户端连接日志 - 如何更改上次连接的代码

发布于 2025-01-09 03:33:56 字数 2307 浏览 2 评论 0原文

这是在互联网上找到的标准例程 获取已连接客户端的 IP/MAC 地址。有用。 我不明白的是如何只获取最后一个访问的 MAC 以保留访问日志。 我不想每次有新连接时都重复每次连接。 我找不到 STAILQ_NEXT 上的文档,所以我不知道它在做什么。 帮助表示赞赏。

  unsigned char number_client;
  struct station_info *stat_info;
//  struct ip_addr *IPaddress;
  struct ip4_addr *IPaddress;
  IPAddress address;
  int i=1;
  number_client= wifi_softap_get_station_num();
  stat_info = wifi_softap_get_station_info();
  Serial.print(" Total Connected Clients are = ");
  Serial.println(number_client);
    while (stat_info != NULL) {
      IPaddress = &stat_info->ip;
      address = IPaddress->addr;
      Serial.print(stat_info->bssid[0],HEX);Serial.print(':');
      Serial.print(stat_info->bssid[1],HEX);Serial.print(':');
      Serial.print(stat_info->bssid[2],HEX);Serial.print(':');
      Serial.print(stat_info->bssid[3],HEX);Serial.print(':');
      Serial.print(stat_info->bssid[4],HEX);Serial.print(':');
      Serial.print(stat_info->bssid[5],HEX);Serial.print(' ');
      stat_info = STAILQ_NEXT(stat_info, next);
      i++;
      Serial.println();
    }
  delay(500);
}```



================================
Thanks Majenko. This gives me the MAC, but if I got the rest of the code correct, this is always giving me the last MAC to connect, rather than the last MAC to access the page. If there are 3 connected A, B, C and A refreshes, the log shows C. Not sure if the list if FIFO or LIFO or FILO or ??? or there's something else I am missing. Maybe I am looking at the wrong variable or ???

    ```void clientMAC(){
      struct station_info *scan = wifi_softap_get_station_info();
      byte MAClast[5];
      if (scan != NULL) { // Check there is at least one entry
        while (STAILQ_NEXT(scan, next) != NULL) {
            scan = STAILQ_NEXT(scan, next);
        }
        // scan now points to the last entry
          Serial.print(scan->bssid[0],HEX);Serial.print(':');
          Serial.print(scan->bssid[1],HEX);Serial.print(':');
          Serial.print(scan->bssid[2],HEX);Serial.print(':');
          Serial.print(scan->bssid[3],HEX);Serial.print(':');
          Serial.print(scan->bssid[4],HEX);Serial.print(':');
          Serial.print(scan->bssid[5],HEX);Serial.print(' ');
      }else { // We have no entries
     }
    }
    ```

This is the standard routine found all over the internet for
obtaining the IP/MAC address of an connected client. It works.
What I can't figure out is how to get only the last MAC to access to keep an access log.
I don't want a repeat of every connect, each time there's a new connect.
I can't find docs on STAILQ_NEXT, so I don't know what it is doing.
Help appreciated.

  unsigned char number_client;
  struct station_info *stat_info;
//  struct ip_addr *IPaddress;
  struct ip4_addr *IPaddress;
  IPAddress address;
  int i=1;
  number_client= wifi_softap_get_station_num();
  stat_info = wifi_softap_get_station_info();
  Serial.print(" Total Connected Clients are = ");
  Serial.println(number_client);
    while (stat_info != NULL) {
      IPaddress = &stat_info->ip;
      address = IPaddress->addr;
      Serial.print(stat_info->bssid[0],HEX);Serial.print(':');
      Serial.print(stat_info->bssid[1],HEX);Serial.print(':');
      Serial.print(stat_info->bssid[2],HEX);Serial.print(':');
      Serial.print(stat_info->bssid[3],HEX);Serial.print(':');
      Serial.print(stat_info->bssid[4],HEX);Serial.print(':');
      Serial.print(stat_info->bssid[5],HEX);Serial.print(' ');
      stat_info = STAILQ_NEXT(stat_info, next);
      i++;
      Serial.println();
    }
  delay(500);
}```



================================
Thanks Majenko. This gives me the MAC, but if I got the rest of the code correct, this is always giving me the last MAC to connect, rather than the last MAC to access the page. If there are 3 connected A, B, C and A refreshes, the log shows C. Not sure if the list if FIFO or LIFO or FILO or ??? or there's something else I am missing. Maybe I am looking at the wrong variable or ???

    ```void clientMAC(){
      struct station_info *scan = wifi_softap_get_station_info();
      byte MAClast[5];
      if (scan != NULL) { // Check there is at least one entry
        while (STAILQ_NEXT(scan, next) != NULL) {
            scan = STAILQ_NEXT(scan, next);
        }
        // scan now points to the last entry
          Serial.print(scan->bssid[0],HEX);Serial.print(':');
          Serial.print(scan->bssid[1],HEX);Serial.print(':');
          Serial.print(scan->bssid[2],HEX);Serial.print(':');
          Serial.print(scan->bssid[3],HEX);Serial.print(':');
          Serial.print(scan->bssid[4],HEX);Serial.print(':');
          Serial.print(scan->bssid[5],HEX);Serial.print(' ');
      }else { // We have no entries
     }
    }
    ```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彼岸花似海 2025-01-16 03:33:56

stat_info 是一个 struct station_info * 类型的简单链表,具有由 SDK 强加的预定义结构。以下是完整的实现链:

struct station_info {
    STAILQ_ENTRY(station_info)    next;

    uint8 bssid[6];
    struct ipv4_addr ip;
};

STAILQ_ENTRY(station_info) next 宏扩展为:

    struct {                                
        struct station_info *stqe_next; /* next element */          
    } next;

因此,您有一个基于 stat_info->next.stqe_next 的链表。

STAILQ_NEXT(stat_info, next) 只是将其包装在一些抽象中,以使迭代更容易。它扩展为 ((stat_info)->next.stqe_next

要获取链表的最后一个元素,您需要迭代列表,直到 ->next.stqe_next code> 为 null,此时您知道自己位于列表的末尾。

struct station_info *scan = wifi_softap_get_station_info();
if (scan != NULL) { // Check there is at least one entry
    while (STAILQ_NEXT(scan, next) != NULL) {
        scan = STAILQ_NEXT(scan, next);
    }
    // scan now points to the last entry
} else {
    // We have no entries
}

stat_info is a simple linked list of type struct station_info *, with a predefined structure imposed by the SDK. Here's the full chain of implementation:

struct station_info {
    STAILQ_ENTRY(station_info)    next;

    uint8 bssid[6];
    struct ipv4_addr ip;
};

The STAILQ_ENTRY(station_info) next macro expands to:

    struct {                                
        struct station_info *stqe_next; /* next element */          
    } next;

So you have a linked list that is based around stat_info->next.stqe_next.

The macro STAILQ_NEXT(stat_info, next) just wraps that in a bit of abstraction to make iterating easier. It expands out to ((stat_info)->next.stqe_next.

To get the last element of a linked list you need to iterate through the list until ->next.stqe_next is null, at which point you know you are at the end of the list. For example:

struct station_info *scan = wifi_softap_get_station_info();
if (scan != NULL) { // Check there is at least one entry
    while (STAILQ_NEXT(scan, next) != NULL) {
        scan = STAILQ_NEXT(scan, next);
    }
    // scan now points to the last entry
} else {
    // We have no entries
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文