From 8ba68ed14f8e643993475c927baede3152ed774a Mon Sep 17 00:00:00 2001 From: Brian West Date: Fri, 30 Jan 2009 16:46:37 +0000 Subject: [PATCH] MODENDP-179 - Support for SLA, works with Polycom and Snom(Sylantro mode). Thank you Matthew Kaufman. Might still need more work. git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@11562 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- conf/sip_profiles/internal.xml | 2 +- src/mod/endpoints/mod_sofia/Makefile.am | 2 +- src/mod/endpoints/mod_sofia/mod_sofia.h | 14 +- src/mod/endpoints/mod_sofia/sofia.c | 125 +++++++++- src/mod/endpoints/mod_sofia/sofia_glue.c | 29 +++ src/mod/endpoints/mod_sofia/sofia_presence.c | 28 +++ src/mod/endpoints/mod_sofia/sofia_reg.c | 4 + src/mod/endpoints/mod_sofia/sofia_sla.c | 241 +++++++++++++++++++ 8 files changed, 437 insertions(+), 8 deletions(-) create mode 100644 src/mod/endpoints/mod_sofia/sofia_sla.c diff --git a/conf/sip_profiles/internal.xml b/conf/sip_profiles/internal.xml index f8377abf2f..dc999ec403 100644 --- a/conf/sip_profiles/internal.xml +++ b/conf/sip_profiles/internal.xml @@ -62,7 +62,7 @@ - + diff --git a/src/mod/endpoints/mod_sofia/Makefile.am b/src/mod/endpoints/mod_sofia/Makefile.am index a9ed1aad22..7e41928cd0 100644 --- a/src/mod/endpoints/mod_sofia/Makefile.am +++ b/src/mod/endpoints/mod_sofia/Makefile.am @@ -17,7 +17,7 @@ SOFIAUA_DIR=$(SOFIA_DIR)/libsofia-sip-ua SOFIALA=$(SOFIAUA_DIR)/libsofia-sip-ua.la mod_LTLIBRARIES = mod_sofia.la -mod_sofia_la_SOURCES = mod_sofia.c sofia.c sofia_glue.c sofia_presence.c sofia_reg.c mod_sofia.h +mod_sofia_la_SOURCES = mod_sofia.c sofia.c sofia_glue.c sofia_presence.c sofia_reg.c sofia_sla.c mod_sofia.h mod_sofia_la_CFLAGS = $(AM_CFLAGS) mod_sofia_la_CFLAGS += -I. -I$(SOFIAUA_DIR)/bnf -I$(SOFIAUA_DIR)/features mod_sofia_la_CFLAGS += -I$(SOFIAUA_DIR)/http -I$(SOFIAUA_DIR)/ipt diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.h b/src/mod/endpoints/mod_sofia/mod_sofia.h index d9b138eb6f..98a6fc34ce 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.h +++ b/src/mod/endpoints/mod_sofia/mod_sofia.h @@ -375,6 +375,7 @@ struct sofia_profile { char *tls_bindurl; char *tcp_contact; char *tls_contact; + char *sla_contact; char *sipdomain; char *timer_name; char *hold_music; @@ -439,6 +440,7 @@ struct sofia_profile { sofia_media_options_t media_options; uint32_t force_subscription_expires; switch_rtp_bug_flag_t auto_rtp_bugs; + char manage_shared_appearance; /* pflags was all full up - MTK */ }; struct private_object { @@ -474,7 +476,6 @@ struct private_object { char *from_address; char *to_address; char *callid; - char *far_end_contact; char *contact_url; char *from_str; char *rpid; @@ -771,3 +772,14 @@ const char * sofia_state_string(int state); switch_status_t sofia_glue_tech_set_codec(private_object_t *tech_pvt, int force); void sofia_wait_for_reply(struct private_object *tech_pvt, nua_event_t event, uint32_t timeout); void sofia_glue_set_image_sdp(private_object_t *tech_pvt, switch_t38_options_t *t38_options); + +/* + * SLA (shared line appearance) entrypoints + */ + +void sofia_sla_handle_register(nua_t *nua, sofia_profile_t *profile, sip_t const *sip); +void sofia_sla_handle_sip_i_publish(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sip_t const *sip, tagi_t tags[]); +void sofia_sla_handle_sip_i_subscribe(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sip_t const *sip, tagi_t tags[]); +void sofia_sla_handle_sip_r_subscribe(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sip_t const *sip, tagi_t tags[]); +void sofia_sla_handle_sip_i_notify(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sip_t const *sip, tagi_t tags[]); + diff --git a/src/mod/endpoints/mod_sofia/sofia.c b/src/mod/endpoints/mod_sofia/sofia.c index 07bff10657..0bba963304 100644 --- a/src/mod/endpoints/mod_sofia/sofia.c +++ b/src/mod/endpoints/mod_sofia/sofia.c @@ -94,8 +94,33 @@ void sofia_handle_sip_i_notify(switch_core_session_t *session, int status, goto error; } + /* the following could be refactored back to the calling event handler here in sofia.c XXX MTK */ + /* potentially interesting note: for Linksys shared appearance, we'll probably have to set up to get bare notifies + * and pass them inward to the sla handler. we'll have to set NUTAG_APPL_METHOD("NOTIFY") when creating + * nua, and also pick them off special elsewhere here in sofia.c - MTK + * *and* for Linksys, I believe they use "sa" as their magic appearance agent name for those blind notifies, so + * we'll probably have to change to match + */ + if (profile->manage_shared_appearance) { + + if (!strncmp(sip->sip_request->rq_url->url_user, "sla-agent", sizeof("sla-agent"))) { + int sub_state; + tl_gets(tags, NUTAG_SUBSTATE_REF(sub_state), TAG_END()); + + sofia_sla_handle_sip_i_notify(nua, profile, nh, sip, tags); + + if (sub_state == nua_substate_terminated) { + nua_handle_bind(nh, NULL); + nua_handle_destroy(nh); + } + + return; + } + } + /* Automatically return a 200 OK for Event: keep-alive */ if (!strcasecmp(sip->sip_event->o_type, "keep-alive")) { + /* XXX MTK - is this right? in this case isn't sofia is already sending a 200 itself also? */ nua_respond(nh, SIP_200_OK, NUTAG_WITH_THIS(nua), TAG_END()); return; } @@ -305,6 +330,7 @@ void sofia_event_callback(nua_event_t event, switch_channel_t *channel = NULL; sofia_gateway_t *gateway = NULL; int locked = 0; + int check_destroy = 1; if (sofia_private && sofia_private != &mod_sofia_globals.destroy_private && sofia_private != &mod_sofia_globals.keep_private) { if ((gateway = sofia_private->gateway)) { @@ -476,8 +502,23 @@ void sofia_event_callback(nua_event_t event, switch (event) { case nua_i_subscribe: + case nua_r_notify: + check_destroy = 0; + break; + + case nua_i_notify: + + if (sip->sip_event && !strcmp(sip->sip_event->o_type, "dialog") && sip->sip_event->o_params && !strcmp(sip->sip_event->o_params[0], "sla")) { + check_destroy = 0; + } + break; default: + break; + } + + + if (check_destroy) { if (nh && ((sofia_private && sofia_private->destroy_nh) || !nua_handle_magic(nh))) { if (sofia_private) { nua_handle_bind(nh, NULL); @@ -485,9 +526,8 @@ void sofia_event_callback(nua_event_t event, nua_handle_destroy(nh); nh = NULL; } - break; } - + if (sofia_private && sofia_private->destroy_me) { if (nh) { nua_handle_bind(nh, NULL); @@ -719,9 +759,9 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void TAG_IF(profile->pres_type, NUTAG_ALLOW("SUBSCRIBE")), TAG_IF(profile->pres_type, NUTAG_ENABLEMESSAGE(1)), TAG_IF(profile->pres_type, NUTAG_ALLOW_EVENTS("presence")), - TAG_IF(profile->pres_type, NUTAG_ALLOW_EVENTS("dialog")), + TAG_IF((profile->pres_type || profile->manage_shared_appearance), NUTAG_ALLOW_EVENTS("dialog")), TAG_IF(profile->pres_type, NUTAG_ALLOW_EVENTS("call-info")), - TAG_IF(profile->pres_type, NUTAG_ALLOW_EVENTS("sla")), + TAG_IF((profile->pres_type || profile->manage_shared_appearance), NUTAG_ALLOW_EVENTS("sla")), TAG_IF(profile->pres_type, NUTAG_ALLOW_EVENTS("include-session-description")), TAG_IF(profile->pres_type, NUTAG_ALLOW_EVENTS("presence.winfo")), TAG_IF(profile->pres_type, NUTAG_ALLOW_EVENTS("message-summary")), @@ -738,7 +778,6 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void nua_set_params(node->nua, NUTAG_APPL_METHOD("OPTIONS"), - NUTAG_EARLY_MEDIA(1), NUTAG_AUTOANSWER(0), NUTAG_AUTOALERT(0), TAG_IF((profile->mflags & MFLAG_REGISTER), NUTAG_ALLOW("REGISTER")), @@ -1954,6 +1993,11 @@ switch_status_t config_sofia(int reload, char *profile_name) } else if (switch_true(val)) { profile->pres_type = PRES_TYPE_FULL; } + } else if (!strcasecmp(var, "manage-shared-appearance")) { + if (switch_true(val)) { + profile->manage_shared_appearance = 1; + profile->sla_contact = switch_core_sprintf(profile->pool, "sip:sla-agent@%s", profile->sipip); + } } else if (!strcasecmp(var, "unregister-on-options-fail")) { if (switch_true(val)) { profile->pflags |= PFLAG_UNREG_OPTIONS_FAIL; @@ -4282,7 +4326,78 @@ void sofia_handle_sip_i_invite(nua_t *nua, sofia_profile_t *profile, nua_handle_ } } + if (sip->sip_replaces) { + nua_handle_t *bnh; + if ((bnh = nua_handle_by_replaces(nua, sip->sip_replaces))) { + sofia_private_t *b_private = NULL; + if ((b_private = nua_handle_magic(bnh))) { + switch_core_session_t *b_session = NULL; + if ((b_session = switch_core_session_locate(b_private->uuid))) { + switch_channel_t *b_channel = switch_core_session_get_channel(b_session); + const char *uuid; + int one_leg = 1; + private_object_t *b_tech_pvt = NULL; + const char *app = switch_channel_get_variable(b_channel, SWITCH_CURRENT_APPLICATION_VARIABLE); + const char *data = switch_channel_get_variable(b_channel, SWITCH_CURRENT_APPLICATION_DATA_VARIABLE); + + if (app && data && !strcasecmp(app, "conference")) { + destination_number = switch_core_session_sprintf(b_session, "answer,conference:%s", data); + dialplan = "inline"; + } else { + if (switch_core_session_check_interface(b_session, sofia_endpoint_interface)) { + b_tech_pvt = switch_core_session_get_private(b_session); + } + if ((uuid = switch_channel_get_variable(b_channel, SWITCH_SIGNAL_BOND_VARIABLE))) { + one_leg = 0; + } else { + uuid = switch_core_session_get_uuid(b_session); + } + + if (uuid) { + switch_core_session_t *c_session = NULL; + int do_conf = 0; + + uuid = switch_core_session_strdup(b_session, uuid); + + if ((c_session = switch_core_session_locate(uuid))) { + switch_channel_t *c_channel = switch_core_session_get_channel(c_session); + private_object_t *c_tech_pvt = NULL; + + if (switch_core_session_check_interface(c_session, sofia_endpoint_interface)) { + c_tech_pvt = switch_core_session_get_private(c_session); + } + + + if (!one_leg && + (!b_tech_pvt || !switch_test_flag(b_tech_pvt, TFLAG_SIP_HOLD)) && + (!c_tech_pvt || !switch_test_flag(c_tech_pvt, TFLAG_SIP_HOLD))) { + char *ext = switch_core_session_sprintf(b_session, "conference:%s@sla+flags{mintwo}", uuid); + + switch_channel_set_flag(c_channel, CF_REDIRECT); + switch_ivr_session_transfer(b_session, ext, "inline", NULL); + switch_ivr_session_transfer(c_session, ext, "inline", NULL); + switch_channel_clear_flag(c_channel, CF_REDIRECT); + do_conf = 1; + } + switch_core_session_rwunlock(c_session); + } + + if (do_conf) { + destination_number = switch_core_session_sprintf(b_session, "answer,conference:%s@sla+flags{mintwo}", uuid); + } else { + destination_number = switch_core_session_sprintf(b_session, "answer,intercept:%s", uuid); + } + + dialplan = "inline"; + } + } + switch_core_session_rwunlock(b_session); + } + } + nua_handle_unref(bnh); + } + } check_decode(displayname, session); tech_pvt->caller_profile = switch_caller_profile_new(switch_core_session_get_pool(session), diff --git a/src/mod/endpoints/mod_sofia/sofia_glue.c b/src/mod/endpoints/mod_sofia/sofia_glue.c index ba2f33aa94..ab2817fb87 100644 --- a/src/mod/endpoints/mod_sofia/sofia_glue.c +++ b/src/mod/endpoints/mod_sofia/sofia_glue.c @@ -3040,6 +3040,16 @@ int sofia_glue_init_sql(sofia_profile_t *profile) " hostname VARCHAR(255)\n" ");\n"; + /* should we move this glue to sofia_sla or keep it here where all db init happens? XXX MTK */ + char shared_appearance_sql[] = + "CREATE TABLE sip_shared_appearance_subscriptions (\n" + " subscriber VARCHAR(255),\n" + " call_id VARCHAR(255),\n" + " aor VARCHAR(255),\n" + " profile_name VARCHAR(255),\n" + " hostname VARCHAR(255)\n" + ");\n"; + if (profile->odbc_dsn) { #ifdef SWITCH_HAVE_ODBC if (!(profile->master_odbc = switch_odbc_handle_new(profile->odbc_dsn, profile->odbc_user, profile->odbc_pass))) { @@ -3094,6 +3104,15 @@ int sofia_glue_init_sql(sofia_profile_t *profile) } free(test_sql); + if (profile->manage_shared_appearance) { + test_sql = switch_mprintf("delete from sip_shared_appearance_subscriptions where hostname='%q'", mod_sofia_globals.hostname); + if (switch_odbc_handle_exec(profile->master_odbc, test_sql, NULL) != SWITCH_ODBC_SUCCESS) { + switch_odbc_handle_exec(profile->master_odbc, "DROP TABLE sip_shared_appearance_subscriptions", NULL); + switch_odbc_handle_exec(profile->master_odbc, shared_appearance_sql, NULL); + } + free(test_sql); + } + #else switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC IS NOT AVAILABLE!\n"); #endif @@ -3125,6 +3144,16 @@ int sofia_glue_init_sql(sofia_profile_t *profile) switch_core_db_test_reactive(profile->master_db, test_sql, "DROP TABLE sip_authentication", auth_sql); free(test_sql); + if(profile->manage_shared_appearance) { + test_sql = switch_mprintf("delete from sip_shared_appearance_subscriptions where hostname='%q'", mod_sofia_globals.hostname); + switch_core_db_test_reactive(profile->master_db, test_sql, "DROP TABLE sip_shared_appearance_subscriptions", shared_appearance_sql); + free(test_sql); + + switch_core_db_exec(profile->master_db, "create index if not exists ssa_hostname on sip_shared_appearance_subscriptions (hostname)", NULL, NULL, NULL); + /* XXX MTK create additional index for shared_appearance if necessary */ + } + + switch_core_db_exec(profile->master_db, "create index if not exists sr_call_id on sip_registrations (call_id)", NULL, NULL, NULL); switch_core_db_exec(profile->master_db, "create index if not exists sr_sip_user on sip_registrations (sip_user)", NULL, NULL, NULL); switch_core_db_exec(profile->master_db, "create index if not exists sr_sip_host on sip_registrations (sip_host)", NULL, NULL, NULL); diff --git a/src/mod/endpoints/mod_sofia/sofia_presence.c b/src/mod/endpoints/mod_sofia/sofia_presence.c index 7a424f6885..5afe2670c2 100644 --- a/src/mod/endpoints/mod_sofia/sofia_presence.c +++ b/src/mod/endpoints/mod_sofia/sofia_presence.c @@ -1423,6 +1423,17 @@ void sofia_presence_handle_sip_i_subscribe(int status, return; } + /* the following could be refactored back to the calling event handler in sofia.c XXX MTK */ + if (profile->manage_shared_appearance) { + if (!strncmp(sip->sip_request->rq_url->url_user, "sla-agent", sizeof("sla-agent"))) { + /* only fire this on <200 to try to avoid resubscribes. probably better ways to do this? */ + if (status < 200) { + sofia_sla_handle_sip_i_subscribe(nua, profile, nh, sip, tags); + } + return; + } + } + get_addr(network_ip, sizeof(network_ip), my_addrinfo->ai_addr, my_addrinfo->ai_addrlen); network_port = ntohs(((struct sockaddr_in *) msg_addrinfo(nua_current_request(nua))->ai_addr)->sin_port); @@ -1767,6 +1778,14 @@ void sofia_presence_handle_sip_r_subscribe(int status, return; } + /* the following could possibly be refactored back towards the calling event handler in sofia.c XXX MTK */ + if (profile->manage_shared_appearance) { + if (!strcasecmp(o->o_type, "dialog") && msg_params_find(o->o_params, "sla")) { + sofia_sla_handle_sip_r_subscribe(nua, profile, nh, sip, tags); + return; + } + } + if (!sofia_private || !sofia_private->gateway) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Gateway information missing\n"); return; @@ -1810,6 +1829,15 @@ void sofia_presence_handle_sip_i_publish(nua_t *nua, sofia_profile_t *profile, n sip_payload_t *payload = sip->sip_payload; char *event_type; + /* the following could instead be refactored back to the calling event handler in sofia.c XXX MTK */ + if (profile->manage_shared_appearance) { + /* also it probably is unsafe to dereference so many things in a row without testing XXX MTK */ + if (!strncmp(sip->sip_request->rq_url->url_user, "sla-agent", sizeof("sla-agent"))) { + sofia_sla_handle_sip_i_publish(nua, profile, nh, sip, tags); + return; + } + } + if (from) { from_user = (char *) from->a_url->url_user; from_host = (char *) from->a_url->url_host; diff --git a/src/mod/endpoints/mod_sofia/sofia_reg.c b/src/mod/endpoints/mod_sofia/sofia_reg.c index 557bb867a2..df4b5ec4c3 100644 --- a/src/mod/endpoints/mod_sofia/sofia_reg.c +++ b/src/mod/endpoints/mod_sofia/sofia_reg.c @@ -1102,6 +1102,10 @@ uint8_t sofia_reg_handle_register(nua_t *nua, sofia_profile_t *profile, nua_hand switch_event_fire(&s_event); } + if (profile->manage_shared_appearance) { + sofia_sla_handle_register(nua, profile, sip); + } + return 1; } diff --git a/src/mod/endpoints/mod_sofia/sofia_sla.c b/src/mod/endpoints/mod_sofia/sofia_sla.c new file mode 100644 index 0000000000..04128b6ce3 --- /dev/null +++ b/src/mod/endpoints/mod_sofia/sofia_sla.c @@ -0,0 +1,241 @@ +/* + * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2005/2006, Anthony Minessale II + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Anthony Minessale II + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Anthony Minessale II + * Ken Rice, Asteria Solutions Group, Inc + * Paul D. Tinsley + * Bret McDanel + * + * + * sofia_sla.c -- SOFIA SIP Endpoint (support for shared line appearance) + * This file (and calls into it) developed by Matthew T Kaufman + * + */ +#include "mod_sofia.h" + + +static int sofia_sla_sub_callback(void *pArg, int argc, char **argv, char **columnNames); + + +void sofia_sla_handle_register(nua_t *nua, sofia_profile_t *profile, sip_t const *sip) +{ + nua_handle_t *nh; + + /* TODO: + * check to see if it says in the group or extension xml that we are handling SLA for this AOR + * check to see if we're already subscribed and the call-id in the subscribe matches. if so, + * we can skip this, which would keep us from re-subscribing which would also keep us from + * leaking so horribly much memory like we do now + */ + + nh = nua_handle(nua, NULL, NUTAG_URL(sip->sip_contact->m_url), TAG_NULL()); + + /* we make up and bind a sofia_private so that the existing event handler destruction code won't be confused by us */ + /* (though it isn't clear that this is sufficient... we still have break cases for nua_i_notify and nua_r_notify + * in sofia_event_callback's destruction end because if we don't, the handle gets destroyed. or maybe it is + * something else i'm doing wrong? MTK + + mod_sofia_globals.keep_private is a magic static private things can share for this purpose: ACM + */ + + nua_handle_bind(nh, &mod_sofia_globals.keep_private); + + + nua_subscribe(nh, + SIPTAG_TO(sip->sip_to), + SIPTAG_FROM(sip->sip_to), // ? + SIPTAG_CONTACT_STR(profile->sla_contact), + SIPTAG_EXPIRES_STR("3500"), /* ok, this is totally fake here XXX MTK */ + SIPTAG_EVENT_STR("dialog;sla"), /* some phones want ;include-session-description too? */ + TAG_NULL()); +} + +void sofia_sla_handle_sip_i_publish(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sip_t const *sip, tagi_t tags[]) +{ + /* at present there's no SLA versions that we deal with that do publish. to be safe, we say "OK" */ + nua_respond(nh, SIP_200_OK, NUTAG_WITH_THIS(nua), TAG_END()); +} + +void sofia_sla_handle_sip_i_subscribe(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sip_t const *sip, tagi_t tags[]) +{ + char *aor = NULL; + char *subscriber = NULL; + char *sql = NULL; + + /* + * XXX MTK FIXME - we don't look at the tag to see if NUTAG_SUBSTATE(nua_substate_terminated) or + * a Subscription-State header with state "terminated" and/or expiration of 0. So we never forget + * about them here. + * likewise, we also don't have a hook against nua_r_notify events, so we can't see nua_substate_terminated there. + */ + + /* + * extracting AOR is weird... + * the From is the main extension, not the third-party one... + * and the contact has the phone's own network address, not the AOR address + * so we do what openser's pua_bla does and... + */ + + aor = switch_mprintf("sip:%s@%s",sip->sip_contact->m_url->url_user, sip->sip_from->a_url->url_host); + + /* + * ok, and now that we HAVE the AOR, we REALLY should go check in the XML config and see if this particular + * extension is set up to have shared appearances managed. right now it is all-or-nothing on the profile, + * which won't be sufficient for real life. FIXME XXX MTK + */ + + /* then the subscriber is the user at their network location... this is arguably the wrong way, but works so far... */ + + subscriber = switch_mprintf("sip:%s@%s",sip->sip_from->a_url->url_user, sip->sip_contact->m_url->url_host); + + + if ((sql = + switch_mprintf("delete from sip_shared_appearance_subscriptions where subscriber='%q' and profile name='%q' and hostname='%q'", + subscriber, profile->name, mod_sofia_globals.hostname + ))) { + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); + } + + if ((sql = + switch_mprintf("insert into sip_shared_appearance_subscriptions (subscriber, call_id, aor, profile_name, hostname) " + "values ('%q','%q','%q','%q','%q')", + subscriber, sip->sip_call_id->i_id, aor, profile->name, mod_sofia_globals.hostname))) { + sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE); + } + + + + nua_respond(nh, SIP_202_ACCEPTED, SIPTAG_CONTACT_STR(profile->sla_contact), NUTAG_WITH_THIS(nua), + SIPTAG_SUBSCRIPTION_STATE_STR("active;expires=300"), /* you thought the OTHER time was fake... need delta here FIXME XXX MTK */ + SIPTAG_EXPIRES_STR("300"), /* likewise, totally fake - FIXME XXX MTK */ + /* sofia_presence says something about needing TAG_IF(sticky, NUTAG_PROXY(sticky)) for NAT stuff? */ + TAG_END()); + + switch_safe_free(aor); + switch_safe_free(subscriber); + switch_safe_free(sql); +} + +struct sla_notify_helper { + sofia_profile_t *profile; + char *payload; +}; + +void sofia_sla_handle_sip_r_subscribe(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sip_t const *sip, tagi_t tags[]) +{ + /* apparently, we do nothing */ +} + +void sofia_sla_handle_sip_i_notify(nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sip_t const *sip, tagi_t tags[]) +{ + char *sql = NULL; + struct sla_notify_helper helper; + char *aor = NULL; + char *contact = NULL; + + /* + * things we know we don't do: + * draft-anil-sipping-bla says we should look and see if the specific appearance is in use and if it is + * return an error for the i_notify, to handle the initial line-seize for dialing out case. + * to do that we would need to really track all the appearances *and* override sofia's autoresponder for i_notify + * because at this point, it already sent the 200 for us. + * and we simply don't track all the appearance status by decoding the XML payload out and recording that in + * an SQL line appearance database yet. we'll need to do that in order to do the above, and in order to make + * interoperation possible between devices that disagree on the dialog xml payload OR don't even do it that + * way and instead use things like call-info/line-seize events like the old Broadsoft spec. + * instead we cheat and just reflect the entire payload back to the subscribers (who, because we don't + * yet check each AOR as it comes in to see if it is to be managed, is more subscribers than we probably + * should have). for the current prototype stage, this works ok anyway. + * and because we don't parse the XML, we even reflect it right back to the notifier/sender (which is called + * "target" in the payload XML, of course). + * also because we don't track on a per-appearance basis, there IS NOT a hook back from sofia_glue to add + * an appearance index to the outbound invite for the "next free appearance". this can lead to race + * conditions where a call shows up on slightly different line key numbers at different phones, making + * "pick up on line X" meaningless if such a race occurs. again, it is a prototype. we can fix it later. + */ + + + /* the dispatcher calls us just because it is aimed at us, so check to see if it is dialog;sla at the very least... */ + + if ( (!sip->sip_event) + || (strcasecmp(sip->sip_event->o_type, "dialog")) + || !msg_params_find(sip->sip_event->o_params, "sla") ) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,"sent to sla-agent but not dialog;sla\n"); + return; + } + + /* calculate the AOR we're trying to tell people about. should probably double-check before derferencing XXX MTK */ + aor = switch_mprintf("sip:%s@%s",sip->sip_to->a_url->url_user, sip->sip_to->a_url->url_host); + + /* this isn't sufficient because on things like the polycom, the subscriber is the 'main' ext number, but the + * 'main' ext number isn't in ANY of the headers they send us in the notify. of course. + * as a side effect, the subscriber<>'%q' below isn't sufficient to prevent reflecting the event back + * at a phone that has the ext # != third-party#. see above, can only fix by parsing the XML for the 'target' + * so we don't reflect it back at anyone who is the "boss" config, but we do reflect it back at the "secretary" + * config. if that breaks the phone, just set them all up as the "boss" config where ext#==third-party# + */ + contact = switch_mprintf("sip:%s@%s",sip->sip_contact->m_url->url_user, sip->sip_contact->m_url->url_host); + + if(sip->sip_payload && sip->sip_payload->pl_data) { + sql = switch_mprintf("select subscriber,call_id,aor,profile_name,hostname from sip_shared_appearance_subscriptions where " + "aor='%q' and subscriber<>'%q' and profile_name='%q' and hostname='%q'", + aor, contact, profile->name, mod_sofia_globals.hostname); + + + helper.profile = profile; + helper.payload = sip->sip_payload->pl_data; /* could just send the WHOLE payload. you'd get the type that way. */ + + /* which mutex if any is correct to hold in this callback? XXX MTK FIXME */ + sofia_glue_execute_sql_callback(profile, SWITCH_FALSE, profile->ireg_mutex, sql, sofia_sla_sub_callback, &helper); + + switch_safe_free(sql); + switch_safe_free(aor); + switch_safe_free(contact); + } +} + +static int sofia_sla_sub_callback(void *pArg, int argc, char **argv, char **columnNames) +{ + struct sla_notify_helper *helper = pArg; + /* char *subscriber = argv[0]; */ + char *call_id = argv[1]; + /* char *aor = argv[2]; */ + /* char *profile_name = argv[3]; */ + /* char *hostname = argv[4]; */ + nua_handle_t *nh; + + nh = nua_handle_by_call_id(helper->profile->nua, call_id); /* that's all you need to find the subscription's nh */ + + if(nh) + { + nua_notify(nh, + SIPTAG_SUBSCRIPTION_STATE_STR("active;expires=300"), /* XXX MTK FIXME - this is totally fake calculation */ + SIPTAG_CONTENT_TYPE_STR("application/dialog-info+xml"), /* could've just kept the type from the payload */ + SIPTAG_PAYLOAD_STR(helper->payload), + TAG_END()); + } + return 0; +} +