scapy.layers.can

A minimal implementation of the CANopen protocol, based on Wireshark dissectors. See https://wiki.wireshark.org/CANopen

class scapy.layers.can.BEFloatSignalField(name, default, start, scaling=1, unit='', offset=0, ndigits=3)[source]

Bases: scapy.layers.can.SignalField

class scapy.layers.can.BESignedSignalField(name, default, start, size, scaling=1, unit='', offset=0, ndigits=3)[source]

Bases: scapy.layers.can.SignalField

class scapy.layers.can.BEUnsignedSignalField(name, default, start, size, scaling=1, unit='', offset=0, ndigits=3)[source]

Bases: scapy.layers.can.SignalField

class scapy.layers.can.CAN(*args, **kargs)[source]

Bases: scapy.packet.Packet

A minimal implementation of the CANopen protocol, based on Wireshark dissectors. See https://wiki.wireshark.org/CANopen

aliastypes
extract_padding(p)[source]
fields_desc
Display RFC-like schema
 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|FLAGS|                        IDENTIFIER                       |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|     LENGTH    |                    RESERVED                   |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|               |              DATA             |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

                             Fig. CAN                             
CAN fields

flags

FlagsField

<Flag 0 ()>

identifier

XBitField (29 bits)

0

length

FieldLenField

None

reserved

ThreeBytesField

0

data

StrLenField

b''

static inv_endianness(pkt)[source]

Invert the order of the first four bytes of a CAN packet

This method is meant to be used specifically to convert a CAN packet between the pcap format and the socketCAN format

Parameters

pkt – str of the CAN packet

Returns

packet str with the first four bytes swapped

post_build(pkt, pay)[source]

Implements the swap-bytes functionality when building

this is based on a copy of the Packet.self_build default method. The goal is to affect only the CAN layer data and keep under layers (e.g LinuxCooked) unchanged

post_dissect(s)[source]
pre_dissect(s)[source]

Implements the swap-bytes functionality when dissecting

class scapy.layers.can.CandumpReader(filename, interface=None)[source]

Bases: object

A stateful candump reader. Each packet is returned as a CAN packet

close()[source]
dispatch(callback)[source]

call the specified callback routine for each packet read

This is just a convenience function for the main loop that allows for easy launching of packet processing in a thread.

fileno()[source]
next()[source]

implement the iterator protocol on a set of packets

nonblocking_socket = True
static open(filename)[source]

Open (if necessary) filename.

read_all(count=- 1)[source]

return a list of all packets in the candump file

read_packet(size=65535)[source]

return a single packet read from the file or None if filters apply

raise EOFError when no more packets are available

recv(size=65535)[source]

Emulate a socket

static select(sockets, remain=None)[source]
class scapy.layers.can.LEFloatSignalField(name, default, start, scaling=1, unit='', offset=0, ndigits=3)[source]

Bases: scapy.layers.can.SignalField

class scapy.layers.can.LESignedSignalField(name, default, start, size, scaling=1, unit='', offset=0, ndigits=3)[source]

Bases: scapy.layers.can.SignalField

class scapy.layers.can.LEUnsignedSignalField(name, default, start, size, scaling=1, unit='', offset=0, ndigits=3)[source]

Bases: scapy.layers.can.SignalField

class scapy.layers.can.SignalField(name, default, start, size, scaling=1, unit='', offset=0, ndigits=3, fmt='B')[source]

Bases: scapy.fields.ScalingField

addfield(pkt, s, val)[source]
getfield(pkt, s)[source]
i2len(pkt, x)[source]
randval()[source]
size
start
class scapy.layers.can.SignalHeader(*args, **kargs)[source]

Bases: scapy.layers.can.CAN

aliastypes
extract_padding(s)[source]
fields_desc
Display RFC-like schema
 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|FLAGS|                        IDENTIFIER                       |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|     LENGTH    |                    RESERVED                   |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|               |
+-+-+-+-+-+-+-+-+

                        Fig. SignalHeader                         
SignalHeader fields

flags

FlagsField

<Flag 0 ()>

identifier

XBitField (29 bits)

0

length

LenField

None

reserved

ThreeBytesField

0

class scapy.layers.can.SignalPacket(*args, **kargs)[source]

Bases: scapy.packet.Packet

aliastypes
post_dissect(s)[source]

SignalFields can be dissected on packets with unordered fields. The order of SignalFields is defined from the start parameter. After a build, the consumed bytes of the length of all SignalFields have to be removed from the SignalPacket.

pre_dissect(s)[source]
scapy.layers.can.rdcandump(filename, count=- 1, interface=None)[source]

Read a candump log file and return a packet list

filename: file to read count: read only <count> packets interfaces: return only packets from a specified interface