Changeset 6004 for pjproject/trunk/pjnath/src/pjnath-test/server.c
- Timestamp:
- May 24, 2019 3:32:17 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjnath/src/pjnath-test/server.c
r5534 r6004 24 24 #define MAX_STUN_PKT 1500 25 25 #define TURN_NONCE "thenonce" 26 #define CERT_DIR "../../pjlib/build/" 27 #define CERT_CA_FILE CERT_DIR "cacert.pem" 28 #define CERT_FILE CERT_DIR "cacert.pem" 29 #define CERT_PRIVKEY_FILE CERT_DIR "privkey.pem" 30 #define CERT_PRIVKEY_PASS "" 26 31 27 32 static pj_bool_t stun_on_data_recvfrom(pj_activesock_t *asock, … … 31 36 int addr_len, 32 37 pj_status_t status); 33 static pj_bool_t turn_ on_data_recvfrom(pj_activesock_t *asock,38 static pj_bool_t turn_tcp_on_data_read(pj_activesock_t *asock, 34 39 void *data, 35 40 pj_size_t size, 36 const pj_sockaddr_t *src_addr, 37 int addr_len, 38 pj_status_t status); 41 pj_status_t status, 42 pj_size_t *remainder); 43 static pj_bool_t turn_tls_on_data_read(pj_ssl_sock_t *ssock, 44 void *data, 45 pj_size_t size, 46 pj_status_t status, 47 pj_size_t *remainder); 48 static pj_bool_t turn_udp_on_data_recvfrom(pj_activesock_t *asock, 49 void *data, 50 pj_size_t size, 51 const pj_sockaddr_t *src_addr, 52 int addr_len, 53 pj_status_t status); 54 static pj_bool_t turn_on_data_read(test_server *asock, 55 void *data, 56 pj_size_t size, 57 const pj_sockaddr_t *src_addr, 58 int addr_len, 59 pj_status_t status); 60 static pj_bool_t turn_tcp_on_accept_complete(pj_activesock_t *asock, 61 pj_sock_t newsock, 62 const pj_sockaddr_t *src_addr, 63 int src_addr_len, 64 pj_status_t status); 65 #if USE_TLS 66 static pj_bool_t turn_tls_on_accept_complete2(pj_ssl_sock_t *ssock, 67 pj_ssl_sock_t *newsock, 68 const pj_sockaddr_t *src_addr, 69 int src_addr_len, 70 pj_status_t status); 71 #endif 39 72 static pj_bool_t alloc_on_data_recvfrom(pj_activesock_t *asock, 40 73 void *data, … … 53 86 pj_sockaddr hostip; 54 87 char strbuf[100]; 55 pj_status_t status ;88 pj_status_t status = PJ_EINVAL; 56 89 pj_bool_t use_ipv6 = flags & SERVER_IPV6; 57 90 … … 169 202 170 203 if (flags & CREATE_TURN_SERVER) { 171 pj_activesock_cb turn_sock_cb;204 172 205 pj_sockaddr bound_addr; 173 174 pj_bzero(&turn_sock_cb, sizeof(turn_sock_cb)); 175 turn_sock_cb.on_data_recvfrom = &turn_on_data_recvfrom; 176 206 pj_turn_tp_type tp_type = get_turn_tp_type(flags); 207 177 208 pj_sockaddr_init(GET_AF(use_ipv6), &bound_addr, NULL, TURN_SERVER_PORT); 178 209 179 status = pj_activesock_create_udp(pool, &bound_addr, NULL, 180 test_srv->stun_cfg->ioqueue, 181 &turn_sock_cb, test_srv, 182 &test_srv->turn_sock, NULL); 183 if (status != PJ_SUCCESS) { 184 destroy_test_server(test_srv); 185 return status; 186 } 187 188 status = pj_activesock_start_recvfrom(test_srv->turn_sock, pool, 189 MAX_STUN_PKT, 0); 210 if (tp_type == PJ_TURN_TP_UDP) { 211 pj_activesock_cb turn_sock_cb; 212 213 pj_bzero(&turn_sock_cb, sizeof(turn_sock_cb)); 214 turn_sock_cb.on_data_recvfrom = &turn_udp_on_data_recvfrom; 215 216 status = pj_activesock_create_udp(pool, &bound_addr, NULL, 217 test_srv->stun_cfg->ioqueue, 218 &turn_sock_cb, test_srv, 219 &test_srv->turn_sock, NULL); 220 221 if (status != PJ_SUCCESS) { 222 destroy_test_server(test_srv); 223 return status; 224 } 225 226 status = pj_activesock_start_recvfrom(test_srv->turn_sock, pool, 227 MAX_STUN_PKT, 0); 228 } else if (tp_type == PJ_TURN_TP_TCP) { 229 pj_sock_t sock_fd; 230 pj_activesock_cb turn_sock_cb; 231 232 pj_bzero(&turn_sock_cb, sizeof(turn_sock_cb)); 233 turn_sock_cb.on_accept_complete2 = &turn_tcp_on_accept_complete; 234 status = pj_sock_socket(GET_AF(use_ipv6), pj_SOCK_STREAM(), 0, 235 &sock_fd); 236 if (status != PJ_SUCCESS) { 237 return status; 238 } 239 240 status = pj_sock_bind(sock_fd, &bound_addr, 241 pj_sockaddr_get_len(&bound_addr)); 242 if (status != PJ_SUCCESS) { 243 pj_sock_close(sock_fd); 244 return status; 245 } 246 247 status = pj_sock_listen(sock_fd, 4); 248 if (status != PJ_SUCCESS) { 249 pj_sock_close(sock_fd); 250 return status; 251 } 252 253 status = pj_activesock_create(pool, sock_fd, pj_SOCK_STREAM(), 254 NULL, 255 test_srv->stun_cfg->ioqueue, 256 &turn_sock_cb, test_srv, 257 &test_srv->turn_sock); 258 if (status != PJ_SUCCESS) { 259 pj_sock_close(sock_fd); 260 return status; 261 } 262 263 status = pj_activesock_start_accept(test_srv->turn_sock, 264 pool); 265 } 266 #if USE_TLS 267 else if (tp_type == PJ_TURN_TP_TLS) { 268 pj_ssl_sock_t *ssock_serv = NULL; 269 pj_ssl_sock_param ssl_param; 270 pj_ssl_cert_t *cert = NULL; 271 pj_str_t ca_file = pj_str(CERT_CA_FILE); 272 pj_str_t cert_file = pj_str(CERT_FILE); 273 pj_str_t privkey_file = pj_str(CERT_PRIVKEY_FILE); 274 pj_str_t privkey_pass = pj_str(CERT_PRIVKEY_PASS); 275 276 pj_ssl_sock_param_default(&ssl_param); 277 ssl_param.cb.on_accept_complete2 = &turn_tls_on_accept_complete2; 278 ssl_param.cb.on_data_read = &turn_tls_on_data_read; 279 ssl_param.ioqueue = test_srv->stun_cfg->ioqueue; 280 ssl_param.timer_heap = test_srv->stun_cfg->timer_heap; 281 ssl_param.user_data = test_srv; 282 ssl_param.sock_af = GET_AF(use_ipv6); 283 284 status = pj_ssl_sock_create(pool, &ssl_param, &ssock_serv); 285 if (status != PJ_SUCCESS) { 286 if (ssock_serv) 287 pj_ssl_sock_close(ssock_serv); 288 } 289 290 status = pj_ssl_cert_load_from_files(pool, &ca_file, &cert_file, 291 &privkey_file, &privkey_pass, 292 &cert); 293 if (status != PJ_SUCCESS) { 294 if (ssock_serv) 295 pj_ssl_sock_close(ssock_serv); 296 } 297 298 status = pj_ssl_sock_set_certificate(ssock_serv, pool, cert); 299 if (status != PJ_SUCCESS) { 300 if (ssock_serv) 301 pj_ssl_sock_close(ssock_serv); 302 } 303 test_srv->ssl_srv_sock = ssock_serv; 304 status = pj_ssl_sock_start_accept(ssock_serv, pool, &bound_addr, 305 pj_sockaddr_get_len(&bound_addr)); 306 } 307 #endif 190 308 if (status != PJ_SUCCESS) { 191 309 destroy_test_server(test_srv); … … 201 319 * turn.domain IN A 127.0.0.1 202 320 */ 203 pj_ansi_snprintf(strbuf, sizeof(strbuf), 204 "_turn._udp.%s", domain); 321 switch (tp_type) { 322 case PJ_TURN_TP_TCP: 323 pj_ansi_snprintf(strbuf, sizeof(strbuf), 324 "_turn._tcp.%s", domain); 325 break; 326 case PJ_TURN_TP_TLS: 327 pj_ansi_snprintf(strbuf, sizeof(strbuf), 328 "_turns._tcp.%s", domain); 329 break; 330 default: 331 pj_ansi_snprintf(strbuf, sizeof(strbuf), 332 "_turn._udp.%s", domain); 333 334 } 205 335 pj_strdup2(pool, &res_name, strbuf); 206 336 pj_ansi_snprintf(strbuf, sizeof(strbuf), … … 244 374 test_srv->turn_sock = NULL; 245 375 } 376 377 if (test_srv->cl_turn_sock) { 378 pj_activesock_close(test_srv->cl_turn_sock); 379 test_srv->cl_turn_sock = NULL; 380 } 381 382 #if USE_TLS 383 if (test_srv->ssl_srv_sock) { 384 pj_ssl_sock_close(test_srv->ssl_srv_sock); 385 test_srv->ssl_srv_sock = NULL; 386 } 387 if (test_srv->ssl_cl_sock) { 388 pj_ssl_sock_close(test_srv->ssl_cl_sock); 389 test_srv->ssl_cl_sock = NULL; 390 } 391 #endif 246 392 247 393 if (test_srv->stun_sock) { … … 350 496 } 351 497 352 353 static pj_bool_t turn_on_data_recvfrom(pj_activesock_t *asock, 498 static pj_bool_t turn_tcp_on_data_read(pj_activesock_t *asock, 354 499 void *data, 355 500 pj_size_t size, 356 const pj_sockaddr_t *src_addr, 357 int addr_len, 358 pj_status_t status) 501 pj_status_t status, 502 pj_size_t *remainder) 503 { 504 test_server *test_srv = (test_server *)pj_activesock_get_user_data(asock); 505 506 PJ_UNUSED_ARG(remainder); 507 return turn_on_data_read(test_srv, data, size, &test_srv->remote_addr, 508 sizeof(test_srv->remote_addr), status); 509 } 510 511 static pj_bool_t turn_tls_on_data_read(pj_ssl_sock_t *ssl_sock, 512 void *data, 513 pj_size_t size, 514 pj_status_t status, 515 pj_size_t *remainder) 516 { 517 test_server *test_srv = (test_server *)pj_ssl_sock_get_user_data(ssl_sock); 518 519 PJ_UNUSED_ARG(remainder); 520 return turn_on_data_read(test_srv, data, size, 521 &test_srv->remote_addr, 522 sizeof(test_srv->remote_addr), 523 status); 524 } 525 526 static pj_bool_t turn_udp_on_data_recvfrom(pj_activesock_t *asock, 527 void *data, 528 pj_size_t size, 529 const pj_sockaddr_t *src_addr, 530 int addr_len, 531 pj_status_t status) 359 532 { 360 533 test_server *test_srv; 534 test_srv = (test_server*) pj_activesock_get_user_data(asock); 535 return turn_on_data_read(test_srv, data, size, src_addr, addr_len, status); 536 } 537 538 static pj_bool_t turn_on_data_read(test_server *test_srv, 539 void *data, 540 pj_size_t size, 541 const pj_sockaddr_t *src_addr, 542 int addr_len, 543 pj_status_t status) 544 { 545 361 546 pj_pool_t *pool; 362 547 turn_allocation *alloc; … … 372 557 373 558 pj_sockaddr_print(src_addr, client_info, sizeof(client_info), 3); 374 375 test_srv = (test_server*) pj_activesock_get_user_data(asock); 559 376 560 use_ipv6 = test_srv->flags & SERVER_IPV6; 377 561 pool = pj_pool_create(test_srv->stun_cfg->pf, NULL, 512, 512, NULL); … … 383 567 } 384 568 385 386 if (pj_stun_msg_check((pj_uint8_t*)data, size, PJ_STUN_NO_FINGERPRINT_CHECK)!=PJ_SUCCESS) { 569 if (pj_stun_msg_check((pj_uint8_t*)data, size, 570 PJ_STUN_NO_FINGERPRINT_CHECK)!=PJ_SUCCESS) 571 { 387 572 /* Not STUN message, this probably is a ChannelData */ 388 573 pj_turn_channel_data cd; … … 703 888 send_pkt: 704 889 if (resp) { 890 pj_turn_tp_type tp_type = get_turn_tp_type(test_srv->flags); 891 705 892 status = pj_stun_msg_encode(resp, (pj_uint8_t*)data, MAX_STUN_PKT, 706 893 0, &auth_key, &size); … … 709 896 710 897 len = size; 711 status = pj_activesock_sendto(asock, &test_srv->send_key, data, &len, 712 0, src_addr, addr_len); 898 switch (tp_type) { 899 case PJ_TURN_TP_TCP: 900 status = pj_activesock_send(test_srv->cl_turn_sock, 901 &test_srv->send_key, data, &len, 0); 902 break; 903 #if USE_TLS 904 case PJ_TURN_TP_TLS: 905 status = pj_ssl_sock_send(test_srv->ssl_cl_sock, 906 &test_srv->send_key, data, &len, 0); 907 break; 908 #endif 909 default: 910 status = pj_activesock_sendto(test_srv->turn_sock, 911 &test_srv->send_key, data, 912 &len, 0, src_addr, addr_len); 913 } 713 914 } 714 915 … … 717 918 return PJ_TRUE; 718 919 } 920 921 static pj_bool_t turn_tcp_on_accept_complete(pj_activesock_t *asock, 922 pj_sock_t newsock, 923 const pj_sockaddr_t *src_addr, 924 int src_addr_len, 925 pj_status_t status) 926 { 927 pj_status_t sstatus; 928 pj_activesock_cb asock_cb; 929 test_server *test_srv = (test_server *) pj_activesock_get_user_data(asock); 930 931 PJ_UNUSED_ARG(src_addr_len); 932 933 if (status != PJ_SUCCESS && status != PJ_EPENDING) { 934 return PJ_FALSE; 935 } 936 937 pj_sockaddr_cp(&test_srv->remote_addr, src_addr); 938 pj_bzero(&asock_cb, sizeof(asock_cb)); 939 asock_cb.on_data_read = &turn_tcp_on_data_read; 940 941 sstatus = pj_activesock_create(test_srv->pool, newsock, pj_SOCK_STREAM(), 942 NULL, test_srv->stun_cfg->ioqueue, 943 &asock_cb, test_srv, 944 &test_srv->cl_turn_sock); 945 if (sstatus != PJ_SUCCESS) { 946 goto on_exit; 947 } 948 949 sstatus = pj_activesock_start_read(test_srv->cl_turn_sock, 950 test_srv->pool, MAX_STUN_PKT, 0); 951 if (sstatus != PJ_SUCCESS) { 952 goto on_exit; 953 } 954 955 pj_ioqueue_op_key_init(&test_srv->send_key, sizeof(test_srv->send_key)); 956 957 return PJ_TRUE; 958 959 on_exit: 960 if (test_srv->cl_turn_sock) 961 pj_activesock_close(test_srv->turn_sock); 962 else 963 pj_sock_close(newsock); 964 965 return PJ_FALSE; 966 967 } 968 969 #if USE_TLS 970 static pj_bool_t turn_tls_on_accept_complete2(pj_ssl_sock_t *ssock, 971 pj_ssl_sock_t *newsock, 972 const pj_sockaddr_t *src_addr, 973 int src_addr_len, 974 pj_status_t status) 975 { 976 pj_status_t sstatus; 977 test_server *test_srv = (test_server *) pj_ssl_sock_get_user_data(ssock); 978 979 PJ_UNUSED_ARG(src_addr_len); 980 981 if (status != PJ_SUCCESS && status != PJ_EPENDING) { 982 return PJ_FALSE; 983 } 984 985 pj_ssl_sock_set_user_data(newsock, test_srv); 986 pj_sockaddr_cp(&test_srv->remote_addr, src_addr); 987 test_srv->ssl_cl_sock = newsock; 988 989 sstatus = pj_ssl_sock_start_read(newsock, test_srv->pool, MAX_STUN_PKT, 0); 990 if (sstatus != PJ_SUCCESS) { 991 pj_ssl_sock_close(newsock); 992 test_srv->ssl_cl_sock = NULL; 993 994 } 995 return PJ_TRUE; 996 } 997 #endif 719 998 720 999 /* On received data from peer */
Note: See TracChangeset
for help on using the changeset viewer.