#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <sip.h>
#include "sip_msg.h"
#include "sip_miscdefs.h"
#include "sip_parse_uri.h"
#include "sip_dialog.h"
sip_msg_t
sip_create_dialog_req(sip_method_t method, sip_dialog_t dialog,
char *transport, char *sent_by, int sent_by_port, char *via_param,
uint32_t maxforward, int cseq)
{
_sip_dialog_t *_dialog;
sip_msg_t sip_msg;
char *uri;
int oldseq = 0;
if (!sip_manage_dialog || dialog == NULL || transport == NULL ||
sent_by == NULL) {
return (NULL);
}
if ((sip_msg = sip_new_msg()) == NULL)
return (NULL);
_dialog = (_sip_dialog_t *)dialog;
(void) pthread_mutex_lock(&_dialog->sip_dlg_mutex);
uri = (char *)sip_dialog_req_uri(_dialog);
if (uri == NULL)
goto err_ret;
if (sip_add_request_line(sip_msg, method, uri) != 0) {
free(uri);
goto err_ret;
}
free(uri);
if (sip_copy_header(sip_msg, _dialog->sip_dlg_local_uri_tag, NULL) != 0)
goto err_ret;
if (sip_copy_header(sip_msg, _dialog->sip_dlg_remote_uri_tag, NULL) !=
0) {
goto err_ret;
}
if (sip_copy_header(sip_msg, _dialog->sip_dlg_local_contact, NULL) != 0)
goto err_ret;
if (sip_add_via(sip_msg, transport, sent_by, sent_by_port, via_param) !=
0) {
goto err_ret;
}
if (sip_add_maxforward(sip_msg, maxforward) != 0)
goto err_ret;
if (sip_copy_header(sip_msg, _dialog->sip_dlg_call_id, NULL) != 0)
goto err_ret;
if (cseq < 0) {
if (_dialog->sip_dlg_local_cseq == 0)
_dialog->sip_dlg_local_cseq = 1;
oldseq = _dialog->sip_dlg_local_cseq;
cseq = ++_dialog->sip_dlg_local_cseq;
}
if (sip_add_cseq(sip_msg, method, cseq) != 0) {
_dialog->sip_dlg_local_cseq = oldseq;
goto err_ret;
}
(void) sip_delete_header_by_name(sip_msg, SIP_ROUTE);
if (_dialog->sip_dlg_route_set != NULL) {
if (sip_copy_header(sip_msg, _dialog->sip_dlg_route_set,
NULL) != 0) {
_dialog->sip_dlg_local_cseq = oldseq;
goto err_ret;
}
}
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return (sip_msg);
err_ret:
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
sip_free_msg(sip_msg);
return (NULL);
}
sip_msg_t
sip_create_dialog_req_nocontact(sip_method_t method, sip_dialog_t dialog,
char *transport, char *sent_by, int sent_by_port, char *via_param,
uint32_t maxforward, int cseq)
{
sip_msg_t sip_msg;
sip_msg = sip_create_dialog_req(method, dialog, transport, sent_by,
sent_by_port, via_param, maxforward, cseq);
if (sip_msg != NULL) {
if (sip_delete_header_by_name(sip_msg, SIP_CONTACT) != 0) {
sip_free_msg(sip_msg);
return (NULL);
}
}
return (sip_msg);
}
int
sip_get_dialog_method(sip_dialog_t dialog, int *error)
{
_sip_dialog_t *_dialog;
if (error != NULL)
*error = 0;
if (!sip_manage_dialog) {
if (error != NULL)
*error = EINVAL;
return (0);
}
if (dialog == NULL) {
if (error != NULL)
*error = EINVAL;
return (0);
}
_dialog = (_sip_dialog_t *)dialog;
return (_dialog->sip_dlg_method);
}
int
sip_get_dialog_state(sip_dialog_t dialog, int *error)
{
_sip_dialog_t *_dialog;
if (error != NULL)
*error = 0;
if (!sip_manage_dialog) {
if (error != NULL)
*error = EINVAL;
return (0);
}
if (dialog == NULL) {
if (error != NULL)
*error = EINVAL;
return (0);
}
_dialog = (_sip_dialog_t *)dialog;
return (_dialog->sip_dlg_state);
}
const sip_str_t *
sip_get_dialog_callid(sip_dialog_t dialog, int *error)
{
_sip_dialog_t *_dialog;
const struct sip_value *val;
const sip_str_t *callid = NULL;
if (error != NULL)
*error = 0;
if (!sip_manage_dialog || dialog == NULL) {
if (error != NULL)
*error = EINVAL;
return (NULL);
}
_dialog = (_sip_dialog_t *)dialog;
(void) pthread_mutex_lock(&_dialog->sip_dlg_mutex);
if (dialog->sip_dlg_call_id != NULL) {
val = sip_get_header_value(_dialog->sip_dlg_call_id, error);
if (val == NULL) {
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return (NULL);
}
callid = &((sip_hdr_value_t *)val)->str_val;
}
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return (callid);
}
const sip_str_t *
sip_get_dialog_local_tag(sip_dialog_t dialog, int *error)
{
_sip_dialog_t *_dialog;
const sip_str_t *ltag = NULL;
const struct sip_value *val;
if (error != NULL)
*error = 0;
if (!sip_manage_dialog || dialog == NULL) {
if (error != NULL)
*error = EINVAL;
return (NULL);
}
_dialog = (_sip_dialog_t *)dialog;
(void) pthread_mutex_lock(&_dialog->sip_dlg_mutex);
if (dialog->sip_dlg_local_uri_tag != NULL) {
val = sip_get_header_value(_dialog->sip_dlg_local_uri_tag,
error);
if (val == NULL) {
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return (NULL);
}
ltag = sip_get_param_value((sip_header_value_t)val, "tag",
error);
}
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return (ltag);
}
const sip_str_t *
sip_get_dialog_remote_tag(sip_dialog_t dialog, int *error)
{
_sip_dialog_t *_dialog;
const sip_str_t *ttag = NULL;
const struct sip_value *val;
if (error != NULL)
*error = 0;
if (!sip_manage_dialog || dialog == NULL) {
if (error != NULL)
*error = EINVAL;
return (NULL);
}
_dialog = (_sip_dialog_t *)dialog;
(void) pthread_mutex_lock(&_dialog->sip_dlg_mutex);
if (dialog->sip_dlg_remote_uri_tag != NULL) {
val = sip_get_header_value(_dialog->sip_dlg_remote_uri_tag,
error);
if (val == NULL) {
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return (NULL);
}
ttag = sip_get_param_value((sip_header_value_t)val, "tag",
error);
}
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return (ttag);
}
const struct sip_uri *
sip_get_dialog_local_uri(sip_dialog_t dialog, int *error)
{
_sip_dialog_t *_dialog;
const _sip_uri_t *luri = NULL;
const struct sip_value *val;
if (error != NULL)
*error = 0;
if (!sip_manage_dialog || dialog == NULL) {
if (error != NULL)
*error = EINVAL;
return (NULL);
}
_dialog = (_sip_dialog_t *)dialog;
(void) pthread_mutex_lock(&_dialog->sip_dlg_mutex);
if (dialog->sip_dlg_local_uri_tag != NULL) {
val = sip_get_header_value(_dialog->sip_dlg_local_uri_tag,
error);
if (val == NULL) {
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return (NULL);
}
luri = val->sip_value_parse_uri;
}
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return ((sip_uri_t)luri);
}
const struct sip_uri *
sip_get_dialog_remote_uri(sip_dialog_t dialog, int *error)
{
_sip_dialog_t *_dialog;
const _sip_uri_t *ruri = NULL;
const struct sip_value *val;
if (error != NULL)
*error = 0;
if (!sip_manage_dialog || dialog == NULL) {
if (error != NULL)
*error = EINVAL;
return (NULL);
}
_dialog = (_sip_dialog_t *)dialog;
(void) pthread_mutex_lock(&_dialog->sip_dlg_mutex);
if (dialog->sip_dlg_remote_uri_tag != NULL) {
val = sip_get_header_value(dialog->sip_dlg_remote_uri_tag,
error);
if (val == NULL) {
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return (NULL);
}
ruri = val->sip_value_parse_uri;
}
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return ((sip_uri_t)ruri);
}
const struct sip_uri *
sip_get_dialog_remote_target_uri(sip_dialog_t dialog, int *error)
{
_sip_dialog_t *_dialog;
const struct sip_uri *rtarg = NULL;
const struct sip_value *val;
if (error != NULL)
*error = 0;
if (!sip_manage_dialog || dialog == NULL) {
if (error != NULL)
*error = EINVAL;
return (NULL);
}
_dialog = (_sip_dialog_t *)dialog;
(void) pthread_mutex_lock(&_dialog->sip_dlg_mutex);
if (dialog->sip_dlg_remote_target != NULL) {
val = sip_get_header_value(_dialog->sip_dlg_remote_target,
error);
if (val == NULL) {
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return (NULL);
}
rtarg = val->sip_value_parse_uri;
}
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return ((sip_uri_t)rtarg);
}
const struct sip_uri *
sip_get_dialog_local_contact_uri(sip_dialog_t dialog, int *error)
{
_sip_dialog_t *_dialog;
const struct sip_uri *lcuri = NULL;
const struct sip_value *val;
if (error != NULL)
*error = 0;
if (!sip_manage_dialog || dialog == NULL) {
if (error != NULL)
*error = EINVAL;
return (NULL);
}
_dialog = (_sip_dialog_t *)dialog;
(void) pthread_mutex_lock(&_dialog->sip_dlg_mutex);
if (dialog->sip_dlg_local_contact != NULL) {
val = sip_get_header_value(_dialog->sip_dlg_local_contact,
error);
if (val == NULL) {
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return (NULL);
}
lcuri = val->sip_value_parse_uri;
}
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return ((sip_uri_t)lcuri);
}
const sip_str_t *
sip_get_dialog_route_set(sip_dialog_t dialog, int *error)
{
_sip_dialog_t *_dialog;
if (error != NULL)
*error = 0;
if (!sip_manage_dialog || dialog == NULL) {
if (error != NULL)
*error = EINVAL;
return (NULL);
}
_dialog = (_sip_dialog_t *)dialog;
if (_dialog->sip_dlg_rset.sip_str_len > 0)
return (&_dialog->sip_dlg_rset);
return (NULL);
}
boolean_t
sip_is_dialog_secure(sip_dialog_t dialog, int *error)
{
_sip_dialog_t *_dialog;
boolean_t issecure;
if (error != NULL)
*error = 0;
if (!sip_manage_dialog || dialog == NULL) {
if (error != NULL)
*error = EINVAL;
return (B_FALSE);
}
_dialog = (_sip_dialog_t *)dialog;
(void) pthread_mutex_lock(&_dialog->sip_dlg_mutex);
issecure = _dialog->sip_dlg_secure;
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return (issecure);
}
uint32_t
sip_get_dialog_local_cseq(sip_dialog_t dialog, int *error)
{
_sip_dialog_t *_dialog;
uint32_t cseq;
if (error != NULL)
*error = 0;
if (!sip_manage_dialog || dialog == NULL) {
if (error != NULL)
*error = EINVAL;
return (0);
}
_dialog = (_sip_dialog_t *)dialog;
(void) pthread_mutex_lock(&_dialog->sip_dlg_mutex);
cseq = _dialog->sip_dlg_local_cseq;
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return (cseq);
}
uint32_t
sip_get_dialog_remote_cseq(sip_dialog_t dialog, int *error)
{
_sip_dialog_t *_dialog;
uint32_t cseq;
if (error != NULL)
*error = 0;
if (!sip_manage_dialog || dialog == NULL) {
if (error != NULL)
*error = EINVAL;
return (0);
}
_dialog = (_sip_dialog_t *)dialog;
(void) pthread_mutex_lock(&_dialog->sip_dlg_mutex);
cseq = _dialog->sip_dlg_remote_cseq;
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return (cseq);
}
int
sip_get_dialog_type(sip_dialog_t dialog, int *error)
{
_sip_dialog_t *_dialog;
int type;
if (error != NULL)
*error = 0;
if (!sip_manage_dialog || dialog == NULL) {
if (error != NULL)
*error = EINVAL;
return (-1);
}
_dialog = (_sip_dialog_t *)dialog;
(void) pthread_mutex_lock(&_dialog->sip_dlg_mutex);
type = _dialog->sip_dlg_type;
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return (type);
}
int
sip_get_dialog_msgcnt(sip_dialog_t dialog, int *error)
{
_sip_dialog_t *_dialog;
int nmsgs;
if (error != NULL)
*error = 0;
if (!sip_manage_dialog || dialog == NULL) {
if (error != NULL)
*error = EINVAL;
return (-1);
}
_dialog = (_sip_dialog_t *)dialog;
(void) pthread_mutex_lock(&_dialog->sip_dlg_mutex);
nmsgs = _dialog->sip_dlg_msgcnt;
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return (nmsgs);
}
boolean_t
sip_incomplete_dialog(sip_dialog_t dialog)
{
_sip_dialog_t *_dialog;
boolean_t isnew;
if (!sip_manage_dialog || dialog == NULL)
return (B_FALSE);
_dialog = (_sip_dialog_t *)dialog;
(void) pthread_mutex_lock(&_dialog->sip_dlg_mutex);
isnew = _dialog->sip_dlg_state == SIP_DLG_NEW;
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
return (isnew);
}
void
sip_hold_dialog(sip_dialog_t dialog)
{
_sip_dialog_t *_dialog;
if (!sip_manage_dialog || dialog == NULL)
return;
_dialog = (_sip_dialog_t *)dialog;
(void) pthread_mutex_lock(&_dialog->sip_dlg_mutex);
SIP_DLG_REFCNT_INCR(_dialog);
(void) pthread_mutex_unlock(&_dialog->sip_dlg_mutex);
}
void
sip_release_dialog(sip_dialog_t dialog)
{
_sip_dialog_t *_dialog;
if (!sip_manage_dialog || dialog == NULL)
return;
_dialog = (_sip_dialog_t *)dialog;
SIP_DLG_REFCNT_DECR(_dialog);
}
void
sip_delete_dialog(sip_dialog_t dialog)
{
if (!sip_manage_dialog || dialog == NULL)
return;
sip_dialog_terminate(dialog, NULL);
}