Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: jack <jack.wgm@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Cc: jack <jack.wgm@gmail.com>
Subject: [FFmpeg-devel] [PATCH] libavformat/tcp: add local_addr/local_port for network option
Date: Tue, 28 Feb 2023 18:16:40 +0800
Message-ID: <20230228101640.3337990-1-jack.wgm@gmail.com> (raw)

Signed-off-by: jack <jack.wgm@gmail.com>
---
 libavformat/tcp.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/libavformat/tcp.c b/libavformat/tcp.c
index a11ccbb913..598d61067e 100644
--- a/libavformat/tcp.c
+++ b/libavformat/tcp.c
@@ -36,6 +36,8 @@ typedef struct TCPContext {
     const AVClass *class;
     int fd;
     int listen;
+    int local_port;
+    char *local_addr;
     int open_timeout;
     int rw_timeout;
     int listen_timeout;
@@ -52,6 +54,8 @@ typedef struct TCPContext {
 #define E AV_OPT_FLAG_ENCODING_PARAM
 static const AVOption options[] = {
     { "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_INT,    { .i64 = -1 },      -1, INT_MAX, .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 },
     { "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 },
@@ -73,6 +77,39 @@ static const AVClass tcp_class = {
 static void customize_fd(void *ctx, int fd)
 {
     TCPContext *s = ctx;
+
+    if (s->local_addr) {
+        struct addrinfo hints = { 0 }, *ai;
+        int ret;
+        int port = 0;
+
+        hints.ai_family = AF_UNSPEC;
+        hints.ai_socktype = SOCK_STREAM;
+
+        if (s->local_port > 0)
+            port = s->local_port;
+
+        ret = getaddrinfo(s->local_addr, "0", &hints, &ai);
+        if (ret) {
+            av_log(ctx, AV_LOG_WARNING,
+               "Failed to bind local addr: %s port: %d err: %s\n",
+                s->local_addr, s->local_port, gai_strerror(ret));
+        } else {
+            if (ai->ai_family == AF_INET6) {
+                struct sockaddr_in6 * sockaddr_v6 = (struct sockaddr_in6 *)ai->ai_addr;
+                sockaddr_v6->sin6_port = htons(port);
+            } else {
+                struct sockaddr_in * sockaddr = (struct sockaddr_in *)ai->ai_addr;
+                sockaddr->sin_port = htons(port);
+            }
+            ret = bind(fd, (struct sockaddr *)ai->ai_addr, (int)ai->ai_addrlen);
+            if (ret) {
+                av_log(ctx, AV_LOG_WARNING,
+                "Failed to bind local addr: %s port: %d err: %s\n",
+                    s->local_addr, s->local_port, gai_strerror(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) {
@@ -129,6 +166,13 @@ 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)) {
+            s->local_port = strtol(buf, NULL, 10);
+        }
+        if (av_find_info_tag(buf, sizeof(buf), "local_addr", p)) {
+            av_freep(&s->local_addr);
+            s->local_addr = av_strndup(buf, strlen(buf));
+        }
         if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
             s->rw_timeout = strtol(buf, NULL, 10);
         }
-- 
2.39.2

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

             reply	other threads:[~2023-02-28 10:17 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-28 10:16 jack [this message]
2023-02-28 10:57 ` Nicolas George
2023-02-28 14:27   ` jackarain
2023-02-28 15:44 ` jackarain
2023-03-01 14:44   ` Anton Khirnov
2023-03-01 14:56     ` jackarain
2023-03-01 15:07     ` jackarain
2023-03-01 15:44       ` jackarain
2023-03-02 18:47         ` Rémi Denis-Courmont
2023-03-02 18:51           ` Nicolas George
2023-03-03  9:49             ` jackarain
2023-03-30 10:11               ` Anton Khirnov
2023-02-28 14:24 jackarain

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230228101640.3337990-1-jack.wgm@gmail.com \
    --to=jack.wgm@gmail.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git