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

时间:2019-02-04 13:42:59

标签: capl

我是编程界的新手,我要在这里尝试将传入的以太网消息的MAC地址(数据类型为“ QWord”)存储到字符串中,然后最终对字符串进行比较。

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

我在以下几点寻求帮助:

    如何将MAC地址存储为QWord或字符串。
    如何将QWord与另一个QWord或字符串进行比较。
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);
}

enter image description here

enter image description here

先谢谢您

1 个答案:

答案 0 :(得分:2)

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

您可以使用CAPL函数qword

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

qword转换为字符串可以使用EthGetMacAddressAsString

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

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

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