Skip to content

Commit

Permalink
dhcp client: implement a hook to add default route
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianBaboch committed Jan 24, 2025
1 parent 0a16dd9 commit d19d44c
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pyroute2/dhcp/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,34 @@ async def unbound(self, lease: Lease):
mask=lease.subnet_mask,
broadcast=lease.broadcast_address
)


class ConfigureDefaultRoute(Hook):
async def bound(self, lease: Lease):
if lease.default_gateway is None:
LOG.error("Lease doesn't contain default gateway")
return
LOG.debug('Adding %s as default route through %s',
lease.default_gateway, lease.interface)
async with AsyncIPRoute() as ipr:
ifindex = await ipr.link_lookup(ifname=lease.interface),
await ipr.route(
"replace",
dst="0.0.0.0/0",
gateway=lease.default_gateway,
oif=ifindex
)

async def unbound(self, lease: Lease):
if lease.default_gateway is None:
LOG.error("Lease doesn't contain default gateway")
return
LOG.debug('Removing %s as default route.', lease.default_gateway)
async with AsyncIPRoute() as ipr:
ifindex = await ipr.link_lookup(ifname=lease.interface)
await ipr.route(
"del",
dst="0.0.0.0/0",
gateway=lease.default_gateway,
oif=ifindex
)

0 comments on commit d19d44c

Please sign in to comment.