1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
7 int nGotIRCAddresses
= 0;
19 string
EncodeAddress(const CAddress
& addr
)
25 vector
<unsigned char> vch(UBEGIN(tmp
), UEND(tmp
));
26 return string("u") + EncodeBase58Check(vch
);
29 bool DecodeAddress(string str
, CAddress
& addr
)
31 vector
<unsigned char> vch
;
32 if (!DecodeBase58Check(str
.substr(1), vch
))
36 if (vch
.size() != sizeof(tmp
))
38 memcpy(&tmp
, &vch
[0], sizeof(tmp
));
40 addr
= CAddress(tmp
.ip
, tmp
.port
, NODE_NETWORK
);
49 static bool Send(SOCKET hSocket
, const char* pszSend
)
51 if (strstr(pszSend
, "PONG") != pszSend
)
52 printf("IRC SENDING: %s\n", pszSend
);
53 const char* psz
= pszSend
;
54 const char* pszEnd
= psz
+ strlen(psz
);
57 int ret
= send(hSocket
, psz
, pszEnd
- psz
, MSG_NOSIGNAL
);
65 bool RecvLine(SOCKET hSocket
, string
& strLine
)
71 int nBytes
= recv(hSocket
, &c
, 1, 0);
79 if (strLine
.size() >= 9000)
87 printf("IRC socket closed\n");
93 int nErr
= WSAGetLastError();
94 if (nErr
!= WSAEMSGSIZE
&& nErr
!= WSAEINTR
&& nErr
!= WSAEINPROGRESS
)
96 printf("IRC recv failed: %d\n", nErr
);
103 bool RecvLineIRC(SOCKET hSocket
, string
& strLine
)
107 bool fRet
= RecvLine(hSocket
, strLine
);
112 vector
<string
> vWords
;
113 ParseString(strLine
, ' ', vWords
);
114 if (vWords
.size() >= 1 && vWords
[0] == "PING")
118 Send(hSocket
, strLine
.c_str());
126 int RecvUntil(SOCKET hSocket
, const char* psz1
, const char* psz2
=NULL
, const char* psz3
=NULL
)
131 if (!RecvLineIRC(hSocket
, strLine
))
133 printf("IRC %s\n", strLine
.c_str());
134 if (psz1
&& strLine
.find(psz1
) != -1)
136 if (psz2
&& strLine
.find(psz2
) != -1)
138 if (psz3
&& strLine
.find(psz3
) != -1)
143 bool Wait(int nSeconds
)
147 printf("IRC waiting %d seconds to reconnect\n", nSeconds
);
148 for (int i
= 0; i
< nSeconds
; i
++)
159 void ThreadIRCSeed(void* parg
)
161 if (mapArgs
.count("-connect"))
163 if (mapArgs
.count("-noirc"))
165 printf("ThreadIRCSeed started\n");
168 bool fNameInUse
= false;
169 bool fTOR
= (fUseProxy
&& addrProxy
.port
== htons(9050));
173 //CAddress addrConnect("216.155.130.130:6667"); // chat.freenode.net
174 CAddress
addrConnect("92.243.23.21:6667"); // irc.lfnet.org
177 //struct hostent* phostent = gethostbyname("chat.freenode.net");
178 struct hostent
* phostent
= gethostbyname("irc.lfnet.org");
179 if (phostent
&& phostent
->h_addr_list
&& phostent
->h_addr_list
[0])
180 addrConnect
= CAddress(*(u_long
*)phostent
->h_addr_list
[0], htons(6667));
184 if (!ConnectSocket(addrConnect
, hSocket
))
186 printf("IRC connect failed\n");
187 nErrorWait
= nErrorWait
* 11 / 10;
188 if (Wait(nErrorWait
+= 60))
194 if (!RecvUntil(hSocket
, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname"))
196 closesocket(hSocket
);
197 hSocket
= INVALID_SOCKET
;
198 nErrorWait
= nErrorWait
* 11 / 10;
199 if (Wait(nErrorWait
+= 60))
206 if (addrLocalHost
.IsRoutable() && !fUseProxy
&& !fNameInUse
)
207 strMyName
= EncodeAddress(addrLocalHost
);
209 strMyName
= strprintf("x%u", GetRand(1000000000));
211 Send(hSocket
, strprintf("NICK %s\r", strMyName
.c_str()).c_str());
212 Send(hSocket
, strprintf("USER %s 8 * : %s\r", strMyName
.c_str(), strMyName
.c_str()).c_str());
214 int nRet
= RecvUntil(hSocket
, " 004 ", " 433 ");
217 closesocket(hSocket
);
218 hSocket
= INVALID_SOCKET
;
221 printf("IRC name already in use\n");
226 nErrorWait
= nErrorWait
* 11 / 10;
227 if (Wait(nErrorWait
+= 60))
234 Send(hSocket
, "JOIN #bitcoin\r");
235 Send(hSocket
, "WHO #bitcoin\r");
237 int64 nStart
= GetTime();
239 while (!fShutdown
&& RecvLineIRC(hSocket
, strLine
))
241 if (strLine
.empty() || strLine
.size() > 900 || strLine
[0] != ':')
244 vector
<string
> vWords
;
245 ParseString(strLine
, ' ', vWords
);
246 if (vWords
.size() < 2)
252 if (vWords
[1] == "352" && vWords
.size() >= 8)
254 // index 7 is limited to 16 characters
255 // could get full length name at index 10, but would be different from join messages
256 strlcpy(pszName
, vWords
[7].c_str(), sizeof(pszName
));
257 printf("IRC got who\n");
260 if (vWords
[1] == "JOIN" && vWords
[0].size() > 1)
262 // :username!username@50000007.F000000B.90000002.IP JOIN :#channelname
263 strlcpy(pszName
, vWords
[0].c_str() + 1, sizeof(pszName
));
264 if (strchr(pszName
, '!'))
265 *strchr(pszName
, '!') = '\0';
266 printf("IRC got join\n");
269 if (pszName
[0] == 'u')
272 if (DecodeAddress(pszName
, addr
))
274 addr
.nTime
= GetAdjustedTime() - 51 * 60;
275 if (AddAddress(addr
))
276 printf("IRC got new address\n");
281 printf("IRC decode failed\n");
285 closesocket(hSocket
);
286 hSocket
= INVALID_SOCKET
;
288 // IRC usually blocks TOR, so only try once
292 if (GetTime() - nStart
> 20 * 60)
298 nRetryWait
= nRetryWait
* 11 / 10;
299 if (!Wait(nRetryWait
+= 60))
314 int main(int argc
, char *argv
[])
317 if (WSAStartup(MAKEWORD(2,2), &wsadata
) != NO_ERROR
)
319 printf("Error at WSAStartup()\n");