Add SIP transport data received callback (#3751)

This commit is contained in:
sauwming 2023-11-06 20:17:49 +08:00 committed by GitHub
parent da91020a44
commit ca5255795a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 1 deletions

View File

@ -1661,6 +1661,58 @@ PJ_DECL(pj_status_t) pjsip_tpmgr_set_drop_data_cb(pjsip_tpmgr *mgr,
pjsip_tp_on_rx_dropped_cb cb);
/**
* Structure of received data that will be passed to data received
* notification callback.
*/
typedef struct pjsip_tp_rx_data
{
/**
* The transport receiving the data.
*/
pjsip_transport *tp;
/**
* The data.
*/
void *data;
/**
* The data length.
* If application wishes to discard some data of len p, it can pass
* the remaining data back to PJSIP to be processed by setting the len
* to (len - p).
* If application wants to shutdown the transport from within the
* callback (for example after if finds out that the data is
* suspicious/garbage), app must set the len to zero to prevent
* further processing of the data.
*/
pj_size_t len;
} pjsip_tp_rx_data;
/**
* Type of callback to data received notifications.
*
* @param data The received data.
*/
typedef pj_status_t (*pjsip_tp_on_rx_data_cb)(pjsip_tp_rx_data *data);
/**
* Set callback to be called whenever any data is received by a stream
* oriented transport. This can be useful for application to do its own
* verification, filtering, or logging of potential malicious activities.
*
* @param mgr Transport manager.
* @param cb The callback function, set to NULL to reset the callback.
*
* @return PJ_SUCCESS on success, or the appropriate error code.
*/
PJ_DECL(pj_status_t) pjsip_tpmgr_set_recv_data_cb(pjsip_tpmgr *mgr,
pjsip_tp_on_rx_data_cb cb);
/**
* @}
*/

View File

@ -142,8 +142,9 @@ struct pjsip_tpmgr
#endif
void (*on_rx_msg)(pjsip_endpoint*, pj_status_t, pjsip_rx_data*);
pj_status_t (*on_tx_msg)(pjsip_endpoint*, pjsip_tx_data*);
pjsip_tp_state_callback tp_state_cb;
pjsip_tp_state_callback tp_state_cb;
pjsip_tp_on_rx_dropped_cb tp_drop_data_cb;
pjsip_tp_on_rx_data_cb tp_rx_data_cb;
/* Transmit data list, for transmit data cleanup when transport manager
* is destroyed.
@ -2066,6 +2067,24 @@ PJ_DEF(pj_ssize_t) pjsip_tpmgr_receive_packet( pjsip_tpmgr *mgr,
/* For TCP transport, check if the whole message has been received. */
if ((tr->flag & PJSIP_TRANSPORT_DATAGRAM) == 0) {
pj_status_t msg_status;
if (mgr->tp_rx_data_cb) {
pjsip_tp_rx_data rd;
pj_bzero(&rd, sizeof(rd));
rd.tp = tr;
rd.data = current_pkt;
rd.len = remaining_len;
(*mgr->tp_rx_data_cb)(&rd);
if (rd.len < remaining_len) {
msg_fragment_size = remaining_len - rd.len;
total_processed += msg_fragment_size;
current_pkt += msg_fragment_size;
remaining_len = rd.len;
continue;
}
}
msg_status = pjsip_find_msg(current_pkt, remaining_len, PJ_FALSE,
&msg_fragment_size);
if (msg_status != PJ_SUCCESS) {
@ -2838,3 +2857,18 @@ PJ_DEF(pj_status_t) pjsip_tpmgr_set_drop_data_cb(pjsip_tpmgr *mgr,
return PJ_SUCCESS;
}
/*
* Set callback for custom parser
*/
PJ_DEF(pj_status_t) pjsip_tpmgr_set_recv_data_cb(pjsip_tpmgr *mgr,
pjsip_tp_on_rx_data_cb cb)
{
PJ_ASSERT_RETURN(mgr, PJ_EINVAL);
mgr->tp_rx_data_cb = cb;
return PJ_SUCCESS;
}