blob: 136364ffa5072573a6e3b2574d434288a597a5fd [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) {
681 pj_hostent he;
682
683 pjsua_var.stun_status = pj_gethostbyname(&pjsua_var.ua_cfg.stun_host, &he);
684
685 if (pjsua_var.stun_status == PJ_SUCCESS) {
686 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
687 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
688 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)3478);
689
690 PJ_LOG(3,(THIS_FILE,
691 "STUN server %.*s resolved, address is %s:%d",
692 (int)pjsua_var.ua_cfg.stun_host.slen,
693 pjsua_var.ua_cfg.stun_host.ptr,
694 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
695 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
696 }
697 } else {
698 char errmsg[PJ_ERR_MSG_SIZE];
699
700 pj_strerror(status, errmsg, sizeof(errmsg));
701 PJ_LOG(1,(THIS_FILE,
702 "DNS SRV resolution failed for STUN server %.*s: %s",
703 (int)pjsua_var.ua_cfg.stun_domain.slen,
704 pjsua_var.ua_cfg.stun_domain.ptr,
705 errmsg));
706 }
707 return;
708 }
709
710 pj_memcpy(&pjsua_var.stun_srv, &rec->entry[0].addr,
711 rec->entry[0].addr_len);
712
713 PJ_LOG(3,(THIS_FILE, "_stun._udp.%.*s resolved, found %d entry(s):",
714 (int)pjsua_var.ua_cfg.stun_domain.slen,
715 pjsua_var.ua_cfg.stun_domain.ptr,
716 rec->count));
717
718 for (i=0; i<rec->count; ++i) {
719 PJ_LOG(3,(THIS_FILE,
720 " %d: prio=%d, weight=%d %s:%d",
721 i, rec->entry[i].priority, rec->entry[i].weight,
722 pj_inet_ntoa(rec->entry[i].addr.ipv4.sin_addr),
723 (int)pj_ntohs(rec->entry[i].addr.ipv4.sin_port)));
724 }
725
726}
727
Benny Prijono268ca612006-02-07 12:34:11 +0000728/*
Benny Prijonoc97608e2007-03-23 16:34:20 +0000729 * Resolve STUN server.
730 */
731pj_status_t pjsua_resolve_stun_server(pj_bool_t wait)
732{
733 if (pjsua_var.stun_status == PJ_EUNKNOWN) {
734 /* Initialize STUN configuration */
735 pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0,
736 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
737 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
738
739 /* Start STUN server resolution */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000740
741 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000742
Benny Prijonoebbf6892007-03-24 17:37:25 +0000743 /* If stun_domain is specified, resolve STUN servers with DNS
744 * SRV resolution.
745 */
746 if (pjsua_var.ua_cfg.stun_domain.slen) {
747 pj_str_t res_type;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000748 pj_status_t status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000749
750 /* Fail if resolver is not configured */
751 if (pjsua_var.resolver == NULL) {
752 PJ_LOG(1,(THIS_FILE, "Nameserver must be configured when "
753 "stun_domain is specified"));
754 pjsua_var.stun_status = PJLIB_UTIL_EDNSNONS;
755 return PJLIB_UTIL_EDNSNONS;
756 }
757 res_type = pj_str("_stun._udp");
Benny Prijonoda9785b2007-04-02 20:43:06 +0000758 status =
Benny Prijonoebbf6892007-03-24 17:37:25 +0000759 pj_dns_srv_resolve(&pjsua_var.ua_cfg.stun_domain, &res_type,
760 3478, pjsua_var.pool, pjsua_var.resolver,
Benny Prijonoda9785b2007-04-02 20:43:06 +0000761 0, NULL, &stun_dns_srv_resolver_cb);
762 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +0000763 pjsua_perror(THIS_FILE, "Error starting DNS SRV resolution",
764 pjsua_var.stun_status);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000765 pjsua_var.stun_status = status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000766 return pjsua_var.stun_status;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000767 } else {
768 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000769 }
770 }
771 /* Otherwise if stun_host is specified, resolve STUN server with
772 * gethostbyname().
773 */
774 else if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000775 pj_hostent he;
776
Benny Prijonoebbf6892007-03-24 17:37:25 +0000777 pjsua_var.stun_status = pj_gethostbyname(&pjsua_var.ua_cfg.stun_host, &he);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000778
779 if (pjsua_var.stun_status == PJ_SUCCESS) {
780 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
781 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
782 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)3478);
Benny Prijonof75eceb2007-03-23 19:09:54 +0000783
Benny Prijonoebbf6892007-03-24 17:37:25 +0000784 PJ_LOG(3,(THIS_FILE,
Benny Prijonof75eceb2007-03-23 19:09:54 +0000785 "STUN server %.*s resolved, address is %s:%d",
Benny Prijonoebbf6892007-03-24 17:37:25 +0000786 (int)pjsua_var.ua_cfg.stun_host.slen,
787 pjsua_var.ua_cfg.stun_host.ptr,
Benny Prijonof75eceb2007-03-23 19:09:54 +0000788 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
789 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
Benny Prijonoc97608e2007-03-23 16:34:20 +0000790 }
Benny Prijonoebbf6892007-03-24 17:37:25 +0000791
Benny Prijonoc97608e2007-03-23 16:34:20 +0000792 }
Benny Prijonoebbf6892007-03-24 17:37:25 +0000793 /* Otherwise disable STUN. */
794 else {
795 pjsua_var.stun_status = PJ_SUCCESS;
796 }
797
798
Benny Prijonoc97608e2007-03-23 16:34:20 +0000799 return pjsua_var.stun_status;
800
801 } else if (pjsua_var.stun_status == PJ_EPENDING) {
802 /* STUN server resolution has been started, wait for the
803 * result.
804 */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000805 if (wait) {
806 while (pjsua_var.stun_status == PJ_EPENDING)
807 pjsua_handle_events(10);
808 }
809
810 return pjsua_var.stun_status;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000811
812 } else {
813 /* STUN server has been resolved, return the status */
814 return pjsua_var.stun_status;
815 }
816}
817
818/*
Benny Prijono268ca612006-02-07 12:34:11 +0000819 * Destroy pjsua.
820 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000821PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +0000822{
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000823 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +0000824
825 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000826 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +0000827
828 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000829 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
830 if (pjsua_var.thread[i]) {
831 pj_thread_join(pjsua_var.thread[i]);
832 pj_thread_destroy(pjsua_var.thread[i]);
833 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +0000834 }
Benny Prijono268ca612006-02-07 12:34:11 +0000835 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000836
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000837 if (pjsua_var.endpt) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000838 /* Terminate all calls. */
839 pjsua_call_hangup_all();
840
841 /* Terminate all presence subscriptions. */
842 pjsua_pres_shutdown();
843
844 /* Unregister, if required: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000845 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
846 if (!pjsua_var.acc[i].valid)
847 continue;
848
849 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000850 pjsua_acc_set_registration(i, PJ_FALSE);
851 }
852 }
853
854 /* Wait for some time to allow unregistration to complete: */
Benny Prijono9fc735d2006-05-28 14:58:12 +0000855 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
856 busy_sleep(1000);
857 }
Benny Prijono834aee32006-02-19 01:38:06 +0000858
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000859 /* Destroy media */
860 pjsua_media_subsys_destroy();
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000861
Benny Prijono268ca612006-02-07 12:34:11 +0000862 /* Destroy endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000863 if (pjsua_var.endpt) {
864 pjsip_endpt_destroy(pjsua_var.endpt);
865 pjsua_var.endpt = NULL;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000866 }
Benny Prijono268ca612006-02-07 12:34:11 +0000867
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000868 /* Destroy mutex */
869 if (pjsua_var.mutex) {
870 pj_mutex_destroy(pjsua_var.mutex);
871 pjsua_var.mutex = NULL;
872 }
873
874 /* Destroy pool and pool factory. */
875 if (pjsua_var.pool) {
876 pj_pool_release(pjsua_var.pool);
877 pjsua_var.pool = NULL;
878 pj_caching_pool_destroy(&pjsua_var.cp);
879 }
Benny Prijono268ca612006-02-07 12:34:11 +0000880
881
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000882 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
883
884 /* End logging */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000885 if (pjsua_var.log_file) {
886 pj_file_close(pjsua_var.log_file);
887 pjsua_var.log_file = NULL;
888 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000889
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000890 /* Shutdown PJLIB */
891 pj_shutdown();
892
Benny Prijonof762ee72006-12-01 11:14:37 +0000893 /* Clear pjsua_var */
894 pj_bzero(&pjsua_var, sizeof(pjsua_var));
895
Benny Prijono268ca612006-02-07 12:34:11 +0000896 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000897 return PJ_SUCCESS;
898}
899
900
901/**
902 * Application is recommended to call this function after all initialization
903 * is done, so that the library can do additional checking set up
904 * additional
905 *
906 * @return PJ_SUCCESS on success, or the appropriate error code.
907 */
908PJ_DEF(pj_status_t) pjsua_start(void)
909{
910 pj_status_t status;
911
912 status = pjsua_call_subsys_start();
913 if (status != PJ_SUCCESS)
914 return status;
915
916 status = pjsua_media_subsys_start();
917 if (status != PJ_SUCCESS)
918 return status;
919
920 status = pjsua_pres_start();
921 if (status != PJ_SUCCESS)
922 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000923
924 return PJ_SUCCESS;
925}
926
Benny Prijono9fc735d2006-05-28 14:58:12 +0000927
928/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000929 * Poll pjsua for events, and if necessary block the caller thread for
930 * the specified maximum interval (in miliseconds).
931 */
932PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
933{
934 unsigned count = 0;
935 pj_time_val tv;
936 pj_status_t status;
937
938 tv.sec = 0;
939 tv.msec = msec_timeout;
940 pj_time_val_normalize(&tv);
941
942 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
943
944 if (status != PJ_SUCCESS)
945 return -status;
946
947 return count;
948}
949
950
951/*
952 * Create memory pool.
953 */
954PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
955 pj_size_t increment)
956{
957 /* Pool factory is thread safe, no need to lock */
958 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
959 NULL);
960}
961
962
963/*
964 * Internal function to get SIP endpoint instance of pjsua, which is
965 * needed for example to register module, create transports, etc.
966 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000967 */
968PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
969{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000970 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000971}
972
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000973/*
974 * Internal function to get media endpoint instance.
975 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000976 */
977PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
978{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000979 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000980}
981
Benny Prijono97b87172006-08-24 14:25:14 +0000982/*
983 * Internal function to get PJSUA pool factory.
984 */
985PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
986{
987 return &pjsua_var.cp.factory;
988}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000989
990/*****************************************************************************
991 * PJSUA SIP Transport API.
992 */
993
994/*
995 * Create and initialize SIP socket (and possibly resolve public
996 * address via STUN, depending on config).
997 */
998static pj_status_t create_sip_udp_sock(pj_in_addr bound_addr,
999 int port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001000 pj_sock_t *p_sock,
1001 pj_sockaddr_in *p_pub_addr)
1002{
Benny Prijonoc97608e2007-03-23 16:34:20 +00001003 char ip_addr[32];
1004 pj_str_t stun_srv;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001005 pj_sock_t sock;
1006 pj_status_t status;
1007
Benny Prijonoc97608e2007-03-23 16:34:20 +00001008 /* Make sure STUN server resolution has completed */
1009 status = pjsua_resolve_stun_server(PJ_TRUE);
1010 if (status != PJ_SUCCESS) {
1011 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
1012 return status;
1013 }
1014
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001015 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock);
1016 if (status != PJ_SUCCESS) {
1017 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +00001018 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001019 }
1020
Benny Prijonof90ef4f2007-02-12 14:59:57 +00001021 status = pj_sock_bind_in(sock, pj_ntohl(bound_addr.s_addr),
1022 (pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001023 if (status != PJ_SUCCESS) {
1024 pjsua_perror(THIS_FILE, "bind() error", status);
1025 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001026 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001027 }
1028
Benny Prijonoe347cb02007-02-14 14:36:13 +00001029 /* If port is zero, get the bound port */
1030 if (port == 0) {
1031 pj_sockaddr_in bound_addr;
1032 int namelen = sizeof(bound_addr);
1033 status = pj_sock_getsockname(sock, &bound_addr, &namelen);
1034 if (status != PJ_SUCCESS) {
1035 pjsua_perror(THIS_FILE, "getsockname() error", status);
1036 pj_sock_close(sock);
1037 return status;
1038 }
1039
1040 port = pj_ntohs(bound_addr.sin_port);
1041 }
1042
Benny Prijonoc97608e2007-03-23 16:34:20 +00001043 if (pjsua_var.stun_srv.addr.sa_family != 0) {
1044 pj_ansi_strcpy(ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
1045 stun_srv = pj_str(ip_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001046 } else {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001047 stun_srv.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001048 }
1049
1050 /* Get the published address, either by STUN or by resolving
1051 * the name of local host.
1052 */
Benny Prijonoc97608e2007-03-23 16:34:20 +00001053 if (stun_srv.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +00001054 /*
1055 * STUN is specified, resolve the address with STUN.
1056 */
Benny Prijono14c2b862007-02-21 00:40:05 +00001057 status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
Benny Prijonoc97608e2007-03-23 16:34:20 +00001058 &stun_srv, 3478,
1059 &stun_srv, 3478,
1060 p_pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001061 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +00001062 pjsua_perror(THIS_FILE, "Error contacting STUN server", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001063 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001064 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001065 }
1066
Benny Prijono0a5cad82006-09-26 13:21:02 +00001067 } else if (p_pub_addr->sin_addr.s_addr != 0) {
1068 /*
1069 * Public address is already specified, no need to resolve the
1070 * address, only set the port.
1071 */
Benny Prijonoe347cb02007-02-14 14:36:13 +00001072 if (p_pub_addr->sin_port == 0)
1073 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001074
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001075 } else {
1076
Benny Prijono594e4c52006-09-14 18:51:01 +00001077 pj_bzero(p_pub_addr, sizeof(pj_sockaddr_in));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001078
Benny Prijono594e4c52006-09-14 18:51:01 +00001079 status = pj_gethostip(&p_pub_addr->sin_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001080 if (status != PJ_SUCCESS) {
Benny Prijonob43bad72007-01-20 05:11:08 +00001081 pjsua_perror(THIS_FILE, "Unable to get local host IP", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001082 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001083 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001084 }
1085
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001086 p_pub_addr->sin_family = PJ_AF_INET;
1087 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001088 }
1089
1090 *p_sock = sock;
1091
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001092 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
1093 pj_inet_ntoa(p_pub_addr->sin_addr),
1094 (int)pj_ntohs(p_pub_addr->sin_port)));
1095
Benny Prijonod8410532006-06-15 11:04:33 +00001096 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001097}
1098
1099
1100/*
1101 * Create SIP transport.
1102 */
1103PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
1104 const pjsua_transport_config *cfg,
1105 pjsua_transport_id *p_id)
1106{
1107 pjsip_transport *tp;
1108 unsigned id;
1109 pj_status_t status;
1110
1111 PJSUA_LOCK();
1112
1113 /* Find empty transport slot */
1114 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001115 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001116 break;
1117 }
1118
1119 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1120 status = PJ_ETOOMANY;
1121 pjsua_perror(THIS_FILE, "Error creating transport", status);
1122 goto on_return;
1123 }
1124
1125 /* Create the transport */
1126 if (type == PJSIP_TRANSPORT_UDP) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001127 /*
1128 * Create UDP transport.
1129 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001130 pjsua_transport_config config;
Benny Prijonod8410532006-06-15 11:04:33 +00001131 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001132 pj_sockaddr_in bound_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001133 pj_sockaddr_in pub_addr;
1134 pjsip_host_port addr_name;
1135
1136 /* Supply default config if it's not specified */
1137 if (cfg == NULL) {
1138 pjsua_transport_config_default(&config);
1139 cfg = &config;
1140 }
1141
Benny Prijono0a5cad82006-09-26 13:21:02 +00001142 /* Initialize bound address, if any */
1143 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
1144 if (cfg->bound_addr.slen) {
1145 status = pj_sockaddr_in_set_str_addr(&bound_addr,&cfg->bound_addr);
1146 if (status != PJ_SUCCESS) {
1147 pjsua_perror(THIS_FILE,
1148 "Unable to resolve transport bound address",
1149 status);
1150 goto on_return;
1151 }
1152 }
1153
1154 /* Initialize the public address from the config, if any */
1155 pj_sockaddr_in_init(&pub_addr, NULL, (pj_uint16_t)cfg->port);
1156 if (cfg->public_addr.slen) {
1157 status = pj_sockaddr_in_set_str_addr(&pub_addr, &cfg->public_addr);
1158 if (status != PJ_SUCCESS) {
1159 pjsua_perror(THIS_FILE,
1160 "Unable to resolve transport public address",
1161 status);
1162 goto on_return;
1163 }
1164 }
1165
1166 /* Create the socket and possibly resolve the address with STUN
1167 * (only when public address is not specified).
1168 */
1169 status = create_sip_udp_sock(bound_addr.sin_addr, cfg->port,
Benny Prijono0a5cad82006-09-26 13:21:02 +00001170 &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001171 if (status != PJ_SUCCESS)
1172 goto on_return;
1173
1174 addr_name.host = pj_str(pj_inet_ntoa(pub_addr.sin_addr));
1175 addr_name.port = pj_ntohs(pub_addr.sin_port);
1176
1177 /* Create UDP transport */
1178 status = pjsip_udp_transport_attach( pjsua_var.endpt, sock,
1179 &addr_name, 1,
1180 &tp);
1181 if (status != PJ_SUCCESS) {
1182 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
1183 status);
1184 pj_sock_close(sock);
1185 goto on_return;
1186 }
1187
Benny Prijonoe93e2872006-06-28 16:46:49 +00001188
1189 /* Save the transport */
1190 pjsua_var.tpdata[id].type = type;
1191 pjsua_var.tpdata[id].local_name = tp->local_name;
1192 pjsua_var.tpdata[id].data.tp = tp;
1193
Benny Prijono3569c0d2007-04-06 10:29:20 +00001194#if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0
1195
Benny Prijonoe93e2872006-06-28 16:46:49 +00001196 } else if (type == PJSIP_TRANSPORT_TCP) {
1197 /*
1198 * Create TCP transport.
1199 */
1200 pjsua_transport_config config;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001201 pjsip_host_port a_name;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001202 pjsip_tpfactory *tcp;
1203 pj_sockaddr_in local_addr;
1204
1205 /* Supply default config if it's not specified */
1206 if (cfg == NULL) {
1207 pjsua_transport_config_default(&config);
1208 cfg = &config;
1209 }
1210
1211 /* Init local address */
1212 pj_sockaddr_in_init(&local_addr, 0, 0);
1213
1214 if (cfg->port)
1215 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1216
Benny Prijono0a5cad82006-09-26 13:21:02 +00001217 if (cfg->bound_addr.slen) {
1218 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1219 if (status != PJ_SUCCESS) {
1220 pjsua_perror(THIS_FILE,
1221 "Unable to resolve transport bound address",
1222 status);
1223 goto on_return;
1224 }
1225 }
1226
1227 /* Init published name */
1228 pj_bzero(&a_name, sizeof(pjsip_host_port));
1229 if (cfg->public_addr.slen)
1230 a_name.host = cfg->public_addr;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001231
1232 /* Create the TCP transport */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001233 status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr,
1234 &a_name, 1, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001235
1236 if (status != PJ_SUCCESS) {
1237 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1238 status);
1239 goto on_return;
1240 }
1241
1242 /* Save the transport */
1243 pjsua_var.tpdata[id].type = type;
1244 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1245 pjsua_var.tpdata[id].data.factory = tcp;
1246
Benny Prijono3569c0d2007-04-06 10:29:20 +00001247#endif /* PJ_HAS_TCP */
1248
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001249#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1250 } else if (type == PJSIP_TRANSPORT_TLS) {
1251 /*
1252 * Create TLS transport.
1253 */
Benny Prijonof3bbc132006-12-25 06:43:59 +00001254 /*
1255 * Create TCP transport.
1256 */
1257 pjsua_transport_config config;
1258 pjsip_host_port a_name;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001259 pjsip_tpfactory *tls;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001260 pj_sockaddr_in local_addr;
1261
1262 /* Supply default config if it's not specified */
1263 if (cfg == NULL) {
1264 pjsua_transport_config_default(&config);
1265 config.port = 5061;
1266 cfg = &config;
1267 }
1268
1269 /* Init local address */
1270 pj_sockaddr_in_init(&local_addr, 0, 0);
1271
1272 if (cfg->port)
1273 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1274
1275 if (cfg->bound_addr.slen) {
1276 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1277 if (status != PJ_SUCCESS) {
1278 pjsua_perror(THIS_FILE,
1279 "Unable to resolve transport bound address",
1280 status);
1281 goto on_return;
1282 }
1283 }
1284
1285 /* Init published name */
1286 pj_bzero(&a_name, sizeof(pjsip_host_port));
1287 if (cfg->public_addr.slen)
1288 a_name.host = cfg->public_addr;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001289
1290 status = pjsip_tls_transport_start(pjsua_var.endpt,
Benny Prijonof3bbc132006-12-25 06:43:59 +00001291 &cfg->tls_setting,
1292 &local_addr, &a_name, 1, &tls);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001293 if (status != PJ_SUCCESS) {
1294 pjsua_perror(THIS_FILE, "Error creating SIP TLS listener",
1295 status);
1296 goto on_return;
1297 }
1298
1299 /* Save the transport */
1300 pjsua_var.tpdata[id].type = type;
1301 pjsua_var.tpdata[id].local_name = tls->addr_name;
1302 pjsua_var.tpdata[id].data.factory = tls;
1303#endif
1304
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001305 } else {
1306 status = PJSIP_EUNSUPTRANSPORT;
1307 pjsua_perror(THIS_FILE, "Error creating transport", status);
1308 goto on_return;
1309 }
1310
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001311
1312 /* Return the ID */
1313 if (p_id) *p_id = id;
1314
1315 status = PJ_SUCCESS;
1316
1317on_return:
1318
1319 PJSUA_UNLOCK();
1320
Benny Prijonod8410532006-06-15 11:04:33 +00001321 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001322}
1323
1324
1325/*
1326 * Register transport that has been created by application.
1327 */
1328PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1329 pjsua_transport_id *p_id)
1330{
1331 unsigned id;
1332
1333 PJSUA_LOCK();
1334
1335 /* Find empty transport slot */
1336 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001337 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001338 break;
1339 }
1340
1341 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1342 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1343 PJSUA_UNLOCK();
1344 return PJ_ETOOMANY;
1345 }
1346
1347 /* Save the transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001348 pjsua_var.tpdata[id].type = tp->key.type;
1349 pjsua_var.tpdata[id].local_name = tp->local_name;
1350 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001351
1352 /* Return the ID */
1353 if (p_id) *p_id = id;
1354
1355 PJSUA_UNLOCK();
1356
1357 return PJ_SUCCESS;
1358}
1359
1360
1361/*
1362 * Enumerate all transports currently created in the system.
1363 */
1364PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1365 unsigned *p_count )
1366{
1367 unsigned i, count;
1368
1369 PJSUA_LOCK();
1370
1371 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
1372 ++i)
1373 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001374 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001375 continue;
1376
1377 id[count++] = i;
1378 }
1379
1380 *p_count = count;
1381
1382 PJSUA_UNLOCK();
1383
1384 return PJ_SUCCESS;
1385}
1386
1387
1388/*
1389 * Get information about transports.
1390 */
1391PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
1392 pjsua_transport_info *info)
1393{
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001394 pjsua_transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00001395 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001396
Benny Prijonoac623b32006-07-03 15:19:31 +00001397 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001398
1399 /* Make sure id is in range. */
1400 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1401
1402 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001403 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001404
1405 PJSUA_LOCK();
1406
Benny Prijonoe93e2872006-06-28 16:46:49 +00001407 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
1408
1409 pjsip_transport *tp = t->data.tp;
1410
1411 if (tp == NULL) {
1412 PJSUA_UNLOCK();
1413 return PJ_EINVALIDOP;
1414 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001415
Benny Prijonoe93e2872006-06-28 16:46:49 +00001416 info->id = id;
1417 info->type = tp->key.type;
1418 info->type_name = pj_str(tp->type_name);
1419 info->info = pj_str(tp->info);
1420 info->flag = tp->flag;
1421 info->addr_len = tp->addr_len;
1422 info->local_addr = tp->local_addr;
1423 info->local_name = tp->local_name;
1424 info->usage_count = pj_atomic_get(tp->ref_cnt);
1425
Benny Prijono0a5cad82006-09-26 13:21:02 +00001426 status = PJ_SUCCESS;
1427
Benny Prijonoe93e2872006-06-28 16:46:49 +00001428 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
1429
1430 pjsip_tpfactory *factory = t->data.factory;
1431
1432 if (factory == NULL) {
1433 PJSUA_UNLOCK();
1434 return PJ_EINVALIDOP;
1435 }
1436
1437 info->id = id;
1438 info->type = t->type;
1439 info->type_name = pj_str("TCP");
1440 info->info = pj_str("TCP transport");
1441 info->flag = factory->flag;
1442 info->addr_len = sizeof(factory->local_addr);
1443 info->local_addr = factory->local_addr;
1444 info->local_name = factory->addr_name;
1445 info->usage_count = 0;
1446
Benny Prijono0a5cad82006-09-26 13:21:02 +00001447 status = PJ_SUCCESS;
1448
1449 } else {
1450 pj_assert(!"Unsupported transport");
1451 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001452 }
1453
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001454
1455 PJSUA_UNLOCK();
1456
Benny Prijono0a5cad82006-09-26 13:21:02 +00001457 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001458}
1459
1460
1461/*
1462 * Disable a transport or re-enable it.
1463 */
1464PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
1465 pj_bool_t enabled)
1466{
1467 /* Make sure id is in range. */
1468 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1469
1470 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001471 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001472
1473
1474 /* To be done!! */
1475 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001476 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001477
1478 return PJ_EINVALIDOP;
1479}
1480
1481
1482/*
1483 * Close the transport.
1484 */
1485PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
1486 pj_bool_t force )
1487{
Benny Prijono5ff61872007-02-01 03:37:11 +00001488 pj_status_t status;
1489
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001490 /* Make sure id is in range. */
1491 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1492
1493 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001494 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001495
Benny Prijono0a5cad82006-09-26 13:21:02 +00001496 /* Note: destroy() may not work if there are objects still referencing
1497 * the transport.
1498 */
1499 if (force) {
1500 switch (pjsua_var.tpdata[id].type) {
1501 case PJSIP_TRANSPORT_UDP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001502 status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
1503 if (status != PJ_SUCCESS)
1504 return status;
1505 status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
1506 if (status != PJ_SUCCESS)
1507 return status;
1508 break;
1509
1510 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001511 case PJSIP_TRANSPORT_TCP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001512 /* This will close the TCP listener, but existing TCP/TLS
1513 * connections (if any) will still linger
1514 */
1515 status = (*pjsua_var.tpdata[id].data.factory->destroy)
1516 (pjsua_var.tpdata[id].data.factory);
1517 if (status != PJ_SUCCESS)
1518 return status;
1519
Benny Prijono0a5cad82006-09-26 13:21:02 +00001520 break;
Benny Prijono5ff61872007-02-01 03:37:11 +00001521
Benny Prijono1ebd6142006-10-19 15:48:02 +00001522 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001523 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001524 }
1525
1526 } else {
Benny Prijono5ff61872007-02-01 03:37:11 +00001527 /* If force is not specified, transports will be closed at their
1528 * convenient time. However this will leak PJSUA-API transport
1529 * descriptors as PJSUA-API wouldn't know when exactly the
1530 * transport is closed thus it can't cleanup PJSUA transport
1531 * descriptor.
1532 */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001533 switch (pjsua_var.tpdata[id].type) {
1534 case PJSIP_TRANSPORT_UDP:
1535 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
Benny Prijono5ff61872007-02-01 03:37:11 +00001536 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001537 case PJSIP_TRANSPORT_TCP:
1538 return (*pjsua_var.tpdata[id].data.factory->destroy)
1539 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001540 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001541 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001542 }
1543 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001544
Benny Prijono5ff61872007-02-01 03:37:11 +00001545 /* Cleanup pjsua data when force is applied */
1546 if (force) {
1547 pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED;
1548 pjsua_var.tpdata[id].data.ptr = NULL;
1549 }
1550
1551 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001552}
1553
1554
1555/*
1556 * Add additional headers etc in msg_data specified by application
1557 * when sending requests.
1558 */
1559void pjsua_process_msg_data(pjsip_tx_data *tdata,
1560 const pjsua_msg_data *msg_data)
1561{
1562 pj_bool_t allow_body;
1563 const pjsip_hdr *hdr;
1564
Benny Prijono56315612006-07-18 14:39:40 +00001565 /* Always add User-Agent */
1566 if (pjsua_var.ua_cfg.user_agent.slen &&
1567 tdata->msg->type == PJSIP_REQUEST_MSG)
1568 {
1569 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1570 pjsip_hdr *h;
1571 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
1572 &STR_USER_AGENT,
1573 &pjsua_var.ua_cfg.user_agent);
1574 pjsip_msg_add_hdr(tdata->msg, h);
1575 }
1576
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001577 if (!msg_data)
1578 return;
1579
1580 hdr = msg_data->hdr_list.next;
1581 while (hdr && hdr != &msg_data->hdr_list) {
1582 pjsip_hdr *new_hdr;
1583
1584 new_hdr = pjsip_hdr_clone(tdata->pool, hdr);
1585 pjsip_msg_add_hdr(tdata->msg, new_hdr);
1586
1587 hdr = hdr->next;
1588 }
1589
1590 allow_body = (tdata->msg->body == NULL);
1591
1592 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
1593 pjsip_media_type ctype;
1594 pjsip_msg_body *body;
1595
1596 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
1597 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
1598 &msg_data->msg_body);
1599 tdata->msg->body = body;
1600 }
1601}
1602
1603
1604/*
1605 * Add route_set to outgoing requests
1606 */
1607void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
1608 const pjsip_route_hdr *route_set )
1609{
1610 const pjsip_route_hdr *r;
1611
1612 r = route_set->next;
1613 while (r != route_set) {
1614 pjsip_route_hdr *new_r;
1615
1616 new_r = pjsip_hdr_clone(tdata->pool, r);
1617 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
1618
1619 r = r->next;
1620 }
1621}
1622
1623
1624/*
1625 * Simple version of MIME type parsing (it doesn't support parameters)
1626 */
1627void pjsua_parse_media_type( pj_pool_t *pool,
1628 const pj_str_t *mime,
1629 pjsip_media_type *media_type)
1630{
1631 pj_str_t tmp;
1632 char *pos;
1633
Benny Prijonoac623b32006-07-03 15:19:31 +00001634 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001635
1636 pj_strdup_with_null(pool, &tmp, mime);
1637
1638 pos = pj_strchr(&tmp, '/');
1639 if (pos) {
1640 media_type->type.ptr = tmp.ptr;
1641 media_type->type.slen = (pos-tmp.ptr);
1642 media_type->subtype.ptr = pos+1;
1643 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
1644 } else {
1645 media_type->type = tmp;
1646 }
1647}
1648
1649
1650/*
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001651 * Internal function to init transport selector from transport id.
1652 */
1653void pjsua_init_tpselector(pjsua_transport_id tp_id,
1654 pjsip_tpselector *sel)
1655{
1656 pjsua_transport_data *tpdata;
1657 unsigned flag;
1658
1659 pj_bzero(sel, sizeof(*sel));
1660 if (tp_id == PJSUA_INVALID_ID)
1661 return;
1662
1663 pj_assert(tp_id >= 0 && tp_id < PJ_ARRAY_SIZE(pjsua_var.tpdata));
1664 tpdata = &pjsua_var.tpdata[tp_id];
1665
1666 flag = pjsip_transport_get_flag_from_type(tpdata->type);
1667
1668 if (flag & PJSIP_TRANSPORT_DATAGRAM) {
1669 sel->type = PJSIP_TPSELECTOR_TRANSPORT;
1670 sel->u.transport = tpdata->data.tp;
1671 } else {
1672 sel->type = PJSIP_TPSELECTOR_LISTENER;
1673 sel->u.listener = tpdata->data.factory;
1674 }
1675}
1676
1677
1678/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001679 * Verify that valid SIP url is given.
1680 */
1681PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
1682{
1683 pjsip_uri *p;
1684 pj_pool_t *pool;
1685 char *url;
1686 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
1687
1688 if (!len) return -1;
1689
1690 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
1691 if (!pool) return -1;
1692
1693 url = pj_pool_alloc(pool, len+1);
1694 pj_ansi_strcpy(url, c_url);
1695
1696 p = pjsip_parse_uri(pool, url, len, 0);
1697 if (!p || pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0)
1698 p = NULL;
1699
1700 pj_pool_release(pool);
1701 return p ? 0 : -1;
1702}
Benny Prijonoda9785b2007-04-02 20:43:06 +00001703
1704
1705/*
1706 * This is a utility function to dump the stack states to log, using
1707 * verbosity level 3.
1708 */
1709PJ_DEF(void) pjsua_dump(pj_bool_t detail)
1710{
1711 unsigned old_decor;
1712 unsigned i;
1713 char buf[1024];
1714
1715 PJ_LOG(3,(THIS_FILE, "Start dumping application states:"));
1716
1717 old_decor = pj_log_get_decor();
1718 pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
1719
1720 if (detail)
1721 pj_dump_config();
1722
1723 pjsip_endpt_dump(pjsua_get_pjsip_endpt(), detail);
1724
1725 pjmedia_endpt_dump(pjsua_get_pjmedia_endpt());
1726
1727 PJ_LOG(3,(THIS_FILE, "Dumping media transports:"));
1728 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
1729 pjsua_call *call = &pjsua_var.calls[i];
1730 pjmedia_sock_info skinfo;
1731
1732 pjmedia_transport_get_info(call->med_tp, &skinfo);
1733
1734 PJ_LOG(3,(THIS_FILE, " %s: %s:%d",
1735 (pjsua_var.media_cfg.enable_ice ? "ICE" : "UDP"),
1736 pj_inet_ntoa(skinfo.rtp_addr_name.sin_addr),
1737 (int)pj_ntohs(skinfo.rtp_addr_name.sin_port)));
1738 }
1739
1740 pjsip_tsx_layer_dump(detail);
1741 pjsip_ua_dump(detail);
1742
1743
1744 /* Dump all invite sessions: */
1745 PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:"));
1746
1747 if (pjsua_call_get_count() == 0) {
1748
1749 PJ_LOG(3,(THIS_FILE, " - no sessions -"));
1750
1751 } else {
1752 unsigned i;
1753
1754 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
1755 if (pjsua_call_is_active(i)) {
1756 pjsua_call_dump(i, detail, buf, sizeof(buf), " ");
1757 PJ_LOG(3,(THIS_FILE, "%s", buf));
1758 }
1759 }
1760 }
1761
1762 /* Dump presence status */
1763 pjsua_pres_dump(detail);
1764
1765 pj_log_set_decor(old_decor);
1766 PJ_LOG(3,(THIS_FILE, "Dump complete"));
1767}
1768