Search code examples
capl

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


I am new in programming world, What I am trying to here is to store the MAC address of incoming Ethernet message, which is of Data type 'QWord' to a string, and then finally compare the string.

Below is my code, here snprintf corresponds to the C function sprintf

I am looking for help in the following points:

    How to store MAC address as QWord or as a string.
    How to compare QWord with another QWord or with a string.
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

Thankyou in advance


Solution

  • A qword is simply a 64-bit integer that can be compared with the standard == operator.

    You can convert a string containing a MAC address to a qword by using the CAPL function EthGetMacAddressAsNumber

    Converting from a qword to string can be done with EthGetMacAddressAsString

    In your case the code would look roughly as follows:

    char compareMacStr = "AA::BB::CC::00::FF::EE";
    qword compareMac = EthGetMacAddressAsNumber(compareMacStr);
    
    if(this.destination == compareMac)
    {
        ....
    }