Skip to content
Snippets Groups Projects
Commit 515fbd39 authored by Jussi Kivilinna's avatar Jussi Kivilinna Committed by Pekka Niemimaa
Browse files

netutils/ntpclient: drop plain server IP address setting support

Use of hardcoded IP address for NTP server is known bad practice [1].
So lets only allow configuration for server hostnames.

[1] https://en.wikipedia.org/wiki/NTP_server_misuse_and_abuse



Signed-off-by: default avatarJussi Kivilinna <jussi.kivilinna@haltian.com>
parent 651d7850
No related branches found
No related tags found
No related merge requests found
...@@ -12,27 +12,10 @@ config NETUTILS_NTPCLIENT ...@@ -12,27 +12,10 @@ config NETUTILS_NTPCLIENT
if NETUTILS_NTPCLIENT if NETUTILS_NTPCLIENT
config NETUTILS_NTPCLIENT_USE_SERVERHOSTNAME
bool "Use hostname for NTP server IP address"
depends on NETUTILS_DNSCLIENT
default n
if NETUTILS_NTPCLIENT_USE_SERVERHOSTNAME
config NETUTILS_NTPCLIENT_SERVERHOSTNAME config NETUTILS_NTPCLIENT_SERVERHOSTNAME
string "NTP server hostname" string "NTP server hostnames"
default "0.pool.ntp.org" default "0.pool.ntp.org"
endif #NETUTILS_NTPCLIENT_USE_SERVERHOSTNAME
if !NETUTILS_NTPCLIENT_USE_SERVERHOSTNAME
config NETUTILS_NTPCLIENT_SERVERIP
hex "NTP server IP address"
default 0x0a000001
endif #NETUTILS_NTPCLIENT_USE_SERVERHOSTNAME
config NETUTILS_NTPCLIENT_PORTNO config NETUTILS_NTPCLIENT_PORTNO
int "NTP server port number" int "NTP server port number"
default 123 default 123
......
...@@ -58,9 +58,7 @@ ...@@ -58,9 +58,7 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <apps/netutils/ntpclient.h> #include <apps/netutils/ntpclient.h>
#ifdef CONFIG_NETUTILS_NTPCLIENT_USE_SERVERHOSTNAME
#include <apps/netutils/dnsclient.h> #include <apps/netutils/dnsclient.h>
#endif
#include <nuttx/clock.h> #include <nuttx/clock.h>
...@@ -73,6 +71,9 @@ ...@@ -73,6 +71,9 @@
#ifndef CONFIG_HAVE_LONG_LONG #ifndef CONFIG_HAVE_LONG_LONG
# error "64-bit integer support required for NTP client" # error "64-bit integer support required for NTP client"
#endif #endif
#ifndef CONFIG_NETUTILS_NTPCLIENT
# error "DNS client required for NTP client"
#endif
/* NTP Time is seconds since 1900. Convert to Unix time which is seconds /* NTP Time is seconds since 1900. Convert to Unix time which is seconds
* since 1970 * since 1970
...@@ -89,6 +90,17 @@ ...@@ -89,6 +90,17 @@
# define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif #endif
#ifndef CONFIG_NETUTILS_NTPCLIENT_SERVERHOSTNAME
# ifdef CONFIG_NETUTILS_NTPCLIENT_SERVERIP
/* Old config support */
# warning "NTP server hostname not defined, using deprecated server IP address setting"
# define CONFIG_NETUTILS_NTPCLIENT_SERVERHOSTNAME \
inet_ntoa(CONFIG_NETUTILS_NTPCLIENT_SERVERIP)
# else
# error "NTP server hostname not defined"
# endif
#endif
/**************************************************************************** /****************************************************************************
* Private Types * Private Types
****************************************************************************/ ****************************************************************************/
...@@ -678,6 +690,7 @@ static int ntpc_get_ntp_sample(struct ntp_servers_s *srvs, ...@@ -678,6 +690,7 @@ static int ntpc_get_ntp_sample(struct ntp_servers_s *srvs,
int errval; int errval;
bool retry = true; bool retry = true;
int nsamples = curr_idx; int nsamples = curr_idx;
bool addr_ok;
int ret; int ret;
int sd = -1; int sd = -1;
int i; int i;
...@@ -689,12 +702,11 @@ static int ntpc_get_ntp_sample(struct ntp_servers_s *srvs, ...@@ -689,12 +702,11 @@ static int ntpc_get_ntp_sample(struct ntp_servers_s *srvs,
memset(&server, 0, sizeof(struct sockaddr_in)); memset(&server, 0, sizeof(struct sockaddr_in));
server.sin_family = AF_INET; server.sin_family = AF_INET;
server.sin_port = htons(CONFIG_NETUTILS_NTPCLIENT_PORTNO); server.sin_port = htons(CONFIG_NETUTILS_NTPCLIENT_PORTNO);
#ifndef CONFIG_NETUTILS_NTPCLIENT_USE_SERVERHOSTNAME
server.sin_addr.s_addr = htonl(CONFIG_NETUTILS_NTPCLIENT_SERVERIP);
#endif
retry_dns: do
#ifdef CONFIG_NETUTILS_NTPCLIENT_USE_SERVERHOSTNAME {
addr_ok = true;
ret = ntp_get_next_hostip(srvs, &server.sin_addr.s_addr); ret = ntp_get_next_hostip(srvs, &server.sin_addr.s_addr);
if (ret < 0) if (ret < 0)
{ {
...@@ -704,7 +716,6 @@ retry_dns: ...@@ -704,7 +716,6 @@ retry_dns:
goto sock_error; goto sock_error;
} }
#endif
/* Make sure that this sample is from new server. */ /* Make sure that this sample is from new server. */
...@@ -719,7 +730,8 @@ retry_dns: ...@@ -719,7 +730,8 @@ retry_dns:
if (retry) if (retry)
{ {
retry = false; retry = false;
goto retry_dns; addr_ok = false;
break;
} }
else else
{ {
...@@ -728,6 +740,8 @@ retry_dns: ...@@ -728,6 +740,8 @@ retry_dns:
} }
} }
} }
}
while (!addr_ok);
/* Open socket. */ /* Open socket. */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment