📄 nua_stack.c
字号:
/* * This file is part of the Sofia-SIP package * * Copyright (C) 2005 Nokia Corporation. * * Contact: Pekka Pessi <pekka.pessi@nokia.com> * * 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., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * *//**@CFILE nua_stack.c * @brief Sofia-SIP User Agent Engine implementation * * @author Pekka Pessi <Pekka.Pessi@nokia.com> * @author Kai Vehmanen <Kai.Vehmanen@nokia.com> * @author Martti Mela <Martti Mela@nokia.com> * @author Remeres Jacobs <Remeres.Jacobs@nokia.com> * @author Tat Chan <Tat.Chan@nokia.com> * * @date Created: Wed Feb 14 18:32:58 2001 ppessi */#include "config.h"#include <sofia-sip/su_tag_class.h>#include <sofia-sip/su_tag_class.h>#include <sofia-sip/su_tagarg.h>#include <sofia-sip/su_strlst.h>#include <sofia-sip/su_uniqueid.h>#include <sofia-sip/su_tag_io.h>#define SU_ROOT_MAGIC_T struct nua_s#define SU_MSG_ARG_T struct event_s#define NUA_SAVED_EVENT_T su_msg_t *#define NTA_AGENT_MAGIC_T struct nua_s#define NTA_LEG_MAGIC_T struct nua_handle_s#define NTA_OUTGOING_MAGIC_T struct nua_handle_s#define NTA_INCOMING_MAGIC_T struct nua_handle_s#include <sofia-sip/sip.h>#include <sofia-sip/sip_header.h>#include <sofia-sip/sip_status.h>#include <sofia-sip/sip_util.h>#include <sofia-sip/tport_tag.h>#include <sofia-sip/nta.h>#include <sofia-sip/nta_tport.h>#include <sofia-sip/auth_client.h>#include <sofia-sip/soa.h>#include "sofia-sip/nua.h"#include "sofia-sip/nua_tag.h"#include "nua_stack.h"#include <stddef.h>#include <stdlib.h>#include <string.h>#include <limits.h>#include <stdio.h>#include <assert.h>/* ======================================================================== * * Protocol stack side * * ======================================================================== */nua_handle_t *nh_create(nua_t *nua, tag_type_t t, tag_value_t v, ...);static void nh_append(nua_t *nua, nua_handle_t *nh);static void nh_remove(nua_t *nua, nua_handle_t *nh);static int nh_authorize(nua_handle_t *nh, tag_type_t tag, tag_value_t value, ...);static int nh_challenge(nua_handle_t *nh, sip_t const *sip);static void nua_stack_timer(nua_t *nua, su_timer_t *t, su_timer_arg_t *a);/* ---------------------------------------------------------------------- *//* Constant data *//**@internal Default internal error. */char const nua_internal_error[] = "Internal NUA Error";char const nua_application_sdp[] = "application/sdp";#define NUA_STACK_TIMER_INTERVAL (1000)/* ---------------------------------------------------------------------- * Initialization & deinitialization */int nua_stack_init(su_root_t *root, nua_t *nua){ su_home_t *home; nua_handle_t *dnh; int media_enable = 1; soa_session_t *soa = NULL; char const *soa_name = NULL; static int initialized_logs = 0; enter; if (!initialized_logs) { extern su_log_t tport_log[]; extern su_log_t nta_log[]; extern su_log_t nea_log[]; extern su_log_t iptsec_log[]; su_log_init(tport_log); su_log_init(nta_log); su_log_init(nea_log); su_log_init(iptsec_log); initialized_logs = 1; } nua->nua_root = root; home = nua->nua_home; nua->nua_handles_tail = &nua->nua_handles; sip_from_init(nua->nua_from); dnh = su_home_clone(nua->nua_home, sizeof (*dnh) + sizeof(*dnh->nh_prefs)); if (!dnh) return -1; dnh->nh_prefs = (void *)(dnh + 1); dnh->nh_valid = nua_handle; dnh->nh_nua = nua; nua_handle_ref(dnh); dnh->nh_ref_by_stack = 1; nua_handle_ref(dnh); dnh->nh_ref_by_user = 1; nh_append(nua, dnh); dnh->nh_ds->ds_local = nua->nua_from; dnh->nh_ds->ds_remote = nua->nua_from; if (nua_stack_set_defaults(dnh, dnh->nh_prefs) < 0) return -1; if (nua_stack_init_instance(dnh, nua->nua_args) < 0) return -1; nua->nua_invite_accept = sip_accept_make(home, SDP_MIME_TYPE); nua->nua_nta = nta_agent_create(root, NONE, NULL, NULL, NTATAG_MERGE_482(1), NTATAG_CLIENT_RPORT(1), NTATAG_UA(1),#if HAVE_SOFIA_SMIME NTATAG_SMIME(nua->sm),#endif TPTAG_STUN_SERVER(1), TAG_NEXT(nua->nua_args)); dnh->nh_ds->ds_leg = nta_leg_tcreate(nua->nua_nta, nua_stack_process_request, dnh, NTATAG_NO_DIALOG(1), TAG_END()); if (nua->nua_nta == NULL || dnh->nh_ds->ds_leg == NULL || nta_agent_set_params(nua->nua_nta, NTATAG_UA(1), TAG_END()) < 0 || nua_stack_init_transport(nua, nua->nua_args) < 0) { SU_DEBUG_1(("nua: initializing SIP stack failed\n")); } if (nua_stack_set_from(nua, 1, nua->nua_args) < 0) return -1; /* Set initial nta/soa parameters */ if (tl_gets(nua->nua_args, NUTAG_MEDIA_ENABLE_REF(media_enable), NUTAG_SOA_NAME_REF(soa_name), TAG_NULL()) < 0) return -1; nua->nua_media_enable = media_enable; if (media_enable) { if (soa == NULL) soa = soa_create(soa_name, nua->nua_root, nua->nua_dhandle); dnh->nh_soa = soa; soa_set_params(soa, TAG_NEXT(nua->nua_args)); } nua->nua_timer = su_timer_create(su_root_task(root), NUA_STACK_TIMER_INTERVAL); if (!nua->nua_timer) return -1; nua_stack_timer(nua, nua->nua_timer, NULL); nua->nua_args = NULL; return 0;}void nua_stack_deinit(su_root_t *root, nua_t *nua){ enter; su_timer_destroy(nua->nua_timer), nua->nua_timer = NULL; nta_agent_destroy(nua->nua_nta), nua->nua_nta = NULL;}/* ---------------------------------------------------------------------- * Sending events to client application */static void nua_stack_shutdown(nua_t *);void nua_stack_authenticate(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *), nua_stack_respond(nua_t *, nua_handle_t *, int , char const *, tagi_t const *), nua_stack_destroy_handle(nua_t *, nua_handle_t *, tagi_t const *);/* Notifier */void nua_stack_authorize(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *), nua_stack_notifier(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *), nua_stack_terminate(nua_t *, nua_handle_t *, nua_event_t, tagi_t const *);int nh_notifier_shutdown(nua_handle_t *nh, nea_event_t *ev, tag_type_t t, tag_value_t v, ...);/** @internal Send an event to the application. */int nua_stack_event(nua_t *nua, nua_handle_t *nh, msg_t *msg, nua_event_t event, int status, char const *phrase, tag_type_t tag, tag_value_t value, ...){ su_msg_r sumsg = SU_MSG_R_INIT; ta_list ta; int e_len, len, xtra, p_len; enter; if (nua_log->log_level >= 5) { char const *name = nua_event_name(event) + 4; char const *p = phrase ? phrase : ""; if (status == 0) SU_DEBUG_5(("nua(%p): %s %s\n", nh, name, p)); else SU_DEBUG_5(("nua(%p): %s %u %s\n", nh, name, status, p)); } if (event == nua_r_destroy) { if (msg) msg_destroy(msg); if (status >= 200) { nh_destroy(nua, nh); } return event; } if (event > nua_r_method || (nh && !nh->nh_valid) || (nua->nua_shutdown && event != nua_r_shutdown)) { if (msg) msg_destroy(msg); return event; } ta_start(ta, tag, value); e_len = offsetof(event_t, e_tags); len = tl_len(ta_args(ta)); xtra = tl_xtra(ta_args(ta), len); p_len = phrase ? strlen(phrase) + 1 : 1; if (su_msg_create(sumsg, nua->nua_client, nua->nua_server, nua_event, e_len + len + xtra + p_len) == 0) { event_t *e = su_msg_data(sumsg); tagi_t *t = e->e_tags, *t_end = (tagi_t *)((char *)t + len); void *b = t_end, *end = (char *)b + xtra; t = tl_dup(t, ta_args(ta), &b); assert(t == t_end); assert(b == end); e->e_event = event; e->e_nh = nh ? nua_handle_ref(nh) : nua->nua_dhandle; e->e_status = status; e->e_phrase = strcpy(end, phrase ? phrase : ""); e->e_msg = msg; if (su_msg_send(sumsg) != 0) nua_handle_unref(nh); } ta_end(ta); return event;}/* ---------------------------------------------------------------------- * Post signal to stack itself */void nua_stack_post_signal(nua_handle_t *nh, nua_event_t event, tag_type_t tag, tag_value_t value, ...){ ta_list ta; ta_start(ta, tag, value); nua_signal((nh)->nh_nua, nh, NULL, 1, event, 0, NULL, ta_tags(ta)); ta_end(ta);}/* ---------------------------------------------------------------------- * Receiving events from client */void nua_stack_signal(nua_t *nua, su_msg_r msg, nua_event_data_t *e){ nua_handle_t *nh = e->e_nh; tagi_t *tags = e->e_tags; assert(tags); if (nh) { if (!nh->nh_prev) nh_append(nua, nh); if (!nh->nh_ref_by_stack) { nh->nh_ref_by_stack = 1; nua_handle_ref(nh); } } if (nua_log->log_level >= 5) { char const *name = nua_event_name(e->e_event); if (e->e_status == 0) SU_DEBUG_5(("nua(%p): signal %s\n", nh, name + 4)); else SU_DEBUG_5(("nua(%p): signal %s %u %s\n", nh, name + 4, e->e_status, e->e_phrase ? e->e_phrase : "")); } su_msg_save(nua->nua_signal, msg); if (nua->nua_shutdown && !e->e_always) { /* Shutting down */ nua_stack_event(nua, nh, NULL, e->e_event, 901, "Stack is going down", TAG_END()); } else switch (e->e_event) { case nua_r_get_params: nua_stack_get_params(nua, nh ? nh : nua->nua_dhandle, e->e_event, tags); break; case nua_r_set_params: nua_stack_set_params(nua, nh ? nh : nua->nua_dhandle, e->e_event, tags); break; case nua_r_shutdown: nua_stack_shutdown(nua); break; case nua_r_register: case nua_r_unregister: nua_stack_register(nua, nh, e->e_event, tags); break; case nua_r_invite: nua_stack_invite(nua, nh, e->e_event, tags); break; case nua_r_cancel: nua_stack_cancel(nua, nh, e->e_event, tags); break; case nua_r_bye: nua_stack_bye(nua, nh, e->e_event, tags); break; case nua_r_options: nua_stack_options(nua, nh, e->e_event, tags); break; case nua_r_refer: nua_stack_refer(nua, nh, e->e_event, tags); break; case nua_r_publish: case nua_r_unpublish: nua_stack_publish(nua, nh, e->e_event, tags); break; case nua_r_info: nua_stack_info(nua, nh, e->e_event, tags); break; case nua_r_update: nua_stack_update(nua, nh, e->e_event, tags); break; case nua_r_message: nua_stack_message(nua, nh, e->e_event, tags); break; case nua_r_subscribe: case nua_r_unsubscribe: nua_stack_subscribe(nua, nh, e->e_event, tags); break; case nua_r_notify: nua_stack_notify(nua, nh, e->e_event, tags); break; case nua_r_notifier: nua_stack_notifier(nua, nh, e->e_event, tags); break; case nua_r_terminate: nua_stack_terminate(nua, nh, e->e_event, tags); break; case nua_r_method: nua_stack_method(nua, nh, e->e_event, tags); break; case nua_r_authenticate: nua_stack_authenticate(nua, nh, e->e_event, tags); break; case nua_r_authorize: nua_stack_authorize(nua, nh, e->e_event, tags); break; case nua_r_ack: nua_stack_ack(nua, nh, e->e_event, tags); break; case nua_r_respond: nua_stack_respond(nua, nh, e->e_status, e->e_phrase, tags); break; case nua_r_destroy: nua_stack_destroy_handle(nua, nh, tags); break; default: break; } if (su_msg_is_non_null(nua->nua_signal)) su_msg_destroy(nua->nua_signal); if (nh != nua->nua_dhandle) nua_handle_unref(nh);}/* ====================================================================== */static int nh_call_pending(nua_handle_t *nh, sip_time_t time);/**@internal * Timer routine. * * Go through all active handles and execute pending tasks */void nua_stack_timer(nua_t *nua, su_timer_t *t, su_timer_arg_t *a){ nua_handle_t *nh, *nh_next; sip_time_t now = sip_now(); su_timer_set(t, nua_stack_timer, a); if (nua->nua_shutdown) { nua_stack_shutdown(nua); return; } for (nh = nua->nua_handles; nh; nh = nh_next) { nh_next = nh->nh_next; nh_call_pending(nh, now); }}staticint nh_call_pending(nua_handle_t *nh, sip_time_t now){ nua_dialog_usage_t *du; sip_time_t next = now + NUA_STACK_TIMER_INTERVAL / 1000; for (du = nh->nh_ds->ds_usage; du; du = du->du_next) { if (now == 0) break; if (du->du_refresh && du->du_refresh < next) break; } if (du == NULL) return 0; nua_handle_ref(nh); while (du) { nua_dialog_usage_t *du_next = du->du_next; nua_dialog_usage_refresh(nh, du, now); if (du_next == NULL) break; for (du = nh->nh_ds->ds_usage; du; du = du->du_next) if (du == du_next) break; for (; du; du = du->du_next) { if (now == 0) break; if (du->du_refresh && du->du_refresh < next) break; } } nua_handle_unref(nh); return 1;}/* ====================================================================== *//** @internal Shut down stack. */void nua_stack_shutdown(nua_t *nua){ nua_handle_t *nh, *nh_next; int busy = 0; sip_time_t now = sip_now(); int status; char const *phrase; struct nua_session_state *ss; enter; if (!nua->nua_shutdown) nua->nua_shutdown = now; for (nh = nua->nua_handles; nh; nh = nh_next) { nh_next = nh->nh_next; ss = nh->nh_ss; if (ss->ss_srequest->sr_respond) { ss->ss_srequest->sr_respond(nua, nh, SIP_410_GONE, NULL); busy++; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -