1515import time
1616
1717from scapy .config import conf
18+ from scapy .data import SO_TIMESTAMPNS
1819from scapy .supersocket import SuperSocket
19- from scapy .error import Scapy_Exception , warning
20+ from scapy .error import Scapy_Exception , warning , log_runtime
2021from scapy .packet import Packet
2122from scapy .layers .can import CAN , CAN_MTU , CAN_FD_MTU
22- from scapy .arch .linux import get_last_packet_timestamp
2323from scapy .compat import raw
2424
2525from 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 :
0 commit comments