diff options
author | Pieter Wuille <pieter.wuille@gmail.com> | 2012-01-03 23:33:31 +0100 |
---|---|---|
committer | Pieter Wuille <pieter.wuille@gmail.com> | 2012-01-06 18:55:37 +0100 |
commit | 67a42f929b1434f647c63922fd02dc2b93b28060 (patch) | |
tree | 9c4313e815bd77e817f2dc5b796347d343458d0e /src/irc.cpp | |
parent | 7486c64dd8436febbe59e82dbb875e83ad6b5194 (diff) |
Network stack refactor
This introduces CNetAddr and CService, respectively wrapping an
(IPv6) IP address and an IP+port combination. This functionality used
to be part of CAddress, which also contains network flags and
connection attempt information. These extra fields are however not
always necessary.
These classes, along with logic for creating connections and doing
name lookups, are moved to netbase.{h,cpp}, which does not depend on
headers.h.
Furthermore, CNetAddr is mostly IPv6-ready, though IPv6
functionality is not yet enabled for the application itself.
Diffstat (limited to 'src/irc.cpp')
-rw-r--r-- | src/irc.cpp | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/src/irc.cpp b/src/irc.cpp index fe96a90a1c..8805c7fd5b 100644 --- a/src/irc.cpp +++ b/src/irc.cpp @@ -22,22 +22,25 @@ void ThreadIRCSeed2(void* parg); #pragma pack(push, 1) struct ircaddr { - int ip; + struct in_addr ip; short port; }; #pragma pack(pop) -string EncodeAddress(const CAddress& addr) +string EncodeAddress(const CService& addr) { struct ircaddr tmp; - tmp.ip = addr.ip; - tmp.port = addr.port; + if (addr.GetInAddr(&tmp.ip)) + { + tmp.port = htons(addr.GetPort()); - vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp)); - return string("u") + EncodeBase58Check(vch); + vector<unsigned char> vch(UBEGIN(tmp), UEND(tmp)); + return string("u") + EncodeBase58Check(vch); + } + return ""; } -bool DecodeAddress(string str, CAddress& addr) +bool DecodeAddress(string str, CService& addr) { vector<unsigned char> vch; if (!DecodeBase58Check(str.substr(1), vch)) @@ -48,7 +51,7 @@ bool DecodeAddress(string str, CAddress& addr) return false; memcpy(&tmp, &vch[0], sizeof(tmp)); - addr = CAddress(tmp.ip, ntohs(tmp.port), NODE_NETWORK); + addr = CService(tmp.ip, ntohs(tmp.port)); return true; } @@ -204,7 +207,7 @@ bool RecvCodeLine(SOCKET hSocket, const char* psz1, string& strRet) } } -bool GetIPFromIRC(SOCKET hSocket, string strMyName, unsigned int& ipRet) +bool GetIPFromIRC(SOCKET hSocket, string strMyName, CNetAddr& ipRet) { Send(hSocket, strprintf("USERHOST %s\r", strMyName.c_str()).c_str()); @@ -227,10 +230,10 @@ bool GetIPFromIRC(SOCKET hSocket, string strMyName, unsigned int& ipRet) printf("GetIPFromIRC() got userhost %s\n", strHost.c_str()); if (fUseProxy) return false; - CAddress addr(strHost, 0, true); + CNetAddr addr(strHost, true); if (!addr.IsValid()) return false; - ipRet = addr.ip; + ipRet = addr; return true; } @@ -267,9 +270,9 @@ void ThreadIRCSeed2(void* parg) while (!fShutdown) { - CAddress addrConnect("92.243.23.21", 6667); // irc.lfnet.org + CService addrConnect("92.243.23.21", 6667); // irc.lfnet.org - CAddress addrIRC("irc.lfnet.org", 6667, true); + CService addrIRC("irc.lfnet.org", 6667, true); if (addrIRC.IsValid()) addrConnect = addrIRC; @@ -325,15 +328,15 @@ void ThreadIRCSeed2(void* parg) Sleep(500); // Get our external IP from the IRC server and re-nick before joining the channel - CAddress addrFromIRC; - if (GetIPFromIRC(hSocket, strMyName, addrFromIRC.ip)) + CNetAddr addrFromIRC; + if (GetIPFromIRC(hSocket, strMyName, addrFromIRC)) { - printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToStringIP().c_str()); + printf("GetIPFromIRC() returned %s\n", addrFromIRC.ToString().c_str()); if (!fUseProxy && addrFromIRC.IsRoutable()) { // IRC lets you to re-nick fGotExternalIP = true; - addrLocalHost.ip = addrFromIRC.ip; + addrLocalHost.SetIP(addrFromIRC); strMyName = EncodeAddress(addrLocalHost); Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str()); } |