FFmpeg
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 <string.h>
22 #include "ip.h"
23 #include "libavutil/avstring.h"
24 #include "libavutil/mem.h"
25 
26 static int compare_addr(const struct sockaddr_storage *a,
27  const struct sockaddr_storage *b)
28 {
29  if (a->ss_family != b->ss_family)
30  return 1;
31  if (a->ss_family == AF_INET) {
32  return (((const struct sockaddr_in *)a)->sin_addr.s_addr !=
33  ((const struct sockaddr_in *)b)->sin_addr.s_addr);
34  }
35 
36 #if HAVE_STRUCT_SOCKADDR_IN6
37  if (a->ss_family == AF_INET6) {
38  const uint8_t *s6_addr_a = ((const struct sockaddr_in6 *)a)->sin6_addr.s6_addr;
39  const uint8_t *s6_addr_b = ((const struct sockaddr_in6 *)b)->sin6_addr.s6_addr;
40  return memcmp(s6_addr_a, s6_addr_b, 16);
41  }
42 #endif
43  return 1;
44 }
45 
47 {
48  int i;
49  if (s->nb_exclude_addrs) {
50  for (i = 0; i < s->nb_exclude_addrs; i++) {
51  if (!compare_addr(source_addr_ptr, &s->exclude_addrs[i]))
52  return 1;
53  }
54  }
55  if (s->nb_include_addrs) {
56  for (i = 0; i < s->nb_include_addrs; i++) {
57  if (!compare_addr(source_addr_ptr, &s->include_addrs[i]))
58  return 0;
59  }
60  return 1;
61  }
62  return 0;
63 }
64 
65 struct addrinfo *ff_ip_resolve_host(void *log_ctx,
66  const char *hostname, int port,
67  int type, int family, int flags)
68 {
69  struct addrinfo hints = { 0 }, *res = 0;
70  int error;
71  char sport[16];
72  const char *node = 0, *service = "0";
73 
74  if (port > 0) {
75  snprintf(sport, sizeof(sport), "%d", port);
76  service = sport;
77  }
78  if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
79  node = hostname;
80  }
81  hints.ai_socktype = type;
82  hints.ai_family = family;
83  hints.ai_flags = flags;
84  if ((error = getaddrinfo(node, service, &hints, &res))) {
85  res = NULL;
86  av_log(log_ctx, AV_LOG_ERROR, "getaddrinfo(%s, %s): %s\n",
87  node ? node : "unknown",
88  service,
90  }
91 
92  return res;
93 }
94 
95 
96 static int ip_parse_addr_list(void *log_ctx, const char *buf,
97  struct sockaddr_storage **address_list_ptr,
98  int *address_list_size_ptr)
99 {
100  struct addrinfo *ai = NULL;
101 
102  /* Resolve all of the IPs */
103 
104  while (buf && buf[0]) {
105  char* host = av_get_token(&buf, ",");
106  if (!host)
107  return AVERROR(ENOMEM);
108 
109  ai = ff_ip_resolve_host(log_ctx, host, 0, SOCK_DGRAM, AF_UNSPEC, 0);
110  av_freep(&host);
111 
112  if (ai) {
113  struct sockaddr_storage source_addr = {0};
114  memcpy(&source_addr, ai->ai_addr, ai->ai_addrlen);
115  freeaddrinfo(ai);
116  av_dynarray2_add((void **)address_list_ptr, address_list_size_ptr, sizeof(source_addr), (uint8_t *)&source_addr);
117  if (!*address_list_ptr)
118  return AVERROR(ENOMEM);
119  } else {
120  return AVERROR(EINVAL);
121  }
122 
123  if (*buf)
124  buf++;
125  }
126 
127  return 0;
128 }
129 
130 static int ip_parse_sources_and_blocks(void *log_ctx, const char *buf, IPSourceFilters *filters, int parse_include_list)
131 {
132  int ret;
133  if (parse_include_list)
134  ret = ip_parse_addr_list(log_ctx, buf, &filters->include_addrs, &filters->nb_include_addrs);
135  else
136  ret = ip_parse_addr_list(log_ctx, buf, &filters->exclude_addrs, &filters->nb_exclude_addrs);
137 
138  if (ret >= 0 && filters->nb_include_addrs && filters->nb_exclude_addrs) {
139  av_log(log_ctx, AV_LOG_ERROR, "Simultaneously including and excluding sources is not supported.\n");
140  return AVERROR(EINVAL);
141  }
142  return ret;
143 }
144 
145 int ff_ip_parse_sources(void *log_ctx, const char *buf, IPSourceFilters *filters)
146 {
147  return ip_parse_sources_and_blocks(log_ctx, buf, filters, 1);
148 }
149 
150 int ff_ip_parse_blocks(void *log_ctx, const char *buf, IPSourceFilters *filters)
151 {
152  return ip_parse_sources_and_blocks(log_ctx, buf, filters, 0);
153 }
154 
156 {
157  av_freep(&filters->exclude_addrs);
158  av_freep(&filters->include_addrs);
159  filters->nb_include_addrs = 0;
160  filters->nb_exclude_addrs = 0;
161 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
ip_parse_addr_list
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:96
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ff_ip_parse_sources
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:145
av_dynarray2_add
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:341
b
#define b
Definition: input.c:41
sockaddr_storage
Definition: network.h:111
freeaddrinfo
#define freeaddrinfo
Definition: network.h:218
ff_ip_reset_filters
void ff_ip_reset_filters(IPSourceFilters *filters)
Resets the IP filter list and frees the internal fields of an IPSourceFilters structure.
Definition: ip.c:155
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
ip_parse_sources_and_blocks
static int ip_parse_sources_and_blocks(void *log_ctx, const char *buf, IPSourceFilters *filters, int parse_include_list)
Definition: ip.c:130
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
s
#define s(width, name)
Definition: cbs_vp9.c:256
filters
#define filters(fmt, type, inverse, clp, inverset, clip, one, clip_fn, packed)
Definition: af_crystalizer.c:55
compare_addr
static int compare_addr(const struct sockaddr_storage *a, const struct sockaddr_storage *b)
Definition: ip.c:26
NULL
#define NULL
Definition: coverity.c:32
ff_ip_parse_blocks
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:150
addrinfo::ai_addr
struct sockaddr * ai_addr
Definition: network.h:143
ff_ip_resolve_host
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:65
addrinfo::ai_family
int ai_family
Definition: network.h:139
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
gai_strerror
#define gai_strerror
Definition: network.h:225
addrinfo::ai_addrlen
int ai_addrlen
Definition: network.h:142
getaddrinfo
#define getaddrinfo
Definition: network.h:217
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
ip.h
ret
ret
Definition: filter_design.txt:187
addrinfo::ai_socktype
int ai_socktype
Definition: network.h:140
addrinfo::ai_flags
int ai_flags
Definition: network.h:138
IPSourceFilters
Structure for storing IP (UDP) source filters or block lists.
Definition: ip.h:29
av_get_token
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:144
ff_ip_check_source_lists
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:46
mem.h
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
avstring.h
addrinfo
Definition: network.h:137
snprintf
#define snprintf
Definition: snprintf.h:34