Skip to content

Commit f74b313

Browse files
committed
Small tweaks to #3263, and updated NoSIP plugin with the same changes
1 parent d9a5853 commit f74b313

File tree

2 files changed

+68
-26
lines changed

2 files changed

+68
-26
lines changed

src/plugins/janus_nosip.c

+61-23
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ static gboolean ipv6_disabled = FALSE;
264264
static janus_callbacks *gateway = NULL;
265265

266266
static char *local_ip = NULL, *sdp_ip = NULL;
267+
static janus_network_address janus_network_local_ip = { 0 };
267268
#define DEFAULT_RTP_RANGE_MIN 10000
268269
#define DEFAULT_RTP_RANGE_MAX 60000
269270
static uint16_t rtp_range_min = DEFAULT_RTP_RANGE_MIN;
@@ -688,7 +689,7 @@ int janus_nosip_init(janus_callbacks *callback, const char *config_path) {
688689

689690
janus_config_category *config_general = janus_config_get_create(config, NULL, janus_config_type_category, "general");
690691
janus_config_item *item = janus_config_get(config, config_general, janus_config_type_item, "local_ip");
691-
if(item && item->value) {
692+
if(item && item->value && strlen(item->value) > 0) {
692693
/* Verify that the address is valid */
693694
struct ifaddrs *ifas = NULL;
694695
janus_network_address iface;
@@ -711,11 +712,32 @@ int janus_nosip_init(janus_callbacks *callback, const char *config_path) {
711712
}
712713

713714
item = janus_config_get(config, config_general, janus_config_type_item, "sdp_ip");
714-
if(item && item->value) {
715+
if(item && item->value && strlen(item->value) > 0) {
715716
sdp_ip = g_strdup(item->value);
716717
JANUS_LOG(LOG_VERB, "IP to advertise in SDP: %s\n", sdp_ip);
717718
}
718719

720+
/* Make sure both IPs are valid, if provided */
721+
janus_network_address_nullify(&janus_network_local_ip);
722+
if(local_ip) {
723+
if(janus_network_string_to_address(janus_network_query_options_any_ip, local_ip, &janus_network_local_ip) != 0) {
724+
JANUS_LOG(LOG_ERR, "Invalid local media IP address [%s]...\n", local_ip);
725+
return -1;
726+
}
727+
if((janus_network_local_ip.family == AF_INET && janus_network_local_ip.ipv4.s_addr == INADDR_ANY) ||
728+
(janus_network_local_ip.family == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED(&janus_network_local_ip.ipv6))) {
729+
janus_network_address_nullify(&janus_network_local_ip);
730+
}
731+
}
732+
JANUS_LOG(LOG_VERB, "Binding media address set to [%s]...\n", janus_network_address_is_null(&janus_network_local_ip) ? "any" : local_ip);
733+
if(!sdp_ip) {
734+
char *ip = janus_network_address_is_null(&janus_network_local_ip) ? local_ip : local_ip;
735+
if(ip) {
736+
sdp_ip = g_strdup(ip);
737+
JANUS_LOG(LOG_VERB, "IP to advertise in SDP: %s\n", sdp_ip);
738+
}
739+
}
740+
719741
item = janus_config_get(config, config_general, janus_config_type_item, "rtp_port_range");
720742
if(item && item->value) {
721743
/* Split in min and max port */
@@ -794,19 +816,29 @@ int janus_nosip_init(janus_callbacks *callback, const char *config_path) {
794816
/* This is the callback we'll need to invoke to contact the Janus core */
795817
gateway = callback;
796818

797-
/* Finally, let's check if IPv6 is disabled, as we may need to know for RTP/RTCP sockets */
798-
int fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
799-
if(fd <= 0) {
800-
ipv6_disabled = TRUE;
801-
} else {
802-
int v6only = 0;
803-
if(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)) != 0)
819+
if(janus_network_address_is_null(&janus_network_local_ip) ||
820+
janus_network_local_ip.family == AF_INET6) {
821+
/* Finally, let's check if IPv6 is disabled, as we may need to know for RTP/RTCP sockets */
822+
int fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
823+
if(fd <= 0) {
804824
ipv6_disabled = TRUE;
805-
}
806-
if(fd > 0)
807-
close(fd);
808-
if(ipv6_disabled) {
809-
JANUS_LOG(LOG_WARN, "IPv6 disabled, will only use IPv4 for RTP/RTCP sockets (NoSIP)\n");
825+
} else {
826+
int v6only = 0;
827+
if(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)) != 0)
828+
ipv6_disabled = TRUE;
829+
}
830+
if(fd > 0)
831+
close(fd);
832+
if(ipv6_disabled) {
833+
if(!janus_network_address_is_null(&janus_network_local_ip)) {
834+
JANUS_LOG(LOG_ERR, "IPv6 disabled and local media address is IPv6...\n");
835+
return -1;
836+
}
837+
JANUS_LOG(LOG_WARN, "IPv6 disabled, will only use IPv4 for RTP/RTCP sockets (SIP)\n");
838+
}
839+
} else if(janus_network_local_ip.family == AF_INET) {
840+
/* Disable if we have a specified IPv4 address for RTP/RTCP sockets */
841+
ipv6_disabled = TRUE;
810842
}
811843

812844
g_atomic_int_set(&initialized, 1);
@@ -1936,7 +1968,7 @@ char *janus_nosip_sdp_manipulate(janus_nosip_session *session, janus_sdp *sdp, g
19361968
JANUS_LOG(LOG_VERB, "Setting protocol to %s\n", session->media.require_srtp ? "RTP/SAVP" : "RTP/AVP");
19371969
if(sdp->c_addr) {
19381970
g_free(sdp->c_addr);
1939-
sdp->c_addr = g_strdup(sdp_ip ? sdp_ip : local_ip);
1971+
sdp->c_addr = g_strdup(sdp_ip);
19401972
}
19411973
int opusred_pt = answer ? janus_sdp_get_opusred_pt(sdp, -1) : -1;
19421974
GList *temp = sdp->m_lines;
@@ -1998,20 +2030,23 @@ char *janus_nosip_sdp_manipulate(janus_nosip_session *session, janus_sdp *sdp, g
19982030
}
19992031

20002032
static int janus_nosip_bind_socket(int fd, int port) {
2033+
gboolean use_ipv6_address_family = !ipv6_disabled &&
2034+
(janus_network_address_is_null(&janus_network_local_ip) || janus_network_local_ip.family == AF_INET6);
2035+
socklen_t addrlen = use_ipv6_address_family? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
20012036
struct sockaddr_storage rtp_address = { 0 };
20022037
if(!ipv6_disabled) {
20032038
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)&rtp_address;
20042039
addr->sin6_family = AF_INET6;
20052040
addr->sin6_port = htons(port);
2006-
addr->sin6_addr = in6addr_any;
2041+
addr->sin6_addr = janus_network_address_is_null(&janus_network_local_ip) ? in6addr_any : janus_network_local_ip.ipv6;
20072042
} else {
20082043
struct sockaddr_in *addr = (struct sockaddr_in *)&rtp_address;
20092044
addr->sin_family = AF_INET;
20102045
addr->sin_port = htons(port);
2011-
addr->sin_addr.s_addr = INADDR_ANY;
2046+
addr->sin_addr.s_addr = janus_network_address_is_null(&janus_network_local_ip) ? INADDR_ANY : janus_network_local_ip.ipv4.s_addr;
20122047
}
2013-
if(bind(fd, (struct sockaddr *)(&rtp_address), sizeof(rtp_address)) < 0) {
2014-
JANUS_LOG(LOG_ERR, "Bind failed (port %d)\n", port);
2048+
if(bind(fd, (struct sockaddr *)(&rtp_address), addrlen) < 0) {
2049+
JANUS_LOG(LOG_ERR, "Bind failed (port %d), error (%s)\n", port, g_strerror(errno));
20152050
return -1;
20162051
}
20172052
return 0;
@@ -2023,6 +2058,9 @@ static int janus_nosip_allocate_port_pair(gboolean video, int fds[2], int ports[
20232058
uint16_t rtp_port_start = rtp_port_next;
20242059
gboolean rtp_port_wrap = FALSE;
20252060

2061+
gboolean use_ipv6_address_family = !ipv6_disabled &&
2062+
(janus_network_address_is_null(&janus_network_local_ip) || janus_network_local_ip.family == AF_INET6);
2063+
20262064
int rtp_fd = -1, rtcp_fd = -1;
20272065
while(1) {
20282066
if(rtp_port_wrap && rtp_port_next >= rtp_port_start) { /* Full range scanned */
@@ -2031,9 +2069,9 @@ static int janus_nosip_allocate_port_pair(gboolean video, int fds[2], int ports[
20312069
break;
20322070
}
20332071
if(rtp_fd == -1) {
2034-
rtp_fd = socket(!ipv6_disabled ? AF_INET6 : AF_INET, SOCK_DGRAM, 0);
2072+
rtp_fd = socket(use_ipv6_address_family ? AF_INET6 : AF_INET, SOCK_DGRAM, 0);
20352073
int v6only = 0;
2036-
if(!ipv6_disabled && rtp_fd != -1 && setsockopt(rtp_fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)) != 0) {
2074+
if(use_ipv6_address_family && rtp_fd != -1 && setsockopt(rtp_fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)) != 0) {
20372075
JANUS_LOG(LOG_WARN, "Error setting v6only to false on RTP socket (error=%s)\n",
20382076
g_strerror(errno));
20392077
}
@@ -2056,8 +2094,8 @@ static int janus_nosip_allocate_port_pair(gboolean video, int fds[2], int ports[
20562094
}
20572095
if(rtcp_fd == -1) {
20582096
int v6only = 0;
2059-
rtcp_fd = socket(!ipv6_disabled ? AF_INET6 : AF_INET, SOCK_DGRAM, 0);
2060-
if(!ipv6_disabled && rtcp_fd != -1 && setsockopt(rtcp_fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)) != 0) {
2097+
rtcp_fd = socket(use_ipv6_address_family ? AF_INET6 : AF_INET, SOCK_DGRAM, 0);
2098+
if(use_ipv6_address_family && rtcp_fd != -1 && setsockopt(rtcp_fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)) != 0) {
20612099
JANUS_LOG(LOG_WARN, "Error setting v6only to false on RTP socket (error=%s)\n",
20622100
g_strerror(errno));
20632101
}

src/plugins/janus_sip.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -2019,8 +2019,11 @@ int janus_sip_init(janus_callbacks *callback, const char *config_path) {
20192019
}
20202020
JANUS_LOG(LOG_VERB, "Binding media address set to [%s]...\n", janus_network_address_is_null(&janus_network_local_media_ip) ? "any" : local_media_ip);
20212021
if(!sdp_ip) {
2022-
sdp_ip = janus_network_address_is_null(&janus_network_local_media_ip) ? local_ip : local_media_ip;
2023-
JANUS_LOG(LOG_VERB, "IP to advertise in SDP: %s\n", sdp_ip);
2022+
char *ip = janus_network_address_is_null(&janus_network_local_media_ip) ? local_ip : local_media_ip;
2023+
if(ip) {
2024+
sdp_ip = g_strdup(ip);
2025+
JANUS_LOG(LOG_VERB, "IP to advertise in SDP: %s\n", sdp_ip);
2026+
}
20242027
}
20252028

20262029
/* Setup sofia */
@@ -6745,7 +6748,8 @@ static int janus_sip_allocate_local_ports(janus_sip_session *session, gboolean u
67456748
attempts--;
67466749
continue;
67476750
}
6748-
JANUS_LOG(LOG_VERB, "Audio RTCP listener bound to [%s]:%d(%d)\n", janus_network_address_is_null(&janus_network_local_media_ip) ?"any":local_media_ip, rtcp_port, session->media.audio_rtcp_fd);
6751+
JANUS_LOG(LOG_VERB, "Audio RTCP listener bound to [%s]:%d(%d)\n",
6752+
janus_network_address_is_null(&janus_network_local_media_ip) ? "any" : local_media_ip, rtcp_port, session->media.audio_rtcp_fd);
67496753
session->media.local_audio_rtp_port = rtp_port;
67506754
session->media.local_audio_rtcp_port = rtcp_port;
67516755
}

0 commit comments

Comments
 (0)