@@ -28,19 +28,35 @@ CNetAddr::CNetAddr()
28
28
29
29
void CNetAddr::SetIP (const CNetAddr& ipIn)
30
30
{
31
+ m_net = ipIn.m_net ;
31
32
memcpy (ip, ipIn.ip , sizeof (ip));
32
33
}
33
34
35
+ void CNetAddr::SetLegacyIPv6 (const uint8_t ipv6[16 ])
36
+ {
37
+ if (memcmp (ipv6, pchIPv4, sizeof (pchIPv4)) == 0 ) {
38
+ m_net = NET_IPV4;
39
+ } else if (memcmp (ipv6, pchOnionCat, sizeof (pchOnionCat)) == 0 ) {
40
+ m_net = NET_ONION;
41
+ } else if (memcmp (ipv6, g_internal_prefix, sizeof (g_internal_prefix)) == 0 ) {
42
+ m_net = NET_INTERNAL;
43
+ } else {
44
+ m_net = NET_IPV6;
45
+ }
46
+ memcpy (ip, ipv6, 16 );
47
+ }
48
+
34
49
void CNetAddr::SetRaw (Network network, const uint8_t *ip_in)
35
50
{
36
51
switch (network)
37
52
{
38
53
case NET_IPV4:
54
+ m_net = NET_IPV4;
39
55
memcpy (ip, pchIPv4, 12 );
40
56
memcpy (ip+12 , ip_in, 4 );
41
57
break ;
42
58
case NET_IPV6:
43
- memcpy (ip, ip_in, 16 );
59
+ SetLegacyIPv6 ( ip_in);
44
60
break ;
45
61
default :
46
62
assert (!" invalid network" );
@@ -66,6 +82,7 @@ bool CNetAddr::SetInternal(const std::string &name)
66
82
if (name.empty ()) {
67
83
return false ;
68
84
}
85
+ m_net = NET_INTERNAL;
69
86
unsigned char hash[32 ] = {};
70
87
CSHA256 ().Write ((const unsigned char *)name.data (), name.size ()).Finalize (hash);
71
88
memcpy (ip, g_internal_prefix, sizeof (g_internal_prefix));
@@ -89,6 +106,7 @@ bool CNetAddr::SetSpecial(const std::string &strName)
89
106
std::vector<unsigned char > vchAddr = DecodeBase32 (strName.substr (0 , strName.size () - 6 ).c_str ());
90
107
if (vchAddr.size () != 16 -sizeof (pchOnionCat))
91
108
return false ;
109
+ m_net = NET_ONION;
92
110
memcpy (ip, pchOnionCat, sizeof (pchOnionCat));
93
111
for (unsigned int i=0 ; i<16 -sizeof (pchOnionCat); i++)
94
112
ip[i + sizeof (pchOnionCat)] = vchAddr[i];
@@ -123,15 +141,9 @@ bool CNetAddr::IsBindAny() const
123
141
return true ;
124
142
}
125
143
126
- bool CNetAddr::IsIPv4 () const
127
- {
128
- return (memcmp (ip, pchIPv4, sizeof (pchIPv4)) == 0 );
129
- }
144
+ bool CNetAddr::IsIPv4 () const { return m_net == NET_IPV4; }
130
145
131
- bool CNetAddr::IsIPv6 () const
132
- {
133
- return (!IsIPv4 () && !IsTor () && !IsInternal ());
134
- }
146
+ bool CNetAddr::IsIPv6 () const { return m_net == NET_IPV6; }
135
147
136
148
bool CNetAddr::IsRFC1918 () const
137
149
{
@@ -165,50 +177,54 @@ bool CNetAddr::IsRFC5737() const
165
177
166
178
bool CNetAddr::IsRFC3849 () const
167
179
{
168
- return GetByte (15 ) == 0x20 && GetByte (14 ) == 0x01 && GetByte (13 ) == 0x0D && GetByte (12 ) == 0xB8 ;
180
+ return IsIPv6 () && GetByte (15 ) == 0x20 && GetByte (14 ) == 0x01 &&
181
+ GetByte (13 ) == 0x0D && GetByte (12 ) == 0xB8 ;
169
182
}
170
183
171
184
bool CNetAddr::IsRFC3964 () const
172
185
{
173
- return ( GetByte (15 ) == 0x20 && GetByte (14 ) == 0x02 ) ;
186
+ return IsIPv6 () && GetByte (15 ) == 0x20 && GetByte (14 ) == 0x02 ;
174
187
}
175
188
176
189
bool CNetAddr::IsRFC6052 () const
177
190
{
178
191
static const unsigned char pchRFC6052[] = {0 ,0x64 ,0xFF ,0x9B ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 };
179
- return ( memcmp (ip, pchRFC6052, sizeof (pchRFC6052)) == 0 ) ;
192
+ return IsIPv6 () && memcmp (ip, pchRFC6052, sizeof (pchRFC6052)) == 0 ;
180
193
}
181
194
182
195
bool CNetAddr::IsRFC4380 () const
183
196
{
184
- return (GetByte (15 ) == 0x20 && GetByte (14 ) == 0x01 && GetByte (13 ) == 0 && GetByte (12 ) == 0 );
197
+ return IsIPv6 () && GetByte (15 ) == 0x20 && GetByte (14 ) == 0x01 && GetByte (13 ) == 0 &&
198
+ GetByte (12 ) == 0 ;
185
199
}
186
200
187
201
bool CNetAddr::IsRFC4862 () const
188
202
{
189
203
static const unsigned char pchRFC4862[] = {0xFE ,0x80 ,0 ,0 ,0 ,0 ,0 ,0 };
190
- return ( memcmp (ip, pchRFC4862, sizeof (pchRFC4862)) == 0 ) ;
204
+ return IsIPv6 () && memcmp (ip, pchRFC4862, sizeof (pchRFC4862)) == 0 ;
191
205
}
192
206
193
207
bool CNetAddr::IsRFC4193 () const
194
208
{
195
- return ( (GetByte (15 ) & 0xFE ) == 0xFC ) ;
209
+ return IsIPv6 () && (GetByte (15 ) & 0xFE ) == 0xFC ;
196
210
}
197
211
198
212
bool CNetAddr::IsRFC6145 () const
199
213
{
200
214
static const unsigned char pchRFC6145[] = {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0xFF ,0xFF ,0 ,0 };
201
- return ( memcmp (ip, pchRFC6145, sizeof (pchRFC6145)) == 0 ) ;
215
+ return IsIPv6 () && memcmp (ip, pchRFC6145, sizeof (pchRFC6145)) == 0 ;
202
216
}
203
217
204
218
bool CNetAddr::IsRFC4843 () const
205
219
{
206
- return (GetByte (15 ) == 0x20 && GetByte (14 ) == 0x01 && GetByte (13 ) == 0x00 && (GetByte (12 ) & 0xF0 ) == 0x10 );
220
+ return IsIPv6 () && GetByte (15 ) == 0x20 && GetByte (14 ) == 0x01 &&
221
+ GetByte (13 ) == 0x00 && (GetByte (12 ) & 0xF0 ) == 0x10 ;
207
222
}
208
223
209
224
bool CNetAddr::IsRFC7343 () const
210
225
{
211
- return (GetByte (15 ) == 0x20 && GetByte (14 ) == 0x01 && GetByte (13 ) == 0x00 && (GetByte (12 ) & 0xF0 ) == 0x20 );
226
+ return IsIPv6 () && GetByte (15 ) == 0x20 && GetByte (14 ) == 0x01 &&
227
+ GetByte (13 ) == 0x00 && (GetByte (12 ) & 0xF0 ) == 0x20 ;
212
228
}
213
229
214
230
bool CNetAddr::IsHeNet () const
@@ -222,10 +238,7 @@ bool CNetAddr::IsHeNet() const
222
238
*
223
239
* @see CNetAddr::SetSpecial(const std::string &)
224
240
*/
225
- bool CNetAddr::IsTor () const
226
- {
227
- return (memcmp (ip, pchOnionCat, sizeof (pchOnionCat)) == 0 );
228
- }
241
+ bool CNetAddr::IsTor () const { return m_net == NET_ONION; }
229
242
230
243
bool CNetAddr::IsLocal () const
231
244
{
@@ -235,7 +248,7 @@ bool CNetAddr::IsLocal() const
235
248
236
249
// IPv6 loopback (::1/128)
237
250
static const unsigned char pchLocal[16 ] = {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 };
238
- if (memcmp (ip, pchLocal, 16 ) == 0 )
251
+ if (IsIPv6 () && memcmp (ip, pchLocal, 16 ) == 0 )
239
252
return true ;
240
253
241
254
return false ;
@@ -259,12 +272,12 @@ bool CNetAddr::IsValid() const
259
272
// header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
260
273
// so if the first length field is garbled, it reads the second batch
261
274
// of addr misaligned by 3 bytes.
262
- if (memcmp (ip, pchIPv4+3 , sizeof (pchIPv4)-3 ) == 0 )
275
+ if (IsIPv6 () && memcmp (ip, pchIPv4+3 , sizeof (pchIPv4)-3 ) == 0 )
263
276
return false ;
264
277
265
278
// unspecified IPv6 address (::/128)
266
279
unsigned char ipNone6[16 ] = {};
267
- if (memcmp (ip, ipNone6, 16 ) == 0 )
280
+ if (IsIPv6 () && memcmp (ip, ipNone6, 16 ) == 0 )
268
281
return false ;
269
282
270
283
// documentation IPv6 address
@@ -311,7 +324,7 @@ bool CNetAddr::IsRoutable() const
311
324
*/
312
325
bool CNetAddr::IsInternal () const
313
326
{
314
- return memcmp (ip, g_internal_prefix, sizeof (g_internal_prefix)) == 0 ;
327
+ return m_net == NET_INTERNAL ;
315
328
}
316
329
317
330
enum Network CNetAddr::GetNetwork () const
@@ -322,13 +335,7 @@ enum Network CNetAddr::GetNetwork() const
322
335
if (!IsRoutable ())
323
336
return NET_UNROUTABLE;
324
337
325
- if (IsIPv4 ())
326
- return NET_IPV4;
327
-
328
- if (IsTor ())
329
- return NET_ONION;
330
-
331
- return NET_IPV6;
338
+ return m_net;
332
339
}
333
340
334
341
std::string CNetAddr::ToStringIP () const
@@ -362,12 +369,12 @@ std::string CNetAddr::ToString() const
362
369
363
370
bool operator ==(const CNetAddr& a, const CNetAddr& b)
364
371
{
365
- return ( memcmp (a.ip , b.ip , 16 ) == 0 ) ;
372
+ return a. m_net == b. m_net && memcmp (a.ip , b.ip , 16 ) == 0 ;
366
373
}
367
374
368
375
bool operator <(const CNetAddr& a, const CNetAddr& b)
369
376
{
370
- return ( memcmp (a.ip , b.ip , 16 ) < 0 );
377
+ return a. m_net < b. m_net || (a. m_net == b. m_net && memcmp (a.ip , b.ip , 16 ) < 0 );
371
378
}
372
379
373
380
/* *
@@ -813,7 +820,7 @@ CSubNet::CSubNet(const CNetAddr &addr):
813
820
*/
814
821
bool CSubNet::Match (const CNetAddr &addr) const
815
822
{
816
- if (!valid || !addr.IsValid ())
823
+ if (!valid || !addr.IsValid () || network. m_net != addr. m_net )
817
824
return false ;
818
825
for (int x=0 ; x<16 ; ++x)
819
826
if ((addr.ip [x] & netmask[x]) != network.ip [x])
0 commit comments