/* module.c generated by valac 0.56.19, the Vala compiler
 * generated from module.vala, do not modify */

#include "ice.h"
#include <nice.h>
#include <gee.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "xmpp-vala.h"

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	DINO_PLUGINS_ICE_MODULE_0_PROPERTY,
	DINO_PLUGINS_ICE_MODULE_NUM_PROPERTIES
};
static GParamSpec* dino_plugins_ice_module_properties[DINO_PLUGINS_ICE_MODULE_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _xmpp_xep_external_service_discovery_service_unref0(var) ((var == NULL) ? NULL : (var = (xmpp_xep_external_service_discovery_service_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _dino_plugins_ice_dtls_srtp_credentials_capsule_unref0(var) ((var == NULL) ? NULL : (var = (dino_plugins_ice_dtls_srtp_credentials_capsule_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _DinoPluginsIceModulePrivate {
	NiceAgent* agent;
	GeeHashMap* cerds;
};

static gint DinoPluginsIceModule_private_offset;
static gpointer dino_plugins_ice_module_parent_class = NULL;

static NiceAgent* dino_plugins_ice_module_get_agent (DinoPluginsIceModule* self);
static void dino_plugins_ice_module_agent_unweak (DinoPluginsIceModule* self);
static void _dino_plugins_ice_module_agent_unweak_gweak_notify (gpointer self,
                                                         GObject* object);
static XmppXepJingleTransportParameters* dino_plugins_ice_module_real_create_transport_parameters (XmppXepJingleIceUdpModule* base,
                                                                                            XmppXmppStream* stream,
                                                                                            guint8 components,
                                                                                            XmppJid* local_full_jid,
                                                                                            XmppJid* peer_full_jid,
                                                                                            GError** error);
static DinoPluginsIceDtlsSrtpCredentialsCapsule* dino_plugins_ice_module_get_create_credentials (DinoPluginsIceModule* self,
                                                                                          XmppJid* local_full_jid,
                                                                                          XmppJid* peer_full_jid);
static XmppXepJingleTransportParameters* dino_plugins_ice_module_real_parse_transport_parameters (XmppXepJingleIceUdpModule* base,
                                                                                           XmppXmppStream* stream,
                                                                                           guint8 components,
                                                                                           XmppJid* local_full_jid,
                                                                                           XmppJid* peer_full_jid,
                                                                                           XmppStanzaNode* transport,
                                                                                           GError** error);
VALA_EXTERN DinoPluginsIceDtlsSrtpCredentialsCapsule* dino_plugins_ice_dtls_srtp_handler_generate_credentials (GError** error);
static void dino_plugins_ice_module_finalize (GObject * obj);
static GType dino_plugins_ice_module_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
dino_plugins_ice_module_get_instance_private (DinoPluginsIceModule* self)
{
	return G_STRUCT_MEMBER_P (self, DinoPluginsIceModule_private_offset);
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

static void
_dino_plugins_ice_module_agent_unweak_gweak_notify (gpointer self,
                                                    GObject* object)
{
	dino_plugins_ice_module_agent_unweak ((DinoPluginsIceModule*) self);
}

static NiceAgent*
dino_plugins_ice_module_get_agent (DinoPluginsIceModule* self)
{
	NiceAgent* agent = NULL;
	NiceAgent* _tmp0_;
	NiceAgent* _tmp1_;
	NiceAgent* _tmp2_;
	NiceAgent* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->agent;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	agent = _tmp1_;
	_tmp2_ = agent;
	if (_tmp2_ == NULL) {
		GMainContext* _tmp3_;
		NiceAgent* _tmp4_;
		const gchar* _tmp5_;
		NiceAgent* _tmp9_;
		NiceAgent* _tmp10_;
		NiceAgent* _tmp11_;
		NiceAgent* _tmp12_;
		NiceAgent* _tmp13_;
		gchar* _tmp14_;
		gchar* _tmp15_;
		gchar* _tmp16_;
		NiceAgent* _tmp17_;
		guint _tmp18_;
		guint _tmp19_;
		_tmp3_ = g_main_context_default ();
		_tmp4_ = nice_agent_new (_tmp3_, NICE_COMPATIBILITY_RFC5245);
		_g_object_unref0 (agent);
		agent = _tmp4_;
		_tmp5_ = self->stun_ip;
		if (_tmp5_ != NULL) {
			NiceAgent* _tmp6_;
			const gchar* _tmp7_;
			NiceAgent* _tmp8_;
			_tmp6_ = agent;
			_tmp7_ = self->stun_ip;
			g_object_set (_tmp6_, "stun-server", _tmp7_, NULL);
			_tmp8_ = agent;
			g_object_set (_tmp8_, "stun-server-port", self->stun_port, NULL);
		}
		_tmp9_ = agent;
		g_object_set (_tmp9_, "ice-tcp", FALSE, NULL);
		_tmp10_ = agent;
		nice_agent_set_software (_tmp10_, "Dino");
		_tmp11_ = agent;
		g_object_weak_ref ((GObject*) _tmp11_, _dino_plugins_ice_module_agent_unweak_gweak_notify, self);
		_tmp12_ = agent;
		self->priv->agent = _tmp12_;
		_tmp13_ = agent;
		g_object_get (_tmp13_, "stun-server", &_tmp14_, NULL);
		_tmp15_ = _tmp14_;
		_tmp16_ = _tmp15_;
		_tmp17_ = agent;
		g_object_get (_tmp17_, "stun-server-port", &_tmp18_, NULL);
		_tmp19_ = _tmp18_;
		g_debug ("module.vala:27: STUN server for libnice %s %u", _tmp16_, _tmp19_);
		_g_free0 (_tmp16_);
	}
	result = agent;
	return result;
}

static XmppXepJingleTransportParameters*
dino_plugins_ice_module_real_create_transport_parameters (XmppXepJingleIceUdpModule* base,
                                                          XmppXmppStream* stream,
                                                          guint8 components,
                                                          XmppJid* local_full_jid,
                                                          XmppJid* peer_full_jid,
                                                          GError** error)
{
	DinoPluginsIceModule * self;
	DinoPluginsIceDtlsSrtpCredentialsCapsule* cred = NULL;
	DinoPluginsIceDtlsSrtpCredentialsCapsule* _tmp0_;
	NiceAgent* _tmp1_;
	NiceAgent* _tmp2_;
	XmppXepExternalServiceDiscoveryService* _tmp3_;
	const gchar* _tmp4_;
	DinoPluginsIceTransportParameters* _tmp5_;
	XmppXepJingleTransportParameters* _tmp6_;
	XmppXepJingleTransportParameters* result;
	self = (DinoPluginsIceModule*) base;
	g_return_val_if_fail (stream != NULL, NULL);
	g_return_val_if_fail (local_full_jid != NULL, NULL);
	g_return_val_if_fail (peer_full_jid != NULL, NULL);
	_tmp0_ = dino_plugins_ice_module_get_create_credentials (self, local_full_jid, peer_full_jid);
	cred = _tmp0_;
	_tmp1_ = dino_plugins_ice_module_get_agent (self);
	_tmp2_ = _tmp1_;
	_tmp3_ = self->turn_service;
	_tmp4_ = self->turn_ip;
	_tmp5_ = dino_plugins_ice_transport_parameters_new (_tmp2_, cred, _tmp3_, _tmp4_, components, local_full_jid, peer_full_jid, NULL);
	_tmp6_ = (XmppXepJingleTransportParameters*) _tmp5_;
	_g_object_unref0 (_tmp2_);
	result = _tmp6_;
	_dino_plugins_ice_dtls_srtp_credentials_capsule_unref0 (cred);
	return result;
}

static XmppXepJingleTransportParameters*
dino_plugins_ice_module_real_parse_transport_parameters (XmppXepJingleIceUdpModule* base,
                                                         XmppXmppStream* stream,
                                                         guint8 components,
                                                         XmppJid* local_full_jid,
                                                         XmppJid* peer_full_jid,
                                                         XmppStanzaNode* transport,
                                                         GError** error)
{
	DinoPluginsIceModule * self;
	DinoPluginsIceDtlsSrtpCredentialsCapsule* cred = NULL;
	DinoPluginsIceDtlsSrtpCredentialsCapsule* _tmp0_;
	NiceAgent* _tmp1_;
	NiceAgent* _tmp2_;
	XmppXepExternalServiceDiscoveryService* _tmp3_;
	const gchar* _tmp4_;
	DinoPluginsIceTransportParameters* _tmp5_;
	XmppXepJingleTransportParameters* _tmp6_;
	XmppXepJingleTransportParameters* result;
	self = (DinoPluginsIceModule*) base;
	g_return_val_if_fail (stream != NULL, NULL);
	g_return_val_if_fail (local_full_jid != NULL, NULL);
	g_return_val_if_fail (peer_full_jid != NULL, NULL);
	g_return_val_if_fail (transport != NULL, NULL);
	_tmp0_ = dino_plugins_ice_module_get_create_credentials (self, local_full_jid, peer_full_jid);
	cred = _tmp0_;
	_tmp1_ = dino_plugins_ice_module_get_agent (self);
	_tmp2_ = _tmp1_;
	_tmp3_ = self->turn_service;
	_tmp4_ = self->turn_ip;
	_tmp5_ = dino_plugins_ice_transport_parameters_new (_tmp2_, cred, _tmp3_, _tmp4_, components, local_full_jid, peer_full_jid, transport);
	_tmp6_ = (XmppXepJingleTransportParameters*) _tmp5_;
	_g_object_unref0 (_tmp2_);
	result = _tmp6_;
	_dino_plugins_ice_dtls_srtp_credentials_capsule_unref0 (cred);
	return result;
}

static DinoPluginsIceDtlsSrtpCredentialsCapsule*
dino_plugins_ice_module_get_create_credentials (DinoPluginsIceModule* self,
                                                XmppJid* local_full_jid,
                                                XmppJid* peer_full_jid)
{
	gchar* from_to_id = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	GeeHashMap* _tmp14_;
	const gchar* _tmp15_;
	gpointer _tmp16_;
	GError* _inner_error0_ = NULL;
	DinoPluginsIceDtlsSrtpCredentialsCapsule* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (local_full_jid != NULL, NULL);
	g_return_val_if_fail (peer_full_jid != NULL, NULL);
	_tmp0_ = xmpp_jid_to_string (local_full_jid);
	_tmp1_ = _tmp0_;
	_tmp2_ = xmpp_jid_to_string (peer_full_jid);
	_tmp3_ = _tmp2_;
	_tmp4_ = g_strconcat (_tmp1_, _tmp3_, NULL);
	_tmp5_ = _tmp4_;
	_g_free0 (_tmp3_);
	_g_free0 (_tmp1_);
	from_to_id = _tmp5_;
	{
		GeeHashMap* _tmp6_;
		const gchar* _tmp7_;
		_tmp6_ = self->priv->cerds;
		_tmp7_ = from_to_id;
		if (!gee_abstract_map_has_key ((GeeAbstractMap*) _tmp6_, _tmp7_)) {
			DinoPluginsIceDtlsSrtpCredentialsCapsule* _tmp8_ = NULL;
			DinoPluginsIceDtlsSrtpCredentialsCapsule* _tmp9_;
			GeeHashMap* _tmp10_;
			const gchar* _tmp11_;
			_tmp9_ = dino_plugins_ice_dtls_srtp_handler_generate_credentials (&_inner_error0_);
			_tmp8_ = _tmp9_;
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
			_tmp10_ = self->priv->cerds;
			_tmp11_ = from_to_id;
			gee_abstract_map_set ((GeeAbstractMap*) _tmp10_, _tmp11_, _tmp8_);
			_dino_plugins_ice_dtls_srtp_credentials_capsule_unref0 (_tmp8_);
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp12_;
		const gchar* _tmp13_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp12_ = e;
		_tmp13_ = _tmp12_->message;
		g_warning ("module.vala:47: Error creating dtls credentials: %s", _tmp13_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_free0 (from_to_id);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return NULL;
	}
	_tmp14_ = self->priv->cerds;
	_tmp15_ = from_to_id;
	_tmp16_ = gee_abstract_map_get ((GeeAbstractMap*) _tmp14_, _tmp15_);
	result = (DinoPluginsIceDtlsSrtpCredentialsCapsule*) _tmp16_;
	_g_free0 (from_to_id);
	return result;
}

static void
dino_plugins_ice_module_agent_unweak (DinoPluginsIceModule* self)
{
	g_return_if_fail (self != NULL);
	self->priv->agent = NULL;
}

DinoPluginsIceModule*
dino_plugins_ice_module_construct (GType object_type)
{
	DinoPluginsIceModule * self = NULL;
	self = (DinoPluginsIceModule*) xmpp_xep_jingle_ice_udp_module_construct (object_type);
	return self;
}

DinoPluginsIceModule*
dino_plugins_ice_module_new (void)
{
	return dino_plugins_ice_module_construct (DINO_PLUGINS_ICE_TYPE_MODULE);
}

static void
dino_plugins_ice_module_class_init (DinoPluginsIceModuleClass * klass,
                                    gpointer klass_data)
{
	dino_plugins_ice_module_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &DinoPluginsIceModule_private_offset);
	((XmppXepJingleIceUdpModuleClass *) klass)->create_transport_parameters = (XmppXepJingleTransportParameters* (*) (XmppXepJingleIceUdpModule*, XmppXmppStream*, guint8, XmppJid*, XmppJid*, GError**)) dino_plugins_ice_module_real_create_transport_parameters;
	((XmppXepJingleIceUdpModuleClass *) klass)->parse_transport_parameters = (XmppXepJingleTransportParameters* (*) (XmppXepJingleIceUdpModule*, XmppXmppStream*, guint8, XmppJid*, XmppJid*, XmppStanzaNode*, GError**)) dino_plugins_ice_module_real_parse_transport_parameters;
	G_OBJECT_CLASS (klass)->finalize = dino_plugins_ice_module_finalize;
}

static void
dino_plugins_ice_module_instance_init (DinoPluginsIceModule * self,
                                       gpointer klass)
{
	GeeHashMap* _tmp0_;
	self->priv = dino_plugins_ice_module_get_instance_private (self);
	self->stun_ip = NULL;
	self->stun_port = (guint) 0;
	self->turn_ip = NULL;
	self->turn_service = NULL;
	_tmp0_ = gee_hash_map_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, DINO_PLUGINS_ICE_DTLS_SRTP_TYPE_CREDENTIALS_CAPSULE, (GBoxedCopyFunc) dino_plugins_ice_dtls_srtp_credentials_capsule_ref, (GDestroyNotify) dino_plugins_ice_dtls_srtp_credentials_capsule_unref, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
	self->priv->cerds = _tmp0_;
}

static void
dino_plugins_ice_module_finalize (GObject * obj)
{
	DinoPluginsIceModule * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, DINO_PLUGINS_ICE_TYPE_MODULE, DinoPluginsIceModule);
	_g_free0 (self->stun_ip);
	_g_free0 (self->turn_ip);
	_xmpp_xep_external_service_discovery_service_unref0 (self->turn_service);
	_g_object_unref0 (self->priv->cerds);
	G_OBJECT_CLASS (dino_plugins_ice_module_parent_class)->finalize (obj);
}

static GType
dino_plugins_ice_module_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (DinoPluginsIceModuleClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) dino_plugins_ice_module_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DinoPluginsIceModule), 0, (GInstanceInitFunc) dino_plugins_ice_module_instance_init, NULL };
	GType dino_plugins_ice_module_type_id;
	dino_plugins_ice_module_type_id = g_type_register_static (XMPP_XEP_JINGLE_ICE_UDP_TYPE_MODULE, "DinoPluginsIceModule", &g_define_type_info, 0);
	DinoPluginsIceModule_private_offset = g_type_add_instance_private (dino_plugins_ice_module_type_id, sizeof (DinoPluginsIceModulePrivate));
	return dino_plugins_ice_module_type_id;
}

GType
dino_plugins_ice_module_get_type (void)
{
	static volatile gsize dino_plugins_ice_module_type_id__once = 0;
	if (g_once_init_enter (&dino_plugins_ice_module_type_id__once)) {
		GType dino_plugins_ice_module_type_id;
		dino_plugins_ice_module_type_id = dino_plugins_ice_module_get_type_once ();
		g_once_init_leave (&dino_plugins_ice_module_type_id__once, dino_plugins_ice_module_type_id);
	}
	return dino_plugins_ice_module_type_id__once;
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

