From 34fd2888bbaa55f2d43571ef5c92d50137a608f8 Mon Sep 17 00:00:00 2001 From: Pavel Vasin Date: Mon, 21 Jul 2014 14:24:28 +0400 Subject: [PATCH 1/2] remove option to build without ipv6 support --- Makefile | 2 +- main.cpp | 2 -- netbase.cpp | 30 ------------------------------ netbase.h | 4 ---- 4 files changed, 1 insertion(+), 37 deletions(-) diff --git a/Makefile b/Makefile index 3bbd7f7..7106381 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ dnsseed: dns.o bitcoin.o netbase.o protocol.o db.o main.o util.o g++ -pthread $(LDFLAGS) -o dnsseed dns.o bitcoin.o netbase.o protocol.o db.o main.o util.o -lcrypto %.o: %.cpp bitcoin.h netbase.h protocol.h db.h serialize.h uint256.h util.h - g++ -DUSE_IPV6 -pthread $(CXXFLAGS) -Wno-invalid-offsetof -c -o $@ $< + g++ -pthread $(CXXFLAGS) -Wno-invalid-offsetof -c -o $@ $< dns.o: dns.c gcc -pthread -std=c99 $(CXXFLAGS) dns.c -c -o dns.o diff --git a/main.cpp b/main.cpp index 2e88ed4..2e0e187 100644 --- a/main.cpp +++ b/main.cpp @@ -188,14 +188,12 @@ public: memcpy(&a.data.v4, &addr, 4); cache.push_back(a); nIPv4++; -#ifdef USE_IPV6 } else if ((*it).GetIn6Addr(&addr6)) { addr_t a; a.v = 6; memcpy(&a.data.v6, &addr6, 16); cache.push_back(a); nIPv6++; -#endif } } cacheHits = 0; diff --git a/netbase.cpp b/netbase.cpp index 2630f87..449e635 100644 --- a/netbase.cpp +++ b/netbase.cpp @@ -74,18 +74,10 @@ bool static LookupIntern(const char *pszName, std::vector& vIP, unsign aiHint.ai_socktype = SOCK_STREAM; aiHint.ai_protocol = IPPROTO_TCP; #ifdef WIN32 -# ifdef USE_IPV6 aiHint.ai_family = AF_UNSPEC; -# else - aiHint.ai_family = AF_INET; -# endif aiHint.ai_flags = fAllowLookup ? 0 : AI_NUMERICHOST; #else -# ifdef USE_IPV6 aiHint.ai_family = AF_UNSPEC; -# else - aiHint.ai_family = AF_INET; -# endif aiHint.ai_flags = fAllowLookup ? AI_ADDRCONFIG : AI_NUMERICHOST; #endif struct addrinfo *aiRes = NULL; @@ -102,13 +94,11 @@ bool static LookupIntern(const char *pszName, std::vector& vIP, unsign vIP.push_back(CNetAddr(((struct sockaddr_in*)(aiTrav->ai_addr))->sin_addr)); } -#ifdef USE_IPV6 if (aiTrav->ai_family == AF_INET6) { assert(aiTrav->ai_addrlen >= sizeof(sockaddr_in6)); vIP.push_back(CNetAddr(((struct sockaddr_in6*)(aiTrav->ai_addr))->sin6_addr)); } -#endif aiTrav = aiTrav->ai_next; } @@ -323,11 +313,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe { hSocketRet = INVALID_SOCKET; -#ifdef USE_IPV6 struct sockaddr_storage sockaddr; -#else - struct sockaddr sockaddr; -#endif socklen_t len = sizeof(sockaddr); if (!addrConnect.GetSockAddr((struct sockaddr*)&sockaddr, &len)) { printf("Cannot connect to %s: unsupported network\n", addrConnect.ToString().c_str()); @@ -578,12 +564,10 @@ CNetAddr::CNetAddr(const struct in_addr& ipv4Addr) memcpy(ip+12, &ipv4Addr, 4); } -#ifdef USE_IPV6 CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr) { memcpy(ip, &ipv6Addr, 16); } -#endif CNetAddr::CNetAddr(const char *pszIp, bool fAllowLookup) { @@ -767,11 +751,7 @@ std::string CNetAddr::ToStringIP() const if (IsI2P()) return EncodeBase32(&ip[6], 10) + ".oc.b32.i2p"; CService serv(*this, 0); -#ifdef USE_IPV6 struct sockaddr_storage sockaddr; -#else - struct sockaddr sockaddr; -#endif socklen_t socklen = sizeof(sockaddr); if (serv.GetSockAddr((struct sockaddr*)&sockaddr, &socklen)) { char name[1025] = ""; @@ -816,13 +796,11 @@ bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const return true; } -#ifdef USE_IPV6 bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const { memcpy(pipv6Addr, ip, 16); return true; } -#endif // get canonical identifier of an address' group // no two connections will be attempted to addresses with the same group @@ -1008,23 +986,19 @@ CService::CService(const struct in_addr& ipv4Addr, unsigned short portIn) : CNet { } -#ifdef USE_IPV6 CService::CService(const struct in6_addr& ipv6Addr, unsigned short portIn) : CNetAddr(ipv6Addr), port(portIn) { } -#endif CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port)) { assert(addr.sin_family == AF_INET); } -#ifdef USE_IPV6 CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr), port(ntohs(addr.sin6_port)) { assert(addr.sin6_family == AF_INET6); } -#endif bool CService::SetSockAddr(const struct sockaddr *paddr) { @@ -1032,11 +1006,9 @@ bool CService::SetSockAddr(const struct sockaddr *paddr) case AF_INET: *this = CService(*(const struct sockaddr_in*)paddr); return true; -#ifdef USE_IPV6 case AF_INET6: *this = CService(*(const struct sockaddr_in6*)paddr); return true; -#endif default: return false; } @@ -1108,7 +1080,6 @@ bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const paddrin->sin_port = htons(port); return true; } -#ifdef USE_IPV6 if (IsIPv6()) { if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6)) return false; @@ -1121,7 +1092,6 @@ bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const paddrin6->sin6_port = htons(port); return true; } -#endif return false; } diff --git a/netbase.h b/netbase.h index 560e218..6fbf881 100644 --- a/netbase.h +++ b/netbase.h @@ -73,10 +73,8 @@ class CNetAddr int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const; void print() const; -#ifdef USE_IPV6 CNetAddr(const struct in6_addr& pipv6Addr); bool GetIn6Addr(struct in6_addr* pipv6Addr) const; -#endif friend bool operator==(const CNetAddr& a, const CNetAddr& b); friend bool operator!=(const CNetAddr& a, const CNetAddr& b); @@ -117,10 +115,8 @@ class CService : public CNetAddr std::string ToStringIPPort() const; void print() const; -#ifdef USE_IPV6 CService(const struct in6_addr& ipv6Addr, unsigned short port); CService(const struct sockaddr_in6& addr); -#endif IMPLEMENT_SERIALIZE ( From bb53306a2e05aab76401fb4c1dc0c7229cb4d66d Mon Sep 17 00:00:00 2001 From: Pavel Vasin Date: Mon, 21 Jul 2014 15:26:21 +0400 Subject: [PATCH 2/2] dns: listen on ipv6 --- dns.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/dns.c b/dns.c index 8b01458..1bea4ca 100644 --- a/dns.c +++ b/dns.c @@ -18,18 +18,18 @@ #if defined IP_RECVDSTADDR # define DSTADDR_SOCKOPT IP_RECVDSTADDR -# define DSTADDR_DATASIZE (CMSG_SPACE(sizeof(struct in_addr))) +# define DSTADDR_DATASIZE (CMSG_SPACE(sizeof(struct in6_addr))) # define dstaddr(x) (CMSG_DATA(x)) -#elif defined IP_PKTINFO -struct in_pktinfo { - unsigned int ipi_ifindex; /* Interface index */ - struct in_addr ipi_spec_dst; /* Local address */ - struct in_addr ipi_addr; /* Header Destination address */ -}; +#elif defined IPV6_PKTINFO +struct in6_pktinfo + { + struct in6_addr ipi6_addr; /* src/dst IPv6 address */ + unsigned int ipi6_ifindex; /* send/recv interface index */ + }; -# define DSTADDR_SOCKOPT IP_PKTINFO -# define DSTADDR_DATASIZE (CMSG_SPACE(sizeof(struct in_pktinfo))) -# define dstaddr(x) (&(((struct in_pktinfo *)(CMSG_DATA(x)))->ipi_addr)) +# define DSTADDR_SOCKOPT IPV6_PKTINFO +# define DSTADDR_DATASIZE (CMSG_SPACE(sizeof(struct in6_pktinfo))) +# define dstaddr(x) (&(((struct in6_pktinfo *)(CMSG_DATA(x)))->ipi6_addr)) #else # error "can't determine socket option" #endif @@ -366,31 +366,31 @@ error: static int listenSocket = -1; int dnsserver(dns_opt_t *opt) { - struct sockaddr_in si_other; + struct sockaddr_in6 si_other; int senderSocket = -1; - senderSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + senderSocket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); if (senderSocket == -1) return -3; int replySocket; if (listenSocket == -1) { - struct sockaddr_in si_me; - if ((listenSocket=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) { + struct sockaddr_in6 si_me; + if ((listenSocket=socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP))==-1) { listenSocket = -1; return -1; } - replySocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + replySocket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); if (replySocket == -1) { close(listenSocket); return -1; } int sockopt = 1; - setsockopt(listenSocket, IPPROTO_IP, DSTADDR_SOCKOPT, &sockopt, sizeof sockopt); + setsockopt(listenSocket, IPPROTO_IPV6, DSTADDR_SOCKOPT, &sockopt, sizeof sockopt); memset((char *) &si_me, 0, sizeof(si_me)); - si_me.sin_family = AF_INET; - si_me.sin_port = htons(opt->port); - si_me.sin_addr.s_addr = INADDR_ANY; + si_me.sin6_family = AF_INET6; + si_me.sin6_port = htons(opt->port); + si_me.sin6_addr = in6addr_any; if (bind(listenSocket, (struct sockaddr*)&si_me, sizeof(si_me))==-1) return -2; } @@ -414,7 +414,7 @@ int dnsserver(dns_opt_t *opt) { for (; 1; ++(opt->nRequests)) { ssize_t insize = recvmsg(listenSocket, &msg, 0); - unsigned char *addr = (unsigned char*)&si_other.sin_addr.s_addr; +// unsigned char *addr = (unsigned char*)&si_other.sin_addr.s_addr; // printf("DNS: Request %llu from %i.%i.%i.%i:%i of %i bytes\n", (unsigned long long)(opt->nRequests), addr[0], addr[1], addr[2], addr[3], ntohs(si_other.sin_port), (int)insize); if (insize <= 0) continue;