PJ_DEF(pj_status_t) pjsip_target_set_add_uri( pjsip_target_set *tset,
					      pj_pool_t *pool,
					      const pjsip_uri *uri,
					      int q1000)
{
    pjsip_target *t, *pos = NULL;

    PJ_ASSERT_RETURN(tset && pool && uri, PJ_EINVAL);

    /* Set q-value to 1 if it is not set */
    if (q1000 <= 0)
	q1000 = 1000;

    /* Scan all the elements to see for duplicates, and at the same time
     * get the position where the new element should be inserted to
     * based on the q-value.
     */
    t = tset->head.next;
    while (t != &tset->head) {
	if (pjsip_uri_cmp(PJSIP_URI_IN_REQ_URI, t->uri, uri)==PJ_SUCCESS)
	    return PJ_EEXISTS;
	if (pos==NULL && t->q1000 < q1000)
	    pos = t;
	t = t->next;
    }

    /* Create new element */
    t = PJ_POOL_ZALLOC_T(pool, pjsip_target);
    t->uri = (pjsip_uri*)pjsip_uri_clone(pool, uri);
    t->q1000 = q1000;

    /* Insert */
    if (pos == NULL)
	pj_list_push_back(&tset->head, t);
    else
	pj_list_insert_before(pos, t);

    /* Set current target if this is the first URI */
    if (tset->current == NULL)
	tset->current = t;

    return PJ_SUCCESS;
}
PJ_DEF(pj_status_t) pjsip_target_set_add_uri( pjsip_target_set *tset,
					      pj_pool_t *pool,
					      const pjsip_uri *uri,
					      int q1000)
{
    pjsip_target *t, *pos = NULL;

    PJ_ASSERT_RETURN(tset && pool && uri, PJ_EINVAL);

     
    if (q1000 <= 0)
	q1000 = 1000;

     
    t = tset->head.next;
    while (t != &tset->head) {
	if (pjsip_uri_cmp(PJSIP_URI_IN_REQ_URI, t->uri, uri)==PJ_SUCCESS)
	    return PJ_EEXISTS;
	if (pos==NULL && t->q1000 < q1000)
	    pos = t;
	t = t->next;
    }

     
    t = PJ_POOL_ZALLOC_T(pool, pjsip_target);
    t->uri = (pjsip_uri*)pjsip_uri_clone(pool, uri);
    t->q1000 = q1000;

     
    if (pos == NULL)
	pj_list_push_back(&tset->head, t);
    else
	pj_list_insert_before(pos, t);

     
    if (tset->current == NULL)
	tset->current = t;

    return PJ_SUCCESS;
}