|
| 1 | +/* |
| 2 | + * Copyright (c) 2004-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + */ |
| 10 | +#pragma once |
| 11 | + |
| 12 | +#include <folly/IPAddress.h> |
| 13 | + |
| 14 | +#include "common/network/if/gen-cpp2/Address_types.h" |
| 15 | + |
| 16 | +namespace facebook { |
| 17 | +namespace network { |
| 18 | + |
| 19 | +namespace detail { |
| 20 | + |
| 21 | +// Trait for V4 vs V6 |
| 22 | +template <typename T> |
| 23 | +struct IPVersion; |
| 24 | +template <> |
| 25 | +struct IPVersion<folly::IPAddressV4> { |
| 26 | + static const thrift::AddressType value = thrift::AddressType::V4; |
| 27 | +}; |
| 28 | +template <> |
| 29 | +struct IPVersion<folly::IPAddressV6> { |
| 30 | + static const thrift::AddressType value = thrift::AddressType::V6; |
| 31 | +}; |
| 32 | + |
| 33 | +template <class T> |
| 34 | +thrift::Address toAddressImpl(const T& addr) { |
| 35 | + thrift::Address result; |
| 36 | + *result.addr() = addr.toFullyQualified(); |
| 37 | + *result.type() = IPVersion<T>::value; |
| 38 | + return result; |
| 39 | +} |
| 40 | + |
| 41 | +} // namespace detail |
| 42 | + |
| 43 | +inline thrift::Address toAddress(const folly::IPAddress& ip) { |
| 44 | + // clang-format off |
| 45 | + return |
| 46 | + ip.isV4() ? detail::toAddressImpl(ip.asV4()) : |
| 47 | + ip.isV6() ? detail::toAddressImpl(ip.asV6()) : |
| 48 | + thrift::Address(); |
| 49 | + // clang-format on |
| 50 | +} |
| 51 | + |
| 52 | +template <class IPAddressVx> |
| 53 | +thrift::BinaryAddress toBinaryAddressImpl(const IPAddressVx& addr) { |
| 54 | + thrift::BinaryAddress result; |
| 55 | + result.addr()->append( |
| 56 | + reinterpret_cast<const char*>(addr.bytes()), IPAddressVx::byteCount()); |
| 57 | + return result; |
| 58 | +} |
| 59 | + |
| 60 | +inline thrift::BinaryAddress toBinaryAddress(const folly::IPAddress& addr) { |
| 61 | + // clang-format off |
| 62 | + return |
| 63 | + addr.isV4() ? toBinaryAddressImpl(addr.asV4()) : |
| 64 | + addr.isV6() ? toBinaryAddressImpl(addr.asV6()) : |
| 65 | + thrift::BinaryAddress(); |
| 66 | + // clang-format on |
| 67 | +} |
| 68 | + |
| 69 | +inline folly::IPAddress toIPAddress(const thrift::Address& input) { |
| 70 | + return *input.type() != thrift::AddressType::VUNSPEC |
| 71 | + ? folly::IPAddress(*input.addr()) |
| 72 | + : folly::IPAddress(); |
| 73 | +} |
| 74 | + |
| 75 | +inline folly::Expected<folly::IPAddress, folly::IPAddressFormatError> |
| 76 | +tryToIPAddress(const thrift::Address& input) { |
| 77 | + return folly::IPAddress::tryFromString(*input.addr()); |
| 78 | +} |
| 79 | + |
| 80 | +inline folly::IPAddress toIPAddress(const thrift::BinaryAddress& addr) { |
| 81 | + return folly::IPAddress::fromBinary( |
| 82 | + folly::ByteRange(folly::StringPiece(*addr.addr()))); |
| 83 | +} |
| 84 | + |
| 85 | +inline folly::Expected<folly::IPAddress, folly::IPAddressFormatError> |
| 86 | +tryToIPAddress(const thrift::BinaryAddress& addr) { |
| 87 | + return folly::IPAddress::tryFromBinary( |
| 88 | + folly::ByteRange(folly::StringPiece(*addr.addr()))); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Convert a IPPrefix thrift struct to CIDR prefix. |
| 93 | + * If the `mask` is set, the IP address will be masked by the prefix length. |
| 94 | + */ |
| 95 | +inline folly::CIDRNetwork toCIDRNetwork( |
| 96 | + const thrift::IPPrefix& prefix, |
| 97 | + bool mask = false) { |
| 98 | + auto ip = folly::IPAddress::fromBinary( |
| 99 | + folly::ByteRange(folly::StringPiece(*prefix.prefixAddress()->addr()))); |
| 100 | + auto maskLen = static_cast<uint8_t>(*prefix.prefixLength()); |
| 101 | + |
| 102 | + return {mask ? ip.mask(maskLen) : ip, maskLen}; |
| 103 | +} |
| 104 | + |
| 105 | +inline folly::Expected<folly::CIDRNetwork, folly::IPAddressFormatError> |
| 106 | +tryToCIDRNetwork(const thrift::IPPrefix& prefix, bool mask = false) { |
| 107 | + auto maybeIp = folly::IPAddress::tryFromBinary( |
| 108 | + folly::ByteRange(folly::StringPiece(*prefix.prefixAddress()->addr()))); |
| 109 | + if (maybeIp) { |
| 110 | + auto maskLen = static_cast<uint8_t>(*prefix.prefixLength()); |
| 111 | + return folly::CIDRNetwork{ |
| 112 | + mask ? (*maybeIp).mask(maskLen) : *maybeIp, maskLen}; |
| 113 | + } else { |
| 114 | + return folly::makeUnexpected(maybeIp.error()); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +inline thrift::IPPrefix toIPPrefix(const folly::CIDRNetwork& network) { |
| 119 | + thrift::IPPrefix result; |
| 120 | + *result.prefixAddress() = toBinaryAddress(network.first); |
| 121 | + *result.prefixLength() = network.second; |
| 122 | + return result; |
| 123 | +} |
| 124 | + |
| 125 | +inline thrift::IPPrefix toIPPrefix( |
| 126 | + const std::string& address, |
| 127 | + uint32_t masklen) { |
| 128 | + thrift::IPPrefix result; |
| 129 | + *result.prefixAddress() = toBinaryAddress(folly::IPAddress(address)); |
| 130 | + *result.prefixLength() = masklen; |
| 131 | + return result; |
| 132 | +} |
| 133 | + |
| 134 | +} /* namespace network */ |
| 135 | +} /* namespace facebook */ |
0 commit comments