- 31EB2CBDF4F83F10AE8A7CDD3A69312BA6EAFBECFAFBEDDF7546CE99847BD4F2A674037E2B89A0A7B91C37127D9770501C265A7977EDB0AE0B3A5964272692F9
+ 718F14E6ECCFFD8FC0448E37B114651D90FBF2076FDBB55167E8999A6066213D968E097DE9802F5DEA987CC66DC819E27EC9A01820DA511C8DD2DB064FDA24D4
bitcoin/src/net.cpp
(18 . 8)(18 . 8)
35 void ThreadMessageHandler2(void* parg);
36 void ThreadSocketHandler2(void* parg);
37 void ThreadOpenConnections2(void* parg);
38 bool OpenNetworkConnection(const CAddress& addrConnect);
39
40 void ThreadOpenWires2(void* parg);
41 bool OpenNetworkConnection(const CAddress& addrConnect, bool fWireConnection=false);
42
43
44
(37 . 6)(37 . 7)
46
47 vector<CNode*> vNodes;
48 CCriticalSection cs_vNodes;
49 map<vector<unsigned char>, CAddress> mapWires;
50 map<vector<unsigned char>, CAddress> mapAddresses;
51 CCriticalSection cs_mapAddresses;
52 map<CInv, CDataStream> mapRelay;
(243 . 6)(244 . 20)
54 }
55
56
57 // 'Wires' don't get saved to the DB; they don't get shared with peers,
58 // including one another; they don't voluntarily get disconnected;
59 // if disconnected by other side, they will bang on the door forever;
60 // they carry no ban score. Think of them as 'leased lines.'
61 // They are also permitted to be ports on localhost! (tunnelers, e.g., 'g'.)
62 // Use with caution!!!
63 bool AddWire(CAddress addr)
64 {
65 printf("AddWire(%s)\n", addr.ToString().c_str());
66 mapWires.insert(make_pair(addr.GetKey(), addr)); // NO error checking!
67 return true;
68 }
69
70
71 bool AddAddress(CAddress addr, int64 nTimePenalty, CAddrDB *pAddrDB)
72 {
73 if (!addr.IsRoutable())
(452 . 31)(467 . 26)
75 return NULL;
76 }
77
78 CNode* ConnectNode(CAddress addrConnect, int64 nTimeout)
79 CNode* ConnectNode(CAddress addrConnect, int64 nTimeout, bool fWireNode)
80 {
81 if (addrConnect.ip == addrLocalHost.ip)
82 if (!fWireNode && (addrConnect.ip == addrLocalHost.ip))
83 return NULL;
84
85 // Look for an existing connection
86 CNode* pnode = FindNode(addrConnect.ip);
87 if (pnode)
88 {
89 if (nTimeout != 0)
90 pnode->AddRef(nTimeout);
91 else
92 pnode->AddRef();
93 return pnode;
94 }
95
96
97 if (fWireNode)
98 nTimeout = 0;
99
100 /// debug print
101 printf("trying connection %s lastseen=%.1fhrs lasttry=%.1fhrs\n",
102 addrConnect.ToString().c_str(),
103 (double)(addrConnect.nTime - GetAdjustedTime())/3600.0,
104 (double)(addrConnect.nLastTry - GetAdjustedTime())/3600.0);
105
106 CRITICAL_BLOCK(cs_mapAddresses)
107 mapAddresses[addrConnect.GetKey()].nLastTry = GetAdjustedTime();
108
109 if (!fWireNode)
110 {
111 CRITICAL_BLOCK(cs_mapAddresses)
112 mapAddresses[addrConnect.GetKey()].nLastTry = GetAdjustedTime();
113 }
114
115 // Connect
116 SOCKET hSocket;
117 if (ConnectSocket(addrConnect, hSocket))
(490 . 6)(500 . 8)
119
120 // Add node
121 CNode* pnode = new CNode(hSocket, addrConnect, false);
122 pnode->fWireNode = fWireNode;
123
124 if (nTimeout != 0)
125 pnode->AddRef(nTimeout);
126 else
(508 . 6)(520 . 7)
128
129 void CNode::CloseSocketDisconnect()
130 {
131 if (fWireNode) return;
132 fDisconnect = true;
133 if (hSocket != INVALID_SOCKET)
134 {
(557 . 6)(570 . 12)
136
137 bool CNode::Misbehaving(int howmuch)
138 {
139 if (fWireNode)
140 {
141 printf("Warning: wire node %s misbehaving\n", addr.ToString().c_str());
142 return false;
143 }
144
145 if (addr.IsLocal())
146 {
147 printf("Warning: local node %s misbehaving\n", addr.ToString().c_str());
(624 . 8)(643 . 8)
149 vector<CNode*> vNodesCopy = vNodes;
150 BOOST_FOREACH(CNode* pnode, vNodesCopy)
151 {
152 if (pnode->fDisconnect ||
153 (pnode->GetRefCount() <= 0 && pnode->vRecv.empty() && pnode->vSend.empty()))
154 if (!pnode->fWireNode && (pnode->fDisconnect || // NEVER disconnect wires
155 (pnode->GetRefCount() <= 0 && pnode->vRecv.empty() && pnode->vSend.empty())))
156 {
157 // remove from vNodes
158 vNodes.erase(remove(vNodes.begin(), vNodes.end(), pnode), vNodes.end());
(1061 . 19)(1080 . 23)
160 }
161 }
162
163 bool OpenNetworkConnection(const CAddress& addrConnect)
164 bool OpenNetworkConnection(const CAddress& addrConnect, bool fWireConnection)
165 {
166 //
167 // Initiate outbound network connection
168 //
169 if (fShutdown)
170 return false;
171 if (addrConnect.ip == addrLocalHost.ip || !addrConnect.IsIPv4() ||
172 FindNode(addrConnect.ip) || CNode::IsBanned(addrConnect.ip))
173
174 if (!fWireConnection &&
175 (addrConnect.ip == addrLocalHost.ip || CNode::IsBanned(addrConnect.ip)))
176 return false;
177
178 if (!addrConnect.IsIPv4() || FindNode(addrConnect.ip))
179 return false;
180
181 vnThreadsRunning[1]--;
182 CNode* pnode = ConnectNode(addrConnect);
183 CNode* pnode = ConnectNode(addrConnect, 0, fWireConnection);
184 vnThreadsRunning[1]++;
185 if (fShutdown)
186 return false;
(1085 . 10)(1108 . 42)
188 }
189
190
191 void ThreadOpenWires(void* parg)
192 {
193 IMPLEMENT_RANDOMIZE_STACK(ThreadOpenWires(parg));
194 try
195 {
196 vnThreadsRunning[5]++;
197 ThreadOpenWires2(parg);
198 vnThreadsRunning[5]--;
199 }
200 catch (std::exception& e) {
201 vnThreadsRunning[5]--;
202 PrintException(&e, "ThreadOpenWires()");
203 } catch (...) {
204 vnThreadsRunning[5]--;
205 PrintException(NULL, "ThreadOpenWires()");
206 }
207 printf("ThreadOpenWires exiting\n");
208 }
209
210
211 void ThreadOpenWires2(void* parg)
212 {
213 printf("ThreadOpenWires started\n");
214
215
216 while (1)
217 {
218 BOOST_FOREACH(const PAIRTYPE(vector<unsigned char>, CAddress)& item, mapWires)
219 {
220 const CAddress& addr = item.second;
221 OpenNetworkConnection(addr, true);
222 if (fShutdown)
223 return;
224 }
225 Sleep(500);
226 }
227 }
228
229
230 void ThreadMessageHandler(void* parg)
(1298 . 6)(1353 . 10)
232 if (!CreateThread(ThreadOpenConnections, NULL))
233 printf("Error: CreateThread(ThreadOpenConnections) failed\n");
234
235 // Initiate outbound connections
236 if (!CreateThread(ThreadOpenWires, NULL))
237 printf("Error: CreateThread(ThreadOpenWires) failed\n");
238
239 // Process messages
240 if (!CreateThread(ThreadMessageHandler, NULL))
241 printf("Error: CreateThread(ThreadMessageHandler) failed\n");
(1312 . 7)(1371 . 7)
243 fShutdown = true;
244 nTransactionsUpdated++;
245 int64 nStart = GetTime();
246 while (vnThreadsRunning[0] > 0 || vnThreadsRunning[1] > 0 || vnThreadsRunning[2] > 0 || vnThreadsRunning[3] > 0 || vnThreadsRunning[4] > 0
247 while (vnThreadsRunning[0] > 0 || vnThreadsRunning[1] > 0 || vnThreadsRunning[2] > 0 || vnThreadsRunning[3] > 0 || vnThreadsRunning[4] > 0 || vnThreadsRunning[5] > 0
248 )
249 {
250 if (GetTime() - nStart > 20)
(1324 . 6)(1383 . 7)
252 if (vnThreadsRunning[2] > 0) printf("ThreadMessageHandler still running\n");
253 if (vnThreadsRunning[3] > 0) printf("ThreadBitcoinMiner still running\n");
254 if (vnThreadsRunning[4] > 0) printf("ThreadRPCServer still running\n");
255 if (vnThreadsRunning[5] > 0) printf("ThreadOpenWires still running\n");
256 while (vnThreadsRunning[2] > 0 || vnThreadsRunning[4] > 0)
257 Sleep(20);
258 Sleep(50);