From 3e3f03588ed9877f7f49bc01a6332ef0fb700d46 Mon Sep 17 00:00:00 2001 From: bert hubert Date: Thu, 8 Jan 2015 14:50:49 +0100 Subject: [PATCH] move infrastructure for serving from 0.0.0.0 and :: from Auth server to a shared place with the recursor --- pdns/Makefile-recursor | 2 +- pdns/Makefile.am | 3 +- pdns/dist-recursor | 2 +- pdns/iputils.cc | 75 ++++++++++++++++++++++++++++++++++++++++++ pdns/iputils.hh | 10 ++++++ pdns/nameserver.cc | 64 ----------------------------------- 6 files changed, 89 insertions(+), 67 deletions(-) diff --git a/pdns/Makefile-recursor b/pdns/Makefile-recursor index 2415dc688..215521441 100644 --- a/pdns/Makefile-recursor +++ b/pdns/Makefile-recursor @@ -24,7 +24,7 @@ dns_random.o pubsuffix.o ext/polarssl/library/aes.o dnslabeltext.o \ lua-pdns.o lua-recursor.o randomhelper.o recpacketcache.o dns.o \ reczones.o base32.o nsecrecords.o json.o ws-recursor.o ws-api.o \ version.o responsestats.o webserver.o ext/yahttp/yahttp/reqresp.o ext/yahttp/yahttp/router.o \ -rec-carbon.o secpoll-recursor.o lua-iputils.o +rec-carbon.o secpoll-recursor.o lua-iputils.o iputils.o REC_CONTROL_OBJECTS=rec_channel.o rec_control.o arguments.o misc.o \ unix_utility.o logger.o qtype.o diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 45b71f9b7..ffff79dbe 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -132,7 +132,7 @@ pdns_server_SOURCES = \ dynlistener.cc dynlistener.hh \ dynmessenger.hh \ ednssubnet.cc ednssubnet.hh \ - iputils.hh \ + iputils.cc iputils.hh \ json.cc json.hh \ lock.hh \ logger.cc logger.hh \ @@ -897,6 +897,7 @@ pdns_recursor_SOURCES = \ dnswriter.cc dnswriter.hh \ epollmplexer.cc \ htimer.cc htimer.hh \ + iputils.cc \ json.cc json.hh \ logger.cc \ lua-pdns.cc lua-pdns.hh lua-iputils.cc \ diff --git a/pdns/dist-recursor b/pdns/dist-recursor index c4f39ffec..ced2005cc 100755 --- a/pdns/dist-recursor +++ b/pdns/dist-recursor @@ -33,7 +33,7 @@ recpacketcache.hh base32.hh cachecleaner.hh json.hh version.hh \ ws-recursor.hh ws-api.hh secpoll-recursor.hh \ responsestats.hh webserver.hh" -CFILES="syncres.cc misc.cc unix_utility.cc qtype.cc \ +CFILES="syncres.cc iputils.cc misc.cc unix_utility.cc qtype.cc \ logger.cc arguments.cc lwres.cc pdns_recursor.cc lua-iputils.cc \ recursor_cache.cc dnsparser.cc dnswriter.cc dnsrecords.cc rcpgenerator.cc \ base64.cc zoneparser-tng.cc rec_channel.cc rec_channel_rec.cc rec_control.cc \ diff --git a/pdns/iputils.cc b/pdns/iputils.cc index d9eb9b5a6..11f8181af 100644 --- a/pdns/iputils.cc +++ b/pdns/iputils.cc @@ -58,3 +58,78 @@ int SSetsockopt(int sockfd, int level, int opname, int value) } +bool HarvestTimestamp(struct msghdr* msgh, struct timeval* tv) +{ +#ifdef SO_TIMESTAMP + struct cmsghdr *cmsg; + for (cmsg = CMSG_FIRSTHDR(msgh); cmsg != NULL; cmsg = CMSG_NXTHDR(msgh,cmsg)) { + if ((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SO_TIMESTAMP) && + CMSG_LEN(sizeof(*tv)) == cmsg->cmsg_len) { + memcpy(tv, CMSG_DATA(cmsg), sizeof(*tv)); + return true; + } + } +#endif + return false; +} +bool HarvestDestinationAddress(struct msghdr* msgh, ComboAddress* destination) +{ + memset(destination, 0, sizeof(*destination)); + struct cmsghdr *cmsg; + for (cmsg = CMSG_FIRSTHDR(msgh); cmsg != NULL; cmsg = CMSG_NXTHDR(msgh,cmsg)) { +#if defined(IP_PKTINFO) + if ((cmsg->cmsg_level == IPPROTO_IP) && (cmsg->cmsg_type == IP_PKTINFO)) { + struct in_pktinfo *i = (struct in_pktinfo *) CMSG_DATA(cmsg); + destination->sin4.sin_addr = i->ipi_addr; + destination->sin4.sin_family = AF_INET; + return true; + } +#elif defined(IP_RECVDSTADDR) + if ((cmsg->cmsg_level == IPPROTO_IP) && (cmsg->cmsg_type == IP_RECVDSTADDR)) { + struct in_addr *i = (struct in_addr *) CMSG_DATA(cmsg); + destination->sin4.sin_addr = *i; + destination->sin4.sin_family = AF_INET; + return true; + } +#endif + + if ((cmsg->cmsg_level == IPPROTO_IPV6) && (cmsg->cmsg_type == IPV6_PKTINFO)) { + struct in6_pktinfo *i = (struct in6_pktinfo *) CMSG_DATA(cmsg); + destination->sin6.sin6_addr = i->ipi6_addr; + destination->sin4.sin_family = AF_INET6; + return true; + } + } + return false; +} + +bool IsAnyAddress(const ComboAddress& addr) +{ + if(addr.sin4.sin_family == AF_INET) + return addr.sin4.sin_addr.s_addr == 0; + else if(addr.sin4.sin_family == AF_INET6) + return !memcmp(&addr.sin6.sin6_addr, &in6addr_any, sizeof(addr.sin6.sin6_addr)); + + return false; +} + +int sendfromto(int sock, const char* data, int len, int flags, const ComboAddress& from, const ComboAddress& to) +{ + struct msghdr msgh; + struct iovec iov; + char cbuf[256]; + + /* Set up iov and msgh structures. */ + memset(&msgh, 0, sizeof(struct msghdr)); + iov.iov_base = (void*)data; + iov.iov_len = len; + msgh.msg_iov = &iov; + msgh.msg_iovlen = 1; + msgh.msg_name = (struct sockaddr*)&to; + msgh.msg_namelen = to.getSocklen(); + + if(from.sin4.sin_family) { + addCMsgSrcAddr(&msgh, cbuf, &from); + } + return sendmsg(sock, &msgh, flags); +} diff --git a/pdns/iputils.hh b/pdns/iputils.hh index 7b7ce6282..99abd3384 100644 --- a/pdns/iputils.hh +++ b/pdns/iputils.hh @@ -433,4 +433,14 @@ int SAccept(int sockfd, ComboAddress& remote); int SListen(int sockfd, int limit); int SSetsockopt(int sockfd, int level, int opname, int value); +#if defined(IP_PKTINFO) + #define GEN_IP_PKTINFO IP_PKTINFO +#elif defined(IP_RECVDSTADDR) + #define GEN_IP_PKTINFO IP_RECVDSTADDR +#endif +bool IsAnyAddress(const ComboAddress& addr); +bool HarvestDestinationAddress(struct msghdr* msgh, ComboAddress* destination); +bool HarvestTimestamp(struct msghdr* msgh, struct timeval* tv); + + #endif diff --git a/pdns/nameserver.cc b/pdns/nameserver.cc index 4eaaddc52..fca614170 100644 --- a/pdns/nameserver.cc +++ b/pdns/nameserver.cc @@ -81,12 +81,6 @@ extern StatBag S; The main() of PowerDNS can be found in receiver.cc - start reading there for further insights into the operation of the nameserver */ -#if defined(IP_PKTINFO) - #define GEN_IP_PKTINFO IP_PKTINFO -#elif defined(IP_RECVDSTADDR) - #define GEN_IP_PKTINFO IP_RECVDSTADDR -#endif - vector g_localaddresses; // not static, our unit tests need to poke this void UDPNameserver::bindIPv4() @@ -157,17 +151,6 @@ void UDPNameserver::bindIPv4() } } -static bool IsAnyAddress(const ComboAddress& addr) -{ - if(addr.sin4.sin_family == AF_INET) - return addr.sin4.sin_addr.s_addr == 0; - else if(addr.sin4.sin_family == AF_INET6) - return !memcmp(&addr.sin6.sin6_addr, &in6addr_any, sizeof(addr.sin6.sin6_addr)); - - return false; -} - - bool AddressIsUs(const ComboAddress& remote) { BOOST_FOREACH(const ComboAddress& us, g_localaddresses) { @@ -224,7 +207,6 @@ void UDPNameserver::bindIPv6() if(!Utility::setNonBlocking(s)) throw PDNSException("Unable to set UDPv6 socket to non-blocking: "+stringerror()); - ComboAddress locala(localname, ::arg().asNum("local-port")); if(IsAnyAddress(locala)) { @@ -333,52 +315,6 @@ void UDPNameserver::send(DNSPacket *p) L<cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SO_TIMESTAMP) && - CMSG_LEN(sizeof(*tv)) == cmsg->cmsg_len) { - memcpy(tv, CMSG_DATA(cmsg), sizeof(*tv)); - return true; - } - } -#endif - return false; -} - -static bool HarvestDestinationAddress(struct msghdr* msgh, ComboAddress* destination) -{ - memset(destination, 0, sizeof(*destination)); - struct cmsghdr *cmsg; - for (cmsg = CMSG_FIRSTHDR(msgh); cmsg != NULL; cmsg = CMSG_NXTHDR(msgh,cmsg)) { -#if defined(IP_PKTINFO) - if ((cmsg->cmsg_level == IPPROTO_IP) && (cmsg->cmsg_type == IP_PKTINFO)) { - struct in_pktinfo *i = (struct in_pktinfo *) CMSG_DATA(cmsg); - destination->sin4.sin_addr = i->ipi_addr; - destination->sin4.sin_family = AF_INET; - return true; - } -#elif defined(IP_RECVDSTADDR) - if ((cmsg->cmsg_level == IPPROTO_IP) && (cmsg->cmsg_type == IP_RECVDSTADDR)) { - struct in_addr *i = (struct in_addr *) CMSG_DATA(cmsg); - destination->sin4.sin_addr = *i; - destination->sin4.sin_family = AF_INET; - return true; - } -#endif - - if ((cmsg->cmsg_level == IPPROTO_IPV6) && (cmsg->cmsg_type == IPV6_PKTINFO)) { - struct in6_pktinfo *i = (struct in6_pktinfo *) CMSG_DATA(cmsg); - destination->sin6.sin6_addr = i->ipi6_addr; - destination->sin4.sin_family = AF_INET6; - return true; - } - } - return false; -} - DNSPacket *UDPNameserver::receive(DNSPacket *prefilled) { ComboAddress remote; -- 2.40.0