diff options
Diffstat (limited to 'src/net.h')
| -rw-r--r-- | src/net.h | 204 |
1 files changed, 74 insertions, 130 deletions
| @@ -5,7 +5,6 @@ | |||
| 5 | class CMessageHeader; | 5 | class CMessageHeader; |
| 6 | class CAddress; | 6 | class CAddress; |
| 7 | class CInv; | 7 | class CInv; |
| 8 | class CRequestTracker; | ||
| 9 | class CNode; | 8 | class CNode; |
| 10 | // Defined in main.h, which is included after this file. Only ever used here as | 9 | // Defined in main.h, which is included after this file. Only ever used here as |
| 11 | // a pointer, so the forward declaration is enough. | 10 | // a pointer, so the forward declaration is enough. |
| @@ -14,7 +13,6 @@ class CBlockIndex; | |||
| 14 | 13 | ||
| 15 | 14 | ||
| 16 | static const unsigned short DEFAULT_PORT = htons(8433); | 15 | static const unsigned short DEFAULT_PORT = htons(8433); |
| 17 | static const unsigned int PUBLISH_HOPS = 5; | ||
| 18 | enum | 16 | enum |
| 19 | { | 17 | { |
| 20 | NODE_NETWORK = (1 << 0), | 18 | NODE_NETWORK = (1 << 0), |
| @@ -93,10 +91,9 @@ static const int BTF_HANDSHAKE_GRACE_SECS = 60; | |||
| 93 | 91 | ||
| 94 | // A serialized object cannot be larger than MAX_SIZE, so no valid network | 92 | // A serialized object cannot be larger than MAX_SIZE, so no valid network |
| 95 | // message needs the old 256 MB header allowance. Keep the wire cap aligned with | 93 | // message needs the old 256 MB header allowance. Keep the wire cap aligned with |
| 96 | // what the deserializer can accept and bound how long one peer may sit on a | 94 | // what the deserializer can accept. How long a peer may sit on a partial |
| 97 | // partial payload after a complete header has arrived. | 95 | // payload after a complete header has arrived is BTF_MSG_WAIT_MAX_SECS, below. |
| 98 | static const unsigned int MAX_PROTOCOL_MESSAGE_SIZE = MAX_SIZE; | 96 | static const unsigned int MAX_PROTOCOL_MESSAGE_SIZE = MAX_SIZE; |
| 99 | static const int BTF_INCOMPLETE_MESSAGE_TIMEOUT_SECS = 2 * 60; | ||
| 100 | 97 | ||
| 101 | // Ceiling on simultaneous connections. | 98 | // Ceiling on simultaneous connections. |
| 102 | // | 99 | // |
| @@ -136,6 +133,42 @@ static const unsigned int MAX_CONNECTIONS_PER_IP = 32; | |||
| 136 | // this long regardless of what is still buffered. | 133 | // this long regardless of what is still buffered. |
| 137 | static const int64 DISCONNECT_DRAIN_SECS = 60; | 134 | static const int64 DISCONNECT_DRAIN_SECS = 60; |
| 138 | 135 | ||
| 136 | // How long a pool may go without hearing a height from ANY peer before it | ||
| 137 | // stops issuing jobs. Long enough that ordinary peer churn and a quiet | ||
| 138 | // handshake window do not trip it; short enough that a deaf pool cannot build | ||
| 139 | // more than a few blocks of private fork before it is stopped. Heights | ||
| 140 | // 3966-4085 -- 120 blocks, 5890.50 BTF -- were mined in exactly this state. | ||
| 141 | static const int64 BTF_NO_PEER_HEIGHT_MAX_SECS = 20 * 60; | ||
| 142 | |||
| 143 | // How long a peer may leave this node waiting for the rest of a message it | ||
| 144 | // announced in a header. | ||
| 145 | // | ||
| 146 | // ProcessMessages rewinds the header and breaks when the body has not all | ||
| 147 | // arrived, which is correct for a message still in flight and unbounded for | ||
| 148 | // one that never completes: the same header is re-read, the loop breaks | ||
| 149 | // again, and that peer is never parsed again for the life of the process. | ||
| 150 | // Satoshi left the note -- "need a mechanism to give up waiting for overlong | ||
| 151 | // message size error" -- and this is it. | ||
| 152 | // | ||
| 153 | // It never self-corrects, because any byte that arrives refreshes nLastRecv, | ||
| 154 | // so the inactivity timeout that would otherwise drop the peer never fires. | ||
| 155 | // The socket stays healthy, select() keeps reporting it, and the node goes | ||
| 156 | // silently deaf one peer at a time. | ||
| 157 | // | ||
| 158 | // A header may legitimately outrun its body by a round trip or two; two | ||
| 159 | // minutes is far beyond that, and well inside BTF_RECV_TIMEOUT_SECS. | ||
| 160 | static const int64 BTF_MSG_WAIT_MAX_SECS = 2 * 60; | ||
| 161 | |||
| 162 | // Largest payload a peer may announce in a message header: MAX_PROTOCOL_MESSAGE_SIZE | ||
| 163 | // is defined further up this file. This tree reached the same cap by the same | ||
| 164 | // reasoning as upstream #109 -- a 256 MB allowance is eight times what the | ||
| 165 | // deserializer will accept (serialize.h throws above MAX_SIZE), so the header | ||
| 166 | // check admitted an allocation the parser would only reject later, after | ||
| 167 | // BTF_MSG_WAIT_MAX_SECS of a stalled peer slot -- so only the note survives. | ||
| 168 | // | ||
| 169 | // Both definitions were textually clean and 59 lines apart, so the replay onto | ||
| 170 | // upstream merged them without a conflict and only the compiler objected. | ||
| 171 | |||
| 139 | // Send a ping after this long with nothing to say, so a peer running the | 172 | // Send a ping after this long with nothing to say, so a peer running the |
| 140 | // inactivity check above does not mistake a quiet node for a dead one. Must | 173 | // inactivity check above does not mistake a quiet node for a dead one. Must |
| 141 | // stay well under BTF_RECV_TIMEOUT_SECS. | 174 | // stay well under BTF_RECV_TIMEOUT_SECS. |
| @@ -176,6 +209,7 @@ extern int nBestHeight; | |||
| 176 | // has told us anything. Median rather than maximum so one peer claiming an | 209 | // has told us anything. Median rather than maximum so one peer claiming an |
| 177 | // absurd height cannot move it. | 210 | // absurd height cannot move it. |
| 178 | int GetPeerMedianHeight(); | 211 | int GetPeerMedianHeight(); |
| 212 | int64 GetPeerHeightAgeSecs(); | ||
| 179 | 213 | ||
| 180 | // Lightweight .btf churn accounting. These counters are diagnostics only: they | 214 | // Lightweight .btf churn accounting. These counters are diagnostics only: they |
| 181 | // let an operator tell stale descriptors, relay pairing failures, and paired | 215 | // let an operator tell stale descriptors, relay pairing failures, and paired |
| @@ -193,8 +227,6 @@ void BtfChurnNoteHandshakeTimeout(const string& strBtfAddr, const string& strMee | |||
| 193 | void BtfPexCollect(std::vector<std::string>& vDescOut); | 227 | void BtfPexCollect(std::vector<std::string>& vDescOut); |
| 194 | // Verify descriptors a peer sent and remember the good ones. Returns how many. | 228 | // Verify descriptors a peer sent and remember the good ones. Returns how many. |
| 195 | int BtfPexAccept(const std::vector<std::string>& vDesc); | 229 | int BtfPexAccept(const std::vector<std::string>& vDesc); |
| 196 | void AbandonRequests(void (*fn)(void*, CDataStream&), void* param1); | ||
| 197 | bool AnySubscribed(unsigned int nChannel); | ||
| 198 | void ThreadBitcoinMiner(void* parg); | 230 | void ThreadBitcoinMiner(void* parg); |
| 199 | // Starts MinerThreadCount() miners; returns how many came up. | 231 | // Starts MinerThreadCount() miners; returns how many came up. |
| 200 | int StartMinerThreads(); | 232 | int StartMinerThreads(); |
| @@ -468,13 +500,19 @@ public: | |||
| 468 | 500 | ||
| 469 | 501 | ||
| 470 | 502 | ||
| 503 | // MSG_REVIEW, MSG_PRODUCT and MSG_TABLE no longer have a handler, a producer | ||
| 504 | // or a store -- the marketplace they belonged to is gone. They stay in the | ||
| 505 | // enum, and their names stay in ppszTypeName below, because CInv serialises | ||
| 506 | // `type` as a plain integer: dropping them would renumber nothing today but | ||
| 507 | // would make it tempting to renumber later, and MSG_TX and MSG_BLOCK are wire | ||
| 508 | // values shared with every other node. Reserved, never emitted, never served. | ||
| 471 | enum | 509 | enum |
| 472 | { | 510 | { |
| 473 | MSG_TX = 1, | 511 | MSG_TX = 1, |
| 474 | MSG_BLOCK, | 512 | MSG_BLOCK, |
| 475 | MSG_REVIEW, | 513 | MSG_REVIEW, // reserved, dead |
| 476 | MSG_PRODUCT, | 514 | MSG_PRODUCT, // reserved, dead |
| 477 | MSG_TABLE, | 515 | MSG_TABLE, // reserved, dead |
| 478 | }; | 516 | }; |
| 479 | 517 | ||
| 480 | static const char* ppszTypeName[] = | 518 | static const char* ppszTypeName[] = |
| @@ -561,23 +599,9 @@ public: | |||
| 561 | 599 | ||
| 562 | 600 | ||
| 563 | 601 | ||
| 564 | class CRequestTracker | 602 | // CRequestTracker and the PushRequest/"reply" round trip lived here. Nothing |
| 565 | { | 603 | // in this tree ever called PushRequest, so the table was always empty and the |
| 566 | public: | 604 | // "reply" handler was pure inbound parser. Both removed. |
| 567 | void (*fn)(void*, CDataStream&); | ||
| 568 | void* param1; | ||
| 569 | |||
| 570 | explicit CRequestTracker(void (*fnIn)(void*, CDataStream&)=NULL, void* param1In=NULL) | ||
| 571 | { | ||
| 572 | fn = fnIn; | ||
| 573 | param1 = param1In; | ||
| 574 | } | ||
| 575 | |||
| 576 | bool IsNull() | ||
| 577 | { | ||
| 578 | return fn == NULL; | ||
| 579 | } | ||
| 580 | }; | ||
| 581 | 605 | ||
| 582 | 606 | ||
| 583 | 607 | ||
| @@ -623,9 +647,6 @@ protected: | |||
| 623 | public: | 647 | public: |
| 624 | int64 nReleaseTime; | 648 | int64 nReleaseTime; |
| 625 | int64 nDisconnectSince; | 649 | int64 nDisconnectSince; |
| 626 | map<uint256, CRequestTracker> mapRequests; | ||
| 627 | CCriticalSection cs_mapRequests; | ||
| 628 | |||
| 629 | // inventory based relay | 650 | // inventory based relay |
| 630 | set<CInv> setInventoryKnown; | 651 | set<CInv> setInventoryKnown; |
| 631 | set<CInv> setInventoryKnown2; | 652 | set<CInv> setInventoryKnown2; |
| @@ -633,9 +654,6 @@ public: | |||
| 633 | CCriticalSection cs_inventory; | 654 | CCriticalSection cs_inventory; |
| 634 | multimap<int64, CInv> mapAskFor; | 655 | multimap<int64, CInv> mapAskFor; |
| 635 | 656 | ||
| 636 | // publish and subscription | ||
| 637 | vector<char> vfSubscribe; | ||
| 638 | |||
| 639 | // Last "btfpeers" we accepted from this node, to rate-limit the exchange. | 657 | // Last "btfpeers" we accepted from this node, to rate-limit the exchange. |
| 640 | int64 nLastPexRecv; | 658 | int64 nLastPexRecv; |
| 641 | 659 | ||
| @@ -653,12 +671,17 @@ public: | |||
| 653 | int64 nLastSend; | 671 | int64 nLastSend; |
| 654 | int64 nLastRecv; | 672 | int64 nLastRecv; |
| 655 | int64 nLastSendEmpty; | 673 | int64 nLastSendEmpty; |
| 656 | int64 nIncompleteMessageStart; | ||
| 657 | unsigned int nIncompleteMessageSize; | ||
| 658 | string strIncompleteMessageCommand; | ||
| 659 | string strBtfAddr; | 674 | string strBtfAddr; |
| 660 | string strBtfMeeting; | 675 | string strBtfMeeting; |
| 661 | 676 | ||
| 677 | // When this peer's message loop last rewound a header without having made | ||
| 678 | // progress, or 0 when it is not waiting, plus how much was buffered then. | ||
| 679 | // The pair times a STALL rather than a duration, so a large message | ||
| 680 | // arriving slowly is never mistaken for one that is never arriving. | ||
| 681 | // See ProcessMessages. | ||
| 682 | int64 nMsgWaitSince; | ||
| 683 | unsigned int nMsgWaitSize; | ||
| 684 | |||
| 662 | // Height this peer announced in its version message, or -1 if it sent a | 685 | // Height this peer announced in its version message, or -1 if it sent a |
| 663 | // version message that predates the field. Knowing how far along everyone | 686 | // version message that predates the field. Knowing how far along everyone |
| 664 | // else is, is the only way a node can tell "in sync" from "not hearing". | 687 | // else is, is the only way a node can tell "in sync" from "not hearing". |
| @@ -688,13 +711,14 @@ public: | |||
| 688 | nLastSend = 0; | 711 | nLastSend = 0; |
| 689 | nLastRecv = 0; | 712 | nLastRecv = 0; |
| 690 | nLastSendEmpty = GetTime(); | 713 | nLastSendEmpty = GetTime(); |
| 691 | nIncompleteMessageStart = 0; | ||
| 692 | nIncompleteMessageSize = 0; | ||
| 693 | strIncompleteMessageCommand.clear(); | ||
| 694 | strBtfAddr.clear(); | 714 | strBtfAddr.clear(); |
| 695 | strBtfMeeting.clear(); | 715 | strBtfMeeting.clear(); |
| 716 | // Not waiting on anything yet. Left indeterminate, the first partial | ||
| 717 | // message this peer sends is measured against whatever was in this | ||
| 718 | // memory, and ProcessMessages drops it as unreadable on first contact. | ||
| 719 | nMsgWaitSince = 0; | ||
| 720 | nMsgWaitSize = 0; | ||
| 696 | nStartingHeight = -1; | 721 | nStartingHeight = -1; |
| 697 | vfSubscribe.assign(256, false); | ||
| 698 | 722 | ||
| 699 | // Push a version message | 723 | // Push a version message |
| 700 | /// when NTP implemented, change to just nTime = GetAdjustedTime() | 724 | /// when NTP implemented, change to just nTime = GetAdjustedTime() |
| @@ -722,6 +746,11 @@ public: | |||
| 722 | { | 746 | { |
| 723 | if (hSocket != INVALID_SOCKET) | 747 | if (hSocket != INVALID_SOCKET) |
| 724 | { | 748 | { |
| 749 | // Route through BtfCloseSocket so the accounting sees this close. | ||
| 750 | // A raw closesocket() here was invisible to sockcount, which is | ||
| 751 | // why the leak numbers never added up, and leaving the descriptor | ||
| 752 | // set afterwards is how a number that now belongs to somebody else | ||
| 753 | // gets closed a second time. (#103) | ||
| 725 | BtfCloseSocket(hSocket); | 754 | BtfCloseSocket(hSocket); |
| 726 | hSocket = INVALID_SOCKET; | 755 | hSocket = INVALID_SOCKET; |
| 727 | } | 756 | } |
| @@ -968,49 +997,6 @@ public: | |||
| 968 | } | 997 | } |
| 969 | 998 | ||
| 970 | 999 | ||
| 971 | void PushRequest(const char* pszCommand, | ||
| 972 | void (*fn)(void*, CDataStream&), void* param1) | ||
| 973 | { | ||
| 974 | uint256 hashReply; | ||
| 975 | RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply)); | ||
| 976 | |||
| 977 | CRITICAL_BLOCK(cs_mapRequests) | ||
| 978 | mapRequests[hashReply] = CRequestTracker(fn, param1); | ||
| 979 | |||
| 980 | PushMessage(pszCommand, hashReply); | ||
| 981 | } | ||
| 982 | |||
| 983 | template<typename T1> | ||
| 984 | void PushRequest(const char* pszCommand, const T1& a1, | ||
| 985 | void (*fn)(void*, CDataStream&), void* param1) | ||
| 986 | { | ||
| 987 | uint256 hashReply; | ||
| 988 | RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply)); | ||
| 989 | |||
| 990 | CRITICAL_BLOCK(cs_mapRequests) | ||
| 991 | mapRequests[hashReply] = CRequestTracker(fn, param1); | ||
| 992 | |||
| 993 | PushMessage(pszCommand, hashReply, a1); | ||
| 994 | } | ||
| 995 | |||
| 996 | template<typename T1, typename T2> | ||
| 997 | void PushRequest(const char* pszCommand, const T1& a1, const T2& a2, | ||
| 998 | void (*fn)(void*, CDataStream&), void* param1) | ||
| 999 | { | ||
| 1000 | uint256 hashReply; | ||
| 1001 | RAND_bytes((unsigned char*)&hashReply, sizeof(hashReply)); | ||
| 1002 | |||
| 1003 | CRITICAL_BLOCK(cs_mapRequests) | ||
| 1004 | mapRequests[hashReply] = CRequestTracker(fn, param1); | ||
| 1005 | |||
| 1006 | PushMessage(pszCommand, hashReply, a1, a2); | ||
| 1007 | } | ||
| 1008 | |||
| 1009 | |||
| 1010 | |||
| 1011 | bool IsSubscribed(unsigned int nChannel); | ||
| 1012 | void Subscribe(unsigned int nChannel, unsigned int nHops=0); | ||
| 1013 | void CancelSubscribe(unsigned int nChannel); | ||
| 1014 | void Disconnect(); | 1000 | void Disconnect(); |
| 1015 | }; | 1001 | }; |
| 1016 | 1002 | ||
| @@ -1067,50 +1053,8 @@ inline void RelayMessage<>(const CInv& inv, const CDataStream& ss) | |||
| 1067 | 1053 | ||
| 1068 | 1054 | ||
| 1069 | 1055 | ||
| 1070 | // | 1056 | // The publish/subscribe advert templates that used to close this file |
| 1071 | // Templates for the publish and subscription system. | 1057 | // (AdvertStartPublish / AdvertStopPublish / AdvertRemoveSource) are gone along |
| 1072 | // The object being published as T& obj needs to have: | 1058 | // with the marketplace objects they carried. Nothing ever handled an inbound |
| 1073 | // a set<unsigned int> setSources member | 1059 | // "publish" or "pub-cancel", so the machinery only ever pushed; what it pushed |
| 1074 | // specializations of AdvertInsert and AdvertErase | 1060 | // was CProduct, which no longer exists. |
| 1075 | // Currently implemented for CTable and CProduct. | ||
| 1076 | // | ||
| 1077 | |||
| 1078 | template<typename T> | ||
| 1079 | void AdvertStartPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj) | ||
| 1080 | { | ||
| 1081 | // Add to sources | ||
| 1082 | obj.setSources.insert(pfrom->addr.ip); | ||
| 1083 | |||
| 1084 | if (!AdvertInsert(obj)) | ||
| 1085 | return; | ||
| 1086 | |||
| 1087 | // Relay | ||
| 1088 | CRITICAL_BLOCK(cs_vNodes) | ||
| 1089 | foreach(CNode* pnode, vNodes) | ||
| 1090 | if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel))) | ||
| 1091 | pnode->PushMessage("publish", nChannel, nHops, obj); | ||
| 1092 | } | ||
| 1093 | |||
| 1094 | template<typename T> | ||
| 1095 | void AdvertStopPublish(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj) | ||
| 1096 | { | ||
| 1097 | uint256 hash = obj.GetHash(); | ||
| 1098 | |||
| 1099 | CRITICAL_BLOCK(cs_vNodes) | ||
| 1100 | foreach(CNode* pnode, vNodes) | ||
| 1101 | if (pnode != pfrom && (nHops < PUBLISH_HOPS || pnode->IsSubscribed(nChannel))) | ||
| 1102 | pnode->PushMessage("pub-cancel", nChannel, nHops, hash); | ||
| 1103 | |||
| 1104 | AdvertErase(obj); | ||
| 1105 | } | ||
| 1106 | |||
| 1107 | template<typename T> | ||
| 1108 | void AdvertRemoveSource(CNode* pfrom, unsigned int nChannel, unsigned int nHops, T& obj) | ||
| 1109 | { | ||
| 1110 | // Remove a source | ||
| 1111 | obj.setSources.erase(pfrom->addr.ip); | ||
| 1112 | |||
| 1113 | // If no longer supported by any sources, cancel it | ||
| 1114 | if (obj.setSources.empty()) | ||
| 1115 | AdvertStopPublish(pfrom, nChannel, nHops, obj); | ||
| 1116 | } | ||