📄 sip_ua_layer.c
字号:
/* $Id: sip_ua_layer.c 988 2007-02-20 18:47:12Z bennylp $ *//* * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <pjsip/sip_ua_layer.h>#include <pjsip/sip_module.h>#include <pjsip/sip_dialog.h>#include <pjsip/sip_endpoint.h>#include <pjsip/sip_errno.h>#include <pjsip/sip_transaction.h>#include <pj/os.h>#include <pj/hash.h>#include <pj/assert.h>#include <pj/string.h>#include <pj/pool.h>#include <pj/log.h>#define THIS_FILE "sip_ua_layer.c"/* * Static prototypes. */static pj_status_t mod_ua_load(pjsip_endpoint *endpt);static pj_status_t mod_ua_unload(void);static pj_bool_t mod_ua_on_rx_request(pjsip_rx_data *rdata);static pj_bool_t mod_ua_on_rx_response(pjsip_rx_data *rdata);static void mod_ua_on_tsx_state(pjsip_transaction*, pjsip_event*);extern long pjsip_dlg_lock_tls_id; /* defined in sip_dialog.c *//* This struct is used to represent list of dialog inside a dialog set. * We don't want to use pjsip_dialog for this purpose, to save some * memory (about 100 bytes per dialog set). */struct dlg_set_head{ PJ_DECL_LIST_MEMBER(pjsip_dialog);};/* This struct represents a dialog set. * This is the value that will be put in the UA's hash table. */struct dlg_set{ /* To put this node in free dlg_set nodes in UA. */ PJ_DECL_LIST_MEMBER(struct dlg_set); /* This is the buffer to store this entry in the hash table. */ char ht_entry[PJ_HASH_ENTRY_SIZE]; /* List of dialog in this dialog set. */ struct dlg_set_head dlg_list;};/* * Module interface. */static struct user_agent{ pjsip_module mod; pj_pool_t *pool; pjsip_endpoint *endpt; pj_mutex_t *mutex; pj_hash_table_t *dlg_table; pjsip_ua_init_param param; struct dlg_set free_dlgset_nodes;} mod_ua = { { NULL, NULL, /* prev, next. */ { "mod-ua", 6 }, /* Name. */ -1, /* Id */ PJSIP_MOD_PRIORITY_UA_PROXY_LAYER, /* Priority */ &mod_ua_load, /* load() */ NULL, /* start() */ NULL, /* stop() */ &mod_ua_unload, /* unload() */ &mod_ua_on_rx_request, /* on_rx_request() */ &mod_ua_on_rx_response, /* on_rx_response() */ NULL, /* on_tx_request. */ NULL, /* on_tx_response() */ &mod_ua_on_tsx_state, /* on_tsx_state() */ }};/* * mod_ua_load() * * Called when module is being loaded by endpoint. */static pj_status_t mod_ua_load(pjsip_endpoint *endpt){ pj_status_t status; /* Initialize the user agent. */ mod_ua.endpt = endpt; mod_ua.pool = pjsip_endpt_create_pool( endpt, "ua%p", PJSIP_POOL_LEN_UA, PJSIP_POOL_INC_UA); if (mod_ua.pool == NULL) return PJ_ENOMEM; status = pj_mutex_create_recursive(mod_ua.pool, " ua%p", &mod_ua.mutex); if (status != PJ_SUCCESS) return status; mod_ua.dlg_table = pj_hash_create(mod_ua.pool, PJSIP_MAX_DIALOG_COUNT); if (mod_ua.dlg_table == NULL) return PJ_ENOMEM; pj_list_init(&mod_ua.free_dlgset_nodes); /* Initialize dialog lock. */ status = pj_thread_local_alloc(&pjsip_dlg_lock_tls_id); if (status != PJ_SUCCESS) return status; pj_thread_local_set(pjsip_dlg_lock_tls_id, NULL); return PJ_SUCCESS;}/* * mod_ua_unload() * * Called when module is being unloaded. */static pj_status_t mod_ua_unload(void){ pj_thread_local_free(pjsip_dlg_lock_tls_id); pj_mutex_destroy(mod_ua.mutex); /* Release pool */ if (mod_ua.pool) { pjsip_endpt_release_pool( mod_ua.endpt, mod_ua.pool ); } return PJ_SUCCESS;}/* * mod_ua_on_tsx_stats() * * Called on changed on transaction state. */static void mod_ua_on_tsx_state( pjsip_transaction *tsx, pjsip_event *e){ pjsip_dialog *dlg; /* Get the dialog where this transaction belongs. */ dlg = tsx->mod_data[mod_ua.mod.id]; /* If dialog instance has gone, it could mean that the dialog * may has been destroyed. */ if (dlg == NULL) return; /* Hand over the event to the dialog. */ pjsip_dlg_on_tsx_state(dlg, tsx, e);}/* * Init user agent module and register it to the endpoint. */PJ_DEF(pj_status_t) pjsip_ua_init_module( pjsip_endpoint *endpt, const pjsip_ua_init_param *prm){ pj_status_t status; /* Check if module already registered. */ PJ_ASSERT_RETURN(mod_ua.mod.id == -1, PJ_EINVALIDOP); /* Copy param, if exists. */ if (prm) pj_memcpy(&mod_ua.param, prm, sizeof(pjsip_ua_init_param)); /* Register the module. */ status = pjsip_endpt_register_module(endpt, &mod_ua.mod); return status;}/* * Get the instance of the user agent. * */PJ_DEF(pjsip_user_agent*) pjsip_ua_instance(void){ return &mod_ua.mod;}/* * Get the endpoint where this UA is currently registered. */PJ_DEF(pjsip_endpoint*) pjsip_ua_get_endpt(pjsip_user_agent *ua){ PJ_UNUSED_ARG(ua); pj_assert(ua == &mod_ua.mod); return mod_ua.endpt;}/* * Destroy the user agent layer. */PJ_DEF(pj_status_t) pjsip_ua_destroy(void){ /* Check if module already destroyed. */ PJ_ASSERT_RETURN(mod_ua.mod.id != -1, PJ_EINVALIDOP); return pjsip_endpt_unregister_module(mod_ua.endpt, &mod_ua.mod);}/* * Create key to identify dialog set. */PJ_DEF(void) pjsip_ua_create_dlg_set_key( pj_pool_t *pool, pj_str_t *set_key, const pj_str_t *call_id, const pj_str_t *local_tag){ PJ_ASSERT_ON_FAIL(pool && set_key && call_id && local_tag, return;); set_key->slen = call_id->slen + local_tag->slen + 1; set_key->ptr = pj_pool_alloc(pool, set_key->slen); pj_assert(set_key->ptr != NULL); pj_memcpy(set_key->ptr, call_id->ptr, call_id->slen); set_key->ptr[call_id->slen] = '$'; pj_memcpy(set_key->ptr + call_id->slen + 1, local_tag->ptr, local_tag->slen);}/* * Acquire one dlg_set node to be put in the hash table. * This will first look in the free nodes list, then allocate * a new one from UA's pool when one is not available. */static struct dlg_set *alloc_dlgset_node(void){ struct dlg_set *set; if (!pj_list_empty(&mod_ua.free_dlgset_nodes)) { set = mod_ua.free_dlgset_nodes.next; pj_list_erase(set); return set; } else { set = pj_pool_alloc(mod_ua.pool, sizeof(struct dlg_set)); return set; }}/* * Register new dialog. Called by pjsip_dlg_create_uac() and * pjsip_dlg_create_uas(); */PJ_DEF(pj_status_t) pjsip_ua_register_dlg( pjsip_user_agent *ua, pjsip_dialog *dlg ){ /* Sanity check. */ PJ_ASSERT_RETURN(ua && dlg, PJ_EINVAL); /* For all dialogs, local tag (inc hash) must has been initialized. */ PJ_ASSERT_RETURN(dlg->local.info && dlg->local.info->tag.slen && dlg->local.tag_hval != 0, PJ_EBUG); /* For UAS dialog, remote tag (inc hash) must have been initialized. */ //PJ_ASSERT_RETURN(dlg->role==PJSIP_ROLE_UAC || // (dlg->role==PJSIP_ROLE_UAS && dlg->remote.info->tag.slen // && dlg->remote.tag_hval != 0), PJ_EBUG); /* Lock the user agent. */ pj_mutex_lock(mod_ua.mutex); /* For UAC, check if there is existing dialog in the same set. */ if (dlg->role == PJSIP_ROLE_UAC) { struct dlg_set *dlg_set; dlg_set = pj_hash_get( mod_ua.dlg_table, dlg->local.info->tag.ptr, dlg->local.info->tag.slen, &dlg->local.tag_hval); if (dlg_set) { /* This is NOT the first dialog in the dialog set. * Just add this dialog in the list. */ pj_assert(dlg_set->dlg_list.next != (void*)&dlg_set->dlg_list); pj_list_push_back(&dlg_set->dlg_list, dlg); dlg->dlg_set = dlg_set; } else { /* This is the first dialog in the dialog set. * Create the dialog set and add this dialog to it. */ dlg_set = alloc_dlgset_node(); pj_list_init(&dlg_set->dlg_list); pj_list_push_back(&dlg_set->dlg_list, dlg); dlg->dlg_set = dlg_set; /* Register the dialog set in the hash table. */ pj_hash_set_np(mod_ua.dlg_table, dlg->local.info->tag.ptr, dlg->local.info->tag.slen, dlg->local.tag_hval, dlg_set->ht_entry, dlg_set); } } else { /* For UAS, create the dialog set with a single dialog as member. */ struct dlg_set *dlg_set; dlg_set = alloc_dlgset_node(); pj_list_init(&dlg_set->dlg_list); pj_list_push_back(&dlg_set->dlg_list, dlg); dlg->dlg_set = dlg_set; pj_hash_set_np(mod_ua.dlg_table, dlg->local.info->tag.ptr, dlg->local.info->tag.slen, dlg->local.tag_hval, dlg_set->ht_entry, dlg_set); } /* Unlock user agent. */ pj_mutex_unlock(mod_ua.mutex); /* Done. */ return PJ_SUCCESS;}PJ_DEF(pj_status_t) pjsip_ua_unregister_dlg( pjsip_user_agent *ua, pjsip_dialog *dlg ){ struct dlg_set *dlg_set; pjsip_dialog *d; /* Sanity-check arguments. */ PJ_ASSERT_RETURN(ua && dlg, PJ_EINVAL); /* Check that dialog has been registered. */ PJ_ASSERT_RETURN(dlg->dlg_set, PJ_EINVALIDOP); /* Lock user agent. */ pj_mutex_lock(mod_ua.mutex); /* Find this dialog from the dialog set. */ dlg_set = dlg->dlg_set; d = dlg_set->dlg_list.next; while (d != (pjsip_dialog*)&dlg_set->dlg_list && d != dlg) { d = d->next; } if (d != dlg) { pj_assert(!"Dialog is not registered!"); pj_mutex_unlock(mod_ua.mutex); return PJ_EINVALIDOP; } /* Remove this dialog from the list. */ pj_list_erase(dlg); /* If dialog list is empty, remove the dialog set from the hash table. */ if (pj_list_empty(&dlg_set->dlg_list)) { pj_hash_set(NULL, mod_ua.dlg_table, dlg->local.info->tag.ptr, dlg->local.info->tag.slen, dlg->local.tag_hval, NULL); /* Return dlg_set to free nodes. */ pj_list_push_back(&mod_ua.free_dlgset_nodes, dlg_set); } /* Unlock user agent. */ pj_mutex_unlock(mod_ua.mutex); /* Done. */ return PJ_SUCCESS;}PJ_DEF(pjsip_dialog*) pjsip_rdata_get_dlg( pjsip_rx_data *rdata ){ return rdata->endpt_info.mod_data[mod_ua.mod.id];}PJ_DEF(pjsip_dialog*) pjsip_tsx_get_dlg( pjsip_transaction *tsx ){ return tsx->mod_data[mod_ua.mod.id];}/* * Find a dialog. */PJ_DEF(pjsip_dialog*) pjsip_ua_find_dialog(const pj_str_t *call_id, const pj_str_t *local_tag, const pj_str_t *remote_tag, pj_bool_t lock_dialog){ struct dlg_set *dlg_set; pjsip_dialog *dlg; PJ_ASSERT_RETURN(call_id && local_tag && remote_tag, NULL); /* Lock user agent. */ pj_mutex_lock(mod_ua.mutex); /* Lookup the dialog set. */ dlg_set = pj_hash_get(mod_ua.dlg_table, local_tag->ptr, local_tag->slen, NULL); if (dlg_set == NULL) { /* Not found */ pj_mutex_unlock(mod_ua.mutex); return NULL; } /* Dialog set is found, now find the matching dialog based on the * remote tag. */ dlg = dlg_set->dlg_list.next; while (dlg != (pjsip_dialog*)&dlg_set->dlg_list) { if (pj_strcmp(&dlg->remote.info->tag, remote_tag) == 0) break; dlg = dlg->next; } if (dlg == (pjsip_dialog*)&dlg_set->dlg_list) { /* Not found */ pj_mutex_unlock(mod_ua.mutex); return NULL; } /* Dialog has been found. It SHOULD have the right Call-ID!! */ PJ_ASSERT_ON_FAIL(pj_strcmp(&dlg->call_id->id, call_id)==0, {pj_mutex_unlock(mod_ua.mutex); return NULL;}); if (lock_dialog) { if (pjsip_dlg_try_inc_lock(dlg) != PJ_SUCCESS) { /* * Unable to acquire dialog's lock while holding the user * agent's mutex. Release the UA mutex before retrying once * more. * * THIS MAY CAUSE RACE CONDITION! */ /* Unlock user agent. */ pj_mutex_unlock(mod_ua.mutex); /* Lock dialog */ pjsip_dlg_inc_lock(dlg); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -