@@ -61,6 +61,7 @@ class SuperSocket(six.with_metaclass(_SuperSocket_metaclass)):
61
61
closed = 0
62
62
nonblocking_socket = False
63
63
read_allowed_exceptions = ()
64
+ auxdata_available = False
64
65
65
66
def __init__ (self , family = socket .AF_INET , type = socket .SOCK_STREAM , proto = 0 ): # noqa: E501
66
67
self .ins = socket .socket (family , type , proto )
@@ -85,16 +86,26 @@ def _recv_raw(self, sock, x):
85
86
"""Internal function to receive a Packet,
86
87
and process ancillary data.
87
88
"""
88
- flags_len = socket .CMSG_LEN (4096 )
89
89
timestamp = None
90
+ if not self .auxdata_available :
91
+ pkt , _ , _ , sa_ll = sock .recvmsg (x )
92
+ return pkt , sa_ll , timestamp
93
+ flags_len = socket .CMSG_LEN (4096 )
90
94
pkt , ancdata , flags , sa_ll = sock .recvmsg (x , flags_len )
91
95
if not pkt :
92
- return pkt , sa_ll
96
+ return pkt , sa_ll , timestamp
93
97
for cmsg_lvl , cmsg_type , cmsg_data in ancdata :
94
98
# Check available ancillary data
95
99
if (cmsg_lvl == SOL_PACKET and cmsg_type == PACKET_AUXDATA ):
96
100
# Parse AUXDATA
97
- auxdata = tpacket_auxdata .from_buffer_copy (cmsg_data )
101
+ try :
102
+ auxdata = tpacket_auxdata .from_buffer_copy (cmsg_data )
103
+ except ValueError :
104
+ # Note: according to Python documentation, recvmsg()
105
+ # can return a truncated message. A ValueError
106
+ # exception likely indicates that Auxiliary
107
+ # Data is not supported by the Linux kernel.
108
+ return pkt , sa_ll , timestamp
98
109
if auxdata .tp_vlan_tci != 0 or \
99
110
auxdata .tp_status & TP_STATUS_VLAN_VALID :
100
111
# Insert VLAN tag
@@ -213,13 +224,19 @@ def __init__(self, type=ETH_P_IP, filter=None, iface=None, promisc=None, nofilte
213
224
if iface is not None :
214
225
self .ins .bind ((iface , type ))
215
226
if not six .PY2 :
216
- # Receive Auxiliary Data (VLAN tags)
217
- self .ins .setsockopt (SOL_PACKET , PACKET_AUXDATA , 1 )
218
- self .ins .setsockopt (
219
- socket .SOL_SOCKET ,
220
- SO_TIMESTAMPNS ,
221
- 1
222
- )
227
+ try :
228
+ # Receive Auxiliary Data (VLAN tags)
229
+ self .ins .setsockopt (SOL_PACKET , PACKET_AUXDATA , 1 )
230
+ self .ins .setsockopt (
231
+ socket .SOL_SOCKET ,
232
+ SO_TIMESTAMPNS ,
233
+ 1
234
+ )
235
+ self .auxdata_available = True
236
+ except OSError :
237
+ # Note: Auxiliary Data is only supported since
238
+ # Linux 2.6.21
239
+ warning ("Your Linux Kernel does not support Auxiliary Data!" )
223
240
224
241
def recv (self , x = MTU ):
225
242
pkt , sa_ll , ts = self ._recv_raw (self .ins , x )
0 commit comments