Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 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 <pjnath/stun_sock.h> |
| 21 | #include <pjnath/errno.h> |
| 22 | #include <pjnath/stun_transaction.h> |
| 23 | #include <pjnath/stun_session.h> |
| 24 | #include <pjlib-util/srv_resolver.h> |
| 25 | #include <pj/activesock.h> |
| 26 | #include <pj/addr_resolv.h> |
| 27 | #include <pj/array.h> |
| 28 | #include <pj/assert.h> |
| 29 | #include <pj/ip_helper.h> |
| 30 | #include <pj/log.h> |
| 31 | #include <pj/os.h> |
| 32 | #include <pj/pool.h> |
| 33 | #include <pj/rand.h> |
| 34 | |
| 35 | #if 1 |
| 36 | # define TRACE_(x) PJ_LOG(5,x) |
| 37 | #else |
| 38 | # define TRACE_(x) |
| 39 | #endif |
| 40 | |
| 41 | enum { MAX_BIND_RETRY = 100 }; |
| 42 | |
| 43 | struct pj_stun_sock |
| 44 | { |
| 45 | char *obj_name; /* Log identification */ |
| 46 | pj_pool_t *pool; /* Pool */ |
| 47 | void *user_data; /* Application user data */ |
| 48 | pj_bool_t is_destroying; /* Destroy already called */ |
| 49 | int af; /* Address family */ |
| 50 | pj_stun_config stun_cfg; /* STUN config (ioqueue etc)*/ |
| 51 | pj_stun_sock_cb cb; /* Application callbacks */ |
| 52 | |
| 53 | int ka_interval; /* Keep alive interval */ |
| 54 | pj_timer_entry ka_timer; /* Keep alive timer. */ |
| 55 | |
| 56 | pj_sockaddr srv_addr; /* Resolved server addr */ |
| 57 | pj_sockaddr mapped_addr; /* Our public address */ |
| 58 | |
| 59 | pj_dns_srv_async_query *q; /* Pending DNS query */ |
| 60 | pj_sock_t sock_fd; /* Socket descriptor */ |
| 61 | pj_activesock_t *active_sock; /* Active socket object */ |
| 62 | pj_ioqueue_op_key_t send_key; /* Default send key for app */ |
| 63 | pj_ioqueue_op_key_t int_send_key; /* Send key for internal */ |
| 64 | |
| 65 | pj_uint16_t tsx_id[6]; /* .. to match STUN msg */ |
| 66 | pj_stun_session *stun_sess; /* STUN session */ |
| 67 | pj_grp_lock_t *grp_lock; /* Session group lock */ |
| 68 | }; |
| 69 | |
| 70 | /* |
| 71 | * Prototypes for static functions |
| 72 | */ |
| 73 | |
| 74 | /* Destructor for group lock */ |
| 75 | static void stun_sock_destructor(void *obj); |
| 76 | |
| 77 | /* This callback is called by the STUN session to send packet */ |
| 78 | static pj_status_t sess_on_send_msg(pj_stun_session *sess, |
| 79 | void *token, |
| 80 | const void *pkt, |
| 81 | pj_size_t pkt_size, |
| 82 | const pj_sockaddr_t *dst_addr, |
| 83 | unsigned addr_len); |
| 84 | |
| 85 | /* This callback is called by the STUN session when outgoing transaction |
| 86 | * is complete |
| 87 | */ |
| 88 | static void sess_on_request_complete(pj_stun_session *sess, |
| 89 | pj_status_t status, |
| 90 | void *token, |
| 91 | pj_stun_tx_data *tdata, |
| 92 | const pj_stun_msg *response, |
| 93 | const pj_sockaddr_t *src_addr, |
| 94 | unsigned src_addr_len); |
| 95 | /* DNS resolver callback */ |
| 96 | static void dns_srv_resolver_cb(void *user_data, |
| 97 | pj_status_t status, |
| 98 | const pj_dns_srv_record *rec); |
| 99 | |
| 100 | /* Start sending STUN Binding request */ |
| 101 | static pj_status_t get_mapped_addr(pj_stun_sock *stun_sock); |
| 102 | |
| 103 | /* Callback from active socket when incoming packet is received */ |
| 104 | static pj_bool_t on_data_recvfrom(pj_activesock_t *asock, |
| 105 | void *data, |
| 106 | pj_size_t size, |
| 107 | const pj_sockaddr_t *src_addr, |
| 108 | int addr_len, |
| 109 | pj_status_t status); |
| 110 | |
| 111 | /* Callback from active socket about send status */ |
| 112 | static pj_bool_t on_data_sent(pj_activesock_t *asock, |
| 113 | pj_ioqueue_op_key_t *send_key, |
| 114 | pj_ssize_t sent); |
| 115 | |
| 116 | /* Schedule keep-alive timer */ |
| 117 | static void start_ka_timer(pj_stun_sock *stun_sock); |
| 118 | |
| 119 | /* Keep-alive timer callback */ |
| 120 | static void ka_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te); |
| 121 | |
| 122 | #define INTERNAL_MSG_TOKEN (void*)(pj_ssize_t)1 |
| 123 | |
| 124 | |
| 125 | /* |
| 126 | * Retrieve the name representing the specified operation. |
| 127 | */ |
| 128 | PJ_DEF(const char*) pj_stun_sock_op_name(pj_stun_sock_op op) |
| 129 | { |
| 130 | const char *names[] = { |
| 131 | "?", |
| 132 | "DNS resolution", |
| 133 | "STUN Binding request", |
| 134 | "Keep-alive", |
| 135 | "Mapped addr. changed" |
| 136 | }; |
| 137 | |
| 138 | return op < PJ_ARRAY_SIZE(names) ? names[op] : "???"; |
| 139 | }; |
| 140 | |
| 141 | |
| 142 | /* |
| 143 | * Initialize the STUN transport setting with its default values. |
| 144 | */ |
| 145 | PJ_DEF(void) pj_stun_sock_cfg_default(pj_stun_sock_cfg *cfg) |
| 146 | { |
| 147 | pj_bzero(cfg, sizeof(*cfg)); |
| 148 | cfg->max_pkt_size = PJ_STUN_SOCK_PKT_LEN; |
| 149 | cfg->async_cnt = 1; |
| 150 | cfg->ka_interval = PJ_STUN_KEEP_ALIVE_SEC; |
| 151 | cfg->qos_type = PJ_QOS_TYPE_BEST_EFFORT; |
| 152 | cfg->qos_ignore_error = PJ_TRUE; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /* Check that configuration setting is valid */ |
| 157 | static pj_bool_t pj_stun_sock_cfg_is_valid(const pj_stun_sock_cfg *cfg) |
| 158 | { |
| 159 | return cfg->max_pkt_size > 1 && cfg->async_cnt >= 1; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Create the STUN transport using the specified configuration. |
| 164 | */ |
| 165 | PJ_DEF(pj_status_t) pj_stun_sock_create( pj_stun_config *stun_cfg, |
| 166 | const char *name, |
| 167 | int af, |
| 168 | const pj_stun_sock_cb *cb, |
| 169 | const pj_stun_sock_cfg *cfg, |
| 170 | void *user_data, |
| 171 | pj_stun_sock **p_stun_sock) |
| 172 | { |
| 173 | pj_pool_t *pool; |
| 174 | pj_stun_sock *stun_sock; |
| 175 | pj_stun_sock_cfg default_cfg; |
| 176 | pj_sockaddr bound_addr; |
| 177 | unsigned i; |
| 178 | pj_uint16_t max_bind_retry; |
| 179 | pj_status_t status; |
| 180 | |
| 181 | PJ_ASSERT_RETURN(stun_cfg && cb && p_stun_sock, PJ_EINVAL); |
| 182 | PJ_ASSERT_RETURN(af==pj_AF_INET()||af==pj_AF_INET6(), PJ_EAFNOTSUP); |
| 183 | PJ_ASSERT_RETURN(!cfg || pj_stun_sock_cfg_is_valid(cfg), PJ_EINVAL); |
| 184 | PJ_ASSERT_RETURN(cb->on_status, PJ_EINVAL); |
| 185 | |
| 186 | status = pj_stun_config_check_valid(stun_cfg); |
| 187 | if (status != PJ_SUCCESS) |
| 188 | return status; |
| 189 | |
| 190 | if (name == NULL) |
| 191 | name = "stuntp%p"; |
| 192 | |
| 193 | if (cfg == NULL) { |
| 194 | pj_stun_sock_cfg_default(&default_cfg); |
| 195 | cfg = &default_cfg; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /* Create structure */ |
| 200 | pool = pj_pool_create(stun_cfg->pf, name, 256, 512, NULL); |
| 201 | stun_sock = PJ_POOL_ZALLOC_T(pool, pj_stun_sock); |
| 202 | stun_sock->pool = pool; |
| 203 | stun_sock->obj_name = pool->obj_name; |
| 204 | stun_sock->user_data = user_data; |
| 205 | stun_sock->af = af; |
| 206 | stun_sock->sock_fd = PJ_INVALID_SOCKET; |
| 207 | pj_memcpy(&stun_sock->stun_cfg, stun_cfg, sizeof(*stun_cfg)); |
| 208 | pj_memcpy(&stun_sock->cb, cb, sizeof(*cb)); |
| 209 | |
| 210 | stun_sock->ka_interval = cfg->ka_interval; |
| 211 | if (stun_sock->ka_interval == 0) |
| 212 | stun_sock->ka_interval = PJ_STUN_KEEP_ALIVE_SEC; |
| 213 | |
| 214 | if (cfg && cfg->grp_lock) { |
| 215 | stun_sock->grp_lock = cfg->grp_lock; |
| 216 | } else { |
| 217 | status = pj_grp_lock_create(pool, NULL, &stun_sock->grp_lock); |
| 218 | if (status != PJ_SUCCESS) { |
| 219 | pj_pool_release(pool); |
| 220 | return status; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | pj_grp_lock_add_ref(stun_sock->grp_lock); |
| 225 | pj_grp_lock_add_handler(stun_sock->grp_lock, pool, stun_sock, |
| 226 | &stun_sock_destructor); |
| 227 | |
| 228 | /* Create socket and bind socket */ |
| 229 | status = pj_sock_socket(af, pj_SOCK_DGRAM(), 0, &stun_sock->sock_fd); |
| 230 | if (status != PJ_SUCCESS) |
| 231 | goto on_error; |
| 232 | |
| 233 | /* Apply QoS, if specified */ |
| 234 | status = pj_sock_apply_qos2(stun_sock->sock_fd, cfg->qos_type, |
| 235 | &cfg->qos_params, 2, stun_sock->obj_name, |
| 236 | NULL); |
| 237 | if (status != PJ_SUCCESS && !cfg->qos_ignore_error) |
| 238 | goto on_error; |
| 239 | |
| 240 | /* Apply socket buffer size */ |
| 241 | if (cfg->so_rcvbuf_size > 0) { |
| 242 | unsigned sobuf_size = cfg->so_rcvbuf_size; |
| 243 | status = pj_sock_setsockopt_sobuf(stun_sock->sock_fd, pj_SO_RCVBUF(), |
| 244 | PJ_TRUE, &sobuf_size); |
| 245 | if (status != PJ_SUCCESS) { |
| 246 | pj_perror(3, stun_sock->obj_name, status, |
| 247 | "Failed setting SO_RCVBUF"); |
| 248 | } else { |
| 249 | if (sobuf_size < cfg->so_rcvbuf_size) { |
| 250 | PJ_LOG(4, (stun_sock->obj_name, |
| 251 | "Warning! Cannot set SO_RCVBUF as configured, " |
| 252 | "now=%d, configured=%d", |
| 253 | sobuf_size, cfg->so_rcvbuf_size)); |
| 254 | } else { |
| 255 | PJ_LOG(5, (stun_sock->obj_name, "SO_RCVBUF set to %d", |
| 256 | sobuf_size)); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | if (cfg->so_sndbuf_size > 0) { |
| 261 | unsigned sobuf_size = cfg->so_sndbuf_size; |
| 262 | status = pj_sock_setsockopt_sobuf(stun_sock->sock_fd, pj_SO_SNDBUF(), |
| 263 | PJ_TRUE, &sobuf_size); |
| 264 | if (status != PJ_SUCCESS) { |
| 265 | pj_perror(3, stun_sock->obj_name, status, |
| 266 | "Failed setting SO_SNDBUF"); |
| 267 | } else { |
| 268 | if (sobuf_size < cfg->so_sndbuf_size) { |
| 269 | PJ_LOG(4, (stun_sock->obj_name, |
| 270 | "Warning! Cannot set SO_SNDBUF as configured, " |
| 271 | "now=%d, configured=%d", |
| 272 | sobuf_size, cfg->so_sndbuf_size)); |
| 273 | } else { |
| 274 | PJ_LOG(5, (stun_sock->obj_name, "SO_SNDBUF set to %d", |
| 275 | sobuf_size)); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /* Bind socket */ |
| 281 | max_bind_retry = MAX_BIND_RETRY; |
| 282 | if (cfg->port_range && cfg->port_range < max_bind_retry) |
| 283 | max_bind_retry = cfg->port_range; |
| 284 | pj_sockaddr_init(af, &bound_addr, NULL, 0); |
| 285 | if (cfg->bound_addr.addr.sa_family == pj_AF_INET() || |
| 286 | cfg->bound_addr.addr.sa_family == pj_AF_INET6()) |
| 287 | { |
| 288 | pj_sockaddr_cp(&bound_addr, &cfg->bound_addr); |
| 289 | } |
| 290 | status = pj_sock_bind_random(stun_sock->sock_fd, &bound_addr, |
| 291 | cfg->port_range, max_bind_retry); |
| 292 | if (status != PJ_SUCCESS) |
| 293 | goto on_error; |
| 294 | |
| 295 | /* Create more useful information string about this transport */ |
| 296 | #if 0 |
| 297 | { |
| 298 | pj_sockaddr bound_addr; |
| 299 | int addr_len = sizeof(bound_addr); |
| 300 | |
| 301 | status = pj_sock_getsockname(stun_sock->sock_fd, &bound_addr, |
| 302 | &addr_len); |
| 303 | if (status != PJ_SUCCESS) |
| 304 | goto on_error; |
| 305 | |
| 306 | stun_sock->info = pj_pool_alloc(pool, PJ_INET6_ADDRSTRLEN+10); |
| 307 | pj_sockaddr_print(&bound_addr, stun_sock->info, |
| 308 | PJ_INET6_ADDRSTRLEN, 3); |
| 309 | } |
| 310 | #endif |
| 311 | |
| 312 | /* Init active socket configuration */ |
| 313 | { |
| 314 | pj_activesock_cfg activesock_cfg; |
| 315 | pj_activesock_cb activesock_cb; |
| 316 | |
| 317 | pj_activesock_cfg_default(&activesock_cfg); |
| 318 | activesock_cfg.grp_lock = stun_sock->grp_lock; |
| 319 | activesock_cfg.async_cnt = cfg->async_cnt; |
| 320 | activesock_cfg.concurrency = 0; |
| 321 | |
| 322 | /* Create the active socket */ |
| 323 | pj_bzero(&activesock_cb, sizeof(activesock_cb)); |
| 324 | activesock_cb.on_data_recvfrom = &on_data_recvfrom; |
| 325 | activesock_cb.on_data_sent = &on_data_sent; |
| 326 | status = pj_activesock_create(pool, stun_sock->sock_fd, |
| 327 | pj_SOCK_DGRAM(), |
| 328 | &activesock_cfg, stun_cfg->ioqueue, |
| 329 | &activesock_cb, stun_sock, |
| 330 | &stun_sock->active_sock); |
| 331 | if (status != PJ_SUCCESS) |
| 332 | goto on_error; |
| 333 | |
| 334 | /* Start asynchronous read operations */ |
| 335 | status = pj_activesock_start_recvfrom(stun_sock->active_sock, pool, |
| 336 | cfg->max_pkt_size, 0); |
| 337 | if (status != PJ_SUCCESS) |
| 338 | goto on_error; |
| 339 | |
| 340 | /* Init send keys */ |
| 341 | pj_ioqueue_op_key_init(&stun_sock->send_key, |
| 342 | sizeof(stun_sock->send_key)); |
| 343 | pj_ioqueue_op_key_init(&stun_sock->int_send_key, |
| 344 | sizeof(stun_sock->int_send_key)); |
| 345 | } |
| 346 | |
| 347 | /* Create STUN session */ |
| 348 | { |
| 349 | pj_stun_session_cb sess_cb; |
| 350 | |
| 351 | pj_bzero(&sess_cb, sizeof(sess_cb)); |
| 352 | sess_cb.on_request_complete = &sess_on_request_complete; |
| 353 | sess_cb.on_send_msg = &sess_on_send_msg; |
| 354 | status = pj_stun_session_create(&stun_sock->stun_cfg, |
| 355 | stun_sock->obj_name, |
| 356 | &sess_cb, PJ_FALSE, |
| 357 | stun_sock->grp_lock, |
| 358 | &stun_sock->stun_sess); |
| 359 | if (status != PJ_SUCCESS) |
| 360 | goto on_error; |
| 361 | } |
| 362 | |
| 363 | /* Associate us with the STUN session */ |
| 364 | pj_stun_session_set_user_data(stun_sock->stun_sess, stun_sock); |
| 365 | |
| 366 | /* Initialize random numbers to be used as STUN transaction ID for |
| 367 | * outgoing Binding request. We use the 80bit number to distinguish |
| 368 | * STUN messages we sent with STUN messages that the application sends. |
| 369 | * The last 16bit value in the array is a counter. |
| 370 | */ |
| 371 | for (i=0; i<PJ_ARRAY_SIZE(stun_sock->tsx_id); ++i) { |
| 372 | stun_sock->tsx_id[i] = (pj_uint16_t) pj_rand(); |
| 373 | } |
| 374 | stun_sock->tsx_id[5] = 0; |
| 375 | |
| 376 | |
| 377 | /* Init timer entry */ |
| 378 | stun_sock->ka_timer.cb = &ka_timer_cb; |
| 379 | stun_sock->ka_timer.user_data = stun_sock; |
| 380 | |
| 381 | /* Done */ |
| 382 | *p_stun_sock = stun_sock; |
| 383 | return PJ_SUCCESS; |
| 384 | |
| 385 | on_error: |
| 386 | pj_stun_sock_destroy(stun_sock); |
| 387 | return status; |
| 388 | } |
| 389 | |
| 390 | /* Start socket. */ |
| 391 | PJ_DEF(pj_status_t) pj_stun_sock_start( pj_stun_sock *stun_sock, |
| 392 | const pj_str_t *domain, |
| 393 | pj_uint16_t default_port, |
| 394 | pj_dns_resolver *resolver) |
| 395 | { |
| 396 | pj_status_t status; |
| 397 | |
| 398 | PJ_ASSERT_RETURN(stun_sock && domain && default_port, PJ_EINVAL); |
| 399 | |
| 400 | pj_grp_lock_acquire(stun_sock->grp_lock); |
| 401 | |
| 402 | /* Check whether the domain contains IP address */ |
| 403 | stun_sock->srv_addr.addr.sa_family = (pj_uint16_t)stun_sock->af; |
| 404 | status = pj_inet_pton(stun_sock->af, domain, |
| 405 | pj_sockaddr_get_addr(&stun_sock->srv_addr)); |
| 406 | if (status != PJ_SUCCESS) { |
| 407 | stun_sock->srv_addr.addr.sa_family = (pj_uint16_t)0; |
| 408 | } |
| 409 | |
| 410 | /* If resolver is set, try to resolve with DNS SRV first. It |
| 411 | * will fallback to DNS A/AAAA when no SRV record is found. |
| 412 | */ |
| 413 | if (status != PJ_SUCCESS && resolver) { |
| 414 | const pj_str_t res_name = pj_str("_stun._udp."); |
| 415 | unsigned opt; |
| 416 | |
| 417 | pj_assert(stun_sock->q == NULL); |
| 418 | |
| 419 | opt = PJ_DNS_SRV_FALLBACK_A; |
| 420 | if (stun_sock->af == pj_AF_INET6()) { |
| 421 | opt |= (PJ_DNS_SRV_RESOLVE_AAAA | PJ_DNS_SRV_FALLBACK_AAAA); |
| 422 | } |
| 423 | |
| 424 | status = pj_dns_srv_resolve(domain, &res_name, default_port, |
| 425 | stun_sock->pool, resolver, opt, |
| 426 | stun_sock, &dns_srv_resolver_cb, |
| 427 | &stun_sock->q); |
| 428 | |
| 429 | /* Processing will resume when the DNS SRV callback is called */ |
| 430 | |
| 431 | } else { |
| 432 | |
| 433 | if (status != PJ_SUCCESS) { |
| 434 | pj_addrinfo ai; |
| 435 | unsigned cnt = 1; |
| 436 | |
| 437 | status = pj_getaddrinfo(stun_sock->af, domain, &cnt, &ai); |
| 438 | if (status != PJ_SUCCESS) |
| 439 | return status; |
| 440 | |
| 441 | pj_sockaddr_cp(&stun_sock->srv_addr, &ai.ai_addr); |
| 442 | } |
| 443 | |
| 444 | pj_sockaddr_set_port(&stun_sock->srv_addr, (pj_uint16_t)default_port); |
| 445 | |
| 446 | /* Start sending Binding request */ |
| 447 | status = get_mapped_addr(stun_sock); |
| 448 | } |
| 449 | |
| 450 | pj_grp_lock_release(stun_sock->grp_lock); |
| 451 | return status; |
| 452 | } |
| 453 | |
| 454 | /* Destructor */ |
| 455 | static void stun_sock_destructor(void *obj) |
| 456 | { |
| 457 | pj_stun_sock *stun_sock = (pj_stun_sock*)obj; |
| 458 | |
| 459 | if (stun_sock->q) { |
| 460 | pj_dns_srv_cancel_query(stun_sock->q, PJ_FALSE); |
| 461 | stun_sock->q = NULL; |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | if (stun_sock->stun_sess) { |
| 466 | pj_stun_session_destroy(stun_sock->stun_sess); |
| 467 | stun_sock->stun_sess = NULL; |
| 468 | } |
| 469 | */ |
| 470 | |
| 471 | if (stun_sock->pool) { |
| 472 | pj_pool_t *pool = stun_sock->pool; |
| 473 | stun_sock->pool = NULL; |
| 474 | pj_pool_release(pool); |
| 475 | } |
| 476 | |
| 477 | TRACE_(("", "STUN sock %p destroyed", stun_sock)); |
| 478 | |
| 479 | } |
| 480 | |
| 481 | /* Destroy */ |
| 482 | PJ_DEF(pj_status_t) pj_stun_sock_destroy(pj_stun_sock *stun_sock) |
| 483 | { |
| 484 | TRACE_((stun_sock->obj_name, "STUN sock %p request, ref_cnt=%d", |
| 485 | stun_sock, pj_grp_lock_get_ref(stun_sock->grp_lock))); |
| 486 | |
| 487 | pj_grp_lock_acquire(stun_sock->grp_lock); |
| 488 | if (stun_sock->is_destroying) { |
| 489 | /* Destroy already called */ |
| 490 | pj_grp_lock_release(stun_sock->grp_lock); |
| 491 | return PJ_EINVALIDOP; |
| 492 | } |
| 493 | |
| 494 | stun_sock->is_destroying = PJ_TRUE; |
| 495 | pj_timer_heap_cancel_if_active(stun_sock->stun_cfg.timer_heap, |
| 496 | &stun_sock->ka_timer, 0); |
| 497 | |
| 498 | if (stun_sock->active_sock != NULL) { |
| 499 | stun_sock->sock_fd = PJ_INVALID_SOCKET; |
| 500 | pj_activesock_close(stun_sock->active_sock); |
| 501 | } else if (stun_sock->sock_fd != PJ_INVALID_SOCKET) { |
| 502 | pj_sock_close(stun_sock->sock_fd); |
| 503 | stun_sock->sock_fd = PJ_INVALID_SOCKET; |
| 504 | } |
| 505 | |
| 506 | if (stun_sock->stun_sess) { |
| 507 | pj_stun_session_destroy(stun_sock->stun_sess); |
| 508 | } |
| 509 | pj_grp_lock_dec_ref(stun_sock->grp_lock); |
| 510 | pj_grp_lock_release(stun_sock->grp_lock); |
| 511 | return PJ_SUCCESS; |
| 512 | } |
| 513 | |
| 514 | /* Associate user data */ |
| 515 | PJ_DEF(pj_status_t) pj_stun_sock_set_user_data( pj_stun_sock *stun_sock, |
| 516 | void *user_data) |
| 517 | { |
| 518 | PJ_ASSERT_RETURN(stun_sock, PJ_EINVAL); |
| 519 | stun_sock->user_data = user_data; |
| 520 | return PJ_SUCCESS; |
| 521 | } |
| 522 | |
| 523 | |
| 524 | /* Get user data */ |
| 525 | PJ_DEF(void*) pj_stun_sock_get_user_data(pj_stun_sock *stun_sock) |
| 526 | { |
| 527 | PJ_ASSERT_RETURN(stun_sock, NULL); |
| 528 | return stun_sock->user_data; |
| 529 | } |
| 530 | |
| 531 | /* Notify application that session has failed */ |
| 532 | static pj_bool_t sess_fail(pj_stun_sock *stun_sock, |
| 533 | pj_stun_sock_op op, |
| 534 | pj_status_t status) |
| 535 | { |
| 536 | pj_bool_t ret; |
| 537 | |
| 538 | PJ_PERROR(4,(stun_sock->obj_name, status, |
| 539 | "Session failed because %s failed", |
| 540 | pj_stun_sock_op_name(op))); |
| 541 | |
| 542 | ret = (*stun_sock->cb.on_status)(stun_sock, op, status); |
| 543 | |
| 544 | return ret; |
| 545 | } |
| 546 | |
| 547 | /* DNS resolver callback */ |
| 548 | static void dns_srv_resolver_cb(void *user_data, |
| 549 | pj_status_t status, |
| 550 | const pj_dns_srv_record *rec) |
| 551 | { |
| 552 | pj_stun_sock *stun_sock = (pj_stun_sock*) user_data; |
| 553 | |
| 554 | pj_grp_lock_acquire(stun_sock->grp_lock); |
| 555 | |
| 556 | /* Clear query */ |
| 557 | stun_sock->q = NULL; |
| 558 | |
| 559 | /* Handle error */ |
| 560 | if (status != PJ_SUCCESS) { |
| 561 | sess_fail(stun_sock, PJ_STUN_SOCK_DNS_OP, status); |
| 562 | pj_grp_lock_release(stun_sock->grp_lock); |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | pj_assert(rec->count); |
| 567 | pj_assert(rec->entry[0].server.addr_count); |
| 568 | |
| 569 | PJ_TODO(SUPPORT_IPV6_IN_RESOLVER); |
| 570 | pj_assert(stun_sock->af == pj_AF_INET()); |
| 571 | |
| 572 | /* Set the address */ |
| 573 | pj_sockaddr_in_init(&stun_sock->srv_addr.ipv4, NULL, |
| 574 | rec->entry[0].port); |
| 575 | stun_sock->srv_addr.ipv4.sin_addr = rec->entry[0].server.addr[0]; |
| 576 | |
| 577 | /* Start sending Binding request */ |
| 578 | get_mapped_addr(stun_sock); |
| 579 | |
| 580 | pj_grp_lock_release(stun_sock->grp_lock); |
| 581 | } |
| 582 | |
| 583 | |
| 584 | /* Start sending STUN Binding request */ |
| 585 | static pj_status_t get_mapped_addr(pj_stun_sock *stun_sock) |
| 586 | { |
| 587 | pj_stun_tx_data *tdata; |
| 588 | pj_status_t status; |
| 589 | |
| 590 | /* Increment request counter and create STUN Binding request */ |
| 591 | ++stun_sock->tsx_id[5]; |
| 592 | status = pj_stun_session_create_req(stun_sock->stun_sess, |
| 593 | PJ_STUN_BINDING_REQUEST, |
| 594 | PJ_STUN_MAGIC, |
| 595 | (const pj_uint8_t*)stun_sock->tsx_id, |
| 596 | &tdata); |
| 597 | if (status != PJ_SUCCESS) |
| 598 | goto on_error; |
| 599 | |
| 600 | /* Send request */ |
| 601 | status=pj_stun_session_send_msg(stun_sock->stun_sess, INTERNAL_MSG_TOKEN, |
| 602 | PJ_FALSE, PJ_TRUE, &stun_sock->srv_addr, |
| 603 | pj_sockaddr_get_len(&stun_sock->srv_addr), |
| 604 | tdata); |
| 605 | if (status != PJ_SUCCESS && status != PJ_EPENDING) |
| 606 | goto on_error; |
| 607 | |
| 608 | return PJ_SUCCESS; |
| 609 | |
| 610 | on_error: |
| 611 | sess_fail(stun_sock, PJ_STUN_SOCK_BINDING_OP, status); |
| 612 | return status; |
| 613 | } |
| 614 | |
| 615 | /* Get info */ |
| 616 | PJ_DEF(pj_status_t) pj_stun_sock_get_info( pj_stun_sock *stun_sock, |
| 617 | pj_stun_sock_info *info) |
| 618 | { |
| 619 | int addr_len; |
| 620 | pj_status_t status; |
| 621 | |
| 622 | PJ_ASSERT_RETURN(stun_sock && info, PJ_EINVAL); |
| 623 | |
| 624 | pj_grp_lock_acquire(stun_sock->grp_lock); |
| 625 | |
| 626 | /* Copy STUN server address and mapped address */ |
| 627 | pj_memcpy(&info->srv_addr, &stun_sock->srv_addr, |
| 628 | sizeof(pj_sockaddr)); |
| 629 | pj_memcpy(&info->mapped_addr, &stun_sock->mapped_addr, |
| 630 | sizeof(pj_sockaddr)); |
| 631 | |
| 632 | /* Retrieve bound address */ |
| 633 | addr_len = sizeof(info->bound_addr); |
| 634 | status = pj_sock_getsockname(stun_sock->sock_fd, &info->bound_addr, |
| 635 | &addr_len); |
| 636 | if (status != PJ_SUCCESS) { |
| 637 | pj_grp_lock_release(stun_sock->grp_lock); |
| 638 | return status; |
| 639 | } |
| 640 | |
| 641 | /* If socket is bound to a specific interface, then only put that |
| 642 | * interface in the alias list. Otherwise query all the interfaces |
| 643 | * in the host. |
| 644 | */ |
| 645 | if (pj_sockaddr_has_addr(&info->bound_addr)) { |
| 646 | info->alias_cnt = 1; |
| 647 | pj_sockaddr_cp(&info->aliases[0], &info->bound_addr); |
| 648 | } else { |
| 649 | pj_sockaddr def_addr; |
| 650 | pj_uint16_t port = pj_sockaddr_get_port(&info->bound_addr); |
| 651 | unsigned i; |
| 652 | |
| 653 | /* Get the default address */ |
| 654 | status = pj_gethostip(stun_sock->af, &def_addr); |
| 655 | if (status != PJ_SUCCESS) { |
| 656 | pj_grp_lock_release(stun_sock->grp_lock); |
| 657 | return status; |
| 658 | } |
| 659 | |
| 660 | pj_sockaddr_set_port(&def_addr, port); |
| 661 | |
| 662 | /* Enum all IP interfaces in the host */ |
| 663 | info->alias_cnt = PJ_ARRAY_SIZE(info->aliases); |
| 664 | status = pj_enum_ip_interface(stun_sock->af, &info->alias_cnt, |
| 665 | info->aliases); |
| 666 | if (status != PJ_SUCCESS) { |
| 667 | pj_grp_lock_release(stun_sock->grp_lock); |
| 668 | return status; |
| 669 | } |
| 670 | |
| 671 | /* Set the port number for each address. |
| 672 | */ |
| 673 | for (i=0; i<info->alias_cnt; ++i) { |
| 674 | pj_sockaddr_set_port(&info->aliases[i], port); |
| 675 | } |
| 676 | |
| 677 | /* Put the default IP in the first slot */ |
| 678 | for (i=0; i<info->alias_cnt; ++i) { |
| 679 | if (pj_sockaddr_cmp(&info->aliases[i], &def_addr)==0) { |
| 680 | if (i!=0) { |
| 681 | pj_sockaddr_cp(&info->aliases[i], &info->aliases[0]); |
| 682 | pj_sockaddr_cp(&info->aliases[0], &def_addr); |
| 683 | } |
| 684 | break; |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | pj_grp_lock_release(stun_sock->grp_lock); |
| 690 | return PJ_SUCCESS; |
| 691 | } |
| 692 | |
| 693 | /* Send application data */ |
| 694 | PJ_DEF(pj_status_t) pj_stun_sock_sendto( pj_stun_sock *stun_sock, |
| 695 | pj_ioqueue_op_key_t *send_key, |
| 696 | const void *pkt, |
| 697 | unsigned pkt_len, |
| 698 | unsigned flag, |
| 699 | const pj_sockaddr_t *dst_addr, |
| 700 | unsigned addr_len) |
| 701 | { |
| 702 | pj_ssize_t size; |
| 703 | pj_status_t status; |
| 704 | |
| 705 | PJ_ASSERT_RETURN(stun_sock && pkt && dst_addr && addr_len, PJ_EINVAL); |
| 706 | |
| 707 | pj_grp_lock_acquire(stun_sock->grp_lock); |
| 708 | |
| 709 | if (!stun_sock->active_sock) { |
| 710 | /* We have been shutdown, but this callback may still get called |
| 711 | * by retransmit timer. |
| 712 | */ |
| 713 | pj_grp_lock_release(stun_sock->grp_lock); |
| 714 | return PJ_EINVALIDOP; |
| 715 | } |
| 716 | |
| 717 | if (send_key==NULL) |
| 718 | send_key = &stun_sock->send_key; |
| 719 | |
| 720 | size = pkt_len; |
| 721 | status = pj_activesock_sendto(stun_sock->active_sock, send_key, |
| 722 | pkt, &size, flag, dst_addr, addr_len); |
| 723 | |
| 724 | pj_grp_lock_release(stun_sock->grp_lock); |
| 725 | return status; |
| 726 | } |
| 727 | |
| 728 | /* This callback is called by the STUN session to send packet */ |
| 729 | static pj_status_t sess_on_send_msg(pj_stun_session *sess, |
| 730 | void *token, |
| 731 | const void *pkt, |
| 732 | pj_size_t pkt_size, |
| 733 | const pj_sockaddr_t *dst_addr, |
| 734 | unsigned addr_len) |
| 735 | { |
| 736 | pj_stun_sock *stun_sock; |
| 737 | pj_ssize_t size; |
| 738 | |
| 739 | stun_sock = (pj_stun_sock *) pj_stun_session_get_user_data(sess); |
| 740 | if (!stun_sock || !stun_sock->active_sock) { |
| 741 | /* We have been shutdown, but this callback may still get called |
| 742 | * by retransmit timer. |
| 743 | */ |
| 744 | return PJ_EINVALIDOP; |
| 745 | } |
| 746 | |
| 747 | pj_assert(token==INTERNAL_MSG_TOKEN); |
| 748 | PJ_UNUSED_ARG(token); |
| 749 | |
| 750 | size = pkt_size; |
| 751 | return pj_activesock_sendto(stun_sock->active_sock, |
| 752 | &stun_sock->int_send_key, |
| 753 | pkt, &size, 0, dst_addr, addr_len); |
| 754 | } |
| 755 | |
| 756 | /* This callback is called by the STUN session when outgoing transaction |
| 757 | * is complete |
| 758 | */ |
| 759 | static void sess_on_request_complete(pj_stun_session *sess, |
| 760 | pj_status_t status, |
| 761 | void *token, |
| 762 | pj_stun_tx_data *tdata, |
| 763 | const pj_stun_msg *response, |
| 764 | const pj_sockaddr_t *src_addr, |
| 765 | unsigned src_addr_len) |
| 766 | { |
| 767 | pj_stun_sock *stun_sock; |
| 768 | const pj_stun_sockaddr_attr *mapped_attr; |
| 769 | pj_stun_sock_op op; |
| 770 | pj_bool_t mapped_changed; |
| 771 | pj_bool_t resched = PJ_TRUE; |
| 772 | |
| 773 | stun_sock = (pj_stun_sock *) pj_stun_session_get_user_data(sess); |
| 774 | if (!stun_sock) |
| 775 | return; |
| 776 | |
| 777 | PJ_UNUSED_ARG(tdata); |
| 778 | PJ_UNUSED_ARG(token); |
| 779 | PJ_UNUSED_ARG(src_addr); |
| 780 | PJ_UNUSED_ARG(src_addr_len); |
| 781 | |
| 782 | /* Check if this is a keep-alive or the first Binding request */ |
| 783 | if (pj_sockaddr_has_addr(&stun_sock->mapped_addr)) |
| 784 | op = PJ_STUN_SOCK_KEEP_ALIVE_OP; |
| 785 | else |
| 786 | op = PJ_STUN_SOCK_BINDING_OP; |
| 787 | |
| 788 | /* Handle failure */ |
| 789 | if (status != PJ_SUCCESS) { |
| 790 | resched = sess_fail(stun_sock, op, status); |
| 791 | goto on_return; |
| 792 | } |
| 793 | |
| 794 | /* Get XOR-MAPPED-ADDRESS, or MAPPED-ADDRESS when XOR-MAPPED-ADDRESS |
| 795 | * doesn't exist. |
| 796 | */ |
| 797 | mapped_attr = (const pj_stun_sockaddr_attr*) |
| 798 | pj_stun_msg_find_attr(response, PJ_STUN_ATTR_XOR_MAPPED_ADDR, |
| 799 | 0); |
| 800 | if (mapped_attr==NULL) { |
| 801 | mapped_attr = (const pj_stun_sockaddr_attr*) |
| 802 | pj_stun_msg_find_attr(response, PJ_STUN_ATTR_MAPPED_ADDR, |
| 803 | 0); |
| 804 | } |
| 805 | |
| 806 | if (mapped_attr == NULL) { |
| 807 | resched = sess_fail(stun_sock, op, PJNATH_ESTUNNOMAPPEDADDR); |
| 808 | goto on_return; |
| 809 | } |
| 810 | |
| 811 | /* Determine if mapped address has changed, and save the new mapped |
| 812 | * address and call callback if so |
| 813 | */ |
| 814 | mapped_changed = !pj_sockaddr_has_addr(&stun_sock->mapped_addr) || |
| 815 | pj_sockaddr_cmp(&stun_sock->mapped_addr, |
| 816 | &mapped_attr->sockaddr) != 0; |
| 817 | if (mapped_changed) { |
| 818 | /* Print mapped adress */ |
| 819 | { |
| 820 | char addrinfo[PJ_INET6_ADDRSTRLEN+10]; |
| 821 | PJ_LOG(4,(stun_sock->obj_name, |
| 822 | "STUN mapped address found/changed: %s", |
| 823 | pj_sockaddr_print(&mapped_attr->sockaddr, |
| 824 | addrinfo, sizeof(addrinfo), 3))); |
| 825 | } |
| 826 | |
| 827 | pj_sockaddr_cp(&stun_sock->mapped_addr, &mapped_attr->sockaddr); |
| 828 | |
| 829 | if (op==PJ_STUN_SOCK_KEEP_ALIVE_OP) |
| 830 | op = PJ_STUN_SOCK_MAPPED_ADDR_CHANGE; |
| 831 | } |
| 832 | |
| 833 | /* Notify user */ |
| 834 | resched = (*stun_sock->cb.on_status)(stun_sock, op, PJ_SUCCESS); |
| 835 | |
| 836 | on_return: |
| 837 | /* Start/restart keep-alive timer */ |
| 838 | if (resched) |
| 839 | start_ka_timer(stun_sock); |
| 840 | } |
| 841 | |
| 842 | /* Schedule keep-alive timer */ |
| 843 | static void start_ka_timer(pj_stun_sock *stun_sock) |
| 844 | { |
| 845 | pj_timer_heap_cancel_if_active(stun_sock->stun_cfg.timer_heap, |
| 846 | &stun_sock->ka_timer, 0); |
| 847 | |
| 848 | pj_assert(stun_sock->ka_interval != 0); |
| 849 | if (stun_sock->ka_interval > 0 && !stun_sock->is_destroying) { |
| 850 | pj_time_val delay; |
| 851 | |
| 852 | delay.sec = stun_sock->ka_interval; |
| 853 | delay.msec = 0; |
| 854 | |
| 855 | pj_timer_heap_schedule_w_grp_lock(stun_sock->stun_cfg.timer_heap, |
| 856 | &stun_sock->ka_timer, |
| 857 | &delay, PJ_TRUE, |
| 858 | stun_sock->grp_lock); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | /* Keep-alive timer callback */ |
| 863 | static void ka_timer_cb(pj_timer_heap_t *th, pj_timer_entry *te) |
| 864 | { |
| 865 | pj_stun_sock *stun_sock; |
| 866 | |
| 867 | stun_sock = (pj_stun_sock *) te->user_data; |
| 868 | |
| 869 | PJ_UNUSED_ARG(th); |
| 870 | pj_grp_lock_acquire(stun_sock->grp_lock); |
| 871 | |
| 872 | /* Time to send STUN Binding request */ |
| 873 | if (get_mapped_addr(stun_sock) != PJ_SUCCESS) { |
| 874 | pj_grp_lock_release(stun_sock->grp_lock); |
| 875 | return; |
| 876 | } |
| 877 | |
| 878 | /* Next keep-alive timer will be scheduled once the request |
| 879 | * is complete. |
| 880 | */ |
| 881 | pj_grp_lock_release(stun_sock->grp_lock); |
| 882 | } |
| 883 | |
| 884 | /* Callback from active socket when incoming packet is received */ |
| 885 | static pj_bool_t on_data_recvfrom(pj_activesock_t *asock, |
| 886 | void *data, |
| 887 | pj_size_t size, |
| 888 | const pj_sockaddr_t *src_addr, |
| 889 | int addr_len, |
| 890 | pj_status_t status) |
| 891 | { |
| 892 | pj_stun_sock *stun_sock; |
| 893 | pj_stun_msg_hdr *hdr; |
| 894 | pj_uint16_t type; |
| 895 | |
| 896 | stun_sock = (pj_stun_sock*) pj_activesock_get_user_data(asock); |
| 897 | if (!stun_sock) |
| 898 | return PJ_FALSE; |
| 899 | |
| 900 | /* Log socket error */ |
| 901 | if (status != PJ_SUCCESS) { |
| 902 | PJ_PERROR(2,(stun_sock->obj_name, status, "recvfrom() error")); |
| 903 | return PJ_TRUE; |
| 904 | } |
| 905 | |
| 906 | pj_grp_lock_acquire(stun_sock->grp_lock); |
| 907 | |
| 908 | /* Check that this is STUN message */ |
| 909 | status = pj_stun_msg_check((const pj_uint8_t*)data, size, |
| 910 | PJ_STUN_IS_DATAGRAM | PJ_STUN_CHECK_PACKET); |
| 911 | if (status != PJ_SUCCESS) { |
| 912 | /* Not STUN -- give it to application */ |
| 913 | goto process_app_data; |
| 914 | } |
| 915 | |
| 916 | /* Treat packet as STUN header and copy the STUN message type. |
| 917 | * We don't want to access the type directly from the header |
| 918 | * since it may not be properly aligned. |
| 919 | */ |
| 920 | hdr = (pj_stun_msg_hdr*) data; |
| 921 | pj_memcpy(&type, &hdr->type, 2); |
| 922 | type = pj_ntohs(type); |
| 923 | |
| 924 | /* If the packet is a STUN Binding response and part of the |
| 925 | * transaction ID matches our internal ID, then this is |
| 926 | * our internal STUN message (Binding request or keep alive). |
| 927 | * Give it to our STUN session. |
| 928 | */ |
| 929 | if (!PJ_STUN_IS_RESPONSE(type) || |
| 930 | PJ_STUN_GET_METHOD(type) != PJ_STUN_BINDING_METHOD || |
| 931 | pj_memcmp(hdr->tsx_id, stun_sock->tsx_id, 10) != 0) |
| 932 | { |
| 933 | /* Not STUN Binding response, or STUN transaction ID mismatch. |
| 934 | * This is not our message too -- give it to application. |
| 935 | */ |
| 936 | goto process_app_data; |
| 937 | } |
| 938 | |
| 939 | /* This is our STUN Binding response. Give it to the STUN session */ |
| 940 | status = pj_stun_session_on_rx_pkt(stun_sock->stun_sess, data, size, |
| 941 | PJ_STUN_IS_DATAGRAM, NULL, NULL, |
| 942 | src_addr, addr_len); |
| 943 | |
| 944 | status = pj_grp_lock_release(stun_sock->grp_lock); |
| 945 | |
| 946 | return status!=PJ_EGONE ? PJ_TRUE : PJ_FALSE; |
| 947 | |
| 948 | process_app_data: |
| 949 | if (stun_sock->cb.on_rx_data) { |
| 950 | pj_bool_t ret; |
| 951 | |
| 952 | ret = (*stun_sock->cb.on_rx_data)(stun_sock, data, (unsigned)size, |
| 953 | src_addr, addr_len); |
| 954 | status = pj_grp_lock_release(stun_sock->grp_lock); |
| 955 | return status!=PJ_EGONE ? PJ_TRUE : PJ_FALSE; |
| 956 | } |
| 957 | |
| 958 | status = pj_grp_lock_release(stun_sock->grp_lock); |
| 959 | return status!=PJ_EGONE ? PJ_TRUE : PJ_FALSE; |
| 960 | } |
| 961 | |
| 962 | /* Callback from active socket about send status */ |
| 963 | static pj_bool_t on_data_sent(pj_activesock_t *asock, |
| 964 | pj_ioqueue_op_key_t *send_key, |
| 965 | pj_ssize_t sent) |
| 966 | { |
| 967 | pj_stun_sock *stun_sock; |
| 968 | |
| 969 | stun_sock = (pj_stun_sock*) pj_activesock_get_user_data(asock); |
| 970 | if (!stun_sock) |
| 971 | return PJ_FALSE; |
| 972 | |
| 973 | /* Don't report to callback if this is internal message */ |
| 974 | if (send_key == &stun_sock->int_send_key) { |
| 975 | return PJ_TRUE; |
| 976 | } |
| 977 | |
| 978 | /* Report to callback */ |
| 979 | if (stun_sock->cb.on_data_sent) { |
| 980 | pj_bool_t ret; |
| 981 | |
| 982 | pj_grp_lock_acquire(stun_sock->grp_lock); |
| 983 | |
| 984 | /* If app gives NULL send_key in sendto() function, then give |
| 985 | * NULL in the callback too |
| 986 | */ |
| 987 | if (send_key == &stun_sock->send_key) |
| 988 | send_key = NULL; |
| 989 | |
| 990 | /* Call callback */ |
| 991 | ret = (*stun_sock->cb.on_data_sent)(stun_sock, send_key, sent); |
| 992 | |
| 993 | pj_grp_lock_release(stun_sock->grp_lock); |
| 994 | return ret; |
| 995 | } |
| 996 | |
| 997 | return PJ_TRUE; |
| 998 | } |
| 999 | |