]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
channel: Add multi-tenant identifier.
authorBen Ford <bford@digium.com>
Tue, 21 May 2024 16:11:26 +0000 (11:11 -0500)
committerAsterisk Development Team <asteriskteam@digium.com>
Thu, 5 Sep 2024 17:05:53 +0000 (17:05 +0000)
This patch introduces a new identifier for channels: tenantid. It's
a stringfield on the channel that can be used for general purposes. It
will be inherited by other channels the same way that linkedid is.

You can set tenantid in a few ways. The first is to set it in the
dialplan with the Set and CHANNEL functions:

exten => example,1,Set(CHANNEL(tenantid)=My tenant ID)

It can also be accessed via CHANNEL:

exten => example,2,NoOp(CHANNEL(tenantid))

Another method is to use the new tenantid option for pjsip endpoints in
pjsip.conf:

[my_endpoint]
type=endpoint
tenantid=My tenant ID

This is considered the best approach since you will be able to see the
tenant ID as early as the Newchannel event.

It can also be set using set_var in pjsip.conf on the endpoint like
setting other channel variable:

set_var=CHANNEL(tenantid)=My tenant ID

Note that set_var will not show tenant ID on the Newchannel event,
however.

Tenant ID has also been added to CDR. It's read-only and can be accessed
via CDR(tenantid). You can also get the tenant ID of the last channel
communicated with via CDR(peertenantid).

Tenant ID will also show up in CEL records if it has been set, and the
version number has been bumped accordingly.

Fixes: #740
UserNote: tenantid has been added to channels. It can be read in
dialplan via CHANNEL(tenantid), and it can be set using
Set(CHANNEL(tenantid)=My tenant ID). In pjsip.conf, it is recommended to
use the new tenantid option for pjsip endpoints (e.g., tenantid=My
tenant ID) so that it will show up in Newchannel events. You can set it
like any other channel variable using set_var in pjsip.conf as well, but
note that this will NOT show up in Newchannel events. Tenant ID is also
available in CDR and can be accessed with CDR(tenantid). The peer tenant
ID can also be accessed with CDR(peertenantid). CEL includes tenant ID
as well if it has been set.

UpgradeNote: A new versioned struct (ast_channel_initializers) has been
added that gets passed to __ast_channel_alloc_ap. The new function
ast_channel_alloc_with_initializers should be used when creating
channels that require the use of this struct. Currently the only value
in the struct is for tenantid, but now more fields can be added to the
struct as necessary rather than the __ast_channel_alloc_ap function. A
new option (tenantid) has been added to endpoints in pjsip.conf as well.
CEL has had its version bumped to include tenant ID.

(cherry picked from commit 9ee00e0d60dc2342b71c76fdd85b376bb00bdda6)

25 files changed:
cel/cel_manager.c
channels/chan_pjsip.c
configs/samples/pjsip.conf.sample
contrib/ast-db-manage/config/versions/655054a68ad5_add_pjsip_tenantid.py [new file with mode: 0644]
funcs/func_channel.c
include/asterisk/cdr.h
include/asterisk/cel.h
include/asterisk/channel.h
include/asterisk/channel_internal.h
include/asterisk/event_defs.h
include/asterisk/res_pjsip.h
include/asterisk/stasis_channels.h
main/cdr.c
main/cel.c
main/channel.c
main/channel_internal_api.c
main/cli.c
main/event.c
main/manager_channels.c
main/stasis_channels.c
res/ari/ari_model_validators.c
res/ari/ari_model_validators.h
res/res_pjsip/pjsip_config.xml
res/res_pjsip/pjsip_configuration.c
rest-api/api-docs/channels.json

index e485aab4961d9a4d4708b1d9c6803031dec8a921..12671bc4a3e1a49111c6711fdee1b8e16f7d4ff1 100644 (file)
@@ -229,6 +229,7 @@ static void manager_log(struct ast_event *event)
        struct ast_cel_event_record record = {
                .version = AST_CEL_EVENT_RECORD_VERSION,
        };
+       RAII_VAR(char *, tenant_id, NULL, ast_free);
 
        if (!enablecel) {
                return;
@@ -252,6 +253,10 @@ static void manager_log(struct ast_event *event)
                }
        }
 
+       if (!ast_strlen_zero(record.tenant_id)) {
+               ast_asprintf(&tenant_id, "TenantID: %s\r\n", record.tenant_id);
+       }
+
        manager_event(EVENT_FLAG_CALL, "CEL",
                "EventName: %s\r\n"
                "AccountCode: %s\r\n"
@@ -269,6 +274,7 @@ static void manager_log(struct ast_event *event)
                "AMAFlags: %s\r\n"
                "UniqueID: %s\r\n"
                "LinkedID: %s\r\n"
+               "%s"
                "Userfield: %s\r\n"
                "Peer: %s\r\n"
                "PeerAccount: %s\r\n"
@@ -290,6 +296,7 @@ static void manager_log(struct ast_event *event)
                ast_channel_amaflags2string(record.amaflag),
                record.unique_id,
                record.linked_id,
+               !ast_strlen_zero(tenant_id) ? tenant_id : "",
                record.user_field,
                record.peer,
                record.peer_account,
index fa7496241c70ba76a33064412d17a10dbea56a3b..1d8599abb4db048cfda754df4092960667dc0c78 100644 (file)
@@ -552,19 +552,23 @@ static struct ast_channel *chan_pjsip_new(struct ast_sip_session *session, int s
        struct ast_sip_channel_pvt *channel;
        struct ast_variable *var;
        struct ast_stream_topology *topology;
+       struct ast_channel_initializers initializers = {
+               .version = AST_CHANNEL_INITIALIZERS_VERSION,
+               .tenantid = session->endpoint->tenantid,
+       };
        SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
 
        if (!(pvt = ao2_alloc_options(sizeof(*pvt), chan_pjsip_pvt_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK))) {
                SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create pvt\n");
        }
 
-       chan = ast_channel_alloc_with_endpoint(1, state,
+       chan = ast_channel_alloc_with_initializers(1, state,
                S_COR(session->id.number.valid, session->id.number.str, ""),
                S_COR(session->id.name.valid, session->id.name.str, ""),
                session->endpoint->accountcode,
                exten, session->endpoint->context,
                assignedids, requestor, 0,
-               session->endpoint->persistent, "PJSIP/%s-%08x",
+               session->endpoint->persistent, &initializers, "PJSIP/%s-%08x",
                ast_sorcery_object_get_id(session->endpoint),
                (unsigned) ast_atomic_fetchadd_int((int *) &chan_idx, +1));
        if (!chan) {
@@ -664,7 +668,7 @@ static struct ast_channel *chan_pjsip_new(struct ast_sip_session *session, int s
        for (var = session->endpoint->channel_vars; var; var = var->next) {
                char buf[512];
                pbx_builtin_setvar_helper(chan, var->name, ast_get_encoded_str(
-                                                 var->value, buf, sizeof(buf)));
+                                       var->value, buf, sizeof(buf)));
        }
 
        ast_channel_stage_snapshot_done(chan);
index bc34831bfae578803107480bb8d1732849ada684..9cb233367abac456b4c10775acdc840b150248fd 100644 (file)
                 ; This geolocation profile will be applied to all calls received
                 ; by the channel driver from the dialplan before they're forwarded
                 ; the remote endpoint.
-;
+; tenantid =
+                ; Sets the tenant ID for this endpoint. It can be read in dialplan
+                ; with the CHANNEL function, and it can be changed later via dialplan
+                ; using the same CHANNEL function if needed. Setting tenant ID here
+                ; will cause it to show up on channel creation and the initial
+                ; channel snapshot.
 
 
 ;==========================AUTH SECTION OPTIONS=========================
diff --git a/contrib/ast-db-manage/config/versions/655054a68ad5_add_pjsip_tenantid.py b/contrib/ast-db-manage/config/versions/655054a68ad5_add_pjsip_tenantid.py
new file mode 100644 (file)
index 0000000..068da8f
--- /dev/null
@@ -0,0 +1,22 @@
+"""add pjsip tenantid
+
+Revision ID: 655054a68ad5
+Revises: bd9c5159c7ea
+Create Date: 2024-06-11 11:18:41.466929
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = '655054a68ad5'
+down_revision = '9f3692b1654b'
+
+from alembic import op
+import sqlalchemy as sa
+
+
+def upgrade():
+    op.add_column('ps_endpoints', sa.Column('tenantid', sa.String(80)))
+
+
+def downgrade():
+    op.drop_column('ps_endpoints', 'tenantid')
index 312806d9668c14f5851ec967f5efdd216be7b8e0..1545f40d522d0bcc0049a8dcfda9f791413cc512 100644 (file)
                                        <enum name="linkedid">
                                                <para>R/O returns the linkedid if available, otherwise returns the uniqueid.</para>
                                        </enum>
+                                       <enum name="tenantid">
+                                               <para>R/W The channel tenantid.</para>
+                                       </enum>
                                        <enum name="max_forwards">
                                                <para>R/W The maximum number of forwards allowed.</para>
                                        </enum>
@@ -473,6 +476,8 @@ static int func_channel_read(struct ast_channel *chan, const char *function,
                        ast_callid_strnprint(buf, len, callid);
                }
                ast_channel_unlock(chan);
+       } else if (!strcasecmp(data, "tenantid")) {
+               locked_copy_string(chan, buf, ast_channel_tenantid(chan), len);
        } else if (!ast_channel_tech(chan) || !ast_channel_tech(chan)->func_channel_read || ast_channel_tech(chan)->func_channel_read(chan, function, data, buf, len)) {
                ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
                ret = -1;
@@ -628,6 +633,8 @@ static int func_channel_write_real(struct ast_channel *chan, const char *functio
                        ret = ast_max_forwards_set(chan, max_forwards);
                        ast_channel_unlock(chan);
                }
+       } else if (!strcasecmp(data, "tenantid")) {
+               ast_channel_tenantid_set(chan, value);
        } else if (!ast_channel_tech(chan)->func_channel_write
                 || ast_channel_tech(chan)->func_channel_write(chan, function, data, value)) {
                ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
index e86c6e1d46752f6c8017e2f76bdf4441d515bc1d..df0e98185467399805ea1b35aec8ef19a87ec10f 100644 (file)
@@ -314,6 +314,10 @@ struct ast_cdr {
        char uniqueid[AST_MAX_UNIQUEID];
        /*! Linked group Identifier */
        char linkedid[AST_MAX_UNIQUEID];
+       /*! Channel tenant Identifier */
+       char tenantid[AST_MAX_TENANT_ID];
+       /*! Channel tenant Identifier of the last person we talked to */
+       char peertenantid[AST_MAX_TENANT_ID];
        /*! User field */
        char userfield[AST_MAX_USER_FIELD];
        /*! Sequence field */
index 81f375be77d1b639db586ebdffa8cef27989953e..7444938ce69dc9e3c54dd2e8d23c22623137244c 100644 (file)
@@ -140,7 +140,7 @@ struct ast_cel_event_record {
         * \brief struct ABI version
         * \note This \b must be incremented when the struct changes.
         */
-       #define AST_CEL_EVENT_RECORD_VERSION 2
+       #define AST_CEL_EVENT_RECORD_VERSION 3
        /*!
         * \brief struct ABI version
         * \note This \b must stay as the first member.
@@ -164,6 +164,7 @@ struct ast_cel_event_record {
        const char *peer_account;
        const char *unique_id;
        const char *linked_id;
+       const char *tenant_id;
        uint amaflag;
        const char *user_field;
        const char *peer;
index bc12de8c90270be292d41dd9783312573b26dfa1..0ce0713211be72d1c3929d20f8dd515c12a67a35 100644 (file)
@@ -146,6 +146,8 @@ extern "C" {
  */
 #define AST_MAX_PUBLIC_UNIQUEID 149
 
+#define AST_MAX_TENANT_ID 64 /*!< Max length of a channel tenant_id */
+
 /*!
  * The number of buckets to store channels or channel information
  */
@@ -606,6 +608,24 @@ struct ast_assigned_ids {
        const char *uniqueid2;
 };
 
+/*!
+ * \brief Helper struct for initializing additional channel information on channel creation.
+ * \since 18.25.0
+ */
+struct ast_channel_initializers {
+       /*!
+        * \brief struct ABI version
+        * \note This \b must be incremented when the struct changes.
+        */
+       #define AST_CHANNEL_INITIALIZERS_VERSION 1
+       /*!
+        * \brief struct ABI version
+        * \note This \b must stay as the first member.
+        */
+       uint32_t version;
+       const char *tenantid;
+};
+
 /*!
  * \brief Forward declaration
  */
@@ -1244,6 +1264,27 @@ struct ast_channel * __attribute__((format(printf, 15, 16)))
                const char *file, int line, const char *function,
                const char *name_fmt, ...);
 
+/*!
+ * \brief Create a channel structure
+ * \since 18.25.0
+ *
+ * \retval NULL failure
+ * \retval non-NULL successfully allocated channel
+ *
+ * \note Absolutely _NO_ channel locks should be held before calling this function.
+ * \note By default, new channels are set to the "s" extension
+ *       and "default" context.
+ * \note Same as __ast_channel_alloc but with ast_channel_initializers struct.
+ */
+struct ast_channel * __attribute__((format(printf, 16, 17)))
+       __ast_channel_alloc_with_initializers(int needqueue, int state, const char *cid_num,
+               const char *cid_name, const char *acctcode,
+               const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
+               const struct ast_channel *requestor, enum ama_flags amaflag,
+               struct ast_endpoint *endpoint, struct ast_channel_initializers *initializers,
+               const char *file, int line, const char *function,
+               const char *name_fmt, ...);
+
 /*!
  * \brief Create a channel structure
  *
@@ -1263,6 +1304,11 @@ struct ast_channel * __attribute__((format(printf, 15, 16)))
        __ast_channel_alloc((needqueue), (state), (cid_num), (cid_name), (acctcode), (exten), (context), (assignedids), (requestor), (amaflag), (endpoint), \
                __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
 
+#define ast_channel_alloc_with_initializers(needqueue, state, cid_num, cid_name, acctcode, exten, context, assignedids, requestor, amaflag, endpoint, initializers, ...) \
+       __ast_channel_alloc_with_initializers((needqueue), (state), (cid_num), (cid_name), (acctcode), (exten), (context), (assignedids), (requestor), (amaflag), (endpoint), \
+               (initializers), __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
+
+
 /*!
  * \brief Create a fake channel structure
  *
@@ -4121,6 +4167,8 @@ const char *ast_channel_userfield(const struct ast_channel *chan);
 const char *ast_channel_call_forward(const struct ast_channel *chan);
 const char *ast_channel_uniqueid(const struct ast_channel *chan);
 const char *ast_channel_linkedid(const struct ast_channel *chan);
+const char *ast_channel_tenantid(const struct ast_channel *chan);
+void ast_channel_tenantid_set(struct ast_channel *chan, const char *value);
 const char *ast_channel_parkinglot(const struct ast_channel *chan);
 const char *ast_channel_hangupsource(const struct ast_channel *chan);
 const char *ast_channel_dialcontext(const struct ast_channel *chan);
index 774c9b03c1540e7a19974b6ad63ac55886a9af42..1b994fa9b4a5f2fc12cf252829ca12bab6f43554 100644 (file)
@@ -23,6 +23,8 @@
 
 #define ast_channel_internal_alloc(destructor, assignedid, requestor) __ast_channel_internal_alloc(destructor, assignedid, requestor, __FILE__, __LINE__, __PRETTY_FUNCTION__)
 struct ast_channel *__ast_channel_internal_alloc(void (*destructor)(void *obj), const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *file, int line, const char *function);
+struct ast_channel *__ast_channel_internal_alloc_with_initializers(void (*destructor)(void *obj), const struct ast_assigned_ids *assignedids,
+       const struct ast_channel *requestor, const struct ast_channel_initializers *initializers, const char *file, int line, const char *function);
 void ast_channel_internal_finalize(struct ast_channel *chan);
 int ast_channel_internal_is_finalized(struct ast_channel *chan);
 void ast_channel_internal_cleanup(struct ast_channel *chan);
index fea89ef806d3a8a9ddd73ae13a469a9356f1d3e1..85214ada0c365ee57e53c3b14fffad49fd65ee95 100644 (file)
@@ -311,8 +311,15 @@ enum ast_event_ie_type {
         * Payload type: UINT
         */
        AST_EVENT_IE_NODE_ID             = 0x003e,
+
+       /*!
+        * \brief Channel Event TenantID
+        * Used by: AST_EVENT_CEL
+        * Payload type: STR
+        */
+       AST_EVENT_IE_CEL_TENANTID       = 0x003f,
        /*! \brief Must be the last IE value +1 */
-       AST_EVENT_IE_TOTAL               = 0x003f,
+       AST_EVENT_IE_TOTAL               = 0x0040,
 };
 
 /*!
index aca1e24178126f4b14392c86a215d3d732c8d0f8..a12ef3c3dd1883198989a8af5885470b694f4673 100644 (file)
@@ -911,6 +911,8 @@ struct ast_sip_endpoint {
                AST_STRING_FIELD(incoming_mwi_mailbox);
                /*! STIR/SHAKEN profile to use */
                AST_STRING_FIELD(stir_shaken_profile);
+               /*! Tenant ID for the endpoint */
+               AST_STRING_FIELD(tenantid);
        );
        /*! Configuration for extensions */
        struct ast_sip_endpoint_extensions extensions;
index f3452287177d643789615ebf59252bea72e76725..2bf673c55ae71d6afb15ffb0450524ac3cee9eca 100644 (file)
@@ -109,6 +109,7 @@ struct ast_channel_snapshot_base {
                AST_STRING_FIELD(userfield);   /*!< Userfield for CEL billing */
                AST_STRING_FIELD(language);    /*!< The default spoken language for the channel */
                AST_STRING_FIELD(type);        /*!< Type of channel technology */
+               AST_STRING_FIELD(tenantid);    /*!< Channel tenant identifier */
        );
        struct timeval creationtime; /*!< The time of channel creation */
        int tech_properties;         /*!< Properties of the channel's technology */
index 0b4bed177bbb21f94e164c6b78cdd65c2b4054f4..359d3797006b78fd3d6beddb7290cd1a72001b39 100644 (file)
@@ -723,7 +723,8 @@ struct cdr_object {
        struct ast_flags flags;                 /*!< Flags on the CDR */
        AST_DECLARE_STRING_FIELDS(
                AST_STRING_FIELD(linkedid);         /*!< Linked ID. Cached here as it may change out from party A, which must be immutable */
-               AST_STRING_FIELD(uniqueid);                     /*!< Unique id of party A. Cached here as it is the master CDR container key */
+               AST_STRING_FIELD(uniqueid);         /*!< Unique id of party A. Cached here as it is the master CDR container key */
+               AST_STRING_FIELD(tenantid);         /*!< Tenant ID. Cached here because the value can be manipulated through dialplan */
                AST_STRING_FIELD(name);             /*!< Channel name of party A. Cached here as the party A address may change */
                AST_STRING_FIELD(bridge);           /*!< The bridge the party A happens to be in. */
                AST_STRING_FIELD(appl);             /*!< The last accepted application party A was in */
@@ -1054,6 +1055,7 @@ static struct cdr_object *cdr_object_alloc(struct ast_channel_snapshot *chan, co
        ast_string_field_set(cdr, uniqueid, chan->base->uniqueid);
        ast_string_field_set(cdr, name, chan->base->name);
        ast_string_field_set(cdr, linkedid, chan->peer->linkedid);
+       ast_string_field_set(cdr, tenantid, chan->base->tenantid);
        cdr->disposition = AST_CDR_NULL;
        cdr->sequence = ast_atomic_fetchadd_int(&global_cdr_sequence, +1);
        cdr->lastevent = *event_time;
@@ -1323,6 +1325,7 @@ static struct ast_cdr *cdr_object_create_public_records(struct cdr_object *cdr)
                ast_copy_string(cdr_copy->lastdata, it_cdr->data, sizeof(cdr_copy->lastdata));
                ast_copy_string(cdr_copy->dst, it_cdr->exten, sizeof(cdr_copy->dst));
                ast_copy_string(cdr_copy->dcontext, it_cdr->context, sizeof(cdr_copy->dcontext));
+               ast_copy_string(cdr_copy->tenantid, it_cdr->tenantid, sizeof(cdr_copy->tenantid));
 
                /* Party B */
                if (party_b) {
@@ -1331,6 +1334,7 @@ static struct ast_cdr *cdr_object_create_public_records(struct cdr_object *cdr)
                        if (!ast_strlen_zero(it_cdr->party_b.userfield)) {
                                snprintf(cdr_copy->userfield, sizeof(cdr_copy->userfield), "%s;%s", it_cdr->party_a.userfield, it_cdr->party_b.userfield);
                        }
+                       ast_copy_string(cdr_copy->peertenantid, party_b->base->tenantid, sizeof(cdr_copy->peertenantid));
                }
                if (ast_strlen_zero(cdr_copy->userfield) && !ast_strlen_zero(it_cdr->party_a.userfield)) {
                        ast_copy_string(cdr_copy->userfield, it_cdr->party_a.userfield, sizeof(cdr_copy->userfield));
@@ -3104,6 +3108,10 @@ void ast_cdr_format_var(struct ast_cdr *cdr, const char *name, char **ret, char
                ast_copy_string(workspace, cdr->uniqueid, workspacelen);
        } else if (!strcasecmp(name, "linkedid")) {
                ast_copy_string(workspace, cdr->linkedid, workspacelen);
+       } else if (!strcasecmp(name, "tenantid")) {
+               ast_copy_string(workspace, cdr->tenantid, workspacelen);
+       } else if (!strcasecmp(name, "peertenantid")) {
+               ast_copy_string(workspace, cdr->peertenantid, workspacelen);
        } else if (!strcasecmp(name, "userfield")) {
                ast_copy_string(workspace, cdr->userfield, workspacelen);
        } else if (!strcasecmp(name, "sequence")) {
@@ -3170,6 +3178,7 @@ static const char * const cdr_readonly_vars[] = {
        "accountcode",
        "uniqueid",
        "linkedid",
+       "tenantid",
        "userfield",
        "sequence",
        NULL
@@ -3291,6 +3300,14 @@ static int cdr_object_format_property(struct cdr_object *cdr_obj, const char *na
                ast_copy_string(value, party_a->base->uniqueid, length);
        } else if (!strcasecmp(name, "linkedid")) {
                ast_copy_string(value, cdr_obj->linkedid, length);
+       } else if (!strcasecmp(name, "tenantid")) {
+               ast_copy_string(value, party_a->base->tenantid, length);
+       } else if (!strcasecmp(name, "peertenantid")) {
+               if (party_b) {
+                       ast_copy_string(value, party_b->base->tenantid, length);
+               } else {
+                       ast_copy_string(value, "", length);
+               }
        } else if (!strcasecmp(name, "userfield")) {
                ast_copy_string(value, cdr_obj->party_a.userfield, length);
        } else if (!strcasecmp(name, "sequence")) {
index 82cbf45f694830c3a51c4426d26a5d76d08b4687..6f14804d301d942763c2bc3218bd33dd3b1ac8b2 100644 (file)
@@ -554,6 +554,7 @@ struct ast_event *ast_cel_create_event_with_time(struct ast_channel_snapshot *sn
                AST_EVENT_IE_CEL_PEERACCT, AST_EVENT_IE_PLTYPE_STR, snapshot->peer->account,
                AST_EVENT_IE_CEL_UNIQUEID, AST_EVENT_IE_PLTYPE_STR, snapshot->base->uniqueid,
                AST_EVENT_IE_CEL_LINKEDID, AST_EVENT_IE_PLTYPE_STR, snapshot->peer->linkedid,
+               AST_EVENT_IE_CEL_TENANTID, AST_EVENT_IE_PLTYPE_STR, snapshot->base->tenantid,
                AST_EVENT_IE_CEL_USERFIELD, AST_EVENT_IE_PLTYPE_STR, snapshot->base->userfield,
                AST_EVENT_IE_CEL_EXTRA, AST_EVENT_IE_PLTYPE_STR, S_OR(extra_txt, ""),
                AST_EVENT_IE_CEL_PEER, AST_EVENT_IE_PLTYPE_STR, S_OR(peer, ""),
@@ -853,6 +854,7 @@ int ast_cel_fill_record(const struct ast_event *e, struct ast_cel_event_record *
        r->peer_account     = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_PEERACCT), "");
        r->unique_id        = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_UNIQUEID), "");
        r->linked_id        = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_LINKEDID), "");
+       r->tenant_id        = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_TENANTID), "");
        r->amaflag          = ast_event_get_ie_uint(e, AST_EVENT_IE_CEL_AMAFLAGS);
        r->user_field       = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_USERFIELD), "");
        r->peer             = S_OR(ast_event_get_ie_str(e, AST_EVENT_IE_CEL_PEER), "");
index 546f4f84e6e029e8f6256c134702c2aa6ca6d3aa..4abc1e07a5255e18e500a2989550e0e42f0bdd24 100644 (file)
@@ -729,7 +729,7 @@ static struct ast_channel *__attribute__((format(printf, 15, 0)))
 __ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name,
                       const char *acctcode, const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
                       const struct ast_channel *requestor, enum ama_flags amaflag, struct ast_endpoint *endpoint,
-                      const char *file, int line,
+                      struct ast_channel_initializers *initializers, const char *file, int line,
                       const char *function, const char *name_fmt, va_list ap)
 {
        struct ast_channel *tmp;
@@ -748,7 +748,7 @@ __ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char
                return NULL;
        }
 
-       tmp = __ast_channel_internal_alloc(ast_channel_destructor, assignedids, requestor,
+       tmp = __ast_channel_internal_alloc_with_initializers(ast_channel_destructor, assignedids, requestor, initializers,
                file, line, function);
        if (!tmp) {
                /* Channel structure allocation failure. */
@@ -960,7 +960,26 @@ struct ast_channel *__ast_channel_alloc(int needqueue, int state, const char *ci
 
        va_start(ap, name_fmt);
        result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
-                                       assignedids, requestor, amaflag, endpoint, file, line, function, name_fmt, ap);
+                                       assignedids, requestor, amaflag, endpoint, NULL, file, line, function, name_fmt, ap);
+       va_end(ap);
+
+       return result;
+}
+
+struct ast_channel *__ast_channel_alloc_with_initializers(int needqueue, int state, const char *cid_num,
+                                       const char *cid_name, const char *acctcode,
+                                       const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
+                                       const struct ast_channel *requestor, enum ama_flags amaflag,
+                                       struct ast_endpoint *endpoint, struct ast_channel_initializers *initializers,
+                                       const char *file, int line, const char *function,
+                                       const char *name_fmt, ...)
+{
+       va_list ap;
+       struct ast_channel *result;
+
+       va_start(ap, name_fmt);
+       result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
+                                       assignedids, requestor, amaflag, endpoint, initializers, file, line, function, name_fmt, ap);
        va_end(ap);
 
        return result;
index a50d60096c078e3a84d903586e4d4c75ad6a2aa0..9a7aff89d128bf867240d8dad51c25191a2f4dc1 100644 (file)
@@ -57,6 +57,7 @@ struct ast_channel_id {
        time_t creation_time;                           /*!< Creation time */
        int creation_unique;                            /*!< sub-second unique value */
        char unique_id[AST_MAX_UNIQUEID];       /*!< Unique Identifier */
+       char tenant_id[AST_MAX_TENANT_ID];      /*!< Multi-tenant identifier */
 };
 
 /*!
@@ -314,6 +315,21 @@ const char *ast_channel_linkedid(const struct ast_channel *chan)
        return chan->linkedid.unique_id;
 }
 
+const char *ast_channel_tenantid(const struct ast_channel *chan)
+{
+       /* It's ok for tenantid to be empty, so no need to assert */
+       return chan->linkedid.tenant_id;
+}
+
+void ast_channel_tenantid_set(struct ast_channel *chan, const char *value)
+{
+       if (ast_strlen_zero(value)) {
+               return;
+       }
+       ast_copy_string(chan->linkedid.tenant_id, value, sizeof(chan->linkedid.tenant_id));
+       ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_BASE);
+}
+
 const char *ast_channel_appl(const struct ast_channel *chan)
 {
        return chan->appl;
@@ -1328,7 +1344,8 @@ static int pvt_cause_cmp_fn(void *obj, void *vstr, int flags)
 
 #define DIALED_CAUSES_BUCKETS 37
 
-struct ast_channel *__ast_channel_internal_alloc(void (*destructor)(void *obj), const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *file, int line, const char *function)
+struct ast_channel *__ast_channel_internal_alloc_with_initializers(void (*destructor)(void *obj), const struct ast_assigned_ids *assignedids,
+       const struct ast_channel *requestor, const struct ast_channel_initializers *initializers, const char *file, int line, const char *function)
 {
        struct ast_channel *tmp;
 
@@ -1349,6 +1366,20 @@ struct ast_channel *__ast_channel_internal_alloc(void (*destructor)(void *obj),
                return ast_channel_unref(tmp);
        }
 
+       /* Check initializers validity here for early abort. Unfortunately, we can't do much here because
+        * tenant ID is part of linked ID, which would overwrite it further down. */
+       if (initializers) {
+               if (initializers->version == 0) {
+                       ast_log(LOG_ERROR, "Channel initializers must have a non-zero version.\n");
+                       return ast_channel_unref(tmp);
+               } else if (initializers->version != AST_CHANNEL_INITIALIZERS_VERSION) {
+                       ast_log(LOG_ERROR, "ABI mismatch for ast_channel_initializers. "
+                               "Please ensure all modules were compiled for "
+                               "this version of Asterisk.\n");
+                       return ast_channel_unref(tmp);
+               }
+       }
+
        /* set the creation time in the uniqueid */
        tmp->uniqueid.creation_time = time(NULL);
        tmp->uniqueid.creation_unique = ast_atomic_fetchadd_int(&uniqueint, 1);
@@ -1374,6 +1405,12 @@ struct ast_channel *__ast_channel_internal_alloc(void (*destructor)(void *obj),
                tmp->linkedid = tmp->uniqueid;
        }
 
+       /* Things like tenant ID need to be set here, otherwise they would be overwritten by
+        * things like inheriting linked ID above. */
+       if (initializers) {
+               ast_copy_string(tmp->linkedid.tenant_id, initializers->tenantid, sizeof(tmp->linkedid.tenant_id));
+       }
+
        AST_VECTOR_INIT(&tmp->fds, AST_MAX_FDS);
 
        /* Force all channel snapshot segments to be created on first use, so we don't have to check if
@@ -1384,6 +1421,12 @@ struct ast_channel *__ast_channel_internal_alloc(void (*destructor)(void *obj),
        return tmp;
 }
 
+struct ast_channel *__ast_channel_internal_alloc(void (*destructor)(void *obj), const struct ast_assigned_ids *assignedids,
+       const struct ast_channel *requestor, const char *file, int line, const char *function)
+{
+       return __ast_channel_internal_alloc_with_initializers(destructor, assignedids, requestor, NULL, file, line, function);
+}
+
 struct ast_channel *ast_channel_internal_oldest_linkedid(struct ast_channel *a, struct ast_channel *b)
 {
        ast_assert(a->linkedid.creation_time != 0);
index 7e1359ec031ccd0a9efcaca951b9d54137bbea3d..6c8d5c0c86952f5f49becb349aa05fcd6be95e30 100644 (file)
@@ -1624,6 +1624,7 @@ static char *handle_showchan(struct ast_cli_entry *e, int cmd, struct ast_cli_ar
        ast_callid callid;
        char callid_buf[32];
        int stream_num;
+       RAII_VAR(char *, tenant_id, NULL, ast_free);
 
        switch (cmd) {
        case CLI_INIT:
@@ -1682,12 +1683,17 @@ static char *handle_showchan(struct ast_cli_entry *e, int cmd, struct ast_cli_ar
                ast_callid_strnprint(callid_buf, sizeof(callid_buf), callid);
        }
 
+       if (!ast_strlen_zero(ast_channel_tenantid(chan))) {
+               ast_asprintf(&tenant_id, "       TenantID: %s\n", ast_channel_tenantid(chan));
+       }
+
        ast_str_append(&output, 0,
                " -- General --\n"
                "           Name: %s\n"
                "           Type: %s\n"
                "       UniqueID: %s\n"
                "       LinkedID: %s\n"
+               "%s"
                "      Caller ID: %s\n"
                " Caller ID Name: %s\n"
                "Connected Line ID: %s\n"
@@ -1718,6 +1724,7 @@ static char *handle_showchan(struct ast_cli_entry *e, int cmd, struct ast_cli_ar
                ast_channel_tech(chan)->type,
                ast_channel_uniqueid(chan),
                ast_channel_linkedid(chan),
+               !ast_strlen_zero(tenant_id) ? tenant_id : "",
                S_COR(ast_channel_caller(chan)->id.number.valid,
                      ast_channel_caller(chan)->id.number.str, "(N/A)"),
                S_COR(ast_channel_caller(chan)->id.name.valid,
index 2b73cd7385d44314f8871dec432741484e2dcdfb..7c8af9147507a20fa88db33359f782f4ab5afe13 100644 (file)
@@ -189,6 +189,7 @@ static const struct ie_map {
        [AST_EVENT_IE_PRESENCE_STATE]      = { AST_EVENT_IE_PLTYPE_UINT, "PresenceState" },
        [AST_EVENT_IE_PRESENCE_SUBTYPE]    = { AST_EVENT_IE_PLTYPE_STR,  "PresenceSubtype" },
        [AST_EVENT_IE_PRESENCE_MESSAGE]    = { AST_EVENT_IE_PLTYPE_STR,  "PresenceMessage" },
+       [AST_EVENT_IE_CEL_TENANTID]        = { AST_EVENT_IE_PLTYPE_STR, "TenantID" },
 };
 
 const char *ast_event_get_type_name(const struct ast_event *event)
index cf8b335e5142be4c452abf8971f8060d9e6a1cab..9e18f1556df89c61a03b234934574d34ce91b927 100644 (file)
@@ -543,6 +543,10 @@ struct ast_str *ast_manager_build_channel_state_string_prefix(
                return NULL;
        }
 
+       if (!ast_strlen_zero(snapshot->base->tenantid)) {
+               ast_str_append(&out, 0, "%sTenantid: %s\r\n", prefix, snapshot->base->tenantid);
+       }
+
        if (snapshot->manager_vars) {
                struct ast_var_t *var;
                char *val;
index 5e39c0765411b277ce40cbd563f1e0855067d80b..1f9c0f5b830929ff1a53470778a889da545f8b6f 100644 (file)
@@ -284,6 +284,7 @@ static struct ast_channel_snapshot_base *channel_snapshot_base_create(struct ast
        ast_string_field_set(snapshot, userfield, ast_channel_userfield(chan));
        ast_string_field_set(snapshot, uniqueid, ast_channel_uniqueid(chan));
        ast_string_field_set(snapshot, language, ast_channel_language(chan));
+       ast_string_field_set(snapshot, tenantid, ast_channel_tenantid(chan));
 
        snapshot->creationtime = ast_channel_creationtime(chan);
        snapshot->tech_properties = ast_channel_tech(chan)->properties;
@@ -1291,6 +1292,10 @@ struct ast_json *ast_channel_snapshot_to_json(
                ast_json_object_set(json_chan, "channelvars", ast_json_channel_vars(snapshot->ari_vars));
        }
 
+        if (!ast_strlen_zero(snapshot->base->tenantid)) {
+                ast_json_object_set(json_chan, "tenantid", ast_json_string_create(snapshot->base->tenantid));
+        }
+
        return json_chan;
 }
 
index 0bbbb195e2be4d8212f9d451acb1e0c58a7e0272..7caf60542a9622a73e6b3086c198d82ab2306fd3 100644 (file)
@@ -1145,6 +1145,15 @@ int ast_ari_validate_channel(struct ast_json *json)
                                res = 0;
                        }
                } else
+               if (strcmp("tenantid", ast_json_object_iter_key(iter)) == 0) {
+                       int prop_is_valid;
+                       prop_is_valid = ast_ari_validate_string(
+                               ast_json_object_iter_value(iter));
+                       if (!prop_is_valid) {
+                               ast_log(LOG_ERROR, "ARI Channel field tenantid failed validation\n");
+                               res = 0;
+                       }
+               } else
                {
                        ast_log(LOG_ERROR,
                                "ARI Channel has undocumented field %s\n",
index 583cba33a4e7cfb34e0c5a708a32c491c86eb0af..9ba8e3245c346b84c33415598f9a973cd3e41c84 100644 (file)
@@ -1354,6 +1354,7 @@ ari_validator ast_ari_validate_application_fn(void);
  * - language: string (required)
  * - name: string (required)
  * - state: string (required)
+ * - tenantid: string
  * Dialed
  * DialplanCEP
  * - app_data: string (required)
index 6eaa399c32c7740489887d9d7d502fc38999633c..92d5631a80c72d07f89905fb93e9c9498c424a43 100644 (file)
                                                dialplan application such as <emphasis>Ringing</emphasis>.</para>
                                        </description>
                                </configOption>
+                               <configOption name="tenantid" default="">
+                                       <synopsis>The tenant ID for this endpoint.</synopsis>
+                                       <description><para>
+                                               Sets the tenant ID for this endpoint. When a channel is created,
+                                               tenantid will be set to this value. It can be changed via dialplan
+                                               later if needed.
+                                       </para></description>
+                               </configOption>
                                <configOption name="timers_min_se" default="90">
                                        <synopsis>Minimum session timers expiration period</synopsis>
                                        <description><para>
index bc938b1e23c618a2bbf7cee77bdcde7b717f6a36..d895cd7104522b289fb9fcb183e6c575c32d3f57 100644 (file)
@@ -2227,6 +2227,7 @@ int ast_res_pjsip_initialize_configuration(void)
        ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow_unauthenticated_options", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, allow_unauthenticated_options));
        ast_sorcery_object_field_register(sip_sorcery, "endpoint", "geoloc_incoming_call_profile", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, geoloc_incoming_call_profile));
        ast_sorcery_object_field_register(sip_sorcery, "endpoint", "geoloc_outgoing_call_profile", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, geoloc_outgoing_call_profile));
+       ast_sorcery_object_field_register(sip_sorcery, "endpoint", "tenantid", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, tenantid));
 
        if (ast_sip_initialize_sorcery_transport()) {
                ast_log(LOG_ERROR, "Failed to register SIP transport support with sorcery\n");
index 04d1577337f3213efd9e4ff82ff9d73e39566ec8..8d878a13dc55f936276fc184141370cd7baba83e 100644 (file)
                                        "required": false,
                                        "type": "object",
                                        "description": "Channel variables"
+                               },
+                               "tenantid": {
+                                       "required": false,
+                                       "type": "string",
+                                       "description": "The Tenant ID for the channel"
                                }
                        }
                }