- 31EB2CBDF4F83F10AE8A7CDD3A69312BA6EAFBECFAFBEDDF7546CE99847BD4F2A674037E2B89A0A7B91C37127D9770501C265A7977EDB0AE0B3A5964272692F9
+ 4C763023D33EE372E2DE34FE6F345F7971A670ACCEC364E06828B7A24BF471F844F9535C263412BA779EF1A7E042D09CB7AB42FB7393A4AFE72B62E82EC37D3E
bitcoin/src/net.cpp
(18 . 8)(18 . 8)
52 void ThreadMessageHandler2(void* parg);
53 void ThreadSocketHandler2(void* parg);
54 void ThreadOpenConnections2(void* parg);
55 bool OpenNetworkConnection(const CAddress& addrConnect);
56
57 void ThreadOpenWires2(void* parg);
58 bool OpenNetworkConnection(const CAddress& addrConnect, bool fWireConnection=false);
59
60
61
(37 . 6)(37 . 7)
63
64 vector<CNode*> vNodes;
65 CCriticalSection cs_vNodes;
66 map<vector<unsigned char>, CAddress> mapWires;
67 map<vector<unsigned char>, CAddress> mapAddresses;
68 CCriticalSection cs_mapAddresses;
69 map<CInv, CDataStream> mapRelay;
(243 . 6)(244 . 20)
71 }
72
73
74 // 'Wires' don't get saved to the DB; they don't get shared with peers,
75 // including one another; they don't voluntarily get disconnected;
76 // if disconnected by other side, they will bang on the door forever;
77 // they carry no ban score. Think of them as 'leased lines.'
78 // They are also permitted to be ports on localhost! (tunnelers, e.g., 'g'.)
79 // Use with caution!!!
80 bool AddWire(CAddress addr)
81 {
82 printf("AddWire(%s)\n", addr.ToString().c_str());
83 mapWires.insert(make_pair(addr.GetKey(), addr)); // NO error checking!
84 return true;
85 }
86
87
88 bool AddAddress(CAddress addr, int64 nTimePenalty, CAddrDB *pAddrDB)
89 {
90 if (!addr.IsRoutable())
(452 . 31)(467 . 29)
92 return NULL;
93 }
94
95 CNode* ConnectNode(CAddress addrConnect, int64 nTimeout)
96 CNode* ConnectNode(CAddress addrConnect, int64 nTimeout, bool fWireNode)
97 {
98 if (addrConnect.ip == addrLocalHost.ip)
99 if (!fWireNode && (addrConnect.ip == addrLocalHost.ip))
100 return NULL;
101
102 // Look for an existing connection
103 CNode* pnode = FindNode(addrConnect.ip);
104 if (pnode)
105 {
106 if (nTimeout != 0)
107 pnode->AddRef(nTimeout);
108 else
109 pnode->AddRef();
110 return pnode;
111
112 if (fWireNode) {
113 nTimeout = 0;
114 printf("Connecting to wire node... %s\n",
115 addrConnect.ToString().c_str());
116 }
117
118
119 /// debug print
120 printf("trying connection %s lastseen=%.1fhrs lasttry=%.1fhrs\n",
121 addrConnect.ToString().c_str(),
122 (double)(addrConnect.nTime - GetAdjustedTime())/3600.0,
123 (double)(addrConnect.nLastTry - GetAdjustedTime())/3600.0);
124
125 CRITICAL_BLOCK(cs_mapAddresses)
126 mapAddresses[addrConnect.GetKey()].nLastTry = GetAdjustedTime();
127
128 if (!fWireNode)
129 {
130 CRITICAL_BLOCK(cs_mapAddresses)
131 mapAddresses[addrConnect.GetKey()].nLastTry = GetAdjustedTime();
132 }
133
134 // Connect
135 SOCKET hSocket;
136 if (ConnectSocket(addrConnect, hSocket))
(490 . 6)(503 . 10)
138
139 // Add node
140 CNode* pnode = new CNode(hSocket, addrConnect, false);
141 pnode->fWireNode = fWireNode;
142
143 if (fWireNode) printf("Added wire node !\n");
144
145 if (nTimeout != 0)
146 pnode->AddRef(nTimeout);
147 else
(508 . 6)(525 . 10)
149
150 void CNode::CloseSocketDisconnect()
151 {
152 if (fWireNode) {
153 printf("WARNING: disconnecting wire %s ! (will retry...)\n",
154 addr.ToString().c_str());
155 }
156 fDisconnect = true;
157 if (hSocket != INVALID_SOCKET)
158 {
(557 . 6)(578 . 12)
160
161 bool CNode::Misbehaving(int howmuch)
162 {
163 if (fWireNode)
164 {
165 printf("Warning: wire node %s misbehaving (no disconnect)\n", addr.ToString().c_str());
166 return false;
167 }
168
169 if (addr.IsLocal())
170 {
171 printf("Warning: local node %s misbehaving\n", addr.ToString().c_str());
(873 . 8)(900 . 8)
173 //
174 if (pnode->vSend.empty())
175 pnode->nLastSendEmpty = GetTime();
176 if (GetTime() - pnode->nTimeConnected > 60)
177 {
178 if ((!pnode->fWireNode) && (GetTime() - pnode->nTimeConnected > 60))
179 { // these conditions do not apply to wires !
180 if (pnode->nLastRecv == 0 || pnode->nLastSend == 0)
181 {
182 printf("socket no message in first 60 seconds, %d %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0);
(1061 . 19)(1088 . 23)
184 }
185 }
186
187 bool OpenNetworkConnection(const CAddress& addrConnect)
188 bool OpenNetworkConnection(const CAddress& addrConnect, bool fWireConnection)
189 {
190 //
191 // Initiate outbound network connection
192 //
193 if (fShutdown)
194 return false;
195 if (addrConnect.ip == addrLocalHost.ip || !addrConnect.IsIPv4() ||
196 FindNode(addrConnect.ip) || CNode::IsBanned(addrConnect.ip))
197
198 if (!fWireConnection &&
199 (addrConnect.ip == addrLocalHost.ip || CNode::IsBanned(addrConnect.ip)))
200 return false;
201
202 if (!addrConnect.IsIPv4() || FindNode(addrConnect.ip))
203 return false;
204
205 vnThreadsRunning[1]--;
206 CNode* pnode = ConnectNode(addrConnect);
207 CNode* pnode = ConnectNode(addrConnect, 0, fWireConnection);
208 vnThreadsRunning[1]++;
209 if (fShutdown)
210 return false;
(1085 . 10)(1116 . 42)
212 }
213
214
215 void ThreadOpenWires(void* parg)
216 {
217 IMPLEMENT_RANDOMIZE_STACK(ThreadOpenWires(parg));
218 try
219 {
220 vnThreadsRunning[5]++;
221 ThreadOpenWires2(parg);
222 vnThreadsRunning[5]--;
223 }
224 catch (std::exception& e) {
225 vnThreadsRunning[5]--;
226 PrintException(&e, "ThreadOpenWires()");
227 } catch (...) {
228 vnThreadsRunning[5]--;
229 PrintException(NULL, "ThreadOpenWires()");
230 }
231 printf("ThreadOpenWires exiting\n");
232 }
233
234
235 void ThreadOpenWires2(void* parg)
236 {
237 printf("ThreadOpenWires started\n");
238
239
240 while (1)
241 {
242 BOOST_FOREACH(const PAIRTYPE(vector<unsigned char>, CAddress)& item, mapWires)
243 {
244 const CAddress& addr = item.second;
245 OpenNetworkConnection(addr, true);
246 if (fShutdown)
247 return;
248 }
249 Sleep(wireRefresh);
250 }
251 }
252
253
254 void ThreadMessageHandler(void* parg)
(1298 . 6)(1361 . 10)
256 if (!CreateThread(ThreadOpenConnections, NULL))
257 printf("Error: CreateThread(ThreadOpenConnections) failed\n");
258
259 // Initiate outbound wires, if at least one was defined
260 if (fWires && !CreateThread(ThreadOpenWires, NULL))
261 printf("Error: CreateThread(ThreadOpenWires) failed\n");
262
263 // Process messages
264 if (!CreateThread(ThreadMessageHandler, NULL))
265 printf("Error: CreateThread(ThreadMessageHandler) failed\n");
(1312 . 7)(1379 . 7)
267 fShutdown = true;
268 nTransactionsUpdated++;
269 int64 nStart = GetTime();
270 while (vnThreadsRunning[0] > 0 || vnThreadsRunning[1] > 0 || vnThreadsRunning[2] > 0 || vnThreadsRunning[3] > 0 || vnThreadsRunning[4] > 0
271 while (vnThreadsRunning[0] > 0 || vnThreadsRunning[1] > 0 || vnThreadsRunning[2] > 0 || vnThreadsRunning[3] > 0 || vnThreadsRunning[4] > 0 || vnThreadsRunning[5] > 0
272 )
273 {
274 if (GetTime() - nStart > 20)
(1324 . 6)(1391 . 7)
276 if (vnThreadsRunning[2] > 0) printf("ThreadMessageHandler still running\n");
277 if (vnThreadsRunning[3] > 0) printf("ThreadBitcoinMiner still running\n");
278 if (vnThreadsRunning[4] > 0) printf("ThreadRPCServer still running\n");
279 if (vnThreadsRunning[5] > 0) printf("ThreadOpenWires still running\n");
280 while (vnThreadsRunning[2] > 0 || vnThreadsRunning[4] > 0)
281 Sleep(20);
282 Sleep(50);