/* $Id$ */ /* * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) * Copyright (C) 2003-2008 Benny Prijono * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "test.h" #if INCLUDE_CONCUR_TEST #define THIS_FILE "concur_test.c" /****************************************************************************/ #define WORKER_THREAD_CNT 4 #define SERVER_THREAD_CNT 4 #define MAX_SOCK_CLIENTS (PJ_IOQUEUE_MAX_HANDLES/2) struct stun_test_session { pj_stun_config stun_cfg; pj_lock_t *lock; pj_thread_t *worker_threads[WORKER_THREAD_CNT]; pj_sock_t server_sock; int server_port; pj_thread_t *server_threads[SERVER_THREAD_CNT]; pj_event_t *server_event; pj_bool_t thread_quit_flag; /* Test parameters: */ struct { int client_got_response; pj_bool_t server_wait_for_event; pj_bool_t server_drop_request; int client_sleep_after_start; int client_sleep_before_destroy; } param; }; static int server_thread_proc(void *p) { struct stun_test_session *test_sess = (struct stun_test_session*)p; pj_pool_t *pool; pj_status_t status; PJ_LOG(4,(THIS_FILE, "Server thread running")); pool = pj_pool_create(test_sess->stun_cfg.pf, "server", 512, 512, NULL); while (!test_sess->thread_quit_flag) { pj_time_val timeout = {0, 10}; pj_fd_set_t rdset; int n; /* Serve client */ PJ_FD_ZERO(&rdset); PJ_FD_SET(test_sess->server_sock, &rdset); n = pj_sock_select((int)test_sess->server_sock+1, &rdset, NULL, NULL, &timeout); if (n==1 && PJ_FD_ISSET(test_sess->server_sock, &rdset)) { pj_uint8_t pkt[512]; pj_ssize_t pkt_len; pj_size_t res_len; pj_sockaddr client_addr; int addr_len; pj_stun_msg *stun_req, *stun_res; pj_pool_reset(pool); /* Got query */ pkt_len = sizeof(pkt); addr_len = sizeof(client_addr); status = pj_sock_recvfrom(test_sess->server_sock, pkt, &pkt_len, 0, &client_addr, &addr_len); if (status != PJ_SUCCESS) { continue; } status = pj_stun_msg_decode(pool, pkt, pkt_len, PJ_STUN_IS_DATAGRAM, &stun_req, NULL, NULL); if (status != PJ_SUCCESS) { PJ_PERROR(1,(THIS_FILE, status, "STUN request decode error")); continue; } status = pj_stun_msg_create_response(pool, stun_req, PJ_STUN_SC_BAD_REQUEST, NULL, &stun_res); if (status != PJ_SUCCESS) { PJ_PERROR(1,(THIS_FILE, status, "STUN create response error")); continue; } status = pj_stun_msg_encode(stun_res, pkt, sizeof(pkt), 0, NULL, &res_len); if (status != PJ_SUCCESS) { PJ_PERROR(1,(THIS_FILE, status, "STUN encode error")); continue; } /* Ignore request */ if (test_sess->param.server_drop_request) continue; /* Wait for signal to continue */ if (test_sess->param.server_wait_for_event) pj_event_wait(test_sess->server_event); pkt_len = res_len; pj_sock_sendto(test_sess->server_sock, pkt, &pkt_len, 0, &client_addr, pj_sockaddr_get_len(&client_addr)); } } pj_pool_release(pool); PJ_LOG(4,(THIS_FILE, "Server thread quitting")); return 0; } static int worker_thread_proc(void *p) { struct stun_test_session *test_sess = (struct stun_test_session*)p; PJ_LOG(4,(THIS_FILE, "Worker thread running")); while (!test_sess->thread_quit_flag) { pj_time_val timeout = {0, 10}; pj_timer_heap_poll(test_sess->stun_cfg.timer_heap, NULL); pj_ioqueue_poll(test_sess->stun_cfg.ioqueue, &timeout); } PJ_LOG(4,(THIS_FILE, "Worker thread quitting")); return 0; } static pj_bool_t stun_sock_on_status(pj_stun_sock *stun_sock, pj_stun_sock_op op, pj_status_t status) { struct stun_test_session *test_sess = (struct stun_test_session*)pj_stun_sock_get_user_data(stun_sock); PJ_UNUSED_ARG(op); PJ_UNUSED_ARG(status); test_sess->param.client_got_response++; return PJ_TRUE; } static int stun_destroy_test_session(struct stun_test_session *test_sess) { unsigned i; pj_stun_sock_cb stun_cb; pj_status_t status; pj_stun_sock *stun_sock[MAX_SOCK_CLIENTS]; pj_bzero(&stun_cb, sizeof(stun_cb)); stun_cb.on_status = &stun_sock_on_status; pj_event_reset(test_sess->server_event); /* Create all clients first */ for (i=0; istun_cfg, name, pj_AF_INET(), &stun_cb, NULL, test_sess, &stun_sock[i]); if (status != PJ_SUCCESS) { PJ_PERROR(1,(THIS_FILE, status, "Error creating stun socket")); return -10; } } /* Start resolution */ for (i=0; iserver_port, NULL); if (status != PJ_SUCCESS) { PJ_PERROR(1,(THIS_FILE, status, "Error starting stun socket")); return -20; } } /* settle down */ pj_thread_sleep(test_sess->param.client_sleep_after_start); /* Resume server threads */ pj_event_set(test_sess->server_event); pj_thread_sleep(test_sess->param.client_sleep_before_destroy); /* Destroy clients */ for (i=0; i