blob: 472df63747a6c644e5ca1e1d750b2fe6029e1918 [file] [log] [blame]
Benny Prijono268ca612006-02-07 12:34:11 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono268ca612006-02-07 12:34:11 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
Benny Prijono4f9f64e2006-02-27 00:00:30 +000019#include <pjsua-lib/pjsua.h>
Benny Prijonoeebe9af2006-06-13 22:57:13 +000020#include <pjsua-lib/pjsua_internal.h>
Benny Prijono268ca612006-02-07 12:34:11 +000021
Benny Prijono268ca612006-02-07 12:34:11 +000022
Benny Prijono84126ab2006-02-09 09:30:09 +000023#define THIS_FILE "pjsua_core.c"
Benny Prijono268ca612006-02-07 12:34:11 +000024
25
Benny Prijonoeebe9af2006-06-13 22:57:13 +000026/* PJSUA application instance. */
27struct pjsua_data pjsua_var;
Benny Prijono84126ab2006-02-09 09:30:09 +000028
29
Benny Prijono44e88ea2007-10-30 15:42:35 +000030PJ_DEF(struct pjsua_data*) pjsua_get_var(void)
31{
32 return &pjsua_var;
33}
34
35
Benny Prijonoeebe9af2006-06-13 22:57:13 +000036/* Display error */
37PJ_DEF(void) pjsua_perror( const char *sender, const char *title,
38 pj_status_t status)
Benny Prijono268ca612006-02-07 12:34:11 +000039{
Benny Prijonoeebe9af2006-06-13 22:57:13 +000040 char errmsg[PJ_ERR_MSG_SIZE];
Benny Prijonoa91a0032006-02-26 21:23:45 +000041
Benny Prijonoeebe9af2006-06-13 22:57:13 +000042 pj_strerror(status, errmsg, sizeof(errmsg));
43 PJ_LOG(3,(sender, "%s: %s [status=%d]", title, errmsg, status));
Benny Prijono268ca612006-02-07 12:34:11 +000044}
45
46
Benny Prijonoeebe9af2006-06-13 22:57:13 +000047static void init_data()
Benny Prijonodc39fe82006-05-26 12:17:46 +000048{
49 unsigned i;
50
Benny Prijonoc97608e2007-03-23 16:34:20 +000051 pj_bzero(&pjsua_var, sizeof(pjsua_var));
52
Benny Prijonoeebe9af2006-06-13 22:57:13 +000053 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i)
54 pjsua_var.acc[i].index = i;
55
56 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata); ++i)
57 pjsua_var.tpdata[i].index = i;
Benny Prijonoc97608e2007-03-23 16:34:20 +000058
59 pjsua_var.stun_status = PJ_EUNKNOWN;
Benny Prijono6ba8c542007-10-16 01:34:14 +000060 pjsua_var.nat_status = PJ_EPENDING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000061}
Benny Prijonodc39fe82006-05-26 12:17:46 +000062
63
Benny Prijono1f61a8f2007-08-16 10:11:44 +000064PJ_DEF(void) pjsua_logging_config_default(pjsua_logging_config *cfg)
65{
66 pj_bzero(cfg, sizeof(*cfg));
67
68 cfg->msg_logging = PJ_TRUE;
69 cfg->level = 5;
70 cfg->console_level = 4;
71 cfg->decor = PJ_LOG_HAS_SENDER | PJ_LOG_HAS_TIME |
72 PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE;
73}
74
75PJ_DEF(void) pjsua_logging_config_dup(pj_pool_t *pool,
76 pjsua_logging_config *dst,
77 const pjsua_logging_config *src)
78{
79 pj_memcpy(dst, src, sizeof(*src));
80 pj_strdup_with_null(pool, &dst->log_filename, &src->log_filename);
81}
82
83PJ_DEF(void) pjsua_config_default(pjsua_config *cfg)
84{
85 pj_bzero(cfg, sizeof(*cfg));
86
87 cfg->max_calls = 4;
88 cfg->thread_cnt = 1;
Benny Prijono91a6a172007-10-31 08:59:29 +000089 cfg->nat_type_in_sdp = 1;
Benny Prijono1f61a8f2007-08-16 10:11:44 +000090}
91
Benny Prijono1f61a8f2007-08-16 10:11:44 +000092PJ_DEF(void) pjsua_config_dup(pj_pool_t *pool,
93 pjsua_config *dst,
94 const pjsua_config *src)
95{
96 unsigned i;
97
98 pj_memcpy(dst, src, sizeof(*src));
99
100 for (i=0; i<src->outbound_proxy_cnt; ++i) {
101 pj_strdup_with_null(pool, &dst->outbound_proxy[i],
102 &src->outbound_proxy[i]);
103 }
104
105 for (i=0; i<src->cred_count; ++i) {
106 pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]);
107 }
108
109 pj_strdup_with_null(pool, &dst->user_agent, &src->user_agent);
110 pj_strdup_with_null(pool, &dst->stun_domain, &src->stun_domain);
111 pj_strdup_with_null(pool, &dst->stun_host, &src->stun_host);
112 pj_strdup_with_null(pool, &dst->stun_relay_host, &src->stun_relay_host);
113}
114
115PJ_DEF(void) pjsua_msg_data_init(pjsua_msg_data *msg_data)
116{
117 pj_bzero(msg_data, sizeof(*msg_data));
118 pj_list_init(&msg_data->hdr_list);
119}
120
121PJ_DEF(void) pjsua_transport_config_default(pjsua_transport_config *cfg)
122{
123 pj_bzero(cfg, sizeof(*cfg));
124 pjsip_tls_setting_default(&cfg->tls_setting);
125}
126
127PJ_DEF(void) pjsua_transport_config_dup(pj_pool_t *pool,
128 pjsua_transport_config *dst,
129 const pjsua_transport_config *src)
130{
131 PJ_UNUSED_ARG(pool);
132 pj_memcpy(dst, src, sizeof(*src));
133}
134
135PJ_DEF(void) pjsua_acc_config_default(pjsua_acc_config *cfg)
136{
137 pj_bzero(cfg, sizeof(*cfg));
138
139 cfg->reg_timeout = PJSUA_REG_INTERVAL;
140 cfg->transport_id = PJSUA_INVALID_ID;
Benny Prijono15b02302007-09-27 14:07:07 +0000141 cfg->auto_update_nat = PJ_TRUE;
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000142 cfg->require_100rel = pjsua_var.ua_cfg.require_100rel;
Benny Prijonobddef2c2007-10-31 13:28:08 +0000143 cfg->ka_interval = 15;
144 cfg->ka_data = pj_str("\r\n");
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000145}
146
147PJ_DEF(void) pjsua_buddy_config_default(pjsua_buddy_config *cfg)
148{
149 pj_bzero(cfg, sizeof(*cfg));
150}
151
152PJ_DEF(void) pjsua_media_config_default(pjsua_media_config *cfg)
153{
154 pj_bzero(cfg, sizeof(*cfg));
155
156 cfg->clock_rate = PJSUA_DEFAULT_CLOCK_RATE;
Benny Prijonocf0b4b22007-10-06 17:31:09 +0000157 cfg->audio_frame_ptime = 10;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000158 cfg->max_media_ports = 32;
159 cfg->has_ioqueue = PJ_TRUE;
160 cfg->thread_cnt = 1;
161 cfg->quality = PJSUA_DEFAULT_CODEC_QUALITY;
162 cfg->ilbc_mode = PJSUA_DEFAULT_ILBC_MODE;
163 cfg->ec_tail_len = PJSUA_DEFAULT_EC_TAIL_LEN;
164 cfg->jb_init = cfg->jb_min_pre = cfg->jb_max_pre = cfg->jb_max = -1;
165}
166
Benny Prijonodc39fe82006-05-26 12:17:46 +0000167
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000168/*****************************************************************************
169 * This is a very simple PJSIP module, whose sole purpose is to display
170 * incoming and outgoing messages to log. This module will have priority
171 * higher than transport layer, which means:
172 *
173 * - incoming messages will come to this module first before reaching
174 * transaction layer.
175 *
176 * - outgoing messages will come to this module last, after the message
177 * has been 'printed' to contiguous buffer by transport layer and
178 * appropriate transport instance has been decided for this message.
179 *
180 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000181
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000182/* Notification on incoming messages */
183static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
184{
Benny Prijonob4a17c92006-07-10 14:40:21 +0000185 PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
186 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000187 "--end msg--",
188 rdata->msg_info.len,
189 pjsip_rx_data_get_info(rdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000190 rdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000191 rdata->pkt_info.src_name,
192 rdata->pkt_info.src_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000193 (int)rdata->msg_info.len,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000194 rdata->msg_info.msg_buf));
195
196 /* Always return false, otherwise messages will not get processed! */
197 return PJ_FALSE;
198}
Benny Prijonodc39fe82006-05-26 12:17:46 +0000199
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000200/* Notification on outgoing messages */
201static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
202{
203
204 /* Important note:
205 * tp_info field is only valid after outgoing messages has passed
206 * transport layer. So don't try to access tp_info when the module
207 * has lower priority than transport layer.
208 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000209
Benny Prijonob4a17c92006-07-10 14:40:21 +0000210 PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
211 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000212 "--end msg--",
213 (tdata->buf.cur - tdata->buf.start),
214 pjsip_tx_data_get_info(tdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000215 tdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000216 tdata->tp_info.dst_name,
217 tdata->tp_info.dst_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000218 (int)(tdata->buf.cur - tdata->buf.start),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000219 tdata->buf.start));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000220
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000221 /* Always return success, otherwise message will not get sent! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000222 return PJ_SUCCESS;
223}
224
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000225/* The module instance. */
226static pjsip_module pjsua_msg_logger =
227{
228 NULL, NULL, /* prev, next. */
229 { "mod-pjsua-log", 13 }, /* Name. */
230 -1, /* Id */
231 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
232 NULL, /* load() */
233 NULL, /* start() */
234 NULL, /* stop() */
235 NULL, /* unload() */
236 &logging_on_rx_msg, /* on_rx_request() */
237 &logging_on_rx_msg, /* on_rx_response() */
238 &logging_on_tx_msg, /* on_tx_request. */
239 &logging_on_tx_msg, /* on_tx_response() */
240 NULL, /* on_tsx_state() */
241
242};
243
244
245/*****************************************************************************
Benny Prijono56315612006-07-18 14:39:40 +0000246 * Another simple module to handle incoming OPTIONS request
247 */
248
249/* Notification on incoming request */
250static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata)
251{
252 pjsip_tx_data *tdata;
253 pjsip_response_addr res_addr;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000254 pjmedia_sock_info skinfo;
Benny Prijono56315612006-07-18 14:39:40 +0000255 pjmedia_sdp_session *sdp;
256 const pjsip_hdr *cap_hdr;
257 pj_status_t status;
258
259 /* Only want to handle OPTIONS requests */
260 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000261 pjsip_get_options_method()) != 0)
Benny Prijono56315612006-07-18 14:39:40 +0000262 {
263 return PJ_FALSE;
264 }
265
266 /* Create basic response. */
267 status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL,
268 &tdata);
269 if (status != PJ_SUCCESS) {
270 pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status);
271 return PJ_TRUE;
272 }
273
274 /* Add Allow header */
275 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL);
276 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000277 pjsip_msg_add_hdr(tdata->msg,
278 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000279 }
280
281 /* Add Accept header */
282 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL);
283 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000284 pjsip_msg_add_hdr(tdata->msg,
285 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000286 }
287
288 /* Add Supported header */
289 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL);
290 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000291 pjsip_msg_add_hdr(tdata->msg,
292 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000293 }
294
295 /* Add Allow-Events header from the evsub module */
296 cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL);
297 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000298 pjsip_msg_add_hdr(tdata->msg,
299 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000300 }
301
302 /* Add User-Agent header */
303 if (pjsua_var.ua_cfg.user_agent.slen) {
304 const pj_str_t USER_AGENT = { "User-Agent", 10};
305 pjsip_hdr *h;
306
307 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
308 &USER_AGENT,
309 &pjsua_var.ua_cfg.user_agent);
310 pjsip_msg_add_hdr(tdata->msg, h);
311 }
312
Benny Prijono617c5bc2007-04-02 19:51:21 +0000313 /* Get media socket info */
314 pjmedia_transport_get_info(pjsua_var.calls[0].med_tp, &skinfo);
315
Benny Prijono56315612006-07-18 14:39:40 +0000316 /* Add SDP body, using call0's RTP address */
317 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000318 &skinfo, &sdp);
Benny Prijono56315612006-07-18 14:39:40 +0000319 if (status == PJ_SUCCESS) {
320 pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
321 }
322
323 /* Send response statelessly */
324 pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
325 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL);
326 if (status != PJ_SUCCESS)
327 pjsip_tx_data_dec_ref(tdata);
328
329 return PJ_TRUE;
330}
331
332
333/* The module instance. */
334static pjsip_module pjsua_options_handler =
335{
336 NULL, NULL, /* prev, next. */
337 { "mod-pjsua-options", 17 }, /* Name. */
338 -1, /* Id */
339 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
340 NULL, /* load() */
341 NULL, /* start() */
342 NULL, /* stop() */
343 NULL, /* unload() */
344 &options_on_rx_request, /* on_rx_request() */
345 NULL, /* on_rx_response() */
346 NULL, /* on_tx_request. */
347 NULL, /* on_tx_response() */
348 NULL, /* on_tsx_state() */
349
350};
351
352
353/*****************************************************************************
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000354 * These two functions are the main callbacks registered to PJSIP stack
355 * to receive SIP request and response messages that are outside any
356 * dialogs and any transactions.
357 */
Benny Prijono268ca612006-02-07 12:34:11 +0000358
359/*
360 * Handler for receiving incoming requests.
361 *
362 * This handler serves multiple purposes:
363 * - it receives requests outside dialogs.
364 * - it receives requests inside dialogs, when the requests are
365 * unhandled by other dialog usages. Example of these
366 * requests are: MESSAGE.
367 */
368static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
369{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000370 pj_bool_t processed = PJ_FALSE;
371
372 PJSUA_LOCK();
Benny Prijono38998232006-02-08 22:44:25 +0000373
Benny Prijono84126ab2006-02-09 09:30:09 +0000374 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000375
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000376 processed = pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000377 }
378
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000379 PJSUA_UNLOCK();
380
381 return processed;
Benny Prijono268ca612006-02-07 12:34:11 +0000382}
383
384
385/*
386 * Handler for receiving incoming responses.
387 *
388 * This handler serves multiple purposes:
389 * - it receives strayed responses (i.e. outside any dialog and
390 * outside any transactions).
391 * - it receives responses coming to a transaction, when pjsua
392 * module is set as transaction user for the transaction.
393 * - it receives responses inside a dialog, when these responses
394 * are unhandled by other dialog usages.
395 */
396static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
397{
398 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000399 return PJ_FALSE;
400}
401
402
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000403/*****************************************************************************
404 * Logging.
405 */
406
407/* Log callback */
408static void log_writer(int level, const char *buffer, int len)
Benny Prijono268ca612006-02-07 12:34:11 +0000409{
Benny Prijono572d4852006-11-23 21:50:02 +0000410 /* Write to file, stdout or application callback. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000411
412 if (pjsua_var.log_file) {
413 pj_ssize_t size = len;
414 pj_file_write(pjsua_var.log_file, buffer, &size);
Benny Prijono980ca0a2007-04-03 17:55:08 +0000415 /* This will slow things down considerably! Don't do it!
416 pj_file_flush(pjsua_var.log_file);
417 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000418 }
419
Benny Prijono572d4852006-11-23 21:50:02 +0000420 if (level <= (int)pjsua_var.log_cfg.console_level) {
421 if (pjsua_var.log_cfg.cb)
422 (*pjsua_var.log_cfg.cb)(level, buffer, len);
423 else
424 pj_log_write(level, buffer, len);
425 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000426}
427
428
429/*
430 * Application can call this function at any time (after pjsua_create(), of
431 * course) to change logging settings.
432 */
433PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg)
434{
435 pj_status_t status;
436
437 /* Save config. */
438 pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg);
439
440 /* Redirect log function to ours */
441 pj_log_set_log_func( &log_writer );
442
Benny Prijono9cb09a22007-08-30 09:35:10 +0000443 /* Set decor */
444 pj_log_set_decor(pjsua_var.log_cfg.decor);
445
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000446 /* Close existing file, if any */
447 if (pjsua_var.log_file) {
448 pj_file_close(pjsua_var.log_file);
449 pjsua_var.log_file = NULL;
450 }
451
452 /* If output log file is desired, create the file: */
453 if (pjsua_var.log_cfg.log_filename.slen) {
454
455 status = pj_file_open(pjsua_var.pool,
456 pjsua_var.log_cfg.log_filename.ptr,
457 PJ_O_WRONLY,
458 &pjsua_var.log_file);
459
460 if (status != PJ_SUCCESS) {
461 pjsua_perror(THIS_FILE, "Error creating log file", status);
462 return status;
463 }
464 }
465
466 /* Unregister msg logging if it's previously registered */
467 if (pjsua_msg_logger.id >= 0) {
468 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
469 pjsua_msg_logger.id = -1;
470 }
471
472 /* Enable SIP message logging */
473 if (pjsua_var.log_cfg.msg_logging)
474 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
475
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000476 return PJ_SUCCESS;
477}
478
479
480/*****************************************************************************
481 * PJSUA Base API.
482 */
483
484/* Worker thread function. */
485static int worker_thread(void *arg)
486{
487 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000488
Benny Prijono268ca612006-02-07 12:34:11 +0000489 PJ_UNUSED_ARG(arg);
490
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000491 while (!pjsua_var.thread_quit_flag) {
492 int count;
493
494 count = pjsua_handle_events(TIMEOUT);
495 if (count < 0)
496 pj_thread_sleep(TIMEOUT);
497 }
Benny Prijono268ca612006-02-07 12:34:11 +0000498
499 return 0;
500}
501
Benny Prijonodc39fe82006-05-26 12:17:46 +0000502
Benny Prijono268ca612006-02-07 12:34:11 +0000503/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000504 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000505 */
506PJ_DEF(pj_status_t) pjsua_create(void)
507{
508 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000509
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000510 /* Init pjsua data */
511 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000512
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000513 /* Set default logging settings */
514 pjsua_logging_config_default(&pjsua_var.log_cfg);
515
516 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000517 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000518 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
519
Benny Prijono268ca612006-02-07 12:34:11 +0000520
Benny Prijonofccab712006-02-22 22:23:22 +0000521 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000522 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000523 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000524
Benny Prijonoec921342007-03-24 13:00:30 +0000525 /* Init PJNATH */
526 status = pjnath_init();
527 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono268ca612006-02-07 12:34:11 +0000528
Benny Prijono094d3ad2006-11-21 08:41:00 +0000529 /* Set default sound device ID */
530 pjsua_var.cap_dev = pjsua_var.play_dev = -1;
531
Benny Prijono268ca612006-02-07 12:34:11 +0000532 /* Init caching pool. */
Benny Prijono106f5b72007-08-30 13:49:33 +0000533 pj_caching_pool_init(&pjsua_var.cp, NULL, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000534
535 /* Create memory pool for application. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000536 pjsua_var.pool = pjsua_pool_create("pjsua", 4000, 4000);
537
538 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000539
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000540 /* Create mutex */
541 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
542 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000543 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000544 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000545 return status;
546 }
547
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000548 /* Must create SIP endpoint to initialize SIP parser. The parser
549 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000550 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000551 status = pjsip_endpt_create(&pjsua_var.cp.factory,
552 pj_gethostname()->ptr,
553 &pjsua_var.endpt);
554 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000555
556
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000557 return PJ_SUCCESS;
558}
559
560
561/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000562 * Initialize pjsua with the specified settings. All the settings are
563 * optional, and the default values will be used when the config is not
564 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000565 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000566PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
567 const pjsua_logging_config *log_cfg,
568 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000569{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000570 pjsua_config default_cfg;
571 pjsua_media_config default_media_cfg;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000572 const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
Benny Prijonodc39fe82006-05-26 12:17:46 +0000573 pj_status_t status;
574
575
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000576 /* Create default configurations when the config is not supplied */
577
578 if (ua_cfg == NULL) {
579 pjsua_config_default(&default_cfg);
580 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000581 }
582
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000583 if (media_cfg == NULL) {
584 pjsua_media_config_default(&default_media_cfg);
585 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000586 }
587
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000588 /* Initialize logging first so that info/errors can be captured */
589 if (log_cfg) {
590 status = pjsua_reconfigure_logging(log_cfg);
591 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono9fc735d2006-05-28 14:58:12 +0000592 }
593
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000594 /* If nameserver is configured, create DNS resolver instance and
595 * set it to be used by SIP resolver.
596 */
597 if (ua_cfg->nameserver_count) {
598#if PJSIP_HAS_RESOLVER
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000599 unsigned i;
600
601 /* Create DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000602 status = pjsip_endpt_create_resolver(pjsua_var.endpt,
603 &pjsua_var.resolver);
Benny Prijono318f3842007-05-15 20:27:08 +0000604 if (status != PJ_SUCCESS) {
605 pjsua_perror(THIS_FILE, "Error creating resolver", status);
606 return status;
607 }
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000608
609 /* Configure nameserver for the DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000610 status = pj_dns_resolver_set_ns(pjsua_var.resolver,
611 ua_cfg->nameserver_count,
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000612 ua_cfg->nameserver, NULL);
613 if (status != PJ_SUCCESS) {
614 pjsua_perror(THIS_FILE, "Error setting nameserver", status);
615 return status;
616 }
617
618 /* Set this DNS resolver to be used by the SIP resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000619 status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000620 if (status != PJ_SUCCESS) {
621 pjsua_perror(THIS_FILE, "Error setting DNS resolver", status);
622 return status;
623 }
624
625 /* Print nameservers */
626 for (i=0; i<ua_cfg->nameserver_count; ++i) {
627 PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added",
628 (int)ua_cfg->nameserver[i].slen,
629 ua_cfg->nameserver[i].ptr));
630 }
631#else
632 PJ_LOG(2,(THIS_FILE,
633 "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)"));
634#endif
635 }
636
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000637 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000638
639 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000640 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
641 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000642
Benny Prijonodc39fe82006-05-26 12:17:46 +0000643
644 /* Initialize UA layer module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000645 status = pjsip_ua_init_module( pjsua_var.endpt, NULL );
646 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000647
Benny Prijonodc39fe82006-05-26 12:17:46 +0000648
Benny Prijono053f5222006-11-11 16:16:04 +0000649 /* Initialize Replaces support. */
650 status = pjsip_replaces_init_module( pjsua_var.endpt );
651 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
652
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000653 /* Initialize 100rel support */
654 status = pjsip_100rel_init_module(pjsua_var.endpt);
655 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono053f5222006-11-11 16:16:04 +0000656
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000657 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000658 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000659 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000660 {
661 NULL, NULL, /* prev, next. */
662 { "mod-pjsua", 9 }, /* Name. */
663 -1, /* Id */
664 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
665 NULL, /* load() */
666 NULL, /* start() */
667 NULL, /* stop() */
668 NULL, /* unload() */
669 &mod_pjsua_on_rx_request, /* on_rx_request() */
670 &mod_pjsua_on_rx_response, /* on_rx_response() */
671 NULL, /* on_tx_request. */
672 NULL, /* on_tx_response() */
673 NULL, /* on_tsx_state() */
674 };
675
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000676 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000677
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000678 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
679 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000680 }
681
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000682
Benny Prijonodc39fe82006-05-26 12:17:46 +0000683
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000684 /* Initialize PJSUA call subsystem: */
685 status = pjsua_call_subsys_init(ua_cfg);
686 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000687 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000688
689
Benny Prijonoc97608e2007-03-23 16:34:20 +0000690 /* Start resolving STUN server */
Benny Prijono4ab9fbb2007-10-12 12:14:27 +0000691
Benny Prijonoc97608e2007-03-23 16:34:20 +0000692 status = pjsua_resolve_stun_server(PJ_FALSE);
693 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
694 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
695 return status;
696 }
697
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000698 /* Initialize PJSUA media subsystem */
699 status = pjsua_media_subsys_init(media_cfg);
700 if (status != PJ_SUCCESS)
701 goto on_error;
702
Benny Prijonodc39fe82006-05-26 12:17:46 +0000703
704 /* Init core SIMPLE module : */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000705 status = pjsip_evsub_init_module(pjsua_var.endpt);
706 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000707
Benny Prijonodc39fe82006-05-26 12:17:46 +0000708
709 /* Init presence module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000710 status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance());
711 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000712
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000713 /* Init PUBLISH module */
714 pjsip_publishc_init_module(pjsua_var.endpt);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000715
716 /* Init xfer/REFER module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000717 status = pjsip_xfer_init_module( pjsua_var.endpt );
718 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000719
720 /* Init pjsua presence handler: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000721 status = pjsua_pres_init();
722 if (status != PJ_SUCCESS)
723 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000724
725 /* Init out-of-dialog MESSAGE request handler. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000726 status = pjsua_im_init();
727 if (status != PJ_SUCCESS)
728 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000729
Benny Prijonoc8141a82006-08-20 09:12:19 +0000730 /* Register OPTIONS handler */
731 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler);
732
733 /* Add OPTIONS in Allow header */
734 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
735 NULL, 1, &STR_OPTIONS);
736
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000737 /* Start worker thread if needed. */
738 if (pjsua_var.ua_cfg.thread_cnt) {
739 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000740
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000741 if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread))
742 pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000743
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000744 for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) {
745 status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread,
746 NULL, 0, 0, &pjsua_var.thread[i]);
747 if (status != PJ_SUCCESS)
748 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000749 }
Benny Prijonof521eb02006-08-06 23:07:25 +0000750 PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created",
751 pjsua_var.ua_cfg.thread_cnt));
752 } else {
753 PJ_LOG(4,(THIS_FILE, "No SIP worker threads created"));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000754 }
755
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000756 /* Done! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000757
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000758 PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized",
Benny Prijono106f5b72007-08-30 13:49:33 +0000759 pj_get_version(), PJ_OS_NAME));
Benny Prijonoccf95622006-02-07 18:48:01 +0000760
Benny Prijono268ca612006-02-07 12:34:11 +0000761 return PJ_SUCCESS;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000762
763on_error:
764 pjsua_destroy();
765 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000766}
767
768
Benny Prijono834aee32006-02-19 01:38:06 +0000769/* Sleep with polling */
770static void busy_sleep(unsigned msec)
771{
Benny Prijonob2c96822007-05-03 13:31:21 +0000772#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
Benny Prijono897f9f82007-05-03 19:56:21 +0000773 /* Ideally we shouldn't call pj_thread_sleep() and rather
774 * CActiveScheduler::WaitForAnyRequest() here, but that will
775 * drag in Symbian header and it doesn't look pretty.
776 */
Benny Prijonob2c96822007-05-03 13:31:21 +0000777 pj_thread_sleep(msec);
778#else
Benny Prijono834aee32006-02-19 01:38:06 +0000779 pj_time_val timeout, now;
780
781 pj_gettimeofday(&timeout);
782 timeout.msec += msec;
783 pj_time_val_normalize(&timeout);
784
785 do {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000786 while (pjsua_handle_events(10) > 0)
787 ;
Benny Prijono834aee32006-02-19 01:38:06 +0000788 pj_gettimeofday(&now);
789 } while (PJ_TIME_VAL_LT(now, timeout));
Benny Prijonob2c96822007-05-03 13:31:21 +0000790#endif
Benny Prijono834aee32006-02-19 01:38:06 +0000791}
792
Benny Prijonoebbf6892007-03-24 17:37:25 +0000793
Benny Prijonoebbf6892007-03-24 17:37:25 +0000794/*
795 * Callback function to receive notification from the resolver
796 * when the resolution process completes.
797 */
798static void stun_dns_srv_resolver_cb(void *user_data,
799 pj_status_t status,
800 const pj_dns_srv_record *rec)
801{
802 unsigned i;
803
804 PJ_UNUSED_ARG(user_data);
805
806 pjsua_var.stun_status = status;
807
808 if (status != PJ_SUCCESS) {
809 /* DNS SRV resolution failed. If stun_host is specified, resolve
810 * it with gethostbyname()
811 */
812 if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000813 pj_str_t str_host, str_port;
814 int port;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000815 pj_hostent he;
816
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000817 str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':');
818 if (str_port.ptr != NULL) {
819 str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr;
820 str_host.slen = (str_port.ptr - str_host.ptr);
821 str_port.ptr++;
822 str_port.slen = pjsua_var.ua_cfg.stun_host.slen -
823 str_host.slen - 1;
824 port = (int)pj_strtoul(&str_port);
825 if (port < 1 || port > 65535) {
826 pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL);
827 pjsua_var.stun_status = PJ_EINVAL;
828 return;
829 }
830 } else {
831 str_host = pjsua_var.ua_cfg.stun_host;
832 port = 3478;
833 }
834
835 pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
Benny Prijonoebbf6892007-03-24 17:37:25 +0000836
837 if (pjsua_var.stun_status == PJ_SUCCESS) {
838 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
839 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000840 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoebbf6892007-03-24 17:37:25 +0000841
842 PJ_LOG(3,(THIS_FILE,
843 "STUN server %.*s resolved, address is %s:%d",
844 (int)pjsua_var.ua_cfg.stun_host.slen,
845 pjsua_var.ua_cfg.stun_host.ptr,
846 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
847 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
848 }
849 } else {
850 char errmsg[PJ_ERR_MSG_SIZE];
851
852 pj_strerror(status, errmsg, sizeof(errmsg));
853 PJ_LOG(1,(THIS_FILE,
854 "DNS SRV resolution failed for STUN server %.*s: %s",
855 (int)pjsua_var.ua_cfg.stun_domain.slen,
856 pjsua_var.ua_cfg.stun_domain.ptr,
857 errmsg));
858 }
859 return;
860 }
861
Benny Prijonof5afa922007-06-11 16:51:18 +0000862 pj_assert(rec->count != 0 && rec->entry[0].server.addr_count != 0);
863 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL,
864 rec->entry[0].port);
865 pjsua_var.stun_srv.ipv4.sin_addr.s_addr =
866 rec->entry[0].server.addr[0].s_addr;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000867
868 PJ_LOG(3,(THIS_FILE, "_stun._udp.%.*s resolved, found %d entry(s):",
869 (int)pjsua_var.ua_cfg.stun_domain.slen,
870 pjsua_var.ua_cfg.stun_domain.ptr,
871 rec->count));
872
873 for (i=0; i<rec->count; ++i) {
874 PJ_LOG(3,(THIS_FILE,
875 " %d: prio=%d, weight=%d %s:%d",
876 i, rec->entry[i].priority, rec->entry[i].weight,
Benny Prijonof5afa922007-06-11 16:51:18 +0000877 pj_inet_ntoa(rec->entry[i].server.addr[0]),
878 (int)rec->entry[i].port));
Benny Prijonoebbf6892007-03-24 17:37:25 +0000879 }
880
881}
882
Benny Prijono268ca612006-02-07 12:34:11 +0000883/*
Benny Prijonoc97608e2007-03-23 16:34:20 +0000884 * Resolve STUN server.
885 */
886pj_status_t pjsua_resolve_stun_server(pj_bool_t wait)
887{
888 if (pjsua_var.stun_status == PJ_EUNKNOWN) {
889 /* Initialize STUN configuration */
890 pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0,
891 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
892 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
893
894 /* Start STUN server resolution */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000895
896 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000897
Benny Prijonoebbf6892007-03-24 17:37:25 +0000898 /* If stun_domain is specified, resolve STUN servers with DNS
899 * SRV resolution.
900 */
901 if (pjsua_var.ua_cfg.stun_domain.slen) {
902 pj_str_t res_type;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000903 pj_status_t status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000904
905 /* Fail if resolver is not configured */
906 if (pjsua_var.resolver == NULL) {
907 PJ_LOG(1,(THIS_FILE, "Nameserver must be configured when "
908 "stun_domain is specified"));
909 pjsua_var.stun_status = PJLIB_UTIL_EDNSNONS;
910 return PJLIB_UTIL_EDNSNONS;
911 }
912 res_type = pj_str("_stun._udp");
Benny Prijonoda9785b2007-04-02 20:43:06 +0000913 status =
Benny Prijonoebbf6892007-03-24 17:37:25 +0000914 pj_dns_srv_resolve(&pjsua_var.ua_cfg.stun_domain, &res_type,
915 3478, pjsua_var.pool, pjsua_var.resolver,
Benny Prijonof5afa922007-06-11 16:51:18 +0000916 0, NULL, &stun_dns_srv_resolver_cb, NULL);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000917 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +0000918 pjsua_perror(THIS_FILE, "Error starting DNS SRV resolution",
919 pjsua_var.stun_status);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000920 pjsua_var.stun_status = status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000921 return pjsua_var.stun_status;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000922 } else {
923 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000924 }
925 }
926 /* Otherwise if stun_host is specified, resolve STUN server with
927 * gethostbyname().
928 */
929 else if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000930 pj_str_t str_host, str_port;
931 int port;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000932 pj_hostent he;
933
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000934 str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':');
935 if (str_port.ptr != NULL) {
936 str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr;
937 str_host.slen = (str_port.ptr - str_host.ptr);
938 str_port.ptr++;
939 str_port.slen = pjsua_var.ua_cfg.stun_host.slen -
940 str_host.slen - 1;
941 port = (int)pj_strtoul(&str_port);
942 if (port < 1 || port > 65535) {
943 pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL);
944 pjsua_var.stun_status = PJ_EINVAL;
945 return pjsua_var.stun_status;
946 }
947 } else {
948 str_host = pjsua_var.ua_cfg.stun_host;
949 port = 3478;
950 }
951
Benny Prijono4ab9fbb2007-10-12 12:14:27 +0000952 pjsua_var.stun_status =
953 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, &str_host,
954 (pj_uint16_t)port);
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000955
Benny Prijono4ab9fbb2007-10-12 12:14:27 +0000956 if (pjsua_var.stun_status != PJ_SUCCESS) {
957 pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000958
Benny Prijono4ab9fbb2007-10-12 12:14:27 +0000959 if (pjsua_var.stun_status == PJ_SUCCESS) {
960 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
961 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
962 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
963 }
Benny Prijonoc97608e2007-03-23 16:34:20 +0000964 }
Benny Prijonoebbf6892007-03-24 17:37:25 +0000965
Benny Prijono4ab9fbb2007-10-12 12:14:27 +0000966 PJ_LOG(3,(THIS_FILE,
967 "STUN server %.*s resolved, address is %s:%d",
968 (int)pjsua_var.ua_cfg.stun_host.slen,
969 pjsua_var.ua_cfg.stun_host.ptr,
970 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
971 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
972
Benny Prijonoc97608e2007-03-23 16:34:20 +0000973 }
Benny Prijonoebbf6892007-03-24 17:37:25 +0000974 /* Otherwise disable STUN. */
975 else {
976 pjsua_var.stun_status = PJ_SUCCESS;
977 }
978
979
Benny Prijonoc97608e2007-03-23 16:34:20 +0000980 return pjsua_var.stun_status;
981
982 } else if (pjsua_var.stun_status == PJ_EPENDING) {
983 /* STUN server resolution has been started, wait for the
984 * result.
985 */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000986 if (wait) {
987 while (pjsua_var.stun_status == PJ_EPENDING)
988 pjsua_handle_events(10);
989 }
990
991 return pjsua_var.stun_status;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000992
993 } else {
994 /* STUN server has been resolved, return the status */
995 return pjsua_var.stun_status;
996 }
997}
998
999/*
Benny Prijono268ca612006-02-07 12:34:11 +00001000 * Destroy pjsua.
1001 */
Benny Prijonodc39fe82006-05-26 12:17:46 +00001002PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +00001003{
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001004 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +00001005
1006 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001007 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +00001008
1009 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001010 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
1011 if (pjsua_var.thread[i]) {
1012 pj_thread_join(pjsua_var.thread[i]);
1013 pj_thread_destroy(pjsua_var.thread[i]);
1014 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +00001015 }
Benny Prijono268ca612006-02-07 12:34:11 +00001016 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001017
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001018 if (pjsua_var.endpt) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001019 /* Terminate all calls. */
1020 pjsua_call_hangup_all();
1021
1022 /* Terminate all presence subscriptions. */
1023 pjsua_pres_shutdown();
1024
1025 /* Unregister, if required: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001026 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1027 if (!pjsua_var.acc[i].valid)
1028 continue;
1029
1030 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001031 pjsua_acc_set_registration(i, PJ_FALSE);
1032 }
1033 }
1034
1035 /* Wait for some time to allow unregistration to complete: */
Benny Prijono9fc735d2006-05-28 14:58:12 +00001036 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
1037 busy_sleep(1000);
1038 }
Benny Prijono834aee32006-02-19 01:38:06 +00001039
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001040 /* Destroy media */
1041 pjsua_media_subsys_destroy();
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001042
Benny Prijono268ca612006-02-07 12:34:11 +00001043 /* Destroy endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001044 if (pjsua_var.endpt) {
1045 pjsip_endpt_destroy(pjsua_var.endpt);
1046 pjsua_var.endpt = NULL;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001047 }
Benny Prijono268ca612006-02-07 12:34:11 +00001048
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001049 /* Destroy mutex */
1050 if (pjsua_var.mutex) {
1051 pj_mutex_destroy(pjsua_var.mutex);
1052 pjsua_var.mutex = NULL;
1053 }
1054
1055 /* Destroy pool and pool factory. */
1056 if (pjsua_var.pool) {
1057 pj_pool_release(pjsua_var.pool);
1058 pjsua_var.pool = NULL;
1059 pj_caching_pool_destroy(&pjsua_var.cp);
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00001060
1061 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
1062
1063 /* End logging */
1064 if (pjsua_var.log_file) {
1065 pj_file_close(pjsua_var.log_file);
1066 pjsua_var.log_file = NULL;
1067 }
1068
1069 /* Shutdown PJLIB */
1070 pj_shutdown();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001071 }
Benny Prijono268ca612006-02-07 12:34:11 +00001072
Benny Prijonof762ee72006-12-01 11:14:37 +00001073 /* Clear pjsua_var */
1074 pj_bzero(&pjsua_var, sizeof(pjsua_var));
1075
Benny Prijono268ca612006-02-07 12:34:11 +00001076 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001077 return PJ_SUCCESS;
1078}
1079
1080
1081/**
1082 * Application is recommended to call this function after all initialization
1083 * is done, so that the library can do additional checking set up
1084 * additional
1085 *
1086 * @return PJ_SUCCESS on success, or the appropriate error code.
1087 */
1088PJ_DEF(pj_status_t) pjsua_start(void)
1089{
1090 pj_status_t status;
1091
1092 status = pjsua_call_subsys_start();
1093 if (status != PJ_SUCCESS)
1094 return status;
1095
1096 status = pjsua_media_subsys_start();
1097 if (status != PJ_SUCCESS)
1098 return status;
1099
1100 status = pjsua_pres_start();
1101 if (status != PJ_SUCCESS)
1102 return status;
Benny Prijono268ca612006-02-07 12:34:11 +00001103
1104 return PJ_SUCCESS;
1105}
1106
Benny Prijono9fc735d2006-05-28 14:58:12 +00001107
1108/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001109 * Poll pjsua for events, and if necessary block the caller thread for
1110 * the specified maximum interval (in miliseconds).
1111 */
1112PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
1113{
Benny Prijono897f9f82007-05-03 19:56:21 +00001114#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
1115 /* Ideally we shouldn't call pj_thread_sleep() and rather
1116 * CActiveScheduler::WaitForAnyRequest() here, but that will
1117 * drag in Symbian header and it doesn't look pretty.
1118 */
1119 pj_thread_sleep(msec_timeout);
1120 return msec_timeout;
1121#else
1122
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001123 unsigned count = 0;
1124 pj_time_val tv;
1125 pj_status_t status;
1126
1127 tv.sec = 0;
1128 tv.msec = msec_timeout;
1129 pj_time_val_normalize(&tv);
1130
1131 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
1132
1133 if (status != PJ_SUCCESS)
1134 return -status;
1135
1136 return count;
Benny Prijono897f9f82007-05-03 19:56:21 +00001137#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001138}
1139
1140
1141/*
1142 * Create memory pool.
1143 */
1144PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
1145 pj_size_t increment)
1146{
1147 /* Pool factory is thread safe, no need to lock */
1148 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
1149 NULL);
1150}
1151
1152
1153/*
1154 * Internal function to get SIP endpoint instance of pjsua, which is
1155 * needed for example to register module, create transports, etc.
1156 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001157 */
1158PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
1159{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001160 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001161}
1162
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001163/*
1164 * Internal function to get media endpoint instance.
1165 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001166 */
1167PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
1168{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001169 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001170}
1171
Benny Prijono97b87172006-08-24 14:25:14 +00001172/*
1173 * Internal function to get PJSUA pool factory.
1174 */
1175PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
1176{
1177 return &pjsua_var.cp.factory;
1178}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001179
1180/*****************************************************************************
1181 * PJSUA SIP Transport API.
1182 */
1183
1184/*
1185 * Create and initialize SIP socket (and possibly resolve public
1186 * address via STUN, depending on config).
1187 */
1188static pj_status_t create_sip_udp_sock(pj_in_addr bound_addr,
1189 int port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001190 pj_sock_t *p_sock,
1191 pj_sockaddr_in *p_pub_addr)
1192{
Benny Prijonoc97608e2007-03-23 16:34:20 +00001193 char ip_addr[32];
1194 pj_str_t stun_srv;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001195 pj_sock_t sock;
1196 pj_status_t status;
1197
Benny Prijonoc97608e2007-03-23 16:34:20 +00001198 /* Make sure STUN server resolution has completed */
1199 status = pjsua_resolve_stun_server(PJ_TRUE);
1200 if (status != PJ_SUCCESS) {
1201 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
1202 return status;
1203 }
1204
Benny Prijono8ab968f2007-07-20 08:08:30 +00001205 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001206 if (status != PJ_SUCCESS) {
1207 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +00001208 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001209 }
1210
Benny Prijonof90ef4f2007-02-12 14:59:57 +00001211 status = pj_sock_bind_in(sock, pj_ntohl(bound_addr.s_addr),
1212 (pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001213 if (status != PJ_SUCCESS) {
1214 pjsua_perror(THIS_FILE, "bind() error", status);
1215 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001216 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001217 }
1218
Benny Prijonoe347cb02007-02-14 14:36:13 +00001219 /* If port is zero, get the bound port */
1220 if (port == 0) {
1221 pj_sockaddr_in bound_addr;
1222 int namelen = sizeof(bound_addr);
1223 status = pj_sock_getsockname(sock, &bound_addr, &namelen);
1224 if (status != PJ_SUCCESS) {
1225 pjsua_perror(THIS_FILE, "getsockname() error", status);
1226 pj_sock_close(sock);
1227 return status;
1228 }
1229
1230 port = pj_ntohs(bound_addr.sin_port);
1231 }
1232
Benny Prijonoc97608e2007-03-23 16:34:20 +00001233 if (pjsua_var.stun_srv.addr.sa_family != 0) {
1234 pj_ansi_strcpy(ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
1235 stun_srv = pj_str(ip_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001236 } else {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001237 stun_srv.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001238 }
1239
1240 /* Get the published address, either by STUN or by resolving
1241 * the name of local host.
1242 */
Benny Prijonoc97608e2007-03-23 16:34:20 +00001243 if (stun_srv.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +00001244 /*
1245 * STUN is specified, resolve the address with STUN.
1246 */
Benny Prijono14c2b862007-02-21 00:40:05 +00001247 status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001248 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
1249 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijonoc97608e2007-03-23 16:34:20 +00001250 p_pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001251 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +00001252 pjsua_perror(THIS_FILE, "Error contacting STUN server", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001253 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001254 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001255 }
1256
Benny Prijono0a5cad82006-09-26 13:21:02 +00001257 } else if (p_pub_addr->sin_addr.s_addr != 0) {
1258 /*
1259 * Public address is already specified, no need to resolve the
1260 * address, only set the port.
1261 */
Benny Prijonoe347cb02007-02-14 14:36:13 +00001262 if (p_pub_addr->sin_port == 0)
1263 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001264
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001265 } else {
1266
Benny Prijono594e4c52006-09-14 18:51:01 +00001267 pj_bzero(p_pub_addr, sizeof(pj_sockaddr_in));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001268
Benny Prijono594e4c52006-09-14 18:51:01 +00001269 status = pj_gethostip(&p_pub_addr->sin_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001270 if (status != PJ_SUCCESS) {
Benny Prijonob43bad72007-01-20 05:11:08 +00001271 pjsua_perror(THIS_FILE, "Unable to get local host IP", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001272 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001273 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001274 }
1275
Benny Prijono8ab968f2007-07-20 08:08:30 +00001276 p_pub_addr->sin_family = pj_AF_INET();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001277 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001278 }
1279
1280 *p_sock = sock;
1281
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001282 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
1283 pj_inet_ntoa(p_pub_addr->sin_addr),
1284 (int)pj_ntohs(p_pub_addr->sin_port)));
1285
Benny Prijonod8410532006-06-15 11:04:33 +00001286 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001287}
1288
1289
1290/*
1291 * Create SIP transport.
1292 */
1293PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
1294 const pjsua_transport_config *cfg,
1295 pjsua_transport_id *p_id)
1296{
1297 pjsip_transport *tp;
1298 unsigned id;
1299 pj_status_t status;
1300
1301 PJSUA_LOCK();
1302
1303 /* Find empty transport slot */
1304 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001305 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001306 break;
1307 }
1308
1309 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1310 status = PJ_ETOOMANY;
1311 pjsua_perror(THIS_FILE, "Error creating transport", status);
1312 goto on_return;
1313 }
1314
1315 /* Create the transport */
1316 if (type == PJSIP_TRANSPORT_UDP) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001317 /*
1318 * Create UDP transport.
1319 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001320 pjsua_transport_config config;
Benny Prijonod8410532006-06-15 11:04:33 +00001321 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001322 pj_sockaddr_in bound_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001323 pj_sockaddr_in pub_addr;
1324 pjsip_host_port addr_name;
1325
1326 /* Supply default config if it's not specified */
1327 if (cfg == NULL) {
1328 pjsua_transport_config_default(&config);
1329 cfg = &config;
1330 }
1331
Benny Prijono0a5cad82006-09-26 13:21:02 +00001332 /* Initialize bound address, if any */
1333 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
1334 if (cfg->bound_addr.slen) {
1335 status = pj_sockaddr_in_set_str_addr(&bound_addr,&cfg->bound_addr);
1336 if (status != PJ_SUCCESS) {
1337 pjsua_perror(THIS_FILE,
1338 "Unable to resolve transport bound address",
1339 status);
1340 goto on_return;
1341 }
1342 }
1343
1344 /* Initialize the public address from the config, if any */
1345 pj_sockaddr_in_init(&pub_addr, NULL, (pj_uint16_t)cfg->port);
1346 if (cfg->public_addr.slen) {
1347 status = pj_sockaddr_in_set_str_addr(&pub_addr, &cfg->public_addr);
1348 if (status != PJ_SUCCESS) {
1349 pjsua_perror(THIS_FILE,
1350 "Unable to resolve transport public address",
1351 status);
1352 goto on_return;
1353 }
1354 }
1355
1356 /* Create the socket and possibly resolve the address with STUN
1357 * (only when public address is not specified).
1358 */
1359 status = create_sip_udp_sock(bound_addr.sin_addr, cfg->port,
Benny Prijono0a5cad82006-09-26 13:21:02 +00001360 &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001361 if (status != PJ_SUCCESS)
1362 goto on_return;
1363
1364 addr_name.host = pj_str(pj_inet_ntoa(pub_addr.sin_addr));
1365 addr_name.port = pj_ntohs(pub_addr.sin_port);
1366
1367 /* Create UDP transport */
1368 status = pjsip_udp_transport_attach( pjsua_var.endpt, sock,
1369 &addr_name, 1,
1370 &tp);
1371 if (status != PJ_SUCCESS) {
1372 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
1373 status);
1374 pj_sock_close(sock);
1375 goto on_return;
1376 }
1377
Benny Prijonoe93e2872006-06-28 16:46:49 +00001378
1379 /* Save the transport */
1380 pjsua_var.tpdata[id].type = type;
1381 pjsua_var.tpdata[id].local_name = tp->local_name;
1382 pjsua_var.tpdata[id].data.tp = tp;
1383
Benny Prijono3569c0d2007-04-06 10:29:20 +00001384#if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0
1385
Benny Prijonoe93e2872006-06-28 16:46:49 +00001386 } else if (type == PJSIP_TRANSPORT_TCP) {
1387 /*
1388 * Create TCP transport.
1389 */
1390 pjsua_transport_config config;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001391 pjsip_host_port a_name;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001392 pjsip_tpfactory *tcp;
1393 pj_sockaddr_in local_addr;
1394
1395 /* Supply default config if it's not specified */
1396 if (cfg == NULL) {
1397 pjsua_transport_config_default(&config);
1398 cfg = &config;
1399 }
1400
1401 /* Init local address */
1402 pj_sockaddr_in_init(&local_addr, 0, 0);
1403
1404 if (cfg->port)
1405 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1406
Benny Prijono0a5cad82006-09-26 13:21:02 +00001407 if (cfg->bound_addr.slen) {
1408 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1409 if (status != PJ_SUCCESS) {
1410 pjsua_perror(THIS_FILE,
1411 "Unable to resolve transport bound address",
1412 status);
1413 goto on_return;
1414 }
1415 }
1416
1417 /* Init published name */
1418 pj_bzero(&a_name, sizeof(pjsip_host_port));
1419 if (cfg->public_addr.slen)
1420 a_name.host = cfg->public_addr;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001421
1422 /* Create the TCP transport */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001423 status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr,
1424 &a_name, 1, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001425
1426 if (status != PJ_SUCCESS) {
1427 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1428 status);
1429 goto on_return;
1430 }
1431
1432 /* Save the transport */
1433 pjsua_var.tpdata[id].type = type;
1434 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1435 pjsua_var.tpdata[id].data.factory = tcp;
1436
Benny Prijono3569c0d2007-04-06 10:29:20 +00001437#endif /* PJ_HAS_TCP */
1438
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001439#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1440 } else if (type == PJSIP_TRANSPORT_TLS) {
1441 /*
1442 * Create TLS transport.
1443 */
Benny Prijonof3bbc132006-12-25 06:43:59 +00001444 /*
1445 * Create TCP transport.
1446 */
1447 pjsua_transport_config config;
1448 pjsip_host_port a_name;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001449 pjsip_tpfactory *tls;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001450 pj_sockaddr_in local_addr;
1451
1452 /* Supply default config if it's not specified */
1453 if (cfg == NULL) {
1454 pjsua_transport_config_default(&config);
1455 config.port = 5061;
1456 cfg = &config;
1457 }
1458
1459 /* Init local address */
1460 pj_sockaddr_in_init(&local_addr, 0, 0);
1461
1462 if (cfg->port)
1463 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1464
1465 if (cfg->bound_addr.slen) {
1466 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1467 if (status != PJ_SUCCESS) {
1468 pjsua_perror(THIS_FILE,
1469 "Unable to resolve transport bound address",
1470 status);
1471 goto on_return;
1472 }
1473 }
1474
1475 /* Init published name */
1476 pj_bzero(&a_name, sizeof(pjsip_host_port));
1477 if (cfg->public_addr.slen)
1478 a_name.host = cfg->public_addr;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001479
1480 status = pjsip_tls_transport_start(pjsua_var.endpt,
Benny Prijonof3bbc132006-12-25 06:43:59 +00001481 &cfg->tls_setting,
1482 &local_addr, &a_name, 1, &tls);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001483 if (status != PJ_SUCCESS) {
1484 pjsua_perror(THIS_FILE, "Error creating SIP TLS listener",
1485 status);
1486 goto on_return;
1487 }
1488
1489 /* Save the transport */
1490 pjsua_var.tpdata[id].type = type;
1491 pjsua_var.tpdata[id].local_name = tls->addr_name;
1492 pjsua_var.tpdata[id].data.factory = tls;
1493#endif
1494
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001495 } else {
1496 status = PJSIP_EUNSUPTRANSPORT;
1497 pjsua_perror(THIS_FILE, "Error creating transport", status);
1498 goto on_return;
1499 }
1500
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001501
1502 /* Return the ID */
1503 if (p_id) *p_id = id;
1504
1505 status = PJ_SUCCESS;
1506
1507on_return:
1508
1509 PJSUA_UNLOCK();
1510
Benny Prijonod8410532006-06-15 11:04:33 +00001511 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001512}
1513
1514
1515/*
1516 * Register transport that has been created by application.
1517 */
1518PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1519 pjsua_transport_id *p_id)
1520{
1521 unsigned id;
1522
1523 PJSUA_LOCK();
1524
1525 /* Find empty transport slot */
1526 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001527 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001528 break;
1529 }
1530
1531 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1532 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1533 PJSUA_UNLOCK();
1534 return PJ_ETOOMANY;
1535 }
1536
1537 /* Save the transport */
Benny Prijonod17a5e92007-05-29 11:51:45 +00001538 pjsua_var.tpdata[id].type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001539 pjsua_var.tpdata[id].local_name = tp->local_name;
1540 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001541
1542 /* Return the ID */
1543 if (p_id) *p_id = id;
1544
1545 PJSUA_UNLOCK();
1546
1547 return PJ_SUCCESS;
1548}
1549
1550
1551/*
1552 * Enumerate all transports currently created in the system.
1553 */
1554PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1555 unsigned *p_count )
1556{
1557 unsigned i, count;
1558
1559 PJSUA_LOCK();
1560
1561 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
1562 ++i)
1563 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001564 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001565 continue;
1566
1567 id[count++] = i;
1568 }
1569
1570 *p_count = count;
1571
1572 PJSUA_UNLOCK();
1573
1574 return PJ_SUCCESS;
1575}
1576
1577
1578/*
1579 * Get information about transports.
1580 */
1581PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
1582 pjsua_transport_info *info)
1583{
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001584 pjsua_transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00001585 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001586
Benny Prijonoac623b32006-07-03 15:19:31 +00001587 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001588
1589 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001590 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1591 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001592
1593 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001594 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001595
1596 PJSUA_LOCK();
1597
Benny Prijonoe93e2872006-06-28 16:46:49 +00001598 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
1599
1600 pjsip_transport *tp = t->data.tp;
1601
1602 if (tp == NULL) {
1603 PJSUA_UNLOCK();
1604 return PJ_EINVALIDOP;
1605 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001606
Benny Prijonoe93e2872006-06-28 16:46:49 +00001607 info->id = id;
Benny Prijonod17a5e92007-05-29 11:51:45 +00001608 info->type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001609 info->type_name = pj_str(tp->type_name);
1610 info->info = pj_str(tp->info);
1611 info->flag = tp->flag;
1612 info->addr_len = tp->addr_len;
1613 info->local_addr = tp->local_addr;
1614 info->local_name = tp->local_name;
1615 info->usage_count = pj_atomic_get(tp->ref_cnt);
1616
Benny Prijono0a5cad82006-09-26 13:21:02 +00001617 status = PJ_SUCCESS;
1618
Benny Prijonoe93e2872006-06-28 16:46:49 +00001619 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
1620
1621 pjsip_tpfactory *factory = t->data.factory;
1622
1623 if (factory == NULL) {
1624 PJSUA_UNLOCK();
1625 return PJ_EINVALIDOP;
1626 }
1627
1628 info->id = id;
1629 info->type = t->type;
1630 info->type_name = pj_str("TCP");
1631 info->info = pj_str("TCP transport");
1632 info->flag = factory->flag;
1633 info->addr_len = sizeof(factory->local_addr);
1634 info->local_addr = factory->local_addr;
1635 info->local_name = factory->addr_name;
1636 info->usage_count = 0;
1637
Benny Prijono0a5cad82006-09-26 13:21:02 +00001638 status = PJ_SUCCESS;
1639
1640 } else {
1641 pj_assert(!"Unsupported transport");
1642 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001643 }
1644
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001645
1646 PJSUA_UNLOCK();
1647
Benny Prijono0a5cad82006-09-26 13:21:02 +00001648 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001649}
1650
1651
1652/*
1653 * Disable a transport or re-enable it.
1654 */
1655PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
1656 pj_bool_t enabled)
1657{
1658 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001659 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1660 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001661
1662 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001663 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001664
1665
1666 /* To be done!! */
1667 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001668 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001669
1670 return PJ_EINVALIDOP;
1671}
1672
1673
1674/*
1675 * Close the transport.
1676 */
1677PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
1678 pj_bool_t force )
1679{
Benny Prijono5ff61872007-02-01 03:37:11 +00001680 pj_status_t status;
1681
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001682 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001683 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1684 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001685
1686 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001687 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001688
Benny Prijono0a5cad82006-09-26 13:21:02 +00001689 /* Note: destroy() may not work if there are objects still referencing
1690 * the transport.
1691 */
1692 if (force) {
1693 switch (pjsua_var.tpdata[id].type) {
1694 case PJSIP_TRANSPORT_UDP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001695 status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
1696 if (status != PJ_SUCCESS)
1697 return status;
1698 status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
1699 if (status != PJ_SUCCESS)
1700 return status;
1701 break;
1702
1703 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001704 case PJSIP_TRANSPORT_TCP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001705 /* This will close the TCP listener, but existing TCP/TLS
1706 * connections (if any) will still linger
1707 */
1708 status = (*pjsua_var.tpdata[id].data.factory->destroy)
1709 (pjsua_var.tpdata[id].data.factory);
1710 if (status != PJ_SUCCESS)
1711 return status;
1712
Benny Prijono0a5cad82006-09-26 13:21:02 +00001713 break;
Benny Prijono5ff61872007-02-01 03:37:11 +00001714
Benny Prijono1ebd6142006-10-19 15:48:02 +00001715 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001716 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001717 }
1718
1719 } else {
Benny Prijono5ff61872007-02-01 03:37:11 +00001720 /* If force is not specified, transports will be closed at their
1721 * convenient time. However this will leak PJSUA-API transport
1722 * descriptors as PJSUA-API wouldn't know when exactly the
1723 * transport is closed thus it can't cleanup PJSUA transport
1724 * descriptor.
1725 */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001726 switch (pjsua_var.tpdata[id].type) {
1727 case PJSIP_TRANSPORT_UDP:
1728 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
Benny Prijono5ff61872007-02-01 03:37:11 +00001729 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001730 case PJSIP_TRANSPORT_TCP:
1731 return (*pjsua_var.tpdata[id].data.factory->destroy)
1732 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001733 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001734 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001735 }
1736 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001737
Benny Prijono5ff61872007-02-01 03:37:11 +00001738 /* Cleanup pjsua data when force is applied */
1739 if (force) {
1740 pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED;
1741 pjsua_var.tpdata[id].data.ptr = NULL;
1742 }
1743
1744 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001745}
1746
1747
1748/*
1749 * Add additional headers etc in msg_data specified by application
1750 * when sending requests.
1751 */
1752void pjsua_process_msg_data(pjsip_tx_data *tdata,
1753 const pjsua_msg_data *msg_data)
1754{
1755 pj_bool_t allow_body;
1756 const pjsip_hdr *hdr;
1757
Benny Prijono56315612006-07-18 14:39:40 +00001758 /* Always add User-Agent */
1759 if (pjsua_var.ua_cfg.user_agent.slen &&
1760 tdata->msg->type == PJSIP_REQUEST_MSG)
1761 {
1762 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1763 pjsip_hdr *h;
1764 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
1765 &STR_USER_AGENT,
1766 &pjsua_var.ua_cfg.user_agent);
1767 pjsip_msg_add_hdr(tdata->msg, h);
1768 }
1769
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001770 if (!msg_data)
1771 return;
1772
1773 hdr = msg_data->hdr_list.next;
1774 while (hdr && hdr != &msg_data->hdr_list) {
1775 pjsip_hdr *new_hdr;
1776
Benny Prijonoa1e69682007-05-11 15:14:34 +00001777 new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001778 pjsip_msg_add_hdr(tdata->msg, new_hdr);
1779
1780 hdr = hdr->next;
1781 }
1782
1783 allow_body = (tdata->msg->body == NULL);
1784
1785 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
1786 pjsip_media_type ctype;
1787 pjsip_msg_body *body;
1788
1789 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
1790 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
1791 &msg_data->msg_body);
1792 tdata->msg->body = body;
1793 }
1794}
1795
1796
1797/*
1798 * Add route_set to outgoing requests
1799 */
1800void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
1801 const pjsip_route_hdr *route_set )
1802{
1803 const pjsip_route_hdr *r;
1804
1805 r = route_set->next;
1806 while (r != route_set) {
1807 pjsip_route_hdr *new_r;
1808
Benny Prijonoa1e69682007-05-11 15:14:34 +00001809 new_r = (pjsip_route_hdr*) pjsip_hdr_clone(tdata->pool, r);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001810 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
1811
1812 r = r->next;
1813 }
1814}
1815
1816
1817/*
1818 * Simple version of MIME type parsing (it doesn't support parameters)
1819 */
1820void pjsua_parse_media_type( pj_pool_t *pool,
1821 const pj_str_t *mime,
1822 pjsip_media_type *media_type)
1823{
1824 pj_str_t tmp;
1825 char *pos;
1826
Benny Prijonoac623b32006-07-03 15:19:31 +00001827 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001828
1829 pj_strdup_with_null(pool, &tmp, mime);
1830
1831 pos = pj_strchr(&tmp, '/');
1832 if (pos) {
1833 media_type->type.ptr = tmp.ptr;
1834 media_type->type.slen = (pos-tmp.ptr);
1835 media_type->subtype.ptr = pos+1;
1836 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
1837 } else {
1838 media_type->type = tmp;
1839 }
1840}
1841
1842
1843/*
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001844 * Internal function to init transport selector from transport id.
1845 */
1846void pjsua_init_tpselector(pjsua_transport_id tp_id,
1847 pjsip_tpselector *sel)
1848{
1849 pjsua_transport_data *tpdata;
1850 unsigned flag;
1851
1852 pj_bzero(sel, sizeof(*sel));
1853 if (tp_id == PJSUA_INVALID_ID)
1854 return;
1855
Benny Prijonoa1e69682007-05-11 15:14:34 +00001856 pj_assert(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata));
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001857 tpdata = &pjsua_var.tpdata[tp_id];
1858
1859 flag = pjsip_transport_get_flag_from_type(tpdata->type);
1860
1861 if (flag & PJSIP_TRANSPORT_DATAGRAM) {
1862 sel->type = PJSIP_TPSELECTOR_TRANSPORT;
1863 sel->u.transport = tpdata->data.tp;
1864 } else {
1865 sel->type = PJSIP_TPSELECTOR_LISTENER;
1866 sel->u.listener = tpdata->data.factory;
1867 }
1868}
1869
1870
Benny Prijono6ba8c542007-10-16 01:34:14 +00001871/* Callback upon NAT detection completion */
1872static void nat_detect_cb(void *user_data,
1873 const pj_stun_nat_detect_result *res)
1874{
1875 PJ_UNUSED_ARG(user_data);
1876
1877 pjsua_var.nat_in_progress = PJ_FALSE;
1878 pjsua_var.nat_status = res->status;
1879 pjsua_var.nat_type = res->nat_type;
1880
1881 if (pjsua_var.ua_cfg.cb.on_nat_detect) {
1882 (*pjsua_var.ua_cfg.cb.on_nat_detect)(res);
1883 }
1884}
1885
1886
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001887/*
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001888 * Detect NAT type.
1889 */
Benny Prijono6ba8c542007-10-16 01:34:14 +00001890PJ_DEF(pj_status_t) pjsua_detect_nat_type()
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001891{
1892 pj_status_t status;
1893
Benny Prijono6ba8c542007-10-16 01:34:14 +00001894 if (pjsua_var.nat_in_progress)
1895 return PJ_SUCCESS;
1896
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001897 /* Make sure STUN server resolution has completed */
1898 status = pjsua_resolve_stun_server(PJ_TRUE);
1899 if (status != PJ_SUCCESS) {
Benny Prijono6ba8c542007-10-16 01:34:14 +00001900 pjsua_var.nat_status = status;
1901 pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001902 return status;
1903 }
1904
1905 /* Make sure we have STUN */
1906 if (pjsua_var.stun_srv.ipv4.sin_family == 0) {
Benny Prijono6ba8c542007-10-16 01:34:14 +00001907 pjsua_var.nat_status = PJNATH_ESTUNINSERVER;
1908 return PJNATH_ESTUNINSERVER;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001909 }
1910
Benny Prijono6ba8c542007-10-16 01:34:14 +00001911 status = pj_stun_detect_nat_type(&pjsua_var.stun_srv.ipv4,
1912 &pjsua_var.stun_cfg,
1913 NULL, &nat_detect_cb);
1914
1915 if (status != PJ_SUCCESS) {
1916 pjsua_var.nat_status = status;
1917 pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
1918 return status;
1919 }
1920
1921 pjsua_var.nat_in_progress = PJ_TRUE;
1922
1923 return PJ_SUCCESS;
1924}
1925
1926
1927/*
1928 * Get NAT type.
1929 */
1930PJ_DEF(pj_status_t) pjsua_get_nat_type(pj_stun_nat_type *type)
1931{
1932 *type = pjsua_var.nat_type;
1933 return pjsua_var.nat_status;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001934}
1935
1936
1937/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001938 * Verify that valid SIP url is given.
1939 */
1940PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
1941{
1942 pjsip_uri *p;
1943 pj_pool_t *pool;
1944 char *url;
1945 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
1946
1947 if (!len) return -1;
1948
1949 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
1950 if (!pool) return -1;
1951
Benny Prijonoa1e69682007-05-11 15:14:34 +00001952 url = (char*) pj_pool_alloc(pool, len+1);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001953 pj_ansi_strcpy(url, c_url);
1954
1955 p = pjsip_parse_uri(pool, url, len, 0);
Benny Prijonocf0b4b22007-10-06 17:31:09 +00001956 if (!p || (pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0 &&
1957 pj_stricmp2(pjsip_uri_get_scheme(p), "sips") != 0))
1958 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001959 p = NULL;
Benny Prijonocf0b4b22007-10-06 17:31:09 +00001960 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001961
1962 pj_pool_release(pool);
1963 return p ? 0 : -1;
1964}
Benny Prijonoda9785b2007-04-02 20:43:06 +00001965
1966
1967/*
1968 * This is a utility function to dump the stack states to log, using
1969 * verbosity level 3.
1970 */
1971PJ_DEF(void) pjsua_dump(pj_bool_t detail)
1972{
1973 unsigned old_decor;
1974 unsigned i;
1975 char buf[1024];
1976
1977 PJ_LOG(3,(THIS_FILE, "Start dumping application states:"));
1978
1979 old_decor = pj_log_get_decor();
1980 pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
1981
1982 if (detail)
1983 pj_dump_config();
1984
1985 pjsip_endpt_dump(pjsua_get_pjsip_endpt(), detail);
1986
1987 pjmedia_endpt_dump(pjsua_get_pjmedia_endpt());
1988
1989 PJ_LOG(3,(THIS_FILE, "Dumping media transports:"));
1990 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
1991 pjsua_call *call = &pjsua_var.calls[i];
1992 pjmedia_sock_info skinfo;
1993
Benny Prijono4f093d22007-04-09 08:54:32 +00001994 /* MSVC complains about skinfo not being initialized */
1995 pj_bzero(&skinfo, sizeof(skinfo));
1996
Benny Prijonoda9785b2007-04-02 20:43:06 +00001997 pjmedia_transport_get_info(call->med_tp, &skinfo);
1998
1999 PJ_LOG(3,(THIS_FILE, " %s: %s:%d",
2000 (pjsua_var.media_cfg.enable_ice ? "ICE" : "UDP"),
2001 pj_inet_ntoa(skinfo.rtp_addr_name.sin_addr),
2002 (int)pj_ntohs(skinfo.rtp_addr_name.sin_port)));
2003 }
2004
2005 pjsip_tsx_layer_dump(detail);
2006 pjsip_ua_dump(detail);
2007
2008
2009 /* Dump all invite sessions: */
2010 PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:"));
2011
2012 if (pjsua_call_get_count() == 0) {
2013
2014 PJ_LOG(3,(THIS_FILE, " - no sessions -"));
2015
2016 } else {
2017 unsigned i;
2018
2019 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
2020 if (pjsua_call_is_active(i)) {
2021 pjsua_call_dump(i, detail, buf, sizeof(buf), " ");
2022 PJ_LOG(3,(THIS_FILE, "%s", buf));
2023 }
2024 }
2025 }
2026
2027 /* Dump presence status */
2028 pjsua_pres_dump(detail);
2029
2030 pj_log_set_decor(old_decor);
2031 PJ_LOG(3,(THIS_FILE, "Dump complete"));
2032}
2033