IPAddress.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright 2014-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <sys/types.h>
  18. #include <string>
  19. #include <folly/portability/Sockets.h>
  20. namespace folly {
  21. namespace detail {
  22. std::string familyNameStrDefault(sa_family_t family);
  23. inline std::string familyNameStr(sa_family_t family) {
  24. switch (family) {
  25. case AF_INET:
  26. return "AF_INET";
  27. case AF_INET6:
  28. return "AF_INET6";
  29. case AF_UNSPEC:
  30. return "AF_UNSPEC";
  31. case AF_UNIX:
  32. return "AF_UNIX";
  33. default:
  34. return familyNameStrDefault(family);
  35. }
  36. }
  37. [[noreturn]] void getNthMSBitImplThrow(size_t bitCount, sa_family_t family);
  38. template <typename IPAddrType>
  39. inline bool
  40. getNthMSBitImpl(const IPAddrType& ip, size_t bitIndex, sa_family_t family) {
  41. if (bitIndex >= ip.bitCount()) {
  42. getNthMSBitImplThrow(ip.bitCount(), family);
  43. }
  44. // Underlying bytes are in n/w byte order
  45. return (ip.getNthMSByte(bitIndex / 8) & (0x80 >> (bitIndex % 8))) != 0;
  46. }
  47. } // namespace detail
  48. } // namespace folly