📄 osip.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 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#include <osip2/osip_dialog.h>#ifdef OSIP_MTstatic struct osip_mutex *ixt_fastmutex;#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 */#ifndef MINISIZE __ict_load_fsm (); __ist_load_fsm (); __nict_load_fsm (); __nist_load_fsm ();#endif /* 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 (); ixt_fastmutex = osip_mutex_init ();#endif return 0;}static void__osip_global_free (){#ifndef MINISIZE __ict_unload_fsm (); __ist_unload_fsm (); __nict_unload_fsm (); __nist_unload_fsm ();#endif#ifdef OSIP_MT osip_mutex_destroy (ict_fastmutex); osip_mutex_destroy (ist_fastmutex); osip_mutex_destroy (nict_fastmutex); osip_mutex_destroy (nist_fastmutex); osip_mutex_destroy (ixt_fastmutex);#endif}voidosip_response_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;}intosip_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){ /* add in list 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; /* add in list 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);}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->interval = DEFAULT_T1; osip_gettimeofday(&pixt->start, NULL); add_gettimeofday(&pixt->start, pixt->interval+10); pixt->counter = 10; 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; osip_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 */struct osip_dialog *osip_stop_200ok_retransmissions (osip_t * osip, osip_message_t * ack){ osip_dialog_t *dialog = NULL; 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); dialog = ixt->dialog; ixt_free (ixt); break; } } osip_ixt_unlock (osip); return dialog;}/* 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, struct timeval *current){ if (osip_timercmp(current, &ixt->start, >)) { ixt->interval = ixt->interval * 2; if (ixt->interval > 4000) ixt->interval = 4000; add_gettimeofday (&ixt->start, ixt->interval); 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; ixt_t *ixt; struct timeval current; osip_gettimeofday (¤t, 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, ¤t); if (ixt->counter == 0) { /* remove it */ osip_list_remove (&osip->ixt_retransmissions, i); ixt_free (ixt); i--; } } osip_ixt_unlock (osip);}intosip_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}#if defined(HAVE_DICT_DICT_H)#define HSIZE 200unsigned s_hash (const unsigned char *p);unsigneds_hash (const unsigned char *p){ unsigned hash = 0; while (*p) { hash *= 31; hash ^= *p++; } return hash;}#endifint__osip_add_ict (osip_t * osip, osip_transaction_t * ict){#ifdef OSIP_MT osip_mutex_lock (ict_fastmutex);#endif#if defined(HAVE_DICT_DICT_H) { osip_generic_param_t *b_request = NULL; int rv = -99; osip_via_param_get_byname (ict->topvia, "branch", &b_request); if (b_request != NULL && b_request->gvalue != NULL) rv = dict_insert (osip->osip_ict_hastable, b_request->gvalue, (void *) ict, FALSE);#if 0 else rv = dict_insert (osip->osip_ict_hastable, b_request->gvalue, (void *) ict, FALSE);#endif if (rv == 0) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO1, NULL, "New key inserted in ict hastable `%s'\n", b_request->gvalue)); } else if (rv != -99) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_WARNING, NULL, "already inserted `%s'\n", b_request->gvalue)); } }#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#if defined(HAVE_DICT_DICT_H) { osip_generic_param_t *b_request = NULL; int rv = -99; osip_via_param_get_byname (ist->topvia, "branch", &b_request); if (b_request != NULL && b_request->gvalue != NULL) rv = dict_insert (osip->osip_ist_hastable, b_request->gvalue, (void *) ist, FALSE); else#if 0 rv = dict_insert (osip->osip_ist_hastable, b_request->gvalue, (void *) ist, FALSE);#endif if (rv == 0) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_INFO1, NULL, "New key inserted in ist hastable `%s'\n", b_request->gvalue)); } else if (rv != -99) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_WARNING, NULL, "already inserted `%s'\n", b_request->gvalue)); } }#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#if defined(HAVE_DICT_DICT_H) { osip_generic_param_t *b_request = NULL; int rv = -99;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -