[CAPL]: 比较 QWord 和字符串,或者如何将 MAC 地址存储为 QWord 以与另一个 QWord 进行比较?

[CAPL]: Compare QWord with string, or How to store MAC address as QWord to compare with another QWord?

我是编程界的新手,我在这里尝试的是将数据类型为 'QWord' 的传入以太网消息的 MAC 地址存储到字符串中,最后比较字符串。

下面是我的代码,这里snprintf对应C函数sprintf

我正在寻求以下方面的帮助:

on ethernetPacket *
{
  byte Data[1506];
  int i;
  int Payloadlength;

  char DestinationmacStr[18];
  char SourcemacStr[18];
  char ComparemacStr[18];
  char macStr[18];

  // Store a MAC address to compare with the MAC Address of the incoming ETH message
  int array[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  snprintf(ComparemacStr, elCount(ComparemacStr), "%02x:%02x:%02x:%02x:%02x:%02x",
         array[0], array[1], array[2], array[3], array[4], array[5]);

  Payloadlength=this.Length;

  for(i=0; i<Payloadlength; i++)
  {
    Data[i]=this.byte(i);
  }

  // How to store the Source MAC Address of Source (QWord to string)?
  // Error message when compiling at "this.Source[0]" => no array possible here
  snprintf(SourcemacStr, elCount(SourcemacStr), "%02x:%02x:%02x:%02x:%02x:%02x",
         this.Source[0], this.Source[1], this.Source[2], this.Source[3], this.Source[4], this.Source[5]);

  // How to store the Destination MAC Address of Source (QWord to string)?
  // Error message when compiling at "this.destination[0]" => no array possible here
  snprintf(DestinationmacStr, elCount(DestinationmacStr), "%02x:%02x:%02x:%02x:%02x:%02x",
         this.destination[0], this.destination[1], this.destination[2], this.destination[3], this.destination[4], this.destination[5]);

  write("Source MAC Address: %s",SourcemacStr);
  write("Destination MAC Address: %s",DestinationmacStr);

  if(DestinationmacStr==ComparemacStr)
  {
   // do something
  }
  outputMostEthPkt(1, this.destination, this.length, Data);
}

提前致谢

A qword 只是一个 64 位整数,可以与标准 == 运算符进行比较。

您可以使用 CAPL 函数 EthGetMacAddressAsNumber

将包含 MAC 地址的字符串转换为 qword

可以使用 EthGetMacAddressAsString

qword 转换为字符串

在您的情况下,代码大致如下所示:

char compareMacStr = "AA::BB::CC::00::FF::EE";
qword compareMac = EthGetMacAddressAsNumber(compareMacStr);

if(this.destination == compareMac)
{
    ....
}