blob: d01a02b028ffca2e7f86640166b586312c4b959c [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;
144 pjmedia_sdp_session *sdp;
145 const pjsip_hdr *cap_hdr;
146 pj_status_t status;
147
148 /* Only want to handle OPTIONS requests */
149 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
150 &pjsip_options_method) != 0)
151 {
152 return PJ_FALSE;
153 }
154
155 /* Create basic response. */
156 status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL,
157 &tdata);
158 if (status != PJ_SUCCESS) {
159 pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status);
160 return PJ_TRUE;
161 }
162
163 /* Add Allow header */
164 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL);
165 if (cap_hdr) {
166 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
167 }
168
169 /* Add Accept header */
170 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL);
171 if (cap_hdr) {
172 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
173 }
174
175 /* Add Supported header */
176 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL);
177 if (cap_hdr) {
178 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
179 }
180
181 /* Add Allow-Events header from the evsub module */
182 cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL);
183 if (cap_hdr) {
184 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
185 }
186
187 /* Add User-Agent header */
188 if (pjsua_var.ua_cfg.user_agent.slen) {
189 const pj_str_t USER_AGENT = { "User-Agent", 10};
190 pjsip_hdr *h;
191
192 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
193 &USER_AGENT,
194 &pjsua_var.ua_cfg.user_agent);
195 pjsip_msg_add_hdr(tdata->msg, h);
196 }
197
198 /* Add SDP body, using call0's RTP address */
199 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
200 &pjsua_var.calls[0].skinfo, &sdp);
201 if (status == PJ_SUCCESS) {
202 pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
203 }
204
205 /* Send response statelessly */
206 pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
207 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL);
208 if (status != PJ_SUCCESS)
209 pjsip_tx_data_dec_ref(tdata);
210
211 return PJ_TRUE;
212}
213
214
215/* The module instance. */
216static pjsip_module pjsua_options_handler =
217{
218 NULL, NULL, /* prev, next. */
219 { "mod-pjsua-options", 17 }, /* Name. */
220 -1, /* Id */
221 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
222 NULL, /* load() */
223 NULL, /* start() */
224 NULL, /* stop() */
225 NULL, /* unload() */
226 &options_on_rx_request, /* on_rx_request() */
227 NULL, /* on_rx_response() */
228 NULL, /* on_tx_request. */
229 NULL, /* on_tx_response() */
230 NULL, /* on_tsx_state() */
231
232};
233
234
235/*****************************************************************************
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000236 * These two functions are the main callbacks registered to PJSIP stack
237 * to receive SIP request and response messages that are outside any
238 * dialogs and any transactions.
239 */
Benny Prijono268ca612006-02-07 12:34:11 +0000240
241/*
242 * Handler for receiving incoming requests.
243 *
244 * This handler serves multiple purposes:
245 * - it receives requests outside dialogs.
246 * - it receives requests inside dialogs, when the requests are
247 * unhandled by other dialog usages. Example of these
248 * requests are: MESSAGE.
249 */
250static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
251{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000252 pj_bool_t processed = PJ_FALSE;
253
254 PJSUA_LOCK();
Benny Prijono38998232006-02-08 22:44:25 +0000255
Benny Prijono84126ab2006-02-09 09:30:09 +0000256 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000257
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000258 processed = pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000259 }
260
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000261 PJSUA_UNLOCK();
262
263 return processed;
Benny Prijono268ca612006-02-07 12:34:11 +0000264}
265
266
267/*
268 * Handler for receiving incoming responses.
269 *
270 * This handler serves multiple purposes:
271 * - it receives strayed responses (i.e. outside any dialog and
272 * outside any transactions).
273 * - it receives responses coming to a transaction, when pjsua
274 * module is set as transaction user for the transaction.
275 * - it receives responses inside a dialog, when these responses
276 * are unhandled by other dialog usages.
277 */
278static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
279{
280 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000281 return PJ_FALSE;
282}
283
284
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000285/*****************************************************************************
286 * Logging.
287 */
288
289/* Log callback */
290static void log_writer(int level, const char *buffer, int len)
Benny Prijono268ca612006-02-07 12:34:11 +0000291{
Benny Prijono572d4852006-11-23 21:50:02 +0000292 /* Write to file, stdout or application callback. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000293
294 if (pjsua_var.log_file) {
295 pj_ssize_t size = len;
296 pj_file_write(pjsua_var.log_file, buffer, &size);
Benny Prijonoee738712007-03-16 09:25:47 +0000297 pj_file_flush(pjsua_var.log_file);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000298 }
299
Benny Prijono572d4852006-11-23 21:50:02 +0000300 if (level <= (int)pjsua_var.log_cfg.console_level) {
301 if (pjsua_var.log_cfg.cb)
302 (*pjsua_var.log_cfg.cb)(level, buffer, len);
303 else
304 pj_log_write(level, buffer, len);
305 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000306}
307
308
309/*
310 * Application can call this function at any time (after pjsua_create(), of
311 * course) to change logging settings.
312 */
313PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg)
314{
315 pj_status_t status;
316
317 /* Save config. */
318 pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg);
319
320 /* Redirect log function to ours */
321 pj_log_set_log_func( &log_writer );
322
323 /* Close existing file, if any */
324 if (pjsua_var.log_file) {
325 pj_file_close(pjsua_var.log_file);
326 pjsua_var.log_file = NULL;
327 }
328
329 /* If output log file is desired, create the file: */
330 if (pjsua_var.log_cfg.log_filename.slen) {
331
332 status = pj_file_open(pjsua_var.pool,
333 pjsua_var.log_cfg.log_filename.ptr,
334 PJ_O_WRONLY,
335 &pjsua_var.log_file);
336
337 if (status != PJ_SUCCESS) {
338 pjsua_perror(THIS_FILE, "Error creating log file", status);
339 return status;
340 }
341 }
342
343 /* Unregister msg logging if it's previously registered */
344 if (pjsua_msg_logger.id >= 0) {
345 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
346 pjsua_msg_logger.id = -1;
347 }
348
349 /* Enable SIP message logging */
350 if (pjsua_var.log_cfg.msg_logging)
351 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
352
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000353 return PJ_SUCCESS;
354}
355
356
357/*****************************************************************************
358 * PJSUA Base API.
359 */
360
361/* Worker thread function. */
362static int worker_thread(void *arg)
363{
364 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000365
Benny Prijono268ca612006-02-07 12:34:11 +0000366 PJ_UNUSED_ARG(arg);
367
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000368 while (!pjsua_var.thread_quit_flag) {
369 int count;
370
371 count = pjsua_handle_events(TIMEOUT);
372 if (count < 0)
373 pj_thread_sleep(TIMEOUT);
374 }
Benny Prijono268ca612006-02-07 12:34:11 +0000375
376 return 0;
377}
378
Benny Prijonodc39fe82006-05-26 12:17:46 +0000379
Benny Prijono268ca612006-02-07 12:34:11 +0000380/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000381 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000382 */
383PJ_DEF(pj_status_t) pjsua_create(void)
384{
385 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000386
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000387 /* Init pjsua data */
388 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000389
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000390 /* Set default logging settings */
391 pjsua_logging_config_default(&pjsua_var.log_cfg);
392
393 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000394 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000395 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
396
Benny Prijono268ca612006-02-07 12:34:11 +0000397
Benny Prijonofccab712006-02-22 22:23:22 +0000398 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000399 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000400 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000401
Benny Prijonoec921342007-03-24 13:00:30 +0000402 /* Init PJNATH */
403 status = pjnath_init();
404 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono268ca612006-02-07 12:34:11 +0000405
Benny Prijono094d3ad2006-11-21 08:41:00 +0000406 /* Set default sound device ID */
407 pjsua_var.cap_dev = pjsua_var.play_dev = -1;
408
Benny Prijono268ca612006-02-07 12:34:11 +0000409 /* Init caching pool. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000410 pj_caching_pool_init(&pjsua_var.cp, &pj_pool_factory_default_policy, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000411
412 /* Create memory pool for application. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000413 pjsua_var.pool = pjsua_pool_create("pjsua", 4000, 4000);
414
415 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000416
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000417 /* Create mutex */
418 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
419 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000420 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000421 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000422 return status;
423 }
424
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000425 /* Must create SIP endpoint to initialize SIP parser. The parser
426 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000427 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000428 status = pjsip_endpt_create(&pjsua_var.cp.factory,
429 pj_gethostname()->ptr,
430 &pjsua_var.endpt);
431 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000432
433
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000434 return PJ_SUCCESS;
435}
436
437
438/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000439 * Initialize pjsua with the specified settings. All the settings are
440 * optional, and the default values will be used when the config is not
441 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000442 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000443PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
444 const pjsua_logging_config *log_cfg,
445 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000446{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000447 pjsua_config default_cfg;
448 pjsua_media_config default_media_cfg;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000449 const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
Benny Prijonodc39fe82006-05-26 12:17:46 +0000450 pj_status_t status;
451
452
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000453 /* Create default configurations when the config is not supplied */
454
455 if (ua_cfg == NULL) {
456 pjsua_config_default(&default_cfg);
457 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000458 }
459
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000460 if (media_cfg == NULL) {
461 pjsua_media_config_default(&default_media_cfg);
462 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000463 }
464
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000465 /* Initialize logging first so that info/errors can be captured */
466 if (log_cfg) {
467 status = pjsua_reconfigure_logging(log_cfg);
468 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono9fc735d2006-05-28 14:58:12 +0000469 }
470
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000471 /* If nameserver is configured, create DNS resolver instance and
472 * set it to be used by SIP resolver.
473 */
474 if (ua_cfg->nameserver_count) {
475#if PJSIP_HAS_RESOLVER
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000476 unsigned i;
477
478 /* Create DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000479 status = pjsip_endpt_create_resolver(pjsua_var.endpt,
480 &pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000481 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
482
483 /* Configure nameserver for the DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000484 status = pj_dns_resolver_set_ns(pjsua_var.resolver,
485 ua_cfg->nameserver_count,
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000486 ua_cfg->nameserver, NULL);
487 if (status != PJ_SUCCESS) {
488 pjsua_perror(THIS_FILE, "Error setting nameserver", status);
489 return status;
490 }
491
492 /* Set this DNS resolver to be used by the SIP resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000493 status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000494 if (status != PJ_SUCCESS) {
495 pjsua_perror(THIS_FILE, "Error setting DNS resolver", status);
496 return status;
497 }
498
499 /* Print nameservers */
500 for (i=0; i<ua_cfg->nameserver_count; ++i) {
501 PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added",
502 (int)ua_cfg->nameserver[i].slen,
503 ua_cfg->nameserver[i].ptr));
504 }
505#else
506 PJ_LOG(2,(THIS_FILE,
507 "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)"));
508#endif
509 }
510
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000511 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000512
513 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000514 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
515 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000516
Benny Prijonodc39fe82006-05-26 12:17:46 +0000517
518 /* Initialize UA layer module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000519 status = pjsip_ua_init_module( pjsua_var.endpt, NULL );
520 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000521
Benny Prijonodc39fe82006-05-26 12:17:46 +0000522
Benny Prijono053f5222006-11-11 16:16:04 +0000523 /* Initialize Replaces support. */
524 status = pjsip_replaces_init_module( pjsua_var.endpt );
525 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
526
527
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000528 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000529 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000530 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000531 {
532 NULL, NULL, /* prev, next. */
533 { "mod-pjsua", 9 }, /* Name. */
534 -1, /* Id */
535 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
536 NULL, /* load() */
537 NULL, /* start() */
538 NULL, /* stop() */
539 NULL, /* unload() */
540 &mod_pjsua_on_rx_request, /* on_rx_request() */
541 &mod_pjsua_on_rx_response, /* on_rx_response() */
542 NULL, /* on_tx_request. */
543 NULL, /* on_tx_response() */
544 NULL, /* on_tsx_state() */
545 };
546
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000547 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000548
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000549 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
550 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000551 }
552
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000553
Benny Prijonodc39fe82006-05-26 12:17:46 +0000554
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000555 /* Initialize PJSUA call subsystem: */
556 status = pjsua_call_subsys_init(ua_cfg);
557 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000558 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000559
560
Benny Prijonoc97608e2007-03-23 16:34:20 +0000561 /* Start resolving STUN server */
562 status = pjsua_resolve_stun_server(PJ_FALSE);
563 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
564 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
565 return status;
566 }
567
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000568 /* Initialize PJSUA media subsystem */
569 status = pjsua_media_subsys_init(media_cfg);
570 if (status != PJ_SUCCESS)
571 goto on_error;
572
Benny Prijonodc39fe82006-05-26 12:17:46 +0000573
574 /* Init core SIMPLE module : */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000575 status = pjsip_evsub_init_module(pjsua_var.endpt);
576 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000577
Benny Prijonodc39fe82006-05-26 12:17:46 +0000578
579 /* Init presence module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000580 status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance());
581 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000582
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000583 /* Init PUBLISH module */
584 pjsip_publishc_init_module(pjsua_var.endpt);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000585
586 /* Init xfer/REFER module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000587 status = pjsip_xfer_init_module( pjsua_var.endpt );
588 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000589
590 /* Init pjsua presence handler: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000591 status = pjsua_pres_init();
592 if (status != PJ_SUCCESS)
593 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000594
595 /* Init out-of-dialog MESSAGE request handler. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000596 status = pjsua_im_init();
597 if (status != PJ_SUCCESS)
598 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000599
Benny Prijonoc8141a82006-08-20 09:12:19 +0000600 /* Register OPTIONS handler */
601 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler);
602
603 /* Add OPTIONS in Allow header */
604 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
605 NULL, 1, &STR_OPTIONS);
606
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000607 /* Start worker thread if needed. */
608 if (pjsua_var.ua_cfg.thread_cnt) {
609 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000610
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000611 if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread))
612 pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000613
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000614 for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) {
615 status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread,
616 NULL, 0, 0, &pjsua_var.thread[i]);
617 if (status != PJ_SUCCESS)
618 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000619 }
Benny Prijonof521eb02006-08-06 23:07:25 +0000620 PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created",
621 pjsua_var.ua_cfg.thread_cnt));
622 } else {
623 PJ_LOG(4,(THIS_FILE, "No SIP worker threads created"));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000624 }
625
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000626 /* Done! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000627
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000628 PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized",
629 PJ_VERSION, PJ_OS_NAME));
Benny Prijonoccf95622006-02-07 18:48:01 +0000630
Benny Prijono268ca612006-02-07 12:34:11 +0000631 return PJ_SUCCESS;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000632
633on_error:
634 pjsua_destroy();
635 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000636}
637
638
Benny Prijono834aee32006-02-19 01:38:06 +0000639/* Sleep with polling */
640static void busy_sleep(unsigned msec)
641{
642 pj_time_val timeout, now;
643
644 pj_gettimeofday(&timeout);
645 timeout.msec += msec;
646 pj_time_val_normalize(&timeout);
647
648 do {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000649 while (pjsua_handle_events(10) > 0)
650 ;
Benny Prijono834aee32006-02-19 01:38:06 +0000651 pj_gettimeofday(&now);
652 } while (PJ_TIME_VAL_LT(now, timeout));
653}
654
Benny Prijonoebbf6892007-03-24 17:37:25 +0000655
656static pj_status_t resolve_stun_server(pj_bool_t use_dns_srv);
657
658/*
659 * Callback function to receive notification from the resolver
660 * when the resolution process completes.
661 */
662static void stun_dns_srv_resolver_cb(void *user_data,
663 pj_status_t status,
664 const pj_dns_srv_record *rec)
665{
666 unsigned i;
667
668 PJ_UNUSED_ARG(user_data);
669
670 pjsua_var.stun_status = status;
671
672 if (status != PJ_SUCCESS) {
673 /* DNS SRV resolution failed. If stun_host is specified, resolve
674 * it with gethostbyname()
675 */
676 if (pjsua_var.ua_cfg.stun_host.slen) {
677 pj_hostent he;
678
679 pjsua_var.stun_status = pj_gethostbyname(&pjsua_var.ua_cfg.stun_host, &he);
680
681 if (pjsua_var.stun_status == PJ_SUCCESS) {
682 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
683 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
684 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)3478);
685
686 PJ_LOG(3,(THIS_FILE,
687 "STUN server %.*s resolved, address is %s:%d",
688 (int)pjsua_var.ua_cfg.stun_host.slen,
689 pjsua_var.ua_cfg.stun_host.ptr,
690 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
691 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
692 }
693 } else {
694 char errmsg[PJ_ERR_MSG_SIZE];
695
696 pj_strerror(status, errmsg, sizeof(errmsg));
697 PJ_LOG(1,(THIS_FILE,
698 "DNS SRV resolution failed for STUN server %.*s: %s",
699 (int)pjsua_var.ua_cfg.stun_domain.slen,
700 pjsua_var.ua_cfg.stun_domain.ptr,
701 errmsg));
702 }
703 return;
704 }
705
706 pj_memcpy(&pjsua_var.stun_srv, &rec->entry[0].addr,
707 rec->entry[0].addr_len);
708
709 PJ_LOG(3,(THIS_FILE, "_stun._udp.%.*s resolved, found %d entry(s):",
710 (int)pjsua_var.ua_cfg.stun_domain.slen,
711 pjsua_var.ua_cfg.stun_domain.ptr,
712 rec->count));
713
714 for (i=0; i<rec->count; ++i) {
715 PJ_LOG(3,(THIS_FILE,
716 " %d: prio=%d, weight=%d %s:%d",
717 i, rec->entry[i].priority, rec->entry[i].weight,
718 pj_inet_ntoa(rec->entry[i].addr.ipv4.sin_addr),
719 (int)pj_ntohs(rec->entry[i].addr.ipv4.sin_port)));
720 }
721
722}
723
Benny Prijono268ca612006-02-07 12:34:11 +0000724/*
Benny Prijonoc97608e2007-03-23 16:34:20 +0000725 * Resolve STUN server.
726 */
727pj_status_t pjsua_resolve_stun_server(pj_bool_t wait)
728{
729 if (pjsua_var.stun_status == PJ_EUNKNOWN) {
730 /* Initialize STUN configuration */
731 pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0,
732 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
733 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
734
735 /* Start STUN server resolution */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000736
737 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000738
Benny Prijonoebbf6892007-03-24 17:37:25 +0000739 /* If stun_domain is specified, resolve STUN servers with DNS
740 * SRV resolution.
741 */
742 if (pjsua_var.ua_cfg.stun_domain.slen) {
743 pj_str_t res_type;
744
745 /* Fail if resolver is not configured */
746 if (pjsua_var.resolver == NULL) {
747 PJ_LOG(1,(THIS_FILE, "Nameserver must be configured when "
748 "stun_domain is specified"));
749 pjsua_var.stun_status = PJLIB_UTIL_EDNSNONS;
750 return PJLIB_UTIL_EDNSNONS;
751 }
752 res_type = pj_str("_stun._udp");
753 pjsua_var.stun_status =
754 pj_dns_srv_resolve(&pjsua_var.ua_cfg.stun_domain, &res_type,
755 3478, pjsua_var.pool, pjsua_var.resolver,
756 0, NULL, stun_dns_srv_resolver_cb);
757 if (pjsua_var.stun_status != PJ_SUCCESS) {
758 pjsua_perror(THIS_FILE, "Error starting DNS SRV resolution",
759 pjsua_var.stun_status);
760 return pjsua_var.stun_status;
761 }
762 }
763 /* Otherwise if stun_host is specified, resolve STUN server with
764 * gethostbyname().
765 */
766 else if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000767 pj_hostent he;
768
Benny Prijonoebbf6892007-03-24 17:37:25 +0000769 pjsua_var.stun_status = pj_gethostbyname(&pjsua_var.ua_cfg.stun_host, &he);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000770
771 if (pjsua_var.stun_status == PJ_SUCCESS) {
772 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
773 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
774 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)3478);
Benny Prijonof75eceb2007-03-23 19:09:54 +0000775
Benny Prijonoebbf6892007-03-24 17:37:25 +0000776 PJ_LOG(3,(THIS_FILE,
Benny Prijonof75eceb2007-03-23 19:09:54 +0000777 "STUN server %.*s resolved, address is %s:%d",
Benny Prijonoebbf6892007-03-24 17:37:25 +0000778 (int)pjsua_var.ua_cfg.stun_host.slen,
779 pjsua_var.ua_cfg.stun_host.ptr,
Benny Prijonof75eceb2007-03-23 19:09:54 +0000780 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
781 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
Benny Prijonoc97608e2007-03-23 16:34:20 +0000782 }
Benny Prijonoebbf6892007-03-24 17:37:25 +0000783
Benny Prijonoc97608e2007-03-23 16:34:20 +0000784 }
Benny Prijonoebbf6892007-03-24 17:37:25 +0000785 /* Otherwise disable STUN. */
786 else {
787 pjsua_var.stun_status = PJ_SUCCESS;
788 }
789
790
Benny Prijonoc97608e2007-03-23 16:34:20 +0000791 return pjsua_var.stun_status;
792
793 } else if (pjsua_var.stun_status == PJ_EPENDING) {
794 /* STUN server resolution has been started, wait for the
795 * result.
796 */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000797 if (wait) {
798 while (pjsua_var.stun_status == PJ_EPENDING)
799 pjsua_handle_events(10);
800 }
801
802 return pjsua_var.stun_status;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000803
804 } else {
805 /* STUN server has been resolved, return the status */
806 return pjsua_var.stun_status;
807 }
808}
809
810/*
Benny Prijono268ca612006-02-07 12:34:11 +0000811 * Destroy pjsua.
812 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000813PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +0000814{
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000815 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +0000816
817 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000818 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +0000819
820 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000821 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
822 if (pjsua_var.thread[i]) {
823 pj_thread_join(pjsua_var.thread[i]);
824 pj_thread_destroy(pjsua_var.thread[i]);
825 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +0000826 }
Benny Prijono268ca612006-02-07 12:34:11 +0000827 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000828
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000829 if (pjsua_var.endpt) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000830 /* Terminate all calls. */
831 pjsua_call_hangup_all();
832
833 /* Terminate all presence subscriptions. */
834 pjsua_pres_shutdown();
835
836 /* Unregister, if required: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000837 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
838 if (!pjsua_var.acc[i].valid)
839 continue;
840
841 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000842 pjsua_acc_set_registration(i, PJ_FALSE);
843 }
844 }
845
846 /* Wait for some time to allow unregistration to complete: */
Benny Prijono9fc735d2006-05-28 14:58:12 +0000847 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
848 busy_sleep(1000);
849 }
Benny Prijono834aee32006-02-19 01:38:06 +0000850
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000851 /* Destroy media */
852 pjsua_media_subsys_destroy();
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000853
Benny Prijono268ca612006-02-07 12:34:11 +0000854 /* Destroy endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000855 if (pjsua_var.endpt) {
856 pjsip_endpt_destroy(pjsua_var.endpt);
857 pjsua_var.endpt = NULL;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000858 }
Benny Prijono268ca612006-02-07 12:34:11 +0000859
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000860 /* Destroy mutex */
861 if (pjsua_var.mutex) {
862 pj_mutex_destroy(pjsua_var.mutex);
863 pjsua_var.mutex = NULL;
864 }
865
866 /* Destroy pool and pool factory. */
867 if (pjsua_var.pool) {
868 pj_pool_release(pjsua_var.pool);
869 pjsua_var.pool = NULL;
870 pj_caching_pool_destroy(&pjsua_var.cp);
871 }
Benny Prijono268ca612006-02-07 12:34:11 +0000872
873
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000874 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
875
876 /* End logging */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000877 if (pjsua_var.log_file) {
878 pj_file_close(pjsua_var.log_file);
879 pjsua_var.log_file = NULL;
880 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000881
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000882 /* Shutdown PJLIB */
883 pj_shutdown();
884
Benny Prijonof762ee72006-12-01 11:14:37 +0000885 /* Clear pjsua_var */
886 pj_bzero(&pjsua_var, sizeof(pjsua_var));
887
Benny Prijono268ca612006-02-07 12:34:11 +0000888 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000889 return PJ_SUCCESS;
890}
891
892
893/**
894 * Application is recommended to call this function after all initialization
895 * is done, so that the library can do additional checking set up
896 * additional
897 *
898 * @return PJ_SUCCESS on success, or the appropriate error code.
899 */
900PJ_DEF(pj_status_t) pjsua_start(void)
901{
902 pj_status_t status;
903
904 status = pjsua_call_subsys_start();
905 if (status != PJ_SUCCESS)
906 return status;
907
908 status = pjsua_media_subsys_start();
909 if (status != PJ_SUCCESS)
910 return status;
911
912 status = pjsua_pres_start();
913 if (status != PJ_SUCCESS)
914 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000915
916 return PJ_SUCCESS;
917}
918
Benny Prijono9fc735d2006-05-28 14:58:12 +0000919
920/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000921 * Poll pjsua for events, and if necessary block the caller thread for
922 * the specified maximum interval (in miliseconds).
923 */
924PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
925{
926 unsigned count = 0;
927 pj_time_val tv;
928 pj_status_t status;
929
930 tv.sec = 0;
931 tv.msec = msec_timeout;
932 pj_time_val_normalize(&tv);
933
934 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
935
936 if (status != PJ_SUCCESS)
937 return -status;
938
939 return count;
940}
941
942
943/*
944 * Create memory pool.
945 */
946PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
947 pj_size_t increment)
948{
949 /* Pool factory is thread safe, no need to lock */
950 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
951 NULL);
952}
953
954
955/*
956 * Internal function to get SIP endpoint instance of pjsua, which is
957 * needed for example to register module, create transports, etc.
958 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000959 */
960PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
961{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000962 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000963}
964
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000965/*
966 * Internal function to get media endpoint instance.
967 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000968 */
969PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
970{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000971 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000972}
973
Benny Prijono97b87172006-08-24 14:25:14 +0000974/*
975 * Internal function to get PJSUA pool factory.
976 */
977PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
978{
979 return &pjsua_var.cp.factory;
980}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000981
982/*****************************************************************************
983 * PJSUA SIP Transport API.
984 */
985
986/*
987 * Create and initialize SIP socket (and possibly resolve public
988 * address via STUN, depending on config).
989 */
990static pj_status_t create_sip_udp_sock(pj_in_addr bound_addr,
991 int port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000992 pj_sock_t *p_sock,
993 pj_sockaddr_in *p_pub_addr)
994{
Benny Prijonoc97608e2007-03-23 16:34:20 +0000995 char ip_addr[32];
996 pj_str_t stun_srv;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000997 pj_sock_t sock;
998 pj_status_t status;
999
Benny Prijonoc97608e2007-03-23 16:34:20 +00001000 /* Make sure STUN server resolution has completed */
1001 status = pjsua_resolve_stun_server(PJ_TRUE);
1002 if (status != PJ_SUCCESS) {
1003 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
1004 return status;
1005 }
1006
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001007 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock);
1008 if (status != PJ_SUCCESS) {
1009 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +00001010 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001011 }
1012
Benny Prijonof90ef4f2007-02-12 14:59:57 +00001013 status = pj_sock_bind_in(sock, pj_ntohl(bound_addr.s_addr),
1014 (pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001015 if (status != PJ_SUCCESS) {
1016 pjsua_perror(THIS_FILE, "bind() error", status);
1017 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001018 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001019 }
1020
Benny Prijonoe347cb02007-02-14 14:36:13 +00001021 /* If port is zero, get the bound port */
1022 if (port == 0) {
1023 pj_sockaddr_in bound_addr;
1024 int namelen = sizeof(bound_addr);
1025 status = pj_sock_getsockname(sock, &bound_addr, &namelen);
1026 if (status != PJ_SUCCESS) {
1027 pjsua_perror(THIS_FILE, "getsockname() error", status);
1028 pj_sock_close(sock);
1029 return status;
1030 }
1031
1032 port = pj_ntohs(bound_addr.sin_port);
1033 }
1034
Benny Prijonoc97608e2007-03-23 16:34:20 +00001035 if (pjsua_var.stun_srv.addr.sa_family != 0) {
1036 pj_ansi_strcpy(ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
1037 stun_srv = pj_str(ip_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001038 } else {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001039 stun_srv.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001040 }
1041
1042 /* Get the published address, either by STUN or by resolving
1043 * the name of local host.
1044 */
Benny Prijonoc97608e2007-03-23 16:34:20 +00001045 if (stun_srv.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +00001046 /*
1047 * STUN is specified, resolve the address with STUN.
1048 */
Benny Prijono14c2b862007-02-21 00:40:05 +00001049 status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
Benny Prijonoc97608e2007-03-23 16:34:20 +00001050 &stun_srv, 3478,
1051 &stun_srv, 3478,
1052 p_pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001053 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +00001054 pjsua_perror(THIS_FILE, "Error contacting STUN server", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001055 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001056 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001057 }
1058
Benny Prijono0a5cad82006-09-26 13:21:02 +00001059 } else if (p_pub_addr->sin_addr.s_addr != 0) {
1060 /*
1061 * Public address is already specified, no need to resolve the
1062 * address, only set the port.
1063 */
Benny Prijonoe347cb02007-02-14 14:36:13 +00001064 if (p_pub_addr->sin_port == 0)
1065 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001066
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001067 } else {
1068
Benny Prijono594e4c52006-09-14 18:51:01 +00001069 pj_bzero(p_pub_addr, sizeof(pj_sockaddr_in));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001070
Benny Prijono594e4c52006-09-14 18:51:01 +00001071 status = pj_gethostip(&p_pub_addr->sin_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001072 if (status != PJ_SUCCESS) {
Benny Prijonob43bad72007-01-20 05:11:08 +00001073 pjsua_perror(THIS_FILE, "Unable to get local host IP", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001074 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001075 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001076 }
1077
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001078 p_pub_addr->sin_family = PJ_AF_INET;
1079 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001080 }
1081
1082 *p_sock = sock;
1083
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001084 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
1085 pj_inet_ntoa(p_pub_addr->sin_addr),
1086 (int)pj_ntohs(p_pub_addr->sin_port)));
1087
Benny Prijonod8410532006-06-15 11:04:33 +00001088 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001089}
1090
1091
1092/*
1093 * Create SIP transport.
1094 */
1095PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
1096 const pjsua_transport_config *cfg,
1097 pjsua_transport_id *p_id)
1098{
1099 pjsip_transport *tp;
1100 unsigned id;
1101 pj_status_t status;
1102
1103 PJSUA_LOCK();
1104
1105 /* Find empty transport slot */
1106 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001107 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001108 break;
1109 }
1110
1111 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1112 status = PJ_ETOOMANY;
1113 pjsua_perror(THIS_FILE, "Error creating transport", status);
1114 goto on_return;
1115 }
1116
1117 /* Create the transport */
1118 if (type == PJSIP_TRANSPORT_UDP) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001119 /*
1120 * Create UDP transport.
1121 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001122 pjsua_transport_config config;
Benny Prijonod8410532006-06-15 11:04:33 +00001123 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001124 pj_sockaddr_in bound_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001125 pj_sockaddr_in pub_addr;
1126 pjsip_host_port addr_name;
1127
1128 /* Supply default config if it's not specified */
1129 if (cfg == NULL) {
1130 pjsua_transport_config_default(&config);
1131 cfg = &config;
1132 }
1133
Benny Prijono0a5cad82006-09-26 13:21:02 +00001134 /* Initialize bound address, if any */
1135 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
1136 if (cfg->bound_addr.slen) {
1137 status = pj_sockaddr_in_set_str_addr(&bound_addr,&cfg->bound_addr);
1138 if (status != PJ_SUCCESS) {
1139 pjsua_perror(THIS_FILE,
1140 "Unable to resolve transport bound address",
1141 status);
1142 goto on_return;
1143 }
1144 }
1145
1146 /* Initialize the public address from the config, if any */
1147 pj_sockaddr_in_init(&pub_addr, NULL, (pj_uint16_t)cfg->port);
1148 if (cfg->public_addr.slen) {
1149 status = pj_sockaddr_in_set_str_addr(&pub_addr, &cfg->public_addr);
1150 if (status != PJ_SUCCESS) {
1151 pjsua_perror(THIS_FILE,
1152 "Unable to resolve transport public address",
1153 status);
1154 goto on_return;
1155 }
1156 }
1157
1158 /* Create the socket and possibly resolve the address with STUN
1159 * (only when public address is not specified).
1160 */
1161 status = create_sip_udp_sock(bound_addr.sin_addr, cfg->port,
Benny Prijono0a5cad82006-09-26 13:21:02 +00001162 &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001163 if (status != PJ_SUCCESS)
1164 goto on_return;
1165
1166 addr_name.host = pj_str(pj_inet_ntoa(pub_addr.sin_addr));
1167 addr_name.port = pj_ntohs(pub_addr.sin_port);
1168
1169 /* Create UDP transport */
1170 status = pjsip_udp_transport_attach( pjsua_var.endpt, sock,
1171 &addr_name, 1,
1172 &tp);
1173 if (status != PJ_SUCCESS) {
1174 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
1175 status);
1176 pj_sock_close(sock);
1177 goto on_return;
1178 }
1179
Benny Prijonoe93e2872006-06-28 16:46:49 +00001180
1181 /* Save the transport */
1182 pjsua_var.tpdata[id].type = type;
1183 pjsua_var.tpdata[id].local_name = tp->local_name;
1184 pjsua_var.tpdata[id].data.tp = tp;
1185
1186 } else if (type == PJSIP_TRANSPORT_TCP) {
1187 /*
1188 * Create TCP transport.
1189 */
1190 pjsua_transport_config config;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001191 pjsip_host_port a_name;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001192 pjsip_tpfactory *tcp;
1193 pj_sockaddr_in local_addr;
1194
1195 /* Supply default config if it's not specified */
1196 if (cfg == NULL) {
1197 pjsua_transport_config_default(&config);
1198 cfg = &config;
1199 }
1200
1201 /* Init local address */
1202 pj_sockaddr_in_init(&local_addr, 0, 0);
1203
1204 if (cfg->port)
1205 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1206
Benny Prijono0a5cad82006-09-26 13:21:02 +00001207 if (cfg->bound_addr.slen) {
1208 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1209 if (status != PJ_SUCCESS) {
1210 pjsua_perror(THIS_FILE,
1211 "Unable to resolve transport bound address",
1212 status);
1213 goto on_return;
1214 }
1215 }
1216
1217 /* Init published name */
1218 pj_bzero(&a_name, sizeof(pjsip_host_port));
1219 if (cfg->public_addr.slen)
1220 a_name.host = cfg->public_addr;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001221
1222 /* Create the TCP transport */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001223 status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr,
1224 &a_name, 1, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001225
1226 if (status != PJ_SUCCESS) {
1227 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1228 status);
1229 goto on_return;
1230 }
1231
1232 /* Save the transport */
1233 pjsua_var.tpdata[id].type = type;
1234 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1235 pjsua_var.tpdata[id].data.factory = tcp;
1236
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001237#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1238 } else if (type == PJSIP_TRANSPORT_TLS) {
1239 /*
1240 * Create TLS transport.
1241 */
Benny Prijonof3bbc132006-12-25 06:43:59 +00001242 /*
1243 * Create TCP transport.
1244 */
1245 pjsua_transport_config config;
1246 pjsip_host_port a_name;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001247 pjsip_tpfactory *tls;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001248 pj_sockaddr_in local_addr;
1249
1250 /* Supply default config if it's not specified */
1251 if (cfg == NULL) {
1252 pjsua_transport_config_default(&config);
1253 config.port = 5061;
1254 cfg = &config;
1255 }
1256
1257 /* Init local address */
1258 pj_sockaddr_in_init(&local_addr, 0, 0);
1259
1260 if (cfg->port)
1261 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1262
1263 if (cfg->bound_addr.slen) {
1264 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1265 if (status != PJ_SUCCESS) {
1266 pjsua_perror(THIS_FILE,
1267 "Unable to resolve transport bound address",
1268 status);
1269 goto on_return;
1270 }
1271 }
1272
1273 /* Init published name */
1274 pj_bzero(&a_name, sizeof(pjsip_host_port));
1275 if (cfg->public_addr.slen)
1276 a_name.host = cfg->public_addr;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001277
1278 status = pjsip_tls_transport_start(pjsua_var.endpt,
Benny Prijonof3bbc132006-12-25 06:43:59 +00001279 &cfg->tls_setting,
1280 &local_addr, &a_name, 1, &tls);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001281 if (status != PJ_SUCCESS) {
1282 pjsua_perror(THIS_FILE, "Error creating SIP TLS listener",
1283 status);
1284 goto on_return;
1285 }
1286
1287 /* Save the transport */
1288 pjsua_var.tpdata[id].type = type;
1289 pjsua_var.tpdata[id].local_name = tls->addr_name;
1290 pjsua_var.tpdata[id].data.factory = tls;
1291#endif
1292
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001293 } else {
1294 status = PJSIP_EUNSUPTRANSPORT;
1295 pjsua_perror(THIS_FILE, "Error creating transport", status);
1296 goto on_return;
1297 }
1298
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001299
1300 /* Return the ID */
1301 if (p_id) *p_id = id;
1302
1303 status = PJ_SUCCESS;
1304
1305on_return:
1306
1307 PJSUA_UNLOCK();
1308
Benny Prijonod8410532006-06-15 11:04:33 +00001309 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001310}
1311
1312
1313/*
1314 * Register transport that has been created by application.
1315 */
1316PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1317 pjsua_transport_id *p_id)
1318{
1319 unsigned id;
1320
1321 PJSUA_LOCK();
1322
1323 /* Find empty transport slot */
1324 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001325 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001326 break;
1327 }
1328
1329 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1330 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1331 PJSUA_UNLOCK();
1332 return PJ_ETOOMANY;
1333 }
1334
1335 /* Save the transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001336 pjsua_var.tpdata[id].type = tp->key.type;
1337 pjsua_var.tpdata[id].local_name = tp->local_name;
1338 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001339
1340 /* Return the ID */
1341 if (p_id) *p_id = id;
1342
1343 PJSUA_UNLOCK();
1344
1345 return PJ_SUCCESS;
1346}
1347
1348
1349/*
1350 * Enumerate all transports currently created in the system.
1351 */
1352PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1353 unsigned *p_count )
1354{
1355 unsigned i, count;
1356
1357 PJSUA_LOCK();
1358
1359 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
1360 ++i)
1361 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001362 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001363 continue;
1364
1365 id[count++] = i;
1366 }
1367
1368 *p_count = count;
1369
1370 PJSUA_UNLOCK();
1371
1372 return PJ_SUCCESS;
1373}
1374
1375
1376/*
1377 * Get information about transports.
1378 */
1379PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
1380 pjsua_transport_info *info)
1381{
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001382 pjsua_transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00001383 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001384
Benny Prijonoac623b32006-07-03 15:19:31 +00001385 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001386
1387 /* Make sure id is in range. */
1388 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1389
1390 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001391 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001392
1393 PJSUA_LOCK();
1394
Benny Prijonoe93e2872006-06-28 16:46:49 +00001395 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
1396
1397 pjsip_transport *tp = t->data.tp;
1398
1399 if (tp == NULL) {
1400 PJSUA_UNLOCK();
1401 return PJ_EINVALIDOP;
1402 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001403
Benny Prijonoe93e2872006-06-28 16:46:49 +00001404 info->id = id;
1405 info->type = tp->key.type;
1406 info->type_name = pj_str(tp->type_name);
1407 info->info = pj_str(tp->info);
1408 info->flag = tp->flag;
1409 info->addr_len = tp->addr_len;
1410 info->local_addr = tp->local_addr;
1411 info->local_name = tp->local_name;
1412 info->usage_count = pj_atomic_get(tp->ref_cnt);
1413
Benny Prijono0a5cad82006-09-26 13:21:02 +00001414 status = PJ_SUCCESS;
1415
Benny Prijonoe93e2872006-06-28 16:46:49 +00001416 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
1417
1418 pjsip_tpfactory *factory = t->data.factory;
1419
1420 if (factory == NULL) {
1421 PJSUA_UNLOCK();
1422 return PJ_EINVALIDOP;
1423 }
1424
1425 info->id = id;
1426 info->type = t->type;
1427 info->type_name = pj_str("TCP");
1428 info->info = pj_str("TCP transport");
1429 info->flag = factory->flag;
1430 info->addr_len = sizeof(factory->local_addr);
1431 info->local_addr = factory->local_addr;
1432 info->local_name = factory->addr_name;
1433 info->usage_count = 0;
1434
Benny Prijono0a5cad82006-09-26 13:21:02 +00001435 status = PJ_SUCCESS;
1436
1437 } else {
1438 pj_assert(!"Unsupported transport");
1439 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001440 }
1441
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001442
1443 PJSUA_UNLOCK();
1444
Benny Prijono0a5cad82006-09-26 13:21:02 +00001445 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001446}
1447
1448
1449/*
1450 * Disable a transport or re-enable it.
1451 */
1452PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
1453 pj_bool_t enabled)
1454{
1455 /* Make sure id is in range. */
1456 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1457
1458 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001459 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001460
1461
1462 /* To be done!! */
1463 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001464 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001465
1466 return PJ_EINVALIDOP;
1467}
1468
1469
1470/*
1471 * Close the transport.
1472 */
1473PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
1474 pj_bool_t force )
1475{
Benny Prijono5ff61872007-02-01 03:37:11 +00001476 pj_status_t status;
1477
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001478 /* Make sure id is in range. */
1479 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1480
1481 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001482 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001483
Benny Prijono0a5cad82006-09-26 13:21:02 +00001484 /* Note: destroy() may not work if there are objects still referencing
1485 * the transport.
1486 */
1487 if (force) {
1488 switch (pjsua_var.tpdata[id].type) {
1489 case PJSIP_TRANSPORT_UDP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001490 status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
1491 if (status != PJ_SUCCESS)
1492 return status;
1493 status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
1494 if (status != PJ_SUCCESS)
1495 return status;
1496 break;
1497
1498 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001499 case PJSIP_TRANSPORT_TCP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001500 /* This will close the TCP listener, but existing TCP/TLS
1501 * connections (if any) will still linger
1502 */
1503 status = (*pjsua_var.tpdata[id].data.factory->destroy)
1504 (pjsua_var.tpdata[id].data.factory);
1505 if (status != PJ_SUCCESS)
1506 return status;
1507
Benny Prijono0a5cad82006-09-26 13:21:02 +00001508 break;
Benny Prijono5ff61872007-02-01 03:37:11 +00001509
Benny Prijono1ebd6142006-10-19 15:48:02 +00001510 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001511 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001512 }
1513
1514 } else {
Benny Prijono5ff61872007-02-01 03:37:11 +00001515 /* If force is not specified, transports will be closed at their
1516 * convenient time. However this will leak PJSUA-API transport
1517 * descriptors as PJSUA-API wouldn't know when exactly the
1518 * transport is closed thus it can't cleanup PJSUA transport
1519 * descriptor.
1520 */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001521 switch (pjsua_var.tpdata[id].type) {
1522 case PJSIP_TRANSPORT_UDP:
1523 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
Benny Prijono5ff61872007-02-01 03:37:11 +00001524 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001525 case PJSIP_TRANSPORT_TCP:
1526 return (*pjsua_var.tpdata[id].data.factory->destroy)
1527 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001528 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001529 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001530 }
1531 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001532
Benny Prijono5ff61872007-02-01 03:37:11 +00001533 /* Cleanup pjsua data when force is applied */
1534 if (force) {
1535 pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED;
1536 pjsua_var.tpdata[id].data.ptr = NULL;
1537 }
1538
1539 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001540}
1541
1542
1543/*
1544 * Add additional headers etc in msg_data specified by application
1545 * when sending requests.
1546 */
1547void pjsua_process_msg_data(pjsip_tx_data *tdata,
1548 const pjsua_msg_data *msg_data)
1549{
1550 pj_bool_t allow_body;
1551 const pjsip_hdr *hdr;
1552
Benny Prijono56315612006-07-18 14:39:40 +00001553 /* Always add User-Agent */
1554 if (pjsua_var.ua_cfg.user_agent.slen &&
1555 tdata->msg->type == PJSIP_REQUEST_MSG)
1556 {
1557 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1558 pjsip_hdr *h;
1559 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
1560 &STR_USER_AGENT,
1561 &pjsua_var.ua_cfg.user_agent);
1562 pjsip_msg_add_hdr(tdata->msg, h);
1563 }
1564
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001565 if (!msg_data)
1566 return;
1567
1568 hdr = msg_data->hdr_list.next;
1569 while (hdr && hdr != &msg_data->hdr_list) {
1570 pjsip_hdr *new_hdr;
1571
1572 new_hdr = pjsip_hdr_clone(tdata->pool, hdr);
1573 pjsip_msg_add_hdr(tdata->msg, new_hdr);
1574
1575 hdr = hdr->next;
1576 }
1577
1578 allow_body = (tdata->msg->body == NULL);
1579
1580 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
1581 pjsip_media_type ctype;
1582 pjsip_msg_body *body;
1583
1584 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
1585 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
1586 &msg_data->msg_body);
1587 tdata->msg->body = body;
1588 }
1589}
1590
1591
1592/*
1593 * Add route_set to outgoing requests
1594 */
1595void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
1596 const pjsip_route_hdr *route_set )
1597{
1598 const pjsip_route_hdr *r;
1599
1600 r = route_set->next;
1601 while (r != route_set) {
1602 pjsip_route_hdr *new_r;
1603
1604 new_r = pjsip_hdr_clone(tdata->pool, r);
1605 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
1606
1607 r = r->next;
1608 }
1609}
1610
1611
1612/*
1613 * Simple version of MIME type parsing (it doesn't support parameters)
1614 */
1615void pjsua_parse_media_type( pj_pool_t *pool,
1616 const pj_str_t *mime,
1617 pjsip_media_type *media_type)
1618{
1619 pj_str_t tmp;
1620 char *pos;
1621
Benny Prijonoac623b32006-07-03 15:19:31 +00001622 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001623
1624 pj_strdup_with_null(pool, &tmp, mime);
1625
1626 pos = pj_strchr(&tmp, '/');
1627 if (pos) {
1628 media_type->type.ptr = tmp.ptr;
1629 media_type->type.slen = (pos-tmp.ptr);
1630 media_type->subtype.ptr = pos+1;
1631 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
1632 } else {
1633 media_type->type = tmp;
1634 }
1635}
1636
1637
1638/*
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001639 * Internal function to init transport selector from transport id.
1640 */
1641void pjsua_init_tpselector(pjsua_transport_id tp_id,
1642 pjsip_tpselector *sel)
1643{
1644 pjsua_transport_data *tpdata;
1645 unsigned flag;
1646
1647 pj_bzero(sel, sizeof(*sel));
1648 if (tp_id == PJSUA_INVALID_ID)
1649 return;
1650
1651 pj_assert(tp_id >= 0 && tp_id < PJ_ARRAY_SIZE(pjsua_var.tpdata));
1652 tpdata = &pjsua_var.tpdata[tp_id];
1653
1654 flag = pjsip_transport_get_flag_from_type(tpdata->type);
1655
1656 if (flag & PJSIP_TRANSPORT_DATAGRAM) {
1657 sel->type = PJSIP_TPSELECTOR_TRANSPORT;
1658 sel->u.transport = tpdata->data.tp;
1659 } else {
1660 sel->type = PJSIP_TPSELECTOR_LISTENER;
1661 sel->u.listener = tpdata->data.factory;
1662 }
1663}
1664
1665
1666/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001667 * Verify that valid SIP url is given.
1668 */
1669PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
1670{
1671 pjsip_uri *p;
1672 pj_pool_t *pool;
1673 char *url;
1674 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
1675
1676 if (!len) return -1;
1677
1678 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
1679 if (!pool) return -1;
1680
1681 url = pj_pool_alloc(pool, len+1);
1682 pj_ansi_strcpy(url, c_url);
1683
1684 p = pjsip_parse_uri(pool, url, len, 0);
1685 if (!p || pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0)
1686 p = NULL;
1687
1688 pj_pool_release(pool);
1689 return p ? 0 : -1;
1690}