experimental-genesis 1
experimental-genesis 2
experimental-genesis 3
experimental-genesis 4
experimental-genesis 5
experimental-genesis 6
experimental-genesis 7
experimental-genesis 8
experimental-genesis 9
experimental-genesis 10
experimental-genesis 11
experimental-genesis 12
experimental-genesis 13
experimental-genesis 14
experimental-genesis 15
experimental-genesis 16
experimental-genesis 17
experimental-genesis 18
experimental-genesis 19
experimental-genesis 20
experimental-genesis 21
experimental-genesis 22
experimental-genesis 23
experimental-genesis 24
experimental-genesis 25
experimental-genesis 26
experimental-genesis 27
experimental-genesis 28
experimental-genesis 29
experimental-genesis 30
experimental-genesis 31
experimental-genesis 32
experimental-genesis 33
experimental-genesis 34
experimental-genesis 35
experimental-genesis 36
experimental-genesis 37 #include "protocol.h"
experimental-genesis 38 #include "util.h"
experimental-genesis 39
experimental-genesis 40 #ifndef WIN32
experimental-genesis 41 # include <arpa/inet.h>
experimental-genesis 42 #endif
experimental-genesis 43
experimental-genesis 44
experimental-genesis 45 bool Lookup(const char *pszName, std::vector<CAddress>& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
experimental-genesis 46 bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
experimental-genesis 47
experimental-genesis 48 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
experimental-genesis 49 static const char* ppszTypeName[] =
experimental-genesis 50 {
experimental-genesis 51 "ERROR",
experimental-genesis 52 "tx",
experimental-genesis 53 "block",
experimental-genesis 54 };
experimental-genesis 55
experimental-genesis 56 CMessageHeader::CMessageHeader()
experimental-genesis 57 {
experimental-genesis 58 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
experimental-genesis 59 memset(pchCommand, 0, sizeof(pchCommand));
experimental-genesis 60 pchCommand[1] = 1;
experimental-genesis 61 nMessageSize = -1;
experimental-genesis 62 nChecksum = 0;
experimental-genesis 63 }
experimental-genesis 64
experimental-genesis 65 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
experimental-genesis 66 {
experimental-genesis 67 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
experimental-genesis 68 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
experimental-genesis 69 nMessageSize = nMessageSizeIn;
experimental-genesis 70 nChecksum = 0;
experimental-genesis 71 }
experimental-genesis 72
experimental-genesis 73 std::string CMessageHeader::GetCommand() const
experimental-genesis 74 {
experimental-genesis 75 if (pchCommand[COMMAND_SIZE-1] == 0)
experimental-genesis 76 return std::string(pchCommand, pchCommand + strlen(pchCommand));
experimental-genesis 77 else
experimental-genesis 78 return std::string(pchCommand, pchCommand + COMMAND_SIZE);
experimental-genesis 79 }
experimental-genesis 80
experimental-genesis 81 bool CMessageHeader::IsValid() const
experimental-genesis 82 {
experimental-genesis 83
experimental-genesis 84 if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
experimental-genesis 85 return false;
experimental-genesis 86
experimental-genesis 87
experimental-genesis 88 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
experimental-genesis 89 {
experimental-genesis 90 if (*p1 == 0)
experimental-genesis 91 {
experimental-genesis 92
experimental-genesis 93 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
experimental-genesis 94 if (*p1 != 0)
experimental-genesis 95 return false;
experimental-genesis 96 }
experimental-genesis 97 else if (*p1 < ' ' || *p1 > 0x7E)
experimental-genesis 98 return false;
experimental-genesis 99 }
experimental-genesis 100
experimental-genesis 101
experimental-genesis 102 if (nMessageSize > MAX_SIZE)
experimental-genesis 103 {
experimental-genesis 104 printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
experimental-genesis 105 return false;
experimental-genesis 106 }
experimental-genesis 107
experimental-genesis 108 return true;
experimental-genesis 109 }
experimental-genesis 110
experimental-genesis 111 CAddress::CAddress()
experimental-genesis 112 {
experimental-genesis 113 Init();
experimental-genesis 114 }
experimental-genesis 115
experimental-genesis 116 CAddress::CAddress(unsigned int ipIn, unsigned short portIn, uint64 nServicesIn)
experimental-genesis 117 {
experimental-genesis 118 Init();
experimental-genesis 119 ip = ipIn;
experimental-genesis 120 port = htons(portIn == 0 ? GetDefaultPort() : portIn);
experimental-genesis 121 nServices = nServicesIn;
experimental-genesis 122 }
experimental-genesis 123
experimental-genesis 124 CAddress::CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn)
experimental-genesis 125 {
experimental-genesis 126 Init();
experimental-genesis 127 ip = sockaddr.sin_addr.s_addr;
experimental-genesis 128 port = sockaddr.sin_port;
experimental-genesis 129 nServices = nServicesIn;
experimental-genesis 130 }
experimental-genesis 131
experimental-genesis 132 CAddress::CAddress(const char* pszIn, int portIn, bool fNameLookup, uint64 nServicesIn)
experimental-genesis 133 {
experimental-genesis 134 Init();
experimental-genesis 135 Lookup(pszIn, *this, nServicesIn, fNameLookup, portIn);
experimental-genesis 136 }
experimental-genesis 137
experimental-genesis 138 CAddress::CAddress(const char* pszIn, bool fNameLookup, uint64 nServicesIn)
experimental-genesis 139 {
experimental-genesis 140 Init();
experimental-genesis 141 Lookup(pszIn, *this, nServicesIn, fNameLookup, 0, true);
experimental-genesis 142 }
experimental-genesis 143
experimental-genesis 144 CAddress::CAddress(std::string strIn, int portIn, bool fNameLookup, uint64 nServicesIn)
experimental-genesis 145 {
experimental-genesis 146 Init();
experimental-genesis 147 Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, portIn);
experimental-genesis 148 }
experimental-genesis 149
experimental-genesis 150 CAddress::CAddress(std::string strIn, bool fNameLookup, uint64 nServicesIn)
experimental-genesis 151 {
experimental-genesis 152 Init();
experimental-genesis 153 Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, 0, true);
experimental-genesis 154 }
experimental-genesis 155
experimental-genesis 156 void CAddress::Init()
experimental-genesis 157 {
experimental-genesis 158 nServices = NODE_NETWORK;
experimental-genesis 159 memcpy(pchReserved, pchIPv4, sizeof(pchReserved));
experimental-genesis 160 ip = INADDR_NONE;
experimental-genesis 161 port = htons(GetDefaultPort());
experimental-genesis 162 nTime = 100000000;
experimental-genesis 163 nLastTry = 0;
experimental-genesis 164 }
experimental-genesis 165
experimental-genesis 166 bool operator==(const CAddress& a, const CAddress& b)
experimental-genesis 167 {
experimental-genesis 168 return (memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)) == 0 &&
experimental-genesis 169 a.ip == b.ip &&
experimental-genesis 170 a.port == b.port);
experimental-genesis 171 }
experimental-genesis 172
experimental-genesis 173 bool operator!=(const CAddress& a, const CAddress& b)
experimental-genesis 174 {
experimental-genesis 175 return (!(a == b));
experimental-genesis 176 }
experimental-genesis 177
experimental-genesis 178 bool operator<(const CAddress& a, const CAddress& b)
experimental-genesis 179 {
experimental-genesis 180 int ret = memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved));
experimental-genesis 181 if (ret < 0)
experimental-genesis 182 return true;
experimental-genesis 183 else if (ret == 0)
experimental-genesis 184 {
experimental-genesis 185 if (ntohl(a.ip) < ntohl(b.ip))
experimental-genesis 186 return true;
experimental-genesis 187 else if (a.ip == b.ip)
experimental-genesis 188 return ntohs(a.port) < ntohs(b.port);
experimental-genesis 189 }
experimental-genesis 190 return false;
experimental-genesis 191 }
experimental-genesis 192
experimental-genesis 193 std::vector<unsigned char> CAddress::GetKey() const
experimental-genesis 194 {
experimental-genesis 195 CDataStream ss;
experimental-genesis 196 ss.reserve(18);
experimental-genesis 197 ss << FLATDATA(pchReserved) << ip << port;
experimental-genesis 198
experimental-genesis 199 #if defined(_MSC_VER) && _MSC_VER < 1300
experimental-genesis 200 return std::vector<unsigned char>((unsigned char*)&ss.begin()[0], (unsigned char*)&ss.end()[0]);
experimental-genesis 201 #else
experimental-genesis 202 return std::vector<unsigned char>(ss.begin(), ss.end());
experimental-genesis 203 #endif
experimental-genesis 204 }
experimental-genesis 205
experimental-genesis 206 struct sockaddr_in CAddress::GetSockAddr() const
experimental-genesis 207 {
experimental-genesis 208 struct sockaddr_in sockaddr;
experimental-genesis 209 memset(&sockaddr, 0, sizeof(sockaddr));
experimental-genesis 210 sockaddr.sin_family = AF_INET;
experimental-genesis 211 sockaddr.sin_addr.s_addr = ip;
experimental-genesis 212 sockaddr.sin_port = port;
experimental-genesis 213 return sockaddr;
experimental-genesis 214 }
experimental-genesis 215
experimental-genesis 216 bool CAddress::IsIPv4() const
experimental-genesis 217 {
experimental-genesis 218 return (memcmp(pchReserved, pchIPv4, sizeof(pchIPv4)) == 0);
experimental-genesis 219 }
experimental-genesis 220
experimental-genesis 221 bool CAddress::IsRFC1918() const
experimental-genesis 222 {
experimental-genesis 223 return IsIPv4() && (GetByte(3) == 10 ||
experimental-genesis 224 (GetByte(3) == 192 && GetByte(2) == 168) ||
experimental-genesis 225 (GetByte(3) == 172 &&
experimental-genesis 226 (GetByte(2) >= 16 && GetByte(2) <= 31)));
experimental-genesis 227 }
experimental-genesis 228
experimental-genesis 229 bool CAddress::IsRFC3927() const
experimental-genesis 230 {
experimental-genesis 231 return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
experimental-genesis 232 }
experimental-genesis 233
experimental-genesis 234 bool CAddress::IsLocal() const
experimental-genesis 235 {
experimental-genesis 236 return IsIPv4() && (GetByte(3) == 127 ||
experimental-genesis 237 GetByte(3) == 0);
experimental-genesis 238 }
experimental-genesis 239
experimental-genesis 240 bool CAddress::IsRoutable() const
experimental-genesis 241 {
experimental-genesis 242 return IsValid() &&
experimental-genesis 243 !(IsRFC1918() || IsRFC3927() || IsLocal());
experimental-genesis 244 }
experimental-genesis 245
experimental-genesis 246 bool CAddress::IsValid() const
experimental-genesis 247 {
experimental-genesis 248
experimental-genesis 249
experimental-genesis 250
experimental-genesis 251
experimental-genesis 252
experimental-genesis 253
experimental-genesis 254 if (memcmp(pchReserved, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
experimental-genesis 255 return false;
experimental-genesis 256
experimental-genesis 257 return (ip != 0 && ip != INADDR_NONE && port != htons(USHRT_MAX));
experimental-genesis 258 }
experimental-genesis 259
experimental-genesis 260 unsigned char CAddress::GetByte(int n) const
experimental-genesis 261 {
experimental-genesis 262 return ((unsigned char*)&ip)[3-n];
experimental-genesis 263 }
experimental-genesis 264
experimental-genesis 265 std::string CAddress::ToStringIPPort() const
experimental-genesis 266 {
experimental-genesis 267 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
experimental-genesis 268 }
experimental-genesis 269
experimental-genesis 270 std::string CAddress::ToStringIP() const
experimental-genesis 271 {
experimental-genesis 272 return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
experimental-genesis 273 }
experimental-genesis 274
experimental-genesis 275 std::string CAddress::ToStringPort() const
experimental-genesis 276 {
experimental-genesis 277 return strprintf("%u", ntohs(port));
experimental-genesis 278 }
experimental-genesis 279
experimental-genesis 280 std::string CAddress::ToString() const
experimental-genesis 281 {
experimental-genesis 282 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
experimental-genesis 283 }
experimental-genesis 284
experimental-genesis 285 void CAddress::print() const
experimental-genesis 286 {
experimental-genesis 287 printf("CAddress(%s)\n", ToString().c_str());
experimental-genesis 288 }
experimental-genesis 289
experimental-genesis 290 CInv::CInv()
experimental-genesis 291 {
experimental-genesis 292 type = 0;
experimental-genesis 293 hash = 0;
experimental-genesis 294 }
experimental-genesis 295
experimental-genesis 296 CInv::CInv(int typeIn, const uint256& hashIn)
experimental-genesis 297 {
experimental-genesis 298 type = typeIn;
experimental-genesis 299 hash = hashIn;
experimental-genesis 300 }
experimental-genesis 301
experimental-genesis 302 CInv::CInv(const std::string& strType, const uint256& hashIn)
experimental-genesis 303 {
experimental-genesis 304 int i;
experimental-genesis 305 for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
experimental-genesis 306 {
experimental-genesis 307 if (strType == ppszTypeName[i])
experimental-genesis 308 {
experimental-genesis 309 type = i;
experimental-genesis 310 break;
experimental-genesis 311 }
experimental-genesis 312 }
experimental-genesis 313 if (i == ARRAYLEN(ppszTypeName))
experimental-genesis 314 throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
experimental-genesis 315 hash = hashIn;
experimental-genesis 316 }
experimental-genesis 317
experimental-genesis 318 bool operator<(const CInv& a, const CInv& b)
experimental-genesis 319 {
experimental-genesis 320 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
experimental-genesis 321 }
experimental-genesis 322
experimental-genesis 323 bool CInv::IsKnownType() const
experimental-genesis 324 {
experimental-genesis 325 return (type >= 1 && type < ARRAYLEN(ppszTypeName));
experimental-genesis 326 }
experimental-genesis 327
experimental-genesis 328 const char* CInv::GetCommand() const
experimental-genesis 329 {
experimental-genesis 330 if (!IsKnownType())
experimental-genesis 331 throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
experimental-genesis 332 return ppszTypeName[type];
experimental-genesis 333 }
experimental-genesis 334
experimental-genesis 335 std::string CInv::ToString() const
experimental-genesis 336 {
experimental-genesis 337 return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
experimental-genesis 338 }
experimental-genesis 339
experimental-genesis 340 void CInv::print() const
experimental-genesis 341 {
experimental-genesis 342 printf("CInv(%s)\n", ToString().c_str());
experimental-genesis 343 }