From 4ac57f013e20da2d0f87fff69625cbd4419089f3 Mon Sep 17 00:00:00 2001 From: s_nakamoto Date: Sun, 1 Nov 2009 01:16:51 +0000 Subject: move debug.log and db.log to data dir, portable GetDataDir, optimize GetBalance, fix repaint bogdown, -addnode and -? switches git-svn-id: https://bitcoin.svn.sourceforge.net/svnroot/bitcoin/trunk@25 1a98c847-1fd6-4fd8-948a-caf3550aa51b --- net.h | 78 ++++++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 47 insertions(+), 31 deletions(-) (limited to 'net.h') diff --git a/net.h b/net.h index 2eece2c061..7716426f04 100644 --- a/net.h +++ b/net.h @@ -1,12 +1,6 @@ // Copyright (c) 2009 Satoshi Nakamoto // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. - -#ifndef __WXMSW__ -#define closesocket(s) close(s) -#define INVALID_SOCKET (SOCKET)(~0) -typedef u_int SOCKET; -#endif class CMessageHeader; class CAddress; @@ -148,61 +142,73 @@ public: CAddress() { - nServices = 0; - memcpy(pchReserved, pchIPv4, sizeof(pchReserved)); - ip = 0; - port = DEFAULT_PORT; - nTime = GetAdjustedTime(); - nLastFailed = 0; + Init(); } - CAddress(unsigned int ipIn, unsigned short portIn=DEFAULT_PORT, uint64 nServicesIn=0) + CAddress(unsigned int ipIn, unsigned short portIn=DEFAULT_PORT, uint64 nServicesIn=NODE_NETWORK) { - nServices = nServicesIn; - memcpy(pchReserved, pchIPv4, sizeof(pchReserved)); + Init(); ip = ipIn; port = portIn; - nTime = GetAdjustedTime(); - nLastFailed = 0; + nServices = nServicesIn; } - explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=0) + explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK) { - nServices = nServicesIn; - memcpy(pchReserved, pchIPv4, sizeof(pchReserved)); + Init(); ip = sockaddr.sin_addr.s_addr; port = sockaddr.sin_port; - nTime = GetAdjustedTime(); - nLastFailed = 0; + nServices = nServicesIn; } - explicit CAddress(const char* pszIn, uint64 nServicesIn=0) + explicit CAddress(const char* pszIn, uint64 nServicesIn=NODE_NETWORK) { + Init(); + SetAddress(pszIn); nServices = nServicesIn; + } + + explicit CAddress(string strIn, uint64 nServicesIn=NODE_NETWORK) + { + Init(); + SetAddress(strIn.c_str()); + nServices = nServicesIn; + } + + void Init() + { + nServices = NODE_NETWORK; memcpy(pchReserved, pchIPv4, sizeof(pchReserved)); ip = INADDR_NONE; port = DEFAULT_PORT; nTime = GetAdjustedTime(); nLastFailed = 0; + } + bool SetAddress(const char* pszIn) + { + ip = INADDR_NONE; + port = DEFAULT_PORT; char psz[100]; - if (strlen(pszIn) > ARRAYLEN(psz)-1) - return; - strcpy(psz, pszIn); + strlcpy(psz, pszIn, sizeof(psz)); unsigned int a=0, b=0, c=0, d=0, e=0; if (sscanf(psz, "%u.%u.%u.%u:%u", &a, &b, &c, &d, &e) < 4) - return; + return false; char* pszPort = strchr(psz, ':'); if (pszPort) { *pszPort++ = '\0'; port = htons(atoi(pszPort)); - if (atoi(pszPort) > USHRT_MAX) + if (atoi(pszPort) < 0 || atoi(pszPort) > USHRT_MAX) port = htons(USHRT_MAX); - if (atoi(pszPort) < 0) - port = htons(0); } ip = inet_addr(psz); + return IsValid(); + } + + bool SetAddress(string strIn) + { + return SetAddress(strIn.c_str()); } IMPLEMENT_SERIALIZE @@ -274,7 +280,17 @@ public: bool IsRoutable() const { - return !(GetByte(3) == 10 || (GetByte(3) == 192 && GetByte(2) == 168) || GetByte(3) == 127 || GetByte(3) == 0); + return !(GetByte(3) == 10 || + (GetByte(3) == 192 && GetByte(2) == 168) || + GetByte(3) == 127 || + GetByte(3) == 0 || + ip == 0 || + ip == INADDR_NONE); + } + + bool IsValid() const + { + return (ip != 0 && ip != INADDR_NONE && port != htons(USHRT_MAX)); } unsigned char GetByte(int n) const -- cgit v1.2.3