Skip to content

Commit b6bd0f3

Browse files
authored
fix malformed packet when depacketizing a tecmp message (#17)
--------- Co-authored-by: rawia.moalla <rawia.moalla}technica-engineering.de>
1 parent fe47ffc commit b6bd0f3

File tree

2 files changed

+13
-70
lines changed

2 files changed

+13
-70
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@ captured Ethernet frames and status messages.
1010

1111
```sh
1212
# Conan is only needed to build the sample app, it's not needed for the library itself
13-
pip install conan
14-
conan remote add public-conan https://api.bintray.com/conan/bincrafters/public-conan
15-
16-
conan install conanfile.txt
17-
# or if building Debug
18-
conan install -s build_type=Debug conanfile.txt
13+
pip install conan==1.61.0
1914

2015
# Build CMAKE
2116
mkdir build
2217
cd build
18+
pip install cmake
2319
cmake ..
20+
# Build release
21+
cmake --build . --config Release
2422
```
2523

2624
# License

apps/app.cpp

Lines changed: 9 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,6 @@
1212
using namespace pcpp;
1313
using namespace std;
1414

15-
16-
void print_status(tecmp_header header, uint8_t* data) {
17-
switch (header.message_type)
18-
{
19-
case TECMP_TYPE_CM_STATUS:
20-
{
21-
tecmp_cm_status cm_status;
22-
tecmp_get_cm_status(header, data, &cm_status);
23-
auto v = cm_status.vendor;
24-
printf("CM %u:\n", cm_status.cm_id);
25-
printf("\t Version: v%u.%u.%u\n", v.sw_version_major, v.sw_version_minor, v.sw_version_patch);
26-
break;
27-
}
28-
29-
case TECMP_TYPE_BUS_STATUS:
30-
{
31-
int32_t iterator = 0;
32-
tecmp_bus_status bus_status;
33-
while (!tecmp_next_bus_status(header, data, &iterator, &bus_status)) {
34-
printf("CM %u: %x\n", bus_status.cm_id, bus_status.channel_id);
35-
printf("\t Messages Total: %u\n", bus_status.messages_total);
36-
printf("\t Errors Total: %u\n", bus_status.errors_total);
37-
printf("\t Link Status: %u\n", bus_status.vendor.link_status);
38-
printf("\t Link Quality: %u\n", bus_status.vendor.link_quality);
39-
40-
auto linkup_time = bus_status.vendor.linkup_time;
41-
if (linkup_time == 0) {
42-
printf("\t Linkup Time: no linkup detected yet\n");
43-
}
44-
else if (linkup_time == 0xffff) {
45-
printf("\t Linkup Time: no linkup detected and timeout occurred\n");
46-
}
47-
else {
48-
printf("\t Linkup Time: %ums\n", bus_status.vendor.linkup_time);
49-
}
50-
}
51-
break;
52-
}
53-
}
54-
}
55-
5615
/*
5716
* Sample app to write packetized TECMP into depacketized TECMP
5817
* Also removes the TECMP header from Ethernet Data Frames
@@ -106,32 +65,19 @@ int main(int argc, char* argv[]) {
10665

10766
RawPacket newP;
10867
timeval time = tecmp_get_timeval(header);
68+
// Capture Module MTU = 1600
69+
uint8_t buffer[1600];
10970

110-
if (header.data_type == TECMP_DATA_ETHERNET)
111-
{
112-
// Ethernet Frame, write it back to the PCAP file without TECMP header
113-
newP = RawPacket(data, header.length, time, false);
114-
}
115-
else
116-
{
117-
// Print Status to stdout
118-
print_status(header, data);
119-
120-
// Write back the frame depacketized
71+
// 14 Ethernet header + TECMP header
72+
const uint16_t head_size = 14 + 12;
73+
memcpy(buffer, p.getRawData(), head_size);
12174

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

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

129-
// Copy the data header and data back
130-
memcpy(buffer + head_size, data, header.length);
131-
132-
133-
newP = RawPacket(buffer, header.length + head_size, time, false);
134-
}
80+
newP = RawPacket(buffer, header.length + head_size + hdr_size, time, false);
13581

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

14590
reader->close();
14691
writer.close();

0 commit comments

Comments
 (0)