build: Add feature test macros for glibc 2.19+
[libav.git] / libavformat / udp.c
CommitLineData
de6d9b64
FB
1/*
2 * UDP prototype streaming system
406792e7 3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
de6d9b64 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
3adfb377
LB
21
22/**
ba87f080 23 * @file
3adfb377
LB
24 * UDP protocol
25 */
26
825e463a 27#define _DEFAULT_SOURCE
a9e02947 28#define _BSD_SOURCE /* Needed for using struct ip_mreq with recent glibc */
046f081b 29
8be1c656 30#include "avformat.h"
4ec153bb 31#include "avio_internal.h"
ab0287fc 32#include "libavutil/parseutils.h"
65ac5137 33#include "libavutil/avstring.h"
66028b7b 34#include "libavutil/opt.h"
e4a9e3cc 35#include "internal.h"
42572ef5 36#include "network.h"
087b3272 37#include "os_support.h"
5cec8971 38#include "url.h"
de6d9b64 39
9c633e9a
AB
40#ifndef IPV6_ADD_MEMBERSHIP
41#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
42#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
43#endif
44
daf8cf35 45typedef struct UDPContext {
8dc72eea 46 const AVClass *class;
2d225591
FB
47 int udp_fd;
48 int ttl;
236bb1ab 49 int buffer_size;
66028b7b 50 int pkt_size;
2d225591
FB
51 int is_multicast;
52 int local_port;
34ecc397 53 int reuse_socket;
7a91333f 54 struct sockaddr_storage dest_addr;
397db8ac 55 int dest_addr_len;
6766a354 56 int is_connected;
66028b7b
LB
57 char *localaddr;
58 char *sources;
59 char *block;
de6d9b64
FB
60} UDPContext;
61
62#define UDP_TX_BUF_SIZE 32768
f18cae4d 63#define UDP_MAX_PKT_SIZE 65536
de6d9b64 64
66028b7b
LB
65#define OFFSET(x) offsetof(UDPContext, x)
66#define D AV_OPT_FLAG_DECODING_PARAM
67#define E AV_OPT_FLAG_ENCODING_PARAM
68static const AVOption options[] = {
69 { "ttl", "Time to live (in milliseconds, multicast only)", OFFSET(ttl), AV_OPT_TYPE_INT, { .i64 = 16 }, 0, INT_MAX, .flags = D|E },
70 { "buffer_size", "System data size (in bytes)", OFFSET(buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
71 { "local_port", "Local port", OFFSET(local_port), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
72 { "reuse_socket", "Reuse socket", OFFSET(reuse_socket), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, .flags = D|E },
73 { "connect", "Connect socket", OFFSET(is_connected), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = D|E },
74 { "pkt_size", "Maximum packet size", OFFSET(pkt_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
75 { "localaddr", "Local address", OFFSET(localaddr), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
76 { "sources", "Source list", OFFSET(sources), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
77 { "block", "Block list", OFFSET(block), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E },
78 { NULL }
79};
80
81static const AVClass udp_class = {
82 .class_name = "udp",
83 .item_name = av_default_item_name,
84 .option = options,
85 .version = LIBAVUTIL_VERSION_INT,
86};
87
a840cdda
MS
88static void log_net_error(void *ctx, int level, const char* prefix)
89{
90 char errbuf[100];
91 av_strerror(ff_neterrno(), errbuf, sizeof(errbuf));
92 av_log(ctx, level, "%s: %s\n", prefix, errbuf);
93}
94
64810754
RB
95static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
96 struct sockaddr *addr)
97{
a8bde059 98#ifdef IP_MULTICAST_TTL
7a91333f
HZ
99 if (addr->sa_family == AF_INET) {
100 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
a840cdda 101 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL)");
7a91333f
HZ
102 return -1;
103 }
104 }
a8bde059 105#endif
b4d68544 106#if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
7a91333f
HZ
107 if (addr->sa_family == AF_INET6) {
108 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
a840cdda 109 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS)");
7a91333f
HZ
110 return -1;
111 }
112 }
a8bde059 113#endif
7a91333f
HZ
114 return 0;
115}
116
64810754
RB
117static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
118{
a8bde059 119#ifdef IP_ADD_MEMBERSHIP
7a91333f 120 if (addr->sa_family == AF_INET) {
a8bde059
LA
121 struct ip_mreq mreq;
122
7a91333f
HZ
123 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
124 mreq.imr_interface.s_addr= INADDR_ANY;
125 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
a840cdda 126 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP)");
7a91333f
HZ
127 return -1;
128 }
129 }
a8bde059 130#endif
63206a8f 131#if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
7a91333f 132 if (addr->sa_family == AF_INET6) {
a8bde059
LA
133 struct ipv6_mreq mreq6;
134
7a91333f
HZ
135 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
136 mreq6.ipv6mr_interface= 0;
137 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
a840cdda 138 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP)");
7a91333f
HZ
139 return -1;
140 }
141 }
a8bde059 142#endif
7a91333f
HZ
143 return 0;
144}
145
64810754
RB
146static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
147{
a8bde059 148#ifdef IP_DROP_MEMBERSHIP
7a91333f 149 if (addr->sa_family == AF_INET) {
a8bde059
LA
150 struct ip_mreq mreq;
151
7a91333f
HZ
152 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
153 mreq.imr_interface.s_addr= INADDR_ANY;
154 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
a840cdda 155 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP)");
7a91333f
HZ
156 return -1;
157 }
158 }
a8bde059 159#endif
63206a8f 160#if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
7a91333f 161 if (addr->sa_family == AF_INET6) {
a8bde059
LA
162 struct ipv6_mreq mreq6;
163
7a91333f
HZ
164 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
165 mreq6.ipv6mr_interface= 0;
166 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
a840cdda 167 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP)");
7a91333f
HZ
168 return -1;
169 }
170 }
a8bde059 171#endif
7a91333f
HZ
172 return 0;
173}
174
34af7813
LB
175static struct addrinfo *udp_resolve_host(URLContext *h,
176 const char *hostname, int port,
64810754
RB
177 int type, int family, int flags)
178{
a92be9b8 179 struct addrinfo hints = { 0 }, *res = 0;
7a91333f
HZ
180 int error;
181 char sport[16];
d607861c 182 const char *node = 0, *service = "0";
7a91333f
HZ
183
184 if (port > 0) {
2fc8ea24 185 snprintf(sport, sizeof(sport), "%d", port);
7a91333f
HZ
186 service = sport;
187 }
188 if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
189 node = hostname;
190 }
7d8576c2
LA
191 hints.ai_socktype = type;
192 hints.ai_family = family;
193 hints.ai_flags = flags;
194 if ((error = getaddrinfo(node, service, &hints, &res))) {
11a74f36 195 res = NULL;
12b14382
LB
196 av_log(h, AV_LOG_ERROR, "getaddrinfo(%s, %s): %s\n",
197 node ? node : "unknown",
198 service ? service : "unknown",
34af7813 199 gai_strerror(error));
7d8576c2
LA
200 }
201
7a91333f
HZ
202 return res;
203}
204
34af7813
LB
205static int udp_set_multicast_sources(URLContext *h,
206 int sockfd, struct sockaddr *addr,
75d339e0
MS
207 int addr_len, char **sources,
208 int nb_sources, int include)
209{
210#if HAVE_STRUCT_GROUP_SOURCE_REQ && defined(MCAST_BLOCK_SOURCE) && !defined(_WIN32)
211 /* These ones are available in the microsoft SDK, but don't seem to work
212 * as on linux, so just prefer the v4-only approach there for now. */
213 int i;
214 for (i = 0; i < nb_sources; i++) {
215 struct group_source_req mreqs;
216 int level = addr->sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
34af7813 217 struct addrinfo *sourceaddr = udp_resolve_host(h, sources[i], 0,
75d339e0 218 SOCK_DGRAM, AF_UNSPEC,
3357bccc 219 0);
75d339e0
MS
220 if (!sourceaddr)
221 return AVERROR(ENOENT);
222
223 mreqs.gsr_interface = 0;
224 memcpy(&mreqs.gsr_group, addr, addr_len);
225 memcpy(&mreqs.gsr_source, sourceaddr->ai_addr, sourceaddr->ai_addrlen);
226 freeaddrinfo(sourceaddr);
227
228 if (setsockopt(sockfd, level,
229 include ? MCAST_JOIN_SOURCE_GROUP : MCAST_BLOCK_SOURCE,
230 (const void *)&mreqs, sizeof(mreqs)) < 0) {
231 if (include)
232 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_JOIN_SOURCE_GROUP)");
233 else
234 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_BLOCK_SOURCE)");
235 return ff_neterrno();
236 }
237 }
238#elif HAVE_STRUCT_IP_MREQ_SOURCE && defined(IP_BLOCK_SOURCE)
239 int i;
240 if (addr->sa_family != AF_INET) {
241 av_log(NULL, AV_LOG_ERROR,
242 "Setting multicast sources only supported for IPv4\n");
243 return AVERROR(EINVAL);
244 }
245 for (i = 0; i < nb_sources; i++) {
246 struct ip_mreq_source mreqs;
34af7813 247 struct addrinfo *sourceaddr = udp_resolve_host(h, sources[i], 0,
75d339e0 248 SOCK_DGRAM, AF_UNSPEC,
3357bccc 249 0);
75d339e0
MS
250 if (!sourceaddr)
251 return AVERROR(ENOENT);
252 if (sourceaddr->ai_addr->sa_family != AF_INET) {
253 freeaddrinfo(sourceaddr);
254 av_log(NULL, AV_LOG_ERROR, "%s is of incorrect protocol family\n",
255 sources[i]);
256 return AVERROR(EINVAL);
257 }
258
259 mreqs.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
260 mreqs.imr_interface.s_addr = INADDR_ANY;
261 mreqs.imr_sourceaddr.s_addr = ((struct sockaddr_in *)sourceaddr->ai_addr)->sin_addr.s_addr;
262 freeaddrinfo(sourceaddr);
263
264 if (setsockopt(sockfd, IPPROTO_IP,
265 include ? IP_ADD_SOURCE_MEMBERSHIP : IP_BLOCK_SOURCE,
266 (const void *)&mreqs, sizeof(mreqs)) < 0) {
267 if (include)
268 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_SOURCE_MEMBERSHIP)");
269 else
270 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_BLOCK_SOURCE)");
271 return ff_neterrno();
272 }
273 }
274#else
275 return AVERROR(ENOSYS);
276#endif
277 return 0;
278}
34af7813
LB
279static int udp_set_url(URLContext *h,
280 struct sockaddr_storage *addr,
64810754
RB
281 const char *hostname, int port)
282{
7a91333f 283 struct addrinfo *res0;
d05cb726
LA
284 int addr_len;
285
34af7813 286 res0 = udp_resolve_host(h, hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
6f3e0b21 287 if (res0 == 0) return AVERROR(EIO);
d05cb726
LA
288 memcpy(addr, res0->ai_addr, res0->ai_addrlen);
289 addr_len = res0->ai_addrlen;
7a91333f 290 freeaddrinfo(res0);
d05cb726
LA
291
292 return addr_len;
7a91333f
HZ
293}
294
34af7813 295static int udp_socket_create(URLContext *h, struct sockaddr_storage *addr,
be2efe0c 296 socklen_t *addr_len, const char *localaddr)
aa519c47 297{
34af7813 298 UDPContext *s = h->priv_data;
7a91333f 299 int udp_fd = -1;
276358c1 300 struct addrinfo *res0 = NULL, *res = NULL;
51844e6c 301 int family = AF_UNSPEC;
115329f1 302
51844e6c
RB
303 if (((struct sockaddr *) &s->dest_addr)->sa_family)
304 family = ((struct sockaddr *) &s->dest_addr)->sa_family;
34af7813
LB
305 res0 = udp_resolve_host(h, (localaddr && localaddr[0]) ? localaddr : NULL,
306 s->local_port,
65ac5137 307 SOCK_DGRAM, family, AI_PASSIVE);
7d8576c2
LA
308 if (res0 == 0)
309 goto fail;
310 for (res = res0; res; res=res->ai_next) {
bb937825 311 udp_fd = ff_socket(res->ai_family, SOCK_DGRAM, 0);
042b272a 312 if (udp_fd != -1) break;
a840cdda 313 log_net_error(NULL, AV_LOG_ERROR, "socket");
7d8576c2 314 }
276358c1
HZ
315
316 if (udp_fd < 0)
7a91333f 317 goto fail;
115329f1 318
aa519c47
LA
319 memcpy(addr, res->ai_addr, res->ai_addrlen);
320 *addr_len = res->ai_addrlen;
7a91333f 321
aa519c47 322 freeaddrinfo(res0);
115329f1 323
7a91333f 324 return udp_fd;
115329f1 325
7a91333f
HZ
326 fail:
327 if (udp_fd >= 0)
7a91333f 328 closesocket(udp_fd);
88730be6
MR
329 if(res0)
330 freeaddrinfo(res0);
7a91333f
HZ
331 return -1;
332}
333
aa519c47
LA
334static int udp_port(struct sockaddr_storage *addr, int addr_len)
335{
03c09e43 336 char sbuf[sizeof(int)*3+1];
860b40f1 337 int error;
aa519c47 338
860b40f1
MS
339 if ((error = getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0, sbuf, sizeof(sbuf), NI_NUMERICSERV)) != 0) {
340 av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", gai_strerror(error));
aa519c47
LA
341 return -1;
342 }
343
344 return strtol(sbuf, NULL, 10);
345}
346
7a91333f 347
2d225591
FB
348/**
349 * If no filename is given to av_open_input_file because you want to
350 * get the local port first, then you must call this function to set
351 * the remote server address.
352 *
353 * url syntax: udp://host:port[?option=val...]
35b74c3d 354 * option: 'ttl=n' : set the ttl value (for multicast only)
2d225591 355 * 'localport=n' : set the local port
9899efb4 356 * 'pkt_size=n' : set max packet size
34ecc397 357 * 'reuse=1' : enable reusing the socket
2d225591 358 *
9a58234f 359 * @param h media file context
2d225591
FB
360 * @param uri of the remote server
361 * @return zero if no error.
362 */
4ec153bb 363int ff_udp_set_remote_url(URLContext *h, const char *uri)
2d225591
FB
364{
365 UDPContext *s = h->priv_data;
21a569f3 366 char hostname[256], buf[10];
2d225591 367 int port;
21a569f3 368 const char *p;
115329f1 369
f3bfe388 370 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
de6d9b64 371
2d225591 372 /* set the destination address */
34af7813 373 s->dest_addr_len = udp_set_url(h, &s->dest_addr, hostname, port);
d05cb726 374 if (s->dest_addr_len < 0) {
6f3e0b21 375 return AVERROR(EIO);
d05cb726 376 }
55b6bd1c 377 s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
21a569f3
MS
378 p = strchr(uri, '?');
379 if (p) {
ab0287fc 380 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
21a569f3
MS
381 int was_connected = s->is_connected;
382 s->is_connected = strtol(buf, NULL, 10);
383 if (s->is_connected && !was_connected) {
384 if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
385 s->dest_addr_len)) {
386 s->is_connected = 0;
a840cdda 387 log_net_error(h, AV_LOG_ERROR, "connect");
21a569f3
MS
388 return AVERROR(EIO);
389 }
390 }
391 }
392 }
d05cb726 393
2d225591
FB
394 return 0;
395}
396
397/**
48e77473 398 * Return the local port used by the UDP connection
9a58234f 399 * @param h media file context
2d225591
FB
400 * @return the local port number
401 */
4ec153bb 402int ff_udp_get_local_port(URLContext *h)
2d225591
FB
403{
404 UDPContext *s = h->priv_data;
405 return s->local_port;
406}
407
408/**
409 * Return the udp file handle for select() usage to wait for several RTP
410 * streams at the same time.
411 * @param h media file context
412 */
c4efa7c2 413static int udp_get_file_handle(URLContext *h)
2d225591
FB
414{
415 UDPContext *s = h->priv_data;
416 return s->udp_fd;
417}
418
7d99c929
ET
419static int parse_source_list(char *buf, char **sources, int *num_sources,
420 int max_sources)
421{
422 char *source_start;
423
424 source_start = buf;
425 while (1) {
426 char *next = strchr(source_start, ',');
427 if (next)
428 *next = '\0';
429 sources[*num_sources] = av_strdup(source_start);
430 if (!sources[*num_sources])
431 return AVERROR(ENOMEM);
432 source_start = next + 1;
433 (*num_sources)++;
434 if (*num_sources >= max_sources || !next)
435 break;
436 }
437 return 0;
438}
439
2d225591 440/* put it in UDP context */
de6d9b64
FB
441/* return non zero if error */
442static int udp_open(URLContext *h, const char *uri, int flags)
443{
65ac5137 444 char hostname[1024], localaddr[1024] = "";
36b53281 445 int port, udp_fd = -1, tmp, bind_ret = -1;
7e580505 446 UDPContext *s = h->priv_data;
88730be6 447 int is_output;
2d225591
FB
448 const char *p;
449 char buf[256];
aa519c47 450 struct sockaddr_storage my_addr;
be2efe0c 451 socklen_t len;
7d99c929
ET
452 int i, num_include_sources = 0, num_exclude_sources = 0;
453 char *include_sources[32], *exclude_sources[32];
de6d9b64
FB
454
455 h->is_streamed = 1;
9899efb4 456 h->max_packet_size = 1472;
de6d9b64 457
a7ea5e3d 458 is_output = !(flags & AVIO_FLAG_READ);
115329f1 459
66028b7b
LB
460 if (s->buffer_size < 0)
461 s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
462
463 if (s->sources) {
464 if (parse_source_list(s->sources, include_sources,
465 &num_include_sources,
466 FF_ARRAY_ELEMS(include_sources)))
467 goto fail;
468 }
469
470 if (s->block) {
471 if (parse_source_list(s->block, exclude_sources, &num_exclude_sources,
472 FF_ARRAY_ELEMS(exclude_sources)))
473 goto fail;
474 }
475
ff7adead 476 if (s->pkt_size > 0)
66028b7b 477 h->max_packet_size = s->pkt_size;
236bb1ab 478
2d225591
FB
479 p = strchr(uri, '?');
480 if (p) {
ab0287fc 481 if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
5d62d0b1 482 char *endptr = NULL;
00952be4
DF
483 s->reuse_socket = strtol(buf, &endptr, 10);
484 /* assume if no digits were found it is a request to enable it */
485 if (buf == endptr)
486 s->reuse_socket = 1;
00952be4 487 }
ab0287fc 488 if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
2d225591
FB
489 s->ttl = strtol(buf, NULL, 10);
490 }
ab0287fc 491 if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
2d225591
FB
492 s->local_port = strtol(buf, NULL, 10);
493 }
ab0287fc 494 if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
9899efb4
MK
495 h->max_packet_size = strtol(buf, NULL, 10);
496 }
ab0287fc 497 if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
236bb1ab
JM
498 s->buffer_size = strtol(buf, NULL, 10);
499 }
ab0287fc 500 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
6766a354
MS
501 s->is_connected = strtol(buf, NULL, 10);
502 }
65ac5137
MS
503 if (av_find_info_tag(buf, sizeof(buf), "localaddr", p)) {
504 av_strlcpy(localaddr, buf, sizeof(localaddr));
505 }
7d99c929
ET
506 if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
507 if (parse_source_list(buf, include_sources, &num_include_sources,
508 FF_ARRAY_ELEMS(include_sources)))
509 goto fail;
510 }
511 if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
512 if (parse_source_list(buf, exclude_sources, &num_exclude_sources,
513 FF_ARRAY_ELEMS(exclude_sources)))
514 goto fail;
75d339e0 515 }
de6d9b64 516 }
2d225591
FB
517
518 /* fill the dest addr */
f3bfe388 519 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
115329f1 520
f3bfe388 521 /* XXX: fix av_url_split */
2d225591
FB
522 if (hostname[0] == '\0' || hostname[0] == '?') {
523 /* only accepts null hostname if input */
a7ea5e3d 524 if (!(flags & AVIO_FLAG_READ))
2d225591
FB
525 goto fail;
526 } else {
4ec153bb 527 if (ff_udp_set_remote_url(h, uri) < 0)
82505611 528 goto fail;
2d225591 529 }
de6d9b64 530
61dc9d64 531 if ((s->is_multicast || s->local_port < 0) && (h->flags & AVIO_FLAG_READ))
882d00f2 532 s->local_port = port;
66028b7b
LB
533
534 if (localaddr[0])
34af7813 535 udp_fd = udp_socket_create(h, &my_addr, &len, localaddr);
66028b7b 536 else
34af7813 537 udp_fd = udp_socket_create(h, &my_addr, &len, s->localaddr);
2d225591
FB
538 if (udp_fd < 0)
539 goto fail;
540
00952be4 541 /* Follow the requested reuse option, unless it's multicast in which
da9cea77 542 * case enable reuse unless explicitly disabled.
00952be4 543 */
66028b7b 544 if (s->reuse_socket > 0 || (s->is_multicast && s->reuse_socket < 0)) {
00952be4 545 s->reuse_socket = 1;
34ecc397
T
546 if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
547 goto fail;
00952be4 548 }
34ecc397 549
1b89bcdd
MS
550 /* If multicast, try binding the multicast address first, to avoid
551 * receiving UDP packets from other sources aimed at the same UDP
c700fdb0
MS
552 * port. This fails on windows. This makes sending to the same address
553 * using sendto() fail, so only do it if we're opened in read-only mode. */
554 if (s->is_multicast && !(h->flags & AVIO_FLAG_WRITE)) {
36b53281
JM
555 bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
556 }
557 /* bind to the local address if not multicast or if the multicast
558 * bind failed */
113d3e10 559 /* the bind is needed to give a port to the socket now */
a887c87c 560 if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) {
a840cdda 561 log_net_error(h, AV_LOG_ERROR, "bind failed");
de6d9b64 562 goto fail;
a887c87c 563 }
de6d9b64 564
d4936869
LA
565 len = sizeof(my_addr);
566 getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
aa519c47
LA
567 s->local_port = udp_port(&my_addr, len);
568
7a91333f 569 if (s->is_multicast) {
2bfd92b3 570 if (h->flags & AVIO_FLAG_WRITE) {
a8bde059 571 /* output */
9cdcb04f 572 if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
7a91333f 573 goto fail;
2bfd92b3
MS
574 }
575 if (h->flags & AVIO_FLAG_READ) {
a8bde059 576 /* input */
7d99c929
ET
577 if (num_include_sources && num_exclude_sources) {
578 av_log(h, AV_LOG_ERROR, "Simultaneously including and excluding multicast sources is not supported\n");
579 goto fail;
580 }
581 if (num_include_sources) {
34af7813
LB
582 if (udp_set_multicast_sources(h, udp_fd,
583 (struct sockaddr *)&s->dest_addr,
584 s->dest_addr_len,
585 include_sources,
586 num_include_sources, 1) < 0)
7d99c929
ET
587 goto fail;
588 } else {
75d339e0
MS
589 if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
590 goto fail;
7d99c929
ET
591 }
592 if (num_exclude_sources) {
34af7813
LB
593 if (udp_set_multicast_sources(h, udp_fd,
594 (struct sockaddr *)&s->dest_addr,
595 s->dest_addr_len,
596 exclude_sources,
597 num_exclude_sources, 0) < 0)
75d339e0 598 goto fail;
75d339e0 599 }
7a91333f
HZ
600 }
601 }
de6d9b64 602
2d225591
FB
603 if (is_output) {
604 /* limit the tx buf size to limit latency */
236bb1ab 605 tmp = s->buffer_size;
2d225591 606 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
a840cdda 607 log_net_error(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF)");
2d225591
FB
608 goto fail;
609 }
f18cae4d
RP
610 } else {
611 /* set udp recv buffer size to the largest possible udp packet size to
612 * avoid losing data on OSes that set this too low by default. */
236bb1ab
JM
613 tmp = s->buffer_size;
614 if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
a840cdda 615 log_net_error(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF)");
236bb1ab 616 }
ce09d560
JM
617 /* make the socket non-blocking */
618 ff_socket_nonblock(udp_fd, 1);
2d225591 619 }
6766a354
MS
620 if (s->is_connected) {
621 if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
a840cdda 622 log_net_error(h, AV_LOG_ERROR, "connect");
6766a354
MS
623 goto fail;
624 }
625 }
2d225591 626
7d99c929
ET
627 for (i = 0; i < num_include_sources; i++)
628 av_freep(&include_sources[i]);
629 for (i = 0; i < num_exclude_sources; i++)
630 av_freep(&exclude_sources[i]);
75d339e0 631
2d225591 632 s->udp_fd = udp_fd;
de6d9b64
FB
633 return 0;
634 fail:
2d225591 635 if (udp_fd >= 0)
9ddd71fc 636 closesocket(udp_fd);
7d99c929
ET
637 for (i = 0; i < num_include_sources; i++)
638 av_freep(&include_sources[i]);
639 for (i = 0; i < num_exclude_sources; i++)
640 av_freep(&exclude_sources[i]);
6f3e0b21 641 return AVERROR(EIO);
de6d9b64
FB
642}
643
0c1a9eda 644static int udp_read(URLContext *h, uint8_t *buf, int size)
de6d9b64
FB
645{
646 UDPContext *s = h->priv_data;
7d084299 647 int ret;
2d225591 648
f87b1b37 649 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
ebba2b3e
LB
650 ret = ff_network_wait_fd(s->udp_fd, 0);
651 if (ret < 0)
652 return ret;
2d225591 653 }
ebba2b3e
LB
654 ret = recv(s->udp_fd, buf, size, 0);
655 return ret < 0 ? ff_neterrno() : ret;
de6d9b64
FB
656}
657
27241cbf 658static int udp_write(URLContext *h, const uint8_t *buf, int size)
de6d9b64
FB
659{
660 UDPContext *s = h->priv_data;
2d225591
FB
661 int ret;
662
f87b1b37 663 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
ebba2b3e
LB
664 ret = ff_network_wait_fd(s->udp_fd, 1);
665 if (ret < 0)
666 return ret;
de6d9b64 667 }
ebba2b3e
LB
668
669 if (!s->is_connected) {
670 ret = sendto (s->udp_fd, buf, size, 0,
671 (struct sockaddr *) &s->dest_addr,
672 s->dest_addr_len);
673 } else
674 ret = send(s->udp_fd, buf, size, 0);
675
676 return ret < 0 ? ff_neterrno() : ret;
2d225591
FB
677}
678
679static int udp_close(URLContext *h)
680{
681 UDPContext *s = h->priv_data;
682
a7ea5e3d 683 if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
9cdcb04f 684 udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
9ddd71fc 685 closesocket(s->udp_fd);
2d225591 686 return 0;
de6d9b64
FB
687}
688
2758cded 689const URLProtocol ff_udp_protocol = {
f35ff97f
AK
690 .name = "udp",
691 .url_open = udp_open,
692 .url_read = udp_read,
693 .url_write = udp_write,
694 .url_close = udp_close,
f0a80394 695 .url_get_file_handle = udp_get_file_handle,
7e580505 696 .priv_data_size = sizeof(UDPContext),
32b83aee 697 .flags = URL_PROTOCOL_FLAG_NETWORK,
66028b7b 698 .priv_data_class = &udp_class,
de6d9b64 699};