[FFmpeg-devel] [PATCH] libavformat/tcp: add local_addr/local_port for network option
Anton Khirnov
anton at khirnov.net
Wed Mar 1 16:44:53 EET 2023
Quoting jackarain (2023-02-28 16:44:20)
> -static void customize_fd(void *ctx, int fd)
> +static int customize_fd(void *ctx, int fd)
> {
> TCPContext *s = ctx;
> +
> + if (s->local_addr || s->local_port) {
> + struct addrinfo hints = { 0 }, *ai;
> + int ret;
> +
> + hints.ai_family = AF_UNSPEC;
> + hints.ai_socktype = SOCK_STREAM;
> +
> + ret = getaddrinfo(s->local_addr, s->local_port, &hints, &ai);
> + if (ret) {
> + av_log(ctx, AV_LOG_ERROR,
> + "Failed to getaddrinfo local addr: %s port: %s err: %s\n",
> + s->local_addr, s->local_port, gai_strerror(ret));
> + return ret;
> + } else {
nit: the else clause pointlessly adds an indentation level and serves no
useful purpose
> + struct addrinfo *cur_ai = ai;
> + while (cur_ai) {
> + ret = bind(fd, (struct sockaddr *)cur_ai->ai_addr, (int)cur_ai->ai_addrlen);
> + if (ret)
> + cur_ai = cur_ai->ai_next;
> + else
> + break;
> + }
> + freeaddrinfo(ai);
> +
> + if (ret) {
> + av_log(ctx, AV_LOG_ERROR,
> + "Failed to bind local addr: %s port: %s err: %s\n",
> + s->local_addr, s->local_port, gai_strerror(ret));
> + return ret;
> + }
> + }
> + }
> /* Set the socket's send or receive buffer sizes, if specified.
> If unspecified or setting fails, system default is used. */
> if (s->recv_buffer_size > 0) {
> @@ -97,6 +134,8 @@ static void customize_fd(void *ctx, int fd)
> }
> }
> #endif /* !HAVE_WINSOCK2_H */
> +
> + return 0;
> }
>
> /* return non zero if error */
> @@ -129,6 +168,14 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
> if (buf == endptr)
> s->listen = 1;
> }
> + if (av_find_info_tag(buf, sizeof(buf), "local_port", p)) {
> + av_freep(&s->local_port);
> + s->local_port = av_strndup(buf, strlen(buf));
This does memory allocation, so the result should be checked. Also, it
av_str_n_dup() gives you no advantages since you call strlen anyway.
Just use av_strdup(). Same below.
--
Anton Khirnov
More information about the ffmpeg-devel
mailing list