/* Timer timeout callback */ static void relay_timeout_cb(pj_timer_heap_t *heap, pj_timer_entry *e) { pj_turn_relay_res *rel; pj_turn_allocation *alloc; PJ_UNUSED_ARG(heap); rel = (pj_turn_relay_res*) e->user_data; alloc = rel->allocation; if (e->id == TIMER_ID_TIMEOUT) { e->id = TIMER_ID_NONE; PJ_LOG(4,(alloc->obj_name, "Client %s refresh timed-out, shutting down..", alloc->info)); alloc_shutdown(alloc); } else if (e->id == TIMER_ID_DESTROY) { e->id = TIMER_ID_NONE; PJ_LOG(4,(alloc->obj_name, "Client %s destroying..", alloc->info)); destroy_allocation(alloc); } }
/* * Handle transport closure. */ PJ_DEF(void) pj_turn_allocation_on_transport_closed( pj_turn_allocation *alloc, pj_turn_transport *tp) { PJ_LOG(5,(alloc->obj_name, "Transport %s unexpectedly closed, destroying " "allocation %s", tp->info, alloc->info)); pj_turn_transport_dec_ref(tp, alloc); alloc->transport = NULL; destroy_allocation(alloc); }
void* profiler_allocf(mrb_state *mrb, void *p, size_t size, void *ud, const char *file, uint32_t line) { if (size == 0) { allocation_t *prev = NULL; allocation_t *curr = allocations_list; // find associated entry while( (curr != NULL) && (curr->address != p) ){ prev = curr; curr = curr->next; } if( curr != NULL ){ // printf("[%s][%s:%d] free()\n", (const char *)ud, file, line); if( prev ){ prev->next = curr->next; } else { allocations_list = NULL; } destroy_allocation(curr); } else { // printf("allocation not found: %s:%d !!! \n", file, line); } free(p); return NULL; } else { void *addr = realloc(p, size); allocation_t *curr = create_allocation(mrb, file, line, addr, size); if( allocations_list == NULL ){ allocations_list = curr; } else { // find tip allocation_t *tip = allocations_list_tip(); // and add it at the end tip->next = curr; } // printf("[%s][%s:%d] malloc(%zd)\n", (const char *)ud, file, line, size); // print_backtrace(); return addr; } }
PJ_DECL(void) pj_turn_allocation_destroy(pj_turn_allocation *alloc) { destroy_allocation(alloc); }
/* * Create new allocation. */ PJ_DEF(pj_status_t) pj_turn_allocation_create(pj_turn_transport *transport, const pj_sockaddr_t *src_addr, unsigned src_addr_len, const pj_stun_rx_data *rdata, pj_stun_session *srv_sess, pj_turn_allocation **p_alloc) { pj_turn_srv *srv = transport->listener->server; const pj_stun_msg *msg = rdata->msg; pj_pool_t *pool; alloc_request req; pj_turn_allocation *alloc; pj_stun_session_cb sess_cb; char str_tmp[80]; pj_status_t status; /* Parse ALLOCATE request */ status = parse_allocate_req(&req, srv_sess, rdata, src_addr, src_addr_len); if (status != PJ_SUCCESS) return status; pool = pj_pool_create(srv->core.pf, "alloc%p", 1000, 1000, NULL); /* Init allocation structure */ alloc = PJ_POOL_ZALLOC_T(pool, pj_turn_allocation); alloc->pool = pool; alloc->obj_name = pool->obj_name; alloc->relay.tp.sock = PJ_INVALID_SOCKET; alloc->server = transport->listener->server; alloc->bandwidth = req.bandwidth; /* Set transport */ alloc->transport = transport; pj_turn_transport_add_ref(transport, alloc); alloc->hkey.tp_type = transport->listener->tp_type; pj_memcpy(&alloc->hkey.clt_addr, src_addr, src_addr_len); status = pj_lock_create_recursive_mutex(pool, alloc->obj_name, &alloc->lock); if (status != PJ_SUCCESS) { goto on_error; } /* Create peer hash table */ alloc->peer_table = pj_hash_create(pool, PEER_TABLE_SIZE); /* Create channel hash table */ alloc->ch_table = pj_hash_create(pool, PEER_TABLE_SIZE); /* Print info */ pj_ansi_strcpy(alloc->info, pj_turn_tp_type_name(transport->listener->tp_type)); alloc->info[3] = ':'; pj_sockaddr_print(src_addr, alloc->info+4, sizeof(alloc->info)-4, 3); /* Create STUN session to handle STUN communication with client */ pj_bzero(&sess_cb, sizeof(sess_cb)); sess_cb.on_send_msg = &stun_on_send_msg; sess_cb.on_rx_request = &stun_on_rx_request; sess_cb.on_rx_indication = &stun_on_rx_indication; status = pj_stun_session_create(&srv->core.stun_cfg, alloc->obj_name, &sess_cb, PJ_FALSE, NULL, &alloc->sess); if (status != PJ_SUCCESS) { goto on_error; } /* Attach to STUN session */ pj_stun_session_set_user_data(alloc->sess, alloc); /* Init authentication credential */ status = init_cred(alloc, msg); if (status != PJ_SUCCESS) { goto on_error; } /* Attach authentication credential to STUN session */ pj_stun_session_set_credential(alloc->sess, PJ_STUN_AUTH_LONG_TERM, &alloc->cred); /* Create the relay resource */ status = create_relay(srv, alloc, msg, &req, &alloc->relay); if (status != PJ_SUCCESS) { goto on_error; } /* Register this allocation */ pj_turn_srv_register_allocation(srv, alloc); /* Respond to ALLOCATE request */ status = send_allocate_response(alloc, srv_sess, transport, rdata); if (status != PJ_SUCCESS) goto on_error; /* Done */ pj_sockaddr_print(&alloc->relay.hkey.addr, str_tmp, sizeof(str_tmp), 3); PJ_LOG(4,(alloc->obj_name, "Client %s created, relay addr=%s:%s", alloc->info, pj_turn_tp_type_name(req.tp_type), str_tmp)); /* Success */ *p_alloc = alloc; return PJ_SUCCESS; on_error: /* Send reply to the ALLOCATE request */ pj_strerror(status, str_tmp, sizeof(str_tmp)); pj_stun_session_respond(srv_sess, rdata, PJ_STUN_SC_BAD_REQUEST, str_tmp, transport, PJ_TRUE, src_addr, src_addr_len); /* Cleanup */ destroy_allocation(alloc); return status; }