Changeset 3117


Ignore:
Timestamp:
Mar 6, 2010 2:04:52 AM (14 years ago)
Author:
nanang
Message:

Ticket #1043:

  • Fixed bug of unused timeout setting in Symbian SSL socket, ssl_sock_symbian.cpp.
  • Added an SSL test scenario of SSL connect timeout, SSL socket client tries to connect to non-SSL socket server.
  • Fixed OpenSSL-based SSL socket to start SSL timer before TCP connect (was started after TCP connected and before SSL handshake).
Location:
pjproject/trunk/pjlib/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/src/pj/ssl_sock_ossl.c

    r3106 r3117  
    11661166    switch (timer_id) { 
    11671167    case TIMER_HANDSHAKE_TIMEOUT: 
    1168         PJ_LOG(1,(ssock->pool->obj_name, "SSL handshake timeout after %d.%ds", 
     1168        PJ_LOG(1,(ssock->pool->obj_name, "SSL timeout after %d.%ds", 
    11691169                  ssock->param.timeout.sec, ssock->param.timeout.msec)); 
    11701170 
     
    16071607    ssock->write_state.start = ssock->write_state.buf; 
    16081608    ssock->write_state.len = 0; 
    1609  
    1610     /* Start handshake timer */ 
    1611     if (ssock->param.timer_heap && (ssock->param.timeout.sec != 0 || 
    1612         ssock->param.timeout.msec != 0)) 
    1613     { 
    1614         pj_assert(ssock->timer.id == TIMER_NONE); 
    1615         ssock->timer.id = TIMER_HANDSHAKE_TIMEOUT; 
    1616         status = pj_timer_heap_schedule(ssock->param.timer_heap, 
    1617                                         &ssock->timer, 
    1618                                         &ssock->param.timeout); 
    1619         if (status != PJ_SUCCESS) 
    1620             ssock->timer.id = TIMER_NONE; 
    1621     } 
    16221609 
    16231610#ifdef SSL_set_tlsext_host_name 
     
    22912278    pj_sockaddr_cp(&ssock->rem_addr, remaddr); 
    22922279 
     2280    /* Start timer */ 
     2281    if (ssock->param.timer_heap && (ssock->param.timeout.sec != 0 || 
     2282        ssock->param.timeout.msec != 0)) 
     2283    { 
     2284        pj_assert(ssock->timer.id == TIMER_NONE); 
     2285        ssock->timer.id = TIMER_HANDSHAKE_TIMEOUT; 
     2286        status = pj_timer_heap_schedule(ssock->param.timer_heap, 
     2287                                        &ssock->timer, 
     2288                                        &ssock->param.timeout); 
     2289        if (status != PJ_SUCCESS) 
     2290            ssock->timer.id = TIMER_NONE; 
     2291    } 
     2292 
    22932293    status = pj_activesock_start_connect(ssock->asock, pool, remaddr, 
    22942294                                         addr_len); 
  • pjproject/trunk/pjlib/src/pj/ssl_sock_symbian.cpp

    r3110 r3117  
    172172        reader_ = NULL; 
    173173        if (securesock_) { 
    174             securesock_->Close(); 
     174            if (state_ == SSL_STATE_ESTABLISHED) 
     175                securesock_->Close(); 
    175176            delete securesock_; 
    176177            securesock_ = NULL; 
     
    214215    rem_addr_ = rem_addr; 
    215216    servername_.Set(servername); 
    216     state_ = SSL_STATE_CONNECTING; 
    217  
    218217    rSock.Connect(rem_addr_, iStatus); 
    219218    SetActive(); 
     219    state_ = SSL_STATE_CONNECTING; 
    220220     
    221221    rSock.LocalName(local_addr_); 
     
    277277        { 
    278278            RSocket &rSock = ((CPjSocket*)sock_)->Socket(); 
     279 
    279280            rSock.CancelConnect(); 
    280              
    281281            CleanupSubObjects(); 
    282  
    283282            state_ = SSL_STATE_NULL; 
    284283        } 
     
    287286        { 
    288287            securesock_->CancelHandshake(); 
    289             securesock_->Close(); 
    290              
    291288            CleanupSubObjects(); 
    292              
    293289            state_ = SSL_STATE_NULL; 
    294290        } 
     
    323319                securesock_->SetOpt(KSoSSLDomainName, KSolInetSSL, 
    324320                                    servername_); 
    325             securesock_->FlushSessionCache(); 
     321 
     322            // FlushSessionCache() seems to also fire signals to all  
     323            // completed AOs (something like CActiveScheduler::RunIfReady()) 
     324            // which may cause problem, e.g: we've experienced that when  
     325            // SSL timeout is set to 1s, the SSL timeout timer fires up 
     326            // at this point and securesock_ instance gets deleted here! 
     327            // So be careful using this. And we don't think we need it here. 
     328            //securesock_->FlushSessionCache(); 
     329 
    326330            securesock_->StartClientHandshake(iStatus); 
    327331            SetActive(); 
     
    648652    ssock->cb = param->cb; 
    649653    ssock->user_data = param->user_data; 
     654    ssock->timeout = param->timeout; 
    650655    ssock->ciphers_num = param->ciphers_num; 
    651656    if (param->ciphers_num > 0) { 
     
    11541159        delete ssock->sock; 
    11551160        ssock->sock = NULL; 
     1161        if (err == KErrTimedOut) status = PJ_ETIMEDOUT; 
    11561162    } 
    11571163     
  • pjproject/trunk/pjlib/src/pjlib-test/ssl_sock.c

    r3110 r3117  
    6969{ 
    7070    pj_pool_t      *pool;           /* pool                                 */ 
     71    pj_ioqueue_t   *ioqueue;        /* ioqueue                              */ 
    7172    pj_bool_t       is_server;      /* server role flag                     */ 
    7273    pj_bool_t       is_verbose;     /* verbose flag, e.g: cert info         */ 
     
    743744        return PJ_FALSE; 
    744745    } 
     746 
     747    return PJ_TRUE; 
     748} 
     749 
     750static 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 
     782on_return: 
     783    st->err = status; 
     784 
     785    if (st->err != PJ_SUCCESS) 
     786        pj_activesock_close(new_asock); 
    745787 
    746788    return PJ_TRUE; 
     
    904946 
    905947 
     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 */ 
     954static 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(&param); 
     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(&param.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, &param, &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 
     1084on_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 
    9061100/* Test will perform multiple clients trying to connect to single server. 
    9071101 * Once SSL connection established, echo test will be performed. 
     
    11531347 
    11541348#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     */ 
    11551354 
    11561355    PJ_LOG(3,("", "..echo test w/ TLSv1 and PJ_TLS_RSA_WITH_DES_CBC_SHA cipher")); 
     
    11961395        return ret; 
    11971396 
    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  
    12031397    PJ_LOG(3,("", "..performance test")); 
    12041398    ret = perf_test(PJ_IOQUEUE_MAX_HANDLES/2 - 1, 0); 
     
    12061400        return ret; 
    12071401 
     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 
    12081408#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; 
    12091414 
    12101415    return 0; 
Note: See TracChangeset for help on using the changeset viewer.