Skip to content

Commit

Permalink
obs-outputs: Fix librtmp IP bind / resolve behavior
Browse files Browse the repository at this point in the history
Fixes a case where OBS would prefer trying to connect
to an IPv4 address even though you had bound it to an
IPv6 interface for example.
  • Loading branch information
notr1ch committed Nov 30, 2016
1 parent 532c613 commit 7df46d4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
16 changes: 10 additions & 6 deletions plugins/obs-outputs/librtmp/rtmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ int RTMP_AddStream(RTMP *r, const char *playpath)
}

static int
add_addr_info(struct sockaddr_storage *service, socklen_t *addrlen, AVal *host, int port)
add_addr_info(struct sockaddr_storage *service, socklen_t *addrlen, AVal *host, int port, socklen_t addrlen_hint)
{
char *hostname;
int ret = TRUE;
Expand Down Expand Up @@ -698,7 +698,7 @@ add_addr_info(struct sockaddr_storage *service, socklen_t *addrlen, AVal *host,
// prefer ipv4 results, since lots of ISPs have broken ipv6 connectivity
for (ptr = result; ptr != NULL; ptr = ptr->ai_next)
{
if (ptr->ai_family == AF_INET)
if (ptr->ai_family == AF_INET && (!addrlen_hint || ptr->ai_addrlen == addrlen_hint))
{
memcpy(service, ptr->ai_addr, ptr->ai_addrlen);
*addrlen = (socklen_t)ptr->ai_addrlen;
Expand All @@ -710,7 +710,7 @@ add_addr_info(struct sockaddr_storage *service, socklen_t *addrlen, AVal *host,
{
for (ptr = result; ptr != NULL; ptr = ptr->ai_next)
{
if (ptr->ai_family == AF_INET6)
if (ptr->ai_family == AF_INET6 && (!addrlen_hint || ptr->ai_addrlen == addrlen_hint))
{
memcpy(service, ptr->ai_addr, ptr->ai_addrlen);
*addrlen = (socklen_t)ptr->ai_addrlen;
Expand Down Expand Up @@ -905,6 +905,7 @@ RTMP_Connect(RTMP *r, RTMPPacket *cp)
#endif
struct sockaddr_storage service;
socklen_t addrlen = 0;
socklen_t addrlen_hint = 0;
if (!r->Link.hostname.av_len)
return FALSE;

Expand All @@ -920,16 +921,19 @@ RTMP_Connect(RTMP *r, RTMPPacket *cp)

memset(&service, 0, sizeof(service));

if (r->m_bindIP.addrLen)
addrlen_hint = r->m_bindIP.addrLen;

if (r->Link.socksport)
{
/* Connect via SOCKS */
if (!add_addr_info(&service, &addrlen, &r->Link.sockshost, r->Link.socksport))
if (!add_addr_info(&service, &addrlen, &r->Link.sockshost, r->Link.socksport, addrlen_hint))
return FALSE;
}
else
{
/* Connect directly */
if (!add_addr_info(&service, &addrlen, &r->Link.hostname, r->Link.port))
if (!add_addr_info(&service, &addrlen, &r->Link.hostname, r->Link.port, addrlen_hint))
return FALSE;
}

Expand All @@ -949,7 +953,7 @@ SocksNegotiate(RTMP *r)
socklen_t addrlen = 0;
memset(&service, 0, sizeof(service));

add_addr_info(&service, &addrlen, &r->Link.hostname, r->Link.port);
add_addr_info(&service, &addrlen, &r->Link.hostname, r->Link.port, 0);

// not doing IPv6 socks
if (service.ss_family == AF_INET6)
Expand Down
6 changes: 4 additions & 2 deletions plugins/obs-outputs/rtmp-stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,10 @@ static int try_connect(struct rtmp_stream *stream)
bool success = netif_str_to_addr(&stream->rtmp.m_bindIP.addr,
&stream->rtmp.m_bindIP.addrLen,
stream->bind_ip.array);
if (success)
info("Binding to IP");
if (success) {
info("Binding to IPv%d", (stream->rtmp.m_bindIP.addrLen ==
sizeof(struct sockaddr_in6) ? 6 : 4));
}
}

RTMP_AddStream(&stream->rtmp, stream->key.array);
Expand Down

0 comments on commit 7df46d4

Please sign in to comment.