static pj_status_t create_dialog( pjsip_user_agent *ua,
				  pj_grp_lock_t *grp_lock,
				  pjsip_dialog **p_dlg)
{
    pjsip_endpoint *endpt;
    pj_pool_t *pool;
    pjsip_dialog *dlg;
    pj_status_t status;

    endpt = pjsip_ua_get_endpt(ua);
    if (!endpt)
	return PJ_EINVALIDOP;

    pool = pjsip_endpt_create_pool(endpt, "dlg%p",
				   PJSIP_POOL_LEN_DIALOG,
				   PJSIP_POOL_INC_DIALOG);
    if (!pool)
	return PJ_ENOMEM;

    dlg = PJ_POOL_ZALLOC_T(pool, pjsip_dialog);
    PJ_ASSERT_RETURN(dlg != NULL, PJ_ENOMEM);

    dlg->pool = pool;
    pj_ansi_snprintf(dlg->obj_name, sizeof(dlg->obj_name), "dlg%p", dlg);
    dlg->ua = ua;
    dlg->endpt = endpt;
    dlg->state = PJSIP_DIALOG_STATE_NULL;
    dlg->add_allow = pjsip_include_allow_hdr_in_dlg;

    pj_list_init(&dlg->inv_hdr);
    pj_list_init(&dlg->rem_cap_hdr);

    /* Init client authentication session. */
    status = pjsip_auth_clt_init(&dlg->auth_sess, dlg->endpt,
				 dlg->pool, 0);
    if (status != PJ_SUCCESS)
	goto on_error;

    if (grp_lock) {
	dlg->grp_lock_ = grp_lock;
    } else {
 	status = pj_grp_lock_create(pool, NULL, &dlg->grp_lock_);
 	if (status != PJ_SUCCESS) {
	    goto on_error;
 	}
    }

    pj_grp_lock_add_ref(dlg->grp_lock_);
    pj_grp_lock_add_handler(dlg->grp_lock_, pool, dlg, &dlg_on_destroy);

    pjsip_target_set_init(&dlg->target_set);

    *p_dlg = dlg;
    return PJ_SUCCESS;

on_error:
    pjsip_endpt_release_pool(endpt, pool);
    return status;
}
static pj_status_t create_dialog( pjsip_user_agent *ua,
				  pj_grp_lock_t *grp_lock,
				  pjsip_dialog **p_dlg)
{
    pjsip_endpoint *endpt;
    pj_pool_t *pool;
    pjsip_dialog *dlg;
    pj_status_t status;

    endpt = pjsip_ua_get_endpt(ua);
    if (!endpt)
	return PJ_EINVALIDOP;

    pool = pjsip_endpt_create_pool(endpt, "dlg%p",
				   PJSIP_POOL_LEN_DIALOG,
				   PJSIP_POOL_INC_DIALOG);
    if (!pool)
	return PJ_ENOMEM;

    dlg = PJ_POOL_ZALLOC_T(pool, pjsip_dialog);
    PJ_ASSERT_RETURN(dlg != NULL, PJ_ENOMEM);

    dlg->pool = pool;
    pj_ansi_snprintf(dlg->obj_name, sizeof(dlg->obj_name), "dlg%p", dlg);
    dlg->ua = ua;
    dlg->endpt = endpt;
    dlg->state = PJSIP_DIALOG_STATE_NULL;
    dlg->add_allow = pjsip_include_allow_hdr_in_dlg;

    pj_list_init(&dlg->inv_hdr);
    pj_list_init(&dlg->rem_cap_hdr);

     
    status = pjsip_auth_clt_init(&dlg->auth_sess, dlg->endpt,
				 dlg->pool, 0);
    if (status != PJ_SUCCESS)
	goto on_error;

    if (grp_lock) {
	dlg->grp_lock_ = grp_lock;
    } else {
 	status = pj_grp_lock_create(pool, NULL, &dlg->grp_lock_);
 	if (status != PJ_SUCCESS) {
	    goto on_error;
 	}
    }

    pj_grp_lock_add_ref(dlg->grp_lock_);
    pj_grp_lock_add_handler(dlg->grp_lock_, pool, dlg, &dlg_on_destroy);

    pjsip_target_set_init(&dlg->target_set);

    *p_dlg = dlg;
    return PJ_SUCCESS;

on_error:
    pjsip_endpt_release_pool(endpt, pool);
    return status;
}