| @@ -36,6 +36,8 @@ typedef struct TCPContext { · 35 unmodified lines |
| 36 | 36 | const AVClass *class; |
| 37 | 37 | int fd; |
| 38 | 38 | int listen; |
| 39 | + char *local_port; |
| 40 | + char *local_addr; |
| 39 | 41 | int open_timeout; |
| 40 | 42 | int rw_timeout; |
| 41 | 43 | int listen_timeout; |
| @@ -52,6 +54,8 @@ typedef struct TCPContext { · 10 unmodified lines |
| 52 | 54 | #define E AV_OPT_FLAG_ENCODING_PARAM |
| 53 | 55 | static const AVOption options[] = { |
| 54 | 56 | { "listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, .flags = D|E }, |
| 57 | + { "local_port", "Local port", OFFSET(local_port), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, .flags = D|E }, |
| 58 | + { "local_addr", "Local address", OFFSET(local_addr), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, .flags = D|E }, |
| 55 | 59 | { "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 }, |
| 56 | 60 | { "listen_timeout", "Connection awaiting timeout (in milliseconds)", OFFSET(listen_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E }, |
| 57 | 61 | { "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 = { · 12 unmodified lines |
| 70 | 74 | .version = LIBAVUTIL_VERSION_INT, |
| 71 | 75 | }; |
| 72 | 76 | |
| 73 | | −static void customize_fd(void *ctx, int fd) |
| 77 | +static int customize_fd(void *ctx, int fd, int family) |
| 74 | 78 | { |
| 75 | 79 | TCPContext *s = ctx; |
| 80 | + |
| 81 | + if (s->local_addr || s->local_port) { |
| 82 | + struct addrinfo hints = { 0 }, *ai, *cur_ai; |
| 83 | + int ret; |
| 84 | + |
| 85 | + hints.ai_family = family; |
| 86 | + hints.ai_socktype = SOCK_STREAM; |
| 87 | + |
| 88 | + ret = getaddrinfo(s->local_addr, s->local_port, &hints, &ai); |
| 89 | + if (ret) { |
| 90 | + av_log(ctx, AV_LOG_ERROR, |
| 91 | + "Failed to getaddrinfo local addr: %s port: %s err: %s\n", |
| 92 | + s->local_addr, s->local_port, gai_strerror(ret)); |
| 93 | + return ret; |
| 94 | + } |
| 95 | + |
| 96 | + cur_ai = ai; |
| 97 | + while (cur_ai) { |
| 98 | + ret = bind(fd, (struct sockaddr *)cur_ai->ai_addr, (int)cur_ai->ai_addrlen); |
| 99 | + if (ret) |
| 100 | + cur_ai = cur_ai->ai_next; |
| 101 | + else |
| 102 | + break; |
| 103 | + } |
| 104 | + freeaddrinfo(ai); |
| 105 | + |
| 106 | + if (ret) { |
| 107 | + ff_log_net_error(ctx, AV_LOG_ERROR, "bind local failed"); |
| 108 | + return ret; |
| 109 | + } |
| 110 | + } |
| 76 | 111 | /* Set the socket's send or receive buffer sizes, if specified. |
| 77 | 112 | If unspecified or setting fails, system default is used. */ |
| 78 | 113 | if (s->recv_buffer_size > 0) { |
| @@ -97,6 +132,8 @@ static void customize_fd(void *ctx, int fd) · 18 unmodified lines |
| 97 | 132 | } |
| 98 | 133 | } |
| 99 | 134 | #endif /* !HAVE_WINSOCK2_H */ |
| 135 | + |
| 136 | + return 0; |
| 100 | 137 | } |
| 101 | 138 | |
| 102 | 139 | /* return non zero if error */ |
| @@ -129,6 +166,18 @@ static int tcp_open(URLContext *h, const char *uri, int flags) · 26 unmodified lines |
| 129 | 166 | if (buf == endptr) |
| 130 | 167 | s->listen = 1; |
| 131 | 168 | } |
| 169 | + if (av_find_info_tag(buf, sizeof(buf), "local_port", p)) { |
| 170 | + av_freep(&s->local_port); |
| 171 | + s->local_port = av_strdup(buf); |
| 172 | + if (!s->local_addr) |
| 173 | + return AVERROR(ENOMEM); |
| 174 | + } |
| 175 | + if (av_find_info_tag(buf, sizeof(buf), "local_addr", p)) { |
| 176 | + av_freep(&s->local_addr); |
| 177 | + s->local_addr = av_strdup(buf); |
| 178 | + if (!s->local_addr) |
| 179 | + return AVERROR(ENOMEM); |
| 180 | + } |
| 132 | 181 | if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) { |
| 133 | 182 | s->rw_timeout = strtol(buf, NULL, 10); |
| 134 | 183 | } |
| @@ -183,7 +232,7 @@ static int tcp_open(URLContext *h, const char *uri, int flags) · 48 unmodified lines |
| 183 | 232 | } |
| 184 | 233 | if (fd < 0) |
| 185 | 234 | goto fail1; |
| 186 | | − customize_fd(s, fd); |
| 235 | + customize_fd(s, fd, cur_ai->ai_family); |
| 187 | 236 | } |
| 188 | 237 | |
| 189 | 238 | if (s->listen == 2) { |