blob: 000d469fad172bfe749fd33687d7fd520ab961de [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 Prijonoeebe9af2006-06-13 22:57:13 +0000288 /* Write to stdout, file, and application callback. */
289
290 if (level <= (int)pjsua_var.log_cfg.console_level)
291 pj_log_write(level, buffer, len);
292
293 if (pjsua_var.log_file) {
294 pj_ssize_t size = len;
295 pj_file_write(pjsua_var.log_file, buffer, &size);
296 }
297
298 if (pjsua_var.log_cfg.cb)
299 (*pjsua_var.log_cfg.cb)(level, buffer, len);
300}
301
302
303/*
304 * Application can call this function at any time (after pjsua_create(), of
305 * course) to change logging settings.
306 */
307PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg)
308{
309 pj_status_t status;
310
311 /* Save config. */
312 pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg);
313
314 /* Redirect log function to ours */
315 pj_log_set_log_func( &log_writer );
316
317 /* Close existing file, if any */
318 if (pjsua_var.log_file) {
319 pj_file_close(pjsua_var.log_file);
320 pjsua_var.log_file = NULL;
321 }
322
323 /* If output log file is desired, create the file: */
324 if (pjsua_var.log_cfg.log_filename.slen) {
325
326 status = pj_file_open(pjsua_var.pool,
327 pjsua_var.log_cfg.log_filename.ptr,
328 PJ_O_WRONLY,
329 &pjsua_var.log_file);
330
331 if (status != PJ_SUCCESS) {
332 pjsua_perror(THIS_FILE, "Error creating log file", status);
333 return status;
334 }
335 }
336
337 /* Unregister msg logging if it's previously registered */
338 if (pjsua_msg_logger.id >= 0) {
339 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
340 pjsua_msg_logger.id = -1;
341 }
342
343 /* Enable SIP message logging */
344 if (pjsua_var.log_cfg.msg_logging)
345 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
346
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000347 return PJ_SUCCESS;
348}
349
350
351/*****************************************************************************
352 * PJSUA Base API.
353 */
354
355/* Worker thread function. */
356static int worker_thread(void *arg)
357{
358 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000359
Benny Prijono268ca612006-02-07 12:34:11 +0000360 PJ_UNUSED_ARG(arg);
361
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000362 while (!pjsua_var.thread_quit_flag) {
363 int count;
364
365 count = pjsua_handle_events(TIMEOUT);
366 if (count < 0)
367 pj_thread_sleep(TIMEOUT);
368 }
Benny Prijono268ca612006-02-07 12:34:11 +0000369
370 return 0;
371}
372
Benny Prijonodc39fe82006-05-26 12:17:46 +0000373
Benny Prijono268ca612006-02-07 12:34:11 +0000374/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000375 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000376 */
377PJ_DEF(pj_status_t) pjsua_create(void)
378{
379 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000380
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000381 /* Init pjsua data */
382 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000383
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000384 /* Set default logging settings */
385 pjsua_logging_config_default(&pjsua_var.log_cfg);
386
387 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000388 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000389 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
390
Benny Prijono268ca612006-02-07 12:34:11 +0000391
Benny Prijonofccab712006-02-22 22:23:22 +0000392 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000393 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000394 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000395
Benny Prijono268ca612006-02-07 12:34:11 +0000396
Benny Prijono094d3ad2006-11-21 08:41:00 +0000397 /* Set default sound device ID */
398 pjsua_var.cap_dev = pjsua_var.play_dev = -1;
399
Benny Prijono268ca612006-02-07 12:34:11 +0000400 /* Init caching pool. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000401 pj_caching_pool_init(&pjsua_var.cp, &pj_pool_factory_default_policy, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000402
403 /* Create memory pool for application. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000404 pjsua_var.pool = pjsua_pool_create("pjsua", 4000, 4000);
405
406 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000407
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000408 /* Create mutex */
409 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
410 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000411 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000412 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000413 return status;
414 }
415
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000416 /* Must create SIP endpoint to initialize SIP parser. The parser
417 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000418 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000419 status = pjsip_endpt_create(&pjsua_var.cp.factory,
420 pj_gethostname()->ptr,
421 &pjsua_var.endpt);
422 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000423
424
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000425 return PJ_SUCCESS;
426}
427
428
429/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000430 * Initialize pjsua with the specified settings. All the settings are
431 * optional, and the default values will be used when the config is not
432 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000433 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000434PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
435 const pjsua_logging_config *log_cfg,
436 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000437{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000438 pjsua_config default_cfg;
439 pjsua_media_config default_media_cfg;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000440 const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
Benny Prijonodc39fe82006-05-26 12:17:46 +0000441 pj_status_t status;
442
443
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000444 /* Create default configurations when the config is not supplied */
445
446 if (ua_cfg == NULL) {
447 pjsua_config_default(&default_cfg);
448 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000449 }
450
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000451 if (media_cfg == NULL) {
452 pjsua_media_config_default(&default_media_cfg);
453 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000454 }
455
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000456 /* Initialize logging first so that info/errors can be captured */
457 if (log_cfg) {
458 status = pjsua_reconfigure_logging(log_cfg);
459 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono9fc735d2006-05-28 14:58:12 +0000460 }
461
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000462 /* If nameserver is configured, create DNS resolver instance and
463 * set it to be used by SIP resolver.
464 */
465 if (ua_cfg->nameserver_count) {
466#if PJSIP_HAS_RESOLVER
467 pj_dns_resolver *resv;
468 unsigned i;
469
470 /* Create DNS resolver */
471 status = pjsip_endpt_create_resolver(pjsua_var.endpt, &resv);
472 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
473
474 /* Configure nameserver for the DNS resolver */
475 status = pj_dns_resolver_set_ns(resv, ua_cfg->nameserver_count,
476 ua_cfg->nameserver, NULL);
477 if (status != PJ_SUCCESS) {
478 pjsua_perror(THIS_FILE, "Error setting nameserver", status);
479 return status;
480 }
481
482 /* Set this DNS resolver to be used by the SIP resolver */
483 status = pjsip_endpt_set_resolver(pjsua_var.endpt, resv);
484 if (status != PJ_SUCCESS) {
485 pjsua_perror(THIS_FILE, "Error setting DNS resolver", status);
486 return status;
487 }
488
489 /* Print nameservers */
490 for (i=0; i<ua_cfg->nameserver_count; ++i) {
491 PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added",
492 (int)ua_cfg->nameserver[i].slen,
493 ua_cfg->nameserver[i].ptr));
494 }
495#else
496 PJ_LOG(2,(THIS_FILE,
497 "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)"));
498#endif
499 }
500
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000501 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000502
503 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000504 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
505 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000506
Benny Prijonodc39fe82006-05-26 12:17:46 +0000507
508 /* Initialize UA layer module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000509 status = pjsip_ua_init_module( pjsua_var.endpt, NULL );
510 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000511
Benny Prijonodc39fe82006-05-26 12:17:46 +0000512
Benny Prijono053f5222006-11-11 16:16:04 +0000513 /* Initialize Replaces support. */
514 status = pjsip_replaces_init_module( pjsua_var.endpt );
515 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
516
517
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000518 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000519 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000520 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000521 {
522 NULL, NULL, /* prev, next. */
523 { "mod-pjsua", 9 }, /* Name. */
524 -1, /* Id */
525 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
526 NULL, /* load() */
527 NULL, /* start() */
528 NULL, /* stop() */
529 NULL, /* unload() */
530 &mod_pjsua_on_rx_request, /* on_rx_request() */
531 &mod_pjsua_on_rx_response, /* on_rx_response() */
532 NULL, /* on_tx_request. */
533 NULL, /* on_tx_response() */
534 NULL, /* on_tsx_state() */
535 };
536
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000537 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000538
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000539 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
540 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000541 }
542
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000543
Benny Prijonodc39fe82006-05-26 12:17:46 +0000544
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000545 /* Initialize PJSUA call subsystem: */
546 status = pjsua_call_subsys_init(ua_cfg);
547 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000548 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000549
550
551 /* Initialize PJSUA media subsystem */
552 status = pjsua_media_subsys_init(media_cfg);
553 if (status != PJ_SUCCESS)
554 goto on_error;
555
Benny Prijonodc39fe82006-05-26 12:17:46 +0000556
557 /* Init core SIMPLE module : */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000558 status = pjsip_evsub_init_module(pjsua_var.endpt);
559 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000560
Benny Prijonodc39fe82006-05-26 12:17:46 +0000561
562 /* Init presence module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000563 status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance());
564 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000565
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000566 /* Init PUBLISH module */
567 pjsip_publishc_init_module(pjsua_var.endpt);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000568
569 /* Init xfer/REFER module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000570 status = pjsip_xfer_init_module( pjsua_var.endpt );
571 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000572
573 /* Init pjsua presence handler: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000574 status = pjsua_pres_init();
575 if (status != PJ_SUCCESS)
576 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000577
578 /* Init out-of-dialog MESSAGE request handler. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000579 status = pjsua_im_init();
580 if (status != PJ_SUCCESS)
581 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000582
Benny Prijonoc8141a82006-08-20 09:12:19 +0000583 /* Register OPTIONS handler */
584 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler);
585
586 /* Add OPTIONS in Allow header */
587 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
588 NULL, 1, &STR_OPTIONS);
589
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000590 /* Start worker thread if needed. */
591 if (pjsua_var.ua_cfg.thread_cnt) {
592 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000593
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000594 if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread))
595 pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000596
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000597 for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) {
598 status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread,
599 NULL, 0, 0, &pjsua_var.thread[i]);
600 if (status != PJ_SUCCESS)
601 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000602 }
Benny Prijonof521eb02006-08-06 23:07:25 +0000603 PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created",
604 pjsua_var.ua_cfg.thread_cnt));
605 } else {
606 PJ_LOG(4,(THIS_FILE, "No SIP worker threads created"));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000607 }
608
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000609 /* Done! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000610
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000611 PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized",
612 PJ_VERSION, PJ_OS_NAME));
Benny Prijonoccf95622006-02-07 18:48:01 +0000613
Benny Prijono268ca612006-02-07 12:34:11 +0000614 return PJ_SUCCESS;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000615
616on_error:
617 pjsua_destroy();
618 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000619}
620
621
Benny Prijono834aee32006-02-19 01:38:06 +0000622/* Sleep with polling */
623static void busy_sleep(unsigned msec)
624{
625 pj_time_val timeout, now;
626
627 pj_gettimeofday(&timeout);
628 timeout.msec += msec;
629 pj_time_val_normalize(&timeout);
630
631 do {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000632 while (pjsua_handle_events(10) > 0)
633 ;
Benny Prijono834aee32006-02-19 01:38:06 +0000634 pj_gettimeofday(&now);
635 } while (PJ_TIME_VAL_LT(now, timeout));
636}
637
Benny Prijono268ca612006-02-07 12:34:11 +0000638/*
639 * Destroy pjsua.
640 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000641PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +0000642{
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000643 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +0000644
645 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000646 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +0000647
648 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000649 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
650 if (pjsua_var.thread[i]) {
651 pj_thread_join(pjsua_var.thread[i]);
652 pj_thread_destroy(pjsua_var.thread[i]);
653 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +0000654 }
Benny Prijono268ca612006-02-07 12:34:11 +0000655 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000656
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000657 if (pjsua_var.endpt) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000658 /* Terminate all calls. */
659 pjsua_call_hangup_all();
660
661 /* Terminate all presence subscriptions. */
662 pjsua_pres_shutdown();
663
664 /* Unregister, if required: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000665 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
666 if (!pjsua_var.acc[i].valid)
667 continue;
668
669 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000670 pjsua_acc_set_registration(i, PJ_FALSE);
671 }
672 }
673
674 /* Wait for some time to allow unregistration to complete: */
Benny Prijono9fc735d2006-05-28 14:58:12 +0000675 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
676 busy_sleep(1000);
677 }
Benny Prijono834aee32006-02-19 01:38:06 +0000678
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000679 /* Destroy media */
680 pjsua_media_subsys_destroy();
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000681
Benny Prijono268ca612006-02-07 12:34:11 +0000682 /* Destroy endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000683 if (pjsua_var.endpt) {
684 pjsip_endpt_destroy(pjsua_var.endpt);
685 pjsua_var.endpt = NULL;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000686 }
Benny Prijono268ca612006-02-07 12:34:11 +0000687
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000688 /* Destroy mutex */
689 if (pjsua_var.mutex) {
690 pj_mutex_destroy(pjsua_var.mutex);
691 pjsua_var.mutex = NULL;
692 }
693
694 /* Destroy pool and pool factory. */
695 if (pjsua_var.pool) {
696 pj_pool_release(pjsua_var.pool);
697 pjsua_var.pool = NULL;
698 pj_caching_pool_destroy(&pjsua_var.cp);
699 }
Benny Prijono268ca612006-02-07 12:34:11 +0000700
701
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000702 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
703
704 /* End logging */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000705 if (pjsua_var.log_file) {
706 pj_file_close(pjsua_var.log_file);
707 pjsua_var.log_file = NULL;
708 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000709
Benny Prijono268ca612006-02-07 12:34:11 +0000710 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000711 return PJ_SUCCESS;
712}
713
714
715/**
716 * Application is recommended to call this function after all initialization
717 * is done, so that the library can do additional checking set up
718 * additional
719 *
720 * @return PJ_SUCCESS on success, or the appropriate error code.
721 */
722PJ_DEF(pj_status_t) pjsua_start(void)
723{
724 pj_status_t status;
725
726 status = pjsua_call_subsys_start();
727 if (status != PJ_SUCCESS)
728 return status;
729
730 status = pjsua_media_subsys_start();
731 if (status != PJ_SUCCESS)
732 return status;
733
734 status = pjsua_pres_start();
735 if (status != PJ_SUCCESS)
736 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000737
738 return PJ_SUCCESS;
739}
740
Benny Prijono9fc735d2006-05-28 14:58:12 +0000741
742/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000743 * Poll pjsua for events, and if necessary block the caller thread for
744 * the specified maximum interval (in miliseconds).
745 */
746PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
747{
748 unsigned count = 0;
749 pj_time_val tv;
750 pj_status_t status;
751
752 tv.sec = 0;
753 tv.msec = msec_timeout;
754 pj_time_val_normalize(&tv);
755
756 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
757
758 if (status != PJ_SUCCESS)
759 return -status;
760
761 return count;
762}
763
764
765/*
766 * Create memory pool.
767 */
768PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
769 pj_size_t increment)
770{
771 /* Pool factory is thread safe, no need to lock */
772 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
773 NULL);
774}
775
776
777/*
778 * Internal function to get SIP endpoint instance of pjsua, which is
779 * needed for example to register module, create transports, etc.
780 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000781 */
782PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
783{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000784 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000785}
786
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000787/*
788 * Internal function to get media endpoint instance.
789 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000790 */
791PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
792{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000793 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000794}
795
Benny Prijono97b87172006-08-24 14:25:14 +0000796/*
797 * Internal function to get PJSUA pool factory.
798 */
799PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
800{
801 return &pjsua_var.cp.factory;
802}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000803
804/*****************************************************************************
805 * PJSUA SIP Transport API.
806 */
807
808/*
809 * Create and initialize SIP socket (and possibly resolve public
810 * address via STUN, depending on config).
811 */
812static pj_status_t create_sip_udp_sock(pj_in_addr bound_addr,
813 int port,
814 pj_bool_t use_stun,
815 const pjsua_stun_config *stun_param,
816 pj_sock_t *p_sock,
817 pj_sockaddr_in *p_pub_addr)
818{
819 pjsua_stun_config stun;
820 pj_sock_t sock;
821 pj_status_t status;
822
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000823 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock);
824 if (status != PJ_SUCCESS) {
825 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +0000826 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000827 }
828
829 status = pj_sock_bind_in(sock, bound_addr.s_addr, (pj_uint16_t)port);
830 if (status != PJ_SUCCESS) {
831 pjsua_perror(THIS_FILE, "bind() error", status);
832 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +0000833 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000834 }
835
836 /* Copy and normalize STUN param */
837 if (use_stun) {
838 pj_memcpy(&stun, stun_param, sizeof(*stun_param));
839 pjsua_normalize_stun_config(&stun);
840 } else {
Benny Prijonoac623b32006-07-03 15:19:31 +0000841 pj_bzero(&stun, sizeof(pjsua_stun_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000842 }
843
844 /* Get the published address, either by STUN or by resolving
845 * the name of local host.
846 */
847 if (stun.stun_srv1.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +0000848 /*
849 * STUN is specified, resolve the address with STUN.
850 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000851 status = pj_stun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
852 &stun.stun_srv1,
853 stun.stun_port1,
854 &stun.stun_srv2,
855 stun.stun_port2,
856 p_pub_addr);
857 if (status != PJ_SUCCESS) {
858 pjsua_perror(THIS_FILE, "Error resolving with STUN", status);
859 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +0000860 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000861 }
862
Benny Prijono0a5cad82006-09-26 13:21:02 +0000863 } else if (p_pub_addr->sin_addr.s_addr != 0) {
864 /*
865 * Public address is already specified, no need to resolve the
866 * address, only set the port.
867 */
868 /* Do nothing */
869
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000870 } else {
871
Benny Prijono594e4c52006-09-14 18:51:01 +0000872 pj_bzero(p_pub_addr, sizeof(pj_sockaddr_in));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000873
Benny Prijono594e4c52006-09-14 18:51:01 +0000874 status = pj_gethostip(&p_pub_addr->sin_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000875 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000876 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +0000877 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000878 }
879
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000880 p_pub_addr->sin_family = PJ_AF_INET;
881 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000882 }
883
884 *p_sock = sock;
885
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000886 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
887 pj_inet_ntoa(p_pub_addr->sin_addr),
888 (int)pj_ntohs(p_pub_addr->sin_port)));
889
Benny Prijonod8410532006-06-15 11:04:33 +0000890 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000891}
892
893
894/*
895 * Create SIP transport.
896 */
897PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
898 const pjsua_transport_config *cfg,
899 pjsua_transport_id *p_id)
900{
901 pjsip_transport *tp;
902 unsigned id;
903 pj_status_t status;
904
905 PJSUA_LOCK();
906
907 /* Find empty transport slot */
908 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +0000909 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000910 break;
911 }
912
913 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
914 status = PJ_ETOOMANY;
915 pjsua_perror(THIS_FILE, "Error creating transport", status);
916 goto on_return;
917 }
918
919 /* Create the transport */
920 if (type == PJSIP_TRANSPORT_UDP) {
Benny Prijonoe93e2872006-06-28 16:46:49 +0000921 /*
922 * Create UDP transport.
923 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000924 pjsua_transport_config config;
Benny Prijonod8410532006-06-15 11:04:33 +0000925 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono0a5cad82006-09-26 13:21:02 +0000926 pj_sockaddr_in bound_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000927 pj_sockaddr_in pub_addr;
928 pjsip_host_port addr_name;
929
930 /* Supply default config if it's not specified */
931 if (cfg == NULL) {
932 pjsua_transport_config_default(&config);
933 cfg = &config;
934 }
935
Benny Prijono0a5cad82006-09-26 13:21:02 +0000936 /* Initialize bound address, if any */
937 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
938 if (cfg->bound_addr.slen) {
939 status = pj_sockaddr_in_set_str_addr(&bound_addr,&cfg->bound_addr);
940 if (status != PJ_SUCCESS) {
941 pjsua_perror(THIS_FILE,
942 "Unable to resolve transport bound address",
943 status);
944 goto on_return;
945 }
946 }
947
948 /* Initialize the public address from the config, if any */
949 pj_sockaddr_in_init(&pub_addr, NULL, (pj_uint16_t)cfg->port);
950 if (cfg->public_addr.slen) {
951 status = pj_sockaddr_in_set_str_addr(&pub_addr, &cfg->public_addr);
952 if (status != PJ_SUCCESS) {
953 pjsua_perror(THIS_FILE,
954 "Unable to resolve transport public address",
955 status);
956 goto on_return;
957 }
958 }
959
960 /* Create the socket and possibly resolve the address with STUN
961 * (only when public address is not specified).
962 */
963 status = create_sip_udp_sock(bound_addr.sin_addr, cfg->port,
964 cfg->use_stun, &cfg->stun_config,
965 &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000966 if (status != PJ_SUCCESS)
967 goto on_return;
968
969 addr_name.host = pj_str(pj_inet_ntoa(pub_addr.sin_addr));
970 addr_name.port = pj_ntohs(pub_addr.sin_port);
971
972 /* Create UDP transport */
973 status = pjsip_udp_transport_attach( pjsua_var.endpt, sock,
974 &addr_name, 1,
975 &tp);
976 if (status != PJ_SUCCESS) {
977 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
978 status);
979 pj_sock_close(sock);
980 goto on_return;
981 }
982
Benny Prijonoe93e2872006-06-28 16:46:49 +0000983
984 /* Save the transport */
985 pjsua_var.tpdata[id].type = type;
986 pjsua_var.tpdata[id].local_name = tp->local_name;
987 pjsua_var.tpdata[id].data.tp = tp;
988
989 } else if (type == PJSIP_TRANSPORT_TCP) {
990 /*
991 * Create TCP transport.
992 */
993 pjsua_transport_config config;
Benny Prijono0a5cad82006-09-26 13:21:02 +0000994 pjsip_host_port a_name;
Benny Prijonoe93e2872006-06-28 16:46:49 +0000995 pjsip_tpfactory *tcp;
996 pj_sockaddr_in local_addr;
997
998 /* Supply default config if it's not specified */
999 if (cfg == NULL) {
1000 pjsua_transport_config_default(&config);
1001 cfg = &config;
1002 }
1003
1004 /* Init local address */
1005 pj_sockaddr_in_init(&local_addr, 0, 0);
1006
1007 if (cfg->port)
1008 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1009
Benny Prijono0a5cad82006-09-26 13:21:02 +00001010 if (cfg->bound_addr.slen) {
1011 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1012 if (status != PJ_SUCCESS) {
1013 pjsua_perror(THIS_FILE,
1014 "Unable to resolve transport bound address",
1015 status);
1016 goto on_return;
1017 }
1018 }
1019
1020 /* Init published name */
1021 pj_bzero(&a_name, sizeof(pjsip_host_port));
1022 if (cfg->public_addr.slen)
1023 a_name.host = cfg->public_addr;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001024
1025 /* Create the TCP transport */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001026 status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr,
1027 &a_name, 1, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001028
1029 if (status != PJ_SUCCESS) {
1030 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1031 status);
1032 goto on_return;
1033 }
1034
1035 /* Save the transport */
1036 pjsua_var.tpdata[id].type = type;
1037 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1038 pjsua_var.tpdata[id].data.factory = tcp;
1039
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001040 } else {
1041 status = PJSIP_EUNSUPTRANSPORT;
1042 pjsua_perror(THIS_FILE, "Error creating transport", status);
1043 goto on_return;
1044 }
1045
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001046
1047 /* Return the ID */
1048 if (p_id) *p_id = id;
1049
1050 status = PJ_SUCCESS;
1051
1052on_return:
1053
1054 PJSUA_UNLOCK();
1055
Benny Prijonod8410532006-06-15 11:04:33 +00001056 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001057}
1058
1059
1060/*
1061 * Register transport that has been created by application.
1062 */
1063PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1064 pjsua_transport_id *p_id)
1065{
1066 unsigned id;
1067
1068 PJSUA_LOCK();
1069
1070 /* Find empty transport slot */
1071 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001072 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001073 break;
1074 }
1075
1076 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1077 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1078 PJSUA_UNLOCK();
1079 return PJ_ETOOMANY;
1080 }
1081
1082 /* Save the transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001083 pjsua_var.tpdata[id].type = tp->key.type;
1084 pjsua_var.tpdata[id].local_name = tp->local_name;
1085 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001086
1087 /* Return the ID */
1088 if (p_id) *p_id = id;
1089
1090 PJSUA_UNLOCK();
1091
1092 return PJ_SUCCESS;
1093}
1094
1095
1096/*
1097 * Enumerate all transports currently created in the system.
1098 */
1099PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1100 unsigned *p_count )
1101{
1102 unsigned i, count;
1103
1104 PJSUA_LOCK();
1105
1106 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
1107 ++i)
1108 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001109 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001110 continue;
1111
1112 id[count++] = i;
1113 }
1114
1115 *p_count = count;
1116
1117 PJSUA_UNLOCK();
1118
1119 return PJ_SUCCESS;
1120}
1121
1122
1123/*
1124 * Get information about transports.
1125 */
1126PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
1127 pjsua_transport_info *info)
1128{
Benny Prijonoe93e2872006-06-28 16:46:49 +00001129 struct transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00001130 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001131
Benny Prijonoac623b32006-07-03 15:19:31 +00001132 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001133
1134 /* Make sure id is in range. */
1135 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1136
1137 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001138 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001139
1140 PJSUA_LOCK();
1141
Benny Prijonoe93e2872006-06-28 16:46:49 +00001142 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
1143
1144 pjsip_transport *tp = t->data.tp;
1145
1146 if (tp == NULL) {
1147 PJSUA_UNLOCK();
1148 return PJ_EINVALIDOP;
1149 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001150
Benny Prijonoe93e2872006-06-28 16:46:49 +00001151 info->id = id;
1152 info->type = tp->key.type;
1153 info->type_name = pj_str(tp->type_name);
1154 info->info = pj_str(tp->info);
1155 info->flag = tp->flag;
1156 info->addr_len = tp->addr_len;
1157 info->local_addr = tp->local_addr;
1158 info->local_name = tp->local_name;
1159 info->usage_count = pj_atomic_get(tp->ref_cnt);
1160
Benny Prijono0a5cad82006-09-26 13:21:02 +00001161 status = PJ_SUCCESS;
1162
Benny Prijonoe93e2872006-06-28 16:46:49 +00001163 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
1164
1165 pjsip_tpfactory *factory = t->data.factory;
1166
1167 if (factory == NULL) {
1168 PJSUA_UNLOCK();
1169 return PJ_EINVALIDOP;
1170 }
1171
1172 info->id = id;
1173 info->type = t->type;
1174 info->type_name = pj_str("TCP");
1175 info->info = pj_str("TCP transport");
1176 info->flag = factory->flag;
1177 info->addr_len = sizeof(factory->local_addr);
1178 info->local_addr = factory->local_addr;
1179 info->local_name = factory->addr_name;
1180 info->usage_count = 0;
1181
Benny Prijono0a5cad82006-09-26 13:21:02 +00001182 status = PJ_SUCCESS;
1183
1184 } else {
1185 pj_assert(!"Unsupported transport");
1186 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001187 }
1188
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001189
1190 PJSUA_UNLOCK();
1191
Benny Prijono0a5cad82006-09-26 13:21:02 +00001192 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001193}
1194
1195
1196/*
1197 * Disable a transport or re-enable it.
1198 */
1199PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
1200 pj_bool_t enabled)
1201{
1202 /* Make sure id is in range. */
1203 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1204
1205 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001206 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001207
1208
1209 /* To be done!! */
1210 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001211 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001212
1213 return PJ_EINVALIDOP;
1214}
1215
1216
1217/*
1218 * Close the transport.
1219 */
1220PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
1221 pj_bool_t force )
1222{
1223 /* Make sure id is in range. */
1224 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1225
1226 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001227 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001228
Benny Prijono0a5cad82006-09-26 13:21:02 +00001229 /* Note: destroy() may not work if there are objects still referencing
1230 * the transport.
1231 */
1232 if (force) {
1233 switch (pjsua_var.tpdata[id].type) {
1234 case PJSIP_TRANSPORT_UDP:
1235 return pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
1236 case PJSIP_TRANSPORT_TCP:
1237 break;
Benny Prijono1ebd6142006-10-19 15:48:02 +00001238 default:
1239 break;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001240 }
1241
1242 } else {
1243 switch (pjsua_var.tpdata[id].type) {
1244 case PJSIP_TRANSPORT_UDP:
1245 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
1246 case PJSIP_TRANSPORT_TCP:
1247 return (*pjsua_var.tpdata[id].data.factory->destroy)
1248 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001249 default:
1250 break;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001251 }
1252 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001253
Benny Prijono0a5cad82006-09-26 13:21:02 +00001254 /* Unreachable */
1255 pj_assert(!"Unknown transport");
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001256 return PJ_EINVALIDOP;
1257}
1258
1259
1260/*
1261 * Add additional headers etc in msg_data specified by application
1262 * when sending requests.
1263 */
1264void pjsua_process_msg_data(pjsip_tx_data *tdata,
1265 const pjsua_msg_data *msg_data)
1266{
1267 pj_bool_t allow_body;
1268 const pjsip_hdr *hdr;
1269
Benny Prijono56315612006-07-18 14:39:40 +00001270 /* Always add User-Agent */
1271 if (pjsua_var.ua_cfg.user_agent.slen &&
1272 tdata->msg->type == PJSIP_REQUEST_MSG)
1273 {
1274 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1275 pjsip_hdr *h;
1276 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
1277 &STR_USER_AGENT,
1278 &pjsua_var.ua_cfg.user_agent);
1279 pjsip_msg_add_hdr(tdata->msg, h);
1280 }
1281
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001282 if (!msg_data)
1283 return;
1284
1285 hdr = msg_data->hdr_list.next;
1286 while (hdr && hdr != &msg_data->hdr_list) {
1287 pjsip_hdr *new_hdr;
1288
1289 new_hdr = pjsip_hdr_clone(tdata->pool, hdr);
1290 pjsip_msg_add_hdr(tdata->msg, new_hdr);
1291
1292 hdr = hdr->next;
1293 }
1294
1295 allow_body = (tdata->msg->body == NULL);
1296
1297 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
1298 pjsip_media_type ctype;
1299 pjsip_msg_body *body;
1300
1301 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
1302 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
1303 &msg_data->msg_body);
1304 tdata->msg->body = body;
1305 }
1306}
1307
1308
1309/*
1310 * Add route_set to outgoing requests
1311 */
1312void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
1313 const pjsip_route_hdr *route_set )
1314{
1315 const pjsip_route_hdr *r;
1316
1317 r = route_set->next;
1318 while (r != route_set) {
1319 pjsip_route_hdr *new_r;
1320
1321 new_r = pjsip_hdr_clone(tdata->pool, r);
1322 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
1323
1324 r = r->next;
1325 }
1326}
1327
1328
1329/*
1330 * Simple version of MIME type parsing (it doesn't support parameters)
1331 */
1332void pjsua_parse_media_type( pj_pool_t *pool,
1333 const pj_str_t *mime,
1334 pjsip_media_type *media_type)
1335{
1336 pj_str_t tmp;
1337 char *pos;
1338
Benny Prijonoac623b32006-07-03 15:19:31 +00001339 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001340
1341 pj_strdup_with_null(pool, &tmp, mime);
1342
1343 pos = pj_strchr(&tmp, '/');
1344 if (pos) {
1345 media_type->type.ptr = tmp.ptr;
1346 media_type->type.slen = (pos-tmp.ptr);
1347 media_type->subtype.ptr = pos+1;
1348 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
1349 } else {
1350 media_type->type = tmp;
1351 }
1352}
1353
1354
1355/*
1356 * Verify that valid SIP url is given.
1357 */
1358PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
1359{
1360 pjsip_uri *p;
1361 pj_pool_t *pool;
1362 char *url;
1363 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
1364
1365 if (!len) return -1;
1366
1367 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
1368 if (!pool) return -1;
1369
1370 url = pj_pool_alloc(pool, len+1);
1371 pj_ansi_strcpy(url, c_url);
1372
1373 p = pjsip_parse_uri(pool, url, len, 0);
1374 if (!p || pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0)
1375 p = NULL;
1376
1377 pj_pool_release(pool);
1378 return p ? 0 : -1;
1379}