2 * UDP prototype streaming system
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of Libav.
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.
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.
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
27 #define _BSD_SOURCE /* Needed for using struct ip_mreq with recent glibc */
30 #include "avio_internal.h"
31 #include "libavutil/parseutils.h"
32 #include "libavutil/avstring.h"
36 #include "os_support.h"
40 #ifndef IPV6_ADD_MEMBERSHIP
41 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
42 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
52 struct sockaddr_storage dest_addr;
57 #define UDP_TX_BUF_SIZE 32768
58 #define UDP_MAX_PKT_SIZE 65536
60 static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
61 struct sockaddr *addr)
63 #ifdef IP_MULTICAST_TTL
64 if (addr->sa_family == AF_INET) {
65 if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
66 av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL): %s\n", strerror(errno));
71 #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
72 if (addr->sa_family == AF_INET6) {
73 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
74 av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS): %s\n", strerror(errno));
82 static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
84 #ifdef IP_ADD_MEMBERSHIP
85 if (addr->sa_family == AF_INET) {
88 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
89 mreq.imr_interface.s_addr= INADDR_ANY;
90 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
91 av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP): %s\n", strerror(errno));
96 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
97 if (addr->sa_family == AF_INET6) {
98 struct ipv6_mreq mreq6;
100 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
101 mreq6.ipv6mr_interface= 0;
102 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
103 av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP): %s\n", strerror(errno));
111 static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
113 #ifdef IP_DROP_MEMBERSHIP
114 if (addr->sa_family == AF_INET) {
117 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
118 mreq.imr_interface.s_addr= INADDR_ANY;
119 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
120 av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP): %s\n", strerror(errno));
125 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
126 if (addr->sa_family == AF_INET6) {
127 struct ipv6_mreq mreq6;
129 memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
130 mreq6.ipv6mr_interface= 0;
131 if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
132 av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP): %s\n", strerror(errno));
140 static struct addrinfo* udp_resolve_host(const char *hostname, int port,
141 int type, int family, int flags)
143 struct addrinfo hints, *res = 0;
146 const char *node = 0, *service = "0";
149 snprintf(sport, sizeof(sport), "%d", port);
152 if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
155 memset(&hints, 0, sizeof(hints));
156 hints.ai_socktype = type;
157 hints.ai_family = family;
158 hints.ai_flags = flags;
159 if ((error = getaddrinfo(node, service, &hints, &res))) {
161 av_log(NULL, AV_LOG_ERROR, "udp_resolve_host: %s\n", gai_strerror(error));
167 static int udp_set_url(struct sockaddr_storage *addr,
168 const char *hostname, int port)
170 struct addrinfo *res0;
173 res0 = udp_resolve_host(hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
174 if (res0 == 0) return AVERROR(EIO);
175 memcpy(addr, res0->ai_addr, res0->ai_addrlen);
176 addr_len = res0->ai_addrlen;
182 static int udp_socket_create(UDPContext *s, struct sockaddr_storage *addr,
183 int *addr_len, const char *localaddr)
186 struct addrinfo *res0 = NULL, *res = NULL;
187 int family = AF_UNSPEC;
189 if (((struct sockaddr *) &s->dest_addr)->sa_family)
190 family = ((struct sockaddr *) &s->dest_addr)->sa_family;
191 res0 = udp_resolve_host(localaddr[0] ? localaddr : NULL, s->local_port,
192 SOCK_DGRAM, family, AI_PASSIVE);
195 for (res = res0; res; res=res->ai_next) {
196 udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
197 if (udp_fd > 0) break;
198 av_log(NULL, AV_LOG_ERROR, "socket: %s\n", strerror(errno));
204 memcpy(addr, res->ai_addr, res->ai_addrlen);
205 *addr_len = res->ai_addrlen;
219 static int udp_port(struct sockaddr_storage *addr, int addr_len)
221 char sbuf[sizeof(int)*3+1];
223 if (getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0, sbuf, sizeof(sbuf), NI_NUMERICSERV) != 0) {
224 av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", strerror(errno));
228 return strtol(sbuf, NULL, 10);
233 * If no filename is given to av_open_input_file because you want to
234 * get the local port first, then you must call this function to set
235 * the remote server address.
237 * url syntax: udp://host:port[?option=val...]
238 * option: 'ttl=n' : set the ttl value (for multicast only)
239 * 'localport=n' : set the local port
240 * 'pkt_size=n' : set max packet size
241 * 'reuse=1' : enable reusing the socket
243 * @param h media file context
244 * @param uri of the remote server
245 * @return zero if no error.
247 int ff_udp_set_remote_url(URLContext *h, const char *uri)
249 UDPContext *s = h->priv_data;
250 char hostname[256], buf[10];
254 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
256 /* set the destination address */
257 s->dest_addr_len = udp_set_url(&s->dest_addr, hostname, port);
258 if (s->dest_addr_len < 0) {
261 s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
262 p = strchr(uri, '?');
264 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
265 int was_connected = s->is_connected;
266 s->is_connected = strtol(buf, NULL, 10);
267 if (s->is_connected && !was_connected) {
268 if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
271 av_log(h, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
282 * Return the local port used by the UDP connection
283 * @param h media file context
284 * @return the local port number
286 int ff_udp_get_local_port(URLContext *h)
288 UDPContext *s = h->priv_data;
289 return s->local_port;
293 * Return the udp file handle for select() usage to wait for several RTP
294 * streams at the same time.
295 * @param h media file context
297 static int udp_get_file_handle(URLContext *h)
299 UDPContext *s = h->priv_data;
303 /* put it in UDP context */
304 /* return non zero if error */
305 static int udp_open(URLContext *h, const char *uri, int flags)
307 char hostname[1024], localaddr[1024] = "";
308 int port, udp_fd = -1, tmp, bind_ret = -1;
309 UDPContext *s = h->priv_data;
313 struct sockaddr_storage my_addr;
315 int reuse_specified = 0;
318 h->max_packet_size = 1472;
320 is_output = !(flags & AVIO_FLAG_READ);
323 s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
325 p = strchr(uri, '?');
327 if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
329 s->reuse_socket = strtol(buf, &endptr, 10);
330 /* assume if no digits were found it is a request to enable it */
335 if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
336 s->ttl = strtol(buf, NULL, 10);
338 if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
339 s->local_port = strtol(buf, NULL, 10);
341 if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
342 h->max_packet_size = strtol(buf, NULL, 10);
344 if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
345 s->buffer_size = strtol(buf, NULL, 10);
347 if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
348 s->is_connected = strtol(buf, NULL, 10);
350 if (av_find_info_tag(buf, sizeof(buf), "localaddr", p)) {
351 av_strlcpy(localaddr, buf, sizeof(localaddr));
355 /* fill the dest addr */
356 av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
358 /* XXX: fix av_url_split */
359 if (hostname[0] == '\0' || hostname[0] == '?') {
360 /* only accepts null hostname if input */
361 if (!(flags & AVIO_FLAG_READ))
364 if (ff_udp_set_remote_url(h, uri) < 0)
368 if ((s->is_multicast || !s->local_port) && (h->flags & AVIO_FLAG_READ))
369 s->local_port = port;
370 udp_fd = udp_socket_create(s, &my_addr, &len, localaddr);
374 /* Follow the requested reuse option, unless it's multicast in which
375 * case enable reuse unless explicitely disabled.
377 if (s->reuse_socket || (s->is_multicast && !reuse_specified)) {
379 if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
383 /* the bind is needed to give a port to the socket now */
384 /* if multicast, try the multicast address bind first */
385 if (s->is_multicast && (h->flags & AVIO_FLAG_READ)) {
386 bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
388 /* bind to the local address if not multicast or if the multicast
390 if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
393 len = sizeof(my_addr);
394 getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
395 s->local_port = udp_port(&my_addr, len);
397 if (s->is_multicast) {
398 if (!(h->flags & AVIO_FLAG_READ)) {
400 if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
404 if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0)
410 /* limit the tx buf size to limit latency */
411 tmp = s->buffer_size;
412 if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
413 av_log(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
417 /* set udp recv buffer size to the largest possible udp packet size to
418 * avoid losing data on OSes that set this too low by default. */
419 tmp = s->buffer_size;
420 if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
421 av_log(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
423 /* make the socket non-blocking */
424 ff_socket_nonblock(udp_fd, 1);
426 if (s->is_connected) {
427 if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
428 av_log(h, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
441 static int udp_read(URLContext *h, uint8_t *buf, int size)
443 UDPContext *s = h->priv_data;
446 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
447 ret = ff_network_wait_fd(s->udp_fd, 0);
451 ret = recv(s->udp_fd, buf, size, 0);
452 return ret < 0 ? ff_neterrno() : ret;
455 static int udp_write(URLContext *h, const uint8_t *buf, int size)
457 UDPContext *s = h->priv_data;
460 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
461 ret = ff_network_wait_fd(s->udp_fd, 1);
466 if (!s->is_connected) {
467 ret = sendto (s->udp_fd, buf, size, 0,
468 (struct sockaddr *) &s->dest_addr,
471 ret = send(s->udp_fd, buf, size, 0);
473 return ret < 0 ? ff_neterrno() : ret;
476 static int udp_close(URLContext *h)
478 UDPContext *s = h->priv_data;
480 if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
481 udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
482 closesocket(s->udp_fd);
486 URLProtocol ff_udp_protocol = {
488 .url_open = udp_open,
489 .url_read = udp_read,
490 .url_write = udp_write,
491 .url_close = udp_close,
492 .url_get_file_handle = udp_get_file_handle,
493 .priv_data_size = sizeof(UDPContext),