Skip to content

feat(dhcp-server): support configurable UDP ports#3699

Open
poroh wants to merge 1 commit into
NVIDIA:mainfrom
poroh:dhcp-server-configurable-ports
Open

feat(dhcp-server): support configurable UDP ports#3699
poroh wants to merge 1 commit into
NVIDIA:mainfrom
poroh:dhcp-server-configurable-ports

Conversation

@poroh

@poroh poroh commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Allow forge-dhcp-server to use configurable UDP ports instead of requiring the standard privileged DHCP ports in every environment.

The server previously hardcoded both its listen address to 0.0.0.0:67 and relayed response destinations to UDP port 67. This prevented local and integration environments from exercising the real DHCP packet path without elevated privileges.

This change adds:

  • --listen-addr, defaulting to 0.0.0.0:67.
  • --relay-response-port, defaulting to 67.

The defaults preserve existing production behavior. Direct client and broadcast responses continue to use UDP port 68. Because the server creates an IPv4 socket, IPv6 listen addresses are rejected during argument parsing instead of failing later during socket setup.

Related issues

#3366

Type of Change

  • Add - New feature or capability
  • Change - Changes in existing functionality
  • Fix - Bug fixes
  • Remove - Removed features or deprecated functionality
  • Internal - Internal changes (refactoring, tests, docs, etc.)

Breaking Changes

  • This PR contains breaking changes

Testing

  • Unit tests added/updated
  • Integration tests added/updated
  • Manual testing performed
  • No testing required (docs, internal refactor, etc.)

Additional Notes

Signed-off-by: Dmitry Porokh <dporokh@nvidia.com>
@poroh
poroh requested a review from a team as a code owner July 17, 2026 22:13
@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Summary by CodeRabbit

  • New Features

    • Added configurable IPv4 listening address and relay response port options to the DHCP server.
    • DHCP relay responses now use the configured destination port.
    • Added validation to reject IPv6 listening addresses.
  • Bug Fixes

    • DHCP server sockets now bind to the configured address instead of a fixed port.
  • Tests

    • Added coverage for default settings, command-line overrides, invalid addresses, and relay response routing.

Walkthrough

DHCP server CLI arguments now configure the IPv4 listen address and relay response port. These values propagate through server configuration to UDP binding and relayed packet routing, with tests covering defaults, overrides, IPv6 rejection, and destination ports.

Changes

DHCP socket configuration

Layer / File(s) Summary
CLI endpoint contract and socket binding
crates/dhcp-server/src/command_line.rs, crates/dhcp-server/src/util.rs, crates/dhcp-server/src/main.rs
Adds IPv4 listen_addr and relay_response_port arguments with defaults and uses the configured listen address for UDP socket binding.
Relay response routing
crates/dhcp-server/src/main.rs, crates/dhcp-server/src/packet_handler.rs
Propagates relay_response_port through Config and uses it for relayed DHCP response destinations.
Configuration integration tests
crates/dhcp-server/src/main.rs, crates/dhcp-server/src/command_line.rs
Tests argument parsing, IPv6 rejection, configurable relay ports, and broadcast reply destinations.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CLI
  participant DHCPServer
  participant Config
  participant PacketHandler
  participant UDPSocket
  CLI->>DHCPServer: Provide listen_addr and relay_response_port
  DHCPServer->>Config: Store relay_response_port
  DHCPServer->>UDPSocket: Bind listen_addr per interface
  PacketHandler->>Config: Read relay_response_port
  PacketHandler->>UDPSocket: Send relayed response to configured port
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: configurable UDP ports in the DHCP server.
Description check ✅ Passed The description accurately describes the new listen and relay port options and the IPv6 rejection behavior.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
crates/dhcp-server/src/command_line.rs (1)

85-121: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Make the CLI parser test table-driven.

dhcp_port_arguments hard-codes several argv/result cases in one linear test. Since this maps inputs to parsed values and errors, use a table of cases (check_cases/check_values or an equivalent) covering defaults, overrides, IPv6 rejection, and port-zero rejection.

As per coding guidelines, tests mapping inputs to outputs or errors should be table-driven.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/dhcp-server/src/command_line.rs` around lines 85 - 121, Convert the
dhcp_port_arguments test into a table-driven structure with separate cases for
defaults, overridden values, IPv6 listen-address rejection, and port-zero
rejection. Add reusable helpers or equivalent case handling to assert expected
parsed values and errors, while preserving the current defaults and override
expectations.

Source: Coding guidelines

crates/dhcp-server/src/packet_handler.rs (1)

191-198: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Add coverage for direct-client unicast routing.

The configurable port should affect only relayed packets. Current assertions cover a custom relay destination and broadcast port 68, but no case exercises giaddr == 0 with ciaddr != 0; add one asserting that direct-client replies still target port 68.

Based on path instructions, DHCP/networking changes should include tests for routing correctness and boundary behavior.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/dhcp-server/src/packet_handler.rs` around lines 191 - 198, Add a
routing test covering the direct-client case in decide_dst_ip: use a packet with
giaddr set to 0 and ciaddr nonzero, configure a custom relay response port, and
assert the destination remains the client address on port 68. Keep existing
relayed and broadcast assertions unchanged.

Source: Path instructions

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/dhcp-server/src/command_line.rs`:
- Around line 28-40: Update the argument parsing for listen_addr and
relay_response_port in the command-line configuration to reject port 0 before
server startup. Validate the embedded SocketAddrV4 port and the
relay_response_port value, preserving valid non-zero ports, and add regression
cases covering zero-port inputs for both fields.

---

Nitpick comments:
In `@crates/dhcp-server/src/command_line.rs`:
- Around line 85-121: Convert the dhcp_port_arguments test into a table-driven
structure with separate cases for defaults, overridden values, IPv6
listen-address rejection, and port-zero rejection. Add reusable helpers or
equivalent case handling to assert expected parsed values and errors, while
preserving the current defaults and override expectations.

In `@crates/dhcp-server/src/packet_handler.rs`:
- Around line 191-198: Add a routing test covering the direct-client case in
decide_dst_ip: use a packet with giaddr set to 0 and ciaddr nonzero, configure a
custom relay response port, and assert the destination remains the client
address on port 68. Keep existing relayed and broadcast assertions unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: c36e4a20-9371-4c0e-b9d8-cedb3553fef3

📥 Commits

Reviewing files that changed from the base of the PR and between 0e59cee and 4db299d.

📒 Files selected for processing (4)
  • crates/dhcp-server/src/command_line.rs
  • crates/dhcp-server/src/main.rs
  • crates/dhcp-server/src/packet_handler.rs
  • crates/dhcp-server/src/util.rs

Comment on lines +28 to +40
#[arg(
long,
help = "UDP address where the DHCP server listens.",
default_value = "0.0.0.0:67"
)]
pub listen_addr: SocketAddrV4,

#[arg(
long,
help = "UDP destination port for responses to DHCP relays.",
default_value_t = 67
)]
pub relay_response_port: u16,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Reject port 0 during argument parsing.

Both new fields currently accept zero: SocketAddrV4 permits :0, causing an ephemeral server bind, while relay_response_port = 0 is not a usable DHCP relay destination. Validate both ports as non-zero before starting the server and add regression cases.

As per path instructions, DHCP/networking changes must be reviewed for address-allocation correctness and boundary behavior.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/dhcp-server/src/command_line.rs` around lines 28 - 40, Update the
argument parsing for listen_addr and relay_response_port in the command-line
configuration to reject port 0 before server startup. Validate the embedded
SocketAddrV4 port and the relay_response_port value, preserving valid non-zero
ports, and add regression cases covering zero-port inputs for both fields.

Source: Path instructions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant