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