/src/ffmpeg/libavformat/ip.c
Line | Count | Source |
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 | 0 | { |
29 | 0 | if (a->ss_family != b->ss_family) |
30 | 0 | return 1; |
31 | 0 | if (a->ss_family == AF_INET) { |
32 | 0 | return (((const struct sockaddr_in *)a)->sin_addr.s_addr != |
33 | 0 | ((const struct sockaddr_in *)b)->sin_addr.s_addr); |
34 | 0 | } |
35 | | |
36 | 0 | #if HAVE_STRUCT_SOCKADDR_IN6 |
37 | 0 | if (a->ss_family == AF_INET6) { |
38 | 0 | const uint8_t *s6_addr_a = ((const struct sockaddr_in6 *)a)->sin6_addr.s6_addr; |
39 | 0 | const uint8_t *s6_addr_b = ((const struct sockaddr_in6 *)b)->sin6_addr.s6_addr; |
40 | 0 | return memcmp(s6_addr_a, s6_addr_b, 16); |
41 | 0 | } |
42 | 0 | #endif |
43 | 0 | return 1; |
44 | 0 | } |
45 | | |
46 | | int ff_ip_check_source_lists(struct sockaddr_storage *source_addr_ptr, IPSourceFilters *s) |
47 | 0 | { |
48 | 0 | int i; |
49 | 0 | if (s->nb_exclude_addrs) { |
50 | 0 | for (i = 0; i < s->nb_exclude_addrs; i++) { |
51 | 0 | if (!compare_addr(source_addr_ptr, &s->exclude_addrs[i])) |
52 | 0 | return 1; |
53 | 0 | } |
54 | 0 | } |
55 | 0 | if (s->nb_include_addrs) { |
56 | 0 | for (i = 0; i < s->nb_include_addrs; i++) { |
57 | 0 | if (!compare_addr(source_addr_ptr, &s->include_addrs[i])) |
58 | 0 | return 0; |
59 | 0 | } |
60 | 0 | return 1; |
61 | 0 | } |
62 | 0 | return 0; |
63 | 0 | } |
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 | 0 | { |
69 | 0 | struct addrinfo hints = { 0 }, *res = 0; |
70 | 0 | int error; |
71 | 0 | char sport[16]; |
72 | 0 | const char *node = 0, *service = "0"; |
73 | |
|
74 | 0 | if (port > 0) { |
75 | 0 | snprintf(sport, sizeof(sport), "%d", port); |
76 | 0 | service = sport; |
77 | 0 | } |
78 | 0 | if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) { |
79 | 0 | node = hostname; |
80 | 0 | } |
81 | 0 | hints.ai_socktype = type; |
82 | 0 | hints.ai_family = family; |
83 | 0 | hints.ai_flags = flags; |
84 | 0 | if ((error = getaddrinfo(node, service, &hints, &res))) { |
85 | 0 | res = NULL; |
86 | 0 | av_log(log_ctx, AV_LOG_ERROR, "getaddrinfo(%s, %s): %s\n", |
87 | 0 | node ? node : "unknown", |
88 | 0 | service, |
89 | 0 | gai_strerror(error)); |
90 | 0 | } |
91 | |
|
92 | 0 | return res; |
93 | 0 | } |
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 | 0 | { |
100 | 0 | struct addrinfo *ai = NULL; |
101 | | |
102 | | /* Resolve all of the IPs */ |
103 | |
|
104 | 0 | while (buf && buf[0]) { |
105 | 0 | char* host = av_get_token(&buf, ","); |
106 | 0 | if (!host) |
107 | 0 | return AVERROR(ENOMEM); |
108 | | |
109 | 0 | ai = ff_ip_resolve_host(log_ctx, host, 0, SOCK_DGRAM, AF_UNSPEC, 0); |
110 | 0 | av_freep(&host); |
111 | |
|
112 | 0 | if (ai) { |
113 | 0 | struct sockaddr_storage source_addr = {0}; |
114 | 0 | memcpy(&source_addr, ai->ai_addr, ai->ai_addrlen); |
115 | 0 | freeaddrinfo(ai); |
116 | 0 | av_dynarray2_add((void **)address_list_ptr, address_list_size_ptr, sizeof(source_addr), (uint8_t *)&source_addr); |
117 | 0 | if (!*address_list_ptr) |
118 | 0 | return AVERROR(ENOMEM); |
119 | 0 | } else { |
120 | 0 | return AVERROR(EINVAL); |
121 | 0 | } |
122 | | |
123 | 0 | if (*buf) |
124 | 0 | buf++; |
125 | 0 | } |
126 | | |
127 | 0 | return 0; |
128 | 0 | } |
129 | | |
130 | | static int ip_parse_sources_and_blocks(void *log_ctx, const char *buf, IPSourceFilters *filters, int parse_include_list) |
131 | 0 | { |
132 | 0 | int ret; |
133 | 0 | if (parse_include_list) |
134 | 0 | ret = ip_parse_addr_list(log_ctx, buf, &filters->include_addrs, &filters->nb_include_addrs); |
135 | 0 | else |
136 | 0 | ret = ip_parse_addr_list(log_ctx, buf, &filters->exclude_addrs, &filters->nb_exclude_addrs); |
137 | |
|
138 | 0 | if (ret >= 0 && filters->nb_include_addrs && filters->nb_exclude_addrs) { |
139 | 0 | av_log(log_ctx, AV_LOG_ERROR, "Simultaneously including and excluding sources is not supported.\n"); |
140 | 0 | return AVERROR(EINVAL); |
141 | 0 | } |
142 | 0 | return ret; |
143 | 0 | } |
144 | | |
145 | | int ff_ip_parse_sources(void *log_ctx, const char *buf, IPSourceFilters *filters) |
146 | 0 | { |
147 | 0 | return ip_parse_sources_and_blocks(log_ctx, buf, filters, 1); |
148 | 0 | } |
149 | | |
150 | | int ff_ip_parse_blocks(void *log_ctx, const char *buf, IPSourceFilters *filters) |
151 | 0 | { |
152 | 0 | return ip_parse_sources_and_blocks(log_ctx, buf, filters, 0); |
153 | 0 | } |
154 | | |
155 | | void ff_ip_reset_filters(IPSourceFilters *filters) |
156 | 0 | { |
157 | 0 | av_freep(&filters->exclude_addrs); |
158 | 0 | av_freep(&filters->include_addrs); |
159 | 0 | filters->nb_include_addrs = 0; |
160 | 0 | filters->nb_exclude_addrs = 0; |
161 | 0 | } |