- Timestamp:
- Jan 19, 2008 1:42:40 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/branches/users/nanang/pjmedia/src/pjmedia/transport_srtp.c
r1711 r1713 181 181 182 182 183 pj_str_t srtp_transport_getliberrstr(int err) 184 { 185 pj_str_t msg = {NULL, 0}; 186 183 const char* get_libsrtp_errstr(int err) 184 { 187 185 #if defined(PJ_HAS_ERROR_STRING) && (PJ_HAS_ERROR_STRING != 0) 188 186 static char *liberr[] = { 189 "nothing to report ", /* err_status_ok = 0, */ 190 "unspecified failure ", /* err_status_fail = 1, */ 191 "unsupported parameter ", /* err_status_bad_param = 2, */ 192 "couldn't allocate memory ", /* err_status_alloc_fail = 3, */ 193 "couldn't deallocate properly ", /* err_status_dealloc_fail = 4, */ 194 "couldn't initialize ", /* err_status_init_fail = 5, */ 195 "can't process as much data as requested ", /* err_status_terminus = 6, */ 196 "authentication failure ", /* err_status_auth_fail = 7, */ 197 "cipher failure ", /* err_status_cipher_fail = 8, */ 198 "replay check failed (bad index) ", /* err_status_replay_fail = 9, */ 199 "replay check failed (index too old) ", /* err_status_replay_old = 10, */ 200 "algorithm failed test routine ", /* err_status_algo_fail = 11, */ 201 "unsupported operation ", /* err_status_no_such_op = 12, */ 202 "no appropriate context found ", /* err_status_no_ctx = 13, */ 203 "unable to perform desired validation ", /* err_status_cant_check = 14, */ 204 "can't use key any more ", /* err_status_key_expired = 15, */ 205 "error in use of socket ", /* err_status_socket_err = 16, */ 206 "error in use POSIX signals ", /* err_status_signal_err = 17, */ 207 "nonce check failed ", /* err_status_nonce_bad = 18, */ 208 "couldn't read data ", /* err_status_read_fail = 19, */ 209 "couldn't write data ", /* err_status_write_fail = 20, */ 210 "error pasring data ", /* err_status_parse_err = 21, */ 211 "error encoding data ", /* err_status_encode_err = 22, */ 212 "error while using semaphores ", /* err_status_semaphore_err = 23, */ 213 "error while using pfkey " /* err_status_pfkey_err = 24, */ 187 "ok", /* err_status_ok = 0 */ 188 "unspecified failure", /* err_status_fail = 1 */ 189 "unsupported parameter", /* err_status_bad_param = 2 */ 190 "couldn't allocate memory", /* err_status_alloc_fail = 3 */ 191 "couldn't deallocate properly", /* err_status_dealloc_fail = 4 */ 192 "couldn't initialize", /* err_status_init_fail = 5 */ 193 "can't process as much data as requested", 194 /* err_status_terminus = 6 */ 195 "authentication failure", /* err_status_auth_fail = 7 */ 196 "cipher failure", /* err_status_cipher_fail = 8 */ 197 "replay check failed (bad index)", /* err_status_replay_fail = 9 */ 198 "replay check failed (index too old)", 199 /* err_status_replay_old = 10 */ 200 "algorithm failed test routine", /* err_status_algo_fail = 11 */ 201 "unsupported operation", /* err_status_no_such_op = 12 */ 202 "no appropriate context found", /* err_status_no_ctx = 13 */ 203 "unable to perform desired validation", 204 /* err_status_cant_check = 14 */ 205 "can't use key any more", /* err_status_key_expired = 15 */ 206 "error in use of socket", /* err_status_socket_err = 16 */ 207 "error in use POSIX signals", /* err_status_signal_err = 17 */ 208 "nonce check failed", /* err_status_nonce_bad = 18 */ 209 "couldn't read data", /* err_status_read_fail = 19 */ 210 "couldn't write data", /* err_status_write_fail = 20 */ 211 "error pasring data", /* err_status_parse_err = 21 */ 212 "error encoding data", /* err_status_encode_err = 22 */ 213 "error while using semaphores", /* err_status_semaphore_err = 23 */ 214 "error while using pfkey" /* err_status_pfkey_err = 24 */ 214 215 }; 215 if (err >= 0 && err <= 24) { 216 msg.ptr = liberr[err]; 217 msg.slen = 40; 218 } 216 return liberr[err]; 217 #else 218 return NULL; 219 219 #endif 220 221 return msg;222 220 } 223 221 … … 235 233 236 234 initialized = PJ_TRUE; 237 238 PJ_TODO(REGISTER_LIBSRTP_ERROR_CODES_AND_MESSAGES);239 235 } 240 236 … … 247 243 int cs_cnt = sizeof(crypto_suites)/sizeof(crypto_suites[0]); 248 244 245 if (crypto_name->slen == 0) 246 return 0; 247 249 248 for (i=0; i<cs_cnt; ++i) { 250 249 if (!pj_stricmp2(crypto_name, crypto_suites[i].name)) … … 284 283 PJ_ASSERT_RETURN(endpt && p_tp, PJ_EINVAL); 285 284 285 /* Check crypto availability */ 286 if (opt->crypto_count == 0 && 287 opt->use == PJMEDIA_SRTP_MANDATORY) 288 return PJMEDIA_SRTP_ESDPREQCRYPTO; 289 290 /* Check crypto */ 291 if (opt->use != PJMEDIA_SRTP_DISABLED) { 292 for (i=0; i < opt->crypto_count; ++i) { 293 int cs_idx = get_crypto_idx(&opt->crypto[i].name); 294 295 /* check crypto name */ 296 if (cs_idx == -1) 297 return PJMEDIA_SRTP_ENOTSUPCRYPTO; 298 299 /* check key length */ 300 if (opt->crypto[i].key.slen && 301 opt->crypto[i].key.slen < crypto_suites[cs_idx].cipher_key_len) 302 return PJMEDIA_SRTP_EINKEYLEN; 303 } 304 } 305 286 306 /* Init libsrtp. */ 287 307 status = pjmedia_srtp_init_lib(); … … 294 314 srtp->pool = pool; 295 315 srtp->session_inited = PJ_FALSE; 296 srtp->bypass_srtp = PJ_ TRUE;316 srtp->bypass_srtp = PJ_FALSE; 297 317 298 318 if (opt) { 299 319 srtp->setting = *opt; 300 for (i=0; i < opt->crypto_count; ++i) { 320 if (opt->use == PJMEDIA_SRTP_DISABLED) 321 srtp->setting.crypto_count = 0; 322 323 for (i=0; i < srtp->setting.crypto_count; ++i) { 301 324 int cs_idx = get_crypto_idx(&opt->crypto[i].name); 302 if (cs_idx == -1)303 return PJMEDIA_SRTP_ENOTSUPCRYPTO; 304 325 pj_str_t tmp_key = opt->crypto[i].key; 326 327 /* re-set crypto */ 305 328 srtp->setting.crypto[i].name = pj_str(crypto_suites[cs_idx].name); 306 pj_strdup(pool, &srtp->setting.crypto[i].key, &opt->crypto[i].key); 329 /* cut key length */ 330 tmp_key.slen = crypto_suites[cs_idx].cipher_key_len; 331 pj_strdup(pool, &srtp->setting.crypto[i].key, &tmp_key); 307 332 } 308 333 } else { 309 334 pjmedia_srtp_setting_default(&srtp->setting); 310 335 } 311 312 if (srtp->setting.crypto_count==0 &&313 srtp->setting.use == PJMEDIA_SRTP_MANDATORY)314 return PJMEDIA_SRTP_ESDPREQCRYPTO;315 336 316 337 status = pj_lock_create_null_mutex(pool, pool->obj_name, &srtp->mutex); … … 376 397 if (cr_tx_idx == -1 || cr_rx_idx == -1 || au_tx_idx == -1 || au_rx_idx == -1) 377 398 return PJMEDIA_SRTP_ENOTSUPCRYPTO; 399 400 if (cr_tx_idx == 0 && cr_rx_idx == 0 && au_tx_idx == 0 && au_rx_idx == 0) { 401 srtp->bypass_srtp = PJ_TRUE; 402 return PJ_SUCCESS; 403 } 378 404 379 405 /* Init transmit direction */ … … 429 455 /* Declare SRTP session initialized */ 430 456 srtp->session_inited = PJ_TRUE; 431 srtp->bypass_srtp = PJ_FALSE;432 457 433 458 PJ_LOG(5, (THIS_FILE, "TX: %s key=%s", srtp->tx_policy.name.ptr, … … 545 570 546 571 if (srtp->bypass_srtp) 547 return pjmedia_transport_send_rtp(srtp->real_tp, srtp->tx_buffer, len);572 return pjmedia_transport_send_rtp(srtp->real_tp, pkt, size); 548 573 549 574 if (!srtp->session_inited) … … 578 603 579 604 if (srtp->bypass_srtp) 580 return pjmedia_transport_send_rtp(srtp->real_tp, srtp->tx_buffer, len);605 return pjmedia_transport_send_rtp(srtp->real_tp, pkt, size); 581 606 582 607 if (!srtp->session_inited) … … 642 667 643 668 if (srtp->bypass_srtp) { 644 srtp->rtp_cb(srtp->user_data, srtp->rx_buffer, len);669 srtp->rtp_cb(srtp->user_data, pkt, size); 645 670 return; 646 671 } … … 674 699 675 700 if (srtp->bypass_srtp) { 676 srtp->rtcp_cb(srtp->user_data, srtp->rx_buffer, len);701 srtp->rtcp_cb(srtp->user_data, pkt, size); 677 702 return; 678 703 } … … 746 771 } 747 772 748 if ( (unsigned)crypto->key.slen !=crypto_suites[cs_idx].cipher_key_len)773 if (crypto->key.slen != (int)crypto_suites[cs_idx].cipher_key_len) 749 774 return PJMEDIA_SRTP_EINKEYLEN; 750 775 … … 759 784 b64_key[b64_key_len] = '\0'; 760 785 761 PJ_ASSERT_RETURN( (unsigned)*buffer_len >= (crypto->name.slen + \786 PJ_ASSERT_RETURN(*buffer_len >= (crypto->name.slen + \ 762 787 b64_key_len + 16), PJ_ETOOSMALL); 763 788 … … 959 984 &srtp->setting.crypto[j].name) == 0) 960 985 { 986 int cs_idx = get_crypto_idx(&tmp_rx_crypto.name); 987 988 if (tmp_rx_crypto.key.slen != 989 (int)crypto_suites[cs_idx].cipher_key_len) 990 return PJMEDIA_SRTP_EINKEYLEN; 991 961 992 srtp->tx_policy = srtp->setting.crypto[j]; 962 993 srtp->rx_policy = tmp_rx_crypto; … … 1036 1067 /* bypass if media transport is not RTP/AVP or RTP/SAVP */ 1037 1068 if (pj_stricmp(&m_loc->desc.transport, &ID_RTP_AVP) != 0 && 1038 pj_stricmp(&m_loc->desc.transport, &ID_RTP_SAVP) != 0) 1069 pj_stricmp(&m_loc->desc.transport, &ID_RTP_SAVP) != 0) 1070 { 1071 srtp->bypass_srtp = PJ_TRUE; 1039 1072 goto PROPAGATE_MEDIA_START; 1073 } 1040 1074 1041 1075 /* If the media is inactive, do nothing. */ … … 1043 1077 (m_rem && pjmedia_sdp_media_find_attr(m_rem, &ID_INACTIVE, NULL))) 1044 1078 { 1079 srtp->bypass_srtp = PJ_TRUE; 1045 1080 goto PROPAGATE_MEDIA_START; 1046 1081 } … … 1057 1092 return PJMEDIA_SRTP_ESDPINCRYPTO; 1058 1093 } 1059 goto PROPAGATE_MEDIA_START;; 1094 srtp->bypass_srtp = PJ_TRUE; 1095 goto PROPAGATE_MEDIA_START; 1060 1096 } else if (srtp->setting.use == PJMEDIA_SRTP_OPTIONAL) { 1061 1097 if (pj_stricmp(&m_rem->desc.transport, &m_loc->desc.transport)) { … … 1073 1109 if (srtp->offerer_side) { 1074 1110 /* find supported crypto-suite, get the tag, and assign policy_local */ 1111 pjmedia_srtp_crypto tmp_tx_crypto; 1075 1112 pj_bool_t has_crypto_attr = PJ_FALSE; 1076 1113 int rem_tag; … … 1089 1126 1090 1127 status = parse_attr_crypto(pool, m_rem->attr[i], 1091 & srtp->rx_policy, &rem_tag);1128 &tmp_tx_crypto, &rem_tag); 1092 1129 if (status != PJ_SUCCESS) 1093 1130 return status; … … 1095 1132 1096 1133 /* our offer tag is always ordered by setting */ 1097 if (rem_tag <1 || rem_tag>srtp->setting.crypto_count) {1134 if (rem_tag < 1 || rem_tag > srtp->setting.crypto_count) { 1098 1135 DEACTIVATE_MEDIA(pool, m_loc); 1099 1136 return PJMEDIA_SRTP_ESDPINCRYPTOTAG; … … 1109 1146 1110 1147 srtp->tx_policy = srtp->setting.crypto[rem_tag-1]; 1148 srtp->rx_policy = tmp_tx_crypto; 1111 1149 } 1112 1150 1113 1151 if (srtp->setting.use == PJMEDIA_SRTP_DISABLED) { 1114 1152 /* should never reach here */ 1153 srtp->bypass_srtp = PJ_TRUE; 1115 1154 goto PROPAGATE_MEDIA_START; 1116 1155 } else if (srtp->setting.use == PJMEDIA_SRTP_OPTIONAL) { 1117 if (!has_crypto_attr) 1156 if (!has_crypto_attr) { 1157 srtp->bypass_srtp = PJ_TRUE; 1118 1158 goto PROPAGATE_MEDIA_START; 1159 } 1119 1160 } else if (srtp->setting.use == PJMEDIA_SRTP_MANDATORY) { 1120 1161 if (!has_crypto_attr) { … … 1145 1186 pj_status_t status; 1146 1187 1147 status = pjmedia_transport_srtp_stop(tp); 1148 if (status != PJ_SUCCESS) 1149 PJ_LOG(4, (THIS_FILE, "Failed deinit session.")); 1150 1151 return pjmedia_transport_media_stop(srtp->real_tp); 1152 } 1188 if (srtp->setting.close_member_tp) { 1189 status = pjmedia_transport_media_stop(srtp->real_tp); 1190 if (status != PJ_SUCCESS) 1191 PJ_LOG(4, (THIS_FILE, "Failed deinit session.")); 1192 } 1193 1194 return pjmedia_transport_srtp_stop(tp); 1195 }
Note: See TracChangeset
for help on using the changeset viewer.