scapy.contrib.isotp.isotp_soft_socket

class scapy.contrib.isotp.isotp_soft_socket.ISOTPSocketImplementation(can_socket, src_id, dst_id, padding=False, extended_addr=None, extended_rx_addr=None, rx_block_size=0, stmin=0, listen_only=False)[source]

Bases: object

Implementation of an ISOTP “state machine”.

Most of the ISOTP logic was taken from https://github.com/hartkopp/can-isotp/blob/master/net/can/isotp.c

This class is separated from ISOTPSoftSocket to make sure the background thread can’t hold a reference to ISOTPSoftSocket, allowing it to be collected by the GC.

Parameters
  • can_socket – a CANSocket instance, preferably filtering only can frames with identifier equal to did

  • src_id – the CAN identifier of the sent CAN frames

  • dst_id – the CAN identifier of the received CAN frames

  • padding – If True, pads sending packets with 0x00 which not count to the payload. Does not affect receiving packets.

  • extended_addr – Extended Address byte to be added at the beginning of every CAN frame _sent_ by this object. Can be None in order to disable extended addressing on sent frames.

  • extended_rx_addr – Extended Address byte expected to be found at the beginning of every CAN frame _received_ by this object. Can be None in order to disable extended addressing on received frames.

  • rx_block_size – Block Size byte to be included in every Control Flow Frame sent by this object. The default value of 0 means that all the data will be received in a single block.

  • stmin – Time Minimum Separation byte to be included in every Control Flow Frame sent by this object. The default value of 0 indicates that the peer will not wait any time between sending frames.

  • listen_only – Disables send of flow control frames

begin_send(x)[source]

Begins sending an ISOTP message. This method does not block.

can_send(load)[source]
close()[source]
on_can_recv(p)[source]
on_recv(cf)[source]

Function that must be called every time a CAN frame is received, to advance the state machine.

recv(timeout=None)[source]

Receive an ISOTP frame, blocking if none is available in the buffer for at most ‘timeout’ seconds.

send(p)[source]

Send an ISOTP frame and block until the message is sent or an error happens.

class scapy.contrib.isotp.isotp_soft_socket.ISOTPSoftSocket(can_socket=None, sid=0, did=0, extended_addr=None, extended_rx_addr=None, rx_block_size=0, stmin=0, padding=False, listen_only=False, basecls=<class 'scapy.contrib.isotp.isotp_packet.ISOTP'>)[source]

Bases: scapy.supersocket.SuperSocket

This class is a wrapper around the ISOTPSocketImplementation, for the reasons described below.

The ISOTPSoftSocket aims to be fully compatible with the Linux ISOTP sockets provided by the can-isotp kernel module, while being usable on any operating system. Therefore, this socket needs to be able to respond to an incoming FF frame with a FC frame even before the recv() method is called. A thread is needed for receiving CAN frames in the background, and since the lower layer CAN implementation is not guaranteed to have a functioning POSIX select(), each ISOTP socket needs its own CAN receiver thread. SuperSocket automatically calls the close() method when the GC destroys an ISOTPSoftSocket. However, note that if any thread holds a reference to an ISOTPSoftSocket object, it will not be collected by the GC.

The implementation of the ISOTP protocol, along with the necessary thread, are stored in the ISOTPSocketImplementation class, and therefore:

  • There no reference from ISOTPSocketImplementation to ISOTPSoftSocket

  • ISOTPSoftSocket can be normally garbage collected

  • Upon destruction, ISOTPSoftSocket.close() will be called

  • ISOTPSoftSocket.close() will call ISOTPSocketImplementation.close()

  • RX background thread can be stopped by the garbage collector

Initialize an ISOTPSoftSocket using the provided underlying can socket.

Example (with NativeCANSocket underneath):
>>> conf.contribs['ISOTP'] = {'use-can-isotp-kernel-module': False}
>>> load_contrib('isotp')
>>> with ISOTPSocket("can0", sid=0x641, did=0x241) as sock:
>>>     sock.send(...)
Example (with PythonCANSocket underneath):
>>> conf.contribs['ISOTP'] = {'use-can-isotp-kernel-module': False}
>>> conf.contribs['CANSocket'] = {'use-python-can': True}
>>> load_contrib('isotp')
>>> with ISOTPSocket(CANSocket(bustype='socketcan', channel="can0"), sid=0x641, did=0x241) as sock:
>>>     sock.send(...)
Parameters
  • can_socket – a CANSocket instance, preferably filtering only can frames with identifier equal to did

  • sid – the CAN identifier of the sent CAN frames

  • did – the CAN identifier of the received CAN frames

  • extended_addr – the extended address of the sent ISOTP frames (can be None)

  • extended_rx_addr – the extended address of the received ISOTP frames (can be None)

  • rx_block_size – block size sent in Flow Control ISOTP frames

  • stmin – minimum desired separation time sent in Flow Control ISOTP frames

  • padding – If True, pads sending packets with 0x00 which not count to the payload. Does not affect receiving packets.

  • basecls – base class of the packets emitted by this socket

begin_send(p)[source]

Begin the transmission of message p. This method returns after sending the first frame. If multiple frames are necessary to send the message, this socket will unable to send other messages until either the transmission of this frame succeeds or it fails.

close()[source]
nonblocking_socket = True
recv(x=65535)[source]
recv_raw(x=65535)[source]

Receive a complete ISOTP message, blocking until a message is received or the specified timeout is reached. If self.timeout is 0, then this function doesn’t block and returns the first frame in the receive buffer or None if there isn’t any.

static select(sockets, remain=None)[source]

This function is called during sendrecv() routine to wait for sockets to be ready to receive

class scapy.contrib.isotp.isotp_soft_socket.TimeoutScheduler[source]

Bases: object

A timeout scheduler which uses a single thread for all timeouts, unlike python’s own Timer objects which use a thread each.

GRACE = 0.1
class Handle(when, cb)[source]

Bases: object

Handle for a timeout, consisting of a callback and a time when it should be executed.

cancel()[source]

Cancels this timeout, preventing it from executing its callback

VERBOSE = False
static cancel(handle)[source]

Provided its handle, cancels the execution of a timeout.

static clear()[source]

Cancels the execution of all timeouts.

static schedule(timeout, callback)[source]

Schedules the execution of a timeout.

The function callback will be called in timeout seconds.

Returns a handle that can be used to remove the timeout.