From 4f9ad057ca1391dac5d3ae3b35563185abae36e3 Mon Sep 17 00:00:00 2001 From: Jeffrey Hutzelman Date: Mon, 18 Feb 2013 14:20:12 -0500 Subject: server: c-ares support Add the bits we need to be able to use c-ares for DNS operations in the server. This handles initialization and making sure the resolver's sockets and timeouts are considered in the main loop. --- server/Makefile.in | 2 +- server/global.c | 3 +++ server/main.c | 35 ++++++++++++++++++++++++++++++----- server/zserver.h | 6 ++++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/server/Makefile.in b/server/Makefile.in index a23b509..666e7ec 100644 --- a/server/Makefile.in +++ b/server/Makefile.in @@ -33,7 +33,7 @@ CFLAGS=@CFLAGS@ ALL_CFLAGS=${CFLAGS} -DSYSCONFDIR=\"${sysconfdir}\" -I${top_srcdir}/h \ -I${BUILDTOP}/h -I. ${CPPFLAGS} LDFLAGS=@LDFLAGS@ -LIBS=${LIBZEPHYR} @LIBS@ -lcom_err +LIBS=${LIBZEPHYR} @LIBS@ -lcom_err @ARES_LIBS@ HESIOD_LIBS=@HESIOD_LIBS@ NMOBJS= zsrv_err.o access.o acl_files.o bdump.o class.o client.o common.o \ diff --git a/server/global.c b/server/global.c index 69aea1f..5199f7b 100644 --- a/server/global.c +++ b/server/global.c @@ -18,6 +18,9 @@ int srv_socket; /* dgram socket for clients and other servers */ int bdump_socket = -1; /* brain dump socket fd (closed most of the time) */ +#ifdef HAVE_ARES +ares_channel achannel; /* C-ARES resolver channel */ +#endif fd_set interesting; /* the file descrips we are listening to right now */ struct sockaddr_in srv_addr; /* address of the socket */ diff --git a/server/main.c b/server/main.c index 4b2f9b7..8e326d9 100644 --- a/server/main.c +++ b/server/main.c @@ -93,9 +93,10 @@ int main(int argc, char **argv) { + int nselect; /* #fildes to select on */ int nfound; /* #fildes ready on select */ - fd_set readable; - struct timeval tv; + fd_set readable, writable; + struct timeval tv, *tvp; int init_from_dump = 0; char *dumpfile; #ifdef _POSIX_VERSION @@ -271,13 +272,24 @@ main(int argc, timer_process(); readable = interesting; + FD_ZERO(&writable); + tvp = timer_timeout(&tv); +#ifdef HAVE_ARES + nselect = ares_fds(achannel, &readable, &writable); + if (nselect < nfds) + nselect = nfds; + tvp = ares_timeout(achannel, tvp, &tv); +#else + nselect = nfds; +#endif if (msgs_queued()) { /* when there is input in the queue, we artificially set up to pick up the input */ nfound = 1; FD_ZERO(&readable); + FD_ZERO(&writable); } else { - nfound = select(nfds, &readable, NULL, NULL, timer_timeout(&tv)); + nfound = select(nselect, &readable, &writable, NULL, tvp); } /* Initialize t_local for other uses */ @@ -291,6 +303,10 @@ main(int argc, continue; } +#ifdef HAVE_ARES + ares_process(achannel, &readable, &writable); +#endif + if (nfound == 0) { /* either we timed out or we were just polling for input. Either way we want to continue @@ -301,8 +317,6 @@ main(int argc, bdump_send(); else if (msgs_queued() || FD_ISSET(srv_socket, &readable)) handle_packet(); - else - syslog(LOG_ERR, "select weird?!?!"); } } } @@ -384,6 +398,17 @@ do_net_setup(void) struct hostent *hp; char hostname[NS_MAXDNAME]; int flags; +#ifdef HAVE_ARES + int status; +#endif + +#ifdef HAVE_ARES + status = ares_init(&achannel); + if (status != ARES_SUCCESS) { + syslog(LOG_ERR, "resolver init failed: %s", ares_strerror(status)); + return 1; + } +#endif if (gethostname(hostname, sizeof(hostname))) { syslog(LOG_ERR, "no hostname: %m"); diff --git a/server/zserver.h b/server/zserver.h index 6ded4a4..822a90c 100644 --- a/server/zserver.h +++ b/server/zserver.h @@ -15,6 +15,9 @@ #include #include +#ifdef HAVE_ARES +#include +#endif #include @@ -423,6 +426,9 @@ extern int srv_socket; /* dgram sockets for clients and other servers */ extern int bdump_socket; /* brain dump socket (closed most of the time) */ +#ifdef HAVE_ARES +extern ares_channel achannel; +#endif extern fd_set interesting; /* the file descrips we are listening to right now */ -- cgit v1.2.3