@@ -850,6 +850,7 @@ static struct janus_json_parameter sipmessage_parameters[] = {
850
850
/* Useful stuff */
851
851
static volatile gint initialized = 0 , stopping = 0 ;
852
852
static gboolean notify_events = TRUE;
853
+ static gboolean ipv6_disabled = FALSE;
853
854
static janus_callbacks * gateway = NULL ;
854
855
855
856
static char * local_ip = NULL , * sdp_ip = NULL , * local_media_ip = NULL ;
@@ -2019,6 +2020,21 @@ int janus_sip_init(janus_callbacks *callback, const char *config_path) {
2019
2020
/* This is the callback we'll need to invoke to contact the Janus core */
2020
2021
gateway = callback ;
2021
2022
2023
+ /* Finally, let's check if IPv6 is disabled, as we may need to know for RTP/RTCP sockets */
2024
+ int fd = socket (AF_INET6 , SOCK_DGRAM , IPPROTO_UDP );
2025
+ if (fd <= 0 ) {
2026
+ ipv6_disabled = TRUE;
2027
+ } else {
2028
+ int v6only = 0 ;
2029
+ if (setsockopt (fd , IPPROTO_IPV6 , IPV6_V6ONLY , & v6only , sizeof (v6only )) != 0 )
2030
+ ipv6_disabled = TRUE;
2031
+ }
2032
+ if (fd > 0 )
2033
+ close (fd );
2034
+ if (ipv6_disabled ) {
2035
+ JANUS_LOG (LOG_WARN , "IPv6 disabled, will only use IPv4 for RTP/RTCP sockets (SIP)\n" );
2036
+ }
2037
+
2022
2038
g_atomic_int_set (& initialized , 1 );
2023
2039
2024
2040
/* Launch the thread that will handle incoming messages */
@@ -6608,14 +6624,15 @@ static int janus_sip_allocate_local_ports(janus_sip_session *session, gboolean u
6608
6624
int attempts = 100 ; /* FIXME Don't retry forever */
6609
6625
if (session -> media .has_audio ) {
6610
6626
JANUS_LOG (LOG_VERB , "Allocating audio ports:\n" );
6611
- struct sockaddr_in6 audio_rtp_address , audio_rtcp_address ;
6627
+ struct sockaddr_storage audio_rtp_address , audio_rtcp_address ;
6612
6628
while (session -> media .local_audio_rtp_port == 0 || session -> media .local_audio_rtcp_port == 0 ) {
6613
6629
if (attempts == 0 ) /* Too many failures */
6614
6630
return -1 ;
6615
6631
if (session -> media .audio_rtp_fd == -1 ) {
6616
- session -> media .audio_rtp_fd = socket (AF_INET6 , SOCK_DGRAM , 0 );
6632
+ session -> media .audio_rtp_fd = socket (! ipv6_disabled ? AF_INET6 : AF_INET , SOCK_DGRAM , 0 );
6617
6633
int v6only = 0 ;
6618
- if (session -> media .audio_rtp_fd != -1 && setsockopt (session -> media .audio_rtp_fd , IPPROTO_IPV6 , IPV6_V6ONLY , & v6only , sizeof (v6only )) != 0 ) {
6634
+ if (!ipv6_disabled && session -> media .audio_rtp_fd != -1 &&
6635
+ setsockopt (session -> media .audio_rtp_fd , IPPROTO_IPV6 , IPV6_V6ONLY , & v6only , sizeof (v6only )) != 0 ) {
6619
6636
JANUS_LOG (LOG_WARN , "Error setting v6only to false on audio RTP socket (error=%s)\n" ,
6620
6637
g_strerror (errno ));
6621
6638
}
@@ -6630,9 +6647,10 @@ static int janus_sip_allocate_local_ports(janus_sip_session *session, gboolean u
6630
6647
}
6631
6648
}
6632
6649
if (session -> media .audio_rtcp_fd == -1 ) {
6633
- session -> media .audio_rtcp_fd = socket (AF_INET6 , SOCK_DGRAM , 0 );
6650
+ session -> media .audio_rtcp_fd = socket (! ipv6_disabled ? AF_INET6 : AF_INET , SOCK_DGRAM , 0 );
6634
6651
int v6only = 0 ;
6635
- if (session -> media .audio_rtcp_fd != -1 && setsockopt (session -> media .audio_rtcp_fd , IPPROTO_IPV6 , IPV6_V6ONLY , & v6only , sizeof (v6only )) != 0 ) {
6652
+ if (!ipv6_disabled && session -> media .audio_rtcp_fd != -1 &&
6653
+ setsockopt (session -> media .audio_rtcp_fd , IPPROTO_IPV6 , IPV6_V6ONLY , & v6only , sizeof (v6only )) != 0 ) {
6636
6654
JANUS_LOG (LOG_WARN , "Error setting v6only to false on audio RTCP socket (error=%s)\n" ,
6637
6655
g_strerror (errno ));
6638
6656
}
@@ -6644,9 +6662,17 @@ static int janus_sip_allocate_local_ports(janus_sip_session *session, gboolean u
6644
6662
int rtp_port = g_random_int_range (rtp_range_min , rtp_range_max );
6645
6663
if (rtp_port % 2 )
6646
6664
rtp_port ++ ; /* Pick an even port for RTP */
6647
- audio_rtp_address .sin6_family = AF_INET6 ;
6648
- audio_rtp_address .sin6_port = htons (rtp_port );
6649
- audio_rtp_address .sin6_addr = in6addr_any ;
6665
+ if (!ipv6_disabled ) {
6666
+ struct sockaddr_in6 * addr = (struct sockaddr_in6 * )& audio_rtp_address ;
6667
+ addr -> sin6_family = AF_INET6 ;
6668
+ addr -> sin6_port = htons (rtp_port );
6669
+ addr -> sin6_addr = in6addr_any ;
6670
+ } else {
6671
+ struct sockaddr_in * addr = (struct sockaddr_in * )& audio_rtp_address ;
6672
+ addr -> sin_family = AF_INET ;
6673
+ addr -> sin_port = htons (rtp_port );
6674
+ addr -> sin_addr .s_addr = INADDR_ANY ;
6675
+ }
6650
6676
if (bind (session -> media .audio_rtp_fd , (struct sockaddr * )(& audio_rtp_address ), sizeof (audio_rtp_address )) < 0 ) {
6651
6677
JANUS_LOG (LOG_ERR , "Bind failed for audio RTP (port %d), trying a different one...\n" , rtp_port );
6652
6678
close (session -> media .audio_rtp_fd );
@@ -6656,9 +6682,17 @@ static int janus_sip_allocate_local_ports(janus_sip_session *session, gboolean u
6656
6682
}
6657
6683
JANUS_LOG (LOG_VERB , "Audio RTP listener bound to %s:%d(%d)\n" , (local_media_ip ? local_media_ip : local_ip ), rtp_port , session -> media .audio_rtp_fd );
6658
6684
int rtcp_port = rtp_port + 1 ;
6659
- audio_rtcp_address .sin6_family = AF_INET6 ;
6660
- audio_rtcp_address .sin6_port = htons (rtcp_port );
6661
- audio_rtcp_address .sin6_addr = in6addr_any ;
6685
+ if (!ipv6_disabled ) {
6686
+ struct sockaddr_in6 * addr = (struct sockaddr_in6 * )& audio_rtcp_address ;
6687
+ addr -> sin6_family = AF_INET6 ;
6688
+ addr -> sin6_port = htons (rtcp_port );
6689
+ addr -> sin6_addr = in6addr_any ;
6690
+ } else {
6691
+ struct sockaddr_in * addr = (struct sockaddr_in * )& audio_rtcp_address ;
6692
+ addr -> sin_family = AF_INET ;
6693
+ addr -> sin_port = htons (rtcp_port );
6694
+ addr -> sin_addr .s_addr = INADDR_ANY ;
6695
+ }
6662
6696
if (bind (session -> media .audio_rtcp_fd , (struct sockaddr * )(& audio_rtcp_address ), sizeof (audio_rtcp_address )) < 0 ) {
6663
6697
JANUS_LOG (LOG_ERR , "Bind failed for audio RTCP (port %d), trying a different one...\n" , rtcp_port );
6664
6698
/* RTP socket is not valid anymore, reset it */
@@ -6681,9 +6715,10 @@ static int janus_sip_allocate_local_ports(janus_sip_session *session, gboolean u
6681
6715
if (attempts == 0 ) /* Too many failures */
6682
6716
return -1 ;
6683
6717
if (session -> media .video_rtp_fd == -1 ) {
6684
- session -> media .video_rtp_fd = socket (AF_INET , SOCK_DGRAM , 0 );
6718
+ session -> media .video_rtp_fd = socket (! ipv6_disabled ? AF_INET6 : AF_INET , SOCK_DGRAM , 0 );
6685
6719
int v6only = 0 ;
6686
- if (session -> media .video_rtp_fd != -1 && setsockopt (session -> media .video_rtp_fd , IPPROTO_IPV6 , IPV6_V6ONLY , & v6only , sizeof (v6only )) != 0 ) {
6720
+ if (!ipv6_disabled && session -> media .video_rtp_fd != -1 &&
6721
+ setsockopt (session -> media .video_rtp_fd , IPPROTO_IPV6 , IPV6_V6ONLY , & v6only , sizeof (v6only )) != 0 ) {
6687
6722
JANUS_LOG (LOG_WARN , "Error setting v6only to false on video RTP socket (error=%s)\n" ,
6688
6723
g_strerror (errno ));
6689
6724
}
@@ -6698,9 +6733,10 @@ static int janus_sip_allocate_local_ports(janus_sip_session *session, gboolean u
6698
6733
}
6699
6734
}
6700
6735
if (session -> media .video_rtcp_fd == -1 ) {
6701
- session -> media .video_rtcp_fd = socket (AF_INET , SOCK_DGRAM , 0 );
6736
+ session -> media .video_rtcp_fd = socket (! ipv6_disabled ? AF_INET6 : AF_INET , SOCK_DGRAM , 0 );
6702
6737
int v6only = 0 ;
6703
- if (session -> media .video_rtcp_fd != -1 && setsockopt (session -> media .video_rtcp_fd , IPPROTO_IPV6 , IPV6_V6ONLY , & v6only , sizeof (v6only )) != 0 ) {
6738
+ if (!ipv6_disabled && session -> media .video_rtcp_fd != -1 &&
6739
+ setsockopt (session -> media .video_rtcp_fd , IPPROTO_IPV6 , IPV6_V6ONLY , & v6only , sizeof (v6only )) != 0 ) {
6704
6740
JANUS_LOG (LOG_WARN , "Error setting v6only to false on video RTCP socket (error=%s)\n" ,
6705
6741
g_strerror (errno ));
6706
6742
}
@@ -6712,9 +6748,17 @@ static int janus_sip_allocate_local_ports(janus_sip_session *session, gboolean u
6712
6748
int rtp_port = g_random_int_range (rtp_range_min , rtp_range_max );
6713
6749
if (rtp_port % 2 )
6714
6750
rtp_port ++ ; /* Pick an even port for RTP */
6715
- video_rtp_address .sin6_family = AF_INET6 ;
6716
- video_rtp_address .sin6_port = htons (rtp_port );
6717
- video_rtp_address .sin6_addr = in6addr_any ;
6751
+ if (!ipv6_disabled ) {
6752
+ struct sockaddr_in6 * addr = (struct sockaddr_in6 * )& video_rtp_address ;
6753
+ addr -> sin6_family = AF_INET6 ;
6754
+ addr -> sin6_port = htons (rtp_port );
6755
+ addr -> sin6_addr = in6addr_any ;
6756
+ } else {
6757
+ struct sockaddr_in * addr = (struct sockaddr_in * )& video_rtp_address ;
6758
+ addr -> sin_family = AF_INET ;
6759
+ addr -> sin_port = htons (rtp_port );
6760
+ addr -> sin_addr .s_addr = INADDR_ANY ;
6761
+ }
6718
6762
if (bind (session -> media .video_rtp_fd , (struct sockaddr * )(& video_rtp_address ), sizeof (video_rtp_address )) < 0 ) {
6719
6763
JANUS_LOG (LOG_ERR , "Bind failed for video RTP (port %d), trying a different one...\n" , rtp_port );
6720
6764
close (session -> media .video_rtp_fd );
@@ -6724,9 +6768,17 @@ static int janus_sip_allocate_local_ports(janus_sip_session *session, gboolean u
6724
6768
}
6725
6769
JANUS_LOG (LOG_VERB , "Video RTP listener bound to %s:%d(%d)\n" , (local_media_ip ? local_media_ip : local_ip ), rtp_port , session -> media .video_rtp_fd );
6726
6770
int rtcp_port = rtp_port + 1 ;
6727
- video_rtcp_address .sin6_family = AF_INET ;
6728
- video_rtcp_address .sin6_port = htons (rtcp_port );
6729
- video_rtcp_address .sin6_addr = in6addr_any ;
6771
+ if (!ipv6_disabled ) {
6772
+ struct sockaddr_in6 * addr = (struct sockaddr_in6 * )& video_rtcp_address ;
6773
+ addr -> sin6_family = AF_INET6 ;
6774
+ addr -> sin6_port = htons (rtcp_port );
6775
+ addr -> sin6_addr = in6addr_any ;
6776
+ } else {
6777
+ struct sockaddr_in * addr = (struct sockaddr_in * )& video_rtcp_address ;
6778
+ addr -> sin_family = AF_INET ;
6779
+ addr -> sin_port = htons (rtcp_port );
6780
+ addr -> sin_addr .s_addr = INADDR_ANY ;
6781
+ }
6730
6782
if (bind (session -> media .video_rtcp_fd , (struct sockaddr * )(& video_rtcp_address ), sizeof (video_rtcp_address )) < 0 ) {
6731
6783
JANUS_LOG (LOG_ERR , "Bind failed for video RTCP (port %d), trying a different one...\n" , rtcp_port );
6732
6784
/* RTP socket is not valid anymore, reset it */
0 commit comments