Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic EtherCat layer #569

Closed
wants to merge 0 commits into from
Closed

Conversation

speakinghedge
Copy link
Contributor

This pull request adds basic support for Type 12 EtherCat messages (IEC 61158-4-12).

As the EtherCat protocol uses LittleEndian byte order - even for BitFields - this also adds LEBitFields support. There is a small regression test on top of the EtherCat-tests - but I don't know if the whole LEBitField implementation is mature enough...

if pad_len > 0:

pad = Padding()
pad.load = '\x00' * pad_len
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please adapt your code to the new contributing rules:

That only means using b'\x0....' instead of '\x0....'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do. there is also a bug in the frame padding (frames are to big).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. padding is fixed & tested against real devices.

scapy/fields.py Outdated
as we don't know the final size of the full bitfield we need to accumulate the data.
if we reach a field that ends at a octet boundary, we build the whole string

!!! NEVER MIX LEBitField with BitField !!!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean ?

Would it be possible to check that and raise an error ?

Copy link
Contributor Author

@speakinghedge speakinghedge Apr 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so and I will add an exception. It was not completely clear to me how to handle that kind of internal error as I was not able to derive some kind of rule by looking at the remaining code base 😄

edit: typo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed by 3fd2b76, adds explicit check for lower/upper layer sharing LEBitField type

@p-l-
Copy link
Member

p-l- commented Apr 18, 2017

Hi,

I'm not sure what you mean by BitFields being LE, but if I understand correctly, you can use regular BitFields and set negative lengths.

@speakinghedge
Copy link
Contributor Author

Hi,

negative size works only on BitFields using 16 or 32 bit length.

quick example (number after the # is assigned to the field):

class BitFieldReverseTest(Packet):
    fields_desc = [
        BitField('a', 0, 16), # 0xabcd
        BitField('b', 0, -16), # 0xabcd
        BitField('c0', 0, -4), # 0xa
        BitField('c1', 0, -4), # 0xb
        BitField('c2', 0, -4), # 0xc
        BitField('c3', 0, -4), # 0xd
        BitField('d0', 0, -4), # 0x5
        BitField('d1', 0, -16), # 0x1234
        BitField('d2', 0, -4), # 0x6
    ]

gives (added some spaces to separate fields)

ABCD CDAB A B C D 5 3412 6

As EtherCat uses some 2-11-3-sized-fields negative field length was no option as this is not handled by the current implementation. Even worse: it silently returns the unchanged value - maybe an Exception should be thrown here as this is not the expected behavior (as shown by your comment too).

Open question: what is the effort to add the ability to handle non 16/32 bit sized fields with the stock-BitField class....

@codecov-io
Copy link

codecov-io commented Jun 13, 2017

Codecov Report

Merging #569 into master will increase coverage by 0.12%.
The diff coverage is 97.69%.

@@            Coverage Diff            @@
##           master    #569      +/-   ##
=========================================
+ Coverage   83.68%   83.8%   +0.12%     
=========================================
  Files         159     160       +1     
  Lines       38120   38337     +217     
=========================================
+ Hits        31899   32129     +230     
+ Misses       6221    6208      -13
Impacted Files Coverage Δ
scapy/fields.py 90.87% <ø> (+0.17%) ⬆️
scapy/contrib/ethercat.py 97.69% <97.69%> (ø)
scapy/layers/inet6.py 78.2% <0%> (ø) ⬆️
scapy/asn1/ber.py 82.28% <0%> (+0.28%) ⬆️
scapy/layers/tls/basefields.py 80.27% <0%> (+0.68%) ⬆️
scapy/layers/tls/record.py 92.08% <0%> (+0.87%) ⬆️
scapy/layers/tls/record_sslv2.py 88.82% <0%> (+1.67%) ⬆️
scapy/layers/tls/keyexchange.py 84.76% <0%> (+1.71%) ⬆️

@guedou
Copy link
Member

guedou commented Dec 16, 2017

@speakinghedge could you rebase you changes ? Thanks.

@speakinghedge
Copy link
Contributor Author

did. also reduced the plethora of commits to some meaningful ones...

@guedou guedou removed the conflicts label Dec 22, 2017
@@ -0,0 +1,256 @@
from enum import test
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in ae0317c (I was fooled by pycharm. again. sorry.)

@p-l-
Copy link
Member

p-l- commented Jan 15, 2018

Please rebase against current master as we now have Python 3 tests. Also , do not use _ in your tests as they break because of that.

@speakinghedge
Copy link
Contributor Author

underscores removed in 49fd726.

maybe this should be added to your contribution rules.

@speakinghedge
Copy link
Contributor Author

I will take care for the Python 3 related issues.

@guedou
Copy link
Member

guedou commented Jan 24, 2018

@speakinghedge #1090 updates the documentation. Thanks for the idea.

@speakinghedge
Copy link
Contributor Author

I was really busy - will start working on this later today.

@speakinghedge
Copy link
Contributor Author

I was really busy - will start working on this later today.

...6 days ago. naa. that human timescale is wrong.

@guedou
Copy link
Member

guedou commented Feb 23, 2018

@speakinghedge I restarted the failing osx test and it is now ok. Could you rename your files to ethercat.py and ethertypes.uts ?

@gpotter2
Copy link
Member

You certainly mean ethercat.uts

@speakinghedge
Copy link
Contributor Author

@speakinghedge I restarted the failing osx test and it is now ok. Could you rename your files to ethercat.py and ethertypes.uts ?

done in c4b5843.

how could this survive that long? 😃

assert(len(frm) == 60)
frm = Ether(frm.do_build())
assert(frm[EtherCat].length == 10)
frm = Ether()/EtherCat()/Raw('012345678901234567890123456789012345678901234567890123456789')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to write:

frm = Ether()/EtherCat()/Raw(b'012345678901234567890123456789012345678901234567890123456789')

The same comment apply to other part of your code.

@guedou
Copy link
Member

guedou commented Feb 24, 2018

Sorry, I have more requests:

  • please remove the new fields from fields.py and move them to ethercat.py as they are specific to your changes
  • rebase against master. Your tests will fail on Python3.

scapy/fields.py Outdated
@@ -1121,6 +1121,7 @@ def any2i(self, pkt, x):
def i2repr(self, pkt, x):
return _EnumField.i2repr(self, pkt, x)


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please remove this useless change to preserve this file's code history? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as I touched that part of the code (by adding new fields but later on I moved them to another location) I honored the boy scout rule and added a newline between the two classes to follow pep8. but I see your point - 3da7f3f

@dsheets
Copy link

dsheets commented Apr 1, 2018

What happened to this PR? I am very interested in this functionality.

@gpotter2
Copy link
Member

gpotter2 commented Apr 1, 2018

It seems that the PR moved to https://github.com/speakinghedge/scapy/tree/EtherCat-support from speakinghedge/master, and he did not reopen the PR

@speakinghedge
Copy link
Contributor Author

Sorry - I lost somehow track - this 'close and move' didn't happen on purpose. Seems https://github.com/speakinghedge/scapy/tree/EtherCat-support is also in a bad state.

I will go and

  • create a new clean branch from master and add all EtherCat related changes on top and
  • open an new PR.

Side note: I'm not able to test this against real hardware anymore (as I switched the employer).

@speakinghedge speakinghedge mentioned this pull request Apr 3, 2018
speakinghedge added a commit to speakinghedge/scapy that referenced this pull request Apr 3, 2018
this is the cleaned up version of broken PR secdev#569

it incorporates all requested changes up to 3da7f3f

sorry for the noise.
guedou pushed a commit to guedou/scapy-issues that referenced this pull request Apr 18, 2018
this is the cleaned up version of broken PR secdev#569

it incorporates all requested changes up to 3da7f3f

sorry for the noise.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants