]> 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)
committerBen Ford <bford@digium.com>
Mon, 12 Aug 2024 15:21:31 +0000 (15:21 +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.

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 e3d982c6907fffe4477582d3335907e8afd22bb1..dc3e66636e6572674ff643c761d8a7dfb0c047d9 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 6b8936b2d4c5c7e5691be7f74a07ef0bd8f9353c..61c8846b8f6553098f8a3c32774273fa26f3441f 100644 (file)
                 ; AOC updates can be sent using the AOCMessage AMI action or come
                 ; from PRI channels.
                 ; (default: no)
+;
+; 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..f64a8d8
--- /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 = '2b7c507d7d12'
+
+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 2ee51657513c962d98d90b6a64767d83a50ad9d3..19c72430802521e9c63db6d031bae3702729289b 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>
@@ -565,6 +568,8 @@ static int func_channel_read(struct ast_channel *chan, const char *function,
                }
        } else if (!strcasecmp(data, "device_name")) {
                ret = ast_channel_get_device_name(chan, buf, len);
+       } 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;
@@ -737,6 +742,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 06307c91f2c7cf55e9b7f5b2c94dbba6b25e16ef..788a8797204cab21f49f287b3ff8a249df29b777 100644 (file)
@@ -317,6 +317,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 0ee66a6f2fbfe52f34a1c2a3acbfaf48eb5c1522..4747ee2c4365650edc1e049d68d89d91035e3c35 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
  *
@@ -4151,6 +4197,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 b9b87e25ff23fc8ddaaed7d5d1a43809192b5800..d4e0033f6aae20b74366f624d44547255a1c2016 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 d0e1e5922350cd555c8b828830b5fe78dba81792..dc66a40184f408b80d672a115ecf044bef3403c5 100644 (file)
@@ -987,6 +987,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 73771864a6ea7449c4a5191d0c537d8c864ebdca..5b58be52049d785311744addea10edeaf2326ad3 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 0bf319b8b7348f08854c594a9903853061078d54..ee261241b053566e35e56e648a76ea3572f282aa 100644 (file)
@@ -765,7 +765,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 */
@@ -1094,6 +1095,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;
@@ -1362,6 +1364,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) {
@@ -1370,6 +1373,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));
@@ -3166,6 +3170,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")) {
@@ -3232,6 +3240,7 @@ static const char * const cdr_readonly_vars[] = {
        "accountcode",
        "uniqueid",
        "linkedid",
+       "tenantid",
        "userfield",
        "sequence",
        NULL
@@ -3353,6 +3362,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 d41543c27a7f58c648b5f59d04bf7d4a57005ab2..98d31b551a1cfd7898f08977e98b90e4b31ee468 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 57533c82fbc19caa7216c5cfd20cd362a3895abe..4eae7fee74102aa13d051473bf7fa8695945c06a 100644 (file)
@@ -733,7 +733,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;
@@ -752,7 +752,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. */
@@ -964,7 +964,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 bc0fb4c6417dcf9bd95a84e91841e9019781e3e0..8f3227779ae969f14de76643ef06e3e81d3139f8 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 */
 };
 
 /*!
@@ -316,6 +317,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;
@@ -1350,7 +1366,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;
 
@@ -1371,6 +1388,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);
@@ -1396,6 +1427,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
@@ -1406,6 +1443,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 19355ab6db4e59ee7584daca1fc8e93e2db7070d..db9a86e5745e0cdd695d072825c3f40a98890ffe 100644 (file)
@@ -1659,6 +1659,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:
@@ -1717,12 +1718,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"
@@ -1753,6 +1759,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 344a2487a77c75415688cd9718bf55bf9417a51b..1ffd36d1bc363d1fedd5b313b29a3b6b10c252b5 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 a84f18968e1cd0b92c7e6ffa58b5d4d0cd3711e2..193f8bf438d603b483c0595b65bb341b02b6fff9 100644 (file)
@@ -285,6 +285,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;
@@ -1329,6 +1330,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 a5ba5cc768d134555d223453bb10340ae582de6f..e7773d268390e606c6918d3dd27e8282b178eb15 100644 (file)
@@ -1165,6 +1165,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 f350c8de33c8455c29c75e48244a28b6fee4ad53..39df8b48666bc676a0bbb48ba7fd7da5c710c9e1 100644 (file)
@@ -1356,6 +1356,7 @@ ari_validator ast_ari_validate_application_fn(void);
  * - name: string (required)
  * - protocol_id: string (required)
  * - state: string (required)
+ * - tenantid: string
  * Dialed
  * DialplanCEP
  * - app_data: string (required)
index 74024a45e8a183126ad8bb3f6efcd3dafa5f6376..2de4620387a151b3c0515fc2cf3960e9b14693b1 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 58a50c484a2f4a8a087892f5a5863b63b367b9f6..c83ee33179d8cba5a310f389aab320545b82b658 100644 (file)
@@ -2303,6 +2303,7 @@ int ast_res_pjsip_initialize_configuration(void)
        ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "security_mechanisms", "", security_mechanism_handler, security_mechanism_to_str, NULL, 0, 0);
        ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "security_negotiation", "no", security_negotiation_handler, security_negotiation_to_str, NULL, 0, 0);
        ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_aoc", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, send_aoc));
+       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 1b49329fcf9695cd86cb56b9d1eacbd0e4b55b72..2a1d0a0cb244a02b3c036f14df38a6c2830d4760 100644 (file)
                                "caller_rdnis": {
                                        "type": "string",
                                        "description": "The Caller ID RDNIS"
+                               },
+                               "tenantid": {
+                                       "required": false,
+                                       "type": "string",
+                                       "description": "The Tenant ID for the channel"
                                }
                        }
                }