前言 对于EXOSIP_REGISTRATION_FAILURE事件,有两种处理方式: 1、自动处理 在eXosip事件处理循环中调用eXosip_automatic_action()函数,该函数会自动帮助用户处理401/407状态,用户并不再需要手动发
前言
对于EXOSIP_REGISTRATION_FAILURE事件,有两种处理方式:
1、自动处理
在eXosip事件处理循环中调用eXosip_automatic_action()函数,该函数会自动帮助用户处理401/407状态,用户并不再需要手动发送带认证信息的注册请求,只需要在第一次注册时就添加好认证信息即可;
2、手动处理,需要在EXOSIP_REGISTRATION_FAILURE事件中增加处理代码
函数说明
1)处理订阅
2)自动处理注册
3)自动进行发布
代码
void
eXosip_automatic_action (struct eXosip_t *excontext)
{
eXosip_call_t *jc;
eXosip_dialog_t *jd;
#ifndef MINISIZE
eXosip_subscribe_t *js;
eXosip_notify_t *jn;
#endif
eXosip_reg_t *jr;
#ifndef MINISIZE
eXosip_pub_t *jpub;
#endif
time_t now;
now = osip_getsystemtime (NULL);
for (jc = excontext->j_calls; jc != NULL; jc = jc->next) {
if (jc->c_id < 1) {
}
else if (jc->c_dialogs == NULL || jc->c_dialogs->d_dialog == NULL) {
/* an EARLY dialog may have failed with 401,407 or 3Xx */
osip_transaction_t *out_tr = NULL;
out_tr = jc->c_out_tr;
if (out_tr != NULL
&& (out_tr->state == ICT_TERMINATED
|| out_tr->state == NICT_TERMINATED
|| out_tr->state == ICT_COMPLETED
|| out_tr->state == NICT_COMPLETED) && now - out_tr->birth_time < 120 && out_tr->orig_request != NULL && out_tr->last_response != NULL && (out_tr->last_response->status_code == 401 || out_tr->last_response->status_code == 407)) {
/* retry with credential */
if (jc->c_retry < 3) {
int i;
i = _eXosip_call_retry_request (excontext, jc, NULL, out_tr);
if (i != 0) {
OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "eXosip: could not clone msg for authentication\n"));
}
jc->c_retry++;
}
}
else if (out_tr != NULL
&& (out_tr->state == ICT_TERMINATED
|| out_tr->state == NICT_TERMINATED
|| out_tr->state == ICT_COMPLETED || out_tr->state == NICT_COMPLETED) && now - out_tr->birth_time < 120 && out_tr->orig_request != NULL && out_tr->last_response != NULL && out_tr->last_response->status_code == 422) {
/* retry with higher Session-Expires / Min-SE */
if (jc->c_retry < 3) {
int i;
i = _eXosip_call_retry_request (excontext, jc, NULL, out_tr);
if (i != 0) {
OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "eXosip: could not clone msg for updating Session-Expires\n"));
}
jc->c_retry++;
}
}
else if (out_tr != NULL
&& (out_tr->state == ICT_TERMINATED
|| out_tr->state == NICT_TERMINATED
|| out_tr->state == ICT_COMPLETED
|| out_tr->state == NICT_COMPLETED) && now - out_tr->birth_time < 120 && out_tr->orig_request != NULL && out_tr->last_response != NULL && (out_tr->last_response->status_code >= 300 && out_tr->last_response->status_code <= 399)) {
/* retry with credential */
int i;
i = _eXosip_call_retry_request (excontext, jc, NULL, out_tr);
if (i != 0) {
OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "eXosip: could not clone msg for redirection\n"));
}
}
}
for (jd = jc->c_dialogs; jd != NULL; jd = jd->next) {
if (jd->d_dialog == NULL) { /* finished call */
}
else {
osip_transaction_t *out_tr = NULL;
out_tr = osip_list_get (jd->d_out_trs, 0);
if (out_tr == NULL)
out_tr = jc->c_out_tr;
if (out_tr != NULL
&& (out_tr->state == ICT_TERMINATED
|| out_tr->state == NICT_TERMINATED
|| out_tr->state == ICT_COMPLETED
|| out_tr->state == NICT_COMPLETED) && now - out_tr->birth_time < 120 && out_tr->orig_request != NULL && out_tr->last_response != NULL && (out_tr->last_response->status_code == 401 || out_tr->last_response->status_code == 407)) {
/* retry with credential */
if (jd->d_retry < 3) {
int i;
i = _eXosip_call_retry_request (excontext, jc, jd, out_tr);
if (i != 0) {
OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "eXosip: could not clone msg for authentication\n"));
}
jd->d_retry++;
}
}
else if (out_tr != NULL
&& (out_tr->state == ICT_TERMINATED
|| out_tr->state == NICT_TERMINATED
|| out_tr->state == ICT_COMPLETED || out_tr->state == NICT_COMPLETED) && now - out_tr->birth_time < 120 && out_tr->orig_request != NULL && out_tr->last_response != NULL && out_tr->last_response->status_code == 422) {
/* retry with higher Session-Expires / Min-SE */
if (jd->d_retry < 3) {
int i;
i = _eXosip_call_retry_request (excontext, jc, jd, out_tr);
if (i != 0) {
OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "eXosip: could not clone msg for updating Session-Expires\n"));
}
jd->d_retry++;
}
}
else if (out_tr != NULL
&& (out_tr->state == ICT_TERMINATED
|| out_tr->state == NICT_TERMINATED
|| out_tr->state == ICT_COMPLETED
|| out_tr->state == NICT_COMPLETED) && now - out_tr->birth_time < 120 && out_tr->orig_request != NULL && out_tr->last_response != NULL && (out_tr->last_response->status_code >= 300 && out_tr->last_response->status_code <= 399)) {
/* retry with credential */
int i;
i = _eXosip_call_retry_request (excontext, jc, jd, out_tr);
if (i != 0) {
OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "eXosip: could not clone msg for redirection\n"));
}
}
else {
/* check if we need session-timer */
if (jd->d_refresher == -1) {
}
else if (jd->d_refresher == 0) {
/* I'm the refresher */
if (now - (jd->d_session_timer_start + jd->d_session_timer_length) > 0) {
/* means we tried hard... but certainly failed... */
}
else if ((jd->d_session_timer_start + jd->d_session_timer_length) - now < (jd->d_session_timer_length / 2)) {
osip_message_t *request = NULL;
if (jd->d_session_timer_use_update == 1) {
eXosip_call_build_update (excontext, jd->d_id, &request);
if (request != NULL) {
char session_exp[32];
int i;
memset (session_exp, 0, sizeof (session_exp));
snprintf (session_exp, sizeof (session_exp) - 1, "%i;refresher=uac", jd->d_session_timer_length);
osip_message_set_header (request, "Session-Expires", session_exp);
osip_message_set_supported (request, "timer");
i = eXosip_call_send_request (excontext, jd->d_id, request);
if (i == 0) {
/* update timer */
jd->d_session_timer_start = osip_getsystemtime (NULL) + jd->d_session_timer_length;
}
}
}
else {
eXosip_call_build_request (excontext, jd->d_id, "INVITE", &request);
if (request != NULL) {
char session_exp[32];
int i;
memset (session_exp, 0, sizeof (session_exp));
snprintf (session_exp, sizeof (session_exp) - 1, "%i;refresher=uac", jd->d_session_timer_length);
osip_message_set_header (request, "Session-Expires", session_exp);
osip_message_set_supported (request, "timer");
i = eXosip_call_send_request (excontext, jd->d_id, request);
if (i == 0) {
/* update timer */
jd->d_session_timer_start = osip_getsystemtime (NULL) + jd->d_session_timer_length;
}
}
}
}
}
else if (jd->d_refresher == 1) {
/* remote is the refresher */
}
}
}
}
}
#ifndef MINISIZE
for (js = excontext->j_subscribes; js != NULL; js = js->next) {
if (js->s_id < 1) {
}
else if (js->s_dialogs == NULL) {
osip_transaction_t *out_tr = NULL;
out_tr = js->s_out_tr;
if (out_tr != NULL
&& (out_tr->state == NICT_TERMINATED
|| out_tr->state == NICT_COMPLETED) &&
now - out_tr->birth_time < 120 && out_tr->orig_request != NULL && out_tr->last_response != NULL && (out_tr->last_response->status_code == 401 || out_tr->last_response->status_code == 407 || out_tr->last_response->status_code == 423)) {
/* retry with credential */
if (js->s_retry < 3) {
int i;
i = _eXosip_subscribe_send_request_with_credential (excontext, js, NULL, out_tr);
if (i != 0) {
OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "eXosip: could not clone msg for authentication\n"));
}
js->s_retry++;
}
}
}
for (jd = js->s_dialogs; jd != NULL; jd = jd->next) {
if (jd->d_dialog != NULL) { /* finished call */
if (jd->d_id >= 1) {
osip_transaction_t *out_tr = NULL;
out_tr = osip_list_get (jd->d_out_trs, 0);
if (out_tr == NULL)
out_tr = js->s_out_tr;
if (out_tr != NULL
&& (out_tr->state == NICT_TERMINATED
|| out_tr->state == NICT_COMPLETED) && now - out_tr->birth_time < 120 && out_tr->orig_request != NULL && out_tr->last_response != NULL && (out_tr->last_response->status_code == 401 || out_tr->last_response->status_code == 407)) {
/* retry with credential */
if (jd->d_retry < 3) {
int i;
i = _eXosip_subscribe_send_request_with_credential (excontext, js, jd, out_tr);
if (i != 0) {
OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "eXosip: could not clone suscbribe for authentication\n"));
}
jd->d_retry++;
}
}
else if (js->s_reg_period == 0 || out_tr == NULL) {
}
else if ((out_tr->state == NICT_TERMINATED || out_tr->state == NICT_COMPLETED) && out_tr->orig_request != NULL && out_tr->last_response != NULL && (out_tr->last_response->status_code >= 300)) {
/* refresh are not authorized after an error */
}
else if ((out_tr->state == NICT_TERMINATED || out_tr->state == NICT_COMPLETED) && (now - out_tr->birth_time > js->s_reg_period - (js->s_reg_period / 10) || now - out_tr->birth_time > js->s_reg_period - 6)) { /* will expires in js->s_reg_period/10 sec OR 6 seconds: send refresh! */
int i;
i = _eXosip_subscribe_automatic_refresh (excontext, js, jd, out_tr);
if (i != 0) {
OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "eXosip: could not clone subscribe for refresh\n"));
}
}
}
}
}
}
for (jn = excontext->j_notifies; jn != NULL; jn = jn->next) {
for (jd = jn->n_dialogs; jd != NULL; jd = jd->next) {
if (jd->d_dialog != NULL) { /* finished call */
if (jd->d_id >= 1) {
osip_transaction_t *out_tr = NULL;
out_tr = osip_list_get (jd->d_out_trs, 0);
if (out_tr != NULL
&& (out_tr->state == NICT_TERMINATED
|| out_tr->state == NICT_COMPLETED) && now - out_tr->birth_time < 120 && out_tr->orig_request != NULL && out_tr->last_response != NULL && (out_tr->last_response->status_code == 401 || out_tr->last_response->status_code == 407)) {
/* retry with credential */
if (jd->d_retry < 3) {
int i;
i = _eXosip_insubscription_send_request_with_credential (excontext, jn, jd, out_tr);
if (i != 0) {
OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "eXosip: could not clone notify for authentication\n"));
}
jd->d_retry++;
}
}
}
}
}
}
#endif
for (jr = excontext->j_reg; jr != NULL; jr = jr->next) {
if (jr->r_id >= 1 && jr->r_last_tr != NULL) {
if (jr->r_reg_period != 0 && now - jr->r_last_tr->birth_time > 900) {
/* automatic refresh */
eXosip_register_send_register (excontext, jr->r_id, NULL);
}
else if (jr->r_reg_period != 0 && (now - jr->r_last_tr->birth_time > jr->r_reg_period - (jr->r_reg_period / 10) || now - jr->r_last_tr->birth_time > jr->r_reg_period - 6)) {
/* automatic refresh */
eXosip_register_send_register (excontext, jr->r_id, NULL);
}
else if (jr->r_reg_period != 0 && now - jr->r_last_tr->birth_time > 120 && (jr->r_last_tr->last_response == NULL || (!MSG_IS_STATUS_2XX (jr->r_last_tr->last_response)))) {
/* automatic refresh */
eXosip_register_send_register (excontext, jr->r_id, NULL);
}
else if (now - jr->r_last_tr->birth_time < 120 &&
jr->r_last_tr->orig_request != NULL && (jr->r_last_tr->last_response != NULL && (jr->r_last_tr->last_response->status_code == 401 || jr->r_last_tr->last_response->status_code == 407 || jr->r_last_tr->last_response->status_code == 423))) {
if (jr->r_retry < 3) {
/* TODO: improve support for several retries when
several credentials are needed */
eXosip_register_send_register (excontext, jr->r_id, NULL);
jr->r_retry++;
}
}
}
}
#ifndef MINISIZE
for (jpub = excontext->j_pub; jpub != NULL; jpub = jpub->next) {
if (jpub->p_id >= 1 && jpub->p_last_tr != NULL) {
if (jpub->p_period != 0 && now - jpub->p_last_tr->birth_time > 900) {
/* automatic refresh */
_eXosip_publish_refresh (excontext, NULL, &jpub->p_last_tr, NULL);
}
else if (jpub->p_period != 0 && now - jpub->p_last_tr->birth_time > jpub->p_period - (jpub->p_period / 10)) {
/* automatic refresh */
_eXosip_publish_refresh (excontext, NULL, &jpub->p_last_tr, NULL);
}
else if (jpub->p_period != 0 && now - jpub->p_last_tr->birth_time > 120 && (jpub->p_last_tr->last_response == NULL || (!MSG_IS_STATUS_2XX (jpub->p_last_tr->last_response)))) {
/* automatic refresh */
_eXosip_publish_refresh (excontext, NULL, &jpub->p_last_tr, NULL);
}
else if (now - jpub->p_last_tr->birth_time < 120 && jpub->p_last_tr->orig_request != NULL && (jpub->p_last_tr->last_response != NULL && (jpub->p_last_tr->last_response->status_code == 401 || jpub->p_last_tr->last_response->status_code == 407))) {
if (jpub->p_retry < 3) {
/* TODO: improve support for several retries when
several credentials are needed */
_eXosip_retry_with_auth (excontext, NULL, &jpub->p_last_tr, NULL);
jpub->p_retry++;
}
}
else if (now - jpub->p_last_tr->birth_time < 120 && jpub->p_last_tr->orig_request != NULL && (jpub->p_last_tr->last_response != NULL && (jpub->p_last_tr->last_response->status_code == 412 || jpub->p_last_tr->last_response->status_code == 423))) {
_eXosip_publish_refresh (excontext, NULL, &jpub->p_last_tr, NULL);
}
}
}
#endif
}