Skip to content

Implement J1939 Soft Socket for SAE J1939 Transport Protocol in Python - #5011

Open
polybassa wants to merge 8 commits into
secdev:masterfrom
polybassa:j1939_soft_sockets
Open

Implement J1939 Soft Socket for SAE J1939 Transport Protocol in Python#5011
polybassa wants to merge 8 commits into
secdev:masterfrom
polybassa:j1939_soft_sockets

Conversation

@polybassa

Copy link
Copy Markdown
Contributor

AI-Assisted: yes (GitHub Copilot)

@codecov

codecov Bot commented Jun 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.56357% with 55 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.61%. Comparing base (b1a9799) to head (ec4a1c1).

Files with missing lines Patch % Lines
scapy/contrib/j1939.py 89.56% 55 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5011      +/-   ##
==========================================
+ Coverage   80.05%   80.61%   +0.56%     
==========================================
  Files         390      390              
  Lines       96810    97328     +518     
==========================================
+ Hits        77499    78463     +964     
+ Misses      19311    18865     -446     
Files with missing lines Coverage Δ
scapy/contrib/j1939.py 87.88% <89.56%> (+3.67%) ⬆️

... and 22 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@polybassa

Copy link
Copy Markdown
Contributor Author

@BenGardiner Please have a look.

@BenGardiner

Copy link
Copy Markdown
Contributor

Hi @polybassa , :) it looks like a much better implementation of what I tried out over in https://github.com/BenGardiner/scapy/tree/j1939_scanning . Thank you.

I will try to rebase and port my scanner, DM , DM scanner and address claim implementation onto this branch and see how well those features still work.

Nils Weiss and others added 7 commits August 13, 2026 09:27
AI-Assisted: yes (Claude Sonnet 4.6)
AI-Assisted: no
…ostile transfers

The soft socket handled the transfers its own tests produce, all of them
under 100 bytes from a single well-behaved peer. Everything past that was
either dropped or actively harmful, and none of it was visible because no
test exceeded one CTS block, ran two senders, or sent a malformed frame.

A payload of more than 1785 bytes needs more TP.DT packets than a sequence
number can express, so building the announcement raised inside the
scheduler thread after the TX state had already been set. The message
vanished with a log line and the state machine stayed latched, which
silently discarded every later send on that socket. send() now refuses
such a payload, _J1939_TP_MAX_DATA finally being used for what it was
defined for, and a failure anywhere in _begin_send resets the machine
instead of wedging it.

Reception was a single set of rx_* attributes, so a second announcement
threw away the transfer in progress. Since a busy J1939 bus has several
ECUs broadcasting at once, a monitor built on this socket lost most of
what it saw. Sessions now live in a dict keyed by the (source address,
destination) pair the protocol itself uses, capped so a hostile bus cannot
grow it without bound, and a peer that asks for a second PGN while one is
running is refused with an abort rather than displacing it.

Frames from the bus are no longer taken at face value. An announcement
whose size and packet count cannot describe a message is refused instead
of delivering an empty payload; a CTS naming a packet outside the message
is aborted instead of indexing the buffer backwards and emitting sequence
number 0; and CTS, acknowledgement and abort frames must now name the PGN
of the session they claim to be part of. tx_peer_sa is cleared when a
session ends, so a node that took part in an earlier transfer can no
longer abort an unrelated broadcast.

The rest are smaller: the receiver honours the max_packets of a request
and issues a CTS per block instead of authorising everything at once,
close() derives its drain budget from what is left to send rather than
truncating any broadcast longer than two seconds while __del__ no longer
drains at all, basecls is used for delivered messages and recv_raw returns
the payload, transport frames carry the caller's priority as single frames
already did, a source address of 0xFF warns because it cannot legally
appear on the wire, and a CAN socket that goes away closes the J1939
socket instead of leaving a caller blocked in recv() forever.

The twelve new cases in the campaign each fail on the code before this
commit and pass after it. The existing 185 are untouched and still pass.

AI-Assisted: yes (Cursor)
Co-authored-by: Cursor <cursoragent@cursor.com>
Four things the first pass got wrong or left rough. send() measured the
payload differently from the code that transmits it, so the size guard and
the wire could disagree for a message whose data was not bytes; both now
go through one helper. The check for a CAN socket that has gone away was
written twice in can_recv, once at each end. The new basecls parameter and
the per-peer session model were undocumented. And the priority a caller
asks for now reaches the TP.CM and TP.DT frames of a multi-packet message,
which is a deliberate change of default from the 7 the code used to
hardcode for TP.DT, so the docstring says so.

AI-Assisted: yes (Cursor)
Co-authored-by: Cursor <cursoragent@cursor.com>
… hid

Four handlers cancelled a scheduler timeout inside try/except/pass, which
Codacy flags and which hides a real failure as readily as the expected
one. The expected one is narrow: TimeoutScheduler raises Scapy_Exception
when a timeout has already fired or been cancelled, which races normally
against the state machine dropping it. One _cancel helper now does that in
the five places that needed it, logging anything else at debug level, and
send() sets sent_time behind an isinstance check rather than catching the
AttributeError a non-packet would raise.

Building a connection abort was written out five times and refusing a
session three times; both are helpers now, which is what made it obvious
that the check for a TP.DT past the authorised block can never fire: the
next CTS is sent from the same handler that completes a block, so the
window it guards does not exist. Writing the test for it is what showed
that, and both the branch and the test are gone.

The four new cases cover what had no test: the session table filling up
and answering with 'system resources', a stalled reception aborting with
'timeout' once its wall-clock ceiling passes, a sender aborting a
reception it started, and close(timeout=0) as the way to give up on a
transfer on purpose.

AI-Assisted: yes (Cursor)
Co-authored-by: Cursor <cursoragent@cursor.com>
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.

2 participants