blob: 8c2b3b56186f049c589d13232d9a40d0a7feb7f9 [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 Prijonodcfc0ba2007-09-30 16:50:27 +0000144 cfg->require_100rel = pjsua_var.ua_cfg.require_100rel;
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;
157 cfg->max_media_ports = 32;
158 cfg->has_ioqueue = PJ_TRUE;
159 cfg->thread_cnt = 1;
160 cfg->quality = PJSUA_DEFAULT_CODEC_QUALITY;
161 cfg->ilbc_mode = PJSUA_DEFAULT_ILBC_MODE;
162 cfg->ec_tail_len = PJSUA_DEFAULT_EC_TAIL_LEN;
163 cfg->jb_init = cfg->jb_min_pre = cfg->jb_max_pre = cfg->jb_max = -1;
164}
165
Benny Prijonodc39fe82006-05-26 12:17:46 +0000166
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000167/*****************************************************************************
168 * This is a very simple PJSIP module, whose sole purpose is to display
169 * incoming and outgoing messages to log. This module will have priority
170 * higher than transport layer, which means:
171 *
172 * - incoming messages will come to this module first before reaching
173 * transaction layer.
174 *
175 * - outgoing messages will come to this module last, after the message
176 * has been 'printed' to contiguous buffer by transport layer and
177 * appropriate transport instance has been decided for this message.
178 *
179 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000180
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000181/* Notification on incoming messages */
182static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
183{
Benny Prijonob4a17c92006-07-10 14:40:21 +0000184 PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
185 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000186 "--end msg--",
187 rdata->msg_info.len,
188 pjsip_rx_data_get_info(rdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000189 rdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000190 rdata->pkt_info.src_name,
191 rdata->pkt_info.src_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000192 (int)rdata->msg_info.len,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000193 rdata->msg_info.msg_buf));
194
195 /* Always return false, otherwise messages will not get processed! */
196 return PJ_FALSE;
197}
Benny Prijonodc39fe82006-05-26 12:17:46 +0000198
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000199/* Notification on outgoing messages */
200static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
201{
202
203 /* Important note:
204 * tp_info field is only valid after outgoing messages has passed
205 * transport layer. So don't try to access tp_info when the module
206 * has lower priority than transport layer.
207 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000208
Benny Prijonob4a17c92006-07-10 14:40:21 +0000209 PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
210 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000211 "--end msg--",
212 (tdata->buf.cur - tdata->buf.start),
213 pjsip_tx_data_get_info(tdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000214 tdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000215 tdata->tp_info.dst_name,
216 tdata->tp_info.dst_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000217 (int)(tdata->buf.cur - tdata->buf.start),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000218 tdata->buf.start));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000219
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000220 /* Always return success, otherwise message will not get sent! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000221 return PJ_SUCCESS;
222}
223
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000224/* The module instance. */
225static pjsip_module pjsua_msg_logger =
226{
227 NULL, NULL, /* prev, next. */
228 { "mod-pjsua-log", 13 }, /* Name. */
229 -1, /* Id */
230 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
231 NULL, /* load() */
232 NULL, /* start() */
233 NULL, /* stop() */
234 NULL, /* unload() */
235 &logging_on_rx_msg, /* on_rx_request() */
236 &logging_on_rx_msg, /* on_rx_response() */
237 &logging_on_tx_msg, /* on_tx_request. */
238 &logging_on_tx_msg, /* on_tx_response() */
239 NULL, /* on_tsx_state() */
240
241};
242
243
244/*****************************************************************************
Benny Prijono56315612006-07-18 14:39:40 +0000245 * Another simple module to handle incoming OPTIONS request
246 */
247
248/* Notification on incoming request */
249static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata)
250{
251 pjsip_tx_data *tdata;
252 pjsip_response_addr res_addr;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000253 pjmedia_sock_info skinfo;
Benny Prijono56315612006-07-18 14:39:40 +0000254 pjmedia_sdp_session *sdp;
255 const pjsip_hdr *cap_hdr;
256 pj_status_t status;
257
258 /* Only want to handle OPTIONS requests */
259 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000260 pjsip_get_options_method()) != 0)
Benny Prijono56315612006-07-18 14:39:40 +0000261 {
262 return PJ_FALSE;
263 }
264
265 /* Create basic response. */
266 status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL,
267 &tdata);
268 if (status != PJ_SUCCESS) {
269 pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status);
270 return PJ_TRUE;
271 }
272
273 /* Add Allow header */
274 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL);
275 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000276 pjsip_msg_add_hdr(tdata->msg,
277 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000278 }
279
280 /* Add Accept header */
281 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL);
282 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000283 pjsip_msg_add_hdr(tdata->msg,
284 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000285 }
286
287 /* Add Supported header */
288 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL);
289 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000290 pjsip_msg_add_hdr(tdata->msg,
291 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000292 }
293
294 /* Add Allow-Events header from the evsub module */
295 cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL);
296 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000297 pjsip_msg_add_hdr(tdata->msg,
298 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000299 }
300
301 /* Add User-Agent header */
302 if (pjsua_var.ua_cfg.user_agent.slen) {
303 const pj_str_t USER_AGENT = { "User-Agent", 10};
304 pjsip_hdr *h;
305
306 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
307 &USER_AGENT,
308 &pjsua_var.ua_cfg.user_agent);
309 pjsip_msg_add_hdr(tdata->msg, h);
310 }
311
Benny Prijono617c5bc2007-04-02 19:51:21 +0000312 /* Get media socket info */
313 pjmedia_transport_get_info(pjsua_var.calls[0].med_tp, &skinfo);
314
Benny Prijono56315612006-07-18 14:39:40 +0000315 /* Add SDP body, using call0's RTP address */
316 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000317 &skinfo, &sdp);
Benny Prijono56315612006-07-18 14:39:40 +0000318 if (status == PJ_SUCCESS) {
319 pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
320 }
321
322 /* Send response statelessly */
323 pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
324 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL);
325 if (status != PJ_SUCCESS)
326 pjsip_tx_data_dec_ref(tdata);
327
328 return PJ_TRUE;
329}
330
331
332/* The module instance. */
333static pjsip_module pjsua_options_handler =
334{
335 NULL, NULL, /* prev, next. */
336 { "mod-pjsua-options", 17 }, /* Name. */
337 -1, /* Id */
338 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
339 NULL, /* load() */
340 NULL, /* start() */
341 NULL, /* stop() */
342 NULL, /* unload() */
343 &options_on_rx_request, /* on_rx_request() */
344 NULL, /* on_rx_response() */
345 NULL, /* on_tx_request. */
346 NULL, /* on_tx_response() */
347 NULL, /* on_tsx_state() */
348
349};
350
351
352/*****************************************************************************
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000353 * These two functions are the main callbacks registered to PJSIP stack
354 * to receive SIP request and response messages that are outside any
355 * dialogs and any transactions.
356 */
Benny Prijono268ca612006-02-07 12:34:11 +0000357
358/*
359 * Handler for receiving incoming requests.
360 *
361 * This handler serves multiple purposes:
362 * - it receives requests outside dialogs.
363 * - it receives requests inside dialogs, when the requests are
364 * unhandled by other dialog usages. Example of these
365 * requests are: MESSAGE.
366 */
367static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
368{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000369 pj_bool_t processed = PJ_FALSE;
370
371 PJSUA_LOCK();
Benny Prijono38998232006-02-08 22:44:25 +0000372
Benny Prijono84126ab2006-02-09 09:30:09 +0000373 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000374
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000375 processed = pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000376 }
377
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000378 PJSUA_UNLOCK();
379
380 return processed;
Benny Prijono268ca612006-02-07 12:34:11 +0000381}
382
383
384/*
385 * Handler for receiving incoming responses.
386 *
387 * This handler serves multiple purposes:
388 * - it receives strayed responses (i.e. outside any dialog and
389 * outside any transactions).
390 * - it receives responses coming to a transaction, when pjsua
391 * module is set as transaction user for the transaction.
392 * - it receives responses inside a dialog, when these responses
393 * are unhandled by other dialog usages.
394 */
395static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
396{
397 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000398 return PJ_FALSE;
399}
400
401
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000402/*****************************************************************************
403 * Logging.
404 */
405
406/* Log callback */
407static void log_writer(int level, const char *buffer, int len)
Benny Prijono268ca612006-02-07 12:34:11 +0000408{
Benny Prijono572d4852006-11-23 21:50:02 +0000409 /* Write to file, stdout or application callback. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000410
411 if (pjsua_var.log_file) {
412 pj_ssize_t size = len;
413 pj_file_write(pjsua_var.log_file, buffer, &size);
Benny Prijono980ca0a2007-04-03 17:55:08 +0000414 /* This will slow things down considerably! Don't do it!
415 pj_file_flush(pjsua_var.log_file);
416 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000417 }
418
Benny Prijono572d4852006-11-23 21:50:02 +0000419 if (level <= (int)pjsua_var.log_cfg.console_level) {
420 if (pjsua_var.log_cfg.cb)
421 (*pjsua_var.log_cfg.cb)(level, buffer, len);
422 else
423 pj_log_write(level, buffer, len);
424 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000425}
426
427
428/*
429 * Application can call this function at any time (after pjsua_create(), of
430 * course) to change logging settings.
431 */
432PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg)
433{
434 pj_status_t status;
435
436 /* Save config. */
437 pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg);
438
439 /* Redirect log function to ours */
440 pj_log_set_log_func( &log_writer );
441
Benny Prijono9cb09a22007-08-30 09:35:10 +0000442 /* Set decor */
443 pj_log_set_decor(pjsua_var.log_cfg.decor);
444
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000445 /* Close existing file, if any */
446 if (pjsua_var.log_file) {
447 pj_file_close(pjsua_var.log_file);
448 pjsua_var.log_file = NULL;
449 }
450
451 /* If output log file is desired, create the file: */
452 if (pjsua_var.log_cfg.log_filename.slen) {
453
454 status = pj_file_open(pjsua_var.pool,
455 pjsua_var.log_cfg.log_filename.ptr,
456 PJ_O_WRONLY,
457 &pjsua_var.log_file);
458
459 if (status != PJ_SUCCESS) {
460 pjsua_perror(THIS_FILE, "Error creating log file", status);
461 return status;
462 }
463 }
464
465 /* Unregister msg logging if it's previously registered */
466 if (pjsua_msg_logger.id >= 0) {
467 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
468 pjsua_msg_logger.id = -1;
469 }
470
471 /* Enable SIP message logging */
472 if (pjsua_var.log_cfg.msg_logging)
473 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
474
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000475 return PJ_SUCCESS;
476}
477
478
479/*****************************************************************************
480 * PJSUA Base API.
481 */
482
483/* Worker thread function. */
484static int worker_thread(void *arg)
485{
486 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000487
Benny Prijono268ca612006-02-07 12:34:11 +0000488 PJ_UNUSED_ARG(arg);
489
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000490 while (!pjsua_var.thread_quit_flag) {
491 int count;
492
493 count = pjsua_handle_events(TIMEOUT);
494 if (count < 0)
495 pj_thread_sleep(TIMEOUT);
496 }
Benny Prijono268ca612006-02-07 12:34:11 +0000497
498 return 0;
499}
500
Benny Prijonodc39fe82006-05-26 12:17:46 +0000501
Benny Prijono268ca612006-02-07 12:34:11 +0000502/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000503 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000504 */
505PJ_DEF(pj_status_t) pjsua_create(void)
506{
507 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000508
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000509 /* Init pjsua data */
510 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000511
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000512 /* Set default logging settings */
513 pjsua_logging_config_default(&pjsua_var.log_cfg);
514
515 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000516 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000517 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
518
Benny Prijono268ca612006-02-07 12:34:11 +0000519
Benny Prijonofccab712006-02-22 22:23:22 +0000520 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000521 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000522 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000523
Benny Prijonoec921342007-03-24 13:00:30 +0000524 /* Init PJNATH */
525 status = pjnath_init();
526 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono268ca612006-02-07 12:34:11 +0000527
Benny Prijono094d3ad2006-11-21 08:41:00 +0000528 /* Set default sound device ID */
529 pjsua_var.cap_dev = pjsua_var.play_dev = -1;
530
Benny Prijono268ca612006-02-07 12:34:11 +0000531 /* Init caching pool. */
Benny Prijono106f5b72007-08-30 13:49:33 +0000532 pj_caching_pool_init(&pjsua_var.cp, NULL, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000533
534 /* Create memory pool for application. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000535 pjsua_var.pool = pjsua_pool_create("pjsua", 4000, 4000);
536
537 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000538
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000539 /* Create mutex */
540 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
541 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000542 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000543 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000544 return status;
545 }
546
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000547 /* Must create SIP endpoint to initialize SIP parser. The parser
548 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000549 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000550 status = pjsip_endpt_create(&pjsua_var.cp.factory,
551 pj_gethostname()->ptr,
552 &pjsua_var.endpt);
553 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000554
555
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000556 return PJ_SUCCESS;
557}
558
559
560/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000561 * Initialize pjsua with the specified settings. All the settings are
562 * optional, and the default values will be used when the config is not
563 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000564 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000565PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
566 const pjsua_logging_config *log_cfg,
567 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000568{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000569 pjsua_config default_cfg;
570 pjsua_media_config default_media_cfg;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000571 const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
Benny Prijonodc39fe82006-05-26 12:17:46 +0000572 pj_status_t status;
573
574
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000575 /* Create default configurations when the config is not supplied */
576
577 if (ua_cfg == NULL) {
578 pjsua_config_default(&default_cfg);
579 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000580 }
581
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000582 if (media_cfg == NULL) {
583 pjsua_media_config_default(&default_media_cfg);
584 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000585 }
586
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000587 /* Initialize logging first so that info/errors can be captured */
588 if (log_cfg) {
589 status = pjsua_reconfigure_logging(log_cfg);
590 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono9fc735d2006-05-28 14:58:12 +0000591 }
592
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000593 /* If nameserver is configured, create DNS resolver instance and
594 * set it to be used by SIP resolver.
595 */
596 if (ua_cfg->nameserver_count) {
597#if PJSIP_HAS_RESOLVER
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000598 unsigned i;
599
600 /* Create DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000601 status = pjsip_endpt_create_resolver(pjsua_var.endpt,
602 &pjsua_var.resolver);
Benny Prijono318f3842007-05-15 20:27:08 +0000603 if (status != PJ_SUCCESS) {
604 pjsua_perror(THIS_FILE, "Error creating resolver", status);
605 return status;
606 }
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000607
608 /* Configure nameserver for the DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000609 status = pj_dns_resolver_set_ns(pjsua_var.resolver,
610 ua_cfg->nameserver_count,
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000611 ua_cfg->nameserver, NULL);
612 if (status != PJ_SUCCESS) {
613 pjsua_perror(THIS_FILE, "Error setting nameserver", status);
614 return status;
615 }
616
617 /* Set this DNS resolver to be used by the SIP resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000618 status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000619 if (status != PJ_SUCCESS) {
620 pjsua_perror(THIS_FILE, "Error setting DNS resolver", status);
621 return status;
622 }
623
624 /* Print nameservers */
625 for (i=0; i<ua_cfg->nameserver_count; ++i) {
626 PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added",
627 (int)ua_cfg->nameserver[i].slen,
628 ua_cfg->nameserver[i].ptr));
629 }
630#else
631 PJ_LOG(2,(THIS_FILE,
632 "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)"));
633#endif
634 }
635
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000636 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000637
638 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000639 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
640 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000641
Benny Prijonodc39fe82006-05-26 12:17:46 +0000642
643 /* Initialize UA layer module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000644 status = pjsip_ua_init_module( pjsua_var.endpt, NULL );
645 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000646
Benny Prijonodc39fe82006-05-26 12:17:46 +0000647
Benny Prijono053f5222006-11-11 16:16:04 +0000648 /* Initialize Replaces support. */
649 status = pjsip_replaces_init_module( pjsua_var.endpt );
650 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
651
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000652#if PJSIP_HAS_100REL
653 /* Initialize 100rel support */
654 status = pjsip_100rel_init_module(pjsua_var.endpt);
655 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
656#endif
Benny Prijono053f5222006-11-11 16:16:04 +0000657
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000658 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000659 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000660 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000661 {
662 NULL, NULL, /* prev, next. */
663 { "mod-pjsua", 9 }, /* Name. */
664 -1, /* Id */
665 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
666 NULL, /* load() */
667 NULL, /* start() */
668 NULL, /* stop() */
669 NULL, /* unload() */
670 &mod_pjsua_on_rx_request, /* on_rx_request() */
671 &mod_pjsua_on_rx_response, /* on_rx_response() */
672 NULL, /* on_tx_request. */
673 NULL, /* on_tx_response() */
674 NULL, /* on_tsx_state() */
675 };
676
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000677 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000678
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000679 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
680 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000681 }
682
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000683
Benny Prijonodc39fe82006-05-26 12:17:46 +0000684
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000685 /* Initialize PJSUA call subsystem: */
686 status = pjsua_call_subsys_init(ua_cfg);
687 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000688 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000689
690
Benny Prijonoc97608e2007-03-23 16:34:20 +0000691 /* Start resolving STUN server */
692 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
952
953 pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000954
955 if (pjsua_var.stun_status == PJ_SUCCESS) {
956 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
957 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000958 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonof75eceb2007-03-23 19:09:54 +0000959
Benny Prijonoebbf6892007-03-24 17:37:25 +0000960 PJ_LOG(3,(THIS_FILE,
Benny Prijonof75eceb2007-03-23 19:09:54 +0000961 "STUN server %.*s resolved, address is %s:%d",
Benny Prijonoebbf6892007-03-24 17:37:25 +0000962 (int)pjsua_var.ua_cfg.stun_host.slen,
963 pjsua_var.ua_cfg.stun_host.ptr,
Benny Prijonof75eceb2007-03-23 19:09:54 +0000964 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
965 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
Benny Prijonoc97608e2007-03-23 16:34:20 +0000966 }
Benny Prijonoebbf6892007-03-24 17:37:25 +0000967
Benny Prijonoc97608e2007-03-23 16:34:20 +0000968 }
Benny Prijonoebbf6892007-03-24 17:37:25 +0000969 /* Otherwise disable STUN. */
970 else {
971 pjsua_var.stun_status = PJ_SUCCESS;
972 }
973
974
Benny Prijonoc97608e2007-03-23 16:34:20 +0000975 return pjsua_var.stun_status;
976
977 } else if (pjsua_var.stun_status == PJ_EPENDING) {
978 /* STUN server resolution has been started, wait for the
979 * result.
980 */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000981 if (wait) {
982 while (pjsua_var.stun_status == PJ_EPENDING)
983 pjsua_handle_events(10);
984 }
985
986 return pjsua_var.stun_status;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000987
988 } else {
989 /* STUN server has been resolved, return the status */
990 return pjsua_var.stun_status;
991 }
992}
993
994/*
Benny Prijono268ca612006-02-07 12:34:11 +0000995 * Destroy pjsua.
996 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000997PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +0000998{
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000999 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +00001000
1001 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001002 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +00001003
1004 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001005 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
1006 if (pjsua_var.thread[i]) {
1007 pj_thread_join(pjsua_var.thread[i]);
1008 pj_thread_destroy(pjsua_var.thread[i]);
1009 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +00001010 }
Benny Prijono268ca612006-02-07 12:34:11 +00001011 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001012
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001013 if (pjsua_var.endpt) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001014 /* Terminate all calls. */
1015 pjsua_call_hangup_all();
1016
1017 /* Terminate all presence subscriptions. */
1018 pjsua_pres_shutdown();
1019
1020 /* Unregister, if required: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001021 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1022 if (!pjsua_var.acc[i].valid)
1023 continue;
1024
1025 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001026 pjsua_acc_set_registration(i, PJ_FALSE);
1027 }
1028 }
1029
1030 /* Wait for some time to allow unregistration to complete: */
Benny Prijono9fc735d2006-05-28 14:58:12 +00001031 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
1032 busy_sleep(1000);
1033 }
Benny Prijono834aee32006-02-19 01:38:06 +00001034
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001035 /* Destroy media */
1036 pjsua_media_subsys_destroy();
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001037
Benny Prijono268ca612006-02-07 12:34:11 +00001038 /* Destroy endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001039 if (pjsua_var.endpt) {
1040 pjsip_endpt_destroy(pjsua_var.endpt);
1041 pjsua_var.endpt = NULL;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001042 }
Benny Prijono268ca612006-02-07 12:34:11 +00001043
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001044 /* Destroy mutex */
1045 if (pjsua_var.mutex) {
1046 pj_mutex_destroy(pjsua_var.mutex);
1047 pjsua_var.mutex = NULL;
1048 }
1049
1050 /* Destroy pool and pool factory. */
1051 if (pjsua_var.pool) {
1052 pj_pool_release(pjsua_var.pool);
1053 pjsua_var.pool = NULL;
1054 pj_caching_pool_destroy(&pjsua_var.cp);
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00001055
1056 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
1057
1058 /* End logging */
1059 if (pjsua_var.log_file) {
1060 pj_file_close(pjsua_var.log_file);
1061 pjsua_var.log_file = NULL;
1062 }
1063
1064 /* Shutdown PJLIB */
1065 pj_shutdown();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001066 }
Benny Prijono268ca612006-02-07 12:34:11 +00001067
Benny Prijonof762ee72006-12-01 11:14:37 +00001068 /* Clear pjsua_var */
1069 pj_bzero(&pjsua_var, sizeof(pjsua_var));
1070
Benny Prijono268ca612006-02-07 12:34:11 +00001071 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001072 return PJ_SUCCESS;
1073}
1074
1075
1076/**
1077 * Application is recommended to call this function after all initialization
1078 * is done, so that the library can do additional checking set up
1079 * additional
1080 *
1081 * @return PJ_SUCCESS on success, or the appropriate error code.
1082 */
1083PJ_DEF(pj_status_t) pjsua_start(void)
1084{
1085 pj_status_t status;
1086
1087 status = pjsua_call_subsys_start();
1088 if (status != PJ_SUCCESS)
1089 return status;
1090
1091 status = pjsua_media_subsys_start();
1092 if (status != PJ_SUCCESS)
1093 return status;
1094
1095 status = pjsua_pres_start();
1096 if (status != PJ_SUCCESS)
1097 return status;
Benny Prijono268ca612006-02-07 12:34:11 +00001098
1099 return PJ_SUCCESS;
1100}
1101
Benny Prijono9fc735d2006-05-28 14:58:12 +00001102
1103/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001104 * Poll pjsua for events, and if necessary block the caller thread for
1105 * the specified maximum interval (in miliseconds).
1106 */
1107PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
1108{
Benny Prijono897f9f82007-05-03 19:56:21 +00001109#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
1110 /* Ideally we shouldn't call pj_thread_sleep() and rather
1111 * CActiveScheduler::WaitForAnyRequest() here, but that will
1112 * drag in Symbian header and it doesn't look pretty.
1113 */
1114 pj_thread_sleep(msec_timeout);
1115 return msec_timeout;
1116#else
1117
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001118 unsigned count = 0;
1119 pj_time_val tv;
1120 pj_status_t status;
1121
1122 tv.sec = 0;
1123 tv.msec = msec_timeout;
1124 pj_time_val_normalize(&tv);
1125
1126 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
1127
1128 if (status != PJ_SUCCESS)
1129 return -status;
1130
1131 return count;
Benny Prijono897f9f82007-05-03 19:56:21 +00001132#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001133}
1134
1135
1136/*
1137 * Create memory pool.
1138 */
1139PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
1140 pj_size_t increment)
1141{
1142 /* Pool factory is thread safe, no need to lock */
1143 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
1144 NULL);
1145}
1146
1147
1148/*
1149 * Internal function to get SIP endpoint instance of pjsua, which is
1150 * needed for example to register module, create transports, etc.
1151 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001152 */
1153PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
1154{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001155 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001156}
1157
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001158/*
1159 * Internal function to get media endpoint instance.
1160 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001161 */
1162PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
1163{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001164 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001165}
1166
Benny Prijono97b87172006-08-24 14:25:14 +00001167/*
1168 * Internal function to get PJSUA pool factory.
1169 */
1170PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
1171{
1172 return &pjsua_var.cp.factory;
1173}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001174
1175/*****************************************************************************
1176 * PJSUA SIP Transport API.
1177 */
1178
1179/*
1180 * Create and initialize SIP socket (and possibly resolve public
1181 * address via STUN, depending on config).
1182 */
1183static pj_status_t create_sip_udp_sock(pj_in_addr bound_addr,
1184 int port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001185 pj_sock_t *p_sock,
1186 pj_sockaddr_in *p_pub_addr)
1187{
Benny Prijonoc97608e2007-03-23 16:34:20 +00001188 char ip_addr[32];
1189 pj_str_t stun_srv;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001190 pj_sock_t sock;
1191 pj_status_t status;
1192
Benny Prijonoc97608e2007-03-23 16:34:20 +00001193 /* Make sure STUN server resolution has completed */
1194 status = pjsua_resolve_stun_server(PJ_TRUE);
1195 if (status != PJ_SUCCESS) {
1196 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
1197 return status;
1198 }
1199
Benny Prijono8ab968f2007-07-20 08:08:30 +00001200 status = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001201 if (status != PJ_SUCCESS) {
1202 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +00001203 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001204 }
1205
Benny Prijonof90ef4f2007-02-12 14:59:57 +00001206 status = pj_sock_bind_in(sock, pj_ntohl(bound_addr.s_addr),
1207 (pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001208 if (status != PJ_SUCCESS) {
1209 pjsua_perror(THIS_FILE, "bind() error", status);
1210 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001211 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001212 }
1213
Benny Prijonoe347cb02007-02-14 14:36:13 +00001214 /* If port is zero, get the bound port */
1215 if (port == 0) {
1216 pj_sockaddr_in bound_addr;
1217 int namelen = sizeof(bound_addr);
1218 status = pj_sock_getsockname(sock, &bound_addr, &namelen);
1219 if (status != PJ_SUCCESS) {
1220 pjsua_perror(THIS_FILE, "getsockname() error", status);
1221 pj_sock_close(sock);
1222 return status;
1223 }
1224
1225 port = pj_ntohs(bound_addr.sin_port);
1226 }
1227
Benny Prijonoc97608e2007-03-23 16:34:20 +00001228 if (pjsua_var.stun_srv.addr.sa_family != 0) {
1229 pj_ansi_strcpy(ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
1230 stun_srv = pj_str(ip_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001231 } else {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001232 stun_srv.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001233 }
1234
1235 /* Get the published address, either by STUN or by resolving
1236 * the name of local host.
1237 */
Benny Prijonoc97608e2007-03-23 16:34:20 +00001238 if (stun_srv.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +00001239 /*
1240 * STUN is specified, resolve the address with STUN.
1241 */
Benny Prijono14c2b862007-02-21 00:40:05 +00001242 status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001243 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
1244 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijonoc97608e2007-03-23 16:34:20 +00001245 p_pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001246 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +00001247 pjsua_perror(THIS_FILE, "Error contacting STUN server", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001248 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001249 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001250 }
1251
Benny Prijono0a5cad82006-09-26 13:21:02 +00001252 } else if (p_pub_addr->sin_addr.s_addr != 0) {
1253 /*
1254 * Public address is already specified, no need to resolve the
1255 * address, only set the port.
1256 */
Benny Prijonoe347cb02007-02-14 14:36:13 +00001257 if (p_pub_addr->sin_port == 0)
1258 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001259
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001260 } else {
1261
Benny Prijono594e4c52006-09-14 18:51:01 +00001262 pj_bzero(p_pub_addr, sizeof(pj_sockaddr_in));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001263
Benny Prijono594e4c52006-09-14 18:51:01 +00001264 status = pj_gethostip(&p_pub_addr->sin_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001265 if (status != PJ_SUCCESS) {
Benny Prijonob43bad72007-01-20 05:11:08 +00001266 pjsua_perror(THIS_FILE, "Unable to get local host IP", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001267 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001268 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001269 }
1270
Benny Prijono8ab968f2007-07-20 08:08:30 +00001271 p_pub_addr->sin_family = pj_AF_INET();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001272 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001273 }
1274
1275 *p_sock = sock;
1276
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001277 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
1278 pj_inet_ntoa(p_pub_addr->sin_addr),
1279 (int)pj_ntohs(p_pub_addr->sin_port)));
1280
Benny Prijonod8410532006-06-15 11:04:33 +00001281 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001282}
1283
1284
1285/*
1286 * Create SIP transport.
1287 */
1288PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
1289 const pjsua_transport_config *cfg,
1290 pjsua_transport_id *p_id)
1291{
1292 pjsip_transport *tp;
1293 unsigned id;
1294 pj_status_t status;
1295
1296 PJSUA_LOCK();
1297
1298 /* Find empty transport slot */
1299 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001300 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001301 break;
1302 }
1303
1304 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1305 status = PJ_ETOOMANY;
1306 pjsua_perror(THIS_FILE, "Error creating transport", status);
1307 goto on_return;
1308 }
1309
1310 /* Create the transport */
1311 if (type == PJSIP_TRANSPORT_UDP) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001312 /*
1313 * Create UDP transport.
1314 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001315 pjsua_transport_config config;
Benny Prijonod8410532006-06-15 11:04:33 +00001316 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001317 pj_sockaddr_in bound_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001318 pj_sockaddr_in pub_addr;
1319 pjsip_host_port addr_name;
1320
1321 /* Supply default config if it's not specified */
1322 if (cfg == NULL) {
1323 pjsua_transport_config_default(&config);
1324 cfg = &config;
1325 }
1326
Benny Prijono0a5cad82006-09-26 13:21:02 +00001327 /* Initialize bound address, if any */
1328 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
1329 if (cfg->bound_addr.slen) {
1330 status = pj_sockaddr_in_set_str_addr(&bound_addr,&cfg->bound_addr);
1331 if (status != PJ_SUCCESS) {
1332 pjsua_perror(THIS_FILE,
1333 "Unable to resolve transport bound address",
1334 status);
1335 goto on_return;
1336 }
1337 }
1338
1339 /* Initialize the public address from the config, if any */
1340 pj_sockaddr_in_init(&pub_addr, NULL, (pj_uint16_t)cfg->port);
1341 if (cfg->public_addr.slen) {
1342 status = pj_sockaddr_in_set_str_addr(&pub_addr, &cfg->public_addr);
1343 if (status != PJ_SUCCESS) {
1344 pjsua_perror(THIS_FILE,
1345 "Unable to resolve transport public address",
1346 status);
1347 goto on_return;
1348 }
1349 }
1350
1351 /* Create the socket and possibly resolve the address with STUN
1352 * (only when public address is not specified).
1353 */
1354 status = create_sip_udp_sock(bound_addr.sin_addr, cfg->port,
Benny Prijono0a5cad82006-09-26 13:21:02 +00001355 &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001356 if (status != PJ_SUCCESS)
1357 goto on_return;
1358
1359 addr_name.host = pj_str(pj_inet_ntoa(pub_addr.sin_addr));
1360 addr_name.port = pj_ntohs(pub_addr.sin_port);
1361
1362 /* Create UDP transport */
1363 status = pjsip_udp_transport_attach( pjsua_var.endpt, sock,
1364 &addr_name, 1,
1365 &tp);
1366 if (status != PJ_SUCCESS) {
1367 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
1368 status);
1369 pj_sock_close(sock);
1370 goto on_return;
1371 }
1372
Benny Prijonoe93e2872006-06-28 16:46:49 +00001373
1374 /* Save the transport */
1375 pjsua_var.tpdata[id].type = type;
1376 pjsua_var.tpdata[id].local_name = tp->local_name;
1377 pjsua_var.tpdata[id].data.tp = tp;
1378
Benny Prijono3569c0d2007-04-06 10:29:20 +00001379#if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0
1380
Benny Prijonoe93e2872006-06-28 16:46:49 +00001381 } else if (type == PJSIP_TRANSPORT_TCP) {
1382 /*
1383 * Create TCP transport.
1384 */
1385 pjsua_transport_config config;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001386 pjsip_host_port a_name;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001387 pjsip_tpfactory *tcp;
1388 pj_sockaddr_in local_addr;
1389
1390 /* Supply default config if it's not specified */
1391 if (cfg == NULL) {
1392 pjsua_transport_config_default(&config);
1393 cfg = &config;
1394 }
1395
1396 /* Init local address */
1397 pj_sockaddr_in_init(&local_addr, 0, 0);
1398
1399 if (cfg->port)
1400 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1401
Benny Prijono0a5cad82006-09-26 13:21:02 +00001402 if (cfg->bound_addr.slen) {
1403 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1404 if (status != PJ_SUCCESS) {
1405 pjsua_perror(THIS_FILE,
1406 "Unable to resolve transport bound address",
1407 status);
1408 goto on_return;
1409 }
1410 }
1411
1412 /* Init published name */
1413 pj_bzero(&a_name, sizeof(pjsip_host_port));
1414 if (cfg->public_addr.slen)
1415 a_name.host = cfg->public_addr;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001416
1417 /* Create the TCP transport */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001418 status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr,
1419 &a_name, 1, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001420
1421 if (status != PJ_SUCCESS) {
1422 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1423 status);
1424 goto on_return;
1425 }
1426
1427 /* Save the transport */
1428 pjsua_var.tpdata[id].type = type;
1429 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1430 pjsua_var.tpdata[id].data.factory = tcp;
1431
Benny Prijono3569c0d2007-04-06 10:29:20 +00001432#endif /* PJ_HAS_TCP */
1433
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001434#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1435 } else if (type == PJSIP_TRANSPORT_TLS) {
1436 /*
1437 * Create TLS transport.
1438 */
Benny Prijonof3bbc132006-12-25 06:43:59 +00001439 /*
1440 * Create TCP transport.
1441 */
1442 pjsua_transport_config config;
1443 pjsip_host_port a_name;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001444 pjsip_tpfactory *tls;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001445 pj_sockaddr_in local_addr;
1446
1447 /* Supply default config if it's not specified */
1448 if (cfg == NULL) {
1449 pjsua_transport_config_default(&config);
1450 config.port = 5061;
1451 cfg = &config;
1452 }
1453
1454 /* Init local address */
1455 pj_sockaddr_in_init(&local_addr, 0, 0);
1456
1457 if (cfg->port)
1458 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1459
1460 if (cfg->bound_addr.slen) {
1461 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1462 if (status != PJ_SUCCESS) {
1463 pjsua_perror(THIS_FILE,
1464 "Unable to resolve transport bound address",
1465 status);
1466 goto on_return;
1467 }
1468 }
1469
1470 /* Init published name */
1471 pj_bzero(&a_name, sizeof(pjsip_host_port));
1472 if (cfg->public_addr.slen)
1473 a_name.host = cfg->public_addr;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001474
1475 status = pjsip_tls_transport_start(pjsua_var.endpt,
Benny Prijonof3bbc132006-12-25 06:43:59 +00001476 &cfg->tls_setting,
1477 &local_addr, &a_name, 1, &tls);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001478 if (status != PJ_SUCCESS) {
1479 pjsua_perror(THIS_FILE, "Error creating SIP TLS listener",
1480 status);
1481 goto on_return;
1482 }
1483
1484 /* Save the transport */
1485 pjsua_var.tpdata[id].type = type;
1486 pjsua_var.tpdata[id].local_name = tls->addr_name;
1487 pjsua_var.tpdata[id].data.factory = tls;
1488#endif
1489
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001490 } else {
1491 status = PJSIP_EUNSUPTRANSPORT;
1492 pjsua_perror(THIS_FILE, "Error creating transport", status);
1493 goto on_return;
1494 }
1495
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001496
1497 /* Return the ID */
1498 if (p_id) *p_id = id;
1499
1500 status = PJ_SUCCESS;
1501
1502on_return:
1503
1504 PJSUA_UNLOCK();
1505
Benny Prijonod8410532006-06-15 11:04:33 +00001506 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001507}
1508
1509
1510/*
1511 * Register transport that has been created by application.
1512 */
1513PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1514 pjsua_transport_id *p_id)
1515{
1516 unsigned id;
1517
1518 PJSUA_LOCK();
1519
1520 /* Find empty transport slot */
1521 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001522 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001523 break;
1524 }
1525
1526 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1527 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1528 PJSUA_UNLOCK();
1529 return PJ_ETOOMANY;
1530 }
1531
1532 /* Save the transport */
Benny Prijonod17a5e92007-05-29 11:51:45 +00001533 pjsua_var.tpdata[id].type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001534 pjsua_var.tpdata[id].local_name = tp->local_name;
1535 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001536
1537 /* Return the ID */
1538 if (p_id) *p_id = id;
1539
1540 PJSUA_UNLOCK();
1541
1542 return PJ_SUCCESS;
1543}
1544
1545
1546/*
1547 * Enumerate all transports currently created in the system.
1548 */
1549PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1550 unsigned *p_count )
1551{
1552 unsigned i, count;
1553
1554 PJSUA_LOCK();
1555
1556 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
1557 ++i)
1558 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001559 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001560 continue;
1561
1562 id[count++] = i;
1563 }
1564
1565 *p_count = count;
1566
1567 PJSUA_UNLOCK();
1568
1569 return PJ_SUCCESS;
1570}
1571
1572
1573/*
1574 * Get information about transports.
1575 */
1576PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
1577 pjsua_transport_info *info)
1578{
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001579 pjsua_transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00001580 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001581
Benny Prijonoac623b32006-07-03 15:19:31 +00001582 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001583
1584 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001585 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1586 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001587
1588 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001589 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001590
1591 PJSUA_LOCK();
1592
Benny Prijonoe93e2872006-06-28 16:46:49 +00001593 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
1594
1595 pjsip_transport *tp = t->data.tp;
1596
1597 if (tp == NULL) {
1598 PJSUA_UNLOCK();
1599 return PJ_EINVALIDOP;
1600 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001601
Benny Prijonoe93e2872006-06-28 16:46:49 +00001602 info->id = id;
Benny Prijonod17a5e92007-05-29 11:51:45 +00001603 info->type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001604 info->type_name = pj_str(tp->type_name);
1605 info->info = pj_str(tp->info);
1606 info->flag = tp->flag;
1607 info->addr_len = tp->addr_len;
1608 info->local_addr = tp->local_addr;
1609 info->local_name = tp->local_name;
1610 info->usage_count = pj_atomic_get(tp->ref_cnt);
1611
Benny Prijono0a5cad82006-09-26 13:21:02 +00001612 status = PJ_SUCCESS;
1613
Benny Prijonoe93e2872006-06-28 16:46:49 +00001614 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
1615
1616 pjsip_tpfactory *factory = t->data.factory;
1617
1618 if (factory == NULL) {
1619 PJSUA_UNLOCK();
1620 return PJ_EINVALIDOP;
1621 }
1622
1623 info->id = id;
1624 info->type = t->type;
1625 info->type_name = pj_str("TCP");
1626 info->info = pj_str("TCP transport");
1627 info->flag = factory->flag;
1628 info->addr_len = sizeof(factory->local_addr);
1629 info->local_addr = factory->local_addr;
1630 info->local_name = factory->addr_name;
1631 info->usage_count = 0;
1632
Benny Prijono0a5cad82006-09-26 13:21:02 +00001633 status = PJ_SUCCESS;
1634
1635 } else {
1636 pj_assert(!"Unsupported transport");
1637 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001638 }
1639
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001640
1641 PJSUA_UNLOCK();
1642
Benny Prijono0a5cad82006-09-26 13:21:02 +00001643 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001644}
1645
1646
1647/*
1648 * Disable a transport or re-enable it.
1649 */
1650PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
1651 pj_bool_t enabled)
1652{
1653 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001654 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1655 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001656
1657 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001658 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001659
1660
1661 /* To be done!! */
1662 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001663 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001664
1665 return PJ_EINVALIDOP;
1666}
1667
1668
1669/*
1670 * Close the transport.
1671 */
1672PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
1673 pj_bool_t force )
1674{
Benny Prijono5ff61872007-02-01 03:37:11 +00001675 pj_status_t status;
1676
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001677 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001678 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1679 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001680
1681 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001682 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001683
Benny Prijono0a5cad82006-09-26 13:21:02 +00001684 /* Note: destroy() may not work if there are objects still referencing
1685 * the transport.
1686 */
1687 if (force) {
1688 switch (pjsua_var.tpdata[id].type) {
1689 case PJSIP_TRANSPORT_UDP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001690 status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
1691 if (status != PJ_SUCCESS)
1692 return status;
1693 status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
1694 if (status != PJ_SUCCESS)
1695 return status;
1696 break;
1697
1698 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001699 case PJSIP_TRANSPORT_TCP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001700 /* This will close the TCP listener, but existing TCP/TLS
1701 * connections (if any) will still linger
1702 */
1703 status = (*pjsua_var.tpdata[id].data.factory->destroy)
1704 (pjsua_var.tpdata[id].data.factory);
1705 if (status != PJ_SUCCESS)
1706 return status;
1707
Benny Prijono0a5cad82006-09-26 13:21:02 +00001708 break;
Benny Prijono5ff61872007-02-01 03:37:11 +00001709
Benny Prijono1ebd6142006-10-19 15:48:02 +00001710 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001711 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001712 }
1713
1714 } else {
Benny Prijono5ff61872007-02-01 03:37:11 +00001715 /* If force is not specified, transports will be closed at their
1716 * convenient time. However this will leak PJSUA-API transport
1717 * descriptors as PJSUA-API wouldn't know when exactly the
1718 * transport is closed thus it can't cleanup PJSUA transport
1719 * descriptor.
1720 */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001721 switch (pjsua_var.tpdata[id].type) {
1722 case PJSIP_TRANSPORT_UDP:
1723 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
Benny Prijono5ff61872007-02-01 03:37:11 +00001724 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001725 case PJSIP_TRANSPORT_TCP:
1726 return (*pjsua_var.tpdata[id].data.factory->destroy)
1727 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001728 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001729 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001730 }
1731 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001732
Benny Prijono5ff61872007-02-01 03:37:11 +00001733 /* Cleanup pjsua data when force is applied */
1734 if (force) {
1735 pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED;
1736 pjsua_var.tpdata[id].data.ptr = NULL;
1737 }
1738
1739 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001740}
1741
1742
1743/*
1744 * Add additional headers etc in msg_data specified by application
1745 * when sending requests.
1746 */
1747void pjsua_process_msg_data(pjsip_tx_data *tdata,
1748 const pjsua_msg_data *msg_data)
1749{
1750 pj_bool_t allow_body;
1751 const pjsip_hdr *hdr;
1752
Benny Prijono56315612006-07-18 14:39:40 +00001753 /* Always add User-Agent */
1754 if (pjsua_var.ua_cfg.user_agent.slen &&
1755 tdata->msg->type == PJSIP_REQUEST_MSG)
1756 {
1757 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1758 pjsip_hdr *h;
1759 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
1760 &STR_USER_AGENT,
1761 &pjsua_var.ua_cfg.user_agent);
1762 pjsip_msg_add_hdr(tdata->msg, h);
1763 }
1764
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001765 if (!msg_data)
1766 return;
1767
1768 hdr = msg_data->hdr_list.next;
1769 while (hdr && hdr != &msg_data->hdr_list) {
1770 pjsip_hdr *new_hdr;
1771
Benny Prijonoa1e69682007-05-11 15:14:34 +00001772 new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001773 pjsip_msg_add_hdr(tdata->msg, new_hdr);
1774
1775 hdr = hdr->next;
1776 }
1777
1778 allow_body = (tdata->msg->body == NULL);
1779
1780 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
1781 pjsip_media_type ctype;
1782 pjsip_msg_body *body;
1783
1784 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
1785 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
1786 &msg_data->msg_body);
1787 tdata->msg->body = body;
1788 }
1789}
1790
1791
1792/*
1793 * Add route_set to outgoing requests
1794 */
1795void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
1796 const pjsip_route_hdr *route_set )
1797{
1798 const pjsip_route_hdr *r;
1799
1800 r = route_set->next;
1801 while (r != route_set) {
1802 pjsip_route_hdr *new_r;
1803
Benny Prijonoa1e69682007-05-11 15:14:34 +00001804 new_r = (pjsip_route_hdr*) pjsip_hdr_clone(tdata->pool, r);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001805 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
1806
1807 r = r->next;
1808 }
1809}
1810
1811
1812/*
1813 * Simple version of MIME type parsing (it doesn't support parameters)
1814 */
1815void pjsua_parse_media_type( pj_pool_t *pool,
1816 const pj_str_t *mime,
1817 pjsip_media_type *media_type)
1818{
1819 pj_str_t tmp;
1820 char *pos;
1821
Benny Prijonoac623b32006-07-03 15:19:31 +00001822 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001823
1824 pj_strdup_with_null(pool, &tmp, mime);
1825
1826 pos = pj_strchr(&tmp, '/');
1827 if (pos) {
1828 media_type->type.ptr = tmp.ptr;
1829 media_type->type.slen = (pos-tmp.ptr);
1830 media_type->subtype.ptr = pos+1;
1831 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
1832 } else {
1833 media_type->type = tmp;
1834 }
1835}
1836
1837
1838/*
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001839 * Internal function to init transport selector from transport id.
1840 */
1841void pjsua_init_tpselector(pjsua_transport_id tp_id,
1842 pjsip_tpselector *sel)
1843{
1844 pjsua_transport_data *tpdata;
1845 unsigned flag;
1846
1847 pj_bzero(sel, sizeof(*sel));
1848 if (tp_id == PJSUA_INVALID_ID)
1849 return;
1850
Benny Prijonoa1e69682007-05-11 15:14:34 +00001851 pj_assert(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata));
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001852 tpdata = &pjsua_var.tpdata[tp_id];
1853
1854 flag = pjsip_transport_get_flag_from_type(tpdata->type);
1855
1856 if (flag & PJSIP_TRANSPORT_DATAGRAM) {
1857 sel->type = PJSIP_TPSELECTOR_TRANSPORT;
1858 sel->u.transport = tpdata->data.tp;
1859 } else {
1860 sel->type = PJSIP_TPSELECTOR_LISTENER;
1861 sel->u.listener = tpdata->data.factory;
1862 }
1863}
1864
1865
1866/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001867 * Verify that valid SIP url is given.
1868 */
1869PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
1870{
1871 pjsip_uri *p;
1872 pj_pool_t *pool;
1873 char *url;
1874 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
1875
1876 if (!len) return -1;
1877
1878 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
1879 if (!pool) return -1;
1880
Benny Prijonoa1e69682007-05-11 15:14:34 +00001881 url = (char*) pj_pool_alloc(pool, len+1);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001882 pj_ansi_strcpy(url, c_url);
1883
1884 p = pjsip_parse_uri(pool, url, len, 0);
1885 if (!p || pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0)
1886 p = NULL;
1887
1888 pj_pool_release(pool);
1889 return p ? 0 : -1;
1890}
Benny Prijonoda9785b2007-04-02 20:43:06 +00001891
1892
1893/*
1894 * This is a utility function to dump the stack states to log, using
1895 * verbosity level 3.
1896 */
1897PJ_DEF(void) pjsua_dump(pj_bool_t detail)
1898{
1899 unsigned old_decor;
1900 unsigned i;
1901 char buf[1024];
1902
1903 PJ_LOG(3,(THIS_FILE, "Start dumping application states:"));
1904
1905 old_decor = pj_log_get_decor();
1906 pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
1907
1908 if (detail)
1909 pj_dump_config();
1910
1911 pjsip_endpt_dump(pjsua_get_pjsip_endpt(), detail);
1912
1913 pjmedia_endpt_dump(pjsua_get_pjmedia_endpt());
1914
1915 PJ_LOG(3,(THIS_FILE, "Dumping media transports:"));
1916 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
1917 pjsua_call *call = &pjsua_var.calls[i];
1918 pjmedia_sock_info skinfo;
1919
Benny Prijono4f093d22007-04-09 08:54:32 +00001920 /* MSVC complains about skinfo not being initialized */
1921 pj_bzero(&skinfo, sizeof(skinfo));
1922
Benny Prijonoda9785b2007-04-02 20:43:06 +00001923 pjmedia_transport_get_info(call->med_tp, &skinfo);
1924
1925 PJ_LOG(3,(THIS_FILE, " %s: %s:%d",
1926 (pjsua_var.media_cfg.enable_ice ? "ICE" : "UDP"),
1927 pj_inet_ntoa(skinfo.rtp_addr_name.sin_addr),
1928 (int)pj_ntohs(skinfo.rtp_addr_name.sin_port)));
1929 }
1930
1931 pjsip_tsx_layer_dump(detail);
1932 pjsip_ua_dump(detail);
1933
1934
1935 /* Dump all invite sessions: */
1936 PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:"));
1937
1938 if (pjsua_call_get_count() == 0) {
1939
1940 PJ_LOG(3,(THIS_FILE, " - no sessions -"));
1941
1942 } else {
1943 unsigned i;
1944
1945 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
1946 if (pjsua_call_is_active(i)) {
1947 pjsua_call_dump(i, detail, buf, sizeof(buf), " ");
1948 PJ_LOG(3,(THIS_FILE, "%s", buf));
1949 }
1950 }
1951 }
1952
1953 /* Dump presence status */
1954 pjsua_pres_dump(detail);
1955
1956 pj_log_set_decor(old_decor);
1957 PJ_LOG(3,(THIS_FILE, "Dump complete"));
1958}
1959