Merge "Makes rogue_dhcp.py script Python3 aware"

This commit is contained in:
Zuul 2019-05-21 19:28:23 +00:00 committed by Gerrit Code Review
commit eb9ca985ef
1 changed files with 20 additions and 11 deletions

View File

@ -14,6 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import fcntl
import six
import socket
import struct
import sys
@ -109,9 +110,8 @@ class DHCPDiscover(object):
def udp_checksum(self):
pseudo_header = self.ip_pseudo_header()
generated_checksum = self._checksum(pseudo_header +
self.udp_header(checksum=0) +
self.dhcp_discover_payload())
generated_checksum = self._checksum(pseudo_header + self.udp_header(
checksum=0) + self.dhcp_discover_payload())
return socket.htons(generated_checksum)
def ip_pseudo_header(self):
@ -155,7 +155,10 @@ class DHCPDiscover(object):
def _checksum(self, msg):
s = 0
for i in range(0, len(msg), 2):
w = ord(msg[i]) + (ord(msg[i + 1]) << 8)
if six.PY3:
w = msg[i] + (msg[i + 1] << 8)
else:
w = ord(msg[i]) + (ord(msg[i + 1]) << 8)
s = s + w
s = (s >> 16) + (s & 0xffff)
s = s + (s >> 16)
@ -168,27 +171,33 @@ def get_hw_addresses(interfaces):
for interface in interfaces:
info = fcntl.ioctl(s.fileno(),
SIOCGIFHWADDR,
struct.pack('256s', interface[:15]))
struct.pack('256s', interface[:15].encode('utf-8')))
interfaces_addresses[interface] = info[18:24]
s.close()
def inspect_frame(data):
eth_type = struct.unpack('!H', data[12:14])[0]
protocol = ord(data[23])
protocol = data[23] if six.PY3 else ord(data[23])
src_port = struct.unpack('!H', data[34:36])[0]
dst_port = struct.unpack('!H', data[36:38])[0]
msg_type = ord(data[42])
msg_type = data[42] if six.PY3 else ord(data[42])
# Make sure we got a DHCP Offer
if eth_type == ETH_P_IP \
and protocol == socket.IPPROTO_UDP \
and src_port == 67 \
and dst_port == 68 \
and msg_type == 2: # DHCP Boot Reply
server_ip_address = '.'.join(["%s" % ord(m) for m in
data[26:30]])
server_hw_address = ":".join(["%02x" % ord(m) for m in
data[6:12]])
if six.PY3:
server_ip_address = '.'.join(["%s" % m for m in
data[26:30]])
server_hw_address = ":".join(["%02x" % m for m in
data[6:12]])
else:
server_ip_address = '.'.join(["%s" % ord(m) for m in
data[26:30]])
server_hw_address = ":".join(["%02x" % ord(m) for m in
data[6:12]])
dhcp_servers.append([server_ip_address, server_hw_address])