FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ip.c
Go to the documentation of this file.
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 
21 #include "ip.h"
22 #include "libavutil/avstring.h"
23 
24 static int compare_addr(const struct sockaddr_storage *a,
25  const struct sockaddr_storage *b)
26 {
27  if (a->ss_family != b->ss_family)
28  return 1;
29  if (a->ss_family == AF_INET) {
30  return (((const struct sockaddr_in *)a)->sin_addr.s_addr !=
31  ((const struct sockaddr_in *)b)->sin_addr.s_addr);
32  }
33 
34 #if HAVE_STRUCT_SOCKADDR_IN6
35  if (a->ss_family == AF_INET6) {
36  const uint8_t *s6_addr_a = ((const struct sockaddr_in6 *)a)->sin6_addr.s6_addr;
37  const uint8_t *s6_addr_b = ((const struct sockaddr_in6 *)b)->sin6_addr.s6_addr;
38  return memcmp(s6_addr_a, s6_addr_b, 16);
39  }
40 #endif
41  return 1;
42 }
43 
45 {
46  int i;
47  if (s->nb_exclude_addrs) {
48  for (i = 0; i < s->nb_exclude_addrs; i++) {
49  if (!compare_addr(source_addr_ptr, &s->exclude_addrs[i]))
50  return 1;
51  }
52  }
53  if (s->nb_include_addrs) {
54  for (i = 0; i < s->nb_include_addrs; i++) {
55  if (!compare_addr(source_addr_ptr, &s->include_addrs[i]))
56  return 0;
57  }
58  return 1;
59  }
60  return 0;
61 }
62 
63 struct addrinfo *ff_ip_resolve_host(void *log_ctx,
64  const char *hostname, int port,
65  int type, int family, int flags)
66 {
67  struct addrinfo hints = { 0 }, *res = 0;
68  int error;
69  char sport[16];
70  const char *node = 0, *service = "0";
71 
72  if (port > 0) {
73  snprintf(sport, sizeof(sport), "%d", port);
74  service = sport;
75  }
76  if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
77  node = hostname;
78  }
79  hints.ai_socktype = type;
80  hints.ai_family = family;
81  hints.ai_flags = flags;
82  if ((error = getaddrinfo(node, service, &hints, &res))) {
83  res = NULL;
84  av_log(log_ctx, AV_LOG_ERROR, "getaddrinfo(%s, %s): %s\n",
85  node ? node : "unknown",
86  service,
87  gai_strerror(error));
88  }
89 
90  return res;
91 }
92 
93 
94 static int ip_parse_addr_list(void *log_ctx, const char *buf,
95  struct sockaddr_storage **address_list_ptr,
96  int *address_list_size_ptr)
97 {
98  struct addrinfo *ai = NULL;
99 
100  /* Resolve all of the IPs */
101 
102  while (buf && buf[0]) {
103  char* host = av_get_token(&buf, ",");
104  if (!host)
105  return AVERROR(ENOMEM);
106 
107  ai = ff_ip_resolve_host(log_ctx, host, 0, SOCK_DGRAM, AF_UNSPEC, 0);
108  av_freep(&host);
109 
110  if (ai) {
111  struct sockaddr_storage source_addr = {0};
112  memcpy(&source_addr, ai->ai_addr, ai->ai_addrlen);
113  freeaddrinfo(ai);
114  av_dynarray2_add((void **)address_list_ptr, address_list_size_ptr, sizeof(source_addr), (uint8_t *)&source_addr);
115  if (!*address_list_ptr)
116  return AVERROR(ENOMEM);
117  } else {
118  return AVERROR(EINVAL);
119  }
120 
121  if (*buf)
122  buf++;
123  }
124 
125  return 0;
126 }
127 
128 static int ip_parse_sources_and_blocks(void *log_ctx, const char *buf, IPSourceFilters *filters, int parse_include_list)
129 {
130  int ret;
131  if (parse_include_list)
132  ret = ip_parse_addr_list(log_ctx, buf, &filters->include_addrs, &filters->nb_include_addrs);
133  else
134  ret = ip_parse_addr_list(log_ctx, buf, &filters->exclude_addrs, &filters->nb_exclude_addrs);
135 
136  if (ret >= 0 && filters->nb_include_addrs && filters->nb_exclude_addrs) {
137  av_log(log_ctx, AV_LOG_ERROR, "Simultaneously including and excluding sources is not supported.\n");
138  return AVERROR(EINVAL);
139  }
140  return ret;
141 }
142 
143 int ff_ip_parse_sources(void *log_ctx, const char *buf, IPSourceFilters *filters)
144 {
145  return ip_parse_sources_and_blocks(log_ctx, buf, filters, 1);
146 }
147 
148 int ff_ip_parse_blocks(void *log_ctx, const char *buf, IPSourceFilters *filters)
149 {
150  return ip_parse_sources_and_blocks(log_ctx, buf, filters, 0);
151 }
152 
154 {
155  av_freep(&filters->exclude_addrs);
156  av_freep(&filters->include_addrs);
157  filters->nb_include_addrs = 0;
158  filters->nb_exclude_addrs = 0;
159 }
static int compare_addr(const struct sockaddr_storage *a, const struct sockaddr_storage *b)
Definition: ip.c:24
#define NULL
Definition: coverity.c:32
const char * b
Definition: vf_curves.c:116
int ff_ip_parse_blocks(void *log_ctx, const char *buf, IPSourceFilters *filters)
Parses the address[,address] source block list in buf and adds it to the filters in the IPSourceFilte...
Definition: ip.c:148
struct sockaddr_storage * exclude_addrs
Definition: ip.h:33
#define freeaddrinfo
Definition: network.h:215
int ff_ip_parse_sources(void *log_ctx, const char *buf, IPSourceFilters *filters)
Parses the address[,address] source list in buf and adds it to the filters in the IPSourceFilters str...
Definition: ip.c:143
uint8_t
uint16_t ss_family
Definition: network.h:113
int nb_include_addrs
Definition: ip.h:30
struct sockaddr_storage * include_addrs
Definition: ip.h:32
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
void ff_ip_reset_filters(IPSourceFilters *filters)
Resets the IP filter list and frees the internal fields of an IPSourceFilters structure.
Definition: ip.c:153
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition: avstring.c:149
int ai_addrlen
Definition: network.h:139
static int ip_parse_addr_list(void *log_ctx, const char *buf, struct sockaddr_storage **address_list_ptr, int *address_list_size_ptr)
Definition: ip.c:94
#define s(width, name)
Definition: cbs_vp9.c:257
static void error(const char *err)
void * av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, const uint8_t *elem_data)
Add an element of size elem_size to a dynamic array.
Definition: mem.c:322
void * buf
Definition: avisynth_c.h:690
GLint GLenum type
Definition: opengl_enc.c:105
int ff_ip_check_source_lists(struct sockaddr_storage *source_addr_ptr, IPSourceFilters *s)
Checks the source address against a given IP source filter.
Definition: ip.c:44
#define gai_strerror
Definition: network.h:222
#define snprintf
Definition: snprintf.h:34
int ai_socktype
Definition: network.h:137
#define flags(name, subs,...)
Definition: cbs_av1.c:596
#define getaddrinfo
Definition: network.h:214
int nb_exclude_addrs
Definition: ip.h:31
static int ip_parse_sources_and_blocks(void *log_ctx, const char *buf, IPSourceFilters *filters, int parse_include_list)
Definition: ip.c:128
static const struct PPFilter filters[]
Definition: postprocess.c:134
struct addrinfo * ff_ip_resolve_host(void *log_ctx, const char *hostname, int port, int type, int family, int flags)
Resolves hostname into an addrinfo structure.
Definition: ip.c:63
int ai_flags
Definition: network.h:135
#define av_freep(p)
struct sockaddr * ai_addr
Definition: network.h:140
Structure for storing IP (UDP) source filters or block lists.
Definition: ip.h:29
int ai_family
Definition: network.h:136