Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #include "turn.h" |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 21 | #include "auth.h" |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 22 | |
| 23 | #define MAX_CLIENTS 32 |
| 24 | #define MAX_PEERS_PER_CLIENT 8 |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 25 | //#define MAX_HANDLES (MAX_CLIENTS*MAX_PEERS_PER_CLIENT+MAX_LISTENERS) |
| 26 | #define MAX_HANDLES PJ_IOQUEUE_MAX_HANDLES |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 27 | #define MAX_TIMER (MAX_HANDLES * 2) |
| 28 | #define MIN_PORT 49152 |
| 29 | #define MAX_PORT 65535 |
| 30 | #define MAX_LISTENERS 16 |
| 31 | #define MAX_THREADS 2 |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 32 | #define MAX_NET_EVENTS 1000 |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 33 | |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 34 | /* Prototypes */ |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 35 | static int server_thread_proc(void *arg); |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 36 | static pj_status_t on_tx_stun_msg( pj_stun_session *sess, |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 37 | void *token, |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 38 | const void *pkt, |
| 39 | pj_size_t pkt_size, |
| 40 | const pj_sockaddr_t *dst_addr, |
| 41 | unsigned addr_len); |
| 42 | static pj_status_t on_rx_stun_request(pj_stun_session *sess, |
| 43 | const pj_uint8_t *pkt, |
| 44 | unsigned pkt_len, |
Benny Prijono | a5d214f | 2008-03-19 23:00:30 +0000 | [diff] [blame] | 45 | const pj_stun_rx_data *rdata, |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 46 | void *user_data, |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 47 | const pj_sockaddr_t *src_addr, |
| 48 | unsigned src_addr_len); |
| 49 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 50 | struct saved_cred |
| 51 | { |
| 52 | pj_str_t realm; |
| 53 | pj_str_t username; |
| 54 | pj_str_t nonce; |
| 55 | int data_type; |
| 56 | pj_str_t data; |
| 57 | }; |
| 58 | |
| 59 | |
Benny Prijono | b05aafc | 2008-03-08 00:54:04 +0000 | [diff] [blame] | 60 | /* |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 61 | * Get transport type name, normally for logging purpose only. |
Benny Prijono | b05aafc | 2008-03-08 00:54:04 +0000 | [diff] [blame] | 62 | */ |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 63 | PJ_DEF(const char*) pj_turn_tp_type_name(int tp_type) |
Benny Prijono | b05aafc | 2008-03-08 00:54:04 +0000 | [diff] [blame] | 64 | { |
| 65 | /* Must be 3 characters long! */ |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 66 | if (tp_type == PJ_TURN_TP_UDP) { |
Benny Prijono | b05aafc | 2008-03-08 00:54:04 +0000 | [diff] [blame] | 67 | return "UDP"; |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 68 | } else if (tp_type == PJ_TURN_TP_TCP) { |
Benny Prijono | b05aafc | 2008-03-08 00:54:04 +0000 | [diff] [blame] | 69 | return "TCP"; |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 70 | } else { |
| 71 | pj_assert(!"Unsupported transport"); |
Benny Prijono | b05aafc | 2008-03-08 00:54:04 +0000 | [diff] [blame] | 72 | return "???"; |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 73 | } |
Benny Prijono | b05aafc | 2008-03-08 00:54:04 +0000 | [diff] [blame] | 74 | } |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * Create server. |
| 78 | */ |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 79 | PJ_DEF(pj_status_t) pj_turn_srv_create(pj_pool_factory *pf, |
| 80 | pj_turn_srv **p_srv) |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 81 | { |
| 82 | pj_pool_t *pool; |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 83 | pj_stun_session_cb sess_cb; |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 84 | pj_turn_srv *srv; |
| 85 | unsigned i; |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 86 | pj_status_t status; |
| 87 | |
| 88 | PJ_ASSERT_RETURN(pf && p_srv, PJ_EINVAL); |
| 89 | |
| 90 | /* Create server and init core settings */ |
| 91 | pool = pj_pool_create(pf, "srv%p", 1000, 1000, NULL); |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 92 | srv = PJ_POOL_ZALLOC_T(pool, pj_turn_srv); |
| 93 | srv->obj_name = pool->obj_name; |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 94 | srv->core.pf = pf; |
| 95 | srv->core.pool = pool; |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 96 | srv->core.tls_key = srv->core.tls_data = -1; |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 97 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 98 | /* Create ioqueue */ |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 99 | status = pj_ioqueue_create(pool, MAX_HANDLES, &srv->core.ioqueue); |
| 100 | if (status != PJ_SUCCESS) |
| 101 | goto on_error; |
| 102 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 103 | /* Server mutex */ |
| 104 | status = pj_lock_create_recursive_mutex(pool, srv->obj_name, |
| 105 | &srv->core.lock); |
| 106 | if (status != PJ_SUCCESS) |
| 107 | goto on_error; |
| 108 | |
| 109 | /* Allocate TLS */ |
| 110 | status = pj_thread_local_alloc(&srv->core.tls_key); |
| 111 | if (status != PJ_SUCCESS) |
| 112 | goto on_error; |
| 113 | |
| 114 | status = pj_thread_local_alloc(&srv->core.tls_data); |
| 115 | if (status != PJ_SUCCESS) |
| 116 | goto on_error; |
| 117 | |
| 118 | /* Create timer heap */ |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 119 | status = pj_timer_heap_create(pool, MAX_TIMER, &srv->core.timer_heap); |
| 120 | if (status != PJ_SUCCESS) |
| 121 | goto on_error; |
| 122 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 123 | /* Configure lock for the timer heap */ |
| 124 | pj_timer_heap_set_lock(srv->core.timer_heap, srv->core.lock, PJ_FALSE); |
| 125 | |
| 126 | /* Array of listeners */ |
| 127 | srv->core.listener = (pj_turn_listener**) |
| 128 | pj_pool_calloc(pool, MAX_LISTENERS, |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 129 | sizeof(srv->core.listener[0])); |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 130 | |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 131 | /* Create hash tables */ |
| 132 | srv->tables.alloc = pj_hash_create(pool, MAX_CLIENTS); |
| 133 | srv->tables.res = pj_hash_create(pool, MAX_CLIENTS); |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 134 | |
| 135 | /* Init ports settings */ |
| 136 | srv->ports.min_udp = srv->ports.next_udp = MIN_PORT; |
Benny Prijono | 9e6d60a | 2008-03-20 16:32:06 +0000 | [diff] [blame] | 137 | srv->ports.max_udp = MAX_PORT; |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 138 | srv->ports.min_tcp = srv->ports.next_tcp = MIN_PORT; |
| 139 | srv->ports.max_tcp = MAX_PORT; |
| 140 | |
| 141 | /* Init STUN config */ |
| 142 | pj_stun_config_init(&srv->core.stun_cfg, pf, 0, srv->core.ioqueue, |
| 143 | srv->core.timer_heap); |
| 144 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 145 | /* Init STUN credential */ |
| 146 | srv->core.cred.type = PJ_STUN_AUTH_CRED_DYNAMIC; |
| 147 | srv->core.cred.data.dyn_cred.user_data = srv; |
| 148 | srv->core.cred.data.dyn_cred.get_auth = &pj_turn_get_auth; |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 149 | srv->core.cred.data.dyn_cred.get_password = &pj_turn_get_password; |
| 150 | srv->core.cred.data.dyn_cred.verify_nonce = &pj_turn_verify_nonce; |
| 151 | |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 152 | /* Create STUN session to handle new allocation */ |
| 153 | pj_bzero(&sess_cb, sizeof(sess_cb)); |
| 154 | sess_cb.on_rx_request = &on_rx_stun_request; |
| 155 | sess_cb.on_send_msg = &on_tx_stun_msg; |
| 156 | |
| 157 | status = pj_stun_session_create(&srv->core.stun_cfg, srv->obj_name, |
| 158 | &sess_cb, PJ_FALSE, &srv->core.stun_sess); |
| 159 | if (status != PJ_SUCCESS) { |
| 160 | goto on_error; |
| 161 | } |
| 162 | |
| 163 | pj_stun_session_set_user_data(srv->core.stun_sess, srv); |
| 164 | pj_stun_session_set_credential(srv->core.stun_sess, PJ_STUN_AUTH_LONG_TERM, |
| 165 | &srv->core.cred); |
| 166 | |
| 167 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 168 | /* Array of worker threads */ |
| 169 | srv->core.thread_cnt = MAX_THREADS; |
| 170 | srv->core.thread = (pj_thread_t**) |
| 171 | pj_pool_calloc(pool, srv->core.thread_cnt, |
| 172 | sizeof(pj_thread_t*)); |
| 173 | |
| 174 | /* Start the worker threads */ |
| 175 | for (i=0; i<srv->core.thread_cnt; ++i) { |
| 176 | status = pj_thread_create(pool, srv->obj_name, &server_thread_proc, |
| 177 | srv, 0, 0, &srv->core.thread[i]); |
| 178 | if (status != PJ_SUCCESS) |
| 179 | goto on_error; |
| 180 | } |
| 181 | |
| 182 | /* We're done. Application should add listeners now */ |
| 183 | PJ_LOG(4,(srv->obj_name, "TURN server v%s is running", |
| 184 | pj_get_version())); |
| 185 | |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 186 | *p_srv = srv; |
| 187 | return PJ_SUCCESS; |
| 188 | |
| 189 | on_error: |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 190 | pj_turn_srv_destroy(srv); |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 191 | return status; |
| 192 | } |
| 193 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 194 | |
| 195 | /* |
| 196 | * Handle timer and network events |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 197 | */ |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 198 | static void srv_handle_events(pj_turn_srv *srv, const pj_time_val *max_timeout) |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 199 | { |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 200 | /* timeout is 'out' var. This just to make compiler happy. */ |
| 201 | pj_time_val timeout = { 0, 0}; |
| 202 | unsigned net_event_count = 0; |
| 203 | int c; |
| 204 | |
| 205 | /* Poll the timer. The timer heap has its own mutex for better |
| 206 | * granularity, so we don't need to lock the server. |
| 207 | */ |
| 208 | timeout.sec = timeout.msec = 0; |
| 209 | c = pj_timer_heap_poll( srv->core.timer_heap, &timeout ); |
| 210 | |
| 211 | /* timer_heap_poll should never ever returns negative value, or otherwise |
| 212 | * ioqueue_poll() will block forever! |
| 213 | */ |
| 214 | pj_assert(timeout.sec >= 0 && timeout.msec >= 0); |
| 215 | if (timeout.msec >= 1000) timeout.msec = 999; |
| 216 | |
| 217 | /* If caller specifies maximum time to wait, then compare the value with |
| 218 | * the timeout to wait from timer, and use the minimum value. |
| 219 | */ |
| 220 | if (max_timeout && PJ_TIME_VAL_GT(timeout, *max_timeout)) { |
| 221 | timeout = *max_timeout; |
| 222 | } |
| 223 | |
| 224 | /* Poll ioqueue. |
| 225 | * Repeat polling the ioqueue while we have immediate events, because |
| 226 | * timer heap may process more than one events, so if we only process |
| 227 | * one network events at a time (such as when IOCP backend is used), |
| 228 | * the ioqueue may have trouble keeping up with the request rate. |
| 229 | * |
| 230 | * For example, for each send() request, one network event will be |
| 231 | * reported by ioqueue for the send() completion. If we don't poll |
| 232 | * the ioqueue often enough, the send() completion will not be |
| 233 | * reported in timely manner. |
| 234 | */ |
| 235 | do { |
| 236 | c = pj_ioqueue_poll( srv->core.ioqueue, &timeout); |
| 237 | if (c < 0) { |
| 238 | pj_thread_sleep(PJ_TIME_VAL_MSEC(timeout)); |
| 239 | return; |
| 240 | } else if (c == 0) { |
| 241 | break; |
| 242 | } else { |
| 243 | net_event_count += c; |
| 244 | timeout.sec = timeout.msec = 0; |
| 245 | } |
| 246 | } while (c > 0 && net_event_count < MAX_NET_EVENTS); |
| 247 | |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Server worker thread proc. |
| 252 | */ |
| 253 | static int server_thread_proc(void *arg) |
| 254 | { |
| 255 | pj_turn_srv *srv = (pj_turn_srv*)arg; |
| 256 | |
| 257 | while (!srv->core.quit) { |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 258 | pj_time_val timeout_max = {0, 100}; |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 259 | srv_handle_events(srv, &timeout_max); |
| 260 | } |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Destroy the server. |
| 267 | */ |
| 268 | PJ_DEF(pj_status_t) pj_turn_srv_destroy(pj_turn_srv *srv) |
| 269 | { |
| 270 | pj_hash_iterator_t itbuf, *it; |
| 271 | unsigned i; |
| 272 | |
| 273 | /* Stop all worker threads */ |
| 274 | srv->core.quit = PJ_TRUE; |
| 275 | for (i=0; i<srv->core.thread_cnt; ++i) { |
| 276 | if (srv->core.thread[i]) { |
| 277 | pj_thread_join(srv->core.thread[i]); |
| 278 | pj_thread_destroy(srv->core.thread[i]); |
| 279 | srv->core.thread[i] = NULL; |
| 280 | } |
| 281 | } |
| 282 | |
Benny Prijono | 9e6d60a | 2008-03-20 16:32:06 +0000 | [diff] [blame] | 283 | /* Destroy all allocations FIRST */ |
| 284 | if (srv->tables.alloc) { |
| 285 | it = pj_hash_first(srv->tables.alloc, &itbuf); |
| 286 | while (it != NULL) { |
| 287 | pj_turn_allocation *alloc = (pj_turn_allocation*) |
| 288 | pj_hash_this(srv->tables.alloc, it); |
| 289 | pj_hash_iterator_t *next = pj_hash_next(srv->tables.alloc, it); |
| 290 | pj_turn_allocation_destroy(alloc); |
| 291 | it = next; |
| 292 | } |
| 293 | } |
| 294 | |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 295 | /* Destroy all listeners. */ |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 296 | for (i=0; i<srv->core.lis_cnt; ++i) { |
| 297 | if (srv->core.listener[i]) { |
| 298 | pj_turn_listener_destroy(srv->core.listener[i]); |
| 299 | srv->core.listener[i] = NULL; |
| 300 | } |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* Destroy STUN session */ |
| 304 | if (srv->core.stun_sess) { |
| 305 | pj_stun_session_destroy(srv->core.stun_sess); |
| 306 | srv->core.stun_sess = NULL; |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 309 | /* Destroy hash tables (well, sort of) */ |
| 310 | if (srv->tables.alloc) { |
| 311 | srv->tables.alloc = NULL; |
| 312 | srv->tables.res = NULL; |
| 313 | } |
| 314 | |
| 315 | /* Destroy timer heap */ |
| 316 | if (srv->core.timer_heap) { |
| 317 | pj_timer_heap_destroy(srv->core.timer_heap); |
| 318 | srv->core.timer_heap = NULL; |
| 319 | } |
| 320 | |
| 321 | /* Destroy ioqueue */ |
| 322 | if (srv->core.ioqueue) { |
| 323 | pj_ioqueue_destroy(srv->core.ioqueue); |
| 324 | srv->core.ioqueue = NULL; |
| 325 | } |
| 326 | |
| 327 | /* Destroy thread local IDs */ |
| 328 | if (srv->core.tls_key != -1) { |
| 329 | pj_thread_local_free(srv->core.tls_key); |
| 330 | srv->core.tls_key = -1; |
| 331 | } |
| 332 | if (srv->core.tls_data != -1) { |
| 333 | pj_thread_local_free(srv->core.tls_data); |
| 334 | srv->core.tls_data = -1; |
| 335 | } |
| 336 | |
| 337 | /* Destroy server lock */ |
| 338 | if (srv->core.lock) { |
| 339 | pj_lock_destroy(srv->core.lock); |
| 340 | srv->core.lock = NULL; |
| 341 | } |
| 342 | |
| 343 | /* Release pool */ |
| 344 | if (srv->core.pool) { |
| 345 | pj_pool_t *pool = srv->core.pool; |
| 346 | srv->core.pool = NULL; |
| 347 | pj_pool_release(pool); |
| 348 | } |
| 349 | |
| 350 | /* Done */ |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 351 | return PJ_SUCCESS; |
| 352 | } |
| 353 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 354 | |
| 355 | /* |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 356 | * Add listener. |
| 357 | */ |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 358 | PJ_DEF(pj_status_t) pj_turn_srv_add_listener(pj_turn_srv *srv, |
| 359 | pj_turn_listener *lis) |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 360 | { |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 361 | unsigned index; |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 362 | |
| 363 | PJ_ASSERT_RETURN(srv && lis, PJ_EINVAL); |
| 364 | PJ_ASSERT_RETURN(srv->core.lis_cnt < MAX_LISTENERS, PJ_ETOOMANY); |
| 365 | |
| 366 | /* Add to array */ |
| 367 | index = srv->core.lis_cnt; |
| 368 | srv->core.listener[index] = lis; |
| 369 | lis->server = srv; |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 370 | lis->id = index; |
| 371 | srv->core.lis_cnt++; |
| 372 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 373 | PJ_LOG(4,(srv->obj_name, "Listener %s/%s added at index %d", |
| 374 | lis->obj_name, lis->info, lis->id)); |
| 375 | |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 376 | return PJ_SUCCESS; |
| 377 | } |
| 378 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 379 | |
| 380 | /* |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 381 | * Destroy listener. |
| 382 | */ |
| 383 | PJ_DEF(pj_status_t) pj_turn_listener_destroy(pj_turn_listener *listener) |
| 384 | { |
| 385 | pj_turn_srv *srv = listener->server; |
| 386 | unsigned i; |
| 387 | |
| 388 | /* Remove from our listener list */ |
| 389 | pj_lock_acquire(srv->core.lock); |
| 390 | for (i=0; i<srv->core.lis_cnt; ++i) { |
| 391 | if (srv->core.listener[i] == listener) { |
| 392 | srv->core.listener[i] = NULL; |
| 393 | srv->core.lis_cnt--; |
| 394 | listener->id = PJ_TURN_INVALID_LIS_ID; |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 395 | break; |
| 396 | } |
| 397 | } |
| 398 | pj_lock_release(srv->core.lock); |
| 399 | |
| 400 | /* Destroy */ |
| 401 | return listener->destroy(listener); |
| 402 | } |
| 403 | |
| 404 | |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 405 | /** |
| 406 | * Add a reference to a transport. |
| 407 | */ |
| 408 | PJ_DEF(void) pj_turn_transport_add_ref( pj_turn_transport *transport, |
| 409 | pj_turn_allocation *alloc) |
| 410 | { |
| 411 | transport->add_ref(transport, alloc); |
| 412 | } |
| 413 | |
| 414 | |
| 415 | /** |
| 416 | * Decrement transport reference counter. |
| 417 | */ |
| 418 | PJ_DEF(void) pj_turn_transport_dec_ref( pj_turn_transport *transport, |
| 419 | pj_turn_allocation *alloc) |
| 420 | { |
| 421 | transport->dec_ref(transport, alloc); |
| 422 | } |
| 423 | |
| 424 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 425 | /* |
| 426 | * Register an allocation to the hash tables. |
| 427 | */ |
| 428 | PJ_DEF(pj_status_t) pj_turn_srv_register_allocation(pj_turn_srv *srv, |
| 429 | pj_turn_allocation *alloc) |
Benny Prijono | b05aafc | 2008-03-08 00:54:04 +0000 | [diff] [blame] | 430 | { |
| 431 | /* Add to hash tables */ |
| 432 | pj_lock_acquire(srv->core.lock); |
| 433 | pj_hash_set(alloc->pool, srv->tables.alloc, |
| 434 | &alloc->hkey, sizeof(alloc->hkey), 0, alloc); |
| 435 | pj_hash_set(alloc->pool, srv->tables.res, |
| 436 | &alloc->relay.hkey, sizeof(alloc->relay.hkey), 0, |
| 437 | &alloc->relay); |
| 438 | pj_lock_release(srv->core.lock); |
| 439 | |
| 440 | return PJ_SUCCESS; |
| 441 | } |
| 442 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 443 | |
| 444 | /* |
| 445 | * Unregister an allocation from the hash tables. |
Benny Prijono | b05aafc | 2008-03-08 00:54:04 +0000 | [diff] [blame] | 446 | */ |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 447 | PJ_DEF(pj_status_t) pj_turn_srv_unregister_allocation(pj_turn_srv *srv, |
| 448 | pj_turn_allocation *alloc) |
Benny Prijono | b05aafc | 2008-03-08 00:54:04 +0000 | [diff] [blame] | 449 | { |
| 450 | /* Unregister from hash tables */ |
| 451 | pj_lock_acquire(srv->core.lock); |
| 452 | pj_hash_set(alloc->pool, srv->tables.alloc, |
| 453 | &alloc->hkey, sizeof(alloc->hkey), 0, NULL); |
| 454 | pj_hash_set(alloc->pool, srv->tables.res, |
| 455 | &alloc->relay.hkey, sizeof(alloc->relay.hkey), 0, NULL); |
| 456 | pj_lock_release(srv->core.lock); |
| 457 | |
| 458 | return PJ_SUCCESS; |
| 459 | } |
| 460 | |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 461 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 462 | /* Callback from our own STUN session whenever it needs to send |
| 463 | * outgoing STUN packet. |
| 464 | */ |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 465 | static pj_status_t on_tx_stun_msg( pj_stun_session *sess, |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 466 | void *token, |
| 467 | const void *pdu, |
| 468 | pj_size_t pdu_size, |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 469 | const pj_sockaddr_t *dst_addr, |
| 470 | unsigned addr_len) |
| 471 | { |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 472 | pj_turn_transport *transport = (pj_turn_transport*) token; |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 473 | |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 474 | PJ_ASSERT_RETURN(transport!=NULL, PJ_EINVALIDOP); |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 475 | |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 476 | PJ_UNUSED_ARG(sess); |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 477 | |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 478 | return transport->sendto(transport, pdu, pdu_size, 0, |
| 479 | dst_addr, addr_len); |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 482 | |
| 483 | /* Respond to STUN request */ |
Benny Prijono | a5d214f | 2008-03-19 23:00:30 +0000 | [diff] [blame] | 484 | static pj_status_t stun_respond(pj_stun_session *sess, |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 485 | pj_turn_transport *transport, |
Benny Prijono | a5d214f | 2008-03-19 23:00:30 +0000 | [diff] [blame] | 486 | const pj_stun_rx_data *rdata, |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 487 | unsigned code, |
| 488 | const char *errmsg, |
| 489 | pj_bool_t cache, |
| 490 | const pj_sockaddr_t *dst_addr, |
| 491 | unsigned addr_len) |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 492 | { |
| 493 | pj_status_t status; |
| 494 | pj_str_t reason; |
| 495 | pj_stun_tx_data *tdata; |
| 496 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 497 | /* Create response */ |
Benny Prijono | a5d214f | 2008-03-19 23:00:30 +0000 | [diff] [blame] | 498 | status = pj_stun_session_create_res(sess, rdata, code, |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 499 | (errmsg?pj_cstr(&reason,errmsg):NULL), |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 500 | &tdata); |
| 501 | if (status != PJ_SUCCESS) |
Benny Prijono | b05aafc | 2008-03-08 00:54:04 +0000 | [diff] [blame] | 502 | return status; |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 503 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 504 | /* Send the response */ |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 505 | return pj_stun_session_send_msg(sess, transport, cache, PJ_FALSE, |
| 506 | dst_addr, addr_len, tdata); |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 509 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 510 | /* Callback from our own STUN session when incoming request arrives. |
| 511 | * This function is triggered by pj_stun_session_on_rx_pkt() call in |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 512 | * pj_turn_srv_on_rx_pkt() function below. |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 513 | */ |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 514 | static pj_status_t on_rx_stun_request(pj_stun_session *sess, |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 515 | const pj_uint8_t *pdu, |
| 516 | unsigned pdu_len, |
Benny Prijono | a5d214f | 2008-03-19 23:00:30 +0000 | [diff] [blame] | 517 | const pj_stun_rx_data *rdata, |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 518 | void *token, |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 519 | const pj_sockaddr_t *src_addr, |
| 520 | unsigned src_addr_len) |
| 521 | { |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 522 | pj_turn_transport *transport; |
Benny Prijono | a5d214f | 2008-03-19 23:00:30 +0000 | [diff] [blame] | 523 | const pj_stun_msg *msg = rdata->msg; |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 524 | pj_turn_srv *srv; |
| 525 | pj_turn_allocation *alloc; |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 526 | pj_status_t status; |
| 527 | |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 528 | PJ_UNUSED_ARG(pdu); |
| 529 | PJ_UNUSED_ARG(pdu_len); |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 530 | |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 531 | transport = (pj_turn_transport*) token; |
| 532 | srv = transport->listener->server; |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 533 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 534 | /* Respond any requests other than ALLOCATE with 437 response */ |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 535 | if (msg->hdr.type != PJ_STUN_ALLOCATE_REQUEST) { |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 536 | stun_respond(sess, transport, rdata, PJ_STUN_SC_ALLOCATION_MISMATCH, |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 537 | NULL, PJ_FALSE, src_addr, src_addr_len); |
| 538 | return PJ_SUCCESS; |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Benny Prijono | b05aafc | 2008-03-08 00:54:04 +0000 | [diff] [blame] | 541 | /* Create new allocation. The relay resource will be allocated |
| 542 | * in this function. |
| 543 | */ |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 544 | status = pj_turn_allocation_create(transport, src_addr, src_addr_len, |
Benny Prijono | a5d214f | 2008-03-19 23:00:30 +0000 | [diff] [blame] | 545 | rdata, sess, &alloc); |
Benny Prijono | b05aafc | 2008-03-08 00:54:04 +0000 | [diff] [blame] | 546 | if (status != PJ_SUCCESS) { |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 547 | /* STUN response has been sent, no need to reply here */ |
| 548 | return PJ_SUCCESS; |
Benny Prijono | b05aafc | 2008-03-08 00:54:04 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Benny Prijono | b05aafc | 2008-03-08 00:54:04 +0000 | [diff] [blame] | 551 | /* Done. */ |
| 552 | return PJ_SUCCESS; |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Benny Prijono | efa014b | 2008-08-22 17:46:33 +0000 | [diff] [blame] | 555 | /* Handle STUN Binding request */ |
| 556 | static void handle_binding_request(pj_turn_pkt *pkt, |
| 557 | unsigned options) |
| 558 | { |
| 559 | pj_stun_msg *request, *response; |
| 560 | pj_uint8_t pdu[200]; |
| 561 | pj_size_t len; |
| 562 | pj_status_t status; |
| 563 | |
| 564 | /* Decode request */ |
| 565 | status = pj_stun_msg_decode(pkt->pool, pkt->pkt, pkt->len, options, |
| 566 | &request, NULL, NULL); |
| 567 | if (status != PJ_SUCCESS) |
| 568 | return; |
| 569 | |
| 570 | /* Create response */ |
| 571 | status = pj_stun_msg_create_response(pkt->pool, request, 0, NULL, |
| 572 | &response); |
| 573 | if (status != PJ_SUCCESS) |
| 574 | return; |
| 575 | |
| 576 | /* Add XOR-MAPPED-ADDRESS */ |
| 577 | pj_stun_msg_add_sockaddr_attr(pkt->pool, response, |
| 578 | PJ_STUN_ATTR_XOR_MAPPED_ADDR, |
| 579 | PJ_TRUE, |
| 580 | &pkt->src.clt_addr, |
| 581 | pkt->src_addr_len); |
| 582 | |
| 583 | /* Encode */ |
| 584 | status = pj_stun_msg_encode(response, pdu, sizeof(pdu), 0, NULL, &len); |
| 585 | if (status != PJ_SUCCESS) |
| 586 | return; |
| 587 | |
| 588 | /* Send response */ |
| 589 | pkt->transport->sendto(pkt->transport, pdu, len, 0, |
| 590 | &pkt->src.clt_addr, pkt->src_addr_len); |
| 591 | } |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 592 | |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 593 | /* |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 594 | * This callback is called by UDP listener on incoming packet. This is |
| 595 | * the first entry for incoming packet (from client) to the server. From |
| 596 | * here, the packet may be handed over to an allocation if an allocation |
| 597 | * is found for the client address, or handed over to owned STUN session |
| 598 | * if an allocation is not found. |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 599 | */ |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 600 | PJ_DEF(void) pj_turn_srv_on_rx_pkt(pj_turn_srv *srv, |
| 601 | pj_turn_pkt *pkt) |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 602 | { |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 603 | pj_turn_allocation *alloc; |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 604 | |
| 605 | /* Get TURN allocation from the source address */ |
| 606 | pj_lock_acquire(srv->core.lock); |
Benny Prijono | 24a2185 | 2008-04-14 04:04:30 +0000 | [diff] [blame] | 607 | alloc = (pj_turn_allocation*) |
| 608 | pj_hash_get(srv->tables.alloc, &pkt->src, sizeof(pkt->src), NULL); |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 609 | pj_lock_release(srv->core.lock); |
| 610 | |
| 611 | /* If allocation is found, just hand over the packet to the |
| 612 | * allocation. |
| 613 | */ |
| 614 | if (alloc) { |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 615 | pj_turn_allocation_on_rx_client_pkt(alloc, pkt); |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 616 | } else { |
| 617 | /* Otherwise this is a new client */ |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 618 | unsigned options; |
Nanang Izzuddin | 838cb32 | 2008-12-18 17:52:57 +0000 | [diff] [blame] | 619 | pj_size_t parsed_len; |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 620 | pj_status_t status; |
| 621 | |
| 622 | /* Check that this is a STUN message */ |
Benny Prijono | e722685 | 2008-04-13 21:48:44 +0000 | [diff] [blame] | 623 | options = PJ_STUN_CHECK_PACKET | PJ_STUN_NO_FINGERPRINT_CHECK; |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 624 | if (pkt->transport->listener->tp_type == PJ_TURN_TP_UDP) |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 625 | options |= PJ_STUN_IS_DATAGRAM; |
| 626 | |
| 627 | status = pj_stun_msg_check(pkt->pkt, pkt->len, options); |
| 628 | if (status != PJ_SUCCESS) { |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 629 | /* If the first byte are not STUN, drop the packet. First byte |
| 630 | * of STUN message is always 0x00 or 0x01. Otherwise wait for |
| 631 | * more data as the data might have come from TCP. |
| 632 | * |
| 633 | * Also drop packet if it's unreasonably too big, as this might |
| 634 | * indicate invalid data that's building up in the buffer. |
| 635 | * |
| 636 | * Or if packet is a datagram. |
| 637 | */ |
| 638 | if ((*pkt->pkt != 0x00 && *pkt->pkt != 0x01) || |
| 639 | pkt->len > 1600 || |
| 640 | (options & PJ_STUN_IS_DATAGRAM)) |
| 641 | { |
| 642 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 643 | char ip[PJ_INET6_ADDRSTRLEN+10]; |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 644 | |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 645 | pkt->len = 0; |
| 646 | |
| 647 | pj_strerror(status, errmsg, sizeof(errmsg)); |
| 648 | PJ_LOG(5,(srv->obj_name, |
| 649 | "Non-STUN packet from %s is dropped: %s", |
| 650 | pj_sockaddr_print(&pkt->src.clt_addr, ip, sizeof(ip), 3), |
| 651 | errmsg)); |
| 652 | } |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 653 | return; |
| 654 | } |
| 655 | |
Benny Prijono | efa014b | 2008-08-22 17:46:33 +0000 | [diff] [blame] | 656 | /* Special handling for Binding Request. We won't give it to the |
| 657 | * STUN session since this request is not authenticated. |
| 658 | */ |
| 659 | if (pkt->pkt[1] == 1) { |
| 660 | handle_binding_request(pkt, options); |
| 661 | return; |
| 662 | } |
| 663 | |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 664 | /* Hand over processing to STUN session. This will trigger |
| 665 | * on_rx_stun_request() callback to be called if the STUN |
| 666 | * message is a request. |
| 667 | */ |
| 668 | options &= ~PJ_STUN_CHECK_PACKET; |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 669 | parsed_len = 0; |
| 670 | status = pj_stun_session_on_rx_pkt(srv->core.stun_sess, pkt->pkt, |
| 671 | pkt->len, options, pkt->transport, |
| 672 | &parsed_len, &pkt->src.clt_addr, |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 673 | pkt->src_addr_len); |
| 674 | if (status != PJ_SUCCESS) { |
| 675 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 676 | char ip[PJ_INET6_ADDRSTRLEN+10]; |
| 677 | |
| 678 | pj_strerror(status, errmsg, sizeof(errmsg)); |
| 679 | PJ_LOG(5,(srv->obj_name, |
| 680 | "Error processing STUN packet from %s: %s", |
| 681 | pj_sockaddr_print(&pkt->src.clt_addr, ip, sizeof(ip), 3), |
| 682 | errmsg)); |
Benny Prijono | 879ad1a | 2008-04-09 09:38:12 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | if (pkt->transport->listener->tp_type == PJ_TURN_TP_UDP) { |
| 686 | pkt->len = 0; |
| 687 | } else if (parsed_len > 0) { |
| 688 | if (parsed_len == pkt->len) { |
| 689 | pkt->len = 0; |
| 690 | } else { |
| 691 | pj_memmove(pkt->pkt, pkt->pkt+parsed_len, |
| 692 | pkt->len - parsed_len); |
| 693 | pkt->len -= parsed_len; |
| 694 | } |
Benny Prijono | 708725a | 2008-03-09 12:55:00 +0000 | [diff] [blame] | 695 | } |
Benny Prijono | d1e862f | 2008-02-21 15:54:27 +0000 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
| 699 | |