From 73796053a15bb017cdcfae6db6c27c1b4eb12a22 Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Wed, 2 Jan 2013 11:37:29 +0100 Subject: [PATCH] Add SNTP option support --- src/dhcpv6.c | 22 +++++++++++++++++++++- src/odhcp6c.c | 2 ++ src/odhcp6c.h | 9 +++++++++ src/script.c | 6 +++++- 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/dhcpv6.c b/src/dhcpv6.c index f1122bd..e93d894 100644 --- a/src/dhcpv6.c +++ b/src/dhcpv6.c @@ -573,11 +573,13 @@ static int dhcpv6_handle_reply(_unused enum dhcpv6_msg orig, t1 = t2 = t3 = 86400; - size_t ia_na_len, dns_len, search_len; + size_t ia_na_len, dns_len, search_len, sntp_ip_len, sntp_dns_len; uint8_t *ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len); uint8_t *ia_end; odhcp6c_get_state(STATE_DNS, &dns_len); odhcp6c_get_state(STATE_SEARCH, &search_len); + odhcp6c_get_state(STATE_SNTP_IP, &sntp_ip_len); + odhcp6c_get_state(STATE_SNTP_FQDN, &sntp_dns_len); // Decrease valid and preferred lifetime of prefixes size_t ia_pd_len; @@ -649,6 +651,20 @@ static int dhcpv6_handle_reply(_unused enum dhcpv6_msg orig, odhcp6c_add_state(STATE_DNS, odata, olen); } else if (otype == DHCPV6_OPT_DNS_DOMAIN) { odhcp6c_add_state(STATE_SEARCH, odata, olen); + } else if (otype == DHCPV6_OPT_NTP_SERVER) { + uint16_t stype, slen; + uint8_t *sdata; + // Test status and bail if error + dhcpv6_for_each_option(odata, odata + olen, + stype, slen, sdata) { + if (slen == 16 && (stype == NTP_MC_ADDR || + stype == NTP_SRV_ADDR)) + odhcp6c_add_state(STATE_SNTP_IP, + sdata, slen); + else if (slen > 0 && stype == NTP_SRV_FQDN) + odhcp6c_add_state(STATE_SNTP_FQDN, + sdata, slen); + } } else if (otype == DHCPV6_OPT_INFO_REFRESH && olen >= 4) { uint32_t refresh = ntohl(*((uint32_t*)odata)); if (refresh < (uint32_t)t1) @@ -663,6 +679,10 @@ static int dhcpv6_handle_reply(_unused enum dhcpv6_msg orig, if (opt) { have_update |= odhcp6c_commit_state(STATE_DNS, dns_len); have_update |= odhcp6c_commit_state(STATE_SEARCH, search_len); + have_update |= odhcp6c_commit_state(STATE_SNTP_IP, + sntp_ip_len); + have_update |= odhcp6c_commit_state(STATE_SNTP_FQDN, + sntp_dns_len); size_t new_ia_pd_len, new_ia_na_len; odhcp6c_get_state(STATE_IA_PD, &new_ia_pd_len); odhcp6c_get_state(STATE_IA_NA, &new_ia_na_len); diff --git a/src/odhcp6c.c b/src/odhcp6c.c index 9aaa008..f3634d2 100644 --- a/src/odhcp6c.c +++ b/src/odhcp6c.c @@ -171,6 +171,8 @@ int main(_unused int argc, char* const argv[]) odhcp6c_clear_state(STATE_SERVER_CAND); odhcp6c_clear_state(STATE_IA_PD); odhcp6c_clear_state(STATE_IA_PD_LOST); + odhcp6c_clear_state(STATE_SNTP_IP); + odhcp6c_clear_state(STATE_SNTP_FQDN); dhcpv6_set_ia_na_mode(ia_na_mode); alarm(timeout); diff --git a/src/odhcp6c.h b/src/odhcp6c.h index 2d4fa90..2b3ce9f 100644 --- a/src/odhcp6c.h +++ b/src/odhcp6c.h @@ -48,6 +48,13 @@ enum dhcvp6_opt { DHCPV6_OPT_IA_PREFIX = 26, DHCPV6_OPT_INFO_REFRESH = 32, DHCPV6_OPT_FQDN = 39, + DHCPV6_OPT_NTP_SERVER = 56, +}; + +enum dhcpv6_opt_npt { + NTP_SRV_ADDR = 1, + NTP_MC_ADDR = 2, + NTP_SRV_FQDN = 3 }; enum dhcpv6_msg { @@ -150,6 +157,8 @@ enum odhcp6c_state { STATE_IA_PD, STATE_IA_PD_LOST, STATE_CUSTOM_OPTS, + STATE_SNTP_IP, + STATE_SNTP_FQDN, _STATE_MAX }; diff --git a/src/script.c b/src/script.c index 2117aeb..d082ff6 100644 --- a/src/script.c +++ b/src/script.c @@ -159,10 +159,12 @@ static void prefix_to_env(const char *name, const uint8_t *fqdn, size_t len) void script_call(const char *status) { syslog(LOG_WARNING, "State for %s changed to %s", argv[1], status); - size_t dns_len, search_len, custom_len; + size_t dns_len, search_len, custom_len, sntp_ip_len, sntp_dns_len; struct in6_addr *dns = odhcp6c_get_state(STATE_DNS, &dns_len); uint8_t *search = odhcp6c_get_state(STATE_SEARCH, &search_len); uint8_t *custom = odhcp6c_get_state(STATE_CUSTOM_OPTS, &custom_len); + struct in6_addr *sntp = odhcp6c_get_state(STATE_SNTP_IP, &sntp_ip_len); + uint8_t *sntp_dns = odhcp6c_get_state(STATE_SNTP_FQDN, &sntp_dns_len); size_t prefix_len, lost_pd_len; uint8_t *prefix = odhcp6c_get_state(STATE_IA_PD, &prefix_len); @@ -171,7 +173,9 @@ void script_call(const char *status) // Don't set environment before forking, because env is leaky. if (fork() == 0) { ipv6_to_env("RDNSS", dns, dns_len / sizeof(*dns)); + ipv6_to_env("SNTP_IP", sntp, sntp_ip_len / sizeof(*sntp)); fqdn_to_env("DOMAINS", search, search_len); + fqdn_to_env("SNTP_FQDN", sntp_dns, sntp_dns_len); bin_to_env(custom, custom_len); prefix_to_env("PREFIXES", prefix, prefix_len); prefix_to_env("PREFIXES_LOST", lost_pd, lost_pd_len); -- 2.25.1