15
15
static const unsigned char pchIPv4[12 ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0xff , 0xff };
16
16
static const unsigned char pchOnionCat[] = {0xFD ,0x87 ,0xD8 ,0x7E ,0xEB ,0x43 };
17
17
18
+ // 0xFD + sha256("bitcoin")[0:5]
19
+ static const unsigned char g_internal_prefix[] = { 0xFD , 0x6B , 0x88 , 0xC0 , 0x87 , 0x24 };
20
+
18
21
void CNetAddr::Init ()
19
22
{
20
23
memset (ip, 0 , sizeof (ip));
@@ -42,6 +45,18 @@ void CNetAddr::SetRaw(Network network, const uint8_t *ip_in)
42
45
}
43
46
}
44
47
48
+ bool CNetAddr::SetInternal (const std::string &name)
49
+ {
50
+ if (name.empty ()) {
51
+ return false ;
52
+ }
53
+ unsigned char hash[32 ] = {};
54
+ CSHA256 ().Write ((const unsigned char *)name.data (), name.size ()).Finalize (hash);
55
+ memcpy (ip, g_internal_prefix, sizeof (g_internal_prefix));
56
+ memcpy (ip + sizeof (g_internal_prefix), hash, sizeof (ip) - sizeof (g_internal_prefix));
57
+ return true ;
58
+ }
59
+
45
60
bool CNetAddr::SetSpecial (const std::string &strName)
46
61
{
47
62
if (strName.size ()>6 && strName.substr (strName.size () - 6 , 6 ) == " .onion" ) {
@@ -84,7 +99,7 @@ bool CNetAddr::IsIPv4() const
84
99
85
100
bool CNetAddr::IsIPv6 () const
86
101
{
87
- return (!IsIPv4 () && !IsTor ());
102
+ return (!IsIPv4 () && !IsTor () && ! IsInternal () );
88
103
}
89
104
90
105
bool CNetAddr::IsRFC1918 () const
@@ -199,6 +214,9 @@ bool CNetAddr::IsValid() const
199
214
if (IsRFC3849 ())
200
215
return false ;
201
216
217
+ if (IsInternal ())
218
+ return false ;
219
+
202
220
if (IsIPv4 ())
203
221
{
204
222
// INADDR_NONE
@@ -217,11 +235,19 @@ bool CNetAddr::IsValid() const
217
235
218
236
bool CNetAddr::IsRoutable () const
219
237
{
220
- return IsValid () && !(IsRFC1918 () || IsRFC2544 () || IsRFC3927 () || IsRFC4862 () || IsRFC6598 () || IsRFC5737 () || (IsRFC4193 () && !IsTor ()) || IsRFC4843 () || IsLocal ());
238
+ return IsValid () && !(IsRFC1918 () || IsRFC2544 () || IsRFC3927 () || IsRFC4862 () || IsRFC6598 () || IsRFC5737 () || (IsRFC4193 () && !IsTor ()) || IsRFC4843 () || IsLocal () || IsInternal ());
239
+ }
240
+
241
+ bool CNetAddr::IsInternal () const
242
+ {
243
+ return memcmp (ip, g_internal_prefix, sizeof (g_internal_prefix)) == 0 ;
221
244
}
222
245
223
246
enum Network CNetAddr::GetNetwork () const
224
247
{
248
+ if (IsInternal ())
249
+ return NET_INTERNAL;
250
+
225
251
if (!IsRoutable ())
226
252
return NET_UNROUTABLE;
227
253
@@ -238,6 +264,8 @@ std::string CNetAddr::ToStringIP() const
238
264
{
239
265
if (IsTor ())
240
266
return EncodeBase32 (&ip[6 ], 10 ) + " .onion" ;
267
+ if (IsInternal ())
268
+ return EncodeBase32 (ip + sizeof (g_internal_prefix), sizeof (ip) - sizeof (g_internal_prefix)) + " .internal" ;
241
269
CService serv (*this , 0 );
242
270
struct sockaddr_storage sockaddr;
243
271
socklen_t socklen = sizeof (sockaddr);
@@ -305,9 +333,15 @@ std::vector<unsigned char> CNetAddr::GetGroup() const
305
333
nClass = 255 ;
306
334
nBits = 0 ;
307
335
}
308
-
309
- // all unroutable addresses belong to the same group
310
- if (!IsRoutable ())
336
+ // all internal-usage addresses get their own group
337
+ if (IsInternal ())
338
+ {
339
+ nClass = NET_INTERNAL;
340
+ nStartByte = sizeof (g_internal_prefix);
341
+ nBits = (sizeof (ip) - sizeof (g_internal_prefix)) * 8 ;
342
+ }
343
+ // all other unroutable addresses belong to the same group
344
+ else if (!IsRoutable ())
311
345
{
312
346
nClass = NET_UNROUTABLE;
313
347
nBits = 0 ;
@@ -393,7 +427,7 @@ int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
393
427
REACH_PRIVATE
394
428
};
395
429
396
- if (!IsRoutable ())
430
+ if (!IsRoutable () || IsInternal () )
397
431
return REACH_UNREACHABLE;
398
432
399
433
int ourNet = GetExtNetwork (this );
@@ -552,7 +586,7 @@ std::string CService::ToStringPort() const
552
586
553
587
std::string CService::ToStringIPPort () const
554
588
{
555
- if (IsIPv4 () || IsTor ()) {
589
+ if (IsIPv4 () || IsTor () || IsInternal () ) {
556
590
return ToStringIP () + " :" + ToStringPort ();
557
591
} else {
558
592
return " [" + ToStringIP () + " ]:" + ToStringPort ();
0 commit comments