11import 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
33
44class RawPacket ():
55 def __init__ (self , data ):
@@ -27,7 +27,7 @@ def __init__(self, interface, protocol, sock=None, no_recv_protocol=False):
2727 if not 0x0000 < protocol < 0xFFFF :
2828 raise ValueError ("Protocol has to be in the range 0 to 65535" )
2929 self .protocol = socket .htons (protocol )
30- self .ethertype = self . protocol_to_ethertype (protocol )
30+ self .ethertype = protocol_to_ethertype (protocol )
3131 self .interface = interface
3232 self .mac = get_hw (self .interface )
3333 if no_recv_protocol :
@@ -42,14 +42,11 @@ def sock_create(interface, protocol, sock=None):
4242 sock .bind ((interface , 0 ))
4343 return sock
4444
45- @staticmethod
46- def protocol_to_ethertype (protocol ):
47- return chr ((protocol & 0xFF00 ) >> 8 ) + chr (protocol & 0x00FF )
48-
4945 def send (self , msg , dest = None , ethertype = None ):
5046 if ethertype is None : ethertype = self .ethertype
5147 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 )
5350
5451 def recv (self ):
5552 data = self .sock .recv (1500 )
0 commit comments