diff --git a/core/networking.py b/core/networking.py index 79db0e0..e0006c1 100644 --- a/core/networking.py +++ b/core/networking.py @@ -1,45 +1,60 @@ +""" +This module provides functions for enabling/disabling IP forwarding +and retrieving the IP address of the current machine. +""" + import subprocess import socket -import core.platform as platform +from core import platform def enable_ip_forwarding(): + """ + Enables IP forwarding on the current machine. + IP forwarding is enabled based on the operating system type + (macOS, Linux, or Windows). + """ os_platform = platform.get_os() - + cmd = None if os_platform == 'mac': cmd = ['/usr/sbin/sysctl', '-w', 'net.inet.ip.forwarding=1'] elif os_platform == 'linux': cmd = ['sysctl', '-w', 'net.ipv4.ip_forward=1'] elif os_platform == 'windows': cmd = ['powershell', 'Set-NetIPInterface', '-Forwarding', 'Enabled'] - + else: + raise ValueError(f"Unsupported OS platform: {os_platform}") assert subprocess.call(cmd) == 0 def disable_ip_forwarding(): + """ + Disables IP forwarding on the current machine. + IP forwarding is disabled based on the operating system type + (macOS, Linux, or Windows). + """ os_platform = platform.get_os() - + cmd = None if os_platform == 'mac': cmd = ['/usr/sbin/sysctl', '-w', 'net.inet.ip.forwarding=0'] elif os_platform == 'linux': cmd = ['sysctl', '-w', 'net.ipv4.ip_forward=0'] elif os_platform == 'windows': cmd = ['powershell', 'Set-NetIPInterface', '-Forwarding', 'Disabled'] + else: + raise ValueError(f"Unsupported OS platform: {os_platform}") assert subprocess.call(cmd) == 0 def get_ip_address(): """Get the IP address of the current machine.""" try: - # Create a socket object with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: - # Connect to a remote server (doesn't have to be reachable) s.connect(("8.8.8.8", 80)) - # Get the IP address of the current machine ip_address = s.getsockname()[0] return ip_address - except Exception as e: + except OSError as e: print(f"Error while getting IP address: {e}") return None diff --git a/core/vendor.py b/core/vendor.py index 3bd9fe8..bf98de3 100644 --- a/core/vendor.py +++ b/core/vendor.py @@ -53,11 +53,15 @@ def _load_from_file(cls, filename): """ with open(filename, 'r', encoding='utf-8') as file: csvreader = csv.reader(file) - next(csvreader) # Skip header mac_vendor_data = {} for row in csvreader: - oui = row[1].replace("-", "").upper()[:6] - vendor = row[2] + # Ensure the row has at least 2 elements (OUI and vendor name) + if len(row) < 2: + continue + + oui = row[0].replace("-", "").upper()[:6] # OUI is in the first column (index 0) + vendor = row[1] # Vendor name is in the second column (index 1) + print(f"Processing OUI: {oui}, Vendor: {vendor}") mac_vendor_data[oui] = vendor return mac_vendor_data