Skip to content

Commit

Permalink
util: remove ipv4 generator
Browse files Browse the repository at this point in the history
  • Loading branch information
willcl-ark committed Mar 5, 2024
1 parent 14cf126 commit 325ff00
Showing 1 changed file with 0 additions and 41 deletions.
41 changes: 0 additions & 41 deletions src/warnet/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import functools
import ipaddress
import json
import logging
import os
Expand Down Expand Up @@ -110,46 +109,6 @@ def get_architecture():
return arch


def generate_ipv4_addr(subnet):
"""
Generate a valid random IPv4 address within the given subnet.
:param subnet: Subnet in CIDR notation (e.g., '100.0.0.0/8')
:return: Random IP address within the subnet
"""
reserved_ips = [
"0.0.0.0/8",
"10.0.0.0/8",
"100.64.0.0/10",
"127.0.0.0/8",
"169.254.0.0/16",
"172.16.0.0/12",
"192.0.0.0/24",
"192.0.2.0/24",
"192.88.99.0/24",
"192.168.0.0/16",
"198.18.0.0/15",
"198.51.100.0/24",
"203.0.113.0/24",
"224.0.0.0/4",
]

def is_public(ip):
for reserved in reserved_ips:
if ipaddress.ip_address(ip) in ipaddress.ip_network(reserved, strict=False):
return False
return True

network = ipaddress.ip_network(subnet, strict=False)

# Generate a random IP within the subnet range
while True:
ip_int = random.randint(int(network.network_address), int(network.broadcast_address))
ip_str = str(ipaddress.ip_address(ip_int))
if is_public(ip_str):
return ip_str


def sanitize_tc_netem_command(command: str) -> bool:
"""
Sanitize the tc-netem command to ensure it's valid and safe to execute, as we run it as root on a container.
Expand Down

0 comments on commit 325ff00

Please sign in to comment.