如何将 ARP 绑定从文件获取到数组中

发布于 2024-11-14 09:26:06 字数 904 浏览 4 评论 0原文

我正在尝试将 Linux 中的 ARP 表获取到一个数组,代码如下。我总是在变量 ip 和 mac 中获取地址,但是当分配给数组时,它只是显示一些疯狂的数字。我做错了什么吗? (我不太擅长编程)

struct ARP_entry 
{
  char IPaddr;
  char MACaddr;
  char ARPstatus;
  int timec;
};

static struct ARP_entry ARP_table[ARP_table_vel];


void getARP()
{
  int i=0;
  const char filename[] = "/proc/net/arp";
  char ip[16], mac[18], output[128];
  FILE *file = fopen(filename, "r");
  if ( file )
  {
    char line [ BUFSIZ ];
    fgets(line, sizeof line, file);
    while ( fgets(line, sizeof line, file) )
    {
      char  a,b,c,d;
      if ( sscanf(line, "%s %s %s %s %s %s", &ip, &a, &b, &mac, &c, &d) < 10 )
        {
    if ( ARP_table_vel > i)
    {
      ARP_table[i].IPaddr = ip;
      ARP_table[i].MACaddr = mac;
          ARP_table[i].ARPstatus = STATUS_CON;
      i++;
    }
        }
    }
  }
  else
  {
    perror(filename);
  }

I am trying to get the ARP table in Linux to an array with code posted below. I always get the addresses in the variables ip and mac, but when assigning to the array, it just shows some crazy numbers. Am I doing sth wrong? (I am not really skilled in programming)

struct ARP_entry 
{
  char IPaddr;
  char MACaddr;
  char ARPstatus;
  int timec;
};

static struct ARP_entry ARP_table[ARP_table_vel];


void getARP()
{
  int i=0;
  const char filename[] = "/proc/net/arp";
  char ip[16], mac[18], output[128];
  FILE *file = fopen(filename, "r");
  if ( file )
  {
    char line [ BUFSIZ ];
    fgets(line, sizeof line, file);
    while ( fgets(line, sizeof line, file) )
    {
      char  a,b,c,d;
      if ( sscanf(line, "%s %s %s %s %s %s", &ip, &a, &b, &mac, &c, &d) < 10 )
        {
    if ( ARP_table_vel > i)
    {
      ARP_table[i].IPaddr = ip;
      ARP_table[i].MACaddr = mac;
          ARP_table[i].ARPstatus = STATUS_CON;
      i++;
    }
        }
    }
  }
  else
  {
    perror(filename);
  }

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

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

发布评论

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

评论(1

怀中猫帐中妖 2024-11-21 09:26:06

您需要修复您的结构并将 char 变量放入 char 数组:

struct ARP_entry 
{
  char IPaddr[16];
  char MACaddr[18];
  char ARPstatus;
  int timec;
};

然后您需要对数据进行正确的复制,以便可以保留它们:

if ( ARP_table_vel > i)
    {
      snprintf(ARP_table[i].IPaddr, 16, "%s", ip);
      snprintf(ARP_table[i].MACaddr, 18, "%s", mac);
      ARP_table[i].ARPstatus = STATUS_CON;
      i++;
    }

最后,ARP 表有标题,因此您需要丢弃第一行。

You need to fix your structure and make the char variables into char arrays:

struct ARP_entry 
{
  char IPaddr[16];
  char MACaddr[18];
  char ARPstatus;
  int timec;
};

Then you need to do proper copies of the data so you can preserve them:

if ( ARP_table_vel > i)
    {
      snprintf(ARP_table[i].IPaddr, 16, "%s", ip);
      snprintf(ARP_table[i].MACaddr, 18, "%s", mac);
      ARP_table[i].ARPstatus = STATUS_CON;
      i++;
    }

Finally, the ARP table has a header, so you'll need to discard the first row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文