Skip to content

Commit

Permalink
fix malformed packet when depacketizing a tecmp message (#17)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: rawia.moalla <rawia.moalla}technica-engineering.de>
  • Loading branch information
rawiamoalla authored Oct 30, 2023
1 parent fe47ffc commit b6bd0f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 70 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ captured Ethernet frames and status messages.

```sh
# Conan is only needed to build the sample app, it's not needed for the library itself
pip install conan
conan remote add public-conan https://api.bintray.com/conan/bincrafters/public-conan

conan install conanfile.txt
# or if building Debug
conan install -s build_type=Debug conanfile.txt
pip install conan==1.61.0

# Build CMAKE
mkdir build
cd build
pip install cmake
cmake ..
# Build release
cmake --build . --config Release
```

# License
Expand Down
73 changes: 9 additions & 64 deletions apps/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,6 @@
using namespace pcpp;
using namespace std;


void print_status(tecmp_header header, uint8_t* data) {
switch (header.message_type)
{
case TECMP_TYPE_CM_STATUS:
{
tecmp_cm_status cm_status;
tecmp_get_cm_status(header, data, &cm_status);
auto v = cm_status.vendor;
printf("CM %u:\n", cm_status.cm_id);
printf("\t Version: v%u.%u.%u\n", v.sw_version_major, v.sw_version_minor, v.sw_version_patch);
break;
}

case TECMP_TYPE_BUS_STATUS:
{
int32_t iterator = 0;
tecmp_bus_status bus_status;
while (!tecmp_next_bus_status(header, data, &iterator, &bus_status)) {
printf("CM %u: %x\n", bus_status.cm_id, bus_status.channel_id);
printf("\t Messages Total: %u\n", bus_status.messages_total);
printf("\t Errors Total: %u\n", bus_status.errors_total);
printf("\t Link Status: %u\n", bus_status.vendor.link_status);
printf("\t Link Quality: %u\n", bus_status.vendor.link_quality);

auto linkup_time = bus_status.vendor.linkup_time;
if (linkup_time == 0) {
printf("\t Linkup Time: no linkup detected yet\n");
}
else if (linkup_time == 0xffff) {
printf("\t Linkup Time: no linkup detected and timeout occurred\n");
}
else {
printf("\t Linkup Time: %ums\n", bus_status.vendor.linkup_time);
}
}
break;
}
}
}

/*
* Sample app to write packetized TECMP into depacketized TECMP
* Also removes the TECMP header from Ethernet Data Frames
Expand Down Expand Up @@ -106,32 +65,19 @@ int main(int argc, char* argv[]) {

RawPacket newP;
timeval time = tecmp_get_timeval(header);
// Capture Module MTU = 1600
uint8_t buffer[1600];

if (header.data_type == TECMP_DATA_ETHERNET)
{
// Ethernet Frame, write it back to the PCAP file without TECMP header
newP = RawPacket(data, header.length, time, false);
}
else
{
// Print Status to stdout
print_status(header, data);

// Write back the frame depacketized
// 14 Ethernet header + TECMP header
const uint16_t head_size = 14 + 12;
memcpy(buffer, p.getRawData(), head_size);

// Capture Module MTU = 1600
uint8_t buffer[1600];
// Copy the payload header and data
const uint16_t hdr_size = 16;
memcpy(buffer + head_size, data - hdr_size, header.length + hdr_size);

// 14 Ethernet header + TECMP header
const uint16_t head_size = 14 + sizeof(tecmp_header);
memcpy(buffer, p.getRawData(), head_size);

// Copy the data header and data back
memcpy(buffer + head_size, data, header.length);


newP = RawPacket(buffer, header.length + head_size, time, false);
}
newP = RawPacket(buffer, header.length + head_size + hdr_size, time, false);

// Write packet back
writer.writePacket(newP);
Expand All @@ -140,7 +86,6 @@ int main(int argc, char* argv[]) {
res = tecmp_next(p.getRawData(), p.getRawDataLen(), &iterator, &header, &data);
}
}
}

reader->close();
writer.close();
Expand Down

0 comments on commit b6bd0f3

Please sign in to comment.