MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / PCPWrapAddress

Function PCPWrapAddress

src/common/pcp.cpp:188–206  ·  view source on GitHub ↗

Wrap address in IPv6 according to RFC6887. wrapped_addr needs to be able to store 16 bytes.

Source from the content-addressed store, hash-verified

186
187//! Wrap address in IPv6 according to RFC6887. wrapped_addr needs to be able to store 16 bytes.
188[[nodiscard]] bool PCPWrapAddress(std::span<uint8_t> wrapped_addr, const CNetAddr &addr)
189{
190 Assume(wrapped_addr.size() == ADDR_IPV6_SIZE);
191 if (addr.IsIPv4()) {
192 struct in_addr addr4;
193 if (!addr.GetInAddr(&addr4)) return false;
194 // Section 5: "When the address field holds an IPv4 address, an IPv4-mapped IPv6 address [RFC4291] is used (::ffff:0:0/96)."
195 std::memcpy(wrapped_addr.data(), IPV4_IN_IPV6_PREFIX.data(), IPV4_IN_IPV6_PREFIX.size());
196 std::memcpy(wrapped_addr.data() + IPV4_IN_IPV6_PREFIX.size(), &addr4, ADDR_IPV4_SIZE);
197 return true;
198 } else if (addr.IsIPv6()) {
199 struct in6_addr addr6;
200 if (!addr.GetIn6Addr(&addr6)) return false;
201 std::memcpy(wrapped_addr.data(), &addr6, ADDR_IPV6_SIZE);
202 return true;
203 } else {
204 return false;
205 }
206}
207
208//! Unwrap PCP-encoded address according to RFC6887.
209CNetAddr PCPUnwrapAddress(std::span<const uint8_t> wrapped_addr)

Callers 1

PCPRequestPortMapFunction · 0.85

Calls 4

GetInAddrMethod · 0.80
GetIn6AddrMethod · 0.80
sizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected