MCPcopy Create free account
hub / github.com/secdev/scapy / CAN

Class CAN

scapy/layers/can.py:64–168  ·  view source on GitHub ↗

A implementation of CAN messages. Dissection of CAN messages from Wireshark captures and Linux PF_CAN sockets are supported from protocol specification. See https://wiki.wireshark.org/CANopen for further information on the Wireshark dissector. Linux PF_CAN and Wireshark use differen

Source from the content-addressed store, hash-verified

62
63
64class CAN(Packet):
65 """A implementation of CAN messages.
66
67 Dissection of CAN messages from Wireshark captures and Linux PF_CAN sockets
68 are supported from protocol specification.
69 See https://wiki.wireshark.org/CANopen for further information on
70 the Wireshark dissector. Linux PF_CAN and Wireshark use different
71 endianness for the first 32 bit of a CAN message. This dissector can be
72 configured for both use cases.
73
74 Configuration ``swap-bytes``:
75 Wireshark dissection:
76 >>> conf.contribs['CAN']['swap-bytes'] = False
77
78 PF_CAN Socket dissection:
79 >>> conf.contribs['CAN']['swap-bytes'] = True
80
81 Configuration ``remove-padding``:
82 Linux PF_CAN Sockets always return 16 bytes per CAN frame receive.
83 This implicates that CAN frames get padded from the Linux PF_CAN socket
84 with zeros up to 8 bytes of data. The real length from the CAN frame on
85 the wire is given by the length field. To obtain only the CAN frame from
86 the wire, this additional padding has to be removed. Nevertheless, for
87 corner cases, it might be useful to also get the padding. This can be
88 configured through the **remove-padding** configuration.
89
90 Truncate CAN frame based on length field:
91 >>> conf.contribs['CAN']['remove-padding'] = True
92
93 Show entire CAN frame received from socket:
94 >>> conf.contribs['CAN']['remove-padding'] = False
95
96 """
97 fields_desc = [
98 FlagsField('flags', 0, 3, ['error',
99 'remote_transmission_request',
100 'extended']),
101 XBitField('identifier', 0, 29),
102 FieldLenField('length', None, length_of='data', fmt='B'),
103 ThreeBytesField('reserved', 0),
104 StrLenField('data', b'', length_from=lambda pkt: int(pkt.length)),
105 ]
106
107 @classmethod
108 def dispatch_hook(cls,
109 _pkt=None, # type: Optional[bytes]
110 *args, # type: Any
111 **kargs # type: Any
112 ): # type: (...) -> Type[Packet]
113 if _pkt:
114 fdf_set = len(_pkt) > 5 and _pkt[5] & 0x04 and \
115 not _pkt[5] & 0xf8
116 if fdf_set:
117 return CANFD
118 elif len(_pkt) > 4 and _pkt[4] > 8:
119 return CANFD
120 return CAN
121

Callers 1

read_packetMethod · 0.85

Calls 5

FlagsFieldClass · 0.90
XBitFieldClass · 0.90
FieldLenFieldClass · 0.90
ThreeBytesFieldClass · 0.90
StrLenFieldClass · 0.90

Tested by

no test coverage detected