Skip to content

Commit

Permalink
Add option to shutdown all transports on IP change (pjsip#3781)
Browse files Browse the repository at this point in the history
  • Loading branch information
nanangizz authored and dshamaev-intermedia committed Apr 2, 2024
1 parent 07cc148 commit 0cab06e
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pjsip-apps/src/pjsua/pjsua_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,11 @@ void on_ip_change_progress(pjsua_ip_change_op op,

if (status == PJ_SUCCESS) {
switch (op) {
case PJSUA_IP_CHANGE_OP_SHUTDOWN_TP:
pj_ansi_snprintf(info_str, sizeof(info_str),
"TCP/TLS transports shutdown");
break;

case PJSUA_IP_CHANGE_OP_RESTART_LIS:
pjsua_transport_get_info(info->lis_restart.transport_id, &tp_info);
pj_ansi_snprintf(info_str, sizeof(info_str),
Expand Down Expand Up @@ -1107,6 +1112,7 @@ void on_ip_change_progress(pjsua_ip_change_op op,
pj_ansi_snprintf(info_str, sizeof(info_str),
"done");
default:
info_str[0] = '\0';
break;
}
PJ_LOG(3,(THIS_FILE, "IP change progress report : %s", info_str));
Expand Down
1 change: 1 addition & 0 deletions pjsip-apps/src/swig/symbols.i
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,7 @@ typedef enum pjsua_snd_dev_mode
typedef enum pjsua_ip_change_op
{
PJSUA_IP_CHANGE_OP_NULL,
PJSUA_IP_CHANGE_OP_SHUTDOWN_TP,
PJSUA_IP_CHANGE_OP_RESTART_LIS,
PJSUA_IP_CHANGE_OP_ACC_SHUTDOWN_TP,
PJSUA_IP_CHANGE_OP_ACC_UPDATE_CONTACT,
Expand Down
47 changes: 47 additions & 0 deletions pjsip/include/pjsip/sip_transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,53 @@ PJ_DECL(pj_status_t) pjsip_tpmgr_destroy(pjsip_tpmgr *mgr);
PJ_DECL(void) pjsip_tpmgr_dump_transports(pjsip_tpmgr *mgr);


/**
* Parameter for pjsip_tpmgr_shutdown_all() function.
*/
typedef struct pjsip_tpmgr_shutdown_param
{
/**
* Specify whether disconnection state notification should be sent
* immediately, see pjsip_transport_shutdown2() for more info.
*
* Default: PJ_TRUE.
*/
pj_bool_t force;

/**
* Specify whether UDP transports should also be shutdown.
*
* Default: PJ_TRUE.
*/
pj_bool_t include_udp;

} pjsip_tpmgr_shutdown_param;


/**
* Initialize transports shutdown parameter with default values.
*
* @param prm The parameter to be initialized.
*/
PJ_DECL(void) pjsip_tpmgr_shutdown_param_default(
pjsip_tpmgr_shutdown_param *prm);


/**
* Shutdown all transports. This basically invokes pjsip_transport_shutdown2()
* on all transports.
*
* @param mgr The transport manager.
* @param param The function parameters.
*
* @return PJ_SUCCESS on success.
*/
PJ_DECL(pj_status_t) pjsip_tpmgr_shutdown_all(
pjsip_tpmgr *mgr,
const pjsip_tpmgr_shutdown_param *param);



/*****************************************************************************
*
* PUBLIC API
Expand Down
16 changes: 15 additions & 1 deletion pjsip/include/pjsua-lib/pjsua.h
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,11 @@ typedef enum pjsua_ip_change_op {
*/
PJSUA_IP_CHANGE_OP_NULL,

/**
* The restart listener process.
*/
PJSUA_IP_CHANGE_OP_SHUTDOWN_TP,

/**
* The restart listener process.
*/
Expand Down Expand Up @@ -2787,6 +2792,15 @@ typedef struct pjsua_ip_change_param
*/
unsigned restart_lis_delay;

/**
* If set to PJ_TRUE, this will forcefully shutdown all transports.
* Note that this will shutdown TCP/TLS transports only, UDP transport
* should be restarted via restart_listener.
*
* Default : PJ_TRUE
*/
pj_bool_t shutdown_transport;

} pjsua_ip_change_param;


Expand Down Expand Up @@ -7505,7 +7519,7 @@ typedef struct pjsua_snd_dev_param
*/
unsigned mode;

/*
/**
* The library will maintain the global sound device settings set when
* opening the sound device for the first time and later can be modified
* using #pjsua_snd_set_setting(). These setings are then applied to any
Expand Down
9 changes: 9 additions & 0 deletions pjsip/include/pjsua2/endpoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,15 @@ struct IpChangeParam {
*/
unsigned restartLisDelay;

/**
* If set to PJ_TRUE, this will forcefully shutdown all transports.
* Note that this will shutdown TCP/TLS transports only, UDP transport
* should be restarted via restart_listener.
*
* Default : PJ_TRUE
*/
bool shutdownTransport;

public:
/**
* Constructor.
Expand Down
54 changes: 54 additions & 0 deletions pjsip/src/pjsip/sip_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -1985,6 +1985,60 @@ PJ_DEF(pj_status_t) pjsip_tpmgr_destroy( pjsip_tpmgr *mgr )
}


/**
* Initialize transports shutdown parameter with default values.
*
* @param prm The parameter to be initialized.
*/
PJ_DEF(void) pjsip_tpmgr_shutdown_param_default(
pjsip_tpmgr_shutdown_param *prm)
{
pj_bzero(prm, sizeof(*prm));
prm->force = PJ_TRUE;
prm->include_udp = PJ_TRUE;
}

/*
* Shutdown all transports.
*/
PJ_DEF(pj_status_t) pjsip_tpmgr_shutdown_all(
pjsip_tpmgr *mgr,
const pjsip_tpmgr_shutdown_param *prm)
{
pj_hash_iterator_t itr_val;
pj_hash_iterator_t *itr;

PJ_ASSERT_RETURN(mgr, PJ_EINVAL);

PJ_LOG(3, (THIS_FILE, "Shutting down all transports"));

pj_lock_acquire(mgr->lock);

itr = pj_hash_first(mgr->table, &itr_val);
while (itr) {
transport *tp_entry = (transport*)pj_hash_this(mgr->table, itr);
if (tp_entry) {
transport *tp_iter = tp_entry;
do {
pjsip_transport *tp = tp_iter->tp;
if (prm->include_udp ||
((tp->key.type & ~PJSIP_TRANSPORT_IPV6) !=
PJSIP_TRANSPORT_UDP))
{
pjsip_transport_shutdown2(tp, prm->force);
}
tp_iter = tp_iter->next;
} while (tp_iter != tp_entry);
}
itr = pj_hash_next(mgr->table, itr);
}

pj_lock_release(mgr->lock);

return PJ_SUCCESS;
}


/*
* pjsip_tpmgr_receive_packet()
*
Expand Down
23 changes: 23 additions & 0 deletions pjsip/src/pjsua-lib/pjsua_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -3255,6 +3255,7 @@ PJ_DEF(void) pjsua_ip_change_param_default(pjsua_ip_change_param *param)
pj_bzero(param, sizeof(*param));
param->restart_listener = PJ_TRUE;
param->restart_lis_delay = PJSUA_TRANSPORT_RESTART_DELAY_TIME;
param->shutdown_transport = PJ_TRUE;
}


Expand Down Expand Up @@ -3985,6 +3986,28 @@ PJ_DEF(pj_status_t) pjsua_handle_ip_change(const pjsua_ip_change_param *param)
PJ_LOG(4,(THIS_FILE,"IP change temporarily ignores request timeout"));
}

/* Shutdown all TCP/TLS transports */
if (param->shutdown_transport) {
pjsip_tpmgr_shutdown_param param;
pjsua_ip_change_op_info info;

pjsip_tpmgr_shutdown_param_default(&param);
param.include_udp = PJ_FALSE;

PJ_LOG(4,(THIS_FILE, "IP change shutting down transports.."));
status = pjsip_tpmgr_shutdown_all(
pjsip_endpt_get_tpmgr(pjsua_var.endpt),
&param);

/* Provide dummy info instead of NULL info to avoid possible crash
* (if app does not check).
*/
pj_bzero(&info, sizeof(info));
pjsua_var.ua_cfg.cb.on_ip_change_progress(
PJSUA_IP_CHANGE_OP_SHUTDOWN_TP,
status, &info);
}

if (param->restart_listener) {
PJSUA_LOCK();
/* Restart listener/transport, handle_ip_change_on_acc() will
Expand Down
2 changes: 2 additions & 0 deletions pjsip/src/pjsua2/endpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ pjsua_ip_change_param IpChangeParam::toPj() const

param.restart_listener = restartListener;
param.restart_lis_delay = restartLisDelay;
param.shutdown_transport = shutdownTransport;

return param;
}
Expand All @@ -239,6 +240,7 @@ void IpChangeParam::fromPj(const pjsua_ip_change_param &param)
{
restartListener = PJ2BOOL(param.restart_listener);
restartLisDelay = param.restart_lis_delay;
shutdownTransport = PJ2BOOL(param.shutdown_transport);
}

///////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 0cab06e

Please sign in to comment.