[FFmpeg-devel] [PATCH v6] avformat/udp: return the error code instead of generic EIO

Nicolas George george at nsup.org
Tue Jan 12 16:36:00 EET 2021


lance.lmwang at gmail.com (12021-01-12):
> From: Limin Wang <lance.lmwang at gmail.com>
> 
> Signed-off-by: Limin Wang <lance.lmwang at gmail.com>
> ---
>  libavformat/udp.c | 58 ++++++++++++++++++++++++++++++++++---------------------
>  1 file changed, 36 insertions(+), 22 deletions(-)
> 
> diff --git a/libavformat/udp.c b/libavformat/udp.c
> index 13c346a..42e4563 100644
> --- a/libavformat/udp.c
> +++ b/libavformat/udp.c
> @@ -165,7 +165,7 @@ static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
>      if (addr->sa_family == AF_INET) {
>          if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
>              ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL)");
> -            return -1;
> +            return ff_neterrno();
>          }
>      }
>  #endif
> @@ -173,7 +173,7 @@ static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
>      if (addr->sa_family == AF_INET6) {
>          if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
>              ff_log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS)");
> -            return -1;
> +            return ff_neterrno();
>          }
>      }
>  #endif
> @@ -193,7 +193,7 @@ static int udp_join_multicast_group(int sockfd, struct sockaddr *addr,struct soc
>              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;
> +            return ff_neterrno();
>          }
>      }
>  #endif
> @@ -206,7 +206,7 @@ static int udp_join_multicast_group(int sockfd, struct sockaddr *addr,struct soc
>          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;
> +            return ff_neterrno();
>          }
>      }
>  #endif
> @@ -633,6 +633,7 @@ static int udp_open(URLContext *h, const char *uri, int flags)
>      char buf[256];
>      struct sockaddr_storage my_addr;
>      socklen_t len;

> +    int ret = AVERROR(EIO);

ret should be left uninited, so that the compiler can warn you if there
is a code path where you return it without setting it.

>  
>      h->is_streamed = 1;
>  
> @@ -641,12 +642,12 @@ static int udp_open(URLContext *h, const char *uri, int flags)
>          s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_RX_BUF_SIZE;
>  
>      if (s->sources) {
> -        if (ff_ip_parse_sources(h, s->sources, &s->filters) < 0)
> +        if ((ret = ff_ip_parse_sources(h, s->sources, &s->filters)) < 0)
>              goto fail;
>      }
>  
>      if (s->block) {
> -        if (ff_ip_parse_blocks(h, s->block, &s->filters) < 0)
> +        if ((ret = ff_ip_parse_blocks(h, s->block, &s->filters)) < 0)
>              goto fail;
>      }
>  
> @@ -712,11 +713,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 (ff_ip_parse_sources(h, buf, &s->filters) < 0)
> +            if ((ret = ff_ip_parse_sources(h, buf, &s->filters)) < 0)
>                  goto fail;
>          }
>          if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
> -            if (ff_ip_parse_blocks(h, buf, &s->filters) < 0)
> +            if ((ret = ff_ip_parse_blocks(h, buf, &s->filters)) < 0)
>                  goto fail;
>          }
>          if (!is_output && av_find_info_tag(buf, sizeof(buf), "timeout", p))
> @@ -742,7 +743,7 @@ static int udp_open(URLContext *h, const char *uri, int flags)
>          if (!(flags & AVIO_FLAG_READ))
>              goto fail;
>      } else {
> -        if (ff_udp_set_remote_url(h, uri) < 0)
> +        if ((ret = ff_udp_set_remote_url(h, uri)) < 0)
>              goto fail;
>      }
>  
> @@ -763,15 +764,22 @@ static int udp_open(URLContext *h, const char *uri, int flags)
>       */
>      if (s->reuse_socket > 0 || (s->is_multicast && s->reuse_socket < 0)) {
>          s->reuse_socket = 1;
> -        if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
> +        if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0) {
> +            ret = ff_neterrno();
>              goto fail;
> +        }
>      }
>  
>      if (s->is_broadcast) {
>  #ifdef SO_BROADCAST
> -        if (setsockopt (udp_fd, SOL_SOCKET, SO_BROADCAST, &(s->is_broadcast), sizeof(s->is_broadcast)) != 0)
> +        if (setsockopt (udp_fd, SOL_SOCKET, SO_BROADCAST, &(s->is_broadcast), sizeof(s->is_broadcast)) != 0) {
> +            ret = ff_neterrno();
> +            goto fail;
> +        }
> +#else

> +        ret = AVERROR(EINVAL);

ENOSYS

> +        goto fail;
>  #endif
> -           goto fail;
>      }
>  
>      /* Set the checksum coverage for UDP-Lite (RFC 3828) for sending and receiving.
> @@ -788,8 +796,10 @@ static int udp_open(URLContext *h, const char *uri, int flags)
>  
>      if (dscp >= 0) {
>          dscp <<= 2;
> -        if (setsockopt (udp_fd, IPPROTO_IP, IP_TOS, &dscp, sizeof(dscp)) != 0)
> +        if (setsockopt (udp_fd, IPPROTO_IP, IP_TOS, &dscp, sizeof(dscp)) != 0) {
> +            ret = ff_neterrno();
>              goto fail;
> +        }
>      }
>  
>      /* If multicast, try binding the multicast address first, to avoid
> @@ -804,6 +814,7 @@ static int udp_open(URLContext *h, const char *uri, int flags)
>      /* the bind is needed to give a port to the socket now */
>      if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) {
>          ff_log_net_error(h, AV_LOG_ERROR, "bind failed");
> +        ret = ff_neterrno();
>          goto fail;
>      }
>  
> @@ -814,28 +825,28 @@ static int udp_open(URLContext *h, const char *uri, int flags)
>      if (s->is_multicast) {
>          if (h->flags & AVIO_FLAG_WRITE) {
>              /* output */
> -            if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
> +            if ((ret = udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr)) < 0)
>                  goto fail;
>          }
>          if (h->flags & AVIO_FLAG_READ) {
>              /* input */
>              if (s->filters.nb_include_addrs) {
> -                if (udp_set_multicast_sources(h, udp_fd,
> +                if ((ret = udp_set_multicast_sources(h, udp_fd,
>                                                (struct sockaddr *)&s->dest_addr,
>                                                s->dest_addr_len, &s->local_addr_storage,
>                                                s->filters.include_addrs,
> -                                              s->filters.nb_include_addrs, 1) < 0)
> +                                              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)
> +                if ((ret = udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr,(struct sockaddr *)&s->local_addr_storage)) < 0)
>                      goto fail;
>              }
>              if (s->filters.nb_exclude_addrs) {
> -                if (udp_set_multicast_sources(h, udp_fd,
> +                if ((ret = udp_set_multicast_sources(h, udp_fd,
>                                                (struct sockaddr *)&s->dest_addr,
>                                                s->dest_addr_len, &s->local_addr_storage,
>                                                s->filters.exclude_addrs,
> -                                              s->filters.nb_exclude_addrs, 0) < 0)
> +                                              s->filters.nb_exclude_addrs, 0)) < 0)
>                      goto fail;
>              }
>          }
> @@ -846,6 +857,7 @@ static int udp_open(URLContext *h, const char *uri, int flags)
>          tmp = s->buffer_size;
>          if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
>              ff_log_net_error(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF)");
> +            ret = ff_neterrno();
>              goto fail;
>          }
>      } else {
> @@ -869,6 +881,7 @@ static int udp_open(URLContext *h, const char *uri, int flags)
>      if (s->is_connected) {
>          if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
>              ff_log_net_error(h, AV_LOG_ERROR, "connect");
> +            ret = ff_neterrno();
>              goto fail;
>          }
>      }
> @@ -888,23 +901,24 @@ static int udp_open(URLContext *h, const char *uri, int flags)
>      }
>  
>      if ((!is_output && s->circular_buffer_size) || (is_output && s->bitrate && s->circular_buffer_size)) {
> -        int ret;
> -
>          /* start the task going */
>          s->fifo = av_fifo_alloc(s->circular_buffer_size);
>          ret = pthread_mutex_init(&s->mutex, NULL);
>          if (ret != 0) {
>              av_log(h, AV_LOG_ERROR, "pthread_mutex_init failed : %s\n", strerror(ret));
> +            ret =  AVERROR(ret);
>              goto fail;
>          }
>          ret = pthread_cond_init(&s->cond, NULL);
>          if (ret != 0) {
>              av_log(h, AV_LOG_ERROR, "pthread_cond_init failed : %s\n", strerror(ret));
> +            ret =  AVERROR(ret);
>              goto cond_fail;
>          }
>          ret = pthread_create(&s->circular_buffer_thread, NULL, is_output?circular_buffer_task_tx:circular_buffer_task_rx, h);
>          if (ret != 0) {
>              av_log(h, AV_LOG_ERROR, "pthread_create failed : %s\n", strerror(ret));
> +            ret =  AVERROR(ret);
>              goto thread_fail;
>          }
>          s->thread_started = 1;
> @@ -923,7 +937,7 @@ static int udp_open(URLContext *h, const char *uri, int flags)
>          closesocket(udp_fd);
>      av_fifo_freep(&s->fifo);
>      ff_ip_reset_filters(&s->filters);
> -    return AVERROR(EIO);
> +    return ret;
>  }
>  
>  static int udplite_open(URLContext *h, const char *uri, int flags)

Thanks for your efforts.

Regards,

-- 
  Nicolas George
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20210112/85134ca7/attachment.sig>


More information about the ffmpeg-devel mailing list