X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fodhcpd.git;a=blobdiff_plain;f=src%2Fodhcpd.c;h=8a1c66e6721a37c83af600ba8b29ef401744e29b;hp=9a76e4d638ee54b3a0ffcd4592c6b383479e126e;hb=93abe6f02ad6e050996c4ead2481f103417df64f;hpb=51c756cfc15c63322df9fdb70d5c701cfb6b9a9f diff --git a/src/odhcpd.c b/src/odhcpd.c index 9a76e4d..8a1c66e 100644 --- a/src/odhcpd.c +++ b/src/odhcpd.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -52,7 +53,6 @@ static int ioctl_sock; static struct nl_sock *rtnl_socket = NULL; static int urandom_fd = -1; -static int log_level = LOG_INFO; static void sighandler(_unused int signal) { @@ -65,7 +65,7 @@ static void print_usage(const char *app) "== %s Usage ==\n\n" " -h, --help Print this help\n" " -l level Specify log level 0..7 (default %d)\n", - app, log_level + app, config.log_level ); } @@ -80,12 +80,12 @@ int main(int argc, char **argv) print_usage(argv[0]); return 0; case 'l': - log_level = atoi(optarg); - fprintf(stderr, "Log level set to %d\n", log_level); + config.log_level = (atoi(optarg) & LOG_PRIMASK); + fprintf(stderr, "Log level set to %d\n", config.log_level); break; } } - setlogmask(LOG_UPTO(log_level)); + setlogmask(LOG_UPTO(config.log_level)); uloop_init(); if (getuid() != 0) { @@ -336,7 +336,7 @@ out: return ctxt.ret; } -int odhcpd_get_linklocal_interface_address(int ifindex, struct in6_addr *lladdr) +static int odhcpd_get_linklocal_interface_address(int ifindex, struct in6_addr *lladdr) { int status = -1; struct sockaddr_in6 addr = {AF_INET6, 0, 0, ALL_IPV6_ROUTERS, ifindex}; @@ -354,6 +354,51 @@ int odhcpd_get_linklocal_interface_address(int ifindex, struct in6_addr *lladdr) return status; } +/* + * DNS address selection criteria order : + * - use IPv6 address with valid lifetime if none is yet selected + * - use IPv6 address with a preferred lifetime if the already selected IPv6 address is deprecated + * - use an IPv6 ULA address if the already selected IPv6 address is not an ULA address + * - use the IPv6 address with the longest preferred lifetime + */ +int odhcpd_get_interface_dns_addr(const struct interface *iface, struct in6_addr *addr) +{ + time_t now = odhcpd_time(); + ssize_t m = -1; + + for (size_t i = 0; i < iface->ia_addr_len; ++i) { + if (iface->ia_addr[i].valid <= (uint32_t)now) + continue; + + if (m < 0) { + m = i; + continue; + } + + if (iface->ia_addr[m].preferred >= (uint32_t)now && + iface->ia_addr[i].preferred < (uint32_t)now) + continue; + + if (IN6_IS_ADDR_ULA(&iface->ia_addr[i].addr)) { + if (!IN6_IS_ADDR_ULA(&iface->ia_addr[m].addr)) { + m = i; + continue; + } + } else if (IN6_IS_ADDR_ULA(&iface->ia_addr[m].addr)) + continue; + + if (iface->ia_addr[i].preferred > iface->ia_addr[m].preferred) + m = i; + } + + if (m >= 0) { + *addr = iface->ia_addr[m].addr; + return 0; + } + + return odhcpd_get_linklocal_interface_address(iface->ifindex, addr); +} + int odhcpd_setup_route(const struct in6_addr *addr, const int prefixlen, const struct interface *iface, const struct in6_addr *gw, const uint32_t metric, const bool add)