blob: 8a6814274fdb7e0973646d143ef979e71629e3f5 [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 Prijonoeebe9af2006-06-13 22:57:13 +000045 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i)
46 pjsua_var.acc[i].index = i;
47
48 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata); ++i)
49 pjsua_var.tpdata[i].index = i;
50}
Benny Prijonodc39fe82006-05-26 12:17:46 +000051
52
Benny Prijonodc39fe82006-05-26 12:17:46 +000053
Benny Prijonoeebe9af2006-06-13 22:57:13 +000054/*****************************************************************************
55 * This is a very simple PJSIP module, whose sole purpose is to display
56 * incoming and outgoing messages to log. This module will have priority
57 * higher than transport layer, which means:
58 *
59 * - incoming messages will come to this module first before reaching
60 * transaction layer.
61 *
62 * - outgoing messages will come to this module last, after the message
63 * has been 'printed' to contiguous buffer by transport layer and
64 * appropriate transport instance has been decided for this message.
65 *
66 */
Benny Prijonodc39fe82006-05-26 12:17:46 +000067
Benny Prijonoeebe9af2006-06-13 22:57:13 +000068/* Notification on incoming messages */
69static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
70{
Benny Prijonob4a17c92006-07-10 14:40:21 +000071 PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
72 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +000073 "--end msg--",
74 rdata->msg_info.len,
75 pjsip_rx_data_get_info(rdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +000076 rdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +000077 rdata->pkt_info.src_name,
78 rdata->pkt_info.src_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +000079 (int)rdata->msg_info.len,
Benny Prijonoeebe9af2006-06-13 22:57:13 +000080 rdata->msg_info.msg_buf));
81
82 /* Always return false, otherwise messages will not get processed! */
83 return PJ_FALSE;
84}
Benny Prijonodc39fe82006-05-26 12:17:46 +000085
Benny Prijonoeebe9af2006-06-13 22:57:13 +000086/* Notification on outgoing messages */
87static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
88{
89
90 /* Important note:
91 * tp_info field is only valid after outgoing messages has passed
92 * transport layer. So don't try to access tp_info when the module
93 * has lower priority than transport layer.
94 */
Benny Prijonodc39fe82006-05-26 12:17:46 +000095
Benny Prijonob4a17c92006-07-10 14:40:21 +000096 PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
97 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +000098 "--end msg--",
99 (tdata->buf.cur - tdata->buf.start),
100 pjsip_tx_data_get_info(tdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000101 tdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000102 tdata->tp_info.dst_name,
103 tdata->tp_info.dst_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000104 (int)(tdata->buf.cur - tdata->buf.start),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000105 tdata->buf.start));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000106
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000107 /* Always return success, otherwise message will not get sent! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000108 return PJ_SUCCESS;
109}
110
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000111/* The module instance. */
112static pjsip_module pjsua_msg_logger =
113{
114 NULL, NULL, /* prev, next. */
115 { "mod-pjsua-log", 13 }, /* Name. */
116 -1, /* Id */
117 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
118 NULL, /* load() */
119 NULL, /* start() */
120 NULL, /* stop() */
121 NULL, /* unload() */
122 &logging_on_rx_msg, /* on_rx_request() */
123 &logging_on_rx_msg, /* on_rx_response() */
124 &logging_on_tx_msg, /* on_tx_request. */
125 &logging_on_tx_msg, /* on_tx_response() */
126 NULL, /* on_tsx_state() */
127
128};
129
130
131/*****************************************************************************
Benny Prijono56315612006-07-18 14:39:40 +0000132 * Another simple module to handle incoming OPTIONS request
133 */
134
135/* Notification on incoming request */
136static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata)
137{
138 pjsip_tx_data *tdata;
139 pjsip_response_addr res_addr;
140 pjmedia_sdp_session *sdp;
141 const pjsip_hdr *cap_hdr;
142 pj_status_t status;
143
144 /* Only want to handle OPTIONS requests */
145 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
146 &pjsip_options_method) != 0)
147 {
148 return PJ_FALSE;
149 }
150
151 /* Create basic response. */
152 status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL,
153 &tdata);
154 if (status != PJ_SUCCESS) {
155 pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status);
156 return PJ_TRUE;
157 }
158
159 /* Add Allow header */
160 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL);
161 if (cap_hdr) {
162 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
163 }
164
165 /* Add Accept header */
166 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL);
167 if (cap_hdr) {
168 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
169 }
170
171 /* Add Supported header */
172 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL);
173 if (cap_hdr) {
174 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
175 }
176
177 /* Add Allow-Events header from the evsub module */
178 cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL);
179 if (cap_hdr) {
180 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
181 }
182
183 /* Add User-Agent header */
184 if (pjsua_var.ua_cfg.user_agent.slen) {
185 const pj_str_t USER_AGENT = { "User-Agent", 10};
186 pjsip_hdr *h;
187
188 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
189 &USER_AGENT,
190 &pjsua_var.ua_cfg.user_agent);
191 pjsip_msg_add_hdr(tdata->msg, h);
192 }
193
194 /* Add SDP body, using call0's RTP address */
195 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
196 &pjsua_var.calls[0].skinfo, &sdp);
197 if (status == PJ_SUCCESS) {
198 pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
199 }
200
201 /* Send response statelessly */
202 pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
203 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL);
204 if (status != PJ_SUCCESS)
205 pjsip_tx_data_dec_ref(tdata);
206
207 return PJ_TRUE;
208}
209
210
211/* The module instance. */
212static pjsip_module pjsua_options_handler =
213{
214 NULL, NULL, /* prev, next. */
215 { "mod-pjsua-options", 17 }, /* Name. */
216 -1, /* Id */
217 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
218 NULL, /* load() */
219 NULL, /* start() */
220 NULL, /* stop() */
221 NULL, /* unload() */
222 &options_on_rx_request, /* on_rx_request() */
223 NULL, /* on_rx_response() */
224 NULL, /* on_tx_request. */
225 NULL, /* on_tx_response() */
226 NULL, /* on_tsx_state() */
227
228};
229
230
231/*****************************************************************************
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000232 * These two functions are the main callbacks registered to PJSIP stack
233 * to receive SIP request and response messages that are outside any
234 * dialogs and any transactions.
235 */
Benny Prijono268ca612006-02-07 12:34:11 +0000236
237/*
238 * Handler for receiving incoming requests.
239 *
240 * This handler serves multiple purposes:
241 * - it receives requests outside dialogs.
242 * - it receives requests inside dialogs, when the requests are
243 * unhandled by other dialog usages. Example of these
244 * requests are: MESSAGE.
245 */
246static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
247{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000248 pj_bool_t processed = PJ_FALSE;
249
250 PJSUA_LOCK();
Benny Prijono38998232006-02-08 22:44:25 +0000251
Benny Prijono84126ab2006-02-09 09:30:09 +0000252 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000253
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000254 processed = pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000255 }
256
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000257 PJSUA_UNLOCK();
258
259 return processed;
Benny Prijono268ca612006-02-07 12:34:11 +0000260}
261
262
263/*
264 * Handler for receiving incoming responses.
265 *
266 * This handler serves multiple purposes:
267 * - it receives strayed responses (i.e. outside any dialog and
268 * outside any transactions).
269 * - it receives responses coming to a transaction, when pjsua
270 * module is set as transaction user for the transaction.
271 * - it receives responses inside a dialog, when these responses
272 * are unhandled by other dialog usages.
273 */
274static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
275{
276 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000277 return PJ_FALSE;
278}
279
280
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000281/*****************************************************************************
282 * Logging.
283 */
284
285/* Log callback */
286static void log_writer(int level, const char *buffer, int len)
Benny Prijono268ca612006-02-07 12:34:11 +0000287{
Benny Prijono572d4852006-11-23 21:50:02 +0000288 /* Write to file, stdout or application callback. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000289
290 if (pjsua_var.log_file) {
291 pj_ssize_t size = len;
292 pj_file_write(pjsua_var.log_file, buffer, &size);
293 }
294
Benny Prijono572d4852006-11-23 21:50:02 +0000295 if (level <= (int)pjsua_var.log_cfg.console_level) {
296 if (pjsua_var.log_cfg.cb)
297 (*pjsua_var.log_cfg.cb)(level, buffer, len);
298 else
299 pj_log_write(level, buffer, len);
300 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000301}
302
303
304/*
305 * Application can call this function at any time (after pjsua_create(), of
306 * course) to change logging settings.
307 */
308PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg)
309{
310 pj_status_t status;
311
312 /* Save config. */
313 pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg);
314
315 /* Redirect log function to ours */
316 pj_log_set_log_func( &log_writer );
317
318 /* Close existing file, if any */
319 if (pjsua_var.log_file) {
320 pj_file_close(pjsua_var.log_file);
321 pjsua_var.log_file = NULL;
322 }
323
324 /* If output log file is desired, create the file: */
325 if (pjsua_var.log_cfg.log_filename.slen) {
326
327 status = pj_file_open(pjsua_var.pool,
328 pjsua_var.log_cfg.log_filename.ptr,
329 PJ_O_WRONLY,
330 &pjsua_var.log_file);
331
332 if (status != PJ_SUCCESS) {
333 pjsua_perror(THIS_FILE, "Error creating log file", status);
334 return status;
335 }
336 }
337
338 /* Unregister msg logging if it's previously registered */
339 if (pjsua_msg_logger.id >= 0) {
340 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
341 pjsua_msg_logger.id = -1;
342 }
343
344 /* Enable SIP message logging */
345 if (pjsua_var.log_cfg.msg_logging)
346 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
347
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000348 return PJ_SUCCESS;
349}
350
351
352/*****************************************************************************
353 * PJSUA Base API.
354 */
355
356/* Worker thread function. */
357static int worker_thread(void *arg)
358{
359 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000360
Benny Prijono268ca612006-02-07 12:34:11 +0000361 PJ_UNUSED_ARG(arg);
362
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000363 while (!pjsua_var.thread_quit_flag) {
364 int count;
365
366 count = pjsua_handle_events(TIMEOUT);
367 if (count < 0)
368 pj_thread_sleep(TIMEOUT);
369 }
Benny Prijono268ca612006-02-07 12:34:11 +0000370
371 return 0;
372}
373
Benny Prijonodc39fe82006-05-26 12:17:46 +0000374
Benny Prijono268ca612006-02-07 12:34:11 +0000375/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000376 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000377 */
378PJ_DEF(pj_status_t) pjsua_create(void)
379{
380 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000381
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000382 /* Init pjsua data */
383 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000384
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000385 /* Set default logging settings */
386 pjsua_logging_config_default(&pjsua_var.log_cfg);
387
388 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000389 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000390 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
391
Benny Prijono268ca612006-02-07 12:34:11 +0000392
Benny Prijonofccab712006-02-22 22:23:22 +0000393 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000394 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000395 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000396
Benny Prijono268ca612006-02-07 12:34:11 +0000397
Benny Prijono094d3ad2006-11-21 08:41:00 +0000398 /* Set default sound device ID */
399 pjsua_var.cap_dev = pjsua_var.play_dev = -1;
400
Benny Prijono268ca612006-02-07 12:34:11 +0000401 /* Init caching pool. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000402 pj_caching_pool_init(&pjsua_var.cp, &pj_pool_factory_default_policy, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000403
404 /* Create memory pool for application. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000405 pjsua_var.pool = pjsua_pool_create("pjsua", 4000, 4000);
406
407 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000408
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000409 /* Create mutex */
410 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
411 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000412 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000413 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000414 return status;
415 }
416
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000417 /* Must create SIP endpoint to initialize SIP parser. The parser
418 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000419 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000420 status = pjsip_endpt_create(&pjsua_var.cp.factory,
421 pj_gethostname()->ptr,
422 &pjsua_var.endpt);
423 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000424
425
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000426 return PJ_SUCCESS;
427}
428
429
430/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000431 * Initialize pjsua with the specified settings. All the settings are
432 * optional, and the default values will be used when the config is not
433 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000434 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000435PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
436 const pjsua_logging_config *log_cfg,
437 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000438{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000439 pjsua_config default_cfg;
440 pjsua_media_config default_media_cfg;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000441 const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
Benny Prijonodc39fe82006-05-26 12:17:46 +0000442 pj_status_t status;
443
444
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000445 /* Create default configurations when the config is not supplied */
446
447 if (ua_cfg == NULL) {
448 pjsua_config_default(&default_cfg);
449 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000450 }
451
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000452 if (media_cfg == NULL) {
453 pjsua_media_config_default(&default_media_cfg);
454 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000455 }
456
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000457 /* Initialize logging first so that info/errors can be captured */
458 if (log_cfg) {
459 status = pjsua_reconfigure_logging(log_cfg);
460 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono9fc735d2006-05-28 14:58:12 +0000461 }
462
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000463 /* If nameserver is configured, create DNS resolver instance and
464 * set it to be used by SIP resolver.
465 */
466 if (ua_cfg->nameserver_count) {
467#if PJSIP_HAS_RESOLVER
468 pj_dns_resolver *resv;
469 unsigned i;
470
471 /* Create DNS resolver */
472 status = pjsip_endpt_create_resolver(pjsua_var.endpt, &resv);
473 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
474
475 /* Configure nameserver for the DNS resolver */
476 status = pj_dns_resolver_set_ns(resv, ua_cfg->nameserver_count,
477 ua_cfg->nameserver, NULL);
478 if (status != PJ_SUCCESS) {
479 pjsua_perror(THIS_FILE, "Error setting nameserver", status);
480 return status;
481 }
482
483 /* Set this DNS resolver to be used by the SIP resolver */
484 status = pjsip_endpt_set_resolver(pjsua_var.endpt, resv);
485 if (status != PJ_SUCCESS) {
486 pjsua_perror(THIS_FILE, "Error setting DNS resolver", status);
487 return status;
488 }
489
490 /* Print nameservers */
491 for (i=0; i<ua_cfg->nameserver_count; ++i) {
492 PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added",
493 (int)ua_cfg->nameserver[i].slen,
494 ua_cfg->nameserver[i].ptr));
495 }
496#else
497 PJ_LOG(2,(THIS_FILE,
498 "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)"));
499#endif
500 }
501
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000502 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000503
504 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000505 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
506 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000507
Benny Prijonodc39fe82006-05-26 12:17:46 +0000508
509 /* Initialize UA layer module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000510 status = pjsip_ua_init_module( pjsua_var.endpt, NULL );
511 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000512
Benny Prijonodc39fe82006-05-26 12:17:46 +0000513
Benny Prijono053f5222006-11-11 16:16:04 +0000514 /* Initialize Replaces support. */
515 status = pjsip_replaces_init_module( pjsua_var.endpt );
516 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
517
518
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000519 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000520 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000521 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000522 {
523 NULL, NULL, /* prev, next. */
524 { "mod-pjsua", 9 }, /* Name. */
525 -1, /* Id */
526 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
527 NULL, /* load() */
528 NULL, /* start() */
529 NULL, /* stop() */
530 NULL, /* unload() */
531 &mod_pjsua_on_rx_request, /* on_rx_request() */
532 &mod_pjsua_on_rx_response, /* on_rx_response() */
533 NULL, /* on_tx_request. */
534 NULL, /* on_tx_response() */
535 NULL, /* on_tsx_state() */
536 };
537
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000538 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000539
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000540 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
541 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000542 }
543
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000544
Benny Prijonodc39fe82006-05-26 12:17:46 +0000545
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000546 /* Initialize PJSUA call subsystem: */
547 status = pjsua_call_subsys_init(ua_cfg);
548 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000549 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000550
551
552 /* Initialize PJSUA media subsystem */
553 status = pjsua_media_subsys_init(media_cfg);
554 if (status != PJ_SUCCESS)
555 goto on_error;
556
Benny Prijonodc39fe82006-05-26 12:17:46 +0000557
558 /* Init core SIMPLE module : */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000559 status = pjsip_evsub_init_module(pjsua_var.endpt);
560 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000561
Benny Prijonodc39fe82006-05-26 12:17:46 +0000562
563 /* Init presence module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000564 status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance());
565 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000566
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000567 /* Init PUBLISH module */
568 pjsip_publishc_init_module(pjsua_var.endpt);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000569
570 /* Init xfer/REFER module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000571 status = pjsip_xfer_init_module( pjsua_var.endpt );
572 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000573
574 /* Init pjsua presence handler: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000575 status = pjsua_pres_init();
576 if (status != PJ_SUCCESS)
577 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000578
579 /* Init out-of-dialog MESSAGE request handler. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000580 status = pjsua_im_init();
581 if (status != PJ_SUCCESS)
582 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000583
Benny Prijonoc8141a82006-08-20 09:12:19 +0000584 /* Register OPTIONS handler */
585 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler);
586
587 /* Add OPTIONS in Allow header */
588 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
589 NULL, 1, &STR_OPTIONS);
590
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000591 /* Start worker thread if needed. */
592 if (pjsua_var.ua_cfg.thread_cnt) {
593 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000594
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000595 if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread))
596 pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000597
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000598 for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) {
599 status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread,
600 NULL, 0, 0, &pjsua_var.thread[i]);
601 if (status != PJ_SUCCESS)
602 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000603 }
Benny Prijonof521eb02006-08-06 23:07:25 +0000604 PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created",
605 pjsua_var.ua_cfg.thread_cnt));
606 } else {
607 PJ_LOG(4,(THIS_FILE, "No SIP worker threads created"));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000608 }
609
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000610 /* Done! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000611
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000612 PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized",
613 PJ_VERSION, PJ_OS_NAME));
Benny Prijonoccf95622006-02-07 18:48:01 +0000614
Benny Prijono268ca612006-02-07 12:34:11 +0000615 return PJ_SUCCESS;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000616
617on_error:
618 pjsua_destroy();
619 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000620}
621
622
Benny Prijono834aee32006-02-19 01:38:06 +0000623/* Sleep with polling */
624static void busy_sleep(unsigned msec)
625{
626 pj_time_val timeout, now;
627
628 pj_gettimeofday(&timeout);
629 timeout.msec += msec;
630 pj_time_val_normalize(&timeout);
631
632 do {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000633 while (pjsua_handle_events(10) > 0)
634 ;
Benny Prijono834aee32006-02-19 01:38:06 +0000635 pj_gettimeofday(&now);
636 } while (PJ_TIME_VAL_LT(now, timeout));
637}
638
Benny Prijono268ca612006-02-07 12:34:11 +0000639/*
640 * Destroy pjsua.
641 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000642PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +0000643{
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000644 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +0000645
646 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000647 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +0000648
649 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000650 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
651 if (pjsua_var.thread[i]) {
652 pj_thread_join(pjsua_var.thread[i]);
653 pj_thread_destroy(pjsua_var.thread[i]);
654 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +0000655 }
Benny Prijono268ca612006-02-07 12:34:11 +0000656 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000657
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000658 if (pjsua_var.endpt) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000659 /* Terminate all calls. */
660 pjsua_call_hangup_all();
661
662 /* Terminate all presence subscriptions. */
663 pjsua_pres_shutdown();
664
665 /* Unregister, if required: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000666 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
667 if (!pjsua_var.acc[i].valid)
668 continue;
669
670 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000671 pjsua_acc_set_registration(i, PJ_FALSE);
672 }
673 }
674
675 /* Wait for some time to allow unregistration to complete: */
Benny Prijono9fc735d2006-05-28 14:58:12 +0000676 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
677 busy_sleep(1000);
678 }
Benny Prijono834aee32006-02-19 01:38:06 +0000679
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000680 /* Destroy media */
681 pjsua_media_subsys_destroy();
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000682
Benny Prijono268ca612006-02-07 12:34:11 +0000683 /* Destroy endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000684 if (pjsua_var.endpt) {
685 pjsip_endpt_destroy(pjsua_var.endpt);
686 pjsua_var.endpt = NULL;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000687 }
Benny Prijono268ca612006-02-07 12:34:11 +0000688
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000689 /* Destroy mutex */
690 if (pjsua_var.mutex) {
691 pj_mutex_destroy(pjsua_var.mutex);
692 pjsua_var.mutex = NULL;
693 }
694
695 /* Destroy pool and pool factory. */
696 if (pjsua_var.pool) {
697 pj_pool_release(pjsua_var.pool);
698 pjsua_var.pool = NULL;
699 pj_caching_pool_destroy(&pjsua_var.cp);
700 }
Benny Prijono268ca612006-02-07 12:34:11 +0000701
702
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000703 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
704
705 /* End logging */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000706 if (pjsua_var.log_file) {
707 pj_file_close(pjsua_var.log_file);
708 pjsua_var.log_file = NULL;
709 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000710
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000711 /* Shutdown PJLIB */
712 pj_shutdown();
713
Benny Prijonof762ee72006-12-01 11:14:37 +0000714 /* Clear pjsua_var */
715 pj_bzero(&pjsua_var, sizeof(pjsua_var));
716
Benny Prijono268ca612006-02-07 12:34:11 +0000717 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000718 return PJ_SUCCESS;
719}
720
721
722/**
723 * Application is recommended to call this function after all initialization
724 * is done, so that the library can do additional checking set up
725 * additional
726 *
727 * @return PJ_SUCCESS on success, or the appropriate error code.
728 */
729PJ_DEF(pj_status_t) pjsua_start(void)
730{
731 pj_status_t status;
732
733 status = pjsua_call_subsys_start();
734 if (status != PJ_SUCCESS)
735 return status;
736
737 status = pjsua_media_subsys_start();
738 if (status != PJ_SUCCESS)
739 return status;
740
741 status = pjsua_pres_start();
742 if (status != PJ_SUCCESS)
743 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000744
745 return PJ_SUCCESS;
746}
747
Benny Prijono9fc735d2006-05-28 14:58:12 +0000748
749/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000750 * Poll pjsua for events, and if necessary block the caller thread for
751 * the specified maximum interval (in miliseconds).
752 */
753PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
754{
755 unsigned count = 0;
756 pj_time_val tv;
757 pj_status_t status;
758
759 tv.sec = 0;
760 tv.msec = msec_timeout;
761 pj_time_val_normalize(&tv);
762
763 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
764
765 if (status != PJ_SUCCESS)
766 return -status;
767
768 return count;
769}
770
771
772/*
773 * Create memory pool.
774 */
775PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
776 pj_size_t increment)
777{
778 /* Pool factory is thread safe, no need to lock */
779 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
780 NULL);
781}
782
783
784/*
785 * Internal function to get SIP endpoint instance of pjsua, which is
786 * needed for example to register module, create transports, etc.
787 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000788 */
789PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
790{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000791 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000792}
793
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000794/*
795 * Internal function to get media endpoint instance.
796 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000797 */
798PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
799{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000800 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000801}
802
Benny Prijono97b87172006-08-24 14:25:14 +0000803/*
804 * Internal function to get PJSUA pool factory.
805 */
806PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
807{
808 return &pjsua_var.cp.factory;
809}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000810
811/*****************************************************************************
812 * PJSUA SIP Transport API.
813 */
814
815/*
816 * Create and initialize SIP socket (and possibly resolve public
817 * address via STUN, depending on config).
818 */
819static pj_status_t create_sip_udp_sock(pj_in_addr bound_addr,
820 int port,
821 pj_bool_t use_stun,
822 const pjsua_stun_config *stun_param,
823 pj_sock_t *p_sock,
824 pj_sockaddr_in *p_pub_addr)
825{
826 pjsua_stun_config stun;
827 pj_sock_t sock;
828 pj_status_t status;
829
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000830 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock);
831 if (status != PJ_SUCCESS) {
832 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +0000833 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000834 }
835
Benny Prijonof90ef4f2007-02-12 14:59:57 +0000836 status = pj_sock_bind_in(sock, pj_ntohl(bound_addr.s_addr),
837 (pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000838 if (status != PJ_SUCCESS) {
839 pjsua_perror(THIS_FILE, "bind() error", status);
840 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +0000841 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000842 }
843
Benny Prijonoe347cb02007-02-14 14:36:13 +0000844 /* If port is zero, get the bound port */
845 if (port == 0) {
846 pj_sockaddr_in bound_addr;
847 int namelen = sizeof(bound_addr);
848 status = pj_sock_getsockname(sock, &bound_addr, &namelen);
849 if (status != PJ_SUCCESS) {
850 pjsua_perror(THIS_FILE, "getsockname() error", status);
851 pj_sock_close(sock);
852 return status;
853 }
854
855 port = pj_ntohs(bound_addr.sin_port);
856 }
857
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000858 /* Copy and normalize STUN param */
859 if (use_stun) {
860 pj_memcpy(&stun, stun_param, sizeof(*stun_param));
861 pjsua_normalize_stun_config(&stun);
862 } else {
Benny Prijonoac623b32006-07-03 15:19:31 +0000863 pj_bzero(&stun, sizeof(pjsua_stun_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000864 }
865
866 /* Get the published address, either by STUN or by resolving
867 * the name of local host.
868 */
869 if (stun.stun_srv1.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +0000870 /*
871 * STUN is specified, resolve the address with STUN.
872 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000873 status = pj_stun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
874 &stun.stun_srv1,
875 stun.stun_port1,
876 &stun.stun_srv2,
877 stun.stun_port2,
878 p_pub_addr);
879 if (status != PJ_SUCCESS) {
880 pjsua_perror(THIS_FILE, "Error resolving with STUN", status);
881 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +0000882 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000883 }
884
Benny Prijono0a5cad82006-09-26 13:21:02 +0000885 } else if (p_pub_addr->sin_addr.s_addr != 0) {
886 /*
887 * Public address is already specified, no need to resolve the
888 * address, only set the port.
889 */
Benny Prijonoe347cb02007-02-14 14:36:13 +0000890 if (p_pub_addr->sin_port == 0)
891 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijono0a5cad82006-09-26 13:21:02 +0000892
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000893 } else {
894
Benny Prijono594e4c52006-09-14 18:51:01 +0000895 pj_bzero(p_pub_addr, sizeof(pj_sockaddr_in));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000896
Benny Prijono594e4c52006-09-14 18:51:01 +0000897 status = pj_gethostip(&p_pub_addr->sin_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000898 if (status != PJ_SUCCESS) {
Benny Prijonob43bad72007-01-20 05:11:08 +0000899 pjsua_perror(THIS_FILE, "Unable to get local host IP", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000900 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +0000901 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000902 }
903
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000904 p_pub_addr->sin_family = PJ_AF_INET;
905 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000906 }
907
908 *p_sock = sock;
909
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000910 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
911 pj_inet_ntoa(p_pub_addr->sin_addr),
912 (int)pj_ntohs(p_pub_addr->sin_port)));
913
Benny Prijonod8410532006-06-15 11:04:33 +0000914 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000915}
916
917
918/*
919 * Create SIP transport.
920 */
921PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
922 const pjsua_transport_config *cfg,
923 pjsua_transport_id *p_id)
924{
925 pjsip_transport *tp;
926 unsigned id;
927 pj_status_t status;
928
929 PJSUA_LOCK();
930
931 /* Find empty transport slot */
932 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +0000933 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000934 break;
935 }
936
937 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
938 status = PJ_ETOOMANY;
939 pjsua_perror(THIS_FILE, "Error creating transport", status);
940 goto on_return;
941 }
942
943 /* Create the transport */
944 if (type == PJSIP_TRANSPORT_UDP) {
Benny Prijonoe93e2872006-06-28 16:46:49 +0000945 /*
946 * Create UDP transport.
947 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000948 pjsua_transport_config config;
Benny Prijonod8410532006-06-15 11:04:33 +0000949 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono0a5cad82006-09-26 13:21:02 +0000950 pj_sockaddr_in bound_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000951 pj_sockaddr_in pub_addr;
952 pjsip_host_port addr_name;
953
954 /* Supply default config if it's not specified */
955 if (cfg == NULL) {
956 pjsua_transport_config_default(&config);
957 cfg = &config;
958 }
959
Benny Prijono0a5cad82006-09-26 13:21:02 +0000960 /* Initialize bound address, if any */
961 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
962 if (cfg->bound_addr.slen) {
963 status = pj_sockaddr_in_set_str_addr(&bound_addr,&cfg->bound_addr);
964 if (status != PJ_SUCCESS) {
965 pjsua_perror(THIS_FILE,
966 "Unable to resolve transport bound address",
967 status);
968 goto on_return;
969 }
970 }
971
972 /* Initialize the public address from the config, if any */
973 pj_sockaddr_in_init(&pub_addr, NULL, (pj_uint16_t)cfg->port);
974 if (cfg->public_addr.slen) {
975 status = pj_sockaddr_in_set_str_addr(&pub_addr, &cfg->public_addr);
976 if (status != PJ_SUCCESS) {
977 pjsua_perror(THIS_FILE,
978 "Unable to resolve transport public address",
979 status);
980 goto on_return;
981 }
982 }
983
984 /* Create the socket and possibly resolve the address with STUN
985 * (only when public address is not specified).
986 */
987 status = create_sip_udp_sock(bound_addr.sin_addr, cfg->port,
988 cfg->use_stun, &cfg->stun_config,
989 &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000990 if (status != PJ_SUCCESS)
991 goto on_return;
992
993 addr_name.host = pj_str(pj_inet_ntoa(pub_addr.sin_addr));
994 addr_name.port = pj_ntohs(pub_addr.sin_port);
995
996 /* Create UDP transport */
997 status = pjsip_udp_transport_attach( pjsua_var.endpt, sock,
998 &addr_name, 1,
999 &tp);
1000 if (status != PJ_SUCCESS) {
1001 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
1002 status);
1003 pj_sock_close(sock);
1004 goto on_return;
1005 }
1006
Benny Prijonoe93e2872006-06-28 16:46:49 +00001007
1008 /* Save the transport */
1009 pjsua_var.tpdata[id].type = type;
1010 pjsua_var.tpdata[id].local_name = tp->local_name;
1011 pjsua_var.tpdata[id].data.tp = tp;
1012
1013 } else if (type == PJSIP_TRANSPORT_TCP) {
1014 /*
1015 * Create TCP transport.
1016 */
1017 pjsua_transport_config config;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001018 pjsip_host_port a_name;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001019 pjsip_tpfactory *tcp;
1020 pj_sockaddr_in local_addr;
1021
1022 /* Supply default config if it's not specified */
1023 if (cfg == NULL) {
1024 pjsua_transport_config_default(&config);
1025 cfg = &config;
1026 }
1027
1028 /* Init local address */
1029 pj_sockaddr_in_init(&local_addr, 0, 0);
1030
1031 if (cfg->port)
1032 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1033
Benny Prijono0a5cad82006-09-26 13:21:02 +00001034 if (cfg->bound_addr.slen) {
1035 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1036 if (status != PJ_SUCCESS) {
1037 pjsua_perror(THIS_FILE,
1038 "Unable to resolve transport bound address",
1039 status);
1040 goto on_return;
1041 }
1042 }
1043
1044 /* Init published name */
1045 pj_bzero(&a_name, sizeof(pjsip_host_port));
1046 if (cfg->public_addr.slen)
1047 a_name.host = cfg->public_addr;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001048
1049 /* Create the TCP transport */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001050 status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr,
1051 &a_name, 1, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001052
1053 if (status != PJ_SUCCESS) {
1054 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1055 status);
1056 goto on_return;
1057 }
1058
1059 /* Save the transport */
1060 pjsua_var.tpdata[id].type = type;
1061 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1062 pjsua_var.tpdata[id].data.factory = tcp;
1063
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001064#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1065 } else if (type == PJSIP_TRANSPORT_TLS) {
1066 /*
1067 * Create TLS transport.
1068 */
Benny Prijonof3bbc132006-12-25 06:43:59 +00001069 /*
1070 * Create TCP transport.
1071 */
1072 pjsua_transport_config config;
1073 pjsip_host_port a_name;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001074 pjsip_tpfactory *tls;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001075 pj_sockaddr_in local_addr;
1076
1077 /* Supply default config if it's not specified */
1078 if (cfg == NULL) {
1079 pjsua_transport_config_default(&config);
1080 config.port = 5061;
1081 cfg = &config;
1082 }
1083
1084 /* Init local address */
1085 pj_sockaddr_in_init(&local_addr, 0, 0);
1086
1087 if (cfg->port)
1088 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1089
1090 if (cfg->bound_addr.slen) {
1091 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1092 if (status != PJ_SUCCESS) {
1093 pjsua_perror(THIS_FILE,
1094 "Unable to resolve transport bound address",
1095 status);
1096 goto on_return;
1097 }
1098 }
1099
1100 /* Init published name */
1101 pj_bzero(&a_name, sizeof(pjsip_host_port));
1102 if (cfg->public_addr.slen)
1103 a_name.host = cfg->public_addr;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001104
1105 status = pjsip_tls_transport_start(pjsua_var.endpt,
Benny Prijonof3bbc132006-12-25 06:43:59 +00001106 &cfg->tls_setting,
1107 &local_addr, &a_name, 1, &tls);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001108 if (status != PJ_SUCCESS) {
1109 pjsua_perror(THIS_FILE, "Error creating SIP TLS listener",
1110 status);
1111 goto on_return;
1112 }
1113
1114 /* Save the transport */
1115 pjsua_var.tpdata[id].type = type;
1116 pjsua_var.tpdata[id].local_name = tls->addr_name;
1117 pjsua_var.tpdata[id].data.factory = tls;
1118#endif
1119
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001120 } else {
1121 status = PJSIP_EUNSUPTRANSPORT;
1122 pjsua_perror(THIS_FILE, "Error creating transport", status);
1123 goto on_return;
1124 }
1125
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001126
1127 /* Return the ID */
1128 if (p_id) *p_id = id;
1129
1130 status = PJ_SUCCESS;
1131
1132on_return:
1133
1134 PJSUA_UNLOCK();
1135
Benny Prijonod8410532006-06-15 11:04:33 +00001136 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001137}
1138
1139
1140/*
1141 * Register transport that has been created by application.
1142 */
1143PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1144 pjsua_transport_id *p_id)
1145{
1146 unsigned id;
1147
1148 PJSUA_LOCK();
1149
1150 /* Find empty transport slot */
1151 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001152 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001153 break;
1154 }
1155
1156 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1157 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1158 PJSUA_UNLOCK();
1159 return PJ_ETOOMANY;
1160 }
1161
1162 /* Save the transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001163 pjsua_var.tpdata[id].type = tp->key.type;
1164 pjsua_var.tpdata[id].local_name = tp->local_name;
1165 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001166
1167 /* Return the ID */
1168 if (p_id) *p_id = id;
1169
1170 PJSUA_UNLOCK();
1171
1172 return PJ_SUCCESS;
1173}
1174
1175
1176/*
1177 * Enumerate all transports currently created in the system.
1178 */
1179PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1180 unsigned *p_count )
1181{
1182 unsigned i, count;
1183
1184 PJSUA_LOCK();
1185
1186 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
1187 ++i)
1188 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001189 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001190 continue;
1191
1192 id[count++] = i;
1193 }
1194
1195 *p_count = count;
1196
1197 PJSUA_UNLOCK();
1198
1199 return PJ_SUCCESS;
1200}
1201
1202
1203/*
1204 * Get information about transports.
1205 */
1206PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
1207 pjsua_transport_info *info)
1208{
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001209 pjsua_transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00001210 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001211
Benny Prijonoac623b32006-07-03 15:19:31 +00001212 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001213
1214 /* Make sure id is in range. */
1215 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1216
1217 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001218 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001219
1220 PJSUA_LOCK();
1221
Benny Prijonoe93e2872006-06-28 16:46:49 +00001222 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
1223
1224 pjsip_transport *tp = t->data.tp;
1225
1226 if (tp == NULL) {
1227 PJSUA_UNLOCK();
1228 return PJ_EINVALIDOP;
1229 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001230
Benny Prijonoe93e2872006-06-28 16:46:49 +00001231 info->id = id;
1232 info->type = tp->key.type;
1233 info->type_name = pj_str(tp->type_name);
1234 info->info = pj_str(tp->info);
1235 info->flag = tp->flag;
1236 info->addr_len = tp->addr_len;
1237 info->local_addr = tp->local_addr;
1238 info->local_name = tp->local_name;
1239 info->usage_count = pj_atomic_get(tp->ref_cnt);
1240
Benny Prijono0a5cad82006-09-26 13:21:02 +00001241 status = PJ_SUCCESS;
1242
Benny Prijonoe93e2872006-06-28 16:46:49 +00001243 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
1244
1245 pjsip_tpfactory *factory = t->data.factory;
1246
1247 if (factory == NULL) {
1248 PJSUA_UNLOCK();
1249 return PJ_EINVALIDOP;
1250 }
1251
1252 info->id = id;
1253 info->type = t->type;
1254 info->type_name = pj_str("TCP");
1255 info->info = pj_str("TCP transport");
1256 info->flag = factory->flag;
1257 info->addr_len = sizeof(factory->local_addr);
1258 info->local_addr = factory->local_addr;
1259 info->local_name = factory->addr_name;
1260 info->usage_count = 0;
1261
Benny Prijono0a5cad82006-09-26 13:21:02 +00001262 status = PJ_SUCCESS;
1263
1264 } else {
1265 pj_assert(!"Unsupported transport");
1266 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001267 }
1268
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001269
1270 PJSUA_UNLOCK();
1271
Benny Prijono0a5cad82006-09-26 13:21:02 +00001272 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001273}
1274
1275
1276/*
1277 * Disable a transport or re-enable it.
1278 */
1279PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
1280 pj_bool_t enabled)
1281{
1282 /* Make sure id is in range. */
1283 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1284
1285 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001286 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001287
1288
1289 /* To be done!! */
1290 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001291 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001292
1293 return PJ_EINVALIDOP;
1294}
1295
1296
1297/*
1298 * Close the transport.
1299 */
1300PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
1301 pj_bool_t force )
1302{
Benny Prijono5ff61872007-02-01 03:37:11 +00001303 pj_status_t status;
1304
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001305 /* Make sure id is in range. */
1306 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1307
1308 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001309 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001310
Benny Prijono0a5cad82006-09-26 13:21:02 +00001311 /* Note: destroy() may not work if there are objects still referencing
1312 * the transport.
1313 */
1314 if (force) {
1315 switch (pjsua_var.tpdata[id].type) {
1316 case PJSIP_TRANSPORT_UDP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001317 status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
1318 if (status != PJ_SUCCESS)
1319 return status;
1320 status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
1321 if (status != PJ_SUCCESS)
1322 return status;
1323 break;
1324
1325 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001326 case PJSIP_TRANSPORT_TCP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001327 /* This will close the TCP listener, but existing TCP/TLS
1328 * connections (if any) will still linger
1329 */
1330 status = (*pjsua_var.tpdata[id].data.factory->destroy)
1331 (pjsua_var.tpdata[id].data.factory);
1332 if (status != PJ_SUCCESS)
1333 return status;
1334
Benny Prijono0a5cad82006-09-26 13:21:02 +00001335 break;
Benny Prijono5ff61872007-02-01 03:37:11 +00001336
Benny Prijono1ebd6142006-10-19 15:48:02 +00001337 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001338 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001339 }
1340
1341 } else {
Benny Prijono5ff61872007-02-01 03:37:11 +00001342 /* If force is not specified, transports will be closed at their
1343 * convenient time. However this will leak PJSUA-API transport
1344 * descriptors as PJSUA-API wouldn't know when exactly the
1345 * transport is closed thus it can't cleanup PJSUA transport
1346 * descriptor.
1347 */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001348 switch (pjsua_var.tpdata[id].type) {
1349 case PJSIP_TRANSPORT_UDP:
1350 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
Benny Prijono5ff61872007-02-01 03:37:11 +00001351 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001352 case PJSIP_TRANSPORT_TCP:
1353 return (*pjsua_var.tpdata[id].data.factory->destroy)
1354 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001355 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001356 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001357 }
1358 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001359
Benny Prijono5ff61872007-02-01 03:37:11 +00001360 /* Cleanup pjsua data when force is applied */
1361 if (force) {
1362 pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED;
1363 pjsua_var.tpdata[id].data.ptr = NULL;
1364 }
1365
1366 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001367}
1368
1369
1370/*
1371 * Add additional headers etc in msg_data specified by application
1372 * when sending requests.
1373 */
1374void pjsua_process_msg_data(pjsip_tx_data *tdata,
1375 const pjsua_msg_data *msg_data)
1376{
1377 pj_bool_t allow_body;
1378 const pjsip_hdr *hdr;
1379
Benny Prijono56315612006-07-18 14:39:40 +00001380 /* Always add User-Agent */
1381 if (pjsua_var.ua_cfg.user_agent.slen &&
1382 tdata->msg->type == PJSIP_REQUEST_MSG)
1383 {
1384 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1385 pjsip_hdr *h;
1386 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
1387 &STR_USER_AGENT,
1388 &pjsua_var.ua_cfg.user_agent);
1389 pjsip_msg_add_hdr(tdata->msg, h);
1390 }
1391
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001392 if (!msg_data)
1393 return;
1394
1395 hdr = msg_data->hdr_list.next;
1396 while (hdr && hdr != &msg_data->hdr_list) {
1397 pjsip_hdr *new_hdr;
1398
1399 new_hdr = pjsip_hdr_clone(tdata->pool, hdr);
1400 pjsip_msg_add_hdr(tdata->msg, new_hdr);
1401
1402 hdr = hdr->next;
1403 }
1404
1405 allow_body = (tdata->msg->body == NULL);
1406
1407 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
1408 pjsip_media_type ctype;
1409 pjsip_msg_body *body;
1410
1411 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
1412 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
1413 &msg_data->msg_body);
1414 tdata->msg->body = body;
1415 }
1416}
1417
1418
1419/*
1420 * Add route_set to outgoing requests
1421 */
1422void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
1423 const pjsip_route_hdr *route_set )
1424{
1425 const pjsip_route_hdr *r;
1426
1427 r = route_set->next;
1428 while (r != route_set) {
1429 pjsip_route_hdr *new_r;
1430
1431 new_r = pjsip_hdr_clone(tdata->pool, r);
1432 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
1433
1434 r = r->next;
1435 }
1436}
1437
1438
1439/*
1440 * Simple version of MIME type parsing (it doesn't support parameters)
1441 */
1442void pjsua_parse_media_type( pj_pool_t *pool,
1443 const pj_str_t *mime,
1444 pjsip_media_type *media_type)
1445{
1446 pj_str_t tmp;
1447 char *pos;
1448
Benny Prijonoac623b32006-07-03 15:19:31 +00001449 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001450
1451 pj_strdup_with_null(pool, &tmp, mime);
1452
1453 pos = pj_strchr(&tmp, '/');
1454 if (pos) {
1455 media_type->type.ptr = tmp.ptr;
1456 media_type->type.slen = (pos-tmp.ptr);
1457 media_type->subtype.ptr = pos+1;
1458 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
1459 } else {
1460 media_type->type = tmp;
1461 }
1462}
1463
1464
1465/*
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001466 * Internal function to init transport selector from transport id.
1467 */
1468void pjsua_init_tpselector(pjsua_transport_id tp_id,
1469 pjsip_tpselector *sel)
1470{
1471 pjsua_transport_data *tpdata;
1472 unsigned flag;
1473
1474 pj_bzero(sel, sizeof(*sel));
1475 if (tp_id == PJSUA_INVALID_ID)
1476 return;
1477
1478 pj_assert(tp_id >= 0 && tp_id < PJ_ARRAY_SIZE(pjsua_var.tpdata));
1479 tpdata = &pjsua_var.tpdata[tp_id];
1480
1481 flag = pjsip_transport_get_flag_from_type(tpdata->type);
1482
1483 if (flag & PJSIP_TRANSPORT_DATAGRAM) {
1484 sel->type = PJSIP_TPSELECTOR_TRANSPORT;
1485 sel->u.transport = tpdata->data.tp;
1486 } else {
1487 sel->type = PJSIP_TPSELECTOR_LISTENER;
1488 sel->u.listener = tpdata->data.factory;
1489 }
1490}
1491
1492
1493/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001494 * Verify that valid SIP url is given.
1495 */
1496PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
1497{
1498 pjsip_uri *p;
1499 pj_pool_t *pool;
1500 char *url;
1501 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
1502
1503 if (!len) return -1;
1504
1505 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
1506 if (!pool) return -1;
1507
1508 url = pj_pool_alloc(pool, len+1);
1509 pj_ansi_strcpy(url, c_url);
1510
1511 p = pjsip_parse_uri(pool, url, len, 0);
1512 if (!p || pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0)
1513 p = NULL;
1514
1515 pj_pool_release(pool);
1516 return p ? 0 : -1;
1517}