1
0
Fork 0
mirror of https://gitlab.gnome.org/GNOME/calls.git synced 2024-10-05 18:25:26 +00:00
Purism-Calls/plugins/sip/calls-sip-media-manager.c
Evangelos Ribeiro Tzaras 8b126484cb sip: Use per origin IP instead of a global IP
Sofia detects a NAT by presence of the "received" parameter in the Via header in
the response to a REGISTER. Sofia will then update the Contact header to use the
IP as reported by the registrar.

The "received" parameter MUST be included in the response according to
https://datatracker.ietf.org/doc/html/rfc3261#section-18.2.1
when the registrar detects a difference between the domain part of the top Via
header and the packet source address but practice has shown that this will not
always be the case.

Addditionally this change allows us to have origins bound to different network
interfaces which would be useful when a registrar can only be accessed through a
VPN.

This also fixes an issue with SDP introduced in
36880c3d34 which was only seen on some SIP
providers:

The session name ("s=") line is not relevant for establishing a connection,
the connection data (c=") line is.

See https://datatracker.ietf.org/doc/html/rfc4566 section 5.3 and 5.7
2022-01-08 21:25:09 +00:00

411 lines
12 KiB
C

/*
* Copyright (C) 2021 Purism SPC
*
* This file is part of Calls.
*
* Calls is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Calls is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Calls. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Evangelos Ribeiro Tzaras <evangelos.tzaras@puri.sm>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#define G_LOG_DOMAIN "CallsSipMediaManager"
#include "calls-settings.h"
#include "calls-sip-media-manager.h"
#include "gst-rfc3551.h"
#include <gst/gst.h>
/**
* SECTION:sip-media-manager
* @short_description: The media manager singleton
* @Title: CallsSipMediaManager
*
* #CallsSipMediaManager is mainly responsible for generating appropriate
* SDP messages for the set of supported codecs. In the future it
* shall also manage the #CallsSipMediaPipeline objects that are in use.
*/
enum {
PROP_0,
PROP_SESSION_IP,
PROP_LAST_PROP
};
static GParamSpec *props[PROP_LAST_PROP];
typedef struct _CallsSipMediaManager
{
GObject parent;
char *session_ip;
int address_family;
struct addrinfo hints;
CallsSettings *settings;
GList *preferred_codecs;
} CallsSipMediaManager;
G_DEFINE_TYPE (CallsSipMediaManager, calls_sip_media_manager, G_TYPE_OBJECT);
static const char *
get_address_family_string (CallsSipMediaManager *self,
const char *ip)
{
struct addrinfo *result = NULL;
const char *family;
if (getaddrinfo (ip, NULL, &self->hints, &result) != 0) {
g_warning ("Cannot parse session IP %s", ip);
return NULL;
}
/* check if IP is IPv4 or IPv6. We need to specify this in the c= line of SDP */
self->address_family = result->ai_family;
if (result->ai_family == AF_INET)
family = "IP4";
else if (result->ai_family == AF_INET6)
family = "IP6";
else
family = NULL;
freeaddrinfo (result);
return family;
}
static void
on_notify_preferred_audio_codecs (CallsSipMediaManager *self)
{
GList *supported_codecs;
g_auto (GStrv) settings_codec_preference = NULL;
g_assert (CALLS_IS_SIP_MEDIA_MANAGER (self));
g_clear_list (&self->preferred_codecs, NULL);
supported_codecs = media_codecs_get_candidates ();
if (!supported_codecs) {
g_warning ("There aren't any supported codecs installed on your system");
return;
}
settings_codec_preference = calls_settings_get_preferred_audio_codecs (self->settings);
if (!settings_codec_preference) {
g_debug ("No audio codec preference set. Using all supported codecs");
self->preferred_codecs = supported_codecs;
return;
}
for (guint i = 0; settings_codec_preference[i] != NULL; i++) {
MediaCodecInfo *codec = media_codec_by_name (settings_codec_preference[i]);
if (!codec) {
g_debug ("Did not find audio codec %s", settings_codec_preference[i]);
continue;
}
if (media_codec_available_in_gst (codec))
self->preferred_codecs = g_list_append (self->preferred_codecs, codec);
}
if (!self->preferred_codecs) {
g_warning ("Cannot satisfy audio codec preference, "
"falling back to all supported codecs");
self->preferred_codecs = supported_codecs;
} else {
g_list_free (supported_codecs);
}
}
static void
calls_sip_media_manager_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
CallsSipMediaManager *self = CALLS_SIP_MEDIA_MANAGER (object);
switch (property_id) {
case PROP_SESSION_IP:
calls_sip_media_manager_set_session_ip (self, g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
calls_sip_media_manager_finalize (GObject *object)
{
CallsSipMediaManager *self = CALLS_SIP_MEDIA_MANAGER (object);
gst_deinit ();
g_list_free (self->preferred_codecs);
g_free (self->session_ip);
g_object_unref (self->settings);
G_OBJECT_CLASS (calls_sip_media_manager_parent_class)->finalize (object);
}
static void
calls_sip_media_manager_class_init (CallsSipMediaManagerClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->set_property = calls_sip_media_manager_set_property;
object_class->finalize = calls_sip_media_manager_finalize;
props[PROP_SESSION_IP] =
g_param_spec_string ("session-ip",
"Session IP",
"The public IP used as the session line in SDP",
NULL,
G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, PROP_LAST_PROP, props);
}
static void
calls_sip_media_manager_init (CallsSipMediaManager *self)
{
gst_init (NULL, NULL);
self->settings = calls_settings_new ();
g_signal_connect_swapped (self->settings,
"notify::preferred-audio-codecs",
G_CALLBACK (on_notify_preferred_audio_codecs),
self);
on_notify_preferred_audio_codecs (self);
/* Hints are used with getaddrinfo() when setting the session IP */
self->hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_NUMERICHOST;
self->hints.ai_family = AF_UNSPEC;
}
/* Public functions */
CallsSipMediaManager *
calls_sip_media_manager_default (void)
{
static CallsSipMediaManager *instance = NULL;
if (instance == NULL) {
g_debug ("Creating CallsSipMediaManager");
instance = g_object_new (CALLS_TYPE_SIP_MEDIA_MANAGER, NULL);
g_object_add_weak_pointer (G_OBJECT (instance), (gpointer *) &instance);
}
return instance;
}
/* calls_sip_media_manager_get_capabilities:
*
* @self: A #CallsSipMediaManager
* @port: Should eventually come from the ICE stack
* @use_srtp: Whether to use srtp (not really handled)
* @supported_codecs: A #GList of #MediaCodecInfo
*
* Returns: (transfer full): string describing capabilities
* to be used in the session description (SDP)
*/
char *
calls_sip_media_manager_get_capabilities (CallsSipMediaManager *self,
const char *own_ip,
guint port,
gboolean use_srtp,
GList *supported_codecs)
{
char *payload_type = use_srtp ? "SAVP" : "AVP";
g_autoptr (GString) media_line = NULL;
g_autoptr (GString) attribute_lines = NULL;
GList *node;
const char *address_family_string;
g_return_val_if_fail (CALLS_IS_SIP_MEDIA_MANAGER (self), NULL);
media_line = g_string_new (NULL);
attribute_lines = g_string_new (NULL);
if (supported_codecs == NULL) {
g_warning ("No supported codecs found. Can't build meaningful SDP message");
g_string_append_printf (media_line, "m=audio 0 RTP/AVP");
goto done;
}
/* media lines look f.e like "audio 31337 RTP/AVP 9 8 0" */
g_string_append_printf (media_line,
"m=audio %d RTP/%s", port, payload_type);
for (node = supported_codecs; node != NULL; node = node->next) {
MediaCodecInfo *codec = node->data;
g_string_append_printf (media_line, " %u", codec->payload_id);
g_string_append_printf (attribute_lines,
"a=rtpmap:%u %s/%u%s",
codec->payload_id,
codec->name,
codec->clock_rate,
"\r\n");
}
g_string_append_printf (attribute_lines, "a=rtcp:%d\r\n", port + 1);
done:
if (own_ip && *own_ip)
address_family_string = get_address_family_string (self, own_ip);
if (own_ip && *own_ip && address_family_string)
return g_strdup_printf ("v=0\r\n"
"c=IN %s %s\r\n"
"%s\r\n"
"%s\r\n",
address_family_string,
own_ip,
media_line->str,
attribute_lines->str);
else
return g_strdup_printf ("v=0\r\n"
"%s\r\n"
"%s\r\n",
media_line->str,
attribute_lines->str);
}
/* calls_sip_media_manager_static_capabilities:
*
* @self: A #CallsSipMediaManager
* @port: Should eventually come from the ICE stack
* @use_srtp: Whether to use srtp (not really handled)
*
* Returns: (transfer full): string describing capabilities
* to be used in the session description (SDP)
*/
char *
calls_sip_media_manager_static_capabilities (CallsSipMediaManager *self,
const char *own_ip,
guint port,
gboolean use_srtp)
{
g_return_val_if_fail (CALLS_IS_SIP_MEDIA_MANAGER (self), NULL);
return calls_sip_media_manager_get_capabilities (self,
own_ip,
port,
use_srtp,
self->preferred_codecs);
}
MediaCodecInfo*
get_best_codec (CallsSipMediaManager *self)
{
return media_codec_by_name ("PCMA");
}
/* calls_sip_media_manager_codec_candiates:
*
* @self: A #CallsSipMediaManager
*
* Returns: (transfer none): A #GList of supported
* #MediaCodecInfo
*/
GList *
calls_sip_media_manager_codec_candidates (CallsSipMediaManager *self)
{
g_return_val_if_fail (CALLS_IS_SIP_MEDIA_MANAGER (self), NULL);
return self->preferred_codecs;
}
/* calls_sip_media_manager_get_codecs_from_sdp
*
* @self: A #CallsSipMediaManager
* @sdp: A #sdp_media_t media description
*
* Returns: (transfer container): A #GList of codecs found in the
* SDP message
*/
GList *
calls_sip_media_manager_get_codecs_from_sdp (CallsSipMediaManager *self,
sdp_media_t *sdp_media)
{
GList *codecs = NULL;
sdp_rtpmap_t *rtpmap = NULL;
g_return_val_if_fail (CALLS_IS_SIP_MEDIA_MANAGER (self), NULL);
g_return_val_if_fail (sdp_media, NULL);
if (sdp_media->m_type != sdp_media_audio) {
g_warning ("Only the 'audio' media type is supported");
return NULL;
}
for (rtpmap = sdp_media->m_rtpmaps; rtpmap != NULL; rtpmap = rtpmap->rm_next) {
MediaCodecInfo *codec = media_codec_by_payload_id (rtpmap->rm_pt);
if (codec)
codecs = g_list_append (codecs, codec);
}
if (sdp_media->m_next != NULL)
g_warning ("Currently only a single media session is supported");
if (codecs == NULL)
g_warning ("Did not find any common codecs");
return codecs;
}
void
calls_sip_media_manager_set_session_ip (CallsSipMediaManager *self,
const char *session_ip)
{
g_return_if_fail (CALLS_IS_SIP_MEDIA_MANAGER (self));
g_clear_pointer (&self->session_ip, g_free);
if (session_ip && *session_ip) {
struct addrinfo *result = NULL;
if (getaddrinfo (session_ip, NULL, &self->hints, &result) != 0) {
g_warning ("Cannot parse session IP %s", session_ip);
return;
}
/* check if IP is IPv4 or IPv6. We need to specify this in the c= line of SDP */
self->address_family = result->ai_family;
g_debug ("Setting session IP to %s", session_ip);
g_free (self->session_ip);
self->session_ip = g_strdup (session_ip);
freeaddrinfo (result);
}
}