blob: fba772b726b3b039c7b25c23b84a5beeadf8b260 [file] [log] [blame]
Benny Prijono268ca612006-02-07 12:34:11 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono268ca612006-02-07 12:34:11 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
Benny Prijono4f9f64e2006-02-27 00:00:30 +000019#include <pjsua-lib/pjsua.h>
Benny Prijonoeebe9af2006-06-13 22:57:13 +000020#include <pjsua-lib/pjsua_internal.h>
Benny Prijono268ca612006-02-07 12:34:11 +000021
Benny Prijono268ca612006-02-07 12:34:11 +000022
Benny Prijono84126ab2006-02-09 09:30:09 +000023#define THIS_FILE "pjsua_core.c"
Benny Prijono268ca612006-02-07 12:34:11 +000024
25
Benny Prijonoeebe9af2006-06-13 22:57:13 +000026/* PJSUA application instance. */
27struct pjsua_data pjsua_var;
Benny Prijono84126ab2006-02-09 09:30:09 +000028
29
Benny Prijonoeebe9af2006-06-13 22:57:13 +000030/* Display error */
31PJ_DEF(void) pjsua_perror( const char *sender, const char *title,
32 pj_status_t status)
Benny Prijono268ca612006-02-07 12:34:11 +000033{
Benny Prijonoeebe9af2006-06-13 22:57:13 +000034 char errmsg[PJ_ERR_MSG_SIZE];
Benny Prijonoa91a0032006-02-26 21:23:45 +000035
Benny Prijonoeebe9af2006-06-13 22:57:13 +000036 pj_strerror(status, errmsg, sizeof(errmsg));
37 PJ_LOG(3,(sender, "%s: %s [status=%d]", title, errmsg, status));
Benny Prijono268ca612006-02-07 12:34:11 +000038}
39
40
Benny Prijonoeebe9af2006-06-13 22:57:13 +000041static void init_data()
Benny Prijonodc39fe82006-05-26 12:17:46 +000042{
43 unsigned i;
44
Benny Prijonoc97608e2007-03-23 16:34:20 +000045 pj_bzero(&pjsua_var, sizeof(pjsua_var));
46
Benny Prijonoeebe9af2006-06-13 22:57:13 +000047 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i)
48 pjsua_var.acc[i].index = i;
49
50 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata); ++i)
51 pjsua_var.tpdata[i].index = i;
Benny Prijonoc97608e2007-03-23 16:34:20 +000052
53 pjsua_var.stun_status = PJ_EUNKNOWN;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000054}
Benny Prijonodc39fe82006-05-26 12:17:46 +000055
56
Benny Prijonodc39fe82006-05-26 12:17:46 +000057
Benny Prijonoeebe9af2006-06-13 22:57:13 +000058/*****************************************************************************
59 * This is a very simple PJSIP module, whose sole purpose is to display
60 * incoming and outgoing messages to log. This module will have priority
61 * higher than transport layer, which means:
62 *
63 * - incoming messages will come to this module first before reaching
64 * transaction layer.
65 *
66 * - outgoing messages will come to this module last, after the message
67 * has been 'printed' to contiguous buffer by transport layer and
68 * appropriate transport instance has been decided for this message.
69 *
70 */
Benny Prijonodc39fe82006-05-26 12:17:46 +000071
Benny Prijonoeebe9af2006-06-13 22:57:13 +000072/* Notification on incoming messages */
73static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
74{
Benny Prijonob4a17c92006-07-10 14:40:21 +000075 PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
76 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +000077 "--end msg--",
78 rdata->msg_info.len,
79 pjsip_rx_data_get_info(rdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +000080 rdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +000081 rdata->pkt_info.src_name,
82 rdata->pkt_info.src_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +000083 (int)rdata->msg_info.len,
Benny Prijonoeebe9af2006-06-13 22:57:13 +000084 rdata->msg_info.msg_buf));
85
86 /* Always return false, otherwise messages will not get processed! */
87 return PJ_FALSE;
88}
Benny Prijonodc39fe82006-05-26 12:17:46 +000089
Benny Prijonoeebe9af2006-06-13 22:57:13 +000090/* Notification on outgoing messages */
91static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
92{
93
94 /* Important note:
95 * tp_info field is only valid after outgoing messages has passed
96 * transport layer. So don't try to access tp_info when the module
97 * has lower priority than transport layer.
98 */
Benny Prijonodc39fe82006-05-26 12:17:46 +000099
Benny Prijonob4a17c92006-07-10 14:40:21 +0000100 PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
101 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000102 "--end msg--",
103 (tdata->buf.cur - tdata->buf.start),
104 pjsip_tx_data_get_info(tdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000105 tdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000106 tdata->tp_info.dst_name,
107 tdata->tp_info.dst_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000108 (int)(tdata->buf.cur - tdata->buf.start),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000109 tdata->buf.start));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000110
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000111 /* Always return success, otherwise message will not get sent! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000112 return PJ_SUCCESS;
113}
114
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000115/* The module instance. */
116static pjsip_module pjsua_msg_logger =
117{
118 NULL, NULL, /* prev, next. */
119 { "mod-pjsua-log", 13 }, /* Name. */
120 -1, /* Id */
121 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
122 NULL, /* load() */
123 NULL, /* start() */
124 NULL, /* stop() */
125 NULL, /* unload() */
126 &logging_on_rx_msg, /* on_rx_request() */
127 &logging_on_rx_msg, /* on_rx_response() */
128 &logging_on_tx_msg, /* on_tx_request. */
129 &logging_on_tx_msg, /* on_tx_response() */
130 NULL, /* on_tsx_state() */
131
132};
133
134
135/*****************************************************************************
Benny Prijono56315612006-07-18 14:39:40 +0000136 * Another simple module to handle incoming OPTIONS request
137 */
138
139/* Notification on incoming request */
140static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata)
141{
142 pjsip_tx_data *tdata;
143 pjsip_response_addr res_addr;
144 pjmedia_sdp_session *sdp;
145 const pjsip_hdr *cap_hdr;
146 pj_status_t status;
147
148 /* Only want to handle OPTIONS requests */
149 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
150 &pjsip_options_method) != 0)
151 {
152 return PJ_FALSE;
153 }
154
155 /* Create basic response. */
156 status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL,
157 &tdata);
158 if (status != PJ_SUCCESS) {
159 pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status);
160 return PJ_TRUE;
161 }
162
163 /* Add Allow header */
164 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL);
165 if (cap_hdr) {
166 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
167 }
168
169 /* Add Accept header */
170 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL);
171 if (cap_hdr) {
172 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
173 }
174
175 /* Add Supported header */
176 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL);
177 if (cap_hdr) {
178 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
179 }
180
181 /* Add Allow-Events header from the evsub module */
182 cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL);
183 if (cap_hdr) {
184 pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr));
185 }
186
187 /* Add User-Agent header */
188 if (pjsua_var.ua_cfg.user_agent.slen) {
189 const pj_str_t USER_AGENT = { "User-Agent", 10};
190 pjsip_hdr *h;
191
192 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
193 &USER_AGENT,
194 &pjsua_var.ua_cfg.user_agent);
195 pjsip_msg_add_hdr(tdata->msg, h);
196 }
197
198 /* Add SDP body, using call0's RTP address */
199 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
200 &pjsua_var.calls[0].skinfo, &sdp);
201 if (status == PJ_SUCCESS) {
202 pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
203 }
204
205 /* Send response statelessly */
206 pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
207 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL);
208 if (status != PJ_SUCCESS)
209 pjsip_tx_data_dec_ref(tdata);
210
211 return PJ_TRUE;
212}
213
214
215/* The module instance. */
216static pjsip_module pjsua_options_handler =
217{
218 NULL, NULL, /* prev, next. */
219 { "mod-pjsua-options", 17 }, /* Name. */
220 -1, /* Id */
221 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
222 NULL, /* load() */
223 NULL, /* start() */
224 NULL, /* stop() */
225 NULL, /* unload() */
226 &options_on_rx_request, /* on_rx_request() */
227 NULL, /* on_rx_response() */
228 NULL, /* on_tx_request. */
229 NULL, /* on_tx_response() */
230 NULL, /* on_tsx_state() */
231
232};
233
234
235/*****************************************************************************
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000236 * These two functions are the main callbacks registered to PJSIP stack
237 * to receive SIP request and response messages that are outside any
238 * dialogs and any transactions.
239 */
Benny Prijono268ca612006-02-07 12:34:11 +0000240
241/*
242 * Handler for receiving incoming requests.
243 *
244 * This handler serves multiple purposes:
245 * - it receives requests outside dialogs.
246 * - it receives requests inside dialogs, when the requests are
247 * unhandled by other dialog usages. Example of these
248 * requests are: MESSAGE.
249 */
250static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
251{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000252 pj_bool_t processed = PJ_FALSE;
253
254 PJSUA_LOCK();
Benny Prijono38998232006-02-08 22:44:25 +0000255
Benny Prijono84126ab2006-02-09 09:30:09 +0000256 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000257
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000258 processed = pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000259 }
260
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000261 PJSUA_UNLOCK();
262
263 return processed;
Benny Prijono268ca612006-02-07 12:34:11 +0000264}
265
266
267/*
268 * Handler for receiving incoming responses.
269 *
270 * This handler serves multiple purposes:
271 * - it receives strayed responses (i.e. outside any dialog and
272 * outside any transactions).
273 * - it receives responses coming to a transaction, when pjsua
274 * module is set as transaction user for the transaction.
275 * - it receives responses inside a dialog, when these responses
276 * are unhandled by other dialog usages.
277 */
278static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
279{
280 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000281 return PJ_FALSE;
282}
283
284
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000285/*****************************************************************************
286 * Logging.
287 */
288
289/* Log callback */
290static void log_writer(int level, const char *buffer, int len)
Benny Prijono268ca612006-02-07 12:34:11 +0000291{
Benny Prijono572d4852006-11-23 21:50:02 +0000292 /* Write to file, stdout or application callback. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000293
294 if (pjsua_var.log_file) {
295 pj_ssize_t size = len;
296 pj_file_write(pjsua_var.log_file, buffer, &size);
Benny Prijonoee738712007-03-16 09:25:47 +0000297 pj_file_flush(pjsua_var.log_file);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000298 }
299
Benny Prijono572d4852006-11-23 21:50:02 +0000300 if (level <= (int)pjsua_var.log_cfg.console_level) {
301 if (pjsua_var.log_cfg.cb)
302 (*pjsua_var.log_cfg.cb)(level, buffer, len);
303 else
304 pj_log_write(level, buffer, len);
305 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000306}
307
308
309/*
310 * Application can call this function at any time (after pjsua_create(), of
311 * course) to change logging settings.
312 */
313PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg)
314{
315 pj_status_t status;
316
317 /* Save config. */
318 pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg);
319
320 /* Redirect log function to ours */
321 pj_log_set_log_func( &log_writer );
322
323 /* Close existing file, if any */
324 if (pjsua_var.log_file) {
325 pj_file_close(pjsua_var.log_file);
326 pjsua_var.log_file = NULL;
327 }
328
329 /* If output log file is desired, create the file: */
330 if (pjsua_var.log_cfg.log_filename.slen) {
331
332 status = pj_file_open(pjsua_var.pool,
333 pjsua_var.log_cfg.log_filename.ptr,
334 PJ_O_WRONLY,
335 &pjsua_var.log_file);
336
337 if (status != PJ_SUCCESS) {
338 pjsua_perror(THIS_FILE, "Error creating log file", status);
339 return status;
340 }
341 }
342
343 /* Unregister msg logging if it's previously registered */
344 if (pjsua_msg_logger.id >= 0) {
345 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
346 pjsua_msg_logger.id = -1;
347 }
348
349 /* Enable SIP message logging */
350 if (pjsua_var.log_cfg.msg_logging)
351 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
352
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000353 return PJ_SUCCESS;
354}
355
356
357/*****************************************************************************
358 * PJSUA Base API.
359 */
360
361/* Worker thread function. */
362static int worker_thread(void *arg)
363{
364 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000365
Benny Prijono268ca612006-02-07 12:34:11 +0000366 PJ_UNUSED_ARG(arg);
367
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000368 while (!pjsua_var.thread_quit_flag) {
369 int count;
370
371 count = pjsua_handle_events(TIMEOUT);
372 if (count < 0)
373 pj_thread_sleep(TIMEOUT);
374 }
Benny Prijono268ca612006-02-07 12:34:11 +0000375
376 return 0;
377}
378
Benny Prijonodc39fe82006-05-26 12:17:46 +0000379
Benny Prijono268ca612006-02-07 12:34:11 +0000380/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000381 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000382 */
383PJ_DEF(pj_status_t) pjsua_create(void)
384{
385 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000386
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000387 /* Init pjsua data */
388 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000389
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000390 /* Set default logging settings */
391 pjsua_logging_config_default(&pjsua_var.log_cfg);
392
393 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000394 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000395 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
396
Benny Prijono268ca612006-02-07 12:34:11 +0000397
Benny Prijonofccab712006-02-22 22:23:22 +0000398 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000399 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000400 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000401
Benny Prijonoec921342007-03-24 13:00:30 +0000402 /* Init PJNATH */
403 status = pjnath_init();
404 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono268ca612006-02-07 12:34:11 +0000405
Benny Prijono094d3ad2006-11-21 08:41:00 +0000406 /* Set default sound device ID */
407 pjsua_var.cap_dev = pjsua_var.play_dev = -1;
408
Benny Prijono268ca612006-02-07 12:34:11 +0000409 /* Init caching pool. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000410 pj_caching_pool_init(&pjsua_var.cp, &pj_pool_factory_default_policy, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000411
412 /* Create memory pool for application. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000413 pjsua_var.pool = pjsua_pool_create("pjsua", 4000, 4000);
414
415 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000416
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000417 /* Create mutex */
418 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
419 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000420 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000421 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000422 return status;
423 }
424
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000425 /* Must create SIP endpoint to initialize SIP parser. The parser
426 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000427 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000428 status = pjsip_endpt_create(&pjsua_var.cp.factory,
429 pj_gethostname()->ptr,
430 &pjsua_var.endpt);
431 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000432
433
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000434 return PJ_SUCCESS;
435}
436
437
438/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000439 * Initialize pjsua with the specified settings. All the settings are
440 * optional, and the default values will be used when the config is not
441 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000442 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000443PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
444 const pjsua_logging_config *log_cfg,
445 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000446{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000447 pjsua_config default_cfg;
448 pjsua_media_config default_media_cfg;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000449 const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
Benny Prijonodc39fe82006-05-26 12:17:46 +0000450 pj_status_t status;
451
452
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000453 /* Create default configurations when the config is not supplied */
454
455 if (ua_cfg == NULL) {
456 pjsua_config_default(&default_cfg);
457 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000458 }
459
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000460 if (media_cfg == NULL) {
461 pjsua_media_config_default(&default_media_cfg);
462 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000463 }
464
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000465 /* Initialize logging first so that info/errors can be captured */
466 if (log_cfg) {
467 status = pjsua_reconfigure_logging(log_cfg);
468 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono9fc735d2006-05-28 14:58:12 +0000469 }
470
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000471 /* If nameserver is configured, create DNS resolver instance and
472 * set it to be used by SIP resolver.
473 */
474 if (ua_cfg->nameserver_count) {
475#if PJSIP_HAS_RESOLVER
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000476 unsigned i;
477
478 /* Create DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000479 status = pjsip_endpt_create_resolver(pjsua_var.endpt,
480 &pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000481 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
482
483 /* Configure nameserver for the DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000484 status = pj_dns_resolver_set_ns(pjsua_var.resolver,
485 ua_cfg->nameserver_count,
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000486 ua_cfg->nameserver, NULL);
487 if (status != PJ_SUCCESS) {
488 pjsua_perror(THIS_FILE, "Error setting nameserver", status);
489 return status;
490 }
491
492 /* Set this DNS resolver to be used by the SIP resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000493 status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000494 if (status != PJ_SUCCESS) {
495 pjsua_perror(THIS_FILE, "Error setting DNS resolver", status);
496 return status;
497 }
498
499 /* Print nameservers */
500 for (i=0; i<ua_cfg->nameserver_count; ++i) {
501 PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added",
502 (int)ua_cfg->nameserver[i].slen,
503 ua_cfg->nameserver[i].ptr));
504 }
505#else
506 PJ_LOG(2,(THIS_FILE,
507 "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)"));
508#endif
509 }
510
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000511 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000512
513 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000514 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
515 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000516
Benny Prijonodc39fe82006-05-26 12:17:46 +0000517
518 /* Initialize UA layer module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000519 status = pjsip_ua_init_module( pjsua_var.endpt, NULL );
520 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000521
Benny Prijonodc39fe82006-05-26 12:17:46 +0000522
Benny Prijono053f5222006-11-11 16:16:04 +0000523 /* Initialize Replaces support. */
524 status = pjsip_replaces_init_module( pjsua_var.endpt );
525 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
526
527
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000528 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000529 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000530 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000531 {
532 NULL, NULL, /* prev, next. */
533 { "mod-pjsua", 9 }, /* Name. */
534 -1, /* Id */
535 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
536 NULL, /* load() */
537 NULL, /* start() */
538 NULL, /* stop() */
539 NULL, /* unload() */
540 &mod_pjsua_on_rx_request, /* on_rx_request() */
541 &mod_pjsua_on_rx_response, /* on_rx_response() */
542 NULL, /* on_tx_request. */
543 NULL, /* on_tx_response() */
544 NULL, /* on_tsx_state() */
545 };
546
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000547 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000548
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000549 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
550 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000551 }
552
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000553
Benny Prijonodc39fe82006-05-26 12:17:46 +0000554
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000555 /* Initialize PJSUA call subsystem: */
556 status = pjsua_call_subsys_init(ua_cfg);
557 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000558 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000559
560
Benny Prijonoc97608e2007-03-23 16:34:20 +0000561 /* Start resolving STUN server */
562 status = pjsua_resolve_stun_server(PJ_FALSE);
563 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
564 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
565 return status;
566 }
567
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000568 /* Initialize PJSUA media subsystem */
569 status = pjsua_media_subsys_init(media_cfg);
570 if (status != PJ_SUCCESS)
571 goto on_error;
572
Benny Prijonodc39fe82006-05-26 12:17:46 +0000573
574 /* Init core SIMPLE module : */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000575 status = pjsip_evsub_init_module(pjsua_var.endpt);
576 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000577
Benny Prijonodc39fe82006-05-26 12:17:46 +0000578
579 /* Init presence module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000580 status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance());
581 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000582
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000583 /* Init PUBLISH module */
584 pjsip_publishc_init_module(pjsua_var.endpt);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000585
586 /* Init xfer/REFER module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000587 status = pjsip_xfer_init_module( pjsua_var.endpt );
588 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000589
590 /* Init pjsua presence handler: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000591 status = pjsua_pres_init();
592 if (status != PJ_SUCCESS)
593 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000594
595 /* Init out-of-dialog MESSAGE request handler. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000596 status = pjsua_im_init();
597 if (status != PJ_SUCCESS)
598 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000599
Benny Prijonoc8141a82006-08-20 09:12:19 +0000600 /* Register OPTIONS handler */
601 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler);
602
603 /* Add OPTIONS in Allow header */
604 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
605 NULL, 1, &STR_OPTIONS);
606
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000607 /* Start worker thread if needed. */
608 if (pjsua_var.ua_cfg.thread_cnt) {
609 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000610
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000611 if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread))
612 pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000613
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000614 for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) {
615 status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread,
616 NULL, 0, 0, &pjsua_var.thread[i]);
617 if (status != PJ_SUCCESS)
618 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000619 }
Benny Prijonof521eb02006-08-06 23:07:25 +0000620 PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created",
621 pjsua_var.ua_cfg.thread_cnt));
622 } else {
623 PJ_LOG(4,(THIS_FILE, "No SIP worker threads created"));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000624 }
625
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000626 /* Done! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000627
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000628 PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized",
629 PJ_VERSION, PJ_OS_NAME));
Benny Prijonoccf95622006-02-07 18:48:01 +0000630
Benny Prijono268ca612006-02-07 12:34:11 +0000631 return PJ_SUCCESS;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000632
633on_error:
634 pjsua_destroy();
635 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000636}
637
638
Benny Prijono834aee32006-02-19 01:38:06 +0000639/* Sleep with polling */
640static void busy_sleep(unsigned msec)
641{
642 pj_time_val timeout, now;
643
644 pj_gettimeofday(&timeout);
645 timeout.msec += msec;
646 pj_time_val_normalize(&timeout);
647
648 do {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000649 while (pjsua_handle_events(10) > 0)
650 ;
Benny Prijono834aee32006-02-19 01:38:06 +0000651 pj_gettimeofday(&now);
652 } while (PJ_TIME_VAL_LT(now, timeout));
653}
654
Benny Prijono268ca612006-02-07 12:34:11 +0000655/*
Benny Prijonoc97608e2007-03-23 16:34:20 +0000656 * Resolve STUN server.
657 */
658pj_status_t pjsua_resolve_stun_server(pj_bool_t wait)
659{
660 if (pjsua_var.stun_status == PJ_EUNKNOWN) {
661 /* Initialize STUN configuration */
662 pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0,
663 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
664 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
665
666 /* Start STUN server resolution */
667 /* For now just do DNS A resolution */
668
669 if (pjsua_var.ua_cfg.stun_srv.slen == 0) {
670 pjsua_var.stun_status = PJ_SUCCESS;
671 } else {
672 pj_hostent he;
673
674 pjsua_var.stun_status =
675 pj_gethostbyname(&pjsua_var.ua_cfg.stun_srv, &he);
676
677 if (pjsua_var.stun_status == PJ_SUCCESS) {
678 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
679 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
680 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)3478);
Benny Prijonof75eceb2007-03-23 19:09:54 +0000681
682 PJ_LOG(4,(THIS_FILE,
683 "STUN server %.*s resolved, address is %s:%d",
684 (int)pjsua_var.ua_cfg.stun_srv.slen,
685 pjsua_var.ua_cfg.stun_srv.ptr,
686 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
687 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
Benny Prijonoc97608e2007-03-23 16:34:20 +0000688 }
689 }
690 return pjsua_var.stun_status;
691
692 } else if (pjsua_var.stun_status == PJ_EPENDING) {
693 /* STUN server resolution has been started, wait for the
694 * result.
695 */
696 pj_assert(!"Should not happen");
697 return PJ_EBUG;
698
699 } else {
700 /* STUN server has been resolved, return the status */
701 return pjsua_var.stun_status;
702 }
703}
704
705/*
Benny Prijono268ca612006-02-07 12:34:11 +0000706 * Destroy pjsua.
707 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000708PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +0000709{
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000710 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +0000711
712 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000713 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +0000714
715 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000716 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
717 if (pjsua_var.thread[i]) {
718 pj_thread_join(pjsua_var.thread[i]);
719 pj_thread_destroy(pjsua_var.thread[i]);
720 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +0000721 }
Benny Prijono268ca612006-02-07 12:34:11 +0000722 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000723
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000724 if (pjsua_var.endpt) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000725 /* Terminate all calls. */
726 pjsua_call_hangup_all();
727
728 /* Terminate all presence subscriptions. */
729 pjsua_pres_shutdown();
730
731 /* Unregister, if required: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000732 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
733 if (!pjsua_var.acc[i].valid)
734 continue;
735
736 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000737 pjsua_acc_set_registration(i, PJ_FALSE);
738 }
739 }
740
741 /* Wait for some time to allow unregistration to complete: */
Benny Prijono9fc735d2006-05-28 14:58:12 +0000742 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
743 busy_sleep(1000);
744 }
Benny Prijono834aee32006-02-19 01:38:06 +0000745
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000746 /* Destroy media */
747 pjsua_media_subsys_destroy();
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000748
Benny Prijono268ca612006-02-07 12:34:11 +0000749 /* Destroy endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000750 if (pjsua_var.endpt) {
751 pjsip_endpt_destroy(pjsua_var.endpt);
752 pjsua_var.endpt = NULL;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000753 }
Benny Prijono268ca612006-02-07 12:34:11 +0000754
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000755 /* Destroy mutex */
756 if (pjsua_var.mutex) {
757 pj_mutex_destroy(pjsua_var.mutex);
758 pjsua_var.mutex = NULL;
759 }
760
761 /* Destroy pool and pool factory. */
762 if (pjsua_var.pool) {
763 pj_pool_release(pjsua_var.pool);
764 pjsua_var.pool = NULL;
765 pj_caching_pool_destroy(&pjsua_var.cp);
766 }
Benny Prijono268ca612006-02-07 12:34:11 +0000767
768
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000769 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
770
771 /* End logging */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000772 if (pjsua_var.log_file) {
773 pj_file_close(pjsua_var.log_file);
774 pjsua_var.log_file = NULL;
775 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000776
Benny Prijonoaf1bb1e2006-11-21 12:39:31 +0000777 /* Shutdown PJLIB */
778 pj_shutdown();
779
Benny Prijonof762ee72006-12-01 11:14:37 +0000780 /* Clear pjsua_var */
781 pj_bzero(&pjsua_var, sizeof(pjsua_var));
782
Benny Prijono268ca612006-02-07 12:34:11 +0000783 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000784 return PJ_SUCCESS;
785}
786
787
788/**
789 * Application is recommended to call this function after all initialization
790 * is done, so that the library can do additional checking set up
791 * additional
792 *
793 * @return PJ_SUCCESS on success, or the appropriate error code.
794 */
795PJ_DEF(pj_status_t) pjsua_start(void)
796{
797 pj_status_t status;
798
799 status = pjsua_call_subsys_start();
800 if (status != PJ_SUCCESS)
801 return status;
802
803 status = pjsua_media_subsys_start();
804 if (status != PJ_SUCCESS)
805 return status;
806
807 status = pjsua_pres_start();
808 if (status != PJ_SUCCESS)
809 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000810
811 return PJ_SUCCESS;
812}
813
Benny Prijono9fc735d2006-05-28 14:58:12 +0000814
815/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000816 * Poll pjsua for events, and if necessary block the caller thread for
817 * the specified maximum interval (in miliseconds).
818 */
819PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
820{
821 unsigned count = 0;
822 pj_time_val tv;
823 pj_status_t status;
824
825 tv.sec = 0;
826 tv.msec = msec_timeout;
827 pj_time_val_normalize(&tv);
828
829 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
830
831 if (status != PJ_SUCCESS)
832 return -status;
833
834 return count;
835}
836
837
838/*
839 * Create memory pool.
840 */
841PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
842 pj_size_t increment)
843{
844 /* Pool factory is thread safe, no need to lock */
845 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
846 NULL);
847}
848
849
850/*
851 * Internal function to get SIP endpoint instance of pjsua, which is
852 * needed for example to register module, create transports, etc.
853 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000854 */
855PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
856{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000857 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000858}
859
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000860/*
861 * Internal function to get media endpoint instance.
862 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000863 */
864PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
865{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000866 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000867}
868
Benny Prijono97b87172006-08-24 14:25:14 +0000869/*
870 * Internal function to get PJSUA pool factory.
871 */
872PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
873{
874 return &pjsua_var.cp.factory;
875}
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000876
877/*****************************************************************************
878 * PJSUA SIP Transport API.
879 */
880
881/*
882 * Create and initialize SIP socket (and possibly resolve public
883 * address via STUN, depending on config).
884 */
885static pj_status_t create_sip_udp_sock(pj_in_addr bound_addr,
886 int port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000887 pj_sock_t *p_sock,
888 pj_sockaddr_in *p_pub_addr)
889{
Benny Prijonoc97608e2007-03-23 16:34:20 +0000890 char ip_addr[32];
891 pj_str_t stun_srv;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000892 pj_sock_t sock;
893 pj_status_t status;
894
Benny Prijonoc97608e2007-03-23 16:34:20 +0000895 /* Make sure STUN server resolution has completed */
896 status = pjsua_resolve_stun_server(PJ_TRUE);
897 if (status != PJ_SUCCESS) {
898 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
899 return status;
900 }
901
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000902 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock);
903 if (status != PJ_SUCCESS) {
904 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +0000905 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000906 }
907
Benny Prijonof90ef4f2007-02-12 14:59:57 +0000908 status = pj_sock_bind_in(sock, pj_ntohl(bound_addr.s_addr),
909 (pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000910 if (status != PJ_SUCCESS) {
911 pjsua_perror(THIS_FILE, "bind() error", status);
912 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +0000913 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000914 }
915
Benny Prijonoe347cb02007-02-14 14:36:13 +0000916 /* If port is zero, get the bound port */
917 if (port == 0) {
918 pj_sockaddr_in bound_addr;
919 int namelen = sizeof(bound_addr);
920 status = pj_sock_getsockname(sock, &bound_addr, &namelen);
921 if (status != PJ_SUCCESS) {
922 pjsua_perror(THIS_FILE, "getsockname() error", status);
923 pj_sock_close(sock);
924 return status;
925 }
926
927 port = pj_ntohs(bound_addr.sin_port);
928 }
929
Benny Prijonoc97608e2007-03-23 16:34:20 +0000930 if (pjsua_var.stun_srv.addr.sa_family != 0) {
931 pj_ansi_strcpy(ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
932 stun_srv = pj_str(ip_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000933 } else {
Benny Prijonoc97608e2007-03-23 16:34:20 +0000934 stun_srv.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000935 }
936
937 /* Get the published address, either by STUN or by resolving
938 * the name of local host.
939 */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000940 if (stun_srv.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +0000941 /*
942 * STUN is specified, resolve the address with STUN.
943 */
Benny Prijono14c2b862007-02-21 00:40:05 +0000944 status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
Benny Prijonoc97608e2007-03-23 16:34:20 +0000945 &stun_srv, 3478,
946 &stun_srv, 3478,
947 p_pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000948 if (status != PJ_SUCCESS) {
949 pjsua_perror(THIS_FILE, "Error resolving with STUN", status);
950 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +0000951 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000952 }
953
Benny Prijono0a5cad82006-09-26 13:21:02 +0000954 } else if (p_pub_addr->sin_addr.s_addr != 0) {
955 /*
956 * Public address is already specified, no need to resolve the
957 * address, only set the port.
958 */
Benny Prijonoe347cb02007-02-14 14:36:13 +0000959 if (p_pub_addr->sin_port == 0)
960 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijono0a5cad82006-09-26 13:21:02 +0000961
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000962 } else {
963
Benny Prijono594e4c52006-09-14 18:51:01 +0000964 pj_bzero(p_pub_addr, sizeof(pj_sockaddr_in));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000965
Benny Prijono594e4c52006-09-14 18:51:01 +0000966 status = pj_gethostip(&p_pub_addr->sin_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000967 if (status != PJ_SUCCESS) {
Benny Prijonob43bad72007-01-20 05:11:08 +0000968 pjsua_perror(THIS_FILE, "Unable to get local host IP", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000969 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +0000970 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000971 }
972
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000973 p_pub_addr->sin_family = PJ_AF_INET;
974 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000975 }
976
977 *p_sock = sock;
978
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000979 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
980 pj_inet_ntoa(p_pub_addr->sin_addr),
981 (int)pj_ntohs(p_pub_addr->sin_port)));
982
Benny Prijonod8410532006-06-15 11:04:33 +0000983 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000984}
985
986
987/*
988 * Create SIP transport.
989 */
990PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
991 const pjsua_transport_config *cfg,
992 pjsua_transport_id *p_id)
993{
994 pjsip_transport *tp;
995 unsigned id;
996 pj_status_t status;
997
998 PJSUA_LOCK();
999
1000 /* Find empty transport slot */
1001 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001002 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001003 break;
1004 }
1005
1006 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1007 status = PJ_ETOOMANY;
1008 pjsua_perror(THIS_FILE, "Error creating transport", status);
1009 goto on_return;
1010 }
1011
1012 /* Create the transport */
1013 if (type == PJSIP_TRANSPORT_UDP) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001014 /*
1015 * Create UDP transport.
1016 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001017 pjsua_transport_config config;
Benny Prijonod8410532006-06-15 11:04:33 +00001018 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001019 pj_sockaddr_in bound_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001020 pj_sockaddr_in pub_addr;
1021 pjsip_host_port addr_name;
1022
1023 /* Supply default config if it's not specified */
1024 if (cfg == NULL) {
1025 pjsua_transport_config_default(&config);
1026 cfg = &config;
1027 }
1028
Benny Prijono0a5cad82006-09-26 13:21:02 +00001029 /* Initialize bound address, if any */
1030 bound_addr.sin_addr.s_addr = PJ_INADDR_ANY;
1031 if (cfg->bound_addr.slen) {
1032 status = pj_sockaddr_in_set_str_addr(&bound_addr,&cfg->bound_addr);
1033 if (status != PJ_SUCCESS) {
1034 pjsua_perror(THIS_FILE,
1035 "Unable to resolve transport bound address",
1036 status);
1037 goto on_return;
1038 }
1039 }
1040
1041 /* Initialize the public address from the config, if any */
1042 pj_sockaddr_in_init(&pub_addr, NULL, (pj_uint16_t)cfg->port);
1043 if (cfg->public_addr.slen) {
1044 status = pj_sockaddr_in_set_str_addr(&pub_addr, &cfg->public_addr);
1045 if (status != PJ_SUCCESS) {
1046 pjsua_perror(THIS_FILE,
1047 "Unable to resolve transport public address",
1048 status);
1049 goto on_return;
1050 }
1051 }
1052
1053 /* Create the socket and possibly resolve the address with STUN
1054 * (only when public address is not specified).
1055 */
1056 status = create_sip_udp_sock(bound_addr.sin_addr, cfg->port,
Benny Prijono0a5cad82006-09-26 13:21:02 +00001057 &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001058 if (status != PJ_SUCCESS)
1059 goto on_return;
1060
1061 addr_name.host = pj_str(pj_inet_ntoa(pub_addr.sin_addr));
1062 addr_name.port = pj_ntohs(pub_addr.sin_port);
1063
1064 /* Create UDP transport */
1065 status = pjsip_udp_transport_attach( pjsua_var.endpt, sock,
1066 &addr_name, 1,
1067 &tp);
1068 if (status != PJ_SUCCESS) {
1069 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
1070 status);
1071 pj_sock_close(sock);
1072 goto on_return;
1073 }
1074
Benny Prijonoe93e2872006-06-28 16:46:49 +00001075
1076 /* Save the transport */
1077 pjsua_var.tpdata[id].type = type;
1078 pjsua_var.tpdata[id].local_name = tp->local_name;
1079 pjsua_var.tpdata[id].data.tp = tp;
1080
1081 } else if (type == PJSIP_TRANSPORT_TCP) {
1082 /*
1083 * Create TCP transport.
1084 */
1085 pjsua_transport_config config;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001086 pjsip_host_port a_name;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001087 pjsip_tpfactory *tcp;
1088 pj_sockaddr_in local_addr;
1089
1090 /* Supply default config if it's not specified */
1091 if (cfg == NULL) {
1092 pjsua_transport_config_default(&config);
1093 cfg = &config;
1094 }
1095
1096 /* Init local address */
1097 pj_sockaddr_in_init(&local_addr, 0, 0);
1098
1099 if (cfg->port)
1100 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1101
Benny Prijono0a5cad82006-09-26 13:21:02 +00001102 if (cfg->bound_addr.slen) {
1103 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1104 if (status != PJ_SUCCESS) {
1105 pjsua_perror(THIS_FILE,
1106 "Unable to resolve transport bound address",
1107 status);
1108 goto on_return;
1109 }
1110 }
1111
1112 /* Init published name */
1113 pj_bzero(&a_name, sizeof(pjsip_host_port));
1114 if (cfg->public_addr.slen)
1115 a_name.host = cfg->public_addr;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001116
1117 /* Create the TCP transport */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001118 status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr,
1119 &a_name, 1, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001120
1121 if (status != PJ_SUCCESS) {
1122 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1123 status);
1124 goto on_return;
1125 }
1126
1127 /* Save the transport */
1128 pjsua_var.tpdata[id].type = type;
1129 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1130 pjsua_var.tpdata[id].data.factory = tcp;
1131
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001132#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1133 } else if (type == PJSIP_TRANSPORT_TLS) {
1134 /*
1135 * Create TLS transport.
1136 */
Benny Prijonof3bbc132006-12-25 06:43:59 +00001137 /*
1138 * Create TCP transport.
1139 */
1140 pjsua_transport_config config;
1141 pjsip_host_port a_name;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001142 pjsip_tpfactory *tls;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001143 pj_sockaddr_in local_addr;
1144
1145 /* Supply default config if it's not specified */
1146 if (cfg == NULL) {
1147 pjsua_transport_config_default(&config);
1148 config.port = 5061;
1149 cfg = &config;
1150 }
1151
1152 /* Init local address */
1153 pj_sockaddr_in_init(&local_addr, 0, 0);
1154
1155 if (cfg->port)
1156 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1157
1158 if (cfg->bound_addr.slen) {
1159 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1160 if (status != PJ_SUCCESS) {
1161 pjsua_perror(THIS_FILE,
1162 "Unable to resolve transport bound address",
1163 status);
1164 goto on_return;
1165 }
1166 }
1167
1168 /* Init published name */
1169 pj_bzero(&a_name, sizeof(pjsip_host_port));
1170 if (cfg->public_addr.slen)
1171 a_name.host = cfg->public_addr;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001172
1173 status = pjsip_tls_transport_start(pjsua_var.endpt,
Benny Prijonof3bbc132006-12-25 06:43:59 +00001174 &cfg->tls_setting,
1175 &local_addr, &a_name, 1, &tls);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001176 if (status != PJ_SUCCESS) {
1177 pjsua_perror(THIS_FILE, "Error creating SIP TLS listener",
1178 status);
1179 goto on_return;
1180 }
1181
1182 /* Save the transport */
1183 pjsua_var.tpdata[id].type = type;
1184 pjsua_var.tpdata[id].local_name = tls->addr_name;
1185 pjsua_var.tpdata[id].data.factory = tls;
1186#endif
1187
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001188 } else {
1189 status = PJSIP_EUNSUPTRANSPORT;
1190 pjsua_perror(THIS_FILE, "Error creating transport", status);
1191 goto on_return;
1192 }
1193
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001194
1195 /* Return the ID */
1196 if (p_id) *p_id = id;
1197
1198 status = PJ_SUCCESS;
1199
1200on_return:
1201
1202 PJSUA_UNLOCK();
1203
Benny Prijonod8410532006-06-15 11:04:33 +00001204 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001205}
1206
1207
1208/*
1209 * Register transport that has been created by application.
1210 */
1211PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1212 pjsua_transport_id *p_id)
1213{
1214 unsigned id;
1215
1216 PJSUA_LOCK();
1217
1218 /* Find empty transport slot */
1219 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001220 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001221 break;
1222 }
1223
1224 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1225 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1226 PJSUA_UNLOCK();
1227 return PJ_ETOOMANY;
1228 }
1229
1230 /* Save the transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001231 pjsua_var.tpdata[id].type = tp->key.type;
1232 pjsua_var.tpdata[id].local_name = tp->local_name;
1233 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001234
1235 /* Return the ID */
1236 if (p_id) *p_id = id;
1237
1238 PJSUA_UNLOCK();
1239
1240 return PJ_SUCCESS;
1241}
1242
1243
1244/*
1245 * Enumerate all transports currently created in the system.
1246 */
1247PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1248 unsigned *p_count )
1249{
1250 unsigned i, count;
1251
1252 PJSUA_LOCK();
1253
1254 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
1255 ++i)
1256 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001257 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001258 continue;
1259
1260 id[count++] = i;
1261 }
1262
1263 *p_count = count;
1264
1265 PJSUA_UNLOCK();
1266
1267 return PJ_SUCCESS;
1268}
1269
1270
1271/*
1272 * Get information about transports.
1273 */
1274PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
1275 pjsua_transport_info *info)
1276{
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001277 pjsua_transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00001278 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001279
Benny Prijonoac623b32006-07-03 15:19:31 +00001280 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001281
1282 /* Make sure id is in range. */
1283 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1284
1285 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001286 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001287
1288 PJSUA_LOCK();
1289
Benny Prijonoe93e2872006-06-28 16:46:49 +00001290 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
1291
1292 pjsip_transport *tp = t->data.tp;
1293
1294 if (tp == NULL) {
1295 PJSUA_UNLOCK();
1296 return PJ_EINVALIDOP;
1297 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001298
Benny Prijonoe93e2872006-06-28 16:46:49 +00001299 info->id = id;
1300 info->type = tp->key.type;
1301 info->type_name = pj_str(tp->type_name);
1302 info->info = pj_str(tp->info);
1303 info->flag = tp->flag;
1304 info->addr_len = tp->addr_len;
1305 info->local_addr = tp->local_addr;
1306 info->local_name = tp->local_name;
1307 info->usage_count = pj_atomic_get(tp->ref_cnt);
1308
Benny Prijono0a5cad82006-09-26 13:21:02 +00001309 status = PJ_SUCCESS;
1310
Benny Prijonoe93e2872006-06-28 16:46:49 +00001311 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
1312
1313 pjsip_tpfactory *factory = t->data.factory;
1314
1315 if (factory == NULL) {
1316 PJSUA_UNLOCK();
1317 return PJ_EINVALIDOP;
1318 }
1319
1320 info->id = id;
1321 info->type = t->type;
1322 info->type_name = pj_str("TCP");
1323 info->info = pj_str("TCP transport");
1324 info->flag = factory->flag;
1325 info->addr_len = sizeof(factory->local_addr);
1326 info->local_addr = factory->local_addr;
1327 info->local_name = factory->addr_name;
1328 info->usage_count = 0;
1329
Benny Prijono0a5cad82006-09-26 13:21:02 +00001330 status = PJ_SUCCESS;
1331
1332 } else {
1333 pj_assert(!"Unsupported transport");
1334 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001335 }
1336
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001337
1338 PJSUA_UNLOCK();
1339
Benny Prijono0a5cad82006-09-26 13:21:02 +00001340 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001341}
1342
1343
1344/*
1345 * Disable a transport or re-enable it.
1346 */
1347PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
1348 pj_bool_t enabled)
1349{
1350 /* Make sure id is in range. */
1351 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1352
1353 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001354 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001355
1356
1357 /* To be done!! */
1358 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001359 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001360
1361 return PJ_EINVALIDOP;
1362}
1363
1364
1365/*
1366 * Close the transport.
1367 */
1368PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
1369 pj_bool_t force )
1370{
Benny Prijono5ff61872007-02-01 03:37:11 +00001371 pj_status_t status;
1372
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001373 /* Make sure id is in range. */
1374 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1375
1376 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001377 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001378
Benny Prijono0a5cad82006-09-26 13:21:02 +00001379 /* Note: destroy() may not work if there are objects still referencing
1380 * the transport.
1381 */
1382 if (force) {
1383 switch (pjsua_var.tpdata[id].type) {
1384 case PJSIP_TRANSPORT_UDP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001385 status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
1386 if (status != PJ_SUCCESS)
1387 return status;
1388 status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
1389 if (status != PJ_SUCCESS)
1390 return status;
1391 break;
1392
1393 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001394 case PJSIP_TRANSPORT_TCP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001395 /* This will close the TCP listener, but existing TCP/TLS
1396 * connections (if any) will still linger
1397 */
1398 status = (*pjsua_var.tpdata[id].data.factory->destroy)
1399 (pjsua_var.tpdata[id].data.factory);
1400 if (status != PJ_SUCCESS)
1401 return status;
1402
Benny Prijono0a5cad82006-09-26 13:21:02 +00001403 break;
Benny Prijono5ff61872007-02-01 03:37:11 +00001404
Benny Prijono1ebd6142006-10-19 15:48:02 +00001405 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001406 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001407 }
1408
1409 } else {
Benny Prijono5ff61872007-02-01 03:37:11 +00001410 /* If force is not specified, transports will be closed at their
1411 * convenient time. However this will leak PJSUA-API transport
1412 * descriptors as PJSUA-API wouldn't know when exactly the
1413 * transport is closed thus it can't cleanup PJSUA transport
1414 * descriptor.
1415 */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001416 switch (pjsua_var.tpdata[id].type) {
1417 case PJSIP_TRANSPORT_UDP:
1418 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
Benny Prijono5ff61872007-02-01 03:37:11 +00001419 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001420 case PJSIP_TRANSPORT_TCP:
1421 return (*pjsua_var.tpdata[id].data.factory->destroy)
1422 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001423 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001424 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001425 }
1426 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001427
Benny Prijono5ff61872007-02-01 03:37:11 +00001428 /* Cleanup pjsua data when force is applied */
1429 if (force) {
1430 pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED;
1431 pjsua_var.tpdata[id].data.ptr = NULL;
1432 }
1433
1434 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001435}
1436
1437
1438/*
1439 * Add additional headers etc in msg_data specified by application
1440 * when sending requests.
1441 */
1442void pjsua_process_msg_data(pjsip_tx_data *tdata,
1443 const pjsua_msg_data *msg_data)
1444{
1445 pj_bool_t allow_body;
1446 const pjsip_hdr *hdr;
1447
Benny Prijono56315612006-07-18 14:39:40 +00001448 /* Always add User-Agent */
1449 if (pjsua_var.ua_cfg.user_agent.slen &&
1450 tdata->msg->type == PJSIP_REQUEST_MSG)
1451 {
1452 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1453 pjsip_hdr *h;
1454 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
1455 &STR_USER_AGENT,
1456 &pjsua_var.ua_cfg.user_agent);
1457 pjsip_msg_add_hdr(tdata->msg, h);
1458 }
1459
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001460 if (!msg_data)
1461 return;
1462
1463 hdr = msg_data->hdr_list.next;
1464 while (hdr && hdr != &msg_data->hdr_list) {
1465 pjsip_hdr *new_hdr;
1466
1467 new_hdr = pjsip_hdr_clone(tdata->pool, hdr);
1468 pjsip_msg_add_hdr(tdata->msg, new_hdr);
1469
1470 hdr = hdr->next;
1471 }
1472
1473 allow_body = (tdata->msg->body == NULL);
1474
1475 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
1476 pjsip_media_type ctype;
1477 pjsip_msg_body *body;
1478
1479 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
1480 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
1481 &msg_data->msg_body);
1482 tdata->msg->body = body;
1483 }
1484}
1485
1486
1487/*
1488 * Add route_set to outgoing requests
1489 */
1490void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
1491 const pjsip_route_hdr *route_set )
1492{
1493 const pjsip_route_hdr *r;
1494
1495 r = route_set->next;
1496 while (r != route_set) {
1497 pjsip_route_hdr *new_r;
1498
1499 new_r = pjsip_hdr_clone(tdata->pool, r);
1500 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
1501
1502 r = r->next;
1503 }
1504}
1505
1506
1507/*
1508 * Simple version of MIME type parsing (it doesn't support parameters)
1509 */
1510void pjsua_parse_media_type( pj_pool_t *pool,
1511 const pj_str_t *mime,
1512 pjsip_media_type *media_type)
1513{
1514 pj_str_t tmp;
1515 char *pos;
1516
Benny Prijonoac623b32006-07-03 15:19:31 +00001517 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001518
1519 pj_strdup_with_null(pool, &tmp, mime);
1520
1521 pos = pj_strchr(&tmp, '/');
1522 if (pos) {
1523 media_type->type.ptr = tmp.ptr;
1524 media_type->type.slen = (pos-tmp.ptr);
1525 media_type->subtype.ptr = pos+1;
1526 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
1527 } else {
1528 media_type->type = tmp;
1529 }
1530}
1531
1532
1533/*
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001534 * Internal function to init transport selector from transport id.
1535 */
1536void pjsua_init_tpselector(pjsua_transport_id tp_id,
1537 pjsip_tpselector *sel)
1538{
1539 pjsua_transport_data *tpdata;
1540 unsigned flag;
1541
1542 pj_bzero(sel, sizeof(*sel));
1543 if (tp_id == PJSUA_INVALID_ID)
1544 return;
1545
1546 pj_assert(tp_id >= 0 && tp_id < PJ_ARRAY_SIZE(pjsua_var.tpdata));
1547 tpdata = &pjsua_var.tpdata[tp_id];
1548
1549 flag = pjsip_transport_get_flag_from_type(tpdata->type);
1550
1551 if (flag & PJSIP_TRANSPORT_DATAGRAM) {
1552 sel->type = PJSIP_TPSELECTOR_TRANSPORT;
1553 sel->u.transport = tpdata->data.tp;
1554 } else {
1555 sel->type = PJSIP_TPSELECTOR_LISTENER;
1556 sel->u.listener = tpdata->data.factory;
1557 }
1558}
1559
1560
1561/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001562 * Verify that valid SIP url is given.
1563 */
1564PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
1565{
1566 pjsip_uri *p;
1567 pj_pool_t *pool;
1568 char *url;
1569 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
1570
1571 if (!len) return -1;
1572
1573 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
1574 if (!pool) return -1;
1575
1576 url = pj_pool_alloc(pool, len+1);
1577 pj_ansi_strcpy(url, c_url);
1578
1579 p = pjsip_parse_uri(pool, url, len, 0);
1580 if (!p || pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0)
1581 p = NULL;
1582
1583 pj_pool_release(pool);
1584 return p ? 0 : -1;
1585}