PcapPlusPlus icon indicating copy to clipboard operation
PcapPlusPlus copied to clipboard

#1754 Add Modbus Support

Open yahyayozo opened this issue 6 months ago • 8 comments

  • Add ModbusLayer first implementation with header parsing only (no test included)

yahyayozo avatar May 18 '25 18:05 yahyayozo

Codecov Report

:x: Patch coverage is 91.86992% with 10 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 83.47%. Comparing base (699f7b0) to head (e3461d0). :warning: Report is 6 commits behind head on dev.

Files with missing lines Patch % Lines
Packet++/src/ModbusLayer.cpp 88.05% 8 Missing :warning:
Packet++/header/ModbusLayer.h 90.00% 2 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1823      +/-   ##
==========================================
- Coverage   83.47%   83.47%   -0.01%     
==========================================
  Files         298      304       +6     
  Lines       53945    54152     +207     
  Branches    11705    12013     +308     
==========================================
+ Hits        45029    45201     +172     
- Misses       7709     7735      +26     
- Partials     1207     1216       +9     
Flag Coverage Δ
alpine320 75.45% <93.33%> (+0.02%) :arrow_up:
fedora42 75.60% <93.44%> (+0.06%) :arrow_up:
macos-13 81.67% <89.10%> (+<0.01%) :arrow_up:
macos-14 81.67% <89.10%> (+<0.01%) :arrow_up:
macos-15 81.67% <89.10%> (+<0.01%) :arrow_up:
mingw32 70.29% <78.72%> (-0.01%) :arrow_down:
mingw64 70.27% <78.72%> (-0.02%) :arrow_down:
rhel94 75.30% <93.44%> (+0.02%) :arrow_up:
ubuntu2004 59.24% <76.56%> (+0.03%) :arrow_up:
ubuntu2004-zstd 59.36% <76.56%> (+0.05%) :arrow_up:
ubuntu2204 75.23% <93.44%> (-0.01%) :arrow_down:
ubuntu2204-icpx 60.93% <70.58%> (+0.04%) :arrow_up:
ubuntu2404 75.50% <93.44%> (+0.03%) :arrow_up:
ubuntu2404-arm64 75.47% <93.44%> (+0.02%) :arrow_up:
unittest 83.47% <91.86%> (-0.01%) :arrow_down:
windows-2022 85.49% <86.95%> (+0.01%) :arrow_up:
windows-2025 85.51% <86.95%> (+0.01%) :arrow_up:
winpcap 85.51% <86.95%> (+0.01%) :arrow_up:
xdp 53.09% <93.44%> (+0.12%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar May 18 '25 19:05 codecov[bot]

@seladb I've prepared only 1 constructor which initiates the layer with default fields values Can you please mention what are the other constructors I need to implement? also please mention if I need to follow on other protocols layers?

yahyayozo avatar May 21 '25 20:05 yahyayozo

@yahyayozo we usually have at least 2 c'tors, sometimes more:

  1. As part of the packet parsing flow. For example: https://github.com/seladb/PcapPlusPlus/blob/a49a79e0b67b402ad75ffa96c1795def36df75c8/Packet%2B%2B/header/MplsLayer.h#L37 which is used here (and in some other places also): https://github.com/seladb/PcapPlusPlus/blob/a49a79e0b67b402ad75ffa96c1795def36df75c8/Packet%2B%2B/src/VlanLayer.cpp#L101
  2. When the user wants to create a new layer of this type. This is the c'tor you already implemented, but usually we don't implement (only) c'tors with zero arguments. We tend to implement c'tors that allow the user to build the layer. In your case, I'd add arguments like transactionId, unitId and functionCode

seladb avatar May 22 '25 06:05 seladb

Hi @seladb can you please guide me briefly on how to add tests for my ModbusLayer for parsing/crafting headers? As I want to pass to function types request/response after testing the header part.

yahyayozo avatar May 24 '25 10:05 yahyayozo

@yahyayozo sure! You can learn about tests here: https://pcapplusplus.github.io/docs/tests

At a high level:

  • You should add your tests under Tests/Packet++Test/Tests (don't forget to add your .cpp file to CMakeFiles.txt)
  • The tests use a homegrown testing framework. A test case is PTF_TEST_CASE. You can write 3 of those: for parsing, crafting, and editing
  • You can find the possible assertions here, the most common one is PTF_ASSERT_EQUAL(actual, expected)
  • You need to add your test cases to https://github.com/seladb/PcapPlusPlus/blob/master/Tests/Packet%2B%2BTest/main.cpp and add a PTF_RUN_TEST for each test case
  • When you build the tests, you should run them from the Tests/Packet++Test directory, like this: Bin/Packet++Test -t <YOUR_TEST_CASE>

Please look at similar tests that were written for other layers and follow the same patterns.

Please let me know if you have any questions.

seladb avatar May 25 '25 07:05 seladb

@seladb I'll add tests for the headers crafting/parsing implementation and then you can a review before we move to adding the different PDU types

yahyayozo avatar May 28 '25 21:05 yahyayozo

@seladb @Dimi1010 I've started adding some tests, but I have a question regarding the ".dat" files; how can I generate examples for some Modbus frames?

yahyayozo avatar Jun 02 '25 20:06 yahyayozo

@seladb @Dimi1010 I've started adding some tests, but I have a question regarding the ".dat" files; how can I generate examples for some Modbus frames?

If you open the file in Wireshark, you can choose a packet and right click -> Copy -> ... as a Hex Stream:

image

seladb avatar Jun 02 '25 21:06 seladb

@yahyayozo CI fails, can you please look into it? 🙁

seladb avatar Jun 12 '25 03:06 seladb

@yahyayozo CI fails, can you please look into it? 🙁

yahyayozo avatar Jun 12 '25 18:06 yahyayozo

@seladb I'll check further

yahyayozo avatar Jun 12 '25 18:06 yahyayozo

@seladb all the pre-commit checks have passed + the build was successful I think if the pipeline build fails, I need to check my compiler, maybe I'm using a wrong version

yahyayozo avatar Jun 15 '25 19:06 yahyayozo

@seladb CI passed! can you please make a review of the already done work before moving forward?

yahyayozo avatar Jun 20 '25 21:06 yahyayozo

@seladb Hi, can you please check the progress I've reached until now? please leave comments on any part that needs adjustement

yahyayozo avatar Jul 19 '25 20:07 yahyayozo

@seladb Hi, can you please check the progress I've reached until now? please leave comments on any part that needs adjustement

@yahyayozo sure, I'm sorry for the long delay. I'll take a look shortly

seladb avatar Jul 19 '25 20:07 seladb

@seladb pcap files are ignored, in the gitignore, how can I commit my modbus.pcap file?

yahyayozo avatar Jul 30 '25 23:07 yahyayozo

@seladb pcap files are ignored, in the gitignore, how can I commit my modbus.pcap file?

@yahyayozo you can use git add -f modbus.pcap to force adding a pcap file despite of .gitignore

seladb avatar Jul 31 '25 03:07 seladb

@seladb I've made the changes of your last review, and the pipeline is working I've also included the pcap file for Modbus Can you please review again?

yahyayozo avatar Aug 08 '25 20:08 yahyayozo

@yahyayozo please scroll up and see all the comments that weren't addressed

seladb avatar Aug 10 '25 08:08 seladb

@yahyayozo the tests fail in CI...

seladb avatar Aug 12 '25 02:08 seladb

@seladb should be fixed now

yahyayozo avatar Aug 12 '25 22:08 yahyayozo

@yahyayozo please also see older comments:

  • https://github.com/seladb/PcapPlusPlus/pull/1823#discussion_r2265164208
  • https://github.com/seladb/PcapPlusPlus/pull/1823#discussion_r2278509145
  • https://github.com/seladb/PcapPlusPlus/pull/1823#discussion_r2287041041
  • https://github.com/seladb/PcapPlusPlus/pull/1823#discussion_r2287041757
  • https://github.com/seladb/PcapPlusPlus/pull/1823#discussion_r2278514017

seladb avatar Aug 20 '25 05:08 seladb

@yahyayozo I think the only comments left are:

  • https://github.com/seladb/PcapPlusPlus/pull/1823#discussion_r2278509145
  • https://github.com/seladb/PcapPlusPlus/pull/1823#discussion_r2278514017

Once you address those I think we can merge this PR

seladb avatar Aug 23 '25 20:08 seladb

@seladb all the comments shall be addressed now I kept the ModbusReadInputRegisters struct because I use in the constructor and also to keep it as an example for the future implementation of other functions Codes

yahyayozo avatar Aug 25 '25 11:08 yahyayozo

@seladb can you please check why it shows forbid submodules in the failed pre-commit?

yahyayozo avatar Aug 25 '25 14:08 yahyayozo

@seladb can you please check why it shows forbid submodules in the failed pre-commit?

a7b926daa892f6d5242076be0e1f63c8301cdf95 this commit adds deps/benchmark-src to the repo. I think this is a mistake.

Dimi1010 avatar Aug 25 '25 16:08 Dimi1010

@seladb @Dimi1010 I removed it I don't know how it was added as a submodule!!

yahyayozo avatar Aug 25 '25 16:08 yahyayozo

@yahyayozo thank you so much for working on this and contributing to PcapPlusPlus, it is much appreciated! 🙏

seladb avatar Aug 26 '25 06:08 seladb

@seladb so, the other PDUs types will added in another PR?

yahyayozo avatar Aug 26 '25 18:08 yahyayozo

Yes, it's easier to review that way as it results in smaller differential changes in PRs.

Dimi1010 avatar Aug 26 '25 18:08 Dimi1010