Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
net.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_NET_H
7 #define BITCOIN_NET_H
8 
9 #include "bloom.h"
10 #include "compat.h"
11 #include "hash.h"
12 #include "limitedmap.h"
13 #include "mruset.h"
14 #include "netbase.h"
15 #include "protocol.h"
16 #include "sync.h"
17 #include "uint256.h"
18 #include "util.h"
19 
20 #include <deque>
21 #include <stdint.h>
22 
23 #ifndef WIN32
24 #include <arpa/inet.h>
25 #endif
26 
27 #include <boost/foreach.hpp>
28 #include <boost/signals2/signal.hpp>
29 #include <openssl/rand.h>
30 
31 class CAddrMan;
32 class CBlockIndex;
33 class CNode;
34 
35 namespace boost {
36  class thread_group;
37 }
38 
40 static const unsigned int MAX_INV_SZ = 50000;
42 static const size_t MAPASKFOR_MAX_SZ = MAX_INV_SZ;
43 
44 inline unsigned int ReceiveFloodSize() { return 1000*GetArg("-maxreceivebuffer", 5*1000); }
45 inline unsigned int SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 1*1000); }
46 
47 void AddOneShot(std::string strDest);
48 bool RecvLine(SOCKET hSocket, std::string& strLine);
49 bool GetMyExternalIP(CNetAddr& ipRet);
50 void AddressCurrentlyConnected(const CService& addr);
51 CNode* FindNode(const CNetAddr& ip);
52 CNode* FindNode(const CService& ip);
53 CNode* ConnectNode(CAddress addrConnect, const char *strDest = NULL);
54 void MapPort(bool fUseUPnP);
55 unsigned short GetListenPort();
56 bool BindListenPort(const CService &bindAddr, std::string& strError=REF(std::string()));
57 void StartNode(boost::thread_group& threadGroup);
58 bool StopNode();
59 void SocketSendData(CNode *pnode);
60 
61 typedef int NodeId;
62 
63 // Signals for message handling
65 {
66  boost::signals2::signal<int ()> GetHeight;
67  boost::signals2::signal<bool (CNode*)> ProcessMessages;
68  boost::signals2::signal<bool (CNode*, bool)> SendMessages;
69  boost::signals2::signal<void (NodeId, const CNode*)> InitializeNode;
70  boost::signals2::signal<void (NodeId)> FinalizeNode;
71 };
72 
73 
75 
76 
77 enum
78 {
79  LOCAL_NONE, // unknown
80  LOCAL_IF, // address a local interface listens on
81  LOCAL_BIND, // address explicit bound to
82  LOCAL_UPNP, // address reported by UPnP
83  LOCAL_HTTP, // address reported by whatismyip.com and similar
84  LOCAL_MANUAL, // address explicitly specified (-externalip=)
85 
87 };
88 
89 void SetLimited(enum Network net, bool fLimited = true);
90 bool IsLimited(enum Network net);
91 bool IsLimited(const CNetAddr& addr);
92 bool AddLocal(const CService& addr, int nScore = LOCAL_NONE);
93 bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE);
94 bool SeenLocal(const CService& addr);
95 bool IsLocal(const CService& addr);
96 bool GetLocal(CService &addr, const CNetAddr *paddrPeer = NULL);
97 bool IsReachable(const CNetAddr &addr);
98 void SetReachable(enum Network net, bool fFlag = true);
99 CAddress GetLocalAddress(const CNetAddr *paddrPeer = NULL);
100 
101 
102 extern bool fDiscover;
103 extern uint64_t nLocalServices;
104 extern uint64_t nLocalHostNonce;
105 extern CAddrMan addrman;
106 extern int nMaxConnections;
107 
108 extern std::vector<CNode*> vNodes;
110 extern std::map<CInv, CDataStream> mapRelay;
111 extern std::deque<std::pair<int64_t, CInv> > vRelayExpiration;
114 
115 extern std::vector<std::string> vAddedNodes;
117 
118 extern NodeId nLastNodeId;
120 
122  int nScore;
123  int nPort;
124 };
125 
127 extern map<CNetAddr, LocalServiceInfo> mapLocalHost;
128 
130 {
131 public:
132  NodeId nodeid;
133  uint64_t nServices;
134  int64_t nLastSend;
135  int64_t nLastRecv;
136  int64_t nTimeConnected;
137  std::string addrName;
138  int nVersion;
139  std::string cleanSubVer;
140  bool fInbound;
142  uint64_t nSendBytes;
143  uint64_t nRecvBytes;
144  bool fSyncNode;
145  double dPingTime;
146  double dPingWait;
147  std::string addrLocal;
148 };
149 
150 
151 
152 
153 class CNetMessage {
154 public:
155  bool in_data; // parsing header (false) or data (true)
156 
157  CDataStream hdrbuf; // partially received header
158  CMessageHeader hdr; // complete header
159  unsigned int nHdrPos;
160 
161  CDataStream vRecv; // received message data
162  unsigned int nDataPos;
163 
164  CNetMessage(int nTypeIn, int nVersionIn) : hdrbuf(nTypeIn, nVersionIn), vRecv(nTypeIn, nVersionIn) {
165  hdrbuf.resize(24);
166  in_data = false;
167  nHdrPos = 0;
168  nDataPos = 0;
169  }
170 
171  bool complete() const
172  {
173  if (!in_data)
174  return false;
175  return (hdr.nMessageSize == nDataPos);
176  }
177 
178  void SetVersion(int nVersionIn)
179  {
180  hdrbuf.SetVersion(nVersionIn);
181  vRecv.SetVersion(nVersionIn);
182  }
183 
184  int readHeader(const char *pch, unsigned int nBytes);
185  int readData(const char *pch, unsigned int nBytes);
186 };
187 
188 
189 
190 
191 
193 class CNode
194 {
195 public:
196  // socket
197  uint64_t nServices;
200  size_t nSendSize; // total size of all vSendMsg entries
201  size_t nSendOffset; // offset inside the first vSendMsg already sent
202  uint64_t nSendBytes;
203  std::deque<CSerializeData> vSendMsg;
205 
206  std::deque<CInv> vRecvGetData;
207  std::deque<CNetMessage> vRecvMsg;
209  uint64_t nRecvBytes;
211 
212  int64_t nLastSend;
213  int64_t nLastRecv;
214  int64_t nLastSendEmpty;
215  int64_t nTimeConnected;
217  std::string addrName;
219  int nVersion;
220  // strSubVer is whatever byte array we read from the wire. However, this field is intended
221  // to be printed out, displayed to humans in various forms and so on. So we sanitize it and
222  // store the sanitized version in cleanSubVer. The original should be used when dealing with
223  // the network or wire types and the cleaned string used when displayed or logged.
224  std::string strSubVer, cleanSubVer;
225  bool fOneShot;
226  bool fClient;
227  bool fInbound;
231  // We use fRelayTxes for two purposes -
232  // a) it allows us to not relay tx invs before receiving the peer's version message
233  // b) the peer may tell us in their version message that we should not relay tx invs
234  // until they have initialized their bloom filter.
240  NodeId id;
241 protected:
242 
243  // Denial-of-service detection/prevention
244  // Key is IP address, value is banned-until-time
245  static std::map<CNetAddr, int64_t> setBanned;
247 
248  // Basic fuzz-testing
249  void Fuzz(int nChance); // modifies ssSend
250 
251 public:
257 
258  // flood relay
259  std::vector<CAddress> vAddrToSend;
261  bool fGetAddr;
262  std::set<uint256> setKnown;
263 
264  // inventory based relay
266  std::vector<CInv> vInventoryToSend;
268  std::multimap<int64_t, CInv> mapAskFor;
269 
270  // Ping time measurement
271  uint64_t nPingNonceSent;
272  int64_t nPingUsecStart;
273  int64_t nPingUsecTime;
275 
276  CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn = "", bool fInboundIn=false) : ssSend(SER_NETWORK, INIT_PROTO_VERSION), setAddrKnown(5000)
277  {
278  nServices = 0;
279  hSocket = hSocketIn;
280  nRecvVersion = INIT_PROTO_VERSION;
281  nLastSend = 0;
282  nLastRecv = 0;
283  nSendBytes = 0;
284  nRecvBytes = 0;
285  nLastSendEmpty = GetTime();
286  nTimeConnected = GetTime();
287  addr = addrIn;
288  addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
289  nVersion = 0;
290  strSubVer = "";
291  fOneShot = false;
292  fClient = false; // set by version message
293  fInbound = fInboundIn;
294  fNetworkNode = false;
295  fSuccessfullyConnected = false;
296  fDisconnect = false;
297  nRefCount = 0;
298  nSendSize = 0;
299  nSendOffset = 0;
300  hashContinue = 0;
301  pindexLastGetBlocksBegin = 0;
302  hashLastGetBlocksEnd = 0;
303  nStartingHeight = -1;
304  fStartSync = false;
305  fGetAddr = false;
306  fRelayTxes = false;
307  setInventoryKnown.max_size(SendBufferSize() / 1000);
308  pfilter = new CBloomFilter();
309  nPingNonceSent = 0;
310  nPingUsecStart = 0;
311  nPingUsecTime = 0;
312  fPingQueued = false;
313 
314  {
315  LOCK(cs_nLastNodeId);
316  id = nLastNodeId++;
317  }
318 
319  // Be shy and don't send version until we hear
320  if (hSocket != INVALID_SOCKET && !fInbound)
321  PushVersion();
322 
324  }
325 
327  {
328  if (hSocket != INVALID_SOCKET)
329  {
330  closesocket(hSocket);
331  hSocket = INVALID_SOCKET;
332  }
333  if (pfilter)
334  delete pfilter;
336  }
337 
338 private:
339  // Network usage totals
342  static uint64_t nTotalBytesRecv;
343  static uint64_t nTotalBytesSent;
344 
345  CNode(const CNode&);
346  void operator=(const CNode&);
347 
348 public:
349 
350  NodeId GetId() const {
351  return id;
352  }
353 
355  {
356  assert(nRefCount >= 0);
357  return nRefCount;
358  }
359 
360  // requires LOCK(cs_vRecvMsg)
361  unsigned int GetTotalRecvSize()
362  {
363  unsigned int total = 0;
364  BOOST_FOREACH(const CNetMessage &msg, vRecvMsg)
365  total += msg.vRecv.size() + 24;
366  return total;
367  }
368 
369  // requires LOCK(cs_vRecvMsg)
370  bool ReceiveMsgBytes(const char *pch, unsigned int nBytes);
371 
372  // requires LOCK(cs_vRecvMsg)
373  void SetRecvVersion(int nVersionIn)
374  {
375  nRecvVersion = nVersionIn;
376  BOOST_FOREACH(CNetMessage &msg, vRecvMsg)
377  msg.SetVersion(nVersionIn);
378  }
379 
381  {
382  nRefCount++;
383  return this;
384  }
385 
386  void Release()
387  {
388  nRefCount--;
389  }
390 
391 
392 
393  void AddAddressKnown(const CAddress& addr)
394  {
395  setAddrKnown.insert(addr);
396  }
397 
398  void PushAddress(const CAddress& addr)
399  {
400  // Known checking here is only to save space from duplicates.
401  // SendMessages will filter it again for knowns that were added
402  // after addresses were pushed.
403  if (addr.IsValid() && !setAddrKnown.count(addr))
404  vAddrToSend.push_back(addr);
405  }
406 
407 
408  void AddInventoryKnown(const CInv& inv)
409  {
410  {
411  LOCK(cs_inventory);
412  setInventoryKnown.insert(inv);
413  }
414  }
415 
416  void PushInventory(const CInv& inv)
417  {
418  {
419  LOCK(cs_inventory);
420  if (!setInventoryKnown.count(inv))
421  vInventoryToSend.push_back(inv);
422  }
423  }
424 
425  void AskFor(const CInv& inv)
426  {
427  if (mapAskFor.size() > MAPASKFOR_MAX_SZ)
428  return;
429 
430  // We're using mapAskFor as a priority queue,
431  // the key is the earliest time the request can be sent
432  int64_t nRequestTime;
433  limitedmap<CInv, int64_t>::const_iterator it = mapAlreadyAskedFor.find(inv);
434  if (it != mapAlreadyAskedFor.end())
435  nRequestTime = it->second;
436  else
437  nRequestTime = 0;
438  LogPrint("net", "askfor %s %d (%s)\n", inv.ToString().c_str(), nRequestTime, DateTimeStrFormat("%H:%M:%S", nRequestTime/1000000).c_str());
439 
440  // Make sure not to reuse time indexes to keep things in the same order
441  int64_t nNow = GetTimeMicros() - 1000000;
442  static int64_t nLastTime;
443  ++nLastTime;
444  nNow = std::max(nNow, nLastTime);
445  nLastTime = nNow;
446 
447  // Each retry is 2 minutes after the last
448  nRequestTime = std::max(nRequestTime + 2 * 60 * 1000000, nNow);
449  if (it != mapAlreadyAskedFor.end())
450  mapAlreadyAskedFor.update(it, nRequestTime);
451  else
452  mapAlreadyAskedFor.insert(std::make_pair(inv, nRequestTime));
453  mapAskFor.insert(std::make_pair(nRequestTime, inv));
454  }
455 
456 
457 
458  // TODO: Document the postcondition of this function. Is cs_vSend locked?
459  void BeginMessage(const char* pszCommand) EXCLUSIVE_LOCK_FUNCTION(cs_vSend)
460  {
461  ENTER_CRITICAL_SECTION(cs_vSend);
462  assert(ssSend.size() == 0);
463  ssSend << CMessageHeader(pszCommand, 0);
464  LogPrint("net", "sending: %s ", pszCommand);
465  }
466 
467  // TODO: Document the precondition of this function. Is cs_vSend locked?
468  void AbortMessage() UNLOCK_FUNCTION(cs_vSend)
469  {
470  ssSend.clear();
471 
472  LEAVE_CRITICAL_SECTION(cs_vSend);
473 
474  LogPrint("net", "(aborted)\n");
475  }
476 
477  // TODO: Document the precondition of this function. Is cs_vSend locked?
478  void EndMessage() UNLOCK_FUNCTION(cs_vSend)
479  {
480  // The -*messagestest options are intentionally not documented in the help message,
481  // since they are only used during development to debug the networking code and are
482  // not intended for end-users.
483  if (mapArgs.count("-dropmessagestest") && GetRand(GetArg("-dropmessagestest", 2)) == 0)
484  {
485  LogPrint("net", "dropmessages DROPPING SEND MESSAGE\n");
486  AbortMessage();
487  return;
488  }
489  if (mapArgs.count("-fuzzmessagestest"))
490  Fuzz(GetArg("-fuzzmessagestest", 10));
491 
492  if (ssSend.size() == 0)
493  return;
494 
495  // Set the size
496  unsigned int nSize = ssSend.size() - CMessageHeader::HEADER_SIZE;
497  memcpy((char*)&ssSend[CMessageHeader::MESSAGE_SIZE_OFFSET], &nSize, sizeof(nSize));
498 
499  // Set the checksum
500  uint256 hash = Hash(ssSend.begin() + CMessageHeader::HEADER_SIZE, ssSend.end());
501  unsigned int nChecksum = 0;
502  memcpy(&nChecksum, &hash, sizeof(nChecksum));
503  assert(ssSend.size () >= CMessageHeader::CHECKSUM_OFFSET + sizeof(nChecksum));
504  memcpy((char*)&ssSend[CMessageHeader::CHECKSUM_OFFSET], &nChecksum, sizeof(nChecksum));
505 
506  LogPrint("net", "(%d bytes)\n", nSize);
507 
508  std::deque<CSerializeData>::iterator it = vSendMsg.insert(vSendMsg.end(), CSerializeData());
509  ssSend.GetAndClear(*it);
510  nSendSize += (*it).size();
511 
512  // If write queue empty, attempt "optimistic write"
513  if (it == vSendMsg.begin())
514  SocketSendData(this);
515 
516  LEAVE_CRITICAL_SECTION(cs_vSend);
517  }
518 
519  void PushVersion();
520 
521 
522  void PushMessage(const char* pszCommand)
523  {
524  try
525  {
526  BeginMessage(pszCommand);
527  EndMessage();
528  }
529  catch (...)
530  {
531  AbortMessage();
532  throw;
533  }
534  }
535 
536  template<typename T1>
537  void PushMessage(const char* pszCommand, const T1& a1)
538  {
539  try
540  {
541  BeginMessage(pszCommand);
542  ssSend << a1;
543  EndMessage();
544  }
545  catch (...)
546  {
547  AbortMessage();
548  throw;
549  }
550  }
551 
552  template<typename T1, typename T2>
553  void PushMessage(const char* pszCommand, const T1& a1, const T2& a2)
554  {
555  try
556  {
557  BeginMessage(pszCommand);
558  ssSend << a1 << a2;
559  EndMessage();
560  }
561  catch (...)
562  {
563  AbortMessage();
564  throw;
565  }
566  }
567 
568  template<typename T1, typename T2, typename T3>
569  void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3)
570  {
571  try
572  {
573  BeginMessage(pszCommand);
574  ssSend << a1 << a2 << a3;
575  EndMessage();
576  }
577  catch (...)
578  {
579  AbortMessage();
580  throw;
581  }
582  }
583 
584  template<typename T1, typename T2, typename T3, typename T4>
585  void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4)
586  {
587  try
588  {
589  BeginMessage(pszCommand);
590  ssSend << a1 << a2 << a3 << a4;
591  EndMessage();
592  }
593  catch (...)
594  {
595  AbortMessage();
596  throw;
597  }
598  }
599 
600  template<typename T1, typename T2, typename T3, typename T4, typename T5>
601  void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5)
602  {
603  try
604  {
605  BeginMessage(pszCommand);
606  ssSend << a1 << a2 << a3 << a4 << a5;
607  EndMessage();
608  }
609  catch (...)
610  {
611  AbortMessage();
612  throw;
613  }
614  }
615 
616  template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
617  void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6)
618  {
619  try
620  {
621  BeginMessage(pszCommand);
622  ssSend << a1 << a2 << a3 << a4 << a5 << a6;
623  EndMessage();
624  }
625  catch (...)
626  {
627  AbortMessage();
628  throw;
629  }
630  }
631 
632  template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
633  void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6, const T7& a7)
634  {
635  try
636  {
637  BeginMessage(pszCommand);
638  ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7;
639  EndMessage();
640  }
641  catch (...)
642  {
643  AbortMessage();
644  throw;
645  }
646  }
647 
648  template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
649  void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6, const T7& a7, const T8& a8)
650  {
651  try
652  {
653  BeginMessage(pszCommand);
654  ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8;
655  EndMessage();
656  }
657  catch (...)
658  {
659  AbortMessage();
660  throw;
661  }
662  }
663 
664  template<typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
665  void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5, const T6& a6, const T7& a7, const T8& a8, const T9& a9)
666  {
667  try
668  {
669  BeginMessage(pszCommand);
670  ssSend << a1 << a2 << a3 << a4 << a5 << a6 << a7 << a8 << a9;
671  EndMessage();
672  }
673  catch (...)
674  {
675  AbortMessage();
676  throw;
677  }
678  }
679 
680  bool IsSubscribed(unsigned int nChannel);
681  void Subscribe(unsigned int nChannel, unsigned int nHops=0);
682  void CancelSubscribe(unsigned int nChannel);
683  void CloseSocketDisconnect();
684  void Cleanup();
685 
686 
687  // Denial-of-service detection/prevention
688  // The idea is to detect peers that are behaving
689  // badly and disconnect/ban them, but do it in a
690  // one-coding-mistake-won't-shatter-the-entire-network
691  // way.
692  // IMPORTANT: There should be nothing I can give a
693  // node that it will forward on that will make that
694  // node's peers drop it. If there is, an attacker
695  // can isolate a node and/or try to split the network.
696  // Dropping a node for sending stuff that is invalid
697  // now but might be valid in a later version is also
698  // dangerous, because it can cause a network split
699  // between nodes running old code and nodes running
700  // new code.
701  static void ClearBanned(); // needed for unit testing
702  static bool IsBanned(CNetAddr ip);
703  static bool Ban(const CNetAddr &ip);
704  void copyStats(CNodeStats &stats);
705 
706  // Network stats
707  static void RecordBytesRecv(uint64_t bytes);
708  static void RecordBytesSent(uint64_t bytes);
709 
710  static uint64_t GetTotalBytesRecv();
711  static uint64_t GetTotalBytesSent();
712 };
713 
714 
715 
716 class CTransaction;
717 void RelayTransaction(const CTransaction& tx, const uint256& hash);
718 void RelayTransaction(const CTransaction& tx, const uint256& hash, const CDataStream& ss);
719 
721 class CAddrDB
722 {
723 private:
724  boost::filesystem::path pathAddr;
725 public:
726  CAddrDB();
727  bool Write(const CAddrMan& addr);
728  bool Read(CAddrMan& addr);
729 };
730 
731 #endif
std::map< K, V >::const_iterator const_iterator
Definition: limitedmap.h:18
CNetMessage(int nTypeIn, int nVersionIn)
Definition: net.h:164
void PushMessage(const char *pszCommand)
Definition: net.h:522
uint64_t GetRand(uint64_t nMax)
Definition: util.cpp:186
std::vector< CNode * > vNodes
Definition: net.cpp:63
static uint64_t GetTotalBytesRecv()
Definition: net.cpp:1858
int nStartingHeight
Definition: net.h:141
Access to the (IP) address database (peers.dat)
Definition: net.h:721
bool fDisconnect
Definition: net.h:230
#define EXCLUSIVE_LOCK_FUNCTION(...)
Definition: threadsafety.h:43
bool GetLocal(CService &addr, const CNetAddr *paddrPeer=NULL)
Definition: net.cpp:100
CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn="", bool fInboundIn=false)
Definition: net.h:276
CCriticalSection cs_filter
Definition: net.h:237
std::string addrName
Definition: net.h:217
const_iterator begin() const
Definition: serialize.h:924
void AddOneShot(std::string strDest)
Definition: net.cpp:88
STL-like map container that only keeps the N elements with the highest value.
Definition: limitedmap.h:12
Definition: init.h:13
void EndMessage() UNLOCK_FUNCTION(cs_vSend)
Definition: net.h:478
void GetAndClear(CSerializeData &data)
Definition: serialize.h:1124
void insert(const value_type &x)
Definition: limitedmap.h:36
CCriticalSection cs_mapRelay
Definition: net.cpp:67
void PushMessage(const char *pszCommand, const T1 &a1)
Definition: net.h:537
void PushMessage(const char *pszCommand, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4, const T5 &a5, const T6 &a6, const T7 &a7, const T8 &a8, const T9 &a9)
Definition: net.h:665
void CancelSubscribe(unsigned int nChannel)
CAddress addr
Definition: net.h:216
bool SeenLocal(const CService &addr)
vote for a local address
Definition: net.cpp:271
CCriticalSection cs_vNodes
Definition: net.cpp:64
static void ClearBanned()
Definition: net.cpp:543
bool StopNode()
Definition: net.cpp:1751
Definition: net.h:81
void update(const_iterator itIn, const mapped_type &v)
Definition: limitedmap.h:67
bool IsSubscribed(unsigned int nChannel)
CAddrDB()
Definition: net.cpp:1909
inv message data
Definition: protocol.h:105
void PushMessage(const char *pszCommand, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4, const T5 &a5, const T6 &a6)
Definition: net.h:617
std::string ToStringIPPort() const
Definition: netbase.cpp:1102
void resize(size_type n, value_type c=0)
Definition: serialize.h:930
std::pair< iterator, bool > insert(const key_type &x)
Definition: mruset.h:39
void AskFor(const CInv &inv)
Definition: net.h:425
#define closesocket(s)
Definition: compat.h:74
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:42
CAddrMan addrman
Definition: net.cpp:60
unsigned int nHdrPos
Definition: net.h:159
RAII-style semaphore lock.
Definition: sync.h:208
uint256 hashLastGetBlocksEnd
Definition: net.h:254
std::string cleanSubVer
Definition: net.h:139
void SetVersion(int nVersionIn)
Definition: net.h:178
u_int SOCKET
Definition: compat.h:47
bool RecvLine(SOCKET hSocket, std::string &strLine)
Definition: net.cpp:138
int64_t nTimeConnected
Definition: net.h:136
Definition: net.h:83
size_type max_size() const
Definition: mruset.h:53
void MapPort(bool fUseUPnP)
Definition: net.cpp:1154
Double ended buffer combining vector and stream-like interfaces.
Definition: serialize.h:839
CCriticalSection cs_inventory
Definition: net.h:267
CDataStream hdrbuf
Definition: net.h:157
std::vector< CAddress > vAddrToSend
Definition: net.h:259
CCriticalSection cs_vAddedNodes
Definition: net.cpp:77
#define INVALID_SOCKET
Definition: compat.h:58
void Cleanup()
Definition: net.cpp:517
void AddAddressKnown(const CAddress &addr)
Definition: net.h:393
int64_t nLastSendEmpty
Definition: net.h:214
static const size_t MAPASKFOR_MAX_SZ
The maximum number of entries in mapAskFor.
Definition: net.h:42
std::string cleanSubVer
Definition: net.h:224
void PushMessage(const char *pszCommand, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4, const T5 &a5)
Definition: net.h:601
std::string DateTimeStrFormat(const char *pszFormat, int64_t nTime)
Definition: util.cpp:1429
int64_t nPingUsecStart
Definition: net.h:272
void SetRecvVersion(int nVersionIn)
Definition: net.h:373
bool in_data
Definition: net.h:155
Definition: net.h:86
static void RecordBytesRecv(uint64_t bytes)
Definition: net.cpp:1846
CNode * ConnectNode(CAddress addrConnect, const char *strDest=NULL)
Definition: net.cpp:440
void PushInventory(const CInv &inv)
Definition: net.h:416
size_type count(const key_type &k) const
Definition: mruset.h:34
uint64_t nLocalHostNonce
Definition: net.cpp:58
bool fSyncNode
Definition: net.h:144
CAddress GetLocalAddress(const CNetAddr *paddrPeer=NULL)
Definition: net.cpp:125
std::deque< CInv > vRecvGetData
Definition: net.h:206
void BeginMessage(const char *pszCommand) EXCLUSIVE_LOCK_FUNCTION(cs_vSend)
Definition: net.h:459
#define UNLOCK_FUNCTION(...)
Definition: threadsafety.h:47
void PushMessage(const char *pszCommand, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4)
Definition: net.h:585
CDataStream vRecv
Definition: net.h:161
std::set< uint256 > setKnown
Definition: net.h:262
Stochastical (IP) address manager.
Definition: addrman.h:166
int64_t nLastRecv
Definition: net.h:213
limitedmap< CInv, int64_t > mapAlreadyAskedFor
void AddressCurrentlyConnected(const CService &addr)
Definition: net.cpp:400
std::deque< CNetMessage > vRecvMsg
Definition: net.h:207
std::deque< std::pair< int64_t, CInv > > vRelayExpiration
Definition: net.cpp:66
unsigned int GetTotalRecvSize()
Definition: net.h:361
int nVersion
Definition: net.h:138
int nStartingHeight
Definition: net.h:255
bool fClient
Definition: net.h:226
std::string strSubVer
Definition: net.h:224
void CloseSocketDisconnect()
Definition: net.cpp:497
std::map< CInv, CDataStream > mapRelay
Definition: net.cpp:65
bool fPingQueued
Definition: net.h:274
bool Write(const CAddrMan &addr)
Definition: net.cpp:1914
void Release()
Definition: net.h:386
bool IsReachable(const CNetAddr &addr)
check whether a given address is in a network we can probably connect to
Definition: net.cpp:293
#define LEAVE_CRITICAL_SECTION(cs)
Definition: sync.h:166
bool fInbound
Definition: net.h:227
static int LogPrint(const char *category, const char *format)
Definition: util.h:143
void RelayTransaction(const CTransaction &tx, const uint256 &hash)
Definition: net.cpp:1807
CCriticalSection cs_nLastNodeId
Definition: net.cpp:80
bool IsValid() const
Definition: netbase.cpp:695
bool fOneShot
Definition: net.h:225
static uint64_t nTotalBytesRecv
Definition: net.h:342
mruset< CAddress > setAddrKnown
Definition: net.h:260
void Fuzz(int nChance)
Definition: net.cpp:1870
unsigned int ReceiveFloodSize()
Definition: net.h:44
#define LOCK(cs)
Definition: sync.h:156
size_type size() const
Definition: serialize.h:928
static const int INIT_PROTO_VERSION
Definition: version.h:32
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netbase.h:94
std::vector< char, zero_after_free_allocator< char > > CSerializeData
Definition: serialize.h:832
CBlockIndex * pindexLastGetBlocksBegin
Definition: net.h:253
CMessageHeader hdr
Definition: net.h:158
static CCriticalSection cs_totalBytesRecv
Definition: net.h:340
bool fInbound
Definition: net.h:140
bool ReceiveMsgBytes(const char *pch, unsigned int nBytes)
Definition: net.cpp:613
mruset< CInv > setInventoryKnown
Definition: net.h:265
uint64_t nSendBytes
Definition: net.h:202
CNode * FindNode(const CNetAddr &ip)
Definition: net.cpp:413
A CService with information about it as peer.
Definition: protocol.h:68
unsigned int SendBufferSize()
Definition: net.h:45
int nRefCount
Definition: net.h:239
uint64_t nRecvBytes
Definition: net.h:143
int64_t nLastSend
Definition: net.h:212
double dPingTime
Definition: net.h:145
bool fSuccessfullyConnected
Definition: net.h:229
std::string addrName
Definition: net.h:137
bool GetMyExternalIP(CNetAddr &ipRet)
Definition: net.cpp:346
uint256 Hash(const T1 pbegin, const T1 pend)
Definition: hash.h:19
bool fGetAddr
Definition: net.h:261
int64_t GetTimeMicros()
Definition: util.h:317
uint64_t nRecvBytes
Definition: net.h:209
const_iterator end() const
Definition: limitedmap.h:31
uint64_t nSendBytes
Definition: net.h:142
size_t nSendOffset
Definition: net.h:201
void SetLimited(enum Network net, bool fLimited=true)
Make a particular network entirely off-limits (no automatic connects to it)
Definition: net.cpp:251
static uint64_t GetTotalBytesSent()
Definition: net.cpp:1864
void PushMessage(const char *pszCommand, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4, const T5 &a5, const T6 &a6, const T7 &a7, const T8 &a8)
Definition: net.h:649
boost::signals2::signal< bool(CNode *, bool)> SendMessages
Definition: net.h:68
static CCriticalSection cs_totalBytesSent
Definition: net.h:341
int64_t GetTime()
Definition: util.cpp:1215
size_t nSendSize
Definition: net.h:200
void PushVersion()
Definition: net.cpp:522
int nMaxConnections
Definition: net.cpp:61
map< CNetAddr, LocalServiceInfo > mapLocalHost
Definition: net.cpp:53
boost::signals2::signal< bool(CNode *)> ProcessMessages
Definition: net.h:67
int readData(const char *pch, unsigned int nBytes)
Definition: net.cpp:673
Definition: net.h:79
~CNode()
Definition: net.h:326
bool fStartSync
Definition: net.h:256
Network
Definition: netbase.h:26
void PushMessage(const char *pszCommand, const T1 &a1, const T2 &a2, const T3 &a3, const T4 &a4, const T5 &a5, const T6 &a6, const T7 &a7)
Definition: net.h:633
boost::signals2::signal< int()> GetHeight
Definition: net.h:66
#define ENTER_CRITICAL_SECTION(cs)
Definition: sync.h:160
void SocketSendData(CNode *pnode)
Definition: net.cpp:693
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netbase.h:40
static void RecordBytesSent(uint64_t bytes)
Definition: net.cpp:1852
void PushMessage(const char *pszCommand, const T1 &a1, const T2 &a2, const T3 &a3)
Definition: net.h:569
void StartNode(boost::thread_group &threadGroup)
Definition: net.cpp:1708
CService addrLocal
Definition: net.h:218
256-bit unsigned integer
Definition: uint256.h:531
void AddInventoryKnown(const CInv &inv)
Definition: net.h:408
int nScore
Definition: net.h:122
NodeId id
Definition: net.h:240
uint64_t nLocalServices
Definition: net.cpp:51
CCriticalSection cs_vRecvMsg
Definition: net.h:208
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: main.h:688
CSemaphoreGrant grantOutbound
Definition: net.h:236
std::vector< CInv > vInventoryToSend
Definition: net.h:266
uint64_t nServices
Definition: net.h:197
uint256 hashContinue
Definition: net.h:252
void * memcpy(void *a, const void *b, size_t c)
Definition: glibc_compat.cpp:7
boost::filesystem::path pathAddr
Definition: net.h:724
unsigned int nDataPos
Definition: net.h:162
static bool IsBanned(CNetAddr ip)
Definition: net.cpp:548
std::string addrLocal
Definition: net.h:147
void SetReachable(enum Network net, bool fFlag=true)
Definition: net.cpp:207
int NodeId
Definition: net.h:61
int nRecvVersion
Definition: net.h:210
NodeId GetId() const
Definition: net.h:350
unsigned int nMessageSize
Definition: protocol.h:57
static CCriticalSection cs_setBanned
Definition: net.h:246
void operator=(const CNode &)
uint64_t nServices
Definition: net.h:133
static bool Ban(const CNetAddr &ip)
Definition: net.cpp:564
std::vector< std::string > vAddedNodes
Definition: net.cpp:76
SOCKET hSocket
Definition: net.h:198
Definition: net.h:82
bool fRelayTxes
Definition: net.h:235
void Subscribe(unsigned int nChannel, unsigned int nHops=0)
bool BindListenPort(const CService &bindAddr, std::string &strError=REF(std::string()))
Definition: net.cpp:1561
void PushMessage(const char *pszCommand, const T1 &a1, const T2 &a2)
Definition: net.h:553
int readHeader(const char *pch, unsigned int nBytes)
Definition: net.cpp:641
int nVersion
Definition: net.h:219
bool complete() const
Definition: net.h:171
void clear()
Definition: serialize.h:934
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
Definition: util.cpp:505
uint64_t nPingNonceSent
Definition: net.h:271
int64_t nPingUsecTime
Definition: net.h:273
int64_t nTimeConnected
Definition: net.h:215
CBloomFilter * pfilter
Definition: net.h:238
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: core.h:183
Definition: net.h:80
bool fNetworkNode
Definition: net.h:228
Information about a peer.
Definition: net.h:193
boost::signals2::signal< void(NodeId)> FinalizeNode
Definition: net.h:70
std::string ToString() const
Definition: protocol.cpp:142
CCriticalSection cs_vSend
Definition: net.h:204
bool fDiscover
Definition: net.cpp:50
void SetVersion(int n)
Definition: serialize.h:1040
static uint64_t nTotalBytesSent
Definition: net.h:343
void AbortMessage() UNLOCK_FUNCTION(cs_vSend)
Definition: net.h:468
void copyStats(CNodeStats &stats)
Definition: net.cpp:576
CNode * AddRef()
Definition: net.h:380
double dPingWait
Definition: net.h:146
T & REF(const T &val)
Definition: serialize.h:35
boost::signals2::signal< void(NodeId, const CNode *)> InitializeNode
Definition: net.h:69
static const unsigned int MAX_INV_SZ
The maximum number of entries in an 'inv' protocol message.
Definition: net.h:40
bool AddLocal(const CService &addr, int nScore=LOCAL_NONE)
Definition: net.cpp:216
NodeId nLastNodeId
Definition: net.cpp:79
CNodeSignals & GetNodeSignals()
Definition: net.cpp:86
bool IsLocal(const CService &addr)
check whether a given address is potentially local
Definition: net.cpp:286
std::multimap< int64_t, CInv > mapAskFor
Definition: net.h:268
CCriticalSection cs_mapLocalHost
Definition: net.cpp:52
bool Read(CAddrMan &addr)
Definition: net.cpp:1952
const_iterator find(const key_type &k) const
Definition: limitedmap.h:34
int64_t nLastSend
Definition: net.h:134
static std::map< CNetAddr, int64_t > setBanned
Definition: net.h:245
int GetRefCount()
Definition: net.h:354
unsigned short GetListenPort()
Definition: net.cpp:94
bool IsLimited(enum Network net)
Definition: net.cpp:259
map< string, string > mapArgs
Definition: util.cpp:89
NodeId nodeid
Definition: net.h:132
int64_t nLastRecv
Definition: net.h:135
std::deque< CSerializeData > vSendMsg
Definition: net.h:203
const_iterator end() const
Definition: serialize.h:926
void PushAddress(const CAddress &addr)
Definition: net.h:398
CDataStream ssSend
Definition: net.h:199
Message header.
Definition: protocol.h:27