Skip to content

Commit

Permalink
Small tweaks to #3263, and updated NoSIP plugin with the same changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lminiero committed Nov 16, 2023
1 parent d9a5853 commit f74b313
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 26 deletions.
84 changes: 61 additions & 23 deletions src/plugins/janus_nosip.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ static gboolean ipv6_disabled = FALSE;
static janus_callbacks *gateway = NULL;

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

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

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

/* Make sure both IPs are valid, if provided */
janus_network_address_nullify(&janus_network_local_ip);
if(local_ip) {
if(janus_network_string_to_address(janus_network_query_options_any_ip, local_ip, &janus_network_local_ip) != 0) {
JANUS_LOG(LOG_ERR, "Invalid local media IP address [%s]...\n", local_ip);
return -1;
}
if((janus_network_local_ip.family == AF_INET && janus_network_local_ip.ipv4.s_addr == INADDR_ANY) ||
(janus_network_local_ip.family == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED(&janus_network_local_ip.ipv6))) {
janus_network_address_nullify(&janus_network_local_ip);
}
}
JANUS_LOG(LOG_VERB, "Binding media address set to [%s]...\n", janus_network_address_is_null(&janus_network_local_ip) ? "any" : local_ip);
if(!sdp_ip) {
char *ip = janus_network_address_is_null(&janus_network_local_ip) ? local_ip : local_ip;
if(ip) {
sdp_ip = g_strdup(ip);
JANUS_LOG(LOG_VERB, "IP to advertise in SDP: %s\n", sdp_ip);
}
}

item = janus_config_get(config, config_general, janus_config_type_item, "rtp_port_range");
if(item && item->value) {
/* Split in min and max port */
Expand Down Expand Up @@ -794,19 +816,29 @@ int janus_nosip_init(janus_callbacks *callback, const char *config_path) {
/* This is the callback we'll need to invoke to contact the Janus core */
gateway = callback;

/* Finally, let's check if IPv6 is disabled, as we may need to know for RTP/RTCP sockets */
int fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if(fd <= 0) {
ipv6_disabled = TRUE;
} else {
int v6only = 0;
if(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)) != 0)
if(janus_network_address_is_null(&janus_network_local_ip) ||
janus_network_local_ip.family == AF_INET6) {
/* Finally, let's check if IPv6 is disabled, as we may need to know for RTP/RTCP sockets */
int fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if(fd <= 0) {
ipv6_disabled = TRUE;
}
if(fd > 0)
close(fd);
if(ipv6_disabled) {
JANUS_LOG(LOG_WARN, "IPv6 disabled, will only use IPv4 for RTP/RTCP sockets (NoSIP)\n");
} else {
int v6only = 0;
if(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)) != 0)
ipv6_disabled = TRUE;
}
if(fd > 0)
close(fd);
if(ipv6_disabled) {
if(!janus_network_address_is_null(&janus_network_local_ip)) {
JANUS_LOG(LOG_ERR, "IPv6 disabled and local media address is IPv6...\n");
return -1;
}
JANUS_LOG(LOG_WARN, "IPv6 disabled, will only use IPv4 for RTP/RTCP sockets (SIP)\n");
}
} else if(janus_network_local_ip.family == AF_INET) {
/* Disable if we have a specified IPv4 address for RTP/RTCP sockets */
ipv6_disabled = TRUE;
}

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

static int janus_nosip_bind_socket(int fd, int port) {
gboolean use_ipv6_address_family = !ipv6_disabled &&
(janus_network_address_is_null(&janus_network_local_ip) || janus_network_local_ip.family == AF_INET6);
socklen_t addrlen = use_ipv6_address_family? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in);
struct sockaddr_storage rtp_address = { 0 };
if(!ipv6_disabled) {
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)&rtp_address;
addr->sin6_family = AF_INET6;
addr->sin6_port = htons(port);
addr->sin6_addr = in6addr_any;
addr->sin6_addr = janus_network_address_is_null(&janus_network_local_ip) ? in6addr_any : janus_network_local_ip.ipv6;
} else {
struct sockaddr_in *addr = (struct sockaddr_in *)&rtp_address;
addr->sin_family = AF_INET;
addr->sin_port = htons(port);
addr->sin_addr.s_addr = INADDR_ANY;
addr->sin_addr.s_addr = janus_network_address_is_null(&janus_network_local_ip) ? INADDR_ANY : janus_network_local_ip.ipv4.s_addr;
}
if(bind(fd, (struct sockaddr *)(&rtp_address), sizeof(rtp_address)) < 0) {
JANUS_LOG(LOG_ERR, "Bind failed (port %d)\n", port);
if(bind(fd, (struct sockaddr *)(&rtp_address), addrlen) < 0) {
JANUS_LOG(LOG_ERR, "Bind failed (port %d), error (%s)\n", port, g_strerror(errno));
return -1;
}
return 0;
Expand All @@ -2023,6 +2058,9 @@ static int janus_nosip_allocate_port_pair(gboolean video, int fds[2], int ports[
uint16_t rtp_port_start = rtp_port_next;
gboolean rtp_port_wrap = FALSE;

gboolean use_ipv6_address_family = !ipv6_disabled &&
(janus_network_address_is_null(&janus_network_local_ip) || janus_network_local_ip.family == AF_INET6);

int rtp_fd = -1, rtcp_fd = -1;
while(1) {
if(rtp_port_wrap && rtp_port_next >= rtp_port_start) { /* Full range scanned */
Expand All @@ -2031,9 +2069,9 @@ static int janus_nosip_allocate_port_pair(gboolean video, int fds[2], int ports[
break;
}
if(rtp_fd == -1) {
rtp_fd = socket(!ipv6_disabled ? AF_INET6 : AF_INET, SOCK_DGRAM, 0);
rtp_fd = socket(use_ipv6_address_family ? AF_INET6 : AF_INET, SOCK_DGRAM, 0);
int v6only = 0;
if(!ipv6_disabled && rtp_fd != -1 && setsockopt(rtp_fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)) != 0) {
if(use_ipv6_address_family && rtp_fd != -1 && setsockopt(rtp_fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)) != 0) {
JANUS_LOG(LOG_WARN, "Error setting v6only to false on RTP socket (error=%s)\n",
g_strerror(errno));
}
Expand All @@ -2056,8 +2094,8 @@ static int janus_nosip_allocate_port_pair(gboolean video, int fds[2], int ports[
}
if(rtcp_fd == -1) {
int v6only = 0;
rtcp_fd = socket(!ipv6_disabled ? AF_INET6 : AF_INET, SOCK_DGRAM, 0);
if(!ipv6_disabled && rtcp_fd != -1 && setsockopt(rtcp_fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)) != 0) {
rtcp_fd = socket(use_ipv6_address_family ? AF_INET6 : AF_INET, SOCK_DGRAM, 0);
if(use_ipv6_address_family && rtcp_fd != -1 && setsockopt(rtcp_fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)) != 0) {
JANUS_LOG(LOG_WARN, "Error setting v6only to false on RTP socket (error=%s)\n",
g_strerror(errno));
}
Expand Down
10 changes: 7 additions & 3 deletions src/plugins/janus_sip.c
Original file line number Diff line number Diff line change
Expand Up @@ -2019,8 +2019,11 @@ int janus_sip_init(janus_callbacks *callback, const char *config_path) {
}
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);
if(!sdp_ip) {
sdp_ip = janus_network_address_is_null(&janus_network_local_media_ip) ? local_ip : local_media_ip;
JANUS_LOG(LOG_VERB, "IP to advertise in SDP: %s\n", sdp_ip);
char *ip = janus_network_address_is_null(&janus_network_local_media_ip) ? local_ip : local_media_ip;
if(ip) {
sdp_ip = g_strdup(ip);
JANUS_LOG(LOG_VERB, "IP to advertise in SDP: %s\n", sdp_ip);
}
}

/* Setup sofia */
Expand Down Expand Up @@ -6745,7 +6748,8 @@ static int janus_sip_allocate_local_ports(janus_sip_session *session, gboolean u
attempts--;
continue;
}
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);
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);
session->media.local_audio_rtp_port = rtp_port;
session->media.local_audio_rtcp_port = rtcp_port;
}
Expand Down

0 comments on commit f74b313

Please sign in to comment.