build: Add feature test macros for glibc 2.19+
[libav.git] / libavformat / udp.c
1 /*
2 * UDP prototype streaming system
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * UDP protocol
25 */
26
27 #define _DEFAULT_SOURCE
28 #define _BSD_SOURCE /* Needed for using struct ip_mreq with recent glibc */
29
30 #include "avformat.h"
31 #include "avio_internal.h"
32 #include "libavutil/parseutils.h"
33 #include "libavutil/avstring.h"
34 #include "libavutil/opt.h"
35 #include "internal.h"
36 #include "network.h"
37 #include "os_support.h"
38 #include "url.h"
39
40 #ifndef IPV6_ADD_MEMBERSHIP
41 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
42 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
43 #endif
44
45 typedef struct UDPContext {
46 const AVClass *class;
47 int udp_fd;
48 int ttl;
49 int buffer_size;
50 int pkt_size;
51 int is_multicast;
52 int local_port;
53 int reuse_socket;
54 struct sockaddr_storage dest_addr;
55 int dest_addr_len;
56 int is_connected;
57 char *localaddr;
58 char *sources;
59 char *block;
60 } UDPContext;
61
62 #define UDP_TX_BUF_SIZE 32768
63 #define UDP_MAX_PKT_SIZE 65536
64
65 #define OFFSET(x) offsetof(UDPContext, x)
66 #define D AV_OPT_FLAG_DECODING_PARAM
67 #define E AV_OPT_FLAG_ENCODING_PARAM
68 static 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
81 static 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
88 static 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
95 static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
96 struct sockaddr *addr)
97 {
98 #ifdef IP_MULTICAST_TTL
99 if (addr->sa_family == AF_INET) {
100 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
101 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL)");
102 return -1;
103 }
104 }
105 #endif
106 #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
107 if (addr->sa_family == AF_INET6) {
108 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
109 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS)");
110 return -1;
111 }
112 }
113 #endif
114 return 0;
115 }
116
117 static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
118 {
119 #ifdef IP_ADD_MEMBERSHIP
120 if (addr->sa_family == AF_INET) {
121 struct ip_mreq mreq;
122
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) {
126 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP)");
127 return -1;
128 }
129 }
130 #endif
131 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
132 if (addr->sa_family == AF_INET6) {
133 struct ipv6_mreq mreq6;
134
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) {
138 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP)");
139 return -1;
140 }
141 }
142 #endif
143 return 0;
144 }
145
146 static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
147 {
148 #ifdef IP_DROP_MEMBERSHIP
149 if (addr->sa_family == AF_INET) {
150 struct ip_mreq mreq;
151
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) {
155 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP)");
156 return -1;
157 }
158 }
159 #endif
160 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
161 if (addr->sa_family == AF_INET6) {
162 struct ipv6_mreq mreq6;
163
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) {
167 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP)");
168 return -1;
169 }
170 }
171 #endif
172 return 0;
173 }
174
175 static struct addrinfo *udp_resolve_host(URLContext *h,
176 const char *hostname, int port,
177 int type, int family, int flags)
178 {
179 struct addrinfo hints = { 0 }, *res = 0;
180 int error;
181 char sport[16];
182 const char *node = 0, *service = "0";
183
184 if (port > 0) {
185 snprintf(sport, sizeof(sport), "%d", port);
186 service = sport;
187 }
188 if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
189 node = hostname;
190 }
191 hints.ai_socktype = type;
192 hints.ai_family = family;
193 hints.ai_flags = flags;
194 if ((error = getaddrinfo(node, service, &hints, &res))) {
195 res = NULL;
196 av_log(h, AV_LOG_ERROR, "getaddrinfo(%s, %s): %s\n",
197 node ? node : "unknown",
198 service ? service : "unknown",
199 gai_strerror(error));
200 }
201
202 return res;
203 }
204
205 static int udp_set_multicast_sources(URLContext *h,
206 int sockfd, struct sockaddr *addr,
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;
217 struct addrinfo *sourceaddr = udp_resolve_host(h, sources[i], 0,
218 SOCK_DGRAM, AF_UNSPEC,
219 0);
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;
247 struct addrinfo *sourceaddr = udp_resolve_host(h, sources[i], 0,
248 SOCK_DGRAM, AF_UNSPEC,
249 0);
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 }
279 static int udp_set_url(URLContext *h,
280 struct sockaddr_storage *addr,
281 const char *hostname, int port)
282 {
283 struct addrinfo *res0;
284 int addr_len;
285
286 res0 = udp_resolve_host(h, hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
287 if (res0 == 0) return AVERROR(EIO);
288 memcpy(addr, res0->ai_addr, res0->ai_addrlen);
289 addr_len = res0->ai_addrlen;
290 freeaddrinfo(res0);
291
292 return addr_len;
293 }
294
295 static int udp_socket_create(URLContext *h, struct sockaddr_storage *addr,
296 socklen_t *addr_len, const char *localaddr)
297 {
298 UDPContext *s = h->priv_data;
299 int udp_fd = -1;
300 struct addrinfo *res0 = NULL, *res = NULL;
301 int family = AF_UNSPEC;
302
303 if (((struct sockaddr *) &s->dest_addr)->sa_family)
304 family = ((struct sockaddr *) &s->dest_addr)->sa_family;
305 res0 = udp_resolve_host(h, (localaddr && localaddr[0]) ? localaddr : NULL,
306 s->local_port,
307 SOCK_DGRAM, family, AI_PASSIVE);
308 if (res0 == 0)
309 goto fail;
310 for (res = res0; res; res=res->ai_next) {
311 udp_fd = ff_socket(res->ai_family, SOCK_DGRAM, 0);
312 if (udp_fd != -1) break;
313 log_net_error(NULL, AV_LOG_ERROR, "socket");
314 }
315
316 if (udp_fd < 0)
317 goto fail;
318
319 memcpy(addr, res->ai_addr, res->ai_addrlen);
320 *addr_len = res->ai_addrlen;
321
322 freeaddrinfo(res0);
323
324 return udp_fd;
325
326 fail:
327 if (udp_fd >= 0)
328 closesocket(udp_fd);
329 if(res0)
330 freeaddrinfo(res0);
331 return -1;
332 }
333
334 static int udp_port(struct sockaddr_storage *addr, int addr_len)
335 {
336 char sbuf[sizeof(int)*3+1];
337 int error;
338
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));
341 return -1;
342 }
343
344 return strtol(sbuf, NULL, 10);
345 }
346
347
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...]
354 * option: 'ttl=n' : set the ttl value (for multicast only)
355 * 'localport=n' : set the local port
356 * 'pkt_size=n' : set max packet size
357 * 'reuse=1' : enable reusing the socket
358 *
359 * @param h media file context
360 * @param uri of the remote server
361 * @return zero if no error.
362 */
363 int ff_udp_set_remote_url(URLContext *h, const char *uri)
364 {
365 UDPContext *s = h->priv_data;
366 char hostname[256], buf[10];
367 int port;
368 const char *p;
369
370 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
371
372 /* set the destination address */
373 s->dest_addr_len = udp_set_url(h, &s->dest_addr, hostname, port);
374 if (s->dest_addr_len < 0) {
375 return AVERROR(EIO);
376 }
377 s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
378 p = strchr(uri, '?');
379 if (p) {
380 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
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;
387 log_net_error(h, AV_LOG_ERROR, "connect");
388 return AVERROR(EIO);
389 }
390 }
391 }
392 }
393
394 return 0;
395 }
396
397 /**
398 * Return the local port used by the UDP connection
399 * @param h media file context
400 * @return the local port number
401 */
402 int ff_udp_get_local_port(URLContext *h)
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 */
413 static int udp_get_file_handle(URLContext *h)
414 {
415 UDPContext *s = h->priv_data;
416 return s->udp_fd;
417 }
418
419 static 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
440 /* put it in UDP context */
441 /* return non zero if error */
442 static int udp_open(URLContext *h, const char *uri, int flags)
443 {
444 char hostname[1024], localaddr[1024] = "";
445 int port, udp_fd = -1, tmp, bind_ret = -1;
446 UDPContext *s = h->priv_data;
447 int is_output;
448 const char *p;
449 char buf[256];
450 struct sockaddr_storage my_addr;
451 socklen_t len;
452 int i, num_include_sources = 0, num_exclude_sources = 0;
453 char *include_sources[32], *exclude_sources[32];
454
455 h->is_streamed = 1;
456 h->max_packet_size = 1472;
457
458 is_output = !(flags & AVIO_FLAG_READ);
459
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
476 if (s->pkt_size > 0)
477 h->max_packet_size = s->pkt_size;
478
479 p = strchr(uri, '?');
480 if (p) {
481 if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
482 char *endptr = NULL;
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;
487 }
488 if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
489 s->ttl = strtol(buf, NULL, 10);
490 }
491 if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
492 s->local_port = strtol(buf, NULL, 10);
493 }
494 if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
495 h->max_packet_size = strtol(buf, NULL, 10);
496 }
497 if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
498 s->buffer_size = strtol(buf, NULL, 10);
499 }
500 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
501 s->is_connected = strtol(buf, NULL, 10);
502 }
503 if (av_find_info_tag(buf, sizeof(buf), "localaddr", p)) {
504 av_strlcpy(localaddr, buf, sizeof(localaddr));
505 }
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;
515 }
516 }
517
518 /* fill the dest addr */
519 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
520
521 /* XXX: fix av_url_split */
522 if (hostname[0] == '\0' || hostname[0] == '?') {
523 /* only accepts null hostname if input */
524 if (!(flags & AVIO_FLAG_READ))
525 goto fail;
526 } else {
527 if (ff_udp_set_remote_url(h, uri) < 0)
528 goto fail;
529 }
530
531 if ((s->is_multicast || s->local_port < 0) && (h->flags & AVIO_FLAG_READ))
532 s->local_port = port;
533
534 if (localaddr[0])
535 udp_fd = udp_socket_create(h, &my_addr, &len, localaddr);
536 else
537 udp_fd = udp_socket_create(h, &my_addr, &len, s->localaddr);
538 if (udp_fd < 0)
539 goto fail;
540
541 /* Follow the requested reuse option, unless it's multicast in which
542 * case enable reuse unless explicitly disabled.
543 */
544 if (s->reuse_socket > 0 || (s->is_multicast && s->reuse_socket < 0)) {
545 s->reuse_socket = 1;
546 if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
547 goto fail;
548 }
549
550 /* If multicast, try binding the multicast address first, to avoid
551 * receiving UDP packets from other sources aimed at the same UDP
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)) {
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 */
559 /* the bind is needed to give a port to the socket now */
560 if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) {
561 log_net_error(h, AV_LOG_ERROR, "bind failed");
562 goto fail;
563 }
564
565 len = sizeof(my_addr);
566 getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
567 s->local_port = udp_port(&my_addr, len);
568
569 if (s->is_multicast) {
570 if (h->flags & AVIO_FLAG_WRITE) {
571 /* output */
572 if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
573 goto fail;
574 }
575 if (h->flags & AVIO_FLAG_READ) {
576 /* input */
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) {
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)
587 goto fail;
588 } else {
589 if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
590 goto fail;
591 }
592 if (num_exclude_sources) {
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)
598 goto fail;
599 }
600 }
601 }
602
603 if (is_output) {
604 /* limit the tx buf size to limit latency */
605 tmp = s->buffer_size;
606 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
607 log_net_error(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF)");
608 goto fail;
609 }
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. */
613 tmp = s->buffer_size;
614 if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
615 log_net_error(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF)");
616 }
617 /* make the socket non-blocking */
618 ff_socket_nonblock(udp_fd, 1);
619 }
620 if (s->is_connected) {
621 if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
622 log_net_error(h, AV_LOG_ERROR, "connect");
623 goto fail;
624 }
625 }
626
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]);
631
632 s->udp_fd = udp_fd;
633 return 0;
634 fail:
635 if (udp_fd >= 0)
636 closesocket(udp_fd);
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]);
641 return AVERROR(EIO);
642 }
643
644 static int udp_read(URLContext *h, uint8_t *buf, int size)
645 {
646 UDPContext *s = h->priv_data;
647 int ret;
648
649 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
650 ret = ff_network_wait_fd(s->udp_fd, 0);
651 if (ret < 0)
652 return ret;
653 }
654 ret = recv(s->udp_fd, buf, size, 0);
655 return ret < 0 ? ff_neterrno() : ret;
656 }
657
658 static int udp_write(URLContext *h, const uint8_t *buf, int size)
659 {
660 UDPContext *s = h->priv_data;
661 int ret;
662
663 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
664 ret = ff_network_wait_fd(s->udp_fd, 1);
665 if (ret < 0)
666 return ret;
667 }
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;
677 }
678
679 static int udp_close(URLContext *h)
680 {
681 UDPContext *s = h->priv_data;
682
683 if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
684 udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
685 closesocket(s->udp_fd);
686 return 0;
687 }
688
689 const URLProtocol ff_udp_protocol = {
690 .name = "udp",
691 .url_open = udp_open,
692 .url_read = udp_read,
693 .url_write = udp_write,
694 .url_close = udp_close,
695 .url_get_file_handle = udp_get_file_handle,
696 .priv_data_size = sizeof(UDPContext),
697 .flags = URL_PROTOCOL_FLAG_NETWORK,
698 .priv_data_class = &udp_class,
699 };