blob: 96ca174a42ad2fb02f7362354d4950c7cd042f9b [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) {
167 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
168 }
169
170 /* Add Accept header */
171 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL);
172 if (cap_hdr) {
173 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
174 }
175
176 /* Add Supported header */
177 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL);
178 if (cap_hdr) {
179 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
180 }
181
182 /* Add Allow-Events header from the evsub module */
183 cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL);
184 if (cap_hdr) {
185 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
186 }
187
188 /* Add User-Agent header */
189 if (pjsua_var.ua_cfg.user_agent.slen) {
190 const pj_str_t USER_AGENT = { "User-Agent", 10};
191 pjsip_hdr *h;
192
193 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
194 &USER_AGENT,
195 &pjsua_var.ua_cfg.user_agent);
196 pjsip_msg_add_hdr(tdata->msg, h);
197 }
198
Benny Prijono617c5bc2007-04-02 19:51:21 +0000199 /* Get media socket info */
200 pjmedia_transport_get_info(pjsua_var.calls[0].med_tp, &skinfo);
201
Benny Prijono56315612006-07-18 14:39:40 +0000202 /* Add SDP body, using call0's RTP address */
203 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000204 &skinfo, &sdp);
Benny Prijono56315612006-07-18 14:39:40 +0000205 if (status == PJ_SUCCESS) {
206 pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
207 }
208
209 /* Send response statelessly */
210 pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
211 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL);
212 if (status != PJ_SUCCESS)
213 pjsip_tx_data_dec_ref(tdata);
214
215 return PJ_TRUE;
216}
217
218
219/* The module instance. */
220static pjsip_module pjsua_options_handler =
221{
222 NULL, NULL, /* prev, next. */
223 { "mod-pjsua-options", 17 }, /* Name. */
224 -1, /* Id */
225 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
226 NULL, /* load() */
227 NULL, /* start() */
228 NULL, /* stop() */
229 NULL, /* unload() */
230 &options_on_rx_request, /* on_rx_request() */
231 NULL, /* on_rx_response() */
232 NULL, /* on_tx_request. */
233 NULL, /* on_tx_response() */
234 NULL, /* on_tsx_state() */
235
236};
237
238
239/*****************************************************************************
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000240 * These two functions are the main callbacks registered to PJSIP stack
241 * to receive SIP request and response messages that are outside any
242 * dialogs and any transactions.
243 */
Benny Prijono268ca612006-02-07 12:34:11 +0000244
245/*
246 * Handler for receiving incoming requests.
247 *
248 * This handler serves multiple purposes:
249 * - it receives requests outside dialogs.
250 * - it receives requests inside dialogs, when the requests are
251 * unhandled by other dialog usages. Example of these
252 * requests are: MESSAGE.
253 */
254static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
255{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000256 pj_bool_t processed = PJ_FALSE;
257
258 PJSUA_LOCK();
Benny Prijono38998232006-02-08 22:44:25 +0000259
Benny Prijono84126ab2006-02-09 09:30:09 +0000260 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000261
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000262 processed = pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000263 }
264
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000265 PJSUA_UNLOCK();
266
267 return processed;
Benny Prijono268ca612006-02-07 12:34:11 +0000268}
269
270
271/*
272 * Handler for receiving incoming responses.
273 *
274 * This handler serves multiple purposes:
275 * - it receives strayed responses (i.e. outside any dialog and
276 * outside any transactions).
277 * - it receives responses coming to a transaction, when pjsua
278 * module is set as transaction user for the transaction.
279 * - it receives responses inside a dialog, when these responses
280 * are unhandled by other dialog usages.
281 */
282static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
283{
284 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000285 return PJ_FALSE;
286}
287
288
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000289/*****************************************************************************
290 * Logging.
291 */
292
293/* Log callback */
294static void log_writer(int level, const char *buffer, int len)
Benny Prijono268ca612006-02-07 12:34:11 +0000295{
Benny Prijono572d4852006-11-23 21:50:02 +0000296 /* Write to file, stdout or application callback. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000297
298 if (pjsua_var.log_file) {
299 pj_ssize_t size = len;
300 pj_file_write(pjsua_var.log_file, buffer, &size);
Benny Prijono980ca0a2007-04-03 17:55:08 +0000301 /* This will slow things down considerably! Don't do it!
302 pj_file_flush(pjsua_var.log_file);
303 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000304 }
305
Benny Prijono572d4852006-11-23 21:50:02 +0000306 if (level <= (int)pjsua_var.log_cfg.console_level) {
307 if (pjsua_var.log_cfg.cb)
308 (*pjsua_var.log_cfg.cb)(level, buffer, len);
309 else
310 pj_log_write(level, buffer, len);
311 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000312}
313
314
315/*
316 * Application can call this function at any time (after pjsua_create(), of
317 * course) to change logging settings.
318 */
319PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg)
320{
321 pj_status_t status;
322
323 /* Save config. */
324 pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg);
325
326 /* Redirect log function to ours */
327 pj_log_set_log_func( &log_writer );
328
329 /* Close existing file, if any */
330 if (pjsua_var.log_file) {
331 pj_file_close(pjsua_var.log_file);
332 pjsua_var.log_file = NULL;
333 }
334
335 /* If output log file is desired, create the file: */
336 if (pjsua_var.log_cfg.log_filename.slen) {
337
338 status = pj_file_open(pjsua_var.pool,
339 pjsua_var.log_cfg.log_filename.ptr,
340 PJ_O_WRONLY,
341 &pjsua_var.log_file);
342
343 if (status != PJ_SUCCESS) {
344 pjsua_perror(THIS_FILE, "Error creating log file", status);
345 return status;
346 }
347 }
348
349 /* Unregister msg logging if it's previously registered */
350 if (pjsua_msg_logger.id >= 0) {
351 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
352 pjsua_msg_logger.id = -1;
353 }
354
355 /* Enable SIP message logging */
356 if (pjsua_var.log_cfg.msg_logging)
357 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
358
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000359 return PJ_SUCCESS;
360}
361
362
363/*****************************************************************************
364 * PJSUA Base API.
365 */
366
367/* Worker thread function. */
368static int worker_thread(void *arg)
369{
370 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000371
Benny Prijono268ca612006-02-07 12:34:11 +0000372 PJ_UNUSED_ARG(arg);
373
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000374 while (!pjsua_var.thread_quit_flag) {
375 int count;
376
377 count = pjsua_handle_events(TIMEOUT);
378 if (count < 0)
379 pj_thread_sleep(TIMEOUT);
380 }
Benny Prijono268ca612006-02-07 12:34:11 +0000381
382 return 0;
383}
384
Benny Prijonodc39fe82006-05-26 12:17:46 +0000385
Benny Prijono268ca612006-02-07 12:34:11 +0000386/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000387 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000388 */
389PJ_DEF(pj_status_t) pjsua_create(void)
390{
391 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000392
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000393 /* Init pjsua data */
394 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000395
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000396 /* Set default logging settings */
397 pjsua_logging_config_default(&pjsua_var.log_cfg);
398
399 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000400 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000401 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
402
Benny Prijono268ca612006-02-07 12:34:11 +0000403
Benny Prijonofccab712006-02-22 22:23:22 +0000404 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000405 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000406 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000407
Benny Prijonoec921342007-03-24 13:00:30 +0000408 /* Init PJNATH */
409 status = pjnath_init();
410 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono268ca612006-02-07 12:34:11 +0000411
Benny Prijono094d3ad2006-11-21 08:41:00 +0000412 /* Set default sound device ID */
413 pjsua_var.cap_dev = pjsua_var.play_dev = -1;
414
Benny Prijono268ca612006-02-07 12:34:11 +0000415 /* Init caching pool. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000416 pj_caching_pool_init(&pjsua_var.cp, &pj_pool_factory_default_policy, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000417
418 /* Create memory pool for application. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000419 pjsua_var.pool = pjsua_pool_create("pjsua", 4000, 4000);
420
421 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000422
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000423 /* Create mutex */
424 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
425 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000426 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000427 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000428 return status;
429 }
430
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000431 /* Must create SIP endpoint to initialize SIP parser. The parser
432 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000433 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000434 status = pjsip_endpt_create(&pjsua_var.cp.factory,
435 pj_gethostname()->ptr,
436 &pjsua_var.endpt);
437 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000438
439
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000440 return PJ_SUCCESS;
441}
442
443
444/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000445 * Initialize pjsua with the specified settings. All the settings are
446 * optional, and the default values will be used when the config is not
447 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000448 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000449PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
450 const pjsua_logging_config *log_cfg,
451 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000452{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000453 pjsua_config default_cfg;
454 pjsua_media_config default_media_cfg;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000455 const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
Benny Prijonodc39fe82006-05-26 12:17:46 +0000456 pj_status_t status;
457
458
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000459 /* Create default configurations when the config is not supplied */
460
461 if (ua_cfg == NULL) {
462 pjsua_config_default(&default_cfg);
463 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000464 }
465
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000466 if (media_cfg == NULL) {
467 pjsua_media_config_default(&default_media_cfg);
468 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000469 }
470
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000471 /* Initialize logging first so that info/errors can be captured */
472 if (log_cfg) {
473 status = pjsua_reconfigure_logging(log_cfg);
474 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono9fc735d2006-05-28 14:58:12 +0000475 }
476
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000477 /* If nameserver is configured, create DNS resolver instance and
478 * set it to be used by SIP resolver.
479 */
480 if (ua_cfg->nameserver_count) {
481#if PJSIP_HAS_RESOLVER
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000482 unsigned i;
483
484 /* Create DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000485 status = pjsip_endpt_create_resolver(pjsua_var.endpt,
486 &pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000487 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
488
489 /* Configure nameserver for the DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000490 status = pj_dns_resolver_set_ns(pjsua_var.resolver,
491 ua_cfg->nameserver_count,
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000492 ua_cfg->nameserver, NULL);
493 if (status != PJ_SUCCESS) {
494 pjsua_perror(THIS_FILE, "Error setting nameserver", status);
495 return status;
496 }
497
498 /* Set this DNS resolver to be used by the SIP resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000499 status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000500 if (status != PJ_SUCCESS) {
501 pjsua_perror(THIS_FILE, "Error setting DNS resolver", status);
502 return status;
503 }
504
505 /* Print nameservers */
506 for (i=0; i<ua_cfg->nameserver_count; ++i) {
507 PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added",
508 (int)ua_cfg->nameserver[i].slen,
509 ua_cfg->nameserver[i].ptr));
510 }
511#else
512 PJ_LOG(2,(THIS_FILE,
513 "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)"));
514#endif
515 }
516
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000517 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000518
519 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000520 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
521 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000522
Benny Prijonodc39fe82006-05-26 12:17:46 +0000523
524 /* Initialize UA layer module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000525 status = pjsip_ua_init_module( pjsua_var.endpt, NULL );
526 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000527
Benny Prijonodc39fe82006-05-26 12:17:46 +0000528
Benny Prijono053f5222006-11-11 16:16:04 +0000529 /* Initialize Replaces support. */
530 status = pjsip_replaces_init_module( pjsua_var.endpt );
531 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
532
533
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000534 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000535 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000536 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000537 {
538 NULL, NULL, /* prev, next. */
539 { "mod-pjsua", 9 }, /* Name. */
540 -1, /* Id */
541 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
542 NULL, /* load() */
543 NULL, /* start() */
544 NULL, /* stop() */
545 NULL, /* unload() */
546 &mod_pjsua_on_rx_request, /* on_rx_request() */
547 &mod_pjsua_on_rx_response, /* on_rx_response() */
548 NULL, /* on_tx_request. */
549 NULL, /* on_tx_response() */
550 NULL, /* on_tsx_state() */
551 };
552
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000553 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000554
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000555 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
556 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000557 }
558
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000559
Benny Prijonodc39fe82006-05-26 12:17:46 +0000560
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000561 /* Initialize PJSUA call subsystem: */
562 status = pjsua_call_subsys_init(ua_cfg);
563 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000564 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000565
566
Benny Prijonoc97608e2007-03-23 16:34:20 +0000567 /* Start resolving STUN server */
568 status = pjsua_resolve_stun_server(PJ_FALSE);
569 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
570 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
571 return status;
572 }
573
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000574 /* Initialize PJSUA media subsystem */
575 status = pjsua_media_subsys_init(media_cfg);
576 if (status != PJ_SUCCESS)
577 goto on_error;
578
Benny Prijonodc39fe82006-05-26 12:17:46 +0000579
580 /* Init core SIMPLE module : */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000581 status = pjsip_evsub_init_module(pjsua_var.endpt);
582 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000583
Benny Prijonodc39fe82006-05-26 12:17:46 +0000584
585 /* Init presence module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000586 status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance());
587 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000588
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000589 /* Init PUBLISH module */
590 pjsip_publishc_init_module(pjsua_var.endpt);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000591
592 /* Init xfer/REFER module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000593 status = pjsip_xfer_init_module( pjsua_var.endpt );
594 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000595
596 /* Init pjsua presence handler: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000597 status = pjsua_pres_init();
598 if (status != PJ_SUCCESS)
599 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000600
601 /* Init out-of-dialog MESSAGE request handler. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000602 status = pjsua_im_init();
603 if (status != PJ_SUCCESS)
604 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000605
Benny Prijonoc8141a82006-08-20 09:12:19 +0000606 /* Register OPTIONS handler */
607 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler);
608
609 /* Add OPTIONS in Allow header */
610 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
611 NULL, 1, &STR_OPTIONS);
612
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000613 /* Start worker thread if needed. */
614 if (pjsua_var.ua_cfg.thread_cnt) {
615 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000616
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000617 if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread))
618 pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000619
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000620 for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) {
621 status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread,
622 NULL, 0, 0, &pjsua_var.thread[i]);
623 if (status != PJ_SUCCESS)
624 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000625 }
Benny Prijonof521eb02006-08-06 23:07:25 +0000626 PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created",
627 pjsua_var.ua_cfg.thread_cnt));
628 } else {
629 PJ_LOG(4,(THIS_FILE, "No SIP worker threads created"));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000630 }
631
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000632 /* Done! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000633
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000634 PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized",
635 PJ_VERSION, PJ_OS_NAME));
Benny Prijonoccf95622006-02-07 18:48:01 +0000636
Benny Prijono268ca612006-02-07 12:34:11 +0000637 return PJ_SUCCESS;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000638
639on_error:
640 pjsua_destroy();
641 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000642}
643
644
Benny Prijono834aee32006-02-19 01:38:06 +0000645/* Sleep with polling */
646static void busy_sleep(unsigned msec)
647{
648 pj_time_val timeout, now;
649
650 pj_gettimeofday(&timeout);
651 timeout.msec += msec;
652 pj_time_val_normalize(&timeout);
653
654 do {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000655 while (pjsua_handle_events(10) > 0)
656 ;
Benny Prijono834aee32006-02-19 01:38:06 +0000657 pj_gettimeofday(&now);
658 } while (PJ_TIME_VAL_LT(now, timeout));
659}
660
Benny Prijonoebbf6892007-03-24 17:37:25 +0000661
Benny Prijonoebbf6892007-03-24 17:37:25 +0000662/*
663 * Callback function to receive notification from the resolver
664 * when the resolution process completes.
665 */
666static void stun_dns_srv_resolver_cb(void *user_data,
667 pj_status_t status,
668 const pj_dns_srv_record *rec)
669{
670 unsigned i;
671
672 PJ_UNUSED_ARG(user_data);
673
674 pjsua_var.stun_status = status;
675
676 if (status != PJ_SUCCESS) {
677 /* DNS SRV resolution failed. If stun_host is specified, resolve
678 * it with gethostbyname()
679 */
680 if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000681 pj_str_t str_host, str_port;
682 int port;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000683 pj_hostent he;
684
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000685 str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':');
686 if (str_port.ptr != NULL) {
687 str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr;
688 str_host.slen = (str_port.ptr - str_host.ptr);
689 str_port.ptr++;
690 str_port.slen = pjsua_var.ua_cfg.stun_host.slen -
691 str_host.slen - 1;
692 port = (int)pj_strtoul(&str_port);
693 if (port < 1 || port > 65535) {
694 pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL);
695 pjsua_var.stun_status = PJ_EINVAL;
696 return;
697 }
698 } else {
699 str_host = pjsua_var.ua_cfg.stun_host;
700 port = 3478;
701 }
702
703 pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
Benny Prijonoebbf6892007-03-24 17:37:25 +0000704
705 if (pjsua_var.stun_status == PJ_SUCCESS) {
706 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
707 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000708 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoebbf6892007-03-24 17:37:25 +0000709
710 PJ_LOG(3,(THIS_FILE,
711 "STUN server %.*s resolved, address is %s:%d",
712 (int)pjsua_var.ua_cfg.stun_host.slen,
713 pjsua_var.ua_cfg.stun_host.ptr,
714 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
715 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
716 }
717 } else {
718 char errmsg[PJ_ERR_MSG_SIZE];
719
720 pj_strerror(status, errmsg, sizeof(errmsg));
721 PJ_LOG(1,(THIS_FILE,
722 "DNS SRV resolution failed for STUN server %.*s: %s",
723 (int)pjsua_var.ua_cfg.stun_domain.slen,
724 pjsua_var.ua_cfg.stun_domain.ptr,
725 errmsg));
726 }
727 return;
728 }
729
730 pj_memcpy(&pjsua_var.stun_srv, &rec->entry[0].addr,
731 rec->entry[0].addr_len);
732
733 PJ_LOG(3,(THIS_FILE, "_stun._udp.%.*s resolved, found %d entry(s):",
734 (int)pjsua_var.ua_cfg.stun_domain.slen,
735 pjsua_var.ua_cfg.stun_domain.ptr,
736 rec->count));
737
738 for (i=0; i<rec->count; ++i) {
739 PJ_LOG(3,(THIS_FILE,
740 " %d: prio=%d, weight=%d %s:%d",
741 i, rec->entry[i].priority, rec->entry[i].weight,
742 pj_inet_ntoa(rec->entry[i].addr.ipv4.sin_addr),
743 (int)pj_ntohs(rec->entry[i].addr.ipv4.sin_port)));
744 }
745
746}
747
Benny Prijono268ca612006-02-07 12:34:11 +0000748/*
Benny Prijonoc97608e2007-03-23 16:34:20 +0000749 * Resolve STUN server.
750 */
751pj_status_t pjsua_resolve_stun_server(pj_bool_t wait)
752{
753 if (pjsua_var.stun_status == PJ_EUNKNOWN) {
754 /* Initialize STUN configuration */
755 pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0,
756 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
757 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
758
759 /* Start STUN server resolution */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000760
761 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000762
Benny Prijonoebbf6892007-03-24 17:37:25 +0000763 /* If stun_domain is specified, resolve STUN servers with DNS
764 * SRV resolution.
765 */
766 if (pjsua_var.ua_cfg.stun_domain.slen) {
767 pj_str_t res_type;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000768 pj_status_t status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000769
770 /* Fail if resolver is not configured */
771 if (pjsua_var.resolver == NULL) {
772 PJ_LOG(1,(THIS_FILE, "Nameserver must be configured when "
773 "stun_domain is specified"));
774 pjsua_var.stun_status = PJLIB_UTIL_EDNSNONS;
775 return PJLIB_UTIL_EDNSNONS;
776 }
777 res_type = pj_str("_stun._udp");
Benny Prijonoda9785b2007-04-02 20:43:06 +0000778 status =
Benny Prijonoebbf6892007-03-24 17:37:25 +0000779 pj_dns_srv_resolve(&pjsua_var.ua_cfg.stun_domain, &res_type,
780 3478, pjsua_var.pool, pjsua_var.resolver,
Benny Prijonoda9785b2007-04-02 20:43:06 +0000781 0, NULL, &stun_dns_srv_resolver_cb);
782 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +0000783 pjsua_perror(THIS_FILE, "Error starting DNS SRV resolution",
784 pjsua_var.stun_status);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000785 pjsua_var.stun_status = status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000786 return pjsua_var.stun_status;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000787 } else {
788 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000789 }
790 }
791 /* Otherwise if stun_host is specified, resolve STUN server with
792 * gethostbyname().
793 */
794 else if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000795 pj_str_t str_host, str_port;
796 int port;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000797 pj_hostent he;
798
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000799 str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':');
800 if (str_port.ptr != NULL) {
801 str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr;
802 str_host.slen = (str_port.ptr - str_host.ptr);
803 str_port.ptr++;
804 str_port.slen = pjsua_var.ua_cfg.stun_host.slen -
805 str_host.slen - 1;
806 port = (int)pj_strtoul(&str_port);
807 if (port < 1 || port > 65535) {
808 pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL);
809 pjsua_var.stun_status = PJ_EINVAL;
810 return pjsua_var.stun_status;
811 }
812 } else {
813 str_host = pjsua_var.ua_cfg.stun_host;
814 port = 3478;
815 }
816
817
818 pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000819
820 if (pjsua_var.stun_status == PJ_SUCCESS) {
821 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
822 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000823 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonof75eceb2007-03-23 19:09:54 +0000824
Benny Prijonoebbf6892007-03-24 17:37:25 +0000825 PJ_LOG(3,(THIS_FILE,
Benny Prijonof75eceb2007-03-23 19:09:54 +0000826 "STUN server %.*s resolved, address is %s:%d",
Benny Prijonoebbf6892007-03-24 17:37:25 +0000827 (int)pjsua_var.ua_cfg.stun_host.slen,
828 pjsua_var.ua_cfg.stun_host.ptr,
Benny Prijonof75eceb2007-03-23 19:09:54 +0000829 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
830 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
Benny Prijonoc97608e2007-03-23 16:34:20 +0000831 }
Benny Prijonoebbf6892007-03-24 17:37:25 +0000832
Benny Prijonoc97608e2007-03-23 16:34:20 +0000833 }
Benny Prijonoebbf6892007-03-24 17:37:25 +0000834 /* Otherwise disable STUN. */
835 else {
836 pjsua_var.stun_status = PJ_SUCCESS;
837 }
838
839
Benny Prijonoc97608e2007-03-23 16:34:20 +0000840 return pjsua_var.stun_status;
841
842 } else if (pjsua_var.stun_status == PJ_EPENDING) {
843 /* STUN server resolution has been started, wait for the
844 * result.
845 */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000846 if (wait) {
847 while (pjsua_var.stun_status == PJ_EPENDING)
848 pjsua_handle_events(10);
849 }
850
851 return pjsua_var.stun_status;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000852
853 } else {
854 /* STUN server has been resolved, return the status */
855 return pjsua_var.stun_status;
856 }
857}
858
859/*
Benny Prijono268ca612006-02-07 12:34:11 +0000860 * Destroy pjsua.
861 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000862PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +0000863{
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000864 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +0000865
866 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000867 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +0000868
869 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000870 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
871 if (pjsua_var.thread[i]) {
872 pj_thread_join(pjsua_var.thread[i]);
873 pj_thread_destroy(pjsua_var.thread[i]);
874 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +0000875 }
Benny Prijono268ca612006-02-07 12:34:11 +0000876 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000877
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000878 if (pjsua_var.endpt) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000879 /* Terminate all calls. */
880 pjsua_call_hangup_all();
881
882 /* Terminate all presence subscriptions. */
883 pjsua_pres_shutdown();
884
885 /* Unregister, if required: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000886 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
887 if (!pjsua_var.acc[i].valid)
888 continue;
889
890 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000891 pjsua_acc_set_registration(i, PJ_FALSE);
892 }
893 }
894
895 /* Wait for some time to allow unregistration to complete: */
Benny Prijono9fc735d2006-05-28 14:58:12 +0000896 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
897 busy_sleep(1000);
898 }
Benny Prijono834aee32006-02-19 01:38:06 +0000899
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000900 /* Destroy media */
901 pjsua_media_subsys_destroy();
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000902
Benny Prijono268ca612006-02-07 12:34:11 +0000903 /* Destroy endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000904 if (pjsua_var.endpt) {
905 pjsip_endpt_destroy(pjsua_var.endpt);
906 pjsua_var.endpt = NULL;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000907 }
Benny Prijono268ca612006-02-07 12:34:11 +0000908
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000909 /* Destroy mutex */
910 if (pjsua_var.mutex) {
911 pj_mutex_destroy(pjsua_var.mutex);
912 pjsua_var.mutex = NULL;
913 }
914
915 /* Destroy pool and pool factory. */
916 if (pjsua_var.pool) {
917 pj_pool_release(pjsua_var.pool);
918 pjsua_var.pool = NULL;
919 pj_caching_pool_destroy(&pjsua_var.cp);
920 }
Benny Prijono268ca612006-02-07 12:34:11 +0000921
922
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000923 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
924
925 /* End logging */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000926 if (pjsua_var.log_file) {
927 pj_file_close(pjsua_var.log_file);
928 pjsua_var.log_file = NULL;
929 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000930
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000931 /* Shutdown PJLIB */
932 pj_shutdown();
933
Benny Prijonof762ee72006-12-01 11:14:37 +0000934 /* Clear pjsua_var */
935 pj_bzero(&pjsua_var, sizeof(pjsua_var));
936
Benny Prijono268ca612006-02-07 12:34:11 +0000937 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000938 return PJ_SUCCESS;
939}
940
941
942/**
943 * Application is recommended to call this function after all initialization
944 * is done, so that the library can do additional checking set up
945 * additional
946 *
947 * @return PJ_SUCCESS on success, or the appropriate error code.
948 */
949PJ_DEF(pj_status_t) pjsua_start(void)
950{
951 pj_status_t status;
952
953 status = pjsua_call_subsys_start();
954 if (status != PJ_SUCCESS)
955 return status;
956
957 status = pjsua_media_subsys_start();
958 if (status != PJ_SUCCESS)
959 return status;
960
961 status = pjsua_pres_start();
962 if (status != PJ_SUCCESS)
963 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000964
965 return PJ_SUCCESS;
966}
967
Benny Prijono9fc735d2006-05-28 14:58:12 +0000968
969/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000970 * Poll pjsua for events, and if necessary block the caller thread for
971 * the specified maximum interval (in miliseconds).
972 */
973PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
974{
975 unsigned count = 0;
976 pj_time_val tv;
977 pj_status_t status;
978
979 tv.sec = 0;
980 tv.msec = msec_timeout;
981 pj_time_val_normalize(&tv);
982
983 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
984
985 if (status != PJ_SUCCESS)
986 return -status;
987
988 return count;
989}
990
991
992/*
993 * Create memory pool.
994 */
995PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
996 pj_size_t increment)
997{
998 /* Pool factory is thread safe, no need to lock */
999 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
1000 NULL);
1001}
1002
1003
1004/*
1005 * Internal function to get SIP endpoint instance of pjsua, which is
1006 * needed for example to register module, create transports, etc.
1007 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001008 */
1009PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
1010{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001011 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001012}
1013
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001014/*
1015 * Internal function to get media endpoint instance.
1016 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001017 */
1018PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
1019{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001020 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001021}
1022
Benny Prijono97b87172006-08-24 14:25:14 +00001023/*
1024 * Internal function to get PJSUA pool factory.
1025 */
1026PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
1027{
1028 return &pjsua_var.cp.factory;
1029}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001030
1031/*****************************************************************************
1032 * PJSUA SIP Transport API.
1033 */
1034
1035/*
1036 * Create and initialize SIP socket (and possibly resolve public
1037 * address via STUN, depending on config).
1038 */
1039static pj_status_t create_sip_udp_sock(pj_in_addr bound_addr,
1040 int port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001041 pj_sock_t *p_sock,
1042 pj_sockaddr_in *p_pub_addr)
1043{
Benny Prijonoc97608e2007-03-23 16:34:20 +00001044 char ip_addr[32];
1045 pj_str_t stun_srv;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001046 pj_sock_t sock;
1047 pj_status_t status;
1048
Benny Prijonoc97608e2007-03-23 16:34:20 +00001049 /* Make sure STUN server resolution has completed */
1050 status = pjsua_resolve_stun_server(PJ_TRUE);
1051 if (status != PJ_SUCCESS) {
1052 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
1053 return status;
1054 }
1055
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001056 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock);
1057 if (status != PJ_SUCCESS) {
1058 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +00001059 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001060 }
1061
Benny Prijonof90ef4f2007-02-12 14:59:57 +00001062 status = pj_sock_bind_in(sock, pj_ntohl(bound_addr.s_addr),
1063 (pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001064 if (status != PJ_SUCCESS) {
1065 pjsua_perror(THIS_FILE, "bind() error", status);
1066 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001067 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001068 }
1069
Benny Prijonoe347cb02007-02-14 14:36:13 +00001070 /* If port is zero, get the bound port */
1071 if (port == 0) {
1072 pj_sockaddr_in bound_addr;
1073 int namelen = sizeof(bound_addr);
1074 status = pj_sock_getsockname(sock, &bound_addr, &namelen);
1075 if (status != PJ_SUCCESS) {
1076 pjsua_perror(THIS_FILE, "getsockname() error", status);
1077 pj_sock_close(sock);
1078 return status;
1079 }
1080
1081 port = pj_ntohs(bound_addr.sin_port);
1082 }
1083
Benny Prijonoc97608e2007-03-23 16:34:20 +00001084 if (pjsua_var.stun_srv.addr.sa_family != 0) {
1085 pj_ansi_strcpy(ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
1086 stun_srv = pj_str(ip_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001087 } else {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001088 stun_srv.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001089 }
1090
1091 /* Get the published address, either by STUN or by resolving
1092 * the name of local host.
1093 */
Benny Prijonoc97608e2007-03-23 16:34:20 +00001094 if (stun_srv.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +00001095 /*
1096 * STUN is specified, resolve the address with STUN.
1097 */
Benny Prijono14c2b862007-02-21 00:40:05 +00001098 status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001099 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
1100 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijonoc97608e2007-03-23 16:34:20 +00001101 p_pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001102 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +00001103 pjsua_perror(THIS_FILE, "Error contacting STUN server", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001104 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001105 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001106 }
1107
Benny Prijono0a5cad82006-09-26 13:21:02 +00001108 } else if (p_pub_addr->sin_addr.s_addr != 0) {
1109 /*
1110 * Public address is already specified, no need to resolve the
1111 * address, only set the port.
1112 */
Benny Prijonoe347cb02007-02-14 14:36:13 +00001113 if (p_pub_addr->sin_port == 0)
1114 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001115
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001116 } else {
1117
Benny Prijono594e4c52006-09-14 18:51:01 +00001118 pj_bzero(p_pub_addr, sizeof(pj_sockaddr_in));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001119
Benny Prijono594e4c52006-09-14 18:51:01 +00001120 status = pj_gethostip(&p_pub_addr->sin_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001121 if (status != PJ_SUCCESS) {
Benny Prijonob43bad72007-01-20 05:11:08 +00001122 pjsua_perror(THIS_FILE, "Unable to get local host IP", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001123 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001124 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001125 }
1126
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001127 p_pub_addr->sin_family = PJ_AF_INET;
1128 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001129 }
1130
1131 *p_sock = sock;
1132
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001133 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
1134 pj_inet_ntoa(p_pub_addr->sin_addr),
1135 (int)pj_ntohs(p_pub_addr->sin_port)));
1136
Benny Prijonod8410532006-06-15 11:04:33 +00001137 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001138}
1139
1140
1141/*
1142 * Create SIP transport.
1143 */
1144PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
1145 const pjsua_transport_config *cfg,
1146 pjsua_transport_id *p_id)
1147{
1148 pjsip_transport *tp;
1149 unsigned id;
1150 pj_status_t status;
1151
1152 PJSUA_LOCK();
1153
1154 /* Find empty transport slot */
1155 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001156 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001157 break;
1158 }
1159
1160 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1161 status = PJ_ETOOMANY;
1162 pjsua_perror(THIS_FILE, "Error creating transport", status);
1163 goto on_return;
1164 }
1165
1166 /* Create the transport */
1167 if (type == PJSIP_TRANSPORT_UDP) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001168 /*
1169 * Create UDP transport.
1170 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001171 pjsua_transport_config config;
Benny Prijonod8410532006-06-15 11:04:33 +00001172 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001173 pj_sockaddr_in bound_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001174 pj_sockaddr_in pub_addr;
1175 pjsip_host_port addr_name;
1176
1177 /* Supply default config if it's not specified */
1178 if (cfg == NULL) {
1179 pjsua_transport_config_default(&config);
1180 cfg = &config;
1181 }
1182
Benny Prijono0a5cad82006-09-26 13:21:02 +00001183 /* Initialize bound address, if any */
1184 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
1185 if (cfg->bound_addr.slen) {
1186 status = pj_sockaddr_in_set_str_addr(&bound_addr,&cfg->bound_addr);
1187 if (status != PJ_SUCCESS) {
1188 pjsua_perror(THIS_FILE,
1189 "Unable to resolve transport bound address",
1190 status);
1191 goto on_return;
1192 }
1193 }
1194
1195 /* Initialize the public address from the config, if any */
1196 pj_sockaddr_in_init(&pub_addr, NULL, (pj_uint16_t)cfg->port);
1197 if (cfg->public_addr.slen) {
1198 status = pj_sockaddr_in_set_str_addr(&pub_addr, &cfg->public_addr);
1199 if (status != PJ_SUCCESS) {
1200 pjsua_perror(THIS_FILE,
1201 "Unable to resolve transport public address",
1202 status);
1203 goto on_return;
1204 }
1205 }
1206
1207 /* Create the socket and possibly resolve the address with STUN
1208 * (only when public address is not specified).
1209 */
1210 status = create_sip_udp_sock(bound_addr.sin_addr, cfg->port,
Benny Prijono0a5cad82006-09-26 13:21:02 +00001211 &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001212 if (status != PJ_SUCCESS)
1213 goto on_return;
1214
1215 addr_name.host = pj_str(pj_inet_ntoa(pub_addr.sin_addr));
1216 addr_name.port = pj_ntohs(pub_addr.sin_port);
1217
1218 /* Create UDP transport */
1219 status = pjsip_udp_transport_attach( pjsua_var.endpt, sock,
1220 &addr_name, 1,
1221 &tp);
1222 if (status != PJ_SUCCESS) {
1223 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
1224 status);
1225 pj_sock_close(sock);
1226 goto on_return;
1227 }
1228
Benny Prijonoe93e2872006-06-28 16:46:49 +00001229
1230 /* Save the transport */
1231 pjsua_var.tpdata[id].type = type;
1232 pjsua_var.tpdata[id].local_name = tp->local_name;
1233 pjsua_var.tpdata[id].data.tp = tp;
1234
Benny Prijono3569c0d2007-04-06 10:29:20 +00001235#if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0
1236
Benny Prijonoe93e2872006-06-28 16:46:49 +00001237 } else if (type == PJSIP_TRANSPORT_TCP) {
1238 /*
1239 * Create TCP transport.
1240 */
1241 pjsua_transport_config config;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001242 pjsip_host_port a_name;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001243 pjsip_tpfactory *tcp;
1244 pj_sockaddr_in local_addr;
1245
1246 /* Supply default config if it's not specified */
1247 if (cfg == NULL) {
1248 pjsua_transport_config_default(&config);
1249 cfg = &config;
1250 }
1251
1252 /* Init local address */
1253 pj_sockaddr_in_init(&local_addr, 0, 0);
1254
1255 if (cfg->port)
1256 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1257
Benny Prijono0a5cad82006-09-26 13:21:02 +00001258 if (cfg->bound_addr.slen) {
1259 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1260 if (status != PJ_SUCCESS) {
1261 pjsua_perror(THIS_FILE,
1262 "Unable to resolve transport bound address",
1263 status);
1264 goto on_return;
1265 }
1266 }
1267
1268 /* Init published name */
1269 pj_bzero(&a_name, sizeof(pjsip_host_port));
1270 if (cfg->public_addr.slen)
1271 a_name.host = cfg->public_addr;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001272
1273 /* Create the TCP transport */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001274 status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr,
1275 &a_name, 1, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001276
1277 if (status != PJ_SUCCESS) {
1278 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1279 status);
1280 goto on_return;
1281 }
1282
1283 /* Save the transport */
1284 pjsua_var.tpdata[id].type = type;
1285 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1286 pjsua_var.tpdata[id].data.factory = tcp;
1287
Benny Prijono3569c0d2007-04-06 10:29:20 +00001288#endif /* PJ_HAS_TCP */
1289
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001290#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1291 } else if (type == PJSIP_TRANSPORT_TLS) {
1292 /*
1293 * Create TLS transport.
1294 */
Benny Prijonof3bbc132006-12-25 06:43:59 +00001295 /*
1296 * Create TCP transport.
1297 */
1298 pjsua_transport_config config;
1299 pjsip_host_port a_name;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001300 pjsip_tpfactory *tls;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001301 pj_sockaddr_in local_addr;
1302
1303 /* Supply default config if it's not specified */
1304 if (cfg == NULL) {
1305 pjsua_transport_config_default(&config);
1306 config.port = 5061;
1307 cfg = &config;
1308 }
1309
1310 /* Init local address */
1311 pj_sockaddr_in_init(&local_addr, 0, 0);
1312
1313 if (cfg->port)
1314 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1315
1316 if (cfg->bound_addr.slen) {
1317 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1318 if (status != PJ_SUCCESS) {
1319 pjsua_perror(THIS_FILE,
1320 "Unable to resolve transport bound address",
1321 status);
1322 goto on_return;
1323 }
1324 }
1325
1326 /* Init published name */
1327 pj_bzero(&a_name, sizeof(pjsip_host_port));
1328 if (cfg->public_addr.slen)
1329 a_name.host = cfg->public_addr;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001330
1331 status = pjsip_tls_transport_start(pjsua_var.endpt,
Benny Prijonof3bbc132006-12-25 06:43:59 +00001332 &cfg->tls_setting,
1333 &local_addr, &a_name, 1, &tls);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001334 if (status != PJ_SUCCESS) {
1335 pjsua_perror(THIS_FILE, "Error creating SIP TLS listener",
1336 status);
1337 goto on_return;
1338 }
1339
1340 /* Save the transport */
1341 pjsua_var.tpdata[id].type = type;
1342 pjsua_var.tpdata[id].local_name = tls->addr_name;
1343 pjsua_var.tpdata[id].data.factory = tls;
1344#endif
1345
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001346 } else {
1347 status = PJSIP_EUNSUPTRANSPORT;
1348 pjsua_perror(THIS_FILE, "Error creating transport", status);
1349 goto on_return;
1350 }
1351
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001352
1353 /* Return the ID */
1354 if (p_id) *p_id = id;
1355
1356 status = PJ_SUCCESS;
1357
1358on_return:
1359
1360 PJSUA_UNLOCK();
1361
Benny Prijonod8410532006-06-15 11:04:33 +00001362 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001363}
1364
1365
1366/*
1367 * Register transport that has been created by application.
1368 */
1369PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1370 pjsua_transport_id *p_id)
1371{
1372 unsigned id;
1373
1374 PJSUA_LOCK();
1375
1376 /* Find empty transport slot */
1377 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001378 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001379 break;
1380 }
1381
1382 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1383 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1384 PJSUA_UNLOCK();
1385 return PJ_ETOOMANY;
1386 }
1387
1388 /* Save the transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001389 pjsua_var.tpdata[id].type = tp->key.type;
1390 pjsua_var.tpdata[id].local_name = tp->local_name;
1391 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001392
1393 /* Return the ID */
1394 if (p_id) *p_id = id;
1395
1396 PJSUA_UNLOCK();
1397
1398 return PJ_SUCCESS;
1399}
1400
1401
1402/*
1403 * Enumerate all transports currently created in the system.
1404 */
1405PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1406 unsigned *p_count )
1407{
1408 unsigned i, count;
1409
1410 PJSUA_LOCK();
1411
1412 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
1413 ++i)
1414 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001415 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001416 continue;
1417
1418 id[count++] = i;
1419 }
1420
1421 *p_count = count;
1422
1423 PJSUA_UNLOCK();
1424
1425 return PJ_SUCCESS;
1426}
1427
1428
1429/*
1430 * Get information about transports.
1431 */
1432PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
1433 pjsua_transport_info *info)
1434{
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001435 pjsua_transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00001436 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001437
Benny Prijonoac623b32006-07-03 15:19:31 +00001438 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001439
1440 /* Make sure id is in range. */
1441 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1442
1443 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001444 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001445
1446 PJSUA_LOCK();
1447
Benny Prijonoe93e2872006-06-28 16:46:49 +00001448 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
1449
1450 pjsip_transport *tp = t->data.tp;
1451
1452 if (tp == NULL) {
1453 PJSUA_UNLOCK();
1454 return PJ_EINVALIDOP;
1455 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001456
Benny Prijonoe93e2872006-06-28 16:46:49 +00001457 info->id = id;
1458 info->type = tp->key.type;
1459 info->type_name = pj_str(tp->type_name);
1460 info->info = pj_str(tp->info);
1461 info->flag = tp->flag;
1462 info->addr_len = tp->addr_len;
1463 info->local_addr = tp->local_addr;
1464 info->local_name = tp->local_name;
1465 info->usage_count = pj_atomic_get(tp->ref_cnt);
1466
Benny Prijono0a5cad82006-09-26 13:21:02 +00001467 status = PJ_SUCCESS;
1468
Benny Prijonoe93e2872006-06-28 16:46:49 +00001469 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
1470
1471 pjsip_tpfactory *factory = t->data.factory;
1472
1473 if (factory == NULL) {
1474 PJSUA_UNLOCK();
1475 return PJ_EINVALIDOP;
1476 }
1477
1478 info->id = id;
1479 info->type = t->type;
1480 info->type_name = pj_str("TCP");
1481 info->info = pj_str("TCP transport");
1482 info->flag = factory->flag;
1483 info->addr_len = sizeof(factory->local_addr);
1484 info->local_addr = factory->local_addr;
1485 info->local_name = factory->addr_name;
1486 info->usage_count = 0;
1487
Benny Prijono0a5cad82006-09-26 13:21:02 +00001488 status = PJ_SUCCESS;
1489
1490 } else {
1491 pj_assert(!"Unsupported transport");
1492 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001493 }
1494
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001495
1496 PJSUA_UNLOCK();
1497
Benny Prijono0a5cad82006-09-26 13:21:02 +00001498 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001499}
1500
1501
1502/*
1503 * Disable a transport or re-enable it.
1504 */
1505PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
1506 pj_bool_t enabled)
1507{
1508 /* Make sure id is in range. */
1509 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1510
1511 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001512 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001513
1514
1515 /* To be done!! */
1516 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001517 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001518
1519 return PJ_EINVALIDOP;
1520}
1521
1522
1523/*
1524 * Close the transport.
1525 */
1526PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
1527 pj_bool_t force )
1528{
Benny Prijono5ff61872007-02-01 03:37:11 +00001529 pj_status_t status;
1530
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001531 /* Make sure id is in range. */
1532 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1533
1534 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001535 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001536
Benny Prijono0a5cad82006-09-26 13:21:02 +00001537 /* Note: destroy() may not work if there are objects still referencing
1538 * the transport.
1539 */
1540 if (force) {
1541 switch (pjsua_var.tpdata[id].type) {
1542 case PJSIP_TRANSPORT_UDP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001543 status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
1544 if (status != PJ_SUCCESS)
1545 return status;
1546 status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
1547 if (status != PJ_SUCCESS)
1548 return status;
1549 break;
1550
1551 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001552 case PJSIP_TRANSPORT_TCP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001553 /* This will close the TCP listener, but existing TCP/TLS
1554 * connections (if any) will still linger
1555 */
1556 status = (*pjsua_var.tpdata[id].data.factory->destroy)
1557 (pjsua_var.tpdata[id].data.factory);
1558 if (status != PJ_SUCCESS)
1559 return status;
1560
Benny Prijono0a5cad82006-09-26 13:21:02 +00001561 break;
Benny Prijono5ff61872007-02-01 03:37:11 +00001562
Benny Prijono1ebd6142006-10-19 15:48:02 +00001563 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001564 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001565 }
1566
1567 } else {
Benny Prijono5ff61872007-02-01 03:37:11 +00001568 /* If force is not specified, transports will be closed at their
1569 * convenient time. However this will leak PJSUA-API transport
1570 * descriptors as PJSUA-API wouldn't know when exactly the
1571 * transport is closed thus it can't cleanup PJSUA transport
1572 * descriptor.
1573 */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001574 switch (pjsua_var.tpdata[id].type) {
1575 case PJSIP_TRANSPORT_UDP:
1576 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
Benny Prijono5ff61872007-02-01 03:37:11 +00001577 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001578 case PJSIP_TRANSPORT_TCP:
1579 return (*pjsua_var.tpdata[id].data.factory->destroy)
1580 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001581 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001582 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001583 }
1584 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001585
Benny Prijono5ff61872007-02-01 03:37:11 +00001586 /* Cleanup pjsua data when force is applied */
1587 if (force) {
1588 pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED;
1589 pjsua_var.tpdata[id].data.ptr = NULL;
1590 }
1591
1592 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001593}
1594
1595
1596/*
1597 * Add additional headers etc in msg_data specified by application
1598 * when sending requests.
1599 */
1600void pjsua_process_msg_data(pjsip_tx_data *tdata,
1601 const pjsua_msg_data *msg_data)
1602{
1603 pj_bool_t allow_body;
1604 const pjsip_hdr *hdr;
1605
Benny Prijono56315612006-07-18 14:39:40 +00001606 /* Always add User-Agent */
1607 if (pjsua_var.ua_cfg.user_agent.slen &&
1608 tdata->msg->type == PJSIP_REQUEST_MSG)
1609 {
1610 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1611 pjsip_hdr *h;
1612 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
1613 &STR_USER_AGENT,
1614 &pjsua_var.ua_cfg.user_agent);
1615 pjsip_msg_add_hdr(tdata->msg, h);
1616 }
1617
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001618 if (!msg_data)
1619 return;
1620
1621 hdr = msg_data->hdr_list.next;
1622 while (hdr && hdr != &msg_data->hdr_list) {
1623 pjsip_hdr *new_hdr;
1624
1625 new_hdr = pjsip_hdr_clone(tdata->pool, hdr);
1626 pjsip_msg_add_hdr(tdata->msg, new_hdr);
1627
1628 hdr = hdr->next;
1629 }
1630
1631 allow_body = (tdata->msg->body == NULL);
1632
1633 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
1634 pjsip_media_type ctype;
1635 pjsip_msg_body *body;
1636
1637 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
1638 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
1639 &msg_data->msg_body);
1640 tdata->msg->body = body;
1641 }
1642}
1643
1644
1645/*
1646 * Add route_set to outgoing requests
1647 */
1648void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
1649 const pjsip_route_hdr *route_set )
1650{
1651 const pjsip_route_hdr *r;
1652
1653 r = route_set->next;
1654 while (r != route_set) {
1655 pjsip_route_hdr *new_r;
1656
1657 new_r = pjsip_hdr_clone(tdata->pool, r);
1658 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
1659
1660 r = r->next;
1661 }
1662}
1663
1664
1665/*
1666 * Simple version of MIME type parsing (it doesn't support parameters)
1667 */
1668void pjsua_parse_media_type( pj_pool_t *pool,
1669 const pj_str_t *mime,
1670 pjsip_media_type *media_type)
1671{
1672 pj_str_t tmp;
1673 char *pos;
1674
Benny Prijonoac623b32006-07-03 15:19:31 +00001675 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001676
1677 pj_strdup_with_null(pool, &tmp, mime);
1678
1679 pos = pj_strchr(&tmp, '/');
1680 if (pos) {
1681 media_type->type.ptr = tmp.ptr;
1682 media_type->type.slen = (pos-tmp.ptr);
1683 media_type->subtype.ptr = pos+1;
1684 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
1685 } else {
1686 media_type->type = tmp;
1687 }
1688}
1689
1690
1691/*
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001692 * Internal function to init transport selector from transport id.
1693 */
1694void pjsua_init_tpselector(pjsua_transport_id tp_id,
1695 pjsip_tpselector *sel)
1696{
1697 pjsua_transport_data *tpdata;
1698 unsigned flag;
1699
1700 pj_bzero(sel, sizeof(*sel));
1701 if (tp_id == PJSUA_INVALID_ID)
1702 return;
1703
1704 pj_assert(tp_id >= 0 && tp_id < PJ_ARRAY_SIZE(pjsua_var.tpdata));
1705 tpdata = &pjsua_var.tpdata[tp_id];
1706
1707 flag = pjsip_transport_get_flag_from_type(tpdata->type);
1708
1709 if (flag & PJSIP_TRANSPORT_DATAGRAM) {
1710 sel->type = PJSIP_TPSELECTOR_TRANSPORT;
1711 sel->u.transport = tpdata->data.tp;
1712 } else {
1713 sel->type = PJSIP_TPSELECTOR_LISTENER;
1714 sel->u.listener = tpdata->data.factory;
1715 }
1716}
1717
1718
1719/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001720 * Verify that valid SIP url is given.
1721 */
1722PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
1723{
1724 pjsip_uri *p;
1725 pj_pool_t *pool;
1726 char *url;
1727 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
1728
1729 if (!len) return -1;
1730
1731 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
1732 if (!pool) return -1;
1733
1734 url = pj_pool_alloc(pool, len+1);
1735 pj_ansi_strcpy(url, c_url);
1736
1737 p = pjsip_parse_uri(pool, url, len, 0);
1738 if (!p || pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0)
1739 p = NULL;
1740
1741 pj_pool_release(pool);
1742 return p ? 0 : -1;
1743}
Benny Prijonoda9785b2007-04-02 20:43:06 +00001744
1745
1746/*
1747 * This is a utility function to dump the stack states to log, using
1748 * verbosity level 3.
1749 */
1750PJ_DEF(void) pjsua_dump(pj_bool_t detail)
1751{
1752 unsigned old_decor;
1753 unsigned i;
1754 char buf[1024];
1755
1756 PJ_LOG(3,(THIS_FILE, "Start dumping application states:"));
1757
1758 old_decor = pj_log_get_decor();
1759 pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
1760
1761 if (detail)
1762 pj_dump_config();
1763
1764 pjsip_endpt_dump(pjsua_get_pjsip_endpt(), detail);
1765
1766 pjmedia_endpt_dump(pjsua_get_pjmedia_endpt());
1767
1768 PJ_LOG(3,(THIS_FILE, "Dumping media transports:"));
1769 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
1770 pjsua_call *call = &pjsua_var.calls[i];
1771 pjmedia_sock_info skinfo;
1772
Benny Prijono4f093d22007-04-09 08:54:32 +00001773 /* MSVC complains about skinfo not being initialized */
1774 pj_bzero(&skinfo, sizeof(skinfo));
1775
Benny Prijonoda9785b2007-04-02 20:43:06 +00001776 pjmedia_transport_get_info(call->med_tp, &skinfo);
1777
1778 PJ_LOG(3,(THIS_FILE, " %s: %s:%d",
1779 (pjsua_var.media_cfg.enable_ice ? "ICE" : "UDP"),
1780 pj_inet_ntoa(skinfo.rtp_addr_name.sin_addr),
1781 (int)pj_ntohs(skinfo.rtp_addr_name.sin_port)));
1782 }
1783
1784 pjsip_tsx_layer_dump(detail);
1785 pjsip_ua_dump(detail);
1786
1787
1788 /* Dump all invite sessions: */
1789 PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:"));
1790
1791 if (pjsua_call_get_count() == 0) {
1792
1793 PJ_LOG(3,(THIS_FILE, " - no sessions -"));
1794
1795 } else {
1796 unsigned i;
1797
1798 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
1799 if (pjsua_call_is_active(i)) {
1800 pjsua_call_dump(i, detail, buf, sizeof(buf), " ");
1801 PJ_LOG(3,(THIS_FILE, "%s", buf));
1802 }
1803 }
1804 }
1805
1806 /* Dump presence status */
1807 pjsua_pres_dump(detail);
1808
1809 pj_log_set_decor(old_decor);
1810 PJ_LOG(3,(THIS_FILE, "Dump complete"));
1811}
1812