1 /*
2 * Copyright (c) 2016, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /**
30 * @file
31 * This file implements IPv6 addresses.
32 */
33
34 #include "ip6_address.hpp"
35
36 #include <stdio.h>
37
38 #include "common/array.hpp"
39 #include "common/as_core_type.hpp"
40 #include "common/code_utils.hpp"
41 #include "common/encoding.hpp"
42 #include "common/num_utils.hpp"
43 #include "common/numeric_limits.hpp"
44 #include "common/random.hpp"
45 #include "instance/instance.hpp"
46 #include "net/ip4_types.hpp"
47 #include "net/netif.hpp"
48
49 namespace ot {
50 namespace Ip6 {
51
52 //---------------------------------------------------------------------------------------------------------------------
53 // NetworkPrefix methods
54
GenerateRandomUla(void)55 Error NetworkPrefix::GenerateRandomUla(void)
56 {
57 m8[0] = 0xfd;
58
59 return Random::Crypto::FillBuffer(&m8[1], kSize - 1);
60 }
61
62 //---------------------------------------------------------------------------------------------------------------------
63 // Prefix methods
64
Set(const uint8_t * aPrefix,uint8_t aLength)65 void Prefix::Set(const uint8_t *aPrefix, uint8_t aLength)
66 {
67 memcpy(mPrefix.mFields.m8, aPrefix, SizeForLength(aLength));
68 mLength = aLength;
69 }
70
IsLinkLocal(void) const71 bool Prefix::IsLinkLocal(void) const
72 {
73 return (mLength >= 10) &&
74 ((mPrefix.mFields.m16[0] & BigEndian::HostSwap16(0xffc0)) == BigEndian::HostSwap16(0xfe80));
75 }
76
IsMulticast(void) const77 bool Prefix::IsMulticast(void) const { return (mLength >= 8) && (mPrefix.mFields.m8[0] == 0xff); }
78
IsUniqueLocal(void) const79 bool Prefix::IsUniqueLocal(void) const { return (mLength >= 7) && ((mPrefix.mFields.m8[0] & 0xfe) == 0xfc); }
80
IsEqual(const uint8_t * aPrefixBytes,uint8_t aPrefixLength) const81 bool Prefix::IsEqual(const uint8_t *aPrefixBytes, uint8_t aPrefixLength) const
82 {
83 return (mLength == aPrefixLength) && (MatchLength(GetBytes(), aPrefixBytes, GetBytesSize()) >= mLength);
84 }
85
ContainsPrefix(const Prefix & aSubPrefix) const86 bool Prefix::ContainsPrefix(const Prefix &aSubPrefix) const
87 {
88 return (mLength >= aSubPrefix.mLength) &&
89 (MatchLength(GetBytes(), aSubPrefix.GetBytes(), aSubPrefix.GetBytesSize()) >= aSubPrefix.GetLength());
90 }
91
ContainsPrefix(const NetworkPrefix & aSubPrefix) const92 bool Prefix::ContainsPrefix(const NetworkPrefix &aSubPrefix) const
93 {
94 return (mLength >= NetworkPrefix::kLength) &&
95 (MatchLength(GetBytes(), aSubPrefix.m8, NetworkPrefix::kSize) >= NetworkPrefix::kLength);
96 }
97
Tidy(void)98 void Prefix::Tidy(void)
99 {
100 uint8_t byteLength = GetBytesSize();
101 uint8_t lastByteBitMask = ~(static_cast<uint8_t>(1 << (byteLength * 8 - mLength)) - 1);
102
103 if (byteLength != 0)
104 {
105 mPrefix.mFields.m8[byteLength - 1] &= lastByteBitMask;
106 }
107
108 for (uint16_t i = byteLength; i < GetArrayLength(mPrefix.mFields.m8); i++)
109 {
110 mPrefix.mFields.m8[i] = 0;
111 }
112 }
113
operator ==(const Prefix & aOther) const114 bool Prefix::operator==(const Prefix &aOther) const
115 {
116 return (mLength == aOther.mLength) && (MatchLength(GetBytes(), aOther.GetBytes(), GetBytesSize()) >= GetLength());
117 }
118
operator <(const Prefix & aOther) const119 bool Prefix::operator<(const Prefix &aOther) const
120 {
121 bool isSmaller;
122 uint8_t minLength;
123 uint8_t matchedLength;
124
125 minLength = Min(GetLength(), aOther.GetLength());
126 matchedLength = MatchLength(GetBytes(), aOther.GetBytes(), SizeForLength(minLength));
127
128 if (matchedLength >= minLength)
129 {
130 isSmaller = (GetLength() < aOther.GetLength());
131 ExitNow();
132 }
133
134 isSmaller = GetBytes()[matchedLength / kBitsPerByte] < aOther.GetBytes()[matchedLength / kBitsPerByte];
135
136 exit:
137 return isSmaller;
138 }
139
MatchLength(const uint8_t * aPrefixA,const uint8_t * aPrefixB,uint8_t aMaxSize)140 uint8_t Prefix::MatchLength(const uint8_t *aPrefixA, const uint8_t *aPrefixB, uint8_t aMaxSize)
141 {
142 uint8_t matchedLength = 0;
143
144 OT_ASSERT(aMaxSize <= Address::kSize);
145
146 for (uint8_t i = 0; i < aMaxSize; i++)
147 {
148 uint8_t diff = aPrefixA[i] ^ aPrefixB[i];
149
150 if (diff == 0)
151 {
152 matchedLength += kBitsPerByte;
153 }
154 else
155 {
156 while ((diff & 0x80) == 0)
157 {
158 matchedLength++;
159 diff <<= 1;
160 }
161
162 break;
163 }
164 }
165
166 return matchedLength;
167 }
168
IsValidNat64PrefixLength(uint8_t aLength)169 bool Prefix::IsValidNat64PrefixLength(uint8_t aLength)
170 {
171 return (aLength == 32) || (aLength == 40) || (aLength == 48) || (aLength == 56) || (aLength == 64) ||
172 (aLength == 96);
173 }
174
FromString(const char * aString)175 Error Prefix::FromString(const char *aString)
176 {
177 constexpr char kSlashChar = '/';
178 constexpr char kNullChar = '\0';
179
180 Error error = kErrorParse;
181 const char *cur;
182
183 VerifyOrExit(aString != nullptr);
184
185 cur = StringFind(aString, kSlashChar);
186 VerifyOrExit(cur != nullptr);
187
188 SuccessOrExit(AsCoreType(&mPrefix).ParseFrom(aString, kSlashChar));
189
190 cur++;
191 SuccessOrExit(StringParseUint8(cur, mLength, kMaxLength));
192 VerifyOrExit(*cur == kNullChar);
193
194 error = kErrorNone;
195
196 exit:
197 return error;
198 }
199
ToString(void) const200 Prefix::InfoString Prefix::ToString(void) const
201 {
202 InfoString string;
203
204 ToString(string);
205
206 return string;
207 }
208
ToString(char * aBuffer,uint16_t aSize) const209 void Prefix::ToString(char *aBuffer, uint16_t aSize) const
210 {
211 StringWriter writer(aBuffer, aSize);
212
213 ToString(writer);
214 }
215
ToString(StringWriter & aWriter) const216 void Prefix::ToString(StringWriter &aWriter) const
217 {
218 uint8_t sizeInUint16 = (GetBytesSize() + sizeof(uint16_t) - 1) / sizeof(uint16_t);
219 Prefix tidyPrefix = *this;
220
221 tidyPrefix.Tidy();
222 AsCoreType(&tidyPrefix.mPrefix).AppendHexWords(aWriter, sizeInUint16);
223
224 if (GetBytesSize() < Address::kSize - 1)
225 {
226 aWriter.Append("::");
227 }
228
229 aWriter.Append("/%d", mLength);
230 }
231
232 //---------------------------------------------------------------------------------------------------------------------
233 // InterfaceIdentifier methods
234
IsUnspecified(void) const235 bool InterfaceIdentifier::IsUnspecified(void) const { return (mFields.m32[0] == 0) && (mFields.m32[1] == 0); }
236
IsReserved(void) const237 bool InterfaceIdentifier::IsReserved(void) const
238 {
239 return IsSubnetRouterAnycast() || IsReservedSubnetAnycast() || IsAnycastLocator();
240 }
241
IsSubnetRouterAnycast(void) const242 bool InterfaceIdentifier::IsSubnetRouterAnycast(void) const { return (mFields.m32[0] == 0) && (mFields.m32[1] == 0); }
243
IsReservedSubnetAnycast(void) const244 bool InterfaceIdentifier::IsReservedSubnetAnycast(void) const
245 {
246 // Format of IID in a Reserved Subnet Anycast Address (RFC 2526)
247 //
248 // | 57 bits | 7 bits |
249 // +------------------+------------+
250 // | 1111110111...111 | anycast ID |
251 // +------------------+------------+
252
253 return (mFields.m32[0] == BigEndian::HostSwap32(0xfdffffff) && mFields.m16[2] == BigEndian::HostSwap16(0xffff) &&
254 mFields.m8[6] == 0xff && mFields.m8[7] >= 0x80);
255 }
256
GenerateRandom(void)257 void InterfaceIdentifier::GenerateRandom(void) { SuccessOrAssert(Random::Crypto::Fill(*this)); }
258
SetBytes(const uint8_t * aBuffer)259 void InterfaceIdentifier::SetBytes(const uint8_t *aBuffer) { memcpy(mFields.m8, aBuffer, kSize); }
260
SetFromExtAddress(const Mac::ExtAddress & aExtAddress)261 void InterfaceIdentifier::SetFromExtAddress(const Mac::ExtAddress &aExtAddress)
262 {
263 Mac::ExtAddress addr;
264
265 addr = aExtAddress;
266 addr.ToggleLocal();
267 addr.CopyTo(mFields.m8);
268 }
269
ConvertToExtAddress(Mac::ExtAddress & aExtAddress) const270 void InterfaceIdentifier::ConvertToExtAddress(Mac::ExtAddress &aExtAddress) const
271 {
272 aExtAddress.Set(mFields.m8);
273 aExtAddress.ToggleLocal();
274 }
275
ConvertToMacAddress(Mac::Address & aMacAddress) const276 void InterfaceIdentifier::ConvertToMacAddress(Mac::Address &aMacAddress) const
277 {
278 aMacAddress.SetExtended(mFields.m8);
279 aMacAddress.GetExtended().ToggleLocal();
280 }
281
SetToLocator(uint16_t aLocator)282 void InterfaceIdentifier::SetToLocator(uint16_t aLocator)
283 {
284 // Locator IID pattern `0000:00ff:fe00:xxxx`
285 mFields.m32[0] = BigEndian::HostSwap32(0x000000ff);
286 mFields.m16[2] = BigEndian::HostSwap16(0xfe00);
287 mFields.m16[3] = BigEndian::HostSwap16(aLocator);
288 }
289
IsLocator(void) const290 bool InterfaceIdentifier::IsLocator(void) const
291 {
292 // Locator IID pattern 0000:00ff:fe00:xxxx
293 return (mFields.m32[0] == BigEndian::HostSwap32(0x000000ff) && mFields.m16[2] == BigEndian::HostSwap16(0xfe00));
294 }
295
IsRoutingLocator(void) const296 bool InterfaceIdentifier::IsRoutingLocator(void) const
297 {
298 return (IsLocator() && (mFields.m8[6] < kAloc16Mask) && ((mFields.m8[6] & kRloc16ReservedBitMask) == 0));
299 }
300
IsAnycastLocator(void) const301 bool InterfaceIdentifier::IsAnycastLocator(void) const
302 {
303 // Anycast locator range 0xfc00- 0xfcff (`kAloc16Mask` is 0xfc)
304 return (IsLocator() && (mFields.m8[6] == kAloc16Mask));
305 }
306
IsAnycastServiceLocator(void) const307 bool InterfaceIdentifier::IsAnycastServiceLocator(void) const
308 {
309 uint16_t locator = GetLocator();
310
311 return (IsLocator() && (locator >= Mle::kAloc16ServiceStart) && (locator <= Mle::kAloc16ServiceEnd));
312 }
313
ApplyPrefix(const Prefix & aPrefix)314 void InterfaceIdentifier::ApplyPrefix(const Prefix &aPrefix)
315 {
316 if (aPrefix.GetLength() > NetworkPrefix::kLength)
317 {
318 Address::CopyBits(mFields.m8, aPrefix.GetBytes() + NetworkPrefix::kSize,
319 aPrefix.GetLength() - NetworkPrefix::kLength);
320 }
321 }
322
ToString(void) const323 InterfaceIdentifier::InfoString InterfaceIdentifier::ToString(void) const
324 {
325 InfoString string;
326
327 string.AppendHexBytes(mFields.m8, kSize);
328
329 return string;
330 }
331
332 //---------------------------------------------------------------------------------------------------------------------
333 // Address methods
334
IsUnspecified(void) const335 bool Address::IsUnspecified(void) const
336 {
337 return (mFields.m32[0] == 0 && mFields.m32[1] == 0 && mFields.m32[2] == 0 && mFields.m32[3] == 0);
338 }
339
IsLoopback(void) const340 bool Address::IsLoopback(void) const
341 {
342 return (mFields.m32[0] == 0 && mFields.m32[1] == 0 && mFields.m32[2] == 0 &&
343 mFields.m32[3] == BigEndian::HostSwap32(1));
344 }
345
IsLinkLocal(void) const346 bool Address::IsLinkLocal(void) const
347 {
348 return (mFields.m16[0] & BigEndian::HostSwap16(0xffc0)) == BigEndian::HostSwap16(0xfe80);
349 }
350
SetToLinkLocalAddress(const Mac::ExtAddress & aExtAddress)351 void Address::SetToLinkLocalAddress(const Mac::ExtAddress &aExtAddress)
352 {
353 mFields.m32[0] = BigEndian::HostSwap32(0xfe800000);
354 mFields.m32[1] = 0;
355 GetIid().SetFromExtAddress(aExtAddress);
356 }
357
SetToLinkLocalAddress(const InterfaceIdentifier & aIid)358 void Address::SetToLinkLocalAddress(const InterfaceIdentifier &aIid)
359 {
360 mFields.m32[0] = BigEndian::HostSwap32(0xfe800000);
361 mFields.m32[1] = 0;
362 SetIid(aIid);
363 }
364
IsLinkLocalMulticast(void) const365 bool Address::IsLinkLocalMulticast(void) const { return IsMulticast() && (GetScope() == kLinkLocalScope); }
366
IsLinkLocalAllNodesMulticast(void) const367 bool Address::IsLinkLocalAllNodesMulticast(void) const { return (*this == GetLinkLocalAllNodesMulticast()); }
368
SetToLinkLocalAllNodesMulticast(void)369 void Address::SetToLinkLocalAllNodesMulticast(void) { *this = GetLinkLocalAllNodesMulticast(); }
370
IsLinkLocalAllRoutersMulticast(void) const371 bool Address::IsLinkLocalAllRoutersMulticast(void) const { return (*this == GetLinkLocalAllRoutersMulticast()); }
372
SetToLinkLocalAllRoutersMulticast(void)373 void Address::SetToLinkLocalAllRoutersMulticast(void) { *this = GetLinkLocalAllRoutersMulticast(); }
374
IsRealmLocalMulticast(void) const375 bool Address::IsRealmLocalMulticast(void) const { return IsMulticast() && (GetScope() == kRealmLocalScope); }
376
IsMulticastLargerThanRealmLocal(void) const377 bool Address::IsMulticastLargerThanRealmLocal(void) const { return IsMulticast() && (GetScope() > kRealmLocalScope); }
378
IsRealmLocalAllNodesMulticast(void) const379 bool Address::IsRealmLocalAllNodesMulticast(void) const { return (*this == GetRealmLocalAllNodesMulticast()); }
380
SetToRealmLocalAllNodesMulticast(void)381 void Address::SetToRealmLocalAllNodesMulticast(void) { *this = GetRealmLocalAllNodesMulticast(); }
382
IsRealmLocalAllRoutersMulticast(void) const383 bool Address::IsRealmLocalAllRoutersMulticast(void) const { return (*this == GetRealmLocalAllRoutersMulticast()); }
384
SetToRealmLocalAllRoutersMulticast(void)385 void Address::SetToRealmLocalAllRoutersMulticast(void) { *this = GetRealmLocalAllRoutersMulticast(); }
386
IsRealmLocalAllMplForwarders(void) const387 bool Address::IsRealmLocalAllMplForwarders(void) const { return (*this == GetRealmLocalAllMplForwarders()); }
388
SetToRealmLocalAllMplForwarders(void)389 void Address::SetToRealmLocalAllMplForwarders(void) { *this = GetRealmLocalAllMplForwarders(); }
390
MatchesPrefix(const Prefix & aPrefix) const391 bool Address::MatchesPrefix(const Prefix &aPrefix) const
392 {
393 return Prefix::MatchLength(mFields.m8, aPrefix.GetBytes(), aPrefix.GetBytesSize()) >= aPrefix.GetLength();
394 }
395
MatchesPrefix(const uint8_t * aPrefix,uint8_t aPrefixLength) const396 bool Address::MatchesPrefix(const uint8_t *aPrefix, uint8_t aPrefixLength) const
397 {
398 return Prefix::MatchLength(mFields.m8, aPrefix, Prefix::SizeForLength(aPrefixLength)) >= aPrefixLength;
399 }
400
SetPrefix(const NetworkPrefix & aNetworkPrefix)401 void Address::SetPrefix(const NetworkPrefix &aNetworkPrefix) { mFields.mComponents.mNetworkPrefix = aNetworkPrefix; }
402
SetPrefix(const Prefix & aPrefix)403 void Address::SetPrefix(const Prefix &aPrefix) { CopyBits(mFields.m8, aPrefix.GetBytes(), aPrefix.GetLength()); }
404
CopyBits(uint8_t * aDst,const uint8_t * aSrc,uint8_t aNumBits)405 void Address::CopyBits(uint8_t *aDst, const uint8_t *aSrc, uint8_t aNumBits)
406 {
407 // This method copies `aNumBits` from `aSrc` into `aDst` handling
408 // the case where `aNumBits` may not be a multiple of 8. It leaves the
409 // remaining bits beyond `aNumBits` in `aDst` unchanged.
410
411 uint8_t numBytes = aNumBits / kBitsPerByte;
412 uint8_t extraBits = aNumBits % kBitsPerByte;
413
414 memcpy(aDst, aSrc, numBytes);
415
416 if (extraBits > 0)
417 {
418 uint8_t mask = ((0x80 >> (extraBits - 1)) - 1);
419
420 // `mask` has its higher (msb) `extraBits` bits as `0` and the remaining as `1`.
421 // Example with `extraBits` = 3:
422 // ((0x80 >> 2) - 1) = (0b0010_0000 - 1) = 0b0001_1111
423
424 aDst[numBytes] &= mask;
425 aDst[numBytes] |= (aSrc[numBytes] & ~mask);
426 }
427 }
428
SetMulticastNetworkPrefix(const uint8_t * aPrefix,uint8_t aPrefixLength)429 void Address::SetMulticastNetworkPrefix(const uint8_t *aPrefix, uint8_t aPrefixLength)
430 {
431 CopyBits(&mFields.m8[kMulticastNetworkPrefixOffset], aPrefix, aPrefixLength);
432 mFields.m8[kMulticastNetworkPrefixLengthOffset] = aPrefixLength;
433 }
434
SetToLocator(const NetworkPrefix & aNetworkPrefix,uint16_t aLocator)435 void Address::SetToLocator(const NetworkPrefix &aNetworkPrefix, uint16_t aLocator)
436 {
437 SetPrefix(aNetworkPrefix);
438 GetIid().SetToLocator(aLocator);
439 }
440
GetScope(void) const441 uint8_t Address::GetScope(void) const
442 {
443 uint8_t rval;
444
445 if (IsMulticast())
446 {
447 rval = mFields.m8[1] & 0xf;
448 }
449 else if (IsLinkLocal())
450 {
451 rval = kLinkLocalScope;
452 }
453 else if (IsLoopback())
454 {
455 rval = kNodeLocalScope;
456 }
457 else
458 {
459 rval = kGlobalScope;
460 }
461
462 return rval;
463 }
464
PrefixMatch(const Address & aOther) const465 uint8_t Address::PrefixMatch(const Address &aOther) const
466 {
467 return Prefix::MatchLength(mFields.m8, aOther.mFields.m8, sizeof(Address));
468 }
469
MatchesFilter(TypeFilter aFilter) const470 bool Address::MatchesFilter(TypeFilter aFilter) const
471 {
472 bool matches = true;
473
474 switch (aFilter)
475 {
476 case kTypeAny:
477 break;
478
479 case kTypeUnicast:
480 matches = !IsUnspecified() && !IsMulticast();
481 break;
482
483 case kTypeMulticast:
484 matches = IsMulticast();
485 break;
486
487 case kTypeMulticastLargerThanRealmLocal:
488 matches = IsMulticastLargerThanRealmLocal();
489 break;
490 }
491
492 return matches;
493 }
494
SynthesizeFromIp4Address(const Prefix & aPrefix,const Ip4::Address & aIp4Address)495 void Address::SynthesizeFromIp4Address(const Prefix &aPrefix, const Ip4::Address &aIp4Address)
496 {
497 // The prefix length must be 32, 40, 48, 56, 64, 96. IPv4 bytes are added
498 // after the prefix, skipping over the bits 64 to 71 (byte at `kSkipIndex`)
499 // which must be set to zero. The suffix is set to zero (per RFC 6052).
500 //
501 // +--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
502 // |PL| 0-------------32--40--48--56--64--72--80--88--96--104---------|
503 // +--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
504 // |32| prefix |v4(32) | u | suffix |
505 // +--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
506 // |40| prefix |v4(24) | u |(8)| suffix |
507 // +--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
508 // |48| prefix |v4(16) | u | (16) | suffix |
509 // +--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
510 // |56| prefix |(8)| u | v4(24) | suffix |
511 // +--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
512 // |64| prefix | u | v4(32) | suffix |
513 // +--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
514 // |96| prefix | v4(32) |
515 // +--+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
516
517 constexpr uint8_t kSkipIndex = 8;
518
519 uint8_t ip6Index;
520
521 OT_ASSERT(aPrefix.IsValidNat64());
522
523 Clear();
524 SetPrefix(aPrefix);
525
526 ip6Index = aPrefix.GetLength() / kBitsPerByte;
527
528 for (uint8_t i = 0; i < Ip4::Address::kSize; i++)
529 {
530 if (ip6Index == kSkipIndex)
531 {
532 ip6Index++;
533 }
534
535 mFields.m8[ip6Index++] = aIp4Address.GetBytes()[i];
536 }
537 }
538
FromString(const char * aString)539 Error Address::FromString(const char *aString)
540 {
541 constexpr char kNullChar = '\0';
542
543 return ParseFrom(aString, kNullChar);
544 }
545
ParseFrom(const char * aString,char aTerminatorChar)546 Error Address::ParseFrom(const char *aString, char aTerminatorChar)
547 {
548 constexpr uint8_t kInvalidIndex = 0xff;
549 constexpr char kColonChar = ':';
550 constexpr char kDotChar = '.';
551
552 Error error = kErrorParse;
553 uint8_t index = 0;
554 uint8_t endIndex = kSize / sizeof(uint16_t);
555 uint8_t colonIndex = kInvalidIndex;
556 bool hasIp4 = false;
557
558 // Check if the string starts with "::".
559
560 if (*aString == kColonChar)
561 {
562 aString++;
563 VerifyOrExit(*aString == kColonChar);
564 aString++;
565 colonIndex = index;
566 }
567
568 while (*aString != aTerminatorChar)
569 {
570 const char *start = aString;
571 uint32_t value = 0;
572
573 // Parse hex number
574
575 while (true)
576 {
577 char c = *aString;
578 uint8_t digit;
579
580 if (('A' <= c) && (c <= 'F'))
581 {
582 digit = static_cast<uint8_t>(c - 'A' + 10);
583 }
584 else if (('a' <= c) && (c <= 'f'))
585 {
586 digit = static_cast<uint8_t>(c - 'a' + 10);
587 }
588 else if (('0' <= c) && (c <= '9'))
589 {
590 digit = static_cast<uint8_t>(c - '0');
591 }
592 else
593 {
594 break;
595 }
596
597 aString++;
598 value = (value << 4) + digit;
599
600 VerifyOrExit(value <= NumericLimits<uint16_t>::kMax);
601 }
602
603 VerifyOrExit(aString != start);
604
605 if (*aString == kDotChar)
606 {
607 // IPv6 address contains an embedded IPv4 address.
608 aString = start;
609 hasIp4 = true;
610 endIndex -= Ip4::Address::kSize / sizeof(uint16_t);
611 VerifyOrExit(index <= endIndex);
612 break;
613 }
614
615 VerifyOrExit((*aString == kColonChar) || (*aString == aTerminatorChar));
616
617 VerifyOrExit(index < endIndex);
618 mFields.m16[index++] = BigEndian::HostSwap16(static_cast<uint16_t>(value));
619
620 if (*aString == kColonChar)
621 {
622 aString++;
623
624 if (*aString == kColonChar)
625 {
626 VerifyOrExit(colonIndex == kInvalidIndex);
627 colonIndex = index;
628 aString++;
629 }
630 }
631 }
632
633 if (index < endIndex)
634 {
635 uint8_t wordsToCopy;
636
637 VerifyOrExit(colonIndex != kInvalidIndex);
638
639 wordsToCopy = index - colonIndex;
640
641 memmove(&mFields.m16[endIndex - wordsToCopy], &mFields.m16[colonIndex], wordsToCopy * sizeof(uint16_t));
642 memset(&mFields.m16[colonIndex], 0, (endIndex - index) * sizeof(uint16_t));
643 }
644
645 if (hasIp4)
646 {
647 Ip4::Address ip4Addr;
648
649 SuccessOrExit(error = ip4Addr.FromString(aString, aTerminatorChar));
650 memcpy(GetArrayEnd(mFields.m8) - Ip4::Address::kSize, ip4Addr.GetBytes(), Ip4::Address::kSize);
651 }
652
653 error = kErrorNone;
654
655 exit:
656 return error;
657 }
658
ToString(void) const659 Address::InfoString Address::ToString(void) const
660 {
661 InfoString string;
662
663 ToString(string);
664
665 return string;
666 }
667
ToString(char * aBuffer,uint16_t aSize) const668 void Address::ToString(char *aBuffer, uint16_t aSize) const
669 {
670 StringWriter writer(aBuffer, aSize);
671 ToString(writer);
672 }
673
ToString(StringWriter & aWriter) const674 void Address::ToString(StringWriter &aWriter) const
675 {
676 AppendHexWords(aWriter, static_cast<uint8_t>(GetArrayLength(mFields.m16)));
677 }
678
AppendHexWords(StringWriter & aWriter,uint8_t aLength) const679 void Address::AppendHexWords(StringWriter &aWriter, uint8_t aLength) const
680 {
681 // Appends the first `aLength` elements in `mFields.m16[]` array
682 // as hex words.
683
684 for (uint8_t index = 0; index < aLength; index++)
685 {
686 if (index > 0)
687 {
688 aWriter.Append(":");
689 }
690
691 aWriter.Append("%x", BigEndian::HostSwap16(mFields.m16[index]));
692 }
693 }
694
GetLinkLocalAllNodesMulticast(void)695 const Address &Address::GetLinkLocalAllNodesMulticast(void)
696 {
697 return AsCoreType(&Netif::kLinkLocalAllNodesMulticastAddress.mAddress);
698 }
699
GetLinkLocalAllRoutersMulticast(void)700 const Address &Address::GetLinkLocalAllRoutersMulticast(void)
701 {
702 return AsCoreType(&Netif::kLinkLocalAllRoutersMulticastAddress.mAddress);
703 }
704
GetRealmLocalAllNodesMulticast(void)705 const Address &Address::GetRealmLocalAllNodesMulticast(void)
706 {
707 return AsCoreType(&Netif::kRealmLocalAllNodesMulticastAddress.mAddress);
708 }
709
GetRealmLocalAllRoutersMulticast(void)710 const Address &Address::GetRealmLocalAllRoutersMulticast(void)
711 {
712 return AsCoreType(&Netif::kRealmLocalAllRoutersMulticastAddress.mAddress);
713 }
714
GetRealmLocalAllMplForwarders(void)715 const Address &Address::GetRealmLocalAllMplForwarders(void)
716 {
717 return AsCoreType(&Netif::kRealmLocalAllMplForwardersMulticastAddress.mAddress);
718 }
719
720 } // namespace Ip6
721 } // namespace ot
722