]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/udp.c
Fix memleak caused by the fact that url_open_buf() allocates a context
[ffmpeg] / libavformat / udp.c
index da3e253820d85f0f3a2dd17cafed4bab9d1b8260..bbdb930e4ef7a69632a1f90ff873ce6796f8888c 100644 (file)
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
+
+/**
+ * @file udp.c
+ * UDP protocol
+ */
+
+#define _BSD_SOURCE     /* Needed for using struct ip_mreq with recent glibc */
 #include "avformat.h"
 #include <unistd.h>
 #include "network.h"
 #include "os_support.h"
+#ifdef HAVE_SYS_SELECT_H
+#include <sys/select.h>
+#endif
 
 #ifndef IPV6_ADD_MEMBERSHIP
 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
 #endif
+#ifndef IN_MULTICAST
+#define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
+#endif
+#ifndef IN6_IS_ADDR_MULTICAST
+#define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
+#endif
 
 typedef struct {
     int udp_fd;
     int ttl;
+    int buffer_size;
     int is_multicast;
     int local_port;
     int reuse_socket;
@@ -39,7 +56,7 @@ typedef struct {
 #else
     struct sockaddr_storage dest_addr;
 #endif
-    size_t dest_addr_len;
+    int dest_addr_len;
 } UDPContext;
 
 #define UDP_TX_BUF_SIZE 32768
@@ -159,6 +176,18 @@ static int udp_set_url(struct sockaddr_storage *addr, const char *hostname, int
     return addr_len;
 }
 
+static int is_multicast_address(struct sockaddr_storage *addr)
+{
+    if (addr->ss_family == AF_INET) {
+        return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
+    }
+    if (addr->ss_family == AF_INET6) {
+        return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
+    }
+
+    return 0;
+}
+
 static int udp_socket_create(UDPContext *s, struct sockaddr_storage *addr, int *addr_len)
 {
     int udp_fd = -1;
@@ -219,6 +248,11 @@ static int udp_set_url(struct sockaddr_in *addr, const char *hostname, int port)
     return sizeof(struct sockaddr_in);
 }
 
+static int is_multicast_address(struct sockaddr_in *addr)
+{
+    return IN_MULTICAST(ntohl(addr->sin_addr.s_addr));
+}
+
 static int udp_socket_create(UDPContext *s, struct sockaddr_in *addr, int *addr_len)
 {
     int fd;
@@ -248,8 +282,7 @@ static int udp_port(struct sockaddr_in *addr, int len)
  * the remote server address.
  *
  * url syntax: udp://host:port[?option=val...]
- * option: 'multicast=1' : enable multicast
- *         'ttl=n'       : set the ttl value (for multicast only)
+ * option: 'ttl=n'       : set the ttl value (for multicast only)
  *         'localport=n' : set the local port
  *         'pkt_size=n'  : set max packet size
  *         'reuse=1'     : enable reusing the socket
@@ -271,6 +304,7 @@ int udp_set_remote_url(URLContext *h, const char *uri)
     if (s->dest_addr_len < 0) {
         return AVERROR(EIO);
     }
+    s->is_multicast = is_multicast_address(&s->dest_addr);
 
     return 0;
 }
@@ -319,15 +353,19 @@ static int udp_open(URLContext *h, const char *uri, int flags)
 
     is_output = (flags & URL_WRONLY);
 
+    if(!ff_network_init())
+        return AVERROR(EIO);
+
     s = av_mallocz(sizeof(UDPContext));
     if (!s)
         return AVERROR(ENOMEM);
 
     h->priv_data = s;
     s->ttl = 16;
+    s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
+
     p = strchr(uri, '?');
     if (p) {
-        s->is_multicast = find_info_tag(buf, sizeof(buf), "multicast", p);
         s->reuse_socket = find_info_tag(buf, sizeof(buf), "reuse", p);
         if (find_info_tag(buf, sizeof(buf), "ttl", p)) {
             s->ttl = strtol(buf, NULL, 10);
@@ -338,6 +376,9 @@ static int udp_open(URLContext *h, const char *uri, int flags)
         if (find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
             h->max_packet_size = strtol(buf, NULL, 10);
         }
+        if (find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
+            s->buffer_size = strtol(buf, NULL, 10);
+        }
     }
 
     /* fill the dest addr */
@@ -346,15 +387,12 @@ static int udp_open(URLContext *h, const char *uri, int flags)
     /* XXX: fix url_split */
     if (hostname[0] == '\0' || hostname[0] == '?') {
         /* only accepts null hostname if input */
-        if (s->is_multicast || (flags & URL_WRONLY))
+        if (flags & URL_WRONLY)
             goto fail;
     } else {
         udp_set_remote_url(h, uri);
     }
 
-    if(!ff_network_init())
-        return AVERROR(EIO);
-
     if (s->is_multicast && !(h->flags & URL_WRONLY))
         s->local_port = port;
     udp_fd = udp_socket_create(s, &my_addr, &len);
@@ -387,7 +425,7 @@ static int udp_open(URLContext *h, const char *uri, int flags)
 
     if (is_output) {
         /* limit the tx buf size to limit latency */
-        tmp = UDP_TX_BUF_SIZE;
+        tmp = s->buffer_size;
         if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
             av_log(NULL, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno));
             goto fail;
@@ -395,8 +433,12 @@ static int udp_open(URLContext *h, const char *uri, int flags)
     } else {
         /* set udp recv buffer size to the largest possible udp packet size to
          * avoid losing data on OSes that set this too low by default. */
-        tmp = UDP_MAX_PKT_SIZE;
-        setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp));
+        tmp = s->buffer_size;
+        if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
+            av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno));
+        }
+        /* make the socket non-blocking */
+        ff_socket_nonblock(udp_fd, 1);
     }
 
     s->udp_fd = udp_fd;
@@ -412,8 +454,22 @@ static int udp_read(URLContext *h, uint8_t *buf, int size)
 {
     UDPContext *s = h->priv_data;
     int len;
+    fd_set rfds;
+    int ret;
+    struct timeval tv;
 
     for(;;) {
+        if (url_interrupt_cb())
+            return AVERROR(EINTR);
+        FD_ZERO(&rfds);
+        FD_SET(s->udp_fd, &rfds);
+        tv.tv_sec = 0;
+        tv.tv_usec = 100 * 1000;
+        ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv);
+        if (ret < 0)
+            return AVERROR(EIO);
+        if (!(ret > 0 && FD_ISSET(s->udp_fd, &rfds)))
+            continue;
         len = recv(s->udp_fd, buf, size, 0);
         if (len < 0) {
             if (ff_neterrno() != FF_NETERROR(EAGAIN) &&