From: Otto Moerbeek Date: Mon, 15 Apr 2024 11:55:20 +0000 (+0200) Subject: Format X-Git-Tag: rec-5.1.0-alpha1~19^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0720348204cd663bd21beec2bde57358aa534ae0;p=thirdparty%2Fpdns.git Format --- diff --git a/.not-formatted b/.not-formatted index 9acec46689..71868548cd 100644 --- a/.not-formatted +++ b/.not-formatted @@ -1,4 +1,4 @@ -./ext/lmdb-safe/lmdb-safe.cc +]./ext/lmdb-safe/lmdb-safe.cc ./ext/lmdb-safe/lmdb-safe.hh ./ext/lmdb-safe/lmdb-typed.cc ./ext/lmdb-safe/lmdb-typed.hh @@ -108,8 +108,6 @@ ./pdns/gettime.hh ./pdns/histog.hh ./pdns/inflighter.cc -./pdns/iputils.cc -./pdns/iputils.hh ./pdns/ixfr.cc ./pdns/ixfr.hh ./pdns/ixfrdist-stats.cc diff --git a/pdns/iputils.cc b/pdns/iputils.cc index bd1204e3ac..f4eeba8f3d 100644 --- a/pdns/iputils.cc +++ b/pdns/iputils.cc @@ -67,10 +67,10 @@ int SConnect(int sockfd, const ComboAddress& remote) int SConnectWithTimeout(int sockfd, const ComboAddress& remote, const struct timeval& timeout) { int ret = connect(sockfd, reinterpret_cast(&remote), remote.getSocklen()); - if(ret < 0) { + if (ret < 0) { int savederrno = errno; if (savederrno == EINPROGRESS) { - if (timeout <= timeval{0,0}) { + if (timeout <= timeval{0, 0}) { return savederrno; } @@ -82,7 +82,7 @@ int SConnectWithTimeout(int sockfd, const ComboAddress& remote, const struct tim if (error) { savederrno = 0; socklen_t errlen = sizeof(savederrno); - if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *)&savederrno, &errlen) == 0) { + if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void*)&savederrno, &errlen) == 0) { NetworkErr("connecting to " + remote.toStringWithPort() + " failed: " + stringerror(savederrno)); } else { @@ -96,7 +96,8 @@ int SConnectWithTimeout(int sockfd, const ComboAddress& remote, const struct tim } else if (res == 0) { NetworkErr("timeout while connecting to " + remote.toStringWithPort()); - } else if (res < 0) { + } + else if (res < 0) { savederrno = errno; NetworkErr("waiting to connect to " + remote.toStringWithPort() + ": " + stringerror(savederrno)); } @@ -153,40 +154,40 @@ void setSocketIgnorePMTU([[maybe_unused]] int sockfd, [[maybe_unused]] int famil if (family == AF_INET) { #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT) #ifdef IP_PMTUDISC_OMIT - /* Linux 3.15+ has IP_PMTUDISC_OMIT, which discards PMTU information to prevent - poisoning, but still allows fragmentation if the packet size exceeds the - outgoing interface MTU, which is good. - */ + /* Linux 3.15+ has IP_PMTUDISC_OMIT, which discards PMTU information to prevent + poisoning, but still allows fragmentation if the packet size exceeds the + outgoing interface MTU, which is good. + */ try { SSetsockopt(sockfd, IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_OMIT); return; } - catch(const std::exception& e) { + catch (const std::exception& e) { /* failed, let's try IP_PMTUDISC_DONT instead */ } #endif /* IP_PMTUDISC_OMIT */ - /* IP_PMTUDISC_DONT disables Path MTU discovery */ + /* IP_PMTUDISC_DONT disables Path MTU discovery */ SSetsockopt(sockfd, IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_DONT); #endif /* defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT) */ } else { - #if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DONT) +#if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DONT) #ifdef IPV6_PMTUDISC_OMIT - /* Linux 3.15+ has IPV6_PMTUDISC_OMIT, which discards PMTU information to prevent - poisoning, but still allows fragmentation if the packet size exceeds the - outgoing interface MTU, which is good. - */ + /* Linux 3.15+ has IPV6_PMTUDISC_OMIT, which discards PMTU information to prevent + poisoning, but still allows fragmentation if the packet size exceeds the + outgoing interface MTU, which is good. + */ try { SSetsockopt(sockfd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, IPV6_PMTUDISC_OMIT); return; } - catch(const std::exception& e) { + catch (const std::exception& e) { /* failed, let's try IP_PMTUDISC_DONT instead */ } #endif /* IPV6_PMTUDISC_OMIT */ - /* IPV6_PMTUDISC_DONT disables Path MTU discovery */ + /* IPV6_PMTUDISC_DONT disables Path MTU discovery */ SSetsockopt(sockfd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, IPV6_PMTUDISC_DONT); #endif /* defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DONT) */ } @@ -239,10 +240,9 @@ bool setReusePort(int sockfd) bool HarvestTimestamp(struct msghdr* msgh, struct timeval* tv) { #ifdef SO_TIMESTAMP - struct cmsghdr *cmsg; - for (cmsg = CMSG_FIRSTHDR(msgh); cmsg != nullptr; cmsg = CMSG_NXTHDR(msgh,cmsg)) { - if ((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SO_TIMESTAMP || cmsg->cmsg_type == SCM_TIMESTAMP) && - CMSG_LEN(sizeof(*tv)) == cmsg->cmsg_len) { + struct cmsghdr* cmsg; + for (cmsg = CMSG_FIRSTHDR(msgh); cmsg != nullptr; cmsg = CMSG_NXTHDR(msgh, cmsg)) { + if ((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SO_TIMESTAMP || cmsg->cmsg_type == SCM_TIMESTAMP) && CMSG_LEN(sizeof(*tv)) == cmsg->cmsg_len) { memcpy(tv, CMSG_DATA(cmsg), sizeof(*tv)); return true; } @@ -260,15 +260,15 @@ bool HarvestDestinationAddress(const struct msghdr* msgh, ComboAddress* destinat #endif for (cmsg = CMSG_FIRSTHDR(msgh); cmsg != nullptr; cmsg = CMSG_NXTHDR(const_cast(msgh), const_cast(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; + 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); + struct in_addr* i = (struct in_addr*)CMSG_DATA(cmsg); destination->sin4.sin_addr = *i; destination->sin4.sin_family = AF_INET; return true; @@ -276,10 +276,10 @@ bool HarvestDestinationAddress(const struct msghdr* msgh, ComboAddress* destinat #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; + 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; @@ -287,14 +287,14 @@ bool HarvestDestinationAddress(const struct msghdr* msgh, ComboAddress* destinat bool IsAnyAddress(const ComboAddress& addr) { - if(addr.sin4.sin_family == AF_INET) + if (addr.sin4.sin_family == AF_INET) return addr.sin4.sin_addr.s_addr == 0; - else if(addr.sin4.sin_family == AF_INET6) + else if (addr.sin4.sin_family == AF_INET6) return !memcmp(&addr.sin6.sin6_addr, &in6addr_any, sizeof(addr.sin6.sin6_addr)); return false; } -int sendOnNBSocket(int fd, const struct msghdr *msgh) +int sendOnNBSocket(int fd, const struct msghdr* msgh) { int sendErr = 0; #ifdef __OpenBSD__ @@ -323,7 +323,7 @@ int sendOnNBSocket(int fd, const struct msghdr *msgh) void fillMSGHdr(struct msghdr* msgh, struct iovec* iov, cmsgbuf_aligned* cbuf, size_t cbufsize, char* data, size_t datalen, ComboAddress* addr) { iov->iov_base = data; - iov->iov_len = datalen; + iov->iov_len = datalen; memset(msgh, 0, sizeof(struct msghdr)); @@ -331,7 +331,7 @@ void fillMSGHdr(struct msghdr* msgh, struct iovec* iov, cmsgbuf_aligned* cbuf, s msgh->msg_controllen = cbufsize; msgh->msg_name = addr; msgh->msg_namelen = addr->getSocklen(); - msgh->msg_iov = iov; + msgh->msg_iov = iov; msgh->msg_iovlen = 1; msgh->msg_flags = 0; } @@ -340,30 +340,30 @@ void fillMSGHdr(struct msghdr* msgh, struct iovec* iov, cmsgbuf_aligned* cbuf, s void ComboAddress::truncate(unsigned int bits) noexcept { uint8_t* start; - int len=4; - if(sin4.sin_family==AF_INET) { - if(bits >= 32) + int len = 4; + if (sin4.sin_family == AF_INET) { + if (bits >= 32) return; start = (uint8_t*)&sin4.sin_addr.s_addr; - len=4; + len = 4; } else { - if(bits >= 128) + if (bits >= 128) return; start = (uint8_t*)&sin6.sin6_addr.s6_addr; - len=16; + len = 16; } - auto tozero= len*8 - bits; // if set to 22, this will clear 1 byte, as it should + auto tozero = len * 8 - bits; // if set to 22, this will clear 1 byte, as it should - memset(start + len - tozero/8, 0, tozero/8); // blot out the whole bytes on the right + memset(start + len - tozero / 8, 0, tozero / 8); // blot out the whole bytes on the right - auto bitsleft=tozero % 8; // 2 bits left to clear + auto bitsleft = tozero % 8; // 2 bits left to clear // a b c d, to truncate to 22 bits, we just zeroed 'd' and need to zero 2 bits from c // so and by '11111100', which is ~((1<<2)-1) = ~3 - uint8_t* place = start + len - 1 - tozero/8; - *place &= (~((1<(reinterpret_cast(iov.iov_base) + written); @@ -445,8 +445,7 @@ size_t sendMsgWithOptions(int socketDesc, const void* buffer, size_t len, const } unixDie("failed in sendMsgWithOptions"); } - } - while (true); + } while (true); return 0; } @@ -522,7 +521,7 @@ ComboAddress parseIPAndPort(const std::string& input, uint16_t port) return ComboAddress(input, port); case 1: { // case 3 string::size_type cpos = input.rfind(':'); - pair both; + pair both; both.first = input.substr(0, cpos); both.second = input.substr(cpos + 1); @@ -598,12 +597,12 @@ std::set getListOfNetworkInterfaces() { std::set result; #ifdef HAVE_GETIFADDRS - struct ifaddrs *ifaddr; + struct ifaddrs* ifaddr; if (getifaddrs(&ifaddr) == -1) { return result; } - for (struct ifaddrs *ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) { + for (struct ifaddrs* ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) { if (ifa->ifa_name == nullptr) { continue; } @@ -619,12 +618,12 @@ std::set getListOfNetworkInterfaces() std::vector getListOfAddressesOfNetworkInterface(const std::string& itf) { std::vector result; - struct ifaddrs *ifaddr = nullptr; + struct ifaddrs* ifaddr = nullptr; if (getifaddrs(&ifaddr) == -1) { return result; } - for (struct ifaddrs *ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) { + for (struct ifaddrs* ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) { if (ifa->ifa_name == nullptr || strcmp(ifa->ifa_name, itf.c_str()) != 0) { continue; } @@ -651,7 +650,7 @@ std::vector getListOfAddressesOfNetworkInterface(const std::string std::vector result; return result; } -#endif // HAVE_GETIFADDRS +#endif // HAVE_GETIFADDRS #ifdef HAVE_GETIFADDRS static uint8_t convertNetmaskToBits(const uint8_t* mask, socklen_t len) @@ -678,12 +677,12 @@ static uint8_t convertNetmaskToBits(const uint8_t* mask, socklen_t len) std::vector getListOfRangesOfNetworkInterface(const std::string& itf) { std::vector result; - struct ifaddrs *ifaddr = nullptr; + struct ifaddrs* ifaddr = nullptr; if (getifaddrs(&ifaddr) == -1) { return result; } - for (struct ifaddrs *ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) { + for (struct ifaddrs* ifa = ifaddr; ifa != nullptr; ifa = ifa->ifa_next) { if (ifa->ifa_name == nullptr || strcmp(ifa->ifa_name, itf.c_str()) != 0) { continue; } @@ -719,4 +718,4 @@ std::vector getListOfRangesOfNetworkInterface(const std::string& /* itf std::vector result; return result; } -#endif // HAVE_GETIFADDRS +#endif // HAVE_GETIFADDRS diff --git a/pdns/iputils.hh b/pdns/iputils.hh index 2a318f7327..38fc351ead 100644 --- a/pdns/iputils.hh +++ b/pdns/iputils.hh @@ -83,36 +83,37 @@ #undef IP_PKTINFO #endif -union ComboAddress { +union ComboAddress +{ struct sockaddr_in sin4; struct sockaddr_in6 sin6; bool operator==(const ComboAddress& rhs) const { - if(std::tie(sin4.sin_family, sin4.sin_port) != std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) + if (std::tie(sin4.sin_family, sin4.sin_port) != std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) return false; - if(sin4.sin_family == AF_INET) + if (sin4.sin_family == AF_INET) return sin4.sin_addr.s_addr == rhs.sin4.sin_addr.s_addr; else - return memcmp(&sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(sin6.sin6_addr.s6_addr))==0; + return memcmp(&sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(sin6.sin6_addr.s6_addr)) == 0; } bool operator!=(const ComboAddress& rhs) const { - return(!operator==(rhs)); + return (!operator==(rhs)); } bool operator<(const ComboAddress& rhs) const { - if(sin4.sin_family == 0) { + if (sin4.sin_family == 0) { return false; } - if(std::tie(sin4.sin_family, sin4.sin_port) < std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) + if (std::tie(sin4.sin_family, sin4.sin_port) < std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) return true; - if(std::tie(sin4.sin_family, sin4.sin_port) > std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) + if (std::tie(sin4.sin_family, sin4.sin_port) > std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) return false; - if(sin4.sin_family == AF_INET) + if (sin4.sin_family == AF_INET) return sin4.sin_addr.s_addr < rhs.sin4.sin_addr.s_addr; else return memcmp(&sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(sin6.sin6_addr.s6_addr)) < 0; @@ -163,11 +164,11 @@ union ComboAddress { { bool operator()(const ComboAddress& a, const ComboAddress& b) const { - if(a.sin4.sin_family < b.sin4.sin_family) + if (a.sin4.sin_family < b.sin4.sin_family) return true; - if(a.sin4.sin_family > b.sin4.sin_family) + if (a.sin4.sin_family > b.sin4.sin_family) return false; - if(a.sin4.sin_family == AF_INET) + if (a.sin4.sin_family == AF_INET) return a.sin4.sin_addr.s_addr < b.sin4.sin_addr.s_addr; else return memcmp(&a.sin6.sin6_addr.s6_addr, &b.sin6.sin6_addr.s6_addr, sizeof(a.sin6.sin6_addr.s6_addr)) < 0; @@ -178,19 +179,18 @@ union ComboAddress { { bool operator()(const ComboAddress& a, const ComboAddress& b) const { - if(a.sin4.sin_family != b.sin4.sin_family) + if (a.sin4.sin_family != b.sin4.sin_family) return false; - if(a.sin4.sin_family == AF_INET) + if (a.sin4.sin_family == AF_INET) return a.sin4.sin_addr.s_addr == b.sin4.sin_addr.s_addr; else return !memcmp(&a.sin6.sin6_addr.s6_addr, &b.sin6.sin6_addr.s6_addr, sizeof(a.sin6.sin6_addr.s6_addr)); } }; - socklen_t getSocklen() const { - if(sin4.sin_family == AF_INET) + if (sin4.sin_family == AF_INET) return sizeof(sin4); else return sizeof(sin6); @@ -198,45 +198,49 @@ union ComboAddress { ComboAddress() { - sin4.sin_family=AF_INET; - sin4.sin_addr.s_addr=0; - sin4.sin_port=0; + sin4.sin_family = AF_INET; + sin4.sin_addr.s_addr = 0; + sin4.sin_port = 0; sin6.sin6_scope_id = 0; sin6.sin6_flowinfo = 0; } - ComboAddress(const struct sockaddr *sa, socklen_t salen) { + ComboAddress(const struct sockaddr* sa, socklen_t salen) + { setSockaddr(sa, salen); }; - ComboAddress(const struct sockaddr_in6 *sa) { + ComboAddress(const struct sockaddr_in6* sa) + { setSockaddr((const struct sockaddr*)sa, sizeof(struct sockaddr_in6)); }; - ComboAddress(const struct sockaddr_in *sa) { + ComboAddress(const struct sockaddr_in* sa) + { setSockaddr((const struct sockaddr*)sa, sizeof(struct sockaddr_in)); }; - void setSockaddr(const struct sockaddr *sa, socklen_t salen) { - if (salen > sizeof(struct sockaddr_in6)) throw PDNSException("ComboAddress can't handle other than sockaddr_in or sockaddr_in6"); + void setSockaddr(const struct sockaddr* sa, socklen_t salen) + { + if (salen > sizeof(struct sockaddr_in6)) + throw PDNSException("ComboAddress can't handle other than sockaddr_in or sockaddr_in6"); memcpy(this, sa, salen); } // 'port' sets a default value in case 'str' does not set a port - explicit ComboAddress(const string& str, uint16_t port=0) + explicit ComboAddress(const string& str, uint16_t port = 0) { memset(&sin6, 0, sizeof(sin6)); sin4.sin_family = AF_INET; sin4.sin_port = 0; - if(makeIPv4sockaddr(str, &sin4)) { + if (makeIPv4sockaddr(str, &sin4)) { sin6.sin6_family = AF_INET6; - if(makeIPv6sockaddr(str, &sin6) < 0) { - throw PDNSException("Unable to convert presentation address '"+ str +"'"); + if (makeIPv6sockaddr(str, &sin6) < 0) { + throw PDNSException("Unable to convert presentation address '" + str + "'"); } - } - if(!sin4.sin_port) // 'str' overrides port! - sin4.sin_port=htons(port); + if (!sin4.sin_port) // 'str' overrides port! + sin4.sin_port = htons(port); } bool isIPv6() const @@ -248,19 +252,19 @@ union ComboAddress { return sin4.sin_family == AF_INET; } - bool isMappedIPv4() const + bool isMappedIPv4() const { - if(sin4.sin_family!=AF_INET6) + if (sin4.sin_family != AF_INET6) return false; - int n=0; + int n = 0; const unsigned char* ptr = reinterpret_cast(&sin6.sin6_addr.s6_addr); - for(n=0; n < 10; ++n) - if(ptr[n]) + for (n = 0; n < 10; ++n) + if (ptr[n]) return false; - for(; n < 12; ++n) - if(ptr[n]!=0xff) + for (; n < 12; ++n) + if (ptr[n] != 0xff) return false; return true; @@ -268,14 +272,14 @@ union ComboAddress { ComboAddress mapToIPv4() const { - if(!isMappedIPv4()) + if (!isMappedIPv4()) throw PDNSException("ComboAddress can't map non-mapped IPv6 address back to IPv4"); ComboAddress ret; - ret.sin4.sin_family=AF_INET; - ret.sin4.sin_port=sin4.sin_port; + ret.sin4.sin_family = AF_INET; + ret.sin4.sin_port = sin4.sin_port; const unsigned char* ptr = reinterpret_cast(&sin6.sin6_addr.s6_addr); - ptr+=(sizeof(sin6.sin6_addr.s6_addr) - sizeof(ret.sin4.sin_addr.s_addr)); + ptr += (sizeof(sin6.sin6_addr.s6_addr) - sizeof(ret.sin4.sin_addr.s_addr)); memcpy(&ret.sin4.sin_addr.s_addr, ptr, sizeof(ret.sin4.sin_addr.s_addr)); return ret; } @@ -284,22 +288,22 @@ union ComboAddress { { char host[1024]; int retval = 0; - if(sin4.sin_family && !(retval = getnameinfo(reinterpret_cast(this), getSocklen(), host, sizeof(host),0, 0, NI_NUMERICHOST))) + if (sin4.sin_family && !(retval = getnameinfo(reinterpret_cast(this), getSocklen(), host, sizeof(host), 0, 0, NI_NUMERICHOST))) return string(host); else - return "invalid "+string(gai_strerror(retval)); + return "invalid " + string(gai_strerror(retval)); } //! Ignores any interface specifiers possibly available in the sockaddr data. string toStringNoInterface() const { char host[1024]; - if(sin4.sin_family == AF_INET && (nullptr != inet_ntop(sin4.sin_family, &sin4.sin_addr, host, sizeof(host)))) + if (sin4.sin_family == AF_INET && (nullptr != inet_ntop(sin4.sin_family, &sin4.sin_addr, host, sizeof(host)))) return string(host); - else if(sin4.sin_family == AF_INET6 && (nullptr != inet_ntop(sin4.sin_family, &sin6.sin6_addr, host, sizeof(host)))) + else if (sin4.sin_family == AF_INET6 && (nullptr != inet_ntop(sin4.sin_family, &sin6.sin6_addr, host, sizeof(host)))) return string(host); else - return "invalid "+stringerror(); + return "invalid " + stringerror(); } [[nodiscard]] string toStringReversed() const @@ -330,20 +334,20 @@ union ComboAddress { string toStringWithPort() const { - if(sin4.sin_family==AF_INET) + if (sin4.sin_family == AF_INET) return toString() + ":" + std::to_string(ntohs(sin4.sin_port)); else - return "["+toString() + "]:" + std::to_string(ntohs(sin4.sin_port)); + return "[" + toString() + "]:" + std::to_string(ntohs(sin4.sin_port)); } string toStringWithPortExcept(int port) const { - if(ntohs(sin4.sin_port) == port) + if (ntohs(sin4.sin_port) == port) return toString(); - if(sin4.sin_family==AF_INET) + if (sin4.sin_family == AF_INET) return toString() + ":" + std::to_string(ntohs(sin4.sin_port)); else - return "["+toString() + "]:" + std::to_string(ntohs(sin4.sin_port)); + return "[" + toString() + "]:" + std::to_string(ntohs(sin4.sin_port)); } string toLogString() const @@ -400,7 +404,7 @@ union ComboAddress { */ bool getBit(int index) const { - if(isIPv4()) { + if (isIPv4()) { if (index >= 32) return false; if (index < 0) { @@ -411,9 +415,9 @@ union ComboAddress { uint32_t ls_addr = ntohl(sin4.sin_addr.s_addr); - return ((ls_addr & (1U<= 128) return false; if (index < 0) { @@ -426,7 +430,7 @@ union ComboAddress { uint8_t byte_idx = index / 8; uint8_t bit_idx = index % 8; - return ((ls_addr[15-byte_idx] & (1U << bit_idx)) != 0x00); + return ((ls_addr[15 - byte_idx] & (1U << bit_idx)) != 0x00); } return false; } @@ -437,35 +441,37 @@ union ComboAddress { * \param withPort Also print the port (default true) * \param portExcept Print the port, except when this is the port (default 53) */ - template < template < class ... > class Container, class ... Args > - static string caContainerToString(const Container& c, const bool withPort = true, const uint16_t portExcept = 53) { - vector strs; - for (const auto& ca : c) { - if (withPort) { - strs.push_back(ca.toStringWithPortExcept(portExcept)); - continue; + template