Changeset 1709


Ignore:
Timestamp:
Jan 18, 2008 6:49:29 PM (16 years ago)
Author:
bennylp
Message:

More ticket #452: SRTP creation/destruction, added use_srtp and srtp_secure_signaling in pjsua_account_config, updated pjsua

Location:
pjproject/branches/users/nanang
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • pjproject/branches/users/nanang/pjsip-apps/src/pjsua/pjsua_app.c

    r1698 r1709  
    121121    puts  ("SIP Account options:"); 
    122122    puts  ("  --use-ims           Enable 3GPP/IMS related settings on this account"); 
     123    puts  ("  --use-srtp=N        Use SRTP N= 0: disabled, 1: optional, 2: mandatory"); 
    123124    puts  ("  --registrar=url     Set the URL of registrar server"); 
    124125    puts  ("  --id=url            Set the URL of local ID (used in From header)"); 
     
    166167    puts  ("Media Options:"); 
    167168    puts  ("  --use-ice           Enable ICE (default:no)"); 
    168     puts  ("  --use-srtp          Enable SRTP (default:no)"); 
    169169    puts  ("  --add-codec=name    Manually add codec (default is to enable all)"); 
    170170    puts  ("  --dis-codec=name    Disable codec (can be specified multiple times)"); 
     
    443443        { "rtp-port",   1, 0, OPT_RTP_PORT}, 
    444444        { "use-ice",    0, 0, OPT_USE_ICE}, 
    445         { "use-srtp",   0, 0, OPT_USE_SRTP}, 
     445        { "use-srtp",   1, 0, OPT_USE_SRTP}, 
    446446        { "add-codec",  1, 0, OPT_ADD_CODEC}, 
    447447        { "dis-codec",  1, 0, OPT_DIS_CODEC}, 
     
    800800 
    801801        case OPT_USE_SRTP: 
    802             cfg->media_cfg.enable_srtp = PJ_TRUE; 
     802            i = my_atoi(pj_optarg); 
     803            if (!pj_isdigit(*pj_optarg) || i > 2) { 
     804                PJ_LOG(1,(THIS_FILE, "Invalid value for --use-srtp option")); 
     805                return -1; 
     806            } 
     807            cur_acc->use_srtp = i; 
    803808            break; 
    804809 
     
    11151120        pj_ansi_sprintf(line, "--auto-update-nat %i\n", 
    11161121                        (int)acc_cfg->auto_update_nat); 
     1122        pj_strcat2(result, line); 
     1123    } 
     1124 
     1125    /* SRTP */ 
     1126    if (acc_cfg->use_srtp) { 
     1127        pj_ansi_sprintf(line, "--use-srtp %i\n", 
     1128                        (int)acc_cfg->use_srtp); 
    11171129        pj_strcat2(result, line); 
    11181130    } 
  • pjproject/branches/users/nanang/pjsip/include/pjsua-lib/pjsua.h

    r1698 r1709  
    19721972    pj_str_t         ka_data; 
    19731973 
     1974    /** 
     1975     * Specify whether secure media transport should be used for this account. 
     1976     * Valid values are PJMEDIA_SRTP_DISABLED, PJMEDIA_SRTP_OPTIONAL, and 
     1977     * PJMEDIA_SRTP_MANDATORY. 
     1978     * 
     1979     * Default: 
     1980     *  PJMEDIA_SRTP_DISABLED 
     1981     */ 
     1982    pjmedia_srtp_use    use_srtp; 
     1983 
     1984    /** 
     1985     * Specify whether SRTP requires secure signaling to be used. This option 
     1986     * is only used when \a use_srtp option above is non-zero. 
     1987     * 
     1988     * Valid values are: 
     1989     *  0: SRTP does not require secure signaling 
     1990     *  1: SRTP requires secure transport such as TLS 
     1991     *  2: SRTP requires secure end-to-end transport (SIPS) 
     1992     * 
     1993     * Default: 0 
     1994     */ 
     1995    int              srtp_secure_signaling; 
     1996 
    19741997} pjsua_acc_config; 
    19751998 
     
    37693792     */ 
    37703793    pj_bool_t           enable_relay; 
    3771  
    3772     /** 
    3773      * Enable SRTP 
    3774      */ 
    3775     pj_bool_t           enable_srtp; 
    37763794}; 
    37773795 
  • pjproject/branches/users/nanang/pjsip/include/pjsua-lib/pjsua_internal.h

    r1698 r1709  
    5151    pjsip_evsub         *xfer_sub;  /**< Xfer server subscription, if this 
    5252                                         call was triggered by xfer.        */ 
    53     pjmedia_transport   *med_tp;    /**< Media transport.                   */ 
     53    pjmedia_transport   *med_tp;    /**< Current media transport.           */ 
     54    pjmedia_transport   *med_orig;  /**< Original media transport           */ 
    5455    pj_timer_entry       refresh_tm;/**< Timer to send re-INVITE.           */ 
    5556    pj_timer_entry       hangup_tm; /**< Timer to hangup call.              */ 
     
    313314 */ 
    314315pj_status_t pjsua_media_channel_init(pjsua_call_id call_id, 
    315                                     pjsip_role_e role); 
     316                                     pjsip_role_e role, 
     317                                     int security_level); 
    316318pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,  
    317319                                           pj_pool_t *pool, 
  • pjproject/branches/users/nanang/pjsip/src/pjsua-lib/pjsua_call.c

    r1698 r1709  
    215215#define LATE_SDP    0 
    216216 
     217static pj_bool_t pj_stristr(const pj_str_t *str, const pj_str_t *substr) 
     218{ 
     219    int i; 
     220 
     221    for (i=0; i<(str->slen-substr->slen); ++i) { 
     222        pj_str_t s; 
     223        s.ptr = str->ptr+i; 
     224        s.slen = substr->slen; 
     225 
     226        if (pj_stricmp(&s, substr)==0) 
     227            return PJ_TRUE; 
     228    } 
     229    return PJ_FALSE; 
     230} 
     231 
     232/* Get signaling secure level. 
     233 * Return: 
     234 *  0: if signaling is not secure 
     235 *  1: if TLS transport is used for immediate hop 
     236 *  2: if end-to-end signaling is secure. 
     237 * 
     238 * NOTE: 
     239 *  THIS IS WRONG. It should take into account the route-set. 
     240 */ 
     241static int get_secure_level(const pj_str_t *dst_uri) 
     242{ 
     243    const pj_str_t tls = pj_str(";transport=tls"); 
     244    const pj_str_t sips = pj_str("sips:"); 
     245 
     246    PJ_TODO(Fix_get_secure_level); 
     247 
     248    if (pj_stristr(dst_uri, &sips)) 
     249        return 2; 
     250    if (pj_stristr(dst_uri, &tls)) 
     251        return 1; 
     252    return 0; 
     253} 
     254 
    217255/* 
    218256 * Make outgoing call to the specified URI using the specified account. 
     
    325363 
    326364    /* Init media channel */ 
    327     status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC); 
     365    status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC,  
     366                                      get_secure_level(dest_uri)); 
    328367    if (status != PJ_SUCCESS) { 
    329368        pjsua_perror(THIS_FILE, "Error initializing media channel", status); 
     
    482521    pjsua_call *call; 
    483522    int call_id = -1; 
     523    int secure_level; 
    484524    pjmedia_sdp_session *offer, *answer; 
    485525    pj_status_t status; 
     
    580620    } 
    581621 
     622    /*  
     623     * Get which account is most likely to be associated with this incoming 
     624     * call. We need the account to find which contact URI to put for 
     625     * the call. 
     626     */ 
     627    acc_id = call->acc_id = pjsua_acc_find_for_incoming(rdata); 
     628 
     629    /* Get signaling security level, only when required by SRTP */ 
     630    if (pjsua_var.acc[acc_id].cfg.srtp_secure_signaling < 2) { 
     631        secure_level = PJSIP_TRANSPORT_IS_SECURE(rdata->tp_info.transport)!=0; 
     632    } else { 
     633        char *uri; 
     634        int uri_len; 
     635        pj_str_t dst; 
     636 
     637        uri = pj_pool_alloc(rdata->tp_info.pool, PJSIP_MAX_URL_SIZE); 
     638        uri_len = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, 
     639                                  rdata->msg_info.msg->line.req.uri, 
     640                                  uri, PJSIP_MAX_URL_SIZE); 
     641        if (uri_len < 1) { 
     642            pjsua_perror(THIS_FILE, "Error analyzing dst URI",  
     643                         PJSIP_EURITOOLONG); 
     644            uri_len = 0; 
     645        } 
     646 
     647        dst.ptr = uri; 
     648        dst.slen = uri_len; 
     649 
     650        secure_level = get_secure_level(&dst); 
     651    } 
    582652 
    583653    /* Init media channel */ 
    584     status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAS); 
     654    status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAS,  
     655                                      secure_level); 
    585656    if (status != PJ_SUCCESS) { 
    586657        pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL, 
     
    618689    } 
    619690 
    620     /*  
    621      * Get which account is most likely to be associated with this incoming 
    622      * call. We need the account to find which contact URI to put for 
    623      * the call. 
    624      */ 
    625     acc_id = call->acc_id = pjsua_acc_find_for_incoming(rdata); 
    626  
    627691    /* Verify that we can handle the request. */ 
    628692    options |= PJSIP_INV_SUPPORT_100REL; 
     
    13211385 
    13221386    /* Init media channel */ 
    1323     status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC); 
     1387    status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC, 
     1388                                      get_secure_level(&dlg->remote.info_str)); 
    13241389    if (status != PJ_SUCCESS) { 
    13251390        pjsua_perror(THIS_FILE, "Error initializing media channel", status); 
     
    13901455 
    13911456    /* Init media channel */ 
    1392     status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC); 
     1457    status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC, 
     1458                                      get_secure_level(&dlg->remote.info_str)); 
    13931459    if (status != PJ_SUCCESS) { 
    13941460        pjsua_perror(THIS_FILE, "Error initializing media channel", status); 
     
    25092575        status = create_inactive_sdp( call, &answer ); 
    25102576    } else { 
     2577        int secure_level; 
    25112578 
    25122579        PJ_LOG(4,(THIS_FILE, "Call %d: received updated media offer", 
     
    25142581 
    25152582        /* Init media channel */ 
    2516         status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAS); 
     2583        secure_level = get_secure_level(&call->inv->dlg->remote.info_str); 
     2584        status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAS, 
     2585                                          secure_level); 
    25172586        if (status != PJ_SUCCESS) { 
    25182587            pjsua_perror(THIS_FILE, "Error initializing media channel", status); 
     
    25622631        status = create_inactive_sdp( call, offer ); 
    25632632    } else { 
     2633        int secure_level; 
    25642634 
    25652635        PJ_LOG(4,(THIS_FILE, "Call %d: asked to send a new offer", 
     
    25672637 
    25682638        /* Init media channel */ 
    2569         status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC); 
     2639        secure_level = get_secure_level(&call->inv->dlg->remote.info_str); 
     2640        status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC, 
     2641                                          secure_level); 
    25702642        if (status != PJ_SUCCESS) { 
    25712643            pjsua_perror(THIS_FILE, "Error initializing media channel", status); 
  • pjproject/branches/users/nanang/pjsip/src/pjsua-lib/pjsua_core.c

    r1675 r1709  
    143143    cfg->ka_interval = 15; 
    144144    cfg->ka_data = pj_str("\r\n"); 
     145    cfg->use_srtp = PJMEDIA_SRTP_DISABLED; 
     146    cfg->srtp_secure_signaling = 0; 
    145147} 
    146148 
  • pjproject/branches/users/nanang/pjsip/src/pjsua-lib/pjsua_media.c

    r1700 r1709  
    536536        } 
    537537 
    538         if (pjsua_var.media_cfg.enable_srtp) { 
    539             pjmedia_transport *tp; 
    540             unsigned srtp_options = 0; 
    541  
    542             status = pjmedia_transport_udp_attach(pjsua_var.med_endpt, NULL, 
    543                                                   &skinfo, 0, &tp); 
    544             status = pjmedia_transport_srtp_create(pjsua_var.med_endpt, tp, 
    545                                                    srtp_options,  
    546                                                    &pjsua_var.calls[i].med_tp); 
    547         } else { 
    548             status = pjmedia_transport_udp_attach(pjsua_var.med_endpt, NULL, 
    549                                                   &skinfo, 0, 
    550                                                   &pjsua_var.calls[i].med_tp); 
    551         } 
     538        status = pjmedia_transport_udp_attach(pjsua_var.med_endpt, NULL, 
     539                                              &skinfo, 0, 
     540                                              &pjsua_var.calls[i].med_tp); 
    552541        if (status != PJ_SUCCESS) { 
    553542            pjsua_perror(THIS_FILE, "Unable to create media transport", 
     
    757746 
    758747pj_status_t pjsua_media_channel_init(pjsua_call_id call_id, 
    759                                      pjsip_role_e role) 
     748                                     pjsip_role_e role, 
     749                                     int security_level) 
    760750{ 
    761751    pjsua_call *call = &pjsua_var.calls[call_id]; 
    762  
    763     if (pjsua_var.media_cfg.enable_ice) { 
    764         pj_ice_sess_role ice_role; 
    765         pj_status_t status; 
    766  
    767         ice_role = (role==PJSIP_ROLE_UAC ? PJ_ICE_SESS_ROLE_CONTROLLING :  
    768                                            PJ_ICE_SESS_ROLE_CONTROLLED); 
    769  
    770         /* Restart ICE */ 
    771         pjmedia_transport_media_stop(call->med_tp); 
    772  
    773         status = pjmedia_ice_init_ice(call->med_tp, ice_role, NULL, NULL); 
    774         if (status != PJ_SUCCESS) 
    775             return status; 
    776     } 
    777  
    778     return PJ_SUCCESS; 
    779 } 
    780  
    781 pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,  
    782                                            pj_pool_t *pool, 
    783                                            const pjmedia_sdp_session *rem_sdp, 
    784                                            pjmedia_sdp_session **p_sdp) 
    785 { 
    786     pjmedia_sdp_session *sdp; 
    787     pjmedia_sock_info skinfo; 
    788     pjsua_call *call = &pjsua_var.calls[call_id]; 
    789     pj_status_t status; 
     752    pjsua_acc *acc = &pjsua_var.acc[call->acc_id]; 
     753    pjmedia_srtp_use use_srtp; 
     754 
     755    PJ_UNUSED_ARG(role); 
    790756 
    791757    /* Return error if media transport has not been created yet 
     
    796762    } 
    797763 
     764    /* Stop media transport (for good measure!) */ 
     765    pjmedia_transport_media_stop(call->med_tp); 
     766     
     767    /* See if we need to use SRTP */ 
     768    use_srtp = acc->cfg.use_srtp; 
     769    if (use_srtp != PJMEDIA_SRTP_DISABLED) { 
     770        pj_status_t status; 
     771        pjmedia_transport *srtp; 
     772 
     773        if (security_level < acc->cfg.srtp_secure_signaling) { 
     774            return PJSIP_ESESSIONINSECURE; 
     775        } 
     776 
     777        /* Create SRTP */ 
     778        status = pjmedia_transport_srtp_create(pjsua_var.med_endpt,  
     779                                               call->med_tp, 
     780                                               NULL, &srtp); 
     781        if (status != PJ_SUCCESS) 
     782            return status; 
     783 
     784        /* Set SRTP as current media transport */ 
     785        call->med_tp = srtp; 
     786    } 
     787 
     788    return PJ_SUCCESS; 
     789} 
     790 
     791pj_status_t pjsua_media_channel_create_sdp(pjsua_call_id call_id,  
     792                                           pj_pool_t *pool, 
     793                                           const pjmedia_sdp_session *rem_sdp, 
     794                                           pjmedia_sdp_session **p_sdp) 
     795{ 
     796    enum { MAX_MEDIA = 1, MEDIA_IDX = 0 }; 
     797    pjmedia_sdp_session *sdp; 
     798    pjmedia_sock_info skinfo; 
     799    pjsua_call *call = &pjsua_var.calls[call_id]; 
     800    pj_status_t status; 
     801 
     802    /* Return error if media transport has not been created yet 
     803     * (e.g. application is starting) 
     804     */ 
     805    if (call->med_tp == NULL) { 
     806        return PJ_EBUSY; 
     807    } 
     808 
    798809    /* Get media socket info */ 
    799810    pjmedia_transport_get_info(call->med_tp, &skinfo); 
    800811 
    801812    /* Create SDP */ 
    802     status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pool, 1, 
     813    status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pool, MAX_MEDIA, 
    803814                                      &skinfo, &sdp); 
    804815    if (status != PJ_SUCCESS) 
     
    829840    } 
    830841 
    831     //if (pjsua_var.media_cfg.enable_ice) { 
    832         //status = pjmedia_transport_media_create(call->med_tp, pool, sdp, NULL); 
    833         status = pjmedia_transport_media_create(call->med_tp, pool,  
    834                                                 sdp, rem_sdp); 
    835         if (status != PJ_SUCCESS) 
    836             goto on_error; 
    837     //} 
     842    /* Give the SDP to media transport */ 
     843    status = pjmedia_transport_media_create(call->med_tp, pool,  
     844                                            sdp, rem_sdp, MEDIA_IDX); 
     845    if (status != PJ_SUCCESS) 
     846        goto on_error; 
    838847 
    839848    *p_sdp = sdp; 
     
    874883    stop_media_session(call_id); 
    875884 
    876     //if (pjsua_var.media_cfg.enable_ice) { 
    877         pjmedia_transport_media_stop(call->med_tp); 
    878     //} 
    879  
     885    pjmedia_transport_media_stop(call->med_tp); 
     886 
     887    if (call->med_tp != call->med_orig) { 
     888        pjmedia_transport_close(call->med_tp); 
     889        call->med_tp = call->med_orig; 
     890    } 
    880891    return PJ_SUCCESS; 
    881892} 
     
    962973        call->media_dir = PJMEDIA_DIR_NONE; 
    963974 
    964         /* Shutdown ICE session */ 
    965         //if (pjsua_var.media_cfg.enable_ice) { 
    966             pjmedia_transport_media_stop(call->med_tp); 
    967         //} 
     975        /* Shutdown transport's session */ 
     976        pjmedia_transport_media_stop(call->med_tp); 
    968977 
    969978        /* No need because we need keepalive? */ 
    970979 
    971980    } else { 
    972         /* Start ICE */ 
    973         //if (pjsua_var.media_cfg.enable_ice) { 
    974             status = pjmedia_transport_media_start(call->med_tp,  
    975                                                    call->inv->pool, 
    976                                                    local_sdp, remote_sdp, 0); 
    977             if (status != PJ_SUCCESS) 
    978                 return status; 
    979         //} 
     981        /* Start media transport */ 
     982        status = pjmedia_transport_media_start(call->med_tp,  
     983                                               call->inv->pool, 
     984                                               local_sdp, remote_sdp, 0); 
     985        if (status != PJ_SUCCESS) 
     986            return status; 
    980987 
    981988        /* Override ptime, if this option is specified. */ 
Note: See TracChangeset for help on using the changeset viewer.