Freeswitch-svn
Threads by month
- ----- 2026 -----
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- July
- June
- 12151 discussions
22 Nov '08
Author: mikej
Date: Fri Nov 21 23:37:36 2008
New Revision: 10516
Log:
minor simplification
Modified:
freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c
Modified: freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c
==============================================================================
--- freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c (original)
+++ freeswitch/trunk/src/mod/applications/mod_dptools/mod_dptools.c Fri Nov 21 23:37:36 2008
@@ -1628,7 +1628,7 @@
*mdata++ = '\0';
}
- lang = switch_channel_get_variable(switch_core_session_get_channel(session), "language");
+ lang = switch_channel_get_variable(channel, "language");
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Execute %s(%s) lang %s\n", macro, switch_str_nil(mdata), switch_str_nil(lang));
1
0
22 Nov '08
Author: anthm
Date: Fri Nov 21 21:51:00 2008
New Revision: 10515
Log:
update
Modified:
freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c
Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c (original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c Fri Nov 21 21:51:00 2008
@@ -374,7 +374,7 @@
"sip_subscriptions.accept,sip_subscriptions.profile_name"
",1,'%q','%q',sip_presence.status,sip_presence.rpid "
"from sip_subscriptions left join sip_presence on "
- "(sip_subscriptions.sip_user=sip_presence.sip_user and sip_subscriptions.sip_host=sip_presence.sip_host and "
+ "(sip_subscriptions.sub_to_user=sip_presence.sip_user and sip_subscriptions.sub_to_host=sip_presence.sip_host and "
"sip_subscriptions.profile_name=sip_presence.profile_name) "
"where sip_subscriptions.event='presence' and sip_subscriptions.full_from like '%%%q%%'", status, rpid, from);
} else {
@@ -386,7 +386,7 @@
"sip_subscriptions.accept,sip_subscriptions.profile_name"
",1,'%q','%q',sip_presence.status,sip_presence.rpid "
"from sip_subscriptions left join sip_presence on "
- "(sip_subscriptions.sip_user=sip_presence.sip_user and sip_subscriptions.sip_host=sip_presence.sip_host and "
+ "(sip_subscriptions.sub_to_user=sip_presence.sip_user and sip_subscriptions.sub_to_host=sip_presence.sip_host and "
"sip_subscriptions.profile_name=sip_presence.profile_name) "
"where sip_subscriptions.event='presence'", status, rpid);
}
@@ -489,7 +489,7 @@
",1,'%q','%q','%q',sip_presence.status,sip_presence.rpid "
"from sip_subscriptions "
"left join sip_presence on "
- "(sip_subscriptions.sip_user=sip_presence.sip_user and sip_subscriptions.sip_host=sip_presence.sip_host and "
+ "(sip_subscriptions.sub_to_user=sip_presence.sip_user and sip_subscriptions.sub_to_host=sip_presence.sip_host and "
"sip_subscriptions.profile_name=sip_presence.profile_name) "
"where (event='%q' or event='%q') and sub_to_user='%q' "
"and (sub_to_host='%q' or presence_hosts like '%%%q%%')",
@@ -505,7 +505,7 @@
",0,'%q','%q','%q',sip_presence.status,sip_presence.rpid "
"from sip_subscriptions "
"left join sip_presence on "
- "(sip_subscriptions.sip_user=sip_presence.sip_user and sip_subscriptions.sip_host=sip_presence.sip_host and "
+ "(sip_subscriptions.sub_to_user=sip_presence.sip_user and sip_subscriptions.sub_to_host=sip_presence.sip_host and "
"sip_subscriptions.profile_name=sip_presence.profile_name) "
"where (event='%q' or event='%q') and sub_to_user='%q' "
"and (sub_to_host='%q' or presence_hosts like '%%%q%%')",
@@ -898,12 +898,6 @@
char *status = argv[15];
char *rpid = argv[16];
char *sub_to_host = argv[17];
-
- if (argc > 19 && !switch_strlen_zero(argv[18]) && !switch_strlen_zero(argv[19])) {
- status = argv[18];
- rpid = argv[19];
- }
-
nua_handle_t *nh;
char *to = NULL;
@@ -916,6 +910,17 @@
int is_dialog = 0;
sofia_profile_t *ext_profile = NULL, *profile = helper->profile;
+
+ if (argc > 19 && !switch_strlen_zero(argv[18]) && !switch_strlen_zero(argv[19])) {
+ status = argv[18];
+ rpid = argv[19];
+ }
+
+ if (switch_strlen_zero(status)) {
+ status = "Available";
+ }
+
+
if (profile_name && strcasecmp(profile_name, helper->profile->name)) {
if ((ext_profile = sofia_glue_find_profile(profile_name))) {
1
0
22 Nov '08
Author: anthm
Date: Fri Nov 21 20:50:21 2008
New Revision: 10514
Log:
add patch from MODENDP-152
Modified:
freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h
freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c
Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h (original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/mod_sofia.h Fri Nov 21 20:50:21 2008
@@ -75,6 +75,7 @@
#define MY_EVENT_UNREGISTER "sofia::unregister"
#define MY_EVENT_EXPIRE "sofia::expire"
#define MY_EVENT_GATEWAY_STATE "sofia::gateway_state"
+#define MY_EVENT_NOTIFY_REFER "sofia::notify_refer"
#define MULTICAST_EVENT "multicast::event"
#define SOFIA_REPLACES_HEADER "_sofia_replaces_"
Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c (original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c Fri Nov 21 20:50:21 2008
@@ -84,6 +84,7 @@
{
switch_channel_t *channel = NULL;
private_object_t *tech_pvt = NULL;
+ switch_event_t *s_event = NULL;
/* make sure we have a proper event */
if (!sip || !sip->sip_event) {
@@ -95,7 +96,7 @@
nua_respond(nh, SIP_200_OK, NUTAG_WITH_THIS(nua), TAG_END());
return;
}
-
+
if (session) {
channel = switch_core_session_get_channel(session);
switch_assert(channel != NULL);
@@ -103,6 +104,49 @@
switch_assert(tech_pvt != NULL);
}
+ /* For additional NOTIFY event packages see http://www.iana.org/assignments/sip-events. */
+ if (!strcasecmp(sip->sip_event->o_type, "refer")) {
+ if (switch_event_create_subclass(&s_event, SWITCH_EVENT_CUSTOM, MY_EVENT_NOTIFY_REFER) == SWITCH_STATUS_SUCCESS) {
+ switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "content-type", sip->sip_content_type->c_type);
+ switch_event_add_body(s_event, "%s", sip->sip_payload->pl_data);
+ }
+ }
+
+ /* add common headers for the NOTIFY to the switch_event and fire if it exists */
+ if (s_event != NULL) {
+ switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "event-package", sip->sip_event->o_type);
+ switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "event-id", sip->sip_event->o_id);
+
+ switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "contact", "%s@%s",
+ sip->sip_contact->m_url->url_user, sip->sip_contact->m_url->url_host);
+ switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "from", "%s@%s",
+ sip->sip_from->a_url->url_user, sip->sip_from->a_url->url_host);
+ switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "from-tag", sip->sip_from->a_tag);
+ switch_event_add_header(s_event, SWITCH_STACK_BOTTOM, "to", "%s@%s",
+ sip->sip_to->a_url->url_user, sip->sip_to->a_url->url_host);
+ switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "to-tag", sip->sip_to->a_tag);
+
+ if (sip->sip_call_id && sip->sip_call_id->i_id) {
+ switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "call-id", sip->sip_call_id->i_id);
+ }
+ if (sip->sip_subscription_state && sip->sip_subscription_state->ss_substate) {
+ switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "subscription-substate", sip->sip_subscription_state->ss_substate);
+ }
+ if (sip->sip_subscription_state && sip->sip_subscription_state->ss_reason) {
+ switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "subscription-reason", sip->sip_subscription_state->ss_reason);
+ }
+ if (sip->sip_subscription_state && sip->sip_subscription_state->ss_retry_after) {
+ switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "subscription-retry-after", sip->sip_subscription_state->ss_retry_after);
+ }
+ if (sip->sip_subscription_state && sip->sip_subscription_state->ss_expires) {
+ switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "subscription-expires", sip->sip_subscription_state->ss_expires);
+ }
+ if (session) {
+ switch_event_add_header_string(s_event, SWITCH_STACK_BOTTOM, "UniqueID", switch_core_session_get_uuid(session));
+ }
+ switch_event_fire(&s_event);
+ }
+
if (!strcasecmp(sip->sip_event->o_type, "refer")) {
if (session && channel && tech_pvt) {
if (sip->sip_payload && sip->sip_payload->pl_data) {
1
0
Author: anthm
Date: Fri Nov 21 20:46:31 2008
New Revision: 10513
Log:
fix FSCORE-234
Modified:
freeswitch/trunk/src/switch_core_io.c
Modified: freeswitch/trunk/src/switch_core_io.c
==============================================================================
--- freeswitch/trunk/src/switch_core_io.c (original)
+++ freeswitch/trunk/src/switch_core_io.c Fri Nov 21 20:46:31 2008
@@ -150,6 +150,7 @@
if (switch_channel_test_flag(session->channel, CF_HOLD)) {
+ switch_yield(session->read_codec->implementation->microseconds_per_packet);
status = SWITCH_STATUS_BREAK;
goto even_more_done;
}
1
0
22 Nov '08
Author: anthm
Date: Fri Nov 21 20:34:19 2008
New Revision: 10512
Log:
presence stuff
Modified:
freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c
freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c
freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c
Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c (original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_glue.c Fri Nov 21 20:34:19 2008
@@ -2820,6 +2820,19 @@
" server_user VARCHAR(255),\n"
" server_host VARCHAR(255),\n"
" profile_name VARCHAR(255),\n"
+ " hostname VARCHAR(255),\n"
+ ");\n";
+
+
+ char pres_sql[] =
+ "CREATE TABLE sip_presence (\n"
+ " sip_user VARCHAR(255),\n"
+ " sip_host VARCHAR(255),\n"
+ " status VARCHAR(255),\n"
+ " rpid VARCHAR(255),\n"
+ " expires INTEGER,\n"
+ " user_agent VARCHAR(255),\n"
+ " profile_name VARCHAR(255),\n"
" hostname VARCHAR(255)\n"
");\n";
@@ -2906,6 +2919,13 @@
switch_odbc_handle_exec(profile->master_odbc, dialog_sql, NULL);
}
+ test_sql = switch_mprintf("delete from sip_presence 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_presence", NULL);
+ switch_odbc_handle_exec(profile->master_odbc, pres_sql, NULL);
+ }
+
free(test_sql);
test_sql = switch_mprintf("delete from sip_authentication where hostname='%q'", mod_sofia_globals.hostname);
@@ -2937,6 +2957,11 @@
test_sql = switch_mprintf("delete from sip_dialogs where hostname='%q'", mod_sofia_globals.hostname);
switch_core_db_test_reactive(profile->master_db, test_sql, "DROP TABLE sip_dialogs", dialog_sql);
free(test_sql);
+
+ test_sql = switch_mprintf("delete from sip_presence where hostname='%q'", mod_sofia_globals.hostname);
+ switch_core_db_test_reactive(profile->master_db, test_sql, "DROP TABLE sip_presence", pres_sql);
+ free(test_sql);
+
test_sql = switch_mprintf("delete from sip_authentication where hostname='%q'", mod_sofia_globals.hostname);
switch_core_db_test_reactive(profile->master_db, test_sql, "DROP TABLE sip_authentication", auth_sql);
free(test_sql);
Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c (original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_presence.c Fri Nov 21 20:34:19 2008
@@ -367,13 +367,28 @@
if (from) {
sql = switch_mprintf(
- "select proto,sip_user,sip_host,sub_to_user,sub_to_host,event,contact,call_id,full_from,"
- "full_via,expires,user_agent,accept,profile_name"
- ",1,'%q','%q' from sip_subscriptions where event='presence' and full_from like '%%%q%%'", status, rpid, from);
+ "select sip_subscriptions.proto,sip_subscriptions.sip_user,sip_subscriptions.sip_host,"
+ "sip_subscriptions.sub_to_user,sip_subscriptions.sub_to_host,sip_subscriptions.event,"
+ "sip_subscriptions.contact,sip_subscriptions.call_id,sip_subscriptions.full_from,"
+ "sip_subscriptions.full_via,sip_subscriptions.expires,sip_subscriptions.user_agent,"
+ "sip_subscriptions.accept,sip_subscriptions.profile_name"
+ ",1,'%q','%q',sip_presence.status,sip_presence.rpid "
+ "from sip_subscriptions left join sip_presence on "
+ "(sip_subscriptions.sip_user=sip_presence.sip_user and sip_subscriptions.sip_host=sip_presence.sip_host and "
+ "sip_subscriptions.profile_name=sip_presence.profile_name) "
+ "where sip_subscriptions.event='presence' and sip_subscriptions.full_from like '%%%q%%'", status, rpid, from);
} else {
- sql = switch_mprintf("select proto,sip_user,sip_host,sub_to_user,sub_to_host,event,contact,call_id,full_from,"
- "full_via,expires,user_agent,accept,profile_name"
- ",1,'%q','%q' from sip_subscriptions where event='presence'", status, rpid);
+ sql = switch_mprintf(
+ "select sip_subscriptions.proto,sip_subscriptions.sip_user,sip_subscriptions.sip_host,"
+ "sip_subscriptions.sub_to_user,sip_subscriptions.sub_to_host,sip_subscriptions.event,"
+ "sip_subscriptions.contact,sip_subscriptions.call_id,sip_subscriptions.full_from,"
+ "sip_subscriptions.full_via,sip_subscriptions.expires,sip_subscriptions.user_agent,"
+ "sip_subscriptions.accept,sip_subscriptions.profile_name"
+ ",1,'%q','%q',sip_presence.status,sip_presence.rpid "
+ "from sip_subscriptions left join sip_presence on "
+ "(sip_subscriptions.sip_user=sip_presence.sip_user and sip_subscriptions.sip_host=sip_presence.sip_host and "
+ "sip_subscriptions.profile_name=sip_presence.profile_name) "
+ "where sip_subscriptions.event='presence'", status, rpid);
}
switch_assert(sql != NULL);
@@ -442,10 +457,13 @@
if (probe_euser && probe_host && (profile = sofia_glue_find_profile(probe_host))) {
sql = switch_mprintf("select sip_registrations.sip_user, '%q', sip_registrations.status, "
"sip_registrations.rpid,'', sip_dialogs.uuid, sip_dialogs.state, sip_dialogs.direction, "
- "sip_dialogs.sip_to_user, sip_dialogs.sip_to_host "
+ "sip_dialogs.sip_to_user, sip_dialogs.sip_to_host, sip_presence.status,sip_presence.rpid "
"from sip_registrations left join sip_dialogs on "
- "(sip_dialogs.sip_from_user = sip_registrations.sip_user) "
- "and sip_dialogs.sip_from_host = sip_registrations.sip_host "
+ "(sip_dialogs.sip_from_user = sip_registrations.sip_user "
+ "and sip_dialogs.sip_from_host = sip_registrations.sip_host) "
+ "left join sip_presence on "
+ "(sip_registrations.sip_user=sip_presence.sip_user and sip_registrations.sip_host=sip_presence.sip_host and "
+ "sip_registrations.profile_name=sip_presence.profile_name) "
"where sip_registrations.sip_user='%q' and "
"(sip_registrations.sip_host='%q' or sip_registrations.presence_hosts like '%%%q%%')",
probe_host, probe_euser, probe_host, probe_host);
@@ -461,17 +479,36 @@
}
return;
case SWITCH_EVENT_PRESENCE_IN:
- sql = switch_mprintf("select proto,sip_user,sip_host,sub_to_user,sub_to_host,event,contact,call_id,full_from,"
- "full_via,expires,user_agent,accept,profile_name"
- ",1,'%q','%q','%q' from sip_subscriptions where (event='%q' or event='%q') and sub_to_user='%q' "
+ sql = switch_mprintf(
+
+ "select sip_subscriptions.proto,sip_subscriptions.sip_user,sip_subscriptions.sip_host,"
+ "sip_subscriptions.sub_to_user,sip_subscriptions.sub_to_host,sip_subscriptions.event,"
+ "sip_subscriptions.contact,sip_subscriptions.call_id,sip_subscriptions.full_from,"
+ "sip_subscriptions.full_via,sip_subscriptions.expires,sip_subscriptions.user_agent,"
+ "sip_subscriptions.accept,sip_subscriptions.profile_name"
+ ",1,'%q','%q','%q',sip_presence.status,sip_presence.rpid "
+ "from sip_subscriptions "
+ "left join sip_presence on "
+ "(sip_subscriptions.sip_user=sip_presence.sip_user and sip_subscriptions.sip_host=sip_presence.sip_host and "
+ "sip_subscriptions.profile_name=sip_presence.profile_name) "
+ "where (event='%q' or event='%q') and sub_to_user='%q' "
"and (sub_to_host='%q' or presence_hosts like '%%%q%%')",
status, rpid, host, event_type, alt_event_type, euser, host, host);
break;
case SWITCH_EVENT_PRESENCE_OUT:
- sql = switch_mprintf("select proto,sip_user,sip_host,sub_to_user,sub_to_host,event,contact,call_id,full_from,"
- "full_via,expires,user_agent,accept,profile_name"
- ",0,'%q','%q','%q' from sip_subscriptions where (event='%q' or event='%q') "
- "and sub_to_user='%q' and (sub_to_host='%q' or presence_hosts like '%%%q%%')",
+ sql = switch_mprintf(
+ "select sip_subscriptions.proto,sip_subscriptions.sip_user,sip_subscriptions.sip_host,"
+ "sip_subscriptions.sub_to_user,sip_subscriptions.sub_to_host,sip_subscriptions.event,"
+ "sip_subscriptions.contact,sip_subscriptions.call_id,sip_subscriptions.full_from,"
+ "sip_subscriptions.full_via,sip_subscriptions.expires,sip_subscriptions.user_agent,"
+ "sip_subscriptions.accept,sip_subscriptions.profile_name"
+ ",0,'%q','%q','%q',sip_presence.status,sip_presence.rpid "
+ "from sip_subscriptions "
+ "left join sip_presence on "
+ "(sip_subscriptions.sip_user=sip_presence.sip_user and sip_subscriptions.sip_host=sip_presence.sip_host and "
+ "sip_subscriptions.profile_name=sip_presence.profile_name) "
+ "where (event='%q' or event='%q') and sub_to_user='%q' "
+ "and (sub_to_host='%q' or presence_hosts like '%%%q%%')",
status, rpid, host, event_type, alt_event_type, euser, host, host);
break;
default:
@@ -701,6 +738,7 @@
char *status = argv[2];
char *rpid = argv[3];
char *proto = argv[4];
+
char *to_user = NULL;
char *uuid = NULL;
char *state = NULL;
@@ -717,6 +755,10 @@
switch_url_decode(to_buf);
to_user = to_buf;
}
+ if (argc > 10 && !switch_strlen_zero(argv[9]) && !switch_strlen_zero(argv[10])) {
+ status = argv[9];
+ rpid = argv[10];
+ }
}
if (switch_strlen_zero(proto)) {
@@ -834,7 +876,7 @@
" </rpid:activities>\n"
" <dm:note>%s</dm:note>"
" </dm:person>\n"
- "</presence>", id, prpid, rpid, status);
+ "</presence>", id, open, prpid, status);
}
}
@@ -843,20 +885,26 @@
struct presence_helper *helper = (struct presence_helper *) pArg;
char *pl = NULL;
char *clean_id = NULL, *id = NULL;
- uint32_t in = atoi(argv[14]);
- char *status = argv[15];
- char *rpid = argv[16];
-
char *proto = argv[0];
char *user = argv[1];
char *host = argv[2];
char *sub_to_user = argv[3];
- char *sub_to_host = argv[17];
char *event = argv[5];
char *call_id = argv[7];
char *expires = argv[10];
char *user_agent = argv[11];
char *profile_name = argv[13];
+ uint32_t in = atoi(argv[14]);
+ char *status = argv[15];
+ char *rpid = argv[16];
+ char *sub_to_host = argv[17];
+
+ if (argc > 19 && !switch_strlen_zero(argv[18]) && !switch_strlen_zero(argv[19])) {
+ status = argv[18];
+ rpid = argv[19];
+ }
+
+
nua_handle_t *nh;
char *to = NULL;
char *open;
@@ -1058,6 +1106,9 @@
if (!is_dialog) {
char status_line[256] = "";
+
+ switch_set_string(status_line, status);
+
if (in) {
if (!strcmp(astate, "early")) {
switch_snprintf(status_line, sizeof(status_line), "Ring %s", switch_str_nil(from_id));
@@ -1073,19 +1124,11 @@
}
if (switch_strlen_zero(from_id)) {
- switch_snprintf(status_line, sizeof(status_line), "On The Phone");
+ switch_snprintf(status_line, sizeof(status_line), "On The Phone %s", status);
} else {
switch_snprintf(status_line, sizeof(status_line), "Talk %s", switch_str_nil(from_id));
}
rpid = "on-the-phone";
- } else if (!strcmp(astate, "terminated")) {
- if (!strcasecmp(rpid, "on-the-phone")) {
- switch_snprintf(status_line, sizeof(status_line), "Dialing");
- } else {
- switch_snprintf(status_line, sizeof(status_line), "Available");
- }
- } else {
- switch_set_string(status_line, status);
}
open = "open";
@@ -1093,10 +1136,6 @@
open = "closed";
}
- if (!strncasecmp(status_line, "registered", 10)) {
- switch_snprintf(status_line, sizeof(status_line), "Available");
- }
-
prpid = translate_rpid(rpid);
pl = gen_pidf(user_agent, clean_id, profile->url, open, rpid, prpid, status_line, &ct);
}
@@ -1598,10 +1637,16 @@
switch_event_t *event;
uint8_t in = 0;
char *sql;
+ char *full_agent = NULL;
+ long exp, exp_raw;
if ((xml = switch_xml_parse_str(payload->pl_data, strlen(payload->pl_data)))) {
char *status_txt = "", *note_txt = "";
+ if (sip->sip_user_agent) {
+ full_agent = sip_header_as_string(profile->home, (void *) sip->sip_user_agent);
+ }
+
if ((tuple = switch_xml_child(xml, "tuple")) && (status = switch_xml_child(tuple, "status"))
&& (basic = switch_xml_child(status, "basic"))) {
status_txt = basic->txt;
@@ -1630,12 +1675,26 @@
}
}
+ exp_raw = (sip->sip_expires ? sip->sip_expires->ex_delta : 3600);
+ exp = (long) switch_timestamp(NULL) + exp_raw;
+
if ((sql =
- switch_mprintf("update sip_registrations set status='%q',rpid='%q' where sip_user='%q' and sip_host='%q'",
- note_txt, rpid, from_user, from_host))) {
+ switch_mprintf("delete from sip_presence where sip_user='%q' and sip_host='%q' "
+ " and profile_name='%q' and hostname='%q'",
+ from_user, from_host, profile->name, mod_sofia_globals.hostname
+ ))) {
sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
+ switch_safe_free(sql);
}
-
+
+ if ((sql =
+ switch_mprintf("insert into sip_presence (sip_user, sip_host, status, rpid, expires, user_agent, profile_name, hostname) "
+ "values ('%q','%q','%q','%q',%ld,'%q','%q','%q')",
+ from_user, from_host, note_txt, rpid, exp, full_agent, profile->name, mod_sofia_globals.hostname))) {
+ sofia_glue_execute_sql(profile, &sql, SWITCH_TRUE);
+ switch_safe_free(sql);
+ }
+
event_type = sip_header_as_string(profile->home, (void *) sip->sip_event);
if (in) {
@@ -1643,8 +1702,8 @@
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", SOFIA_CHAT_PROTO);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "rpid", rpid);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", profile->url);
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "user-agent", full_agent);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", from_user, from_host);
-
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "status", note_txt);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", event_type);
switch_event_fire(&event);
@@ -1654,6 +1713,7 @@
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", SOFIA_CHAT_PROTO);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "rpid", rpid);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "login", profile->url);
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "user-agent", full_agent);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "from", "%s@%s", from_user, from_host);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "event_type", event_type);
@@ -1664,6 +1724,10 @@
if (event_type) {
su_free(profile->home, event_type);
}
+
+ if (full_agent) {
+ su_free(profile->home, full_agent);
+ }
switch_xml_free(xml);
}
}
Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c (original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia_reg.c Fri Nov 21 20:34:19 2008
@@ -402,6 +402,16 @@
sofia_glue_actually_execute_sql(profile, SWITCH_FALSE, sql, NULL);
+
+ if (now) {
+ switch_snprintf(sql, sizeof(sql), "delete from sip_presence where expires > 0 and expires <= %ld and hostname='%s'",
+ (long) now, mod_sofia_globals.hostname);
+ } else {
+ switch_snprintf(sql, sizeof(sql), "delete from sip_presence where expires > 0 and hostname='%s'", mod_sofia_globals.hostname);
+ }
+
+ sofia_glue_actually_execute_sql(profile, SWITCH_FALSE, sql, NULL);
+
if (now) {
switch_snprintf(sql, sizeof(sql), "delete from sip_authentication where expires > 0 and expires <= %ld and hostname='%s'",
(long) now, mod_sofia_globals.hostname);
1
0
21 Nov '08
Author: mikej
Date: Fri Nov 21 18:24:03 2008
New Revision: 10511
Log:
uninitialized var
Modified:
freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c
Modified: freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c (original)
+++ freeswitch/trunk/src/mod/endpoints/mod_sofia/sofia.c Fri Nov 21 18:24:03 2008
@@ -107,7 +107,7 @@
if (session && channel && tech_pvt) {
if (sip->sip_payload && sip->sip_payload->pl_data) {
char *p;
- int status;
+ int status = 0;
if ((p = strchr(sip->sip_payload->pl_data, ' '))) {
p++;
if (p) {
1
0
[commit] r10510 - freeswitch/trunk/scripts/contrib/intralanman/C/lcr/mod_lcr
by FreeSWITCH SVN 21 Nov '08
by FreeSWITCH SVN 21 Nov '08
21 Nov '08
Author: mikej
Date: Fri Nov 21 18:04:55 2008
New Revision: 10510
Log:
svn:eol-style
Modified:
freeswitch/trunk/scripts/contrib/intralanman/C/lcr/mod_lcr/mod_lcr.c (contents, props changed)
Modified: freeswitch/trunk/scripts/contrib/intralanman/C/lcr/mod_lcr/mod_lcr.c
==============================================================================
--- freeswitch/trunk/scripts/contrib/intralanman/C/lcr/mod_lcr/mod_lcr.c (original)
+++ freeswitch/trunk/scripts/contrib/intralanman/C/lcr/mod_lcr/mod_lcr.c Fri Nov 21 18:04:55 2008
@@ -1,601 +1,601 @@
-/*
- * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
- * Copyright (C) 2005/2006, Anthony Minessale II <anthmct(a)yahoo.com>
- *
- * 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 <anthmct(a)yahoo.com>
- * Portions created by the Initial Developer are Copyright (C)
- * the Initial Developer. All Rights Reserved.
- *
- * Contributor(s):
- *
- * Raymond Chandler <intralanman(a)gmail.com>
- * Rupa Schomaker <rupa(a)rupa.com>
- *
- * mod_lcr.c -- Least Cost Routing Module
- *
- */
-
-#include <switch.h>
-#include <switch_odbc.h>
-
-
-#define LCR_SYNTAX "lcr <digits>"
-
-/* SQL Query places */
-#define LCR_DIGITS_PLACE 0
-#define LCR_CARRIER_PLACE 1
-#define LCR_RATE_PLACE 2
-#define LCR_GW_PREFIX_PLACE 3
-#define LCR_GW_SUFFIX_PLACE 4
-#define LCR_LSTRIP_PLACE 5
-#define LCR_TSTRIP_PLACE 6
-#define LCR_PREFIX_PLACE 7
-#define LCR_SUFFIX_PLACE 8
-
-
-#define LCR_DIALSTRING_PLACE 3
-#define LCR_HEADERS_COUNT 4
-char headers[LCR_HEADERS_COUNT][32] = {
- "Digit Match",
- "Carrier",
- "Rate",
- "Dialstring",
-};
-
-struct odbc_obj {
- switch_odbc_handle_t *handle;
- SQLHSTMT stmt;
- SQLCHAR *colbuf;
- int32_t cblen;
- SQLCHAR *code;
- int32_t codelen;
-};
-
-struct lcr_obj {
- char *carrier_name;
- char *gw_prefix;
- char *gw_suffix;
- char *digit_str;
- char *prefix;
- char *suffix;
- char *dialstring;
- float rate;
- char *rate_str;
- size_t lstrip;
- size_t tstrip;
- size_t digit_len;
- struct lcr_obj *next;
-};
-
-struct max_obj {
- size_t carrier_name;
- size_t digit_str;
- size_t rate;
- size_t dialstring;
-};
-
-typedef struct odbc_obj odbc_obj_t;
-typedef odbc_obj_t *odbc_handle;
-
-typedef struct lcr_obj lcr_obj_t;
-typedef lcr_obj_t *lcr_route;
-
-typedef struct max_obj max_obj_t;
-typedef max_obj_t *max_len;
-
-struct callback_obj {
- lcr_route head;
- int matches;
- char *lookup_number;
-};
-typedef struct callback_obj callback_t;
-
-static struct {
- switch_memory_pool_t *pool;
- char *dbname;
- char *odbc_dsn;
- switch_mutex_t *mutex;
- switch_odbc_handle_t *master_odbc;
- void *filler1;
-} globals;
-
-
-SWITCH_MODULE_LOAD_FUNCTION(mod_lcr_load);
-SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_lcr_shutdown);
-SWITCH_MODULE_DEFINITION(mod_lcr, mod_lcr_load, mod_lcr_shutdown, NULL);
-
-static char *get_bridge_data(const char *dialed_number, lcr_route cur_route) {
- size_t lstrip;
- size_t tstrip;
- char *data = NULL;
- char *destination_number = NULL;
- char *orig_destination_number = NULL;
-
- orig_destination_number = destination_number = strdup(dialed_number);
-
- tstrip = ((cur_route->digit_len - cur_route->tstrip) + 1);
- lstrip = cur_route->lstrip;
-
- if (strlen(destination_number) > tstrip && cur_route->tstrip > 0) {
- destination_number[tstrip] = '\0';
- }
- if (strlen(destination_number) > lstrip && cur_route->lstrip > 0) {
- destination_number += lstrip;
- }
-
- data = switch_mprintf("%s%s%s%s%s", cur_route->gw_prefix, cur_route->prefix
- , destination_number, cur_route->suffix, cur_route->gw_suffix);
-
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Returning Dialstring %s\n", data);
- switch_safe_free(orig_destination_number);
- return data;
-}
-
-void init_max_lens(max_len maxes) {
- maxes->digit_str = (headers[LCR_DIGITS_PLACE] == NULL ? 0 : strlen(headers[LCR_DIGITS_PLACE]));
- maxes->carrier_name = (headers[LCR_CARRIER_PLACE] == NULL ? 0 : strlen(headers[LCR_CARRIER_PLACE]));
- maxes->dialstring = (headers[LCR_DIALSTRING_PLACE] == NULL ? 0 : strlen(headers[LCR_DIALSTRING_PLACE]));
- maxes->digit_str = (headers[LCR_DIGITS_PLACE] == NULL ? 0 : strlen(headers[LCR_DIGITS_PLACE]));
- maxes->rate = 8;
-}
-
-switch_status_t process_max_lengths(max_obj_t *maxes, lcr_route routes, char *destination_number) {
- lcr_route current = NULL;
-
- if (routes == NULL) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "no routes\n");
- return SWITCH_STATUS_FALSE;
- }
- if (maxes == NULL) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "no maxes\n");
- return SWITCH_STATUS_FALSE;
- }
-
- init_max_lens(maxes);
-
- for (current = routes; current; current = current->next) {
- size_t this_len;
-
- if (current->carrier_name != NULL) {
- this_len = strlen(current->carrier_name);
- if (this_len > maxes->carrier_name) {
- maxes->carrier_name = this_len;
- }
- }
- if (current->dialstring != NULL) {
- this_len = strlen(current->dialstring);
- if (this_len > maxes->dialstring) {
- maxes->dialstring = this_len;
- }
- }
- if (current->digit_str != NULL) {
- if (current->digit_len > maxes->digit_str) {
- maxes->digit_str = current->digit_len;
- }
- }
- if (current->rate_str != NULL) {
- this_len = strlen(current->rate_str);
- if (this_len > maxes->rate) {
- maxes->rate = this_len;
- }
- }
- }
- return SWITCH_STATUS_SUCCESS;
-}
-
-static switch_bool_t lcr_execute_sql_callback(char *sql, switch_core_db_callback_func_t callback, void *pdata) {
- if (globals.odbc_dsn) {
- switch_odbc_handle_callback_exec(globals.master_odbc, sql, callback, pdata);
- return SWITCH_TRUE;
- }
- return SWITCH_FALSE;
-}
-
-int route_add_callback(void *pArg, int argc, char **argv, char **columnNames) {
- lcr_route additional = NULL;
- lcr_route current = NULL;
- callback_t *cbt = (callback_t *) pArg;
- char srate[10];
-
- cbt->matches++;
-
- switch_zmalloc(additional, sizeof(lcr_obj_t));
-
- additional->digit_len = strlen(argv[LCR_DIGITS_PLACE]);
- additional->digit_str = switch_safe_strdup(argv[LCR_DIGITS_PLACE]);
- additional->suffix = switch_safe_strdup(argv[LCR_SUFFIX_PLACE]);
- additional->prefix = switch_safe_strdup(argv[LCR_PREFIX_PLACE]);
- additional->carrier_name = switch_safe_strdup(argv[LCR_CARRIER_PLACE]);
- additional->rate = (float)atof(argv[LCR_RATE_PLACE]);
- switch_snprintf(srate, sizeof(srate), "%0.5f", additional->rate);
- additional->rate_str = switch_safe_strdup(srate);
- additional->gw_prefix = switch_safe_strdup(argv[LCR_GW_PREFIX_PLACE]);
- additional->gw_suffix = switch_safe_strdup(argv[LCR_GW_SUFFIX_PLACE]);
- additional->lstrip = atoi(argv[LCR_LSTRIP_PLACE]);
- additional->tstrip = atoi(argv[LCR_TSTRIP_PLACE]);
- additional->dialstring = get_bridge_data(cbt->lookup_number, additional);
-
- if (cbt->head == NULL) {
- additional->next = cbt->head;
- cbt->head = additional;
-
- return SWITCH_STATUS_SUCCESS;
- }
-
- for (current = cbt->head; current; current = current->next) {
-
- if (switch_strlen_zero(additional->gw_prefix) && switch_strlen_zero(additional->gw_suffix) ) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING
- , "WTF?!? There's no way to dial this Gateway: Carrier: \"%s\" Prefix: \"%s\", Suffix \"%s\"\n"
- , additional->carrier_name
- , additional->gw_prefix, additional->gw_suffix
- );
- break;
- }
-
- if (!strcmp(current->gw_prefix, additional->gw_prefix) && !strcmp(current->gw_suffix, additional->gw_suffix)) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG
- , "Ignoring Duplicate route for termination point (%s:%s)\n"
- , additional->gw_prefix, additional->gw_suffix
- );
- switch_safe_free(additional);
- break;
- }
-
- if (current->next == NULL) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "adding route to end of list\n");
- current->next = additional;
- break;
- }
- }
- return SWITCH_STATUS_SUCCESS;
-}
-
-switch_status_t lcr_do_lookup(char *digits, callback_t *cb_struct) {
- /* instantiate the object/struct we defined earlier */
- switch_stream_handle_t sql_stream = { 0 };
- size_t n, digit_len = strlen(digits);
- char *digits_copy;
-
- if (switch_strlen_zero(digits)) {
- return SWITCH_FALSE;
- }
-
- digits_copy = strdup(digits);
-
- SWITCH_STANDARD_STREAM(sql_stream);
-
- /* set up the query to be executed */
- sql_stream.write_function(&sql_stream,
- "SELECT l.digits, c.carrier_name, l.rate, cg.prefix AS gw_prefix, cg.suffix AS gw_suffix, l.lead_strip, l.trail_strip, l.prefix, l.suffix "
- );
- sql_stream.write_function(&sql_stream, "FROM lcr l JOIN carriers c ON l.carrier_id=c.id JOIN carrier_gateway cg ON c.id=cg.carrier_id ");
- for (n = digit_len; n > 0; n--) {
- digits_copy[n] = '\0';
- sql_stream.write_function(&sql_stream, "%s digits='%s' ", (n==digit_len ? "WHERE" : "OR"), digits_copy);
- }
- sql_stream.write_function(&sql_stream, "ORDER BY digits DESC, rate;");
-
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s\n", (char *)sql_stream.data);
-
- lcr_execute_sql_callback((char *)sql_stream.data, route_add_callback, cb_struct);
- switch_safe_free(sql_stream.data);
-
- return SWITCH_STATUS_SUCCESS;
-}
-
-static switch_status_t lcr_load_config() {
- char *cf = "lcr.conf";
- switch_xml_t cfg, xml, settings, param;
- switch_status_t status = SWITCH_STATUS_SUCCESS;
- char *odbc_user = NULL;
- char *odbc_pass = NULL;
-
- if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf);
- return SWITCH_STATUS_TERM;
- }
-
- if ((settings = switch_xml_child(cfg, "settings"))) {
- for (param = switch_xml_child(settings, "param"); param; param = param->next) {
- char *var = NULL;
- char *val = NULL;
- var = (char *) switch_xml_attr_soft(param, "name");
- val = (char *) switch_xml_attr_soft(param, "value");
- if (!strcasecmp(var, "odbc-dsn") && !switch_strlen_zero(val)) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "odbc_dsn is %s\n", val);
- globals.odbc_dsn = switch_core_strdup(globals.pool, val);
- if ((odbc_user = strchr(globals.odbc_dsn, ':'))) {
- *odbc_user++ = '\0';
- if ((odbc_pass = strchr(odbc_user, ':'))) {
- *odbc_pass++ = '\0';
- }
- }
- }
- }
- }
- if (globals.odbc_dsn) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO
- , "dsn is \"%s\", user is \"%s\", and password is \"%s\"\n"
- , globals.odbc_dsn, odbc_user, odbc_pass
- );
- if (!(globals.master_odbc = switch_odbc_handle_new(globals.odbc_dsn, odbc_user, odbc_pass))) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
- status = SWITCH_STATUS_FALSE;
- goto done;
- }
- if (switch_odbc_handle_connect(globals.master_odbc) != SWITCH_ODBC_SUCCESS) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
- status = SWITCH_STATUS_FALSE;
- goto done;
- }
- }
- done:
- switch_xml_free(xml);
- return status;
-}
-
-static void destroy_list(lcr_route *head) {
- lcr_route cur = NULL, top = *head;
-
- while (top) {
- cur = top;
- top = top->next;
- switch_safe_free(cur->digit_str);
- switch_safe_free(cur->suffix);
- switch_safe_free(cur->prefix);
- switch_safe_free(cur->carrier_name);
- switch_safe_free(cur->gw_prefix);
- switch_safe_free(cur->gw_suffix);
- switch_safe_free(cur->rate_str);
- switch_safe_free(cur);
- }
- *head = NULL;
-}
-
-SWITCH_STANDARD_DIALPLAN(lcr_dialplan_hunt) {
- switch_caller_extension_t *extension = NULL;
- switch_channel_t *channel = switch_core_session_get_channel(session);
- callback_t routes = { 0 };
- lcr_route cur_route = { 0 };
- char *bridge_data = NULL;;
-
- if (!caller_profile) {
- caller_profile = switch_channel_get_caller_profile(channel);
- }
-
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "LCR Lookup on %s\n", caller_profile->destination_number);
- routes.lookup_number = caller_profile->destination_number;
- if (lcr_do_lookup(caller_profile->destination_number, &routes) == SWITCH_STATUS_SUCCESS) {
- if ((extension = switch_caller_extension_new(session, caller_profile->destination_number, caller_profile->destination_number)) == 0) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "memory error!\n");
- destroy_list(&routes.head);
- return NULL;
- }
-
- switch_channel_set_variable(channel, SWITCH_CONTINUE_ON_FAILURE_VARIABLE, "true");
- switch_channel_set_variable(channel, SWITCH_HANGUP_AFTER_BRIDGE_VARIABLE, "true");
-
- for (cur_route = routes.head; cur_route; cur_route = cur_route->next) {
- //bridge_data = get_bridge_data(caller_profile->destination_number, cur_route);
- switch_caller_extension_add_application(session, extension, "bridge", cur_route->dialstring);
- switch_safe_free(bridge_data);
- }
- destroy_list(&routes.head);
- } else {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "LCR lookup failed for %s\n", caller_profile->destination_number);
- }
- switch_safe_free(bridge_data);
-
- return extension;
-}
-
-void str_repeat(size_t how_many, char *what, switch_stream_handle_t *str_stream) {
- size_t i;
-
- //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "repeating %d of '%s'\n", how_many, what);
-
- for (i=0; i<how_many; i++) {
- str_stream->write_function(str_stream, "%s", what);
- }
-}
-
-SWITCH_STANDARD_APP(lcr_app_function)
-{
- int argc = 0;
- char *argv[4] = { 0 };
- char *mydata = NULL;
- char *dest = NULL;
- char rbuf[1024] = "";
- char vbuf[1024] = "";
- char *rbp = rbuf;
- switch_size_t l = 0, rbl = sizeof(rbuf);
- uint32_t cnt = 1;
- switch_channel_t *channel = switch_core_session_get_channel(session);
- char *last_delim = "|";
- callback_t routes = { 0 };
- lcr_route cur_route = { 0 };
-
- if (!(mydata = switch_core_session_strdup(session, data))) {
- return;
- }
-
- if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
- dest = argv[0];
-
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "LCR Lookup on %s\n", dest);
- routes.lookup_number = dest;
- if (lcr_do_lookup(dest, &routes) == SWITCH_STATUS_SUCCESS) {
- for (cur_route = routes.head; cur_route; cur_route = cur_route->next) {
- switch_snprintf(vbuf, sizeof(vbuf), "lcr_route_%d", cnt++);
- switch_channel_set_variable(channel, vbuf, cur_route->dialstring);
- switch_snprintf(rbp, rbl, "%s|", cur_route->dialstring);
- last_delim = end_of_p(rbp);
- l = strlen(cur_route->dialstring) + 1;
- rbp += l;
- rbl -= l;
- }
- switch_snprintf(vbuf, sizeof(vbuf), "%d", cnt - 1);
- switch_channel_set_variable(channel, "lcr_route_count", vbuf);
- *(rbuf + strlen(rbuf) - 1) = '\0';
- switch_channel_set_variable(channel, "lcr_auto_route", rbuf);
- destroy_list(&routes.head);
- } else {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "LCR lookup failed for %s\n", dest);
- }
- }
-}
-
-SWITCH_STANDARD_API(dialplan_lcr_function) {
- char *argv[4] = { 0 };
- int argc;
- char *mydata = NULL;
- char *dialstring = NULL;
- char *destination_number = NULL;
- lcr_route current = NULL;
- max_obj_t maximum_lengths = { 0 };
- callback_t cb_struct = { 0 };
- //switch_malloc(maximum_lengths, sizeof(max_obj_t));
-
- if (switch_strlen_zero(cmd)) {
- goto usage;
- }
-
- mydata = switch_safe_strdup(cmd);
-
- if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
- destination_number = strdup(argv[0]);
- cb_struct.lookup_number = destination_number;
-
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO
- , "data passed to lcr is [%s]\n", cmd
- );
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO
- , "lcr lookup returned [%d]\n"
- , lcr_do_lookup(destination_number, &cb_struct)
- );
- if (cb_struct.head != NULL) {
- size_t len;
-
- process_max_lengths(&maximum_lengths, cb_struct.head, destination_number);
-
- stream->write_function(stream, " | %s", headers[LCR_DIGITS_PLACE]);
- if ((len = (maximum_lengths.digit_str - strlen(headers[LCR_DIGITS_PLACE]))) > 0) {
- str_repeat(len, " ", stream);
- }
-
- stream->write_function(stream, " | %s", headers[LCR_CARRIER_PLACE]);
- if ((len = (maximum_lengths.carrier_name - strlen(headers[LCR_CARRIER_PLACE]))) > 0) {
- str_repeat(len, " ", stream);
- }
-
- stream->write_function(stream, " | %s", headers[LCR_RATE_PLACE]);
- if ((len = (maximum_lengths.rate - strlen(headers[LCR_RATE_PLACE]))) > 0) {
- str_repeat(len, " ", stream);
- }
-
- stream->write_function(stream, " | %s", headers[LCR_DIALSTRING_PLACE]);
- if ((len = (maximum_lengths.dialstring - strlen(headers[LCR_DIALSTRING_PLACE]))) > 0) {
- str_repeat(len, " ", stream);
- }
-
- stream->write_function(stream, " |\n");
-
- current = cb_struct.head;
- while (current) {
-
- dialstring = get_bridge_data(destination_number, current);
-
- stream->write_function(stream, " | %s", current->digit_str);
- str_repeat((maximum_lengths.digit_str - current->digit_len), " ", stream);
-
- stream->write_function(stream, " | %s", current->carrier_name );
- str_repeat((maximum_lengths.carrier_name - strlen(current->carrier_name)), " ", stream);
-
- stream->write_function(stream, " | %s", current->rate_str );
- str_repeat((maximum_lengths.rate - strlen(current->rate_str)), " ", stream);
-
- stream->write_function(stream, " | %s", dialstring);
- str_repeat((maximum_lengths.dialstring - strlen(dialstring)), " ", stream);
-
- stream->write_function(stream, " |\n");
-
- switch_safe_free(dialstring);
- current = current->next;
- }
-
- destroy_list(&cb_struct.head);
- switch_safe_free(dialstring);
- } else {
- stream->write_function(stream, "No Routes To Display\n");
- }
- }
-
- switch_safe_free(mydata);
- return SWITCH_STATUS_SUCCESS;
- usage:
- stream->write_function(stream, "USAGE: %s\n", LCR_SYNTAX);
- return SWITCH_STATUS_SUCCESS;
-}
-
-SWITCH_MODULE_LOAD_FUNCTION(mod_lcr_load) {
- switch_api_interface_t *dialplan_lcr_api_interface;
- switch_application_interface_t *app_interface;
- switch_dialplan_interface_t *dp_interface;
-
- *module_interface = switch_loadable_module_create_module_interface(pool, modname);
-
-#ifndef SWITCH_HAVE_ODBC
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "You must have ODBC support in FreeSWITCH to use this module\n");
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "\t./configure --enable-core-odbc-support\n");
- return SWITCH_STATUS_FALSE;
-#endif
-
- globals.pool = pool;
-
- if (lcr_load_config() != SWITCH_STATUS_SUCCESS) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to load lcr config file\n");
- return SWITCH_STATUS_FALSE;
- }
- if (switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, globals.pool) != SWITCH_STATUS_SUCCESS) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "failed to initialize mutex\n");
- }
-
- SWITCH_ADD_API(dialplan_lcr_api_interface, "lcr", "Least Cost Routing Module", dialplan_lcr_function, LCR_SYNTAX);
- SWITCH_ADD_APP(app_interface, "lcr", "Perform an LCR lookup", "Perform an LCR lookup",
- lcr_app_function, "<number>", SAF_SUPPORT_NOMEDIA);
- SWITCH_ADD_DIALPLAN(dp_interface, "lcr", lcr_dialplan_hunt);
-
- /* indicate that the module should continue to be loaded */
- return SWITCH_STATUS_SUCCESS;
-}
-
-SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_lcr_shutdown) {
- return SWITCH_STATUS_SUCCESS;
-}
-
-/* For Emacs:
- * Local Variables:
- * mode:c
- * indent-tabs-mode:t
- * tab-width:4
- * c-basic-offset:4
- * End:
- * For VIM:
- * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
- */
+/*
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ * Copyright (C) 2005/2006, Anthony Minessale II <anthmct(a)yahoo.com>
+ *
+ * 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 <anthmct(a)yahoo.com>
+ * Portions created by the Initial Developer are Copyright (C)
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *
+ * Raymond Chandler <intralanman(a)gmail.com>
+ * Rupa Schomaker <rupa(a)rupa.com>
+ *
+ * mod_lcr.c -- Least Cost Routing Module
+ *
+ */
+
+#include <switch.h>
+#include <switch_odbc.h>
+
+
+#define LCR_SYNTAX "lcr <digits>"
+
+/* SQL Query places */
+#define LCR_DIGITS_PLACE 0
+#define LCR_CARRIER_PLACE 1
+#define LCR_RATE_PLACE 2
+#define LCR_GW_PREFIX_PLACE 3
+#define LCR_GW_SUFFIX_PLACE 4
+#define LCR_LSTRIP_PLACE 5
+#define LCR_TSTRIP_PLACE 6
+#define LCR_PREFIX_PLACE 7
+#define LCR_SUFFIX_PLACE 8
+
+
+#define LCR_DIALSTRING_PLACE 3
+#define LCR_HEADERS_COUNT 4
+char headers[LCR_HEADERS_COUNT][32] = {
+ "Digit Match",
+ "Carrier",
+ "Rate",
+ "Dialstring",
+};
+
+struct odbc_obj {
+ switch_odbc_handle_t *handle;
+ SQLHSTMT stmt;
+ SQLCHAR *colbuf;
+ int32_t cblen;
+ SQLCHAR *code;
+ int32_t codelen;
+};
+
+struct lcr_obj {
+ char *carrier_name;
+ char *gw_prefix;
+ char *gw_suffix;
+ char *digit_str;
+ char *prefix;
+ char *suffix;
+ char *dialstring;
+ float rate;
+ char *rate_str;
+ size_t lstrip;
+ size_t tstrip;
+ size_t digit_len;
+ struct lcr_obj *next;
+};
+
+struct max_obj {
+ size_t carrier_name;
+ size_t digit_str;
+ size_t rate;
+ size_t dialstring;
+};
+
+typedef struct odbc_obj odbc_obj_t;
+typedef odbc_obj_t *odbc_handle;
+
+typedef struct lcr_obj lcr_obj_t;
+typedef lcr_obj_t *lcr_route;
+
+typedef struct max_obj max_obj_t;
+typedef max_obj_t *max_len;
+
+struct callback_obj {
+ lcr_route head;
+ int matches;
+ char *lookup_number;
+};
+typedef struct callback_obj callback_t;
+
+static struct {
+ switch_memory_pool_t *pool;
+ char *dbname;
+ char *odbc_dsn;
+ switch_mutex_t *mutex;
+ switch_odbc_handle_t *master_odbc;
+ void *filler1;
+} globals;
+
+
+SWITCH_MODULE_LOAD_FUNCTION(mod_lcr_load);
+SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_lcr_shutdown);
+SWITCH_MODULE_DEFINITION(mod_lcr, mod_lcr_load, mod_lcr_shutdown, NULL);
+
+static char *get_bridge_data(const char *dialed_number, lcr_route cur_route) {
+ size_t lstrip;
+ size_t tstrip;
+ char *data = NULL;
+ char *destination_number = NULL;
+ char *orig_destination_number = NULL;
+
+ orig_destination_number = destination_number = strdup(dialed_number);
+
+ tstrip = ((cur_route->digit_len - cur_route->tstrip) + 1);
+ lstrip = cur_route->lstrip;
+
+ if (strlen(destination_number) > tstrip && cur_route->tstrip > 0) {
+ destination_number[tstrip] = '\0';
+ }
+ if (strlen(destination_number) > lstrip && cur_route->lstrip > 0) {
+ destination_number += lstrip;
+ }
+
+ data = switch_mprintf("%s%s%s%s%s", cur_route->gw_prefix, cur_route->prefix
+ , destination_number, cur_route->suffix, cur_route->gw_suffix);
+
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Returning Dialstring %s\n", data);
+ switch_safe_free(orig_destination_number);
+ return data;
+}
+
+void init_max_lens(max_len maxes) {
+ maxes->digit_str = (headers[LCR_DIGITS_PLACE] == NULL ? 0 : strlen(headers[LCR_DIGITS_PLACE]));
+ maxes->carrier_name = (headers[LCR_CARRIER_PLACE] == NULL ? 0 : strlen(headers[LCR_CARRIER_PLACE]));
+ maxes->dialstring = (headers[LCR_DIALSTRING_PLACE] == NULL ? 0 : strlen(headers[LCR_DIALSTRING_PLACE]));
+ maxes->digit_str = (headers[LCR_DIGITS_PLACE] == NULL ? 0 : strlen(headers[LCR_DIGITS_PLACE]));
+ maxes->rate = 8;
+}
+
+switch_status_t process_max_lengths(max_obj_t *maxes, lcr_route routes, char *destination_number) {
+ lcr_route current = NULL;
+
+ if (routes == NULL) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "no routes\n");
+ return SWITCH_STATUS_FALSE;
+ }
+ if (maxes == NULL) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "no maxes\n");
+ return SWITCH_STATUS_FALSE;
+ }
+
+ init_max_lens(maxes);
+
+ for (current = routes; current; current = current->next) {
+ size_t this_len;
+
+ if (current->carrier_name != NULL) {
+ this_len = strlen(current->carrier_name);
+ if (this_len > maxes->carrier_name) {
+ maxes->carrier_name = this_len;
+ }
+ }
+ if (current->dialstring != NULL) {
+ this_len = strlen(current->dialstring);
+ if (this_len > maxes->dialstring) {
+ maxes->dialstring = this_len;
+ }
+ }
+ if (current->digit_str != NULL) {
+ if (current->digit_len > maxes->digit_str) {
+ maxes->digit_str = current->digit_len;
+ }
+ }
+ if (current->rate_str != NULL) {
+ this_len = strlen(current->rate_str);
+ if (this_len > maxes->rate) {
+ maxes->rate = this_len;
+ }
+ }
+ }
+ return SWITCH_STATUS_SUCCESS;
+}
+
+static switch_bool_t lcr_execute_sql_callback(char *sql, switch_core_db_callback_func_t callback, void *pdata) {
+ if (globals.odbc_dsn) {
+ switch_odbc_handle_callback_exec(globals.master_odbc, sql, callback, pdata);
+ return SWITCH_TRUE;
+ }
+ return SWITCH_FALSE;
+}
+
+int route_add_callback(void *pArg, int argc, char **argv, char **columnNames) {
+ lcr_route additional = NULL;
+ lcr_route current = NULL;
+ callback_t *cbt = (callback_t *) pArg;
+ char srate[10];
+
+ cbt->matches++;
+
+ switch_zmalloc(additional, sizeof(lcr_obj_t));
+
+ additional->digit_len = strlen(argv[LCR_DIGITS_PLACE]);
+ additional->digit_str = switch_safe_strdup(argv[LCR_DIGITS_PLACE]);
+ additional->suffix = switch_safe_strdup(argv[LCR_SUFFIX_PLACE]);
+ additional->prefix = switch_safe_strdup(argv[LCR_PREFIX_PLACE]);
+ additional->carrier_name = switch_safe_strdup(argv[LCR_CARRIER_PLACE]);
+ additional->rate = (float)atof(argv[LCR_RATE_PLACE]);
+ switch_snprintf(srate, sizeof(srate), "%0.5f", additional->rate);
+ additional->rate_str = switch_safe_strdup(srate);
+ additional->gw_prefix = switch_safe_strdup(argv[LCR_GW_PREFIX_PLACE]);
+ additional->gw_suffix = switch_safe_strdup(argv[LCR_GW_SUFFIX_PLACE]);
+ additional->lstrip = atoi(argv[LCR_LSTRIP_PLACE]);
+ additional->tstrip = atoi(argv[LCR_TSTRIP_PLACE]);
+ additional->dialstring = get_bridge_data(cbt->lookup_number, additional);
+
+ if (cbt->head == NULL) {
+ additional->next = cbt->head;
+ cbt->head = additional;
+
+ return SWITCH_STATUS_SUCCESS;
+ }
+
+ for (current = cbt->head; current; current = current->next) {
+
+ if (switch_strlen_zero(additional->gw_prefix) && switch_strlen_zero(additional->gw_suffix) ) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING
+ , "WTF?!? There's no way to dial this Gateway: Carrier: \"%s\" Prefix: \"%s\", Suffix \"%s\"\n"
+ , additional->carrier_name
+ , additional->gw_prefix, additional->gw_suffix
+ );
+ break;
+ }
+
+ if (!strcmp(current->gw_prefix, additional->gw_prefix) && !strcmp(current->gw_suffix, additional->gw_suffix)) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG
+ , "Ignoring Duplicate route for termination point (%s:%s)\n"
+ , additional->gw_prefix, additional->gw_suffix
+ );
+ switch_safe_free(additional);
+ break;
+ }
+
+ if (current->next == NULL) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "adding route to end of list\n");
+ current->next = additional;
+ break;
+ }
+ }
+ return SWITCH_STATUS_SUCCESS;
+}
+
+switch_status_t lcr_do_lookup(char *digits, callback_t *cb_struct) {
+ /* instantiate the object/struct we defined earlier */
+ switch_stream_handle_t sql_stream = { 0 };
+ size_t n, digit_len = strlen(digits);
+ char *digits_copy;
+
+ if (switch_strlen_zero(digits)) {
+ return SWITCH_FALSE;
+ }
+
+ digits_copy = strdup(digits);
+
+ SWITCH_STANDARD_STREAM(sql_stream);
+
+ /* set up the query to be executed */
+ sql_stream.write_function(&sql_stream,
+ "SELECT l.digits, c.carrier_name, l.rate, cg.prefix AS gw_prefix, cg.suffix AS gw_suffix, l.lead_strip, l.trail_strip, l.prefix, l.suffix "
+ );
+ sql_stream.write_function(&sql_stream, "FROM lcr l JOIN carriers c ON l.carrier_id=c.id JOIN carrier_gateway cg ON c.id=cg.carrier_id ");
+ for (n = digit_len; n > 0; n--) {
+ digits_copy[n] = '\0';
+ sql_stream.write_function(&sql_stream, "%s digits='%s' ", (n==digit_len ? "WHERE" : "OR"), digits_copy);
+ }
+ sql_stream.write_function(&sql_stream, "ORDER BY digits DESC, rate;");
+
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s\n", (char *)sql_stream.data);
+
+ lcr_execute_sql_callback((char *)sql_stream.data, route_add_callback, cb_struct);
+ switch_safe_free(sql_stream.data);
+
+ return SWITCH_STATUS_SUCCESS;
+}
+
+static switch_status_t lcr_load_config() {
+ char *cf = "lcr.conf";
+ switch_xml_t cfg, xml, settings, param;
+ switch_status_t status = SWITCH_STATUS_SUCCESS;
+ char *odbc_user = NULL;
+ char *odbc_pass = NULL;
+
+ if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf);
+ return SWITCH_STATUS_TERM;
+ }
+
+ if ((settings = switch_xml_child(cfg, "settings"))) {
+ for (param = switch_xml_child(settings, "param"); param; param = param->next) {
+ char *var = NULL;
+ char *val = NULL;
+ var = (char *) switch_xml_attr_soft(param, "name");
+ val = (char *) switch_xml_attr_soft(param, "value");
+ if (!strcasecmp(var, "odbc-dsn") && !switch_strlen_zero(val)) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "odbc_dsn is %s\n", val);
+ globals.odbc_dsn = switch_core_strdup(globals.pool, val);
+ if ((odbc_user = strchr(globals.odbc_dsn, ':'))) {
+ *odbc_user++ = '\0';
+ if ((odbc_pass = strchr(odbc_user, ':'))) {
+ *odbc_pass++ = '\0';
+ }
+ }
+ }
+ }
+ }
+ if (globals.odbc_dsn) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO
+ , "dsn is \"%s\", user is \"%s\", and password is \"%s\"\n"
+ , globals.odbc_dsn, odbc_user, odbc_pass
+ );
+ if (!(globals.master_odbc = switch_odbc_handle_new(globals.odbc_dsn, odbc_user, odbc_pass))) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
+ status = SWITCH_STATUS_FALSE;
+ goto done;
+ }
+ if (switch_odbc_handle_connect(globals.master_odbc) != SWITCH_ODBC_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
+ status = SWITCH_STATUS_FALSE;
+ goto done;
+ }
+ }
+ done:
+ switch_xml_free(xml);
+ return status;
+}
+
+static void destroy_list(lcr_route *head) {
+ lcr_route cur = NULL, top = *head;
+
+ while (top) {
+ cur = top;
+ top = top->next;
+ switch_safe_free(cur->digit_str);
+ switch_safe_free(cur->suffix);
+ switch_safe_free(cur->prefix);
+ switch_safe_free(cur->carrier_name);
+ switch_safe_free(cur->gw_prefix);
+ switch_safe_free(cur->gw_suffix);
+ switch_safe_free(cur->rate_str);
+ switch_safe_free(cur);
+ }
+ *head = NULL;
+}
+
+SWITCH_STANDARD_DIALPLAN(lcr_dialplan_hunt) {
+ switch_caller_extension_t *extension = NULL;
+ switch_channel_t *channel = switch_core_session_get_channel(session);
+ callback_t routes = { 0 };
+ lcr_route cur_route = { 0 };
+ char *bridge_data = NULL;;
+
+ if (!caller_profile) {
+ caller_profile = switch_channel_get_caller_profile(channel);
+ }
+
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "LCR Lookup on %s\n", caller_profile->destination_number);
+ routes.lookup_number = caller_profile->destination_number;
+ if (lcr_do_lookup(caller_profile->destination_number, &routes) == SWITCH_STATUS_SUCCESS) {
+ if ((extension = switch_caller_extension_new(session, caller_profile->destination_number, caller_profile->destination_number)) == 0) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "memory error!\n");
+ destroy_list(&routes.head);
+ return NULL;
+ }
+
+ switch_channel_set_variable(channel, SWITCH_CONTINUE_ON_FAILURE_VARIABLE, "true");
+ switch_channel_set_variable(channel, SWITCH_HANGUP_AFTER_BRIDGE_VARIABLE, "true");
+
+ for (cur_route = routes.head; cur_route; cur_route = cur_route->next) {
+ //bridge_data = get_bridge_data(caller_profile->destination_number, cur_route);
+ switch_caller_extension_add_application(session, extension, "bridge", cur_route->dialstring);
+ switch_safe_free(bridge_data);
+ }
+ destroy_list(&routes.head);
+ } else {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "LCR lookup failed for %s\n", caller_profile->destination_number);
+ }
+ switch_safe_free(bridge_data);
+
+ return extension;
+}
+
+void str_repeat(size_t how_many, char *what, switch_stream_handle_t *str_stream) {
+ size_t i;
+
+ //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "repeating %d of '%s'\n", how_many, what);
+
+ for (i=0; i<how_many; i++) {
+ str_stream->write_function(str_stream, "%s", what);
+ }
+}
+
+SWITCH_STANDARD_APP(lcr_app_function)
+{
+ int argc = 0;
+ char *argv[4] = { 0 };
+ char *mydata = NULL;
+ char *dest = NULL;
+ char rbuf[1024] = "";
+ char vbuf[1024] = "";
+ char *rbp = rbuf;
+ switch_size_t l = 0, rbl = sizeof(rbuf);
+ uint32_t cnt = 1;
+ switch_channel_t *channel = switch_core_session_get_channel(session);
+ char *last_delim = "|";
+ callback_t routes = { 0 };
+ lcr_route cur_route = { 0 };
+
+ if (!(mydata = switch_core_session_strdup(session, data))) {
+ return;
+ }
+
+ if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
+ dest = argv[0];
+
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "LCR Lookup on %s\n", dest);
+ routes.lookup_number = dest;
+ if (lcr_do_lookup(dest, &routes) == SWITCH_STATUS_SUCCESS) {
+ for (cur_route = routes.head; cur_route; cur_route = cur_route->next) {
+ switch_snprintf(vbuf, sizeof(vbuf), "lcr_route_%d", cnt++);
+ switch_channel_set_variable(channel, vbuf, cur_route->dialstring);
+ switch_snprintf(rbp, rbl, "%s|", cur_route->dialstring);
+ last_delim = end_of_p(rbp);
+ l = strlen(cur_route->dialstring) + 1;
+ rbp += l;
+ rbl -= l;
+ }
+ switch_snprintf(vbuf, sizeof(vbuf), "%d", cnt - 1);
+ switch_channel_set_variable(channel, "lcr_route_count", vbuf);
+ *(rbuf + strlen(rbuf) - 1) = '\0';
+ switch_channel_set_variable(channel, "lcr_auto_route", rbuf);
+ destroy_list(&routes.head);
+ } else {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "LCR lookup failed for %s\n", dest);
+ }
+ }
+}
+
+SWITCH_STANDARD_API(dialplan_lcr_function) {
+ char *argv[4] = { 0 };
+ int argc;
+ char *mydata = NULL;
+ char *dialstring = NULL;
+ char *destination_number = NULL;
+ lcr_route current = NULL;
+ max_obj_t maximum_lengths = { 0 };
+ callback_t cb_struct = { 0 };
+ //switch_malloc(maximum_lengths, sizeof(max_obj_t));
+
+ if (switch_strlen_zero(cmd)) {
+ goto usage;
+ }
+
+ mydata = switch_safe_strdup(cmd);
+
+ if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
+ destination_number = strdup(argv[0]);
+ cb_struct.lookup_number = destination_number;
+
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO
+ , "data passed to lcr is [%s]\n", cmd
+ );
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO
+ , "lcr lookup returned [%d]\n"
+ , lcr_do_lookup(destination_number, &cb_struct)
+ );
+ if (cb_struct.head != NULL) {
+ size_t len;
+
+ process_max_lengths(&maximum_lengths, cb_struct.head, destination_number);
+
+ stream->write_function(stream, " | %s", headers[LCR_DIGITS_PLACE]);
+ if ((len = (maximum_lengths.digit_str - strlen(headers[LCR_DIGITS_PLACE]))) > 0) {
+ str_repeat(len, " ", stream);
+ }
+
+ stream->write_function(stream, " | %s", headers[LCR_CARRIER_PLACE]);
+ if ((len = (maximum_lengths.carrier_name - strlen(headers[LCR_CARRIER_PLACE]))) > 0) {
+ str_repeat(len, " ", stream);
+ }
+
+ stream->write_function(stream, " | %s", headers[LCR_RATE_PLACE]);
+ if ((len = (maximum_lengths.rate - strlen(headers[LCR_RATE_PLACE]))) > 0) {
+ str_repeat(len, " ", stream);
+ }
+
+ stream->write_function(stream, " | %s", headers[LCR_DIALSTRING_PLACE]);
+ if ((len = (maximum_lengths.dialstring - strlen(headers[LCR_DIALSTRING_PLACE]))) > 0) {
+ str_repeat(len, " ", stream);
+ }
+
+ stream->write_function(stream, " |\n");
+
+ current = cb_struct.head;
+ while (current) {
+
+ dialstring = get_bridge_data(destination_number, current);
+
+ stream->write_function(stream, " | %s", current->digit_str);
+ str_repeat((maximum_lengths.digit_str - current->digit_len), " ", stream);
+
+ stream->write_function(stream, " | %s", current->carrier_name );
+ str_repeat((maximum_lengths.carrier_name - strlen(current->carrier_name)), " ", stream);
+
+ stream->write_function(stream, " | %s", current->rate_str );
+ str_repeat((maximum_lengths.rate - strlen(current->rate_str)), " ", stream);
+
+ stream->write_function(stream, " | %s", dialstring);
+ str_repeat((maximum_lengths.dialstring - strlen(dialstring)), " ", stream);
+
+ stream->write_function(stream, " |\n");
+
+ switch_safe_free(dialstring);
+ current = current->next;
+ }
+
+ destroy_list(&cb_struct.head);
+ switch_safe_free(dialstring);
+ } else {
+ stream->write_function(stream, "No Routes To Display\n");
+ }
+ }
+
+ switch_safe_free(mydata);
+ return SWITCH_STATUS_SUCCESS;
+ usage:
+ stream->write_function(stream, "USAGE: %s\n", LCR_SYNTAX);
+ return SWITCH_STATUS_SUCCESS;
+}
+
+SWITCH_MODULE_LOAD_FUNCTION(mod_lcr_load) {
+ switch_api_interface_t *dialplan_lcr_api_interface;
+ switch_application_interface_t *app_interface;
+ switch_dialplan_interface_t *dp_interface;
+
+ *module_interface = switch_loadable_module_create_module_interface(pool, modname);
+
+#ifndef SWITCH_HAVE_ODBC
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "You must have ODBC support in FreeSWITCH to use this module\n");
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "\t./configure --enable-core-odbc-support\n");
+ return SWITCH_STATUS_FALSE;
+#endif
+
+ globals.pool = pool;
+
+ if (lcr_load_config() != SWITCH_STATUS_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to load lcr config file\n");
+ return SWITCH_STATUS_FALSE;
+ }
+ if (switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, globals.pool) != SWITCH_STATUS_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "failed to initialize mutex\n");
+ }
+
+ SWITCH_ADD_API(dialplan_lcr_api_interface, "lcr", "Least Cost Routing Module", dialplan_lcr_function, LCR_SYNTAX);
+ SWITCH_ADD_APP(app_interface, "lcr", "Perform an LCR lookup", "Perform an LCR lookup",
+ lcr_app_function, "<number>", SAF_SUPPORT_NOMEDIA);
+ SWITCH_ADD_DIALPLAN(dp_interface, "lcr", lcr_dialplan_hunt);
+
+ /* indicate that the module should continue to be loaded */
+ return SWITCH_STATUS_SUCCESS;
+}
+
+SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_lcr_shutdown) {
+ return SWITCH_STATUS_SUCCESS;
+}
+
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
+ */
1
0
[commit] r10509 - freeswitch/trunk/scripts/contrib/intralanman/C/lcr/sql
by FreeSWITCH SVN 21 Nov '08
by FreeSWITCH SVN 21 Nov '08
21 Nov '08
Author: intralanman
Date: Fri Nov 21 17:48:28 2008
New Revision: 10509
Log:
schema for mysql 5.0 (should work, i think :-))
Added:
freeswitch/trunk/scripts/contrib/intralanman/C/lcr/sql/mysql-5.0.sql
Added: freeswitch/trunk/scripts/contrib/intralanman/C/lcr/sql/mysql-5.0.sql
==============================================================================
--- (empty file)
+++ freeswitch/trunk/scripts/contrib/intralanman/C/lcr/sql/mysql-5.0.sql Fri Nov 21 17:48:28 2008
@@ -0,0 +1,80 @@
+-- MySQL dump 10.11
+--
+-- Host: lmdt.intralanman.com Database: freeswitch
+-- ------------------------------------------------------
+-- Server version 5.0.51a-17-log
+
+/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
+/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
+/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
+/*!40101 SET NAMES utf8 */;
+/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
+/*!40103 SET TIME_ZONE='+00:00' */;
+/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
+/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
+/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
+/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
+
+--
+-- Table structure for table `lcr`
+--
+
+DROP TABLE IF EXISTS `lcr`;
+CREATE TABLE `lcr` (
+ `digits` varchar(15) default NULL,
+ `rate` float(11,5) unsigned NOT NULL,
+ `carrier_id` int(11) NOT NULL,
+ `lead_strip` int(11) NOT NULL,
+ `trail_strip` int(11) NOT NULL,
+ `prefix` varchar(16) NOT NULL,
+ `suffix` varchar(16) NOT NULL,
+ `lcr_profile` varchar(32) default NULL,
+ `date_start` datetime NOT NULL,
+ `date_end` datetime NOT NULL,
+ `quality` float(10,6) NOT NULL,
+ `reliability` float(10,6) NOT NULL,
+ `id` int(11) NOT NULL auto_increment,
+ PRIMARY KEY (`id`),
+ KEY `carrier_id` (`carrier_id`),
+ KEY `digits` (`digits`),
+ KEY `lcr_profile` (`lcr_profile`),
+ KEY `rate` (`rate`),
+ KEY `digits_profile_cid_rate` USING BTREE (`digits`,`rate`),
+ CONSTRAINT `carrier_id` FOREIGN KEY (`carrier_id`) REFERENCES `carriers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
+) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
+
+--
+-- Table structure for table `carriers`
+--
+
+DROP TABLE IF EXISTS `carriers`;
+CREATE TABLE `carriers` (
+ `id` int(11) NOT NULL auto_increment,
+ `carrier_name` varchar(255) default NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;
+
+--
+-- Table structure for table `carrier_gateway`
+--
+
+DROP TABLE IF EXISTS `carrier_gateway`;
+CREATE TABLE `carrier_gateway` (
+ `id` int(11) NOT NULL auto_increment,
+ `carrier_id` int(11) default NULL,
+ `prefix` varchar(255) NOT NULL,
+ `suffix` varchar(255) NOT NULL,
+ PRIMARY KEY (`id`),
+ KEY `carrier_id` (`carrier_id`)
+) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=latin1;
+/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
+
+/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
+/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
+/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
+/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
+/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
+/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
+/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
+
+-- Dump completed on 2008-11-21 22:48:11
1
0
[commit] r10508 - freeswitch/trunk/scripts/contrib/intralanman/C/lcr/mod_lcr
by FreeSWITCH SVN 21 Nov '08
by FreeSWITCH SVN 21 Nov '08
21 Nov '08
Author: intralanman
Date: Fri Nov 21 17:43:55 2008
New Revision: 10508
Log:
more patches from rupa
Modified:
freeswitch/trunk/scripts/contrib/intralanman/C/lcr/mod_lcr/mod_lcr.c
Modified: freeswitch/trunk/scripts/contrib/intralanman/C/lcr/mod_lcr/mod_lcr.c
==============================================================================
--- freeswitch/trunk/scripts/contrib/intralanman/C/lcr/mod_lcr/mod_lcr.c (original)
+++ freeswitch/trunk/scripts/contrib/intralanman/C/lcr/mod_lcr/mod_lcr.c Fri Nov 21 17:43:55 2008
@@ -1,561 +1,601 @@
-/*
- * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
- * Copyright (C) 2005/2006, Anthony Minessale II <anthmct(a)yahoo.com>
- *
- * 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 <anthmct(a)yahoo.com>
- * Portions created by the Initial Developer are Copyright (C)
- * the Initial Developer. All Rights Reserved.
- *
- * Contributor(s):
- *
- * Raymond Chandler <intralanman(a)gmail.com>
- *
- * mod_lcr.c -- Least Cost Routing Module
- *
- */
-
-#include <switch.h>
-#include <switch_odbc.h>
-
-
-#define LCR_SYNTAX "lcr <digits>"
-
-/* SQL Query places */
-#define LCR_DIGITS_PLACE 0
-#define LCR_CARRIER_PLACE 1
-#define LCR_RATE_PLACE 2
-#define LCR_GATEWAY_PLACE 3
-#define LCR_IP_ADDRESS_PLACE 4
-#define LCR_PORT_PLACE 5
-#define LCR_LSTRIP_PLACE 6
-#define LCR_TSTRIP_PLACE 7
-#define LCR_PREFIX_PLACE 8
-#define LCR_SUFFIX_PLACE 9
-
-
-#define LCR_DIALSTRING_PLACE 3
-#define LCR_HEADERS_COUNT 4
-char headers[LCR_HEADERS_COUNT][32] = {
- "Digit Match",
- "Carrier",
- "Rate",
- "Dialstring",
-};
-
-struct odbc_obj {
- switch_odbc_handle_t *handle;
- SQLHSTMT stmt;
- SQLCHAR *colbuf;
- int32_t cblen;
- SQLCHAR *code;
- int32_t codelen;
-};
-
-struct lcr_obj {
- char *carrier_name;
- char *gateway_name;
- char *term_host;
- char *port;
- char *digit_str;
- char *prefix;
- char *suffix;
- char *dialstring;
- float rate;
- size_t lstrip;
- size_t tstrip;
- size_t digit_len;
- struct lcr_obj *next;
-};
-
-struct max_obj {
- size_t carrier_name;
- size_t gateway_name;
- size_t digit_str;
- size_t rate;
- size_t dialstring;
-};
-
-typedef struct odbc_obj odbc_obj_t;
-typedef odbc_obj_t *odbc_handle;
-
-typedef struct lcr_obj lcr_obj_t;
-typedef lcr_obj_t *lcr_route;
-
-typedef struct max_obj max_obj_t;
-typedef max_obj_t *max_len;
-
-struct callback_obj {
- lcr_route head;
- int matches;
- char *lookup_number;
-};
-typedef struct callback_obj callback_t;
-
-static struct {
- switch_memory_pool_t *pool;
- char *dbname;
- char *odbc_dsn;
- switch_mutex_t *mutex;
- switch_odbc_handle_t *master_odbc;
- void *filler1;
-} globals;
-
-
-SWITCH_MODULE_LOAD_FUNCTION(mod_lcr_load);
-SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_lcr_shutdown);
-SWITCH_MODULE_DEFINITION(mod_lcr, mod_lcr_load, mod_lcr_shutdown, NULL);
-
-static char *get_bridge_data(const char *dialed_number, lcr_route cur_route) {
- size_t lstrip;
- size_t tstrip;
- char *data = NULL;
- char *destination_number = NULL;
-
- destination_number = strdup(dialed_number);
-
- tstrip = ((cur_route->digit_len - cur_route->tstrip) + 1);
- lstrip = cur_route->lstrip;
-
- if (strlen(destination_number) > tstrip && cur_route->tstrip > 0) {
- destination_number[tstrip] = '\0';
- }
- if (strlen(destination_number) > lstrip && cur_route->lstrip > 0) {
- destination_number += lstrip;
- }
-
- if (strlen(cur_route->gateway_name) > 0) {
- data = switch_mprintf("sofia/gateway/%s/%s%s%s", cur_route->gateway_name
- , cur_route->prefix, destination_number, cur_route->suffix
- );
- } else {
- char port[8];
- switch_snprintf(port, sizeof(port), ":%s", cur_route->port);
- data = switch_mprintf("sofia/${use_profile}/%s@%s%s"
- , destination_number, cur_route->term_host, (strlen(cur_route->port) > 0 ? port : "")
- );
- }
-
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Returning Dialstring %s\n", data);
- switch_safe_free(destination_number);
- return data;
-}
-
-void init_max_lens(max_len maxes) {
- maxes->digit_str = (headers[LCR_DIGITS_PLACE] == NULL ? 0 : strlen(headers[LCR_DIGITS_PLACE]));
- maxes->carrier_name = (headers[LCR_CARRIER_PLACE] == NULL ? 0 : strlen(headers[LCR_CARRIER_PLACE]));
- maxes->gateway_name = (headers[LCR_GATEWAY_PLACE] == NULL ? 0 : strlen(headers[LCR_GATEWAY_PLACE]));
- maxes->dialstring = (headers[LCR_DIALSTRING_PLACE] == NULL ? 0 : strlen(headers[LCR_DIALSTRING_PLACE]));
- maxes->digit_str = (headers[LCR_DIGITS_PLACE] == NULL ? 0 : strlen(headers[LCR_DIGITS_PLACE]));
- maxes->rate = 8;
-}
-
-switch_status_t process_max_lengths(max_obj_t *maxes, lcr_route routes, char *destination_number) {
- lcr_route current = NULL;
-
- if (routes == NULL) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "no routes\n");
- return SWITCH_STATUS_FALSE;
- }
- if (maxes == NULL) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "no maxes\n");
- return SWITCH_STATUS_FALSE;
- }
-
- init_max_lens(maxes);
-
- for (current = routes; current; current = current->next) {
- size_t this_len;
-
- if (current->gateway_name != NULL) {
- this_len = strlen(current->gateway_name);
- if (this_len > maxes->gateway_name) {
- maxes->gateway_name = this_len;
- }
- }
- if (current->carrier_name != NULL) {
- this_len = strlen(current->carrier_name);
- if (this_len > maxes->carrier_name) {
- maxes->carrier_name = this_len;
- }
- }
- if (current->dialstring != NULL) {
- this_len = strlen(current->dialstring);
- if (this_len > maxes->dialstring) {
- maxes->dialstring = this_len;
- }
- }
- if (current->digit_str != NULL) {
- if (current->digit_len > maxes->digit_str) {
- maxes->digit_str = current->digit_len;
- }
- }
- }
- return SWITCH_STATUS_SUCCESS;
-}
-
-static switch_bool_t lcr_execute_sql_callback(char *sql, switch_core_db_callback_func_t callback, void *pdata) {
- if (globals.odbc_dsn) {
- switch_odbc_handle_callback_exec(globals.master_odbc, sql, callback, pdata);
- return SWITCH_TRUE;
- }
- return SWITCH_FALSE;
-}
-
-int route_add_callback(void *pArg, int argc, char **argv, char **columnNames) {
- lcr_route additional = NULL;
- lcr_route current = NULL;
- callback_t *cbt = (callback_t *) pArg;
-
- cbt->matches++;
-
- switch_zmalloc(additional, sizeof(lcr_obj_t));
-
- additional->digit_len = strlen(argv[LCR_DIGITS_PLACE]);
- additional->digit_str = switch_safe_strdup(argv[LCR_DIGITS_PLACE]);
- additional->suffix = switch_safe_strdup(argv[LCR_SUFFIX_PLACE]);
- additional->prefix = switch_safe_strdup(argv[LCR_PREFIX_PLACE]);
- additional->carrier_name = switch_safe_strdup(argv[LCR_CARRIER_PLACE]);
- additional->rate = (float)atof(argv[LCR_RATE_PLACE]);
- additional->gateway_name = switch_safe_strdup(argv[LCR_GATEWAY_PLACE]);
- additional->term_host = switch_safe_strdup(argv[LCR_IP_ADDRESS_PLACE]);
- additional->port = switch_safe_strdup(argv[LCR_PORT_PLACE]);
- additional->lstrip = atoi(argv[LCR_LSTRIP_PLACE]);
- additional->tstrip = atoi(argv[LCR_TSTRIP_PLACE]);
- additional->dialstring = get_bridge_data(cbt->lookup_number, additional);
-
- if (cbt->head == NULL) {
- additional->next = cbt->head;
- cbt->head = additional;
-
- return SWITCH_STATUS_SUCCESS;
- }
-
- for (current = cbt->head; current; current = current->next) {
-
- if (switch_strlen_zero(additional->gateway_name) && switch_strlen_zero(additional->term_host) && switch_strlen_zero(additional->port) ) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING
- , "WTF?!? There's no way to dial this Gateway: %s IP:Port %s:%s\n"
- , additional->gateway_name, additional->term_host, additional->port
- );
- break;
- }
-
- if (!strcmp(current->gateway_name, additional->gateway_name)) {
- if (!strcmp(current->term_host, additional->term_host) && !strcmp(current->port, additional->port)) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG
- , "Ignoring Duplicate route for termination point (%s:%s)\n"
- , additional->term_host, additional->port
- );
- switch_safe_free(additional);
- break;
- }
- }
-
- if (current->next == NULL) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "adding route to end of list\n");
- current->next = additional;
- break;
- }
- }
- return SWITCH_STATUS_SUCCESS;
-}
-
-switch_status_t lcr_do_lookup(char *digits, callback_t *cb_struct) {
- /* instantiate the object/struct we defined earlier */
- switch_stream_handle_t sql_stream = { 0 };
- size_t n, digit_len = strlen(digits);
- char *digits_copy;
-
- if (switch_strlen_zero(digits)) {
- return SWITCH_FALSE;
- }
-
- digits_copy = strdup(digits);
-
- SWITCH_STANDARD_STREAM(sql_stream);
-
- /* set up the query to be executed */
- sql_stream.write_function(&sql_stream,
- "SELECT l.digits, c.Carrier_Name, l.rate, cg.gateway, cg.term_host, cg.port, l.lead_strip, l.trail_strip, l.prefix, l.suffix "
- );
- sql_stream.write_function(&sql_stream, "FROM lcr l JOIN carriers c ON l.carrier_id=c.id JOIN carrier_gateway cg ON c.id=cg.carrier_id ");
- for (n = digit_len; n > 0; n--) {
- digits_copy[n] = '\0';
- sql_stream.write_function(&sql_stream, "%s digits='%s' ", (n==digit_len ? "WHERE" : "OR"), digits_copy);
- }
- sql_stream.write_function(&sql_stream, "ORDER BY digits DESC, rate;");
-
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s\n", (char *)sql_stream.data);
-
- lcr_execute_sql_callback((char *)sql_stream.data, route_add_callback, cb_struct);
- switch_safe_free(sql_stream.data);
-
- return SWITCH_STATUS_SUCCESS;
-}
-
-static switch_status_t lcr_load_config() {
- char *cf = "lcr.conf";
- switch_xml_t cfg, xml, settings, param;
- switch_status_t status = SWITCH_STATUS_SUCCESS;
- char *odbc_user = NULL;
- char *odbc_pass = NULL;
-
- if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf);
- return SWITCH_STATUS_TERM;
- }
-
- if ((settings = switch_xml_child(cfg, "settings"))) {
- for (param = switch_xml_child(settings, "param"); param; param = param->next) {
- char *var = NULL;
- char *val = NULL;
- var = (char *) switch_xml_attr_soft(param, "name");
- val = (char *) switch_xml_attr_soft(param, "value");
- if (!strcasecmp(var, "odbc-dsn") && !switch_strlen_zero(val)) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "odbc_dsn is %s\n", val);
- globals.odbc_dsn = switch_core_strdup(globals.pool, val);
- if ((odbc_user = strchr(globals.odbc_dsn, ':'))) {
- *odbc_user++ = '\0';
- if ((odbc_pass = strchr(odbc_user, ':'))) {
- *odbc_pass++ = '\0';
- }
- }
- }
- }
- }
- if (globals.odbc_dsn) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO
- , "dsn is \"%s\", user is \"%s\", and password is \"%s\"\n"
- , globals.odbc_dsn, odbc_user, odbc_pass
- );
- if (!(globals.master_odbc = switch_odbc_handle_new(globals.odbc_dsn, odbc_user, odbc_pass))) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
- status = SWITCH_STATUS_FALSE;
- goto done;
- }
- if (switch_odbc_handle_connect(globals.master_odbc) != SWITCH_ODBC_SUCCESS) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
- status = SWITCH_STATUS_FALSE;
- goto done;
- }
- }
- done:
- switch_xml_free(xml);
- return status;
-}
-
-static void destroy_list(lcr_route *head) {
- lcr_route cur = NULL, top = *head;
-
- while (top) {
- cur = top;
- top = top->next;
- switch_safe_free(cur->digit_str);
- switch_safe_free(cur->suffix);
- switch_safe_free(cur->prefix);
- switch_safe_free(cur->carrier_name);
- switch_safe_free(cur->gateway_name);
- switch_safe_free(cur);
- }
- *head = NULL;
-}
-
-SWITCH_STANDARD_DIALPLAN(lcr_dialplan_hunt) {
- switch_caller_extension_t *extension = NULL;
- switch_channel_t *channel = switch_core_session_get_channel(session);
- callback_t routes = { 0 };
- lcr_route cur_route = { 0 };
- char *bridge_data = NULL;;
-
- if (!caller_profile) {
- caller_profile = switch_channel_get_caller_profile(channel);
- }
-
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "LCR Lookup on %s\n", caller_profile->destination_number);
- routes.lookup_number = caller_profile->destination_number;
- if (lcr_do_lookup(caller_profile->destination_number, &routes) == SWITCH_STATUS_SUCCESS) {
- if ((extension = switch_caller_extension_new(session, caller_profile->destination_number, caller_profile->destination_number)) == 0) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "memory error!\n");
- destroy_list(&routes.head);
- return NULL;
- }
-
- switch_channel_set_variable(channel, SWITCH_CONTINUE_ON_FAILURE_VARIABLE, "true");
- switch_channel_set_variable(channel, SWITCH_HANGUP_AFTER_BRIDGE_VARIABLE, "true");
-
- for (cur_route = routes.head; cur_route; cur_route = cur_route->next) {
- //bridge_data = get_bridge_data(caller_profile->destination_number, cur_route);
- switch_caller_extension_add_application(session, extension, "bridge", cur_route->dialstring);
- switch_safe_free(bridge_data);
- }
- destroy_list(&routes.head);
- } else {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "LCR lookup failed for %s\n", caller_profile->destination_number);
- }
- switch_safe_free(bridge_data);
-
- return extension;
-}
-
-void str_repeat(size_t how_many, char *what, switch_stream_handle_t *str_stream) {
- size_t i;
-
- //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "repeating %d of '%s'\n", how_many, what);
-
- for (i=0; i<how_many; i++) {
- str_stream->write_function(str_stream, "%s", what);
- }
-}
-
-SWITCH_STANDARD_API(dialplan_lcr_function) {
- char *argv[4] = { 0 };
- int argc;
- char *mydata = NULL;
- char *dialstring = NULL;
- char *destination_number = NULL;
- lcr_route current = NULL;
- max_obj_t maximum_lengths = { 0 };
- callback_t cb_struct = { 0 };
- //switch_malloc(maximum_lengths, sizeof(max_obj_t));
-
- if (switch_strlen_zero(cmd)) {
- goto usage;
- }
-
- mydata = switch_safe_strdup(cmd);
-
- if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
- destination_number = strdup(argv[0]);
- cb_struct.lookup_number = destination_number;
-
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO
- , "data passed to lcr is [%s]\n", cmd
- );
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO
- , "lcr lookup returned [%d]\n"
- , lcr_do_lookup(destination_number, &cb_struct)
- );
- if (cb_struct.head != NULL) {
- size_t len;
-
- process_max_lengths(&maximum_lengths, cb_struct.head, destination_number);
-
- stream->write_function(stream, " | %s", headers[LCR_DIGITS_PLACE]);
- if ((len = (maximum_lengths.digit_str - strlen(headers[LCR_DIGITS_PLACE]))) > 0) {
- str_repeat(len, " ", stream);
- }
-
- stream->write_function(stream, " | %s", headers[LCR_CARRIER_PLACE]);
- if ((len = (maximum_lengths.carrier_name - strlen(headers[LCR_CARRIER_PLACE]))) > 0) {
- str_repeat(len, " ", stream);
- }
-
- stream->write_function(stream, " | %s", headers[LCR_RATE_PLACE]);
- if ((len = (maximum_lengths.rate - strlen(headers[LCR_RATE_PLACE]))) > 0) {
- str_repeat(len, " ", stream);
- }
-
- stream->write_function(stream, " | %s", headers[LCR_DIALSTRING_PLACE]);
- if ((len = (maximum_lengths.dialstring - strlen(headers[LCR_DIALSTRING_PLACE]))) > 0) {
- str_repeat(len, " ", stream);
- }
-
- stream->write_function(stream, " |\n");
-
- current = cb_struct.head;
- while (current) {
- char srate[10];
-
- dialstring = get_bridge_data(destination_number, current);
- switch_snprintf(srate, sizeof(srate), "%0.5f", current->rate);
-
- stream->write_function(stream, " | %s", current->digit_str);
- str_repeat((maximum_lengths.digit_str - current->digit_len), " ", stream);
-
- stream->write_function(stream, " | %s", current->carrier_name );
- str_repeat((maximum_lengths.carrier_name - strlen(current->carrier_name)), " ", stream);
-
- stream->write_function(stream, " | %s", srate );
- str_repeat((maximum_lengths.rate - strlen(srate)), " ", stream);
-
- stream->write_function(stream, " | %s", dialstring);
- str_repeat((maximum_lengths.dialstring - strlen(dialstring)), " ", stream);
-
- stream->write_function(stream, " |\n");
-
- switch_safe_free(dialstring);
- current = current->next;
- }
-
- destroy_list(&cb_struct.head);
- switch_safe_free(dialstring);
- } else {
- stream->write_function(stream, "No Routes To Display\n");
- }
- }
-
- switch_safe_free(mydata);
- return SWITCH_STATUS_SUCCESS;
- usage:
- stream->write_function(stream, "USAGE: %s\n", LCR_SYNTAX);
- return SWITCH_STATUS_SUCCESS;
-}
-
-SWITCH_MODULE_LOAD_FUNCTION(mod_lcr_load) {
- switch_api_interface_t *dialplan_lcr_api_interface;
- switch_dialplan_interface_t *dp_interface;
-
- *module_interface = switch_loadable_module_create_module_interface(pool, modname);
-
-#ifndef SWITCH_HAVE_ODBC
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "You must have ODBC support in FreeSWITCH to use this module\n");
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "\t./configure --enable-core-odbc-support\n");
- return SWITCH_STATUS_FALSE;
-#endif
-
- globals.pool = pool;
-
- if (lcr_load_config() != SWITCH_STATUS_SUCCESS) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to load lcr config file\n");
- return SWITCH_STATUS_FALSE;
- }
- if (switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, globals.pool) != SWITCH_STATUS_SUCCESS) {
- switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "failed to initialize mutex\n");
- }
-
- SWITCH_ADD_API(dialplan_lcr_api_interface, "lcr", "Least Cost Routing Module", dialplan_lcr_function, LCR_SYNTAX);
- SWITCH_ADD_DIALPLAN(dp_interface, "lcr", lcr_dialplan_hunt);
-
- /* indicate that the module should continue to be loaded */
- return SWITCH_STATUS_SUCCESS;
-}
-
-SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_lcr_shutdown) {
- return SWITCH_STATUS_SUCCESS;
-}
-
-/* For Emacs:
- * Local Variables:
- * mode:c
- * indent-tabs-mode:t
- * tab-width:4
- * c-basic-offset:4
- * End:
- * For VIM:
- * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
- */
+/*
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ * Copyright (C) 2005/2006, Anthony Minessale II <anthmct(a)yahoo.com>
+ *
+ * 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 <anthmct(a)yahoo.com>
+ * Portions created by the Initial Developer are Copyright (C)
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *
+ * Raymond Chandler <intralanman(a)gmail.com>
+ * Rupa Schomaker <rupa(a)rupa.com>
+ *
+ * mod_lcr.c -- Least Cost Routing Module
+ *
+ */
+
+#include <switch.h>
+#include <switch_odbc.h>
+
+
+#define LCR_SYNTAX "lcr <digits>"
+
+/* SQL Query places */
+#define LCR_DIGITS_PLACE 0
+#define LCR_CARRIER_PLACE 1
+#define LCR_RATE_PLACE 2
+#define LCR_GW_PREFIX_PLACE 3
+#define LCR_GW_SUFFIX_PLACE 4
+#define LCR_LSTRIP_PLACE 5
+#define LCR_TSTRIP_PLACE 6
+#define LCR_PREFIX_PLACE 7
+#define LCR_SUFFIX_PLACE 8
+
+
+#define LCR_DIALSTRING_PLACE 3
+#define LCR_HEADERS_COUNT 4
+char headers[LCR_HEADERS_COUNT][32] = {
+ "Digit Match",
+ "Carrier",
+ "Rate",
+ "Dialstring",
+};
+
+struct odbc_obj {
+ switch_odbc_handle_t *handle;
+ SQLHSTMT stmt;
+ SQLCHAR *colbuf;
+ int32_t cblen;
+ SQLCHAR *code;
+ int32_t codelen;
+};
+
+struct lcr_obj {
+ char *carrier_name;
+ char *gw_prefix;
+ char *gw_suffix;
+ char *digit_str;
+ char *prefix;
+ char *suffix;
+ char *dialstring;
+ float rate;
+ char *rate_str;
+ size_t lstrip;
+ size_t tstrip;
+ size_t digit_len;
+ struct lcr_obj *next;
+};
+
+struct max_obj {
+ size_t carrier_name;
+ size_t digit_str;
+ size_t rate;
+ size_t dialstring;
+};
+
+typedef struct odbc_obj odbc_obj_t;
+typedef odbc_obj_t *odbc_handle;
+
+typedef struct lcr_obj lcr_obj_t;
+typedef lcr_obj_t *lcr_route;
+
+typedef struct max_obj max_obj_t;
+typedef max_obj_t *max_len;
+
+struct callback_obj {
+ lcr_route head;
+ int matches;
+ char *lookup_number;
+};
+typedef struct callback_obj callback_t;
+
+static struct {
+ switch_memory_pool_t *pool;
+ char *dbname;
+ char *odbc_dsn;
+ switch_mutex_t *mutex;
+ switch_odbc_handle_t *master_odbc;
+ void *filler1;
+} globals;
+
+
+SWITCH_MODULE_LOAD_FUNCTION(mod_lcr_load);
+SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_lcr_shutdown);
+SWITCH_MODULE_DEFINITION(mod_lcr, mod_lcr_load, mod_lcr_shutdown, NULL);
+
+static char *get_bridge_data(const char *dialed_number, lcr_route cur_route) {
+ size_t lstrip;
+ size_t tstrip;
+ char *data = NULL;
+ char *destination_number = NULL;
+ char *orig_destination_number = NULL;
+
+ orig_destination_number = destination_number = strdup(dialed_number);
+
+ tstrip = ((cur_route->digit_len - cur_route->tstrip) + 1);
+ lstrip = cur_route->lstrip;
+
+ if (strlen(destination_number) > tstrip && cur_route->tstrip > 0) {
+ destination_number[tstrip] = '\0';
+ }
+ if (strlen(destination_number) > lstrip && cur_route->lstrip > 0) {
+ destination_number += lstrip;
+ }
+
+ data = switch_mprintf("%s%s%s%s%s", cur_route->gw_prefix, cur_route->prefix
+ , destination_number, cur_route->suffix, cur_route->gw_suffix);
+
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Returning Dialstring %s\n", data);
+ switch_safe_free(orig_destination_number);
+ return data;
+}
+
+void init_max_lens(max_len maxes) {
+ maxes->digit_str = (headers[LCR_DIGITS_PLACE] == NULL ? 0 : strlen(headers[LCR_DIGITS_PLACE]));
+ maxes->carrier_name = (headers[LCR_CARRIER_PLACE] == NULL ? 0 : strlen(headers[LCR_CARRIER_PLACE]));
+ maxes->dialstring = (headers[LCR_DIALSTRING_PLACE] == NULL ? 0 : strlen(headers[LCR_DIALSTRING_PLACE]));
+ maxes->digit_str = (headers[LCR_DIGITS_PLACE] == NULL ? 0 : strlen(headers[LCR_DIGITS_PLACE]));
+ maxes->rate = 8;
+}
+
+switch_status_t process_max_lengths(max_obj_t *maxes, lcr_route routes, char *destination_number) {
+ lcr_route current = NULL;
+
+ if (routes == NULL) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "no routes\n");
+ return SWITCH_STATUS_FALSE;
+ }
+ if (maxes == NULL) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "no maxes\n");
+ return SWITCH_STATUS_FALSE;
+ }
+
+ init_max_lens(maxes);
+
+ for (current = routes; current; current = current->next) {
+ size_t this_len;
+
+ if (current->carrier_name != NULL) {
+ this_len = strlen(current->carrier_name);
+ if (this_len > maxes->carrier_name) {
+ maxes->carrier_name = this_len;
+ }
+ }
+ if (current->dialstring != NULL) {
+ this_len = strlen(current->dialstring);
+ if (this_len > maxes->dialstring) {
+ maxes->dialstring = this_len;
+ }
+ }
+ if (current->digit_str != NULL) {
+ if (current->digit_len > maxes->digit_str) {
+ maxes->digit_str = current->digit_len;
+ }
+ }
+ if (current->rate_str != NULL) {
+ this_len = strlen(current->rate_str);
+ if (this_len > maxes->rate) {
+ maxes->rate = this_len;
+ }
+ }
+ }
+ return SWITCH_STATUS_SUCCESS;
+}
+
+static switch_bool_t lcr_execute_sql_callback(char *sql, switch_core_db_callback_func_t callback, void *pdata) {
+ if (globals.odbc_dsn) {
+ switch_odbc_handle_callback_exec(globals.master_odbc, sql, callback, pdata);
+ return SWITCH_TRUE;
+ }
+ return SWITCH_FALSE;
+}
+
+int route_add_callback(void *pArg, int argc, char **argv, char **columnNames) {
+ lcr_route additional = NULL;
+ lcr_route current = NULL;
+ callback_t *cbt = (callback_t *) pArg;
+ char srate[10];
+
+ cbt->matches++;
+
+ switch_zmalloc(additional, sizeof(lcr_obj_t));
+
+ additional->digit_len = strlen(argv[LCR_DIGITS_PLACE]);
+ additional->digit_str = switch_safe_strdup(argv[LCR_DIGITS_PLACE]);
+ additional->suffix = switch_safe_strdup(argv[LCR_SUFFIX_PLACE]);
+ additional->prefix = switch_safe_strdup(argv[LCR_PREFIX_PLACE]);
+ additional->carrier_name = switch_safe_strdup(argv[LCR_CARRIER_PLACE]);
+ additional->rate = (float)atof(argv[LCR_RATE_PLACE]);
+ switch_snprintf(srate, sizeof(srate), "%0.5f", additional->rate);
+ additional->rate_str = switch_safe_strdup(srate);
+ additional->gw_prefix = switch_safe_strdup(argv[LCR_GW_PREFIX_PLACE]);
+ additional->gw_suffix = switch_safe_strdup(argv[LCR_GW_SUFFIX_PLACE]);
+ additional->lstrip = atoi(argv[LCR_LSTRIP_PLACE]);
+ additional->tstrip = atoi(argv[LCR_TSTRIP_PLACE]);
+ additional->dialstring = get_bridge_data(cbt->lookup_number, additional);
+
+ if (cbt->head == NULL) {
+ additional->next = cbt->head;
+ cbt->head = additional;
+
+ return SWITCH_STATUS_SUCCESS;
+ }
+
+ for (current = cbt->head; current; current = current->next) {
+
+ if (switch_strlen_zero(additional->gw_prefix) && switch_strlen_zero(additional->gw_suffix) ) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING
+ , "WTF?!? There's no way to dial this Gateway: Carrier: \"%s\" Prefix: \"%s\", Suffix \"%s\"\n"
+ , additional->carrier_name
+ , additional->gw_prefix, additional->gw_suffix
+ );
+ break;
+ }
+
+ if (!strcmp(current->gw_prefix, additional->gw_prefix) && !strcmp(current->gw_suffix, additional->gw_suffix)) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG
+ , "Ignoring Duplicate route for termination point (%s:%s)\n"
+ , additional->gw_prefix, additional->gw_suffix
+ );
+ switch_safe_free(additional);
+ break;
+ }
+
+ if (current->next == NULL) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "adding route to end of list\n");
+ current->next = additional;
+ break;
+ }
+ }
+ return SWITCH_STATUS_SUCCESS;
+}
+
+switch_status_t lcr_do_lookup(char *digits, callback_t *cb_struct) {
+ /* instantiate the object/struct we defined earlier */
+ switch_stream_handle_t sql_stream = { 0 };
+ size_t n, digit_len = strlen(digits);
+ char *digits_copy;
+
+ if (switch_strlen_zero(digits)) {
+ return SWITCH_FALSE;
+ }
+
+ digits_copy = strdup(digits);
+
+ SWITCH_STANDARD_STREAM(sql_stream);
+
+ /* set up the query to be executed */
+ sql_stream.write_function(&sql_stream,
+ "SELECT l.digits, c.carrier_name, l.rate, cg.prefix AS gw_prefix, cg.suffix AS gw_suffix, l.lead_strip, l.trail_strip, l.prefix, l.suffix "
+ );
+ sql_stream.write_function(&sql_stream, "FROM lcr l JOIN carriers c ON l.carrier_id=c.id JOIN carrier_gateway cg ON c.id=cg.carrier_id ");
+ for (n = digit_len; n > 0; n--) {
+ digits_copy[n] = '\0';
+ sql_stream.write_function(&sql_stream, "%s digits='%s' ", (n==digit_len ? "WHERE" : "OR"), digits_copy);
+ }
+ sql_stream.write_function(&sql_stream, "ORDER BY digits DESC, rate;");
+
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s\n", (char *)sql_stream.data);
+
+ lcr_execute_sql_callback((char *)sql_stream.data, route_add_callback, cb_struct);
+ switch_safe_free(sql_stream.data);
+
+ return SWITCH_STATUS_SUCCESS;
+}
+
+static switch_status_t lcr_load_config() {
+ char *cf = "lcr.conf";
+ switch_xml_t cfg, xml, settings, param;
+ switch_status_t status = SWITCH_STATUS_SUCCESS;
+ char *odbc_user = NULL;
+ char *odbc_pass = NULL;
+
+ if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf);
+ return SWITCH_STATUS_TERM;
+ }
+
+ if ((settings = switch_xml_child(cfg, "settings"))) {
+ for (param = switch_xml_child(settings, "param"); param; param = param->next) {
+ char *var = NULL;
+ char *val = NULL;
+ var = (char *) switch_xml_attr_soft(param, "name");
+ val = (char *) switch_xml_attr_soft(param, "value");
+ if (!strcasecmp(var, "odbc-dsn") && !switch_strlen_zero(val)) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "odbc_dsn is %s\n", val);
+ globals.odbc_dsn = switch_core_strdup(globals.pool, val);
+ if ((odbc_user = strchr(globals.odbc_dsn, ':'))) {
+ *odbc_user++ = '\0';
+ if ((odbc_pass = strchr(odbc_user, ':'))) {
+ *odbc_pass++ = '\0';
+ }
+ }
+ }
+ }
+ }
+ if (globals.odbc_dsn) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO
+ , "dsn is \"%s\", user is \"%s\", and password is \"%s\"\n"
+ , globals.odbc_dsn, odbc_user, odbc_pass
+ );
+ if (!(globals.master_odbc = switch_odbc_handle_new(globals.odbc_dsn, odbc_user, odbc_pass))) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
+ status = SWITCH_STATUS_FALSE;
+ goto done;
+ }
+ if (switch_odbc_handle_connect(globals.master_odbc) != SWITCH_ODBC_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Cannot Open ODBC Database!\n");
+ status = SWITCH_STATUS_FALSE;
+ goto done;
+ }
+ }
+ done:
+ switch_xml_free(xml);
+ return status;
+}
+
+static void destroy_list(lcr_route *head) {
+ lcr_route cur = NULL, top = *head;
+
+ while (top) {
+ cur = top;
+ top = top->next;
+ switch_safe_free(cur->digit_str);
+ switch_safe_free(cur->suffix);
+ switch_safe_free(cur->prefix);
+ switch_safe_free(cur->carrier_name);
+ switch_safe_free(cur->gw_prefix);
+ switch_safe_free(cur->gw_suffix);
+ switch_safe_free(cur->rate_str);
+ switch_safe_free(cur);
+ }
+ *head = NULL;
+}
+
+SWITCH_STANDARD_DIALPLAN(lcr_dialplan_hunt) {
+ switch_caller_extension_t *extension = NULL;
+ switch_channel_t *channel = switch_core_session_get_channel(session);
+ callback_t routes = { 0 };
+ lcr_route cur_route = { 0 };
+ char *bridge_data = NULL;;
+
+ if (!caller_profile) {
+ caller_profile = switch_channel_get_caller_profile(channel);
+ }
+
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "LCR Lookup on %s\n", caller_profile->destination_number);
+ routes.lookup_number = caller_profile->destination_number;
+ if (lcr_do_lookup(caller_profile->destination_number, &routes) == SWITCH_STATUS_SUCCESS) {
+ if ((extension = switch_caller_extension_new(session, caller_profile->destination_number, caller_profile->destination_number)) == 0) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "memory error!\n");
+ destroy_list(&routes.head);
+ return NULL;
+ }
+
+ switch_channel_set_variable(channel, SWITCH_CONTINUE_ON_FAILURE_VARIABLE, "true");
+ switch_channel_set_variable(channel, SWITCH_HANGUP_AFTER_BRIDGE_VARIABLE, "true");
+
+ for (cur_route = routes.head; cur_route; cur_route = cur_route->next) {
+ //bridge_data = get_bridge_data(caller_profile->destination_number, cur_route);
+ switch_caller_extension_add_application(session, extension, "bridge", cur_route->dialstring);
+ switch_safe_free(bridge_data);
+ }
+ destroy_list(&routes.head);
+ } else {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "LCR lookup failed for %s\n", caller_profile->destination_number);
+ }
+ switch_safe_free(bridge_data);
+
+ return extension;
+}
+
+void str_repeat(size_t how_many, char *what, switch_stream_handle_t *str_stream) {
+ size_t i;
+
+ //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "repeating %d of '%s'\n", how_many, what);
+
+ for (i=0; i<how_many; i++) {
+ str_stream->write_function(str_stream, "%s", what);
+ }
+}
+
+SWITCH_STANDARD_APP(lcr_app_function)
+{
+ int argc = 0;
+ char *argv[4] = { 0 };
+ char *mydata = NULL;
+ char *dest = NULL;
+ char rbuf[1024] = "";
+ char vbuf[1024] = "";
+ char *rbp = rbuf;
+ switch_size_t l = 0, rbl = sizeof(rbuf);
+ uint32_t cnt = 1;
+ switch_channel_t *channel = switch_core_session_get_channel(session);
+ char *last_delim = "|";
+ callback_t routes = { 0 };
+ lcr_route cur_route = { 0 };
+
+ if (!(mydata = switch_core_session_strdup(session, data))) {
+ return;
+ }
+
+ if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
+ dest = argv[0];
+
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "LCR Lookup on %s\n", dest);
+ routes.lookup_number = dest;
+ if (lcr_do_lookup(dest, &routes) == SWITCH_STATUS_SUCCESS) {
+ for (cur_route = routes.head; cur_route; cur_route = cur_route->next) {
+ switch_snprintf(vbuf, sizeof(vbuf), "lcr_route_%d", cnt++);
+ switch_channel_set_variable(channel, vbuf, cur_route->dialstring);
+ switch_snprintf(rbp, rbl, "%s|", cur_route->dialstring);
+ last_delim = end_of_p(rbp);
+ l = strlen(cur_route->dialstring) + 1;
+ rbp += l;
+ rbl -= l;
+ }
+ switch_snprintf(vbuf, sizeof(vbuf), "%d", cnt - 1);
+ switch_channel_set_variable(channel, "lcr_route_count", vbuf);
+ *(rbuf + strlen(rbuf) - 1) = '\0';
+ switch_channel_set_variable(channel, "lcr_auto_route", rbuf);
+ destroy_list(&routes.head);
+ } else {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "LCR lookup failed for %s\n", dest);
+ }
+ }
+}
+
+SWITCH_STANDARD_API(dialplan_lcr_function) {
+ char *argv[4] = { 0 };
+ int argc;
+ char *mydata = NULL;
+ char *dialstring = NULL;
+ char *destination_number = NULL;
+ lcr_route current = NULL;
+ max_obj_t maximum_lengths = { 0 };
+ callback_t cb_struct = { 0 };
+ //switch_malloc(maximum_lengths, sizeof(max_obj_t));
+
+ if (switch_strlen_zero(cmd)) {
+ goto usage;
+ }
+
+ mydata = switch_safe_strdup(cmd);
+
+ if ((argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
+ destination_number = strdup(argv[0]);
+ cb_struct.lookup_number = destination_number;
+
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO
+ , "data passed to lcr is [%s]\n", cmd
+ );
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO
+ , "lcr lookup returned [%d]\n"
+ , lcr_do_lookup(destination_number, &cb_struct)
+ );
+ if (cb_struct.head != NULL) {
+ size_t len;
+
+ process_max_lengths(&maximum_lengths, cb_struct.head, destination_number);
+
+ stream->write_function(stream, " | %s", headers[LCR_DIGITS_PLACE]);
+ if ((len = (maximum_lengths.digit_str - strlen(headers[LCR_DIGITS_PLACE]))) > 0) {
+ str_repeat(len, " ", stream);
+ }
+
+ stream->write_function(stream, " | %s", headers[LCR_CARRIER_PLACE]);
+ if ((len = (maximum_lengths.carrier_name - strlen(headers[LCR_CARRIER_PLACE]))) > 0) {
+ str_repeat(len, " ", stream);
+ }
+
+ stream->write_function(stream, " | %s", headers[LCR_RATE_PLACE]);
+ if ((len = (maximum_lengths.rate - strlen(headers[LCR_RATE_PLACE]))) > 0) {
+ str_repeat(len, " ", stream);
+ }
+
+ stream->write_function(stream, " | %s", headers[LCR_DIALSTRING_PLACE]);
+ if ((len = (maximum_lengths.dialstring - strlen(headers[LCR_DIALSTRING_PLACE]))) > 0) {
+ str_repeat(len, " ", stream);
+ }
+
+ stream->write_function(stream, " |\n");
+
+ current = cb_struct.head;
+ while (current) {
+
+ dialstring = get_bridge_data(destination_number, current);
+
+ stream->write_function(stream, " | %s", current->digit_str);
+ str_repeat((maximum_lengths.digit_str - current->digit_len), " ", stream);
+
+ stream->write_function(stream, " | %s", current->carrier_name );
+ str_repeat((maximum_lengths.carrier_name - strlen(current->carrier_name)), " ", stream);
+
+ stream->write_function(stream, " | %s", current->rate_str );
+ str_repeat((maximum_lengths.rate - strlen(current->rate_str)), " ", stream);
+
+ stream->write_function(stream, " | %s", dialstring);
+ str_repeat((maximum_lengths.dialstring - strlen(dialstring)), " ", stream);
+
+ stream->write_function(stream, " |\n");
+
+ switch_safe_free(dialstring);
+ current = current->next;
+ }
+
+ destroy_list(&cb_struct.head);
+ switch_safe_free(dialstring);
+ } else {
+ stream->write_function(stream, "No Routes To Display\n");
+ }
+ }
+
+ switch_safe_free(mydata);
+ return SWITCH_STATUS_SUCCESS;
+ usage:
+ stream->write_function(stream, "USAGE: %s\n", LCR_SYNTAX);
+ return SWITCH_STATUS_SUCCESS;
+}
+
+SWITCH_MODULE_LOAD_FUNCTION(mod_lcr_load) {
+ switch_api_interface_t *dialplan_lcr_api_interface;
+ switch_application_interface_t *app_interface;
+ switch_dialplan_interface_t *dp_interface;
+
+ *module_interface = switch_loadable_module_create_module_interface(pool, modname);
+
+#ifndef SWITCH_HAVE_ODBC
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "You must have ODBC support in FreeSWITCH to use this module\n");
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "\t./configure --enable-core-odbc-support\n");
+ return SWITCH_STATUS_FALSE;
+#endif
+
+ globals.pool = pool;
+
+ if (lcr_load_config() != SWITCH_STATUS_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to load lcr config file\n");
+ return SWITCH_STATUS_FALSE;
+ }
+ if (switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, globals.pool) != SWITCH_STATUS_SUCCESS) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "failed to initialize mutex\n");
+ }
+
+ SWITCH_ADD_API(dialplan_lcr_api_interface, "lcr", "Least Cost Routing Module", dialplan_lcr_function, LCR_SYNTAX);
+ SWITCH_ADD_APP(app_interface, "lcr", "Perform an LCR lookup", "Perform an LCR lookup",
+ lcr_app_function, "<number>", SAF_SUPPORT_NOMEDIA);
+ SWITCH_ADD_DIALPLAN(dp_interface, "lcr", lcr_dialplan_hunt);
+
+ /* indicate that the module should continue to be loaded */
+ return SWITCH_STATUS_SUCCESS;
+}
+
+SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_lcr_shutdown) {
+ return SWITCH_STATUS_SUCCESS;
+}
+
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
+ */
1
0
21 Nov '08
Author: mikej
Date: Fri Nov 21 17:08:12 2008
New Revision: 10507
Log:
MODLANG-90 (remove extra return added in 10472 breaking jsapi)
Modified:
freeswitch/trunk/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c
Modified: freeswitch/trunk/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c
==============================================================================
--- freeswitch/trunk/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c (original)
+++ freeswitch/trunk/src/mod/languages/mod_spidermonkey/mod_spidermonkey.c Fri Nov 21 17:08:12 2008
@@ -3510,7 +3510,6 @@
if (!(session && new_js_session(cx, javascript_global_object, session, &jss, "session", flags))) {
switch_snprintf(buf, sizeof(buf), "~var session = false;");
eval_some_js(buf, cx, javascript_global_object, &rval);
- return;
}
if (ro) {
new_request(cx, javascript_global_object, ro);
1
0