diff --git a/src/seeder/bitcoin.cpp b/src/seeder/bitcoin.cpp --- a/src/seeder/bitcoin.cpp +++ b/src/seeder/bitcoin.cpp @@ -140,7 +140,9 @@ int64_t now = time(nullptr); std::vector::iterator it = vAddrNew.begin(); if (vAddrNew.size() > 1) { - if (doneAfter == 0 || doneAfter > now + 1) doneAfter = now + 1; + if (doneAfter == 0 || doneAfter > now + 1) { + doneAfter = now + 1; + } } while (it != vAddrNew.end()) { CAddress &addr = *it; @@ -296,7 +298,9 @@ } int ret = select(sock + 1, &fdsetRecv, nullptr, &fdsetError, &wa); if (ret != 1) { - if (!doneAfter) res = false; + if (!doneAfter) { + res = false; + } break; } int nBytes = recv(sock, pchBuf, sizeof(pchBuf), 0); @@ -318,7 +322,9 @@ ProcessMessages(); Send(); } - if (sock == INVALID_SOCKET) res = false; + if (sock == INVALID_SOCKET) { + res = false; + } close(sock); sock = INVALID_SOCKET; return (ban == 0) && res; diff --git a/src/seeder/db.h b/src/seeder/db.h --- a/src/seeder/db.h +++ b/src/seeder/db.h @@ -117,19 +117,41 @@ } bool IsGood() const { - if (ip.GetPort() != GetDefaultPort()) return false; - if (!(services & NODE_NETWORK)) return false; - if (!ip.IsRoutable()) return false; - if (clientVersion && clientVersion < REQUIRE_VERSION) return false; - if (blocks && blocks < GetRequireHeight()) return false; + if (ip.GetPort() != GetDefaultPort()) { + return false; + } + if (!(services & NODE_NETWORK)) { + return false; + } + if (!ip.IsRoutable()) { + return false; + } + if (clientVersion && clientVersion < REQUIRE_VERSION) { + return false; + } + if (blocks && blocks < GetRequireHeight()) { + return false; + } - if (total <= 3 && success * 2 >= total) return true; + if (total <= 3 && success * 2 >= total) { + return true; + } - if (stat2H.reliability > 0.85 && stat2H.count > 2) return true; - if (stat8H.reliability > 0.70 && stat8H.count > 4) return true; - if (stat1D.reliability > 0.55 && stat1D.count > 8) return true; - if (stat1W.reliability > 0.45 && stat1W.count > 16) return true; - if (stat1M.reliability > 0.35 && stat1M.count > 32) return true; + if (stat2H.reliability > 0.85 && stat2H.count > 2) { + return true; + } + if (stat8H.reliability > 0.70 && stat8H.count > 4) { + return true; + } + if (stat1D.reliability > 0.55 && stat1D.count > 8) { + return true; + } + if (stat1W.reliability > 0.45 && stat1W.count > 16) { + return true; + } + if (stat1M.reliability > 0.35 && stat1M.count > 32) { + return true; + } return false; } @@ -212,9 +234,15 @@ READWRITE(total); READWRITE(success); READWRITE(clientVersion); - if (version >= 2) READWRITE(clientSubVersion); - if (version >= 3) READWRITE(blocks); - if (version >= 4) READWRITE(ourLastSuccess); + if (version >= 2) { + READWRITE(clientSubVersion); + } + if (version >= 3) { + READWRITE(blocks); + } + if (version >= 4) { + READWRITE(ourLastSuccess); + } } }; @@ -374,7 +402,9 @@ db->ipToId[info.ip] = id; if (info.ourLastTry) { db->ourId.push_back(id); - if (info.IsGood()) db->goodId.insert(id); + if (info.IsGood()) { + db->goodId.insert(id); + } } else { db->unkId.insert(id); } diff --git a/src/seeder/db.cpp b/src/seeder/db.cpp --- a/src/seeder/db.cpp +++ b/src/seeder/db.cpp @@ -8,7 +8,9 @@ void CAddrInfo::Update(bool good) { int64_t now = time(nullptr); - if (ourLastTry == 0) ourLastTry = now - MIN_RETRY; + if (ourLastTry == 0) { + ourLastTry = now - MIN_RETRY; + } int age = now - ourLastTry; lastTry = now; ourLastTry = now; @@ -23,8 +25,9 @@ stat1W.Update(good, age, 3600 * 24 * 7); stat1M.Update(good, age, 3600 * 24 * 30); int64_t ign = GetIgnoreTime(); - if (ign && (ignoreTill == 0 || ignoreTill < ign + now)) + if (ign && (ignoreTill == 0 || ignoreTill < ign + now)) { ignoreTill = ign + now; + } // fprintf(stdout, "%s: got %s result: success=%i/%i; // 2H:%.2f%%-%.2f%%(%.2f) 8H:%.2f%%-%.2f%%(%.2f) 1D:%.2f%%-%.2f%%(%.2f) // 1W:%.2f%%-%.2f%%(%.2f) \n", ToString(ip).c_str(), good ? "good" : "bad", @@ -75,14 +78,18 @@ } int CAddrDb::Lookup_(const CService &ip) { - if (ipToId.count(ip)) return ipToId[ip]; + if (ipToId.count(ip)) { + return ipToId[ip]; + } return -1; } void CAddrDb::Good_(const CService &addr, int clientV, std::string clientSV, int blocks) { int id = Lookup_(addr); - if (id == -1) return; + if (id == -1) { + return; + } unkId.erase(id); banned.erase(addr); CAddrInfo &info = idToInfo[id]; @@ -101,7 +108,9 @@ void CAddrDb::Bad_(const CService &addr, int ban) { int id = Lookup_(addr); - if (id == -1) return; + if (id == -1) { + return; + } unkId.erase(id); CAddrInfo &info = idToInfo[id]; info.Update(false); @@ -131,7 +140,9 @@ void CAddrDb::Skipped_(const CService &addr) { int id = Lookup_(addr); - if (id == -1) return; + if (id == -1) { + return; + } unkId.erase(id); ourId.push_back(id); // fprintf(stdout, "%s: skipped\n", ToString(addr).c_str()); diff --git a/src/seeder/dns.cpp b/src/seeder/dns.cpp --- a/src/seeder/dns.cpp +++ b/src/seeder/dns.cpp @@ -37,7 +37,10 @@ uint8_t data[DSTADDR_DATASIZE]; }; -typedef enum { CLASS_IN = 1, QCLASS_ANY = 255 } dns_class; +typedef enum { + CLASS_IN = 1, + QCLASS_ANY = 255, +} dns_class; typedef enum { TYPE_A = 1, @@ -55,7 +58,9 @@ size_t bufused = 0; int init = 1; do { - if (*inpos == inend) return -1; + if (*inpos == inend) { + return -1; + } // read length of next component int octet = *((*inpos)++); if (octet == 0) { @@ -64,26 +69,40 @@ } // add dot in output if (!init) { - if (bufused == bufsize - 1) return -2; + if (bufused == bufsize - 1) { + return -2; + } buf[bufused++] = '.'; } else init = 0; // handle references if ((octet & 0xC0) == 0xC0) { - if (*inpos == inend) return -1; + if (*inpos == inend) { + return -1; + } int ref = ((octet - 0xC0) << 8) + *((*inpos)++); - if (ref < 0 || ref >= (*inpos) - inbuf - 2) return -1; + if (ref < 0 || ref >= (*inpos) - inbuf - 2) { + return -1; + } const uint8_t *newbuf = inbuf + ref; return parse_name(&newbuf, (*inpos) - 2, inbuf, buf + bufused, bufsize - bufused); } - if (octet > 63) return -1; + if (octet > 63) { + return -1; + } // copy label while (octet) { - if (*inpos == inend) return -1; - if (bufused == bufsize - 1) return -2; + if (*inpos == inend) { + return -1; + } + if (bufused == bufsize - 1) { + return -2; + } int c = *((*inpos)++); - if (c == '.') return -1; + if (c == '.') { + return -1; + } octet--; buf[bufused++] = c; } @@ -99,22 +118,36 @@ while (*name != 0) { const char *dot = strchr(name, '.'); const char *fin = dot; - if (!dot) fin = name + strlen(name); - if (fin - name > 63) return -1; - if (fin == name) return -3; - if (outend - *outpos < fin - name + 2) return -2; + if (!dot) { + fin = name + strlen(name); + } + if (fin - name > 63) { + return -1; + } + if (fin == name) { + return -3; + } + if (outend - *outpos < fin - name + 2) { + return -2; + } *((*outpos)++) = fin - name; memcpy(*outpos, name, fin - name); *outpos += fin - name; - if (!dot) break; + if (!dot) { + break; + } name = dot + 1; } if (offset < 0) { // no reference - if (outend == *outpos) return -2; + if (outend == *outpos) { + return -2; + } *((*outpos)++) = 0; } else { - if (outend - *outpos < 2) return -2; + if (outend - *outpos < 2) { + return -2; + } *((*outpos)++) = (offset >> 8) | 0xC0; *((*outpos)++) = offset & 0xFF; } @@ -156,11 +189,15 @@ static int write_record_a(uint8_t **outpos, const uint8_t *outend, const char *name, int offset, dns_class cls, int ttl, const addr_t *ip) { - if (ip->v != 4) return -6; + if (ip->v != 4) { + return -6; + } uint8_t *oldpos = *outpos; int error = 0; int ret = write_record(outpos, outend, name, offset, TYPE_A, cls, ttl); - if (ret) return ret; + if (ret) { + return ret; + } if (outend - *outpos < 6) { error = -5; goto error; @@ -169,8 +206,9 @@ *((*outpos)++) = 0; *((*outpos)++) = 4; // rdata - for (int i = 0; i < 4; i++) + for (int i = 0; i < 4; i++) { *((*outpos)++) = ip->data.v4[i]; + } return 0; error: *outpos = oldpos; @@ -180,11 +218,15 @@ static int write_record_aaaa(uint8_t **outpos, const uint8_t *outend, const char *name, int offset, dns_class cls, int ttl, const addr_t *ip) { - if (ip->v != 6) return -6; + if (ip->v != 6) { + return -6; + } uint8_t *oldpos = *outpos; int error = 0; int ret = write_record(outpos, outend, name, offset, TYPE_AAAA, cls, ttl); - if (ret) return ret; + if (ret) { + return ret; + } if (outend - *outpos < 6) { error = -5; goto error; @@ -193,8 +235,9 @@ *((*outpos)++) = 0; *((*outpos)++) = 16; // rdata - for (int i = 0; i < 16; i++) + for (int i = 0; i < 16; i++) { *((*outpos)++) = ip->data.v6[i]; + } return 0; error: *outpos = oldpos; @@ -423,8 +466,9 @@ write_record_soa(&newpos, outend, "", offset, CLASS_IN, opt->nsttl, opt->ns, opt->mbox, time(NULL), 604800, 86400, 2592000, 604800); - if (max_auth_size < newpos - outpos) + if (max_auth_size < newpos - outpos) { max_auth_size = newpos - outpos; + } // fprintf(stdout, "Authority section will claim %i bytes max\n", // max_auth_size); } @@ -535,7 +579,9 @@ struct sockaddr_in6 si_other; int senderSocket = -1; senderSocket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); - if (senderSocket == -1) return -3; + if (senderSocket == -1) { + return -3; + } int replySocket; if (listenSocket == -1) { @@ -556,8 +602,10 @@ 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) + if (bind(listenSocket, (struct sockaddr *)&si_me, sizeof(si_me)) == + -1) { return -2; + } } uint8_t inbuf[BUFLEN], outbuf[BUFLEN]; @@ -583,10 +631,14 @@ // fprintf(stdout, "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; + if (insize <= 0) { + continue; + } ssize_t ret = dnshandle(opt, inbuf, insize, outbuf); - if (ret <= 0) continue; + if (ret <= 0) { + continue; + } bool handled = false; for (struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg); hdr; @@ -601,9 +653,10 @@ handled = true; } } - if (!handled) + if (!handled) { sendto(listenSocket, outbuf, ret, 0, (struct sockaddr *)&si_other, sizeof(si_other)); + } } return 0; }