Skip to content

Commit

Permalink
Network stack refactor
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sipa committed Jan 6, 2012
1 parent 7486c64 commit 67a42f9
Show file tree
Hide file tree
Showing 21 changed files with 1,041 additions and 546 deletions.
2 changes: 2 additions & 0 deletions bitcoin-qt.pro
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ HEADERS += src/qt/bitcoingui.h \
src/base58.h \
src/bignum.h \
src/checkpoints.h \
src/compat.h \
src/util.h \
src/uint256.h \
src/serialize.h \
Expand Down Expand Up @@ -156,6 +157,7 @@ SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \
src/qt/editaddressdialog.cpp \
src/qt/bitcoinaddressvalidator.cpp \
src/util.cpp \
src/netbase.cpp \
src/key.cpp \
src/script.cpp \
src/main.cpp \
Expand Down
42 changes: 42 additions & 0 deletions src/compat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2011 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
#ifndef _BITCOIN_COMPAT_H
#define _BITCOIN_COMPAT_H 1

typedef u_int SOCKET;
#ifdef WIN32
#define MSG_NOSIGNAL 0
#define MSG_DONTWAIT 0
typedef int socklen_t;
#else
#include "errno.h"
#define WSAGetLastError() errno
#define WSAEINVAL EINVAL
#define WSAEALREADY EALREADY
#define WSAEWOULDBLOCK EWOULDBLOCK
#define WSAEMSGSIZE EMSGSIZE
#define WSAEINTR EINTR
#define WSAEINPROGRESS EINPROGRESS
#define WSAEADDRINUSE EADDRINUSE
#define WSAENOTSOCK EBADF
#define INVALID_SOCKET (SOCKET)(~0)
#define SOCKET_ERROR -1
#endif

inline int myclosesocket(SOCKET& hSocket)
{
if (hSocket == INVALID_SOCKET)
return WSAENOTSOCK;
#ifdef WIN32
int ret = closesocket(hSocket);
#else
int ret = close(hSocket);
#endif
hSocket = INVALID_SOCKET;
return ret;
}
#define closesocket(s) myclosesocket(s)

#endif
2 changes: 1 addition & 1 deletion src/headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#ifdef _WIN32_WINNT
#undef _WIN32_WINNT
#endif
#define _WIN32_WINNT 0x0500
#define _WIN32_WINNT 0x0501
#ifdef _WIN32_IE
#undef _WIN32_IE
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ bool AppInit2(int argc, char* argv[])
}
}

bool fTor = (fUseProxy && addrProxy.port == htons(9050));
bool fTor = (fUseProxy && addrProxy.GetPort() == 9050);
if (fTor)
{
// Use SoftSetArg here so user can override any of these if they wish.
Expand Down
37 changes: 20 additions & 17 deletions src/irc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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;
}

Expand Down Expand Up @@ -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());

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
Expand Down
13 changes: 7 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1931,7 +1931,7 @@ unsigned char pchMessageStart[4] = { 0xf9, 0xbe, 0xb4, 0xd9 };

bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
{
static map<unsigned int, vector<unsigned char> > mapReuseKey;
static map<CService, vector<unsigned char> > mapReuseKey;
RandAddSeedPerfmon();
if (fDebug) {
printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str());
Expand Down Expand Up @@ -1987,7 +1987,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)

pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);

AddTimeData(pfrom->addr.ip, nTime);
AddTimeData(pfrom->addr, nTime);

// Change version
if (pfrom->nVersion >= 209)
Expand Down Expand Up @@ -2093,7 +2093,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
static uint256 hashSalt;
if (hashSalt == 0)
RAND_bytes((unsigned char*)&hashSalt, sizeof(hashSalt));
uint256 hashRand = hashSalt ^ (((int64)addr.ip)<<32) ^ ((GetTime()+addr.ip)/(24*60*60));
int64 hashAddr = addr.GetHash();
uint256 hashRand = hashSalt ^ (hashAddr<<32) ^ ((GetTime()+hashAddr)/(24*60*60));
hashRand = Hash(BEGIN(hashRand), END(hashRand));
multimap<uint256, CNode*> mapMix;
BOOST_FOREACH(CNode* pnode, vNodes)
Expand Down Expand Up @@ -2392,12 +2393,12 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
/// we have a chance to check the order here

// Keep giving the same key to the same ip until they use it
if (!mapReuseKey.count(pfrom->addr.ip))
pwalletMain->GetKeyFromPool(mapReuseKey[pfrom->addr.ip], true);
if (!mapReuseKey.count(pfrom->addr))
pwalletMain->GetKeyFromPool(mapReuseKey[pfrom->addr], true);

// Send back approval of order and pubkey to use
CScript scriptPubKey;
scriptPubKey << mapReuseKey[pfrom->addr.ip] << OP_CHECKSIG;
scriptPubKey << mapReuseKey[pfrom->addr] << OP_CHECKSIG;
pfrom->PushMessage("reply", hashReply, (int)0, scriptPubKey);
}

Expand Down
2 changes: 2 additions & 0 deletions src/makefile.linux-mingw
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ HEADERS = \
base58.h \
bignum.h \
checkpoints.h \
compat.h \
crypter.h \
db.h \
headers.h \
Expand Down Expand Up @@ -63,6 +64,7 @@ LIBS += -l mingwthrd -l kernel32 -l user32 -l gdi32 -l comdlg32 -l winspool -l w

OBJS= \
obj/checkpoints.o \
obj/netbase.o \
obj/crypter.o \
obj/key.o \
obj/db.o \
Expand Down
2 changes: 2 additions & 0 deletions src/makefile.mingw
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ HEADERS = \
base58.h \
bignum.h \
checkpoints.h \
compat.h \
crypter.h \
db.h \
headers.h \
Expand Down Expand Up @@ -60,6 +61,7 @@ LIBS += -l kernel32 -l user32 -l gdi32 -l comdlg32 -l winspool -l winmm -l shell

OBJS= \
obj/checkpoints.o \
obj/netbase.o \
obj/crypter.o \
obj/key.o \
obj/db.o \
Expand Down
2 changes: 2 additions & 0 deletions src/makefile.osx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ HEADERS = \
base58.h \
bignum.h \
checkpoints.h \
compat.h \
crypter.h \
db.h \
headers.h \
Expand All @@ -72,6 +73,7 @@ HEADERS = \

OBJS= \
obj/checkpoints.o \
obj/netbase.o \
obj/crypter.o \
obj/key.o \
obj/db.o \
Expand Down
2 changes: 2 additions & 0 deletions src/makefile.unix
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ HEADERS = \
base58.h \
bignum.h \
checkpoints.h \
compat.h \
crypter.h \
db.h \
headers.h \
Expand All @@ -109,6 +110,7 @@ HEADERS = \

OBJS= \
obj/checkpoints.o \
obj/netbase.o \
obj/crypter.o \
obj/key.o \
obj/db.o \
Expand Down
Loading

0 comments on commit 67a42f9

Please sign in to comment.