From ed96b4d49d436643f769e1f9d32e8fc7a96a1c73 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Wed, 23 Apr 2014 16:04:47 +0200 Subject: [PATCH] nameserv: prepare for asynchronous resolving Introduce a new function with callback to resolve names to IP addresses asynchronously. For now, use a blocking wrapper around DNS_Name2IPAddress. --- Makefile.in | 2 +- nameserv_async.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ nameserv_async.h | 41 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 nameserv_async.c create mode 100644 nameserv_async.h diff --git a/Makefile.in b/Makefile.in index 010d03f..5ea20fc 100644 --- a/Makefile.in +++ b/Makefile.in @@ -42,7 +42,7 @@ OBJS = util.o sched.o regress.o local.o \ sys.o main.o ntp_io.o ntp_core.o ntp_sources.o \ sources.o sourcestats.o reference.o \ logging.o conf.o cmdmon.o keys.o \ - nameserv.o manual.o addrfilt.o \ + nameserv.o nameserv_async.o manual.o addrfilt.o \ cmdparse.o mkdirpp.o rtc.o pktlength.o clientlog.o \ broadcast.o refclock.o refclock_phc.o refclock_pps.o \ refclock_shm.o refclock_sock.o tempcomp.o $(HASH_OBJ) diff --git a/nameserv_async.c b/nameserv_async.c new file mode 100644 index 0000000..b11da86 --- /dev/null +++ b/nameserv_async.c @@ -0,0 +1,49 @@ +/* + chronyd/chronyc - Programs for keeping computer clocks accurate. + + ********************************************************************** + * Copyright (C) Miroslav Lichvar 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + ********************************************************************** + + ======================================================================= + + Functions to asynchronously convert name to IP address + + */ + +#include "config.h" +#include "sysincl.h" + +#include "nameserv_async.h" +#include "logging.h" +#include "util.h" + +/* ================================================== */ + +/* This is a blocking implementation used when nothing else is available */ + +void +DNS_Name2IPAddressAsync(const char *name, DNS_NameResolveHandler handler, void *anything) +{ + IPAddr addr; + DNS_Status status; + + status = DNS_Name2IPAddress(name, &addr); + (handler)(status, &addr, anything); +} + +/* ================================================== */ diff --git a/nameserv_async.h b/nameserv_async.h new file mode 100644 index 0000000..5c1704e --- /dev/null +++ b/nameserv_async.h @@ -0,0 +1,41 @@ +/* + chronyd/chronyc - Programs for keeping computer clocks accurate. + + ********************************************************************** + * Copyright (C) Miroslav Lichvar 2014 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + ********************************************************************** + + ======================================================================= + + Header for asynchronous nameserver functions + */ + + +#ifndef GOT_NAMESERV_ASYNC_H +#define GOT_NAMESERV_ASYNC_H + +#include "nameserv.h" + +/* Function type for callback to process the result */ +typedef void (*DNS_NameResolveHandler)(DNS_Status status, IPAddr *ip_addr, void *anything); + +/* Request resolving of a name to IP address. The handler will be + called when the result is available, but it may be also called + directly from this function call. */ +extern void DNS_Name2IPAddressAsync(const char *name, DNS_NameResolveHandler handler, void *anything); + +#endif