📄 osip.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 <osip2/internal.h>#include <osip2/osip.h>#include "fsm.h"#include "xixt.h"#ifdef OSIP_MTstatic struct osip_mutex *ict_fastmutex;static struct osip_mutex *ist_fastmutex;static struct osip_mutex *nict_fastmutex;static struct osip_mutex *nist_fastmutex;#endif#ifdef OSIP_RETRANSMIT_2XX#include <osip2/osip_dialog.h>#ifdef OSIP_MTstatic struct osip_mutex *ixt_fastmutex;#endif#endifstatic int __osip_global_init (void);static void __osip_global_free (void);static int increase_ref_count (void);static void decrease_ref_count (void);static int__osip_global_init (){ /* load the fsm configuration */ __ict_load_fsm (); __ist_load_fsm (); __nict_load_fsm (); __nist_load_fsm (); /* load the parser configuration */ parser_init ();#ifdef OSIP_MT ict_fastmutex = osip_mutex_init (); ist_fastmutex = osip_mutex_init (); nict_fastmutex = osip_mutex_init (); nist_fastmutex = osip_mutex_init ();#ifdef OSIP_RETRANSMIT_2XX ixt_fastmutex = osip_mutex_init ();#endif#endif return 0;}static void__osip_global_free (){ __ict_unload_fsm (); __ist_unload_fsm (); __nict_unload_fsm (); __nist_unload_fsm ();#ifdef OSIP_MT osip_mutex_destroy (ict_fastmutex); osip_mutex_destroy (ist_fastmutex); osip_mutex_destroy (nict_fastmutex); osip_mutex_destroy (nist_fastmutex);#ifdef OSIP_RETRANSMIT_2XX osip_mutex_destroy (ixt_fastmutex);#endif#endif}#ifdef OSIP_RETRANSMIT_2XXintosip_ixt_lock (osip_t * osip){#ifdef OSIP_MT return osip_mutex_lock (ixt_fastmutex);#else return 0;#endif}intosip_ixt_unlock (osip_t * osip){#ifdef OSIP_MT return osip_mutex_unlock (ixt_fastmutex);#else return 0;#endif}/* these are for transactions that would need retransmission not handled by state machines */voidosip_add_ixt (osip_t * osip, ixt_t * ixt){ /* ajout dans la liste de osip_t->ixt */ osip_ixt_lock (osip); osip_list_add (osip->ixt_retransmissions, (void *) ixt, 0); osip_ixt_unlock (osip);}voidosip_remove_ixt (osip_t * osip, ixt_t * ixt){ int i; int found = 0; ixt_t *tmp; /* ajout dans la liste de osip_t->ixt */ osip_ixt_lock (osip); for (i = 0; !osip_list_eol (osip->ixt_retransmissions, i); i++) { tmp = (ixt_t *) osip_list_get (osip->ixt_retransmissions, i); if (tmp == ixt) { osip_list_remove (osip->ixt_retransmissions, i); found = 1; break; } } osip_ixt_unlock (osip);}voidresponse_get_destination (osip_message_t * response, char **address, int *portnum){ osip_via_t *via; char *host = NULL; int port = 0; via = (osip_via_t *) osip_list_get (response->vias, 0); if (via) { osip_generic_param_t *maddr; osip_generic_param_t *received; osip_generic_param_t *rport; osip_via_param_get_byname (via, "maddr", &maddr); osip_via_param_get_byname (via, "received", &received); osip_via_param_get_byname (via, "rport", &rport); /* 1: user should not use the provided information (host and port) if they are using a reliable transport. Instead, they should use the already open socket attached to this transaction. */ /* 2: check maddr and multicast usage */ if (maddr != NULL) host = maddr->gvalue; /* we should check if this is a multicast address and use set the "ttl" in this case. (this must be done in the UDP message (not at the SIP layer) */ else if (received != NULL) host = received->gvalue; else host = via->host; if (rport == NULL || rport->gvalue == NULL) { if (via->port != NULL) port = osip_atoi (via->port); else port = 5060; } else port = osip_atoi (rport->gvalue); } *portnum = port; if (host != NULL) *address = osip_strdup (host); else *address = NULL;}intixt_init (ixt_t ** ixt){ ixt_t *pixt; *ixt = pixt = (ixt_t *) osip_malloc (sizeof (ixt_t)); if (pixt==NULL) return -1; pixt->dialog = NULL; pixt->msg2xx = NULL; pixt->ack = NULL; pixt->start = time (NULL); pixt->interval = 500; pixt->counter = 7; pixt->dest = NULL; pixt->port = 5060; pixt->sock = -1; return 0;}voidixt_free (ixt_t * ixt){ osip_message_free (ixt->ack); osip_message_free (ixt->msg2xx); osip_free (ixt->dest); osip_free (ixt);}/* usefull for UAs */voidosip_start_200ok_retransmissions (osip_t * osip, osip_dialog_t * dialog, osip_message_t * msg200ok, int sock){ ixt_t *ixt; ixt_init (&ixt); ixt->dialog = dialog; osip_message_clone (msg200ok, &ixt->msg2xx); ixt->sock = sock; response_get_destination (msg200ok, &ixt->dest, &ixt->port); osip_add_ixt (osip, ixt);}voidosip_start_ack_retransmissions (osip_t * osip, osip_dialog_t * dialog, osip_message_t * ack, char *dest, int port, int sock){ int i; ixt_t *ixt; i = ixt_init (&ixt); if (i != 0) return; ixt->dialog = dialog; osip_message_clone (ack, &ixt->ack); ixt->dest = osip_strdup (dest); ixt->port = port; ixt->sock = sock; osip_add_ixt (osip, ixt);}/* we stop the 200ok when receiving the corresponding ack */voidosip_stop_200ok_retransmissions (osip_t * osip, osip_message_t * ack){ int i; ixt_t *ixt; osip_ixt_lock (osip); for (i = 0; !osip_list_eol (osip->ixt_retransmissions, i); i++) { ixt = (ixt_t *) osip_list_get (osip->ixt_retransmissions, i); if (osip_dialog_match_as_uas (ixt->dialog, ack) == 0) { osip_list_remove (osip->ixt_retransmissions, i); ixt_free (ixt); break; } } osip_ixt_unlock (osip);}/* when a dialog is destroyed by the application, it is safer to remove all ixt that are related to it */voidosip_stop_retransmissions_from_dialog (osip_t * osip, osip_dialog_t * dialog){ int i; ixt_t *ixt; osip_ixt_lock (osip); for (i = 0; !osip_list_eol (osip->ixt_retransmissions, i); i++) { ixt = (ixt_t *) osip_list_get (osip->ixt_retransmissions, i); if (ixt->dialog == dialog) { osip_list_remove (osip->ixt_retransmissions, i); ixt_free (ixt); i--; } } osip_ixt_unlock (osip);}voidixt_retransmit (osip_t * osip, ixt_t * ixt, time_t current){ if ((current - ixt->start) * 1000 > ixt->interval) { ixt->interval = ixt->interval * 2; ixt->start = current; if (ixt->ack != NULL) osip->cb_send_message (NULL, ixt->ack, ixt->dest, ixt->port, ixt->sock); else if (ixt->msg2xx != NULL) osip->cb_send_message (NULL, ixt->msg2xx, ixt->dest, ixt->port, ixt->sock); ixt->counter--; }}voidosip_retransmissions_execute (osip_t * osip){ int i; time_t current; ixt_t *ixt; current = time (NULL); osip_ixt_lock (osip); for (i = 0; !osip_list_eol (osip->ixt_retransmissions, i); i++) { ixt = (ixt_t *) osip_list_get (osip->ixt_retransmissions, i); ixt_retransmit (osip, ixt, current); if (ixt->counter == 0) { /* remove it */ osip_list_remove (osip->ixt_retransmissions, i); ixt_free (ixt); i--; } } osip_ixt_unlock (osip);}#endifintosip_ict_lock (osip_t * osip){#ifdef OSIP_MT return osip_mutex_lock (ict_fastmutex);#else return 0;#endif}intosip_ict_unlock (osip_t * osip){#ifdef OSIP_MT return osip_mutex_unlock (ict_fastmutex);#else return 0;#endif}intosip_ist_lock (osip_t * osip){#ifdef OSIP_MT return osip_mutex_lock (ist_fastmutex);#else return 0;#endif}intosip_ist_unlock (osip_t * osip){#ifdef OSIP_MT return osip_mutex_unlock (ist_fastmutex);#else return 0;#endif}intosip_nict_lock (osip_t * osip){#ifdef OSIP_MT return osip_mutex_lock (nict_fastmutex);#else return 0;#endif}intosip_nict_unlock (osip_t * osip){#ifdef OSIP_MT return osip_mutex_unlock (nict_fastmutex);#else return 0;#endif}intosip_nist_lock (osip_t * osip){#ifdef OSIP_MT return osip_mutex_lock (nist_fastmutex);#else return 0;#endif}intosip_nist_unlock (osip_t * osip){#ifdef OSIP_MT return osip_mutex_unlock (nist_fastmutex);#else return 0;#endif}int__osip_add_ict (osip_t * osip, osip_transaction_t * ict){#ifdef OSIP_MT osip_mutex_lock (ict_fastmutex);#endif osip_list_add (osip->osip_ict_transactions, ict, -1);#ifdef OSIP_MT osip_mutex_unlock (ict_fastmutex);#endif return 0;}int__osip_add_ist (osip_t * osip, osip_transaction_t * ist){#ifdef OSIP_MT osip_mutex_lock (ist_fastmutex);#endif osip_list_add (osip->osip_ist_transactions, ist, -1);#ifdef OSIP_MT osip_mutex_unlock (ist_fastmutex);#endif return 0;}int__osip_add_nict (osip_t * osip, osip_transaction_t * nict){#ifdef OSIP_MT osip_mutex_lock (nict_fastmutex);#endif osip_list_add (osip->osip_nict_transactions, nict, -1);#ifdef OSIP_MT osip_mutex_unlock (nict_fastmutex);#endif return 0;}int__osip_add_nist (osip_t * osip, osip_transaction_t * nist){#ifdef OSIP_MT osip_mutex_lock (nist_fastmutex);#endif osip_list_add (osip->osip_nist_transactions, nist, -1);#ifdef OSIP_MT osip_mutex_unlock (nist_fastmutex);#endif return 0;}intosip_remove_transaction (osip_t * osip, osip_transaction_t * tr){ int i = -1; if (tr == NULL) return -1; if (tr->ctx_type == ICT) i = __osip_remove_ict_transaction (osip, tr); else if (tr->ctx_type == IST) i = __osip_remove_ist_transaction (osip, tr); else if (tr->ctx_type == NICT) i = __osip_remove_nict_transaction (osip, tr); else if (tr->ctx_type == NIST) i = __osip_remove_nist_transaction (osip, tr); else return -1; return i;}int__osip_remove_ict_transaction (osip_t * osip, osip_transaction_t * ict){ int pos = 0; osip_transaction_t *tmp;#ifdef OSIP_MT osip_mutex_lock (ict_fastmutex);#endif while (!osip_list_eol (osip->osip_ict_transactions, pos)) { tmp = osip_list_get (osip->osip_ict_transactions, pos); if (tmp->transactionid == ict->transactionid) { osip_list_remove (osip->osip_ict_transactions, pos);#ifdef OSIP_MT osip_mutex_unlock (ict_fastmutex);#endif return 0; } pos++; }#ifdef OSIP_MT osip_mutex_unlock (ict_fastmutex);#endif return -1;}int__osip_remove_ist_transaction (osip_t * osip, osip_transaction_t * ist){ int pos = 0; osip_transaction_t *tmp;#ifdef OSIP_MT osip_mutex_lock (ist_fastmutex);#endif while (!osip_list_eol (osip->osip_ist_transactions, pos)) { tmp = osip_list_get (osip->osip_ist_transactions, pos); if (tmp->transactionid == ist->transactionid) { osip_list_remove (osip->osip_ist_transactions, pos);#ifdef OSIP_MT osip_mutex_unlock (ist_fastmutex);#endif return 0; } pos++; }#ifdef OSIP_MT osip_mutex_unlock (ist_fastmutex);#endif return -1;}int__osip_remove_nict_transaction (osip_t * osip, osip_transaction_t * nict){ int pos = 0; osip_transaction_t *tmp;#ifdef OSIP_MT
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -