@@ -35,13 +35,12 @@ class _L2bpfSocket(SuperSocket):
35
35
""""Generic Scapy BPF Super Socket"""
36
36
37
37
desc = "read/write packets using BPF"
38
- assigned_interface = None
39
- fd_flags = None
40
- ins = None
41
- closed = False
38
+ nonblocking_socket = True
42
39
43
40
def __init__ (self , iface = None , type = ETH_P_ALL , promisc = None , filter = None ,
44
41
nofilter = 0 , monitor = False ):
42
+ self .fd_flags = None
43
+ self .assigned_interface = None
45
44
46
45
# SuperSocket mandatory variables
47
46
if promisc is None :
@@ -208,11 +207,15 @@ def close(self):
208
207
209
208
def send (self , x ):
210
209
"""Dummy send method"""
211
- raise Exception ("Can't send anything with %s" % self .__name__ )
210
+ raise Exception (
211
+ "Can't send anything with %s" % self .__class__ .__name__
212
+ )
212
213
213
- def recv (self , x = BPF_BUFFER_LENGTH ):
214
+ def recv_raw (self , x = BPF_BUFFER_LENGTH ):
214
215
"""Dummy recv method"""
215
- raise Exception ("Can't recv anything with %s" % self .__name__ )
216
+ raise Exception (
217
+ "Can't recv anything with %s" % self .__class__ .__name__
218
+ )
216
219
217
220
@staticmethod
218
221
def select (sockets , remain = None ):
@@ -273,22 +276,20 @@ def extract_frames(self, bpf_buffer):
273
276
274
277
# Get and store the Scapy object
275
278
frame_str = bpf_buffer [bh_hdrlen :bh_hdrlen + bh_caplen ]
276
- try :
277
- pkt = self .guessed_cls (frame_str )
278
- except Exception :
279
- if conf .debug_dissector :
280
- raise
281
- pkt = conf .raw_layer (frame_str )
282
- self .received_frames .append (pkt )
279
+ self .received_frames .append (
280
+ (self .guessed_cls , frame_str , None )
281
+ )
283
282
284
283
# Extract the next frame
285
284
end = self .bpf_align (bh_hdrlen , bh_caplen )
286
285
if (len_bb - end ) >= 20 :
287
286
self .extract_frames (bpf_buffer [end :])
288
287
289
- def recv (self , x = BPF_BUFFER_LENGTH ):
288
+ def recv_raw (self , x = BPF_BUFFER_LENGTH ):
290
289
"""Receive a frame from the network"""
291
290
291
+ x = min (x , BPF_BUFFER_LENGTH )
292
+
292
293
if self .buffered_frames ():
293
294
# Get a frame from the buffer
294
295
return self .get_frame ()
@@ -299,7 +300,7 @@ def recv(self, x=BPF_BUFFER_LENGTH):
299
300
except EnvironmentError as exc :
300
301
if exc .errno != errno .EAGAIN :
301
302
warning ("BPF recv()" , exc_info = True )
302
- return
303
+ return None , None , None
303
304
304
305
# Extract all frames from the BPF buffer
305
306
self .extract_frames (bpf_buffer )
@@ -318,7 +319,7 @@ def nonblock_recv(self):
318
319
319
320
if self .buffered_frames ():
320
321
# Get a frame from the buffer
321
- return self . get_frame ( )
322
+ return L2bpfListenSocket . recv ( self )
322
323
323
324
# Set the non blocking flag, read from the socket, and unset the flag
324
325
self .set_nonblock (True )
@@ -329,11 +330,11 @@ def nonblock_recv(self):
329
330
330
331
class L3bpfSocket (L2bpfSocket ):
331
332
332
- def get_frame (self ):
333
- """Get a frame or packet from the received list """
334
- pkt = super ( L3bpfSocket , self ). get_frame ( )
335
- if pkt is not None :
336
- return pkt .payload
333
+ def recv (self , x = BPF_BUFFER_LENGTH ):
334
+ """Receive on layer 3 """
335
+ r = SuperSocket . recv ( self , x )
336
+ if r :
337
+ return r .payload
337
338
338
339
def send (self , pkt ):
339
340
"""Send a packet"""
@@ -363,7 +364,10 @@ def send(self, pkt):
363
364
364
365
def isBPFSocket (obj ):
365
366
"""Return True is obj is a BPF Super Socket"""
366
- return isinstance (obj , L2bpfListenSocket ) or isinstance (obj , L2bpfListenSocket ) or isinstance (obj , L3bpfSocket ) # noqa: E501
367
+ return isinstance (
368
+ obj ,
369
+ (L2bpfListenSocket , L2bpfListenSocket , L3bpfSocket )
370
+ )
367
371
368
372
369
373
def bpf_select (fds_list , timeout = None ):
0 commit comments