📄 osip_transaction.c
字号:
/* The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-) Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Aymeric MOIZARD jack@atosc.org This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/#include <osip2/internal.h>#include <osip2/osip.h>#include "fsm.h"#include "xixt.h"#ifdef MINISIZEextern osip_statemachine_t ict_fsm;extern osip_statemachine_t ist_fsm;extern osip_statemachine_t nict_fsm;extern osip_statemachine_t nist_fsm;#endifstatic int __osip_transaction_set_topvia (osip_transaction_t * transaction, osip_via_t * topvia);static int __osip_transaction_set_from (osip_transaction_t * transaction, osip_from_t * from);static int __osip_transaction_set_to (osip_transaction_t * transaction, osip_to_t * to);static int __osip_transaction_set_call_id (osip_transaction_t * transaction, osip_call_id_t * call_id);static int __osip_transaction_set_cseq (osip_transaction_t * transaction, osip_cseq_t * cseq);static int__osip_transaction_set_topvia (osip_transaction_t * transaction, osip_via_t * topvia){ int i; if (transaction == NULL) return -1; i = osip_via_clone (topvia, &(transaction->topvia)); if (i == 0) return 0; transaction->topvia = NULL; return -1;}static int__osip_transaction_set_from (osip_transaction_t * transaction, osip_from_t * from){ int i; if (transaction == NULL) return -1; i = osip_from_clone (from, &(transaction->from)); if (i == 0) return 0; transaction->from = NULL; return -1;}static int__osip_transaction_set_to (osip_transaction_t * transaction, osip_to_t * to){ int i; if (transaction == NULL) return -1; i = osip_to_clone (to, &(transaction->to)); if (i == 0) return 0; transaction->to = NULL; return -1;}static int__osip_transaction_set_call_id (osip_transaction_t * transaction, osip_call_id_t * call_id){ int i; if (transaction == NULL) return -1; i = osip_call_id_clone (call_id, &(transaction->callid)); if (i == 0) return 0; transaction->callid = NULL; return -1;}static int__osip_transaction_set_cseq (osip_transaction_t * transaction, osip_cseq_t * cseq){ int i; if (transaction == NULL) return -1; i = osip_cseq_clone (cseq, &(transaction->cseq)); if (i == 0) return 0; transaction->cseq = NULL; return -1;}intosip_transaction_init (osip_transaction_t ** transaction, osip_fsm_type_t ctx_type, osip_t * osip, osip_message_t * request){ static int transactionid = 1; osip_via_t *topvia; int i; time_t now; *transaction = NULL; if (request == NULL) return -1; if (request->call_id == NULL) return -1; if (request->call_id->number == NULL) return -1; OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO2, NULL, "allocating transaction ressource %i %s\n", transactionid, request->call_id->number)); *transaction = (osip_transaction_t *) osip_malloc (sizeof (osip_transaction_t)); if (*transaction == NULL) return -1; now = time (NULL); memset (*transaction, 0, sizeof (osip_transaction_t)); (*transaction)->birth_time = now; (*transaction)->transactionid = transactionid; transactionid++; topvia = osip_list_get (&request->vias, 0); if (topvia == NULL) goto ti_error_1; i = __osip_transaction_set_topvia (*transaction, topvia); if (i != 0) goto ti_error_1; /* In some situation, some of those informtions might be useless. Mostly, I prefer to keep them in all case for backward compatibility. */ i = __osip_transaction_set_from (*transaction, request->from); if (i != 0) goto ti_error_2; i = __osip_transaction_set_to (*transaction, request->to); if (i != 0) goto ti_error_3; i = __osip_transaction_set_call_id (*transaction, request->call_id); if (i != 0) goto ti_error_4; i = __osip_transaction_set_cseq (*transaction, request->cseq); if (i != 0) goto ti_error_5; /* RACE conditions can happen for server transactions */ /* (*transaction)->orig_request = request; */ (*transaction)->orig_request = NULL; (*transaction)->config = osip; (*transaction)->transactionff = (osip_fifo_t *) osip_malloc (sizeof (osip_fifo_t)); if ((*transaction)->transactionff == NULL) goto ti_error_6; osip_fifo_init ((*transaction)->transactionff); (*transaction)->ctx_type = ctx_type; (*transaction)->ict_context = NULL; (*transaction)->ist_context = NULL; (*transaction)->nict_context = NULL; (*transaction)->nist_context = NULL; if (ctx_type == ICT) { (*transaction)->state = ICT_PRE_CALLING; i = __osip_ict_init (&((*transaction)->ict_context), osip, request); if (i != 0) goto ti_error_7; __osip_add_ict (osip, *transaction); } else if (ctx_type == IST) { (*transaction)->state = IST_PRE_PROCEEDING; i = __osip_ist_init (&((*transaction)->ist_context), osip, request); if (i != 0) goto ti_error_7; __osip_add_ist (osip, *transaction); } else if (ctx_type == NICT) { (*transaction)->state = NICT_PRE_TRYING; i = __osip_nict_init (&((*transaction)->nict_context), osip, request); if (i != 0) goto ti_error_7; __osip_add_nict (osip, *transaction); } else { (*transaction)->state = NIST_PRE_TRYING; i = __osip_nist_init (&((*transaction)->nist_context), osip, request); if (i != 0) goto ti_error_7; __osip_add_nist (osip, *transaction); } return 0;ti_error_7: osip_fifo_free ((*transaction)->transactionff);ti_error_6: osip_cseq_free ((*transaction)->cseq);ti_error_5: osip_call_id_free ((*transaction)->callid);ti_error_4: osip_to_free ((*transaction)->to);ti_error_3: osip_from_free ((*transaction)->from);ti_error_2: osip_via_free ((*transaction)->topvia);ti_error_1: osip_free (*transaction); *transaction = NULL; return -1;}/* This method automaticly remove the transaction context from the osip stack. This task is required for proper operation when a transaction goes in the TERMINATED STATE. However the user might want to just take the context out of the SIP stack andf keep it for future use without freeing all ressource.... This way the transaction context can be kept without being used by the oSIP stack. new methods that replace this one: osip_remove_transaction + osip_transaction_free2(); */intosip_transaction_free (osip_transaction_t * transaction){ int i; if (transaction == NULL) return -1; i = osip_remove_transaction (transaction->config, transaction); if (i != 0) /* yet removed ??? */ { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL, "transaction already removed from list %i!\n", transaction->transactionid)); } return osip_transaction_free2 (transaction);}/* same as osip_transaction_free() but assume the transaction is already removed from the list of transaction in the osip stack */intosip_transaction_free2 (osip_transaction_t * transaction){ osip_event_t *evt; if (transaction == NULL) return -1; if (transaction->orig_request != NULL && transaction->orig_request->call_id != NULL && transaction->orig_request->call_id->number != NULL) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO2, NULL, "free transaction ressource %i %s\n", transaction->transactionid, transaction->orig_request->call_id->number)); } if (transaction->ctx_type == ICT) { __osip_ict_free (transaction->ict_context); } else if (transaction->ctx_type == IST) { __osip_ist_free (transaction->ist_context); } else if (transaction->ctx_type == NICT) { __osip_nict_free (transaction->nict_context); } else { __osip_nist_free (transaction->nist_context); } /* empty the fifo */ evt = osip_fifo_tryget (transaction->transactionff); while (evt != NULL) { osip_message_free (evt->sip); osip_free (evt); evt = osip_fifo_tryget (transaction->transactionff); } osip_fifo_free (transaction->transactionff); osip_message_free (transaction->orig_request); osip_message_free (transaction->last_response); osip_message_free (transaction->ack); osip_via_free (transaction->topvia); osip_from_free (transaction->from); osip_to_free (transaction->to); osip_call_id_free (transaction->callid); osip_cseq_free (transaction->cseq); osip_free (transaction); return 0;}intosip_transaction_add_event (osip_transaction_t * transaction, osip_event_t * evt){ if (evt == NULL) return -1; if (transaction == NULL) return -1; evt->transactionid = transaction->transactionid; osip_fifo_add (transaction->transactionff, evt); return 0;}intosip_transaction_execute (osip_transaction_t * transaction, osip_event_t * evt){ osip_statemachine_t *statemachine; /* to kill the process, simply send this type of event. */ if (EVT_IS_KILL_TRANSACTION (evt)) { /* MAJOR CHANGE! TRANSACTION MUST NOW BE RELEASED BY END-USER: So Any usefull data can be save and re-used */ /* osip_transaction_free(transaction); osip_free(transaction); */ osip_free (evt); return 0; } OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL, "sipevent tr->transactionid: %i\n", transaction->transactionid)); OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL, "sipevent tr->state: %i\n", transaction->state)); OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL, "sipevent evt->type: %i\n", evt->type)); OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL, "sipevent evt->sip: %x\n", evt->sip));#ifndef MINISIZE if (transaction->ctx_type == ICT) statemachine = __ict_get_fsm (); else if (transaction->ctx_type == IST) statemachine = __ist_get_fsm (); else if (transaction->ctx_type == NICT) statemachine = __nict_get_fsm (); else statemachine = __nist_get_fsm ();#else if (transaction->ctx_type == ICT) statemachine = &ict_fsm; else if (transaction->ctx_type == IST) statemachine = &ist_fsm; else if (transaction->ctx_type == NICT) statemachine = &nict_fsm; else statemachine = &nist_fsm;#endif if (-1 == fsm_callmethod (evt->type, transaction->state, statemachine, evt, transaction)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -