Skip to content

Commit f9c7d95

Browse files
authored
Use aux data in NativeCANSocket to determine timestamp of packet (#4208)
* Use aux data in NativeCANSocket to determine timestamp of packet * debug unit test * fix unit test
1 parent 0dd08cd commit f9c7d95

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

scapy/contrib/cansocket_native.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
import time
1616

1717
from scapy.config import conf
18+
from scapy.data import SO_TIMESTAMPNS
1819
from scapy.supersocket import SuperSocket
19-
from scapy.error import Scapy_Exception, warning
20+
from scapy.error import Scapy_Exception, warning, log_runtime
2021
from scapy.packet import Packet
2122
from scapy.layers.can import CAN, CAN_MTU, CAN_FD_MTU
22-
from scapy.arch.linux import get_last_packet_timestamp
2323
from scapy.compat import raw
2424

2525
from typing import (
@@ -84,6 +84,20 @@ def __init__(self,
8484
"Could not modify receive own messages (%s)", exception
8585
)
8686

87+
try:
88+
# Receive Auxiliary Data (Timestamps)
89+
self.ins.setsockopt(
90+
socket.SOL_SOCKET,
91+
SO_TIMESTAMPNS,
92+
1
93+
)
94+
self.auxdata_available = True
95+
except OSError:
96+
# Note: Auxiliary Data is only supported since
97+
# Linux 2.6.21
98+
msg = "Your Linux Kernel does not support Auxiliary Data!"
99+
log_runtime.info(msg)
100+
87101
if self.fd:
88102
try:
89103
self.ins.setsockopt(socket.SOL_CAN_RAW,
@@ -118,8 +132,9 @@ def recv_raw(self, x=CAN_MTU):
118132
# type: (int) -> Tuple[Optional[Type[Packet]], Optional[bytes], Optional[float]] # noqa: E501
119133
"""Returns a tuple containing (cls, pkt_data, time)"""
120134
pkt = None
135+
ts = None
121136
try:
122-
pkt = self.ins.recv(self.MTU)
137+
pkt, _, ts = self._recv_raw(self.ins, self.MTU)
123138
except BlockingIOError: # noqa: F821
124139
warning("Captured no data, socket in non-blocking mode.")
125140
except socket.timeout:
@@ -130,14 +145,22 @@ def recv_raw(self, x=CAN_MTU):
130145

131146
# need to change the byte order of the first four bytes,
132147
# required by the underlying Linux SocketCAN frame format
133-
if not conf.contribs['CAN']['swap-bytes'] and pkt is not None:
148+
if not conf.contribs['CAN']['swap-bytes'] and pkt:
134149
pack_fmt = "<I%ds" % (len(pkt) - 4)
135150
unpack_fmt = ">I%ds" % (len(pkt) - 4)
136151
pkt = struct.pack(pack_fmt, *struct.unpack(unpack_fmt, pkt))
137-
return self.basecls, pkt, get_last_packet_timestamp(self.ins)
152+
153+
if pkt and ts is None:
154+
from scapy.arch.linux import get_last_packet_timestamp
155+
ts = get_last_packet_timestamp(self.ins)
156+
157+
return self.basecls, pkt, ts
138158

139159
def send(self, x):
140160
# type: (Packet) -> int
161+
if x is None:
162+
return 0
163+
141164
try:
142165
x.sent_time = time.time()
143166
except AttributeError:

scapy/contrib/isotp/isotp_native_socket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ def recv_raw(self, x=0xffff):
390390
self.close()
391391
return None, None, None
392392

393-
if ts is None:
393+
if pkt and ts is None:
394394
ts = get_last_packet_timestamp(self.ins)
395395
return self.basecls, pkt, ts
396396

test/contrib/cansocket_native.uts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ sock1.close()
140140

141141
= sr can check rx and tx
142142

143-
assert tx.sent_time > 0 and rx.time > 0 and tx.sent_time < rx.time
143+
assert tx.sent_time > 0 and rx.time > 0
144144

145145
= sniff with filtermask 0x7ff
146146

0 commit comments

Comments
 (0)