libavformat/tcp: add local_addr/local_port for network option

Signed-off-by: jackarain <jack.wgm@gmail.com>
Signed-off-by: Anton Khirnov <anton@khirnov.net>
This commit is contained in:
jackarain 2023-03-03 17:49:26 +08:00 committed by Anton Khirnov
parent 0684e58886
commit 4d216654ca
5 changed files with 69 additions and 8 deletions

View File

@ -1882,6 +1882,12 @@ The list of supported options follows.
Listen for an incoming connection. 0 disables listen, 1 enables listen in Listen for an incoming connection. 0 disables listen, 1 enables listen in
single client mode, 2 enables listen in multi-client mode. Default value is 0. single client mode, 2 enables listen in multi-client mode. Default value is 0.
@item local_addr=@var{addr}
Local IP address of a network interface used for tcp socket connect.
@item local_port=@var{port}
Local port used for tcp socket connect.
@item timeout=@var{microseconds} @item timeout=@var{microseconds}
Set raise error timeout, expressed in microseconds. Set raise error timeout, expressed in microseconds.

View File

@ -356,7 +356,7 @@ struct ConnectionAttempt {
static int start_connect_attempt(struct ConnectionAttempt *attempt, static int start_connect_attempt(struct ConnectionAttempt *attempt,
struct addrinfo **ptr, int timeout_ms, struct addrinfo **ptr, int timeout_ms,
URLContext *h, URLContext *h,
void (*customize_fd)(void *, int), void *customize_ctx) int (*customize_fd)(void *, int, int), void *customize_ctx)
{ {
struct addrinfo *ai = *ptr; struct addrinfo *ai = *ptr;
int ret; int ret;
@ -371,8 +371,14 @@ static int start_connect_attempt(struct ConnectionAttempt *attempt,
ff_socket_nonblock(attempt->fd, 1); ff_socket_nonblock(attempt->fd, 1);
if (customize_fd) if (customize_fd) {
customize_fd(customize_ctx, attempt->fd); ret = customize_fd(customize_ctx, attempt->fd, ai->ai_family);
if (ret) {
closesocket(attempt->fd);
attempt->fd = -1;
return ret;
}
}
while ((ret = connect(attempt->fd, ai->ai_addr, ai->ai_addrlen))) { while ((ret = connect(attempt->fd, ai->ai_addr, ai->ai_addrlen))) {
ret = ff_neterrno(); ret = ff_neterrno();
@ -402,7 +408,7 @@ static int start_connect_attempt(struct ConnectionAttempt *attempt,
int ff_connect_parallel(struct addrinfo *addrs, int timeout_ms_per_address, int ff_connect_parallel(struct addrinfo *addrs, int timeout_ms_per_address,
int parallel, URLContext *h, int *fd, int parallel, URLContext *h, int *fd,
void (*customize_fd)(void *, int), void *customize_ctx) int (*customize_fd)(void *, int, int), void *customize_ctx)
{ {
struct ConnectionAttempt attempts[3]; struct ConnectionAttempt attempts[3];
struct pollfd pfd[3]; struct pollfd pfd[3];

View File

@ -336,6 +336,6 @@ void ff_log_net_error(void *ctx, int level, const char* prefix);
*/ */
int ff_connect_parallel(struct addrinfo *addrs, int timeout_ms_per_address, int ff_connect_parallel(struct addrinfo *addrs, int timeout_ms_per_address,
int parallel, URLContext *h, int *fd, int parallel, URLContext *h, int *fd,
void (*customize_fd)(void *, int), void *customize_ctx); int (*customize_fd)(void *, int, int), void *customize_ctx);
#endif /* AVFORMAT_NETWORK_H */ #endif /* AVFORMAT_NETWORK_H */

View File

@ -36,6 +36,8 @@ typedef struct TCPContext {
const AVClass *class; const AVClass *class;
int fd; int fd;
int listen; int listen;
char *local_port;
char *local_addr;
int open_timeout; int open_timeout;
int rw_timeout; int rw_timeout;
int listen_timeout; int listen_timeout;
@ -52,6 +54,8 @@ typedef struct TCPContext {
#define E AV_OPT_FLAG_ENCODING_PARAM #define E AV_OPT_FLAG_ENCODING_PARAM
static const AVOption options[] = { static const AVOption options[] = {
{ "listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, .flags = D|E }, { "listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, .flags = D|E },
{ "local_port", "Local port", OFFSET(local_port), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, .flags = D|E },
{ "local_addr", "Local address", OFFSET(local_addr), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, .flags = D|E },
{ "timeout", "set timeout (in microseconds) of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E }, { "timeout", "set timeout (in microseconds) of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
{ "listen_timeout", "Connection awaiting timeout (in milliseconds)", OFFSET(listen_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E }, { "listen_timeout", "Connection awaiting timeout (in milliseconds)", OFFSET(listen_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
{ "send_buffer_size", "Socket send buffer size (in bytes)", OFFSET(send_buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E }, { "send_buffer_size", "Socket send buffer size (in bytes)", OFFSET(send_buffer_size), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
@ -70,9 +74,40 @@ static const AVClass tcp_class = {
.version = LIBAVUTIL_VERSION_INT, .version = LIBAVUTIL_VERSION_INT,
}; };
static void customize_fd(void *ctx, int fd) static int customize_fd(void *ctx, int fd, int family)
{ {
TCPContext *s = ctx; TCPContext *s = ctx;
if (s->local_addr || s->local_port) {
struct addrinfo hints = { 0 }, *ai, *cur_ai;
int ret;
hints.ai_family = family;
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;
}
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) {
ff_log_net_error(ctx, AV_LOG_ERROR, "bind local failed");
return ret;
}
}
/* Set the socket's send or receive buffer sizes, if specified. /* Set the socket's send or receive buffer sizes, if specified.
If unspecified or setting fails, system default is used. */ If unspecified or setting fails, system default is used. */
if (s->recv_buffer_size > 0) { if (s->recv_buffer_size > 0) {
@ -97,6 +132,8 @@ static void customize_fd(void *ctx, int fd)
} }
} }
#endif /* !HAVE_WINSOCK2_H */ #endif /* !HAVE_WINSOCK2_H */
return 0;
} }
/* return non zero if error */ /* return non zero if error */
@ -129,6 +166,18 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
if (buf == endptr) if (buf == endptr)
s->listen = 1; s->listen = 1;
} }
if (av_find_info_tag(buf, sizeof(buf), "local_port", p)) {
av_freep(&s->local_port);
s->local_port = av_strdup(buf);
if (!s->local_addr)
return AVERROR(ENOMEM);
}
if (av_find_info_tag(buf, sizeof(buf), "local_addr", p)) {
av_freep(&s->local_addr);
s->local_addr = av_strdup(buf);
if (!s->local_addr)
return AVERROR(ENOMEM);
}
if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) { if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
s->rw_timeout = strtol(buf, NULL, 10); s->rw_timeout = strtol(buf, NULL, 10);
} }
@ -183,7 +232,7 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
} }
if (fd < 0) if (fd < 0)
goto fail1; goto fail1;
customize_fd(s, fd); customize_fd(s, fd, cur_ai->ai_family);
} }
if (s->listen == 2) { if (s->listen == 2) {

View File

@ -32,7 +32,7 @@
#include "version_major.h" #include "version_major.h"
#define LIBAVFORMAT_VERSION_MINOR 4 #define LIBAVFORMAT_VERSION_MINOR 4
#define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_MICRO 101
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
LIBAVFORMAT_VERSION_MINOR, \ LIBAVFORMAT_VERSION_MINOR, \