@@ -264,6 +264,7 @@ static gboolean ipv6_disabled = FALSE;
264
264
static janus_callbacks * gateway = NULL ;
265
265
266
266
static char * local_ip = NULL , * sdp_ip = NULL ;
267
+ static janus_network_address janus_network_local_ip = { 0 };
267
268
#define DEFAULT_RTP_RANGE_MIN 10000
268
269
#define DEFAULT_RTP_RANGE_MAX 60000
269
270
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) {
688
689
689
690
janus_config_category * config_general = janus_config_get_create (config , NULL , janus_config_type_category , "general" );
690
691
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 ) {
692
693
/* Verify that the address is valid */
693
694
struct ifaddrs * ifas = NULL ;
694
695
janus_network_address iface ;
@@ -711,11 +712,32 @@ int janus_nosip_init(janus_callbacks *callback, const char *config_path) {
711
712
}
712
713
713
714
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 ) {
715
716
sdp_ip = g_strdup (item -> value );
716
717
JANUS_LOG (LOG_VERB , "IP to advertise in SDP: %s\n" , sdp_ip );
717
718
}
718
719
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
+
719
741
item = janus_config_get (config , config_general , janus_config_type_item , "rtp_port_range" );
720
742
if (item && item -> value ) {
721
743
/* Split in min and max port */
@@ -794,19 +816,29 @@ int janus_nosip_init(janus_callbacks *callback, const char *config_path) {
794
816
/* This is the callback we'll need to invoke to contact the Janus core */
795
817
gateway = callback ;
796
818
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 ) {
804
824
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;
810
842
}
811
843
812
844
g_atomic_int_set (& initialized , 1 );
@@ -1936,7 +1968,7 @@ char *janus_nosip_sdp_manipulate(janus_nosip_session *session, janus_sdp *sdp, g
1936
1968
JANUS_LOG (LOG_VERB , "Setting protocol to %s\n" , session -> media .require_srtp ? "RTP/SAVP" : "RTP/AVP" );
1937
1969
if (sdp -> c_addr ) {
1938
1970
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 );
1940
1972
}
1941
1973
int opusred_pt = answer ? janus_sdp_get_opusred_pt (sdp , -1 ) : -1 ;
1942
1974
GList * temp = sdp -> m_lines ;
@@ -1998,20 +2030,23 @@ char *janus_nosip_sdp_manipulate(janus_nosip_session *session, janus_sdp *sdp, g
1998
2030
}
1999
2031
2000
2032
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 );
2001
2036
struct sockaddr_storage rtp_address = { 0 };
2002
2037
if (!ipv6_disabled ) {
2003
2038
struct sockaddr_in6 * addr = (struct sockaddr_in6 * )& rtp_address ;
2004
2039
addr -> sin6_family = AF_INET6 ;
2005
2040
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 ;
2007
2042
} else {
2008
2043
struct sockaddr_in * addr = (struct sockaddr_in * )& rtp_address ;
2009
2044
addr -> sin_family = AF_INET ;
2010
2045
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 ;
2012
2047
}
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 ) );
2015
2050
return -1 ;
2016
2051
}
2017
2052
return 0 ;
@@ -2023,6 +2058,9 @@ static int janus_nosip_allocate_port_pair(gboolean video, int fds[2], int ports[
2023
2058
uint16_t rtp_port_start = rtp_port_next ;
2024
2059
gboolean rtp_port_wrap = FALSE;
2025
2060
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
+
2026
2064
int rtp_fd = -1 , rtcp_fd = -1 ;
2027
2065
while (1 ) {
2028
2066
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[
2031
2069
break ;
2032
2070
}
2033
2071
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 );
2035
2073
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 ) {
2037
2075
JANUS_LOG (LOG_WARN , "Error setting v6only to false on RTP socket (error=%s)\n" ,
2038
2076
g_strerror (errno ));
2039
2077
}
@@ -2056,8 +2094,8 @@ static int janus_nosip_allocate_port_pair(gboolean video, int fds[2], int ports[
2056
2094
}
2057
2095
if (rtcp_fd == -1 ) {
2058
2096
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 ) {
2061
2099
JANUS_LOG (LOG_WARN , "Error setting v6only to false on RTP socket (error=%s)\n" ,
2062
2100
g_strerror (errno ));
2063
2101
}
0 commit comments