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