X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;ds=inline;f=libavformat%2Fudp.c;h=13c346a6a917c92287a55ad3ad28ce79410b2976;hb=fcf7ef0a8128e73cd0b921732d309b0eb558e80e;hp=bbdeda05fe1157ccc17057685136b153a3fa0e95;hpb=203bbaccfaaeac9548862e83792d38509a8c8167;p=ffmpeg diff --git a/libavformat/udp.c b/libavformat/udp.c index bbdeda05fe1..13c346a6a91 100644 --- a/libavformat/udp.c +++ b/libavformat/udp.c @@ -41,6 +41,7 @@ #include "network.h" #include "os_support.h" #include "url.h" +#include "ip.h" #ifdef __APPLE__ #include "TargetConditionals.h" @@ -60,8 +61,13 @@ #define IPPROTO_UDPLITE 136 #endif +#if HAVE_W32THREADS +#undef HAVE_PTHREAD_CANCEL +#define HAVE_PTHREAD_CANCEL 1 +#endif + #if HAVE_PTHREAD_CANCEL -#include +#include "libavutil/thread.h" #endif #ifndef IPV6_ADD_MEMBERSHIP @@ -70,6 +76,7 @@ #endif #define UDP_TX_BUF_SIZE 32768 +#define UDP_RX_BUF_SIZE 393216 #define UDP_MAX_PKT_SIZE 65536 #define UDP_HEADER_SIZE 8 @@ -109,6 +116,7 @@ typedef struct UDPContext { struct sockaddr_storage local_addr_storage; char *sources; char *block; + IPSourceFilters filters; } UDPContext; #define OFFSET(x) offsetof(UDPContext, x) @@ -130,7 +138,7 @@ static const AVOption options[] = { { "connect", "set if connect() should be called on socket", OFFSET(is_connected), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = D|E }, { "fifo_size", "set the UDP receiving circular buffer size, expressed as a number of packets with size of 188 bytes", OFFSET(circular_buffer_size), AV_OPT_TYPE_INT, {.i64 = 7*4096}, 0, INT_MAX, D }, { "overrun_nonfatal", "survive in case of UDP receiving circular buffer overrun", OFFSET(overrun_nonfatal), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, D }, - { "timeout", "set raise error timeout (only in read mode)", OFFSET(timeout), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, D }, + { "timeout", "set raise error timeout, in microseconds (only in read mode)",OFFSET(timeout), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D }, { "sources", "Source list", OFFSET(sources), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E }, { "block", "Block list", OFFSET(block), AV_OPT_TYPE_STRING, { .str = NULL }, .flags = D|E }, { NULL } @@ -182,7 +190,7 @@ static int udp_join_multicast_group(int sockfd, struct sockaddr *addr,struct soc if (local_addr) mreq.imr_interface= ((struct sockaddr_in *)local_addr)->sin_addr; else - mreq.imr_interface.s_addr= INADDR_ANY; + mreq.imr_interface.s_addr = INADDR_ANY; if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) { ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP)"); return -1; @@ -194,7 +202,8 @@ static int udp_join_multicast_group(int sockfd, struct sockaddr *addr,struct soc struct ipv6_mreq mreq6; memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr)); - mreq6.ipv6mr_interface= 0; + //TODO: Interface index should be looked up from local_addr + mreq6.ipv6mr_interface = 0; if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) { ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP)"); return -1; @@ -212,9 +221,9 @@ static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr,struct so mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr; if (local_addr) - mreq.imr_interface= ((struct sockaddr_in *)local_addr)->sin_addr; + mreq.imr_interface = ((struct sockaddr_in *)local_addr)->sin_addr; else - mreq.imr_interface.s_addr= INADDR_ANY; + mreq.imr_interface.s_addr = INADDR_ANY; if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) { ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP)"); return -1; @@ -226,7 +235,8 @@ static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr,struct so struct ipv6_mreq mreq6; memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr)); - mreq6.ipv6mr_interface= 0; + //TODO: Interface index should be looked up from local_addr + mreq6.ipv6mr_interface = 0; if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) { ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP)"); return -1; @@ -236,102 +246,67 @@ static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr,struct so return 0; } -static struct addrinfo *udp_resolve_host(URLContext *h, - const char *hostname, int port, - int type, int family, int flags) -{ - struct addrinfo hints = { 0 }, *res = 0; - int error; - char sport[16]; - const char *node = 0, *service = "0"; - - if (port > 0) { - snprintf(sport, sizeof(sport), "%d", port); - service = sport; - } - if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) { - node = hostname; - } - hints.ai_socktype = type; - hints.ai_family = family; - hints.ai_flags = flags; - if ((error = getaddrinfo(node, service, &hints, &res))) { - res = NULL; - av_log(h, AV_LOG_ERROR, "getaddrinfo(%s, %s): %s\n", - node ? node : "unknown", - service, - gai_strerror(error)); - } - - return res; -} - static int udp_set_multicast_sources(URLContext *h, int sockfd, struct sockaddr *addr, - int addr_len, char **sources, + int addr_len, struct sockaddr_storage *local_addr, + struct sockaddr_storage *sources, int nb_sources, int include) { -#if HAVE_STRUCT_GROUP_SOURCE_REQ && defined(MCAST_BLOCK_SOURCE) && !defined(_WIN32) && (!defined(TARGET_OS_TV) || !TARGET_OS_TV) - /* These ones are available in the microsoft SDK, but don't seem to work - * as on linux, so just prefer the v4-only approach there for now. */ - int i; - for (i = 0; i < nb_sources; i++) { - struct group_source_req mreqs; - int level = addr->sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; - struct addrinfo *sourceaddr = udp_resolve_host(h, sources[i], 0, - SOCK_DGRAM, AF_UNSPEC, - 0); - if (!sourceaddr) - return AVERROR(ENOENT); - - mreqs.gsr_interface = 0; - memcpy(&mreqs.gsr_group, addr, addr_len); - memcpy(&mreqs.gsr_source, sourceaddr->ai_addr, sourceaddr->ai_addrlen); - freeaddrinfo(sourceaddr); - - if (setsockopt(sockfd, level, - include ? MCAST_JOIN_SOURCE_GROUP : MCAST_BLOCK_SOURCE, - (const void *)&mreqs, sizeof(mreqs)) < 0) { - if (include) - ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_JOIN_SOURCE_GROUP)"); - else - ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_BLOCK_SOURCE)"); - return ff_neterrno(); - } - } -#elif HAVE_STRUCT_IP_MREQ_SOURCE && defined(IP_BLOCK_SOURCE) int i; if (addr->sa_family != AF_INET) { - av_log(NULL, AV_LOG_ERROR, +#if HAVE_STRUCT_GROUP_SOURCE_REQ && defined(MCAST_BLOCK_SOURCE) + /* For IPv4 prefer the old approach, as that alone works reliably on + * Windows and it also supports supplying the interface based on its + * address. */ + int i; + for (i = 0; i < nb_sources; i++) { + struct group_source_req mreqs; + int level = addr->sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6; + + //TODO: Interface index should be looked up from local_addr + mreqs.gsr_interface = 0; + memcpy(&mreqs.gsr_group, addr, addr_len); + memcpy(&mreqs.gsr_source, &sources[i], sizeof(*sources)); + + if (setsockopt(sockfd, level, + include ? MCAST_JOIN_SOURCE_GROUP : MCAST_BLOCK_SOURCE, + (const void *)&mreqs, sizeof(mreqs)) < 0) { + if (include) + ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_JOIN_SOURCE_GROUP)"); + else + ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_BLOCK_SOURCE)"); + return ff_neterrno(); + } + } + return 0; +#else + av_log(h, AV_LOG_ERROR, "Setting multicast sources only supported for IPv4\n"); return AVERROR(EINVAL); +#endif } +#if HAVE_STRUCT_IP_MREQ_SOURCE && defined(IP_BLOCK_SOURCE) for (i = 0; i < nb_sources; i++) { struct ip_mreq_source mreqs; - struct addrinfo *sourceaddr = udp_resolve_host(h, sources[i], 0, - SOCK_DGRAM, AF_UNSPEC, - 0); - if (!sourceaddr) - return AVERROR(ENOENT); - if (sourceaddr->ai_addr->sa_family != AF_INET) { - freeaddrinfo(sourceaddr); - av_log(NULL, AV_LOG_ERROR, "%s is of incorrect protocol family\n", - sources[i]); + if (sources[i].ss_family != AF_INET) { + av_log(h, AV_LOG_ERROR, "Source/block address %d is of incorrect protocol family\n", i + 1); return AVERROR(EINVAL); } mreqs.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr; - mreqs.imr_interface.s_addr = INADDR_ANY; - mreqs.imr_sourceaddr.s_addr = ((struct sockaddr_in *)sourceaddr->ai_addr)->sin_addr.s_addr; - freeaddrinfo(sourceaddr); + if (local_addr) + mreqs.imr_interface = ((struct sockaddr_in *)local_addr)->sin_addr; + else + mreqs.imr_interface.s_addr = INADDR_ANY; + mreqs.imr_sourceaddr.s_addr = ((struct sockaddr_in *)&sources[i])->sin_addr.s_addr; if (setsockopt(sockfd, IPPROTO_IP, include ? IP_ADD_SOURCE_MEMBERSHIP : IP_BLOCK_SOURCE, (const void *)&mreqs, sizeof(mreqs)) < 0) { if (include) - ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_SOURCE_MEMBERSHIP)"); + ff_log_net_error(h, AV_LOG_ERROR, "setsockopt(IP_ADD_SOURCE_MEMBERSHIP)"); else - ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_BLOCK_SOURCE)"); + ff_log_net_error(h, AV_LOG_ERROR, "setsockopt(IP_BLOCK_SOURCE)"); return ff_neterrno(); } } @@ -347,7 +322,7 @@ static int udp_set_url(URLContext *h, struct addrinfo *res0; int addr_len; - res0 = udp_resolve_host(h, hostname, port, SOCK_DGRAM, AF_UNSPEC, 0); + res0 = ff_ip_resolve_host(h, hostname, port, SOCK_DGRAM, AF_UNSPEC, 0); if (!res0) return AVERROR(EIO); memcpy(addr, res0->ai_addr, res0->ai_addrlen); addr_len = res0->ai_addrlen; @@ -366,7 +341,7 @@ static int udp_socket_create(URLContext *h, struct sockaddr_storage *addr, if (((struct sockaddr *) &s->dest_addr)->sa_family) family = ((struct sockaddr *) &s->dest_addr)->sa_family; - res0 = udp_resolve_host(h, (localaddr && localaddr[0]) ? localaddr : NULL, + res0 = ff_ip_resolve_host(h, (localaddr && localaddr[0]) ? localaddr : NULL, s->local_port, SOCK_DGRAM, family, AI_PASSIVE); if (!res0) @@ -500,13 +475,15 @@ static void *circular_buffer_task_rx( void *_URLContext) } while(1) { int len; + struct sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); pthread_mutex_unlock(&s->mutex); /* Blocking operations are always cancellation points; see "General Information" / "Thread Cancelation Overview" in Single Unix. */ pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate); - len = recv(s->udp_fd, s->tmp+4, sizeof(s->tmp)-4, 0); + len = recvfrom(s->udp_fd, s->tmp+4, sizeof(s->tmp)-4, 0, (struct sockaddr *)&addr, &addr_len); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate); pthread_mutex_lock(&s->mutex); if (len < 0) { @@ -516,6 +493,8 @@ static void *circular_buffer_task_rx( void *_URLContext) } continue; } + if (ff_ip_check_source_lists(&addr, &s->filters)) + continue; AV_WL32(s->tmp, len); if(av_fifo_space(s->fifo) < len + 4) { @@ -546,14 +525,12 @@ static void *circular_buffer_task_tx( void *_URLContext) { URLContext *h = _URLContext; UDPContext *s = h->priv_data; - int old_cancelstate; int64_t target_timestamp = av_gettime_relative(); int64_t start_timestamp = av_gettime_relative(); int64_t sent_bits = 0; int64_t burst_interval = s->bitrate ? (s->burst_bits * 1000000 / s->bitrate) : 0; int64_t max_delay = s->bitrate ? ((int64_t)h->max_packet_size * 8 * 1000000 / s->bitrate + 1) : 0; - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate); pthread_mutex_lock(&s->mutex); if (ff_socket_nonblock(s->udp_fd, 0) < 0) { @@ -568,7 +545,7 @@ static void *circular_buffer_task_tx( void *_URLContext) uint8_t tmp[4]; int64_t timestamp; - len=av_fifo_size(s->fifo); + len = av_fifo_size(s->fifo); while (len<4) { if (s->close_req) @@ -576,11 +553,11 @@ static void *circular_buffer_task_tx( void *_URLContext) if (pthread_cond_wait(&s->cond, &s->mutex) < 0) { goto end; } - len=av_fifo_size(s->fifo); + len = av_fifo_size(s->fifo); } av_fifo_generic_read(s->fifo, tmp, 4, NULL); - len=AV_RL32(tmp); + len = AV_RL32(tmp); av_assert0(len >= 0); av_assert0(len <= sizeof(s->tmp)); @@ -588,7 +565,6 @@ static void *circular_buffer_task_tx( void *_URLContext) av_fifo_generic_read(s->fifo, s->tmp, len, NULL); pthread_mutex_unlock(&s->mutex); - pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate); if (s->bitrate) { timestamp = av_gettime_relative(); @@ -634,7 +610,6 @@ static void *circular_buffer_task_tx( void *_URLContext) } } - pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate); pthread_mutex_lock(&s->mutex); } @@ -646,27 +621,6 @@ end: #endif -static int parse_source_list(char *buf, char **sources, int *num_sources, - int max_sources) -{ - char *source_start; - - source_start = buf; - while (1) { - char *next = strchr(source_start, ','); - if (next) - *next = '\0'; - sources[*num_sources] = av_strdup(source_start); - if (!sources[*num_sources]) - return AVERROR(ENOMEM); - source_start = next + 1; - (*num_sources)++; - if (*num_sources >= max_sources || !next) - break; - } - return 0; -} - /* put it in UDP context */ /* return non zero if error */ static int udp_open(URLContext *h, const char *uri, int flags) @@ -679,31 +633,23 @@ static int udp_open(URLContext *h, const char *uri, int flags) char buf[256]; struct sockaddr_storage my_addr; socklen_t len; - int i, num_include_sources = 0, num_exclude_sources = 0; - char *include_sources[32], *exclude_sources[32]; h->is_streamed = 1; is_output = !(flags & AVIO_FLAG_READ); if (s->buffer_size < 0) - s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE; + s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_RX_BUF_SIZE; if (s->sources) { - if (parse_source_list(s->sources, include_sources, - &num_include_sources, - FF_ARRAY_ELEMS(include_sources))) + if (ff_ip_parse_sources(h, s->sources, &s->filters) < 0) goto fail; } if (s->block) { - if (parse_source_list(s->block, exclude_sources, &num_exclude_sources, - FF_ARRAY_ELEMS(exclude_sources))) + if (ff_ip_parse_blocks(h, s->block, &s->filters) < 0) goto fail; } - if (s->pkt_size > 0) - h->max_packet_size = s->pkt_size; - p = strchr(uri, '?'); if (p) { if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) { @@ -766,13 +712,11 @@ static int udp_open(URLContext *h, const char *uri, int flags) av_strlcpy(localaddr, buf, sizeof(localaddr)); } if (av_find_info_tag(buf, sizeof(buf), "sources", p)) { - if (parse_source_list(buf, include_sources, &num_include_sources, - FF_ARRAY_ELEMS(include_sources))) + if (ff_ip_parse_sources(h, buf, &s->filters) < 0) goto fail; } if (av_find_info_tag(buf, sizeof(buf), "block", p)) { - if (parse_source_list(buf, exclude_sources, &num_exclude_sources, - FF_ARRAY_ELEMS(exclude_sources))) + if (ff_ip_parse_blocks(h, buf, &s->filters) < 0) goto fail; } if (!is_output && av_find_info_tag(buf, sizeof(buf), "timeout", p)) @@ -852,7 +796,7 @@ static int udp_open(URLContext *h, const char *uri, int flags) * receiving UDP packets from other sources aimed at the same UDP * port. This fails on windows. This makes sending to the same address * using sendto() fail, so only do it if we're opened in read-only mode. */ - if (s->is_multicast && !(h->flags & AVIO_FLAG_WRITE)) { + if (s->is_multicast && (h->flags & AVIO_FLAG_READ)) { bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len); } /* bind to the local address if not multicast or if the multicast @@ -875,27 +819,23 @@ static int udp_open(URLContext *h, const char *uri, int flags) } if (h->flags & AVIO_FLAG_READ) { /* input */ - if (num_include_sources && num_exclude_sources) { - av_log(h, AV_LOG_ERROR, "Simultaneously including and excluding multicast sources is not supported\n"); - goto fail; - } - if (num_include_sources) { + if (s->filters.nb_include_addrs) { if (udp_set_multicast_sources(h, udp_fd, (struct sockaddr *)&s->dest_addr, - s->dest_addr_len, - include_sources, - num_include_sources, 1) < 0) + s->dest_addr_len, &s->local_addr_storage, + s->filters.include_addrs, + s->filters.nb_include_addrs, 1) < 0) goto fail; } else { if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr,(struct sockaddr *)&s->local_addr_storage) < 0) goto fail; } - if (num_exclude_sources) { + if (s->filters.nb_exclude_addrs) { if (udp_set_multicast_sources(h, udp_fd, (struct sockaddr *)&s->dest_addr, - s->dest_addr_len, - exclude_sources, - num_exclude_sources, 0) < 0) + s->dest_addr_len, &s->local_addr_storage, + s->filters.exclude_addrs, + s->filters.nb_exclude_addrs, 0) < 0) goto fail; } } @@ -909,7 +849,7 @@ static int udp_open(URLContext *h, const char *uri, int flags) goto fail; } } else { - /* set udp recv buffer size to the requested value (default 64K) */ + /* set udp recv buffer size to the requested value (default UDP_RX_BUF_SIZE) */ tmp = s->buffer_size; if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) { ff_log_net_error(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF)"); @@ -920,7 +860,7 @@ static int udp_open(URLContext *h, const char *uri, int flags) } else { av_log(h, AV_LOG_DEBUG, "end receive buffer size reported is %d\n", tmp); if(tmp < s->buffer_size) - av_log(h, AV_LOG_WARNING, "attempted to set receive buffer to size %d but it only ended up set as %d", s->buffer_size, tmp); + av_log(h, AV_LOG_WARNING, "attempted to set receive buffer to size %d but it only ended up set as %d\n", s->buffer_size, tmp); } /* make the socket non-blocking */ @@ -933,11 +873,6 @@ static int udp_open(URLContext *h, const char *uri, int flags) } } - for (i = 0; i < num_include_sources; i++) - av_freep(&include_sources[i]); - for (i = 0; i < num_exclude_sources; i++) - av_freep(&exclude_sources[i]); - s->udp_fd = udp_fd; #if HAVE_PTHREAD_CANCEL @@ -987,10 +922,7 @@ static int udp_open(URLContext *h, const char *uri, int flags) if (udp_fd >= 0) closesocket(udp_fd); av_fifo_freep(&s->fifo); - for (i = 0; i < num_include_sources; i++) - av_freep(&include_sources[i]); - for (i = 0; i < num_exclude_sources; i++) - av_freep(&exclude_sources[i]); + ff_ip_reset_filters(&s->filters); return AVERROR(EIO); } @@ -1008,6 +940,8 @@ static int udp_read(URLContext *h, uint8_t *buf, int size) { UDPContext *s = h->priv_data; int ret; + struct sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); #if HAVE_PTHREAD_CANCEL int avail, nonblock = h->flags & AVIO_FLAG_NONBLOCK; @@ -1019,10 +953,10 @@ static int udp_read(URLContext *h, uint8_t *buf, int size) uint8_t tmp[4]; av_fifo_generic_read(s->fifo, tmp, 4, NULL); - avail= AV_RL32(tmp); + avail = AV_RL32(tmp); if(avail > size){ av_log(h, AV_LOG_WARNING, "Part of datagram lost due to insufficient buffer size\n"); - avail= size; + avail = size; } av_fifo_generic_read(s->fifo, buf, avail, NULL); @@ -1036,20 +970,20 @@ static int udp_read(URLContext *h, uint8_t *buf, int size) } else if(nonblock) { pthread_mutex_unlock(&s->mutex); return AVERROR(EAGAIN); - } - else { + } else { /* FIXME: using the monotonic clock would be better, but it does not exist on all supported platforms. */ int64_t t = av_gettime() + 100000; struct timespec tv = { .tv_sec = t / 1000000, .tv_nsec = (t % 1000000) * 1000 }; - if (pthread_cond_timedwait(&s->cond, &s->mutex, &tv) < 0) { + int err = pthread_cond_timedwait(&s->cond, &s->mutex, &tv); + if (err) { pthread_mutex_unlock(&s->mutex); - return AVERROR(errno == ETIMEDOUT ? EAGAIN : errno); + return AVERROR(err == ETIMEDOUT ? EAGAIN : err); } nonblock = 1; } - } while( 1); + } while(1); } #endif @@ -1058,9 +992,12 @@ static int udp_read(URLContext *h, uint8_t *buf, int size) if (ret < 0) return ret; } - ret = recv(s->udp_fd, buf, size, 0); - - return ret < 0 ? ff_neterrno() : ret; + ret = recvfrom(s->udp_fd, buf, size, 0, (struct sockaddr *)&addr, &addr_len); + if (ret < 0) + return ff_neterrno(); + if (ff_ip_check_source_lists(&addr, &s->filters)) + return AVERROR(EINTR); + return ret; } static int udp_write(URLContext *h, const uint8_t *buf, int size) @@ -1079,7 +1016,7 @@ static int udp_write(URLContext *h, const uint8_t *buf, int size) Here we can't know on which packet error was, but it needs to know that error exists. */ if (s->circular_buffer_error<0) { - int err=s->circular_buffer_error; + int err = s->circular_buffer_error; pthread_mutex_unlock(&s->mutex); return err; } @@ -1133,8 +1070,17 @@ static int udp_close(URLContext *h) if (s->thread_started) { int ret; // Cancel only read, as write has been signaled as success to the user - if (h->flags & AVIO_FLAG_READ) + if (h->flags & AVIO_FLAG_READ) { +#ifdef _WIN32 + /* recvfrom() is not a cancellation point for win32, so we shutdown + * the socket and abort pending IO, subsequent recvfrom() calls + * will fail with WSAESHUTDOWN causing the thread to exit. */ + shutdown(s->udp_fd, SD_RECEIVE); + CancelIoEx((HANDLE)(SOCKET)s->udp_fd, NULL); +#else pthread_cancel(s->circular_buffer_thread); +#endif + } ret = pthread_join(s->circular_buffer_thread, NULL); if (ret != 0) av_log(h, AV_LOG_ERROR, "pthread_join(): %s\n", strerror(ret)); @@ -1144,6 +1090,7 @@ static int udp_close(URLContext *h) #endif closesocket(s->udp_fd); av_fifo_freep(&s->fifo); + ff_ip_reset_filters(&s->filters); return 0; }