Hey there,
I needed the ability to specify the userpart of the "From" header when
making calls.
Since PJSUA automatically uses the associated account ID for that, it
wasn't possible.
I added a new field to pjsua_call_setting which allows the user to set a
custom From user.
When pjsua_call_make_call() is called, only the userpart from the
account ID will be changed and used for the dialog.
This is especially useful when you need to make calls on behalf of
someone else and want to carry their number/identity along
which is almost always the case when writing session border controllers
or some kind of B2B gateways.
I'm providing the patch here in case somebody else needs something like
this.
@Teluu:
Is there a chance that this feature gets into upstream?
Are there others reading here who might find this feature useful?
Best Regards,
Andreas Wehrmann
diff -uNr pjproject-2.9-orig/pjsip/include/pjsua-lib/pjsua.h pjproject-2.9/pjsip/include/pjsua-lib/pjsua.h
--- pjproject-2.9-orig/pjsip/include/pjsua-lib/pjsua.h 2020-02-21 11:30:59.545909356 +0100
+++ pjproject-2.9/pjsip/include/pjsua-lib/pjsua.h 2020-02-21 11:34:08.945510607 +0100
@@ -880,6 +880,14 @@
*/
unsigned vid_cnt;
+ /**
+ * When initially making a call, this can be used to set the user part
+ * of the From header in the outgoing INVITE.
+ *
+ * Default: Use account id
+ */
+ pj_str_t from_user;
+
} pjsua_call_setting;
diff -uNr pjproject-2.9-orig/pjsip/src/pjsua-lib/pjsua_call.c pjproject-2.9/pjsip/src/pjsua-lib/pjsua_call.c
--- pjproject-2.9-orig/pjsip/src/pjsua-lib/pjsua_call.c 2020-02-21 11:30:59.553909341 +0100
+++ pjproject-2.9/pjsip/src/pjsua-lib/pjsua_call.c 2020-02-21 11:31:49.613815349 +0100
@@ -792,8 +792,13 @@
pjsua_acc *acc;
pjsua_call *call;
int call_id = -1;
+ pjsip_sip_uri* from_uri;
+ pj_str_t from_user;
+ pj_str_t opt_from_user;
pj_str_t contact;
pj_status_t status;
+ char new_from_uri[ PJSIP_MAX_URL_SIZE ];
+ int len;
/* Check that account is valid */
PJ_ASSERT_RETURN(acc_id>=0 || acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
@@ -901,10 +906,53 @@
goto on_error;
}
}
+
+ /* Build From header (if any)
+ */
+ pj_strassign(&from_user, &acc->cfg.id);
+ if(opt) {
+ if(pj_strlen(&opt->from_user) > 0) {
+ pj_strdup_with_null(tmp_pool, &opt_from_user, &acc->cfg.id);
+ pjsip_name_addr* na = (pjsip_name_addr*)pjsip_parse_uri(tmp_pool,
+ (char*)pj_strbuf( &opt_from_user ),
+ pj_strlen( &opt_from_user ),
+ PJSIP_PARSE_URI_AS_NAMEADDR );
+ if (! na) {
+ status = PJSIP_EINVALIDREQURI;
+ pjsua_perror(THIS_FILE, "Failed to parse account ID",
+ status);
+ goto on_error;
+ }
+
+ from_uri = pjsip_uri_get_uri(na);
+ if(! from_uri) {
+ status = PJSIP_EINVALIDREQURI;
+ pjsua_perror(THIS_FILE, "Failed to get account URI",
+ status);
+ goto on_error;
+ }
+
+ pj_strassign( &from_uri->user,
+ (pj_str_t*)&opt->from_user );
+
+ /* From user changed, need to print the entire thing now
+ */
+ len = pjsip_uri_print( PJSIP_URI_IN_FROMTO_HDR, na, new_from_uri, PJSIP_MAX_URL_SIZE);
+ if( len < 0 ) {
+ status = PJSIP_EINVALIDREQURI;
+ pjsua_perror(THIS_FILE, "Failed to print new From URI",
+ status);
+ goto on_error;
+ }
+
+ pj_strset( &from_user,
+ new_from_uri, len );
+ }
+ }
/* Create outgoing dialog: */
status = pjsip_dlg_create_uac( pjsip_ua_instance(),
- &acc->cfg.id, &contact,
+ &from_user, &contact,
dest_uri,
(msg_data && msg_data->target_uri.slen?
&msg_data->target_uri: dest_uri),
_______________________________________________
Visit our blog: http://blog.pjsip.org
pjsip mailing list
pjsip@xxxxxxxxxxxxxxx
http://lists.pjsip.org/mailman/listinfo/pjsip_lists.pjsip.org