1
1
import socket , select , struct , time
2
- from .get_hw import get_hw , u_to_str
2
+ from .util import get_hw , u_to_str , protocol_to_ethertype , to_bytes
3
3
4
4
class RawPacket ():
5
5
def __init__ (self , data ):
@@ -27,7 +27,7 @@ def __init__(self, interface, protocol, sock=None, no_recv_protocol=False):
27
27
if not 0x0000 < protocol < 0xFFFF :
28
28
raise ValueError ("Protocol has to be in the range 0 to 65535" )
29
29
self .protocol = socket .htons (protocol )
30
- self .ethertype = self . protocol_to_ethertype (protocol )
30
+ self .ethertype = protocol_to_ethertype (protocol )
31
31
self .interface = interface
32
32
self .mac = get_hw (self .interface )
33
33
if no_recv_protocol :
@@ -42,14 +42,11 @@ def sock_create(interface, protocol, sock=None):
42
42
sock .bind ((interface , 0 ))
43
43
return sock
44
44
45
- @staticmethod
46
- def protocol_to_ethertype (protocol ):
47
- return chr ((protocol & 0xFF00 ) >> 8 ) + chr (protocol & 0x00FF )
48
-
49
45
def send (self , msg , dest = None , ethertype = None ):
50
46
if ethertype is None : ethertype = self .ethertype
51
47
if dest is None : dest = self .BROADCAST
52
- self .sock .send (dest + self .mac + ethertype + msg )
48
+ payload = (to_bytes (dest ) + self .mac + to_bytes (ethertype ) + to_bytes (msg ))
49
+ self .sock .send (payload )
53
50
54
51
def recv (self ):
55
52
data = self .sock .recv (1500 )
0 commit comments