Skip to content

netutils: add DHCP-provided NTP server for ntpc#3451

Merged
acassis merged 2 commits intoapache:masterfrom
masc2008:dhcp-ntp-option42-v2
Apr 24, 2026
Merged

netutils: add DHCP-provided NTP server for ntpc#3451
acassis merged 2 commits intoapache:masterfrom
masc2008:dhcp-ntp-option42-v2

Conversation

@masc2008
Copy link
Copy Markdown
Contributor

@masc2008 masc2008 commented Apr 12, 2026

Summary

  • request and parse DHCP option 42 in dhcpc

  • store DHCP-provided IPv4 NTP server addresses in struct dhcpc_state

  • pass the learned NTP server list through DHCP IPv4 bring-up

  • let ntpc fall back to DHCP-provided servers when CONFIG_NETUTILS_NTPCLIENT_SERVER is empty

  • keep configured NTP servers as the highest-priority source

  • If ntpc is running with the configured default and DHCP later provides NTP servers, ntpc restarts and
    switches to the DHCP list.

  • If ntpc is already running with DHCP-provided servers and the DHCP NTP list changes, ntpc restarts
    and uses the updated DHCP list.

  • If ntpc previously had no available server and did not start, receiving DHCP option 42 later will
    automatically start ntpc.

  • If DHCP no longer provides NTP servers, ntpc falls back to CONFIG_NETUTILS_NTPCLIENT_SERVER when
    configured.

Impact

  • Is new feature added? YES, ntpc can now use DHCP-provided NTP servers.
  • Impact on user (will user need to adapt to change)? NO.
  • Impact on build (will build process change)? NO.
  • Impact on hardware (will arch(s) / board(s) / driver(s) change)? NO.
  • Impact on documentation (is update required / provided)? NO.
  • Impact on security (any sort of implications)? NO.
  • Impact on compatibility (backward/forward/interoperability)? NO. Configured NTP servers still take precedence.
  • Anything else to consider or add? DHCP fallback is used only when CONFIG_NETUTILS_NTPCLIENT_SERVER is empty.

Testing

I confirm that changes are verified on local setup:

  • Build Host(s): Linux x86_64, GCC 13.4.0 toolchain
  • Target(s): arm/qemu-armv7a:full
  • ../nuttx/tools/checkpatch.sh -m -g HEAD~2..HEAD
  • local integration build against the matching NuttX tree progressed through final link for arm/qemu-armv7a:full

start dhcp server with dnsmasq:

dnsmasq --no-daemon --log-dhcp --log-queries \
    --interface=tap0 --bind-interfaces \
    --dhcp-authoritative \
    --dhcp-range=192.168.50.20,192.168.50.50,255.255.255.0 \
    --dhcp-option=option:router,192.168.50.1 \
    --dhcp-option=option:dns-server,1.1.1.1 \
    --dhcp-option=option:ntp-server,162.159.200.123

related logs:

Details

dhcpc_request: Received ACK
dhcpc_request: Got IP address 192.168.50.42
dhcpc_request: Got netmask 255.255.255.0
dhcpc_request: Got DNS server 0: 1.1.1.1
dhcpc_request: Got NTP server 0: 162.159.200.123
dhcpc_request: Got default router 192.168.50.1
dhcpc_request: Lease expires in 3600 seconds
.
.
.
ntpc_daemon: NTP sample[0]: offset: 1.000 sec, round-trip delay: 0.056 sec
ntpc_daemon: NTP sample[1]: offset: 1.000 sec, round-trip delay: 0.057 sec
ntpc_daemon: NTP sample[2]: offset: 1.000 sec, round-trip delay: 0.056 sec
ntpc_daemon: NTP sample[3]: offset: 1.001 sec, round-trip delay: 0.057 sec
ntpc_daemon: NTP sample[4]: offset: 1.001 sec, round-trip delay: 0.059 sec
ntpc_settime: Set time to 1775977211.017 seconds (offset: 1.000).

@cederom
Copy link
Copy Markdown
Contributor

cederom commented Apr 12, 2026

Thank you @masc2008 very nice useful feature :-)
Please resolve branch conflicts :-)

@masc2008
Copy link
Copy Markdown
Contributor Author

Thank you @masc2008 very nice useful feature :-) Please resolve branch conflicts :-)

oh, I can't image that you keep eyes on those PR in a so timely style. you just replied while I am still working on this PR. I am quite new to the PR process, thanks for understanding.

@cederom
Copy link
Copy Markdown
Contributor

cederom commented Apr 12, 2026

Thank you @masc2008 very nice useful feature :-) Please resolve branch conflicts :-)

oh, I can't image that you keep eyes on those PR in a so timely style. you just replied while I am still working on this PR. I am quite new to the PR process, thanks for understanding.

no worries and have fun :-) i am just working on some NuttX code too :-)

Comment thread include/netutils/ntpclient.h Outdated
@cederom
Copy link
Copy Markdown
Contributor

cederom commented Apr 12, 2026

Can you please provide in the PR description runtime logs showing that/how the solution works?

Can you please provide a documentation update with information about this feature?

Thanks again @masc2008 :-)

@masc2008
Copy link
Copy Markdown
Contributor Author

Can you please provide in the PR description runtime logs showing that/how the solution works?

Can you please provide a documentation update with information about this feature?

Thanks again @masc2008 :-)

sure, will do it later.

@masc2008 masc2008 force-pushed the dhcp-ntp-option42-v2 branch 2 times, most recently from bbdb39f to 68ce878 Compare April 12, 2026 04:59
Copy link
Copy Markdown
Contributor Author

@masc2008 masc2008 left a comment

Choose a reason for hiding this comment

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

code should be ready now, will keep on working on doc later.

@masc2008 masc2008 marked this pull request as ready for review April 12, 2026 05:01
@masc2008 masc2008 force-pushed the dhcp-ntp-option42-v2 branch from 68ce878 to a4873bb Compare April 12, 2026 07:54
@masc2008
Copy link
Copy Markdown
Contributor Author

Can you please provide in the PR description runtime logs showing that/how the solution works?
Can you please provide a documentation update with information about this feature?
Thanks again @masc2008 :-)

sure, will do it later.

updated in PR, added related info in PR.

simbit18
simbit18 previously approved these changes Apr 13, 2026
Comment thread include/netutils/dhcpc.h Outdated
Comment thread netutils/netlib/netlib_obtainipv4addr.c
Comment thread netutils/ntpclient/ntpclient.c Outdated
Comment thread netutils/netlib/netlib_obtainipv4addr.c
Request DHCP option 42 and store the returned IPv4 NTP server
addresses in struct dhcpc_state for later consumers.

Signed-off-by: Jerry Ma <masc2008@gmail.com>
@masc2008
Copy link
Copy Markdown
Contributor Author

Hi @masc2008, please fix

====================================================================================
Configuration/Tool: lx_cpu/nsh,CONFIG_ARM_TOOLCHAIN_GNU_EABI
2026-04-13 11:41:57
------------------------------------------------------------------------------------
  Cleaning...
  Configuring...
  Disabling CONFIG_ARM_TOOLCHAIN_GNU_EABI
  Enabling CONFIG_ARM_TOOLCHAIN_GNU_EABI
  Building NuttX...
netlib_obtainipv4addr.c: In function 'dhcp_set_ntp_servers':
Error: netlib_obtainipv4addr.c:118:24: error: 'CONFIG_NETUTILS_DHCPC_NTP_SERVER_NUM' undeclared (first use in this function); did you mean 'CONFIG_NETUTILS_DHCPC_NTP_SERVERS'?
  118 |   char ntp_server_list[CONFIG_NETUTILS_DHCPC_NTP_SERVER_NUM *
      |                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                        CONFIG_NETUTILS_DHCPC_NTP_SERVERS
netlib_obtainipv4addr.c:118:24: note: each undeclared identifier is reported only once for each function it appears in
Error: netlib_obtainipv4addr.c:118:8: error: unused variable 'ntp_server_list' [-Werror=unused-variable]
  118 |   char ntp_server_list[CONFIG_NETUTILS_DHCPC_NTP_SERVER_NUM *
      |        ^~~~~~~~~~~~~~~
Error: netlib_obtainipv4addr.c:163:1: error: control reaches end of non-void function [-Werror=return-type]
  163 | }
      | ^
cc1: all warnings being treated as errors
make[2]: *** [/github/workspace/sources/apps/Application.mk:239: netlib_obtainipv4addr.c.github.workspace.sources.apps.netutils.netlib.o] Error 1
make[2]: Target 'all' not remade because of errors.
make[1]: *** [Makefile:54: /github/workspace/sources/apps/netutils/netlib_all] Error 2
make[1]: Target 'all' not remade because of errors.
make: *** [tools/LibTargets.mk:248: /github/workspace/sources/apps/libapps.a] Error 2
make: Target 'all' not remade because of errors.
/github/workspace/sources/nuttx/tools/testbuild.sh: line 385: /github/workspace/sources/nuttx/../nuttx/nuttx.manifest: No such file or directory
  [1/1] Normalize lx_cpu/nsh
====================================================================================

sorry, I missed this one when last commit.

@masc2008 masc2008 force-pushed the dhcp-ntp-option42-v2 branch from 9f18374 to db11971 Compare April 14, 2026 04:01
@masc2008 masc2008 changed the title netutils: add DHCP-provided NTP server fallback for ntpc netutils: add DHCP-provided NTP server for ntpc Apr 14, 2026
Copy link
Copy Markdown
Contributor Author

@masc2008 masc2008 left a comment

Choose a reason for hiding this comment

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

updated version, please review.

jerpelea
jerpelea previously approved these changes Apr 14, 2026
Use the DHCP-learned NTP server list as the default ntpc server source when DHCP option 42 is available.

Fall back to CONFIG_NETUTILS_NTPCLIENT_SERVER only when DHCP does not provide any NTP servers, and restart ntpc when the DHCP-provided server list changes.

Signed-off-by: Jerry Ma <masc2008@gmail.com>
@acassis
Copy link
Copy Markdown
Contributor

acassis commented Apr 15, 2026

@masc2008 nice work! Please update https://nuttx.apache.org/docs/latest/applications/system/ntpc/index.html to explain that NTP now can be provided over DHCP. Please include there the details you added here in the Summary.
This is a nice feature and if it is not Documented (and explained how to use/test) it becomes another hidden feature of NuttX

Copy link
Copy Markdown
Contributor

@acassis acassis left a comment

Choose a reason for hiding this comment

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

Please update Documentation/ (to reflect at https://nuttx.apache.org/docs/latest/applications/system/ntpc/index.html ) and include the details from Summary showing how to test it.

@masc2008
Copy link
Copy Markdown
Contributor Author

Please update Documentation/ (to reflect at https://nuttx.apache.org/docs/latest/applications/system/ntpc/index.html ) and include the details from Summary showing how to test it.

Here is doc: apache/nuttx#18752

@masc2008 masc2008 requested a review from acassis April 17, 2026 02:11
Copy link
Copy Markdown
Contributor

@cederom cederom left a comment

Choose a reason for hiding this comment

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

Thank you @masc2008 great work :-)

Comment thread netutils/netlib/netlib_dhcp_ntp.c
@acassis
Copy link
Copy Markdown
Contributor

acassis commented Apr 23, 2026

@masc2008 you missed to reply @xiaoxiang781216 question about "g_dhcp_ntp_servers and g_dhcp_ntp_callback"

@masc2008
Copy link
Copy Markdown
Contributor Author

masc2008 commented Apr 24, 2026

@xiaoxiang781216 , @acassis
I am sorry I didn't notice this early, in fact I replied, while it shows "pending"
Here is the reason:
g_dhcp_ntp_callback and g_dhcp_ntp_servers serve different purposes:

g_dhcp_ntp_callback is the current subscriber.
g_dhcp_ntp_servers is the last known DHCP option 42 state.
image

@acassis acassis merged commit c829571 into apache:master Apr 24, 2026
41 checks passed
@acassis
Copy link
Copy Markdown
Contributor

acassis commented Apr 24, 2026

@xiaoxiang781216 , @acassis I am sorry I didn't notice this early, in fact I replied, while it shows "pending" Here is the reason: g_dhcp_ntp_callback and g_dhcp_ntp_servers serve different purposes:

g_dhcp_ntp_callback is the current subscriber.
g_dhcp_ntp_servers is the last known DHCP option 42 state.
image

Please notice the "Pending" in the image. It means you forgot to submit the reply

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants