Skip to content

Commit

Permalink
Handle no default route in get_default_ip_address() (#279)
Browse files Browse the repository at this point in the history
A host is not required to have a default gateway, more specific
routes may be present instead.

In this situation get_default_gateway_interface() returns None,
which is reasonable. get_default_ip_address() then fails with a
struct pack error.

This patch allows get_default_ip_address to fall through and also
return None when there is no interface identified as the
default gateway.

Co-authored-by: Jonathan Rosser <jonathan.rosser@rd.bbc.co.uk>
  • Loading branch information
jrosser and Jonathan Rosser authored Dec 27, 2023
1 parent e841cb5 commit 84da957
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions magnum_cluster_api/proxy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ def get_default_gateway_interface():

def get_default_ip_address():
interface = get_default_gateway_interface()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ip_address = socket.inet_ntoa(
fcntl.ioctl(
sock.fileno(),
0x8915, # SIOCGIFADDR
struct.pack("256s", interface.encode("utf-8")[:15]),
)[20:24]
)
return ip_address
if interface:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ip_address = socket.inet_ntoa(
fcntl.ioctl(
sock.fileno(),
0x8915, # SIOCGIFADDR
struct.pack("256s", interface.encode("utf-8")[:15]),
)[20:24]
)
return ip_address


def find_free_port():
Expand Down

0 comments on commit 84da957

Please sign in to comment.