blob: 3b51266649b1241c439c0901ad46c60ea619a2d5 [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 Prijonoeebe9af2006-06-13 22:57:13 +000030/* Display error */
31PJ_DEF(void) pjsua_perror( const char *sender, const char *title,
32 pj_status_t status)
Benny Prijono268ca612006-02-07 12:34:11 +000033{
Benny Prijonoeebe9af2006-06-13 22:57:13 +000034 char errmsg[PJ_ERR_MSG_SIZE];
Benny Prijonoa91a0032006-02-26 21:23:45 +000035
Benny Prijonoeebe9af2006-06-13 22:57:13 +000036 pj_strerror(status, errmsg, sizeof(errmsg));
37 PJ_LOG(3,(sender, "%s: %s [status=%d]", title, errmsg, status));
Benny Prijono268ca612006-02-07 12:34:11 +000038}
39
40
Benny Prijonoeebe9af2006-06-13 22:57:13 +000041static void init_data()
Benny Prijonodc39fe82006-05-26 12:17:46 +000042{
43 unsigned i;
44
Benny Prijonoc97608e2007-03-23 16:34:20 +000045 pj_bzero(&pjsua_var, sizeof(pjsua_var));
46
Benny Prijonoeebe9af2006-06-13 22:57:13 +000047 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i)
48 pjsua_var.acc[i].index = i;
49
50 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata); ++i)
51 pjsua_var.tpdata[i].index = i;
Benny Prijonoc97608e2007-03-23 16:34:20 +000052
53 pjsua_var.stun_status = PJ_EUNKNOWN;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000054}
Benny Prijonodc39fe82006-05-26 12:17:46 +000055
56
Benny Prijono1f61a8f2007-08-16 10:11:44 +000057PJ_DEF(void) pjsua_logging_config_default(pjsua_logging_config *cfg)
58{
59 pj_bzero(cfg, sizeof(*cfg));
60
61 cfg->msg_logging = PJ_TRUE;
62 cfg->level = 5;
63 cfg->console_level = 4;
64 cfg->decor = PJ_LOG_HAS_SENDER | PJ_LOG_HAS_TIME |
65 PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE;
66}
67
68PJ_DEF(void) pjsua_logging_config_dup(pj_pool_t *pool,
69 pjsua_logging_config *dst,
70 const pjsua_logging_config *src)
71{
72 pj_memcpy(dst, src, sizeof(*src));
73 pj_strdup_with_null(pool, &dst->log_filename, &src->log_filename);
74}
75
76PJ_DEF(void) pjsua_config_default(pjsua_config *cfg)
77{
78 pj_bzero(cfg, sizeof(*cfg));
79
80 cfg->max_calls = 4;
81 cfg->thread_cnt = 1;
82}
83
84PJ_DEF(void) pjsip_cred_dup( pj_pool_t *pool,
85 pjsip_cred_info *dst,
86 const pjsip_cred_info *src)
87{
88 pj_strdup_with_null(pool, &dst->realm, &src->realm);
89 pj_strdup_with_null(pool, &dst->scheme, &src->scheme);
90 pj_strdup_with_null(pool, &dst->username, &src->username);
91 pj_strdup_with_null(pool, &dst->data, &src->data);
92}
93
94PJ_DEF(void) pjsua_config_dup(pj_pool_t *pool,
95 pjsua_config *dst,
96 const pjsua_config *src)
97{
98 unsigned i;
99
100 pj_memcpy(dst, src, sizeof(*src));
101
102 for (i=0; i<src->outbound_proxy_cnt; ++i) {
103 pj_strdup_with_null(pool, &dst->outbound_proxy[i],
104 &src->outbound_proxy[i]);
105 }
106
107 for (i=0; i<src->cred_count; ++i) {
108 pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]);
109 }
110
111 pj_strdup_with_null(pool, &dst->user_agent, &src->user_agent);
112 pj_strdup_with_null(pool, &dst->stun_domain, &src->stun_domain);
113 pj_strdup_with_null(pool, &dst->stun_host, &src->stun_host);
114 pj_strdup_with_null(pool, &dst->stun_relay_host, &src->stun_relay_host);
115}
116
117PJ_DEF(void) pjsua_msg_data_init(pjsua_msg_data *msg_data)
118{
119 pj_bzero(msg_data, sizeof(*msg_data));
120 pj_list_init(&msg_data->hdr_list);
121}
122
123PJ_DEF(void) pjsua_transport_config_default(pjsua_transport_config *cfg)
124{
125 pj_bzero(cfg, sizeof(*cfg));
126 pjsip_tls_setting_default(&cfg->tls_setting);
127}
128
129PJ_DEF(void) pjsua_transport_config_dup(pj_pool_t *pool,
130 pjsua_transport_config *dst,
131 const pjsua_transport_config *src)
132{
133 PJ_UNUSED_ARG(pool);
134 pj_memcpy(dst, src, sizeof(*src));
135}
136
137PJ_DEF(void) pjsua_acc_config_default(pjsua_acc_config *cfg)
138{
139 pj_bzero(cfg, sizeof(*cfg));
140
141 cfg->reg_timeout = PJSUA_REG_INTERVAL;
142 cfg->transport_id = PJSUA_INVALID_ID;
Benny Prijono15b02302007-09-27 14:07:07 +0000143 cfg->auto_update_nat = PJ_TRUE;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000144}
145
146PJ_DEF(void) pjsua_buddy_config_default(pjsua_buddy_config *cfg)
147{
148 pj_bzero(cfg, sizeof(*cfg));
149}
150
151PJ_DEF(void) pjsua_media_config_default(pjsua_media_config *cfg)
152{
153 pj_bzero(cfg, sizeof(*cfg));
154
155 cfg->clock_rate = PJSUA_DEFAULT_CLOCK_RATE;
156 cfg->max_media_ports = 32;
157 cfg->has_ioqueue = PJ_TRUE;
158 cfg->thread_cnt = 1;
159 cfg->quality = PJSUA_DEFAULT_CODEC_QUALITY;
160 cfg->ilbc_mode = PJSUA_DEFAULT_ILBC_MODE;
161 cfg->ec_tail_len = PJSUA_DEFAULT_EC_TAIL_LEN;
162 cfg->jb_init = cfg->jb_min_pre = cfg->jb_max_pre = cfg->jb_max = -1;
163}
164
Benny Prijonodc39fe82006-05-26 12:17:46 +0000165
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000166/*****************************************************************************
167 * This is a very simple PJSIP module, whose sole purpose is to display
168 * incoming and outgoing messages to log. This module will have priority
169 * higher than transport layer, which means:
170 *
171 * - incoming messages will come to this module first before reaching
172 * transaction layer.
173 *
174 * - outgoing messages will come to this module last, after the message
175 * has been 'printed' to contiguous buffer by transport layer and
176 * appropriate transport instance has been decided for this message.
177 *
178 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000179
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000180/* Notification on incoming messages */
181static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
182{
Benny Prijonob4a17c92006-07-10 14:40:21 +0000183 PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
184 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000185 "--end msg--",
186 rdata->msg_info.len,
187 pjsip_rx_data_get_info(rdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000188 rdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000189 rdata->pkt_info.src_name,
190 rdata->pkt_info.src_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000191 (int)rdata->msg_info.len,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000192 rdata->msg_info.msg_buf));
193
194 /* Always return false, otherwise messages will not get processed! */
195 return PJ_FALSE;
196}
Benny Prijonodc39fe82006-05-26 12:17:46 +0000197
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000198/* Notification on outgoing messages */
199static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
200{
201
202 /* Important note:
203 * tp_info field is only valid after outgoing messages has passed
204 * transport layer. So don't try to access tp_info when the module
205 * has lower priority than transport layer.
206 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000207
Benny Prijonob4a17c92006-07-10 14:40:21 +0000208 PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
209 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000210 "--end msg--",
211 (tdata->buf.cur - tdata->buf.start),
212 pjsip_tx_data_get_info(tdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000213 tdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000214 tdata->tp_info.dst_name,
215 tdata->tp_info.dst_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000216 (int)(tdata->buf.cur - tdata->buf.start),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000217 tdata->buf.start));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000218
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000219 /* Always return success, otherwise message will not get sent! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000220 return PJ_SUCCESS;
221}
222
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000223/* The module instance. */
224static pjsip_module pjsua_msg_logger =
225{
226 NULL, NULL, /* prev, next. */
227 { "mod-pjsua-log", 13 }, /* Name. */
228 -1, /* Id */
229 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
230 NULL, /* load() */
231 NULL, /* start() */
232 NULL, /* stop() */
233 NULL, /* unload() */
234 &logging_on_rx_msg, /* on_rx_request() */
235 &logging_on_rx_msg, /* on_rx_response() */
236 &logging_on_tx_msg, /* on_tx_request. */
237 &logging_on_tx_msg, /* on_tx_response() */
238 NULL, /* on_tsx_state() */
239
240};
241
242
243/*****************************************************************************
Benny Prijono56315612006-07-18 14:39:40 +0000244 * Another simple module to handle incoming OPTIONS request
245 */
246
247/* Notification on incoming request */
248static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata)
249{
250 pjsip_tx_data *tdata;
251 pjsip_response_addr res_addr;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000252 pjmedia_sock_info skinfo;
Benny Prijono56315612006-07-18 14:39:40 +0000253 pjmedia_sdp_session *sdp;
254 const pjsip_hdr *cap_hdr;
255 pj_status_t status;
256
257 /* Only want to handle OPTIONS requests */
258 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000259 pjsip_get_options_method()) != 0)
Benny Prijono56315612006-07-18 14:39:40 +0000260 {
261 return PJ_FALSE;
262 }
263
264 /* Create basic response. */
265 status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL,
266 &tdata);
267 if (status != PJ_SUCCESS) {
268 pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status);
269 return PJ_TRUE;
270 }
271
272 /* Add Allow header */
273 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL);
274 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000275 pjsip_msg_add_hdr(tdata->msg,
276 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000277 }
278
279 /* Add Accept header */
280 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL);
281 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000282 pjsip_msg_add_hdr(tdata->msg,
283 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000284 }
285
286 /* Add Supported header */
287 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL);
288 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000289 pjsip_msg_add_hdr(tdata->msg,
290 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000291 }
292
293 /* Add Allow-Events header from the evsub module */
294 cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL);
295 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000296 pjsip_msg_add_hdr(tdata->msg,
297 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000298 }
299
300 /* Add User-Agent header */
301 if (pjsua_var.ua_cfg.user_agent.slen) {
302 const pj_str_t USER_AGENT = { "User-Agent", 10};
303 pjsip_hdr *h;
304
305 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
306 &USER_AGENT,
307 &pjsua_var.ua_cfg.user_agent);
308 pjsip_msg_add_hdr(tdata->msg, h);
309 }
310
Benny Prijono617c5bc2007-04-02 19:51:21 +0000311 /* Get media socket info */
312 pjmedia_transport_get_info(pjsua_var.calls[0].med_tp, &skinfo);
313
Benny Prijono56315612006-07-18 14:39:40 +0000314 /* Add SDP body, using call0's RTP address */
315 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000316 &skinfo, &sdp);
Benny Prijono56315612006-07-18 14:39:40 +0000317 if (status == PJ_SUCCESS) {
318 pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
319 }
320
321 /* Send response statelessly */
322 pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
323 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL);
324 if (status != PJ_SUCCESS)
325 pjsip_tx_data_dec_ref(tdata);
326
327 return PJ_TRUE;
328}
329
330
331/* The module instance. */
332static pjsip_module pjsua_options_handler =
333{
334 NULL, NULL, /* prev, next. */
335 { "mod-pjsua-options", 17 }, /* Name. */
336 -1, /* Id */
337 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
338 NULL, /* load() */
339 NULL, /* start() */
340 NULL, /* stop() */
341 NULL, /* unload() */
342 &options_on_rx_request, /* on_rx_request() */
343 NULL, /* on_rx_response() */
344 NULL, /* on_tx_request. */
345 NULL, /* on_tx_response() */
346 NULL, /* on_tsx_state() */
347
348};
349
350
351/*****************************************************************************
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000352 * These two functions are the main callbacks registered to PJSIP stack
353 * to receive SIP request and response messages that are outside any
354 * dialogs and any transactions.
355 */
Benny Prijono268ca612006-02-07 12:34:11 +0000356
357/*
358 * Handler for receiving incoming requests.
359 *
360 * This handler serves multiple purposes:
361 * - it receives requests outside dialogs.
362 * - it receives requests inside dialogs, when the requests are
363 * unhandled by other dialog usages. Example of these
364 * requests are: MESSAGE.
365 */
366static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
367{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000368 pj_bool_t processed = PJ_FALSE;
369
370 PJSUA_LOCK();
Benny Prijono38998232006-02-08 22:44:25 +0000371
Benny Prijono84126ab2006-02-09 09:30:09 +0000372 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000373
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000374 processed = pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000375 }
376
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000377 PJSUA_UNLOCK();
378
379 return processed;
Benny Prijono268ca612006-02-07 12:34:11 +0000380}
381
382
383/*
384 * Handler for receiving incoming responses.
385 *
386 * This handler serves multiple purposes:
387 * - it receives strayed responses (i.e. outside any dialog and
388 * outside any transactions).
389 * - it receives responses coming to a transaction, when pjsua
390 * module is set as transaction user for the transaction.
391 * - it receives responses inside a dialog, when these responses
392 * are unhandled by other dialog usages.
393 */
394static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
395{
396 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000397 return PJ_FALSE;
398}
399
400
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000401/*****************************************************************************
402 * Logging.
403 */
404
405/* Log callback */
406static void log_writer(int level, const char *buffer, int len)
Benny Prijono268ca612006-02-07 12:34:11 +0000407{
Benny Prijono572d4852006-11-23 21:50:02 +0000408 /* Write to file, stdout or application callback. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000409
410 if (pjsua_var.log_file) {
411 pj_ssize_t size = len;
412 pj_file_write(pjsua_var.log_file, buffer, &size);
Benny Prijono980ca0a2007-04-03 17:55:08 +0000413 /* This will slow things down considerably! Don't do it!
414 pj_file_flush(pjsua_var.log_file);
415 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000416 }
417
Benny Prijono572d4852006-11-23 21:50:02 +0000418 if (level <= (int)pjsua_var.log_cfg.console_level) {
419 if (pjsua_var.log_cfg.cb)
420 (*pjsua_var.log_cfg.cb)(level, buffer, len);
421 else
422 pj_log_write(level, buffer, len);
423 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000424}
425
426
427/*
428 * Application can call this function at any time (after pjsua_create(), of
429 * course) to change logging settings.
430 */
431PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg)
432{
433 pj_status_t status;
434
435 /* Save config. */
436 pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg);
437
438 /* Redirect log function to ours */
439 pj_log_set_log_func( &log_writer );
440
Benny Prijono9cb09a22007-08-30 09:35:10 +0000441 /* Set decor */
442 pj_log_set_decor(pjsua_var.log_cfg.decor);
443
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000444 /* Close existing file, if any */
445 if (pjsua_var.log_file) {
446 pj_file_close(pjsua_var.log_file);
447 pjsua_var.log_file = NULL;
448 }
449
450 /* If output log file is desired, create the file: */
451 if (pjsua_var.log_cfg.log_filename.slen) {
452
453 status = pj_file_open(pjsua_var.pool,
454 pjsua_var.log_cfg.log_filename.ptr,
455 PJ_O_WRONLY,
456 &pjsua_var.log_file);
457
458 if (status != PJ_SUCCESS) {
459 pjsua_perror(THIS_FILE, "Error creating log file", status);
460 return status;
461 }
462 }
463
464 /* Unregister msg logging if it's previously registered */
465 if (pjsua_msg_logger.id >= 0) {
466 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
467 pjsua_msg_logger.id = -1;
468 }
469
470 /* Enable SIP message logging */
471 if (pjsua_var.log_cfg.msg_logging)
472 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
473
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000474 return PJ_SUCCESS;
475}
476
477
478/*****************************************************************************
479 * PJSUA Base API.
480 */
481
482/* Worker thread function. */
483static int worker_thread(void *arg)
484{
485 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000486
Benny Prijono268ca612006-02-07 12:34:11 +0000487 PJ_UNUSED_ARG(arg);
488
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000489 while (!pjsua_var.thread_quit_flag) {
490 int count;
491
492 count = pjsua_handle_events(TIMEOUT);
493 if (count < 0)
494 pj_thread_sleep(TIMEOUT);
495 }
Benny Prijono268ca612006-02-07 12:34:11 +0000496
497 return 0;
498}
499
Benny Prijonodc39fe82006-05-26 12:17:46 +0000500
Benny Prijono268ca612006-02-07 12:34:11 +0000501/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000502 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000503 */
504PJ_DEF(pj_status_t) pjsua_create(void)
505{
506 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000507
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000508 /* Init pjsua data */
509 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000510
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000511 /* Set default logging settings */
512 pjsua_logging_config_default(&pjsua_var.log_cfg);
513
514 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000515 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000516 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
517
Benny Prijono268ca612006-02-07 12:34:11 +0000518
Benny Prijonofccab712006-02-22 22:23:22 +0000519 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000520 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000521 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000522
Benny Prijonoec921342007-03-24 13:00:30 +0000523 /* Init PJNATH */
524 status = pjnath_init();
525 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono268ca612006-02-07 12:34:11 +0000526
Benny Prijono094d3ad2006-11-21 08:41:00 +0000527 /* Set default sound device ID */
528 pjsua_var.cap_dev = pjsua_var.play_dev = -1;
529
Benny Prijono268ca612006-02-07 12:34:11 +0000530 /* Init caching pool. */
Benny Prijono106f5b72007-08-30 13:49:33 +0000531 pj_caching_pool_init(&pjsua_var.cp, NULL, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000532
533 /* Create memory pool for application. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000534 pjsua_var.pool = pjsua_pool_create("pjsua", 4000, 4000);
535
536 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000537
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000538 /* Create mutex */
539 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
540 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000541 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000542 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000543 return status;
544 }
545
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000546 /* Must create SIP endpoint to initialize SIP parser. The parser
547 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000548 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000549 status = pjsip_endpt_create(&pjsua_var.cp.factory,
550 pj_gethostname()->ptr,
551 &pjsua_var.endpt);
552 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000553
554
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000555 return PJ_SUCCESS;
556}
557
558
559/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000560 * Initialize pjsua with the specified settings. All the settings are
561 * optional, and the default values will be used when the config is not
562 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000563 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000564PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
565 const pjsua_logging_config *log_cfg,
566 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000567{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000568 pjsua_config default_cfg;
569 pjsua_media_config default_media_cfg;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000570 const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
Benny Prijonodc39fe82006-05-26 12:17:46 +0000571 pj_status_t status;
572
573
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000574 /* Create default configurations when the config is not supplied */
575
576 if (ua_cfg == NULL) {
577 pjsua_config_default(&default_cfg);
578 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000579 }
580
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000581 if (media_cfg == NULL) {
582 pjsua_media_config_default(&default_media_cfg);
583 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000584 }
585
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000586 /* Initialize logging first so that info/errors can be captured */
587 if (log_cfg) {
588 status = pjsua_reconfigure_logging(log_cfg);
589 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono9fc735d2006-05-28 14:58:12 +0000590 }
591
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000592 /* If nameserver is configured, create DNS resolver instance and
593 * set it to be used by SIP resolver.
594 */
595 if (ua_cfg->nameserver_count) {
596#if PJSIP_HAS_RESOLVER
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000597 unsigned i;
598
599 /* Create DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000600 status = pjsip_endpt_create_resolver(pjsua_var.endpt,
601 &pjsua_var.resolver);
Benny Prijono318f3842007-05-15 20:27:08 +0000602 if (status != PJ_SUCCESS) {
603 pjsua_perror(THIS_FILE, "Error creating resolver", status);
604 return status;
605 }
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000606
607 /* Configure nameserver for the DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000608 status = pj_dns_resolver_set_ns(pjsua_var.resolver,
609 ua_cfg->nameserver_count,
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000610 ua_cfg->nameserver, NULL);
611 if (status != PJ_SUCCESS) {
612 pjsua_perror(THIS_FILE, "Error setting nameserver", status);
613 return status;
614 }
615
616 /* Set this DNS resolver to be used by the SIP resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000617 status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000618 if (status != PJ_SUCCESS) {
619 pjsua_perror(THIS_FILE, "Error setting DNS resolver", status);
620 return status;
621 }
622
623 /* Print nameservers */
624 for (i=0; i<ua_cfg->nameserver_count; ++i) {
625 PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added",
626 (int)ua_cfg->nameserver[i].slen,
627 ua_cfg->nameserver[i].ptr));
628 }
629#else
630 PJ_LOG(2,(THIS_FILE,
631 "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)"));
632#endif
633 }
634
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000635 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000636
637 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000638 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
639 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000640
Benny Prijonodc39fe82006-05-26 12:17:46 +0000641
642 /* Initialize UA layer module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000643 status = pjsip_ua_init_module( pjsua_var.endpt, NULL );
644 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000645
Benny Prijonodc39fe82006-05-26 12:17:46 +0000646
Benny Prijono053f5222006-11-11 16:16:04 +0000647 /* Initialize Replaces support. */
648 status = pjsip_replaces_init_module( pjsua_var.endpt );
649 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
650
651
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000652 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000653 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000654 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000655 {
656 NULL, NULL, /* prev, next. */
657 { "mod-pjsua", 9 }, /* Name. */
658 -1, /* Id */
659 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
660 NULL, /* load() */
661 NULL, /* start() */
662 NULL, /* stop() */
663 NULL, /* unload() */
664 &mod_pjsua_on_rx_request, /* on_rx_request() */
665 &mod_pjsua_on_rx_response, /* on_rx_response() */
666 NULL, /* on_tx_request. */
667 NULL, /* on_tx_response() */
668 NULL, /* on_tsx_state() */
669 };
670
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000671 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000672
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000673 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
674 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000675 }
676
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000677
Benny Prijonodc39fe82006-05-26 12:17:46 +0000678
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000679 /* Initialize PJSUA call subsystem: */
680 status = pjsua_call_subsys_init(ua_cfg);
681 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000682 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000683
684
Benny Prijonoc97608e2007-03-23 16:34:20 +0000685 /* Start resolving STUN server */
686 status = pjsua_resolve_stun_server(PJ_FALSE);
687 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
688 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
689 return status;
690 }
691
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000692 /* Initialize PJSUA media subsystem */
693 status = pjsua_media_subsys_init(media_cfg);
694 if (status != PJ_SUCCESS)
695 goto on_error;
696
Benny Prijonodc39fe82006-05-26 12:17:46 +0000697
698 /* Init core SIMPLE module : */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000699 status = pjsip_evsub_init_module(pjsua_var.endpt);
700 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000701
Benny Prijonodc39fe82006-05-26 12:17:46 +0000702
703 /* Init presence module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000704 status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance());
705 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000706
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000707 /* Init PUBLISH module */
708 pjsip_publishc_init_module(pjsua_var.endpt);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000709
710 /* Init xfer/REFER module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000711 status = pjsip_xfer_init_module( pjsua_var.endpt );
712 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000713
714 /* Init pjsua presence handler: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000715 status = pjsua_pres_init();
716 if (status != PJ_SUCCESS)
717 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000718
719 /* Init out-of-dialog MESSAGE request handler. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000720 status = pjsua_im_init();
721 if (status != PJ_SUCCESS)
722 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000723
Benny Prijonoc8141a82006-08-20 09:12:19 +0000724 /* Register OPTIONS handler */
725 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler);
726
727 /* Add OPTIONS in Allow header */
728 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
729 NULL, 1, &STR_OPTIONS);
730
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000731 /* Start worker thread if needed. */
732 if (pjsua_var.ua_cfg.thread_cnt) {
733 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000734
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000735 if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread))
736 pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000737
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000738 for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) {
739 status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread,
740 NULL, 0, 0, &pjsua_var.thread[i]);
741 if (status != PJ_SUCCESS)
742 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000743 }
Benny Prijonof521eb02006-08-06 23:07:25 +0000744 PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created",
745 pjsua_var.ua_cfg.thread_cnt));
746 } else {
747 PJ_LOG(4,(THIS_FILE, "No SIP worker threads created"));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000748 }
749
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000750 /* Done! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000751
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000752 PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized",
Benny Prijono106f5b72007-08-30 13:49:33 +0000753 pj_get_version(), PJ_OS_NAME));
Benny Prijonoccf95622006-02-07 18:48:01 +0000754
Benny Prijono268ca612006-02-07 12:34:11 +0000755 return PJ_SUCCESS;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000756
757on_error:
758 pjsua_destroy();
759 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000760}
761
762
Benny Prijono834aee32006-02-19 01:38:06 +0000763/* Sleep with polling */
764static void busy_sleep(unsigned msec)
765{
Benny Prijonob2c96822007-05-03 13:31:21 +0000766#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
Benny Prijono897f9f82007-05-03 19:56:21 +0000767 /* Ideally we shouldn't call pj_thread_sleep() and rather
768 * CActiveScheduler::WaitForAnyRequest() here, but that will
769 * drag in Symbian header and it doesn't look pretty.
770 */
Benny Prijonob2c96822007-05-03 13:31:21 +0000771 pj_thread_sleep(msec);
772#else
Benny Prijono834aee32006-02-19 01:38:06 +0000773 pj_time_val timeout, now;
774
775 pj_gettimeofday(&timeout);
776 timeout.msec += msec;
777 pj_time_val_normalize(&timeout);
778
779 do {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000780 while (pjsua_handle_events(10) > 0)
781 ;
Benny Prijono834aee32006-02-19 01:38:06 +0000782 pj_gettimeofday(&now);
783 } while (PJ_TIME_VAL_LT(now, timeout));
Benny Prijonob2c96822007-05-03 13:31:21 +0000784#endif
Benny Prijono834aee32006-02-19 01:38:06 +0000785}
786
Benny Prijonoebbf6892007-03-24 17:37:25 +0000787
Benny Prijonoebbf6892007-03-24 17:37:25 +0000788/*
789 * Callback function to receive notification from the resolver
790 * when the resolution process completes.
791 */
792static void stun_dns_srv_resolver_cb(void *user_data,
793 pj_status_t status,
794 const pj_dns_srv_record *rec)
795{
796 unsigned i;
797
798 PJ_UNUSED_ARG(user_data);
799
800 pjsua_var.stun_status = status;
801
802 if (status != PJ_SUCCESS) {
803 /* DNS SRV resolution failed. If stun_host is specified, resolve
804 * it with gethostbyname()
805 */
806 if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000807 pj_str_t str_host, str_port;
808 int port;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000809 pj_hostent he;
810
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000811 str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':');
812 if (str_port.ptr != NULL) {
813 str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr;
814 str_host.slen = (str_port.ptr - str_host.ptr);
815 str_port.ptr++;
816 str_port.slen = pjsua_var.ua_cfg.stun_host.slen -
817 str_host.slen - 1;
818 port = (int)pj_strtoul(&str_port);
819 if (port < 1 || port > 65535) {
820 pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL);
821 pjsua_var.stun_status = PJ_EINVAL;
822 return;
823 }
824 } else {
825 str_host = pjsua_var.ua_cfg.stun_host;
826 port = 3478;
827 }
828
829 pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
Benny Prijonoebbf6892007-03-24 17:37:25 +0000830
831 if (pjsua_var.stun_status == PJ_SUCCESS) {
832 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
833 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000834 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoebbf6892007-03-24 17:37:25 +0000835
836 PJ_LOG(3,(THIS_FILE,
837 "STUN server %.*s resolved, address is %s:%d",
838 (int)pjsua_var.ua_cfg.stun_host.slen,
839 pjsua_var.ua_cfg.stun_host.ptr,
840 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
841 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
842 }
843 } else {
844 char errmsg[PJ_ERR_MSG_SIZE];
845
846 pj_strerror(status, errmsg, sizeof(errmsg));
847 PJ_LOG(1,(THIS_FILE,
848 "DNS SRV resolution failed for STUN server %.*s: %s",
849 (int)pjsua_var.ua_cfg.stun_domain.slen,
850 pjsua_var.ua_cfg.stun_domain.ptr,
851 errmsg));
852 }
853 return;
854 }
855
Benny Prijonof5afa922007-06-11 16:51:18 +0000856 pj_assert(rec->count != 0 && rec->entry[0].server.addr_count != 0);
857 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL,
858 rec->entry[0].port);
859 pjsua_var.stun_srv.ipv4.sin_addr.s_addr =
860 rec->entry[0].server.addr[0].s_addr;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000861
862 PJ_LOG(3,(THIS_FILE, "_stun._udp.%.*s resolved, found %d entry(s):",
863 (int)pjsua_var.ua_cfg.stun_domain.slen,
864 pjsua_var.ua_cfg.stun_domain.ptr,
865 rec->count));
866
867 for (i=0; i<rec->count; ++i) {
868 PJ_LOG(3,(THIS_FILE,
869 " %d: prio=%d, weight=%d %s:%d",
870 i, rec->entry[i].priority, rec->entry[i].weight,
Benny Prijonof5afa922007-06-11 16:51:18 +0000871 pj_inet_ntoa(rec->entry[i].server.addr[0]),
872 (int)rec->entry[i].port));
Benny Prijonoebbf6892007-03-24 17:37:25 +0000873 }
874
875}
876
Benny Prijono268ca612006-02-07 12:34:11 +0000877/*
Benny Prijonoc97608e2007-03-23 16:34:20 +0000878 * Resolve STUN server.
879 */
880pj_status_t pjsua_resolve_stun_server(pj_bool_t wait)
881{
882 if (pjsua_var.stun_status == PJ_EUNKNOWN) {
883 /* Initialize STUN configuration */
884 pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0,
885 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
886 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
887
888 /* Start STUN server resolution */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000889
890 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000891
Benny Prijonoebbf6892007-03-24 17:37:25 +0000892 /* If stun_domain is specified, resolve STUN servers with DNS
893 * SRV resolution.
894 */
895 if (pjsua_var.ua_cfg.stun_domain.slen) {
896 pj_str_t res_type;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000897 pj_status_t status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000898
899 /* Fail if resolver is not configured */
900 if (pjsua_var.resolver == NULL) {
901 PJ_LOG(1,(THIS_FILE, "Nameserver must be configured when "
902 "stun_domain is specified"));
903 pjsua_var.stun_status = PJLIB_UTIL_EDNSNONS;
904 return PJLIB_UTIL_EDNSNONS;
905 }
906 res_type = pj_str("_stun._udp");
Benny Prijonoda9785b2007-04-02 20:43:06 +0000907 status =
Benny Prijonoebbf6892007-03-24 17:37:25 +0000908 pj_dns_srv_resolve(&pjsua_var.ua_cfg.stun_domain, &res_type,
909 3478, pjsua_var.pool, pjsua_var.resolver,
Benny Prijonof5afa922007-06-11 16:51:18 +0000910 0, NULL, &stun_dns_srv_resolver_cb, NULL);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000911 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +0000912 pjsua_perror(THIS_FILE, "Error starting DNS SRV resolution",
913 pjsua_var.stun_status);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000914 pjsua_var.stun_status = status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000915 return pjsua_var.stun_status;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000916 } else {
917 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000918 }
919 }
920 /* Otherwise if stun_host is specified, resolve STUN server with
921 * gethostbyname().
922 */
923 else if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000924 pj_str_t str_host, str_port;
925 int port;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000926 pj_hostent he;
927
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000928 str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':');
929 if (str_port.ptr != NULL) {
930 str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr;
931 str_host.slen = (str_port.ptr - str_host.ptr);
932 str_port.ptr++;
933 str_port.slen = pjsua_var.ua_cfg.stun_host.slen -
934 str_host.slen - 1;
935 port = (int)pj_strtoul(&str_port);
936 if (port < 1 || port > 65535) {
937 pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL);
938 pjsua_var.stun_status = PJ_EINVAL;
939 return pjsua_var.stun_status;
940 }
941 } else {
942 str_host = pjsua_var.ua_cfg.stun_host;
943 port = 3478;
944 }
945
946
947 pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000948
949 if (pjsua_var.stun_status == PJ_SUCCESS) {
950 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
951 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000952 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonof75eceb2007-03-23 19:09:54 +0000953
Benny Prijonoebbf6892007-03-24 17:37:25 +0000954 PJ_LOG(3,(THIS_FILE,
Benny Prijonof75eceb2007-03-23 19:09:54 +0000955 "STUN server %.*s resolved, address is %s:%d",
Benny Prijonoebbf6892007-03-24 17:37:25 +0000956 (int)pjsua_var.ua_cfg.stun_host.slen,
957 pjsua_var.ua_cfg.stun_host.ptr,
Benny Prijonof75eceb2007-03-23 19:09:54 +0000958 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
959 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
Benny Prijonoc97608e2007-03-23 16:34:20 +0000960 }
Benny Prijonoebbf6892007-03-24 17:37:25 +0000961
Benny Prijonoc97608e2007-03-23 16:34:20 +0000962 }
Benny Prijonoebbf6892007-03-24 17:37:25 +0000963 /* Otherwise disable STUN. */
964 else {
965 pjsua_var.stun_status = PJ_SUCCESS;
966 }
967
968
Benny Prijonoc97608e2007-03-23 16:34:20 +0000969 return pjsua_var.stun_status;
970
971 } else if (pjsua_var.stun_status == PJ_EPENDING) {
972 /* STUN server resolution has been started, wait for the
973 * result.
974 */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000975 if (wait) {
976 while (pjsua_var.stun_status == PJ_EPENDING)
977 pjsua_handle_events(10);
978 }
979
980 return pjsua_var.stun_status;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000981
982 } else {
983 /* STUN server has been resolved, return the status */
984 return pjsua_var.stun_status;
985 }
986}
987
988/*
Benny Prijono268ca612006-02-07 12:34:11 +0000989 * Destroy pjsua.
990 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000991PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +0000992{
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000993 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +0000994
995 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000996 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +0000997
998 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000999 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
1000 if (pjsua_var.thread[i]) {
1001 pj_thread_join(pjsua_var.thread[i]);
1002 pj_thread_destroy(pjsua_var.thread[i]);
1003 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +00001004 }
Benny Prijono268ca612006-02-07 12:34:11 +00001005 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001006
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001007 if (pjsua_var.endpt) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001008 /* Terminate all calls. */
1009 pjsua_call_hangup_all();
1010
1011 /* Terminate all presence subscriptions. */
1012 pjsua_pres_shutdown();
1013
1014 /* Unregister, if required: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001015 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1016 if (!pjsua_var.acc[i].valid)
1017 continue;
1018
1019 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001020 pjsua_acc_set_registration(i, PJ_FALSE);
1021 }
1022 }
1023
1024 /* Wait for some time to allow unregistration to complete: */
Benny Prijono9fc735d2006-05-28 14:58:12 +00001025 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
1026 busy_sleep(1000);
1027 }
Benny Prijono834aee32006-02-19 01:38:06 +00001028
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001029 /* Destroy media */
1030 pjsua_media_subsys_destroy();
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001031
Benny Prijono268ca612006-02-07 12:34:11 +00001032 /* Destroy endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001033 if (pjsua_var.endpt) {
1034 pjsip_endpt_destroy(pjsua_var.endpt);
1035 pjsua_var.endpt = NULL;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001036 }
Benny Prijono268ca612006-02-07 12:34:11 +00001037
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001038 /* Destroy mutex */
1039 if (pjsua_var.mutex) {
1040 pj_mutex_destroy(pjsua_var.mutex);
1041 pjsua_var.mutex = NULL;
1042 }
1043
1044 /* Destroy pool and pool factory. */
1045 if (pjsua_var.pool) {
1046 pj_pool_release(pjsua_var.pool);
1047 pjsua_var.pool = NULL;
1048 pj_caching_pool_destroy(&pjsua_var.cp);
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00001049
1050 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
1051
1052 /* End logging */
1053 if (pjsua_var.log_file) {
1054 pj_file_close(pjsua_var.log_file);
1055 pjsua_var.log_file = NULL;
1056 }
1057
1058 /* Shutdown PJLIB */
1059 pj_shutdown();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001060 }
Benny Prijono268ca612006-02-07 12:34:11 +00001061
Benny Prijonof762ee72006-12-01 11:14:37 +00001062 /* Clear pjsua_var */
1063 pj_bzero(&pjsua_var, sizeof(pjsua_var));
1064
Benny Prijono268ca612006-02-07 12:34:11 +00001065 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001066 return PJ_SUCCESS;
1067}
1068
1069
1070/**
1071 * Application is recommended to call this function after all initialization
1072 * is done, so that the library can do additional checking set up
1073 * additional
1074 *
1075 * @return PJ_SUCCESS on success, or the appropriate error code.
1076 */
1077PJ_DEF(pj_status_t) pjsua_start(void)
1078{
1079 pj_status_t status;
1080
1081 status = pjsua_call_subsys_start();
1082 if (status != PJ_SUCCESS)
1083 return status;
1084
1085 status = pjsua_media_subsys_start();
1086 if (status != PJ_SUCCESS)
1087 return status;
1088
1089 status = pjsua_pres_start();
1090 if (status != PJ_SUCCESS)
1091 return status;
Benny Prijono268ca612006-02-07 12:34:11 +00001092
1093 return PJ_SUCCESS;
1094}
1095
Benny Prijono9fc735d2006-05-28 14:58:12 +00001096
1097/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001098 * Poll pjsua for events, and if necessary block the caller thread for
1099 * the specified maximum interval (in miliseconds).
1100 */
1101PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
1102{
Benny Prijono897f9f82007-05-03 19:56:21 +00001103#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
1104 /* Ideally we shouldn't call pj_thread_sleep() and rather
1105 * CActiveScheduler::WaitForAnyRequest() here, but that will
1106 * drag in Symbian header and it doesn't look pretty.
1107 */
1108 pj_thread_sleep(msec_timeout);
1109 return msec_timeout;
1110#else
1111
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001112 unsigned count = 0;
1113 pj_time_val tv;
1114 pj_status_t status;
1115
1116 tv.sec = 0;
1117 tv.msec = msec_timeout;
1118 pj_time_val_normalize(&tv);
1119
1120 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
1121
1122 if (status != PJ_SUCCESS)
1123 return -status;
1124
1125 return count;
Benny Prijono897f9f82007-05-03 19:56:21 +00001126#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001127}
1128
1129
1130/*
1131 * Create memory pool.
1132 */
1133PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
1134 pj_size_t increment)
1135{
1136 /* Pool factory is thread safe, no need to lock */
1137 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
1138 NULL);
1139}
1140
1141
1142/*
1143 * Internal function to get SIP endpoint instance of pjsua, which is
1144 * needed for example to register module, create transports, etc.
1145 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001146 */
1147PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
1148{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001149 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001150}
1151
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001152/*
1153 * Internal function to get media endpoint instance.
1154 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001155 */
1156PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
1157{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001158 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001159}
1160
Benny Prijono97b87172006-08-24 14:25:14 +00001161/*
1162 * Internal function to get PJSUA pool factory.
1163 */
1164PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
1165{
1166 return &pjsua_var.cp.factory;
1167}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001168
1169/*****************************************************************************
1170 * PJSUA SIP Transport API.
1171 */
1172
1173/*
1174 * Create and initialize SIP socket (and possibly resolve public
1175 * address via STUN, depending on config).
1176 */
1177static pj_status_t create_sip_udp_sock(pj_in_addr bound_addr,
1178 int port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001179 pj_sock_t *p_sock,
1180 pj_sockaddr_in *p_pub_addr)
1181{
Benny Prijonoc97608e2007-03-23 16:34:20 +00001182 char ip_addr[32];
1183 pj_str_t stun_srv;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001184 pj_sock_t sock;
1185 pj_status_t status;
1186
Benny Prijonoc97608e2007-03-23 16:34:20 +00001187 /* Make sure STUN server resolution has completed */
1188 status = pjsua_resolve_stun_server(PJ_TRUE);
1189 if (status != PJ_SUCCESS) {
1190 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
1191 return status;
1192 }
1193
Benny Prijono8ab968f2007-07-20 08:08:30 +00001194 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001195 if (status != PJ_SUCCESS) {
1196 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +00001197 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001198 }
1199
Benny Prijonof90ef4f2007-02-12 14:59:57 +00001200 status = pj_sock_bind_in(sock, pj_ntohl(bound_addr.s_addr),
1201 (pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001202 if (status != PJ_SUCCESS) {
1203 pjsua_perror(THIS_FILE, "bind() error", status);
1204 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001205 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001206 }
1207
Benny Prijonoe347cb02007-02-14 14:36:13 +00001208 /* If port is zero, get the bound port */
1209 if (port == 0) {
1210 pj_sockaddr_in bound_addr;
1211 int namelen = sizeof(bound_addr);
1212 status = pj_sock_getsockname(sock, &bound_addr, &namelen);
1213 if (status != PJ_SUCCESS) {
1214 pjsua_perror(THIS_FILE, "getsockname() error", status);
1215 pj_sock_close(sock);
1216 return status;
1217 }
1218
1219 port = pj_ntohs(bound_addr.sin_port);
1220 }
1221
Benny Prijonoc97608e2007-03-23 16:34:20 +00001222 if (pjsua_var.stun_srv.addr.sa_family != 0) {
1223 pj_ansi_strcpy(ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
1224 stun_srv = pj_str(ip_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001225 } else {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001226 stun_srv.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001227 }
1228
1229 /* Get the published address, either by STUN or by resolving
1230 * the name of local host.
1231 */
Benny Prijonoc97608e2007-03-23 16:34:20 +00001232 if (stun_srv.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +00001233 /*
1234 * STUN is specified, resolve the address with STUN.
1235 */
Benny Prijono14c2b862007-02-21 00:40:05 +00001236 status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001237 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
1238 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijonoc97608e2007-03-23 16:34:20 +00001239 p_pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001240 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +00001241 pjsua_perror(THIS_FILE, "Error contacting STUN server", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001242 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001243 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001244 }
1245
Benny Prijono0a5cad82006-09-26 13:21:02 +00001246 } else if (p_pub_addr->sin_addr.s_addr != 0) {
1247 /*
1248 * Public address is already specified, no need to resolve the
1249 * address, only set the port.
1250 */
Benny Prijonoe347cb02007-02-14 14:36:13 +00001251 if (p_pub_addr->sin_port == 0)
1252 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001253
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001254 } else {
1255
Benny Prijono594e4c52006-09-14 18:51:01 +00001256 pj_bzero(p_pub_addr, sizeof(pj_sockaddr_in));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001257
Benny Prijono594e4c52006-09-14 18:51:01 +00001258 status = pj_gethostip(&p_pub_addr->sin_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001259 if (status != PJ_SUCCESS) {
Benny Prijonob43bad72007-01-20 05:11:08 +00001260 pjsua_perror(THIS_FILE, "Unable to get local host IP", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001261 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001262 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001263 }
1264
Benny Prijono8ab968f2007-07-20 08:08:30 +00001265 p_pub_addr->sin_family = pj_AF_INET();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001266 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001267 }
1268
1269 *p_sock = sock;
1270
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001271 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
1272 pj_inet_ntoa(p_pub_addr->sin_addr),
1273 (int)pj_ntohs(p_pub_addr->sin_port)));
1274
Benny Prijonod8410532006-06-15 11:04:33 +00001275 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001276}
1277
1278
1279/*
1280 * Create SIP transport.
1281 */
1282PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
1283 const pjsua_transport_config *cfg,
1284 pjsua_transport_id *p_id)
1285{
1286 pjsip_transport *tp;
1287 unsigned id;
1288 pj_status_t status;
1289
1290 PJSUA_LOCK();
1291
1292 /* Find empty transport slot */
1293 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001294 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001295 break;
1296 }
1297
1298 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1299 status = PJ_ETOOMANY;
1300 pjsua_perror(THIS_FILE, "Error creating transport", status);
1301 goto on_return;
1302 }
1303
1304 /* Create the transport */
1305 if (type == PJSIP_TRANSPORT_UDP) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001306 /*
1307 * Create UDP transport.
1308 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001309 pjsua_transport_config config;
Benny Prijonod8410532006-06-15 11:04:33 +00001310 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001311 pj_sockaddr_in bound_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001312 pj_sockaddr_in pub_addr;
1313 pjsip_host_port addr_name;
1314
1315 /* Supply default config if it's not specified */
1316 if (cfg == NULL) {
1317 pjsua_transport_config_default(&config);
1318 cfg = &config;
1319 }
1320
Benny Prijono0a5cad82006-09-26 13:21:02 +00001321 /* Initialize bound address, if any */
1322 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
1323 if (cfg->bound_addr.slen) {
1324 status = pj_sockaddr_in_set_str_addr(&bound_addr,&cfg->bound_addr);
1325 if (status != PJ_SUCCESS) {
1326 pjsua_perror(THIS_FILE,
1327 "Unable to resolve transport bound address",
1328 status);
1329 goto on_return;
1330 }
1331 }
1332
1333 /* Initialize the public address from the config, if any */
1334 pj_sockaddr_in_init(&pub_addr, NULL, (pj_uint16_t)cfg->port);
1335 if (cfg->public_addr.slen) {
1336 status = pj_sockaddr_in_set_str_addr(&pub_addr, &cfg->public_addr);
1337 if (status != PJ_SUCCESS) {
1338 pjsua_perror(THIS_FILE,
1339 "Unable to resolve transport public address",
1340 status);
1341 goto on_return;
1342 }
1343 }
1344
1345 /* Create the socket and possibly resolve the address with STUN
1346 * (only when public address is not specified).
1347 */
1348 status = create_sip_udp_sock(bound_addr.sin_addr, cfg->port,
Benny Prijono0a5cad82006-09-26 13:21:02 +00001349 &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001350 if (status != PJ_SUCCESS)
1351 goto on_return;
1352
1353 addr_name.host = pj_str(pj_inet_ntoa(pub_addr.sin_addr));
1354 addr_name.port = pj_ntohs(pub_addr.sin_port);
1355
1356 /* Create UDP transport */
1357 status = pjsip_udp_transport_attach( pjsua_var.endpt, sock,
1358 &addr_name, 1,
1359 &tp);
1360 if (status != PJ_SUCCESS) {
1361 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
1362 status);
1363 pj_sock_close(sock);
1364 goto on_return;
1365 }
1366
Benny Prijonoe93e2872006-06-28 16:46:49 +00001367
1368 /* Save the transport */
1369 pjsua_var.tpdata[id].type = type;
1370 pjsua_var.tpdata[id].local_name = tp->local_name;
1371 pjsua_var.tpdata[id].data.tp = tp;
1372
Benny Prijono3569c0d2007-04-06 10:29:20 +00001373#if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0
1374
Benny Prijonoe93e2872006-06-28 16:46:49 +00001375 } else if (type == PJSIP_TRANSPORT_TCP) {
1376 /*
1377 * Create TCP transport.
1378 */
1379 pjsua_transport_config config;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001380 pjsip_host_port a_name;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001381 pjsip_tpfactory *tcp;
1382 pj_sockaddr_in local_addr;
1383
1384 /* Supply default config if it's not specified */
1385 if (cfg == NULL) {
1386 pjsua_transport_config_default(&config);
1387 cfg = &config;
1388 }
1389
1390 /* Init local address */
1391 pj_sockaddr_in_init(&local_addr, 0, 0);
1392
1393 if (cfg->port)
1394 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1395
Benny Prijono0a5cad82006-09-26 13:21:02 +00001396 if (cfg->bound_addr.slen) {
1397 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1398 if (status != PJ_SUCCESS) {
1399 pjsua_perror(THIS_FILE,
1400 "Unable to resolve transport bound address",
1401 status);
1402 goto on_return;
1403 }
1404 }
1405
1406 /* Init published name */
1407 pj_bzero(&a_name, sizeof(pjsip_host_port));
1408 if (cfg->public_addr.slen)
1409 a_name.host = cfg->public_addr;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001410
1411 /* Create the TCP transport */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001412 status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr,
1413 &a_name, 1, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001414
1415 if (status != PJ_SUCCESS) {
1416 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1417 status);
1418 goto on_return;
1419 }
1420
1421 /* Save the transport */
1422 pjsua_var.tpdata[id].type = type;
1423 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1424 pjsua_var.tpdata[id].data.factory = tcp;
1425
Benny Prijono3569c0d2007-04-06 10:29:20 +00001426#endif /* PJ_HAS_TCP */
1427
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001428#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1429 } else if (type == PJSIP_TRANSPORT_TLS) {
1430 /*
1431 * Create TLS transport.
1432 */
Benny Prijonof3bbc132006-12-25 06:43:59 +00001433 /*
1434 * Create TCP transport.
1435 */
1436 pjsua_transport_config config;
1437 pjsip_host_port a_name;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001438 pjsip_tpfactory *tls;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001439 pj_sockaddr_in local_addr;
1440
1441 /* Supply default config if it's not specified */
1442 if (cfg == NULL) {
1443 pjsua_transport_config_default(&config);
1444 config.port = 5061;
1445 cfg = &config;
1446 }
1447
1448 /* Init local address */
1449 pj_sockaddr_in_init(&local_addr, 0, 0);
1450
1451 if (cfg->port)
1452 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1453
1454 if (cfg->bound_addr.slen) {
1455 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1456 if (status != PJ_SUCCESS) {
1457 pjsua_perror(THIS_FILE,
1458 "Unable to resolve transport bound address",
1459 status);
1460 goto on_return;
1461 }
1462 }
1463
1464 /* Init published name */
1465 pj_bzero(&a_name, sizeof(pjsip_host_port));
1466 if (cfg->public_addr.slen)
1467 a_name.host = cfg->public_addr;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001468
1469 status = pjsip_tls_transport_start(pjsua_var.endpt,
Benny Prijonof3bbc132006-12-25 06:43:59 +00001470 &cfg->tls_setting,
1471 &local_addr, &a_name, 1, &tls);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001472 if (status != PJ_SUCCESS) {
1473 pjsua_perror(THIS_FILE, "Error creating SIP TLS listener",
1474 status);
1475 goto on_return;
1476 }
1477
1478 /* Save the transport */
1479 pjsua_var.tpdata[id].type = type;
1480 pjsua_var.tpdata[id].local_name = tls->addr_name;
1481 pjsua_var.tpdata[id].data.factory = tls;
1482#endif
1483
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001484 } else {
1485 status = PJSIP_EUNSUPTRANSPORT;
1486 pjsua_perror(THIS_FILE, "Error creating transport", status);
1487 goto on_return;
1488 }
1489
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001490
1491 /* Return the ID */
1492 if (p_id) *p_id = id;
1493
1494 status = PJ_SUCCESS;
1495
1496on_return:
1497
1498 PJSUA_UNLOCK();
1499
Benny Prijonod8410532006-06-15 11:04:33 +00001500 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001501}
1502
1503
1504/*
1505 * Register transport that has been created by application.
1506 */
1507PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1508 pjsua_transport_id *p_id)
1509{
1510 unsigned id;
1511
1512 PJSUA_LOCK();
1513
1514 /* Find empty transport slot */
1515 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001516 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001517 break;
1518 }
1519
1520 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1521 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1522 PJSUA_UNLOCK();
1523 return PJ_ETOOMANY;
1524 }
1525
1526 /* Save the transport */
Benny Prijonod17a5e92007-05-29 11:51:45 +00001527 pjsua_var.tpdata[id].type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001528 pjsua_var.tpdata[id].local_name = tp->local_name;
1529 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001530
1531 /* Return the ID */
1532 if (p_id) *p_id = id;
1533
1534 PJSUA_UNLOCK();
1535
1536 return PJ_SUCCESS;
1537}
1538
1539
1540/*
1541 * Enumerate all transports currently created in the system.
1542 */
1543PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1544 unsigned *p_count )
1545{
1546 unsigned i, count;
1547
1548 PJSUA_LOCK();
1549
1550 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
1551 ++i)
1552 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001553 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001554 continue;
1555
1556 id[count++] = i;
1557 }
1558
1559 *p_count = count;
1560
1561 PJSUA_UNLOCK();
1562
1563 return PJ_SUCCESS;
1564}
1565
1566
1567/*
1568 * Get information about transports.
1569 */
1570PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
1571 pjsua_transport_info *info)
1572{
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001573 pjsua_transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00001574 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001575
Benny Prijonoac623b32006-07-03 15:19:31 +00001576 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001577
1578 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001579 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1580 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001581
1582 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001583 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001584
1585 PJSUA_LOCK();
1586
Benny Prijonoe93e2872006-06-28 16:46:49 +00001587 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
1588
1589 pjsip_transport *tp = t->data.tp;
1590
1591 if (tp == NULL) {
1592 PJSUA_UNLOCK();
1593 return PJ_EINVALIDOP;
1594 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001595
Benny Prijonoe93e2872006-06-28 16:46:49 +00001596 info->id = id;
Benny Prijonod17a5e92007-05-29 11:51:45 +00001597 info->type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001598 info->type_name = pj_str(tp->type_name);
1599 info->info = pj_str(tp->info);
1600 info->flag = tp->flag;
1601 info->addr_len = tp->addr_len;
1602 info->local_addr = tp->local_addr;
1603 info->local_name = tp->local_name;
1604 info->usage_count = pj_atomic_get(tp->ref_cnt);
1605
Benny Prijono0a5cad82006-09-26 13:21:02 +00001606 status = PJ_SUCCESS;
1607
Benny Prijonoe93e2872006-06-28 16:46:49 +00001608 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
1609
1610 pjsip_tpfactory *factory = t->data.factory;
1611
1612 if (factory == NULL) {
1613 PJSUA_UNLOCK();
1614 return PJ_EINVALIDOP;
1615 }
1616
1617 info->id = id;
1618 info->type = t->type;
1619 info->type_name = pj_str("TCP");
1620 info->info = pj_str("TCP transport");
1621 info->flag = factory->flag;
1622 info->addr_len = sizeof(factory->local_addr);
1623 info->local_addr = factory->local_addr;
1624 info->local_name = factory->addr_name;
1625 info->usage_count = 0;
1626
Benny Prijono0a5cad82006-09-26 13:21:02 +00001627 status = PJ_SUCCESS;
1628
1629 } else {
1630 pj_assert(!"Unsupported transport");
1631 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001632 }
1633
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001634
1635 PJSUA_UNLOCK();
1636
Benny Prijono0a5cad82006-09-26 13:21:02 +00001637 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001638}
1639
1640
1641/*
1642 * Disable a transport or re-enable it.
1643 */
1644PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
1645 pj_bool_t enabled)
1646{
1647 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001648 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1649 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001650
1651 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001652 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001653
1654
1655 /* To be done!! */
1656 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001657 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001658
1659 return PJ_EINVALIDOP;
1660}
1661
1662
1663/*
1664 * Close the transport.
1665 */
1666PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
1667 pj_bool_t force )
1668{
Benny Prijono5ff61872007-02-01 03:37:11 +00001669 pj_status_t status;
1670
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001671 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001672 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1673 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001674
1675 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001676 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001677
Benny Prijono0a5cad82006-09-26 13:21:02 +00001678 /* Note: destroy() may not work if there are objects still referencing
1679 * the transport.
1680 */
1681 if (force) {
1682 switch (pjsua_var.tpdata[id].type) {
1683 case PJSIP_TRANSPORT_UDP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001684 status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
1685 if (status != PJ_SUCCESS)
1686 return status;
1687 status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
1688 if (status != PJ_SUCCESS)
1689 return status;
1690 break;
1691
1692 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001693 case PJSIP_TRANSPORT_TCP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001694 /* This will close the TCP listener, but existing TCP/TLS
1695 * connections (if any) will still linger
1696 */
1697 status = (*pjsua_var.tpdata[id].data.factory->destroy)
1698 (pjsua_var.tpdata[id].data.factory);
1699 if (status != PJ_SUCCESS)
1700 return status;
1701
Benny Prijono0a5cad82006-09-26 13:21:02 +00001702 break;
Benny Prijono5ff61872007-02-01 03:37:11 +00001703
Benny Prijono1ebd6142006-10-19 15:48:02 +00001704 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001705 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001706 }
1707
1708 } else {
Benny Prijono5ff61872007-02-01 03:37:11 +00001709 /* If force is not specified, transports will be closed at their
1710 * convenient time. However this will leak PJSUA-API transport
1711 * descriptors as PJSUA-API wouldn't know when exactly the
1712 * transport is closed thus it can't cleanup PJSUA transport
1713 * descriptor.
1714 */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001715 switch (pjsua_var.tpdata[id].type) {
1716 case PJSIP_TRANSPORT_UDP:
1717 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
Benny Prijono5ff61872007-02-01 03:37:11 +00001718 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001719 case PJSIP_TRANSPORT_TCP:
1720 return (*pjsua_var.tpdata[id].data.factory->destroy)
1721 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001722 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001723 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001724 }
1725 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001726
Benny Prijono5ff61872007-02-01 03:37:11 +00001727 /* Cleanup pjsua data when force is applied */
1728 if (force) {
1729 pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED;
1730 pjsua_var.tpdata[id].data.ptr = NULL;
1731 }
1732
1733 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001734}
1735
1736
1737/*
1738 * Add additional headers etc in msg_data specified by application
1739 * when sending requests.
1740 */
1741void pjsua_process_msg_data(pjsip_tx_data *tdata,
1742 const pjsua_msg_data *msg_data)
1743{
1744 pj_bool_t allow_body;
1745 const pjsip_hdr *hdr;
1746
Benny Prijono56315612006-07-18 14:39:40 +00001747 /* Always add User-Agent */
1748 if (pjsua_var.ua_cfg.user_agent.slen &&
1749 tdata->msg->type == PJSIP_REQUEST_MSG)
1750 {
1751 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1752 pjsip_hdr *h;
1753 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
1754 &STR_USER_AGENT,
1755 &pjsua_var.ua_cfg.user_agent);
1756 pjsip_msg_add_hdr(tdata->msg, h);
1757 }
1758
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001759 if (!msg_data)
1760 return;
1761
1762 hdr = msg_data->hdr_list.next;
1763 while (hdr && hdr != &msg_data->hdr_list) {
1764 pjsip_hdr *new_hdr;
1765
Benny Prijonoa1e69682007-05-11 15:14:34 +00001766 new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001767 pjsip_msg_add_hdr(tdata->msg, new_hdr);
1768
1769 hdr = hdr->next;
1770 }
1771
1772 allow_body = (tdata->msg->body == NULL);
1773
1774 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
1775 pjsip_media_type ctype;
1776 pjsip_msg_body *body;
1777
1778 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
1779 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
1780 &msg_data->msg_body);
1781 tdata->msg->body = body;
1782 }
1783}
1784
1785
1786/*
1787 * Add route_set to outgoing requests
1788 */
1789void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
1790 const pjsip_route_hdr *route_set )
1791{
1792 const pjsip_route_hdr *r;
1793
1794 r = route_set->next;
1795 while (r != route_set) {
1796 pjsip_route_hdr *new_r;
1797
Benny Prijonoa1e69682007-05-11 15:14:34 +00001798 new_r = (pjsip_route_hdr*) pjsip_hdr_clone(tdata->pool, r);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001799 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
1800
1801 r = r->next;
1802 }
1803}
1804
1805
1806/*
1807 * Simple version of MIME type parsing (it doesn't support parameters)
1808 */
1809void pjsua_parse_media_type( pj_pool_t *pool,
1810 const pj_str_t *mime,
1811 pjsip_media_type *media_type)
1812{
1813 pj_str_t tmp;
1814 char *pos;
1815
Benny Prijonoac623b32006-07-03 15:19:31 +00001816 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001817
1818 pj_strdup_with_null(pool, &tmp, mime);
1819
1820 pos = pj_strchr(&tmp, '/');
1821 if (pos) {
1822 media_type->type.ptr = tmp.ptr;
1823 media_type->type.slen = (pos-tmp.ptr);
1824 media_type->subtype.ptr = pos+1;
1825 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
1826 } else {
1827 media_type->type = tmp;
1828 }
1829}
1830
1831
1832/*
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001833 * Internal function to init transport selector from transport id.
1834 */
1835void pjsua_init_tpselector(pjsua_transport_id tp_id,
1836 pjsip_tpselector *sel)
1837{
1838 pjsua_transport_data *tpdata;
1839 unsigned flag;
1840
1841 pj_bzero(sel, sizeof(*sel));
1842 if (tp_id == PJSUA_INVALID_ID)
1843 return;
1844
Benny Prijonoa1e69682007-05-11 15:14:34 +00001845 pj_assert(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata));
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001846 tpdata = &pjsua_var.tpdata[tp_id];
1847
1848 flag = pjsip_transport_get_flag_from_type(tpdata->type);
1849
1850 if (flag & PJSIP_TRANSPORT_DATAGRAM) {
1851 sel->type = PJSIP_TPSELECTOR_TRANSPORT;
1852 sel->u.transport = tpdata->data.tp;
1853 } else {
1854 sel->type = PJSIP_TPSELECTOR_LISTENER;
1855 sel->u.listener = tpdata->data.factory;
1856 }
1857}
1858
1859
1860/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001861 * Verify that valid SIP url is given.
1862 */
1863PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
1864{
1865 pjsip_uri *p;
1866 pj_pool_t *pool;
1867 char *url;
1868 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
1869
1870 if (!len) return -1;
1871
1872 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
1873 if (!pool) return -1;
1874
Benny Prijonoa1e69682007-05-11 15:14:34 +00001875 url = (char*) pj_pool_alloc(pool, len+1);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001876 pj_ansi_strcpy(url, c_url);
1877
1878 p = pjsip_parse_uri(pool, url, len, 0);
1879 if (!p || pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0)
1880 p = NULL;
1881
1882 pj_pool_release(pool);
1883 return p ? 0 : -1;
1884}
Benny Prijonoda9785b2007-04-02 20:43:06 +00001885
1886
1887/*
1888 * This is a utility function to dump the stack states to log, using
1889 * verbosity level 3.
1890 */
1891PJ_DEF(void) pjsua_dump(pj_bool_t detail)
1892{
1893 unsigned old_decor;
1894 unsigned i;
1895 char buf[1024];
1896
1897 PJ_LOG(3,(THIS_FILE, "Start dumping application states:"));
1898
1899 old_decor = pj_log_get_decor();
1900 pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
1901
1902 if (detail)
1903 pj_dump_config();
1904
1905 pjsip_endpt_dump(pjsua_get_pjsip_endpt(), detail);
1906
1907 pjmedia_endpt_dump(pjsua_get_pjmedia_endpt());
1908
1909 PJ_LOG(3,(THIS_FILE, "Dumping media transports:"));
1910 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
1911 pjsua_call *call = &pjsua_var.calls[i];
1912 pjmedia_sock_info skinfo;
1913
Benny Prijono4f093d22007-04-09 08:54:32 +00001914 /* MSVC complains about skinfo not being initialized */
1915 pj_bzero(&skinfo, sizeof(skinfo));
1916
Benny Prijonoda9785b2007-04-02 20:43:06 +00001917 pjmedia_transport_get_info(call->med_tp, &skinfo);
1918
1919 PJ_LOG(3,(THIS_FILE, " %s: %s:%d",
1920 (pjsua_var.media_cfg.enable_ice ? "ICE" : "UDP"),
1921 pj_inet_ntoa(skinfo.rtp_addr_name.sin_addr),
1922 (int)pj_ntohs(skinfo.rtp_addr_name.sin_port)));
1923 }
1924
1925 pjsip_tsx_layer_dump(detail);
1926 pjsip_ua_dump(detail);
1927
1928
1929 /* Dump all invite sessions: */
1930 PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:"));
1931
1932 if (pjsua_call_get_count() == 0) {
1933
1934 PJ_LOG(3,(THIS_FILE, " - no sessions -"));
1935
1936 } else {
1937 unsigned i;
1938
1939 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
1940 if (pjsua_call_is_active(i)) {
1941 pjsua_call_dump(i, detail, buf, sizeof(buf), " ");
1942 PJ_LOG(3,(THIS_FILE, "%s", buf));
1943 }
1944 }
1945 }
1946
1947 /* Dump presence status */
1948 pjsua_pres_dump(detail);
1949
1950 pj_log_set_decor(old_decor);
1951 PJ_LOG(3,(THIS_FILE, "Dump complete"));
1952}
1953