Changeset 1879


Ignore:
Timestamp:
Mar 20, 2008 4:32:06 PM (16 years ago)
Author:
bennylp
Message:

More ticket #485: client and server self tested

Location:
pjproject/trunk/pjnath/src
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjnath/src/pjnath/stun_session.c

    r1877 r1879  
    268268    *notify_user = PJ_TRUE; 
    269269 
     270    if (response==NULL) 
     271        return PJ_SUCCESS; 
     272 
    270273    if (sess->auth_type != PJ_STUN_AUTH_LONG_TERM) 
    271274        return PJ_SUCCESS; 
  • pjproject/trunk/pjnath/src/pjnath/turn_session.c

    r1877 r1879  
    189189    sess->ka_interval = PJ_TURN_KEEP_ALIVE_SEC; 
    190190    sess->user_data = user_data; 
     191    sess->next_ch = PJ_TURN_CHANNEL_MIN; 
    191192 
    192193    /* Copy callback */ 
     
    326327        /* Send REFRESH with LIFETIME=0 */ 
    327328        can_destroy = PJ_FALSE; 
    328         sess->pending_destroy = PJ_TRUE; 
     329        send_refresh(sess, 0); 
    329330        break; 
    330331    case PJ_TURN_STATE_DEALLOCATING: 
     
    779780    pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, 
    780781                              PJ_STUN_ATTR_CHANNEL_NUMBER, 
    781                               PJ_STUN_SET_CH_NB(sess->next_ch)); 
     782                              PJ_STUN_SET_CH_NB(ch_num)); 
    782783 
    783784    /* Add PEER-ADDRESS attribute */ 
     
    10241025     
    10251026    /* Save relayed address */ 
    1026     pj_memcpy(&sess->relay_addr, &raddr_attr->sockaddr, sizeof(pj_sockaddr)); 
     1027    if (raddr_attr) { 
     1028        /* If we already have relay address, check if the relay address  
     1029         * in the response matches our relay address. 
     1030         */ 
     1031        if (pj_sockaddr_has_addr(&sess->relay_addr)) { 
     1032            if (pj_sockaddr_cmp(&sess->relay_addr, &raddr_attr->sockaddr)) { 
     1033                on_session_fail(sess, method, PJNATH_EINSTUNMSG, 
     1034                                pj_cstr(&s, "Error: different RELAY-ADDRESS is" 
     1035                                            "returned by server")); 
     1036                return; 
     1037            } 
     1038        } else { 
     1039            /* Otherwise save the relayed address */ 
     1040            pj_memcpy(&sess->relay_addr, &raddr_attr->sockaddr,  
     1041                      sizeof(pj_sockaddr)); 
     1042        } 
     1043    } 
    10271044 
    10281045    /* Success */ 
     
    11811198    pj_stun_data_attr *data_attr; 
    11821199 
     1200    PJ_UNUSED_ARG(pkt); 
     1201    PJ_UNUSED_ARG(pkt_len); 
    11831202    PJ_UNUSED_ARG(src_addr); 
    11841203    PJ_UNUSED_ARG(src_addr_len); 
     
    12101229    /* Notify application */ 
    12111230    if (sess->cb.on_rx_data) { 
    1212         (*sess->cb.on_rx_data)(sess, pkt, pkt_len,  
     1231        (*sess->cb.on_rx_data)(sess, data_attr->data, data_attr->length,  
    12131232                               &peer_attr->sockaddr, 
    12141233                               pj_sockaddr_get_len(&peer_attr->sockaddr)); 
     
    12991318 
    13001319        if (bind_channel) { 
     1320            pj_uint32_t hval = 0; 
    13011321            /* Register by channel number */ 
    13021322            pj_assert(peer->ch_id != PJ_TURN_INVALID_CHANNEL && peer->bound); 
    1303             pj_assert(pj_hash_get(sess->peer_table, &peer->ch_id,  
    1304                                   sizeof(peer->ch_id), NULL)==0); 
    1305  
    1306             pj_hash_set(sess->pool, sess->peer_table, &peer->ch_id, 
    1307                         sizeof(peer->ch_id), 0, peer); 
     1323 
     1324            if (pj_hash_get(sess->peer_table, &peer->ch_id,  
     1325                            sizeof(peer->ch_id), &hval)==0) { 
     1326                pj_hash_set(sess->pool, sess->peer_table, &peer->ch_id, 
     1327                            sizeof(peer->ch_id), hval, peer); 
     1328            } 
    13081329        } 
    13091330    } 
     
    14061427            delay.msec = 0; 
    14071428 
     1429            sess->timer.id = TIMER_KEEP_ALIVE; 
    14081430            pj_timer_heap_schedule(sess->timer_heap, &sess->timer, &delay); 
    14091431        } 
     
    14151437        pj_lock_release(sess->lock); 
    14161438        do_destroy(sess); 
    1417     }     
    1418 } 
    1419  
     1439    } else { 
     1440        pj_assert(!"Unknown timer event"); 
     1441        pj_lock_release(sess->lock); 
     1442    } 
     1443} 
     1444 
  • pjproject/trunk/pjnath/src/pjnath/turn_udp.c

    r1869 r1879  
    2020#include <pj/assert.h> 
    2121#include <pj/errno.h> 
     22#include <pj/lock.h> 
    2223#include <pj/log.h> 
    2324#include <pj/pool.h> 
    2425#include <pj/ioqueue.h> 
     26 
     27enum 
     28{ 
     29    TIMER_NONE, 
     30    TIMER_DESTROY 
     31}; 
    2532 
    2633struct pj_turn_udp 
     
    3037    pj_turn_udp_cb       cb; 
    3138    void                *user_data; 
     39 
     40    pj_lock_t           *lock; 
     41 
     42    pj_bool_t            destroy_request; 
     43    pj_timer_heap_t     *timer_heap; 
     44    pj_timer_entry       timer; 
    3245 
    3346    pj_sock_t            sock; 
     
    6578 
    6679 
     80static void destroy(pj_turn_udp *udp_rel); 
     81static void timer_cb(pj_timer_heap_t *th, pj_timer_entry *e); 
     82 
     83 
    6784/* 
    6885 * Create. 
     
    92109    if (cb) { 
    93110        pj_memcpy(&udp_rel->cb, cb, sizeof(*cb)); 
     111    } 
     112 
     113    /* Create lock */ 
     114    status = pj_lock_create_recursive_mutex(pool, pool->obj_name,  
     115                                            &udp_rel->lock); 
     116    if (status != PJ_SUCCESS) { 
     117        destroy(udp_rel); 
     118        return status; 
     119    } 
     120 
     121    /* Init timer */ 
     122    udp_rel->timer_heap = cfg->timer_heap; 
     123    pj_timer_entry_init(&udp_rel->timer, TIMER_NONE, udp_rel, &timer_cb); 
     124 
     125    /* Init socket */ 
     126    status = pj_sock_socket(af, pj_SOCK_DGRAM(), 0, &udp_rel->sock); 
     127    if (status != PJ_SUCCESS) { 
     128        destroy(udp_rel); 
     129        return status; 
     130    } 
     131 
     132    /* Bind to any */ 
     133    pj_sockaddr_init(af, &udp_rel->src_addr, NULL, 0); 
     134    status = pj_sock_bind(udp_rel->sock, &udp_rel->src_addr,  
     135                          pj_sockaddr_get_len(&udp_rel->src_addr)); 
     136    if (status != PJ_SUCCESS) { 
     137        destroy(udp_rel); 
     138        return status; 
     139    } 
     140 
     141    /* Register to ioqeuue */ 
     142    pj_bzero(&ioq_cb, sizeof(ioq_cb)); 
     143    ioq_cb.on_read_complete = &on_read_complete; 
     144    status = pj_ioqueue_register_sock(udp_rel->pool, cfg->ioqueue,  
     145                                      udp_rel->sock, udp_rel,  
     146                                      &ioq_cb, &udp_rel->key); 
     147    if (status != PJ_SUCCESS) { 
     148        destroy(udp_rel); 
     149        return status; 
    94150    } 
    95151 
     
    103159                                    &sess_cb, udp_rel, 0, &udp_rel->sess); 
    104160    if (status != PJ_SUCCESS) { 
    105         pj_turn_udp_destroy(udp_rel); 
    106         return status; 
    107     } 
    108  
    109     /* Init socket */ 
    110     status = pj_sock_socket(af, pj_SOCK_DGRAM(), 0, &udp_rel->sock); 
    111     if (status != PJ_SUCCESS) { 
    112         pj_turn_udp_destroy(udp_rel); 
    113         return status; 
    114     } 
    115  
    116     /* Bind to any */ 
    117     pj_sockaddr_init(af, &udp_rel->src_addr, NULL, 0); 
    118     status = pj_sock_bind(udp_rel->sock, &udp_rel->src_addr,  
    119                           pj_sockaddr_get_len(&udp_rel->src_addr)); 
    120     if (status != PJ_SUCCESS) { 
    121         pj_turn_udp_destroy(udp_rel); 
    122         return status; 
    123     } 
    124  
    125     /* Register to ioqeuue */ 
    126     pj_bzero(&ioq_cb, sizeof(ioq_cb)); 
    127     ioq_cb.on_read_complete = &on_read_complete; 
    128     status = pj_ioqueue_register_sock(udp_rel->pool, cfg->ioqueue,  
    129                                       udp_rel->sock, udp_rel,  
    130                                       &ioq_cb, &udp_rel->key); 
    131     if (status != PJ_SUCCESS) { 
    132         pj_turn_udp_destroy(udp_rel); 
     161        destroy(udp_rel); 
    133162        return status; 
    134163    } 
     
    145174 * Destroy. 
    146175 */ 
    147 PJ_DEF(void) pj_turn_udp_destroy(pj_turn_udp *udp_rel) 
    148 { 
     176static void destroy(pj_turn_udp *udp_rel) 
     177{ 
     178    if (udp_rel->lock) { 
     179        pj_lock_acquire(udp_rel->lock); 
     180    } 
     181 
    149182    if (udp_rel->sess) { 
     183        pj_turn_session_set_user_data(udp_rel->sess, NULL); 
    150184        pj_turn_session_destroy(udp_rel->sess); 
    151185        udp_rel->sess = NULL; 
     186    } 
     187 
     188    if (udp_rel->key) { 
     189        pj_ioqueue_unregister(udp_rel->key); 
     190        udp_rel->key = NULL; 
     191        udp_rel->sock = 0; 
     192    } else if (udp_rel->sock) { 
     193        pj_sock_close(udp_rel->sock); 
     194        udp_rel->sock = 0; 
     195    } 
     196 
     197    if (udp_rel->lock) { 
     198        pj_lock_release(udp_rel->lock); 
     199        pj_lock_destroy(udp_rel->lock); 
     200        udp_rel->lock = NULL; 
    152201    } 
    153202 
     
    158207    } 
    159208} 
     209 
     210PJ_DEF(void) pj_turn_udp_destroy(pj_turn_udp *udp_rel) 
     211{ 
     212    pj_lock_acquire(udp_rel->lock); 
     213    udp_rel->destroy_request = PJ_TRUE; 
     214 
     215    if (udp_rel->sess) { 
     216        pj_turn_session_destroy(udp_rel->sess); 
     217        /* This will ultimately call our state callback, and when 
     218         * session state is DESTROYING we will schedule a timer to 
     219         * destroy ourselves. 
     220         */ 
     221        pj_lock_release(udp_rel->lock); 
     222    } else { 
     223        pj_lock_release(udp_rel->lock); 
     224        destroy(udp_rel); 
     225    } 
     226 
     227} 
     228 
     229/* Timer callback */ 
     230static void timer_cb(pj_timer_heap_t *th, pj_timer_entry *e) 
     231{ 
     232    pj_turn_udp *udp_rel = (pj_turn_udp*)e->user_data; 
     233    int eid = e->id; 
     234 
     235    PJ_UNUSED_ARG(th); 
     236 
     237    e->id = TIMER_NONE; 
     238 
     239    switch (eid) { 
     240    case TIMER_DESTROY: 
     241        destroy(udp_rel); 
     242        break; 
     243    default: 
     244        pj_assert(!"Invalid timer id"); 
     245        break; 
     246    } 
     247} 
     248 
    160249 
    161250/* 
     
    272361 
    273362    udp_rel = (pj_turn_udp*) pj_ioqueue_get_user_data(key); 
     363    pj_lock_acquire(udp_rel->lock); 
    274364 
    275365    do { 
     
    301391             ++retry < MAX_RETRY); 
    302392 
     393    pj_lock_release(udp_rel->lock); 
    303394} 
    304395 
     
    317408    pj_ssize_t len = pkt_len; 
    318409 
     410    if (udp_rel == NULL) { 
     411        /* We've been destroyed */ 
     412        pj_assert(!"We should shutdown gracefully"); 
     413        return PJ_EINVALIDOP; 
     414    } 
     415 
    319416    return pj_sock_sendto(udp_rel->sock, pkt, &len, 0, 
    320417                          dst_addr, dst_addr_len); 
     
    348445    pj_turn_udp *udp_rel = (pj_turn_udp*)  
    349446                           pj_turn_session_get_user_data(sess); 
     447    if (udp_rel == NULL) { 
     448        /* We've been destroyed */ 
     449        return; 
     450    } 
     451 
    350452    if (udp_rel->cb.on_rx_data) { 
    351453        (*udp_rel->cb.on_rx_data)(udp_rel, pkt, pkt_len,  
     
    364466    pj_turn_udp *udp_rel = (pj_turn_udp*)  
    365467                           pj_turn_session_get_user_data(sess); 
     468    if (udp_rel == NULL) { 
     469        /* We've been destroyed */ 
     470        return; 
     471    } 
     472 
    366473    if (udp_rel->cb.on_state) { 
    367474        (*udp_rel->cb.on_state)(udp_rel, old_state, new_state); 
    368475    } 
    369476 
    370     if (new_state > PJ_TURN_STATE_READY) { 
    371         udp_rel->sess = NULL; 
    372     } 
    373 } 
    374  
    375  
     477    if (new_state >= PJ_TURN_STATE_DESTROYING && udp_rel->sess) { 
     478        if (udp_rel->destroy_request) { 
     479            pj_time_val delay = {0, 0}; 
     480 
     481            pj_turn_session_set_user_data(udp_rel->sess, NULL); 
     482 
     483            udp_rel->timer.id = TIMER_DESTROY; 
     484            pj_timer_heap_schedule(udp_rel->timer_heap, &udp_rel->timer,  
     485                                   &delay); 
     486        } else { 
     487            udp_rel->sess = NULL; 
     488        } 
     489    } 
     490} 
     491 
     492 
  • pjproject/trunk/pjnath/src/pjturn-client/client_main.c

    r1869 r1879  
    6262    char    *user_name; 
    6363    char    *password; 
    64     char    *nonce; 
    6564    pj_bool_t use_fingerprint; 
    6665} o; 
     
    136135 
    137136        CHECK( pj_gethostip(pj_AF_INET(), &g.peer[i].addr) ); 
    138         pj_sockaddr_set_port(&g.peer[0].addr, port); 
     137        pj_sockaddr_set_port(&g.peer[i].addr, port); 
    139138 
    140139    } 
     
    266265        cred.data.static_cred.data_type = 0; 
    267266        cred.data.static_cred.data = pj_str(o.password); 
    268         cred.data.static_cred.nonce = pj_str(o.nonce); 
     267        //cred.data.static_cred.nonce = pj_str(o.nonce); 
    269268    } else { 
    270269        PJ_LOG(2,(THIS_FILE, "Warning: no credential is set")); 
     
    287286    if (g.udp_rel) { 
    288287        pj_turn_udp_destroy(g.udp_rel); 
    289         g.udp_rel = NULL; 
    290288    } 
    291289} 
     
    310308                          pj_turn_state_t new_state) 
    311309{ 
     310    PJ_LOG(3,(THIS_FILE, "State %s --> %s", pj_turn_state_name(old_state),  
     311              pj_turn_state_name(new_state))); 
     312 
    312313    if (new_state == PJ_TURN_STATE_READY) { 
    313314        pj_turn_session_info info; 
    314315        pj_turn_udp_get_info(udp_rel, &info); 
    315316        pj_memcpy(&g.relay_addr, &info.relay_addr, sizeof(pj_sockaddr)); 
     317    } else if (new_state > PJ_TURN_STATE_READY && g.udp_rel) { 
     318        PJ_LOG(3,(THIS_FILE, "Relay shutting down..")); 
     319        g.udp_rel = NULL; 
    316320    } 
    317321} 
     
    380384           relay_addr); 
    381385    puts("|                                   | 0  Send data to relay address  |"); 
    382     puts("| A      Allocate relay             +--------------------------------+        "); 
    383     puts("| S[01]  Send data to peer 0/1      |             PEER-1             |"); 
    384     puts("| B[01]  BindChannel to peer 0/1    |                                |"); 
    385     printf("| X      Delete allocation          | Address: %-21s |\n", 
     386    puts("| a      Allocate relay             +--------------------------------+        "); 
     387    puts("| s,ss   Send data to peer 0/1      |             PEER-1             |"); 
     388    puts("| b,bb   BindChannel to peer 0/1    |                                |"); 
     389    printf("| x      Delete allocation          | Address: %-21s |\n", 
    386390          peer1_addr); 
    387391    puts("+-----------------------------------+                                |"); 
     
    406410         
    407411        switch (input[0]) { 
    408         case 'A': 
     412        case 'a': 
    409413            create_relay(); 
    410414            break; 
    411         case 'S': 
     415        case 's': 
    412416            if (g.udp_rel == NULL) { 
    413417                puts("Error: no relay"); 
    414418                continue; 
    415419            } 
    416             if (input[1] != '0' && input[1] != '1') { 
    417                 puts("Usage: S0 or S1"); 
    418                 continue; 
    419             } 
    420             peer = &g.peer[input[1]-'0']; 
     420            if (input[1]!='s') 
     421                peer = &g.peer[0]; 
     422            else 
     423                peer = &g.peer[1]; 
     424 
    421425            strcpy(input, "Hello from client"); 
    422426            status = pj_turn_udp_sendto(g.udp_rel, input, strlen(input)+1,  
     
    426430                my_perror("turn_udp_sendto() failed", status); 
    427431            break; 
    428         case 'B': 
     432        case 'b': 
    429433            if (g.udp_rel == NULL) { 
    430434                puts("Error: no relay"); 
    431435                continue; 
    432436            } 
    433             if (input[1] != '0' && input[1] != '1') { 
    434                 puts("Usage: B0 or B1"); 
    435                 continue; 
    436             } 
    437             peer = &g.peer[input[1]-'0']; 
     437            if (input[1]!='b') 
     438                peer = &g.peer[0]; 
     439            else 
     440                peer = &g.peer[1]; 
     441 
    438442            status = pj_turn_udp_bind_channel(g.udp_rel, &peer->addr, 
    439443                                              pj_sockaddr_get_len(&peer->addr)); 
     
    441445                my_perror("turn_udp_bind_channel() failed", status); 
    442446            break; 
    443         case 'X': 
     447        case 'x': 
    444448            if (g.udp_rel == NULL) { 
    445449                puts("Error: no relay"); 
     
    450454        case '0': 
    451455        case '1': 
    452             peer = &g.peer[input[1]-'0']; 
     456            peer = &g.peer[input[0]-'0']; 
    453457            sprintf(input, "Hello from peer%d", input[0]-'0'); 
    454458            len = strlen(input)+1; 
     
    474478    puts(" --username, -u    Set username of the credential"); 
    475479    puts(" --password, -p    Set password of the credential"); 
    476     puts(" --nonce, -N       Set NONCE");    
    477480    puts(" --fingerprint, -F Use fingerprint for outgoing requests"); 
    478481    puts(" --help, -h"); 
     
    485488        { "username",   1, 0, 'u'}, 
    486489        { "password",   1, 0, 'p'}, 
    487         { "nonce",      1, 0, 'N'}, 
    488490        { "fingerprint",0, 0, 'F'}, 
    489491        { "data",       1, 0, 'D'}, 
     
    505507            o.password = pj_optarg; 
    506508            break; 
    507         case 'N': 
    508             o.nonce = pj_optarg; 
    509             break; 
    510509        case 'h': 
    511510            usage(); 
     
    538537        goto on_return; 
    539538     
    540     if ((status=create_relay()) != 0) 
    541         goto on_return; 
     539    //if ((status=create_relay()) != 0) 
     540    //  goto on_return; 
    542541     
    543542    console_main(); 
  • pjproject/trunk/pjnath/src/pjturn-srv/allocation.c

    r1877 r1879  
    669669    if (status != PJ_SUCCESS) { 
    670670        /* Unable to allocate port */ 
    671         PJ_LOG(4,(THIS_FILE, "bind() failed: err %d",  
     671        PJ_LOG(4,(THIS_FILE, "Unable to allocate relay, giving up: err %d",  
    672672                  status)); 
    673673        pj_sock_close(relay->tp.sock); 
     
    689689        pj_sockaddr_copy_addr(&relay->hkey.addr, &alloc->listener->addr); 
    690690    } 
     691    if (!pj_sockaddr_has_addr(&relay->hkey.addr)) { 
     692        pj_sockaddr tmp_addr; 
     693        pj_gethostip(af, &tmp_addr); 
     694        pj_sockaddr_copy_addr(&relay->hkey.addr, &tmp_addr); 
     695    } 
    691696 
    692697    /* Init ioqueue */ 
     
    752757    } 
    753758 
    754     /* Add LIFETIME. */ 
    755     pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, 
    756                               PJ_STUN_ATTR_LIFETIME, interval); 
    757  
    758     /* Add BANDWIDTH */ 
    759     pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, 
    760                               PJ_STUN_ATTR_BANDWIDTH, 
    761                               alloc->bandwidth); 
     759    /* Add LIFETIME if this is not ChannelBind. */ 
     760    if (PJ_STUN_GET_METHOD(tdata->msg->hdr.type)!=PJ_STUN_CHANNEL_BIND_METHOD){ 
     761        pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, 
     762                                  PJ_STUN_ATTR_LIFETIME, interval); 
     763 
     764        /* Add BANDWIDTH if lifetime is not zero */ 
     765        if (interval != 0) { 
     766            pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, 
     767                                      PJ_STUN_ATTR_BANDWIDTH, 
     768                                      alloc->bandwidth); 
     769        } 
     770    } 
    762771 
    763772    status = pj_stun_session_send_msg(alloc->sess, PJ_TRUE,  
     
    774783/* Create new permission */ 
    775784static pj_turn_permission *create_permission(pj_turn_allocation *alloc, 
    776                                             const pj_sockaddr_t *peer_addr, 
    777                                             unsigned addr_len) 
     785                                             const pj_sockaddr_t *peer_addr, 
     786                                             unsigned addr_len) 
    778787{ 
    779788    pj_turn_permission *perm; 
     
    795804    perm->expiry.sec += PJ_TURN_PERM_TIMEOUT; 
    796805 
     806    /* Register to hash table */ 
     807    pj_hash_set(alloc->pool, alloc->peer_table, &perm->hkey.peer_addr,  
     808                pj_sockaddr_get_len(&perm->hkey.peer_addr), 0, perm); 
     809 
    797810    return perm; 
    798811} 
     
    805818 
    806819    pj_gettimeofday(&now); 
    807     if (PJ_TIME_VAL_LT(perm->expiry, now)) { 
     820    if (PJ_TIME_VAL_GT(perm->expiry, now)) { 
    808821        /* Permission has not expired */ 
    809822        return perm; 
     
    811824 
    812825    /* Remove from permission hash table */ 
    813     pj_hash_set(NULL, alloc->peer_table, &perm->hkey, sizeof(perm->hkey), 
    814                 0, NULL); 
     826    pj_hash_set(NULL, alloc->peer_table, &perm->hkey.peer_addr,  
     827                pj_sockaddr_get_len(&perm->hkey.peer_addr), 0, NULL); 
    815828 
    816829    /* Remove from channel hash table, if assigned a channel number */ 
     
    829842                          unsigned addr_len) 
    830843{ 
    831     pj_turn_permission_key key; 
    832844    pj_turn_permission *perm; 
    833845 
    834     pj_bzero(&key, sizeof(key)); 
    835     pj_memcpy(&key, peer_addr, addr_len); 
    836  
    837846    /* Lookup in peer hash table */ 
    838     perm = (pj_turn_permission*) pj_hash_get(alloc->peer_table, &key, 
    839                                             sizeof(key), NULL); 
    840     return check_permission_expiry(perm); 
     847    perm = (pj_turn_permission*) pj_hash_get(alloc->peer_table, peer_addr, 
     848                                             addr_len, NULL); 
     849    return perm ? check_permission_expiry(perm) : NULL; 
    841850} 
    842851 
     
    850859 
    851860    /* Lookup in peer hash table */ 
    852     perm = (pj_turn_permission*) pj_hash_get(alloc->peer_table, &chnum16, 
     861    perm = (pj_turn_permission*) pj_hash_get(alloc->ch_table, &chnum16, 
    853862                                            sizeof(chnum16), NULL); 
    854     return check_permission_expiry(perm); 
     863    return perm ? check_permission_expiry(perm) : NULL; 
    855864} 
    856865 
     
    931940            /* Discard */ 
    932941            PJ_LOG(4,(alloc->obj_name,  
    933                       "ChannelData from %s discarded: not found", 
    934                       alloc->info)); 
     942                      "ChannelData from %s discarded: ch#0x%x not found", 
     943                      alloc->info, pj_ntohs(cd->ch_number))); 
    935944            goto on_return; 
    936945        } 
     
    9921001 
    9931002        /* Copy data */ 
    994         pj_memcpy(rel->tp.rx_pkt+sizeof(pj_turn_channel_data), pkt, len); 
     1003        pj_memcpy(rel->tp.tx_pkt+sizeof(pj_turn_channel_data), pkt, len); 
    9951004 
    9961005        /* Send to client */ 
     
    10101019            return; 
    10111020        } 
     1021 
     1022        pj_stun_msg_add_sockaddr_attr(tdata->pool, tdata->msg,  
     1023                                      PJ_STUN_ATTR_PEER_ADDR, PJ_TRUE, 
     1024                                      src_addr, pj_sockaddr_get_len(src_addr)); 
     1025        pj_stun_msg_add_binary_attr(tdata->pool, tdata->msg, 
     1026                                    PJ_STUN_ATTR_DATA,  
     1027                                    (const pj_uint8_t*)pkt, len); 
     1028 
     1029        pj_stun_session_send_msg(alloc->sess, PJ_FALSE,  
     1030                                 &alloc->hkey.clt_addr,  
     1031                                 pj_sockaddr_get_len(&alloc->hkey.clt_addr),  
     1032                                 tdata); 
    10121033    } 
    10131034} 
     
    11871208            refresh_permission(p1); 
    11881209 
     1210            /* Send response */ 
     1211            send_reply_ok(alloc, rdata); 
     1212 
    11891213            /* Done */ 
    11901214            return PJ_SUCCESS; 
     
    12121236        /* Assign channel number to permission */ 
    12131237        p2->channel = PJ_STUN_GET_CH_NB(ch_attr->value); 
     1238 
     1239        /* Register to hash table */ 
     1240        pj_assert(sizeof(p2->channel==2)); 
     1241        pj_hash_set(alloc->pool, alloc->ch_table, &p2->channel,  
     1242                    sizeof(p2->channel), 0, p2); 
    12141243 
    12151244        /* Update */ 
  • pjproject/trunk/pjnath/src/pjturn-srv/auth.c

    r1877 r1879  
    114114 * PJ_FALSE, 438 (Stale Nonce) response will be created. 
    115115 */ 
    116 PJ_DEF(pj_status_t) pj_turn_verify_nonce(const pj_stun_msg *msg, 
    117                                         void *user_data, 
    118                                         const pj_str_t *realm, 
    119                                         const pj_str_t *username, 
    120                                         const pj_str_t *nonce) 
     116PJ_DEF(pj_bool_t) pj_turn_verify_nonce(const pj_stun_msg *msg, 
     117                                      void *user_data, 
     118                                      const pj_str_t *realm, 
     119                                      const pj_str_t *username, 
     120                                      const pj_str_t *nonce) 
    121121{ 
    122122    PJ_UNUSED_ARG(msg); 
     
    128128        return PJ_FALSE; 
    129129 
    130     return PJ_SUCCESS; 
     130    return PJ_TRUE; 
    131131} 
    132132 
  • pjproject/trunk/pjnath/src/pjturn-srv/auth.h

    r1877 r1879  
    106106 *                      NONCE can be accepted. 
    107107 */ 
    108 PJ_DECL(pj_status_t) pj_turn_verify_nonce(const pj_stun_msg *msg, 
    109                                           void *user_data, 
    110                                           const pj_str_t *realm, 
    111                                           const pj_str_t *username, 
    112                                           const pj_str_t *nonce); 
     108PJ_DECL(pj_bool_t) pj_turn_verify_nonce(const pj_stun_msg *msg, 
     109                                        void *user_data, 
     110                                        const pj_str_t *realm, 
     111                                        const pj_str_t *username, 
     112                                        const pj_str_t *nonce); 
    113113 
    114114#endif  /* __PJ_TURN_SRV_AUTH_H__ */ 
  • pjproject/trunk/pjnath/src/pjturn-srv/main.c

    r1869 r1879  
    2222#define REALM   "pjsip.org" 
    2323 
     24static pj_caching_pool g_cp; 
     25 
    2426int err(const char *title, pj_status_t status) 
    2527{ 
     
    3133} 
    3234 
     35static void dump_status(pj_turn_srv *srv) 
     36{ 
     37    char addr[80]; 
     38    pj_hash_iterator_t itbuf, *it; 
     39    pj_time_val now; 
     40    unsigned i; 
     41 
     42    for (i=0; i<srv->core.lis_cnt; ++i) { 
     43        pj_turn_listener *lis = srv->core.listener[i]; 
     44        printf("Server address : %s\n", lis->info); 
     45    } 
     46 
     47    printf("Worker threads : %d\n", srv->core.thread_cnt); 
     48    printf("Total mem usage: %d.%03dMB\n", g_cp.used_size / 1000000,  
     49           (g_cp.used_size % 1000000)/1000); 
     50    printf("UDP port range : %u %u %u (next/min/max)\n", srv->ports.next_udp, 
     51           srv->ports.min_udp, srv->ports.max_udp); 
     52    printf("TCP port range : %u %u %u (next/min/max)\n", srv->ports.next_tcp, 
     53           srv->ports.min_tcp, srv->ports.max_tcp); 
     54    printf("Clients #      : %u\n", pj_hash_count(srv->tables.alloc)); 
     55 
     56    puts(""); 
     57 
     58    if (pj_hash_count(srv->tables.alloc)==0) { 
     59        return; 
     60    } 
     61 
     62    puts("#    Client addr.          Alloc addr.            Username Lftm Expy #prm #chl"); 
     63    puts("------------------------------------------------------------------------------"); 
     64 
     65    pj_gettimeofday(&now); 
     66 
     67    it = pj_hash_first(srv->tables.alloc, &itbuf); 
     68    i=1; 
     69    while (it) { 
     70        pj_turn_allocation *alloc = (pj_turn_allocation*)  
     71                                    pj_hash_this(srv->tables.alloc, it); 
     72        printf("%-3d %-22s %-22s %-8.*s %-4d %-4d %-4d %-4d\n", 
     73               i, 
     74               alloc->info, 
     75               pj_sockaddr_print(&alloc->relay.hkey.addr, addr, sizeof(addr), 3), 
     76               (int)alloc->cred.data.static_cred.username.slen, 
     77               (int)alloc->cred.data.static_cred.username.ptr, 
     78               alloc->relay.lifetime, 
     79               alloc->relay.expiry.sec - now.sec, 
     80               pj_hash_count(alloc->peer_table),  
     81               pj_hash_count(alloc->ch_table)); 
     82        it = pj_hash_next(srv->tables.alloc, it); 
     83        ++i; 
     84    } 
     85} 
     86 
     87static void menu(void) 
     88{ 
     89    puts(""); 
     90    puts("Menu:"); 
     91    puts(" d   Dump status"); 
     92    puts(" q   Quit"); 
     93    printf(">> "); 
     94} 
     95 
     96static void console_main(pj_turn_srv *srv) 
     97{ 
     98    pj_bool_t quit = PJ_FALSE; 
     99 
     100    while (!quit) { 
     101        char line[10]; 
     102         
     103        menu(); 
     104             
     105        fgets(line, sizeof(line), stdin); 
     106 
     107        switch (line[0]) { 
     108        case 'd': 
     109            dump_status(srv); 
     110            break; 
     111        case 'q': 
     112            quit = PJ_TRUE; 
     113            break; 
     114        } 
     115    } 
     116} 
     117 
    33118int main() 
    34119{ 
    35     pj_caching_pool cp; 
    36120    pj_turn_srv *srv; 
    37121    pj_turn_listener *listener; 
     
    45129    pjnath_init(); 
    46130 
    47     pj_caching_pool_init(&cp, NULL, 0); 
     131    pj_caching_pool_init(&g_cp, NULL, 0); 
    48132 
    49133    pj_turn_auth_init(REALM); 
    50134 
    51     status = pj_turn_srv_create(&cp.factory, &srv); 
     135    status = pj_turn_srv_create(&g_cp.factory, &srv); 
    52136    if (status != PJ_SUCCESS) 
    53137        return err("Error creating server", status); 
     
    63147 
    64148    puts("Server is running"); 
    65     puts("Press <ENTER> to quit"); 
    66149 
    67     { 
    68         char line[10]; 
    69         fgets(line, sizeof(line), stdin); 
    70     } 
     150    console_main(srv); 
    71151 
    72152    pj_turn_srv_destroy(srv); 
    73     pj_caching_pool_destroy(&cp); 
     153    pj_caching_pool_destroy(&g_cp); 
    74154    pj_shutdown(); 
    75155 
  • pjproject/trunk/pjnath/src/pjturn-srv/server.c

    r1877 r1879  
    136136    /* Init ports settings */ 
    137137    srv->ports.min_udp = srv->ports.next_udp = MIN_PORT; 
    138     srv->ports.max_tcp = MAX_PORT; 
     138    srv->ports.max_udp = MAX_PORT; 
    139139    srv->ports.min_tcp = srv->ports.next_tcp = MIN_PORT; 
    140140    srv->ports.max_tcp = MAX_PORT; 
     
    266266    } 
    267267 
     268    /* Destroy all allocations FIRST */ 
     269    if (srv->tables.alloc) { 
     270        it = pj_hash_first(srv->tables.alloc, &itbuf); 
     271        while (it != NULL) { 
     272            pj_turn_allocation *alloc = (pj_turn_allocation*) 
     273                                        pj_hash_this(srv->tables.alloc, it); 
     274            pj_hash_iterator_t *next = pj_hash_next(srv->tables.alloc, it); 
     275            pj_turn_allocation_destroy(alloc); 
     276            it = next; 
     277        } 
     278    } 
     279     
    268280    /* Destroy all listeners and STUN sessions associated with them. */ 
    269281    for (i=0; i<srv->core.lis_cnt; ++i) { 
     
    277289        } 
    278290    } 
    279  
    280     /* Destroy all allocations */ 
    281     if (srv->tables.alloc) { 
    282         it = pj_hash_first(srv->tables.alloc, &itbuf); 
    283         while (it != NULL) { 
    284             pj_turn_allocation *alloc = (pj_turn_allocation*) 
    285                                         pj_hash_this(srv->tables.alloc, it); 
    286             pj_turn_allocation_destroy(alloc); 
    287             it = pj_hash_next(srv->tables.alloc, it); 
    288         } 
    289     } 
    290      
    291291 
    292292    /* Destroy hash tables (well, sort of) */ 
  • pjproject/trunk/pjnath/src/pjturn-srv/turn.h

    r1877 r1879  
    191191 
    192192    /** Optional channel number, or PJ_TURN_INVALID_CHANNEL if channel number 
    193      *  is not requested for this permission. 
     193     *  is not requested for this permission.  
    194194     */ 
    195195    pj_uint16_t         channel; 
Note: See TracChangeset for help on using the changeset viewer.