scapy.layers.can module

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

class scapy.layers.can.CAN

Bases: scapy.packet.Packet

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

aliastypes = [<class 'scapy.layers.can.CAN'>, <class 'scapy.packet.Packet'>]
extract_padding(p)

DEV: to be overloaded to extract current layer’s padding.

Parameters:s (str) – the current layer
Returns:a couple of strings (actual layer, padding)
fields_desc = [<Field (CAN).flags>, <Field (CAN).identifier>, <Field (CAN).length>, <Field (CAN).reserved>, <Field (CAN).data>]
static inv_endianness(pkt)

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)

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)

DEV: is called right after the current layer has been dissected

pre_dissect(s)

Implements the swap-bytes functionality when dissecting

class scapy.layers.can.SignalPacket

Bases: scapy.packet.Packet

aliastypes = [<class 'scapy.layers.can.SignalPacket'>, <class 'scapy.packet.Packet'>]
post_dissect(s)

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)

DEV: is called right before the current layer is dissected

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

Bases: scapy.fields.ScalingField

addfield(pkt, s, val)

Add an internal value to a string

Copy the network representation of field val (belonging to layer pkt) to the raw string packet s, and return the new string packet.

getfield(pkt, s)

Extract an internal value from a string

Extract from the raw packet s the field value belonging to layer pkt.

Returns a two-element list, first the raw packet string after having removed the extracted field, second the extracted field itself in internal representation.

i2len(pkt, x)

Convert internal value to a length usable by a FieldLenField

randval()

Return a volatile object whose value is both random and suitable for this field

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

Bases: scapy.layers.can.SignalField

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

Bases: scapy.layers.can.SignalField

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

Bases: scapy.layers.can.SignalField

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

Bases: scapy.layers.can.SignalField

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

Bases: scapy.layers.can.SignalField

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

Bases: scapy.layers.can.SignalField

scapy.layers.can.rdcandump(filename, count=-1, interface=None)

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

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

Bases: object

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

close()
dispatch(callback)

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()
next()

implement the iterator protocol on a set of packets

nonblocking_socket = True
static open(filename)

Open (if necessary) filename.

read_all(count=-1)

return a list of all packets in the candump file

read_allowed_exceptions = ()
read_packet(size=65535)

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

raise EOFError when no more packets are available

recv(size=65535)

Emulate a socket

static select(sockets, remain=None)
class scapy.layers.can.SignalHeader

Bases: scapy.layers.can.CAN

aliastypes = [<class 'scapy.layers.can.SignalHeader'>, <class 'scapy.layers.can.CAN'>, <class 'scapy.packet.Packet'>]
extract_padding(s)

DEV: to be overloaded to extract current layer’s padding.

Parameters:s (str) – the current layer
Returns:a couple of strings (actual layer, padding)
fields_desc = [<Field (SignalHeader).flags>, <Field (SignalHeader).identifier>, <Field (SignalHeader).length>, <Field (SignalHeader).reserved>]