Changeset 1850 for pjproject/trunk/pjnath/src/pjturn-srv/server.c
- Timestamp:
- Mar 8, 2008 12:54:04 AM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjnath/src/pjturn-srv/server.c
r1812 r1850 36 36 37 37 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 43 38 /* Prototypes */ 44 39 static pj_status_t on_tx_stun_msg( pj_stun_session *sess, … … 54 49 unsigned src_addr_len); 55 50 51 /* 52 * Get transport type name. 53 */ 54 PJ_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 } 56 64 57 65 /* … … 98 106 srv->tables.alloc = pj_hash_create(pool, MAX_CLIENTS); 99 107 srv->tables.res = pj_hash_create(pool, MAX_CLIENTS); 100 srv->tables.peer = pj_hash_create(pool, MAX_CLIENTS*MAX_PEERS_PER_CLIENT);101 108 102 109 /* Init ports settings */ … … 162 169 lis->id = index; 163 170 srv->core.lis_cnt++; 171 172 return PJ_SUCCESS; 173 } 174 175 /** 176 * Register an allocation. 177 */ 178 PJ_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 */ 196 PJ_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); 164 206 165 207 return PJ_SUCCESS; … … 185 227 186 228 /* 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) 229 static 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) 190 233 { 191 234 pj_status_t status; … … 194 237 195 238 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), 197 240 &tdata); 198 241 if (status != PJ_SUCCESS) 199 return stat ys;242 return status; 200 243 201 244 status = pj_stun_session_send_msg(sess, cache, dst_addr, addr_len, tdata); … … 221 264 222 265 /* 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); 224 268 if (attr_bw) { 225 269 cfg->bandwidth = attr_bw->value; … … 230 274 /* Check if we can satisfy the bandwidth */ 231 275 if (cfg->bandwidth > MAX_CLIENT_BANDWIDTH) { 232 respond_error(sess, msg, PJ_FALSE,276 respond_error(sess, req, PJ_FALSE, 233 277 PJ_STUN_SC_ALLOCATION_QUOTA_REACHED, 234 278 "Invalid bandwidth", src_addr, src_addr_len); … … 237 281 238 282 /* 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); 240 285 if (attr_req_tp) { 241 286 cfg->tp_type = PJ_STUN_GET_RT_PROTO(attr_req_tp->value); … … 246 291 /* Can only support UDP for now */ 247 292 if (cfg->tp_type != PJTURN_TP_UDP) { 248 respond_error(sess, msg, PJ_FALSE,293 respond_error(sess, req, PJ_FALSE, 249 294 PJ_STUN_SC_UNSUPP_TRANSPORT_PROTO, 250 295 NULL, src_addr, src_addr_len); … … 253 298 254 299 /* 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); 256 302 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); 259 305 } 260 306 261 307 /* 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); 263 310 if (attr_rpp) { 264 311 cfg->rpp_bits = PJ_STUN_GET_RPP_BITS(attr_rpp->value); … … 270 317 271 318 /* 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); 273 321 if (attr_lifetime) { 274 322 cfg->lifetime = attr_lifetime->value; 275 323 if (cfg->lifetime < MIN_LIFETIME || cfg->lifetime > MAX_LIFETIME) { 276 respond_error(sess, msg, PJ_FALSE,324 respond_error(sess, req, PJ_FALSE, 277 325 PJ_STUN_SC_BAD_REQUEST, 278 326 "Invalid LIFETIME value", src_addr, … … 296 344 { 297 345 pjturn_listener *listener; 346 pjturn_srv *srv; 298 347 pjturn_allocation_req req; 348 pjturn_allocation *alloc; 349 pj_stun_tx_data *tdata; 299 350 pj_status_t status; 300 351 301 352 listener = (pjturn_listener*) pj_stun_session_get_user_data(sess); 353 srv = listener->server; 302 354 303 355 /* Handle strayed REFRESH request */ … … 322 374 return status; 323 375 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; 326 433 } 327 434 … … 331 438 pjturn_pkt *pkt) 332 439 { 333 pj_stun_msg *req, *res;334 440 unsigned options, lis_id; 335 441 pj_status_t status; … … 392 498 */ 393 499 if (alloc) { 394 pjturn_allocation_on_rx_ pkt(alloc, pkt);500 pjturn_allocation_on_rx_client_pkt(alloc, pkt); 395 501 } else { 396 502 /* Otherwise this is a new client */
Note: See TracChangeset
for help on using the changeset viewer.