📄 transaction.c
字号:
/* The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-) Copyright (C) 2001,2002,2003 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 <osip/port.h>#include <osip/osip.h>#include "fsm.h"inttransaction_init (transaction_t ** transaction, context_type_t ctx_type, osip_t * osip, sip_t * request){ static int transactionid = 1; via_t *topvia; int i; time_t now; OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO2, NULL, "allocating transaction ressource %i %s\n", transactionid, request->call_id->number)); *transaction = (transaction_t *) smalloc (sizeof (transaction_t)); if (*transaction == NULL) return -1; now = time (NULL);#ifndef DISABLE_MEMSET memset (*transaction, 0, sizeof (transaction_t));#else (*transaction)->your_instance = NULL; (*transaction)->last_response = NULL; (*transaction)->ack = NULL; (*transaction)->completed_time = 0; (*transaction)->in_socket = 0; (*transaction)->out_socket = 0;#endif (*transaction)->birth_time = now; (*transaction)->transactionid = transactionid; transactionid++; topvia = list_get (request->vias, 0); if (topvia == NULL) goto ti_error_1; i = 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 = transaction_set_from (*transaction, request->from); if (i != 0) goto ti_error_2; i = transaction_set_to (*transaction, request->to); if (i != 0) goto ti_error_3; i = transaction_set_call_id (*transaction, request->call_id); if (i != 0) goto ti_error_4; i = 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 = (fifo_t *) smalloc (sizeof (fifo_t)); if ((*transaction)->transactionff == NULL) goto ti_error_6; 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 = 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 = 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 = 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 = nist_init (&((*transaction)->nist_context), osip, request); if (i != 0) goto ti_error_7; osip_add_nist (osip, *transaction); } return 0;ti_error_7: fifo_free ((*transaction)->transactionff); sfree ((*transaction)->transactionff);ti_error_6: cseq_free ((*transaction)->cseq); sfree ((*transaction)->cseq);ti_error_5: call_id_free ((*transaction)->callid); sfree ((*transaction)->callid);ti_error_4: to_free ((*transaction)->to); sfree ((*transaction)->to);ti_error_3: from_free ((*transaction)->from); sfree ((*transaction)->from);ti_error_2: via_free ((*transaction)->topvia); sfree ((*transaction)->topvia);ti_error_1: sfree (*transaction); 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_ict osip_remove_nict osip_remove_ist osip_remove_nist + transaction_free2(); */inttransaction_free (transaction_t * transaction){ sipevent_t *evt; int i; if (transaction == NULL) return -1; if (transaction->orig_request != 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) { i = osip_remove_ict (transaction->config, transaction); ict_free (transaction->ict_context); sfree (transaction->ict_context); } else if (transaction->ctx_type == IST) { i = osip_remove_ist (transaction->config, transaction); ist_free (transaction->ist_context); sfree (transaction->ist_context); } else if (transaction->ctx_type == NICT) { i = osip_remove_nict (transaction->config, transaction); nict_free (transaction->nict_context); sfree (transaction->nict_context); } else { i = osip_remove_nist (transaction->config, transaction); nist_free (transaction->nist_context); sfree (transaction->nist_context); } if (i != 0) /* yet removed ??? */ { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_BUG, NULL, "transaction already removed from list %i!\n", transaction->transactionid)); } /* empty the fifo */ evt = fifo_tryget (transaction->transactionff); while (evt != NULL) { msg_free (evt->sip); sfree (evt->sip); sfree (evt); evt = fifo_tryget (transaction->transactionff); } fifo_free (transaction->transactionff); sfree (transaction->transactionff); msg_free (transaction->orig_request); sfree (transaction->orig_request); msg_free (transaction->last_response); sfree (transaction->last_response); msg_free (transaction->ack); sfree (transaction->ack); via_free (transaction->topvia); sfree (transaction->topvia); from_free (transaction->from); sfree (transaction->from); to_free (transaction->to); sfree (transaction->to); call_id_free (transaction->callid); sfree (transaction->callid); cseq_free (transaction->cseq); sfree (transaction->cseq); return 0;}/* same as transaction_free() but assume the transaction is already removed from the list of transaction in the osip stack */inttransaction_free2 (transaction_t * transaction){ sipevent_t *evt; if (transaction == NULL) return -1; if (transaction->orig_request != 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) { ict_free (transaction->ict_context); sfree (transaction->ict_context); } else if (transaction->ctx_type == IST) { ist_free (transaction->ist_context); sfree (transaction->ist_context); } else if (transaction->ctx_type == NICT) { nict_free (transaction->nict_context); sfree (transaction->nict_context); } else { nist_free (transaction->nist_context); sfree (transaction->nist_context); } /* empty the fifo */ evt = fifo_tryget (transaction->transactionff); while (evt != NULL) { msg_free (evt->sip); sfree (evt->sip); sfree (evt); evt = fifo_tryget (transaction->transactionff); } fifo_free (transaction->transactionff); sfree (transaction->transactionff); msg_free (transaction->orig_request); sfree (transaction->orig_request); msg_free (transaction->last_response); sfree (transaction->last_response); msg_free (transaction->ack); sfree (transaction->ack); via_free (transaction->topvia); sfree (transaction->topvia); from_free (transaction->from); sfree (transaction->from); to_free (transaction->to); sfree (transaction->to); call_id_free (transaction->callid); sfree (transaction->callid); cseq_free (transaction->cseq); sfree (transaction->cseq); return 0;}inttransaction_add_event (transaction_t * transaction, sipevent_t * evt){ fifo_add (transaction->transactionff, evt); return 0;}inttransaction_execute (transaction_t * transaction, sipevent_t * evt){ 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 */ /* transaction_free(transaction); sfree(transaction); */ sfree (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)); 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 (); if (-1 == fsm_callmethod (evt->type, transaction->state, statemachine, evt, transaction)) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO3, NULL, "USELESS event!\n")); /* message is useless. */ if (EVT_IS_MSG (evt)) { if (evt->sip != NULL) { msg_free (evt->sip); sfree (evt->sip); } } } else { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO4, NULL, "sipevent evt: method called!\n")); } sfree (evt); /* this is the ONLY place for freeing event!! */ return 1;}inttransaction_get_destination(transaction_t * transaction, char **ip, int *port){ *ip=NULL; *port=0; if (transaction==NULL) return -1; if (transaction->ict_context!=NULL) { *ip = transaction->ict_context->destination; *port = transaction->ict_context->port; return 0; } else if (transaction->nict_context!=NULL) { *ip = transaction->nict_context->destination; *port = transaction->nict_context->port; return 0; } return -1;}inttransaction_set_your_instance (transaction_t * transaction, void *instance){ if (transaction == NULL) return -1; transaction->your_instance = instance; return 0;}void *transaction_get_your_instance (transaction_t * transaction){ if (transaction == NULL) return NULL; return transaction->your_instance;}inttransaction_set_topvia (transaction_t * transaction, via_t * topvia){ int i; if (transaction == NULL) return -1; i = via_clone (topvia, &(transaction->topvia)); if (i == 0) return 0; transaction->topvia = NULL; return -1;}inttransaction_set_from (transaction_t * transaction, from_t * from){ int i;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -