Ignore:
Timestamp:
Mar 8, 2008 12:54:04 AM (16 years ago)
Author:
bennylp
Message:

More work on ticket #485: more TURN-07 work

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjnath/src/pjturn-srv/server.c

    r1812 r1850  
    3636 
    3737 
    38 /* Globals */ 
    39 PJ_DEF_DATA(int) PJTURN_TP_UDP = 1; 
    40 PJ_DEF_DATA(int) PJTURN_TP_TCP = 2; 
    41 PJ_DEF_DATA(int) PJTURN_TP_TLS = 3; 
    42  
    4338/* Prototypes */ 
    4439static pj_status_t on_tx_stun_msg( pj_stun_session *sess, 
     
    5449                                      unsigned src_addr_len); 
    5550 
     51/* 
     52 * Get transport type name. 
     53 */ 
     54PJ_DEF(const char*) pjturn_tp_type_name(int tp_type) 
     55{ 
     56    /* Must be 3 characters long! */ 
     57    if (tp_type == PJTURN_TP_UDP) 
     58        return "UDP"; 
     59    else if (tp_type == PJTURN_TP_TCP) 
     60        return "TCP"; 
     61    else 
     62        return "???"; 
     63} 
    5664 
    5765/* 
     
    98106    srv->tables.alloc = pj_hash_create(pool, MAX_CLIENTS); 
    99107    srv->tables.res = pj_hash_create(pool, MAX_CLIENTS); 
    100     srv->tables.peer = pj_hash_create(pool, MAX_CLIENTS*MAX_PEERS_PER_CLIENT); 
    101108 
    102109    /* Init ports settings */ 
     
    162169    lis->id = index; 
    163170    srv->core.lis_cnt++; 
     171 
     172    return PJ_SUCCESS; 
     173} 
     174 
     175/** 
     176 * Register an allocation. 
     177 */ 
     178PJ_DEF(pj_status_t) pjturn_srv_register_allocation(pjturn_srv *srv, 
     179                                                   pjturn_allocation *alloc) 
     180{ 
     181    /* Add to hash tables */ 
     182    pj_lock_acquire(srv->core.lock); 
     183    pj_hash_set(alloc->pool, srv->tables.alloc, 
     184                &alloc->hkey, sizeof(alloc->hkey), 0, alloc); 
     185    pj_hash_set(alloc->pool, srv->tables.res, 
     186                &alloc->relay.hkey, sizeof(alloc->relay.hkey), 0, 
     187                &alloc->relay); 
     188    pj_lock_release(srv->core.lock); 
     189 
     190    return PJ_SUCCESS; 
     191} 
     192 
     193/** 
     194 * Unregister an allocation. 
     195 */ 
     196PJ_DEF(pj_status_t) pjturn_srv_unregister_allocation(pjturn_srv *srv, 
     197                                                     pjturn_allocation *alloc) 
     198{ 
     199    /* Unregister from hash tables */ 
     200    pj_lock_acquire(srv->core.lock); 
     201    pj_hash_set(alloc->pool, srv->tables.alloc, 
     202                &alloc->hkey, sizeof(alloc->hkey), 0, NULL); 
     203    pj_hash_set(alloc->pool, srv->tables.res, 
     204                &alloc->relay.hkey, sizeof(alloc->relay.hkey), 0, NULL); 
     205    pj_lock_release(srv->core.lock); 
    164206 
    165207    return PJ_SUCCESS; 
     
    185227 
    186228/* Create and send error response */ 
    187 static pj_status_t respond_error(pj_stun_sess *sess, const pj_stun_msg *req, 
    188                                  pj_bool_t cache, int code, const char *err_msg, 
    189                                  const pj_sockaddr_t *addr, unsigned addr_len) 
     229static pj_status_t respond_error(pj_stun_session *sess, const pj_stun_msg *req, 
     230                                 pj_bool_t cache, int code, const char *errmsg, 
     231                                 const pj_sockaddr_t *dst_addr,  
     232                                 unsigned addr_len) 
    190233{ 
    191234    pj_status_t status; 
     
    194237 
    195238    status = pj_stun_session_create_res(sess, req,  
    196                                         code, (err_msg?pj_cstr(&reason,err_msg):NULL),  
     239                                        code, (errmsg?pj_cstr(&reason,errmsg):NULL),  
    197240                                        &tdata); 
    198241    if (status != PJ_SUCCESS) 
    199         return statys; 
     242        return status; 
    200243 
    201244    status = pj_stun_session_send_msg(sess, cache, dst_addr,  addr_len, tdata); 
     
    221264 
    222265    /* Get BANDWIDTH attribute, if any. */ 
    223     attr_bw = pj_stun_msg_find_attr(msg, PJ_STUN_BANDWIDTH_ATTR, 0); 
     266    attr_bw = (pj_stun_uint_attr*) 
     267              pj_stun_msg_find_attr(req, PJ_STUN_ATTR_BANDWIDTH, 0); 
    224268    if (attr_bw) { 
    225269        cfg->bandwidth = attr_bw->value; 
     
    230274    /* Check if we can satisfy the bandwidth */ 
    231275    if (cfg->bandwidth > MAX_CLIENT_BANDWIDTH) { 
    232         respond_error(sess, msg, PJ_FALSE,  
     276        respond_error(sess, req, PJ_FALSE,  
    233277                      PJ_STUN_SC_ALLOCATION_QUOTA_REACHED,  
    234278                      "Invalid bandwidth", src_addr, src_addr_len); 
     
    237281 
    238282    /* Get REQUESTED-TRANSPORT attribute, is any */ 
    239     attr_req_tp = pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_REQ_TRANSPORT, 0); 
     283    attr_req_tp = (pj_stun_uint_attr*) 
     284                  pj_stun_msg_find_attr(req, PJ_STUN_ATTR_REQ_TRANSPORT, 0); 
    240285    if (attr_req_tp) { 
    241286        cfg->tp_type = PJ_STUN_GET_RT_PROTO(attr_req_tp->value); 
     
    246291    /* Can only support UDP for now */ 
    247292    if (cfg->tp_type != PJTURN_TP_UDP) { 
    248         respond_error(sess, msg, PJ_FALSE,  
     293        respond_error(sess, req, PJ_FALSE,  
    249294                      PJ_STUN_SC_UNSUPP_TRANSPORT_PROTO,  
    250295                      NULL, src_addr, src_addr_len); 
     
    253298 
    254299    /* Get REQUESTED-IP attribute, if any */ 
    255     attr_req_ip = pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_REQ_IP, 0); 
     300    attr_req_ip = (pj_stun_sockaddr_attr*) 
     301                  pj_stun_msg_find_attr(req, PJ_STUN_ATTR_REQ_IP, 0); 
    256302    if (attr_req_ip) { 
    257         pj_memcpy(&cfg->addr, &attr_req_ip->sockaddr,  
    258                   sizeof(attr_req_ip->sockaddr)); 
     303        pj_sockaddr_print(&attr_req_ip->sockaddr, cfg->addr,  
     304                          sizeof(cfg->addr), 0); 
    259305    } 
    260306 
    261307    /* Get REQUESTED-PORT-PROPS attribute, if any */ 
    262     attr_rpp = pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_REQ_PORT_PROPS, 0); 
     308    attr_rpp = (pj_stun_uint_attr*) 
     309               pj_stun_msg_find_attr(req, PJ_STUN_ATTR_REQ_PORT_PROPS, 0); 
    263310    if (attr_rpp) { 
    264311        cfg->rpp_bits = PJ_STUN_GET_RPP_BITS(attr_rpp->value); 
     
    270317 
    271318    /* Get LIFETIME attribute */ 
    272     attr_lifetime = pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_LIFETIME, 0); 
     319    attr_lifetime = (pj_stun_uint_attr*) 
     320                    pj_stun_msg_find_attr(req, PJ_STUN_ATTR_LIFETIME, 0); 
    273321    if (attr_lifetime) { 
    274322        cfg->lifetime = attr_lifetime->value; 
    275323        if (cfg->lifetime < MIN_LIFETIME || cfg->lifetime > MAX_LIFETIME) { 
    276             respond_error(sess, msg, PJ_FALSE,  
     324            respond_error(sess, req, PJ_FALSE,  
    277325                          PJ_STUN_SC_BAD_REQUEST,  
    278326                          "Invalid LIFETIME value", src_addr,  
     
    296344{ 
    297345    pjturn_listener *listener; 
     346    pjturn_srv *srv; 
    298347    pjturn_allocation_req req; 
     348    pjturn_allocation *alloc; 
     349    pj_stun_tx_data *tdata; 
    299350    pj_status_t status; 
    300351 
    301352    listener = (pjturn_listener*) pj_stun_session_get_user_data(sess); 
     353    srv = listener->server; 
    302354 
    303355    /* Handle strayed REFRESH request */ 
     
    322374        return status; 
    323375 
    324     /* Ready to allocate now */ 
    325  
     376    /* Create new allocation. The relay resource will be allocated 
     377     * in this function. 
     378     */ 
     379    status = pjturn_allocation_create(listener, src_addr, src_addr_len, 
     380                                      msg, &req, &alloc); 
     381    if (status != PJ_SUCCESS) { 
     382        char errmsg[PJ_ERR_MSG_SIZE]; 
     383 
     384        pj_strerror(status, errmsg, sizeof(errmsg)); 
     385        return respond_error(sess, msg, PJ_FALSE, PJ_STUN_SC_SERVER_ERROR, 
     386                             errmsg, src_addr, src_addr_len); 
     387    } 
     388 
     389    /* Respond the original ALLOCATE request */ 
     390    status = pj_stun_session_create_res(srv->core.stun_sess[listener->id], 
     391                                        msg, 0, NULL, &tdata); 
     392    if (status != PJ_SUCCESS) { 
     393        char errmsg[PJ_ERR_MSG_SIZE]; 
     394 
     395        pjturn_allocation_destroy(alloc); 
     396 
     397        pj_strerror(status, errmsg, sizeof(errmsg)); 
     398        return respond_error(sess, msg, PJ_FALSE, PJ_STUN_SC_SERVER_ERROR, 
     399                             errmsg, src_addr, src_addr_len); 
     400    } 
     401 
     402    /* Add RELAYED-ADDRESS attribute */ 
     403    pj_stun_msg_add_sockaddr_attr(tdata->pool, tdata->msg, 
     404                                  PJ_STUN_ATTR_RELAY_ADDR, PJ_TRUE, 
     405                                  &alloc->relay.hkey.addr, 
     406                                  pj_sockaddr_get_len(&alloc->relay.hkey.addr)); 
     407 
     408    /* Add LIFETIME. */ 
     409    pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, 
     410                              PJ_STUN_ATTR_LIFETIME,  
     411                              (unsigned)alloc->relay.lifetime); 
     412 
     413    /* Add BANDWIDTH */ 
     414    pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, 
     415                              PJ_STUN_ATTR_BANDWIDTH, 
     416                              alloc->bandwidth); 
     417 
     418    /* Add RESERVATION-TOKEN */ 
     419    PJ_TODO(ADD_RESERVATION_TOKEN); 
     420 
     421    /* Add XOR-MAPPED-ADDRESS */ 
     422    pj_stun_msg_add_sockaddr_attr(tdata->pool, tdata->msg, 
     423                                  PJ_STUN_ATTR_XOR_MAPPED_ADDR, PJ_TRUE, 
     424                                  &alloc->hkey.clt_addr, 
     425                                  pj_sockaddr_get_len(&alloc->hkey.clt_addr)); 
     426     
     427    /* Send the response */ 
     428    pj_stun_session_send_msg(srv->core.stun_sess[listener->id], PJ_TRUE, 
     429                             src_addr, src_addr_len, tdata); 
     430 
     431    /* Done. */ 
     432    return PJ_SUCCESS; 
    326433} 
    327434 
     
    331438                               pjturn_pkt *pkt) 
    332439{ 
    333     pj_stun_msg *req, *res; 
    334440    unsigned options, lis_id; 
    335441    pj_status_t status; 
     
    392498     */ 
    393499    if (alloc) { 
    394         pjturn_allocation_on_rx_pkt(alloc, pkt); 
     500        pjturn_allocation_on_rx_client_pkt(alloc, pkt); 
    395501    } else { 
    396502        /* Otherwise this is a new client */ 
Note: See TracChangeset for help on using the changeset viewer.