You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.9KB

  1. /*
  2. * IP common code
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FFmpeg; if not, write to the Free Software * Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "ip.h"
  21. #include "libavutil/avstring.h"
  22. static int compare_addr(const struct sockaddr_storage *a,
  23. const struct sockaddr_storage *b)
  24. {
  25. if (a->ss_family != b->ss_family)
  26. return 1;
  27. if (a->ss_family == AF_INET) {
  28. return (((const struct sockaddr_in *)a)->sin_addr.s_addr !=
  29. ((const struct sockaddr_in *)b)->sin_addr.s_addr);
  30. }
  31. #if HAVE_STRUCT_SOCKADDR_IN6
  32. if (a->ss_family == AF_INET6) {
  33. const uint8_t *s6_addr_a = ((const struct sockaddr_in6 *)a)->sin6_addr.s6_addr;
  34. const uint8_t *s6_addr_b = ((const struct sockaddr_in6 *)b)->sin6_addr.s6_addr;
  35. return memcmp(s6_addr_a, s6_addr_b, 16);
  36. }
  37. #endif
  38. return 1;
  39. }
  40. int ff_ip_check_source_lists(struct sockaddr_storage *source_addr_ptr, IPSourceFilters *s)
  41. {
  42. int i;
  43. if (s->nb_exclude_addrs) {
  44. for (i = 0; i < s->nb_exclude_addrs; i++) {
  45. if (!compare_addr(source_addr_ptr, &s->exclude_addrs[i]))
  46. return 1;
  47. }
  48. }
  49. if (s->nb_include_addrs) {
  50. for (i = 0; i < s->nb_include_addrs; i++) {
  51. if (!compare_addr(source_addr_ptr, &s->include_addrs[i]))
  52. return 0;
  53. }
  54. return 1;
  55. }
  56. return 0;
  57. }
  58. struct addrinfo *ff_ip_resolve_host(void *log_ctx,
  59. const char *hostname, int port,
  60. int type, int family, int flags)
  61. {
  62. struct addrinfo hints = { 0 }, *res = 0;
  63. int error;
  64. char sport[16];
  65. const char *node = 0, *service = "0";
  66. if (port > 0) {
  67. snprintf(sport, sizeof(sport), "%d", port);
  68. service = sport;
  69. }
  70. if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
  71. node = hostname;
  72. }
  73. hints.ai_socktype = type;
  74. hints.ai_family = family;
  75. hints.ai_flags = flags;
  76. if ((error = getaddrinfo(node, service, &hints, &res))) {
  77. res = NULL;
  78. av_log(log_ctx, AV_LOG_ERROR, "getaddrinfo(%s, %s): %s\n",
  79. node ? node : "unknown",
  80. service,
  81. gai_strerror(error));
  82. }
  83. return res;
  84. }
  85. static int ip_parse_addr_list(void *log_ctx, const char *buf,
  86. struct sockaddr_storage **address_list_ptr,
  87. int *address_list_size_ptr)
  88. {
  89. struct addrinfo *ai = NULL;
  90. /* Resolve all of the IPs */
  91. while (buf && buf[0]) {
  92. char* host = av_get_token(&buf, ",");
  93. if (!host)
  94. return AVERROR(ENOMEM);
  95. ai = ff_ip_resolve_host(log_ctx, host, 0, SOCK_DGRAM, AF_UNSPEC, 0);
  96. av_freep(&host);
  97. if (ai) {
  98. struct sockaddr_storage source_addr = {0};
  99. memcpy(&source_addr, ai->ai_addr, ai->ai_addrlen);
  100. freeaddrinfo(ai);
  101. av_dynarray2_add((void **)address_list_ptr, address_list_size_ptr, sizeof(source_addr), (uint8_t *)&source_addr);
  102. if (!*address_list_ptr)
  103. return AVERROR(ENOMEM);
  104. } else {
  105. return AVERROR(EINVAL);
  106. }
  107. if (*buf)
  108. buf++;
  109. }
  110. return 0;
  111. }
  112. static int ip_parse_sources_and_blocks(void *log_ctx, const char *buf, IPSourceFilters *filters, int parse_include_list)
  113. {
  114. int ret;
  115. if (parse_include_list)
  116. ret = ip_parse_addr_list(log_ctx, buf, &filters->include_addrs, &filters->nb_include_addrs);
  117. else
  118. ret = ip_parse_addr_list(log_ctx, buf, &filters->exclude_addrs, &filters->nb_exclude_addrs);
  119. if (ret >= 0 && filters->nb_include_addrs && filters->nb_exclude_addrs) {
  120. av_log(log_ctx, AV_LOG_ERROR, "Simultaneously including and excluding sources is not supported.\n");
  121. return AVERROR(EINVAL);
  122. }
  123. return ret;
  124. }
  125. int ff_ip_parse_sources(void *log_ctx, const char *buf, IPSourceFilters *filters)
  126. {
  127. return ip_parse_sources_and_blocks(log_ctx, buf, filters, 1);
  128. }
  129. int ff_ip_parse_blocks(void *log_ctx, const char *buf, IPSourceFilters *filters)
  130. {
  131. return ip_parse_sources_and_blocks(log_ctx, buf, filters, 0);
  132. }
  133. void ff_ip_reset_filters(IPSourceFilters *filters)
  134. {
  135. av_freep(&filters->exclude_addrs);
  136. av_freep(&filters->include_addrs);
  137. filters->nb_include_addrs = 0;
  138. filters->nb_exclude_addrs = 0;
  139. }