Skip to content

Commit 95799af

Browse files
committed
Python3 compat: Using bytearray
1 parent 26e19e5 commit 95799af

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,4 @@ venv.bak/
102102

103103
# mypy
104104
.mypy_cache/
105+
.idea

rawsocketpy/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Package init
22

3-
from get_hw import get_hw, u_to_str
4-
from main import RawPacket, RawSocket
3+
from .util import get_hw, u_to_str, protocol_to_ethertype, to_bytes
4+
from .main import RawPacket, RawSocket

rawsocketpy/main.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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
33

44
class 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)

rawsocketpy/get_hw.py renamed to rawsocketpy/util.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,33 @@
1111
import binascii
1212
def get_hw(ifname):
1313
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
14-
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytes(ifname[:15], 'utf-8')))
14+
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytearray(ifname[:15], 'utf-8')))
1515
return info[18:24]
1616

17-
def u_to_str(data, separator=":"):
18-
return separator.join("{:02x}".format(ord(c)) for c in data).upper()
1917
else:
20-
2118
def get_hw(ifname):
2219
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
2320
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15]))
2421
return info[18:24]
2522

26-
def u_to_str(data, separator=":"):
27-
return separator.join("{:02x}".format(ord(c)) for c in data).upper()
23+
def u_to_str(data, separator=":"):
24+
return separator.join("{:02x}".format(ord(c)) for c in data).upper()
25+
26+
def protocol_to_ethertype(protocol):
27+
return chr((protocol & 0xFF00) >> 8) + chr(protocol & 0x00FF)
28+
29+
def to_bytes(*data):
30+
result = bytearray()
31+
for d in data:
32+
if type(d) in [tuple, list]:
33+
baa = map(to_bytes, d)
34+
for ba in baa:
35+
result += ba
36+
if type(d) is int:
37+
result += bytearray([d])
38+
if type(d) is str:
39+
result += bytearray(map(ord, d))
40+
return result
2841

2942
def main():
3043
print(u_to_str(get_hw("wlp2s0")))

0 commit comments

Comments
 (0)