Minor restyling & disable STUN logging by default.
authorCryptoManiac <balthazar@yandex.ru>
Mon, 11 Apr 2016 18:37:44 +0000 (21:37 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Mon, 11 Apr 2016 18:37:44 +0000 (21:37 +0300)
src/alert.cpp
src/net.cpp
src/stun.cpp

index 380fece..619cc52 100644 (file)
@@ -15,10 +15,10 @@ using namespace std;
 map<uint256, CAlert> mapAlerts;
 CCriticalSection cs_mapAlerts;
 
-static const char* pszMainKey = "043fa441fd4203d03f5df2b75ea14e36f20d39f43e7a61aa7552ab9bcd7ecb0e77a3be4585b13fcdaa22ef6e51f1ff6f2929bec2494385b086fb86610e33193195";
+static const string strMainKey = "043fa441fd4203d03f5df2b75ea14e36f20d39f43e7a61aa7552ab9bcd7ecb0e77a3be4585b13fcdaa22ef6e51f1ff6f2929bec2494385b086fb86610e33193195";
 
 // TestNet alerts pubKey
-static const char* pszTestKey = "0471dc165db490094d35cde15b1f5d755fa6ad6f2b5ed0f340e3f17f57389c3c2af113a8cbcc885bde73305a553b5640c83021128008ddf882e856336269080496";
+static const string strTestKey = "0471dc165db490094d35cde15b1f5d755fa6ad6f2b5ed0f340e3f17f57389c3c2af113a8cbcc885bde73305a553b5640c83021128008ddf882e856336269080496";
 
 // TestNet alerts private key
 // "308201130201010420b665cff1884e53da26376fd1b433812c9a5a8a4d5221533b15b9629789bb7e42a081a53081a2020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a1440342000471dc165db490094d35cde15b1f5d755fa6ad6f2b5ed0f340e3f17f57389c3c2af113a8cbcc885bde73305a553b5640c83021128008ddf882e856336269080496"
@@ -41,13 +41,13 @@ void CUnsignedAlert::SetNull()
     strReserved.clear();
 }
 
-std::string CUnsignedAlert::ToString() const
+string CUnsignedAlert::ToString() const
 {
-    std::string strSetCancel;
+    string strSetCancel;
     for(int32_t n :  setCancel)
         strSetCancel += strprintf("%" PRId32 " ", n);
-    std::string strSetSubVer;
-    for(std::string str :  setSubVer)
+    string strSetSubVer;
+    for(string str :  setSubVer)
         strSetSubVer += "\"" + str + "\" ";
     return strprintf(
         "CAlert(\n"
@@ -107,7 +107,7 @@ bool CAlert::Cancels(const CAlert& alert) const
     return (alert.nID <= nCancel || setCancel.count(alert.nID));
 }
 
-bool CAlert::AppliesTo(int nVersion, std::string strSubVerIn) const
+bool CAlert::AppliesTo(int nVersion, string strSubVerIn) const
 {
     // TODO: rework for client-version-embedded-in-strSubVer ?
     return (IsInEffect() &&
@@ -117,7 +117,7 @@ bool CAlert::AppliesTo(int nVersion, std::string strSubVerIn) const
 
 bool CAlert::AppliesToMe() const
 {
-    return AppliesTo(PROTOCOL_VERSION, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<std::string>()));
+    return AppliesTo(PROTOCOL_VERSION, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, vector<string>()));
 }
 
 bool CAlert::RelayTo(CNode* pnode) const
@@ -144,7 +144,7 @@ bool CAlert::RelayTo(CNode* pnode) const
 bool CAlert::CheckSignature() const
 {
     CPubKey key;
-    key.Set(ParseHex(fTestNet ? pszTestKey : pszMainKey));
+    key.Set(ParseHex(fTestNet ? strTestKey : strMainKey));
     if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig))
         return error("CAlert::CheckSignature() : verify signature failed");
 
@@ -180,7 +180,7 @@ bool CAlert::ProcessAlert()
     // alerts or it will be ignored (so an attacker can't
     // send an "everything is OK, don't panic" version that
     // cannot be overridden):
-    int maxInt = std::numeric_limits<int>::max();
+    int maxInt = numeric_limits<int>::max();
     if (nID == maxInt)
     {
         if (!(
@@ -200,7 +200,7 @@ bool CAlert::ProcessAlert()
         // Cancel previous alerts
         for (auto mi = mapAlerts.begin(); mi != mapAlerts.end();)
         {
-            const CAlert& alert = (*mi).second;
+            const auto& alert = (*mi).second;
             if (Cancels(alert))
             {
                 printf("cancelling alert %" PRId32 "\n", alert.nID);
@@ -220,7 +220,7 @@ bool CAlert::ProcessAlert()
         // Check if this alert has been cancelled
         for(auto& item : mapAlerts)
         {
-            const CAlert& alert = item.second;
+            const auto& alert = item.second;
             if (alert.Cancels(*this))
             {
                 printf("alert already cancelled by %" PRId32 "\n", alert.nID);
index d3e1bc4..c155638 100644 (file)
@@ -351,7 +351,9 @@ bool GetMyExternalIP(CNetAddr& ipRet)
     int rc = GetExternalIPbySTUN(rnd, &mapped, &srv);
     if(rc >= 0) {
         ipRet = CNetAddr(mapped.sin_addr);
-        printf("GetExternalIPbySTUN(%" PRIu64 ") returned %s in attempt %d; Server=%s\n", rnd, ipRet.ToStringIP().c_str(), rc, srv);
+        if (fDebugNet) {
+            printf("GetExternalIPbySTUN(%" PRIu64 ") returned %s in attempt %d; Server=%s\n", rnd, ipRet.ToStringIP().c_str(), rc, srv);
+        }
         return true;
     }
     return false;
@@ -370,10 +372,6 @@ void ThreadGetMyExternalIP(void* parg)
     }
 }
 
-
-
-
-
 void AddressCurrentlyConnected(const CService& addr)
 {
     addrman.Connected(addr);
index 42d02c5..d8dcfe9 100644 (file)
@@ -51,6 +51,8 @@
 #include "ministun.h"
 #include "netbase.h"
 
+using namespace std;
+
 extern int GetRandInt(int nMax);
 extern uint64_t GetRand(uint64_t nMax);
 
@@ -339,14 +341,14 @@ static int stun_send(int s, struct sockaddr_in *dst, struct stun_header *resp)
 }
 
 /* helper function to generate a random request id */
-static uint64_t randfiller = GetRand(std::numeric_limits<uint64_t>::max());
+static uint64_t randfiller = GetRand(numeric_limits<uint64_t>::max());
 static void stun_req_id(struct stun_header *req)
 {
     const uint64_t *S_block = (const uint64_t *)StunSrvList;
-    req->id.id[0] = GetRandInt(std::numeric_limits<int32_t>::max());
-    req->id.id[1] = GetRandInt(std::numeric_limits<int32_t>::max());
-    req->id.id[2] = GetRandInt(std::numeric_limits<int32_t>::max());
-    req->id.id[3] = GetRandInt(std::numeric_limits<int32_t>::max());
+    req->id.id[0] = GetRandInt(numeric_limits<int32_t>::max());
+    req->id.id[1] = GetRandInt(numeric_limits<int32_t>::max());
+    req->id.id[2] = GetRandInt(numeric_limits<int32_t>::max());
+    req->id.id[3] = GetRandInt(numeric_limits<int32_t>::max());
 
     req->id.id[0] |= 0x55555555;
     req->id.id[1] &= 0x55555555;
@@ -375,10 +377,9 @@ typedef int (stun_cb_f)(struct stun_attr *attr, void *arg);
 static int stun_handle_packet(int s, struct sockaddr_in *src,
                               unsigned char *data, size_t len, stun_cb_f *stun_cb, void *arg)
 {
-    struct stun_header *hdr = (struct stun_header *)data;
+    auto hdr = (struct stun_header *)data;
     struct stun_attr *attr;
     int ret = len;
-    unsigned int x;
 
     /* On entry, 'len' is the length of the udp payload. After the
    * initial checks it becomes the size of unprocessed options,
@@ -389,7 +390,7 @@ static int stun_handle_packet(int s, struct sockaddr_in *src,
 
     len -= sizeof(struct stun_header);
     data += sizeof(struct stun_header);
-    x = ntohs(hdr->msglen); /* len as advertised in the message */
+    uint32_t x = ntohs(hdr->msglen); /* len as advertised in the message */
     if(x < len)
         len = x;
 
@@ -438,8 +439,8 @@ static int stun_handle_packet(int s, struct sockaddr_in *src,
  */
 static int stun_get_mapped(struct stun_attr *attr, void *arg)
 {
-    struct stun_addr *addr = (struct stun_addr *)(attr + 1);
-    struct sockaddr_in *sa = (struct sockaddr_in *)arg;
+    auto addr = (struct stun_addr *)(attr + 1);
+    auto sa = (struct sockaddr_in *)arg;
 
     if (ntohs(attr->attr) != STUN_MAPPED_ADDRESS || ntohs(attr->len) != 8)
         return 1; /* not us. */
@@ -537,14 +538,11 @@ int GetExternalIPbySTUN(uint64_t rnd, struct sockaddr_in *mapped, const char **s
         step = rnd % StunSrvListQty;
     } while(step == 0);
 
-    uint16_t attempt;
-    for(attempt = 1; attempt < StunSrvListQty * 2; attempt++) {
+    for(uint16_t attempt = 1; attempt < StunSrvListQty * 2; attempt++) {
         pos = (pos + step) % StunSrvListQty;
         int rc = StunRequest(*srv = StunSrvList[pos].name, StunSrvList[pos].port, mapped);
         if(rc >= 0)
             return attempt;
-        // fprintf(stderr, "Lookup: %s:%u\t%s\t%d\n", StunSrvList[pos].name,
-        // StunSrvList[pos].port, inet_ntoa(mapped->sin_addr), rc);
     }
     return -1;
 }