Ignore:
Timestamp:
Feb 7, 2006 6:48:01 PM (18 years ago)
Author:
bennylp
Message:

Tested initial implementation: basic UAC, client registration, authentication, etc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip/src/pjsua/pjsua.c

    r139 r141  
    2424 
    2525 
    26 #define PJSUA_LOCAL_URI     "<sip:bennylp@192.168.0.7>" 
    27 #define PJSUA_CONTACT_URI   "<sip:bennylp@192.168.0.7>" 
     26#define PJSUA_LOCAL_URI     "<sip:user@127.0.0.1>" 
    2827 
    2928static char *PJSUA_DUMMY_SDP_OFFER =  
     
    7271 
    7372    pjsua.stun_port1 = pjsua.stun_port2 = 0; 
     73 
     74    /* Default URIs: */ 
     75 
     76    pjsua.local_uri = pj_str(PJSUA_LOCAL_URI); 
    7477} 
    7578 
     
    130133static void pjsua_inv_on_state_changed(pjsip_inv_session *inv, pjsip_event *e) 
    131134{ 
    132     ui_inv_on_state_changed(inv, e); 
     135    pjsua_ui_inv_on_state_changed(inv, e); 
    133136} 
    134137 
     
    397400 
    398401 
     402    /* Done */ 
     403 
     404    return PJ_SUCCESS; 
     405 
     406 
     407on_error: 
     408    pjsip_endpt_destroy(pjsua.endpt); 
     409    pjsua.endpt = NULL; 
     410    return status; 
     411} 
     412 
     413 
     414static int PJ_THREAD_FUNC pjsua_worker_thread(void *arg) 
     415{ 
     416    PJ_UNUSED_ARG(arg); 
     417 
     418    while (!pjsua.quit_flag) { 
     419        pj_time_val timeout = { 0, 10 }; 
     420        pjsip_endpt_handle_events (pjsua.endpt, &timeout); 
     421    } 
     422 
     423    return 0; 
     424} 
     425 
     426/* 
     427 * Initialize pjsua application. 
     428 * This will initialize all libraries, create endpoint instance, and register 
     429 * pjsip modules. 
     430 */ 
     431pj_status_t pjsua_init(void) 
     432{ 
     433    pj_status_t status; 
     434 
     435    /* Init PJLIB logging: */ 
     436 
     437    pj_log_set_level(pjsua.log_level); 
     438    pj_log_set_decor(pjsua.log_decor); 
     439 
     440 
     441    /* Init PJLIB: */ 
     442 
     443    status = pj_init(); 
     444    if (status != PJ_SUCCESS) { 
     445        pjsua_perror("pj_init() error", status); 
     446        return status; 
     447    } 
     448 
     449    /* Init memory pool: */ 
     450 
     451    /* Init caching pool. */ 
     452    pj_caching_pool_init(&pjsua.cp, &pj_pool_factory_default_policy, 0); 
     453 
     454    /* Create memory pool for application. */ 
     455    pjsua.pool = pj_pool_create(&pjsua.cp.factory, "pjsua", 4000, 4000, NULL); 
     456 
     457 
     458    /* Init PJSIP and all the modules: */ 
     459 
     460    status = init_stack(); 
     461    if (status != PJ_SUCCESS) { 
     462        pj_caching_pool_destroy(&pjsua.cp); 
     463        pjsua_perror("Stack initialization has returned error", status); 
     464        return status; 
     465    } 
     466 
     467    /* Done. */ 
     468    return PJ_SUCCESS; 
     469} 
     470 
     471 
     472 
     473/* 
     474 * Start pjsua stack. 
     475 * This will start the registration process, if registration is configured. 
     476 */ 
     477pj_status_t pjsua_start(void) 
     478{ 
     479    int i;  /* Must be signed */ 
     480    pjsip_transport *udp_transport; 
     481    pj_status_t status; 
     482 
     483    /* Init sockets (STUN etc): */ 
     484 
     485    status = init_sockets(); 
     486    if (status != PJ_SUCCESS) { 
     487        pj_caching_pool_destroy(&pjsua.cp); 
     488        pjsua_perror("init_sockets() has returned error", status); 
     489        return status; 
     490    } 
     491 
     492 
    399493    /* Add UDP transport: */ 
    400494 
     
    413507 
    414508        status = pjsip_udp_transport_attach( pjsua.endpt, pjsua.sip_sock, 
    415                                              &addr_name, 1, NULL); 
     509                                             &addr_name, 1,  
     510                                             &udp_transport); 
    416511        if (status != PJ_SUCCESS) { 
    417512            pjsua_perror("Unable to start UDP transport", status); 
    418             goto on_error; 
    419         } 
    420     } 
    421  
    422     /* Initialize local user info and contact: */ 
    423  
    424     { 
    425         pj_strdup2(pjsua.pool, &pjsua.local_uri, PJSUA_LOCAL_URI); 
    426         pj_strdup2(pjsua.pool, &pjsua.contact_uri, PJSUA_CONTACT_URI); 
     513            return status; 
     514        } 
     515    } 
     516 
     517    /* Initialize Contact URI, if one is not specified: */ 
     518 
     519    if (pjsua.contact_uri.slen == 0 && pjsua.local_uri.slen) { 
     520 
     521        pjsip_uri *uri; 
     522        pjsip_sip_uri *sip_uri; 
     523        char contact[128]; 
     524        int len; 
     525 
     526        /* The local Contact is the username@ip-addr, where 
     527         *  - username is taken from the local URI, 
     528         *  - ip-addr in UDP transport's address name (which may have been 
     529         *    resolved from STUN. 
     530         */ 
     531         
     532        /* Need to parse local_uri to get the elements: */ 
     533 
     534        uri = pjsip_parse_uri(pjsua.pool, pjsua.local_uri.ptr,  
     535                              pjsua.local_uri.slen, 0); 
     536        if (uri == NULL) { 
     537            pjsua_perror("Invalid local URI", PJSIP_EINVALIDURI); 
     538            return PJSIP_EINVALIDURI; 
     539        } 
     540 
     541 
     542        /* Local URI MUST be a SIP or SIPS: */ 
     543 
     544        if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri)) { 
     545            pjsua_perror("Invalid local URI", PJSIP_EINVALIDSCHEME); 
     546            return PJSIP_EINVALIDSCHEME; 
     547        } 
     548 
     549 
     550        /* Get the SIP URI object: */ 
     551 
     552        sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri); 
     553 
     554         
     555        /* Build temporary contact string. */ 
     556 
     557        if (sip_uri->user.slen) { 
     558 
     559            /* With the user part. */ 
     560            len = pj_snprintf(contact, sizeof(contact), 
     561                              "<sip:%.*s@%.*s:%d>", 
     562                              sip_uri->user.slen, 
     563                              sip_uri->user.ptr, 
     564                              udp_transport->local_name.host.slen, 
     565                              udp_transport->local_name.host.ptr, 
     566                              udp_transport->local_name.port); 
     567        } else { 
     568 
     569            /* Without user part */ 
     570 
     571            len = pj_snprintf(contact, sizeof(contact), 
     572                              "<sip:%.*s:%d>", 
     573                              udp_transport->local_name.host.slen, 
     574                              udp_transport->local_name.host.ptr, 
     575                              udp_transport->local_name.port); 
     576        } 
     577 
     578        if (len < 1 || len >= sizeof(contact)) { 
     579            pjsua_perror("Invalid Contact", PJSIP_EURITOOLONG); 
     580            return PJSIP_EURITOOLONG; 
     581        } 
     582 
     583        /* Duplicate Contact uri. */ 
     584 
     585        pj_strdup2(pjsua.pool, &pjsua.contact_uri, contact); 
     586 
    427587    } 
    428588 
     
    431591    PJ_TODO(INIT_GLOBAL_ROUTE_SET); 
    432592 
    433  
    434     /* Start registration: */ 
    435  
    436     PJ_TODO(START_REGISTRATION); 
    437  
    438     /* Done? */ 
    439  
    440     return PJ_SUCCESS; 
    441  
    442  
    443 on_error: 
    444     pjsip_endpt_destroy(pjsua.endpt); 
    445     pjsua.endpt = NULL; 
    446     return status; 
    447 } 
    448  
    449  
    450 static int PJ_THREAD_FUNC pjsua_worker_thread(void *arg) 
    451 { 
    452     PJ_UNUSED_ARG(arg); 
    453  
    454     while (!pjsua.quit_flag) { 
    455         pj_time_val timeout = { 0, 10 }; 
    456         pjsip_endpt_handle_events (pjsua.endpt, &timeout); 
    457     } 
    458  
    459     return 0; 
    460 } 
    461  
    462 /* 
    463  * Initialize pjsua application. 
    464  * This will start the registration process, if registration is configured. 
    465  */ 
    466 pj_status_t pjsua_init(void) 
    467 { 
    468     int i;  /* Must be signed */ 
    469     pj_status_t status; 
    470  
    471     /* Init PJLIB logging: */ 
    472  
    473     pj_log_set_level(pjsua.log_level); 
    474     pj_log_set_decor(pjsua.log_decor); 
    475  
    476  
    477     /* Init PJLIB: */ 
    478  
    479     status = pj_init(); 
    480     if (status != PJ_SUCCESS) { 
    481         pjsua_perror("pj_init() error", status); 
    482         return status; 
    483     } 
    484  
    485     /* Init memory pool: */ 
    486  
    487     /* Init caching pool. */ 
    488     pj_caching_pool_init(&pjsua.cp, &pj_pool_factory_default_policy, 0); 
    489  
    490     /* Create memory pool for application. */ 
    491     pjsua.pool = pj_pool_create(&pjsua.cp.factory, "pjsua", 4000, 4000, NULL); 
    492  
    493  
    494     /* Init sockets (STUN etc): */ 
    495  
    496     status = init_sockets(); 
    497     if (status != PJ_SUCCESS) { 
    498         pj_caching_pool_destroy(&pjsua.cp); 
    499         pjsua_perror("init_sockets() has returned error", status); 
    500         return status; 
    501     } 
    502  
    503  
    504     /* Init PJSIP and all the modules: */ 
    505  
    506     status = init_stack(); 
    507     if (status != PJ_SUCCESS) { 
    508         pj_caching_pool_destroy(&pjsua.cp); 
    509         pjsua_perror("Stack initialization has returned error", status); 
    510         return status; 
    511     } 
    512593 
    513594    /* Create worker thread(s), if required: */ 
     
    527608    } 
    528609 
    529     /* Done. */ 
     610    /* Start registration: */ 
     611 
     612    /* Create client registration session: */ 
     613 
     614    status = pjsua_regc_init(); 
     615    if (status != PJ_SUCCESS) 
     616        return status; 
     617 
     618    /* Perform registration, if required. */ 
     619    if (pjsua.regc) { 
     620        pjsua_regc_update(1); 
     621    } 
     622 
     623 
     624 
    530625    return PJ_SUCCESS; 
    531626} 
     
    538633{ 
    539634    int i; 
     635 
     636    /* Unregister, if required: */ 
     637    if (pjsua.regc) { 
     638 
     639        pjsua_regc_update(0); 
     640 
     641        /* Wait for some time to allow unregistration to complete: */ 
     642 
     643        pj_thread_sleep(500); 
     644    } 
    540645 
    541646    /* Signal threads to quit: */ 
     
    611716 
    612717 
     718    /* Set dialog Route-Set: */ 
     719 
     720    PJ_TODO(INIT_DIALOG_ROUTE_SET); 
     721 
    613722    /* Set credentials: */ 
    614723 
    615     PJ_TODO(SET_DIALOG_CREDENTIALS); 
     724    pjsip_auth_clt_set_credentials( &dlg->auth_sess, pjsua.cred_count,  
     725                                    pjsua.cred_info); 
    616726 
    617727 
Note: See TracChangeset for help on using the changeset viewer.