blob: 8c6e350610ae9686b9351e1179374699bc0c2d70 [file] [log] [blame]
Benny Prijono268ca612006-02-07 12:34:11 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono268ca612006-02-07 12:34:11 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
Benny Prijono4f9f64e2006-02-27 00:00:30 +000019#include <pjsua-lib/pjsua.h>
Benny Prijonoeebe9af2006-06-13 22:57:13 +000020#include <pjsua-lib/pjsua_internal.h>
Benny Prijono268ca612006-02-07 12:34:11 +000021
Benny Prijono268ca612006-02-07 12:34:11 +000022
Benny Prijono84126ab2006-02-09 09:30:09 +000023#define THIS_FILE "pjsua_core.c"
Benny Prijono268ca612006-02-07 12:34:11 +000024
25
Benny Prijonoeebe9af2006-06-13 22:57:13 +000026/* PJSUA application instance. */
27struct pjsua_data pjsua_var;
Benny Prijono84126ab2006-02-09 09:30:09 +000028
29
Benny Prijonoeebe9af2006-06-13 22:57:13 +000030/* Display error */
31PJ_DEF(void) pjsua_perror( const char *sender, const char *title,
32 pj_status_t status)
Benny Prijono268ca612006-02-07 12:34:11 +000033{
Benny Prijonoeebe9af2006-06-13 22:57:13 +000034 char errmsg[PJ_ERR_MSG_SIZE];
Benny Prijonoa91a0032006-02-26 21:23:45 +000035
Benny Prijonoeebe9af2006-06-13 22:57:13 +000036 pj_strerror(status, errmsg, sizeof(errmsg));
37 PJ_LOG(3,(sender, "%s: %s [status=%d]", title, errmsg, status));
Benny Prijono268ca612006-02-07 12:34:11 +000038}
39
40
Benny Prijonoeebe9af2006-06-13 22:57:13 +000041static void init_data()
Benny Prijonodc39fe82006-05-26 12:17:46 +000042{
43 unsigned i;
44
Benny Prijonoc97608e2007-03-23 16:34:20 +000045 pj_bzero(&pjsua_var, sizeof(pjsua_var));
46
Benny Prijonoeebe9af2006-06-13 22:57:13 +000047 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i)
48 pjsua_var.acc[i].index = i;
49
50 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata); ++i)
51 pjsua_var.tpdata[i].index = i;
Benny Prijonoc97608e2007-03-23 16:34:20 +000052
53 pjsua_var.stun_status = PJ_EUNKNOWN;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000054}
Benny Prijonodc39fe82006-05-26 12:17:46 +000055
56
Benny Prijonodc39fe82006-05-26 12:17:46 +000057
Benny Prijonoeebe9af2006-06-13 22:57:13 +000058/*****************************************************************************
59 * This is a very simple PJSIP module, whose sole purpose is to display
60 * incoming and outgoing messages to log. This module will have priority
61 * higher than transport layer, which means:
62 *
63 * - incoming messages will come to this module first before reaching
64 * transaction layer.
65 *
66 * - outgoing messages will come to this module last, after the message
67 * has been 'printed' to contiguous buffer by transport layer and
68 * appropriate transport instance has been decided for this message.
69 *
70 */
Benny Prijonodc39fe82006-05-26 12:17:46 +000071
Benny Prijonoeebe9af2006-06-13 22:57:13 +000072/* Notification on incoming messages */
73static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
74{
Benny Prijonob4a17c92006-07-10 14:40:21 +000075 PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
76 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +000077 "--end msg--",
78 rdata->msg_info.len,
79 pjsip_rx_data_get_info(rdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +000080 rdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +000081 rdata->pkt_info.src_name,
82 rdata->pkt_info.src_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +000083 (int)rdata->msg_info.len,
Benny Prijonoeebe9af2006-06-13 22:57:13 +000084 rdata->msg_info.msg_buf));
85
86 /* Always return false, otherwise messages will not get processed! */
87 return PJ_FALSE;
88}
Benny Prijonodc39fe82006-05-26 12:17:46 +000089
Benny Prijonoeebe9af2006-06-13 22:57:13 +000090/* Notification on outgoing messages */
91static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
92{
93
94 /* Important note:
95 * tp_info field is only valid after outgoing messages has passed
96 * transport layer. So don't try to access tp_info when the module
97 * has lower priority than transport layer.
98 */
Benny Prijonodc39fe82006-05-26 12:17:46 +000099
Benny Prijonob4a17c92006-07-10 14:40:21 +0000100 PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
101 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000102 "--end msg--",
103 (tdata->buf.cur - tdata->buf.start),
104 pjsip_tx_data_get_info(tdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000105 tdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000106 tdata->tp_info.dst_name,
107 tdata->tp_info.dst_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000108 (int)(tdata->buf.cur - tdata->buf.start),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000109 tdata->buf.start));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000110
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000111 /* Always return success, otherwise message will not get sent! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000112 return PJ_SUCCESS;
113}
114
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000115/* The module instance. */
116static pjsip_module pjsua_msg_logger =
117{
118 NULL, NULL, /* prev, next. */
119 { "mod-pjsua-log", 13 }, /* Name. */
120 -1, /* Id */
121 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
122 NULL, /* load() */
123 NULL, /* start() */
124 NULL, /* stop() */
125 NULL, /* unload() */
126 &logging_on_rx_msg, /* on_rx_request() */
127 &logging_on_rx_msg, /* on_rx_response() */
128 &logging_on_tx_msg, /* on_tx_request. */
129 &logging_on_tx_msg, /* on_tx_response() */
130 NULL, /* on_tsx_state() */
131
132};
133
134
135/*****************************************************************************
Benny Prijono56315612006-07-18 14:39:40 +0000136 * Another simple module to handle incoming OPTIONS request
137 */
138
139/* Notification on incoming request */
140static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata)
141{
142 pjsip_tx_data *tdata;
143 pjsip_response_addr res_addr;
144 pjmedia_sdp_session *sdp;
145 const pjsip_hdr *cap_hdr;
146 pj_status_t status;
147
148 /* Only want to handle OPTIONS requests */
149 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
150 &pjsip_options_method) != 0)
151 {
152 return PJ_FALSE;
153 }
154
155 /* Create basic response. */
156 status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL,
157 &tdata);
158 if (status != PJ_SUCCESS) {
159 pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status);
160 return PJ_TRUE;
161 }
162
163 /* Add Allow header */
164 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL);
165 if (cap_hdr) {
166 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
167 }
168
169 /* Add Accept header */
170 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL);
171 if (cap_hdr) {
172 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
173 }
174
175 /* Add Supported header */
176 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL);
177 if (cap_hdr) {
178 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
179 }
180
181 /* Add Allow-Events header from the evsub module */
182 cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL);
183 if (cap_hdr) {
184 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
185 }
186
187 /* Add User-Agent header */
188 if (pjsua_var.ua_cfg.user_agent.slen) {
189 const pj_str_t USER_AGENT = { "User-Agent", 10};
190 pjsip_hdr *h;
191
192 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
193 &USER_AGENT,
194 &pjsua_var.ua_cfg.user_agent);
195 pjsip_msg_add_hdr(tdata->msg, h);
196 }
197
198 /* Add SDP body, using call0's RTP address */
199 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
200 &pjsua_var.calls[0].skinfo, &sdp);
201 if (status == PJ_SUCCESS) {
202 pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
203 }
204
205 /* Send response statelessly */
206 pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
207 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL);
208 if (status != PJ_SUCCESS)
209 pjsip_tx_data_dec_ref(tdata);
210
211 return PJ_TRUE;
212}
213
214
215/* The module instance. */
216static pjsip_module pjsua_options_handler =
217{
218 NULL, NULL, /* prev, next. */
219 { "mod-pjsua-options", 17 }, /* Name. */
220 -1, /* Id */
221 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
222 NULL, /* load() */
223 NULL, /* start() */
224 NULL, /* stop() */
225 NULL, /* unload() */
226 &options_on_rx_request, /* on_rx_request() */
227 NULL, /* on_rx_response() */
228 NULL, /* on_tx_request. */
229 NULL, /* on_tx_response() */
230 NULL, /* on_tsx_state() */
231
232};
233
234
235/*****************************************************************************
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000236 * These two functions are the main callbacks registered to PJSIP stack
237 * to receive SIP request and response messages that are outside any
238 * dialogs and any transactions.
239 */
Benny Prijono268ca612006-02-07 12:34:11 +0000240
241/*
242 * Handler for receiving incoming requests.
243 *
244 * This handler serves multiple purposes:
245 * - it receives requests outside dialogs.
246 * - it receives requests inside dialogs, when the requests are
247 * unhandled by other dialog usages. Example of these
248 * requests are: MESSAGE.
249 */
250static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
251{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000252 pj_bool_t processed = PJ_FALSE;
253
254 PJSUA_LOCK();
Benny Prijono38998232006-02-08 22:44:25 +0000255
Benny Prijono84126ab2006-02-09 09:30:09 +0000256 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000257
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000258 processed = pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000259 }
260
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000261 PJSUA_UNLOCK();
262
263 return processed;
Benny Prijono268ca612006-02-07 12:34:11 +0000264}
265
266
267/*
268 * Handler for receiving incoming responses.
269 *
270 * This handler serves multiple purposes:
271 * - it receives strayed responses (i.e. outside any dialog and
272 * outside any transactions).
273 * - it receives responses coming to a transaction, when pjsua
274 * module is set as transaction user for the transaction.
275 * - it receives responses inside a dialog, when these responses
276 * are unhandled by other dialog usages.
277 */
278static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
279{
280 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000281 return PJ_FALSE;
282}
283
284
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000285/*****************************************************************************
286 * Logging.
287 */
288
289/* Log callback */
290static void log_writer(int level, const char *buffer, int len)
Benny Prijono268ca612006-02-07 12:34:11 +0000291{
Benny Prijono572d4852006-11-23 21:50:02 +0000292 /* Write to file, stdout or application callback. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000293
294 if (pjsua_var.log_file) {
295 pj_ssize_t size = len;
296 pj_file_write(pjsua_var.log_file, buffer, &size);
Benny Prijonoee738712007-03-16 09:25:47 +0000297 pj_file_flush(pjsua_var.log_file);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000298 }
299
Benny Prijono572d4852006-11-23 21:50:02 +0000300 if (level <= (int)pjsua_var.log_cfg.console_level) {
301 if (pjsua_var.log_cfg.cb)
302 (*pjsua_var.log_cfg.cb)(level, buffer, len);
303 else
304 pj_log_write(level, buffer, len);
305 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000306}
307
308
309/*
310 * Application can call this function at any time (after pjsua_create(), of
311 * course) to change logging settings.
312 */
313PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg)
314{
315 pj_status_t status;
316
317 /* Save config. */
318 pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg);
319
320 /* Redirect log function to ours */
321 pj_log_set_log_func( &log_writer );
322
323 /* Close existing file, if any */
324 if (pjsua_var.log_file) {
325 pj_file_close(pjsua_var.log_file);
326 pjsua_var.log_file = NULL;
327 }
328
329 /* If output log file is desired, create the file: */
330 if (pjsua_var.log_cfg.log_filename.slen) {
331
332 status = pj_file_open(pjsua_var.pool,
333 pjsua_var.log_cfg.log_filename.ptr,
334 PJ_O_WRONLY,
335 &pjsua_var.log_file);
336
337 if (status != PJ_SUCCESS) {
338 pjsua_perror(THIS_FILE, "Error creating log file", status);
339 return status;
340 }
341 }
342
343 /* Unregister msg logging if it's previously registered */
344 if (pjsua_msg_logger.id >= 0) {
345 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
346 pjsua_msg_logger.id = -1;
347 }
348
349 /* Enable SIP message logging */
350 if (pjsua_var.log_cfg.msg_logging)
351 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
352
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000353 return PJ_SUCCESS;
354}
355
356
357/*****************************************************************************
358 * PJSUA Base API.
359 */
360
361/* Worker thread function. */
362static int worker_thread(void *arg)
363{
364 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000365
Benny Prijono268ca612006-02-07 12:34:11 +0000366 PJ_UNUSED_ARG(arg);
367
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000368 while (!pjsua_var.thread_quit_flag) {
369 int count;
370
371 count = pjsua_handle_events(TIMEOUT);
372 if (count < 0)
373 pj_thread_sleep(TIMEOUT);
374 }
Benny Prijono268ca612006-02-07 12:34:11 +0000375
376 return 0;
377}
378
Benny Prijonodc39fe82006-05-26 12:17:46 +0000379
Benny Prijono268ca612006-02-07 12:34:11 +0000380/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000381 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000382 */
383PJ_DEF(pj_status_t) pjsua_create(void)
384{
385 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000386
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000387 /* Init pjsua data */
388 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000389
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000390 /* Set default logging settings */
391 pjsua_logging_config_default(&pjsua_var.log_cfg);
392
393 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000394 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000395 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
396
Benny Prijono268ca612006-02-07 12:34:11 +0000397
Benny Prijonofccab712006-02-22 22:23:22 +0000398 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000399 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000400 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000401
Benny Prijono268ca612006-02-07 12:34:11 +0000402
Benny Prijono094d3ad2006-11-21 08:41:00 +0000403 /* Set default sound device ID */
404 pjsua_var.cap_dev = pjsua_var.play_dev = -1;
405
Benny Prijono268ca612006-02-07 12:34:11 +0000406 /* Init caching pool. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000407 pj_caching_pool_init(&pjsua_var.cp, &pj_pool_factory_default_policy, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000408
409 /* Create memory pool for application. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000410 pjsua_var.pool = pjsua_pool_create("pjsua", 4000, 4000);
411
412 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000413
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000414 /* Create mutex */
415 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
416 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000417 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000418 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000419 return status;
420 }
421
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000422 /* Must create SIP endpoint to initialize SIP parser. The parser
423 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000424 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000425 status = pjsip_endpt_create(&pjsua_var.cp.factory,
426 pj_gethostname()->ptr,
427 &pjsua_var.endpt);
428 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000429
430
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000431 return PJ_SUCCESS;
432}
433
434
435/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000436 * Initialize pjsua with the specified settings. All the settings are
437 * optional, and the default values will be used when the config is not
438 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000439 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000440PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
441 const pjsua_logging_config *log_cfg,
442 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000443{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000444 pjsua_config default_cfg;
445 pjsua_media_config default_media_cfg;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000446 const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
Benny Prijonodc39fe82006-05-26 12:17:46 +0000447 pj_status_t status;
448
449
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000450 /* Create default configurations when the config is not supplied */
451
452 if (ua_cfg == NULL) {
453 pjsua_config_default(&default_cfg);
454 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000455 }
456
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000457 if (media_cfg == NULL) {
458 pjsua_media_config_default(&default_media_cfg);
459 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000460 }
461
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000462 /* Initialize logging first so that info/errors can be captured */
463 if (log_cfg) {
464 status = pjsua_reconfigure_logging(log_cfg);
465 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono9fc735d2006-05-28 14:58:12 +0000466 }
467
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000468 /* If nameserver is configured, create DNS resolver instance and
469 * set it to be used by SIP resolver.
470 */
471 if (ua_cfg->nameserver_count) {
472#if PJSIP_HAS_RESOLVER
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000473 unsigned i;
474
475 /* Create DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000476 status = pjsip_endpt_create_resolver(pjsua_var.endpt,
477 &pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000478 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
479
480 /* Configure nameserver for the DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000481 status = pj_dns_resolver_set_ns(pjsua_var.resolver,
482 ua_cfg->nameserver_count,
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000483 ua_cfg->nameserver, NULL);
484 if (status != PJ_SUCCESS) {
485 pjsua_perror(THIS_FILE, "Error setting nameserver", status);
486 return status;
487 }
488
489 /* Set this DNS resolver to be used by the SIP resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000490 status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000491 if (status != PJ_SUCCESS) {
492 pjsua_perror(THIS_FILE, "Error setting DNS resolver", status);
493 return status;
494 }
495
496 /* Print nameservers */
497 for (i=0; i<ua_cfg->nameserver_count; ++i) {
498 PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added",
499 (int)ua_cfg->nameserver[i].slen,
500 ua_cfg->nameserver[i].ptr));
501 }
502#else
503 PJ_LOG(2,(THIS_FILE,
504 "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)"));
505#endif
506 }
507
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000508 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000509
510 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000511 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
512 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000513
Benny Prijonodc39fe82006-05-26 12:17:46 +0000514
515 /* Initialize UA layer module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000516 status = pjsip_ua_init_module( pjsua_var.endpt, NULL );
517 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000518
Benny Prijonodc39fe82006-05-26 12:17:46 +0000519
Benny Prijono053f5222006-11-11 16:16:04 +0000520 /* Initialize Replaces support. */
521 status = pjsip_replaces_init_module( pjsua_var.endpt );
522 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
523
524
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000525 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000526 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000527 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000528 {
529 NULL, NULL, /* prev, next. */
530 { "mod-pjsua", 9 }, /* Name. */
531 -1, /* Id */
532 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
533 NULL, /* load() */
534 NULL, /* start() */
535 NULL, /* stop() */
536 NULL, /* unload() */
537 &mod_pjsua_on_rx_request, /* on_rx_request() */
538 &mod_pjsua_on_rx_response, /* on_rx_response() */
539 NULL, /* on_tx_request. */
540 NULL, /* on_tx_response() */
541 NULL, /* on_tsx_state() */
542 };
543
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000544 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000545
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000546 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
547 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000548 }
549
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000550
Benny Prijonodc39fe82006-05-26 12:17:46 +0000551
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000552 /* Initialize PJSUA call subsystem: */
553 status = pjsua_call_subsys_init(ua_cfg);
554 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000555 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000556
557
Benny Prijonoc97608e2007-03-23 16:34:20 +0000558 /* Start resolving STUN server */
559 status = pjsua_resolve_stun_server(PJ_FALSE);
560 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
561 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
562 return status;
563 }
564
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000565 /* Initialize PJSUA media subsystem */
566 status = pjsua_media_subsys_init(media_cfg);
567 if (status != PJ_SUCCESS)
568 goto on_error;
569
Benny Prijonodc39fe82006-05-26 12:17:46 +0000570
571 /* Init core SIMPLE module : */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000572 status = pjsip_evsub_init_module(pjsua_var.endpt);
573 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000574
Benny Prijonodc39fe82006-05-26 12:17:46 +0000575
576 /* Init presence module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000577 status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance());
578 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000579
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000580 /* Init PUBLISH module */
581 pjsip_publishc_init_module(pjsua_var.endpt);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000582
583 /* Init xfer/REFER module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000584 status = pjsip_xfer_init_module( pjsua_var.endpt );
585 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000586
587 /* Init pjsua presence handler: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000588 status = pjsua_pres_init();
589 if (status != PJ_SUCCESS)
590 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000591
592 /* Init out-of-dialog MESSAGE request handler. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000593 status = pjsua_im_init();
594 if (status != PJ_SUCCESS)
595 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000596
Benny Prijonoc8141a82006-08-20 09:12:19 +0000597 /* Register OPTIONS handler */
598 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler);
599
600 /* Add OPTIONS in Allow header */
601 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
602 NULL, 1, &STR_OPTIONS);
603
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000604 /* Start worker thread if needed. */
605 if (pjsua_var.ua_cfg.thread_cnt) {
606 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000607
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000608 if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread))
609 pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000610
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000611 for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) {
612 status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread,
613 NULL, 0, 0, &pjsua_var.thread[i]);
614 if (status != PJ_SUCCESS)
615 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000616 }
Benny Prijonof521eb02006-08-06 23:07:25 +0000617 PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created",
618 pjsua_var.ua_cfg.thread_cnt));
619 } else {
620 PJ_LOG(4,(THIS_FILE, "No SIP worker threads created"));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000621 }
622
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000623 /* Done! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000624
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000625 PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized",
626 PJ_VERSION, PJ_OS_NAME));
Benny Prijonoccf95622006-02-07 18:48:01 +0000627
Benny Prijono268ca612006-02-07 12:34:11 +0000628 return PJ_SUCCESS;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000629
630on_error:
631 pjsua_destroy();
632 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000633}
634
635
Benny Prijono834aee32006-02-19 01:38:06 +0000636/* Sleep with polling */
637static void busy_sleep(unsigned msec)
638{
639 pj_time_val timeout, now;
640
641 pj_gettimeofday(&timeout);
642 timeout.msec += msec;
643 pj_time_val_normalize(&timeout);
644
645 do {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000646 while (pjsua_handle_events(10) > 0)
647 ;
Benny Prijono834aee32006-02-19 01:38:06 +0000648 pj_gettimeofday(&now);
649 } while (PJ_TIME_VAL_LT(now, timeout));
650}
651
Benny Prijono268ca612006-02-07 12:34:11 +0000652/*
Benny Prijonoc97608e2007-03-23 16:34:20 +0000653 * Resolve STUN server.
654 */
655pj_status_t pjsua_resolve_stun_server(pj_bool_t wait)
656{
657 if (pjsua_var.stun_status == PJ_EUNKNOWN) {
658 /* Initialize STUN configuration */
659 pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0,
660 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
661 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
662
663 /* Start STUN server resolution */
664 /* For now just do DNS A resolution */
665
666 if (pjsua_var.ua_cfg.stun_srv.slen == 0) {
667 pjsua_var.stun_status = PJ_SUCCESS;
668 } else {
669 pj_hostent he;
670
671 pjsua_var.stun_status =
672 pj_gethostbyname(&pjsua_var.ua_cfg.stun_srv, &he);
673
674 if (pjsua_var.stun_status == PJ_SUCCESS) {
675 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
676 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
677 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)3478);
Benny Prijonof75eceb2007-03-23 19:09:54 +0000678
679 PJ_LOG(4,(THIS_FILE,
680 "STUN server %.*s resolved, address is %s:%d",
681 (int)pjsua_var.ua_cfg.stun_srv.slen,
682 pjsua_var.ua_cfg.stun_srv.ptr,
683 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
684 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
Benny Prijonoc97608e2007-03-23 16:34:20 +0000685 }
686 }
687 return pjsua_var.stun_status;
688
689 } else if (pjsua_var.stun_status == PJ_EPENDING) {
690 /* STUN server resolution has been started, wait for the
691 * result.
692 */
693 pj_assert(!"Should not happen");
694 return PJ_EBUG;
695
696 } else {
697 /* STUN server has been resolved, return the status */
698 return pjsua_var.stun_status;
699 }
700}
701
702/*
Benny Prijono268ca612006-02-07 12:34:11 +0000703 * Destroy pjsua.
704 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000705PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +0000706{
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000707 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +0000708
709 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000710 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +0000711
712 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000713 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
714 if (pjsua_var.thread[i]) {
715 pj_thread_join(pjsua_var.thread[i]);
716 pj_thread_destroy(pjsua_var.thread[i]);
717 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +0000718 }
Benny Prijono268ca612006-02-07 12:34:11 +0000719 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000720
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000721 if (pjsua_var.endpt) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000722 /* Terminate all calls. */
723 pjsua_call_hangup_all();
724
725 /* Terminate all presence subscriptions. */
726 pjsua_pres_shutdown();
727
728 /* Unregister, if required: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000729 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
730 if (!pjsua_var.acc[i].valid)
731 continue;
732
733 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000734 pjsua_acc_set_registration(i, PJ_FALSE);
735 }
736 }
737
738 /* Wait for some time to allow unregistration to complete: */
Benny Prijono9fc735d2006-05-28 14:58:12 +0000739 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
740 busy_sleep(1000);
741 }
Benny Prijono834aee32006-02-19 01:38:06 +0000742
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000743 /* Destroy media */
744 pjsua_media_subsys_destroy();
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000745
Benny Prijono268ca612006-02-07 12:34:11 +0000746 /* Destroy endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000747 if (pjsua_var.endpt) {
748 pjsip_endpt_destroy(pjsua_var.endpt);
749 pjsua_var.endpt = NULL;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000750 }
Benny Prijono268ca612006-02-07 12:34:11 +0000751
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000752 /* Destroy mutex */
753 if (pjsua_var.mutex) {
754 pj_mutex_destroy(pjsua_var.mutex);
755 pjsua_var.mutex = NULL;
756 }
757
758 /* Destroy pool and pool factory. */
759 if (pjsua_var.pool) {
760 pj_pool_release(pjsua_var.pool);
761 pjsua_var.pool = NULL;
762 pj_caching_pool_destroy(&pjsua_var.cp);
763 }
Benny Prijono268ca612006-02-07 12:34:11 +0000764
765
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000766 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
767
768 /* End logging */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000769 if (pjsua_var.log_file) {
770 pj_file_close(pjsua_var.log_file);
771 pjsua_var.log_file = NULL;
772 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000773
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000774 /* Shutdown PJLIB */
775 pj_shutdown();
776
Benny Prijonof762ee72006-12-01 11:14:37 +0000777 /* Clear pjsua_var */
778 pj_bzero(&pjsua_var, sizeof(pjsua_var));
779
Benny Prijono268ca612006-02-07 12:34:11 +0000780 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000781 return PJ_SUCCESS;
782}
783
784
785/**
786 * Application is recommended to call this function after all initialization
787 * is done, so that the library can do additional checking set up
788 * additional
789 *
790 * @return PJ_SUCCESS on success, or the appropriate error code.
791 */
792PJ_DEF(pj_status_t) pjsua_start(void)
793{
794 pj_status_t status;
795
796 status = pjsua_call_subsys_start();
797 if (status != PJ_SUCCESS)
798 return status;
799
800 status = pjsua_media_subsys_start();
801 if (status != PJ_SUCCESS)
802 return status;
803
804 status = pjsua_pres_start();
805 if (status != PJ_SUCCESS)
806 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000807
808 return PJ_SUCCESS;
809}
810
Benny Prijono9fc735d2006-05-28 14:58:12 +0000811
812/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000813 * Poll pjsua for events, and if necessary block the caller thread for
814 * the specified maximum interval (in miliseconds).
815 */
816PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
817{
818 unsigned count = 0;
819 pj_time_val tv;
820 pj_status_t status;
821
822 tv.sec = 0;
823 tv.msec = msec_timeout;
824 pj_time_val_normalize(&tv);
825
826 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
827
828 if (status != PJ_SUCCESS)
829 return -status;
830
831 return count;
832}
833
834
835/*
836 * Create memory pool.
837 */
838PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
839 pj_size_t increment)
840{
841 /* Pool factory is thread safe, no need to lock */
842 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
843 NULL);
844}
845
846
847/*
848 * Internal function to get SIP endpoint instance of pjsua, which is
849 * needed for example to register module, create transports, etc.
850 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000851 */
852PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
853{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000854 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000855}
856
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000857/*
858 * Internal function to get media endpoint instance.
859 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000860 */
861PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
862{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000863 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000864}
865
Benny Prijono97b87172006-08-24 14:25:14 +0000866/*
867 * Internal function to get PJSUA pool factory.
868 */
869PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
870{
871 return &pjsua_var.cp.factory;
872}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000873
874/*****************************************************************************
875 * PJSUA SIP Transport API.
876 */
877
878/*
879 * Create and initialize SIP socket (and possibly resolve public
880 * address via STUN, depending on config).
881 */
882static pj_status_t create_sip_udp_sock(pj_in_addr bound_addr,
883 int port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000884 pj_sock_t *p_sock,
885 pj_sockaddr_in *p_pub_addr)
886{
Benny Prijonoc97608e2007-03-23 16:34:20 +0000887 char ip_addr[32];
888 pj_str_t stun_srv;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000889 pj_sock_t sock;
890 pj_status_t status;
891
Benny Prijonoc97608e2007-03-23 16:34:20 +0000892 /* Make sure STUN server resolution has completed */
893 status = pjsua_resolve_stun_server(PJ_TRUE);
894 if (status != PJ_SUCCESS) {
895 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
896 return status;
897 }
898
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000899 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock);
900 if (status != PJ_SUCCESS) {
901 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +0000902 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000903 }
904
Benny Prijonof90ef4f2007-02-12 14:59:57 +0000905 status = pj_sock_bind_in(sock, pj_ntohl(bound_addr.s_addr),
906 (pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000907 if (status != PJ_SUCCESS) {
908 pjsua_perror(THIS_FILE, "bind() error", status);
909 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +0000910 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000911 }
912
Benny Prijonoe347cb02007-02-14 14:36:13 +0000913 /* If port is zero, get the bound port */
914 if (port == 0) {
915 pj_sockaddr_in bound_addr;
916 int namelen = sizeof(bound_addr);
917 status = pj_sock_getsockname(sock, &bound_addr, &namelen);
918 if (status != PJ_SUCCESS) {
919 pjsua_perror(THIS_FILE, "getsockname() error", status);
920 pj_sock_close(sock);
921 return status;
922 }
923
924 port = pj_ntohs(bound_addr.sin_port);
925 }
926
Benny Prijonoc97608e2007-03-23 16:34:20 +0000927 if (pjsua_var.stun_srv.addr.sa_family != 0) {
928 pj_ansi_strcpy(ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
929 stun_srv = pj_str(ip_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000930 } else {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000931 stun_srv.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000932 }
933
934 /* Get the published address, either by STUN or by resolving
935 * the name of local host.
936 */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000937 if (stun_srv.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +0000938 /*
939 * STUN is specified, resolve the address with STUN.
940 */
Benny Prijono14c2b862007-02-21 00:40:05 +0000941 status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
Benny Prijonoc97608e2007-03-23 16:34:20 +0000942 &stun_srv, 3478,
943 &stun_srv, 3478,
944 p_pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000945 if (status != PJ_SUCCESS) {
946 pjsua_perror(THIS_FILE, "Error resolving with STUN", status);
947 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +0000948 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000949 }
950
Benny Prijono0a5cad82006-09-26 13:21:02 +0000951 } else if (p_pub_addr->sin_addr.s_addr != 0) {
952 /*
953 * Public address is already specified, no need to resolve the
954 * address, only set the port.
955 */
Benny Prijonoe347cb02007-02-14 14:36:13 +0000956 if (p_pub_addr->sin_port == 0)
957 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijono0a5cad82006-09-26 13:21:02 +0000958
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000959 } else {
960
Benny Prijono594e4c52006-09-14 18:51:01 +0000961 pj_bzero(p_pub_addr, sizeof(pj_sockaddr_in));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000962
Benny Prijono594e4c52006-09-14 18:51:01 +0000963 status = pj_gethostip(&p_pub_addr->sin_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000964 if (status != PJ_SUCCESS) {
Benny Prijonob43bad72007-01-20 05:11:08 +0000965 pjsua_perror(THIS_FILE, "Unable to get local host IP", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000966 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +0000967 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000968 }
969
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000970 p_pub_addr->sin_family = PJ_AF_INET;
971 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000972 }
973
974 *p_sock = sock;
975
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000976 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
977 pj_inet_ntoa(p_pub_addr->sin_addr),
978 (int)pj_ntohs(p_pub_addr->sin_port)));
979
Benny Prijonod8410532006-06-15 11:04:33 +0000980 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000981}
982
983
984/*
985 * Create SIP transport.
986 */
987PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
988 const pjsua_transport_config *cfg,
989 pjsua_transport_id *p_id)
990{
991 pjsip_transport *tp;
992 unsigned id;
993 pj_status_t status;
994
995 PJSUA_LOCK();
996
997 /* Find empty transport slot */
998 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +0000999 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001000 break;
1001 }
1002
1003 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1004 status = PJ_ETOOMANY;
1005 pjsua_perror(THIS_FILE, "Error creating transport", status);
1006 goto on_return;
1007 }
1008
1009 /* Create the transport */
1010 if (type == PJSIP_TRANSPORT_UDP) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001011 /*
1012 * Create UDP transport.
1013 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001014 pjsua_transport_config config;
Benny Prijonod8410532006-06-15 11:04:33 +00001015 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001016 pj_sockaddr_in bound_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001017 pj_sockaddr_in pub_addr;
1018 pjsip_host_port addr_name;
1019
1020 /* Supply default config if it's not specified */
1021 if (cfg == NULL) {
1022 pjsua_transport_config_default(&config);
1023 cfg = &config;
1024 }
1025
Benny Prijono0a5cad82006-09-26 13:21:02 +00001026 /* Initialize bound address, if any */
1027 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
1028 if (cfg->bound_addr.slen) {
1029 status = pj_sockaddr_in_set_str_addr(&bound_addr,&cfg->bound_addr);
1030 if (status != PJ_SUCCESS) {
1031 pjsua_perror(THIS_FILE,
1032 "Unable to resolve transport bound address",
1033 status);
1034 goto on_return;
1035 }
1036 }
1037
1038 /* Initialize the public address from the config, if any */
1039 pj_sockaddr_in_init(&pub_addr, NULL, (pj_uint16_t)cfg->port);
1040 if (cfg->public_addr.slen) {
1041 status = pj_sockaddr_in_set_str_addr(&pub_addr, &cfg->public_addr);
1042 if (status != PJ_SUCCESS) {
1043 pjsua_perror(THIS_FILE,
1044 "Unable to resolve transport public address",
1045 status);
1046 goto on_return;
1047 }
1048 }
1049
1050 /* Create the socket and possibly resolve the address with STUN
1051 * (only when public address is not specified).
1052 */
1053 status = create_sip_udp_sock(bound_addr.sin_addr, cfg->port,
Benny Prijono0a5cad82006-09-26 13:21:02 +00001054 &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001055 if (status != PJ_SUCCESS)
1056 goto on_return;
1057
1058 addr_name.host = pj_str(pj_inet_ntoa(pub_addr.sin_addr));
1059 addr_name.port = pj_ntohs(pub_addr.sin_port);
1060
1061 /* Create UDP transport */
1062 status = pjsip_udp_transport_attach( pjsua_var.endpt, sock,
1063 &addr_name, 1,
1064 &tp);
1065 if (status != PJ_SUCCESS) {
1066 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
1067 status);
1068 pj_sock_close(sock);
1069 goto on_return;
1070 }
1071
Benny Prijonoe93e2872006-06-28 16:46:49 +00001072
1073 /* Save the transport */
1074 pjsua_var.tpdata[id].type = type;
1075 pjsua_var.tpdata[id].local_name = tp->local_name;
1076 pjsua_var.tpdata[id].data.tp = tp;
1077
1078 } else if (type == PJSIP_TRANSPORT_TCP) {
1079 /*
1080 * Create TCP transport.
1081 */
1082 pjsua_transport_config config;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001083 pjsip_host_port a_name;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001084 pjsip_tpfactory *tcp;
1085 pj_sockaddr_in local_addr;
1086
1087 /* Supply default config if it's not specified */
1088 if (cfg == NULL) {
1089 pjsua_transport_config_default(&config);
1090 cfg = &config;
1091 }
1092
1093 /* Init local address */
1094 pj_sockaddr_in_init(&local_addr, 0, 0);
1095
1096 if (cfg->port)
1097 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1098
Benny Prijono0a5cad82006-09-26 13:21:02 +00001099 if (cfg->bound_addr.slen) {
1100 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1101 if (status != PJ_SUCCESS) {
1102 pjsua_perror(THIS_FILE,
1103 "Unable to resolve transport bound address",
1104 status);
1105 goto on_return;
1106 }
1107 }
1108
1109 /* Init published name */
1110 pj_bzero(&a_name, sizeof(pjsip_host_port));
1111 if (cfg->public_addr.slen)
1112 a_name.host = cfg->public_addr;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001113
1114 /* Create the TCP transport */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001115 status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr,
1116 &a_name, 1, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001117
1118 if (status != PJ_SUCCESS) {
1119 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1120 status);
1121 goto on_return;
1122 }
1123
1124 /* Save the transport */
1125 pjsua_var.tpdata[id].type = type;
1126 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1127 pjsua_var.tpdata[id].data.factory = tcp;
1128
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001129#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1130 } else if (type == PJSIP_TRANSPORT_TLS) {
1131 /*
1132 * Create TLS transport.
1133 */
Benny Prijonof3bbc132006-12-25 06:43:59 +00001134 /*
1135 * Create TCP transport.
1136 */
1137 pjsua_transport_config config;
1138 pjsip_host_port a_name;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001139 pjsip_tpfactory *tls;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001140 pj_sockaddr_in local_addr;
1141
1142 /* Supply default config if it's not specified */
1143 if (cfg == NULL) {
1144 pjsua_transport_config_default(&config);
1145 config.port = 5061;
1146 cfg = &config;
1147 }
1148
1149 /* Init local address */
1150 pj_sockaddr_in_init(&local_addr, 0, 0);
1151
1152 if (cfg->port)
1153 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1154
1155 if (cfg->bound_addr.slen) {
1156 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1157 if (status != PJ_SUCCESS) {
1158 pjsua_perror(THIS_FILE,
1159 "Unable to resolve transport bound address",
1160 status);
1161 goto on_return;
1162 }
1163 }
1164
1165 /* Init published name */
1166 pj_bzero(&a_name, sizeof(pjsip_host_port));
1167 if (cfg->public_addr.slen)
1168 a_name.host = cfg->public_addr;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001169
1170 status = pjsip_tls_transport_start(pjsua_var.endpt,
Benny Prijonof3bbc132006-12-25 06:43:59 +00001171 &cfg->tls_setting,
1172 &local_addr, &a_name, 1, &tls);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001173 if (status != PJ_SUCCESS) {
1174 pjsua_perror(THIS_FILE, "Error creating SIP TLS listener",
1175 status);
1176 goto on_return;
1177 }
1178
1179 /* Save the transport */
1180 pjsua_var.tpdata[id].type = type;
1181 pjsua_var.tpdata[id].local_name = tls->addr_name;
1182 pjsua_var.tpdata[id].data.factory = tls;
1183#endif
1184
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001185 } else {
1186 status = PJSIP_EUNSUPTRANSPORT;
1187 pjsua_perror(THIS_FILE, "Error creating transport", status);
1188 goto on_return;
1189 }
1190
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001191
1192 /* Return the ID */
1193 if (p_id) *p_id = id;
1194
1195 status = PJ_SUCCESS;
1196
1197on_return:
1198
1199 PJSUA_UNLOCK();
1200
Benny Prijonod8410532006-06-15 11:04:33 +00001201 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001202}
1203
1204
1205/*
1206 * Register transport that has been created by application.
1207 */
1208PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1209 pjsua_transport_id *p_id)
1210{
1211 unsigned id;
1212
1213 PJSUA_LOCK();
1214
1215 /* Find empty transport slot */
1216 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001217 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001218 break;
1219 }
1220
1221 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1222 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1223 PJSUA_UNLOCK();
1224 return PJ_ETOOMANY;
1225 }
1226
1227 /* Save the transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001228 pjsua_var.tpdata[id].type = tp->key.type;
1229 pjsua_var.tpdata[id].local_name = tp->local_name;
1230 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001231
1232 /* Return the ID */
1233 if (p_id) *p_id = id;
1234
1235 PJSUA_UNLOCK();
1236
1237 return PJ_SUCCESS;
1238}
1239
1240
1241/*
1242 * Enumerate all transports currently created in the system.
1243 */
1244PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1245 unsigned *p_count )
1246{
1247 unsigned i, count;
1248
1249 PJSUA_LOCK();
1250
1251 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
1252 ++i)
1253 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001254 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001255 continue;
1256
1257 id[count++] = i;
1258 }
1259
1260 *p_count = count;
1261
1262 PJSUA_UNLOCK();
1263
1264 return PJ_SUCCESS;
1265}
1266
1267
1268/*
1269 * Get information about transports.
1270 */
1271PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
1272 pjsua_transport_info *info)
1273{
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001274 pjsua_transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00001275 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001276
Benny Prijonoac623b32006-07-03 15:19:31 +00001277 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001278
1279 /* Make sure id is in range. */
1280 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1281
1282 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001283 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001284
1285 PJSUA_LOCK();
1286
Benny Prijonoe93e2872006-06-28 16:46:49 +00001287 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
1288
1289 pjsip_transport *tp = t->data.tp;
1290
1291 if (tp == NULL) {
1292 PJSUA_UNLOCK();
1293 return PJ_EINVALIDOP;
1294 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001295
Benny Prijonoe93e2872006-06-28 16:46:49 +00001296 info->id = id;
1297 info->type = tp->key.type;
1298 info->type_name = pj_str(tp->type_name);
1299 info->info = pj_str(tp->info);
1300 info->flag = tp->flag;
1301 info->addr_len = tp->addr_len;
1302 info->local_addr = tp->local_addr;
1303 info->local_name = tp->local_name;
1304 info->usage_count = pj_atomic_get(tp->ref_cnt);
1305
Benny Prijono0a5cad82006-09-26 13:21:02 +00001306 status = PJ_SUCCESS;
1307
Benny Prijonoe93e2872006-06-28 16:46:49 +00001308 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
1309
1310 pjsip_tpfactory *factory = t->data.factory;
1311
1312 if (factory == NULL) {
1313 PJSUA_UNLOCK();
1314 return PJ_EINVALIDOP;
1315 }
1316
1317 info->id = id;
1318 info->type = t->type;
1319 info->type_name = pj_str("TCP");
1320 info->info = pj_str("TCP transport");
1321 info->flag = factory->flag;
1322 info->addr_len = sizeof(factory->local_addr);
1323 info->local_addr = factory->local_addr;
1324 info->local_name = factory->addr_name;
1325 info->usage_count = 0;
1326
Benny Prijono0a5cad82006-09-26 13:21:02 +00001327 status = PJ_SUCCESS;
1328
1329 } else {
1330 pj_assert(!"Unsupported transport");
1331 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001332 }
1333
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001334
1335 PJSUA_UNLOCK();
1336
Benny Prijono0a5cad82006-09-26 13:21:02 +00001337 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001338}
1339
1340
1341/*
1342 * Disable a transport or re-enable it.
1343 */
1344PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
1345 pj_bool_t enabled)
1346{
1347 /* Make sure id is in range. */
1348 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1349
1350 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001351 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001352
1353
1354 /* To be done!! */
1355 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001356 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001357
1358 return PJ_EINVALIDOP;
1359}
1360
1361
1362/*
1363 * Close the transport.
1364 */
1365PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
1366 pj_bool_t force )
1367{
Benny Prijono5ff61872007-02-01 03:37:11 +00001368 pj_status_t status;
1369
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001370 /* Make sure id is in range. */
1371 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1372
1373 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001374 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001375
Benny Prijono0a5cad82006-09-26 13:21:02 +00001376 /* Note: destroy() may not work if there are objects still referencing
1377 * the transport.
1378 */
1379 if (force) {
1380 switch (pjsua_var.tpdata[id].type) {
1381 case PJSIP_TRANSPORT_UDP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001382 status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
1383 if (status != PJ_SUCCESS)
1384 return status;
1385 status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
1386 if (status != PJ_SUCCESS)
1387 return status;
1388 break;
1389
1390 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001391 case PJSIP_TRANSPORT_TCP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001392 /* This will close the TCP listener, but existing TCP/TLS
1393 * connections (if any) will still linger
1394 */
1395 status = (*pjsua_var.tpdata[id].data.factory->destroy)
1396 (pjsua_var.tpdata[id].data.factory);
1397 if (status != PJ_SUCCESS)
1398 return status;
1399
Benny Prijono0a5cad82006-09-26 13:21:02 +00001400 break;
Benny Prijono5ff61872007-02-01 03:37:11 +00001401
Benny Prijono1ebd6142006-10-19 15:48:02 +00001402 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001403 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001404 }
1405
1406 } else {
Benny Prijono5ff61872007-02-01 03:37:11 +00001407 /* If force is not specified, transports will be closed at their
1408 * convenient time. However this will leak PJSUA-API transport
1409 * descriptors as PJSUA-API wouldn't know when exactly the
1410 * transport is closed thus it can't cleanup PJSUA transport
1411 * descriptor.
1412 */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001413 switch (pjsua_var.tpdata[id].type) {
1414 case PJSIP_TRANSPORT_UDP:
1415 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
Benny Prijono5ff61872007-02-01 03:37:11 +00001416 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001417 case PJSIP_TRANSPORT_TCP:
1418 return (*pjsua_var.tpdata[id].data.factory->destroy)
1419 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001420 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001421 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001422 }
1423 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001424
Benny Prijono5ff61872007-02-01 03:37:11 +00001425 /* Cleanup pjsua data when force is applied */
1426 if (force) {
1427 pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED;
1428 pjsua_var.tpdata[id].data.ptr = NULL;
1429 }
1430
1431 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001432}
1433
1434
1435/*
1436 * Add additional headers etc in msg_data specified by application
1437 * when sending requests.
1438 */
1439void pjsua_process_msg_data(pjsip_tx_data *tdata,
1440 const pjsua_msg_data *msg_data)
1441{
1442 pj_bool_t allow_body;
1443 const pjsip_hdr *hdr;
1444
Benny Prijono56315612006-07-18 14:39:40 +00001445 /* Always add User-Agent */
1446 if (pjsua_var.ua_cfg.user_agent.slen &&
1447 tdata->msg->type == PJSIP_REQUEST_MSG)
1448 {
1449 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1450 pjsip_hdr *h;
1451 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
1452 &STR_USER_AGENT,
1453 &pjsua_var.ua_cfg.user_agent);
1454 pjsip_msg_add_hdr(tdata->msg, h);
1455 }
1456
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001457 if (!msg_data)
1458 return;
1459
1460 hdr = msg_data->hdr_list.next;
1461 while (hdr && hdr != &msg_data->hdr_list) {
1462 pjsip_hdr *new_hdr;
1463
1464 new_hdr = pjsip_hdr_clone(tdata->pool, hdr);
1465 pjsip_msg_add_hdr(tdata->msg, new_hdr);
1466
1467 hdr = hdr->next;
1468 }
1469
1470 allow_body = (tdata->msg->body == NULL);
1471
1472 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
1473 pjsip_media_type ctype;
1474 pjsip_msg_body *body;
1475
1476 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
1477 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
1478 &msg_data->msg_body);
1479 tdata->msg->body = body;
1480 }
1481}
1482
1483
1484/*
1485 * Add route_set to outgoing requests
1486 */
1487void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
1488 const pjsip_route_hdr *route_set )
1489{
1490 const pjsip_route_hdr *r;
1491
1492 r = route_set->next;
1493 while (r != route_set) {
1494 pjsip_route_hdr *new_r;
1495
1496 new_r = pjsip_hdr_clone(tdata->pool, r);
1497 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
1498
1499 r = r->next;
1500 }
1501}
1502
1503
1504/*
1505 * Simple version of MIME type parsing (it doesn't support parameters)
1506 */
1507void pjsua_parse_media_type( pj_pool_t *pool,
1508 const pj_str_t *mime,
1509 pjsip_media_type *media_type)
1510{
1511 pj_str_t tmp;
1512 char *pos;
1513
Benny Prijonoac623b32006-07-03 15:19:31 +00001514 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001515
1516 pj_strdup_with_null(pool, &tmp, mime);
1517
1518 pos = pj_strchr(&tmp, '/');
1519 if (pos) {
1520 media_type->type.ptr = tmp.ptr;
1521 media_type->type.slen = (pos-tmp.ptr);
1522 media_type->subtype.ptr = pos+1;
1523 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
1524 } else {
1525 media_type->type = tmp;
1526 }
1527}
1528
1529
1530/*
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001531 * Internal function to init transport selector from transport id.
1532 */
1533void pjsua_init_tpselector(pjsua_transport_id tp_id,
1534 pjsip_tpselector *sel)
1535{
1536 pjsua_transport_data *tpdata;
1537 unsigned flag;
1538
1539 pj_bzero(sel, sizeof(*sel));
1540 if (tp_id == PJSUA_INVALID_ID)
1541 return;
1542
1543 pj_assert(tp_id >= 0 && tp_id < PJ_ARRAY_SIZE(pjsua_var.tpdata));
1544 tpdata = &pjsua_var.tpdata[tp_id];
1545
1546 flag = pjsip_transport_get_flag_from_type(tpdata->type);
1547
1548 if (flag & PJSIP_TRANSPORT_DATAGRAM) {
1549 sel->type = PJSIP_TPSELECTOR_TRANSPORT;
1550 sel->u.transport = tpdata->data.tp;
1551 } else {
1552 sel->type = PJSIP_TPSELECTOR_LISTENER;
1553 sel->u.listener = tpdata->data.factory;
1554 }
1555}
1556
1557
1558/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001559 * Verify that valid SIP url is given.
1560 */
1561PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
1562{
1563 pjsip_uri *p;
1564 pj_pool_t *pool;
1565 char *url;
1566 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
1567
1568 if (!len) return -1;
1569
1570 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
1571 if (!pool) return -1;
1572
1573 url = pj_pool_alloc(pool, len+1);
1574 pj_ansi_strcpy(url, c_url);
1575
1576 p = pjsip_parse_uri(pool, url, len, 0);
1577 if (!p || pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0)
1578 p = NULL;
1579
1580 pj_pool_release(pool);
1581 return p ? 0 : -1;
1582}