diff --git a/src/include/switch_core.h b/src/include/switch_core.h index 23e0a467d4..edcddac529 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -502,9 +502,11 @@ SWITCH_DECLARE(switch_memory_pool_t *) switch_core_session_get_pool(_In_ switch_ \param pool the pool to use for the allocation (a new one will be used if NULL) \return the newly created session */ -SWITCH_DECLARE(switch_core_session_t *) switch_core_session_request(_In_ const switch_endpoint_interface_t *endpoint_interface, - _Inout_opt_ switch_memory_pool_t **pool); +SWITCH_DECLARE(switch_core_session_t *) switch_core_session_request_uuid(_In_ const switch_endpoint_interface_t *endpoint_interface, + _Inout_opt_ switch_memory_pool_t **pool, _In_ const char *use_uuid); +#define switch_core_session_request(_ep, _p) switch_core_session_request_uuid(_ep, _p, NULL) +SWITCH_DECLARE(switch_status_t) switch_core_session_set_uuid(switch_core_session_t *session, const char *use_uuid); SWITCH_DECLARE(void) switch_core_session_perform_destroy(_Inout_ switch_core_session_t **session, _In_z_ const char *file, _In_z_ const char *func, _In_ int line); diff --git a/src/include/switch_types.h b/src/include/switch_types.h index 1390605901..7adb22add8 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -1038,6 +1038,7 @@ typedef uint32_t switch_io_flag_t; SWITCH_EVENT_CHANNEL_UNPARK - A channel has been unparked SWITCH_EVENT_CHANNEL_APPLICATION- A channel has called and event from an application SWITCH_EVENT_CHANNEL_ORIGINATE - A channel has been originated + SWITCH_EVENT_CHANNEL_UUID - A channel has changed uuid SWITCH_EVENT_API - An API call has been executed SWITCH_EVENT_LOG - A LOG event has been triggered SWITCH_EVENT_INBOUND_CHAN - A new inbound channel has been created @@ -1095,6 +1096,7 @@ typedef enum { SWITCH_EVENT_CHANNEL_UNPARK, SWITCH_EVENT_CHANNEL_APPLICATION, SWITCH_EVENT_CHANNEL_ORIGINATE, + SWITCH_EVENT_CHANNEL_UUID, SWITCH_EVENT_API, SWITCH_EVENT_LOG, SWITCH_EVENT_INBOUND_CHAN, diff --git a/src/mod/event_handlers/mod_event_socket/mod_event_socket.c b/src/mod/event_handlers/mod_event_socket/mod_event_socket.c index fc22da5047..0bd3d4674d 100644 --- a/src/mod/event_handlers/mod_event_socket/mod_event_socket.c +++ b/src/mod/event_handlers/mod_event_socket/mod_event_socket.c @@ -1250,6 +1250,7 @@ static switch_status_t parse_command(listener_t *listener, switch_event_t **even listener->event_list[SWITCH_EVENT_CHANNEL_EXECUTE_COMPLETE] = 1; listener->event_list[SWITCH_EVENT_CHANNEL_HANGUP] = 1; listener->event_list[SWITCH_EVENT_CHANNEL_ORIGINATE] = 1; + listener->event_list[SWITCH_EVENT_CHANNEL_UUID] = 1; listener->event_list[SWITCH_EVENT_CHANNEL_OUTGOING] = 1; listener->event_list[SWITCH_EVENT_CHANNEL_PARK] = 1; listener->event_list[SWITCH_EVENT_CHANNEL_PROGRESS] = 1; diff --git a/src/switch_channel.c b/src/switch_channel.c index 49ce775ee6..df242ad640 100644 --- a/src/switch_channel.c +++ b/src/switch_channel.c @@ -1150,6 +1150,7 @@ SWITCH_DECLARE(void) switch_channel_event_set_data(switch_channel_t *channel, sw if (switch_test_flag(channel, CF_VERBOSE_EVENTS) || event->event_id == SWITCH_EVENT_CHANNEL_ORIGINATE || + event->event_id == SWITCH_EVENT_CHANNEL_UUID || event->event_id == SWITCH_EVENT_CHANNEL_ANSWER || event->event_id == SWITCH_EVENT_CHANNEL_PROGRESS || event->event_id == SWITCH_EVENT_CHANNEL_PROGRESS_MEDIA || diff --git a/src/switch_core_session.c b/src/switch_core_session.c index 76a9aab689..6475af16b0 100644 --- a/src/switch_core_session.c +++ b/src/switch_core_session.c @@ -316,11 +316,21 @@ SWITCH_DECLARE(switch_call_cause_t) switch_core_session_outgoing_channel(switch_ switch_caller_profile_t *profile = NULL, *peer_profile = NULL, *cloned_profile = NULL; switch_event_t *event; switch_channel_t *peer_channel = switch_core_session_get_channel(*new_session); + const char *use_uuid; switch_assert(peer_channel); peer_profile = switch_channel_get_caller_profile(peer_channel); - + + if ((use_uuid = switch_event_get_header(var_event, "origination_uuid"))) { + if (switch_core_session_set_uuid(*new_session, use_uuid) == SWITCH_STATUS_SUCCESS) { + switch_event_del_header(var_event, "origination_uuid"); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s set UUID=%s\n", switch_channel_get_name(peer_channel), use_uuid); + } else { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "%s set UUID=%s FAILED\n", switch_channel_get_name(peer_channel), use_uuid); + } + } + if (channel) { const char *export_vars, *val; switch_codec_t *read_codec = switch_core_session_get_read_codec(session); @@ -917,8 +927,33 @@ SWITCH_DECLARE(void) switch_core_session_launch_thread(switch_core_session_t *se } -SWITCH_DECLARE(switch_core_session_t *) switch_core_session_request(const switch_endpoint_interface_t - *endpoint_interface, switch_memory_pool_t **pool) +SWITCH_DECLARE(switch_status_t) switch_core_session_set_uuid(switch_core_session_t *session, const char *use_uuid) +{ + switch_event_t *event; + + switch_assert(use_uuid); + + if (switch_core_hash_find(session_manager.session_table, use_uuid)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Duplicate UUID!\n"); + return SWITCH_STATUS_FALSE; + } + + switch_event_create(&event, SWITCH_EVENT_CHANNEL_UUID); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Old-Unique-ID", session->uuid_str); + switch_mutex_lock(runtime.throttle_mutex); + switch_core_hash_delete(session_manager.session_table, session->uuid_str); + switch_set_string(session->uuid_str, use_uuid); + switch_core_hash_insert(session_manager.session_table, session->uuid_str, session); + switch_mutex_unlock(runtime.throttle_mutex); + switch_channel_event_set_data(session->channel, event); + switch_event_fire(&event); + + + return SWITCH_STATUS_SUCCESS; +} + +SWITCH_DECLARE(switch_core_session_t *) switch_core_session_request_uuid(const switch_endpoint_interface_t + *endpoint_interface, switch_memory_pool_t **pool, const char *use_uuid) { switch_memory_pool_t *usepool; switch_core_session_t *session; @@ -926,6 +961,11 @@ SWITCH_DECLARE(switch_core_session_t *) switch_core_session_request(const switch uint32_t count = 0; int32_t sps = 0; + if (use_uuid && switch_core_hash_find(session_manager.session_table, use_uuid)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Duplicate UUID!\n"); + return NULL; + } + if (!switch_core_ready() || endpoint_interface == NULL) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "The system cannot create any sessions at this time.\n"); return NULL; @@ -957,7 +997,7 @@ SWITCH_DECLARE(switch_core_session_t *) switch_core_session_request(const switch session = switch_core_alloc(usepool, sizeof(*session)); session->pool = usepool; - + if (switch_channel_alloc(&session->channel, session->pool) != SWITCH_STATUS_SUCCESS) { abort(); } @@ -967,8 +1007,13 @@ SWITCH_DECLARE(switch_core_session_t *) switch_core_session_request(const switch /* The session *IS* the pool you may not alter it because you have no idea how its all private it will be passed to the thread run function */ - switch_uuid_get(&uuid); - switch_uuid_format(session->uuid_str, &uuid); + if (use_uuid) { + switch_set_string(session->uuid_str, use_uuid); + } else { + switch_uuid_get(&uuid); + switch_uuid_format(session->uuid_str, &uuid); + } + session->endpoint_interface = endpoint_interface; session->raw_write_frame.data = session->raw_write_buf; session->raw_write_frame.buflen = sizeof(session->raw_write_buf); diff --git a/src/switch_core_sqldb.c b/src/switch_core_sqldb.c index 06bca95859..7727e0729b 100644 --- a/src/switch_core_sqldb.c +++ b/src/switch_core_sqldb.c @@ -258,6 +258,21 @@ static void core_event_handler(switch_event_t *event) case SWITCH_EVENT_CHANNEL_DESTROY: sql = switch_mprintf("delete from channels where uuid='%q'", switch_event_get_header_nil(event, "unique-id")); break; + case SWITCH_EVENT_CHANNEL_UUID: + { + sql = switch_mprintf( + "update channels set uuid='%q' where uuid='%q';" + "update calls set caller_uuid='%q' where caller_uuid='%q';" + "update calls set callee_uuid='%q' where callee_uuid='%q'", + switch_event_get_header_nil(event, "unique-id"), + switch_event_get_header_nil(event, "old-unique-id"), + switch_event_get_header_nil(event, "unique-id"), + switch_event_get_header_nil(event, "old-unique-id"), + switch_event_get_header_nil(event, "unique-id"), + switch_event_get_header_nil(event, "old-unique-id") + ); + break; + } case SWITCH_EVENT_CHANNEL_CREATE: sql = switch_mprintf("insert into channels (uuid,created,created_epoch, name,state,dialplan,context) values('%q','%q','%ld','%q','%q','%q','%q')", switch_event_get_header_nil(event, "unique-id"), diff --git a/src/switch_event.c b/src/switch_event.c index 31e7d2e5a3..22e8ecd93f 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -125,6 +125,7 @@ static char *EVENT_NAMES[] = { "CHANNEL_UNPARK", "CHANNEL_APPLICATION", "CHANNEL_ORIGINATE", + "CHANNEL_UUID", "API", "LOG", "INBOUND_CHAN", diff --git a/src/switch_ivr_originate.c b/src/switch_ivr_originate.c index 52961857a7..8c4d19d24a 100644 --- a/src/switch_ivr_originate.c +++ b/src/switch_ivr_originate.c @@ -1043,6 +1043,19 @@ SWITCH_DECLARE(switch_status_t) switch_ivr_originate(switch_core_session_t *sess myflags |= SOF_NO_EFFECTIVE_CID_NAME; } + if (vdata && (var_begin = switch_stristr("origination_uuid=", vdata))) { + char tmp[512] = ""; + var_begin += strlen("origination_uuid="); + var_end = strchr(var_begin, '|'); + if (var_end) { + strncpy(tmp, var_begin, var_end-var_begin); + } else { + strncpy(tmp, var_begin, strlen(var_begin)); + } + new_profile->caller_id_name = switch_core_strdup(new_profile->pool, tmp); + switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "origination_uuid", tmp); + } + switch_event_add_header_string(var_event, SWITCH_STACK_BOTTOM, "originate_early_media", early_ok ? "true" : "false");