Ignore:
Timestamp:
Mar 9, 2008 12:55:00 PM (16 years ago)
Author:
bennylp
Message:

More work for ticket #485: updated pjnath with TURN-07 and added authentication in the server

File:
1 edited

Legend:

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

    r1850 r1852  
    1818 */ 
    1919#include "turn.h" 
     20#include "auth.h" 
     21 
    2022 
    2123#define THIS_FILE   "allocation.c" 
     
    3133#define PEER_TABLE_SIZE     32 
    3234 
     35#define MAX_CLIENT_BANDWIDTH    128  /* In Kbps */ 
     36#define DEFA_CLIENT_BANDWIDTH   64 
     37 
     38#define MIN_LIFETIME            30 
     39#define MAX_LIFETIME            600 
     40#define DEF_LIFETIME            300 
     41 
     42 
     43 
    3344/* ChannelData header */ 
    3445typedef struct channel_data_hdr 
     
    3950 
    4051 
     52/* Parsed Allocation request. */ 
     53typedef struct alloc_request 
     54{ 
     55    unsigned            tp_type;                    /* Requested transport  */ 
     56    char                addr[PJ_INET6_ADDRSTRLEN];  /* Requested IP         */ 
     57    unsigned            bandwidth;                  /* Requested bandwidth  */ 
     58    unsigned            lifetime;                   /* Lifetime.            */ 
     59    unsigned            rpp_bits;                   /* A bits               */ 
     60    unsigned            rpp_port;                   /* Requested port       */ 
     61} alloc_request; 
     62 
     63 
     64 
    4165/* Prototypes */ 
    42 static pj_status_t create_relay(pjturn_allocation *alloc, 
    43                                 const pjturn_allocation_req *req); 
     66static void destroy_allocation(pj_turn_allocation *alloc); 
     67static pj_status_t create_relay(pj_turn_srv *srv, 
     68                                pj_turn_allocation *alloc, 
     69                                const pj_stun_msg *msg, 
     70                                const alloc_request *req, 
     71                                pj_turn_relay_res *relay); 
     72static void destroy_relay(pj_turn_relay_res *relay); 
    4473static void on_rx_from_peer(pj_ioqueue_key_t *key,  
    4574                            pj_ioqueue_op_key_t *op_key,  
    4675                            pj_ssize_t bytes_read); 
    47 static void destroy_relay(pjturn_relay_res *relay); 
    4876static pj_status_t stun_on_send_msg(pj_stun_session *sess, 
    4977                                    const void *pkt, 
     
    6593 
    6694/* Log allocation error */ 
    67 static void alloc_err(pjturn_allocation *alloc, const char *title, 
     95static void alloc_err(pj_turn_allocation *alloc, const char *title, 
    6896                      pj_status_t status) 
    6997{ 
     
    75103} 
    76104 
     105 
     106/* Parse ALLOCATE request */ 
     107static pj_status_t parse_allocate_req(alloc_request *cfg, 
     108                                      pj_stun_session *sess, 
     109                                      const pj_stun_msg *req, 
     110                                      const pj_sockaddr_t *src_addr, 
     111                                      unsigned src_addr_len) 
     112{ 
     113    pj_stun_bandwidth_attr *attr_bw; 
     114    pj_stun_req_transport_attr *attr_req_tp; 
     115    pj_stun_res_token_attr *attr_res_token; 
     116    pj_stun_req_props_attr *attr_rpp; 
     117    pj_stun_lifetime_attr *attr_lifetime; 
     118 
     119    pj_bzero(cfg, sizeof(*cfg)); 
     120 
     121    /* Get BANDWIDTH attribute, if any. */ 
     122    attr_bw = (pj_stun_uint_attr*) 
     123              pj_stun_msg_find_attr(req, PJ_STUN_ATTR_BANDWIDTH, 0); 
     124    if (attr_bw) { 
     125        cfg->bandwidth = attr_bw->value; 
     126    } else { 
     127        cfg->bandwidth = DEFA_CLIENT_BANDWIDTH; 
     128    } 
     129 
     130    /* Check if we can satisfy the bandwidth */ 
     131    if (cfg->bandwidth > MAX_CLIENT_BANDWIDTH) { 
     132        pj_stun_session_respond(sess, req, PJ_STUN_SC_ALLOCATION_QUOTA_REACHED, 
     133                                "Invalid bandwidth", PJ_TRUE, 
     134                                src_addr, src_addr_len); 
     135        return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_ALLOCATION_QUOTA_REACHED); 
     136    } 
     137 
     138    /* MUST have REQUESTED-TRANSPORT attribute */ 
     139    attr_req_tp = (pj_stun_uint_attr*) 
     140                  pj_stun_msg_find_attr(req, PJ_STUN_ATTR_REQ_TRANSPORT, 0); 
     141    if (attr_req_tp == NULL) { 
     142        pj_stun_session_respond(sess, req, PJ_STUN_SC_BAD_REQUEST, 
     143                                "Missing REQUESTED-TRANSPORT attribute",  
     144                                PJ_TRUE, src_addr, src_addr_len); 
     145        return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_BAD_REQUEST); 
     146    } 
     147 
     148    cfg->tp_type = PJ_STUN_GET_RT_PROTO(attr_req_tp->value); 
     149 
     150    /* Can only support UDP for now */ 
     151    if (cfg->tp_type != PJ_TURN_TP_UDP) { 
     152        pj_stun_session_respond(sess, req, PJ_STUN_SC_UNSUPP_TRANSPORT_PROTO, 
     153                                NULL, PJ_TRUE, src_addr, src_addr_len); 
     154        return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_UNSUPP_TRANSPORT_PROTO); 
     155    } 
     156 
     157    /* Get RESERVATION-TOKEN attribute, if any */ 
     158    attr_res_token = (pj_stun_res_token_attr*) 
     159                     pj_stun_msg_find_attr(req, PJ_STUN_ATTR_RESERVATION_TOKEN, 
     160                                           0); 
     161    if (attr_res_token) { 
     162        /* We don't support RESERVATION-TOKEN for now */ 
     163        pj_stun_session_respond(sess, req,  
     164                                PJ_STUN_SC_BAD_REQUEST, 
     165                                "RESERVATION-TOKEN is not supported", PJ_TRUE,  
     166                                src_addr, src_addr_len); 
     167        return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_BAD_REQUEST); 
     168    } 
     169 
     170    /* Get REQUESTED-PROPS attribute, if any */ 
     171    attr_rpp = (pj_stun_req_props_attr*) 
     172               pj_stun_msg_find_attr(req, PJ_STUN_ATTR_REQ_PROPS, 0); 
     173    if (attr_rpp) { 
     174        /* We don't support REQUESTED-PROPS for now */ 
     175        pj_stun_session_respond(sess, req,  
     176                                PJ_STUN_SC_BAD_REQUEST, 
     177                                "REQUESTED-PROPS is not supported", PJ_TRUE,  
     178                                src_addr, src_addr_len); 
     179        return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_BAD_REQUEST); 
     180    } 
     181 
     182    /* Get LIFETIME attribute */ 
     183    attr_lifetime = (pj_stun_uint_attr*) 
     184                    pj_stun_msg_find_attr(req, PJ_STUN_ATTR_LIFETIME, 0); 
     185    if (attr_lifetime) { 
     186        cfg->lifetime = attr_lifetime->value; 
     187        if (cfg->lifetime < MIN_LIFETIME) { 
     188            pj_stun_session_respond(sess, req, PJ_STUN_SC_BAD_REQUEST, 
     189                                    "LIFETIME too short", PJ_TRUE,  
     190                                    src_addr, src_addr_len); 
     191            return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_BAD_REQUEST); 
     192        } 
     193        if (cfg->lifetime > MAX_LIFETIME) 
     194            cfg->lifetime = MAX_LIFETIME; 
     195    } else { 
     196        cfg->lifetime = DEF_LIFETIME; 
     197    } 
     198 
     199    return PJ_SUCCESS; 
     200} 
     201 
     202 
     203/* Respond to ALLOCATE request */ 
     204static pj_status_t send_allocate_response(pj_turn_allocation *alloc, 
     205                                          pj_stun_session *srv_sess, 
     206                                          const pj_stun_msg *msg) 
     207{ 
     208    pj_stun_tx_data *tdata; 
     209    pj_status_t status; 
     210 
     211    /* Respond the original ALLOCATE request */ 
     212    status = pj_stun_session_create_res(srv_sess, msg, 0, NULL, &tdata); 
     213    if (status != PJ_SUCCESS) 
     214        return status; 
     215 
     216    /* Add RELAYED-ADDRESS attribute */ 
     217    pj_stun_msg_add_sockaddr_attr(tdata->pool, tdata->msg, 
     218                                  PJ_STUN_ATTR_RELAY_ADDR, PJ_TRUE, 
     219                                  &alloc->relay.hkey.addr, 
     220                                  pj_sockaddr_get_len(&alloc->relay.hkey.addr)); 
     221 
     222    /* Add LIFETIME. */ 
     223    pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, 
     224                              PJ_STUN_ATTR_LIFETIME,  
     225                              (unsigned)alloc->relay.lifetime); 
     226 
     227    /* Add BANDWIDTH */ 
     228    pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, 
     229                              PJ_STUN_ATTR_BANDWIDTH, 
     230                              alloc->bandwidth); 
     231 
     232    /* Add RESERVATION-TOKEN */ 
     233    PJ_TODO(ADD_RESERVATION_TOKEN); 
     234 
     235    /* Add XOR-MAPPED-ADDRESS */ 
     236    pj_stun_msg_add_sockaddr_attr(tdata->pool, tdata->msg, 
     237                                  PJ_STUN_ATTR_XOR_MAPPED_ADDR, PJ_TRUE, 
     238                                  &alloc->hkey.clt_addr, 
     239                                  pj_sockaddr_get_len(&alloc->hkey.clt_addr)); 
     240     
     241    /* Send the response */ 
     242    return pj_stun_session_send_msg(srv_sess, PJ_TRUE, 
     243                                    &alloc->hkey.clt_addr,  
     244                                    pj_sockaddr_get_len(&alloc->hkey.clt_addr), 
     245                                    tdata); 
     246} 
     247 
     248 
     249/* 
     250 * Init credential for the allocation. We use static credential, meaning that 
     251 * the user's password must not change during allocation. 
     252 */ 
     253static pj_status_t init_cred(pj_turn_allocation *alloc, const pj_stun_msg *req) 
     254{ 
     255    const pj_stun_username_attr *user; 
     256    const pj_stun_realm_attr *realm; 
     257    const pj_stun_nonce_attr *nonce; 
     258    pj_status_t status; 
     259 
     260    realm = (const pj_stun_realm_attr*) 
     261            pj_stun_msg_find_attr(req, PJ_STUN_ATTR_REALM, 0); 
     262    PJ_ASSERT_RETURN(realm != NULL, PJ_EBUG); 
     263 
     264    user = (const pj_stun_username_attr*) 
     265           pj_stun_msg_find_attr(req, PJ_STUN_ATTR_USERNAME, 0); 
     266    PJ_ASSERT_RETURN(user != NULL, PJ_EBUG); 
     267 
     268    nonce = (const pj_stun_nonce_attr*) 
     269            pj_stun_msg_find_attr(req, PJ_STUN_ATTR_NONCE, 0); 
     270    PJ_ASSERT_RETURN(nonce != NULL, PJ_EBUG); 
     271 
     272    /* Lookup the password */ 
     273    status = pj_turn_get_password(NULL, NULL, &realm->value,  
     274                                  &user->value, alloc->pool,  
     275                                  &alloc->cred.data.static_cred.data_type,  
     276                                  &alloc->cred.data.static_cred.data); 
     277    if (status != PJ_SUCCESS) 
     278        return status; 
     279 
     280    /* Save credential */ 
     281    alloc->cred.type = PJ_STUN_AUTH_CRED_STATIC; 
     282    pj_strdup(alloc->pool, &alloc->cred.data.static_cred.realm, &realm->value); 
     283    pj_strdup(alloc->pool, &alloc->cred.data.static_cred.username, &user->value); 
     284    pj_strdup(alloc->pool, &alloc->cred.data.static_cred.nonce, &nonce->value); 
     285 
     286    return PJ_SUCCESS; 
     287} 
     288 
     289 
    77290/* 
    78291 * Create new allocation. 
    79292 */ 
    80 PJ_DEF(pj_status_t) pjturn_allocation_create(pjturn_listener *listener, 
    81                                              const pj_sockaddr_t *src_addr, 
    82                                              unsigned src_addr_len, 
    83                                              const pj_stun_msg *msg, 
    84                                              const pjturn_allocation_req *req, 
    85                                              pjturn_allocation **p_alloc) 
    86 { 
    87     pjturn_srv *srv = listener->server; 
     293PJ_DEF(pj_status_t) pj_turn_allocation_create(pj_turn_listener *listener, 
     294                                              const pj_sockaddr_t *src_addr, 
     295                                              unsigned src_addr_len, 
     296                                              const pj_stun_msg *msg, 
     297                                              pj_stun_session *srv_sess, 
     298                                              pj_turn_allocation **p_alloc) 
     299{ 
     300    pj_turn_srv *srv = listener->server; 
    88301    pj_pool_t *pool; 
    89     pjturn_allocation *alloc; 
     302    alloc_request req; 
     303    pj_turn_allocation *alloc; 
    90304    pj_stun_session_cb sess_cb; 
    91     char relay_info[80]; 
     305    char str_tmp[80]; 
    92306    pj_status_t status; 
    93307 
     308    /* Parse ALLOCATE request */ 
     309    status = parse_allocate_req(&req, srv_sess, msg, src_addr, src_addr_len); 
     310    if (status != PJ_SUCCESS) 
     311        return status; 
     312 
    94313    pool = pj_pool_create(srv->core.pf, "alloc%p", 1000, 1000, NULL); 
    95314 
    96315    /* Init allocation structure */ 
    97     alloc = PJ_POOL_ZALLOC_T(pool, pjturn_allocation); 
     316    alloc = PJ_POOL_ZALLOC_T(pool, pj_turn_allocation); 
    98317    alloc->pool = pool; 
    99318    alloc->obj_name = pool->obj_name; 
     
    102321    alloc->relay.tp.sock = PJ_INVALID_SOCKET; 
    103322 
    104     alloc->bandwidth = req->bandwidth; 
     323    alloc->bandwidth = req.bandwidth; 
    105324 
    106325    alloc->hkey.tp_type = listener->tp_type; 
     
    110329                                            &alloc->lock); 
    111330    if (status != PJ_SUCCESS) { 
    112         pjturn_allocation_destroy(alloc); 
    113         return status; 
     331        goto on_error; 
    114332    } 
    115333 
     
    121339 
    122340    /* Print info */ 
    123     pj_ansi_strcpy(alloc->info, pjturn_tp_type_name(listener->tp_type)); 
     341    pj_ansi_strcpy(alloc->info, pj_turn_tp_type_name(listener->tp_type)); 
    124342    alloc->info[3] = ':'; 
    125343    pj_sockaddr_print(src_addr, alloc->info+4, sizeof(alloc->info)-4, 3); 
     
    133351                                    &sess_cb, PJ_FALSE, &alloc->sess); 
    134352    if (status != PJ_SUCCESS) { 
    135         pjturn_allocation_destroy(alloc); 
    136         return status; 
     353        goto on_error; 
    137354    } 
    138355 
     
    140357    pj_stun_session_set_user_data(alloc->sess, alloc); 
    141358 
     359    /* Init authentication credential */ 
     360    status = init_cred(alloc, msg); 
     361    if (status != PJ_SUCCESS) { 
     362        goto on_error; 
     363    } 
     364 
     365    /* Attach authentication credential to STUN session */ 
     366    pj_stun_session_set_credential(alloc->sess, &alloc->cred); 
     367 
    142368    /* Create the relay resource */ 
    143     status = pjturn_allocation_create_relay(srv, alloc, msg, req,  
    144                                             &alloc->relay); 
     369    status = create_relay(srv, alloc, msg, &req, &alloc->relay); 
    145370    if (status != PJ_SUCCESS) { 
    146         pjturn_allocation_destroy(alloc); 
    147         return status; 
     371        goto on_error; 
    148372    } 
    149373 
    150374    /* Register this allocation */ 
    151     pjturn_srv_register_allocation(srv, alloc); 
    152  
    153     pj_sockaddr_print(&alloc->relay.hkey.addr, relay_info,  
    154                       sizeof(relay_info), 3); 
     375    pj_turn_srv_register_allocation(srv, alloc); 
     376 
     377    /* Respond to ALLOCATE request */ 
     378    status = send_allocate_response(alloc, srv_sess, msg); 
     379    if (status != PJ_SUCCESS) 
     380        goto on_error; 
     381 
     382    /* Done */ 
     383    pj_sockaddr_print(&alloc->relay.hkey.addr, str_tmp,  
     384                      sizeof(str_tmp), 3); 
    155385    PJ_LOG(4,(alloc->obj_name, "Client %s created, relay addr=%s:%s",  
    156               alloc->info, pjturn_tp_type_name(req->tp_type), relay_info)); 
     386              alloc->info, pj_turn_tp_type_name(req.tp_type), str_tmp)); 
    157387 
    158388    /* Success */ 
    159389    *p_alloc = alloc; 
    160390    return PJ_SUCCESS; 
    161 } 
    162  
    163  
    164 /* 
    165  * Destroy allocation. 
    166  */ 
    167 PJ_DECL(void) pjturn_allocation_destroy(pjturn_allocation *alloc) 
    168 { 
    169     pj_pool_t *pool; 
    170  
    171     /* Unregister this allocation */ 
    172     pjturn_srv_unregister_allocation(alloc->listener->server, alloc); 
    173  
    174     /* Destroy relay */ 
    175     destroy_relay(&alloc->relay); 
    176  
    177     /* Must lock only after destroying relay otherwise deadlock */ 
    178     if (alloc->lock) { 
    179         pj_lock_acquire(alloc->lock); 
    180     } 
    181  
    182     /* Destroy STUN session */ 
    183     if (alloc->sess) { 
    184         pj_stun_session_destroy(alloc->sess); 
    185         alloc->sess = NULL; 
    186     } 
    187  
    188     /* Destroy lock */ 
    189     if (alloc->lock) { 
    190         pj_lock_release(alloc->lock); 
    191         pj_lock_destroy(alloc->lock); 
    192         alloc->lock = NULL; 
    193     } 
    194  
    195     /* Destroy pool */ 
    196     pool = alloc->pool; 
    197     if (pool) { 
    198         alloc->pool = NULL; 
    199         pj_pool_release(pool); 
    200     } 
     391 
     392on_error: 
     393    /* Send reply to the ALLOCATE request */ 
     394    pj_strerror(status, str_tmp, sizeof(str_tmp)); 
     395    pj_stun_session_respond(srv_sess, msg, PJ_STUN_SC_BAD_REQUEST, str_tmp,  
     396                            PJ_TRUE, src_addr, src_addr_len); 
     397 
     398    /* Cleanup */ 
     399    destroy_allocation(alloc); 
     400    return status; 
    201401} 
    202402 
    203403 
    204404/* Destroy relay resource */ 
    205 static void destroy_relay(pjturn_relay_res *relay) 
     405static void destroy_relay(pj_turn_relay_res *relay) 
    206406{ 
    207407    if (relay->timer.id) { 
     
    224424} 
    225425 
    226 /* Initiate shutdown sequence for this allocation */ 
    227 static void alloc_shutdown(pjturn_allocation *alloc) 
     426 
     427/* 
     428 * Really destroy allocation. 
     429 */ 
     430static void destroy_allocation(pj_turn_allocation *alloc) 
     431{ 
     432    pj_pool_t *pool; 
     433 
     434    /* Unregister this allocation */ 
     435    pj_turn_srv_unregister_allocation(alloc->listener->server, alloc); 
     436 
     437    /* Destroy relay */ 
     438    destroy_relay(&alloc->relay); 
     439 
     440    /* Must lock only after destroying relay otherwise deadlock */ 
     441    if (alloc->lock) { 
     442        pj_lock_acquire(alloc->lock); 
     443    } 
     444 
     445    /* Destroy STUN session */ 
     446    if (alloc->sess) { 
     447        pj_stun_session_destroy(alloc->sess); 
     448        alloc->sess = NULL; 
     449    } 
     450 
     451    /* Destroy lock */ 
     452    if (alloc->lock) { 
     453        pj_lock_release(alloc->lock); 
     454        pj_lock_destroy(alloc->lock); 
     455        alloc->lock = NULL; 
     456    } 
     457 
     458    /* Destroy pool */ 
     459    pool = alloc->pool; 
     460    if (pool) { 
     461        alloc->pool = NULL; 
     462        pj_pool_release(pool); 
     463    } 
     464} 
     465 
     466 
     467PJ_DECL(void) pj_turn_allocation_destroy(pj_turn_allocation *alloc) 
     468{ 
     469    destroy_allocation(alloc); 
     470} 
     471 
     472 
     473/* Initiate shutdown sequence for this allocation and start destroy timer. 
     474 * Once allocation is marked as shutting down, any packets will be  
     475 * rejected/discarded  
     476 */ 
     477static void alloc_shutdown(pj_turn_allocation *alloc) 
    228478{ 
    229479    pj_time_val destroy_delay = DESTROY_DELAY; 
     
    231481    /* Work with existing schedule */ 
    232482    if (alloc->relay.timer.id == TIMER_ID_TIMEOUT) { 
    233         /* Cancel existing timer */ 
     483        /* Cancel existing shutdown timer */ 
    234484        pj_timer_heap_cancel(alloc->listener->server->core.timer_heap, 
    235485                             &alloc->relay.timer); 
     
    258508} 
    259509 
     510 
    260511/* Reschedule timeout using current lifetime setting */ 
    261 static pj_status_t resched_timeout(pjturn_allocation *alloc) 
     512static pj_status_t resched_timeout(pj_turn_allocation *alloc) 
    262513{ 
    263514    pj_time_val delay; 
     
    292543static void relay_timeout_cb(pj_timer_heap_t *heap, pj_timer_entry *e) 
    293544{ 
    294     pjturn_relay_res *rel; 
    295     pjturn_allocation *alloc; 
    296  
    297     rel = (pjturn_relay_res*) e->user_data; 
     545    pj_turn_relay_res *rel; 
     546    pj_turn_allocation *alloc; 
     547 
     548    PJ_UNUSED_ARG(heap); 
     549 
     550    rel = (pj_turn_relay_res*) e->user_data; 
    298551    alloc = rel->allocation; 
    299552 
     
    314567                  alloc->info)); 
    315568 
    316         pjturn_allocation_destroy(alloc); 
     569        destroy_allocation(alloc); 
    317570    } 
    318571} 
     
    322575 * Create relay. 
    323576 */ 
    324 PJ_DEF(pj_status_t) pjturn_allocation_create_relay(pjturn_srv *srv, 
    325                                                    pjturn_allocation *alloc, 
    326                                                    const pj_stun_msg *msg, 
    327                                                    const pjturn_allocation_req *req, 
    328                                                    pjturn_relay_res *relay) 
     577static pj_status_t create_relay(pj_turn_srv *srv, 
     578                                pj_turn_allocation *alloc, 
     579                                const pj_stun_msg *msg, 
     580                                const alloc_request *req, 
     581                                pj_turn_relay_res *relay) 
    329582{ 
    330583    enum { RETRY = 40 }; 
     
    366619 
    367620    /* Create the socket */ 
    368     if (req->tp_type == PJTURN_TP_UDP) { 
     621    if (req->tp_type == PJ_TURN_TP_UDP) { 
    369622        sock_type = pj_SOCK_DGRAM(); 
    370     } else if (req->tp_type == PJTURN_TP_TCP) { 
     623    } else if (req->tp_type == PJ_TURN_TP_TCP) { 
    371624        sock_type = pj_SOCK_STREAM(); 
    372625    } else { 
     
    396649        if (req->rpp_port) { 
    397650            port = (pj_uint16_t) req->rpp_port; 
    398         } else if (req->tp_type == PJTURN_TP_UDP) { 
     651        } else if (req->tp_type == PJ_TURN_TP_UDP) { 
    399652            port = (pj_uint16_t) srv->ports.next_udp++; 
    400653            if (srv->ports.next_udp > srv->ports.max_udp) 
    401654                srv->ports.next_udp = srv->ports.min_udp; 
    402         } else if (req->tp_type == PJTURN_TP_TCP) { 
     655        } else if (req->tp_type == PJ_TURN_TP_TCP) { 
    403656            port = (pj_uint16_t) srv->ports.next_tcp++; 
    404657            if (srv->ports.next_tcp > srv->ports.max_tcp) 
     
    406659        } else { 
    407660            pj_assert(!"Invalid transport"); 
     661            port = 0; 
    408662        } 
    409663 
     
    464718 
    465719/* Create and send error response */ 
    466 static void send_reply_err(pjturn_allocation *alloc, 
     720static void send_reply_err(pj_turn_allocation *alloc, 
    467721                           const pj_stun_msg *req, 
    468722                           pj_bool_t cache,  
     
    470724{ 
    471725    pj_status_t status; 
    472     pj_str_t reason; 
    473     pj_stun_tx_data *tdata; 
    474  
    475     status = pj_stun_session_create_res(alloc->sess, req,  
    476                                         code, (errmsg?pj_cstr(&reason,errmsg):NULL),  
    477                                         &tdata); 
    478     if (status != PJ_SUCCESS) { 
    479         alloc_err(alloc, "Error creating STUN error response", status); 
    480         return; 
    481     } 
    482  
    483     status = pj_stun_session_send_msg(alloc->sess, cache,  
    484                                       &alloc->hkey.clt_addr,   
    485                                       pj_sockaddr_get_len(&alloc->hkey.clt_addr),  
    486                                       tdata); 
     726 
     727    status = pj_stun_session_respond(alloc->sess, req, code, errmsg, cache, 
     728                                     &alloc->hkey.clt_addr, 
     729                                     pj_sockaddr_get_len(&alloc->hkey.clt_addr.addr)); 
    487730    if (status != PJ_SUCCESS) { 
    488731        alloc_err(alloc, "Error sending STUN error response", status); 
     
    492735 
    493736/* Create and send successful response */ 
    494 static void send_reply_ok(pjturn_allocation *alloc, 
     737static void send_reply_ok(pj_turn_allocation *alloc, 
    495738                          const pj_stun_msg *req) 
    496739{ 
     
    535778 
    536779/* Create new permission */ 
    537 static pjturn_permission *create_permission(pjturn_allocation *alloc, 
     780static pj_turn_permission *create_permission(pj_turn_allocation *alloc, 
    538781                                            const pj_sockaddr_t *peer_addr, 
    539782                                            unsigned addr_len) 
    540783{ 
    541     pjturn_permission *perm; 
    542  
    543     perm = PJ_POOL_ZALLOC_T(alloc->pool, pjturn_permission); 
     784    pj_turn_permission *perm; 
     785 
     786    perm = PJ_POOL_ZALLOC_T(alloc->pool, pj_turn_permission); 
    544787    pj_memcpy(&perm->hkey.peer_addr, peer_addr, addr_len); 
    545788     
    546     if (alloc->listener->tp_type == PJTURN_TP_UDP) { 
     789    if (alloc->listener->tp_type == PJ_TURN_TP_UDP) { 
    547790        perm->sock = alloc->listener->sock; 
    548791    } else { 
     
    552795 
    553796    perm->allocation = alloc; 
    554     perm->channel = PJTURN_INVALID_CHANNEL; 
     797    perm->channel = PJ_TURN_INVALID_CHANNEL; 
    555798 
    556799    pj_gettimeofday(&perm->expiry); 
    557     perm->expiry.sec += PJTURN_PERM_TIMEOUT; 
     800    perm->expiry.sec += PJ_TURN_PERM_TIMEOUT; 
    558801 
    559802    return perm; 
     
    561804 
    562805/* Check if a permission isn't expired. Return NULL if expired. */ 
    563 static pjturn_permission *check_permission_expiry(pjturn_permission *perm) 
    564 { 
    565     pjturn_allocation *alloc = perm->allocation; 
     806static pj_turn_permission *check_permission_expiry(pj_turn_permission *perm) 
     807{ 
     808    pj_turn_allocation *alloc = perm->allocation; 
    566809    pj_time_val now; 
    567810 
     
    577820 
    578821    /* Remove from channel hash table, if assigned a channel number */ 
    579     if (perm->channel != PJTURN_INVALID_CHANNEL) { 
     822    if (perm->channel != PJ_TURN_INVALID_CHANNEL) { 
    580823        pj_hash_set(NULL, alloc->ch_table, &perm->channel,  
    581824                    sizeof(perm->channel), 0, NULL); 
     
    586829 
    587830/* Lookup permission in hash table by the peer address */ 
    588 static pjturn_permission* 
    589 lookup_permission_by_addr(pjturn_allocation *alloc, 
     831static pj_turn_permission* 
     832lookup_permission_by_addr(pj_turn_allocation *alloc, 
    590833                          const pj_sockaddr_t *peer_addr, 
    591834                          unsigned addr_len) 
    592835{ 
    593     pjturn_permission_key key; 
    594     pjturn_permission *perm; 
     836    pj_turn_permission_key key; 
     837    pj_turn_permission *perm; 
    595838 
    596839    pj_bzero(&key, sizeof(key)); 
     
    598841 
    599842    /* Lookup in peer hash table */ 
    600     perm = (pjturn_permission*) pj_hash_get(alloc->peer_table, &key, 
     843    perm = (pj_turn_permission*) pj_hash_get(alloc->peer_table, &key, 
    601844                                            sizeof(key), NULL); 
    602845    return check_permission_expiry(perm); 
     
    604847 
    605848/* Lookup permission in hash table by the channel number */ 
    606 static pjturn_permission* 
    607 lookup_permission_by_chnum(pjturn_allocation *alloc, 
     849static pj_turn_permission* 
     850lookup_permission_by_chnum(pj_turn_allocation *alloc, 
    608851                           unsigned chnum) 
    609852{ 
    610853    pj_uint16_t chnum16 = (pj_uint16_t)chnum; 
    611     pjturn_permission *perm; 
     854    pj_turn_permission *perm; 
    612855 
    613856    /* Lookup in peer hash table */ 
    614     perm = (pjturn_permission*) pj_hash_get(alloc->peer_table, &chnum16, 
     857    perm = (pj_turn_permission*) pj_hash_get(alloc->peer_table, &chnum16, 
    615858                                            sizeof(chnum16), NULL); 
    616859    return check_permission_expiry(perm); 
     
    620863 * Return PJ_TRUE is permission is found. 
    621864 */ 
    622 static pj_bool_t refresh_permission(pjturn_permission *perm) 
     865static pj_bool_t refresh_permission(pj_turn_permission *perm) 
    623866{ 
    624867    pj_gettimeofday(&perm->expiry); 
    625     if (perm->channel == PJTURN_INVALID_CHANNEL) 
    626         perm->expiry.sec += PJTURN_PERM_TIMEOUT; 
     868    if (perm->channel == PJ_TURN_INVALID_CHANNEL) 
     869        perm->expiry.sec += PJ_TURN_PERM_TIMEOUT; 
    627870    else 
    628         perm->expiry.sec += PJTURN_CHANNEL_TIMEOUT; 
     871        perm->expiry.sec += PJ_TURN_CHANNEL_TIMEOUT; 
    629872    return PJ_TRUE; 
    630873} 
    631874 
    632875/* 
    633  * Handle incoming packet from client. 
     876 * Handle incoming packet from client. This would have been called by 
     877 * server upon receiving packet from a listener. 
    634878 */ 
    635 PJ_DEF(void) pjturn_allocation_on_rx_client_pkt( pjturn_allocation *alloc, 
    636                                                  pjturn_pkt *pkt) 
     879PJ_DEF(void) pj_turn_allocation_on_rx_client_pkt(pj_turn_allocation *alloc, 
     880                                                 pj_turn_pkt *pkt) 
    637881{ 
    638882    pj_bool_t is_stun; 
    639883    pj_status_t status; 
     884 
     885    /* Lock this allocation */ 
     886    pj_lock_acquire(alloc->lock); 
    640887 
    641888    /* Quickly check if this is STUN message */ 
     
    650897         */ 
    651898        unsigned options = PJ_STUN_CHECK_PACKET; 
    652         if (pkt->listener->tp_type == PJTURN_TP_UDP) 
     899        if (pkt->listener->tp_type == PJ_TURN_TP_UDP) 
    653900            options |= PJ_STUN_IS_DATAGRAM; 
    654901 
     
    659906        if (status != PJ_SUCCESS) { 
    660907            alloc_err(alloc, "Error handling STUN packet", status); 
    661             return; 
     908            goto on_return; 
    662909        } 
    663910 
     
    667914         */ 
    668915        channel_data_hdr *cd = (channel_data_hdr*)pkt->pkt; 
    669         pjturn_permission *perm; 
     916        pj_turn_permission *perm; 
    670917        pj_ssize_t len; 
    671918 
    672919        /* For UDP check the packet length */ 
    673         if (alloc->listener->tp_type == PJTURN_TP_UDP) { 
     920        if (alloc->listener->tp_type == PJ_TURN_TP_UDP) { 
    674921            if (pkt->len < pj_ntohs(cd->length)+sizeof(*cd)) { 
    675922                PJ_LOG(4,(alloc->obj_name,  
    676923                          "ChannelData from %s discarded: UDP size error", 
    677924                          alloc->info)); 
    678                 return; 
     925                goto on_return; 
    679926            } 
    680927        } else { 
    681928            pj_assert(!"Unsupported transport"); 
    682             return; 
     929            goto on_return; 
    683930        } 
    684931 
     
    689936                      "ChannelData from %s discarded: not found", 
    690937                      alloc->info)); 
    691             return; 
     938            goto on_return; 
    692939        } 
    693940 
     
    701948        refresh_permission(perm); 
    702949    } 
    703 } 
     950 
     951on_return: 
     952    /* Release lock */ 
     953    pj_lock_release(alloc->lock); 
     954} 
     955 
    704956 
    705957/* 
     
    707959 * on_rx_from_peer(). 
    708960 */ 
    709 static void on_rx_peer_pkt(pjturn_allocation *alloc, 
    710                            pjturn_relay_res *rel, 
    711                            char *pkt, pj_size_t len, 
    712                            const pj_sockaddr *src_addr) 
    713 { 
    714     pjturn_permission *perm; 
     961static void handle_peer_pkt(pj_turn_allocation *alloc, 
     962                            pj_turn_relay_res *rel, 
     963                            char *pkt, pj_size_t len, 
     964                            const pj_sockaddr *src_addr) 
     965{ 
     966    pj_turn_permission *perm; 
    715967 
    716968    /* Lookup permission */ 
     
    725977     * this permission is attached to a channel number. 
    726978     */ 
    727     if (perm->channel != PJTURN_INVALID_CHANNEL) { 
     979    if (perm->channel != PJ_TURN_INVALID_CHANNEL) { 
    728980        /* Send ChannelData */ 
    729981        channel_data_hdr *cd = (channel_data_hdr*)rel->tp.tx_pkt; 
    730982 
    731         if (len > PJTURN_MAX_PKT_LEN) { 
     983        if (len > PJ_TURN_MAX_PKT_LEN) { 
    732984            char peer_addr[80]; 
    733985            pj_sockaddr_print(src_addr, peer_addr, sizeof(peer_addr), 3); 
    734             PJ_LOG(1,(alloc->obj_name, "Client %s: discarded data from %s " 
     986            PJ_LOG(4,(alloc->obj_name, "Client %s: discarded data from %s " 
    735987                      "because it's too long (%d bytes)", 
    736988                      alloc->info, peer_addr, len)); 
     
    746998 
    747999        /* Send to client */ 
    748         pjturn_listener_sendto(alloc->listener, rel->tp.tx_pkt, 
     1000        pj_turn_listener_sendto(alloc->listener, rel->tp.tx_pkt, 
    7491001                               len+sizeof(channel_data_hdr), 0, 
    7501002                               &alloc->hkey.clt_addr, 
     
    7711023                            pj_ssize_t bytes_read) 
    7721024{ 
    773     pjturn_relay_res *rel; 
     1025    pj_turn_relay_res *rel; 
    7741026    pj_status_t status; 
    7751027 
    776     rel = (pjturn_relay_res*) pj_ioqueue_get_user_data(key); 
     1028    rel = (pj_turn_relay_res*) pj_ioqueue_get_user_data(key); 
     1029 
     1030    /* Lock the allocation */ 
     1031    pj_lock_acquire(rel->allocation->lock); 
    7771032 
    7781033    do { 
    7791034        if (bytes_read > 0) { 
    780             on_rx_peer_pkt(rel->allocation, rel, rel->tp.rx_pkt, 
    781                            bytes_read, &rel->tp.src_addr); 
     1035            handle_peer_pkt(rel->allocation, rel, rel->tp.rx_pkt, 
     1036                            bytes_read, &rel->tp.src_addr); 
    7821037        } 
    7831038 
     
    7951050    } while (status != PJ_EPENDING && status != PJ_ECANCELLED); 
    7961051 
     1052    /* Release allocation lock */ 
     1053    pj_lock_release(rel->allocation->lock); 
    7971054} 
    7981055 
     
    8071064                                    unsigned addr_len) 
    8081065{ 
    809     pjturn_allocation *alloc; 
    810  
    811     alloc = (pjturn_allocation*) pj_stun_session_get_user_data(sess); 
    812  
    813     return pjturn_listener_sendto(alloc->listener, pkt, pkt_size, 0, 
     1066    pj_turn_allocation *alloc; 
     1067 
     1068    alloc = (pj_turn_allocation*) pj_stun_session_get_user_data(sess); 
     1069 
     1070    return pj_turn_listener_sendto(alloc->listener, pkt, pkt_size, 0, 
    8141071                                  dst_addr, addr_len); 
    8151072} 
     
    8181075 * Callback notification from STUN session when it receives STUN 
    8191076 * requests. This callback was trigger by STUN incoming message 
    820  * processing in pjturn_allocation_on_rx_client_pkt(). 
     1077 * processing in pj_turn_allocation_on_rx_client_pkt(). 
    8211078 */ 
    8221079static pj_status_t stun_on_rx_request(pj_stun_session *sess, 
     
    8271084                                      unsigned src_addr_len) 
    8281085{ 
    829     pjturn_allocation *alloc; 
    830  
    831     alloc = (pjturn_allocation*) pj_stun_session_get_user_data(sess); 
     1086    pj_turn_allocation *alloc; 
     1087 
     1088    PJ_UNUSED_ARG(pkt); 
     1089    PJ_UNUSED_ARG(pkt_len); 
     1090    PJ_UNUSED_ARG(src_addr); 
     1091    PJ_UNUSED_ARG(src_addr_len); 
     1092 
     1093    alloc = (pj_turn_allocation*) pj_stun_session_get_user_data(sess); 
    8321094 
    8331095    /* Refuse to serve any request if we've been shutdown */ 
    8341096    if (alloc->relay.lifetime == 0) { 
     1097        /* Reject with 437 if we're shutting down */ 
    8351098        send_reply_err(alloc, msg, PJ_TRUE,  
    8361099                       PJ_STUN_SC_ALLOCATION_MISMATCH, NULL); 
     
    8951158        pj_stun_channel_number_attr *ch_attr; 
    8961159        pj_stun_peer_addr_attr *peer_attr; 
    897         pjturn_permission *p1, *p2; 
     1160        pj_turn_permission *p1, *p2; 
    8981161 
    8991162        ch_attr = (pj_stun_channel_number_attr*) 
     
    9341197        p2 = lookup_permission_by_addr(alloc, &peer_attr->sockaddr, 
    9351198                                       pj_sockaddr_get_len(&peer_attr->sockaddr)); 
    936         if (p2 && p2->channel != PJTURN_INVALID_CHANNEL) { 
     1199        if (p2 && p2->channel != PJ_TURN_INVALID_CHANNEL) { 
    9371200            send_reply_err(alloc, msg, PJ_TRUE, PJ_STUN_SC_BAD_REQUEST,  
    9381201                           "Peer address already assigned a channel number"); 
     
    9771240 * Callback notification from STUN session when it receives STUN 
    9781241 * indications. This callback was trigger by STUN incoming message 
    979  * processing in pjturn_allocation_on_rx_client_pkt(). 
     1242 * processing in pj_turn_allocation_on_rx_client_pkt(). 
    9801243 */ 
    9811244static pj_status_t stun_on_rx_indication(pj_stun_session *sess, 
     
    9881251    pj_stun_peer_addr_attr *peer_attr; 
    9891252    pj_stun_data_attr *data_attr; 
    990     pjturn_allocation *alloc; 
    991     pjturn_permission *perm; 
    992  
    993     alloc = (pjturn_allocation*) pj_stun_session_get_user_data(sess); 
     1253    pj_turn_allocation *alloc; 
     1254    pj_turn_permission *perm; 
     1255 
     1256    PJ_UNUSED_ARG(pkt); 
     1257    PJ_UNUSED_ARG(pkt_len); 
     1258    PJ_UNUSED_ARG(src_addr); 
     1259    PJ_UNUSED_ARG(src_addr_len); 
     1260 
     1261    alloc = (pj_turn_allocation*) pj_stun_session_get_user_data(sess); 
    9941262 
    9951263    /* Only expect Send Indication */ 
     
    10251293 
    10261294    /* Relay the data to client */ 
    1027     if (alloc->hkey.tp_type == PJTURN_TP_UDP) { 
     1295    if (alloc->hkey.tp_type == PJ_TURN_TP_UDP) { 
    10281296        pj_ssize_t len = data_attr->length; 
    10291297        pj_sock_sendto(alloc->listener->sock, data_attr->data,  
Note: See TracChangeset for help on using the changeset viewer.