blob: f7a3b07a2697d2bc3ae457637511a378a8aa6921 [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 Prijonodc39fe82006-05-26 12:17:46 +000057
Benny Prijonoeebe9af2006-06-13 22:57:13 +000058/*****************************************************************************
59 * This is a very simple PJSIP module, whose sole purpose is to display
60 * incoming and outgoing messages to log. This module will have priority
61 * higher than transport layer, which means:
62 *
63 * - incoming messages will come to this module first before reaching
64 * transaction layer.
65 *
66 * - outgoing messages will come to this module last, after the message
67 * has been 'printed' to contiguous buffer by transport layer and
68 * appropriate transport instance has been decided for this message.
69 *
70 */
Benny Prijonodc39fe82006-05-26 12:17:46 +000071
Benny Prijonoeebe9af2006-06-13 22:57:13 +000072/* Notification on incoming messages */
73static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
74{
Benny Prijonob4a17c92006-07-10 14:40:21 +000075 PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
76 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +000077 "--end msg--",
78 rdata->msg_info.len,
79 pjsip_rx_data_get_info(rdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +000080 rdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +000081 rdata->pkt_info.src_name,
82 rdata->pkt_info.src_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +000083 (int)rdata->msg_info.len,
Benny Prijonoeebe9af2006-06-13 22:57:13 +000084 rdata->msg_info.msg_buf));
85
86 /* Always return false, otherwise messages will not get processed! */
87 return PJ_FALSE;
88}
Benny Prijonodc39fe82006-05-26 12:17:46 +000089
Benny Prijonoeebe9af2006-06-13 22:57:13 +000090/* Notification on outgoing messages */
91static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
92{
93
94 /* Important note:
95 * tp_info field is only valid after outgoing messages has passed
96 * transport layer. So don't try to access tp_info when the module
97 * has lower priority than transport layer.
98 */
Benny Prijonodc39fe82006-05-26 12:17:46 +000099
Benny Prijonob4a17c92006-07-10 14:40:21 +0000100 PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
101 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000102 "--end msg--",
103 (tdata->buf.cur - tdata->buf.start),
104 pjsip_tx_data_get_info(tdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000105 tdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000106 tdata->tp_info.dst_name,
107 tdata->tp_info.dst_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000108 (int)(tdata->buf.cur - tdata->buf.start),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000109 tdata->buf.start));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000110
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000111 /* Always return success, otherwise message will not get sent! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000112 return PJ_SUCCESS;
113}
114
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000115/* The module instance. */
116static pjsip_module pjsua_msg_logger =
117{
118 NULL, NULL, /* prev, next. */
119 { "mod-pjsua-log", 13 }, /* Name. */
120 -1, /* Id */
121 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
122 NULL, /* load() */
123 NULL, /* start() */
124 NULL, /* stop() */
125 NULL, /* unload() */
126 &logging_on_rx_msg, /* on_rx_request() */
127 &logging_on_rx_msg, /* on_rx_response() */
128 &logging_on_tx_msg, /* on_tx_request. */
129 &logging_on_tx_msg, /* on_tx_response() */
130 NULL, /* on_tsx_state() */
131
132};
133
134
135/*****************************************************************************
Benny Prijono56315612006-07-18 14:39:40 +0000136 * Another simple module to handle incoming OPTIONS request
137 */
138
139/* Notification on incoming request */
140static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata)
141{
142 pjsip_tx_data *tdata;
143 pjsip_response_addr res_addr;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000144 pjmedia_sock_info skinfo;
Benny Prijono56315612006-07-18 14:39:40 +0000145 pjmedia_sdp_session *sdp;
146 const pjsip_hdr *cap_hdr;
147 pj_status_t status;
148
149 /* Only want to handle OPTIONS requests */
150 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
151 &pjsip_options_method) != 0)
152 {
153 return PJ_FALSE;
154 }
155
156 /* Create basic response. */
157 status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL,
158 &tdata);
159 if (status != PJ_SUCCESS) {
160 pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status);
161 return PJ_TRUE;
162 }
163
164 /* Add Allow header */
165 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL);
166 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000167 pjsip_msg_add_hdr(tdata->msg,
168 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000169 }
170
171 /* Add Accept header */
172 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL);
173 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000174 pjsip_msg_add_hdr(tdata->msg,
175 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000176 }
177
178 /* Add Supported header */
179 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL);
180 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000181 pjsip_msg_add_hdr(tdata->msg,
182 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000183 }
184
185 /* Add Allow-Events header from the evsub module */
186 cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL);
187 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000188 pjsip_msg_add_hdr(tdata->msg,
189 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000190 }
191
192 /* Add User-Agent header */
193 if (pjsua_var.ua_cfg.user_agent.slen) {
194 const pj_str_t USER_AGENT = { "User-Agent", 10};
195 pjsip_hdr *h;
196
197 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
198 &USER_AGENT,
199 &pjsua_var.ua_cfg.user_agent);
200 pjsip_msg_add_hdr(tdata->msg, h);
201 }
202
Benny Prijono617c5bc2007-04-02 19:51:21 +0000203 /* Get media socket info */
204 pjmedia_transport_get_info(pjsua_var.calls[0].med_tp, &skinfo);
205
Benny Prijono56315612006-07-18 14:39:40 +0000206 /* Add SDP body, using call0's RTP address */
207 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000208 &skinfo, &sdp);
Benny Prijono56315612006-07-18 14:39:40 +0000209 if (status == PJ_SUCCESS) {
210 pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
211 }
212
213 /* Send response statelessly */
214 pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
215 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL);
216 if (status != PJ_SUCCESS)
217 pjsip_tx_data_dec_ref(tdata);
218
219 return PJ_TRUE;
220}
221
222
223/* The module instance. */
224static pjsip_module pjsua_options_handler =
225{
226 NULL, NULL, /* prev, next. */
227 { "mod-pjsua-options", 17 }, /* Name. */
228 -1, /* Id */
229 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
230 NULL, /* load() */
231 NULL, /* start() */
232 NULL, /* stop() */
233 NULL, /* unload() */
234 &options_on_rx_request, /* on_rx_request() */
235 NULL, /* on_rx_response() */
236 NULL, /* on_tx_request. */
237 NULL, /* on_tx_response() */
238 NULL, /* on_tsx_state() */
239
240};
241
242
243/*****************************************************************************
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000244 * These two functions are the main callbacks registered to PJSIP stack
245 * to receive SIP request and response messages that are outside any
246 * dialogs and any transactions.
247 */
Benny Prijono268ca612006-02-07 12:34:11 +0000248
249/*
250 * Handler for receiving incoming requests.
251 *
252 * This handler serves multiple purposes:
253 * - it receives requests outside dialogs.
254 * - it receives requests inside dialogs, when the requests are
255 * unhandled by other dialog usages. Example of these
256 * requests are: MESSAGE.
257 */
258static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
259{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000260 pj_bool_t processed = PJ_FALSE;
261
262 PJSUA_LOCK();
Benny Prijono38998232006-02-08 22:44:25 +0000263
Benny Prijono84126ab2006-02-09 09:30:09 +0000264 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000265
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000266 processed = pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000267 }
268
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000269 PJSUA_UNLOCK();
270
271 return processed;
Benny Prijono268ca612006-02-07 12:34:11 +0000272}
273
274
275/*
276 * Handler for receiving incoming responses.
277 *
278 * This handler serves multiple purposes:
279 * - it receives strayed responses (i.e. outside any dialog and
280 * outside any transactions).
281 * - it receives responses coming to a transaction, when pjsua
282 * module is set as transaction user for the transaction.
283 * - it receives responses inside a dialog, when these responses
284 * are unhandled by other dialog usages.
285 */
286static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
287{
288 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000289 return PJ_FALSE;
290}
291
292
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000293/*****************************************************************************
294 * Logging.
295 */
296
297/* Log callback */
298static void log_writer(int level, const char *buffer, int len)
Benny Prijono268ca612006-02-07 12:34:11 +0000299{
Benny Prijono572d4852006-11-23 21:50:02 +0000300 /* Write to file, stdout or application callback. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000301
302 if (pjsua_var.log_file) {
303 pj_ssize_t size = len;
304 pj_file_write(pjsua_var.log_file, buffer, &size);
Benny Prijono980ca0a2007-04-03 17:55:08 +0000305 /* This will slow things down considerably! Don't do it!
306 pj_file_flush(pjsua_var.log_file);
307 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000308 }
309
Benny Prijono572d4852006-11-23 21:50:02 +0000310 if (level <= (int)pjsua_var.log_cfg.console_level) {
311 if (pjsua_var.log_cfg.cb)
312 (*pjsua_var.log_cfg.cb)(level, buffer, len);
313 else
314 pj_log_write(level, buffer, len);
315 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000316}
317
318
319/*
320 * Application can call this function at any time (after pjsua_create(), of
321 * course) to change logging settings.
322 */
323PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg)
324{
325 pj_status_t status;
326
327 /* Save config. */
328 pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg);
329
330 /* Redirect log function to ours */
331 pj_log_set_log_func( &log_writer );
332
333 /* Close existing file, if any */
334 if (pjsua_var.log_file) {
335 pj_file_close(pjsua_var.log_file);
336 pjsua_var.log_file = NULL;
337 }
338
339 /* If output log file is desired, create the file: */
340 if (pjsua_var.log_cfg.log_filename.slen) {
341
342 status = pj_file_open(pjsua_var.pool,
343 pjsua_var.log_cfg.log_filename.ptr,
344 PJ_O_WRONLY,
345 &pjsua_var.log_file);
346
347 if (status != PJ_SUCCESS) {
348 pjsua_perror(THIS_FILE, "Error creating log file", status);
349 return status;
350 }
351 }
352
353 /* Unregister msg logging if it's previously registered */
354 if (pjsua_msg_logger.id >= 0) {
355 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
356 pjsua_msg_logger.id = -1;
357 }
358
359 /* Enable SIP message logging */
360 if (pjsua_var.log_cfg.msg_logging)
361 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
362
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000363 return PJ_SUCCESS;
364}
365
366
367/*****************************************************************************
368 * PJSUA Base API.
369 */
370
371/* Worker thread function. */
372static int worker_thread(void *arg)
373{
374 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000375
Benny Prijono268ca612006-02-07 12:34:11 +0000376 PJ_UNUSED_ARG(arg);
377
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000378 while (!pjsua_var.thread_quit_flag) {
379 int count;
380
381 count = pjsua_handle_events(TIMEOUT);
382 if (count < 0)
383 pj_thread_sleep(TIMEOUT);
384 }
Benny Prijono268ca612006-02-07 12:34:11 +0000385
386 return 0;
387}
388
Benny Prijonodc39fe82006-05-26 12:17:46 +0000389
Benny Prijono268ca612006-02-07 12:34:11 +0000390/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000391 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000392 */
393PJ_DEF(pj_status_t) pjsua_create(void)
394{
395 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000396
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000397 /* Init pjsua data */
398 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000399
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000400 /* Set default logging settings */
401 pjsua_logging_config_default(&pjsua_var.log_cfg);
402
403 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000404 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000405 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
406
Benny Prijono268ca612006-02-07 12:34:11 +0000407
Benny Prijonofccab712006-02-22 22:23:22 +0000408 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000409 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000410 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000411
Benny Prijonoec921342007-03-24 13:00:30 +0000412 /* Init PJNATH */
413 status = pjnath_init();
414 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono268ca612006-02-07 12:34:11 +0000415
Benny Prijono094d3ad2006-11-21 08:41:00 +0000416 /* Set default sound device ID */
417 pjsua_var.cap_dev = pjsua_var.play_dev = -1;
418
Benny Prijono268ca612006-02-07 12:34:11 +0000419 /* Init caching pool. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000420 pj_caching_pool_init(&pjsua_var.cp, &pj_pool_factory_default_policy, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000421
422 /* Create memory pool for application. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000423 pjsua_var.pool = pjsua_pool_create("pjsua", 4000, 4000);
424
425 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000426
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000427 /* Create mutex */
428 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
429 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000430 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000431 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000432 return status;
433 }
434
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000435 /* Must create SIP endpoint to initialize SIP parser. The parser
436 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000437 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000438 status = pjsip_endpt_create(&pjsua_var.cp.factory,
439 pj_gethostname()->ptr,
440 &pjsua_var.endpt);
441 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000442
443
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000444 return PJ_SUCCESS;
445}
446
447
448/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000449 * Initialize pjsua with the specified settings. All the settings are
450 * optional, and the default values will be used when the config is not
451 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000452 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000453PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
454 const pjsua_logging_config *log_cfg,
455 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000456{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000457 pjsua_config default_cfg;
458 pjsua_media_config default_media_cfg;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000459 const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
Benny Prijonodc39fe82006-05-26 12:17:46 +0000460 pj_status_t status;
461
462
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000463 /* Create default configurations when the config is not supplied */
464
465 if (ua_cfg == NULL) {
466 pjsua_config_default(&default_cfg);
467 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000468 }
469
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000470 if (media_cfg == NULL) {
471 pjsua_media_config_default(&default_media_cfg);
472 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000473 }
474
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000475 /* Initialize logging first so that info/errors can be captured */
476 if (log_cfg) {
477 status = pjsua_reconfigure_logging(log_cfg);
478 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono9fc735d2006-05-28 14:58:12 +0000479 }
480
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000481 /* If nameserver is configured, create DNS resolver instance and
482 * set it to be used by SIP resolver.
483 */
484 if (ua_cfg->nameserver_count) {
485#if PJSIP_HAS_RESOLVER
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000486 unsigned i;
487
488 /* Create DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000489 status = pjsip_endpt_create_resolver(pjsua_var.endpt,
490 &pjsua_var.resolver);
Benny Prijono318f3842007-05-15 20:27:08 +0000491 if (status != PJ_SUCCESS) {
492 pjsua_perror(THIS_FILE, "Error creating resolver", status);
493 return status;
494 }
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000495
496 /* Configure nameserver for the DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000497 status = pj_dns_resolver_set_ns(pjsua_var.resolver,
498 ua_cfg->nameserver_count,
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000499 ua_cfg->nameserver, NULL);
500 if (status != PJ_SUCCESS) {
501 pjsua_perror(THIS_FILE, "Error setting nameserver", status);
502 return status;
503 }
504
505 /* Set this DNS resolver to be used by the SIP resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000506 status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000507 if (status != PJ_SUCCESS) {
508 pjsua_perror(THIS_FILE, "Error setting DNS resolver", status);
509 return status;
510 }
511
512 /* Print nameservers */
513 for (i=0; i<ua_cfg->nameserver_count; ++i) {
514 PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added",
515 (int)ua_cfg->nameserver[i].slen,
516 ua_cfg->nameserver[i].ptr));
517 }
518#else
519 PJ_LOG(2,(THIS_FILE,
520 "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)"));
521#endif
522 }
523
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000524 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000525
526 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000527 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
528 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000529
Benny Prijonodc39fe82006-05-26 12:17:46 +0000530
531 /* Initialize UA layer module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000532 status = pjsip_ua_init_module( pjsua_var.endpt, NULL );
533 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000534
Benny Prijonodc39fe82006-05-26 12:17:46 +0000535
Benny Prijono053f5222006-11-11 16:16:04 +0000536 /* Initialize Replaces support. */
537 status = pjsip_replaces_init_module( pjsua_var.endpt );
538 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
539
540
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000541 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000542 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000543 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000544 {
545 NULL, NULL, /* prev, next. */
546 { "mod-pjsua", 9 }, /* Name. */
547 -1, /* Id */
548 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
549 NULL, /* load() */
550 NULL, /* start() */
551 NULL, /* stop() */
552 NULL, /* unload() */
553 &mod_pjsua_on_rx_request, /* on_rx_request() */
554 &mod_pjsua_on_rx_response, /* on_rx_response() */
555 NULL, /* on_tx_request. */
556 NULL, /* on_tx_response() */
557 NULL, /* on_tsx_state() */
558 };
559
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000560 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000561
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000562 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
563 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000564 }
565
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000566
Benny Prijonodc39fe82006-05-26 12:17:46 +0000567
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000568 /* Initialize PJSUA call subsystem: */
569 status = pjsua_call_subsys_init(ua_cfg);
570 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000571 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000572
573
Benny Prijonoc97608e2007-03-23 16:34:20 +0000574 /* Start resolving STUN server */
575 status = pjsua_resolve_stun_server(PJ_FALSE);
576 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
577 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
578 return status;
579 }
580
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000581 /* Initialize PJSUA media subsystem */
582 status = pjsua_media_subsys_init(media_cfg);
583 if (status != PJ_SUCCESS)
584 goto on_error;
585
Benny Prijonodc39fe82006-05-26 12:17:46 +0000586
587 /* Init core SIMPLE module : */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000588 status = pjsip_evsub_init_module(pjsua_var.endpt);
589 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000590
Benny Prijonodc39fe82006-05-26 12:17:46 +0000591
592 /* Init presence module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000593 status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance());
594 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000595
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000596 /* Init PUBLISH module */
597 pjsip_publishc_init_module(pjsua_var.endpt);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000598
599 /* Init xfer/REFER module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000600 status = pjsip_xfer_init_module( pjsua_var.endpt );
601 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000602
603 /* Init pjsua presence handler: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000604 status = pjsua_pres_init();
605 if (status != PJ_SUCCESS)
606 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000607
608 /* Init out-of-dialog MESSAGE request handler. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000609 status = pjsua_im_init();
610 if (status != PJ_SUCCESS)
611 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000612
Benny Prijonoc8141a82006-08-20 09:12:19 +0000613 /* Register OPTIONS handler */
614 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler);
615
616 /* Add OPTIONS in Allow header */
617 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
618 NULL, 1, &STR_OPTIONS);
619
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000620 /* Start worker thread if needed. */
621 if (pjsua_var.ua_cfg.thread_cnt) {
622 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000623
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000624 if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread))
625 pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000626
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000627 for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) {
628 status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread,
629 NULL, 0, 0, &pjsua_var.thread[i]);
630 if (status != PJ_SUCCESS)
631 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000632 }
Benny Prijonof521eb02006-08-06 23:07:25 +0000633 PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created",
634 pjsua_var.ua_cfg.thread_cnt));
635 } else {
636 PJ_LOG(4,(THIS_FILE, "No SIP worker threads created"));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000637 }
638
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000639 /* Done! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000640
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000641 PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized",
642 PJ_VERSION, PJ_OS_NAME));
Benny Prijonoccf95622006-02-07 18:48:01 +0000643
Benny Prijono268ca612006-02-07 12:34:11 +0000644 return PJ_SUCCESS;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000645
646on_error:
647 pjsua_destroy();
648 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000649}
650
651
Benny Prijono834aee32006-02-19 01:38:06 +0000652/* Sleep with polling */
653static void busy_sleep(unsigned msec)
654{
Benny Prijonob2c96822007-05-03 13:31:21 +0000655#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
Benny Prijono897f9f82007-05-03 19:56:21 +0000656 /* Ideally we shouldn't call pj_thread_sleep() and rather
657 * CActiveScheduler::WaitForAnyRequest() here, but that will
658 * drag in Symbian header and it doesn't look pretty.
659 */
Benny Prijonob2c96822007-05-03 13:31:21 +0000660 pj_thread_sleep(msec);
661#else
Benny Prijono834aee32006-02-19 01:38:06 +0000662 pj_time_val timeout, now;
663
664 pj_gettimeofday(&timeout);
665 timeout.msec += msec;
666 pj_time_val_normalize(&timeout);
667
668 do {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000669 while (pjsua_handle_events(10) > 0)
670 ;
Benny Prijono834aee32006-02-19 01:38:06 +0000671 pj_gettimeofday(&now);
672 } while (PJ_TIME_VAL_LT(now, timeout));
Benny Prijonob2c96822007-05-03 13:31:21 +0000673#endif
Benny Prijono834aee32006-02-19 01:38:06 +0000674}
675
Benny Prijonoebbf6892007-03-24 17:37:25 +0000676
Benny Prijonoebbf6892007-03-24 17:37:25 +0000677/*
678 * Callback function to receive notification from the resolver
679 * when the resolution process completes.
680 */
681static void stun_dns_srv_resolver_cb(void *user_data,
682 pj_status_t status,
683 const pj_dns_srv_record *rec)
684{
685 unsigned i;
686
687 PJ_UNUSED_ARG(user_data);
688
689 pjsua_var.stun_status = status;
690
691 if (status != PJ_SUCCESS) {
692 /* DNS SRV resolution failed. If stun_host is specified, resolve
693 * it with gethostbyname()
694 */
695 if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000696 pj_str_t str_host, str_port;
697 int port;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000698 pj_hostent he;
699
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000700 str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':');
701 if (str_port.ptr != NULL) {
702 str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr;
703 str_host.slen = (str_port.ptr - str_host.ptr);
704 str_port.ptr++;
705 str_port.slen = pjsua_var.ua_cfg.stun_host.slen -
706 str_host.slen - 1;
707 port = (int)pj_strtoul(&str_port);
708 if (port < 1 || port > 65535) {
709 pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL);
710 pjsua_var.stun_status = PJ_EINVAL;
711 return;
712 }
713 } else {
714 str_host = pjsua_var.ua_cfg.stun_host;
715 port = 3478;
716 }
717
718 pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
Benny Prijonoebbf6892007-03-24 17:37:25 +0000719
720 if (pjsua_var.stun_status == PJ_SUCCESS) {
721 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
722 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000723 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoebbf6892007-03-24 17:37:25 +0000724
725 PJ_LOG(3,(THIS_FILE,
726 "STUN server %.*s resolved, address is %s:%d",
727 (int)pjsua_var.ua_cfg.stun_host.slen,
728 pjsua_var.ua_cfg.stun_host.ptr,
729 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
730 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
731 }
732 } else {
733 char errmsg[PJ_ERR_MSG_SIZE];
734
735 pj_strerror(status, errmsg, sizeof(errmsg));
736 PJ_LOG(1,(THIS_FILE,
737 "DNS SRV resolution failed for STUN server %.*s: %s",
738 (int)pjsua_var.ua_cfg.stun_domain.slen,
739 pjsua_var.ua_cfg.stun_domain.ptr,
740 errmsg));
741 }
742 return;
743 }
744
745 pj_memcpy(&pjsua_var.stun_srv, &rec->entry[0].addr,
746 rec->entry[0].addr_len);
747
748 PJ_LOG(3,(THIS_FILE, "_stun._udp.%.*s resolved, found %d entry(s):",
749 (int)pjsua_var.ua_cfg.stun_domain.slen,
750 pjsua_var.ua_cfg.stun_domain.ptr,
751 rec->count));
752
753 for (i=0; i<rec->count; ++i) {
754 PJ_LOG(3,(THIS_FILE,
755 " %d: prio=%d, weight=%d %s:%d",
756 i, rec->entry[i].priority, rec->entry[i].weight,
757 pj_inet_ntoa(rec->entry[i].addr.ipv4.sin_addr),
758 (int)pj_ntohs(rec->entry[i].addr.ipv4.sin_port)));
759 }
760
761}
762
Benny Prijono268ca612006-02-07 12:34:11 +0000763/*
Benny Prijonoc97608e2007-03-23 16:34:20 +0000764 * Resolve STUN server.
765 */
766pj_status_t pjsua_resolve_stun_server(pj_bool_t wait)
767{
768 if (pjsua_var.stun_status == PJ_EUNKNOWN) {
769 /* Initialize STUN configuration */
770 pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0,
771 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
772 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
773
774 /* Start STUN server resolution */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000775
776 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000777
Benny Prijonoebbf6892007-03-24 17:37:25 +0000778 /* If stun_domain is specified, resolve STUN servers with DNS
779 * SRV resolution.
780 */
781 if (pjsua_var.ua_cfg.stun_domain.slen) {
782 pj_str_t res_type;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000783 pj_status_t status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000784
785 /* Fail if resolver is not configured */
786 if (pjsua_var.resolver == NULL) {
787 PJ_LOG(1,(THIS_FILE, "Nameserver must be configured when "
788 "stun_domain is specified"));
789 pjsua_var.stun_status = PJLIB_UTIL_EDNSNONS;
790 return PJLIB_UTIL_EDNSNONS;
791 }
792 res_type = pj_str("_stun._udp");
Benny Prijonoda9785b2007-04-02 20:43:06 +0000793 status =
Benny Prijonoebbf6892007-03-24 17:37:25 +0000794 pj_dns_srv_resolve(&pjsua_var.ua_cfg.stun_domain, &res_type,
795 3478, pjsua_var.pool, pjsua_var.resolver,
Benny Prijonoda9785b2007-04-02 20:43:06 +0000796 0, NULL, &stun_dns_srv_resolver_cb);
797 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +0000798 pjsua_perror(THIS_FILE, "Error starting DNS SRV resolution",
799 pjsua_var.stun_status);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000800 pjsua_var.stun_status = status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000801 return pjsua_var.stun_status;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000802 } else {
803 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000804 }
805 }
806 /* Otherwise if stun_host is specified, resolve STUN server with
807 * gethostbyname().
808 */
809 else if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000810 pj_str_t str_host, str_port;
811 int port;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000812 pj_hostent he;
813
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000814 str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':');
815 if (str_port.ptr != NULL) {
816 str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr;
817 str_host.slen = (str_port.ptr - str_host.ptr);
818 str_port.ptr++;
819 str_port.slen = pjsua_var.ua_cfg.stun_host.slen -
820 str_host.slen - 1;
821 port = (int)pj_strtoul(&str_port);
822 if (port < 1 || port > 65535) {
823 pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL);
824 pjsua_var.stun_status = PJ_EINVAL;
825 return pjsua_var.stun_status;
826 }
827 } else {
828 str_host = pjsua_var.ua_cfg.stun_host;
829 port = 3478;
830 }
831
832
833 pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000834
835 if (pjsua_var.stun_status == PJ_SUCCESS) {
836 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
837 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000838 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonof75eceb2007-03-23 19:09:54 +0000839
Benny Prijonoebbf6892007-03-24 17:37:25 +0000840 PJ_LOG(3,(THIS_FILE,
Benny Prijonof75eceb2007-03-23 19:09:54 +0000841 "STUN server %.*s resolved, address is %s:%d",
Benny Prijonoebbf6892007-03-24 17:37:25 +0000842 (int)pjsua_var.ua_cfg.stun_host.slen,
843 pjsua_var.ua_cfg.stun_host.ptr,
Benny Prijonof75eceb2007-03-23 19:09:54 +0000844 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
845 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
Benny Prijonoc97608e2007-03-23 16:34:20 +0000846 }
Benny Prijonoebbf6892007-03-24 17:37:25 +0000847
Benny Prijonoc97608e2007-03-23 16:34:20 +0000848 }
Benny Prijonoebbf6892007-03-24 17:37:25 +0000849 /* Otherwise disable STUN. */
850 else {
851 pjsua_var.stun_status = PJ_SUCCESS;
852 }
853
854
Benny Prijonoc97608e2007-03-23 16:34:20 +0000855 return pjsua_var.stun_status;
856
857 } else if (pjsua_var.stun_status == PJ_EPENDING) {
858 /* STUN server resolution has been started, wait for the
859 * result.
860 */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000861 if (wait) {
862 while (pjsua_var.stun_status == PJ_EPENDING)
863 pjsua_handle_events(10);
864 }
865
866 return pjsua_var.stun_status;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000867
868 } else {
869 /* STUN server has been resolved, return the status */
870 return pjsua_var.stun_status;
871 }
872}
873
874/*
Benny Prijono268ca612006-02-07 12:34:11 +0000875 * Destroy pjsua.
876 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000877PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +0000878{
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000879 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +0000880
881 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000882 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +0000883
884 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000885 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
886 if (pjsua_var.thread[i]) {
887 pj_thread_join(pjsua_var.thread[i]);
888 pj_thread_destroy(pjsua_var.thread[i]);
889 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +0000890 }
Benny Prijono268ca612006-02-07 12:34:11 +0000891 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000892
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000893 if (pjsua_var.endpt) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000894 /* Terminate all calls. */
895 pjsua_call_hangup_all();
896
897 /* Terminate all presence subscriptions. */
898 pjsua_pres_shutdown();
899
900 /* Unregister, if required: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000901 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
902 if (!pjsua_var.acc[i].valid)
903 continue;
904
905 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000906 pjsua_acc_set_registration(i, PJ_FALSE);
907 }
908 }
909
910 /* Wait for some time to allow unregistration to complete: */
Benny Prijono9fc735d2006-05-28 14:58:12 +0000911 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
912 busy_sleep(1000);
913 }
Benny Prijono834aee32006-02-19 01:38:06 +0000914
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000915 /* Destroy media */
916 pjsua_media_subsys_destroy();
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000917
Benny Prijono268ca612006-02-07 12:34:11 +0000918 /* Destroy endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000919 if (pjsua_var.endpt) {
920 pjsip_endpt_destroy(pjsua_var.endpt);
921 pjsua_var.endpt = NULL;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000922 }
Benny Prijono268ca612006-02-07 12:34:11 +0000923
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000924 /* Destroy mutex */
925 if (pjsua_var.mutex) {
926 pj_mutex_destroy(pjsua_var.mutex);
927 pjsua_var.mutex = NULL;
928 }
929
930 /* Destroy pool and pool factory. */
931 if (pjsua_var.pool) {
932 pj_pool_release(pjsua_var.pool);
933 pjsua_var.pool = NULL;
934 pj_caching_pool_destroy(&pjsua_var.cp);
Benny Prijonoad2e0ca2007-04-29 12:31:51 +0000935
936 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
937
938 /* End logging */
939 if (pjsua_var.log_file) {
940 pj_file_close(pjsua_var.log_file);
941 pjsua_var.log_file = NULL;
942 }
943
944 /* Shutdown PJLIB */
945 pj_shutdown();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000946 }
Benny Prijono268ca612006-02-07 12:34:11 +0000947
Benny Prijonof762ee72006-12-01 11:14:37 +0000948 /* Clear pjsua_var */
949 pj_bzero(&pjsua_var, sizeof(pjsua_var));
950
Benny Prijono268ca612006-02-07 12:34:11 +0000951 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000952 return PJ_SUCCESS;
953}
954
955
956/**
957 * Application is recommended to call this function after all initialization
958 * is done, so that the library can do additional checking set up
959 * additional
960 *
961 * @return PJ_SUCCESS on success, or the appropriate error code.
962 */
963PJ_DEF(pj_status_t) pjsua_start(void)
964{
965 pj_status_t status;
966
967 status = pjsua_call_subsys_start();
968 if (status != PJ_SUCCESS)
969 return status;
970
971 status = pjsua_media_subsys_start();
972 if (status != PJ_SUCCESS)
973 return status;
974
975 status = pjsua_pres_start();
976 if (status != PJ_SUCCESS)
977 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000978
979 return PJ_SUCCESS;
980}
981
Benny Prijono9fc735d2006-05-28 14:58:12 +0000982
983/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000984 * Poll pjsua for events, and if necessary block the caller thread for
985 * the specified maximum interval (in miliseconds).
986 */
987PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
988{
Benny Prijono897f9f82007-05-03 19:56:21 +0000989#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
990 /* Ideally we shouldn't call pj_thread_sleep() and rather
991 * CActiveScheduler::WaitForAnyRequest() here, but that will
992 * drag in Symbian header and it doesn't look pretty.
993 */
994 pj_thread_sleep(msec_timeout);
995 return msec_timeout;
996#else
997
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000998 unsigned count = 0;
999 pj_time_val tv;
1000 pj_status_t status;
1001
1002 tv.sec = 0;
1003 tv.msec = msec_timeout;
1004 pj_time_val_normalize(&tv);
1005
1006 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
1007
1008 if (status != PJ_SUCCESS)
1009 return -status;
1010
1011 return count;
Benny Prijono897f9f82007-05-03 19:56:21 +00001012#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001013}
1014
1015
1016/*
1017 * Create memory pool.
1018 */
1019PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
1020 pj_size_t increment)
1021{
1022 /* Pool factory is thread safe, no need to lock */
1023 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
1024 NULL);
1025}
1026
1027
1028/*
1029 * Internal function to get SIP endpoint instance of pjsua, which is
1030 * needed for example to register module, create transports, etc.
1031 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001032 */
1033PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
1034{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001035 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001036}
1037
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001038/*
1039 * Internal function to get media endpoint instance.
1040 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001041 */
1042PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
1043{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001044 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001045}
1046
Benny Prijono97b87172006-08-24 14:25:14 +00001047/*
1048 * Internal function to get PJSUA pool factory.
1049 */
1050PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
1051{
1052 return &pjsua_var.cp.factory;
1053}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001054
1055/*****************************************************************************
1056 * PJSUA SIP Transport API.
1057 */
1058
1059/*
1060 * Create and initialize SIP socket (and possibly resolve public
1061 * address via STUN, depending on config).
1062 */
1063static pj_status_t create_sip_udp_sock(pj_in_addr bound_addr,
1064 int port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001065 pj_sock_t *p_sock,
1066 pj_sockaddr_in *p_pub_addr)
1067{
Benny Prijonoc97608e2007-03-23 16:34:20 +00001068 char ip_addr[32];
1069 pj_str_t stun_srv;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001070 pj_sock_t sock;
1071 pj_status_t status;
1072
Benny Prijonoc97608e2007-03-23 16:34:20 +00001073 /* Make sure STUN server resolution has completed */
1074 status = pjsua_resolve_stun_server(PJ_TRUE);
1075 if (status != PJ_SUCCESS) {
1076 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
1077 return status;
1078 }
1079
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001080 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock);
1081 if (status != PJ_SUCCESS) {
1082 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +00001083 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001084 }
1085
Benny Prijonof90ef4f2007-02-12 14:59:57 +00001086 status = pj_sock_bind_in(sock, pj_ntohl(bound_addr.s_addr),
1087 (pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001088 if (status != PJ_SUCCESS) {
1089 pjsua_perror(THIS_FILE, "bind() error", status);
1090 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001091 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001092 }
1093
Benny Prijonoe347cb02007-02-14 14:36:13 +00001094 /* If port is zero, get the bound port */
1095 if (port == 0) {
1096 pj_sockaddr_in bound_addr;
1097 int namelen = sizeof(bound_addr);
1098 status = pj_sock_getsockname(sock, &bound_addr, &namelen);
1099 if (status != PJ_SUCCESS) {
1100 pjsua_perror(THIS_FILE, "getsockname() error", status);
1101 pj_sock_close(sock);
1102 return status;
1103 }
1104
1105 port = pj_ntohs(bound_addr.sin_port);
1106 }
1107
Benny Prijonoc97608e2007-03-23 16:34:20 +00001108 if (pjsua_var.stun_srv.addr.sa_family != 0) {
1109 pj_ansi_strcpy(ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
1110 stun_srv = pj_str(ip_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001111 } else {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001112 stun_srv.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001113 }
1114
1115 /* Get the published address, either by STUN or by resolving
1116 * the name of local host.
1117 */
Benny Prijonoc97608e2007-03-23 16:34:20 +00001118 if (stun_srv.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +00001119 /*
1120 * STUN is specified, resolve the address with STUN.
1121 */
Benny Prijono14c2b862007-02-21 00:40:05 +00001122 status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001123 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
1124 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijonoc97608e2007-03-23 16:34:20 +00001125 p_pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001126 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +00001127 pjsua_perror(THIS_FILE, "Error contacting STUN server", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001128 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001129 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001130 }
1131
Benny Prijono0a5cad82006-09-26 13:21:02 +00001132 } else if (p_pub_addr->sin_addr.s_addr != 0) {
1133 /*
1134 * Public address is already specified, no need to resolve the
1135 * address, only set the port.
1136 */
Benny Prijonoe347cb02007-02-14 14:36:13 +00001137 if (p_pub_addr->sin_port == 0)
1138 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001139
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001140 } else {
1141
Benny Prijono594e4c52006-09-14 18:51:01 +00001142 pj_bzero(p_pub_addr, sizeof(pj_sockaddr_in));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001143
Benny Prijono594e4c52006-09-14 18:51:01 +00001144 status = pj_gethostip(&p_pub_addr->sin_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001145 if (status != PJ_SUCCESS) {
Benny Prijonob43bad72007-01-20 05:11:08 +00001146 pjsua_perror(THIS_FILE, "Unable to get local host IP", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001147 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001148 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001149 }
1150
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001151 p_pub_addr->sin_family = PJ_AF_INET;
1152 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001153 }
1154
1155 *p_sock = sock;
1156
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001157 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
1158 pj_inet_ntoa(p_pub_addr->sin_addr),
1159 (int)pj_ntohs(p_pub_addr->sin_port)));
1160
Benny Prijonod8410532006-06-15 11:04:33 +00001161 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001162}
1163
1164
1165/*
1166 * Create SIP transport.
1167 */
1168PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
1169 const pjsua_transport_config *cfg,
1170 pjsua_transport_id *p_id)
1171{
1172 pjsip_transport *tp;
1173 unsigned id;
1174 pj_status_t status;
1175
1176 PJSUA_LOCK();
1177
1178 /* Find empty transport slot */
1179 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001180 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001181 break;
1182 }
1183
1184 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1185 status = PJ_ETOOMANY;
1186 pjsua_perror(THIS_FILE, "Error creating transport", status);
1187 goto on_return;
1188 }
1189
1190 /* Create the transport */
1191 if (type == PJSIP_TRANSPORT_UDP) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001192 /*
1193 * Create UDP transport.
1194 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001195 pjsua_transport_config config;
Benny Prijonod8410532006-06-15 11:04:33 +00001196 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001197 pj_sockaddr_in bound_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001198 pj_sockaddr_in pub_addr;
1199 pjsip_host_port addr_name;
1200
1201 /* Supply default config if it's not specified */
1202 if (cfg == NULL) {
1203 pjsua_transport_config_default(&config);
1204 cfg = &config;
1205 }
1206
Benny Prijono0a5cad82006-09-26 13:21:02 +00001207 /* Initialize bound address, if any */
1208 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
1209 if (cfg->bound_addr.slen) {
1210 status = pj_sockaddr_in_set_str_addr(&bound_addr,&cfg->bound_addr);
1211 if (status != PJ_SUCCESS) {
1212 pjsua_perror(THIS_FILE,
1213 "Unable to resolve transport bound address",
1214 status);
1215 goto on_return;
1216 }
1217 }
1218
1219 /* Initialize the public address from the config, if any */
1220 pj_sockaddr_in_init(&pub_addr, NULL, (pj_uint16_t)cfg->port);
1221 if (cfg->public_addr.slen) {
1222 status = pj_sockaddr_in_set_str_addr(&pub_addr, &cfg->public_addr);
1223 if (status != PJ_SUCCESS) {
1224 pjsua_perror(THIS_FILE,
1225 "Unable to resolve transport public address",
1226 status);
1227 goto on_return;
1228 }
1229 }
1230
1231 /* Create the socket and possibly resolve the address with STUN
1232 * (only when public address is not specified).
1233 */
1234 status = create_sip_udp_sock(bound_addr.sin_addr, cfg->port,
Benny Prijono0a5cad82006-09-26 13:21:02 +00001235 &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001236 if (status != PJ_SUCCESS)
1237 goto on_return;
1238
1239 addr_name.host = pj_str(pj_inet_ntoa(pub_addr.sin_addr));
1240 addr_name.port = pj_ntohs(pub_addr.sin_port);
1241
1242 /* Create UDP transport */
1243 status = pjsip_udp_transport_attach( pjsua_var.endpt, sock,
1244 &addr_name, 1,
1245 &tp);
1246 if (status != PJ_SUCCESS) {
1247 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
1248 status);
1249 pj_sock_close(sock);
1250 goto on_return;
1251 }
1252
Benny Prijonoe93e2872006-06-28 16:46:49 +00001253
1254 /* Save the transport */
1255 pjsua_var.tpdata[id].type = type;
1256 pjsua_var.tpdata[id].local_name = tp->local_name;
1257 pjsua_var.tpdata[id].data.tp = tp;
1258
Benny Prijono3569c0d2007-04-06 10:29:20 +00001259#if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0
1260
Benny Prijonoe93e2872006-06-28 16:46:49 +00001261 } else if (type == PJSIP_TRANSPORT_TCP) {
1262 /*
1263 * Create TCP transport.
1264 */
1265 pjsua_transport_config config;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001266 pjsip_host_port a_name;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001267 pjsip_tpfactory *tcp;
1268 pj_sockaddr_in local_addr;
1269
1270 /* Supply default config if it's not specified */
1271 if (cfg == NULL) {
1272 pjsua_transport_config_default(&config);
1273 cfg = &config;
1274 }
1275
1276 /* Init local address */
1277 pj_sockaddr_in_init(&local_addr, 0, 0);
1278
1279 if (cfg->port)
1280 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1281
Benny Prijono0a5cad82006-09-26 13:21:02 +00001282 if (cfg->bound_addr.slen) {
1283 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1284 if (status != PJ_SUCCESS) {
1285 pjsua_perror(THIS_FILE,
1286 "Unable to resolve transport bound address",
1287 status);
1288 goto on_return;
1289 }
1290 }
1291
1292 /* Init published name */
1293 pj_bzero(&a_name, sizeof(pjsip_host_port));
1294 if (cfg->public_addr.slen)
1295 a_name.host = cfg->public_addr;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001296
1297 /* Create the TCP transport */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001298 status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr,
1299 &a_name, 1, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001300
1301 if (status != PJ_SUCCESS) {
1302 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1303 status);
1304 goto on_return;
1305 }
1306
1307 /* Save the transport */
1308 pjsua_var.tpdata[id].type = type;
1309 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1310 pjsua_var.tpdata[id].data.factory = tcp;
1311
Benny Prijono3569c0d2007-04-06 10:29:20 +00001312#endif /* PJ_HAS_TCP */
1313
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001314#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1315 } else if (type == PJSIP_TRANSPORT_TLS) {
1316 /*
1317 * Create TLS transport.
1318 */
Benny Prijonof3bbc132006-12-25 06:43:59 +00001319 /*
1320 * Create TCP transport.
1321 */
1322 pjsua_transport_config config;
1323 pjsip_host_port a_name;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001324 pjsip_tpfactory *tls;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001325 pj_sockaddr_in local_addr;
1326
1327 /* Supply default config if it's not specified */
1328 if (cfg == NULL) {
1329 pjsua_transport_config_default(&config);
1330 config.port = 5061;
1331 cfg = &config;
1332 }
1333
1334 /* Init local address */
1335 pj_sockaddr_in_init(&local_addr, 0, 0);
1336
1337 if (cfg->port)
1338 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1339
1340 if (cfg->bound_addr.slen) {
1341 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1342 if (status != PJ_SUCCESS) {
1343 pjsua_perror(THIS_FILE,
1344 "Unable to resolve transport bound address",
1345 status);
1346 goto on_return;
1347 }
1348 }
1349
1350 /* Init published name */
1351 pj_bzero(&a_name, sizeof(pjsip_host_port));
1352 if (cfg->public_addr.slen)
1353 a_name.host = cfg->public_addr;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001354
1355 status = pjsip_tls_transport_start(pjsua_var.endpt,
Benny Prijonof3bbc132006-12-25 06:43:59 +00001356 &cfg->tls_setting,
1357 &local_addr, &a_name, 1, &tls);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001358 if (status != PJ_SUCCESS) {
1359 pjsua_perror(THIS_FILE, "Error creating SIP TLS listener",
1360 status);
1361 goto on_return;
1362 }
1363
1364 /* Save the transport */
1365 pjsua_var.tpdata[id].type = type;
1366 pjsua_var.tpdata[id].local_name = tls->addr_name;
1367 pjsua_var.tpdata[id].data.factory = tls;
1368#endif
1369
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001370 } else {
1371 status = PJSIP_EUNSUPTRANSPORT;
1372 pjsua_perror(THIS_FILE, "Error creating transport", status);
1373 goto on_return;
1374 }
1375
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001376
1377 /* Return the ID */
1378 if (p_id) *p_id = id;
1379
1380 status = PJ_SUCCESS;
1381
1382on_return:
1383
1384 PJSUA_UNLOCK();
1385
Benny Prijonod8410532006-06-15 11:04:33 +00001386 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001387}
1388
1389
1390/*
1391 * Register transport that has been created by application.
1392 */
1393PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1394 pjsua_transport_id *p_id)
1395{
1396 unsigned id;
1397
1398 PJSUA_LOCK();
1399
1400 /* Find empty transport slot */
1401 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001402 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001403 break;
1404 }
1405
1406 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1407 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1408 PJSUA_UNLOCK();
1409 return PJ_ETOOMANY;
1410 }
1411
1412 /* Save the transport */
Benny Prijonod17a5e92007-05-29 11:51:45 +00001413 pjsua_var.tpdata[id].type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001414 pjsua_var.tpdata[id].local_name = tp->local_name;
1415 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001416
1417 /* Return the ID */
1418 if (p_id) *p_id = id;
1419
1420 PJSUA_UNLOCK();
1421
1422 return PJ_SUCCESS;
1423}
1424
1425
1426/*
1427 * Enumerate all transports currently created in the system.
1428 */
1429PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1430 unsigned *p_count )
1431{
1432 unsigned i, count;
1433
1434 PJSUA_LOCK();
1435
1436 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
1437 ++i)
1438 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001439 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001440 continue;
1441
1442 id[count++] = i;
1443 }
1444
1445 *p_count = count;
1446
1447 PJSUA_UNLOCK();
1448
1449 return PJ_SUCCESS;
1450}
1451
1452
1453/*
1454 * Get information about transports.
1455 */
1456PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
1457 pjsua_transport_info *info)
1458{
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001459 pjsua_transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00001460 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001461
Benny Prijonoac623b32006-07-03 15:19:31 +00001462 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001463
1464 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001465 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1466 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001467
1468 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001469 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001470
1471 PJSUA_LOCK();
1472
Benny Prijonoe93e2872006-06-28 16:46:49 +00001473 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
1474
1475 pjsip_transport *tp = t->data.tp;
1476
1477 if (tp == NULL) {
1478 PJSUA_UNLOCK();
1479 return PJ_EINVALIDOP;
1480 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001481
Benny Prijonoe93e2872006-06-28 16:46:49 +00001482 info->id = id;
Benny Prijonod17a5e92007-05-29 11:51:45 +00001483 info->type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001484 info->type_name = pj_str(tp->type_name);
1485 info->info = pj_str(tp->info);
1486 info->flag = tp->flag;
1487 info->addr_len = tp->addr_len;
1488 info->local_addr = tp->local_addr;
1489 info->local_name = tp->local_name;
1490 info->usage_count = pj_atomic_get(tp->ref_cnt);
1491
Benny Prijono0a5cad82006-09-26 13:21:02 +00001492 status = PJ_SUCCESS;
1493
Benny Prijonoe93e2872006-06-28 16:46:49 +00001494 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
1495
1496 pjsip_tpfactory *factory = t->data.factory;
1497
1498 if (factory == NULL) {
1499 PJSUA_UNLOCK();
1500 return PJ_EINVALIDOP;
1501 }
1502
1503 info->id = id;
1504 info->type = t->type;
1505 info->type_name = pj_str("TCP");
1506 info->info = pj_str("TCP transport");
1507 info->flag = factory->flag;
1508 info->addr_len = sizeof(factory->local_addr);
1509 info->local_addr = factory->local_addr;
1510 info->local_name = factory->addr_name;
1511 info->usage_count = 0;
1512
Benny Prijono0a5cad82006-09-26 13:21:02 +00001513 status = PJ_SUCCESS;
1514
1515 } else {
1516 pj_assert(!"Unsupported transport");
1517 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001518 }
1519
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001520
1521 PJSUA_UNLOCK();
1522
Benny Prijono0a5cad82006-09-26 13:21:02 +00001523 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001524}
1525
1526
1527/*
1528 * Disable a transport or re-enable it.
1529 */
1530PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
1531 pj_bool_t enabled)
1532{
1533 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001534 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1535 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001536
1537 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001538 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001539
1540
1541 /* To be done!! */
1542 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001543 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001544
1545 return PJ_EINVALIDOP;
1546}
1547
1548
1549/*
1550 * Close the transport.
1551 */
1552PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
1553 pj_bool_t force )
1554{
Benny Prijono5ff61872007-02-01 03:37:11 +00001555 pj_status_t status;
1556
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001557 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001558 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1559 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001560
1561 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001562 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001563
Benny Prijono0a5cad82006-09-26 13:21:02 +00001564 /* Note: destroy() may not work if there are objects still referencing
1565 * the transport.
1566 */
1567 if (force) {
1568 switch (pjsua_var.tpdata[id].type) {
1569 case PJSIP_TRANSPORT_UDP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001570 status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
1571 if (status != PJ_SUCCESS)
1572 return status;
1573 status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
1574 if (status != PJ_SUCCESS)
1575 return status;
1576 break;
1577
1578 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001579 case PJSIP_TRANSPORT_TCP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001580 /* This will close the TCP listener, but existing TCP/TLS
1581 * connections (if any) will still linger
1582 */
1583 status = (*pjsua_var.tpdata[id].data.factory->destroy)
1584 (pjsua_var.tpdata[id].data.factory);
1585 if (status != PJ_SUCCESS)
1586 return status;
1587
Benny Prijono0a5cad82006-09-26 13:21:02 +00001588 break;
Benny Prijono5ff61872007-02-01 03:37:11 +00001589
Benny Prijono1ebd6142006-10-19 15:48:02 +00001590 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001591 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001592 }
1593
1594 } else {
Benny Prijono5ff61872007-02-01 03:37:11 +00001595 /* If force is not specified, transports will be closed at their
1596 * convenient time. However this will leak PJSUA-API transport
1597 * descriptors as PJSUA-API wouldn't know when exactly the
1598 * transport is closed thus it can't cleanup PJSUA transport
1599 * descriptor.
1600 */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001601 switch (pjsua_var.tpdata[id].type) {
1602 case PJSIP_TRANSPORT_UDP:
1603 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
Benny Prijono5ff61872007-02-01 03:37:11 +00001604 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001605 case PJSIP_TRANSPORT_TCP:
1606 return (*pjsua_var.tpdata[id].data.factory->destroy)
1607 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001608 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001609 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001610 }
1611 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001612
Benny Prijono5ff61872007-02-01 03:37:11 +00001613 /* Cleanup pjsua data when force is applied */
1614 if (force) {
1615 pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED;
1616 pjsua_var.tpdata[id].data.ptr = NULL;
1617 }
1618
1619 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001620}
1621
1622
1623/*
1624 * Add additional headers etc in msg_data specified by application
1625 * when sending requests.
1626 */
1627void pjsua_process_msg_data(pjsip_tx_data *tdata,
1628 const pjsua_msg_data *msg_data)
1629{
1630 pj_bool_t allow_body;
1631 const pjsip_hdr *hdr;
1632
Benny Prijono56315612006-07-18 14:39:40 +00001633 /* Always add User-Agent */
1634 if (pjsua_var.ua_cfg.user_agent.slen &&
1635 tdata->msg->type == PJSIP_REQUEST_MSG)
1636 {
1637 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1638 pjsip_hdr *h;
1639 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
1640 &STR_USER_AGENT,
1641 &pjsua_var.ua_cfg.user_agent);
1642 pjsip_msg_add_hdr(tdata->msg, h);
1643 }
1644
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001645 if (!msg_data)
1646 return;
1647
1648 hdr = msg_data->hdr_list.next;
1649 while (hdr && hdr != &msg_data->hdr_list) {
1650 pjsip_hdr *new_hdr;
1651
Benny Prijonoa1e69682007-05-11 15:14:34 +00001652 new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001653 pjsip_msg_add_hdr(tdata->msg, new_hdr);
1654
1655 hdr = hdr->next;
1656 }
1657
1658 allow_body = (tdata->msg->body == NULL);
1659
1660 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
1661 pjsip_media_type ctype;
1662 pjsip_msg_body *body;
1663
1664 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
1665 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
1666 &msg_data->msg_body);
1667 tdata->msg->body = body;
1668 }
1669}
1670
1671
1672/*
1673 * Add route_set to outgoing requests
1674 */
1675void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
1676 const pjsip_route_hdr *route_set )
1677{
1678 const pjsip_route_hdr *r;
1679
1680 r = route_set->next;
1681 while (r != route_set) {
1682 pjsip_route_hdr *new_r;
1683
Benny Prijonoa1e69682007-05-11 15:14:34 +00001684 new_r = (pjsip_route_hdr*) pjsip_hdr_clone(tdata->pool, r);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001685 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
1686
1687 r = r->next;
1688 }
1689}
1690
1691
1692/*
1693 * Simple version of MIME type parsing (it doesn't support parameters)
1694 */
1695void pjsua_parse_media_type( pj_pool_t *pool,
1696 const pj_str_t *mime,
1697 pjsip_media_type *media_type)
1698{
1699 pj_str_t tmp;
1700 char *pos;
1701
Benny Prijonoac623b32006-07-03 15:19:31 +00001702 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001703
1704 pj_strdup_with_null(pool, &tmp, mime);
1705
1706 pos = pj_strchr(&tmp, '/');
1707 if (pos) {
1708 media_type->type.ptr = tmp.ptr;
1709 media_type->type.slen = (pos-tmp.ptr);
1710 media_type->subtype.ptr = pos+1;
1711 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
1712 } else {
1713 media_type->type = tmp;
1714 }
1715}
1716
1717
1718/*
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001719 * Internal function to init transport selector from transport id.
1720 */
1721void pjsua_init_tpselector(pjsua_transport_id tp_id,
1722 pjsip_tpselector *sel)
1723{
1724 pjsua_transport_data *tpdata;
1725 unsigned flag;
1726
1727 pj_bzero(sel, sizeof(*sel));
1728 if (tp_id == PJSUA_INVALID_ID)
1729 return;
1730
Benny Prijonoa1e69682007-05-11 15:14:34 +00001731 pj_assert(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata));
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001732 tpdata = &pjsua_var.tpdata[tp_id];
1733
1734 flag = pjsip_transport_get_flag_from_type(tpdata->type);
1735
1736 if (flag & PJSIP_TRANSPORT_DATAGRAM) {
1737 sel->type = PJSIP_TPSELECTOR_TRANSPORT;
1738 sel->u.transport = tpdata->data.tp;
1739 } else {
1740 sel->type = PJSIP_TPSELECTOR_LISTENER;
1741 sel->u.listener = tpdata->data.factory;
1742 }
1743}
1744
1745
1746/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001747 * Verify that valid SIP url is given.
1748 */
1749PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
1750{
1751 pjsip_uri *p;
1752 pj_pool_t *pool;
1753 char *url;
1754 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
1755
1756 if (!len) return -1;
1757
1758 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
1759 if (!pool) return -1;
1760
Benny Prijonoa1e69682007-05-11 15:14:34 +00001761 url = (char*) pj_pool_alloc(pool, len+1);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001762 pj_ansi_strcpy(url, c_url);
1763
1764 p = pjsip_parse_uri(pool, url, len, 0);
1765 if (!p || pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0)
1766 p = NULL;
1767
1768 pj_pool_release(pool);
1769 return p ? 0 : -1;
1770}
Benny Prijonoda9785b2007-04-02 20:43:06 +00001771
1772
1773/*
1774 * This is a utility function to dump the stack states to log, using
1775 * verbosity level 3.
1776 */
1777PJ_DEF(void) pjsua_dump(pj_bool_t detail)
1778{
1779 unsigned old_decor;
1780 unsigned i;
1781 char buf[1024];
1782
1783 PJ_LOG(3,(THIS_FILE, "Start dumping application states:"));
1784
1785 old_decor = pj_log_get_decor();
1786 pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
1787
1788 if (detail)
1789 pj_dump_config();
1790
1791 pjsip_endpt_dump(pjsua_get_pjsip_endpt(), detail);
1792
1793 pjmedia_endpt_dump(pjsua_get_pjmedia_endpt());
1794
1795 PJ_LOG(3,(THIS_FILE, "Dumping media transports:"));
1796 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
1797 pjsua_call *call = &pjsua_var.calls[i];
1798 pjmedia_sock_info skinfo;
1799
Benny Prijono4f093d22007-04-09 08:54:32 +00001800 /* MSVC complains about skinfo not being initialized */
1801 pj_bzero(&skinfo, sizeof(skinfo));
1802
Benny Prijonoda9785b2007-04-02 20:43:06 +00001803 pjmedia_transport_get_info(call->med_tp, &skinfo);
1804
1805 PJ_LOG(3,(THIS_FILE, " %s: %s:%d",
1806 (pjsua_var.media_cfg.enable_ice ? "ICE" : "UDP"),
1807 pj_inet_ntoa(skinfo.rtp_addr_name.sin_addr),
1808 (int)pj_ntohs(skinfo.rtp_addr_name.sin_port)));
1809 }
1810
1811 pjsip_tsx_layer_dump(detail);
1812 pjsip_ua_dump(detail);
1813
1814
1815 /* Dump all invite sessions: */
1816 PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:"));
1817
1818 if (pjsua_call_get_count() == 0) {
1819
1820 PJ_LOG(3,(THIS_FILE, " - no sessions -"));
1821
1822 } else {
1823 unsigned i;
1824
1825 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
1826 if (pjsua_call_is_active(i)) {
1827 pjsua_call_dump(i, detail, buf, sizeof(buf), " ");
1828 PJ_LOG(3,(THIS_FILE, "%s", buf));
1829 }
1830 }
1831 }
1832
1833 /* Dump presence status */
1834 pjsua_pres_dump(detail);
1835
1836 pj_log_set_decor(old_decor);
1837 PJ_LOG(3,(THIS_FILE, "Dump complete"));
1838}
1839