sockaddr_utils.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #ifndef GRPC_CORE_LIB_IOMGR_SOCKADDR_UTILS_H
  19. #define GRPC_CORE_LIB_IOMGR_SOCKADDR_UTILS_H
  20. #include <grpc/support/port_platform.h>
  21. #include <string>
  22. #include "src/core/lib/iomgr/resolve_address.h"
  23. /* Returns true if addr is an IPv4-mapped IPv6 address within the
  24. ::ffff:0.0.0.0/96 range, or false otherwise.
  25. If addr4_out is non-NULL, the inner IPv4 address will be copied here when
  26. returning true. */
  27. int grpc_sockaddr_is_v4mapped(const grpc_resolved_address* addr,
  28. grpc_resolved_address* addr4_out);
  29. /* If addr is an AF_INET address, writes the corresponding ::ffff:0.0.0.0/96
  30. address to addr6_out and returns true. Otherwise returns false. */
  31. int grpc_sockaddr_to_v4mapped(const grpc_resolved_address* addr,
  32. grpc_resolved_address* addr6_out);
  33. /* If addr is ::, 0.0.0.0, or ::ffff:0.0.0.0, writes the port number to
  34. *port_out (if not NULL) and returns true, otherwise returns false. */
  35. int grpc_sockaddr_is_wildcard(const grpc_resolved_address* addr, int* port_out);
  36. /* Writes 0.0.0.0:port and [::]:port to separate sockaddrs. */
  37. void grpc_sockaddr_make_wildcards(int port, grpc_resolved_address* wild4_out,
  38. grpc_resolved_address* wild6_out);
  39. /* Writes 0.0.0.0:port. */
  40. void grpc_sockaddr_make_wildcard4(int port, grpc_resolved_address* wild_out);
  41. /* Writes [::]:port. */
  42. void grpc_sockaddr_make_wildcard6(int port, grpc_resolved_address* wild_out);
  43. /* Return the IP port number of a sockaddr */
  44. int grpc_sockaddr_get_port(const grpc_resolved_address* addr);
  45. /* Set IP port number of a sockaddr */
  46. int grpc_sockaddr_set_port(grpc_resolved_address* addr, int port);
  47. // Converts a sockaddr into a newly-allocated human-readable string.
  48. //
  49. // Currently, only the AF_INET and AF_INET6 families are recognized.
  50. // If the normalize flag is enabled, ::ffff:0.0.0.0/96 IPv6 addresses are
  51. // displayed as plain IPv4.
  52. std::string grpc_sockaddr_to_string(const grpc_resolved_address* addr,
  53. bool normalize);
  54. // TODO(yashykt): Remove this function and replace usages with
  55. // `grpc_string_to_sockaddr_new`
  56. void grpc_string_to_sockaddr(grpc_resolved_address* out, const char* addr,
  57. int port);
  58. // Newer form of grpc_string_to_sockaddr which returns an error instead of
  59. // crashing if \a addr is not IPv6/IPv6
  60. grpc_error* grpc_string_to_sockaddr_new(grpc_resolved_address* out,
  61. const char* addr, int port);
  62. /* Returns the URI string corresponding to \a addr */
  63. std::string grpc_sockaddr_to_uri(const grpc_resolved_address* addr);
  64. /* Returns the URI scheme corresponding to \a addr */
  65. const char* grpc_sockaddr_get_uri_scheme(const grpc_resolved_address* addr);
  66. int grpc_sockaddr_get_family(const grpc_resolved_address* resolved_addr);
  67. std::string grpc_sockaddr_get_packed_host(
  68. const grpc_resolved_address* resolved_addr);
  69. // Applies a mask of \a mask_bits to IPv4/IPv6 addresses. Has no effect if the
  70. // address type is not IPv4/IPv6.
  71. void grpc_sockaddr_mask_bits(grpc_resolved_address* address,
  72. uint32_t mask_bits);
  73. // If \a address is IPv4/IPv6, checks if the IP address falls in the CIDR
  74. // specified by \a subnet_address and \a mask_bits.
  75. // Returns false if \a address is not an IPv4/IPv6 address. The ports (if set)
  76. // are ignored for matching purposes. Note that, \a subnet_address should be
  77. // normalized, i.e., `grpc_sockaddr_mask_bits` should have been called on it if
  78. // necessary.
  79. bool grpc_sockaddr_match_subnet(const grpc_resolved_address* address,
  80. const grpc_resolved_address* subnet_address,
  81. uint32_t mask_bits);
  82. #endif /* GRPC_CORE_LIB_IOMGR_SOCKADDR_UTILS_H */