Changeset 3117 for pjproject/trunk/pjlib/src/pjlib-test/ssl_sock.c
- Timestamp:
- Mar 6, 2010 2:04:52 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjlib/src/pjlib-test/ssl_sock.c
r3110 r3117 69 69 { 70 70 pj_pool_t *pool; /* pool */ 71 pj_ioqueue_t *ioqueue; /* ioqueue */ 71 72 pj_bool_t is_server; /* server role flag */ 72 73 pj_bool_t is_verbose; /* verbose flag, e.g: cert info */ … … 743 744 return PJ_FALSE; 744 745 } 746 747 return PJ_TRUE; 748 } 749 750 static pj_bool_t asock_on_accept_complete(pj_activesock_t *asock, 751 pj_sock_t newsock, 752 const pj_sockaddr_t *src_addr, 753 int src_addr_len) 754 { 755 struct test_state *st; 756 void *read_buf[1]; 757 pj_activesock_t *new_asock; 758 pj_activesock_cb asock_cb = { 0 }; 759 pj_status_t status; 760 761 PJ_UNUSED_ARG(src_addr); 762 PJ_UNUSED_ARG(src_addr_len); 763 764 st = (struct test_state*) pj_activesock_get_user_data(asock); 765 766 asock_cb.on_data_read = &asock_on_data_read; 767 status = pj_activesock_create(st->pool, newsock, pj_SOCK_STREAM(), NULL, 768 st->ioqueue, &asock_cb, st, &new_asock); 769 if (status != PJ_SUCCESS) { 770 goto on_return; 771 } 772 773 /* Start reading data */ 774 read_buf[0] = st->read_buf; 775 status = pj_activesock_start_read2(new_asock, st->pool, 776 sizeof(st->read_buf), 777 (void**)read_buf, 0); 778 if (status != PJ_SUCCESS) { 779 app_perror("...ERROR pj_ssl_sock_start_read2()", status); 780 } 781 782 on_return: 783 st->err = status; 784 785 if (st->err != PJ_SUCCESS) 786 pj_activesock_close(new_asock); 745 787 746 788 return PJ_TRUE; … … 904 946 905 947 948 /* SSL socket try to connect to raw TCP socket server, once 949 * connection established, SSL socket will try to perform SSL 950 * handshake. SSL client socket should be able to close the 951 * connection after specified timeout period (set ms_timeout to 952 * 0 to disable timer). 953 */ 954 static int server_non_ssl(unsigned ms_timeout) 955 { 956 pj_pool_t *pool = NULL; 957 pj_ioqueue_t *ioqueue = NULL; 958 pj_timer_heap_t *timer = NULL; 959 pj_activesock_t *asock_serv = NULL; 960 pj_ssl_sock_t *ssock_cli = NULL; 961 pj_activesock_cb asock_cb = { 0 }; 962 pj_sock_t sock = PJ_INVALID_SOCKET; 963 pj_ssl_sock_param param; 964 struct test_state state_serv = { 0 }; 965 struct test_state state_cli = { 0 }; 966 pj_sockaddr addr, listen_addr; 967 pj_status_t status; 968 969 pool = pj_pool_create(mem, "ssl_connect_raw_tcp", 256, 256, NULL); 970 971 status = pj_ioqueue_create(pool, 4, &ioqueue); 972 if (status != PJ_SUCCESS) { 973 goto on_return; 974 } 975 976 status = pj_timer_heap_create(pool, 4, &timer); 977 if (status != PJ_SUCCESS) { 978 goto on_return; 979 } 980 981 /* SERVER */ 982 state_serv.pool = pool; 983 state_serv.ioqueue = ioqueue; 984 985 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_STREAM(), 0, &sock); 986 if (status != PJ_SUCCESS) { 987 goto on_return; 988 } 989 990 /* Init bind address */ 991 { 992 pj_str_t tmp_st; 993 pj_sockaddr_init(PJ_AF_INET, &listen_addr, pj_strset2(&tmp_st, "127.0.0.1"), 0); 994 } 995 996 status = pj_sock_bind(sock, (pj_sockaddr_t*)&listen_addr, 997 pj_sockaddr_get_len((pj_sockaddr_t*)&listen_addr)); 998 if (status != PJ_SUCCESS) { 999 goto on_return; 1000 } 1001 1002 status = pj_sock_listen(sock, PJ_SOMAXCONN); 1003 if (status != PJ_SUCCESS) { 1004 goto on_return; 1005 } 1006 1007 asock_cb.on_accept_complete = &asock_on_accept_complete; 1008 status = pj_activesock_create(pool, sock, pj_SOCK_STREAM(), NULL, 1009 ioqueue, &asock_cb, &state_serv, &asock_serv); 1010 if (status != PJ_SUCCESS) { 1011 goto on_return; 1012 } 1013 1014 status = pj_activesock_start_accept(asock_serv, pool); 1015 if (status != PJ_SUCCESS) 1016 goto on_return; 1017 1018 /* Update listener address */ 1019 { 1020 int addr_len; 1021 1022 addr_len = sizeof(listen_addr); 1023 pj_sock_getsockname(sock, (pj_sockaddr_t*)&listen_addr, &addr_len); 1024 } 1025 1026 /* CLIENT */ 1027 pj_ssl_sock_param_default(¶m); 1028 param.cb.on_connect_complete = &ssl_on_connect_complete; 1029 param.cb.on_data_read = &ssl_on_data_read; 1030 param.cb.on_data_sent = &ssl_on_data_sent; 1031 param.ioqueue = ioqueue; 1032 param.timer_heap = timer; 1033 param.timeout.sec = 0; 1034 param.timeout.msec = ms_timeout; 1035 pj_time_val_normalize(¶m.timeout); 1036 param.user_data = &state_cli; 1037 1038 state_cli.pool = pool; 1039 state_cli.is_server = PJ_FALSE; 1040 state_cli.is_verbose = PJ_TRUE; 1041 1042 status = pj_ssl_sock_create(pool, ¶m, &ssock_cli); 1043 if (status != PJ_SUCCESS) { 1044 goto on_return; 1045 } 1046 1047 /* Init default bind address */ 1048 { 1049 pj_str_t tmp_st; 1050 pj_sockaddr_init(PJ_AF_INET, &addr, pj_strset2(&tmp_st, "127.0.0.1"), 0); 1051 } 1052 1053 status = pj_ssl_sock_start_connect(ssock_cli, pool, 1054 (pj_sockaddr_t*)&addr, 1055 (pj_sockaddr_t*)&listen_addr, 1056 pj_sockaddr_get_len(&listen_addr)); 1057 if (status != PJ_EPENDING) { 1058 goto on_return; 1059 } 1060 1061 /* Wait until everything has been sent/received or error */ 1062 while ((!state_serv.err && !state_serv.done) || (!state_cli.err && !state_cli.done)) 1063 { 1064 #ifdef PJ_SYMBIAN 1065 pj_symbianos_poll(-1, 1000); 1066 #else 1067 pj_time_val delay = {0, 100}; 1068 pj_ioqueue_poll(ioqueue, &delay); 1069 pj_timer_heap_poll(timer, &delay); 1070 #endif 1071 } 1072 1073 if (state_serv.err || state_cli.err) { 1074 if (state_cli.err != PJ_SUCCESS) 1075 status = state_cli.err; 1076 else 1077 status = state_serv.err; 1078 1079 goto on_return; 1080 } 1081 1082 PJ_LOG(3, ("", "...Done!")); 1083 1084 on_return: 1085 if (asock_serv) 1086 pj_activesock_close(asock_serv); 1087 if (ssock_cli && !state_cli.err && !state_cli.done) 1088 pj_ssl_sock_close(ssock_cli); 1089 if (timer) 1090 pj_timer_heap_destroy(timer); 1091 if (ioqueue) 1092 pj_ioqueue_destroy(ioqueue); 1093 if (pool) 1094 pj_pool_release(pool); 1095 1096 return status; 1097 } 1098 1099 906 1100 /* Test will perform multiple clients trying to connect to single server. 907 1101 * Once SSL connection established, echo test will be performed. … … 1153 1347 1154 1348 #ifndef PJ_SYMBIAN 1349 1350 /* On Symbian platforms, SSL socket is implemented using CSecureSocket, 1351 * and it hasn't supported server mode, so exclude the following tests, 1352 * which require SSL server, for now. 1353 */ 1155 1354 1156 1355 PJ_LOG(3,("", "..echo test w/ TLSv1 and PJ_TLS_RSA_WITH_DES_CBC_SHA cipher")); … … 1196 1395 return ret; 1197 1396 1198 PJ_LOG(3,("", "..client non-SSL (handshake timeout 5 secs)"));1199 ret = client_non_ssl(5000);1200 if (ret != 0)1201 return ret;1202 1203 1397 PJ_LOG(3,("", "..performance test")); 1204 1398 ret = perf_test(PJ_IOQUEUE_MAX_HANDLES/2 - 1, 0); … … 1206 1400 return ret; 1207 1401 1402 PJ_LOG(3,("", "..client non-SSL (handshake timeout 5 secs)")); 1403 ret = client_non_ssl(5000); 1404 /* PJ_TIMEDOUT won't be returned as accepted socket is deleted silently */ 1405 if (ret != 0) 1406 return ret; 1407 1208 1408 #endif 1409 1410 PJ_LOG(3,("", "..server non-SSL (handshake timeout 5 secs)")); 1411 ret = server_non_ssl(5000); 1412 if (ret != PJ_ETIMEDOUT) 1413 return ret; 1209 1414 1210 1415 return 0;
Note: See TracChangeset
for help on using the changeset viewer.